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"
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
68 typedef struct cp_token GTY (())
70 /* The kind of token. */
71 ENUM_BITFIELD (cpp_ttype) type : 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 ENUM_BITFIELD (rid) keyword : 8;
77 /* The value associated with this token, if any. */
79 /* The location at which this token was found. */
83 /* The number of tokens in a single token block.
84 Computed so that cp_token_block fits in a 512B allocation unit. */
86 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
88 /* A group of tokens. These groups are chained together to store
89 large numbers of tokens. (For example, a token block is created
90 when the body of an inline member function is first encountered;
91 the tokens are processed later after the class definition is
94 This somewhat ungainly data structure (as opposed to, say, a
95 variable-length array), is used due to constraints imposed by the
96 current garbage-collection methodology. If it is made more
97 flexible, we could perhaps simplify the data structures involved. */
99 typedef struct cp_token_block GTY (())
102 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103 /* The number of tokens in this block. */
105 /* The next token block in the chain. */
106 struct cp_token_block *next;
107 /* The previous block in the chain. */
108 struct cp_token_block *prev;
111 typedef struct cp_token_cache GTY (())
113 /* The first block in the cache. NULL if there are no tokens in the
115 cp_token_block *first;
116 /* The last block in the cache. NULL If there are no tokens in the
118 cp_token_block *last;
123 static cp_token_cache *cp_token_cache_new
125 static void cp_token_cache_push_token
126 (cp_token_cache *, cp_token *);
128 /* Create a new cp_token_cache. */
130 static cp_token_cache *
131 cp_token_cache_new (void)
133 return ggc_alloc_cleared (sizeof (cp_token_cache));
136 /* Add *TOKEN to *CACHE. */
139 cp_token_cache_push_token (cp_token_cache *cache,
142 cp_token_block *b = cache->last;
144 /* See if we need to allocate a new token block. */
145 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
147 b = ggc_alloc_cleared (sizeof (cp_token_block));
148 b->prev = cache->last;
151 cache->last->next = b;
155 cache->first = cache->last = b;
157 /* Add this token to the current token block. */
158 b->tokens[b->num_tokens++] = *token;
161 /* The cp_lexer structure represents the C++ lexer. It is responsible
162 for managing the token stream from the preprocessor and supplying
165 typedef struct cp_lexer GTY (())
167 /* The memory allocated for the buffer. Never NULL. */
168 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169 /* A pointer just past the end of the memory allocated for the buffer. */
170 cp_token * GTY ((skip (""))) buffer_end;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token * GTY ((skip (""))) first_token;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token * GTY ((skip (""))) next_token;
176 /* A pointer just past the last available token. If FIRST_TOKEN is
177 NULL, however, there are no available tokens, and then this
178 location is simply the place in which the next token read will be
179 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180 When the LAST_TOKEN == BUFFER, then the last token is at the
181 highest memory address in the BUFFER. */
182 cp_token * GTY ((skip (""))) last_token;
184 /* A stack indicating positions at which cp_lexer_save_tokens was
185 called. The top entry is the most recent position at which we
186 began saving tokens. The entries are differences in token
187 position between FIRST_TOKEN and the first saved token.
189 If the stack is non-empty, we are saving tokens. When a token is
190 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191 pointer will not. The token stream will be preserved so that it
192 can be reexamined later.
194 If the stack is empty, then we are not saving tokens. Whenever a
195 token is consumed, the FIRST_TOKEN pointer will be moved, and the
196 consumed token will be gone forever. */
197 varray_type saved_tokens;
199 /* The STRING_CST tokens encountered while processing the current
201 varray_type string_tokens;
203 /* True if we should obtain more tokens from the preprocessor; false
204 if we are processing a saved token cache. */
207 /* True if we should output debugging information. */
210 /* The next lexer in a linked list of lexers. */
211 struct cp_lexer *next;
216 static cp_lexer *cp_lexer_new_main
218 static cp_lexer *cp_lexer_new_from_tokens
219 (struct cp_token_cache *);
220 static int cp_lexer_saving_tokens
222 static cp_token *cp_lexer_next_token
223 (cp_lexer *, cp_token *);
224 static cp_token *cp_lexer_prev_token
225 (cp_lexer *, cp_token *);
226 static ptrdiff_t cp_lexer_token_difference
227 (cp_lexer *, cp_token *, cp_token *);
228 static cp_token *cp_lexer_read_token
230 static void cp_lexer_maybe_grow_buffer
232 static void cp_lexer_get_preprocessor_token
233 (cp_lexer *, cp_token *);
234 static cp_token *cp_lexer_peek_token
236 static cp_token *cp_lexer_peek_nth_token
237 (cp_lexer *, size_t);
238 static inline bool cp_lexer_next_token_is
239 (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_not
241 (cp_lexer *, enum cpp_ttype);
242 static bool cp_lexer_next_token_is_keyword
243 (cp_lexer *, enum rid);
244 static cp_token *cp_lexer_consume_token
246 static void cp_lexer_purge_token
248 static void cp_lexer_purge_tokens_after
249 (cp_lexer *, cp_token *);
250 static void cp_lexer_save_tokens
252 static void cp_lexer_commit_tokens
254 static void cp_lexer_rollback_tokens
256 static inline void cp_lexer_set_source_position_from_token
257 (cp_lexer *, const cp_token *);
258 static void cp_lexer_print_token
259 (FILE *, cp_token *);
260 static inline bool cp_lexer_debugging_p
262 static void cp_lexer_start_debugging
263 (cp_lexer *) ATTRIBUTE_UNUSED;
264 static void cp_lexer_stop_debugging
265 (cp_lexer *) ATTRIBUTE_UNUSED;
267 /* Manifest constants. */
269 #define CP_TOKEN_BUFFER_SIZE 5
270 #define CP_SAVED_TOKENS_SIZE 5
272 /* A token type for keywords, as opposed to ordinary identifiers. */
273 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
275 /* A token type for template-ids. If a template-id is processed while
276 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277 the value of the CPP_TEMPLATE_ID is whatever was returned by
278 cp_parser_template_id. */
279 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
281 /* A token type for nested-name-specifiers. If a
282 nested-name-specifier is processed while parsing tentatively, it is
283 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285 cp_parser_nested_name_specifier_opt. */
286 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
288 /* A token type for tokens that are not tokens at all; these are used
289 to mark the end of a token block. */
290 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
294 /* The stream to which debugging output should be written. */
295 static FILE *cp_lexer_debug_stream;
297 /* Create a new main C++ lexer, the lexer that gets tokens from the
301 cp_lexer_new_main (void)
304 cp_token first_token;
306 /* It's possible that lexing the first token will load a PCH file,
307 which is a GC collection point. So we have to grab the first
308 token before allocating any memory. */
309 cp_lexer_get_preprocessor_token (NULL, &first_token);
310 c_common_no_more_pch ();
312 /* Allocate the memory. */
313 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
315 /* Create the circular buffer. */
316 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
317 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
319 /* There is one token in the buffer. */
320 lexer->last_token = lexer->buffer + 1;
321 lexer->first_token = lexer->buffer;
322 lexer->next_token = lexer->buffer;
323 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
325 /* This lexer obtains more tokens by calling c_lex. */
326 lexer->main_lexer_p = true;
328 /* Create the SAVED_TOKENS stack. */
329 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
331 /* Create the STRINGS array. */
332 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
334 /* Assume we are not debugging. */
335 lexer->debugging_p = false;
340 /* Create a new lexer whose token stream is primed with the TOKENS.
341 When these tokens are exhausted, no new tokens will be read. */
344 cp_lexer_new_from_tokens (cp_token_cache *tokens)
348 cp_token_block *block;
349 ptrdiff_t num_tokens;
351 /* Allocate the memory. */
352 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
354 /* Create a new buffer, appropriately sized. */
356 for (block = tokens->first; block != NULL; block = block->next)
357 num_tokens += block->num_tokens;
358 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
359 lexer->buffer_end = lexer->buffer + num_tokens;
361 /* Install the tokens. */
362 token = lexer->buffer;
363 for (block = tokens->first; block != NULL; block = block->next)
365 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
366 token += block->num_tokens;
369 /* The FIRST_TOKEN is the beginning of the buffer. */
370 lexer->first_token = lexer->buffer;
371 /* The next available token is also at the beginning of the buffer. */
372 lexer->next_token = lexer->buffer;
373 /* The buffer is full. */
374 lexer->last_token = lexer->first_token;
376 /* This lexer doesn't obtain more tokens. */
377 lexer->main_lexer_p = false;
379 /* Create the SAVED_TOKENS stack. */
380 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
382 /* Create the STRINGS array. */
383 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
385 /* Assume we are not debugging. */
386 lexer->debugging_p = false;
391 /* Returns nonzero if debugging information should be output. */
394 cp_lexer_debugging_p (cp_lexer *lexer)
396 return lexer->debugging_p;
399 /* Set the current source position from the information stored in
403 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
404 const cp_token *token)
406 /* Ideally, the source position information would not be a global
407 variable, but it is. */
409 /* Update the line number. */
410 if (token->type != CPP_EOF)
411 input_location = token->location;
414 /* TOKEN points into the circular token buffer. Return a pointer to
415 the next token in the buffer. */
417 static inline cp_token *
418 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
421 if (token == lexer->buffer_end)
422 token = lexer->buffer;
426 /* TOKEN points into the circular token buffer. Return a pointer to
427 the previous token in the buffer. */
429 static inline cp_token *
430 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
432 if (token == lexer->buffer)
433 token = lexer->buffer_end;
437 /* nonzero if we are presently saving tokens. */
440 cp_lexer_saving_tokens (const cp_lexer* lexer)
442 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
445 /* Return a pointer to the token that is N tokens beyond TOKEN in the
449 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
452 if (token >= lexer->buffer_end)
453 token = lexer->buffer + (token - lexer->buffer_end);
457 /* Returns the number of times that START would have to be incremented
458 to reach FINISH. If START and FINISH are the same, returns zero. */
461 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
464 return finish - start;
466 return ((lexer->buffer_end - lexer->buffer)
470 /* Obtain another token from the C preprocessor and add it to the
471 token buffer. Returns the newly read token. */
474 cp_lexer_read_token (cp_lexer* lexer)
478 /* Make sure there is room in the buffer. */
479 cp_lexer_maybe_grow_buffer (lexer);
481 /* If there weren't any tokens, then this one will be the first. */
482 if (!lexer->first_token)
483 lexer->first_token = lexer->last_token;
484 /* Similarly, if there were no available tokens, there is one now. */
485 if (!lexer->next_token)
486 lexer->next_token = lexer->last_token;
488 /* Figure out where we're going to store the new token. */
489 token = lexer->last_token;
491 /* Get a new token from the preprocessor. */
492 cp_lexer_get_preprocessor_token (lexer, token);
494 /* Increment LAST_TOKEN. */
495 lexer->last_token = cp_lexer_next_token (lexer, token);
497 /* Strings should have type `const char []'. Right now, we will
498 have an ARRAY_TYPE that is constant rather than an array of
500 FIXME: Make fix_string_type get this right in the first place. */
501 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
502 && flag_const_strings)
506 /* Get the current type. It will be an ARRAY_TYPE. */
507 type = TREE_TYPE (token->value);
508 /* Use build_cplus_array_type to rebuild the array, thereby
509 getting the right type. */
510 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511 /* Reset the type of the token. */
512 TREE_TYPE (token->value) = type;
518 /* If the circular buffer is full, make it bigger. */
521 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
523 /* If the buffer is full, enlarge it. */
524 if (lexer->last_token == lexer->first_token)
526 cp_token *new_buffer;
527 cp_token *old_buffer;
528 cp_token *new_first_token;
529 ptrdiff_t buffer_length;
530 size_t num_tokens_to_copy;
532 /* Remember the current buffer pointer. It will become invalid,
533 but we will need to do pointer arithmetic involving this
535 old_buffer = lexer->buffer;
536 /* Compute the current buffer size. */
537 buffer_length = lexer->buffer_end - lexer->buffer;
538 /* Allocate a buffer twice as big. */
539 new_buffer = ggc_realloc (lexer->buffer,
540 2 * buffer_length * sizeof (cp_token));
542 /* Because the buffer is circular, logically consecutive tokens
543 are not necessarily placed consecutively in memory.
544 Therefore, we must keep move the tokens that were before
545 FIRST_TOKEN to the second half of the newly allocated
547 num_tokens_to_copy = (lexer->first_token - old_buffer);
548 memcpy (new_buffer + buffer_length,
550 num_tokens_to_copy * sizeof (cp_token));
551 /* Clear the rest of the buffer. We never look at this storage,
552 but the garbage collector may. */
553 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
554 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
556 /* Now recompute all of the buffer pointers. */
558 = new_buffer + (lexer->first_token - old_buffer);
559 if (lexer->next_token != NULL)
561 ptrdiff_t next_token_delta;
563 if (lexer->next_token > lexer->first_token)
564 next_token_delta = lexer->next_token - lexer->first_token;
567 buffer_length - (lexer->first_token - lexer->next_token);
568 lexer->next_token = new_first_token + next_token_delta;
570 lexer->last_token = new_first_token + buffer_length;
571 lexer->buffer = new_buffer;
572 lexer->buffer_end = new_buffer + buffer_length * 2;
573 lexer->first_token = new_first_token;
577 /* Store the next token from the preprocessor in *TOKEN. */
580 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
586 if (lexer != NULL && !lexer->main_lexer_p)
588 token->type = CPP_EOF;
589 token->location.line = 0;
590 token->location.file = NULL;
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
598 /* Keep going until we get a token we like. */
601 /* Get a new token from the preprocessor. */
602 token->type = c_lex_with_flags (&token->value, &token->flags);
603 /* Issue messages about tokens we cannot process. */
609 error ("invalid token");
613 /* This is a good token, so we exit the loop. */
618 /* Now we've got our token. */
619 token->location = input_location;
621 /* Check to see if this token is a keyword. */
622 if (token->type == CPP_NAME
623 && C_IS_RESERVED_WORD (token->value))
625 /* Mark this token as a keyword. */
626 token->type = CPP_KEYWORD;
627 /* Record which keyword. */
628 token->keyword = C_RID_CODE (token->value);
629 /* Update the value. Some keywords are mapped to particular
630 entities, rather than simply having the value of the
631 corresponding IDENTIFIER_NODE. For example, `__const' is
632 mapped to `const'. */
633 token->value = ridpointers[token->keyword];
636 token->keyword = RID_MAX;
639 /* Return a pointer to the next token in the token stream, but do not
643 cp_lexer_peek_token (cp_lexer* lexer)
647 /* If there are no tokens, read one now. */
648 if (!lexer->next_token)
649 cp_lexer_read_token (lexer);
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
654 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656 fprintf (cp_lexer_debug_stream, "\n");
659 token = lexer->next_token;
660 cp_lexer_set_source_position_from_token (lexer, token);
664 /* Return true if the next token has the indicated TYPE. */
667 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
671 /* Peek at the next token. */
672 token = cp_lexer_peek_token (lexer);
673 /* Check to see if it has the indicated TYPE. */
674 return token->type == type;
677 /* Return true if the next token does not have the indicated TYPE. */
680 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
682 return !cp_lexer_next_token_is (lexer, type);
685 /* Return true if the next token is the indicated KEYWORD. */
688 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
692 /* Peek at the next token. */
693 token = cp_lexer_peek_token (lexer);
694 /* Check to see if it is the indicated keyword. */
695 return token->keyword == keyword;
698 /* Return a pointer to the Nth token in the token stream. If N is 1,
699 then this is precisely equivalent to cp_lexer_peek_token. */
702 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
706 /* N is 1-based, not zero-based. */
707 my_friendly_assert (n > 0, 20000224);
709 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
710 token = lexer->next_token;
711 /* If there are no tokens in the buffer, get one now. */
714 cp_lexer_read_token (lexer);
715 token = lexer->next_token;
718 /* Now, read tokens until we have enough. */
721 /* Advance to the next token. */
722 token = cp_lexer_next_token (lexer, token);
723 /* If that's all the tokens we have, read a new one. */
724 if (token == lexer->last_token)
725 token = cp_lexer_read_token (lexer);
731 /* Consume the next token. The pointer returned is valid only until
732 another token is read. Callers should preserve copy the token
733 explicitly if they will need its value for a longer period of
737 cp_lexer_consume_token (cp_lexer* lexer)
741 /* If there are no tokens, read one now. */
742 if (!lexer->next_token)
743 cp_lexer_read_token (lexer);
745 /* Remember the token we'll be returning. */
746 token = lexer->next_token;
748 /* Increment NEXT_TOKEN. */
749 lexer->next_token = cp_lexer_next_token (lexer,
751 /* Check to see if we're all out of tokens. */
752 if (lexer->next_token == lexer->last_token)
753 lexer->next_token = NULL;
755 /* If we're not saving tokens, then move FIRST_TOKEN too. */
756 if (!cp_lexer_saving_tokens (lexer))
758 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
759 if (!lexer->next_token)
760 lexer->first_token = NULL;
762 lexer->first_token = lexer->next_token;
765 /* Provide debugging output. */
766 if (cp_lexer_debugging_p (lexer))
768 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 fprintf (cp_lexer_debug_stream, "\n");
776 /* Permanently remove the next token from the token stream. There
777 must be a valid next token already; this token never reads
778 additional tokens from the preprocessor. */
781 cp_lexer_purge_token (cp_lexer *lexer)
784 cp_token *next_token;
786 token = lexer->next_token;
789 next_token = cp_lexer_next_token (lexer, token);
790 if (next_token == lexer->last_token)
792 *token = *next_token;
796 lexer->last_token = token;
797 /* The token purged may have been the only token remaining; if so,
799 if (lexer->next_token == token)
800 lexer->next_token = NULL;
803 /* Permanently remove all tokens after TOKEN, up to, but not
804 including, the token that will be returned next by
805 cp_lexer_peek_token. */
808 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
814 if (lexer->next_token)
816 /* Copy the tokens that have not yet been read to the location
817 immediately following TOKEN. */
818 t1 = cp_lexer_next_token (lexer, token);
819 t2 = peek = cp_lexer_peek_token (lexer);
820 /* Move tokens into the vacant area between TOKEN and PEEK. */
821 while (t2 != lexer->last_token)
824 t1 = cp_lexer_next_token (lexer, t1);
825 t2 = cp_lexer_next_token (lexer, t2);
827 /* Now, the next available token is right after TOKEN. */
828 lexer->next_token = cp_lexer_next_token (lexer, token);
829 /* And the last token is wherever we ended up. */
830 lexer->last_token = t1;
834 /* There are no tokens in the buffer, so there is nothing to
835 copy. The last token in the buffer is TOKEN itself. */
836 lexer->last_token = cp_lexer_next_token (lexer, token);
840 /* Begin saving tokens. All tokens consumed after this point will be
844 cp_lexer_save_tokens (cp_lexer* lexer)
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
850 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851 restore the tokens if required. */
852 if (!lexer->next_token)
853 cp_lexer_read_token (lexer);
855 VARRAY_PUSH_INT (lexer->saved_tokens,
856 cp_lexer_token_difference (lexer,
861 /* Commit to the portion of the token stream most recently saved. */
864 cp_lexer_commit_tokens (cp_lexer* lexer)
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
870 VARRAY_POP (lexer->saved_tokens);
873 /* Return all tokens saved since the last call to cp_lexer_save_tokens
874 to the token stream. Stop saving tokens. */
877 cp_lexer_rollback_tokens (cp_lexer* lexer)
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer))
883 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
885 /* Find the token that was the NEXT_TOKEN when we started saving
887 delta = VARRAY_TOP_INT(lexer->saved_tokens);
888 /* Make it the next token again now. */
889 lexer->next_token = cp_lexer_advance_token (lexer,
892 /* It might be the case that there were no tokens when we started
893 saving tokens, but that there are some tokens now. */
894 if (!lexer->next_token && lexer->first_token)
895 lexer->next_token = lexer->first_token;
897 /* Stop saving tokens. */
898 VARRAY_POP (lexer->saved_tokens);
901 /* Print a representation of the TOKEN on the STREAM. */
904 cp_lexer_print_token (FILE * stream, cp_token* token)
906 const char *token_type = NULL;
908 /* Figure out what kind of token this is. */
916 token_type = "COMMA";
920 token_type = "OPEN_PAREN";
923 case CPP_CLOSE_PAREN:
924 token_type = "CLOSE_PAREN";
928 token_type = "OPEN_BRACE";
931 case CPP_CLOSE_BRACE:
932 token_type = "CLOSE_BRACE";
936 token_type = "SEMICOLON";
948 token_type = "keyword";
951 /* This is not a token that we know how to handle yet. */
956 /* If we have a name for the token, print it out. Otherwise, we
957 simply give the numeric code. */
959 fprintf (stream, "%s", token_type);
961 fprintf (stream, "%d", token->type);
962 /* And, for an identifier, print the identifier name. */
963 if (token->type == CPP_NAME
964 /* Some keywords have a value that is not an IDENTIFIER_NODE.
965 For example, `struct' is mapped to an INTEGER_CST. */
966 || (token->type == CPP_KEYWORD
967 && TREE_CODE (token->value) == IDENTIFIER_NODE))
968 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
971 /* Start emitting debugging information. */
974 cp_lexer_start_debugging (cp_lexer* lexer)
976 ++lexer->debugging_p;
979 /* Stop emitting debugging information. */
982 cp_lexer_stop_debugging (cp_lexer* lexer)
984 --lexer->debugging_p;
993 A cp_parser parses the token stream as specified by the C++
994 grammar. Its job is purely parsing, not semantic analysis. For
995 example, the parser breaks the token stream into declarators,
996 expressions, statements, and other similar syntactic constructs.
997 It does not check that the types of the expressions on either side
998 of an assignment-statement are compatible, or that a function is
999 not declared with a parameter of type `void'.
1001 The parser invokes routines elsewhere in the compiler to perform
1002 semantic analysis and to build up the abstract syntax tree for the
1005 The parser (and the template instantiation code, which is, in a
1006 way, a close relative of parsing) are the only parts of the
1007 compiler that should be calling push_scope and pop_scope, or
1008 related functions. The parser (and template instantiation code)
1009 keeps track of what scope is presently active; everything else
1010 should simply honor that. (The code that generates static
1011 initializers may also need to set the scope, in order to check
1012 access control correctly when emitting the initializers.)
1017 The parser is of the standard recursive-descent variety. Upcoming
1018 tokens in the token stream are examined in order to determine which
1019 production to use when parsing a non-terminal. Some C++ constructs
1020 require arbitrary look ahead to disambiguate. For example, it is
1021 impossible, in the general case, to tell whether a statement is an
1022 expression or declaration without scanning the entire statement.
1023 Therefore, the parser is capable of "parsing tentatively." When the
1024 parser is not sure what construct comes next, it enters this mode.
1025 Then, while we attempt to parse the construct, the parser queues up
1026 error messages, rather than issuing them immediately, and saves the
1027 tokens it consumes. If the construct is parsed successfully, the
1028 parser "commits", i.e., it issues any queued error messages and
1029 the tokens that were being preserved are permanently discarded.
1030 If, however, the construct is not parsed successfully, the parser
1031 rolls back its state completely so that it can resume parsing using
1032 a different alternative.
1037 The performance of the parser could probably be improved
1038 substantially. Some possible improvements include:
1040 - The expression parser recurses through the various levels of
1041 precedence as specified in the grammar, rather than using an
1042 operator-precedence technique. Therefore, parsing a simple
1043 identifier requires multiple recursive calls.
1045 - We could often eliminate the need to parse tentatively by
1046 looking ahead a little bit. In some places, this approach
1047 might not entirely eliminate the need to parse tentatively, but
1048 it might still speed up the average case. */
1050 /* Flags that are passed to some parsing functions. These values can
1051 be bitwise-ored together. */
1053 typedef enum cp_parser_flags
1056 CP_PARSER_FLAGS_NONE = 0x0,
1057 /* The construct is optional. If it is not present, then no error
1058 should be issued. */
1059 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060 /* When parsing a type-specifier, do not allow user-defined types. */
1061 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1064 /* The different kinds of declarators we want to parse. */
1066 typedef enum cp_parser_declarator_kind
1068 /* We want an abstract declarator. */
1069 CP_PARSER_DECLARATOR_ABSTRACT,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED,
1072 /* We don't mind, but the name must be an unqualified-id. */
1073 CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind;
1076 /* A mapping from a token type to a corresponding tree node type. */
1078 typedef struct cp_parser_token_tree_map_node
1080 /* The token type. */
1081 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1082 /* The corresponding tree code. */
1083 ENUM_BITFIELD (tree_code) tree_type : 8;
1084 } cp_parser_token_tree_map_node;
1086 /* A complete map consists of several ordinary entries, followed by a
1087 terminator. The terminating entry has a token_type of CPP_EOF. */
1089 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1091 /* The status of a tentative parse. */
1093 typedef enum cp_parser_status_kind
1095 /* No errors have occurred. */
1096 CP_PARSER_STATUS_KIND_NO_ERROR,
1097 /* An error has occurred. */
1098 CP_PARSER_STATUS_KIND_ERROR,
1099 /* We are committed to this tentative parse, whether or not an error
1101 CP_PARSER_STATUS_KIND_COMMITTED
1102 } cp_parser_status_kind;
1104 /* Context that is saved and restored when parsing tentatively. */
1106 typedef struct cp_parser_context GTY (())
1108 /* If this is a tentative parsing context, the status of the
1110 enum cp_parser_status_kind status;
1111 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1112 that are looked up in this context must be looked up both in the
1113 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114 the context of the containing expression. */
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context *next;
1118 } cp_parser_context;
1122 /* Constructors and destructors. */
1124 static cp_parser_context *cp_parser_context_new
1125 (cp_parser_context *);
1127 /* Class variables. */
1129 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1131 /* Constructors and destructors. */
1133 /* Construct a new context. The context below this one on the stack
1134 is given by NEXT. */
1136 static cp_parser_context *
1137 cp_parser_context_new (cp_parser_context* next)
1139 cp_parser_context *context;
1141 /* Allocate the storage. */
1142 if (cp_parser_context_free_list != NULL)
1144 /* Pull the first entry from the free list. */
1145 context = cp_parser_context_free_list;
1146 cp_parser_context_free_list = context->next;
1147 memset (context, 0, sizeof (*context));
1150 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1151 /* No errors have occurred yet in this context. */
1152 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153 /* If this is not the bottomost context, copy information that we
1154 need from the previous context. */
1157 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158 expression, then we are parsing one in this context, too. */
1159 context->object_type = next->object_type;
1160 /* Thread the stack. */
1161 context->next = next;
1167 /* The cp_parser structure represents the C++ parser. */
1169 typedef struct cp_parser GTY(())
1171 /* The lexer from which we are obtaining tokens. */
1174 /* The scope in which names should be looked up. If NULL_TREE, then
1175 we look up names in the scope that is currently open in the
1176 source program. If non-NULL, this is either a TYPE or
1177 NAMESPACE_DECL for the scope in which we should look.
1179 This value is not cleared automatically after a name is looked
1180 up, so we must be careful to clear it before starting a new look
1181 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1182 will look up `Z' in the scope of `X', rather than the current
1183 scope.) Unfortunately, it is difficult to tell when name lookup
1184 is complete, because we sometimes peek at a token, look it up,
1185 and then decide not to consume it. */
1188 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189 last lookup took place. OBJECT_SCOPE is used if an expression
1190 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191 respectively. QUALIFYING_SCOPE is used for an expression of the
1192 form "X::Y"; it refers to X. */
1194 tree qualifying_scope;
1196 /* A stack of parsing contexts. All but the bottom entry on the
1197 stack will be tentative contexts.
1199 We parse tentatively in order to determine which construct is in
1200 use in some situations. For example, in order to determine
1201 whether a statement is an expression-statement or a
1202 declaration-statement we parse it tentatively as a
1203 declaration-statement. If that fails, we then reparse the same
1204 token stream as an expression-statement. */
1205 cp_parser_context *context;
1207 /* True if we are parsing GNU C++. If this flag is not set, then
1208 GNU extensions are not recognized. */
1209 bool allow_gnu_extensions_p;
1211 /* TRUE if the `>' token should be interpreted as the greater-than
1212 operator. FALSE if it is the end of a template-id or
1213 template-parameter-list. */
1214 bool greater_than_is_operator_p;
1216 /* TRUE if default arguments are allowed within a parameter list
1217 that starts at this point. FALSE if only a gnu extension makes
1218 them permissible. */
1219 bool default_arg_ok_p;
1221 /* TRUE if we are parsing an integral constant-expression. See
1222 [expr.const] for a precise definition. */
1223 bool integral_constant_expression_p;
1225 /* TRUE if we are parsing an integral constant-expression -- but a
1226 non-constant expression should be permitted as well. This flag
1227 is used when parsing an array bound so that GNU variable-length
1228 arrays are tolerated. */
1229 bool allow_non_integral_constant_expression_p;
1231 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232 been seen that makes the expression non-constant. */
1233 bool non_integral_constant_expression_p;
1235 /* TRUE if we are parsing the argument to "__offsetof__". */
1238 /* TRUE if local variable names and `this' are forbidden in the
1240 bool local_variables_forbidden_p;
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1245 bool in_unbraced_linkage_specification_p;
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p;
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p;
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p;
1258 /* TRUE if we are presently parsing the body of a switch
1260 bool in_switch_statement_p;
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1265 bool in_type_id_in_expr_p;
1267 /* If non-NULL, then we are parsing a construct where new type
1268 definitions are not permitted. The string stored here will be
1269 issued as an error message if a type is defined. */
1270 const char *type_definition_forbidden_message;
1272 /* A list of lists. The outer list is a stack, used for member
1273 functions of local classes. At each level there are two sub-list,
1274 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1275 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1276 TREE_VALUE's. The functions are chained in reverse declaration
1279 The TREE_PURPOSE sublist contains those functions with default
1280 arguments that need post processing, and the TREE_VALUE sublist
1281 contains those functions with definitions that need post
1284 These lists can only be processed once the outermost class being
1285 defined is complete. */
1286 tree unparsed_functions_queues;
1288 /* The number of classes whose definitions are currently in
1290 unsigned num_classes_being_defined;
1292 /* The number of template parameter lists that apply directly to the
1293 current declaration. */
1294 unsigned num_template_parameter_lists;
1297 /* The type of a function that parses some kind of expression. */
1298 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1302 /* Constructors and destructors. */
1304 static cp_parser *cp_parser_new
1307 /* Routines to parse various constructs.
1309 Those that return `tree' will return the error_mark_node (rather
1310 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1311 Sometimes, they will return an ordinary node if error-recovery was
1312 attempted, even though a parse error occurred. So, to check
1313 whether or not a parse error occurred, you should always use
1314 cp_parser_error_occurred. If the construct is optional (indicated
1315 either by an `_opt' in the name of the function that does the
1316 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1317 the construct is not present. */
1319 /* Lexical conventions [gram.lex] */
1321 static tree cp_parser_identifier
1324 /* Basic concepts [gram.basic] */
1326 static bool cp_parser_translation_unit
1329 /* Expressions [gram.expr] */
1331 static tree cp_parser_primary_expression
1332 (cp_parser *, cp_id_kind *, tree *);
1333 static tree cp_parser_id_expression
1334 (cp_parser *, bool, bool, bool *, bool);
1335 static tree cp_parser_unqualified_id
1336 (cp_parser *, bool, bool, bool);
1337 static tree cp_parser_nested_name_specifier_opt
1338 (cp_parser *, bool, bool, bool, bool);
1339 static tree cp_parser_nested_name_specifier
1340 (cp_parser *, bool, bool, bool, bool);
1341 static tree cp_parser_class_or_namespace_name
1342 (cp_parser *, bool, bool, bool, bool, bool);
1343 static tree cp_parser_postfix_expression
1344 (cp_parser *, bool);
1345 static tree cp_parser_parenthesized_expression_list
1346 (cp_parser *, bool, bool *);
1347 static void cp_parser_pseudo_destructor_name
1348 (cp_parser *, tree *, tree *);
1349 static tree cp_parser_unary_expression
1350 (cp_parser *, bool);
1351 static enum tree_code cp_parser_unary_operator
1353 static tree cp_parser_new_expression
1355 static tree cp_parser_new_placement
1357 static tree cp_parser_new_type_id
1359 static tree cp_parser_new_declarator_opt
1361 static tree cp_parser_direct_new_declarator
1363 static tree cp_parser_new_initializer
1365 static tree cp_parser_delete_expression
1367 static tree cp_parser_cast_expression
1368 (cp_parser *, bool);
1369 static tree cp_parser_pm_expression
1371 static tree cp_parser_multiplicative_expression
1373 static tree cp_parser_additive_expression
1375 static tree cp_parser_shift_expression
1377 static tree cp_parser_relational_expression
1379 static tree cp_parser_equality_expression
1381 static tree cp_parser_and_expression
1383 static tree cp_parser_exclusive_or_expression
1385 static tree cp_parser_inclusive_or_expression
1387 static tree cp_parser_logical_and_expression
1389 static tree cp_parser_logical_or_expression
1391 static tree cp_parser_question_colon_clause
1392 (cp_parser *, tree);
1393 static tree cp_parser_assignment_expression
1395 static enum tree_code cp_parser_assignment_operator_opt
1397 static tree cp_parser_expression
1399 static tree cp_parser_constant_expression
1400 (cp_parser *, bool, bool *);
1402 /* Statements [gram.stmt.stmt] */
1404 static void cp_parser_statement
1405 (cp_parser *, bool);
1406 static tree cp_parser_labeled_statement
1407 (cp_parser *, bool);
1408 static tree cp_parser_expression_statement
1409 (cp_parser *, bool);
1410 static tree cp_parser_compound_statement
1411 (cp_parser *, bool);
1412 static void cp_parser_statement_seq_opt
1413 (cp_parser *, bool);
1414 static tree cp_parser_selection_statement
1416 static tree cp_parser_condition
1418 static tree cp_parser_iteration_statement
1420 static void cp_parser_for_init_statement
1422 static tree cp_parser_jump_statement
1424 static void cp_parser_declaration_statement
1427 static tree cp_parser_implicitly_scoped_statement
1429 static void cp_parser_already_scoped_statement
1432 /* Declarations [gram.dcl.dcl] */
1434 static void cp_parser_declaration_seq_opt
1436 static void cp_parser_declaration
1438 static void cp_parser_block_declaration
1439 (cp_parser *, bool);
1440 static void cp_parser_simple_declaration
1441 (cp_parser *, bool);
1442 static tree cp_parser_decl_specifier_seq
1443 (cp_parser *, cp_parser_flags, tree *, int *);
1444 static tree cp_parser_storage_class_specifier_opt
1446 static tree cp_parser_function_specifier_opt
1448 static tree cp_parser_type_specifier
1449 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1450 static tree cp_parser_simple_type_specifier
1451 (cp_parser *, cp_parser_flags, bool);
1452 static tree cp_parser_type_name
1454 static tree cp_parser_elaborated_type_specifier
1455 (cp_parser *, bool, bool);
1456 static tree cp_parser_enum_specifier
1458 static void cp_parser_enumerator_list
1459 (cp_parser *, tree);
1460 static void cp_parser_enumerator_definition
1461 (cp_parser *, tree);
1462 static tree cp_parser_namespace_name
1464 static void cp_parser_namespace_definition
1466 static void cp_parser_namespace_body
1468 static tree cp_parser_qualified_namespace_specifier
1470 static void cp_parser_namespace_alias_definition
1472 static void cp_parser_using_declaration
1474 static void cp_parser_using_directive
1476 static void cp_parser_asm_definition
1478 static void cp_parser_linkage_specification
1481 /* Declarators [gram.dcl.decl] */
1483 static tree cp_parser_init_declarator
1484 (cp_parser *, tree, tree, bool, bool, int, bool *);
1485 static tree cp_parser_declarator
1486 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1487 static tree cp_parser_direct_declarator
1488 (cp_parser *, cp_parser_declarator_kind, int *);
1489 static enum tree_code cp_parser_ptr_operator
1490 (cp_parser *, tree *, tree *);
1491 static tree cp_parser_cv_qualifier_seq_opt
1493 static tree cp_parser_cv_qualifier_opt
1495 static tree cp_parser_declarator_id
1497 static tree cp_parser_type_id
1499 static tree cp_parser_type_specifier_seq
1501 static tree cp_parser_parameter_declaration_clause
1503 static tree cp_parser_parameter_declaration_list
1505 static tree cp_parser_parameter_declaration
1506 (cp_parser *, bool, bool *);
1507 static void cp_parser_function_body
1509 static tree cp_parser_initializer
1510 (cp_parser *, bool *, bool *);
1511 static tree cp_parser_initializer_clause
1512 (cp_parser *, bool *);
1513 static tree cp_parser_initializer_list
1514 (cp_parser *, bool *);
1516 static bool cp_parser_ctor_initializer_opt_and_function_body
1519 /* Classes [gram.class] */
1521 static tree cp_parser_class_name
1522 (cp_parser *, bool, bool, bool, bool, bool, bool);
1523 static tree cp_parser_class_specifier
1525 static tree cp_parser_class_head
1526 (cp_parser *, bool *);
1527 static enum tag_types cp_parser_class_key
1529 static void cp_parser_member_specification_opt
1531 static void cp_parser_member_declaration
1533 static tree cp_parser_pure_specifier
1535 static tree cp_parser_constant_initializer
1538 /* Derived classes [gram.class.derived] */
1540 static tree cp_parser_base_clause
1542 static tree cp_parser_base_specifier
1545 /* Special member functions [gram.special] */
1547 static tree cp_parser_conversion_function_id
1549 static tree cp_parser_conversion_type_id
1551 static tree cp_parser_conversion_declarator_opt
1553 static bool cp_parser_ctor_initializer_opt
1555 static void cp_parser_mem_initializer_list
1557 static tree cp_parser_mem_initializer
1559 static tree cp_parser_mem_initializer_id
1562 /* Overloading [gram.over] */
1564 static tree cp_parser_operator_function_id
1566 static tree cp_parser_operator
1569 /* Templates [gram.temp] */
1571 static void cp_parser_template_declaration
1572 (cp_parser *, bool);
1573 static tree cp_parser_template_parameter_list
1575 static tree cp_parser_template_parameter
1577 static tree cp_parser_type_parameter
1579 static tree cp_parser_template_id
1580 (cp_parser *, bool, bool, bool);
1581 static tree cp_parser_template_name
1582 (cp_parser *, bool, bool, bool, bool *);
1583 static tree cp_parser_template_argument_list
1585 static tree cp_parser_template_argument
1587 static void cp_parser_explicit_instantiation
1589 static void cp_parser_explicit_specialization
1592 /* Exception handling [gram.exception] */
1594 static tree cp_parser_try_block
1596 static bool cp_parser_function_try_block
1598 static void cp_parser_handler_seq
1600 static void cp_parser_handler
1602 static tree cp_parser_exception_declaration
1604 static tree cp_parser_throw_expression
1606 static tree cp_parser_exception_specification_opt
1608 static tree cp_parser_type_id_list
1611 /* GNU Extensions */
1613 static tree cp_parser_asm_specification_opt
1615 static tree cp_parser_asm_operand_list
1617 static tree cp_parser_asm_clobber_list
1619 static tree cp_parser_attributes_opt
1621 static tree cp_parser_attribute_list
1623 static bool cp_parser_extension_opt
1624 (cp_parser *, int *);
1625 static void cp_parser_label_declaration
1628 /* Utility Routines */
1630 static tree cp_parser_lookup_name
1631 (cp_parser *, tree, bool, bool, bool, bool);
1632 static tree cp_parser_lookup_name_simple
1633 (cp_parser *, tree);
1634 static tree cp_parser_maybe_treat_template_as_class
1636 static bool cp_parser_check_declarator_template_parameters
1637 (cp_parser *, tree);
1638 static bool cp_parser_check_template_parameters
1639 (cp_parser *, unsigned);
1640 static tree cp_parser_simple_cast_expression
1642 static tree cp_parser_binary_expression
1643 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1644 static tree cp_parser_global_scope_opt
1645 (cp_parser *, bool);
1646 static bool cp_parser_constructor_declarator_p
1647 (cp_parser *, bool);
1648 static tree cp_parser_function_definition_from_specifiers_and_declarator
1649 (cp_parser *, tree, tree, tree);
1650 static tree cp_parser_function_definition_after_declarator
1651 (cp_parser *, bool);
1652 static void cp_parser_template_declaration_after_export
1653 (cp_parser *, bool);
1654 static tree cp_parser_single_declaration
1655 (cp_parser *, bool, bool *);
1656 static tree cp_parser_functional_cast
1657 (cp_parser *, tree);
1658 static tree cp_parser_save_member_function_body
1659 (cp_parser *, tree, tree, tree);
1660 static tree cp_parser_enclosed_template_argument_list
1662 static void cp_parser_save_default_args
1663 (cp_parser *, tree);
1664 static void cp_parser_late_parsing_for_member
1665 (cp_parser *, tree);
1666 static void cp_parser_late_parsing_default_args
1667 (cp_parser *, tree);
1668 static tree cp_parser_sizeof_operand
1669 (cp_parser *, enum rid);
1670 static bool cp_parser_declares_only_class_p
1672 static bool cp_parser_friend_p
1674 static cp_token *cp_parser_require
1675 (cp_parser *, enum cpp_ttype, const char *);
1676 static cp_token *cp_parser_require_keyword
1677 (cp_parser *, enum rid, const char *);
1678 static bool cp_parser_token_starts_function_definition_p
1680 static bool cp_parser_next_token_starts_class_definition_p
1682 static bool cp_parser_next_token_ends_template_argument_p
1684 static bool cp_parser_nth_token_starts_template_argument_list_p
1685 (cp_parser *, size_t);
1686 static enum tag_types cp_parser_token_is_class_key
1688 static void cp_parser_check_class_key
1689 (enum tag_types, tree type);
1690 static void cp_parser_check_access_in_redeclaration
1692 static bool cp_parser_optional_template_keyword
1694 static void cp_parser_pre_parsed_nested_name_specifier
1696 static void cp_parser_cache_group
1697 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1698 static void cp_parser_parse_tentatively
1700 static void cp_parser_commit_to_tentative_parse
1702 static void cp_parser_abort_tentative_parse
1704 static bool cp_parser_parse_definitely
1706 static inline bool cp_parser_parsing_tentatively
1708 static bool cp_parser_committed_to_tentative_parse
1710 static void cp_parser_error
1711 (cp_parser *, const char *);
1712 static void cp_parser_name_lookup_error
1713 (cp_parser *, tree, tree, const char *);
1714 static bool cp_parser_simulate_error
1716 static void cp_parser_check_type_definition
1718 static void cp_parser_check_for_definition_in_return_type
1720 static void cp_parser_check_for_invalid_template_id
1721 (cp_parser *, tree);
1722 static tree cp_parser_non_integral_constant_expression
1724 static bool cp_parser_diagnose_invalid_type_name
1726 static int cp_parser_skip_to_closing_parenthesis
1727 (cp_parser *, bool, bool, bool);
1728 static void cp_parser_skip_to_end_of_statement
1730 static void cp_parser_consume_semicolon_at_end_of_statement
1732 static void cp_parser_skip_to_end_of_block_or_statement
1734 static void cp_parser_skip_to_closing_brace
1736 static void cp_parser_skip_until_found
1737 (cp_parser *, enum cpp_ttype, const char *);
1738 static bool cp_parser_error_occurred
1740 static bool cp_parser_allow_gnu_extensions_p
1742 static bool cp_parser_is_string_literal
1744 static bool cp_parser_is_keyword
1745 (cp_token *, enum rid);
1747 /* Returns nonzero if we are parsing tentatively. */
1750 cp_parser_parsing_tentatively (cp_parser* parser)
1752 return parser->context->next != NULL;
1755 /* Returns nonzero if TOKEN is a string literal. */
1758 cp_parser_is_string_literal (cp_token* token)
1760 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1763 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1766 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1768 return token->keyword == keyword;
1771 /* Issue the indicated error MESSAGE. */
1774 cp_parser_error (cp_parser* parser, const char* message)
1776 /* Output the MESSAGE -- unless we're parsing tentatively. */
1777 if (!cp_parser_simulate_error (parser))
1780 token = cp_lexer_peek_token (parser->lexer);
1781 c_parse_error (message,
1782 /* Because c_parser_error does not understand
1783 CPP_KEYWORD, keywords are treated like
1785 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1790 /* Issue an error about name-lookup failing. NAME is the
1791 IDENTIFIER_NODE DECL is the result of
1792 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1793 the thing that we hoped to find. */
1796 cp_parser_name_lookup_error (cp_parser* parser,
1799 const char* desired)
1801 /* If name lookup completely failed, tell the user that NAME was not
1803 if (decl == error_mark_node)
1805 if (parser->scope && parser->scope != global_namespace)
1806 error ("`%D::%D' has not been declared",
1807 parser->scope, name);
1808 else if (parser->scope == global_namespace)
1809 error ("`::%D' has not been declared", name);
1811 error ("`%D' has not been declared", name);
1813 else if (parser->scope && parser->scope != global_namespace)
1814 error ("`%D::%D' %s", parser->scope, name, desired);
1815 else if (parser->scope == global_namespace)
1816 error ("`::%D' %s", name, desired);
1818 error ("`%D' %s", name, desired);
1821 /* If we are parsing tentatively, remember that an error has occurred
1822 during this tentative parse. Returns true if the error was
1823 simulated; false if a messgae should be issued by the caller. */
1826 cp_parser_simulate_error (cp_parser* parser)
1828 if (cp_parser_parsing_tentatively (parser)
1829 && !cp_parser_committed_to_tentative_parse (parser))
1831 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1837 /* This function is called when a type is defined. If type
1838 definitions are forbidden at this point, an error message is
1842 cp_parser_check_type_definition (cp_parser* parser)
1844 /* If types are forbidden here, issue a message. */
1845 if (parser->type_definition_forbidden_message)
1846 /* Use `%s' to print the string in case there are any escape
1847 characters in the message. */
1848 error ("%s", parser->type_definition_forbidden_message);
1851 /* This function is called when a declaration is parsed. If
1852 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1853 indicates that a type was defined in the decl-specifiers for DECL,
1854 then an error is issued. */
1857 cp_parser_check_for_definition_in_return_type (tree declarator,
1858 int declares_class_or_enum)
1860 /* [dcl.fct] forbids type definitions in return types.
1861 Unfortunately, it's not easy to know whether or not we are
1862 processing a return type until after the fact. */
1864 && (TREE_CODE (declarator) == INDIRECT_REF
1865 || TREE_CODE (declarator) == ADDR_EXPR))
1866 declarator = TREE_OPERAND (declarator, 0);
1868 && TREE_CODE (declarator) == CALL_EXPR
1869 && declares_class_or_enum & 2)
1870 error ("new types may not be defined in a return type");
1873 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1874 "<" in any valid C++ program. If the next token is indeed "<",
1875 issue a message warning the user about what appears to be an
1876 invalid attempt to form a template-id. */
1879 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1885 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1888 error ("`%T' is not a template", type);
1889 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1890 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1892 error ("invalid template-id");
1893 /* Remember the location of the invalid "<". */
1894 if (cp_parser_parsing_tentatively (parser)
1895 && !cp_parser_committed_to_tentative_parse (parser))
1897 token = cp_lexer_peek_token (parser->lexer);
1898 token = cp_lexer_prev_token (parser->lexer, token);
1899 start = cp_lexer_token_difference (parser->lexer,
1900 parser->lexer->first_token,
1905 /* Consume the "<". */
1906 cp_lexer_consume_token (parser->lexer);
1907 /* Parse the template arguments. */
1908 cp_parser_enclosed_template_argument_list (parser);
1909 /* Permanently remove the invalid template arguments so that
1910 this error message is not issued again. */
1913 token = cp_lexer_advance_token (parser->lexer,
1914 parser->lexer->first_token,
1916 cp_lexer_purge_tokens_after (parser->lexer, token);
1921 /* Issue an error message about the fact that THING appeared in a
1922 constant-expression. Returns ERROR_MARK_NODE. */
1925 cp_parser_non_integral_constant_expression (const char *thing)
1927 error ("%s cannot appear in a constant-expression", thing);
1928 return error_mark_node;
1931 /* Check for a common situation where a type-name should be present,
1932 but is not, and issue a sensible error message. Returns true if an
1933 invalid type-name was detected. */
1936 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1938 /* If the next two tokens are both identifiers, the code is
1939 erroneous. The usual cause of this situation is code like:
1943 where "T" should name a type -- but does not. */
1944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1945 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1949 /* If parsing tentatively, we should commit; we really are
1950 looking at a declaration. */
1951 /* Consume the first identifier. */
1952 name = cp_lexer_consume_token (parser->lexer)->value;
1953 /* Issue an error message. */
1954 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1955 /* If we're in a template class, it's possible that the user was
1956 referring to a type from a base class. For example:
1958 template <typename T> struct A { typedef T X; };
1959 template <typename T> struct B : public A<T> { X x; };
1961 The user should have said "typename A<T>::X". */
1962 if (processing_template_decl && current_class_type)
1966 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1970 tree base_type = BINFO_TYPE (b);
1971 if (CLASS_TYPE_P (base_type)
1972 && dependent_type_p (base_type))
1975 /* Go from a particular instantiation of the
1976 template (which will have an empty TYPE_FIELDs),
1977 to the main version. */
1978 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1979 for (field = TYPE_FIELDS (base_type);
1981 field = TREE_CHAIN (field))
1982 if (TREE_CODE (field) == TYPE_DECL
1983 && DECL_NAME (field) == name)
1985 error ("(perhaps `typename %T::%s' was intended)",
1986 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1994 /* Skip to the end of the declaration; there's no point in
1995 trying to process it. */
1996 cp_parser_skip_to_end_of_statement (parser);
2004 /* Consume tokens up to, and including, the next non-nested closing `)'.
2005 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2006 are doing error recovery. Returns -1 if OR_COMMA is true and we
2007 found an unnested comma. */
2010 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2015 unsigned paren_depth = 0;
2016 unsigned brace_depth = 0;
2018 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2019 && !cp_parser_committed_to_tentative_parse (parser))
2026 /* If we've run out of tokens, then there is no closing `)'. */
2027 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2030 token = cp_lexer_peek_token (parser->lexer);
2032 /* This matches the processing in skip_to_end_of_statement. */
2033 if (token->type == CPP_SEMICOLON && !brace_depth)
2035 if (token->type == CPP_OPEN_BRACE)
2037 if (token->type == CPP_CLOSE_BRACE)
2042 if (recovering && or_comma && token->type == CPP_COMMA
2043 && !brace_depth && !paren_depth)
2048 /* If it is an `(', we have entered another level of nesting. */
2049 if (token->type == CPP_OPEN_PAREN)
2051 /* If it is a `)', then we might be done. */
2052 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2055 cp_lexer_consume_token (parser->lexer);
2060 /* Consume the token. */
2061 cp_lexer_consume_token (parser->lexer);
2065 /* Consume tokens until we reach the end of the current statement.
2066 Normally, that will be just before consuming a `;'. However, if a
2067 non-nested `}' comes first, then we stop before consuming that. */
2070 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2072 unsigned nesting_depth = 0;
2078 /* Peek at the next token. */
2079 token = cp_lexer_peek_token (parser->lexer);
2080 /* If we've run out of tokens, stop. */
2081 if (token->type == CPP_EOF)
2083 /* If the next token is a `;', we have reached the end of the
2085 if (token->type == CPP_SEMICOLON && !nesting_depth)
2087 /* If the next token is a non-nested `}', then we have reached
2088 the end of the current block. */
2089 if (token->type == CPP_CLOSE_BRACE)
2091 /* If this is a non-nested `}', stop before consuming it.
2092 That way, when confronted with something like:
2096 we stop before consuming the closing `}', even though we
2097 have not yet reached a `;'. */
2098 if (nesting_depth == 0)
2100 /* If it is the closing `}' for a block that we have
2101 scanned, stop -- but only after consuming the token.
2107 we will stop after the body of the erroneously declared
2108 function, but before consuming the following `typedef'
2110 if (--nesting_depth == 0)
2112 cp_lexer_consume_token (parser->lexer);
2116 /* If it the next token is a `{', then we are entering a new
2117 block. Consume the entire block. */
2118 else if (token->type == CPP_OPEN_BRACE)
2120 /* Consume the token. */
2121 cp_lexer_consume_token (parser->lexer);
2125 /* This function is called at the end of a statement or declaration.
2126 If the next token is a semicolon, it is consumed; otherwise, error
2127 recovery is attempted. */
2130 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2132 /* Look for the trailing `;'. */
2133 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2135 /* If there is additional (erroneous) input, skip to the end of
2137 cp_parser_skip_to_end_of_statement (parser);
2138 /* If the next token is now a `;', consume it. */
2139 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2140 cp_lexer_consume_token (parser->lexer);
2144 /* Skip tokens until we have consumed an entire block, or until we
2145 have consumed a non-nested `;'. */
2148 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2150 unsigned nesting_depth = 0;
2156 /* Peek at the next token. */
2157 token = cp_lexer_peek_token (parser->lexer);
2158 /* If we've run out of tokens, stop. */
2159 if (token->type == CPP_EOF)
2161 /* If the next token is a `;', we have reached the end of the
2163 if (token->type == CPP_SEMICOLON && !nesting_depth)
2165 /* Consume the `;'. */
2166 cp_lexer_consume_token (parser->lexer);
2169 /* Consume the token. */
2170 token = cp_lexer_consume_token (parser->lexer);
2171 /* If the next token is a non-nested `}', then we have reached
2172 the end of the current block. */
2173 if (token->type == CPP_CLOSE_BRACE
2174 && (nesting_depth == 0 || --nesting_depth == 0))
2176 /* If it the next token is a `{', then we are entering a new
2177 block. Consume the entire block. */
2178 if (token->type == CPP_OPEN_BRACE)
2183 /* Skip tokens until a non-nested closing curly brace is the next
2187 cp_parser_skip_to_closing_brace (cp_parser *parser)
2189 unsigned nesting_depth = 0;
2195 /* Peek at the next token. */
2196 token = cp_lexer_peek_token (parser->lexer);
2197 /* If we've run out of tokens, stop. */
2198 if (token->type == CPP_EOF)
2200 /* If the next token is a non-nested `}', then we have reached
2201 the end of the current block. */
2202 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2204 /* If it the next token is a `{', then we are entering a new
2205 block. Consume the entire block. */
2206 else if (token->type == CPP_OPEN_BRACE)
2208 /* Consume the token. */
2209 cp_lexer_consume_token (parser->lexer);
2213 /* Create a new C++ parser. */
2216 cp_parser_new (void)
2221 /* cp_lexer_new_main is called before calling ggc_alloc because
2222 cp_lexer_new_main might load a PCH file. */
2223 lexer = cp_lexer_new_main ();
2225 parser = ggc_alloc_cleared (sizeof (cp_parser));
2226 parser->lexer = lexer;
2227 parser->context = cp_parser_context_new (NULL);
2229 /* For now, we always accept GNU extensions. */
2230 parser->allow_gnu_extensions_p = 1;
2232 /* The `>' token is a greater-than operator, not the end of a
2234 parser->greater_than_is_operator_p = true;
2236 parser->default_arg_ok_p = true;
2238 /* We are not parsing a constant-expression. */
2239 parser->integral_constant_expression_p = false;
2240 parser->allow_non_integral_constant_expression_p = false;
2241 parser->non_integral_constant_expression_p = false;
2243 /* We are not parsing offsetof. */
2244 parser->in_offsetof_p = false;
2246 /* Local variable names are not forbidden. */
2247 parser->local_variables_forbidden_p = false;
2249 /* We are not processing an `extern "C"' declaration. */
2250 parser->in_unbraced_linkage_specification_p = false;
2252 /* We are not processing a declarator. */
2253 parser->in_declarator_p = false;
2255 /* We are not processing a template-argument-list. */
2256 parser->in_template_argument_list_p = false;
2258 /* We are not in an iteration statement. */
2259 parser->in_iteration_statement_p = false;
2261 /* We are not in a switch statement. */
2262 parser->in_switch_statement_p = false;
2264 /* We are not parsing a type-id inside an expression. */
2265 parser->in_type_id_in_expr_p = false;
2267 /* The unparsed function queue is empty. */
2268 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2270 /* There are no classes being defined. */
2271 parser->num_classes_being_defined = 0;
2273 /* No template parameters apply. */
2274 parser->num_template_parameter_lists = 0;
2279 /* Lexical conventions [gram.lex] */
2281 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2285 cp_parser_identifier (cp_parser* parser)
2289 /* Look for the identifier. */
2290 token = cp_parser_require (parser, CPP_NAME, "identifier");
2291 /* Return the value. */
2292 return token ? token->value : error_mark_node;
2295 /* Basic concepts [gram.basic] */
2297 /* Parse a translation-unit.
2300 declaration-seq [opt]
2302 Returns TRUE if all went well. */
2305 cp_parser_translation_unit (cp_parser* parser)
2309 cp_parser_declaration_seq_opt (parser);
2311 /* If there are no tokens left then all went well. */
2312 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2315 /* Otherwise, issue an error message. */
2316 cp_parser_error (parser, "expected declaration");
2320 /* Consume the EOF token. */
2321 cp_parser_require (parser, CPP_EOF, "end-of-file");
2324 finish_translation_unit ();
2326 /* All went well. */
2330 /* Expressions [gram.expr] */
2332 /* Parse a primary-expression.
2343 ( compound-statement )
2344 __builtin_va_arg ( assignment-expression , type-id )
2349 Returns a representation of the expression.
2351 *IDK indicates what kind of id-expression (if any) was present.
2353 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2354 used as the operand of a pointer-to-member. In that case,
2355 *QUALIFYING_CLASS gives the class that is used as the qualifying
2356 class in the pointer-to-member. */
2359 cp_parser_primary_expression (cp_parser *parser,
2361 tree *qualifying_class)
2365 /* Assume the primary expression is not an id-expression. */
2366 *idk = CP_ID_KIND_NONE;
2367 /* And that it cannot be used as pointer-to-member. */
2368 *qualifying_class = NULL_TREE;
2370 /* Peek at the next token. */
2371 token = cp_lexer_peek_token (parser->lexer);
2372 switch (token->type)
2385 token = cp_lexer_consume_token (parser->lexer);
2386 return token->value;
2388 case CPP_OPEN_PAREN:
2391 bool saved_greater_than_is_operator_p;
2393 /* Consume the `('. */
2394 cp_lexer_consume_token (parser->lexer);
2395 /* Within a parenthesized expression, a `>' token is always
2396 the greater-than operator. */
2397 saved_greater_than_is_operator_p
2398 = parser->greater_than_is_operator_p;
2399 parser->greater_than_is_operator_p = true;
2400 /* If we see `( { ' then we are looking at the beginning of
2401 a GNU statement-expression. */
2402 if (cp_parser_allow_gnu_extensions_p (parser)
2403 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2405 /* Statement-expressions are not allowed by the standard. */
2407 pedwarn ("ISO C++ forbids braced-groups within expressions");
2409 /* And they're not allowed outside of a function-body; you
2410 cannot, for example, write:
2412 int i = ({ int j = 3; j + 1; });
2414 at class or namespace scope. */
2415 if (!at_function_scope_p ())
2416 error ("statement-expressions are allowed only inside functions");
2417 /* Start the statement-expression. */
2418 expr = begin_stmt_expr ();
2419 /* Parse the compound-statement. */
2420 cp_parser_compound_statement (parser, true);
2422 expr = finish_stmt_expr (expr, false);
2426 /* Parse the parenthesized expression. */
2427 expr = cp_parser_expression (parser);
2428 /* Let the front end know that this expression was
2429 enclosed in parentheses. This matters in case, for
2430 example, the expression is of the form `A::B', since
2431 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2433 finish_parenthesized_expr (expr);
2435 /* The `>' token might be the end of a template-id or
2436 template-parameter-list now. */
2437 parser->greater_than_is_operator_p
2438 = saved_greater_than_is_operator_p;
2439 /* Consume the `)'. */
2440 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2441 cp_parser_skip_to_end_of_statement (parser);
2447 switch (token->keyword)
2449 /* These two are the boolean literals. */
2451 cp_lexer_consume_token (parser->lexer);
2452 return boolean_true_node;
2454 cp_lexer_consume_token (parser->lexer);
2455 return boolean_false_node;
2457 /* The `__null' literal. */
2459 cp_lexer_consume_token (parser->lexer);
2462 /* Recognize the `this' keyword. */
2464 cp_lexer_consume_token (parser->lexer);
2465 if (parser->local_variables_forbidden_p)
2467 error ("`this' may not be used in this context");
2468 return error_mark_node;
2470 /* Pointers cannot appear in constant-expressions. */
2471 if (parser->integral_constant_expression_p)
2473 if (!parser->allow_non_integral_constant_expression_p)
2474 return cp_parser_non_integral_constant_expression ("`this'");
2475 parser->non_integral_constant_expression_p = true;
2477 return finish_this_expr ();
2479 /* The `operator' keyword can be the beginning of an
2484 case RID_FUNCTION_NAME:
2485 case RID_PRETTY_FUNCTION_NAME:
2486 case RID_C99_FUNCTION_NAME:
2487 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2488 __func__ are the names of variables -- but they are
2489 treated specially. Therefore, they are handled here,
2490 rather than relying on the generic id-expression logic
2491 below. Grammatically, these names are id-expressions.
2493 Consume the token. */
2494 token = cp_lexer_consume_token (parser->lexer);
2495 /* Look up the name. */
2496 return finish_fname (token->value);
2503 /* The `__builtin_va_arg' construct is used to handle
2504 `va_arg'. Consume the `__builtin_va_arg' token. */
2505 cp_lexer_consume_token (parser->lexer);
2506 /* Look for the opening `('. */
2507 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2508 /* Now, parse the assignment-expression. */
2509 expression = cp_parser_assignment_expression (parser);
2510 /* Look for the `,'. */
2511 cp_parser_require (parser, CPP_COMMA, "`,'");
2512 /* Parse the type-id. */
2513 type = cp_parser_type_id (parser);
2514 /* Look for the closing `)'. */
2515 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2516 /* Using `va_arg' in a constant-expression is not
2518 if (parser->integral_constant_expression_p)
2520 if (!parser->allow_non_integral_constant_expression_p)
2521 return cp_parser_non_integral_constant_expression ("`va_arg'");
2522 parser->non_integral_constant_expression_p = true;
2524 return build_x_va_arg (expression, type);
2530 bool saved_in_offsetof_p;
2532 /* Consume the "__offsetof__" token. */
2533 cp_lexer_consume_token (parser->lexer);
2534 /* Consume the opening `('. */
2535 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2536 /* Parse the parenthesized (almost) constant-expression. */
2537 saved_in_offsetof_p = parser->in_offsetof_p;
2538 parser->in_offsetof_p = true;
2540 = cp_parser_constant_expression (parser,
2541 /*allow_non_constant_p=*/false,
2542 /*non_constant_p=*/NULL);
2543 parser->in_offsetof_p = saved_in_offsetof_p;
2544 /* Consume the closing ')'. */
2545 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2551 cp_parser_error (parser, "expected primary-expression");
2552 return error_mark_node;
2555 /* An id-expression can start with either an identifier, a
2556 `::' as the beginning of a qualified-id, or the "operator"
2560 case CPP_TEMPLATE_ID:
2561 case CPP_NESTED_NAME_SPECIFIER:
2565 const char *error_msg;
2568 /* Parse the id-expression. */
2570 = cp_parser_id_expression (parser,
2571 /*template_keyword_p=*/false,
2572 /*check_dependency_p=*/true,
2573 /*template_p=*/NULL,
2574 /*declarator_p=*/false);
2575 if (id_expression == error_mark_node)
2576 return error_mark_node;
2577 /* If we have a template-id, then no further lookup is
2578 required. If the template-id was for a template-class, we
2579 will sometimes have a TYPE_DECL at this point. */
2580 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2581 || TREE_CODE (id_expression) == TYPE_DECL)
2582 decl = id_expression;
2583 /* Look up the name. */
2586 decl = cp_parser_lookup_name_simple (parser, id_expression);
2587 /* If name lookup gives us a SCOPE_REF, then the
2588 qualifying scope was dependent. Just propagate the
2590 if (TREE_CODE (decl) == SCOPE_REF)
2592 if (TYPE_P (TREE_OPERAND (decl, 0)))
2593 *qualifying_class = TREE_OPERAND (decl, 0);
2596 /* Check to see if DECL is a local variable in a context
2597 where that is forbidden. */
2598 if (parser->local_variables_forbidden_p
2599 && local_variable_p (decl))
2601 /* It might be that we only found DECL because we are
2602 trying to be generous with pre-ISO scoping rules.
2603 For example, consider:
2607 for (int i = 0; i < 10; ++i) {}
2608 extern void f(int j = i);
2611 Here, name look up will originally find the out
2612 of scope `i'. We need to issue a warning message,
2613 but then use the global `i'. */
2614 decl = check_for_out_of_scope_variable (decl);
2615 if (local_variable_p (decl))
2617 error ("local variable `%D' may not appear in this context",
2619 return error_mark_node;
2624 decl = finish_id_expression (id_expression, decl, parser->scope,
2625 idk, qualifying_class,
2626 parser->integral_constant_expression_p,
2627 parser->allow_non_integral_constant_expression_p,
2628 &parser->non_integral_constant_expression_p,
2631 cp_parser_error (parser, error_msg);
2635 /* Anything else is an error. */
2637 cp_parser_error (parser, "expected primary-expression");
2638 return error_mark_node;
2642 /* Parse an id-expression.
2649 :: [opt] nested-name-specifier template [opt] unqualified-id
2651 :: operator-function-id
2654 Return a representation of the unqualified portion of the
2655 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2656 a `::' or nested-name-specifier.
2658 Often, if the id-expression was a qualified-id, the caller will
2659 want to make a SCOPE_REF to represent the qualified-id. This
2660 function does not do this in order to avoid wastefully creating
2661 SCOPE_REFs when they are not required.
2663 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2666 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2667 uninstantiated templates.
2669 If *TEMPLATE_P is non-NULL, it is set to true iff the
2670 `template' keyword is used to explicitly indicate that the entity
2671 named is a template.
2673 If DECLARATOR_P is true, the id-expression is appearing as part of
2674 a declarator, rather than as part of an expression. */
2677 cp_parser_id_expression (cp_parser *parser,
2678 bool template_keyword_p,
2679 bool check_dependency_p,
2683 bool global_scope_p;
2684 bool nested_name_specifier_p;
2686 /* Assume the `template' keyword was not used. */
2688 *template_p = false;
2690 /* Look for the optional `::' operator. */
2692 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2694 /* Look for the optional nested-name-specifier. */
2695 nested_name_specifier_p
2696 = (cp_parser_nested_name_specifier_opt (parser,
2697 /*typename_keyword_p=*/false,
2700 /*is_declarator=*/false)
2702 /* If there is a nested-name-specifier, then we are looking at
2703 the first qualified-id production. */
2704 if (nested_name_specifier_p)
2707 tree saved_object_scope;
2708 tree saved_qualifying_scope;
2709 tree unqualified_id;
2712 /* See if the next token is the `template' keyword. */
2714 template_p = &is_template;
2715 *template_p = cp_parser_optional_template_keyword (parser);
2716 /* Name lookup we do during the processing of the
2717 unqualified-id might obliterate SCOPE. */
2718 saved_scope = parser->scope;
2719 saved_object_scope = parser->object_scope;
2720 saved_qualifying_scope = parser->qualifying_scope;
2721 /* Process the final unqualified-id. */
2722 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2725 /* Restore the SAVED_SCOPE for our caller. */
2726 parser->scope = saved_scope;
2727 parser->object_scope = saved_object_scope;
2728 parser->qualifying_scope = saved_qualifying_scope;
2730 return unqualified_id;
2732 /* Otherwise, if we are in global scope, then we are looking at one
2733 of the other qualified-id productions. */
2734 else if (global_scope_p)
2739 /* Peek at the next token. */
2740 token = cp_lexer_peek_token (parser->lexer);
2742 /* If it's an identifier, and the next token is not a "<", then
2743 we can avoid the template-id case. This is an optimization
2744 for this common case. */
2745 if (token->type == CPP_NAME
2746 && !cp_parser_nth_token_starts_template_argument_list_p
2748 return cp_parser_identifier (parser);
2750 cp_parser_parse_tentatively (parser);
2751 /* Try a template-id. */
2752 id = cp_parser_template_id (parser,
2753 /*template_keyword_p=*/false,
2754 /*check_dependency_p=*/true,
2756 /* If that worked, we're done. */
2757 if (cp_parser_parse_definitely (parser))
2760 /* Peek at the next token. (Changes in the token buffer may
2761 have invalidated the pointer obtained above.) */
2762 token = cp_lexer_peek_token (parser->lexer);
2764 switch (token->type)
2767 return cp_parser_identifier (parser);
2770 if (token->keyword == RID_OPERATOR)
2771 return cp_parser_operator_function_id (parser);
2775 cp_parser_error (parser, "expected id-expression");
2776 return error_mark_node;
2780 return cp_parser_unqualified_id (parser, template_keyword_p,
2781 /*check_dependency_p=*/true,
2785 /* Parse an unqualified-id.
2789 operator-function-id
2790 conversion-function-id
2794 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2795 keyword, in a construct like `A::template ...'.
2797 Returns a representation of unqualified-id. For the `identifier'
2798 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2799 production a BIT_NOT_EXPR is returned; the operand of the
2800 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2801 other productions, see the documentation accompanying the
2802 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2803 names are looked up in uninstantiated templates. If DECLARATOR_P
2804 is true, the unqualified-id is appearing as part of a declarator,
2805 rather than as part of an expression. */
2808 cp_parser_unqualified_id (cp_parser* parser,
2809 bool template_keyword_p,
2810 bool check_dependency_p,
2815 /* Peek at the next token. */
2816 token = cp_lexer_peek_token (parser->lexer);
2818 switch (token->type)
2824 /* We don't know yet whether or not this will be a
2826 cp_parser_parse_tentatively (parser);
2827 /* Try a template-id. */
2828 id = cp_parser_template_id (parser, template_keyword_p,
2831 /* If it worked, we're done. */
2832 if (cp_parser_parse_definitely (parser))
2834 /* Otherwise, it's an ordinary identifier. */
2835 return cp_parser_identifier (parser);
2838 case CPP_TEMPLATE_ID:
2839 return cp_parser_template_id (parser, template_keyword_p,
2846 tree qualifying_scope;
2850 /* Consume the `~' token. */
2851 cp_lexer_consume_token (parser->lexer);
2852 /* Parse the class-name. The standard, as written, seems to
2855 template <typename T> struct S { ~S (); };
2856 template <typename T> S<T>::~S() {}
2858 is invalid, since `~' must be followed by a class-name, but
2859 `S<T>' is dependent, and so not known to be a class.
2860 That's not right; we need to look in uninstantiated
2861 templates. A further complication arises from:
2863 template <typename T> void f(T t) {
2867 Here, it is not possible to look up `T' in the scope of `T'
2868 itself. We must look in both the current scope, and the
2869 scope of the containing complete expression.
2871 Yet another issue is:
2880 The standard does not seem to say that the `S' in `~S'
2881 should refer to the type `S' and not the data member
2884 /* DR 244 says that we look up the name after the "~" in the
2885 same scope as we looked up the qualifying name. That idea
2886 isn't fully worked out; it's more complicated than that. */
2887 scope = parser->scope;
2888 object_scope = parser->object_scope;
2889 qualifying_scope = parser->qualifying_scope;
2891 /* If the name is of the form "X::~X" it's OK. */
2892 if (scope && TYPE_P (scope)
2893 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2894 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2896 && (cp_lexer_peek_token (parser->lexer)->value
2897 == TYPE_IDENTIFIER (scope)))
2899 cp_lexer_consume_token (parser->lexer);
2900 return build_nt (BIT_NOT_EXPR, scope);
2903 /* If there was an explicit qualification (S::~T), first look
2904 in the scope given by the qualification (i.e., S). */
2907 cp_parser_parse_tentatively (parser);
2908 type_decl = cp_parser_class_name (parser,
2909 /*typename_keyword_p=*/false,
2910 /*template_keyword_p=*/false,
2912 /*check_dependency=*/false,
2913 /*class_head_p=*/false,
2915 if (cp_parser_parse_definitely (parser))
2916 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2918 /* In "N::S::~S", look in "N" as well. */
2919 if (scope && qualifying_scope)
2921 cp_parser_parse_tentatively (parser);
2922 parser->scope = qualifying_scope;
2923 parser->object_scope = NULL_TREE;
2924 parser->qualifying_scope = NULL_TREE;
2926 = cp_parser_class_name (parser,
2927 /*typename_keyword_p=*/false,
2928 /*template_keyword_p=*/false,
2930 /*check_dependency=*/false,
2931 /*class_head_p=*/false,
2933 if (cp_parser_parse_definitely (parser))
2934 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2936 /* In "p->S::~T", look in the scope given by "*p" as well. */
2937 else if (object_scope)
2939 cp_parser_parse_tentatively (parser);
2940 parser->scope = object_scope;
2941 parser->object_scope = NULL_TREE;
2942 parser->qualifying_scope = NULL_TREE;
2944 = cp_parser_class_name (parser,
2945 /*typename_keyword_p=*/false,
2946 /*template_keyword_p=*/false,
2948 /*check_dependency=*/false,
2949 /*class_head_p=*/false,
2951 if (cp_parser_parse_definitely (parser))
2952 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2954 /* Look in the surrounding context. */
2955 parser->scope = NULL_TREE;
2956 parser->object_scope = NULL_TREE;
2957 parser->qualifying_scope = NULL_TREE;
2959 = cp_parser_class_name (parser,
2960 /*typename_keyword_p=*/false,
2961 /*template_keyword_p=*/false,
2963 /*check_dependency=*/false,
2964 /*class_head_p=*/false,
2966 /* If an error occurred, assume that the name of the
2967 destructor is the same as the name of the qualifying
2968 class. That allows us to keep parsing after running
2969 into ill-formed destructor names. */
2970 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2971 return build_nt (BIT_NOT_EXPR, scope);
2972 else if (type_decl == error_mark_node)
2973 return error_mark_node;
2977 A typedef-name that names a class shall not be used as the
2978 identifier in the declarator for a destructor declaration. */
2980 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2981 && !DECL_SELF_REFERENCE_P (type_decl))
2982 error ("typedef-name `%D' used as destructor declarator",
2985 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2989 if (token->keyword == RID_OPERATOR)
2993 /* This could be a template-id, so we try that first. */
2994 cp_parser_parse_tentatively (parser);
2995 /* Try a template-id. */
2996 id = cp_parser_template_id (parser, template_keyword_p,
2997 /*check_dependency_p=*/true,
2999 /* If that worked, we're done. */
3000 if (cp_parser_parse_definitely (parser))
3002 /* We still don't know whether we're looking at an
3003 operator-function-id or a conversion-function-id. */
3004 cp_parser_parse_tentatively (parser);
3005 /* Try an operator-function-id. */
3006 id = cp_parser_operator_function_id (parser);
3007 /* If that didn't work, try a conversion-function-id. */
3008 if (!cp_parser_parse_definitely (parser))
3009 id = cp_parser_conversion_function_id (parser);
3016 cp_parser_error (parser, "expected unqualified-id");
3017 return error_mark_node;
3021 /* Parse an (optional) nested-name-specifier.
3023 nested-name-specifier:
3024 class-or-namespace-name :: nested-name-specifier [opt]
3025 class-or-namespace-name :: template nested-name-specifier [opt]
3027 PARSER->SCOPE should be set appropriately before this function is
3028 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3029 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3032 Sets PARSER->SCOPE to the class (TYPE) or namespace
3033 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3034 it unchanged if there is no nested-name-specifier. Returns the new
3035 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3037 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3038 part of a declaration and/or decl-specifier. */
3041 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3042 bool typename_keyword_p,
3043 bool check_dependency_p,
3045 bool is_declaration)
3047 bool success = false;
3048 tree access_check = NULL_TREE;
3052 /* If the next token corresponds to a nested name specifier, there
3053 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3054 false, it may have been true before, in which case something
3055 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3056 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3057 CHECK_DEPENDENCY_P is false, we have to fall through into the
3059 if (check_dependency_p
3060 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3062 cp_parser_pre_parsed_nested_name_specifier (parser);
3063 return parser->scope;
3066 /* Remember where the nested-name-specifier starts. */
3067 if (cp_parser_parsing_tentatively (parser)
3068 && !cp_parser_committed_to_tentative_parse (parser))
3070 token = cp_lexer_peek_token (parser->lexer);
3071 start = cp_lexer_token_difference (parser->lexer,
3072 parser->lexer->first_token,
3078 push_deferring_access_checks (dk_deferred);
3084 tree saved_qualifying_scope;
3085 bool template_keyword_p;
3087 /* Spot cases that cannot be the beginning of a
3088 nested-name-specifier. */
3089 token = cp_lexer_peek_token (parser->lexer);
3091 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3092 the already parsed nested-name-specifier. */
3093 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3095 /* Grab the nested-name-specifier and continue the loop. */
3096 cp_parser_pre_parsed_nested_name_specifier (parser);
3101 /* Spot cases that cannot be the beginning of a
3102 nested-name-specifier. On the second and subsequent times
3103 through the loop, we look for the `template' keyword. */
3104 if (success && token->keyword == RID_TEMPLATE)
3106 /* A template-id can start a nested-name-specifier. */
3107 else if (token->type == CPP_TEMPLATE_ID)
3111 /* If the next token is not an identifier, then it is
3112 definitely not a class-or-namespace-name. */
3113 if (token->type != CPP_NAME)
3115 /* If the following token is neither a `<' (to begin a
3116 template-id), nor a `::', then we are not looking at a
3117 nested-name-specifier. */
3118 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3119 if (token->type != CPP_SCOPE
3120 && !cp_parser_nth_token_starts_template_argument_list_p
3125 /* The nested-name-specifier is optional, so we parse
3127 cp_parser_parse_tentatively (parser);
3129 /* Look for the optional `template' keyword, if this isn't the
3130 first time through the loop. */
3132 template_keyword_p = cp_parser_optional_template_keyword (parser);
3134 template_keyword_p = false;
3136 /* Save the old scope since the name lookup we are about to do
3137 might destroy it. */
3138 old_scope = parser->scope;
3139 saved_qualifying_scope = parser->qualifying_scope;
3140 /* Parse the qualifying entity. */
3142 = cp_parser_class_or_namespace_name (parser,
3148 /* Look for the `::' token. */
3149 cp_parser_require (parser, CPP_SCOPE, "`::'");
3151 /* If we found what we wanted, we keep going; otherwise, we're
3153 if (!cp_parser_parse_definitely (parser))
3155 bool error_p = false;
3157 /* Restore the OLD_SCOPE since it was valid before the
3158 failed attempt at finding the last
3159 class-or-namespace-name. */
3160 parser->scope = old_scope;
3161 parser->qualifying_scope = saved_qualifying_scope;
3162 /* If the next token is an identifier, and the one after
3163 that is a `::', then any valid interpretation would have
3164 found a class-or-namespace-name. */
3165 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3166 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3168 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3171 token = cp_lexer_consume_token (parser->lexer);
3176 decl = cp_parser_lookup_name_simple (parser, token->value);
3177 if (TREE_CODE (decl) == TEMPLATE_DECL)
3178 error ("`%D' used without template parameters",
3181 cp_parser_name_lookup_error
3182 (parser, token->value, decl,
3183 "is not a class or namespace");
3184 parser->scope = NULL_TREE;
3186 /* Treat this as a successful nested-name-specifier
3191 If the name found is not a class-name (clause
3192 _class_) or namespace-name (_namespace.def_), the
3193 program is ill-formed. */
3196 cp_lexer_consume_token (parser->lexer);
3201 /* We've found one valid nested-name-specifier. */
3203 /* Make sure we look in the right scope the next time through
3205 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3206 ? TREE_TYPE (new_scope)
3208 /* If it is a class scope, try to complete it; we are about to
3209 be looking up names inside the class. */
3210 if (TYPE_P (parser->scope)
3211 /* Since checking types for dependency can be expensive,
3212 avoid doing it if the type is already complete. */
3213 && !COMPLETE_TYPE_P (parser->scope)
3214 /* Do not try to complete dependent types. */
3215 && !dependent_type_p (parser->scope))
3216 complete_type (parser->scope);
3219 /* Retrieve any deferred checks. Do not pop this access checks yet
3220 so the memory will not be reclaimed during token replacing below. */
3221 access_check = get_deferred_access_checks ();
3223 /* If parsing tentatively, replace the sequence of tokens that makes
3224 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3225 token. That way, should we re-parse the token stream, we will
3226 not have to repeat the effort required to do the parse, nor will
3227 we issue duplicate error messages. */
3228 if (success && start >= 0)
3230 /* Find the token that corresponds to the start of the
3232 token = cp_lexer_advance_token (parser->lexer,
3233 parser->lexer->first_token,
3236 /* Reset the contents of the START token. */
3237 token->type = CPP_NESTED_NAME_SPECIFIER;
3238 token->value = build_tree_list (access_check, parser->scope);
3239 TREE_TYPE (token->value) = parser->qualifying_scope;
3240 token->keyword = RID_MAX;
3241 /* Purge all subsequent tokens. */
3242 cp_lexer_purge_tokens_after (parser->lexer, token);
3245 pop_deferring_access_checks ();
3246 return success ? parser->scope : NULL_TREE;
3249 /* Parse a nested-name-specifier. See
3250 cp_parser_nested_name_specifier_opt for details. This function
3251 behaves identically, except that it will an issue an error if no
3252 nested-name-specifier is present, and it will return
3253 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3257 cp_parser_nested_name_specifier (cp_parser *parser,
3258 bool typename_keyword_p,
3259 bool check_dependency_p,
3261 bool is_declaration)
3265 /* Look for the nested-name-specifier. */
3266 scope = cp_parser_nested_name_specifier_opt (parser,
3271 /* If it was not present, issue an error message. */
3274 cp_parser_error (parser, "expected nested-name-specifier");
3275 parser->scope = NULL_TREE;
3276 return error_mark_node;
3282 /* Parse a class-or-namespace-name.
3284 class-or-namespace-name:
3288 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3289 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3290 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3291 TYPE_P is TRUE iff the next name should be taken as a class-name,
3292 even the same name is declared to be another entity in the same
3295 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3296 specified by the class-or-namespace-name. If neither is found the
3297 ERROR_MARK_NODE is returned. */
3300 cp_parser_class_or_namespace_name (cp_parser *parser,
3301 bool typename_keyword_p,
3302 bool template_keyword_p,
3303 bool check_dependency_p,
3305 bool is_declaration)
3308 tree saved_qualifying_scope;
3309 tree saved_object_scope;
3313 /* Before we try to parse the class-name, we must save away the
3314 current PARSER->SCOPE since cp_parser_class_name will destroy
3316 saved_scope = parser->scope;
3317 saved_qualifying_scope = parser->qualifying_scope;
3318 saved_object_scope = parser->object_scope;
3319 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3320 there is no need to look for a namespace-name. */
3321 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3323 cp_parser_parse_tentatively (parser);
3324 scope = cp_parser_class_name (parser,
3329 /*class_head_p=*/false,
3331 /* If that didn't work, try for a namespace-name. */
3332 if (!only_class_p && !cp_parser_parse_definitely (parser))
3334 /* Restore the saved scope. */
3335 parser->scope = saved_scope;
3336 parser->qualifying_scope = saved_qualifying_scope;
3337 parser->object_scope = saved_object_scope;
3338 /* If we are not looking at an identifier followed by the scope
3339 resolution operator, then this is not part of a
3340 nested-name-specifier. (Note that this function is only used
3341 to parse the components of a nested-name-specifier.) */
3342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3343 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3344 return error_mark_node;
3345 scope = cp_parser_namespace_name (parser);
3351 /* Parse a postfix-expression.
3355 postfix-expression [ expression ]
3356 postfix-expression ( expression-list [opt] )
3357 simple-type-specifier ( expression-list [opt] )
3358 typename :: [opt] nested-name-specifier identifier
3359 ( expression-list [opt] )
3360 typename :: [opt] nested-name-specifier template [opt] template-id
3361 ( expression-list [opt] )
3362 postfix-expression . template [opt] id-expression
3363 postfix-expression -> template [opt] id-expression
3364 postfix-expression . pseudo-destructor-name
3365 postfix-expression -> pseudo-destructor-name
3366 postfix-expression ++
3367 postfix-expression --
3368 dynamic_cast < type-id > ( expression )
3369 static_cast < type-id > ( expression )
3370 reinterpret_cast < type-id > ( expression )
3371 const_cast < type-id > ( expression )
3372 typeid ( expression )
3378 ( type-id ) { initializer-list , [opt] }
3380 This extension is a GNU version of the C99 compound-literal
3381 construct. (The C99 grammar uses `type-name' instead of `type-id',
3382 but they are essentially the same concept.)
3384 If ADDRESS_P is true, the postfix expression is the operand of the
3387 Returns a representation of the expression. */
3390 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3394 cp_id_kind idk = CP_ID_KIND_NONE;
3395 tree postfix_expression = NULL_TREE;
3396 /* Non-NULL only if the current postfix-expression can be used to
3397 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3398 class used to qualify the member. */
3399 tree qualifying_class = NULL_TREE;
3401 /* Peek at the next token. */
3402 token = cp_lexer_peek_token (parser->lexer);
3403 /* Some of the productions are determined by keywords. */
3404 keyword = token->keyword;
3414 const char *saved_message;
3416 /* All of these can be handled in the same way from the point
3417 of view of parsing. Begin by consuming the token
3418 identifying the cast. */
3419 cp_lexer_consume_token (parser->lexer);
3421 /* New types cannot be defined in the cast. */
3422 saved_message = parser->type_definition_forbidden_message;
3423 parser->type_definition_forbidden_message
3424 = "types may not be defined in casts";
3426 /* Look for the opening `<'. */
3427 cp_parser_require (parser, CPP_LESS, "`<'");
3428 /* Parse the type to which we are casting. */
3429 type = cp_parser_type_id (parser);
3430 /* Look for the closing `>'. */
3431 cp_parser_require (parser, CPP_GREATER, "`>'");
3432 /* Restore the old message. */
3433 parser->type_definition_forbidden_message = saved_message;
3435 /* And the expression which is being cast. */
3436 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3437 expression = cp_parser_expression (parser);
3438 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3440 /* Only type conversions to integral or enumeration types
3441 can be used in constant-expressions. */
3442 if (parser->integral_constant_expression_p
3443 && !dependent_type_p (type)
3444 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3445 /* A cast to pointer or reference type is allowed in the
3446 implementation of "offsetof". */
3447 && !(parser->in_offsetof_p && POINTER_TYPE_P (type)))
3449 if (!parser->allow_non_integral_constant_expression_p)
3450 return (cp_parser_non_integral_constant_expression
3451 ("a cast to a type other than an integral or "
3452 "enumeration type"));
3453 parser->non_integral_constant_expression_p = true;
3460 = build_dynamic_cast (type, expression);
3464 = build_static_cast (type, expression);
3468 = build_reinterpret_cast (type, expression);
3472 = build_const_cast (type, expression);
3483 const char *saved_message;
3484 bool saved_in_type_id_in_expr_p;
3486 /* Consume the `typeid' token. */
3487 cp_lexer_consume_token (parser->lexer);
3488 /* Look for the `(' token. */
3489 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3490 /* Types cannot be defined in a `typeid' expression. */
3491 saved_message = parser->type_definition_forbidden_message;
3492 parser->type_definition_forbidden_message
3493 = "types may not be defined in a `typeid\' expression";
3494 /* We can't be sure yet whether we're looking at a type-id or an
3496 cp_parser_parse_tentatively (parser);
3497 /* Try a type-id first. */
3498 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3499 parser->in_type_id_in_expr_p = true;
3500 type = cp_parser_type_id (parser);
3501 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3502 /* Look for the `)' token. Otherwise, we can't be sure that
3503 we're not looking at an expression: consider `typeid (int
3504 (3))', for example. */
3505 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3506 /* If all went well, simply lookup the type-id. */
3507 if (cp_parser_parse_definitely (parser))
3508 postfix_expression = get_typeid (type);
3509 /* Otherwise, fall back to the expression variant. */
3514 /* Look for an expression. */
3515 expression = cp_parser_expression (parser);
3516 /* Compute its typeid. */
3517 postfix_expression = build_typeid (expression);
3518 /* Look for the `)' token. */
3519 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3522 /* Restore the saved message. */
3523 parser->type_definition_forbidden_message = saved_message;
3529 bool template_p = false;
3533 /* Consume the `typename' token. */
3534 cp_lexer_consume_token (parser->lexer);
3535 /* Look for the optional `::' operator. */
3536 cp_parser_global_scope_opt (parser,
3537 /*current_scope_valid_p=*/false);
3538 /* Look for the nested-name-specifier. */
3539 cp_parser_nested_name_specifier (parser,
3540 /*typename_keyword_p=*/true,
3541 /*check_dependency_p=*/true,
3543 /*is_declaration=*/true);
3544 /* Look for the optional `template' keyword. */
3545 template_p = cp_parser_optional_template_keyword (parser);
3546 /* We don't know whether we're looking at a template-id or an
3548 cp_parser_parse_tentatively (parser);
3549 /* Try a template-id. */
3550 id = cp_parser_template_id (parser, template_p,
3551 /*check_dependency_p=*/true,
3552 /*is_declaration=*/true);
3553 /* If that didn't work, try an identifier. */
3554 if (!cp_parser_parse_definitely (parser))
3555 id = cp_parser_identifier (parser);
3556 /* Create a TYPENAME_TYPE to represent the type to which the
3557 functional cast is being performed. */
3558 type = make_typename_type (parser->scope, id,
3561 postfix_expression = cp_parser_functional_cast (parser, type);
3569 /* If the next thing is a simple-type-specifier, we may be
3570 looking at a functional cast. We could also be looking at
3571 an id-expression. So, we try the functional cast, and if
3572 that doesn't work we fall back to the primary-expression. */
3573 cp_parser_parse_tentatively (parser);
3574 /* Look for the simple-type-specifier. */
3575 type = cp_parser_simple_type_specifier (parser,
3576 CP_PARSER_FLAGS_NONE,
3577 /*identifier_p=*/false);
3578 /* Parse the cast itself. */
3579 if (!cp_parser_error_occurred (parser))
3581 = cp_parser_functional_cast (parser, type);
3582 /* If that worked, we're done. */
3583 if (cp_parser_parse_definitely (parser))
3586 /* If the functional-cast didn't work out, try a
3587 compound-literal. */
3588 if (cp_parser_allow_gnu_extensions_p (parser)
3589 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3591 tree initializer_list = NULL_TREE;
3592 bool saved_in_type_id_in_expr_p;
3594 cp_parser_parse_tentatively (parser);
3595 /* Consume the `('. */
3596 cp_lexer_consume_token (parser->lexer);
3597 /* Parse the type. */
3598 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3599 parser->in_type_id_in_expr_p = true;
3600 type = cp_parser_type_id (parser);
3601 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3602 /* Look for the `)'. */
3603 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3604 /* Look for the `{'. */
3605 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3606 /* If things aren't going well, there's no need to
3608 if (!cp_parser_error_occurred (parser))
3610 bool non_constant_p;
3611 /* Parse the initializer-list. */
3613 = cp_parser_initializer_list (parser, &non_constant_p);
3614 /* Allow a trailing `,'. */
3615 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3616 cp_lexer_consume_token (parser->lexer);
3617 /* Look for the final `}'. */
3618 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3620 /* If that worked, we're definitely looking at a
3621 compound-literal expression. */
3622 if (cp_parser_parse_definitely (parser))
3624 /* Warn the user that a compound literal is not
3625 allowed in standard C++. */
3627 pedwarn ("ISO C++ forbids compound-literals");
3628 /* Form the representation of the compound-literal. */
3630 = finish_compound_literal (type, initializer_list);
3635 /* It must be a primary-expression. */
3636 postfix_expression = cp_parser_primary_expression (parser,
3643 /* If we were avoiding committing to the processing of a
3644 qualified-id until we knew whether or not we had a
3645 pointer-to-member, we now know. */
3646 if (qualifying_class)
3650 /* Peek at the next token. */
3651 token = cp_lexer_peek_token (parser->lexer);
3652 done = (token->type != CPP_OPEN_SQUARE
3653 && token->type != CPP_OPEN_PAREN
3654 && token->type != CPP_DOT
3655 && token->type != CPP_DEREF
3656 && token->type != CPP_PLUS_PLUS
3657 && token->type != CPP_MINUS_MINUS);
3659 postfix_expression = finish_qualified_id_expr (qualifying_class,
3664 return postfix_expression;
3667 /* Keep looping until the postfix-expression is complete. */
3670 if (idk == CP_ID_KIND_UNQUALIFIED
3671 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3672 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3673 /* It is not a Koenig lookup function call. */
3675 = unqualified_name_lookup_error (postfix_expression);
3677 /* Peek at the next token. */
3678 token = cp_lexer_peek_token (parser->lexer);
3680 switch (token->type)
3682 case CPP_OPEN_SQUARE:
3683 /* postfix-expression [ expression ] */
3687 /* Consume the `[' token. */
3688 cp_lexer_consume_token (parser->lexer);
3689 /* Parse the index expression. */
3690 index = cp_parser_expression (parser);
3691 /* Look for the closing `]'. */
3692 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3694 /* Build the ARRAY_REF. */
3696 = grok_array_decl (postfix_expression, index);
3697 idk = CP_ID_KIND_NONE;
3698 /* Array references are not permitted in
3699 constant-expressions. */
3700 if (parser->integral_constant_expression_p)
3702 if (!parser->allow_non_integral_constant_expression_p)
3704 = cp_parser_non_integral_constant_expression ("an array reference");
3705 parser->non_integral_constant_expression_p = true;
3710 case CPP_OPEN_PAREN:
3711 /* postfix-expression ( expression-list [opt] ) */
3714 tree args = (cp_parser_parenthesized_expression_list
3715 (parser, false, /*non_constant_p=*/NULL));
3717 if (args == error_mark_node)
3719 postfix_expression = error_mark_node;
3723 /* Function calls are not permitted in
3724 constant-expressions. */
3725 if (parser->integral_constant_expression_p)
3727 if (!parser->allow_non_integral_constant_expression_p)
3730 = cp_parser_non_integral_constant_expression ("a function call");
3733 parser->non_integral_constant_expression_p = true;
3737 if (idk == CP_ID_KIND_UNQUALIFIED)
3740 && (is_overloaded_fn (postfix_expression)
3741 || DECL_P (postfix_expression)
3742 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3746 = perform_koenig_lookup (postfix_expression, args);
3748 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3750 = unqualified_fn_lookup_error (postfix_expression);
3753 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3755 tree instance = TREE_OPERAND (postfix_expression, 0);
3756 tree fn = TREE_OPERAND (postfix_expression, 1);
3758 if (processing_template_decl
3759 && (type_dependent_expression_p (instance)
3760 || (!BASELINK_P (fn)
3761 && TREE_CODE (fn) != FIELD_DECL)
3762 || type_dependent_expression_p (fn)
3763 || any_type_dependent_arguments_p (args)))
3766 = build_min_nt (CALL_EXPR, postfix_expression, args);
3770 if (BASELINK_P (fn))
3772 = (build_new_method_call
3773 (instance, fn, args, NULL_TREE,
3774 (idk == CP_ID_KIND_QUALIFIED
3775 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3778 = finish_call_expr (postfix_expression, args,
3779 /*disallow_virtual=*/false,
3780 /*koenig_p=*/false);
3782 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3783 || TREE_CODE (postfix_expression) == MEMBER_REF
3784 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3785 postfix_expression = (build_offset_ref_call_from_tree
3786 (postfix_expression, args));
3787 else if (idk == CP_ID_KIND_QUALIFIED)
3788 /* A call to a static class member, or a namespace-scope
3791 = finish_call_expr (postfix_expression, args,
3792 /*disallow_virtual=*/true,
3795 /* All other function calls. */
3797 = finish_call_expr (postfix_expression, args,
3798 /*disallow_virtual=*/false,
3801 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3802 idk = CP_ID_KIND_NONE;
3808 /* postfix-expression . template [opt] id-expression
3809 postfix-expression . pseudo-destructor-name
3810 postfix-expression -> template [opt] id-expression
3811 postfix-expression -> pseudo-destructor-name */
3816 tree scope = NULL_TREE;
3817 enum cpp_ttype token_type = token->type;
3819 /* If this is a `->' operator, dereference the pointer. */
3820 if (token->type == CPP_DEREF)
3821 postfix_expression = build_x_arrow (postfix_expression);
3822 /* Check to see whether or not the expression is
3824 dependent_p = type_dependent_expression_p (postfix_expression);
3825 /* The identifier following the `->' or `.' is not
3827 parser->scope = NULL_TREE;
3828 parser->qualifying_scope = NULL_TREE;
3829 parser->object_scope = NULL_TREE;
3830 idk = CP_ID_KIND_NONE;
3831 /* Enter the scope corresponding to the type of the object
3832 given by the POSTFIX_EXPRESSION. */
3834 && TREE_TYPE (postfix_expression) != NULL_TREE)
3836 scope = TREE_TYPE (postfix_expression);
3837 /* According to the standard, no expression should
3838 ever have reference type. Unfortunately, we do not
3839 currently match the standard in this respect in
3840 that our internal representation of an expression
3841 may have reference type even when the standard says
3842 it does not. Therefore, we have to manually obtain
3843 the underlying type here. */
3844 scope = non_reference (scope);
3845 /* The type of the POSTFIX_EXPRESSION must be
3847 scope = complete_type_or_else (scope, NULL_TREE);
3848 /* Let the name lookup machinery know that we are
3849 processing a class member access expression. */
3850 parser->context->object_type = scope;
3851 /* If something went wrong, we want to be able to
3852 discern that case, as opposed to the case where
3853 there was no SCOPE due to the type of expression
3856 scope = error_mark_node;
3857 /* If the SCOPE was erroneous, make the various
3858 semantic analysis functions exit quickly -- and
3859 without issuing additional error messages. */
3860 if (scope == error_mark_node)
3861 postfix_expression = error_mark_node;
3864 /* Consume the `.' or `->' operator. */
3865 cp_lexer_consume_token (parser->lexer);
3866 /* If the SCOPE is not a scalar type, we are looking at an
3867 ordinary class member access expression, rather than a
3868 pseudo-destructor-name. */
3869 if (!scope || !SCALAR_TYPE_P (scope))
3871 template_p = cp_parser_optional_template_keyword (parser);
3872 /* Parse the id-expression. */
3873 name = cp_parser_id_expression (parser,
3875 /*check_dependency_p=*/true,
3876 /*template_p=*/NULL,
3877 /*declarator_p=*/false);
3878 /* In general, build a SCOPE_REF if the member name is
3879 qualified. However, if the name was not dependent
3880 and has already been resolved; there is no need to
3881 build the SCOPE_REF. For example;
3883 struct X { void f(); };
3884 template <typename T> void f(T* t) { t->X::f(); }
3886 Even though "t" is dependent, "X::f" is not and has
3887 been resolved to a BASELINK; there is no need to
3888 include scope information. */
3890 /* But we do need to remember that there was an explicit
3891 scope for virtual function calls. */
3893 idk = CP_ID_KIND_QUALIFIED;
3895 if (name != error_mark_node
3896 && !BASELINK_P (name)
3899 name = build_nt (SCOPE_REF, parser->scope, name);
3900 parser->scope = NULL_TREE;
3901 parser->qualifying_scope = NULL_TREE;
3902 parser->object_scope = NULL_TREE;
3905 = finish_class_member_access_expr (postfix_expression, name);
3907 /* Otherwise, try the pseudo-destructor-name production. */
3913 /* Parse the pseudo-destructor-name. */
3914 cp_parser_pseudo_destructor_name (parser, &s, &type);
3915 /* Form the call. */
3917 = finish_pseudo_destructor_expr (postfix_expression,
3918 s, TREE_TYPE (type));
3921 /* We no longer need to look up names in the scope of the
3922 object on the left-hand side of the `.' or `->'
3924 parser->context->object_type = NULL_TREE;
3925 /* These operators may not appear in constant-expressions. */
3926 if (parser->integral_constant_expression_p
3927 /* The "->" operator is allowed in the implementation
3928 of "offsetof". The "." operator may appear in the
3929 name of the member. */
3930 && !parser->in_offsetof_p)
3932 if (!parser->allow_non_integral_constant_expression_p)
3934 = (cp_parser_non_integral_constant_expression
3935 (token_type == CPP_DEREF ? "'->'" : "`.'"));
3936 parser->non_integral_constant_expression_p = true;
3942 /* postfix-expression ++ */
3943 /* Consume the `++' token. */
3944 cp_lexer_consume_token (parser->lexer);
3945 /* Generate a representation for the complete expression. */
3947 = finish_increment_expr (postfix_expression,
3948 POSTINCREMENT_EXPR);
3949 /* Increments may not appear in constant-expressions. */
3950 if (parser->integral_constant_expression_p)
3952 if (!parser->allow_non_integral_constant_expression_p)
3954 = cp_parser_non_integral_constant_expression ("an increment");
3955 parser->non_integral_constant_expression_p = true;
3957 idk = CP_ID_KIND_NONE;
3960 case CPP_MINUS_MINUS:
3961 /* postfix-expression -- */
3962 /* Consume the `--' token. */
3963 cp_lexer_consume_token (parser->lexer);
3964 /* Generate a representation for the complete expression. */
3966 = finish_increment_expr (postfix_expression,
3967 POSTDECREMENT_EXPR);
3968 /* Decrements may not appear in constant-expressions. */
3969 if (parser->integral_constant_expression_p)
3971 if (!parser->allow_non_integral_constant_expression_p)
3973 = cp_parser_non_integral_constant_expression ("a decrement");
3974 parser->non_integral_constant_expression_p = true;
3976 idk = CP_ID_KIND_NONE;
3980 return postfix_expression;
3984 /* We should never get here. */
3986 return error_mark_node;
3989 /* Parse a parenthesized expression-list.
3992 assignment-expression
3993 expression-list, assignment-expression
3998 identifier, expression-list
4000 Returns a TREE_LIST. The TREE_VALUE of each node is a
4001 representation of an assignment-expression. Note that a TREE_LIST
4002 is returned even if there is only a single expression in the list.
4003 error_mark_node is returned if the ( and or ) are
4004 missing. NULL_TREE is returned on no expressions. The parentheses
4005 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4006 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4007 indicates whether or not all of the expressions in the list were
4011 cp_parser_parenthesized_expression_list (cp_parser* parser,
4012 bool is_attribute_list,
4013 bool *non_constant_p)
4015 tree expression_list = NULL_TREE;
4016 tree identifier = NULL_TREE;
4018 /* Assume all the expressions will be constant. */
4020 *non_constant_p = false;
4022 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4023 return error_mark_node;
4025 /* Consume expressions until there are no more. */
4026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4031 /* At the beginning of attribute lists, check to see if the
4032 next token is an identifier. */
4033 if (is_attribute_list
4034 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4038 /* Consume the identifier. */
4039 token = cp_lexer_consume_token (parser->lexer);
4040 /* Save the identifier. */
4041 identifier = token->value;
4045 /* Parse the next assignment-expression. */
4048 bool expr_non_constant_p;
4049 expr = (cp_parser_constant_expression
4050 (parser, /*allow_non_constant_p=*/true,
4051 &expr_non_constant_p));
4052 if (expr_non_constant_p)
4053 *non_constant_p = true;
4056 expr = cp_parser_assignment_expression (parser);
4058 /* Add it to the list. We add error_mark_node
4059 expressions to the list, so that we can still tell if
4060 the correct form for a parenthesized expression-list
4061 is found. That gives better errors. */
4062 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4064 if (expr == error_mark_node)
4068 /* After the first item, attribute lists look the same as
4069 expression lists. */
4070 is_attribute_list = false;
4073 /* If the next token isn't a `,', then we are done. */
4074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4077 /* Otherwise, consume the `,' and keep going. */
4078 cp_lexer_consume_token (parser->lexer);
4081 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4086 /* We try and resync to an unnested comma, as that will give the
4087 user better diagnostics. */
4088 ending = cp_parser_skip_to_closing_parenthesis (parser,
4089 /*recovering=*/true,
4091 /*consume_paren=*/true);
4095 return error_mark_node;
4098 /* We built up the list in reverse order so we must reverse it now. */
4099 expression_list = nreverse (expression_list);
4101 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4103 return expression_list;
4106 /* Parse a pseudo-destructor-name.
4108 pseudo-destructor-name:
4109 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4110 :: [opt] nested-name-specifier template template-id :: ~ type-name
4111 :: [opt] nested-name-specifier [opt] ~ type-name
4113 If either of the first two productions is used, sets *SCOPE to the
4114 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4115 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4116 or ERROR_MARK_NODE if no type-name is present. */
4119 cp_parser_pseudo_destructor_name (cp_parser* parser,
4123 bool nested_name_specifier_p;
4125 /* Look for the optional `::' operator. */
4126 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4127 /* Look for the optional nested-name-specifier. */
4128 nested_name_specifier_p
4129 = (cp_parser_nested_name_specifier_opt (parser,
4130 /*typename_keyword_p=*/false,
4131 /*check_dependency_p=*/true,
4133 /*is_declaration=*/true)
4135 /* Now, if we saw a nested-name-specifier, we might be doing the
4136 second production. */
4137 if (nested_name_specifier_p
4138 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4140 /* Consume the `template' keyword. */
4141 cp_lexer_consume_token (parser->lexer);
4142 /* Parse the template-id. */
4143 cp_parser_template_id (parser,
4144 /*template_keyword_p=*/true,
4145 /*check_dependency_p=*/false,
4146 /*is_declaration=*/true);
4147 /* Look for the `::' token. */
4148 cp_parser_require (parser, CPP_SCOPE, "`::'");
4150 /* If the next token is not a `~', then there might be some
4151 additional qualification. */
4152 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4154 /* Look for the type-name. */
4155 *scope = TREE_TYPE (cp_parser_type_name (parser));
4156 /* Look for the `::' token. */
4157 cp_parser_require (parser, CPP_SCOPE, "`::'");
4162 /* Look for the `~'. */
4163 cp_parser_require (parser, CPP_COMPL, "`~'");
4164 /* Look for the type-name again. We are not responsible for
4165 checking that it matches the first type-name. */
4166 *type = cp_parser_type_name (parser);
4169 /* Parse a unary-expression.
4175 unary-operator cast-expression
4176 sizeof unary-expression
4184 __extension__ cast-expression
4185 __alignof__ unary-expression
4186 __alignof__ ( type-id )
4187 __real__ cast-expression
4188 __imag__ cast-expression
4191 ADDRESS_P is true iff the unary-expression is appearing as the
4192 operand of the `&' operator.
4194 Returns a representation of the expression. */
4197 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4200 enum tree_code unary_operator;
4202 /* Peek at the next token. */
4203 token = cp_lexer_peek_token (parser->lexer);
4204 /* Some keywords give away the kind of expression. */
4205 if (token->type == CPP_KEYWORD)
4207 enum rid keyword = token->keyword;
4217 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4218 /* Consume the token. */
4219 cp_lexer_consume_token (parser->lexer);
4220 /* Parse the operand. */
4221 operand = cp_parser_sizeof_operand (parser, keyword);
4223 if (TYPE_P (operand))
4224 return cxx_sizeof_or_alignof_type (operand, op, true);
4226 return cxx_sizeof_or_alignof_expr (operand, op);
4230 return cp_parser_new_expression (parser);
4233 return cp_parser_delete_expression (parser);
4237 /* The saved value of the PEDANTIC flag. */
4241 /* Save away the PEDANTIC flag. */
4242 cp_parser_extension_opt (parser, &saved_pedantic);
4243 /* Parse the cast-expression. */
4244 expr = cp_parser_simple_cast_expression (parser);
4245 /* Restore the PEDANTIC flag. */
4246 pedantic = saved_pedantic;
4256 /* Consume the `__real__' or `__imag__' token. */
4257 cp_lexer_consume_token (parser->lexer);
4258 /* Parse the cast-expression. */
4259 expression = cp_parser_simple_cast_expression (parser);
4260 /* Create the complete representation. */
4261 return build_x_unary_op ((keyword == RID_REALPART
4262 ? REALPART_EXPR : IMAGPART_EXPR),
4272 /* Look for the `:: new' and `:: delete', which also signal the
4273 beginning of a new-expression, or delete-expression,
4274 respectively. If the next token is `::', then it might be one of
4276 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4280 /* See if the token after the `::' is one of the keywords in
4281 which we're interested. */
4282 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4283 /* If it's `new', we have a new-expression. */
4284 if (keyword == RID_NEW)
4285 return cp_parser_new_expression (parser);
4286 /* Similarly, for `delete'. */
4287 else if (keyword == RID_DELETE)
4288 return cp_parser_delete_expression (parser);
4291 /* Look for a unary operator. */
4292 unary_operator = cp_parser_unary_operator (token);
4293 /* The `++' and `--' operators can be handled similarly, even though
4294 they are not technically unary-operators in the grammar. */
4295 if (unary_operator == ERROR_MARK)
4297 if (token->type == CPP_PLUS_PLUS)
4298 unary_operator = PREINCREMENT_EXPR;
4299 else if (token->type == CPP_MINUS_MINUS)
4300 unary_operator = PREDECREMENT_EXPR;
4301 /* Handle the GNU address-of-label extension. */
4302 else if (cp_parser_allow_gnu_extensions_p (parser)
4303 && token->type == CPP_AND_AND)
4307 /* Consume the '&&' token. */
4308 cp_lexer_consume_token (parser->lexer);
4309 /* Look for the identifier. */
4310 identifier = cp_parser_identifier (parser);
4311 /* Create an expression representing the address. */
4312 return finish_label_address_expr (identifier);
4315 if (unary_operator != ERROR_MARK)
4317 tree cast_expression;
4318 tree expression = error_mark_node;
4319 const char *non_constant_p = NULL;
4321 /* Consume the operator token. */
4322 token = cp_lexer_consume_token (parser->lexer);
4323 /* Parse the cast-expression. */
4325 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4326 /* Now, build an appropriate representation. */
4327 switch (unary_operator)
4330 non_constant_p = "`*'";
4331 expression = build_x_indirect_ref (cast_expression, "unary *");
4335 /* The "&" operator is allowed in the implementation of
4337 if (!parser->in_offsetof_p)
4338 non_constant_p = "`&'";
4341 expression = build_x_unary_op (unary_operator, cast_expression);
4344 case PREINCREMENT_EXPR:
4345 case PREDECREMENT_EXPR:
4346 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4351 case TRUTH_NOT_EXPR:
4352 expression = finish_unary_op_expr (unary_operator, cast_expression);
4359 if (non_constant_p && parser->integral_constant_expression_p)
4361 if (!parser->allow_non_integral_constant_expression_p)
4362 return cp_parser_non_integral_constant_expression (non_constant_p);
4363 parser->non_integral_constant_expression_p = true;
4369 return cp_parser_postfix_expression (parser, address_p);
4372 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4373 unary-operator, the corresponding tree code is returned. */
4375 static enum tree_code
4376 cp_parser_unary_operator (cp_token* token)
4378 switch (token->type)
4381 return INDIRECT_REF;
4387 return CONVERT_EXPR;
4393 return TRUTH_NOT_EXPR;
4396 return BIT_NOT_EXPR;
4403 /* Parse a new-expression.
4406 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4407 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4409 Returns a representation of the expression. */
4412 cp_parser_new_expression (cp_parser* parser)
4414 bool global_scope_p;
4419 /* Look for the optional `::' operator. */
4421 = (cp_parser_global_scope_opt (parser,
4422 /*current_scope_valid_p=*/false)
4424 /* Look for the `new' operator. */
4425 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4426 /* There's no easy way to tell a new-placement from the
4427 `( type-id )' construct. */
4428 cp_parser_parse_tentatively (parser);
4429 /* Look for a new-placement. */
4430 placement = cp_parser_new_placement (parser);
4431 /* If that didn't work out, there's no new-placement. */
4432 if (!cp_parser_parse_definitely (parser))
4433 placement = NULL_TREE;
4435 /* If the next token is a `(', then we have a parenthesized
4437 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4439 /* Consume the `('. */
4440 cp_lexer_consume_token (parser->lexer);
4441 /* Parse the type-id. */
4442 type = cp_parser_type_id (parser);
4443 /* Look for the closing `)'. */
4444 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4446 /* Otherwise, there must be a new-type-id. */
4448 type = cp_parser_new_type_id (parser);
4450 /* If the next token is a `(', then we have a new-initializer. */
4451 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4452 initializer = cp_parser_new_initializer (parser);
4454 initializer = NULL_TREE;
4456 /* Create a representation of the new-expression. */
4457 return build_new (placement, type, initializer, global_scope_p);
4460 /* Parse a new-placement.
4465 Returns the same representation as for an expression-list. */
4468 cp_parser_new_placement (cp_parser* parser)
4470 tree expression_list;
4472 /* Parse the expression-list. */
4473 expression_list = (cp_parser_parenthesized_expression_list
4474 (parser, false, /*non_constant_p=*/NULL));
4476 return expression_list;
4479 /* Parse a new-type-id.
4482 type-specifier-seq new-declarator [opt]
4484 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4485 and whose TREE_VALUE is the new-declarator. */
4488 cp_parser_new_type_id (cp_parser* parser)
4490 tree type_specifier_seq;
4492 const char *saved_message;
4494 /* The type-specifier sequence must not contain type definitions.
4495 (It cannot contain declarations of new types either, but if they
4496 are not definitions we will catch that because they are not
4498 saved_message = parser->type_definition_forbidden_message;
4499 parser->type_definition_forbidden_message
4500 = "types may not be defined in a new-type-id";
4501 /* Parse the type-specifier-seq. */
4502 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4503 /* Restore the old message. */
4504 parser->type_definition_forbidden_message = saved_message;
4505 /* Parse the new-declarator. */
4506 declarator = cp_parser_new_declarator_opt (parser);
4508 return build_tree_list (type_specifier_seq, declarator);
4511 /* Parse an (optional) new-declarator.
4514 ptr-operator new-declarator [opt]
4515 direct-new-declarator
4517 Returns a representation of the declarator. See
4518 cp_parser_declarator for the representations used. */
4521 cp_parser_new_declarator_opt (cp_parser* parser)
4523 enum tree_code code;
4525 tree cv_qualifier_seq;
4527 /* We don't know if there's a ptr-operator next, or not. */
4528 cp_parser_parse_tentatively (parser);
4529 /* Look for a ptr-operator. */
4530 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4531 /* If that worked, look for more new-declarators. */
4532 if (cp_parser_parse_definitely (parser))
4536 /* Parse another optional declarator. */
4537 declarator = cp_parser_new_declarator_opt (parser);
4539 /* Create the representation of the declarator. */
4540 if (code == INDIRECT_REF)
4541 declarator = make_pointer_declarator (cv_qualifier_seq,
4544 declarator = make_reference_declarator (cv_qualifier_seq,
4547 /* Handle the pointer-to-member case. */
4549 declarator = build_nt (SCOPE_REF, type, declarator);
4554 /* If the next token is a `[', there is a direct-new-declarator. */
4555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4556 return cp_parser_direct_new_declarator (parser);
4561 /* Parse a direct-new-declarator.
4563 direct-new-declarator:
4565 direct-new-declarator [constant-expression]
4567 Returns an ARRAY_REF, following the same conventions as are
4568 documented for cp_parser_direct_declarator. */
4571 cp_parser_direct_new_declarator (cp_parser* parser)
4573 tree declarator = NULL_TREE;
4579 /* Look for the opening `['. */
4580 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4581 /* The first expression is not required to be constant. */
4584 expression = cp_parser_expression (parser);
4585 /* The standard requires that the expression have integral
4586 type. DR 74 adds enumeration types. We believe that the
4587 real intent is that these expressions be handled like the
4588 expression in a `switch' condition, which also allows
4589 classes with a single conversion to integral or
4590 enumeration type. */
4591 if (!processing_template_decl)
4594 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4599 error ("expression in new-declarator must have integral or enumeration type");
4600 expression = error_mark_node;
4604 /* But all the other expressions must be. */
4607 = cp_parser_constant_expression (parser,
4608 /*allow_non_constant=*/false,
4610 /* Look for the closing `]'. */
4611 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4613 /* Add this bound to the declarator. */
4614 declarator = build_nt (ARRAY_REF, declarator, expression);
4616 /* If the next token is not a `[', then there are no more
4618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4625 /* Parse a new-initializer.
4628 ( expression-list [opt] )
4630 Returns a representation of the expression-list. If there is no
4631 expression-list, VOID_ZERO_NODE is returned. */
4634 cp_parser_new_initializer (cp_parser* parser)
4636 tree expression_list;
4638 expression_list = (cp_parser_parenthesized_expression_list
4639 (parser, false, /*non_constant_p=*/NULL));
4640 if (!expression_list)
4641 expression_list = void_zero_node;
4643 return expression_list;
4646 /* Parse a delete-expression.
4649 :: [opt] delete cast-expression
4650 :: [opt] delete [ ] cast-expression
4652 Returns a representation of the expression. */
4655 cp_parser_delete_expression (cp_parser* parser)
4657 bool global_scope_p;
4661 /* Look for the optional `::' operator. */
4663 = (cp_parser_global_scope_opt (parser,
4664 /*current_scope_valid_p=*/false)
4666 /* Look for the `delete' keyword. */
4667 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4668 /* See if the array syntax is in use. */
4669 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4671 /* Consume the `[' token. */
4672 cp_lexer_consume_token (parser->lexer);
4673 /* Look for the `]' token. */
4674 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4675 /* Remember that this is the `[]' construct. */
4681 /* Parse the cast-expression. */
4682 expression = cp_parser_simple_cast_expression (parser);
4684 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4687 /* Parse a cast-expression.
4691 ( type-id ) cast-expression
4693 Returns a representation of the expression. */
4696 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4698 /* If it's a `(', then we might be looking at a cast. */
4699 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4701 tree type = NULL_TREE;
4702 tree expr = NULL_TREE;
4703 bool compound_literal_p;
4704 const char *saved_message;
4706 /* There's no way to know yet whether or not this is a cast.
4707 For example, `(int (3))' is a unary-expression, while `(int)
4708 3' is a cast. So, we resort to parsing tentatively. */
4709 cp_parser_parse_tentatively (parser);
4710 /* Types may not be defined in a cast. */
4711 saved_message = parser->type_definition_forbidden_message;
4712 parser->type_definition_forbidden_message
4713 = "types may not be defined in casts";
4714 /* Consume the `('. */
4715 cp_lexer_consume_token (parser->lexer);
4716 /* A very tricky bit is that `(struct S) { 3 }' is a
4717 compound-literal (which we permit in C++ as an extension).
4718 But, that construct is not a cast-expression -- it is a
4719 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4720 is legal; if the compound-literal were a cast-expression,
4721 you'd need an extra set of parentheses.) But, if we parse
4722 the type-id, and it happens to be a class-specifier, then we
4723 will commit to the parse at that point, because we cannot
4724 undo the action that is done when creating a new class. So,
4725 then we cannot back up and do a postfix-expression.
4727 Therefore, we scan ahead to the closing `)', and check to see
4728 if the token after the `)' is a `{'. If so, we are not
4729 looking at a cast-expression.
4731 Save tokens so that we can put them back. */
4732 cp_lexer_save_tokens (parser->lexer);
4733 /* Skip tokens until the next token is a closing parenthesis.
4734 If we find the closing `)', and the next token is a `{', then
4735 we are looking at a compound-literal. */
4737 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4738 /*consume_paren=*/true)
4739 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4740 /* Roll back the tokens we skipped. */
4741 cp_lexer_rollback_tokens (parser->lexer);
4742 /* If we were looking at a compound-literal, simulate an error
4743 so that the call to cp_parser_parse_definitely below will
4745 if (compound_literal_p)
4746 cp_parser_simulate_error (parser);
4749 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4750 parser->in_type_id_in_expr_p = true;
4751 /* Look for the type-id. */
4752 type = cp_parser_type_id (parser);
4753 /* Look for the closing `)'. */
4754 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4755 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4758 /* Restore the saved message. */
4759 parser->type_definition_forbidden_message = saved_message;
4761 /* If ok so far, parse the dependent expression. We cannot be
4762 sure it is a cast. Consider `(T ())'. It is a parenthesized
4763 ctor of T, but looks like a cast to function returning T
4764 without a dependent expression. */
4765 if (!cp_parser_error_occurred (parser))
4766 expr = cp_parser_simple_cast_expression (parser);
4768 if (cp_parser_parse_definitely (parser))
4770 /* Warn about old-style casts, if so requested. */
4771 if (warn_old_style_cast
4772 && !in_system_header
4773 && !VOID_TYPE_P (type)
4774 && current_lang_name != lang_name_c)
4775 warning ("use of old-style cast");
4777 /* Only type conversions to integral or enumeration types
4778 can be used in constant-expressions. */
4779 if (parser->integral_constant_expression_p
4780 && !dependent_type_p (type)
4781 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4783 if (!parser->allow_non_integral_constant_expression_p)
4784 return (cp_parser_non_integral_constant_expression
4785 ("a casts to a type other than an integral or "
4786 "enumeration type"));
4787 parser->non_integral_constant_expression_p = true;
4789 /* Perform the cast. */
4790 expr = build_c_cast (type, expr);
4795 /* If we get here, then it's not a cast, so it must be a
4796 unary-expression. */
4797 return cp_parser_unary_expression (parser, address_p);
4800 /* Parse a pm-expression.
4804 pm-expression .* cast-expression
4805 pm-expression ->* cast-expression
4807 Returns a representation of the expression. */
4810 cp_parser_pm_expression (cp_parser* parser)
4812 static const cp_parser_token_tree_map map = {
4813 { CPP_DEREF_STAR, MEMBER_REF },
4814 { CPP_DOT_STAR, DOTSTAR_EXPR },
4815 { CPP_EOF, ERROR_MARK }
4818 return cp_parser_binary_expression (parser, map,
4819 cp_parser_simple_cast_expression);
4822 /* Parse a multiplicative-expression.
4824 mulitplicative-expression:
4826 multiplicative-expression * pm-expression
4827 multiplicative-expression / pm-expression
4828 multiplicative-expression % pm-expression
4830 Returns a representation of the expression. */
4833 cp_parser_multiplicative_expression (cp_parser* parser)
4835 static const cp_parser_token_tree_map map = {
4836 { CPP_MULT, MULT_EXPR },
4837 { CPP_DIV, TRUNC_DIV_EXPR },
4838 { CPP_MOD, TRUNC_MOD_EXPR },
4839 { CPP_EOF, ERROR_MARK }
4842 return cp_parser_binary_expression (parser,
4844 cp_parser_pm_expression);
4847 /* Parse an additive-expression.
4849 additive-expression:
4850 multiplicative-expression
4851 additive-expression + multiplicative-expression
4852 additive-expression - multiplicative-expression
4854 Returns a representation of the expression. */
4857 cp_parser_additive_expression (cp_parser* parser)
4859 static const cp_parser_token_tree_map map = {
4860 { CPP_PLUS, PLUS_EXPR },
4861 { CPP_MINUS, MINUS_EXPR },
4862 { CPP_EOF, ERROR_MARK }
4865 return cp_parser_binary_expression (parser,
4867 cp_parser_multiplicative_expression);
4870 /* Parse a shift-expression.
4874 shift-expression << additive-expression
4875 shift-expression >> additive-expression
4877 Returns a representation of the expression. */
4880 cp_parser_shift_expression (cp_parser* parser)
4882 static const cp_parser_token_tree_map map = {
4883 { CPP_LSHIFT, LSHIFT_EXPR },
4884 { CPP_RSHIFT, RSHIFT_EXPR },
4885 { CPP_EOF, ERROR_MARK }
4888 return cp_parser_binary_expression (parser,
4890 cp_parser_additive_expression);
4893 /* Parse a relational-expression.
4895 relational-expression:
4897 relational-expression < shift-expression
4898 relational-expression > shift-expression
4899 relational-expression <= shift-expression
4900 relational-expression >= shift-expression
4904 relational-expression:
4905 relational-expression <? shift-expression
4906 relational-expression >? shift-expression
4908 Returns a representation of the expression. */
4911 cp_parser_relational_expression (cp_parser* parser)
4913 static const cp_parser_token_tree_map map = {
4914 { CPP_LESS, LT_EXPR },
4915 { CPP_GREATER, GT_EXPR },
4916 { CPP_LESS_EQ, LE_EXPR },
4917 { CPP_GREATER_EQ, GE_EXPR },
4918 { CPP_MIN, MIN_EXPR },
4919 { CPP_MAX, MAX_EXPR },
4920 { CPP_EOF, ERROR_MARK }
4923 return cp_parser_binary_expression (parser,
4925 cp_parser_shift_expression);
4928 /* Parse an equality-expression.
4930 equality-expression:
4931 relational-expression
4932 equality-expression == relational-expression
4933 equality-expression != relational-expression
4935 Returns a representation of the expression. */
4938 cp_parser_equality_expression (cp_parser* parser)
4940 static const cp_parser_token_tree_map map = {
4941 { CPP_EQ_EQ, EQ_EXPR },
4942 { CPP_NOT_EQ, NE_EXPR },
4943 { CPP_EOF, ERROR_MARK }
4946 return cp_parser_binary_expression (parser,
4948 cp_parser_relational_expression);
4951 /* Parse an and-expression.
4955 and-expression & equality-expression
4957 Returns a representation of the expression. */
4960 cp_parser_and_expression (cp_parser* parser)
4962 static const cp_parser_token_tree_map map = {
4963 { CPP_AND, BIT_AND_EXPR },
4964 { CPP_EOF, ERROR_MARK }
4967 return cp_parser_binary_expression (parser,
4969 cp_parser_equality_expression);
4972 /* Parse an exclusive-or-expression.
4974 exclusive-or-expression:
4976 exclusive-or-expression ^ and-expression
4978 Returns a representation of the expression. */
4981 cp_parser_exclusive_or_expression (cp_parser* parser)
4983 static const cp_parser_token_tree_map map = {
4984 { CPP_XOR, BIT_XOR_EXPR },
4985 { CPP_EOF, ERROR_MARK }
4988 return cp_parser_binary_expression (parser,
4990 cp_parser_and_expression);
4994 /* Parse an inclusive-or-expression.
4996 inclusive-or-expression:
4997 exclusive-or-expression
4998 inclusive-or-expression | exclusive-or-expression
5000 Returns a representation of the expression. */
5003 cp_parser_inclusive_or_expression (cp_parser* parser)
5005 static const cp_parser_token_tree_map map = {
5006 { CPP_OR, BIT_IOR_EXPR },
5007 { CPP_EOF, ERROR_MARK }
5010 return cp_parser_binary_expression (parser,
5012 cp_parser_exclusive_or_expression);
5015 /* Parse a logical-and-expression.
5017 logical-and-expression:
5018 inclusive-or-expression
5019 logical-and-expression && inclusive-or-expression
5021 Returns a representation of the expression. */
5024 cp_parser_logical_and_expression (cp_parser* parser)
5026 static const cp_parser_token_tree_map map = {
5027 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5028 { CPP_EOF, ERROR_MARK }
5031 return cp_parser_binary_expression (parser,
5033 cp_parser_inclusive_or_expression);
5036 /* Parse a logical-or-expression.
5038 logical-or-expression:
5039 logical-and-expression
5040 logical-or-expression || logical-and-expression
5042 Returns a representation of the expression. */
5045 cp_parser_logical_or_expression (cp_parser* parser)
5047 static const cp_parser_token_tree_map map = {
5048 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5049 { CPP_EOF, ERROR_MARK }
5052 return cp_parser_binary_expression (parser,
5054 cp_parser_logical_and_expression);
5057 /* Parse the `? expression : assignment-expression' part of a
5058 conditional-expression. The LOGICAL_OR_EXPR is the
5059 logical-or-expression that started the conditional-expression.
5060 Returns a representation of the entire conditional-expression.
5062 This routine is used by cp_parser_assignment_expression.
5064 ? expression : assignment-expression
5068 ? : assignment-expression */
5071 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5074 tree assignment_expr;
5076 /* Consume the `?' token. */
5077 cp_lexer_consume_token (parser->lexer);
5078 if (cp_parser_allow_gnu_extensions_p (parser)
5079 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5080 /* Implicit true clause. */
5083 /* Parse the expression. */
5084 expr = cp_parser_expression (parser);
5086 /* The next token should be a `:'. */
5087 cp_parser_require (parser, CPP_COLON, "`:'");
5088 /* Parse the assignment-expression. */
5089 assignment_expr = cp_parser_assignment_expression (parser);
5091 /* Build the conditional-expression. */
5092 return build_x_conditional_expr (logical_or_expr,
5097 /* Parse an assignment-expression.
5099 assignment-expression:
5100 conditional-expression
5101 logical-or-expression assignment-operator assignment_expression
5104 Returns a representation for the expression. */
5107 cp_parser_assignment_expression (cp_parser* parser)
5111 /* If the next token is the `throw' keyword, then we're looking at
5112 a throw-expression. */
5113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5114 expr = cp_parser_throw_expression (parser);
5115 /* Otherwise, it must be that we are looking at a
5116 logical-or-expression. */
5119 /* Parse the logical-or-expression. */
5120 expr = cp_parser_logical_or_expression (parser);
5121 /* If the next token is a `?' then we're actually looking at a
5122 conditional-expression. */
5123 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5124 return cp_parser_question_colon_clause (parser, expr);
5127 enum tree_code assignment_operator;
5129 /* If it's an assignment-operator, we're using the second
5132 = cp_parser_assignment_operator_opt (parser);
5133 if (assignment_operator != ERROR_MARK)
5137 /* Parse the right-hand side of the assignment. */
5138 rhs = cp_parser_assignment_expression (parser);
5139 /* An assignment may not appear in a
5140 constant-expression. */
5141 if (parser->integral_constant_expression_p)
5143 if (!parser->allow_non_integral_constant_expression_p)
5144 return cp_parser_non_integral_constant_expression ("an assignment");
5145 parser->non_integral_constant_expression_p = true;
5147 /* Build the assignment expression. */
5148 expr = build_x_modify_expr (expr,
5149 assignment_operator,
5158 /* Parse an (optional) assignment-operator.
5160 assignment-operator: one of
5161 = *= /= %= += -= >>= <<= &= ^= |=
5165 assignment-operator: one of
5168 If the next token is an assignment operator, the corresponding tree
5169 code is returned, and the token is consumed. For example, for
5170 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5171 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5172 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5173 operator, ERROR_MARK is returned. */
5175 static enum tree_code
5176 cp_parser_assignment_operator_opt (cp_parser* parser)
5181 /* Peek at the next toen. */
5182 token = cp_lexer_peek_token (parser->lexer);
5184 switch (token->type)
5195 op = TRUNC_DIV_EXPR;
5199 op = TRUNC_MOD_EXPR;
5239 /* Nothing else is an assignment operator. */
5243 /* If it was an assignment operator, consume it. */
5244 if (op != ERROR_MARK)
5245 cp_lexer_consume_token (parser->lexer);
5250 /* Parse an expression.
5253 assignment-expression
5254 expression , assignment-expression
5256 Returns a representation of the expression. */
5259 cp_parser_expression (cp_parser* parser)
5261 tree expression = NULL_TREE;
5265 tree assignment_expression;
5267 /* Parse the next assignment-expression. */
5268 assignment_expression
5269 = cp_parser_assignment_expression (parser);
5270 /* If this is the first assignment-expression, we can just
5273 expression = assignment_expression;
5275 expression = build_x_compound_expr (expression,
5276 assignment_expression);
5277 /* If the next token is not a comma, then we are done with the
5279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5281 /* Consume the `,'. */
5282 cp_lexer_consume_token (parser->lexer);
5283 /* A comma operator cannot appear in a constant-expression. */
5284 if (parser->integral_constant_expression_p)
5286 if (!parser->allow_non_integral_constant_expression_p)
5288 = cp_parser_non_integral_constant_expression ("a comma operator");
5289 parser->non_integral_constant_expression_p = true;
5296 /* Parse a constant-expression.
5298 constant-expression:
5299 conditional-expression
5301 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5302 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5303 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5304 is false, NON_CONSTANT_P should be NULL. */
5307 cp_parser_constant_expression (cp_parser* parser,
5308 bool allow_non_constant_p,
5309 bool *non_constant_p)
5311 bool saved_integral_constant_expression_p;
5312 bool saved_allow_non_integral_constant_expression_p;
5313 bool saved_non_integral_constant_expression_p;
5316 /* It might seem that we could simply parse the
5317 conditional-expression, and then check to see if it were
5318 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5319 one that the compiler can figure out is constant, possibly after
5320 doing some simplifications or optimizations. The standard has a
5321 precise definition of constant-expression, and we must honor
5322 that, even though it is somewhat more restrictive.
5328 is not a legal declaration, because `(2, 3)' is not a
5329 constant-expression. The `,' operator is forbidden in a
5330 constant-expression. However, GCC's constant-folding machinery
5331 will fold this operation to an INTEGER_CST for `3'. */
5333 /* Save the old settings. */
5334 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5335 saved_allow_non_integral_constant_expression_p
5336 = parser->allow_non_integral_constant_expression_p;
5337 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5338 /* We are now parsing a constant-expression. */
5339 parser->integral_constant_expression_p = true;
5340 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5341 parser->non_integral_constant_expression_p = false;
5342 /* Although the grammar says "conditional-expression", we parse an
5343 "assignment-expression", which also permits "throw-expression"
5344 and the use of assignment operators. In the case that
5345 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5346 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5347 actually essential that we look for an assignment-expression.
5348 For example, cp_parser_initializer_clauses uses this function to
5349 determine whether a particular assignment-expression is in fact
5351 expression = cp_parser_assignment_expression (parser);
5352 /* Restore the old settings. */
5353 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5354 parser->allow_non_integral_constant_expression_p
5355 = saved_allow_non_integral_constant_expression_p;
5356 if (allow_non_constant_p)
5357 *non_constant_p = parser->non_integral_constant_expression_p;
5358 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5363 /* Statements [gram.stmt.stmt] */
5365 /* Parse a statement.
5369 expression-statement
5374 declaration-statement
5378 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5382 int statement_line_number;
5384 /* There is no statement yet. */
5385 statement = NULL_TREE;
5386 /* Peek at the next token. */
5387 token = cp_lexer_peek_token (parser->lexer);
5388 /* Remember the line number of the first token in the statement. */
5389 statement_line_number = token->location.line;
5390 /* If this is a keyword, then that will often determine what kind of
5391 statement we have. */
5392 if (token->type == CPP_KEYWORD)
5394 enum rid keyword = token->keyword;
5400 statement = cp_parser_labeled_statement (parser,
5401 in_statement_expr_p);
5406 statement = cp_parser_selection_statement (parser);
5412 statement = cp_parser_iteration_statement (parser);
5419 statement = cp_parser_jump_statement (parser);
5423 statement = cp_parser_try_block (parser);
5427 /* It might be a keyword like `int' that can start a
5428 declaration-statement. */
5432 else if (token->type == CPP_NAME)
5434 /* If the next token is a `:', then we are looking at a
5435 labeled-statement. */
5436 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5437 if (token->type == CPP_COLON)
5438 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5440 /* Anything that starts with a `{' must be a compound-statement. */
5441 else if (token->type == CPP_OPEN_BRACE)
5442 statement = cp_parser_compound_statement (parser, false);
5444 /* Everything else must be a declaration-statement or an
5445 expression-statement. Try for the declaration-statement
5446 first, unless we are looking at a `;', in which case we know that
5447 we have an expression-statement. */
5450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5452 cp_parser_parse_tentatively (parser);
5453 /* Try to parse the declaration-statement. */
5454 cp_parser_declaration_statement (parser);
5455 /* If that worked, we're done. */
5456 if (cp_parser_parse_definitely (parser))
5459 /* Look for an expression-statement instead. */
5460 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5463 /* Set the line number for the statement. */
5464 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5465 STMT_LINENO (statement) = statement_line_number;
5468 /* Parse a labeled-statement.
5471 identifier : statement
5472 case constant-expression : statement
5478 case constant-expression ... constant-expression : statement
5480 Returns the new CASE_LABEL, for a `case' or `default' label. For
5481 an ordinary label, returns a LABEL_STMT. */
5484 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5487 tree statement = error_mark_node;
5489 /* The next token should be an identifier. */
5490 token = cp_lexer_peek_token (parser->lexer);
5491 if (token->type != CPP_NAME
5492 && token->type != CPP_KEYWORD)
5494 cp_parser_error (parser, "expected labeled-statement");
5495 return error_mark_node;
5498 switch (token->keyword)
5505 /* Consume the `case' token. */
5506 cp_lexer_consume_token (parser->lexer);
5507 /* Parse the constant-expression. */
5508 expr = cp_parser_constant_expression (parser,
5509 /*allow_non_constant_p=*/false,
5512 ellipsis = cp_lexer_peek_token (parser->lexer);
5513 if (ellipsis->type == CPP_ELLIPSIS)
5515 /* Consume the `...' token. */
5516 cp_lexer_consume_token (parser->lexer);
5518 cp_parser_constant_expression (parser,
5519 /*allow_non_constant_p=*/false,
5521 /* We don't need to emit warnings here, as the common code
5522 will do this for us. */
5525 expr_hi = NULL_TREE;
5527 if (!parser->in_switch_statement_p)
5528 error ("case label `%E' not within a switch statement", expr);
5530 statement = finish_case_label (expr, expr_hi);
5535 /* Consume the `default' token. */
5536 cp_lexer_consume_token (parser->lexer);
5537 if (!parser->in_switch_statement_p)
5538 error ("case label not within a switch statement");
5540 statement = finish_case_label (NULL_TREE, NULL_TREE);
5544 /* Anything else must be an ordinary label. */
5545 statement = finish_label_stmt (cp_parser_identifier (parser));
5549 /* Require the `:' token. */
5550 cp_parser_require (parser, CPP_COLON, "`:'");
5551 /* Parse the labeled statement. */
5552 cp_parser_statement (parser, in_statement_expr_p);
5554 /* Return the label, in the case of a `case' or `default' label. */
5558 /* Parse an expression-statement.
5560 expression-statement:
5563 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5564 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5565 indicates whether this expression-statement is part of an
5566 expression statement. */
5569 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5571 tree statement = NULL_TREE;
5573 /* If the next token is a ';', then there is no expression
5575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5576 statement = cp_parser_expression (parser);
5578 /* Consume the final `;'. */
5579 cp_parser_consume_semicolon_at_end_of_statement (parser);
5581 if (in_statement_expr_p
5582 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5584 /* This is the final expression statement of a statement
5586 statement = finish_stmt_expr_expr (statement);
5589 statement = finish_expr_stmt (statement);
5596 /* Parse a compound-statement.
5599 { statement-seq [opt] }
5601 Returns a COMPOUND_STMT representing the statement. */
5604 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5608 /* Consume the `{'. */
5609 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5610 return error_mark_node;
5611 /* Begin the compound-statement. */
5612 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5613 /* Parse an (optional) statement-seq. */
5614 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5615 /* Finish the compound-statement. */
5616 finish_compound_stmt (compound_stmt);
5617 /* Consume the `}'. */
5618 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5620 return compound_stmt;
5623 /* Parse an (optional) statement-seq.
5627 statement-seq [opt] statement */
5630 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5632 /* Scan statements until there aren't any more. */
5635 /* If we're looking at a `}', then we've run out of statements. */
5636 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5637 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5640 /* Parse the statement. */
5641 cp_parser_statement (parser, in_statement_expr_p);
5645 /* Parse a selection-statement.
5647 selection-statement:
5648 if ( condition ) statement
5649 if ( condition ) statement else statement
5650 switch ( condition ) statement
5652 Returns the new IF_STMT or SWITCH_STMT. */
5655 cp_parser_selection_statement (cp_parser* parser)
5660 /* Peek at the next token. */
5661 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5663 /* See what kind of keyword it is. */
5664 keyword = token->keyword;
5673 /* Look for the `('. */
5674 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5676 cp_parser_skip_to_end_of_statement (parser);
5677 return error_mark_node;
5680 /* Begin the selection-statement. */
5681 if (keyword == RID_IF)
5682 statement = begin_if_stmt ();
5684 statement = begin_switch_stmt ();
5686 /* Parse the condition. */
5687 condition = cp_parser_condition (parser);
5688 /* Look for the `)'. */
5689 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5690 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5691 /*consume_paren=*/true);
5693 if (keyword == RID_IF)
5697 /* Add the condition. */
5698 finish_if_stmt_cond (condition, statement);
5700 /* Parse the then-clause. */
5701 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5702 finish_then_clause (statement);
5704 /* If the next token is `else', parse the else-clause. */
5705 if (cp_lexer_next_token_is_keyword (parser->lexer,
5710 /* Consume the `else' keyword. */
5711 cp_lexer_consume_token (parser->lexer);
5712 /* Parse the else-clause. */
5714 = cp_parser_implicitly_scoped_statement (parser);
5715 finish_else_clause (statement);
5718 /* Now we're all done with the if-statement. */
5724 bool in_switch_statement_p;
5726 /* Add the condition. */
5727 finish_switch_cond (condition, statement);
5729 /* Parse the body of the switch-statement. */
5730 in_switch_statement_p = parser->in_switch_statement_p;
5731 parser->in_switch_statement_p = true;
5732 body = cp_parser_implicitly_scoped_statement (parser);
5733 parser->in_switch_statement_p = in_switch_statement_p;
5735 /* Now we're all done with the switch-statement. */
5736 finish_switch_stmt (statement);
5744 cp_parser_error (parser, "expected selection-statement");
5745 return error_mark_node;
5749 /* Parse a condition.
5753 type-specifier-seq declarator = assignment-expression
5758 type-specifier-seq declarator asm-specification [opt]
5759 attributes [opt] = assignment-expression
5761 Returns the expression that should be tested. */
5764 cp_parser_condition (cp_parser* parser)
5766 tree type_specifiers;
5767 const char *saved_message;
5769 /* Try the declaration first. */
5770 cp_parser_parse_tentatively (parser);
5771 /* New types are not allowed in the type-specifier-seq for a
5773 saved_message = parser->type_definition_forbidden_message;
5774 parser->type_definition_forbidden_message
5775 = "types may not be defined in conditions";
5776 /* Parse the type-specifier-seq. */
5777 type_specifiers = cp_parser_type_specifier_seq (parser);
5778 /* Restore the saved message. */
5779 parser->type_definition_forbidden_message = saved_message;
5780 /* If all is well, we might be looking at a declaration. */
5781 if (!cp_parser_error_occurred (parser))
5784 tree asm_specification;
5787 tree initializer = NULL_TREE;
5789 /* Parse the declarator. */
5790 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5791 /*ctor_dtor_or_conv_p=*/NULL,
5792 /*parenthesized_p=*/NULL);
5793 /* Parse the attributes. */
5794 attributes = cp_parser_attributes_opt (parser);
5795 /* Parse the asm-specification. */
5796 asm_specification = cp_parser_asm_specification_opt (parser);
5797 /* If the next token is not an `=', then we might still be
5798 looking at an expression. For example:
5802 looks like a decl-specifier-seq and a declarator -- but then
5803 there is no `=', so this is an expression. */
5804 cp_parser_require (parser, CPP_EQ, "`='");
5805 /* If we did see an `=', then we are looking at a declaration
5807 if (cp_parser_parse_definitely (parser))
5809 /* Create the declaration. */
5810 decl = start_decl (declarator, type_specifiers,
5811 /*initialized_p=*/true,
5812 attributes, /*prefix_attributes=*/NULL_TREE);
5813 /* Parse the assignment-expression. */
5814 initializer = cp_parser_assignment_expression (parser);
5816 /* Process the initializer. */
5817 cp_finish_decl (decl,
5820 LOOKUP_ONLYCONVERTING);
5822 return convert_from_reference (decl);
5825 /* If we didn't even get past the declarator successfully, we are
5826 definitely not looking at a declaration. */
5828 cp_parser_abort_tentative_parse (parser);
5830 /* Otherwise, we are looking at an expression. */
5831 return cp_parser_expression (parser);
5834 /* Parse an iteration-statement.
5836 iteration-statement:
5837 while ( condition ) statement
5838 do statement while ( expression ) ;
5839 for ( for-init-statement condition [opt] ; expression [opt] )
5842 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5845 cp_parser_iteration_statement (cp_parser* parser)
5850 bool in_iteration_statement_p;
5853 /* Peek at the next token. */
5854 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5856 return error_mark_node;
5858 /* Remember whether or not we are already within an iteration
5860 in_iteration_statement_p = parser->in_iteration_statement_p;
5862 /* See what kind of keyword it is. */
5863 keyword = token->keyword;
5870 /* Begin the while-statement. */
5871 statement = begin_while_stmt ();
5872 /* Look for the `('. */
5873 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5874 /* Parse the condition. */
5875 condition = cp_parser_condition (parser);
5876 finish_while_stmt_cond (condition, statement);
5877 /* Look for the `)'. */
5878 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5879 /* Parse the dependent statement. */
5880 parser->in_iteration_statement_p = true;
5881 cp_parser_already_scoped_statement (parser);
5882 parser->in_iteration_statement_p = in_iteration_statement_p;
5883 /* We're done with the while-statement. */
5884 finish_while_stmt (statement);
5892 /* Begin the do-statement. */
5893 statement = begin_do_stmt ();
5894 /* Parse the body of the do-statement. */
5895 parser->in_iteration_statement_p = true;
5896 cp_parser_implicitly_scoped_statement (parser);
5897 parser->in_iteration_statement_p = in_iteration_statement_p;
5898 finish_do_body (statement);
5899 /* Look for the `while' keyword. */
5900 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5901 /* Look for the `('. */
5902 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5903 /* Parse the expression. */
5904 expression = cp_parser_expression (parser);
5905 /* We're done with the do-statement. */
5906 finish_do_stmt (expression, statement);
5907 /* Look for the `)'. */
5908 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5909 /* Look for the `;'. */
5910 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5916 tree condition = NULL_TREE;
5917 tree expression = NULL_TREE;
5919 /* Begin the for-statement. */
5920 statement = begin_for_stmt ();
5921 /* Look for the `('. */
5922 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5923 /* Parse the initialization. */
5924 cp_parser_for_init_statement (parser);
5925 finish_for_init_stmt (statement);
5927 /* If there's a condition, process it. */
5928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5929 condition = cp_parser_condition (parser);
5930 finish_for_cond (condition, statement);
5931 /* Look for the `;'. */
5932 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5934 /* If there's an expression, process it. */
5935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5936 expression = cp_parser_expression (parser);
5937 finish_for_expr (expression, statement);
5938 /* Look for the `)'. */
5939 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5941 /* Parse the body of the for-statement. */
5942 parser->in_iteration_statement_p = true;
5943 cp_parser_already_scoped_statement (parser);
5944 parser->in_iteration_statement_p = in_iteration_statement_p;
5946 /* We're done with the for-statement. */
5947 finish_for_stmt (statement);
5952 cp_parser_error (parser, "expected iteration-statement");
5953 statement = error_mark_node;
5960 /* Parse a for-init-statement.
5963 expression-statement
5964 simple-declaration */
5967 cp_parser_for_init_statement (cp_parser* parser)
5969 /* If the next token is a `;', then we have an empty
5970 expression-statement. Grammatically, this is also a
5971 simple-declaration, but an invalid one, because it does not
5972 declare anything. Therefore, if we did not handle this case
5973 specially, we would issue an error message about an invalid
5975 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5977 /* We're going to speculatively look for a declaration, falling back
5978 to an expression, if necessary. */
5979 cp_parser_parse_tentatively (parser);
5980 /* Parse the declaration. */
5981 cp_parser_simple_declaration (parser,
5982 /*function_definition_allowed_p=*/false);
5983 /* If the tentative parse failed, then we shall need to look for an
5984 expression-statement. */
5985 if (cp_parser_parse_definitely (parser))
5989 cp_parser_expression_statement (parser, false);
5992 /* Parse a jump-statement.
5997 return expression [opt] ;
6005 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6009 cp_parser_jump_statement (cp_parser* parser)
6011 tree statement = error_mark_node;
6015 /* Peek at the next token. */
6016 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6018 return error_mark_node;
6020 /* See what kind of keyword it is. */
6021 keyword = token->keyword;
6025 if (!parser->in_switch_statement_p
6026 && !parser->in_iteration_statement_p)
6028 error ("break statement not within loop or switch");
6029 statement = error_mark_node;
6032 statement = finish_break_stmt ();
6033 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6037 if (!parser->in_iteration_statement_p)
6039 error ("continue statement not within a loop");
6040 statement = error_mark_node;
6043 statement = finish_continue_stmt ();
6044 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6051 /* If the next token is a `;', then there is no
6053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6054 expr = cp_parser_expression (parser);
6057 /* Build the return-statement. */
6058 statement = finish_return_stmt (expr);
6059 /* Look for the final `;'. */
6060 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6065 /* Create the goto-statement. */
6066 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6068 /* Issue a warning about this use of a GNU extension. */
6070 pedwarn ("ISO C++ forbids computed gotos");
6071 /* Consume the '*' token. */
6072 cp_lexer_consume_token (parser->lexer);
6073 /* Parse the dependent expression. */
6074 finish_goto_stmt (cp_parser_expression (parser));
6077 finish_goto_stmt (cp_parser_identifier (parser));
6078 /* Look for the final `;'. */
6079 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6083 cp_parser_error (parser, "expected jump-statement");
6090 /* Parse a declaration-statement.
6092 declaration-statement:
6093 block-declaration */
6096 cp_parser_declaration_statement (cp_parser* parser)
6098 /* Parse the block-declaration. */
6099 cp_parser_block_declaration (parser, /*statement_p=*/true);
6101 /* Finish off the statement. */
6105 /* Some dependent statements (like `if (cond) statement'), are
6106 implicitly in their own scope. In other words, if the statement is
6107 a single statement (as opposed to a compound-statement), it is
6108 none-the-less treated as if it were enclosed in braces. Any
6109 declarations appearing in the dependent statement are out of scope
6110 after control passes that point. This function parses a statement,
6111 but ensures that is in its own scope, even if it is not a
6114 Returns the new statement. */
6117 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6121 /* If the token is not a `{', then we must take special action. */
6122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6124 /* Create a compound-statement. */
6125 statement = begin_compound_stmt (/*has_no_scope=*/false);
6126 /* Parse the dependent-statement. */
6127 cp_parser_statement (parser, false);
6128 /* Finish the dummy compound-statement. */
6129 finish_compound_stmt (statement);
6131 /* Otherwise, we simply parse the statement directly. */
6133 statement = cp_parser_compound_statement (parser, false);
6135 /* Return the statement. */
6139 /* For some dependent statements (like `while (cond) statement'), we
6140 have already created a scope. Therefore, even if the dependent
6141 statement is a compound-statement, we do not want to create another
6145 cp_parser_already_scoped_statement (cp_parser* parser)
6147 /* If the token is not a `{', then we must take special action. */
6148 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6152 /* Create a compound-statement. */
6153 statement = begin_compound_stmt (/*has_no_scope=*/true);
6154 /* Parse the dependent-statement. */
6155 cp_parser_statement (parser, false);
6156 /* Finish the dummy compound-statement. */
6157 finish_compound_stmt (statement);
6159 /* Otherwise, we simply parse the statement directly. */
6161 cp_parser_statement (parser, false);
6164 /* Declarations [gram.dcl.dcl] */
6166 /* Parse an optional declaration-sequence.
6170 declaration-seq declaration */
6173 cp_parser_declaration_seq_opt (cp_parser* parser)
6179 token = cp_lexer_peek_token (parser->lexer);
6181 if (token->type == CPP_CLOSE_BRACE
6182 || token->type == CPP_EOF)
6185 if (token->type == CPP_SEMICOLON)
6187 /* A declaration consisting of a single semicolon is
6188 invalid. Allow it unless we're being pedantic. */
6189 if (pedantic && !in_system_header)
6190 pedwarn ("extra `;'");
6191 cp_lexer_consume_token (parser->lexer);
6195 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6196 parser to enter or exit implicit `extern "C"' blocks. */
6197 while (pending_lang_change > 0)
6199 push_lang_context (lang_name_c);
6200 --pending_lang_change;
6202 while (pending_lang_change < 0)
6204 pop_lang_context ();
6205 ++pending_lang_change;
6208 /* Parse the declaration itself. */
6209 cp_parser_declaration (parser);
6213 /* Parse a declaration.
6218 template-declaration
6219 explicit-instantiation
6220 explicit-specialization
6221 linkage-specification
6222 namespace-definition
6227 __extension__ declaration */
6230 cp_parser_declaration (cp_parser* parser)
6236 /* Check for the `__extension__' keyword. */
6237 if (cp_parser_extension_opt (parser, &saved_pedantic))
6239 /* Parse the qualified declaration. */
6240 cp_parser_declaration (parser);
6241 /* Restore the PEDANTIC flag. */
6242 pedantic = saved_pedantic;
6247 /* Try to figure out what kind of declaration is present. */
6248 token1 = *cp_lexer_peek_token (parser->lexer);
6249 if (token1.type != CPP_EOF)
6250 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6252 /* If the next token is `extern' and the following token is a string
6253 literal, then we have a linkage specification. */
6254 if (token1.keyword == RID_EXTERN
6255 && cp_parser_is_string_literal (&token2))
6256 cp_parser_linkage_specification (parser);
6257 /* If the next token is `template', then we have either a template
6258 declaration, an explicit instantiation, or an explicit
6260 else if (token1.keyword == RID_TEMPLATE)
6262 /* `template <>' indicates a template specialization. */
6263 if (token2.type == CPP_LESS
6264 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6265 cp_parser_explicit_specialization (parser);
6266 /* `template <' indicates a template declaration. */
6267 else if (token2.type == CPP_LESS)
6268 cp_parser_template_declaration (parser, /*member_p=*/false);
6269 /* Anything else must be an explicit instantiation. */
6271 cp_parser_explicit_instantiation (parser);
6273 /* If the next token is `export', then we have a template
6275 else if (token1.keyword == RID_EXPORT)
6276 cp_parser_template_declaration (parser, /*member_p=*/false);
6277 /* If the next token is `extern', 'static' or 'inline' and the one
6278 after that is `template', we have a GNU extended explicit
6279 instantiation directive. */
6280 else if (cp_parser_allow_gnu_extensions_p (parser)
6281 && (token1.keyword == RID_EXTERN
6282 || token1.keyword == RID_STATIC
6283 || token1.keyword == RID_INLINE)
6284 && token2.keyword == RID_TEMPLATE)
6285 cp_parser_explicit_instantiation (parser);
6286 /* If the next token is `namespace', check for a named or unnamed
6287 namespace definition. */
6288 else if (token1.keyword == RID_NAMESPACE
6289 && (/* A named namespace definition. */
6290 (token2.type == CPP_NAME
6291 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6293 /* An unnamed namespace definition. */
6294 || token2.type == CPP_OPEN_BRACE))
6295 cp_parser_namespace_definition (parser);
6296 /* We must have either a block declaration or a function
6299 /* Try to parse a block-declaration, or a function-definition. */
6300 cp_parser_block_declaration (parser, /*statement_p=*/false);
6303 /* Parse a block-declaration.
6308 namespace-alias-definition
6315 __extension__ block-declaration
6318 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6319 part of a declaration-statement. */
6322 cp_parser_block_declaration (cp_parser *parser,
6328 /* Check for the `__extension__' keyword. */
6329 if (cp_parser_extension_opt (parser, &saved_pedantic))
6331 /* Parse the qualified declaration. */
6332 cp_parser_block_declaration (parser, statement_p);
6333 /* Restore the PEDANTIC flag. */
6334 pedantic = saved_pedantic;
6339 /* Peek at the next token to figure out which kind of declaration is
6341 token1 = cp_lexer_peek_token (parser->lexer);
6343 /* If the next keyword is `asm', we have an asm-definition. */
6344 if (token1->keyword == RID_ASM)
6347 cp_parser_commit_to_tentative_parse (parser);
6348 cp_parser_asm_definition (parser);
6350 /* If the next keyword is `namespace', we have a
6351 namespace-alias-definition. */
6352 else if (token1->keyword == RID_NAMESPACE)
6353 cp_parser_namespace_alias_definition (parser);
6354 /* If the next keyword is `using', we have either a
6355 using-declaration or a using-directive. */
6356 else if (token1->keyword == RID_USING)
6361 cp_parser_commit_to_tentative_parse (parser);
6362 /* If the token after `using' is `namespace', then we have a
6364 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6365 if (token2->keyword == RID_NAMESPACE)
6366 cp_parser_using_directive (parser);
6367 /* Otherwise, it's a using-declaration. */
6369 cp_parser_using_declaration (parser);
6371 /* If the next keyword is `__label__' we have a label declaration. */
6372 else if (token1->keyword == RID_LABEL)
6375 cp_parser_commit_to_tentative_parse (parser);
6376 cp_parser_label_declaration (parser);
6378 /* Anything else must be a simple-declaration. */
6380 cp_parser_simple_declaration (parser, !statement_p);
6383 /* Parse a simple-declaration.
6386 decl-specifier-seq [opt] init-declarator-list [opt] ;
6388 init-declarator-list:
6390 init-declarator-list , init-declarator
6392 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6393 function-definition as a simple-declaration. */
6396 cp_parser_simple_declaration (cp_parser* parser,
6397 bool function_definition_allowed_p)
6399 tree decl_specifiers;
6401 int declares_class_or_enum;
6402 bool saw_declarator;
6404 /* Defer access checks until we know what is being declared; the
6405 checks for names appearing in the decl-specifier-seq should be
6406 done as if we were in the scope of the thing being declared. */
6407 push_deferring_access_checks (dk_deferred);
6409 /* Parse the decl-specifier-seq. We have to keep track of whether
6410 or not the decl-specifier-seq declares a named class or
6411 enumeration type, since that is the only case in which the
6412 init-declarator-list is allowed to be empty.
6416 In a simple-declaration, the optional init-declarator-list can be
6417 omitted only when declaring a class or enumeration, that is when
6418 the decl-specifier-seq contains either a class-specifier, an
6419 elaborated-type-specifier, or an enum-specifier. */
6421 = cp_parser_decl_specifier_seq (parser,
6422 CP_PARSER_FLAGS_OPTIONAL,
6424 &declares_class_or_enum);
6425 /* We no longer need to defer access checks. */
6426 stop_deferring_access_checks ();
6428 /* In a block scope, a valid declaration must always have a
6429 decl-specifier-seq. By not trying to parse declarators, we can
6430 resolve the declaration/expression ambiguity more quickly. */
6431 if (!function_definition_allowed_p && !decl_specifiers)
6433 cp_parser_error (parser, "expected declaration");
6437 /* If the next two tokens are both identifiers, the code is
6438 erroneous. The usual cause of this situation is code like:
6442 where "T" should name a type -- but does not. */
6443 if (cp_parser_diagnose_invalid_type_name (parser))
6445 /* If parsing tentatively, we should commit; we really are
6446 looking at a declaration. */
6447 cp_parser_commit_to_tentative_parse (parser);
6452 /* Keep going until we hit the `;' at the end of the simple
6454 saw_declarator = false;
6455 while (cp_lexer_next_token_is_not (parser->lexer,
6459 bool function_definition_p;
6462 saw_declarator = true;
6463 /* Parse the init-declarator. */
6464 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6465 function_definition_allowed_p,
6467 declares_class_or_enum,
6468 &function_definition_p);
6469 /* If an error occurred while parsing tentatively, exit quickly.
6470 (That usually happens when in the body of a function; each
6471 statement is treated as a declaration-statement until proven
6473 if (cp_parser_error_occurred (parser))
6475 /* Handle function definitions specially. */
6476 if (function_definition_p)
6478 /* If the next token is a `,', then we are probably
6479 processing something like:
6483 which is erroneous. */
6484 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6485 error ("mixing declarations and function-definitions is forbidden");
6486 /* Otherwise, we're done with the list of declarators. */
6489 pop_deferring_access_checks ();
6493 /* The next token should be either a `,' or a `;'. */
6494 token = cp_lexer_peek_token (parser->lexer);
6495 /* If it's a `,', there are more declarators to come. */
6496 if (token->type == CPP_COMMA)
6497 cp_lexer_consume_token (parser->lexer);
6498 /* If it's a `;', we are done. */
6499 else if (token->type == CPP_SEMICOLON)
6501 /* Anything else is an error. */
6504 cp_parser_error (parser, "expected `,' or `;'");
6505 /* Skip tokens until we reach the end of the statement. */
6506 cp_parser_skip_to_end_of_statement (parser);
6507 /* If the next token is now a `;', consume it. */
6508 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6509 cp_lexer_consume_token (parser->lexer);
6512 /* After the first time around, a function-definition is not
6513 allowed -- even if it was OK at first. For example:
6518 function_definition_allowed_p = false;
6521 /* Issue an error message if no declarators are present, and the
6522 decl-specifier-seq does not itself declare a class or
6524 if (!saw_declarator)
6526 if (cp_parser_declares_only_class_p (parser))
6527 shadow_tag (decl_specifiers);
6528 /* Perform any deferred access checks. */
6529 perform_deferred_access_checks ();
6532 /* Consume the `;'. */
6533 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6536 pop_deferring_access_checks ();
6539 /* Parse a decl-specifier-seq.
6542 decl-specifier-seq [opt] decl-specifier
6545 storage-class-specifier
6554 decl-specifier-seq [opt] attributes
6556 Returns a TREE_LIST, giving the decl-specifiers in the order they
6557 appear in the source code. The TREE_VALUE of each node is the
6558 decl-specifier. For a keyword (such as `auto' or `friend'), the
6559 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6560 representation of a type-specifier, see cp_parser_type_specifier.
6562 If there are attributes, they will be stored in *ATTRIBUTES,
6563 represented as described above cp_parser_attributes.
6565 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6566 appears, and the entity that will be a friend is not going to be a
6567 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6568 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6569 friendship is granted might not be a class.
6571 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6574 1: one of the decl-specifiers is an elaborated-type-specifier
6575 (i.e., a type declaration)
6576 2: one of the decl-specifiers is an enum-specifier or a
6577 class-specifier (i.e., a type definition)
6582 cp_parser_decl_specifier_seq (cp_parser* parser,
6583 cp_parser_flags flags,
6585 int* declares_class_or_enum)
6587 tree decl_specs = NULL_TREE;
6588 bool friend_p = false;
6589 bool constructor_possible_p = !parser->in_declarator_p;
6591 /* Assume no class or enumeration type is declared. */
6592 *declares_class_or_enum = 0;
6594 /* Assume there are no attributes. */
6595 *attributes = NULL_TREE;
6597 /* Keep reading specifiers until there are no more to read. */
6600 tree decl_spec = NULL_TREE;
6604 /* Peek at the next token. */
6605 token = cp_lexer_peek_token (parser->lexer);
6606 /* Handle attributes. */
6607 if (token->keyword == RID_ATTRIBUTE)
6609 /* Parse the attributes. */
6610 decl_spec = cp_parser_attributes_opt (parser);
6611 /* Add them to the list. */
6612 *attributes = chainon (*attributes, decl_spec);
6615 /* If the next token is an appropriate keyword, we can simply
6616 add it to the list. */
6617 switch (token->keyword)
6623 error ("duplicate `friend'");
6626 /* The representation of the specifier is simply the
6627 appropriate TREE_IDENTIFIER node. */
6628 decl_spec = token->value;
6629 /* Consume the token. */
6630 cp_lexer_consume_token (parser->lexer);
6633 /* function-specifier:
6640 decl_spec = cp_parser_function_specifier_opt (parser);
6646 /* The representation of the specifier is simply the
6647 appropriate TREE_IDENTIFIER node. */
6648 decl_spec = token->value;
6649 /* Consume the token. */
6650 cp_lexer_consume_token (parser->lexer);
6651 /* A constructor declarator cannot appear in a typedef. */
6652 constructor_possible_p = false;
6653 /* The "typedef" keyword can only occur in a declaration; we
6654 may as well commit at this point. */
6655 cp_parser_commit_to_tentative_parse (parser);
6658 /* storage-class-specifier:
6673 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6680 /* Constructors are a special case. The `S' in `S()' is not a
6681 decl-specifier; it is the beginning of the declarator. */
6682 constructor_p = (!decl_spec
6683 && constructor_possible_p
6684 && cp_parser_constructor_declarator_p (parser,
6687 /* If we don't have a DECL_SPEC yet, then we must be looking at
6688 a type-specifier. */
6689 if (!decl_spec && !constructor_p)
6691 int decl_spec_declares_class_or_enum;
6692 bool is_cv_qualifier;
6695 = cp_parser_type_specifier (parser, flags,
6697 /*is_declaration=*/true,
6698 &decl_spec_declares_class_or_enum,
6701 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6703 /* If this type-specifier referenced a user-defined type
6704 (a typedef, class-name, etc.), then we can't allow any
6705 more such type-specifiers henceforth.
6709 The longest sequence of decl-specifiers that could
6710 possibly be a type name is taken as the
6711 decl-specifier-seq of a declaration. The sequence shall
6712 be self-consistent as described below.
6716 As a general rule, at most one type-specifier is allowed
6717 in the complete decl-specifier-seq of a declaration. The
6718 only exceptions are the following:
6720 -- const or volatile can be combined with any other
6723 -- signed or unsigned can be combined with char, long,
6731 void g (const int Pc);
6733 Here, Pc is *not* part of the decl-specifier seq; it's
6734 the declarator. Therefore, once we see a type-specifier
6735 (other than a cv-qualifier), we forbid any additional
6736 user-defined types. We *do* still allow things like `int
6737 int' to be considered a decl-specifier-seq, and issue the
6738 error message later. */
6739 if (decl_spec && !is_cv_qualifier)
6740 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6741 /* A constructor declarator cannot follow a type-specifier. */
6743 constructor_possible_p = false;
6746 /* If we still do not have a DECL_SPEC, then there are no more
6750 /* Issue an error message, unless the entire construct was
6752 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6754 cp_parser_error (parser, "expected decl specifier");
6755 return error_mark_node;
6761 /* Add the DECL_SPEC to the list of specifiers. */
6762 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6763 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6765 /* After we see one decl-specifier, further decl-specifiers are
6767 flags |= CP_PARSER_FLAGS_OPTIONAL;
6770 /* Don't allow a friend specifier with a class definition. */
6771 if (friend_p && (*declares_class_or_enum & 2))
6772 error ("class definition may not be declared a friend");
6774 /* We have built up the DECL_SPECS in reverse order. Return them in
6775 the correct order. */
6776 return nreverse (decl_specs);
6779 /* Parse an (optional) storage-class-specifier.
6781 storage-class-specifier:
6790 storage-class-specifier:
6793 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6796 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6798 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6806 /* Consume the token. */
6807 return cp_lexer_consume_token (parser->lexer)->value;
6814 /* Parse an (optional) function-specifier.
6821 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6824 cp_parser_function_specifier_opt (cp_parser* parser)
6826 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6831 /* Consume the token. */
6832 return cp_lexer_consume_token (parser->lexer)->value;
6839 /* Parse a linkage-specification.
6841 linkage-specification:
6842 extern string-literal { declaration-seq [opt] }
6843 extern string-literal declaration */
6846 cp_parser_linkage_specification (cp_parser* parser)
6851 /* Look for the `extern' keyword. */
6852 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6854 /* Peek at the next token. */
6855 token = cp_lexer_peek_token (parser->lexer);
6856 /* If it's not a string-literal, then there's a problem. */
6857 if (!cp_parser_is_string_literal (token))
6859 cp_parser_error (parser, "expected language-name");
6862 /* Consume the token. */
6863 cp_lexer_consume_token (parser->lexer);
6865 /* Transform the literal into an identifier. If the literal is a
6866 wide-character string, or contains embedded NULs, then we can't
6867 handle it as the user wants. */
6868 if (token->type == CPP_WSTRING
6869 || (strlen (TREE_STRING_POINTER (token->value))
6870 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6872 cp_parser_error (parser, "invalid linkage-specification");
6873 /* Assume C++ linkage. */
6874 linkage = get_identifier ("c++");
6876 /* If it's a simple string constant, things are easier. */
6878 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6880 /* We're now using the new linkage. */
6881 push_lang_context (linkage);
6883 /* If the next token is a `{', then we're using the first
6885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6887 /* Consume the `{' token. */
6888 cp_lexer_consume_token (parser->lexer);
6889 /* Parse the declarations. */
6890 cp_parser_declaration_seq_opt (parser);
6891 /* Look for the closing `}'. */
6892 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6894 /* Otherwise, there's just one declaration. */
6897 bool saved_in_unbraced_linkage_specification_p;
6899 saved_in_unbraced_linkage_specification_p
6900 = parser->in_unbraced_linkage_specification_p;
6901 parser->in_unbraced_linkage_specification_p = true;
6902 have_extern_spec = true;
6903 cp_parser_declaration (parser);
6904 have_extern_spec = false;
6905 parser->in_unbraced_linkage_specification_p
6906 = saved_in_unbraced_linkage_specification_p;
6909 /* We're done with the linkage-specification. */
6910 pop_lang_context ();
6913 /* Special member functions [gram.special] */
6915 /* Parse a conversion-function-id.
6917 conversion-function-id:
6918 operator conversion-type-id
6920 Returns an IDENTIFIER_NODE representing the operator. */
6923 cp_parser_conversion_function_id (cp_parser* parser)
6927 tree saved_qualifying_scope;
6928 tree saved_object_scope;
6930 /* Look for the `operator' token. */
6931 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6932 return error_mark_node;
6933 /* When we parse the conversion-type-id, the current scope will be
6934 reset. However, we need that information in able to look up the
6935 conversion function later, so we save it here. */
6936 saved_scope = parser->scope;
6937 saved_qualifying_scope = parser->qualifying_scope;
6938 saved_object_scope = parser->object_scope;
6939 /* We must enter the scope of the class so that the names of
6940 entities declared within the class are available in the
6941 conversion-type-id. For example, consider:
6948 S::operator I() { ... }
6950 In order to see that `I' is a type-name in the definition, we
6951 must be in the scope of `S'. */
6953 push_scope (saved_scope);
6954 /* Parse the conversion-type-id. */
6955 type = cp_parser_conversion_type_id (parser);
6956 /* Leave the scope of the class, if any. */
6958 pop_scope (saved_scope);
6959 /* Restore the saved scope. */
6960 parser->scope = saved_scope;
6961 parser->qualifying_scope = saved_qualifying_scope;
6962 parser->object_scope = saved_object_scope;
6963 /* If the TYPE is invalid, indicate failure. */
6964 if (type == error_mark_node)
6965 return error_mark_node;
6966 return mangle_conv_op_name_for_type (type);
6969 /* Parse a conversion-type-id:
6972 type-specifier-seq conversion-declarator [opt]
6974 Returns the TYPE specified. */
6977 cp_parser_conversion_type_id (cp_parser* parser)
6980 tree type_specifiers;
6983 /* Parse the attributes. */
6984 attributes = cp_parser_attributes_opt (parser);
6985 /* Parse the type-specifiers. */
6986 type_specifiers = cp_parser_type_specifier_seq (parser);
6987 /* If that didn't work, stop. */
6988 if (type_specifiers == error_mark_node)
6989 return error_mark_node;
6990 /* Parse the conversion-declarator. */
6991 declarator = cp_parser_conversion_declarator_opt (parser);
6993 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6994 /*initialized=*/0, &attributes);
6997 /* Parse an (optional) conversion-declarator.
6999 conversion-declarator:
7000 ptr-operator conversion-declarator [opt]
7002 Returns a representation of the declarator. See
7003 cp_parser_declarator for details. */
7006 cp_parser_conversion_declarator_opt (cp_parser* parser)
7008 enum tree_code code;
7010 tree cv_qualifier_seq;
7012 /* We don't know if there's a ptr-operator next, or not. */
7013 cp_parser_parse_tentatively (parser);
7014 /* Try the ptr-operator. */
7015 code = cp_parser_ptr_operator (parser, &class_type,
7017 /* If it worked, look for more conversion-declarators. */
7018 if (cp_parser_parse_definitely (parser))
7022 /* Parse another optional declarator. */
7023 declarator = cp_parser_conversion_declarator_opt (parser);
7025 /* Create the representation of the declarator. */
7026 if (code == INDIRECT_REF)
7027 declarator = make_pointer_declarator (cv_qualifier_seq,
7030 declarator = make_reference_declarator (cv_qualifier_seq,
7033 /* Handle the pointer-to-member case. */
7035 declarator = build_nt (SCOPE_REF, class_type, declarator);
7043 /* Parse an (optional) ctor-initializer.
7046 : mem-initializer-list
7048 Returns TRUE iff the ctor-initializer was actually present. */
7051 cp_parser_ctor_initializer_opt (cp_parser* parser)
7053 /* If the next token is not a `:', then there is no
7054 ctor-initializer. */
7055 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7057 /* Do default initialization of any bases and members. */
7058 if (DECL_CONSTRUCTOR_P (current_function_decl))
7059 finish_mem_initializers (NULL_TREE);
7064 /* Consume the `:' token. */
7065 cp_lexer_consume_token (parser->lexer);
7066 /* And the mem-initializer-list. */
7067 cp_parser_mem_initializer_list (parser);
7072 /* Parse a mem-initializer-list.
7074 mem-initializer-list:
7076 mem-initializer , mem-initializer-list */
7079 cp_parser_mem_initializer_list (cp_parser* parser)
7081 tree mem_initializer_list = NULL_TREE;
7083 /* Let the semantic analysis code know that we are starting the
7084 mem-initializer-list. */
7085 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7086 error ("only constructors take base initializers");
7088 /* Loop through the list. */
7091 tree mem_initializer;
7093 /* Parse the mem-initializer. */
7094 mem_initializer = cp_parser_mem_initializer (parser);
7095 /* Add it to the list, unless it was erroneous. */
7096 if (mem_initializer)
7098 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7099 mem_initializer_list = mem_initializer;
7101 /* If the next token is not a `,', we're done. */
7102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7104 /* Consume the `,' token. */
7105 cp_lexer_consume_token (parser->lexer);
7108 /* Perform semantic analysis. */
7109 if (DECL_CONSTRUCTOR_P (current_function_decl))
7110 finish_mem_initializers (mem_initializer_list);
7113 /* Parse a mem-initializer.
7116 mem-initializer-id ( expression-list [opt] )
7121 ( expression-list [opt] )
7123 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7124 class) or FIELD_DECL (for a non-static data member) to initialize;
7125 the TREE_VALUE is the expression-list. */
7128 cp_parser_mem_initializer (cp_parser* parser)
7130 tree mem_initializer_id;
7131 tree expression_list;
7134 /* Find out what is being initialized. */
7135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7137 pedwarn ("anachronistic old-style base class initializer");
7138 mem_initializer_id = NULL_TREE;
7141 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7142 member = expand_member_init (mem_initializer_id);
7143 if (member && !DECL_P (member))
7144 in_base_initializer = 1;
7147 = cp_parser_parenthesized_expression_list (parser, false,
7148 /*non_constant_p=*/NULL);
7149 if (!expression_list)
7150 expression_list = void_type_node;
7152 in_base_initializer = 0;
7154 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7157 /* Parse a mem-initializer-id.
7160 :: [opt] nested-name-specifier [opt] class-name
7163 Returns a TYPE indicating the class to be initializer for the first
7164 production. Returns an IDENTIFIER_NODE indicating the data member
7165 to be initialized for the second production. */
7168 cp_parser_mem_initializer_id (cp_parser* parser)
7170 bool global_scope_p;
7171 bool nested_name_specifier_p;
7174 /* Look for the optional `::' operator. */
7176 = (cp_parser_global_scope_opt (parser,
7177 /*current_scope_valid_p=*/false)
7179 /* Look for the optional nested-name-specifier. The simplest way to
7184 The keyword `typename' is not permitted in a base-specifier or
7185 mem-initializer; in these contexts a qualified name that
7186 depends on a template-parameter is implicitly assumed to be a
7189 is to assume that we have seen the `typename' keyword at this
7191 nested_name_specifier_p
7192 = (cp_parser_nested_name_specifier_opt (parser,
7193 /*typename_keyword_p=*/true,
7194 /*check_dependency_p=*/true,
7196 /*is_declaration=*/true)
7198 /* If there is a `::' operator or a nested-name-specifier, then we
7199 are definitely looking for a class-name. */
7200 if (global_scope_p || nested_name_specifier_p)
7201 return cp_parser_class_name (parser,
7202 /*typename_keyword_p=*/true,
7203 /*template_keyword_p=*/false,
7205 /*check_dependency_p=*/true,
7206 /*class_head_p=*/false,
7207 /*is_declaration=*/true);
7208 /* Otherwise, we could also be looking for an ordinary identifier. */
7209 cp_parser_parse_tentatively (parser);
7210 /* Try a class-name. */
7211 id = cp_parser_class_name (parser,
7212 /*typename_keyword_p=*/true,
7213 /*template_keyword_p=*/false,
7215 /*check_dependency_p=*/true,
7216 /*class_head_p=*/false,
7217 /*is_declaration=*/true);
7218 /* If we found one, we're done. */
7219 if (cp_parser_parse_definitely (parser))
7221 /* Otherwise, look for an ordinary identifier. */
7222 return cp_parser_identifier (parser);
7225 /* Overloading [gram.over] */
7227 /* Parse an operator-function-id.
7229 operator-function-id:
7232 Returns an IDENTIFIER_NODE for the operator which is a
7233 human-readable spelling of the identifier, e.g., `operator +'. */
7236 cp_parser_operator_function_id (cp_parser* parser)
7238 /* Look for the `operator' keyword. */
7239 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7240 return error_mark_node;
7241 /* And then the name of the operator itself. */
7242 return cp_parser_operator (parser);
7245 /* Parse an operator.
7248 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7249 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7250 || ++ -- , ->* -> () []
7257 Returns an IDENTIFIER_NODE for the operator which is a
7258 human-readable spelling of the identifier, e.g., `operator +'. */
7261 cp_parser_operator (cp_parser* parser)
7263 tree id = NULL_TREE;
7266 /* Peek at the next token. */
7267 token = cp_lexer_peek_token (parser->lexer);
7268 /* Figure out which operator we have. */
7269 switch (token->type)
7275 /* The keyword should be either `new' or `delete'. */
7276 if (token->keyword == RID_NEW)
7278 else if (token->keyword == RID_DELETE)
7283 /* Consume the `new' or `delete' token. */
7284 cp_lexer_consume_token (parser->lexer);
7286 /* Peek at the next token. */
7287 token = cp_lexer_peek_token (parser->lexer);
7288 /* If it's a `[' token then this is the array variant of the
7290 if (token->type == CPP_OPEN_SQUARE)
7292 /* Consume the `[' token. */
7293 cp_lexer_consume_token (parser->lexer);
7294 /* Look for the `]' token. */
7295 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7296 id = ansi_opname (op == NEW_EXPR
7297 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7299 /* Otherwise, we have the non-array variant. */
7301 id = ansi_opname (op);
7307 id = ansi_opname (PLUS_EXPR);
7311 id = ansi_opname (MINUS_EXPR);
7315 id = ansi_opname (MULT_EXPR);
7319 id = ansi_opname (TRUNC_DIV_EXPR);
7323 id = ansi_opname (TRUNC_MOD_EXPR);
7327 id = ansi_opname (BIT_XOR_EXPR);
7331 id = ansi_opname (BIT_AND_EXPR);
7335 id = ansi_opname (BIT_IOR_EXPR);
7339 id = ansi_opname (BIT_NOT_EXPR);
7343 id = ansi_opname (TRUTH_NOT_EXPR);
7347 id = ansi_assopname (NOP_EXPR);
7351 id = ansi_opname (LT_EXPR);
7355 id = ansi_opname (GT_EXPR);
7359 id = ansi_assopname (PLUS_EXPR);
7363 id = ansi_assopname (MINUS_EXPR);
7367 id = ansi_assopname (MULT_EXPR);
7371 id = ansi_assopname (TRUNC_DIV_EXPR);
7375 id = ansi_assopname (TRUNC_MOD_EXPR);
7379 id = ansi_assopname (BIT_XOR_EXPR);
7383 id = ansi_assopname (BIT_AND_EXPR);
7387 id = ansi_assopname (BIT_IOR_EXPR);
7391 id = ansi_opname (LSHIFT_EXPR);
7395 id = ansi_opname (RSHIFT_EXPR);
7399 id = ansi_assopname (LSHIFT_EXPR);
7403 id = ansi_assopname (RSHIFT_EXPR);
7407 id = ansi_opname (EQ_EXPR);
7411 id = ansi_opname (NE_EXPR);
7415 id = ansi_opname (LE_EXPR);
7418 case CPP_GREATER_EQ:
7419 id = ansi_opname (GE_EXPR);
7423 id = ansi_opname (TRUTH_ANDIF_EXPR);
7427 id = ansi_opname (TRUTH_ORIF_EXPR);
7431 id = ansi_opname (POSTINCREMENT_EXPR);
7434 case CPP_MINUS_MINUS:
7435 id = ansi_opname (PREDECREMENT_EXPR);
7439 id = ansi_opname (COMPOUND_EXPR);
7442 case CPP_DEREF_STAR:
7443 id = ansi_opname (MEMBER_REF);
7447 id = ansi_opname (COMPONENT_REF);
7450 case CPP_OPEN_PAREN:
7451 /* Consume the `('. */
7452 cp_lexer_consume_token (parser->lexer);
7453 /* Look for the matching `)'. */
7454 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7455 return ansi_opname (CALL_EXPR);
7457 case CPP_OPEN_SQUARE:
7458 /* Consume the `['. */
7459 cp_lexer_consume_token (parser->lexer);
7460 /* Look for the matching `]'. */
7461 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7462 return ansi_opname (ARRAY_REF);
7466 id = ansi_opname (MIN_EXPR);
7470 id = ansi_opname (MAX_EXPR);
7474 id = ansi_assopname (MIN_EXPR);
7478 id = ansi_assopname (MAX_EXPR);
7482 /* Anything else is an error. */
7486 /* If we have selected an identifier, we need to consume the
7489 cp_lexer_consume_token (parser->lexer);
7490 /* Otherwise, no valid operator name was present. */
7493 cp_parser_error (parser, "expected operator");
7494 id = error_mark_node;
7500 /* Parse a template-declaration.
7502 template-declaration:
7503 export [opt] template < template-parameter-list > declaration
7505 If MEMBER_P is TRUE, this template-declaration occurs within a
7508 The grammar rule given by the standard isn't correct. What
7511 template-declaration:
7512 export [opt] template-parameter-list-seq
7513 decl-specifier-seq [opt] init-declarator [opt] ;
7514 export [opt] template-parameter-list-seq
7517 template-parameter-list-seq:
7518 template-parameter-list-seq [opt]
7519 template < template-parameter-list > */
7522 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7524 /* Check for `export'. */
7525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7527 /* Consume the `export' token. */
7528 cp_lexer_consume_token (parser->lexer);
7529 /* Warn that we do not support `export'. */
7530 warning ("keyword `export' not implemented, and will be ignored");
7533 cp_parser_template_declaration_after_export (parser, member_p);
7536 /* Parse a template-parameter-list.
7538 template-parameter-list:
7540 template-parameter-list , template-parameter
7542 Returns a TREE_LIST. Each node represents a template parameter.
7543 The nodes are connected via their TREE_CHAINs. */
7546 cp_parser_template_parameter_list (cp_parser* parser)
7548 tree parameter_list = NULL_TREE;
7555 /* Parse the template-parameter. */
7556 parameter = cp_parser_template_parameter (parser);
7557 /* Add it to the list. */
7558 parameter_list = process_template_parm (parameter_list,
7561 /* Peek at the next token. */
7562 token = cp_lexer_peek_token (parser->lexer);
7563 /* If it's not a `,', we're done. */
7564 if (token->type != CPP_COMMA)
7566 /* Otherwise, consume the `,' token. */
7567 cp_lexer_consume_token (parser->lexer);
7570 return parameter_list;
7573 /* Parse a template-parameter.
7577 parameter-declaration
7579 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7580 TREE_PURPOSE is the default value, if any. */
7583 cp_parser_template_parameter (cp_parser* parser)
7587 /* Peek at the next token. */
7588 token = cp_lexer_peek_token (parser->lexer);
7589 /* If it is `class' or `template', we have a type-parameter. */
7590 if (token->keyword == RID_TEMPLATE)
7591 return cp_parser_type_parameter (parser);
7592 /* If it is `class' or `typename' we do not know yet whether it is a
7593 type parameter or a non-type parameter. Consider:
7595 template <typename T, typename T::X X> ...
7599 template <class C, class D*> ...
7601 Here, the first parameter is a type parameter, and the second is
7602 a non-type parameter. We can tell by looking at the token after
7603 the identifier -- if it is a `,', `=', or `>' then we have a type
7605 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7607 /* Peek at the token after `class' or `typename'. */
7608 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7609 /* If it's an identifier, skip it. */
7610 if (token->type == CPP_NAME)
7611 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7612 /* Now, see if the token looks like the end of a template
7614 if (token->type == CPP_COMMA
7615 || token->type == CPP_EQ
7616 || token->type == CPP_GREATER)
7617 return cp_parser_type_parameter (parser);
7620 /* Otherwise, it is a non-type parameter.
7624 When parsing a default template-argument for a non-type
7625 template-parameter, the first non-nested `>' is taken as the end
7626 of the template parameter-list rather than a greater-than
7629 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7630 /*parenthesized_p=*/NULL);
7633 /* Parse a type-parameter.
7636 class identifier [opt]
7637 class identifier [opt] = type-id
7638 typename identifier [opt]
7639 typename identifier [opt] = type-id
7640 template < template-parameter-list > class identifier [opt]
7641 template < template-parameter-list > class identifier [opt]
7644 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7645 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7646 the declaration of the parameter. */
7649 cp_parser_type_parameter (cp_parser* parser)
7654 /* Look for a keyword to tell us what kind of parameter this is. */
7655 token = cp_parser_require (parser, CPP_KEYWORD,
7656 "`class', `typename', or `template'");
7658 return error_mark_node;
7660 switch (token->keyword)
7666 tree default_argument;
7668 /* If the next token is an identifier, then it names the
7670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7671 identifier = cp_parser_identifier (parser);
7673 identifier = NULL_TREE;
7675 /* Create the parameter. */
7676 parameter = finish_template_type_parm (class_type_node, identifier);
7678 /* If the next token is an `=', we have a default argument. */
7679 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7681 /* Consume the `=' token. */
7682 cp_lexer_consume_token (parser->lexer);
7683 /* Parse the default-argument. */
7684 default_argument = cp_parser_type_id (parser);
7687 default_argument = NULL_TREE;
7689 /* Create the combined representation of the parameter and the
7690 default argument. */
7691 parameter = build_tree_list (default_argument, parameter);
7697 tree parameter_list;
7699 tree default_argument;
7701 /* Look for the `<'. */
7702 cp_parser_require (parser, CPP_LESS, "`<'");
7703 /* Parse the template-parameter-list. */
7704 begin_template_parm_list ();
7706 = cp_parser_template_parameter_list (parser);
7707 parameter_list = end_template_parm_list (parameter_list);
7708 /* Look for the `>'. */
7709 cp_parser_require (parser, CPP_GREATER, "`>'");
7710 /* Look for the `class' keyword. */
7711 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7712 /* If the next token is an `=', then there is a
7713 default-argument. If the next token is a `>', we are at
7714 the end of the parameter-list. If the next token is a `,',
7715 then we are at the end of this parameter. */
7716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7717 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7718 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7719 identifier = cp_parser_identifier (parser);
7721 identifier = NULL_TREE;
7722 /* Create the template parameter. */
7723 parameter = finish_template_template_parm (class_type_node,
7726 /* If the next token is an `=', then there is a
7727 default-argument. */
7728 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7732 /* Consume the `='. */
7733 cp_lexer_consume_token (parser->lexer);
7734 /* Parse the id-expression. */
7736 = cp_parser_id_expression (parser,
7737 /*template_keyword_p=*/false,
7738 /*check_dependency_p=*/true,
7739 /*template_p=*/&is_template,
7740 /*declarator_p=*/false);
7741 if (TREE_CODE (default_argument) == TYPE_DECL)
7742 /* If the id-expression was a template-id that refers to
7743 a template-class, we already have the declaration here,
7744 so no further lookup is needed. */
7747 /* Look up the name. */
7749 = cp_parser_lookup_name (parser, default_argument,
7751 /*is_template=*/is_template,
7752 /*is_namespace=*/false,
7753 /*check_dependency=*/true);
7754 /* See if the default argument is valid. */
7756 = check_template_template_default_arg (default_argument);
7759 default_argument = NULL_TREE;
7761 /* Create the combined representation of the parameter and the
7762 default argument. */
7763 parameter = build_tree_list (default_argument, parameter);
7768 /* Anything else is an error. */
7769 cp_parser_error (parser,
7770 "expected `class', `typename', or `template'");
7771 parameter = error_mark_node;
7777 /* Parse a template-id.
7780 template-name < template-argument-list [opt] >
7782 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7783 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7784 returned. Otherwise, if the template-name names a function, or set
7785 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7786 names a class, returns a TYPE_DECL for the specialization.
7788 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7789 uninstantiated templates. */
7792 cp_parser_template_id (cp_parser *parser,
7793 bool template_keyword_p,
7794 bool check_dependency_p,
7795 bool is_declaration)
7800 ptrdiff_t start_of_id;
7801 tree access_check = NULL_TREE;
7802 cp_token *next_token, *next_token_2;
7805 /* If the next token corresponds to a template-id, there is no need
7807 next_token = cp_lexer_peek_token (parser->lexer);
7808 if (next_token->type == CPP_TEMPLATE_ID)
7813 /* Get the stored value. */
7814 value = cp_lexer_consume_token (parser->lexer)->value;
7815 /* Perform any access checks that were deferred. */
7816 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7817 perform_or_defer_access_check (TREE_PURPOSE (check),
7818 TREE_VALUE (check));
7819 /* Return the stored value. */
7820 return TREE_VALUE (value);
7823 /* Avoid performing name lookup if there is no possibility of
7824 finding a template-id. */
7825 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7826 || (next_token->type == CPP_NAME
7827 && !cp_parser_nth_token_starts_template_argument_list_p
7830 cp_parser_error (parser, "expected template-id");
7831 return error_mark_node;
7834 /* Remember where the template-id starts. */
7835 if (cp_parser_parsing_tentatively (parser)
7836 && !cp_parser_committed_to_tentative_parse (parser))
7838 next_token = cp_lexer_peek_token (parser->lexer);
7839 start_of_id = cp_lexer_token_difference (parser->lexer,
7840 parser->lexer->first_token,
7846 push_deferring_access_checks (dk_deferred);
7848 /* Parse the template-name. */
7849 is_identifier = false;
7850 template = cp_parser_template_name (parser, template_keyword_p,
7854 if (template == error_mark_node || is_identifier)
7856 pop_deferring_access_checks ();
7860 /* If we find the sequence `[:' after a template-name, it's probably
7861 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7862 parse correctly the argument list. */
7863 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7864 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7865 if (next_token->type == CPP_OPEN_SQUARE
7866 && next_token->flags & DIGRAPH
7867 && next_token_2->type == CPP_COLON
7868 && !(next_token_2->flags & PREV_WHITE))
7870 cp_parser_parse_tentatively (parser);
7871 /* Change `:' into `::'. */
7872 next_token_2->type = CPP_SCOPE;
7873 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7875 cp_lexer_consume_token (parser->lexer);
7876 /* Parse the arguments. */
7877 arguments = cp_parser_enclosed_template_argument_list (parser);
7878 if (!cp_parser_parse_definitely (parser))
7880 /* If we couldn't parse an argument list, then we revert our changes
7881 and return simply an error. Maybe this is not a template-id
7883 next_token_2->type = CPP_COLON;
7884 cp_parser_error (parser, "expected `<'");
7885 pop_deferring_access_checks ();
7886 return error_mark_node;
7888 /* Otherwise, emit an error about the invalid digraph, but continue
7889 parsing because we got our argument list. */
7890 pedwarn ("`<::' cannot begin a template-argument list");
7891 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7892 "between `<' and `::'");
7893 if (!flag_permissive)
7898 inform ("(if you use `-fpermissive' G++ will accept your code)");
7905 /* Look for the `<' that starts the template-argument-list. */
7906 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7908 pop_deferring_access_checks ();
7909 return error_mark_node;
7911 /* Parse the arguments. */
7912 arguments = cp_parser_enclosed_template_argument_list (parser);
7915 /* Build a representation of the specialization. */
7916 if (TREE_CODE (template) == IDENTIFIER_NODE)
7917 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7918 else if (DECL_CLASS_TEMPLATE_P (template)
7919 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7921 = finish_template_type (template, arguments,
7922 cp_lexer_next_token_is (parser->lexer,
7926 /* If it's not a class-template or a template-template, it should be
7927 a function-template. */
7928 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7929 || TREE_CODE (template) == OVERLOAD
7930 || BASELINK_P (template)),
7933 template_id = lookup_template_function (template, arguments);
7936 /* Retrieve any deferred checks. Do not pop this access checks yet
7937 so the memory will not be reclaimed during token replacing below. */
7938 access_check = get_deferred_access_checks ();
7940 /* If parsing tentatively, replace the sequence of tokens that makes
7941 up the template-id with a CPP_TEMPLATE_ID token. That way,
7942 should we re-parse the token stream, we will not have to repeat
7943 the effort required to do the parse, nor will we issue duplicate
7944 error messages about problems during instantiation of the
7946 if (start_of_id >= 0)
7950 /* Find the token that corresponds to the start of the
7952 token = cp_lexer_advance_token (parser->lexer,
7953 parser->lexer->first_token,
7956 /* Reset the contents of the START_OF_ID token. */
7957 token->type = CPP_TEMPLATE_ID;
7958 token->value = build_tree_list (access_check, template_id);
7959 token->keyword = RID_MAX;
7960 /* Purge all subsequent tokens. */
7961 cp_lexer_purge_tokens_after (parser->lexer, token);
7964 pop_deferring_access_checks ();
7968 /* Parse a template-name.
7973 The standard should actually say:
7977 operator-function-id
7979 A defect report has been filed about this issue.
7981 A conversion-function-id cannot be a template name because they cannot
7982 be part of a template-id. In fact, looking at this code:
7986 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
7987 It is impossible to call a templated conversion-function-id with an
7988 explicit argument list, since the only allowed template parameter is
7989 the type to which it is converting.
7991 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7992 `template' keyword, in a construction like:
7996 In that case `f' is taken to be a template-name, even though there
7997 is no way of knowing for sure.
7999 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8000 name refers to a set of overloaded functions, at least one of which
8001 is a template, or an IDENTIFIER_NODE with the name of the template,
8002 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8003 names are looked up inside uninstantiated templates. */
8006 cp_parser_template_name (cp_parser* parser,
8007 bool template_keyword_p,
8008 bool check_dependency_p,
8009 bool is_declaration,
8010 bool *is_identifier)
8016 /* If the next token is `operator', then we have either an
8017 operator-function-id or a conversion-function-id. */
8018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8020 /* We don't know whether we're looking at an
8021 operator-function-id or a conversion-function-id. */
8022 cp_parser_parse_tentatively (parser);
8023 /* Try an operator-function-id. */
8024 identifier = cp_parser_operator_function_id (parser);
8025 /* If that didn't work, try a conversion-function-id. */
8026 if (!cp_parser_parse_definitely (parser))
8028 cp_parser_error (parser, "expected template-name");
8029 return error_mark_node;
8032 /* Look for the identifier. */
8034 identifier = cp_parser_identifier (parser);
8036 /* If we didn't find an identifier, we don't have a template-id. */
8037 if (identifier == error_mark_node)
8038 return error_mark_node;
8040 /* If the name immediately followed the `template' keyword, then it
8041 is a template-name. However, if the next token is not `<', then
8042 we do not treat it as a template-name, since it is not being used
8043 as part of a template-id. This enables us to handle constructs
8046 template <typename T> struct S { S(); };
8047 template <typename T> S<T>::S();
8049 correctly. We would treat `S' as a template -- if it were `S<T>'
8050 -- but we do not if there is no `<'. */
8052 if (processing_template_decl
8053 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8055 /* In a declaration, in a dependent context, we pretend that the
8056 "template" keyword was present in order to improve error
8057 recovery. For example, given:
8059 template <typename T> void f(T::X<int>);
8061 we want to treat "X<int>" as a template-id. */
8063 && !template_keyword_p
8064 && parser->scope && TYPE_P (parser->scope)
8065 && dependent_type_p (parser->scope))
8069 /* Explain what went wrong. */
8070 error ("non-template `%D' used as template", identifier);
8071 error ("(use `%T::template %D' to indicate that it is a template)",
8072 parser->scope, identifier);
8073 /* If parsing tentatively, find the location of the "<"
8075 if (cp_parser_parsing_tentatively (parser)
8076 && !cp_parser_committed_to_tentative_parse (parser))
8078 cp_parser_simulate_error (parser);
8079 token = cp_lexer_peek_token (parser->lexer);
8080 token = cp_lexer_prev_token (parser->lexer, token);
8081 start = cp_lexer_token_difference (parser->lexer,
8082 parser->lexer->first_token,
8087 /* Parse the template arguments so that we can issue error
8088 messages about them. */
8089 cp_lexer_consume_token (parser->lexer);
8090 cp_parser_enclosed_template_argument_list (parser);
8091 /* Skip tokens until we find a good place from which to
8092 continue parsing. */
8093 cp_parser_skip_to_closing_parenthesis (parser,
8094 /*recovering=*/true,
8096 /*consume_paren=*/false);
8097 /* If parsing tentatively, permanently remove the
8098 template argument list. That will prevent duplicate
8099 error messages from being issued about the missing
8100 "template" keyword. */
8103 token = cp_lexer_advance_token (parser->lexer,
8104 parser->lexer->first_token,
8106 cp_lexer_purge_tokens_after (parser->lexer, token);
8109 *is_identifier = true;
8112 if (template_keyword_p)
8116 /* Look up the name. */
8117 decl = cp_parser_lookup_name (parser, identifier,
8119 /*is_template=*/false,
8120 /*is_namespace=*/false,
8121 check_dependency_p);
8122 decl = maybe_get_template_decl_from_type_decl (decl);
8124 /* If DECL is a template, then the name was a template-name. */
8125 if (TREE_CODE (decl) == TEMPLATE_DECL)
8129 /* The standard does not explicitly indicate whether a name that
8130 names a set of overloaded declarations, some of which are
8131 templates, is a template-name. However, such a name should
8132 be a template-name; otherwise, there is no way to form a
8133 template-id for the overloaded templates. */
8134 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8135 if (TREE_CODE (fns) == OVERLOAD)
8139 for (fn = fns; fn; fn = OVL_NEXT (fn))
8140 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8145 /* Otherwise, the name does not name a template. */
8146 cp_parser_error (parser, "expected template-name");
8147 return error_mark_node;
8151 /* If DECL is dependent, and refers to a function, then just return
8152 its name; we will look it up again during template instantiation. */
8153 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8155 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8156 if (TYPE_P (scope) && dependent_type_p (scope))
8163 /* Parse a template-argument-list.
8165 template-argument-list:
8167 template-argument-list , template-argument
8169 Returns a TREE_VEC containing the arguments. */
8172 cp_parser_template_argument_list (cp_parser* parser)
8174 tree fixed_args[10];
8175 unsigned n_args = 0;
8176 unsigned alloced = 10;
8177 tree *arg_ary = fixed_args;
8179 bool saved_in_template_argument_list_p;
8181 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8182 parser->in_template_argument_list_p = true;
8188 /* Consume the comma. */
8189 cp_lexer_consume_token (parser->lexer);
8191 /* Parse the template-argument. */
8192 argument = cp_parser_template_argument (parser);
8193 if (n_args == alloced)
8197 if (arg_ary == fixed_args)
8199 arg_ary = xmalloc (sizeof (tree) * alloced);
8200 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8203 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8205 arg_ary[n_args++] = argument;
8207 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8209 vec = make_tree_vec (n_args);
8212 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8214 if (arg_ary != fixed_args)
8216 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8220 /* Parse a template-argument.
8223 assignment-expression
8227 The representation is that of an assignment-expression, type-id, or
8228 id-expression -- except that the qualified id-expression is
8229 evaluated, so that the value returned is either a DECL or an
8232 Although the standard says "assignment-expression", it forbids
8233 throw-expressions or assignments in the template argument.
8234 Therefore, we use "conditional-expression" instead. */
8237 cp_parser_template_argument (cp_parser* parser)
8242 bool maybe_type_id = false;
8245 tree qualifying_class;
8247 /* There's really no way to know what we're looking at, so we just
8248 try each alternative in order.
8252 In a template-argument, an ambiguity between a type-id and an
8253 expression is resolved to a type-id, regardless of the form of
8254 the corresponding template-parameter.
8256 Therefore, we try a type-id first. */
8257 cp_parser_parse_tentatively (parser);
8258 argument = cp_parser_type_id (parser);
8259 /* If there was no error parsing the type-id but the next token is a '>>',
8260 we probably found a typo for '> >'. But there are type-id which are
8261 also valid expressions. For instance:
8263 struct X { int operator >> (int); };
8264 template <int V> struct Foo {};
8267 Here 'X()' is a valid type-id of a function type, but the user just
8268 wanted to write the expression "X() >> 5". Thus, we remember that we
8269 found a valid type-id, but we still try to parse the argument as an
8270 expression to see what happens. */
8271 if (!cp_parser_error_occurred (parser)
8272 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8274 maybe_type_id = true;
8275 cp_parser_abort_tentative_parse (parser);
8279 /* If the next token isn't a `,' or a `>', then this argument wasn't
8280 really finished. This means that the argument is not a valid
8282 if (!cp_parser_next_token_ends_template_argument_p (parser))
8283 cp_parser_error (parser, "expected template-argument");
8284 /* If that worked, we're done. */
8285 if (cp_parser_parse_definitely (parser))
8288 /* We're still not sure what the argument will be. */
8289 cp_parser_parse_tentatively (parser);
8290 /* Try a template. */
8291 argument = cp_parser_id_expression (parser,
8292 /*template_keyword_p=*/false,
8293 /*check_dependency_p=*/true,
8295 /*declarator_p=*/false);
8296 /* If the next token isn't a `,' or a `>', then this argument wasn't
8298 if (!cp_parser_next_token_ends_template_argument_p (parser))
8299 cp_parser_error (parser, "expected template-argument");
8300 if (!cp_parser_error_occurred (parser))
8302 /* Figure out what is being referred to. */
8303 argument = cp_parser_lookup_name (parser, argument,
8305 /*is_template=*/template_p,
8306 /*is_namespace=*/false,
8307 /*check_dependency=*/true);
8308 if (TREE_CODE (argument) != TEMPLATE_DECL
8309 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8310 cp_parser_error (parser, "expected template-name");
8312 if (cp_parser_parse_definitely (parser))
8314 /* It must be a non-type argument. There permitted cases are given
8315 in [temp.arg.nontype]:
8317 -- an integral constant-expression of integral or enumeration
8320 -- the name of a non-type template-parameter; or
8322 -- the name of an object or function with external linkage...
8324 -- the address of an object or function with external linkage...
8326 -- a pointer to member... */
8327 /* Look for a non-type template parameter. */
8328 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8330 cp_parser_parse_tentatively (parser);
8331 argument = cp_parser_primary_expression (parser,
8334 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8335 || !cp_parser_next_token_ends_template_argument_p (parser))
8336 cp_parser_simulate_error (parser);
8337 if (cp_parser_parse_definitely (parser))
8340 /* If the next token is "&", the argument must be the address of an
8341 object or function with external linkage. */
8342 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8344 cp_lexer_consume_token (parser->lexer);
8345 /* See if we might have an id-expression. */
8346 token = cp_lexer_peek_token (parser->lexer);
8347 if (token->type == CPP_NAME
8348 || token->keyword == RID_OPERATOR
8349 || token->type == CPP_SCOPE
8350 || token->type == CPP_TEMPLATE_ID
8351 || token->type == CPP_NESTED_NAME_SPECIFIER)
8353 cp_parser_parse_tentatively (parser);
8354 argument = cp_parser_primary_expression (parser,
8357 if (cp_parser_error_occurred (parser)
8358 || !cp_parser_next_token_ends_template_argument_p (parser))
8359 cp_parser_abort_tentative_parse (parser);
8362 if (qualifying_class)
8363 argument = finish_qualified_id_expr (qualifying_class,
8367 if (TREE_CODE (argument) == VAR_DECL)
8369 /* A variable without external linkage might still be a
8370 valid constant-expression, so no error is issued here
8371 if the external-linkage check fails. */
8372 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8373 cp_parser_simulate_error (parser);
8375 else if (is_overloaded_fn (argument))
8376 /* All overloaded functions are allowed; if the external
8377 linkage test does not pass, an error will be issued
8381 && (TREE_CODE (argument) == OFFSET_REF
8382 || TREE_CODE (argument) == SCOPE_REF))
8383 /* A pointer-to-member. */
8386 cp_parser_simulate_error (parser);
8388 if (cp_parser_parse_definitely (parser))
8391 argument = build_x_unary_op (ADDR_EXPR, argument);
8396 /* If the argument started with "&", there are no other valid
8397 alternatives at this point. */
8400 cp_parser_error (parser, "invalid non-type template argument");
8401 return error_mark_node;
8403 /* If the argument wasn't successfully parsed as a type-id followed
8404 by '>>', the argument can only be a constant expression now.
8405 Otherwise, we try parsing the constant-expression tentatively,
8406 because the argument could really be a type-id. */
8408 cp_parser_parse_tentatively (parser);
8409 argument = cp_parser_constant_expression (parser,
8410 /*allow_non_constant_p=*/false,
8411 /*non_constant_p=*/NULL);
8412 argument = fold_non_dependent_expr (argument);
8415 if (!cp_parser_next_token_ends_template_argument_p (parser))
8416 cp_parser_error (parser, "expected template-argument");
8417 if (cp_parser_parse_definitely (parser))
8419 /* We did our best to parse the argument as a non type-id, but that
8420 was the only alternative that matched (albeit with a '>' after
8421 it). We can assume it's just a typo from the user, and a
8422 diagnostic will then be issued. */
8423 return cp_parser_type_id (parser);
8426 /* Parse an explicit-instantiation.
8428 explicit-instantiation:
8429 template declaration
8431 Although the standard says `declaration', what it really means is:
8433 explicit-instantiation:
8434 template decl-specifier-seq [opt] declarator [opt] ;
8436 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8437 supposed to be allowed. A defect report has been filed about this
8442 explicit-instantiation:
8443 storage-class-specifier template
8444 decl-specifier-seq [opt] declarator [opt] ;
8445 function-specifier template
8446 decl-specifier-seq [opt] declarator [opt] ; */
8449 cp_parser_explicit_instantiation (cp_parser* parser)
8451 int declares_class_or_enum;
8452 tree decl_specifiers;
8454 tree extension_specifier = NULL_TREE;
8456 /* Look for an (optional) storage-class-specifier or
8457 function-specifier. */
8458 if (cp_parser_allow_gnu_extensions_p (parser))
8461 = cp_parser_storage_class_specifier_opt (parser);
8462 if (!extension_specifier)
8463 extension_specifier = cp_parser_function_specifier_opt (parser);
8466 /* Look for the `template' keyword. */
8467 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8468 /* Let the front end know that we are processing an explicit
8470 begin_explicit_instantiation ();
8471 /* [temp.explicit] says that we are supposed to ignore access
8472 control while processing explicit instantiation directives. */
8473 push_deferring_access_checks (dk_no_check);
8474 /* Parse a decl-specifier-seq. */
8476 = cp_parser_decl_specifier_seq (parser,
8477 CP_PARSER_FLAGS_OPTIONAL,
8479 &declares_class_or_enum);
8480 /* If there was exactly one decl-specifier, and it declared a class,
8481 and there's no declarator, then we have an explicit type
8483 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8487 type = check_tag_decl (decl_specifiers);
8488 /* Turn access control back on for names used during
8489 template instantiation. */
8490 pop_deferring_access_checks ();
8492 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8499 /* Parse the declarator. */
8501 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8502 /*ctor_dtor_or_conv_p=*/NULL,
8503 /*parenthesized_p=*/NULL);
8504 cp_parser_check_for_definition_in_return_type (declarator,
8505 declares_class_or_enum);
8506 if (declarator != error_mark_node)
8508 decl = grokdeclarator (declarator, decl_specifiers,
8510 /* Turn access control back on for names used during
8511 template instantiation. */
8512 pop_deferring_access_checks ();
8513 /* Do the explicit instantiation. */
8514 do_decl_instantiation (decl, extension_specifier);
8518 pop_deferring_access_checks ();
8519 /* Skip the body of the explicit instantiation. */
8520 cp_parser_skip_to_end_of_statement (parser);
8523 /* We're done with the instantiation. */
8524 end_explicit_instantiation ();
8526 cp_parser_consume_semicolon_at_end_of_statement (parser);
8529 /* Parse an explicit-specialization.
8531 explicit-specialization:
8532 template < > declaration
8534 Although the standard says `declaration', what it really means is:
8536 explicit-specialization:
8537 template <> decl-specifier [opt] init-declarator [opt] ;
8538 template <> function-definition
8539 template <> explicit-specialization
8540 template <> template-declaration */
8543 cp_parser_explicit_specialization (cp_parser* parser)
8545 /* Look for the `template' keyword. */
8546 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8547 /* Look for the `<'. */
8548 cp_parser_require (parser, CPP_LESS, "`<'");
8549 /* Look for the `>'. */
8550 cp_parser_require (parser, CPP_GREATER, "`>'");
8551 /* We have processed another parameter list. */
8552 ++parser->num_template_parameter_lists;
8553 /* Let the front end know that we are beginning a specialization. */
8554 begin_specialization ();
8556 /* If the next keyword is `template', we need to figure out whether
8557 or not we're looking a template-declaration. */
8558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8560 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8561 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8562 cp_parser_template_declaration_after_export (parser,
8563 /*member_p=*/false);
8565 cp_parser_explicit_specialization (parser);
8568 /* Parse the dependent declaration. */
8569 cp_parser_single_declaration (parser,
8573 /* We're done with the specialization. */
8574 end_specialization ();
8575 /* We're done with this parameter list. */
8576 --parser->num_template_parameter_lists;
8579 /* Parse a type-specifier.
8582 simple-type-specifier
8585 elaborated-type-specifier
8593 Returns a representation of the type-specifier. If the
8594 type-specifier is a keyword (like `int' or `const', or
8595 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8596 For a class-specifier, enum-specifier, or elaborated-type-specifier
8597 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8599 If IS_FRIEND is TRUE then this type-specifier is being declared a
8600 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8601 appearing in a decl-specifier-seq.
8603 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8604 class-specifier, enum-specifier, or elaborated-type-specifier, then
8605 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8606 if a type is declared; 2 if it is defined. Otherwise, it is set to
8609 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8610 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8614 cp_parser_type_specifier (cp_parser* parser,
8615 cp_parser_flags flags,
8617 bool is_declaration,
8618 int* declares_class_or_enum,
8619 bool* is_cv_qualifier)
8621 tree type_spec = NULL_TREE;
8625 /* Assume this type-specifier does not declare a new type. */
8626 if (declares_class_or_enum)
8627 *declares_class_or_enum = 0;
8628 /* And that it does not specify a cv-qualifier. */
8629 if (is_cv_qualifier)
8630 *is_cv_qualifier = false;
8631 /* Peek at the next token. */
8632 token = cp_lexer_peek_token (parser->lexer);
8634 /* If we're looking at a keyword, we can use that to guide the
8635 production we choose. */
8636 keyword = token->keyword;
8639 /* Any of these indicate either a class-specifier, or an
8640 elaborated-type-specifier. */
8645 /* Parse tentatively so that we can back up if we don't find a
8646 class-specifier or enum-specifier. */
8647 cp_parser_parse_tentatively (parser);
8648 /* Look for the class-specifier or enum-specifier. */
8649 if (keyword == RID_ENUM)
8650 type_spec = cp_parser_enum_specifier (parser);
8652 type_spec = cp_parser_class_specifier (parser);
8654 /* If that worked, we're done. */
8655 if (cp_parser_parse_definitely (parser))
8657 if (declares_class_or_enum)
8658 *declares_class_or_enum = 2;
8665 /* Look for an elaborated-type-specifier. */
8666 type_spec = cp_parser_elaborated_type_specifier (parser,
8669 /* We're declaring a class or enum -- unless we're using
8671 if (declares_class_or_enum && keyword != RID_TYPENAME)
8672 *declares_class_or_enum = 1;
8678 type_spec = cp_parser_cv_qualifier_opt (parser);
8679 /* Even though we call a routine that looks for an optional
8680 qualifier, we know that there should be one. */
8681 my_friendly_assert (type_spec != NULL, 20000328);
8682 /* This type-specifier was a cv-qualified. */
8683 if (is_cv_qualifier)
8684 *is_cv_qualifier = true;
8689 /* The `__complex__' keyword is a GNU extension. */
8690 return cp_lexer_consume_token (parser->lexer)->value;
8696 /* If we do not already have a type-specifier, assume we are looking
8697 at a simple-type-specifier. */
8698 type_spec = cp_parser_simple_type_specifier (parser, flags,
8699 /*identifier_p=*/true);
8701 /* If we didn't find a type-specifier, and a type-specifier was not
8702 optional in this context, issue an error message. */
8703 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8705 cp_parser_error (parser, "expected type specifier");
8706 return error_mark_node;
8712 /* Parse a simple-type-specifier.
8714 simple-type-specifier:
8715 :: [opt] nested-name-specifier [opt] type-name
8716 :: [opt] nested-name-specifier template template-id
8731 simple-type-specifier:
8732 __typeof__ unary-expression
8733 __typeof__ ( type-id )
8735 For the various keywords, the value returned is simply the
8736 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8737 For the first two productions, and if IDENTIFIER_P is false, the
8738 value returned is the indicated TYPE_DECL. */
8741 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8744 tree type = NULL_TREE;
8747 /* Peek at the next token. */
8748 token = cp_lexer_peek_token (parser->lexer);
8750 /* If we're looking at a keyword, things are easy. */
8751 switch (token->keyword)
8754 type = char_type_node;
8757 type = wchar_type_node;
8760 type = boolean_type_node;
8763 type = short_integer_type_node;
8766 type = integer_type_node;
8769 type = long_integer_type_node;
8772 type = integer_type_node;
8775 type = unsigned_type_node;
8778 type = float_type_node;
8781 type = double_type_node;
8784 type = void_type_node;
8791 /* Consume the `typeof' token. */
8792 cp_lexer_consume_token (parser->lexer);
8793 /* Parse the operand to `typeof'. */
8794 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8795 /* If it is not already a TYPE, take its type. */
8796 if (!TYPE_P (operand))
8797 operand = finish_typeof (operand);
8806 /* If the type-specifier was for a built-in type, we're done. */
8811 /* Consume the token. */
8812 id = cp_lexer_consume_token (parser->lexer)->value;
8814 /* There is no valid C++ program where a non-template type is
8815 followed by a "<". That usually indicates that the user thought
8816 that the type was a template. */
8817 cp_parser_check_for_invalid_template_id (parser, type);
8819 return identifier_p ? id : TYPE_NAME (type);
8822 /* The type-specifier must be a user-defined type. */
8823 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8825 /* Don't gobble tokens or issue error messages if this is an
8826 optional type-specifier. */
8827 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8828 cp_parser_parse_tentatively (parser);
8830 /* Look for the optional `::' operator. */
8831 cp_parser_global_scope_opt (parser,
8832 /*current_scope_valid_p=*/false);
8833 /* Look for the nested-name specifier. */
8834 cp_parser_nested_name_specifier_opt (parser,
8835 /*typename_keyword_p=*/false,
8836 /*check_dependency_p=*/true,
8838 /*is_declaration=*/false);
8839 /* If we have seen a nested-name-specifier, and the next token
8840 is `template', then we are using the template-id production. */
8842 && cp_parser_optional_template_keyword (parser))
8844 /* Look for the template-id. */
8845 type = cp_parser_template_id (parser,
8846 /*template_keyword_p=*/true,
8847 /*check_dependency_p=*/true,
8848 /*is_declaration=*/false);
8849 /* If the template-id did not name a type, we are out of
8851 if (TREE_CODE (type) != TYPE_DECL)
8853 cp_parser_error (parser, "expected template-id for type");
8857 /* Otherwise, look for a type-name. */
8859 type = cp_parser_type_name (parser);
8860 /* If it didn't work out, we don't have a TYPE. */
8861 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8862 && !cp_parser_parse_definitely (parser))
8866 /* If we didn't get a type-name, issue an error message. */
8867 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8869 cp_parser_error (parser, "expected type-name");
8870 return error_mark_node;
8873 /* There is no valid C++ program where a non-template type is
8874 followed by a "<". That usually indicates that the user thought
8875 that the type was a template. */
8876 if (type && type != error_mark_node)
8877 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8882 /* Parse a type-name.
8895 Returns a TYPE_DECL for the the type. */
8898 cp_parser_type_name (cp_parser* parser)
8903 /* We can't know yet whether it is a class-name or not. */
8904 cp_parser_parse_tentatively (parser);
8905 /* Try a class-name. */
8906 type_decl = cp_parser_class_name (parser,
8907 /*typename_keyword_p=*/false,
8908 /*template_keyword_p=*/false,
8910 /*check_dependency_p=*/true,
8911 /*class_head_p=*/false,
8912 /*is_declaration=*/false);
8913 /* If it's not a class-name, keep looking. */
8914 if (!cp_parser_parse_definitely (parser))
8916 /* It must be a typedef-name or an enum-name. */
8917 identifier = cp_parser_identifier (parser);
8918 if (identifier == error_mark_node)
8919 return error_mark_node;
8921 /* Look up the type-name. */
8922 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8923 /* Issue an error if we did not find a type-name. */
8924 if (TREE_CODE (type_decl) != TYPE_DECL)
8926 if (!cp_parser_simulate_error (parser))
8927 cp_parser_name_lookup_error (parser, identifier, type_decl,
8929 type_decl = error_mark_node;
8931 /* Remember that the name was used in the definition of the
8932 current class so that we can check later to see if the
8933 meaning would have been different after the class was
8934 entirely defined. */
8935 else if (type_decl != error_mark_node
8937 maybe_note_name_used_in_class (identifier, type_decl);
8944 /* Parse an elaborated-type-specifier. Note that the grammar given
8945 here incorporates the resolution to DR68.
8947 elaborated-type-specifier:
8948 class-key :: [opt] nested-name-specifier [opt] identifier
8949 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8950 enum :: [opt] nested-name-specifier [opt] identifier
8951 typename :: [opt] nested-name-specifier identifier
8952 typename :: [opt] nested-name-specifier template [opt]
8957 elaborated-type-specifier:
8958 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8959 class-key attributes :: [opt] nested-name-specifier [opt]
8960 template [opt] template-id
8961 enum attributes :: [opt] nested-name-specifier [opt] identifier
8963 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8964 declared `friend'. If IS_DECLARATION is TRUE, then this
8965 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8966 something is being declared.
8968 Returns the TYPE specified. */
8971 cp_parser_elaborated_type_specifier (cp_parser* parser,
8973 bool is_declaration)
8975 enum tag_types tag_type;
8977 tree type = NULL_TREE;
8978 tree attributes = NULL_TREE;
8980 /* See if we're looking at the `enum' keyword. */
8981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8983 /* Consume the `enum' token. */
8984 cp_lexer_consume_token (parser->lexer);
8985 /* Remember that it's an enumeration type. */
8986 tag_type = enum_type;
8987 /* Parse the attributes. */
8988 attributes = cp_parser_attributes_opt (parser);
8990 /* Or, it might be `typename'. */
8991 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8994 /* Consume the `typename' token. */
8995 cp_lexer_consume_token (parser->lexer);
8996 /* Remember that it's a `typename' type. */
8997 tag_type = typename_type;
8998 /* The `typename' keyword is only allowed in templates. */
8999 if (!processing_template_decl)
9000 pedwarn ("using `typename' outside of template");
9002 /* Otherwise it must be a class-key. */
9005 tag_type = cp_parser_class_key (parser);
9006 if (tag_type == none_type)
9007 return error_mark_node;
9008 /* Parse the attributes. */
9009 attributes = cp_parser_attributes_opt (parser);
9012 /* Look for the `::' operator. */
9013 cp_parser_global_scope_opt (parser,
9014 /*current_scope_valid_p=*/false);
9015 /* Look for the nested-name-specifier. */
9016 if (tag_type == typename_type)
9018 if (cp_parser_nested_name_specifier (parser,
9019 /*typename_keyword_p=*/true,
9020 /*check_dependency_p=*/true,
9024 return error_mark_node;
9027 /* Even though `typename' is not present, the proposed resolution
9028 to Core Issue 180 says that in `class A<T>::B', `B' should be
9029 considered a type-name, even if `A<T>' is dependent. */
9030 cp_parser_nested_name_specifier_opt (parser,
9031 /*typename_keyword_p=*/true,
9032 /*check_dependency_p=*/true,
9035 /* For everything but enumeration types, consider a template-id. */
9036 if (tag_type != enum_type)
9038 bool template_p = false;
9041 /* Allow the `template' keyword. */
9042 template_p = cp_parser_optional_template_keyword (parser);
9043 /* If we didn't see `template', we don't know if there's a
9044 template-id or not. */
9046 cp_parser_parse_tentatively (parser);
9047 /* Parse the template-id. */
9048 decl = cp_parser_template_id (parser, template_p,
9049 /*check_dependency_p=*/true,
9051 /* If we didn't find a template-id, look for an ordinary
9053 if (!template_p && !cp_parser_parse_definitely (parser))
9055 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9056 in effect, then we must assume that, upon instantiation, the
9057 template will correspond to a class. */
9058 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9059 && tag_type == typename_type)
9060 type = make_typename_type (parser->scope, decl,
9063 type = TREE_TYPE (decl);
9066 /* For an enumeration type, consider only a plain identifier. */
9069 identifier = cp_parser_identifier (parser);
9071 if (identifier == error_mark_node)
9073 parser->scope = NULL_TREE;
9074 return error_mark_node;
9077 /* For a `typename', we needn't call xref_tag. */
9078 if (tag_type == typename_type)
9079 return make_typename_type (parser->scope, identifier,
9081 /* Look up a qualified name in the usual way. */
9086 /* In an elaborated-type-specifier, names are assumed to name
9087 types, so we set IS_TYPE to TRUE when calling
9088 cp_parser_lookup_name. */
9089 decl = cp_parser_lookup_name (parser, identifier,
9091 /*is_template=*/false,
9092 /*is_namespace=*/false,
9093 /*check_dependency=*/true);
9095 /* If we are parsing friend declaration, DECL may be a
9096 TEMPLATE_DECL tree node here. However, we need to check
9097 whether this TEMPLATE_DECL results in valid code. Consider
9098 the following example:
9101 template <class T> class C {};
9104 template <class T> friend class N::C; // #1, valid code
9106 template <class T> class Y {
9107 friend class N::C; // #2, invalid code
9110 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9111 name lookup of `N::C'. We see that friend declaration must
9112 be template for the code to be valid. Note that
9113 processing_template_decl does not work here since it is
9114 always 1 for the above two cases. */
9116 decl = (cp_parser_maybe_treat_template_as_class
9117 (decl, /*tag_name_p=*/is_friend
9118 && parser->num_template_parameter_lists));
9120 if (TREE_CODE (decl) != TYPE_DECL)
9122 error ("expected type-name");
9123 return error_mark_node;
9126 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9127 check_elaborated_type_specifier
9129 (parser->num_template_parameter_lists
9130 || DECL_SELF_REFERENCE_P (decl)));
9132 type = TREE_TYPE (decl);
9136 /* An elaborated-type-specifier sometimes introduces a new type and
9137 sometimes names an existing type. Normally, the rule is that it
9138 introduces a new type only if there is not an existing type of
9139 the same name already in scope. For example, given:
9142 void f() { struct S s; }
9144 the `struct S' in the body of `f' is the same `struct S' as in
9145 the global scope; the existing definition is used. However, if
9146 there were no global declaration, this would introduce a new
9147 local class named `S'.
9149 An exception to this rule applies to the following code:
9151 namespace N { struct S; }
9153 Here, the elaborated-type-specifier names a new type
9154 unconditionally; even if there is already an `S' in the
9155 containing scope this declaration names a new type.
9156 This exception only applies if the elaborated-type-specifier
9157 forms the complete declaration:
9161 A declaration consisting solely of `class-key identifier ;' is
9162 either a redeclaration of the name in the current scope or a
9163 forward declaration of the identifier as a class name. It
9164 introduces the name into the current scope.
9166 We are in this situation precisely when the next token is a `;'.
9168 An exception to the exception is that a `friend' declaration does
9169 *not* name a new type; i.e., given:
9171 struct S { friend struct T; };
9173 `T' is not a new type in the scope of `S'.
9175 Also, `new struct S' or `sizeof (struct S)' never results in the
9176 definition of a new type; a new type can only be declared in a
9177 declaration context. */
9179 /* Warn about attributes. They are ignored. */
9181 warning ("type attributes are honored only at type definition");
9183 type = xref_tag (tag_type, identifier,
9184 /*attributes=*/NULL_TREE,
9187 || cp_lexer_next_token_is_not (parser->lexer,
9189 parser->num_template_parameter_lists);
9192 if (tag_type != enum_type)
9193 cp_parser_check_class_key (tag_type, type);
9195 /* A "<" cannot follow an elaborated type specifier. If that
9196 happens, the user was probably trying to form a template-id. */
9197 cp_parser_check_for_invalid_template_id (parser, type);
9202 /* Parse an enum-specifier.
9205 enum identifier [opt] { enumerator-list [opt] }
9207 Returns an ENUM_TYPE representing the enumeration. */
9210 cp_parser_enum_specifier (cp_parser* parser)
9213 tree identifier = NULL_TREE;
9216 /* Look for the `enum' keyword. */
9217 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9218 return error_mark_node;
9219 /* Peek at the next token. */
9220 token = cp_lexer_peek_token (parser->lexer);
9222 /* See if it is an identifier. */
9223 if (token->type == CPP_NAME)
9224 identifier = cp_parser_identifier (parser);
9226 /* Look for the `{'. */
9227 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9228 return error_mark_node;
9230 /* At this point, we're going ahead with the enum-specifier, even
9231 if some other problem occurs. */
9232 cp_parser_commit_to_tentative_parse (parser);
9234 /* Issue an error message if type-definitions are forbidden here. */
9235 cp_parser_check_type_definition (parser);
9237 /* Create the new type. */
9238 type = start_enum (identifier ? identifier : make_anon_name ());
9240 /* Peek at the next token. */
9241 token = cp_lexer_peek_token (parser->lexer);
9242 /* If it's not a `}', then there are some enumerators. */
9243 if (token->type != CPP_CLOSE_BRACE)
9244 cp_parser_enumerator_list (parser, type);
9245 /* Look for the `}'. */
9246 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9248 /* Finish up the enumeration. */
9254 /* Parse an enumerator-list. The enumerators all have the indicated
9258 enumerator-definition
9259 enumerator-list , enumerator-definition */
9262 cp_parser_enumerator_list (cp_parser* parser, tree type)
9268 /* Parse an enumerator-definition. */
9269 cp_parser_enumerator_definition (parser, type);
9270 /* Peek at the next token. */
9271 token = cp_lexer_peek_token (parser->lexer);
9272 /* If it's not a `,', then we've reached the end of the
9274 if (token->type != CPP_COMMA)
9276 /* Otherwise, consume the `,' and keep going. */
9277 cp_lexer_consume_token (parser->lexer);
9278 /* If the next token is a `}', there is a trailing comma. */
9279 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9281 if (pedantic && !in_system_header)
9282 pedwarn ("comma at end of enumerator list");
9288 /* Parse an enumerator-definition. The enumerator has the indicated
9291 enumerator-definition:
9293 enumerator = constant-expression
9299 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9305 /* Look for the identifier. */
9306 identifier = cp_parser_identifier (parser);
9307 if (identifier == error_mark_node)
9310 /* Peek at the next token. */
9311 token = cp_lexer_peek_token (parser->lexer);
9312 /* If it's an `=', then there's an explicit value. */
9313 if (token->type == CPP_EQ)
9315 /* Consume the `=' token. */
9316 cp_lexer_consume_token (parser->lexer);
9317 /* Parse the value. */
9318 value = cp_parser_constant_expression (parser,
9319 /*allow_non_constant_p=*/false,
9325 /* Create the enumerator. */
9326 build_enumerator (identifier, value, type);
9329 /* Parse a namespace-name.
9332 original-namespace-name
9335 Returns the NAMESPACE_DECL for the namespace. */
9338 cp_parser_namespace_name (cp_parser* parser)
9341 tree namespace_decl;
9343 /* Get the name of the namespace. */
9344 identifier = cp_parser_identifier (parser);
9345 if (identifier == error_mark_node)
9346 return error_mark_node;
9348 /* Look up the identifier in the currently active scope. Look only
9349 for namespaces, due to:
9353 When looking up a namespace-name in a using-directive or alias
9354 definition, only namespace names are considered.
9360 During the lookup of a name preceding the :: scope resolution
9361 operator, object, function, and enumerator names are ignored.
9363 (Note that cp_parser_class_or_namespace_name only calls this
9364 function if the token after the name is the scope resolution
9366 namespace_decl = cp_parser_lookup_name (parser, identifier,
9368 /*is_template=*/false,
9369 /*is_namespace=*/true,
9370 /*check_dependency=*/true);
9371 /* If it's not a namespace, issue an error. */
9372 if (namespace_decl == error_mark_node
9373 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9375 cp_parser_error (parser, "expected namespace-name");
9376 namespace_decl = error_mark_node;
9379 return namespace_decl;
9382 /* Parse a namespace-definition.
9384 namespace-definition:
9385 named-namespace-definition
9386 unnamed-namespace-definition
9388 named-namespace-definition:
9389 original-namespace-definition
9390 extension-namespace-definition
9392 original-namespace-definition:
9393 namespace identifier { namespace-body }
9395 extension-namespace-definition:
9396 namespace original-namespace-name { namespace-body }
9398 unnamed-namespace-definition:
9399 namespace { namespace-body } */
9402 cp_parser_namespace_definition (cp_parser* parser)
9406 /* Look for the `namespace' keyword. */
9407 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9409 /* Get the name of the namespace. We do not attempt to distinguish
9410 between an original-namespace-definition and an
9411 extension-namespace-definition at this point. The semantic
9412 analysis routines are responsible for that. */
9413 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9414 identifier = cp_parser_identifier (parser);
9416 identifier = NULL_TREE;
9418 /* Look for the `{' to start the namespace. */
9419 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9420 /* Start the namespace. */
9421 push_namespace (identifier);
9422 /* Parse the body of the namespace. */
9423 cp_parser_namespace_body (parser);
9424 /* Finish the namespace. */
9426 /* Look for the final `}'. */
9427 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9430 /* Parse a namespace-body.
9433 declaration-seq [opt] */
9436 cp_parser_namespace_body (cp_parser* parser)
9438 cp_parser_declaration_seq_opt (parser);
9441 /* Parse a namespace-alias-definition.
9443 namespace-alias-definition:
9444 namespace identifier = qualified-namespace-specifier ; */
9447 cp_parser_namespace_alias_definition (cp_parser* parser)
9450 tree namespace_specifier;
9452 /* Look for the `namespace' keyword. */
9453 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9454 /* Look for the identifier. */
9455 identifier = cp_parser_identifier (parser);
9456 if (identifier == error_mark_node)
9458 /* Look for the `=' token. */
9459 cp_parser_require (parser, CPP_EQ, "`='");
9460 /* Look for the qualified-namespace-specifier. */
9462 = cp_parser_qualified_namespace_specifier (parser);
9463 /* Look for the `;' token. */
9464 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9466 /* Register the alias in the symbol table. */
9467 do_namespace_alias (identifier, namespace_specifier);
9470 /* Parse a qualified-namespace-specifier.
9472 qualified-namespace-specifier:
9473 :: [opt] nested-name-specifier [opt] namespace-name
9475 Returns a NAMESPACE_DECL corresponding to the specified
9479 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9481 /* Look for the optional `::'. */
9482 cp_parser_global_scope_opt (parser,
9483 /*current_scope_valid_p=*/false);
9485 /* Look for the optional nested-name-specifier. */
9486 cp_parser_nested_name_specifier_opt (parser,
9487 /*typename_keyword_p=*/false,
9488 /*check_dependency_p=*/true,
9490 /*is_declaration=*/true);
9492 return cp_parser_namespace_name (parser);
9495 /* Parse a using-declaration.
9498 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9499 using :: unqualified-id ; */
9502 cp_parser_using_declaration (cp_parser* parser)
9505 bool typename_p = false;
9506 bool global_scope_p;
9512 /* Look for the `using' keyword. */
9513 cp_parser_require_keyword (parser, RID_USING, "`using'");
9515 /* Peek at the next token. */
9516 token = cp_lexer_peek_token (parser->lexer);
9517 /* See if it's `typename'. */
9518 if (token->keyword == RID_TYPENAME)
9520 /* Remember that we've seen it. */
9522 /* Consume the `typename' token. */
9523 cp_lexer_consume_token (parser->lexer);
9526 /* Look for the optional global scope qualification. */
9528 = (cp_parser_global_scope_opt (parser,
9529 /*current_scope_valid_p=*/false)
9532 /* If we saw `typename', or didn't see `::', then there must be a
9533 nested-name-specifier present. */
9534 if (typename_p || !global_scope_p)
9535 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9536 /*check_dependency_p=*/true,
9538 /*is_declaration=*/true);
9539 /* Otherwise, we could be in either of the two productions. In that
9540 case, treat the nested-name-specifier as optional. */
9542 qscope = cp_parser_nested_name_specifier_opt (parser,
9543 /*typename_keyword_p=*/false,
9544 /*check_dependency_p=*/true,
9546 /*is_declaration=*/true);
9548 qscope = global_namespace;
9550 /* Parse the unqualified-id. */
9551 identifier = cp_parser_unqualified_id (parser,
9552 /*template_keyword_p=*/false,
9553 /*check_dependency_p=*/true,
9554 /*declarator_p=*/true);
9556 /* The function we call to handle a using-declaration is different
9557 depending on what scope we are in. */
9558 if (identifier == error_mark_node)
9560 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9561 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9562 /* [namespace.udecl]
9564 A using declaration shall not name a template-id. */
9565 error ("a template-id may not appear in a using-declaration");
9568 scope = current_scope ();
9569 if (scope && TYPE_P (scope))
9571 /* Create the USING_DECL. */
9572 decl = do_class_using_decl (build_nt (SCOPE_REF,
9575 /* Add it to the list of members in this class. */
9576 finish_member_declaration (decl);
9580 decl = cp_parser_lookup_name_simple (parser, identifier);
9581 if (decl == error_mark_node)
9582 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9584 do_local_using_decl (decl, qscope, identifier);
9586 do_toplevel_using_decl (decl, qscope, identifier);
9590 /* Look for the final `;'. */
9591 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9594 /* Parse a using-directive.
9597 using namespace :: [opt] nested-name-specifier [opt]
9601 cp_parser_using_directive (cp_parser* parser)
9603 tree namespace_decl;
9606 /* Look for the `using' keyword. */
9607 cp_parser_require_keyword (parser, RID_USING, "`using'");
9608 /* And the `namespace' keyword. */
9609 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9610 /* Look for the optional `::' operator. */
9611 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9612 /* And the optional nested-name-specifier. */
9613 cp_parser_nested_name_specifier_opt (parser,
9614 /*typename_keyword_p=*/false,
9615 /*check_dependency_p=*/true,
9617 /*is_declaration=*/true);
9618 /* Get the namespace being used. */
9619 namespace_decl = cp_parser_namespace_name (parser);
9620 /* And any specified attributes. */
9621 attribs = cp_parser_attributes_opt (parser);
9622 /* Update the symbol table. */
9623 parse_using_directive (namespace_decl, attribs);
9624 /* Look for the final `;'. */
9625 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9628 /* Parse an asm-definition.
9631 asm ( string-literal ) ;
9636 asm volatile [opt] ( string-literal ) ;
9637 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9638 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9639 : asm-operand-list [opt] ) ;
9640 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9641 : asm-operand-list [opt]
9642 : asm-operand-list [opt] ) ; */
9645 cp_parser_asm_definition (cp_parser* parser)
9649 tree outputs = NULL_TREE;
9650 tree inputs = NULL_TREE;
9651 tree clobbers = NULL_TREE;
9653 bool volatile_p = false;
9654 bool extended_p = false;
9656 /* Look for the `asm' keyword. */
9657 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9658 /* See if the next token is `volatile'. */
9659 if (cp_parser_allow_gnu_extensions_p (parser)
9660 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9662 /* Remember that we saw the `volatile' keyword. */
9664 /* Consume the token. */
9665 cp_lexer_consume_token (parser->lexer);
9667 /* Look for the opening `('. */
9668 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9669 /* Look for the string. */
9670 token = cp_parser_require (parser, CPP_STRING, "asm body");
9673 string = token->value;
9674 /* If we're allowing GNU extensions, check for the extended assembly
9675 syntax. Unfortunately, the `:' tokens need not be separated by
9676 a space in C, and so, for compatibility, we tolerate that here
9677 too. Doing that means that we have to treat the `::' operator as
9679 if (cp_parser_allow_gnu_extensions_p (parser)
9680 && at_function_scope_p ()
9681 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9682 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9684 bool inputs_p = false;
9685 bool clobbers_p = false;
9687 /* The extended syntax was used. */
9690 /* Look for outputs. */
9691 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9693 /* Consume the `:'. */
9694 cp_lexer_consume_token (parser->lexer);
9695 /* Parse the output-operands. */
9696 if (cp_lexer_next_token_is_not (parser->lexer,
9698 && cp_lexer_next_token_is_not (parser->lexer,
9700 && cp_lexer_next_token_is_not (parser->lexer,
9702 outputs = cp_parser_asm_operand_list (parser);
9704 /* If the next token is `::', there are no outputs, and the
9705 next token is the beginning of the inputs. */
9706 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9708 /* Consume the `::' token. */
9709 cp_lexer_consume_token (parser->lexer);
9710 /* The inputs are coming next. */
9714 /* Look for inputs. */
9716 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9719 /* Consume the `:'. */
9720 cp_lexer_consume_token (parser->lexer);
9721 /* Parse the output-operands. */
9722 if (cp_lexer_next_token_is_not (parser->lexer,
9724 && cp_lexer_next_token_is_not (parser->lexer,
9726 && cp_lexer_next_token_is_not (parser->lexer,
9728 inputs = cp_parser_asm_operand_list (parser);
9730 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9731 /* The clobbers are coming next. */
9734 /* Look for clobbers. */
9736 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9739 /* Consume the `:'. */
9740 cp_lexer_consume_token (parser->lexer);
9741 /* Parse the clobbers. */
9742 if (cp_lexer_next_token_is_not (parser->lexer,
9744 clobbers = cp_parser_asm_clobber_list (parser);
9747 /* Look for the closing `)'. */
9748 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9749 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9750 /*consume_paren=*/true);
9751 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9753 /* Create the ASM_STMT. */
9754 if (at_function_scope_p ())
9757 finish_asm_stmt (volatile_p
9758 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9759 string, outputs, inputs, clobbers);
9760 /* If the extended syntax was not used, mark the ASM_STMT. */
9762 ASM_INPUT_P (asm_stmt) = 1;
9765 assemble_asm (string);
9768 /* Declarators [gram.dcl.decl] */
9770 /* Parse an init-declarator.
9773 declarator initializer [opt]
9778 declarator asm-specification [opt] attributes [opt] initializer [opt]
9780 function-definition:
9781 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9783 decl-specifier-seq [opt] declarator function-try-block
9787 function-definition:
9788 __extension__ function-definition
9790 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9791 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9792 then this declarator appears in a class scope. The new DECL created
9793 by this declarator is returned.
9795 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9796 for a function-definition here as well. If the declarator is a
9797 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9798 be TRUE upon return. By that point, the function-definition will
9799 have been completely parsed.
9801 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9805 cp_parser_init_declarator (cp_parser* parser,
9806 tree decl_specifiers,
9807 tree prefix_attributes,
9808 bool function_definition_allowed_p,
9810 int declares_class_or_enum,
9811 bool* function_definition_p)
9816 tree asm_specification;
9818 tree decl = NULL_TREE;
9820 bool is_initialized;
9821 bool is_parenthesized_init;
9822 bool is_non_constant_init;
9823 int ctor_dtor_or_conv_p;
9826 /* Assume that this is not the declarator for a function
9828 if (function_definition_p)
9829 *function_definition_p = false;
9831 /* Defer access checks while parsing the declarator; we cannot know
9832 what names are accessible until we know what is being
9834 resume_deferring_access_checks ();
9836 /* Parse the declarator. */
9838 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9839 &ctor_dtor_or_conv_p,
9840 /*parenthesized_p=*/NULL);
9841 /* Gather up the deferred checks. */
9842 stop_deferring_access_checks ();
9844 /* If the DECLARATOR was erroneous, there's no need to go
9846 if (declarator == error_mark_node)
9847 return error_mark_node;
9849 cp_parser_check_for_definition_in_return_type (declarator,
9850 declares_class_or_enum);
9852 /* Figure out what scope the entity declared by the DECLARATOR is
9853 located in. `grokdeclarator' sometimes changes the scope, so
9854 we compute it now. */
9855 scope = get_scope_of_declarator (declarator);
9857 /* If we're allowing GNU extensions, look for an asm-specification
9859 if (cp_parser_allow_gnu_extensions_p (parser))
9861 /* Look for an asm-specification. */
9862 asm_specification = cp_parser_asm_specification_opt (parser);
9863 /* And attributes. */
9864 attributes = cp_parser_attributes_opt (parser);
9868 asm_specification = NULL_TREE;
9869 attributes = NULL_TREE;
9872 /* Peek at the next token. */
9873 token = cp_lexer_peek_token (parser->lexer);
9874 /* Check to see if the token indicates the start of a
9875 function-definition. */
9876 if (cp_parser_token_starts_function_definition_p (token))
9878 if (!function_definition_allowed_p)
9880 /* If a function-definition should not appear here, issue an
9882 cp_parser_error (parser,
9883 "a function-definition is not allowed here");
9884 return error_mark_node;
9888 /* Neither attributes nor an asm-specification are allowed
9889 on a function-definition. */
9890 if (asm_specification)
9891 error ("an asm-specification is not allowed on a function-definition");
9893 error ("attributes are not allowed on a function-definition");
9894 /* This is a function-definition. */
9895 *function_definition_p = true;
9897 /* Parse the function definition. */
9899 decl = cp_parser_save_member_function_body (parser,
9905 = (cp_parser_function_definition_from_specifiers_and_declarator
9906 (parser, decl_specifiers, prefix_attributes, declarator));
9914 Only in function declarations for constructors, destructors, and
9915 type conversions can the decl-specifier-seq be omitted.
9917 We explicitly postpone this check past the point where we handle
9918 function-definitions because we tolerate function-definitions
9919 that are missing their return types in some modes. */
9920 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9922 cp_parser_error (parser,
9923 "expected constructor, destructor, or type conversion");
9924 return error_mark_node;
9927 /* An `=' or an `(' indicates an initializer. */
9928 is_initialized = (token->type == CPP_EQ
9929 || token->type == CPP_OPEN_PAREN);
9930 /* If the init-declarator isn't initialized and isn't followed by a
9931 `,' or `;', it's not a valid init-declarator. */
9933 && token->type != CPP_COMMA
9934 && token->type != CPP_SEMICOLON)
9936 cp_parser_error (parser, "expected init-declarator");
9937 return error_mark_node;
9940 /* Because start_decl has side-effects, we should only call it if we
9941 know we're going ahead. By this point, we know that we cannot
9942 possibly be looking at any other construct. */
9943 cp_parser_commit_to_tentative_parse (parser);
9945 /* If the decl specifiers were bad, issue an error now that we're
9946 sure this was intended to be a declarator. Then continue
9947 declaring the variable(s), as int, to try to cut down on further
9949 if (decl_specifiers != NULL
9950 && TREE_VALUE (decl_specifiers) == error_mark_node)
9952 cp_parser_error (parser, "invalid type in declaration");
9953 TREE_VALUE (decl_specifiers) = integer_type_node;
9956 /* Check to see whether or not this declaration is a friend. */
9957 friend_p = cp_parser_friend_p (decl_specifiers);
9959 /* Check that the number of template-parameter-lists is OK. */
9960 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9961 return error_mark_node;
9963 /* Enter the newly declared entry in the symbol table. If we're
9964 processing a declaration in a class-specifier, we wait until
9965 after processing the initializer. */
9968 if (parser->in_unbraced_linkage_specification_p)
9970 decl_specifiers = tree_cons (error_mark_node,
9971 get_identifier ("extern"),
9973 have_extern_spec = false;
9975 decl = start_decl (declarator, decl_specifiers,
9976 is_initialized, attributes, prefix_attributes);
9979 /* Enter the SCOPE. That way unqualified names appearing in the
9980 initializer will be looked up in SCOPE. */
9984 /* Perform deferred access control checks, now that we know in which
9985 SCOPE the declared entity resides. */
9986 if (!member_p && decl)
9988 tree saved_current_function_decl = NULL_TREE;
9990 /* If the entity being declared is a function, pretend that we
9991 are in its scope. If it is a `friend', it may have access to
9992 things that would not otherwise be accessible. */
9993 if (TREE_CODE (decl) == FUNCTION_DECL)
9995 saved_current_function_decl = current_function_decl;
9996 current_function_decl = decl;
9999 /* Perform the access control checks for the declarator and the
10000 the decl-specifiers. */
10001 perform_deferred_access_checks ();
10003 /* Restore the saved value. */
10004 if (TREE_CODE (decl) == FUNCTION_DECL)
10005 current_function_decl = saved_current_function_decl;
10008 /* Parse the initializer. */
10009 if (is_initialized)
10010 initializer = cp_parser_initializer (parser,
10011 &is_parenthesized_init,
10012 &is_non_constant_init);
10015 initializer = NULL_TREE;
10016 is_parenthesized_init = false;
10017 is_non_constant_init = true;
10020 /* The old parser allows attributes to appear after a parenthesized
10021 initializer. Mark Mitchell proposed removing this functionality
10022 on the GCC mailing lists on 2002-08-13. This parser accepts the
10023 attributes -- but ignores them. */
10024 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10025 if (cp_parser_attributes_opt (parser))
10026 warning ("attributes after parenthesized initializer ignored");
10028 /* Leave the SCOPE, now that we have processed the initializer. It
10029 is important to do this before calling cp_finish_decl because it
10030 makes decisions about whether to create DECL_STMTs or not based
10031 on the current scope. */
10035 /* For an in-class declaration, use `grokfield' to create the
10039 decl = grokfield (declarator, decl_specifiers,
10040 initializer, /*asmspec=*/NULL_TREE,
10041 /*attributes=*/NULL_TREE);
10042 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10043 cp_parser_save_default_args (parser, decl);
10046 /* Finish processing the declaration. But, skip friend
10048 if (!friend_p && decl)
10049 cp_finish_decl (decl,
10052 /* If the initializer is in parentheses, then this is
10053 a direct-initialization, which means that an
10054 `explicit' constructor is OK. Otherwise, an
10055 `explicit' constructor cannot be used. */
10056 ((is_parenthesized_init || !is_initialized)
10057 ? 0 : LOOKUP_ONLYCONVERTING));
10059 /* Remember whether or not variables were initialized by
10060 constant-expressions. */
10061 if (decl && TREE_CODE (decl) == VAR_DECL
10062 && is_initialized && !is_non_constant_init)
10063 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10068 /* Parse a declarator.
10072 ptr-operator declarator
10074 abstract-declarator:
10075 ptr-operator abstract-declarator [opt]
10076 direct-abstract-declarator
10081 attributes [opt] direct-declarator
10082 attributes [opt] ptr-operator declarator
10084 abstract-declarator:
10085 attributes [opt] ptr-operator abstract-declarator [opt]
10086 attributes [opt] direct-abstract-declarator
10088 Returns a representation of the declarator. If the declarator has
10089 the form `* declarator', then an INDIRECT_REF is returned, whose
10090 only operand is the sub-declarator. Analogously, `& declarator' is
10091 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10092 used. The first operand is the TYPE for `X'. The second operand
10093 is an INDIRECT_REF whose operand is the sub-declarator.
10095 Otherwise, the representation is as for a direct-declarator.
10097 (It would be better to define a structure type to represent
10098 declarators, rather than abusing `tree' nodes to represent
10099 declarators. That would be much clearer and save some memory.
10100 There is no reason for declarators to be garbage-collected, for
10101 example; they are created during parser and no longer needed after
10102 `grokdeclarator' has been called.)
10104 For a ptr-operator that has the optional cv-qualifier-seq,
10105 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10108 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10109 detect constructor, destructor or conversion operators. It is set
10110 to -1 if the declarator is a name, and +1 if it is a
10111 function. Otherwise it is set to zero. Usually you just want to
10112 test for >0, but internally the negative value is used.
10114 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10115 a decl-specifier-seq unless it declares a constructor, destructor,
10116 or conversion. It might seem that we could check this condition in
10117 semantic analysis, rather than parsing, but that makes it difficult
10118 to handle something like `f()'. We want to notice that there are
10119 no decl-specifiers, and therefore realize that this is an
10120 expression, not a declaration.)
10122 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10123 the declarator is a direct-declarator of the form "(...)". */
10126 cp_parser_declarator (cp_parser* parser,
10127 cp_parser_declarator_kind dcl_kind,
10128 int* ctor_dtor_or_conv_p,
10129 bool* parenthesized_p)
10133 enum tree_code code;
10134 tree cv_qualifier_seq;
10136 tree attributes = NULL_TREE;
10138 /* Assume this is not a constructor, destructor, or type-conversion
10140 if (ctor_dtor_or_conv_p)
10141 *ctor_dtor_or_conv_p = 0;
10143 if (cp_parser_allow_gnu_extensions_p (parser))
10144 attributes = cp_parser_attributes_opt (parser);
10146 /* Peek at the next token. */
10147 token = cp_lexer_peek_token (parser->lexer);
10149 /* Check for the ptr-operator production. */
10150 cp_parser_parse_tentatively (parser);
10151 /* Parse the ptr-operator. */
10152 code = cp_parser_ptr_operator (parser,
10154 &cv_qualifier_seq);
10155 /* If that worked, then we have a ptr-operator. */
10156 if (cp_parser_parse_definitely (parser))
10158 /* If a ptr-operator was found, then this declarator was not
10160 if (parenthesized_p)
10161 *parenthesized_p = true;
10162 /* The dependent declarator is optional if we are parsing an
10163 abstract-declarator. */
10164 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10165 cp_parser_parse_tentatively (parser);
10167 /* Parse the dependent declarator. */
10168 declarator = cp_parser_declarator (parser, dcl_kind,
10169 /*ctor_dtor_or_conv_p=*/NULL,
10170 /*parenthesized_p=*/NULL);
10172 /* If we are parsing an abstract-declarator, we must handle the
10173 case where the dependent declarator is absent. */
10174 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10175 && !cp_parser_parse_definitely (parser))
10176 declarator = NULL_TREE;
10178 /* Build the representation of the ptr-operator. */
10179 if (code == INDIRECT_REF)
10180 declarator = make_pointer_declarator (cv_qualifier_seq,
10183 declarator = make_reference_declarator (cv_qualifier_seq,
10185 /* Handle the pointer-to-member case. */
10187 declarator = build_nt (SCOPE_REF, class_type, declarator);
10189 /* Everything else is a direct-declarator. */
10192 if (parenthesized_p)
10193 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10195 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10196 ctor_dtor_or_conv_p);
10199 if (attributes && declarator != error_mark_node)
10200 declarator = tree_cons (attributes, declarator, NULL_TREE);
10205 /* Parse a direct-declarator or direct-abstract-declarator.
10209 direct-declarator ( parameter-declaration-clause )
10210 cv-qualifier-seq [opt]
10211 exception-specification [opt]
10212 direct-declarator [ constant-expression [opt] ]
10215 direct-abstract-declarator:
10216 direct-abstract-declarator [opt]
10217 ( parameter-declaration-clause )
10218 cv-qualifier-seq [opt]
10219 exception-specification [opt]
10220 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10221 ( abstract-declarator )
10223 Returns a representation of the declarator. DCL_KIND is
10224 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10225 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10226 we are parsing a direct-declarator. It is
10227 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10228 of ambiguity we prefer an abstract declarator, as per
10229 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10230 cp_parser_declarator.
10232 For the declarator-id production, the representation is as for an
10233 id-expression, except that a qualified name is represented as a
10234 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10235 see the documentation of the FUNCTION_DECLARATOR_* macros for
10236 information about how to find the various declarator components.
10237 An array-declarator is represented as an ARRAY_REF. The
10238 direct-declarator is the first operand; the constant-expression
10239 indicating the size of the array is the second operand. */
10242 cp_parser_direct_declarator (cp_parser* parser,
10243 cp_parser_declarator_kind dcl_kind,
10244 int* ctor_dtor_or_conv_p)
10247 tree declarator = NULL_TREE;
10248 tree scope = NULL_TREE;
10249 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10250 bool saved_in_declarator_p = parser->in_declarator_p;
10255 /* Peek at the next token. */
10256 token = cp_lexer_peek_token (parser->lexer);
10257 if (token->type == CPP_OPEN_PAREN)
10259 /* This is either a parameter-declaration-clause, or a
10260 parenthesized declarator. When we know we are parsing a
10261 named declarator, it must be a parenthesized declarator
10262 if FIRST is true. For instance, `(int)' is a
10263 parameter-declaration-clause, with an omitted
10264 direct-abstract-declarator. But `((*))', is a
10265 parenthesized abstract declarator. Finally, when T is a
10266 template parameter `(T)' is a
10267 parameter-declaration-clause, and not a parenthesized
10270 We first try and parse a parameter-declaration-clause,
10271 and then try a nested declarator (if FIRST is true).
10273 It is not an error for it not to be a
10274 parameter-declaration-clause, even when FIRST is
10280 The first is the declaration of a function while the
10281 second is a the definition of a variable, including its
10284 Having seen only the parenthesis, we cannot know which of
10285 these two alternatives should be selected. Even more
10286 complex are examples like:
10291 The former is a function-declaration; the latter is a
10292 variable initialization.
10294 Thus again, we try a parameter-declaration-clause, and if
10295 that fails, we back out and return. */
10297 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10300 unsigned saved_num_template_parameter_lists;
10302 cp_parser_parse_tentatively (parser);
10304 /* Consume the `('. */
10305 cp_lexer_consume_token (parser->lexer);
10308 /* If this is going to be an abstract declarator, we're
10309 in a declarator and we can't have default args. */
10310 parser->default_arg_ok_p = false;
10311 parser->in_declarator_p = true;
10314 /* Inside the function parameter list, surrounding
10315 template-parameter-lists do not apply. */
10316 saved_num_template_parameter_lists
10317 = parser->num_template_parameter_lists;
10318 parser->num_template_parameter_lists = 0;
10320 /* Parse the parameter-declaration-clause. */
10321 params = cp_parser_parameter_declaration_clause (parser);
10323 parser->num_template_parameter_lists
10324 = saved_num_template_parameter_lists;
10326 /* If all went well, parse the cv-qualifier-seq and the
10327 exception-specification. */
10328 if (cp_parser_parse_definitely (parser))
10330 tree cv_qualifiers;
10331 tree exception_specification;
10333 if (ctor_dtor_or_conv_p)
10334 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10336 /* Consume the `)'. */
10337 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10339 /* Parse the cv-qualifier-seq. */
10340 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10341 /* And the exception-specification. */
10342 exception_specification
10343 = cp_parser_exception_specification_opt (parser);
10345 /* Create the function-declarator. */
10346 declarator = make_call_declarator (declarator,
10349 exception_specification);
10350 /* Any subsequent parameter lists are to do with
10351 return type, so are not those of the declared
10353 parser->default_arg_ok_p = false;
10355 /* Repeat the main loop. */
10360 /* If this is the first, we can try a parenthesized
10364 bool saved_in_type_id_in_expr_p;
10366 parser->default_arg_ok_p = saved_default_arg_ok_p;
10367 parser->in_declarator_p = saved_in_declarator_p;
10369 /* Consume the `('. */
10370 cp_lexer_consume_token (parser->lexer);
10371 /* Parse the nested declarator. */
10372 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10373 parser->in_type_id_in_expr_p = true;
10375 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10376 /*parenthesized_p=*/NULL);
10377 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10379 /* Expect a `)'. */
10380 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10381 declarator = error_mark_node;
10382 if (declarator == error_mark_node)
10385 goto handle_declarator;
10387 /* Otherwise, we must be done. */
10391 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10392 && token->type == CPP_OPEN_SQUARE)
10394 /* Parse an array-declarator. */
10397 if (ctor_dtor_or_conv_p)
10398 *ctor_dtor_or_conv_p = 0;
10401 parser->default_arg_ok_p = false;
10402 parser->in_declarator_p = true;
10403 /* Consume the `['. */
10404 cp_lexer_consume_token (parser->lexer);
10405 /* Peek at the next token. */
10406 token = cp_lexer_peek_token (parser->lexer);
10407 /* If the next token is `]', then there is no
10408 constant-expression. */
10409 if (token->type != CPP_CLOSE_SQUARE)
10411 bool non_constant_p;
10414 = cp_parser_constant_expression (parser,
10415 /*allow_non_constant=*/true,
10417 if (!non_constant_p)
10418 bounds = fold_non_dependent_expr (bounds);
10421 bounds = NULL_TREE;
10422 /* Look for the closing `]'. */
10423 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10425 declarator = error_mark_node;
10429 declarator = build_nt (ARRAY_REF, declarator, bounds);
10431 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10433 /* Parse a declarator-id */
10434 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10435 cp_parser_parse_tentatively (parser);
10436 declarator = cp_parser_declarator_id (parser);
10437 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10439 if (!cp_parser_parse_definitely (parser))
10440 declarator = error_mark_node;
10441 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10443 cp_parser_error (parser, "expected unqualified-id");
10444 declarator = error_mark_node;
10448 if (declarator == error_mark_node)
10451 if (TREE_CODE (declarator) == SCOPE_REF
10452 && !current_scope ())
10454 tree scope = TREE_OPERAND (declarator, 0);
10456 /* In the declaration of a member of a template class
10457 outside of the class itself, the SCOPE will sometimes
10458 be a TYPENAME_TYPE. For example, given:
10460 template <typename T>
10461 int S<T>::R::i = 3;
10463 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10464 this context, we must resolve S<T>::R to an ordinary
10465 type, rather than a typename type.
10467 The reason we normally avoid resolving TYPENAME_TYPEs
10468 is that a specialization of `S' might render
10469 `S<T>::R' not a type. However, if `S' is
10470 specialized, then this `i' will not be used, so there
10471 is no harm in resolving the types here. */
10472 if (TREE_CODE (scope) == TYPENAME_TYPE)
10476 /* Resolve the TYPENAME_TYPE. */
10477 type = resolve_typename_type (scope,
10478 /*only_current_p=*/false);
10479 /* If that failed, the declarator is invalid. */
10480 if (type != error_mark_node)
10482 /* Build a new DECLARATOR. */
10483 declarator = build_nt (SCOPE_REF,
10485 TREE_OPERAND (declarator, 1));
10489 /* Check to see whether the declarator-id names a constructor,
10490 destructor, or conversion. */
10491 if (declarator && ctor_dtor_or_conv_p
10492 && ((TREE_CODE (declarator) == SCOPE_REF
10493 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10494 || (TREE_CODE (declarator) != SCOPE_REF
10495 && at_class_scope_p ())))
10497 tree unqualified_name;
10500 /* Get the unqualified part of the name. */
10501 if (TREE_CODE (declarator) == SCOPE_REF)
10503 class_type = TREE_OPERAND (declarator, 0);
10504 unqualified_name = TREE_OPERAND (declarator, 1);
10508 class_type = current_class_type;
10509 unqualified_name = declarator;
10512 /* See if it names ctor, dtor or conv. */
10513 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10514 || IDENTIFIER_TYPENAME_P (unqualified_name)
10515 || constructor_name_p (unqualified_name, class_type))
10516 *ctor_dtor_or_conv_p = -1;
10519 handle_declarator:;
10520 scope = get_scope_of_declarator (declarator);
10522 /* Any names that appear after the declarator-id for a member
10523 are looked up in the containing scope. */
10524 push_scope (scope);
10525 parser->in_declarator_p = true;
10526 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10528 && (TREE_CODE (declarator) == SCOPE_REF
10529 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10530 /* Default args are only allowed on function
10532 parser->default_arg_ok_p = saved_default_arg_ok_p;
10534 parser->default_arg_ok_p = false;
10543 /* For an abstract declarator, we might wind up with nothing at this
10544 point. That's an error; the declarator is not optional. */
10546 cp_parser_error (parser, "expected declarator");
10548 /* If we entered a scope, we must exit it now. */
10552 parser->default_arg_ok_p = saved_default_arg_ok_p;
10553 parser->in_declarator_p = saved_in_declarator_p;
10558 /* Parse a ptr-operator.
10561 * cv-qualifier-seq [opt]
10563 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10568 & cv-qualifier-seq [opt]
10570 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10571 used. Returns ADDR_EXPR if a reference was used. In the
10572 case of a pointer-to-member, *TYPE is filled in with the
10573 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10574 with the cv-qualifier-seq, or NULL_TREE, if there are no
10575 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10577 static enum tree_code
10578 cp_parser_ptr_operator (cp_parser* parser,
10580 tree* cv_qualifier_seq)
10582 enum tree_code code = ERROR_MARK;
10585 /* Assume that it's not a pointer-to-member. */
10587 /* And that there are no cv-qualifiers. */
10588 *cv_qualifier_seq = NULL_TREE;
10590 /* Peek at the next token. */
10591 token = cp_lexer_peek_token (parser->lexer);
10592 /* If it's a `*' or `&' we have a pointer or reference. */
10593 if (token->type == CPP_MULT || token->type == CPP_AND)
10595 /* Remember which ptr-operator we were processing. */
10596 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10598 /* Consume the `*' or `&'. */
10599 cp_lexer_consume_token (parser->lexer);
10601 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10602 `&', if we are allowing GNU extensions. (The only qualifier
10603 that can legally appear after `&' is `restrict', but that is
10604 enforced during semantic analysis. */
10605 if (code == INDIRECT_REF
10606 || cp_parser_allow_gnu_extensions_p (parser))
10607 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10611 /* Try the pointer-to-member case. */
10612 cp_parser_parse_tentatively (parser);
10613 /* Look for the optional `::' operator. */
10614 cp_parser_global_scope_opt (parser,
10615 /*current_scope_valid_p=*/false);
10616 /* Look for the nested-name specifier. */
10617 cp_parser_nested_name_specifier (parser,
10618 /*typename_keyword_p=*/false,
10619 /*check_dependency_p=*/true,
10621 /*is_declaration=*/false);
10622 /* If we found it, and the next token is a `*', then we are
10623 indeed looking at a pointer-to-member operator. */
10624 if (!cp_parser_error_occurred (parser)
10625 && cp_parser_require (parser, CPP_MULT, "`*'"))
10627 /* The type of which the member is a member is given by the
10629 *type = parser->scope;
10630 /* The next name will not be qualified. */
10631 parser->scope = NULL_TREE;
10632 parser->qualifying_scope = NULL_TREE;
10633 parser->object_scope = NULL_TREE;
10634 /* Indicate that the `*' operator was used. */
10635 code = INDIRECT_REF;
10636 /* Look for the optional cv-qualifier-seq. */
10637 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10639 /* If that didn't work we don't have a ptr-operator. */
10640 if (!cp_parser_parse_definitely (parser))
10641 cp_parser_error (parser, "expected ptr-operator");
10647 /* Parse an (optional) cv-qualifier-seq.
10650 cv-qualifier cv-qualifier-seq [opt]
10652 Returns a TREE_LIST. The TREE_VALUE of each node is the
10653 representation of a cv-qualifier. */
10656 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10658 tree cv_qualifiers = NULL_TREE;
10664 /* Look for the next cv-qualifier. */
10665 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10666 /* If we didn't find one, we're done. */
10670 /* Add this cv-qualifier to the list. */
10672 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10675 /* We built up the list in reverse order. */
10676 return nreverse (cv_qualifiers);
10679 /* Parse an (optional) cv-qualifier.
10691 cp_parser_cv_qualifier_opt (cp_parser* parser)
10694 tree cv_qualifier = NULL_TREE;
10696 /* Peek at the next token. */
10697 token = cp_lexer_peek_token (parser->lexer);
10698 /* See if it's a cv-qualifier. */
10699 switch (token->keyword)
10704 /* Save the value of the token. */
10705 cv_qualifier = token->value;
10706 /* Consume the token. */
10707 cp_lexer_consume_token (parser->lexer);
10714 return cv_qualifier;
10717 /* Parse a declarator-id.
10721 :: [opt] nested-name-specifier [opt] type-name
10723 In the `id-expression' case, the value returned is as for
10724 cp_parser_id_expression if the id-expression was an unqualified-id.
10725 If the id-expression was a qualified-id, then a SCOPE_REF is
10726 returned. The first operand is the scope (either a NAMESPACE_DECL
10727 or TREE_TYPE), but the second is still just a representation of an
10731 cp_parser_declarator_id (cp_parser* parser)
10733 tree id_expression;
10735 /* The expression must be an id-expression. Assume that qualified
10736 names are the names of types so that:
10739 int S<T>::R::i = 3;
10741 will work; we must treat `S<T>::R' as the name of a type.
10742 Similarly, assume that qualified names are templates, where
10746 int S<T>::R<T>::i = 3;
10749 id_expression = cp_parser_id_expression (parser,
10750 /*template_keyword_p=*/false,
10751 /*check_dependency_p=*/false,
10752 /*template_p=*/NULL,
10753 /*declarator_p=*/true);
10754 /* If the name was qualified, create a SCOPE_REF to represent
10758 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10759 parser->scope = NULL_TREE;
10762 return id_expression;
10765 /* Parse a type-id.
10768 type-specifier-seq abstract-declarator [opt]
10770 Returns the TYPE specified. */
10773 cp_parser_type_id (cp_parser* parser)
10775 tree type_specifier_seq;
10776 tree abstract_declarator;
10778 /* Parse the type-specifier-seq. */
10780 = cp_parser_type_specifier_seq (parser);
10781 if (type_specifier_seq == error_mark_node)
10782 return error_mark_node;
10784 /* There might or might not be an abstract declarator. */
10785 cp_parser_parse_tentatively (parser);
10786 /* Look for the declarator. */
10787 abstract_declarator
10788 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10789 /*parenthesized_p=*/NULL);
10790 /* Check to see if there really was a declarator. */
10791 if (!cp_parser_parse_definitely (parser))
10792 abstract_declarator = NULL_TREE;
10794 return groktypename (build_tree_list (type_specifier_seq,
10795 abstract_declarator));
10798 /* Parse a type-specifier-seq.
10800 type-specifier-seq:
10801 type-specifier type-specifier-seq [opt]
10805 type-specifier-seq:
10806 attributes type-specifier-seq [opt]
10808 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10809 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10812 cp_parser_type_specifier_seq (cp_parser* parser)
10814 bool seen_type_specifier = false;
10815 tree type_specifier_seq = NULL_TREE;
10817 /* Parse the type-specifiers and attributes. */
10820 tree type_specifier;
10822 /* Check for attributes first. */
10823 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10825 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10827 type_specifier_seq);
10831 /* After the first type-specifier, others are optional. */
10832 if (seen_type_specifier)
10833 cp_parser_parse_tentatively (parser);
10834 /* Look for the type-specifier. */
10835 type_specifier = cp_parser_type_specifier (parser,
10836 CP_PARSER_FLAGS_NONE,
10837 /*is_friend=*/false,
10838 /*is_declaration=*/false,
10841 /* If the first type-specifier could not be found, this is not a
10842 type-specifier-seq at all. */
10843 if (!seen_type_specifier && type_specifier == error_mark_node)
10844 return error_mark_node;
10845 /* If subsequent type-specifiers could not be found, the
10846 type-specifier-seq is complete. */
10847 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10850 /* Add the new type-specifier to the list. */
10852 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10853 seen_type_specifier = true;
10856 /* We built up the list in reverse order. */
10857 return nreverse (type_specifier_seq);
10860 /* Parse a parameter-declaration-clause.
10862 parameter-declaration-clause:
10863 parameter-declaration-list [opt] ... [opt]
10864 parameter-declaration-list , ...
10866 Returns a representation for the parameter declarations. Each node
10867 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10868 representation.) If the parameter-declaration-clause ends with an
10869 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10870 list. A return value of NULL_TREE indicates a
10871 parameter-declaration-clause consisting only of an ellipsis. */
10874 cp_parser_parameter_declaration_clause (cp_parser* parser)
10880 /* Peek at the next token. */
10881 token = cp_lexer_peek_token (parser->lexer);
10882 /* Check for trivial parameter-declaration-clauses. */
10883 if (token->type == CPP_ELLIPSIS)
10885 /* Consume the `...' token. */
10886 cp_lexer_consume_token (parser->lexer);
10889 else if (token->type == CPP_CLOSE_PAREN)
10890 /* There are no parameters. */
10892 #ifndef NO_IMPLICIT_EXTERN_C
10893 if (in_system_header && current_class_type == NULL
10894 && current_lang_name == lang_name_c)
10898 return void_list_node;
10900 /* Check for `(void)', too, which is a special case. */
10901 else if (token->keyword == RID_VOID
10902 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10903 == CPP_CLOSE_PAREN))
10905 /* Consume the `void' token. */
10906 cp_lexer_consume_token (parser->lexer);
10907 /* There are no parameters. */
10908 return void_list_node;
10911 /* Parse the parameter-declaration-list. */
10912 parameters = cp_parser_parameter_declaration_list (parser);
10913 /* If a parse error occurred while parsing the
10914 parameter-declaration-list, then the entire
10915 parameter-declaration-clause is erroneous. */
10916 if (parameters == error_mark_node)
10917 return error_mark_node;
10919 /* Peek at the next token. */
10920 token = cp_lexer_peek_token (parser->lexer);
10921 /* If it's a `,', the clause should terminate with an ellipsis. */
10922 if (token->type == CPP_COMMA)
10924 /* Consume the `,'. */
10925 cp_lexer_consume_token (parser->lexer);
10926 /* Expect an ellipsis. */
10928 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10930 /* It might also be `...' if the optional trailing `,' was
10932 else if (token->type == CPP_ELLIPSIS)
10934 /* Consume the `...' token. */
10935 cp_lexer_consume_token (parser->lexer);
10936 /* And remember that we saw it. */
10940 ellipsis_p = false;
10942 /* Finish the parameter list. */
10943 return finish_parmlist (parameters, ellipsis_p);
10946 /* Parse a parameter-declaration-list.
10948 parameter-declaration-list:
10949 parameter-declaration
10950 parameter-declaration-list , parameter-declaration
10952 Returns a representation of the parameter-declaration-list, as for
10953 cp_parser_parameter_declaration_clause. However, the
10954 `void_list_node' is never appended to the list. */
10957 cp_parser_parameter_declaration_list (cp_parser* parser)
10959 tree parameters = NULL_TREE;
10961 /* Look for more parameters. */
10965 bool parenthesized_p;
10966 /* Parse the parameter. */
10968 = cp_parser_parameter_declaration (parser,
10969 /*template_parm_p=*/false,
10972 /* If a parse error occurred parsing the parameter declaration,
10973 then the entire parameter-declaration-list is erroneous. */
10974 if (parameter == error_mark_node)
10976 parameters = error_mark_node;
10979 /* Add the new parameter to the list. */
10980 TREE_CHAIN (parameter) = parameters;
10981 parameters = parameter;
10983 /* Peek at the next token. */
10984 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10985 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10986 /* The parameter-declaration-list is complete. */
10988 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10992 /* Peek at the next token. */
10993 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10994 /* If it's an ellipsis, then the list is complete. */
10995 if (token->type == CPP_ELLIPSIS)
10997 /* Otherwise, there must be more parameters. Consume the
10999 cp_lexer_consume_token (parser->lexer);
11000 /* When parsing something like:
11002 int i(float f, double d)
11004 we can tell after seeing the declaration for "f" that we
11005 are not looking at an initialization of a variable "i",
11006 but rather at the declaration of a function "i".
11008 Due to the fact that the parsing of template arguments
11009 (as specified to a template-id) requires backtracking we
11010 cannot use this technique when inside a template argument
11012 if (!parser->in_template_argument_list_p
11013 && cp_parser_parsing_tentatively (parser)
11014 && !cp_parser_committed_to_tentative_parse (parser)
11015 /* However, a parameter-declaration of the form
11016 "foat(f)" (which is a valid declaration of a
11017 parameter "f") can also be interpreted as an
11018 expression (the conversion of "f" to "float"). */
11019 && !parenthesized_p)
11020 cp_parser_commit_to_tentative_parse (parser);
11024 cp_parser_error (parser, "expected `,' or `...'");
11025 if (!cp_parser_parsing_tentatively (parser)
11026 || cp_parser_committed_to_tentative_parse (parser))
11027 cp_parser_skip_to_closing_parenthesis (parser,
11028 /*recovering=*/true,
11029 /*or_comma=*/false,
11030 /*consume_paren=*/false);
11035 /* We built up the list in reverse order; straighten it out now. */
11036 return nreverse (parameters);
11039 /* Parse a parameter declaration.
11041 parameter-declaration:
11042 decl-specifier-seq declarator
11043 decl-specifier-seq declarator = assignment-expression
11044 decl-specifier-seq abstract-declarator [opt]
11045 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11047 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11048 declares a template parameter. (In that case, a non-nested `>'
11049 token encountered during the parsing of the assignment-expression
11050 is not interpreted as a greater-than operator.)
11052 Returns a TREE_LIST representing the parameter-declaration. The
11053 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11054 there is no default argument. The TREE_VALUE is a representation
11055 of the decl-specifier-seq and declarator. In particular, the
11056 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11057 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11058 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11059 the declarator is of the form "(p)". */
11062 cp_parser_parameter_declaration (cp_parser *parser,
11063 bool template_parm_p,
11064 bool *parenthesized_p)
11066 int declares_class_or_enum;
11067 bool greater_than_is_operator_p;
11068 tree decl_specifiers;
11071 tree default_argument;
11074 const char *saved_message;
11076 /* In a template parameter, `>' is not an operator.
11080 When parsing a default template-argument for a non-type
11081 template-parameter, the first non-nested `>' is taken as the end
11082 of the template parameter-list rather than a greater-than
11084 greater_than_is_operator_p = !template_parm_p;
11086 /* Type definitions may not appear in parameter types. */
11087 saved_message = parser->type_definition_forbidden_message;
11088 parser->type_definition_forbidden_message
11089 = "types may not be defined in parameter types";
11091 /* Parse the declaration-specifiers. */
11093 = cp_parser_decl_specifier_seq (parser,
11094 CP_PARSER_FLAGS_NONE,
11096 &declares_class_or_enum);
11097 /* If an error occurred, there's no reason to attempt to parse the
11098 rest of the declaration. */
11099 if (cp_parser_error_occurred (parser))
11101 parser->type_definition_forbidden_message = saved_message;
11102 return error_mark_node;
11105 /* Peek at the next token. */
11106 token = cp_lexer_peek_token (parser->lexer);
11107 /* If the next token is a `)', `,', `=', `>', or `...', then there
11108 is no declarator. */
11109 if (token->type == CPP_CLOSE_PAREN
11110 || token->type == CPP_COMMA
11111 || token->type == CPP_EQ
11112 || token->type == CPP_ELLIPSIS
11113 || token->type == CPP_GREATER)
11115 declarator = NULL_TREE;
11116 if (parenthesized_p)
11117 *parenthesized_p = false;
11119 /* Otherwise, there should be a declarator. */
11122 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11123 parser->default_arg_ok_p = false;
11125 /* After seeing a decl-specifier-seq, if the next token is not a
11126 "(", there is no possibility that the code is a valid
11127 expression. Therefore, if parsing tentatively, we commit at
11129 if (!parser->in_template_argument_list_p
11130 /* In an expression context, having seen:
11134 we cannot be sure whether we are looking at a
11135 function-type (taking a "char" as a parameter) or a cast
11136 of some object of type "char" to "int". */
11137 && !parser->in_type_id_in_expr_p
11138 && cp_parser_parsing_tentatively (parser)
11139 && !cp_parser_committed_to_tentative_parse (parser)
11140 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11141 cp_parser_commit_to_tentative_parse (parser);
11142 /* Parse the declarator. */
11143 declarator = cp_parser_declarator (parser,
11144 CP_PARSER_DECLARATOR_EITHER,
11145 /*ctor_dtor_or_conv_p=*/NULL,
11147 parser->default_arg_ok_p = saved_default_arg_ok_p;
11148 /* After the declarator, allow more attributes. */
11149 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11152 /* The restriction on defining new types applies only to the type
11153 of the parameter, not to the default argument. */
11154 parser->type_definition_forbidden_message = saved_message;
11156 /* If the next token is `=', then process a default argument. */
11157 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11159 bool saved_greater_than_is_operator_p;
11160 /* Consume the `='. */
11161 cp_lexer_consume_token (parser->lexer);
11163 /* If we are defining a class, then the tokens that make up the
11164 default argument must be saved and processed later. */
11165 if (!template_parm_p && at_class_scope_p ()
11166 && TYPE_BEING_DEFINED (current_class_type))
11168 unsigned depth = 0;
11170 /* Create a DEFAULT_ARG to represented the unparsed default
11172 default_argument = make_node (DEFAULT_ARG);
11173 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11175 /* Add tokens until we have processed the entire default
11182 /* Peek at the next token. */
11183 token = cp_lexer_peek_token (parser->lexer);
11184 /* What we do depends on what token we have. */
11185 switch (token->type)
11187 /* In valid code, a default argument must be
11188 immediately followed by a `,' `)', or `...'. */
11190 case CPP_CLOSE_PAREN:
11192 /* If we run into a non-nested `;', `}', or `]',
11193 then the code is invalid -- but the default
11194 argument is certainly over. */
11195 case CPP_SEMICOLON:
11196 case CPP_CLOSE_BRACE:
11197 case CPP_CLOSE_SQUARE:
11200 /* Update DEPTH, if necessary. */
11201 else if (token->type == CPP_CLOSE_PAREN
11202 || token->type == CPP_CLOSE_BRACE
11203 || token->type == CPP_CLOSE_SQUARE)
11207 case CPP_OPEN_PAREN:
11208 case CPP_OPEN_SQUARE:
11209 case CPP_OPEN_BRACE:
11214 /* If we see a non-nested `>', and `>' is not an
11215 operator, then it marks the end of the default
11217 if (!depth && !greater_than_is_operator_p)
11221 /* If we run out of tokens, issue an error message. */
11223 error ("file ends in default argument");
11229 /* In these cases, we should look for template-ids.
11230 For example, if the default argument is
11231 `X<int, double>()', we need to do name lookup to
11232 figure out whether or not `X' is a template; if
11233 so, the `,' does not end the default argument.
11235 That is not yet done. */
11242 /* If we've reached the end, stop. */
11246 /* Add the token to the token block. */
11247 token = cp_lexer_consume_token (parser->lexer);
11248 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11252 /* Outside of a class definition, we can just parse the
11253 assignment-expression. */
11256 bool saved_local_variables_forbidden_p;
11258 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11260 saved_greater_than_is_operator_p
11261 = parser->greater_than_is_operator_p;
11262 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11263 /* Local variable names (and the `this' keyword) may not
11264 appear in a default argument. */
11265 saved_local_variables_forbidden_p
11266 = parser->local_variables_forbidden_p;
11267 parser->local_variables_forbidden_p = true;
11268 /* Parse the assignment-expression. */
11269 default_argument = cp_parser_assignment_expression (parser);
11270 /* Restore saved state. */
11271 parser->greater_than_is_operator_p
11272 = saved_greater_than_is_operator_p;
11273 parser->local_variables_forbidden_p
11274 = saved_local_variables_forbidden_p;
11276 if (!parser->default_arg_ok_p)
11278 if (!flag_pedantic_errors)
11279 warning ("deprecated use of default argument for parameter of non-function");
11282 error ("default arguments are only permitted for function parameters");
11283 default_argument = NULL_TREE;
11288 default_argument = NULL_TREE;
11290 /* Create the representation of the parameter. */
11292 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11293 parameter = build_tree_list (default_argument,
11294 build_tree_list (decl_specifiers,
11300 /* Parse a function-body.
11303 compound_statement */
11306 cp_parser_function_body (cp_parser *parser)
11308 cp_parser_compound_statement (parser, false);
11311 /* Parse a ctor-initializer-opt followed by a function-body. Return
11312 true if a ctor-initializer was present. */
11315 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11318 bool ctor_initializer_p;
11320 /* Begin the function body. */
11321 body = begin_function_body ();
11322 /* Parse the optional ctor-initializer. */
11323 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11324 /* Parse the function-body. */
11325 cp_parser_function_body (parser);
11326 /* Finish the function body. */
11327 finish_function_body (body);
11329 return ctor_initializer_p;
11332 /* Parse an initializer.
11335 = initializer-clause
11336 ( expression-list )
11338 Returns a expression representing the initializer. If no
11339 initializer is present, NULL_TREE is returned.
11341 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11342 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11343 set to FALSE if there is no initializer present. If there is an
11344 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11345 is set to true; otherwise it is set to false. */
11348 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11349 bool* non_constant_p)
11354 /* Peek at the next token. */
11355 token = cp_lexer_peek_token (parser->lexer);
11357 /* Let our caller know whether or not this initializer was
11359 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11360 /* Assume that the initializer is constant. */
11361 *non_constant_p = false;
11363 if (token->type == CPP_EQ)
11365 /* Consume the `='. */
11366 cp_lexer_consume_token (parser->lexer);
11367 /* Parse the initializer-clause. */
11368 init = cp_parser_initializer_clause (parser, non_constant_p);
11370 else if (token->type == CPP_OPEN_PAREN)
11371 init = cp_parser_parenthesized_expression_list (parser, false,
11375 /* Anything else is an error. */
11376 cp_parser_error (parser, "expected initializer");
11377 init = error_mark_node;
11383 /* Parse an initializer-clause.
11385 initializer-clause:
11386 assignment-expression
11387 { initializer-list , [opt] }
11390 Returns an expression representing the initializer.
11392 If the `assignment-expression' production is used the value
11393 returned is simply a representation for the expression.
11395 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11396 the elements of the initializer-list (or NULL_TREE, if the last
11397 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11398 NULL_TREE. There is no way to detect whether or not the optional
11399 trailing `,' was provided. NON_CONSTANT_P is as for
11400 cp_parser_initializer. */
11403 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11407 /* If it is not a `{', then we are looking at an
11408 assignment-expression. */
11409 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11411 = cp_parser_constant_expression (parser,
11412 /*allow_non_constant_p=*/true,
11416 /* Consume the `{' token. */
11417 cp_lexer_consume_token (parser->lexer);
11418 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11419 initializer = make_node (CONSTRUCTOR);
11420 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11421 necessary, but check_initializer depends upon it, for
11423 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11424 /* If it's not a `}', then there is a non-trivial initializer. */
11425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11427 /* Parse the initializer list. */
11428 CONSTRUCTOR_ELTS (initializer)
11429 = cp_parser_initializer_list (parser, non_constant_p);
11430 /* A trailing `,' token is allowed. */
11431 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11432 cp_lexer_consume_token (parser->lexer);
11434 /* Now, there should be a trailing `}'. */
11435 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11438 return initializer;
11441 /* Parse an initializer-list.
11445 initializer-list , initializer-clause
11450 identifier : initializer-clause
11451 initializer-list, identifier : initializer-clause
11453 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11454 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11455 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11456 as for cp_parser_initializer. */
11459 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11461 tree initializers = NULL_TREE;
11463 /* Assume all of the expressions are constant. */
11464 *non_constant_p = false;
11466 /* Parse the rest of the list. */
11472 bool clause_non_constant_p;
11474 /* If the next token is an identifier and the following one is a
11475 colon, we are looking at the GNU designated-initializer
11477 if (cp_parser_allow_gnu_extensions_p (parser)
11478 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11479 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11481 /* Consume the identifier. */
11482 identifier = cp_lexer_consume_token (parser->lexer)->value;
11483 /* Consume the `:'. */
11484 cp_lexer_consume_token (parser->lexer);
11487 identifier = NULL_TREE;
11489 /* Parse the initializer. */
11490 initializer = cp_parser_initializer_clause (parser,
11491 &clause_non_constant_p);
11492 /* If any clause is non-constant, so is the entire initializer. */
11493 if (clause_non_constant_p)
11494 *non_constant_p = true;
11495 /* Add it to the list. */
11496 initializers = tree_cons (identifier, initializer, initializers);
11498 /* If the next token is not a comma, we have reached the end of
11500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11503 /* Peek at the next token. */
11504 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11505 /* If the next token is a `}', then we're still done. An
11506 initializer-clause can have a trailing `,' after the
11507 initializer-list and before the closing `}'. */
11508 if (token->type == CPP_CLOSE_BRACE)
11511 /* Consume the `,' token. */
11512 cp_lexer_consume_token (parser->lexer);
11515 /* The initializers were built up in reverse order, so we need to
11516 reverse them now. */
11517 return nreverse (initializers);
11520 /* Classes [gram.class] */
11522 /* Parse a class-name.
11528 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11529 to indicate that names looked up in dependent types should be
11530 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11531 keyword has been used to indicate that the name that appears next
11532 is a template. TYPE_P is true iff the next name should be treated
11533 as class-name, even if it is declared to be some other kind of name
11534 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11535 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11536 being defined in a class-head.
11538 Returns the TYPE_DECL representing the class. */
11541 cp_parser_class_name (cp_parser *parser,
11542 bool typename_keyword_p,
11543 bool template_keyword_p,
11545 bool check_dependency_p,
11547 bool is_declaration)
11554 /* All class-names start with an identifier. */
11555 token = cp_lexer_peek_token (parser->lexer);
11556 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11558 cp_parser_error (parser, "expected class-name");
11559 return error_mark_node;
11562 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11563 to a template-id, so we save it here. */
11564 scope = parser->scope;
11565 if (scope == error_mark_node)
11566 return error_mark_node;
11568 /* Any name names a type if we're following the `typename' keyword
11569 in a qualified name where the enclosing scope is type-dependent. */
11570 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11571 && dependent_type_p (scope));
11572 /* Handle the common case (an identifier, but not a template-id)
11574 if (token->type == CPP_NAME
11575 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11579 /* Look for the identifier. */
11580 identifier = cp_parser_identifier (parser);
11581 /* If the next token isn't an identifier, we are certainly not
11582 looking at a class-name. */
11583 if (identifier == error_mark_node)
11584 decl = error_mark_node;
11585 /* If we know this is a type-name, there's no need to look it
11587 else if (typename_p)
11591 /* If the next token is a `::', then the name must be a type
11594 [basic.lookup.qual]
11596 During the lookup for a name preceding the :: scope
11597 resolution operator, object, function, and enumerator
11598 names are ignored. */
11599 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11601 /* Look up the name. */
11602 decl = cp_parser_lookup_name (parser, identifier,
11604 /*is_template=*/false,
11605 /*is_namespace=*/false,
11606 check_dependency_p);
11611 /* Try a template-id. */
11612 decl = cp_parser_template_id (parser, template_keyword_p,
11613 check_dependency_p,
11615 if (decl == error_mark_node)
11616 return error_mark_node;
11619 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11621 /* If this is a typename, create a TYPENAME_TYPE. */
11622 if (typename_p && decl != error_mark_node)
11624 decl = make_typename_type (scope, decl, /*complain=*/1);
11625 if (decl != error_mark_node)
11626 decl = TYPE_NAME (decl);
11629 /* Check to see that it is really the name of a class. */
11630 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11631 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11632 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11633 /* Situations like this:
11635 template <typename T> struct A {
11636 typename T::template X<int>::I i;
11639 are problematic. Is `T::template X<int>' a class-name? The
11640 standard does not seem to be definitive, but there is no other
11641 valid interpretation of the following `::'. Therefore, those
11642 names are considered class-names. */
11643 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11644 else if (decl == error_mark_node
11645 || TREE_CODE (decl) != TYPE_DECL
11646 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11648 cp_parser_error (parser, "expected class-name");
11649 return error_mark_node;
11655 /* Parse a class-specifier.
11658 class-head { member-specification [opt] }
11660 Returns the TREE_TYPE representing the class. */
11663 cp_parser_class_specifier (cp_parser* parser)
11667 tree attributes = NULL_TREE;
11668 int has_trailing_semicolon;
11669 bool nested_name_specifier_p;
11670 unsigned saved_num_template_parameter_lists;
11672 push_deferring_access_checks (dk_no_deferred);
11674 /* Parse the class-head. */
11675 type = cp_parser_class_head (parser,
11676 &nested_name_specifier_p);
11677 /* If the class-head was a semantic disaster, skip the entire body
11681 cp_parser_skip_to_end_of_block_or_statement (parser);
11682 pop_deferring_access_checks ();
11683 return error_mark_node;
11686 /* Look for the `{'. */
11687 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11689 pop_deferring_access_checks ();
11690 return error_mark_node;
11693 /* Issue an error message if type-definitions are forbidden here. */
11694 cp_parser_check_type_definition (parser);
11695 /* Remember that we are defining one more class. */
11696 ++parser->num_classes_being_defined;
11697 /* Inside the class, surrounding template-parameter-lists do not
11699 saved_num_template_parameter_lists
11700 = parser->num_template_parameter_lists;
11701 parser->num_template_parameter_lists = 0;
11703 /* Start the class. */
11704 if (nested_name_specifier_p)
11705 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11706 type = begin_class_definition (type);
11707 if (type == error_mark_node)
11708 /* If the type is erroneous, skip the entire body of the class. */
11709 cp_parser_skip_to_closing_brace (parser);
11711 /* Parse the member-specification. */
11712 cp_parser_member_specification_opt (parser);
11713 /* Look for the trailing `}'. */
11714 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11715 /* We get better error messages by noticing a common problem: a
11716 missing trailing `;'. */
11717 token = cp_lexer_peek_token (parser->lexer);
11718 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11719 /* Look for attributes to apply to this class. */
11720 if (cp_parser_allow_gnu_extensions_p (parser))
11721 attributes = cp_parser_attributes_opt (parser);
11722 /* If we got any attributes in class_head, xref_tag will stick them in
11723 TREE_TYPE of the type. Grab them now. */
11724 if (type != error_mark_node)
11726 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11727 TYPE_ATTRIBUTES (type) = NULL_TREE;
11728 type = finish_struct (type, attributes);
11730 if (nested_name_specifier_p)
11731 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11732 /* If this class is not itself within the scope of another class,
11733 then we need to parse the bodies of all of the queued function
11734 definitions. Note that the queued functions defined in a class
11735 are not always processed immediately following the
11736 class-specifier for that class. Consider:
11739 struct B { void f() { sizeof (A); } };
11742 If `f' were processed before the processing of `A' were
11743 completed, there would be no way to compute the size of `A'.
11744 Note that the nesting we are interested in here is lexical --
11745 not the semantic nesting given by TYPE_CONTEXT. In particular,
11748 struct A { struct B; };
11749 struct A::B { void f() { } };
11751 there is no need to delay the parsing of `A::B::f'. */
11752 if (--parser->num_classes_being_defined == 0)
11757 /* In a first pass, parse default arguments to the functions.
11758 Then, in a second pass, parse the bodies of the functions.
11759 This two-phased approach handles cases like:
11767 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11768 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11769 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11770 TREE_PURPOSE (parser->unparsed_functions_queues)
11771 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11773 fn = TREE_VALUE (queue_entry);
11774 /* Make sure that any template parameters are in scope. */
11775 maybe_begin_member_template_processing (fn);
11776 /* If there are default arguments that have not yet been processed,
11777 take care of them now. */
11778 cp_parser_late_parsing_default_args (parser, fn);
11779 /* Remove any template parameters from the symbol table. */
11780 maybe_end_member_template_processing ();
11782 /* Now parse the body of the functions. */
11783 for (TREE_VALUE (parser->unparsed_functions_queues)
11784 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11785 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11786 TREE_VALUE (parser->unparsed_functions_queues)
11787 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11789 /* Figure out which function we need to process. */
11790 fn = TREE_VALUE (queue_entry);
11792 /* A hack to prevent garbage collection. */
11795 /* Parse the function. */
11796 cp_parser_late_parsing_for_member (parser, fn);
11802 /* Put back any saved access checks. */
11803 pop_deferring_access_checks ();
11805 /* Restore the count of active template-parameter-lists. */
11806 parser->num_template_parameter_lists
11807 = saved_num_template_parameter_lists;
11812 /* Parse a class-head.
11815 class-key identifier [opt] base-clause [opt]
11816 class-key nested-name-specifier identifier base-clause [opt]
11817 class-key nested-name-specifier [opt] template-id
11821 class-key attributes identifier [opt] base-clause [opt]
11822 class-key attributes nested-name-specifier identifier base-clause [opt]
11823 class-key attributes nested-name-specifier [opt] template-id
11826 Returns the TYPE of the indicated class. Sets
11827 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11828 involving a nested-name-specifier was used, and FALSE otherwise.
11830 Returns NULL_TREE if the class-head is syntactically valid, but
11831 semantically invalid in a way that means we should skip the entire
11832 body of the class. */
11835 cp_parser_class_head (cp_parser* parser,
11836 bool* nested_name_specifier_p)
11839 tree nested_name_specifier;
11840 enum tag_types class_key;
11841 tree id = NULL_TREE;
11842 tree type = NULL_TREE;
11844 bool template_id_p = false;
11845 bool qualified_p = false;
11846 bool invalid_nested_name_p = false;
11847 bool invalid_explicit_specialization_p = false;
11848 unsigned num_templates;
11850 /* Assume no nested-name-specifier will be present. */
11851 *nested_name_specifier_p = false;
11852 /* Assume no template parameter lists will be used in defining the
11856 /* Look for the class-key. */
11857 class_key = cp_parser_class_key (parser);
11858 if (class_key == none_type)
11859 return error_mark_node;
11861 /* Parse the attributes. */
11862 attributes = cp_parser_attributes_opt (parser);
11864 /* If the next token is `::', that is invalid -- but sometimes
11865 people do try to write:
11869 Handle this gracefully by accepting the extra qualifier, and then
11870 issuing an error about it later if this really is a
11871 class-head. If it turns out just to be an elaborated type
11872 specifier, remain silent. */
11873 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11874 qualified_p = true;
11876 push_deferring_access_checks (dk_no_check);
11878 /* Determine the name of the class. Begin by looking for an
11879 optional nested-name-specifier. */
11880 nested_name_specifier
11881 = cp_parser_nested_name_specifier_opt (parser,
11882 /*typename_keyword_p=*/false,
11883 /*check_dependency_p=*/false,
11885 /*is_declaration=*/false);
11886 /* If there was a nested-name-specifier, then there *must* be an
11888 if (nested_name_specifier)
11890 /* Although the grammar says `identifier', it really means
11891 `class-name' or `template-name'. You are only allowed to
11892 define a class that has already been declared with this
11895 The proposed resolution for Core Issue 180 says that whever
11896 you see `class T::X' you should treat `X' as a type-name.
11898 It is OK to define an inaccessible class; for example:
11900 class A { class B; };
11903 We do not know if we will see a class-name, or a
11904 template-name. We look for a class-name first, in case the
11905 class-name is a template-id; if we looked for the
11906 template-name first we would stop after the template-name. */
11907 cp_parser_parse_tentatively (parser);
11908 type = cp_parser_class_name (parser,
11909 /*typename_keyword_p=*/false,
11910 /*template_keyword_p=*/false,
11912 /*check_dependency_p=*/false,
11913 /*class_head_p=*/true,
11914 /*is_declaration=*/false);
11915 /* If that didn't work, ignore the nested-name-specifier. */
11916 if (!cp_parser_parse_definitely (parser))
11918 invalid_nested_name_p = true;
11919 id = cp_parser_identifier (parser);
11920 if (id == error_mark_node)
11923 /* If we could not find a corresponding TYPE, treat this
11924 declaration like an unqualified declaration. */
11925 if (type == error_mark_node)
11926 nested_name_specifier = NULL_TREE;
11927 /* Otherwise, count the number of templates used in TYPE and its
11928 containing scopes. */
11933 for (scope = TREE_TYPE (type);
11934 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11935 scope = (TYPE_P (scope)
11936 ? TYPE_CONTEXT (scope)
11937 : DECL_CONTEXT (scope)))
11939 && CLASS_TYPE_P (scope)
11940 && CLASSTYPE_TEMPLATE_INFO (scope)
11941 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11942 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11946 /* Otherwise, the identifier is optional. */
11949 /* We don't know whether what comes next is a template-id,
11950 an identifier, or nothing at all. */
11951 cp_parser_parse_tentatively (parser);
11952 /* Check for a template-id. */
11953 id = cp_parser_template_id (parser,
11954 /*template_keyword_p=*/false,
11955 /*check_dependency_p=*/true,
11956 /*is_declaration=*/true);
11957 /* If that didn't work, it could still be an identifier. */
11958 if (!cp_parser_parse_definitely (parser))
11960 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11961 id = cp_parser_identifier (parser);
11967 template_id_p = true;
11972 pop_deferring_access_checks ();
11974 cp_parser_check_for_invalid_template_id (parser, id);
11976 /* If it's not a `:' or a `{' then we can't really be looking at a
11977 class-head, since a class-head only appears as part of a
11978 class-specifier. We have to detect this situation before calling
11979 xref_tag, since that has irreversible side-effects. */
11980 if (!cp_parser_next_token_starts_class_definition_p (parser))
11982 cp_parser_error (parser, "expected `{' or `:'");
11983 return error_mark_node;
11986 /* At this point, we're going ahead with the class-specifier, even
11987 if some other problem occurs. */
11988 cp_parser_commit_to_tentative_parse (parser);
11989 /* Issue the error about the overly-qualified name now. */
11991 cp_parser_error (parser,
11992 "global qualification of class name is invalid");
11993 else if (invalid_nested_name_p)
11994 cp_parser_error (parser,
11995 "qualified name does not name a class");
11996 else if (nested_name_specifier)
11999 /* Figure out in what scope the declaration is being placed. */
12000 scope = current_scope ();
12002 scope = current_namespace;
12003 /* If that scope does not contain the scope in which the
12004 class was originally declared, the program is invalid. */
12005 if (scope && !is_ancestor (scope, nested_name_specifier))
12007 error ("declaration of `%D' in `%D' which does not "
12008 "enclose `%D'", type, scope, nested_name_specifier);
12014 A declarator-id shall not be qualified exception of the
12015 definition of a ... nested class outside of its class
12016 ... [or] a the definition or explicit instantiation of a
12017 class member of a namespace outside of its namespace. */
12018 if (scope == nested_name_specifier)
12020 pedwarn ("extra qualification ignored");
12021 nested_name_specifier = NULL_TREE;
12025 /* An explicit-specialization must be preceded by "template <>". If
12026 it is not, try to recover gracefully. */
12027 if (at_namespace_scope_p ()
12028 && parser->num_template_parameter_lists == 0
12031 error ("an explicit specialization must be preceded by 'template <>'");
12032 invalid_explicit_specialization_p = true;
12033 /* Take the same action that would have been taken by
12034 cp_parser_explicit_specialization. */
12035 ++parser->num_template_parameter_lists;
12036 begin_specialization ();
12038 /* There must be no "return" statements between this point and the
12039 end of this function; set "type "to the correct return value and
12040 use "goto done;" to return. */
12041 /* Make sure that the right number of template parameters were
12043 if (!cp_parser_check_template_parameters (parser, num_templates))
12045 /* If something went wrong, there is no point in even trying to
12046 process the class-definition. */
12051 /* Look up the type. */
12054 type = TREE_TYPE (id);
12055 maybe_process_partial_specialization (type);
12057 else if (!nested_name_specifier)
12059 /* If the class was unnamed, create a dummy name. */
12061 id = make_anon_name ();
12062 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
12063 parser->num_template_parameter_lists);
12071 template <typename T> struct S { struct T };
12072 template <typename T> struct S<T>::T { };
12074 we will get a TYPENAME_TYPE when processing the definition of
12075 `S::T'. We need to resolve it to the actual type before we
12076 try to define it. */
12077 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12079 class_type = resolve_typename_type (TREE_TYPE (type),
12080 /*only_current_p=*/false);
12081 if (class_type != error_mark_node)
12082 type = TYPE_NAME (class_type);
12085 cp_parser_error (parser, "could not resolve typename type");
12086 type = error_mark_node;
12090 maybe_process_partial_specialization (TREE_TYPE (type));
12091 class_type = current_class_type;
12092 /* Enter the scope indicated by the nested-name-specifier. */
12093 if (nested_name_specifier)
12094 push_scope (nested_name_specifier);
12095 /* Get the canonical version of this type. */
12096 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12097 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12098 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12099 type = push_template_decl (type);
12100 type = TREE_TYPE (type);
12101 if (nested_name_specifier)
12103 *nested_name_specifier_p = true;
12104 pop_scope (nested_name_specifier);
12107 /* Indicate whether this class was declared as a `class' or as a
12109 if (TREE_CODE (type) == RECORD_TYPE)
12110 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12111 cp_parser_check_class_key (class_key, type);
12113 /* Enter the scope containing the class; the names of base classes
12114 should be looked up in that context. For example, given:
12116 struct A { struct B {}; struct C; };
12117 struct A::C : B {};
12120 if (nested_name_specifier)
12121 push_scope (nested_name_specifier);
12122 /* Now, look for the base-clause. */
12123 token = cp_lexer_peek_token (parser->lexer);
12124 if (token->type == CPP_COLON)
12128 /* Get the list of base-classes. */
12129 bases = cp_parser_base_clause (parser);
12130 /* Process them. */
12131 xref_basetypes (type, bases);
12133 /* Leave the scope given by the nested-name-specifier. We will
12134 enter the class scope itself while processing the members. */
12135 if (nested_name_specifier)
12136 pop_scope (nested_name_specifier);
12139 if (invalid_explicit_specialization_p)
12141 end_specialization ();
12142 --parser->num_template_parameter_lists;
12147 /* Parse a class-key.
12154 Returns the kind of class-key specified, or none_type to indicate
12157 static enum tag_types
12158 cp_parser_class_key (cp_parser* parser)
12161 enum tag_types tag_type;
12163 /* Look for the class-key. */
12164 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12168 /* Check to see if the TOKEN is a class-key. */
12169 tag_type = cp_parser_token_is_class_key (token);
12171 cp_parser_error (parser, "expected class-key");
12175 /* Parse an (optional) member-specification.
12177 member-specification:
12178 member-declaration member-specification [opt]
12179 access-specifier : member-specification [opt] */
12182 cp_parser_member_specification_opt (cp_parser* parser)
12189 /* Peek at the next token. */
12190 token = cp_lexer_peek_token (parser->lexer);
12191 /* If it's a `}', or EOF then we've seen all the members. */
12192 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12195 /* See if this token is a keyword. */
12196 keyword = token->keyword;
12200 case RID_PROTECTED:
12202 /* Consume the access-specifier. */
12203 cp_lexer_consume_token (parser->lexer);
12204 /* Remember which access-specifier is active. */
12205 current_access_specifier = token->value;
12206 /* Look for the `:'. */
12207 cp_parser_require (parser, CPP_COLON, "`:'");
12211 /* Otherwise, the next construction must be a
12212 member-declaration. */
12213 cp_parser_member_declaration (parser);
12218 /* Parse a member-declaration.
12220 member-declaration:
12221 decl-specifier-seq [opt] member-declarator-list [opt] ;
12222 function-definition ; [opt]
12223 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12225 template-declaration
12227 member-declarator-list:
12229 member-declarator-list , member-declarator
12232 declarator pure-specifier [opt]
12233 declarator constant-initializer [opt]
12234 identifier [opt] : constant-expression
12238 member-declaration:
12239 __extension__ member-declaration
12242 declarator attributes [opt] pure-specifier [opt]
12243 declarator attributes [opt] constant-initializer [opt]
12244 identifier [opt] attributes [opt] : constant-expression */
12247 cp_parser_member_declaration (cp_parser* parser)
12249 tree decl_specifiers;
12250 tree prefix_attributes;
12252 int declares_class_or_enum;
12255 int saved_pedantic;
12257 /* Check for the `__extension__' keyword. */
12258 if (cp_parser_extension_opt (parser, &saved_pedantic))
12261 cp_parser_member_declaration (parser);
12262 /* Restore the old value of the PEDANTIC flag. */
12263 pedantic = saved_pedantic;
12268 /* Check for a template-declaration. */
12269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12271 /* Parse the template-declaration. */
12272 cp_parser_template_declaration (parser, /*member_p=*/true);
12277 /* Check for a using-declaration. */
12278 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12280 /* Parse the using-declaration. */
12281 cp_parser_using_declaration (parser);
12286 /* Parse the decl-specifier-seq. */
12288 = cp_parser_decl_specifier_seq (parser,
12289 CP_PARSER_FLAGS_OPTIONAL,
12290 &prefix_attributes,
12291 &declares_class_or_enum);
12292 /* Check for an invalid type-name. */
12293 if (cp_parser_diagnose_invalid_type_name (parser))
12295 /* If there is no declarator, then the decl-specifier-seq should
12297 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12299 /* If there was no decl-specifier-seq, and the next token is a
12300 `;', then we have something like:
12306 Each member-declaration shall declare at least one member
12307 name of the class. */
12308 if (!decl_specifiers)
12311 pedwarn ("extra semicolon");
12317 /* See if this declaration is a friend. */
12318 friend_p = cp_parser_friend_p (decl_specifiers);
12319 /* If there were decl-specifiers, check to see if there was
12320 a class-declaration. */
12321 type = check_tag_decl (decl_specifiers);
12322 /* Nested classes have already been added to the class, but
12323 a `friend' needs to be explicitly registered. */
12326 /* If the `friend' keyword was present, the friend must
12327 be introduced with a class-key. */
12328 if (!declares_class_or_enum)
12329 error ("a class-key must be used when declaring a friend");
12332 template <typename T> struct A {
12333 friend struct A<T>::B;
12336 A<T>::B will be represented by a TYPENAME_TYPE, and
12337 therefore not recognized by check_tag_decl. */
12342 for (specifier = decl_specifiers;
12344 specifier = TREE_CHAIN (specifier))
12346 tree s = TREE_VALUE (specifier);
12348 if (TREE_CODE (s) == IDENTIFIER_NODE)
12349 get_global_value_if_present (s, &type);
12350 if (TREE_CODE (s) == TYPE_DECL)
12359 if (!type || !TYPE_P (type))
12360 error ("friend declaration does not name a class or "
12363 make_friend_class (current_class_type, type,
12364 /*complain=*/true);
12366 /* If there is no TYPE, an error message will already have
12370 /* An anonymous aggregate has to be handled specially; such
12371 a declaration really declares a data member (with a
12372 particular type), as opposed to a nested class. */
12373 else if (ANON_AGGR_TYPE_P (type))
12375 /* Remove constructors and such from TYPE, now that we
12376 know it is an anonymous aggregate. */
12377 fixup_anonymous_aggr (type);
12378 /* And make the corresponding data member. */
12379 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12380 /* Add it to the class. */
12381 finish_member_declaration (decl);
12384 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12389 /* See if these declarations will be friends. */
12390 friend_p = cp_parser_friend_p (decl_specifiers);
12392 /* Keep going until we hit the `;' at the end of the
12394 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12396 tree attributes = NULL_TREE;
12397 tree first_attribute;
12399 /* Peek at the next token. */
12400 token = cp_lexer_peek_token (parser->lexer);
12402 /* Check for a bitfield declaration. */
12403 if (token->type == CPP_COLON
12404 || (token->type == CPP_NAME
12405 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12411 /* Get the name of the bitfield. Note that we cannot just
12412 check TOKEN here because it may have been invalidated by
12413 the call to cp_lexer_peek_nth_token above. */
12414 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12415 identifier = cp_parser_identifier (parser);
12417 identifier = NULL_TREE;
12419 /* Consume the `:' token. */
12420 cp_lexer_consume_token (parser->lexer);
12421 /* Get the width of the bitfield. */
12423 = cp_parser_constant_expression (parser,
12424 /*allow_non_constant=*/false,
12427 /* Look for attributes that apply to the bitfield. */
12428 attributes = cp_parser_attributes_opt (parser);
12429 /* Remember which attributes are prefix attributes and
12431 first_attribute = attributes;
12432 /* Combine the attributes. */
12433 attributes = chainon (prefix_attributes, attributes);
12435 /* Create the bitfield declaration. */
12436 decl = grokbitfield (identifier,
12439 /* Apply the attributes. */
12440 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12446 tree asm_specification;
12447 int ctor_dtor_or_conv_p;
12449 /* Parse the declarator. */
12451 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12452 &ctor_dtor_or_conv_p,
12453 /*parenthesized_p=*/NULL);
12455 /* If something went wrong parsing the declarator, make sure
12456 that we at least consume some tokens. */
12457 if (declarator == error_mark_node)
12459 /* Skip to the end of the statement. */
12460 cp_parser_skip_to_end_of_statement (parser);
12461 /* If the next token is not a semicolon, that is
12462 probably because we just skipped over the body of
12463 a function. So, we consume a semicolon if
12464 present, but do not issue an error message if it
12466 if (cp_lexer_next_token_is (parser->lexer,
12468 cp_lexer_consume_token (parser->lexer);
12472 cp_parser_check_for_definition_in_return_type
12473 (declarator, declares_class_or_enum);
12475 /* Look for an asm-specification. */
12476 asm_specification = cp_parser_asm_specification_opt (parser);
12477 /* Look for attributes that apply to the declaration. */
12478 attributes = cp_parser_attributes_opt (parser);
12479 /* Remember which attributes are prefix attributes and
12481 first_attribute = attributes;
12482 /* Combine the attributes. */
12483 attributes = chainon (prefix_attributes, attributes);
12485 /* If it's an `=', then we have a constant-initializer or a
12486 pure-specifier. It is not correct to parse the
12487 initializer before registering the member declaration
12488 since the member declaration should be in scope while
12489 its initializer is processed. However, the rest of the
12490 front end does not yet provide an interface that allows
12491 us to handle this correctly. */
12492 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12496 A pure-specifier shall be used only in the declaration of
12497 a virtual function.
12499 A member-declarator can contain a constant-initializer
12500 only if it declares a static member of integral or
12503 Therefore, if the DECLARATOR is for a function, we look
12504 for a pure-specifier; otherwise, we look for a
12505 constant-initializer. When we call `grokfield', it will
12506 perform more stringent semantics checks. */
12507 if (TREE_CODE (declarator) == CALL_EXPR)
12508 initializer = cp_parser_pure_specifier (parser);
12510 /* Parse the initializer. */
12511 initializer = cp_parser_constant_initializer (parser);
12513 /* Otherwise, there is no initializer. */
12515 initializer = NULL_TREE;
12517 /* See if we are probably looking at a function
12518 definition. We are certainly not looking at at a
12519 member-declarator. Calling `grokfield' has
12520 side-effects, so we must not do it unless we are sure
12521 that we are looking at a member-declarator. */
12522 if (cp_parser_token_starts_function_definition_p
12523 (cp_lexer_peek_token (parser->lexer)))
12525 /* The grammar does not allow a pure-specifier to be
12526 used when a member function is defined. (It is
12527 possible that this fact is an oversight in the
12528 standard, since a pure function may be defined
12529 outside of the class-specifier. */
12531 error ("pure-specifier on function-definition");
12532 decl = cp_parser_save_member_function_body (parser,
12536 /* If the member was not a friend, declare it here. */
12538 finish_member_declaration (decl);
12539 /* Peek at the next token. */
12540 token = cp_lexer_peek_token (parser->lexer);
12541 /* If the next token is a semicolon, consume it. */
12542 if (token->type == CPP_SEMICOLON)
12543 cp_lexer_consume_token (parser->lexer);
12548 /* Create the declaration. */
12549 decl = grokfield (declarator, decl_specifiers,
12550 initializer, asm_specification,
12552 /* Any initialization must have been from a
12553 constant-expression. */
12554 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12555 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12559 /* Reset PREFIX_ATTRIBUTES. */
12560 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12561 attributes = TREE_CHAIN (attributes);
12563 TREE_CHAIN (attributes) = NULL_TREE;
12565 /* If there is any qualification still in effect, clear it
12566 now; we will be starting fresh with the next declarator. */
12567 parser->scope = NULL_TREE;
12568 parser->qualifying_scope = NULL_TREE;
12569 parser->object_scope = NULL_TREE;
12570 /* If it's a `,', then there are more declarators. */
12571 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12572 cp_lexer_consume_token (parser->lexer);
12573 /* If the next token isn't a `;', then we have a parse error. */
12574 else if (cp_lexer_next_token_is_not (parser->lexer,
12577 cp_parser_error (parser, "expected `;'");
12578 /* Skip tokens until we find a `;'. */
12579 cp_parser_skip_to_end_of_statement (parser);
12586 /* Add DECL to the list of members. */
12588 finish_member_declaration (decl);
12590 if (TREE_CODE (decl) == FUNCTION_DECL)
12591 cp_parser_save_default_args (parser, decl);
12596 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12599 /* Parse a pure-specifier.
12604 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12605 Otherwise, ERROR_MARK_NODE is returned. */
12608 cp_parser_pure_specifier (cp_parser* parser)
12612 /* Look for the `=' token. */
12613 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12614 return error_mark_node;
12615 /* Look for the `0' token. */
12616 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12617 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12618 to get information from the lexer about how the number was
12619 spelled in order to fix this problem. */
12620 if (!token || !integer_zerop (token->value))
12621 return error_mark_node;
12623 return integer_zero_node;
12626 /* Parse a constant-initializer.
12628 constant-initializer:
12629 = constant-expression
12631 Returns a representation of the constant-expression. */
12634 cp_parser_constant_initializer (cp_parser* parser)
12636 /* Look for the `=' token. */
12637 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12638 return error_mark_node;
12640 /* It is invalid to write:
12642 struct S { static const int i = { 7 }; };
12645 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12647 cp_parser_error (parser,
12648 "a brace-enclosed initializer is not allowed here");
12649 /* Consume the opening brace. */
12650 cp_lexer_consume_token (parser->lexer);
12651 /* Skip the initializer. */
12652 cp_parser_skip_to_closing_brace (parser);
12653 /* Look for the trailing `}'. */
12654 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12656 return error_mark_node;
12659 return cp_parser_constant_expression (parser,
12660 /*allow_non_constant=*/false,
12664 /* Derived classes [gram.class.derived] */
12666 /* Parse a base-clause.
12669 : base-specifier-list
12671 base-specifier-list:
12673 base-specifier-list , base-specifier
12675 Returns a TREE_LIST representing the base-classes, in the order in
12676 which they were declared. The representation of each node is as
12677 described by cp_parser_base_specifier.
12679 In the case that no bases are specified, this function will return
12680 NULL_TREE, not ERROR_MARK_NODE. */
12683 cp_parser_base_clause (cp_parser* parser)
12685 tree bases = NULL_TREE;
12687 /* Look for the `:' that begins the list. */
12688 cp_parser_require (parser, CPP_COLON, "`:'");
12690 /* Scan the base-specifier-list. */
12696 /* Look for the base-specifier. */
12697 base = cp_parser_base_specifier (parser);
12698 /* Add BASE to the front of the list. */
12699 if (base != error_mark_node)
12701 TREE_CHAIN (base) = bases;
12704 /* Peek at the next token. */
12705 token = cp_lexer_peek_token (parser->lexer);
12706 /* If it's not a comma, then the list is complete. */
12707 if (token->type != CPP_COMMA)
12709 /* Consume the `,'. */
12710 cp_lexer_consume_token (parser->lexer);
12713 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12714 base class had a qualified name. However, the next name that
12715 appears is certainly not qualified. */
12716 parser->scope = NULL_TREE;
12717 parser->qualifying_scope = NULL_TREE;
12718 parser->object_scope = NULL_TREE;
12720 return nreverse (bases);
12723 /* Parse a base-specifier.
12726 :: [opt] nested-name-specifier [opt] class-name
12727 virtual access-specifier [opt] :: [opt] nested-name-specifier
12729 access-specifier virtual [opt] :: [opt] nested-name-specifier
12732 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12733 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12734 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12735 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12738 cp_parser_base_specifier (cp_parser* parser)
12742 bool virtual_p = false;
12743 bool duplicate_virtual_error_issued_p = false;
12744 bool duplicate_access_error_issued_p = false;
12745 bool class_scope_p, template_p;
12746 tree access = access_default_node;
12749 /* Process the optional `virtual' and `access-specifier'. */
12752 /* Peek at the next token. */
12753 token = cp_lexer_peek_token (parser->lexer);
12754 /* Process `virtual'. */
12755 switch (token->keyword)
12758 /* If `virtual' appears more than once, issue an error. */
12759 if (virtual_p && !duplicate_virtual_error_issued_p)
12761 cp_parser_error (parser,
12762 "`virtual' specified more than once in base-specified");
12763 duplicate_virtual_error_issued_p = true;
12768 /* Consume the `virtual' token. */
12769 cp_lexer_consume_token (parser->lexer);
12774 case RID_PROTECTED:
12776 /* If more than one access specifier appears, issue an
12778 if (access != access_default_node
12779 && !duplicate_access_error_issued_p)
12781 cp_parser_error (parser,
12782 "more than one access specifier in base-specified");
12783 duplicate_access_error_issued_p = true;
12786 access = ridpointers[(int) token->keyword];
12788 /* Consume the access-specifier. */
12789 cp_lexer_consume_token (parser->lexer);
12798 /* It is not uncommon to see programs mechanically, erroneously, use
12799 the 'typename' keyword to denote (dependent) qualified types
12800 as base classes. */
12801 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12803 if (!processing_template_decl)
12804 error ("keyword `typename' not allowed outside of templates");
12806 error ("keyword `typename' not allowed in this context "
12807 "(the base class is implicitly a type)");
12808 cp_lexer_consume_token (parser->lexer);
12811 /* Look for the optional `::' operator. */
12812 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12813 /* Look for the nested-name-specifier. The simplest way to
12818 The keyword `typename' is not permitted in a base-specifier or
12819 mem-initializer; in these contexts a qualified name that
12820 depends on a template-parameter is implicitly assumed to be a
12823 is to pretend that we have seen the `typename' keyword at this
12825 cp_parser_nested_name_specifier_opt (parser,
12826 /*typename_keyword_p=*/true,
12827 /*check_dependency_p=*/true,
12829 /*is_declaration=*/true);
12830 /* If the base class is given by a qualified name, assume that names
12831 we see are type names or templates, as appropriate. */
12832 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12833 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12835 /* Finally, look for the class-name. */
12836 type = cp_parser_class_name (parser,
12840 /*check_dependency_p=*/true,
12841 /*class_head_p=*/false,
12842 /*is_declaration=*/true);
12844 if (type == error_mark_node)
12845 return error_mark_node;
12847 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12850 /* Exception handling [gram.exception] */
12852 /* Parse an (optional) exception-specification.
12854 exception-specification:
12855 throw ( type-id-list [opt] )
12857 Returns a TREE_LIST representing the exception-specification. The
12858 TREE_VALUE of each node is a type. */
12861 cp_parser_exception_specification_opt (cp_parser* parser)
12866 /* Peek at the next token. */
12867 token = cp_lexer_peek_token (parser->lexer);
12868 /* If it's not `throw', then there's no exception-specification. */
12869 if (!cp_parser_is_keyword (token, RID_THROW))
12872 /* Consume the `throw'. */
12873 cp_lexer_consume_token (parser->lexer);
12875 /* Look for the `('. */
12876 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12878 /* Peek at the next token. */
12879 token = cp_lexer_peek_token (parser->lexer);
12880 /* If it's not a `)', then there is a type-id-list. */
12881 if (token->type != CPP_CLOSE_PAREN)
12883 const char *saved_message;
12885 /* Types may not be defined in an exception-specification. */
12886 saved_message = parser->type_definition_forbidden_message;
12887 parser->type_definition_forbidden_message
12888 = "types may not be defined in an exception-specification";
12889 /* Parse the type-id-list. */
12890 type_id_list = cp_parser_type_id_list (parser);
12891 /* Restore the saved message. */
12892 parser->type_definition_forbidden_message = saved_message;
12895 type_id_list = empty_except_spec;
12897 /* Look for the `)'. */
12898 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12900 return type_id_list;
12903 /* Parse an (optional) type-id-list.
12907 type-id-list , type-id
12909 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12910 in the order that the types were presented. */
12913 cp_parser_type_id_list (cp_parser* parser)
12915 tree types = NULL_TREE;
12922 /* Get the next type-id. */
12923 type = cp_parser_type_id (parser);
12924 /* Add it to the list. */
12925 types = add_exception_specifier (types, type, /*complain=*/1);
12926 /* Peek at the next token. */
12927 token = cp_lexer_peek_token (parser->lexer);
12928 /* If it is not a `,', we are done. */
12929 if (token->type != CPP_COMMA)
12931 /* Consume the `,'. */
12932 cp_lexer_consume_token (parser->lexer);
12935 return nreverse (types);
12938 /* Parse a try-block.
12941 try compound-statement handler-seq */
12944 cp_parser_try_block (cp_parser* parser)
12948 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12949 try_block = begin_try_block ();
12950 cp_parser_compound_statement (parser, false);
12951 finish_try_block (try_block);
12952 cp_parser_handler_seq (parser);
12953 finish_handler_sequence (try_block);
12958 /* Parse a function-try-block.
12960 function-try-block:
12961 try ctor-initializer [opt] function-body handler-seq */
12964 cp_parser_function_try_block (cp_parser* parser)
12967 bool ctor_initializer_p;
12969 /* Look for the `try' keyword. */
12970 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12972 /* Let the rest of the front-end know where we are. */
12973 try_block = begin_function_try_block ();
12974 /* Parse the function-body. */
12976 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12977 /* We're done with the `try' part. */
12978 finish_function_try_block (try_block);
12979 /* Parse the handlers. */
12980 cp_parser_handler_seq (parser);
12981 /* We're done with the handlers. */
12982 finish_function_handler_sequence (try_block);
12984 return ctor_initializer_p;
12987 /* Parse a handler-seq.
12990 handler handler-seq [opt] */
12993 cp_parser_handler_seq (cp_parser* parser)
12999 /* Parse the handler. */
13000 cp_parser_handler (parser);
13001 /* Peek at the next token. */
13002 token = cp_lexer_peek_token (parser->lexer);
13003 /* If it's not `catch' then there are no more handlers. */
13004 if (!cp_parser_is_keyword (token, RID_CATCH))
13009 /* Parse a handler.
13012 catch ( exception-declaration ) compound-statement */
13015 cp_parser_handler (cp_parser* parser)
13020 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13021 handler = begin_handler ();
13022 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13023 declaration = cp_parser_exception_declaration (parser);
13024 finish_handler_parms (declaration, handler);
13025 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13026 cp_parser_compound_statement (parser, false);
13027 finish_handler (handler);
13030 /* Parse an exception-declaration.
13032 exception-declaration:
13033 type-specifier-seq declarator
13034 type-specifier-seq abstract-declarator
13038 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13039 ellipsis variant is used. */
13042 cp_parser_exception_declaration (cp_parser* parser)
13044 tree type_specifiers;
13046 const char *saved_message;
13048 /* If it's an ellipsis, it's easy to handle. */
13049 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13051 /* Consume the `...' token. */
13052 cp_lexer_consume_token (parser->lexer);
13056 /* Types may not be defined in exception-declarations. */
13057 saved_message = parser->type_definition_forbidden_message;
13058 parser->type_definition_forbidden_message
13059 = "types may not be defined in exception-declarations";
13061 /* Parse the type-specifier-seq. */
13062 type_specifiers = cp_parser_type_specifier_seq (parser);
13063 /* If it's a `)', then there is no declarator. */
13064 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13065 declarator = NULL_TREE;
13067 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13068 /*ctor_dtor_or_conv_p=*/NULL,
13069 /*parenthesized_p=*/NULL);
13071 /* Restore the saved message. */
13072 parser->type_definition_forbidden_message = saved_message;
13074 return start_handler_parms (type_specifiers, declarator);
13077 /* Parse a throw-expression.
13080 throw assignment-expression [opt]
13082 Returns a THROW_EXPR representing the throw-expression. */
13085 cp_parser_throw_expression (cp_parser* parser)
13090 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13091 token = cp_lexer_peek_token (parser->lexer);
13092 /* Figure out whether or not there is an assignment-expression
13093 following the "throw" keyword. */
13094 if (token->type == CPP_COMMA
13095 || token->type == CPP_SEMICOLON
13096 || token->type == CPP_CLOSE_PAREN
13097 || token->type == CPP_CLOSE_SQUARE
13098 || token->type == CPP_CLOSE_BRACE
13099 || token->type == CPP_COLON)
13100 expression = NULL_TREE;
13102 expression = cp_parser_assignment_expression (parser);
13104 return build_throw (expression);
13107 /* GNU Extensions */
13109 /* Parse an (optional) asm-specification.
13112 asm ( string-literal )
13114 If the asm-specification is present, returns a STRING_CST
13115 corresponding to the string-literal. Otherwise, returns
13119 cp_parser_asm_specification_opt (cp_parser* parser)
13122 tree asm_specification;
13124 /* Peek at the next token. */
13125 token = cp_lexer_peek_token (parser->lexer);
13126 /* If the next token isn't the `asm' keyword, then there's no
13127 asm-specification. */
13128 if (!cp_parser_is_keyword (token, RID_ASM))
13131 /* Consume the `asm' token. */
13132 cp_lexer_consume_token (parser->lexer);
13133 /* Look for the `('. */
13134 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13136 /* Look for the string-literal. */
13137 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13139 asm_specification = token->value;
13141 asm_specification = NULL_TREE;
13143 /* Look for the `)'. */
13144 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13146 return asm_specification;
13149 /* Parse an asm-operand-list.
13153 asm-operand-list , asm-operand
13156 string-literal ( expression )
13157 [ string-literal ] string-literal ( expression )
13159 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13160 each node is the expression. The TREE_PURPOSE is itself a
13161 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13162 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13163 is a STRING_CST for the string literal before the parenthesis. */
13166 cp_parser_asm_operand_list (cp_parser* parser)
13168 tree asm_operands = NULL_TREE;
13172 tree string_literal;
13177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13179 /* Consume the `[' token. */
13180 cp_lexer_consume_token (parser->lexer);
13181 /* Read the operand name. */
13182 name = cp_parser_identifier (parser);
13183 if (name != error_mark_node)
13184 name = build_string (IDENTIFIER_LENGTH (name),
13185 IDENTIFIER_POINTER (name));
13186 /* Look for the closing `]'. */
13187 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13191 /* Look for the string-literal. */
13192 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13193 string_literal = token ? token->value : error_mark_node;
13194 /* Look for the `('. */
13195 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13196 /* Parse the expression. */
13197 expression = cp_parser_expression (parser);
13198 /* Look for the `)'. */
13199 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13200 /* Add this operand to the list. */
13201 asm_operands = tree_cons (build_tree_list (name, string_literal),
13204 /* If the next token is not a `,', there are no more
13206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13208 /* Consume the `,'. */
13209 cp_lexer_consume_token (parser->lexer);
13212 return nreverse (asm_operands);
13215 /* Parse an asm-clobber-list.
13219 asm-clobber-list , string-literal
13221 Returns a TREE_LIST, indicating the clobbers in the order that they
13222 appeared. The TREE_VALUE of each node is a STRING_CST. */
13225 cp_parser_asm_clobber_list (cp_parser* parser)
13227 tree clobbers = NULL_TREE;
13232 tree string_literal;
13234 /* Look for the string literal. */
13235 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13236 string_literal = token ? token->value : error_mark_node;
13237 /* Add it to the list. */
13238 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13239 /* If the next token is not a `,', then the list is
13241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13243 /* Consume the `,' token. */
13244 cp_lexer_consume_token (parser->lexer);
13250 /* Parse an (optional) series of attributes.
13253 attributes attribute
13256 __attribute__ (( attribute-list [opt] ))
13258 The return value is as for cp_parser_attribute_list. */
13261 cp_parser_attributes_opt (cp_parser* parser)
13263 tree attributes = NULL_TREE;
13268 tree attribute_list;
13270 /* Peek at the next token. */
13271 token = cp_lexer_peek_token (parser->lexer);
13272 /* If it's not `__attribute__', then we're done. */
13273 if (token->keyword != RID_ATTRIBUTE)
13276 /* Consume the `__attribute__' keyword. */
13277 cp_lexer_consume_token (parser->lexer);
13278 /* Look for the two `(' tokens. */
13279 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13280 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13282 /* Peek at the next token. */
13283 token = cp_lexer_peek_token (parser->lexer);
13284 if (token->type != CPP_CLOSE_PAREN)
13285 /* Parse the attribute-list. */
13286 attribute_list = cp_parser_attribute_list (parser);
13288 /* If the next token is a `)', then there is no attribute
13290 attribute_list = NULL;
13292 /* Look for the two `)' tokens. */
13293 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13294 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13296 /* Add these new attributes to the list. */
13297 attributes = chainon (attributes, attribute_list);
13303 /* Parse an attribute-list.
13307 attribute-list , attribute
13311 identifier ( identifier )
13312 identifier ( identifier , expression-list )
13313 identifier ( expression-list )
13315 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13316 TREE_PURPOSE of each node is the identifier indicating which
13317 attribute is in use. The TREE_VALUE represents the arguments, if
13321 cp_parser_attribute_list (cp_parser* parser)
13323 tree attribute_list = NULL_TREE;
13331 /* Look for the identifier. We also allow keywords here; for
13332 example `__attribute__ ((const))' is legal. */
13333 token = cp_lexer_peek_token (parser->lexer);
13334 if (token->type != CPP_NAME
13335 && token->type != CPP_KEYWORD)
13336 return error_mark_node;
13337 /* Consume the token. */
13338 token = cp_lexer_consume_token (parser->lexer);
13340 /* Save away the identifier that indicates which attribute this is. */
13341 identifier = token->value;
13342 attribute = build_tree_list (identifier, NULL_TREE);
13344 /* Peek at the next token. */
13345 token = cp_lexer_peek_token (parser->lexer);
13346 /* If it's an `(', then parse the attribute arguments. */
13347 if (token->type == CPP_OPEN_PAREN)
13351 arguments = (cp_parser_parenthesized_expression_list
13352 (parser, true, /*non_constant_p=*/NULL));
13353 /* Save the identifier and arguments away. */
13354 TREE_VALUE (attribute) = arguments;
13357 /* Add this attribute to the list. */
13358 TREE_CHAIN (attribute) = attribute_list;
13359 attribute_list = attribute;
13361 /* Now, look for more attributes. */
13362 token = cp_lexer_peek_token (parser->lexer);
13363 /* If the next token isn't a `,', we're done. */
13364 if (token->type != CPP_COMMA)
13367 /* Consume the comma and keep going. */
13368 cp_lexer_consume_token (parser->lexer);
13371 /* We built up the list in reverse order. */
13372 return nreverse (attribute_list);
13375 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13376 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13377 current value of the PEDANTIC flag, regardless of whether or not
13378 the `__extension__' keyword is present. The caller is responsible
13379 for restoring the value of the PEDANTIC flag. */
13382 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13384 /* Save the old value of the PEDANTIC flag. */
13385 *saved_pedantic = pedantic;
13387 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13389 /* Consume the `__extension__' token. */
13390 cp_lexer_consume_token (parser->lexer);
13391 /* We're not being pedantic while the `__extension__' keyword is
13401 /* Parse a label declaration.
13404 __label__ label-declarator-seq ;
13406 label-declarator-seq:
13407 identifier , label-declarator-seq
13411 cp_parser_label_declaration (cp_parser* parser)
13413 /* Look for the `__label__' keyword. */
13414 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13420 /* Look for an identifier. */
13421 identifier = cp_parser_identifier (parser);
13422 /* Declare it as a lobel. */
13423 finish_label_decl (identifier);
13424 /* If the next token is a `;', stop. */
13425 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13427 /* Look for the `,' separating the label declarations. */
13428 cp_parser_require (parser, CPP_COMMA, "`,'");
13431 /* Look for the final `;'. */
13432 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13435 /* Support Functions */
13437 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13438 NAME should have one of the representations used for an
13439 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13440 is returned. If PARSER->SCOPE is a dependent type, then a
13441 SCOPE_REF is returned.
13443 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13444 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13445 was formed. Abstractly, such entities should not be passed to this
13446 function, because they do not need to be looked up, but it is
13447 simpler to check for this special case here, rather than at the
13450 In cases not explicitly covered above, this function returns a
13451 DECL, OVERLOAD, or baselink representing the result of the lookup.
13452 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13455 If IS_TYPE is TRUE, bindings that do not refer to types are
13458 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13461 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13464 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13468 cp_parser_lookup_name (cp_parser *parser, tree name,
13469 bool is_type, bool is_template, bool is_namespace,
13470 bool check_dependency)
13473 tree object_type = parser->context->object_type;
13475 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13476 no longer valid. Note that if we are parsing tentatively, and
13477 the parse fails, OBJECT_TYPE will be automatically restored. */
13478 parser->context->object_type = NULL_TREE;
13480 if (name == error_mark_node)
13481 return error_mark_node;
13483 /* A template-id has already been resolved; there is no lookup to
13485 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13487 if (BASELINK_P (name))
13489 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13490 == TEMPLATE_ID_EXPR),
13495 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13496 it should already have been checked to make sure that the name
13497 used matches the type being destroyed. */
13498 if (TREE_CODE (name) == BIT_NOT_EXPR)
13502 /* Figure out to which type this destructor applies. */
13504 type = parser->scope;
13505 else if (object_type)
13506 type = object_type;
13508 type = current_class_type;
13509 /* If that's not a class type, there is no destructor. */
13510 if (!type || !CLASS_TYPE_P (type))
13511 return error_mark_node;
13512 if (!CLASSTYPE_DESTRUCTORS (type))
13513 return error_mark_node;
13514 /* If it was a class type, return the destructor. */
13515 return CLASSTYPE_DESTRUCTORS (type);
13518 /* By this point, the NAME should be an ordinary identifier. If
13519 the id-expression was a qualified name, the qualifying scope is
13520 stored in PARSER->SCOPE at this point. */
13521 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13524 /* Perform the lookup. */
13529 if (parser->scope == error_mark_node)
13530 return error_mark_node;
13532 /* If the SCOPE is dependent, the lookup must be deferred until
13533 the template is instantiated -- unless we are explicitly
13534 looking up names in uninstantiated templates. Even then, we
13535 cannot look up the name if the scope is not a class type; it
13536 might, for example, be a template type parameter. */
13537 dependent_p = (TYPE_P (parser->scope)
13538 && !(parser->in_declarator_p
13539 && currently_open_class (parser->scope))
13540 && dependent_type_p (parser->scope));
13541 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13545 /* The resolution to Core Issue 180 says that `struct A::B'
13546 should be considered a type-name, even if `A' is
13548 decl = TYPE_NAME (make_typename_type (parser->scope,
13551 else if (is_template)
13552 decl = make_unbound_class_template (parser->scope,
13556 decl = build_nt (SCOPE_REF, parser->scope, name);
13560 /* If PARSER->SCOPE is a dependent type, then it must be a
13561 class type, and we must not be checking dependencies;
13562 otherwise, we would have processed this lookup above. So
13563 that PARSER->SCOPE is not considered a dependent base by
13564 lookup_member, we must enter the scope here. */
13566 push_scope (parser->scope);
13567 /* If the PARSER->SCOPE is a a template specialization, it
13568 may be instantiated during name lookup. In that case,
13569 errors may be issued. Even if we rollback the current
13570 tentative parse, those errors are valid. */
13571 decl = lookup_qualified_name (parser->scope, name, is_type,
13572 /*complain=*/true);
13574 pop_scope (parser->scope);
13576 parser->qualifying_scope = parser->scope;
13577 parser->object_scope = NULL_TREE;
13579 else if (object_type)
13581 tree object_decl = NULL_TREE;
13582 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13583 OBJECT_TYPE is not a class. */
13584 if (CLASS_TYPE_P (object_type))
13585 /* If the OBJECT_TYPE is a template specialization, it may
13586 be instantiated during name lookup. In that case, errors
13587 may be issued. Even if we rollback the current tentative
13588 parse, those errors are valid. */
13589 object_decl = lookup_member (object_type,
13591 /*protect=*/0, is_type);
13592 /* Look it up in the enclosing context, too. */
13593 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13596 parser->object_scope = object_type;
13597 parser->qualifying_scope = NULL_TREE;
13599 decl = object_decl;
13603 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13606 parser->qualifying_scope = NULL_TREE;
13607 parser->object_scope = NULL_TREE;
13610 /* If the lookup failed, let our caller know. */
13612 || decl == error_mark_node
13613 || (TREE_CODE (decl) == FUNCTION_DECL
13614 && DECL_ANTICIPATED (decl)))
13615 return error_mark_node;
13617 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13618 if (TREE_CODE (decl) == TREE_LIST)
13620 /* The error message we have to print is too complicated for
13621 cp_parser_error, so we incorporate its actions directly. */
13622 if (!cp_parser_simulate_error (parser))
13624 error ("reference to `%D' is ambiguous", name);
13625 print_candidates (decl);
13627 return error_mark_node;
13630 my_friendly_assert (DECL_P (decl)
13631 || TREE_CODE (decl) == OVERLOAD
13632 || TREE_CODE (decl) == SCOPE_REF
13633 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13634 || BASELINK_P (decl),
13637 /* If we have resolved the name of a member declaration, check to
13638 see if the declaration is accessible. When the name resolves to
13639 set of overloaded functions, accessibility is checked when
13640 overload resolution is done.
13642 During an explicit instantiation, access is not checked at all,
13643 as per [temp.explicit]. */
13645 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13650 /* Like cp_parser_lookup_name, but for use in the typical case where
13651 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13652 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13655 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13657 return cp_parser_lookup_name (parser, name,
13659 /*is_template=*/false,
13660 /*is_namespace=*/false,
13661 /*check_dependency=*/true);
13664 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13665 the current context, return the TYPE_DECL. If TAG_NAME_P is
13666 true, the DECL indicates the class being defined in a class-head,
13667 or declared in an elaborated-type-specifier.
13669 Otherwise, return DECL. */
13672 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13674 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13675 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13678 template <typename T> struct B;
13681 template <typename T> struct A::B {};
13683 Similarly, in a elaborated-type-specifier:
13685 namespace N { struct X{}; }
13688 template <typename T> friend struct N::X;
13691 However, if the DECL refers to a class type, and we are in
13692 the scope of the class, then the name lookup automatically
13693 finds the TYPE_DECL created by build_self_reference rather
13694 than a TEMPLATE_DECL. For example, in:
13696 template <class T> struct S {
13700 there is no need to handle such case. */
13702 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13703 return DECL_TEMPLATE_RESULT (decl);
13708 /* If too many, or too few, template-parameter lists apply to the
13709 declarator, issue an error message. Returns TRUE if all went well,
13710 and FALSE otherwise. */
13713 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13716 unsigned num_templates;
13718 /* We haven't seen any classes that involve template parameters yet. */
13721 switch (TREE_CODE (declarator))
13728 tree main_declarator = TREE_OPERAND (declarator, 0);
13730 cp_parser_check_declarator_template_parameters (parser,
13739 scope = TREE_OPERAND (declarator, 0);
13740 member = TREE_OPERAND (declarator, 1);
13742 /* If this is a pointer-to-member, then we are not interested
13743 in the SCOPE, because it does not qualify the thing that is
13745 if (TREE_CODE (member) == INDIRECT_REF)
13746 return (cp_parser_check_declarator_template_parameters
13749 while (scope && CLASS_TYPE_P (scope))
13751 /* You're supposed to have one `template <...>'
13752 for every template class, but you don't need one
13753 for a full specialization. For example:
13755 template <class T> struct S{};
13756 template <> struct S<int> { void f(); };
13757 void S<int>::f () {}
13759 is correct; there shouldn't be a `template <>' for
13760 the definition of `S<int>::f'. */
13761 if (CLASSTYPE_TEMPLATE_INFO (scope)
13762 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13763 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13764 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13767 scope = TYPE_CONTEXT (scope);
13771 /* Fall through. */
13774 /* If the DECLARATOR has the form `X<y>' then it uses one
13775 additional level of template parameters. */
13776 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13779 return cp_parser_check_template_parameters (parser,
13784 /* NUM_TEMPLATES were used in the current declaration. If that is
13785 invalid, return FALSE and issue an error messages. Otherwise,
13789 cp_parser_check_template_parameters (cp_parser* parser,
13790 unsigned num_templates)
13792 /* If there are more template classes than parameter lists, we have
13795 template <class T> void S<T>::R<T>::f (); */
13796 if (parser->num_template_parameter_lists < num_templates)
13798 error ("too few template-parameter-lists");
13801 /* If there are the same number of template classes and parameter
13802 lists, that's OK. */
13803 if (parser->num_template_parameter_lists == num_templates)
13805 /* If there are more, but only one more, then we are referring to a
13806 member template. That's OK too. */
13807 if (parser->num_template_parameter_lists == num_templates + 1)
13809 /* Otherwise, there are too many template parameter lists. We have
13812 template <class T> template <class U> void S::f(); */
13813 error ("too many template-parameter-lists");
13817 /* Parse a binary-expression of the general form:
13821 binary-expression <token> <expr>
13823 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13824 to parser the <expr>s. If the first production is used, then the
13825 value returned by FN is returned directly. Otherwise, a node with
13826 the indicated EXPR_TYPE is returned, with operands corresponding to
13827 the two sub-expressions. */
13830 cp_parser_binary_expression (cp_parser* parser,
13831 const cp_parser_token_tree_map token_tree_map,
13832 cp_parser_expression_fn fn)
13836 /* Parse the first expression. */
13837 lhs = (*fn) (parser);
13838 /* Now, look for more expressions. */
13842 const cp_parser_token_tree_map_node *map_node;
13845 /* Peek at the next token. */
13846 token = cp_lexer_peek_token (parser->lexer);
13847 /* If the token is `>', and that's not an operator at the
13848 moment, then we're done. */
13849 if (token->type == CPP_GREATER
13850 && !parser->greater_than_is_operator_p)
13852 /* If we find one of the tokens we want, build the corresponding
13853 tree representation. */
13854 for (map_node = token_tree_map;
13855 map_node->token_type != CPP_EOF;
13857 if (map_node->token_type == token->type)
13859 /* Consume the operator token. */
13860 cp_lexer_consume_token (parser->lexer);
13861 /* Parse the right-hand side of the expression. */
13862 rhs = (*fn) (parser);
13863 /* Build the binary tree node. */
13864 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13868 /* If the token wasn't one of the ones we want, we're done. */
13869 if (map_node->token_type == CPP_EOF)
13876 /* Parse an optional `::' token indicating that the following name is
13877 from the global namespace. If so, PARSER->SCOPE is set to the
13878 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13879 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13880 Returns the new value of PARSER->SCOPE, if the `::' token is
13881 present, and NULL_TREE otherwise. */
13884 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13888 /* Peek at the next token. */
13889 token = cp_lexer_peek_token (parser->lexer);
13890 /* If we're looking at a `::' token then we're starting from the
13891 global namespace, not our current location. */
13892 if (token->type == CPP_SCOPE)
13894 /* Consume the `::' token. */
13895 cp_lexer_consume_token (parser->lexer);
13896 /* Set the SCOPE so that we know where to start the lookup. */
13897 parser->scope = global_namespace;
13898 parser->qualifying_scope = global_namespace;
13899 parser->object_scope = NULL_TREE;
13901 return parser->scope;
13903 else if (!current_scope_valid_p)
13905 parser->scope = NULL_TREE;
13906 parser->qualifying_scope = NULL_TREE;
13907 parser->object_scope = NULL_TREE;
13913 /* Returns TRUE if the upcoming token sequence is the start of a
13914 constructor declarator. If FRIEND_P is true, the declarator is
13915 preceded by the `friend' specifier. */
13918 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13920 bool constructor_p;
13921 tree type_decl = NULL_TREE;
13922 bool nested_name_p;
13923 cp_token *next_token;
13925 /* The common case is that this is not a constructor declarator, so
13926 try to avoid doing lots of work if at all possible. It's not
13927 valid declare a constructor at function scope. */
13928 if (at_function_scope_p ())
13930 /* And only certain tokens can begin a constructor declarator. */
13931 next_token = cp_lexer_peek_token (parser->lexer);
13932 if (next_token->type != CPP_NAME
13933 && next_token->type != CPP_SCOPE
13934 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13935 && next_token->type != CPP_TEMPLATE_ID)
13938 /* Parse tentatively; we are going to roll back all of the tokens
13940 cp_parser_parse_tentatively (parser);
13941 /* Assume that we are looking at a constructor declarator. */
13942 constructor_p = true;
13944 /* Look for the optional `::' operator. */
13945 cp_parser_global_scope_opt (parser,
13946 /*current_scope_valid_p=*/false);
13947 /* Look for the nested-name-specifier. */
13949 = (cp_parser_nested_name_specifier_opt (parser,
13950 /*typename_keyword_p=*/false,
13951 /*check_dependency_p=*/false,
13953 /*is_declaration=*/false)
13955 /* Outside of a class-specifier, there must be a
13956 nested-name-specifier. */
13957 if (!nested_name_p &&
13958 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13960 constructor_p = false;
13961 /* If we still think that this might be a constructor-declarator,
13962 look for a class-name. */
13967 template <typename T> struct S { S(); };
13968 template <typename T> S<T>::S ();
13970 we must recognize that the nested `S' names a class.
13973 template <typename T> S<T>::S<T> ();
13975 we must recognize that the nested `S' names a template. */
13976 type_decl = cp_parser_class_name (parser,
13977 /*typename_keyword_p=*/false,
13978 /*template_keyword_p=*/false,
13980 /*check_dependency_p=*/false,
13981 /*class_head_p=*/false,
13982 /*is_declaration=*/false);
13983 /* If there was no class-name, then this is not a constructor. */
13984 constructor_p = !cp_parser_error_occurred (parser);
13987 /* If we're still considering a constructor, we have to see a `(',
13988 to begin the parameter-declaration-clause, followed by either a
13989 `)', an `...', or a decl-specifier. We need to check for a
13990 type-specifier to avoid being fooled into thinking that:
13994 is a constructor. (It is actually a function named `f' that
13995 takes one parameter (of type `int') and returns a value of type
13998 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14001 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14002 && !cp_parser_storage_class_specifier_opt (parser))
14005 unsigned saved_num_template_parameter_lists;
14007 /* Names appearing in the type-specifier should be looked up
14008 in the scope of the class. */
14009 if (current_class_type)
14013 type = TREE_TYPE (type_decl);
14014 if (TREE_CODE (type) == TYPENAME_TYPE)
14016 type = resolve_typename_type (type,
14017 /*only_current_p=*/false);
14018 if (type == error_mark_node)
14020 cp_parser_abort_tentative_parse (parser);
14027 /* Inside the constructor parameter list, surrounding
14028 template-parameter-lists do not apply. */
14029 saved_num_template_parameter_lists
14030 = parser->num_template_parameter_lists;
14031 parser->num_template_parameter_lists = 0;
14033 /* Look for the type-specifier. */
14034 cp_parser_type_specifier (parser,
14035 CP_PARSER_FLAGS_NONE,
14036 /*is_friend=*/false,
14037 /*is_declarator=*/true,
14038 /*declares_class_or_enum=*/NULL,
14039 /*is_cv_qualifier=*/NULL);
14041 parser->num_template_parameter_lists
14042 = saved_num_template_parameter_lists;
14044 /* Leave the scope of the class. */
14048 constructor_p = !cp_parser_error_occurred (parser);
14052 constructor_p = false;
14053 /* We did not really want to consume any tokens. */
14054 cp_parser_abort_tentative_parse (parser);
14056 return constructor_p;
14059 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14060 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14061 they must be performed once we are in the scope of the function.
14063 Returns the function defined. */
14066 cp_parser_function_definition_from_specifiers_and_declarator
14067 (cp_parser* parser,
14068 tree decl_specifiers,
14075 /* Begin the function-definition. */
14076 success_p = begin_function_definition (decl_specifiers,
14080 /* If there were names looked up in the decl-specifier-seq that we
14081 did not check, check them now. We must wait until we are in the
14082 scope of the function to perform the checks, since the function
14083 might be a friend. */
14084 perform_deferred_access_checks ();
14088 /* If begin_function_definition didn't like the definition, skip
14089 the entire function. */
14090 error ("invalid function declaration");
14091 cp_parser_skip_to_end_of_block_or_statement (parser);
14092 fn = error_mark_node;
14095 fn = cp_parser_function_definition_after_declarator (parser,
14096 /*inline_p=*/false);
14101 /* Parse the part of a function-definition that follows the
14102 declarator. INLINE_P is TRUE iff this function is an inline
14103 function defined with a class-specifier.
14105 Returns the function defined. */
14108 cp_parser_function_definition_after_declarator (cp_parser* parser,
14112 bool ctor_initializer_p = false;
14113 bool saved_in_unbraced_linkage_specification_p;
14114 unsigned saved_num_template_parameter_lists;
14116 /* If the next token is `return', then the code may be trying to
14117 make use of the "named return value" extension that G++ used to
14119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14121 /* Consume the `return' keyword. */
14122 cp_lexer_consume_token (parser->lexer);
14123 /* Look for the identifier that indicates what value is to be
14125 cp_parser_identifier (parser);
14126 /* Issue an error message. */
14127 error ("named return values are no longer supported");
14128 /* Skip tokens until we reach the start of the function body. */
14129 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14130 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14131 cp_lexer_consume_token (parser->lexer);
14133 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14134 anything declared inside `f'. */
14135 saved_in_unbraced_linkage_specification_p
14136 = parser->in_unbraced_linkage_specification_p;
14137 parser->in_unbraced_linkage_specification_p = false;
14138 /* Inside the function, surrounding template-parameter-lists do not
14140 saved_num_template_parameter_lists
14141 = parser->num_template_parameter_lists;
14142 parser->num_template_parameter_lists = 0;
14143 /* If the next token is `try', then we are looking at a
14144 function-try-block. */
14145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14146 ctor_initializer_p = cp_parser_function_try_block (parser);
14147 /* A function-try-block includes the function-body, so we only do
14148 this next part if we're not processing a function-try-block. */
14151 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14153 /* Finish the function. */
14154 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14155 (inline_p ? 2 : 0));
14156 /* Generate code for it, if necessary. */
14157 expand_or_defer_fn (fn);
14158 /* Restore the saved values. */
14159 parser->in_unbraced_linkage_specification_p
14160 = saved_in_unbraced_linkage_specification_p;
14161 parser->num_template_parameter_lists
14162 = saved_num_template_parameter_lists;
14167 /* Parse a template-declaration, assuming that the `export' (and
14168 `extern') keywords, if present, has already been scanned. MEMBER_P
14169 is as for cp_parser_template_declaration. */
14172 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14174 tree decl = NULL_TREE;
14175 tree parameter_list;
14176 bool friend_p = false;
14178 /* Look for the `template' keyword. */
14179 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14183 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14186 /* If the next token is `>', then we have an invalid
14187 specialization. Rather than complain about an invalid template
14188 parameter, issue an error message here. */
14189 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14191 cp_parser_error (parser, "invalid explicit specialization");
14192 begin_specialization ();
14193 parameter_list = NULL_TREE;
14197 /* Parse the template parameters. */
14198 begin_template_parm_list ();
14199 parameter_list = cp_parser_template_parameter_list (parser);
14200 parameter_list = end_template_parm_list (parameter_list);
14203 /* Look for the `>'. */
14204 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14205 /* We just processed one more parameter list. */
14206 ++parser->num_template_parameter_lists;
14207 /* If the next token is `template', there are more template
14209 if (cp_lexer_next_token_is_keyword (parser->lexer,
14211 cp_parser_template_declaration_after_export (parser, member_p);
14214 decl = cp_parser_single_declaration (parser,
14218 /* If this is a member template declaration, let the front
14220 if (member_p && !friend_p && decl)
14222 if (TREE_CODE (decl) == TYPE_DECL)
14223 cp_parser_check_access_in_redeclaration (decl);
14225 decl = finish_member_template_decl (decl);
14227 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14228 make_friend_class (current_class_type, TREE_TYPE (decl),
14229 /*complain=*/true);
14231 /* We are done with the current parameter list. */
14232 --parser->num_template_parameter_lists;
14235 finish_template_decl (parameter_list);
14237 /* Register member declarations. */
14238 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14239 finish_member_declaration (decl);
14241 /* If DECL is a function template, we must return to parse it later.
14242 (Even though there is no definition, there might be default
14243 arguments that need handling.) */
14244 if (member_p && decl
14245 && (TREE_CODE (decl) == FUNCTION_DECL
14246 || DECL_FUNCTION_TEMPLATE_P (decl)))
14247 TREE_VALUE (parser->unparsed_functions_queues)
14248 = tree_cons (NULL_TREE, decl,
14249 TREE_VALUE (parser->unparsed_functions_queues));
14252 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14253 `function-definition' sequence. MEMBER_P is true, this declaration
14254 appears in a class scope.
14256 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14257 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14260 cp_parser_single_declaration (cp_parser* parser,
14264 int declares_class_or_enum;
14265 tree decl = NULL_TREE;
14266 tree decl_specifiers;
14268 bool function_definition_p = false;
14270 /* Defer access checks until we know what is being declared. */
14271 push_deferring_access_checks (dk_deferred);
14273 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14276 = cp_parser_decl_specifier_seq (parser,
14277 CP_PARSER_FLAGS_OPTIONAL,
14279 &declares_class_or_enum);
14281 *friend_p = cp_parser_friend_p (decl_specifiers);
14282 /* Gather up the access checks that occurred the
14283 decl-specifier-seq. */
14284 stop_deferring_access_checks ();
14286 /* Check for the declaration of a template class. */
14287 if (declares_class_or_enum)
14289 if (cp_parser_declares_only_class_p (parser))
14291 decl = shadow_tag (decl_specifiers);
14293 decl = TYPE_NAME (decl);
14295 decl = error_mark_node;
14300 /* If it's not a template class, try for a template function. If
14301 the next token is a `;', then this declaration does not declare
14302 anything. But, if there were errors in the decl-specifiers, then
14303 the error might well have come from an attempted class-specifier.
14304 In that case, there's no need to warn about a missing declarator. */
14306 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14307 || !value_member (error_mark_node, decl_specifiers)))
14308 decl = cp_parser_init_declarator (parser,
14311 /*function_definition_allowed_p=*/true,
14313 declares_class_or_enum,
14314 &function_definition_p);
14316 pop_deferring_access_checks ();
14318 /* Clear any current qualification; whatever comes next is the start
14319 of something new. */
14320 parser->scope = NULL_TREE;
14321 parser->qualifying_scope = NULL_TREE;
14322 parser->object_scope = NULL_TREE;
14323 /* Look for a trailing `;' after the declaration. */
14324 if (!function_definition_p
14325 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14326 cp_parser_skip_to_end_of_block_or_statement (parser);
14331 /* Parse a cast-expression that is not the operand of a unary "&". */
14334 cp_parser_simple_cast_expression (cp_parser *parser)
14336 return cp_parser_cast_expression (parser, /*address_p=*/false);
14339 /* Parse a functional cast to TYPE. Returns an expression
14340 representing the cast. */
14343 cp_parser_functional_cast (cp_parser* parser, tree type)
14345 tree expression_list;
14348 = cp_parser_parenthesized_expression_list (parser, false,
14349 /*non_constant_p=*/NULL);
14351 return build_functional_cast (type, expression_list);
14354 /* Save the tokens that make up the body of a member function defined
14355 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14356 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14357 specifiers applied to the declaration. Returns the FUNCTION_DECL
14358 for the member function. */
14361 cp_parser_save_member_function_body (cp_parser* parser,
14362 tree decl_specifiers,
14366 cp_token_cache *cache;
14369 /* Create the function-declaration. */
14370 fn = start_method (decl_specifiers, declarator, attributes);
14371 /* If something went badly wrong, bail out now. */
14372 if (fn == error_mark_node)
14374 /* If there's a function-body, skip it. */
14375 if (cp_parser_token_starts_function_definition_p
14376 (cp_lexer_peek_token (parser->lexer)))
14377 cp_parser_skip_to_end_of_block_or_statement (parser);
14378 return error_mark_node;
14381 /* Remember it, if there default args to post process. */
14382 cp_parser_save_default_args (parser, fn);
14384 /* Create a token cache. */
14385 cache = cp_token_cache_new ();
14386 /* Save away the tokens that make up the body of the
14388 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14389 /* Handle function try blocks. */
14390 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14391 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14393 /* Save away the inline definition; we will process it when the
14394 class is complete. */
14395 DECL_PENDING_INLINE_INFO (fn) = cache;
14396 DECL_PENDING_INLINE_P (fn) = 1;
14398 /* We need to know that this was defined in the class, so that
14399 friend templates are handled correctly. */
14400 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14402 /* We're done with the inline definition. */
14403 finish_method (fn);
14405 /* Add FN to the queue of functions to be parsed later. */
14406 TREE_VALUE (parser->unparsed_functions_queues)
14407 = tree_cons (NULL_TREE, fn,
14408 TREE_VALUE (parser->unparsed_functions_queues));
14413 /* Parse a template-argument-list, as well as the trailing ">" (but
14414 not the opening ">"). See cp_parser_template_argument_list for the
14418 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14422 tree saved_qualifying_scope;
14423 tree saved_object_scope;
14424 bool saved_greater_than_is_operator_p;
14428 When parsing a template-id, the first non-nested `>' is taken as
14429 the end of the template-argument-list rather than a greater-than
14431 saved_greater_than_is_operator_p
14432 = parser->greater_than_is_operator_p;
14433 parser->greater_than_is_operator_p = false;
14434 /* Parsing the argument list may modify SCOPE, so we save it
14436 saved_scope = parser->scope;
14437 saved_qualifying_scope = parser->qualifying_scope;
14438 saved_object_scope = parser->object_scope;
14439 /* Parse the template-argument-list itself. */
14440 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14441 arguments = NULL_TREE;
14443 arguments = cp_parser_template_argument_list (parser);
14444 /* Look for the `>' that ends the template-argument-list. If we find
14445 a '>>' instead, it's probably just a typo. */
14446 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14448 if (!saved_greater_than_is_operator_p)
14450 /* If we're in a nested template argument list, the '>>' has to be
14451 a typo for '> >'. We emit the error message, but we continue
14452 parsing and we push a '>' as next token, so that the argument
14453 list will be parsed correctly.. */
14455 error ("`>>' should be `> >' within a nested template argument list");
14456 token = cp_lexer_peek_token (parser->lexer);
14457 token->type = CPP_GREATER;
14461 /* If this is not a nested template argument list, the '>>' is
14462 a typo for '>'. Emit an error message and continue. */
14463 error ("spurious `>>', use `>' to terminate a template argument list");
14464 cp_lexer_consume_token (parser->lexer);
14468 cp_parser_require (parser, CPP_GREATER, "`>'");
14469 /* The `>' token might be a greater-than operator again now. */
14470 parser->greater_than_is_operator_p
14471 = saved_greater_than_is_operator_p;
14472 /* Restore the SAVED_SCOPE. */
14473 parser->scope = saved_scope;
14474 parser->qualifying_scope = saved_qualifying_scope;
14475 parser->object_scope = saved_object_scope;
14480 /* MEMBER_FUNCTION is a member function, or a friend. If default
14481 arguments, or the body of the function have not yet been parsed,
14485 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14487 cp_lexer *saved_lexer;
14489 /* If this member is a template, get the underlying
14491 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14492 member_function = DECL_TEMPLATE_RESULT (member_function);
14494 /* There should not be any class definitions in progress at this
14495 point; the bodies of members are only parsed outside of all class
14497 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14498 /* While we're parsing the member functions we might encounter more
14499 classes. We want to handle them right away, but we don't want
14500 them getting mixed up with functions that are currently in the
14502 parser->unparsed_functions_queues
14503 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14505 /* Make sure that any template parameters are in scope. */
14506 maybe_begin_member_template_processing (member_function);
14508 /* If the body of the function has not yet been parsed, parse it
14510 if (DECL_PENDING_INLINE_P (member_function))
14512 tree function_scope;
14513 cp_token_cache *tokens;
14515 /* The function is no longer pending; we are processing it. */
14516 tokens = DECL_PENDING_INLINE_INFO (member_function);
14517 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14518 DECL_PENDING_INLINE_P (member_function) = 0;
14519 /* If this was an inline function in a local class, enter the scope
14520 of the containing function. */
14521 function_scope = decl_function_context (member_function);
14522 if (function_scope)
14523 push_function_context_to (function_scope);
14525 /* Save away the current lexer. */
14526 saved_lexer = parser->lexer;
14527 /* Make a new lexer to feed us the tokens saved for this function. */
14528 parser->lexer = cp_lexer_new_from_tokens (tokens);
14529 parser->lexer->next = saved_lexer;
14531 /* Set the current source position to be the location of the first
14532 token in the saved inline body. */
14533 cp_lexer_peek_token (parser->lexer);
14535 /* Let the front end know that we going to be defining this
14537 start_function (NULL_TREE, member_function, NULL_TREE,
14538 SF_PRE_PARSED | SF_INCLASS_INLINE);
14540 /* Now, parse the body of the function. */
14541 cp_parser_function_definition_after_declarator (parser,
14542 /*inline_p=*/true);
14544 /* Leave the scope of the containing function. */
14545 if (function_scope)
14546 pop_function_context_from (function_scope);
14547 /* Restore the lexer. */
14548 parser->lexer = saved_lexer;
14551 /* Remove any template parameters from the symbol table. */
14552 maybe_end_member_template_processing ();
14554 /* Restore the queue. */
14555 parser->unparsed_functions_queues
14556 = TREE_CHAIN (parser->unparsed_functions_queues);
14559 /* If DECL contains any default args, remember it on the unparsed
14560 functions queue. */
14563 cp_parser_save_default_args (cp_parser* parser, tree decl)
14567 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14569 probe = TREE_CHAIN (probe))
14570 if (TREE_PURPOSE (probe))
14572 TREE_PURPOSE (parser->unparsed_functions_queues)
14573 = tree_cons (NULL_TREE, decl,
14574 TREE_PURPOSE (parser->unparsed_functions_queues));
14580 /* FN is a FUNCTION_DECL which may contains a parameter with an
14581 unparsed DEFAULT_ARG. Parse the default args now. */
14584 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14586 cp_lexer *saved_lexer;
14587 cp_token_cache *tokens;
14588 bool saved_local_variables_forbidden_p;
14591 /* While we're parsing the default args, we might (due to the
14592 statement expression extension) encounter more classes. We want
14593 to handle them right away, but we don't want them getting mixed
14594 up with default args that are currently in the queue. */
14595 parser->unparsed_functions_queues
14596 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14598 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14600 parameters = TREE_CHAIN (parameters))
14602 if (!TREE_PURPOSE (parameters)
14603 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14606 /* Save away the current lexer. */
14607 saved_lexer = parser->lexer;
14608 /* Create a new one, using the tokens we have saved. */
14609 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14610 parser->lexer = cp_lexer_new_from_tokens (tokens);
14612 /* Set the current source position to be the location of the
14613 first token in the default argument. */
14614 cp_lexer_peek_token (parser->lexer);
14616 /* Local variable names (and the `this' keyword) may not appear
14617 in a default argument. */
14618 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14619 parser->local_variables_forbidden_p = true;
14620 /* Parse the assignment-expression. */
14621 if (DECL_CLASS_SCOPE_P (fn))
14622 push_nested_class (DECL_CONTEXT (fn));
14623 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14624 if (DECL_CLASS_SCOPE_P (fn))
14625 pop_nested_class ();
14627 /* Restore saved state. */
14628 parser->lexer = saved_lexer;
14629 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14632 /* Restore the queue. */
14633 parser->unparsed_functions_queues
14634 = TREE_CHAIN (parser->unparsed_functions_queues);
14637 /* Parse the operand of `sizeof' (or a similar operator). Returns
14638 either a TYPE or an expression, depending on the form of the
14639 input. The KEYWORD indicates which kind of expression we have
14643 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14645 static const char *format;
14646 tree expr = NULL_TREE;
14647 const char *saved_message;
14648 bool saved_integral_constant_expression_p;
14650 /* Initialize FORMAT the first time we get here. */
14652 format = "types may not be defined in `%s' expressions";
14654 /* Types cannot be defined in a `sizeof' expression. Save away the
14656 saved_message = parser->type_definition_forbidden_message;
14657 /* And create the new one. */
14658 parser->type_definition_forbidden_message
14659 = xmalloc (strlen (format)
14660 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14662 sprintf ((char *) parser->type_definition_forbidden_message,
14663 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14665 /* The restrictions on constant-expressions do not apply inside
14666 sizeof expressions. */
14667 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14668 parser->integral_constant_expression_p = false;
14670 /* Do not actually evaluate the expression. */
14672 /* If it's a `(', then we might be looking at the type-id
14674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14677 bool saved_in_type_id_in_expr_p;
14679 /* We can't be sure yet whether we're looking at a type-id or an
14681 cp_parser_parse_tentatively (parser);
14682 /* Consume the `('. */
14683 cp_lexer_consume_token (parser->lexer);
14684 /* Parse the type-id. */
14685 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14686 parser->in_type_id_in_expr_p = true;
14687 type = cp_parser_type_id (parser);
14688 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14689 /* Now, look for the trailing `)'. */
14690 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14691 /* If all went well, then we're done. */
14692 if (cp_parser_parse_definitely (parser))
14694 /* Build a list of decl-specifiers; right now, we have only
14695 a single type-specifier. */
14696 type = build_tree_list (NULL_TREE,
14699 /* Call grokdeclarator to figure out what type this is. */
14700 expr = grokdeclarator (NULL_TREE,
14704 /*attrlist=*/NULL);
14708 /* If the type-id production did not work out, then we must be
14709 looking at the unary-expression production. */
14711 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14712 /* Go back to evaluating expressions. */
14715 /* Free the message we created. */
14716 free ((char *) parser->type_definition_forbidden_message);
14717 /* And restore the old one. */
14718 parser->type_definition_forbidden_message = saved_message;
14719 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14724 /* If the current declaration has no declarator, return true. */
14727 cp_parser_declares_only_class_p (cp_parser *parser)
14729 /* If the next token is a `;' or a `,' then there is no
14731 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14732 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14735 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14736 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14739 cp_parser_friend_p (tree decl_specifiers)
14741 while (decl_specifiers)
14743 /* See if this decl-specifier is `friend'. */
14744 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14745 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14748 /* Go on to the next decl-specifier. */
14749 decl_specifiers = TREE_CHAIN (decl_specifiers);
14755 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14756 issue an error message indicating that TOKEN_DESC was expected.
14758 Returns the token consumed, if the token had the appropriate type.
14759 Otherwise, returns NULL. */
14762 cp_parser_require (cp_parser* parser,
14763 enum cpp_ttype type,
14764 const char* token_desc)
14766 if (cp_lexer_next_token_is (parser->lexer, type))
14767 return cp_lexer_consume_token (parser->lexer);
14770 /* Output the MESSAGE -- unless we're parsing tentatively. */
14771 if (!cp_parser_simulate_error (parser))
14773 char *message = concat ("expected ", token_desc, NULL);
14774 cp_parser_error (parser, message);
14781 /* Like cp_parser_require, except that tokens will be skipped until
14782 the desired token is found. An error message is still produced if
14783 the next token is not as expected. */
14786 cp_parser_skip_until_found (cp_parser* parser,
14787 enum cpp_ttype type,
14788 const char* token_desc)
14791 unsigned nesting_depth = 0;
14793 if (cp_parser_require (parser, type, token_desc))
14796 /* Skip tokens until the desired token is found. */
14799 /* Peek at the next token. */
14800 token = cp_lexer_peek_token (parser->lexer);
14801 /* If we've reached the token we want, consume it and
14803 if (token->type == type && !nesting_depth)
14805 cp_lexer_consume_token (parser->lexer);
14808 /* If we've run out of tokens, stop. */
14809 if (token->type == CPP_EOF)
14811 if (token->type == CPP_OPEN_BRACE
14812 || token->type == CPP_OPEN_PAREN
14813 || token->type == CPP_OPEN_SQUARE)
14815 else if (token->type == CPP_CLOSE_BRACE
14816 || token->type == CPP_CLOSE_PAREN
14817 || token->type == CPP_CLOSE_SQUARE)
14819 if (nesting_depth-- == 0)
14822 /* Consume this token. */
14823 cp_lexer_consume_token (parser->lexer);
14827 /* If the next token is the indicated keyword, consume it. Otherwise,
14828 issue an error message indicating that TOKEN_DESC was expected.
14830 Returns the token consumed, if the token had the appropriate type.
14831 Otherwise, returns NULL. */
14834 cp_parser_require_keyword (cp_parser* parser,
14836 const char* token_desc)
14838 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14840 if (token && token->keyword != keyword)
14842 dyn_string_t error_msg;
14844 /* Format the error message. */
14845 error_msg = dyn_string_new (0);
14846 dyn_string_append_cstr (error_msg, "expected ");
14847 dyn_string_append_cstr (error_msg, token_desc);
14848 cp_parser_error (parser, error_msg->s);
14849 dyn_string_delete (error_msg);
14856 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14857 function-definition. */
14860 cp_parser_token_starts_function_definition_p (cp_token* token)
14862 return (/* An ordinary function-body begins with an `{'. */
14863 token->type == CPP_OPEN_BRACE
14864 /* A ctor-initializer begins with a `:'. */
14865 || token->type == CPP_COLON
14866 /* A function-try-block begins with `try'. */
14867 || token->keyword == RID_TRY
14868 /* The named return value extension begins with `return'. */
14869 || token->keyword == RID_RETURN);
14872 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14876 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14880 token = cp_lexer_peek_token (parser->lexer);
14881 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14884 /* Returns TRUE iff the next token is the "," or ">" ending a
14885 template-argument. ">>" is also accepted (after the full
14886 argument was parsed) because it's probably a typo for "> >",
14887 and there is a specific diagnostic for this. */
14890 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14894 token = cp_lexer_peek_token (parser->lexer);
14895 return (token->type == CPP_COMMA || token->type == CPP_GREATER
14896 || token->type == CPP_RSHIFT);
14899 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
14900 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
14903 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
14908 token = cp_lexer_peek_nth_token (parser->lexer, n);
14909 if (token->type == CPP_LESS)
14911 /* Check for the sequence `<::' in the original code. It would be lexed as
14912 `[:', where `[' is a digraph, and there is no whitespace before
14914 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
14917 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
14918 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
14924 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14925 or none_type otherwise. */
14927 static enum tag_types
14928 cp_parser_token_is_class_key (cp_token* token)
14930 switch (token->keyword)
14935 return record_type;
14944 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14947 cp_parser_check_class_key (enum tag_types class_key, tree type)
14949 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14950 pedwarn ("`%s' tag used in naming `%#T'",
14951 class_key == union_type ? "union"
14952 : class_key == record_type ? "struct" : "class",
14956 /* Issue an error message if DECL is redeclared with different
14957 access than its original declaration [class.access.spec/3].
14958 This applies to nested classes and nested class templates.
14961 static void cp_parser_check_access_in_redeclaration (tree decl)
14963 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14966 if ((TREE_PRIVATE (decl)
14967 != (current_access_specifier == access_private_node))
14968 || (TREE_PROTECTED (decl)
14969 != (current_access_specifier == access_protected_node)))
14970 error ("%D redeclared with different access", decl);
14973 /* Look for the `template' keyword, as a syntactic disambiguator.
14974 Return TRUE iff it is present, in which case it will be
14978 cp_parser_optional_template_keyword (cp_parser *parser)
14980 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14982 /* The `template' keyword can only be used within templates;
14983 outside templates the parser can always figure out what is a
14984 template and what is not. */
14985 if (!processing_template_decl)
14987 error ("`template' (as a disambiguator) is only allowed "
14988 "within templates");
14989 /* If this part of the token stream is rescanned, the same
14990 error message would be generated. So, we purge the token
14991 from the stream. */
14992 cp_lexer_purge_token (parser->lexer);
14997 /* Consume the `template' keyword. */
14998 cp_lexer_consume_token (parser->lexer);
15006 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15007 set PARSER->SCOPE, and perform other related actions. */
15010 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15015 /* Get the stored value. */
15016 value = cp_lexer_consume_token (parser->lexer)->value;
15017 /* Perform any access checks that were deferred. */
15018 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15019 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15020 /* Set the scope from the stored value. */
15021 parser->scope = TREE_VALUE (value);
15022 parser->qualifying_scope = TREE_TYPE (value);
15023 parser->object_scope = NULL_TREE;
15026 /* Add tokens to CACHE until a non-nested END token appears. */
15029 cp_parser_cache_group (cp_parser *parser,
15030 cp_token_cache *cache,
15031 enum cpp_ttype end,
15038 /* Abort a parenthesized expression if we encounter a brace. */
15039 if ((end == CPP_CLOSE_PAREN || depth == 0)
15040 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15042 /* If we've reached the end of the file, stop. */
15043 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15045 /* Consume the next token. */
15046 token = cp_lexer_consume_token (parser->lexer);
15047 /* Add this token to the tokens we are saving. */
15048 cp_token_cache_push_token (cache, token);
15049 /* See if it starts a new group. */
15050 if (token->type == CPP_OPEN_BRACE)
15052 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15056 else if (token->type == CPP_OPEN_PAREN)
15057 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15058 else if (token->type == end)
15063 /* Begin parsing tentatively. We always save tokens while parsing
15064 tentatively so that if the tentative parsing fails we can restore the
15068 cp_parser_parse_tentatively (cp_parser* parser)
15070 /* Enter a new parsing context. */
15071 parser->context = cp_parser_context_new (parser->context);
15072 /* Begin saving tokens. */
15073 cp_lexer_save_tokens (parser->lexer);
15074 /* In order to avoid repetitive access control error messages,
15075 access checks are queued up until we are no longer parsing
15077 push_deferring_access_checks (dk_deferred);
15080 /* Commit to the currently active tentative parse. */
15083 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15085 cp_parser_context *context;
15088 /* Mark all of the levels as committed. */
15089 lexer = parser->lexer;
15090 for (context = parser->context; context->next; context = context->next)
15092 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15094 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15095 while (!cp_lexer_saving_tokens (lexer))
15096 lexer = lexer->next;
15097 cp_lexer_commit_tokens (lexer);
15101 /* Abort the currently active tentative parse. All consumed tokens
15102 will be rolled back, and no diagnostics will be issued. */
15105 cp_parser_abort_tentative_parse (cp_parser* parser)
15107 cp_parser_simulate_error (parser);
15108 /* Now, pretend that we want to see if the construct was
15109 successfully parsed. */
15110 cp_parser_parse_definitely (parser);
15113 /* Stop parsing tentatively. If a parse error has occurred, restore the
15114 token stream. Otherwise, commit to the tokens we have consumed.
15115 Returns true if no error occurred; false otherwise. */
15118 cp_parser_parse_definitely (cp_parser* parser)
15120 bool error_occurred;
15121 cp_parser_context *context;
15123 /* Remember whether or not an error occurred, since we are about to
15124 destroy that information. */
15125 error_occurred = cp_parser_error_occurred (parser);
15126 /* Remove the topmost context from the stack. */
15127 context = parser->context;
15128 parser->context = context->next;
15129 /* If no parse errors occurred, commit to the tentative parse. */
15130 if (!error_occurred)
15132 /* Commit to the tokens read tentatively, unless that was
15134 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15135 cp_lexer_commit_tokens (parser->lexer);
15137 pop_to_parent_deferring_access_checks ();
15139 /* Otherwise, if errors occurred, roll back our state so that things
15140 are just as they were before we began the tentative parse. */
15143 cp_lexer_rollback_tokens (parser->lexer);
15144 pop_deferring_access_checks ();
15146 /* Add the context to the front of the free list. */
15147 context->next = cp_parser_context_free_list;
15148 cp_parser_context_free_list = context;
15150 return !error_occurred;
15153 /* Returns true if we are parsing tentatively -- but have decided that
15154 we will stick with this tentative parse, even if errors occur. */
15157 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15159 return (cp_parser_parsing_tentatively (parser)
15160 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15163 /* Returns nonzero iff an error has occurred during the most recent
15164 tentative parse. */
15167 cp_parser_error_occurred (cp_parser* parser)
15169 return (cp_parser_parsing_tentatively (parser)
15170 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15173 /* Returns nonzero if GNU extensions are allowed. */
15176 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15178 return parser->allow_gnu_extensions_p;
15185 static GTY (()) cp_parser *the_parser;
15187 /* External interface. */
15189 /* Parse one entire translation unit. */
15192 c_parse_file (void)
15194 bool error_occurred;
15196 the_parser = cp_parser_new ();
15197 push_deferring_access_checks (flag_access_control
15198 ? dk_no_deferred : dk_no_check);
15199 error_occurred = cp_parser_translation_unit (the_parser);
15203 /* Clean up after parsing the entire translation unit. */
15206 free_parser_stacks (void)
15208 /* Nothing to do. */
15211 /* This variable must be provided by every front end. */
15215 #include "gt-cp-parser.h"