2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
51 We use a circular buffer to store incoming tokens.
53 Some artifacts of the C++ language (such as the
54 expression/declaration ambiguity) require arbitrary look-ahead.
55 The strategy we adopt for dealing with these problems is to attempt
56 to parse one construct (e.g., the declaration) and fall back to the
57 other (e.g., the expression) if that attempt does not succeed.
58 Therefore, we must sometimes store an arbitrary number of tokens.
60 The parser routinely peeks at the next token, and then consumes it
61 later. That also requires a buffer in which to store the tokens.
63 In order to easily permit adding tokens to the end of the buffer,
64 while removing them from the beginning of the buffer, we use a
69 typedef struct cp_token GTY (())
71 /* The kind of token. */
72 ENUM_BITFIELD (cpp_ttype) type : 8;
73 /* If this token is a keyword, this value indicates which keyword.
74 Otherwise, this value is RID_MAX. */
75 ENUM_BITFIELD (rid) keyword : 8;
78 /* The value associated with this token, if any. */
80 /* The location at which this token was found. */
84 /* The number of tokens in a single token block.
85 Computed so that cp_token_block fits in a 512B allocation unit. */
87 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
89 /* A group of tokens. These groups are chained together to store
90 large numbers of tokens. (For example, a token block is created
91 when the body of an inline member function is first encountered;
92 the tokens are processed later after the class definition is
95 This somewhat ungainly data structure (as opposed to, say, a
96 variable-length array), is used due to constraints imposed by the
97 current garbage-collection methodology. If it is made more
98 flexible, we could perhaps simplify the data structures involved. */
100 typedef struct cp_token_block GTY (())
103 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
104 /* The number of tokens in this block. */
106 /* The next token block in the chain. */
107 struct cp_token_block *next;
108 /* The previous block in the chain. */
109 struct cp_token_block *prev;
112 typedef struct cp_token_cache GTY (())
114 /* The first block in the cache. NULL if there are no tokens in the
116 cp_token_block *first;
117 /* The last block in the cache. NULL If there are no tokens in the
119 cp_token_block *last;
124 static cp_token_cache *cp_token_cache_new
126 static void cp_token_cache_push_token
127 (cp_token_cache *, cp_token *);
129 /* Create a new cp_token_cache. */
131 static cp_token_cache *
132 cp_token_cache_new (void)
134 return ggc_alloc_cleared (sizeof (cp_token_cache));
137 /* Add *TOKEN to *CACHE. */
140 cp_token_cache_push_token (cp_token_cache *cache,
143 cp_token_block *b = cache->last;
145 /* See if we need to allocate a new token block. */
146 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
148 b = ggc_alloc_cleared (sizeof (cp_token_block));
149 b->prev = cache->last;
152 cache->last->next = b;
156 cache->first = cache->last = b;
158 /* Add this token to the current token block. */
159 b->tokens[b->num_tokens++] = *token;
162 /* The cp_lexer structure represents the C++ lexer. It is responsible
163 for managing the token stream from the preprocessor and supplying
166 typedef struct cp_lexer GTY (())
168 /* The memory allocated for the buffer. Never NULL. */
169 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
170 /* A pointer just past the end of the memory allocated for the buffer. */
171 cp_token * GTY ((skip)) buffer_end;
172 /* The first valid token in the buffer, or NULL if none. */
173 cp_token * GTY ((skip)) first_token;
174 /* The next available token. If NEXT_TOKEN is NULL, then there are
175 no more available tokens. */
176 cp_token * GTY ((skip)) next_token;
177 /* A pointer just past the last available token. If FIRST_TOKEN is
178 NULL, however, there are no available tokens, and then this
179 location is simply the place in which the next token read will be
180 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
181 When the LAST_TOKEN == BUFFER, then the last token is at the
182 highest memory address in the BUFFER. */
183 cp_token * GTY ((skip)) last_token;
185 /* A stack indicating positions at which cp_lexer_save_tokens was
186 called. The top entry is the most recent position at which we
187 began saving tokens. The entries are differences in token
188 position between FIRST_TOKEN and the first saved token.
190 If the stack is non-empty, we are saving tokens. When a token is
191 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
192 pointer will not. The token stream will be preserved so that it
193 can be reexamined later.
195 If the stack is empty, then we are not saving tokens. Whenever a
196 token is consumed, the FIRST_TOKEN pointer will be moved, and the
197 consumed token will be gone forever. */
198 varray_type saved_tokens;
200 /* The STRING_CST tokens encountered while processing the current
202 varray_type string_tokens;
204 /* True if we should obtain more tokens from the preprocessor; false
205 if we are processing a saved token cache. */
208 /* True if we should output debugging information. */
211 /* The next lexer in a linked list of lexers. */
212 struct cp_lexer *next;
217 static cp_lexer *cp_lexer_new_main
219 static cp_lexer *cp_lexer_new_from_tokens
220 (struct cp_token_cache *);
221 static int cp_lexer_saving_tokens
223 static cp_token *cp_lexer_next_token
224 (cp_lexer *, cp_token *);
225 static cp_token *cp_lexer_prev_token
226 (cp_lexer *, cp_token *);
227 static ptrdiff_t cp_lexer_token_difference
228 (cp_lexer *, cp_token *, cp_token *);
229 static cp_token *cp_lexer_read_token
231 static void cp_lexer_maybe_grow_buffer
233 static void cp_lexer_get_preprocessor_token
234 (cp_lexer *, cp_token *);
235 static cp_token *cp_lexer_peek_token
237 static cp_token *cp_lexer_peek_nth_token
238 (cp_lexer *, size_t);
239 static inline bool cp_lexer_next_token_is
240 (cp_lexer *, enum cpp_ttype);
241 static bool cp_lexer_next_token_is_not
242 (cp_lexer *, enum cpp_ttype);
243 static bool cp_lexer_next_token_is_keyword
244 (cp_lexer *, enum rid);
245 static cp_token *cp_lexer_consume_token
247 static void cp_lexer_purge_token
249 static void cp_lexer_purge_tokens_after
250 (cp_lexer *, cp_token *);
251 static void cp_lexer_save_tokens
253 static void cp_lexer_commit_tokens
255 static void cp_lexer_rollback_tokens
257 static inline void cp_lexer_set_source_position_from_token
258 (cp_lexer *, const cp_token *);
259 static void cp_lexer_print_token
260 (FILE *, cp_token *);
261 static inline bool cp_lexer_debugging_p
263 static void cp_lexer_start_debugging
264 (cp_lexer *) ATTRIBUTE_UNUSED;
265 static void cp_lexer_stop_debugging
266 (cp_lexer *) ATTRIBUTE_UNUSED;
268 /* Manifest constants. */
270 #define CP_TOKEN_BUFFER_SIZE 5
271 #define CP_SAVED_TOKENS_SIZE 5
273 /* A token type for keywords, as opposed to ordinary identifiers. */
274 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
276 /* A token type for template-ids. If a template-id is processed while
277 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
278 the value of the CPP_TEMPLATE_ID is whatever was returned by
279 cp_parser_template_id. */
280 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
282 /* A token type for nested-name-specifiers. If a
283 nested-name-specifier is processed while parsing tentatively, it is
284 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
285 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
286 cp_parser_nested_name_specifier_opt. */
287 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
289 /* A token type for tokens that are not tokens at all; these are used
290 to mark the end of a token block. */
291 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
295 /* The stream to which debugging output should be written. */
296 static FILE *cp_lexer_debug_stream;
298 /* Create a new main C++ lexer, the lexer that gets tokens from the
302 cp_lexer_new_main (void)
305 cp_token first_token;
307 /* It's possible that lexing the first token will load a PCH file,
308 which is a GC collection point. So we have to grab the first
309 token before allocating any memory. */
310 cp_lexer_get_preprocessor_token (NULL, &first_token);
311 c_common_no_more_pch ();
313 /* Allocate the memory. */
314 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
316 /* Create the circular buffer. */
317 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
318 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
320 /* There is one token in the buffer. */
321 lexer->last_token = lexer->buffer + 1;
322 lexer->first_token = lexer->buffer;
323 lexer->next_token = lexer->buffer;
324 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
326 /* This lexer obtains more tokens by calling c_lex. */
327 lexer->main_lexer_p = true;
329 /* Create the SAVED_TOKENS stack. */
330 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
332 /* Create the STRINGS array. */
333 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
335 /* Assume we are not debugging. */
336 lexer->debugging_p = false;
341 /* Create a new lexer whose token stream is primed with the TOKENS.
342 When these tokens are exhausted, no new tokens will be read. */
345 cp_lexer_new_from_tokens (cp_token_cache *tokens)
349 cp_token_block *block;
350 ptrdiff_t num_tokens;
352 /* Allocate the memory. */
353 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
355 /* Create a new buffer, appropriately sized. */
357 for (block = tokens->first; block != NULL; block = block->next)
358 num_tokens += block->num_tokens;
359 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
360 lexer->buffer_end = lexer->buffer + num_tokens;
362 /* Install the tokens. */
363 token = lexer->buffer;
364 for (block = tokens->first; block != NULL; block = block->next)
366 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
367 token += block->num_tokens;
370 /* The FIRST_TOKEN is the beginning of the buffer. */
371 lexer->first_token = lexer->buffer;
372 /* The next available token is also at the beginning of the buffer. */
373 lexer->next_token = lexer->buffer;
374 /* The buffer is full. */
375 lexer->last_token = lexer->first_token;
377 /* This lexer doesn't obtain more tokens. */
378 lexer->main_lexer_p = false;
380 /* Create the SAVED_TOKENS stack. */
381 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
383 /* Create the STRINGS array. */
384 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
386 /* Assume we are not debugging. */
387 lexer->debugging_p = false;
392 /* Returns nonzero if debugging information should be output. */
395 cp_lexer_debugging_p (cp_lexer *lexer)
397 return lexer->debugging_p;
400 /* Set the current source position from the information stored in
404 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
405 const cp_token *token)
407 /* Ideally, the source position information would not be a global
408 variable, but it is. */
410 /* Update the line number. */
411 if (token->type != CPP_EOF)
412 input_location = token->location;
415 /* TOKEN points into the circular token buffer. Return a pointer to
416 the next token in the buffer. */
418 static inline cp_token *
419 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
422 if (token == lexer->buffer_end)
423 token = lexer->buffer;
427 /* TOKEN points into the circular token buffer. Return a pointer to
428 the previous token in the buffer. */
430 static inline cp_token *
431 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
433 if (token == lexer->buffer)
434 token = lexer->buffer_end;
438 /* nonzero if we are presently saving tokens. */
441 cp_lexer_saving_tokens (const cp_lexer* lexer)
443 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
446 /* Return a pointer to the token that is N tokens beyond TOKEN in the
450 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
453 if (token >= lexer->buffer_end)
454 token = lexer->buffer + (token - lexer->buffer_end);
458 /* Returns the number of times that START would have to be incremented
459 to reach FINISH. If START and FINISH are the same, returns zero. */
462 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
465 return finish - start;
467 return ((lexer->buffer_end - lexer->buffer)
471 /* Obtain another token from the C preprocessor and add it to the
472 token buffer. Returns the newly read token. */
475 cp_lexer_read_token (cp_lexer* lexer)
479 /* Make sure there is room in the buffer. */
480 cp_lexer_maybe_grow_buffer (lexer);
482 /* If there weren't any tokens, then this one will be the first. */
483 if (!lexer->first_token)
484 lexer->first_token = lexer->last_token;
485 /* Similarly, if there were no available tokens, there is one now. */
486 if (!lexer->next_token)
487 lexer->next_token = lexer->last_token;
489 /* Figure out where we're going to store the new token. */
490 token = lexer->last_token;
492 /* Get a new token from the preprocessor. */
493 cp_lexer_get_preprocessor_token (lexer, token);
495 /* Increment LAST_TOKEN. */
496 lexer->last_token = cp_lexer_next_token (lexer, token);
498 /* Strings should have type `const char []'. Right now, we will
499 have an ARRAY_TYPE that is constant rather than an array of
501 FIXME: Make fix_string_type get this right in the first place. */
502 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
503 && flag_const_strings)
505 if (c_lex_string_translate)
507 tree value = token->value;
510 /* We might as well go ahead and release the chained
511 translated string such that we can reuse its memory. */
512 if (TREE_CHAIN (value))
513 value = TREE_CHAIN (token->value);
515 /* Get the current type. It will be an ARRAY_TYPE. */
516 type = TREE_TYPE (value);
517 /* Use build_cplus_array_type to rebuild the array, thereby
518 getting the right type. */
519 type = build_cplus_array_type (TREE_TYPE (type),
521 /* Reset the type of the token. */
522 TREE_TYPE (value) = type;
529 /* If the circular buffer is full, make it bigger. */
532 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
534 /* If the buffer is full, enlarge it. */
535 if (lexer->last_token == lexer->first_token)
537 cp_token *new_buffer;
538 cp_token *old_buffer;
539 cp_token *new_first_token;
540 ptrdiff_t buffer_length;
541 size_t num_tokens_to_copy;
543 /* Remember the current buffer pointer. It will become invalid,
544 but we will need to do pointer arithmetic involving this
546 old_buffer = lexer->buffer;
547 /* Compute the current buffer size. */
548 buffer_length = lexer->buffer_end - lexer->buffer;
549 /* Allocate a buffer twice as big. */
550 new_buffer = ggc_realloc (lexer->buffer,
551 2 * buffer_length * sizeof (cp_token));
553 /* Because the buffer is circular, logically consecutive tokens
554 are not necessarily placed consecutively in memory.
555 Therefore, we must keep move the tokens that were before
556 FIRST_TOKEN to the second half of the newly allocated
558 num_tokens_to_copy = (lexer->first_token - old_buffer);
559 memcpy (new_buffer + buffer_length,
561 num_tokens_to_copy * sizeof (cp_token));
562 /* Clear the rest of the buffer. We never look at this storage,
563 but the garbage collector may. */
564 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
565 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
567 /* Now recompute all of the buffer pointers. */
569 = new_buffer + (lexer->first_token - old_buffer);
570 if (lexer->next_token != NULL)
572 ptrdiff_t next_token_delta;
574 if (lexer->next_token > lexer->first_token)
575 next_token_delta = lexer->next_token - lexer->first_token;
578 buffer_length - (lexer->first_token - lexer->next_token);
579 lexer->next_token = new_first_token + next_token_delta;
581 lexer->last_token = new_first_token + buffer_length;
582 lexer->buffer = new_buffer;
583 lexer->buffer_end = new_buffer + buffer_length * 2;
584 lexer->first_token = new_first_token;
588 /* Store the next token from the preprocessor in *TOKEN. */
591 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
596 /* If this not the main lexer, return a terminating CPP_EOF token. */
597 if (lexer != NULL && !lexer->main_lexer_p)
599 token->type = CPP_EOF;
600 token->location.line = 0;
601 token->location.file = NULL;
602 token->value = NULL_TREE;
603 token->keyword = RID_MAX;
609 /* Keep going until we get a token we like. */
612 /* Get a new token from the preprocessor. */
613 token->type = c_lex_with_flags (&token->value, &token->flags);
614 /* Issue messages about tokens we cannot process. */
620 error ("invalid token");
624 /* This is a good token, so we exit the loop. */
629 /* Now we've got our token. */
630 token->location = input_location;
632 /* Check to see if this token is a keyword. */
633 if (token->type == CPP_NAME
634 && C_IS_RESERVED_WORD (token->value))
636 /* Mark this token as a keyword. */
637 token->type = CPP_KEYWORD;
638 /* Record which keyword. */
639 token->keyword = C_RID_CODE (token->value);
640 /* Update the value. Some keywords are mapped to particular
641 entities, rather than simply having the value of the
642 corresponding IDENTIFIER_NODE. For example, `__const' is
643 mapped to `const'. */
644 token->value = ridpointers[token->keyword];
647 token->keyword = RID_MAX;
650 /* Return a pointer to the next token in the token stream, but do not
654 cp_lexer_peek_token (cp_lexer* lexer)
658 /* If there are no tokens, read one now. */
659 if (!lexer->next_token)
660 cp_lexer_read_token (lexer);
662 /* Provide debugging output. */
663 if (cp_lexer_debugging_p (lexer))
665 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
666 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
667 fprintf (cp_lexer_debug_stream, "\n");
670 token = lexer->next_token;
671 cp_lexer_set_source_position_from_token (lexer, token);
675 /* Return true if the next token has the indicated TYPE. */
678 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
682 /* Peek at the next token. */
683 token = cp_lexer_peek_token (lexer);
684 /* Check to see if it has the indicated TYPE. */
685 return token->type == type;
688 /* Return true if the next token does not have the indicated TYPE. */
691 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
693 return !cp_lexer_next_token_is (lexer, type);
696 /* Return true if the next token is the indicated KEYWORD. */
699 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
703 /* Peek at the next token. */
704 token = cp_lexer_peek_token (lexer);
705 /* Check to see if it is the indicated keyword. */
706 return token->keyword == keyword;
709 /* Return a pointer to the Nth token in the token stream. If N is 1,
710 then this is precisely equivalent to cp_lexer_peek_token. */
713 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
717 /* N is 1-based, not zero-based. */
718 my_friendly_assert (n > 0, 20000224);
720 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
721 token = lexer->next_token;
722 /* If there are no tokens in the buffer, get one now. */
725 cp_lexer_read_token (lexer);
726 token = lexer->next_token;
729 /* Now, read tokens until we have enough. */
732 /* Advance to the next token. */
733 token = cp_lexer_next_token (lexer, token);
734 /* If that's all the tokens we have, read a new one. */
735 if (token == lexer->last_token)
736 token = cp_lexer_read_token (lexer);
742 /* Consume the next token. The pointer returned is valid only until
743 another token is read. Callers should preserve copy the token
744 explicitly if they will need its value for a longer period of
748 cp_lexer_consume_token (cp_lexer* lexer)
752 /* If there are no tokens, read one now. */
753 if (!lexer->next_token)
754 cp_lexer_read_token (lexer);
756 /* Remember the token we'll be returning. */
757 token = lexer->next_token;
759 /* Increment NEXT_TOKEN. */
760 lexer->next_token = cp_lexer_next_token (lexer,
762 /* Check to see if we're all out of tokens. */
763 if (lexer->next_token == lexer->last_token)
764 lexer->next_token = NULL;
766 /* If we're not saving tokens, then move FIRST_TOKEN too. */
767 if (!cp_lexer_saving_tokens (lexer))
769 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
770 if (!lexer->next_token)
771 lexer->first_token = NULL;
773 lexer->first_token = lexer->next_token;
776 /* Provide debugging output. */
777 if (cp_lexer_debugging_p (lexer))
779 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
780 cp_lexer_print_token (cp_lexer_debug_stream, token);
781 fprintf (cp_lexer_debug_stream, "\n");
787 /* Permanently remove the next token from the token stream. There
788 must be a valid next token already; this token never reads
789 additional tokens from the preprocessor. */
792 cp_lexer_purge_token (cp_lexer *lexer)
795 cp_token *next_token;
797 token = lexer->next_token;
800 next_token = cp_lexer_next_token (lexer, token);
801 if (next_token == lexer->last_token)
803 *token = *next_token;
807 lexer->last_token = token;
808 /* The token purged may have been the only token remaining; if so,
810 if (lexer->next_token == token)
811 lexer->next_token = NULL;
814 /* Permanently remove all tokens after TOKEN, up to, but not
815 including, the token that will be returned next by
816 cp_lexer_peek_token. */
819 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
825 if (lexer->next_token)
827 /* Copy the tokens that have not yet been read to the location
828 immediately following TOKEN. */
829 t1 = cp_lexer_next_token (lexer, token);
830 t2 = peek = cp_lexer_peek_token (lexer);
831 /* Move tokens into the vacant area between TOKEN and PEEK. */
832 while (t2 != lexer->last_token)
835 t1 = cp_lexer_next_token (lexer, t1);
836 t2 = cp_lexer_next_token (lexer, t2);
838 /* Now, the next available token is right after TOKEN. */
839 lexer->next_token = cp_lexer_next_token (lexer, token);
840 /* And the last token is wherever we ended up. */
841 lexer->last_token = t1;
845 /* There are no tokens in the buffer, so there is nothing to
846 copy. The last token in the buffer is TOKEN itself. */
847 lexer->last_token = cp_lexer_next_token (lexer, token);
851 /* Begin saving tokens. All tokens consumed after this point will be
855 cp_lexer_save_tokens (cp_lexer* lexer)
857 /* Provide debugging output. */
858 if (cp_lexer_debugging_p (lexer))
859 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
861 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
862 restore the tokens if required. */
863 if (!lexer->next_token)
864 cp_lexer_read_token (lexer);
866 VARRAY_PUSH_INT (lexer->saved_tokens,
867 cp_lexer_token_difference (lexer,
872 /* Commit to the portion of the token stream most recently saved. */
875 cp_lexer_commit_tokens (cp_lexer* lexer)
877 /* Provide debugging output. */
878 if (cp_lexer_debugging_p (lexer))
879 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
881 VARRAY_POP (lexer->saved_tokens);
884 /* Return all tokens saved since the last call to cp_lexer_save_tokens
885 to the token stream. Stop saving tokens. */
888 cp_lexer_rollback_tokens (cp_lexer* lexer)
892 /* Provide debugging output. */
893 if (cp_lexer_debugging_p (lexer))
894 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
896 /* Find the token that was the NEXT_TOKEN when we started saving
898 delta = VARRAY_TOP_INT(lexer->saved_tokens);
899 /* Make it the next token again now. */
900 lexer->next_token = cp_lexer_advance_token (lexer,
903 /* It might be the case that there were no tokens when we started
904 saving tokens, but that there are some tokens now. */
905 if (!lexer->next_token && lexer->first_token)
906 lexer->next_token = lexer->first_token;
908 /* Stop saving tokens. */
909 VARRAY_POP (lexer->saved_tokens);
912 /* Print a representation of the TOKEN on the STREAM. */
915 cp_lexer_print_token (FILE * stream, cp_token* token)
917 const char *token_type = NULL;
919 /* Figure out what kind of token this is. */
927 token_type = "COMMA";
931 token_type = "OPEN_PAREN";
934 case CPP_CLOSE_PAREN:
935 token_type = "CLOSE_PAREN";
939 token_type = "OPEN_BRACE";
942 case CPP_CLOSE_BRACE:
943 token_type = "CLOSE_BRACE";
947 token_type = "SEMICOLON";
959 token_type = "keyword";
962 /* This is not a token that we know how to handle yet. */
967 /* If we have a name for the token, print it out. Otherwise, we
968 simply give the numeric code. */
970 fprintf (stream, "%s", token_type);
972 fprintf (stream, "%d", token->type);
973 /* And, for an identifier, print the identifier name. */
974 if (token->type == CPP_NAME
975 /* Some keywords have a value that is not an IDENTIFIER_NODE.
976 For example, `struct' is mapped to an INTEGER_CST. */
977 || (token->type == CPP_KEYWORD
978 && TREE_CODE (token->value) == IDENTIFIER_NODE))
979 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
982 /* Start emitting debugging information. */
985 cp_lexer_start_debugging (cp_lexer* lexer)
987 ++lexer->debugging_p;
990 /* Stop emitting debugging information. */
993 cp_lexer_stop_debugging (cp_lexer* lexer)
995 --lexer->debugging_p;
999 /* Decl-specifiers. */
1001 static void clear_decl_specs
1002 (cp_decl_specifier_seq *);
1004 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1007 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1009 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1014 /* Nothing other than the parser should be creating declarators;
1015 declarators are a semi-syntactic representation of C++ entities.
1016 Other parts of the front end that need to create entities (like
1017 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1019 static cp_declarator *make_id_declarator
1021 static cp_declarator *make_call_declarator
1022 (cp_declarator *, cp_parameter_declarator *, tree, tree);
1023 static cp_declarator *make_array_declarator
1024 (cp_declarator *, tree);
1025 static cp_declarator *make_pointer_declarator
1026 (tree, cp_declarator *);
1027 static cp_declarator *make_reference_declarator
1028 (tree, cp_declarator *);
1029 static cp_parameter_declarator *make_parameter_declarator
1030 (cp_decl_specifier_seq *, cp_declarator *, tree);
1031 static cp_declarator *make_ptrmem_declarator
1032 (tree, tree, cp_declarator *);
1034 cp_declarator *cp_error_declarator;
1036 /* The obstack on which declarators and related data structures are
1038 static struct obstack declarator_obstack;
1040 /* Alloc BYTES from the declarator memory pool. */
1042 static inline void *
1043 alloc_declarator (size_t bytes)
1045 return obstack_alloc (&declarator_obstack, bytes);
1048 /* Allocate a declarator of the indicated KIND. Clear fields that are
1049 common to all declarators. */
1051 static cp_declarator *
1052 make_declarator (cp_declarator_kind kind)
1054 cp_declarator *declarator;
1056 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1057 declarator->kind = kind;
1058 declarator->attributes = NULL_TREE;
1059 declarator->declarator = NULL;
1064 /* Make a declarator for a generalized identifier. */
1067 make_id_declarator (tree id)
1069 cp_declarator *declarator;
1071 declarator = make_declarator (cdk_id);
1072 declarator->u.id.name = id;
1073 declarator->u.id.sfk = sfk_none;
1078 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1079 of modifiers such as const or volatile to apply to the pointer
1080 type, represented as identifiers. */
1083 make_pointer_declarator (tree cv_qualifiers, cp_declarator *target)
1085 cp_declarator *declarator;
1087 declarator = make_declarator (cdk_pointer);
1088 declarator->declarator = target;
1089 declarator->u.pointer.qualifiers = cv_qualifiers;
1090 declarator->u.pointer.class_type = NULL_TREE;
1095 /* Like make_pointer_declarator -- but for references. */
1098 make_reference_declarator (tree cv_qualifiers, cp_declarator *target)
1100 cp_declarator *declarator;
1102 declarator = make_declarator (cdk_reference);
1103 declarator->declarator = target;
1104 declarator->u.pointer.qualifiers = cv_qualifiers;
1105 declarator->u.pointer.class_type = NULL_TREE;
1110 /* Like make_pointer_declarator -- but for a pointer to a non-static
1111 member of CLASS_TYPE. */
1114 make_ptrmem_declarator (tree cv_qualifiers, tree class_type,
1115 cp_declarator *pointee)
1117 cp_declarator *declarator;
1119 declarator = make_declarator (cdk_ptrmem);
1120 declarator->declarator = pointee;
1121 declarator->u.pointer.qualifiers = cv_qualifiers;
1122 declarator->u.pointer.class_type = class_type;
1127 /* Make a declarator for the function given by TARGET, with the
1128 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1129 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1130 indicates what exceptions can be thrown. */
1133 make_call_declarator (cp_declarator *target,
1134 cp_parameter_declarator *parms,
1136 tree exception_specification)
1138 cp_declarator *declarator;
1140 declarator = make_declarator (cdk_function);
1141 declarator->declarator = target;
1142 declarator->u.function.parameters = parms;
1143 declarator->u.function.qualifiers = cv_qualifiers;
1144 declarator->u.function.exception_specification = exception_specification;
1149 /* Make a declarator for an array of BOUNDS elements, each of which is
1150 defined by ELEMENT. */
1153 make_array_declarator (cp_declarator *element, tree bounds)
1155 cp_declarator *declarator;
1157 declarator = make_declarator (cdk_array);
1158 declarator->declarator = element;
1159 declarator->u.array.bounds = bounds;
1164 cp_parameter_declarator *no_parameters;
1166 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1167 DECLARATOR and DEFAULT_ARGUMENT. */
1169 cp_parameter_declarator *
1170 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1171 cp_declarator *declarator,
1172 tree default_argument)
1174 cp_parameter_declarator *parameter;
1176 parameter = ((cp_parameter_declarator *)
1177 alloc_declarator (sizeof (cp_parameter_declarator)));
1178 parameter->next = NULL;
1179 if (decl_specifiers)
1180 parameter->decl_specifiers = *decl_specifiers;
1182 clear_decl_specs (¶meter->decl_specifiers);
1183 parameter->declarator = declarator;
1184 parameter->default_argument = default_argument;
1185 parameter->ellipsis_p = false;
1195 A cp_parser parses the token stream as specified by the C++
1196 grammar. Its job is purely parsing, not semantic analysis. For
1197 example, the parser breaks the token stream into declarators,
1198 expressions, statements, and other similar syntactic constructs.
1199 It does not check that the types of the expressions on either side
1200 of an assignment-statement are compatible, or that a function is
1201 not declared with a parameter of type `void'.
1203 The parser invokes routines elsewhere in the compiler to perform
1204 semantic analysis and to build up the abstract syntax tree for the
1207 The parser (and the template instantiation code, which is, in a
1208 way, a close relative of parsing) are the only parts of the
1209 compiler that should be calling push_scope and pop_scope, or
1210 related functions. The parser (and template instantiation code)
1211 keeps track of what scope is presently active; everything else
1212 should simply honor that. (The code that generates static
1213 initializers may also need to set the scope, in order to check
1214 access control correctly when emitting the initializers.)
1219 The parser is of the standard recursive-descent variety. Upcoming
1220 tokens in the token stream are examined in order to determine which
1221 production to use when parsing a non-terminal. Some C++ constructs
1222 require arbitrary look ahead to disambiguate. For example, it is
1223 impossible, in the general case, to tell whether a statement is an
1224 expression or declaration without scanning the entire statement.
1225 Therefore, the parser is capable of "parsing tentatively." When the
1226 parser is not sure what construct comes next, it enters this mode.
1227 Then, while we attempt to parse the construct, the parser queues up
1228 error messages, rather than issuing them immediately, and saves the
1229 tokens it consumes. If the construct is parsed successfully, the
1230 parser "commits", i.e., it issues any queued error messages and
1231 the tokens that were being preserved are permanently discarded.
1232 If, however, the construct is not parsed successfully, the parser
1233 rolls back its state completely so that it can resume parsing using
1234 a different alternative.
1239 The performance of the parser could probably be improved
1240 substantially. Some possible improvements include:
1242 - The expression parser recurses through the various levels of
1243 precedence as specified in the grammar, rather than using an
1244 operator-precedence technique. Therefore, parsing a simple
1245 identifier requires multiple recursive calls.
1247 - We could often eliminate the need to parse tentatively by
1248 looking ahead a little bit. In some places, this approach
1249 might not entirely eliminate the need to parse tentatively, but
1250 it might still speed up the average case. */
1252 /* Flags that are passed to some parsing functions. These values can
1253 be bitwise-ored together. */
1255 typedef enum cp_parser_flags
1258 CP_PARSER_FLAGS_NONE = 0x0,
1259 /* The construct is optional. If it is not present, then no error
1260 should be issued. */
1261 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1262 /* When parsing a type-specifier, do not allow user-defined types. */
1263 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1266 /* The different kinds of declarators we want to parse. */
1268 typedef enum cp_parser_declarator_kind
1270 /* We want an abstract declarator. */
1271 CP_PARSER_DECLARATOR_ABSTRACT,
1272 /* We want a named declarator. */
1273 CP_PARSER_DECLARATOR_NAMED,
1274 /* We don't mind, but the name must be an unqualified-id. */
1275 CP_PARSER_DECLARATOR_EITHER
1276 } cp_parser_declarator_kind;
1278 /* A mapping from a token type to a corresponding tree node type. */
1280 typedef struct cp_parser_token_tree_map_node
1282 /* The token type. */
1283 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1284 /* The corresponding tree code. */
1285 ENUM_BITFIELD (tree_code) tree_type : 8;
1286 } cp_parser_token_tree_map_node;
1288 /* A complete map consists of several ordinary entries, followed by a
1289 terminator. The terminating entry has a token_type of CPP_EOF. */
1291 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1293 /* The status of a tentative parse. */
1295 typedef enum cp_parser_status_kind
1297 /* No errors have occurred. */
1298 CP_PARSER_STATUS_KIND_NO_ERROR,
1299 /* An error has occurred. */
1300 CP_PARSER_STATUS_KIND_ERROR,
1301 /* We are committed to this tentative parse, whether or not an error
1303 CP_PARSER_STATUS_KIND_COMMITTED
1304 } cp_parser_status_kind;
1306 /* Context that is saved and restored when parsing tentatively. */
1308 typedef struct cp_parser_context GTY (())
1310 /* If this is a tentative parsing context, the status of the
1312 enum cp_parser_status_kind status;
1313 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1314 that are looked up in this context must be looked up both in the
1315 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1316 the context of the containing expression. */
1318 /* The next parsing context in the stack. */
1319 struct cp_parser_context *next;
1320 } cp_parser_context;
1324 /* Constructors and destructors. */
1326 static cp_parser_context *cp_parser_context_new
1327 (cp_parser_context *);
1329 /* Class variables. */
1331 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1333 /* Constructors and destructors. */
1335 /* Construct a new context. The context below this one on the stack
1336 is given by NEXT. */
1338 static cp_parser_context *
1339 cp_parser_context_new (cp_parser_context* next)
1341 cp_parser_context *context;
1343 /* Allocate the storage. */
1344 if (cp_parser_context_free_list != NULL)
1346 /* Pull the first entry from the free list. */
1347 context = cp_parser_context_free_list;
1348 cp_parser_context_free_list = context->next;
1349 memset (context, 0, sizeof (*context));
1352 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1353 /* No errors have occurred yet in this context. */
1354 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1355 /* If this is not the bottomost context, copy information that we
1356 need from the previous context. */
1359 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1360 expression, then we are parsing one in this context, too. */
1361 context->object_type = next->object_type;
1362 /* Thread the stack. */
1363 context->next = next;
1369 /* The cp_parser structure represents the C++ parser. */
1371 typedef struct cp_parser GTY(())
1373 /* The lexer from which we are obtaining tokens. */
1376 /* The scope in which names should be looked up. If NULL_TREE, then
1377 we look up names in the scope that is currently open in the
1378 source program. If non-NULL, this is either a TYPE or
1379 NAMESPACE_DECL for the scope in which we should look.
1381 This value is not cleared automatically after a name is looked
1382 up, so we must be careful to clear it before starting a new look
1383 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1384 will look up `Z' in the scope of `X', rather than the current
1385 scope.) Unfortunately, it is difficult to tell when name lookup
1386 is complete, because we sometimes peek at a token, look it up,
1387 and then decide not to consume it. */
1390 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1391 last lookup took place. OBJECT_SCOPE is used if an expression
1392 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1393 respectively. QUALIFYING_SCOPE is used for an expression of the
1394 form "X::Y"; it refers to X. */
1396 tree qualifying_scope;
1398 /* A stack of parsing contexts. All but the bottom entry on the
1399 stack will be tentative contexts.
1401 We parse tentatively in order to determine which construct is in
1402 use in some situations. For example, in order to determine
1403 whether a statement is an expression-statement or a
1404 declaration-statement we parse it tentatively as a
1405 declaration-statement. If that fails, we then reparse the same
1406 token stream as an expression-statement. */
1407 cp_parser_context *context;
1409 /* True if we are parsing GNU C++. If this flag is not set, then
1410 GNU extensions are not recognized. */
1411 bool allow_gnu_extensions_p;
1413 /* TRUE if the `>' token should be interpreted as the greater-than
1414 operator. FALSE if it is the end of a template-id or
1415 template-parameter-list. */
1416 bool greater_than_is_operator_p;
1418 /* TRUE if default arguments are allowed within a parameter list
1419 that starts at this point. FALSE if only a gnu extension makes
1420 them permissible. */
1421 bool default_arg_ok_p;
1423 /* TRUE if we are parsing an integral constant-expression. See
1424 [expr.const] for a precise definition. */
1425 bool integral_constant_expression_p;
1427 /* TRUE if we are parsing an integral constant-expression -- but a
1428 non-constant expression should be permitted as well. This flag
1429 is used when parsing an array bound so that GNU variable-length
1430 arrays are tolerated. */
1431 bool allow_non_integral_constant_expression_p;
1433 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1434 been seen that makes the expression non-constant. */
1435 bool non_integral_constant_expression_p;
1437 /* TRUE if local variable names and `this' are forbidden in the
1439 bool local_variables_forbidden_p;
1441 /* TRUE if the declaration we are parsing is part of a
1442 linkage-specification of the form `extern string-literal
1444 bool in_unbraced_linkage_specification_p;
1446 /* TRUE if we are presently parsing a declarator, after the
1447 direct-declarator. */
1448 bool in_declarator_p;
1450 /* TRUE if we are presently parsing a template-argument-list. */
1451 bool in_template_argument_list_p;
1453 /* TRUE if we are presently parsing the body of an
1454 iteration-statement. */
1455 bool in_iteration_statement_p;
1457 /* TRUE if we are presently parsing the body of a switch
1459 bool in_switch_statement_p;
1461 /* TRUE if we are parsing a type-id in an expression context. In
1462 such a situation, both "type (expr)" and "type (type)" are valid
1464 bool in_type_id_in_expr_p;
1466 /* If non-NULL, then we are parsing a construct where new type
1467 definitions are not permitted. The string stored here will be
1468 issued as an error message if a type is defined. */
1469 const char *type_definition_forbidden_message;
1471 /* A list of lists. The outer list is a stack, used for member
1472 functions of local classes. At each level there are two sub-list,
1473 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1474 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1475 TREE_VALUE's. The functions are chained in reverse declaration
1478 The TREE_PURPOSE sublist contains those functions with default
1479 arguments that need post processing, and the TREE_VALUE sublist
1480 contains those functions with definitions that need post
1483 These lists can only be processed once the outermost class being
1484 defined is complete. */
1485 tree unparsed_functions_queues;
1487 /* The number of classes whose definitions are currently in
1489 unsigned num_classes_being_defined;
1491 /* The number of template parameter lists that apply directly to the
1492 current declaration. */
1493 unsigned num_template_parameter_lists;
1496 /* The type of a function that parses some kind of expression. */
1497 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1501 /* Constructors and destructors. */
1503 static cp_parser *cp_parser_new
1506 /* Routines to parse various constructs.
1508 Those that return `tree' will return the error_mark_node (rather
1509 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1510 Sometimes, they will return an ordinary node if error-recovery was
1511 attempted, even though a parse error occurred. So, to check
1512 whether or not a parse error occurred, you should always use
1513 cp_parser_error_occurred. If the construct is optional (indicated
1514 either by an `_opt' in the name of the function that does the
1515 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1516 the construct is not present. */
1518 /* Lexical conventions [gram.lex] */
1520 static tree cp_parser_identifier
1523 /* Basic concepts [gram.basic] */
1525 static bool cp_parser_translation_unit
1528 /* Expressions [gram.expr] */
1530 static tree cp_parser_primary_expression
1531 (cp_parser *, cp_id_kind *, tree *);
1532 static tree cp_parser_id_expression
1533 (cp_parser *, bool, bool, bool *, bool);
1534 static tree cp_parser_unqualified_id
1535 (cp_parser *, bool, bool, bool);
1536 static tree cp_parser_nested_name_specifier_opt
1537 (cp_parser *, bool, bool, bool, bool);
1538 static tree cp_parser_nested_name_specifier
1539 (cp_parser *, bool, bool, bool, bool);
1540 static tree cp_parser_class_or_namespace_name
1541 (cp_parser *, bool, bool, bool, bool, bool);
1542 static tree cp_parser_postfix_expression
1543 (cp_parser *, bool);
1544 static tree cp_parser_postfix_open_square_expression
1545 (cp_parser *, tree, bool);
1546 static tree cp_parser_postfix_dot_deref_expression
1547 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1548 static tree cp_parser_parenthesized_expression_list
1549 (cp_parser *, bool, bool *);
1550 static void cp_parser_pseudo_destructor_name
1551 (cp_parser *, tree *, tree *);
1552 static tree cp_parser_unary_expression
1553 (cp_parser *, bool);
1554 static enum tree_code cp_parser_unary_operator
1556 static tree cp_parser_new_expression
1558 static tree cp_parser_new_placement
1560 static tree cp_parser_new_type_id
1561 (cp_parser *, tree *);
1562 static cp_declarator *cp_parser_new_declarator_opt
1564 static cp_declarator *cp_parser_direct_new_declarator
1566 static tree cp_parser_new_initializer
1568 static tree cp_parser_delete_expression
1570 static tree cp_parser_cast_expression
1571 (cp_parser *, bool);
1572 static tree cp_parser_pm_expression
1574 static tree cp_parser_multiplicative_expression
1576 static tree cp_parser_additive_expression
1578 static tree cp_parser_shift_expression
1580 static tree cp_parser_relational_expression
1582 static tree cp_parser_equality_expression
1584 static tree cp_parser_and_expression
1586 static tree cp_parser_exclusive_or_expression
1588 static tree cp_parser_inclusive_or_expression
1590 static tree cp_parser_logical_and_expression
1592 static tree cp_parser_logical_or_expression
1594 static tree cp_parser_question_colon_clause
1595 (cp_parser *, tree);
1596 static tree cp_parser_assignment_expression
1598 static enum tree_code cp_parser_assignment_operator_opt
1600 static tree cp_parser_expression
1602 static tree cp_parser_constant_expression
1603 (cp_parser *, bool, bool *);
1604 static tree cp_parser_builtin_offsetof
1607 /* Statements [gram.stmt.stmt] */
1609 static void cp_parser_statement
1610 (cp_parser *, tree);
1611 static tree cp_parser_labeled_statement
1612 (cp_parser *, tree);
1613 static tree cp_parser_expression_statement
1614 (cp_parser *, tree);
1615 static tree cp_parser_compound_statement
1616 (cp_parser *, tree, bool);
1617 static void cp_parser_statement_seq_opt
1618 (cp_parser *, tree);
1619 static tree cp_parser_selection_statement
1621 static tree cp_parser_condition
1623 static tree cp_parser_iteration_statement
1625 static void cp_parser_for_init_statement
1627 static tree cp_parser_jump_statement
1629 static void cp_parser_declaration_statement
1632 static tree cp_parser_implicitly_scoped_statement
1634 static void cp_parser_already_scoped_statement
1637 /* Declarations [gram.dcl.dcl] */
1639 static void cp_parser_declaration_seq_opt
1641 static void cp_parser_declaration
1643 static void cp_parser_block_declaration
1644 (cp_parser *, bool);
1645 static void cp_parser_simple_declaration
1646 (cp_parser *, bool);
1647 static void cp_parser_decl_specifier_seq
1648 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1649 static tree cp_parser_storage_class_specifier_opt
1651 static tree cp_parser_function_specifier_opt
1652 (cp_parser *, cp_decl_specifier_seq *);
1653 static tree cp_parser_type_specifier
1654 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1656 static tree cp_parser_simple_type_specifier
1657 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1658 static tree cp_parser_type_name
1660 static tree cp_parser_elaborated_type_specifier
1661 (cp_parser *, bool, bool);
1662 static tree cp_parser_enum_specifier
1664 static void cp_parser_enumerator_list
1665 (cp_parser *, tree);
1666 static void cp_parser_enumerator_definition
1667 (cp_parser *, tree);
1668 static tree cp_parser_namespace_name
1670 static void cp_parser_namespace_definition
1672 static void cp_parser_namespace_body
1674 static tree cp_parser_qualified_namespace_specifier
1676 static void cp_parser_namespace_alias_definition
1678 static void cp_parser_using_declaration
1680 static void cp_parser_using_directive
1682 static void cp_parser_asm_definition
1684 static void cp_parser_linkage_specification
1687 /* Declarators [gram.dcl.decl] */
1689 static tree cp_parser_init_declarator
1690 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1691 static cp_declarator *cp_parser_declarator
1692 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1693 static cp_declarator *cp_parser_direct_declarator
1694 (cp_parser *, cp_parser_declarator_kind, int *);
1695 static enum tree_code cp_parser_ptr_operator
1696 (cp_parser *, tree *, tree *);
1697 static tree cp_parser_cv_qualifier_seq_opt
1699 static tree cp_parser_cv_qualifier_opt
1701 static tree cp_parser_declarator_id
1703 static tree cp_parser_type_id
1705 static void cp_parser_type_specifier_seq
1706 (cp_parser *, cp_decl_specifier_seq *);
1707 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1709 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1710 (cp_parser *, bool *);
1711 static cp_parameter_declarator *cp_parser_parameter_declaration
1712 (cp_parser *, bool, bool *);
1713 static void cp_parser_function_body
1715 static tree cp_parser_initializer
1716 (cp_parser *, bool *, bool *);
1717 static tree cp_parser_initializer_clause
1718 (cp_parser *, bool *);
1719 static tree cp_parser_initializer_list
1720 (cp_parser *, bool *);
1722 static bool cp_parser_ctor_initializer_opt_and_function_body
1725 /* Classes [gram.class] */
1727 static tree cp_parser_class_name
1728 (cp_parser *, bool, bool, bool, bool, bool, bool);
1729 static tree cp_parser_class_specifier
1731 static tree cp_parser_class_head
1732 (cp_parser *, bool *, tree *);
1733 static enum tag_types cp_parser_class_key
1735 static void cp_parser_member_specification_opt
1737 static void cp_parser_member_declaration
1739 static tree cp_parser_pure_specifier
1741 static tree cp_parser_constant_initializer
1744 /* Derived classes [gram.class.derived] */
1746 static tree cp_parser_base_clause
1748 static tree cp_parser_base_specifier
1751 /* Special member functions [gram.special] */
1753 static tree cp_parser_conversion_function_id
1755 static tree cp_parser_conversion_type_id
1757 static cp_declarator *cp_parser_conversion_declarator_opt
1759 static bool cp_parser_ctor_initializer_opt
1761 static void cp_parser_mem_initializer_list
1763 static tree cp_parser_mem_initializer
1765 static tree cp_parser_mem_initializer_id
1768 /* Overloading [gram.over] */
1770 static tree cp_parser_operator_function_id
1772 static tree cp_parser_operator
1775 /* Templates [gram.temp] */
1777 static void cp_parser_template_declaration
1778 (cp_parser *, bool);
1779 static tree cp_parser_template_parameter_list
1781 static tree cp_parser_template_parameter
1782 (cp_parser *, bool *);
1783 static tree cp_parser_type_parameter
1785 static tree cp_parser_template_id
1786 (cp_parser *, bool, bool, bool);
1787 static tree cp_parser_template_name
1788 (cp_parser *, bool, bool, bool, bool *);
1789 static tree cp_parser_template_argument_list
1791 static tree cp_parser_template_argument
1793 static void cp_parser_explicit_instantiation
1795 static void cp_parser_explicit_specialization
1798 /* Exception handling [gram.exception] */
1800 static tree cp_parser_try_block
1802 static bool cp_parser_function_try_block
1804 static void cp_parser_handler_seq
1806 static void cp_parser_handler
1808 static tree cp_parser_exception_declaration
1810 static tree cp_parser_throw_expression
1812 static tree cp_parser_exception_specification_opt
1814 static tree cp_parser_type_id_list
1817 /* GNU Extensions */
1819 static tree cp_parser_asm_specification_opt
1821 static tree cp_parser_asm_operand_list
1823 static tree cp_parser_asm_clobber_list
1825 static tree cp_parser_attributes_opt
1827 static tree cp_parser_attribute_list
1829 static bool cp_parser_extension_opt
1830 (cp_parser *, int *);
1831 static void cp_parser_label_declaration
1834 /* Utility Routines */
1836 static tree cp_parser_lookup_name
1837 (cp_parser *, tree, bool, bool, bool, bool);
1838 static tree cp_parser_lookup_name_simple
1839 (cp_parser *, tree);
1840 static tree cp_parser_maybe_treat_template_as_class
1842 static bool cp_parser_check_declarator_template_parameters
1843 (cp_parser *, cp_declarator *);
1844 static bool cp_parser_check_template_parameters
1845 (cp_parser *, unsigned);
1846 static tree cp_parser_simple_cast_expression
1848 static tree cp_parser_binary_expression
1849 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1850 static tree cp_parser_global_scope_opt
1851 (cp_parser *, bool);
1852 static bool cp_parser_constructor_declarator_p
1853 (cp_parser *, bool);
1854 static tree cp_parser_function_definition_from_specifiers_and_declarator
1855 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1856 static tree cp_parser_function_definition_after_declarator
1857 (cp_parser *, bool);
1858 static void cp_parser_template_declaration_after_export
1859 (cp_parser *, bool);
1860 static tree cp_parser_single_declaration
1861 (cp_parser *, bool, bool *);
1862 static tree cp_parser_functional_cast
1863 (cp_parser *, tree);
1864 static tree cp_parser_save_member_function_body
1865 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1866 static tree cp_parser_enclosed_template_argument_list
1868 static void cp_parser_save_default_args
1869 (cp_parser *, tree);
1870 static void cp_parser_late_parsing_for_member
1871 (cp_parser *, tree);
1872 static void cp_parser_late_parsing_default_args
1873 (cp_parser *, tree);
1874 static tree cp_parser_sizeof_operand
1875 (cp_parser *, enum rid);
1876 static bool cp_parser_declares_only_class_p
1878 static void cp_parser_set_storage_class
1879 (cp_decl_specifier_seq *, cp_storage_class);
1880 static void cp_parser_set_decl_spec_type
1881 (cp_decl_specifier_seq *, tree, bool);
1882 static bool cp_parser_friend_p
1883 (const cp_decl_specifier_seq *);
1884 static cp_token *cp_parser_require
1885 (cp_parser *, enum cpp_ttype, const char *);
1886 static cp_token *cp_parser_require_keyword
1887 (cp_parser *, enum rid, const char *);
1888 static bool cp_parser_token_starts_function_definition_p
1890 static bool cp_parser_next_token_starts_class_definition_p
1892 static bool cp_parser_next_token_ends_template_argument_p
1894 static bool cp_parser_nth_token_starts_template_argument_list_p
1895 (cp_parser *, size_t);
1896 static enum tag_types cp_parser_token_is_class_key
1898 static void cp_parser_check_class_key
1899 (enum tag_types, tree type);
1900 static void cp_parser_check_access_in_redeclaration
1902 static bool cp_parser_optional_template_keyword
1904 static void cp_parser_pre_parsed_nested_name_specifier
1906 static void cp_parser_cache_group
1907 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1908 static void cp_parser_parse_tentatively
1910 static void cp_parser_commit_to_tentative_parse
1912 static void cp_parser_abort_tentative_parse
1914 static bool cp_parser_parse_definitely
1916 static inline bool cp_parser_parsing_tentatively
1918 static bool cp_parser_committed_to_tentative_parse
1920 static void cp_parser_error
1921 (cp_parser *, const char *);
1922 static void cp_parser_name_lookup_error
1923 (cp_parser *, tree, tree, const char *);
1924 static bool cp_parser_simulate_error
1926 static void cp_parser_check_type_definition
1928 static void cp_parser_check_for_definition_in_return_type
1929 (cp_declarator *, int);
1930 static void cp_parser_check_for_invalid_template_id
1931 (cp_parser *, tree);
1932 static bool cp_parser_non_integral_constant_expression
1933 (cp_parser *, const char *);
1934 static void cp_parser_diagnose_invalid_type_name
1935 (cp_parser *, tree, tree);
1936 static bool cp_parser_parse_and_diagnose_invalid_type_name
1938 static int cp_parser_skip_to_closing_parenthesis
1939 (cp_parser *, bool, bool, bool);
1940 static void cp_parser_skip_to_end_of_statement
1942 static void cp_parser_consume_semicolon_at_end_of_statement
1944 static void cp_parser_skip_to_end_of_block_or_statement
1946 static void cp_parser_skip_to_closing_brace
1948 static void cp_parser_skip_until_found
1949 (cp_parser *, enum cpp_ttype, const char *);
1950 static bool cp_parser_error_occurred
1952 static bool cp_parser_allow_gnu_extensions_p
1954 static bool cp_parser_is_string_literal
1956 static bool cp_parser_is_keyword
1957 (cp_token *, enum rid);
1958 static tree cp_parser_make_typename_type
1959 (cp_parser *, tree, tree);
1961 /* Returns nonzero if we are parsing tentatively. */
1964 cp_parser_parsing_tentatively (cp_parser* parser)
1966 return parser->context->next != NULL;
1969 /* Returns nonzero if TOKEN is a string literal. */
1972 cp_parser_is_string_literal (cp_token* token)
1974 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1977 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1980 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1982 return token->keyword == keyword;
1985 /* Issue the indicated error MESSAGE. */
1988 cp_parser_error (cp_parser* parser, const char* message)
1990 /* Output the MESSAGE -- unless we're parsing tentatively. */
1991 if (!cp_parser_simulate_error (parser))
1994 token = cp_lexer_peek_token (parser->lexer);
1995 c_parse_error (message,
1996 /* Because c_parser_error does not understand
1997 CPP_KEYWORD, keywords are treated like
1999 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2004 /* Issue an error about name-lookup failing. NAME is the
2005 IDENTIFIER_NODE DECL is the result of
2006 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2007 the thing that we hoped to find. */
2010 cp_parser_name_lookup_error (cp_parser* parser,
2013 const char* desired)
2015 /* If name lookup completely failed, tell the user that NAME was not
2017 if (decl == error_mark_node)
2019 if (parser->scope && parser->scope != global_namespace)
2020 error ("`%D::%D' has not been declared",
2021 parser->scope, name);
2022 else if (parser->scope == global_namespace)
2023 error ("`::%D' has not been declared", name);
2025 error ("`%D' has not been declared", name);
2027 else if (parser->scope && parser->scope != global_namespace)
2028 error ("`%D::%D' %s", parser->scope, name, desired);
2029 else if (parser->scope == global_namespace)
2030 error ("`::%D' %s", name, desired);
2032 error ("`%D' %s", name, desired);
2035 /* If we are parsing tentatively, remember that an error has occurred
2036 during this tentative parse. Returns true if the error was
2037 simulated; false if a message should be issued by the caller. */
2040 cp_parser_simulate_error (cp_parser* parser)
2042 if (cp_parser_parsing_tentatively (parser)
2043 && !cp_parser_committed_to_tentative_parse (parser))
2045 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2051 /* This function is called when a type is defined. If type
2052 definitions are forbidden at this point, an error message is
2056 cp_parser_check_type_definition (cp_parser* parser)
2058 /* If types are forbidden here, issue a message. */
2059 if (parser->type_definition_forbidden_message)
2060 /* Use `%s' to print the string in case there are any escape
2061 characters in the message. */
2062 error ("%s", parser->type_definition_forbidden_message);
2065 /* This function is called when a declaration is parsed. If
2066 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
2067 indicates that a type was defined in the decl-specifiers for DECL,
2068 then an error is issued. */
2071 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2072 int declares_class_or_enum)
2074 /* [dcl.fct] forbids type definitions in return types.
2075 Unfortunately, it's not easy to know whether or not we are
2076 processing a return type until after the fact. */
2078 && (declarator->kind == cdk_pointer
2079 || declarator->kind == cdk_reference
2080 || declarator->kind == cdk_ptrmem))
2081 declarator = declarator->declarator;
2083 && declarator->kind == cdk_function
2084 && declares_class_or_enum & 2)
2085 error ("new types may not be defined in a return type");
2088 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2089 "<" in any valid C++ program. If the next token is indeed "<",
2090 issue a message warning the user about what appears to be an
2091 invalid attempt to form a template-id. */
2094 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2100 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2103 error ("`%T' is not a template", type);
2104 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2105 error ("`%E' is not a template", type);
2107 error ("invalid template-id");
2108 /* Remember the location of the invalid "<". */
2109 if (cp_parser_parsing_tentatively (parser)
2110 && !cp_parser_committed_to_tentative_parse (parser))
2112 token = cp_lexer_peek_token (parser->lexer);
2113 token = cp_lexer_prev_token (parser->lexer, token);
2114 start = cp_lexer_token_difference (parser->lexer,
2115 parser->lexer->first_token,
2120 /* Consume the "<". */
2121 cp_lexer_consume_token (parser->lexer);
2122 /* Parse the template arguments. */
2123 cp_parser_enclosed_template_argument_list (parser);
2124 /* Permanently remove the invalid template arguments so that
2125 this error message is not issued again. */
2128 token = cp_lexer_advance_token (parser->lexer,
2129 parser->lexer->first_token,
2131 cp_lexer_purge_tokens_after (parser->lexer, token);
2136 /* If parsing an integral constant-expression, issue an error message
2137 about the fact that THING appeared and return true. Otherwise,
2138 return false, marking the current expression as non-constant. */
2141 cp_parser_non_integral_constant_expression (cp_parser *parser,
2144 if (parser->integral_constant_expression_p)
2146 if (!parser->allow_non_integral_constant_expression_p)
2148 error ("%s cannot appear in a constant-expression", thing);
2151 parser->non_integral_constant_expression_p = true;
2156 /* Emit a diagnostic for an invalid type name. Consider also if it is
2157 qualified or not and the result of a lookup, to provide a better
2161 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2163 tree decl, old_scope;
2164 /* Try to lookup the identifier. */
2165 old_scope = parser->scope;
2166 parser->scope = scope;
2167 decl = cp_parser_lookup_name_simple (parser, id);
2168 parser->scope = old_scope;
2169 /* If the lookup found a template-name, it means that the user forgot
2170 to specify an argument list. Emit an useful error message. */
2171 if (TREE_CODE (decl) == TEMPLATE_DECL)
2172 error ("invalid use of template-name `%E' without an argument list",
2174 else if (!parser->scope)
2176 /* Issue an error message. */
2177 error ("`%E' does not name a type", id);
2178 /* If we're in a template class, it's possible that the user was
2179 referring to a type from a base class. For example:
2181 template <typename T> struct A { typedef T X; };
2182 template <typename T> struct B : public A<T> { X x; };
2184 The user should have said "typename A<T>::X". */
2185 if (processing_template_decl && current_class_type)
2189 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2193 tree base_type = BINFO_TYPE (b);
2194 if (CLASS_TYPE_P (base_type)
2195 && dependent_type_p (base_type))
2198 /* Go from a particular instantiation of the
2199 template (which will have an empty TYPE_FIELDs),
2200 to the main version. */
2201 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2202 for (field = TYPE_FIELDS (base_type);
2204 field = TREE_CHAIN (field))
2205 if (TREE_CODE (field) == TYPE_DECL
2206 && DECL_NAME (field) == id)
2208 inform ("(perhaps `typename %T::%E' was intended)",
2209 BINFO_TYPE (b), id);
2218 /* Here we diagnose qualified-ids where the scope is actually correct,
2219 but the identifier does not resolve to a valid type name. */
2222 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2223 error ("`%E' in namespace `%E' does not name a type",
2225 else if (TYPE_P (parser->scope))
2226 error ("`%E' in class `%T' does not name a type",
2233 /* Check for a common situation where a type-name should be present,
2234 but is not, and issue a sensible error message. Returns true if an
2235 invalid type-name was detected.
2237 The situation handled by this function are variable declarations of the
2238 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2239 Usually, `ID' should name a type, but if we got here it means that it
2240 does not. We try to emit the best possible error message depending on
2241 how exactly the id-expression looks like.
2245 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2249 cp_parser_parse_tentatively (parser);
2250 id = cp_parser_id_expression (parser,
2251 /*template_keyword_p=*/false,
2252 /*check_dependency_p=*/true,
2253 /*template_p=*/NULL,
2254 /*declarator_p=*/true);
2255 /* After the id-expression, there should be a plain identifier,
2256 otherwise this is not a simple variable declaration. Also, if
2257 the scope is dependent, we cannot do much. */
2258 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2259 || (parser->scope && TYPE_P (parser->scope)
2260 && dependent_type_p (parser->scope)))
2262 cp_parser_abort_tentative_parse (parser);
2265 if (!cp_parser_parse_definitely (parser))
2268 /* If we got here, this cannot be a valid variable declaration, thus
2269 the cp_parser_id_expression must have resolved to a plain identifier
2270 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2271 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2272 /* Emit a diagnostic for the invalid type. */
2273 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2274 /* Skip to the end of the declaration; there's no point in
2275 trying to process it. */
2276 cp_parser_skip_to_end_of_block_or_statement (parser);
2280 /* Consume tokens up to, and including, the next non-nested closing `)'.
2281 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2282 are doing error recovery. Returns -1 if OR_COMMA is true and we
2283 found an unnested comma. */
2286 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2291 unsigned paren_depth = 0;
2292 unsigned brace_depth = 0;
2293 int saved_c_lex_string_translate = c_lex_string_translate;
2296 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2297 && !cp_parser_committed_to_tentative_parse (parser))
2301 /* If we're looking ahead, keep both translated and untranslated
2303 c_lex_string_translate = -1;
2309 /* If we've run out of tokens, then there is no closing `)'. */
2310 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2316 token = cp_lexer_peek_token (parser->lexer);
2318 /* This matches the processing in skip_to_end_of_statement. */
2319 if (token->type == CPP_SEMICOLON && !brace_depth)
2324 if (token->type == CPP_OPEN_BRACE)
2326 if (token->type == CPP_CLOSE_BRACE)
2334 if (recovering && or_comma && token->type == CPP_COMMA
2335 && !brace_depth && !paren_depth)
2343 /* If it is an `(', we have entered another level of nesting. */
2344 if (token->type == CPP_OPEN_PAREN)
2346 /* If it is a `)', then we might be done. */
2347 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2350 cp_lexer_consume_token (parser->lexer);
2358 /* Consume the token. */
2359 cp_lexer_consume_token (parser->lexer);
2362 c_lex_string_translate = saved_c_lex_string_translate;
2366 /* Consume tokens until we reach the end of the current statement.
2367 Normally, that will be just before consuming a `;'. However, if a
2368 non-nested `}' comes first, then we stop before consuming that. */
2371 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2373 unsigned nesting_depth = 0;
2379 /* Peek at the next token. */
2380 token = cp_lexer_peek_token (parser->lexer);
2381 /* If we've run out of tokens, stop. */
2382 if (token->type == CPP_EOF)
2384 /* If the next token is a `;', we have reached the end of the
2386 if (token->type == CPP_SEMICOLON && !nesting_depth)
2388 /* If the next token is a non-nested `}', then we have reached
2389 the end of the current block. */
2390 if (token->type == CPP_CLOSE_BRACE)
2392 /* If this is a non-nested `}', stop before consuming it.
2393 That way, when confronted with something like:
2397 we stop before consuming the closing `}', even though we
2398 have not yet reached a `;'. */
2399 if (nesting_depth == 0)
2401 /* If it is the closing `}' for a block that we have
2402 scanned, stop -- but only after consuming the token.
2408 we will stop after the body of the erroneously declared
2409 function, but before consuming the following `typedef'
2411 if (--nesting_depth == 0)
2413 cp_lexer_consume_token (parser->lexer);
2417 /* If it the next token is a `{', then we are entering a new
2418 block. Consume the entire block. */
2419 else if (token->type == CPP_OPEN_BRACE)
2421 /* Consume the token. */
2422 cp_lexer_consume_token (parser->lexer);
2426 /* This function is called at the end of a statement or declaration.
2427 If the next token is a semicolon, it is consumed; otherwise, error
2428 recovery is attempted. */
2431 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2433 /* Look for the trailing `;'. */
2434 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2436 /* If there is additional (erroneous) input, skip to the end of
2438 cp_parser_skip_to_end_of_statement (parser);
2439 /* If the next token is now a `;', consume it. */
2440 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2441 cp_lexer_consume_token (parser->lexer);
2445 /* Skip tokens until we have consumed an entire block, or until we
2446 have consumed a non-nested `;'. */
2449 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2451 unsigned nesting_depth = 0;
2457 /* Peek at the next token. */
2458 token = cp_lexer_peek_token (parser->lexer);
2459 /* If we've run out of tokens, stop. */
2460 if (token->type == CPP_EOF)
2462 /* If the next token is a `;', we have reached the end of the
2464 if (token->type == CPP_SEMICOLON && !nesting_depth)
2466 /* Consume the `;'. */
2467 cp_lexer_consume_token (parser->lexer);
2470 /* Consume the token. */
2471 token = cp_lexer_consume_token (parser->lexer);
2472 /* If the next token is a non-nested `}', then we have reached
2473 the end of the current block. */
2474 if (token->type == CPP_CLOSE_BRACE
2475 && (nesting_depth == 0 || --nesting_depth == 0))
2477 /* If it the next token is a `{', then we are entering a new
2478 block. Consume the entire block. */
2479 if (token->type == CPP_OPEN_BRACE)
2484 /* Skip tokens until a non-nested closing curly brace is the next
2488 cp_parser_skip_to_closing_brace (cp_parser *parser)
2490 unsigned nesting_depth = 0;
2496 /* Peek at the next token. */
2497 token = cp_lexer_peek_token (parser->lexer);
2498 /* If we've run out of tokens, stop. */
2499 if (token->type == CPP_EOF)
2501 /* If the next token is a non-nested `}', then we have reached
2502 the end of the current block. */
2503 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2505 /* If it the next token is a `{', then we are entering a new
2506 block. Consume the entire block. */
2507 else if (token->type == CPP_OPEN_BRACE)
2509 /* Consume the token. */
2510 cp_lexer_consume_token (parser->lexer);
2514 /* This is a simple wrapper around make_typename_type. When the id is
2515 an unresolved identifier node, we can provide a superior diagnostic
2516 using cp_parser_diagnose_invalid_type_name. */
2519 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2522 if (TREE_CODE (id) == IDENTIFIER_NODE)
2524 result = make_typename_type (scope, id, /*complain=*/0);
2525 if (result == error_mark_node)
2526 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2529 return make_typename_type (scope, id, tf_error);
2533 /* Create a new C++ parser. */
2536 cp_parser_new (void)
2541 /* cp_lexer_new_main is called before calling ggc_alloc because
2542 cp_lexer_new_main might load a PCH file. */
2543 lexer = cp_lexer_new_main ();
2545 parser = ggc_alloc_cleared (sizeof (cp_parser));
2546 parser->lexer = lexer;
2547 parser->context = cp_parser_context_new (NULL);
2549 /* For now, we always accept GNU extensions. */
2550 parser->allow_gnu_extensions_p = 1;
2552 /* The `>' token is a greater-than operator, not the end of a
2554 parser->greater_than_is_operator_p = true;
2556 parser->default_arg_ok_p = true;
2558 /* We are not parsing a constant-expression. */
2559 parser->integral_constant_expression_p = false;
2560 parser->allow_non_integral_constant_expression_p = false;
2561 parser->non_integral_constant_expression_p = false;
2563 /* Local variable names are not forbidden. */
2564 parser->local_variables_forbidden_p = false;
2566 /* We are not processing an `extern "C"' declaration. */
2567 parser->in_unbraced_linkage_specification_p = false;
2569 /* We are not processing a declarator. */
2570 parser->in_declarator_p = false;
2572 /* We are not processing a template-argument-list. */
2573 parser->in_template_argument_list_p = false;
2575 /* We are not in an iteration statement. */
2576 parser->in_iteration_statement_p = false;
2578 /* We are not in a switch statement. */
2579 parser->in_switch_statement_p = false;
2581 /* We are not parsing a type-id inside an expression. */
2582 parser->in_type_id_in_expr_p = false;
2584 /* The unparsed function queue is empty. */
2585 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2587 /* There are no classes being defined. */
2588 parser->num_classes_being_defined = 0;
2590 /* No template parameters apply. */
2591 parser->num_template_parameter_lists = 0;
2596 /* Lexical conventions [gram.lex] */
2598 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2602 cp_parser_identifier (cp_parser* parser)
2606 /* Look for the identifier. */
2607 token = cp_parser_require (parser, CPP_NAME, "identifier");
2608 /* Return the value. */
2609 return token ? token->value : error_mark_node;
2612 /* Basic concepts [gram.basic] */
2614 /* Parse a translation-unit.
2617 declaration-seq [opt]
2619 Returns TRUE if all went well. */
2622 cp_parser_translation_unit (cp_parser* parser)
2624 /* The address of the first non-permanent object on the declarator
2626 static void *declarator_obstack_base;
2630 /* Create the declarator obstack, if necessary. */
2631 if (!cp_error_declarator)
2633 gcc_obstack_init (&declarator_obstack);
2634 /* Create the error declarator. */
2635 cp_error_declarator = make_declarator (cdk_error);
2636 /* Create the empty parameter list. */
2637 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2638 /* Remember where the base of the declarator obstack lies. */
2639 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2644 cp_parser_declaration_seq_opt (parser);
2646 /* If there are no tokens left then all went well. */
2647 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2649 /* Consume the EOF token. */
2650 cp_parser_require (parser, CPP_EOF, "end-of-file");
2653 finish_translation_unit ();
2660 cp_parser_error (parser, "expected declaration");
2666 /* Make sure the declarator obstack was fully cleaned up. */
2667 my_friendly_assert (obstack_next_free (&declarator_obstack) ==
2668 declarator_obstack_base,
2671 /* All went well. */
2675 /* Expressions [gram.expr] */
2677 /* Parse a primary-expression.
2688 ( compound-statement )
2689 __builtin_va_arg ( assignment-expression , type-id )
2694 Returns a representation of the expression.
2696 *IDK indicates what kind of id-expression (if any) was present.
2698 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2699 used as the operand of a pointer-to-member. In that case,
2700 *QUALIFYING_CLASS gives the class that is used as the qualifying
2701 class in the pointer-to-member. */
2704 cp_parser_primary_expression (cp_parser *parser,
2706 tree *qualifying_class)
2710 /* Assume the primary expression is not an id-expression. */
2711 *idk = CP_ID_KIND_NONE;
2712 /* And that it cannot be used as pointer-to-member. */
2713 *qualifying_class = NULL_TREE;
2715 /* Peek at the next token. */
2716 token = cp_lexer_peek_token (parser->lexer);
2717 switch (token->type)
2728 token = cp_lexer_consume_token (parser->lexer);
2729 return token->value;
2733 token = cp_lexer_consume_token (parser->lexer);
2734 if (TREE_CHAIN (token->value))
2735 return TREE_CHAIN (token->value);
2737 return token->value;
2739 case CPP_OPEN_PAREN:
2742 bool saved_greater_than_is_operator_p;
2744 /* Consume the `('. */
2745 cp_lexer_consume_token (parser->lexer);
2746 /* Within a parenthesized expression, a `>' token is always
2747 the greater-than operator. */
2748 saved_greater_than_is_operator_p
2749 = parser->greater_than_is_operator_p;
2750 parser->greater_than_is_operator_p = true;
2751 /* If we see `( { ' then we are looking at the beginning of
2752 a GNU statement-expression. */
2753 if (cp_parser_allow_gnu_extensions_p (parser)
2754 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2756 /* Statement-expressions are not allowed by the standard. */
2758 pedwarn ("ISO C++ forbids braced-groups within expressions");
2760 /* And they're not allowed outside of a function-body; you
2761 cannot, for example, write:
2763 int i = ({ int j = 3; j + 1; });
2765 at class or namespace scope. */
2766 if (!at_function_scope_p ())
2767 error ("statement-expressions are allowed only inside functions");
2768 /* Start the statement-expression. */
2769 expr = begin_stmt_expr ();
2770 /* Parse the compound-statement. */
2771 cp_parser_compound_statement (parser, expr, false);
2773 expr = finish_stmt_expr (expr, false);
2777 /* Parse the parenthesized expression. */
2778 expr = cp_parser_expression (parser);
2779 /* Let the front end know that this expression was
2780 enclosed in parentheses. This matters in case, for
2781 example, the expression is of the form `A::B', since
2782 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2784 finish_parenthesized_expr (expr);
2786 /* The `>' token might be the end of a template-id or
2787 template-parameter-list now. */
2788 parser->greater_than_is_operator_p
2789 = saved_greater_than_is_operator_p;
2790 /* Consume the `)'. */
2791 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2792 cp_parser_skip_to_end_of_statement (parser);
2798 switch (token->keyword)
2800 /* These two are the boolean literals. */
2802 cp_lexer_consume_token (parser->lexer);
2803 return boolean_true_node;
2805 cp_lexer_consume_token (parser->lexer);
2806 return boolean_false_node;
2808 /* The `__null' literal. */
2810 cp_lexer_consume_token (parser->lexer);
2813 /* Recognize the `this' keyword. */
2815 cp_lexer_consume_token (parser->lexer);
2816 if (parser->local_variables_forbidden_p)
2818 error ("`this' may not be used in this context");
2819 return error_mark_node;
2821 /* Pointers cannot appear in constant-expressions. */
2822 if (cp_parser_non_integral_constant_expression (parser,
2824 return error_mark_node;
2825 return finish_this_expr ();
2827 /* The `operator' keyword can be the beginning of an
2832 case RID_FUNCTION_NAME:
2833 case RID_PRETTY_FUNCTION_NAME:
2834 case RID_C99_FUNCTION_NAME:
2835 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2836 __func__ are the names of variables -- but they are
2837 treated specially. Therefore, they are handled here,
2838 rather than relying on the generic id-expression logic
2839 below. Grammatically, these names are id-expressions.
2841 Consume the token. */
2842 token = cp_lexer_consume_token (parser->lexer);
2843 /* Look up the name. */
2844 return finish_fname (token->value);
2851 /* The `__builtin_va_arg' construct is used to handle
2852 `va_arg'. Consume the `__builtin_va_arg' token. */
2853 cp_lexer_consume_token (parser->lexer);
2854 /* Look for the opening `('. */
2855 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2856 /* Now, parse the assignment-expression. */
2857 expression = cp_parser_assignment_expression (parser);
2858 /* Look for the `,'. */
2859 cp_parser_require (parser, CPP_COMMA, "`,'");
2860 /* Parse the type-id. */
2861 type = cp_parser_type_id (parser);
2862 /* Look for the closing `)'. */
2863 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2864 /* Using `va_arg' in a constant-expression is not
2866 if (cp_parser_non_integral_constant_expression (parser,
2868 return error_mark_node;
2869 return build_x_va_arg (expression, type);
2873 return cp_parser_builtin_offsetof (parser);
2876 cp_parser_error (parser, "expected primary-expression");
2877 return error_mark_node;
2880 /* An id-expression can start with either an identifier, a
2881 `::' as the beginning of a qualified-id, or the "operator"
2885 case CPP_TEMPLATE_ID:
2886 case CPP_NESTED_NAME_SPECIFIER:
2890 const char *error_msg;
2893 /* Parse the id-expression. */
2895 = cp_parser_id_expression (parser,
2896 /*template_keyword_p=*/false,
2897 /*check_dependency_p=*/true,
2898 /*template_p=*/NULL,
2899 /*declarator_p=*/false);
2900 if (id_expression == error_mark_node)
2901 return error_mark_node;
2902 /* If we have a template-id, then no further lookup is
2903 required. If the template-id was for a template-class, we
2904 will sometimes have a TYPE_DECL at this point. */
2905 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2906 || TREE_CODE (id_expression) == TYPE_DECL)
2907 decl = id_expression;
2908 /* Look up the name. */
2911 decl = cp_parser_lookup_name_simple (parser, id_expression);
2912 /* If name lookup gives us a SCOPE_REF, then the
2913 qualifying scope was dependent. Just propagate the
2915 if (TREE_CODE (decl) == SCOPE_REF)
2917 if (TYPE_P (TREE_OPERAND (decl, 0)))
2918 *qualifying_class = TREE_OPERAND (decl, 0);
2921 /* Check to see if DECL is a local variable in a context
2922 where that is forbidden. */
2923 if (parser->local_variables_forbidden_p
2924 && local_variable_p (decl))
2926 /* It might be that we only found DECL because we are
2927 trying to be generous with pre-ISO scoping rules.
2928 For example, consider:
2932 for (int i = 0; i < 10; ++i) {}
2933 extern void f(int j = i);
2936 Here, name look up will originally find the out
2937 of scope `i'. We need to issue a warning message,
2938 but then use the global `i'. */
2939 decl = check_for_out_of_scope_variable (decl);
2940 if (local_variable_p (decl))
2942 error ("local variable `%D' may not appear in this context",
2944 return error_mark_node;
2949 decl = finish_id_expression (id_expression, decl, parser->scope,
2950 idk, qualifying_class,
2951 parser->integral_constant_expression_p,
2952 parser->allow_non_integral_constant_expression_p,
2953 &parser->non_integral_constant_expression_p,
2956 cp_parser_error (parser, error_msg);
2960 /* Anything else is an error. */
2962 cp_parser_error (parser, "expected primary-expression");
2963 return error_mark_node;
2967 /* Parse an id-expression.
2974 :: [opt] nested-name-specifier template [opt] unqualified-id
2976 :: operator-function-id
2979 Return a representation of the unqualified portion of the
2980 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2981 a `::' or nested-name-specifier.
2983 Often, if the id-expression was a qualified-id, the caller will
2984 want to make a SCOPE_REF to represent the qualified-id. This
2985 function does not do this in order to avoid wastefully creating
2986 SCOPE_REFs when they are not required.
2988 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2991 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2992 uninstantiated templates.
2994 If *TEMPLATE_P is non-NULL, it is set to true iff the
2995 `template' keyword is used to explicitly indicate that the entity
2996 named is a template.
2998 If DECLARATOR_P is true, the id-expression is appearing as part of
2999 a declarator, rather than as part of an expression. */
3002 cp_parser_id_expression (cp_parser *parser,
3003 bool template_keyword_p,
3004 bool check_dependency_p,
3008 bool global_scope_p;
3009 bool nested_name_specifier_p;
3011 /* Assume the `template' keyword was not used. */
3013 *template_p = false;
3015 /* Look for the optional `::' operator. */
3017 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3019 /* Look for the optional nested-name-specifier. */
3020 nested_name_specifier_p
3021 = (cp_parser_nested_name_specifier_opt (parser,
3022 /*typename_keyword_p=*/false,
3025 /*is_declarator=*/false)
3027 /* If there is a nested-name-specifier, then we are looking at
3028 the first qualified-id production. */
3029 if (nested_name_specifier_p)
3032 tree saved_object_scope;
3033 tree saved_qualifying_scope;
3034 tree unqualified_id;
3037 /* See if the next token is the `template' keyword. */
3039 template_p = &is_template;
3040 *template_p = cp_parser_optional_template_keyword (parser);
3041 /* Name lookup we do during the processing of the
3042 unqualified-id might obliterate SCOPE. */
3043 saved_scope = parser->scope;
3044 saved_object_scope = parser->object_scope;
3045 saved_qualifying_scope = parser->qualifying_scope;
3046 /* Process the final unqualified-id. */
3047 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3050 /* Restore the SAVED_SCOPE for our caller. */
3051 parser->scope = saved_scope;
3052 parser->object_scope = saved_object_scope;
3053 parser->qualifying_scope = saved_qualifying_scope;
3055 return unqualified_id;
3057 /* Otherwise, if we are in global scope, then we are looking at one
3058 of the other qualified-id productions. */
3059 else if (global_scope_p)
3064 /* Peek at the next token. */
3065 token = cp_lexer_peek_token (parser->lexer);
3067 /* If it's an identifier, and the next token is not a "<", then
3068 we can avoid the template-id case. This is an optimization
3069 for this common case. */
3070 if (token->type == CPP_NAME
3071 && !cp_parser_nth_token_starts_template_argument_list_p
3073 return cp_parser_identifier (parser);
3075 cp_parser_parse_tentatively (parser);
3076 /* Try a template-id. */
3077 id = cp_parser_template_id (parser,
3078 /*template_keyword_p=*/false,
3079 /*check_dependency_p=*/true,
3081 /* If that worked, we're done. */
3082 if (cp_parser_parse_definitely (parser))
3085 /* Peek at the next token. (Changes in the token buffer may
3086 have invalidated the pointer obtained above.) */
3087 token = cp_lexer_peek_token (parser->lexer);
3089 switch (token->type)
3092 return cp_parser_identifier (parser);
3095 if (token->keyword == RID_OPERATOR)
3096 return cp_parser_operator_function_id (parser);
3100 cp_parser_error (parser, "expected id-expression");
3101 return error_mark_node;
3105 return cp_parser_unqualified_id (parser, template_keyword_p,
3106 /*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. If DECLARATOR_P
3129 is true, the unqualified-id is appearing as part of a declarator,
3130 rather than as part of an expression. */
3133 cp_parser_unqualified_id (cp_parser* parser,
3134 bool template_keyword_p,
3135 bool check_dependency_p,
3140 /* Peek at the next token. */
3141 token = cp_lexer_peek_token (parser->lexer);
3143 switch (token->type)
3149 /* We don't know yet whether or not this will be a
3151 cp_parser_parse_tentatively (parser);
3152 /* Try a template-id. */
3153 id = cp_parser_template_id (parser, template_keyword_p,
3156 /* If it worked, we're done. */
3157 if (cp_parser_parse_definitely (parser))
3159 /* Otherwise, it's an ordinary identifier. */
3160 return cp_parser_identifier (parser);
3163 case CPP_TEMPLATE_ID:
3164 return cp_parser_template_id (parser, template_keyword_p,
3171 tree qualifying_scope;
3175 /* Consume the `~' token. */
3176 cp_lexer_consume_token (parser->lexer);
3177 /* Parse the class-name. The standard, as written, seems to
3180 template <typename T> struct S { ~S (); };
3181 template <typename T> S<T>::~S() {}
3183 is invalid, since `~' must be followed by a class-name, but
3184 `S<T>' is dependent, and so not known to be a class.
3185 That's not right; we need to look in uninstantiated
3186 templates. A further complication arises from:
3188 template <typename T> void f(T t) {
3192 Here, it is not possible to look up `T' in the scope of `T'
3193 itself. We must look in both the current scope, and the
3194 scope of the containing complete expression.
3196 Yet another issue is:
3205 The standard does not seem to say that the `S' in `~S'
3206 should refer to the type `S' and not the data member
3209 /* DR 244 says that we look up the name after the "~" in the
3210 same scope as we looked up the qualifying name. That idea
3211 isn't fully worked out; it's more complicated than that. */
3212 scope = parser->scope;
3213 object_scope = parser->object_scope;
3214 qualifying_scope = parser->qualifying_scope;
3216 /* If the name is of the form "X::~X" it's OK. */
3217 if (scope && TYPE_P (scope)
3218 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3219 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3221 && (cp_lexer_peek_token (parser->lexer)->value
3222 == TYPE_IDENTIFIER (scope)))
3224 cp_lexer_consume_token (parser->lexer);
3225 return build_nt (BIT_NOT_EXPR, scope);
3228 /* If there was an explicit qualification (S::~T), first look
3229 in the scope given by the qualification (i.e., S). */
3232 cp_parser_parse_tentatively (parser);
3233 type_decl = cp_parser_class_name (parser,
3234 /*typename_keyword_p=*/false,
3235 /*template_keyword_p=*/false,
3237 /*check_dependency=*/false,
3238 /*class_head_p=*/false,
3240 if (cp_parser_parse_definitely (parser))
3241 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3243 /* In "N::S::~S", look in "N" as well. */
3244 if (scope && qualifying_scope)
3246 cp_parser_parse_tentatively (parser);
3247 parser->scope = qualifying_scope;
3248 parser->object_scope = NULL_TREE;
3249 parser->qualifying_scope = NULL_TREE;
3251 = cp_parser_class_name (parser,
3252 /*typename_keyword_p=*/false,
3253 /*template_keyword_p=*/false,
3255 /*check_dependency=*/false,
3256 /*class_head_p=*/false,
3258 if (cp_parser_parse_definitely (parser))
3259 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3261 /* In "p->S::~T", look in the scope given by "*p" as well. */
3262 else if (object_scope)
3264 cp_parser_parse_tentatively (parser);
3265 parser->scope = object_scope;
3266 parser->object_scope = NULL_TREE;
3267 parser->qualifying_scope = NULL_TREE;
3269 = cp_parser_class_name (parser,
3270 /*typename_keyword_p=*/false,
3271 /*template_keyword_p=*/false,
3273 /*check_dependency=*/false,
3274 /*class_head_p=*/false,
3276 if (cp_parser_parse_definitely (parser))
3277 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3279 /* Look in the surrounding context. */
3280 parser->scope = NULL_TREE;
3281 parser->object_scope = NULL_TREE;
3282 parser->qualifying_scope = NULL_TREE;
3284 = cp_parser_class_name (parser,
3285 /*typename_keyword_p=*/false,
3286 /*template_keyword_p=*/false,
3288 /*check_dependency=*/false,
3289 /*class_head_p=*/false,
3291 /* If an error occurred, assume that the name of the
3292 destructor is the same as the name of the qualifying
3293 class. That allows us to keep parsing after running
3294 into ill-formed destructor names. */
3295 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3296 return build_nt (BIT_NOT_EXPR, scope);
3297 else if (type_decl == error_mark_node)
3298 return error_mark_node;
3302 A typedef-name that names a class shall not be used as the
3303 identifier in the declarator for a destructor declaration. */
3305 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3306 && !DECL_SELF_REFERENCE_P (type_decl))
3307 error ("typedef-name `%D' used as destructor declarator",
3310 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3314 if (token->keyword == RID_OPERATOR)
3318 /* This could be a template-id, so we try that first. */
3319 cp_parser_parse_tentatively (parser);
3320 /* Try a template-id. */
3321 id = cp_parser_template_id (parser, template_keyword_p,
3322 /*check_dependency_p=*/true,
3324 /* If that worked, we're done. */
3325 if (cp_parser_parse_definitely (parser))
3327 /* We still don't know whether we're looking at an
3328 operator-function-id or a conversion-function-id. */
3329 cp_parser_parse_tentatively (parser);
3330 /* Try an operator-function-id. */
3331 id = cp_parser_operator_function_id (parser);
3332 /* If that didn't work, try a conversion-function-id. */
3333 if (!cp_parser_parse_definitely (parser))
3334 id = cp_parser_conversion_function_id (parser);
3341 cp_parser_error (parser, "expected unqualified-id");
3342 return error_mark_node;
3346 /* Parse an (optional) nested-name-specifier.
3348 nested-name-specifier:
3349 class-or-namespace-name :: nested-name-specifier [opt]
3350 class-or-namespace-name :: template nested-name-specifier [opt]
3352 PARSER->SCOPE should be set appropriately before this function is
3353 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3354 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3357 Sets PARSER->SCOPE to the class (TYPE) or namespace
3358 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3359 it unchanged if there is no nested-name-specifier. Returns the new
3360 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3362 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3363 part of a declaration and/or decl-specifier. */
3366 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3367 bool typename_keyword_p,
3368 bool check_dependency_p,
3370 bool is_declaration)
3372 bool success = false;
3373 tree access_check = NULL_TREE;
3377 /* If the next token corresponds to a nested name specifier, there
3378 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3379 false, it may have been true before, in which case something
3380 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3381 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3382 CHECK_DEPENDENCY_P is false, we have to fall through into the
3384 if (check_dependency_p
3385 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3387 cp_parser_pre_parsed_nested_name_specifier (parser);
3388 return parser->scope;
3391 /* Remember where the nested-name-specifier starts. */
3392 if (cp_parser_parsing_tentatively (parser)
3393 && !cp_parser_committed_to_tentative_parse (parser))
3395 token = cp_lexer_peek_token (parser->lexer);
3396 start = cp_lexer_token_difference (parser->lexer,
3397 parser->lexer->first_token,
3403 push_deferring_access_checks (dk_deferred);
3409 tree saved_qualifying_scope;
3410 bool template_keyword_p;
3412 /* Spot cases that cannot be the beginning of a
3413 nested-name-specifier. */
3414 token = cp_lexer_peek_token (parser->lexer);
3416 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3417 the already parsed nested-name-specifier. */
3418 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3420 /* Grab the nested-name-specifier and continue the loop. */
3421 cp_parser_pre_parsed_nested_name_specifier (parser);
3426 /* Spot cases that cannot be the beginning of a
3427 nested-name-specifier. On the second and subsequent times
3428 through the loop, we look for the `template' keyword. */
3429 if (success && token->keyword == RID_TEMPLATE)
3431 /* A template-id can start a nested-name-specifier. */
3432 else if (token->type == CPP_TEMPLATE_ID)
3436 /* If the next token is not an identifier, then it is
3437 definitely not a class-or-namespace-name. */
3438 if (token->type != CPP_NAME)
3440 /* If the following token is neither a `<' (to begin a
3441 template-id), nor a `::', then we are not looking at a
3442 nested-name-specifier. */
3443 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3444 if (token->type != CPP_SCOPE
3445 && !cp_parser_nth_token_starts_template_argument_list_p
3450 /* The nested-name-specifier is optional, so we parse
3452 cp_parser_parse_tentatively (parser);
3454 /* Look for the optional `template' keyword, if this isn't the
3455 first time through the loop. */
3457 template_keyword_p = cp_parser_optional_template_keyword (parser);
3459 template_keyword_p = false;
3461 /* Save the old scope since the name lookup we are about to do
3462 might destroy it. */
3463 old_scope = parser->scope;
3464 saved_qualifying_scope = parser->qualifying_scope;
3465 /* Parse the qualifying entity. */
3467 = cp_parser_class_or_namespace_name (parser,
3473 /* Look for the `::' token. */
3474 cp_parser_require (parser, CPP_SCOPE, "`::'");
3476 /* If we found what we wanted, we keep going; otherwise, we're
3478 if (!cp_parser_parse_definitely (parser))
3480 bool error_p = false;
3482 /* Restore the OLD_SCOPE since it was valid before the
3483 failed attempt at finding the last
3484 class-or-namespace-name. */
3485 parser->scope = old_scope;
3486 parser->qualifying_scope = saved_qualifying_scope;
3487 /* If the next token is an identifier, and the one after
3488 that is a `::', then any valid interpretation would have
3489 found a class-or-namespace-name. */
3490 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3491 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3493 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3496 token = cp_lexer_consume_token (parser->lexer);
3501 decl = cp_parser_lookup_name_simple (parser, token->value);
3502 if (TREE_CODE (decl) == TEMPLATE_DECL)
3503 error ("`%D' used without template parameters",
3506 cp_parser_name_lookup_error
3507 (parser, token->value, decl,
3508 "is not a class or namespace");
3509 parser->scope = NULL_TREE;
3511 /* Treat this as a successful nested-name-specifier
3516 If the name found is not a class-name (clause
3517 _class_) or namespace-name (_namespace.def_), the
3518 program is ill-formed. */
3521 cp_lexer_consume_token (parser->lexer);
3526 /* We've found one valid nested-name-specifier. */
3528 /* Make sure we look in the right scope the next time through
3530 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3531 ? TREE_TYPE (new_scope)
3533 /* If it is a class scope, try to complete it; we are about to
3534 be looking up names inside the class. */
3535 if (TYPE_P (parser->scope)
3536 /* Since checking types for dependency can be expensive,
3537 avoid doing it if the type is already complete. */
3538 && !COMPLETE_TYPE_P (parser->scope)
3539 /* Do not try to complete dependent types. */
3540 && !dependent_type_p (parser->scope))
3541 complete_type (parser->scope);
3544 /* Retrieve any deferred checks. Do not pop this access checks yet
3545 so the memory will not be reclaimed during token replacing below. */
3546 access_check = get_deferred_access_checks ();
3548 /* If parsing tentatively, replace the sequence of tokens that makes
3549 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3550 token. That way, should we re-parse the token stream, we will
3551 not have to repeat the effort required to do the parse, nor will
3552 we issue duplicate error messages. */
3553 if (success && start >= 0)
3555 /* Find the token that corresponds to the start of the
3557 token = cp_lexer_advance_token (parser->lexer,
3558 parser->lexer->first_token,
3561 /* Reset the contents of the START token. */
3562 token->type = CPP_NESTED_NAME_SPECIFIER;
3563 token->value = build_tree_list (access_check, parser->scope);
3564 TREE_TYPE (token->value) = parser->qualifying_scope;
3565 token->keyword = RID_MAX;
3566 /* Purge all subsequent tokens. */
3567 cp_lexer_purge_tokens_after (parser->lexer, token);
3570 pop_deferring_access_checks ();
3571 return success ? parser->scope : NULL_TREE;
3574 /* Parse a nested-name-specifier. See
3575 cp_parser_nested_name_specifier_opt for details. This function
3576 behaves identically, except that it will an issue an error if no
3577 nested-name-specifier is present, and it will return
3578 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3582 cp_parser_nested_name_specifier (cp_parser *parser,
3583 bool typename_keyword_p,
3584 bool check_dependency_p,
3586 bool is_declaration)
3590 /* Look for the nested-name-specifier. */
3591 scope = cp_parser_nested_name_specifier_opt (parser,
3596 /* If it was not present, issue an error message. */
3599 cp_parser_error (parser, "expected nested-name-specifier");
3600 parser->scope = NULL_TREE;
3601 return error_mark_node;
3607 /* Parse a class-or-namespace-name.
3609 class-or-namespace-name:
3613 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3614 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3615 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3616 TYPE_P is TRUE iff the next name should be taken as a class-name,
3617 even the same name is declared to be another entity in the same
3620 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3621 specified by the class-or-namespace-name. If neither is found the
3622 ERROR_MARK_NODE is returned. */
3625 cp_parser_class_or_namespace_name (cp_parser *parser,
3626 bool typename_keyword_p,
3627 bool template_keyword_p,
3628 bool check_dependency_p,
3630 bool is_declaration)
3633 tree saved_qualifying_scope;
3634 tree saved_object_scope;
3638 /* Before we try to parse the class-name, we must save away the
3639 current PARSER->SCOPE since cp_parser_class_name will destroy
3641 saved_scope = parser->scope;
3642 saved_qualifying_scope = parser->qualifying_scope;
3643 saved_object_scope = parser->object_scope;
3644 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3645 there is no need to look for a namespace-name. */
3646 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3648 cp_parser_parse_tentatively (parser);
3649 scope = cp_parser_class_name (parser,
3654 /*class_head_p=*/false,
3656 /* If that didn't work, try for a namespace-name. */
3657 if (!only_class_p && !cp_parser_parse_definitely (parser))
3659 /* Restore the saved scope. */
3660 parser->scope = saved_scope;
3661 parser->qualifying_scope = saved_qualifying_scope;
3662 parser->object_scope = saved_object_scope;
3663 /* If we are not looking at an identifier followed by the scope
3664 resolution operator, then this is not part of a
3665 nested-name-specifier. (Note that this function is only used
3666 to parse the components of a nested-name-specifier.) */
3667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3668 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3669 return error_mark_node;
3670 scope = cp_parser_namespace_name (parser);
3676 /* Parse a postfix-expression.
3680 postfix-expression [ expression ]
3681 postfix-expression ( expression-list [opt] )
3682 simple-type-specifier ( expression-list [opt] )
3683 typename :: [opt] nested-name-specifier identifier
3684 ( expression-list [opt] )
3685 typename :: [opt] nested-name-specifier template [opt] template-id
3686 ( expression-list [opt] )
3687 postfix-expression . template [opt] id-expression
3688 postfix-expression -> template [opt] id-expression
3689 postfix-expression . pseudo-destructor-name
3690 postfix-expression -> pseudo-destructor-name
3691 postfix-expression ++
3692 postfix-expression --
3693 dynamic_cast < type-id > ( expression )
3694 static_cast < type-id > ( expression )
3695 reinterpret_cast < type-id > ( expression )
3696 const_cast < type-id > ( expression )
3697 typeid ( expression )
3703 ( type-id ) { initializer-list , [opt] }
3705 This extension is a GNU version of the C99 compound-literal
3706 construct. (The C99 grammar uses `type-name' instead of `type-id',
3707 but they are essentially the same concept.)
3709 If ADDRESS_P is true, the postfix expression is the operand of the
3712 Returns a representation of the expression. */
3715 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3719 cp_id_kind idk = CP_ID_KIND_NONE;
3720 tree postfix_expression = NULL_TREE;
3721 /* Non-NULL only if the current postfix-expression can be used to
3722 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3723 class used to qualify the member. */
3724 tree qualifying_class = NULL_TREE;
3726 /* Peek at the next token. */
3727 token = cp_lexer_peek_token (parser->lexer);
3728 /* Some of the productions are determined by keywords. */
3729 keyword = token->keyword;
3739 const char *saved_message;
3741 /* All of these can be handled in the same way from the point
3742 of view of parsing. Begin by consuming the token
3743 identifying the cast. */
3744 cp_lexer_consume_token (parser->lexer);
3746 /* New types cannot be defined in the cast. */
3747 saved_message = parser->type_definition_forbidden_message;
3748 parser->type_definition_forbidden_message
3749 = "types may not be defined in casts";
3751 /* Look for the opening `<'. */
3752 cp_parser_require (parser, CPP_LESS, "`<'");
3753 /* Parse the type to which we are casting. */
3754 type = cp_parser_type_id (parser);
3755 /* Look for the closing `>'. */
3756 cp_parser_require (parser, CPP_GREATER, "`>'");
3757 /* Restore the old message. */
3758 parser->type_definition_forbidden_message = saved_message;
3760 /* And the expression which is being cast. */
3761 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3762 expression = cp_parser_expression (parser);
3763 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3765 /* Only type conversions to integral or enumeration types
3766 can be used in constant-expressions. */
3767 if (parser->integral_constant_expression_p
3768 && !dependent_type_p (type)
3769 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3770 && (cp_parser_non_integral_constant_expression
3772 "a cast to a type other than an integral or "
3773 "enumeration type")))
3774 return error_mark_node;
3780 = build_dynamic_cast (type, expression);
3784 = build_static_cast (type, expression);
3788 = build_reinterpret_cast (type, expression);
3792 = build_const_cast (type, expression);
3803 const char *saved_message;
3804 bool saved_in_type_id_in_expr_p;
3806 /* Consume the `typeid' token. */
3807 cp_lexer_consume_token (parser->lexer);
3808 /* Look for the `(' token. */
3809 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3810 /* Types cannot be defined in a `typeid' expression. */
3811 saved_message = parser->type_definition_forbidden_message;
3812 parser->type_definition_forbidden_message
3813 = "types may not be defined in a `typeid\' expression";
3814 /* We can't be sure yet whether we're looking at a type-id or an
3816 cp_parser_parse_tentatively (parser);
3817 /* Try a type-id first. */
3818 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3819 parser->in_type_id_in_expr_p = true;
3820 type = cp_parser_type_id (parser);
3821 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3822 /* Look for the `)' token. Otherwise, we can't be sure that
3823 we're not looking at an expression: consider `typeid (int
3824 (3))', for example. */
3825 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3826 /* If all went well, simply lookup the type-id. */
3827 if (cp_parser_parse_definitely (parser))
3828 postfix_expression = get_typeid (type);
3829 /* Otherwise, fall back to the expression variant. */
3834 /* Look for an expression. */
3835 expression = cp_parser_expression (parser);
3836 /* Compute its typeid. */
3837 postfix_expression = build_typeid (expression);
3838 /* Look for the `)' token. */
3839 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3841 /* `typeid' may not appear in an integral constant expression. */
3842 if (cp_parser_non_integral_constant_expression(parser,
3843 "`typeid' operator"))
3844 return error_mark_node;
3845 /* Restore the saved message. */
3846 parser->type_definition_forbidden_message = saved_message;
3852 bool template_p = false;
3856 /* Consume the `typename' token. */
3857 cp_lexer_consume_token (parser->lexer);
3858 /* Look for the optional `::' operator. */
3859 cp_parser_global_scope_opt (parser,
3860 /*current_scope_valid_p=*/false);
3861 /* Look for the nested-name-specifier. */
3862 cp_parser_nested_name_specifier (parser,
3863 /*typename_keyword_p=*/true,
3864 /*check_dependency_p=*/true,
3866 /*is_declaration=*/true);
3867 /* Look for the optional `template' keyword. */
3868 template_p = cp_parser_optional_template_keyword (parser);
3869 /* We don't know whether we're looking at a template-id or an
3871 cp_parser_parse_tentatively (parser);
3872 /* Try a template-id. */
3873 id = cp_parser_template_id (parser, template_p,
3874 /*check_dependency_p=*/true,
3875 /*is_declaration=*/true);
3876 /* If that didn't work, try an identifier. */
3877 if (!cp_parser_parse_definitely (parser))
3878 id = cp_parser_identifier (parser);
3879 /* If we look up a template-id in a non-dependent qualifying
3880 scope, there's no need to create a dependent type. */
3881 if (TREE_CODE (id) == TYPE_DECL
3882 && !dependent_type_p (parser->scope))
3883 type = TREE_TYPE (id);
3884 /* Create a TYPENAME_TYPE to represent the type to which the
3885 functional cast is being performed. */
3887 type = make_typename_type (parser->scope, id,
3890 postfix_expression = cp_parser_functional_cast (parser, type);
3898 /* If the next thing is a simple-type-specifier, we may be
3899 looking at a functional cast. We could also be looking at
3900 an id-expression. So, we try the functional cast, and if
3901 that doesn't work we fall back to the primary-expression. */
3902 cp_parser_parse_tentatively (parser);
3903 /* Look for the simple-type-specifier. */
3904 type = cp_parser_simple_type_specifier (parser,
3905 /*decl_specs=*/NULL,
3906 CP_PARSER_FLAGS_NONE);
3907 /* Parse the cast itself. */
3908 if (!cp_parser_error_occurred (parser))
3910 = cp_parser_functional_cast (parser, type);
3911 /* If that worked, we're done. */
3912 if (cp_parser_parse_definitely (parser))
3915 /* If the functional-cast didn't work out, try a
3916 compound-literal. */
3917 if (cp_parser_allow_gnu_extensions_p (parser)
3918 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3920 tree initializer_list = NULL_TREE;
3921 bool saved_in_type_id_in_expr_p;
3923 cp_parser_parse_tentatively (parser);
3924 /* Consume the `('. */
3925 cp_lexer_consume_token (parser->lexer);
3926 /* Parse the type. */
3927 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3928 parser->in_type_id_in_expr_p = true;
3929 type = cp_parser_type_id (parser);
3930 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3931 /* Look for the `)'. */
3932 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3933 /* Look for the `{'. */
3934 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3935 /* If things aren't going well, there's no need to
3937 if (!cp_parser_error_occurred (parser))
3939 bool non_constant_p;
3940 /* Parse the initializer-list. */
3942 = cp_parser_initializer_list (parser, &non_constant_p);
3943 /* Allow a trailing `,'. */
3944 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3945 cp_lexer_consume_token (parser->lexer);
3946 /* Look for the final `}'. */
3947 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3949 /* If that worked, we're definitely looking at a
3950 compound-literal expression. */
3951 if (cp_parser_parse_definitely (parser))
3953 /* Warn the user that a compound literal is not
3954 allowed in standard C++. */
3956 pedwarn ("ISO C++ forbids compound-literals");
3957 /* Form the representation of the compound-literal. */
3959 = finish_compound_literal (type, initializer_list);
3964 /* It must be a primary-expression. */
3965 postfix_expression = cp_parser_primary_expression (parser,
3972 /* If we were avoiding committing to the processing of a
3973 qualified-id until we knew whether or not we had a
3974 pointer-to-member, we now know. */
3975 if (qualifying_class)
3979 /* Peek at the next token. */
3980 token = cp_lexer_peek_token (parser->lexer);
3981 done = (token->type != CPP_OPEN_SQUARE
3982 && token->type != CPP_OPEN_PAREN
3983 && token->type != CPP_DOT
3984 && token->type != CPP_DEREF
3985 && token->type != CPP_PLUS_PLUS
3986 && token->type != CPP_MINUS_MINUS);
3988 postfix_expression = finish_qualified_id_expr (qualifying_class,
3993 return postfix_expression;
3996 /* Keep looping until the postfix-expression is complete. */
3999 if (idk == CP_ID_KIND_UNQUALIFIED
4000 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4001 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4002 /* It is not a Koenig lookup function call. */
4004 = unqualified_name_lookup_error (postfix_expression);
4006 /* Peek at the next token. */
4007 token = cp_lexer_peek_token (parser->lexer);
4009 switch (token->type)
4011 case CPP_OPEN_SQUARE:
4013 = cp_parser_postfix_open_square_expression (parser,
4016 idk = CP_ID_KIND_NONE;
4019 case CPP_OPEN_PAREN:
4020 /* postfix-expression ( expression-list [opt] ) */
4023 tree args = (cp_parser_parenthesized_expression_list
4024 (parser, false, /*non_constant_p=*/NULL));
4026 if (args == error_mark_node)
4028 postfix_expression = error_mark_node;
4032 /* Function calls are not permitted in
4033 constant-expressions. */
4034 if (cp_parser_non_integral_constant_expression (parser,
4037 postfix_expression = error_mark_node;
4042 if (idk == CP_ID_KIND_UNQUALIFIED)
4044 /* We do not perform argument-dependent lookup if
4045 normal lookup finds a non-function, in accordance
4046 with the expected resolution of DR 218. */
4048 && (is_overloaded_fn (postfix_expression)
4049 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
4053 = perform_koenig_lookup (postfix_expression, args);
4055 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4057 = unqualified_fn_lookup_error (postfix_expression);
4060 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4062 tree instance = TREE_OPERAND (postfix_expression, 0);
4063 tree fn = TREE_OPERAND (postfix_expression, 1);
4065 if (processing_template_decl
4066 && (type_dependent_expression_p (instance)
4067 || (!BASELINK_P (fn)
4068 && TREE_CODE (fn) != FIELD_DECL)
4069 || type_dependent_expression_p (fn)
4070 || any_type_dependent_arguments_p (args)))
4073 = build_min_nt (CALL_EXPR, postfix_expression,
4078 if (BASELINK_P (fn))
4080 = (build_new_method_call
4081 (instance, fn, args, NULL_TREE,
4082 (idk == CP_ID_KIND_QUALIFIED
4083 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4086 = finish_call_expr (postfix_expression, args,
4087 /*disallow_virtual=*/false,
4088 /*koenig_p=*/false);
4090 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4091 || TREE_CODE (postfix_expression) == MEMBER_REF
4092 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4093 postfix_expression = (build_offset_ref_call_from_tree
4094 (postfix_expression, args));
4095 else if (idk == CP_ID_KIND_QUALIFIED)
4096 /* A call to a static class member, or a namespace-scope
4099 = finish_call_expr (postfix_expression, args,
4100 /*disallow_virtual=*/true,
4103 /* All other function calls. */
4105 = finish_call_expr (postfix_expression, args,
4106 /*disallow_virtual=*/false,
4109 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4110 idk = CP_ID_KIND_NONE;
4116 /* postfix-expression . template [opt] id-expression
4117 postfix-expression . pseudo-destructor-name
4118 postfix-expression -> template [opt] id-expression
4119 postfix-expression -> pseudo-destructor-name */
4121 /* Consume the `.' or `->' operator. */
4122 cp_lexer_consume_token (parser->lexer);
4125 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4131 /* postfix-expression ++ */
4132 /* Consume the `++' token. */
4133 cp_lexer_consume_token (parser->lexer);
4134 /* Generate a representation for the complete expression. */
4136 = finish_increment_expr (postfix_expression,
4137 POSTINCREMENT_EXPR);
4138 /* Increments may not appear in constant-expressions. */
4139 if (cp_parser_non_integral_constant_expression (parser,
4141 postfix_expression = error_mark_node;
4142 idk = CP_ID_KIND_NONE;
4145 case CPP_MINUS_MINUS:
4146 /* postfix-expression -- */
4147 /* Consume the `--' token. */
4148 cp_lexer_consume_token (parser->lexer);
4149 /* Generate a representation for the complete expression. */
4151 = finish_increment_expr (postfix_expression,
4152 POSTDECREMENT_EXPR);
4153 /* Decrements may not appear in constant-expressions. */
4154 if (cp_parser_non_integral_constant_expression (parser,
4156 postfix_expression = error_mark_node;
4157 idk = CP_ID_KIND_NONE;
4161 return postfix_expression;
4165 /* We should never get here. */
4167 return error_mark_node;
4170 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4171 by cp_parser_builtin_offsetof. We're looking for
4173 postfix-expression [ expression ]
4175 FOR_OFFSETOF is set if we're being called in that context, which
4176 changes how we deal with integer constant expressions. */
4179 cp_parser_postfix_open_square_expression (cp_parser *parser,
4180 tree postfix_expression,
4185 /* Consume the `[' token. */
4186 cp_lexer_consume_token (parser->lexer);
4188 /* Parse the index expression. */
4189 /* ??? For offsetof, there is a question of what to allow here. If
4190 offsetof is not being used in an integral constant expression context,
4191 then we *could* get the right answer by computing the value at runtime.
4192 If we are in an integral constant expression context, then we might
4193 could accept any constant expression; hard to say without analysis.
4194 Rather than open the barn door too wide right away, allow only integer
4195 constant expresions here. */
4197 index = cp_parser_constant_expression (parser, false, NULL);
4199 index = cp_parser_expression (parser);
4201 /* Look for the closing `]'. */
4202 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4204 /* Build the ARRAY_REF. */
4205 postfix_expression = grok_array_decl (postfix_expression, index);
4207 /* When not doing offsetof, array references are not permitted in
4208 constant-expressions. */
4210 && (cp_parser_non_integral_constant_expression
4211 (parser, "an array reference")))
4212 postfix_expression = error_mark_node;
4214 return postfix_expression;
4217 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4218 by cp_parser_builtin_offsetof. We're looking for
4220 postfix-expression . template [opt] id-expression
4221 postfix-expression . pseudo-destructor-name
4222 postfix-expression -> template [opt] id-expression
4223 postfix-expression -> pseudo-destructor-name
4225 FOR_OFFSETOF is set if we're being called in that context. That sorta
4226 limits what of the above we'll actually accept, but nevermind.
4227 TOKEN_TYPE is the "." or "->" token, which will already have been
4228 removed from the stream. */
4231 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4232 enum cpp_ttype token_type,
4233 tree postfix_expression,
4234 bool for_offsetof, cp_id_kind *idk)
4239 tree scope = NULL_TREE;
4241 /* If this is a `->' operator, dereference the pointer. */
4242 if (token_type == CPP_DEREF)
4243 postfix_expression = build_x_arrow (postfix_expression);
4244 /* Check to see whether or not the expression is type-dependent. */
4245 dependent_p = type_dependent_expression_p (postfix_expression);
4246 /* The identifier following the `->' or `.' is not qualified. */
4247 parser->scope = NULL_TREE;
4248 parser->qualifying_scope = NULL_TREE;
4249 parser->object_scope = NULL_TREE;
4250 *idk = CP_ID_KIND_NONE;
4251 /* Enter the scope corresponding to the type of the object
4252 given by the POSTFIX_EXPRESSION. */
4253 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4255 scope = TREE_TYPE (postfix_expression);
4256 /* According to the standard, no expression should ever have
4257 reference type. Unfortunately, we do not currently match
4258 the standard in this respect in that our internal representation
4259 of an expression may have reference type even when the standard
4260 says it does not. Therefore, we have to manually obtain the
4261 underlying type here. */
4262 scope = non_reference (scope);
4263 /* The type of the POSTFIX_EXPRESSION must be complete. */
4264 scope = complete_type_or_else (scope, NULL_TREE);
4265 /* Let the name lookup machinery know that we are processing a
4266 class member access expression. */
4267 parser->context->object_type = scope;
4268 /* If something went wrong, we want to be able to discern that case,
4269 as opposed to the case where there was no SCOPE due to the type
4270 of expression being dependent. */
4272 scope = error_mark_node;
4273 /* If the SCOPE was erroneous, make the various semantic analysis
4274 functions exit quickly -- and without issuing additional error
4276 if (scope == error_mark_node)
4277 postfix_expression = error_mark_node;
4280 /* If the SCOPE is not a scalar type, we are looking at an
4281 ordinary class member access expression, rather than a
4282 pseudo-destructor-name. */
4283 if (!scope || !SCALAR_TYPE_P (scope))
4285 template_p = cp_parser_optional_template_keyword (parser);
4286 /* Parse the id-expression. */
4287 name = cp_parser_id_expression (parser, template_p,
4288 /*check_dependency_p=*/true,
4289 /*template_p=*/NULL,
4290 /*declarator_p=*/false);
4291 /* In general, build a SCOPE_REF if the member name is qualified.
4292 However, if the name was not dependent and has already been
4293 resolved; there is no need to build the SCOPE_REF. For example;
4295 struct X { void f(); };
4296 template <typename T> void f(T* t) { t->X::f(); }
4298 Even though "t" is dependent, "X::f" is not and has been resolved
4299 to a BASELINK; there is no need to include scope information. */
4301 /* But we do need to remember that there was an explicit scope for
4302 virtual function calls. */
4304 *idk = CP_ID_KIND_QUALIFIED;
4306 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4308 name = build_nt (SCOPE_REF, parser->scope, name);
4309 parser->scope = NULL_TREE;
4310 parser->qualifying_scope = NULL_TREE;
4311 parser->object_scope = NULL_TREE;
4313 if (scope && name && BASELINK_P (name))
4314 adjust_result_of_qualified_name_lookup
4315 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4317 = finish_class_member_access_expr (postfix_expression, name);
4319 /* Otherwise, try the pseudo-destructor-name production. */
4325 /* Parse the pseudo-destructor-name. */
4326 cp_parser_pseudo_destructor_name (parser, &s, &type);
4327 /* Form the call. */
4329 = finish_pseudo_destructor_expr (postfix_expression,
4330 s, TREE_TYPE (type));
4333 /* We no longer need to look up names in the scope of the object on
4334 the left-hand side of the `.' or `->' operator. */
4335 parser->context->object_type = NULL_TREE;
4337 /* Outside of offsetof, these operators may not appear in
4338 constant-expressions. */
4340 && (cp_parser_non_integral_constant_expression
4341 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4342 postfix_expression = error_mark_node;
4344 return postfix_expression;
4347 /* Parse a parenthesized expression-list.
4350 assignment-expression
4351 expression-list, assignment-expression
4356 identifier, expression-list
4358 Returns a TREE_LIST. The TREE_VALUE of each node is a
4359 representation of an assignment-expression. Note that a TREE_LIST
4360 is returned even if there is only a single expression in the list.
4361 error_mark_node is returned if the ( and or ) are
4362 missing. NULL_TREE is returned on no expressions. The parentheses
4363 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4364 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4365 indicates whether or not all of the expressions in the list were
4369 cp_parser_parenthesized_expression_list (cp_parser* parser,
4370 bool is_attribute_list,
4371 bool *non_constant_p)
4373 tree expression_list = NULL_TREE;
4374 tree identifier = NULL_TREE;
4376 /* Assume all the expressions will be constant. */
4378 *non_constant_p = false;
4380 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4381 return error_mark_node;
4383 /* Consume expressions until there are no more. */
4384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4389 /* At the beginning of attribute lists, check to see if the
4390 next token is an identifier. */
4391 if (is_attribute_list
4392 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4396 /* Consume the identifier. */
4397 token = cp_lexer_consume_token (parser->lexer);
4398 /* Save the identifier. */
4399 identifier = token->value;
4403 /* Parse the next assignment-expression. */
4406 bool expr_non_constant_p;
4407 expr = (cp_parser_constant_expression
4408 (parser, /*allow_non_constant_p=*/true,
4409 &expr_non_constant_p));
4410 if (expr_non_constant_p)
4411 *non_constant_p = true;
4414 expr = cp_parser_assignment_expression (parser);
4416 /* Add it to the list. We add error_mark_node
4417 expressions to the list, so that we can still tell if
4418 the correct form for a parenthesized expression-list
4419 is found. That gives better errors. */
4420 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4422 if (expr == error_mark_node)
4426 /* After the first item, attribute lists look the same as
4427 expression lists. */
4428 is_attribute_list = false;
4431 /* If the next token isn't a `,', then we are done. */
4432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4435 /* Otherwise, consume the `,' and keep going. */
4436 cp_lexer_consume_token (parser->lexer);
4439 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4444 /* We try and resync to an unnested comma, as that will give the
4445 user better diagnostics. */
4446 ending = cp_parser_skip_to_closing_parenthesis (parser,
4447 /*recovering=*/true,
4449 /*consume_paren=*/true);
4453 return error_mark_node;
4456 /* We built up the list in reverse order so we must reverse it now. */
4457 expression_list = nreverse (expression_list);
4459 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4461 return expression_list;
4464 /* Parse a pseudo-destructor-name.
4466 pseudo-destructor-name:
4467 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4468 :: [opt] nested-name-specifier template template-id :: ~ type-name
4469 :: [opt] nested-name-specifier [opt] ~ type-name
4471 If either of the first two productions is used, sets *SCOPE to the
4472 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4473 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4474 or ERROR_MARK_NODE if the parse fails. */
4477 cp_parser_pseudo_destructor_name (cp_parser* parser,
4481 bool nested_name_specifier_p;
4483 /* Look for the optional `::' operator. */
4484 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4485 /* Look for the optional nested-name-specifier. */
4486 nested_name_specifier_p
4487 = (cp_parser_nested_name_specifier_opt (parser,
4488 /*typename_keyword_p=*/false,
4489 /*check_dependency_p=*/true,
4491 /*is_declaration=*/true)
4493 /* Now, if we saw a nested-name-specifier, we might be doing the
4494 second production. */
4495 if (nested_name_specifier_p
4496 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4498 /* Consume the `template' keyword. */
4499 cp_lexer_consume_token (parser->lexer);
4500 /* Parse the template-id. */
4501 cp_parser_template_id (parser,
4502 /*template_keyword_p=*/true,
4503 /*check_dependency_p=*/false,
4504 /*is_declaration=*/true);
4505 /* Look for the `::' token. */
4506 cp_parser_require (parser, CPP_SCOPE, "`::'");
4508 /* If the next token is not a `~', then there might be some
4509 additional qualification. */
4510 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4512 /* Look for the type-name. */
4513 *scope = TREE_TYPE (cp_parser_type_name (parser));
4515 /* If we didn't get an aggregate type, or we don't have ::~,
4516 then something has gone wrong. Since the only caller of this
4517 function is looking for something after `.' or `->' after a
4518 scalar type, most likely the program is trying to get a
4519 member of a non-aggregate type. */
4520 if (*scope == error_mark_node
4521 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4522 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4524 cp_parser_error (parser, "request for member of non-aggregate type");
4525 *type = error_mark_node;
4529 /* Look for the `::' token. */
4530 cp_parser_require (parser, CPP_SCOPE, "`::'");
4535 /* Look for the `~'. */
4536 cp_parser_require (parser, CPP_COMPL, "`~'");
4537 /* Look for the type-name again. We are not responsible for
4538 checking that it matches the first type-name. */
4539 *type = cp_parser_type_name (parser);
4542 /* Parse a unary-expression.
4548 unary-operator cast-expression
4549 sizeof unary-expression
4557 __extension__ cast-expression
4558 __alignof__ unary-expression
4559 __alignof__ ( type-id )
4560 __real__ cast-expression
4561 __imag__ cast-expression
4564 ADDRESS_P is true iff the unary-expression is appearing as the
4565 operand of the `&' operator.
4567 Returns a representation of the expression. */
4570 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4573 enum tree_code unary_operator;
4575 /* Peek at the next token. */
4576 token = cp_lexer_peek_token (parser->lexer);
4577 /* Some keywords give away the kind of expression. */
4578 if (token->type == CPP_KEYWORD)
4580 enum rid keyword = token->keyword;
4590 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4591 /* Consume the token. */
4592 cp_lexer_consume_token (parser->lexer);
4593 /* Parse the operand. */
4594 operand = cp_parser_sizeof_operand (parser, keyword);
4596 if (TYPE_P (operand))
4597 return cxx_sizeof_or_alignof_type (operand, op, true);
4599 return cxx_sizeof_or_alignof_expr (operand, op);
4603 return cp_parser_new_expression (parser);
4606 return cp_parser_delete_expression (parser);
4610 /* The saved value of the PEDANTIC flag. */
4614 /* Save away the PEDANTIC flag. */
4615 cp_parser_extension_opt (parser, &saved_pedantic);
4616 /* Parse the cast-expression. */
4617 expr = cp_parser_simple_cast_expression (parser);
4618 /* Restore the PEDANTIC flag. */
4619 pedantic = saved_pedantic;
4629 /* Consume the `__real__' or `__imag__' token. */
4630 cp_lexer_consume_token (parser->lexer);
4631 /* Parse the cast-expression. */
4632 expression = cp_parser_simple_cast_expression (parser);
4633 /* Create the complete representation. */
4634 return build_x_unary_op ((keyword == RID_REALPART
4635 ? REALPART_EXPR : IMAGPART_EXPR),
4645 /* Look for the `:: new' and `:: delete', which also signal the
4646 beginning of a new-expression, or delete-expression,
4647 respectively. If the next token is `::', then it might be one of
4649 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4653 /* See if the token after the `::' is one of the keywords in
4654 which we're interested. */
4655 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4656 /* If it's `new', we have a new-expression. */
4657 if (keyword == RID_NEW)
4658 return cp_parser_new_expression (parser);
4659 /* Similarly, for `delete'. */
4660 else if (keyword == RID_DELETE)
4661 return cp_parser_delete_expression (parser);
4664 /* Look for a unary operator. */
4665 unary_operator = cp_parser_unary_operator (token);
4666 /* The `++' and `--' operators can be handled similarly, even though
4667 they are not technically unary-operators in the grammar. */
4668 if (unary_operator == ERROR_MARK)
4670 if (token->type == CPP_PLUS_PLUS)
4671 unary_operator = PREINCREMENT_EXPR;
4672 else if (token->type == CPP_MINUS_MINUS)
4673 unary_operator = PREDECREMENT_EXPR;
4674 /* Handle the GNU address-of-label extension. */
4675 else if (cp_parser_allow_gnu_extensions_p (parser)
4676 && token->type == CPP_AND_AND)
4680 /* Consume the '&&' token. */
4681 cp_lexer_consume_token (parser->lexer);
4682 /* Look for the identifier. */
4683 identifier = cp_parser_identifier (parser);
4684 /* Create an expression representing the address. */
4685 return finish_label_address_expr (identifier);
4688 if (unary_operator != ERROR_MARK)
4690 tree cast_expression;
4691 tree expression = error_mark_node;
4692 const char *non_constant_p = NULL;
4694 /* Consume the operator token. */
4695 token = cp_lexer_consume_token (parser->lexer);
4696 /* Parse the cast-expression. */
4698 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4699 /* Now, build an appropriate representation. */
4700 switch (unary_operator)
4703 non_constant_p = "`*'";
4704 expression = build_x_indirect_ref (cast_expression, "unary *");
4708 non_constant_p = "`&'";
4711 expression = build_x_unary_op (unary_operator, cast_expression);
4714 case PREINCREMENT_EXPR:
4715 case PREDECREMENT_EXPR:
4716 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4721 case TRUTH_NOT_EXPR:
4722 expression = finish_unary_op_expr (unary_operator, cast_expression);
4730 && cp_parser_non_integral_constant_expression (parser,
4732 expression = error_mark_node;
4737 return cp_parser_postfix_expression (parser, address_p);
4740 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4741 unary-operator, the corresponding tree code is returned. */
4743 static enum tree_code
4744 cp_parser_unary_operator (cp_token* token)
4746 switch (token->type)
4749 return INDIRECT_REF;
4755 return CONVERT_EXPR;
4761 return TRUTH_NOT_EXPR;
4764 return BIT_NOT_EXPR;
4771 /* Parse a new-expression.
4774 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4775 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4777 Returns a representation of the expression. */
4780 cp_parser_new_expression (cp_parser* parser)
4782 bool global_scope_p;
4788 /* Look for the optional `::' operator. */
4790 = (cp_parser_global_scope_opt (parser,
4791 /*current_scope_valid_p=*/false)
4793 /* Look for the `new' operator. */
4794 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4795 /* There's no easy way to tell a new-placement from the
4796 `( type-id )' construct. */
4797 cp_parser_parse_tentatively (parser);
4798 /* Look for a new-placement. */
4799 placement = cp_parser_new_placement (parser);
4800 /* If that didn't work out, there's no new-placement. */
4801 if (!cp_parser_parse_definitely (parser))
4802 placement = NULL_TREE;
4804 /* If the next token is a `(', then we have a parenthesized
4806 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4808 /* Consume the `('. */
4809 cp_lexer_consume_token (parser->lexer);
4810 /* Parse the type-id. */
4811 type = cp_parser_type_id (parser);
4812 /* Look for the closing `)'. */
4813 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4814 /* There should not be a direct-new-declarator in this production,
4815 but GCC used to allowed this, so we check and emit a sensible error
4816 message for this case. */
4817 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4819 error ("array bound forbidden after parenthesized type-id");
4820 inform ("try removing the parentheses around the type-id");
4821 cp_parser_direct_new_declarator (parser);
4823 nelts = integer_one_node;
4825 /* Otherwise, there must be a new-type-id. */
4827 type = cp_parser_new_type_id (parser, &nelts);
4829 /* If the next token is a `(', then we have a new-initializer. */
4830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4831 initializer = cp_parser_new_initializer (parser);
4833 initializer = NULL_TREE;
4835 /* A new-expression may not appear in an integral constant
4837 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4838 return error_mark_node;
4840 /* Create a representation of the new-expression. */
4841 return build_new (placement, type, nelts, initializer, global_scope_p);
4844 /* Parse a new-placement.
4849 Returns the same representation as for an expression-list. */
4852 cp_parser_new_placement (cp_parser* parser)
4854 tree expression_list;
4856 /* Parse the expression-list. */
4857 expression_list = (cp_parser_parenthesized_expression_list
4858 (parser, false, /*non_constant_p=*/NULL));
4860 return expression_list;
4863 /* Parse a new-type-id.
4866 type-specifier-seq new-declarator [opt]
4868 Returns the TYPE allocated. If the new-type-id indicates an array
4869 type, *NELTS is set to the number of elements in the last array
4870 bound; the TYPE will not include the last array bound. */
4873 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4875 cp_decl_specifier_seq type_specifier_seq;
4876 cp_declarator *new_declarator;
4877 cp_declarator *declarator;
4878 cp_declarator *outer_declarator;
4879 const char *saved_message;
4882 /* The type-specifier sequence must not contain type definitions.
4883 (It cannot contain declarations of new types either, but if they
4884 are not definitions we will catch that because they are not
4886 saved_message = parser->type_definition_forbidden_message;
4887 parser->type_definition_forbidden_message
4888 = "types may not be defined in a new-type-id";
4889 /* Parse the type-specifier-seq. */
4890 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4891 /* Restore the old message. */
4892 parser->type_definition_forbidden_message = saved_message;
4893 /* Parse the new-declarator. */
4894 new_declarator = cp_parser_new_declarator_opt (parser);
4896 /* Determine the number of elements in the last array dimension, if
4899 /* Skip down to the last array dimension. */
4900 declarator = new_declarator;
4901 outer_declarator = NULL;
4902 while (declarator && (declarator->kind == cdk_pointer
4903 || declarator->kind == cdk_ptrmem))
4905 outer_declarator = declarator;
4906 declarator = declarator->declarator;
4909 && declarator->kind == cdk_array
4910 && declarator->declarator
4911 && declarator->declarator->kind == cdk_array)
4913 outer_declarator = declarator;
4914 declarator = declarator->declarator;
4917 if (declarator && declarator->kind == cdk_array)
4919 *nelts = declarator->u.array.bounds;
4920 if (*nelts == error_mark_node)
4921 *nelts = integer_one_node;
4922 else if (!processing_template_decl)
4924 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4926 pedwarn ("size in array new must have integral type");
4927 *nelts = save_expr (cp_convert (sizetype, *nelts));
4928 if (*nelts == integer_zero_node)
4929 warning ("zero size array reserves no space");
4931 if (outer_declarator)
4932 outer_declarator->declarator = declarator->declarator;
4934 new_declarator = NULL;
4937 type = groktypename (&type_specifier_seq, new_declarator);
4938 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4940 *nelts = array_type_nelts_top (type);
4941 type = TREE_TYPE (type);
4946 /* Parse an (optional) new-declarator.
4949 ptr-operator new-declarator [opt]
4950 direct-new-declarator
4952 Returns the declarator. */
4954 static cp_declarator *
4955 cp_parser_new_declarator_opt (cp_parser* parser)
4957 enum tree_code code;
4959 tree cv_qualifier_seq;
4961 /* We don't know if there's a ptr-operator next, or not. */
4962 cp_parser_parse_tentatively (parser);
4963 /* Look for a ptr-operator. */
4964 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4965 /* If that worked, look for more new-declarators. */
4966 if (cp_parser_parse_definitely (parser))
4968 cp_declarator *declarator;
4970 /* Parse another optional declarator. */
4971 declarator = cp_parser_new_declarator_opt (parser);
4973 /* Create the representation of the declarator. */
4975 declarator = make_ptrmem_declarator (cv_qualifier_seq,
4978 else if (code == INDIRECT_REF)
4979 declarator = make_pointer_declarator (cv_qualifier_seq,
4982 declarator = make_reference_declarator (cv_qualifier_seq,
4988 /* If the next token is a `[', there is a direct-new-declarator. */
4989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4990 return cp_parser_direct_new_declarator (parser);
4995 /* Parse a direct-new-declarator.
4997 direct-new-declarator:
4999 direct-new-declarator [constant-expression]
5003 static cp_declarator *
5004 cp_parser_direct_new_declarator (cp_parser* parser)
5006 cp_declarator *declarator = NULL;
5012 /* Look for the opening `['. */
5013 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5014 /* The first expression is not required to be constant. */
5017 expression = cp_parser_expression (parser);
5018 /* The standard requires that the expression have integral
5019 type. DR 74 adds enumeration types. We believe that the
5020 real intent is that these expressions be handled like the
5021 expression in a `switch' condition, which also allows
5022 classes with a single conversion to integral or
5023 enumeration type. */
5024 if (!processing_template_decl)
5027 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5032 error ("expression in new-declarator must have integral or enumeration type");
5033 expression = error_mark_node;
5037 /* But all the other expressions must be. */
5040 = cp_parser_constant_expression (parser,
5041 /*allow_non_constant=*/false,
5043 /* Look for the closing `]'. */
5044 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5046 /* Add this bound to the declarator. */
5047 declarator = make_array_declarator (declarator, expression);
5049 /* If the next token is not a `[', then there are no more
5051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5058 /* Parse a new-initializer.
5061 ( expression-list [opt] )
5063 Returns a representation of the expression-list. If there is no
5064 expression-list, VOID_ZERO_NODE is returned. */
5067 cp_parser_new_initializer (cp_parser* parser)
5069 tree expression_list;
5071 expression_list = (cp_parser_parenthesized_expression_list
5072 (parser, false, /*non_constant_p=*/NULL));
5073 if (!expression_list)
5074 expression_list = void_zero_node;
5076 return expression_list;
5079 /* Parse a delete-expression.
5082 :: [opt] delete cast-expression
5083 :: [opt] delete [ ] cast-expression
5085 Returns a representation of the expression. */
5088 cp_parser_delete_expression (cp_parser* parser)
5090 bool global_scope_p;
5094 /* Look for the optional `::' operator. */
5096 = (cp_parser_global_scope_opt (parser,
5097 /*current_scope_valid_p=*/false)
5099 /* Look for the `delete' keyword. */
5100 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5101 /* See if the array syntax is in use. */
5102 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5104 /* Consume the `[' token. */
5105 cp_lexer_consume_token (parser->lexer);
5106 /* Look for the `]' token. */
5107 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5108 /* Remember that this is the `[]' construct. */
5114 /* Parse the cast-expression. */
5115 expression = cp_parser_simple_cast_expression (parser);
5117 /* A delete-expression may not appear in an integral constant
5119 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5120 return error_mark_node;
5122 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5125 /* Parse a cast-expression.
5129 ( type-id ) cast-expression
5131 Returns a representation of the expression. */
5134 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5136 /* If it's a `(', then we might be looking at a cast. */
5137 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5139 tree type = NULL_TREE;
5140 tree expr = NULL_TREE;
5141 bool compound_literal_p;
5142 const char *saved_message;
5144 /* There's no way to know yet whether or not this is a cast.
5145 For example, `(int (3))' is a unary-expression, while `(int)
5146 3' is a cast. So, we resort to parsing tentatively. */
5147 cp_parser_parse_tentatively (parser);
5148 /* Types may not be defined in a cast. */
5149 saved_message = parser->type_definition_forbidden_message;
5150 parser->type_definition_forbidden_message
5151 = "types may not be defined in casts";
5152 /* Consume the `('. */
5153 cp_lexer_consume_token (parser->lexer);
5154 /* A very tricky bit is that `(struct S) { 3 }' is a
5155 compound-literal (which we permit in C++ as an extension).
5156 But, that construct is not a cast-expression -- it is a
5157 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5158 is legal; if the compound-literal were a cast-expression,
5159 you'd need an extra set of parentheses.) But, if we parse
5160 the type-id, and it happens to be a class-specifier, then we
5161 will commit to the parse at that point, because we cannot
5162 undo the action that is done when creating a new class. So,
5163 then we cannot back up and do a postfix-expression.
5165 Therefore, we scan ahead to the closing `)', and check to see
5166 if the token after the `)' is a `{'. If so, we are not
5167 looking at a cast-expression.
5169 Save tokens so that we can put them back. */
5170 cp_lexer_save_tokens (parser->lexer);
5171 /* Skip tokens until the next token is a closing parenthesis.
5172 If we find the closing `)', and the next token is a `{', then
5173 we are looking at a compound-literal. */
5175 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5176 /*consume_paren=*/true)
5177 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5178 /* Roll back the tokens we skipped. */
5179 cp_lexer_rollback_tokens (parser->lexer);
5180 /* If we were looking at a compound-literal, simulate an error
5181 so that the call to cp_parser_parse_definitely below will
5183 if (compound_literal_p)
5184 cp_parser_simulate_error (parser);
5187 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5188 parser->in_type_id_in_expr_p = true;
5189 /* Look for the type-id. */
5190 type = cp_parser_type_id (parser);
5191 /* Look for the closing `)'. */
5192 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5193 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5196 /* Restore the saved message. */
5197 parser->type_definition_forbidden_message = saved_message;
5199 /* If ok so far, parse the dependent expression. We cannot be
5200 sure it is a cast. Consider `(T ())'. It is a parenthesized
5201 ctor of T, but looks like a cast to function returning T
5202 without a dependent expression. */
5203 if (!cp_parser_error_occurred (parser))
5204 expr = cp_parser_simple_cast_expression (parser);
5206 if (cp_parser_parse_definitely (parser))
5208 /* Warn about old-style casts, if so requested. */
5209 if (warn_old_style_cast
5210 && !in_system_header
5211 && !VOID_TYPE_P (type)
5212 && current_lang_name != lang_name_c)
5213 warning ("use of old-style cast");
5215 /* Only type conversions to integral or enumeration types
5216 can be used in constant-expressions. */
5217 if (parser->integral_constant_expression_p
5218 && !dependent_type_p (type)
5219 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5220 && (cp_parser_non_integral_constant_expression
5222 "a cast to a type other than an integral or "
5223 "enumeration type")))
5224 return error_mark_node;
5226 /* Perform the cast. */
5227 expr = build_c_cast (type, expr);
5232 /* If we get here, then it's not a cast, so it must be a
5233 unary-expression. */
5234 return cp_parser_unary_expression (parser, address_p);
5237 /* Parse a pm-expression.
5241 pm-expression .* cast-expression
5242 pm-expression ->* cast-expression
5244 Returns a representation of the expression. */
5247 cp_parser_pm_expression (cp_parser* parser)
5249 static const cp_parser_token_tree_map map = {
5250 { CPP_DEREF_STAR, MEMBER_REF },
5251 { CPP_DOT_STAR, DOTSTAR_EXPR },
5252 { CPP_EOF, ERROR_MARK }
5255 return cp_parser_binary_expression (parser, map,
5256 cp_parser_simple_cast_expression);
5259 /* Parse a multiplicative-expression.
5261 multiplicative-expression:
5263 multiplicative-expression * pm-expression
5264 multiplicative-expression / pm-expression
5265 multiplicative-expression % pm-expression
5267 Returns a representation of the expression. */
5270 cp_parser_multiplicative_expression (cp_parser* parser)
5272 static const cp_parser_token_tree_map map = {
5273 { CPP_MULT, MULT_EXPR },
5274 { CPP_DIV, TRUNC_DIV_EXPR },
5275 { CPP_MOD, TRUNC_MOD_EXPR },
5276 { CPP_EOF, ERROR_MARK }
5279 return cp_parser_binary_expression (parser,
5281 cp_parser_pm_expression);
5284 /* Parse an additive-expression.
5286 additive-expression:
5287 multiplicative-expression
5288 additive-expression + multiplicative-expression
5289 additive-expression - multiplicative-expression
5291 Returns a representation of the expression. */
5294 cp_parser_additive_expression (cp_parser* parser)
5296 static const cp_parser_token_tree_map map = {
5297 { CPP_PLUS, PLUS_EXPR },
5298 { CPP_MINUS, MINUS_EXPR },
5299 { CPP_EOF, ERROR_MARK }
5302 return cp_parser_binary_expression (parser,
5304 cp_parser_multiplicative_expression);
5307 /* Parse a shift-expression.
5311 shift-expression << additive-expression
5312 shift-expression >> additive-expression
5314 Returns a representation of the expression. */
5317 cp_parser_shift_expression (cp_parser* parser)
5319 static const cp_parser_token_tree_map map = {
5320 { CPP_LSHIFT, LSHIFT_EXPR },
5321 { CPP_RSHIFT, RSHIFT_EXPR },
5322 { CPP_EOF, ERROR_MARK }
5325 return cp_parser_binary_expression (parser,
5327 cp_parser_additive_expression);
5330 /* Parse a relational-expression.
5332 relational-expression:
5334 relational-expression < shift-expression
5335 relational-expression > shift-expression
5336 relational-expression <= shift-expression
5337 relational-expression >= shift-expression
5341 relational-expression:
5342 relational-expression <? shift-expression
5343 relational-expression >? shift-expression
5345 Returns a representation of the expression. */
5348 cp_parser_relational_expression (cp_parser* parser)
5350 static const cp_parser_token_tree_map map = {
5351 { CPP_LESS, LT_EXPR },
5352 { CPP_GREATER, GT_EXPR },
5353 { CPP_LESS_EQ, LE_EXPR },
5354 { CPP_GREATER_EQ, GE_EXPR },
5355 { CPP_MIN, MIN_EXPR },
5356 { CPP_MAX, MAX_EXPR },
5357 { CPP_EOF, ERROR_MARK }
5360 return cp_parser_binary_expression (parser,
5362 cp_parser_shift_expression);
5365 /* Parse an equality-expression.
5367 equality-expression:
5368 relational-expression
5369 equality-expression == relational-expression
5370 equality-expression != relational-expression
5372 Returns a representation of the expression. */
5375 cp_parser_equality_expression (cp_parser* parser)
5377 static const cp_parser_token_tree_map map = {
5378 { CPP_EQ_EQ, EQ_EXPR },
5379 { CPP_NOT_EQ, NE_EXPR },
5380 { CPP_EOF, ERROR_MARK }
5383 return cp_parser_binary_expression (parser,
5385 cp_parser_relational_expression);
5388 /* Parse an and-expression.
5392 and-expression & equality-expression
5394 Returns a representation of the expression. */
5397 cp_parser_and_expression (cp_parser* parser)
5399 static const cp_parser_token_tree_map map = {
5400 { CPP_AND, BIT_AND_EXPR },
5401 { CPP_EOF, ERROR_MARK }
5404 return cp_parser_binary_expression (parser,
5406 cp_parser_equality_expression);
5409 /* Parse an exclusive-or-expression.
5411 exclusive-or-expression:
5413 exclusive-or-expression ^ and-expression
5415 Returns a representation of the expression. */
5418 cp_parser_exclusive_or_expression (cp_parser* parser)
5420 static const cp_parser_token_tree_map map = {
5421 { CPP_XOR, BIT_XOR_EXPR },
5422 { CPP_EOF, ERROR_MARK }
5425 return cp_parser_binary_expression (parser,
5427 cp_parser_and_expression);
5431 /* Parse an inclusive-or-expression.
5433 inclusive-or-expression:
5434 exclusive-or-expression
5435 inclusive-or-expression | exclusive-or-expression
5437 Returns a representation of the expression. */
5440 cp_parser_inclusive_or_expression (cp_parser* parser)
5442 static const cp_parser_token_tree_map map = {
5443 { CPP_OR, BIT_IOR_EXPR },
5444 { CPP_EOF, ERROR_MARK }
5447 return cp_parser_binary_expression (parser,
5449 cp_parser_exclusive_or_expression);
5452 /* Parse a logical-and-expression.
5454 logical-and-expression:
5455 inclusive-or-expression
5456 logical-and-expression && inclusive-or-expression
5458 Returns a representation of the expression. */
5461 cp_parser_logical_and_expression (cp_parser* parser)
5463 static const cp_parser_token_tree_map map = {
5464 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5465 { CPP_EOF, ERROR_MARK }
5468 return cp_parser_binary_expression (parser,
5470 cp_parser_inclusive_or_expression);
5473 /* Parse a logical-or-expression.
5475 logical-or-expression:
5476 logical-and-expression
5477 logical-or-expression || logical-and-expression
5479 Returns a representation of the expression. */
5482 cp_parser_logical_or_expression (cp_parser* parser)
5484 static const cp_parser_token_tree_map map = {
5485 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5486 { CPP_EOF, ERROR_MARK }
5489 return cp_parser_binary_expression (parser,
5491 cp_parser_logical_and_expression);
5494 /* Parse the `? expression : assignment-expression' part of a
5495 conditional-expression. The LOGICAL_OR_EXPR is the
5496 logical-or-expression that started the conditional-expression.
5497 Returns a representation of the entire conditional-expression.
5499 This routine is used by cp_parser_assignment_expression.
5501 ? expression : assignment-expression
5505 ? : assignment-expression */
5508 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5511 tree assignment_expr;
5513 /* Consume the `?' token. */
5514 cp_lexer_consume_token (parser->lexer);
5515 if (cp_parser_allow_gnu_extensions_p (parser)
5516 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5517 /* Implicit true clause. */
5520 /* Parse the expression. */
5521 expr = cp_parser_expression (parser);
5523 /* The next token should be a `:'. */
5524 cp_parser_require (parser, CPP_COLON, "`:'");
5525 /* Parse the assignment-expression. */
5526 assignment_expr = cp_parser_assignment_expression (parser);
5528 /* Build the conditional-expression. */
5529 return build_x_conditional_expr (logical_or_expr,
5534 /* Parse an assignment-expression.
5536 assignment-expression:
5537 conditional-expression
5538 logical-or-expression assignment-operator assignment_expression
5541 Returns a representation for the expression. */
5544 cp_parser_assignment_expression (cp_parser* parser)
5548 /* If the next token is the `throw' keyword, then we're looking at
5549 a throw-expression. */
5550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5551 expr = cp_parser_throw_expression (parser);
5552 /* Otherwise, it must be that we are looking at a
5553 logical-or-expression. */
5556 /* Parse the logical-or-expression. */
5557 expr = cp_parser_logical_or_expression (parser);
5558 /* If the next token is a `?' then we're actually looking at a
5559 conditional-expression. */
5560 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5561 return cp_parser_question_colon_clause (parser, expr);
5564 enum tree_code assignment_operator;
5566 /* If it's an assignment-operator, we're using the second
5569 = cp_parser_assignment_operator_opt (parser);
5570 if (assignment_operator != ERROR_MARK)
5574 /* Parse the right-hand side of the assignment. */
5575 rhs = cp_parser_assignment_expression (parser);
5576 /* An assignment may not appear in a
5577 constant-expression. */
5578 if (cp_parser_non_integral_constant_expression (parser,
5580 return error_mark_node;
5581 /* Build the assignment expression. */
5582 expr = build_x_modify_expr (expr,
5583 assignment_operator,
5592 /* Parse an (optional) assignment-operator.
5594 assignment-operator: one of
5595 = *= /= %= += -= >>= <<= &= ^= |=
5599 assignment-operator: one of
5602 If the next token is an assignment operator, the corresponding tree
5603 code is returned, and the token is consumed. For example, for
5604 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5605 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5606 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5607 operator, ERROR_MARK is returned. */
5609 static enum tree_code
5610 cp_parser_assignment_operator_opt (cp_parser* parser)
5615 /* Peek at the next toen. */
5616 token = cp_lexer_peek_token (parser->lexer);
5618 switch (token->type)
5629 op = TRUNC_DIV_EXPR;
5633 op = TRUNC_MOD_EXPR;
5673 /* Nothing else is an assignment operator. */
5677 /* If it was an assignment operator, consume it. */
5678 if (op != ERROR_MARK)
5679 cp_lexer_consume_token (parser->lexer);
5684 /* Parse an expression.
5687 assignment-expression
5688 expression , assignment-expression
5690 Returns a representation of the expression. */
5693 cp_parser_expression (cp_parser* parser)
5695 tree expression = NULL_TREE;
5699 tree assignment_expression;
5701 /* Parse the next assignment-expression. */
5702 assignment_expression
5703 = cp_parser_assignment_expression (parser);
5704 /* If this is the first assignment-expression, we can just
5707 expression = assignment_expression;
5709 expression = build_x_compound_expr (expression,
5710 assignment_expression);
5711 /* If the next token is not a comma, then we are done with the
5713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5715 /* Consume the `,'. */
5716 cp_lexer_consume_token (parser->lexer);
5717 /* A comma operator cannot appear in a constant-expression. */
5718 if (cp_parser_non_integral_constant_expression (parser,
5719 "a comma operator"))
5720 expression = error_mark_node;
5726 /* Parse a constant-expression.
5728 constant-expression:
5729 conditional-expression
5731 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5732 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5733 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5734 is false, NON_CONSTANT_P should be NULL. */
5737 cp_parser_constant_expression (cp_parser* parser,
5738 bool allow_non_constant_p,
5739 bool *non_constant_p)
5741 bool saved_integral_constant_expression_p;
5742 bool saved_allow_non_integral_constant_expression_p;
5743 bool saved_non_integral_constant_expression_p;
5746 /* It might seem that we could simply parse the
5747 conditional-expression, and then check to see if it were
5748 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5749 one that the compiler can figure out is constant, possibly after
5750 doing some simplifications or optimizations. The standard has a
5751 precise definition of constant-expression, and we must honor
5752 that, even though it is somewhat more restrictive.
5758 is not a legal declaration, because `(2, 3)' is not a
5759 constant-expression. The `,' operator is forbidden in a
5760 constant-expression. However, GCC's constant-folding machinery
5761 will fold this operation to an INTEGER_CST for `3'. */
5763 /* Save the old settings. */
5764 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5765 saved_allow_non_integral_constant_expression_p
5766 = parser->allow_non_integral_constant_expression_p;
5767 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5768 /* We are now parsing a constant-expression. */
5769 parser->integral_constant_expression_p = true;
5770 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5771 parser->non_integral_constant_expression_p = false;
5772 /* Although the grammar says "conditional-expression", we parse an
5773 "assignment-expression", which also permits "throw-expression"
5774 and the use of assignment operators. In the case that
5775 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5776 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5777 actually essential that we look for an assignment-expression.
5778 For example, cp_parser_initializer_clauses uses this function to
5779 determine whether a particular assignment-expression is in fact
5781 expression = cp_parser_assignment_expression (parser);
5782 /* Restore the old settings. */
5783 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5784 parser->allow_non_integral_constant_expression_p
5785 = saved_allow_non_integral_constant_expression_p;
5786 if (allow_non_constant_p)
5787 *non_constant_p = parser->non_integral_constant_expression_p;
5788 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5793 /* Parse __builtin_offsetof.
5795 offsetof-expression:
5796 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5798 offsetof-member-designator:
5800 | offsetof-member-designator "." id-expression
5801 | offsetof-member-designator "[" expression "]"
5805 cp_parser_builtin_offsetof (cp_parser *parser)
5807 int save_ice_p, save_non_ice_p;
5811 /* We're about to accept non-integral-constant things, but will
5812 definitely yield an integral constant expression. Save and
5813 restore these values around our local parsing. */
5814 save_ice_p = parser->integral_constant_expression_p;
5815 save_non_ice_p = parser->non_integral_constant_expression_p;
5817 /* Consume the "__builtin_offsetof" token. */
5818 cp_lexer_consume_token (parser->lexer);
5819 /* Consume the opening `('. */
5820 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5821 /* Parse the type-id. */
5822 type = cp_parser_type_id (parser);
5823 /* Look for the `,'. */
5824 cp_parser_require (parser, CPP_COMMA, "`,'");
5826 /* Build the (type *)null that begins the traditional offsetof macro. */
5827 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5829 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5830 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5834 cp_token *token = cp_lexer_peek_token (parser->lexer);
5835 switch (token->type)
5837 case CPP_OPEN_SQUARE:
5838 /* offsetof-member-designator "[" expression "]" */
5839 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5843 /* offsetof-member-designator "." identifier */
5844 cp_lexer_consume_token (parser->lexer);
5845 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5849 case CPP_CLOSE_PAREN:
5850 /* Consume the ")" token. */
5851 cp_lexer_consume_token (parser->lexer);
5855 /* Error. We know the following require will fail, but
5856 that gives the proper error message. */
5857 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5858 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5859 expr = error_mark_node;
5865 /* We've finished the parsing, now finish with the semantics. At present
5866 we're just mirroring the traditional macro implementation. Better
5867 would be to do the lowering of the ADDR_EXPR to flat pointer arithmetic
5868 here rather than in build_x_unary_op. */
5869 expr = build_reinterpret_cast (build_reference_type (char_type_node), expr);
5870 expr = build_x_unary_op (ADDR_EXPR, expr);
5871 expr = build_reinterpret_cast (size_type_node, expr);
5874 parser->integral_constant_expression_p = save_ice_p;
5875 parser->non_integral_constant_expression_p = save_non_ice_p;
5880 /* Statements [gram.stmt.stmt] */
5882 /* Parse a statement.
5886 expression-statement
5891 declaration-statement
5895 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5899 location_t statement_locus;
5901 /* There is no statement yet. */
5902 statement = NULL_TREE;
5903 /* Peek at the next token. */
5904 token = cp_lexer_peek_token (parser->lexer);
5905 /* Remember the location of the first token in the statement. */
5906 statement_locus = token->location;
5907 /* If this is a keyword, then that will often determine what kind of
5908 statement we have. */
5909 if (token->type == CPP_KEYWORD)
5911 enum rid keyword = token->keyword;
5917 statement = cp_parser_labeled_statement (parser,
5923 statement = cp_parser_selection_statement (parser);
5929 statement = cp_parser_iteration_statement (parser);
5936 statement = cp_parser_jump_statement (parser);
5940 statement = cp_parser_try_block (parser);
5944 /* It might be a keyword like `int' that can start a
5945 declaration-statement. */
5949 else if (token->type == CPP_NAME)
5951 /* If the next token is a `:', then we are looking at a
5952 labeled-statement. */
5953 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5954 if (token->type == CPP_COLON)
5955 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5957 /* Anything that starts with a `{' must be a compound-statement. */
5958 else if (token->type == CPP_OPEN_BRACE)
5959 statement = cp_parser_compound_statement (parser, NULL, false);
5961 /* Everything else must be a declaration-statement or an
5962 expression-statement. Try for the declaration-statement
5963 first, unless we are looking at a `;', in which case we know that
5964 we have an expression-statement. */
5967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5969 cp_parser_parse_tentatively (parser);
5970 /* Try to parse the declaration-statement. */
5971 cp_parser_declaration_statement (parser);
5972 /* If that worked, we're done. */
5973 if (cp_parser_parse_definitely (parser))
5976 /* Look for an expression-statement instead. */
5977 statement = cp_parser_expression_statement (parser, in_statement_expr);
5980 /* Set the line number for the statement. */
5981 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5983 SET_EXPR_LOCUS (statement, NULL);
5984 annotate_with_locus (statement, statement_locus);
5988 /* Parse a labeled-statement.
5991 identifier : statement
5992 case constant-expression : statement
5998 case constant-expression ... constant-expression : statement
6000 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6001 For an ordinary label, returns a LABEL_EXPR. */
6004 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6007 tree statement = error_mark_node;
6009 /* The next token should be an identifier. */
6010 token = cp_lexer_peek_token (parser->lexer);
6011 if (token->type != CPP_NAME
6012 && token->type != CPP_KEYWORD)
6014 cp_parser_error (parser, "expected labeled-statement");
6015 return error_mark_node;
6018 switch (token->keyword)
6025 /* Consume the `case' token. */
6026 cp_lexer_consume_token (parser->lexer);
6027 /* Parse the constant-expression. */
6028 expr = cp_parser_constant_expression (parser,
6029 /*allow_non_constant_p=*/false,
6032 ellipsis = cp_lexer_peek_token (parser->lexer);
6033 if (ellipsis->type == CPP_ELLIPSIS)
6035 /* Consume the `...' token. */
6036 cp_lexer_consume_token (parser->lexer);
6038 cp_parser_constant_expression (parser,
6039 /*allow_non_constant_p=*/false,
6041 /* We don't need to emit warnings here, as the common code
6042 will do this for us. */
6045 expr_hi = NULL_TREE;
6047 if (!parser->in_switch_statement_p)
6048 error ("case label `%E' not within a switch statement", expr);
6050 statement = finish_case_label (expr, expr_hi);
6055 /* Consume the `default' token. */
6056 cp_lexer_consume_token (parser->lexer);
6057 if (!parser->in_switch_statement_p)
6058 error ("case label not within a switch statement");
6060 statement = finish_case_label (NULL_TREE, NULL_TREE);
6064 /* Anything else must be an ordinary label. */
6065 statement = finish_label_stmt (cp_parser_identifier (parser));
6069 /* Require the `:' token. */
6070 cp_parser_require (parser, CPP_COLON, "`:'");
6071 /* Parse the labeled statement. */
6072 cp_parser_statement (parser, in_statement_expr);
6074 /* Return the label, in the case of a `case' or `default' label. */
6078 /* Parse an expression-statement.
6080 expression-statement:
6083 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6084 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6085 indicates whether this expression-statement is part of an
6086 expression statement. */
6089 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6091 tree statement = NULL_TREE;
6093 /* If the next token is a ';', then there is no expression
6095 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6096 statement = cp_parser_expression (parser);
6098 /* Consume the final `;'. */
6099 cp_parser_consume_semicolon_at_end_of_statement (parser);
6101 if (in_statement_expr
6102 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6104 /* This is the final expression statement of a statement
6106 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6109 statement = finish_expr_stmt (statement);
6116 /* Parse a compound-statement.
6119 { statement-seq [opt] }
6121 Returns a tree representing the statement. */
6124 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6129 /* Consume the `{'. */
6130 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6131 return error_mark_node;
6132 /* Begin the compound-statement. */
6133 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6134 /* Parse an (optional) statement-seq. */
6135 cp_parser_statement_seq_opt (parser, in_statement_expr);
6136 /* Finish the compound-statement. */
6137 finish_compound_stmt (compound_stmt);
6138 /* Consume the `}'. */
6139 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6141 return compound_stmt;
6144 /* Parse an (optional) statement-seq.
6148 statement-seq [opt] statement */
6151 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6153 /* Scan statements until there aren't any more. */
6156 /* If we're looking at a `}', then we've run out of statements. */
6157 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6158 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6161 /* Parse the statement. */
6162 cp_parser_statement (parser, in_statement_expr);
6166 /* Parse a selection-statement.
6168 selection-statement:
6169 if ( condition ) statement
6170 if ( condition ) statement else statement
6171 switch ( condition ) statement
6173 Returns the new IF_STMT or SWITCH_STMT. */
6176 cp_parser_selection_statement (cp_parser* parser)
6181 /* Peek at the next token. */
6182 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6184 /* See what kind of keyword it is. */
6185 keyword = token->keyword;
6194 /* Look for the `('. */
6195 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6197 cp_parser_skip_to_end_of_statement (parser);
6198 return error_mark_node;
6201 /* Begin the selection-statement. */
6202 if (keyword == RID_IF)
6203 statement = begin_if_stmt ();
6205 statement = begin_switch_stmt ();
6207 /* Parse the condition. */
6208 condition = cp_parser_condition (parser);
6209 /* Look for the `)'. */
6210 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6211 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6212 /*consume_paren=*/true);
6214 if (keyword == RID_IF)
6216 /* Add the condition. */
6217 finish_if_stmt_cond (condition, statement);
6219 /* Parse the then-clause. */
6220 cp_parser_implicitly_scoped_statement (parser);
6221 finish_then_clause (statement);
6223 /* If the next token is `else', parse the else-clause. */
6224 if (cp_lexer_next_token_is_keyword (parser->lexer,
6227 /* Consume the `else' keyword. */
6228 cp_lexer_consume_token (parser->lexer);
6229 begin_else_clause (statement);
6230 /* Parse the else-clause. */
6231 cp_parser_implicitly_scoped_statement (parser);
6232 finish_else_clause (statement);
6235 /* Now we're all done with the if-statement. */
6236 finish_if_stmt (statement);
6240 bool in_switch_statement_p;
6242 /* Add the condition. */
6243 finish_switch_cond (condition, statement);
6245 /* Parse the body of the switch-statement. */
6246 in_switch_statement_p = parser->in_switch_statement_p;
6247 parser->in_switch_statement_p = true;
6248 cp_parser_implicitly_scoped_statement (parser);
6249 parser->in_switch_statement_p = in_switch_statement_p;
6251 /* Now we're all done with the switch-statement. */
6252 finish_switch_stmt (statement);
6260 cp_parser_error (parser, "expected selection-statement");
6261 return error_mark_node;
6265 /* Parse a condition.
6269 type-specifier-seq declarator = assignment-expression
6274 type-specifier-seq declarator asm-specification [opt]
6275 attributes [opt] = assignment-expression
6277 Returns the expression that should be tested. */
6280 cp_parser_condition (cp_parser* parser)
6282 cp_decl_specifier_seq type_specifiers;
6283 const char *saved_message;
6285 /* Try the declaration first. */
6286 cp_parser_parse_tentatively (parser);
6287 /* New types are not allowed in the type-specifier-seq for a
6289 saved_message = parser->type_definition_forbidden_message;
6290 parser->type_definition_forbidden_message
6291 = "types may not be defined in conditions";
6292 /* Parse the type-specifier-seq. */
6293 cp_parser_type_specifier_seq (parser, &type_specifiers);
6294 /* Restore the saved message. */
6295 parser->type_definition_forbidden_message = saved_message;
6296 /* If all is well, we might be looking at a declaration. */
6297 if (!cp_parser_error_occurred (parser))
6300 tree asm_specification;
6302 cp_declarator *declarator;
6303 tree initializer = NULL_TREE;
6305 /* Parse the declarator. */
6306 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6307 /*ctor_dtor_or_conv_p=*/NULL,
6308 /*parenthesized_p=*/NULL);
6309 /* Parse the attributes. */
6310 attributes = cp_parser_attributes_opt (parser);
6311 /* Parse the asm-specification. */
6312 asm_specification = cp_parser_asm_specification_opt (parser);
6313 /* If the next token is not an `=', then we might still be
6314 looking at an expression. For example:
6318 looks like a decl-specifier-seq and a declarator -- but then
6319 there is no `=', so this is an expression. */
6320 cp_parser_require (parser, CPP_EQ, "`='");
6321 /* If we did see an `=', then we are looking at a declaration
6323 if (cp_parser_parse_definitely (parser))
6325 /* Create the declaration. */
6326 decl = start_decl (declarator, &type_specifiers,
6327 /*initialized_p=*/true,
6328 attributes, /*prefix_attributes=*/NULL_TREE);
6329 /* Parse the assignment-expression. */
6330 initializer = cp_parser_assignment_expression (parser);
6332 /* Process the initializer. */
6333 cp_finish_decl (decl,
6336 LOOKUP_ONLYCONVERTING);
6338 return convert_from_reference (decl);
6341 /* If we didn't even get past the declarator successfully, we are
6342 definitely not looking at a declaration. */
6344 cp_parser_abort_tentative_parse (parser);
6346 /* Otherwise, we are looking at an expression. */
6347 return cp_parser_expression (parser);
6350 /* Parse an iteration-statement.
6352 iteration-statement:
6353 while ( condition ) statement
6354 do statement while ( expression ) ;
6355 for ( for-init-statement condition [opt] ; expression [opt] )
6358 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6361 cp_parser_iteration_statement (cp_parser* parser)
6366 bool in_iteration_statement_p;
6369 /* Peek at the next token. */
6370 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6372 return error_mark_node;
6374 /* Remember whether or not we are already within an iteration
6376 in_iteration_statement_p = parser->in_iteration_statement_p;
6378 /* See what kind of keyword it is. */
6379 keyword = token->keyword;
6386 /* Begin the while-statement. */
6387 statement = begin_while_stmt ();
6388 /* Look for the `('. */
6389 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6390 /* Parse the condition. */
6391 condition = cp_parser_condition (parser);
6392 finish_while_stmt_cond (condition, statement);
6393 /* Look for the `)'. */
6394 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6395 /* Parse the dependent statement. */
6396 parser->in_iteration_statement_p = true;
6397 cp_parser_already_scoped_statement (parser);
6398 parser->in_iteration_statement_p = in_iteration_statement_p;
6399 /* We're done with the while-statement. */
6400 finish_while_stmt (statement);
6408 /* Begin the do-statement. */
6409 statement = begin_do_stmt ();
6410 /* Parse the body of the do-statement. */
6411 parser->in_iteration_statement_p = true;
6412 cp_parser_implicitly_scoped_statement (parser);
6413 parser->in_iteration_statement_p = in_iteration_statement_p;
6414 finish_do_body (statement);
6415 /* Look for the `while' keyword. */
6416 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6417 /* Look for the `('. */
6418 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6419 /* Parse the expression. */
6420 expression = cp_parser_expression (parser);
6421 /* We're done with the do-statement. */
6422 finish_do_stmt (expression, statement);
6423 /* Look for the `)'. */
6424 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6425 /* Look for the `;'. */
6426 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6432 tree condition = NULL_TREE;
6433 tree expression = NULL_TREE;
6435 /* Begin the for-statement. */
6436 statement = begin_for_stmt ();
6437 /* Look for the `('. */
6438 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6439 /* Parse the initialization. */
6440 cp_parser_for_init_statement (parser);
6441 finish_for_init_stmt (statement);
6443 /* If there's a condition, process it. */
6444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6445 condition = cp_parser_condition (parser);
6446 finish_for_cond (condition, statement);
6447 /* Look for the `;'. */
6448 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6450 /* If there's an expression, process it. */
6451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6452 expression = cp_parser_expression (parser);
6453 finish_for_expr (expression, statement);
6454 /* Look for the `)'. */
6455 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6457 /* Parse the body of the for-statement. */
6458 parser->in_iteration_statement_p = true;
6459 cp_parser_already_scoped_statement (parser);
6460 parser->in_iteration_statement_p = in_iteration_statement_p;
6462 /* We're done with the for-statement. */
6463 finish_for_stmt (statement);
6468 cp_parser_error (parser, "expected iteration-statement");
6469 statement = error_mark_node;
6476 /* Parse a for-init-statement.
6479 expression-statement
6480 simple-declaration */
6483 cp_parser_for_init_statement (cp_parser* parser)
6485 /* If the next token is a `;', then we have an empty
6486 expression-statement. Grammatically, this is also a
6487 simple-declaration, but an invalid one, because it does not
6488 declare anything. Therefore, if we did not handle this case
6489 specially, we would issue an error message about an invalid
6491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6493 /* We're going to speculatively look for a declaration, falling back
6494 to an expression, if necessary. */
6495 cp_parser_parse_tentatively (parser);
6496 /* Parse the declaration. */
6497 cp_parser_simple_declaration (parser,
6498 /*function_definition_allowed_p=*/false);
6499 /* If the tentative parse failed, then we shall need to look for an
6500 expression-statement. */
6501 if (cp_parser_parse_definitely (parser))
6505 cp_parser_expression_statement (parser, false);
6508 /* Parse a jump-statement.
6513 return expression [opt] ;
6521 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6524 cp_parser_jump_statement (cp_parser* parser)
6526 tree statement = error_mark_node;
6530 /* Peek at the next token. */
6531 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6533 return error_mark_node;
6535 /* See what kind of keyword it is. */
6536 keyword = token->keyword;
6540 if (!parser->in_switch_statement_p
6541 && !parser->in_iteration_statement_p)
6543 error ("break statement not within loop or switch");
6544 statement = error_mark_node;
6547 statement = finish_break_stmt ();
6548 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6552 if (!parser->in_iteration_statement_p)
6554 error ("continue statement not within a loop");
6555 statement = error_mark_node;
6558 statement = finish_continue_stmt ();
6559 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6566 /* If the next token is a `;', then there is no
6568 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6569 expr = cp_parser_expression (parser);
6572 /* Build the return-statement. */
6573 statement = finish_return_stmt (expr);
6574 /* Look for the final `;'. */
6575 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6580 /* Create the goto-statement. */
6581 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6583 /* Issue a warning about this use of a GNU extension. */
6585 pedwarn ("ISO C++ forbids computed gotos");
6586 /* Consume the '*' token. */
6587 cp_lexer_consume_token (parser->lexer);
6588 /* Parse the dependent expression. */
6589 finish_goto_stmt (cp_parser_expression (parser));
6592 finish_goto_stmt (cp_parser_identifier (parser));
6593 /* Look for the final `;'. */
6594 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6598 cp_parser_error (parser, "expected jump-statement");
6605 /* Parse a declaration-statement.
6607 declaration-statement:
6608 block-declaration */
6611 cp_parser_declaration_statement (cp_parser* parser)
6615 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6616 p = obstack_alloc (&declarator_obstack, 0);
6618 /* Parse the block-declaration. */
6619 cp_parser_block_declaration (parser, /*statement_p=*/true);
6621 /* Free any declarators allocated. */
6622 obstack_free (&declarator_obstack, p);
6624 /* Finish off the statement. */
6628 /* Some dependent statements (like `if (cond) statement'), are
6629 implicitly in their own scope. In other words, if the statement is
6630 a single statement (as opposed to a compound-statement), it is
6631 none-the-less treated as if it were enclosed in braces. Any
6632 declarations appearing in the dependent statement are out of scope
6633 after control passes that point. This function parses a statement,
6634 but ensures that is in its own scope, even if it is not a
6637 Returns the new statement. */
6640 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6644 /* If the token is not a `{', then we must take special action. */
6645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6647 /* Create a compound-statement. */
6648 statement = begin_compound_stmt (0);
6649 /* Parse the dependent-statement. */
6650 cp_parser_statement (parser, false);
6651 /* Finish the dummy compound-statement. */
6652 finish_compound_stmt (statement);
6654 /* Otherwise, we simply parse the statement directly. */
6656 statement = cp_parser_compound_statement (parser, NULL, false);
6658 /* Return the statement. */
6662 /* For some dependent statements (like `while (cond) statement'), we
6663 have already created a scope. Therefore, even if the dependent
6664 statement is a compound-statement, we do not want to create another
6668 cp_parser_already_scoped_statement (cp_parser* parser)
6670 /* If the token is a `{', then we must take special action. */
6671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6672 cp_parser_statement (parser, false);
6675 /* Avoid calling cp_parser_compound_statement, so that we
6676 don't create a new scope. Do everything else by hand. */
6677 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6678 cp_parser_statement_seq_opt (parser, false);
6679 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6683 /* Declarations [gram.dcl.dcl] */
6685 /* Parse an optional declaration-sequence.
6689 declaration-seq declaration */
6692 cp_parser_declaration_seq_opt (cp_parser* parser)
6698 token = cp_lexer_peek_token (parser->lexer);
6700 if (token->type == CPP_CLOSE_BRACE
6701 || token->type == CPP_EOF)
6704 if (token->type == CPP_SEMICOLON)
6706 /* A declaration consisting of a single semicolon is
6707 invalid. Allow it unless we're being pedantic. */
6708 if (pedantic && !in_system_header)
6709 pedwarn ("extra `;'");
6710 cp_lexer_consume_token (parser->lexer);
6714 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6715 parser to enter or exit implicit `extern "C"' blocks. */
6716 while (pending_lang_change > 0)
6718 push_lang_context (lang_name_c);
6719 --pending_lang_change;
6721 while (pending_lang_change < 0)
6723 pop_lang_context ();
6724 ++pending_lang_change;
6727 /* Parse the declaration itself. */
6728 cp_parser_declaration (parser);
6732 /* Parse a declaration.
6737 template-declaration
6738 explicit-instantiation
6739 explicit-specialization
6740 linkage-specification
6741 namespace-definition
6746 __extension__ declaration */
6749 cp_parser_declaration (cp_parser* parser)
6756 /* Set this here since we can be called after
6757 pushing the linkage specification. */
6758 c_lex_string_translate = 1;
6760 /* Check for the `__extension__' keyword. */
6761 if (cp_parser_extension_opt (parser, &saved_pedantic))
6763 /* Parse the qualified declaration. */
6764 cp_parser_declaration (parser);
6765 /* Restore the PEDANTIC flag. */
6766 pedantic = saved_pedantic;
6771 /* Try to figure out what kind of declaration is present. */
6772 token1 = *cp_lexer_peek_token (parser->lexer);
6774 /* Don't translate the CPP_STRING in extern "C". */
6775 if (token1.keyword == RID_EXTERN)
6776 c_lex_string_translate = 0;
6778 if (token1.type != CPP_EOF)
6779 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6781 c_lex_string_translate = 1;
6783 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6784 p = obstack_alloc (&declarator_obstack, 0);
6786 /* If the next token is `extern' and the following token is a string
6787 literal, then we have a linkage specification. */
6788 if (token1.keyword == RID_EXTERN
6789 && cp_parser_is_string_literal (&token2))
6790 cp_parser_linkage_specification (parser);
6791 /* If the next token is `template', then we have either a template
6792 declaration, an explicit instantiation, or an explicit
6794 else if (token1.keyword == RID_TEMPLATE)
6796 /* `template <>' indicates a template specialization. */
6797 if (token2.type == CPP_LESS
6798 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6799 cp_parser_explicit_specialization (parser);
6800 /* `template <' indicates a template declaration. */
6801 else if (token2.type == CPP_LESS)
6802 cp_parser_template_declaration (parser, /*member_p=*/false);
6803 /* Anything else must be an explicit instantiation. */
6805 cp_parser_explicit_instantiation (parser);
6807 /* If the next token is `export', then we have a template
6809 else if (token1.keyword == RID_EXPORT)
6810 cp_parser_template_declaration (parser, /*member_p=*/false);
6811 /* If the next token is `extern', 'static' or 'inline' and the one
6812 after that is `template', we have a GNU extended explicit
6813 instantiation directive. */
6814 else if (cp_parser_allow_gnu_extensions_p (parser)
6815 && (token1.keyword == RID_EXTERN
6816 || token1.keyword == RID_STATIC
6817 || token1.keyword == RID_INLINE)
6818 && token2.keyword == RID_TEMPLATE)
6819 cp_parser_explicit_instantiation (parser);
6820 /* If the next token is `namespace', check for a named or unnamed
6821 namespace definition. */
6822 else if (token1.keyword == RID_NAMESPACE
6823 && (/* A named namespace definition. */
6824 (token2.type == CPP_NAME
6825 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6827 /* An unnamed namespace definition. */
6828 || token2.type == CPP_OPEN_BRACE))
6829 cp_parser_namespace_definition (parser);
6830 /* We must have either a block declaration or a function
6833 /* Try to parse a block-declaration, or a function-definition. */
6834 cp_parser_block_declaration (parser, /*statement_p=*/false);
6836 /* Free any declarators allocated. */
6837 obstack_free (&declarator_obstack, p);
6840 /* Parse a block-declaration.
6845 namespace-alias-definition
6852 __extension__ block-declaration
6855 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6856 part of a declaration-statement. */
6859 cp_parser_block_declaration (cp_parser *parser,
6865 /* Check for the `__extension__' keyword. */
6866 if (cp_parser_extension_opt (parser, &saved_pedantic))
6868 /* Parse the qualified declaration. */
6869 cp_parser_block_declaration (parser, statement_p);
6870 /* Restore the PEDANTIC flag. */
6871 pedantic = saved_pedantic;
6876 /* Peek at the next token to figure out which kind of declaration is
6878 token1 = cp_lexer_peek_token (parser->lexer);
6880 /* If the next keyword is `asm', we have an asm-definition. */
6881 if (token1->keyword == RID_ASM)
6884 cp_parser_commit_to_tentative_parse (parser);
6885 cp_parser_asm_definition (parser);
6887 /* If the next keyword is `namespace', we have a
6888 namespace-alias-definition. */
6889 else if (token1->keyword == RID_NAMESPACE)
6890 cp_parser_namespace_alias_definition (parser);
6891 /* If the next keyword is `using', we have either a
6892 using-declaration or a using-directive. */
6893 else if (token1->keyword == RID_USING)
6898 cp_parser_commit_to_tentative_parse (parser);
6899 /* If the token after `using' is `namespace', then we have a
6901 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6902 if (token2->keyword == RID_NAMESPACE)
6903 cp_parser_using_directive (parser);
6904 /* Otherwise, it's a using-declaration. */
6906 cp_parser_using_declaration (parser);
6908 /* If the next keyword is `__label__' we have a label declaration. */
6909 else if (token1->keyword == RID_LABEL)
6912 cp_parser_commit_to_tentative_parse (parser);
6913 cp_parser_label_declaration (parser);
6915 /* Anything else must be a simple-declaration. */
6917 cp_parser_simple_declaration (parser, !statement_p);
6920 /* Parse a simple-declaration.
6923 decl-specifier-seq [opt] init-declarator-list [opt] ;
6925 init-declarator-list:
6927 init-declarator-list , init-declarator
6929 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6930 function-definition as a simple-declaration. */
6933 cp_parser_simple_declaration (cp_parser* parser,
6934 bool function_definition_allowed_p)
6936 cp_decl_specifier_seq decl_specifiers;
6937 int declares_class_or_enum;
6938 bool saw_declarator;
6940 /* Defer access checks until we know what is being declared; the
6941 checks for names appearing in the decl-specifier-seq should be
6942 done as if we were in the scope of the thing being declared. */
6943 push_deferring_access_checks (dk_deferred);
6945 /* Parse the decl-specifier-seq. We have to keep track of whether
6946 or not the decl-specifier-seq declares a named class or
6947 enumeration type, since that is the only case in which the
6948 init-declarator-list is allowed to be empty.
6952 In a simple-declaration, the optional init-declarator-list can be
6953 omitted only when declaring a class or enumeration, that is when
6954 the decl-specifier-seq contains either a class-specifier, an
6955 elaborated-type-specifier, or an enum-specifier. */
6956 cp_parser_decl_specifier_seq (parser,
6957 CP_PARSER_FLAGS_OPTIONAL,
6959 &declares_class_or_enum);
6960 /* We no longer need to defer access checks. */
6961 stop_deferring_access_checks ();
6963 /* In a block scope, a valid declaration must always have a
6964 decl-specifier-seq. By not trying to parse declarators, we can
6965 resolve the declaration/expression ambiguity more quickly. */
6966 if (!function_definition_allowed_p
6967 && !decl_specifiers.any_specifiers_p)
6969 cp_parser_error (parser, "expected declaration");
6973 /* If the next two tokens are both identifiers, the code is
6974 erroneous. The usual cause of this situation is code like:
6978 where "T" should name a type -- but does not. */
6979 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6981 /* If parsing tentatively, we should commit; we really are
6982 looking at a declaration. */
6983 cp_parser_commit_to_tentative_parse (parser);
6988 /* Keep going until we hit the `;' at the end of the simple
6990 saw_declarator = false;
6991 while (cp_lexer_next_token_is_not (parser->lexer,
6995 bool function_definition_p;
6998 saw_declarator = true;
6999 /* Parse the init-declarator. */
7000 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7001 function_definition_allowed_p,
7003 declares_class_or_enum,
7004 &function_definition_p);
7005 /* If an error occurred while parsing tentatively, exit quickly.
7006 (That usually happens when in the body of a function; each
7007 statement is treated as a declaration-statement until proven
7009 if (cp_parser_error_occurred (parser))
7011 /* Handle function definitions specially. */
7012 if (function_definition_p)
7014 /* If the next token is a `,', then we are probably
7015 processing something like:
7019 which is erroneous. */
7020 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7021 error ("mixing declarations and function-definitions is forbidden");
7022 /* Otherwise, we're done with the list of declarators. */
7025 pop_deferring_access_checks ();
7029 /* The next token should be either a `,' or a `;'. */
7030 token = cp_lexer_peek_token (parser->lexer);
7031 /* If it's a `,', there are more declarators to come. */
7032 if (token->type == CPP_COMMA)
7033 cp_lexer_consume_token (parser->lexer);
7034 /* If it's a `;', we are done. */
7035 else if (token->type == CPP_SEMICOLON)
7037 /* Anything else is an error. */
7040 cp_parser_error (parser, "expected `,' or `;'");
7041 /* Skip tokens until we reach the end of the statement. */
7042 cp_parser_skip_to_end_of_statement (parser);
7043 /* If the next token is now a `;', consume it. */
7044 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7045 cp_lexer_consume_token (parser->lexer);
7048 /* After the first time around, a function-definition is not
7049 allowed -- even if it was OK at first. For example:
7054 function_definition_allowed_p = false;
7057 /* Issue an error message if no declarators are present, and the
7058 decl-specifier-seq does not itself declare a class or
7060 if (!saw_declarator)
7062 if (cp_parser_declares_only_class_p (parser))
7063 shadow_tag (&decl_specifiers);
7064 /* Perform any deferred access checks. */
7065 perform_deferred_access_checks ();
7068 /* Consume the `;'. */
7069 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7072 pop_deferring_access_checks ();
7075 /* Parse a decl-specifier-seq.
7078 decl-specifier-seq [opt] decl-specifier
7081 storage-class-specifier
7092 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7094 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
7095 appears, and the entity that will be a friend is not going to be a
7096 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
7097 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
7098 friendship is granted might not be a class.
7100 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7103 1: one of the decl-specifiers is an elaborated-type-specifier
7104 (i.e., a type declaration)
7105 2: one of the decl-specifiers is an enum-specifier or a
7106 class-specifier (i.e., a type definition)
7111 cp_parser_decl_specifier_seq (cp_parser* parser,
7112 cp_parser_flags flags,
7113 cp_decl_specifier_seq *decl_specs,
7114 int* declares_class_or_enum)
7116 bool constructor_possible_p = !parser->in_declarator_p;
7118 /* Clear DECL_SPECS. */
7119 clear_decl_specs (decl_specs);
7121 /* Assume no class or enumeration type is declared. */
7122 *declares_class_or_enum = 0;
7124 /* Keep reading specifiers until there are no more to read. */
7128 bool found_decl_spec;
7131 /* Peek at the next token. */
7132 token = cp_lexer_peek_token (parser->lexer);
7133 /* Handle attributes. */
7134 if (token->keyword == RID_ATTRIBUTE)
7136 /* Parse the attributes. */
7137 decl_specs->attributes
7138 = chainon (decl_specs->attributes,
7139 cp_parser_attributes_opt (parser));
7142 /* Assume we will find a decl-specifier keyword. */
7143 found_decl_spec = true;
7144 /* If the next token is an appropriate keyword, we can simply
7145 add it to the list. */
7146 switch (token->keyword)
7151 if (decl_specs->specs[(int) ds_friend]++)
7152 error ("duplicate `friend'");
7153 /* Consume the token. */
7154 cp_lexer_consume_token (parser->lexer);
7157 /* function-specifier:
7164 cp_parser_function_specifier_opt (parser, decl_specs);
7170 ++decl_specs->specs[(int) ds_typedef];
7171 /* Consume the token. */
7172 cp_lexer_consume_token (parser->lexer);
7173 /* A constructor declarator cannot appear in a typedef. */
7174 constructor_possible_p = false;
7175 /* The "typedef" keyword can only occur in a declaration; we
7176 may as well commit at this point. */
7177 cp_parser_commit_to_tentative_parse (parser);
7180 /* storage-class-specifier:
7190 /* Consume the token. */
7191 cp_lexer_consume_token (parser->lexer);
7192 cp_parser_set_storage_class (decl_specs, sc_auto);
7195 /* Consume the token. */
7196 cp_lexer_consume_token (parser->lexer);
7197 cp_parser_set_storage_class (decl_specs, sc_register);
7200 /* Consume the token. */
7201 cp_lexer_consume_token (parser->lexer);
7202 if (decl_specs->specs[(int) ds_thread])
7204 error ("`__thread' before `static'");
7205 decl_specs->specs[(int) ds_thread] = 0;
7207 cp_parser_set_storage_class (decl_specs, sc_static);
7210 /* Consume the token. */
7211 cp_lexer_consume_token (parser->lexer);
7212 if (decl_specs->specs[(int) ds_thread])
7214 error ("`__thread' before `extern'");
7215 decl_specs->specs[(int) ds_thread] = 0;
7217 cp_parser_set_storage_class (decl_specs, sc_extern);
7220 /* Consume the token. */
7221 cp_lexer_consume_token (parser->lexer);
7222 cp_parser_set_storage_class (decl_specs, sc_mutable);
7225 /* Consume the token. */
7226 cp_lexer_consume_token (parser->lexer);
7227 ++decl_specs->specs[(int) ds_thread];
7231 /* We did not yet find a decl-specifier yet. */
7232 found_decl_spec = false;
7236 /* Constructors are a special case. The `S' in `S()' is not a
7237 decl-specifier; it is the beginning of the declarator. */
7240 && constructor_possible_p
7241 && (cp_parser_constructor_declarator_p
7242 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7244 /* If we don't have a DECL_SPEC yet, then we must be looking at
7245 a type-specifier. */
7246 if (!found_decl_spec && !constructor_p)
7248 int decl_spec_declares_class_or_enum;
7249 bool is_cv_qualifier;
7253 = cp_parser_type_specifier (parser, flags,
7255 /*is_declaration=*/true,
7256 &decl_spec_declares_class_or_enum,
7259 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7261 /* If this type-specifier referenced a user-defined type
7262 (a typedef, class-name, etc.), then we can't allow any
7263 more such type-specifiers henceforth.
7267 The longest sequence of decl-specifiers that could
7268 possibly be a type name is taken as the
7269 decl-specifier-seq of a declaration. The sequence shall
7270 be self-consistent as described below.
7274 As a general rule, at most one type-specifier is allowed
7275 in the complete decl-specifier-seq of a declaration. The
7276 only exceptions are the following:
7278 -- const or volatile can be combined with any other
7281 -- signed or unsigned can be combined with char, long,
7289 void g (const int Pc);
7291 Here, Pc is *not* part of the decl-specifier seq; it's
7292 the declarator. Therefore, once we see a type-specifier
7293 (other than a cv-qualifier), we forbid any additional
7294 user-defined types. We *do* still allow things like `int
7295 int' to be considered a decl-specifier-seq, and issue the
7296 error message later. */
7297 if (type_spec && !is_cv_qualifier)
7298 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7299 /* A constructor declarator cannot follow a type-specifier. */
7302 constructor_possible_p = false;
7303 found_decl_spec = true;
7307 /* If we still do not have a DECL_SPEC, then there are no more
7309 if (!found_decl_spec)
7312 decl_specs->any_specifiers_p = true;
7313 /* After we see one decl-specifier, further decl-specifiers are
7315 flags |= CP_PARSER_FLAGS_OPTIONAL;
7318 /* Don't allow a friend specifier with a class definition. */
7319 if (decl_specs->specs[(int) ds_friend] != 0
7320 && (*declares_class_or_enum & 2))
7321 error ("class definition may not be declared a friend");
7324 /* Parse an (optional) storage-class-specifier.
7326 storage-class-specifier:
7335 storage-class-specifier:
7338 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7341 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7343 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7351 /* Consume the token. */
7352 return cp_lexer_consume_token (parser->lexer)->value;
7359 /* Parse an (optional) function-specifier.
7366 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7367 Updates DECL_SPECS, if it is non-NULL. */
7370 cp_parser_function_specifier_opt (cp_parser* parser,
7371 cp_decl_specifier_seq *decl_specs)
7373 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7377 ++decl_specs->specs[(int) ds_inline];
7382 ++decl_specs->specs[(int) ds_virtual];
7387 ++decl_specs->specs[(int) ds_explicit];
7394 /* Consume the token. */
7395 return cp_lexer_consume_token (parser->lexer)->value;
7398 /* Parse a linkage-specification.
7400 linkage-specification:
7401 extern string-literal { declaration-seq [opt] }
7402 extern string-literal declaration */
7405 cp_parser_linkage_specification (cp_parser* parser)
7410 /* Look for the `extern' keyword. */
7411 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7413 /* Peek at the next token. */
7414 token = cp_lexer_peek_token (parser->lexer);
7415 /* If it's not a string-literal, then there's a problem. */
7416 if (!cp_parser_is_string_literal (token))
7418 cp_parser_error (parser, "expected language-name");
7421 /* Consume the token. */
7422 cp_lexer_consume_token (parser->lexer);
7424 /* Transform the literal into an identifier. If the literal is a
7425 wide-character string, or contains embedded NULs, then we can't
7426 handle it as the user wants. */
7427 if (token->type == CPP_WSTRING
7428 || (strlen (TREE_STRING_POINTER (token->value))
7429 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7431 cp_parser_error (parser, "invalid linkage-specification");
7432 /* Assume C++ linkage. */
7433 linkage = get_identifier ("c++");
7435 /* If the string is chained to another string, take the latter,
7436 that's the untranslated string. */
7437 else if (TREE_CHAIN (token->value))
7438 linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value)));
7439 /* If it's a simple string constant, things are easier. */
7441 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7443 /* We're now using the new linkage. */
7444 push_lang_context (linkage);
7446 /* If the next token is a `{', then we're using the first
7448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7450 /* Consume the `{' token. */
7451 cp_lexer_consume_token (parser->lexer);
7452 /* Parse the declarations. */
7453 cp_parser_declaration_seq_opt (parser);
7454 /* Look for the closing `}'. */
7455 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7457 /* Otherwise, there's just one declaration. */
7460 bool saved_in_unbraced_linkage_specification_p;
7462 saved_in_unbraced_linkage_specification_p
7463 = parser->in_unbraced_linkage_specification_p;
7464 parser->in_unbraced_linkage_specification_p = true;
7465 have_extern_spec = true;
7466 cp_parser_declaration (parser);
7467 have_extern_spec = false;
7468 parser->in_unbraced_linkage_specification_p
7469 = saved_in_unbraced_linkage_specification_p;
7472 /* We're done with the linkage-specification. */
7473 pop_lang_context ();
7476 /* Special member functions [gram.special] */
7478 /* Parse a conversion-function-id.
7480 conversion-function-id:
7481 operator conversion-type-id
7483 Returns an IDENTIFIER_NODE representing the operator. */
7486 cp_parser_conversion_function_id (cp_parser* parser)
7490 tree saved_qualifying_scope;
7491 tree saved_object_scope;
7494 /* Look for the `operator' token. */
7495 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7496 return error_mark_node;
7497 /* When we parse the conversion-type-id, the current scope will be
7498 reset. However, we need that information in able to look up the
7499 conversion function later, so we save it here. */
7500 saved_scope = parser->scope;
7501 saved_qualifying_scope = parser->qualifying_scope;
7502 saved_object_scope = parser->object_scope;
7503 /* We must enter the scope of the class so that the names of
7504 entities declared within the class are available in the
7505 conversion-type-id. For example, consider:
7512 S::operator I() { ... }
7514 In order to see that `I' is a type-name in the definition, we
7515 must be in the scope of `S'. */
7517 pop_p = push_scope (saved_scope);
7518 /* Parse the conversion-type-id. */
7519 type = cp_parser_conversion_type_id (parser);
7520 /* Leave the scope of the class, if any. */
7522 pop_scope (saved_scope);
7523 /* Restore the saved scope. */
7524 parser->scope = saved_scope;
7525 parser->qualifying_scope = saved_qualifying_scope;
7526 parser->object_scope = saved_object_scope;
7527 /* If the TYPE is invalid, indicate failure. */
7528 if (type == error_mark_node)
7529 return error_mark_node;
7530 return mangle_conv_op_name_for_type (type);
7533 /* Parse a conversion-type-id:
7536 type-specifier-seq conversion-declarator [opt]
7538 Returns the TYPE specified. */
7541 cp_parser_conversion_type_id (cp_parser* parser)
7544 cp_decl_specifier_seq type_specifiers;
7545 cp_declarator *declarator;
7547 /* Parse the attributes. */
7548 attributes = cp_parser_attributes_opt (parser);
7549 /* Parse the type-specifiers. */
7550 cp_parser_type_specifier_seq (parser, &type_specifiers);
7551 /* If that didn't work, stop. */
7552 if (type_specifiers.type == error_mark_node)
7553 return error_mark_node;
7554 /* Parse the conversion-declarator. */
7555 declarator = cp_parser_conversion_declarator_opt (parser);
7557 return grokdeclarator (declarator, &type_specifiers, TYPENAME,
7558 /*initialized=*/0, &attributes);
7561 /* Parse an (optional) conversion-declarator.
7563 conversion-declarator:
7564 ptr-operator conversion-declarator [opt]
7568 static cp_declarator *
7569 cp_parser_conversion_declarator_opt (cp_parser* parser)
7571 enum tree_code code;
7573 tree cv_qualifier_seq;
7575 /* We don't know if there's a ptr-operator next, or not. */
7576 cp_parser_parse_tentatively (parser);
7577 /* Try the ptr-operator. */
7578 code = cp_parser_ptr_operator (parser, &class_type,
7580 /* If it worked, look for more conversion-declarators. */
7581 if (cp_parser_parse_definitely (parser))
7583 cp_declarator *declarator;
7585 /* Parse another optional declarator. */
7586 declarator = cp_parser_conversion_declarator_opt (parser);
7588 /* Create the representation of the declarator. */
7590 declarator = make_ptrmem_declarator (cv_qualifier_seq,
7593 else if (code == INDIRECT_REF)
7594 declarator = make_pointer_declarator (cv_qualifier_seq,
7597 declarator = make_reference_declarator (cv_qualifier_seq,
7606 /* Parse an (optional) ctor-initializer.
7609 : mem-initializer-list
7611 Returns TRUE iff the ctor-initializer was actually present. */
7614 cp_parser_ctor_initializer_opt (cp_parser* parser)
7616 /* If the next token is not a `:', then there is no
7617 ctor-initializer. */
7618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7620 /* Do default initialization of any bases and members. */
7621 if (DECL_CONSTRUCTOR_P (current_function_decl))
7622 finish_mem_initializers (NULL_TREE);
7627 /* Consume the `:' token. */
7628 cp_lexer_consume_token (parser->lexer);
7629 /* And the mem-initializer-list. */
7630 cp_parser_mem_initializer_list (parser);
7635 /* Parse a mem-initializer-list.
7637 mem-initializer-list:
7639 mem-initializer , mem-initializer-list */
7642 cp_parser_mem_initializer_list (cp_parser* parser)
7644 tree mem_initializer_list = NULL_TREE;
7646 /* Let the semantic analysis code know that we are starting the
7647 mem-initializer-list. */
7648 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7649 error ("only constructors take base initializers");
7651 /* Loop through the list. */
7654 tree mem_initializer;
7656 /* Parse the mem-initializer. */
7657 mem_initializer = cp_parser_mem_initializer (parser);
7658 /* Add it to the list, unless it was erroneous. */
7659 if (mem_initializer)
7661 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7662 mem_initializer_list = mem_initializer;
7664 /* If the next token is not a `,', we're done. */
7665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7667 /* Consume the `,' token. */
7668 cp_lexer_consume_token (parser->lexer);
7671 /* Perform semantic analysis. */
7672 if (DECL_CONSTRUCTOR_P (current_function_decl))
7673 finish_mem_initializers (mem_initializer_list);
7676 /* Parse a mem-initializer.
7679 mem-initializer-id ( expression-list [opt] )
7684 ( expression-list [opt] )
7686 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7687 class) or FIELD_DECL (for a non-static data member) to initialize;
7688 the TREE_VALUE is the expression-list. */
7691 cp_parser_mem_initializer (cp_parser* parser)
7693 tree mem_initializer_id;
7694 tree expression_list;
7697 /* Find out what is being initialized. */
7698 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7700 pedwarn ("anachronistic old-style base class initializer");
7701 mem_initializer_id = NULL_TREE;
7704 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7705 member = expand_member_init (mem_initializer_id);
7706 if (member && !DECL_P (member))
7707 in_base_initializer = 1;
7710 = cp_parser_parenthesized_expression_list (parser, false,
7711 /*non_constant_p=*/NULL);
7712 if (!expression_list)
7713 expression_list = void_type_node;
7715 in_base_initializer = 0;
7717 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7720 /* Parse a mem-initializer-id.
7723 :: [opt] nested-name-specifier [opt] class-name
7726 Returns a TYPE indicating the class to be initializer for the first
7727 production. Returns an IDENTIFIER_NODE indicating the data member
7728 to be initialized for the second production. */
7731 cp_parser_mem_initializer_id (cp_parser* parser)
7733 bool global_scope_p;
7734 bool nested_name_specifier_p;
7735 bool template_p = false;
7738 /* `typename' is not allowed in this context ([temp.res]). */
7739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7741 error ("keyword `typename' not allowed in this context (a qualified "
7742 "member initializer is implicitly a type)");
7743 cp_lexer_consume_token (parser->lexer);
7745 /* Look for the optional `::' operator. */
7747 = (cp_parser_global_scope_opt (parser,
7748 /*current_scope_valid_p=*/false)
7750 /* Look for the optional nested-name-specifier. The simplest way to
7755 The keyword `typename' is not permitted in a base-specifier or
7756 mem-initializer; in these contexts a qualified name that
7757 depends on a template-parameter is implicitly assumed to be a
7760 is to assume that we have seen the `typename' keyword at this
7762 nested_name_specifier_p
7763 = (cp_parser_nested_name_specifier_opt (parser,
7764 /*typename_keyword_p=*/true,
7765 /*check_dependency_p=*/true,
7767 /*is_declaration=*/true)
7769 if (nested_name_specifier_p)
7770 template_p = cp_parser_optional_template_keyword (parser);
7771 /* If there is a `::' operator or a nested-name-specifier, then we
7772 are definitely looking for a class-name. */
7773 if (global_scope_p || nested_name_specifier_p)
7774 return cp_parser_class_name (parser,
7775 /*typename_keyword_p=*/true,
7776 /*template_keyword_p=*/template_p,
7778 /*check_dependency_p=*/true,
7779 /*class_head_p=*/false,
7780 /*is_declaration=*/true);
7781 /* Otherwise, we could also be looking for an ordinary identifier. */
7782 cp_parser_parse_tentatively (parser);
7783 /* Try a class-name. */
7784 id = cp_parser_class_name (parser,
7785 /*typename_keyword_p=*/true,
7786 /*template_keyword_p=*/false,
7788 /*check_dependency_p=*/true,
7789 /*class_head_p=*/false,
7790 /*is_declaration=*/true);
7791 /* If we found one, we're done. */
7792 if (cp_parser_parse_definitely (parser))
7794 /* Otherwise, look for an ordinary identifier. */
7795 return cp_parser_identifier (parser);
7798 /* Overloading [gram.over] */
7800 /* Parse an operator-function-id.
7802 operator-function-id:
7805 Returns an IDENTIFIER_NODE for the operator which is a
7806 human-readable spelling of the identifier, e.g., `operator +'. */
7809 cp_parser_operator_function_id (cp_parser* parser)
7811 /* Look for the `operator' keyword. */
7812 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7813 return error_mark_node;
7814 /* And then the name of the operator itself. */
7815 return cp_parser_operator (parser);
7818 /* Parse an operator.
7821 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7822 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7823 || ++ -- , ->* -> () []
7830 Returns an IDENTIFIER_NODE for the operator which is a
7831 human-readable spelling of the identifier, e.g., `operator +'. */
7834 cp_parser_operator (cp_parser* parser)
7836 tree id = NULL_TREE;
7839 /* Peek at the next token. */
7840 token = cp_lexer_peek_token (parser->lexer);
7841 /* Figure out which operator we have. */
7842 switch (token->type)
7848 /* The keyword should be either `new' or `delete'. */
7849 if (token->keyword == RID_NEW)
7851 else if (token->keyword == RID_DELETE)
7856 /* Consume the `new' or `delete' token. */
7857 cp_lexer_consume_token (parser->lexer);
7859 /* Peek at the next token. */
7860 token = cp_lexer_peek_token (parser->lexer);
7861 /* If it's a `[' token then this is the array variant of the
7863 if (token->type == CPP_OPEN_SQUARE)
7865 /* Consume the `[' token. */
7866 cp_lexer_consume_token (parser->lexer);
7867 /* Look for the `]' token. */
7868 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7869 id = ansi_opname (op == NEW_EXPR
7870 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7872 /* Otherwise, we have the non-array variant. */
7874 id = ansi_opname (op);
7880 id = ansi_opname (PLUS_EXPR);
7884 id = ansi_opname (MINUS_EXPR);
7888 id = ansi_opname (MULT_EXPR);
7892 id = ansi_opname (TRUNC_DIV_EXPR);
7896 id = ansi_opname (TRUNC_MOD_EXPR);
7900 id = ansi_opname (BIT_XOR_EXPR);
7904 id = ansi_opname (BIT_AND_EXPR);
7908 id = ansi_opname (BIT_IOR_EXPR);
7912 id = ansi_opname (BIT_NOT_EXPR);
7916 id = ansi_opname (TRUTH_NOT_EXPR);
7920 id = ansi_assopname (NOP_EXPR);
7924 id = ansi_opname (LT_EXPR);
7928 id = ansi_opname (GT_EXPR);
7932 id = ansi_assopname (PLUS_EXPR);
7936 id = ansi_assopname (MINUS_EXPR);
7940 id = ansi_assopname (MULT_EXPR);
7944 id = ansi_assopname (TRUNC_DIV_EXPR);
7948 id = ansi_assopname (TRUNC_MOD_EXPR);
7952 id = ansi_assopname (BIT_XOR_EXPR);
7956 id = ansi_assopname (BIT_AND_EXPR);
7960 id = ansi_assopname (BIT_IOR_EXPR);
7964 id = ansi_opname (LSHIFT_EXPR);
7968 id = ansi_opname (RSHIFT_EXPR);
7972 id = ansi_assopname (LSHIFT_EXPR);
7976 id = ansi_assopname (RSHIFT_EXPR);
7980 id = ansi_opname (EQ_EXPR);
7984 id = ansi_opname (NE_EXPR);
7988 id = ansi_opname (LE_EXPR);
7991 case CPP_GREATER_EQ:
7992 id = ansi_opname (GE_EXPR);
7996 id = ansi_opname (TRUTH_ANDIF_EXPR);
8000 id = ansi_opname (TRUTH_ORIF_EXPR);
8004 id = ansi_opname (POSTINCREMENT_EXPR);
8007 case CPP_MINUS_MINUS:
8008 id = ansi_opname (PREDECREMENT_EXPR);
8012 id = ansi_opname (COMPOUND_EXPR);
8015 case CPP_DEREF_STAR:
8016 id = ansi_opname (MEMBER_REF);
8020 id = ansi_opname (COMPONENT_REF);
8023 case CPP_OPEN_PAREN:
8024 /* Consume the `('. */
8025 cp_lexer_consume_token (parser->lexer);
8026 /* Look for the matching `)'. */
8027 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8028 return ansi_opname (CALL_EXPR);
8030 case CPP_OPEN_SQUARE:
8031 /* Consume the `['. */
8032 cp_lexer_consume_token (parser->lexer);
8033 /* Look for the matching `]'. */
8034 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8035 return ansi_opname (ARRAY_REF);
8039 id = ansi_opname (MIN_EXPR);
8043 id = ansi_opname (MAX_EXPR);
8047 id = ansi_assopname (MIN_EXPR);
8051 id = ansi_assopname (MAX_EXPR);
8055 /* Anything else is an error. */
8059 /* If we have selected an identifier, we need to consume the
8062 cp_lexer_consume_token (parser->lexer);
8063 /* Otherwise, no valid operator name was present. */
8066 cp_parser_error (parser, "expected operator");
8067 id = error_mark_node;
8073 /* Parse a template-declaration.
8075 template-declaration:
8076 export [opt] template < template-parameter-list > declaration
8078 If MEMBER_P is TRUE, this template-declaration occurs within a
8081 The grammar rule given by the standard isn't correct. What
8084 template-declaration:
8085 export [opt] template-parameter-list-seq
8086 decl-specifier-seq [opt] init-declarator [opt] ;
8087 export [opt] template-parameter-list-seq
8090 template-parameter-list-seq:
8091 template-parameter-list-seq [opt]
8092 template < template-parameter-list > */
8095 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8097 /* Check for `export'. */
8098 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8100 /* Consume the `export' token. */
8101 cp_lexer_consume_token (parser->lexer);
8102 /* Warn that we do not support `export'. */
8103 warning ("keyword `export' not implemented, and will be ignored");
8106 cp_parser_template_declaration_after_export (parser, member_p);
8109 /* Parse a template-parameter-list.
8111 template-parameter-list:
8113 template-parameter-list , template-parameter
8115 Returns a TREE_LIST. Each node represents a template parameter.
8116 The nodes are connected via their TREE_CHAINs. */
8119 cp_parser_template_parameter_list (cp_parser* parser)
8121 tree parameter_list = NULL_TREE;
8129 /* Parse the template-parameter. */
8130 parameter = cp_parser_template_parameter (parser, &is_non_type);
8131 /* Add it to the list. */
8132 parameter_list = process_template_parm (parameter_list,
8135 /* Peek at the next token. */
8136 token = cp_lexer_peek_token (parser->lexer);
8137 /* If it's not a `,', we're done. */
8138 if (token->type != CPP_COMMA)
8140 /* Otherwise, consume the `,' token. */
8141 cp_lexer_consume_token (parser->lexer);
8144 return parameter_list;
8147 /* Parse a template-parameter.
8151 parameter-declaration
8153 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8154 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8155 true iff this parameter is a non-type parameter. */
8158 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8161 cp_parameter_declarator *parameter_declarator;
8163 /* Assume it is a type parameter or a template parameter. */
8164 *is_non_type = false;
8165 /* Peek at the next token. */
8166 token = cp_lexer_peek_token (parser->lexer);
8167 /* If it is `class' or `template', we have a type-parameter. */
8168 if (token->keyword == RID_TEMPLATE)
8169 return cp_parser_type_parameter (parser);
8170 /* If it is `class' or `typename' we do not know yet whether it is a
8171 type parameter or a non-type parameter. Consider:
8173 template <typename T, typename T::X X> ...
8177 template <class C, class D*> ...
8179 Here, the first parameter is a type parameter, and the second is
8180 a non-type parameter. We can tell by looking at the token after
8181 the identifier -- if it is a `,', `=', or `>' then we have a type
8183 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8185 /* Peek at the token after `class' or `typename'. */
8186 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8187 /* If it's an identifier, skip it. */
8188 if (token->type == CPP_NAME)
8189 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8190 /* Now, see if the token looks like the end of a template
8192 if (token->type == CPP_COMMA
8193 || token->type == CPP_EQ
8194 || token->type == CPP_GREATER)
8195 return cp_parser_type_parameter (parser);
8198 /* Otherwise, it is a non-type parameter.
8202 When parsing a default template-argument for a non-type
8203 template-parameter, the first non-nested `>' is taken as the end
8204 of the template parameter-list rather than a greater-than
8206 *is_non_type = true;
8207 parameter_declarator
8208 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8209 /*parenthesized_p=*/NULL);
8210 return (build_tree_list
8211 (parameter_declarator->default_argument,
8212 grokdeclarator (parameter_declarator->declarator,
8213 ¶meter_declarator->decl_specifiers,
8214 PARM, /*initialized=*/0,
8215 /*attrlist=*/NULL)));
8218 /* Parse a type-parameter.
8221 class identifier [opt]
8222 class identifier [opt] = type-id
8223 typename identifier [opt]
8224 typename identifier [opt] = type-id
8225 template < template-parameter-list > class identifier [opt]
8226 template < template-parameter-list > class identifier [opt]
8229 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8230 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8231 the declaration of the parameter. */
8234 cp_parser_type_parameter (cp_parser* parser)
8239 /* Look for a keyword to tell us what kind of parameter this is. */
8240 token = cp_parser_require (parser, CPP_KEYWORD,
8241 "`class', `typename', or `template'");
8243 return error_mark_node;
8245 switch (token->keyword)
8251 tree default_argument;
8253 /* If the next token is an identifier, then it names the
8255 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8256 identifier = cp_parser_identifier (parser);
8258 identifier = NULL_TREE;
8260 /* Create the parameter. */
8261 parameter = finish_template_type_parm (class_type_node, identifier);
8263 /* If the next token is an `=', we have a default argument. */
8264 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8266 /* Consume the `=' token. */
8267 cp_lexer_consume_token (parser->lexer);
8268 /* Parse the default-argument. */
8269 default_argument = cp_parser_type_id (parser);
8272 default_argument = NULL_TREE;
8274 /* Create the combined representation of the parameter and the
8275 default argument. */
8276 parameter = build_tree_list (default_argument, parameter);
8282 tree parameter_list;
8284 tree default_argument;
8286 /* Look for the `<'. */
8287 cp_parser_require (parser, CPP_LESS, "`<'");
8288 /* Parse the template-parameter-list. */
8289 begin_template_parm_list ();
8291 = cp_parser_template_parameter_list (parser);
8292 parameter_list = end_template_parm_list (parameter_list);
8293 /* Look for the `>'. */
8294 cp_parser_require (parser, CPP_GREATER, "`>'");
8295 /* Look for the `class' keyword. */
8296 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8297 /* If the next token is an `=', then there is a
8298 default-argument. If the next token is a `>', we are at
8299 the end of the parameter-list. If the next token is a `,',
8300 then we are at the end of this parameter. */
8301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8302 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8303 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8304 identifier = cp_parser_identifier (parser);
8306 identifier = NULL_TREE;
8307 /* Create the template parameter. */
8308 parameter = finish_template_template_parm (class_type_node,
8311 /* If the next token is an `=', then there is a
8312 default-argument. */
8313 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8317 /* Consume the `='. */
8318 cp_lexer_consume_token (parser->lexer);
8319 /* Parse the id-expression. */
8321 = cp_parser_id_expression (parser,
8322 /*template_keyword_p=*/false,
8323 /*check_dependency_p=*/true,
8324 /*template_p=*/&is_template,
8325 /*declarator_p=*/false);
8326 if (TREE_CODE (default_argument) == TYPE_DECL)
8327 /* If the id-expression was a template-id that refers to
8328 a template-class, we already have the declaration here,
8329 so no further lookup is needed. */
8332 /* Look up the name. */
8334 = cp_parser_lookup_name (parser, default_argument,
8336 /*is_template=*/is_template,
8337 /*is_namespace=*/false,
8338 /*check_dependency=*/true);
8339 /* See if the default argument is valid. */
8341 = check_template_template_default_arg (default_argument);
8344 default_argument = NULL_TREE;
8346 /* Create the combined representation of the parameter and the
8347 default argument. */
8348 parameter = build_tree_list (default_argument, parameter);
8353 /* Anything else is an error. */
8354 cp_parser_error (parser,
8355 "expected `class', `typename', or `template'");
8356 parameter = error_mark_node;
8362 /* Parse a template-id.
8365 template-name < template-argument-list [opt] >
8367 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8368 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8369 returned. Otherwise, if the template-name names a function, or set
8370 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8371 names a class, returns a TYPE_DECL for the specialization.
8373 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8374 uninstantiated templates. */
8377 cp_parser_template_id (cp_parser *parser,
8378 bool template_keyword_p,
8379 bool check_dependency_p,
8380 bool is_declaration)
8385 ptrdiff_t start_of_id;
8386 tree access_check = NULL_TREE;
8387 cp_token *next_token, *next_token_2;
8390 /* If the next token corresponds to a template-id, there is no need
8392 next_token = cp_lexer_peek_token (parser->lexer);
8393 if (next_token->type == CPP_TEMPLATE_ID)
8398 /* Get the stored value. */
8399 value = cp_lexer_consume_token (parser->lexer)->value;
8400 /* Perform any access checks that were deferred. */
8401 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8402 perform_or_defer_access_check (TREE_PURPOSE (check),
8403 TREE_VALUE (check));
8404 /* Return the stored value. */
8405 return TREE_VALUE (value);
8408 /* Avoid performing name lookup if there is no possibility of
8409 finding a template-id. */
8410 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8411 || (next_token->type == CPP_NAME
8412 && !cp_parser_nth_token_starts_template_argument_list_p
8415 cp_parser_error (parser, "expected template-id");
8416 return error_mark_node;
8419 /* Remember where the template-id starts. */
8420 if (cp_parser_parsing_tentatively (parser)
8421 && !cp_parser_committed_to_tentative_parse (parser))
8423 next_token = cp_lexer_peek_token (parser->lexer);
8424 start_of_id = cp_lexer_token_difference (parser->lexer,
8425 parser->lexer->first_token,
8431 push_deferring_access_checks (dk_deferred);
8433 /* Parse the template-name. */
8434 is_identifier = false;
8435 template = cp_parser_template_name (parser, template_keyword_p,
8439 if (template == error_mark_node || is_identifier)
8441 pop_deferring_access_checks ();
8445 /* If we find the sequence `[:' after a template-name, it's probably
8446 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8447 parse correctly the argument list. */
8448 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8449 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8450 if (next_token->type == CPP_OPEN_SQUARE
8451 && next_token->flags & DIGRAPH
8452 && next_token_2->type == CPP_COLON
8453 && !(next_token_2->flags & PREV_WHITE))
8455 cp_parser_parse_tentatively (parser);
8456 /* Change `:' into `::'. */
8457 next_token_2->type = CPP_SCOPE;
8458 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8460 cp_lexer_consume_token (parser->lexer);
8461 /* Parse the arguments. */
8462 arguments = cp_parser_enclosed_template_argument_list (parser);
8463 if (!cp_parser_parse_definitely (parser))
8465 /* If we couldn't parse an argument list, then we revert our changes
8466 and return simply an error. Maybe this is not a template-id
8468 next_token_2->type = CPP_COLON;
8469 cp_parser_error (parser, "expected `<'");
8470 pop_deferring_access_checks ();
8471 return error_mark_node;
8473 /* Otherwise, emit an error about the invalid digraph, but continue
8474 parsing because we got our argument list. */
8475 pedwarn ("`<::' cannot begin a template-argument list");
8476 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8477 "between `<' and `::'");
8478 if (!flag_permissive)
8483 inform ("(if you use `-fpermissive' G++ will accept your code)");
8490 /* Look for the `<' that starts the template-argument-list. */
8491 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8493 pop_deferring_access_checks ();
8494 return error_mark_node;
8496 /* Parse the arguments. */
8497 arguments = cp_parser_enclosed_template_argument_list (parser);
8500 /* Build a representation of the specialization. */
8501 if (TREE_CODE (template) == IDENTIFIER_NODE)
8502 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8503 else if (DECL_CLASS_TEMPLATE_P (template)
8504 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8506 = finish_template_type (template, arguments,
8507 cp_lexer_next_token_is (parser->lexer,
8511 /* If it's not a class-template or a template-template, it should be
8512 a function-template. */
8513 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8514 || TREE_CODE (template) == OVERLOAD
8515 || BASELINK_P (template)),
8518 template_id = lookup_template_function (template, arguments);
8521 /* Retrieve any deferred checks. Do not pop this access checks yet
8522 so the memory will not be reclaimed during token replacing below. */
8523 access_check = get_deferred_access_checks ();
8525 /* If parsing tentatively, replace the sequence of tokens that makes
8526 up the template-id with a CPP_TEMPLATE_ID token. That way,
8527 should we re-parse the token stream, we will not have to repeat
8528 the effort required to do the parse, nor will we issue duplicate
8529 error messages about problems during instantiation of the
8531 if (start_of_id >= 0)
8535 /* Find the token that corresponds to the start of the
8537 token = cp_lexer_advance_token (parser->lexer,
8538 parser->lexer->first_token,
8541 /* Reset the contents of the START_OF_ID token. */
8542 token->type = CPP_TEMPLATE_ID;
8543 token->value = build_tree_list (access_check, template_id);
8544 token->keyword = RID_MAX;
8545 /* Purge all subsequent tokens. */
8546 cp_lexer_purge_tokens_after (parser->lexer, token);
8549 pop_deferring_access_checks ();
8553 /* Parse a template-name.
8558 The standard should actually say:
8562 operator-function-id
8564 A defect report has been filed about this issue.
8566 A conversion-function-id cannot be a template name because they cannot
8567 be part of a template-id. In fact, looking at this code:
8571 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8572 It is impossible to call a templated conversion-function-id with an
8573 explicit argument list, since the only allowed template parameter is
8574 the type to which it is converting.
8576 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8577 `template' keyword, in a construction like:
8581 In that case `f' is taken to be a template-name, even though there
8582 is no way of knowing for sure.
8584 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8585 name refers to a set of overloaded functions, at least one of which
8586 is a template, or an IDENTIFIER_NODE with the name of the template,
8587 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8588 names are looked up inside uninstantiated templates. */
8591 cp_parser_template_name (cp_parser* parser,
8592 bool template_keyword_p,
8593 bool check_dependency_p,
8594 bool is_declaration,
8595 bool *is_identifier)
8601 /* If the next token is `operator', then we have either an
8602 operator-function-id or a conversion-function-id. */
8603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8605 /* We don't know whether we're looking at an
8606 operator-function-id or a conversion-function-id. */
8607 cp_parser_parse_tentatively (parser);
8608 /* Try an operator-function-id. */
8609 identifier = cp_parser_operator_function_id (parser);
8610 /* If that didn't work, try a conversion-function-id. */
8611 if (!cp_parser_parse_definitely (parser))
8613 cp_parser_error (parser, "expected template-name");
8614 return error_mark_node;
8617 /* Look for the identifier. */
8619 identifier = cp_parser_identifier (parser);
8621 /* If we didn't find an identifier, we don't have a template-id. */
8622 if (identifier == error_mark_node)
8623 return error_mark_node;
8625 /* If the name immediately followed the `template' keyword, then it
8626 is a template-name. However, if the next token is not `<', then
8627 we do not treat it as a template-name, since it is not being used
8628 as part of a template-id. This enables us to handle constructs
8631 template <typename T> struct S { S(); };
8632 template <typename T> S<T>::S();
8634 correctly. We would treat `S' as a template -- if it were `S<T>'
8635 -- but we do not if there is no `<'. */
8637 if (processing_template_decl
8638 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8640 /* In a declaration, in a dependent context, we pretend that the
8641 "template" keyword was present in order to improve error
8642 recovery. For example, given:
8644 template <typename T> void f(T::X<int>);
8646 we want to treat "X<int>" as a template-id. */
8648 && !template_keyword_p
8649 && parser->scope && TYPE_P (parser->scope)
8650 && dependent_type_p (parser->scope)
8651 /* Do not do this for dtors (or ctors), since they never
8652 need the template keyword before their name. */
8653 && !constructor_name_p (identifier, parser->scope))
8657 /* Explain what went wrong. */
8658 error ("non-template `%D' used as template", identifier);
8659 inform ("use `%T::template %D' to indicate that it is a template",
8660 parser->scope, identifier);
8661 /* If parsing tentatively, find the location of the "<"
8663 if (cp_parser_parsing_tentatively (parser)
8664 && !cp_parser_committed_to_tentative_parse (parser))
8666 cp_parser_simulate_error (parser);
8667 token = cp_lexer_peek_token (parser->lexer);
8668 token = cp_lexer_prev_token (parser->lexer, token);
8669 start = cp_lexer_token_difference (parser->lexer,
8670 parser->lexer->first_token,
8675 /* Parse the template arguments so that we can issue error
8676 messages about them. */
8677 cp_lexer_consume_token (parser->lexer);
8678 cp_parser_enclosed_template_argument_list (parser);
8679 /* Skip tokens until we find a good place from which to
8680 continue parsing. */
8681 cp_parser_skip_to_closing_parenthesis (parser,
8682 /*recovering=*/true,
8684 /*consume_paren=*/false);
8685 /* If parsing tentatively, permanently remove the
8686 template argument list. That will prevent duplicate
8687 error messages from being issued about the missing
8688 "template" keyword. */
8691 token = cp_lexer_advance_token (parser->lexer,
8692 parser->lexer->first_token,
8694 cp_lexer_purge_tokens_after (parser->lexer, token);
8697 *is_identifier = true;
8701 /* If the "template" keyword is present, then there is generally
8702 no point in doing name-lookup, so we just return IDENTIFIER.
8703 But, if the qualifying scope is non-dependent then we can
8704 (and must) do name-lookup normally. */
8705 if (template_keyword_p
8707 || (TYPE_P (parser->scope)
8708 && dependent_type_p (parser->scope))))
8712 /* Look up the name. */
8713 decl = cp_parser_lookup_name (parser, identifier,
8715 /*is_template=*/false,
8716 /*is_namespace=*/false,
8717 check_dependency_p);
8718 decl = maybe_get_template_decl_from_type_decl (decl);
8720 /* If DECL is a template, then the name was a template-name. */
8721 if (TREE_CODE (decl) == TEMPLATE_DECL)
8725 /* The standard does not explicitly indicate whether a name that
8726 names a set of overloaded declarations, some of which are
8727 templates, is a template-name. However, such a name should
8728 be a template-name; otherwise, there is no way to form a
8729 template-id for the overloaded templates. */
8730 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8731 if (TREE_CODE (fns) == OVERLOAD)
8735 for (fn = fns; fn; fn = OVL_NEXT (fn))
8736 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8741 /* Otherwise, the name does not name a template. */
8742 cp_parser_error (parser, "expected template-name");
8743 return error_mark_node;
8747 /* If DECL is dependent, and refers to a function, then just return
8748 its name; we will look it up again during template instantiation. */
8749 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8751 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8752 if (TYPE_P (scope) && dependent_type_p (scope))
8759 /* Parse a template-argument-list.
8761 template-argument-list:
8763 template-argument-list , template-argument
8765 Returns a TREE_VEC containing the arguments. */
8768 cp_parser_template_argument_list (cp_parser* parser)
8770 tree fixed_args[10];
8771 unsigned n_args = 0;
8772 unsigned alloced = 10;
8773 tree *arg_ary = fixed_args;
8775 bool saved_in_template_argument_list_p;
8777 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8778 parser->in_template_argument_list_p = true;
8784 /* Consume the comma. */
8785 cp_lexer_consume_token (parser->lexer);
8787 /* Parse the template-argument. */
8788 argument = cp_parser_template_argument (parser);
8789 if (n_args == alloced)
8793 if (arg_ary == fixed_args)
8795 arg_ary = xmalloc (sizeof (tree) * alloced);
8796 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8799 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8801 arg_ary[n_args++] = argument;
8803 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8805 vec = make_tree_vec (n_args);
8808 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8810 if (arg_ary != fixed_args)
8812 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8816 /* Parse a template-argument.
8819 assignment-expression
8823 The representation is that of an assignment-expression, type-id, or
8824 id-expression -- except that the qualified id-expression is
8825 evaluated, so that the value returned is either a DECL or an
8828 Although the standard says "assignment-expression", it forbids
8829 throw-expressions or assignments in the template argument.
8830 Therefore, we use "conditional-expression" instead. */
8833 cp_parser_template_argument (cp_parser* parser)
8838 bool maybe_type_id = false;
8841 tree qualifying_class;
8843 /* There's really no way to know what we're looking at, so we just
8844 try each alternative in order.
8848 In a template-argument, an ambiguity between a type-id and an
8849 expression is resolved to a type-id, regardless of the form of
8850 the corresponding template-parameter.
8852 Therefore, we try a type-id first. */
8853 cp_parser_parse_tentatively (parser);
8854 argument = cp_parser_type_id (parser);
8855 /* If there was no error parsing the type-id but the next token is a '>>',
8856 we probably found a typo for '> >'. But there are type-id which are
8857 also valid expressions. For instance:
8859 struct X { int operator >> (int); };
8860 template <int V> struct Foo {};
8863 Here 'X()' is a valid type-id of a function type, but the user just
8864 wanted to write the expression "X() >> 5". Thus, we remember that we
8865 found a valid type-id, but we still try to parse the argument as an
8866 expression to see what happens. */
8867 if (!cp_parser_error_occurred (parser)
8868 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8870 maybe_type_id = true;
8871 cp_parser_abort_tentative_parse (parser);
8875 /* If the next token isn't a `,' or a `>', then this argument wasn't
8876 really finished. This means that the argument is not a valid
8878 if (!cp_parser_next_token_ends_template_argument_p (parser))
8879 cp_parser_error (parser, "expected template-argument");
8880 /* If that worked, we're done. */
8881 if (cp_parser_parse_definitely (parser))
8884 /* We're still not sure what the argument will be. */
8885 cp_parser_parse_tentatively (parser);
8886 /* Try a template. */
8887 argument = cp_parser_id_expression (parser,
8888 /*template_keyword_p=*/false,
8889 /*check_dependency_p=*/true,
8891 /*declarator_p=*/false);
8892 /* If the next token isn't a `,' or a `>', then this argument wasn't
8894 if (!cp_parser_next_token_ends_template_argument_p (parser))
8895 cp_parser_error (parser, "expected template-argument");
8896 if (!cp_parser_error_occurred (parser))
8898 /* Figure out what is being referred to. If the id-expression
8899 was for a class template specialization, then we will have a
8900 TYPE_DECL at this point. There is no need to do name lookup
8901 at this point in that case. */
8902 if (TREE_CODE (argument) != TYPE_DECL)
8903 argument = cp_parser_lookup_name (parser, argument,
8905 /*is_template=*/template_p,
8906 /*is_namespace=*/false,
8907 /*check_dependency=*/true);
8908 if (TREE_CODE (argument) != TEMPLATE_DECL
8909 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8910 cp_parser_error (parser, "expected template-name");
8912 if (cp_parser_parse_definitely (parser))
8914 /* It must be a non-type argument. There permitted cases are given
8915 in [temp.arg.nontype]:
8917 -- an integral constant-expression of integral or enumeration
8920 -- the name of a non-type template-parameter; or
8922 -- the name of an object or function with external linkage...
8924 -- the address of an object or function with external linkage...
8926 -- a pointer to member... */
8927 /* Look for a non-type template parameter. */
8928 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8930 cp_parser_parse_tentatively (parser);
8931 argument = cp_parser_primary_expression (parser,
8934 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8935 || !cp_parser_next_token_ends_template_argument_p (parser))
8936 cp_parser_simulate_error (parser);
8937 if (cp_parser_parse_definitely (parser))
8940 /* If the next token is "&", the argument must be the address of an
8941 object or function with external linkage. */
8942 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8944 cp_lexer_consume_token (parser->lexer);
8945 /* See if we might have an id-expression. */
8946 token = cp_lexer_peek_token (parser->lexer);
8947 if (token->type == CPP_NAME
8948 || token->keyword == RID_OPERATOR
8949 || token->type == CPP_SCOPE
8950 || token->type == CPP_TEMPLATE_ID
8951 || token->type == CPP_NESTED_NAME_SPECIFIER)
8953 cp_parser_parse_tentatively (parser);
8954 argument = cp_parser_primary_expression (parser,
8957 if (cp_parser_error_occurred (parser)
8958 || !cp_parser_next_token_ends_template_argument_p (parser))
8959 cp_parser_abort_tentative_parse (parser);
8962 if (qualifying_class)
8963 argument = finish_qualified_id_expr (qualifying_class,
8967 if (TREE_CODE (argument) == VAR_DECL)
8969 /* A variable without external linkage might still be a
8970 valid constant-expression, so no error is issued here
8971 if the external-linkage check fails. */
8972 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8973 cp_parser_simulate_error (parser);
8975 else if (is_overloaded_fn (argument))
8976 /* All overloaded functions are allowed; if the external
8977 linkage test does not pass, an error will be issued
8981 && (TREE_CODE (argument) == OFFSET_REF
8982 || TREE_CODE (argument) == SCOPE_REF))
8983 /* A pointer-to-member. */
8986 cp_parser_simulate_error (parser);
8988 if (cp_parser_parse_definitely (parser))
8991 argument = build_x_unary_op (ADDR_EXPR, argument);
8996 /* If the argument started with "&", there are no other valid
8997 alternatives at this point. */
9000 cp_parser_error (parser, "invalid non-type template argument");
9001 return error_mark_node;
9003 /* If the argument wasn't successfully parsed as a type-id followed
9004 by '>>', the argument can only be a constant expression now.
9005 Otherwise, we try parsing the constant-expression tentatively,
9006 because the argument could really be a type-id. */
9008 cp_parser_parse_tentatively (parser);
9009 argument = cp_parser_constant_expression (parser,
9010 /*allow_non_constant_p=*/false,
9011 /*non_constant_p=*/NULL);
9012 argument = fold_non_dependent_expr (argument);
9015 if (!cp_parser_next_token_ends_template_argument_p (parser))
9016 cp_parser_error (parser, "expected template-argument");
9017 if (cp_parser_parse_definitely (parser))
9019 /* We did our best to parse the argument as a non type-id, but that
9020 was the only alternative that matched (albeit with a '>' after
9021 it). We can assume it's just a typo from the user, and a
9022 diagnostic will then be issued. */
9023 return cp_parser_type_id (parser);
9026 /* Parse an explicit-instantiation.
9028 explicit-instantiation:
9029 template declaration
9031 Although the standard says `declaration', what it really means is:
9033 explicit-instantiation:
9034 template decl-specifier-seq [opt] declarator [opt] ;
9036 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9037 supposed to be allowed. A defect report has been filed about this
9042 explicit-instantiation:
9043 storage-class-specifier template
9044 decl-specifier-seq [opt] declarator [opt] ;
9045 function-specifier template
9046 decl-specifier-seq [opt] declarator [opt] ; */
9049 cp_parser_explicit_instantiation (cp_parser* parser)
9051 int declares_class_or_enum;
9052 cp_decl_specifier_seq decl_specifiers;
9053 tree extension_specifier = NULL_TREE;
9055 /* Look for an (optional) storage-class-specifier or
9056 function-specifier. */
9057 if (cp_parser_allow_gnu_extensions_p (parser))
9060 = cp_parser_storage_class_specifier_opt (parser);
9061 if (!extension_specifier)
9063 = cp_parser_function_specifier_opt (parser,
9064 /*decl_specs=*/NULL);
9067 /* Look for the `template' keyword. */
9068 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9069 /* Let the front end know that we are processing an explicit
9071 begin_explicit_instantiation ();
9072 /* [temp.explicit] says that we are supposed to ignore access
9073 control while processing explicit instantiation directives. */
9074 push_deferring_access_checks (dk_no_check);
9075 /* Parse a decl-specifier-seq. */
9076 cp_parser_decl_specifier_seq (parser,
9077 CP_PARSER_FLAGS_OPTIONAL,
9079 &declares_class_or_enum);
9080 /* If there was exactly one decl-specifier, and it declared a class,
9081 and there's no declarator, then we have an explicit type
9083 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9087 type = check_tag_decl (&decl_specifiers);
9088 /* Turn access control back on for names used during
9089 template instantiation. */
9090 pop_deferring_access_checks ();
9092 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9096 cp_declarator *declarator;
9099 /* Parse the declarator. */
9101 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9102 /*ctor_dtor_or_conv_p=*/NULL,
9103 /*parenthesized_p=*/NULL);
9104 cp_parser_check_for_definition_in_return_type (declarator,
9105 declares_class_or_enum);
9106 if (declarator != cp_error_declarator)
9108 decl = grokdeclarator (declarator, &decl_specifiers,
9110 /* Turn access control back on for names used during
9111 template instantiation. */
9112 pop_deferring_access_checks ();
9113 /* Do the explicit instantiation. */
9114 do_decl_instantiation (decl, extension_specifier);
9118 pop_deferring_access_checks ();
9119 /* Skip the body of the explicit instantiation. */
9120 cp_parser_skip_to_end_of_statement (parser);
9123 /* We're done with the instantiation. */
9124 end_explicit_instantiation ();
9126 cp_parser_consume_semicolon_at_end_of_statement (parser);
9129 /* Parse an explicit-specialization.
9131 explicit-specialization:
9132 template < > declaration
9134 Although the standard says `declaration', what it really means is:
9136 explicit-specialization:
9137 template <> decl-specifier [opt] init-declarator [opt] ;
9138 template <> function-definition
9139 template <> explicit-specialization
9140 template <> template-declaration */
9143 cp_parser_explicit_specialization (cp_parser* parser)
9145 /* Look for the `template' keyword. */
9146 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9147 /* Look for the `<'. */
9148 cp_parser_require (parser, CPP_LESS, "`<'");
9149 /* Look for the `>'. */
9150 cp_parser_require (parser, CPP_GREATER, "`>'");
9151 /* We have processed another parameter list. */
9152 ++parser->num_template_parameter_lists;
9153 /* Let the front end know that we are beginning a specialization. */
9154 begin_specialization ();
9156 /* If the next keyword is `template', we need to figure out whether
9157 or not we're looking a template-declaration. */
9158 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9160 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9161 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9162 cp_parser_template_declaration_after_export (parser,
9163 /*member_p=*/false);
9165 cp_parser_explicit_specialization (parser);
9168 /* Parse the dependent declaration. */
9169 cp_parser_single_declaration (parser,
9173 /* We're done with the specialization. */
9174 end_specialization ();
9175 /* We're done with this parameter list. */
9176 --parser->num_template_parameter_lists;
9179 /* Parse a type-specifier.
9182 simple-type-specifier
9185 elaborated-type-specifier
9193 Returns a representation of the type-specifier. For a
9194 class-specifier, enum-specifier, or elaborated-type-specifier, a
9195 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9197 If IS_FRIEND is TRUE then this type-specifier is being declared a
9198 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
9199 appearing in a decl-specifier-seq.
9201 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9202 class-specifier, enum-specifier, or elaborated-type-specifier, then
9203 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9204 if a type is declared; 2 if it is defined. Otherwise, it is set to
9207 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9208 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9212 cp_parser_type_specifier (cp_parser* parser,
9213 cp_parser_flags flags,
9214 cp_decl_specifier_seq *decl_specs,
9215 bool is_declaration,
9216 int* declares_class_or_enum,
9217 bool* is_cv_qualifier)
9219 tree type_spec = NULL_TREE;
9222 cp_decl_spec ds = ds_last;
9224 /* Assume this type-specifier does not declare a new type. */
9225 if (declares_class_or_enum)
9226 *declares_class_or_enum = 0;
9227 /* And that it does not specify a cv-qualifier. */
9228 if (is_cv_qualifier)
9229 *is_cv_qualifier = false;
9230 /* Peek at the next token. */
9231 token = cp_lexer_peek_token (parser->lexer);
9233 /* If we're looking at a keyword, we can use that to guide the
9234 production we choose. */
9235 keyword = token->keyword;
9238 /* Any of these indicate either a class-specifier, or an
9239 elaborated-type-specifier. */
9244 /* Parse tentatively so that we can back up if we don't find a
9245 class-specifier or enum-specifier. */
9246 cp_parser_parse_tentatively (parser);
9247 /* Look for the class-specifier or enum-specifier. */
9248 if (keyword == RID_ENUM)
9249 type_spec = cp_parser_enum_specifier (parser);
9251 type_spec = cp_parser_class_specifier (parser);
9253 /* If that worked, we're done. */
9254 if (cp_parser_parse_definitely (parser))
9256 if (declares_class_or_enum)
9257 *declares_class_or_enum = 2;
9259 cp_parser_set_decl_spec_type (decl_specs,
9261 /*user_defined_p=*/true);
9268 /* Look for an elaborated-type-specifier. */
9270 = (cp_parser_elaborated_type_specifier
9272 decl_specs && decl_specs->specs[(int) ds_friend],
9274 /* We're declaring a class or enum -- unless we're using
9276 if (declares_class_or_enum && keyword != RID_TYPENAME)
9277 *declares_class_or_enum = 1;
9279 cp_parser_set_decl_spec_type (decl_specs,
9281 /*user_defined_p=*/true);
9286 if (is_cv_qualifier)
9287 *is_cv_qualifier = true;
9292 if (is_cv_qualifier)
9293 *is_cv_qualifier = true;
9298 if (is_cv_qualifier)
9299 *is_cv_qualifier = true;
9303 /* The `__complex__' keyword is a GNU extension. */
9311 /* Handle simple keywords. */
9316 ++decl_specs->specs[(int)ds];
9317 decl_specs->any_specifiers_p = true;
9319 return cp_lexer_consume_token (parser->lexer)->value;
9322 /* If we do not already have a type-specifier, assume we are looking
9323 at a simple-type-specifier. */
9324 type_spec = cp_parser_simple_type_specifier (parser,
9328 /* If we didn't find a type-specifier, and a type-specifier was not
9329 optional in this context, issue an error message. */
9330 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9332 cp_parser_error (parser, "expected type specifier");
9333 return error_mark_node;
9339 /* Parse a simple-type-specifier.
9341 simple-type-specifier:
9342 :: [opt] nested-name-specifier [opt] type-name
9343 :: [opt] nested-name-specifier template template-id
9358 simple-type-specifier:
9359 __typeof__ unary-expression
9360 __typeof__ ( type-id )
9362 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9363 appropriately updated. */
9366 cp_parser_simple_type_specifier (cp_parser* parser,
9367 cp_decl_specifier_seq *decl_specs,
9368 cp_parser_flags flags)
9370 tree type = NULL_TREE;
9373 /* Peek at the next token. */
9374 token = cp_lexer_peek_token (parser->lexer);
9376 /* If we're looking at a keyword, things are easy. */
9377 switch (token->keyword)
9381 decl_specs->explicit_char_p = true;
9382 type = char_type_node;
9385 type = wchar_type_node;
9388 type = boolean_type_node;
9392 ++decl_specs->specs[(int) ds_short];
9393 type = short_integer_type_node;
9397 decl_specs->explicit_int_p = true;
9398 type = integer_type_node;
9402 ++decl_specs->specs[(int) ds_long];
9403 type = long_integer_type_node;
9407 ++decl_specs->specs[(int) ds_signed];
9408 type = integer_type_node;
9412 ++decl_specs->specs[(int) ds_unsigned];
9413 type = unsigned_type_node;
9416 type = float_type_node;
9419 type = double_type_node;
9422 type = void_type_node;
9426 /* Consume the `typeof' token. */
9427 cp_lexer_consume_token (parser->lexer);
9428 /* Parse the operand to `typeof'. */
9429 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9430 /* If it is not already a TYPE, take its type. */
9432 type = finish_typeof (type);
9435 cp_parser_set_decl_spec_type (decl_specs, type,
9436 /*user_defined_p=*/true);
9444 /* If the type-specifier was for a built-in type, we're done. */
9449 /* Record the type. */
9451 && (token->keyword != RID_SIGNED
9452 && token->keyword != RID_UNSIGNED
9453 && token->keyword != RID_SHORT
9454 && token->keyword != RID_LONG))
9455 cp_parser_set_decl_spec_type (decl_specs,
9457 /*user_defined=*/false);
9459 decl_specs->any_specifiers_p = true;
9461 /* Consume the token. */
9462 id = cp_lexer_consume_token (parser->lexer)->value;
9464 /* There is no valid C++ program where a non-template type is
9465 followed by a "<". That usually indicates that the user thought
9466 that the type was a template. */
9467 cp_parser_check_for_invalid_template_id (parser, type);
9469 return TYPE_NAME (type);
9472 /* The type-specifier must be a user-defined type. */
9473 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9477 /* Don't gobble tokens or issue error messages if this is an
9478 optional type-specifier. */
9479 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9480 cp_parser_parse_tentatively (parser);
9482 /* Look for the optional `::' operator. */
9483 cp_parser_global_scope_opt (parser,
9484 /*current_scope_valid_p=*/false);
9485 /* Look for the nested-name specifier. */
9487 = (cp_parser_nested_name_specifier_opt (parser,
9488 /*typename_keyword_p=*/false,
9489 /*check_dependency_p=*/true,
9491 /*is_declaration=*/false)
9493 /* If we have seen a nested-name-specifier, and the next token
9494 is `template', then we are using the template-id production. */
9496 && cp_parser_optional_template_keyword (parser))
9498 /* Look for the template-id. */
9499 type = cp_parser_template_id (parser,
9500 /*template_keyword_p=*/true,
9501 /*check_dependency_p=*/true,
9502 /*is_declaration=*/false);
9503 /* If the template-id did not name a type, we are out of
9505 if (TREE_CODE (type) != TYPE_DECL)
9507 cp_parser_error (parser, "expected template-id for type");
9511 /* Otherwise, look for a type-name. */
9513 type = cp_parser_type_name (parser);
9514 /* Keep track of all name-lookups performed in class scopes. */
9517 && TREE_CODE (type) == TYPE_DECL
9518 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9519 maybe_note_name_used_in_class (DECL_NAME (type), type);
9520 /* If it didn't work out, we don't have a TYPE. */
9521 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9522 && !cp_parser_parse_definitely (parser))
9524 if (type && decl_specs)
9525 cp_parser_set_decl_spec_type (decl_specs, type,
9526 /*user_defined=*/true);
9529 /* If we didn't get a type-name, issue an error message. */
9530 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9532 cp_parser_error (parser, "expected type-name");
9533 return error_mark_node;
9536 /* There is no valid C++ program where a non-template type is
9537 followed by a "<". That usually indicates that the user thought
9538 that the type was a template. */
9539 if (type && type != error_mark_node)
9540 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9545 /* Parse a type-name.
9558 Returns a TYPE_DECL for the the type. */
9561 cp_parser_type_name (cp_parser* parser)
9566 /* We can't know yet whether it is a class-name or not. */
9567 cp_parser_parse_tentatively (parser);
9568 /* Try a class-name. */
9569 type_decl = cp_parser_class_name (parser,
9570 /*typename_keyword_p=*/false,
9571 /*template_keyword_p=*/false,
9573 /*check_dependency_p=*/true,
9574 /*class_head_p=*/false,
9575 /*is_declaration=*/false);
9576 /* If it's not a class-name, keep looking. */
9577 if (!cp_parser_parse_definitely (parser))
9579 /* It must be a typedef-name or an enum-name. */
9580 identifier = cp_parser_identifier (parser);
9581 if (identifier == error_mark_node)
9582 return error_mark_node;
9584 /* Look up the type-name. */
9585 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9586 /* Issue an error if we did not find a type-name. */
9587 if (TREE_CODE (type_decl) != TYPE_DECL)
9589 if (!cp_parser_simulate_error (parser))
9590 cp_parser_name_lookup_error (parser, identifier, type_decl,
9592 type_decl = error_mark_node;
9594 /* Remember that the name was used in the definition of the
9595 current class so that we can check later to see if the
9596 meaning would have been different after the class was
9597 entirely defined. */
9598 else if (type_decl != error_mark_node
9600 maybe_note_name_used_in_class (identifier, type_decl);
9607 /* Parse an elaborated-type-specifier. Note that the grammar given
9608 here incorporates the resolution to DR68.
9610 elaborated-type-specifier:
9611 class-key :: [opt] nested-name-specifier [opt] identifier
9612 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9613 enum :: [opt] nested-name-specifier [opt] identifier
9614 typename :: [opt] nested-name-specifier identifier
9615 typename :: [opt] nested-name-specifier template [opt]
9620 elaborated-type-specifier:
9621 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9622 class-key attributes :: [opt] nested-name-specifier [opt]
9623 template [opt] template-id
9624 enum attributes :: [opt] nested-name-specifier [opt] identifier
9626 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9627 declared `friend'. If IS_DECLARATION is TRUE, then this
9628 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9629 something is being declared.
9631 Returns the TYPE specified. */
9634 cp_parser_elaborated_type_specifier (cp_parser* parser,
9636 bool is_declaration)
9638 enum tag_types tag_type;
9640 tree type = NULL_TREE;
9641 tree attributes = NULL_TREE;
9643 /* See if we're looking at the `enum' keyword. */
9644 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9646 /* Consume the `enum' token. */
9647 cp_lexer_consume_token (parser->lexer);
9648 /* Remember that it's an enumeration type. */
9649 tag_type = enum_type;
9650 /* Parse the attributes. */
9651 attributes = cp_parser_attributes_opt (parser);
9653 /* Or, it might be `typename'. */
9654 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9657 /* Consume the `typename' token. */
9658 cp_lexer_consume_token (parser->lexer);
9659 /* Remember that it's a `typename' type. */
9660 tag_type = typename_type;
9661 /* The `typename' keyword is only allowed in templates. */
9662 if (!processing_template_decl)
9663 pedwarn ("using `typename' outside of template");
9665 /* Otherwise it must be a class-key. */
9668 tag_type = cp_parser_class_key (parser);
9669 if (tag_type == none_type)
9670 return error_mark_node;
9671 /* Parse the attributes. */
9672 attributes = cp_parser_attributes_opt (parser);
9675 /* Look for the `::' operator. */
9676 cp_parser_global_scope_opt (parser,
9677 /*current_scope_valid_p=*/false);
9678 /* Look for the nested-name-specifier. */
9679 if (tag_type == typename_type)
9681 if (cp_parser_nested_name_specifier (parser,
9682 /*typename_keyword_p=*/true,
9683 /*check_dependency_p=*/true,
9687 return error_mark_node;
9690 /* Even though `typename' is not present, the proposed resolution
9691 to Core Issue 180 says that in `class A<T>::B', `B' should be
9692 considered a type-name, even if `A<T>' is dependent. */
9693 cp_parser_nested_name_specifier_opt (parser,
9694 /*typename_keyword_p=*/true,
9695 /*check_dependency_p=*/true,
9698 /* For everything but enumeration types, consider a template-id. */
9699 if (tag_type != enum_type)
9701 bool template_p = false;
9704 /* Allow the `template' keyword. */
9705 template_p = cp_parser_optional_template_keyword (parser);
9706 /* If we didn't see `template', we don't know if there's a
9707 template-id or not. */
9709 cp_parser_parse_tentatively (parser);
9710 /* Parse the template-id. */
9711 decl = cp_parser_template_id (parser, template_p,
9712 /*check_dependency_p=*/true,
9714 /* If we didn't find a template-id, look for an ordinary
9716 if (!template_p && !cp_parser_parse_definitely (parser))
9718 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9719 in effect, then we must assume that, upon instantiation, the
9720 template will correspond to a class. */
9721 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9722 && tag_type == typename_type)
9723 type = make_typename_type (parser->scope, decl,
9726 type = TREE_TYPE (decl);
9729 /* For an enumeration type, consider only a plain identifier. */
9732 identifier = cp_parser_identifier (parser);
9734 if (identifier == error_mark_node)
9736 parser->scope = NULL_TREE;
9737 return error_mark_node;
9740 /* For a `typename', we needn't call xref_tag. */
9741 if (tag_type == typename_type)
9742 return cp_parser_make_typename_type (parser, parser->scope,
9744 /* Look up a qualified name in the usual way. */
9749 /* In an elaborated-type-specifier, names are assumed to name
9750 types, so we set IS_TYPE to TRUE when calling
9751 cp_parser_lookup_name. */
9752 decl = cp_parser_lookup_name (parser, identifier,
9754 /*is_template=*/false,
9755 /*is_namespace=*/false,
9756 /*check_dependency=*/true);
9758 /* If we are parsing friend declaration, DECL may be a
9759 TEMPLATE_DECL tree node here. However, we need to check
9760 whether this TEMPLATE_DECL results in valid code. Consider
9761 the following example:
9764 template <class T> class C {};
9767 template <class T> friend class N::C; // #1, valid code
9769 template <class T> class Y {
9770 friend class N::C; // #2, invalid code
9773 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9774 name lookup of `N::C'. We see that friend declaration must
9775 be template for the code to be valid. Note that
9776 processing_template_decl does not work here since it is
9777 always 1 for the above two cases. */
9779 decl = (cp_parser_maybe_treat_template_as_class
9780 (decl, /*tag_name_p=*/is_friend
9781 && parser->num_template_parameter_lists));
9783 if (TREE_CODE (decl) != TYPE_DECL)
9785 error ("expected type-name");
9786 return error_mark_node;
9789 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9790 check_elaborated_type_specifier
9792 (parser->num_template_parameter_lists
9793 || DECL_SELF_REFERENCE_P (decl)));
9795 type = TREE_TYPE (decl);
9799 /* An elaborated-type-specifier sometimes introduces a new type and
9800 sometimes names an existing type. Normally, the rule is that it
9801 introduces a new type only if there is not an existing type of
9802 the same name already in scope. For example, given:
9805 void f() { struct S s; }
9807 the `struct S' in the body of `f' is the same `struct S' as in
9808 the global scope; the existing definition is used. However, if
9809 there were no global declaration, this would introduce a new
9810 local class named `S'.
9812 An exception to this rule applies to the following code:
9814 namespace N { struct S; }
9816 Here, the elaborated-type-specifier names a new type
9817 unconditionally; even if there is already an `S' in the
9818 containing scope this declaration names a new type.
9819 This exception only applies if the elaborated-type-specifier
9820 forms the complete declaration:
9824 A declaration consisting solely of `class-key identifier ;' is
9825 either a redeclaration of the name in the current scope or a
9826 forward declaration of the identifier as a class name. It
9827 introduces the name into the current scope.
9829 We are in this situation precisely when the next token is a `;'.
9831 An exception to the exception is that a `friend' declaration does
9832 *not* name a new type; i.e., given:
9834 struct S { friend struct T; };
9836 `T' is not a new type in the scope of `S'.
9838 Also, `new struct S' or `sizeof (struct S)' never results in the
9839 definition of a new type; a new type can only be declared in a
9840 declaration context. */
9842 /* Warn about attributes. They are ignored. */
9844 warning ("type attributes are honored only at type definition");
9846 type = xref_tag (tag_type, identifier,
9849 || cp_lexer_next_token_is_not (parser->lexer,
9851 parser->num_template_parameter_lists);
9854 if (tag_type != enum_type)
9855 cp_parser_check_class_key (tag_type, type);
9857 /* A "<" cannot follow an elaborated type specifier. If that
9858 happens, the user was probably trying to form a template-id. */
9859 cp_parser_check_for_invalid_template_id (parser, type);
9864 /* Parse an enum-specifier.
9867 enum identifier [opt] { enumerator-list [opt] }
9869 Returns an ENUM_TYPE representing the enumeration. */
9872 cp_parser_enum_specifier (cp_parser* parser)
9875 tree identifier = NULL_TREE;
9878 /* Look for the `enum' keyword. */
9879 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9880 return error_mark_node;
9881 /* Peek at the next token. */
9882 token = cp_lexer_peek_token (parser->lexer);
9884 /* See if it is an identifier. */
9885 if (token->type == CPP_NAME)
9886 identifier = cp_parser_identifier (parser);
9888 /* Look for the `{'. */
9889 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9890 return error_mark_node;
9892 /* At this point, we're going ahead with the enum-specifier, even
9893 if some other problem occurs. */
9894 cp_parser_commit_to_tentative_parse (parser);
9896 /* Issue an error message if type-definitions are forbidden here. */
9897 cp_parser_check_type_definition (parser);
9899 /* Create the new type. */
9900 type = start_enum (identifier ? identifier : make_anon_name ());
9902 /* Peek at the next token. */
9903 token = cp_lexer_peek_token (parser->lexer);
9904 /* If it's not a `}', then there are some enumerators. */
9905 if (token->type != CPP_CLOSE_BRACE)
9906 cp_parser_enumerator_list (parser, type);
9907 /* Look for the `}'. */
9908 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9910 /* Finish up the enumeration. */
9916 /* Parse an enumerator-list. The enumerators all have the indicated
9920 enumerator-definition
9921 enumerator-list , enumerator-definition */
9924 cp_parser_enumerator_list (cp_parser* parser, tree type)
9930 /* Parse an enumerator-definition. */
9931 cp_parser_enumerator_definition (parser, type);
9932 /* Peek at the next token. */
9933 token = cp_lexer_peek_token (parser->lexer);
9934 /* If it's not a `,', then we've reached the end of the
9936 if (token->type != CPP_COMMA)
9938 /* Otherwise, consume the `,' and keep going. */
9939 cp_lexer_consume_token (parser->lexer);
9940 /* If the next token is a `}', there is a trailing comma. */
9941 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9943 if (pedantic && !in_system_header)
9944 pedwarn ("comma at end of enumerator list");
9950 /* Parse an enumerator-definition. The enumerator has the indicated
9953 enumerator-definition:
9955 enumerator = constant-expression
9961 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9967 /* Look for the identifier. */
9968 identifier = cp_parser_identifier (parser);
9969 if (identifier == error_mark_node)
9972 /* Peek at the next token. */
9973 token = cp_lexer_peek_token (parser->lexer);
9974 /* If it's an `=', then there's an explicit value. */
9975 if (token->type == CPP_EQ)
9977 /* Consume the `=' token. */
9978 cp_lexer_consume_token (parser->lexer);
9979 /* Parse the value. */
9980 value = cp_parser_constant_expression (parser,
9981 /*allow_non_constant_p=*/false,
9987 /* Create the enumerator. */
9988 build_enumerator (identifier, value, type);
9991 /* Parse a namespace-name.
9994 original-namespace-name
9997 Returns the NAMESPACE_DECL for the namespace. */
10000 cp_parser_namespace_name (cp_parser* parser)
10003 tree namespace_decl;
10005 /* Get the name of the namespace. */
10006 identifier = cp_parser_identifier (parser);
10007 if (identifier == error_mark_node)
10008 return error_mark_node;
10010 /* Look up the identifier in the currently active scope. Look only
10011 for namespaces, due to:
10013 [basic.lookup.udir]
10015 When looking up a namespace-name in a using-directive or alias
10016 definition, only namespace names are considered.
10020 [basic.lookup.qual]
10022 During the lookup of a name preceding the :: scope resolution
10023 operator, object, function, and enumerator names are ignored.
10025 (Note that cp_parser_class_or_namespace_name only calls this
10026 function if the token after the name is the scope resolution
10028 namespace_decl = cp_parser_lookup_name (parser, identifier,
10030 /*is_template=*/false,
10031 /*is_namespace=*/true,
10032 /*check_dependency=*/true);
10033 /* If it's not a namespace, issue an error. */
10034 if (namespace_decl == error_mark_node
10035 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10037 cp_parser_error (parser, "expected namespace-name");
10038 namespace_decl = error_mark_node;
10041 return namespace_decl;
10044 /* Parse a namespace-definition.
10046 namespace-definition:
10047 named-namespace-definition
10048 unnamed-namespace-definition
10050 named-namespace-definition:
10051 original-namespace-definition
10052 extension-namespace-definition
10054 original-namespace-definition:
10055 namespace identifier { namespace-body }
10057 extension-namespace-definition:
10058 namespace original-namespace-name { namespace-body }
10060 unnamed-namespace-definition:
10061 namespace { namespace-body } */
10064 cp_parser_namespace_definition (cp_parser* parser)
10068 /* Look for the `namespace' keyword. */
10069 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10071 /* Get the name of the namespace. We do not attempt to distinguish
10072 between an original-namespace-definition and an
10073 extension-namespace-definition at this point. The semantic
10074 analysis routines are responsible for that. */
10075 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10076 identifier = cp_parser_identifier (parser);
10078 identifier = NULL_TREE;
10080 /* Look for the `{' to start the namespace. */
10081 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10082 /* Start the namespace. */
10083 push_namespace (identifier);
10084 /* Parse the body of the namespace. */
10085 cp_parser_namespace_body (parser);
10086 /* Finish the namespace. */
10088 /* Look for the final `}'. */
10089 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10092 /* Parse a namespace-body.
10095 declaration-seq [opt] */
10098 cp_parser_namespace_body (cp_parser* parser)
10100 cp_parser_declaration_seq_opt (parser);
10103 /* Parse a namespace-alias-definition.
10105 namespace-alias-definition:
10106 namespace identifier = qualified-namespace-specifier ; */
10109 cp_parser_namespace_alias_definition (cp_parser* parser)
10112 tree namespace_specifier;
10114 /* Look for the `namespace' keyword. */
10115 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10116 /* Look for the identifier. */
10117 identifier = cp_parser_identifier (parser);
10118 if (identifier == error_mark_node)
10120 /* Look for the `=' token. */
10121 cp_parser_require (parser, CPP_EQ, "`='");
10122 /* Look for the qualified-namespace-specifier. */
10123 namespace_specifier
10124 = cp_parser_qualified_namespace_specifier (parser);
10125 /* Look for the `;' token. */
10126 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10128 /* Register the alias in the symbol table. */
10129 do_namespace_alias (identifier, namespace_specifier);
10132 /* Parse a qualified-namespace-specifier.
10134 qualified-namespace-specifier:
10135 :: [opt] nested-name-specifier [opt] namespace-name
10137 Returns a NAMESPACE_DECL corresponding to the specified
10141 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10143 /* Look for the optional `::'. */
10144 cp_parser_global_scope_opt (parser,
10145 /*current_scope_valid_p=*/false);
10147 /* Look for the optional nested-name-specifier. */
10148 cp_parser_nested_name_specifier_opt (parser,
10149 /*typename_keyword_p=*/false,
10150 /*check_dependency_p=*/true,
10152 /*is_declaration=*/true);
10154 return cp_parser_namespace_name (parser);
10157 /* Parse a using-declaration.
10160 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10161 using :: unqualified-id ; */
10164 cp_parser_using_declaration (cp_parser* parser)
10167 bool typename_p = false;
10168 bool global_scope_p;
10174 /* Look for the `using' keyword. */
10175 cp_parser_require_keyword (parser, RID_USING, "`using'");
10177 /* Peek at the next token. */
10178 token = cp_lexer_peek_token (parser->lexer);
10179 /* See if it's `typename'. */
10180 if (token->keyword == RID_TYPENAME)
10182 /* Remember that we've seen it. */
10184 /* Consume the `typename' token. */
10185 cp_lexer_consume_token (parser->lexer);
10188 /* Look for the optional global scope qualification. */
10190 = (cp_parser_global_scope_opt (parser,
10191 /*current_scope_valid_p=*/false)
10194 /* If we saw `typename', or didn't see `::', then there must be a
10195 nested-name-specifier present. */
10196 if (typename_p || !global_scope_p)
10197 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10198 /*check_dependency_p=*/true,
10200 /*is_declaration=*/true);
10201 /* Otherwise, we could be in either of the two productions. In that
10202 case, treat the nested-name-specifier as optional. */
10204 qscope = cp_parser_nested_name_specifier_opt (parser,
10205 /*typename_keyword_p=*/false,
10206 /*check_dependency_p=*/true,
10208 /*is_declaration=*/true);
10210 qscope = global_namespace;
10212 /* Parse the unqualified-id. */
10213 identifier = cp_parser_unqualified_id (parser,
10214 /*template_keyword_p=*/false,
10215 /*check_dependency_p=*/true,
10216 /*declarator_p=*/true);
10218 /* The function we call to handle a using-declaration is different
10219 depending on what scope we are in. */
10220 if (identifier == error_mark_node)
10222 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10223 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10224 /* [namespace.udecl]
10226 A using declaration shall not name a template-id. */
10227 error ("a template-id may not appear in a using-declaration");
10230 scope = current_scope ();
10231 if (scope && TYPE_P (scope))
10233 /* Create the USING_DECL. */
10234 decl = do_class_using_decl (build_nt (SCOPE_REF,
10237 /* Add it to the list of members in this class. */
10238 finish_member_declaration (decl);
10242 decl = cp_parser_lookup_name_simple (parser, identifier);
10243 if (decl == error_mark_node)
10244 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10246 do_local_using_decl (decl, qscope, identifier);
10248 do_toplevel_using_decl (decl, qscope, identifier);
10252 /* Look for the final `;'. */
10253 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10256 /* Parse a using-directive.
10259 using namespace :: [opt] nested-name-specifier [opt]
10260 namespace-name ; */
10263 cp_parser_using_directive (cp_parser* parser)
10265 tree namespace_decl;
10268 /* Look for the `using' keyword. */
10269 cp_parser_require_keyword (parser, RID_USING, "`using'");
10270 /* And the `namespace' keyword. */
10271 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10272 /* Look for the optional `::' operator. */
10273 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10274 /* And the optional nested-name-specifier. */
10275 cp_parser_nested_name_specifier_opt (parser,
10276 /*typename_keyword_p=*/false,
10277 /*check_dependency_p=*/true,
10279 /*is_declaration=*/true);
10280 /* Get the namespace being used. */
10281 namespace_decl = cp_parser_namespace_name (parser);
10282 /* And any specified attributes. */
10283 attribs = cp_parser_attributes_opt (parser);
10284 /* Update the symbol table. */
10285 parse_using_directive (namespace_decl, attribs);
10286 /* Look for the final `;'. */
10287 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10290 /* Parse an asm-definition.
10293 asm ( string-literal ) ;
10298 asm volatile [opt] ( string-literal ) ;
10299 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10300 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10301 : asm-operand-list [opt] ) ;
10302 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10303 : asm-operand-list [opt]
10304 : asm-operand-list [opt] ) ; */
10307 cp_parser_asm_definition (cp_parser* parser)
10311 tree outputs = NULL_TREE;
10312 tree inputs = NULL_TREE;
10313 tree clobbers = NULL_TREE;
10315 bool volatile_p = false;
10316 bool extended_p = false;
10318 /* Look for the `asm' keyword. */
10319 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10320 /* See if the next token is `volatile'. */
10321 if (cp_parser_allow_gnu_extensions_p (parser)
10322 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10324 /* Remember that we saw the `volatile' keyword. */
10326 /* Consume the token. */
10327 cp_lexer_consume_token (parser->lexer);
10329 /* Look for the opening `('. */
10330 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
10331 /* Look for the string. */
10332 c_lex_string_translate = 0;
10333 token = cp_parser_require (parser, CPP_STRING, "asm body");
10336 string = token->value;
10337 /* If we're allowing GNU extensions, check for the extended assembly
10338 syntax. Unfortunately, the `:' tokens need not be separated by
10339 a space in C, and so, for compatibility, we tolerate that here
10340 too. Doing that means that we have to treat the `::' operator as
10342 if (cp_parser_allow_gnu_extensions_p (parser)
10343 && at_function_scope_p ()
10344 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10345 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10347 bool inputs_p = false;
10348 bool clobbers_p = false;
10350 /* The extended syntax was used. */
10353 /* Look for outputs. */
10354 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10356 /* Consume the `:'. */
10357 cp_lexer_consume_token (parser->lexer);
10358 /* Parse the output-operands. */
10359 if (cp_lexer_next_token_is_not (parser->lexer,
10361 && cp_lexer_next_token_is_not (parser->lexer,
10363 && cp_lexer_next_token_is_not (parser->lexer,
10365 outputs = cp_parser_asm_operand_list (parser);
10367 /* If the next token is `::', there are no outputs, and the
10368 next token is the beginning of the inputs. */
10369 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10371 /* Consume the `::' token. */
10372 cp_lexer_consume_token (parser->lexer);
10373 /* The inputs are coming next. */
10377 /* Look for inputs. */
10379 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10382 /* Consume the `:'. */
10383 cp_lexer_consume_token (parser->lexer);
10384 /* Parse the output-operands. */
10385 if (cp_lexer_next_token_is_not (parser->lexer,
10387 && cp_lexer_next_token_is_not (parser->lexer,
10389 && cp_lexer_next_token_is_not (parser->lexer,
10391 inputs = cp_parser_asm_operand_list (parser);
10393 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10394 /* The clobbers are coming next. */
10397 /* Look for clobbers. */
10399 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10402 /* Consume the `:'. */
10403 cp_lexer_consume_token (parser->lexer);
10404 /* Parse the clobbers. */
10405 if (cp_lexer_next_token_is_not (parser->lexer,
10407 clobbers = cp_parser_asm_clobber_list (parser);
10410 /* Look for the closing `)'. */
10411 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10412 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10413 /*consume_paren=*/true);
10414 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10416 /* Create the ASM_EXPR. */
10417 if (at_function_scope_p ())
10419 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10421 /* If the extended syntax was not used, mark the ASM_EXPR. */
10423 ASM_INPUT_P (asm_stmt) = 1;
10426 assemble_asm (string);
10429 c_lex_string_translate = 1;
10432 /* Declarators [gram.dcl.decl] */
10434 /* Parse an init-declarator.
10437 declarator initializer [opt]
10442 declarator asm-specification [opt] attributes [opt] initializer [opt]
10444 function-definition:
10445 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10447 decl-specifier-seq [opt] declarator function-try-block
10451 function-definition:
10452 __extension__ function-definition
10454 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10455 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10456 then this declarator appears in a class scope. The new DECL created
10457 by this declarator is returned.
10459 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10460 for a function-definition here as well. If the declarator is a
10461 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10462 be TRUE upon return. By that point, the function-definition will
10463 have been completely parsed.
10465 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10469 cp_parser_init_declarator (cp_parser* parser,
10470 cp_decl_specifier_seq *decl_specifiers,
10471 bool function_definition_allowed_p,
10473 int declares_class_or_enum,
10474 bool* function_definition_p)
10477 cp_declarator *declarator;
10478 tree prefix_attributes;
10480 tree asm_specification;
10482 tree decl = NULL_TREE;
10484 bool is_initialized;
10485 bool is_parenthesized_init;
10486 bool is_non_constant_init;
10487 int ctor_dtor_or_conv_p;
10489 bool pop_p = false;
10491 /* Gather the attributes that were provided with the
10492 decl-specifiers. */
10493 prefix_attributes = decl_specifiers->attributes;
10494 decl_specifiers->attributes = NULL_TREE;
10496 /* Assume that this is not the declarator for a function
10498 if (function_definition_p)
10499 *function_definition_p = false;
10501 /* Defer access checks while parsing the declarator; we cannot know
10502 what names are accessible until we know what is being
10504 resume_deferring_access_checks ();
10506 /* Parse the declarator. */
10508 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10509 &ctor_dtor_or_conv_p,
10510 /*parenthesized_p=*/NULL);
10511 /* Gather up the deferred checks. */
10512 stop_deferring_access_checks ();
10514 /* If the DECLARATOR was erroneous, there's no need to go
10516 if (declarator == cp_error_declarator)
10517 return error_mark_node;
10519 cp_parser_check_for_definition_in_return_type (declarator,
10520 declares_class_or_enum);
10522 /* Figure out what scope the entity declared by the DECLARATOR is
10523 located in. `grokdeclarator' sometimes changes the scope, so
10524 we compute it now. */
10525 scope = get_scope_of_declarator (declarator);
10527 /* If we're allowing GNU extensions, look for an asm-specification
10529 if (cp_parser_allow_gnu_extensions_p (parser))
10531 /* Look for an asm-specification. */
10532 asm_specification = cp_parser_asm_specification_opt (parser);
10533 /* And attributes. */
10534 attributes = cp_parser_attributes_opt (parser);
10538 asm_specification = NULL_TREE;
10539 attributes = NULL_TREE;
10542 /* Peek at the next token. */
10543 token = cp_lexer_peek_token (parser->lexer);
10544 /* Check to see if the token indicates the start of a
10545 function-definition. */
10546 if (cp_parser_token_starts_function_definition_p (token))
10548 if (!function_definition_allowed_p)
10550 /* If a function-definition should not appear here, issue an
10552 cp_parser_error (parser,
10553 "a function-definition is not allowed here");
10554 return error_mark_node;
10558 /* Neither attributes nor an asm-specification are allowed
10559 on a function-definition. */
10560 if (asm_specification)
10561 error ("an asm-specification is not allowed on a function-definition");
10563 error ("attributes are not allowed on a function-definition");
10564 /* This is a function-definition. */
10565 *function_definition_p = true;
10567 /* Parse the function definition. */
10569 decl = cp_parser_save_member_function_body (parser,
10572 prefix_attributes);
10575 = (cp_parser_function_definition_from_specifiers_and_declarator
10576 (parser, decl_specifiers, prefix_attributes, declarator));
10584 Only in function declarations for constructors, destructors, and
10585 type conversions can the decl-specifier-seq be omitted.
10587 We explicitly postpone this check past the point where we handle
10588 function-definitions because we tolerate function-definitions
10589 that are missing their return types in some modes. */
10590 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10592 cp_parser_error (parser,
10593 "expected constructor, destructor, or type conversion");
10594 return error_mark_node;
10597 /* An `=' or an `(' indicates an initializer. */
10598 is_initialized = (token->type == CPP_EQ
10599 || token->type == CPP_OPEN_PAREN);
10600 /* If the init-declarator isn't initialized and isn't followed by a
10601 `,' or `;', it's not a valid init-declarator. */
10602 if (!is_initialized
10603 && token->type != CPP_COMMA
10604 && token->type != CPP_SEMICOLON)
10606 cp_parser_error (parser, "expected init-declarator");
10607 return error_mark_node;
10610 /* Because start_decl has side-effects, we should only call it if we
10611 know we're going ahead. By this point, we know that we cannot
10612 possibly be looking at any other construct. */
10613 cp_parser_commit_to_tentative_parse (parser);
10615 /* If the decl specifiers were bad, issue an error now that we're
10616 sure this was intended to be a declarator. Then continue
10617 declaring the variable(s), as int, to try to cut down on further
10619 if (decl_specifiers->any_specifiers_p
10620 && decl_specifiers->type == error_mark_node)
10622 cp_parser_error (parser, "invalid type in declaration");
10623 decl_specifiers->type = integer_type_node;
10626 /* Check to see whether or not this declaration is a friend. */
10627 friend_p = cp_parser_friend_p (decl_specifiers);
10629 /* Check that the number of template-parameter-lists is OK. */
10630 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10631 return error_mark_node;
10633 /* Enter the newly declared entry in the symbol table. If we're
10634 processing a declaration in a class-specifier, we wait until
10635 after processing the initializer. */
10638 if (parser->in_unbraced_linkage_specification_p)
10640 decl_specifiers->storage_class = sc_extern;
10641 have_extern_spec = false;
10643 decl = start_decl (declarator, decl_specifiers,
10644 is_initialized, attributes, prefix_attributes);
10647 /* Enter the SCOPE. That way unqualified names appearing in the
10648 initializer will be looked up in SCOPE. */
10650 pop_p = push_scope (scope);
10652 /* Perform deferred access control checks, now that we know in which
10653 SCOPE the declared entity resides. */
10654 if (!member_p && decl)
10656 tree saved_current_function_decl = NULL_TREE;
10658 /* If the entity being declared is a function, pretend that we
10659 are in its scope. If it is a `friend', it may have access to
10660 things that would not otherwise be accessible. */
10661 if (TREE_CODE (decl) == FUNCTION_DECL)
10663 saved_current_function_decl = current_function_decl;
10664 current_function_decl = decl;
10667 /* Perform the access control checks for the declarator and the
10668 the decl-specifiers. */
10669 perform_deferred_access_checks ();
10671 /* Restore the saved value. */
10672 if (TREE_CODE (decl) == FUNCTION_DECL)
10673 current_function_decl = saved_current_function_decl;
10676 /* Parse the initializer. */
10677 if (is_initialized)
10678 initializer = cp_parser_initializer (parser,
10679 &is_parenthesized_init,
10680 &is_non_constant_init);
10683 initializer = NULL_TREE;
10684 is_parenthesized_init = false;
10685 is_non_constant_init = true;
10688 /* The old parser allows attributes to appear after a parenthesized
10689 initializer. Mark Mitchell proposed removing this functionality
10690 on the GCC mailing lists on 2002-08-13. This parser accepts the
10691 attributes -- but ignores them. */
10692 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10693 if (cp_parser_attributes_opt (parser))
10694 warning ("attributes after parenthesized initializer ignored");
10696 /* Leave the SCOPE, now that we have processed the initializer. It
10697 is important to do this before calling cp_finish_decl because it
10698 makes decisions about whether to create DECL_EXPRs or not based
10699 on the current scope. */
10703 /* For an in-class declaration, use `grokfield' to create the
10707 decl = grokfield (declarator, decl_specifiers,
10708 initializer, /*asmspec=*/NULL_TREE,
10709 /*attributes=*/NULL_TREE);
10710 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10711 cp_parser_save_default_args (parser, decl);
10714 /* Finish processing the declaration. But, skip friend
10716 if (!friend_p && decl)
10717 cp_finish_decl (decl,
10720 /* If the initializer is in parentheses, then this is
10721 a direct-initialization, which means that an
10722 `explicit' constructor is OK. Otherwise, an
10723 `explicit' constructor cannot be used. */
10724 ((is_parenthesized_init || !is_initialized)
10725 ? 0 : LOOKUP_ONLYCONVERTING));
10727 /* Remember whether or not variables were initialized by
10728 constant-expressions. */
10729 if (decl && TREE_CODE (decl) == VAR_DECL
10730 && is_initialized && !is_non_constant_init)
10731 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10736 /* Parse a declarator.
10740 ptr-operator declarator
10742 abstract-declarator:
10743 ptr-operator abstract-declarator [opt]
10744 direct-abstract-declarator
10749 attributes [opt] direct-declarator
10750 attributes [opt] ptr-operator declarator
10752 abstract-declarator:
10753 attributes [opt] ptr-operator abstract-declarator [opt]
10754 attributes [opt] direct-abstract-declarator
10756 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10757 detect constructor, destructor or conversion operators. It is set
10758 to -1 if the declarator is a name, and +1 if it is a
10759 function. Otherwise it is set to zero. Usually you just want to
10760 test for >0, but internally the negative value is used.
10762 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10763 a decl-specifier-seq unless it declares a constructor, destructor,
10764 or conversion. It might seem that we could check this condition in
10765 semantic analysis, rather than parsing, but that makes it difficult
10766 to handle something like `f()'. We want to notice that there are
10767 no decl-specifiers, and therefore realize that this is an
10768 expression, not a declaration.)
10770 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10771 the declarator is a direct-declarator of the form "(...)". */
10773 static cp_declarator *
10774 cp_parser_declarator (cp_parser* parser,
10775 cp_parser_declarator_kind dcl_kind,
10776 int* ctor_dtor_or_conv_p,
10777 bool* parenthesized_p)
10780 cp_declarator *declarator;
10781 enum tree_code code;
10782 tree cv_qualifier_seq;
10784 tree attributes = NULL_TREE;
10786 /* Assume this is not a constructor, destructor, or type-conversion
10788 if (ctor_dtor_or_conv_p)
10789 *ctor_dtor_or_conv_p = 0;
10791 if (cp_parser_allow_gnu_extensions_p (parser))
10792 attributes = cp_parser_attributes_opt (parser);
10794 /* Peek at the next token. */
10795 token = cp_lexer_peek_token (parser->lexer);
10797 /* Check for the ptr-operator production. */
10798 cp_parser_parse_tentatively (parser);
10799 /* Parse the ptr-operator. */
10800 code = cp_parser_ptr_operator (parser,
10802 &cv_qualifier_seq);
10803 /* If that worked, then we have a ptr-operator. */
10804 if (cp_parser_parse_definitely (parser))
10806 /* If a ptr-operator was found, then this declarator was not
10808 if (parenthesized_p)
10809 *parenthesized_p = true;
10810 /* The dependent declarator is optional if we are parsing an
10811 abstract-declarator. */
10812 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10813 cp_parser_parse_tentatively (parser);
10815 /* Parse the dependent declarator. */
10816 declarator = cp_parser_declarator (parser, dcl_kind,
10817 /*ctor_dtor_or_conv_p=*/NULL,
10818 /*parenthesized_p=*/NULL);
10820 /* If we are parsing an abstract-declarator, we must handle the
10821 case where the dependent declarator is absent. */
10822 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10823 && !cp_parser_parse_definitely (parser))
10826 /* Build the representation of the ptr-operator. */
10828 declarator = make_ptrmem_declarator (cv_qualifier_seq,
10831 else if (code == INDIRECT_REF)
10832 declarator = make_pointer_declarator (cv_qualifier_seq,
10835 declarator = make_reference_declarator (cv_qualifier_seq,
10838 /* Everything else is a direct-declarator. */
10841 if (parenthesized_p)
10842 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10844 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10845 ctor_dtor_or_conv_p);
10848 if (attributes && declarator != cp_error_declarator)
10849 declarator->attributes = attributes;
10854 /* Parse a direct-declarator or direct-abstract-declarator.
10858 direct-declarator ( parameter-declaration-clause )
10859 cv-qualifier-seq [opt]
10860 exception-specification [opt]
10861 direct-declarator [ constant-expression [opt] ]
10864 direct-abstract-declarator:
10865 direct-abstract-declarator [opt]
10866 ( parameter-declaration-clause )
10867 cv-qualifier-seq [opt]
10868 exception-specification [opt]
10869 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10870 ( abstract-declarator )
10872 Returns a representation of the declarator. DCL_KIND is
10873 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10874 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10875 we are parsing a direct-declarator. It is
10876 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10877 of ambiguity we prefer an abstract declarator, as per
10878 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10879 cp_parser_declarator. */
10881 static cp_declarator *
10882 cp_parser_direct_declarator (cp_parser* parser,
10883 cp_parser_declarator_kind dcl_kind,
10884 int* ctor_dtor_or_conv_p)
10887 cp_declarator *declarator = NULL;
10888 tree scope = NULL_TREE;
10889 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10890 bool saved_in_declarator_p = parser->in_declarator_p;
10892 bool pop_p = false;
10896 /* Peek at the next token. */
10897 token = cp_lexer_peek_token (parser->lexer);
10898 if (token->type == CPP_OPEN_PAREN)
10900 /* This is either a parameter-declaration-clause, or a
10901 parenthesized declarator. When we know we are parsing a
10902 named declarator, it must be a parenthesized declarator
10903 if FIRST is true. For instance, `(int)' is a
10904 parameter-declaration-clause, with an omitted
10905 direct-abstract-declarator. But `((*))', is a
10906 parenthesized abstract declarator. Finally, when T is a
10907 template parameter `(T)' is a
10908 parameter-declaration-clause, and not a parenthesized
10911 We first try and parse a parameter-declaration-clause,
10912 and then try a nested declarator (if FIRST is true).
10914 It is not an error for it not to be a
10915 parameter-declaration-clause, even when FIRST is
10921 The first is the declaration of a function while the
10922 second is a the definition of a variable, including its
10925 Having seen only the parenthesis, we cannot know which of
10926 these two alternatives should be selected. Even more
10927 complex are examples like:
10932 The former is a function-declaration; the latter is a
10933 variable initialization.
10935 Thus again, we try a parameter-declaration-clause, and if
10936 that fails, we back out and return. */
10938 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10940 cp_parameter_declarator *params;
10941 unsigned saved_num_template_parameter_lists;
10943 cp_parser_parse_tentatively (parser);
10945 /* Consume the `('. */
10946 cp_lexer_consume_token (parser->lexer);
10949 /* If this is going to be an abstract declarator, we're
10950 in a declarator and we can't have default args. */
10951 parser->default_arg_ok_p = false;
10952 parser->in_declarator_p = true;
10955 /* Inside the function parameter list, surrounding
10956 template-parameter-lists do not apply. */
10957 saved_num_template_parameter_lists
10958 = parser->num_template_parameter_lists;
10959 parser->num_template_parameter_lists = 0;
10961 /* Parse the parameter-declaration-clause. */
10962 params = cp_parser_parameter_declaration_clause (parser);
10964 parser->num_template_parameter_lists
10965 = saved_num_template_parameter_lists;
10967 /* If all went well, parse the cv-qualifier-seq and the
10968 exception-specification. */
10969 if (cp_parser_parse_definitely (parser))
10971 tree cv_qualifiers;
10972 tree exception_specification;
10974 if (ctor_dtor_or_conv_p)
10975 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10977 /* Consume the `)'. */
10978 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10980 /* Parse the cv-qualifier-seq. */
10981 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10982 /* And the exception-specification. */
10983 exception_specification
10984 = cp_parser_exception_specification_opt (parser);
10986 /* Create the function-declarator. */
10987 declarator = make_call_declarator (declarator,
10990 exception_specification);
10991 /* Any subsequent parameter lists are to do with
10992 return type, so are not those of the declared
10994 parser->default_arg_ok_p = false;
10996 /* Repeat the main loop. */
11001 /* If this is the first, we can try a parenthesized
11005 bool saved_in_type_id_in_expr_p;
11007 parser->default_arg_ok_p = saved_default_arg_ok_p;
11008 parser->in_declarator_p = saved_in_declarator_p;
11010 /* Consume the `('. */
11011 cp_lexer_consume_token (parser->lexer);
11012 /* Parse the nested declarator. */
11013 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11014 parser->in_type_id_in_expr_p = true;
11016 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11017 /*parenthesized_p=*/NULL);
11018 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11020 /* Expect a `)'. */
11021 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11022 declarator = cp_error_declarator;
11023 if (declarator == cp_error_declarator)
11026 goto handle_declarator;
11028 /* Otherwise, we must be done. */
11032 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11033 && token->type == CPP_OPEN_SQUARE)
11035 /* Parse an array-declarator. */
11038 if (ctor_dtor_or_conv_p)
11039 *ctor_dtor_or_conv_p = 0;
11042 parser->default_arg_ok_p = false;
11043 parser->in_declarator_p = true;
11044 /* Consume the `['. */
11045 cp_lexer_consume_token (parser->lexer);
11046 /* Peek at the next token. */
11047 token = cp_lexer_peek_token (parser->lexer);
11048 /* If the next token is `]', then there is no
11049 constant-expression. */
11050 if (token->type != CPP_CLOSE_SQUARE)
11052 bool non_constant_p;
11055 = cp_parser_constant_expression (parser,
11056 /*allow_non_constant=*/true,
11058 if (!non_constant_p)
11059 bounds = fold_non_dependent_expr (bounds);
11062 bounds = NULL_TREE;
11063 /* Look for the closing `]'. */
11064 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11066 declarator = cp_error_declarator;
11070 declarator = make_array_declarator (declarator, bounds);
11072 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11076 /* Parse a declarator-id */
11077 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11078 cp_parser_parse_tentatively (parser);
11079 id = cp_parser_declarator_id (parser);
11080 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11082 if (!cp_parser_parse_definitely (parser))
11083 id = error_mark_node;
11084 else if (TREE_CODE (id) != IDENTIFIER_NODE)
11086 cp_parser_error (parser, "expected unqualified-id");
11087 id = error_mark_node;
11091 if (id == error_mark_node)
11093 declarator = cp_error_declarator;
11097 if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11099 tree scope = TREE_OPERAND (id, 0);
11101 /* In the declaration of a member of a template class
11102 outside of the class itself, the SCOPE will sometimes
11103 be a TYPENAME_TYPE. For example, given:
11105 template <typename T>
11106 int S<T>::R::i = 3;
11108 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11109 this context, we must resolve S<T>::R to an ordinary
11110 type, rather than a typename type.
11112 The reason we normally avoid resolving TYPENAME_TYPEs
11113 is that a specialization of `S' might render
11114 `S<T>::R' not a type. However, if `S' is
11115 specialized, then this `i' will not be used, so there
11116 is no harm in resolving the types here. */
11117 if (TREE_CODE (scope) == TYPENAME_TYPE)
11121 /* Resolve the TYPENAME_TYPE. */
11122 type = resolve_typename_type (scope,
11123 /*only_current_p=*/false);
11124 /* If that failed, the declarator is invalid. */
11125 if (type == error_mark_node)
11126 error ("`%T::%D' is not a type",
11127 TYPE_CONTEXT (scope),
11128 TYPE_IDENTIFIER (scope));
11129 /* Build a new DECLARATOR. */
11130 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11134 declarator = make_id_declarator (id);
11138 tree unqualified_name;
11140 if (TREE_CODE (id) == SCOPE_REF
11141 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11143 class_type = TREE_OPERAND (id, 0);
11144 unqualified_name = TREE_OPERAND (id, 1);
11148 class_type = current_class_type;
11149 unqualified_name = id;
11154 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11155 declarator->u.id.sfk = sfk_destructor;
11156 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11157 declarator->u.id.sfk = sfk_conversion;
11158 else if (constructor_name_p (unqualified_name,
11160 || (TREE_CODE (unqualified_name) == TYPE_DECL
11161 && same_type_p (TREE_TYPE (unqualified_name),
11163 declarator->u.id.sfk = sfk_constructor;
11165 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11166 *ctor_dtor_or_conv_p = -1;
11167 if (TREE_CODE (id) == SCOPE_REF
11168 && TREE_CODE (unqualified_name) == TYPE_DECL
11169 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11171 error ("invalid use of constructor as a template");
11172 inform ("use `%T::%D' instead of `%T::%T' to name the "
11173 "constructor in a qualified name", class_type,
11174 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11175 class_type, class_type);
11180 handle_declarator:;
11181 scope = get_scope_of_declarator (declarator);
11183 /* Any names that appear after the declarator-id for a
11184 member are looked up in the containing scope. */
11185 pop_p = push_scope (scope);
11186 parser->in_declarator_p = true;
11187 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11188 || (declarator && declarator->kind == cdk_id))
11189 /* Default args are only allowed on function
11191 parser->default_arg_ok_p = saved_default_arg_ok_p;
11193 parser->default_arg_ok_p = false;
11202 /* For an abstract declarator, we might wind up with nothing at this
11203 point. That's an error; the declarator is not optional. */
11205 cp_parser_error (parser, "expected declarator");
11207 /* If we entered a scope, we must exit it now. */
11211 parser->default_arg_ok_p = saved_default_arg_ok_p;
11212 parser->in_declarator_p = saved_in_declarator_p;
11217 /* Parse a ptr-operator.
11220 * cv-qualifier-seq [opt]
11222 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11227 & cv-qualifier-seq [opt]
11229 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
11230 used. Returns ADDR_EXPR if a reference was used. In the
11231 case of a pointer-to-member, *TYPE is filled in with the
11232 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
11233 with the cv-qualifier-seq, or NULL_TREE, if there are no
11234 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
11236 static enum tree_code
11237 cp_parser_ptr_operator (cp_parser* parser,
11239 tree* cv_qualifier_seq)
11241 enum tree_code code = ERROR_MARK;
11244 /* Assume that it's not a pointer-to-member. */
11246 /* And that there are no cv-qualifiers. */
11247 *cv_qualifier_seq = NULL_TREE;
11249 /* Peek at the next token. */
11250 token = cp_lexer_peek_token (parser->lexer);
11251 /* If it's a `*' or `&' we have a pointer or reference. */
11252 if (token->type == CPP_MULT || token->type == CPP_AND)
11254 /* Remember which ptr-operator we were processing. */
11255 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11257 /* Consume the `*' or `&'. */
11258 cp_lexer_consume_token (parser->lexer);
11260 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11261 `&', if we are allowing GNU extensions. (The only qualifier
11262 that can legally appear after `&' is `restrict', but that is
11263 enforced during semantic analysis. */
11264 if (code == INDIRECT_REF
11265 || cp_parser_allow_gnu_extensions_p (parser))
11266 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
11270 /* Try the pointer-to-member case. */
11271 cp_parser_parse_tentatively (parser);
11272 /* Look for the optional `::' operator. */
11273 cp_parser_global_scope_opt (parser,
11274 /*current_scope_valid_p=*/false);
11275 /* Look for the nested-name specifier. */
11276 cp_parser_nested_name_specifier (parser,
11277 /*typename_keyword_p=*/false,
11278 /*check_dependency_p=*/true,
11280 /*is_declaration=*/false);
11281 /* If we found it, and the next token is a `*', then we are
11282 indeed looking at a pointer-to-member operator. */
11283 if (!cp_parser_error_occurred (parser)
11284 && cp_parser_require (parser, CPP_MULT, "`*'"))
11286 /* The type of which the member is a member is given by the
11288 *type = parser->scope;
11289 /* The next name will not be qualified. */
11290 parser->scope = NULL_TREE;
11291 parser->qualifying_scope = NULL_TREE;
11292 parser->object_scope = NULL_TREE;
11293 /* Indicate that the `*' operator was used. */
11294 code = INDIRECT_REF;
11295 /* Look for the optional cv-qualifier-seq. */
11296 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
11298 /* If that didn't work we don't have a ptr-operator. */
11299 if (!cp_parser_parse_definitely (parser))
11300 cp_parser_error (parser, "expected ptr-operator");
11306 /* Parse an (optional) cv-qualifier-seq.
11309 cv-qualifier cv-qualifier-seq [opt]
11311 Returns a TREE_LIST. The TREE_VALUE of each node is the
11312 representation of a cv-qualifier. */
11315 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11317 tree cv_qualifiers = NULL_TREE;
11323 /* Look for the next cv-qualifier. */
11324 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
11325 /* If we didn't find one, we're done. */
11329 /* Add this cv-qualifier to the list. */
11331 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
11334 /* We built up the list in reverse order. */
11335 return nreverse (cv_qualifiers);
11338 /* Parse an (optional) cv-qualifier.
11350 cp_parser_cv_qualifier_opt (cp_parser* parser)
11353 tree cv_qualifier = NULL_TREE;
11355 /* Peek at the next token. */
11356 token = cp_lexer_peek_token (parser->lexer);
11357 /* See if it's a cv-qualifier. */
11358 switch (token->keyword)
11363 /* Save the value of the token. */
11364 cv_qualifier = token->value;
11365 /* Consume the token. */
11366 cp_lexer_consume_token (parser->lexer);
11373 return cv_qualifier;
11376 /* Parse a declarator-id.
11380 :: [opt] nested-name-specifier [opt] type-name
11382 In the `id-expression' case, the value returned is as for
11383 cp_parser_id_expression if the id-expression was an unqualified-id.
11384 If the id-expression was a qualified-id, then a SCOPE_REF is
11385 returned. The first operand is the scope (either a NAMESPACE_DECL
11386 or TREE_TYPE), but the second is still just a representation of an
11390 cp_parser_declarator_id (cp_parser* parser)
11392 tree id_expression;
11394 /* The expression must be an id-expression. Assume that qualified
11395 names are the names of types so that:
11398 int S<T>::R::i = 3;
11400 will work; we must treat `S<T>::R' as the name of a type.
11401 Similarly, assume that qualified names are templates, where
11405 int S<T>::R<T>::i = 3;
11408 id_expression = cp_parser_id_expression (parser,
11409 /*template_keyword_p=*/false,
11410 /*check_dependency_p=*/false,
11411 /*template_p=*/NULL,
11412 /*declarator_p=*/true);
11413 /* If the name was qualified, create a SCOPE_REF to represent
11417 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11418 parser->scope = NULL_TREE;
11421 return id_expression;
11424 /* Parse a type-id.
11427 type-specifier-seq abstract-declarator [opt]
11429 Returns the TYPE specified. */
11432 cp_parser_type_id (cp_parser* parser)
11434 cp_decl_specifier_seq type_specifier_seq;
11435 cp_declarator *abstract_declarator;
11437 /* Parse the type-specifier-seq. */
11438 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11439 if (type_specifier_seq.type == error_mark_node)
11440 return error_mark_node;
11442 /* There might or might not be an abstract declarator. */
11443 cp_parser_parse_tentatively (parser);
11444 /* Look for the declarator. */
11445 abstract_declarator
11446 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11447 /*parenthesized_p=*/NULL);
11448 /* Check to see if there really was a declarator. */
11449 if (!cp_parser_parse_definitely (parser))
11450 abstract_declarator = NULL;
11452 return groktypename (&type_specifier_seq, abstract_declarator);
11455 /* Parse a type-specifier-seq.
11457 type-specifier-seq:
11458 type-specifier type-specifier-seq [opt]
11462 type-specifier-seq:
11463 attributes type-specifier-seq [opt]
11465 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11468 cp_parser_type_specifier_seq (cp_parser* parser,
11469 cp_decl_specifier_seq *type_specifier_seq)
11471 bool seen_type_specifier = false;
11473 /* Clear the TYPE_SPECIFIER_SEQ. */
11474 clear_decl_specs (type_specifier_seq);
11476 /* Parse the type-specifiers and attributes. */
11479 tree type_specifier;
11481 /* Check for attributes first. */
11482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11484 type_specifier_seq->attributes =
11485 chainon (type_specifier_seq->attributes,
11486 cp_parser_attributes_opt (parser));
11490 /* Look for the type-specifier. */
11491 type_specifier = cp_parser_type_specifier (parser,
11492 CP_PARSER_FLAGS_OPTIONAL,
11493 type_specifier_seq,
11494 /*is_declaration=*/false,
11497 /* If the first type-specifier could not be found, this is not a
11498 type-specifier-seq at all. */
11499 if (!seen_type_specifier && !type_specifier)
11501 cp_parser_error (parser, "expected type-specifier");
11502 type_specifier_seq->type = error_mark_node;
11505 /* If subsequent type-specifiers could not be found, the
11506 type-specifier-seq is complete. */
11507 else if (seen_type_specifier && !type_specifier)
11510 seen_type_specifier = true;
11516 /* Parse a parameter-declaration-clause.
11518 parameter-declaration-clause:
11519 parameter-declaration-list [opt] ... [opt]
11520 parameter-declaration-list , ...
11522 Returns a representation for the parameter declarations. A return
11523 value of NULL indicates a parameter-declaration-clause consisting
11524 only of an ellipsis. */
11526 static cp_parameter_declarator *
11527 cp_parser_parameter_declaration_clause (cp_parser* parser)
11529 cp_parameter_declarator *parameters;
11534 /* Peek at the next token. */
11535 token = cp_lexer_peek_token (parser->lexer);
11536 /* Check for trivial parameter-declaration-clauses. */
11537 if (token->type == CPP_ELLIPSIS)
11539 /* Consume the `...' token. */
11540 cp_lexer_consume_token (parser->lexer);
11543 else if (token->type == CPP_CLOSE_PAREN)
11544 /* There are no parameters. */
11546 #ifndef NO_IMPLICIT_EXTERN_C
11547 if (in_system_header && current_class_type == NULL
11548 && current_lang_name == lang_name_c)
11552 return no_parameters;
11554 /* Check for `(void)', too, which is a special case. */
11555 else if (token->keyword == RID_VOID
11556 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11557 == CPP_CLOSE_PAREN))
11559 /* Consume the `void' token. */
11560 cp_lexer_consume_token (parser->lexer);
11561 /* There are no parameters. */
11562 return no_parameters;
11565 /* Parse the parameter-declaration-list. */
11566 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11567 /* If a parse error occurred while parsing the
11568 parameter-declaration-list, then the entire
11569 parameter-declaration-clause is erroneous. */
11573 /* Peek at the next token. */
11574 token = cp_lexer_peek_token (parser->lexer);
11575 /* If it's a `,', the clause should terminate with an ellipsis. */
11576 if (token->type == CPP_COMMA)
11578 /* Consume the `,'. */
11579 cp_lexer_consume_token (parser->lexer);
11580 /* Expect an ellipsis. */
11582 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11584 /* It might also be `...' if the optional trailing `,' was
11586 else if (token->type == CPP_ELLIPSIS)
11588 /* Consume the `...' token. */
11589 cp_lexer_consume_token (parser->lexer);
11590 /* And remember that we saw it. */
11594 ellipsis_p = false;
11596 /* Finish the parameter list. */
11597 if (parameters && ellipsis_p)
11598 parameters->ellipsis_p = true;
11603 /* Parse a parameter-declaration-list.
11605 parameter-declaration-list:
11606 parameter-declaration
11607 parameter-declaration-list , parameter-declaration
11609 Returns a representation of the parameter-declaration-list, as for
11610 cp_parser_parameter_declaration_clause. However, the
11611 `void_list_node' is never appended to the list. Upon return,
11612 *IS_ERROR will be true iff an error occurred. */
11614 static cp_parameter_declarator *
11615 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11617 cp_parameter_declarator *parameters = NULL;
11618 cp_parameter_declarator **tail = ¶meters;
11620 /* Assume all will go well. */
11623 /* Look for more parameters. */
11626 cp_parameter_declarator *parameter;
11627 bool parenthesized_p;
11628 /* Parse the parameter. */
11630 = cp_parser_parameter_declaration (parser,
11631 /*template_parm_p=*/false,
11634 /* If a parse error occurred parsing the parameter declaration,
11635 then the entire parameter-declaration-list is erroneous. */
11642 /* Add the new parameter to the list. */
11644 tail = ¶meter->next;
11646 /* Peek at the next token. */
11647 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11648 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11649 /* The parameter-declaration-list is complete. */
11651 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11655 /* Peek at the next token. */
11656 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11657 /* If it's an ellipsis, then the list is complete. */
11658 if (token->type == CPP_ELLIPSIS)
11660 /* Otherwise, there must be more parameters. Consume the
11662 cp_lexer_consume_token (parser->lexer);
11663 /* When parsing something like:
11665 int i(float f, double d)
11667 we can tell after seeing the declaration for "f" that we
11668 are not looking at an initialization of a variable "i",
11669 but rather at the declaration of a function "i".
11671 Due to the fact that the parsing of template arguments
11672 (as specified to a template-id) requires backtracking we
11673 cannot use this technique when inside a template argument
11675 if (!parser->in_template_argument_list_p
11676 && !parser->in_type_id_in_expr_p
11677 && cp_parser_parsing_tentatively (parser)
11678 && !cp_parser_committed_to_tentative_parse (parser)
11679 /* However, a parameter-declaration of the form
11680 "foat(f)" (which is a valid declaration of a
11681 parameter "f") can also be interpreted as an
11682 expression (the conversion of "f" to "float"). */
11683 && !parenthesized_p)
11684 cp_parser_commit_to_tentative_parse (parser);
11688 cp_parser_error (parser, "expected `,' or `...'");
11689 if (!cp_parser_parsing_tentatively (parser)
11690 || cp_parser_committed_to_tentative_parse (parser))
11691 cp_parser_skip_to_closing_parenthesis (parser,
11692 /*recovering=*/true,
11693 /*or_comma=*/false,
11694 /*consume_paren=*/false);
11702 /* Parse a parameter declaration.
11704 parameter-declaration:
11705 decl-specifier-seq declarator
11706 decl-specifier-seq declarator = assignment-expression
11707 decl-specifier-seq abstract-declarator [opt]
11708 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11710 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11711 declares a template parameter. (In that case, a non-nested `>'
11712 token encountered during the parsing of the assignment-expression
11713 is not interpreted as a greater-than operator.)
11715 Returns a representation of the parameter, or NULL if an error
11716 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11717 true iff the declarator is of the form "(p)". */
11719 static cp_parameter_declarator *
11720 cp_parser_parameter_declaration (cp_parser *parser,
11721 bool template_parm_p,
11722 bool *parenthesized_p)
11724 int declares_class_or_enum;
11725 bool greater_than_is_operator_p;
11726 cp_decl_specifier_seq decl_specifiers;
11727 cp_declarator *declarator;
11728 tree default_argument;
11730 const char *saved_message;
11732 /* In a template parameter, `>' is not an operator.
11736 When parsing a default template-argument for a non-type
11737 template-parameter, the first non-nested `>' is taken as the end
11738 of the template parameter-list rather than a greater-than
11740 greater_than_is_operator_p = !template_parm_p;
11742 /* Type definitions may not appear in parameter types. */
11743 saved_message = parser->type_definition_forbidden_message;
11744 parser->type_definition_forbidden_message
11745 = "types may not be defined in parameter types";
11747 /* Parse the declaration-specifiers. */
11748 cp_parser_decl_specifier_seq (parser,
11749 CP_PARSER_FLAGS_NONE,
11751 &declares_class_or_enum);
11752 /* If an error occurred, there's no reason to attempt to parse the
11753 rest of the declaration. */
11754 if (cp_parser_error_occurred (parser))
11756 parser->type_definition_forbidden_message = saved_message;
11760 /* Peek at the next token. */
11761 token = cp_lexer_peek_token (parser->lexer);
11762 /* If the next token is a `)', `,', `=', `>', or `...', then there
11763 is no declarator. */
11764 if (token->type == CPP_CLOSE_PAREN
11765 || token->type == CPP_COMMA
11766 || token->type == CPP_EQ
11767 || token->type == CPP_ELLIPSIS
11768 || token->type == CPP_GREATER)
11771 if (parenthesized_p)
11772 *parenthesized_p = false;
11774 /* Otherwise, there should be a declarator. */
11777 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11778 parser->default_arg_ok_p = false;
11780 /* After seeing a decl-specifier-seq, if the next token is not a
11781 "(", there is no possibility that the code is a valid
11782 expression. Therefore, if parsing tentatively, we commit at
11784 if (!parser->in_template_argument_list_p
11785 /* In an expression context, having seen:
11789 we cannot be sure whether we are looking at a
11790 function-type (taking a "char" as a parameter) or a cast
11791 of some object of type "char" to "int". */
11792 && !parser->in_type_id_in_expr_p
11793 && cp_parser_parsing_tentatively (parser)
11794 && !cp_parser_committed_to_tentative_parse (parser)
11795 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11796 cp_parser_commit_to_tentative_parse (parser);
11797 /* Parse the declarator. */
11798 declarator = cp_parser_declarator (parser,
11799 CP_PARSER_DECLARATOR_EITHER,
11800 /*ctor_dtor_or_conv_p=*/NULL,
11802 parser->default_arg_ok_p = saved_default_arg_ok_p;
11803 /* After the declarator, allow more attributes. */
11804 decl_specifiers.attributes
11805 = chainon (decl_specifiers.attributes,
11806 cp_parser_attributes_opt (parser));
11809 /* The restriction on defining new types applies only to the type
11810 of the parameter, not to the default argument. */
11811 parser->type_definition_forbidden_message = saved_message;
11813 /* If the next token is `=', then process a default argument. */
11814 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11816 bool saved_greater_than_is_operator_p;
11817 /* Consume the `='. */
11818 cp_lexer_consume_token (parser->lexer);
11820 /* If we are defining a class, then the tokens that make up the
11821 default argument must be saved and processed later. */
11822 if (!template_parm_p && at_class_scope_p ()
11823 && TYPE_BEING_DEFINED (current_class_type))
11825 unsigned depth = 0;
11827 /* Create a DEFAULT_ARG to represented the unparsed default
11829 default_argument = make_node (DEFAULT_ARG);
11830 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11832 /* Add tokens until we have processed the entire default
11839 /* Peek at the next token. */
11840 token = cp_lexer_peek_token (parser->lexer);
11841 /* What we do depends on what token we have. */
11842 switch (token->type)
11844 /* In valid code, a default argument must be
11845 immediately followed by a `,' `)', or `...'. */
11847 case CPP_CLOSE_PAREN:
11849 /* If we run into a non-nested `;', `}', or `]',
11850 then the code is invalid -- but the default
11851 argument is certainly over. */
11852 case CPP_SEMICOLON:
11853 case CPP_CLOSE_BRACE:
11854 case CPP_CLOSE_SQUARE:
11857 /* Update DEPTH, if necessary. */
11858 else if (token->type == CPP_CLOSE_PAREN
11859 || token->type == CPP_CLOSE_BRACE
11860 || token->type == CPP_CLOSE_SQUARE)
11864 case CPP_OPEN_PAREN:
11865 case CPP_OPEN_SQUARE:
11866 case CPP_OPEN_BRACE:
11871 /* If we see a non-nested `>', and `>' is not an
11872 operator, then it marks the end of the default
11874 if (!depth && !greater_than_is_operator_p)
11878 /* If we run out of tokens, issue an error message. */
11880 error ("file ends in default argument");
11886 /* In these cases, we should look for template-ids.
11887 For example, if the default argument is
11888 `X<int, double>()', we need to do name lookup to
11889 figure out whether or not `X' is a template; if
11890 so, the `,' does not end the default argument.
11892 That is not yet done. */
11899 /* If we've reached the end, stop. */
11903 /* Add the token to the token block. */
11904 token = cp_lexer_consume_token (parser->lexer);
11905 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11909 /* Outside of a class definition, we can just parse the
11910 assignment-expression. */
11913 bool saved_local_variables_forbidden_p;
11915 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11917 saved_greater_than_is_operator_p
11918 = parser->greater_than_is_operator_p;
11919 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11920 /* Local variable names (and the `this' keyword) may not
11921 appear in a default argument. */
11922 saved_local_variables_forbidden_p
11923 = parser->local_variables_forbidden_p;
11924 parser->local_variables_forbidden_p = true;
11925 /* Parse the assignment-expression. */
11926 default_argument = cp_parser_assignment_expression (parser);
11927 /* Restore saved state. */
11928 parser->greater_than_is_operator_p
11929 = saved_greater_than_is_operator_p;
11930 parser->local_variables_forbidden_p
11931 = saved_local_variables_forbidden_p;
11933 if (!parser->default_arg_ok_p)
11935 if (!flag_pedantic_errors)
11936 warning ("deprecated use of default argument for parameter of non-function");
11939 error ("default arguments are only permitted for function parameters");
11940 default_argument = NULL_TREE;
11945 default_argument = NULL_TREE;
11947 return make_parameter_declarator (&decl_specifiers,
11952 /* Parse a function-body.
11955 compound_statement */
11958 cp_parser_function_body (cp_parser *parser)
11960 cp_parser_compound_statement (parser, NULL, false);
11963 /* Parse a ctor-initializer-opt followed by a function-body. Return
11964 true if a ctor-initializer was present. */
11967 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11970 bool ctor_initializer_p;
11972 /* Begin the function body. */
11973 body = begin_function_body ();
11974 /* Parse the optional ctor-initializer. */
11975 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11976 /* Parse the function-body. */
11977 cp_parser_function_body (parser);
11978 /* Finish the function body. */
11979 finish_function_body (body);
11981 return ctor_initializer_p;
11984 /* Parse an initializer.
11987 = initializer-clause
11988 ( expression-list )
11990 Returns a expression representing the initializer. If no
11991 initializer is present, NULL_TREE is returned.
11993 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11994 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11995 set to FALSE if there is no initializer present. If there is an
11996 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11997 is set to true; otherwise it is set to false. */
12000 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12001 bool* non_constant_p)
12006 /* Peek at the next token. */
12007 token = cp_lexer_peek_token (parser->lexer);
12009 /* Let our caller know whether or not this initializer was
12011 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12012 /* Assume that the initializer is constant. */
12013 *non_constant_p = false;
12015 if (token->type == CPP_EQ)
12017 /* Consume the `='. */
12018 cp_lexer_consume_token (parser->lexer);
12019 /* Parse the initializer-clause. */
12020 init = cp_parser_initializer_clause (parser, non_constant_p);
12022 else if (token->type == CPP_OPEN_PAREN)
12023 init = cp_parser_parenthesized_expression_list (parser, false,
12027 /* Anything else is an error. */
12028 cp_parser_error (parser, "expected initializer");
12029 init = error_mark_node;
12035 /* Parse an initializer-clause.
12037 initializer-clause:
12038 assignment-expression
12039 { initializer-list , [opt] }
12042 Returns an expression representing the initializer.
12044 If the `assignment-expression' production is used the value
12045 returned is simply a representation for the expression.
12047 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12048 the elements of the initializer-list (or NULL_TREE, if the last
12049 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12050 NULL_TREE. There is no way to detect whether or not the optional
12051 trailing `,' was provided. NON_CONSTANT_P is as for
12052 cp_parser_initializer. */
12055 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12059 /* If it is not a `{', then we are looking at an
12060 assignment-expression. */
12061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12064 = cp_parser_constant_expression (parser,
12065 /*allow_non_constant_p=*/true,
12067 if (!*non_constant_p)
12068 initializer = fold_non_dependent_expr (initializer);
12072 /* Consume the `{' token. */
12073 cp_lexer_consume_token (parser->lexer);
12074 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12075 initializer = make_node (CONSTRUCTOR);
12076 /* If it's not a `}', then there is a non-trivial initializer. */
12077 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12079 /* Parse the initializer list. */
12080 CONSTRUCTOR_ELTS (initializer)
12081 = cp_parser_initializer_list (parser, non_constant_p);
12082 /* A trailing `,' token is allowed. */
12083 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12084 cp_lexer_consume_token (parser->lexer);
12086 /* Now, there should be a trailing `}'. */
12087 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12090 return initializer;
12093 /* Parse an initializer-list.
12097 initializer-list , initializer-clause
12102 identifier : initializer-clause
12103 initializer-list, identifier : initializer-clause
12105 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12106 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12107 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12108 as for cp_parser_initializer. */
12111 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12113 tree initializers = NULL_TREE;
12115 /* Assume all of the expressions are constant. */
12116 *non_constant_p = false;
12118 /* Parse the rest of the list. */
12124 bool clause_non_constant_p;
12126 /* If the next token is an identifier and the following one is a
12127 colon, we are looking at the GNU designated-initializer
12129 if (cp_parser_allow_gnu_extensions_p (parser)
12130 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12131 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12133 /* Consume the identifier. */
12134 identifier = cp_lexer_consume_token (parser->lexer)->value;
12135 /* Consume the `:'. */
12136 cp_lexer_consume_token (parser->lexer);
12139 identifier = NULL_TREE;
12141 /* Parse the initializer. */
12142 initializer = cp_parser_initializer_clause (parser,
12143 &clause_non_constant_p);
12144 /* If any clause is non-constant, so is the entire initializer. */
12145 if (clause_non_constant_p)
12146 *non_constant_p = true;
12147 /* Add it to the list. */
12148 initializers = tree_cons (identifier, initializer, initializers);
12150 /* If the next token is not a comma, we have reached the end of
12152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12155 /* Peek at the next token. */
12156 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12157 /* If the next token is a `}', then we're still done. An
12158 initializer-clause can have a trailing `,' after the
12159 initializer-list and before the closing `}'. */
12160 if (token->type == CPP_CLOSE_BRACE)
12163 /* Consume the `,' token. */
12164 cp_lexer_consume_token (parser->lexer);
12167 /* The initializers were built up in reverse order, so we need to
12168 reverse them now. */
12169 return nreverse (initializers);
12172 /* Classes [gram.class] */
12174 /* Parse a class-name.
12180 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12181 to indicate that names looked up in dependent types should be
12182 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12183 keyword has been used to indicate that the name that appears next
12184 is a template. TYPE_P is true iff the next name should be treated
12185 as class-name, even if it is declared to be some other kind of name
12186 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12187 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12188 being defined in a class-head.
12190 Returns the TYPE_DECL representing the class. */
12193 cp_parser_class_name (cp_parser *parser,
12194 bool typename_keyword_p,
12195 bool template_keyword_p,
12197 bool check_dependency_p,
12199 bool is_declaration)
12206 /* All class-names start with an identifier. */
12207 token = cp_lexer_peek_token (parser->lexer);
12208 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12210 cp_parser_error (parser, "expected class-name");
12211 return error_mark_node;
12214 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12215 to a template-id, so we save it here. */
12216 scope = parser->scope;
12217 if (scope == error_mark_node)
12218 return error_mark_node;
12220 /* Any name names a type if we're following the `typename' keyword
12221 in a qualified name where the enclosing scope is type-dependent. */
12222 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12223 && dependent_type_p (scope));
12224 /* Handle the common case (an identifier, but not a template-id)
12226 if (token->type == CPP_NAME
12227 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12231 /* Look for the identifier. */
12232 identifier = cp_parser_identifier (parser);
12233 /* If the next token isn't an identifier, we are certainly not
12234 looking at a class-name. */
12235 if (identifier == error_mark_node)
12236 decl = error_mark_node;
12237 /* If we know this is a type-name, there's no need to look it
12239 else if (typename_p)
12243 /* If the next token is a `::', then the name must be a type
12246 [basic.lookup.qual]
12248 During the lookup for a name preceding the :: scope
12249 resolution operator, object, function, and enumerator
12250 names are ignored. */
12251 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12253 /* Look up the name. */
12254 decl = cp_parser_lookup_name (parser, identifier,
12256 /*is_template=*/false,
12257 /*is_namespace=*/false,
12258 check_dependency_p);
12263 /* Try a template-id. */
12264 decl = cp_parser_template_id (parser, template_keyword_p,
12265 check_dependency_p,
12267 if (decl == error_mark_node)
12268 return error_mark_node;
12271 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12273 /* If this is a typename, create a TYPENAME_TYPE. */
12274 if (typename_p && decl != error_mark_node)
12276 decl = make_typename_type (scope, decl, /*complain=*/1);
12277 if (decl != error_mark_node)
12278 decl = TYPE_NAME (decl);
12281 /* Check to see that it is really the name of a class. */
12282 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12283 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12284 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12285 /* Situations like this:
12287 template <typename T> struct A {
12288 typename T::template X<int>::I i;
12291 are problematic. Is `T::template X<int>' a class-name? The
12292 standard does not seem to be definitive, but there is no other
12293 valid interpretation of the following `::'. Therefore, those
12294 names are considered class-names. */
12295 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12296 else if (decl == error_mark_node
12297 || TREE_CODE (decl) != TYPE_DECL
12298 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12300 cp_parser_error (parser, "expected class-name");
12301 return error_mark_node;
12307 /* Parse a class-specifier.
12310 class-head { member-specification [opt] }
12312 Returns the TREE_TYPE representing the class. */
12315 cp_parser_class_specifier (cp_parser* parser)
12319 tree attributes = NULL_TREE;
12320 int has_trailing_semicolon;
12321 bool nested_name_specifier_p;
12322 unsigned saved_num_template_parameter_lists;
12323 bool pop_p = false;
12325 push_deferring_access_checks (dk_no_deferred);
12327 /* Parse the class-head. */
12328 type = cp_parser_class_head (parser,
12329 &nested_name_specifier_p,
12331 /* If the class-head was a semantic disaster, skip the entire body
12335 cp_parser_skip_to_end_of_block_or_statement (parser);
12336 pop_deferring_access_checks ();
12337 return error_mark_node;
12340 /* Look for the `{'. */
12341 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12343 pop_deferring_access_checks ();
12344 return error_mark_node;
12347 /* Issue an error message if type-definitions are forbidden here. */
12348 cp_parser_check_type_definition (parser);
12349 /* Remember that we are defining one more class. */
12350 ++parser->num_classes_being_defined;
12351 /* Inside the class, surrounding template-parameter-lists do not
12353 saved_num_template_parameter_lists
12354 = parser->num_template_parameter_lists;
12355 parser->num_template_parameter_lists = 0;
12357 /* Start the class. */
12358 if (nested_name_specifier_p)
12359 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
12360 type = begin_class_definition (type);
12361 if (type == error_mark_node)
12362 /* If the type is erroneous, skip the entire body of the class. */
12363 cp_parser_skip_to_closing_brace (parser);
12365 /* Parse the member-specification. */
12366 cp_parser_member_specification_opt (parser);
12367 /* Look for the trailing `}'. */
12368 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12369 /* We get better error messages by noticing a common problem: a
12370 missing trailing `;'. */
12371 token = cp_lexer_peek_token (parser->lexer);
12372 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12373 /* Look for trailing attributes to apply to this class. */
12374 if (cp_parser_allow_gnu_extensions_p (parser))
12376 tree sub_attr = cp_parser_attributes_opt (parser);
12377 attributes = chainon (attributes, sub_attr);
12379 if (type != error_mark_node)
12380 type = finish_struct (type, attributes);
12382 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
12383 /* If this class is not itself within the scope of another class,
12384 then we need to parse the bodies of all of the queued function
12385 definitions. Note that the queued functions defined in a class
12386 are not always processed immediately following the
12387 class-specifier for that class. Consider:
12390 struct B { void f() { sizeof (A); } };
12393 If `f' were processed before the processing of `A' were
12394 completed, there would be no way to compute the size of `A'.
12395 Note that the nesting we are interested in here is lexical --
12396 not the semantic nesting given by TYPE_CONTEXT. In particular,
12399 struct A { struct B; };
12400 struct A::B { void f() { } };
12402 there is no need to delay the parsing of `A::B::f'. */
12403 if (--parser->num_classes_being_defined == 0)
12408 /* In a first pass, parse default arguments to the functions.
12409 Then, in a second pass, parse the bodies of the functions.
12410 This two-phased approach handles cases like:
12418 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12419 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12420 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12421 TREE_PURPOSE (parser->unparsed_functions_queues)
12422 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12424 fn = TREE_VALUE (queue_entry);
12425 /* Make sure that any template parameters are in scope. */
12426 maybe_begin_member_template_processing (fn);
12427 /* If there are default arguments that have not yet been processed,
12428 take care of them now. */
12429 cp_parser_late_parsing_default_args (parser, fn);
12430 /* Remove any template parameters from the symbol table. */
12431 maybe_end_member_template_processing ();
12433 /* Now parse the body of the functions. */
12434 for (TREE_VALUE (parser->unparsed_functions_queues)
12435 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12436 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12437 TREE_VALUE (parser->unparsed_functions_queues)
12438 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12440 /* Figure out which function we need to process. */
12441 fn = TREE_VALUE (queue_entry);
12443 /* A hack to prevent garbage collection. */
12446 /* Parse the function. */
12447 cp_parser_late_parsing_for_member (parser, fn);
12453 /* Put back any saved access checks. */
12454 pop_deferring_access_checks ();
12456 /* Restore the count of active template-parameter-lists. */
12457 parser->num_template_parameter_lists
12458 = saved_num_template_parameter_lists;
12463 /* Parse a class-head.
12466 class-key identifier [opt] base-clause [opt]
12467 class-key nested-name-specifier identifier base-clause [opt]
12468 class-key nested-name-specifier [opt] template-id
12472 class-key attributes identifier [opt] base-clause [opt]
12473 class-key attributes nested-name-specifier identifier base-clause [opt]
12474 class-key attributes nested-name-specifier [opt] template-id
12477 Returns the TYPE of the indicated class. Sets
12478 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12479 involving a nested-name-specifier was used, and FALSE otherwise.
12481 Returns NULL_TREE if the class-head is syntactically valid, but
12482 semantically invalid in a way that means we should skip the entire
12483 body of the class. */
12486 cp_parser_class_head (cp_parser* parser,
12487 bool* nested_name_specifier_p,
12488 tree *attributes_p)
12491 tree nested_name_specifier;
12492 enum tag_types class_key;
12493 tree id = NULL_TREE;
12494 tree type = NULL_TREE;
12496 bool template_id_p = false;
12497 bool qualified_p = false;
12498 bool invalid_nested_name_p = false;
12499 bool invalid_explicit_specialization_p = false;
12500 bool pop_p = false;
12501 unsigned num_templates;
12503 /* Assume no nested-name-specifier will be present. */
12504 *nested_name_specifier_p = false;
12505 /* Assume no template parameter lists will be used in defining the
12509 /* Look for the class-key. */
12510 class_key = cp_parser_class_key (parser);
12511 if (class_key == none_type)
12512 return error_mark_node;
12514 /* Parse the attributes. */
12515 attributes = cp_parser_attributes_opt (parser);
12517 /* If the next token is `::', that is invalid -- but sometimes
12518 people do try to write:
12522 Handle this gracefully by accepting the extra qualifier, and then
12523 issuing an error about it later if this really is a
12524 class-head. If it turns out just to be an elaborated type
12525 specifier, remain silent. */
12526 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12527 qualified_p = true;
12529 push_deferring_access_checks (dk_no_check);
12531 /* Determine the name of the class. Begin by looking for an
12532 optional nested-name-specifier. */
12533 nested_name_specifier
12534 = cp_parser_nested_name_specifier_opt (parser,
12535 /*typename_keyword_p=*/false,
12536 /*check_dependency_p=*/false,
12538 /*is_declaration=*/false);
12539 /* If there was a nested-name-specifier, then there *must* be an
12541 if (nested_name_specifier)
12543 /* Although the grammar says `identifier', it really means
12544 `class-name' or `template-name'. You are only allowed to
12545 define a class that has already been declared with this
12548 The proposed resolution for Core Issue 180 says that whever
12549 you see `class T::X' you should treat `X' as a type-name.
12551 It is OK to define an inaccessible class; for example:
12553 class A { class B; };
12556 We do not know if we will see a class-name, or a
12557 template-name. We look for a class-name first, in case the
12558 class-name is a template-id; if we looked for the
12559 template-name first we would stop after the template-name. */
12560 cp_parser_parse_tentatively (parser);
12561 type = cp_parser_class_name (parser,
12562 /*typename_keyword_p=*/false,
12563 /*template_keyword_p=*/false,
12565 /*check_dependency_p=*/false,
12566 /*class_head_p=*/true,
12567 /*is_declaration=*/false);
12568 /* If that didn't work, ignore the nested-name-specifier. */
12569 if (!cp_parser_parse_definitely (parser))
12571 invalid_nested_name_p = true;
12572 id = cp_parser_identifier (parser);
12573 if (id == error_mark_node)
12576 /* If we could not find a corresponding TYPE, treat this
12577 declaration like an unqualified declaration. */
12578 if (type == error_mark_node)
12579 nested_name_specifier = NULL_TREE;
12580 /* Otherwise, count the number of templates used in TYPE and its
12581 containing scopes. */
12586 for (scope = TREE_TYPE (type);
12587 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12588 scope = (TYPE_P (scope)
12589 ? TYPE_CONTEXT (scope)
12590 : DECL_CONTEXT (scope)))
12592 && CLASS_TYPE_P (scope)
12593 && CLASSTYPE_TEMPLATE_INFO (scope)
12594 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12595 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12599 /* Otherwise, the identifier is optional. */
12602 /* We don't know whether what comes next is a template-id,
12603 an identifier, or nothing at all. */
12604 cp_parser_parse_tentatively (parser);
12605 /* Check for a template-id. */
12606 id = cp_parser_template_id (parser,
12607 /*template_keyword_p=*/false,
12608 /*check_dependency_p=*/true,
12609 /*is_declaration=*/true);
12610 /* If that didn't work, it could still be an identifier. */
12611 if (!cp_parser_parse_definitely (parser))
12613 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12614 id = cp_parser_identifier (parser);
12620 template_id_p = true;
12625 pop_deferring_access_checks ();
12628 cp_parser_check_for_invalid_template_id (parser, id);
12630 /* If it's not a `:' or a `{' then we can't really be looking at a
12631 class-head, since a class-head only appears as part of a
12632 class-specifier. We have to detect this situation before calling
12633 xref_tag, since that has irreversible side-effects. */
12634 if (!cp_parser_next_token_starts_class_definition_p (parser))
12636 cp_parser_error (parser, "expected `{' or `:'");
12637 return error_mark_node;
12640 /* At this point, we're going ahead with the class-specifier, even
12641 if some other problem occurs. */
12642 cp_parser_commit_to_tentative_parse (parser);
12643 /* Issue the error about the overly-qualified name now. */
12645 cp_parser_error (parser,
12646 "global qualification of class name is invalid");
12647 else if (invalid_nested_name_p)
12648 cp_parser_error (parser,
12649 "qualified name does not name a class");
12650 else if (nested_name_specifier)
12653 /* Figure out in what scope the declaration is being placed. */
12654 scope = current_scope ();
12656 scope = current_namespace;
12657 /* If that scope does not contain the scope in which the
12658 class was originally declared, the program is invalid. */
12659 if (scope && !is_ancestor (scope, nested_name_specifier))
12661 error ("declaration of `%D' in `%D' which does not "
12662 "enclose `%D'", type, scope, nested_name_specifier);
12668 A declarator-id shall not be qualified exception of the
12669 definition of a ... nested class outside of its class
12670 ... [or] a the definition or explicit instantiation of a
12671 class member of a namespace outside of its namespace. */
12672 if (scope == nested_name_specifier)
12674 pedwarn ("extra qualification ignored");
12675 nested_name_specifier = NULL_TREE;
12679 /* An explicit-specialization must be preceded by "template <>". If
12680 it is not, try to recover gracefully. */
12681 if (at_namespace_scope_p ()
12682 && parser->num_template_parameter_lists == 0
12685 error ("an explicit specialization must be preceded by 'template <>'");
12686 invalid_explicit_specialization_p = true;
12687 /* Take the same action that would have been taken by
12688 cp_parser_explicit_specialization. */
12689 ++parser->num_template_parameter_lists;
12690 begin_specialization ();
12692 /* There must be no "return" statements between this point and the
12693 end of this function; set "type "to the correct return value and
12694 use "goto done;" to return. */
12695 /* Make sure that the right number of template parameters were
12697 if (!cp_parser_check_template_parameters (parser, num_templates))
12699 /* If something went wrong, there is no point in even trying to
12700 process the class-definition. */
12705 /* Look up the type. */
12708 type = TREE_TYPE (id);
12709 maybe_process_partial_specialization (type);
12711 else if (!nested_name_specifier)
12713 /* If the class was unnamed, create a dummy name. */
12715 id = make_anon_name ();
12716 type = xref_tag (class_key, id, /*globalize=*/false,
12717 parser->num_template_parameter_lists);
12722 bool pop_p = false;
12726 template <typename T> struct S { struct T };
12727 template <typename T> struct S<T>::T { };
12729 we will get a TYPENAME_TYPE when processing the definition of
12730 `S::T'. We need to resolve it to the actual type before we
12731 try to define it. */
12732 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12734 class_type = resolve_typename_type (TREE_TYPE (type),
12735 /*only_current_p=*/false);
12736 if (class_type != error_mark_node)
12737 type = TYPE_NAME (class_type);
12740 cp_parser_error (parser, "could not resolve typename type");
12741 type = error_mark_node;
12745 maybe_process_partial_specialization (TREE_TYPE (type));
12746 class_type = current_class_type;
12747 /* Enter the scope indicated by the nested-name-specifier. */
12748 if (nested_name_specifier)
12749 pop_p = push_scope (nested_name_specifier);
12750 /* Get the canonical version of this type. */
12751 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12752 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12753 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12754 type = push_template_decl (type);
12755 type = TREE_TYPE (type);
12756 if (nested_name_specifier)
12758 *nested_name_specifier_p = true;
12760 pop_scope (nested_name_specifier);
12763 /* Indicate whether this class was declared as a `class' or as a
12765 if (TREE_CODE (type) == RECORD_TYPE)
12766 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12767 cp_parser_check_class_key (class_key, type);
12769 /* Enter the scope containing the class; the names of base classes
12770 should be looked up in that context. For example, given:
12772 struct A { struct B {}; struct C; };
12773 struct A::C : B {};
12776 if (nested_name_specifier)
12777 pop_p = push_scope (nested_name_specifier);
12778 /* Now, look for the base-clause. */
12779 token = cp_lexer_peek_token (parser->lexer);
12780 if (token->type == CPP_COLON)
12784 /* Get the list of base-classes. */
12785 bases = cp_parser_base_clause (parser);
12786 /* Process them. */
12787 xref_basetypes (type, bases);
12789 /* Leave the scope given by the nested-name-specifier. We will
12790 enter the class scope itself while processing the members. */
12792 pop_scope (nested_name_specifier);
12795 if (invalid_explicit_specialization_p)
12797 end_specialization ();
12798 --parser->num_template_parameter_lists;
12800 *attributes_p = attributes;
12804 /* Parse a class-key.
12811 Returns the kind of class-key specified, or none_type to indicate
12814 static enum tag_types
12815 cp_parser_class_key (cp_parser* parser)
12818 enum tag_types tag_type;
12820 /* Look for the class-key. */
12821 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12825 /* Check to see if the TOKEN is a class-key. */
12826 tag_type = cp_parser_token_is_class_key (token);
12828 cp_parser_error (parser, "expected class-key");
12832 /* Parse an (optional) member-specification.
12834 member-specification:
12835 member-declaration member-specification [opt]
12836 access-specifier : member-specification [opt] */
12839 cp_parser_member_specification_opt (cp_parser* parser)
12846 /* Peek at the next token. */
12847 token = cp_lexer_peek_token (parser->lexer);
12848 /* If it's a `}', or EOF then we've seen all the members. */
12849 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12852 /* See if this token is a keyword. */
12853 keyword = token->keyword;
12857 case RID_PROTECTED:
12859 /* Consume the access-specifier. */
12860 cp_lexer_consume_token (parser->lexer);
12861 /* Remember which access-specifier is active. */
12862 current_access_specifier = token->value;
12863 /* Look for the `:'. */
12864 cp_parser_require (parser, CPP_COLON, "`:'");
12868 /* Otherwise, the next construction must be a
12869 member-declaration. */
12870 cp_parser_member_declaration (parser);
12875 /* Parse a member-declaration.
12877 member-declaration:
12878 decl-specifier-seq [opt] member-declarator-list [opt] ;
12879 function-definition ; [opt]
12880 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12882 template-declaration
12884 member-declarator-list:
12886 member-declarator-list , member-declarator
12889 declarator pure-specifier [opt]
12890 declarator constant-initializer [opt]
12891 identifier [opt] : constant-expression
12895 member-declaration:
12896 __extension__ member-declaration
12899 declarator attributes [opt] pure-specifier [opt]
12900 declarator attributes [opt] constant-initializer [opt]
12901 identifier [opt] attributes [opt] : constant-expression */
12904 cp_parser_member_declaration (cp_parser* parser)
12906 cp_decl_specifier_seq decl_specifiers;
12907 tree prefix_attributes;
12909 int declares_class_or_enum;
12912 int saved_pedantic;
12914 /* Check for the `__extension__' keyword. */
12915 if (cp_parser_extension_opt (parser, &saved_pedantic))
12918 cp_parser_member_declaration (parser);
12919 /* Restore the old value of the PEDANTIC flag. */
12920 pedantic = saved_pedantic;
12925 /* Check for a template-declaration. */
12926 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12928 /* Parse the template-declaration. */
12929 cp_parser_template_declaration (parser, /*member_p=*/true);
12934 /* Check for a using-declaration. */
12935 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12937 /* Parse the using-declaration. */
12938 cp_parser_using_declaration (parser);
12943 /* Parse the decl-specifier-seq. */
12944 cp_parser_decl_specifier_seq (parser,
12945 CP_PARSER_FLAGS_OPTIONAL,
12947 &declares_class_or_enum);
12948 prefix_attributes = decl_specifiers.attributes;
12949 decl_specifiers.attributes = NULL_TREE;
12950 /* Check for an invalid type-name. */
12951 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12953 /* If there is no declarator, then the decl-specifier-seq should
12955 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12957 /* If there was no decl-specifier-seq, and the next token is a
12958 `;', then we have something like:
12964 Each member-declaration shall declare at least one member
12965 name of the class. */
12966 if (!decl_specifiers.any_specifiers_p)
12969 pedwarn ("extra semicolon");
12975 /* See if this declaration is a friend. */
12976 friend_p = cp_parser_friend_p (&decl_specifiers);
12977 /* If there were decl-specifiers, check to see if there was
12978 a class-declaration. */
12979 type = check_tag_decl (&decl_specifiers);
12980 /* Nested classes have already been added to the class, but
12981 a `friend' needs to be explicitly registered. */
12984 /* If the `friend' keyword was present, the friend must
12985 be introduced with a class-key. */
12986 if (!declares_class_or_enum)
12987 error ("a class-key must be used when declaring a friend");
12990 template <typename T> struct A {
12991 friend struct A<T>::B;
12994 A<T>::B will be represented by a TYPENAME_TYPE, and
12995 therefore not recognized by check_tag_decl. */
12997 && decl_specifiers.type
12998 && TYPE_P (decl_specifiers.type))
12999 type = decl_specifiers.type;
13000 if (!type || !TYPE_P (type))
13001 error ("friend declaration does not name a class or "
13004 make_friend_class (current_class_type, type,
13005 /*complain=*/true);
13007 /* If there is no TYPE, an error message will already have
13009 else if (!type || type == error_mark_node)
13011 /* An anonymous aggregate has to be handled specially; such
13012 a declaration really declares a data member (with a
13013 particular type), as opposed to a nested class. */
13014 else if (ANON_AGGR_TYPE_P (type))
13016 /* Remove constructors and such from TYPE, now that we
13017 know it is an anonymous aggregate. */
13018 fixup_anonymous_aggr (type);
13019 /* And make the corresponding data member. */
13020 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13021 /* Add it to the class. */
13022 finish_member_declaration (decl);
13025 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13030 /* See if these declarations will be friends. */
13031 friend_p = cp_parser_friend_p (&decl_specifiers);
13033 /* Keep going until we hit the `;' at the end of the
13035 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13037 tree attributes = NULL_TREE;
13038 tree first_attribute;
13040 /* Peek at the next token. */
13041 token = cp_lexer_peek_token (parser->lexer);
13043 /* Check for a bitfield declaration. */
13044 if (token->type == CPP_COLON
13045 || (token->type == CPP_NAME
13046 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13052 /* Get the name of the bitfield. Note that we cannot just
13053 check TOKEN here because it may have been invalidated by
13054 the call to cp_lexer_peek_nth_token above. */
13055 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13056 identifier = cp_parser_identifier (parser);
13058 identifier = NULL_TREE;
13060 /* Consume the `:' token. */
13061 cp_lexer_consume_token (parser->lexer);
13062 /* Get the width of the bitfield. */
13064 = cp_parser_constant_expression (parser,
13065 /*allow_non_constant=*/false,
13068 /* Look for attributes that apply to the bitfield. */
13069 attributes = cp_parser_attributes_opt (parser);
13070 /* Remember which attributes are prefix attributes and
13072 first_attribute = attributes;
13073 /* Combine the attributes. */
13074 attributes = chainon (prefix_attributes, attributes);
13076 /* Create the bitfield declaration. */
13077 decl = grokbitfield (identifier
13078 ? make_id_declarator (identifier)
13082 /* Apply the attributes. */
13083 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13087 cp_declarator *declarator;
13089 tree asm_specification;
13090 int ctor_dtor_or_conv_p;
13092 /* Parse the declarator. */
13094 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13095 &ctor_dtor_or_conv_p,
13096 /*parenthesized_p=*/NULL);
13098 /* If something went wrong parsing the declarator, make sure
13099 that we at least consume some tokens. */
13100 if (declarator == cp_error_declarator)
13102 /* Skip to the end of the statement. */
13103 cp_parser_skip_to_end_of_statement (parser);
13104 /* If the next token is not a semicolon, that is
13105 probably because we just skipped over the body of
13106 a function. So, we consume a semicolon if
13107 present, but do not issue an error message if it
13109 if (cp_lexer_next_token_is (parser->lexer,
13111 cp_lexer_consume_token (parser->lexer);
13115 cp_parser_check_for_definition_in_return_type
13116 (declarator, declares_class_or_enum);
13118 /* Look for an asm-specification. */
13119 asm_specification = cp_parser_asm_specification_opt (parser);
13120 /* Look for attributes that apply to the declaration. */
13121 attributes = cp_parser_attributes_opt (parser);
13122 /* Remember which attributes are prefix attributes and
13124 first_attribute = attributes;
13125 /* Combine the attributes. */
13126 attributes = chainon (prefix_attributes, attributes);
13128 /* If it's an `=', then we have a constant-initializer or a
13129 pure-specifier. It is not correct to parse the
13130 initializer before registering the member declaration
13131 since the member declaration should be in scope while
13132 its initializer is processed. However, the rest of the
13133 front end does not yet provide an interface that allows
13134 us to handle this correctly. */
13135 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13139 A pure-specifier shall be used only in the declaration of
13140 a virtual function.
13142 A member-declarator can contain a constant-initializer
13143 only if it declares a static member of integral or
13146 Therefore, if the DECLARATOR is for a function, we look
13147 for a pure-specifier; otherwise, we look for a
13148 constant-initializer. When we call `grokfield', it will
13149 perform more stringent semantics checks. */
13150 if (declarator->kind == cdk_function)
13151 initializer = cp_parser_pure_specifier (parser);
13153 /* Parse the initializer. */
13154 initializer = cp_parser_constant_initializer (parser);
13156 /* Otherwise, there is no initializer. */
13158 initializer = NULL_TREE;
13160 /* See if we are probably looking at a function
13161 definition. We are certainly not looking at at a
13162 member-declarator. Calling `grokfield' has
13163 side-effects, so we must not do it unless we are sure
13164 that we are looking at a member-declarator. */
13165 if (cp_parser_token_starts_function_definition_p
13166 (cp_lexer_peek_token (parser->lexer)))
13168 /* The grammar does not allow a pure-specifier to be
13169 used when a member function is defined. (It is
13170 possible that this fact is an oversight in the
13171 standard, since a pure function may be defined
13172 outside of the class-specifier. */
13174 error ("pure-specifier on function-definition");
13175 decl = cp_parser_save_member_function_body (parser,
13179 /* If the member was not a friend, declare it here. */
13181 finish_member_declaration (decl);
13182 /* Peek at the next token. */
13183 token = cp_lexer_peek_token (parser->lexer);
13184 /* If the next token is a semicolon, consume it. */
13185 if (token->type == CPP_SEMICOLON)
13186 cp_lexer_consume_token (parser->lexer);
13191 /* Create the declaration. */
13192 decl = grokfield (declarator, &decl_specifiers,
13193 initializer, asm_specification,
13195 /* Any initialization must have been from a
13196 constant-expression. */
13197 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13198 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13202 /* Reset PREFIX_ATTRIBUTES. */
13203 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13204 attributes = TREE_CHAIN (attributes);
13206 TREE_CHAIN (attributes) = NULL_TREE;
13208 /* If there is any qualification still in effect, clear it
13209 now; we will be starting fresh with the next declarator. */
13210 parser->scope = NULL_TREE;
13211 parser->qualifying_scope = NULL_TREE;
13212 parser->object_scope = NULL_TREE;
13213 /* If it's a `,', then there are more declarators. */
13214 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13215 cp_lexer_consume_token (parser->lexer);
13216 /* If the next token isn't a `;', then we have a parse error. */
13217 else if (cp_lexer_next_token_is_not (parser->lexer,
13220 cp_parser_error (parser, "expected `;'");
13221 /* Skip tokens until we find a `;'. */
13222 cp_parser_skip_to_end_of_statement (parser);
13229 /* Add DECL to the list of members. */
13231 finish_member_declaration (decl);
13233 if (TREE_CODE (decl) == FUNCTION_DECL)
13234 cp_parser_save_default_args (parser, decl);
13239 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13242 /* Parse a pure-specifier.
13247 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13248 Otherwise, ERROR_MARK_NODE is returned. */
13251 cp_parser_pure_specifier (cp_parser* parser)
13255 /* Look for the `=' token. */
13256 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13257 return error_mark_node;
13258 /* Look for the `0' token. */
13259 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13260 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13261 to get information from the lexer about how the number was
13262 spelled in order to fix this problem. */
13263 if (!token || !integer_zerop (token->value))
13264 return error_mark_node;
13266 return integer_zero_node;
13269 /* Parse a constant-initializer.
13271 constant-initializer:
13272 = constant-expression
13274 Returns a representation of the constant-expression. */
13277 cp_parser_constant_initializer (cp_parser* parser)
13279 /* Look for the `=' token. */
13280 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13281 return error_mark_node;
13283 /* It is invalid to write:
13285 struct S { static const int i = { 7 }; };
13288 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13290 cp_parser_error (parser,
13291 "a brace-enclosed initializer is not allowed here");
13292 /* Consume the opening brace. */
13293 cp_lexer_consume_token (parser->lexer);
13294 /* Skip the initializer. */
13295 cp_parser_skip_to_closing_brace (parser);
13296 /* Look for the trailing `}'. */
13297 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13299 return error_mark_node;
13302 return cp_parser_constant_expression (parser,
13303 /*allow_non_constant=*/false,
13307 /* Derived classes [gram.class.derived] */
13309 /* Parse a base-clause.
13312 : base-specifier-list
13314 base-specifier-list:
13316 base-specifier-list , base-specifier
13318 Returns a TREE_LIST representing the base-classes, in the order in
13319 which they were declared. The representation of each node is as
13320 described by cp_parser_base_specifier.
13322 In the case that no bases are specified, this function will return
13323 NULL_TREE, not ERROR_MARK_NODE. */
13326 cp_parser_base_clause (cp_parser* parser)
13328 tree bases = NULL_TREE;
13330 /* Look for the `:' that begins the list. */
13331 cp_parser_require (parser, CPP_COLON, "`:'");
13333 /* Scan the base-specifier-list. */
13339 /* Look for the base-specifier. */
13340 base = cp_parser_base_specifier (parser);
13341 /* Add BASE to the front of the list. */
13342 if (base != error_mark_node)
13344 TREE_CHAIN (base) = bases;
13347 /* Peek at the next token. */
13348 token = cp_lexer_peek_token (parser->lexer);
13349 /* If it's not a comma, then the list is complete. */
13350 if (token->type != CPP_COMMA)
13352 /* Consume the `,'. */
13353 cp_lexer_consume_token (parser->lexer);
13356 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13357 base class had a qualified name. However, the next name that
13358 appears is certainly not qualified. */
13359 parser->scope = NULL_TREE;
13360 parser->qualifying_scope = NULL_TREE;
13361 parser->object_scope = NULL_TREE;
13363 return nreverse (bases);
13366 /* Parse a base-specifier.
13369 :: [opt] nested-name-specifier [opt] class-name
13370 virtual access-specifier [opt] :: [opt] nested-name-specifier
13372 access-specifier virtual [opt] :: [opt] nested-name-specifier
13375 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13376 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13377 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13378 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13381 cp_parser_base_specifier (cp_parser* parser)
13385 bool virtual_p = false;
13386 bool duplicate_virtual_error_issued_p = false;
13387 bool duplicate_access_error_issued_p = false;
13388 bool class_scope_p, template_p;
13389 tree access = access_default_node;
13392 /* Process the optional `virtual' and `access-specifier'. */
13395 /* Peek at the next token. */
13396 token = cp_lexer_peek_token (parser->lexer);
13397 /* Process `virtual'. */
13398 switch (token->keyword)
13401 /* If `virtual' appears more than once, issue an error. */
13402 if (virtual_p && !duplicate_virtual_error_issued_p)
13404 cp_parser_error (parser,
13405 "`virtual' specified more than once in base-specified");
13406 duplicate_virtual_error_issued_p = true;
13411 /* Consume the `virtual' token. */
13412 cp_lexer_consume_token (parser->lexer);
13417 case RID_PROTECTED:
13419 /* If more than one access specifier appears, issue an
13421 if (access != access_default_node
13422 && !duplicate_access_error_issued_p)
13424 cp_parser_error (parser,
13425 "more than one access specifier in base-specified");
13426 duplicate_access_error_issued_p = true;
13429 access = ridpointers[(int) token->keyword];
13431 /* Consume the access-specifier. */
13432 cp_lexer_consume_token (parser->lexer);
13441 /* It is not uncommon to see programs mechanically, erroneously, use
13442 the 'typename' keyword to denote (dependent) qualified types
13443 as base classes. */
13444 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13446 if (!processing_template_decl)
13447 error ("keyword `typename' not allowed outside of templates");
13449 error ("keyword `typename' not allowed in this context "
13450 "(the base class is implicitly a type)");
13451 cp_lexer_consume_token (parser->lexer);
13454 /* Look for the optional `::' operator. */
13455 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13456 /* Look for the nested-name-specifier. The simplest way to
13461 The keyword `typename' is not permitted in a base-specifier or
13462 mem-initializer; in these contexts a qualified name that
13463 depends on a template-parameter is implicitly assumed to be a
13466 is to pretend that we have seen the `typename' keyword at this
13468 cp_parser_nested_name_specifier_opt (parser,
13469 /*typename_keyword_p=*/true,
13470 /*check_dependency_p=*/true,
13472 /*is_declaration=*/true);
13473 /* If the base class is given by a qualified name, assume that names
13474 we see are type names or templates, as appropriate. */
13475 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13476 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13478 /* Finally, look for the class-name. */
13479 type = cp_parser_class_name (parser,
13483 /*check_dependency_p=*/true,
13484 /*class_head_p=*/false,
13485 /*is_declaration=*/true);
13487 if (type == error_mark_node)
13488 return error_mark_node;
13490 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13493 /* Exception handling [gram.exception] */
13495 /* Parse an (optional) exception-specification.
13497 exception-specification:
13498 throw ( type-id-list [opt] )
13500 Returns a TREE_LIST representing the exception-specification. The
13501 TREE_VALUE of each node is a type. */
13504 cp_parser_exception_specification_opt (cp_parser* parser)
13509 /* Peek at the next token. */
13510 token = cp_lexer_peek_token (parser->lexer);
13511 /* If it's not `throw', then there's no exception-specification. */
13512 if (!cp_parser_is_keyword (token, RID_THROW))
13515 /* Consume the `throw'. */
13516 cp_lexer_consume_token (parser->lexer);
13518 /* Look for the `('. */
13519 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13521 /* Peek at the next token. */
13522 token = cp_lexer_peek_token (parser->lexer);
13523 /* If it's not a `)', then there is a type-id-list. */
13524 if (token->type != CPP_CLOSE_PAREN)
13526 const char *saved_message;
13528 /* Types may not be defined in an exception-specification. */
13529 saved_message = parser->type_definition_forbidden_message;
13530 parser->type_definition_forbidden_message
13531 = "types may not be defined in an exception-specification";
13532 /* Parse the type-id-list. */
13533 type_id_list = cp_parser_type_id_list (parser);
13534 /* Restore the saved message. */
13535 parser->type_definition_forbidden_message = saved_message;
13538 type_id_list = empty_except_spec;
13540 /* Look for the `)'. */
13541 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13543 return type_id_list;
13546 /* Parse an (optional) type-id-list.
13550 type-id-list , type-id
13552 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13553 in the order that the types were presented. */
13556 cp_parser_type_id_list (cp_parser* parser)
13558 tree types = NULL_TREE;
13565 /* Get the next type-id. */
13566 type = cp_parser_type_id (parser);
13567 /* Add it to the list. */
13568 types = add_exception_specifier (types, type, /*complain=*/1);
13569 /* Peek at the next token. */
13570 token = cp_lexer_peek_token (parser->lexer);
13571 /* If it is not a `,', we are done. */
13572 if (token->type != CPP_COMMA)
13574 /* Consume the `,'. */
13575 cp_lexer_consume_token (parser->lexer);
13578 return nreverse (types);
13581 /* Parse a try-block.
13584 try compound-statement handler-seq */
13587 cp_parser_try_block (cp_parser* parser)
13591 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13592 try_block = begin_try_block ();
13593 cp_parser_compound_statement (parser, NULL, true);
13594 finish_try_block (try_block);
13595 cp_parser_handler_seq (parser);
13596 finish_handler_sequence (try_block);
13601 /* Parse a function-try-block.
13603 function-try-block:
13604 try ctor-initializer [opt] function-body handler-seq */
13607 cp_parser_function_try_block (cp_parser* parser)
13610 bool ctor_initializer_p;
13612 /* Look for the `try' keyword. */
13613 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13615 /* Let the rest of the front-end know where we are. */
13616 try_block = begin_function_try_block ();
13617 /* Parse the function-body. */
13619 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13620 /* We're done with the `try' part. */
13621 finish_function_try_block (try_block);
13622 /* Parse the handlers. */
13623 cp_parser_handler_seq (parser);
13624 /* We're done with the handlers. */
13625 finish_function_handler_sequence (try_block);
13627 return ctor_initializer_p;
13630 /* Parse a handler-seq.
13633 handler handler-seq [opt] */
13636 cp_parser_handler_seq (cp_parser* parser)
13642 /* Parse the handler. */
13643 cp_parser_handler (parser);
13644 /* Peek at the next token. */
13645 token = cp_lexer_peek_token (parser->lexer);
13646 /* If it's not `catch' then there are no more handlers. */
13647 if (!cp_parser_is_keyword (token, RID_CATCH))
13652 /* Parse a handler.
13655 catch ( exception-declaration ) compound-statement */
13658 cp_parser_handler (cp_parser* parser)
13663 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13664 handler = begin_handler ();
13665 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13666 declaration = cp_parser_exception_declaration (parser);
13667 finish_handler_parms (declaration, handler);
13668 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13669 cp_parser_compound_statement (parser, NULL, false);
13670 finish_handler (handler);
13673 /* Parse an exception-declaration.
13675 exception-declaration:
13676 type-specifier-seq declarator
13677 type-specifier-seq abstract-declarator
13681 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13682 ellipsis variant is used. */
13685 cp_parser_exception_declaration (cp_parser* parser)
13688 cp_decl_specifier_seq type_specifiers;
13689 cp_declarator *declarator;
13690 const char *saved_message;
13692 /* If it's an ellipsis, it's easy to handle. */
13693 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13695 /* Consume the `...' token. */
13696 cp_lexer_consume_token (parser->lexer);
13700 /* Types may not be defined in exception-declarations. */
13701 saved_message = parser->type_definition_forbidden_message;
13702 parser->type_definition_forbidden_message
13703 = "types may not be defined in exception-declarations";
13705 /* Parse the type-specifier-seq. */
13706 cp_parser_type_specifier_seq (parser, &type_specifiers);
13707 /* If it's a `)', then there is no declarator. */
13708 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13711 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13712 /*ctor_dtor_or_conv_p=*/NULL,
13713 /*parenthesized_p=*/NULL);
13715 /* Restore the saved message. */
13716 parser->type_definition_forbidden_message = saved_message;
13718 if (type_specifiers.any_specifiers_p)
13720 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13721 if (decl == NULL_TREE)
13722 error ("invalid catch parameter");
13730 /* Parse a throw-expression.
13733 throw assignment-expression [opt]
13735 Returns a THROW_EXPR representing the throw-expression. */
13738 cp_parser_throw_expression (cp_parser* parser)
13743 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13744 token = cp_lexer_peek_token (parser->lexer);
13745 /* Figure out whether or not there is an assignment-expression
13746 following the "throw" keyword. */
13747 if (token->type == CPP_COMMA
13748 || token->type == CPP_SEMICOLON
13749 || token->type == CPP_CLOSE_PAREN
13750 || token->type == CPP_CLOSE_SQUARE
13751 || token->type == CPP_CLOSE_BRACE
13752 || token->type == CPP_COLON)
13753 expression = NULL_TREE;
13755 expression = cp_parser_assignment_expression (parser);
13757 return build_throw (expression);
13760 /* GNU Extensions */
13762 /* Parse an (optional) asm-specification.
13765 asm ( string-literal )
13767 If the asm-specification is present, returns a STRING_CST
13768 corresponding to the string-literal. Otherwise, returns
13772 cp_parser_asm_specification_opt (cp_parser* parser)
13775 tree asm_specification;
13777 /* Peek at the next token. */
13778 token = cp_lexer_peek_token (parser->lexer);
13779 /* If the next token isn't the `asm' keyword, then there's no
13780 asm-specification. */
13781 if (!cp_parser_is_keyword (token, RID_ASM))
13784 /* Consume the `asm' token. */
13785 cp_lexer_consume_token (parser->lexer);
13786 /* Look for the `('. */
13787 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13789 /* Look for the string-literal. */
13790 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13792 asm_specification = token->value;
13794 asm_specification = NULL_TREE;
13796 /* Look for the `)'. */
13797 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13799 return asm_specification;
13802 /* Parse an asm-operand-list.
13806 asm-operand-list , asm-operand
13809 string-literal ( expression )
13810 [ string-literal ] string-literal ( expression )
13812 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13813 each node is the expression. The TREE_PURPOSE is itself a
13814 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13815 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13816 is a STRING_CST for the string literal before the parenthesis. */
13819 cp_parser_asm_operand_list (cp_parser* parser)
13821 tree asm_operands = NULL_TREE;
13825 tree string_literal;
13830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13832 /* Consume the `[' token. */
13833 cp_lexer_consume_token (parser->lexer);
13834 /* Read the operand name. */
13835 name = cp_parser_identifier (parser);
13836 if (name != error_mark_node)
13837 name = build_string (IDENTIFIER_LENGTH (name),
13838 IDENTIFIER_POINTER (name));
13839 /* Look for the closing `]'. */
13840 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13844 /* Look for the string-literal. */
13845 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13846 string_literal = token ? token->value : error_mark_node;
13847 c_lex_string_translate = 1;
13848 /* Look for the `('. */
13849 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13850 /* Parse the expression. */
13851 expression = cp_parser_expression (parser);
13852 /* Look for the `)'. */
13853 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13854 c_lex_string_translate = 0;
13855 /* Add this operand to the list. */
13856 asm_operands = tree_cons (build_tree_list (name, string_literal),
13859 /* If the next token is not a `,', there are no more
13861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13863 /* Consume the `,'. */
13864 cp_lexer_consume_token (parser->lexer);
13867 return nreverse (asm_operands);
13870 /* Parse an asm-clobber-list.
13874 asm-clobber-list , string-literal
13876 Returns a TREE_LIST, indicating the clobbers in the order that they
13877 appeared. The TREE_VALUE of each node is a STRING_CST. */
13880 cp_parser_asm_clobber_list (cp_parser* parser)
13882 tree clobbers = NULL_TREE;
13887 tree string_literal;
13889 /* Look for the string literal. */
13890 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13891 string_literal = token ? token->value : error_mark_node;
13892 /* Add it to the list. */
13893 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13894 /* If the next token is not a `,', then the list is
13896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13898 /* Consume the `,' token. */
13899 cp_lexer_consume_token (parser->lexer);
13905 /* Parse an (optional) series of attributes.
13908 attributes attribute
13911 __attribute__ (( attribute-list [opt] ))
13913 The return value is as for cp_parser_attribute_list. */
13916 cp_parser_attributes_opt (cp_parser* parser)
13918 tree attributes = NULL_TREE;
13923 tree attribute_list;
13925 /* Peek at the next token. */
13926 token = cp_lexer_peek_token (parser->lexer);
13927 /* If it's not `__attribute__', then we're done. */
13928 if (token->keyword != RID_ATTRIBUTE)
13931 /* Consume the `__attribute__' keyword. */
13932 cp_lexer_consume_token (parser->lexer);
13933 /* Look for the two `(' tokens. */
13934 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13935 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13937 /* Peek at the next token. */
13938 token = cp_lexer_peek_token (parser->lexer);
13939 if (token->type != CPP_CLOSE_PAREN)
13940 /* Parse the attribute-list. */
13941 attribute_list = cp_parser_attribute_list (parser);
13943 /* If the next token is a `)', then there is no attribute
13945 attribute_list = NULL;
13947 /* Look for the two `)' tokens. */
13948 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13949 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13951 /* Add these new attributes to the list. */
13952 attributes = chainon (attributes, attribute_list);
13958 /* Parse an attribute-list.
13962 attribute-list , attribute
13966 identifier ( identifier )
13967 identifier ( identifier , expression-list )
13968 identifier ( expression-list )
13970 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13971 TREE_PURPOSE of each node is the identifier indicating which
13972 attribute is in use. The TREE_VALUE represents the arguments, if
13976 cp_parser_attribute_list (cp_parser* parser)
13978 tree attribute_list = NULL_TREE;
13980 c_lex_string_translate = 0;
13987 /* Look for the identifier. We also allow keywords here; for
13988 example `__attribute__ ((const))' is legal. */
13989 token = cp_lexer_peek_token (parser->lexer);
13990 if (token->type != CPP_NAME
13991 && token->type != CPP_KEYWORD)
13992 return error_mark_node;
13993 /* Consume the token. */
13994 token = cp_lexer_consume_token (parser->lexer);
13996 /* Save away the identifier that indicates which attribute this is. */
13997 identifier = token->value;
13998 attribute = build_tree_list (identifier, NULL_TREE);
14000 /* Peek at the next token. */
14001 token = cp_lexer_peek_token (parser->lexer);
14002 /* If it's an `(', then parse the attribute arguments. */
14003 if (token->type == CPP_OPEN_PAREN)
14007 arguments = (cp_parser_parenthesized_expression_list
14008 (parser, true, /*non_constant_p=*/NULL));
14009 /* Save the identifier and arguments away. */
14010 TREE_VALUE (attribute) = arguments;
14013 /* Add this attribute to the list. */
14014 TREE_CHAIN (attribute) = attribute_list;
14015 attribute_list = attribute;
14017 /* Now, look for more attributes. */
14018 token = cp_lexer_peek_token (parser->lexer);
14019 /* If the next token isn't a `,', we're done. */
14020 if (token->type != CPP_COMMA)
14023 /* Consume the comma and keep going. */
14024 cp_lexer_consume_token (parser->lexer);
14026 c_lex_string_translate = 1;
14028 /* We built up the list in reverse order. */
14029 return nreverse (attribute_list);
14032 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14033 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14034 current value of the PEDANTIC flag, regardless of whether or not
14035 the `__extension__' keyword is present. The caller is responsible
14036 for restoring the value of the PEDANTIC flag. */
14039 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14041 /* Save the old value of the PEDANTIC flag. */
14042 *saved_pedantic = pedantic;
14044 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14046 /* Consume the `__extension__' token. */
14047 cp_lexer_consume_token (parser->lexer);
14048 /* We're not being pedantic while the `__extension__' keyword is
14058 /* Parse a label declaration.
14061 __label__ label-declarator-seq ;
14063 label-declarator-seq:
14064 identifier , label-declarator-seq
14068 cp_parser_label_declaration (cp_parser* parser)
14070 /* Look for the `__label__' keyword. */
14071 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14077 /* Look for an identifier. */
14078 identifier = cp_parser_identifier (parser);
14079 /* Declare it as a lobel. */
14080 finish_label_decl (identifier);
14081 /* If the next token is a `;', stop. */
14082 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14084 /* Look for the `,' separating the label declarations. */
14085 cp_parser_require (parser, CPP_COMMA, "`,'");
14088 /* Look for the final `;'. */
14089 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14092 /* Support Functions */
14094 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14095 NAME should have one of the representations used for an
14096 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14097 is returned. If PARSER->SCOPE is a dependent type, then a
14098 SCOPE_REF is returned.
14100 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14101 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14102 was formed. Abstractly, such entities should not be passed to this
14103 function, because they do not need to be looked up, but it is
14104 simpler to check for this special case here, rather than at the
14107 In cases not explicitly covered above, this function returns a
14108 DECL, OVERLOAD, or baselink representing the result of the lookup.
14109 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14112 If IS_TYPE is TRUE, bindings that do not refer to types are
14115 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14118 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14121 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14125 cp_parser_lookup_name (cp_parser *parser, tree name,
14126 bool is_type, bool is_template, bool is_namespace,
14127 bool check_dependency)
14130 tree object_type = parser->context->object_type;
14132 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14133 no longer valid. Note that if we are parsing tentatively, and
14134 the parse fails, OBJECT_TYPE will be automatically restored. */
14135 parser->context->object_type = NULL_TREE;
14137 if (name == error_mark_node)
14138 return error_mark_node;
14140 /* A template-id has already been resolved; there is no lookup to
14142 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14144 if (BASELINK_P (name))
14146 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
14147 == TEMPLATE_ID_EXPR),
14152 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14153 it should already have been checked to make sure that the name
14154 used matches the type being destroyed. */
14155 if (TREE_CODE (name) == BIT_NOT_EXPR)
14159 /* Figure out to which type this destructor applies. */
14161 type = parser->scope;
14162 else if (object_type)
14163 type = object_type;
14165 type = current_class_type;
14166 /* If that's not a class type, there is no destructor. */
14167 if (!type || !CLASS_TYPE_P (type))
14168 return error_mark_node;
14169 if (!CLASSTYPE_DESTRUCTORS (type))
14170 return error_mark_node;
14171 /* If it was a class type, return the destructor. */
14172 return CLASSTYPE_DESTRUCTORS (type);
14175 /* By this point, the NAME should be an ordinary identifier. If
14176 the id-expression was a qualified name, the qualifying scope is
14177 stored in PARSER->SCOPE at this point. */
14178 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
14181 /* Perform the lookup. */
14186 if (parser->scope == error_mark_node)
14187 return error_mark_node;
14189 /* If the SCOPE is dependent, the lookup must be deferred until
14190 the template is instantiated -- unless we are explicitly
14191 looking up names in uninstantiated templates. Even then, we
14192 cannot look up the name if the scope is not a class type; it
14193 might, for example, be a template type parameter. */
14194 dependent_p = (TYPE_P (parser->scope)
14195 && !(parser->in_declarator_p
14196 && currently_open_class (parser->scope))
14197 && dependent_type_p (parser->scope));
14198 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14202 /* The resolution to Core Issue 180 says that `struct A::B'
14203 should be considered a type-name, even if `A' is
14205 decl = TYPE_NAME (make_typename_type (parser->scope,
14208 else if (is_template)
14209 decl = make_unbound_class_template (parser->scope,
14213 decl = build_nt (SCOPE_REF, parser->scope, name);
14217 bool pop_p = false;
14219 /* If PARSER->SCOPE is a dependent type, then it must be a
14220 class type, and we must not be checking dependencies;
14221 otherwise, we would have processed this lookup above. So
14222 that PARSER->SCOPE is not considered a dependent base by
14223 lookup_member, we must enter the scope here. */
14225 pop_p = push_scope (parser->scope);
14226 /* If the PARSER->SCOPE is a a template specialization, it
14227 may be instantiated during name lookup. In that case,
14228 errors may be issued. Even if we rollback the current
14229 tentative parse, those errors are valid. */
14230 decl = lookup_qualified_name (parser->scope, name, is_type,
14231 /*complain=*/true);
14233 pop_scope (parser->scope);
14235 parser->qualifying_scope = parser->scope;
14236 parser->object_scope = NULL_TREE;
14238 else if (object_type)
14240 tree object_decl = NULL_TREE;
14241 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14242 OBJECT_TYPE is not a class. */
14243 if (CLASS_TYPE_P (object_type))
14244 /* If the OBJECT_TYPE is a template specialization, it may
14245 be instantiated during name lookup. In that case, errors
14246 may be issued. Even if we rollback the current tentative
14247 parse, those errors are valid. */
14248 object_decl = lookup_member (object_type,
14250 /*protect=*/0, is_type);
14251 /* Look it up in the enclosing context, too. */
14252 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14255 parser->object_scope = object_type;
14256 parser->qualifying_scope = NULL_TREE;
14258 decl = object_decl;
14262 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14265 parser->qualifying_scope = NULL_TREE;
14266 parser->object_scope = NULL_TREE;
14269 /* If the lookup failed, let our caller know. */
14271 || decl == error_mark_node
14272 || (TREE_CODE (decl) == FUNCTION_DECL
14273 && DECL_ANTICIPATED (decl)))
14274 return error_mark_node;
14276 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14277 if (TREE_CODE (decl) == TREE_LIST)
14279 /* The error message we have to print is too complicated for
14280 cp_parser_error, so we incorporate its actions directly. */
14281 if (!cp_parser_simulate_error (parser))
14283 error ("reference to `%D' is ambiguous", name);
14284 print_candidates (decl);
14286 return error_mark_node;
14289 my_friendly_assert (DECL_P (decl)
14290 || TREE_CODE (decl) == OVERLOAD
14291 || TREE_CODE (decl) == SCOPE_REF
14292 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14293 || BASELINK_P (decl),
14296 /* If we have resolved the name of a member declaration, check to
14297 see if the declaration is accessible. When the name resolves to
14298 set of overloaded functions, accessibility is checked when
14299 overload resolution is done.
14301 During an explicit instantiation, access is not checked at all,
14302 as per [temp.explicit]. */
14304 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14309 /* Like cp_parser_lookup_name, but for use in the typical case where
14310 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14311 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14314 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14316 return cp_parser_lookup_name (parser, name,
14318 /*is_template=*/false,
14319 /*is_namespace=*/false,
14320 /*check_dependency=*/true);
14323 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14324 the current context, return the TYPE_DECL. If TAG_NAME_P is
14325 true, the DECL indicates the class being defined in a class-head,
14326 or declared in an elaborated-type-specifier.
14328 Otherwise, return DECL. */
14331 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14333 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14334 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14337 template <typename T> struct B;
14340 template <typename T> struct A::B {};
14342 Similarly, in a elaborated-type-specifier:
14344 namespace N { struct X{}; }
14347 template <typename T> friend struct N::X;
14350 However, if the DECL refers to a class type, and we are in
14351 the scope of the class, then the name lookup automatically
14352 finds the TYPE_DECL created by build_self_reference rather
14353 than a TEMPLATE_DECL. For example, in:
14355 template <class T> struct S {
14359 there is no need to handle such case. */
14361 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14362 return DECL_TEMPLATE_RESULT (decl);
14367 /* If too many, or too few, template-parameter lists apply to the
14368 declarator, issue an error message. Returns TRUE if all went well,
14369 and FALSE otherwise. */
14372 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14373 cp_declarator *declarator)
14375 unsigned num_templates;
14377 /* We haven't seen any classes that involve template parameters yet. */
14380 switch (declarator->kind)
14383 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14388 scope = TREE_OPERAND (declarator->u.id.name, 0);
14389 member = TREE_OPERAND (declarator->u.id.name, 1);
14391 while (scope && CLASS_TYPE_P (scope))
14393 /* You're supposed to have one `template <...>'
14394 for every template class, but you don't need one
14395 for a full specialization. For example:
14397 template <class T> struct S{};
14398 template <> struct S<int> { void f(); };
14399 void S<int>::f () {}
14401 is correct; there shouldn't be a `template <>' for
14402 the definition of `S<int>::f'. */
14403 if (CLASSTYPE_TEMPLATE_INFO (scope)
14404 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14405 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14406 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14409 scope = TYPE_CONTEXT (scope);
14413 /* If the DECLARATOR has the form `X<y>' then it uses one
14414 additional level of template parameters. */
14415 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14418 return cp_parser_check_template_parameters (parser,
14424 case cdk_reference:
14426 return (cp_parser_check_declarator_template_parameters
14427 (parser, declarator->declarator));
14438 /* NUM_TEMPLATES were used in the current declaration. If that is
14439 invalid, return FALSE and issue an error messages. Otherwise,
14443 cp_parser_check_template_parameters (cp_parser* parser,
14444 unsigned num_templates)
14446 /* If there are more template classes than parameter lists, we have
14449 template <class T> void S<T>::R<T>::f (); */
14450 if (parser->num_template_parameter_lists < num_templates)
14452 error ("too few template-parameter-lists");
14455 /* If there are the same number of template classes and parameter
14456 lists, that's OK. */
14457 if (parser->num_template_parameter_lists == num_templates)
14459 /* If there are more, but only one more, then we are referring to a
14460 member template. That's OK too. */
14461 if (parser->num_template_parameter_lists == num_templates + 1)
14463 /* Otherwise, there are too many template parameter lists. We have
14466 template <class T> template <class U> void S::f(); */
14467 error ("too many template-parameter-lists");
14471 /* Parse a binary-expression of the general form:
14475 binary-expression <token> <expr>
14477 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14478 to parser the <expr>s. If the first production is used, then the
14479 value returned by FN is returned directly. Otherwise, a node with
14480 the indicated EXPR_TYPE is returned, with operands corresponding to
14481 the two sub-expressions. */
14484 cp_parser_binary_expression (cp_parser* parser,
14485 const cp_parser_token_tree_map token_tree_map,
14486 cp_parser_expression_fn fn)
14490 /* Parse the first expression. */
14491 lhs = (*fn) (parser);
14492 /* Now, look for more expressions. */
14496 const cp_parser_token_tree_map_node *map_node;
14499 /* Peek at the next token. */
14500 token = cp_lexer_peek_token (parser->lexer);
14501 /* If the token is `>', and that's not an operator at the
14502 moment, then we're done. */
14503 if (token->type == CPP_GREATER
14504 && !parser->greater_than_is_operator_p)
14506 /* If we find one of the tokens we want, build the corresponding
14507 tree representation. */
14508 for (map_node = token_tree_map;
14509 map_node->token_type != CPP_EOF;
14511 if (map_node->token_type == token->type)
14513 /* Assume that an overloaded operator will not be used. */
14514 bool overloaded_p = false;
14516 /* Consume the operator token. */
14517 cp_lexer_consume_token (parser->lexer);
14518 /* Parse the right-hand side of the expression. */
14519 rhs = (*fn) (parser);
14520 /* Build the binary tree node. */
14521 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14523 /* If the binary operator required the use of an
14524 overloaded operator, then this expression cannot be an
14525 integral constant-expression. An overloaded operator
14526 can be used even if both operands are otherwise
14527 permissible in an integral constant-expression if at
14528 least one of the operands is of enumeration type. */
14530 && (cp_parser_non_integral_constant_expression
14531 (parser, "calls to overloaded operators")))
14532 lhs = error_mark_node;
14536 /* If the token wasn't one of the ones we want, we're done. */
14537 if (map_node->token_type == CPP_EOF)
14544 /* Parse an optional `::' token indicating that the following name is
14545 from the global namespace. If so, PARSER->SCOPE is set to the
14546 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14547 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14548 Returns the new value of PARSER->SCOPE, if the `::' token is
14549 present, and NULL_TREE otherwise. */
14552 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14556 /* Peek at the next token. */
14557 token = cp_lexer_peek_token (parser->lexer);
14558 /* If we're looking at a `::' token then we're starting from the
14559 global namespace, not our current location. */
14560 if (token->type == CPP_SCOPE)
14562 /* Consume the `::' token. */
14563 cp_lexer_consume_token (parser->lexer);
14564 /* Set the SCOPE so that we know where to start the lookup. */
14565 parser->scope = global_namespace;
14566 parser->qualifying_scope = global_namespace;
14567 parser->object_scope = NULL_TREE;
14569 return parser->scope;
14571 else if (!current_scope_valid_p)
14573 parser->scope = NULL_TREE;
14574 parser->qualifying_scope = NULL_TREE;
14575 parser->object_scope = NULL_TREE;
14581 /* Returns TRUE if the upcoming token sequence is the start of a
14582 constructor declarator. If FRIEND_P is true, the declarator is
14583 preceded by the `friend' specifier. */
14586 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14588 bool constructor_p;
14589 tree type_decl = NULL_TREE;
14590 bool nested_name_p;
14591 cp_token *next_token;
14593 /* The common case is that this is not a constructor declarator, so
14594 try to avoid doing lots of work if at all possible. It's not
14595 valid declare a constructor at function scope. */
14596 if (at_function_scope_p ())
14598 /* And only certain tokens can begin a constructor declarator. */
14599 next_token = cp_lexer_peek_token (parser->lexer);
14600 if (next_token->type != CPP_NAME
14601 && next_token->type != CPP_SCOPE
14602 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14603 && next_token->type != CPP_TEMPLATE_ID)
14606 /* Parse tentatively; we are going to roll back all of the tokens
14608 cp_parser_parse_tentatively (parser);
14609 /* Assume that we are looking at a constructor declarator. */
14610 constructor_p = true;
14612 /* Look for the optional `::' operator. */
14613 cp_parser_global_scope_opt (parser,
14614 /*current_scope_valid_p=*/false);
14615 /* Look for the nested-name-specifier. */
14617 = (cp_parser_nested_name_specifier_opt (parser,
14618 /*typename_keyword_p=*/false,
14619 /*check_dependency_p=*/false,
14621 /*is_declaration=*/false)
14623 /* Outside of a class-specifier, there must be a
14624 nested-name-specifier. */
14625 if (!nested_name_p &&
14626 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14628 constructor_p = false;
14629 /* If we still think that this might be a constructor-declarator,
14630 look for a class-name. */
14635 template <typename T> struct S { S(); };
14636 template <typename T> S<T>::S ();
14638 we must recognize that the nested `S' names a class.
14641 template <typename T> S<T>::S<T> ();
14643 we must recognize that the nested `S' names a template. */
14644 type_decl = cp_parser_class_name (parser,
14645 /*typename_keyword_p=*/false,
14646 /*template_keyword_p=*/false,
14648 /*check_dependency_p=*/false,
14649 /*class_head_p=*/false,
14650 /*is_declaration=*/false);
14651 /* If there was no class-name, then this is not a constructor. */
14652 constructor_p = !cp_parser_error_occurred (parser);
14655 /* If we're still considering a constructor, we have to see a `(',
14656 to begin the parameter-declaration-clause, followed by either a
14657 `)', an `...', or a decl-specifier. We need to check for a
14658 type-specifier to avoid being fooled into thinking that:
14662 is a constructor. (It is actually a function named `f' that
14663 takes one parameter (of type `int') and returns a value of type
14666 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14669 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14670 /* A parameter declaration begins with a decl-specifier,
14671 which is either the "attribute" keyword, a storage class
14672 specifier, or (usually) a type-specifier. */
14673 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14674 && !cp_parser_storage_class_specifier_opt (parser))
14677 bool pop_p = false;
14678 unsigned saved_num_template_parameter_lists;
14680 /* Names appearing in the type-specifier should be looked up
14681 in the scope of the class. */
14682 if (current_class_type)
14686 type = TREE_TYPE (type_decl);
14687 if (TREE_CODE (type) == TYPENAME_TYPE)
14689 type = resolve_typename_type (type,
14690 /*only_current_p=*/false);
14691 if (type == error_mark_node)
14693 cp_parser_abort_tentative_parse (parser);
14697 pop_p = push_scope (type);
14700 /* Inside the constructor parameter list, surrounding
14701 template-parameter-lists do not apply. */
14702 saved_num_template_parameter_lists
14703 = parser->num_template_parameter_lists;
14704 parser->num_template_parameter_lists = 0;
14706 /* Look for the type-specifier. */
14707 cp_parser_type_specifier (parser,
14708 CP_PARSER_FLAGS_NONE,
14709 /*decl_specs=*/NULL,
14710 /*is_declarator=*/true,
14711 /*declares_class_or_enum=*/NULL,
14712 /*is_cv_qualifier=*/NULL);
14714 parser->num_template_parameter_lists
14715 = saved_num_template_parameter_lists;
14717 /* Leave the scope of the class. */
14721 constructor_p = !cp_parser_error_occurred (parser);
14725 constructor_p = false;
14726 /* We did not really want to consume any tokens. */
14727 cp_parser_abort_tentative_parse (parser);
14729 return constructor_p;
14732 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14733 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14734 they must be performed once we are in the scope of the function.
14736 Returns the function defined. */
14739 cp_parser_function_definition_from_specifiers_and_declarator
14740 (cp_parser* parser,
14741 cp_decl_specifier_seq *decl_specifiers,
14743 const cp_declarator *declarator)
14748 /* Begin the function-definition. */
14749 success_p = start_function (decl_specifiers, declarator, attributes);
14751 /* The things we're about to see are not directly qualified by any
14752 template headers we've seen thus far. */
14753 reset_specialization ();
14755 /* If there were names looked up in the decl-specifier-seq that we
14756 did not check, check them now. We must wait until we are in the
14757 scope of the function to perform the checks, since the function
14758 might be a friend. */
14759 perform_deferred_access_checks ();
14763 /* Skip the entire function. */
14764 error ("invalid function declaration");
14765 cp_parser_skip_to_end_of_block_or_statement (parser);
14766 fn = error_mark_node;
14769 fn = cp_parser_function_definition_after_declarator (parser,
14770 /*inline_p=*/false);
14775 /* Parse the part of a function-definition that follows the
14776 declarator. INLINE_P is TRUE iff this function is an inline
14777 function defined with a class-specifier.
14779 Returns the function defined. */
14782 cp_parser_function_definition_after_declarator (cp_parser* parser,
14786 bool ctor_initializer_p = false;
14787 bool saved_in_unbraced_linkage_specification_p;
14788 unsigned saved_num_template_parameter_lists;
14790 /* If the next token is `return', then the code may be trying to
14791 make use of the "named return value" extension that G++ used to
14793 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14795 /* Consume the `return' keyword. */
14796 cp_lexer_consume_token (parser->lexer);
14797 /* Look for the identifier that indicates what value is to be
14799 cp_parser_identifier (parser);
14800 /* Issue an error message. */
14801 error ("named return values are no longer supported");
14802 /* Skip tokens until we reach the start of the function body. */
14803 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14804 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14805 cp_lexer_consume_token (parser->lexer);
14807 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14808 anything declared inside `f'. */
14809 saved_in_unbraced_linkage_specification_p
14810 = parser->in_unbraced_linkage_specification_p;
14811 parser->in_unbraced_linkage_specification_p = false;
14812 /* Inside the function, surrounding template-parameter-lists do not
14814 saved_num_template_parameter_lists
14815 = parser->num_template_parameter_lists;
14816 parser->num_template_parameter_lists = 0;
14817 /* If the next token is `try', then we are looking at a
14818 function-try-block. */
14819 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14820 ctor_initializer_p = cp_parser_function_try_block (parser);
14821 /* A function-try-block includes the function-body, so we only do
14822 this next part if we're not processing a function-try-block. */
14825 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14827 /* Finish the function. */
14828 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14829 (inline_p ? 2 : 0));
14830 /* Generate code for it, if necessary. */
14831 expand_or_defer_fn (fn);
14832 /* Restore the saved values. */
14833 parser->in_unbraced_linkage_specification_p
14834 = saved_in_unbraced_linkage_specification_p;
14835 parser->num_template_parameter_lists
14836 = saved_num_template_parameter_lists;
14841 /* Parse a template-declaration, assuming that the `export' (and
14842 `extern') keywords, if present, has already been scanned. MEMBER_P
14843 is as for cp_parser_template_declaration. */
14846 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14848 tree decl = NULL_TREE;
14849 tree parameter_list;
14850 bool friend_p = false;
14852 /* Look for the `template' keyword. */
14853 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14857 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14860 /* If the next token is `>', then we have an invalid
14861 specialization. Rather than complain about an invalid template
14862 parameter, issue an error message here. */
14863 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14865 cp_parser_error (parser, "invalid explicit specialization");
14866 begin_specialization ();
14867 parameter_list = NULL_TREE;
14871 /* Parse the template parameters. */
14872 begin_template_parm_list ();
14873 parameter_list = cp_parser_template_parameter_list (parser);
14874 parameter_list = end_template_parm_list (parameter_list);
14877 /* Look for the `>'. */
14878 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14879 /* We just processed one more parameter list. */
14880 ++parser->num_template_parameter_lists;
14881 /* If the next token is `template', there are more template
14883 if (cp_lexer_next_token_is_keyword (parser->lexer,
14885 cp_parser_template_declaration_after_export (parser, member_p);
14888 decl = cp_parser_single_declaration (parser,
14892 /* If this is a member template declaration, let the front
14894 if (member_p && !friend_p && decl)
14896 if (TREE_CODE (decl) == TYPE_DECL)
14897 cp_parser_check_access_in_redeclaration (decl);
14899 decl = finish_member_template_decl (decl);
14901 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14902 make_friend_class (current_class_type, TREE_TYPE (decl),
14903 /*complain=*/true);
14905 /* We are done with the current parameter list. */
14906 --parser->num_template_parameter_lists;
14909 finish_template_decl (parameter_list);
14911 /* Register member declarations. */
14912 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14913 finish_member_declaration (decl);
14915 /* If DECL is a function template, we must return to parse it later.
14916 (Even though there is no definition, there might be default
14917 arguments that need handling.) */
14918 if (member_p && decl
14919 && (TREE_CODE (decl) == FUNCTION_DECL
14920 || DECL_FUNCTION_TEMPLATE_P (decl)))
14921 TREE_VALUE (parser->unparsed_functions_queues)
14922 = tree_cons (NULL_TREE, decl,
14923 TREE_VALUE (parser->unparsed_functions_queues));
14926 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14927 `function-definition' sequence. MEMBER_P is true, this declaration
14928 appears in a class scope.
14930 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14931 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14934 cp_parser_single_declaration (cp_parser* parser,
14938 int declares_class_or_enum;
14939 tree decl = NULL_TREE;
14940 cp_decl_specifier_seq decl_specifiers;
14941 bool function_definition_p = false;
14943 /* Defer access checks until we know what is being declared. */
14944 push_deferring_access_checks (dk_deferred);
14946 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14948 cp_parser_decl_specifier_seq (parser,
14949 CP_PARSER_FLAGS_OPTIONAL,
14951 &declares_class_or_enum);
14953 *friend_p = cp_parser_friend_p (&decl_specifiers);
14954 /* Gather up the access checks that occurred the
14955 decl-specifier-seq. */
14956 stop_deferring_access_checks ();
14958 /* Check for the declaration of a template class. */
14959 if (declares_class_or_enum)
14961 if (cp_parser_declares_only_class_p (parser))
14963 decl = shadow_tag (&decl_specifiers);
14964 if (decl && decl != error_mark_node)
14965 decl = TYPE_NAME (decl);
14967 decl = error_mark_node;
14972 /* If it's not a template class, try for a template function. If
14973 the next token is a `;', then this declaration does not declare
14974 anything. But, if there were errors in the decl-specifiers, then
14975 the error might well have come from an attempted class-specifier.
14976 In that case, there's no need to warn about a missing declarator. */
14978 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14979 || decl_specifiers.type != error_mark_node))
14980 decl = cp_parser_init_declarator (parser,
14982 /*function_definition_allowed_p=*/true,
14984 declares_class_or_enum,
14985 &function_definition_p);
14987 pop_deferring_access_checks ();
14989 /* Clear any current qualification; whatever comes next is the start
14990 of something new. */
14991 parser->scope = NULL_TREE;
14992 parser->qualifying_scope = NULL_TREE;
14993 parser->object_scope = NULL_TREE;
14994 /* Look for a trailing `;' after the declaration. */
14995 if (!function_definition_p
14996 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14997 cp_parser_skip_to_end_of_block_or_statement (parser);
15002 /* Parse a cast-expression that is not the operand of a unary "&". */
15005 cp_parser_simple_cast_expression (cp_parser *parser)
15007 return cp_parser_cast_expression (parser, /*address_p=*/false);
15010 /* Parse a functional cast to TYPE. Returns an expression
15011 representing the cast. */
15014 cp_parser_functional_cast (cp_parser* parser, tree type)
15016 tree expression_list;
15020 = cp_parser_parenthesized_expression_list (parser, false,
15021 /*non_constant_p=*/NULL);
15023 cast = build_functional_cast (type, expression_list);
15024 /* [expr.const]/1: In an integral constant expression "only type
15025 conversions to integral or enumeration type can be used". */
15026 if (cast != error_mark_node && !type_dependent_expression_p (type)
15027 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15029 if (cp_parser_non_integral_constant_expression
15030 (parser, "a call to a constructor"))
15031 return error_mark_node;
15036 /* Save the tokens that make up the body of a member function defined
15037 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15038 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15039 specifiers applied to the declaration. Returns the FUNCTION_DECL
15040 for the member function. */
15043 cp_parser_save_member_function_body (cp_parser* parser,
15044 cp_decl_specifier_seq *decl_specifiers,
15045 cp_declarator *declarator,
15048 cp_token_cache *cache;
15051 /* Create the function-declaration. */
15052 fn = start_method (decl_specifiers, declarator, attributes);
15053 /* If something went badly wrong, bail out now. */
15054 if (fn == error_mark_node)
15056 /* If there's a function-body, skip it. */
15057 if (cp_parser_token_starts_function_definition_p
15058 (cp_lexer_peek_token (parser->lexer)))
15059 cp_parser_skip_to_end_of_block_or_statement (parser);
15060 return error_mark_node;
15063 /* Remember it, if there default args to post process. */
15064 cp_parser_save_default_args (parser, fn);
15066 /* Create a token cache. */
15067 cache = cp_token_cache_new ();
15068 /* Save away the tokens that make up the body of the
15070 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15071 /* Handle function try blocks. */
15072 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15073 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15075 /* Save away the inline definition; we will process it when the
15076 class is complete. */
15077 DECL_PENDING_INLINE_INFO (fn) = cache;
15078 DECL_PENDING_INLINE_P (fn) = 1;
15080 /* We need to know that this was defined in the class, so that
15081 friend templates are handled correctly. */
15082 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15084 /* We're done with the inline definition. */
15085 finish_method (fn);
15087 /* Add FN to the queue of functions to be parsed later. */
15088 TREE_VALUE (parser->unparsed_functions_queues)
15089 = tree_cons (NULL_TREE, fn,
15090 TREE_VALUE (parser->unparsed_functions_queues));
15095 /* Parse a template-argument-list, as well as the trailing ">" (but
15096 not the opening ">"). See cp_parser_template_argument_list for the
15100 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15104 tree saved_qualifying_scope;
15105 tree saved_object_scope;
15106 bool saved_greater_than_is_operator_p;
15110 When parsing a template-id, the first non-nested `>' is taken as
15111 the end of the template-argument-list rather than a greater-than
15113 saved_greater_than_is_operator_p
15114 = parser->greater_than_is_operator_p;
15115 parser->greater_than_is_operator_p = false;
15116 /* Parsing the argument list may modify SCOPE, so we save it
15118 saved_scope = parser->scope;
15119 saved_qualifying_scope = parser->qualifying_scope;
15120 saved_object_scope = parser->object_scope;
15121 /* Parse the template-argument-list itself. */
15122 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15123 arguments = NULL_TREE;
15125 arguments = cp_parser_template_argument_list (parser);
15126 /* Look for the `>' that ends the template-argument-list. If we find
15127 a '>>' instead, it's probably just a typo. */
15128 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15130 if (!saved_greater_than_is_operator_p)
15132 /* If we're in a nested template argument list, the '>>' has to be
15133 a typo for '> >'. We emit the error message, but we continue
15134 parsing and we push a '>' as next token, so that the argument
15135 list will be parsed correctly.. */
15137 error ("`>>' should be `> >' within a nested template argument list");
15138 token = cp_lexer_peek_token (parser->lexer);
15139 token->type = CPP_GREATER;
15143 /* If this is not a nested template argument list, the '>>' is
15144 a typo for '>'. Emit an error message and continue. */
15145 error ("spurious `>>', use `>' to terminate a template argument list");
15146 cp_lexer_consume_token (parser->lexer);
15149 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
15150 error ("missing `>' to terminate the template argument list");
15151 /* The `>' token might be a greater-than operator again now. */
15152 parser->greater_than_is_operator_p
15153 = saved_greater_than_is_operator_p;
15154 /* Restore the SAVED_SCOPE. */
15155 parser->scope = saved_scope;
15156 parser->qualifying_scope = saved_qualifying_scope;
15157 parser->object_scope = saved_object_scope;
15162 /* MEMBER_FUNCTION is a member function, or a friend. If default
15163 arguments, or the body of the function have not yet been parsed,
15167 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15169 cp_lexer *saved_lexer;
15171 /* If this member is a template, get the underlying
15173 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15174 member_function = DECL_TEMPLATE_RESULT (member_function);
15176 /* There should not be any class definitions in progress at this
15177 point; the bodies of members are only parsed outside of all class
15179 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
15180 /* While we're parsing the member functions we might encounter more
15181 classes. We want to handle them right away, but we don't want
15182 them getting mixed up with functions that are currently in the
15184 parser->unparsed_functions_queues
15185 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15187 /* Make sure that any template parameters are in scope. */
15188 maybe_begin_member_template_processing (member_function);
15190 /* If the body of the function has not yet been parsed, parse it
15192 if (DECL_PENDING_INLINE_P (member_function))
15194 tree function_scope;
15195 cp_token_cache *tokens;
15197 /* The function is no longer pending; we are processing it. */
15198 tokens = DECL_PENDING_INLINE_INFO (member_function);
15199 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15200 DECL_PENDING_INLINE_P (member_function) = 0;
15201 /* If this was an inline function in a local class, enter the scope
15202 of the containing function. */
15203 function_scope = decl_function_context (member_function);
15204 if (function_scope)
15205 push_function_context_to (function_scope);
15207 /* Save away the current lexer. */
15208 saved_lexer = parser->lexer;
15209 /* Make a new lexer to feed us the tokens saved for this function. */
15210 parser->lexer = cp_lexer_new_from_tokens (tokens);
15211 parser->lexer->next = saved_lexer;
15213 /* Set the current source position to be the location of the first
15214 token in the saved inline body. */
15215 cp_lexer_peek_token (parser->lexer);
15217 /* Let the front end know that we going to be defining this
15219 start_preparsed_function (member_function, NULL_TREE,
15220 SF_PRE_PARSED | SF_INCLASS_INLINE);
15222 /* Now, parse the body of the function. */
15223 cp_parser_function_definition_after_declarator (parser,
15224 /*inline_p=*/true);
15226 /* Leave the scope of the containing function. */
15227 if (function_scope)
15228 pop_function_context_from (function_scope);
15229 /* Restore the lexer. */
15230 parser->lexer = saved_lexer;
15233 /* Remove any template parameters from the symbol table. */
15234 maybe_end_member_template_processing ();
15236 /* Restore the queue. */
15237 parser->unparsed_functions_queues
15238 = TREE_CHAIN (parser->unparsed_functions_queues);
15241 /* If DECL contains any default args, remember it on the unparsed
15242 functions queue. */
15245 cp_parser_save_default_args (cp_parser* parser, tree decl)
15249 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15251 probe = TREE_CHAIN (probe))
15252 if (TREE_PURPOSE (probe))
15254 TREE_PURPOSE (parser->unparsed_functions_queues)
15255 = tree_cons (NULL_TREE, decl,
15256 TREE_PURPOSE (parser->unparsed_functions_queues));
15262 /* FN is a FUNCTION_DECL which may contains a parameter with an
15263 unparsed DEFAULT_ARG. Parse the default args now. */
15266 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15268 cp_lexer *saved_lexer;
15269 cp_token_cache *tokens;
15270 bool saved_local_variables_forbidden_p;
15273 /* While we're parsing the default args, we might (due to the
15274 statement expression extension) encounter more classes. We want
15275 to handle them right away, but we don't want them getting mixed
15276 up with default args that are currently in the queue. */
15277 parser->unparsed_functions_queues
15278 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15280 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
15282 parameters = TREE_CHAIN (parameters))
15284 if (!TREE_PURPOSE (parameters)
15285 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
15288 /* Save away the current lexer. */
15289 saved_lexer = parser->lexer;
15290 /* Create a new one, using the tokens we have saved. */
15291 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
15292 parser->lexer = cp_lexer_new_from_tokens (tokens);
15294 /* Set the current source position to be the location of the
15295 first token in the default argument. */
15296 cp_lexer_peek_token (parser->lexer);
15298 /* Local variable names (and the `this' keyword) may not appear
15299 in a default argument. */
15300 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15301 parser->local_variables_forbidden_p = true;
15302 /* Parse the assignment-expression. */
15303 if (DECL_CLASS_SCOPE_P (fn))
15304 push_nested_class (DECL_CONTEXT (fn));
15305 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
15306 if (DECL_CLASS_SCOPE_P (fn))
15307 pop_nested_class ();
15309 /* If the token stream has not been completely used up, then
15310 there was extra junk after the end of the default
15312 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15313 cp_parser_error (parser, "expected `,'");
15315 /* Restore saved state. */
15316 parser->lexer = saved_lexer;
15317 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15320 /* Restore the queue. */
15321 parser->unparsed_functions_queues
15322 = TREE_CHAIN (parser->unparsed_functions_queues);
15325 /* Parse the operand of `sizeof' (or a similar operator). Returns
15326 either a TYPE or an expression, depending on the form of the
15327 input. The KEYWORD indicates which kind of expression we have
15331 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15333 static const char *format;
15334 tree expr = NULL_TREE;
15335 const char *saved_message;
15336 bool saved_integral_constant_expression_p;
15338 /* Initialize FORMAT the first time we get here. */
15340 format = "types may not be defined in `%s' expressions";
15342 /* Types cannot be defined in a `sizeof' expression. Save away the
15344 saved_message = parser->type_definition_forbidden_message;
15345 /* And create the new one. */
15346 parser->type_definition_forbidden_message
15347 = xmalloc (strlen (format)
15348 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15350 sprintf ((char *) parser->type_definition_forbidden_message,
15351 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15353 /* The restrictions on constant-expressions do not apply inside
15354 sizeof expressions. */
15355 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15356 parser->integral_constant_expression_p = false;
15358 /* Do not actually evaluate the expression. */
15360 /* If it's a `(', then we might be looking at the type-id
15362 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15365 bool saved_in_type_id_in_expr_p;
15367 /* We can't be sure yet whether we're looking at a type-id or an
15369 cp_parser_parse_tentatively (parser);
15370 /* Consume the `('. */
15371 cp_lexer_consume_token (parser->lexer);
15372 /* Parse the type-id. */
15373 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15374 parser->in_type_id_in_expr_p = true;
15375 type = cp_parser_type_id (parser);
15376 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15377 /* Now, look for the trailing `)'. */
15378 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15379 /* If all went well, then we're done. */
15380 if (cp_parser_parse_definitely (parser))
15382 cp_decl_specifier_seq decl_specs;
15384 /* Build a trivial decl-specifier-seq. */
15385 clear_decl_specs (&decl_specs);
15386 decl_specs.type = type;
15388 /* Call grokdeclarator to figure out what type this is. */
15389 expr = grokdeclarator (NULL,
15393 /*attrlist=*/NULL);
15397 /* If the type-id production did not work out, then we must be
15398 looking at the unary-expression production. */
15400 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15401 /* Go back to evaluating expressions. */
15404 /* Free the message we created. */
15405 free ((char *) parser->type_definition_forbidden_message);
15406 /* And restore the old one. */
15407 parser->type_definition_forbidden_message = saved_message;
15408 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15413 /* If the current declaration has no declarator, return true. */
15416 cp_parser_declares_only_class_p (cp_parser *parser)
15418 /* If the next token is a `;' or a `,' then there is no
15420 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15421 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15424 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15427 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15428 cp_storage_class storage_class)
15430 if (decl_specs->storage_class != sc_none)
15431 decl_specs->multiple_storage_classes_p = true;
15433 decl_specs->storage_class = storage_class;
15436 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15437 is true, the type is a user-defined type; otherwise it is a
15438 built-in type specified by a keyword. */
15441 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15443 bool user_defined_p)
15445 decl_specs->any_specifiers_p = true;
15446 if (decl_specs->type)
15448 if (decl_specs->specs[(int)ds_typedef] && !user_defined_p)
15449 decl_specs->redefined_builtin_type = type_spec;
15451 decl_specs->multiple_types_p = true;
15455 decl_specs->type = type_spec;
15456 decl_specs->user_defined_type_p = user_defined_p;
15460 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15461 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15464 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15466 return decl_specifiers->specs[(int) ds_friend] != 0;
15469 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15470 issue an error message indicating that TOKEN_DESC was expected.
15472 Returns the token consumed, if the token had the appropriate type.
15473 Otherwise, returns NULL. */
15476 cp_parser_require (cp_parser* parser,
15477 enum cpp_ttype type,
15478 const char* token_desc)
15480 if (cp_lexer_next_token_is (parser->lexer, type))
15481 return cp_lexer_consume_token (parser->lexer);
15484 /* Output the MESSAGE -- unless we're parsing tentatively. */
15485 if (!cp_parser_simulate_error (parser))
15487 char *message = concat ("expected ", token_desc, NULL);
15488 cp_parser_error (parser, message);
15495 /* Like cp_parser_require, except that tokens will be skipped until
15496 the desired token is found. An error message is still produced if
15497 the next token is not as expected. */
15500 cp_parser_skip_until_found (cp_parser* parser,
15501 enum cpp_ttype type,
15502 const char* token_desc)
15505 unsigned nesting_depth = 0;
15507 if (cp_parser_require (parser, type, token_desc))
15510 /* Skip tokens until the desired token is found. */
15513 /* Peek at the next token. */
15514 token = cp_lexer_peek_token (parser->lexer);
15515 /* If we've reached the token we want, consume it and
15517 if (token->type == type && !nesting_depth)
15519 cp_lexer_consume_token (parser->lexer);
15522 /* If we've run out of tokens, stop. */
15523 if (token->type == CPP_EOF)
15525 if (token->type == CPP_OPEN_BRACE
15526 || token->type == CPP_OPEN_PAREN
15527 || token->type == CPP_OPEN_SQUARE)
15529 else if (token->type == CPP_CLOSE_BRACE
15530 || token->type == CPP_CLOSE_PAREN
15531 || token->type == CPP_CLOSE_SQUARE)
15533 if (nesting_depth-- == 0)
15536 /* Consume this token. */
15537 cp_lexer_consume_token (parser->lexer);
15541 /* If the next token is the indicated keyword, consume it. Otherwise,
15542 issue an error message indicating that TOKEN_DESC was expected.
15544 Returns the token consumed, if the token had the appropriate type.
15545 Otherwise, returns NULL. */
15548 cp_parser_require_keyword (cp_parser* parser,
15550 const char* token_desc)
15552 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15554 if (token && token->keyword != keyword)
15556 dyn_string_t error_msg;
15558 /* Format the error message. */
15559 error_msg = dyn_string_new (0);
15560 dyn_string_append_cstr (error_msg, "expected ");
15561 dyn_string_append_cstr (error_msg, token_desc);
15562 cp_parser_error (parser, error_msg->s);
15563 dyn_string_delete (error_msg);
15570 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15571 function-definition. */
15574 cp_parser_token_starts_function_definition_p (cp_token* token)
15576 return (/* An ordinary function-body begins with an `{'. */
15577 token->type == CPP_OPEN_BRACE
15578 /* A ctor-initializer begins with a `:'. */
15579 || token->type == CPP_COLON
15580 /* A function-try-block begins with `try'. */
15581 || token->keyword == RID_TRY
15582 /* The named return value extension begins with `return'. */
15583 || token->keyword == RID_RETURN);
15586 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15590 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15594 token = cp_lexer_peek_token (parser->lexer);
15595 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15598 /* Returns TRUE iff the next token is the "," or ">" ending a
15599 template-argument. ">>" is also accepted (after the full
15600 argument was parsed) because it's probably a typo for "> >",
15601 and there is a specific diagnostic for this. */
15604 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15608 token = cp_lexer_peek_token (parser->lexer);
15609 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15610 || token->type == CPP_RSHIFT);
15613 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15614 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15617 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15622 token = cp_lexer_peek_nth_token (parser->lexer, n);
15623 if (token->type == CPP_LESS)
15625 /* Check for the sequence `<::' in the original code. It would be lexed as
15626 `[:', where `[' is a digraph, and there is no whitespace before
15628 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15631 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15632 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15638 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15639 or none_type otherwise. */
15641 static enum tag_types
15642 cp_parser_token_is_class_key (cp_token* token)
15644 switch (token->keyword)
15649 return record_type;
15658 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15661 cp_parser_check_class_key (enum tag_types class_key, tree type)
15663 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15664 pedwarn ("`%s' tag used in naming `%#T'",
15665 class_key == union_type ? "union"
15666 : class_key == record_type ? "struct" : "class",
15670 /* Issue an error message if DECL is redeclared with different
15671 access than its original declaration [class.access.spec/3].
15672 This applies to nested classes and nested class templates.
15675 static void cp_parser_check_access_in_redeclaration (tree decl)
15677 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15680 if ((TREE_PRIVATE (decl)
15681 != (current_access_specifier == access_private_node))
15682 || (TREE_PROTECTED (decl)
15683 != (current_access_specifier == access_protected_node)))
15684 error ("%D redeclared with different access", decl);
15687 /* Look for the `template' keyword, as a syntactic disambiguator.
15688 Return TRUE iff it is present, in which case it will be
15692 cp_parser_optional_template_keyword (cp_parser *parser)
15694 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15696 /* The `template' keyword can only be used within templates;
15697 outside templates the parser can always figure out what is a
15698 template and what is not. */
15699 if (!processing_template_decl)
15701 error ("`template' (as a disambiguator) is only allowed "
15702 "within templates");
15703 /* If this part of the token stream is rescanned, the same
15704 error message would be generated. So, we purge the token
15705 from the stream. */
15706 cp_lexer_purge_token (parser->lexer);
15711 /* Consume the `template' keyword. */
15712 cp_lexer_consume_token (parser->lexer);
15720 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15721 set PARSER->SCOPE, and perform other related actions. */
15724 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15729 /* Get the stored value. */
15730 value = cp_lexer_consume_token (parser->lexer)->value;
15731 /* Perform any access checks that were deferred. */
15732 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15733 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15734 /* Set the scope from the stored value. */
15735 parser->scope = TREE_VALUE (value);
15736 parser->qualifying_scope = TREE_TYPE (value);
15737 parser->object_scope = NULL_TREE;
15740 /* Add tokens to CACHE until a non-nested END token appears. */
15743 cp_parser_cache_group_1 (cp_parser *parser,
15744 cp_token_cache *cache,
15745 enum cpp_ttype end,
15752 /* Abort a parenthesized expression if we encounter a brace. */
15753 if ((end == CPP_CLOSE_PAREN || depth == 0)
15754 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15756 /* If we've reached the end of the file, stop. */
15757 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15759 /* Consume the next token. */
15760 token = cp_lexer_consume_token (parser->lexer);
15761 /* Add this token to the tokens we are saving. */
15762 cp_token_cache_push_token (cache, token);
15763 /* See if it starts a new group. */
15764 if (token->type == CPP_OPEN_BRACE)
15766 cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15770 else if (token->type == CPP_OPEN_PAREN)
15771 cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15772 else if (token->type == end)
15777 /* Convenient interface for cp_parser_cache_group_1 that makes sure we
15778 preserve string tokens in both translated and untranslated
15782 cp_parser_cache_group (cp_parser *parser,
15783 cp_token_cache *cache,
15784 enum cpp_ttype end,
15787 int saved_c_lex_string_translate;
15789 saved_c_lex_string_translate = c_lex_string_translate;
15790 c_lex_string_translate = -1;
15792 cp_parser_cache_group_1 (parser, cache, end, depth);
15794 c_lex_string_translate = saved_c_lex_string_translate;
15798 /* Begin parsing tentatively. We always save tokens while parsing
15799 tentatively so that if the tentative parsing fails we can restore the
15803 cp_parser_parse_tentatively (cp_parser* parser)
15805 /* Enter a new parsing context. */
15806 parser->context = cp_parser_context_new (parser->context);
15807 /* Begin saving tokens. */
15808 cp_lexer_save_tokens (parser->lexer);
15809 /* In order to avoid repetitive access control error messages,
15810 access checks are queued up until we are no longer parsing
15812 push_deferring_access_checks (dk_deferred);
15815 /* Commit to the currently active tentative parse. */
15818 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15820 cp_parser_context *context;
15823 /* Mark all of the levels as committed. */
15824 lexer = parser->lexer;
15825 for (context = parser->context; context->next; context = context->next)
15827 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15829 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15830 while (!cp_lexer_saving_tokens (lexer))
15831 lexer = lexer->next;
15832 cp_lexer_commit_tokens (lexer);
15836 /* Abort the currently active tentative parse. All consumed tokens
15837 will be rolled back, and no diagnostics will be issued. */
15840 cp_parser_abort_tentative_parse (cp_parser* parser)
15842 cp_parser_simulate_error (parser);
15843 /* Now, pretend that we want to see if the construct was
15844 successfully parsed. */
15845 cp_parser_parse_definitely (parser);
15848 /* Stop parsing tentatively. If a parse error has occurred, restore the
15849 token stream. Otherwise, commit to the tokens we have consumed.
15850 Returns true if no error occurred; false otherwise. */
15853 cp_parser_parse_definitely (cp_parser* parser)
15855 bool error_occurred;
15856 cp_parser_context *context;
15858 /* Remember whether or not an error occurred, since we are about to
15859 destroy that information. */
15860 error_occurred = cp_parser_error_occurred (parser);
15861 /* Remove the topmost context from the stack. */
15862 context = parser->context;
15863 parser->context = context->next;
15864 /* If no parse errors occurred, commit to the tentative parse. */
15865 if (!error_occurred)
15867 /* Commit to the tokens read tentatively, unless that was
15869 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15870 cp_lexer_commit_tokens (parser->lexer);
15872 pop_to_parent_deferring_access_checks ();
15874 /* Otherwise, if errors occurred, roll back our state so that things
15875 are just as they were before we began the tentative parse. */
15878 cp_lexer_rollback_tokens (parser->lexer);
15879 pop_deferring_access_checks ();
15881 /* Add the context to the front of the free list. */
15882 context->next = cp_parser_context_free_list;
15883 cp_parser_context_free_list = context;
15885 return !error_occurred;
15888 /* Returns true if we are parsing tentatively -- but have decided that
15889 we will stick with this tentative parse, even if errors occur. */
15892 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15894 return (cp_parser_parsing_tentatively (parser)
15895 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15898 /* Returns nonzero iff an error has occurred during the most recent
15899 tentative parse. */
15902 cp_parser_error_occurred (cp_parser* parser)
15904 return (cp_parser_parsing_tentatively (parser)
15905 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15908 /* Returns nonzero if GNU extensions are allowed. */
15911 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15913 return parser->allow_gnu_extensions_p;
15919 static GTY (()) cp_parser *the_parser;
15921 /* External interface. */
15923 /* Parse one entire translation unit. */
15926 c_parse_file (void)
15928 bool error_occurred;
15929 static bool already_called = false;
15931 if (already_called)
15933 sorry ("inter-module optimizations not implemented for C++");
15936 already_called = true;
15938 the_parser = cp_parser_new ();
15939 push_deferring_access_checks (flag_access_control
15940 ? dk_no_deferred : dk_no_check);
15941 error_occurred = cp_parser_translation_unit (the_parser);
15945 /* This variable must be provided by every front end. */
15949 #include "gt-cp-parser.h"