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 were 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 /* The next parsing context in the stack. */
1198 struct cp_parser_context *next;
1199 } cp_parser_context;
1203 /* Constructors and destructors. */
1205 static cp_parser_context *cp_parser_context_new
1206 PARAMS ((cp_parser_context *));
1208 /* Class variables. */
1210 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1212 /* Constructors and destructors. */
1214 /* Construct a new context. The context below this one on the stack
1215 is given by NEXT. */
1217 static cp_parser_context *
1218 cp_parser_context_new (next)
1219 cp_parser_context *next;
1221 cp_parser_context *context;
1223 /* Allocate the storage. */
1224 if (cp_parser_context_free_list != NULL)
1226 /* Pull the first entry from the free list. */
1227 context = cp_parser_context_free_list;
1228 cp_parser_context_free_list = context->next;
1229 memset ((char *)context, 0, sizeof (*context));
1232 context = ((cp_parser_context *)
1233 ggc_alloc_cleared (sizeof (cp_parser_context)));
1234 /* No errors have occurred yet in this context. */
1235 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1236 /* If this is not the bottomost context, copy information that we
1237 need from the previous context. */
1240 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1241 expression, then we are parsing one in this context, too. */
1242 context->object_type = next->object_type;
1243 /* Thread the stack. */
1244 context->next = next;
1250 /* The cp_parser structure represents the C++ parser. */
1252 typedef struct cp_parser GTY(())
1254 /* The lexer from which we are obtaining tokens. */
1257 /* The scope in which names should be looked up. If NULL_TREE, then
1258 we look up names in the scope that is currently open in the
1259 source program. If non-NULL, this is either a TYPE or
1260 NAMESPACE_DECL for the scope in which we should look.
1262 This value is not cleared automatically after a name is looked
1263 up, so we must be careful to clear it before starting a new look
1264 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1265 will look up `Z' in the scope of `X', rather than the current
1266 scope.) Unfortunately, it is difficult to tell when name lookup
1267 is complete, because we sometimes peek at a token, look it up,
1268 and then decide not to consume it. */
1271 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1272 last lookup took place. OBJECT_SCOPE is used if an expression
1273 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1274 respectively. QUALIFYING_SCOPE is used for an expression of the
1275 form "X::Y"; it refers to X. */
1277 tree qualifying_scope;
1279 /* A stack of parsing contexts. All but the bottom entry on the
1280 stack will be tentative contexts.
1282 We parse tentatively in order to determine which construct is in
1283 use in some situations. For example, in order to determine
1284 whether a statement is an expression-statement or a
1285 declaration-statement we parse it tentatively as a
1286 declaration-statement. If that fails, we then reparse the same
1287 token stream as an expression-statement. */
1288 cp_parser_context *context;
1290 /* True if we are parsing GNU C++. If this flag is not set, then
1291 GNU extensions are not recognized. */
1292 bool allow_gnu_extensions_p;
1294 /* TRUE if the `>' token should be interpreted as the greater-than
1295 operator. FALSE if it is the end of a template-id or
1296 template-parameter-list. */
1297 bool greater_than_is_operator_p;
1299 /* TRUE if default arguments are allowed within a parameter list
1300 that starts at this point. FALSE if only a gnu extension makes
1301 them permissable. */
1302 bool default_arg_ok_p;
1304 /* TRUE if we are parsing an integral constant-expression. See
1305 [expr.const] for a precise definition. */
1306 /* FIXME: Need to implement code that checks this flag. */
1307 bool constant_expression_p;
1309 /* TRUE if local variable names and `this' are forbidden in the
1311 bool local_variables_forbidden_p;
1313 /* TRUE if the declaration we are parsing is part of a
1314 linkage-specification of the form `extern string-literal
1316 bool in_unbraced_linkage_specification_p;
1318 /* TRUE if we are presently parsing a declarator, after the
1319 direct-declarator. */
1320 bool in_declarator_p;
1322 /* If non-NULL, then we are parsing a construct where new type
1323 definitions are not permitted. The string stored here will be
1324 issued as an error message if a type is defined. */
1325 const char *type_definition_forbidden_message;
1327 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1328 but may not have been parsed. These functions are friends of
1329 members defined within a class-specification; they are not
1330 procssed until the class is complete. The active queue is at the
1333 Within each queue, functions appear in the reverse order that
1334 they appeared in the source. Each TREE_VALUE is a
1335 FUNCTION_DECL of TEMPLATE_DECL corresponding to a member
1337 tree unparsed_functions_queues;
1339 /* The number of classes whose definitions are currently in
1341 unsigned num_classes_being_defined;
1343 /* The number of template parameter lists that apply directly to the
1344 current declaration. */
1345 unsigned num_template_parameter_lists;
1348 /* The type of a function that parses some kind of expression */
1349 typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1353 /* Constructors and destructors. */
1355 static cp_parser *cp_parser_new
1358 /* Routines to parse various constructs.
1360 Those that return `tree' will return the error_mark_node (rather
1361 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1362 Sometimes, they will return an ordinary node if error-recovery was
1363 attempted, even though a parse error occurrred. So, to check
1364 whether or not a parse error occurred, you should always use
1365 cp_parser_error_occurred. If the construct is optional (indicated
1366 either by an `_opt' in the name of the function that does the
1367 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1368 the construct is not present. */
1370 /* Lexical conventions [gram.lex] */
1372 static tree cp_parser_identifier
1373 PARAMS ((cp_parser *));
1375 /* Basic concepts [gram.basic] */
1377 static bool cp_parser_translation_unit
1378 PARAMS ((cp_parser *));
1380 /* Expressions [gram.expr] */
1382 static tree cp_parser_primary_expression
1383 (cp_parser *, cp_parser_id_kind *, tree *);
1384 static tree cp_parser_id_expression
1385 PARAMS ((cp_parser *, bool, bool, bool *));
1386 static tree cp_parser_unqualified_id
1387 PARAMS ((cp_parser *, bool, bool));
1388 static tree cp_parser_nested_name_specifier_opt
1389 (cp_parser *, bool, bool, bool);
1390 static tree cp_parser_nested_name_specifier
1391 (cp_parser *, bool, bool, bool);
1392 static tree cp_parser_class_or_namespace_name
1393 (cp_parser *, bool, bool, bool, bool);
1394 static tree cp_parser_postfix_expression
1395 (cp_parser *, bool);
1396 static tree cp_parser_expression_list
1397 PARAMS ((cp_parser *));
1398 static void cp_parser_pseudo_destructor_name
1399 PARAMS ((cp_parser *, tree *, tree *));
1400 static tree cp_parser_unary_expression
1401 (cp_parser *, bool);
1402 static enum tree_code cp_parser_unary_operator
1403 PARAMS ((cp_token *));
1404 static tree cp_parser_new_expression
1405 PARAMS ((cp_parser *));
1406 static tree cp_parser_new_placement
1407 PARAMS ((cp_parser *));
1408 static tree cp_parser_new_type_id
1409 PARAMS ((cp_parser *));
1410 static tree cp_parser_new_declarator_opt
1411 PARAMS ((cp_parser *));
1412 static tree cp_parser_direct_new_declarator
1413 PARAMS ((cp_parser *));
1414 static tree cp_parser_new_initializer
1415 PARAMS ((cp_parser *));
1416 static tree cp_parser_delete_expression
1417 PARAMS ((cp_parser *));
1418 static tree cp_parser_cast_expression
1419 (cp_parser *, bool);
1420 static tree cp_parser_pm_expression
1421 PARAMS ((cp_parser *));
1422 static tree cp_parser_multiplicative_expression
1423 PARAMS ((cp_parser *));
1424 static tree cp_parser_additive_expression
1425 PARAMS ((cp_parser *));
1426 static tree cp_parser_shift_expression
1427 PARAMS ((cp_parser *));
1428 static tree cp_parser_relational_expression
1429 PARAMS ((cp_parser *));
1430 static tree cp_parser_equality_expression
1431 PARAMS ((cp_parser *));
1432 static tree cp_parser_and_expression
1433 PARAMS ((cp_parser *));
1434 static tree cp_parser_exclusive_or_expression
1435 PARAMS ((cp_parser *));
1436 static tree cp_parser_inclusive_or_expression
1437 PARAMS ((cp_parser *));
1438 static tree cp_parser_logical_and_expression
1439 PARAMS ((cp_parser *));
1440 static tree cp_parser_logical_or_expression
1441 PARAMS ((cp_parser *));
1442 static tree cp_parser_conditional_expression
1443 PARAMS ((cp_parser *));
1444 static tree cp_parser_question_colon_clause
1445 PARAMS ((cp_parser *, tree));
1446 static tree cp_parser_assignment_expression
1447 PARAMS ((cp_parser *));
1448 static enum tree_code cp_parser_assignment_operator_opt
1449 PARAMS ((cp_parser *));
1450 static tree cp_parser_expression
1451 PARAMS ((cp_parser *));
1452 static tree cp_parser_constant_expression
1453 PARAMS ((cp_parser *));
1455 /* Statements [gram.stmt.stmt] */
1457 static void cp_parser_statement
1458 PARAMS ((cp_parser *));
1459 static tree cp_parser_labeled_statement
1460 PARAMS ((cp_parser *));
1461 static tree cp_parser_expression_statement
1462 PARAMS ((cp_parser *));
1463 static tree cp_parser_compound_statement
1465 static void cp_parser_statement_seq_opt
1466 PARAMS ((cp_parser *));
1467 static tree cp_parser_selection_statement
1468 PARAMS ((cp_parser *));
1469 static tree cp_parser_condition
1470 PARAMS ((cp_parser *));
1471 static tree cp_parser_iteration_statement
1472 PARAMS ((cp_parser *));
1473 static void cp_parser_for_init_statement
1474 PARAMS ((cp_parser *));
1475 static tree cp_parser_jump_statement
1476 PARAMS ((cp_parser *));
1477 static void cp_parser_declaration_statement
1478 PARAMS ((cp_parser *));
1480 static tree cp_parser_implicitly_scoped_statement
1481 PARAMS ((cp_parser *));
1482 static void cp_parser_already_scoped_statement
1483 PARAMS ((cp_parser *));
1485 /* Declarations [gram.dcl.dcl] */
1487 static void cp_parser_declaration_seq_opt
1488 PARAMS ((cp_parser *));
1489 static void cp_parser_declaration
1490 PARAMS ((cp_parser *));
1491 static void cp_parser_block_declaration
1492 PARAMS ((cp_parser *, bool));
1493 static void cp_parser_simple_declaration
1494 PARAMS ((cp_parser *, bool));
1495 static tree cp_parser_decl_specifier_seq
1496 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1497 static tree cp_parser_storage_class_specifier_opt
1498 PARAMS ((cp_parser *));
1499 static tree cp_parser_function_specifier_opt
1500 PARAMS ((cp_parser *));
1501 static tree cp_parser_type_specifier
1502 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1503 static tree cp_parser_simple_type_specifier
1504 PARAMS ((cp_parser *, cp_parser_flags));
1505 static tree cp_parser_type_name
1506 PARAMS ((cp_parser *));
1507 static tree cp_parser_elaborated_type_specifier
1508 PARAMS ((cp_parser *, bool, bool));
1509 static tree cp_parser_enum_specifier
1510 PARAMS ((cp_parser *));
1511 static void cp_parser_enumerator_list
1512 PARAMS ((cp_parser *, tree));
1513 static void cp_parser_enumerator_definition
1514 PARAMS ((cp_parser *, tree));
1515 static tree cp_parser_namespace_name
1516 PARAMS ((cp_parser *));
1517 static void cp_parser_namespace_definition
1518 PARAMS ((cp_parser *));
1519 static void cp_parser_namespace_body
1520 PARAMS ((cp_parser *));
1521 static tree cp_parser_qualified_namespace_specifier
1522 PARAMS ((cp_parser *));
1523 static void cp_parser_namespace_alias_definition
1524 PARAMS ((cp_parser *));
1525 static void cp_parser_using_declaration
1526 PARAMS ((cp_parser *));
1527 static void cp_parser_using_directive
1528 PARAMS ((cp_parser *));
1529 static void cp_parser_asm_definition
1530 PARAMS ((cp_parser *));
1531 static void cp_parser_linkage_specification
1532 PARAMS ((cp_parser *));
1534 /* Declarators [gram.dcl.decl] */
1536 static tree cp_parser_init_declarator
1537 PARAMS ((cp_parser *, tree, tree, bool, bool, bool *));
1538 static tree cp_parser_declarator
1539 PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
1540 static tree cp_parser_direct_declarator
1541 PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
1542 static enum tree_code cp_parser_ptr_operator
1543 PARAMS ((cp_parser *, tree *, tree *));
1544 static tree cp_parser_cv_qualifier_seq_opt
1545 PARAMS ((cp_parser *));
1546 static tree cp_parser_cv_qualifier_opt
1547 PARAMS ((cp_parser *));
1548 static tree cp_parser_declarator_id
1549 PARAMS ((cp_parser *));
1550 static tree cp_parser_type_id
1551 PARAMS ((cp_parser *));
1552 static tree cp_parser_type_specifier_seq
1553 PARAMS ((cp_parser *));
1554 static tree cp_parser_parameter_declaration_clause
1555 PARAMS ((cp_parser *));
1556 static tree cp_parser_parameter_declaration_list
1557 PARAMS ((cp_parser *));
1558 static tree cp_parser_parameter_declaration
1559 PARAMS ((cp_parser *, bool));
1560 static tree cp_parser_function_definition
1561 PARAMS ((cp_parser *, bool *));
1562 static void cp_parser_function_body
1564 static tree cp_parser_initializer
1565 PARAMS ((cp_parser *, bool *));
1566 static tree cp_parser_initializer_clause
1567 PARAMS ((cp_parser *));
1568 static tree cp_parser_initializer_list
1569 PARAMS ((cp_parser *));
1571 static bool cp_parser_ctor_initializer_opt_and_function_body
1574 /* Classes [gram.class] */
1576 static tree cp_parser_class_name
1577 (cp_parser *, bool, bool, bool, bool, bool, bool);
1578 static tree cp_parser_class_specifier
1579 PARAMS ((cp_parser *));
1580 static tree cp_parser_class_head
1581 PARAMS ((cp_parser *, bool *));
1582 static enum tag_types cp_parser_class_key
1583 PARAMS ((cp_parser *));
1584 static void cp_parser_member_specification_opt
1585 PARAMS ((cp_parser *));
1586 static void cp_parser_member_declaration
1587 PARAMS ((cp_parser *));
1588 static tree cp_parser_pure_specifier
1589 PARAMS ((cp_parser *));
1590 static tree cp_parser_constant_initializer
1591 PARAMS ((cp_parser *));
1593 /* Derived classes [gram.class.derived] */
1595 static tree cp_parser_base_clause
1596 PARAMS ((cp_parser *));
1597 static tree cp_parser_base_specifier
1598 PARAMS ((cp_parser *));
1600 /* Special member functions [gram.special] */
1602 static tree cp_parser_conversion_function_id
1603 PARAMS ((cp_parser *));
1604 static tree cp_parser_conversion_type_id
1605 PARAMS ((cp_parser *));
1606 static tree cp_parser_conversion_declarator_opt
1607 PARAMS ((cp_parser *));
1608 static bool cp_parser_ctor_initializer_opt
1609 PARAMS ((cp_parser *));
1610 static void cp_parser_mem_initializer_list
1611 PARAMS ((cp_parser *));
1612 static tree cp_parser_mem_initializer
1613 PARAMS ((cp_parser *));
1614 static tree cp_parser_mem_initializer_id
1615 PARAMS ((cp_parser *));
1617 /* Overloading [gram.over] */
1619 static tree cp_parser_operator_function_id
1620 PARAMS ((cp_parser *));
1621 static tree cp_parser_operator
1622 PARAMS ((cp_parser *));
1624 /* Templates [gram.temp] */
1626 static void cp_parser_template_declaration
1627 PARAMS ((cp_parser *, bool));
1628 static tree cp_parser_template_parameter_list
1629 PARAMS ((cp_parser *));
1630 static tree cp_parser_template_parameter
1631 PARAMS ((cp_parser *));
1632 static tree cp_parser_type_parameter
1633 PARAMS ((cp_parser *));
1634 static tree cp_parser_template_id
1635 PARAMS ((cp_parser *, bool, bool));
1636 static tree cp_parser_template_name
1637 PARAMS ((cp_parser *, bool, bool));
1638 static tree cp_parser_template_argument_list
1639 PARAMS ((cp_parser *));
1640 static tree cp_parser_template_argument
1641 PARAMS ((cp_parser *));
1642 static void cp_parser_explicit_instantiation
1643 PARAMS ((cp_parser *));
1644 static void cp_parser_explicit_specialization
1645 PARAMS ((cp_parser *));
1647 /* Exception handling [gram.exception] */
1649 static tree cp_parser_try_block
1650 PARAMS ((cp_parser *));
1651 static bool cp_parser_function_try_block
1652 PARAMS ((cp_parser *));
1653 static void cp_parser_handler_seq
1654 PARAMS ((cp_parser *));
1655 static void cp_parser_handler
1656 PARAMS ((cp_parser *));
1657 static tree cp_parser_exception_declaration
1658 PARAMS ((cp_parser *));
1659 static tree cp_parser_throw_expression
1660 PARAMS ((cp_parser *));
1661 static tree cp_parser_exception_specification_opt
1662 PARAMS ((cp_parser *));
1663 static tree cp_parser_type_id_list
1664 PARAMS ((cp_parser *));
1666 /* GNU Extensions */
1668 static tree cp_parser_asm_specification_opt
1669 PARAMS ((cp_parser *));
1670 static tree cp_parser_asm_operand_list
1671 PARAMS ((cp_parser *));
1672 static tree cp_parser_asm_clobber_list
1673 PARAMS ((cp_parser *));
1674 static tree cp_parser_attributes_opt
1675 PARAMS ((cp_parser *));
1676 static tree cp_parser_attribute_list
1677 PARAMS ((cp_parser *));
1678 static bool cp_parser_extension_opt
1679 PARAMS ((cp_parser *, int *));
1680 static void cp_parser_label_declaration
1681 PARAMS ((cp_parser *));
1683 /* Utility Routines */
1685 static tree cp_parser_lookup_name
1686 PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
1687 static tree cp_parser_lookup_name_simple
1688 PARAMS ((cp_parser *, tree));
1689 static tree cp_parser_resolve_typename_type
1690 PARAMS ((cp_parser *, tree));
1691 static tree cp_parser_maybe_treat_template_as_class
1693 static bool cp_parser_check_declarator_template_parameters
1694 PARAMS ((cp_parser *, tree));
1695 static bool cp_parser_check_template_parameters
1696 PARAMS ((cp_parser *, unsigned));
1697 static tree cp_parser_binary_expression
1698 PARAMS ((cp_parser *,
1699 const cp_parser_token_tree_map,
1700 cp_parser_expression_fn));
1701 static tree cp_parser_global_scope_opt
1702 PARAMS ((cp_parser *, bool));
1703 static bool cp_parser_constructor_declarator_p
1704 (cp_parser *, bool);
1705 static tree cp_parser_function_definition_from_specifiers_and_declarator
1706 PARAMS ((cp_parser *, tree, tree, tree));
1707 static tree cp_parser_function_definition_after_declarator
1708 PARAMS ((cp_parser *, bool));
1709 static void cp_parser_template_declaration_after_export
1710 PARAMS ((cp_parser *, bool));
1711 static tree cp_parser_single_declaration
1712 PARAMS ((cp_parser *, bool, bool *));
1713 static tree cp_parser_functional_cast
1714 PARAMS ((cp_parser *, tree));
1715 static void cp_parser_late_parsing_for_member
1716 PARAMS ((cp_parser *, tree));
1717 static void cp_parser_late_parsing_default_args
1718 (cp_parser *, tree);
1719 static tree cp_parser_sizeof_operand
1720 PARAMS ((cp_parser *, enum rid));
1721 static bool cp_parser_declares_only_class_p
1722 PARAMS ((cp_parser *));
1723 static bool cp_parser_friend_p
1725 static cp_token *cp_parser_require
1726 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1727 static cp_token *cp_parser_require_keyword
1728 PARAMS ((cp_parser *, enum rid, const char *));
1729 static bool cp_parser_token_starts_function_definition_p
1730 PARAMS ((cp_token *));
1731 static bool cp_parser_next_token_starts_class_definition_p
1733 static enum tag_types cp_parser_token_is_class_key
1734 PARAMS ((cp_token *));
1735 static void cp_parser_check_class_key
1736 (enum tag_types, tree type);
1737 static bool cp_parser_optional_template_keyword
1739 static void cp_parser_pre_parsed_nested_name_specifier
1741 static void cp_parser_cache_group
1742 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1743 static void cp_parser_parse_tentatively
1744 PARAMS ((cp_parser *));
1745 static void cp_parser_commit_to_tentative_parse
1746 PARAMS ((cp_parser *));
1747 static void cp_parser_abort_tentative_parse
1748 PARAMS ((cp_parser *));
1749 static bool cp_parser_parse_definitely
1750 PARAMS ((cp_parser *));
1751 static inline bool cp_parser_parsing_tentatively
1752 PARAMS ((cp_parser *));
1753 static bool cp_parser_committed_to_tentative_parse
1754 PARAMS ((cp_parser *));
1755 static void cp_parser_error
1756 PARAMS ((cp_parser *, const char *));
1757 static bool cp_parser_simulate_error
1758 PARAMS ((cp_parser *));
1759 static void cp_parser_check_type_definition
1760 PARAMS ((cp_parser *));
1761 static bool cp_parser_skip_to_closing_parenthesis
1762 PARAMS ((cp_parser *));
1763 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1765 static void cp_parser_skip_to_end_of_statement
1766 PARAMS ((cp_parser *));
1767 static void cp_parser_skip_to_end_of_block_or_statement
1768 PARAMS ((cp_parser *));
1769 static void cp_parser_skip_to_closing_brace
1771 static void cp_parser_skip_until_found
1772 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1773 static bool cp_parser_error_occurred
1774 PARAMS ((cp_parser *));
1775 static bool cp_parser_allow_gnu_extensions_p
1776 PARAMS ((cp_parser *));
1777 static bool cp_parser_is_string_literal
1778 PARAMS ((cp_token *));
1779 static bool cp_parser_is_keyword
1780 PARAMS ((cp_token *, enum rid));
1781 static bool cp_parser_dependent_type_p
1783 static bool cp_parser_value_dependent_expression_p
1785 static bool cp_parser_type_dependent_expression_p
1787 static bool cp_parser_dependent_template_arg_p
1789 static bool cp_parser_dependent_template_id_p
1791 static bool cp_parser_dependent_template_p
1793 static tree cp_parser_scope_through_which_access_occurs
1796 /* Returns non-zero if we are parsing tentatively. */
1799 cp_parser_parsing_tentatively (parser)
1802 return parser->context->next != NULL;
1805 /* Returns non-zero if TOKEN is a string literal. */
1808 cp_parser_is_string_literal (token)
1811 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1814 /* Returns non-zero if TOKEN is the indicated KEYWORD. */
1817 cp_parser_is_keyword (token, keyword)
1821 return token->keyword == keyword;
1824 /* Returns TRUE if TYPE is dependent, in the sense of
1828 cp_parser_dependent_type_p (type)
1833 if (!processing_template_decl)
1836 /* If the type is NULL, we have not computed a type for the entity
1837 in question; in that case, the type is dependent. */
1841 /* Erroneous types can be considered non-dependent. */
1842 if (type == error_mark_node)
1847 A type is dependent if it is:
1849 -- a template parameter. */
1850 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1852 /* -- a qualified-id with a nested-name-specifier which contains a
1853 class-name that names a dependent type or whose unqualified-id
1854 names a dependent type. */
1855 if (TREE_CODE (type) == TYPENAME_TYPE)
1857 /* -- a cv-qualified type where the cv-unqualified type is
1859 type = TYPE_MAIN_VARIANT (type);
1860 /* -- a compound type constructed from any dependent type. */
1861 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1862 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1863 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1865 else if (TREE_CODE (type) == POINTER_TYPE
1866 || TREE_CODE (type) == REFERENCE_TYPE)
1867 return cp_parser_dependent_type_p (TREE_TYPE (type));
1868 else if (TREE_CODE (type) == FUNCTION_TYPE
1869 || TREE_CODE (type) == METHOD_TYPE)
1873 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1875 for (arg_type = TYPE_ARG_TYPES (type);
1877 arg_type = TREE_CHAIN (arg_type))
1878 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1882 /* -- an array type constructed from any dependent type or whose
1883 size is specified by a constant expression that is
1885 if (TREE_CODE (type) == ARRAY_TYPE)
1887 if (TYPE_DOMAIN (type)
1888 && ((cp_parser_value_dependent_expression_p
1889 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1890 || (cp_parser_type_dependent_expression_p
1891 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1893 return cp_parser_dependent_type_p (TREE_TYPE (type));
1895 /* -- a template-id in which either the template name is a template
1896 parameter or any of the template arguments is a dependent type or
1897 an expression that is type-dependent or value-dependent.
1899 This language seems somewhat confused; for example, it does not
1900 discuss template template arguments. Therefore, we use the
1901 definition for dependent template arguments in [temp.dep.temp]. */
1902 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1903 && (cp_parser_dependent_template_id_p
1904 (CLASSTYPE_TI_TEMPLATE (type),
1905 CLASSTYPE_TI_ARGS (type))))
1907 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1909 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1910 expression is not type-dependent, then it should already been
1912 if (TREE_CODE (type) == TYPEOF_TYPE)
1914 /* The standard does not specifically mention types that are local
1915 to template functions or local classes, but they should be
1916 considered dependent too. For example:
1918 template <int I> void f() {
1923 The size of `E' cannot be known until the value of `I' has been
1924 determined. Therefore, `E' must be considered dependent. */
1925 scope = TYPE_CONTEXT (type);
1926 if (scope && TYPE_P (scope))
1927 return cp_parser_dependent_type_p (scope);
1928 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1929 return cp_parser_type_dependent_expression_p (scope);
1931 /* Other types are non-dependent. */
1935 /* Returns TRUE if the EXPRESSION is value-dependent. */
1938 cp_parser_value_dependent_expression_p (tree expression)
1940 if (!processing_template_decl)
1943 /* A name declared with a dependent type. */
1944 if (DECL_P (expression)
1945 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1947 /* A non-type template parameter. */
1948 if ((TREE_CODE (expression) == CONST_DECL
1949 && DECL_TEMPLATE_PARM_P (expression))
1950 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1952 /* A constant with integral or enumeration type and is initialized
1953 with an expression that is value-dependent. */
1954 if (TREE_CODE (expression) == VAR_DECL
1955 && DECL_INITIAL (expression)
1956 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1957 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1958 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1960 /* These expressions are value-dependent if the type to which the
1961 cast occurs is dependent. */
1962 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1963 || TREE_CODE (expression) == STATIC_CAST_EXPR
1964 || TREE_CODE (expression) == CONST_CAST_EXPR
1965 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1966 || TREE_CODE (expression) == CAST_EXPR)
1967 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1969 /* A `sizeof' expression where the sizeof operand is a type is
1970 value-dependent if the type is dependent. If the type was not
1971 dependent, we would no longer have a SIZEOF_EXPR, so any
1972 SIZEOF_EXPR is dependent. */
1973 if (TREE_CODE (expression) == SIZEOF_EXPR)
1975 /* A constant expression is value-dependent if any subexpression is
1977 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1979 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1982 return (cp_parser_value_dependent_expression_p
1983 (TREE_OPERAND (expression, 0)));
1986 return ((cp_parser_value_dependent_expression_p
1987 (TREE_OPERAND (expression, 0)))
1988 || (cp_parser_value_dependent_expression_p
1989 (TREE_OPERAND (expression, 1))));
1994 i < TREE_CODE_LENGTH (TREE_CODE (expression));
1996 if (cp_parser_value_dependent_expression_p
1997 (TREE_OPERAND (expression, i)))
2004 /* The expression is not value-dependent. */
2008 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2012 cp_parser_type_dependent_expression_p (expression)
2015 if (!processing_template_decl)
2018 /* Some expression forms are never type-dependent. */
2019 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2020 || TREE_CODE (expression) == SIZEOF_EXPR
2021 || TREE_CODE (expression) == ALIGNOF_EXPR
2022 || TREE_CODE (expression) == TYPEID_EXPR
2023 || TREE_CODE (expression) == DELETE_EXPR
2024 || TREE_CODE (expression) == VEC_DELETE_EXPR
2025 || TREE_CODE (expression) == THROW_EXPR)
2028 /* The types of these expressions depends only on the type to which
2030 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2031 || TREE_CODE (expression) == STATIC_CAST_EXPR
2032 || TREE_CODE (expression) == CONST_CAST_EXPR
2033 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2034 || TREE_CODE (expression) == CAST_EXPR)
2035 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2036 /* The types of these expressions depends only on the type created
2037 by the expression. */
2038 else if (TREE_CODE (expression) == NEW_EXPR
2039 || TREE_CODE (expression) == VEC_NEW_EXPR)
2040 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2042 if (TREE_CODE (expression) == FUNCTION_DECL
2043 && DECL_LANG_SPECIFIC (expression)
2044 && DECL_TEMPLATE_INFO (expression)
2045 && (cp_parser_dependent_template_id_p
2046 (DECL_TI_TEMPLATE (expression),
2047 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2050 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2053 /* Returns TRUE if the ARG (a template argument) is dependent. */
2056 cp_parser_dependent_template_arg_p (tree arg)
2058 if (!processing_template_decl)
2061 if (TREE_CODE (arg) == TEMPLATE_DECL
2062 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2063 return cp_parser_dependent_template_p (arg);
2064 else if (TYPE_P (arg))
2065 return cp_parser_dependent_type_p (arg);
2067 return (cp_parser_type_dependent_expression_p (arg)
2068 || cp_parser_value_dependent_expression_p (arg));
2071 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2074 cp_parser_dependent_template_id_p (tree tmpl, tree args)
2078 if (cp_parser_dependent_template_p (tmpl))
2080 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2081 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2086 /* Returns TRUE if the template TMPL is dependent. */
2089 cp_parser_dependent_template_p (tree tmpl)
2091 /* Template template parameters are dependent. */
2092 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2093 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2095 /* So are member templates of dependent classes. */
2096 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2097 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2101 /* Returns the scope through which DECL is being accessed, or
2102 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2103 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2104 or `x', respectively. If the DECL was named as `A::B' then
2105 NESTED_NAME_SPECIFIER is `A'. */
2108 cp_parser_scope_through_which_access_occurs (decl,
2110 nested_name_specifier)
2113 tree nested_name_specifier;
2116 tree qualifying_type = NULL_TREE;
2118 /* Determine the SCOPE of DECL. */
2119 scope = context_for_name_lookup (decl);
2120 /* If the SCOPE is not a type, then DECL is not a member. */
2121 if (!TYPE_P (scope))
2123 /* Figure out the type through which DECL is being accessed. */
2125 /* OBJECT_TYPE might not be a class type; consider:
2127 class A { typedef int I; };
2131 In this case, we will have "A::I" as the DECL, but "I" as the
2133 && CLASS_TYPE_P (object_type)
2134 && DERIVED_FROM_P (scope, object_type))
2135 /* If we are processing a `->' or `.' expression, use the type of the
2137 qualifying_type = object_type;
2138 else if (nested_name_specifier)
2140 /* If the reference is to a non-static member of the
2141 current class, treat it as if it were referenced through
2143 if (DECL_NONSTATIC_MEMBER_P (decl)
2144 && current_class_ptr
2145 && DERIVED_FROM_P (scope, current_class_type))
2146 qualifying_type = current_class_type;
2147 /* Otherwise, use the type indicated by the
2148 nested-name-specifier. */
2150 qualifying_type = nested_name_specifier;
2153 /* Otherwise, the name must be from the current class or one of
2155 qualifying_type = currently_open_derived_class (scope);
2157 return qualifying_type;
2160 /* Issue the indicated error MESSAGE. */
2163 cp_parser_error (parser, message)
2165 const char *message;
2167 /* Output the MESSAGE -- unless we're parsing tentatively. */
2168 if (!cp_parser_simulate_error (parser))
2172 /* If we are parsing tentatively, remember that an error has occurred
2173 during this tentative parse. Returns true if the error was
2174 simulated; false if a messgae should be issued by the caller. */
2177 cp_parser_simulate_error (parser)
2180 if (cp_parser_parsing_tentatively (parser)
2181 && !cp_parser_committed_to_tentative_parse (parser))
2183 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2189 /* This function is called when a type is defined. If type
2190 definitions are forbidden at this point, an error message is
2194 cp_parser_check_type_definition (parser)
2197 /* If types are forbidden here, issue a message. */
2198 if (parser->type_definition_forbidden_message)
2199 /* Use `%s' to print the string in case there are any escape
2200 characters in the message. */
2201 error ("%s", parser->type_definition_forbidden_message);
2204 /* Consume tokens up to, and including, the next non-nested closing `)'.
2205 Returns TRUE iff we found a closing `)'. */
2208 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2210 unsigned nesting_depth = 0;
2216 /* If we've run out of tokens, then there is no closing `)'. */
2217 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2219 /* Consume the token. */
2220 token = cp_lexer_consume_token (parser->lexer);
2221 /* If it is an `(', we have entered another level of nesting. */
2222 if (token->type == CPP_OPEN_PAREN)
2224 /* If it is a `)', then we might be done. */
2225 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2230 /* Consume tokens until the next token is a `)', or a `,'. Returns
2231 TRUE if the next token is a `,'. */
2234 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2236 unsigned nesting_depth = 0;
2240 cp_token *token = cp_lexer_peek_token (parser->lexer);
2242 /* If we've run out of tokens, then there is no closing `)'. */
2243 if (token->type == CPP_EOF)
2245 /* If it is a `,' stop. */
2246 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2248 /* If it is a `)', stop. */
2249 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2251 /* If it is an `(', we have entered another level of nesting. */
2252 else if (token->type == CPP_OPEN_PAREN)
2254 /* Consume the token. */
2255 token = cp_lexer_consume_token (parser->lexer);
2259 /* Consume tokens until we reach the end of the current statement.
2260 Normally, that will be just before consuming a `;'. However, if a
2261 non-nested `}' comes first, then we stop before consuming that. */
2264 cp_parser_skip_to_end_of_statement (parser)
2267 unsigned nesting_depth = 0;
2273 /* Peek at the next token. */
2274 token = cp_lexer_peek_token (parser->lexer);
2275 /* If we've run out of tokens, stop. */
2276 if (token->type == CPP_EOF)
2278 /* If the next token is a `;', we have reached the end of the
2280 if (token->type == CPP_SEMICOLON && !nesting_depth)
2282 /* If the next token is a non-nested `}', then we have reached
2283 the end of the current block. */
2284 if (token->type == CPP_CLOSE_BRACE)
2286 /* If this is a non-nested `}', stop before consuming it.
2287 That way, when confronted with something like:
2291 we stop before consuming the closing `}', even though we
2292 have not yet reached a `;'. */
2293 if (nesting_depth == 0)
2295 /* If it is the closing `}' for a block that we have
2296 scanned, stop -- but only after consuming the token.
2302 we will stop after the body of the erroneously declared
2303 function, but before consuming the following `typedef'
2305 if (--nesting_depth == 0)
2307 cp_lexer_consume_token (parser->lexer);
2311 /* If it the next token is a `{', then we are entering a new
2312 block. Consume the entire block. */
2313 else if (token->type == CPP_OPEN_BRACE)
2315 /* Consume the token. */
2316 cp_lexer_consume_token (parser->lexer);
2320 /* Skip tokens until we have consumed an entire block, or until we
2321 have consumed a non-nested `;'. */
2324 cp_parser_skip_to_end_of_block_or_statement (parser)
2327 unsigned nesting_depth = 0;
2333 /* Peek at the next token. */
2334 token = cp_lexer_peek_token (parser->lexer);
2335 /* If we've run out of tokens, stop. */
2336 if (token->type == CPP_EOF)
2338 /* If the next token is a `;', we have reached the end of the
2340 if (token->type == CPP_SEMICOLON && !nesting_depth)
2342 /* Consume the `;'. */
2343 cp_lexer_consume_token (parser->lexer);
2346 /* Consume the token. */
2347 token = cp_lexer_consume_token (parser->lexer);
2348 /* If the next token is a non-nested `}', then we have reached
2349 the end of the current block. */
2350 if (token->type == CPP_CLOSE_BRACE
2351 && (nesting_depth == 0 || --nesting_depth == 0))
2353 /* If it the next token is a `{', then we are entering a new
2354 block. Consume the entire block. */
2355 if (token->type == CPP_OPEN_BRACE)
2360 /* Skip tokens until a non-nested closing curly brace is the next
2364 cp_parser_skip_to_closing_brace (cp_parser *parser)
2366 unsigned nesting_depth = 0;
2372 /* Peek at the next token. */
2373 token = cp_lexer_peek_token (parser->lexer);
2374 /* If we've run out of tokens, stop. */
2375 if (token->type == CPP_EOF)
2377 /* If the next token is a non-nested `}', then we have reached
2378 the end of the current block. */
2379 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2381 /* If it the next token is a `{', then we are entering a new
2382 block. Consume the entire block. */
2383 else if (token->type == CPP_OPEN_BRACE)
2385 /* Consume the token. */
2386 cp_lexer_consume_token (parser->lexer);
2390 /* Create a new C++ parser. */
2398 /* cp_lexer_new_main is called before calling ggc_alloc because
2399 cp_lexer_new_main might load a PCH file. */
2400 lexer = cp_lexer_new_main ();
2402 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2403 parser->lexer = lexer;
2404 parser->context = cp_parser_context_new (NULL);
2406 /* For now, we always accept GNU extensions. */
2407 parser->allow_gnu_extensions_p = 1;
2409 /* The `>' token is a greater-than operator, not the end of a
2411 parser->greater_than_is_operator_p = true;
2413 parser->default_arg_ok_p = true;
2415 /* We are not parsing a constant-expression. */
2416 parser->constant_expression_p = false;
2418 /* Local variable names are not forbidden. */
2419 parser->local_variables_forbidden_p = false;
2421 /* We are not procesing an `extern "C"' declaration. */
2422 parser->in_unbraced_linkage_specification_p = false;
2424 /* We are not processing a declarator. */
2425 parser->in_declarator_p = false;
2427 /* The unparsed function queue is empty. */
2428 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2430 /* There are no classes being defined. */
2431 parser->num_classes_being_defined = 0;
2433 /* No template parameters apply. */
2434 parser->num_template_parameter_lists = 0;
2439 /* Lexical conventions [gram.lex] */
2441 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2445 cp_parser_identifier (parser)
2450 /* Look for the identifier. */
2451 token = cp_parser_require (parser, CPP_NAME, "identifier");
2452 /* Return the value. */
2453 return token ? token->value : error_mark_node;
2456 /* Basic concepts [gram.basic] */
2458 /* Parse a translation-unit.
2461 declaration-seq [opt]
2463 Returns TRUE if all went well. */
2466 cp_parser_translation_unit (parser)
2471 cp_parser_declaration_seq_opt (parser);
2473 /* If there are no tokens left then all went well. */
2474 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2477 /* Otherwise, issue an error message. */
2478 cp_parser_error (parser, "expected declaration");
2482 /* Consume the EOF token. */
2483 cp_parser_require (parser, CPP_EOF, "end-of-file");
2486 finish_translation_unit ();
2488 /* All went well. */
2492 /* Expressions [gram.expr] */
2494 /* Parse a primary-expression.
2505 ( compound-statement )
2506 __builtin_va_arg ( assignment-expression , type-id )
2511 Returns a representation of the expression.
2513 *IDK indicates what kind of id-expression (if any) was present.
2515 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2516 used as the operand of a pointer-to-member. In that case,
2517 *QUALIFYING_CLASS gives the class that is used as the qualifying
2518 class in the pointer-to-member. */
2521 cp_parser_primary_expression (cp_parser *parser,
2522 cp_parser_id_kind *idk,
2523 tree *qualifying_class)
2527 /* Assume the primary expression is not an id-expression. */
2528 *idk = CP_PARSER_ID_KIND_NONE;
2529 /* And that it cannot be used as pointer-to-member. */
2530 *qualifying_class = NULL_TREE;
2532 /* Peek at the next token. */
2533 token = cp_lexer_peek_token (parser->lexer);
2534 switch (token->type)
2547 token = cp_lexer_consume_token (parser->lexer);
2548 return token->value;
2550 case CPP_OPEN_PAREN:
2553 bool saved_greater_than_is_operator_p;
2555 /* Consume the `('. */
2556 cp_lexer_consume_token (parser->lexer);
2557 /* Within a parenthesized expression, a `>' token is always
2558 the greater-than operator. */
2559 saved_greater_than_is_operator_p
2560 = parser->greater_than_is_operator_p;
2561 parser->greater_than_is_operator_p = true;
2562 /* If we see `( { ' then we are looking at the beginning of
2563 a GNU statement-expression. */
2564 if (cp_parser_allow_gnu_extensions_p (parser)
2565 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2567 /* Statement-expressions are not allowed by the standard. */
2569 pedwarn ("ISO C++ forbids braced-groups within expressions");
2571 /* And they're not allowed outside of a function-body; you
2572 cannot, for example, write:
2574 int i = ({ int j = 3; j + 1; });
2576 at class or namespace scope. */
2577 if (!at_function_scope_p ())
2578 error ("statement-expressions are allowed only inside functions");
2579 /* Start the statement-expression. */
2580 expr = begin_stmt_expr ();
2581 /* Parse the compound-statement. */
2582 cp_parser_compound_statement (parser);
2584 expr = finish_stmt_expr (expr);
2588 /* Parse the parenthesized expression. */
2589 expr = cp_parser_expression (parser);
2590 /* Let the front end know that this expression was
2591 enclosed in parentheses. This matters in case, for
2592 example, the expression is of the form `A::B', since
2593 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2595 finish_parenthesized_expr (expr);
2597 /* The `>' token might be the end of a template-id or
2598 template-parameter-list now. */
2599 parser->greater_than_is_operator_p
2600 = saved_greater_than_is_operator_p;
2601 /* Consume the `)'. */
2602 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2603 cp_parser_skip_to_end_of_statement (parser);
2609 switch (token->keyword)
2611 /* These two are the boolean literals. */
2613 cp_lexer_consume_token (parser->lexer);
2614 return boolean_true_node;
2616 cp_lexer_consume_token (parser->lexer);
2617 return boolean_false_node;
2619 /* The `__null' literal. */
2621 cp_lexer_consume_token (parser->lexer);
2624 /* Recognize the `this' keyword. */
2626 cp_lexer_consume_token (parser->lexer);
2627 if (parser->local_variables_forbidden_p)
2629 error ("`this' may not be used in this context");
2630 return error_mark_node;
2632 return finish_this_expr ();
2634 /* The `operator' keyword can be the beginning of an
2639 case RID_FUNCTION_NAME:
2640 case RID_PRETTY_FUNCTION_NAME:
2641 case RID_C99_FUNCTION_NAME:
2642 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2643 __func__ are the names of variables -- but they are
2644 treated specially. Therefore, they are handled here,
2645 rather than relying on the generic id-expression logic
2646 below. Gramatically, these names are id-expressions.
2648 Consume the token. */
2649 token = cp_lexer_consume_token (parser->lexer);
2650 /* Look up the name. */
2651 return finish_fname (token->value);
2658 /* The `__builtin_va_arg' construct is used to handle
2659 `va_arg'. Consume the `__builtin_va_arg' token. */
2660 cp_lexer_consume_token (parser->lexer);
2661 /* Look for the opening `('. */
2662 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2663 /* Now, parse the assignment-expression. */
2664 expression = cp_parser_assignment_expression (parser);
2665 /* Look for the `,'. */
2666 cp_parser_require (parser, CPP_COMMA, "`,'");
2667 /* Parse the type-id. */
2668 type = cp_parser_type_id (parser);
2669 /* Look for the closing `)'. */
2670 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2672 return build_x_va_arg (expression, type);
2676 cp_parser_error (parser, "expected primary-expression");
2677 return error_mark_node;
2681 /* An id-expression can start with either an identifier, a
2682 `::' as the beginning of a qualified-id, or the "operator"
2686 case CPP_TEMPLATE_ID:
2687 case CPP_NESTED_NAME_SPECIFIER:
2693 /* Parse the id-expression. */
2695 = cp_parser_id_expression (parser,
2696 /*template_keyword_p=*/false,
2697 /*check_dependency_p=*/true,
2698 /*template_p=*/NULL);
2699 if (id_expression == error_mark_node)
2700 return error_mark_node;
2701 /* If we have a template-id, then no further lookup is
2702 required. If the template-id was for a template-class, we
2703 will sometimes have a TYPE_DECL at this point. */
2704 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2705 || TREE_CODE (id_expression) == TYPE_DECL)
2706 decl = id_expression;
2707 /* Look up the name. */
2710 decl = cp_parser_lookup_name_simple (parser, id_expression);
2711 /* If name lookup gives us a SCOPE_REF, then the
2712 qualifying scope was dependent. Just propagate the
2714 if (TREE_CODE (decl) == SCOPE_REF)
2716 if (TYPE_P (TREE_OPERAND (decl, 0)))
2717 *qualifying_class = TREE_OPERAND (decl, 0);
2720 /* Check to see if DECL is a local variable in a context
2721 where that is forbidden. */
2722 if (parser->local_variables_forbidden_p
2723 && local_variable_p (decl))
2725 /* It might be that we only found DECL because we are
2726 trying to be generous with pre-ISO scoping rules.
2727 For example, consider:
2731 for (int i = 0; i < 10; ++i) {}
2732 extern void f(int j = i);
2735 Here, name look up will originally find the out
2736 of scope `i'. We need to issue a warning message,
2737 but then use the global `i'. */
2738 decl = check_for_out_of_scope_variable (decl);
2739 if (local_variable_p (decl))
2741 error ("local variable `%D' may not appear in this context",
2743 return error_mark_node;
2747 /* If unqualified name lookup fails while processing a
2748 template, that just means that we need to do name
2749 lookup again when the template is instantiated. */
2751 && decl == error_mark_node
2752 && processing_template_decl)
2754 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2755 return build_min_nt (LOOKUP_EXPR, id_expression);
2757 else if (decl == error_mark_node
2758 && !processing_template_decl)
2762 /* It may be resolvable as a koenig lookup function
2764 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2765 return id_expression;
2767 else if (TYPE_P (parser->scope)
2768 && !COMPLETE_TYPE_P (parser->scope))
2769 error ("incomplete type `%T' used in nested name specifier",
2771 else if (parser->scope != global_namespace)
2772 error ("`%D' is not a member of `%D'",
2773 id_expression, parser->scope);
2775 error ("`::%D' has not been declared", id_expression);
2777 /* If DECL is a variable would be out of scope under
2778 ANSI/ISO rules, but in scope in the ARM, name lookup
2779 will succeed. Issue a diagnostic here. */
2781 decl = check_for_out_of_scope_variable (decl);
2783 /* Remember that the name was used in the definition of
2784 the current class so that we can check later to see if
2785 the meaning would have been different after the class
2786 was entirely defined. */
2787 if (!parser->scope && decl != error_mark_node)
2788 maybe_note_name_used_in_class (id_expression, decl);
2791 /* If we didn't find anything, or what we found was a type,
2792 then this wasn't really an id-expression. */
2793 if (TREE_CODE (decl) == TYPE_DECL
2794 || TREE_CODE (decl) == NAMESPACE_DECL
2795 || (TREE_CODE (decl) == TEMPLATE_DECL
2796 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2798 cp_parser_error (parser,
2799 "expected primary-expression");
2800 return error_mark_node;
2803 /* If the name resolved to a template parameter, there is no
2804 need to look it up again later. Similarly, we resolve
2805 enumeration constants to their underlying values. */
2806 if (TREE_CODE (decl) == CONST_DECL)
2808 *idk = CP_PARSER_ID_KIND_NONE;
2809 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2810 return DECL_INITIAL (decl);
2817 /* If the declaration was explicitly qualified indicate
2818 that. The semantics of `A::f(3)' are different than
2819 `f(3)' if `f' is virtual. */
2820 *idk = (parser->scope
2821 ? CP_PARSER_ID_KIND_QUALIFIED
2822 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2823 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2824 : CP_PARSER_ID_KIND_UNQUALIFIED));
2829 An id-expression is type-dependent if it contains an
2830 identifier that was declared with a dependent type.
2832 As an optimization, we could choose not to create a
2833 LOOKUP_EXPR for a name that resolved to a local
2834 variable in the template function that we are currently
2835 declaring; such a name cannot ever resolve to anything
2836 else. If we did that we would not have to look up
2837 these names at instantiation time.
2839 The standard is not very specific about an
2840 id-expression that names a set of overloaded functions.
2841 What if some of them have dependent types and some of
2842 them do not? Presumably, such a name should be treated
2843 as a dependent name. */
2844 /* Assume the name is not dependent. */
2845 dependent_p = false;
2846 if (!processing_template_decl)
2847 /* No names are dependent outside a template. */
2849 /* A template-id where the name of the template was not
2850 resolved is definitely dependent. */
2851 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2852 && (TREE_CODE (TREE_OPERAND (decl, 0))
2853 == IDENTIFIER_NODE))
2855 /* For anything except an overloaded function, just check
2857 else if (!is_overloaded_fn (decl))
2859 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2860 /* For a set of overloaded functions, check each of the
2866 if (BASELINK_P (fns))
2867 fns = BASELINK_FUNCTIONS (fns);
2869 /* For a template-id, check to see if the template
2870 arguments are dependent. */
2871 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2873 tree args = TREE_OPERAND (fns, 1);
2875 if (args && TREE_CODE (args) == TREE_LIST)
2879 if (cp_parser_dependent_template_arg_p
2880 (TREE_VALUE (args)))
2885 args = TREE_CHAIN (args);
2888 else if (args && TREE_CODE (args) == TREE_VEC)
2891 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2892 if (cp_parser_dependent_template_arg_p
2893 (TREE_VEC_ELT (args, i)))
2900 /* The functions are those referred to by the
2902 fns = TREE_OPERAND (fns, 0);
2905 /* If there are no dependent template arguments, go
2906 through the overlaoded functions. */
2907 while (fns && !dependent_p)
2909 tree fn = OVL_CURRENT (fns);
2911 /* Member functions of dependent classes are
2913 if (TREE_CODE (fn) == FUNCTION_DECL
2914 && cp_parser_type_dependent_expression_p (fn))
2916 else if (TREE_CODE (fn) == TEMPLATE_DECL
2917 && cp_parser_dependent_template_p (fn))
2920 fns = OVL_NEXT (fns);
2924 /* If the name was dependent on a template parameter,
2925 we will resolve the name at instantiation time. */
2928 /* Create a SCOPE_REF for qualified names. */
2931 if (TYPE_P (parser->scope))
2932 *qualifying_class = parser->scope;
2933 return build_nt (SCOPE_REF,
2937 /* A TEMPLATE_ID already contains all the information
2939 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2940 return id_expression;
2941 /* Create a LOOKUP_EXPR for other unqualified names. */
2942 return build_min_nt (LOOKUP_EXPR, id_expression);
2947 decl = (adjust_result_of_qualified_name_lookup
2948 (decl, parser->scope, current_class_type));
2949 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2950 *qualifying_class = parser->scope;
2953 /* Transform references to non-static data members into
2955 decl = hack_identifier (decl, id_expression);
2957 /* Resolve references to variables of anonymous unions
2958 into COMPONENT_REFs. */
2959 if (TREE_CODE (decl) == ALIAS_DECL)
2960 decl = DECL_INITIAL (decl);
2963 if (TREE_DEPRECATED (decl))
2964 warn_deprecated_use (decl);
2969 /* Anything else is an error. */
2971 cp_parser_error (parser, "expected primary-expression");
2972 return error_mark_node;
2976 /* Parse an id-expression.
2983 :: [opt] nested-name-specifier template [opt] unqualified-id
2985 :: operator-function-id
2988 Return a representation of the unqualified portion of the
2989 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2990 a `::' or nested-name-specifier.
2992 Often, if the id-expression was a qualified-id, the caller will
2993 want to make a SCOPE_REF to represent the qualified-id. This
2994 function does not do this in order to avoid wastefully creating
2995 SCOPE_REFs when they are not required.
2997 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3000 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3001 uninstantiated templates.
3003 If *TEMPLATE_P is non-NULL, it is set to true iff the
3004 `template' keyword is used to explicitly indicate that the entity
3005 named is a template. */
3008 cp_parser_id_expression (cp_parser *parser,
3009 bool template_keyword_p,
3010 bool check_dependency_p,
3013 bool global_scope_p;
3014 bool nested_name_specifier_p;
3016 /* Assume the `template' keyword was not used. */
3018 *template_p = false;
3020 /* Look for the optional `::' operator. */
3022 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3024 /* Look for the optional nested-name-specifier. */
3025 nested_name_specifier_p
3026 = (cp_parser_nested_name_specifier_opt (parser,
3027 /*typename_keyword_p=*/false,
3031 /* If there is a nested-name-specifier, then we are looking at
3032 the first qualified-id production. */
3033 if (nested_name_specifier_p)
3036 tree saved_object_scope;
3037 tree saved_qualifying_scope;
3038 tree unqualified_id;
3041 /* See if the next token is the `template' keyword. */
3043 template_p = &is_template;
3044 *template_p = cp_parser_optional_template_keyword (parser);
3045 /* Name lookup we do during the processing of the
3046 unqualified-id might obliterate SCOPE. */
3047 saved_scope = parser->scope;
3048 saved_object_scope = parser->object_scope;
3049 saved_qualifying_scope = parser->qualifying_scope;
3050 /* Process the final unqualified-id. */
3051 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3052 check_dependency_p);
3053 /* Restore the SAVED_SCOPE for our caller. */
3054 parser->scope = saved_scope;
3055 parser->object_scope = saved_object_scope;
3056 parser->qualifying_scope = saved_qualifying_scope;
3058 return unqualified_id;
3060 /* Otherwise, if we are in global scope, then we are looking at one
3061 of the other qualified-id productions. */
3062 else if (global_scope_p)
3067 /* Peek at the next token. */
3068 token = cp_lexer_peek_token (parser->lexer);
3070 /* If it's an identifier, and the next token is not a "<", then
3071 we can avoid the template-id case. This is an optimization
3072 for this common case. */
3073 if (token->type == CPP_NAME
3074 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3075 return cp_parser_identifier (parser);
3077 cp_parser_parse_tentatively (parser);
3078 /* Try a template-id. */
3079 id = cp_parser_template_id (parser,
3080 /*template_keyword_p=*/false,
3081 /*check_dependency_p=*/true);
3082 /* If that worked, we're done. */
3083 if (cp_parser_parse_definitely (parser))
3086 /* Peek at the next token. (Changes in the token buffer may
3087 have invalidated the pointer obtained above.) */
3088 token = cp_lexer_peek_token (parser->lexer);
3090 switch (token->type)
3093 return cp_parser_identifier (parser);
3096 if (token->keyword == RID_OPERATOR)
3097 return cp_parser_operator_function_id (parser);
3101 cp_parser_error (parser, "expected id-expression");
3102 return error_mark_node;
3106 return cp_parser_unqualified_id (parser, template_keyword_p,
3107 /*check_dependency_p=*/true);
3110 /* Parse an unqualified-id.
3114 operator-function-id
3115 conversion-function-id
3119 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3120 keyword, in a construct like `A::template ...'.
3122 Returns a representation of unqualified-id. For the `identifier'
3123 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3124 production a BIT_NOT_EXPR is returned; the operand of the
3125 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3126 other productions, see the documentation accompanying the
3127 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3128 names are looked up in uninstantiated templates. */
3131 cp_parser_unqualified_id (parser, template_keyword_p,
3134 bool template_keyword_p;
3135 bool check_dependency_p;
3139 /* Peek at the next token. */
3140 token = cp_lexer_peek_token (parser->lexer);
3142 switch (token->type)
3148 /* We don't know yet whether or not this will be a
3150 cp_parser_parse_tentatively (parser);
3151 /* Try a template-id. */
3152 id = cp_parser_template_id (parser, template_keyword_p,
3153 check_dependency_p);
3154 /* If it worked, we're done. */
3155 if (cp_parser_parse_definitely (parser))
3157 /* Otherwise, it's an ordinary identifier. */
3158 return cp_parser_identifier (parser);
3161 case CPP_TEMPLATE_ID:
3162 return cp_parser_template_id (parser, template_keyword_p,
3163 check_dependency_p);
3168 tree qualifying_scope;
3172 /* Consume the `~' token. */
3173 cp_lexer_consume_token (parser->lexer);
3174 /* Parse the class-name. The standard, as written, seems to
3177 template <typename T> struct S { ~S (); };
3178 template <typename T> S<T>::~S() {}
3180 is invalid, since `~' must be followed by a class-name, but
3181 `S<T>' is dependent, and so not known to be a class.
3182 That's not right; we need to look in uninstantiated
3183 templates. A further complication arises from:
3185 template <typename T> void f(T t) {
3189 Here, it is not possible to look up `T' in the scope of `T'
3190 itself. We must look in both the current scope, and the
3191 scope of the containing complete expression.
3193 Yet another issue is:
3202 The standard does not seem to say that the `S' in `~S'
3203 should refer to the type `S' and not the data member
3206 /* DR 244 says that we look up the name after the "~" in the
3207 same scope as we looked up the qualifying name. That idea
3208 isn't fully worked out; it's more complicated than that. */
3209 scope = parser->scope;
3210 object_scope = parser->object_scope;
3211 qualifying_scope = parser->qualifying_scope;
3213 /* If the name is of the form "X::~X" it's OK. */
3214 if (scope && TYPE_P (scope)
3215 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3216 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3218 && (cp_lexer_peek_token (parser->lexer)->value
3219 == TYPE_IDENTIFIER (scope)))
3221 cp_lexer_consume_token (parser->lexer);
3222 return build_nt (BIT_NOT_EXPR, scope);
3225 /* If there was an explicit qualification (S::~T), first look
3226 in the scope given by the qualification (i.e., S). */
3229 cp_parser_parse_tentatively (parser);
3230 type_decl = cp_parser_class_name (parser,
3231 /*typename_keyword_p=*/false,
3232 /*template_keyword_p=*/false,
3234 /*check_access_p=*/true,
3235 /*check_dependency=*/false,
3236 /*class_head_p=*/false);
3237 if (cp_parser_parse_definitely (parser))
3238 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3240 /* In "N::S::~S", look in "N" as well. */
3241 if (scope && qualifying_scope)
3243 cp_parser_parse_tentatively (parser);
3244 parser->scope = qualifying_scope;
3245 parser->object_scope = NULL_TREE;
3246 parser->qualifying_scope = NULL_TREE;
3248 = cp_parser_class_name (parser,
3249 /*typename_keyword_p=*/false,
3250 /*template_keyword_p=*/false,
3252 /*check_access_p=*/true,
3253 /*check_dependency=*/false,
3254 /*class_head_p=*/false);
3255 if (cp_parser_parse_definitely (parser))
3256 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3258 /* In "p->S::~T", look in the scope given by "*p" as well. */
3259 else if (object_scope)
3261 cp_parser_parse_tentatively (parser);
3262 parser->scope = object_scope;
3263 parser->object_scope = NULL_TREE;
3264 parser->qualifying_scope = NULL_TREE;
3266 = cp_parser_class_name (parser,
3267 /*typename_keyword_p=*/false,
3268 /*template_keyword_p=*/false,
3270 /*check_access_p=*/true,
3271 /*check_dependency=*/false,
3272 /*class_head_p=*/false);
3273 if (cp_parser_parse_definitely (parser))
3274 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3276 /* Look in the surrounding context. */
3277 parser->scope = NULL_TREE;
3278 parser->object_scope = NULL_TREE;
3279 parser->qualifying_scope = NULL_TREE;
3281 = cp_parser_class_name (parser,
3282 /*typename_keyword_p=*/false,
3283 /*template_keyword_p=*/false,
3285 /*check_access_p=*/true,
3286 /*check_dependency=*/false,
3287 /*class_head_p=*/false);
3288 /* If an error occurred, assume that the name of the
3289 destructor is the same as the name of the qualifying
3290 class. That allows us to keep parsing after running
3291 into ill-formed destructor names. */
3292 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3293 return build_nt (BIT_NOT_EXPR, scope);
3294 else if (type_decl == error_mark_node)
3295 return error_mark_node;
3297 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3301 if (token->keyword == RID_OPERATOR)
3305 /* This could be a template-id, so we try that first. */
3306 cp_parser_parse_tentatively (parser);
3307 /* Try a template-id. */
3308 id = cp_parser_template_id (parser, template_keyword_p,
3309 /*check_dependency_p=*/true);
3310 /* If that worked, we're done. */
3311 if (cp_parser_parse_definitely (parser))
3313 /* We still don't know whether we're looking at an
3314 operator-function-id or a conversion-function-id. */
3315 cp_parser_parse_tentatively (parser);
3316 /* Try an operator-function-id. */
3317 id = cp_parser_operator_function_id (parser);
3318 /* If that didn't work, try a conversion-function-id. */
3319 if (!cp_parser_parse_definitely (parser))
3320 id = cp_parser_conversion_function_id (parser);
3327 cp_parser_error (parser, "expected unqualified-id");
3328 return error_mark_node;
3332 /* Parse an (optional) nested-name-specifier.
3334 nested-name-specifier:
3335 class-or-namespace-name :: nested-name-specifier [opt]
3336 class-or-namespace-name :: template nested-name-specifier [opt]
3338 PARSER->SCOPE should be set appropriately before this function is
3339 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3340 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3343 Sets PARSER->SCOPE to the class (TYPE) or namespace
3344 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3345 it unchanged if there is no nested-name-specifier. Returns the new
3346 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3349 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3350 bool typename_keyword_p,
3351 bool check_dependency_p,
3354 bool success = false;
3355 tree access_check = NULL_TREE;
3359 /* If the next token corresponds to a nested name specifier, there
3360 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3361 false, it may have been true before, in which case something
3362 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3363 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3364 CHECK_DEPENDENCY_P is false, we have to fall through into the
3366 if (check_dependency_p
3367 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3369 cp_parser_pre_parsed_nested_name_specifier (parser);
3370 return parser->scope;
3373 /* Remember where the nested-name-specifier starts. */
3374 if (cp_parser_parsing_tentatively (parser)
3375 && !cp_parser_committed_to_tentative_parse (parser))
3377 token = cp_lexer_peek_token (parser->lexer);
3378 start = cp_lexer_token_difference (parser->lexer,
3379 parser->lexer->first_token,
3385 push_deferring_access_checks (true);
3391 tree saved_qualifying_scope;
3392 bool template_keyword_p;
3394 /* Spot cases that cannot be the beginning of a
3395 nested-name-specifier. */
3396 token = cp_lexer_peek_token (parser->lexer);
3398 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3399 the already parsed nested-name-specifier. */
3400 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3402 /* Grab the nested-name-specifier and continue the loop. */
3403 cp_parser_pre_parsed_nested_name_specifier (parser);
3408 /* Spot cases that cannot be the beginning of a
3409 nested-name-specifier. On the second and subsequent times
3410 through the loop, we look for the `template' keyword. */
3411 if (success && token->keyword == RID_TEMPLATE)
3413 /* A template-id can start a nested-name-specifier. */
3414 else if (token->type == CPP_TEMPLATE_ID)
3418 /* If the next token is not an identifier, then it is
3419 definitely not a class-or-namespace-name. */
3420 if (token->type != CPP_NAME)
3422 /* If the following token is neither a `<' (to begin a
3423 template-id), nor a `::', then we are not looking at a
3424 nested-name-specifier. */
3425 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3426 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3430 /* The nested-name-specifier is optional, so we parse
3432 cp_parser_parse_tentatively (parser);
3434 /* Look for the optional `template' keyword, if this isn't the
3435 first time through the loop. */
3437 template_keyword_p = cp_parser_optional_template_keyword (parser);
3439 template_keyword_p = false;
3441 /* Save the old scope since the name lookup we are about to do
3442 might destroy it. */
3443 old_scope = parser->scope;
3444 saved_qualifying_scope = parser->qualifying_scope;
3445 /* Parse the qualifying entity. */
3447 = cp_parser_class_or_namespace_name (parser,
3452 /* Look for the `::' token. */
3453 cp_parser_require (parser, CPP_SCOPE, "`::'");
3455 /* If we found what we wanted, we keep going; otherwise, we're
3457 if (!cp_parser_parse_definitely (parser))
3459 bool error_p = false;
3461 /* Restore the OLD_SCOPE since it was valid before the
3462 failed attempt at finding the last
3463 class-or-namespace-name. */
3464 parser->scope = old_scope;
3465 parser->qualifying_scope = saved_qualifying_scope;
3466 /* If the next token is an identifier, and the one after
3467 that is a `::', then any valid interpretation would have
3468 found a class-or-namespace-name. */
3469 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3470 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3472 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3475 token = cp_lexer_consume_token (parser->lexer);
3480 decl = cp_parser_lookup_name_simple (parser, token->value);
3481 if (TREE_CODE (decl) == TEMPLATE_DECL)
3482 error ("`%D' used without template parameters",
3484 else if (parser->scope)
3486 if (TYPE_P (parser->scope))
3487 error ("`%T::%D' is not a class-name or "
3489 parser->scope, token->value);
3491 error ("`%D::%D' is not a class-name or "
3493 parser->scope, token->value);
3496 error ("`%D' is not a class-name or namespace-name",
3498 parser->scope = NULL_TREE;
3500 /* Treat this as a successful nested-name-specifier
3505 If the name found is not a class-name (clause
3506 _class_) or namespace-name (_namespace.def_), the
3507 program is ill-formed. */
3510 cp_lexer_consume_token (parser->lexer);
3515 /* We've found one valid nested-name-specifier. */
3517 /* Make sure we look in the right scope the next time through
3519 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3520 ? TREE_TYPE (new_scope)
3522 /* If it is a class scope, try to complete it; we are about to
3523 be looking up names inside the class. */
3524 if (TYPE_P (parser->scope))
3525 complete_type (parser->scope);
3528 /* Retrieve any deferred checks. Do not pop this access checks yet
3529 so the memory will not be reclaimed during token replacing below. */
3530 access_check = get_deferred_access_checks ();
3532 /* If parsing tentatively, replace the sequence of tokens that makes
3533 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3534 token. That way, should we re-parse the token stream, we will
3535 not have to repeat the effort required to do the parse, nor will
3536 we issue duplicate error messages. */
3537 if (success && start >= 0)
3539 /* Find the token that corresponds to the start of the
3541 token = cp_lexer_advance_token (parser->lexer,
3542 parser->lexer->first_token,
3545 /* Reset the contents of the START token. */
3546 token->type = CPP_NESTED_NAME_SPECIFIER;
3547 token->value = build_tree_list (access_check, parser->scope);
3548 TREE_TYPE (token->value) = parser->qualifying_scope;
3549 token->keyword = RID_MAX;
3550 /* Purge all subsequent tokens. */
3551 cp_lexer_purge_tokens_after (parser->lexer, token);
3554 pop_deferring_access_checks ();
3555 return success ? parser->scope : NULL_TREE;
3558 /* Parse a nested-name-specifier. See
3559 cp_parser_nested_name_specifier_opt for details. This function
3560 behaves identically, except that it will an issue an error if no
3561 nested-name-specifier is present, and it will return
3562 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3566 cp_parser_nested_name_specifier (cp_parser *parser,
3567 bool typename_keyword_p,
3568 bool check_dependency_p,
3573 /* Look for the nested-name-specifier. */
3574 scope = cp_parser_nested_name_specifier_opt (parser,
3578 /* If it was not present, issue an error message. */
3581 cp_parser_error (parser, "expected nested-name-specifier");
3582 return error_mark_node;
3588 /* Parse a class-or-namespace-name.
3590 class-or-namespace-name:
3594 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3595 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3596 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3597 TYPE_P is TRUE iff the next name should be taken as a class-name,
3598 even the same name is declared to be another entity in the same
3601 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3602 specified by the class-or-namespace-name. If neither is found the
3603 ERROR_MARK_NODE is returned. */
3606 cp_parser_class_or_namespace_name (cp_parser *parser,
3607 bool typename_keyword_p,
3608 bool template_keyword_p,
3609 bool check_dependency_p,
3613 tree saved_qualifying_scope;
3614 tree saved_object_scope;
3618 /* If the next token is the `template' keyword, we know that we are
3619 looking at a class-name. */
3620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3621 return cp_parser_class_name (parser,
3625 /*check_access_p=*/true,
3627 /*class_head_p=*/false);
3628 /* Before we try to parse the class-name, we must save away the
3629 current PARSER->SCOPE since cp_parser_class_name will destroy
3631 saved_scope = parser->scope;
3632 saved_qualifying_scope = parser->qualifying_scope;
3633 saved_object_scope = parser->object_scope;
3634 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3635 there is no need to look for a namespace-name. */
3636 only_class_p = saved_scope && TYPE_P (saved_scope);
3638 cp_parser_parse_tentatively (parser);
3639 scope = cp_parser_class_name (parser,
3643 /*check_access_p=*/true,
3645 /*class_head_p=*/false);
3646 /* If that didn't work, try for a namespace-name. */
3647 if (!only_class_p && !cp_parser_parse_definitely (parser))
3649 /* Restore the saved scope. */
3650 parser->scope = saved_scope;
3651 parser->qualifying_scope = saved_qualifying_scope;
3652 parser->object_scope = saved_object_scope;
3653 /* If we are not looking at an identifier followed by the scope
3654 resolution operator, then this is not part of a
3655 nested-name-specifier. (Note that this function is only used
3656 to parse the components of a nested-name-specifier.) */
3657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3658 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3659 return error_mark_node;
3660 scope = cp_parser_namespace_name (parser);
3666 /* Parse a postfix-expression.
3670 postfix-expression [ expression ]
3671 postfix-expression ( expression-list [opt] )
3672 simple-type-specifier ( expression-list [opt] )
3673 typename :: [opt] nested-name-specifier identifier
3674 ( expression-list [opt] )
3675 typename :: [opt] nested-name-specifier template [opt] template-id
3676 ( expression-list [opt] )
3677 postfix-expression . template [opt] id-expression
3678 postfix-expression -> template [opt] id-expression
3679 postfix-expression . pseudo-destructor-name
3680 postfix-expression -> pseudo-destructor-name
3681 postfix-expression ++
3682 postfix-expression --
3683 dynamic_cast < type-id > ( expression )
3684 static_cast < type-id > ( expression )
3685 reinterpret_cast < type-id > ( expression )
3686 const_cast < type-id > ( expression )
3687 typeid ( expression )
3693 ( type-id ) { initializer-list , [opt] }
3695 This extension is a GNU version of the C99 compound-literal
3696 construct. (The C99 grammar uses `type-name' instead of `type-id',
3697 but they are essentially the same concept.)
3699 If ADDRESS_P is true, the postfix expression is the operand of the
3702 Returns a representation of the expression. */
3705 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3709 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3710 tree postfix_expression = NULL_TREE;
3711 /* Non-NULL only if the current postfix-expression can be used to
3712 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3713 class used to qualify the member. */
3714 tree qualifying_class = NULL_TREE;
3717 /* Peek at the next token. */
3718 token = cp_lexer_peek_token (parser->lexer);
3719 /* Some of the productions are determined by keywords. */
3720 keyword = token->keyword;
3730 const char *saved_message;
3732 /* All of these can be handled in the same way from the point
3733 of view of parsing. Begin by consuming the token
3734 identifying the cast. */
3735 cp_lexer_consume_token (parser->lexer);
3737 /* New types cannot be defined in the cast. */
3738 saved_message = parser->type_definition_forbidden_message;
3739 parser->type_definition_forbidden_message
3740 = "types may not be defined in casts";
3742 /* Look for the opening `<'. */
3743 cp_parser_require (parser, CPP_LESS, "`<'");
3744 /* Parse the type to which we are casting. */
3745 type = cp_parser_type_id (parser);
3746 /* Look for the closing `>'. */
3747 cp_parser_require (parser, CPP_GREATER, "`>'");
3748 /* Restore the old message. */
3749 parser->type_definition_forbidden_message = saved_message;
3751 /* And the expression which is being cast. */
3752 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3753 expression = cp_parser_expression (parser);
3754 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3760 = build_dynamic_cast (type, expression);
3764 = build_static_cast (type, expression);
3768 = build_reinterpret_cast (type, expression);
3772 = build_const_cast (type, expression);
3783 const char *saved_message;
3785 /* Consume the `typeid' token. */
3786 cp_lexer_consume_token (parser->lexer);
3787 /* Look for the `(' token. */
3788 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3789 /* Types cannot be defined in a `typeid' expression. */
3790 saved_message = parser->type_definition_forbidden_message;
3791 parser->type_definition_forbidden_message
3792 = "types may not be defined in a `typeid\' expression";
3793 /* We can't be sure yet whether we're looking at a type-id or an
3795 cp_parser_parse_tentatively (parser);
3796 /* Try a type-id first. */
3797 type = cp_parser_type_id (parser);
3798 /* Look for the `)' token. Otherwise, we can't be sure that
3799 we're not looking at an expression: consider `typeid (int
3800 (3))', for example. */
3801 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3802 /* If all went well, simply lookup the type-id. */
3803 if (cp_parser_parse_definitely (parser))
3804 postfix_expression = get_typeid (type);
3805 /* Otherwise, fall back to the expression variant. */
3810 /* Look for an expression. */
3811 expression = cp_parser_expression (parser);
3812 /* Compute its typeid. */
3813 postfix_expression = build_typeid (expression);
3814 /* Look for the `)' token. */
3815 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3818 /* Restore the saved message. */
3819 parser->type_definition_forbidden_message = saved_message;
3825 bool template_p = false;
3829 /* Consume the `typename' token. */
3830 cp_lexer_consume_token (parser->lexer);
3831 /* Look for the optional `::' operator. */
3832 cp_parser_global_scope_opt (parser,
3833 /*current_scope_valid_p=*/false);
3834 /* Look for the nested-name-specifier. */
3835 cp_parser_nested_name_specifier (parser,
3836 /*typename_keyword_p=*/true,
3837 /*check_dependency_p=*/true,
3839 /* Look for the optional `template' keyword. */
3840 template_p = cp_parser_optional_template_keyword (parser);
3841 /* We don't know whether we're looking at a template-id or an
3843 cp_parser_parse_tentatively (parser);
3844 /* Try a template-id. */
3845 id = cp_parser_template_id (parser, template_p,
3846 /*check_dependency_p=*/true);
3847 /* If that didn't work, try an identifier. */
3848 if (!cp_parser_parse_definitely (parser))
3849 id = cp_parser_identifier (parser);
3850 /* Create a TYPENAME_TYPE to represent the type to which the
3851 functional cast is being performed. */
3852 type = make_typename_type (parser->scope, id,
3855 postfix_expression = cp_parser_functional_cast (parser, type);
3863 /* If the next thing is a simple-type-specifier, we may be
3864 looking at a functional cast. We could also be looking at
3865 an id-expression. So, we try the functional cast, and if
3866 that doesn't work we fall back to the primary-expression. */
3867 cp_parser_parse_tentatively (parser);
3868 /* Look for the simple-type-specifier. */
3869 type = cp_parser_simple_type_specifier (parser,
3870 CP_PARSER_FLAGS_NONE);
3871 /* Parse the cast itself. */
3872 if (!cp_parser_error_occurred (parser))
3874 = cp_parser_functional_cast (parser, type);
3875 /* If that worked, we're done. */
3876 if (cp_parser_parse_definitely (parser))
3879 /* If the functional-cast didn't work out, try a
3880 compound-literal. */
3881 if (cp_parser_allow_gnu_extensions_p (parser))
3883 tree initializer_list = NULL_TREE;
3885 cp_parser_parse_tentatively (parser);
3886 /* Look for the `('. */
3887 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3889 type = cp_parser_type_id (parser);
3890 /* Look for the `)'. */
3891 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3892 /* Look for the `{'. */
3893 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3894 /* If things aren't going well, there's no need to
3896 if (!cp_parser_error_occurred (parser))
3898 /* Parse the initializer-list. */
3900 = cp_parser_initializer_list (parser);
3901 /* Allow a trailing `,'. */
3902 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3903 cp_lexer_consume_token (parser->lexer);
3904 /* Look for the final `}'. */
3905 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3908 /* If that worked, we're definitely looking at a
3909 compound-literal expression. */
3910 if (cp_parser_parse_definitely (parser))
3912 /* Warn the user that a compound literal is not
3913 allowed in standard C++. */
3915 pedwarn ("ISO C++ forbids compound-literals");
3916 /* Form the representation of the compound-literal. */
3918 = finish_compound_literal (type, initializer_list);
3923 /* It must be a primary-expression. */
3924 postfix_expression = cp_parser_primary_expression (parser,
3931 /* Peek at the next token. */
3932 token = cp_lexer_peek_token (parser->lexer);
3933 done = (token->type != CPP_OPEN_SQUARE
3934 && token->type != CPP_OPEN_PAREN
3935 && token->type != CPP_DOT
3936 && token->type != CPP_DEREF
3937 && token->type != CPP_PLUS_PLUS
3938 && token->type != CPP_MINUS_MINUS);
3940 /* If the postfix expression is complete, finish up. */
3941 if (address_p && qualifying_class && done)
3943 if (TREE_CODE (postfix_expression) == SCOPE_REF)
3944 postfix_expression = TREE_OPERAND (postfix_expression, 1);
3946 = build_offset_ref (qualifying_class, postfix_expression);
3947 return postfix_expression;
3950 /* Otherwise, if we were avoiding committing until we knew
3951 whether or not we had a pointer-to-member, we now know that
3952 the expression is an ordinary reference to a qualified name. */
3953 if (qualifying_class && !processing_template_decl)
3955 if (TREE_CODE (postfix_expression) == FIELD_DECL)
3957 = finish_non_static_data_member (postfix_expression,
3959 else if (BASELINK_P (postfix_expression))
3964 /* See if any of the functions are non-static members. */
3965 fns = BASELINK_FUNCTIONS (postfix_expression);
3966 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3967 fns = TREE_OPERAND (fns, 0);
3968 for (fn = fns; fn; fn = OVL_NEXT (fn))
3969 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3971 /* If so, the expression may be relative to the current
3973 if (fn && current_class_type
3974 && DERIVED_FROM_P (qualifying_class, current_class_type))
3976 = (build_class_member_access_expr
3977 (maybe_dummy_object (qualifying_class, NULL),
3979 BASELINK_ACCESS_BINFO (postfix_expression),
3980 /*preserve_reference=*/false));
3982 return build_offset_ref (qualifying_class,
3983 postfix_expression);
3987 /* Remember that there was a reference to this entity. */
3988 if (DECL_P (postfix_expression))
3989 mark_used (postfix_expression);
3991 /* Keep looping until the postfix-expression is complete. */
3994 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3995 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3997 /* It is not a Koenig lookup function call. */
3998 unqualified_name_lookup_error (postfix_expression);
3999 postfix_expression = error_mark_node;
4002 /* Peek at the next token. */
4003 token = cp_lexer_peek_token (parser->lexer);
4005 switch (token->type)
4007 case CPP_OPEN_SQUARE:
4008 /* postfix-expression [ expression ] */
4012 /* Consume the `[' token. */
4013 cp_lexer_consume_token (parser->lexer);
4014 /* Parse the index expression. */
4015 index = cp_parser_expression (parser);
4016 /* Look for the closing `]'. */
4017 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4019 /* Build the ARRAY_REF. */
4021 = grok_array_decl (postfix_expression, index);
4022 idk = CP_PARSER_ID_KIND_NONE;
4026 case CPP_OPEN_PAREN:
4027 /* postfix-expression ( expression-list [opt] ) */
4031 /* Consume the `(' token. */
4032 cp_lexer_consume_token (parser->lexer);
4033 /* If the next token is not a `)', then there are some
4035 if (cp_lexer_next_token_is_not (parser->lexer,
4037 args = cp_parser_expression_list (parser);
4040 /* Look for the closing `)'. */
4041 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4043 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4044 && (is_overloaded_fn (postfix_expression)
4045 || DECL_P (postfix_expression)
4046 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4050 tree identifier = NULL_TREE;
4051 tree functions = NULL_TREE;
4053 /* Find the name of the overloaded function. */
4054 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4055 identifier = postfix_expression;
4056 else if (is_overloaded_fn (postfix_expression))
4058 functions = postfix_expression;
4059 identifier = DECL_NAME (get_first_fn (functions));
4061 else if (DECL_P (postfix_expression))
4063 functions = postfix_expression;
4064 identifier = DECL_NAME (postfix_expression);
4067 /* A call to a namespace-scope function using an
4070 Do Koenig lookup -- unless any of the arguments are
4072 for (arg = args; arg; arg = TREE_CHAIN (arg))
4073 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4078 = lookup_arg_dependent(identifier, functions, args);
4079 if (!postfix_expression)
4081 /* The unqualified name could not be resolved. */
4082 unqualified_name_lookup_error (identifier);
4083 postfix_expression = error_mark_node;
4086 = build_call_from_tree (postfix_expression, args,
4087 /*diallow_virtual=*/false);
4090 postfix_expression = build_min_nt (LOOKUP_EXPR,
4093 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4094 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4096 /* The unqualified name could not be resolved. */
4097 unqualified_name_lookup_error (postfix_expression);
4098 postfix_expression = error_mark_node;
4102 /* In the body of a template, no further processing is
4104 if (processing_template_decl)
4106 postfix_expression = build_nt (CALL_EXPR,
4112 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4114 = (build_new_method_call
4115 (TREE_OPERAND (postfix_expression, 0),
4116 TREE_OPERAND (postfix_expression, 1),
4118 (idk == CP_PARSER_ID_KIND_QUALIFIED
4119 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4120 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4121 postfix_expression = (build_offset_ref_call_from_tree
4122 (postfix_expression, args));
4123 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4124 /* A call to a static class member, or a namespace-scope
4127 = finish_call_expr (postfix_expression, args,
4128 /*disallow_virtual=*/true);
4130 /* All other function calls. */
4132 = finish_call_expr (postfix_expression, args,
4133 /*disallow_virtual=*/false);
4135 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4136 idk = CP_PARSER_ID_KIND_NONE;
4142 /* postfix-expression . template [opt] id-expression
4143 postfix-expression . pseudo-destructor-name
4144 postfix-expression -> template [opt] id-expression
4145 postfix-expression -> pseudo-destructor-name */
4150 tree scope = NULL_TREE;
4152 /* If this is a `->' operator, dereference the pointer. */
4153 if (token->type == CPP_DEREF)
4154 postfix_expression = build_x_arrow (postfix_expression);
4155 /* Check to see whether or not the expression is
4157 dependent_p = (cp_parser_type_dependent_expression_p
4158 (postfix_expression));
4159 /* The identifier following the `->' or `.' is not
4161 parser->scope = NULL_TREE;
4162 parser->qualifying_scope = NULL_TREE;
4163 parser->object_scope = NULL_TREE;
4164 /* Enter the scope corresponding to the type of the object
4165 given by the POSTFIX_EXPRESSION. */
4167 && TREE_TYPE (postfix_expression) != NULL_TREE)
4169 scope = TREE_TYPE (postfix_expression);
4170 /* According to the standard, no expression should
4171 ever have reference type. Unfortunately, we do not
4172 currently match the standard in this respect in
4173 that our internal representation of an expression
4174 may have reference type even when the standard says
4175 it does not. Therefore, we have to manually obtain
4176 the underlying type here. */
4177 if (TREE_CODE (scope) == REFERENCE_TYPE)
4178 scope = TREE_TYPE (scope);
4179 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4180 type of the field. We get an OFFSET_TYPE for
4185 Probably, we should not get an OFFSET_TYPE here;
4186 that transformation should be made only if `&S::T'
4188 if (TREE_CODE (scope) == OFFSET_TYPE)
4189 scope = TREE_TYPE (scope);
4190 /* The type of the POSTFIX_EXPRESSION must be
4192 scope = complete_type_or_else (scope, NULL_TREE);
4193 /* Let the name lookup machinery know that we are
4194 processing a class member access expression. */
4195 parser->context->object_type = scope;
4196 /* If something went wrong, we want to be able to
4197 discern that case, as opposed to the case where
4198 there was no SCOPE due to the type of expression
4201 scope = error_mark_node;
4204 /* Consume the `.' or `->' operator. */
4205 cp_lexer_consume_token (parser->lexer);
4206 /* If the SCOPE is not a scalar type, we are looking at an
4207 ordinary class member access expression, rather than a
4208 pseudo-destructor-name. */
4209 if (!scope || !SCALAR_TYPE_P (scope))
4211 template_p = cp_parser_optional_template_keyword (parser);
4212 /* Parse the id-expression. */
4213 name = cp_parser_id_expression (parser,
4215 /*check_dependency_p=*/true,
4216 /*template_p=*/NULL);
4217 /* In general, build a SCOPE_REF if the member name is
4218 qualified. However, if the name was not dependent
4219 and has already been resolved; there is no need to
4220 build the SCOPE_REF. For example;
4222 struct X { void f(); };
4223 template <typename T> void f(T* t) { t->X::f(); }
4225 Even though "t" is dependent, "X::f" is not and has
4226 except that for a BASELINK there is no need to
4227 include scope information. */
4228 if (name != error_mark_node
4229 && !BASELINK_P (name)
4232 name = build_nt (SCOPE_REF, parser->scope, name);
4233 parser->scope = NULL_TREE;
4234 parser->qualifying_scope = NULL_TREE;
4235 parser->object_scope = NULL_TREE;
4238 = finish_class_member_access_expr (postfix_expression, name);
4240 /* Otherwise, try the pseudo-destructor-name production. */
4246 /* Parse the pseudo-destructor-name. */
4247 cp_parser_pseudo_destructor_name (parser, &s, &type);
4248 /* Form the call. */
4250 = finish_pseudo_destructor_expr (postfix_expression,
4251 s, TREE_TYPE (type));
4254 /* We no longer need to look up names in the scope of the
4255 object on the left-hand side of the `.' or `->'
4257 parser->context->object_type = NULL_TREE;
4258 idk = CP_PARSER_ID_KIND_NONE;
4263 /* postfix-expression ++ */
4264 /* Consume the `++' token. */
4265 cp_lexer_consume_token (parser->lexer);
4266 /* Generate a reprsentation for the complete expression. */
4268 = finish_increment_expr (postfix_expression,
4269 POSTINCREMENT_EXPR);
4270 idk = CP_PARSER_ID_KIND_NONE;
4273 case CPP_MINUS_MINUS:
4274 /* postfix-expression -- */
4275 /* Consume the `--' token. */
4276 cp_lexer_consume_token (parser->lexer);
4277 /* Generate a reprsentation for the complete expression. */
4279 = finish_increment_expr (postfix_expression,
4280 POSTDECREMENT_EXPR);
4281 idk = CP_PARSER_ID_KIND_NONE;
4285 return postfix_expression;
4289 /* We should never get here. */
4291 return error_mark_node;
4294 /* Parse an expression-list.
4297 assignment-expression
4298 expression-list, assignment-expression
4300 Returns a TREE_LIST. The TREE_VALUE of each node is a
4301 representation of an assignment-expression. Note that a TREE_LIST
4302 is returned even if there is only a single expression in the list. */
4305 cp_parser_expression_list (parser)
4308 tree expression_list = NULL_TREE;
4310 /* Consume expressions until there are no more. */
4315 /* Parse the next assignment-expression. */
4316 expr = cp_parser_assignment_expression (parser);
4317 /* Add it to the list. */
4318 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4320 /* If the next token isn't a `,', then we are done. */
4321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4323 /* All uses of expression-list in the grammar are followed
4324 by a `)'. Therefore, if the next token is not a `)' an
4325 error will be issued, unless we are parsing tentatively.
4326 Skip ahead to see if there is another `,' before the `)';
4327 if so, we can go there and recover. */
4328 if (cp_parser_parsing_tentatively (parser)
4329 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4330 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4334 /* Otherwise, consume the `,' and keep going. */
4335 cp_lexer_consume_token (parser->lexer);
4338 /* We built up the list in reverse order so we must reverse it now. */
4339 return nreverse (expression_list);
4342 /* Parse a pseudo-destructor-name.
4344 pseudo-destructor-name:
4345 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4346 :: [opt] nested-name-specifier template template-id :: ~ type-name
4347 :: [opt] nested-name-specifier [opt] ~ type-name
4349 If either of the first two productions is used, sets *SCOPE to the
4350 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4351 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4352 or ERROR_MARK_NODE if no type-name is present. */
4355 cp_parser_pseudo_destructor_name (parser, scope, type)
4360 bool nested_name_specifier_p;
4362 /* Look for the optional `::' operator. */
4363 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4364 /* Look for the optional nested-name-specifier. */
4365 nested_name_specifier_p
4366 = (cp_parser_nested_name_specifier_opt (parser,
4367 /*typename_keyword_p=*/false,
4368 /*check_dependency_p=*/true,
4371 /* Now, if we saw a nested-name-specifier, we might be doing the
4372 second production. */
4373 if (nested_name_specifier_p
4374 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4376 /* Consume the `template' keyword. */
4377 cp_lexer_consume_token (parser->lexer);
4378 /* Parse the template-id. */
4379 cp_parser_template_id (parser,
4380 /*template_keyword_p=*/true,
4381 /*check_dependency_p=*/false);
4382 /* Look for the `::' token. */
4383 cp_parser_require (parser, CPP_SCOPE, "`::'");
4385 /* If the next token is not a `~', then there might be some
4386 additional qualification. */
4387 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4389 /* Look for the type-name. */
4390 *scope = TREE_TYPE (cp_parser_type_name (parser));
4391 /* Look for the `::' token. */
4392 cp_parser_require (parser, CPP_SCOPE, "`::'");
4397 /* Look for the `~'. */
4398 cp_parser_require (parser, CPP_COMPL, "`~'");
4399 /* Look for the type-name again. We are not responsible for
4400 checking that it matches the first type-name. */
4401 *type = cp_parser_type_name (parser);
4404 /* Parse a unary-expression.
4410 unary-operator cast-expression
4411 sizeof unary-expression
4419 __extension__ cast-expression
4420 __alignof__ unary-expression
4421 __alignof__ ( type-id )
4422 __real__ cast-expression
4423 __imag__ cast-expression
4426 ADDRESS_P is true iff the unary-expression is appearing as the
4427 operand of the `&' operator.
4429 Returns a representation of the expresion. */
4432 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4435 enum tree_code unary_operator;
4437 /* Peek at the next token. */
4438 token = cp_lexer_peek_token (parser->lexer);
4439 /* Some keywords give away the kind of expression. */
4440 if (token->type == CPP_KEYWORD)
4442 enum rid keyword = token->keyword;
4448 /* Consume the `alignof' token. */
4449 cp_lexer_consume_token (parser->lexer);
4450 /* Parse the operand. */
4451 return finish_alignof (cp_parser_sizeof_operand
4459 /* Consume the `sizeof' token. */
4460 cp_lexer_consume_token (parser->lexer);
4461 /* Parse the operand. */
4462 operand = cp_parser_sizeof_operand (parser, keyword);
4464 /* If the type of the operand cannot be determined build a
4466 if (TYPE_P (operand)
4467 ? cp_parser_dependent_type_p (operand)
4468 : cp_parser_type_dependent_expression_p (operand))
4469 return build_min (SIZEOF_EXPR, size_type_node, operand);
4470 /* Otherwise, compute the constant value. */
4472 return finish_sizeof (operand);
4476 return cp_parser_new_expression (parser);
4479 return cp_parser_delete_expression (parser);
4483 /* The saved value of the PEDANTIC flag. */
4487 /* Save away the PEDANTIC flag. */
4488 cp_parser_extension_opt (parser, &saved_pedantic);
4489 /* Parse the cast-expression. */
4490 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4491 /* Restore the PEDANTIC flag. */
4492 pedantic = saved_pedantic;
4502 /* Consume the `__real__' or `__imag__' token. */
4503 cp_lexer_consume_token (parser->lexer);
4504 /* Parse the cast-expression. */
4505 expression = cp_parser_cast_expression (parser,
4506 /*address_p=*/false);
4507 /* Create the complete representation. */
4508 return build_x_unary_op ((keyword == RID_REALPART
4509 ? REALPART_EXPR : IMAGPART_EXPR),
4519 /* Look for the `:: new' and `:: delete', which also signal the
4520 beginning of a new-expression, or delete-expression,
4521 respectively. If the next token is `::', then it might be one of
4523 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4527 /* See if the token after the `::' is one of the keywords in
4528 which we're interested. */
4529 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4530 /* If it's `new', we have a new-expression. */
4531 if (keyword == RID_NEW)
4532 return cp_parser_new_expression (parser);
4533 /* Similarly, for `delete'. */
4534 else if (keyword == RID_DELETE)
4535 return cp_parser_delete_expression (parser);
4538 /* Look for a unary operator. */
4539 unary_operator = cp_parser_unary_operator (token);
4540 /* The `++' and `--' operators can be handled similarly, even though
4541 they are not technically unary-operators in the grammar. */
4542 if (unary_operator == ERROR_MARK)
4544 if (token->type == CPP_PLUS_PLUS)
4545 unary_operator = PREINCREMENT_EXPR;
4546 else if (token->type == CPP_MINUS_MINUS)
4547 unary_operator = PREDECREMENT_EXPR;
4548 /* Handle the GNU address-of-label extension. */
4549 else if (cp_parser_allow_gnu_extensions_p (parser)
4550 && token->type == CPP_AND_AND)
4554 /* Consume the '&&' token. */
4555 cp_lexer_consume_token (parser->lexer);
4556 /* Look for the identifier. */
4557 identifier = cp_parser_identifier (parser);
4558 /* Create an expression representing the address. */
4559 return finish_label_address_expr (identifier);
4562 if (unary_operator != ERROR_MARK)
4564 tree cast_expression;
4566 /* Consume the operator token. */
4567 token = cp_lexer_consume_token (parser->lexer);
4568 /* Parse the cast-expression. */
4570 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4571 /* Now, build an appropriate representation. */
4572 switch (unary_operator)
4575 return build_x_indirect_ref (cast_expression, "unary *");
4578 return build_x_unary_op (ADDR_EXPR, cast_expression);
4582 case TRUTH_NOT_EXPR:
4583 case PREINCREMENT_EXPR:
4584 case PREDECREMENT_EXPR:
4585 return finish_unary_op_expr (unary_operator, cast_expression);
4588 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4592 return error_mark_node;
4596 return cp_parser_postfix_expression (parser, address_p);
4599 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4600 unary-operator, the corresponding tree code is returned. */
4602 static enum tree_code
4603 cp_parser_unary_operator (token)
4606 switch (token->type)
4609 return INDIRECT_REF;
4615 return CONVERT_EXPR;
4621 return TRUTH_NOT_EXPR;
4624 return BIT_NOT_EXPR;
4631 /* Parse a new-expression.
4633 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4634 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4636 Returns a representation of the expression. */
4639 cp_parser_new_expression (parser)
4642 bool global_scope_p;
4647 /* Look for the optional `::' operator. */
4649 = (cp_parser_global_scope_opt (parser,
4650 /*current_scope_valid_p=*/false)
4652 /* Look for the `new' operator. */
4653 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4654 /* There's no easy way to tell a new-placement from the
4655 `( type-id )' construct. */
4656 cp_parser_parse_tentatively (parser);
4657 /* Look for a new-placement. */
4658 placement = cp_parser_new_placement (parser);
4659 /* If that didn't work out, there's no new-placement. */
4660 if (!cp_parser_parse_definitely (parser))
4661 placement = NULL_TREE;
4663 /* If the next token is a `(', then we have a parenthesized
4665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4667 /* Consume the `('. */
4668 cp_lexer_consume_token (parser->lexer);
4669 /* Parse the type-id. */
4670 type = cp_parser_type_id (parser);
4671 /* Look for the closing `)'. */
4672 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4674 /* Otherwise, there must be a new-type-id. */
4676 type = cp_parser_new_type_id (parser);
4678 /* If the next token is a `(', then we have a new-initializer. */
4679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4680 initializer = cp_parser_new_initializer (parser);
4682 initializer = NULL_TREE;
4684 /* Create a representation of the new-expression. */
4685 return build_new (placement, type, initializer, global_scope_p);
4688 /* Parse a new-placement.
4693 Returns the same representation as for an expression-list. */
4696 cp_parser_new_placement (parser)
4699 tree expression_list;
4701 /* Look for the opening `('. */
4702 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4703 return error_mark_node;
4704 /* Parse the expression-list. */
4705 expression_list = cp_parser_expression_list (parser);
4706 /* Look for the closing `)'. */
4707 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4709 return expression_list;
4712 /* Parse a new-type-id.
4715 type-specifier-seq new-declarator [opt]
4717 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4718 and whose TREE_VALUE is the new-declarator. */
4721 cp_parser_new_type_id (parser)
4724 tree type_specifier_seq;
4726 const char *saved_message;
4728 /* The type-specifier sequence must not contain type definitions.
4729 (It cannot contain declarations of new types either, but if they
4730 are not definitions we will catch that because they are not
4732 saved_message = parser->type_definition_forbidden_message;
4733 parser->type_definition_forbidden_message
4734 = "types may not be defined in a new-type-id";
4735 /* Parse the type-specifier-seq. */
4736 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4737 /* Restore the old message. */
4738 parser->type_definition_forbidden_message = saved_message;
4739 /* Parse the new-declarator. */
4740 declarator = cp_parser_new_declarator_opt (parser);
4742 return build_tree_list (type_specifier_seq, declarator);
4745 /* Parse an (optional) new-declarator.
4748 ptr-operator new-declarator [opt]
4749 direct-new-declarator
4751 Returns a representation of the declarator. See
4752 cp_parser_declarator for the representations used. */
4755 cp_parser_new_declarator_opt (parser)
4758 enum tree_code code;
4760 tree cv_qualifier_seq;
4762 /* We don't know if there's a ptr-operator next, or not. */
4763 cp_parser_parse_tentatively (parser);
4764 /* Look for a ptr-operator. */
4765 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4766 /* If that worked, look for more new-declarators. */
4767 if (cp_parser_parse_definitely (parser))
4771 /* Parse another optional declarator. */
4772 declarator = cp_parser_new_declarator_opt (parser);
4774 /* Create the representation of the declarator. */
4775 if (code == INDIRECT_REF)
4776 declarator = make_pointer_declarator (cv_qualifier_seq,
4779 declarator = make_reference_declarator (cv_qualifier_seq,
4782 /* Handle the pointer-to-member case. */
4784 declarator = build_nt (SCOPE_REF, type, declarator);
4789 /* If the next token is a `[', there is a direct-new-declarator. */
4790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4791 return cp_parser_direct_new_declarator (parser);
4796 /* Parse a direct-new-declarator.
4798 direct-new-declarator:
4800 direct-new-declarator [constant-expression]
4802 Returns an ARRAY_REF, following the same conventions as are
4803 documented for cp_parser_direct_declarator. */
4806 cp_parser_direct_new_declarator (parser)
4809 tree declarator = NULL_TREE;
4815 /* Look for the opening `['. */
4816 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4817 /* The first expression is not required to be constant. */
4820 expression = cp_parser_expression (parser);
4821 /* The standard requires that the expression have integral
4822 type. DR 74 adds enumeration types. We believe that the
4823 real intent is that these expressions be handled like the
4824 expression in a `switch' condition, which also allows
4825 classes with a single conversion to integral or
4826 enumeration type. */
4827 if (!processing_template_decl)
4830 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4835 error ("expression in new-declarator must have integral or enumeration type");
4836 expression = error_mark_node;
4840 /* But all the other expressions must be. */
4842 expression = cp_parser_constant_expression (parser);
4843 /* Look for the closing `]'. */
4844 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4846 /* Add this bound to the declarator. */
4847 declarator = build_nt (ARRAY_REF, declarator, expression);
4849 /* If the next token is not a `[', then there are no more
4851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4858 /* Parse a new-initializer.
4861 ( expression-list [opt] )
4863 Returns a reprsentation of the expression-list. If there is no
4864 expression-list, VOID_ZERO_NODE is returned. */
4867 cp_parser_new_initializer (parser)
4870 tree expression_list;
4872 /* Look for the opening parenthesis. */
4873 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4874 /* If the next token is not a `)', then there is an
4876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4877 expression_list = cp_parser_expression_list (parser);
4879 expression_list = void_zero_node;
4880 /* Look for the closing parenthesis. */
4881 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4883 return expression_list;
4886 /* Parse a delete-expression.
4889 :: [opt] delete cast-expression
4890 :: [opt] delete [ ] cast-expression
4892 Returns a representation of the expression. */
4895 cp_parser_delete_expression (parser)
4898 bool global_scope_p;
4902 /* Look for the optional `::' operator. */
4904 = (cp_parser_global_scope_opt (parser,
4905 /*current_scope_valid_p=*/false)
4907 /* Look for the `delete' keyword. */
4908 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4909 /* See if the array syntax is in use. */
4910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4912 /* Consume the `[' token. */
4913 cp_lexer_consume_token (parser->lexer);
4914 /* Look for the `]' token. */
4915 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4916 /* Remember that this is the `[]' construct. */
4922 /* Parse the cast-expression. */
4923 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
4925 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4928 /* Parse a cast-expression.
4932 ( type-id ) cast-expression
4934 Returns a representation of the expression. */
4937 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4939 /* If it's a `(', then we might be looking at a cast. */
4940 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4942 tree type = NULL_TREE;
4943 tree expr = NULL_TREE;
4944 bool compound_literal_p;
4945 const char *saved_message;
4947 /* There's no way to know yet whether or not this is a cast.
4948 For example, `(int (3))' is a unary-expression, while `(int)
4949 3' is a cast. So, we resort to parsing tentatively. */
4950 cp_parser_parse_tentatively (parser);
4951 /* Types may not be defined in a cast. */
4952 saved_message = parser->type_definition_forbidden_message;
4953 parser->type_definition_forbidden_message
4954 = "types may not be defined in casts";
4955 /* Consume the `('. */
4956 cp_lexer_consume_token (parser->lexer);
4957 /* A very tricky bit is that `(struct S) { 3 }' is a
4958 compound-literal (which we permit in C++ as an extension).
4959 But, that construct is not a cast-expression -- it is a
4960 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4961 is legal; if the compound-literal were a cast-expression,
4962 you'd need an extra set of parentheses.) But, if we parse
4963 the type-id, and it happens to be a class-specifier, then we
4964 will commit to the parse at that point, because we cannot
4965 undo the action that is done when creating a new class. So,
4966 then we cannot back up and do a postfix-expression.
4968 Therefore, we scan ahead to the closing `)', and check to see
4969 if the token after the `)' is a `{'. If so, we are not
4970 looking at a cast-expression.
4972 Save tokens so that we can put them back. */
4973 cp_lexer_save_tokens (parser->lexer);
4974 /* Skip tokens until the next token is a closing parenthesis.
4975 If we find the closing `)', and the next token is a `{', then
4976 we are looking at a compound-literal. */
4978 = (cp_parser_skip_to_closing_parenthesis (parser)
4979 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4980 /* Roll back the tokens we skipped. */
4981 cp_lexer_rollback_tokens (parser->lexer);
4982 /* If we were looking at a compound-literal, simulate an error
4983 so that the call to cp_parser_parse_definitely below will
4985 if (compound_literal_p)
4986 cp_parser_simulate_error (parser);
4989 /* Look for the type-id. */
4990 type = cp_parser_type_id (parser);
4991 /* Look for the closing `)'. */
4992 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4995 /* Restore the saved message. */
4996 parser->type_definition_forbidden_message = saved_message;
4998 /* If all went well, this is a cast. */
4999 if (cp_parser_parse_definitely (parser))
5001 /* Parse the dependent expression. */
5002 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5003 /* Warn about old-style casts, if so requested. */
5004 if (warn_old_style_cast
5005 && !in_system_header
5006 && !VOID_TYPE_P (type)
5007 && current_lang_name != lang_name_c)
5008 warning ("use of old-style cast");
5009 /* Perform the cast. */
5010 expr = build_c_cast (type, expr);
5017 /* If we get here, then it's not a cast, so it must be a
5018 unary-expression. */
5019 return cp_parser_unary_expression (parser, address_p);
5022 /* Parse a pm-expression.
5026 pm-expression .* cast-expression
5027 pm-expression ->* cast-expression
5029 Returns a representation of the expression. */
5032 cp_parser_pm_expression (parser)
5038 /* Parse the cast-expresion. */
5039 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5040 pm_expr = cast_expr;
5041 /* Now look for pointer-to-member operators. */
5045 enum cpp_ttype token_type;
5047 /* Peek at the next token. */
5048 token = cp_lexer_peek_token (parser->lexer);
5049 token_type = token->type;
5050 /* If it's not `.*' or `->*' there's no pointer-to-member
5052 if (token_type != CPP_DOT_STAR
5053 && token_type != CPP_DEREF_STAR)
5056 /* Consume the token. */
5057 cp_lexer_consume_token (parser->lexer);
5059 /* Parse another cast-expression. */
5060 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5062 /* Build the representation of the pointer-to-member
5064 if (token_type == CPP_DEREF_STAR)
5065 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5067 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5073 /* Parse a multiplicative-expression.
5075 mulitplicative-expression:
5077 multiplicative-expression * pm-expression
5078 multiplicative-expression / pm-expression
5079 multiplicative-expression % pm-expression
5081 Returns a representation of the expression. */
5084 cp_parser_multiplicative_expression (parser)
5087 static const cp_parser_token_tree_map map = {
5088 { CPP_MULT, MULT_EXPR },
5089 { CPP_DIV, TRUNC_DIV_EXPR },
5090 { CPP_MOD, TRUNC_MOD_EXPR },
5091 { CPP_EOF, ERROR_MARK }
5094 return cp_parser_binary_expression (parser,
5096 cp_parser_pm_expression);
5099 /* Parse an additive-expression.
5101 additive-expression:
5102 multiplicative-expression
5103 additive-expression + multiplicative-expression
5104 additive-expression - multiplicative-expression
5106 Returns a representation of the expression. */
5109 cp_parser_additive_expression (parser)
5112 static const cp_parser_token_tree_map map = {
5113 { CPP_PLUS, PLUS_EXPR },
5114 { CPP_MINUS, MINUS_EXPR },
5115 { CPP_EOF, ERROR_MARK }
5118 return cp_parser_binary_expression (parser,
5120 cp_parser_multiplicative_expression);
5123 /* Parse a shift-expression.
5127 shift-expression << additive-expression
5128 shift-expression >> additive-expression
5130 Returns a representation of the expression. */
5133 cp_parser_shift_expression (parser)
5136 static const cp_parser_token_tree_map map = {
5137 { CPP_LSHIFT, LSHIFT_EXPR },
5138 { CPP_RSHIFT, RSHIFT_EXPR },
5139 { CPP_EOF, ERROR_MARK }
5142 return cp_parser_binary_expression (parser,
5144 cp_parser_additive_expression);
5147 /* Parse a relational-expression.
5149 relational-expression:
5151 relational-expression < shift-expression
5152 relational-expression > shift-expression
5153 relational-expression <= shift-expression
5154 relational-expression >= shift-expression
5158 relational-expression:
5159 relational-expression <? shift-expression
5160 relational-expression >? shift-expression
5162 Returns a representation of the expression. */
5165 cp_parser_relational_expression (parser)
5168 static const cp_parser_token_tree_map map = {
5169 { CPP_LESS, LT_EXPR },
5170 { CPP_GREATER, GT_EXPR },
5171 { CPP_LESS_EQ, LE_EXPR },
5172 { CPP_GREATER_EQ, GE_EXPR },
5173 { CPP_MIN, MIN_EXPR },
5174 { CPP_MAX, MAX_EXPR },
5175 { CPP_EOF, ERROR_MARK }
5178 return cp_parser_binary_expression (parser,
5180 cp_parser_shift_expression);
5183 /* Parse an equality-expression.
5185 equality-expression:
5186 relational-expression
5187 equality-expression == relational-expression
5188 equality-expression != relational-expression
5190 Returns a representation of the expression. */
5193 cp_parser_equality_expression (parser)
5196 static const cp_parser_token_tree_map map = {
5197 { CPP_EQ_EQ, EQ_EXPR },
5198 { CPP_NOT_EQ, NE_EXPR },
5199 { CPP_EOF, ERROR_MARK }
5202 return cp_parser_binary_expression (parser,
5204 cp_parser_relational_expression);
5207 /* Parse an and-expression.
5211 and-expression & equality-expression
5213 Returns a representation of the expression. */
5216 cp_parser_and_expression (parser)
5219 static const cp_parser_token_tree_map map = {
5220 { CPP_AND, BIT_AND_EXPR },
5221 { CPP_EOF, ERROR_MARK }
5224 return cp_parser_binary_expression (parser,
5226 cp_parser_equality_expression);
5229 /* Parse an exclusive-or-expression.
5231 exclusive-or-expression:
5233 exclusive-or-expression ^ and-expression
5235 Returns a representation of the expression. */
5238 cp_parser_exclusive_or_expression (parser)
5241 static const cp_parser_token_tree_map map = {
5242 { CPP_XOR, BIT_XOR_EXPR },
5243 { CPP_EOF, ERROR_MARK }
5246 return cp_parser_binary_expression (parser,
5248 cp_parser_and_expression);
5252 /* Parse an inclusive-or-expression.
5254 inclusive-or-expression:
5255 exclusive-or-expression
5256 inclusive-or-expression | exclusive-or-expression
5258 Returns a representation of the expression. */
5261 cp_parser_inclusive_or_expression (parser)
5264 static const cp_parser_token_tree_map map = {
5265 { CPP_OR, BIT_IOR_EXPR },
5266 { CPP_EOF, ERROR_MARK }
5269 return cp_parser_binary_expression (parser,
5271 cp_parser_exclusive_or_expression);
5274 /* Parse a logical-and-expression.
5276 logical-and-expression:
5277 inclusive-or-expression
5278 logical-and-expression && inclusive-or-expression
5280 Returns a representation of the expression. */
5283 cp_parser_logical_and_expression (parser)
5286 static const cp_parser_token_tree_map map = {
5287 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5288 { CPP_EOF, ERROR_MARK }
5291 return cp_parser_binary_expression (parser,
5293 cp_parser_inclusive_or_expression);
5296 /* Parse a logical-or-expression.
5298 logical-or-expression:
5299 logical-and-expresion
5300 logical-or-expression || logical-and-expression
5302 Returns a representation of the expression. */
5305 cp_parser_logical_or_expression (parser)
5308 static const cp_parser_token_tree_map map = {
5309 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5310 { CPP_EOF, ERROR_MARK }
5313 return cp_parser_binary_expression (parser,
5315 cp_parser_logical_and_expression);
5318 /* Parse a conditional-expression.
5320 conditional-expression:
5321 logical-or-expression
5322 logical-or-expression ? expression : assignment-expression
5326 conditional-expression:
5327 logical-or-expression ? : assignment-expression
5329 Returns a representation of the expression. */
5332 cp_parser_conditional_expression (parser)
5335 tree logical_or_expr;
5337 /* Parse the logical-or-expression. */
5338 logical_or_expr = cp_parser_logical_or_expression (parser);
5339 /* If the next token is a `?', then we have a real conditional
5341 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5342 return cp_parser_question_colon_clause (parser, logical_or_expr);
5343 /* Otherwise, the value is simply the logical-or-expression. */
5345 return logical_or_expr;
5348 /* Parse the `? expression : assignment-expression' part of a
5349 conditional-expression. The LOGICAL_OR_EXPR is the
5350 logical-or-expression that started the conditional-expression.
5351 Returns a representation of the entire conditional-expression.
5353 This routine exists only so that it can be shared between
5354 cp_parser_conditional_expression and
5355 cp_parser_assignment_expression.
5357 ? expression : assignment-expression
5361 ? : assignment-expression */
5364 cp_parser_question_colon_clause (parser, logical_or_expr)
5366 tree logical_or_expr;
5369 tree assignment_expr;
5371 /* Consume the `?' token. */
5372 cp_lexer_consume_token (parser->lexer);
5373 if (cp_parser_allow_gnu_extensions_p (parser)
5374 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5375 /* Implicit true clause. */
5378 /* Parse the expression. */
5379 expr = cp_parser_expression (parser);
5381 /* The next token should be a `:'. */
5382 cp_parser_require (parser, CPP_COLON, "`:'");
5383 /* Parse the assignment-expression. */
5384 assignment_expr = cp_parser_assignment_expression (parser);
5386 /* Build the conditional-expression. */
5387 return build_x_conditional_expr (logical_or_expr,
5392 /* Parse an assignment-expression.
5394 assignment-expression:
5395 conditional-expression
5396 logical-or-expression assignment-operator assignment_expression
5399 Returns a representation for the expression. */
5402 cp_parser_assignment_expression (parser)
5407 /* If the next token is the `throw' keyword, then we're looking at
5408 a throw-expression. */
5409 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5410 expr = cp_parser_throw_expression (parser);
5411 /* Otherwise, it must be that we are looking at a
5412 logical-or-expression. */
5415 /* Parse the logical-or-expression. */
5416 expr = cp_parser_logical_or_expression (parser);
5417 /* If the next token is a `?' then we're actually looking at a
5418 conditional-expression. */
5419 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5420 return cp_parser_question_colon_clause (parser, expr);
5423 enum tree_code assignment_operator;
5425 /* If it's an assignment-operator, we're using the second
5428 = cp_parser_assignment_operator_opt (parser);
5429 if (assignment_operator != ERROR_MARK)
5433 /* Parse the right-hand side of the assignment. */
5434 rhs = cp_parser_assignment_expression (parser);
5435 /* Build the asignment expression. */
5436 expr = build_x_modify_expr (expr,
5437 assignment_operator,
5446 /* Parse an (optional) assignment-operator.
5448 assignment-operator: one of
5449 = *= /= %= += -= >>= <<= &= ^= |=
5453 assignment-operator: one of
5456 If the next token is an assignment operator, the corresponding tree
5457 code is returned, and the token is consumed. For example, for
5458 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5459 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5460 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5461 operator, ERROR_MARK is returned. */
5463 static enum tree_code
5464 cp_parser_assignment_operator_opt (parser)
5470 /* Peek at the next toen. */
5471 token = cp_lexer_peek_token (parser->lexer);
5473 switch (token->type)
5484 op = TRUNC_DIV_EXPR;
5488 op = TRUNC_MOD_EXPR;
5528 /* Nothing else is an assignment operator. */
5532 /* If it was an assignment operator, consume it. */
5533 if (op != ERROR_MARK)
5534 cp_lexer_consume_token (parser->lexer);
5539 /* Parse an expression.
5542 assignment-expression
5543 expression , assignment-expression
5545 Returns a representation of the expression. */
5548 cp_parser_expression (parser)
5551 tree expression = NULL_TREE;
5552 bool saw_comma_p = false;
5556 tree assignment_expression;
5558 /* Parse the next assignment-expression. */
5559 assignment_expression
5560 = cp_parser_assignment_expression (parser);
5561 /* If this is the first assignment-expression, we can just
5564 expression = assignment_expression;
5565 /* Otherwise, chain the expressions together. It is unclear why
5566 we do not simply build COMPOUND_EXPRs as we go. */
5568 expression = tree_cons (NULL_TREE,
5569 assignment_expression,
5571 /* If the next token is not a comma, then we are done with the
5573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5575 /* Consume the `,'. */
5576 cp_lexer_consume_token (parser->lexer);
5577 /* The first time we see a `,', we must take special action
5578 because the representation used for a single expression is
5579 different from that used for a list containing the single
5583 /* Remember that this expression has a `,' in it. */
5585 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5586 additional expressions to it. */
5587 expression = build_tree_list (NULL_TREE, expression);
5591 /* Build a COMPOUND_EXPR to represent the entire expression, if
5592 necessary. We built up the list in reverse order, so we must
5593 straighten it out here. */
5595 expression = build_x_compound_expr (nreverse (expression));
5600 /* Parse a constant-expression.
5602 constant-expression:
5603 conditional-expression */
5606 cp_parser_constant_expression (parser)
5609 bool saved_constant_expression_p;
5612 /* It might seem that we could simply parse the
5613 conditional-expression, and then check to see if it were
5614 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5615 one that the compiler can figure out is constant, possibly after
5616 doing some simplifications or optimizations. The standard has a
5617 precise definition of constant-expression, and we must honor
5618 that, even though it is somewhat more restrictive.
5624 is not a legal declaration, because `(2, 3)' is not a
5625 constant-expression. The `,' operator is forbidden in a
5626 constant-expression. However, GCC's constant-folding machinery
5627 will fold this operation to an INTEGER_CST for `3'. */
5629 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5630 saved_constant_expression_p = parser->constant_expression_p;
5631 /* We are now parsing a constant-expression. */
5632 parser->constant_expression_p = true;
5633 /* Parse the conditional-expression. */
5634 expression = cp_parser_conditional_expression (parser);
5635 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5636 parser->constant_expression_p = saved_constant_expression_p;
5641 /* Statements [gram.stmt.stmt] */
5643 /* Parse a statement.
5647 expression-statement
5652 declaration-statement
5656 cp_parser_statement (parser)
5661 int statement_line_number;
5663 /* There is no statement yet. */
5664 statement = NULL_TREE;
5665 /* Peek at the next token. */
5666 token = cp_lexer_peek_token (parser->lexer);
5667 /* Remember the line number of the first token in the statement. */
5668 statement_line_number = token->line_number;
5669 /* If this is a keyword, then that will often determine what kind of
5670 statement we have. */
5671 if (token->type == CPP_KEYWORD)
5673 enum rid keyword = token->keyword;
5679 statement = cp_parser_labeled_statement (parser);
5684 statement = cp_parser_selection_statement (parser);
5690 statement = cp_parser_iteration_statement (parser);
5697 statement = cp_parser_jump_statement (parser);
5701 statement = cp_parser_try_block (parser);
5705 /* It might be a keyword like `int' that can start a
5706 declaration-statement. */
5710 else if (token->type == CPP_NAME)
5712 /* If the next token is a `:', then we are looking at a
5713 labeled-statement. */
5714 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5715 if (token->type == CPP_COLON)
5716 statement = cp_parser_labeled_statement (parser);
5718 /* Anything that starts with a `{' must be a compound-statement. */
5719 else if (token->type == CPP_OPEN_BRACE)
5720 statement = cp_parser_compound_statement (parser);
5722 /* Everything else must be a declaration-statement or an
5723 expression-statement. Try for the declaration-statement
5724 first, unless we are looking at a `;', in which case we know that
5725 we have an expression-statement. */
5728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5730 cp_parser_parse_tentatively (parser);
5731 /* Try to parse the declaration-statement. */
5732 cp_parser_declaration_statement (parser);
5733 /* If that worked, we're done. */
5734 if (cp_parser_parse_definitely (parser))
5737 /* Look for an expression-statement instead. */
5738 statement = cp_parser_expression_statement (parser);
5741 /* Set the line number for the statement. */
5742 if (statement && statement_code_p (TREE_CODE (statement)))
5743 STMT_LINENO (statement) = statement_line_number;
5746 /* Parse a labeled-statement.
5749 identifier : statement
5750 case constant-expression : statement
5753 Returns the new CASE_LABEL, for a `case' or `default' label. For
5754 an ordinary label, returns a LABEL_STMT. */
5757 cp_parser_labeled_statement (parser)
5761 tree statement = NULL_TREE;
5763 /* The next token should be an identifier. */
5764 token = cp_lexer_peek_token (parser->lexer);
5765 if (token->type != CPP_NAME
5766 && token->type != CPP_KEYWORD)
5768 cp_parser_error (parser, "expected labeled-statement");
5769 return error_mark_node;
5772 switch (token->keyword)
5778 /* Consume the `case' token. */
5779 cp_lexer_consume_token (parser->lexer);
5780 /* Parse the constant-expression. */
5781 expr = cp_parser_constant_expression (parser);
5782 /* Create the label. */
5783 statement = finish_case_label (expr, NULL_TREE);
5788 /* Consume the `default' token. */
5789 cp_lexer_consume_token (parser->lexer);
5790 /* Create the label. */
5791 statement = finish_case_label (NULL_TREE, NULL_TREE);
5795 /* Anything else must be an ordinary label. */
5796 statement = finish_label_stmt (cp_parser_identifier (parser));
5800 /* Require the `:' token. */
5801 cp_parser_require (parser, CPP_COLON, "`:'");
5802 /* Parse the labeled statement. */
5803 cp_parser_statement (parser);
5805 /* Return the label, in the case of a `case' or `default' label. */
5809 /* Parse an expression-statement.
5811 expression-statement:
5814 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5815 statement consists of nothing more than an `;'. */
5818 cp_parser_expression_statement (parser)
5823 /* If the next token is not a `;', then there is an expression to parse. */
5824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5825 statement = finish_expr_stmt (cp_parser_expression (parser));
5826 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5830 statement = NULL_TREE;
5832 /* Consume the final `;'. */
5833 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5835 /* If there is additional (erroneous) input, skip to the end of
5837 cp_parser_skip_to_end_of_statement (parser);
5838 /* If the next token is now a `;', consume it. */
5839 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5840 cp_lexer_consume_token (parser->lexer);
5846 /* Parse a compound-statement.
5849 { statement-seq [opt] }
5851 Returns a COMPOUND_STMT representing the statement. */
5854 cp_parser_compound_statement (cp_parser *parser)
5858 /* Consume the `{'. */
5859 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5860 return error_mark_node;
5861 /* Begin the compound-statement. */
5862 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5863 /* Parse an (optional) statement-seq. */
5864 cp_parser_statement_seq_opt (parser);
5865 /* Finish the compound-statement. */
5866 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5867 /* Consume the `}'. */
5868 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5870 return compound_stmt;
5873 /* Parse an (optional) statement-seq.
5877 statement-seq [opt] statement */
5880 cp_parser_statement_seq_opt (parser)
5883 /* Scan statements until there aren't any more. */
5886 /* If we're looking at a `}', then we've run out of statements. */
5887 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5888 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5891 /* Parse the statement. */
5892 cp_parser_statement (parser);
5896 /* Parse a selection-statement.
5898 selection-statement:
5899 if ( condition ) statement
5900 if ( condition ) statement else statement
5901 switch ( condition ) statement
5903 Returns the new IF_STMT or SWITCH_STMT. */
5906 cp_parser_selection_statement (parser)
5912 /* Peek at the next token. */
5913 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5915 /* See what kind of keyword it is. */
5916 keyword = token->keyword;
5925 /* Look for the `('. */
5926 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5928 cp_parser_skip_to_end_of_statement (parser);
5929 return error_mark_node;
5932 /* Begin the selection-statement. */
5933 if (keyword == RID_IF)
5934 statement = begin_if_stmt ();
5936 statement = begin_switch_stmt ();
5938 /* Parse the condition. */
5939 condition = cp_parser_condition (parser);
5940 /* Look for the `)'. */
5941 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5942 cp_parser_skip_to_closing_parenthesis (parser);
5944 if (keyword == RID_IF)
5948 /* Add the condition. */
5949 finish_if_stmt_cond (condition, statement);
5951 /* Parse the then-clause. */
5952 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5953 finish_then_clause (statement);
5955 /* If the next token is `else', parse the else-clause. */
5956 if (cp_lexer_next_token_is_keyword (parser->lexer,
5961 /* Consume the `else' keyword. */
5962 cp_lexer_consume_token (parser->lexer);
5963 /* Parse the else-clause. */
5965 = cp_parser_implicitly_scoped_statement (parser);
5966 finish_else_clause (statement);
5969 /* Now we're all done with the if-statement. */
5976 /* Add the condition. */
5977 finish_switch_cond (condition, statement);
5979 /* Parse the body of the switch-statement. */
5980 body = cp_parser_implicitly_scoped_statement (parser);
5982 /* Now we're all done with the switch-statement. */
5983 finish_switch_stmt (statement);
5991 cp_parser_error (parser, "expected selection-statement");
5992 return error_mark_node;
5996 /* Parse a condition.
6000 type-specifier-seq declarator = assignment-expression
6005 type-specifier-seq declarator asm-specification [opt]
6006 attributes [opt] = assignment-expression
6008 Returns the expression that should be tested. */
6011 cp_parser_condition (parser)
6014 tree type_specifiers;
6015 const char *saved_message;
6017 /* Try the declaration first. */
6018 cp_parser_parse_tentatively (parser);
6019 /* New types are not allowed in the type-specifier-seq for a
6021 saved_message = parser->type_definition_forbidden_message;
6022 parser->type_definition_forbidden_message
6023 = "types may not be defined in conditions";
6024 /* Parse the type-specifier-seq. */
6025 type_specifiers = cp_parser_type_specifier_seq (parser);
6026 /* Restore the saved message. */
6027 parser->type_definition_forbidden_message = saved_message;
6028 /* If all is well, we might be looking at a declaration. */
6029 if (!cp_parser_error_occurred (parser))
6032 tree asm_specification;
6035 tree initializer = NULL_TREE;
6037 /* Parse the declarator. */
6038 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6039 /*ctor_dtor_or_conv_p=*/NULL);
6040 /* Parse the attributes. */
6041 attributes = cp_parser_attributes_opt (parser);
6042 /* Parse the asm-specification. */
6043 asm_specification = cp_parser_asm_specification_opt (parser);
6044 /* If the next token is not an `=', then we might still be
6045 looking at an expression. For example:
6049 looks like a decl-specifier-seq and a declarator -- but then
6050 there is no `=', so this is an expression. */
6051 cp_parser_require (parser, CPP_EQ, "`='");
6052 /* If we did see an `=', then we are looking at a declaration
6054 if (cp_parser_parse_definitely (parser))
6056 /* Create the declaration. */
6057 decl = start_decl (declarator, type_specifiers,
6058 /*initialized_p=*/true,
6059 attributes, /*prefix_attributes=*/NULL_TREE);
6060 /* Parse the assignment-expression. */
6061 initializer = cp_parser_assignment_expression (parser);
6063 /* Process the initializer. */
6064 cp_finish_decl (decl,
6067 LOOKUP_ONLYCONVERTING);
6069 return convert_from_reference (decl);
6072 /* If we didn't even get past the declarator successfully, we are
6073 definitely not looking at a declaration. */
6075 cp_parser_abort_tentative_parse (parser);
6077 /* Otherwise, we are looking at an expression. */
6078 return cp_parser_expression (parser);
6081 /* Parse an iteration-statement.
6083 iteration-statement:
6084 while ( condition ) statement
6085 do statement while ( expression ) ;
6086 for ( for-init-statement condition [opt] ; expression [opt] )
6089 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6092 cp_parser_iteration_statement (parser)
6099 /* Peek at the next token. */
6100 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6102 return error_mark_node;
6104 /* See what kind of keyword it is. */
6105 keyword = token->keyword;
6112 /* Begin the while-statement. */
6113 statement = begin_while_stmt ();
6114 /* Look for the `('. */
6115 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6116 /* Parse the condition. */
6117 condition = cp_parser_condition (parser);
6118 finish_while_stmt_cond (condition, statement);
6119 /* Look for the `)'. */
6120 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6121 /* Parse the dependent statement. */
6122 cp_parser_already_scoped_statement (parser);
6123 /* We're done with the while-statement. */
6124 finish_while_stmt (statement);
6132 /* Begin the do-statement. */
6133 statement = begin_do_stmt ();
6134 /* Parse the body of the do-statement. */
6135 cp_parser_implicitly_scoped_statement (parser);
6136 finish_do_body (statement);
6137 /* Look for the `while' keyword. */
6138 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6139 /* Look for the `('. */
6140 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6141 /* Parse the expression. */
6142 expression = cp_parser_expression (parser);
6143 /* We're done with the do-statement. */
6144 finish_do_stmt (expression, statement);
6145 /* Look for the `)'. */
6146 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6147 /* Look for the `;'. */
6148 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6154 tree condition = NULL_TREE;
6155 tree expression = NULL_TREE;
6157 /* Begin the for-statement. */
6158 statement = begin_for_stmt ();
6159 /* Look for the `('. */
6160 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6161 /* Parse the initialization. */
6162 cp_parser_for_init_statement (parser);
6163 finish_for_init_stmt (statement);
6165 /* If there's a condition, process it. */
6166 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6167 condition = cp_parser_condition (parser);
6168 finish_for_cond (condition, statement);
6169 /* Look for the `;'. */
6170 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6172 /* If there's an expression, process it. */
6173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6174 expression = cp_parser_expression (parser);
6175 finish_for_expr (expression, statement);
6176 /* Look for the `)'. */
6177 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6179 /* Parse the body of the for-statement. */
6180 cp_parser_already_scoped_statement (parser);
6182 /* We're done with the for-statement. */
6183 finish_for_stmt (statement);
6188 cp_parser_error (parser, "expected iteration-statement");
6189 statement = error_mark_node;
6196 /* Parse a for-init-statement.
6199 expression-statement
6200 simple-declaration */
6203 cp_parser_for_init_statement (parser)
6206 /* If the next token is a `;', then we have an empty
6207 expression-statement. Gramatically, this is also a
6208 simple-declaration, but an invalid one, because it does not
6209 declare anything. Therefore, if we did not handle this case
6210 specially, we would issue an error message about an invalid
6212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6214 /* We're going to speculatively look for a declaration, falling back
6215 to an expression, if necessary. */
6216 cp_parser_parse_tentatively (parser);
6217 /* Parse the declaration. */
6218 cp_parser_simple_declaration (parser,
6219 /*function_definition_allowed_p=*/false);
6220 /* If the tentative parse failed, then we shall need to look for an
6221 expression-statement. */
6222 if (cp_parser_parse_definitely (parser))
6226 cp_parser_expression_statement (parser);
6229 /* Parse a jump-statement.
6234 return expression [opt] ;
6242 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6246 cp_parser_jump_statement (parser)
6249 tree statement = error_mark_node;
6253 /* Peek at the next token. */
6254 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6256 return error_mark_node;
6258 /* See what kind of keyword it is. */
6259 keyword = token->keyword;
6263 statement = finish_break_stmt ();
6264 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6268 statement = finish_continue_stmt ();
6269 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6276 /* If the next token is a `;', then there is no
6278 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6279 expr = cp_parser_expression (parser);
6282 /* Build the return-statement. */
6283 statement = finish_return_stmt (expr);
6284 /* Look for the final `;'. */
6285 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6290 /* Create the goto-statement. */
6291 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6293 /* Issue a warning about this use of a GNU extension. */
6295 pedwarn ("ISO C++ forbids computed gotos");
6296 /* Consume the '*' token. */
6297 cp_lexer_consume_token (parser->lexer);
6298 /* Parse the dependent expression. */
6299 finish_goto_stmt (cp_parser_expression (parser));
6302 finish_goto_stmt (cp_parser_identifier (parser));
6303 /* Look for the final `;'. */
6304 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6308 cp_parser_error (parser, "expected jump-statement");
6315 /* Parse a declaration-statement.
6317 declaration-statement:
6318 block-declaration */
6321 cp_parser_declaration_statement (parser)
6324 /* Parse the block-declaration. */
6325 cp_parser_block_declaration (parser, /*statement_p=*/true);
6327 /* Finish off the statement. */
6331 /* Some dependent statements (like `if (cond) statement'), are
6332 implicitly in their own scope. In other words, if the statement is
6333 a single statement (as opposed to a compound-statement), it is
6334 none-the-less treated as if it were enclosed in braces. Any
6335 declarations appearing in the dependent statement are out of scope
6336 after control passes that point. This function parses a statement,
6337 but ensures that is in its own scope, even if it is not a
6340 Returns the new statement. */
6343 cp_parser_implicitly_scoped_statement (parser)
6348 /* If the token is not a `{', then we must take special action. */
6349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6351 /* Create a compound-statement. */
6352 statement = begin_compound_stmt (/*has_no_scope=*/0);
6353 /* Parse the dependent-statement. */
6354 cp_parser_statement (parser);
6355 /* Finish the dummy compound-statement. */
6356 finish_compound_stmt (/*has_no_scope=*/0, statement);
6358 /* Otherwise, we simply parse the statement directly. */
6360 statement = cp_parser_compound_statement (parser);
6362 /* Return the statement. */
6366 /* For some dependent statements (like `while (cond) statement'), we
6367 have already created a scope. Therefore, even if the dependent
6368 statement is a compound-statement, we do not want to create another
6372 cp_parser_already_scoped_statement (parser)
6375 /* If the token is not a `{', then we must take special action. */
6376 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6380 /* Create a compound-statement. */
6381 statement = begin_compound_stmt (/*has_no_scope=*/1);
6382 /* Parse the dependent-statement. */
6383 cp_parser_statement (parser);
6384 /* Finish the dummy compound-statement. */
6385 finish_compound_stmt (/*has_no_scope=*/1, statement);
6387 /* Otherwise, we simply parse the statement directly. */
6389 cp_parser_statement (parser);
6392 /* Declarations [gram.dcl.dcl] */
6394 /* Parse an optional declaration-sequence.
6398 declaration-seq declaration */
6401 cp_parser_declaration_seq_opt (parser)
6408 token = cp_lexer_peek_token (parser->lexer);
6410 if (token->type == CPP_CLOSE_BRACE
6411 || token->type == CPP_EOF)
6414 if (token->type == CPP_SEMICOLON)
6416 /* A declaration consisting of a single semicolon is
6417 invalid. Allow it unless we're being pedantic. */
6419 pedwarn ("extra `;'");
6420 cp_lexer_consume_token (parser->lexer);
6424 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6425 parser to enter or exit implict `extern "C"' blocks. */
6426 while (pending_lang_change > 0)
6428 push_lang_context (lang_name_c);
6429 --pending_lang_change;
6431 while (pending_lang_change < 0)
6433 pop_lang_context ();
6434 ++pending_lang_change;
6437 /* Parse the declaration itself. */
6438 cp_parser_declaration (parser);
6442 /* Parse a declaration.
6447 template-declaration
6448 explicit-instantiation
6449 explicit-specialization
6450 linkage-specification
6451 namespace-definition
6456 __extension__ declaration */
6459 cp_parser_declaration (parser)
6466 /* Check for the `__extension__' keyword. */
6467 if (cp_parser_extension_opt (parser, &saved_pedantic))
6469 /* Parse the qualified declaration. */
6470 cp_parser_declaration (parser);
6471 /* Restore the PEDANTIC flag. */
6472 pedantic = saved_pedantic;
6477 /* Try to figure out what kind of declaration is present. */
6478 token1 = *cp_lexer_peek_token (parser->lexer);
6479 if (token1.type != CPP_EOF)
6480 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6482 /* If the next token is `extern' and the following token is a string
6483 literal, then we have a linkage specification. */
6484 if (token1.keyword == RID_EXTERN
6485 && cp_parser_is_string_literal (&token2))
6486 cp_parser_linkage_specification (parser);
6487 /* If the next token is `template', then we have either a template
6488 declaration, an explicit instantiation, or an explicit
6490 else if (token1.keyword == RID_TEMPLATE)
6492 /* `template <>' indicates a template specialization. */
6493 if (token2.type == CPP_LESS
6494 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6495 cp_parser_explicit_specialization (parser);
6496 /* `template <' indicates a template declaration. */
6497 else if (token2.type == CPP_LESS)
6498 cp_parser_template_declaration (parser, /*member_p=*/false);
6499 /* Anything else must be an explicit instantiation. */
6501 cp_parser_explicit_instantiation (parser);
6503 /* If the next token is `export', then we have a template
6505 else if (token1.keyword == RID_EXPORT)
6506 cp_parser_template_declaration (parser, /*member_p=*/false);
6507 /* If the next token is `extern', 'static' or 'inline' and the one
6508 after that is `template', we have a GNU extended explicit
6509 instantiation directive. */
6510 else if (cp_parser_allow_gnu_extensions_p (parser)
6511 && (token1.keyword == RID_EXTERN
6512 || token1.keyword == RID_STATIC
6513 || token1.keyword == RID_INLINE)
6514 && token2.keyword == RID_TEMPLATE)
6515 cp_parser_explicit_instantiation (parser);
6516 /* If the next token is `namespace', check for a named or unnamed
6517 namespace definition. */
6518 else if (token1.keyword == RID_NAMESPACE
6519 && (/* A named namespace definition. */
6520 (token2.type == CPP_NAME
6521 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6523 /* An unnamed namespace definition. */
6524 || token2.type == CPP_OPEN_BRACE))
6525 cp_parser_namespace_definition (parser);
6526 /* We must have either a block declaration or a function
6529 /* Try to parse a block-declaration, or a function-definition. */
6530 cp_parser_block_declaration (parser, /*statement_p=*/false);
6533 /* Parse a block-declaration.
6538 namespace-alias-definition
6545 __extension__ block-declaration
6548 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6549 part of a declaration-statement. */
6552 cp_parser_block_declaration (cp_parser *parser,
6558 /* Check for the `__extension__' keyword. */
6559 if (cp_parser_extension_opt (parser, &saved_pedantic))
6561 /* Parse the qualified declaration. */
6562 cp_parser_block_declaration (parser, statement_p);
6563 /* Restore the PEDANTIC flag. */
6564 pedantic = saved_pedantic;
6569 /* Peek at the next token to figure out which kind of declaration is
6571 token1 = cp_lexer_peek_token (parser->lexer);
6573 /* If the next keyword is `asm', we have an asm-definition. */
6574 if (token1->keyword == RID_ASM)
6577 cp_parser_commit_to_tentative_parse (parser);
6578 cp_parser_asm_definition (parser);
6580 /* If the next keyword is `namespace', we have a
6581 namespace-alias-definition. */
6582 else if (token1->keyword == RID_NAMESPACE)
6583 cp_parser_namespace_alias_definition (parser);
6584 /* If the next keyword is `using', we have either a
6585 using-declaration or a using-directive. */
6586 else if (token1->keyword == RID_USING)
6591 cp_parser_commit_to_tentative_parse (parser);
6592 /* If the token after `using' is `namespace', then we have a
6594 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6595 if (token2->keyword == RID_NAMESPACE)
6596 cp_parser_using_directive (parser);
6597 /* Otherwise, it's a using-declaration. */
6599 cp_parser_using_declaration (parser);
6601 /* If the next keyword is `__label__' we have a label declaration. */
6602 else if (token1->keyword == RID_LABEL)
6605 cp_parser_commit_to_tentative_parse (parser);
6606 cp_parser_label_declaration (parser);
6608 /* Anything else must be a simple-declaration. */
6610 cp_parser_simple_declaration (parser, !statement_p);
6613 /* Parse a simple-declaration.
6616 decl-specifier-seq [opt] init-declarator-list [opt] ;
6618 init-declarator-list:
6620 init-declarator-list , init-declarator
6622 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6623 function-definition as a simple-declaration. */
6626 cp_parser_simple_declaration (parser, function_definition_allowed_p)
6628 bool function_definition_allowed_p;
6630 tree decl_specifiers;
6632 bool declares_class_or_enum;
6633 bool saw_declarator;
6635 /* Defer access checks until we know what is being declared; the
6636 checks for names appearing in the decl-specifier-seq should be
6637 done as if we were in the scope of the thing being declared. */
6638 push_deferring_access_checks (true);
6640 /* Parse the decl-specifier-seq. We have to keep track of whether
6641 or not the decl-specifier-seq declares a named class or
6642 enumeration type, since that is the only case in which the
6643 init-declarator-list is allowed to be empty.
6647 In a simple-declaration, the optional init-declarator-list can be
6648 omitted only when declaring a class or enumeration, that is when
6649 the decl-specifier-seq contains either a class-specifier, an
6650 elaborated-type-specifier, or an enum-specifier. */
6652 = cp_parser_decl_specifier_seq (parser,
6653 CP_PARSER_FLAGS_OPTIONAL,
6655 &declares_class_or_enum);
6656 /* We no longer need to defer access checks. */
6657 stop_deferring_access_checks ();
6659 /* Keep going until we hit the `;' at the end of the simple
6661 saw_declarator = false;
6662 while (cp_lexer_next_token_is_not (parser->lexer,
6666 bool function_definition_p;
6668 saw_declarator = true;
6669 /* Parse the init-declarator. */
6670 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6671 function_definition_allowed_p,
6673 &function_definition_p);
6674 /* Handle function definitions specially. */
6675 if (function_definition_p)
6677 /* If the next token is a `,', then we are probably
6678 processing something like:
6682 which is erroneous. */
6683 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6684 error ("mixing declarations and function-definitions is forbidden");
6685 /* Otherwise, we're done with the list of declarators. */
6688 pop_deferring_access_checks ();
6692 /* The next token should be either a `,' or a `;'. */
6693 token = cp_lexer_peek_token (parser->lexer);
6694 /* If it's a `,', there are more declarators to come. */
6695 if (token->type == CPP_COMMA)
6696 cp_lexer_consume_token (parser->lexer);
6697 /* If it's a `;', we are done. */
6698 else if (token->type == CPP_SEMICOLON)
6700 /* Anything else is an error. */
6703 cp_parser_error (parser, "expected `,' or `;'");
6704 /* Skip tokens until we reach the end of the statement. */
6705 cp_parser_skip_to_end_of_statement (parser);
6706 pop_deferring_access_checks ();
6709 /* After the first time around, a function-definition is not
6710 allowed -- even if it was OK at first. For example:
6715 function_definition_allowed_p = false;
6718 /* Issue an error message if no declarators are present, and the
6719 decl-specifier-seq does not itself declare a class or
6721 if (!saw_declarator)
6723 if (cp_parser_declares_only_class_p (parser))
6724 shadow_tag (decl_specifiers);
6725 /* Perform any deferred access checks. */
6726 perform_deferred_access_checks ();
6729 pop_deferring_access_checks ();
6731 /* Consume the `;'. */
6732 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6734 /* Mark all the classes that appeared in the decl-specifier-seq as
6735 having received a `;'. */
6736 note_list_got_semicolon (decl_specifiers);
6739 /* Parse a decl-specifier-seq.
6742 decl-specifier-seq [opt] decl-specifier
6745 storage-class-specifier
6754 decl-specifier-seq [opt] attributes
6756 Returns a TREE_LIST, giving the decl-specifiers in the order they
6757 appear in the source code. The TREE_VALUE of each node is the
6758 decl-specifier. For a keyword (such as `auto' or `friend'), the
6759 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6760 representation of a type-specifier, see cp_parser_type_specifier.
6762 If there are attributes, they will be stored in *ATTRIBUTES,
6763 represented as described above cp_parser_attributes.
6765 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6766 appears, and the entity that will be a friend is not going to be a
6767 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6768 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6769 friendship is granted might not be a class. */
6772 cp_parser_decl_specifier_seq (parser, flags, attributes,
6773 declares_class_or_enum)
6775 cp_parser_flags flags;
6777 bool *declares_class_or_enum;
6779 tree decl_specs = NULL_TREE;
6780 bool friend_p = false;
6781 bool constructor_possible_p = true;
6783 /* Assume no class or enumeration type is declared. */
6784 *declares_class_or_enum = false;
6786 /* Assume there are no attributes. */
6787 *attributes = NULL_TREE;
6789 /* Keep reading specifiers until there are no more to read. */
6792 tree decl_spec = NULL_TREE;
6796 /* Peek at the next token. */
6797 token = cp_lexer_peek_token (parser->lexer);
6798 /* Handle attributes. */
6799 if (token->keyword == RID_ATTRIBUTE)
6801 /* Parse the attributes. */
6802 decl_spec = cp_parser_attributes_opt (parser);
6803 /* Add them to the list. */
6804 *attributes = chainon (*attributes, decl_spec);
6807 /* If the next token is an appropriate keyword, we can simply
6808 add it to the list. */
6809 switch (token->keyword)
6815 /* The representation of the specifier is simply the
6816 appropriate TREE_IDENTIFIER node. */
6817 decl_spec = token->value;
6818 /* Consume the token. */
6819 cp_lexer_consume_token (parser->lexer);
6822 /* function-specifier:
6829 decl_spec = cp_parser_function_specifier_opt (parser);
6835 /* The representation of the specifier is simply the
6836 appropriate TREE_IDENTIFIER node. */
6837 decl_spec = token->value;
6838 /* Consume the token. */
6839 cp_lexer_consume_token (parser->lexer);
6840 /* A constructor declarator cannot appear in a typedef. */
6841 constructor_possible_p = false;
6844 /* storage-class-specifier:
6859 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6866 /* Constructors are a special case. The `S' in `S()' is not a
6867 decl-specifier; it is the beginning of the declarator. */
6868 constructor_p = (!decl_spec
6869 && constructor_possible_p
6870 && cp_parser_constructor_declarator_p (parser,
6873 /* If we don't have a DECL_SPEC yet, then we must be looking at
6874 a type-specifier. */
6875 if (!decl_spec && !constructor_p)
6877 bool decl_spec_declares_class_or_enum;
6878 bool is_cv_qualifier;
6881 = cp_parser_type_specifier (parser, flags,
6883 /*is_declaration=*/true,
6884 &decl_spec_declares_class_or_enum,
6887 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6889 /* If this type-specifier referenced a user-defined type
6890 (a typedef, class-name, etc.), then we can't allow any
6891 more such type-specifiers henceforth.
6895 The longest sequence of decl-specifiers that could
6896 possibly be a type name is taken as the
6897 decl-specifier-seq of a declaration. The sequence shall
6898 be self-consistent as described below.
6902 As a general rule, at most one type-specifier is allowed
6903 in the complete decl-specifier-seq of a declaration. The
6904 only exceptions are the following:
6906 -- const or volatile can be combined with any other
6909 -- signed or unsigned can be combined with char, long,
6917 void g (const int Pc);
6919 Here, Pc is *not* part of the decl-specifier seq; it's
6920 the declarator. Therefore, once we see a type-specifier
6921 (other than a cv-qualifier), we forbid any additional
6922 user-defined types. We *do* still allow things like `int
6923 int' to be considered a decl-specifier-seq, and issue the
6924 error message later. */
6925 if (decl_spec && !is_cv_qualifier)
6926 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6927 /* A constructor declarator cannot follow a type-specifier. */
6929 constructor_possible_p = false;
6932 /* If we still do not have a DECL_SPEC, then there are no more
6936 /* Issue an error message, unless the entire construct was
6938 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6940 cp_parser_error (parser, "expected decl specifier");
6941 return error_mark_node;
6947 /* Add the DECL_SPEC to the list of specifiers. */
6948 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6950 /* After we see one decl-specifier, further decl-specifiers are
6952 flags |= CP_PARSER_FLAGS_OPTIONAL;
6955 /* We have built up the DECL_SPECS in reverse order. Return them in
6956 the correct order. */
6957 return nreverse (decl_specs);
6960 /* Parse an (optional) storage-class-specifier.
6962 storage-class-specifier:
6971 storage-class-specifier:
6974 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6977 cp_parser_storage_class_specifier_opt (parser)
6980 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6988 /* Consume the token. */
6989 return cp_lexer_consume_token (parser->lexer)->value;
6996 /* Parse an (optional) function-specifier.
7003 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7006 cp_parser_function_specifier_opt (parser)
7009 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7014 /* Consume the token. */
7015 return cp_lexer_consume_token (parser->lexer)->value;
7022 /* Parse a linkage-specification.
7024 linkage-specification:
7025 extern string-literal { declaration-seq [opt] }
7026 extern string-literal declaration */
7029 cp_parser_linkage_specification (parser)
7035 /* Look for the `extern' keyword. */
7036 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7038 /* Peek at the next token. */
7039 token = cp_lexer_peek_token (parser->lexer);
7040 /* If it's not a string-literal, then there's a problem. */
7041 if (!cp_parser_is_string_literal (token))
7043 cp_parser_error (parser, "expected language-name");
7046 /* Consume the token. */
7047 cp_lexer_consume_token (parser->lexer);
7049 /* Transform the literal into an identifier. If the literal is a
7050 wide-character string, or contains embedded NULs, then we can't
7051 handle it as the user wants. */
7052 if (token->type == CPP_WSTRING
7053 || (strlen (TREE_STRING_POINTER (token->value))
7054 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7056 cp_parser_error (parser, "invalid linkage-specification");
7057 /* Assume C++ linkage. */
7058 linkage = get_identifier ("c++");
7060 /* If it's a simple string constant, things are easier. */
7062 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7064 /* We're now using the new linkage. */
7065 push_lang_context (linkage);
7067 /* If the next token is a `{', then we're using the first
7069 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7071 /* Consume the `{' token. */
7072 cp_lexer_consume_token (parser->lexer);
7073 /* Parse the declarations. */
7074 cp_parser_declaration_seq_opt (parser);
7075 /* Look for the closing `}'. */
7076 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7078 /* Otherwise, there's just one declaration. */
7081 bool saved_in_unbraced_linkage_specification_p;
7083 saved_in_unbraced_linkage_specification_p
7084 = parser->in_unbraced_linkage_specification_p;
7085 parser->in_unbraced_linkage_specification_p = true;
7086 have_extern_spec = true;
7087 cp_parser_declaration (parser);
7088 have_extern_spec = false;
7089 parser->in_unbraced_linkage_specification_p
7090 = saved_in_unbraced_linkage_specification_p;
7093 /* We're done with the linkage-specification. */
7094 pop_lang_context ();
7097 /* Special member functions [gram.special] */
7099 /* Parse a conversion-function-id.
7101 conversion-function-id:
7102 operator conversion-type-id
7104 Returns an IDENTIFIER_NODE representing the operator. */
7107 cp_parser_conversion_function_id (parser)
7112 tree saved_qualifying_scope;
7113 tree saved_object_scope;
7115 /* Look for the `operator' token. */
7116 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7117 return error_mark_node;
7118 /* When we parse the conversion-type-id, the current scope will be
7119 reset. However, we need that information in able to look up the
7120 conversion function later, so we save it here. */
7121 saved_scope = parser->scope;
7122 saved_qualifying_scope = parser->qualifying_scope;
7123 saved_object_scope = parser->object_scope;
7124 /* We must enter the scope of the class so that the names of
7125 entities declared within the class are available in the
7126 conversion-type-id. For example, consider:
7133 S::operator I() { ... }
7135 In order to see that `I' is a type-name in the definition, we
7136 must be in the scope of `S'. */
7138 push_scope (saved_scope);
7139 /* Parse the conversion-type-id. */
7140 type = cp_parser_conversion_type_id (parser);
7141 /* Leave the scope of the class, if any. */
7143 pop_scope (saved_scope);
7144 /* Restore the saved scope. */
7145 parser->scope = saved_scope;
7146 parser->qualifying_scope = saved_qualifying_scope;
7147 parser->object_scope = saved_object_scope;
7148 /* If the TYPE is invalid, indicate failure. */
7149 if (type == error_mark_node)
7150 return error_mark_node;
7151 return mangle_conv_op_name_for_type (type);
7154 /* Parse a conversion-type-id:
7157 type-specifier-seq conversion-declarator [opt]
7159 Returns the TYPE specified. */
7162 cp_parser_conversion_type_id (parser)
7166 tree type_specifiers;
7169 /* Parse the attributes. */
7170 attributes = cp_parser_attributes_opt (parser);
7171 /* Parse the type-specifiers. */
7172 type_specifiers = cp_parser_type_specifier_seq (parser);
7173 /* If that didn't work, stop. */
7174 if (type_specifiers == error_mark_node)
7175 return error_mark_node;
7176 /* Parse the conversion-declarator. */
7177 declarator = cp_parser_conversion_declarator_opt (parser);
7179 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7180 /*initialized=*/0, &attributes);
7183 /* Parse an (optional) conversion-declarator.
7185 conversion-declarator:
7186 ptr-operator conversion-declarator [opt]
7188 Returns a representation of the declarator. See
7189 cp_parser_declarator for details. */
7192 cp_parser_conversion_declarator_opt (parser)
7195 enum tree_code code;
7197 tree cv_qualifier_seq;
7199 /* We don't know if there's a ptr-operator next, or not. */
7200 cp_parser_parse_tentatively (parser);
7201 /* Try the ptr-operator. */
7202 code = cp_parser_ptr_operator (parser, &class_type,
7204 /* If it worked, look for more conversion-declarators. */
7205 if (cp_parser_parse_definitely (parser))
7209 /* Parse another optional declarator. */
7210 declarator = cp_parser_conversion_declarator_opt (parser);
7212 /* Create the representation of the declarator. */
7213 if (code == INDIRECT_REF)
7214 declarator = make_pointer_declarator (cv_qualifier_seq,
7217 declarator = make_reference_declarator (cv_qualifier_seq,
7220 /* Handle the pointer-to-member case. */
7222 declarator = build_nt (SCOPE_REF, class_type, declarator);
7230 /* Parse an (optional) ctor-initializer.
7233 : mem-initializer-list
7235 Returns TRUE iff the ctor-initializer was actually present. */
7238 cp_parser_ctor_initializer_opt (parser)
7241 /* If the next token is not a `:', then there is no
7242 ctor-initializer. */
7243 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7245 /* Do default initialization of any bases and members. */
7246 if (DECL_CONSTRUCTOR_P (current_function_decl))
7247 finish_mem_initializers (NULL_TREE);
7252 /* Consume the `:' token. */
7253 cp_lexer_consume_token (parser->lexer);
7254 /* And the mem-initializer-list. */
7255 cp_parser_mem_initializer_list (parser);
7260 /* Parse a mem-initializer-list.
7262 mem-initializer-list:
7264 mem-initializer , mem-initializer-list */
7267 cp_parser_mem_initializer_list (parser)
7270 tree mem_initializer_list = NULL_TREE;
7272 /* Let the semantic analysis code know that we are starting the
7273 mem-initializer-list. */
7274 begin_mem_initializers ();
7276 /* Loop through the list. */
7279 tree mem_initializer;
7281 /* Parse the mem-initializer. */
7282 mem_initializer = cp_parser_mem_initializer (parser);
7283 /* Add it to the list, unless it was erroneous. */
7284 if (mem_initializer)
7286 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7287 mem_initializer_list = mem_initializer;
7289 /* If the next token is not a `,', we're done. */
7290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7292 /* Consume the `,' token. */
7293 cp_lexer_consume_token (parser->lexer);
7296 /* Perform semantic analysis. */
7297 finish_mem_initializers (mem_initializer_list);
7300 /* Parse a mem-initializer.
7303 mem-initializer-id ( expression-list [opt] )
7308 ( expresion-list [opt] )
7310 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7311 class) or FIELD_DECL (for a non-static data member) to initialize;
7312 the TREE_VALUE is the expression-list. */
7315 cp_parser_mem_initializer (parser)
7318 tree mem_initializer_id;
7319 tree expression_list;
7321 /* Find out what is being initialized. */
7322 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7324 pedwarn ("anachronistic old-style base class initializer");
7325 mem_initializer_id = NULL_TREE;
7328 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7329 /* Look for the opening `('. */
7330 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7331 /* Parse the expression-list. */
7332 if (cp_lexer_next_token_is_not (parser->lexer,
7334 expression_list = cp_parser_expression_list (parser);
7336 expression_list = void_type_node;
7337 /* Look for the closing `)'. */
7338 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7340 return expand_member_init (mem_initializer_id,
7344 /* Parse a mem-initializer-id.
7347 :: [opt] nested-name-specifier [opt] class-name
7350 Returns a TYPE indicating the class to be initializer for the first
7351 production. Returns an IDENTIFIER_NODE indicating the data member
7352 to be initialized for the second production. */
7355 cp_parser_mem_initializer_id (parser)
7358 bool global_scope_p;
7359 bool nested_name_specifier_p;
7362 /* Look for the optional `::' operator. */
7364 = (cp_parser_global_scope_opt (parser,
7365 /*current_scope_valid_p=*/false)
7367 /* Look for the optional nested-name-specifier. The simplest way to
7372 The keyword `typename' is not permitted in a base-specifier or
7373 mem-initializer; in these contexts a qualified name that
7374 depends on a template-parameter is implicitly assumed to be a
7377 is to assume that we have seen the `typename' keyword at this
7379 nested_name_specifier_p
7380 = (cp_parser_nested_name_specifier_opt (parser,
7381 /*typename_keyword_p=*/true,
7382 /*check_dependency_p=*/true,
7385 /* If there is a `::' operator or a nested-name-specifier, then we
7386 are definitely looking for a class-name. */
7387 if (global_scope_p || nested_name_specifier_p)
7388 return cp_parser_class_name (parser,
7389 /*typename_keyword_p=*/true,
7390 /*template_keyword_p=*/false,
7392 /*check_access_p=*/true,
7393 /*check_dependency_p=*/true,
7394 /*class_head_p=*/false);
7395 /* Otherwise, we could also be looking for an ordinary identifier. */
7396 cp_parser_parse_tentatively (parser);
7397 /* Try a class-name. */
7398 id = cp_parser_class_name (parser,
7399 /*typename_keyword_p=*/true,
7400 /*template_keyword_p=*/false,
7402 /*check_access_p=*/true,
7403 /*check_dependency_p=*/true,
7404 /*class_head_p=*/false);
7405 /* If we found one, we're done. */
7406 if (cp_parser_parse_definitely (parser))
7408 /* Otherwise, look for an ordinary identifier. */
7409 return cp_parser_identifier (parser);
7412 /* Overloading [gram.over] */
7414 /* Parse an operator-function-id.
7416 operator-function-id:
7419 Returns an IDENTIFIER_NODE for the operator which is a
7420 human-readable spelling of the identifier, e.g., `operator +'. */
7423 cp_parser_operator_function_id (parser)
7426 /* Look for the `operator' keyword. */
7427 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7428 return error_mark_node;
7429 /* And then the name of the operator itself. */
7430 return cp_parser_operator (parser);
7433 /* Parse an operator.
7436 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7437 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7438 || ++ -- , ->* -> () []
7445 Returns an IDENTIFIER_NODE for the operator which is a
7446 human-readable spelling of the identifier, e.g., `operator +'. */
7449 cp_parser_operator (parser)
7452 tree id = NULL_TREE;
7455 /* Peek at the next token. */
7456 token = cp_lexer_peek_token (parser->lexer);
7457 /* Figure out which operator we have. */
7458 switch (token->type)
7464 /* The keyword should be either `new' or `delete'. */
7465 if (token->keyword == RID_NEW)
7467 else if (token->keyword == RID_DELETE)
7472 /* Consume the `new' or `delete' token. */
7473 cp_lexer_consume_token (parser->lexer);
7475 /* Peek at the next token. */
7476 token = cp_lexer_peek_token (parser->lexer);
7477 /* If it's a `[' token then this is the array variant of the
7479 if (token->type == CPP_OPEN_SQUARE)
7481 /* Consume the `[' token. */
7482 cp_lexer_consume_token (parser->lexer);
7483 /* Look for the `]' token. */
7484 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7485 id = ansi_opname (op == NEW_EXPR
7486 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7488 /* Otherwise, we have the non-array variant. */
7490 id = ansi_opname (op);
7496 id = ansi_opname (PLUS_EXPR);
7500 id = ansi_opname (MINUS_EXPR);
7504 id = ansi_opname (MULT_EXPR);
7508 id = ansi_opname (TRUNC_DIV_EXPR);
7512 id = ansi_opname (TRUNC_MOD_EXPR);
7516 id = ansi_opname (BIT_XOR_EXPR);
7520 id = ansi_opname (BIT_AND_EXPR);
7524 id = ansi_opname (BIT_IOR_EXPR);
7528 id = ansi_opname (BIT_NOT_EXPR);
7532 id = ansi_opname (TRUTH_NOT_EXPR);
7536 id = ansi_assopname (NOP_EXPR);
7540 id = ansi_opname (LT_EXPR);
7544 id = ansi_opname (GT_EXPR);
7548 id = ansi_assopname (PLUS_EXPR);
7552 id = ansi_assopname (MINUS_EXPR);
7556 id = ansi_assopname (MULT_EXPR);
7560 id = ansi_assopname (TRUNC_DIV_EXPR);
7564 id = ansi_assopname (TRUNC_MOD_EXPR);
7568 id = ansi_assopname (BIT_XOR_EXPR);
7572 id = ansi_assopname (BIT_AND_EXPR);
7576 id = ansi_assopname (BIT_IOR_EXPR);
7580 id = ansi_opname (LSHIFT_EXPR);
7584 id = ansi_opname (RSHIFT_EXPR);
7588 id = ansi_assopname (LSHIFT_EXPR);
7592 id = ansi_assopname (RSHIFT_EXPR);
7596 id = ansi_opname (EQ_EXPR);
7600 id = ansi_opname (NE_EXPR);
7604 id = ansi_opname (LE_EXPR);
7607 case CPP_GREATER_EQ:
7608 id = ansi_opname (GE_EXPR);
7612 id = ansi_opname (TRUTH_ANDIF_EXPR);
7616 id = ansi_opname (TRUTH_ORIF_EXPR);
7620 id = ansi_opname (POSTINCREMENT_EXPR);
7623 case CPP_MINUS_MINUS:
7624 id = ansi_opname (PREDECREMENT_EXPR);
7628 id = ansi_opname (COMPOUND_EXPR);
7631 case CPP_DEREF_STAR:
7632 id = ansi_opname (MEMBER_REF);
7636 id = ansi_opname (COMPONENT_REF);
7639 case CPP_OPEN_PAREN:
7640 /* Consume the `('. */
7641 cp_lexer_consume_token (parser->lexer);
7642 /* Look for the matching `)'. */
7643 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7644 return ansi_opname (CALL_EXPR);
7646 case CPP_OPEN_SQUARE:
7647 /* Consume the `['. */
7648 cp_lexer_consume_token (parser->lexer);
7649 /* Look for the matching `]'. */
7650 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7651 return ansi_opname (ARRAY_REF);
7655 id = ansi_opname (MIN_EXPR);
7659 id = ansi_opname (MAX_EXPR);
7663 id = ansi_assopname (MIN_EXPR);
7667 id = ansi_assopname (MAX_EXPR);
7671 /* Anything else is an error. */
7675 /* If we have selected an identifier, we need to consume the
7678 cp_lexer_consume_token (parser->lexer);
7679 /* Otherwise, no valid operator name was present. */
7682 cp_parser_error (parser, "expected operator");
7683 id = error_mark_node;
7689 /* Parse a template-declaration.
7691 template-declaration:
7692 export [opt] template < template-parameter-list > declaration
7694 If MEMBER_P is TRUE, this template-declaration occurs within a
7697 The grammar rule given by the standard isn't correct. What
7700 template-declaration:
7701 export [opt] template-parameter-list-seq
7702 decl-specifier-seq [opt] init-declarator [opt] ;
7703 export [opt] template-parameter-list-seq
7706 template-parameter-list-seq:
7707 template-parameter-list-seq [opt]
7708 template < template-parameter-list > */
7711 cp_parser_template_declaration (parser, member_p)
7715 /* Check for `export'. */
7716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7718 /* Consume the `export' token. */
7719 cp_lexer_consume_token (parser->lexer);
7720 /* Warn that we do not support `export'. */
7721 warning ("keyword `export' not implemented, and will be ignored");
7724 cp_parser_template_declaration_after_export (parser, member_p);
7727 /* Parse a template-parameter-list.
7729 template-parameter-list:
7731 template-parameter-list , template-parameter
7733 Returns a TREE_LIST. Each node represents a template parameter.
7734 The nodes are connected via their TREE_CHAINs. */
7737 cp_parser_template_parameter_list (parser)
7740 tree parameter_list = NULL_TREE;
7747 /* Parse the template-parameter. */
7748 parameter = cp_parser_template_parameter (parser);
7749 /* Add it to the list. */
7750 parameter_list = process_template_parm (parameter_list,
7753 /* Peek at the next token. */
7754 token = cp_lexer_peek_token (parser->lexer);
7755 /* If it's not a `,', we're done. */
7756 if (token->type != CPP_COMMA)
7758 /* Otherwise, consume the `,' token. */
7759 cp_lexer_consume_token (parser->lexer);
7762 return parameter_list;
7765 /* Parse a template-parameter.
7769 parameter-declaration
7771 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7772 TREE_PURPOSE is the default value, if any. */
7775 cp_parser_template_parameter (parser)
7780 /* Peek at the next token. */
7781 token = cp_lexer_peek_token (parser->lexer);
7782 /* If it is `class' or `template', we have a type-parameter. */
7783 if (token->keyword == RID_TEMPLATE)
7784 return cp_parser_type_parameter (parser);
7785 /* If it is `class' or `typename' we do not know yet whether it is a
7786 type parameter or a non-type parameter. Consider:
7788 template <typename T, typename T::X X> ...
7792 template <class C, class D*> ...
7794 Here, the first parameter is a type parameter, and the second is
7795 a non-type parameter. We can tell by looking at the token after
7796 the identifier -- if it is a `,', `=', or `>' then we have a type
7798 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7800 /* Peek at the token after `class' or `typename'. */
7801 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7802 /* If it's an identifier, skip it. */
7803 if (token->type == CPP_NAME)
7804 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7805 /* Now, see if the token looks like the end of a template
7807 if (token->type == CPP_COMMA
7808 || token->type == CPP_EQ
7809 || token->type == CPP_GREATER)
7810 return cp_parser_type_parameter (parser);
7813 /* Otherwise, it is a non-type parameter.
7817 When parsing a default template-argument for a non-type
7818 template-parameter, the first non-nested `>' is taken as the end
7819 of the template parameter-list rather than a greater-than
7822 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7825 /* Parse a type-parameter.
7828 class identifier [opt]
7829 class identifier [opt] = type-id
7830 typename identifier [opt]
7831 typename identifier [opt] = type-id
7832 template < template-parameter-list > class identifier [opt]
7833 template < template-parameter-list > class identifier [opt]
7836 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7837 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7838 the declaration of the parameter. */
7841 cp_parser_type_parameter (parser)
7847 /* Look for a keyword to tell us what kind of parameter this is. */
7848 token = cp_parser_require (parser, CPP_KEYWORD,
7849 "expected `class', `typename', or `template'");
7851 return error_mark_node;
7853 switch (token->keyword)
7859 tree default_argument;
7861 /* If the next token is an identifier, then it names the
7863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7864 identifier = cp_parser_identifier (parser);
7866 identifier = NULL_TREE;
7868 /* Create the parameter. */
7869 parameter = finish_template_type_parm (class_type_node, identifier);
7871 /* If the next token is an `=', we have a default argument. */
7872 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7874 /* Consume the `=' token. */
7875 cp_lexer_consume_token (parser->lexer);
7876 /* Parse the default-argumen. */
7877 default_argument = cp_parser_type_id (parser);
7880 default_argument = NULL_TREE;
7882 /* Create the combined representation of the parameter and the
7883 default argument. */
7884 parameter = build_tree_list (default_argument,
7891 tree parameter_list;
7893 tree default_argument;
7895 /* Look for the `<'. */
7896 cp_parser_require (parser, CPP_LESS, "`<'");
7897 /* Parse the template-parameter-list. */
7898 begin_template_parm_list ();
7900 = cp_parser_template_parameter_list (parser);
7901 parameter_list = end_template_parm_list (parameter_list);
7902 /* Look for the `>'. */
7903 cp_parser_require (parser, CPP_GREATER, "`>'");
7904 /* Look for the `class' keyword. */
7905 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7906 /* If the next token is an `=', then there is a
7907 default-argument. If the next token is a `>', we are at
7908 the end of the parameter-list. If the next token is a `,',
7909 then we are at the end of this parameter. */
7910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7911 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7912 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7913 identifier = cp_parser_identifier (parser);
7915 identifier = NULL_TREE;
7916 /* Create the template parameter. */
7917 parameter = finish_template_template_parm (class_type_node,
7920 /* If the next token is an `=', then there is a
7921 default-argument. */
7922 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7924 /* Consume the `='. */
7925 cp_lexer_consume_token (parser->lexer);
7926 /* Parse the id-expression. */
7928 = cp_parser_id_expression (parser,
7929 /*template_keyword_p=*/false,
7930 /*check_dependency_p=*/true,
7931 /*template_p=*/NULL);
7932 /* Look up the name. */
7934 = cp_parser_lookup_name_simple (parser, default_argument);
7935 /* See if the default argument is valid. */
7937 = check_template_template_default_arg (default_argument);
7940 default_argument = NULL_TREE;
7942 /* Create the combined representation of the parameter and the
7943 default argument. */
7944 parameter = build_tree_list (default_argument,
7950 /* Anything else is an error. */
7951 cp_parser_error (parser,
7952 "expected `class', `typename', or `template'");
7953 parameter = error_mark_node;
7959 /* Parse a template-id.
7962 template-name < template-argument-list [opt] >
7964 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7965 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7966 returned. Otherwise, if the template-name names a function, or set
7967 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7968 names a class, returns a TYPE_DECL for the specialization.
7970 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7971 uninstantiated templates. */
7974 cp_parser_template_id (cp_parser *parser,
7975 bool template_keyword_p,
7976 bool check_dependency_p)
7981 tree saved_qualifying_scope;
7982 tree saved_object_scope;
7984 bool saved_greater_than_is_operator_p;
7985 ptrdiff_t start_of_id;
7986 tree access_check = NULL_TREE;
7987 cp_token *next_token;
7989 /* If the next token corresponds to a template-id, there is no need
7991 next_token = cp_lexer_peek_token (parser->lexer);
7992 if (next_token->type == CPP_TEMPLATE_ID)
7997 /* Get the stored value. */
7998 value = cp_lexer_consume_token (parser->lexer)->value;
7999 /* Perform any access checks that were deferred. */
8000 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8001 perform_or_defer_access_check (TREE_PURPOSE (check),
8002 TREE_VALUE (check));
8003 /* Return the stored value. */
8004 return TREE_VALUE (value);
8007 /* Avoid performing name lookup if there is no possibility of
8008 finding a template-id. */
8009 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8010 || (next_token->type == CPP_NAME
8011 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
8013 cp_parser_error (parser, "expected template-id");
8014 return error_mark_node;
8017 /* Remember where the template-id starts. */
8018 if (cp_parser_parsing_tentatively (parser)
8019 && !cp_parser_committed_to_tentative_parse (parser))
8021 next_token = cp_lexer_peek_token (parser->lexer);
8022 start_of_id = cp_lexer_token_difference (parser->lexer,
8023 parser->lexer->first_token,
8029 push_deferring_access_checks (true);
8031 /* Parse the template-name. */
8032 template = cp_parser_template_name (parser, template_keyword_p,
8033 check_dependency_p);
8034 if (template == error_mark_node)
8036 pop_deferring_access_checks ();
8037 return error_mark_node;
8040 /* Look for the `<' that starts the template-argument-list. */
8041 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8043 pop_deferring_access_checks ();
8044 return error_mark_node;
8049 When parsing a template-id, the first non-nested `>' is taken as
8050 the end of the template-argument-list rather than a greater-than
8052 saved_greater_than_is_operator_p
8053 = parser->greater_than_is_operator_p;
8054 parser->greater_than_is_operator_p = false;
8055 /* Parsing the argument list may modify SCOPE, so we save it
8057 saved_scope = parser->scope;
8058 saved_qualifying_scope = parser->qualifying_scope;
8059 saved_object_scope = parser->object_scope;
8060 /* Parse the template-argument-list itself. */
8061 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8062 arguments = NULL_TREE;
8064 arguments = cp_parser_template_argument_list (parser);
8065 /* Look for the `>' that ends the template-argument-list. */
8066 cp_parser_require (parser, CPP_GREATER, "`>'");
8067 /* The `>' token might be a greater-than operator again now. */
8068 parser->greater_than_is_operator_p
8069 = saved_greater_than_is_operator_p;
8070 /* Restore the SAVED_SCOPE. */
8071 parser->scope = saved_scope;
8072 parser->qualifying_scope = saved_qualifying_scope;
8073 parser->object_scope = saved_object_scope;
8075 /* Build a representation of the specialization. */
8076 if (TREE_CODE (template) == IDENTIFIER_NODE)
8077 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8078 else if (DECL_CLASS_TEMPLATE_P (template)
8079 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8081 = finish_template_type (template, arguments,
8082 cp_lexer_next_token_is (parser->lexer,
8086 /* If it's not a class-template or a template-template, it should be
8087 a function-template. */
8088 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8089 || TREE_CODE (template) == OVERLOAD
8090 || BASELINK_P (template)),
8093 template_id = lookup_template_function (template, arguments);
8096 /* Retrieve any deferred checks. Do not pop this access checks yet
8097 so the memory will not be reclaimed during token replacing below. */
8098 access_check = get_deferred_access_checks ();
8100 /* If parsing tentatively, replace the sequence of tokens that makes
8101 up the template-id with a CPP_TEMPLATE_ID token. That way,
8102 should we re-parse the token stream, we will not have to repeat
8103 the effort required to do the parse, nor will we issue duplicate
8104 error messages about problems during instantiation of the
8106 if (start_of_id >= 0)
8110 /* Find the token that corresponds to the start of the
8112 token = cp_lexer_advance_token (parser->lexer,
8113 parser->lexer->first_token,
8116 /* Reset the contents of the START_OF_ID token. */
8117 token->type = CPP_TEMPLATE_ID;
8118 token->value = build_tree_list (access_check, template_id);
8119 token->keyword = RID_MAX;
8120 /* Purge all subsequent tokens. */
8121 cp_lexer_purge_tokens_after (parser->lexer, token);
8124 pop_deferring_access_checks ();
8128 /* Parse a template-name.
8133 The standard should actually say:
8137 operator-function-id
8138 conversion-function-id
8140 A defect report has been filed about this issue.
8142 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8143 `template' keyword, in a construction like:
8147 In that case `f' is taken to be a template-name, even though there
8148 is no way of knowing for sure.
8150 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8151 name refers to a set of overloaded functions, at least one of which
8152 is a template, or an IDENTIFIER_NODE with the name of the template,
8153 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8154 names are looked up inside uninstantiated templates. */
8157 cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8159 bool template_keyword_p;
8160 bool check_dependency_p;
8166 /* If the next token is `operator', then we have either an
8167 operator-function-id or a conversion-function-id. */
8168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8170 /* We don't know whether we're looking at an
8171 operator-function-id or a conversion-function-id. */
8172 cp_parser_parse_tentatively (parser);
8173 /* Try an operator-function-id. */
8174 identifier = cp_parser_operator_function_id (parser);
8175 /* If that didn't work, try a conversion-function-id. */
8176 if (!cp_parser_parse_definitely (parser))
8177 identifier = cp_parser_conversion_function_id (parser);
8179 /* Look for the identifier. */
8181 identifier = cp_parser_identifier (parser);
8183 /* If we didn't find an identifier, we don't have a template-id. */
8184 if (identifier == error_mark_node)
8185 return error_mark_node;
8187 /* If the name immediately followed the `template' keyword, then it
8188 is a template-name. However, if the next token is not `<', then
8189 we do not treat it as a template-name, since it is not being used
8190 as part of a template-id. This enables us to handle constructs
8193 template <typename T> struct S { S(); };
8194 template <typename T> S<T>::S();
8196 correctly. We would treat `S' as a template -- if it were `S<T>'
8197 -- but we do not if there is no `<'. */
8198 if (template_keyword_p && processing_template_decl
8199 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8202 /* Look up the name. */
8203 decl = cp_parser_lookup_name (parser, identifier,
8204 /*check_access=*/true,
8206 /*is_namespace=*/false,
8207 check_dependency_p);
8208 decl = maybe_get_template_decl_from_type_decl (decl);
8210 /* If DECL is a template, then the name was a template-name. */
8211 if (TREE_CODE (decl) == TEMPLATE_DECL)
8215 /* The standard does not explicitly indicate whether a name that
8216 names a set of overloaded declarations, some of which are
8217 templates, is a template-name. However, such a name should
8218 be a template-name; otherwise, there is no way to form a
8219 template-id for the overloaded templates. */
8220 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8221 if (TREE_CODE (fns) == OVERLOAD)
8225 for (fn = fns; fn; fn = OVL_NEXT (fn))
8226 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8231 /* Otherwise, the name does not name a template. */
8232 cp_parser_error (parser, "expected template-name");
8233 return error_mark_node;
8237 /* If DECL is dependent, and refers to a function, then just return
8238 its name; we will look it up again during template instantiation. */
8239 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8241 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8242 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8249 /* Parse a template-argument-list.
8251 template-argument-list:
8253 template-argument-list , template-argument
8255 Returns a TREE_LIST representing the arguments, in the order they
8256 appeared. The TREE_VALUE of each node is a representation of the
8260 cp_parser_template_argument_list (parser)
8263 tree arguments = NULL_TREE;
8269 /* Parse the template-argument. */
8270 argument = cp_parser_template_argument (parser);
8271 /* Add it to the list. */
8272 arguments = tree_cons (NULL_TREE, argument, arguments);
8273 /* If it is not a `,', then there are no more arguments. */
8274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8276 /* Otherwise, consume the ','. */
8277 cp_lexer_consume_token (parser->lexer);
8280 /* We built up the arguments in reverse order. */
8281 return nreverse (arguments);
8284 /* Parse a template-argument.
8287 assignment-expression
8291 The representation is that of an assignment-expression, type-id, or
8292 id-expression -- except that the qualified id-expression is
8293 evaluated, so that the value returned is either a DECL or an
8297 cp_parser_template_argument (parser)
8303 /* There's really no way to know what we're looking at, so we just
8304 try each alternative in order.
8308 In a template-argument, an ambiguity between a type-id and an
8309 expression is resolved to a type-id, regardless of the form of
8310 the corresponding template-parameter.
8312 Therefore, we try a type-id first. */
8313 cp_parser_parse_tentatively (parser);
8314 argument = cp_parser_type_id (parser);
8315 /* If the next token isn't a `,' or a `>', then this argument wasn't
8317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8318 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8319 cp_parser_error (parser, "expected template-argument");
8320 /* If that worked, we're done. */
8321 if (cp_parser_parse_definitely (parser))
8323 /* We're still not sure what the argument will be. */
8324 cp_parser_parse_tentatively (parser);
8325 /* Try a template. */
8326 argument = cp_parser_id_expression (parser,
8327 /*template_keyword_p=*/false,
8328 /*check_dependency_p=*/true,
8330 /* If the next token isn't a `,' or a `>', then this argument wasn't
8332 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8333 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8334 cp_parser_error (parser, "expected template-argument");
8335 if (!cp_parser_error_occurred (parser))
8337 /* Figure out what is being referred to. */
8338 argument = cp_parser_lookup_name_simple (parser, argument);
8340 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8341 TREE_OPERAND (argument, 1),
8342 tf_error | tf_parsing);
8343 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8344 cp_parser_error (parser, "expected template-name");
8346 if (cp_parser_parse_definitely (parser))
8348 /* It must be an assignment-expression. */
8349 return cp_parser_assignment_expression (parser);
8352 /* Parse an explicit-instantiation.
8354 explicit-instantiation:
8355 template declaration
8357 Although the standard says `declaration', what it really means is:
8359 explicit-instantiation:
8360 template decl-specifier-seq [opt] declarator [opt] ;
8362 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8363 supposed to be allowed. A defect report has been filed about this
8368 explicit-instantiation:
8369 storage-class-specifier template
8370 decl-specifier-seq [opt] declarator [opt] ;
8371 function-specifier template
8372 decl-specifier-seq [opt] declarator [opt] ; */
8375 cp_parser_explicit_instantiation (parser)
8378 bool declares_class_or_enum;
8379 tree decl_specifiers;
8381 tree extension_specifier = NULL_TREE;
8383 /* Look for an (optional) storage-class-specifier or
8384 function-specifier. */
8385 if (cp_parser_allow_gnu_extensions_p (parser))
8388 = cp_parser_storage_class_specifier_opt (parser);
8389 if (!extension_specifier)
8390 extension_specifier = cp_parser_function_specifier_opt (parser);
8393 /* Look for the `template' keyword. */
8394 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8395 /* Let the front end know that we are processing an explicit
8397 begin_explicit_instantiation ();
8398 /* [temp.explicit] says that we are supposed to ignore access
8399 control while processing explicit instantiation directives. */
8400 scope_chain->check_access = 0;
8401 /* Parse a decl-specifier-seq. */
8403 = cp_parser_decl_specifier_seq (parser,
8404 CP_PARSER_FLAGS_OPTIONAL,
8406 &declares_class_or_enum);
8407 /* If there was exactly one decl-specifier, and it declared a class,
8408 and there's no declarator, then we have an explicit type
8410 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8414 type = check_tag_decl (decl_specifiers);
8416 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8423 /* Parse the declarator. */
8425 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8426 /*ctor_dtor_or_conv_p=*/NULL);
8427 decl = grokdeclarator (declarator, decl_specifiers,
8429 /* Do the explicit instantiation. */
8430 do_decl_instantiation (decl, extension_specifier);
8432 /* We're done with the instantiation. */
8433 end_explicit_instantiation ();
8434 /* Trun access control back on. */
8435 scope_chain->check_access = flag_access_control;
8437 /* Look for the trailing `;'. */
8438 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8441 /* Parse an explicit-specialization.
8443 explicit-specialization:
8444 template < > declaration
8446 Although the standard says `declaration', what it really means is:
8448 explicit-specialization:
8449 template <> decl-specifier [opt] init-declarator [opt] ;
8450 template <> function-definition
8451 template <> explicit-specialization
8452 template <> template-declaration */
8455 cp_parser_explicit_specialization (parser)
8458 /* Look for the `template' keyword. */
8459 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8460 /* Look for the `<'. */
8461 cp_parser_require (parser, CPP_LESS, "`<'");
8462 /* Look for the `>'. */
8463 cp_parser_require (parser, CPP_GREATER, "`>'");
8464 /* We have processed another parameter list. */
8465 ++parser->num_template_parameter_lists;
8466 /* Let the front end know that we are beginning a specialization. */
8467 begin_specialization ();
8469 /* If the next keyword is `template', we need to figure out whether
8470 or not we're looking a template-declaration. */
8471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8473 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8474 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8475 cp_parser_template_declaration_after_export (parser,
8476 /*member_p=*/false);
8478 cp_parser_explicit_specialization (parser);
8481 /* Parse the dependent declaration. */
8482 cp_parser_single_declaration (parser,
8486 /* We're done with the specialization. */
8487 end_specialization ();
8488 /* We're done with this parameter list. */
8489 --parser->num_template_parameter_lists;
8492 /* Parse a type-specifier.
8495 simple-type-specifier
8498 elaborated-type-specifier
8506 Returns a representation of the type-specifier. If the
8507 type-specifier is a keyword (like `int' or `const', or
8508 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8509 For a class-specifier, enum-specifier, or elaborated-type-specifier
8510 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8512 If IS_FRIEND is TRUE then this type-specifier is being declared a
8513 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8514 appearing in a decl-specifier-seq.
8516 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8517 class-specifier, enum-specifier, or elaborated-type-specifier, then
8518 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8521 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8522 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8526 cp_parser_type_specifier (parser,
8530 declares_class_or_enum,
8533 cp_parser_flags flags;
8535 bool is_declaration;
8536 bool *declares_class_or_enum;
8537 bool *is_cv_qualifier;
8539 tree type_spec = NULL_TREE;
8543 /* Assume this type-specifier does not declare a new type. */
8544 if (declares_class_or_enum)
8545 *declares_class_or_enum = false;
8546 /* And that it does not specify a cv-qualifier. */
8547 if (is_cv_qualifier)
8548 *is_cv_qualifier = false;
8549 /* Peek at the next token. */
8550 token = cp_lexer_peek_token (parser->lexer);
8552 /* If we're looking at a keyword, we can use that to guide the
8553 production we choose. */
8554 keyword = token->keyword;
8557 /* Any of these indicate either a class-specifier, or an
8558 elaborated-type-specifier. */
8563 /* Parse tentatively so that we can back up if we don't find a
8564 class-specifier or enum-specifier. */
8565 cp_parser_parse_tentatively (parser);
8566 /* Look for the class-specifier or enum-specifier. */
8567 if (keyword == RID_ENUM)
8568 type_spec = cp_parser_enum_specifier (parser);
8570 type_spec = cp_parser_class_specifier (parser);
8572 /* If that worked, we're done. */
8573 if (cp_parser_parse_definitely (parser))
8575 if (declares_class_or_enum)
8576 *declares_class_or_enum = true;
8583 /* Look for an elaborated-type-specifier. */
8584 type_spec = cp_parser_elaborated_type_specifier (parser,
8587 /* We're declaring a class or enum -- unless we're using
8589 if (declares_class_or_enum && keyword != RID_TYPENAME)
8590 *declares_class_or_enum = true;
8596 type_spec = cp_parser_cv_qualifier_opt (parser);
8597 /* Even though we call a routine that looks for an optional
8598 qualifier, we know that there should be one. */
8599 my_friendly_assert (type_spec != NULL, 20000328);
8600 /* This type-specifier was a cv-qualified. */
8601 if (is_cv_qualifier)
8602 *is_cv_qualifier = true;
8607 /* The `__complex__' keyword is a GNU extension. */
8608 return cp_lexer_consume_token (parser->lexer)->value;
8614 /* If we do not already have a type-specifier, assume we are looking
8615 at a simple-type-specifier. */
8616 type_spec = cp_parser_simple_type_specifier (parser, flags);
8618 /* If we didn't find a type-specifier, and a type-specifier was not
8619 optional in this context, issue an error message. */
8620 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8622 cp_parser_error (parser, "expected type specifier");
8623 return error_mark_node;
8629 /* Parse a simple-type-specifier.
8631 simple-type-specifier:
8632 :: [opt] nested-name-specifier [opt] type-name
8633 :: [opt] nested-name-specifier template template-id
8648 simple-type-specifier:
8649 __typeof__ unary-expression
8650 __typeof__ ( type-id )
8652 For the various keywords, the value returned is simply the
8653 TREE_IDENTIFIER representing the keyword. For the first two
8654 productions, the value returned is the indicated TYPE_DECL. */
8657 cp_parser_simple_type_specifier (parser, flags)
8659 cp_parser_flags flags;
8661 tree type = NULL_TREE;
8664 /* Peek at the next token. */
8665 token = cp_lexer_peek_token (parser->lexer);
8667 /* If we're looking at a keyword, things are easy. */
8668 switch (token->keyword)
8681 /* Consume the token. */
8682 return cp_lexer_consume_token (parser->lexer)->value;
8688 /* Consume the `typeof' token. */
8689 cp_lexer_consume_token (parser->lexer);
8690 /* Parse the operand to `typeof' */
8691 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8692 /* If it is not already a TYPE, take its type. */
8693 if (!TYPE_P (operand))
8694 operand = finish_typeof (operand);
8703 /* The type-specifier must be a user-defined type. */
8704 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8706 /* Don't gobble tokens or issue error messages if this is an
8707 optional type-specifier. */
8708 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8709 cp_parser_parse_tentatively (parser);
8711 /* Look for the optional `::' operator. */
8712 cp_parser_global_scope_opt (parser,
8713 /*current_scope_valid_p=*/false);
8714 /* Look for the nested-name specifier. */
8715 cp_parser_nested_name_specifier_opt (parser,
8716 /*typename_keyword_p=*/false,
8717 /*check_dependency_p=*/true,
8719 /* If we have seen a nested-name-specifier, and the next token
8720 is `template', then we are using the template-id production. */
8722 && cp_parser_optional_template_keyword (parser))
8724 /* Look for the template-id. */
8725 type = cp_parser_template_id (parser,
8726 /*template_keyword_p=*/true,
8727 /*check_dependency_p=*/true);
8728 /* If the template-id did not name a type, we are out of
8730 if (TREE_CODE (type) != TYPE_DECL)
8732 cp_parser_error (parser, "expected template-id for type");
8736 /* Otherwise, look for a type-name. */
8739 type = cp_parser_type_name (parser);
8740 if (type == error_mark_node)
8744 /* If it didn't work out, we don't have a TYPE. */
8745 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8746 && !cp_parser_parse_definitely (parser))
8750 /* If we didn't get a type-name, issue an error message. */
8751 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8753 cp_parser_error (parser, "expected type-name");
8754 return error_mark_node;
8760 /* Parse a type-name.
8773 Returns a TYPE_DECL for the the type. */
8776 cp_parser_type_name (parser)
8782 /* We can't know yet whether it is a class-name or not. */
8783 cp_parser_parse_tentatively (parser);
8784 /* Try a class-name. */
8785 type_decl = cp_parser_class_name (parser,
8786 /*typename_keyword_p=*/false,
8787 /*template_keyword_p=*/false,
8789 /*check_access_p=*/true,
8790 /*check_dependency_p=*/true,
8791 /*class_head_p=*/false);
8792 /* If it's not a class-name, keep looking. */
8793 if (!cp_parser_parse_definitely (parser))
8795 /* It must be a typedef-name or an enum-name. */
8796 identifier = cp_parser_identifier (parser);
8797 if (identifier == error_mark_node)
8798 return error_mark_node;
8800 /* Look up the type-name. */
8801 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8802 /* Issue an error if we did not find a type-name. */
8803 if (TREE_CODE (type_decl) != TYPE_DECL)
8805 cp_parser_error (parser, "expected type-name");
8806 type_decl = error_mark_node;
8808 /* Remember that the name was used in the definition of the
8809 current class so that we can check later to see if the
8810 meaning would have been different after the class was
8811 entirely defined. */
8812 else if (type_decl != error_mark_node
8814 maybe_note_name_used_in_class (identifier, type_decl);
8821 /* Parse an elaborated-type-specifier. Note that the grammar given
8822 here incorporates the resolution to DR68.
8824 elaborated-type-specifier:
8825 class-key :: [opt] nested-name-specifier [opt] identifier
8826 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8827 enum :: [opt] nested-name-specifier [opt] identifier
8828 typename :: [opt] nested-name-specifier identifier
8829 typename :: [opt] nested-name-specifier template [opt]
8832 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8833 declared `friend'. If IS_DECLARATION is TRUE, then this
8834 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8835 something is being declared.
8837 Returns the TYPE specified. */
8840 cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8843 bool is_declaration;
8845 enum tag_types tag_type;
8847 tree type = NULL_TREE;
8849 /* See if we're looking at the `enum' keyword. */
8850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8852 /* Consume the `enum' token. */
8853 cp_lexer_consume_token (parser->lexer);
8854 /* Remember that it's an enumeration type. */
8855 tag_type = enum_type;
8857 /* Or, it might be `typename'. */
8858 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8861 /* Consume the `typename' token. */
8862 cp_lexer_consume_token (parser->lexer);
8863 /* Remember that it's a `typename' type. */
8864 tag_type = typename_type;
8865 /* The `typename' keyword is only allowed in templates. */
8866 if (!processing_template_decl)
8867 pedwarn ("using `typename' outside of template");
8869 /* Otherwise it must be a class-key. */
8872 tag_type = cp_parser_class_key (parser);
8873 if (tag_type == none_type)
8874 return error_mark_node;
8877 /* Look for the `::' operator. */
8878 cp_parser_global_scope_opt (parser,
8879 /*current_scope_valid_p=*/false);
8880 /* Look for the nested-name-specifier. */
8881 if (tag_type == typename_type)
8883 if (cp_parser_nested_name_specifier (parser,
8884 /*typename_keyword_p=*/true,
8885 /*check_dependency_p=*/true,
8888 return error_mark_node;
8891 /* Even though `typename' is not present, the proposed resolution
8892 to Core Issue 180 says that in `class A<T>::B', `B' should be
8893 considered a type-name, even if `A<T>' is dependent. */
8894 cp_parser_nested_name_specifier_opt (parser,
8895 /*typename_keyword_p=*/true,
8896 /*check_dependency_p=*/true,
8898 /* For everything but enumeration types, consider a template-id. */
8899 if (tag_type != enum_type)
8901 bool template_p = false;
8904 /* Allow the `template' keyword. */
8905 template_p = cp_parser_optional_template_keyword (parser);
8906 /* If we didn't see `template', we don't know if there's a
8907 template-id or not. */
8909 cp_parser_parse_tentatively (parser);
8910 /* Parse the template-id. */
8911 decl = cp_parser_template_id (parser, template_p,
8912 /*check_dependency_p=*/true);
8913 /* If we didn't find a template-id, look for an ordinary
8915 if (!template_p && !cp_parser_parse_definitely (parser))
8917 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8918 in effect, then we must assume that, upon instantiation, the
8919 template will correspond to a class. */
8920 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8921 && tag_type == typename_type)
8922 type = make_typename_type (parser->scope, decl,
8925 type = TREE_TYPE (decl);
8928 /* For an enumeration type, consider only a plain identifier. */
8931 identifier = cp_parser_identifier (parser);
8933 if (identifier == error_mark_node)
8934 return error_mark_node;
8936 /* For a `typename', we needn't call xref_tag. */
8937 if (tag_type == typename_type)
8938 return make_typename_type (parser->scope, identifier,
8940 /* Look up a qualified name in the usual way. */
8945 /* In an elaborated-type-specifier, names are assumed to name
8946 types, so we set IS_TYPE to TRUE when calling
8947 cp_parser_lookup_name. */
8948 decl = cp_parser_lookup_name (parser, identifier,
8949 /*check_access=*/true,
8951 /*is_namespace=*/false,
8952 /*check_dependency=*/true);
8953 decl = (cp_parser_maybe_treat_template_as_class
8954 (decl, /*tag_name_p=*/is_friend));
8956 if (TREE_CODE (decl) != TYPE_DECL)
8958 error ("expected type-name");
8959 return error_mark_node;
8961 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8962 && tag_type != enum_type)
8963 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8964 tag_type == record_type ? "struct" : "class");
8965 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8966 && tag_type == enum_type)
8967 error ("`%T' referred to as enum", TREE_TYPE (decl));
8969 type = TREE_TYPE (decl);
8973 /* An elaborated-type-specifier sometimes introduces a new type and
8974 sometimes names an existing type. Normally, the rule is that it
8975 introduces a new type only if there is not an existing type of
8976 the same name already in scope. For example, given:
8979 void f() { struct S s; }
8981 the `struct S' in the body of `f' is the same `struct S' as in
8982 the global scope; the existing definition is used. However, if
8983 there were no global declaration, this would introduce a new
8984 local class named `S'.
8986 An exception to this rule applies to the following code:
8988 namespace N { struct S; }
8990 Here, the elaborated-type-specifier names a new type
8991 unconditionally; even if there is already an `S' in the
8992 containing scope this declaration names a new type.
8993 This exception only applies if the elaborated-type-specifier
8994 forms the complete declaration:
8998 A declaration consisting solely of `class-key identifier ;' is
8999 either a redeclaration of the name in the current scope or a
9000 forward declaration of the identifier as a class name. It
9001 introduces the name into the current scope.
9003 We are in this situation precisely when the next token is a `;'.
9005 An exception to the exception is that a `friend' declaration does
9006 *not* name a new type; i.e., given:
9008 struct S { friend struct T; };
9010 `T' is not a new type in the scope of `S'.
9012 Also, `new struct S' or `sizeof (struct S)' never results in the
9013 definition of a new type; a new type can only be declared in a
9014 declaration context. */
9016 type = xref_tag (tag_type, identifier,
9017 /*attributes=*/NULL_TREE,
9020 || cp_lexer_next_token_is_not (parser->lexer,
9024 if (tag_type != enum_type)
9025 cp_parser_check_class_key (tag_type, type);
9029 /* Parse an enum-specifier.
9032 enum identifier [opt] { enumerator-list [opt] }
9034 Returns an ENUM_TYPE representing the enumeration. */
9037 cp_parser_enum_specifier (parser)
9041 tree identifier = NULL_TREE;
9044 /* Look for the `enum' keyword. */
9045 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9046 return error_mark_node;
9047 /* Peek at the next token. */
9048 token = cp_lexer_peek_token (parser->lexer);
9050 /* See if it is an identifier. */
9051 if (token->type == CPP_NAME)
9052 identifier = cp_parser_identifier (parser);
9054 /* Look for the `{'. */
9055 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9056 return error_mark_node;
9058 /* At this point, we're going ahead with the enum-specifier, even
9059 if some other problem occurs. */
9060 cp_parser_commit_to_tentative_parse (parser);
9062 /* Issue an error message if type-definitions are forbidden here. */
9063 cp_parser_check_type_definition (parser);
9065 /* Create the new type. */
9066 type = start_enum (identifier ? identifier : make_anon_name ());
9068 /* Peek at the next token. */
9069 token = cp_lexer_peek_token (parser->lexer);
9070 /* If it's not a `}', then there are some enumerators. */
9071 if (token->type != CPP_CLOSE_BRACE)
9072 cp_parser_enumerator_list (parser, type);
9073 /* Look for the `}'. */
9074 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9076 /* Finish up the enumeration. */
9082 /* Parse an enumerator-list. The enumerators all have the indicated
9086 enumerator-definition
9087 enumerator-list , enumerator-definition */
9090 cp_parser_enumerator_list (parser, type)
9098 /* Parse an enumerator-definition. */
9099 cp_parser_enumerator_definition (parser, type);
9100 /* Peek at the next token. */
9101 token = cp_lexer_peek_token (parser->lexer);
9102 /* If it's not a `,', then we've reached the end of the
9104 if (token->type != CPP_COMMA)
9106 /* Otherwise, consume the `,' and keep going. */
9107 cp_lexer_consume_token (parser->lexer);
9108 /* If the next token is a `}', there is a trailing comma. */
9109 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9111 if (pedantic && !in_system_header)
9112 pedwarn ("comma at end of enumerator list");
9118 /* Parse an enumerator-definition. The enumerator has the indicated
9121 enumerator-definition:
9123 enumerator = constant-expression
9129 cp_parser_enumerator_definition (parser, type)
9137 /* Look for the identifier. */
9138 identifier = cp_parser_identifier (parser);
9139 if (identifier == error_mark_node)
9142 /* Peek at the next token. */
9143 token = cp_lexer_peek_token (parser->lexer);
9144 /* If it's an `=', then there's an explicit value. */
9145 if (token->type == CPP_EQ)
9147 /* Consume the `=' token. */
9148 cp_lexer_consume_token (parser->lexer);
9149 /* Parse the value. */
9150 value = cp_parser_constant_expression (parser);
9155 /* Create the enumerator. */
9156 build_enumerator (identifier, value, type);
9159 /* Parse a namespace-name.
9162 original-namespace-name
9165 Returns the NAMESPACE_DECL for the namespace. */
9168 cp_parser_namespace_name (parser)
9172 tree namespace_decl;
9174 /* Get the name of the namespace. */
9175 identifier = cp_parser_identifier (parser);
9176 if (identifier == error_mark_node)
9177 return error_mark_node;
9179 /* Look up the identifier in the currently active scope. Look only
9180 for namespaces, due to:
9184 When looking up a namespace-name in a using-directive or alias
9185 definition, only namespace names are considered.
9191 During the lookup of a name preceding the :: scope resolution
9192 operator, object, function, and enumerator names are ignored.
9194 (Note that cp_parser_class_or_namespace_name only calls this
9195 function if the token after the name is the scope resolution
9197 namespace_decl = cp_parser_lookup_name (parser, identifier,
9198 /*check_access=*/true,
9200 /*is_namespace=*/true,
9201 /*check_dependency=*/true);
9202 /* If it's not a namespace, issue an error. */
9203 if (namespace_decl == error_mark_node
9204 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9206 cp_parser_error (parser, "expected namespace-name");
9207 namespace_decl = error_mark_node;
9210 return namespace_decl;
9213 /* Parse a namespace-definition.
9215 namespace-definition:
9216 named-namespace-definition
9217 unnamed-namespace-definition
9219 named-namespace-definition:
9220 original-namespace-definition
9221 extension-namespace-definition
9223 original-namespace-definition:
9224 namespace identifier { namespace-body }
9226 extension-namespace-definition:
9227 namespace original-namespace-name { namespace-body }
9229 unnamed-namespace-definition:
9230 namespace { namespace-body } */
9233 cp_parser_namespace_definition (parser)
9238 /* Look for the `namespace' keyword. */
9239 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9241 /* Get the name of the namespace. We do not attempt to distinguish
9242 between an original-namespace-definition and an
9243 extension-namespace-definition at this point. The semantic
9244 analysis routines are responsible for that. */
9245 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9246 identifier = cp_parser_identifier (parser);
9248 identifier = NULL_TREE;
9250 /* Look for the `{' to start the namespace. */
9251 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9252 /* Start the namespace. */
9253 push_namespace (identifier);
9254 /* Parse the body of the namespace. */
9255 cp_parser_namespace_body (parser);
9256 /* Finish the namespace. */
9258 /* Look for the final `}'. */
9259 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9262 /* Parse a namespace-body.
9265 declaration-seq [opt] */
9268 cp_parser_namespace_body (parser)
9271 cp_parser_declaration_seq_opt (parser);
9274 /* Parse a namespace-alias-definition.
9276 namespace-alias-definition:
9277 namespace identifier = qualified-namespace-specifier ; */
9280 cp_parser_namespace_alias_definition (parser)
9284 tree namespace_specifier;
9286 /* Look for the `namespace' keyword. */
9287 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9288 /* Look for the identifier. */
9289 identifier = cp_parser_identifier (parser);
9290 if (identifier == error_mark_node)
9292 /* Look for the `=' token. */
9293 cp_parser_require (parser, CPP_EQ, "`='");
9294 /* Look for the qualified-namespace-specifier. */
9296 = cp_parser_qualified_namespace_specifier (parser);
9297 /* Look for the `;' token. */
9298 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9300 /* Register the alias in the symbol table. */
9301 do_namespace_alias (identifier, namespace_specifier);
9304 /* Parse a qualified-namespace-specifier.
9306 qualified-namespace-specifier:
9307 :: [opt] nested-name-specifier [opt] namespace-name
9309 Returns a NAMESPACE_DECL corresponding to the specified
9313 cp_parser_qualified_namespace_specifier (parser)
9316 /* Look for the optional `::'. */
9317 cp_parser_global_scope_opt (parser,
9318 /*current_scope_valid_p=*/false);
9320 /* Look for the optional nested-name-specifier. */
9321 cp_parser_nested_name_specifier_opt (parser,
9322 /*typename_keyword_p=*/false,
9323 /*check_dependency_p=*/true,
9326 return cp_parser_namespace_name (parser);
9329 /* Parse a using-declaration.
9332 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9333 using :: unqualified-id ; */
9336 cp_parser_using_declaration (parser)
9340 bool typename_p = false;
9341 bool global_scope_p;
9346 /* Look for the `using' keyword. */
9347 cp_parser_require_keyword (parser, RID_USING, "`using'");
9349 /* Peek at the next token. */
9350 token = cp_lexer_peek_token (parser->lexer);
9351 /* See if it's `typename'. */
9352 if (token->keyword == RID_TYPENAME)
9354 /* Remember that we've seen it. */
9356 /* Consume the `typename' token. */
9357 cp_lexer_consume_token (parser->lexer);
9360 /* Look for the optional global scope qualification. */
9362 = (cp_parser_global_scope_opt (parser,
9363 /*current_scope_valid_p=*/false)
9366 /* If we saw `typename', or didn't see `::', then there must be a
9367 nested-name-specifier present. */
9368 if (typename_p || !global_scope_p)
9369 cp_parser_nested_name_specifier (parser, typename_p,
9370 /*check_dependency_p=*/true,
9372 /* Otherwise, we could be in either of the two productions. In that
9373 case, treat the nested-name-specifier as optional. */
9375 cp_parser_nested_name_specifier_opt (parser,
9376 /*typename_keyword_p=*/false,
9377 /*check_dependency_p=*/true,
9380 /* Parse the unqualified-id. */
9381 identifier = cp_parser_unqualified_id (parser,
9382 /*template_keyword_p=*/false,
9383 /*check_dependency_p=*/true);
9385 /* The function we call to handle a using-declaration is different
9386 depending on what scope we are in. */
9387 scope = current_scope ();
9388 if (scope && TYPE_P (scope))
9390 /* Create the USING_DECL. */
9391 decl = do_class_using_decl (build_nt (SCOPE_REF,
9394 /* Add it to the list of members in this class. */
9395 finish_member_declaration (decl);
9399 decl = cp_parser_lookup_name_simple (parser, identifier);
9401 do_local_using_decl (decl);
9403 do_toplevel_using_decl (decl);
9406 /* Look for the final `;'. */
9407 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9410 /* Parse a using-directive.
9413 using namespace :: [opt] nested-name-specifier [opt]
9417 cp_parser_using_directive (parser)
9420 tree namespace_decl;
9422 /* Look for the `using' keyword. */
9423 cp_parser_require_keyword (parser, RID_USING, "`using'");
9424 /* And the `namespace' keyword. */
9425 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9426 /* Look for the optional `::' operator. */
9427 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9428 /* And the optional nested-name-sepcifier. */
9429 cp_parser_nested_name_specifier_opt (parser,
9430 /*typename_keyword_p=*/false,
9431 /*check_dependency_p=*/true,
9433 /* Get the namespace being used. */
9434 namespace_decl = cp_parser_namespace_name (parser);
9435 /* Update the symbol table. */
9436 do_using_directive (namespace_decl);
9437 /* Look for the final `;'. */
9438 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9441 /* Parse an asm-definition.
9444 asm ( string-literal ) ;
9449 asm volatile [opt] ( string-literal ) ;
9450 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9451 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9452 : asm-operand-list [opt] ) ;
9453 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9454 : asm-operand-list [opt]
9455 : asm-operand-list [opt] ) ; */
9458 cp_parser_asm_definition (parser)
9463 tree outputs = NULL_TREE;
9464 tree inputs = NULL_TREE;
9465 tree clobbers = NULL_TREE;
9467 bool volatile_p = false;
9468 bool extended_p = false;
9470 /* Look for the `asm' keyword. */
9471 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9472 /* See if the next token is `volatile'. */
9473 if (cp_parser_allow_gnu_extensions_p (parser)
9474 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9476 /* Remember that we saw the `volatile' keyword. */
9478 /* Consume the token. */
9479 cp_lexer_consume_token (parser->lexer);
9481 /* Look for the opening `('. */
9482 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9483 /* Look for the string. */
9484 token = cp_parser_require (parser, CPP_STRING, "asm body");
9487 string = token->value;
9488 /* If we're allowing GNU extensions, check for the extended assembly
9489 syntax. Unfortunately, the `:' tokens need not be separated by
9490 a space in C, and so, for compatibility, we tolerate that here
9491 too. Doing that means that we have to treat the `::' operator as
9493 if (cp_parser_allow_gnu_extensions_p (parser)
9494 && at_function_scope_p ()
9495 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9496 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9498 bool inputs_p = false;
9499 bool clobbers_p = false;
9501 /* The extended syntax was used. */
9504 /* Look for outputs. */
9505 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9507 /* Consume the `:'. */
9508 cp_lexer_consume_token (parser->lexer);
9509 /* Parse the output-operands. */
9510 if (cp_lexer_next_token_is_not (parser->lexer,
9512 && cp_lexer_next_token_is_not (parser->lexer,
9514 && cp_lexer_next_token_is_not (parser->lexer,
9516 outputs = cp_parser_asm_operand_list (parser);
9518 /* If the next token is `::', there are no outputs, and the
9519 next token is the beginning of the inputs. */
9520 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9522 /* Consume the `::' token. */
9523 cp_lexer_consume_token (parser->lexer);
9524 /* The inputs are coming next. */
9528 /* Look for inputs. */
9530 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9533 /* Consume the `:'. */
9534 cp_lexer_consume_token (parser->lexer);
9535 /* Parse the output-operands. */
9536 if (cp_lexer_next_token_is_not (parser->lexer,
9538 && cp_lexer_next_token_is_not (parser->lexer,
9540 && cp_lexer_next_token_is_not (parser->lexer,
9542 inputs = cp_parser_asm_operand_list (parser);
9544 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9545 /* The clobbers are coming next. */
9548 /* Look for clobbers. */
9550 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9553 /* Consume the `:'. */
9554 cp_lexer_consume_token (parser->lexer);
9555 /* Parse the clobbers. */
9556 if (cp_lexer_next_token_is_not (parser->lexer,
9558 clobbers = cp_parser_asm_clobber_list (parser);
9561 /* Look for the closing `)'. */
9562 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9563 cp_parser_skip_to_closing_parenthesis (parser);
9564 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9566 /* Create the ASM_STMT. */
9567 if (at_function_scope_p ())
9570 finish_asm_stmt (volatile_p
9571 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9572 string, outputs, inputs, clobbers);
9573 /* If the extended syntax was not used, mark the ASM_STMT. */
9575 ASM_INPUT_P (asm_stmt) = 1;
9578 assemble_asm (string);
9581 /* Declarators [gram.dcl.decl] */
9583 /* Parse an init-declarator.
9586 declarator initializer [opt]
9591 declarator asm-specification [opt] attributes [opt] initializer [opt]
9593 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9594 Returns a reprsentation of the entity declared. If MEMBER_P is TRUE,
9595 then this declarator appears in a class scope. The new DECL created
9596 by this declarator is returned.
9598 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9599 for a function-definition here as well. If the declarator is a
9600 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9601 be TRUE upon return. By that point, the function-definition will
9602 have been completely parsed.
9604 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9608 cp_parser_init_declarator (parser,
9611 function_definition_allowed_p,
9613 function_definition_p)
9615 tree decl_specifiers;
9616 tree prefix_attributes;
9617 bool function_definition_allowed_p;
9619 bool *function_definition_p;
9624 tree asm_specification;
9626 tree decl = NULL_TREE;
9628 bool is_initialized;
9629 bool is_parenthesized_init;
9630 bool ctor_dtor_or_conv_p;
9633 /* Assume that this is not the declarator for a function
9635 if (function_definition_p)
9636 *function_definition_p = false;
9638 /* Defer access checks while parsing the declarator; we cannot know
9639 what names are accessible until we know what is being
9641 resume_deferring_access_checks ();
9643 /* Parse the declarator. */
9645 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9646 &ctor_dtor_or_conv_p);
9647 /* Gather up the deferred checks. */
9648 stop_deferring_access_checks ();
9650 /* If the DECLARATOR was erroneous, there's no need to go
9652 if (declarator == error_mark_node)
9653 return error_mark_node;
9655 /* Figure out what scope the entity declared by the DECLARATOR is
9656 located in. `grokdeclarator' sometimes changes the scope, so
9657 we compute it now. */
9658 scope = get_scope_of_declarator (declarator);
9660 /* If we're allowing GNU extensions, look for an asm-specification
9662 if (cp_parser_allow_gnu_extensions_p (parser))
9664 /* Look for an asm-specification. */
9665 asm_specification = cp_parser_asm_specification_opt (parser);
9666 /* And attributes. */
9667 attributes = cp_parser_attributes_opt (parser);
9671 asm_specification = NULL_TREE;
9672 attributes = NULL_TREE;
9675 /* Peek at the next token. */
9676 token = cp_lexer_peek_token (parser->lexer);
9677 /* Check to see if the token indicates the start of a
9678 function-definition. */
9679 if (cp_parser_token_starts_function_definition_p (token))
9681 if (!function_definition_allowed_p)
9683 /* If a function-definition should not appear here, issue an
9685 cp_parser_error (parser,
9686 "a function-definition is not allowed here");
9687 return error_mark_node;
9691 /* Neither attributes nor an asm-specification are allowed
9692 on a function-definition. */
9693 if (asm_specification)
9694 error ("an asm-specification is not allowed on a function-definition");
9696 error ("attributes are not allowed on a function-definition");
9697 /* This is a function-definition. */
9698 *function_definition_p = true;
9700 /* Parse the function definition. */
9701 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9702 (parser, decl_specifiers, prefix_attributes, declarator));
9710 Only in function declarations for constructors, destructors, and
9711 type conversions can the decl-specifier-seq be omitted.
9713 We explicitly postpone this check past the point where we handle
9714 function-definitions because we tolerate function-definitions
9715 that are missing their return types in some modes. */
9716 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9718 cp_parser_error (parser,
9719 "expected constructor, destructor, or type conversion");
9720 return error_mark_node;
9723 /* An `=' or an `(' indicates an initializer. */
9724 is_initialized = (token->type == CPP_EQ
9725 || token->type == CPP_OPEN_PAREN);
9726 /* If the init-declarator isn't initialized and isn't followed by a
9727 `,' or `;', it's not a valid init-declarator. */
9729 && token->type != CPP_COMMA
9730 && token->type != CPP_SEMICOLON)
9732 cp_parser_error (parser, "expected init-declarator");
9733 return error_mark_node;
9736 /* Because start_decl has side-effects, we should only call it if we
9737 know we're going ahead. By this point, we know that we cannot
9738 possibly be looking at any other construct. */
9739 cp_parser_commit_to_tentative_parse (parser);
9741 /* Check to see whether or not this declaration is a friend. */
9742 friend_p = cp_parser_friend_p (decl_specifiers);
9744 /* Check that the number of template-parameter-lists is OK. */
9745 if (!cp_parser_check_declarator_template_parameters (parser,
9747 return error_mark_node;
9749 /* Enter the newly declared entry in the symbol table. If we're
9750 processing a declaration in a class-specifier, we wait until
9751 after processing the initializer. */
9754 if (parser->in_unbraced_linkage_specification_p)
9756 decl_specifiers = tree_cons (error_mark_node,
9757 get_identifier ("extern"),
9759 have_extern_spec = false;
9761 decl = start_decl (declarator,
9768 /* Enter the SCOPE. That way unqualified names appearing in the
9769 initializer will be looked up in SCOPE. */
9773 /* Perform deferred access control checks, now that we know in which
9774 SCOPE the declared entity resides. */
9775 if (!member_p && decl)
9777 tree saved_current_function_decl = NULL_TREE;
9779 /* If the entity being declared is a function, pretend that we
9780 are in its scope. If it is a `friend', it may have access to
9781 things that would not otherwise be accessible. */
9782 if (TREE_CODE (decl) == FUNCTION_DECL)
9784 saved_current_function_decl = current_function_decl;
9785 current_function_decl = decl;
9788 /* Perform the access control checks for the declarator and the
9789 the decl-specifiers. */
9790 perform_deferred_access_checks ();
9792 /* Restore the saved value. */
9793 if (TREE_CODE (decl) == FUNCTION_DECL)
9794 current_function_decl = saved_current_function_decl;
9797 /* Parse the initializer. */
9799 initializer = cp_parser_initializer (parser,
9800 &is_parenthesized_init);
9803 initializer = NULL_TREE;
9804 is_parenthesized_init = false;
9807 /* The old parser allows attributes to appear after a parenthesized
9808 initializer. Mark Mitchell proposed removing this functionality
9809 on the GCC mailing lists on 2002-08-13. This parser accepts the
9810 attributes -- but ignores them. */
9811 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9812 if (cp_parser_attributes_opt (parser))
9813 warning ("attributes after parenthesized initializer ignored");
9815 /* Leave the SCOPE, now that we have processed the initializer. It
9816 is important to do this before calling cp_finish_decl because it
9817 makes decisions about whether to create DECL_STMTs or not based
9818 on the current scope. */
9822 /* For an in-class declaration, use `grokfield' to create the
9825 decl = grokfield (declarator, decl_specifiers,
9826 initializer, /*asmspec=*/NULL_TREE,
9827 /*attributes=*/NULL_TREE);
9829 /* Finish processing the declaration. But, skip friend
9831 if (!friend_p && decl)
9832 cp_finish_decl (decl,
9835 /* If the initializer is in parentheses, then this is
9836 a direct-initialization, which means that an
9837 `explicit' constructor is OK. Otherwise, an
9838 `explicit' constructor cannot be used. */
9839 ((is_parenthesized_init || !is_initialized)
9840 ? 0 : LOOKUP_ONLYCONVERTING));
9845 /* Parse a declarator.
9849 ptr-operator declarator
9851 abstract-declarator:
9852 ptr-operator abstract-declarator [opt]
9853 direct-abstract-declarator
9858 attributes [opt] direct-declarator
9859 attributes [opt] ptr-operator declarator
9861 abstract-declarator:
9862 attributes [opt] ptr-operator abstract-declarator [opt]
9863 attributes [opt] direct-abstract-declarator
9865 Returns a representation of the declarator. If the declarator has
9866 the form `* declarator', then an INDIRECT_REF is returned, whose
9867 only operand is the sub-declarator. Analagously, `& declarator' is
9868 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9869 used. The first operand is the TYPE for `X'. The second operand
9870 is an INDIRECT_REF whose operand is the sub-declarator.
9872 Otherwise, the reprsentation is as for a direct-declarator.
9874 (It would be better to define a structure type to represent
9875 declarators, rather than abusing `tree' nodes to represent
9876 declarators. That would be much clearer and save some memory.
9877 There is no reason for declarators to be garbage-collected, for
9878 example; they are created during parser and no longer needed after
9879 `grokdeclarator' has been called.)
9881 For a ptr-operator that has the optional cv-qualifier-seq,
9882 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9885 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9886 true if this declarator represents a constructor, destructor, or
9887 type conversion operator. Otherwise, it is set to false.
9889 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9890 a decl-specifier-seq unless it declares a constructor, destructor,
9891 or conversion. It might seem that we could check this condition in
9892 semantic analysis, rather than parsing, but that makes it difficult
9893 to handle something like `f()'. We want to notice that there are
9894 no decl-specifiers, and therefore realize that this is an
9895 expression, not a declaration.) */
9898 cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
9900 cp_parser_declarator_kind dcl_kind;
9901 bool *ctor_dtor_or_conv_p;
9905 enum tree_code code;
9906 tree cv_qualifier_seq;
9908 tree attributes = NULL_TREE;
9910 /* Assume this is not a constructor, destructor, or type-conversion
9912 if (ctor_dtor_or_conv_p)
9913 *ctor_dtor_or_conv_p = false;
9915 if (cp_parser_allow_gnu_extensions_p (parser))
9916 attributes = cp_parser_attributes_opt (parser);
9918 /* Peek at the next token. */
9919 token = cp_lexer_peek_token (parser->lexer);
9921 /* Check for the ptr-operator production. */
9922 cp_parser_parse_tentatively (parser);
9923 /* Parse the ptr-operator. */
9924 code = cp_parser_ptr_operator (parser,
9927 /* If that worked, then we have a ptr-operator. */
9928 if (cp_parser_parse_definitely (parser))
9930 /* The dependent declarator is optional if we are parsing an
9931 abstract-declarator. */
9932 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9933 cp_parser_parse_tentatively (parser);
9935 /* Parse the dependent declarator. */
9936 declarator = cp_parser_declarator (parser, dcl_kind,
9937 /*ctor_dtor_or_conv_p=*/NULL);
9939 /* If we are parsing an abstract-declarator, we must handle the
9940 case where the dependent declarator is absent. */
9941 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9942 && !cp_parser_parse_definitely (parser))
9943 declarator = NULL_TREE;
9945 /* Build the representation of the ptr-operator. */
9946 if (code == INDIRECT_REF)
9947 declarator = make_pointer_declarator (cv_qualifier_seq,
9950 declarator = make_reference_declarator (cv_qualifier_seq,
9952 /* Handle the pointer-to-member case. */
9954 declarator = build_nt (SCOPE_REF, class_type, declarator);
9956 /* Everything else is a direct-declarator. */
9958 declarator = cp_parser_direct_declarator (parser,
9960 ctor_dtor_or_conv_p);
9962 if (attributes && declarator != error_mark_node)
9963 declarator = tree_cons (attributes, declarator, NULL_TREE);
9968 /* Parse a direct-declarator or direct-abstract-declarator.
9972 direct-declarator ( parameter-declaration-clause )
9973 cv-qualifier-seq [opt]
9974 exception-specification [opt]
9975 direct-declarator [ constant-expression [opt] ]
9978 direct-abstract-declarator:
9979 direct-abstract-declarator [opt]
9980 ( parameter-declaration-clause )
9981 cv-qualifier-seq [opt]
9982 exception-specification [opt]
9983 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9984 ( abstract-declarator )
9986 Returns a representation of the declarator. DCL_KIND is
9987 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9988 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9989 we are parsing a direct-declarator. It is
9990 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9991 of ambiguity we prefer an abstract declarator, as per
9992 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9993 cp_parser_declarator.
9995 For the declarator-id production, the representation is as for an
9996 id-expression, except that a qualified name is represented as a
9997 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9998 see the documentation of the FUNCTION_DECLARATOR_* macros for
9999 information about how to find the various declarator components.
10000 An array-declarator is represented as an ARRAY_REF. The
10001 direct-declarator is the first operand; the constant-expression
10002 indicating the size of the array is the second operand. */
10005 cp_parser_direct_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
10007 cp_parser_declarator_kind dcl_kind;
10008 bool *ctor_dtor_or_conv_p;
10011 tree declarator = NULL_TREE;
10012 tree scope = NULL_TREE;
10013 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10014 bool saved_in_declarator_p = parser->in_declarator_p;
10019 /* Peek at the next token. */
10020 token = cp_lexer_peek_token (parser->lexer);
10021 if (token->type == CPP_OPEN_PAREN)
10023 /* This is either a parameter-declaration-clause, or a
10024 parenthesized declarator. When we know we are parsing a
10025 named declarator, it must be a paranthesized declarator
10026 if FIRST is true. For instance, `(int)' is a
10027 parameter-declaration-clause, with an omitted
10028 direct-abstract-declarator. But `((*))', is a
10029 parenthesized abstract declarator. Finally, when T is a
10030 template parameter `(T)' is a
10031 paremeter-declaration-clause, and not a parenthesized
10034 We first try and parse a parameter-declaration-clause,
10035 and then try a nested declarator (if FIRST is true).
10037 It is not an error for it not to be a
10038 parameter-declaration-clause, even when FIRST is
10044 The first is the declaration of a function while the
10045 second is a the definition of a variable, including its
10048 Having seen only the parenthesis, we cannot know which of
10049 these two alternatives should be selected. Even more
10050 complex are examples like:
10055 The former is a function-declaration; the latter is a
10056 variable initialization.
10058 Thus again, we try a parameter-declation-clause, and if
10059 that fails, we back out and return. */
10061 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10065 cp_parser_parse_tentatively (parser);
10067 /* Consume the `('. */
10068 cp_lexer_consume_token (parser->lexer);
10071 /* If this is going to be an abstract declarator, we're
10072 in a declarator and we can't have default args. */
10073 parser->default_arg_ok_p = false;
10074 parser->in_declarator_p = true;
10077 /* Parse the parameter-declaration-clause. */
10078 params = cp_parser_parameter_declaration_clause (parser);
10080 /* If all went well, parse the cv-qualifier-seq and the
10081 exception-specfication. */
10082 if (cp_parser_parse_definitely (parser))
10084 tree cv_qualifiers;
10085 tree exception_specification;
10088 /* Consume the `)'. */
10089 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10091 /* Parse the cv-qualifier-seq. */
10092 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10093 /* And the exception-specification. */
10094 exception_specification
10095 = cp_parser_exception_specification_opt (parser);
10097 /* Create the function-declarator. */
10098 declarator = make_call_declarator (declarator,
10101 exception_specification);
10102 /* Any subsequent parameter lists are to do with
10103 return type, so are not those of the declared
10105 parser->default_arg_ok_p = false;
10107 /* Repeat the main loop. */
10112 /* If this is the first, we can try a parenthesized
10116 parser->default_arg_ok_p = saved_default_arg_ok_p;
10117 parser->in_declarator_p = saved_in_declarator_p;
10119 /* Consume the `('. */
10120 cp_lexer_consume_token (parser->lexer);
10121 /* Parse the nested declarator. */
10123 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10125 /* Expect a `)'. */
10126 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10127 declarator = error_mark_node;
10128 if (declarator == error_mark_node)
10131 goto handle_declarator;
10133 /* Otherwise, we must be done. */
10137 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10138 && token->type == CPP_OPEN_SQUARE)
10140 /* Parse an array-declarator. */
10144 parser->default_arg_ok_p = false;
10145 parser->in_declarator_p = true;
10146 /* Consume the `['. */
10147 cp_lexer_consume_token (parser->lexer);
10148 /* Peek at the next token. */
10149 token = cp_lexer_peek_token (parser->lexer);
10150 /* If the next token is `]', then there is no
10151 constant-expression. */
10152 if (token->type != CPP_CLOSE_SQUARE)
10153 bounds = cp_parser_constant_expression (parser);
10155 bounds = NULL_TREE;
10156 /* Look for the closing `]'. */
10157 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10159 declarator = error_mark_node;
10163 declarator = build_nt (ARRAY_REF, declarator, bounds);
10165 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10167 /* Parse a declarator_id */
10168 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10169 cp_parser_parse_tentatively (parser);
10170 declarator = cp_parser_declarator_id (parser);
10171 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER
10172 && !cp_parser_parse_definitely (parser))
10173 declarator = error_mark_node;
10174 if (declarator == error_mark_node)
10177 if (TREE_CODE (declarator) == SCOPE_REF)
10179 tree scope = TREE_OPERAND (declarator, 0);
10181 /* In the declaration of a member of a template class
10182 outside of the class itself, the SCOPE will sometimes
10183 be a TYPENAME_TYPE. For example, given:
10185 template <typename T>
10186 int S<T>::R::i = 3;
10188 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10189 this context, we must resolve S<T>::R to an ordinary
10190 type, rather than a typename type.
10192 The reason we normally avoid resolving TYPENAME_TYPEs
10193 is that a specialization of `S' might render
10194 `S<T>::R' not a type. However, if `S' is
10195 specialized, then this `i' will not be used, so there
10196 is no harm in resolving the types here. */
10197 if (TREE_CODE (scope) == TYPENAME_TYPE)
10199 /* Resolve the TYPENAME_TYPE. */
10200 scope = cp_parser_resolve_typename_type (parser, scope);
10201 /* If that failed, the declarator is invalid. */
10202 if (scope == error_mark_node)
10203 return error_mark_node;
10204 /* Build a new DECLARATOR. */
10205 declarator = build_nt (SCOPE_REF,
10207 TREE_OPERAND (declarator, 1));
10211 /* Check to see whether the declarator-id names a constructor,
10212 destructor, or conversion. */
10213 if (declarator && ctor_dtor_or_conv_p
10214 && ((TREE_CODE (declarator) == SCOPE_REF
10215 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10216 || (TREE_CODE (declarator) != SCOPE_REF
10217 && at_class_scope_p ())))
10219 tree unqualified_name;
10222 /* Get the unqualified part of the name. */
10223 if (TREE_CODE (declarator) == SCOPE_REF)
10225 class_type = TREE_OPERAND (declarator, 0);
10226 unqualified_name = TREE_OPERAND (declarator, 1);
10230 class_type = current_class_type;
10231 unqualified_name = declarator;
10234 /* See if it names ctor, dtor or conv. */
10235 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10236 || IDENTIFIER_TYPENAME_P (unqualified_name)
10237 || constructor_name_p (unqualified_name, class_type))
10238 *ctor_dtor_or_conv_p = true;
10241 handle_declarator:;
10242 scope = get_scope_of_declarator (declarator);
10244 /* Any names that appear after the declarator-id for a member
10245 are looked up in the containing scope. */
10246 push_scope (scope);
10247 parser->in_declarator_p = true;
10248 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10250 && (TREE_CODE (declarator) == SCOPE_REF
10251 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10252 /* Default args are only allowed on function
10254 parser->default_arg_ok_p = saved_default_arg_ok_p;
10256 parser->default_arg_ok_p = false;
10265 /* For an abstract declarator, we might wind up with nothing at this
10266 point. That's an error; the declarator is not optional. */
10268 cp_parser_error (parser, "expected declarator");
10270 /* If we entered a scope, we must exit it now. */
10274 parser->default_arg_ok_p = saved_default_arg_ok_p;
10275 parser->in_declarator_p = saved_in_declarator_p;
10280 /* Parse a ptr-operator.
10283 * cv-qualifier-seq [opt]
10285 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10290 & cv-qualifier-seq [opt]
10292 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10293 used. Returns ADDR_EXPR if a reference was used. In the
10294 case of a pointer-to-member, *TYPE is filled in with the
10295 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10296 with the cv-qualifier-seq, or NULL_TREE, if there are no
10297 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10299 static enum tree_code
10300 cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10303 tree *cv_qualifier_seq;
10305 enum tree_code code = ERROR_MARK;
10308 /* Assume that it's not a pointer-to-member. */
10310 /* And that there are no cv-qualifiers. */
10311 *cv_qualifier_seq = NULL_TREE;
10313 /* Peek at the next token. */
10314 token = cp_lexer_peek_token (parser->lexer);
10315 /* If it's a `*' or `&' we have a pointer or reference. */
10316 if (token->type == CPP_MULT || token->type == CPP_AND)
10318 /* Remember which ptr-operator we were processing. */
10319 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10321 /* Consume the `*' or `&'. */
10322 cp_lexer_consume_token (parser->lexer);
10324 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10325 `&', if we are allowing GNU extensions. (The only qualifier
10326 that can legally appear after `&' is `restrict', but that is
10327 enforced during semantic analysis. */
10328 if (code == INDIRECT_REF
10329 || cp_parser_allow_gnu_extensions_p (parser))
10330 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10334 /* Try the pointer-to-member case. */
10335 cp_parser_parse_tentatively (parser);
10336 /* Look for the optional `::' operator. */
10337 cp_parser_global_scope_opt (parser,
10338 /*current_scope_valid_p=*/false);
10339 /* Look for the nested-name specifier. */
10340 cp_parser_nested_name_specifier (parser,
10341 /*typename_keyword_p=*/false,
10342 /*check_dependency_p=*/true,
10344 /* If we found it, and the next token is a `*', then we are
10345 indeed looking at a pointer-to-member operator. */
10346 if (!cp_parser_error_occurred (parser)
10347 && cp_parser_require (parser, CPP_MULT, "`*'"))
10349 /* The type of which the member is a member is given by the
10351 *type = parser->scope;
10352 /* The next name will not be qualified. */
10353 parser->scope = NULL_TREE;
10354 parser->qualifying_scope = NULL_TREE;
10355 parser->object_scope = NULL_TREE;
10356 /* Indicate that the `*' operator was used. */
10357 code = INDIRECT_REF;
10358 /* Look for the optional cv-qualifier-seq. */
10359 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10361 /* If that didn't work we don't have a ptr-operator. */
10362 if (!cp_parser_parse_definitely (parser))
10363 cp_parser_error (parser, "expected ptr-operator");
10369 /* Parse an (optional) cv-qualifier-seq.
10372 cv-qualifier cv-qualifier-seq [opt]
10374 Returns a TREE_LIST. The TREE_VALUE of each node is the
10375 representation of a cv-qualifier. */
10378 cp_parser_cv_qualifier_seq_opt (parser)
10381 tree cv_qualifiers = NULL_TREE;
10387 /* Look for the next cv-qualifier. */
10388 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10389 /* If we didn't find one, we're done. */
10393 /* Add this cv-qualifier to the list. */
10395 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10398 /* We built up the list in reverse order. */
10399 return nreverse (cv_qualifiers);
10402 /* Parse an (optional) cv-qualifier.
10414 cp_parser_cv_qualifier_opt (parser)
10418 tree cv_qualifier = NULL_TREE;
10420 /* Peek at the next token. */
10421 token = cp_lexer_peek_token (parser->lexer);
10422 /* See if it's a cv-qualifier. */
10423 switch (token->keyword)
10428 /* Save the value of the token. */
10429 cv_qualifier = token->value;
10430 /* Consume the token. */
10431 cp_lexer_consume_token (parser->lexer);
10438 return cv_qualifier;
10441 /* Parse a declarator-id.
10445 :: [opt] nested-name-specifier [opt] type-name
10447 In the `id-expression' case, the value returned is as for
10448 cp_parser_id_expression if the id-expression was an unqualified-id.
10449 If the id-expression was a qualified-id, then a SCOPE_REF is
10450 returned. The first operand is the scope (either a NAMESPACE_DECL
10451 or TREE_TYPE), but the second is still just a representation of an
10455 cp_parser_declarator_id (parser)
10458 tree id_expression;
10460 /* The expression must be an id-expression. Assume that qualified
10461 names are the names of types so that:
10464 int S<T>::R::i = 3;
10466 will work; we must treat `S<T>::R' as the name of a type.
10467 Similarly, assume that qualified names are templates, where
10471 int S<T>::R<T>::i = 3;
10474 id_expression = cp_parser_id_expression (parser,
10475 /*template_keyword_p=*/false,
10476 /*check_dependency_p=*/false,
10477 /*template_p=*/NULL);
10478 /* If the name was qualified, create a SCOPE_REF to represent
10481 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10483 return id_expression;
10486 /* Parse a type-id.
10489 type-specifier-seq abstract-declarator [opt]
10491 Returns the TYPE specified. */
10494 cp_parser_type_id (parser)
10497 tree type_specifier_seq;
10498 tree abstract_declarator;
10500 /* Parse the type-specifier-seq. */
10502 = cp_parser_type_specifier_seq (parser);
10503 if (type_specifier_seq == error_mark_node)
10504 return error_mark_node;
10506 /* There might or might not be an abstract declarator. */
10507 cp_parser_parse_tentatively (parser);
10508 /* Look for the declarator. */
10509 abstract_declarator
10510 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10511 /* Check to see if there really was a declarator. */
10512 if (!cp_parser_parse_definitely (parser))
10513 abstract_declarator = NULL_TREE;
10515 return groktypename (build_tree_list (type_specifier_seq,
10516 abstract_declarator));
10519 /* Parse a type-specifier-seq.
10521 type-specifier-seq:
10522 type-specifier type-specifier-seq [opt]
10526 type-specifier-seq:
10527 attributes type-specifier-seq [opt]
10529 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10530 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10533 cp_parser_type_specifier_seq (parser)
10536 bool seen_type_specifier = false;
10537 tree type_specifier_seq = NULL_TREE;
10539 /* Parse the type-specifiers and attributes. */
10542 tree type_specifier;
10544 /* Check for attributes first. */
10545 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10547 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10549 type_specifier_seq);
10553 /* After the first type-specifier, others are optional. */
10554 if (seen_type_specifier)
10555 cp_parser_parse_tentatively (parser);
10556 /* Look for the type-specifier. */
10557 type_specifier = cp_parser_type_specifier (parser,
10558 CP_PARSER_FLAGS_NONE,
10559 /*is_friend=*/false,
10560 /*is_declaration=*/false,
10563 /* If the first type-specifier could not be found, this is not a
10564 type-specifier-seq at all. */
10565 if (!seen_type_specifier && type_specifier == error_mark_node)
10566 return error_mark_node;
10567 /* If subsequent type-specifiers could not be found, the
10568 type-specifier-seq is complete. */
10569 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10572 /* Add the new type-specifier to the list. */
10574 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10575 seen_type_specifier = true;
10578 /* We built up the list in reverse order. */
10579 return nreverse (type_specifier_seq);
10582 /* Parse a parameter-declaration-clause.
10584 parameter-declaration-clause:
10585 parameter-declaration-list [opt] ... [opt]
10586 parameter-declaration-list , ...
10588 Returns a representation for the parameter declarations. Each node
10589 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10590 representation.) If the parameter-declaration-clause ends with an
10591 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10592 list. A return value of NULL_TREE indicates a
10593 parameter-declaration-clause consisting only of an ellipsis. */
10596 cp_parser_parameter_declaration_clause (parser)
10603 /* Peek at the next token. */
10604 token = cp_lexer_peek_token (parser->lexer);
10605 /* Check for trivial parameter-declaration-clauses. */
10606 if (token->type == CPP_ELLIPSIS)
10608 /* Consume the `...' token. */
10609 cp_lexer_consume_token (parser->lexer);
10612 else if (token->type == CPP_CLOSE_PAREN)
10613 /* There are no parameters. */
10615 #ifndef NO_IMPLICIT_EXTERN_C
10616 if (in_system_header && current_class_type == NULL
10617 && current_lang_name == lang_name_c)
10621 return void_list_node;
10623 /* Check for `(void)', too, which is a special case. */
10624 else if (token->keyword == RID_VOID
10625 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10626 == CPP_CLOSE_PAREN))
10628 /* Consume the `void' token. */
10629 cp_lexer_consume_token (parser->lexer);
10630 /* There are no parameters. */
10631 return void_list_node;
10634 /* Parse the parameter-declaration-list. */
10635 parameters = cp_parser_parameter_declaration_list (parser);
10636 /* If a parse error occurred while parsing the
10637 parameter-declaration-list, then the entire
10638 parameter-declaration-clause is erroneous. */
10639 if (parameters == error_mark_node)
10640 return error_mark_node;
10642 /* Peek at the next token. */
10643 token = cp_lexer_peek_token (parser->lexer);
10644 /* If it's a `,', the clause should terminate with an ellipsis. */
10645 if (token->type == CPP_COMMA)
10647 /* Consume the `,'. */
10648 cp_lexer_consume_token (parser->lexer);
10649 /* Expect an ellipsis. */
10651 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10653 /* It might also be `...' if the optional trailing `,' was
10655 else if (token->type == CPP_ELLIPSIS)
10657 /* Consume the `...' token. */
10658 cp_lexer_consume_token (parser->lexer);
10659 /* And remember that we saw it. */
10663 ellipsis_p = false;
10665 /* Finish the parameter list. */
10666 return finish_parmlist (parameters, ellipsis_p);
10669 /* Parse a parameter-declaration-list.
10671 parameter-declaration-list:
10672 parameter-declaration
10673 parameter-declaration-list , parameter-declaration
10675 Returns a representation of the parameter-declaration-list, as for
10676 cp_parser_parameter_declaration_clause. However, the
10677 `void_list_node' is never appended to the list. */
10680 cp_parser_parameter_declaration_list (parser)
10683 tree parameters = NULL_TREE;
10685 /* Look for more parameters. */
10689 /* Parse the parameter. */
10691 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10693 /* If a parse error ocurred parsing the parameter declaration,
10694 then the entire parameter-declaration-list is erroneous. */
10695 if (parameter == error_mark_node)
10697 parameters = error_mark_node;
10700 /* Add the new parameter to the list. */
10701 TREE_CHAIN (parameter) = parameters;
10702 parameters = parameter;
10704 /* Peek at the next token. */
10705 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10706 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10707 /* The parameter-declaration-list is complete. */
10709 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10713 /* Peek at the next token. */
10714 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10715 /* If it's an ellipsis, then the list is complete. */
10716 if (token->type == CPP_ELLIPSIS)
10718 /* Otherwise, there must be more parameters. Consume the
10720 cp_lexer_consume_token (parser->lexer);
10724 cp_parser_error (parser, "expected `,' or `...'");
10729 /* We built up the list in reverse order; straighten it out now. */
10730 return nreverse (parameters);
10733 /* Parse a parameter declaration.
10735 parameter-declaration:
10736 decl-specifier-seq declarator
10737 decl-specifier-seq declarator = assignment-expression
10738 decl-specifier-seq abstract-declarator [opt]
10739 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10741 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10742 declares a template parameter. (In that case, a non-nested `>'
10743 token encountered during the parsing of the assignment-expression
10744 is not interpreted as a greater-than operator.)
10746 Returns a TREE_LIST representing the parameter-declaration. The
10747 TREE_VALUE is a representation of the decl-specifier-seq and
10748 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10749 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10750 TREE_VALUE represents the declarator. */
10753 cp_parser_parameter_declaration (cp_parser *parser,
10754 bool template_parm_p)
10756 bool declares_class_or_enum;
10757 bool greater_than_is_operator_p;
10758 tree decl_specifiers;
10761 tree default_argument;
10764 const char *saved_message;
10766 /* In a template parameter, `>' is not an operator.
10770 When parsing a default template-argument for a non-type
10771 template-parameter, the first non-nested `>' is taken as the end
10772 of the template parameter-list rather than a greater-than
10774 greater_than_is_operator_p = !template_parm_p;
10776 /* Type definitions may not appear in parameter types. */
10777 saved_message = parser->type_definition_forbidden_message;
10778 parser->type_definition_forbidden_message
10779 = "types may not be defined in parameter types";
10781 /* Parse the declaration-specifiers. */
10783 = cp_parser_decl_specifier_seq (parser,
10784 CP_PARSER_FLAGS_NONE,
10786 &declares_class_or_enum);
10787 /* If an error occurred, there's no reason to attempt to parse the
10788 rest of the declaration. */
10789 if (cp_parser_error_occurred (parser))
10791 parser->type_definition_forbidden_message = saved_message;
10792 return error_mark_node;
10795 /* Peek at the next token. */
10796 token = cp_lexer_peek_token (parser->lexer);
10797 /* If the next token is a `)', `,', `=', `>', or `...', then there
10798 is no declarator. */
10799 if (token->type == CPP_CLOSE_PAREN
10800 || token->type == CPP_COMMA
10801 || token->type == CPP_EQ
10802 || token->type == CPP_ELLIPSIS
10803 || token->type == CPP_GREATER)
10804 declarator = NULL_TREE;
10805 /* Otherwise, there should be a declarator. */
10808 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10809 parser->default_arg_ok_p = false;
10811 declarator = cp_parser_declarator (parser,
10812 CP_PARSER_DECLARATOR_EITHER,
10813 /*ctor_dtor_or_conv_p=*/NULL);
10814 parser->default_arg_ok_p = saved_default_arg_ok_p;
10815 /* After the declarator, allow more attributes. */
10816 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10819 /* The restriction on defining new types applies only to the type
10820 of the parameter, not to the default argument. */
10821 parser->type_definition_forbidden_message = saved_message;
10823 /* If the next token is `=', then process a default argument. */
10824 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10826 bool saved_greater_than_is_operator_p;
10827 /* Consume the `='. */
10828 cp_lexer_consume_token (parser->lexer);
10830 /* If we are defining a class, then the tokens that make up the
10831 default argument must be saved and processed later. */
10832 if (!template_parm_p && at_class_scope_p ()
10833 && TYPE_BEING_DEFINED (current_class_type))
10835 unsigned depth = 0;
10837 /* Create a DEFAULT_ARG to represented the unparsed default
10839 default_argument = make_node (DEFAULT_ARG);
10840 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10842 /* Add tokens until we have processed the entire default
10849 /* Peek at the next token. */
10850 token = cp_lexer_peek_token (parser->lexer);
10851 /* What we do depends on what token we have. */
10852 switch (token->type)
10854 /* In valid code, a default argument must be
10855 immediately followed by a `,' `)', or `...'. */
10857 case CPP_CLOSE_PAREN:
10859 /* If we run into a non-nested `;', `}', or `]',
10860 then the code is invalid -- but the default
10861 argument is certainly over. */
10862 case CPP_SEMICOLON:
10863 case CPP_CLOSE_BRACE:
10864 case CPP_CLOSE_SQUARE:
10867 /* Update DEPTH, if necessary. */
10868 else if (token->type == CPP_CLOSE_PAREN
10869 || token->type == CPP_CLOSE_BRACE
10870 || token->type == CPP_CLOSE_SQUARE)
10874 case CPP_OPEN_PAREN:
10875 case CPP_OPEN_SQUARE:
10876 case CPP_OPEN_BRACE:
10881 /* If we see a non-nested `>', and `>' is not an
10882 operator, then it marks the end of the default
10884 if (!depth && !greater_than_is_operator_p)
10888 /* If we run out of tokens, issue an error message. */
10890 error ("file ends in default argument");
10896 /* In these cases, we should look for template-ids.
10897 For example, if the default argument is
10898 `X<int, double>()', we need to do name lookup to
10899 figure out whether or not `X' is a template; if
10900 so, the `,' does not end the deault argument.
10902 That is not yet done. */
10909 /* If we've reached the end, stop. */
10913 /* Add the token to the token block. */
10914 token = cp_lexer_consume_token (parser->lexer);
10915 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10919 /* Outside of a class definition, we can just parse the
10920 assignment-expression. */
10923 bool saved_local_variables_forbidden_p;
10925 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10927 saved_greater_than_is_operator_p
10928 = parser->greater_than_is_operator_p;
10929 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10930 /* Local variable names (and the `this' keyword) may not
10931 appear in a default argument. */
10932 saved_local_variables_forbidden_p
10933 = parser->local_variables_forbidden_p;
10934 parser->local_variables_forbidden_p = true;
10935 /* Parse the assignment-expression. */
10936 default_argument = cp_parser_assignment_expression (parser);
10937 /* Restore saved state. */
10938 parser->greater_than_is_operator_p
10939 = saved_greater_than_is_operator_p;
10940 parser->local_variables_forbidden_p
10941 = saved_local_variables_forbidden_p;
10943 if (!parser->default_arg_ok_p)
10945 pedwarn ("default arguments are only permitted on functions");
10946 if (flag_pedantic_errors)
10947 default_argument = NULL_TREE;
10951 default_argument = NULL_TREE;
10953 /* Create the representation of the parameter. */
10955 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10956 parameter = build_tree_list (default_argument,
10957 build_tree_list (decl_specifiers,
10963 /* Parse a function-definition.
10965 function-definition:
10966 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10968 decl-specifier-seq [opt] declarator function-try-block
10972 function-definition:
10973 __extension__ function-definition
10975 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10976 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10980 cp_parser_function_definition (parser, friend_p)
10984 tree decl_specifiers;
10989 bool declares_class_or_enum;
10991 /* The saved value of the PEDANTIC flag. */
10992 int saved_pedantic;
10994 /* Any pending qualification must be cleared by our caller. It is
10995 more robust to force the callers to clear PARSER->SCOPE than to
10996 do it here since if the qualification is in effect here, it might
10997 also end up in effect elsewhere that it is not intended. */
10998 my_friendly_assert (!parser->scope, 20010821);
11000 /* Handle `__extension__'. */
11001 if (cp_parser_extension_opt (parser, &saved_pedantic))
11003 /* Parse the function-definition. */
11004 fn = cp_parser_function_definition (parser, friend_p);
11005 /* Restore the PEDANTIC flag. */
11006 pedantic = saved_pedantic;
11011 /* Check to see if this definition appears in a class-specifier. */
11012 member_p = (at_class_scope_p ()
11013 && TYPE_BEING_DEFINED (current_class_type));
11014 /* Defer access checks in the decl-specifier-seq until we know what
11015 function is being defined. There is no need to do this for the
11016 definition of member functions; we cannot be defining a member
11017 from another class. */
11018 push_deferring_access_checks (!member_p);
11020 /* Parse the decl-specifier-seq. */
11022 = cp_parser_decl_specifier_seq (parser,
11023 CP_PARSER_FLAGS_OPTIONAL,
11025 &declares_class_or_enum);
11026 /* Figure out whether this declaration is a `friend'. */
11028 *friend_p = cp_parser_friend_p (decl_specifiers);
11030 /* Parse the declarator. */
11031 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11032 /*ctor_dtor_or_conv_p=*/NULL);
11034 /* Gather up any access checks that occurred. */
11035 stop_deferring_access_checks ();
11037 /* If something has already gone wrong, we may as well stop now. */
11038 if (declarator == error_mark_node)
11040 /* Skip to the end of the function, or if this wasn't anything
11041 like a function-definition, to a `;' in the hopes of finding
11042 a sensible place from which to continue parsing. */
11043 cp_parser_skip_to_end_of_block_or_statement (parser);
11044 pop_deferring_access_checks ();
11045 return error_mark_node;
11048 /* The next character should be a `{' (for a simple function
11049 definition), a `:' (for a ctor-initializer), or `try' (for a
11050 function-try block). */
11051 token = cp_lexer_peek_token (parser->lexer);
11052 if (!cp_parser_token_starts_function_definition_p (token))
11054 /* Issue the error-message. */
11055 cp_parser_error (parser, "expected function-definition");
11056 /* Skip to the next `;'. */
11057 cp_parser_skip_to_end_of_block_or_statement (parser);
11059 pop_deferring_access_checks ();
11060 return error_mark_node;
11063 /* If we are in a class scope, then we must handle
11064 function-definitions specially. In particular, we save away the
11065 tokens that make up the function body, and parse them again
11066 later, in order to handle code like:
11069 int f () { return i; }
11073 Here, we cannot parse the body of `f' until after we have seen
11074 the declaration of `i'. */
11077 cp_token_cache *cache;
11079 /* Create the function-declaration. */
11080 fn = start_method (decl_specifiers, declarator, attributes);
11081 /* If something went badly wrong, bail out now. */
11082 if (fn == error_mark_node)
11084 /* If there's a function-body, skip it. */
11085 if (cp_parser_token_starts_function_definition_p
11086 (cp_lexer_peek_token (parser->lexer)))
11087 cp_parser_skip_to_end_of_block_or_statement (parser);
11088 pop_deferring_access_checks ();
11089 return error_mark_node;
11092 /* Create a token cache. */
11093 cache = cp_token_cache_new ();
11094 /* Save away the tokens that make up the body of the
11096 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11097 /* Handle function try blocks. */
11098 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11099 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11101 /* Save away the inline definition; we will process it when the
11102 class is complete. */
11103 DECL_PENDING_INLINE_INFO (fn) = cache;
11104 DECL_PENDING_INLINE_P (fn) = 1;
11106 /* We're done with the inline definition. */
11107 finish_method (fn);
11109 /* Add FN to the queue of functions to be parsed later. */
11110 TREE_VALUE (parser->unparsed_functions_queues)
11111 = tree_cons (NULL_TREE, fn,
11112 TREE_VALUE (parser->unparsed_functions_queues));
11114 pop_deferring_access_checks ();
11118 /* Check that the number of template-parameter-lists is OK. */
11119 if (!cp_parser_check_declarator_template_parameters (parser,
11122 cp_parser_skip_to_end_of_block_or_statement (parser);
11123 pop_deferring_access_checks ();
11124 return error_mark_node;
11127 fn = cp_parser_function_definition_from_specifiers_and_declarator
11128 (parser, decl_specifiers, attributes, declarator);
11129 pop_deferring_access_checks ();
11133 /* Parse a function-body.
11136 compound_statement */
11139 cp_parser_function_body (cp_parser *parser)
11141 cp_parser_compound_statement (parser);
11144 /* Parse a ctor-initializer-opt followed by a function-body. Return
11145 true if a ctor-initializer was present. */
11148 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11151 bool ctor_initializer_p;
11153 /* Begin the function body. */
11154 body = begin_function_body ();
11155 /* Parse the optional ctor-initializer. */
11156 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11157 /* Parse the function-body. */
11158 cp_parser_function_body (parser);
11159 /* Finish the function body. */
11160 finish_function_body (body);
11162 return ctor_initializer_p;
11165 /* Parse an initializer.
11168 = initializer-clause
11169 ( expression-list )
11171 Returns a expression representing the initializer. If no
11172 initializer is present, NULL_TREE is returned.
11174 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11175 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11176 set to FALSE if there is no initializer present. */
11179 cp_parser_initializer (parser, is_parenthesized_init)
11181 bool *is_parenthesized_init;
11186 /* Peek at the next token. */
11187 token = cp_lexer_peek_token (parser->lexer);
11189 /* Let our caller know whether or not this initializer was
11191 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11193 if (token->type == CPP_EQ)
11195 /* Consume the `='. */
11196 cp_lexer_consume_token (parser->lexer);
11197 /* Parse the initializer-clause. */
11198 init = cp_parser_initializer_clause (parser);
11200 else if (token->type == CPP_OPEN_PAREN)
11202 /* Consume the `('. */
11203 cp_lexer_consume_token (parser->lexer);
11204 /* Parse the expression-list. */
11205 init = cp_parser_expression_list (parser);
11206 /* Consume the `)' token. */
11207 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11208 cp_parser_skip_to_closing_parenthesis (parser);
11212 /* Anything else is an error. */
11213 cp_parser_error (parser, "expected initializer");
11214 init = error_mark_node;
11220 /* Parse an initializer-clause.
11222 initializer-clause:
11223 assignment-expression
11224 { initializer-list , [opt] }
11227 Returns an expression representing the initializer.
11229 If the `assignment-expression' production is used the value
11230 returned is simply a reprsentation for the expression.
11232 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11233 the elements of the initializer-list (or NULL_TREE, if the last
11234 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11235 NULL_TREE. There is no way to detect whether or not the optional
11236 trailing `,' was provided. */
11239 cp_parser_initializer_clause (parser)
11244 /* If it is not a `{', then we are looking at an
11245 assignment-expression. */
11246 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11247 initializer = cp_parser_assignment_expression (parser);
11250 /* Consume the `{' token. */
11251 cp_lexer_consume_token (parser->lexer);
11252 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11253 initializer = make_node (CONSTRUCTOR);
11254 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11255 necessary, but check_initializer depends upon it, for
11257 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11258 /* If it's not a `}', then there is a non-trivial initializer. */
11259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11261 /* Parse the initializer list. */
11262 CONSTRUCTOR_ELTS (initializer)
11263 = cp_parser_initializer_list (parser);
11264 /* A trailing `,' token is allowed. */
11265 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11266 cp_lexer_consume_token (parser->lexer);
11269 /* Now, there should be a trailing `}'. */
11270 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11273 return initializer;
11276 /* Parse an initializer-list.
11280 initializer-list , initializer-clause
11285 identifier : initializer-clause
11286 initializer-list, identifier : initializer-clause
11288 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11289 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11290 IDENTIFIER_NODE naming the field to initialize. */
11293 cp_parser_initializer_list (parser)
11296 tree initializers = NULL_TREE;
11298 /* Parse the rest of the list. */
11305 /* If the next token is an identifier and the following one is a
11306 colon, we are looking at the GNU designated-initializer
11308 if (cp_parser_allow_gnu_extensions_p (parser)
11309 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11310 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11312 /* Consume the identifier. */
11313 identifier = cp_lexer_consume_token (parser->lexer)->value;
11314 /* Consume the `:'. */
11315 cp_lexer_consume_token (parser->lexer);
11318 identifier = NULL_TREE;
11320 /* Parse the initializer. */
11321 initializer = cp_parser_initializer_clause (parser);
11323 /* Add it to the list. */
11324 initializers = tree_cons (identifier, initializer, initializers);
11326 /* If the next token is not a comma, we have reached the end of
11328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11331 /* Peek at the next token. */
11332 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11333 /* If the next token is a `}', then we're still done. An
11334 initializer-clause can have a trailing `,' after the
11335 initializer-list and before the closing `}'. */
11336 if (token->type == CPP_CLOSE_BRACE)
11339 /* Consume the `,' token. */
11340 cp_lexer_consume_token (parser->lexer);
11343 /* The initializers were built up in reverse order, so we need to
11344 reverse them now. */
11345 return nreverse (initializers);
11348 /* Classes [gram.class] */
11350 /* Parse a class-name.
11356 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11357 to indicate that names looked up in dependent types should be
11358 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11359 keyword has been used to indicate that the name that appears next
11360 is a template. TYPE_P is true iff the next name should be treated
11361 as class-name, even if it is declared to be some other kind of name
11362 as well. The accessibility of the class-name is checked iff
11363 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11364 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11365 is the class being defined in a class-head.
11367 Returns the TYPE_DECL representing the class. */
11370 cp_parser_class_name (cp_parser *parser,
11371 bool typename_keyword_p,
11372 bool template_keyword_p,
11374 bool check_access_p,
11375 bool check_dependency_p,
11383 /* All class-names start with an identifier. */
11384 token = cp_lexer_peek_token (parser->lexer);
11385 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11387 cp_parser_error (parser, "expected class-name");
11388 return error_mark_node;
11391 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11392 to a template-id, so we save it here. */
11393 scope = parser->scope;
11394 /* Any name names a type if we're following the `typename' keyword
11395 in a qualified name where the enclosing scope is type-dependent. */
11396 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11397 && cp_parser_dependent_type_p (scope));
11398 /* Handle the common case (an identifier, but not a template-id)
11400 if (token->type == CPP_NAME
11401 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11405 /* Look for the identifier. */
11406 identifier = cp_parser_identifier (parser);
11407 /* If the next token isn't an identifier, we are certainly not
11408 looking at a class-name. */
11409 if (identifier == error_mark_node)
11410 decl = error_mark_node;
11411 /* If we know this is a type-name, there's no need to look it
11413 else if (typename_p)
11417 /* If the next token is a `::', then the name must be a type
11420 [basic.lookup.qual]
11422 During the lookup for a name preceding the :: scope
11423 resolution operator, object, function, and enumerator
11424 names are ignored. */
11425 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11427 /* Look up the name. */
11428 decl = cp_parser_lookup_name (parser, identifier,
11431 /*is_namespace=*/false,
11432 check_dependency_p);
11437 /* Try a template-id. */
11438 decl = cp_parser_template_id (parser, template_keyword_p,
11439 check_dependency_p);
11440 if (decl == error_mark_node)
11441 return error_mark_node;
11444 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11446 /* If this is a typename, create a TYPENAME_TYPE. */
11447 if (typename_p && decl != error_mark_node)
11448 decl = TYPE_NAME (make_typename_type (scope, decl,
11451 /* Check to see that it is really the name of a class. */
11452 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11453 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11454 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11455 /* Situations like this:
11457 template <typename T> struct A {
11458 typename T::template X<int>::I i;
11461 are problematic. Is `T::template X<int>' a class-name? The
11462 standard does not seem to be definitive, but there is no other
11463 valid interpretation of the following `::'. Therefore, those
11464 names are considered class-names. */
11465 decl = TYPE_NAME (make_typename_type (scope, decl,
11466 tf_error | tf_parsing));
11467 else if (decl == error_mark_node
11468 || TREE_CODE (decl) != TYPE_DECL
11469 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11471 cp_parser_error (parser, "expected class-name");
11472 return error_mark_node;
11478 /* Parse a class-specifier.
11481 class-head { member-specification [opt] }
11483 Returns the TREE_TYPE representing the class. */
11486 cp_parser_class_specifier (parser)
11491 tree attributes = NULL_TREE;
11492 int has_trailing_semicolon;
11493 bool nested_name_specifier_p;
11494 unsigned saved_num_template_parameter_lists;
11496 push_deferring_access_checks (false);
11498 /* Parse the class-head. */
11499 type = cp_parser_class_head (parser,
11500 &nested_name_specifier_p);
11501 /* If the class-head was a semantic disaster, skip the entire body
11505 cp_parser_skip_to_end_of_block_or_statement (parser);
11506 pop_deferring_access_checks ();
11507 return error_mark_node;
11510 /* Look for the `{'. */
11511 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11513 pop_deferring_access_checks ();
11514 return error_mark_node;
11517 /* Issue an error message if type-definitions are forbidden here. */
11518 cp_parser_check_type_definition (parser);
11519 /* Remember that we are defining one more class. */
11520 ++parser->num_classes_being_defined;
11521 /* Inside the class, surrounding template-parameter-lists do not
11523 saved_num_template_parameter_lists
11524 = parser->num_template_parameter_lists;
11525 parser->num_template_parameter_lists = 0;
11526 /* Start the class. */
11527 type = begin_class_definition (type);
11528 if (type == error_mark_node)
11529 /* If the type is erroneous, skip the entire body of the class. */
11530 cp_parser_skip_to_closing_brace (parser);
11532 /* Parse the member-specification. */
11533 cp_parser_member_specification_opt (parser);
11534 /* Look for the trailing `}'. */
11535 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11536 /* We get better error messages by noticing a common problem: a
11537 missing trailing `;'. */
11538 token = cp_lexer_peek_token (parser->lexer);
11539 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11540 /* Look for attributes to apply to this class. */
11541 if (cp_parser_allow_gnu_extensions_p (parser))
11542 attributes = cp_parser_attributes_opt (parser);
11543 /* Finish the class definition. */
11544 type = finish_class_definition (type,
11546 has_trailing_semicolon,
11547 nested_name_specifier_p);
11548 /* If this class is not itself within the scope of another class,
11549 then we need to parse the bodies of all of the queued function
11550 definitions. Note that the queued functions defined in a class
11551 are not always processed immediately following the
11552 class-specifier for that class. Consider:
11555 struct B { void f() { sizeof (A); } };
11558 If `f' were processed before the processing of `A' were
11559 completed, there would be no way to compute the size of `A'.
11560 Note that the nesting we are interested in here is lexical --
11561 not the semantic nesting given by TYPE_CONTEXT. In particular,
11564 struct A { struct B; };
11565 struct A::B { void f() { } };
11567 there is no need to delay the parsing of `A::B::f'. */
11568 if (--parser->num_classes_being_defined == 0)
11570 tree last_scope = NULL_TREE;
11574 /* Reverse the queue, so that we process it in the order the
11575 functions were declared. */
11576 TREE_VALUE (parser->unparsed_functions_queues)
11577 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11578 /* In a first pass, parse default arguments to the functions.
11579 Then, in a second pass, parse the bodies of the functions.
11580 This two-phased approach handles cases like:
11588 for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11590 queue_entry = TREE_CHAIN (queue_entry))
11592 fn = TREE_VALUE (queue_entry);
11593 if (DECL_FUNCTION_TEMPLATE_P (fn))
11594 fn = DECL_TEMPLATE_RESULT (fn);
11595 /* Make sure that any template parameters are in scope. */
11596 maybe_begin_member_template_processing (fn);
11597 /* If there are default arguments that have not yet been processed,
11598 take care of them now. */
11599 cp_parser_late_parsing_default_args (parser, fn);
11600 /* Remove any template parameters from the symbol table. */
11601 maybe_end_member_template_processing ();
11603 /* Now parse the body of the functions. */
11604 while (TREE_VALUE (parser->unparsed_functions_queues))
11607 /* Figure out which function we need to process. */
11608 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11609 fn = TREE_VALUE (queue_entry);
11611 /* Parse the function. */
11612 cp_parser_late_parsing_for_member (parser, fn);
11614 TREE_VALUE (parser->unparsed_functions_queues)
11615 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11618 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11619 more time than we have popped, so me must pop here. */
11621 pop_scope (last_scope);
11624 /* Put back any saved access checks. */
11625 pop_deferring_access_checks ();
11627 /* Restore the count of active template-parameter-lists. */
11628 parser->num_template_parameter_lists
11629 = saved_num_template_parameter_lists;
11634 /* Parse a class-head.
11637 class-key identifier [opt] base-clause [opt]
11638 class-key nested-name-specifier identifier base-clause [opt]
11639 class-key nested-name-specifier [opt] template-id
11643 class-key attributes identifier [opt] base-clause [opt]
11644 class-key attributes nested-name-specifier identifier base-clause [opt]
11645 class-key attributes nested-name-specifier [opt] template-id
11648 Returns the TYPE of the indicated class. Sets
11649 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11650 involving a nested-name-specifier was used, and FALSE otherwise.
11652 Returns NULL_TREE if the class-head is syntactically valid, but
11653 semantically invalid in a way that means we should skip the entire
11654 body of the class. */
11657 cp_parser_class_head (parser,
11658 nested_name_specifier_p)
11660 bool *nested_name_specifier_p;
11663 tree nested_name_specifier;
11664 enum tag_types class_key;
11665 tree id = NULL_TREE;
11666 tree type = NULL_TREE;
11668 bool template_id_p = false;
11669 bool qualified_p = false;
11670 bool invalid_nested_name_p = false;
11671 unsigned num_templates;
11673 /* Assume no nested-name-specifier will be present. */
11674 *nested_name_specifier_p = false;
11675 /* Assume no template parameter lists will be used in defining the
11679 /* Look for the class-key. */
11680 class_key = cp_parser_class_key (parser);
11681 if (class_key == none_type)
11682 return error_mark_node;
11684 /* Parse the attributes. */
11685 attributes = cp_parser_attributes_opt (parser);
11687 /* If the next token is `::', that is invalid -- but sometimes
11688 people do try to write:
11692 Handle this gracefully by accepting the extra qualifier, and then
11693 issuing an error about it later if this really is a
11694 class-head. If it turns out just to be an elaborated type
11695 specifier, remain silent. */
11696 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11697 qualified_p = true;
11699 /* Determine the name of the class. Begin by looking for an
11700 optional nested-name-specifier. */
11701 nested_name_specifier
11702 = cp_parser_nested_name_specifier_opt (parser,
11703 /*typename_keyword_p=*/false,
11704 /*check_dependency_p=*/true,
11706 /* If there was a nested-name-specifier, then there *must* be an
11708 if (nested_name_specifier)
11710 /* Although the grammar says `identifier', it really means
11711 `class-name' or `template-name'. You are only allowed to
11712 define a class that has already been declared with this
11715 The proposed resolution for Core Issue 180 says that whever
11716 you see `class T::X' you should treat `X' as a type-name.
11718 It is OK to define an inaccessible class; for example:
11720 class A { class B; };
11723 So, we ask cp_parser_class_name not to check accessibility.
11725 We do not know if we will see a class-name, or a
11726 template-name. We look for a class-name first, in case the
11727 class-name is a template-id; if we looked for the
11728 template-name first we would stop after the template-name. */
11729 cp_parser_parse_tentatively (parser);
11730 type = cp_parser_class_name (parser,
11731 /*typename_keyword_p=*/false,
11732 /*template_keyword_p=*/false,
11734 /*check_access_p=*/false,
11735 /*check_dependency_p=*/false,
11736 /*class_head_p=*/true);
11737 /* If that didn't work, ignore the nested-name-specifier. */
11738 if (!cp_parser_parse_definitely (parser))
11740 invalid_nested_name_p = true;
11741 id = cp_parser_identifier (parser);
11742 if (id == error_mark_node)
11745 /* If we could not find a corresponding TYPE, treat this
11746 declaration like an unqualified declaration. */
11747 if (type == error_mark_node)
11748 nested_name_specifier = NULL_TREE;
11749 /* Otherwise, count the number of templates used in TYPE and its
11750 containing scopes. */
11755 for (scope = TREE_TYPE (type);
11756 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11757 scope = (TYPE_P (scope)
11758 ? TYPE_CONTEXT (scope)
11759 : DECL_CONTEXT (scope)))
11761 && CLASS_TYPE_P (scope)
11762 && CLASSTYPE_TEMPLATE_INFO (scope)
11763 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11764 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11768 /* Otherwise, the identifier is optional. */
11771 /* We don't know whether what comes next is a template-id,
11772 an identifier, or nothing at all. */
11773 cp_parser_parse_tentatively (parser);
11774 /* Check for a template-id. */
11775 id = cp_parser_template_id (parser,
11776 /*template_keyword_p=*/false,
11777 /*check_dependency_p=*/true);
11778 /* If that didn't work, it could still be an identifier. */
11779 if (!cp_parser_parse_definitely (parser))
11781 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11782 id = cp_parser_identifier (parser);
11788 template_id_p = true;
11793 /* If it's not a `:' or a `{' then we can't really be looking at a
11794 class-head, since a class-head only appears as part of a
11795 class-specifier. We have to detect this situation before calling
11796 xref_tag, since that has irreversible side-effects. */
11797 if (!cp_parser_next_token_starts_class_definition_p (parser))
11799 cp_parser_error (parser, "expected `{' or `:'");
11800 return error_mark_node;
11803 /* At this point, we're going ahead with the class-specifier, even
11804 if some other problem occurs. */
11805 cp_parser_commit_to_tentative_parse (parser);
11806 /* Issue the error about the overly-qualified name now. */
11808 cp_parser_error (parser,
11809 "global qualification of class name is invalid");
11810 else if (invalid_nested_name_p)
11811 cp_parser_error (parser,
11812 "qualified name does not name a class");
11813 /* Make sure that the right number of template parameters were
11815 if (!cp_parser_check_template_parameters (parser, num_templates))
11816 /* If something went wrong, there is no point in even trying to
11817 process the class-definition. */
11820 /* Look up the type. */
11823 type = TREE_TYPE (id);
11824 maybe_process_partial_specialization (type);
11826 else if (!nested_name_specifier)
11828 /* If the class was unnamed, create a dummy name. */
11830 id = make_anon_name ();
11831 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11840 template <typename T> struct S { struct T };
11841 template <typename T> struct S::T { };
11843 we will get a TYPENAME_TYPE when processing the definition of
11844 `S::T'. We need to resolve it to the actual type before we
11845 try to define it. */
11846 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11848 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11849 if (type != error_mark_node)
11850 type = TYPE_NAME (type);
11853 maybe_process_partial_specialization (TREE_TYPE (type));
11854 class_type = current_class_type;
11855 type = TREE_TYPE (handle_class_head (class_key,
11856 nested_name_specifier,
11861 if (type != error_mark_node)
11863 if (!class_type && TYPE_CONTEXT (type))
11864 *nested_name_specifier_p = true;
11865 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11867 *nested_name_specifier_p = true;
11870 /* Indicate whether this class was declared as a `class' or as a
11872 if (TREE_CODE (type) == RECORD_TYPE)
11873 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11874 cp_parser_check_class_key (class_key, type);
11876 /* Enter the scope containing the class; the names of base classes
11877 should be looked up in that context. For example, given:
11879 struct A { struct B {}; struct C; };
11880 struct A::C : B {};
11883 if (nested_name_specifier)
11884 push_scope (nested_name_specifier);
11885 /* Now, look for the base-clause. */
11886 token = cp_lexer_peek_token (parser->lexer);
11887 if (token->type == CPP_COLON)
11891 /* Get the list of base-classes. */
11892 bases = cp_parser_base_clause (parser);
11893 /* Process them. */
11894 xref_basetypes (type, bases);
11896 /* Leave the scope given by the nested-name-specifier. We will
11897 enter the class scope itself while processing the members. */
11898 if (nested_name_specifier)
11899 pop_scope (nested_name_specifier);
11904 /* Parse a class-key.
11911 Returns the kind of class-key specified, or none_type to indicate
11914 static enum tag_types
11915 cp_parser_class_key (parser)
11919 enum tag_types tag_type;
11921 /* Look for the class-key. */
11922 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11926 /* Check to see if the TOKEN is a class-key. */
11927 tag_type = cp_parser_token_is_class_key (token);
11929 cp_parser_error (parser, "expected class-key");
11933 /* Parse an (optional) member-specification.
11935 member-specification:
11936 member-declaration member-specification [opt]
11937 access-specifier : member-specification [opt] */
11940 cp_parser_member_specification_opt (parser)
11948 /* Peek at the next token. */
11949 token = cp_lexer_peek_token (parser->lexer);
11950 /* If it's a `}', or EOF then we've seen all the members. */
11951 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11954 /* See if this token is a keyword. */
11955 keyword = token->keyword;
11959 case RID_PROTECTED:
11961 /* Consume the access-specifier. */
11962 cp_lexer_consume_token (parser->lexer);
11963 /* Remember which access-specifier is active. */
11964 current_access_specifier = token->value;
11965 /* Look for the `:'. */
11966 cp_parser_require (parser, CPP_COLON, "`:'");
11970 /* Otherwise, the next construction must be a
11971 member-declaration. */
11972 cp_parser_member_declaration (parser);
11977 /* Parse a member-declaration.
11979 member-declaration:
11980 decl-specifier-seq [opt] member-declarator-list [opt] ;
11981 function-definition ; [opt]
11982 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11984 template-declaration
11986 member-declarator-list:
11988 member-declarator-list , member-declarator
11991 declarator pure-specifier [opt]
11992 declarator constant-initializer [opt]
11993 identifier [opt] : constant-expression
11997 member-declaration:
11998 __extension__ member-declaration
12001 declarator attributes [opt] pure-specifier [opt]
12002 declarator attributes [opt] constant-initializer [opt]
12003 identifier [opt] attributes [opt] : constant-expression */
12006 cp_parser_member_declaration (parser)
12009 tree decl_specifiers;
12010 tree prefix_attributes;
12012 bool declares_class_or_enum;
12015 int saved_pedantic;
12017 /* Check for the `__extension__' keyword. */
12018 if (cp_parser_extension_opt (parser, &saved_pedantic))
12021 cp_parser_member_declaration (parser);
12022 /* Restore the old value of the PEDANTIC flag. */
12023 pedantic = saved_pedantic;
12028 /* Check for a template-declaration. */
12029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12031 /* Parse the template-declaration. */
12032 cp_parser_template_declaration (parser, /*member_p=*/true);
12037 /* Check for a using-declaration. */
12038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12040 /* Parse the using-declaration. */
12041 cp_parser_using_declaration (parser);
12046 /* We can't tell whether we're looking at a declaration or a
12047 function-definition. */
12048 cp_parser_parse_tentatively (parser);
12050 /* Parse the decl-specifier-seq. */
12052 = cp_parser_decl_specifier_seq (parser,
12053 CP_PARSER_FLAGS_OPTIONAL,
12054 &prefix_attributes,
12055 &declares_class_or_enum);
12056 /* If there is no declarator, then the decl-specifier-seq should
12058 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12060 /* If there was no decl-specifier-seq, and the next token is a
12061 `;', then we have something like:
12067 Each member-declaration shall declare at least one member
12068 name of the class. */
12069 if (!decl_specifiers)
12072 pedwarn ("extra semicolon");
12078 /* See if this declaration is a friend. */
12079 friend_p = cp_parser_friend_p (decl_specifiers);
12080 /* If there were decl-specifiers, check to see if there was
12081 a class-declaration. */
12082 type = check_tag_decl (decl_specifiers);
12083 /* Nested classes have already been added to the class, but
12084 a `friend' needs to be explicitly registered. */
12087 /* If the `friend' keyword was present, the friend must
12088 be introduced with a class-key. */
12089 if (!declares_class_or_enum)
12090 error ("a class-key must be used when declaring a friend");
12093 template <typename T> struct A {
12094 friend struct A<T>::B;
12097 A<T>::B will be represented by a TYPENAME_TYPE, and
12098 therefore not recognized by check_tag_decl. */
12103 for (specifier = decl_specifiers;
12105 specifier = TREE_CHAIN (specifier))
12107 tree s = TREE_VALUE (specifier);
12109 if (TREE_CODE (s) == IDENTIFIER_NODE
12110 && IDENTIFIER_GLOBAL_VALUE (s))
12111 type = IDENTIFIER_GLOBAL_VALUE (s);
12112 if (TREE_CODE (s) == TYPE_DECL)
12122 error ("friend declaration does not name a class or "
12125 make_friend_class (current_class_type, type);
12127 /* If there is no TYPE, an error message will already have
12131 /* An anonymous aggregate has to be handled specially; such
12132 a declaration really declares a data member (with a
12133 particular type), as opposed to a nested class. */
12134 else if (ANON_AGGR_TYPE_P (type))
12136 /* Remove constructors and such from TYPE, now that we
12137 know it is an anoymous aggregate. */
12138 fixup_anonymous_aggr (type);
12139 /* And make the corresponding data member. */
12140 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12141 /* Add it to the class. */
12142 finish_member_declaration (decl);
12148 /* See if these declarations will be friends. */
12149 friend_p = cp_parser_friend_p (decl_specifiers);
12151 /* Keep going until we hit the `;' at the end of the
12153 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12155 tree attributes = NULL_TREE;
12156 tree first_attribute;
12158 /* Peek at the next token. */
12159 token = cp_lexer_peek_token (parser->lexer);
12161 /* Check for a bitfield declaration. */
12162 if (token->type == CPP_COLON
12163 || (token->type == CPP_NAME
12164 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12170 /* Get the name of the bitfield. Note that we cannot just
12171 check TOKEN here because it may have been invalidated by
12172 the call to cp_lexer_peek_nth_token above. */
12173 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12174 identifier = cp_parser_identifier (parser);
12176 identifier = NULL_TREE;
12178 /* Consume the `:' token. */
12179 cp_lexer_consume_token (parser->lexer);
12180 /* Get the width of the bitfield. */
12181 width = cp_parser_constant_expression (parser);
12183 /* Look for attributes that apply to the bitfield. */
12184 attributes = cp_parser_attributes_opt (parser);
12185 /* Remember which attributes are prefix attributes and
12187 first_attribute = attributes;
12188 /* Combine the attributes. */
12189 attributes = chainon (prefix_attributes, attributes);
12191 /* Create the bitfield declaration. */
12192 decl = grokbitfield (identifier,
12195 /* Apply the attributes. */
12196 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12202 tree asm_specification;
12203 bool ctor_dtor_or_conv_p;
12205 /* Parse the declarator. */
12207 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12208 &ctor_dtor_or_conv_p);
12210 /* If something went wrong parsing the declarator, make sure
12211 that we at least consume some tokens. */
12212 if (declarator == error_mark_node)
12214 /* Skip to the end of the statement. */
12215 cp_parser_skip_to_end_of_statement (parser);
12219 /* Look for an asm-specification. */
12220 asm_specification = cp_parser_asm_specification_opt (parser);
12221 /* Look for attributes that apply to the declaration. */
12222 attributes = cp_parser_attributes_opt (parser);
12223 /* Remember which attributes are prefix attributes and
12225 first_attribute = attributes;
12226 /* Combine the attributes. */
12227 attributes = chainon (prefix_attributes, attributes);
12229 /* If it's an `=', then we have a constant-initializer or a
12230 pure-specifier. It is not correct to parse the
12231 initializer before registering the member declaration
12232 since the member declaration should be in scope while
12233 its initializer is processed. However, the rest of the
12234 front end does not yet provide an interface that allows
12235 us to handle this correctly. */
12236 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12240 A pure-specifier shall be used only in the declaration of
12241 a virtual function.
12243 A member-declarator can contain a constant-initializer
12244 only if it declares a static member of integral or
12247 Therefore, if the DECLARATOR is for a function, we look
12248 for a pure-specifier; otherwise, we look for a
12249 constant-initializer. When we call `grokfield', it will
12250 perform more stringent semantics checks. */
12251 if (TREE_CODE (declarator) == CALL_EXPR)
12252 initializer = cp_parser_pure_specifier (parser);
12255 /* This declaration cannot be a function
12257 cp_parser_commit_to_tentative_parse (parser);
12258 /* Parse the initializer. */
12259 initializer = cp_parser_constant_initializer (parser);
12262 /* Otherwise, there is no initializer. */
12264 initializer = NULL_TREE;
12266 /* See if we are probably looking at a function
12267 definition. We are certainly not looking at at a
12268 member-declarator. Calling `grokfield' has
12269 side-effects, so we must not do it unless we are sure
12270 that we are looking at a member-declarator. */
12271 if (cp_parser_token_starts_function_definition_p
12272 (cp_lexer_peek_token (parser->lexer)))
12273 decl = error_mark_node;
12275 /* Create the declaration. */
12276 decl = grokfield (declarator,
12283 /* Reset PREFIX_ATTRIBUTES. */
12284 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12285 attributes = TREE_CHAIN (attributes);
12287 TREE_CHAIN (attributes) = NULL_TREE;
12289 /* If there is any qualification still in effect, clear it
12290 now; we will be starting fresh with the next declarator. */
12291 parser->scope = NULL_TREE;
12292 parser->qualifying_scope = NULL_TREE;
12293 parser->object_scope = NULL_TREE;
12294 /* If it's a `,', then there are more declarators. */
12295 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12296 cp_lexer_consume_token (parser->lexer);
12297 /* If the next token isn't a `;', then we have a parse error. */
12298 else if (cp_lexer_next_token_is_not (parser->lexer,
12301 cp_parser_error (parser, "expected `;'");
12302 /* Skip tokens until we find a `;' */
12303 cp_parser_skip_to_end_of_statement (parser);
12310 /* Add DECL to the list of members. */
12312 finish_member_declaration (decl);
12314 /* If DECL is a function, we must return
12315 to parse it later. (Even though there is no definition,
12316 there might be default arguments that need handling.) */
12317 if (TREE_CODE (decl) == FUNCTION_DECL)
12318 TREE_VALUE (parser->unparsed_functions_queues)
12319 = tree_cons (NULL_TREE, decl,
12320 TREE_VALUE (parser->unparsed_functions_queues));
12325 /* If everything went well, look for the `;'. */
12326 if (cp_parser_parse_definitely (parser))
12328 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12332 /* Parse the function-definition. */
12333 decl = cp_parser_function_definition (parser, &friend_p);
12334 /* If the member was not a friend, declare it here. */
12336 finish_member_declaration (decl);
12337 /* Peek at the next token. */
12338 token = cp_lexer_peek_token (parser->lexer);
12339 /* If the next token is a semicolon, consume it. */
12340 if (token->type == CPP_SEMICOLON)
12341 cp_lexer_consume_token (parser->lexer);
12344 /* Parse a pure-specifier.
12349 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12350 Otherwiser, ERROR_MARK_NODE is returned. */
12353 cp_parser_pure_specifier (parser)
12358 /* Look for the `=' token. */
12359 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12360 return error_mark_node;
12361 /* Look for the `0' token. */
12362 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12363 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12364 to get information from the lexer about how the number was
12365 spelled in order to fix this problem. */
12366 if (!token || !integer_zerop (token->value))
12367 return error_mark_node;
12369 return integer_zero_node;
12372 /* Parse a constant-initializer.
12374 constant-initializer:
12375 = constant-expression
12377 Returns a representation of the constant-expression. */
12380 cp_parser_constant_initializer (parser)
12383 /* Look for the `=' token. */
12384 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12385 return error_mark_node;
12387 /* It is invalid to write:
12389 struct S { static const int i = { 7 }; };
12392 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12394 cp_parser_error (parser,
12395 "a brace-enclosed initializer is not allowed here");
12396 /* Consume the opening brace. */
12397 cp_lexer_consume_token (parser->lexer);
12398 /* Skip the initializer. */
12399 cp_parser_skip_to_closing_brace (parser);
12400 /* Look for the trailing `}'. */
12401 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12403 return error_mark_node;
12406 return cp_parser_constant_expression (parser);
12409 /* Derived classes [gram.class.derived] */
12411 /* Parse a base-clause.
12414 : base-specifier-list
12416 base-specifier-list:
12418 base-specifier-list , base-specifier
12420 Returns a TREE_LIST representing the base-classes, in the order in
12421 which they were declared. The representation of each node is as
12422 described by cp_parser_base_specifier.
12424 In the case that no bases are specified, this function will return
12425 NULL_TREE, not ERROR_MARK_NODE. */
12428 cp_parser_base_clause (parser)
12431 tree bases = NULL_TREE;
12433 /* Look for the `:' that begins the list. */
12434 cp_parser_require (parser, CPP_COLON, "`:'");
12436 /* Scan the base-specifier-list. */
12442 /* Look for the base-specifier. */
12443 base = cp_parser_base_specifier (parser);
12444 /* Add BASE to the front of the list. */
12445 if (base != error_mark_node)
12447 TREE_CHAIN (base) = bases;
12450 /* Peek at the next token. */
12451 token = cp_lexer_peek_token (parser->lexer);
12452 /* If it's not a comma, then the list is complete. */
12453 if (token->type != CPP_COMMA)
12455 /* Consume the `,'. */
12456 cp_lexer_consume_token (parser->lexer);
12459 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12460 base class had a qualified name. However, the next name that
12461 appears is certainly not qualified. */
12462 parser->scope = NULL_TREE;
12463 parser->qualifying_scope = NULL_TREE;
12464 parser->object_scope = NULL_TREE;
12466 return nreverse (bases);
12469 /* Parse a base-specifier.
12472 :: [opt] nested-name-specifier [opt] class-name
12473 virtual access-specifier [opt] :: [opt] nested-name-specifier
12475 access-specifier virtual [opt] :: [opt] nested-name-specifier
12478 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12479 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12480 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12481 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12484 cp_parser_base_specifier (parser)
12489 bool virtual_p = false;
12490 bool duplicate_virtual_error_issued_p = false;
12491 bool duplicate_access_error_issued_p = false;
12492 bool class_scope_p;
12493 access_kind access = ak_none;
12497 /* Process the optional `virtual' and `access-specifier'. */
12500 /* Peek at the next token. */
12501 token = cp_lexer_peek_token (parser->lexer);
12502 /* Process `virtual'. */
12503 switch (token->keyword)
12506 /* If `virtual' appears more than once, issue an error. */
12507 if (virtual_p && !duplicate_virtual_error_issued_p)
12509 cp_parser_error (parser,
12510 "`virtual' specified more than once in base-specified");
12511 duplicate_virtual_error_issued_p = true;
12516 /* Consume the `virtual' token. */
12517 cp_lexer_consume_token (parser->lexer);
12522 case RID_PROTECTED:
12524 /* If more than one access specifier appears, issue an
12526 if (access != ak_none && !duplicate_access_error_issued_p)
12528 cp_parser_error (parser,
12529 "more than one access specifier in base-specified");
12530 duplicate_access_error_issued_p = true;
12533 access = ((access_kind)
12534 tree_low_cst (ridpointers[(int) token->keyword],
12537 /* Consume the access-specifier. */
12538 cp_lexer_consume_token (parser->lexer);
12548 /* Map `virtual_p' and `access' onto one of the access
12554 access_node = access_default_node;
12557 access_node = access_public_node;
12560 access_node = access_protected_node;
12563 access_node = access_private_node;
12572 access_node = access_default_virtual_node;
12575 access_node = access_public_virtual_node;
12578 access_node = access_protected_virtual_node;
12581 access_node = access_private_virtual_node;
12587 /* Look for the optional `::' operator. */
12588 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12589 /* Look for the nested-name-specifier. The simplest way to
12594 The keyword `typename' is not permitted in a base-specifier or
12595 mem-initializer; in these contexts a qualified name that
12596 depends on a template-parameter is implicitly assumed to be a
12599 is to pretend that we have seen the `typename' keyword at this
12601 cp_parser_nested_name_specifier_opt (parser,
12602 /*typename_keyword_p=*/true,
12603 /*check_dependency_p=*/true,
12605 /* If the base class is given by a qualified name, assume that names
12606 we see are type names or templates, as appropriate. */
12607 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12608 /* Finally, look for the class-name. */
12609 type = cp_parser_class_name (parser,
12613 /*check_access=*/true,
12614 /*check_dependency_p=*/true,
12615 /*class_head_p=*/false);
12617 if (type == error_mark_node)
12618 return error_mark_node;
12620 return finish_base_specifier (access_node, TREE_TYPE (type));
12623 /* Exception handling [gram.exception] */
12625 /* Parse an (optional) exception-specification.
12627 exception-specification:
12628 throw ( type-id-list [opt] )
12630 Returns a TREE_LIST representing the exception-specification. The
12631 TREE_VALUE of each node is a type. */
12634 cp_parser_exception_specification_opt (parser)
12640 /* Peek at the next token. */
12641 token = cp_lexer_peek_token (parser->lexer);
12642 /* If it's not `throw', then there's no exception-specification. */
12643 if (!cp_parser_is_keyword (token, RID_THROW))
12646 /* Consume the `throw'. */
12647 cp_lexer_consume_token (parser->lexer);
12649 /* Look for the `('. */
12650 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12652 /* Peek at the next token. */
12653 token = cp_lexer_peek_token (parser->lexer);
12654 /* If it's not a `)', then there is a type-id-list. */
12655 if (token->type != CPP_CLOSE_PAREN)
12657 const char *saved_message;
12659 /* Types may not be defined in an exception-specification. */
12660 saved_message = parser->type_definition_forbidden_message;
12661 parser->type_definition_forbidden_message
12662 = "types may not be defined in an exception-specification";
12663 /* Parse the type-id-list. */
12664 type_id_list = cp_parser_type_id_list (parser);
12665 /* Restore the saved message. */
12666 parser->type_definition_forbidden_message = saved_message;
12669 type_id_list = empty_except_spec;
12671 /* Look for the `)'. */
12672 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12674 return type_id_list;
12677 /* Parse an (optional) type-id-list.
12681 type-id-list , type-id
12683 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12684 in the order that the types were presented. */
12687 cp_parser_type_id_list (parser)
12690 tree types = NULL_TREE;
12697 /* Get the next type-id. */
12698 type = cp_parser_type_id (parser);
12699 /* Add it to the list. */
12700 types = add_exception_specifier (types, type, /*complain=*/1);
12701 /* Peek at the next token. */
12702 token = cp_lexer_peek_token (parser->lexer);
12703 /* If it is not a `,', we are done. */
12704 if (token->type != CPP_COMMA)
12706 /* Consume the `,'. */
12707 cp_lexer_consume_token (parser->lexer);
12710 return nreverse (types);
12713 /* Parse a try-block.
12716 try compound-statement handler-seq */
12719 cp_parser_try_block (parser)
12724 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12725 try_block = begin_try_block ();
12726 cp_parser_compound_statement (parser);
12727 finish_try_block (try_block);
12728 cp_parser_handler_seq (parser);
12729 finish_handler_sequence (try_block);
12734 /* Parse a function-try-block.
12736 function-try-block:
12737 try ctor-initializer [opt] function-body handler-seq */
12740 cp_parser_function_try_block (parser)
12744 bool ctor_initializer_p;
12746 /* Look for the `try' keyword. */
12747 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12749 /* Let the rest of the front-end know where we are. */
12750 try_block = begin_function_try_block ();
12751 /* Parse the function-body. */
12753 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12754 /* We're done with the `try' part. */
12755 finish_function_try_block (try_block);
12756 /* Parse the handlers. */
12757 cp_parser_handler_seq (parser);
12758 /* We're done with the handlers. */
12759 finish_function_handler_sequence (try_block);
12761 return ctor_initializer_p;
12764 /* Parse a handler-seq.
12767 handler handler-seq [opt] */
12770 cp_parser_handler_seq (parser)
12777 /* Parse the handler. */
12778 cp_parser_handler (parser);
12779 /* Peek at the next token. */
12780 token = cp_lexer_peek_token (parser->lexer);
12781 /* If it's not `catch' then there are no more handlers. */
12782 if (!cp_parser_is_keyword (token, RID_CATCH))
12787 /* Parse a handler.
12790 catch ( exception-declaration ) compound-statement */
12793 cp_parser_handler (parser)
12799 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12800 handler = begin_handler ();
12801 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12802 declaration = cp_parser_exception_declaration (parser);
12803 finish_handler_parms (declaration, handler);
12804 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12805 cp_parser_compound_statement (parser);
12806 finish_handler (handler);
12809 /* Parse an exception-declaration.
12811 exception-declaration:
12812 type-specifier-seq declarator
12813 type-specifier-seq abstract-declarator
12817 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12818 ellipsis variant is used. */
12821 cp_parser_exception_declaration (parser)
12824 tree type_specifiers;
12826 const char *saved_message;
12828 /* If it's an ellipsis, it's easy to handle. */
12829 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12831 /* Consume the `...' token. */
12832 cp_lexer_consume_token (parser->lexer);
12836 /* Types may not be defined in exception-declarations. */
12837 saved_message = parser->type_definition_forbidden_message;
12838 parser->type_definition_forbidden_message
12839 = "types may not be defined in exception-declarations";
12841 /* Parse the type-specifier-seq. */
12842 type_specifiers = cp_parser_type_specifier_seq (parser);
12843 /* If it's a `)', then there is no declarator. */
12844 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12845 declarator = NULL_TREE;
12847 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12848 /*ctor_dtor_or_conv_p=*/NULL);
12850 /* Restore the saved message. */
12851 parser->type_definition_forbidden_message = saved_message;
12853 return start_handler_parms (type_specifiers, declarator);
12856 /* Parse a throw-expression.
12859 throw assignment-expresion [opt]
12861 Returns a THROW_EXPR representing the throw-expression. */
12864 cp_parser_throw_expression (parser)
12869 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12870 /* We can't be sure if there is an assignment-expression or not. */
12871 cp_parser_parse_tentatively (parser);
12873 expression = cp_parser_assignment_expression (parser);
12874 /* If it didn't work, this is just a rethrow. */
12875 if (!cp_parser_parse_definitely (parser))
12876 expression = NULL_TREE;
12878 return build_throw (expression);
12881 /* GNU Extensions */
12883 /* Parse an (optional) asm-specification.
12886 asm ( string-literal )
12888 If the asm-specification is present, returns a STRING_CST
12889 corresponding to the string-literal. Otherwise, returns
12893 cp_parser_asm_specification_opt (parser)
12897 tree asm_specification;
12899 /* Peek at the next token. */
12900 token = cp_lexer_peek_token (parser->lexer);
12901 /* If the next token isn't the `asm' keyword, then there's no
12902 asm-specification. */
12903 if (!cp_parser_is_keyword (token, RID_ASM))
12906 /* Consume the `asm' token. */
12907 cp_lexer_consume_token (parser->lexer);
12908 /* Look for the `('. */
12909 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12911 /* Look for the string-literal. */
12912 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12914 asm_specification = token->value;
12916 asm_specification = NULL_TREE;
12918 /* Look for the `)'. */
12919 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12921 return asm_specification;
12924 /* Parse an asm-operand-list.
12928 asm-operand-list , asm-operand
12931 string-literal ( expression )
12932 [ string-literal ] string-literal ( expression )
12934 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12935 each node is the expression. The TREE_PURPOSE is itself a
12936 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12937 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12938 is a STRING_CST for the string literal before the parenthesis. */
12941 cp_parser_asm_operand_list (parser)
12944 tree asm_operands = NULL_TREE;
12948 tree string_literal;
12953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12955 /* Consume the `[' token. */
12956 cp_lexer_consume_token (parser->lexer);
12957 /* Read the operand name. */
12958 name = cp_parser_identifier (parser);
12959 if (name != error_mark_node)
12960 name = build_string (IDENTIFIER_LENGTH (name),
12961 IDENTIFIER_POINTER (name));
12962 /* Look for the closing `]'. */
12963 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12967 /* Look for the string-literal. */
12968 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12969 string_literal = token ? token->value : error_mark_node;
12970 /* Look for the `('. */
12971 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12972 /* Parse the expression. */
12973 expression = cp_parser_expression (parser);
12974 /* Look for the `)'. */
12975 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12976 /* Add this operand to the list. */
12977 asm_operands = tree_cons (build_tree_list (name, string_literal),
12980 /* If the next token is not a `,', there are no more
12982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12984 /* Consume the `,'. */
12985 cp_lexer_consume_token (parser->lexer);
12988 return nreverse (asm_operands);
12991 /* Parse an asm-clobber-list.
12995 asm-clobber-list , string-literal
12997 Returns a TREE_LIST, indicating the clobbers in the order that they
12998 appeared. The TREE_VALUE of each node is a STRING_CST. */
13001 cp_parser_asm_clobber_list (parser)
13004 tree clobbers = NULL_TREE;
13009 tree string_literal;
13011 /* Look for the string literal. */
13012 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13013 string_literal = token ? token->value : error_mark_node;
13014 /* Add it to the list. */
13015 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13016 /* If the next token is not a `,', then the list is
13018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13020 /* Consume the `,' token. */
13021 cp_lexer_consume_token (parser->lexer);
13027 /* Parse an (optional) series of attributes.
13030 attributes attribute
13033 __attribute__ (( attribute-list [opt] ))
13035 The return value is as for cp_parser_attribute_list. */
13038 cp_parser_attributes_opt (parser)
13041 tree attributes = NULL_TREE;
13046 tree attribute_list;
13048 /* Peek at the next token. */
13049 token = cp_lexer_peek_token (parser->lexer);
13050 /* If it's not `__attribute__', then we're done. */
13051 if (token->keyword != RID_ATTRIBUTE)
13054 /* Consume the `__attribute__' keyword. */
13055 cp_lexer_consume_token (parser->lexer);
13056 /* Look for the two `(' tokens. */
13057 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13058 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13060 /* Peek at the next token. */
13061 token = cp_lexer_peek_token (parser->lexer);
13062 if (token->type != CPP_CLOSE_PAREN)
13063 /* Parse the attribute-list. */
13064 attribute_list = cp_parser_attribute_list (parser);
13066 /* If the next token is a `)', then there is no attribute
13068 attribute_list = NULL;
13070 /* Look for the two `)' tokens. */
13071 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13072 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13074 /* Add these new attributes to the list. */
13075 attributes = chainon (attributes, attribute_list);
13081 /* Parse an attribute-list.
13085 attribute-list , attribute
13089 identifier ( identifier )
13090 identifier ( identifier , expression-list )
13091 identifier ( expression-list )
13093 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13094 TREE_PURPOSE of each node is the identifier indicating which
13095 attribute is in use. The TREE_VALUE represents the arguments, if
13099 cp_parser_attribute_list (parser)
13102 tree attribute_list = NULL_TREE;
13110 /* Look for the identifier. We also allow keywords here; for
13111 example `__attribute__ ((const))' is legal. */
13112 token = cp_lexer_peek_token (parser->lexer);
13113 if (token->type != CPP_NAME
13114 && token->type != CPP_KEYWORD)
13115 return error_mark_node;
13116 /* Consume the token. */
13117 token = cp_lexer_consume_token (parser->lexer);
13119 /* Save away the identifier that indicates which attribute this is. */
13120 identifier = token->value;
13121 attribute = build_tree_list (identifier, NULL_TREE);
13123 /* Peek at the next token. */
13124 token = cp_lexer_peek_token (parser->lexer);
13125 /* If it's an `(', then parse the attribute arguments. */
13126 if (token->type == CPP_OPEN_PAREN)
13129 int arguments_allowed_p = 1;
13131 /* Consume the `('. */
13132 cp_lexer_consume_token (parser->lexer);
13133 /* Peek at the next token. */
13134 token = cp_lexer_peek_token (parser->lexer);
13135 /* Check to see if the next token is an identifier. */
13136 if (token->type == CPP_NAME)
13138 /* Save the identifier. */
13139 identifier = token->value;
13140 /* Consume the identifier. */
13141 cp_lexer_consume_token (parser->lexer);
13142 /* Peek at the next token. */
13143 token = cp_lexer_peek_token (parser->lexer);
13144 /* If the next token is a `,', then there are some other
13145 expressions as well. */
13146 if (token->type == CPP_COMMA)
13147 /* Consume the comma. */
13148 cp_lexer_consume_token (parser->lexer);
13150 arguments_allowed_p = 0;
13153 identifier = NULL_TREE;
13155 /* If there are arguments, parse them too. */
13156 if (arguments_allowed_p)
13157 arguments = cp_parser_expression_list (parser);
13159 arguments = NULL_TREE;
13161 /* Combine the identifier and the arguments. */
13163 arguments = tree_cons (NULL_TREE, identifier, arguments);
13165 /* Save the identifier and arguments away. */
13166 TREE_VALUE (attribute) = arguments;
13168 /* Look for the closing `)'. */
13169 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13172 /* Add this attribute to the list. */
13173 TREE_CHAIN (attribute) = attribute_list;
13174 attribute_list = attribute;
13176 /* Now, look for more attributes. */
13177 token = cp_lexer_peek_token (parser->lexer);
13178 /* If the next token isn't a `,', we're done. */
13179 if (token->type != CPP_COMMA)
13182 /* Consume the commma and keep going. */
13183 cp_lexer_consume_token (parser->lexer);
13186 /* We built up the list in reverse order. */
13187 return nreverse (attribute_list);
13190 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13191 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13192 current value of the PEDANTIC flag, regardless of whether or not
13193 the `__extension__' keyword is present. The caller is responsible
13194 for restoring the value of the PEDANTIC flag. */
13197 cp_parser_extension_opt (parser, saved_pedantic)
13199 int *saved_pedantic;
13201 /* Save the old value of the PEDANTIC flag. */
13202 *saved_pedantic = pedantic;
13204 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13206 /* Consume the `__extension__' token. */
13207 cp_lexer_consume_token (parser->lexer);
13208 /* We're not being pedantic while the `__extension__' keyword is
13218 /* Parse a label declaration.
13221 __label__ label-declarator-seq ;
13223 label-declarator-seq:
13224 identifier , label-declarator-seq
13228 cp_parser_label_declaration (parser)
13231 /* Look for the `__label__' keyword. */
13232 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13238 /* Look for an identifier. */
13239 identifier = cp_parser_identifier (parser);
13240 /* Declare it as a lobel. */
13241 finish_label_decl (identifier);
13242 /* If the next token is a `;', stop. */
13243 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13245 /* Look for the `,' separating the label declarations. */
13246 cp_parser_require (parser, CPP_COMMA, "`,'");
13249 /* Look for the final `;'. */
13250 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13253 /* Support Functions */
13255 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13256 NAME should have one of the representations used for an
13257 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13258 is returned. If PARSER->SCOPE is a dependent type, then a
13259 SCOPE_REF is returned.
13261 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13262 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13263 was formed. Abstractly, such entities should not be passed to this
13264 function, because they do not need to be looked up, but it is
13265 simpler to check for this special case here, rather than at the
13268 In cases not explicitly covered above, this function returns a
13269 DECL, OVERLOAD, or baselink representing the result of the lookup.
13270 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13273 If CHECK_ACCESS is TRUE, then access control is performed on the
13274 declaration to which the name resolves, and an error message is
13275 issued if the declaration is inaccessible.
13277 If IS_TYPE is TRUE, bindings that do not refer to types are
13280 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13283 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13287 cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13288 bool is_type, bool is_namespace, bool check_dependency)
13291 tree object_type = parser->context->object_type;
13293 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13294 no longer valid. Note that if we are parsing tentatively, and
13295 the parse fails, OBJECT_TYPE will be automatically restored. */
13296 parser->context->object_type = NULL_TREE;
13298 if (name == error_mark_node)
13299 return error_mark_node;
13301 /* A template-id has already been resolved; there is no lookup to
13303 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13305 if (BASELINK_P (name))
13307 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13308 == TEMPLATE_ID_EXPR),
13313 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13314 it should already have been checked to make sure that the name
13315 used matches the type being destroyed. */
13316 if (TREE_CODE (name) == BIT_NOT_EXPR)
13320 /* Figure out to which type this destructor applies. */
13322 type = parser->scope;
13323 else if (object_type)
13324 type = object_type;
13326 type = current_class_type;
13327 /* If that's not a class type, there is no destructor. */
13328 if (!type || !CLASS_TYPE_P (type))
13329 return error_mark_node;
13330 /* If it was a class type, return the destructor. */
13331 return CLASSTYPE_DESTRUCTORS (type);
13334 /* By this point, the NAME should be an ordinary identifier. If
13335 the id-expression was a qualified name, the qualifying scope is
13336 stored in PARSER->SCOPE at this point. */
13337 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13340 /* Perform the lookup. */
13343 bool dependent_type_p;
13345 if (parser->scope == error_mark_node)
13346 return error_mark_node;
13348 /* If the SCOPE is dependent, the lookup must be deferred until
13349 the template is instantiated -- unless we are explicitly
13350 looking up names in uninstantiated templates. Even then, we
13351 cannot look up the name if the scope is not a class type; it
13352 might, for example, be a template type parameter. */
13353 dependent_type_p = (TYPE_P (parser->scope)
13354 && !(parser->in_declarator_p
13355 && currently_open_class (parser->scope))
13356 && cp_parser_dependent_type_p (parser->scope));
13357 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13358 && dependent_type_p)
13361 decl = build_nt (SCOPE_REF, parser->scope, name);
13363 /* The resolution to Core Issue 180 says that `struct A::B'
13364 should be considered a type-name, even if `A' is
13366 decl = TYPE_NAME (make_typename_type (parser->scope,
13372 /* If PARSER->SCOPE is a dependent type, then it must be a
13373 class type, and we must not be checking dependencies;
13374 otherwise, we would have processed this lookup above. So
13375 that PARSER->SCOPE is not considered a dependent base by
13376 lookup_member, we must enter the scope here. */
13377 if (dependent_type_p)
13378 push_scope (parser->scope);
13379 /* If the PARSER->SCOPE is a a template specialization, it
13380 may be instantiated during name lookup. In that case,
13381 errors may be issued. Even if we rollback the current
13382 tentative parse, those errors are valid. */
13383 decl = lookup_qualified_name (parser->scope, name, is_type,
13385 if (dependent_type_p)
13386 pop_scope (parser->scope);
13388 parser->qualifying_scope = parser->scope;
13389 parser->object_scope = NULL_TREE;
13391 else if (object_type)
13393 tree object_decl = NULL_TREE;
13394 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13395 OBJECT_TYPE is not a class. */
13396 if (CLASS_TYPE_P (object_type))
13397 /* If the OBJECT_TYPE is a template specialization, it may
13398 be instantiated during name lookup. In that case, errors
13399 may be issued. Even if we rollback the current tentative
13400 parse, those errors are valid. */
13401 object_decl = lookup_member (object_type,
13403 /*protect=*/0, is_type);
13404 /* Look it up in the enclosing context, too. */
13405 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13408 parser->object_scope = object_type;
13409 parser->qualifying_scope = NULL_TREE;
13411 decl = object_decl;
13415 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13418 parser->qualifying_scope = NULL_TREE;
13419 parser->object_scope = NULL_TREE;
13422 /* If the lookup failed, let our caller know. */
13424 || decl == error_mark_node
13425 || (TREE_CODE (decl) == FUNCTION_DECL
13426 && DECL_ANTICIPATED (decl)))
13427 return error_mark_node;
13429 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13430 if (TREE_CODE (decl) == TREE_LIST)
13432 /* The error message we have to print is too complicated for
13433 cp_parser_error, so we incorporate its actions directly. */
13434 if (!cp_parser_simulate_error (parser))
13436 error ("reference to `%D' is ambiguous", name);
13437 print_candidates (decl);
13439 return error_mark_node;
13442 my_friendly_assert (DECL_P (decl)
13443 || TREE_CODE (decl) == OVERLOAD
13444 || TREE_CODE (decl) == SCOPE_REF
13445 || BASELINK_P (decl),
13448 /* If we have resolved the name of a member declaration, check to
13449 see if the declaration is accessible. When the name resolves to
13450 set of overloaded functions, accesibility is checked when
13451 overload resolution is done.
13453 During an explicit instantiation, access is not checked at all,
13454 as per [temp.explicit]. */
13455 if (check_access && scope_chain->check_access && DECL_P (decl))
13457 tree qualifying_type;
13459 /* Figure out the type through which DECL is being
13462 = cp_parser_scope_through_which_access_occurs (decl,
13465 if (qualifying_type)
13466 perform_or_defer_access_check (qualifying_type, decl);
13472 /* Like cp_parser_lookup_name, but for use in the typical case where
13473 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13477 cp_parser_lookup_name_simple (parser, name)
13481 return cp_parser_lookup_name (parser, name,
13482 /*check_access=*/true,
13484 /*is_namespace=*/false,
13485 /*check_dependency=*/true);
13488 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13489 TYPENAME_TYPE corresponds. Note that this function peers inside
13490 uninstantiated templates and therefore should be used only in
13491 extremely limited situations. */
13494 cp_parser_resolve_typename_type (parser, type)
13502 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13505 scope = TYPE_CONTEXT (type);
13506 name = DECL_NAME (TYPE_NAME (type));
13508 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13509 it first before we can figure out what NAME refers to. */
13510 if (TREE_CODE (scope) == TYPENAME_TYPE)
13511 scope = cp_parser_resolve_typename_type (parser, scope);
13512 /* If we don't know what SCOPE refers to, then we cannot resolve the
13514 if (scope == error_mark_node)
13515 return error_mark_node;
13516 /* If the SCOPE is a template type parameter, we have no way of
13517 resolving the name. */
13518 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13520 /* Enter the SCOPE so that name lookup will be resolved as if we
13521 were in the class definition. In particular, SCOPE will no
13522 longer be considered a dependent type. */
13523 push_scope (scope);
13524 /* Look up the declaration. */
13525 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13526 /* If all went well, we got a TYPE_DECL for a non-typename. */
13528 || TREE_CODE (decl) != TYPE_DECL
13529 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13531 cp_parser_error (parser, "could not resolve typename type");
13532 type = error_mark_node;
13535 type = TREE_TYPE (decl);
13536 /* Leave the SCOPE. */
13542 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13543 the current context, return the TYPE_DECL. If TAG_NAME_P is
13544 true, the DECL indicates the class being defined in a class-head,
13545 or declared in an elaborated-type-specifier.
13547 Otherwise, return DECL. */
13550 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13552 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13553 the scope of the class, then treat the TEMPLATE_DECL as a
13554 class-name. For example, in:
13556 template <class T> struct S {
13562 If the TEMPLATE_DECL is being declared as part of a class-head,
13563 the same translation occurs:
13566 template <typename T> struct B;
13569 template <typename T> struct A::B {};
13571 Similarly, in a elaborated-type-specifier:
13573 namespace N { struct X{}; }
13576 template <typename T> friend struct N::X;
13580 if (DECL_CLASS_TEMPLATE_P (decl)
13582 || (current_class_type
13583 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13584 current_class_type))))
13585 return DECL_TEMPLATE_RESULT (decl);
13590 /* If too many, or too few, template-parameter lists apply to the
13591 declarator, issue an error message. Returns TRUE if all went well,
13592 and FALSE otherwise. */
13595 cp_parser_check_declarator_template_parameters (parser, declarator)
13599 unsigned num_templates;
13601 /* We haven't seen any classes that involve template parameters yet. */
13604 switch (TREE_CODE (declarator))
13611 tree main_declarator = TREE_OPERAND (declarator, 0);
13613 cp_parser_check_declarator_template_parameters (parser,
13622 scope = TREE_OPERAND (declarator, 0);
13623 member = TREE_OPERAND (declarator, 1);
13625 /* If this is a pointer-to-member, then we are not interested
13626 in the SCOPE, because it does not qualify the thing that is
13628 if (TREE_CODE (member) == INDIRECT_REF)
13629 return (cp_parser_check_declarator_template_parameters
13632 while (scope && CLASS_TYPE_P (scope))
13634 /* You're supposed to have one `template <...>'
13635 for every template class, but you don't need one
13636 for a full specialization. For example:
13638 template <class T> struct S{};
13639 template <> struct S<int> { void f(); };
13640 void S<int>::f () {}
13642 is correct; there shouldn't be a `template <>' for
13643 the definition of `S<int>::f'. */
13644 if (CLASSTYPE_TEMPLATE_INFO (scope)
13645 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13646 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13647 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13650 scope = TYPE_CONTEXT (scope);
13654 /* Fall through. */
13657 /* If the DECLARATOR has the form `X<y>' then it uses one
13658 additional level of template parameters. */
13659 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13662 return cp_parser_check_template_parameters (parser,
13667 /* NUM_TEMPLATES were used in the current declaration. If that is
13668 invalid, return FALSE and issue an error messages. Otherwise,
13672 cp_parser_check_template_parameters (parser, num_templates)
13674 unsigned num_templates;
13676 /* If there are more template classes than parameter lists, we have
13679 template <class T> void S<T>::R<T>::f (); */
13680 if (parser->num_template_parameter_lists < num_templates)
13682 error ("too few template-parameter-lists");
13685 /* If there are the same number of template classes and parameter
13686 lists, that's OK. */
13687 if (parser->num_template_parameter_lists == num_templates)
13689 /* If there are more, but only one more, then we are referring to a
13690 member template. That's OK too. */
13691 if (parser->num_template_parameter_lists == num_templates + 1)
13693 /* Otherwise, there are too many template parameter lists. We have
13696 template <class T> template <class U> void S::f(); */
13697 error ("too many template-parameter-lists");
13701 /* Parse a binary-expression of the general form:
13705 binary-expression <token> <expr>
13707 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13708 to parser the <expr>s. If the first production is used, then the
13709 value returned by FN is returned directly. Otherwise, a node with
13710 the indicated EXPR_TYPE is returned, with operands corresponding to
13711 the two sub-expressions. */
13714 cp_parser_binary_expression (parser, token_tree_map, fn)
13716 const cp_parser_token_tree_map token_tree_map;
13717 cp_parser_expression_fn fn;
13721 /* Parse the first expression. */
13722 lhs = (*fn) (parser);
13723 /* Now, look for more expressions. */
13727 const cp_parser_token_tree_map_node *map_node;
13730 /* Peek at the next token. */
13731 token = cp_lexer_peek_token (parser->lexer);
13732 /* If the token is `>', and that's not an operator at the
13733 moment, then we're done. */
13734 if (token->type == CPP_GREATER
13735 && !parser->greater_than_is_operator_p)
13737 /* If we find one of the tokens we want, build the correspoding
13738 tree representation. */
13739 for (map_node = token_tree_map;
13740 map_node->token_type != CPP_EOF;
13742 if (map_node->token_type == token->type)
13744 /* Consume the operator token. */
13745 cp_lexer_consume_token (parser->lexer);
13746 /* Parse the right-hand side of the expression. */
13747 rhs = (*fn) (parser);
13748 /* Build the binary tree node. */
13749 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13753 /* If the token wasn't one of the ones we want, we're done. */
13754 if (map_node->token_type == CPP_EOF)
13761 /* Parse an optional `::' token indicating that the following name is
13762 from the global namespace. If so, PARSER->SCOPE is set to the
13763 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13764 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13765 Returns the new value of PARSER->SCOPE, if the `::' token is
13766 present, and NULL_TREE otherwise. */
13769 cp_parser_global_scope_opt (parser, current_scope_valid_p)
13771 bool current_scope_valid_p;
13775 /* Peek at the next token. */
13776 token = cp_lexer_peek_token (parser->lexer);
13777 /* If we're looking at a `::' token then we're starting from the
13778 global namespace, not our current location. */
13779 if (token->type == CPP_SCOPE)
13781 /* Consume the `::' token. */
13782 cp_lexer_consume_token (parser->lexer);
13783 /* Set the SCOPE so that we know where to start the lookup. */
13784 parser->scope = global_namespace;
13785 parser->qualifying_scope = global_namespace;
13786 parser->object_scope = NULL_TREE;
13788 return parser->scope;
13790 else if (!current_scope_valid_p)
13792 parser->scope = NULL_TREE;
13793 parser->qualifying_scope = NULL_TREE;
13794 parser->object_scope = NULL_TREE;
13800 /* Returns TRUE if the upcoming token sequence is the start of a
13801 constructor declarator. If FRIEND_P is true, the declarator is
13802 preceded by the `friend' specifier. */
13805 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13807 bool constructor_p;
13808 tree type_decl = NULL_TREE;
13809 bool nested_name_p;
13810 cp_token *next_token;
13812 /* The common case is that this is not a constructor declarator, so
13813 try to avoid doing lots of work if at all possible. */
13814 next_token = cp_lexer_peek_token (parser->lexer);
13815 if (next_token->type != CPP_NAME
13816 && next_token->type != CPP_SCOPE
13817 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13818 && next_token->type != CPP_TEMPLATE_ID)
13821 /* Parse tentatively; we are going to roll back all of the tokens
13823 cp_parser_parse_tentatively (parser);
13824 /* Assume that we are looking at a constructor declarator. */
13825 constructor_p = true;
13826 /* Look for the optional `::' operator. */
13827 cp_parser_global_scope_opt (parser,
13828 /*current_scope_valid_p=*/false);
13829 /* Look for the nested-name-specifier. */
13831 = (cp_parser_nested_name_specifier_opt (parser,
13832 /*typename_keyword_p=*/false,
13833 /*check_dependency_p=*/false,
13836 /* Outside of a class-specifier, there must be a
13837 nested-name-specifier. */
13838 if (!nested_name_p &&
13839 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13841 constructor_p = false;
13842 /* If we still think that this might be a constructor-declarator,
13843 look for a class-name. */
13848 template <typename T> struct S { S(); }
13849 template <typename T> S<T>::S ();
13851 we must recognize that the nested `S' names a class.
13854 template <typename T> S<T>::S<T> ();
13856 we must recognize that the nested `S' names a template. */
13857 type_decl = cp_parser_class_name (parser,
13858 /*typename_keyword_p=*/false,
13859 /*template_keyword_p=*/false,
13861 /*check_access_p=*/false,
13862 /*check_dependency_p=*/false,
13863 /*class_head_p=*/false);
13864 /* If there was no class-name, then this is not a constructor. */
13865 constructor_p = !cp_parser_error_occurred (parser);
13867 /* If we're still considering a constructor, we have to see a `(',
13868 to begin the parameter-declaration-clause, followed by either a
13869 `)', an `...', or a decl-specifier. We need to check for a
13870 type-specifier to avoid being fooled into thinking that:
13874 is a constructor. (It is actually a function named `f' that
13875 takes one parameter (of type `int') and returns a value of type
13878 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13881 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13882 && !cp_parser_storage_class_specifier_opt (parser))
13884 if (current_class_type
13885 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13886 /* The constructor for one class cannot be declared inside
13888 constructor_p = false;
13893 /* Names appearing in the type-specifier should be looked up
13894 in the scope of the class. */
13895 if (current_class_type)
13899 type = TREE_TYPE (type_decl);
13900 if (TREE_CODE (type) == TYPENAME_TYPE)
13901 type = cp_parser_resolve_typename_type (parser, type);
13904 /* Look for the type-specifier. */
13905 cp_parser_type_specifier (parser,
13906 CP_PARSER_FLAGS_NONE,
13907 /*is_friend=*/false,
13908 /*is_declarator=*/true,
13909 /*declares_class_or_enum=*/NULL,
13910 /*is_cv_qualifier=*/NULL);
13911 /* Leave the scope of the class. */
13915 constructor_p = !cp_parser_error_occurred (parser);
13920 constructor_p = false;
13921 /* We did not really want to consume any tokens. */
13922 cp_parser_abort_tentative_parse (parser);
13924 return constructor_p;
13927 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13928 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13929 they must be performed once we are in the scope of the function.
13931 Returns the function defined. */
13934 cp_parser_function_definition_from_specifiers_and_declarator
13935 (parser, decl_specifiers, attributes, declarator)
13937 tree decl_specifiers;
13944 /* Begin the function-definition. */
13945 success_p = begin_function_definition (decl_specifiers,
13949 /* If there were names looked up in the decl-specifier-seq that we
13950 did not check, check them now. We must wait until we are in the
13951 scope of the function to perform the checks, since the function
13952 might be a friend. */
13953 perform_deferred_access_checks ();
13957 /* If begin_function_definition didn't like the definition, skip
13958 the entire function. */
13959 error ("invalid function declaration");
13960 cp_parser_skip_to_end_of_block_or_statement (parser);
13961 fn = error_mark_node;
13964 fn = cp_parser_function_definition_after_declarator (parser,
13965 /*inline_p=*/false);
13970 /* Parse the part of a function-definition that follows the
13971 declarator. INLINE_P is TRUE iff this function is an inline
13972 function defined with a class-specifier.
13974 Returns the function defined. */
13977 cp_parser_function_definition_after_declarator (parser,
13983 bool ctor_initializer_p = false;
13984 bool saved_in_unbraced_linkage_specification_p;
13985 unsigned saved_num_template_parameter_lists;
13987 /* If the next token is `return', then the code may be trying to
13988 make use of the "named return value" extension that G++ used to
13990 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13992 /* Consume the `return' keyword. */
13993 cp_lexer_consume_token (parser->lexer);
13994 /* Look for the identifier that indicates what value is to be
13996 cp_parser_identifier (parser);
13997 /* Issue an error message. */
13998 error ("named return values are no longer supported");
13999 /* Skip tokens until we reach the start of the function body. */
14000 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14001 cp_lexer_consume_token (parser->lexer);
14003 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14004 anything declared inside `f'. */
14005 saved_in_unbraced_linkage_specification_p
14006 = parser->in_unbraced_linkage_specification_p;
14007 parser->in_unbraced_linkage_specification_p = false;
14008 /* Inside the function, surrounding template-parameter-lists do not
14010 saved_num_template_parameter_lists
14011 = parser->num_template_parameter_lists;
14012 parser->num_template_parameter_lists = 0;
14013 /* If the next token is `try', then we are looking at a
14014 function-try-block. */
14015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14016 ctor_initializer_p = cp_parser_function_try_block (parser);
14017 /* A function-try-block includes the function-body, so we only do
14018 this next part if we're not processing a function-try-block. */
14021 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14023 /* Finish the function. */
14024 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14025 (inline_p ? 2 : 0));
14026 /* Generate code for it, if necessary. */
14028 /* Restore the saved values. */
14029 parser->in_unbraced_linkage_specification_p
14030 = saved_in_unbraced_linkage_specification_p;
14031 parser->num_template_parameter_lists
14032 = saved_num_template_parameter_lists;
14037 /* Parse a template-declaration, assuming that the `export' (and
14038 `extern') keywords, if present, has already been scanned. MEMBER_P
14039 is as for cp_parser_template_declaration. */
14042 cp_parser_template_declaration_after_export (parser, member_p)
14046 tree decl = NULL_TREE;
14047 tree parameter_list;
14048 bool friend_p = false;
14050 /* Look for the `template' keyword. */
14051 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14055 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14058 /* Parse the template parameters. */
14059 begin_template_parm_list ();
14060 /* If the next token is `>', then we have an invalid
14061 specialization. Rather than complain about an invalid template
14062 parameter, issue an error message here. */
14063 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14065 cp_parser_error (parser, "invalid explicit specialization");
14066 parameter_list = NULL_TREE;
14069 parameter_list = cp_parser_template_parameter_list (parser);
14070 parameter_list = end_template_parm_list (parameter_list);
14071 /* Look for the `>'. */
14072 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14073 /* We just processed one more parameter list. */
14074 ++parser->num_template_parameter_lists;
14075 /* If the next token is `template', there are more template
14077 if (cp_lexer_next_token_is_keyword (parser->lexer,
14079 cp_parser_template_declaration_after_export (parser, member_p);
14082 decl = cp_parser_single_declaration (parser,
14086 /* If this is a member template declaration, let the front
14088 if (member_p && !friend_p && decl)
14089 decl = finish_member_template_decl (decl);
14090 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14091 make_friend_class (current_class_type, TREE_TYPE (decl));
14093 /* We are done with the current parameter list. */
14094 --parser->num_template_parameter_lists;
14097 finish_template_decl (parameter_list);
14099 /* Register member declarations. */
14100 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14101 finish_member_declaration (decl);
14103 /* If DECL is a function template, we must return to parse it later.
14104 (Even though there is no definition, there might be default
14105 arguments that need handling.) */
14106 if (member_p && decl
14107 && (TREE_CODE (decl) == FUNCTION_DECL
14108 || DECL_FUNCTION_TEMPLATE_P (decl)))
14109 TREE_VALUE (parser->unparsed_functions_queues)
14110 = tree_cons (NULL_TREE, decl,
14111 TREE_VALUE (parser->unparsed_functions_queues));
14114 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14115 `function-definition' sequence. MEMBER_P is true, this declaration
14116 appears in a class scope.
14118 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14119 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14122 cp_parser_single_declaration (parser,
14129 bool declares_class_or_enum;
14130 tree decl = NULL_TREE;
14131 tree decl_specifiers;
14134 /* Parse the dependent declaration. We don't know yet
14135 whether it will be a function-definition. */
14136 cp_parser_parse_tentatively (parser);
14137 /* Defer access checks until we know what is being declared. */
14138 push_deferring_access_checks (true);
14140 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14143 = cp_parser_decl_specifier_seq (parser,
14144 CP_PARSER_FLAGS_OPTIONAL,
14146 &declares_class_or_enum);
14147 /* Gather up the access checks that occurred the
14148 decl-specifier-seq. */
14149 stop_deferring_access_checks ();
14151 /* Check for the declaration of a template class. */
14152 if (declares_class_or_enum)
14154 if (cp_parser_declares_only_class_p (parser))
14156 decl = shadow_tag (decl_specifiers);
14158 decl = TYPE_NAME (decl);
14160 decl = error_mark_node;
14165 /* If it's not a template class, try for a template function. If
14166 the next token is a `;', then this declaration does not declare
14167 anything. But, if there were errors in the decl-specifiers, then
14168 the error might well have come from an attempted class-specifier.
14169 In that case, there's no need to warn about a missing declarator. */
14171 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14172 || !value_member (error_mark_node, decl_specifiers)))
14173 decl = cp_parser_init_declarator (parser,
14176 /*function_definition_allowed_p=*/false,
14178 /*function_definition_p=*/NULL);
14180 pop_deferring_access_checks ();
14182 /* Clear any current qualification; whatever comes next is the start
14183 of something new. */
14184 parser->scope = NULL_TREE;
14185 parser->qualifying_scope = NULL_TREE;
14186 parser->object_scope = NULL_TREE;
14187 /* Look for a trailing `;' after the declaration. */
14188 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14189 && cp_parser_committed_to_tentative_parse (parser))
14190 cp_parser_skip_to_end_of_block_or_statement (parser);
14191 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14192 if (cp_parser_parse_definitely (parser))
14195 *friend_p = cp_parser_friend_p (decl_specifiers);
14197 /* Otherwise, try a function-definition. */
14199 decl = cp_parser_function_definition (parser, friend_p);
14204 /* Parse a functional cast to TYPE. Returns an expression
14205 representing the cast. */
14208 cp_parser_functional_cast (parser, type)
14212 tree expression_list;
14214 /* Look for the opening `('. */
14215 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14216 return error_mark_node;
14217 /* If the next token is not an `)', there are arguments to the
14219 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14220 expression_list = cp_parser_expression_list (parser);
14222 expression_list = NULL_TREE;
14223 /* Look for the closing `)'. */
14224 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14226 return build_functional_cast (type, expression_list);
14229 /* MEMBER_FUNCTION is a member function, or a friend. If default
14230 arguments, or the body of the function have not yet been parsed,
14234 cp_parser_late_parsing_for_member (parser, member_function)
14236 tree member_function;
14238 cp_lexer *saved_lexer;
14240 /* If this member is a template, get the underlying
14242 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14243 member_function = DECL_TEMPLATE_RESULT (member_function);
14245 /* There should not be any class definitions in progress at this
14246 point; the bodies of members are only parsed outside of all class
14248 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14249 /* While we're parsing the member functions we might encounter more
14250 classes. We want to handle them right away, but we don't want
14251 them getting mixed up with functions that are currently in the
14253 parser->unparsed_functions_queues
14254 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14256 /* Make sure that any template parameters are in scope. */
14257 maybe_begin_member_template_processing (member_function);
14259 /* If the body of the function has not yet been parsed, parse it
14261 if (DECL_PENDING_INLINE_P (member_function))
14263 tree function_scope;
14264 cp_token_cache *tokens;
14266 /* The function is no longer pending; we are processing it. */
14267 tokens = DECL_PENDING_INLINE_INFO (member_function);
14268 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14269 DECL_PENDING_INLINE_P (member_function) = 0;
14270 /* If this was an inline function in a local class, enter the scope
14271 of the containing function. */
14272 function_scope = decl_function_context (member_function);
14273 if (function_scope)
14274 push_function_context_to (function_scope);
14276 /* Save away the current lexer. */
14277 saved_lexer = parser->lexer;
14278 /* Make a new lexer to feed us the tokens saved for this function. */
14279 parser->lexer = cp_lexer_new_from_tokens (tokens);
14280 parser->lexer->next = saved_lexer;
14282 /* Set the current source position to be the location of the first
14283 token in the saved inline body. */
14284 cp_lexer_peek_token (parser->lexer);
14286 /* Let the front end know that we going to be defining this
14288 start_function (NULL_TREE, member_function, NULL_TREE,
14289 SF_PRE_PARSED | SF_INCLASS_INLINE);
14291 /* Now, parse the body of the function. */
14292 cp_parser_function_definition_after_declarator (parser,
14293 /*inline_p=*/true);
14295 /* Leave the scope of the containing function. */
14296 if (function_scope)
14297 pop_function_context_from (function_scope);
14298 /* Restore the lexer. */
14299 parser->lexer = saved_lexer;
14302 /* Remove any template parameters from the symbol table. */
14303 maybe_end_member_template_processing ();
14305 /* Restore the queue. */
14306 parser->unparsed_functions_queues
14307 = TREE_CHAIN (parser->unparsed_functions_queues);
14310 /* FN is a FUNCTION_DECL which may contains a parameter with an
14311 unparsed DEFAULT_ARG. Parse the default args now. */
14314 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14316 cp_lexer *saved_lexer;
14317 cp_token_cache *tokens;
14318 bool saved_local_variables_forbidden_p;
14321 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14323 parameters = TREE_CHAIN (parameters))
14325 if (!TREE_PURPOSE (parameters)
14326 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14329 /* Save away the current lexer. */
14330 saved_lexer = parser->lexer;
14331 /* Create a new one, using the tokens we have saved. */
14332 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14333 parser->lexer = cp_lexer_new_from_tokens (tokens);
14335 /* Set the current source position to be the location of the
14336 first token in the default argument. */
14337 cp_lexer_peek_token (parser->lexer);
14339 /* Local variable names (and the `this' keyword) may not appear
14340 in a default argument. */
14341 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14342 parser->local_variables_forbidden_p = true;
14343 /* Parse the assignment-expression. */
14344 if (DECL_CONTEXT (fn))
14345 push_nested_class (DECL_CONTEXT (fn), 1);
14346 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14347 if (DECL_CONTEXT (fn))
14348 pop_nested_class ();
14350 /* Restore saved state. */
14351 parser->lexer = saved_lexer;
14352 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14356 /* Parse the operand of `sizeof' (or a similar operator). Returns
14357 either a TYPE or an expression, depending on the form of the
14358 input. The KEYWORD indicates which kind of expression we have
14362 cp_parser_sizeof_operand (parser, keyword)
14366 static const char *format;
14367 tree expr = NULL_TREE;
14368 const char *saved_message;
14369 bool saved_constant_expression_p;
14371 /* Initialize FORMAT the first time we get here. */
14373 format = "types may not be defined in `%s' expressions";
14375 /* Types cannot be defined in a `sizeof' expression. Save away the
14377 saved_message = parser->type_definition_forbidden_message;
14378 /* And create the new one. */
14379 parser->type_definition_forbidden_message
14381 xmalloc (strlen (format)
14382 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14384 sprintf ((char *) parser->type_definition_forbidden_message,
14385 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14387 /* The restrictions on constant-expressions do not apply inside
14388 sizeof expressions. */
14389 saved_constant_expression_p = parser->constant_expression_p;
14390 parser->constant_expression_p = false;
14392 /* Do not actually evaluate the expression. */
14394 /* If it's a `(', then we might be looking at the type-id
14396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14400 /* We can't be sure yet whether we're looking at a type-id or an
14402 cp_parser_parse_tentatively (parser);
14403 /* Consume the `('. */
14404 cp_lexer_consume_token (parser->lexer);
14405 /* Parse the type-id. */
14406 type = cp_parser_type_id (parser);
14407 /* Now, look for the trailing `)'. */
14408 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14409 /* If all went well, then we're done. */
14410 if (cp_parser_parse_definitely (parser))
14412 /* Build a list of decl-specifiers; right now, we have only
14413 a single type-specifier. */
14414 type = build_tree_list (NULL_TREE,
14417 /* Call grokdeclarator to figure out what type this is. */
14418 expr = grokdeclarator (NULL_TREE,
14422 /*attrlist=*/NULL);
14426 /* If the type-id production did not work out, then we must be
14427 looking at the unary-expression production. */
14429 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14430 /* Go back to evaluating expressions. */
14433 /* Free the message we created. */
14434 free ((char *) parser->type_definition_forbidden_message);
14435 /* And restore the old one. */
14436 parser->type_definition_forbidden_message = saved_message;
14437 parser->constant_expression_p = saved_constant_expression_p;
14442 /* If the current declaration has no declarator, return true. */
14445 cp_parser_declares_only_class_p (cp_parser *parser)
14447 /* If the next token is a `;' or a `,' then there is no
14449 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14450 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14453 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14454 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14457 cp_parser_friend_p (decl_specifiers)
14458 tree decl_specifiers;
14460 while (decl_specifiers)
14462 /* See if this decl-specifier is `friend'. */
14463 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14464 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14467 /* Go on to the next decl-specifier. */
14468 decl_specifiers = TREE_CHAIN (decl_specifiers);
14474 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14475 issue an error message indicating that TOKEN_DESC was expected.
14477 Returns the token consumed, if the token had the appropriate type.
14478 Otherwise, returns NULL. */
14481 cp_parser_require (parser, type, token_desc)
14483 enum cpp_ttype type;
14484 const char *token_desc;
14486 if (cp_lexer_next_token_is (parser->lexer, type))
14487 return cp_lexer_consume_token (parser->lexer);
14490 /* Output the MESSAGE -- unless we're parsing tentatively. */
14491 if (!cp_parser_simulate_error (parser))
14492 error ("expected %s", token_desc);
14497 /* Like cp_parser_require, except that tokens will be skipped until
14498 the desired token is found. An error message is still produced if
14499 the next token is not as expected. */
14502 cp_parser_skip_until_found (parser, type, token_desc)
14504 enum cpp_ttype type;
14505 const char *token_desc;
14508 unsigned nesting_depth = 0;
14510 if (cp_parser_require (parser, type, token_desc))
14513 /* Skip tokens until the desired token is found. */
14516 /* Peek at the next token. */
14517 token = cp_lexer_peek_token (parser->lexer);
14518 /* If we've reached the token we want, consume it and
14520 if (token->type == type && !nesting_depth)
14522 cp_lexer_consume_token (parser->lexer);
14525 /* If we've run out of tokens, stop. */
14526 if (token->type == CPP_EOF)
14528 if (token->type == CPP_OPEN_BRACE
14529 || token->type == CPP_OPEN_PAREN
14530 || token->type == CPP_OPEN_SQUARE)
14532 else if (token->type == CPP_CLOSE_BRACE
14533 || token->type == CPP_CLOSE_PAREN
14534 || token->type == CPP_CLOSE_SQUARE)
14536 if (nesting_depth-- == 0)
14539 /* Consume this token. */
14540 cp_lexer_consume_token (parser->lexer);
14544 /* If the next token is the indicated keyword, consume it. Otherwise,
14545 issue an error message indicating that TOKEN_DESC was expected.
14547 Returns the token consumed, if the token had the appropriate type.
14548 Otherwise, returns NULL. */
14551 cp_parser_require_keyword (parser, keyword, token_desc)
14554 const char *token_desc;
14556 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14558 if (token && token->keyword != keyword)
14560 dyn_string_t error_msg;
14562 /* Format the error message. */
14563 error_msg = dyn_string_new (0);
14564 dyn_string_append_cstr (error_msg, "expected ");
14565 dyn_string_append_cstr (error_msg, token_desc);
14566 cp_parser_error (parser, error_msg->s);
14567 dyn_string_delete (error_msg);
14574 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14575 function-definition. */
14578 cp_parser_token_starts_function_definition_p (token)
14581 return (/* An ordinary function-body begins with an `{'. */
14582 token->type == CPP_OPEN_BRACE
14583 /* A ctor-initializer begins with a `:'. */
14584 || token->type == CPP_COLON
14585 /* A function-try-block begins with `try'. */
14586 || token->keyword == RID_TRY
14587 /* The named return value extension begins with `return'. */
14588 || token->keyword == RID_RETURN);
14591 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14595 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14599 token = cp_lexer_peek_token (parser->lexer);
14600 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14603 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14604 or none_type otherwise. */
14606 static enum tag_types
14607 cp_parser_token_is_class_key (token)
14610 switch (token->keyword)
14615 return record_type;
14624 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14627 cp_parser_check_class_key (enum tag_types class_key, tree type)
14629 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14630 pedwarn ("`%s' tag used in naming `%#T'",
14631 class_key == union_type ? "union"
14632 : class_key == record_type ? "struct" : "class",
14636 /* Look for the `template' keyword, as a syntactic disambiguator.
14637 Return TRUE iff it is present, in which case it will be
14641 cp_parser_optional_template_keyword (cp_parser *parser)
14643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14645 /* The `template' keyword can only be used within templates;
14646 outside templates the parser can always figure out what is a
14647 template and what is not. */
14648 if (!processing_template_decl)
14650 error ("`template' (as a disambiguator) is only allowed "
14651 "within templates");
14652 /* If this part of the token stream is rescanned, the same
14653 error message would be generated. So, we purge the token
14654 from the stream. */
14655 cp_lexer_purge_token (parser->lexer);
14660 /* Consume the `template' keyword. */
14661 cp_lexer_consume_token (parser->lexer);
14669 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14670 set PARSER->SCOPE, and perform other related actions. */
14673 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14678 /* Get the stored value. */
14679 value = cp_lexer_consume_token (parser->lexer)->value;
14680 /* Perform any access checks that were deferred. */
14681 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14682 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14683 /* Set the scope from the stored value. */
14684 parser->scope = TREE_VALUE (value);
14685 parser->qualifying_scope = TREE_TYPE (value);
14686 parser->object_scope = NULL_TREE;
14689 /* Add tokens to CACHE until an non-nested END token appears. */
14692 cp_parser_cache_group (cp_parser *parser,
14693 cp_token_cache *cache,
14694 enum cpp_ttype end,
14701 /* Abort a parenthesized expression if we encounter a brace. */
14702 if ((end == CPP_CLOSE_PAREN || depth == 0)
14703 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14705 /* Consume the next token. */
14706 token = cp_lexer_consume_token (parser->lexer);
14707 /* If we've reached the end of the file, stop. */
14708 if (token->type == CPP_EOF)
14710 /* Add this token to the tokens we are saving. */
14711 cp_token_cache_push_token (cache, token);
14712 /* See if it starts a new group. */
14713 if (token->type == CPP_OPEN_BRACE)
14715 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14719 else if (token->type == CPP_OPEN_PAREN)
14720 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14721 else if (token->type == end)
14726 /* Begin parsing tentatively. We always save tokens while parsing
14727 tentatively so that if the tentative parsing fails we can restore the
14731 cp_parser_parse_tentatively (parser)
14734 /* Enter a new parsing context. */
14735 parser->context = cp_parser_context_new (parser->context);
14736 /* Begin saving tokens. */
14737 cp_lexer_save_tokens (parser->lexer);
14738 /* In order to avoid repetitive access control error messages,
14739 access checks are queued up until we are no longer parsing
14741 push_deferring_access_checks (true);
14744 /* Commit to the currently active tentative parse. */
14747 cp_parser_commit_to_tentative_parse (parser)
14750 cp_parser_context *context;
14753 /* Mark all of the levels as committed. */
14754 lexer = parser->lexer;
14755 for (context = parser->context; context->next; context = context->next)
14757 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14759 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14760 while (!cp_lexer_saving_tokens (lexer))
14761 lexer = lexer->next;
14762 cp_lexer_commit_tokens (lexer);
14766 /* Abort the currently active tentative parse. All consumed tokens
14767 will be rolled back, and no diagnostics will be issued. */
14770 cp_parser_abort_tentative_parse (parser)
14773 cp_parser_simulate_error (parser);
14774 /* Now, pretend that we want to see if the construct was
14775 successfully parsed. */
14776 cp_parser_parse_definitely (parser);
14779 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14780 token stream. Otherwise, commit to the tokens we have consumed.
14781 Returns true if no error occurred; false otherwise. */
14784 cp_parser_parse_definitely (parser)
14787 bool error_occurred;
14788 cp_parser_context *context;
14790 /* Remember whether or not an error ocurred, since we are about to
14791 destroy that information. */
14792 error_occurred = cp_parser_error_occurred (parser);
14793 /* Remove the topmost context from the stack. */
14794 context = parser->context;
14795 parser->context = context->next;
14796 /* If no parse errors occurred, commit to the tentative parse. */
14797 if (!error_occurred)
14799 /* Commit to the tokens read tentatively, unless that was
14801 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14802 cp_lexer_commit_tokens (parser->lexer);
14804 pop_to_parent_deferring_access_checks ();
14806 /* Otherwise, if errors occurred, roll back our state so that things
14807 are just as they were before we began the tentative parse. */
14810 cp_lexer_rollback_tokens (parser->lexer);
14811 pop_deferring_access_checks ();
14813 /* Add the context to the front of the free list. */
14814 context->next = cp_parser_context_free_list;
14815 cp_parser_context_free_list = context;
14817 return !error_occurred;
14820 /* Returns true if we are parsing tentatively -- but have decided that
14821 we will stick with this tentative parse, even if errors occur. */
14824 cp_parser_committed_to_tentative_parse (parser)
14827 return (cp_parser_parsing_tentatively (parser)
14828 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14831 /* Returns non-zero iff an error has occurred during the most recent
14832 tentative parse. */
14835 cp_parser_error_occurred (parser)
14838 return (cp_parser_parsing_tentatively (parser)
14839 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14842 /* Returns non-zero if GNU extensions are allowed. */
14845 cp_parser_allow_gnu_extensions_p (parser)
14848 return parser->allow_gnu_extensions_p;
14855 static GTY (()) cp_parser *the_parser;
14857 /* External interface. */
14859 /* Parse the entire translation unit. */
14864 bool error_occurred;
14866 the_parser = cp_parser_new ();
14867 push_deferring_access_checks (false);
14868 error_occurred = cp_parser_translation_unit (the_parser);
14873 return error_occurred;
14876 /* Clean up after parsing the entire translation unit. */
14879 free_parser_stacks ()
14881 /* Nothing to do. */
14884 /* This variable must be provided by every front end. */
14888 #include "gt-cp-parser.h"