2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
51 We use a circular buffer to store incoming tokens.
53 Some artifacts of the C++ language (such as the
54 expression/declaration ambiguity) require arbitrary look-ahead.
55 The strategy we adopt for dealing with these problems is to attempt
56 to parse one construct (e.g., the declaration) and fall back to the
57 other (e.g., the expression) if that attempt does not succeed.
58 Therefore, we must sometimes store an arbitrary number of tokens.
60 The parser routinely peeks at the next token, and then consumes it
61 later. That also requires a buffer in which to store the tokens.
63 In order to easily permit adding tokens to the end of the buffer,
64 while removing them from the beginning of the buffer, we use a
69 typedef struct cp_token GTY (())
71 /* The kind of token. */
72 ENUM_BITFIELD (cpp_ttype) type : 8;
73 /* If this token is a keyword, this value indicates which keyword.
74 Otherwise, this value is RID_MAX. */
75 ENUM_BITFIELD (rid) keyword : 8;
78 /* The value associated with this token, if any. */
80 /* The location at which this token was found. */
84 /* The number of tokens in a single token block.
85 Computed so that cp_token_block fits in a 512B allocation unit. */
87 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
89 /* A group of tokens. These groups are chained together to store
90 large numbers of tokens. (For example, a token block is created
91 when the body of an inline member function is first encountered;
92 the tokens are processed later after the class definition is
95 This somewhat ungainly data structure (as opposed to, say, a
96 variable-length array), is used due to constraints imposed by the
97 current garbage-collection methodology. If it is made more
98 flexible, we could perhaps simplify the data structures involved. */
100 typedef struct cp_token_block GTY (())
103 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
104 /* The number of tokens in this block. */
106 /* The next token block in the chain. */
107 struct cp_token_block *next;
108 /* The previous block in the chain. */
109 struct cp_token_block *prev;
112 typedef struct cp_token_cache GTY (())
114 /* The first block in the cache. NULL if there are no tokens in the
116 cp_token_block *first;
117 /* The last block in the cache. NULL If there are no tokens in the
119 cp_token_block *last;
124 static cp_token_cache *cp_token_cache_new
126 static void cp_token_cache_push_token
127 (cp_token_cache *, cp_token *);
129 /* Create a new cp_token_cache. */
131 static cp_token_cache *
132 cp_token_cache_new (void)
134 return GGC_CNEW (cp_token_cache);
137 /* Add *TOKEN to *CACHE. */
140 cp_token_cache_push_token (cp_token_cache *cache,
143 cp_token_block *b = cache->last;
145 /* See if we need to allocate a new token block. */
146 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
148 b = GGC_CNEW (cp_token_block);
149 b->prev = cache->last;
152 cache->last->next = b;
156 cache->first = cache->last = b;
158 /* Add this token to the current token block. */
159 b->tokens[b->num_tokens++] = *token;
162 /* The cp_lexer structure represents the C++ lexer. It is responsible
163 for managing the token stream from the preprocessor and supplying
166 typedef struct cp_lexer GTY (())
168 /* The memory allocated for the buffer. Never NULL. */
169 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
170 /* A pointer just past the end of the memory allocated for the buffer. */
171 cp_token * GTY ((skip)) buffer_end;
172 /* The first valid token in the buffer, or NULL if none. */
173 cp_token * GTY ((skip)) first_token;
174 /* The next available token. If NEXT_TOKEN is NULL, then there are
175 no more available tokens. */
176 cp_token * GTY ((skip)) next_token;
177 /* A pointer just past the last available token. If FIRST_TOKEN is
178 NULL, however, there are no available tokens, and then this
179 location is simply the place in which the next token read will be
180 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
181 When the LAST_TOKEN == BUFFER, then the last token is at the
182 highest memory address in the BUFFER. */
183 cp_token * GTY ((skip)) last_token;
185 /* A stack indicating positions at which cp_lexer_save_tokens was
186 called. The top entry is the most recent position at which we
187 began saving tokens. The entries are differences in token
188 position between FIRST_TOKEN and the first saved token.
190 If the stack is non-empty, we are saving tokens. When a token is
191 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
192 pointer will not. The token stream will be preserved so that it
193 can be reexamined later.
195 If the stack is empty, then we are not saving tokens. Whenever a
196 token is consumed, the FIRST_TOKEN pointer will be moved, and the
197 consumed token will be gone forever. */
198 varray_type saved_tokens;
200 /* The STRING_CST tokens encountered while processing the current
202 varray_type string_tokens;
204 /* True if we should obtain more tokens from the preprocessor; false
205 if we are processing a saved token cache. */
208 /* True if we should output debugging information. */
211 /* The next lexer in a linked list of lexers. */
212 struct cp_lexer *next;
217 static cp_lexer *cp_lexer_new_main
219 static cp_lexer *cp_lexer_new_from_tokens
220 (struct cp_token_cache *);
221 static int cp_lexer_saving_tokens
223 static cp_token *cp_lexer_next_token
224 (cp_lexer *, cp_token *);
225 static cp_token *cp_lexer_prev_token
226 (cp_lexer *, cp_token *);
227 static ptrdiff_t cp_lexer_token_difference
228 (cp_lexer *, cp_token *, cp_token *);
229 static cp_token *cp_lexer_read_token
231 static void cp_lexer_maybe_grow_buffer
233 static void cp_lexer_get_preprocessor_token
234 (cp_lexer *, cp_token *);
235 static cp_token *cp_lexer_peek_token
237 static cp_token *cp_lexer_peek_nth_token
238 (cp_lexer *, size_t);
239 static inline bool cp_lexer_next_token_is
240 (cp_lexer *, enum cpp_ttype);
241 static bool cp_lexer_next_token_is_not
242 (cp_lexer *, enum cpp_ttype);
243 static bool cp_lexer_next_token_is_keyword
244 (cp_lexer *, enum rid);
245 static cp_token *cp_lexer_consume_token
247 static void cp_lexer_purge_token
249 static void cp_lexer_purge_tokens_after
250 (cp_lexer *, cp_token *);
251 static void cp_lexer_save_tokens
253 static void cp_lexer_commit_tokens
255 static void cp_lexer_rollback_tokens
257 static inline void cp_lexer_set_source_position_from_token
258 (cp_lexer *, const cp_token *);
259 #ifdef ENABLE_CHECKING
260 static void cp_lexer_print_token
261 (FILE *, cp_token *);
262 static inline bool cp_lexer_debugging_p
264 static void cp_lexer_start_debugging
265 (cp_lexer *) ATTRIBUTE_UNUSED;
266 static void cp_lexer_stop_debugging
267 (cp_lexer *) ATTRIBUTE_UNUSED;
269 #define cp_lexer_debug_stream NULL
270 #define cp_lexer_print_token(str, tok)
271 #define cp_lexer_debugging_p(lexer) 0
272 #endif /* ENABLE_CHECKING */
274 /* Manifest constants. */
276 #define CP_TOKEN_BUFFER_SIZE 5
277 #define CP_SAVED_TOKENS_SIZE 5
279 /* A token type for keywords, as opposed to ordinary identifiers. */
280 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
282 /* A token type for template-ids. If a template-id is processed while
283 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
284 the value of the CPP_TEMPLATE_ID is whatever was returned by
285 cp_parser_template_id. */
286 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
288 /* A token type for nested-name-specifiers. If a
289 nested-name-specifier is processed while parsing tentatively, it is
290 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
291 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
292 cp_parser_nested_name_specifier_opt. */
293 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
295 /* A token type for tokens that are not tokens at all; these are used
296 to mark the end of a token block. */
297 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
301 #ifdef ENABLE_CHECKING
302 /* The stream to which debugging output should be written. */
303 static FILE *cp_lexer_debug_stream;
304 #endif /* ENABLE_CHECKING */
306 /* Create a new main C++ lexer, the lexer that gets tokens from the
310 cp_lexer_new_main (void)
313 cp_token first_token;
315 /* It's possible that lexing the first token will load a PCH file,
316 which is a GC collection point. So we have to grab the first
317 token before allocating any memory. */
318 cp_lexer_get_preprocessor_token (NULL, &first_token);
319 c_common_no_more_pch ();
321 /* Allocate the memory. */
322 lexer = GGC_CNEW (cp_lexer);
324 /* Create the circular buffer. */
325 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
326 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
328 /* There is one token in the buffer. */
329 lexer->last_token = lexer->buffer + 1;
330 lexer->first_token = lexer->buffer;
331 lexer->next_token = lexer->buffer;
332 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
334 /* This lexer obtains more tokens by calling c_lex. */
335 lexer->main_lexer_p = true;
337 /* Create the SAVED_TOKENS stack. */
338 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
340 /* Create the STRINGS array. */
341 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
343 #ifdef ENABLE_CHECKING
344 /* Assume we are not debugging. */
345 lexer->debugging_p = false;
346 #endif /* ENABLE_CHECKING */
351 /* Create a new lexer whose token stream is primed with the TOKENS.
352 When these tokens are exhausted, no new tokens will be read. */
355 cp_lexer_new_from_tokens (cp_token_cache *tokens)
359 cp_token_block *block;
360 ptrdiff_t num_tokens;
362 /* Allocate the memory. */
363 lexer = GGC_CNEW (cp_lexer);
365 /* Create a new buffer, appropriately sized. */
367 for (block = tokens->first; block != NULL; block = block->next)
368 num_tokens += block->num_tokens;
369 lexer->buffer = GGC_NEWVEC (cp_token, num_tokens);
370 lexer->buffer_end = lexer->buffer + num_tokens;
372 /* Install the tokens. */
373 token = lexer->buffer;
374 for (block = tokens->first; block != NULL; block = block->next)
376 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
377 token += block->num_tokens;
380 /* The FIRST_TOKEN is the beginning of the buffer. */
381 lexer->first_token = lexer->buffer;
382 /* The next available token is also at the beginning of the buffer. */
383 lexer->next_token = lexer->buffer;
384 /* The buffer is full. */
385 lexer->last_token = lexer->first_token;
387 /* This lexer doesn't obtain more tokens. */
388 lexer->main_lexer_p = false;
390 /* Create the SAVED_TOKENS stack. */
391 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
393 /* Create the STRINGS array. */
394 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
396 #ifdef ENABLE_CHECKING
397 /* Assume we are not debugging. */
398 lexer->debugging_p = false;
399 #endif /* ENABLE_CHECKING */
404 /* Returns nonzero if debugging information should be output. */
406 #ifdef ENABLE_CHECKING
409 cp_lexer_debugging_p (cp_lexer *lexer)
411 return lexer->debugging_p;
414 #endif /* ENABLE_CHECKING */
416 /* Set the current source position from the information stored in
420 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
421 const cp_token *token)
423 /* Ideally, the source position information would not be a global
424 variable, but it is. */
426 /* Update the line number. */
427 if (token->type != CPP_EOF)
428 input_location = token->location;
431 /* TOKEN points into the circular token buffer. Return a pointer to
432 the next token in the buffer. */
434 static inline cp_token *
435 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
438 if (token == lexer->buffer_end)
439 token = lexer->buffer;
443 /* TOKEN points into the circular token buffer. Return a pointer to
444 the previous token in the buffer. */
446 static inline cp_token *
447 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
449 if (token == lexer->buffer)
450 token = lexer->buffer_end;
454 /* nonzero if we are presently saving tokens. */
457 cp_lexer_saving_tokens (const cp_lexer* lexer)
459 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
462 /* Return a pointer to the token that is N tokens beyond TOKEN in the
466 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
469 if (token >= lexer->buffer_end)
470 token = lexer->buffer + (token - lexer->buffer_end);
474 /* Returns the number of times that START would have to be incremented
475 to reach FINISH. If START and FINISH are the same, returns zero. */
478 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
481 return finish - start;
483 return ((lexer->buffer_end - lexer->buffer)
487 /* Obtain another token from the C preprocessor and add it to the
488 token buffer. Returns the newly read token. */
491 cp_lexer_read_token (cp_lexer* lexer)
495 /* Make sure there is room in the buffer. */
496 cp_lexer_maybe_grow_buffer (lexer);
498 /* If there weren't any tokens, then this one will be the first. */
499 if (!lexer->first_token)
500 lexer->first_token = lexer->last_token;
501 /* Similarly, if there were no available tokens, there is one now. */
502 if (!lexer->next_token)
503 lexer->next_token = lexer->last_token;
505 /* Figure out where we're going to store the new token. */
506 token = lexer->last_token;
508 /* Get a new token from the preprocessor. */
509 cp_lexer_get_preprocessor_token (lexer, token);
511 /* Increment LAST_TOKEN. */
512 lexer->last_token = cp_lexer_next_token (lexer, token);
514 /* Strings should have type `const char []'. Right now, we will
515 have an ARRAY_TYPE that is constant rather than an array of
517 FIXME: Make fix_string_type get this right in the first place. */
518 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
519 && flag_const_strings)
521 if (c_lex_string_translate)
523 tree value = token->value;
526 /* We might as well go ahead and release the chained
527 translated string such that we can reuse its memory. */
528 if (TREE_CHAIN (value))
529 value = TREE_CHAIN (token->value);
531 /* Get the current type. It will be an ARRAY_TYPE. */
532 type = TREE_TYPE (value);
533 /* Use build_cplus_array_type to rebuild the array, thereby
534 getting the right type. */
535 type = build_cplus_array_type (TREE_TYPE (type),
537 /* Reset the type of the token. */
538 TREE_TYPE (value) = type;
545 /* If the circular buffer is full, make it bigger. */
548 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
550 /* If the buffer is full, enlarge it. */
551 if (lexer->last_token == lexer->first_token)
553 cp_token *new_buffer;
554 cp_token *old_buffer;
555 cp_token *new_first_token;
556 ptrdiff_t buffer_length;
557 size_t num_tokens_to_copy;
559 /* Remember the current buffer pointer. It will become invalid,
560 but we will need to do pointer arithmetic involving this
562 old_buffer = lexer->buffer;
563 /* Compute the current buffer size. */
564 buffer_length = lexer->buffer_end - lexer->buffer;
565 /* Allocate a buffer twice as big. */
566 new_buffer = ggc_realloc (lexer->buffer,
567 2 * buffer_length * sizeof (cp_token));
569 /* Because the buffer is circular, logically consecutive tokens
570 are not necessarily placed consecutively in memory.
571 Therefore, we must keep move the tokens that were before
572 FIRST_TOKEN to the second half of the newly allocated
574 num_tokens_to_copy = (lexer->first_token - old_buffer);
575 memcpy (new_buffer + buffer_length,
577 num_tokens_to_copy * sizeof (cp_token));
578 /* Clear the rest of the buffer. We never look at this storage,
579 but the garbage collector may. */
580 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
581 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
583 /* Now recompute all of the buffer pointers. */
585 = new_buffer + (lexer->first_token - old_buffer);
586 if (lexer->next_token != NULL)
588 ptrdiff_t next_token_delta;
590 if (lexer->next_token > lexer->first_token)
591 next_token_delta = lexer->next_token - lexer->first_token;
594 buffer_length - (lexer->first_token - lexer->next_token);
595 lexer->next_token = new_first_token + next_token_delta;
597 lexer->last_token = new_first_token + buffer_length;
598 lexer->buffer = new_buffer;
599 lexer->buffer_end = new_buffer + buffer_length * 2;
600 lexer->first_token = new_first_token;
604 /* Store the next token from the preprocessor in *TOKEN. */
607 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
612 /* If this not the main lexer, return a terminating CPP_EOF token. */
613 if (lexer != NULL && !lexer->main_lexer_p)
615 token->type = CPP_EOF;
616 token->location = UNKNOWN_LOCATION;
617 token->value = NULL_TREE;
618 token->keyword = RID_MAX;
624 /* Keep going until we get a token we like. */
627 /* Get a new token from the preprocessor. */
628 token->type = c_lex_with_flags (&token->value, &token->flags);
629 /* Issue messages about tokens we cannot process. */
635 error ("invalid token");
639 /* This is a good token, so we exit the loop. */
644 /* Now we've got our token. */
645 token->location = input_location;
647 /* Check to see if this token is a keyword. */
648 if (token->type == CPP_NAME
649 && C_IS_RESERVED_WORD (token->value))
651 /* Mark this token as a keyword. */
652 token->type = CPP_KEYWORD;
653 /* Record which keyword. */
654 token->keyword = C_RID_CODE (token->value);
655 /* Update the value. Some keywords are mapped to particular
656 entities, rather than simply having the value of the
657 corresponding IDENTIFIER_NODE. For example, `__const' is
658 mapped to `const'. */
659 token->value = ridpointers[token->keyword];
662 token->keyword = RID_MAX;
665 /* Return a pointer to the next token in the token stream, but do not
669 cp_lexer_peek_token (cp_lexer* lexer)
673 /* If there are no tokens, read one now. */
674 if (!lexer->next_token)
675 cp_lexer_read_token (lexer);
677 /* Provide debugging output. */
678 if (cp_lexer_debugging_p (lexer))
680 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
681 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
682 fprintf (cp_lexer_debug_stream, "\n");
685 token = lexer->next_token;
686 cp_lexer_set_source_position_from_token (lexer, token);
690 /* Return true if the next token has the indicated TYPE. */
693 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
697 /* Peek at the next token. */
698 token = cp_lexer_peek_token (lexer);
699 /* Check to see if it has the indicated TYPE. */
700 return token->type == type;
703 /* Return true if the next token does not have the indicated TYPE. */
706 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
708 return !cp_lexer_next_token_is (lexer, type);
711 /* Return true if the next token is the indicated KEYWORD. */
714 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
718 /* Peek at the next token. */
719 token = cp_lexer_peek_token (lexer);
720 /* Check to see if it is the indicated keyword. */
721 return token->keyword == keyword;
724 /* Return a pointer to the Nth token in the token stream. If N is 1,
725 then this is precisely equivalent to cp_lexer_peek_token. */
728 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
732 /* N is 1-based, not zero-based. */
735 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
736 token = lexer->next_token;
737 /* If there are no tokens in the buffer, get one now. */
740 cp_lexer_read_token (lexer);
741 token = lexer->next_token;
744 /* Now, read tokens until we have enough. */
747 /* Advance to the next token. */
748 token = cp_lexer_next_token (lexer, token);
749 /* If that's all the tokens we have, read a new one. */
750 if (token == lexer->last_token)
751 token = cp_lexer_read_token (lexer);
757 /* Consume the next token. The pointer returned is valid only until
758 another token is read. Callers should preserve copy the token
759 explicitly if they will need its value for a longer period of
763 cp_lexer_consume_token (cp_lexer* lexer)
767 /* If there are no tokens, read one now. */
768 if (!lexer->next_token)
769 cp_lexer_read_token (lexer);
771 /* Remember the token we'll be returning. */
772 token = lexer->next_token;
774 /* Increment NEXT_TOKEN. */
775 lexer->next_token = cp_lexer_next_token (lexer,
777 /* Check to see if we're all out of tokens. */
778 if (lexer->next_token == lexer->last_token)
779 lexer->next_token = NULL;
781 /* If we're not saving tokens, then move FIRST_TOKEN too. */
782 if (!cp_lexer_saving_tokens (lexer))
784 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
785 if (!lexer->next_token)
786 lexer->first_token = NULL;
788 lexer->first_token = lexer->next_token;
791 /* Provide debugging output. */
792 if (cp_lexer_debugging_p (lexer))
794 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
795 cp_lexer_print_token (cp_lexer_debug_stream, token);
796 fprintf (cp_lexer_debug_stream, "\n");
802 /* Permanently remove the next token from the token stream. There
803 must be a valid next token already; this token never reads
804 additional tokens from the preprocessor. */
807 cp_lexer_purge_token (cp_lexer *lexer)
810 cp_token *next_token;
812 token = lexer->next_token;
815 next_token = cp_lexer_next_token (lexer, token);
816 if (next_token == lexer->last_token)
818 *token = *next_token;
822 lexer->last_token = token;
823 /* The token purged may have been the only token remaining; if so,
825 if (lexer->next_token == token)
826 lexer->next_token = NULL;
829 /* Permanently remove all tokens after TOKEN, up to, but not
830 including, the token that will be returned next by
831 cp_lexer_peek_token. */
834 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
840 if (lexer->next_token)
842 /* Copy the tokens that have not yet been read to the location
843 immediately following TOKEN. */
844 t1 = cp_lexer_next_token (lexer, token);
845 t2 = peek = cp_lexer_peek_token (lexer);
846 /* Move tokens into the vacant area between TOKEN and PEEK. */
847 while (t2 != lexer->last_token)
850 t1 = cp_lexer_next_token (lexer, t1);
851 t2 = cp_lexer_next_token (lexer, t2);
853 /* Now, the next available token is right after TOKEN. */
854 lexer->next_token = cp_lexer_next_token (lexer, token);
855 /* And the last token is wherever we ended up. */
856 lexer->last_token = t1;
860 /* There are no tokens in the buffer, so there is nothing to
861 copy. The last token in the buffer is TOKEN itself. */
862 lexer->last_token = cp_lexer_next_token (lexer, token);
866 /* Begin saving tokens. All tokens consumed after this point will be
870 cp_lexer_save_tokens (cp_lexer* lexer)
872 /* Provide debugging output. */
873 if (cp_lexer_debugging_p (lexer))
874 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
876 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
877 restore the tokens if required. */
878 if (!lexer->next_token)
879 cp_lexer_read_token (lexer);
881 VARRAY_PUSH_INT (lexer->saved_tokens,
882 cp_lexer_token_difference (lexer,
887 /* Commit to the portion of the token stream most recently saved. */
890 cp_lexer_commit_tokens (cp_lexer* lexer)
892 /* Provide debugging output. */
893 if (cp_lexer_debugging_p (lexer))
894 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
896 VARRAY_POP (lexer->saved_tokens);
899 /* Return all tokens saved since the last call to cp_lexer_save_tokens
900 to the token stream. Stop saving tokens. */
903 cp_lexer_rollback_tokens (cp_lexer* lexer)
907 /* Provide debugging output. */
908 if (cp_lexer_debugging_p (lexer))
909 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
911 /* Find the token that was the NEXT_TOKEN when we started saving
913 delta = VARRAY_TOP_INT(lexer->saved_tokens);
914 /* Make it the next token again now. */
915 lexer->next_token = cp_lexer_advance_token (lexer,
918 /* It might be the case that there were no tokens when we started
919 saving tokens, but that there are some tokens now. */
920 if (!lexer->next_token && lexer->first_token)
921 lexer->next_token = lexer->first_token;
923 /* Stop saving tokens. */
924 VARRAY_POP (lexer->saved_tokens);
927 /* Print a representation of the TOKEN on the STREAM. */
929 #ifdef ENABLE_CHECKING
932 cp_lexer_print_token (FILE * stream, cp_token* token)
934 const char *token_type = NULL;
936 /* Figure out what kind of token this is. */
944 token_type = "COMMA";
948 token_type = "OPEN_PAREN";
951 case CPP_CLOSE_PAREN:
952 token_type = "CLOSE_PAREN";
956 token_type = "OPEN_BRACE";
959 case CPP_CLOSE_BRACE:
960 token_type = "CLOSE_BRACE";
964 token_type = "SEMICOLON";
976 token_type = "keyword";
979 /* This is not a token that we know how to handle yet. */
984 /* If we have a name for the token, print it out. Otherwise, we
985 simply give the numeric code. */
987 fprintf (stream, "%s", token_type);
989 fprintf (stream, "%d", token->type);
990 /* And, for an identifier, print the identifier name. */
991 if (token->type == CPP_NAME
992 /* Some keywords have a value that is not an IDENTIFIER_NODE.
993 For example, `struct' is mapped to an INTEGER_CST. */
994 || (token->type == CPP_KEYWORD
995 && TREE_CODE (token->value) == IDENTIFIER_NODE))
996 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
999 /* Start emitting debugging information. */
1002 cp_lexer_start_debugging (cp_lexer* lexer)
1004 ++lexer->debugging_p;
1007 /* Stop emitting debugging information. */
1010 cp_lexer_stop_debugging (cp_lexer* lexer)
1012 --lexer->debugging_p;
1015 #endif /* ENABLE_CHECKING */
1018 /* Decl-specifiers. */
1020 static void clear_decl_specs
1021 (cp_decl_specifier_seq *);
1023 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1026 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1028 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1033 /* Nothing other than the parser should be creating declarators;
1034 declarators are a semi-syntactic representation of C++ entities.
1035 Other parts of the front end that need to create entities (like
1036 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1038 static cp_declarator *make_id_declarator
1040 static cp_declarator *make_call_declarator
1041 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
1042 static cp_declarator *make_array_declarator
1043 (cp_declarator *, tree);
1044 static cp_declarator *make_pointer_declarator
1045 (cp_cv_quals, cp_declarator *);
1046 static cp_declarator *make_reference_declarator
1047 (cp_cv_quals, cp_declarator *);
1048 static cp_parameter_declarator *make_parameter_declarator
1049 (cp_decl_specifier_seq *, cp_declarator *, tree);
1050 static cp_declarator *make_ptrmem_declarator
1051 (cp_cv_quals, tree, cp_declarator *);
1053 cp_declarator *cp_error_declarator;
1055 /* The obstack on which declarators and related data structures are
1057 static struct obstack declarator_obstack;
1059 /* Alloc BYTES from the declarator memory pool. */
1061 static inline void *
1062 alloc_declarator (size_t bytes)
1064 return obstack_alloc (&declarator_obstack, bytes);
1067 /* Allocate a declarator of the indicated KIND. Clear fields that are
1068 common to all declarators. */
1070 static cp_declarator *
1071 make_declarator (cp_declarator_kind kind)
1073 cp_declarator *declarator;
1075 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1076 declarator->kind = kind;
1077 declarator->attributes = NULL_TREE;
1078 declarator->declarator = NULL;
1083 /* Make a declarator for a generalized identifier. */
1086 make_id_declarator (tree id)
1088 cp_declarator *declarator;
1090 declarator = make_declarator (cdk_id);
1091 declarator->u.id.name = id;
1092 declarator->u.id.sfk = sfk_none;
1097 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1098 of modifiers such as const or volatile to apply to the pointer
1099 type, represented as identifiers. */
1102 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1104 cp_declarator *declarator;
1106 declarator = make_declarator (cdk_pointer);
1107 declarator->declarator = target;
1108 declarator->u.pointer.qualifiers = cv_qualifiers;
1109 declarator->u.pointer.class_type = NULL_TREE;
1114 /* Like make_pointer_declarator -- but for references. */
1117 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1119 cp_declarator *declarator;
1121 declarator = make_declarator (cdk_reference);
1122 declarator->declarator = target;
1123 declarator->u.pointer.qualifiers = cv_qualifiers;
1124 declarator->u.pointer.class_type = NULL_TREE;
1129 /* Like make_pointer_declarator -- but for a pointer to a non-static
1130 member of CLASS_TYPE. */
1133 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1134 cp_declarator *pointee)
1136 cp_declarator *declarator;
1138 declarator = make_declarator (cdk_ptrmem);
1139 declarator->declarator = pointee;
1140 declarator->u.pointer.qualifiers = cv_qualifiers;
1141 declarator->u.pointer.class_type = class_type;
1146 /* Make a declarator for the function given by TARGET, with the
1147 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1148 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1149 indicates what exceptions can be thrown. */
1152 make_call_declarator (cp_declarator *target,
1153 cp_parameter_declarator *parms,
1154 cp_cv_quals cv_qualifiers,
1155 tree exception_specification)
1157 cp_declarator *declarator;
1159 declarator = make_declarator (cdk_function);
1160 declarator->declarator = target;
1161 declarator->u.function.parameters = parms;
1162 declarator->u.function.qualifiers = cv_qualifiers;
1163 declarator->u.function.exception_specification = exception_specification;
1168 /* Make a declarator for an array of BOUNDS elements, each of which is
1169 defined by ELEMENT. */
1172 make_array_declarator (cp_declarator *element, tree bounds)
1174 cp_declarator *declarator;
1176 declarator = make_declarator (cdk_array);
1177 declarator->declarator = element;
1178 declarator->u.array.bounds = bounds;
1183 cp_parameter_declarator *no_parameters;
1185 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1186 DECLARATOR and DEFAULT_ARGUMENT. */
1188 cp_parameter_declarator *
1189 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1190 cp_declarator *declarator,
1191 tree default_argument)
1193 cp_parameter_declarator *parameter;
1195 parameter = ((cp_parameter_declarator *)
1196 alloc_declarator (sizeof (cp_parameter_declarator)));
1197 parameter->next = NULL;
1198 if (decl_specifiers)
1199 parameter->decl_specifiers = *decl_specifiers;
1201 clear_decl_specs (¶meter->decl_specifiers);
1202 parameter->declarator = declarator;
1203 parameter->default_argument = default_argument;
1204 parameter->ellipsis_p = false;
1214 A cp_parser parses the token stream as specified by the C++
1215 grammar. Its job is purely parsing, not semantic analysis. For
1216 example, the parser breaks the token stream into declarators,
1217 expressions, statements, and other similar syntactic constructs.
1218 It does not check that the types of the expressions on either side
1219 of an assignment-statement are compatible, or that a function is
1220 not declared with a parameter of type `void'.
1222 The parser invokes routines elsewhere in the compiler to perform
1223 semantic analysis and to build up the abstract syntax tree for the
1226 The parser (and the template instantiation code, which is, in a
1227 way, a close relative of parsing) are the only parts of the
1228 compiler that should be calling push_scope and pop_scope, or
1229 related functions. The parser (and template instantiation code)
1230 keeps track of what scope is presently active; everything else
1231 should simply honor that. (The code that generates static
1232 initializers may also need to set the scope, in order to check
1233 access control correctly when emitting the initializers.)
1238 The parser is of the standard recursive-descent variety. Upcoming
1239 tokens in the token stream are examined in order to determine which
1240 production to use when parsing a non-terminal. Some C++ constructs
1241 require arbitrary look ahead to disambiguate. For example, it is
1242 impossible, in the general case, to tell whether a statement is an
1243 expression or declaration without scanning the entire statement.
1244 Therefore, the parser is capable of "parsing tentatively." When the
1245 parser is not sure what construct comes next, it enters this mode.
1246 Then, while we attempt to parse the construct, the parser queues up
1247 error messages, rather than issuing them immediately, and saves the
1248 tokens it consumes. If the construct is parsed successfully, the
1249 parser "commits", i.e., it issues any queued error messages and
1250 the tokens that were being preserved are permanently discarded.
1251 If, however, the construct is not parsed successfully, the parser
1252 rolls back its state completely so that it can resume parsing using
1253 a different alternative.
1258 The performance of the parser could probably be improved
1259 substantially. Some possible improvements include:
1261 - The expression parser recurses through the various levels of
1262 precedence as specified in the grammar, rather than using an
1263 operator-precedence technique. Therefore, parsing a simple
1264 identifier requires multiple recursive calls.
1266 - We could often eliminate the need to parse tentatively by
1267 looking ahead a little bit. In some places, this approach
1268 might not entirely eliminate the need to parse tentatively, but
1269 it might still speed up the average case. */
1271 /* Flags that are passed to some parsing functions. These values can
1272 be bitwise-ored together. */
1274 typedef enum cp_parser_flags
1277 CP_PARSER_FLAGS_NONE = 0x0,
1278 /* The construct is optional. If it is not present, then no error
1279 should be issued. */
1280 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1281 /* When parsing a type-specifier, do not allow user-defined types. */
1282 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1285 /* The different kinds of declarators we want to parse. */
1287 typedef enum cp_parser_declarator_kind
1289 /* We want an abstract declarator. */
1290 CP_PARSER_DECLARATOR_ABSTRACT,
1291 /* We want a named declarator. */
1292 CP_PARSER_DECLARATOR_NAMED,
1293 /* We don't mind, but the name must be an unqualified-id. */
1294 CP_PARSER_DECLARATOR_EITHER
1295 } cp_parser_declarator_kind;
1297 /* A mapping from a token type to a corresponding tree node type. */
1299 typedef struct cp_parser_token_tree_map_node
1301 /* The token type. */
1302 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1303 /* The corresponding tree code. */
1304 ENUM_BITFIELD (tree_code) tree_type : 8;
1305 } cp_parser_token_tree_map_node;
1307 /* A complete map consists of several ordinary entries, followed by a
1308 terminator. The terminating entry has a token_type of CPP_EOF. */
1310 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1312 /* The status of a tentative parse. */
1314 typedef enum cp_parser_status_kind
1316 /* No errors have occurred. */
1317 CP_PARSER_STATUS_KIND_NO_ERROR,
1318 /* An error has occurred. */
1319 CP_PARSER_STATUS_KIND_ERROR,
1320 /* We are committed to this tentative parse, whether or not an error
1322 CP_PARSER_STATUS_KIND_COMMITTED
1323 } cp_parser_status_kind;
1325 /* Context that is saved and restored when parsing tentatively. */
1327 typedef struct cp_parser_context GTY (())
1329 /* If this is a tentative parsing context, the status of the
1331 enum cp_parser_status_kind status;
1332 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1333 that are looked up in this context must be looked up both in the
1334 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1335 the context of the containing expression. */
1337 /* The next parsing context in the stack. */
1338 struct cp_parser_context *next;
1339 } cp_parser_context;
1343 /* Constructors and destructors. */
1345 static cp_parser_context *cp_parser_context_new
1346 (cp_parser_context *);
1348 /* Class variables. */
1350 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1352 /* Constructors and destructors. */
1354 /* Construct a new context. The context below this one on the stack
1355 is given by NEXT. */
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1360 cp_parser_context *context;
1362 /* Allocate the storage. */
1363 if (cp_parser_context_free_list != NULL)
1365 /* Pull the first entry from the free list. */
1366 context = cp_parser_context_free_list;
1367 cp_parser_context_free_list = context->next;
1368 memset (context, 0, sizeof (*context));
1371 context = GGC_CNEW (cp_parser_context);
1372 /* No errors have occurred yet in this context. */
1373 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1374 /* If this is not the bottomost context, copy information that we
1375 need from the previous context. */
1378 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1379 expression, then we are parsing one in this context, too. */
1380 context->object_type = next->object_type;
1381 /* Thread the stack. */
1382 context->next = next;
1388 /* The cp_parser structure represents the C++ parser. */
1390 typedef struct cp_parser GTY(())
1392 /* The lexer from which we are obtaining tokens. */
1395 /* The scope in which names should be looked up. If NULL_TREE, then
1396 we look up names in the scope that is currently open in the
1397 source program. If non-NULL, this is either a TYPE or
1398 NAMESPACE_DECL for the scope in which we should look.
1400 This value is not cleared automatically after a name is looked
1401 up, so we must be careful to clear it before starting a new look
1402 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1403 will look up `Z' in the scope of `X', rather than the current
1404 scope.) Unfortunately, it is difficult to tell when name lookup
1405 is complete, because we sometimes peek at a token, look it up,
1406 and then decide not to consume it. */
1409 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1410 last lookup took place. OBJECT_SCOPE is used if an expression
1411 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1412 respectively. QUALIFYING_SCOPE is used for an expression of the
1413 form "X::Y"; it refers to X. */
1415 tree qualifying_scope;
1417 /* A stack of parsing contexts. All but the bottom entry on the
1418 stack will be tentative contexts.
1420 We parse tentatively in order to determine which construct is in
1421 use in some situations. For example, in order to determine
1422 whether a statement is an expression-statement or a
1423 declaration-statement we parse it tentatively as a
1424 declaration-statement. If that fails, we then reparse the same
1425 token stream as an expression-statement. */
1426 cp_parser_context *context;
1428 /* True if we are parsing GNU C++. If this flag is not set, then
1429 GNU extensions are not recognized. */
1430 bool allow_gnu_extensions_p;
1432 /* TRUE if the `>' token should be interpreted as the greater-than
1433 operator. FALSE if it is the end of a template-id or
1434 template-parameter-list. */
1435 bool greater_than_is_operator_p;
1437 /* TRUE if default arguments are allowed within a parameter list
1438 that starts at this point. FALSE if only a gnu extension makes
1439 them permissible. */
1440 bool default_arg_ok_p;
1442 /* TRUE if we are parsing an integral constant-expression. See
1443 [expr.const] for a precise definition. */
1444 bool integral_constant_expression_p;
1446 /* TRUE if we are parsing an integral constant-expression -- but a
1447 non-constant expression should be permitted as well. This flag
1448 is used when parsing an array bound so that GNU variable-length
1449 arrays are tolerated. */
1450 bool allow_non_integral_constant_expression_p;
1452 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1453 been seen that makes the expression non-constant. */
1454 bool non_integral_constant_expression_p;
1456 /* TRUE if local variable names and `this' are forbidden in the
1458 bool local_variables_forbidden_p;
1460 /* TRUE if the declaration we are parsing is part of a
1461 linkage-specification of the form `extern string-literal
1463 bool in_unbraced_linkage_specification_p;
1465 /* TRUE if we are presently parsing a declarator, after the
1466 direct-declarator. */
1467 bool in_declarator_p;
1469 /* TRUE if we are presently parsing a template-argument-list. */
1470 bool in_template_argument_list_p;
1472 /* TRUE if we are presently parsing the body of an
1473 iteration-statement. */
1474 bool in_iteration_statement_p;
1476 /* TRUE if we are presently parsing the body of a switch
1478 bool in_switch_statement_p;
1480 /* TRUE if we are parsing a type-id in an expression context. In
1481 such a situation, both "type (expr)" and "type (type)" are valid
1483 bool in_type_id_in_expr_p;
1485 /* If non-NULL, then we are parsing a construct where new type
1486 definitions are not permitted. The string stored here will be
1487 issued as an error message if a type is defined. */
1488 const char *type_definition_forbidden_message;
1490 /* A list of lists. The outer list is a stack, used for member
1491 functions of local classes. At each level there are two sub-list,
1492 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1493 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1494 TREE_VALUE's. The functions are chained in reverse declaration
1497 The TREE_PURPOSE sublist contains those functions with default
1498 arguments that need post processing, and the TREE_VALUE sublist
1499 contains those functions with definitions that need post
1502 These lists can only be processed once the outermost class being
1503 defined is complete. */
1504 tree unparsed_functions_queues;
1506 /* The number of classes whose definitions are currently in
1508 unsigned num_classes_being_defined;
1510 /* The number of template parameter lists that apply directly to the
1511 current declaration. */
1512 unsigned num_template_parameter_lists;
1515 /* The type of a function that parses some kind of expression. */
1516 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1520 /* Constructors and destructors. */
1522 static cp_parser *cp_parser_new
1525 /* Routines to parse various constructs.
1527 Those that return `tree' will return the error_mark_node (rather
1528 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1529 Sometimes, they will return an ordinary node if error-recovery was
1530 attempted, even though a parse error occurred. So, to check
1531 whether or not a parse error occurred, you should always use
1532 cp_parser_error_occurred. If the construct is optional (indicated
1533 either by an `_opt' in the name of the function that does the
1534 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1535 the construct is not present. */
1537 /* Lexical conventions [gram.lex] */
1539 static tree cp_parser_identifier
1542 /* Basic concepts [gram.basic] */
1544 static bool cp_parser_translation_unit
1547 /* Expressions [gram.expr] */
1549 static tree cp_parser_primary_expression
1550 (cp_parser *, cp_id_kind *, tree *);
1551 static tree cp_parser_id_expression
1552 (cp_parser *, bool, bool, bool *, bool);
1553 static tree cp_parser_unqualified_id
1554 (cp_parser *, bool, bool, bool);
1555 static tree cp_parser_nested_name_specifier_opt
1556 (cp_parser *, bool, bool, bool, bool);
1557 static tree cp_parser_nested_name_specifier
1558 (cp_parser *, bool, bool, bool, bool);
1559 static tree cp_parser_class_or_namespace_name
1560 (cp_parser *, bool, bool, bool, bool, bool);
1561 static tree cp_parser_postfix_expression
1562 (cp_parser *, bool);
1563 static tree cp_parser_postfix_open_square_expression
1564 (cp_parser *, tree, bool);
1565 static tree cp_parser_postfix_dot_deref_expression
1566 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1567 static tree cp_parser_parenthesized_expression_list
1568 (cp_parser *, bool, bool *);
1569 static void cp_parser_pseudo_destructor_name
1570 (cp_parser *, tree *, tree *);
1571 static tree cp_parser_unary_expression
1572 (cp_parser *, bool);
1573 static enum tree_code cp_parser_unary_operator
1575 static tree cp_parser_new_expression
1577 static tree cp_parser_new_placement
1579 static tree cp_parser_new_type_id
1580 (cp_parser *, tree *);
1581 static cp_declarator *cp_parser_new_declarator_opt
1583 static cp_declarator *cp_parser_direct_new_declarator
1585 static tree cp_parser_new_initializer
1587 static tree cp_parser_delete_expression
1589 static tree cp_parser_cast_expression
1590 (cp_parser *, bool);
1591 static tree cp_parser_pm_expression
1593 static tree cp_parser_multiplicative_expression
1595 static tree cp_parser_additive_expression
1597 static tree cp_parser_shift_expression
1599 static tree cp_parser_relational_expression
1601 static tree cp_parser_equality_expression
1603 static tree cp_parser_and_expression
1605 static tree cp_parser_exclusive_or_expression
1607 static tree cp_parser_inclusive_or_expression
1609 static tree cp_parser_logical_and_expression
1611 static tree cp_parser_logical_or_expression
1613 static tree cp_parser_question_colon_clause
1614 (cp_parser *, tree);
1615 static tree cp_parser_assignment_expression
1617 static enum tree_code cp_parser_assignment_operator_opt
1619 static tree cp_parser_expression
1621 static tree cp_parser_constant_expression
1622 (cp_parser *, bool, bool *);
1623 static tree cp_parser_builtin_offsetof
1626 /* Statements [gram.stmt.stmt] */
1628 static void cp_parser_statement
1629 (cp_parser *, tree);
1630 static tree cp_parser_labeled_statement
1631 (cp_parser *, tree);
1632 static tree cp_parser_expression_statement
1633 (cp_parser *, tree);
1634 static tree cp_parser_compound_statement
1635 (cp_parser *, tree, bool);
1636 static void cp_parser_statement_seq_opt
1637 (cp_parser *, tree);
1638 static tree cp_parser_selection_statement
1640 static tree cp_parser_condition
1642 static tree cp_parser_iteration_statement
1644 static void cp_parser_for_init_statement
1646 static tree cp_parser_jump_statement
1648 static void cp_parser_declaration_statement
1651 static tree cp_parser_implicitly_scoped_statement
1653 static void cp_parser_already_scoped_statement
1656 /* Declarations [gram.dcl.dcl] */
1658 static void cp_parser_declaration_seq_opt
1660 static void cp_parser_declaration
1662 static void cp_parser_block_declaration
1663 (cp_parser *, bool);
1664 static void cp_parser_simple_declaration
1665 (cp_parser *, bool);
1666 static void cp_parser_decl_specifier_seq
1667 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1668 static tree cp_parser_storage_class_specifier_opt
1670 static tree cp_parser_function_specifier_opt
1671 (cp_parser *, cp_decl_specifier_seq *);
1672 static tree cp_parser_type_specifier
1673 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1675 static tree cp_parser_simple_type_specifier
1676 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1677 static tree cp_parser_type_name
1679 static tree cp_parser_elaborated_type_specifier
1680 (cp_parser *, bool, bool);
1681 static tree cp_parser_enum_specifier
1683 static void cp_parser_enumerator_list
1684 (cp_parser *, tree);
1685 static void cp_parser_enumerator_definition
1686 (cp_parser *, tree);
1687 static tree cp_parser_namespace_name
1689 static void cp_parser_namespace_definition
1691 static void cp_parser_namespace_body
1693 static tree cp_parser_qualified_namespace_specifier
1695 static void cp_parser_namespace_alias_definition
1697 static void cp_parser_using_declaration
1699 static void cp_parser_using_directive
1701 static void cp_parser_asm_definition
1703 static void cp_parser_linkage_specification
1706 /* Declarators [gram.dcl.decl] */
1708 static tree cp_parser_init_declarator
1709 (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1710 static cp_declarator *cp_parser_declarator
1711 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1712 static cp_declarator *cp_parser_direct_declarator
1713 (cp_parser *, cp_parser_declarator_kind, int *);
1714 static enum tree_code cp_parser_ptr_operator
1715 (cp_parser *, tree *, cp_cv_quals *);
1716 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1718 static tree cp_parser_declarator_id
1720 static tree cp_parser_type_id
1722 static void cp_parser_type_specifier_seq
1723 (cp_parser *, cp_decl_specifier_seq *);
1724 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1726 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1727 (cp_parser *, bool *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration
1729 (cp_parser *, bool, bool *);
1730 static void cp_parser_function_body
1732 static tree cp_parser_initializer
1733 (cp_parser *, bool *, bool *);
1734 static tree cp_parser_initializer_clause
1735 (cp_parser *, bool *);
1736 static tree cp_parser_initializer_list
1737 (cp_parser *, bool *);
1739 static bool cp_parser_ctor_initializer_opt_and_function_body
1742 /* Classes [gram.class] */
1744 static tree cp_parser_class_name
1745 (cp_parser *, bool, bool, bool, bool, bool, bool);
1746 static tree cp_parser_class_specifier
1748 static tree cp_parser_class_head
1749 (cp_parser *, bool *, tree *);
1750 static enum tag_types cp_parser_class_key
1752 static void cp_parser_member_specification_opt
1754 static void cp_parser_member_declaration
1756 static tree cp_parser_pure_specifier
1758 static tree cp_parser_constant_initializer
1761 /* Derived classes [gram.class.derived] */
1763 static tree cp_parser_base_clause
1765 static tree cp_parser_base_specifier
1768 /* Special member functions [gram.special] */
1770 static tree cp_parser_conversion_function_id
1772 static tree cp_parser_conversion_type_id
1774 static cp_declarator *cp_parser_conversion_declarator_opt
1776 static bool cp_parser_ctor_initializer_opt
1778 static void cp_parser_mem_initializer_list
1780 static tree cp_parser_mem_initializer
1782 static tree cp_parser_mem_initializer_id
1785 /* Overloading [gram.over] */
1787 static tree cp_parser_operator_function_id
1789 static tree cp_parser_operator
1792 /* Templates [gram.temp] */
1794 static void cp_parser_template_declaration
1795 (cp_parser *, bool);
1796 static tree cp_parser_template_parameter_list
1798 static tree cp_parser_template_parameter
1799 (cp_parser *, bool *);
1800 static tree cp_parser_type_parameter
1802 static tree cp_parser_template_id
1803 (cp_parser *, bool, bool, bool);
1804 static tree cp_parser_template_name
1805 (cp_parser *, bool, bool, bool, bool *);
1806 static tree cp_parser_template_argument_list
1808 static tree cp_parser_template_argument
1810 static void cp_parser_explicit_instantiation
1812 static void cp_parser_explicit_specialization
1815 /* Exception handling [gram.exception] */
1817 static tree cp_parser_try_block
1819 static bool cp_parser_function_try_block
1821 static void cp_parser_handler_seq
1823 static void cp_parser_handler
1825 static tree cp_parser_exception_declaration
1827 static tree cp_parser_throw_expression
1829 static tree cp_parser_exception_specification_opt
1831 static tree cp_parser_type_id_list
1834 /* GNU Extensions */
1836 static tree cp_parser_asm_specification_opt
1838 static tree cp_parser_asm_operand_list
1840 static tree cp_parser_asm_clobber_list
1842 static tree cp_parser_attributes_opt
1844 static tree cp_parser_attribute_list
1846 static bool cp_parser_extension_opt
1847 (cp_parser *, int *);
1848 static void cp_parser_label_declaration
1851 /* Utility Routines */
1853 static tree cp_parser_lookup_name
1854 (cp_parser *, tree, bool, bool, bool, bool, bool *);
1855 static tree cp_parser_lookup_name_simple
1856 (cp_parser *, tree);
1857 static tree cp_parser_maybe_treat_template_as_class
1859 static bool cp_parser_check_declarator_template_parameters
1860 (cp_parser *, cp_declarator *);
1861 static bool cp_parser_check_template_parameters
1862 (cp_parser *, unsigned);
1863 static tree cp_parser_simple_cast_expression
1865 static tree cp_parser_binary_expression
1866 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1867 static tree cp_parser_global_scope_opt
1868 (cp_parser *, bool);
1869 static bool cp_parser_constructor_declarator_p
1870 (cp_parser *, bool);
1871 static tree cp_parser_function_definition_from_specifiers_and_declarator
1872 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1873 static tree cp_parser_function_definition_after_declarator
1874 (cp_parser *, bool);
1875 static void cp_parser_template_declaration_after_export
1876 (cp_parser *, bool);
1877 static tree cp_parser_single_declaration
1878 (cp_parser *, bool, bool *);
1879 static tree cp_parser_functional_cast
1880 (cp_parser *, tree);
1881 static tree cp_parser_save_member_function_body
1882 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1883 static tree cp_parser_enclosed_template_argument_list
1885 static void cp_parser_save_default_args
1886 (cp_parser *, tree);
1887 static void cp_parser_late_parsing_for_member
1888 (cp_parser *, tree);
1889 static void cp_parser_late_parsing_default_args
1890 (cp_parser *, tree);
1891 static tree cp_parser_sizeof_operand
1892 (cp_parser *, enum rid);
1893 static bool cp_parser_declares_only_class_p
1895 static void cp_parser_set_storage_class
1896 (cp_decl_specifier_seq *, cp_storage_class);
1897 static void cp_parser_set_decl_spec_type
1898 (cp_decl_specifier_seq *, tree, bool);
1899 static bool cp_parser_friend_p
1900 (const cp_decl_specifier_seq *);
1901 static cp_token *cp_parser_require
1902 (cp_parser *, enum cpp_ttype, const char *);
1903 static cp_token *cp_parser_require_keyword
1904 (cp_parser *, enum rid, const char *);
1905 static bool cp_parser_token_starts_function_definition_p
1907 static bool cp_parser_next_token_starts_class_definition_p
1909 static bool cp_parser_next_token_ends_template_argument_p
1911 static bool cp_parser_nth_token_starts_template_argument_list_p
1912 (cp_parser *, size_t);
1913 static enum tag_types cp_parser_token_is_class_key
1915 static void cp_parser_check_class_key
1916 (enum tag_types, tree type);
1917 static void cp_parser_check_access_in_redeclaration
1919 static bool cp_parser_optional_template_keyword
1921 static void cp_parser_pre_parsed_nested_name_specifier
1923 static void cp_parser_cache_group
1924 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1925 static void cp_parser_parse_tentatively
1927 static void cp_parser_commit_to_tentative_parse
1929 static void cp_parser_abort_tentative_parse
1931 static bool cp_parser_parse_definitely
1933 static inline bool cp_parser_parsing_tentatively
1935 static bool cp_parser_committed_to_tentative_parse
1937 static void cp_parser_error
1938 (cp_parser *, const char *);
1939 static void cp_parser_name_lookup_error
1940 (cp_parser *, tree, tree, const char *);
1941 static bool cp_parser_simulate_error
1943 static void cp_parser_check_type_definition
1945 static void cp_parser_check_for_definition_in_return_type
1946 (cp_declarator *, int);
1947 static void cp_parser_check_for_invalid_template_id
1948 (cp_parser *, tree);
1949 static bool cp_parser_non_integral_constant_expression
1950 (cp_parser *, const char *);
1951 static void cp_parser_diagnose_invalid_type_name
1952 (cp_parser *, tree, tree);
1953 static bool cp_parser_parse_and_diagnose_invalid_type_name
1955 static int cp_parser_skip_to_closing_parenthesis
1956 (cp_parser *, bool, bool, bool);
1957 static void cp_parser_skip_to_end_of_statement
1959 static void cp_parser_consume_semicolon_at_end_of_statement
1961 static void cp_parser_skip_to_end_of_block_or_statement
1963 static void cp_parser_skip_to_closing_brace
1965 static void cp_parser_skip_until_found
1966 (cp_parser *, enum cpp_ttype, const char *);
1967 static bool cp_parser_error_occurred
1969 static bool cp_parser_allow_gnu_extensions_p
1971 static bool cp_parser_is_string_literal
1973 static bool cp_parser_is_keyword
1974 (cp_token *, enum rid);
1975 static tree cp_parser_make_typename_type
1976 (cp_parser *, tree, tree);
1978 /* Returns nonzero if we are parsing tentatively. */
1981 cp_parser_parsing_tentatively (cp_parser* parser)
1983 return parser->context->next != NULL;
1986 /* Returns nonzero if TOKEN is a string literal. */
1989 cp_parser_is_string_literal (cp_token* token)
1991 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1994 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1997 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1999 return token->keyword == keyword;
2002 /* Issue the indicated error MESSAGE. */
2005 cp_parser_error (cp_parser* parser, const char* message)
2007 /* Output the MESSAGE -- unless we're parsing tentatively. */
2008 if (!cp_parser_simulate_error (parser))
2011 token = cp_lexer_peek_token (parser->lexer);
2012 c_parse_error (message,
2013 /* Because c_parser_error does not understand
2014 CPP_KEYWORD, keywords are treated like
2016 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2021 /* Issue an error about name-lookup failing. NAME is the
2022 IDENTIFIER_NODE DECL is the result of
2023 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2024 the thing that we hoped to find. */
2027 cp_parser_name_lookup_error (cp_parser* parser,
2030 const char* desired)
2032 /* If name lookup completely failed, tell the user that NAME was not
2034 if (decl == error_mark_node)
2036 if (parser->scope && parser->scope != global_namespace)
2037 error ("`%D::%D' has not been declared",
2038 parser->scope, name);
2039 else if (parser->scope == global_namespace)
2040 error ("`::%D' has not been declared", name);
2041 else if (parser->object_scope
2042 && !CLASS_TYPE_P (parser->object_scope))
2043 error ("request for member `%D' in non-class type `%T'",
2044 name, parser->object_scope);
2045 else if (parser->object_scope)
2046 error ("`%T::%D' has not been declared",
2047 parser->object_scope, name);
2049 error ("`%D' has not been declared", name);
2051 else if (parser->scope && parser->scope != global_namespace)
2052 error ("`%D::%D' %s", parser->scope, name, desired);
2053 else if (parser->scope == global_namespace)
2054 error ("`::%D' %s", name, desired);
2056 error ("`%D' %s", name, desired);
2059 /* If we are parsing tentatively, remember that an error has occurred
2060 during this tentative parse. Returns true if the error was
2061 simulated; false if a message should be issued by the caller. */
2064 cp_parser_simulate_error (cp_parser* parser)
2066 if (cp_parser_parsing_tentatively (parser)
2067 && !cp_parser_committed_to_tentative_parse (parser))
2069 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2075 /* This function is called when a type is defined. If type
2076 definitions are forbidden at this point, an error message is
2080 cp_parser_check_type_definition (cp_parser* parser)
2082 /* If types are forbidden here, issue a message. */
2083 if (parser->type_definition_forbidden_message)
2084 /* Use `%s' to print the string in case there are any escape
2085 characters in the message. */
2086 error ("%s", parser->type_definition_forbidden_message);
2089 /* This function is called when a declaration is parsed. If
2090 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
2091 indicates that a type was defined in the decl-specifiers for DECL,
2092 then an error is issued. */
2095 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2096 int declares_class_or_enum)
2098 /* [dcl.fct] forbids type definitions in return types.
2099 Unfortunately, it's not easy to know whether or not we are
2100 processing a return type until after the fact. */
2102 && (declarator->kind == cdk_pointer
2103 || declarator->kind == cdk_reference
2104 || declarator->kind == cdk_ptrmem))
2105 declarator = declarator->declarator;
2107 && declarator->kind == cdk_function
2108 && declares_class_or_enum & 2)
2109 error ("new types may not be defined in a return type");
2112 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2113 "<" in any valid C++ program. If the next token is indeed "<",
2114 issue a message warning the user about what appears to be an
2115 invalid attempt to form a template-id. */
2118 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2124 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2127 error ("`%T' is not a template", type);
2128 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2129 error ("`%E' is not a template", type);
2131 error ("invalid template-id");
2132 /* Remember the location of the invalid "<". */
2133 if (cp_parser_parsing_tentatively (parser)
2134 && !cp_parser_committed_to_tentative_parse (parser))
2136 token = cp_lexer_peek_token (parser->lexer);
2137 token = cp_lexer_prev_token (parser->lexer, token);
2138 start = cp_lexer_token_difference (parser->lexer,
2139 parser->lexer->first_token,
2144 /* Consume the "<". */
2145 cp_lexer_consume_token (parser->lexer);
2146 /* Parse the template arguments. */
2147 cp_parser_enclosed_template_argument_list (parser);
2148 /* Permanently remove the invalid template arguments so that
2149 this error message is not issued again. */
2152 token = cp_lexer_advance_token (parser->lexer,
2153 parser->lexer->first_token,
2155 cp_lexer_purge_tokens_after (parser->lexer, token);
2160 /* If parsing an integral constant-expression, issue an error message
2161 about the fact that THING appeared and return true. Otherwise,
2162 return false, marking the current expression as non-constant. */
2165 cp_parser_non_integral_constant_expression (cp_parser *parser,
2168 if (parser->integral_constant_expression_p)
2170 if (!parser->allow_non_integral_constant_expression_p)
2172 error ("%s cannot appear in a constant-expression", thing);
2175 parser->non_integral_constant_expression_p = true;
2180 /* Emit a diagnostic for an invalid type name. Consider also if it is
2181 qualified or not and the result of a lookup, to provide a better
2185 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2187 tree decl, old_scope;
2188 /* Try to lookup the identifier. */
2189 old_scope = parser->scope;
2190 parser->scope = scope;
2191 decl = cp_parser_lookup_name_simple (parser, id);
2192 parser->scope = old_scope;
2193 /* If the lookup found a template-name, it means that the user forgot
2194 to specify an argument list. Emit an useful error message. */
2195 if (TREE_CODE (decl) == TEMPLATE_DECL)
2196 error ("invalid use of template-name `%E' without an argument list",
2198 else if (!parser->scope)
2200 /* Issue an error message. */
2201 error ("`%E' does not name a type", id);
2202 /* If we're in a template class, it's possible that the user was
2203 referring to a type from a base class. For example:
2205 template <typename T> struct A { typedef T X; };
2206 template <typename T> struct B : public A<T> { X x; };
2208 The user should have said "typename A<T>::X". */
2209 if (processing_template_decl && current_class_type)
2213 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2217 tree base_type = BINFO_TYPE (b);
2218 if (CLASS_TYPE_P (base_type)
2219 && dependent_type_p (base_type))
2222 /* Go from a particular instantiation of the
2223 template (which will have an empty TYPE_FIELDs),
2224 to the main version. */
2225 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2226 for (field = TYPE_FIELDS (base_type);
2228 field = TREE_CHAIN (field))
2229 if (TREE_CODE (field) == TYPE_DECL
2230 && DECL_NAME (field) == id)
2232 inform ("(perhaps `typename %T::%E' was intended)",
2233 BINFO_TYPE (b), id);
2242 /* Here we diagnose qualified-ids where the scope is actually correct,
2243 but the identifier does not resolve to a valid type name. */
2246 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2247 error ("`%E' in namespace `%E' does not name a type",
2249 else if (TYPE_P (parser->scope))
2250 error ("`%E' in class `%T' does not name a type",
2257 /* Check for a common situation where a type-name should be present,
2258 but is not, and issue a sensible error message. Returns true if an
2259 invalid type-name was detected.
2261 The situation handled by this function are variable declarations of the
2262 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2263 Usually, `ID' should name a type, but if we got here it means that it
2264 does not. We try to emit the best possible error message depending on
2265 how exactly the id-expression looks like.
2269 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2273 cp_parser_parse_tentatively (parser);
2274 id = cp_parser_id_expression (parser,
2275 /*template_keyword_p=*/false,
2276 /*check_dependency_p=*/true,
2277 /*template_p=*/NULL,
2278 /*declarator_p=*/true);
2279 /* After the id-expression, there should be a plain identifier,
2280 otherwise this is not a simple variable declaration. Also, if
2281 the scope is dependent, we cannot do much. */
2282 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2283 || (parser->scope && TYPE_P (parser->scope)
2284 && dependent_type_p (parser->scope)))
2286 cp_parser_abort_tentative_parse (parser);
2289 if (!cp_parser_parse_definitely (parser))
2292 /* If we got here, this cannot be a valid variable declaration, thus
2293 the cp_parser_id_expression must have resolved to a plain identifier
2294 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2295 gcc_assert (TREE_CODE (id) == IDENTIFIER_NODE);
2296 /* Emit a diagnostic for the invalid type. */
2297 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2298 /* Skip to the end of the declaration; there's no point in
2299 trying to process it. */
2300 cp_parser_skip_to_end_of_block_or_statement (parser);
2304 /* Consume tokens up to, and including, the next non-nested closing `)'.
2305 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2306 are doing error recovery. Returns -1 if OR_COMMA is true and we
2307 found an unnested comma. */
2310 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2315 unsigned paren_depth = 0;
2316 unsigned brace_depth = 0;
2317 int saved_c_lex_string_translate = c_lex_string_translate;
2320 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2321 && !cp_parser_committed_to_tentative_parse (parser))
2325 /* If we're looking ahead, keep both translated and untranslated
2327 c_lex_string_translate = -1;
2333 /* If we've run out of tokens, then there is no closing `)'. */
2334 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2340 token = cp_lexer_peek_token (parser->lexer);
2342 /* This matches the processing in skip_to_end_of_statement. */
2343 if (token->type == CPP_SEMICOLON && !brace_depth)
2348 if (token->type == CPP_OPEN_BRACE)
2350 if (token->type == CPP_CLOSE_BRACE)
2358 if (recovering && or_comma && token->type == CPP_COMMA
2359 && !brace_depth && !paren_depth)
2367 /* If it is an `(', we have entered another level of nesting. */
2368 if (token->type == CPP_OPEN_PAREN)
2370 /* If it is a `)', then we might be done. */
2371 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2374 cp_lexer_consume_token (parser->lexer);
2382 /* Consume the token. */
2383 cp_lexer_consume_token (parser->lexer);
2386 c_lex_string_translate = saved_c_lex_string_translate;
2390 /* Consume tokens until we reach the end of the current statement.
2391 Normally, that will be just before consuming a `;'. However, if a
2392 non-nested `}' comes first, then we stop before consuming that. */
2395 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2397 unsigned nesting_depth = 0;
2403 /* Peek at the next token. */
2404 token = cp_lexer_peek_token (parser->lexer);
2405 /* If we've run out of tokens, stop. */
2406 if (token->type == CPP_EOF)
2408 /* If the next token is a `;', we have reached the end of the
2410 if (token->type == CPP_SEMICOLON && !nesting_depth)
2412 /* If the next token is a non-nested `}', then we have reached
2413 the end of the current block. */
2414 if (token->type == CPP_CLOSE_BRACE)
2416 /* If this is a non-nested `}', stop before consuming it.
2417 That way, when confronted with something like:
2421 we stop before consuming the closing `}', even though we
2422 have not yet reached a `;'. */
2423 if (nesting_depth == 0)
2425 /* If it is the closing `}' for a block that we have
2426 scanned, stop -- but only after consuming the token.
2432 we will stop after the body of the erroneously declared
2433 function, but before consuming the following `typedef'
2435 if (--nesting_depth == 0)
2437 cp_lexer_consume_token (parser->lexer);
2441 /* If it the next token is a `{', then we are entering a new
2442 block. Consume the entire block. */
2443 else if (token->type == CPP_OPEN_BRACE)
2445 /* Consume the token. */
2446 cp_lexer_consume_token (parser->lexer);
2450 /* This function is called at the end of a statement or declaration.
2451 If the next token is a semicolon, it is consumed; otherwise, error
2452 recovery is attempted. */
2455 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2457 /* Look for the trailing `;'. */
2458 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2460 /* If there is additional (erroneous) input, skip to the end of
2462 cp_parser_skip_to_end_of_statement (parser);
2463 /* If the next token is now a `;', consume it. */
2464 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2465 cp_lexer_consume_token (parser->lexer);
2469 /* Skip tokens until we have consumed an entire block, or until we
2470 have consumed a non-nested `;'. */
2473 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2475 unsigned nesting_depth = 0;
2481 /* Peek at the next token. */
2482 token = cp_lexer_peek_token (parser->lexer);
2483 /* If we've run out of tokens, stop. */
2484 if (token->type == CPP_EOF)
2486 /* If the next token is a `;', we have reached the end of the
2488 if (token->type == CPP_SEMICOLON && !nesting_depth)
2490 /* Consume the `;'. */
2491 cp_lexer_consume_token (parser->lexer);
2494 /* Consume the token. */
2495 token = cp_lexer_consume_token (parser->lexer);
2496 /* If the next token is a non-nested `}', then we have reached
2497 the end of the current block. */
2498 if (token->type == CPP_CLOSE_BRACE
2499 && (nesting_depth == 0 || --nesting_depth == 0))
2501 /* If it the next token is a `{', then we are entering a new
2502 block. Consume the entire block. */
2503 if (token->type == CPP_OPEN_BRACE)
2508 /* Skip tokens until a non-nested closing curly brace is the next
2512 cp_parser_skip_to_closing_brace (cp_parser *parser)
2514 unsigned nesting_depth = 0;
2520 /* Peek at the next token. */
2521 token = cp_lexer_peek_token (parser->lexer);
2522 /* If we've run out of tokens, stop. */
2523 if (token->type == CPP_EOF)
2525 /* If the next token is a non-nested `}', then we have reached
2526 the end of the current block. */
2527 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2529 /* If it the next token is a `{', then we are entering a new
2530 block. Consume the entire block. */
2531 else if (token->type == CPP_OPEN_BRACE)
2533 /* Consume the token. */
2534 cp_lexer_consume_token (parser->lexer);
2538 /* This is a simple wrapper around make_typename_type. When the id is
2539 an unresolved identifier node, we can provide a superior diagnostic
2540 using cp_parser_diagnose_invalid_type_name. */
2543 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2546 if (TREE_CODE (id) == IDENTIFIER_NODE)
2548 result = make_typename_type (scope, id, /*complain=*/0);
2549 if (result == error_mark_node)
2550 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2553 return make_typename_type (scope, id, tf_error);
2557 /* Create a new C++ parser. */
2560 cp_parser_new (void)
2565 /* cp_lexer_new_main is called before calling ggc_alloc because
2566 cp_lexer_new_main might load a PCH file. */
2567 lexer = cp_lexer_new_main ();
2569 parser = GGC_CNEW (cp_parser);
2570 parser->lexer = lexer;
2571 parser->context = cp_parser_context_new (NULL);
2573 /* For now, we always accept GNU extensions. */
2574 parser->allow_gnu_extensions_p = 1;
2576 /* The `>' token is a greater-than operator, not the end of a
2578 parser->greater_than_is_operator_p = true;
2580 parser->default_arg_ok_p = true;
2582 /* We are not parsing a constant-expression. */
2583 parser->integral_constant_expression_p = false;
2584 parser->allow_non_integral_constant_expression_p = false;
2585 parser->non_integral_constant_expression_p = false;
2587 /* Local variable names are not forbidden. */
2588 parser->local_variables_forbidden_p = false;
2590 /* We are not processing an `extern "C"' declaration. */
2591 parser->in_unbraced_linkage_specification_p = false;
2593 /* We are not processing a declarator. */
2594 parser->in_declarator_p = false;
2596 /* We are not processing a template-argument-list. */
2597 parser->in_template_argument_list_p = false;
2599 /* We are not in an iteration statement. */
2600 parser->in_iteration_statement_p = false;
2602 /* We are not in a switch statement. */
2603 parser->in_switch_statement_p = false;
2605 /* We are not parsing a type-id inside an expression. */
2606 parser->in_type_id_in_expr_p = false;
2608 /* The unparsed function queue is empty. */
2609 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2611 /* There are no classes being defined. */
2612 parser->num_classes_being_defined = 0;
2614 /* No template parameters apply. */
2615 parser->num_template_parameter_lists = 0;
2620 /* Lexical conventions [gram.lex] */
2622 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2626 cp_parser_identifier (cp_parser* parser)
2630 /* Look for the identifier. */
2631 token = cp_parser_require (parser, CPP_NAME, "identifier");
2632 /* Return the value. */
2633 return token ? token->value : error_mark_node;
2636 /* Basic concepts [gram.basic] */
2638 /* Parse a translation-unit.
2641 declaration-seq [opt]
2643 Returns TRUE if all went well. */
2646 cp_parser_translation_unit (cp_parser* parser)
2648 /* The address of the first non-permanent object on the declarator
2650 static void *declarator_obstack_base;
2654 /* Create the declarator obstack, if necessary. */
2655 if (!cp_error_declarator)
2657 gcc_obstack_init (&declarator_obstack);
2658 /* Create the error declarator. */
2659 cp_error_declarator = make_declarator (cdk_error);
2660 /* Create the empty parameter list. */
2661 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2662 /* Remember where the base of the declarator obstack lies. */
2663 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2668 cp_parser_declaration_seq_opt (parser);
2670 /* If there are no tokens left then all went well. */
2671 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2673 /* Consume the EOF token. */
2674 cp_parser_require (parser, CPP_EOF, "end-of-file");
2677 finish_translation_unit ();
2684 cp_parser_error (parser, "expected declaration");
2690 /* Make sure the declarator obstack was fully cleaned up. */
2691 gcc_assert (obstack_next_free (&declarator_obstack)
2692 == declarator_obstack_base);
2694 /* All went well. */
2698 /* Expressions [gram.expr] */
2700 /* Parse a primary-expression.
2711 ( compound-statement )
2712 __builtin_va_arg ( assignment-expression , type-id )
2717 Returns a representation of the expression.
2719 *IDK indicates what kind of id-expression (if any) was present.
2721 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2722 used as the operand of a pointer-to-member. In that case,
2723 *QUALIFYING_CLASS gives the class that is used as the qualifying
2724 class in the pointer-to-member. */
2727 cp_parser_primary_expression (cp_parser *parser,
2729 tree *qualifying_class)
2733 /* Assume the primary expression is not an id-expression. */
2734 *idk = CP_ID_KIND_NONE;
2735 /* And that it cannot be used as pointer-to-member. */
2736 *qualifying_class = NULL_TREE;
2738 /* Peek at the next token. */
2739 token = cp_lexer_peek_token (parser->lexer);
2740 switch (token->type)
2751 token = cp_lexer_consume_token (parser->lexer);
2752 return token->value;
2756 token = cp_lexer_consume_token (parser->lexer);
2757 if (TREE_CHAIN (token->value))
2758 return TREE_CHAIN (token->value);
2760 return token->value;
2762 case CPP_OPEN_PAREN:
2765 bool saved_greater_than_is_operator_p;
2767 /* Consume the `('. */
2768 cp_lexer_consume_token (parser->lexer);
2769 /* Within a parenthesized expression, a `>' token is always
2770 the greater-than operator. */
2771 saved_greater_than_is_operator_p
2772 = parser->greater_than_is_operator_p;
2773 parser->greater_than_is_operator_p = true;
2774 /* If we see `( { ' then we are looking at the beginning of
2775 a GNU statement-expression. */
2776 if (cp_parser_allow_gnu_extensions_p (parser)
2777 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2779 /* Statement-expressions are not allowed by the standard. */
2781 pedwarn ("ISO C++ forbids braced-groups within expressions");
2783 /* And they're not allowed outside of a function-body; you
2784 cannot, for example, write:
2786 int i = ({ int j = 3; j + 1; });
2788 at class or namespace scope. */
2789 if (!at_function_scope_p ())
2790 error ("statement-expressions are allowed only inside functions");
2791 /* Start the statement-expression. */
2792 expr = begin_stmt_expr ();
2793 /* Parse the compound-statement. */
2794 cp_parser_compound_statement (parser, expr, false);
2796 expr = finish_stmt_expr (expr, false);
2800 /* Parse the parenthesized expression. */
2801 expr = cp_parser_expression (parser);
2802 /* Let the front end know that this expression was
2803 enclosed in parentheses. This matters in case, for
2804 example, the expression is of the form `A::B', since
2805 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2807 finish_parenthesized_expr (expr);
2809 /* The `>' token might be the end of a template-id or
2810 template-parameter-list now. */
2811 parser->greater_than_is_operator_p
2812 = saved_greater_than_is_operator_p;
2813 /* Consume the `)'. */
2814 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2815 cp_parser_skip_to_end_of_statement (parser);
2821 switch (token->keyword)
2823 /* These two are the boolean literals. */
2825 cp_lexer_consume_token (parser->lexer);
2826 return boolean_true_node;
2828 cp_lexer_consume_token (parser->lexer);
2829 return boolean_false_node;
2831 /* The `__null' literal. */
2833 cp_lexer_consume_token (parser->lexer);
2836 /* Recognize the `this' keyword. */
2838 cp_lexer_consume_token (parser->lexer);
2839 if (parser->local_variables_forbidden_p)
2841 error ("`this' may not be used in this context");
2842 return error_mark_node;
2844 /* Pointers cannot appear in constant-expressions. */
2845 if (cp_parser_non_integral_constant_expression (parser,
2847 return error_mark_node;
2848 return finish_this_expr ();
2850 /* The `operator' keyword can be the beginning of an
2855 case RID_FUNCTION_NAME:
2856 case RID_PRETTY_FUNCTION_NAME:
2857 case RID_C99_FUNCTION_NAME:
2858 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2859 __func__ are the names of variables -- but they are
2860 treated specially. Therefore, they are handled here,
2861 rather than relying on the generic id-expression logic
2862 below. Grammatically, these names are id-expressions.
2864 Consume the token. */
2865 token = cp_lexer_consume_token (parser->lexer);
2866 /* Look up the name. */
2867 return finish_fname (token->value);
2874 /* The `__builtin_va_arg' construct is used to handle
2875 `va_arg'. Consume the `__builtin_va_arg' token. */
2876 cp_lexer_consume_token (parser->lexer);
2877 /* Look for the opening `('. */
2878 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2879 /* Now, parse the assignment-expression. */
2880 expression = cp_parser_assignment_expression (parser);
2881 /* Look for the `,'. */
2882 cp_parser_require (parser, CPP_COMMA, "`,'");
2883 /* Parse the type-id. */
2884 type = cp_parser_type_id (parser);
2885 /* Look for the closing `)'. */
2886 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2887 /* Using `va_arg' in a constant-expression is not
2889 if (cp_parser_non_integral_constant_expression (parser,
2891 return error_mark_node;
2892 return build_x_va_arg (expression, type);
2896 return cp_parser_builtin_offsetof (parser);
2899 cp_parser_error (parser, "expected primary-expression");
2900 return error_mark_node;
2903 /* An id-expression can start with either an identifier, a
2904 `::' as the beginning of a qualified-id, or the "operator"
2908 case CPP_TEMPLATE_ID:
2909 case CPP_NESTED_NAME_SPECIFIER:
2913 const char *error_msg;
2916 /* Parse the id-expression. */
2918 = cp_parser_id_expression (parser,
2919 /*template_keyword_p=*/false,
2920 /*check_dependency_p=*/true,
2921 /*template_p=*/NULL,
2922 /*declarator_p=*/false);
2923 if (id_expression == error_mark_node)
2924 return error_mark_node;
2925 /* If we have a template-id, then no further lookup is
2926 required. If the template-id was for a template-class, we
2927 will sometimes have a TYPE_DECL at this point. */
2928 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2929 || TREE_CODE (id_expression) == TYPE_DECL)
2930 decl = id_expression;
2931 /* Look up the name. */
2936 decl = cp_parser_lookup_name (parser, id_expression,
2938 /*is_template=*/false,
2939 /*is_namespace=*/false,
2940 /*check_dependency=*/true,
2942 /* If the lookup was ambiguous, an error will already have
2945 return error_mark_node;
2946 /* If name lookup gives us a SCOPE_REF, then the
2947 qualifying scope was dependent. Just propagate the
2949 if (TREE_CODE (decl) == SCOPE_REF)
2951 if (TYPE_P (TREE_OPERAND (decl, 0)))
2952 *qualifying_class = TREE_OPERAND (decl, 0);
2955 /* Check to see if DECL is a local variable in a context
2956 where that is forbidden. */
2957 if (parser->local_variables_forbidden_p
2958 && local_variable_p (decl))
2960 /* It might be that we only found DECL because we are
2961 trying to be generous with pre-ISO scoping rules.
2962 For example, consider:
2966 for (int i = 0; i < 10; ++i) {}
2967 extern void f(int j = i);
2970 Here, name look up will originally find the out
2971 of scope `i'. We need to issue a warning message,
2972 but then use the global `i'. */
2973 decl = check_for_out_of_scope_variable (decl);
2974 if (local_variable_p (decl))
2976 error ("local variable `%D' may not appear in this context",
2978 return error_mark_node;
2983 decl = finish_id_expression (id_expression, decl, parser->scope,
2984 idk, qualifying_class,
2985 parser->integral_constant_expression_p,
2986 parser->allow_non_integral_constant_expression_p,
2987 &parser->non_integral_constant_expression_p,
2990 cp_parser_error (parser, error_msg);
2994 /* Anything else is an error. */
2996 cp_parser_error (parser, "expected primary-expression");
2997 return error_mark_node;
3001 /* Parse an id-expression.
3008 :: [opt] nested-name-specifier template [opt] unqualified-id
3010 :: operator-function-id
3013 Return a representation of the unqualified portion of the
3014 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3015 a `::' or nested-name-specifier.
3017 Often, if the id-expression was a qualified-id, the caller will
3018 want to make a SCOPE_REF to represent the qualified-id. This
3019 function does not do this in order to avoid wastefully creating
3020 SCOPE_REFs when they are not required.
3022 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3025 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3026 uninstantiated templates.
3028 If *TEMPLATE_P is non-NULL, it is set to true iff the
3029 `template' keyword is used to explicitly indicate that the entity
3030 named is a template.
3032 If DECLARATOR_P is true, the id-expression is appearing as part of
3033 a declarator, rather than as part of an expression. */
3036 cp_parser_id_expression (cp_parser *parser,
3037 bool template_keyword_p,
3038 bool check_dependency_p,
3042 bool global_scope_p;
3043 bool nested_name_specifier_p;
3045 /* Assume the `template' keyword was not used. */
3047 *template_p = false;
3049 /* Look for the optional `::' operator. */
3051 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3053 /* Look for the optional nested-name-specifier. */
3054 nested_name_specifier_p
3055 = (cp_parser_nested_name_specifier_opt (parser,
3056 /*typename_keyword_p=*/false,
3059 /*is_declarator=*/false)
3061 /* If there is a nested-name-specifier, then we are looking at
3062 the first qualified-id production. */
3063 if (nested_name_specifier_p)
3066 tree saved_object_scope;
3067 tree saved_qualifying_scope;
3068 tree unqualified_id;
3071 /* See if the next token is the `template' keyword. */
3073 template_p = &is_template;
3074 *template_p = cp_parser_optional_template_keyword (parser);
3075 /* Name lookup we do during the processing of the
3076 unqualified-id might obliterate SCOPE. */
3077 saved_scope = parser->scope;
3078 saved_object_scope = parser->object_scope;
3079 saved_qualifying_scope = parser->qualifying_scope;
3080 /* Process the final unqualified-id. */
3081 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3084 /* Restore the SAVED_SCOPE for our caller. */
3085 parser->scope = saved_scope;
3086 parser->object_scope = saved_object_scope;
3087 parser->qualifying_scope = saved_qualifying_scope;
3089 return unqualified_id;
3091 /* Otherwise, if we are in global scope, then we are looking at one
3092 of the other qualified-id productions. */
3093 else if (global_scope_p)
3098 /* Peek at the next token. */
3099 token = cp_lexer_peek_token (parser->lexer);
3101 /* If it's an identifier, and the next token is not a "<", then
3102 we can avoid the template-id case. This is an optimization
3103 for this common case. */
3104 if (token->type == CPP_NAME
3105 && !cp_parser_nth_token_starts_template_argument_list_p
3107 return cp_parser_identifier (parser);
3109 cp_parser_parse_tentatively (parser);
3110 /* Try a template-id. */
3111 id = cp_parser_template_id (parser,
3112 /*template_keyword_p=*/false,
3113 /*check_dependency_p=*/true,
3115 /* If that worked, we're done. */
3116 if (cp_parser_parse_definitely (parser))
3119 /* Peek at the next token. (Changes in the token buffer may
3120 have invalidated the pointer obtained above.) */
3121 token = cp_lexer_peek_token (parser->lexer);
3123 switch (token->type)
3126 return cp_parser_identifier (parser);
3129 if (token->keyword == RID_OPERATOR)
3130 return cp_parser_operator_function_id (parser);
3134 cp_parser_error (parser, "expected id-expression");
3135 return error_mark_node;
3139 return cp_parser_unqualified_id (parser, template_keyword_p,
3140 /*check_dependency_p=*/true,
3144 /* Parse an unqualified-id.
3148 operator-function-id
3149 conversion-function-id
3153 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3154 keyword, in a construct like `A::template ...'.
3156 Returns a representation of unqualified-id. For the `identifier'
3157 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3158 production a BIT_NOT_EXPR is returned; the operand of the
3159 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3160 other productions, see the documentation accompanying the
3161 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3162 names are looked up in uninstantiated templates. If DECLARATOR_P
3163 is true, the unqualified-id is appearing as part of a declarator,
3164 rather than as part of an expression. */
3167 cp_parser_unqualified_id (cp_parser* parser,
3168 bool template_keyword_p,
3169 bool check_dependency_p,
3174 /* Peek at the next token. */
3175 token = cp_lexer_peek_token (parser->lexer);
3177 switch (token->type)
3183 /* We don't know yet whether or not this will be a
3185 cp_parser_parse_tentatively (parser);
3186 /* Try a template-id. */
3187 id = cp_parser_template_id (parser, template_keyword_p,
3190 /* If it worked, we're done. */
3191 if (cp_parser_parse_definitely (parser))
3193 /* Otherwise, it's an ordinary identifier. */
3194 return cp_parser_identifier (parser);
3197 case CPP_TEMPLATE_ID:
3198 return cp_parser_template_id (parser, template_keyword_p,
3205 tree qualifying_scope;
3209 /* Consume the `~' token. */
3210 cp_lexer_consume_token (parser->lexer);
3211 /* Parse the class-name. The standard, as written, seems to
3214 template <typename T> struct S { ~S (); };
3215 template <typename T> S<T>::~S() {}
3217 is invalid, since `~' must be followed by a class-name, but
3218 `S<T>' is dependent, and so not known to be a class.
3219 That's not right; we need to look in uninstantiated
3220 templates. A further complication arises from:
3222 template <typename T> void f(T t) {
3226 Here, it is not possible to look up `T' in the scope of `T'
3227 itself. We must look in both the current scope, and the
3228 scope of the containing complete expression.
3230 Yet another issue is:
3239 The standard does not seem to say that the `S' in `~S'
3240 should refer to the type `S' and not the data member
3243 /* DR 244 says that we look up the name after the "~" in the
3244 same scope as we looked up the qualifying name. That idea
3245 isn't fully worked out; it's more complicated than that. */
3246 scope = parser->scope;
3247 object_scope = parser->object_scope;
3248 qualifying_scope = parser->qualifying_scope;
3250 /* If the name is of the form "X::~X" it's OK. */
3251 if (scope && TYPE_P (scope)
3252 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3253 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3255 && (cp_lexer_peek_token (parser->lexer)->value
3256 == TYPE_IDENTIFIER (scope)))
3258 cp_lexer_consume_token (parser->lexer);
3259 return build_nt (BIT_NOT_EXPR, scope);
3262 /* If there was an explicit qualification (S::~T), first look
3263 in the scope given by the qualification (i.e., S). */
3266 cp_parser_parse_tentatively (parser);
3267 type_decl = cp_parser_class_name (parser,
3268 /*typename_keyword_p=*/false,
3269 /*template_keyword_p=*/false,
3271 /*check_dependency=*/false,
3272 /*class_head_p=*/false,
3274 if (cp_parser_parse_definitely (parser))
3275 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3277 /* In "N::S::~S", look in "N" as well. */
3278 if (scope && qualifying_scope)
3280 cp_parser_parse_tentatively (parser);
3281 parser->scope = qualifying_scope;
3282 parser->object_scope = NULL_TREE;
3283 parser->qualifying_scope = NULL_TREE;
3285 = cp_parser_class_name (parser,
3286 /*typename_keyword_p=*/false,
3287 /*template_keyword_p=*/false,
3289 /*check_dependency=*/false,
3290 /*class_head_p=*/false,
3292 if (cp_parser_parse_definitely (parser))
3293 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3295 /* In "p->S::~T", look in the scope given by "*p" as well. */
3296 else if (object_scope)
3298 cp_parser_parse_tentatively (parser);
3299 parser->scope = object_scope;
3300 parser->object_scope = NULL_TREE;
3301 parser->qualifying_scope = NULL_TREE;
3303 = cp_parser_class_name (parser,
3304 /*typename_keyword_p=*/false,
3305 /*template_keyword_p=*/false,
3307 /*check_dependency=*/false,
3308 /*class_head_p=*/false,
3310 if (cp_parser_parse_definitely (parser))
3311 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3313 /* Look in the surrounding context. */
3314 parser->scope = NULL_TREE;
3315 parser->object_scope = NULL_TREE;
3316 parser->qualifying_scope = NULL_TREE;
3318 = cp_parser_class_name (parser,
3319 /*typename_keyword_p=*/false,
3320 /*template_keyword_p=*/false,
3322 /*check_dependency=*/false,
3323 /*class_head_p=*/false,
3325 /* If an error occurred, assume that the name of the
3326 destructor is the same as the name of the qualifying
3327 class. That allows us to keep parsing after running
3328 into ill-formed destructor names. */
3329 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3330 return build_nt (BIT_NOT_EXPR, scope);
3331 else if (type_decl == error_mark_node)
3332 return error_mark_node;
3336 A typedef-name that names a class shall not be used as the
3337 identifier in the declarator for a destructor declaration. */
3339 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3340 && !DECL_SELF_REFERENCE_P (type_decl))
3341 error ("typedef-name `%D' used as destructor declarator",
3344 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3348 if (token->keyword == RID_OPERATOR)
3352 /* This could be a template-id, so we try that first. */
3353 cp_parser_parse_tentatively (parser);
3354 /* Try a template-id. */
3355 id = cp_parser_template_id (parser, template_keyword_p,
3356 /*check_dependency_p=*/true,
3358 /* If that worked, we're done. */
3359 if (cp_parser_parse_definitely (parser))
3361 /* We still don't know whether we're looking at an
3362 operator-function-id or a conversion-function-id. */
3363 cp_parser_parse_tentatively (parser);
3364 /* Try an operator-function-id. */
3365 id = cp_parser_operator_function_id (parser);
3366 /* If that didn't work, try a conversion-function-id. */
3367 if (!cp_parser_parse_definitely (parser))
3368 id = cp_parser_conversion_function_id (parser);
3375 cp_parser_error (parser, "expected unqualified-id");
3376 return error_mark_node;
3380 /* Parse an (optional) nested-name-specifier.
3382 nested-name-specifier:
3383 class-or-namespace-name :: nested-name-specifier [opt]
3384 class-or-namespace-name :: template nested-name-specifier [opt]
3386 PARSER->SCOPE should be set appropriately before this function is
3387 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3388 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3391 Sets PARSER->SCOPE to the class (TYPE) or namespace
3392 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3393 it unchanged if there is no nested-name-specifier. Returns the new
3394 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3396 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3397 part of a declaration and/or decl-specifier. */
3400 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3401 bool typename_keyword_p,
3402 bool check_dependency_p,
3404 bool is_declaration)
3406 bool success = false;
3407 tree access_check = NULL_TREE;
3411 /* If the next token corresponds to a nested name specifier, there
3412 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3413 false, it may have been true before, in which case something
3414 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3415 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3416 CHECK_DEPENDENCY_P is false, we have to fall through into the
3418 if (check_dependency_p
3419 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3421 cp_parser_pre_parsed_nested_name_specifier (parser);
3422 return parser->scope;
3425 /* Remember where the nested-name-specifier starts. */
3426 if (cp_parser_parsing_tentatively (parser)
3427 && !cp_parser_committed_to_tentative_parse (parser))
3429 token = cp_lexer_peek_token (parser->lexer);
3430 start = cp_lexer_token_difference (parser->lexer,
3431 parser->lexer->first_token,
3437 push_deferring_access_checks (dk_deferred);
3443 tree saved_qualifying_scope;
3444 bool template_keyword_p;
3446 /* Spot cases that cannot be the beginning of a
3447 nested-name-specifier. */
3448 token = cp_lexer_peek_token (parser->lexer);
3450 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3451 the already parsed nested-name-specifier. */
3452 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3454 /* Grab the nested-name-specifier and continue the loop. */
3455 cp_parser_pre_parsed_nested_name_specifier (parser);
3460 /* Spot cases that cannot be the beginning of a
3461 nested-name-specifier. On the second and subsequent times
3462 through the loop, we look for the `template' keyword. */
3463 if (success && token->keyword == RID_TEMPLATE)
3465 /* A template-id can start a nested-name-specifier. */
3466 else if (token->type == CPP_TEMPLATE_ID)
3470 /* If the next token is not an identifier, then it is
3471 definitely not a class-or-namespace-name. */
3472 if (token->type != CPP_NAME)
3474 /* If the following token is neither a `<' (to begin a
3475 template-id), nor a `::', then we are not looking at a
3476 nested-name-specifier. */
3477 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3478 if (token->type != CPP_SCOPE
3479 && !cp_parser_nth_token_starts_template_argument_list_p
3484 /* The nested-name-specifier is optional, so we parse
3486 cp_parser_parse_tentatively (parser);
3488 /* Look for the optional `template' keyword, if this isn't the
3489 first time through the loop. */
3491 template_keyword_p = cp_parser_optional_template_keyword (parser);
3493 template_keyword_p = false;
3495 /* Save the old scope since the name lookup we are about to do
3496 might destroy it. */
3497 old_scope = parser->scope;
3498 saved_qualifying_scope = parser->qualifying_scope;
3499 /* Parse the qualifying entity. */
3501 = cp_parser_class_or_namespace_name (parser,
3507 /* Look for the `::' token. */
3508 cp_parser_require (parser, CPP_SCOPE, "`::'");
3510 /* If we found what we wanted, we keep going; otherwise, we're
3512 if (!cp_parser_parse_definitely (parser))
3514 bool error_p = false;
3516 /* Restore the OLD_SCOPE since it was valid before the
3517 failed attempt at finding the last
3518 class-or-namespace-name. */
3519 parser->scope = old_scope;
3520 parser->qualifying_scope = saved_qualifying_scope;
3521 /* If the next token is an identifier, and the one after
3522 that is a `::', then any valid interpretation would have
3523 found a class-or-namespace-name. */
3524 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3525 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3527 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3530 token = cp_lexer_consume_token (parser->lexer);
3535 decl = cp_parser_lookup_name_simple (parser, token->value);
3536 if (TREE_CODE (decl) == TEMPLATE_DECL)
3537 error ("`%D' used without template parameters",
3540 cp_parser_name_lookup_error
3541 (parser, token->value, decl,
3542 "is not a class or namespace");
3543 parser->scope = NULL_TREE;
3545 /* Treat this as a successful nested-name-specifier
3550 If the name found is not a class-name (clause
3551 _class_) or namespace-name (_namespace.def_), the
3552 program is ill-formed. */
3555 cp_lexer_consume_token (parser->lexer);
3560 /* We've found one valid nested-name-specifier. */
3562 /* Make sure we look in the right scope the next time through
3564 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3565 ? TREE_TYPE (new_scope)
3567 /* If it is a class scope, try to complete it; we are about to
3568 be looking up names inside the class. */
3569 if (TYPE_P (parser->scope)
3570 /* Since checking types for dependency can be expensive,
3571 avoid doing it if the type is already complete. */
3572 && !COMPLETE_TYPE_P (parser->scope)
3573 /* Do not try to complete dependent types. */
3574 && !dependent_type_p (parser->scope))
3575 complete_type (parser->scope);
3578 /* Retrieve any deferred checks. Do not pop this access checks yet
3579 so the memory will not be reclaimed during token replacing below. */
3580 access_check = get_deferred_access_checks ();
3582 /* If parsing tentatively, replace the sequence of tokens that makes
3583 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3584 token. That way, should we re-parse the token stream, we will
3585 not have to repeat the effort required to do the parse, nor will
3586 we issue duplicate error messages. */
3587 if (success && start >= 0)
3589 /* Find the token that corresponds to the start of the
3591 token = cp_lexer_advance_token (parser->lexer,
3592 parser->lexer->first_token,
3595 /* Reset the contents of the START token. */
3596 token->type = CPP_NESTED_NAME_SPECIFIER;
3597 token->value = build_tree_list (access_check, parser->scope);
3598 TREE_TYPE (token->value) = parser->qualifying_scope;
3599 token->keyword = RID_MAX;
3600 /* Purge all subsequent tokens. */
3601 cp_lexer_purge_tokens_after (parser->lexer, token);
3604 pop_deferring_access_checks ();
3605 return success ? parser->scope : NULL_TREE;
3608 /* Parse a nested-name-specifier. See
3609 cp_parser_nested_name_specifier_opt for details. This function
3610 behaves identically, except that it will an issue an error if no
3611 nested-name-specifier is present, and it will return
3612 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3616 cp_parser_nested_name_specifier (cp_parser *parser,
3617 bool typename_keyword_p,
3618 bool check_dependency_p,
3620 bool is_declaration)
3624 /* Look for the nested-name-specifier. */
3625 scope = cp_parser_nested_name_specifier_opt (parser,
3630 /* If it was not present, issue an error message. */
3633 cp_parser_error (parser, "expected nested-name-specifier");
3634 parser->scope = NULL_TREE;
3635 return error_mark_node;
3641 /* Parse a class-or-namespace-name.
3643 class-or-namespace-name:
3647 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3648 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3649 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3650 TYPE_P is TRUE iff the next name should be taken as a class-name,
3651 even the same name is declared to be another entity in the same
3654 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3655 specified by the class-or-namespace-name. If neither is found the
3656 ERROR_MARK_NODE is returned. */
3659 cp_parser_class_or_namespace_name (cp_parser *parser,
3660 bool typename_keyword_p,
3661 bool template_keyword_p,
3662 bool check_dependency_p,
3664 bool is_declaration)
3667 tree saved_qualifying_scope;
3668 tree saved_object_scope;
3672 /* Before we try to parse the class-name, we must save away the
3673 current PARSER->SCOPE since cp_parser_class_name will destroy
3675 saved_scope = parser->scope;
3676 saved_qualifying_scope = parser->qualifying_scope;
3677 saved_object_scope = parser->object_scope;
3678 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3679 there is no need to look for a namespace-name. */
3680 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3682 cp_parser_parse_tentatively (parser);
3683 scope = cp_parser_class_name (parser,
3688 /*class_head_p=*/false,
3690 /* If that didn't work, try for a namespace-name. */
3691 if (!only_class_p && !cp_parser_parse_definitely (parser))
3693 /* Restore the saved scope. */
3694 parser->scope = saved_scope;
3695 parser->qualifying_scope = saved_qualifying_scope;
3696 parser->object_scope = saved_object_scope;
3697 /* If we are not looking at an identifier followed by the scope
3698 resolution operator, then this is not part of a
3699 nested-name-specifier. (Note that this function is only used
3700 to parse the components of a nested-name-specifier.) */
3701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3702 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3703 return error_mark_node;
3704 scope = cp_parser_namespace_name (parser);
3710 /* Parse a postfix-expression.
3714 postfix-expression [ expression ]
3715 postfix-expression ( expression-list [opt] )
3716 simple-type-specifier ( expression-list [opt] )
3717 typename :: [opt] nested-name-specifier identifier
3718 ( expression-list [opt] )
3719 typename :: [opt] nested-name-specifier template [opt] template-id
3720 ( expression-list [opt] )
3721 postfix-expression . template [opt] id-expression
3722 postfix-expression -> template [opt] id-expression
3723 postfix-expression . pseudo-destructor-name
3724 postfix-expression -> pseudo-destructor-name
3725 postfix-expression ++
3726 postfix-expression --
3727 dynamic_cast < type-id > ( expression )
3728 static_cast < type-id > ( expression )
3729 reinterpret_cast < type-id > ( expression )
3730 const_cast < type-id > ( expression )
3731 typeid ( expression )
3737 ( type-id ) { initializer-list , [opt] }
3739 This extension is a GNU version of the C99 compound-literal
3740 construct. (The C99 grammar uses `type-name' instead of `type-id',
3741 but they are essentially the same concept.)
3743 If ADDRESS_P is true, the postfix expression is the operand of the
3746 Returns a representation of the expression. */
3749 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3753 cp_id_kind idk = CP_ID_KIND_NONE;
3754 tree postfix_expression = NULL_TREE;
3755 /* Non-NULL only if the current postfix-expression can be used to
3756 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3757 class used to qualify the member. */
3758 tree qualifying_class = NULL_TREE;
3760 /* Peek at the next token. */
3761 token = cp_lexer_peek_token (parser->lexer);
3762 /* Some of the productions are determined by keywords. */
3763 keyword = token->keyword;
3773 const char *saved_message;
3775 /* All of these can be handled in the same way from the point
3776 of view of parsing. Begin by consuming the token
3777 identifying the cast. */
3778 cp_lexer_consume_token (parser->lexer);
3780 /* New types cannot be defined in the cast. */
3781 saved_message = parser->type_definition_forbidden_message;
3782 parser->type_definition_forbidden_message
3783 = "types may not be defined in casts";
3785 /* Look for the opening `<'. */
3786 cp_parser_require (parser, CPP_LESS, "`<'");
3787 /* Parse the type to which we are casting. */
3788 type = cp_parser_type_id (parser);
3789 /* Look for the closing `>'. */
3790 cp_parser_require (parser, CPP_GREATER, "`>'");
3791 /* Restore the old message. */
3792 parser->type_definition_forbidden_message = saved_message;
3794 /* And the expression which is being cast. */
3795 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3796 expression = cp_parser_expression (parser);
3797 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3799 /* Only type conversions to integral or enumeration types
3800 can be used in constant-expressions. */
3801 if (parser->integral_constant_expression_p
3802 && !dependent_type_p (type)
3803 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3804 && (cp_parser_non_integral_constant_expression
3806 "a cast to a type other than an integral or "
3807 "enumeration type")))
3808 return error_mark_node;
3814 = build_dynamic_cast (type, expression);
3818 = build_static_cast (type, expression);
3822 = build_reinterpret_cast (type, expression);
3826 = build_const_cast (type, expression);
3837 const char *saved_message;
3838 bool saved_in_type_id_in_expr_p;
3840 /* Consume the `typeid' token. */
3841 cp_lexer_consume_token (parser->lexer);
3842 /* Look for the `(' token. */
3843 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3844 /* Types cannot be defined in a `typeid' expression. */
3845 saved_message = parser->type_definition_forbidden_message;
3846 parser->type_definition_forbidden_message
3847 = "types may not be defined in a `typeid\' expression";
3848 /* We can't be sure yet whether we're looking at a type-id or an
3850 cp_parser_parse_tentatively (parser);
3851 /* Try a type-id first. */
3852 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3853 parser->in_type_id_in_expr_p = true;
3854 type = cp_parser_type_id (parser);
3855 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3856 /* Look for the `)' token. Otherwise, we can't be sure that
3857 we're not looking at an expression: consider `typeid (int
3858 (3))', for example. */
3859 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3860 /* If all went well, simply lookup the type-id. */
3861 if (cp_parser_parse_definitely (parser))
3862 postfix_expression = get_typeid (type);
3863 /* Otherwise, fall back to the expression variant. */
3868 /* Look for an expression. */
3869 expression = cp_parser_expression (parser);
3870 /* Compute its typeid. */
3871 postfix_expression = build_typeid (expression);
3872 /* Look for the `)' token. */
3873 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3875 /* `typeid' may not appear in an integral constant expression. */
3876 if (cp_parser_non_integral_constant_expression(parser,
3877 "`typeid' operator"))
3878 return error_mark_node;
3879 /* Restore the saved message. */
3880 parser->type_definition_forbidden_message = saved_message;
3886 bool template_p = false;
3890 /* Consume the `typename' token. */
3891 cp_lexer_consume_token (parser->lexer);
3892 /* Look for the optional `::' operator. */
3893 cp_parser_global_scope_opt (parser,
3894 /*current_scope_valid_p=*/false);
3895 /* Look for the nested-name-specifier. */
3896 cp_parser_nested_name_specifier (parser,
3897 /*typename_keyword_p=*/true,
3898 /*check_dependency_p=*/true,
3900 /*is_declaration=*/true);
3901 /* Look for the optional `template' keyword. */
3902 template_p = cp_parser_optional_template_keyword (parser);
3903 /* We don't know whether we're looking at a template-id or an
3905 cp_parser_parse_tentatively (parser);
3906 /* Try a template-id. */
3907 id = cp_parser_template_id (parser, template_p,
3908 /*check_dependency_p=*/true,
3909 /*is_declaration=*/true);
3910 /* If that didn't work, try an identifier. */
3911 if (!cp_parser_parse_definitely (parser))
3912 id = cp_parser_identifier (parser);
3913 /* If we look up a template-id in a non-dependent qualifying
3914 scope, there's no need to create a dependent type. */
3915 if (TREE_CODE (id) == TYPE_DECL
3916 && !dependent_type_p (parser->scope))
3917 type = TREE_TYPE (id);
3918 /* Create a TYPENAME_TYPE to represent the type to which the
3919 functional cast is being performed. */
3921 type = make_typename_type (parser->scope, id,
3924 postfix_expression = cp_parser_functional_cast (parser, type);
3932 /* If the next thing is a simple-type-specifier, we may be
3933 looking at a functional cast. We could also be looking at
3934 an id-expression. So, we try the functional cast, and if
3935 that doesn't work we fall back to the primary-expression. */
3936 cp_parser_parse_tentatively (parser);
3937 /* Look for the simple-type-specifier. */
3938 type = cp_parser_simple_type_specifier (parser,
3939 /*decl_specs=*/NULL,
3940 CP_PARSER_FLAGS_NONE);
3941 /* Parse the cast itself. */
3942 if (!cp_parser_error_occurred (parser))
3944 = cp_parser_functional_cast (parser, type);
3945 /* If that worked, we're done. */
3946 if (cp_parser_parse_definitely (parser))
3949 /* If the functional-cast didn't work out, try a
3950 compound-literal. */
3951 if (cp_parser_allow_gnu_extensions_p (parser)
3952 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3954 tree initializer_list = NULL_TREE;
3955 bool saved_in_type_id_in_expr_p;
3957 cp_parser_parse_tentatively (parser);
3958 /* Consume the `('. */
3959 cp_lexer_consume_token (parser->lexer);
3960 /* Parse the type. */
3961 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3962 parser->in_type_id_in_expr_p = true;
3963 type = cp_parser_type_id (parser);
3964 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3965 /* Look for the `)'. */
3966 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3967 /* Look for the `{'. */
3968 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3969 /* If things aren't going well, there's no need to
3971 if (!cp_parser_error_occurred (parser))
3973 bool non_constant_p;
3974 /* Parse the initializer-list. */
3976 = cp_parser_initializer_list (parser, &non_constant_p);
3977 /* Allow a trailing `,'. */
3978 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3979 cp_lexer_consume_token (parser->lexer);
3980 /* Look for the final `}'. */
3981 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3983 /* If that worked, we're definitely looking at a
3984 compound-literal expression. */
3985 if (cp_parser_parse_definitely (parser))
3987 /* Warn the user that a compound literal is not
3988 allowed in standard C++. */
3990 pedwarn ("ISO C++ forbids compound-literals");
3991 /* Form the representation of the compound-literal. */
3993 = finish_compound_literal (type, initializer_list);
3998 /* It must be a primary-expression. */
3999 postfix_expression = cp_parser_primary_expression (parser,
4006 /* If we were avoiding committing to the processing of a
4007 qualified-id until we knew whether or not we had a
4008 pointer-to-member, we now know. */
4009 if (qualifying_class)
4013 /* Peek at the next token. */
4014 token = cp_lexer_peek_token (parser->lexer);
4015 done = (token->type != CPP_OPEN_SQUARE
4016 && token->type != CPP_OPEN_PAREN
4017 && token->type != CPP_DOT
4018 && token->type != CPP_DEREF
4019 && token->type != CPP_PLUS_PLUS
4020 && token->type != CPP_MINUS_MINUS);
4022 postfix_expression = finish_qualified_id_expr (qualifying_class,
4027 return postfix_expression;
4030 /* Keep looping until the postfix-expression is complete. */
4033 if (idk == CP_ID_KIND_UNQUALIFIED
4034 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4035 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4036 /* It is not a Koenig lookup function call. */
4038 = unqualified_name_lookup_error (postfix_expression);
4040 /* Peek at the next token. */
4041 token = cp_lexer_peek_token (parser->lexer);
4043 switch (token->type)
4045 case CPP_OPEN_SQUARE:
4047 = cp_parser_postfix_open_square_expression (parser,
4050 idk = CP_ID_KIND_NONE;
4053 case CPP_OPEN_PAREN:
4054 /* postfix-expression ( expression-list [opt] ) */
4057 tree args = (cp_parser_parenthesized_expression_list
4058 (parser, false, /*non_constant_p=*/NULL));
4060 if (args == error_mark_node)
4062 postfix_expression = error_mark_node;
4066 /* Function calls are not permitted in
4067 constant-expressions. */
4068 if (cp_parser_non_integral_constant_expression (parser,
4071 postfix_expression = error_mark_node;
4076 if (idk == CP_ID_KIND_UNQUALIFIED)
4078 /* We do not perform argument-dependent lookup if
4079 normal lookup finds a non-function, in accordance
4080 with the expected resolution of DR 218. */
4082 && (is_overloaded_fn (postfix_expression)
4083 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
4087 = perform_koenig_lookup (postfix_expression, args);
4089 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4091 = unqualified_fn_lookup_error (postfix_expression);
4094 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4096 tree instance = TREE_OPERAND (postfix_expression, 0);
4097 tree fn = TREE_OPERAND (postfix_expression, 1);
4099 if (processing_template_decl
4100 && (type_dependent_expression_p (instance)
4101 || (!BASELINK_P (fn)
4102 && TREE_CODE (fn) != FIELD_DECL)
4103 || type_dependent_expression_p (fn)
4104 || any_type_dependent_arguments_p (args)))
4107 = build_min_nt (CALL_EXPR, postfix_expression,
4112 if (BASELINK_P (fn))
4114 = (build_new_method_call
4115 (instance, fn, args, NULL_TREE,
4116 (idk == CP_ID_KIND_QUALIFIED
4117 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4120 = finish_call_expr (postfix_expression, args,
4121 /*disallow_virtual=*/false,
4122 /*koenig_p=*/false);
4124 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4125 || TREE_CODE (postfix_expression) == MEMBER_REF
4126 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4127 postfix_expression = (build_offset_ref_call_from_tree
4128 (postfix_expression, args));
4129 else if (idk == CP_ID_KIND_QUALIFIED)
4130 /* A call to a static class member, or a namespace-scope
4133 = finish_call_expr (postfix_expression, args,
4134 /*disallow_virtual=*/true,
4137 /* All other function calls. */
4139 = finish_call_expr (postfix_expression, args,
4140 /*disallow_virtual=*/false,
4143 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4144 idk = CP_ID_KIND_NONE;
4150 /* postfix-expression . template [opt] id-expression
4151 postfix-expression . pseudo-destructor-name
4152 postfix-expression -> template [opt] id-expression
4153 postfix-expression -> pseudo-destructor-name */
4155 /* Consume the `.' or `->' operator. */
4156 cp_lexer_consume_token (parser->lexer);
4159 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4165 /* postfix-expression ++ */
4166 /* Consume the `++' token. */
4167 cp_lexer_consume_token (parser->lexer);
4168 /* Generate a representation for the complete expression. */
4170 = finish_increment_expr (postfix_expression,
4171 POSTINCREMENT_EXPR);
4172 /* Increments may not appear in constant-expressions. */
4173 if (cp_parser_non_integral_constant_expression (parser,
4175 postfix_expression = error_mark_node;
4176 idk = CP_ID_KIND_NONE;
4179 case CPP_MINUS_MINUS:
4180 /* postfix-expression -- */
4181 /* Consume the `--' token. */
4182 cp_lexer_consume_token (parser->lexer);
4183 /* Generate a representation for the complete expression. */
4185 = finish_increment_expr (postfix_expression,
4186 POSTDECREMENT_EXPR);
4187 /* Decrements may not appear in constant-expressions. */
4188 if (cp_parser_non_integral_constant_expression (parser,
4190 postfix_expression = error_mark_node;
4191 idk = CP_ID_KIND_NONE;
4195 return postfix_expression;
4199 /* We should never get here. */
4201 return error_mark_node;
4204 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4205 by cp_parser_builtin_offsetof. We're looking for
4207 postfix-expression [ expression ]
4209 FOR_OFFSETOF is set if we're being called in that context, which
4210 changes how we deal with integer constant expressions. */
4213 cp_parser_postfix_open_square_expression (cp_parser *parser,
4214 tree postfix_expression,
4219 /* Consume the `[' token. */
4220 cp_lexer_consume_token (parser->lexer);
4222 /* Parse the index expression. */
4223 /* ??? For offsetof, there is a question of what to allow here. If
4224 offsetof is not being used in an integral constant expression context,
4225 then we *could* get the right answer by computing the value at runtime.
4226 If we are in an integral constant expression context, then we might
4227 could accept any constant expression; hard to say without analysis.
4228 Rather than open the barn door too wide right away, allow only integer
4229 constant expresions here. */
4231 index = cp_parser_constant_expression (parser, false, NULL);
4233 index = cp_parser_expression (parser);
4235 /* Look for the closing `]'. */
4236 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4238 /* Build the ARRAY_REF. */
4239 postfix_expression = grok_array_decl (postfix_expression, index);
4241 /* When not doing offsetof, array references are not permitted in
4242 constant-expressions. */
4244 && (cp_parser_non_integral_constant_expression
4245 (parser, "an array reference")))
4246 postfix_expression = error_mark_node;
4248 return postfix_expression;
4251 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4252 by cp_parser_builtin_offsetof. We're looking for
4254 postfix-expression . template [opt] id-expression
4255 postfix-expression . pseudo-destructor-name
4256 postfix-expression -> template [opt] id-expression
4257 postfix-expression -> pseudo-destructor-name
4259 FOR_OFFSETOF is set if we're being called in that context. That sorta
4260 limits what of the above we'll actually accept, but nevermind.
4261 TOKEN_TYPE is the "." or "->" token, which will already have been
4262 removed from the stream. */
4265 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4266 enum cpp_ttype token_type,
4267 tree postfix_expression,
4268 bool for_offsetof, cp_id_kind *idk)
4273 tree scope = NULL_TREE;
4275 /* If this is a `->' operator, dereference the pointer. */
4276 if (token_type == CPP_DEREF)
4277 postfix_expression = build_x_arrow (postfix_expression);
4278 /* Check to see whether or not the expression is type-dependent. */
4279 dependent_p = type_dependent_expression_p (postfix_expression);
4280 /* The identifier following the `->' or `.' is not qualified. */
4281 parser->scope = NULL_TREE;
4282 parser->qualifying_scope = NULL_TREE;
4283 parser->object_scope = NULL_TREE;
4284 *idk = CP_ID_KIND_NONE;
4285 /* Enter the scope corresponding to the type of the object
4286 given by the POSTFIX_EXPRESSION. */
4287 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4289 scope = TREE_TYPE (postfix_expression);
4290 /* According to the standard, no expression should ever have
4291 reference type. Unfortunately, we do not currently match
4292 the standard in this respect in that our internal representation
4293 of an expression may have reference type even when the standard
4294 says it does not. Therefore, we have to manually obtain the
4295 underlying type here. */
4296 scope = non_reference (scope);
4297 /* The type of the POSTFIX_EXPRESSION must be complete. */
4298 scope = complete_type_or_else (scope, NULL_TREE);
4299 /* Let the name lookup machinery know that we are processing a
4300 class member access expression. */
4301 parser->context->object_type = scope;
4302 /* If something went wrong, we want to be able to discern that case,
4303 as opposed to the case where there was no SCOPE due to the type
4304 of expression being dependent. */
4306 scope = error_mark_node;
4307 /* If the SCOPE was erroneous, make the various semantic analysis
4308 functions exit quickly -- and without issuing additional error
4310 if (scope == error_mark_node)
4311 postfix_expression = error_mark_node;
4314 /* If the SCOPE is not a scalar type, we are looking at an
4315 ordinary class member access expression, rather than a
4316 pseudo-destructor-name. */
4317 if (!scope || !SCALAR_TYPE_P (scope))
4319 template_p = cp_parser_optional_template_keyword (parser);
4320 /* Parse the id-expression. */
4321 name = cp_parser_id_expression (parser, template_p,
4322 /*check_dependency_p=*/true,
4323 /*template_p=*/NULL,
4324 /*declarator_p=*/false);
4325 /* In general, build a SCOPE_REF if the member name is qualified.
4326 However, if the name was not dependent and has already been
4327 resolved; there is no need to build the SCOPE_REF. For example;
4329 struct X { void f(); };
4330 template <typename T> void f(T* t) { t->X::f(); }
4332 Even though "t" is dependent, "X::f" is not and has been resolved
4333 to a BASELINK; there is no need to include scope information. */
4335 /* But we do need to remember that there was an explicit scope for
4336 virtual function calls. */
4338 *idk = CP_ID_KIND_QUALIFIED;
4340 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4342 name = build_nt (SCOPE_REF, parser->scope, name);
4343 parser->scope = NULL_TREE;
4344 parser->qualifying_scope = NULL_TREE;
4345 parser->object_scope = NULL_TREE;
4347 if (scope && name && BASELINK_P (name))
4348 adjust_result_of_qualified_name_lookup
4349 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4351 = finish_class_member_access_expr (postfix_expression, name);
4353 /* Otherwise, try the pseudo-destructor-name production. */
4359 /* Parse the pseudo-destructor-name. */
4360 cp_parser_pseudo_destructor_name (parser, &s, &type);
4361 /* Form the call. */
4363 = finish_pseudo_destructor_expr (postfix_expression,
4364 s, TREE_TYPE (type));
4367 /* We no longer need to look up names in the scope of the object on
4368 the left-hand side of the `.' or `->' operator. */
4369 parser->context->object_type = NULL_TREE;
4371 /* Outside of offsetof, these operators may not appear in
4372 constant-expressions. */
4374 && (cp_parser_non_integral_constant_expression
4375 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4376 postfix_expression = error_mark_node;
4378 return postfix_expression;
4381 /* Parse a parenthesized expression-list.
4384 assignment-expression
4385 expression-list, assignment-expression
4390 identifier, expression-list
4392 Returns a TREE_LIST. The TREE_VALUE of each node is a
4393 representation of an assignment-expression. Note that a TREE_LIST
4394 is returned even if there is only a single expression in the list.
4395 error_mark_node is returned if the ( and or ) are
4396 missing. NULL_TREE is returned on no expressions. The parentheses
4397 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4398 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4399 indicates whether or not all of the expressions in the list were
4403 cp_parser_parenthesized_expression_list (cp_parser* parser,
4404 bool is_attribute_list,
4405 bool *non_constant_p)
4407 tree expression_list = NULL_TREE;
4408 tree identifier = NULL_TREE;
4410 /* Assume all the expressions will be constant. */
4412 *non_constant_p = false;
4414 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4415 return error_mark_node;
4417 /* Consume expressions until there are no more. */
4418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4423 /* At the beginning of attribute lists, check to see if the
4424 next token is an identifier. */
4425 if (is_attribute_list
4426 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4430 /* Consume the identifier. */
4431 token = cp_lexer_consume_token (parser->lexer);
4432 /* Save the identifier. */
4433 identifier = token->value;
4437 /* Parse the next assignment-expression. */
4440 bool expr_non_constant_p;
4441 expr = (cp_parser_constant_expression
4442 (parser, /*allow_non_constant_p=*/true,
4443 &expr_non_constant_p));
4444 if (expr_non_constant_p)
4445 *non_constant_p = true;
4448 expr = cp_parser_assignment_expression (parser);
4450 /* Add it to the list. We add error_mark_node
4451 expressions to the list, so that we can still tell if
4452 the correct form for a parenthesized expression-list
4453 is found. That gives better errors. */
4454 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4456 if (expr == error_mark_node)
4460 /* After the first item, attribute lists look the same as
4461 expression lists. */
4462 is_attribute_list = false;
4465 /* If the next token isn't a `,', then we are done. */
4466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4469 /* Otherwise, consume the `,' and keep going. */
4470 cp_lexer_consume_token (parser->lexer);
4473 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4478 /* We try and resync to an unnested comma, as that will give the
4479 user better diagnostics. */
4480 ending = cp_parser_skip_to_closing_parenthesis (parser,
4481 /*recovering=*/true,
4483 /*consume_paren=*/true);
4487 return error_mark_node;
4490 /* We built up the list in reverse order so we must reverse it now. */
4491 expression_list = nreverse (expression_list);
4493 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4495 return expression_list;
4498 /* Parse a pseudo-destructor-name.
4500 pseudo-destructor-name:
4501 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4502 :: [opt] nested-name-specifier template template-id :: ~ type-name
4503 :: [opt] nested-name-specifier [opt] ~ type-name
4505 If either of the first two productions is used, sets *SCOPE to the
4506 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4507 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4508 or ERROR_MARK_NODE if the parse fails. */
4511 cp_parser_pseudo_destructor_name (cp_parser* parser,
4515 bool nested_name_specifier_p;
4517 /* Assume that things will not work out. */
4518 *type = error_mark_node;
4520 /* Look for the optional `::' operator. */
4521 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4522 /* Look for the optional nested-name-specifier. */
4523 nested_name_specifier_p
4524 = (cp_parser_nested_name_specifier_opt (parser,
4525 /*typename_keyword_p=*/false,
4526 /*check_dependency_p=*/true,
4528 /*is_declaration=*/true)
4530 /* Now, if we saw a nested-name-specifier, we might be doing the
4531 second production. */
4532 if (nested_name_specifier_p
4533 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4535 /* Consume the `template' keyword. */
4536 cp_lexer_consume_token (parser->lexer);
4537 /* Parse the template-id. */
4538 cp_parser_template_id (parser,
4539 /*template_keyword_p=*/true,
4540 /*check_dependency_p=*/false,
4541 /*is_declaration=*/true);
4542 /* Look for the `::' token. */
4543 cp_parser_require (parser, CPP_SCOPE, "`::'");
4545 /* If the next token is not a `~', then there might be some
4546 additional qualification. */
4547 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4549 /* Look for the type-name. */
4550 *scope = TREE_TYPE (cp_parser_type_name (parser));
4552 if (*scope == error_mark_node)
4555 /* If we don't have ::~, then something has gone wrong. Since
4556 the only caller of this function is looking for something
4557 after `.' or `->' after a scalar type, most likely the
4558 program is trying to get a member of a non-aggregate
4560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4561 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4563 cp_parser_error (parser, "request for member of non-aggregate type");
4567 /* Look for the `::' token. */
4568 cp_parser_require (parser, CPP_SCOPE, "`::'");
4573 /* Look for the `~'. */
4574 cp_parser_require (parser, CPP_COMPL, "`~'");
4575 /* Look for the type-name again. We are not responsible for
4576 checking that it matches the first type-name. */
4577 *type = cp_parser_type_name (parser);
4580 /* Parse a unary-expression.
4586 unary-operator cast-expression
4587 sizeof unary-expression
4595 __extension__ cast-expression
4596 __alignof__ unary-expression
4597 __alignof__ ( type-id )
4598 __real__ cast-expression
4599 __imag__ cast-expression
4602 ADDRESS_P is true iff the unary-expression is appearing as the
4603 operand of the `&' operator.
4605 Returns a representation of the expression. */
4608 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4611 enum tree_code unary_operator;
4613 /* Peek at the next token. */
4614 token = cp_lexer_peek_token (parser->lexer);
4615 /* Some keywords give away the kind of expression. */
4616 if (token->type == CPP_KEYWORD)
4618 enum rid keyword = token->keyword;
4628 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4629 /* Consume the token. */
4630 cp_lexer_consume_token (parser->lexer);
4631 /* Parse the operand. */
4632 operand = cp_parser_sizeof_operand (parser, keyword);
4634 if (TYPE_P (operand))
4635 return cxx_sizeof_or_alignof_type (operand, op, true);
4637 return cxx_sizeof_or_alignof_expr (operand, op);
4641 return cp_parser_new_expression (parser);
4644 return cp_parser_delete_expression (parser);
4648 /* The saved value of the PEDANTIC flag. */
4652 /* Save away the PEDANTIC flag. */
4653 cp_parser_extension_opt (parser, &saved_pedantic);
4654 /* Parse the cast-expression. */
4655 expr = cp_parser_simple_cast_expression (parser);
4656 /* Restore the PEDANTIC flag. */
4657 pedantic = saved_pedantic;
4667 /* Consume the `__real__' or `__imag__' token. */
4668 cp_lexer_consume_token (parser->lexer);
4669 /* Parse the cast-expression. */
4670 expression = cp_parser_simple_cast_expression (parser);
4671 /* Create the complete representation. */
4672 return build_x_unary_op ((keyword == RID_REALPART
4673 ? REALPART_EXPR : IMAGPART_EXPR),
4683 /* Look for the `:: new' and `:: delete', which also signal the
4684 beginning of a new-expression, or delete-expression,
4685 respectively. If the next token is `::', then it might be one of
4687 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4691 /* See if the token after the `::' is one of the keywords in
4692 which we're interested. */
4693 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4694 /* If it's `new', we have a new-expression. */
4695 if (keyword == RID_NEW)
4696 return cp_parser_new_expression (parser);
4697 /* Similarly, for `delete'. */
4698 else if (keyword == RID_DELETE)
4699 return cp_parser_delete_expression (parser);
4702 /* Look for a unary operator. */
4703 unary_operator = cp_parser_unary_operator (token);
4704 /* The `++' and `--' operators can be handled similarly, even though
4705 they are not technically unary-operators in the grammar. */
4706 if (unary_operator == ERROR_MARK)
4708 if (token->type == CPP_PLUS_PLUS)
4709 unary_operator = PREINCREMENT_EXPR;
4710 else if (token->type == CPP_MINUS_MINUS)
4711 unary_operator = PREDECREMENT_EXPR;
4712 /* Handle the GNU address-of-label extension. */
4713 else if (cp_parser_allow_gnu_extensions_p (parser)
4714 && token->type == CPP_AND_AND)
4718 /* Consume the '&&' token. */
4719 cp_lexer_consume_token (parser->lexer);
4720 /* Look for the identifier. */
4721 identifier = cp_parser_identifier (parser);
4722 /* Create an expression representing the address. */
4723 return finish_label_address_expr (identifier);
4726 if (unary_operator != ERROR_MARK)
4728 tree cast_expression;
4729 tree expression = error_mark_node;
4730 const char *non_constant_p = NULL;
4732 /* Consume the operator token. */
4733 token = cp_lexer_consume_token (parser->lexer);
4734 /* Parse the cast-expression. */
4736 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4737 /* Now, build an appropriate representation. */
4738 switch (unary_operator)
4741 non_constant_p = "`*'";
4742 expression = build_x_indirect_ref (cast_expression, "unary *");
4746 non_constant_p = "`&'";
4749 expression = build_x_unary_op (unary_operator, cast_expression);
4752 case PREINCREMENT_EXPR:
4753 case PREDECREMENT_EXPR:
4754 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4759 case TRUTH_NOT_EXPR:
4760 expression = finish_unary_op_expr (unary_operator, cast_expression);
4768 && cp_parser_non_integral_constant_expression (parser,
4770 expression = error_mark_node;
4775 return cp_parser_postfix_expression (parser, address_p);
4778 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4779 unary-operator, the corresponding tree code is returned. */
4781 static enum tree_code
4782 cp_parser_unary_operator (cp_token* token)
4784 switch (token->type)
4787 return INDIRECT_REF;
4793 return CONVERT_EXPR;
4799 return TRUTH_NOT_EXPR;
4802 return BIT_NOT_EXPR;
4809 /* Parse a new-expression.
4812 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4813 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4815 Returns a representation of the expression. */
4818 cp_parser_new_expression (cp_parser* parser)
4820 bool global_scope_p;
4826 /* Look for the optional `::' operator. */
4828 = (cp_parser_global_scope_opt (parser,
4829 /*current_scope_valid_p=*/false)
4831 /* Look for the `new' operator. */
4832 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4833 /* There's no easy way to tell a new-placement from the
4834 `( type-id )' construct. */
4835 cp_parser_parse_tentatively (parser);
4836 /* Look for a new-placement. */
4837 placement = cp_parser_new_placement (parser);
4838 /* If that didn't work out, there's no new-placement. */
4839 if (!cp_parser_parse_definitely (parser))
4840 placement = NULL_TREE;
4842 /* If the next token is a `(', then we have a parenthesized
4844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4846 /* Consume the `('. */
4847 cp_lexer_consume_token (parser->lexer);
4848 /* Parse the type-id. */
4849 type = cp_parser_type_id (parser);
4850 /* Look for the closing `)'. */
4851 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4852 /* There should not be a direct-new-declarator in this production,
4853 but GCC used to allowed this, so we check and emit a sensible error
4854 message for this case. */
4855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4857 error ("array bound forbidden after parenthesized type-id");
4858 inform ("try removing the parentheses around the type-id");
4859 cp_parser_direct_new_declarator (parser);
4861 nelts = integer_one_node;
4863 /* Otherwise, there must be a new-type-id. */
4865 type = cp_parser_new_type_id (parser, &nelts);
4867 /* If the next token is a `(', then we have a new-initializer. */
4868 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4869 initializer = cp_parser_new_initializer (parser);
4871 initializer = NULL_TREE;
4873 /* A new-expression may not appear in an integral constant
4875 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4876 return error_mark_node;
4878 /* Create a representation of the new-expression. */
4879 return build_new (placement, type, nelts, initializer, global_scope_p);
4882 /* Parse a new-placement.
4887 Returns the same representation as for an expression-list. */
4890 cp_parser_new_placement (cp_parser* parser)
4892 tree expression_list;
4894 /* Parse the expression-list. */
4895 expression_list = (cp_parser_parenthesized_expression_list
4896 (parser, false, /*non_constant_p=*/NULL));
4898 return expression_list;
4901 /* Parse a new-type-id.
4904 type-specifier-seq new-declarator [opt]
4906 Returns the TYPE allocated. If the new-type-id indicates an array
4907 type, *NELTS is set to the number of elements in the last array
4908 bound; the TYPE will not include the last array bound. */
4911 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4913 cp_decl_specifier_seq type_specifier_seq;
4914 cp_declarator *new_declarator;
4915 cp_declarator *declarator;
4916 cp_declarator *outer_declarator;
4917 const char *saved_message;
4920 /* The type-specifier sequence must not contain type definitions.
4921 (It cannot contain declarations of new types either, but if they
4922 are not definitions we will catch that because they are not
4924 saved_message = parser->type_definition_forbidden_message;
4925 parser->type_definition_forbidden_message
4926 = "types may not be defined in a new-type-id";
4927 /* Parse the type-specifier-seq. */
4928 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4929 /* Restore the old message. */
4930 parser->type_definition_forbidden_message = saved_message;
4931 /* Parse the new-declarator. */
4932 new_declarator = cp_parser_new_declarator_opt (parser);
4934 /* Determine the number of elements in the last array dimension, if
4937 /* Skip down to the last array dimension. */
4938 declarator = new_declarator;
4939 outer_declarator = NULL;
4940 while (declarator && (declarator->kind == cdk_pointer
4941 || declarator->kind == cdk_ptrmem))
4943 outer_declarator = declarator;
4944 declarator = declarator->declarator;
4947 && declarator->kind == cdk_array
4948 && declarator->declarator
4949 && declarator->declarator->kind == cdk_array)
4951 outer_declarator = declarator;
4952 declarator = declarator->declarator;
4955 if (declarator && declarator->kind == cdk_array)
4957 *nelts = declarator->u.array.bounds;
4958 if (*nelts == error_mark_node)
4959 *nelts = integer_one_node;
4960 else if (!processing_template_decl)
4962 if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4964 pedwarn ("size in array new must have integral type");
4965 *nelts = save_expr (cp_convert (sizetype, *nelts));
4966 if (*nelts == integer_zero_node)
4967 warning ("zero size array reserves no space");
4969 if (outer_declarator)
4970 outer_declarator->declarator = declarator->declarator;
4972 new_declarator = NULL;
4975 type = groktypename (&type_specifier_seq, new_declarator);
4976 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4978 *nelts = array_type_nelts_top (type);
4979 type = TREE_TYPE (type);
4984 /* Parse an (optional) new-declarator.
4987 ptr-operator new-declarator [opt]
4988 direct-new-declarator
4990 Returns the declarator. */
4992 static cp_declarator *
4993 cp_parser_new_declarator_opt (cp_parser* parser)
4995 enum tree_code code;
4997 cp_cv_quals cv_quals;
4999 /* We don't know if there's a ptr-operator next, or not. */
5000 cp_parser_parse_tentatively (parser);
5001 /* Look for a ptr-operator. */
5002 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5003 /* If that worked, look for more new-declarators. */
5004 if (cp_parser_parse_definitely (parser))
5006 cp_declarator *declarator;
5008 /* Parse another optional declarator. */
5009 declarator = cp_parser_new_declarator_opt (parser);
5011 /* Create the representation of the declarator. */
5013 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5014 else if (code == INDIRECT_REF)
5015 declarator = make_pointer_declarator (cv_quals, declarator);
5017 declarator = make_reference_declarator (cv_quals, declarator);
5022 /* If the next token is a `[', there is a direct-new-declarator. */
5023 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5024 return cp_parser_direct_new_declarator (parser);
5029 /* Parse a direct-new-declarator.
5031 direct-new-declarator:
5033 direct-new-declarator [constant-expression]
5037 static cp_declarator *
5038 cp_parser_direct_new_declarator (cp_parser* parser)
5040 cp_declarator *declarator = NULL;
5046 /* Look for the opening `['. */
5047 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5048 /* The first expression is not required to be constant. */
5051 expression = cp_parser_expression (parser);
5052 /* The standard requires that the expression have integral
5053 type. DR 74 adds enumeration types. We believe that the
5054 real intent is that these expressions be handled like the
5055 expression in a `switch' condition, which also allows
5056 classes with a single conversion to integral or
5057 enumeration type. */
5058 if (!processing_template_decl)
5061 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5066 error ("expression in new-declarator must have integral or enumeration type");
5067 expression = error_mark_node;
5071 /* But all the other expressions must be. */
5074 = cp_parser_constant_expression (parser,
5075 /*allow_non_constant=*/false,
5077 /* Look for the closing `]'. */
5078 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5080 /* Add this bound to the declarator. */
5081 declarator = make_array_declarator (declarator, expression);
5083 /* If the next token is not a `[', then there are no more
5085 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5092 /* Parse a new-initializer.
5095 ( expression-list [opt] )
5097 Returns a representation of the expression-list. If there is no
5098 expression-list, VOID_ZERO_NODE is returned. */
5101 cp_parser_new_initializer (cp_parser* parser)
5103 tree expression_list;
5105 expression_list = (cp_parser_parenthesized_expression_list
5106 (parser, false, /*non_constant_p=*/NULL));
5107 if (!expression_list)
5108 expression_list = void_zero_node;
5110 return expression_list;
5113 /* Parse a delete-expression.
5116 :: [opt] delete cast-expression
5117 :: [opt] delete [ ] cast-expression
5119 Returns a representation of the expression. */
5122 cp_parser_delete_expression (cp_parser* parser)
5124 bool global_scope_p;
5128 /* Look for the optional `::' operator. */
5130 = (cp_parser_global_scope_opt (parser,
5131 /*current_scope_valid_p=*/false)
5133 /* Look for the `delete' keyword. */
5134 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5135 /* See if the array syntax is in use. */
5136 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5138 /* Consume the `[' token. */
5139 cp_lexer_consume_token (parser->lexer);
5140 /* Look for the `]' token. */
5141 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5142 /* Remember that this is the `[]' construct. */
5148 /* Parse the cast-expression. */
5149 expression = cp_parser_simple_cast_expression (parser);
5151 /* A delete-expression may not appear in an integral constant
5153 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5154 return error_mark_node;
5156 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5159 /* Parse a cast-expression.
5163 ( type-id ) cast-expression
5165 Returns a representation of the expression. */
5168 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5170 /* If it's a `(', then we might be looking at a cast. */
5171 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5173 tree type = NULL_TREE;
5174 tree expr = NULL_TREE;
5175 bool compound_literal_p;
5176 const char *saved_message;
5178 /* There's no way to know yet whether or not this is a cast.
5179 For example, `(int (3))' is a unary-expression, while `(int)
5180 3' is a cast. So, we resort to parsing tentatively. */
5181 cp_parser_parse_tentatively (parser);
5182 /* Types may not be defined in a cast. */
5183 saved_message = parser->type_definition_forbidden_message;
5184 parser->type_definition_forbidden_message
5185 = "types may not be defined in casts";
5186 /* Consume the `('. */
5187 cp_lexer_consume_token (parser->lexer);
5188 /* A very tricky bit is that `(struct S) { 3 }' is a
5189 compound-literal (which we permit in C++ as an extension).
5190 But, that construct is not a cast-expression -- it is a
5191 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5192 is legal; if the compound-literal were a cast-expression,
5193 you'd need an extra set of parentheses.) But, if we parse
5194 the type-id, and it happens to be a class-specifier, then we
5195 will commit to the parse at that point, because we cannot
5196 undo the action that is done when creating a new class. So,
5197 then we cannot back up and do a postfix-expression.
5199 Therefore, we scan ahead to the closing `)', and check to see
5200 if the token after the `)' is a `{'. If so, we are not
5201 looking at a cast-expression.
5203 Save tokens so that we can put them back. */
5204 cp_lexer_save_tokens (parser->lexer);
5205 /* Skip tokens until the next token is a closing parenthesis.
5206 If we find the closing `)', and the next token is a `{', then
5207 we are looking at a compound-literal. */
5209 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5210 /*consume_paren=*/true)
5211 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5212 /* Roll back the tokens we skipped. */
5213 cp_lexer_rollback_tokens (parser->lexer);
5214 /* If we were looking at a compound-literal, simulate an error
5215 so that the call to cp_parser_parse_definitely below will
5217 if (compound_literal_p)
5218 cp_parser_simulate_error (parser);
5221 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5222 parser->in_type_id_in_expr_p = true;
5223 /* Look for the type-id. */
5224 type = cp_parser_type_id (parser);
5225 /* Look for the closing `)'. */
5226 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5227 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5230 /* Restore the saved message. */
5231 parser->type_definition_forbidden_message = saved_message;
5233 /* If ok so far, parse the dependent expression. We cannot be
5234 sure it is a cast. Consider `(T ())'. It is a parenthesized
5235 ctor of T, but looks like a cast to function returning T
5236 without a dependent expression. */
5237 if (!cp_parser_error_occurred (parser))
5238 expr = cp_parser_simple_cast_expression (parser);
5240 if (cp_parser_parse_definitely (parser))
5242 /* Warn about old-style casts, if so requested. */
5243 if (warn_old_style_cast
5244 && !in_system_header
5245 && !VOID_TYPE_P (type)
5246 && current_lang_name != lang_name_c)
5247 warning ("use of old-style cast");
5249 /* Only type conversions to integral or enumeration types
5250 can be used in constant-expressions. */
5251 if (parser->integral_constant_expression_p
5252 && !dependent_type_p (type)
5253 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5254 && (cp_parser_non_integral_constant_expression
5256 "a cast to a type other than an integral or "
5257 "enumeration type")))
5258 return error_mark_node;
5260 /* Perform the cast. */
5261 expr = build_c_cast (type, expr);
5266 /* If we get here, then it's not a cast, so it must be a
5267 unary-expression. */
5268 return cp_parser_unary_expression (parser, address_p);
5271 /* Parse a pm-expression.
5275 pm-expression .* cast-expression
5276 pm-expression ->* cast-expression
5278 Returns a representation of the expression. */
5281 cp_parser_pm_expression (cp_parser* parser)
5283 static const cp_parser_token_tree_map map = {
5284 { CPP_DEREF_STAR, MEMBER_REF },
5285 { CPP_DOT_STAR, DOTSTAR_EXPR },
5286 { CPP_EOF, ERROR_MARK }
5289 return cp_parser_binary_expression (parser, map,
5290 cp_parser_simple_cast_expression);
5293 /* Parse a multiplicative-expression.
5295 multiplicative-expression:
5297 multiplicative-expression * pm-expression
5298 multiplicative-expression / pm-expression
5299 multiplicative-expression % pm-expression
5301 Returns a representation of the expression. */
5304 cp_parser_multiplicative_expression (cp_parser* parser)
5306 static const cp_parser_token_tree_map map = {
5307 { CPP_MULT, MULT_EXPR },
5308 { CPP_DIV, TRUNC_DIV_EXPR },
5309 { CPP_MOD, TRUNC_MOD_EXPR },
5310 { CPP_EOF, ERROR_MARK }
5313 return cp_parser_binary_expression (parser,
5315 cp_parser_pm_expression);
5318 /* Parse an additive-expression.
5320 additive-expression:
5321 multiplicative-expression
5322 additive-expression + multiplicative-expression
5323 additive-expression - multiplicative-expression
5325 Returns a representation of the expression. */
5328 cp_parser_additive_expression (cp_parser* parser)
5330 static const cp_parser_token_tree_map map = {
5331 { CPP_PLUS, PLUS_EXPR },
5332 { CPP_MINUS, MINUS_EXPR },
5333 { CPP_EOF, ERROR_MARK }
5336 return cp_parser_binary_expression (parser,
5338 cp_parser_multiplicative_expression);
5341 /* Parse a shift-expression.
5345 shift-expression << additive-expression
5346 shift-expression >> additive-expression
5348 Returns a representation of the expression. */
5351 cp_parser_shift_expression (cp_parser* parser)
5353 static const cp_parser_token_tree_map map = {
5354 { CPP_LSHIFT, LSHIFT_EXPR },
5355 { CPP_RSHIFT, RSHIFT_EXPR },
5356 { CPP_EOF, ERROR_MARK }
5359 return cp_parser_binary_expression (parser,
5361 cp_parser_additive_expression);
5364 /* Parse a relational-expression.
5366 relational-expression:
5368 relational-expression < shift-expression
5369 relational-expression > shift-expression
5370 relational-expression <= shift-expression
5371 relational-expression >= shift-expression
5375 relational-expression:
5376 relational-expression <? shift-expression
5377 relational-expression >? shift-expression
5379 Returns a representation of the expression. */
5382 cp_parser_relational_expression (cp_parser* parser)
5384 static const cp_parser_token_tree_map map = {
5385 { CPP_LESS, LT_EXPR },
5386 { CPP_GREATER, GT_EXPR },
5387 { CPP_LESS_EQ, LE_EXPR },
5388 { CPP_GREATER_EQ, GE_EXPR },
5389 { CPP_MIN, MIN_EXPR },
5390 { CPP_MAX, MAX_EXPR },
5391 { CPP_EOF, ERROR_MARK }
5394 return cp_parser_binary_expression (parser,
5396 cp_parser_shift_expression);
5399 /* Parse an equality-expression.
5401 equality-expression:
5402 relational-expression
5403 equality-expression == relational-expression
5404 equality-expression != relational-expression
5406 Returns a representation of the expression. */
5409 cp_parser_equality_expression (cp_parser* parser)
5411 static const cp_parser_token_tree_map map = {
5412 { CPP_EQ_EQ, EQ_EXPR },
5413 { CPP_NOT_EQ, NE_EXPR },
5414 { CPP_EOF, ERROR_MARK }
5417 return cp_parser_binary_expression (parser,
5419 cp_parser_relational_expression);
5422 /* Parse an and-expression.
5426 and-expression & equality-expression
5428 Returns a representation of the expression. */
5431 cp_parser_and_expression (cp_parser* parser)
5433 static const cp_parser_token_tree_map map = {
5434 { CPP_AND, BIT_AND_EXPR },
5435 { CPP_EOF, ERROR_MARK }
5438 return cp_parser_binary_expression (parser,
5440 cp_parser_equality_expression);
5443 /* Parse an exclusive-or-expression.
5445 exclusive-or-expression:
5447 exclusive-or-expression ^ and-expression
5449 Returns a representation of the expression. */
5452 cp_parser_exclusive_or_expression (cp_parser* parser)
5454 static const cp_parser_token_tree_map map = {
5455 { CPP_XOR, BIT_XOR_EXPR },
5456 { CPP_EOF, ERROR_MARK }
5459 return cp_parser_binary_expression (parser,
5461 cp_parser_and_expression);
5465 /* Parse an inclusive-or-expression.
5467 inclusive-or-expression:
5468 exclusive-or-expression
5469 inclusive-or-expression | exclusive-or-expression
5471 Returns a representation of the expression. */
5474 cp_parser_inclusive_or_expression (cp_parser* parser)
5476 static const cp_parser_token_tree_map map = {
5477 { CPP_OR, BIT_IOR_EXPR },
5478 { CPP_EOF, ERROR_MARK }
5481 return cp_parser_binary_expression (parser,
5483 cp_parser_exclusive_or_expression);
5486 /* Parse a logical-and-expression.
5488 logical-and-expression:
5489 inclusive-or-expression
5490 logical-and-expression && inclusive-or-expression
5492 Returns a representation of the expression. */
5495 cp_parser_logical_and_expression (cp_parser* parser)
5497 static const cp_parser_token_tree_map map = {
5498 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5499 { CPP_EOF, ERROR_MARK }
5502 return cp_parser_binary_expression (parser,
5504 cp_parser_inclusive_or_expression);
5507 /* Parse a logical-or-expression.
5509 logical-or-expression:
5510 logical-and-expression
5511 logical-or-expression || logical-and-expression
5513 Returns a representation of the expression. */
5516 cp_parser_logical_or_expression (cp_parser* parser)
5518 static const cp_parser_token_tree_map map = {
5519 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5520 { CPP_EOF, ERROR_MARK }
5523 return cp_parser_binary_expression (parser,
5525 cp_parser_logical_and_expression);
5528 /* Parse the `? expression : assignment-expression' part of a
5529 conditional-expression. The LOGICAL_OR_EXPR is the
5530 logical-or-expression that started the conditional-expression.
5531 Returns a representation of the entire conditional-expression.
5533 This routine is used by cp_parser_assignment_expression.
5535 ? expression : assignment-expression
5539 ? : assignment-expression */
5542 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5545 tree assignment_expr;
5547 /* Consume the `?' token. */
5548 cp_lexer_consume_token (parser->lexer);
5549 if (cp_parser_allow_gnu_extensions_p (parser)
5550 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5551 /* Implicit true clause. */
5554 /* Parse the expression. */
5555 expr = cp_parser_expression (parser);
5557 /* The next token should be a `:'. */
5558 cp_parser_require (parser, CPP_COLON, "`:'");
5559 /* Parse the assignment-expression. */
5560 assignment_expr = cp_parser_assignment_expression (parser);
5562 /* Build the conditional-expression. */
5563 return build_x_conditional_expr (logical_or_expr,
5568 /* Parse an assignment-expression.
5570 assignment-expression:
5571 conditional-expression
5572 logical-or-expression assignment-operator assignment_expression
5575 Returns a representation for the expression. */
5578 cp_parser_assignment_expression (cp_parser* parser)
5582 /* If the next token is the `throw' keyword, then we're looking at
5583 a throw-expression. */
5584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5585 expr = cp_parser_throw_expression (parser);
5586 /* Otherwise, it must be that we are looking at a
5587 logical-or-expression. */
5590 /* Parse the logical-or-expression. */
5591 expr = cp_parser_logical_or_expression (parser);
5592 /* If the next token is a `?' then we're actually looking at a
5593 conditional-expression. */
5594 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5595 return cp_parser_question_colon_clause (parser, expr);
5598 enum tree_code assignment_operator;
5600 /* If it's an assignment-operator, we're using the second
5603 = cp_parser_assignment_operator_opt (parser);
5604 if (assignment_operator != ERROR_MARK)
5608 /* Parse the right-hand side of the assignment. */
5609 rhs = cp_parser_assignment_expression (parser);
5610 /* An assignment may not appear in a
5611 constant-expression. */
5612 if (cp_parser_non_integral_constant_expression (parser,
5614 return error_mark_node;
5615 /* Build the assignment expression. */
5616 expr = build_x_modify_expr (expr,
5617 assignment_operator,
5626 /* Parse an (optional) assignment-operator.
5628 assignment-operator: one of
5629 = *= /= %= += -= >>= <<= &= ^= |=
5633 assignment-operator: one of
5636 If the next token is an assignment operator, the corresponding tree
5637 code is returned, and the token is consumed. For example, for
5638 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5639 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5640 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5641 operator, ERROR_MARK is returned. */
5643 static enum tree_code
5644 cp_parser_assignment_operator_opt (cp_parser* parser)
5649 /* Peek at the next toen. */
5650 token = cp_lexer_peek_token (parser->lexer);
5652 switch (token->type)
5663 op = TRUNC_DIV_EXPR;
5667 op = TRUNC_MOD_EXPR;
5707 /* Nothing else is an assignment operator. */
5711 /* If it was an assignment operator, consume it. */
5712 if (op != ERROR_MARK)
5713 cp_lexer_consume_token (parser->lexer);
5718 /* Parse an expression.
5721 assignment-expression
5722 expression , assignment-expression
5724 Returns a representation of the expression. */
5727 cp_parser_expression (cp_parser* parser)
5729 tree expression = NULL_TREE;
5733 tree assignment_expression;
5735 /* Parse the next assignment-expression. */
5736 assignment_expression
5737 = cp_parser_assignment_expression (parser);
5738 /* If this is the first assignment-expression, we can just
5741 expression = assignment_expression;
5743 expression = build_x_compound_expr (expression,
5744 assignment_expression);
5745 /* If the next token is not a comma, then we are done with the
5747 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5749 /* Consume the `,'. */
5750 cp_lexer_consume_token (parser->lexer);
5751 /* A comma operator cannot appear in a constant-expression. */
5752 if (cp_parser_non_integral_constant_expression (parser,
5753 "a comma operator"))
5754 expression = error_mark_node;
5760 /* Parse a constant-expression.
5762 constant-expression:
5763 conditional-expression
5765 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5766 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5767 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5768 is false, NON_CONSTANT_P should be NULL. */
5771 cp_parser_constant_expression (cp_parser* parser,
5772 bool allow_non_constant_p,
5773 bool *non_constant_p)
5775 bool saved_integral_constant_expression_p;
5776 bool saved_allow_non_integral_constant_expression_p;
5777 bool saved_non_integral_constant_expression_p;
5780 /* It might seem that we could simply parse the
5781 conditional-expression, and then check to see if it were
5782 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5783 one that the compiler can figure out is constant, possibly after
5784 doing some simplifications or optimizations. The standard has a
5785 precise definition of constant-expression, and we must honor
5786 that, even though it is somewhat more restrictive.
5792 is not a legal declaration, because `(2, 3)' is not a
5793 constant-expression. The `,' operator is forbidden in a
5794 constant-expression. However, GCC's constant-folding machinery
5795 will fold this operation to an INTEGER_CST for `3'. */
5797 /* Save the old settings. */
5798 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5799 saved_allow_non_integral_constant_expression_p
5800 = parser->allow_non_integral_constant_expression_p;
5801 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5802 /* We are now parsing a constant-expression. */
5803 parser->integral_constant_expression_p = true;
5804 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5805 parser->non_integral_constant_expression_p = false;
5806 /* Although the grammar says "conditional-expression", we parse an
5807 "assignment-expression", which also permits "throw-expression"
5808 and the use of assignment operators. In the case that
5809 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5810 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5811 actually essential that we look for an assignment-expression.
5812 For example, cp_parser_initializer_clauses uses this function to
5813 determine whether a particular assignment-expression is in fact
5815 expression = cp_parser_assignment_expression (parser);
5816 /* Restore the old settings. */
5817 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5818 parser->allow_non_integral_constant_expression_p
5819 = saved_allow_non_integral_constant_expression_p;
5820 if (allow_non_constant_p)
5821 *non_constant_p = parser->non_integral_constant_expression_p;
5822 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5827 /* Parse __builtin_offsetof.
5829 offsetof-expression:
5830 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5832 offsetof-member-designator:
5834 | offsetof-member-designator "." id-expression
5835 | offsetof-member-designator "[" expression "]"
5839 cp_parser_builtin_offsetof (cp_parser *parser)
5841 int save_ice_p, save_non_ice_p;
5845 /* We're about to accept non-integral-constant things, but will
5846 definitely yield an integral constant expression. Save and
5847 restore these values around our local parsing. */
5848 save_ice_p = parser->integral_constant_expression_p;
5849 save_non_ice_p = parser->non_integral_constant_expression_p;
5851 /* Consume the "__builtin_offsetof" token. */
5852 cp_lexer_consume_token (parser->lexer);
5853 /* Consume the opening `('. */
5854 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5855 /* Parse the type-id. */
5856 type = cp_parser_type_id (parser);
5857 /* Look for the `,'. */
5858 cp_parser_require (parser, CPP_COMMA, "`,'");
5860 /* Build the (type *)null that begins the traditional offsetof macro. */
5861 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5863 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
5864 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5868 cp_token *token = cp_lexer_peek_token (parser->lexer);
5869 switch (token->type)
5871 case CPP_OPEN_SQUARE:
5872 /* offsetof-member-designator "[" expression "]" */
5873 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5877 /* offsetof-member-designator "." identifier */
5878 cp_lexer_consume_token (parser->lexer);
5879 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5883 case CPP_CLOSE_PAREN:
5884 /* Consume the ")" token. */
5885 cp_lexer_consume_token (parser->lexer);
5889 /* Error. We know the following require will fail, but
5890 that gives the proper error message. */
5891 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5892 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5893 expr = error_mark_node;
5899 /* If we're processing a template, we can't finish the semantics yet.
5900 Otherwise we can fold the entire expression now. */
5901 if (processing_template_decl)
5902 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5904 expr = fold_offsetof (expr);
5907 parser->integral_constant_expression_p = save_ice_p;
5908 parser->non_integral_constant_expression_p = save_non_ice_p;
5913 /* Statements [gram.stmt.stmt] */
5915 /* Parse a statement.
5919 expression-statement
5924 declaration-statement
5928 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5932 location_t statement_location;
5934 /* There is no statement yet. */
5935 statement = NULL_TREE;
5936 /* Peek at the next token. */
5937 token = cp_lexer_peek_token (parser->lexer);
5938 /* Remember the location of the first token in the statement. */
5939 statement_location = token->location;
5940 /* If this is a keyword, then that will often determine what kind of
5941 statement we have. */
5942 if (token->type == CPP_KEYWORD)
5944 enum rid keyword = token->keyword;
5950 statement = cp_parser_labeled_statement (parser,
5956 statement = cp_parser_selection_statement (parser);
5962 statement = cp_parser_iteration_statement (parser);
5969 statement = cp_parser_jump_statement (parser);
5973 statement = cp_parser_try_block (parser);
5977 /* It might be a keyword like `int' that can start a
5978 declaration-statement. */
5982 else if (token->type == CPP_NAME)
5984 /* If the next token is a `:', then we are looking at a
5985 labeled-statement. */
5986 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5987 if (token->type == CPP_COLON)
5988 statement = cp_parser_labeled_statement (parser, in_statement_expr);
5990 /* Anything that starts with a `{' must be a compound-statement. */
5991 else if (token->type == CPP_OPEN_BRACE)
5992 statement = cp_parser_compound_statement (parser, NULL, false);
5994 /* Everything else must be a declaration-statement or an
5995 expression-statement. Try for the declaration-statement
5996 first, unless we are looking at a `;', in which case we know that
5997 we have an expression-statement. */
6000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6002 cp_parser_parse_tentatively (parser);
6003 /* Try to parse the declaration-statement. */
6004 cp_parser_declaration_statement (parser);
6005 /* If that worked, we're done. */
6006 if (cp_parser_parse_definitely (parser))
6009 /* Look for an expression-statement instead. */
6010 statement = cp_parser_expression_statement (parser, in_statement_expr);
6013 /* Set the line number for the statement. */
6014 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6015 SET_EXPR_LOCATION (statement, statement_location);
6018 /* Parse a labeled-statement.
6021 identifier : statement
6022 case constant-expression : statement
6028 case constant-expression ... constant-expression : statement
6030 Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6031 For an ordinary label, returns a LABEL_EXPR. */
6034 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6037 tree statement = error_mark_node;
6039 /* The next token should be an identifier. */
6040 token = cp_lexer_peek_token (parser->lexer);
6041 if (token->type != CPP_NAME
6042 && token->type != CPP_KEYWORD)
6044 cp_parser_error (parser, "expected labeled-statement");
6045 return error_mark_node;
6048 switch (token->keyword)
6055 /* Consume the `case' token. */
6056 cp_lexer_consume_token (parser->lexer);
6057 /* Parse the constant-expression. */
6058 expr = cp_parser_constant_expression (parser,
6059 /*allow_non_constant_p=*/false,
6062 ellipsis = cp_lexer_peek_token (parser->lexer);
6063 if (ellipsis->type == CPP_ELLIPSIS)
6065 /* Consume the `...' token. */
6066 cp_lexer_consume_token (parser->lexer);
6068 cp_parser_constant_expression (parser,
6069 /*allow_non_constant_p=*/false,
6071 /* We don't need to emit warnings here, as the common code
6072 will do this for us. */
6075 expr_hi = NULL_TREE;
6077 if (!parser->in_switch_statement_p)
6078 error ("case label `%E' not within a switch statement", expr);
6080 statement = finish_case_label (expr, expr_hi);
6085 /* Consume the `default' token. */
6086 cp_lexer_consume_token (parser->lexer);
6087 if (!parser->in_switch_statement_p)
6088 error ("case label not within a switch statement");
6090 statement = finish_case_label (NULL_TREE, NULL_TREE);
6094 /* Anything else must be an ordinary label. */
6095 statement = finish_label_stmt (cp_parser_identifier (parser));
6099 /* Require the `:' token. */
6100 cp_parser_require (parser, CPP_COLON, "`:'");
6101 /* Parse the labeled statement. */
6102 cp_parser_statement (parser, in_statement_expr);
6104 /* Return the label, in the case of a `case' or `default' label. */
6108 /* Parse an expression-statement.
6110 expression-statement:
6113 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6114 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6115 indicates whether this expression-statement is part of an
6116 expression statement. */
6119 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6121 tree statement = NULL_TREE;
6123 /* If the next token is a ';', then there is no expression
6125 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6126 statement = cp_parser_expression (parser);
6128 /* Consume the final `;'. */
6129 cp_parser_consume_semicolon_at_end_of_statement (parser);
6131 if (in_statement_expr
6132 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6134 /* This is the final expression statement of a statement
6136 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6139 statement = finish_expr_stmt (statement);
6146 /* Parse a compound-statement.
6149 { statement-seq [opt] }
6151 Returns a tree representing the statement. */
6154 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6159 /* Consume the `{'. */
6160 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6161 return error_mark_node;
6162 /* Begin the compound-statement. */
6163 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6164 /* Parse an (optional) statement-seq. */
6165 cp_parser_statement_seq_opt (parser, in_statement_expr);
6166 /* Finish the compound-statement. */
6167 finish_compound_stmt (compound_stmt);
6168 /* Consume the `}'. */
6169 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6171 return compound_stmt;
6174 /* Parse an (optional) statement-seq.
6178 statement-seq [opt] statement */
6181 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6183 /* Scan statements until there aren't any more. */
6186 /* If we're looking at a `}', then we've run out of statements. */
6187 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6188 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6191 /* Parse the statement. */
6192 cp_parser_statement (parser, in_statement_expr);
6196 /* Parse a selection-statement.
6198 selection-statement:
6199 if ( condition ) statement
6200 if ( condition ) statement else statement
6201 switch ( condition ) statement
6203 Returns the new IF_STMT or SWITCH_STMT. */
6206 cp_parser_selection_statement (cp_parser* parser)
6211 /* Peek at the next token. */
6212 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6214 /* See what kind of keyword it is. */
6215 keyword = token->keyword;
6224 /* Look for the `('. */
6225 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6227 cp_parser_skip_to_end_of_statement (parser);
6228 return error_mark_node;
6231 /* Begin the selection-statement. */
6232 if (keyword == RID_IF)
6233 statement = begin_if_stmt ();
6235 statement = begin_switch_stmt ();
6237 /* Parse the condition. */
6238 condition = cp_parser_condition (parser);
6239 /* Look for the `)'. */
6240 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6241 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6242 /*consume_paren=*/true);
6244 if (keyword == RID_IF)
6246 /* Add the condition. */
6247 finish_if_stmt_cond (condition, statement);
6249 /* Parse the then-clause. */
6250 cp_parser_implicitly_scoped_statement (parser);
6251 finish_then_clause (statement);
6253 /* If the next token is `else', parse the else-clause. */
6254 if (cp_lexer_next_token_is_keyword (parser->lexer,
6257 /* Consume the `else' keyword. */
6258 cp_lexer_consume_token (parser->lexer);
6259 begin_else_clause (statement);
6260 /* Parse the else-clause. */
6261 cp_parser_implicitly_scoped_statement (parser);
6262 finish_else_clause (statement);
6265 /* Now we're all done with the if-statement. */
6266 finish_if_stmt (statement);
6270 bool in_switch_statement_p;
6272 /* Add the condition. */
6273 finish_switch_cond (condition, statement);
6275 /* Parse the body of the switch-statement. */
6276 in_switch_statement_p = parser->in_switch_statement_p;
6277 parser->in_switch_statement_p = true;
6278 cp_parser_implicitly_scoped_statement (parser);
6279 parser->in_switch_statement_p = in_switch_statement_p;
6281 /* Now we're all done with the switch-statement. */
6282 finish_switch_stmt (statement);
6290 cp_parser_error (parser, "expected selection-statement");
6291 return error_mark_node;
6295 /* Parse a condition.
6299 type-specifier-seq declarator = assignment-expression
6304 type-specifier-seq declarator asm-specification [opt]
6305 attributes [opt] = assignment-expression
6307 Returns the expression that should be tested. */
6310 cp_parser_condition (cp_parser* parser)
6312 cp_decl_specifier_seq type_specifiers;
6313 const char *saved_message;
6315 /* Try the declaration first. */
6316 cp_parser_parse_tentatively (parser);
6317 /* New types are not allowed in the type-specifier-seq for a
6319 saved_message = parser->type_definition_forbidden_message;
6320 parser->type_definition_forbidden_message
6321 = "types may not be defined in conditions";
6322 /* Parse the type-specifier-seq. */
6323 cp_parser_type_specifier_seq (parser, &type_specifiers);
6324 /* Restore the saved message. */
6325 parser->type_definition_forbidden_message = saved_message;
6326 /* If all is well, we might be looking at a declaration. */
6327 if (!cp_parser_error_occurred (parser))
6330 tree asm_specification;
6332 cp_declarator *declarator;
6333 tree initializer = NULL_TREE;
6335 /* Parse the declarator. */
6336 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6337 /*ctor_dtor_or_conv_p=*/NULL,
6338 /*parenthesized_p=*/NULL);
6339 /* Parse the attributes. */
6340 attributes = cp_parser_attributes_opt (parser);
6341 /* Parse the asm-specification. */
6342 asm_specification = cp_parser_asm_specification_opt (parser);
6343 /* If the next token is not an `=', then we might still be
6344 looking at an expression. For example:
6348 looks like a decl-specifier-seq and a declarator -- but then
6349 there is no `=', so this is an expression. */
6350 cp_parser_require (parser, CPP_EQ, "`='");
6351 /* If we did see an `=', then we are looking at a declaration
6353 if (cp_parser_parse_definitely (parser))
6357 /* Create the declaration. */
6358 decl = start_decl (declarator, &type_specifiers,
6359 /*initialized_p=*/true,
6360 attributes, /*prefix_attributes=*/NULL_TREE,
6362 /* Parse the assignment-expression. */
6363 initializer = cp_parser_assignment_expression (parser);
6365 /* Process the initializer. */
6366 cp_finish_decl (decl,
6369 LOOKUP_ONLYCONVERTING);
6371 pop_scope (DECL_CONTEXT (decl));
6373 return convert_from_reference (decl);
6376 /* If we didn't even get past the declarator successfully, we are
6377 definitely not looking at a declaration. */
6379 cp_parser_abort_tentative_parse (parser);
6381 /* Otherwise, we are looking at an expression. */
6382 return cp_parser_expression (parser);
6385 /* Parse an iteration-statement.
6387 iteration-statement:
6388 while ( condition ) statement
6389 do statement while ( expression ) ;
6390 for ( for-init-statement condition [opt] ; expression [opt] )
6393 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6396 cp_parser_iteration_statement (cp_parser* parser)
6401 bool in_iteration_statement_p;
6404 /* Peek at the next token. */
6405 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6407 return error_mark_node;
6409 /* Remember whether or not we are already within an iteration
6411 in_iteration_statement_p = parser->in_iteration_statement_p;
6413 /* See what kind of keyword it is. */
6414 keyword = token->keyword;
6421 /* Begin the while-statement. */
6422 statement = begin_while_stmt ();
6423 /* Look for the `('. */
6424 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6425 /* Parse the condition. */
6426 condition = cp_parser_condition (parser);
6427 finish_while_stmt_cond (condition, statement);
6428 /* Look for the `)'. */
6429 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6430 /* Parse the dependent statement. */
6431 parser->in_iteration_statement_p = true;
6432 cp_parser_already_scoped_statement (parser);
6433 parser->in_iteration_statement_p = in_iteration_statement_p;
6434 /* We're done with the while-statement. */
6435 finish_while_stmt (statement);
6443 /* Begin the do-statement. */
6444 statement = begin_do_stmt ();
6445 /* Parse the body of the do-statement. */
6446 parser->in_iteration_statement_p = true;
6447 cp_parser_implicitly_scoped_statement (parser);
6448 parser->in_iteration_statement_p = in_iteration_statement_p;
6449 finish_do_body (statement);
6450 /* Look for the `while' keyword. */
6451 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6452 /* Look for the `('. */
6453 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6454 /* Parse the expression. */
6455 expression = cp_parser_expression (parser);
6456 /* We're done with the do-statement. */
6457 finish_do_stmt (expression, statement);
6458 /* Look for the `)'. */
6459 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6460 /* Look for the `;'. */
6461 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6467 tree condition = NULL_TREE;
6468 tree expression = NULL_TREE;
6470 /* Begin the for-statement. */
6471 statement = begin_for_stmt ();
6472 /* Look for the `('. */
6473 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6474 /* Parse the initialization. */
6475 cp_parser_for_init_statement (parser);
6476 finish_for_init_stmt (statement);
6478 /* If there's a condition, process it. */
6479 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6480 condition = cp_parser_condition (parser);
6481 finish_for_cond (condition, statement);
6482 /* Look for the `;'. */
6483 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6485 /* If there's an expression, process it. */
6486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6487 expression = cp_parser_expression (parser);
6488 finish_for_expr (expression, statement);
6489 /* Look for the `)'. */
6490 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6492 /* Parse the body of the for-statement. */
6493 parser->in_iteration_statement_p = true;
6494 cp_parser_already_scoped_statement (parser);
6495 parser->in_iteration_statement_p = in_iteration_statement_p;
6497 /* We're done with the for-statement. */
6498 finish_for_stmt (statement);
6503 cp_parser_error (parser, "expected iteration-statement");
6504 statement = error_mark_node;
6511 /* Parse a for-init-statement.
6514 expression-statement
6515 simple-declaration */
6518 cp_parser_for_init_statement (cp_parser* parser)
6520 /* If the next token is a `;', then we have an empty
6521 expression-statement. Grammatically, this is also a
6522 simple-declaration, but an invalid one, because it does not
6523 declare anything. Therefore, if we did not handle this case
6524 specially, we would issue an error message about an invalid
6526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6528 /* We're going to speculatively look for a declaration, falling back
6529 to an expression, if necessary. */
6530 cp_parser_parse_tentatively (parser);
6531 /* Parse the declaration. */
6532 cp_parser_simple_declaration (parser,
6533 /*function_definition_allowed_p=*/false);
6534 /* If the tentative parse failed, then we shall need to look for an
6535 expression-statement. */
6536 if (cp_parser_parse_definitely (parser))
6540 cp_parser_expression_statement (parser, false);
6543 /* Parse a jump-statement.
6548 return expression [opt] ;
6556 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
6559 cp_parser_jump_statement (cp_parser* parser)
6561 tree statement = error_mark_node;
6565 /* Peek at the next token. */
6566 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6568 return error_mark_node;
6570 /* See what kind of keyword it is. */
6571 keyword = token->keyword;
6575 if (!parser->in_switch_statement_p
6576 && !parser->in_iteration_statement_p)
6578 error ("break statement not within loop or switch");
6579 statement = error_mark_node;
6582 statement = finish_break_stmt ();
6583 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6587 if (!parser->in_iteration_statement_p)
6589 error ("continue statement not within a loop");
6590 statement = error_mark_node;
6593 statement = finish_continue_stmt ();
6594 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6601 /* If the next token is a `;', then there is no
6603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6604 expr = cp_parser_expression (parser);
6607 /* Build the return-statement. */
6608 statement = finish_return_stmt (expr);
6609 /* Look for the final `;'. */
6610 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6615 /* Create the goto-statement. */
6616 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6618 /* Issue a warning about this use of a GNU extension. */
6620 pedwarn ("ISO C++ forbids computed gotos");
6621 /* Consume the '*' token. */
6622 cp_lexer_consume_token (parser->lexer);
6623 /* Parse the dependent expression. */
6624 finish_goto_stmt (cp_parser_expression (parser));
6627 finish_goto_stmt (cp_parser_identifier (parser));
6628 /* Look for the final `;'. */
6629 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6633 cp_parser_error (parser, "expected jump-statement");
6640 /* Parse a declaration-statement.
6642 declaration-statement:
6643 block-declaration */
6646 cp_parser_declaration_statement (cp_parser* parser)
6650 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6651 p = obstack_alloc (&declarator_obstack, 0);
6653 /* Parse the block-declaration. */
6654 cp_parser_block_declaration (parser, /*statement_p=*/true);
6656 /* Free any declarators allocated. */
6657 obstack_free (&declarator_obstack, p);
6659 /* Finish off the statement. */
6663 /* Some dependent statements (like `if (cond) statement'), are
6664 implicitly in their own scope. In other words, if the statement is
6665 a single statement (as opposed to a compound-statement), it is
6666 none-the-less treated as if it were enclosed in braces. Any
6667 declarations appearing in the dependent statement are out of scope
6668 after control passes that point. This function parses a statement,
6669 but ensures that is in its own scope, even if it is not a
6672 Returns the new statement. */
6675 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6679 /* If the token is not a `{', then we must take special action. */
6680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6682 /* Create a compound-statement. */
6683 statement = begin_compound_stmt (0);
6684 /* Parse the dependent-statement. */
6685 cp_parser_statement (parser, false);
6686 /* Finish the dummy compound-statement. */
6687 finish_compound_stmt (statement);
6689 /* Otherwise, we simply parse the statement directly. */
6691 statement = cp_parser_compound_statement (parser, NULL, false);
6693 /* Return the statement. */
6697 /* For some dependent statements (like `while (cond) statement'), we
6698 have already created a scope. Therefore, even if the dependent
6699 statement is a compound-statement, we do not want to create another
6703 cp_parser_already_scoped_statement (cp_parser* parser)
6705 /* If the token is a `{', then we must take special action. */
6706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6707 cp_parser_statement (parser, false);
6710 /* Avoid calling cp_parser_compound_statement, so that we
6711 don't create a new scope. Do everything else by hand. */
6712 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6713 cp_parser_statement_seq_opt (parser, false);
6714 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6718 /* Declarations [gram.dcl.dcl] */
6720 /* Parse an optional declaration-sequence.
6724 declaration-seq declaration */
6727 cp_parser_declaration_seq_opt (cp_parser* parser)
6733 token = cp_lexer_peek_token (parser->lexer);
6735 if (token->type == CPP_CLOSE_BRACE
6736 || token->type == CPP_EOF)
6739 if (token->type == CPP_SEMICOLON)
6741 /* A declaration consisting of a single semicolon is
6742 invalid. Allow it unless we're being pedantic. */
6743 if (pedantic && !in_system_header)
6744 pedwarn ("extra `;'");
6745 cp_lexer_consume_token (parser->lexer);
6749 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6750 parser to enter or exit implicit `extern "C"' blocks. */
6751 while (pending_lang_change > 0)
6753 push_lang_context (lang_name_c);
6754 --pending_lang_change;
6756 while (pending_lang_change < 0)
6758 pop_lang_context ();
6759 ++pending_lang_change;
6762 /* Parse the declaration itself. */
6763 cp_parser_declaration (parser);
6767 /* Parse a declaration.
6772 template-declaration
6773 explicit-instantiation
6774 explicit-specialization
6775 linkage-specification
6776 namespace-definition
6781 __extension__ declaration */
6784 cp_parser_declaration (cp_parser* parser)
6791 /* Set this here since we can be called after
6792 pushing the linkage specification. */
6793 c_lex_string_translate = 1;
6795 /* Check for the `__extension__' keyword. */
6796 if (cp_parser_extension_opt (parser, &saved_pedantic))
6798 /* Parse the qualified declaration. */
6799 cp_parser_declaration (parser);
6800 /* Restore the PEDANTIC flag. */
6801 pedantic = saved_pedantic;
6806 /* Try to figure out what kind of declaration is present. */
6807 token1 = *cp_lexer_peek_token (parser->lexer);
6809 /* Don't translate the CPP_STRING in extern "C". */
6810 if (token1.keyword == RID_EXTERN)
6811 c_lex_string_translate = 0;
6813 if (token1.type != CPP_EOF)
6814 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6816 c_lex_string_translate = 1;
6818 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
6819 p = obstack_alloc (&declarator_obstack, 0);
6821 /* If the next token is `extern' and the following token is a string
6822 literal, then we have a linkage specification. */
6823 if (token1.keyword == RID_EXTERN
6824 && cp_parser_is_string_literal (&token2))
6825 cp_parser_linkage_specification (parser);
6826 /* If the next token is `template', then we have either a template
6827 declaration, an explicit instantiation, or an explicit
6829 else if (token1.keyword == RID_TEMPLATE)
6831 /* `template <>' indicates a template specialization. */
6832 if (token2.type == CPP_LESS
6833 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6834 cp_parser_explicit_specialization (parser);
6835 /* `template <' indicates a template declaration. */
6836 else if (token2.type == CPP_LESS)
6837 cp_parser_template_declaration (parser, /*member_p=*/false);
6838 /* Anything else must be an explicit instantiation. */
6840 cp_parser_explicit_instantiation (parser);
6842 /* If the next token is `export', then we have a template
6844 else if (token1.keyword == RID_EXPORT)
6845 cp_parser_template_declaration (parser, /*member_p=*/false);
6846 /* If the next token is `extern', 'static' or 'inline' and the one
6847 after that is `template', we have a GNU extended explicit
6848 instantiation directive. */
6849 else if (cp_parser_allow_gnu_extensions_p (parser)
6850 && (token1.keyword == RID_EXTERN
6851 || token1.keyword == RID_STATIC
6852 || token1.keyword == RID_INLINE)
6853 && token2.keyword == RID_TEMPLATE)
6854 cp_parser_explicit_instantiation (parser);
6855 /* If the next token is `namespace', check for a named or unnamed
6856 namespace definition. */
6857 else if (token1.keyword == RID_NAMESPACE
6858 && (/* A named namespace definition. */
6859 (token2.type == CPP_NAME
6860 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6862 /* An unnamed namespace definition. */
6863 || token2.type == CPP_OPEN_BRACE))
6864 cp_parser_namespace_definition (parser);
6865 /* We must have either a block declaration or a function
6868 /* Try to parse a block-declaration, or a function-definition. */
6869 cp_parser_block_declaration (parser, /*statement_p=*/false);
6871 /* Free any declarators allocated. */
6872 obstack_free (&declarator_obstack, p);
6875 /* Parse a block-declaration.
6880 namespace-alias-definition
6887 __extension__ block-declaration
6890 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6891 part of a declaration-statement. */
6894 cp_parser_block_declaration (cp_parser *parser,
6900 /* Check for the `__extension__' keyword. */
6901 if (cp_parser_extension_opt (parser, &saved_pedantic))
6903 /* Parse the qualified declaration. */
6904 cp_parser_block_declaration (parser, statement_p);
6905 /* Restore the PEDANTIC flag. */
6906 pedantic = saved_pedantic;
6911 /* Peek at the next token to figure out which kind of declaration is
6913 token1 = cp_lexer_peek_token (parser->lexer);
6915 /* If the next keyword is `asm', we have an asm-definition. */
6916 if (token1->keyword == RID_ASM)
6919 cp_parser_commit_to_tentative_parse (parser);
6920 cp_parser_asm_definition (parser);
6922 /* If the next keyword is `namespace', we have a
6923 namespace-alias-definition. */
6924 else if (token1->keyword == RID_NAMESPACE)
6925 cp_parser_namespace_alias_definition (parser);
6926 /* If the next keyword is `using', we have either a
6927 using-declaration or a using-directive. */
6928 else if (token1->keyword == RID_USING)
6933 cp_parser_commit_to_tentative_parse (parser);
6934 /* If the token after `using' is `namespace', then we have a
6936 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6937 if (token2->keyword == RID_NAMESPACE)
6938 cp_parser_using_directive (parser);
6939 /* Otherwise, it's a using-declaration. */
6941 cp_parser_using_declaration (parser);
6943 /* If the next keyword is `__label__' we have a label declaration. */
6944 else if (token1->keyword == RID_LABEL)
6947 cp_parser_commit_to_tentative_parse (parser);
6948 cp_parser_label_declaration (parser);
6950 /* Anything else must be a simple-declaration. */
6952 cp_parser_simple_declaration (parser, !statement_p);
6955 /* Parse a simple-declaration.
6958 decl-specifier-seq [opt] init-declarator-list [opt] ;
6960 init-declarator-list:
6962 init-declarator-list , init-declarator
6964 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6965 function-definition as a simple-declaration. */
6968 cp_parser_simple_declaration (cp_parser* parser,
6969 bool function_definition_allowed_p)
6971 cp_decl_specifier_seq decl_specifiers;
6972 int declares_class_or_enum;
6973 bool saw_declarator;
6975 /* Defer access checks until we know what is being declared; the
6976 checks for names appearing in the decl-specifier-seq should be
6977 done as if we were in the scope of the thing being declared. */
6978 push_deferring_access_checks (dk_deferred);
6980 /* Parse the decl-specifier-seq. We have to keep track of whether
6981 or not the decl-specifier-seq declares a named class or
6982 enumeration type, since that is the only case in which the
6983 init-declarator-list is allowed to be empty.
6987 In a simple-declaration, the optional init-declarator-list can be
6988 omitted only when declaring a class or enumeration, that is when
6989 the decl-specifier-seq contains either a class-specifier, an
6990 elaborated-type-specifier, or an enum-specifier. */
6991 cp_parser_decl_specifier_seq (parser,
6992 CP_PARSER_FLAGS_OPTIONAL,
6994 &declares_class_or_enum);
6995 /* We no longer need to defer access checks. */
6996 stop_deferring_access_checks ();
6998 /* In a block scope, a valid declaration must always have a
6999 decl-specifier-seq. By not trying to parse declarators, we can
7000 resolve the declaration/expression ambiguity more quickly. */
7001 if (!function_definition_allowed_p
7002 && !decl_specifiers.any_specifiers_p)
7004 cp_parser_error (parser, "expected declaration");
7008 /* If the next two tokens are both identifiers, the code is
7009 erroneous. The usual cause of this situation is code like:
7013 where "T" should name a type -- but does not. */
7014 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
7016 /* If parsing tentatively, we should commit; we really are
7017 looking at a declaration. */
7018 cp_parser_commit_to_tentative_parse (parser);
7023 /* Keep going until we hit the `;' at the end of the simple
7025 saw_declarator = false;
7026 while (cp_lexer_next_token_is_not (parser->lexer,
7030 bool function_definition_p;
7033 saw_declarator = true;
7034 /* Parse the init-declarator. */
7035 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7036 function_definition_allowed_p,
7038 declares_class_or_enum,
7039 &function_definition_p);
7040 /* If an error occurred while parsing tentatively, exit quickly.
7041 (That usually happens when in the body of a function; each
7042 statement is treated as a declaration-statement until proven
7044 if (cp_parser_error_occurred (parser))
7046 /* Handle function definitions specially. */
7047 if (function_definition_p)
7049 /* If the next token is a `,', then we are probably
7050 processing something like:
7054 which is erroneous. */
7055 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7056 error ("mixing declarations and function-definitions is forbidden");
7057 /* Otherwise, we're done with the list of declarators. */
7060 pop_deferring_access_checks ();
7064 /* The next token should be either a `,' or a `;'. */
7065 token = cp_lexer_peek_token (parser->lexer);
7066 /* If it's a `,', there are more declarators to come. */
7067 if (token->type == CPP_COMMA)
7068 cp_lexer_consume_token (parser->lexer);
7069 /* If it's a `;', we are done. */
7070 else if (token->type == CPP_SEMICOLON)
7072 /* Anything else is an error. */
7075 cp_parser_error (parser, "expected `,' or `;'");
7076 /* Skip tokens until we reach the end of the statement. */
7077 cp_parser_skip_to_end_of_statement (parser);
7078 /* If the next token is now a `;', consume it. */
7079 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7080 cp_lexer_consume_token (parser->lexer);
7083 /* After the first time around, a function-definition is not
7084 allowed -- even if it was OK at first. For example:
7089 function_definition_allowed_p = false;
7092 /* Issue an error message if no declarators are present, and the
7093 decl-specifier-seq does not itself declare a class or
7095 if (!saw_declarator)
7097 if (cp_parser_declares_only_class_p (parser))
7098 shadow_tag (&decl_specifiers);
7099 /* Perform any deferred access checks. */
7100 perform_deferred_access_checks ();
7103 /* Consume the `;'. */
7104 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7107 pop_deferring_access_checks ();
7110 /* Parse a decl-specifier-seq.
7113 decl-specifier-seq [opt] decl-specifier
7116 storage-class-specifier
7127 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7129 The parser flags FLAGS is used to control type-specifier parsing.
7131 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7134 1: one of the decl-specifiers is an elaborated-type-specifier
7135 (i.e., a type declaration)
7136 2: one of the decl-specifiers is an enum-specifier or a
7137 class-specifier (i.e., a type definition)
7142 cp_parser_decl_specifier_seq (cp_parser* parser,
7143 cp_parser_flags flags,
7144 cp_decl_specifier_seq *decl_specs,
7145 int* declares_class_or_enum)
7147 bool constructor_possible_p = !parser->in_declarator_p;
7149 /* Clear DECL_SPECS. */
7150 clear_decl_specs (decl_specs);
7152 /* Assume no class or enumeration type is declared. */
7153 *declares_class_or_enum = 0;
7155 /* Keep reading specifiers until there are no more to read. */
7159 bool found_decl_spec;
7162 /* Peek at the next token. */
7163 token = cp_lexer_peek_token (parser->lexer);
7164 /* Handle attributes. */
7165 if (token->keyword == RID_ATTRIBUTE)
7167 /* Parse the attributes. */
7168 decl_specs->attributes
7169 = chainon (decl_specs->attributes,
7170 cp_parser_attributes_opt (parser));
7173 /* Assume we will find a decl-specifier keyword. */
7174 found_decl_spec = true;
7175 /* If the next token is an appropriate keyword, we can simply
7176 add it to the list. */
7177 switch (token->keyword)
7182 if (decl_specs->specs[(int) ds_friend]++)
7183 error ("duplicate `friend'");
7184 /* Consume the token. */
7185 cp_lexer_consume_token (parser->lexer);
7188 /* function-specifier:
7195 cp_parser_function_specifier_opt (parser, decl_specs);
7201 ++decl_specs->specs[(int) ds_typedef];
7202 /* Consume the token. */
7203 cp_lexer_consume_token (parser->lexer);
7204 /* A constructor declarator cannot appear in a typedef. */
7205 constructor_possible_p = false;
7206 /* The "typedef" keyword can only occur in a declaration; we
7207 may as well commit at this point. */
7208 cp_parser_commit_to_tentative_parse (parser);
7211 /* storage-class-specifier:
7221 /* Consume the token. */
7222 cp_lexer_consume_token (parser->lexer);
7223 cp_parser_set_storage_class (decl_specs, sc_auto);
7226 /* Consume the token. */
7227 cp_lexer_consume_token (parser->lexer);
7228 cp_parser_set_storage_class (decl_specs, sc_register);
7231 /* Consume the token. */
7232 cp_lexer_consume_token (parser->lexer);
7233 if (decl_specs->specs[(int) ds_thread])
7235 error ("`__thread' before `static'");
7236 decl_specs->specs[(int) ds_thread] = 0;
7238 cp_parser_set_storage_class (decl_specs, sc_static);
7241 /* Consume the token. */
7242 cp_lexer_consume_token (parser->lexer);
7243 if (decl_specs->specs[(int) ds_thread])
7245 error ("`__thread' before `extern'");
7246 decl_specs->specs[(int) ds_thread] = 0;
7248 cp_parser_set_storage_class (decl_specs, sc_extern);
7251 /* Consume the token. */
7252 cp_lexer_consume_token (parser->lexer);
7253 cp_parser_set_storage_class (decl_specs, sc_mutable);
7256 /* Consume the token. */
7257 cp_lexer_consume_token (parser->lexer);
7258 ++decl_specs->specs[(int) ds_thread];
7262 /* We did not yet find a decl-specifier yet. */
7263 found_decl_spec = false;
7267 /* Constructors are a special case. The `S' in `S()' is not a
7268 decl-specifier; it is the beginning of the declarator. */
7271 && constructor_possible_p
7272 && (cp_parser_constructor_declarator_p
7273 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7275 /* If we don't have a DECL_SPEC yet, then we must be looking at
7276 a type-specifier. */
7277 if (!found_decl_spec && !constructor_p)
7279 int decl_spec_declares_class_or_enum;
7280 bool is_cv_qualifier;
7284 = cp_parser_type_specifier (parser, flags,
7286 /*is_declaration=*/true,
7287 &decl_spec_declares_class_or_enum,
7290 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7292 /* If this type-specifier referenced a user-defined type
7293 (a typedef, class-name, etc.), then we can't allow any
7294 more such type-specifiers henceforth.
7298 The longest sequence of decl-specifiers that could
7299 possibly be a type name is taken as the
7300 decl-specifier-seq of a declaration. The sequence shall
7301 be self-consistent as described below.
7305 As a general rule, at most one type-specifier is allowed
7306 in the complete decl-specifier-seq of a declaration. The
7307 only exceptions are the following:
7309 -- const or volatile can be combined with any other
7312 -- signed or unsigned can be combined with char, long,
7320 void g (const int Pc);
7322 Here, Pc is *not* part of the decl-specifier seq; it's
7323 the declarator. Therefore, once we see a type-specifier
7324 (other than a cv-qualifier), we forbid any additional
7325 user-defined types. We *do* still allow things like `int
7326 int' to be considered a decl-specifier-seq, and issue the
7327 error message later. */
7328 if (type_spec && !is_cv_qualifier)
7329 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7330 /* A constructor declarator cannot follow a type-specifier. */
7333 constructor_possible_p = false;
7334 found_decl_spec = true;
7338 /* If we still do not have a DECL_SPEC, then there are no more
7340 if (!found_decl_spec)
7343 decl_specs->any_specifiers_p = true;
7344 /* After we see one decl-specifier, further decl-specifiers are
7346 flags |= CP_PARSER_FLAGS_OPTIONAL;
7349 /* Don't allow a friend specifier with a class definition. */
7350 if (decl_specs->specs[(int) ds_friend] != 0
7351 && (*declares_class_or_enum & 2))
7352 error ("class definition may not be declared a friend");
7355 /* Parse an (optional) storage-class-specifier.
7357 storage-class-specifier:
7366 storage-class-specifier:
7369 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7372 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7374 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7382 /* Consume the token. */
7383 return cp_lexer_consume_token (parser->lexer)->value;
7390 /* Parse an (optional) function-specifier.
7397 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7398 Updates DECL_SPECS, if it is non-NULL. */
7401 cp_parser_function_specifier_opt (cp_parser* parser,
7402 cp_decl_specifier_seq *decl_specs)
7404 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7408 ++decl_specs->specs[(int) ds_inline];
7413 ++decl_specs->specs[(int) ds_virtual];
7418 ++decl_specs->specs[(int) ds_explicit];
7425 /* Consume the token. */
7426 return cp_lexer_consume_token (parser->lexer)->value;
7429 /* Parse a linkage-specification.
7431 linkage-specification:
7432 extern string-literal { declaration-seq [opt] }
7433 extern string-literal declaration */
7436 cp_parser_linkage_specification (cp_parser* parser)
7441 /* Look for the `extern' keyword. */
7442 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7444 /* Peek at the next token. */
7445 token = cp_lexer_peek_token (parser->lexer);
7446 /* If it's not a string-literal, then there's a problem. */
7447 if (!cp_parser_is_string_literal (token))
7449 cp_parser_error (parser, "expected language-name");
7452 /* Consume the token. */
7453 cp_lexer_consume_token (parser->lexer);
7455 /* Transform the literal into an identifier. If the literal is a
7456 wide-character string, or contains embedded NULs, then we can't
7457 handle it as the user wants. */
7458 if (token->type == CPP_WSTRING
7459 || (strlen (TREE_STRING_POINTER (token->value))
7460 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7462 cp_parser_error (parser, "invalid linkage-specification");
7463 /* Assume C++ linkage. */
7464 linkage = get_identifier ("c++");
7466 /* If the string is chained to another string, take the latter,
7467 that's the untranslated string. */
7468 else if (TREE_CHAIN (token->value))
7469 linkage = get_identifier (TREE_STRING_POINTER (TREE_CHAIN (token->value)));
7470 /* If it's a simple string constant, things are easier. */
7472 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7474 /* We're now using the new linkage. */
7475 push_lang_context (linkage);
7477 /* If the next token is a `{', then we're using the first
7479 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7481 /* Consume the `{' token. */
7482 cp_lexer_consume_token (parser->lexer);
7483 /* Parse the declarations. */
7484 cp_parser_declaration_seq_opt (parser);
7485 /* Look for the closing `}'. */
7486 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7488 /* Otherwise, there's just one declaration. */
7491 bool saved_in_unbraced_linkage_specification_p;
7493 saved_in_unbraced_linkage_specification_p
7494 = parser->in_unbraced_linkage_specification_p;
7495 parser->in_unbraced_linkage_specification_p = true;
7496 have_extern_spec = true;
7497 cp_parser_declaration (parser);
7498 have_extern_spec = false;
7499 parser->in_unbraced_linkage_specification_p
7500 = saved_in_unbraced_linkage_specification_p;
7503 /* We're done with the linkage-specification. */
7504 pop_lang_context ();
7507 /* Special member functions [gram.special] */
7509 /* Parse a conversion-function-id.
7511 conversion-function-id:
7512 operator conversion-type-id
7514 Returns an IDENTIFIER_NODE representing the operator. */
7517 cp_parser_conversion_function_id (cp_parser* parser)
7521 tree saved_qualifying_scope;
7522 tree saved_object_scope;
7525 /* Look for the `operator' token. */
7526 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7527 return error_mark_node;
7528 /* When we parse the conversion-type-id, the current scope will be
7529 reset. However, we need that information in able to look up the
7530 conversion function later, so we save it here. */
7531 saved_scope = parser->scope;
7532 saved_qualifying_scope = parser->qualifying_scope;
7533 saved_object_scope = parser->object_scope;
7534 /* We must enter the scope of the class so that the names of
7535 entities declared within the class are available in the
7536 conversion-type-id. For example, consider:
7543 S::operator I() { ... }
7545 In order to see that `I' is a type-name in the definition, we
7546 must be in the scope of `S'. */
7548 pop_p = push_scope (saved_scope);
7549 /* Parse the conversion-type-id. */
7550 type = cp_parser_conversion_type_id (parser);
7551 /* Leave the scope of the class, if any. */
7553 pop_scope (saved_scope);
7554 /* Restore the saved scope. */
7555 parser->scope = saved_scope;
7556 parser->qualifying_scope = saved_qualifying_scope;
7557 parser->object_scope = saved_object_scope;
7558 /* If the TYPE is invalid, indicate failure. */
7559 if (type == error_mark_node)
7560 return error_mark_node;
7561 return mangle_conv_op_name_for_type (type);
7564 /* Parse a conversion-type-id:
7567 type-specifier-seq conversion-declarator [opt]
7569 Returns the TYPE specified. */
7572 cp_parser_conversion_type_id (cp_parser* parser)
7575 cp_decl_specifier_seq type_specifiers;
7576 cp_declarator *declarator;
7578 /* Parse the attributes. */
7579 attributes = cp_parser_attributes_opt (parser);
7580 /* Parse the type-specifiers. */
7581 cp_parser_type_specifier_seq (parser, &type_specifiers);
7582 /* If that didn't work, stop. */
7583 if (type_specifiers.type == error_mark_node)
7584 return error_mark_node;
7585 /* Parse the conversion-declarator. */
7586 declarator = cp_parser_conversion_declarator_opt (parser);
7588 return grokdeclarator (declarator, &type_specifiers, TYPENAME,
7589 /*initialized=*/0, &attributes);
7592 /* Parse an (optional) conversion-declarator.
7594 conversion-declarator:
7595 ptr-operator conversion-declarator [opt]
7599 static cp_declarator *
7600 cp_parser_conversion_declarator_opt (cp_parser* parser)
7602 enum tree_code code;
7604 cp_cv_quals cv_quals;
7606 /* We don't know if there's a ptr-operator next, or not. */
7607 cp_parser_parse_tentatively (parser);
7608 /* Try the ptr-operator. */
7609 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7610 /* If it worked, look for more conversion-declarators. */
7611 if (cp_parser_parse_definitely (parser))
7613 cp_declarator *declarator;
7615 /* Parse another optional declarator. */
7616 declarator = cp_parser_conversion_declarator_opt (parser);
7618 /* Create the representation of the declarator. */
7620 declarator = make_ptrmem_declarator (cv_quals, class_type,
7622 else if (code == INDIRECT_REF)
7623 declarator = make_pointer_declarator (cv_quals, declarator);
7625 declarator = make_reference_declarator (cv_quals, declarator);
7633 /* Parse an (optional) ctor-initializer.
7636 : mem-initializer-list
7638 Returns TRUE iff the ctor-initializer was actually present. */
7641 cp_parser_ctor_initializer_opt (cp_parser* parser)
7643 /* If the next token is not a `:', then there is no
7644 ctor-initializer. */
7645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7647 /* Do default initialization of any bases and members. */
7648 if (DECL_CONSTRUCTOR_P (current_function_decl))
7649 finish_mem_initializers (NULL_TREE);
7654 /* Consume the `:' token. */
7655 cp_lexer_consume_token (parser->lexer);
7656 /* And the mem-initializer-list. */
7657 cp_parser_mem_initializer_list (parser);
7662 /* Parse a mem-initializer-list.
7664 mem-initializer-list:
7666 mem-initializer , mem-initializer-list */
7669 cp_parser_mem_initializer_list (cp_parser* parser)
7671 tree mem_initializer_list = NULL_TREE;
7673 /* Let the semantic analysis code know that we are starting the
7674 mem-initializer-list. */
7675 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7676 error ("only constructors take base initializers");
7678 /* Loop through the list. */
7681 tree mem_initializer;
7683 /* Parse the mem-initializer. */
7684 mem_initializer = cp_parser_mem_initializer (parser);
7685 /* Add it to the list, unless it was erroneous. */
7686 if (mem_initializer)
7688 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7689 mem_initializer_list = mem_initializer;
7691 /* If the next token is not a `,', we're done. */
7692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7694 /* Consume the `,' token. */
7695 cp_lexer_consume_token (parser->lexer);
7698 /* Perform semantic analysis. */
7699 if (DECL_CONSTRUCTOR_P (current_function_decl))
7700 finish_mem_initializers (mem_initializer_list);
7703 /* Parse a mem-initializer.
7706 mem-initializer-id ( expression-list [opt] )
7711 ( expression-list [opt] )
7713 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7714 class) or FIELD_DECL (for a non-static data member) to initialize;
7715 the TREE_VALUE is the expression-list. */
7718 cp_parser_mem_initializer (cp_parser* parser)
7720 tree mem_initializer_id;
7721 tree expression_list;
7724 /* Find out what is being initialized. */
7725 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7727 pedwarn ("anachronistic old-style base class initializer");
7728 mem_initializer_id = NULL_TREE;
7731 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7732 member = expand_member_init (mem_initializer_id);
7733 if (member && !DECL_P (member))
7734 in_base_initializer = 1;
7737 = cp_parser_parenthesized_expression_list (parser, false,
7738 /*non_constant_p=*/NULL);
7739 if (!expression_list)
7740 expression_list = void_type_node;
7742 in_base_initializer = 0;
7744 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7747 /* Parse a mem-initializer-id.
7750 :: [opt] nested-name-specifier [opt] class-name
7753 Returns a TYPE indicating the class to be initializer for the first
7754 production. Returns an IDENTIFIER_NODE indicating the data member
7755 to be initialized for the second production. */
7758 cp_parser_mem_initializer_id (cp_parser* parser)
7760 bool global_scope_p;
7761 bool nested_name_specifier_p;
7762 bool template_p = false;
7765 /* `typename' is not allowed in this context ([temp.res]). */
7766 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7768 error ("keyword `typename' not allowed in this context (a qualified "
7769 "member initializer is implicitly a type)");
7770 cp_lexer_consume_token (parser->lexer);
7772 /* Look for the optional `::' operator. */
7774 = (cp_parser_global_scope_opt (parser,
7775 /*current_scope_valid_p=*/false)
7777 /* Look for the optional nested-name-specifier. The simplest way to
7782 The keyword `typename' is not permitted in a base-specifier or
7783 mem-initializer; in these contexts a qualified name that
7784 depends on a template-parameter is implicitly assumed to be a
7787 is to assume that we have seen the `typename' keyword at this
7789 nested_name_specifier_p
7790 = (cp_parser_nested_name_specifier_opt (parser,
7791 /*typename_keyword_p=*/true,
7792 /*check_dependency_p=*/true,
7794 /*is_declaration=*/true)
7796 if (nested_name_specifier_p)
7797 template_p = cp_parser_optional_template_keyword (parser);
7798 /* If there is a `::' operator or a nested-name-specifier, then we
7799 are definitely looking for a class-name. */
7800 if (global_scope_p || nested_name_specifier_p)
7801 return cp_parser_class_name (parser,
7802 /*typename_keyword_p=*/true,
7803 /*template_keyword_p=*/template_p,
7805 /*check_dependency_p=*/true,
7806 /*class_head_p=*/false,
7807 /*is_declaration=*/true);
7808 /* Otherwise, we could also be looking for an ordinary identifier. */
7809 cp_parser_parse_tentatively (parser);
7810 /* Try a class-name. */
7811 id = cp_parser_class_name (parser,
7812 /*typename_keyword_p=*/true,
7813 /*template_keyword_p=*/false,
7815 /*check_dependency_p=*/true,
7816 /*class_head_p=*/false,
7817 /*is_declaration=*/true);
7818 /* If we found one, we're done. */
7819 if (cp_parser_parse_definitely (parser))
7821 /* Otherwise, look for an ordinary identifier. */
7822 return cp_parser_identifier (parser);
7825 /* Overloading [gram.over] */
7827 /* Parse an operator-function-id.
7829 operator-function-id:
7832 Returns an IDENTIFIER_NODE for the operator which is a
7833 human-readable spelling of the identifier, e.g., `operator +'. */
7836 cp_parser_operator_function_id (cp_parser* parser)
7838 /* Look for the `operator' keyword. */
7839 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7840 return error_mark_node;
7841 /* And then the name of the operator itself. */
7842 return cp_parser_operator (parser);
7845 /* Parse an operator.
7848 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7849 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7850 || ++ -- , ->* -> () []
7857 Returns an IDENTIFIER_NODE for the operator which is a
7858 human-readable spelling of the identifier, e.g., `operator +'. */
7861 cp_parser_operator (cp_parser* parser)
7863 tree id = NULL_TREE;
7866 /* Peek at the next token. */
7867 token = cp_lexer_peek_token (parser->lexer);
7868 /* Figure out which operator we have. */
7869 switch (token->type)
7875 /* The keyword should be either `new' or `delete'. */
7876 if (token->keyword == RID_NEW)
7878 else if (token->keyword == RID_DELETE)
7883 /* Consume the `new' or `delete' token. */
7884 cp_lexer_consume_token (parser->lexer);
7886 /* Peek at the next token. */
7887 token = cp_lexer_peek_token (parser->lexer);
7888 /* If it's a `[' token then this is the array variant of the
7890 if (token->type == CPP_OPEN_SQUARE)
7892 /* Consume the `[' token. */
7893 cp_lexer_consume_token (parser->lexer);
7894 /* Look for the `]' token. */
7895 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7896 id = ansi_opname (op == NEW_EXPR
7897 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7899 /* Otherwise, we have the non-array variant. */
7901 id = ansi_opname (op);
7907 id = ansi_opname (PLUS_EXPR);
7911 id = ansi_opname (MINUS_EXPR);
7915 id = ansi_opname (MULT_EXPR);
7919 id = ansi_opname (TRUNC_DIV_EXPR);
7923 id = ansi_opname (TRUNC_MOD_EXPR);
7927 id = ansi_opname (BIT_XOR_EXPR);
7931 id = ansi_opname (BIT_AND_EXPR);
7935 id = ansi_opname (BIT_IOR_EXPR);
7939 id = ansi_opname (BIT_NOT_EXPR);
7943 id = ansi_opname (TRUTH_NOT_EXPR);
7947 id = ansi_assopname (NOP_EXPR);
7951 id = ansi_opname (LT_EXPR);
7955 id = ansi_opname (GT_EXPR);
7959 id = ansi_assopname (PLUS_EXPR);
7963 id = ansi_assopname (MINUS_EXPR);
7967 id = ansi_assopname (MULT_EXPR);
7971 id = ansi_assopname (TRUNC_DIV_EXPR);
7975 id = ansi_assopname (TRUNC_MOD_EXPR);
7979 id = ansi_assopname (BIT_XOR_EXPR);
7983 id = ansi_assopname (BIT_AND_EXPR);
7987 id = ansi_assopname (BIT_IOR_EXPR);
7991 id = ansi_opname (LSHIFT_EXPR);
7995 id = ansi_opname (RSHIFT_EXPR);
7999 id = ansi_assopname (LSHIFT_EXPR);
8003 id = ansi_assopname (RSHIFT_EXPR);
8007 id = ansi_opname (EQ_EXPR);
8011 id = ansi_opname (NE_EXPR);
8015 id = ansi_opname (LE_EXPR);
8018 case CPP_GREATER_EQ:
8019 id = ansi_opname (GE_EXPR);
8023 id = ansi_opname (TRUTH_ANDIF_EXPR);
8027 id = ansi_opname (TRUTH_ORIF_EXPR);
8031 id = ansi_opname (POSTINCREMENT_EXPR);
8034 case CPP_MINUS_MINUS:
8035 id = ansi_opname (PREDECREMENT_EXPR);
8039 id = ansi_opname (COMPOUND_EXPR);
8042 case CPP_DEREF_STAR:
8043 id = ansi_opname (MEMBER_REF);
8047 id = ansi_opname (COMPONENT_REF);
8050 case CPP_OPEN_PAREN:
8051 /* Consume the `('. */
8052 cp_lexer_consume_token (parser->lexer);
8053 /* Look for the matching `)'. */
8054 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8055 return ansi_opname (CALL_EXPR);
8057 case CPP_OPEN_SQUARE:
8058 /* Consume the `['. */
8059 cp_lexer_consume_token (parser->lexer);
8060 /* Look for the matching `]'. */
8061 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8062 return ansi_opname (ARRAY_REF);
8066 id = ansi_opname (MIN_EXPR);
8070 id = ansi_opname (MAX_EXPR);
8074 id = ansi_assopname (MIN_EXPR);
8078 id = ansi_assopname (MAX_EXPR);
8082 /* Anything else is an error. */
8086 /* If we have selected an identifier, we need to consume the
8089 cp_lexer_consume_token (parser->lexer);
8090 /* Otherwise, no valid operator name was present. */
8093 cp_parser_error (parser, "expected operator");
8094 id = error_mark_node;
8100 /* Parse a template-declaration.
8102 template-declaration:
8103 export [opt] template < template-parameter-list > declaration
8105 If MEMBER_P is TRUE, this template-declaration occurs within a
8108 The grammar rule given by the standard isn't correct. What
8111 template-declaration:
8112 export [opt] template-parameter-list-seq
8113 decl-specifier-seq [opt] init-declarator [opt] ;
8114 export [opt] template-parameter-list-seq
8117 template-parameter-list-seq:
8118 template-parameter-list-seq [opt]
8119 template < template-parameter-list > */
8122 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8124 /* Check for `export'. */
8125 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8127 /* Consume the `export' token. */
8128 cp_lexer_consume_token (parser->lexer);
8129 /* Warn that we do not support `export'. */
8130 warning ("keyword `export' not implemented, and will be ignored");
8133 cp_parser_template_declaration_after_export (parser, member_p);
8136 /* Parse a template-parameter-list.
8138 template-parameter-list:
8140 template-parameter-list , template-parameter
8142 Returns a TREE_LIST. Each node represents a template parameter.
8143 The nodes are connected via their TREE_CHAINs. */
8146 cp_parser_template_parameter_list (cp_parser* parser)
8148 tree parameter_list = NULL_TREE;
8156 /* Parse the template-parameter. */
8157 parameter = cp_parser_template_parameter (parser, &is_non_type);
8158 /* Add it to the list. */
8159 parameter_list = process_template_parm (parameter_list,
8162 /* Peek at the next token. */
8163 token = cp_lexer_peek_token (parser->lexer);
8164 /* If it's not a `,', we're done. */
8165 if (token->type != CPP_COMMA)
8167 /* Otherwise, consume the `,' token. */
8168 cp_lexer_consume_token (parser->lexer);
8171 return parameter_list;
8174 /* Parse a template-parameter.
8178 parameter-declaration
8180 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
8181 TREE_PURPOSE is the default value, if any. *IS_NON_TYPE is set to
8182 true iff this parameter is a non-type parameter. */
8185 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8188 cp_parameter_declarator *parameter_declarator;
8190 /* Assume it is a type parameter or a template parameter. */
8191 *is_non_type = false;
8192 /* Peek at the next token. */
8193 token = cp_lexer_peek_token (parser->lexer);
8194 /* If it is `class' or `template', we have a type-parameter. */
8195 if (token->keyword == RID_TEMPLATE)
8196 return cp_parser_type_parameter (parser);
8197 /* If it is `class' or `typename' we do not know yet whether it is a
8198 type parameter or a non-type parameter. Consider:
8200 template <typename T, typename T::X X> ...
8204 template <class C, class D*> ...
8206 Here, the first parameter is a type parameter, and the second is
8207 a non-type parameter. We can tell by looking at the token after
8208 the identifier -- if it is a `,', `=', or `>' then we have a type
8210 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8212 /* Peek at the token after `class' or `typename'. */
8213 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8214 /* If it's an identifier, skip it. */
8215 if (token->type == CPP_NAME)
8216 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8217 /* Now, see if the token looks like the end of a template
8219 if (token->type == CPP_COMMA
8220 || token->type == CPP_EQ
8221 || token->type == CPP_GREATER)
8222 return cp_parser_type_parameter (parser);
8225 /* Otherwise, it is a non-type parameter.
8229 When parsing a default template-argument for a non-type
8230 template-parameter, the first non-nested `>' is taken as the end
8231 of the template parameter-list rather than a greater-than
8233 *is_non_type = true;
8234 parameter_declarator
8235 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8236 /*parenthesized_p=*/NULL);
8237 return (build_tree_list
8238 (parameter_declarator->default_argument,
8239 grokdeclarator (parameter_declarator->declarator,
8240 ¶meter_declarator->decl_specifiers,
8241 PARM, /*initialized=*/0,
8242 /*attrlist=*/NULL)));
8245 /* Parse a type-parameter.
8248 class identifier [opt]
8249 class identifier [opt] = type-id
8250 typename identifier [opt]
8251 typename identifier [opt] = type-id
8252 template < template-parameter-list > class identifier [opt]
8253 template < template-parameter-list > class identifier [opt]
8256 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8257 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8258 the declaration of the parameter. */
8261 cp_parser_type_parameter (cp_parser* parser)
8266 /* Look for a keyword to tell us what kind of parameter this is. */
8267 token = cp_parser_require (parser, CPP_KEYWORD,
8268 "`class', `typename', or `template'");
8270 return error_mark_node;
8272 switch (token->keyword)
8278 tree default_argument;
8280 /* If the next token is an identifier, then it names the
8282 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8283 identifier = cp_parser_identifier (parser);
8285 identifier = NULL_TREE;
8287 /* Create the parameter. */
8288 parameter = finish_template_type_parm (class_type_node, identifier);
8290 /* If the next token is an `=', we have a default argument. */
8291 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8293 /* Consume the `=' token. */
8294 cp_lexer_consume_token (parser->lexer);
8295 /* Parse the default-argument. */
8296 default_argument = cp_parser_type_id (parser);
8299 default_argument = NULL_TREE;
8301 /* Create the combined representation of the parameter and the
8302 default argument. */
8303 parameter = build_tree_list (default_argument, parameter);
8309 tree parameter_list;
8311 tree default_argument;
8313 /* Look for the `<'. */
8314 cp_parser_require (parser, CPP_LESS, "`<'");
8315 /* Parse the template-parameter-list. */
8316 begin_template_parm_list ();
8318 = cp_parser_template_parameter_list (parser);
8319 parameter_list = end_template_parm_list (parameter_list);
8320 /* Look for the `>'. */
8321 cp_parser_require (parser, CPP_GREATER, "`>'");
8322 /* Look for the `class' keyword. */
8323 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8324 /* If the next token is an `=', then there is a
8325 default-argument. If the next token is a `>', we are at
8326 the end of the parameter-list. If the next token is a `,',
8327 then we are at the end of this parameter. */
8328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8329 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8330 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8331 identifier = cp_parser_identifier (parser);
8333 identifier = NULL_TREE;
8334 /* Create the template parameter. */
8335 parameter = finish_template_template_parm (class_type_node,
8338 /* If the next token is an `=', then there is a
8339 default-argument. */
8340 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8344 /* Consume the `='. */
8345 cp_lexer_consume_token (parser->lexer);
8346 /* Parse the id-expression. */
8348 = cp_parser_id_expression (parser,
8349 /*template_keyword_p=*/false,
8350 /*check_dependency_p=*/true,
8351 /*template_p=*/&is_template,
8352 /*declarator_p=*/false);
8353 if (TREE_CODE (default_argument) == TYPE_DECL)
8354 /* If the id-expression was a template-id that refers to
8355 a template-class, we already have the declaration here,
8356 so no further lookup is needed. */
8359 /* Look up the name. */
8361 = cp_parser_lookup_name (parser, default_argument,
8363 /*is_template=*/is_template,
8364 /*is_namespace=*/false,
8365 /*check_dependency=*/true,
8366 /*ambiguous_p=*/NULL);
8367 /* See if the default argument is valid. */
8369 = check_template_template_default_arg (default_argument);
8372 default_argument = NULL_TREE;
8374 /* Create the combined representation of the parameter and the
8375 default argument. */
8376 parameter = build_tree_list (default_argument, parameter);
8381 /* Anything else is an error. */
8382 cp_parser_error (parser,
8383 "expected `class', `typename', or `template'");
8384 parameter = error_mark_node;
8390 /* Parse a template-id.
8393 template-name < template-argument-list [opt] >
8395 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8396 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8397 returned. Otherwise, if the template-name names a function, or set
8398 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8399 names a class, returns a TYPE_DECL for the specialization.
8401 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8402 uninstantiated templates. */
8405 cp_parser_template_id (cp_parser *parser,
8406 bool template_keyword_p,
8407 bool check_dependency_p,
8408 bool is_declaration)
8413 ptrdiff_t start_of_id;
8414 tree access_check = NULL_TREE;
8415 cp_token *next_token, *next_token_2;
8418 /* If the next token corresponds to a template-id, there is no need
8420 next_token = cp_lexer_peek_token (parser->lexer);
8421 if (next_token->type == CPP_TEMPLATE_ID)
8426 /* Get the stored value. */
8427 value = cp_lexer_consume_token (parser->lexer)->value;
8428 /* Perform any access checks that were deferred. */
8429 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8430 perform_or_defer_access_check (TREE_PURPOSE (check),
8431 TREE_VALUE (check));
8432 /* Return the stored value. */
8433 return TREE_VALUE (value);
8436 /* Avoid performing name lookup if there is no possibility of
8437 finding a template-id. */
8438 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8439 || (next_token->type == CPP_NAME
8440 && !cp_parser_nth_token_starts_template_argument_list_p
8443 cp_parser_error (parser, "expected template-id");
8444 return error_mark_node;
8447 /* Remember where the template-id starts. */
8448 if (cp_parser_parsing_tentatively (parser)
8449 && !cp_parser_committed_to_tentative_parse (parser))
8451 next_token = cp_lexer_peek_token (parser->lexer);
8452 start_of_id = cp_lexer_token_difference (parser->lexer,
8453 parser->lexer->first_token,
8459 push_deferring_access_checks (dk_deferred);
8461 /* Parse the template-name. */
8462 is_identifier = false;
8463 template = cp_parser_template_name (parser, template_keyword_p,
8467 if (template == error_mark_node || is_identifier)
8469 pop_deferring_access_checks ();
8473 /* If we find the sequence `[:' after a template-name, it's probably
8474 a digraph-typo for `< ::'. Substitute the tokens and check if we can
8475 parse correctly the argument list. */
8476 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
8477 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8478 if (next_token->type == CPP_OPEN_SQUARE
8479 && next_token->flags & DIGRAPH
8480 && next_token_2->type == CPP_COLON
8481 && !(next_token_2->flags & PREV_WHITE))
8483 cp_parser_parse_tentatively (parser);
8484 /* Change `:' into `::'. */
8485 next_token_2->type = CPP_SCOPE;
8486 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8488 cp_lexer_consume_token (parser->lexer);
8489 /* Parse the arguments. */
8490 arguments = cp_parser_enclosed_template_argument_list (parser);
8491 if (!cp_parser_parse_definitely (parser))
8493 /* If we couldn't parse an argument list, then we revert our changes
8494 and return simply an error. Maybe this is not a template-id
8496 next_token_2->type = CPP_COLON;
8497 cp_parser_error (parser, "expected `<'");
8498 pop_deferring_access_checks ();
8499 return error_mark_node;
8501 /* Otherwise, emit an error about the invalid digraph, but continue
8502 parsing because we got our argument list. */
8503 pedwarn ("`<::' cannot begin a template-argument list");
8504 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8505 "between `<' and `::'");
8506 if (!flag_permissive)
8511 inform ("(if you use `-fpermissive' G++ will accept your code)");
8518 /* Look for the `<' that starts the template-argument-list. */
8519 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8521 pop_deferring_access_checks ();
8522 return error_mark_node;
8524 /* Parse the arguments. */
8525 arguments = cp_parser_enclosed_template_argument_list (parser);
8528 /* Build a representation of the specialization. */
8529 if (TREE_CODE (template) == IDENTIFIER_NODE)
8530 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8531 else if (DECL_CLASS_TEMPLATE_P (template)
8532 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8534 = finish_template_type (template, arguments,
8535 cp_lexer_next_token_is (parser->lexer,
8539 /* If it's not a class-template or a template-template, it should be
8540 a function-template. */
8541 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8542 || TREE_CODE (template) == OVERLOAD
8543 || BASELINK_P (template)));
8545 template_id = lookup_template_function (template, arguments);
8548 /* Retrieve any deferred checks. Do not pop this access checks yet
8549 so the memory will not be reclaimed during token replacing below. */
8550 access_check = get_deferred_access_checks ();
8552 /* If parsing tentatively, replace the sequence of tokens that makes
8553 up the template-id with a CPP_TEMPLATE_ID token. That way,
8554 should we re-parse the token stream, we will not have to repeat
8555 the effort required to do the parse, nor will we issue duplicate
8556 error messages about problems during instantiation of the
8558 if (start_of_id >= 0)
8562 /* Find the token that corresponds to the start of the
8564 token = cp_lexer_advance_token (parser->lexer,
8565 parser->lexer->first_token,
8568 /* Reset the contents of the START_OF_ID token. */
8569 token->type = CPP_TEMPLATE_ID;
8570 token->value = build_tree_list (access_check, template_id);
8571 token->keyword = RID_MAX;
8572 /* Purge all subsequent tokens. */
8573 cp_lexer_purge_tokens_after (parser->lexer, token);
8576 pop_deferring_access_checks ();
8580 /* Parse a template-name.
8585 The standard should actually say:
8589 operator-function-id
8591 A defect report has been filed about this issue.
8593 A conversion-function-id cannot be a template name because they cannot
8594 be part of a template-id. In fact, looking at this code:
8598 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8599 It is impossible to call a templated conversion-function-id with an
8600 explicit argument list, since the only allowed template parameter is
8601 the type to which it is converting.
8603 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8604 `template' keyword, in a construction like:
8608 In that case `f' is taken to be a template-name, even though there
8609 is no way of knowing for sure.
8611 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8612 name refers to a set of overloaded functions, at least one of which
8613 is a template, or an IDENTIFIER_NODE with the name of the template,
8614 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8615 names are looked up inside uninstantiated templates. */
8618 cp_parser_template_name (cp_parser* parser,
8619 bool template_keyword_p,
8620 bool check_dependency_p,
8621 bool is_declaration,
8622 bool *is_identifier)
8628 /* If the next token is `operator', then we have either an
8629 operator-function-id or a conversion-function-id. */
8630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8632 /* We don't know whether we're looking at an
8633 operator-function-id or a conversion-function-id. */
8634 cp_parser_parse_tentatively (parser);
8635 /* Try an operator-function-id. */
8636 identifier = cp_parser_operator_function_id (parser);
8637 /* If that didn't work, try a conversion-function-id. */
8638 if (!cp_parser_parse_definitely (parser))
8640 cp_parser_error (parser, "expected template-name");
8641 return error_mark_node;
8644 /* Look for the identifier. */
8646 identifier = cp_parser_identifier (parser);
8648 /* If we didn't find an identifier, we don't have a template-id. */
8649 if (identifier == error_mark_node)
8650 return error_mark_node;
8652 /* If the name immediately followed the `template' keyword, then it
8653 is a template-name. However, if the next token is not `<', then
8654 we do not treat it as a template-name, since it is not being used
8655 as part of a template-id. This enables us to handle constructs
8658 template <typename T> struct S { S(); };
8659 template <typename T> S<T>::S();
8661 correctly. We would treat `S' as a template -- if it were `S<T>'
8662 -- but we do not if there is no `<'. */
8664 if (processing_template_decl
8665 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8667 /* In a declaration, in a dependent context, we pretend that the
8668 "template" keyword was present in order to improve error
8669 recovery. For example, given:
8671 template <typename T> void f(T::X<int>);
8673 we want to treat "X<int>" as a template-id. */
8675 && !template_keyword_p
8676 && parser->scope && TYPE_P (parser->scope)
8677 && dependent_type_p (parser->scope)
8678 /* Do not do this for dtors (or ctors), since they never
8679 need the template keyword before their name. */
8680 && !constructor_name_p (identifier, parser->scope))
8684 /* Explain what went wrong. */
8685 error ("non-template `%D' used as template", identifier);
8686 inform ("use `%T::template %D' to indicate that it is a template",
8687 parser->scope, identifier);
8688 /* If parsing tentatively, find the location of the "<"
8690 if (cp_parser_parsing_tentatively (parser)
8691 && !cp_parser_committed_to_tentative_parse (parser))
8693 cp_parser_simulate_error (parser);
8694 token = cp_lexer_peek_token (parser->lexer);
8695 token = cp_lexer_prev_token (parser->lexer, token);
8696 start = cp_lexer_token_difference (parser->lexer,
8697 parser->lexer->first_token,
8702 /* Parse the template arguments so that we can issue error
8703 messages about them. */
8704 cp_lexer_consume_token (parser->lexer);
8705 cp_parser_enclosed_template_argument_list (parser);
8706 /* Skip tokens until we find a good place from which to
8707 continue parsing. */
8708 cp_parser_skip_to_closing_parenthesis (parser,
8709 /*recovering=*/true,
8711 /*consume_paren=*/false);
8712 /* If parsing tentatively, permanently remove the
8713 template argument list. That will prevent duplicate
8714 error messages from being issued about the missing
8715 "template" keyword. */
8718 token = cp_lexer_advance_token (parser->lexer,
8719 parser->lexer->first_token,
8721 cp_lexer_purge_tokens_after (parser->lexer, token);
8724 *is_identifier = true;
8728 /* If the "template" keyword is present, then there is generally
8729 no point in doing name-lookup, so we just return IDENTIFIER.
8730 But, if the qualifying scope is non-dependent then we can
8731 (and must) do name-lookup normally. */
8732 if (template_keyword_p
8734 || (TYPE_P (parser->scope)
8735 && dependent_type_p (parser->scope))))
8739 /* Look up the name. */
8740 decl = cp_parser_lookup_name (parser, identifier,
8742 /*is_template=*/false,
8743 /*is_namespace=*/false,
8745 /*ambiguous_p=*/NULL);
8746 decl = maybe_get_template_decl_from_type_decl (decl);
8748 /* If DECL is a template, then the name was a template-name. */
8749 if (TREE_CODE (decl) == TEMPLATE_DECL)
8753 /* The standard does not explicitly indicate whether a name that
8754 names a set of overloaded declarations, some of which are
8755 templates, is a template-name. However, such a name should
8756 be a template-name; otherwise, there is no way to form a
8757 template-id for the overloaded templates. */
8758 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8759 if (TREE_CODE (fns) == OVERLOAD)
8763 for (fn = fns; fn; fn = OVL_NEXT (fn))
8764 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8769 /* Otherwise, the name does not name a template. */
8770 cp_parser_error (parser, "expected template-name");
8771 return error_mark_node;
8775 /* If DECL is dependent, and refers to a function, then just return
8776 its name; we will look it up again during template instantiation. */
8777 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8779 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8780 if (TYPE_P (scope) && dependent_type_p (scope))
8787 /* Parse a template-argument-list.
8789 template-argument-list:
8791 template-argument-list , template-argument
8793 Returns a TREE_VEC containing the arguments. */
8796 cp_parser_template_argument_list (cp_parser* parser)
8798 tree fixed_args[10];
8799 unsigned n_args = 0;
8800 unsigned alloced = 10;
8801 tree *arg_ary = fixed_args;
8803 bool saved_in_template_argument_list_p;
8805 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8806 parser->in_template_argument_list_p = true;
8812 /* Consume the comma. */
8813 cp_lexer_consume_token (parser->lexer);
8815 /* Parse the template-argument. */
8816 argument = cp_parser_template_argument (parser);
8817 if (n_args == alloced)
8821 if (arg_ary == fixed_args)
8823 arg_ary = xmalloc (sizeof (tree) * alloced);
8824 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8827 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8829 arg_ary[n_args++] = argument;
8831 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8833 vec = make_tree_vec (n_args);
8836 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8838 if (arg_ary != fixed_args)
8840 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8844 /* Parse a template-argument.
8847 assignment-expression
8851 The representation is that of an assignment-expression, type-id, or
8852 id-expression -- except that the qualified id-expression is
8853 evaluated, so that the value returned is either a DECL or an
8856 Although the standard says "assignment-expression", it forbids
8857 throw-expressions or assignments in the template argument.
8858 Therefore, we use "conditional-expression" instead. */
8861 cp_parser_template_argument (cp_parser* parser)
8866 bool maybe_type_id = false;
8869 tree qualifying_class;
8871 /* There's really no way to know what we're looking at, so we just
8872 try each alternative in order.
8876 In a template-argument, an ambiguity between a type-id and an
8877 expression is resolved to a type-id, regardless of the form of
8878 the corresponding template-parameter.
8880 Therefore, we try a type-id first. */
8881 cp_parser_parse_tentatively (parser);
8882 argument = cp_parser_type_id (parser);
8883 /* If there was no error parsing the type-id but the next token is a '>>',
8884 we probably found a typo for '> >'. But there are type-id which are
8885 also valid expressions. For instance:
8887 struct X { int operator >> (int); };
8888 template <int V> struct Foo {};
8891 Here 'X()' is a valid type-id of a function type, but the user just
8892 wanted to write the expression "X() >> 5". Thus, we remember that we
8893 found a valid type-id, but we still try to parse the argument as an
8894 expression to see what happens. */
8895 if (!cp_parser_error_occurred (parser)
8896 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8898 maybe_type_id = true;
8899 cp_parser_abort_tentative_parse (parser);
8903 /* If the next token isn't a `,' or a `>', then this argument wasn't
8904 really finished. This means that the argument is not a valid
8906 if (!cp_parser_next_token_ends_template_argument_p (parser))
8907 cp_parser_error (parser, "expected template-argument");
8908 /* If that worked, we're done. */
8909 if (cp_parser_parse_definitely (parser))
8912 /* We're still not sure what the argument will be. */
8913 cp_parser_parse_tentatively (parser);
8914 /* Try a template. */
8915 argument = cp_parser_id_expression (parser,
8916 /*template_keyword_p=*/false,
8917 /*check_dependency_p=*/true,
8919 /*declarator_p=*/false);
8920 /* If the next token isn't a `,' or a `>', then this argument wasn't
8922 if (!cp_parser_next_token_ends_template_argument_p (parser))
8923 cp_parser_error (parser, "expected template-argument");
8924 if (!cp_parser_error_occurred (parser))
8926 /* Figure out what is being referred to. If the id-expression
8927 was for a class template specialization, then we will have a
8928 TYPE_DECL at this point. There is no need to do name lookup
8929 at this point in that case. */
8930 if (TREE_CODE (argument) != TYPE_DECL)
8931 argument = cp_parser_lookup_name (parser, argument,
8933 /*is_template=*/template_p,
8934 /*is_namespace=*/false,
8935 /*check_dependency=*/true,
8936 /*ambiguous_p=*/NULL);
8937 if (TREE_CODE (argument) != TEMPLATE_DECL
8938 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8939 cp_parser_error (parser, "expected template-name");
8941 if (cp_parser_parse_definitely (parser))
8943 /* It must be a non-type argument. There permitted cases are given
8944 in [temp.arg.nontype]:
8946 -- an integral constant-expression of integral or enumeration
8949 -- the name of a non-type template-parameter; or
8951 -- the name of an object or function with external linkage...
8953 -- the address of an object or function with external linkage...
8955 -- a pointer to member... */
8956 /* Look for a non-type template parameter. */
8957 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8959 cp_parser_parse_tentatively (parser);
8960 argument = cp_parser_primary_expression (parser,
8963 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8964 || !cp_parser_next_token_ends_template_argument_p (parser))
8965 cp_parser_simulate_error (parser);
8966 if (cp_parser_parse_definitely (parser))
8969 /* If the next token is "&", the argument must be the address of an
8970 object or function with external linkage. */
8971 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8973 cp_lexer_consume_token (parser->lexer);
8974 /* See if we might have an id-expression. */
8975 token = cp_lexer_peek_token (parser->lexer);
8976 if (token->type == CPP_NAME
8977 || token->keyword == RID_OPERATOR
8978 || token->type == CPP_SCOPE
8979 || token->type == CPP_TEMPLATE_ID
8980 || token->type == CPP_NESTED_NAME_SPECIFIER)
8982 cp_parser_parse_tentatively (parser);
8983 argument = cp_parser_primary_expression (parser,
8986 if (cp_parser_error_occurred (parser)
8987 || !cp_parser_next_token_ends_template_argument_p (parser))
8988 cp_parser_abort_tentative_parse (parser);
8991 if (qualifying_class)
8992 argument = finish_qualified_id_expr (qualifying_class,
8996 if (TREE_CODE (argument) == VAR_DECL)
8998 /* A variable without external linkage might still be a
8999 valid constant-expression, so no error is issued here
9000 if the external-linkage check fails. */
9001 if (!DECL_EXTERNAL_LINKAGE_P (argument))
9002 cp_parser_simulate_error (parser);
9004 else if (is_overloaded_fn (argument))
9005 /* All overloaded functions are allowed; if the external
9006 linkage test does not pass, an error will be issued
9010 && (TREE_CODE (argument) == OFFSET_REF
9011 || TREE_CODE (argument) == SCOPE_REF))
9012 /* A pointer-to-member. */
9015 cp_parser_simulate_error (parser);
9017 if (cp_parser_parse_definitely (parser))
9020 argument = build_x_unary_op (ADDR_EXPR, argument);
9025 /* If the argument started with "&", there are no other valid
9026 alternatives at this point. */
9029 cp_parser_error (parser, "invalid non-type template argument");
9030 return error_mark_node;
9032 /* If the argument wasn't successfully parsed as a type-id followed
9033 by '>>', the argument can only be a constant expression now.
9034 Otherwise, we try parsing the constant-expression tentatively,
9035 because the argument could really be a type-id. */
9037 cp_parser_parse_tentatively (parser);
9038 argument = cp_parser_constant_expression (parser,
9039 /*allow_non_constant_p=*/false,
9040 /*non_constant_p=*/NULL);
9041 argument = fold_non_dependent_expr (argument);
9044 if (!cp_parser_next_token_ends_template_argument_p (parser))
9045 cp_parser_error (parser, "expected template-argument");
9046 if (cp_parser_parse_definitely (parser))
9048 /* We did our best to parse the argument as a non type-id, but that
9049 was the only alternative that matched (albeit with a '>' after
9050 it). We can assume it's just a typo from the user, and a
9051 diagnostic will then be issued. */
9052 return cp_parser_type_id (parser);
9055 /* Parse an explicit-instantiation.
9057 explicit-instantiation:
9058 template declaration
9060 Although the standard says `declaration', what it really means is:
9062 explicit-instantiation:
9063 template decl-specifier-seq [opt] declarator [opt] ;
9065 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9066 supposed to be allowed. A defect report has been filed about this
9071 explicit-instantiation:
9072 storage-class-specifier template
9073 decl-specifier-seq [opt] declarator [opt] ;
9074 function-specifier template
9075 decl-specifier-seq [opt] declarator [opt] ; */
9078 cp_parser_explicit_instantiation (cp_parser* parser)
9080 int declares_class_or_enum;
9081 cp_decl_specifier_seq decl_specifiers;
9082 tree extension_specifier = NULL_TREE;
9084 /* Look for an (optional) storage-class-specifier or
9085 function-specifier. */
9086 if (cp_parser_allow_gnu_extensions_p (parser))
9089 = cp_parser_storage_class_specifier_opt (parser);
9090 if (!extension_specifier)
9092 = cp_parser_function_specifier_opt (parser,
9093 /*decl_specs=*/NULL);
9096 /* Look for the `template' keyword. */
9097 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9098 /* Let the front end know that we are processing an explicit
9100 begin_explicit_instantiation ();
9101 /* [temp.explicit] says that we are supposed to ignore access
9102 control while processing explicit instantiation directives. */
9103 push_deferring_access_checks (dk_no_check);
9104 /* Parse a decl-specifier-seq. */
9105 cp_parser_decl_specifier_seq (parser,
9106 CP_PARSER_FLAGS_OPTIONAL,
9108 &declares_class_or_enum);
9109 /* If there was exactly one decl-specifier, and it declared a class,
9110 and there's no declarator, then we have an explicit type
9112 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9116 type = check_tag_decl (&decl_specifiers);
9117 /* Turn access control back on for names used during
9118 template instantiation. */
9119 pop_deferring_access_checks ();
9121 do_type_instantiation (type, extension_specifier, /*complain=*/1);
9125 cp_declarator *declarator;
9128 /* Parse the declarator. */
9130 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9131 /*ctor_dtor_or_conv_p=*/NULL,
9132 /*parenthesized_p=*/NULL);
9133 cp_parser_check_for_definition_in_return_type (declarator,
9134 declares_class_or_enum);
9135 if (declarator != cp_error_declarator)
9137 decl = grokdeclarator (declarator, &decl_specifiers,
9139 /* Turn access control back on for names used during
9140 template instantiation. */
9141 pop_deferring_access_checks ();
9142 /* Do the explicit instantiation. */
9143 do_decl_instantiation (decl, extension_specifier);
9147 pop_deferring_access_checks ();
9148 /* Skip the body of the explicit instantiation. */
9149 cp_parser_skip_to_end_of_statement (parser);
9152 /* We're done with the instantiation. */
9153 end_explicit_instantiation ();
9155 cp_parser_consume_semicolon_at_end_of_statement (parser);
9158 /* Parse an explicit-specialization.
9160 explicit-specialization:
9161 template < > declaration
9163 Although the standard says `declaration', what it really means is:
9165 explicit-specialization:
9166 template <> decl-specifier [opt] init-declarator [opt] ;
9167 template <> function-definition
9168 template <> explicit-specialization
9169 template <> template-declaration */
9172 cp_parser_explicit_specialization (cp_parser* parser)
9174 /* Look for the `template' keyword. */
9175 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9176 /* Look for the `<'. */
9177 cp_parser_require (parser, CPP_LESS, "`<'");
9178 /* Look for the `>'. */
9179 cp_parser_require (parser, CPP_GREATER, "`>'");
9180 /* We have processed another parameter list. */
9181 ++parser->num_template_parameter_lists;
9182 /* Let the front end know that we are beginning a specialization. */
9183 begin_specialization ();
9185 /* If the next keyword is `template', we need to figure out whether
9186 or not we're looking a template-declaration. */
9187 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9189 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9190 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9191 cp_parser_template_declaration_after_export (parser,
9192 /*member_p=*/false);
9194 cp_parser_explicit_specialization (parser);
9197 /* Parse the dependent declaration. */
9198 cp_parser_single_declaration (parser,
9202 /* We're done with the specialization. */
9203 end_specialization ();
9204 /* We're done with this parameter list. */
9205 --parser->num_template_parameter_lists;
9208 /* Parse a type-specifier.
9211 simple-type-specifier
9214 elaborated-type-specifier
9222 Returns a representation of the type-specifier. For a
9223 class-specifier, enum-specifier, or elaborated-type-specifier, a
9224 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9226 The parser flags FLAGS is used to control type-specifier parsing.
9228 If IS_DECLARATION is TRUE, then this type-specifier is appearing
9229 in a decl-specifier-seq.
9231 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9232 class-specifier, enum-specifier, or elaborated-type-specifier, then
9233 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
9234 if a type is declared; 2 if it is defined. Otherwise, it is set to
9237 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9238 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
9242 cp_parser_type_specifier (cp_parser* parser,
9243 cp_parser_flags flags,
9244 cp_decl_specifier_seq *decl_specs,
9245 bool is_declaration,
9246 int* declares_class_or_enum,
9247 bool* is_cv_qualifier)
9249 tree type_spec = NULL_TREE;
9252 cp_decl_spec ds = ds_last;
9254 /* Assume this type-specifier does not declare a new type. */
9255 if (declares_class_or_enum)
9256 *declares_class_or_enum = 0;
9257 /* And that it does not specify a cv-qualifier. */
9258 if (is_cv_qualifier)
9259 *is_cv_qualifier = false;
9260 /* Peek at the next token. */
9261 token = cp_lexer_peek_token (parser->lexer);
9263 /* If we're looking at a keyword, we can use that to guide the
9264 production we choose. */
9265 keyword = token->keyword;
9269 /* 'enum' [identifier] '{' introduces an enum-specifier;
9270 'enum' <anything else> introduces an elaborated-type-specifier. */
9271 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9272 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9273 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9276 type_spec = cp_parser_enum_specifier (parser);
9277 if (declares_class_or_enum)
9278 *declares_class_or_enum = 2;
9280 cp_parser_set_decl_spec_type (decl_specs,
9282 /*user_defined_p=*/true);
9286 goto elaborated_type_specifier;
9288 /* Any of these indicate either a class-specifier, or an
9289 elaborated-type-specifier. */
9293 /* Parse tentatively so that we can back up if we don't find a
9295 cp_parser_parse_tentatively (parser);
9296 /* Look for the class-specifier. */
9297 type_spec = cp_parser_class_specifier (parser);
9298 /* If that worked, we're done. */
9299 if (cp_parser_parse_definitely (parser))
9301 if (declares_class_or_enum)
9302 *declares_class_or_enum = 2;
9304 cp_parser_set_decl_spec_type (decl_specs,
9306 /*user_defined_p=*/true);
9311 elaborated_type_specifier:
9312 /* We're declaring (not defining) a class or enum. */
9313 if (declares_class_or_enum)
9314 *declares_class_or_enum = 1;
9318 /* Look for an elaborated-type-specifier. */
9320 = (cp_parser_elaborated_type_specifier
9322 decl_specs && decl_specs->specs[(int) ds_friend],
9325 cp_parser_set_decl_spec_type (decl_specs,
9327 /*user_defined_p=*/true);
9332 if (is_cv_qualifier)
9333 *is_cv_qualifier = true;
9338 if (is_cv_qualifier)
9339 *is_cv_qualifier = true;
9344 if (is_cv_qualifier)
9345 *is_cv_qualifier = true;
9349 /* The `__complex__' keyword is a GNU extension. */
9357 /* Handle simple keywords. */
9362 ++decl_specs->specs[(int)ds];
9363 decl_specs->any_specifiers_p = true;
9365 return cp_lexer_consume_token (parser->lexer)->value;
9368 /* If we do not already have a type-specifier, assume we are looking
9369 at a simple-type-specifier. */
9370 type_spec = cp_parser_simple_type_specifier (parser,
9374 /* If we didn't find a type-specifier, and a type-specifier was not
9375 optional in this context, issue an error message. */
9376 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9378 cp_parser_error (parser, "expected type specifier");
9379 return error_mark_node;
9385 /* Parse a simple-type-specifier.
9387 simple-type-specifier:
9388 :: [opt] nested-name-specifier [opt] type-name
9389 :: [opt] nested-name-specifier template template-id
9404 simple-type-specifier:
9405 __typeof__ unary-expression
9406 __typeof__ ( type-id )
9408 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
9409 appropriately updated. */
9412 cp_parser_simple_type_specifier (cp_parser* parser,
9413 cp_decl_specifier_seq *decl_specs,
9414 cp_parser_flags flags)
9416 tree type = NULL_TREE;
9419 /* Peek at the next token. */
9420 token = cp_lexer_peek_token (parser->lexer);
9422 /* If we're looking at a keyword, things are easy. */
9423 switch (token->keyword)
9427 decl_specs->explicit_char_p = true;
9428 type = char_type_node;
9431 type = wchar_type_node;
9434 type = boolean_type_node;
9438 ++decl_specs->specs[(int) ds_short];
9439 type = short_integer_type_node;
9443 decl_specs->explicit_int_p = true;
9444 type = integer_type_node;
9448 ++decl_specs->specs[(int) ds_long];
9449 type = long_integer_type_node;
9453 ++decl_specs->specs[(int) ds_signed];
9454 type = integer_type_node;
9458 ++decl_specs->specs[(int) ds_unsigned];
9459 type = unsigned_type_node;
9462 type = float_type_node;
9465 type = double_type_node;
9468 type = void_type_node;
9472 /* Consume the `typeof' token. */
9473 cp_lexer_consume_token (parser->lexer);
9474 /* Parse the operand to `typeof'. */
9475 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9476 /* If it is not already a TYPE, take its type. */
9478 type = finish_typeof (type);
9481 cp_parser_set_decl_spec_type (decl_specs, type,
9482 /*user_defined_p=*/true);
9490 /* If the type-specifier was for a built-in type, we're done. */
9495 /* Record the type. */
9497 && (token->keyword != RID_SIGNED
9498 && token->keyword != RID_UNSIGNED
9499 && token->keyword != RID_SHORT
9500 && token->keyword != RID_LONG))
9501 cp_parser_set_decl_spec_type (decl_specs,
9503 /*user_defined=*/false);
9505 decl_specs->any_specifiers_p = true;
9507 /* Consume the token. */
9508 id = cp_lexer_consume_token (parser->lexer)->value;
9510 /* There is no valid C++ program where a non-template type is
9511 followed by a "<". That usually indicates that the user thought
9512 that the type was a template. */
9513 cp_parser_check_for_invalid_template_id (parser, type);
9515 return TYPE_NAME (type);
9518 /* The type-specifier must be a user-defined type. */
9519 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9524 /* Don't gobble tokens or issue error messages if this is an
9525 optional type-specifier. */
9526 if (flags & CP_PARSER_FLAGS_OPTIONAL)
9527 cp_parser_parse_tentatively (parser);
9529 /* Look for the optional `::' operator. */
9531 = (cp_parser_global_scope_opt (parser,
9532 /*current_scope_valid_p=*/false)
9534 /* Look for the nested-name specifier. */
9536 = (cp_parser_nested_name_specifier_opt (parser,
9537 /*typename_keyword_p=*/false,
9538 /*check_dependency_p=*/true,
9540 /*is_declaration=*/false)
9542 /* If we have seen a nested-name-specifier, and the next token
9543 is `template', then we are using the template-id production. */
9545 && cp_parser_optional_template_keyword (parser))
9547 /* Look for the template-id. */
9548 type = cp_parser_template_id (parser,
9549 /*template_keyword_p=*/true,
9550 /*check_dependency_p=*/true,
9551 /*is_declaration=*/false);
9552 /* If the template-id did not name a type, we are out of
9554 if (TREE_CODE (type) != TYPE_DECL)
9556 cp_parser_error (parser, "expected template-id for type");
9560 /* Otherwise, look for a type-name. */
9562 type = cp_parser_type_name (parser);
9563 /* Keep track of all name-lookups performed in class scopes. */
9567 && TREE_CODE (type) == TYPE_DECL
9568 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9569 maybe_note_name_used_in_class (DECL_NAME (type), type);
9570 /* If it didn't work out, we don't have a TYPE. */
9571 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9572 && !cp_parser_parse_definitely (parser))
9574 if (type && decl_specs)
9575 cp_parser_set_decl_spec_type (decl_specs, type,
9576 /*user_defined=*/true);
9579 /* If we didn't get a type-name, issue an error message. */
9580 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9582 cp_parser_error (parser, "expected type-name");
9583 return error_mark_node;
9586 /* There is no valid C++ program where a non-template type is
9587 followed by a "<". That usually indicates that the user thought
9588 that the type was a template. */
9589 if (type && type != error_mark_node)
9590 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9595 /* Parse a type-name.
9608 Returns a TYPE_DECL for the the type. */
9611 cp_parser_type_name (cp_parser* parser)
9616 /* We can't know yet whether it is a class-name or not. */
9617 cp_parser_parse_tentatively (parser);
9618 /* Try a class-name. */
9619 type_decl = cp_parser_class_name (parser,
9620 /*typename_keyword_p=*/false,
9621 /*template_keyword_p=*/false,
9623 /*check_dependency_p=*/true,
9624 /*class_head_p=*/false,
9625 /*is_declaration=*/false);
9626 /* If it's not a class-name, keep looking. */
9627 if (!cp_parser_parse_definitely (parser))
9629 /* It must be a typedef-name or an enum-name. */
9630 identifier = cp_parser_identifier (parser);
9631 if (identifier == error_mark_node)
9632 return error_mark_node;
9634 /* Look up the type-name. */
9635 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9636 /* Issue an error if we did not find a type-name. */
9637 if (TREE_CODE (type_decl) != TYPE_DECL)
9639 if (!cp_parser_simulate_error (parser))
9640 cp_parser_name_lookup_error (parser, identifier, type_decl,
9642 type_decl = error_mark_node;
9644 /* Remember that the name was used in the definition of the
9645 current class so that we can check later to see if the
9646 meaning would have been different after the class was
9647 entirely defined. */
9648 else if (type_decl != error_mark_node
9650 maybe_note_name_used_in_class (identifier, type_decl);
9657 /* Parse an elaborated-type-specifier. Note that the grammar given
9658 here incorporates the resolution to DR68.
9660 elaborated-type-specifier:
9661 class-key :: [opt] nested-name-specifier [opt] identifier
9662 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9663 enum :: [opt] nested-name-specifier [opt] identifier
9664 typename :: [opt] nested-name-specifier identifier
9665 typename :: [opt] nested-name-specifier template [opt]
9670 elaborated-type-specifier:
9671 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9672 class-key attributes :: [opt] nested-name-specifier [opt]
9673 template [opt] template-id
9674 enum attributes :: [opt] nested-name-specifier [opt] identifier
9676 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9677 declared `friend'. If IS_DECLARATION is TRUE, then this
9678 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9679 something is being declared.
9681 Returns the TYPE specified. */
9684 cp_parser_elaborated_type_specifier (cp_parser* parser,
9686 bool is_declaration)
9688 enum tag_types tag_type;
9690 tree type = NULL_TREE;
9691 tree attributes = NULL_TREE;
9693 /* See if we're looking at the `enum' keyword. */
9694 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9696 /* Consume the `enum' token. */
9697 cp_lexer_consume_token (parser->lexer);
9698 /* Remember that it's an enumeration type. */
9699 tag_type = enum_type;
9700 /* Parse the attributes. */
9701 attributes = cp_parser_attributes_opt (parser);
9703 /* Or, it might be `typename'. */
9704 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9707 /* Consume the `typename' token. */
9708 cp_lexer_consume_token (parser->lexer);
9709 /* Remember that it's a `typename' type. */
9710 tag_type = typename_type;
9711 /* The `typename' keyword is only allowed in templates. */
9712 if (!processing_template_decl)
9713 pedwarn ("using `typename' outside of template");
9715 /* Otherwise it must be a class-key. */
9718 tag_type = cp_parser_class_key (parser);
9719 if (tag_type == none_type)
9720 return error_mark_node;
9721 /* Parse the attributes. */
9722 attributes = cp_parser_attributes_opt (parser);
9725 /* Look for the `::' operator. */
9726 cp_parser_global_scope_opt (parser,
9727 /*current_scope_valid_p=*/false);
9728 /* Look for the nested-name-specifier. */
9729 if (tag_type == typename_type)
9731 if (cp_parser_nested_name_specifier (parser,
9732 /*typename_keyword_p=*/true,
9733 /*check_dependency_p=*/true,
9737 return error_mark_node;
9740 /* Even though `typename' is not present, the proposed resolution
9741 to Core Issue 180 says that in `class A<T>::B', `B' should be
9742 considered a type-name, even if `A<T>' is dependent. */
9743 cp_parser_nested_name_specifier_opt (parser,
9744 /*typename_keyword_p=*/true,
9745 /*check_dependency_p=*/true,
9748 /* For everything but enumeration types, consider a template-id. */
9749 if (tag_type != enum_type)
9751 bool template_p = false;
9754 /* Allow the `template' keyword. */
9755 template_p = cp_parser_optional_template_keyword (parser);
9756 /* If we didn't see `template', we don't know if there's a
9757 template-id or not. */
9759 cp_parser_parse_tentatively (parser);
9760 /* Parse the template-id. */
9761 decl = cp_parser_template_id (parser, template_p,
9762 /*check_dependency_p=*/true,
9764 /* If we didn't find a template-id, look for an ordinary
9766 if (!template_p && !cp_parser_parse_definitely (parser))
9768 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9769 in effect, then we must assume that, upon instantiation, the
9770 template will correspond to a class. */
9771 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9772 && tag_type == typename_type)
9773 type = make_typename_type (parser->scope, decl,
9776 type = TREE_TYPE (decl);
9779 /* For an enumeration type, consider only a plain identifier. */
9782 identifier = cp_parser_identifier (parser);
9784 if (identifier == error_mark_node)
9786 parser->scope = NULL_TREE;
9787 return error_mark_node;
9790 /* For a `typename', we needn't call xref_tag. */
9791 if (tag_type == typename_type)
9792 return cp_parser_make_typename_type (parser, parser->scope,
9794 /* Look up a qualified name in the usual way. */
9799 /* In an elaborated-type-specifier, names are assumed to name
9800 types, so we set IS_TYPE to TRUE when calling
9801 cp_parser_lookup_name. */
9802 decl = cp_parser_lookup_name (parser, identifier,
9804 /*is_template=*/false,
9805 /*is_namespace=*/false,
9806 /*check_dependency=*/true,
9807 /*ambiguous_p=*/NULL);
9809 /* If we are parsing friend declaration, DECL may be a
9810 TEMPLATE_DECL tree node here. However, we need to check
9811 whether this TEMPLATE_DECL results in valid code. Consider
9812 the following example:
9815 template <class T> class C {};
9818 template <class T> friend class N::C; // #1, valid code
9820 template <class T> class Y {
9821 friend class N::C; // #2, invalid code
9824 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9825 name lookup of `N::C'. We see that friend declaration must
9826 be template for the code to be valid. Note that
9827 processing_template_decl does not work here since it is
9828 always 1 for the above two cases. */
9830 decl = (cp_parser_maybe_treat_template_as_class
9831 (decl, /*tag_name_p=*/is_friend
9832 && parser->num_template_parameter_lists));
9834 if (TREE_CODE (decl) != TYPE_DECL)
9836 error ("expected type-name");
9837 return error_mark_node;
9840 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9841 check_elaborated_type_specifier
9843 (parser->num_template_parameter_lists
9844 || DECL_SELF_REFERENCE_P (decl)));
9846 type = TREE_TYPE (decl);
9850 /* An elaborated-type-specifier sometimes introduces a new type and
9851 sometimes names an existing type. Normally, the rule is that it
9852 introduces a new type only if there is not an existing type of
9853 the same name already in scope. For example, given:
9856 void f() { struct S s; }
9858 the `struct S' in the body of `f' is the same `struct S' as in
9859 the global scope; the existing definition is used. However, if
9860 there were no global declaration, this would introduce a new
9861 local class named `S'.
9863 An exception to this rule applies to the following code:
9865 namespace N { struct S; }
9867 Here, the elaborated-type-specifier names a new type
9868 unconditionally; even if there is already an `S' in the
9869 containing scope this declaration names a new type.
9870 This exception only applies if the elaborated-type-specifier
9871 forms the complete declaration:
9875 A declaration consisting solely of `class-key identifier ;' is
9876 either a redeclaration of the name in the current scope or a
9877 forward declaration of the identifier as a class name. It
9878 introduces the name into the current scope.
9880 We are in this situation precisely when the next token is a `;'.
9882 An exception to the exception is that a `friend' declaration does
9883 *not* name a new type; i.e., given:
9885 struct S { friend struct T; };
9887 `T' is not a new type in the scope of `S'.
9889 Also, `new struct S' or `sizeof (struct S)' never results in the
9890 definition of a new type; a new type can only be declared in a
9891 declaration context. */
9893 /* Warn about attributes. They are ignored. */
9895 warning ("type attributes are honored only at type definition");
9897 type = xref_tag (tag_type, identifier,
9900 || cp_lexer_next_token_is_not (parser->lexer,
9902 parser->num_template_parameter_lists);
9905 if (tag_type != enum_type)
9906 cp_parser_check_class_key (tag_type, type);
9908 /* A "<" cannot follow an elaborated type specifier. If that
9909 happens, the user was probably trying to form a template-id. */
9910 cp_parser_check_for_invalid_template_id (parser, type);
9915 /* Parse an enum-specifier.
9918 enum identifier [opt] { enumerator-list [opt] }
9920 Returns an ENUM_TYPE representing the enumeration. */
9923 cp_parser_enum_specifier (cp_parser* parser)
9928 /* Caller guarantees that the current token is 'enum', an identifier
9929 possibly follows, and the token after that is an opening brace.
9930 If we don't have an identifier, fabricate an anonymous name for
9931 the enumeration being defined. */
9932 cp_lexer_consume_token (parser->lexer);
9934 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9935 identifier = cp_parser_identifier (parser);
9937 identifier = make_anon_name ();
9939 cp_lexer_consume_token (parser->lexer);
9941 /* Issue an error message if type-definitions are forbidden here. */
9942 cp_parser_check_type_definition (parser);
9944 /* Create the new type. */
9945 type = start_enum (identifier);
9947 /* If the next token is not '}', then there are some enumerators. */
9948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9949 cp_parser_enumerator_list (parser, type);
9951 /* Consume the final '}'. */
9952 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9954 /* Finish up the enumeration. */
9960 /* Parse an enumerator-list. The enumerators all have the indicated
9964 enumerator-definition
9965 enumerator-list , enumerator-definition */
9968 cp_parser_enumerator_list (cp_parser* parser, tree type)
9972 /* Parse an enumerator-definition. */
9973 cp_parser_enumerator_definition (parser, type);
9975 /* If the next token is not a ',', we've reached the end of
9977 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9979 /* Otherwise, consume the `,' and keep going. */
9980 cp_lexer_consume_token (parser->lexer);
9981 /* If the next token is a `}', there is a trailing comma. */
9982 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9984 if (pedantic && !in_system_header)
9985 pedwarn ("comma at end of enumerator list");
9991 /* Parse an enumerator-definition. The enumerator has the indicated
9994 enumerator-definition:
9996 enumerator = constant-expression
10002 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10007 /* Look for the identifier. */
10008 identifier = cp_parser_identifier (parser);
10009 if (identifier == error_mark_node)
10012 /* If the next token is an '=', then there is an explicit value. */
10013 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10015 /* Consume the `=' token. */
10016 cp_lexer_consume_token (parser->lexer);
10017 /* Parse the value. */
10018 value = cp_parser_constant_expression (parser,
10019 /*allow_non_constant_p=*/false,
10025 /* Create the enumerator. */
10026 build_enumerator (identifier, value, type);
10029 /* Parse a namespace-name.
10032 original-namespace-name
10035 Returns the NAMESPACE_DECL for the namespace. */
10038 cp_parser_namespace_name (cp_parser* parser)
10041 tree namespace_decl;
10043 /* Get the name of the namespace. */
10044 identifier = cp_parser_identifier (parser);
10045 if (identifier == error_mark_node)
10046 return error_mark_node;
10048 /* Look up the identifier in the currently active scope. Look only
10049 for namespaces, due to:
10051 [basic.lookup.udir]
10053 When looking up a namespace-name in a using-directive or alias
10054 definition, only namespace names are considered.
10058 [basic.lookup.qual]
10060 During the lookup of a name preceding the :: scope resolution
10061 operator, object, function, and enumerator names are ignored.
10063 (Note that cp_parser_class_or_namespace_name only calls this
10064 function if the token after the name is the scope resolution
10066 namespace_decl = cp_parser_lookup_name (parser, identifier,
10068 /*is_template=*/false,
10069 /*is_namespace=*/true,
10070 /*check_dependency=*/true,
10071 /*ambiguous_p=*/NULL);
10072 /* If it's not a namespace, issue an error. */
10073 if (namespace_decl == error_mark_node
10074 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10076 cp_parser_error (parser, "expected namespace-name");
10077 namespace_decl = error_mark_node;
10080 return namespace_decl;
10083 /* Parse a namespace-definition.
10085 namespace-definition:
10086 named-namespace-definition
10087 unnamed-namespace-definition
10089 named-namespace-definition:
10090 original-namespace-definition
10091 extension-namespace-definition
10093 original-namespace-definition:
10094 namespace identifier { namespace-body }
10096 extension-namespace-definition:
10097 namespace original-namespace-name { namespace-body }
10099 unnamed-namespace-definition:
10100 namespace { namespace-body } */
10103 cp_parser_namespace_definition (cp_parser* parser)
10107 /* Look for the `namespace' keyword. */
10108 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10110 /* Get the name of the namespace. We do not attempt to distinguish
10111 between an original-namespace-definition and an
10112 extension-namespace-definition at this point. The semantic
10113 analysis routines are responsible for that. */
10114 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10115 identifier = cp_parser_identifier (parser);
10117 identifier = NULL_TREE;
10119 /* Look for the `{' to start the namespace. */
10120 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10121 /* Start the namespace. */
10122 push_namespace (identifier);
10123 /* Parse the body of the namespace. */
10124 cp_parser_namespace_body (parser);
10125 /* Finish the namespace. */
10127 /* Look for the final `}'. */
10128 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10131 /* Parse a namespace-body.
10134 declaration-seq [opt] */
10137 cp_parser_namespace_body (cp_parser* parser)
10139 cp_parser_declaration_seq_opt (parser);
10142 /* Parse a namespace-alias-definition.
10144 namespace-alias-definition:
10145 namespace identifier = qualified-namespace-specifier ; */
10148 cp_parser_namespace_alias_definition (cp_parser* parser)
10151 tree namespace_specifier;
10153 /* Look for the `namespace' keyword. */
10154 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10155 /* Look for the identifier. */
10156 identifier = cp_parser_identifier (parser);
10157 if (identifier == error_mark_node)
10159 /* Look for the `=' token. */
10160 cp_parser_require (parser, CPP_EQ, "`='");
10161 /* Look for the qualified-namespace-specifier. */
10162 namespace_specifier
10163 = cp_parser_qualified_namespace_specifier (parser);
10164 /* Look for the `;' token. */
10165 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10167 /* Register the alias in the symbol table. */
10168 do_namespace_alias (identifier, namespace_specifier);
10171 /* Parse a qualified-namespace-specifier.
10173 qualified-namespace-specifier:
10174 :: [opt] nested-name-specifier [opt] namespace-name
10176 Returns a NAMESPACE_DECL corresponding to the specified
10180 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10182 /* Look for the optional `::'. */
10183 cp_parser_global_scope_opt (parser,
10184 /*current_scope_valid_p=*/false);
10186 /* Look for the optional nested-name-specifier. */
10187 cp_parser_nested_name_specifier_opt (parser,
10188 /*typename_keyword_p=*/false,
10189 /*check_dependency_p=*/true,
10191 /*is_declaration=*/true);
10193 return cp_parser_namespace_name (parser);
10196 /* Parse a using-declaration.
10199 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10200 using :: unqualified-id ; */
10203 cp_parser_using_declaration (cp_parser* parser)
10206 bool typename_p = false;
10207 bool global_scope_p;
10213 /* Look for the `using' keyword. */
10214 cp_parser_require_keyword (parser, RID_USING, "`using'");
10216 /* Peek at the next token. */
10217 token = cp_lexer_peek_token (parser->lexer);
10218 /* See if it's `typename'. */
10219 if (token->keyword == RID_TYPENAME)
10221 /* Remember that we've seen it. */
10223 /* Consume the `typename' token. */
10224 cp_lexer_consume_token (parser->lexer);
10227 /* Look for the optional global scope qualification. */
10229 = (cp_parser_global_scope_opt (parser,
10230 /*current_scope_valid_p=*/false)
10233 /* If we saw `typename', or didn't see `::', then there must be a
10234 nested-name-specifier present. */
10235 if (typename_p || !global_scope_p)
10236 qscope = cp_parser_nested_name_specifier (parser, typename_p,
10237 /*check_dependency_p=*/true,
10239 /*is_declaration=*/true);
10240 /* Otherwise, we could be in either of the two productions. In that
10241 case, treat the nested-name-specifier as optional. */
10243 qscope = cp_parser_nested_name_specifier_opt (parser,
10244 /*typename_keyword_p=*/false,
10245 /*check_dependency_p=*/true,
10247 /*is_declaration=*/true);
10249 qscope = global_namespace;
10251 /* Parse the unqualified-id. */
10252 identifier = cp_parser_unqualified_id (parser,
10253 /*template_keyword_p=*/false,
10254 /*check_dependency_p=*/true,
10255 /*declarator_p=*/true);
10257 /* The function we call to handle a using-declaration is different
10258 depending on what scope we are in. */
10259 if (identifier == error_mark_node)
10261 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10262 && TREE_CODE (identifier) != BIT_NOT_EXPR)
10263 /* [namespace.udecl]
10265 A using declaration shall not name a template-id. */
10266 error ("a template-id may not appear in a using-declaration");
10269 scope = current_scope ();
10270 if (scope && TYPE_P (scope))
10272 /* Create the USING_DECL. */
10273 decl = do_class_using_decl (build_nt (SCOPE_REF,
10276 /* Add it to the list of members in this class. */
10277 finish_member_declaration (decl);
10281 decl = cp_parser_lookup_name_simple (parser, identifier);
10282 if (decl == error_mark_node)
10283 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10285 do_local_using_decl (decl, qscope, identifier);
10287 do_toplevel_using_decl (decl, qscope, identifier);
10291 /* Look for the final `;'. */
10292 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10295 /* Parse a using-directive.
10298 using namespace :: [opt] nested-name-specifier [opt]
10299 namespace-name ; */
10302 cp_parser_using_directive (cp_parser* parser)
10304 tree namespace_decl;
10307 /* Look for the `using' keyword. */
10308 cp_parser_require_keyword (parser, RID_USING, "`using'");
10309 /* And the `namespace' keyword. */
10310 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10311 /* Look for the optional `::' operator. */
10312 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10313 /* And the optional nested-name-specifier. */
10314 cp_parser_nested_name_specifier_opt (parser,
10315 /*typename_keyword_p=*/false,
10316 /*check_dependency_p=*/true,
10318 /*is_declaration=*/true);
10319 /* Get the namespace being used. */
10320 namespace_decl = cp_parser_namespace_name (parser);
10321 /* And any specified attributes. */
10322 attribs = cp_parser_attributes_opt (parser);
10323 /* Update the symbol table. */
10324 parse_using_directive (namespace_decl, attribs);
10325 /* Look for the final `;'. */
10326 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10329 /* Parse an asm-definition.
10332 asm ( string-literal ) ;
10337 asm volatile [opt] ( string-literal ) ;
10338 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10339 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10340 : asm-operand-list [opt] ) ;
10341 asm volatile [opt] ( string-literal : asm-operand-list [opt]
10342 : asm-operand-list [opt]
10343 : asm-operand-list [opt] ) ; */
10346 cp_parser_asm_definition (cp_parser* parser)
10350 tree outputs = NULL_TREE;
10351 tree inputs = NULL_TREE;
10352 tree clobbers = NULL_TREE;
10354 bool volatile_p = false;
10355 bool extended_p = false;
10357 /* Look for the `asm' keyword. */
10358 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10359 /* See if the next token is `volatile'. */
10360 if (cp_parser_allow_gnu_extensions_p (parser)
10361 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10363 /* Remember that we saw the `volatile' keyword. */
10365 /* Consume the token. */
10366 cp_lexer_consume_token (parser->lexer);
10368 /* Look for the opening `('. */
10369 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
10370 /* Look for the string. */
10371 c_lex_string_translate = 0;
10372 token = cp_parser_require (parser, CPP_STRING, "asm body");
10375 string = token->value;
10376 /* If we're allowing GNU extensions, check for the extended assembly
10377 syntax. Unfortunately, the `:' tokens need not be separated by
10378 a space in C, and so, for compatibility, we tolerate that here
10379 too. Doing that means that we have to treat the `::' operator as
10381 if (cp_parser_allow_gnu_extensions_p (parser)
10382 && at_function_scope_p ()
10383 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10384 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10386 bool inputs_p = false;
10387 bool clobbers_p = false;
10389 /* The extended syntax was used. */
10392 /* Look for outputs. */
10393 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10395 /* Consume the `:'. */
10396 cp_lexer_consume_token (parser->lexer);
10397 /* Parse the output-operands. */
10398 if (cp_lexer_next_token_is_not (parser->lexer,
10400 && cp_lexer_next_token_is_not (parser->lexer,
10402 && cp_lexer_next_token_is_not (parser->lexer,
10404 outputs = cp_parser_asm_operand_list (parser);
10406 /* If the next token is `::', there are no outputs, and the
10407 next token is the beginning of the inputs. */
10408 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10409 /* The inputs are coming next. */
10412 /* Look for inputs. */
10414 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10416 /* Consume the `:' or `::'. */
10417 cp_lexer_consume_token (parser->lexer);
10418 /* Parse the output-operands. */
10419 if (cp_lexer_next_token_is_not (parser->lexer,
10421 && cp_lexer_next_token_is_not (parser->lexer,
10423 inputs = cp_parser_asm_operand_list (parser);
10425 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10426 /* The clobbers are coming next. */
10429 /* Look for clobbers. */
10431 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10433 /* Consume the `:' or `::'. */
10434 cp_lexer_consume_token (parser->lexer);
10435 /* Parse the clobbers. */
10436 if (cp_lexer_next_token_is_not (parser->lexer,
10438 clobbers = cp_parser_asm_clobber_list (parser);
10441 /* Look for the closing `)'. */
10442 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10443 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10444 /*consume_paren=*/true);
10445 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10447 /* Create the ASM_EXPR. */
10448 if (at_function_scope_p ())
10450 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10452 /* If the extended syntax was not used, mark the ASM_EXPR. */
10454 ASM_INPUT_P (asm_stmt) = 1;
10457 assemble_asm (string);
10460 c_lex_string_translate = 1;
10463 /* Declarators [gram.dcl.decl] */
10465 /* Parse an init-declarator.
10468 declarator initializer [opt]
10473 declarator asm-specification [opt] attributes [opt] initializer [opt]
10475 function-definition:
10476 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10478 decl-specifier-seq [opt] declarator function-try-block
10482 function-definition:
10483 __extension__ function-definition
10485 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10486 Returns a representation of the entity declared. If MEMBER_P is TRUE,
10487 then this declarator appears in a class scope. The new DECL created
10488 by this declarator is returned.
10490 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10491 for a function-definition here as well. If the declarator is a
10492 declarator for a function-definition, *FUNCTION_DEFINITION_P will
10493 be TRUE upon return. By that point, the function-definition will
10494 have been completely parsed.
10496 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10500 cp_parser_init_declarator (cp_parser* parser,
10501 cp_decl_specifier_seq *decl_specifiers,
10502 bool function_definition_allowed_p,
10504 int declares_class_or_enum,
10505 bool* function_definition_p)
10508 cp_declarator *declarator;
10509 tree prefix_attributes;
10511 tree asm_specification;
10513 tree decl = NULL_TREE;
10515 bool is_initialized;
10516 bool is_parenthesized_init;
10517 bool is_non_constant_init;
10518 int ctor_dtor_or_conv_p;
10520 bool pop_p = false;
10522 /* Gather the attributes that were provided with the
10523 decl-specifiers. */
10524 prefix_attributes = decl_specifiers->attributes;
10526 /* Assume that this is not the declarator for a function
10528 if (function_definition_p)
10529 *function_definition_p = false;
10531 /* Defer access checks while parsing the declarator; we cannot know
10532 what names are accessible until we know what is being
10534 resume_deferring_access_checks ();
10536 /* Parse the declarator. */
10538 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10539 &ctor_dtor_or_conv_p,
10540 /*parenthesized_p=*/NULL);
10541 /* Gather up the deferred checks. */
10542 stop_deferring_access_checks ();
10544 /* If the DECLARATOR was erroneous, there's no need to go
10546 if (declarator == cp_error_declarator)
10547 return error_mark_node;
10549 cp_parser_check_for_definition_in_return_type (declarator,
10550 declares_class_or_enum);
10552 /* Figure out what scope the entity declared by the DECLARATOR is
10553 located in. `grokdeclarator' sometimes changes the scope, so
10554 we compute it now. */
10555 scope = get_scope_of_declarator (declarator);
10557 /* If we're allowing GNU extensions, look for an asm-specification
10559 if (cp_parser_allow_gnu_extensions_p (parser))
10561 /* Look for an asm-specification. */
10562 asm_specification = cp_parser_asm_specification_opt (parser);
10563 /* And attributes. */
10564 attributes = cp_parser_attributes_opt (parser);
10568 asm_specification = NULL_TREE;
10569 attributes = NULL_TREE;
10572 /* Peek at the next token. */
10573 token = cp_lexer_peek_token (parser->lexer);
10574 /* Check to see if the token indicates the start of a
10575 function-definition. */
10576 if (cp_parser_token_starts_function_definition_p (token))
10578 if (!function_definition_allowed_p)
10580 /* If a function-definition should not appear here, issue an
10582 cp_parser_error (parser,
10583 "a function-definition is not allowed here");
10584 return error_mark_node;
10588 /* Neither attributes nor an asm-specification are allowed
10589 on a function-definition. */
10590 if (asm_specification)
10591 error ("an asm-specification is not allowed on a function-definition");
10593 error ("attributes are not allowed on a function-definition");
10594 /* This is a function-definition. */
10595 *function_definition_p = true;
10597 /* Parse the function definition. */
10599 decl = cp_parser_save_member_function_body (parser,
10602 prefix_attributes);
10605 = (cp_parser_function_definition_from_specifiers_and_declarator
10606 (parser, decl_specifiers, prefix_attributes, declarator));
10614 Only in function declarations for constructors, destructors, and
10615 type conversions can the decl-specifier-seq be omitted.
10617 We explicitly postpone this check past the point where we handle
10618 function-definitions because we tolerate function-definitions
10619 that are missing their return types in some modes. */
10620 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10622 cp_parser_error (parser,
10623 "expected constructor, destructor, or type conversion");
10624 return error_mark_node;
10627 /* An `=' or an `(' indicates an initializer. */
10628 is_initialized = (token->type == CPP_EQ
10629 || token->type == CPP_OPEN_PAREN);
10630 /* If the init-declarator isn't initialized and isn't followed by a
10631 `,' or `;', it's not a valid init-declarator. */
10632 if (!is_initialized
10633 && token->type != CPP_COMMA
10634 && token->type != CPP_SEMICOLON)
10636 cp_parser_error (parser, "expected init-declarator");
10637 return error_mark_node;
10640 /* Because start_decl has side-effects, we should only call it if we
10641 know we're going ahead. By this point, we know that we cannot
10642 possibly be looking at any other construct. */
10643 cp_parser_commit_to_tentative_parse (parser);
10645 /* If the decl specifiers were bad, issue an error now that we're
10646 sure this was intended to be a declarator. Then continue
10647 declaring the variable(s), as int, to try to cut down on further
10649 if (decl_specifiers->any_specifiers_p
10650 && decl_specifiers->type == error_mark_node)
10652 cp_parser_error (parser, "invalid type in declaration");
10653 decl_specifiers->type = integer_type_node;
10656 /* Check to see whether or not this declaration is a friend. */
10657 friend_p = cp_parser_friend_p (decl_specifiers);
10659 /* Check that the number of template-parameter-lists is OK. */
10660 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10661 return error_mark_node;
10663 /* Enter the newly declared entry in the symbol table. If we're
10664 processing a declaration in a class-specifier, we wait until
10665 after processing the initializer. */
10668 if (parser->in_unbraced_linkage_specification_p)
10670 decl_specifiers->storage_class = sc_extern;
10671 have_extern_spec = false;
10673 decl = start_decl (declarator, decl_specifiers,
10674 is_initialized, attributes, prefix_attributes,
10678 /* Enter the SCOPE. That way unqualified names appearing in the
10679 initializer will be looked up in SCOPE. */
10680 pop_p = push_scope (scope);
10682 /* Perform deferred access control checks, now that we know in which
10683 SCOPE the declared entity resides. */
10684 if (!member_p && decl)
10686 tree saved_current_function_decl = NULL_TREE;
10688 /* If the entity being declared is a function, pretend that we
10689 are in its scope. If it is a `friend', it may have access to
10690 things that would not otherwise be accessible. */
10691 if (TREE_CODE (decl) == FUNCTION_DECL)
10693 saved_current_function_decl = current_function_decl;
10694 current_function_decl = decl;
10697 /* Perform the access control checks for the declarator and the
10698 the decl-specifiers. */
10699 perform_deferred_access_checks ();
10701 /* Restore the saved value. */
10702 if (TREE_CODE (decl) == FUNCTION_DECL)
10703 current_function_decl = saved_current_function_decl;
10706 /* Parse the initializer. */
10707 if (is_initialized)
10708 initializer = cp_parser_initializer (parser,
10709 &is_parenthesized_init,
10710 &is_non_constant_init);
10713 initializer = NULL_TREE;
10714 is_parenthesized_init = false;
10715 is_non_constant_init = true;
10718 /* The old parser allows attributes to appear after a parenthesized
10719 initializer. Mark Mitchell proposed removing this functionality
10720 on the GCC mailing lists on 2002-08-13. This parser accepts the
10721 attributes -- but ignores them. */
10722 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10723 if (cp_parser_attributes_opt (parser))
10724 warning ("attributes after parenthesized initializer ignored");
10726 /* For an in-class declaration, use `grokfield' to create the
10732 decl = grokfield (declarator, decl_specifiers,
10733 initializer, /*asmspec=*/NULL_TREE,
10734 /*attributes=*/NULL_TREE);
10735 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10736 cp_parser_save_default_args (parser, decl);
10739 /* Finish processing the declaration. But, skip friend
10741 if (!friend_p && decl && decl != error_mark_node)
10743 cp_finish_decl (decl,
10746 /* If the initializer is in parentheses, then this is
10747 a direct-initialization, which means that an
10748 `explicit' constructor is OK. Otherwise, an
10749 `explicit' constructor cannot be used. */
10750 ((is_parenthesized_init || !is_initialized)
10751 ? 0 : LOOKUP_ONLYCONVERTING));
10753 pop_scope (DECL_CONTEXT (decl));
10756 /* Remember whether or not variables were initialized by
10757 constant-expressions. */
10758 if (decl && TREE_CODE (decl) == VAR_DECL
10759 && is_initialized && !is_non_constant_init)
10760 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10765 /* Parse a declarator.
10769 ptr-operator declarator
10771 abstract-declarator:
10772 ptr-operator abstract-declarator [opt]
10773 direct-abstract-declarator
10778 attributes [opt] direct-declarator
10779 attributes [opt] ptr-operator declarator
10781 abstract-declarator:
10782 attributes [opt] ptr-operator abstract-declarator [opt]
10783 attributes [opt] direct-abstract-declarator
10785 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10786 detect constructor, destructor or conversion operators. It is set
10787 to -1 if the declarator is a name, and +1 if it is a
10788 function. Otherwise it is set to zero. Usually you just want to
10789 test for >0, but internally the negative value is used.
10791 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10792 a decl-specifier-seq unless it declares a constructor, destructor,
10793 or conversion. It might seem that we could check this condition in
10794 semantic analysis, rather than parsing, but that makes it difficult
10795 to handle something like `f()'. We want to notice that there are
10796 no decl-specifiers, and therefore realize that this is an
10797 expression, not a declaration.)
10799 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10800 the declarator is a direct-declarator of the form "(...)". */
10802 static cp_declarator *
10803 cp_parser_declarator (cp_parser* parser,
10804 cp_parser_declarator_kind dcl_kind,
10805 int* ctor_dtor_or_conv_p,
10806 bool* parenthesized_p)
10809 cp_declarator *declarator;
10810 enum tree_code code;
10811 cp_cv_quals cv_quals;
10813 tree attributes = NULL_TREE;
10815 /* Assume this is not a constructor, destructor, or type-conversion
10817 if (ctor_dtor_or_conv_p)
10818 *ctor_dtor_or_conv_p = 0;
10820 if (cp_parser_allow_gnu_extensions_p (parser))
10821 attributes = cp_parser_attributes_opt (parser);
10823 /* Peek at the next token. */
10824 token = cp_lexer_peek_token (parser->lexer);
10826 /* Check for the ptr-operator production. */
10827 cp_parser_parse_tentatively (parser);
10828 /* Parse the ptr-operator. */
10829 code = cp_parser_ptr_operator (parser,
10832 /* If that worked, then we have a ptr-operator. */
10833 if (cp_parser_parse_definitely (parser))
10835 /* If a ptr-operator was found, then this declarator was not
10837 if (parenthesized_p)
10838 *parenthesized_p = true;
10839 /* The dependent declarator is optional if we are parsing an
10840 abstract-declarator. */
10841 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10842 cp_parser_parse_tentatively (parser);
10844 /* Parse the dependent declarator. */
10845 declarator = cp_parser_declarator (parser, dcl_kind,
10846 /*ctor_dtor_or_conv_p=*/NULL,
10847 /*parenthesized_p=*/NULL);
10849 /* If we are parsing an abstract-declarator, we must handle the
10850 case where the dependent declarator is absent. */
10851 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10852 && !cp_parser_parse_definitely (parser))
10855 /* Build the representation of the ptr-operator. */
10857 declarator = make_ptrmem_declarator (cv_quals,
10860 else if (code == INDIRECT_REF)
10861 declarator = make_pointer_declarator (cv_quals, declarator);
10863 declarator = make_reference_declarator (cv_quals, declarator);
10865 /* Everything else is a direct-declarator. */
10868 if (parenthesized_p)
10869 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10871 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10872 ctor_dtor_or_conv_p);
10875 if (attributes && declarator != cp_error_declarator)
10876 declarator->attributes = attributes;
10881 /* Parse a direct-declarator or direct-abstract-declarator.
10885 direct-declarator ( parameter-declaration-clause )
10886 cv-qualifier-seq [opt]
10887 exception-specification [opt]
10888 direct-declarator [ constant-expression [opt] ]
10891 direct-abstract-declarator:
10892 direct-abstract-declarator [opt]
10893 ( parameter-declaration-clause )
10894 cv-qualifier-seq [opt]
10895 exception-specification [opt]
10896 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10897 ( abstract-declarator )
10899 Returns a representation of the declarator. DCL_KIND is
10900 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10901 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10902 we are parsing a direct-declarator. It is
10903 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10904 of ambiguity we prefer an abstract declarator, as per
10905 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10906 cp_parser_declarator. */
10908 static cp_declarator *
10909 cp_parser_direct_declarator (cp_parser* parser,
10910 cp_parser_declarator_kind dcl_kind,
10911 int* ctor_dtor_or_conv_p)
10914 cp_declarator *declarator = NULL;
10915 tree scope = NULL_TREE;
10916 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10917 bool saved_in_declarator_p = parser->in_declarator_p;
10919 bool pop_p = false;
10923 /* Peek at the next token. */
10924 token = cp_lexer_peek_token (parser->lexer);
10925 if (token->type == CPP_OPEN_PAREN)
10927 /* This is either a parameter-declaration-clause, or a
10928 parenthesized declarator. When we know we are parsing a
10929 named declarator, it must be a parenthesized declarator
10930 if FIRST is true. For instance, `(int)' is a
10931 parameter-declaration-clause, with an omitted
10932 direct-abstract-declarator. But `((*))', is a
10933 parenthesized abstract declarator. Finally, when T is a
10934 template parameter `(T)' is a
10935 parameter-declaration-clause, and not a parenthesized
10938 We first try and parse a parameter-declaration-clause,
10939 and then try a nested declarator (if FIRST is true).
10941 It is not an error for it not to be a
10942 parameter-declaration-clause, even when FIRST is
10948 The first is the declaration of a function while the
10949 second is a the definition of a variable, including its
10952 Having seen only the parenthesis, we cannot know which of
10953 these two alternatives should be selected. Even more
10954 complex are examples like:
10959 The former is a function-declaration; the latter is a
10960 variable initialization.
10962 Thus again, we try a parameter-declaration-clause, and if
10963 that fails, we back out and return. */
10965 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10967 cp_parameter_declarator *params;
10968 unsigned saved_num_template_parameter_lists;
10970 cp_parser_parse_tentatively (parser);
10972 /* Consume the `('. */
10973 cp_lexer_consume_token (parser->lexer);
10976 /* If this is going to be an abstract declarator, we're
10977 in a declarator and we can't have default args. */
10978 parser->default_arg_ok_p = false;
10979 parser->in_declarator_p = true;
10982 /* Inside the function parameter list, surrounding
10983 template-parameter-lists do not apply. */
10984 saved_num_template_parameter_lists
10985 = parser->num_template_parameter_lists;
10986 parser->num_template_parameter_lists = 0;
10988 /* Parse the parameter-declaration-clause. */
10989 params = cp_parser_parameter_declaration_clause (parser);
10991 parser->num_template_parameter_lists
10992 = saved_num_template_parameter_lists;
10994 /* If all went well, parse the cv-qualifier-seq and the
10995 exception-specification. */
10996 if (cp_parser_parse_definitely (parser))
10998 cp_cv_quals cv_quals;
10999 tree exception_specification;
11001 if (ctor_dtor_or_conv_p)
11002 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11004 /* Consume the `)'. */
11005 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11007 /* Parse the cv-qualifier-seq. */
11008 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11009 /* And the exception-specification. */
11010 exception_specification
11011 = cp_parser_exception_specification_opt (parser);
11013 /* Create the function-declarator. */
11014 declarator = make_call_declarator (declarator,
11017 exception_specification);
11018 /* Any subsequent parameter lists are to do with
11019 return type, so are not those of the declared
11021 parser->default_arg_ok_p = false;
11023 /* Repeat the main loop. */
11028 /* If this is the first, we can try a parenthesized
11032 bool saved_in_type_id_in_expr_p;
11034 parser->default_arg_ok_p = saved_default_arg_ok_p;
11035 parser->in_declarator_p = saved_in_declarator_p;
11037 /* Consume the `('. */
11038 cp_lexer_consume_token (parser->lexer);
11039 /* Parse the nested declarator. */
11040 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11041 parser->in_type_id_in_expr_p = true;
11043 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11044 /*parenthesized_p=*/NULL);
11045 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11047 /* Expect a `)'. */
11048 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11049 declarator = cp_error_declarator;
11050 if (declarator == cp_error_declarator)
11053 goto handle_declarator;
11055 /* Otherwise, we must be done. */
11059 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11060 && token->type == CPP_OPEN_SQUARE)
11062 /* Parse an array-declarator. */
11065 if (ctor_dtor_or_conv_p)
11066 *ctor_dtor_or_conv_p = 0;
11069 parser->default_arg_ok_p = false;
11070 parser->in_declarator_p = true;
11071 /* Consume the `['. */
11072 cp_lexer_consume_token (parser->lexer);
11073 /* Peek at the next token. */
11074 token = cp_lexer_peek_token (parser->lexer);
11075 /* If the next token is `]', then there is no
11076 constant-expression. */
11077 if (token->type != CPP_CLOSE_SQUARE)
11079 bool non_constant_p;
11082 = cp_parser_constant_expression (parser,
11083 /*allow_non_constant=*/true,
11085 if (!non_constant_p)
11086 bounds = fold_non_dependent_expr (bounds);
11089 bounds = NULL_TREE;
11090 /* Look for the closing `]'. */
11091 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11093 declarator = cp_error_declarator;
11097 declarator = make_array_declarator (declarator, bounds);
11099 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11103 /* Parse a declarator-id */
11104 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11105 cp_parser_parse_tentatively (parser);
11106 id = cp_parser_declarator_id (parser);
11107 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11109 if (!cp_parser_parse_definitely (parser))
11110 id = error_mark_node;
11111 else if (TREE_CODE (id) != IDENTIFIER_NODE)
11113 cp_parser_error (parser, "expected unqualified-id");
11114 id = error_mark_node;
11118 if (id == error_mark_node)
11120 declarator = cp_error_declarator;
11124 if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11126 tree scope = TREE_OPERAND (id, 0);
11128 /* In the declaration of a member of a template class
11129 outside of the class itself, the SCOPE will sometimes
11130 be a TYPENAME_TYPE. For example, given:
11132 template <typename T>
11133 int S<T>::R::i = 3;
11135 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
11136 this context, we must resolve S<T>::R to an ordinary
11137 type, rather than a typename type.
11139 The reason we normally avoid resolving TYPENAME_TYPEs
11140 is that a specialization of `S' might render
11141 `S<T>::R' not a type. However, if `S' is
11142 specialized, then this `i' will not be used, so there
11143 is no harm in resolving the types here. */
11144 if (TREE_CODE (scope) == TYPENAME_TYPE)
11148 /* Resolve the TYPENAME_TYPE. */
11149 type = resolve_typename_type (scope,
11150 /*only_current_p=*/false);
11151 /* If that failed, the declarator is invalid. */
11152 if (type == error_mark_node)
11153 error ("`%T::%D' is not a type",
11154 TYPE_CONTEXT (scope),
11155 TYPE_IDENTIFIER (scope));
11156 /* Build a new DECLARATOR. */
11157 id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11161 declarator = make_id_declarator (id);
11165 tree unqualified_name;
11167 if (TREE_CODE (id) == SCOPE_REF
11168 && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11170 class_type = TREE_OPERAND (id, 0);
11171 unqualified_name = TREE_OPERAND (id, 1);
11175 class_type = current_class_type;
11176 unqualified_name = id;
11181 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11182 declarator->u.id.sfk = sfk_destructor;
11183 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11184 declarator->u.id.sfk = sfk_conversion;
11185 else if (constructor_name_p (unqualified_name,
11187 || (TREE_CODE (unqualified_name) == TYPE_DECL
11188 && same_type_p (TREE_TYPE (unqualified_name),
11190 declarator->u.id.sfk = sfk_constructor;
11192 if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11193 *ctor_dtor_or_conv_p = -1;
11194 if (TREE_CODE (id) == SCOPE_REF
11195 && TREE_CODE (unqualified_name) == TYPE_DECL
11196 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11198 error ("invalid use of constructor as a template");
11199 inform ("use `%T::%D' instead of `%T::%T' to name the "
11200 "constructor in a qualified name", class_type,
11201 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11202 class_type, class_type);
11207 handle_declarator:;
11208 scope = get_scope_of_declarator (declarator);
11210 /* Any names that appear after the declarator-id for a
11211 member are looked up in the containing scope. */
11212 pop_p = push_scope (scope);
11213 parser->in_declarator_p = true;
11214 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11215 || (declarator && declarator->kind == cdk_id))
11216 /* Default args are only allowed on function
11218 parser->default_arg_ok_p = saved_default_arg_ok_p;
11220 parser->default_arg_ok_p = false;
11229 /* For an abstract declarator, we might wind up with nothing at this
11230 point. That's an error; the declarator is not optional. */
11232 cp_parser_error (parser, "expected declarator");
11234 /* If we entered a scope, we must exit it now. */
11238 parser->default_arg_ok_p = saved_default_arg_ok_p;
11239 parser->in_declarator_p = saved_in_declarator_p;
11244 /* Parse a ptr-operator.
11247 * cv-qualifier-seq [opt]
11249 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11254 & cv-qualifier-seq [opt]
11256 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11257 Returns ADDR_EXPR if a reference was used. In the case of a
11258 pointer-to-member, *TYPE is filled in with the TYPE containing the
11259 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
11260 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
11261 ERROR_MARK if an error occurred. */
11263 static enum tree_code
11264 cp_parser_ptr_operator (cp_parser* parser,
11266 cp_cv_quals *cv_quals)
11268 enum tree_code code = ERROR_MARK;
11271 /* Assume that it's not a pointer-to-member. */
11273 /* And that there are no cv-qualifiers. */
11274 *cv_quals = TYPE_UNQUALIFIED;
11276 /* Peek at the next token. */
11277 token = cp_lexer_peek_token (parser->lexer);
11278 /* If it's a `*' or `&' we have a pointer or reference. */
11279 if (token->type == CPP_MULT || token->type == CPP_AND)
11281 /* Remember which ptr-operator we were processing. */
11282 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11284 /* Consume the `*' or `&'. */
11285 cp_lexer_consume_token (parser->lexer);
11287 /* A `*' can be followed by a cv-qualifier-seq, and so can a
11288 `&', if we are allowing GNU extensions. (The only qualifier
11289 that can legally appear after `&' is `restrict', but that is
11290 enforced during semantic analysis. */
11291 if (code == INDIRECT_REF
11292 || cp_parser_allow_gnu_extensions_p (parser))
11293 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11297 /* Try the pointer-to-member case. */
11298 cp_parser_parse_tentatively (parser);
11299 /* Look for the optional `::' operator. */
11300 cp_parser_global_scope_opt (parser,
11301 /*current_scope_valid_p=*/false);
11302 /* Look for the nested-name specifier. */
11303 cp_parser_nested_name_specifier (parser,
11304 /*typename_keyword_p=*/false,
11305 /*check_dependency_p=*/true,
11307 /*is_declaration=*/false);
11308 /* If we found it, and the next token is a `*', then we are
11309 indeed looking at a pointer-to-member operator. */
11310 if (!cp_parser_error_occurred (parser)
11311 && cp_parser_require (parser, CPP_MULT, "`*'"))
11313 /* The type of which the member is a member is given by the
11315 *type = parser->scope;
11316 /* The next name will not be qualified. */
11317 parser->scope = NULL_TREE;
11318 parser->qualifying_scope = NULL_TREE;
11319 parser->object_scope = NULL_TREE;
11320 /* Indicate that the `*' operator was used. */
11321 code = INDIRECT_REF;
11322 /* Look for the optional cv-qualifier-seq. */
11323 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11325 /* If that didn't work we don't have a ptr-operator. */
11326 if (!cp_parser_parse_definitely (parser))
11327 cp_parser_error (parser, "expected ptr-operator");
11333 /* Parse an (optional) cv-qualifier-seq.
11336 cv-qualifier cv-qualifier-seq [opt]
11347 Returns a bitmask representing the cv-qualifiers. */
11350 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11352 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11357 cp_cv_quals cv_qualifier;
11359 /* Peek at the next token. */
11360 token = cp_lexer_peek_token (parser->lexer);
11361 /* See if it's a cv-qualifier. */
11362 switch (token->keyword)
11365 cv_qualifier = TYPE_QUAL_CONST;
11369 cv_qualifier = TYPE_QUAL_VOLATILE;
11373 cv_qualifier = TYPE_QUAL_RESTRICT;
11377 cv_qualifier = TYPE_UNQUALIFIED;
11384 if (cv_quals & cv_qualifier)
11386 error ("duplicate cv-qualifier");
11387 cp_lexer_purge_token (parser->lexer);
11391 cp_lexer_consume_token (parser->lexer);
11392 cv_quals |= cv_qualifier;
11399 /* Parse a declarator-id.
11403 :: [opt] nested-name-specifier [opt] type-name
11405 In the `id-expression' case, the value returned is as for
11406 cp_parser_id_expression if the id-expression was an unqualified-id.
11407 If the id-expression was a qualified-id, then a SCOPE_REF is
11408 returned. The first operand is the scope (either a NAMESPACE_DECL
11409 or TREE_TYPE), but the second is still just a representation of an
11413 cp_parser_declarator_id (cp_parser* parser)
11415 tree id_expression;
11417 /* The expression must be an id-expression. Assume that qualified
11418 names are the names of types so that:
11421 int S<T>::R::i = 3;
11423 will work; we must treat `S<T>::R' as the name of a type.
11424 Similarly, assume that qualified names are templates, where
11428 int S<T>::R<T>::i = 3;
11431 id_expression = cp_parser_id_expression (parser,
11432 /*template_keyword_p=*/false,
11433 /*check_dependency_p=*/false,
11434 /*template_p=*/NULL,
11435 /*declarator_p=*/true);
11436 /* If the name was qualified, create a SCOPE_REF to represent
11440 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11441 parser->scope = NULL_TREE;
11444 return id_expression;
11447 /* Parse a type-id.
11450 type-specifier-seq abstract-declarator [opt]
11452 Returns the TYPE specified. */
11455 cp_parser_type_id (cp_parser* parser)
11457 cp_decl_specifier_seq type_specifier_seq;
11458 cp_declarator *abstract_declarator;
11460 /* Parse the type-specifier-seq. */
11461 cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11462 if (type_specifier_seq.type == error_mark_node)
11463 return error_mark_node;
11465 /* There might or might not be an abstract declarator. */
11466 cp_parser_parse_tentatively (parser);
11467 /* Look for the declarator. */
11468 abstract_declarator
11469 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11470 /*parenthesized_p=*/NULL);
11471 /* Check to see if there really was a declarator. */
11472 if (!cp_parser_parse_definitely (parser))
11473 abstract_declarator = NULL;
11475 return groktypename (&type_specifier_seq, abstract_declarator);
11478 /* Parse a type-specifier-seq.
11480 type-specifier-seq:
11481 type-specifier type-specifier-seq [opt]
11485 type-specifier-seq:
11486 attributes type-specifier-seq [opt]
11488 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
11491 cp_parser_type_specifier_seq (cp_parser* parser,
11492 cp_decl_specifier_seq *type_specifier_seq)
11494 bool seen_type_specifier = false;
11496 /* Clear the TYPE_SPECIFIER_SEQ. */
11497 clear_decl_specs (type_specifier_seq);
11499 /* Parse the type-specifiers and attributes. */
11502 tree type_specifier;
11504 /* Check for attributes first. */
11505 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11507 type_specifier_seq->attributes =
11508 chainon (type_specifier_seq->attributes,
11509 cp_parser_attributes_opt (parser));
11513 /* Look for the type-specifier. */
11514 type_specifier = cp_parser_type_specifier (parser,
11515 CP_PARSER_FLAGS_OPTIONAL,
11516 type_specifier_seq,
11517 /*is_declaration=*/false,
11520 /* If the first type-specifier could not be found, this is not a
11521 type-specifier-seq at all. */
11522 if (!seen_type_specifier && !type_specifier)
11524 cp_parser_error (parser, "expected type-specifier");
11525 type_specifier_seq->type = error_mark_node;
11528 /* If subsequent type-specifiers could not be found, the
11529 type-specifier-seq is complete. */
11530 else if (seen_type_specifier && !type_specifier)
11533 seen_type_specifier = true;
11539 /* Parse a parameter-declaration-clause.
11541 parameter-declaration-clause:
11542 parameter-declaration-list [opt] ... [opt]
11543 parameter-declaration-list , ...
11545 Returns a representation for the parameter declarations. A return
11546 value of NULL indicates a parameter-declaration-clause consisting
11547 only of an ellipsis. */
11549 static cp_parameter_declarator *
11550 cp_parser_parameter_declaration_clause (cp_parser* parser)
11552 cp_parameter_declarator *parameters;
11557 /* Peek at the next token. */
11558 token = cp_lexer_peek_token (parser->lexer);
11559 /* Check for trivial parameter-declaration-clauses. */
11560 if (token->type == CPP_ELLIPSIS)
11562 /* Consume the `...' token. */
11563 cp_lexer_consume_token (parser->lexer);
11566 else if (token->type == CPP_CLOSE_PAREN)
11567 /* There are no parameters. */
11569 #ifndef NO_IMPLICIT_EXTERN_C
11570 if (in_system_header && current_class_type == NULL
11571 && current_lang_name == lang_name_c)
11575 return no_parameters;
11577 /* Check for `(void)', too, which is a special case. */
11578 else if (token->keyword == RID_VOID
11579 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11580 == CPP_CLOSE_PAREN))
11582 /* Consume the `void' token. */
11583 cp_lexer_consume_token (parser->lexer);
11584 /* There are no parameters. */
11585 return no_parameters;
11588 /* Parse the parameter-declaration-list. */
11589 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11590 /* If a parse error occurred while parsing the
11591 parameter-declaration-list, then the entire
11592 parameter-declaration-clause is erroneous. */
11596 /* Peek at the next token. */
11597 token = cp_lexer_peek_token (parser->lexer);
11598 /* If it's a `,', the clause should terminate with an ellipsis. */
11599 if (token->type == CPP_COMMA)
11601 /* Consume the `,'. */
11602 cp_lexer_consume_token (parser->lexer);
11603 /* Expect an ellipsis. */
11605 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11607 /* It might also be `...' if the optional trailing `,' was
11609 else if (token->type == CPP_ELLIPSIS)
11611 /* Consume the `...' token. */
11612 cp_lexer_consume_token (parser->lexer);
11613 /* And remember that we saw it. */
11617 ellipsis_p = false;
11619 /* Finish the parameter list. */
11620 if (parameters && ellipsis_p)
11621 parameters->ellipsis_p = true;
11626 /* Parse a parameter-declaration-list.
11628 parameter-declaration-list:
11629 parameter-declaration
11630 parameter-declaration-list , parameter-declaration
11632 Returns a representation of the parameter-declaration-list, as for
11633 cp_parser_parameter_declaration_clause. However, the
11634 `void_list_node' is never appended to the list. Upon return,
11635 *IS_ERROR will be true iff an error occurred. */
11637 static cp_parameter_declarator *
11638 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11640 cp_parameter_declarator *parameters = NULL;
11641 cp_parameter_declarator **tail = ¶meters;
11643 /* Assume all will go well. */
11646 /* Look for more parameters. */
11649 cp_parameter_declarator *parameter;
11650 bool parenthesized_p;
11651 /* Parse the parameter. */
11653 = cp_parser_parameter_declaration (parser,
11654 /*template_parm_p=*/false,
11657 /* If a parse error occurred parsing the parameter declaration,
11658 then the entire parameter-declaration-list is erroneous. */
11665 /* Add the new parameter to the list. */
11667 tail = ¶meter->next;
11669 /* Peek at the next token. */
11670 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11671 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11672 /* The parameter-declaration-list is complete. */
11674 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11678 /* Peek at the next token. */
11679 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11680 /* If it's an ellipsis, then the list is complete. */
11681 if (token->type == CPP_ELLIPSIS)
11683 /* Otherwise, there must be more parameters. Consume the
11685 cp_lexer_consume_token (parser->lexer);
11686 /* When parsing something like:
11688 int i(float f, double d)
11690 we can tell after seeing the declaration for "f" that we
11691 are not looking at an initialization of a variable "i",
11692 but rather at the declaration of a function "i".
11694 Due to the fact that the parsing of template arguments
11695 (as specified to a template-id) requires backtracking we
11696 cannot use this technique when inside a template argument
11698 if (!parser->in_template_argument_list_p
11699 && !parser->in_type_id_in_expr_p
11700 && cp_parser_parsing_tentatively (parser)
11701 && !cp_parser_committed_to_tentative_parse (parser)
11702 /* However, a parameter-declaration of the form
11703 "foat(f)" (which is a valid declaration of a
11704 parameter "f") can also be interpreted as an
11705 expression (the conversion of "f" to "float"). */
11706 && !parenthesized_p)
11707 cp_parser_commit_to_tentative_parse (parser);
11711 cp_parser_error (parser, "expected `,' or `...'");
11712 if (!cp_parser_parsing_tentatively (parser)
11713 || cp_parser_committed_to_tentative_parse (parser))
11714 cp_parser_skip_to_closing_parenthesis (parser,
11715 /*recovering=*/true,
11716 /*or_comma=*/false,
11717 /*consume_paren=*/false);
11725 /* Parse a parameter declaration.
11727 parameter-declaration:
11728 decl-specifier-seq declarator
11729 decl-specifier-seq declarator = assignment-expression
11730 decl-specifier-seq abstract-declarator [opt]
11731 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11733 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11734 declares a template parameter. (In that case, a non-nested `>'
11735 token encountered during the parsing of the assignment-expression
11736 is not interpreted as a greater-than operator.)
11738 Returns a representation of the parameter, or NULL if an error
11739 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11740 true iff the declarator is of the form "(p)". */
11742 static cp_parameter_declarator *
11743 cp_parser_parameter_declaration (cp_parser *parser,
11744 bool template_parm_p,
11745 bool *parenthesized_p)
11747 int declares_class_or_enum;
11748 bool greater_than_is_operator_p;
11749 cp_decl_specifier_seq decl_specifiers;
11750 cp_declarator *declarator;
11751 tree default_argument;
11753 const char *saved_message;
11755 /* In a template parameter, `>' is not an operator.
11759 When parsing a default template-argument for a non-type
11760 template-parameter, the first non-nested `>' is taken as the end
11761 of the template parameter-list rather than a greater-than
11763 greater_than_is_operator_p = !template_parm_p;
11765 /* Type definitions may not appear in parameter types. */
11766 saved_message = parser->type_definition_forbidden_message;
11767 parser->type_definition_forbidden_message
11768 = "types may not be defined in parameter types";
11770 /* Parse the declaration-specifiers. */
11771 cp_parser_decl_specifier_seq (parser,
11772 CP_PARSER_FLAGS_NONE,
11774 &declares_class_or_enum);
11775 /* If an error occurred, there's no reason to attempt to parse the
11776 rest of the declaration. */
11777 if (cp_parser_error_occurred (parser))
11779 parser->type_definition_forbidden_message = saved_message;
11783 /* Peek at the next token. */
11784 token = cp_lexer_peek_token (parser->lexer);
11785 /* If the next token is a `)', `,', `=', `>', or `...', then there
11786 is no declarator. */
11787 if (token->type == CPP_CLOSE_PAREN
11788 || token->type == CPP_COMMA
11789 || token->type == CPP_EQ
11790 || token->type == CPP_ELLIPSIS
11791 || token->type == CPP_GREATER)
11794 if (parenthesized_p)
11795 *parenthesized_p = false;
11797 /* Otherwise, there should be a declarator. */
11800 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11801 parser->default_arg_ok_p = false;
11803 /* After seeing a decl-specifier-seq, if the next token is not a
11804 "(", there is no possibility that the code is a valid
11805 expression. Therefore, if parsing tentatively, we commit at
11807 if (!parser->in_template_argument_list_p
11808 /* In an expression context, having seen:
11812 we cannot be sure whether we are looking at a
11813 function-type (taking a "char" as a parameter) or a cast
11814 of some object of type "char" to "int". */
11815 && !parser->in_type_id_in_expr_p
11816 && cp_parser_parsing_tentatively (parser)
11817 && !cp_parser_committed_to_tentative_parse (parser)
11818 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11819 cp_parser_commit_to_tentative_parse (parser);
11820 /* Parse the declarator. */
11821 declarator = cp_parser_declarator (parser,
11822 CP_PARSER_DECLARATOR_EITHER,
11823 /*ctor_dtor_or_conv_p=*/NULL,
11825 parser->default_arg_ok_p = saved_default_arg_ok_p;
11826 /* After the declarator, allow more attributes. */
11827 decl_specifiers.attributes
11828 = chainon (decl_specifiers.attributes,
11829 cp_parser_attributes_opt (parser));
11832 /* The restriction on defining new types applies only to the type
11833 of the parameter, not to the default argument. */
11834 parser->type_definition_forbidden_message = saved_message;
11836 /* If the next token is `=', then process a default argument. */
11837 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11839 bool saved_greater_than_is_operator_p;
11840 /* Consume the `='. */
11841 cp_lexer_consume_token (parser->lexer);
11843 /* If we are defining a class, then the tokens that make up the
11844 default argument must be saved and processed later. */
11845 if (!template_parm_p && at_class_scope_p ()
11846 && TYPE_BEING_DEFINED (current_class_type))
11848 unsigned depth = 0;
11850 /* Create a DEFAULT_ARG to represented the unparsed default
11852 default_argument = make_node (DEFAULT_ARG);
11853 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11855 /* Add tokens until we have processed the entire default
11862 /* Peek at the next token. */
11863 token = cp_lexer_peek_token (parser->lexer);
11864 /* What we do depends on what token we have. */
11865 switch (token->type)
11867 /* In valid code, a default argument must be
11868 immediately followed by a `,' `)', or `...'. */
11870 case CPP_CLOSE_PAREN:
11872 /* If we run into a non-nested `;', `}', or `]',
11873 then the code is invalid -- but the default
11874 argument is certainly over. */
11875 case CPP_SEMICOLON:
11876 case CPP_CLOSE_BRACE:
11877 case CPP_CLOSE_SQUARE:
11880 /* Update DEPTH, if necessary. */
11881 else if (token->type == CPP_CLOSE_PAREN
11882 || token->type == CPP_CLOSE_BRACE
11883 || token->type == CPP_CLOSE_SQUARE)
11887 case CPP_OPEN_PAREN:
11888 case CPP_OPEN_SQUARE:
11889 case CPP_OPEN_BRACE:
11894 /* If we see a non-nested `>', and `>' is not an
11895 operator, then it marks the end of the default
11897 if (!depth && !greater_than_is_operator_p)
11901 /* If we run out of tokens, issue an error message. */
11903 error ("file ends in default argument");
11909 /* In these cases, we should look for template-ids.
11910 For example, if the default argument is
11911 `X<int, double>()', we need to do name lookup to
11912 figure out whether or not `X' is a template; if
11913 so, the `,' does not end the default argument.
11915 That is not yet done. */
11922 /* If we've reached the end, stop. */
11926 /* Add the token to the token block. */
11927 token = cp_lexer_consume_token (parser->lexer);
11928 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11932 /* Outside of a class definition, we can just parse the
11933 assignment-expression. */
11936 bool saved_local_variables_forbidden_p;
11938 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11940 saved_greater_than_is_operator_p
11941 = parser->greater_than_is_operator_p;
11942 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11943 /* Local variable names (and the `this' keyword) may not
11944 appear in a default argument. */
11945 saved_local_variables_forbidden_p
11946 = parser->local_variables_forbidden_p;
11947 parser->local_variables_forbidden_p = true;
11948 /* Parse the assignment-expression. */
11949 default_argument = cp_parser_assignment_expression (parser);
11950 /* Restore saved state. */
11951 parser->greater_than_is_operator_p
11952 = saved_greater_than_is_operator_p;
11953 parser->local_variables_forbidden_p
11954 = saved_local_variables_forbidden_p;
11956 if (!parser->default_arg_ok_p)
11958 if (!flag_pedantic_errors)
11959 warning ("deprecated use of default argument for parameter of non-function");
11962 error ("default arguments are only permitted for function parameters");
11963 default_argument = NULL_TREE;
11968 default_argument = NULL_TREE;
11970 return make_parameter_declarator (&decl_specifiers,
11975 /* Parse a function-body.
11978 compound_statement */
11981 cp_parser_function_body (cp_parser *parser)
11983 cp_parser_compound_statement (parser, NULL, false);
11986 /* Parse a ctor-initializer-opt followed by a function-body. Return
11987 true if a ctor-initializer was present. */
11990 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11993 bool ctor_initializer_p;
11995 /* Begin the function body. */
11996 body = begin_function_body ();
11997 /* Parse the optional ctor-initializer. */
11998 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11999 /* Parse the function-body. */
12000 cp_parser_function_body (parser);
12001 /* Finish the function body. */
12002 finish_function_body (body);
12004 return ctor_initializer_p;
12007 /* Parse an initializer.
12010 = initializer-clause
12011 ( expression-list )
12013 Returns a expression representing the initializer. If no
12014 initializer is present, NULL_TREE is returned.
12016 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12017 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
12018 set to FALSE if there is no initializer present. If there is an
12019 initializer, and it is not a constant-expression, *NON_CONSTANT_P
12020 is set to true; otherwise it is set to false. */
12023 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12024 bool* non_constant_p)
12029 /* Peek at the next token. */
12030 token = cp_lexer_peek_token (parser->lexer);
12032 /* Let our caller know whether or not this initializer was
12034 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12035 /* Assume that the initializer is constant. */
12036 *non_constant_p = false;
12038 if (token->type == CPP_EQ)
12040 /* Consume the `='. */
12041 cp_lexer_consume_token (parser->lexer);
12042 /* Parse the initializer-clause. */
12043 init = cp_parser_initializer_clause (parser, non_constant_p);
12045 else if (token->type == CPP_OPEN_PAREN)
12046 init = cp_parser_parenthesized_expression_list (parser, false,
12050 /* Anything else is an error. */
12051 cp_parser_error (parser, "expected initializer");
12052 init = error_mark_node;
12058 /* Parse an initializer-clause.
12060 initializer-clause:
12061 assignment-expression
12062 { initializer-list , [opt] }
12065 Returns an expression representing the initializer.
12067 If the `assignment-expression' production is used the value
12068 returned is simply a representation for the expression.
12070 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
12071 the elements of the initializer-list (or NULL_TREE, if the last
12072 production is used). The TREE_TYPE for the CONSTRUCTOR will be
12073 NULL_TREE. There is no way to detect whether or not the optional
12074 trailing `,' was provided. NON_CONSTANT_P is as for
12075 cp_parser_initializer. */
12078 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12082 /* If it is not a `{', then we are looking at an
12083 assignment-expression. */
12084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12087 = cp_parser_constant_expression (parser,
12088 /*allow_non_constant_p=*/true,
12090 if (!*non_constant_p)
12091 initializer = fold_non_dependent_expr (initializer);
12095 /* Consume the `{' token. */
12096 cp_lexer_consume_token (parser->lexer);
12097 /* Create a CONSTRUCTOR to represent the braced-initializer. */
12098 initializer = make_node (CONSTRUCTOR);
12099 /* If it's not a `}', then there is a non-trivial initializer. */
12100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12102 /* Parse the initializer list. */
12103 CONSTRUCTOR_ELTS (initializer)
12104 = cp_parser_initializer_list (parser, non_constant_p);
12105 /* A trailing `,' token is allowed. */
12106 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12107 cp_lexer_consume_token (parser->lexer);
12109 /* Now, there should be a trailing `}'. */
12110 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12113 return initializer;
12116 /* Parse an initializer-list.
12120 initializer-list , initializer-clause
12125 identifier : initializer-clause
12126 initializer-list, identifier : initializer-clause
12128 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
12129 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
12130 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
12131 as for cp_parser_initializer. */
12134 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12136 tree initializers = NULL_TREE;
12138 /* Assume all of the expressions are constant. */
12139 *non_constant_p = false;
12141 /* Parse the rest of the list. */
12147 bool clause_non_constant_p;
12149 /* If the next token is an identifier and the following one is a
12150 colon, we are looking at the GNU designated-initializer
12152 if (cp_parser_allow_gnu_extensions_p (parser)
12153 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12154 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12156 /* Consume the identifier. */
12157 identifier = cp_lexer_consume_token (parser->lexer)->value;
12158 /* Consume the `:'. */
12159 cp_lexer_consume_token (parser->lexer);
12162 identifier = NULL_TREE;
12164 /* Parse the initializer. */
12165 initializer = cp_parser_initializer_clause (parser,
12166 &clause_non_constant_p);
12167 /* If any clause is non-constant, so is the entire initializer. */
12168 if (clause_non_constant_p)
12169 *non_constant_p = true;
12170 /* Add it to the list. */
12171 initializers = tree_cons (identifier, initializer, initializers);
12173 /* If the next token is not a comma, we have reached the end of
12175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12178 /* Peek at the next token. */
12179 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12180 /* If the next token is a `}', then we're still done. An
12181 initializer-clause can have a trailing `,' after the
12182 initializer-list and before the closing `}'. */
12183 if (token->type == CPP_CLOSE_BRACE)
12186 /* Consume the `,' token. */
12187 cp_lexer_consume_token (parser->lexer);
12190 /* The initializers were built up in reverse order, so we need to
12191 reverse them now. */
12192 return nreverse (initializers);
12195 /* Classes [gram.class] */
12197 /* Parse a class-name.
12203 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12204 to indicate that names looked up in dependent types should be
12205 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
12206 keyword has been used to indicate that the name that appears next
12207 is a template. TYPE_P is true iff the next name should be treated
12208 as class-name, even if it is declared to be some other kind of name
12209 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12210 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
12211 being defined in a class-head.
12213 Returns the TYPE_DECL representing the class. */
12216 cp_parser_class_name (cp_parser *parser,
12217 bool typename_keyword_p,
12218 bool template_keyword_p,
12220 bool check_dependency_p,
12222 bool is_declaration)
12229 /* All class-names start with an identifier. */
12230 token = cp_lexer_peek_token (parser->lexer);
12231 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12233 cp_parser_error (parser, "expected class-name");
12234 return error_mark_node;
12237 /* PARSER->SCOPE can be cleared when parsing the template-arguments
12238 to a template-id, so we save it here. */
12239 scope = parser->scope;
12240 if (scope == error_mark_node)
12241 return error_mark_node;
12243 /* Any name names a type if we're following the `typename' keyword
12244 in a qualified name where the enclosing scope is type-dependent. */
12245 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12246 && dependent_type_p (scope));
12247 /* Handle the common case (an identifier, but not a template-id)
12249 if (token->type == CPP_NAME
12250 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12254 /* Look for the identifier. */
12255 identifier = cp_parser_identifier (parser);
12256 /* If the next token isn't an identifier, we are certainly not
12257 looking at a class-name. */
12258 if (identifier == error_mark_node)
12259 decl = error_mark_node;
12260 /* If we know this is a type-name, there's no need to look it
12262 else if (typename_p)
12266 /* If the next token is a `::', then the name must be a type
12269 [basic.lookup.qual]
12271 During the lookup for a name preceding the :: scope
12272 resolution operator, object, function, and enumerator
12273 names are ignored. */
12274 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12276 /* Look up the name. */
12277 decl = cp_parser_lookup_name (parser, identifier,
12279 /*is_template=*/false,
12280 /*is_namespace=*/false,
12281 check_dependency_p,
12282 /*ambiguous_p=*/NULL);
12287 /* Try a template-id. */
12288 decl = cp_parser_template_id (parser, template_keyword_p,
12289 check_dependency_p,
12291 if (decl == error_mark_node)
12292 return error_mark_node;
12295 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12297 /* If this is a typename, create a TYPENAME_TYPE. */
12298 if (typename_p && decl != error_mark_node)
12300 decl = make_typename_type (scope, decl, /*complain=*/1);
12301 if (decl != error_mark_node)
12302 decl = TYPE_NAME (decl);
12305 /* Check to see that it is really the name of a class. */
12306 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12307 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12308 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12309 /* Situations like this:
12311 template <typename T> struct A {
12312 typename T::template X<int>::I i;
12315 are problematic. Is `T::template X<int>' a class-name? The
12316 standard does not seem to be definitive, but there is no other
12317 valid interpretation of the following `::'. Therefore, those
12318 names are considered class-names. */
12319 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12320 else if (decl == error_mark_node
12321 || TREE_CODE (decl) != TYPE_DECL
12322 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12324 cp_parser_error (parser, "expected class-name");
12325 return error_mark_node;
12331 /* Parse a class-specifier.
12334 class-head { member-specification [opt] }
12336 Returns the TREE_TYPE representing the class. */
12339 cp_parser_class_specifier (cp_parser* parser)
12343 tree attributes = NULL_TREE;
12344 int has_trailing_semicolon;
12345 bool nested_name_specifier_p;
12346 unsigned saved_num_template_parameter_lists;
12347 bool pop_p = false;
12348 tree scope = NULL_TREE;
12350 push_deferring_access_checks (dk_no_deferred);
12352 /* Parse the class-head. */
12353 type = cp_parser_class_head (parser,
12354 &nested_name_specifier_p,
12356 /* If the class-head was a semantic disaster, skip the entire body
12360 cp_parser_skip_to_end_of_block_or_statement (parser);
12361 pop_deferring_access_checks ();
12362 return error_mark_node;
12365 /* Look for the `{'. */
12366 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12368 pop_deferring_access_checks ();
12369 return error_mark_node;
12372 /* Issue an error message if type-definitions are forbidden here. */
12373 cp_parser_check_type_definition (parser);
12374 /* Remember that we are defining one more class. */
12375 ++parser->num_classes_being_defined;
12376 /* Inside the class, surrounding template-parameter-lists do not
12378 saved_num_template_parameter_lists
12379 = parser->num_template_parameter_lists;
12380 parser->num_template_parameter_lists = 0;
12382 /* Start the class. */
12383 if (nested_name_specifier_p)
12385 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12386 pop_p = push_scope (scope);
12388 type = begin_class_definition (type);
12390 if (type == error_mark_node)
12391 /* If the type is erroneous, skip the entire body of the class. */
12392 cp_parser_skip_to_closing_brace (parser);
12394 /* Parse the member-specification. */
12395 cp_parser_member_specification_opt (parser);
12397 /* Look for the trailing `}'. */
12398 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12399 /* We get better error messages by noticing a common problem: a
12400 missing trailing `;'. */
12401 token = cp_lexer_peek_token (parser->lexer);
12402 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12403 /* Look for trailing attributes to apply to this class. */
12404 if (cp_parser_allow_gnu_extensions_p (parser))
12406 tree sub_attr = cp_parser_attributes_opt (parser);
12407 attributes = chainon (attributes, sub_attr);
12409 if (type != error_mark_node)
12410 type = finish_struct (type, attributes);
12413 /* If this class is not itself within the scope of another class,
12414 then we need to parse the bodies of all of the queued function
12415 definitions. Note that the queued functions defined in a class
12416 are not always processed immediately following the
12417 class-specifier for that class. Consider:
12420 struct B { void f() { sizeof (A); } };
12423 If `f' were processed before the processing of `A' were
12424 completed, there would be no way to compute the size of `A'.
12425 Note that the nesting we are interested in here is lexical --
12426 not the semantic nesting given by TYPE_CONTEXT. In particular,
12429 struct A { struct B; };
12430 struct A::B { void f() { } };
12432 there is no need to delay the parsing of `A::B::f'. */
12433 if (--parser->num_classes_being_defined == 0)
12440 /* In a first pass, parse default arguments to the functions.
12441 Then, in a second pass, parse the bodies of the functions.
12442 This two-phased approach handles cases like:
12450 class_type = NULL_TREE;
12452 for (TREE_PURPOSE (parser->unparsed_functions_queues)
12453 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12454 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12455 TREE_PURPOSE (parser->unparsed_functions_queues)
12456 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12458 fn = TREE_VALUE (queue_entry);
12459 /* If there are default arguments that have not yet been processed,
12460 take care of them now. */
12461 if (class_type != TREE_PURPOSE (queue_entry))
12464 pop_scope (class_type);
12465 class_type = TREE_PURPOSE (queue_entry);
12466 pop_p = push_scope (class_type);
12468 /* Make sure that any template parameters are in scope. */
12469 maybe_begin_member_template_processing (fn);
12470 /* Parse the default argument expressions. */
12471 cp_parser_late_parsing_default_args (parser, fn);
12472 /* Remove any template parameters from the symbol table. */
12473 maybe_end_member_template_processing ();
12476 pop_scope (class_type);
12477 /* Now parse the body of the functions. */
12478 for (TREE_VALUE (parser->unparsed_functions_queues)
12479 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12480 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12481 TREE_VALUE (parser->unparsed_functions_queues)
12482 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12484 /* Figure out which function we need to process. */
12485 fn = TREE_VALUE (queue_entry);
12487 /* A hack to prevent garbage collection. */
12490 /* Parse the function. */
12491 cp_parser_late_parsing_for_member (parser, fn);
12496 /* Put back any saved access checks. */
12497 pop_deferring_access_checks ();
12499 /* Restore the count of active template-parameter-lists. */
12500 parser->num_template_parameter_lists
12501 = saved_num_template_parameter_lists;
12506 /* Parse a class-head.
12509 class-key identifier [opt] base-clause [opt]
12510 class-key nested-name-specifier identifier base-clause [opt]
12511 class-key nested-name-specifier [opt] template-id
12515 class-key attributes identifier [opt] base-clause [opt]
12516 class-key attributes nested-name-specifier identifier base-clause [opt]
12517 class-key attributes nested-name-specifier [opt] template-id
12520 Returns the TYPE of the indicated class. Sets
12521 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12522 involving a nested-name-specifier was used, and FALSE otherwise.
12524 Returns NULL_TREE if the class-head is syntactically valid, but
12525 semantically invalid in a way that means we should skip the entire
12526 body of the class. */
12529 cp_parser_class_head (cp_parser* parser,
12530 bool* nested_name_specifier_p,
12531 tree *attributes_p)
12533 tree nested_name_specifier;
12534 enum tag_types class_key;
12535 tree id = NULL_TREE;
12536 tree type = NULL_TREE;
12538 bool template_id_p = false;
12539 bool qualified_p = false;
12540 bool invalid_nested_name_p = false;
12541 bool invalid_explicit_specialization_p = false;
12542 bool pop_p = false;
12543 unsigned num_templates;
12546 /* Assume no nested-name-specifier will be present. */
12547 *nested_name_specifier_p = false;
12548 /* Assume no template parameter lists will be used in defining the
12552 /* Look for the class-key. */
12553 class_key = cp_parser_class_key (parser);
12554 if (class_key == none_type)
12555 return error_mark_node;
12557 /* Parse the attributes. */
12558 attributes = cp_parser_attributes_opt (parser);
12560 /* If the next token is `::', that is invalid -- but sometimes
12561 people do try to write:
12565 Handle this gracefully by accepting the extra qualifier, and then
12566 issuing an error about it later if this really is a
12567 class-head. If it turns out just to be an elaborated type
12568 specifier, remain silent. */
12569 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12570 qualified_p = true;
12572 push_deferring_access_checks (dk_no_check);
12574 /* Determine the name of the class. Begin by looking for an
12575 optional nested-name-specifier. */
12576 nested_name_specifier
12577 = cp_parser_nested_name_specifier_opt (parser,
12578 /*typename_keyword_p=*/false,
12579 /*check_dependency_p=*/false,
12581 /*is_declaration=*/false);
12582 /* If there was a nested-name-specifier, then there *must* be an
12584 if (nested_name_specifier)
12586 /* Although the grammar says `identifier', it really means
12587 `class-name' or `template-name'. You are only allowed to
12588 define a class that has already been declared with this
12591 The proposed resolution for Core Issue 180 says that whever
12592 you see `class T::X' you should treat `X' as a type-name.
12594 It is OK to define an inaccessible class; for example:
12596 class A { class B; };
12599 We do not know if we will see a class-name, or a
12600 template-name. We look for a class-name first, in case the
12601 class-name is a template-id; if we looked for the
12602 template-name first we would stop after the template-name. */
12603 cp_parser_parse_tentatively (parser);
12604 type = cp_parser_class_name (parser,
12605 /*typename_keyword_p=*/false,
12606 /*template_keyword_p=*/false,
12608 /*check_dependency_p=*/false,
12609 /*class_head_p=*/true,
12610 /*is_declaration=*/false);
12611 /* If that didn't work, ignore the nested-name-specifier. */
12612 if (!cp_parser_parse_definitely (parser))
12614 invalid_nested_name_p = true;
12615 id = cp_parser_identifier (parser);
12616 if (id == error_mark_node)
12619 /* If we could not find a corresponding TYPE, treat this
12620 declaration like an unqualified declaration. */
12621 if (type == error_mark_node)
12622 nested_name_specifier = NULL_TREE;
12623 /* Otherwise, count the number of templates used in TYPE and its
12624 containing scopes. */
12629 for (scope = TREE_TYPE (type);
12630 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12631 scope = (TYPE_P (scope)
12632 ? TYPE_CONTEXT (scope)
12633 : DECL_CONTEXT (scope)))
12635 && CLASS_TYPE_P (scope)
12636 && CLASSTYPE_TEMPLATE_INFO (scope)
12637 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12638 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12642 /* Otherwise, the identifier is optional. */
12645 /* We don't know whether what comes next is a template-id,
12646 an identifier, or nothing at all. */
12647 cp_parser_parse_tentatively (parser);
12648 /* Check for a template-id. */
12649 id = cp_parser_template_id (parser,
12650 /*template_keyword_p=*/false,
12651 /*check_dependency_p=*/true,
12652 /*is_declaration=*/true);
12653 /* If that didn't work, it could still be an identifier. */
12654 if (!cp_parser_parse_definitely (parser))
12656 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12657 id = cp_parser_identifier (parser);
12663 template_id_p = true;
12668 pop_deferring_access_checks ();
12671 cp_parser_check_for_invalid_template_id (parser, id);
12673 /* If it's not a `:' or a `{' then we can't really be looking at a
12674 class-head, since a class-head only appears as part of a
12675 class-specifier. We have to detect this situation before calling
12676 xref_tag, since that has irreversible side-effects. */
12677 if (!cp_parser_next_token_starts_class_definition_p (parser))
12679 cp_parser_error (parser, "expected `{' or `:'");
12680 return error_mark_node;
12683 /* At this point, we're going ahead with the class-specifier, even
12684 if some other problem occurs. */
12685 cp_parser_commit_to_tentative_parse (parser);
12686 /* Issue the error about the overly-qualified name now. */
12688 cp_parser_error (parser,
12689 "global qualification of class name is invalid");
12690 else if (invalid_nested_name_p)
12691 cp_parser_error (parser,
12692 "qualified name does not name a class");
12693 else if (nested_name_specifier)
12696 /* Figure out in what scope the declaration is being placed. */
12697 scope = current_scope ();
12699 scope = current_namespace;
12700 /* If that scope does not contain the scope in which the
12701 class was originally declared, the program is invalid. */
12702 if (scope && !is_ancestor (scope, nested_name_specifier))
12704 error ("declaration of `%D' in `%D' which does not "
12705 "enclose `%D'", type, scope, nested_name_specifier);
12711 A declarator-id shall not be qualified exception of the
12712 definition of a ... nested class outside of its class
12713 ... [or] a the definition or explicit instantiation of a
12714 class member of a namespace outside of its namespace. */
12715 if (scope == nested_name_specifier)
12717 pedwarn ("extra qualification ignored");
12718 nested_name_specifier = NULL_TREE;
12722 /* An explicit-specialization must be preceded by "template <>". If
12723 it is not, try to recover gracefully. */
12724 if (at_namespace_scope_p ()
12725 && parser->num_template_parameter_lists == 0
12728 error ("an explicit specialization must be preceded by 'template <>'");
12729 invalid_explicit_specialization_p = true;
12730 /* Take the same action that would have been taken by
12731 cp_parser_explicit_specialization. */
12732 ++parser->num_template_parameter_lists;
12733 begin_specialization ();
12735 /* There must be no "return" statements between this point and the
12736 end of this function; set "type "to the correct return value and
12737 use "goto done;" to return. */
12738 /* Make sure that the right number of template parameters were
12740 if (!cp_parser_check_template_parameters (parser, num_templates))
12742 /* If something went wrong, there is no point in even trying to
12743 process the class-definition. */
12748 /* Look up the type. */
12751 type = TREE_TYPE (id);
12752 maybe_process_partial_specialization (type);
12754 else if (!nested_name_specifier)
12756 /* If the class was unnamed, create a dummy name. */
12758 id = make_anon_name ();
12759 type = xref_tag (class_key, id, /*globalize=*/false,
12760 parser->num_template_parameter_lists);
12765 bool pop_p = false;
12769 template <typename T> struct S { struct T };
12770 template <typename T> struct S<T>::T { };
12772 we will get a TYPENAME_TYPE when processing the definition of
12773 `S::T'. We need to resolve it to the actual type before we
12774 try to define it. */
12775 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12777 class_type = resolve_typename_type (TREE_TYPE (type),
12778 /*only_current_p=*/false);
12779 if (class_type != error_mark_node)
12780 type = TYPE_NAME (class_type);
12783 cp_parser_error (parser, "could not resolve typename type");
12784 type = error_mark_node;
12788 maybe_process_partial_specialization (TREE_TYPE (type));
12789 class_type = current_class_type;
12790 /* Enter the scope indicated by the nested-name-specifier. */
12791 if (nested_name_specifier)
12792 pop_p = push_scope (nested_name_specifier);
12793 /* Get the canonical version of this type. */
12794 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12795 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12796 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12797 type = push_template_decl (type);
12798 type = TREE_TYPE (type);
12799 if (nested_name_specifier)
12801 *nested_name_specifier_p = true;
12803 pop_scope (nested_name_specifier);
12806 /* Indicate whether this class was declared as a `class' or as a
12808 if (TREE_CODE (type) == RECORD_TYPE)
12809 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12810 cp_parser_check_class_key (class_key, type);
12812 /* Enter the scope containing the class; the names of base classes
12813 should be looked up in that context. For example, given:
12815 struct A { struct B {}; struct C; };
12816 struct A::C : B {};
12819 if (nested_name_specifier)
12820 pop_p = push_scope (nested_name_specifier);
12824 /* Get the list of base-classes, if there is one. */
12825 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12826 bases = cp_parser_base_clause (parser);
12828 /* Process the base classes. */
12829 xref_basetypes (type, bases);
12831 /* Leave the scope given by the nested-name-specifier. We will
12832 enter the class scope itself while processing the members. */
12834 pop_scope (nested_name_specifier);
12837 if (invalid_explicit_specialization_p)
12839 end_specialization ();
12840 --parser->num_template_parameter_lists;
12842 *attributes_p = attributes;
12846 /* Parse a class-key.
12853 Returns the kind of class-key specified, or none_type to indicate
12856 static enum tag_types
12857 cp_parser_class_key (cp_parser* parser)
12860 enum tag_types tag_type;
12862 /* Look for the class-key. */
12863 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12867 /* Check to see if the TOKEN is a class-key. */
12868 tag_type = cp_parser_token_is_class_key (token);
12870 cp_parser_error (parser, "expected class-key");
12874 /* Parse an (optional) member-specification.
12876 member-specification:
12877 member-declaration member-specification [opt]
12878 access-specifier : member-specification [opt] */
12881 cp_parser_member_specification_opt (cp_parser* parser)
12888 /* Peek at the next token. */
12889 token = cp_lexer_peek_token (parser->lexer);
12890 /* If it's a `}', or EOF then we've seen all the members. */
12891 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12894 /* See if this token is a keyword. */
12895 keyword = token->keyword;
12899 case RID_PROTECTED:
12901 /* Consume the access-specifier. */
12902 cp_lexer_consume_token (parser->lexer);
12903 /* Remember which access-specifier is active. */
12904 current_access_specifier = token->value;
12905 /* Look for the `:'. */
12906 cp_parser_require (parser, CPP_COLON, "`:'");
12910 /* Otherwise, the next construction must be a
12911 member-declaration. */
12912 cp_parser_member_declaration (parser);
12917 /* Parse a member-declaration.
12919 member-declaration:
12920 decl-specifier-seq [opt] member-declarator-list [opt] ;
12921 function-definition ; [opt]
12922 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12924 template-declaration
12926 member-declarator-list:
12928 member-declarator-list , member-declarator
12931 declarator pure-specifier [opt]
12932 declarator constant-initializer [opt]
12933 identifier [opt] : constant-expression
12937 member-declaration:
12938 __extension__ member-declaration
12941 declarator attributes [opt] pure-specifier [opt]
12942 declarator attributes [opt] constant-initializer [opt]
12943 identifier [opt] attributes [opt] : constant-expression */
12946 cp_parser_member_declaration (cp_parser* parser)
12948 cp_decl_specifier_seq decl_specifiers;
12949 tree prefix_attributes;
12951 int declares_class_or_enum;
12954 int saved_pedantic;
12956 /* Check for the `__extension__' keyword. */
12957 if (cp_parser_extension_opt (parser, &saved_pedantic))
12960 cp_parser_member_declaration (parser);
12961 /* Restore the old value of the PEDANTIC flag. */
12962 pedantic = saved_pedantic;
12967 /* Check for a template-declaration. */
12968 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12970 /* Parse the template-declaration. */
12971 cp_parser_template_declaration (parser, /*member_p=*/true);
12976 /* Check for a using-declaration. */
12977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12979 /* Parse the using-declaration. */
12980 cp_parser_using_declaration (parser);
12985 /* Parse the decl-specifier-seq. */
12986 cp_parser_decl_specifier_seq (parser,
12987 CP_PARSER_FLAGS_OPTIONAL,
12989 &declares_class_or_enum);
12990 prefix_attributes = decl_specifiers.attributes;
12991 decl_specifiers.attributes = NULL_TREE;
12992 /* Check for an invalid type-name. */
12993 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12995 /* If there is no declarator, then the decl-specifier-seq should
12997 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12999 /* If there was no decl-specifier-seq, and the next token is a
13000 `;', then we have something like:
13006 Each member-declaration shall declare at least one member
13007 name of the class. */
13008 if (!decl_specifiers.any_specifiers_p)
13011 pedwarn ("extra semicolon");
13017 /* See if this declaration is a friend. */
13018 friend_p = cp_parser_friend_p (&decl_specifiers);
13019 /* If there were decl-specifiers, check to see if there was
13020 a class-declaration. */
13021 type = check_tag_decl (&decl_specifiers);
13022 /* Nested classes have already been added to the class, but
13023 a `friend' needs to be explicitly registered. */
13026 /* If the `friend' keyword was present, the friend must
13027 be introduced with a class-key. */
13028 if (!declares_class_or_enum)
13029 error ("a class-key must be used when declaring a friend");
13032 template <typename T> struct A {
13033 friend struct A<T>::B;
13036 A<T>::B will be represented by a TYPENAME_TYPE, and
13037 therefore not recognized by check_tag_decl. */
13039 && decl_specifiers.type
13040 && TYPE_P (decl_specifiers.type))
13041 type = decl_specifiers.type;
13042 if (!type || !TYPE_P (type))
13043 error ("friend declaration does not name a class or "
13046 make_friend_class (current_class_type, type,
13047 /*complain=*/true);
13049 /* If there is no TYPE, an error message will already have
13051 else if (!type || type == error_mark_node)
13053 /* An anonymous aggregate has to be handled specially; such
13054 a declaration really declares a data member (with a
13055 particular type), as opposed to a nested class. */
13056 else if (ANON_AGGR_TYPE_P (type))
13058 /* Remove constructors and such from TYPE, now that we
13059 know it is an anonymous aggregate. */
13060 fixup_anonymous_aggr (type);
13061 /* And make the corresponding data member. */
13062 decl = build_decl (FIELD_DECL, NULL_TREE, type);
13063 /* Add it to the class. */
13064 finish_member_declaration (decl);
13067 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13072 /* See if these declarations will be friends. */
13073 friend_p = cp_parser_friend_p (&decl_specifiers);
13075 /* Keep going until we hit the `;' at the end of the
13077 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13079 tree attributes = NULL_TREE;
13080 tree first_attribute;
13082 /* Peek at the next token. */
13083 token = cp_lexer_peek_token (parser->lexer);
13085 /* Check for a bitfield declaration. */
13086 if (token->type == CPP_COLON
13087 || (token->type == CPP_NAME
13088 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13094 /* Get the name of the bitfield. Note that we cannot just
13095 check TOKEN here because it may have been invalidated by
13096 the call to cp_lexer_peek_nth_token above. */
13097 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13098 identifier = cp_parser_identifier (parser);
13100 identifier = NULL_TREE;
13102 /* Consume the `:' token. */
13103 cp_lexer_consume_token (parser->lexer);
13104 /* Get the width of the bitfield. */
13106 = cp_parser_constant_expression (parser,
13107 /*allow_non_constant=*/false,
13110 /* Look for attributes that apply to the bitfield. */
13111 attributes = cp_parser_attributes_opt (parser);
13112 /* Remember which attributes are prefix attributes and
13114 first_attribute = attributes;
13115 /* Combine the attributes. */
13116 attributes = chainon (prefix_attributes, attributes);
13118 /* Create the bitfield declaration. */
13119 decl = grokbitfield (identifier
13120 ? make_id_declarator (identifier)
13124 /* Apply the attributes. */
13125 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13129 cp_declarator *declarator;
13131 tree asm_specification;
13132 int ctor_dtor_or_conv_p;
13134 /* Parse the declarator. */
13136 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13137 &ctor_dtor_or_conv_p,
13138 /*parenthesized_p=*/NULL);
13140 /* If something went wrong parsing the declarator, make sure
13141 that we at least consume some tokens. */
13142 if (declarator == cp_error_declarator)
13144 /* Skip to the end of the statement. */
13145 cp_parser_skip_to_end_of_statement (parser);
13146 /* If the next token is not a semicolon, that is
13147 probably because we just skipped over the body of
13148 a function. So, we consume a semicolon if
13149 present, but do not issue an error message if it
13151 if (cp_lexer_next_token_is (parser->lexer,
13153 cp_lexer_consume_token (parser->lexer);
13157 cp_parser_check_for_definition_in_return_type
13158 (declarator, declares_class_or_enum);
13160 /* Look for an asm-specification. */
13161 asm_specification = cp_parser_asm_specification_opt (parser);
13162 /* Look for attributes that apply to the declaration. */
13163 attributes = cp_parser_attributes_opt (parser);
13164 /* Remember which attributes are prefix attributes and
13166 first_attribute = attributes;
13167 /* Combine the attributes. */
13168 attributes = chainon (prefix_attributes, attributes);
13170 /* If it's an `=', then we have a constant-initializer or a
13171 pure-specifier. It is not correct to parse the
13172 initializer before registering the member declaration
13173 since the member declaration should be in scope while
13174 its initializer is processed. However, the rest of the
13175 front end does not yet provide an interface that allows
13176 us to handle this correctly. */
13177 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13181 A pure-specifier shall be used only in the declaration of
13182 a virtual function.
13184 A member-declarator can contain a constant-initializer
13185 only if it declares a static member of integral or
13188 Therefore, if the DECLARATOR is for a function, we look
13189 for a pure-specifier; otherwise, we look for a
13190 constant-initializer. When we call `grokfield', it will
13191 perform more stringent semantics checks. */
13192 if (declarator->kind == cdk_function)
13193 initializer = cp_parser_pure_specifier (parser);
13195 /* Parse the initializer. */
13196 initializer = cp_parser_constant_initializer (parser);
13198 /* Otherwise, there is no initializer. */
13200 initializer = NULL_TREE;
13202 /* See if we are probably looking at a function
13203 definition. We are certainly not looking at at a
13204 member-declarator. Calling `grokfield' has
13205 side-effects, so we must not do it unless we are sure
13206 that we are looking at a member-declarator. */
13207 if (cp_parser_token_starts_function_definition_p
13208 (cp_lexer_peek_token (parser->lexer)))
13210 /* The grammar does not allow a pure-specifier to be
13211 used when a member function is defined. (It is
13212 possible that this fact is an oversight in the
13213 standard, since a pure function may be defined
13214 outside of the class-specifier. */
13216 error ("pure-specifier on function-definition");
13217 decl = cp_parser_save_member_function_body (parser,
13221 /* If the member was not a friend, declare it here. */
13223 finish_member_declaration (decl);
13224 /* Peek at the next token. */
13225 token = cp_lexer_peek_token (parser->lexer);
13226 /* If the next token is a semicolon, consume it. */
13227 if (token->type == CPP_SEMICOLON)
13228 cp_lexer_consume_token (parser->lexer);
13233 /* Create the declaration. */
13234 decl = grokfield (declarator, &decl_specifiers,
13235 initializer, asm_specification,
13237 /* Any initialization must have been from a
13238 constant-expression. */
13239 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13240 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13244 /* Reset PREFIX_ATTRIBUTES. */
13245 while (attributes && TREE_CHAIN (attributes) != first_attribute)
13246 attributes = TREE_CHAIN (attributes);
13248 TREE_CHAIN (attributes) = NULL_TREE;
13250 /* If there is any qualification still in effect, clear it
13251 now; we will be starting fresh with the next declarator. */
13252 parser->scope = NULL_TREE;
13253 parser->qualifying_scope = NULL_TREE;
13254 parser->object_scope = NULL_TREE;
13255 /* If it's a `,', then there are more declarators. */
13256 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13257 cp_lexer_consume_token (parser->lexer);
13258 /* If the next token isn't a `;', then we have a parse error. */
13259 else if (cp_lexer_next_token_is_not (parser->lexer,
13262 cp_parser_error (parser, "expected `;'");
13263 /* Skip tokens until we find a `;'. */
13264 cp_parser_skip_to_end_of_statement (parser);
13271 /* Add DECL to the list of members. */
13273 finish_member_declaration (decl);
13275 if (TREE_CODE (decl) == FUNCTION_DECL)
13276 cp_parser_save_default_args (parser, decl);
13281 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13284 /* Parse a pure-specifier.
13289 Returns INTEGER_ZERO_NODE if a pure specifier is found.
13290 Otherwise, ERROR_MARK_NODE is returned. */
13293 cp_parser_pure_specifier (cp_parser* parser)
13297 /* Look for the `=' token. */
13298 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13299 return error_mark_node;
13300 /* Look for the `0' token. */
13301 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13302 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
13303 to get information from the lexer about how the number was
13304 spelled in order to fix this problem. */
13305 if (!token || !integer_zerop (token->value))
13306 return error_mark_node;
13308 return integer_zero_node;
13311 /* Parse a constant-initializer.
13313 constant-initializer:
13314 = constant-expression
13316 Returns a representation of the constant-expression. */
13319 cp_parser_constant_initializer (cp_parser* parser)
13321 /* Look for the `=' token. */
13322 if (!cp_parser_require (parser, CPP_EQ, "`='"))
13323 return error_mark_node;
13325 /* It is invalid to write:
13327 struct S { static const int i = { 7 }; };
13330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13332 cp_parser_error (parser,
13333 "a brace-enclosed initializer is not allowed here");
13334 /* Consume the opening brace. */
13335 cp_lexer_consume_token (parser->lexer);
13336 /* Skip the initializer. */
13337 cp_parser_skip_to_closing_brace (parser);
13338 /* Look for the trailing `}'. */
13339 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13341 return error_mark_node;
13344 return cp_parser_constant_expression (parser,
13345 /*allow_non_constant=*/false,
13349 /* Derived classes [gram.class.derived] */
13351 /* Parse a base-clause.
13354 : base-specifier-list
13356 base-specifier-list:
13358 base-specifier-list , base-specifier
13360 Returns a TREE_LIST representing the base-classes, in the order in
13361 which they were declared. The representation of each node is as
13362 described by cp_parser_base_specifier.
13364 In the case that no bases are specified, this function will return
13365 NULL_TREE, not ERROR_MARK_NODE. */
13368 cp_parser_base_clause (cp_parser* parser)
13370 tree bases = NULL_TREE;
13372 /* Look for the `:' that begins the list. */
13373 cp_parser_require (parser, CPP_COLON, "`:'");
13375 /* Scan the base-specifier-list. */
13381 /* Look for the base-specifier. */
13382 base = cp_parser_base_specifier (parser);
13383 /* Add BASE to the front of the list. */
13384 if (base != error_mark_node)
13386 TREE_CHAIN (base) = bases;
13389 /* Peek at the next token. */
13390 token = cp_lexer_peek_token (parser->lexer);
13391 /* If it's not a comma, then the list is complete. */
13392 if (token->type != CPP_COMMA)
13394 /* Consume the `,'. */
13395 cp_lexer_consume_token (parser->lexer);
13398 /* PARSER->SCOPE may still be non-NULL at this point, if the last
13399 base class had a qualified name. However, the next name that
13400 appears is certainly not qualified. */
13401 parser->scope = NULL_TREE;
13402 parser->qualifying_scope = NULL_TREE;
13403 parser->object_scope = NULL_TREE;
13405 return nreverse (bases);
13408 /* Parse a base-specifier.
13411 :: [opt] nested-name-specifier [opt] class-name
13412 virtual access-specifier [opt] :: [opt] nested-name-specifier
13414 access-specifier virtual [opt] :: [opt] nested-name-specifier
13417 Returns a TREE_LIST. The TREE_PURPOSE will be one of
13418 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13419 indicate the specifiers provided. The TREE_VALUE will be a TYPE
13420 (or the ERROR_MARK_NODE) indicating the type that was specified. */
13423 cp_parser_base_specifier (cp_parser* parser)
13427 bool virtual_p = false;
13428 bool duplicate_virtual_error_issued_p = false;
13429 bool duplicate_access_error_issued_p = false;
13430 bool class_scope_p, template_p;
13431 tree access = access_default_node;
13434 /* Process the optional `virtual' and `access-specifier'. */
13437 /* Peek at the next token. */
13438 token = cp_lexer_peek_token (parser->lexer);
13439 /* Process `virtual'. */
13440 switch (token->keyword)
13443 /* If `virtual' appears more than once, issue an error. */
13444 if (virtual_p && !duplicate_virtual_error_issued_p)
13446 cp_parser_error (parser,
13447 "`virtual' specified more than once in base-specified");
13448 duplicate_virtual_error_issued_p = true;
13453 /* Consume the `virtual' token. */
13454 cp_lexer_consume_token (parser->lexer);
13459 case RID_PROTECTED:
13461 /* If more than one access specifier appears, issue an
13463 if (access != access_default_node
13464 && !duplicate_access_error_issued_p)
13466 cp_parser_error (parser,
13467 "more than one access specifier in base-specified");
13468 duplicate_access_error_issued_p = true;
13471 access = ridpointers[(int) token->keyword];
13473 /* Consume the access-specifier. */
13474 cp_lexer_consume_token (parser->lexer);
13483 /* It is not uncommon to see programs mechanically, erroneously, use
13484 the 'typename' keyword to denote (dependent) qualified types
13485 as base classes. */
13486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13488 if (!processing_template_decl)
13489 error ("keyword `typename' not allowed outside of templates");
13491 error ("keyword `typename' not allowed in this context "
13492 "(the base class is implicitly a type)");
13493 cp_lexer_consume_token (parser->lexer);
13496 /* Look for the optional `::' operator. */
13497 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13498 /* Look for the nested-name-specifier. The simplest way to
13503 The keyword `typename' is not permitted in a base-specifier or
13504 mem-initializer; in these contexts a qualified name that
13505 depends on a template-parameter is implicitly assumed to be a
13508 is to pretend that we have seen the `typename' keyword at this
13510 cp_parser_nested_name_specifier_opt (parser,
13511 /*typename_keyword_p=*/true,
13512 /*check_dependency_p=*/true,
13514 /*is_declaration=*/true);
13515 /* If the base class is given by a qualified name, assume that names
13516 we see are type names or templates, as appropriate. */
13517 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13518 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13520 /* Finally, look for the class-name. */
13521 type = cp_parser_class_name (parser,
13525 /*check_dependency_p=*/true,
13526 /*class_head_p=*/false,
13527 /*is_declaration=*/true);
13529 if (type == error_mark_node)
13530 return error_mark_node;
13532 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13535 /* Exception handling [gram.exception] */
13537 /* Parse an (optional) exception-specification.
13539 exception-specification:
13540 throw ( type-id-list [opt] )
13542 Returns a TREE_LIST representing the exception-specification. The
13543 TREE_VALUE of each node is a type. */
13546 cp_parser_exception_specification_opt (cp_parser* parser)
13551 /* Peek at the next token. */
13552 token = cp_lexer_peek_token (parser->lexer);
13553 /* If it's not `throw', then there's no exception-specification. */
13554 if (!cp_parser_is_keyword (token, RID_THROW))
13557 /* Consume the `throw'. */
13558 cp_lexer_consume_token (parser->lexer);
13560 /* Look for the `('. */
13561 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13563 /* Peek at the next token. */
13564 token = cp_lexer_peek_token (parser->lexer);
13565 /* If it's not a `)', then there is a type-id-list. */
13566 if (token->type != CPP_CLOSE_PAREN)
13568 const char *saved_message;
13570 /* Types may not be defined in an exception-specification. */
13571 saved_message = parser->type_definition_forbidden_message;
13572 parser->type_definition_forbidden_message
13573 = "types may not be defined in an exception-specification";
13574 /* Parse the type-id-list. */
13575 type_id_list = cp_parser_type_id_list (parser);
13576 /* Restore the saved message. */
13577 parser->type_definition_forbidden_message = saved_message;
13580 type_id_list = empty_except_spec;
13582 /* Look for the `)'. */
13583 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13585 return type_id_list;
13588 /* Parse an (optional) type-id-list.
13592 type-id-list , type-id
13594 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13595 in the order that the types were presented. */
13598 cp_parser_type_id_list (cp_parser* parser)
13600 tree types = NULL_TREE;
13607 /* Get the next type-id. */
13608 type = cp_parser_type_id (parser);
13609 /* Add it to the list. */
13610 types = add_exception_specifier (types, type, /*complain=*/1);
13611 /* Peek at the next token. */
13612 token = cp_lexer_peek_token (parser->lexer);
13613 /* If it is not a `,', we are done. */
13614 if (token->type != CPP_COMMA)
13616 /* Consume the `,'. */
13617 cp_lexer_consume_token (parser->lexer);
13620 return nreverse (types);
13623 /* Parse a try-block.
13626 try compound-statement handler-seq */
13629 cp_parser_try_block (cp_parser* parser)
13633 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13634 try_block = begin_try_block ();
13635 cp_parser_compound_statement (parser, NULL, true);
13636 finish_try_block (try_block);
13637 cp_parser_handler_seq (parser);
13638 finish_handler_sequence (try_block);
13643 /* Parse a function-try-block.
13645 function-try-block:
13646 try ctor-initializer [opt] function-body handler-seq */
13649 cp_parser_function_try_block (cp_parser* parser)
13652 bool ctor_initializer_p;
13654 /* Look for the `try' keyword. */
13655 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13657 /* Let the rest of the front-end know where we are. */
13658 try_block = begin_function_try_block ();
13659 /* Parse the function-body. */
13661 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13662 /* We're done with the `try' part. */
13663 finish_function_try_block (try_block);
13664 /* Parse the handlers. */
13665 cp_parser_handler_seq (parser);
13666 /* We're done with the handlers. */
13667 finish_function_handler_sequence (try_block);
13669 return ctor_initializer_p;
13672 /* Parse a handler-seq.
13675 handler handler-seq [opt] */
13678 cp_parser_handler_seq (cp_parser* parser)
13684 /* Parse the handler. */
13685 cp_parser_handler (parser);
13686 /* Peek at the next token. */
13687 token = cp_lexer_peek_token (parser->lexer);
13688 /* If it's not `catch' then there are no more handlers. */
13689 if (!cp_parser_is_keyword (token, RID_CATCH))
13694 /* Parse a handler.
13697 catch ( exception-declaration ) compound-statement */
13700 cp_parser_handler (cp_parser* parser)
13705 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13706 handler = begin_handler ();
13707 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13708 declaration = cp_parser_exception_declaration (parser);
13709 finish_handler_parms (declaration, handler);
13710 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13711 cp_parser_compound_statement (parser, NULL, false);
13712 finish_handler (handler);
13715 /* Parse an exception-declaration.
13717 exception-declaration:
13718 type-specifier-seq declarator
13719 type-specifier-seq abstract-declarator
13723 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13724 ellipsis variant is used. */
13727 cp_parser_exception_declaration (cp_parser* parser)
13730 cp_decl_specifier_seq type_specifiers;
13731 cp_declarator *declarator;
13732 const char *saved_message;
13734 /* If it's an ellipsis, it's easy to handle. */
13735 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13737 /* Consume the `...' token. */
13738 cp_lexer_consume_token (parser->lexer);
13742 /* Types may not be defined in exception-declarations. */
13743 saved_message = parser->type_definition_forbidden_message;
13744 parser->type_definition_forbidden_message
13745 = "types may not be defined in exception-declarations";
13747 /* Parse the type-specifier-seq. */
13748 cp_parser_type_specifier_seq (parser, &type_specifiers);
13749 /* If it's a `)', then there is no declarator. */
13750 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13753 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13754 /*ctor_dtor_or_conv_p=*/NULL,
13755 /*parenthesized_p=*/NULL);
13757 /* Restore the saved message. */
13758 parser->type_definition_forbidden_message = saved_message;
13760 if (type_specifiers.any_specifiers_p)
13762 decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13763 if (decl == NULL_TREE)
13764 error ("invalid catch parameter");
13772 /* Parse a throw-expression.
13775 throw assignment-expression [opt]
13777 Returns a THROW_EXPR representing the throw-expression. */
13780 cp_parser_throw_expression (cp_parser* parser)
13785 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13786 token = cp_lexer_peek_token (parser->lexer);
13787 /* Figure out whether or not there is an assignment-expression
13788 following the "throw" keyword. */
13789 if (token->type == CPP_COMMA
13790 || token->type == CPP_SEMICOLON
13791 || token->type == CPP_CLOSE_PAREN
13792 || token->type == CPP_CLOSE_SQUARE
13793 || token->type == CPP_CLOSE_BRACE
13794 || token->type == CPP_COLON)
13795 expression = NULL_TREE;
13797 expression = cp_parser_assignment_expression (parser);
13799 return build_throw (expression);
13802 /* GNU Extensions */
13804 /* Parse an (optional) asm-specification.
13807 asm ( string-literal )
13809 If the asm-specification is present, returns a STRING_CST
13810 corresponding to the string-literal. Otherwise, returns
13814 cp_parser_asm_specification_opt (cp_parser* parser)
13817 tree asm_specification;
13819 /* Peek at the next token. */
13820 token = cp_lexer_peek_token (parser->lexer);
13821 /* If the next token isn't the `asm' keyword, then there's no
13822 asm-specification. */
13823 if (!cp_parser_is_keyword (token, RID_ASM))
13826 /* Consume the `asm' token. */
13827 cp_lexer_consume_token (parser->lexer);
13828 /* Look for the `('. */
13829 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13831 /* Look for the string-literal. */
13832 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13834 asm_specification = token->value;
13836 asm_specification = NULL_TREE;
13838 /* Look for the `)'. */
13839 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13841 return asm_specification;
13844 /* Parse an asm-operand-list.
13848 asm-operand-list , asm-operand
13851 string-literal ( expression )
13852 [ string-literal ] string-literal ( expression )
13854 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13855 each node is the expression. The TREE_PURPOSE is itself a
13856 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13857 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13858 is a STRING_CST for the string literal before the parenthesis. */
13861 cp_parser_asm_operand_list (cp_parser* parser)
13863 tree asm_operands = NULL_TREE;
13867 tree string_literal;
13872 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13874 /* Consume the `[' token. */
13875 cp_lexer_consume_token (parser->lexer);
13876 /* Read the operand name. */
13877 name = cp_parser_identifier (parser);
13878 if (name != error_mark_node)
13879 name = build_string (IDENTIFIER_LENGTH (name),
13880 IDENTIFIER_POINTER (name));
13881 /* Look for the closing `]'. */
13882 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13886 /* Look for the string-literal. */
13887 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13888 string_literal = token ? token->value : error_mark_node;
13889 c_lex_string_translate = 1;
13890 /* Look for the `('. */
13891 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13892 /* Parse the expression. */
13893 expression = cp_parser_expression (parser);
13894 /* Look for the `)'. */
13895 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13896 c_lex_string_translate = 0;
13897 /* Add this operand to the list. */
13898 asm_operands = tree_cons (build_tree_list (name, string_literal),
13901 /* If the next token is not a `,', there are no more
13903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13905 /* Consume the `,'. */
13906 cp_lexer_consume_token (parser->lexer);
13909 return nreverse (asm_operands);
13912 /* Parse an asm-clobber-list.
13916 asm-clobber-list , string-literal
13918 Returns a TREE_LIST, indicating the clobbers in the order that they
13919 appeared. The TREE_VALUE of each node is a STRING_CST. */
13922 cp_parser_asm_clobber_list (cp_parser* parser)
13924 tree clobbers = NULL_TREE;
13929 tree string_literal;
13931 /* Look for the string literal. */
13932 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13933 string_literal = token ? token->value : error_mark_node;
13934 /* Add it to the list. */
13935 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13936 /* If the next token is not a `,', then the list is
13938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13940 /* Consume the `,' token. */
13941 cp_lexer_consume_token (parser->lexer);
13947 /* Parse an (optional) series of attributes.
13950 attributes attribute
13953 __attribute__ (( attribute-list [opt] ))
13955 The return value is as for cp_parser_attribute_list. */
13958 cp_parser_attributes_opt (cp_parser* parser)
13960 tree attributes = NULL_TREE;
13965 tree attribute_list;
13967 /* Peek at the next token. */
13968 token = cp_lexer_peek_token (parser->lexer);
13969 /* If it's not `__attribute__', then we're done. */
13970 if (token->keyword != RID_ATTRIBUTE)
13973 /* Consume the `__attribute__' keyword. */
13974 cp_lexer_consume_token (parser->lexer);
13975 /* Look for the two `(' tokens. */
13976 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13977 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13979 /* Peek at the next token. */
13980 token = cp_lexer_peek_token (parser->lexer);
13981 if (token->type != CPP_CLOSE_PAREN)
13982 /* Parse the attribute-list. */
13983 attribute_list = cp_parser_attribute_list (parser);
13985 /* If the next token is a `)', then there is no attribute
13987 attribute_list = NULL;
13989 /* Look for the two `)' tokens. */
13990 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13991 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13993 /* Add these new attributes to the list. */
13994 attributes = chainon (attributes, attribute_list);
14000 /* Parse an attribute-list.
14004 attribute-list , attribute
14008 identifier ( identifier )
14009 identifier ( identifier , expression-list )
14010 identifier ( expression-list )
14012 Returns a TREE_LIST. Each node corresponds to an attribute. THe
14013 TREE_PURPOSE of each node is the identifier indicating which
14014 attribute is in use. The TREE_VALUE represents the arguments, if
14018 cp_parser_attribute_list (cp_parser* parser)
14020 tree attribute_list = NULL_TREE;
14022 c_lex_string_translate = 0;
14029 /* Look for the identifier. We also allow keywords here; for
14030 example `__attribute__ ((const))' is legal. */
14031 token = cp_lexer_peek_token (parser->lexer);
14032 if (token->type != CPP_NAME
14033 && token->type != CPP_KEYWORD)
14034 return error_mark_node;
14035 /* Consume the token. */
14036 token = cp_lexer_consume_token (parser->lexer);
14038 /* Save away the identifier that indicates which attribute this is. */
14039 identifier = token->value;
14040 attribute = build_tree_list (identifier, NULL_TREE);
14042 /* Peek at the next token. */
14043 token = cp_lexer_peek_token (parser->lexer);
14044 /* If it's an `(', then parse the attribute arguments. */
14045 if (token->type == CPP_OPEN_PAREN)
14049 arguments = (cp_parser_parenthesized_expression_list
14050 (parser, true, /*non_constant_p=*/NULL));
14051 /* Save the identifier and arguments away. */
14052 TREE_VALUE (attribute) = arguments;
14055 /* Add this attribute to the list. */
14056 TREE_CHAIN (attribute) = attribute_list;
14057 attribute_list = attribute;
14059 /* Now, look for more attributes. */
14060 token = cp_lexer_peek_token (parser->lexer);
14061 /* If the next token isn't a `,', we're done. */
14062 if (token->type != CPP_COMMA)
14065 /* Consume the comma and keep going. */
14066 cp_lexer_consume_token (parser->lexer);
14068 c_lex_string_translate = 1;
14070 /* We built up the list in reverse order. */
14071 return nreverse (attribute_list);
14074 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
14075 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
14076 current value of the PEDANTIC flag, regardless of whether or not
14077 the `__extension__' keyword is present. The caller is responsible
14078 for restoring the value of the PEDANTIC flag. */
14081 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14083 /* Save the old value of the PEDANTIC flag. */
14084 *saved_pedantic = pedantic;
14086 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14088 /* Consume the `__extension__' token. */
14089 cp_lexer_consume_token (parser->lexer);
14090 /* We're not being pedantic while the `__extension__' keyword is
14100 /* Parse a label declaration.
14103 __label__ label-declarator-seq ;
14105 label-declarator-seq:
14106 identifier , label-declarator-seq
14110 cp_parser_label_declaration (cp_parser* parser)
14112 /* Look for the `__label__' keyword. */
14113 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14119 /* Look for an identifier. */
14120 identifier = cp_parser_identifier (parser);
14121 /* Declare it as a lobel. */
14122 finish_label_decl (identifier);
14123 /* If the next token is a `;', stop. */
14124 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14126 /* Look for the `,' separating the label declarations. */
14127 cp_parser_require (parser, CPP_COMMA, "`,'");
14130 /* Look for the final `;'. */
14131 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14134 /* Support Functions */
14136 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14137 NAME should have one of the representations used for an
14138 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14139 is returned. If PARSER->SCOPE is a dependent type, then a
14140 SCOPE_REF is returned.
14142 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14143 returned; the name was already resolved when the TEMPLATE_ID_EXPR
14144 was formed. Abstractly, such entities should not be passed to this
14145 function, because they do not need to be looked up, but it is
14146 simpler to check for this special case here, rather than at the
14149 In cases not explicitly covered above, this function returns a
14150 DECL, OVERLOAD, or baselink representing the result of the lookup.
14151 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14154 If IS_TYPE is TRUE, bindings that do not refer to types are
14157 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14160 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14163 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14166 If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14167 results in an ambiguity, and false otherwise. */
14170 cp_parser_lookup_name (cp_parser *parser, tree name,
14171 bool is_type, bool is_template, bool is_namespace,
14172 bool check_dependency,
14176 tree object_type = parser->context->object_type;
14178 /* Assume that the lookup will be unambiguous. */
14180 *ambiguous_p = false;
14182 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14183 no longer valid. Note that if we are parsing tentatively, and
14184 the parse fails, OBJECT_TYPE will be automatically restored. */
14185 parser->context->object_type = NULL_TREE;
14187 if (name == error_mark_node)
14188 return error_mark_node;
14190 /* A template-id has already been resolved; there is no lookup to
14192 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14194 if (BASELINK_P (name))
14196 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14197 == TEMPLATE_ID_EXPR);
14201 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
14202 it should already have been checked to make sure that the name
14203 used matches the type being destroyed. */
14204 if (TREE_CODE (name) == BIT_NOT_EXPR)
14208 /* Figure out to which type this destructor applies. */
14210 type = parser->scope;
14211 else if (object_type)
14212 type = object_type;
14214 type = current_class_type;
14215 /* If that's not a class type, there is no destructor. */
14216 if (!type || !CLASS_TYPE_P (type))
14217 return error_mark_node;
14218 if (!CLASSTYPE_DESTRUCTORS (type))
14219 return error_mark_node;
14220 /* If it was a class type, return the destructor. */
14221 return CLASSTYPE_DESTRUCTORS (type);
14224 /* By this point, the NAME should be an ordinary identifier. If
14225 the id-expression was a qualified name, the qualifying scope is
14226 stored in PARSER->SCOPE at this point. */
14227 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14229 /* Perform the lookup. */
14234 if (parser->scope == error_mark_node)
14235 return error_mark_node;
14237 /* If the SCOPE is dependent, the lookup must be deferred until
14238 the template is instantiated -- unless we are explicitly
14239 looking up names in uninstantiated templates. Even then, we
14240 cannot look up the name if the scope is not a class type; it
14241 might, for example, be a template type parameter. */
14242 dependent_p = (TYPE_P (parser->scope)
14243 && !(parser->in_declarator_p
14244 && currently_open_class (parser->scope))
14245 && dependent_type_p (parser->scope));
14246 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14250 /* The resolution to Core Issue 180 says that `struct A::B'
14251 should be considered a type-name, even if `A' is
14253 decl = TYPE_NAME (make_typename_type (parser->scope,
14256 else if (is_template)
14257 decl = make_unbound_class_template (parser->scope,
14261 decl = build_nt (SCOPE_REF, parser->scope, name);
14265 bool pop_p = false;
14267 /* If PARSER->SCOPE is a dependent type, then it must be a
14268 class type, and we must not be checking dependencies;
14269 otherwise, we would have processed this lookup above. So
14270 that PARSER->SCOPE is not considered a dependent base by
14271 lookup_member, we must enter the scope here. */
14273 pop_p = push_scope (parser->scope);
14274 /* If the PARSER->SCOPE is a a template specialization, it
14275 may be instantiated during name lookup. In that case,
14276 errors may be issued. Even if we rollback the current
14277 tentative parse, those errors are valid. */
14278 decl = lookup_qualified_name (parser->scope, name, is_type,
14279 /*complain=*/true);
14281 pop_scope (parser->scope);
14283 parser->qualifying_scope = parser->scope;
14284 parser->object_scope = NULL_TREE;
14286 else if (object_type)
14288 tree object_decl = NULL_TREE;
14289 /* Look up the name in the scope of the OBJECT_TYPE, unless the
14290 OBJECT_TYPE is not a class. */
14291 if (CLASS_TYPE_P (object_type))
14292 /* If the OBJECT_TYPE is a template specialization, it may
14293 be instantiated during name lookup. In that case, errors
14294 may be issued. Even if we rollback the current tentative
14295 parse, those errors are valid. */
14296 object_decl = lookup_member (object_type,
14298 /*protect=*/0, is_type);
14299 /* Look it up in the enclosing context, too. */
14300 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14301 /*block_p=*/true, is_namespace,
14303 parser->object_scope = object_type;
14304 parser->qualifying_scope = NULL_TREE;
14306 decl = object_decl;
14310 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14311 /*block_p=*/true, is_namespace,
14313 parser->qualifying_scope = NULL_TREE;
14314 parser->object_scope = NULL_TREE;
14317 /* If the lookup failed, let our caller know. */
14319 || decl == error_mark_node
14320 || (TREE_CODE (decl) == FUNCTION_DECL
14321 && DECL_ANTICIPATED (decl)))
14322 return error_mark_node;
14324 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
14325 if (TREE_CODE (decl) == TREE_LIST)
14328 *ambiguous_p = true;
14329 /* The error message we have to print is too complicated for
14330 cp_parser_error, so we incorporate its actions directly. */
14331 if (!cp_parser_simulate_error (parser))
14333 error ("reference to `%D' is ambiguous", name);
14334 print_candidates (decl);
14336 return error_mark_node;
14339 gcc_assert (DECL_P (decl)
14340 || TREE_CODE (decl) == OVERLOAD
14341 || TREE_CODE (decl) == SCOPE_REF
14342 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14343 || BASELINK_P (decl));
14345 /* If we have resolved the name of a member declaration, check to
14346 see if the declaration is accessible. When the name resolves to
14347 set of overloaded functions, accessibility is checked when
14348 overload resolution is done.
14350 During an explicit instantiation, access is not checked at all,
14351 as per [temp.explicit]. */
14353 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14358 /* Like cp_parser_lookup_name, but for use in the typical case where
14359 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14360 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
14363 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14365 return cp_parser_lookup_name (parser, name,
14367 /*is_template=*/false,
14368 /*is_namespace=*/false,
14369 /*check_dependency=*/true,
14370 /*ambiguous_p=*/NULL);
14373 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14374 the current context, return the TYPE_DECL. If TAG_NAME_P is
14375 true, the DECL indicates the class being defined in a class-head,
14376 or declared in an elaborated-type-specifier.
14378 Otherwise, return DECL. */
14381 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14383 /* If the TEMPLATE_DECL is being declared as part of a class-head,
14384 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14387 template <typename T> struct B;
14390 template <typename T> struct A::B {};
14392 Similarly, in a elaborated-type-specifier:
14394 namespace N { struct X{}; }
14397 template <typename T> friend struct N::X;
14400 However, if the DECL refers to a class type, and we are in
14401 the scope of the class, then the name lookup automatically
14402 finds the TYPE_DECL created by build_self_reference rather
14403 than a TEMPLATE_DECL. For example, in:
14405 template <class T> struct S {
14409 there is no need to handle such case. */
14411 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14412 return DECL_TEMPLATE_RESULT (decl);
14417 /* If too many, or too few, template-parameter lists apply to the
14418 declarator, issue an error message. Returns TRUE if all went well,
14419 and FALSE otherwise. */
14422 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14423 cp_declarator *declarator)
14425 unsigned num_templates;
14427 /* We haven't seen any classes that involve template parameters yet. */
14430 switch (declarator->kind)
14433 if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14438 scope = TREE_OPERAND (declarator->u.id.name, 0);
14439 member = TREE_OPERAND (declarator->u.id.name, 1);
14441 while (scope && CLASS_TYPE_P (scope))
14443 /* You're supposed to have one `template <...>'
14444 for every template class, but you don't need one
14445 for a full specialization. For example:
14447 template <class T> struct S{};
14448 template <> struct S<int> { void f(); };
14449 void S<int>::f () {}
14451 is correct; there shouldn't be a `template <>' for
14452 the definition of `S<int>::f'. */
14453 if (CLASSTYPE_TEMPLATE_INFO (scope)
14454 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14455 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14456 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14459 scope = TYPE_CONTEXT (scope);
14463 /* If the DECLARATOR has the form `X<y>' then it uses one
14464 additional level of template parameters. */
14465 if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14468 return cp_parser_check_template_parameters (parser,
14474 case cdk_reference:
14476 return (cp_parser_check_declarator_template_parameters
14477 (parser, declarator->declarator));
14483 gcc_unreachable ();
14488 /* NUM_TEMPLATES were used in the current declaration. If that is
14489 invalid, return FALSE and issue an error messages. Otherwise,
14493 cp_parser_check_template_parameters (cp_parser* parser,
14494 unsigned num_templates)
14496 /* If there are more template classes than parameter lists, we have
14499 template <class T> void S<T>::R<T>::f (); */
14500 if (parser->num_template_parameter_lists < num_templates)
14502 error ("too few template-parameter-lists");
14505 /* If there are the same number of template classes and parameter
14506 lists, that's OK. */
14507 if (parser->num_template_parameter_lists == num_templates)
14509 /* If there are more, but only one more, then we are referring to a
14510 member template. That's OK too. */
14511 if (parser->num_template_parameter_lists == num_templates + 1)
14513 /* Otherwise, there are too many template parameter lists. We have
14516 template <class T> template <class U> void S::f(); */
14517 error ("too many template-parameter-lists");
14521 /* Parse a binary-expression of the general form:
14525 binary-expression <token> <expr>
14527 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14528 to parser the <expr>s. If the first production is used, then the
14529 value returned by FN is returned directly. Otherwise, a node with
14530 the indicated EXPR_TYPE is returned, with operands corresponding to
14531 the two sub-expressions. */
14534 cp_parser_binary_expression (cp_parser* parser,
14535 const cp_parser_token_tree_map token_tree_map,
14536 cp_parser_expression_fn fn)
14540 /* Parse the first expression. */
14541 lhs = (*fn) (parser);
14542 /* Now, look for more expressions. */
14546 const cp_parser_token_tree_map_node *map_node;
14549 /* Peek at the next token. */
14550 token = cp_lexer_peek_token (parser->lexer);
14551 /* If the token is `>', and that's not an operator at the
14552 moment, then we're done. */
14553 if (token->type == CPP_GREATER
14554 && !parser->greater_than_is_operator_p)
14556 /* If we find one of the tokens we want, build the corresponding
14557 tree representation. */
14558 for (map_node = token_tree_map;
14559 map_node->token_type != CPP_EOF;
14561 if (map_node->token_type == token->type)
14563 /* Assume that an overloaded operator will not be used. */
14564 bool overloaded_p = false;
14566 /* Consume the operator token. */
14567 cp_lexer_consume_token (parser->lexer);
14568 /* Parse the right-hand side of the expression. */
14569 rhs = (*fn) (parser);
14570 /* Build the binary tree node. */
14571 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14573 /* If the binary operator required the use of an
14574 overloaded operator, then this expression cannot be an
14575 integral constant-expression. An overloaded operator
14576 can be used even if both operands are otherwise
14577 permissible in an integral constant-expression if at
14578 least one of the operands is of enumeration type. */
14580 && (cp_parser_non_integral_constant_expression
14581 (parser, "calls to overloaded operators")))
14582 lhs = error_mark_node;
14586 /* If the token wasn't one of the ones we want, we're done. */
14587 if (map_node->token_type == CPP_EOF)
14594 /* Parse an optional `::' token indicating that the following name is
14595 from the global namespace. If so, PARSER->SCOPE is set to the
14596 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14597 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14598 Returns the new value of PARSER->SCOPE, if the `::' token is
14599 present, and NULL_TREE otherwise. */
14602 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14606 /* Peek at the next token. */
14607 token = cp_lexer_peek_token (parser->lexer);
14608 /* If we're looking at a `::' token then we're starting from the
14609 global namespace, not our current location. */
14610 if (token->type == CPP_SCOPE)
14612 /* Consume the `::' token. */
14613 cp_lexer_consume_token (parser->lexer);
14614 /* Set the SCOPE so that we know where to start the lookup. */
14615 parser->scope = global_namespace;
14616 parser->qualifying_scope = global_namespace;
14617 parser->object_scope = NULL_TREE;
14619 return parser->scope;
14621 else if (!current_scope_valid_p)
14623 parser->scope = NULL_TREE;
14624 parser->qualifying_scope = NULL_TREE;
14625 parser->object_scope = NULL_TREE;
14631 /* Returns TRUE if the upcoming token sequence is the start of a
14632 constructor declarator. If FRIEND_P is true, the declarator is
14633 preceded by the `friend' specifier. */
14636 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14638 bool constructor_p;
14639 tree type_decl = NULL_TREE;
14640 bool nested_name_p;
14641 cp_token *next_token;
14643 /* The common case is that this is not a constructor declarator, so
14644 try to avoid doing lots of work if at all possible. It's not
14645 valid declare a constructor at function scope. */
14646 if (at_function_scope_p ())
14648 /* And only certain tokens can begin a constructor declarator. */
14649 next_token = cp_lexer_peek_token (parser->lexer);
14650 if (next_token->type != CPP_NAME
14651 && next_token->type != CPP_SCOPE
14652 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14653 && next_token->type != CPP_TEMPLATE_ID)
14656 /* Parse tentatively; we are going to roll back all of the tokens
14658 cp_parser_parse_tentatively (parser);
14659 /* Assume that we are looking at a constructor declarator. */
14660 constructor_p = true;
14662 /* Look for the optional `::' operator. */
14663 cp_parser_global_scope_opt (parser,
14664 /*current_scope_valid_p=*/false);
14665 /* Look for the nested-name-specifier. */
14667 = (cp_parser_nested_name_specifier_opt (parser,
14668 /*typename_keyword_p=*/false,
14669 /*check_dependency_p=*/false,
14671 /*is_declaration=*/false)
14673 /* Outside of a class-specifier, there must be a
14674 nested-name-specifier. */
14675 if (!nested_name_p &&
14676 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14678 constructor_p = false;
14679 /* If we still think that this might be a constructor-declarator,
14680 look for a class-name. */
14685 template <typename T> struct S { S(); };
14686 template <typename T> S<T>::S ();
14688 we must recognize that the nested `S' names a class.
14691 template <typename T> S<T>::S<T> ();
14693 we must recognize that the nested `S' names a template. */
14694 type_decl = cp_parser_class_name (parser,
14695 /*typename_keyword_p=*/false,
14696 /*template_keyword_p=*/false,
14698 /*check_dependency_p=*/false,
14699 /*class_head_p=*/false,
14700 /*is_declaration=*/false);
14701 /* If there was no class-name, then this is not a constructor. */
14702 constructor_p = !cp_parser_error_occurred (parser);
14705 /* If we're still considering a constructor, we have to see a `(',
14706 to begin the parameter-declaration-clause, followed by either a
14707 `)', an `...', or a decl-specifier. We need to check for a
14708 type-specifier to avoid being fooled into thinking that:
14712 is a constructor. (It is actually a function named `f' that
14713 takes one parameter (of type `int') and returns a value of type
14716 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14719 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14720 /* A parameter declaration begins with a decl-specifier,
14721 which is either the "attribute" keyword, a storage class
14722 specifier, or (usually) a type-specifier. */
14723 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14724 && !cp_parser_storage_class_specifier_opt (parser))
14727 bool pop_p = false;
14728 unsigned saved_num_template_parameter_lists;
14730 /* Names appearing in the type-specifier should be looked up
14731 in the scope of the class. */
14732 if (current_class_type)
14736 type = TREE_TYPE (type_decl);
14737 if (TREE_CODE (type) == TYPENAME_TYPE)
14739 type = resolve_typename_type (type,
14740 /*only_current_p=*/false);
14741 if (type == error_mark_node)
14743 cp_parser_abort_tentative_parse (parser);
14747 pop_p = push_scope (type);
14750 /* Inside the constructor parameter list, surrounding
14751 template-parameter-lists do not apply. */
14752 saved_num_template_parameter_lists
14753 = parser->num_template_parameter_lists;
14754 parser->num_template_parameter_lists = 0;
14756 /* Look for the type-specifier. */
14757 cp_parser_type_specifier (parser,
14758 CP_PARSER_FLAGS_NONE,
14759 /*decl_specs=*/NULL,
14760 /*is_declarator=*/true,
14761 /*declares_class_or_enum=*/NULL,
14762 /*is_cv_qualifier=*/NULL);
14764 parser->num_template_parameter_lists
14765 = saved_num_template_parameter_lists;
14767 /* Leave the scope of the class. */
14771 constructor_p = !cp_parser_error_occurred (parser);
14775 constructor_p = false;
14776 /* We did not really want to consume any tokens. */
14777 cp_parser_abort_tentative_parse (parser);
14779 return constructor_p;
14782 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14783 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14784 they must be performed once we are in the scope of the function.
14786 Returns the function defined. */
14789 cp_parser_function_definition_from_specifiers_and_declarator
14790 (cp_parser* parser,
14791 cp_decl_specifier_seq *decl_specifiers,
14793 const cp_declarator *declarator)
14798 /* Begin the function-definition. */
14799 success_p = start_function (decl_specifiers, declarator, attributes);
14801 /* The things we're about to see are not directly qualified by any
14802 template headers we've seen thus far. */
14803 reset_specialization ();
14805 /* If there were names looked up in the decl-specifier-seq that we
14806 did not check, check them now. We must wait until we are in the
14807 scope of the function to perform the checks, since the function
14808 might be a friend. */
14809 perform_deferred_access_checks ();
14813 /* Skip the entire function. */
14814 error ("invalid function declaration");
14815 cp_parser_skip_to_end_of_block_or_statement (parser);
14816 fn = error_mark_node;
14819 fn = cp_parser_function_definition_after_declarator (parser,
14820 /*inline_p=*/false);
14825 /* Parse the part of a function-definition that follows the
14826 declarator. INLINE_P is TRUE iff this function is an inline
14827 function defined with a class-specifier.
14829 Returns the function defined. */
14832 cp_parser_function_definition_after_declarator (cp_parser* parser,
14836 bool ctor_initializer_p = false;
14837 bool saved_in_unbraced_linkage_specification_p;
14838 unsigned saved_num_template_parameter_lists;
14840 /* If the next token is `return', then the code may be trying to
14841 make use of the "named return value" extension that G++ used to
14843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14845 /* Consume the `return' keyword. */
14846 cp_lexer_consume_token (parser->lexer);
14847 /* Look for the identifier that indicates what value is to be
14849 cp_parser_identifier (parser);
14850 /* Issue an error message. */
14851 error ("named return values are no longer supported");
14852 /* Skip tokens until we reach the start of the function body. */
14853 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14854 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14855 cp_lexer_consume_token (parser->lexer);
14857 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14858 anything declared inside `f'. */
14859 saved_in_unbraced_linkage_specification_p
14860 = parser->in_unbraced_linkage_specification_p;
14861 parser->in_unbraced_linkage_specification_p = false;
14862 /* Inside the function, surrounding template-parameter-lists do not
14864 saved_num_template_parameter_lists
14865 = parser->num_template_parameter_lists;
14866 parser->num_template_parameter_lists = 0;
14867 /* If the next token is `try', then we are looking at a
14868 function-try-block. */
14869 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14870 ctor_initializer_p = cp_parser_function_try_block (parser);
14871 /* A function-try-block includes the function-body, so we only do
14872 this next part if we're not processing a function-try-block. */
14875 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14877 /* Finish the function. */
14878 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14879 (inline_p ? 2 : 0));
14880 /* Generate code for it, if necessary. */
14881 expand_or_defer_fn (fn);
14882 /* Restore the saved values. */
14883 parser->in_unbraced_linkage_specification_p
14884 = saved_in_unbraced_linkage_specification_p;
14885 parser->num_template_parameter_lists
14886 = saved_num_template_parameter_lists;
14891 /* Parse a template-declaration, assuming that the `export' (and
14892 `extern') keywords, if present, has already been scanned. MEMBER_P
14893 is as for cp_parser_template_declaration. */
14896 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14898 tree decl = NULL_TREE;
14899 tree parameter_list;
14900 bool friend_p = false;
14902 /* Look for the `template' keyword. */
14903 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14907 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14910 /* If the next token is `>', then we have an invalid
14911 specialization. Rather than complain about an invalid template
14912 parameter, issue an error message here. */
14913 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14915 cp_parser_error (parser, "invalid explicit specialization");
14916 begin_specialization ();
14917 parameter_list = NULL_TREE;
14921 /* Parse the template parameters. */
14922 begin_template_parm_list ();
14923 parameter_list = cp_parser_template_parameter_list (parser);
14924 parameter_list = end_template_parm_list (parameter_list);
14927 /* Look for the `>'. */
14928 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14929 /* We just processed one more parameter list. */
14930 ++parser->num_template_parameter_lists;
14931 /* If the next token is `template', there are more template
14933 if (cp_lexer_next_token_is_keyword (parser->lexer,
14935 cp_parser_template_declaration_after_export (parser, member_p);
14938 /* There are no access checks when parsing a template, as we do not
14939 know if a specialization will be a friend. */
14940 push_deferring_access_checks (dk_no_check);
14942 decl = cp_parser_single_declaration (parser,
14946 pop_deferring_access_checks ();
14948 /* If this is a member template declaration, let the front
14950 if (member_p && !friend_p && decl)
14952 if (TREE_CODE (decl) == TYPE_DECL)
14953 cp_parser_check_access_in_redeclaration (decl);
14955 decl = finish_member_template_decl (decl);
14957 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14958 make_friend_class (current_class_type, TREE_TYPE (decl),
14959 /*complain=*/true);
14961 /* We are done with the current parameter list. */
14962 --parser->num_template_parameter_lists;
14965 finish_template_decl (parameter_list);
14967 /* Register member declarations. */
14968 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14969 finish_member_declaration (decl);
14971 /* If DECL is a function template, we must return to parse it later.
14972 (Even though there is no definition, there might be default
14973 arguments that need handling.) */
14974 if (member_p && decl
14975 && (TREE_CODE (decl) == FUNCTION_DECL
14976 || DECL_FUNCTION_TEMPLATE_P (decl)))
14977 TREE_VALUE (parser->unparsed_functions_queues)
14978 = tree_cons (NULL_TREE, decl,
14979 TREE_VALUE (parser->unparsed_functions_queues));
14982 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14983 `function-definition' sequence. MEMBER_P is true, this declaration
14984 appears in a class scope.
14986 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14987 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14990 cp_parser_single_declaration (cp_parser* parser,
14994 int declares_class_or_enum;
14995 tree decl = NULL_TREE;
14996 cp_decl_specifier_seq decl_specifiers;
14997 bool function_definition_p = false;
14999 /* Defer access checks until we know what is being declared. */
15000 push_deferring_access_checks (dk_deferred);
15002 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15004 cp_parser_decl_specifier_seq (parser,
15005 CP_PARSER_FLAGS_OPTIONAL,
15007 &declares_class_or_enum);
15009 *friend_p = cp_parser_friend_p (&decl_specifiers);
15010 /* Gather up the access checks that occurred the
15011 decl-specifier-seq. */
15012 stop_deferring_access_checks ();
15014 /* Check for the declaration of a template class. */
15015 if (declares_class_or_enum)
15017 if (cp_parser_declares_only_class_p (parser))
15019 decl = shadow_tag (&decl_specifiers);
15020 if (decl && decl != error_mark_node)
15021 decl = TYPE_NAME (decl);
15023 decl = error_mark_node;
15028 /* If it's not a template class, try for a template function. If
15029 the next token is a `;', then this declaration does not declare
15030 anything. But, if there were errors in the decl-specifiers, then
15031 the error might well have come from an attempted class-specifier.
15032 In that case, there's no need to warn about a missing declarator. */
15034 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15035 || decl_specifiers.type != error_mark_node))
15036 decl = cp_parser_init_declarator (parser,
15038 /*function_definition_allowed_p=*/true,
15040 declares_class_or_enum,
15041 &function_definition_p);
15043 pop_deferring_access_checks ();
15045 /* Clear any current qualification; whatever comes next is the start
15046 of something new. */
15047 parser->scope = NULL_TREE;
15048 parser->qualifying_scope = NULL_TREE;
15049 parser->object_scope = NULL_TREE;
15050 /* Look for a trailing `;' after the declaration. */
15051 if (!function_definition_p
15052 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
15053 cp_parser_skip_to_end_of_block_or_statement (parser);
15058 /* Parse a cast-expression that is not the operand of a unary "&". */
15061 cp_parser_simple_cast_expression (cp_parser *parser)
15063 return cp_parser_cast_expression (parser, /*address_p=*/false);
15066 /* Parse a functional cast to TYPE. Returns an expression
15067 representing the cast. */
15070 cp_parser_functional_cast (cp_parser* parser, tree type)
15072 tree expression_list;
15076 = cp_parser_parenthesized_expression_list (parser, false,
15077 /*non_constant_p=*/NULL);
15079 cast = build_functional_cast (type, expression_list);
15080 /* [expr.const]/1: In an integral constant expression "only type
15081 conversions to integral or enumeration type can be used". */
15082 if (cast != error_mark_node && !type_dependent_expression_p (type)
15083 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15085 if (cp_parser_non_integral_constant_expression
15086 (parser, "a call to a constructor"))
15087 return error_mark_node;
15092 /* Save the tokens that make up the body of a member function defined
15093 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
15094 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
15095 specifiers applied to the declaration. Returns the FUNCTION_DECL
15096 for the member function. */
15099 cp_parser_save_member_function_body (cp_parser* parser,
15100 cp_decl_specifier_seq *decl_specifiers,
15101 cp_declarator *declarator,
15104 cp_token_cache *cache;
15107 /* Create the function-declaration. */
15108 fn = start_method (decl_specifiers, declarator, attributes);
15109 /* If something went badly wrong, bail out now. */
15110 if (fn == error_mark_node)
15112 /* If there's a function-body, skip it. */
15113 if (cp_parser_token_starts_function_definition_p
15114 (cp_lexer_peek_token (parser->lexer)))
15115 cp_parser_skip_to_end_of_block_or_statement (parser);
15116 return error_mark_node;
15119 /* Remember it, if there default args to post process. */
15120 cp_parser_save_default_args (parser, fn);
15122 /* Create a token cache. */
15123 cache = cp_token_cache_new ();
15124 /* Save away the tokens that make up the body of the
15126 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15127 /* Handle function try blocks. */
15128 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15129 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
15131 /* Save away the inline definition; we will process it when the
15132 class is complete. */
15133 DECL_PENDING_INLINE_INFO (fn) = cache;
15134 DECL_PENDING_INLINE_P (fn) = 1;
15136 /* We need to know that this was defined in the class, so that
15137 friend templates are handled correctly. */
15138 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15140 /* We're done with the inline definition. */
15141 finish_method (fn);
15143 /* Add FN to the queue of functions to be parsed later. */
15144 TREE_VALUE (parser->unparsed_functions_queues)
15145 = tree_cons (NULL_TREE, fn,
15146 TREE_VALUE (parser->unparsed_functions_queues));
15151 /* Parse a template-argument-list, as well as the trailing ">" (but
15152 not the opening ">"). See cp_parser_template_argument_list for the
15156 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15160 tree saved_qualifying_scope;
15161 tree saved_object_scope;
15162 bool saved_greater_than_is_operator_p;
15166 When parsing a template-id, the first non-nested `>' is taken as
15167 the end of the template-argument-list rather than a greater-than
15169 saved_greater_than_is_operator_p
15170 = parser->greater_than_is_operator_p;
15171 parser->greater_than_is_operator_p = false;
15172 /* Parsing the argument list may modify SCOPE, so we save it
15174 saved_scope = parser->scope;
15175 saved_qualifying_scope = parser->qualifying_scope;
15176 saved_object_scope = parser->object_scope;
15177 /* Parse the template-argument-list itself. */
15178 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15179 arguments = NULL_TREE;
15181 arguments = cp_parser_template_argument_list (parser);
15182 /* Look for the `>' that ends the template-argument-list. If we find
15183 a '>>' instead, it's probably just a typo. */
15184 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15186 if (!saved_greater_than_is_operator_p)
15188 /* If we're in a nested template argument list, the '>>' has to be
15189 a typo for '> >'. We emit the error message, but we continue
15190 parsing and we push a '>' as next token, so that the argument
15191 list will be parsed correctly.. */
15193 error ("`>>' should be `> >' within a nested template argument list");
15194 token = cp_lexer_peek_token (parser->lexer);
15195 token->type = CPP_GREATER;
15199 /* If this is not a nested template argument list, the '>>' is
15200 a typo for '>'. Emit an error message and continue. */
15201 error ("spurious `>>', use `>' to terminate a template argument list");
15202 cp_lexer_consume_token (parser->lexer);
15205 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
15206 error ("missing `>' to terminate the template argument list");
15207 /* The `>' token might be a greater-than operator again now. */
15208 parser->greater_than_is_operator_p
15209 = saved_greater_than_is_operator_p;
15210 /* Restore the SAVED_SCOPE. */
15211 parser->scope = saved_scope;
15212 parser->qualifying_scope = saved_qualifying_scope;
15213 parser->object_scope = saved_object_scope;
15218 /* MEMBER_FUNCTION is a member function, or a friend. If default
15219 arguments, or the body of the function have not yet been parsed,
15223 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15225 cp_lexer *saved_lexer;
15227 /* If this member is a template, get the underlying
15229 if (DECL_FUNCTION_TEMPLATE_P (member_function))
15230 member_function = DECL_TEMPLATE_RESULT (member_function);
15232 /* There should not be any class definitions in progress at this
15233 point; the bodies of members are only parsed outside of all class
15235 gcc_assert (parser->num_classes_being_defined == 0);
15236 /* While we're parsing the member functions we might encounter more
15237 classes. We want to handle them right away, but we don't want
15238 them getting mixed up with functions that are currently in the
15240 parser->unparsed_functions_queues
15241 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15243 /* Make sure that any template parameters are in scope. */
15244 maybe_begin_member_template_processing (member_function);
15246 /* If the body of the function has not yet been parsed, parse it
15248 if (DECL_PENDING_INLINE_P (member_function))
15250 tree function_scope;
15251 cp_token_cache *tokens;
15253 /* The function is no longer pending; we are processing it. */
15254 tokens = DECL_PENDING_INLINE_INFO (member_function);
15255 DECL_PENDING_INLINE_INFO (member_function) = NULL;
15256 DECL_PENDING_INLINE_P (member_function) = 0;
15257 /* If this was an inline function in a local class, enter the scope
15258 of the containing function. */
15259 function_scope = decl_function_context (member_function);
15260 if (function_scope)
15261 push_function_context_to (function_scope);
15263 /* Save away the current lexer. */
15264 saved_lexer = parser->lexer;
15265 /* Make a new lexer to feed us the tokens saved for this function. */
15266 parser->lexer = cp_lexer_new_from_tokens (tokens);
15267 parser->lexer->next = saved_lexer;
15269 /* Set the current source position to be the location of the first
15270 token in the saved inline body. */
15271 cp_lexer_peek_token (parser->lexer);
15273 /* Let the front end know that we going to be defining this
15275 start_preparsed_function (member_function, NULL_TREE,
15276 SF_PRE_PARSED | SF_INCLASS_INLINE);
15278 /* Now, parse the body of the function. */
15279 cp_parser_function_definition_after_declarator (parser,
15280 /*inline_p=*/true);
15282 /* Leave the scope of the containing function. */
15283 if (function_scope)
15284 pop_function_context_from (function_scope);
15285 /* Restore the lexer. */
15286 parser->lexer = saved_lexer;
15289 /* Remove any template parameters from the symbol table. */
15290 maybe_end_member_template_processing ();
15292 /* Restore the queue. */
15293 parser->unparsed_functions_queues
15294 = TREE_CHAIN (parser->unparsed_functions_queues);
15297 /* If DECL contains any default args, remember it on the unparsed
15298 functions queue. */
15301 cp_parser_save_default_args (cp_parser* parser, tree decl)
15305 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15307 probe = TREE_CHAIN (probe))
15308 if (TREE_PURPOSE (probe))
15310 TREE_PURPOSE (parser->unparsed_functions_queues)
15311 = tree_cons (current_class_type, decl,
15312 TREE_PURPOSE (parser->unparsed_functions_queues));
15318 /* FN is a FUNCTION_DECL which may contains a parameter with an
15319 unparsed DEFAULT_ARG. Parse the default args now. This function
15320 assumes that the current scope is the scope in which the default
15321 argument should be processed. */
15324 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15326 cp_lexer *saved_lexer;
15327 cp_token_cache *tokens;
15328 bool saved_local_variables_forbidden_p;
15331 /* While we're parsing the default args, we might (due to the
15332 statement expression extension) encounter more classes. We want
15333 to handle them right away, but we don't want them getting mixed
15334 up with default args that are currently in the queue. */
15335 parser->unparsed_functions_queues
15336 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15338 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
15340 parameters = TREE_CHAIN (parameters))
15342 if (!TREE_PURPOSE (parameters)
15343 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
15346 /* Save away the current lexer. */
15347 saved_lexer = parser->lexer;
15348 /* Create a new one, using the tokens we have saved. */
15349 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
15350 parser->lexer = cp_lexer_new_from_tokens (tokens);
15352 /* Set the current source position to be the location of the
15353 first token in the default argument. */
15354 cp_lexer_peek_token (parser->lexer);
15356 /* Local variable names (and the `this' keyword) may not appear
15357 in a default argument. */
15358 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15359 parser->local_variables_forbidden_p = true;
15360 /* Parse the assignment-expression. */
15361 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
15363 /* If the token stream has not been completely used up, then
15364 there was extra junk after the end of the default
15366 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15367 cp_parser_error (parser, "expected `,'");
15369 /* Restore saved state. */
15370 parser->lexer = saved_lexer;
15371 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15374 /* Restore the queue. */
15375 parser->unparsed_functions_queues
15376 = TREE_CHAIN (parser->unparsed_functions_queues);
15379 /* Parse the operand of `sizeof' (or a similar operator). Returns
15380 either a TYPE or an expression, depending on the form of the
15381 input. The KEYWORD indicates which kind of expression we have
15385 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15387 static const char *format;
15388 tree expr = NULL_TREE;
15389 const char *saved_message;
15390 bool saved_integral_constant_expression_p;
15392 /* Initialize FORMAT the first time we get here. */
15394 format = "types may not be defined in `%s' expressions";
15396 /* Types cannot be defined in a `sizeof' expression. Save away the
15398 saved_message = parser->type_definition_forbidden_message;
15399 /* And create the new one. */
15400 parser->type_definition_forbidden_message
15401 = xmalloc (strlen (format)
15402 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15404 sprintf ((char *) parser->type_definition_forbidden_message,
15405 format, IDENTIFIER_POINTER (ridpointers[keyword]));
15407 /* The restrictions on constant-expressions do not apply inside
15408 sizeof expressions. */
15409 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15410 parser->integral_constant_expression_p = false;
15412 /* Do not actually evaluate the expression. */
15414 /* If it's a `(', then we might be looking at the type-id
15416 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15419 bool saved_in_type_id_in_expr_p;
15421 /* We can't be sure yet whether we're looking at a type-id or an
15423 cp_parser_parse_tentatively (parser);
15424 /* Consume the `('. */
15425 cp_lexer_consume_token (parser->lexer);
15426 /* Parse the type-id. */
15427 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15428 parser->in_type_id_in_expr_p = true;
15429 type = cp_parser_type_id (parser);
15430 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15431 /* Now, look for the trailing `)'. */
15432 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15433 /* If all went well, then we're done. */
15434 if (cp_parser_parse_definitely (parser))
15436 cp_decl_specifier_seq decl_specs;
15438 /* Build a trivial decl-specifier-seq. */
15439 clear_decl_specs (&decl_specs);
15440 decl_specs.type = type;
15442 /* Call grokdeclarator to figure out what type this is. */
15443 expr = grokdeclarator (NULL,
15447 /*attrlist=*/NULL);
15451 /* If the type-id production did not work out, then we must be
15452 looking at the unary-expression production. */
15454 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15455 /* Go back to evaluating expressions. */
15458 /* Free the message we created. */
15459 free ((char *) parser->type_definition_forbidden_message);
15460 /* And restore the old one. */
15461 parser->type_definition_forbidden_message = saved_message;
15462 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15467 /* If the current declaration has no declarator, return true. */
15470 cp_parser_declares_only_class_p (cp_parser *parser)
15472 /* If the next token is a `;' or a `,' then there is no
15474 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15475 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15478 /* Update the DECL_SPECS to reflect the STORAGE_CLASS. */
15481 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15482 cp_storage_class storage_class)
15484 if (decl_specs->storage_class != sc_none)
15485 decl_specs->multiple_storage_classes_p = true;
15487 decl_specs->storage_class = storage_class;
15490 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
15491 is true, the type is a user-defined type; otherwise it is a
15492 built-in type specified by a keyword. */
15495 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15497 bool user_defined_p)
15499 decl_specs->any_specifiers_p = true;
15501 /* If the user tries to redeclare a built-in type (with, for example,
15502 in "typedef int wchar_t;") we remember that this is what
15503 happened. In system headers, we ignore these declarations so
15504 that G++ can work with system headers that are not C++-safe. */
15505 if (decl_specs->specs[(int) ds_typedef]
15507 && (decl_specs->type
15508 || decl_specs->specs[(int) ds_long]
15509 || decl_specs->specs[(int) ds_short]
15510 || decl_specs->specs[(int) ds_unsigned]
15511 || decl_specs->specs[(int) ds_signed]))
15513 decl_specs->redefined_builtin_type = type_spec;
15514 if (!decl_specs->type)
15516 decl_specs->type = type_spec;
15517 decl_specs->user_defined_type_p = false;
15520 else if (decl_specs->type)
15521 decl_specs->multiple_types_p = true;
15524 decl_specs->type = type_spec;
15525 decl_specs->user_defined_type_p = user_defined_p;
15526 decl_specs->redefined_builtin_type = NULL_TREE;
15530 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15531 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15534 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15536 return decl_specifiers->specs[(int) ds_friend] != 0;
15539 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15540 issue an error message indicating that TOKEN_DESC was expected.
15542 Returns the token consumed, if the token had the appropriate type.
15543 Otherwise, returns NULL. */
15546 cp_parser_require (cp_parser* parser,
15547 enum cpp_ttype type,
15548 const char* token_desc)
15550 if (cp_lexer_next_token_is (parser->lexer, type))
15551 return cp_lexer_consume_token (parser->lexer);
15554 /* Output the MESSAGE -- unless we're parsing tentatively. */
15555 if (!cp_parser_simulate_error (parser))
15557 char *message = concat ("expected ", token_desc, NULL);
15558 cp_parser_error (parser, message);
15565 /* Like cp_parser_require, except that tokens will be skipped until
15566 the desired token is found. An error message is still produced if
15567 the next token is not as expected. */
15570 cp_parser_skip_until_found (cp_parser* parser,
15571 enum cpp_ttype type,
15572 const char* token_desc)
15575 unsigned nesting_depth = 0;
15577 if (cp_parser_require (parser, type, token_desc))
15580 /* Skip tokens until the desired token is found. */
15583 /* Peek at the next token. */
15584 token = cp_lexer_peek_token (parser->lexer);
15585 /* If we've reached the token we want, consume it and
15587 if (token->type == type && !nesting_depth)
15589 cp_lexer_consume_token (parser->lexer);
15592 /* If we've run out of tokens, stop. */
15593 if (token->type == CPP_EOF)
15595 if (token->type == CPP_OPEN_BRACE
15596 || token->type == CPP_OPEN_PAREN
15597 || token->type == CPP_OPEN_SQUARE)
15599 else if (token->type == CPP_CLOSE_BRACE
15600 || token->type == CPP_CLOSE_PAREN
15601 || token->type == CPP_CLOSE_SQUARE)
15603 if (nesting_depth-- == 0)
15606 /* Consume this token. */
15607 cp_lexer_consume_token (parser->lexer);
15611 /* If the next token is the indicated keyword, consume it. Otherwise,
15612 issue an error message indicating that TOKEN_DESC was expected.
15614 Returns the token consumed, if the token had the appropriate type.
15615 Otherwise, returns NULL. */
15618 cp_parser_require_keyword (cp_parser* parser,
15620 const char* token_desc)
15622 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15624 if (token && token->keyword != keyword)
15626 dyn_string_t error_msg;
15628 /* Format the error message. */
15629 error_msg = dyn_string_new (0);
15630 dyn_string_append_cstr (error_msg, "expected ");
15631 dyn_string_append_cstr (error_msg, token_desc);
15632 cp_parser_error (parser, error_msg->s);
15633 dyn_string_delete (error_msg);
15640 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15641 function-definition. */
15644 cp_parser_token_starts_function_definition_p (cp_token* token)
15646 return (/* An ordinary function-body begins with an `{'. */
15647 token->type == CPP_OPEN_BRACE
15648 /* A ctor-initializer begins with a `:'. */
15649 || token->type == CPP_COLON
15650 /* A function-try-block begins with `try'. */
15651 || token->keyword == RID_TRY
15652 /* The named return value extension begins with `return'. */
15653 || token->keyword == RID_RETURN);
15656 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15660 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15664 token = cp_lexer_peek_token (parser->lexer);
15665 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15668 /* Returns TRUE iff the next token is the "," or ">" ending a
15669 template-argument. ">>" is also accepted (after the full
15670 argument was parsed) because it's probably a typo for "> >",
15671 and there is a specific diagnostic for this. */
15674 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15678 token = cp_lexer_peek_token (parser->lexer);
15679 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15680 || token->type == CPP_RSHIFT);
15683 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15684 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15687 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15692 token = cp_lexer_peek_nth_token (parser->lexer, n);
15693 if (token->type == CPP_LESS)
15695 /* Check for the sequence `<::' in the original code. It would be lexed as
15696 `[:', where `[' is a digraph, and there is no whitespace before
15698 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15701 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15702 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15708 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15709 or none_type otherwise. */
15711 static enum tag_types
15712 cp_parser_token_is_class_key (cp_token* token)
15714 switch (token->keyword)
15719 return record_type;
15728 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15731 cp_parser_check_class_key (enum tag_types class_key, tree type)
15733 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15734 pedwarn ("`%s' tag used in naming `%#T'",
15735 class_key == union_type ? "union"
15736 : class_key == record_type ? "struct" : "class",
15740 /* Issue an error message if DECL is redeclared with different
15741 access than its original declaration [class.access.spec/3].
15742 This applies to nested classes and nested class templates.
15745 static void cp_parser_check_access_in_redeclaration (tree decl)
15747 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15750 if ((TREE_PRIVATE (decl)
15751 != (current_access_specifier == access_private_node))
15752 || (TREE_PROTECTED (decl)
15753 != (current_access_specifier == access_protected_node)))
15754 error ("%D redeclared with different access", decl);
15757 /* Look for the `template' keyword, as a syntactic disambiguator.
15758 Return TRUE iff it is present, in which case it will be
15762 cp_parser_optional_template_keyword (cp_parser *parser)
15764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15766 /* The `template' keyword can only be used within templates;
15767 outside templates the parser can always figure out what is a
15768 template and what is not. */
15769 if (!processing_template_decl)
15771 error ("`template' (as a disambiguator) is only allowed "
15772 "within templates");
15773 /* If this part of the token stream is rescanned, the same
15774 error message would be generated. So, we purge the token
15775 from the stream. */
15776 cp_lexer_purge_token (parser->lexer);
15781 /* Consume the `template' keyword. */
15782 cp_lexer_consume_token (parser->lexer);
15790 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15791 set PARSER->SCOPE, and perform other related actions. */
15794 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15799 /* Get the stored value. */
15800 value = cp_lexer_consume_token (parser->lexer)->value;
15801 /* Perform any access checks that were deferred. */
15802 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15803 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15804 /* Set the scope from the stored value. */
15805 parser->scope = TREE_VALUE (value);
15806 parser->qualifying_scope = TREE_TYPE (value);
15807 parser->object_scope = NULL_TREE;
15810 /* Add tokens to CACHE until a non-nested END token appears. */
15813 cp_parser_cache_group_1 (cp_parser *parser,
15814 cp_token_cache *cache,
15815 enum cpp_ttype end,
15822 /* Abort a parenthesized expression if we encounter a brace. */
15823 if ((end == CPP_CLOSE_PAREN || depth == 0)
15824 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15826 /* If we've reached the end of the file, stop. */
15827 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15829 /* Consume the next token. */
15830 token = cp_lexer_consume_token (parser->lexer);
15831 /* Add this token to the tokens we are saving. */
15832 cp_token_cache_push_token (cache, token);
15833 /* See if it starts a new group. */
15834 if (token->type == CPP_OPEN_BRACE)
15836 cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15840 else if (token->type == CPP_OPEN_PAREN)
15841 cp_parser_cache_group_1 (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15842 else if (token->type == end)
15847 /* Convenient interface for cp_parser_cache_group_1 that makes sure we
15848 preserve string tokens in both translated and untranslated
15852 cp_parser_cache_group (cp_parser *parser,
15853 cp_token_cache *cache,
15854 enum cpp_ttype end,
15857 int saved_c_lex_string_translate;
15859 saved_c_lex_string_translate = c_lex_string_translate;
15860 c_lex_string_translate = -1;
15862 cp_parser_cache_group_1 (parser, cache, end, depth);
15864 c_lex_string_translate = saved_c_lex_string_translate;
15868 /* Begin parsing tentatively. We always save tokens while parsing
15869 tentatively so that if the tentative parsing fails we can restore the
15873 cp_parser_parse_tentatively (cp_parser* parser)
15875 /* Enter a new parsing context. */
15876 parser->context = cp_parser_context_new (parser->context);
15877 /* Begin saving tokens. */
15878 cp_lexer_save_tokens (parser->lexer);
15879 /* In order to avoid repetitive access control error messages,
15880 access checks are queued up until we are no longer parsing
15882 push_deferring_access_checks (dk_deferred);
15885 /* Commit to the currently active tentative parse. */
15888 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15890 cp_parser_context *context;
15893 /* Mark all of the levels as committed. */
15894 lexer = parser->lexer;
15895 for (context = parser->context; context->next; context = context->next)
15897 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15899 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15900 while (!cp_lexer_saving_tokens (lexer))
15901 lexer = lexer->next;
15902 cp_lexer_commit_tokens (lexer);
15906 /* Abort the currently active tentative parse. All consumed tokens
15907 will be rolled back, and no diagnostics will be issued. */
15910 cp_parser_abort_tentative_parse (cp_parser* parser)
15912 cp_parser_simulate_error (parser);
15913 /* Now, pretend that we want to see if the construct was
15914 successfully parsed. */
15915 cp_parser_parse_definitely (parser);
15918 /* Stop parsing tentatively. If a parse error has occurred, restore the
15919 token stream. Otherwise, commit to the tokens we have consumed.
15920 Returns true if no error occurred; false otherwise. */
15923 cp_parser_parse_definitely (cp_parser* parser)
15925 bool error_occurred;
15926 cp_parser_context *context;
15928 /* Remember whether or not an error occurred, since we are about to
15929 destroy that information. */
15930 error_occurred = cp_parser_error_occurred (parser);
15931 /* Remove the topmost context from the stack. */
15932 context = parser->context;
15933 parser->context = context->next;
15934 /* If no parse errors occurred, commit to the tentative parse. */
15935 if (!error_occurred)
15937 /* Commit to the tokens read tentatively, unless that was
15939 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15940 cp_lexer_commit_tokens (parser->lexer);
15942 pop_to_parent_deferring_access_checks ();
15944 /* Otherwise, if errors occurred, roll back our state so that things
15945 are just as they were before we began the tentative parse. */
15948 cp_lexer_rollback_tokens (parser->lexer);
15949 pop_deferring_access_checks ();
15951 /* Add the context to the front of the free list. */
15952 context->next = cp_parser_context_free_list;
15953 cp_parser_context_free_list = context;
15955 return !error_occurred;
15958 /* Returns true if we are parsing tentatively -- but have decided that
15959 we will stick with this tentative parse, even if errors occur. */
15962 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15964 return (cp_parser_parsing_tentatively (parser)
15965 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15968 /* Returns nonzero iff an error has occurred during the most recent
15969 tentative parse. */
15972 cp_parser_error_occurred (cp_parser* parser)
15974 return (cp_parser_parsing_tentatively (parser)
15975 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15978 /* Returns nonzero if GNU extensions are allowed. */
15981 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15983 return parser->allow_gnu_extensions_p;
15989 static GTY (()) cp_parser *the_parser;
15991 /* External interface. */
15993 /* Parse one entire translation unit. */
15996 c_parse_file (void)
15998 bool error_occurred;
15999 static bool already_called = false;
16001 if (already_called)
16003 sorry ("inter-module optimizations not implemented for C++");
16006 already_called = true;
16008 the_parser = cp_parser_new ();
16009 push_deferring_access_checks (flag_access_control
16010 ? dk_no_deferred : dk_no_check);
16011 error_occurred = cp_parser_translation_unit (the_parser);
16015 /* This variable must be provided by every front end. */
16019 #include "gt-cp-parser.h"