svn merge -r102224:107263 svn+ssh://gcc.gnu.org/svn/gcc/branches/gcc-3_4-branch
[official-gcc.git] / gcc / cp / parser.c
blobc04ba670531642bdaf52d4c054d859e4c1e5f89b
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
40 /* The lexer. */
42 /* Overview
43 --------
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
46 look-ahead.
48 Methodology
49 -----------
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
65 circular buffer. */
67 /* A C++ token. */
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;
76 /* Token flags. */
77 unsigned char flags;
78 /* The value associated with this token, if any. */
79 tree value;
80 /* The location at which this token was found. */
81 location_t location;
82 } cp_token;
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
93 complete.)
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 (())
102 /* The tokens. */
103 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
104 /* The number of tokens in this block. */
105 size_t num_tokens;
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;
110 } cp_token_block;
112 typedef struct cp_token_cache GTY (())
114 /* The first block in the cache. NULL if there are no tokens in the
115 cache. */
116 cp_token_block *first;
117 /* The last block in the cache. NULL If there are no tokens in the
118 cache. */
119 cp_token_block *last;
120 } cp_token_cache;
122 /* Prototypes. */
124 static cp_token_cache *cp_token_cache_new
125 (void);
126 static void cp_token_cache_push_token
127 (cp_token_cache *, cp_token *);
129 /* Create a new cp_token_cache. */
131 static cp_token_cache *
132 cp_token_cache_new (void)
134 return ggc_alloc_cleared (sizeof (cp_token_cache));
137 /* Add *TOKEN to *CACHE. */
139 static void
140 cp_token_cache_push_token (cp_token_cache *cache,
141 cp_token *token)
143 cp_token_block *b = cache->last;
145 /* See if we need to allocate a new token block. */
146 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
148 b = ggc_alloc_cleared (sizeof (cp_token_block));
149 b->prev = cache->last;
150 if (cache->last)
152 cache->last->next = b;
153 cache->last = b;
155 else
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
164 it to the parser. */
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
201 string literal. */
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. */
206 bool main_lexer_p;
208 /* True if we should output debugging information. */
209 bool debugging_p;
211 /* The next lexer in a linked list of lexers. */
212 struct cp_lexer *next;
213 } cp_lexer;
215 /* Prototypes. */
217 static cp_lexer *cp_lexer_new_main
218 (void);
219 static cp_lexer *cp_lexer_new_from_tokens
220 (struct cp_token_cache *);
221 static int cp_lexer_saving_tokens
222 (const cp_lexer *);
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
230 (cp_lexer *);
231 static void cp_lexer_maybe_grow_buffer
232 (cp_lexer *);
233 static void cp_lexer_get_preprocessor_token
234 (cp_lexer *, cp_token *);
235 static cp_token *cp_lexer_peek_token
236 (cp_lexer *);
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
246 (cp_lexer *);
247 static void cp_lexer_purge_token
248 (cp_lexer *);
249 static void cp_lexer_purge_tokens_after
250 (cp_lexer *, cp_token *);
251 static void cp_lexer_save_tokens
252 (cp_lexer *);
253 static void cp_lexer_commit_tokens
254 (cp_lexer *);
255 static void cp_lexer_rollback_tokens
256 (cp_lexer *);
257 static inline void cp_lexer_set_source_position_from_token
258 (cp_lexer *, const cp_token *);
259 static void cp_lexer_print_token
260 (FILE *, cp_token *);
261 static inline bool cp_lexer_debugging_p
262 (cp_lexer *);
263 static void cp_lexer_start_debugging
264 (cp_lexer *) ATTRIBUTE_UNUSED;
265 static void cp_lexer_stop_debugging
266 (cp_lexer *) ATTRIBUTE_UNUSED;
268 /* Manifest constants. */
270 #define CP_TOKEN_BUFFER_SIZE 5
271 #define CP_SAVED_TOKENS_SIZE 5
273 /* A token type for keywords, as opposed to ordinary identifiers. */
274 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
276 /* A token type for template-ids. If a template-id is processed while
277 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
278 the value of the CPP_TEMPLATE_ID is whatever was returned by
279 cp_parser_template_id. */
280 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
282 /* A token type for nested-name-specifiers. If a
283 nested-name-specifier is processed while parsing tentatively, it is
284 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
285 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
286 cp_parser_nested_name_specifier_opt. */
287 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
289 /* A token type for tokens that are not tokens at all; these are used
290 to mark the end of a token block. */
291 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
293 /* Variables. */
295 /* The stream to which debugging output should be written. */
296 static FILE *cp_lexer_debug_stream;
298 /* Create a new main C++ lexer, the lexer that gets tokens from the
299 preprocessor. */
301 static cp_lexer *
302 cp_lexer_new_main (void)
304 cp_lexer *lexer;
305 cp_token first_token;
307 /* It's possible that lexing the first token will load a PCH file,
308 which is a GC collection point. So we have to grab the first
309 token before allocating any memory. */
310 cp_lexer_get_preprocessor_token (NULL, &first_token);
311 c_common_no_more_pch ();
313 /* Allocate the memory. */
314 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
316 /* Create the circular buffer. */
317 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
318 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
320 /* There is one token in the buffer. */
321 lexer->last_token = lexer->buffer + 1;
322 lexer->first_token = lexer->buffer;
323 lexer->next_token = lexer->buffer;
324 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
326 /* This lexer obtains more tokens by calling c_lex. */
327 lexer->main_lexer_p = true;
329 /* Create the SAVED_TOKENS stack. */
330 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
332 /* Create the STRINGS array. */
333 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
335 /* Assume we are not debugging. */
336 lexer->debugging_p = false;
338 return lexer;
341 /* Create a new lexer whose token stream is primed with the TOKENS.
342 When these tokens are exhausted, no new tokens will be read. */
344 static cp_lexer *
345 cp_lexer_new_from_tokens (cp_token_cache *tokens)
347 cp_lexer *lexer;
348 cp_token *token;
349 cp_token_block *block;
350 ptrdiff_t num_tokens;
352 /* Allocate the memory. */
353 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
355 /* Create a new buffer, appropriately sized. */
356 num_tokens = 0;
357 for (block = tokens->first; block != NULL; block = block->next)
358 num_tokens += block->num_tokens;
359 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
360 lexer->buffer_end = lexer->buffer + num_tokens;
362 /* Install the tokens. */
363 token = lexer->buffer;
364 for (block = tokens->first; block != NULL; block = block->next)
366 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
367 token += block->num_tokens;
370 /* The FIRST_TOKEN is the beginning of the buffer. */
371 lexer->first_token = lexer->buffer;
372 /* The next available token is also at the beginning of the buffer. */
373 lexer->next_token = lexer->buffer;
374 /* The buffer is full. */
375 lexer->last_token = lexer->first_token;
377 /* This lexer doesn't obtain more tokens. */
378 lexer->main_lexer_p = false;
380 /* Create the SAVED_TOKENS stack. */
381 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
383 /* Create the STRINGS array. */
384 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
386 /* Assume we are not debugging. */
387 lexer->debugging_p = false;
389 return lexer;
392 /* Returns nonzero if debugging information should be output. */
394 static inline bool
395 cp_lexer_debugging_p (cp_lexer *lexer)
397 return lexer->debugging_p;
400 /* Set the current source position from the information stored in
401 TOKEN. */
403 static inline void
404 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
405 const cp_token *token)
407 /* Ideally, the source position information would not be a global
408 variable, but it is. */
410 /* Update the line number. */
411 if (token->type != CPP_EOF)
412 input_location = token->location;
415 /* TOKEN points into the circular token buffer. Return a pointer to
416 the next token in the buffer. */
418 static inline cp_token *
419 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
421 token++;
422 if (token == lexer->buffer_end)
423 token = lexer->buffer;
424 return token;
427 /* TOKEN points into the circular token buffer. Return a pointer to
428 the previous token in the buffer. */
430 static inline cp_token *
431 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
433 if (token == lexer->buffer)
434 token = lexer->buffer_end;
435 return token - 1;
438 /* nonzero if we are presently saving tokens. */
440 static int
441 cp_lexer_saving_tokens (const cp_lexer* lexer)
443 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
446 /* Return a pointer to the token that is N tokens beyond TOKEN in the
447 buffer. */
449 static cp_token *
450 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
452 token += n;
453 if (token >= lexer->buffer_end)
454 token = lexer->buffer + (token - lexer->buffer_end);
455 return token;
458 /* Returns the number of times that START would have to be incremented
459 to reach FINISH. If START and FINISH are the same, returns zero. */
461 static ptrdiff_t
462 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
464 if (finish >= start)
465 return finish - start;
466 else
467 return ((lexer->buffer_end - lexer->buffer)
468 - (start - finish));
471 /* Obtain another token from the C preprocessor and add it to the
472 token buffer. Returns the newly read token. */
474 static cp_token *
475 cp_lexer_read_token (cp_lexer* lexer)
477 cp_token *token;
479 /* Make sure there is room in the buffer. */
480 cp_lexer_maybe_grow_buffer (lexer);
482 /* If there weren't any tokens, then this one will be the first. */
483 if (!lexer->first_token)
484 lexer->first_token = lexer->last_token;
485 /* Similarly, if there were no available tokens, there is one now. */
486 if (!lexer->next_token)
487 lexer->next_token = lexer->last_token;
489 /* Figure out where we're going to store the new token. */
490 token = lexer->last_token;
492 /* Get a new token from the preprocessor. */
493 cp_lexer_get_preprocessor_token (lexer, token);
495 /* Increment LAST_TOKEN. */
496 lexer->last_token = cp_lexer_next_token (lexer, token);
498 /* Strings should have type `const char []'. Right now, we will
499 have an ARRAY_TYPE that is constant rather than an array of
500 constant elements.
501 FIXME: Make fix_string_type get this right in the first place. */
502 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
503 && flag_const_strings)
505 tree type;
507 /* Get the current type. It will be an ARRAY_TYPE. */
508 type = TREE_TYPE (token->value);
509 /* Use build_cplus_array_type to rebuild the array, thereby
510 getting the right type. */
511 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
512 /* Reset the type of the token. */
513 TREE_TYPE (token->value) = type;
516 return token;
519 /* If the circular buffer is full, make it bigger. */
521 static void
522 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
524 /* If the buffer is full, enlarge it. */
525 if (lexer->last_token == lexer->first_token)
527 cp_token *new_buffer;
528 cp_token *old_buffer;
529 cp_token *new_first_token;
530 ptrdiff_t buffer_length;
531 size_t num_tokens_to_copy;
533 /* Remember the current buffer pointer. It will become invalid,
534 but we will need to do pointer arithmetic involving this
535 value. */
536 old_buffer = lexer->buffer;
537 /* Compute the current buffer size. */
538 buffer_length = lexer->buffer_end - lexer->buffer;
539 /* Allocate a buffer twice as big. */
540 new_buffer = ggc_realloc (lexer->buffer,
541 2 * buffer_length * sizeof (cp_token));
543 /* Because the buffer is circular, logically consecutive tokens
544 are not necessarily placed consecutively in memory.
545 Therefore, we must keep move the tokens that were before
546 FIRST_TOKEN to the second half of the newly allocated
547 buffer. */
548 num_tokens_to_copy = (lexer->first_token - old_buffer);
549 memcpy (new_buffer + buffer_length,
550 new_buffer,
551 num_tokens_to_copy * sizeof (cp_token));
552 /* Clear the rest of the buffer. We never look at this storage,
553 but the garbage collector may. */
554 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
555 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
557 /* Now recompute all of the buffer pointers. */
558 new_first_token
559 = new_buffer + (lexer->first_token - old_buffer);
560 if (lexer->next_token != NULL)
562 ptrdiff_t next_token_delta;
564 if (lexer->next_token > lexer->first_token)
565 next_token_delta = lexer->next_token - lexer->first_token;
566 else
567 next_token_delta =
568 buffer_length - (lexer->first_token - lexer->next_token);
569 lexer->next_token = new_first_token + next_token_delta;
571 lexer->last_token = new_first_token + buffer_length;
572 lexer->buffer = new_buffer;
573 lexer->buffer_end = new_buffer + buffer_length * 2;
574 lexer->first_token = new_first_token;
578 /* Store the next token from the preprocessor in *TOKEN. */
580 static void
581 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
582 cp_token *token)
584 bool done;
586 /* If this not the main lexer, return a terminating CPP_EOF token. */
587 if (lexer != NULL && !lexer->main_lexer_p)
589 token->type = CPP_EOF;
590 token->location.line = 0;
591 token->location.file = NULL;
592 token->value = NULL_TREE;
593 token->keyword = RID_MAX;
595 return;
598 done = false;
599 /* Keep going until we get a token we like. */
600 while (!done)
602 /* Get a new token from the preprocessor. */
603 token->type = c_lex_with_flags (&token->value, &token->flags);
604 /* Issue messages about tokens we cannot process. */
605 switch (token->type)
607 case CPP_ATSIGN:
608 case CPP_HASH:
609 case CPP_PASTE:
610 error ("invalid token");
611 break;
613 default:
614 /* This is a good token, so we exit the loop. */
615 done = true;
616 break;
619 /* Now we've got our token. */
620 token->location = input_location;
622 /* Check to see if this token is a keyword. */
623 if (token->type == CPP_NAME
624 && C_IS_RESERVED_WORD (token->value))
626 /* Mark this token as a keyword. */
627 token->type = CPP_KEYWORD;
628 /* Record which keyword. */
629 token->keyword = C_RID_CODE (token->value);
630 /* Update the value. Some keywords are mapped to particular
631 entities, rather than simply having the value of the
632 corresponding IDENTIFIER_NODE. For example, `__const' is
633 mapped to `const'. */
634 token->value = ridpointers[token->keyword];
636 else
637 token->keyword = RID_MAX;
640 /* Return a pointer to the next token in the token stream, but do not
641 consume it. */
643 static cp_token *
644 cp_lexer_peek_token (cp_lexer* lexer)
646 cp_token *token;
648 /* If there are no tokens, read one now. */
649 if (!lexer->next_token)
650 cp_lexer_read_token (lexer);
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer))
655 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
656 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
657 fprintf (cp_lexer_debug_stream, "\n");
660 token = lexer->next_token;
661 cp_lexer_set_source_position_from_token (lexer, token);
662 return token;
665 /* Return true if the next token has the indicated TYPE. */
667 static bool
668 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
670 cp_token *token;
672 /* Peek at the next token. */
673 token = cp_lexer_peek_token (lexer);
674 /* Check to see if it has the indicated TYPE. */
675 return token->type == type;
678 /* Return true if the next token does not have the indicated TYPE. */
680 static bool
681 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
683 return !cp_lexer_next_token_is (lexer, type);
686 /* Return true if the next token is the indicated KEYWORD. */
688 static bool
689 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
691 cp_token *token;
693 /* Peek at the next token. */
694 token = cp_lexer_peek_token (lexer);
695 /* Check to see if it is the indicated keyword. */
696 return token->keyword == keyword;
699 /* Return a pointer to the Nth token in the token stream. If N is 1,
700 then this is precisely equivalent to cp_lexer_peek_token. */
702 static cp_token *
703 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
705 cp_token *token;
707 /* N is 1-based, not zero-based. */
708 my_friendly_assert (n > 0, 20000224);
710 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
711 token = lexer->next_token;
712 /* If there are no tokens in the buffer, get one now. */
713 if (!token)
715 cp_lexer_read_token (lexer);
716 token = lexer->next_token;
719 /* Now, read tokens until we have enough. */
720 while (--n > 0)
722 /* Advance to the next token. */
723 token = cp_lexer_next_token (lexer, token);
724 /* If that's all the tokens we have, read a new one. */
725 if (token == lexer->last_token)
726 token = cp_lexer_read_token (lexer);
729 return token;
732 /* Consume the next token. The pointer returned is valid only until
733 another token is read. Callers should preserve copy the token
734 explicitly if they will need its value for a longer period of
735 time. */
737 static cp_token *
738 cp_lexer_consume_token (cp_lexer* lexer)
740 cp_token *token;
742 /* If there are no tokens, read one now. */
743 if (!lexer->next_token)
744 cp_lexer_read_token (lexer);
746 /* Remember the token we'll be returning. */
747 token = lexer->next_token;
749 /* Increment NEXT_TOKEN. */
750 lexer->next_token = cp_lexer_next_token (lexer,
751 lexer->next_token);
752 /* Check to see if we're all out of tokens. */
753 if (lexer->next_token == lexer->last_token)
754 lexer->next_token = NULL;
756 /* If we're not saving tokens, then move FIRST_TOKEN too. */
757 if (!cp_lexer_saving_tokens (lexer))
759 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
760 if (!lexer->next_token)
761 lexer->first_token = NULL;
762 else
763 lexer->first_token = lexer->next_token;
766 /* Provide debugging output. */
767 if (cp_lexer_debugging_p (lexer))
769 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
770 cp_lexer_print_token (cp_lexer_debug_stream, token);
771 fprintf (cp_lexer_debug_stream, "\n");
774 return token;
777 /* Permanently remove the next token from the token stream. There
778 must be a valid next token already; this token never reads
779 additional tokens from the preprocessor. */
781 static void
782 cp_lexer_purge_token (cp_lexer *lexer)
784 cp_token *token;
785 cp_token *next_token;
787 token = lexer->next_token;
788 while (true)
790 next_token = cp_lexer_next_token (lexer, token);
791 if (next_token == lexer->last_token)
792 break;
793 *token = *next_token;
794 token = next_token;
797 lexer->last_token = token;
798 /* The token purged may have been the only token remaining; if so,
799 clear NEXT_TOKEN. */
800 if (lexer->next_token == token)
801 lexer->next_token = NULL;
804 /* Permanently remove all tokens after TOKEN, up to, but not
805 including, the token that will be returned next by
806 cp_lexer_peek_token. */
808 static void
809 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
811 cp_token *peek;
812 cp_token *t1;
813 cp_token *t2;
815 if (lexer->next_token)
817 /* Copy the tokens that have not yet been read to the location
818 immediately following TOKEN. */
819 t1 = cp_lexer_next_token (lexer, token);
820 t2 = peek = cp_lexer_peek_token (lexer);
821 /* Move tokens into the vacant area between TOKEN and PEEK. */
822 while (t2 != lexer->last_token)
824 *t1 = *t2;
825 t1 = cp_lexer_next_token (lexer, t1);
826 t2 = cp_lexer_next_token (lexer, t2);
828 /* Now, the next available token is right after TOKEN. */
829 lexer->next_token = cp_lexer_next_token (lexer, token);
830 /* And the last token is wherever we ended up. */
831 lexer->last_token = t1;
833 else
835 /* There are no tokens in the buffer, so there is nothing to
836 copy. The last token in the buffer is TOKEN itself. */
837 lexer->last_token = cp_lexer_next_token (lexer, token);
841 /* Begin saving tokens. All tokens consumed after this point will be
842 preserved. */
844 static void
845 cp_lexer_save_tokens (cp_lexer* lexer)
847 /* Provide debugging output. */
848 if (cp_lexer_debugging_p (lexer))
849 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
851 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
852 restore the tokens if required. */
853 if (!lexer->next_token)
854 cp_lexer_read_token (lexer);
856 VARRAY_PUSH_INT (lexer->saved_tokens,
857 cp_lexer_token_difference (lexer,
858 lexer->first_token,
859 lexer->next_token));
862 /* Commit to the portion of the token stream most recently saved. */
864 static void
865 cp_lexer_commit_tokens (cp_lexer* lexer)
867 /* Provide debugging output. */
868 if (cp_lexer_debugging_p (lexer))
869 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
871 VARRAY_POP (lexer->saved_tokens);
874 /* Return all tokens saved since the last call to cp_lexer_save_tokens
875 to the token stream. Stop saving tokens. */
877 static void
878 cp_lexer_rollback_tokens (cp_lexer* lexer)
880 size_t delta;
882 /* Provide debugging output. */
883 if (cp_lexer_debugging_p (lexer))
884 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
886 /* Find the token that was the NEXT_TOKEN when we started saving
887 tokens. */
888 delta = VARRAY_TOP_INT(lexer->saved_tokens);
889 /* Make it the next token again now. */
890 lexer->next_token = cp_lexer_advance_token (lexer,
891 lexer->first_token,
892 delta);
893 /* It might be the case that there were no tokens when we started
894 saving tokens, but that there are some tokens now. */
895 if (!lexer->next_token && lexer->first_token)
896 lexer->next_token = lexer->first_token;
898 /* Stop saving tokens. */
899 VARRAY_POP (lexer->saved_tokens);
902 /* Print a representation of the TOKEN on the STREAM. */
904 static void
905 cp_lexer_print_token (FILE * stream, cp_token* token)
907 const char *token_type = NULL;
909 /* Figure out what kind of token this is. */
910 switch (token->type)
912 case CPP_EQ:
913 token_type = "EQ";
914 break;
916 case CPP_COMMA:
917 token_type = "COMMA";
918 break;
920 case CPP_OPEN_PAREN:
921 token_type = "OPEN_PAREN";
922 break;
924 case CPP_CLOSE_PAREN:
925 token_type = "CLOSE_PAREN";
926 break;
928 case CPP_OPEN_BRACE:
929 token_type = "OPEN_BRACE";
930 break;
932 case CPP_CLOSE_BRACE:
933 token_type = "CLOSE_BRACE";
934 break;
936 case CPP_SEMICOLON:
937 token_type = "SEMICOLON";
938 break;
940 case CPP_NAME:
941 token_type = "NAME";
942 break;
944 case CPP_EOF:
945 token_type = "EOF";
946 break;
948 case CPP_KEYWORD:
949 token_type = "keyword";
950 break;
952 /* This is not a token that we know how to handle yet. */
953 default:
954 break;
957 /* If we have a name for the token, print it out. Otherwise, we
958 simply give the numeric code. */
959 if (token_type)
960 fprintf (stream, "%s", token_type);
961 else
962 fprintf (stream, "%d", token->type);
963 /* And, for an identifier, print the identifier name. */
964 if (token->type == CPP_NAME
965 /* Some keywords have a value that is not an IDENTIFIER_NODE.
966 For example, `struct' is mapped to an INTEGER_CST. */
967 || (token->type == CPP_KEYWORD
968 && TREE_CODE (token->value) == IDENTIFIER_NODE))
969 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
972 /* Start emitting debugging information. */
974 static void
975 cp_lexer_start_debugging (cp_lexer* lexer)
977 ++lexer->debugging_p;
980 /* Stop emitting debugging information. */
982 static void
983 cp_lexer_stop_debugging (cp_lexer* lexer)
985 --lexer->debugging_p;
989 /* The parser. */
991 /* Overview
992 --------
994 A cp_parser parses the token stream as specified by the C++
995 grammar. Its job is purely parsing, not semantic analysis. For
996 example, the parser breaks the token stream into declarators,
997 expressions, statements, and other similar syntactic constructs.
998 It does not check that the types of the expressions on either side
999 of an assignment-statement are compatible, or that a function is
1000 not declared with a parameter of type `void'.
1002 The parser invokes routines elsewhere in the compiler to perform
1003 semantic analysis and to build up the abstract syntax tree for the
1004 code processed.
1006 The parser (and the template instantiation code, which is, in a
1007 way, a close relative of parsing) are the only parts of the
1008 compiler that should be calling push_scope and pop_scope, or
1009 related functions. The parser (and template instantiation code)
1010 keeps track of what scope is presently active; everything else
1011 should simply honor that. (The code that generates static
1012 initializers may also need to set the scope, in order to check
1013 access control correctly when emitting the initializers.)
1015 Methodology
1016 -----------
1018 The parser is of the standard recursive-descent variety. Upcoming
1019 tokens in the token stream are examined in order to determine which
1020 production to use when parsing a non-terminal. Some C++ constructs
1021 require arbitrary look ahead to disambiguate. For example, it is
1022 impossible, in the general case, to tell whether a statement is an
1023 expression or declaration without scanning the entire statement.
1024 Therefore, the parser is capable of "parsing tentatively." When the
1025 parser is not sure what construct comes next, it enters this mode.
1026 Then, while we attempt to parse the construct, the parser queues up
1027 error messages, rather than issuing them immediately, and saves the
1028 tokens it consumes. If the construct is parsed successfully, the
1029 parser "commits", i.e., it issues any queued error messages and
1030 the tokens that were being preserved are permanently discarded.
1031 If, however, the construct is not parsed successfully, the parser
1032 rolls back its state completely so that it can resume parsing using
1033 a different alternative.
1035 Future Improvements
1036 -------------------
1038 The performance of the parser could probably be improved
1039 substantially. Some possible improvements include:
1041 - The expression parser recurses through the various levels of
1042 precedence as specified in the grammar, rather than using an
1043 operator-precedence technique. Therefore, parsing a simple
1044 identifier requires multiple recursive calls.
1046 - We could often eliminate the need to parse tentatively by
1047 looking ahead a little bit. In some places, this approach
1048 might not entirely eliminate the need to parse tentatively, but
1049 it might still speed up the average case. */
1051 /* Flags that are passed to some parsing functions. These values can
1052 be bitwise-ored together. */
1054 typedef enum cp_parser_flags
1056 /* No flags. */
1057 CP_PARSER_FLAGS_NONE = 0x0,
1058 /* The construct is optional. If it is not present, then no error
1059 should be issued. */
1060 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1061 /* When parsing a type-specifier, do not allow user-defined types. */
1062 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1063 } cp_parser_flags;
1065 /* The different kinds of declarators we want to parse. */
1067 typedef enum cp_parser_declarator_kind
1069 /* We want an abstract declartor. */
1070 CP_PARSER_DECLARATOR_ABSTRACT,
1071 /* We want a named declarator. */
1072 CP_PARSER_DECLARATOR_NAMED,
1073 /* We don't mind, but the name must be an unqualified-id. */
1074 CP_PARSER_DECLARATOR_EITHER
1075 } cp_parser_declarator_kind;
1077 /* A mapping from a token type to a corresponding tree node type. */
1079 typedef struct cp_parser_token_tree_map_node
1081 /* The token type. */
1082 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1083 /* The corresponding tree code. */
1084 ENUM_BITFIELD (tree_code) tree_type : 8;
1085 } cp_parser_token_tree_map_node;
1087 /* A complete map consists of several ordinary entries, followed by a
1088 terminator. The terminating entry has a token_type of CPP_EOF. */
1090 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1092 /* The status of a tentative parse. */
1094 typedef enum cp_parser_status_kind
1096 /* No errors have occurred. */
1097 CP_PARSER_STATUS_KIND_NO_ERROR,
1098 /* An error has occurred. */
1099 CP_PARSER_STATUS_KIND_ERROR,
1100 /* We are committed to this tentative parse, whether or not an error
1101 has occurred. */
1102 CP_PARSER_STATUS_KIND_COMMITTED
1103 } cp_parser_status_kind;
1105 /* Context that is saved and restored when parsing tentatively. */
1107 typedef struct cp_parser_context GTY (())
1109 /* If this is a tentative parsing context, the status of the
1110 tentative parse. */
1111 enum cp_parser_status_kind status;
1112 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1113 that are looked up in this context must be looked up both in the
1114 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1115 the context of the containing expression. */
1116 tree object_type;
1117 /* The next parsing context in the stack. */
1118 struct cp_parser_context *next;
1119 } cp_parser_context;
1121 /* Prototypes. */
1123 /* Constructors and destructors. */
1125 static cp_parser_context *cp_parser_context_new
1126 (cp_parser_context *);
1128 /* Class variables. */
1130 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1132 /* Constructors and destructors. */
1134 /* Construct a new context. The context below this one on the stack
1135 is given by NEXT. */
1137 static cp_parser_context *
1138 cp_parser_context_new (cp_parser_context* next)
1140 cp_parser_context *context;
1142 /* Allocate the storage. */
1143 if (cp_parser_context_free_list != NULL)
1145 /* Pull the first entry from the free list. */
1146 context = cp_parser_context_free_list;
1147 cp_parser_context_free_list = context->next;
1148 memset (context, 0, sizeof (*context));
1150 else
1151 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1152 /* No errors have occurred yet in this context. */
1153 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1154 /* If this is not the bottomost context, copy information that we
1155 need from the previous context. */
1156 if (next)
1158 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1159 expression, then we are parsing one in this context, too. */
1160 context->object_type = next->object_type;
1161 /* Thread the stack. */
1162 context->next = next;
1165 return context;
1168 /* The cp_parser structure represents the C++ parser. */
1170 typedef struct cp_parser GTY(())
1172 /* The lexer from which we are obtaining tokens. */
1173 cp_lexer *lexer;
1175 /* The scope in which names should be looked up. If NULL_TREE, then
1176 we look up names in the scope that is currently open in the
1177 source program. If non-NULL, this is either a TYPE or
1178 NAMESPACE_DECL for the scope in which we should look.
1180 This value is not cleared automatically after a name is looked
1181 up, so we must be careful to clear it before starting a new look
1182 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1183 will look up `Z' in the scope of `X', rather than the current
1184 scope.) Unfortunately, it is difficult to tell when name lookup
1185 is complete, because we sometimes peek at a token, look it up,
1186 and then decide not to consume it. */
1187 tree scope;
1189 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1190 last lookup took place. OBJECT_SCOPE is used if an expression
1191 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1192 respectively. QUALIFYING_SCOPE is used for an expression of the
1193 form "X::Y"; it refers to X. */
1194 tree object_scope;
1195 tree qualifying_scope;
1197 /* A stack of parsing contexts. All but the bottom entry on the
1198 stack will be tentative contexts.
1200 We parse tentatively in order to determine which construct is in
1201 use in some situations. For example, in order to determine
1202 whether a statement is an expression-statement or a
1203 declaration-statement we parse it tentatively as a
1204 declaration-statement. If that fails, we then reparse the same
1205 token stream as an expression-statement. */
1206 cp_parser_context *context;
1208 /* True if we are parsing GNU C++. If this flag is not set, then
1209 GNU extensions are not recognized. */
1210 bool allow_gnu_extensions_p;
1212 /* TRUE if the `>' token should be interpreted as the greater-than
1213 operator. FALSE if it is the end of a template-id or
1214 template-parameter-list. */
1215 bool greater_than_is_operator_p;
1217 /* TRUE if default arguments are allowed within a parameter list
1218 that starts at this point. FALSE if only a gnu extension makes
1219 them permissible. */
1220 bool default_arg_ok_p;
1222 /* TRUE if we are parsing an integral constant-expression. See
1223 [expr.const] for a precise definition. */
1224 bool integral_constant_expression_p;
1226 /* TRUE if we are parsing an integral constant-expression -- but a
1227 non-constant expression should be permitted as well. This flag
1228 is used when parsing an array bound so that GNU variable-length
1229 arrays are tolerated. */
1230 bool allow_non_integral_constant_expression_p;
1232 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1233 been seen that makes the expression non-constant. */
1234 bool non_integral_constant_expression_p;
1236 /* TRUE if we are parsing the argument to "__offsetof__". */
1237 bool in_offsetof_p;
1239 /* TRUE if local variable names and `this' are forbidden in the
1240 current context. */
1241 bool local_variables_forbidden_p;
1243 /* TRUE if the declaration we are parsing is part of a
1244 linkage-specification of the form `extern string-literal
1245 declaration'. */
1246 bool in_unbraced_linkage_specification_p;
1248 /* TRUE if we are presently parsing a declarator, after the
1249 direct-declarator. */
1250 bool in_declarator_p;
1252 /* TRUE if we are presently parsing a template-argument-list. */
1253 bool in_template_argument_list_p;
1255 /* TRUE if we are presently parsing the body of an
1256 iteration-statement. */
1257 bool in_iteration_statement_p;
1259 /* TRUE if we are presently parsing the body of a switch
1260 statement. */
1261 bool in_switch_statement_p;
1263 /* TRUE if we are parsing a type-id in an expression context. In
1264 such a situation, both "type (expr)" and "type (type)" are valid
1265 alternatives. */
1266 bool in_type_id_in_expr_p;
1268 /* If non-NULL, then we are parsing a construct where new type
1269 definitions are not permitted. The string stored here will be
1270 issued as an error message if a type is defined. */
1271 const char *type_definition_forbidden_message;
1273 /* A list of lists. The outer list is a stack, used for member
1274 functions of local classes. At each level there are two sub-list,
1275 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1276 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1277 TREE_VALUE's. The functions are chained in reverse declaration
1278 order.
1280 The TREE_PURPOSE sublist contains those functions with default
1281 arguments that need post processing, and the TREE_VALUE sublist
1282 contains those functions with definitions that need post
1283 processing.
1285 These lists can only be processed once the outermost class being
1286 defined is complete. */
1287 tree unparsed_functions_queues;
1289 /* The number of classes whose definitions are currently in
1290 progress. */
1291 unsigned num_classes_being_defined;
1293 /* The number of template parameter lists that apply directly to the
1294 current declaration. */
1295 unsigned num_template_parameter_lists;
1296 } cp_parser;
1298 /* The type of a function that parses some kind of expression. */
1299 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1301 /* Prototypes. */
1303 /* Constructors and destructors. */
1305 static cp_parser *cp_parser_new
1306 (void);
1308 /* Routines to parse various constructs.
1310 Those that return `tree' will return the error_mark_node (rather
1311 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1312 Sometimes, they will return an ordinary node if error-recovery was
1313 attempted, even though a parse error occurred. So, to check
1314 whether or not a parse error occurred, you should always use
1315 cp_parser_error_occurred. If the construct is optional (indicated
1316 either by an `_opt' in the name of the function that does the
1317 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1318 the construct is not present. */
1320 /* Lexical conventions [gram.lex] */
1322 static tree cp_parser_identifier
1323 (cp_parser *);
1325 /* Basic concepts [gram.basic] */
1327 static bool cp_parser_translation_unit
1328 (cp_parser *);
1330 /* Expressions [gram.expr] */
1332 static tree cp_parser_primary_expression
1333 (cp_parser *, cp_id_kind *, tree *);
1334 static tree cp_parser_id_expression
1335 (cp_parser *, bool, bool, bool *, bool);
1336 static tree cp_parser_unqualified_id
1337 (cp_parser *, bool, bool, bool);
1338 static tree cp_parser_nested_name_specifier_opt
1339 (cp_parser *, bool, bool, bool, bool);
1340 static tree cp_parser_nested_name_specifier
1341 (cp_parser *, bool, bool, bool, bool);
1342 static tree cp_parser_class_or_namespace_name
1343 (cp_parser *, bool, bool, bool, bool, bool);
1344 static tree cp_parser_postfix_expression
1345 (cp_parser *, bool);
1346 static tree cp_parser_parenthesized_expression_list
1347 (cp_parser *, bool, bool *);
1348 static void cp_parser_pseudo_destructor_name
1349 (cp_parser *, tree *, tree *);
1350 static tree cp_parser_unary_expression
1351 (cp_parser *, bool);
1352 static enum tree_code cp_parser_unary_operator
1353 (cp_token *);
1354 static tree cp_parser_new_expression
1355 (cp_parser *);
1356 static tree cp_parser_new_placement
1357 (cp_parser *);
1358 static tree cp_parser_new_type_id
1359 (cp_parser *);
1360 static tree cp_parser_new_declarator_opt
1361 (cp_parser *);
1362 static tree cp_parser_direct_new_declarator
1363 (cp_parser *);
1364 static tree cp_parser_new_initializer
1365 (cp_parser *);
1366 static tree cp_parser_delete_expression
1367 (cp_parser *);
1368 static tree cp_parser_cast_expression
1369 (cp_parser *, bool);
1370 static tree cp_parser_pm_expression
1371 (cp_parser *);
1372 static tree cp_parser_multiplicative_expression
1373 (cp_parser *);
1374 static tree cp_parser_additive_expression
1375 (cp_parser *);
1376 static tree cp_parser_shift_expression
1377 (cp_parser *);
1378 static tree cp_parser_relational_expression
1379 (cp_parser *);
1380 static tree cp_parser_equality_expression
1381 (cp_parser *);
1382 static tree cp_parser_and_expression
1383 (cp_parser *);
1384 static tree cp_parser_exclusive_or_expression
1385 (cp_parser *);
1386 static tree cp_parser_inclusive_or_expression
1387 (cp_parser *);
1388 static tree cp_parser_logical_and_expression
1389 (cp_parser *);
1390 static tree cp_parser_logical_or_expression
1391 (cp_parser *);
1392 static tree cp_parser_question_colon_clause
1393 (cp_parser *, tree);
1394 static tree cp_parser_assignment_expression
1395 (cp_parser *);
1396 static enum tree_code cp_parser_assignment_operator_opt
1397 (cp_parser *);
1398 static tree cp_parser_expression
1399 (cp_parser *);
1400 static tree cp_parser_constant_expression
1401 (cp_parser *, bool, bool *);
1403 /* Statements [gram.stmt.stmt] */
1405 static void cp_parser_statement
1406 (cp_parser *, bool);
1407 static tree cp_parser_labeled_statement
1408 (cp_parser *, bool);
1409 static tree cp_parser_expression_statement
1410 (cp_parser *, bool);
1411 static tree cp_parser_compound_statement
1412 (cp_parser *, bool);
1413 static void cp_parser_statement_seq_opt
1414 (cp_parser *, bool);
1415 static tree cp_parser_selection_statement
1416 (cp_parser *);
1417 static tree cp_parser_condition
1418 (cp_parser *);
1419 static tree cp_parser_iteration_statement
1420 (cp_parser *);
1421 static void cp_parser_for_init_statement
1422 (cp_parser *);
1423 static tree cp_parser_jump_statement
1424 (cp_parser *);
1425 static void cp_parser_declaration_statement
1426 (cp_parser *);
1428 static tree cp_parser_implicitly_scoped_statement
1429 (cp_parser *);
1430 static void cp_parser_already_scoped_statement
1431 (cp_parser *);
1433 /* Declarations [gram.dcl.dcl] */
1435 static void cp_parser_declaration_seq_opt
1436 (cp_parser *);
1437 static void cp_parser_declaration
1438 (cp_parser *);
1439 static void cp_parser_block_declaration
1440 (cp_parser *, bool);
1441 static void cp_parser_simple_declaration
1442 (cp_parser *, bool);
1443 static tree cp_parser_decl_specifier_seq
1444 (cp_parser *, cp_parser_flags, tree *, int *);
1445 static tree cp_parser_storage_class_specifier_opt
1446 (cp_parser *);
1447 static tree cp_parser_function_specifier_opt
1448 (cp_parser *);
1449 static tree cp_parser_type_specifier
1450 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1451 static tree cp_parser_simple_type_specifier
1452 (cp_parser *, cp_parser_flags, bool);
1453 static tree cp_parser_type_name
1454 (cp_parser *);
1455 static tree cp_parser_elaborated_type_specifier
1456 (cp_parser *, bool, bool);
1457 static tree cp_parser_enum_specifier
1458 (cp_parser *);
1459 static void cp_parser_enumerator_list
1460 (cp_parser *, tree);
1461 static void cp_parser_enumerator_definition
1462 (cp_parser *, tree);
1463 static tree cp_parser_namespace_name
1464 (cp_parser *);
1465 static void cp_parser_namespace_definition
1466 (cp_parser *);
1467 static void cp_parser_namespace_body
1468 (cp_parser *);
1469 static tree cp_parser_qualified_namespace_specifier
1470 (cp_parser *);
1471 static void cp_parser_namespace_alias_definition
1472 (cp_parser *);
1473 static void cp_parser_using_declaration
1474 (cp_parser *);
1475 static void cp_parser_using_directive
1476 (cp_parser *);
1477 static void cp_parser_asm_definition
1478 (cp_parser *);
1479 static void cp_parser_linkage_specification
1480 (cp_parser *);
1482 /* Declarators [gram.dcl.decl] */
1484 static tree cp_parser_init_declarator
1485 (cp_parser *, tree, tree, bool, bool, int, bool *);
1486 static tree cp_parser_declarator
1487 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1488 static tree cp_parser_direct_declarator
1489 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1490 static enum tree_code cp_parser_ptr_operator
1491 (cp_parser *, tree *, tree *);
1492 static tree cp_parser_cv_qualifier_seq_opt
1493 (cp_parser *);
1494 static tree cp_parser_cv_qualifier_opt
1495 (cp_parser *);
1496 static tree cp_parser_declarator_id
1497 (cp_parser *);
1498 static tree cp_parser_type_id
1499 (cp_parser *);
1500 static tree cp_parser_type_specifier_seq
1501 (cp_parser *);
1502 static tree cp_parser_parameter_declaration_clause
1503 (cp_parser *);
1504 static tree cp_parser_parameter_declaration_list
1505 (cp_parser *);
1506 static tree cp_parser_parameter_declaration
1507 (cp_parser *, bool, bool *);
1508 static void cp_parser_function_body
1509 (cp_parser *);
1510 static tree cp_parser_initializer
1511 (cp_parser *, bool *, bool *);
1512 static tree cp_parser_initializer_clause
1513 (cp_parser *, bool *);
1514 static tree cp_parser_initializer_list
1515 (cp_parser *, bool *);
1517 static bool cp_parser_ctor_initializer_opt_and_function_body
1518 (cp_parser *);
1520 /* Classes [gram.class] */
1522 static tree cp_parser_class_name
1523 (cp_parser *, bool, bool, bool, bool, bool, bool);
1524 static tree cp_parser_class_specifier
1525 (cp_parser *);
1526 static tree cp_parser_class_head
1527 (cp_parser *, bool *, tree *);
1528 static enum tag_types cp_parser_class_key
1529 (cp_parser *);
1530 static void cp_parser_member_specification_opt
1531 (cp_parser *);
1532 static void cp_parser_member_declaration
1533 (cp_parser *);
1534 static tree cp_parser_pure_specifier
1535 (cp_parser *);
1536 static tree cp_parser_constant_initializer
1537 (cp_parser *);
1539 /* Derived classes [gram.class.derived] */
1541 static tree cp_parser_base_clause
1542 (cp_parser *);
1543 static tree cp_parser_base_specifier
1544 (cp_parser *);
1546 /* Special member functions [gram.special] */
1548 static tree cp_parser_conversion_function_id
1549 (cp_parser *);
1550 static tree cp_parser_conversion_type_id
1551 (cp_parser *);
1552 static tree cp_parser_conversion_declarator_opt
1553 (cp_parser *);
1554 static bool cp_parser_ctor_initializer_opt
1555 (cp_parser *);
1556 static void cp_parser_mem_initializer_list
1557 (cp_parser *);
1558 static tree cp_parser_mem_initializer
1559 (cp_parser *);
1560 static tree cp_parser_mem_initializer_id
1561 (cp_parser *);
1563 /* Overloading [gram.over] */
1565 static tree cp_parser_operator_function_id
1566 (cp_parser *);
1567 static tree cp_parser_operator
1568 (cp_parser *);
1570 /* Templates [gram.temp] */
1572 static void cp_parser_template_declaration
1573 (cp_parser *, bool);
1574 static tree cp_parser_template_parameter_list
1575 (cp_parser *);
1576 static tree cp_parser_template_parameter
1577 (cp_parser *);
1578 static tree cp_parser_type_parameter
1579 (cp_parser *);
1580 static tree cp_parser_template_id
1581 (cp_parser *, bool, bool, bool);
1582 static tree cp_parser_template_name
1583 (cp_parser *, bool, bool, bool, bool *);
1584 static tree cp_parser_template_argument_list
1585 (cp_parser *);
1586 static tree cp_parser_template_argument
1587 (cp_parser *);
1588 static void cp_parser_explicit_instantiation
1589 (cp_parser *);
1590 static void cp_parser_explicit_specialization
1591 (cp_parser *);
1593 /* Exception handling [gram.exception] */
1595 static tree cp_parser_try_block
1596 (cp_parser *);
1597 static bool cp_parser_function_try_block
1598 (cp_parser *);
1599 static void cp_parser_handler_seq
1600 (cp_parser *);
1601 static void cp_parser_handler
1602 (cp_parser *);
1603 static tree cp_parser_exception_declaration
1604 (cp_parser *);
1605 static tree cp_parser_throw_expression
1606 (cp_parser *);
1607 static tree cp_parser_exception_specification_opt
1608 (cp_parser *);
1609 static tree cp_parser_type_id_list
1610 (cp_parser *);
1612 /* GNU Extensions */
1614 static tree cp_parser_asm_specification_opt
1615 (cp_parser *);
1616 static tree cp_parser_asm_operand_list
1617 (cp_parser *);
1618 static tree cp_parser_asm_clobber_list
1619 (cp_parser *);
1620 static tree cp_parser_attributes_opt
1621 (cp_parser *);
1622 static tree cp_parser_attribute_list
1623 (cp_parser *);
1624 static bool cp_parser_extension_opt
1625 (cp_parser *, int *);
1626 static void cp_parser_label_declaration
1627 (cp_parser *);
1629 /* Utility Routines */
1631 static tree cp_parser_lookup_name
1632 (cp_parser *, tree, bool, bool, bool, bool);
1633 static tree cp_parser_lookup_name_simple
1634 (cp_parser *, tree);
1635 static tree cp_parser_maybe_treat_template_as_class
1636 (tree, bool);
1637 static bool cp_parser_check_declarator_template_parameters
1638 (cp_parser *, tree);
1639 static bool cp_parser_check_template_parameters
1640 (cp_parser *, unsigned);
1641 static tree cp_parser_simple_cast_expression
1642 (cp_parser *);
1643 static tree cp_parser_binary_expression
1644 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1645 static tree cp_parser_global_scope_opt
1646 (cp_parser *, bool);
1647 static bool cp_parser_constructor_declarator_p
1648 (cp_parser *, bool);
1649 static tree cp_parser_function_definition_from_specifiers_and_declarator
1650 (cp_parser *, tree, tree, tree);
1651 static tree cp_parser_function_definition_after_declarator
1652 (cp_parser *, bool);
1653 static void cp_parser_template_declaration_after_export
1654 (cp_parser *, bool);
1655 static tree cp_parser_single_declaration
1656 (cp_parser *, bool, bool *);
1657 static tree cp_parser_functional_cast
1658 (cp_parser *, tree);
1659 static tree cp_parser_save_member_function_body
1660 (cp_parser *, tree, tree, tree);
1661 static tree cp_parser_enclosed_template_argument_list
1662 (cp_parser *);
1663 static void cp_parser_save_default_args
1664 (cp_parser *, tree);
1665 static void cp_parser_late_parsing_for_member
1666 (cp_parser *, tree);
1667 static void cp_parser_late_parsing_default_args
1668 (cp_parser *, tree);
1669 static tree cp_parser_sizeof_operand
1670 (cp_parser *, enum rid);
1671 static bool cp_parser_declares_only_class_p
1672 (cp_parser *);
1673 static bool cp_parser_friend_p
1674 (tree);
1675 static bool cp_parser_typedef_p
1676 (tree);
1677 static cp_token *cp_parser_require
1678 (cp_parser *, enum cpp_ttype, const char *);
1679 static cp_token *cp_parser_require_keyword
1680 (cp_parser *, enum rid, const char *);
1681 static bool cp_parser_token_starts_function_definition_p
1682 (cp_token *);
1683 static bool cp_parser_next_token_starts_class_definition_p
1684 (cp_parser *);
1685 static bool cp_parser_next_token_ends_template_argument_p
1686 (cp_parser *);
1687 static bool cp_parser_nth_token_starts_template_argument_list_p
1688 (cp_parser *, size_t);
1689 static enum tag_types cp_parser_token_is_class_key
1690 (cp_token *);
1691 static void cp_parser_check_class_key
1692 (enum tag_types, tree type);
1693 static void cp_parser_check_access_in_redeclaration
1694 (tree type);
1695 static bool cp_parser_optional_template_keyword
1696 (cp_parser *);
1697 static void cp_parser_pre_parsed_nested_name_specifier
1698 (cp_parser *);
1699 static void cp_parser_cache_group
1700 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1701 static void cp_parser_parse_tentatively
1702 (cp_parser *);
1703 static void cp_parser_commit_to_tentative_parse
1704 (cp_parser *);
1705 static void cp_parser_abort_tentative_parse
1706 (cp_parser *);
1707 static bool cp_parser_parse_definitely
1708 (cp_parser *);
1709 static inline bool cp_parser_parsing_tentatively
1710 (cp_parser *);
1711 static bool cp_parser_committed_to_tentative_parse
1712 (cp_parser *);
1713 static void cp_parser_error
1714 (cp_parser *, const char *);
1715 static void cp_parser_name_lookup_error
1716 (cp_parser *, tree, tree, const char *);
1717 static bool cp_parser_simulate_error
1718 (cp_parser *);
1719 static void cp_parser_check_type_definition
1720 (cp_parser *);
1721 static void cp_parser_check_for_definition_in_return_type
1722 (tree, tree);
1723 static void cp_parser_check_for_invalid_template_id
1724 (cp_parser *, tree);
1725 static bool cp_parser_non_integral_constant_expression
1726 (cp_parser *, const char *);
1727 static bool cp_parser_diagnose_invalid_type_name
1728 (cp_parser *);
1729 static int cp_parser_skip_to_closing_parenthesis
1730 (cp_parser *, bool, bool, bool);
1731 static void cp_parser_skip_to_end_of_statement
1732 (cp_parser *);
1733 static void cp_parser_consume_semicolon_at_end_of_statement
1734 (cp_parser *);
1735 static void cp_parser_skip_to_end_of_block_or_statement
1736 (cp_parser *);
1737 static void cp_parser_skip_to_closing_brace
1738 (cp_parser *);
1739 static void cp_parser_skip_until_found
1740 (cp_parser *, enum cpp_ttype, const char *);
1741 static bool cp_parser_error_occurred
1742 (cp_parser *);
1743 static bool cp_parser_allow_gnu_extensions_p
1744 (cp_parser *);
1745 static bool cp_parser_is_string_literal
1746 (cp_token *);
1747 static bool cp_parser_is_keyword
1748 (cp_token *, enum rid);
1750 /* Returns nonzero if we are parsing tentatively. */
1752 static inline bool
1753 cp_parser_parsing_tentatively (cp_parser* parser)
1755 return parser->context->next != NULL;
1758 /* Returns nonzero if TOKEN is a string literal. */
1760 static bool
1761 cp_parser_is_string_literal (cp_token* token)
1763 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1766 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1768 static bool
1769 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1771 return token->keyword == keyword;
1774 /* Issue the indicated error MESSAGE. */
1776 static void
1777 cp_parser_error (cp_parser* parser, const char* message)
1779 /* Output the MESSAGE -- unless we're parsing tentatively. */
1780 if (!cp_parser_simulate_error (parser))
1782 cp_token *token;
1783 token = cp_lexer_peek_token (parser->lexer);
1784 c_parse_error (message,
1785 /* Because c_parser_error does not understand
1786 CPP_KEYWORD, keywords are treated like
1787 identifiers. */
1788 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1789 token->value);
1793 /* Issue an error about name-lookup failing. NAME is the
1794 IDENTIFIER_NODE DECL is the result of
1795 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1796 the thing that we hoped to find. */
1798 static void
1799 cp_parser_name_lookup_error (cp_parser* parser,
1800 tree name,
1801 tree decl,
1802 const char* desired)
1804 /* If name lookup completely failed, tell the user that NAME was not
1805 declared. */
1806 if (decl == error_mark_node)
1808 if (parser->scope && parser->scope != global_namespace)
1809 error ("`%D::%D' has not been declared",
1810 parser->scope, name);
1811 else if (parser->scope == global_namespace)
1812 error ("`::%D' has not been declared", name);
1813 else
1814 error ("`%D' has not been declared", name);
1816 else if (parser->scope && parser->scope != global_namespace)
1817 error ("`%D::%D' %s", parser->scope, name, desired);
1818 else if (parser->scope == global_namespace)
1819 error ("`::%D' %s", name, desired);
1820 else
1821 error ("`%D' %s", name, desired);
1824 /* If we are parsing tentatively, remember that an error has occurred
1825 during this tentative parse. Returns true if the error was
1826 simulated; false if a messgae should be issued by the caller. */
1828 static bool
1829 cp_parser_simulate_error (cp_parser* parser)
1831 if (cp_parser_parsing_tentatively (parser)
1832 && !cp_parser_committed_to_tentative_parse (parser))
1834 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1835 return true;
1837 return false;
1840 /* This function is called when a type is defined. If type
1841 definitions are forbidden at this point, an error message is
1842 issued. */
1844 static void
1845 cp_parser_check_type_definition (cp_parser* parser)
1847 /* If types are forbidden here, issue a message. */
1848 if (parser->type_definition_forbidden_message)
1849 /* Use `%s' to print the string in case there are any escape
1850 characters in the message. */
1851 error ("%s", parser->type_definition_forbidden_message);
1854 /* This function is called when the DECLARATOR is processed. The TYPE
1855 was a type defined in the decl-specifiers. If it is invalid to
1856 define a type in the decl-specifiers for DECLARATOR, an error is
1857 issued. */
1859 static void
1860 cp_parser_check_for_definition_in_return_type (tree declarator, tree type)
1862 /* [dcl.fct] forbids type definitions in return types.
1863 Unfortunately, it's not easy to know whether or not we are
1864 processing a return type until after the fact. */
1865 while (declarator
1866 && (TREE_CODE (declarator) == INDIRECT_REF
1867 || TREE_CODE (declarator) == ADDR_EXPR))
1868 declarator = TREE_OPERAND (declarator, 0);
1869 if (declarator
1870 && TREE_CODE (declarator) == CALL_EXPR)
1872 error ("new types may not be defined in a return type");
1873 inform ("(perhaps a semicolon is missing after the definition of `%T')",
1874 type);
1878 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1879 "<" in any valid C++ program. If the next token is indeed "<",
1880 issue a message warning the user about what appears to be an
1881 invalid attempt to form a template-id. */
1883 static void
1884 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1885 tree type)
1887 ptrdiff_t start;
1888 cp_token *token;
1890 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1892 if (TYPE_P (type))
1893 error ("`%T' is not a template", type);
1894 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1895 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1896 else
1897 error ("invalid template-id");
1898 /* Remember the location of the invalid "<". */
1899 if (cp_parser_parsing_tentatively (parser)
1900 && !cp_parser_committed_to_tentative_parse (parser))
1902 token = cp_lexer_peek_token (parser->lexer);
1903 token = cp_lexer_prev_token (parser->lexer, token);
1904 start = cp_lexer_token_difference (parser->lexer,
1905 parser->lexer->first_token,
1906 token);
1908 else
1909 start = -1;
1910 /* Consume the "<". */
1911 cp_lexer_consume_token (parser->lexer);
1912 /* Parse the template arguments. */
1913 cp_parser_enclosed_template_argument_list (parser);
1914 /* Permanently remove the invalid template arguments so that
1915 this error message is not issued again. */
1916 if (start >= 0)
1918 token = cp_lexer_advance_token (parser->lexer,
1919 parser->lexer->first_token,
1920 start);
1921 cp_lexer_purge_tokens_after (parser->lexer, token);
1926 /* If parsing an integral constant-expression, issue an error message
1927 about the fact that THING appeared and return true. Otherwise,
1928 return false, marking the current expression as non-constant. */
1930 static bool
1931 cp_parser_non_integral_constant_expression (cp_parser *parser,
1932 const char *thing)
1934 if (parser->integral_constant_expression_p)
1936 if (!parser->allow_non_integral_constant_expression_p)
1938 error ("%s cannot appear in a constant-expression", thing);
1939 return true;
1941 parser->non_integral_constant_expression_p = true;
1943 return false;
1946 /* Check for a common situation where a type-name should be present,
1947 but is not, and issue a sensible error message. Returns true if an
1948 invalid type-name was detected. */
1950 static bool
1951 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1953 /* If the next two tokens are both identifiers, the code is
1954 erroneous. The usual cause of this situation is code like:
1956 T t;
1958 where "T" should name a type -- but does not. */
1959 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1960 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1962 tree name;
1964 /* If parsing tentatively, we should commit; we really are
1965 looking at a declaration. */
1966 /* Consume the first identifier. */
1967 name = cp_lexer_consume_token (parser->lexer)->value;
1968 /* Issue an error message. */
1969 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1970 /* If we're in a template class, it's possible that the user was
1971 referring to a type from a base class. For example:
1973 template <typename T> struct A { typedef T X; };
1974 template <typename T> struct B : public A<T> { X x; };
1976 The user should have said "typename A<T>::X". */
1977 if (processing_template_decl && current_class_type)
1979 tree b;
1981 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1983 b = TREE_CHAIN (b))
1985 tree base_type = BINFO_TYPE (b);
1986 if (CLASS_TYPE_P (base_type)
1987 && dependent_type_p (base_type))
1989 tree field;
1990 /* Go from a particular instantiation of the
1991 template (which will have an empty TYPE_FIELDs),
1992 to the main version. */
1993 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1994 for (field = TYPE_FIELDS (base_type);
1995 field;
1996 field = TREE_CHAIN (field))
1997 if (TREE_CODE (field) == TYPE_DECL
1998 && DECL_NAME (field) == name)
2000 error ("(perhaps `typename %T::%s' was intended)",
2001 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
2002 break;
2004 if (field)
2005 break;
2009 /* Skip to the end of the declaration; there's no point in
2010 trying to process it. */
2011 cp_parser_skip_to_end_of_statement (parser);
2013 return true;
2016 return false;
2019 /* Consume tokens up to, and including, the next non-nested closing `)'.
2020 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2021 are doing error recovery. Returns -1 if OR_COMMA is true and we
2022 found an unnested comma. */
2024 static int
2025 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2026 bool recovering,
2027 bool or_comma,
2028 bool consume_paren)
2030 unsigned paren_depth = 0;
2031 unsigned brace_depth = 0;
2033 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2034 && !cp_parser_committed_to_tentative_parse (parser))
2035 return 0;
2037 while (true)
2039 cp_token *token;
2041 /* If we've run out of tokens, then there is no closing `)'. */
2042 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2043 return 0;
2045 token = cp_lexer_peek_token (parser->lexer);
2047 /* This matches the processing in skip_to_end_of_statement. */
2048 if (token->type == CPP_SEMICOLON && !brace_depth)
2049 return 0;
2050 if (token->type == CPP_OPEN_BRACE)
2051 ++brace_depth;
2052 if (token->type == CPP_CLOSE_BRACE)
2054 if (!brace_depth--)
2055 return 0;
2057 if (recovering && or_comma && token->type == CPP_COMMA
2058 && !brace_depth && !paren_depth)
2059 return -1;
2061 if (!brace_depth)
2063 /* If it is an `(', we have entered another level of nesting. */
2064 if (token->type == CPP_OPEN_PAREN)
2065 ++paren_depth;
2066 /* If it is a `)', then we might be done. */
2067 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2069 if (consume_paren)
2070 cp_lexer_consume_token (parser->lexer);
2071 return 1;
2075 /* Consume the token. */
2076 cp_lexer_consume_token (parser->lexer);
2080 /* Consume tokens until we reach the end of the current statement.
2081 Normally, that will be just before consuming a `;'. However, if a
2082 non-nested `}' comes first, then we stop before consuming that. */
2084 static void
2085 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2087 unsigned nesting_depth = 0;
2089 while (true)
2091 cp_token *token;
2093 /* Peek at the next token. */
2094 token = cp_lexer_peek_token (parser->lexer);
2095 /* If we've run out of tokens, stop. */
2096 if (token->type == CPP_EOF)
2097 break;
2098 /* If the next token is a `;', we have reached the end of the
2099 statement. */
2100 if (token->type == CPP_SEMICOLON && !nesting_depth)
2101 break;
2102 /* If the next token is a non-nested `}', then we have reached
2103 the end of the current block. */
2104 if (token->type == CPP_CLOSE_BRACE)
2106 /* If this is a non-nested `}', stop before consuming it.
2107 That way, when confronted with something like:
2109 { 3 + }
2111 we stop before consuming the closing `}', even though we
2112 have not yet reached a `;'. */
2113 if (nesting_depth == 0)
2114 break;
2115 /* If it is the closing `}' for a block that we have
2116 scanned, stop -- but only after consuming the token.
2117 That way given:
2119 void f g () { ... }
2120 typedef int I;
2122 we will stop after the body of the erroneously declared
2123 function, but before consuming the following `typedef'
2124 declaration. */
2125 if (--nesting_depth == 0)
2127 cp_lexer_consume_token (parser->lexer);
2128 break;
2131 /* If it the next token is a `{', then we are entering a new
2132 block. Consume the entire block. */
2133 else if (token->type == CPP_OPEN_BRACE)
2134 ++nesting_depth;
2135 /* Consume the token. */
2136 cp_lexer_consume_token (parser->lexer);
2140 /* This function is called at the end of a statement or declaration.
2141 If the next token is a semicolon, it is consumed; otherwise, error
2142 recovery is attempted. */
2144 static void
2145 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2147 /* Look for the trailing `;'. */
2148 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2150 /* If there is additional (erroneous) input, skip to the end of
2151 the statement. */
2152 cp_parser_skip_to_end_of_statement (parser);
2153 /* If the next token is now a `;', consume it. */
2154 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2155 cp_lexer_consume_token (parser->lexer);
2159 /* Skip tokens until we have consumed an entire block, or until we
2160 have consumed a non-nested `;'. */
2162 static void
2163 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2165 unsigned nesting_depth = 0;
2167 while (true)
2169 cp_token *token;
2171 /* Peek at the next token. */
2172 token = cp_lexer_peek_token (parser->lexer);
2173 /* If we've run out of tokens, stop. */
2174 if (token->type == CPP_EOF)
2175 break;
2176 /* If the next token is a `;', we have reached the end of the
2177 statement. */
2178 if (token->type == CPP_SEMICOLON && !nesting_depth)
2180 /* Consume the `;'. */
2181 cp_lexer_consume_token (parser->lexer);
2182 break;
2184 /* Consume the token. */
2185 token = cp_lexer_consume_token (parser->lexer);
2186 /* If the next token is a non-nested `}', then we have reached
2187 the end of the current block. */
2188 if (token->type == CPP_CLOSE_BRACE
2189 && (nesting_depth == 0 || --nesting_depth == 0))
2190 break;
2191 /* If it the next token is a `{', then we are entering a new
2192 block. Consume the entire block. */
2193 if (token->type == CPP_OPEN_BRACE)
2194 ++nesting_depth;
2198 /* Skip tokens until a non-nested closing curly brace is the next
2199 token. */
2201 static void
2202 cp_parser_skip_to_closing_brace (cp_parser *parser)
2204 unsigned nesting_depth = 0;
2206 while (true)
2208 cp_token *token;
2210 /* Peek at the next token. */
2211 token = cp_lexer_peek_token (parser->lexer);
2212 /* If we've run out of tokens, stop. */
2213 if (token->type == CPP_EOF)
2214 break;
2215 /* If the next token is a non-nested `}', then we have reached
2216 the end of the current block. */
2217 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2218 break;
2219 /* If it the next token is a `{', then we are entering a new
2220 block. Consume the entire block. */
2221 else if (token->type == CPP_OPEN_BRACE)
2222 ++nesting_depth;
2223 /* Consume the token. */
2224 cp_lexer_consume_token (parser->lexer);
2228 /* Create a new C++ parser. */
2230 static cp_parser *
2231 cp_parser_new (void)
2233 cp_parser *parser;
2234 cp_lexer *lexer;
2236 /* cp_lexer_new_main is called before calling ggc_alloc because
2237 cp_lexer_new_main might load a PCH file. */
2238 lexer = cp_lexer_new_main ();
2240 parser = ggc_alloc_cleared (sizeof (cp_parser));
2241 parser->lexer = lexer;
2242 parser->context = cp_parser_context_new (NULL);
2244 /* For now, we always accept GNU extensions. */
2245 parser->allow_gnu_extensions_p = 1;
2247 /* The `>' token is a greater-than operator, not the end of a
2248 template-id. */
2249 parser->greater_than_is_operator_p = true;
2251 parser->default_arg_ok_p = true;
2253 /* We are not parsing a constant-expression. */
2254 parser->integral_constant_expression_p = false;
2255 parser->allow_non_integral_constant_expression_p = false;
2256 parser->non_integral_constant_expression_p = false;
2258 /* We are not parsing offsetof. */
2259 parser->in_offsetof_p = false;
2261 /* Local variable names are not forbidden. */
2262 parser->local_variables_forbidden_p = false;
2264 /* We are not processing an `extern "C"' declaration. */
2265 parser->in_unbraced_linkage_specification_p = false;
2267 /* We are not processing a declarator. */
2268 parser->in_declarator_p = false;
2270 /* We are not processing a template-argument-list. */
2271 parser->in_template_argument_list_p = false;
2273 /* We are not in an iteration statement. */
2274 parser->in_iteration_statement_p = false;
2276 /* We are not in a switch statement. */
2277 parser->in_switch_statement_p = false;
2279 /* We are not parsing a type-id inside an expression. */
2280 parser->in_type_id_in_expr_p = false;
2282 /* The unparsed function queue is empty. */
2283 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2285 /* There are no classes being defined. */
2286 parser->num_classes_being_defined = 0;
2288 /* No template parameters apply. */
2289 parser->num_template_parameter_lists = 0;
2291 return parser;
2294 /* Lexical conventions [gram.lex] */
2296 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2297 identifier. */
2299 static tree
2300 cp_parser_identifier (cp_parser* parser)
2302 cp_token *token;
2304 /* Look for the identifier. */
2305 token = cp_parser_require (parser, CPP_NAME, "identifier");
2306 /* Return the value. */
2307 return token ? token->value : error_mark_node;
2310 /* Basic concepts [gram.basic] */
2312 /* Parse a translation-unit.
2314 translation-unit:
2315 declaration-seq [opt]
2317 Returns TRUE if all went well. */
2319 static bool
2320 cp_parser_translation_unit (cp_parser* parser)
2322 while (true)
2324 cp_parser_declaration_seq_opt (parser);
2326 /* If there are no tokens left then all went well. */
2327 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2328 break;
2330 /* Otherwise, issue an error message. */
2331 cp_parser_error (parser, "expected declaration");
2332 return false;
2335 /* Consume the EOF token. */
2336 cp_parser_require (parser, CPP_EOF, "end-of-file");
2338 /* Finish up. */
2339 finish_translation_unit ();
2341 /* All went well. */
2342 return true;
2345 /* Expressions [gram.expr] */
2347 /* Parse a primary-expression.
2349 primary-expression:
2350 literal
2351 this
2352 ( expression )
2353 id-expression
2355 GNU Extensions:
2357 primary-expression:
2358 ( compound-statement )
2359 __builtin_va_arg ( assignment-expression , type-id )
2361 literal:
2362 __null
2364 Returns a representation of the expression.
2366 *IDK indicates what kind of id-expression (if any) was present.
2368 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2369 used as the operand of a pointer-to-member. In that case,
2370 *QUALIFYING_CLASS gives the class that is used as the qualifying
2371 class in the pointer-to-member. */
2373 static tree
2374 cp_parser_primary_expression (cp_parser *parser,
2375 cp_id_kind *idk,
2376 tree *qualifying_class)
2378 cp_token *token;
2380 /* Assume the primary expression is not an id-expression. */
2381 *idk = CP_ID_KIND_NONE;
2382 /* And that it cannot be used as pointer-to-member. */
2383 *qualifying_class = NULL_TREE;
2385 /* Peek at the next token. */
2386 token = cp_lexer_peek_token (parser->lexer);
2387 switch (token->type)
2389 /* literal:
2390 integer-literal
2391 character-literal
2392 floating-literal
2393 string-literal
2394 boolean-literal */
2395 case CPP_CHAR:
2396 case CPP_WCHAR:
2397 case CPP_STRING:
2398 case CPP_WSTRING:
2399 case CPP_NUMBER:
2400 token = cp_lexer_consume_token (parser->lexer);
2401 return token->value;
2403 case CPP_OPEN_PAREN:
2405 tree expr;
2406 bool saved_greater_than_is_operator_p;
2408 /* Consume the `('. */
2409 cp_lexer_consume_token (parser->lexer);
2410 /* Within a parenthesized expression, a `>' token is always
2411 the greater-than operator. */
2412 saved_greater_than_is_operator_p
2413 = parser->greater_than_is_operator_p;
2414 parser->greater_than_is_operator_p = true;
2415 /* If we see `( { ' then we are looking at the beginning of
2416 a GNU statement-expression. */
2417 if (cp_parser_allow_gnu_extensions_p (parser)
2418 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2420 /* Statement-expressions are not allowed by the standard. */
2421 if (pedantic)
2422 pedwarn ("ISO C++ forbids braced-groups within expressions");
2424 /* And they're not allowed outside of a function-body; you
2425 cannot, for example, write:
2427 int i = ({ int j = 3; j + 1; });
2429 at class or namespace scope. */
2430 if (!at_function_scope_p ())
2431 error ("statement-expressions are allowed only inside functions");
2432 /* Start the statement-expression. */
2433 expr = begin_stmt_expr ();
2434 /* Parse the compound-statement. */
2435 cp_parser_compound_statement (parser, true);
2436 /* Finish up. */
2437 expr = finish_stmt_expr (expr, false);
2439 else
2441 /* Parse the parenthesized expression. */
2442 expr = cp_parser_expression (parser);
2443 /* Let the front end know that this expression was
2444 enclosed in parentheses. This matters in case, for
2445 example, the expression is of the form `A::B', since
2446 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2447 not. */
2448 finish_parenthesized_expr (expr);
2450 /* The `>' token might be the end of a template-id or
2451 template-parameter-list now. */
2452 parser->greater_than_is_operator_p
2453 = saved_greater_than_is_operator_p;
2454 /* Consume the `)'. */
2455 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2456 cp_parser_skip_to_end_of_statement (parser);
2458 return expr;
2461 case CPP_KEYWORD:
2462 switch (token->keyword)
2464 /* These two are the boolean literals. */
2465 case RID_TRUE:
2466 cp_lexer_consume_token (parser->lexer);
2467 return boolean_true_node;
2468 case RID_FALSE:
2469 cp_lexer_consume_token (parser->lexer);
2470 return boolean_false_node;
2472 /* The `__null' literal. */
2473 case RID_NULL:
2474 cp_lexer_consume_token (parser->lexer);
2475 return null_node;
2477 /* Recognize the `this' keyword. */
2478 case RID_THIS:
2479 cp_lexer_consume_token (parser->lexer);
2480 if (parser->local_variables_forbidden_p)
2482 error ("`this' may not be used in this context");
2483 return error_mark_node;
2485 /* Pointers cannot appear in constant-expressions. */
2486 if (cp_parser_non_integral_constant_expression (parser,
2487 "`this'"))
2488 return error_mark_node;
2489 return finish_this_expr ();
2491 /* The `operator' keyword can be the beginning of an
2492 id-expression. */
2493 case RID_OPERATOR:
2494 goto id_expression;
2496 case RID_FUNCTION_NAME:
2497 case RID_PRETTY_FUNCTION_NAME:
2498 case RID_C99_FUNCTION_NAME:
2499 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2500 __func__ are the names of variables -- but they are
2501 treated specially. Therefore, they are handled here,
2502 rather than relying on the generic id-expression logic
2503 below. Grammatically, these names are id-expressions.
2505 Consume the token. */
2506 token = cp_lexer_consume_token (parser->lexer);
2507 /* Look up the name. */
2508 return finish_fname (token->value);
2510 case RID_VA_ARG:
2512 tree expression;
2513 tree type;
2515 /* The `__builtin_va_arg' construct is used to handle
2516 `va_arg'. Consume the `__builtin_va_arg' token. */
2517 cp_lexer_consume_token (parser->lexer);
2518 /* Look for the opening `('. */
2519 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2520 /* Now, parse the assignment-expression. */
2521 expression = cp_parser_assignment_expression (parser);
2522 /* Look for the `,'. */
2523 cp_parser_require (parser, CPP_COMMA, "`,'");
2524 /* Parse the type-id. */
2525 type = cp_parser_type_id (parser);
2526 /* Look for the closing `)'. */
2527 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2528 /* Using `va_arg' in a constant-expression is not
2529 allowed. */
2530 if (cp_parser_non_integral_constant_expression (parser,
2531 "`va_arg'"))
2532 return error_mark_node;
2533 return build_x_va_arg (expression, type);
2536 case RID_OFFSETOF:
2538 tree expression;
2539 bool saved_in_offsetof_p;
2541 /* Consume the "__offsetof__" token. */
2542 cp_lexer_consume_token (parser->lexer);
2543 /* Consume the opening `('. */
2544 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2545 /* Parse the parenthesized (almost) constant-expression. */
2546 saved_in_offsetof_p = parser->in_offsetof_p;
2547 parser->in_offsetof_p = true;
2548 expression
2549 = cp_parser_constant_expression (parser,
2550 /*allow_non_constant_p=*/false,
2551 /*non_constant_p=*/NULL);
2552 parser->in_offsetof_p = saved_in_offsetof_p;
2553 /* Consume the closing ')'. */
2554 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2556 return expression;
2559 default:
2560 cp_parser_error (parser, "expected primary-expression");
2561 return error_mark_node;
2564 /* An id-expression can start with either an identifier, a
2565 `::' as the beginning of a qualified-id, or the "operator"
2566 keyword. */
2567 case CPP_NAME:
2568 case CPP_SCOPE:
2569 case CPP_TEMPLATE_ID:
2570 case CPP_NESTED_NAME_SPECIFIER:
2572 tree id_expression;
2573 tree decl;
2574 const char *error_msg;
2576 id_expression:
2577 /* Parse the id-expression. */
2578 id_expression
2579 = cp_parser_id_expression (parser,
2580 /*template_keyword_p=*/false,
2581 /*check_dependency_p=*/true,
2582 /*template_p=*/NULL,
2583 /*declarator_p=*/false);
2584 if (id_expression == error_mark_node)
2585 return error_mark_node;
2586 /* If we have a template-id, then no further lookup is
2587 required. If the template-id was for a template-class, we
2588 will sometimes have a TYPE_DECL at this point. */
2589 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2590 || TREE_CODE (id_expression) == TYPE_DECL)
2591 decl = id_expression;
2592 /* Look up the name. */
2593 else
2595 decl = cp_parser_lookup_name_simple (parser, id_expression);
2596 /* If name lookup gives us a SCOPE_REF, then the
2597 qualifying scope was dependent. Just propagate the
2598 name. */
2599 if (TREE_CODE (decl) == SCOPE_REF)
2601 if (TYPE_P (TREE_OPERAND (decl, 0)))
2602 *qualifying_class = TREE_OPERAND (decl, 0);
2603 return decl;
2605 /* Check to see if DECL is a local variable in a context
2606 where that is forbidden. */
2607 if (parser->local_variables_forbidden_p
2608 && local_variable_p (decl))
2610 /* It might be that we only found DECL because we are
2611 trying to be generous with pre-ISO scoping rules.
2612 For example, consider:
2614 int i;
2615 void g() {
2616 for (int i = 0; i < 10; ++i) {}
2617 extern void f(int j = i);
2620 Here, name look up will originally find the out
2621 of scope `i'. We need to issue a warning message,
2622 but then use the global `i'. */
2623 decl = check_for_out_of_scope_variable (decl);
2624 if (local_variable_p (decl))
2626 error ("local variable `%D' may not appear in this context",
2627 decl);
2628 return error_mark_node;
2633 decl = finish_id_expression (id_expression, decl, parser->scope,
2634 idk, qualifying_class,
2635 parser->integral_constant_expression_p,
2636 parser->allow_non_integral_constant_expression_p,
2637 &parser->non_integral_constant_expression_p,
2638 &error_msg);
2639 if (error_msg)
2640 cp_parser_error (parser, error_msg);
2641 return decl;
2644 /* Anything else is an error. */
2645 default:
2646 cp_parser_error (parser, "expected primary-expression");
2647 return error_mark_node;
2651 /* Parse an id-expression.
2653 id-expression:
2654 unqualified-id
2655 qualified-id
2657 qualified-id:
2658 :: [opt] nested-name-specifier template [opt] unqualified-id
2659 :: identifier
2660 :: operator-function-id
2661 :: template-id
2663 Return a representation of the unqualified portion of the
2664 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2665 a `::' or nested-name-specifier.
2667 Often, if the id-expression was a qualified-id, the caller will
2668 want to make a SCOPE_REF to represent the qualified-id. This
2669 function does not do this in order to avoid wastefully creating
2670 SCOPE_REFs when they are not required.
2672 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2673 `template' keyword.
2675 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2676 uninstantiated templates.
2678 If *TEMPLATE_P is non-NULL, it is set to true iff the
2679 `template' keyword is used to explicitly indicate that the entity
2680 named is a template.
2682 If DECLARATOR_P is true, the id-expression is appearing as part of
2683 a declarator, rather than as part of an expression. */
2685 static tree
2686 cp_parser_id_expression (cp_parser *parser,
2687 bool template_keyword_p,
2688 bool check_dependency_p,
2689 bool *template_p,
2690 bool declarator_p)
2692 bool global_scope_p;
2693 bool nested_name_specifier_p;
2695 /* Assume the `template' keyword was not used. */
2696 if (template_p)
2697 *template_p = false;
2699 /* Look for the optional `::' operator. */
2700 global_scope_p
2701 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2702 != NULL_TREE);
2703 /* Look for the optional nested-name-specifier. */
2704 nested_name_specifier_p
2705 = (cp_parser_nested_name_specifier_opt (parser,
2706 /*typename_keyword_p=*/false,
2707 check_dependency_p,
2708 /*type_p=*/false,
2709 declarator_p)
2710 != NULL_TREE);
2711 /* If there is a nested-name-specifier, then we are looking at
2712 the first qualified-id production. */
2713 if (nested_name_specifier_p)
2715 tree saved_scope;
2716 tree saved_object_scope;
2717 tree saved_qualifying_scope;
2718 tree unqualified_id;
2719 bool is_template;
2721 /* See if the next token is the `template' keyword. */
2722 if (!template_p)
2723 template_p = &is_template;
2724 *template_p = cp_parser_optional_template_keyword (parser);
2725 /* Name lookup we do during the processing of the
2726 unqualified-id might obliterate SCOPE. */
2727 saved_scope = parser->scope;
2728 saved_object_scope = parser->object_scope;
2729 saved_qualifying_scope = parser->qualifying_scope;
2730 /* Process the final unqualified-id. */
2731 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2732 check_dependency_p,
2733 declarator_p);
2734 /* Restore the SAVED_SCOPE for our caller. */
2735 parser->scope = saved_scope;
2736 parser->object_scope = saved_object_scope;
2737 parser->qualifying_scope = saved_qualifying_scope;
2739 return unqualified_id;
2741 /* Otherwise, if we are in global scope, then we are looking at one
2742 of the other qualified-id productions. */
2743 else if (global_scope_p)
2745 cp_token *token;
2746 tree id;
2748 /* Peek at the next token. */
2749 token = cp_lexer_peek_token (parser->lexer);
2751 /* If it's an identifier, and the next token is not a "<", then
2752 we can avoid the template-id case. This is an optimization
2753 for this common case. */
2754 if (token->type == CPP_NAME
2755 && !cp_parser_nth_token_starts_template_argument_list_p
2756 (parser, 2))
2757 return cp_parser_identifier (parser);
2759 cp_parser_parse_tentatively (parser);
2760 /* Try a template-id. */
2761 id = cp_parser_template_id (parser,
2762 /*template_keyword_p=*/false,
2763 /*check_dependency_p=*/true,
2764 declarator_p);
2765 /* If that worked, we're done. */
2766 if (cp_parser_parse_definitely (parser))
2767 return id;
2769 /* Peek at the next token. (Changes in the token buffer may
2770 have invalidated the pointer obtained above.) */
2771 token = cp_lexer_peek_token (parser->lexer);
2773 switch (token->type)
2775 case CPP_NAME:
2776 return cp_parser_identifier (parser);
2778 case CPP_KEYWORD:
2779 if (token->keyword == RID_OPERATOR)
2780 return cp_parser_operator_function_id (parser);
2781 /* Fall through. */
2783 default:
2784 cp_parser_error (parser, "expected id-expression");
2785 return error_mark_node;
2788 else
2789 return cp_parser_unqualified_id (parser, template_keyword_p,
2790 /*check_dependency_p=*/true,
2791 declarator_p);
2794 /* Parse an unqualified-id.
2796 unqualified-id:
2797 identifier
2798 operator-function-id
2799 conversion-function-id
2800 ~ class-name
2801 template-id
2803 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2804 keyword, in a construct like `A::template ...'.
2806 Returns a representation of unqualified-id. For the `identifier'
2807 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2808 production a BIT_NOT_EXPR is returned; the operand of the
2809 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2810 other productions, see the documentation accompanying the
2811 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2812 names are looked up in uninstantiated templates. If DECLARATOR_P
2813 is true, the unqualified-id is appearing as part of a declarator,
2814 rather than as part of an expression. */
2816 static tree
2817 cp_parser_unqualified_id (cp_parser* parser,
2818 bool template_keyword_p,
2819 bool check_dependency_p,
2820 bool declarator_p)
2822 cp_token *token;
2824 /* Peek at the next token. */
2825 token = cp_lexer_peek_token (parser->lexer);
2827 switch (token->type)
2829 case CPP_NAME:
2831 tree id;
2833 /* We don't know yet whether or not this will be a
2834 template-id. */
2835 cp_parser_parse_tentatively (parser);
2836 /* Try a template-id. */
2837 id = cp_parser_template_id (parser, template_keyword_p,
2838 check_dependency_p,
2839 declarator_p);
2840 /* If it worked, we're done. */
2841 if (cp_parser_parse_definitely (parser))
2842 return id;
2843 /* Otherwise, it's an ordinary identifier. */
2844 return cp_parser_identifier (parser);
2847 case CPP_TEMPLATE_ID:
2848 return cp_parser_template_id (parser, template_keyword_p,
2849 check_dependency_p,
2850 declarator_p);
2852 case CPP_COMPL:
2854 tree type_decl;
2855 tree qualifying_scope;
2856 tree object_scope;
2857 tree scope;
2858 bool done;
2860 /* Consume the `~' token. */
2861 cp_lexer_consume_token (parser->lexer);
2862 /* Parse the class-name. The standard, as written, seems to
2863 say that:
2865 template <typename T> struct S { ~S (); };
2866 template <typename T> S<T>::~S() {}
2868 is invalid, since `~' must be followed by a class-name, but
2869 `S<T>' is dependent, and so not known to be a class.
2870 That's not right; we need to look in uninstantiated
2871 templates. A further complication arises from:
2873 template <typename T> void f(T t) {
2874 t.T::~T();
2877 Here, it is not possible to look up `T' in the scope of `T'
2878 itself. We must look in both the current scope, and the
2879 scope of the containing complete expression.
2881 Yet another issue is:
2883 struct S {
2884 int S;
2885 ~S();
2888 S::~S() {}
2890 The standard does not seem to say that the `S' in `~S'
2891 should refer to the type `S' and not the data member
2892 `S::S'. */
2894 /* DR 244 says that we look up the name after the "~" in the
2895 same scope as we looked up the qualifying name. That idea
2896 isn't fully worked out; it's more complicated than that. */
2897 scope = parser->scope;
2898 object_scope = parser->object_scope;
2899 qualifying_scope = parser->qualifying_scope;
2901 /* If the name is of the form "X::~X" it's OK. */
2902 if (scope && TYPE_P (scope)
2903 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2904 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2905 == CPP_OPEN_PAREN)
2906 && (cp_lexer_peek_token (parser->lexer)->value
2907 == TYPE_IDENTIFIER (scope)))
2909 cp_lexer_consume_token (parser->lexer);
2910 return build_nt (BIT_NOT_EXPR, scope);
2913 /* If there was an explicit qualification (S::~T), first look
2914 in the scope given by the qualification (i.e., S). */
2915 done = false;
2916 type_decl = NULL_TREE;
2917 if (scope)
2919 cp_parser_parse_tentatively (parser);
2920 type_decl = cp_parser_class_name (parser,
2921 /*typename_keyword_p=*/false,
2922 /*template_keyword_p=*/false,
2923 /*type_p=*/false,
2924 /*check_dependency=*/false,
2925 /*class_head_p=*/false,
2926 declarator_p);
2927 if (cp_parser_parse_definitely (parser))
2928 done = true;
2930 /* In "N::S::~S", look in "N" as well. */
2931 if (!done && scope && qualifying_scope)
2933 cp_parser_parse_tentatively (parser);
2934 parser->scope = qualifying_scope;
2935 parser->object_scope = NULL_TREE;
2936 parser->qualifying_scope = NULL_TREE;
2937 type_decl
2938 = cp_parser_class_name (parser,
2939 /*typename_keyword_p=*/false,
2940 /*template_keyword_p=*/false,
2941 /*type_p=*/false,
2942 /*check_dependency=*/false,
2943 /*class_head_p=*/false,
2944 declarator_p);
2945 if (cp_parser_parse_definitely (parser))
2946 done = true;
2948 /* In "p->S::~T", look in the scope given by "*p" as well. */
2949 else if (!done && object_scope)
2951 cp_parser_parse_tentatively (parser);
2952 parser->scope = object_scope;
2953 parser->object_scope = NULL_TREE;
2954 parser->qualifying_scope = NULL_TREE;
2955 type_decl
2956 = cp_parser_class_name (parser,
2957 /*typename_keyword_p=*/false,
2958 /*template_keyword_p=*/false,
2959 /*type_p=*/false,
2960 /*check_dependency=*/false,
2961 /*class_head_p=*/false,
2962 declarator_p);
2963 if (cp_parser_parse_definitely (parser))
2964 done = true;
2966 /* Look in the surrounding context. */
2967 if (!done)
2969 parser->scope = NULL_TREE;
2970 parser->object_scope = NULL_TREE;
2971 parser->qualifying_scope = NULL_TREE;
2972 type_decl
2973 = cp_parser_class_name (parser,
2974 /*typename_keyword_p=*/false,
2975 /*template_keyword_p=*/false,
2976 /*type_p=*/false,
2977 /*check_dependency=*/false,
2978 /*class_head_p=*/false,
2979 declarator_p);
2981 /* If an error occurred, assume that the name of the
2982 destructor is the same as the name of the qualifying
2983 class. That allows us to keep parsing after running
2984 into ill-formed destructor names. */
2985 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2986 return build_nt (BIT_NOT_EXPR, scope);
2987 else if (type_decl == error_mark_node)
2988 return error_mark_node;
2990 /* [class.dtor]
2992 A typedef-name that names a class shall not be used as the
2993 identifier in the declarator for a destructor declaration. */
2994 if (declarator_p
2995 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2996 && !DECL_SELF_REFERENCE_P (type_decl))
2997 error ("typedef-name `%D' used as destructor declarator",
2998 type_decl);
3000 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3003 case CPP_KEYWORD:
3004 if (token->keyword == RID_OPERATOR)
3006 tree id;
3008 /* This could be a template-id, so we try that first. */
3009 cp_parser_parse_tentatively (parser);
3010 /* Try a template-id. */
3011 id = cp_parser_template_id (parser, template_keyword_p,
3012 /*check_dependency_p=*/true,
3013 declarator_p);
3014 /* If that worked, we're done. */
3015 if (cp_parser_parse_definitely (parser))
3016 return id;
3017 /* We still don't know whether we're looking at an
3018 operator-function-id or a conversion-function-id. */
3019 cp_parser_parse_tentatively (parser);
3020 /* Try an operator-function-id. */
3021 id = cp_parser_operator_function_id (parser);
3022 /* If that didn't work, try a conversion-function-id. */
3023 if (!cp_parser_parse_definitely (parser))
3024 id = cp_parser_conversion_function_id (parser);
3026 return id;
3028 /* Fall through. */
3030 default:
3031 cp_parser_error (parser, "expected unqualified-id");
3032 return error_mark_node;
3036 /* Parse an (optional) nested-name-specifier.
3038 nested-name-specifier:
3039 class-or-namespace-name :: nested-name-specifier [opt]
3040 class-or-namespace-name :: template nested-name-specifier [opt]
3042 PARSER->SCOPE should be set appropriately before this function is
3043 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3044 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3045 in name lookups.
3047 Sets PARSER->SCOPE to the class (TYPE) or namespace
3048 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3049 it unchanged if there is no nested-name-specifier. Returns the new
3050 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3052 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3053 part of a declaration and/or decl-specifier. */
3055 static tree
3056 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3057 bool typename_keyword_p,
3058 bool check_dependency_p,
3059 bool type_p,
3060 bool is_declaration)
3062 bool success = false;
3063 tree access_check = NULL_TREE;
3064 ptrdiff_t start;
3065 cp_token* token;
3067 /* If the next token corresponds to a nested name specifier, there
3068 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3069 false, it may have been true before, in which case something
3070 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3071 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3072 CHECK_DEPENDENCY_P is false, we have to fall through into the
3073 main loop. */
3074 if (check_dependency_p
3075 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3077 cp_parser_pre_parsed_nested_name_specifier (parser);
3078 return parser->scope;
3081 /* Remember where the nested-name-specifier starts. */
3082 if (cp_parser_parsing_tentatively (parser)
3083 && !cp_parser_committed_to_tentative_parse (parser))
3085 token = cp_lexer_peek_token (parser->lexer);
3086 start = cp_lexer_token_difference (parser->lexer,
3087 parser->lexer->first_token,
3088 token);
3090 else
3091 start = -1;
3093 push_deferring_access_checks (dk_deferred);
3095 while (true)
3097 tree new_scope;
3098 tree old_scope;
3099 tree saved_qualifying_scope;
3100 bool template_keyword_p;
3102 /* Spot cases that cannot be the beginning of a
3103 nested-name-specifier. */
3104 token = cp_lexer_peek_token (parser->lexer);
3106 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3107 the already parsed nested-name-specifier. */
3108 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3110 /* Grab the nested-name-specifier and continue the loop. */
3111 cp_parser_pre_parsed_nested_name_specifier (parser);
3112 success = true;
3113 continue;
3116 /* Spot cases that cannot be the beginning of a
3117 nested-name-specifier. On the second and subsequent times
3118 through the loop, we look for the `template' keyword. */
3119 if (success && token->keyword == RID_TEMPLATE)
3121 /* A template-id can start a nested-name-specifier. */
3122 else if (token->type == CPP_TEMPLATE_ID)
3124 else
3126 /* If the next token is not an identifier, then it is
3127 definitely not a class-or-namespace-name. */
3128 if (token->type != CPP_NAME)
3129 break;
3130 /* If the following token is neither a `<' (to begin a
3131 template-id), nor a `::', then we are not looking at a
3132 nested-name-specifier. */
3133 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3134 if (token->type != CPP_SCOPE
3135 && !cp_parser_nth_token_starts_template_argument_list_p
3136 (parser, 2))
3137 break;
3140 /* The nested-name-specifier is optional, so we parse
3141 tentatively. */
3142 cp_parser_parse_tentatively (parser);
3144 /* Look for the optional `template' keyword, if this isn't the
3145 first time through the loop. */
3146 if (success)
3147 template_keyword_p = cp_parser_optional_template_keyword (parser);
3148 else
3149 template_keyword_p = false;
3151 /* Save the old scope since the name lookup we are about to do
3152 might destroy it. */
3153 old_scope = parser->scope;
3154 saved_qualifying_scope = parser->qualifying_scope;
3155 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3156 look up names in "X<T>::I" in order to determine that "Y" is
3157 a template. So, if we have a typename at this point, we make
3158 an effort to look through it. */
3159 if (is_declaration
3160 && !typename_keyword_p
3161 && parser->scope
3162 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3163 parser->scope = resolve_typename_type (parser->scope,
3164 /*only_current_p=*/false);
3165 /* Parse the qualifying entity. */
3166 new_scope
3167 = cp_parser_class_or_namespace_name (parser,
3168 typename_keyword_p,
3169 template_keyword_p,
3170 check_dependency_p,
3171 type_p,
3172 is_declaration);
3173 /* Look for the `::' token. */
3174 cp_parser_require (parser, CPP_SCOPE, "`::'");
3176 /* If we found what we wanted, we keep going; otherwise, we're
3177 done. */
3178 if (!cp_parser_parse_definitely (parser))
3180 bool error_p = false;
3182 /* Restore the OLD_SCOPE since it was valid before the
3183 failed attempt at finding the last
3184 class-or-namespace-name. */
3185 parser->scope = old_scope;
3186 parser->qualifying_scope = saved_qualifying_scope;
3187 /* If the next token is an identifier, and the one after
3188 that is a `::', then any valid interpretation would have
3189 found a class-or-namespace-name. */
3190 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3191 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3192 == CPP_SCOPE)
3193 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3194 != CPP_COMPL))
3196 token = cp_lexer_consume_token (parser->lexer);
3197 if (!error_p)
3199 tree decl;
3201 decl = cp_parser_lookup_name_simple (parser, token->value);
3202 if (TREE_CODE (decl) == TEMPLATE_DECL)
3203 error ("`%D' used without template parameters",
3204 decl);
3205 else
3206 cp_parser_name_lookup_error
3207 (parser, token->value, decl,
3208 "is not a class or namespace");
3209 parser->scope = NULL_TREE;
3210 error_p = true;
3211 /* Treat this as a successful nested-name-specifier
3212 due to:
3214 [basic.lookup.qual]
3216 If the name found is not a class-name (clause
3217 _class_) or namespace-name (_namespace.def_), the
3218 program is ill-formed. */
3219 success = true;
3221 cp_lexer_consume_token (parser->lexer);
3223 break;
3226 /* We've found one valid nested-name-specifier. */
3227 success = true;
3228 /* Make sure we look in the right scope the next time through
3229 the loop. */
3230 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3231 ? TREE_TYPE (new_scope)
3232 : new_scope);
3233 /* If it is a class scope, try to complete it; we are about to
3234 be looking up names inside the class. */
3235 if (TYPE_P (parser->scope)
3236 /* Since checking types for dependency can be expensive,
3237 avoid doing it if the type is already complete. */
3238 && !COMPLETE_TYPE_P (parser->scope)
3239 /* Do not try to complete dependent types. */
3240 && !dependent_type_p (parser->scope))
3241 complete_type (parser->scope);
3244 /* Retrieve any deferred checks. Do not pop this access checks yet
3245 so the memory will not be reclaimed during token replacing below. */
3246 access_check = get_deferred_access_checks ();
3248 /* If parsing tentatively, replace the sequence of tokens that makes
3249 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3250 token. That way, should we re-parse the token stream, we will
3251 not have to repeat the effort required to do the parse, nor will
3252 we issue duplicate error messages. */
3253 if (success && start >= 0)
3255 /* Find the token that corresponds to the start of the
3256 template-id. */
3257 token = cp_lexer_advance_token (parser->lexer,
3258 parser->lexer->first_token,
3259 start);
3261 /* Reset the contents of the START token. */
3262 token->type = CPP_NESTED_NAME_SPECIFIER;
3263 token->value = build_tree_list (access_check, parser->scope);
3264 TREE_TYPE (token->value) = parser->qualifying_scope;
3265 token->keyword = RID_MAX;
3266 /* Purge all subsequent tokens. */
3267 cp_lexer_purge_tokens_after (parser->lexer, token);
3270 pop_deferring_access_checks ();
3271 return success ? parser->scope : NULL_TREE;
3274 /* Parse a nested-name-specifier. See
3275 cp_parser_nested_name_specifier_opt for details. This function
3276 behaves identically, except that it will an issue an error if no
3277 nested-name-specifier is present, and it will return
3278 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3279 is present. */
3281 static tree
3282 cp_parser_nested_name_specifier (cp_parser *parser,
3283 bool typename_keyword_p,
3284 bool check_dependency_p,
3285 bool type_p,
3286 bool is_declaration)
3288 tree scope;
3290 /* Look for the nested-name-specifier. */
3291 scope = cp_parser_nested_name_specifier_opt (parser,
3292 typename_keyword_p,
3293 check_dependency_p,
3294 type_p,
3295 is_declaration);
3296 /* If it was not present, issue an error message. */
3297 if (!scope)
3299 cp_parser_error (parser, "expected nested-name-specifier");
3300 parser->scope = NULL_TREE;
3301 return error_mark_node;
3304 return scope;
3307 /* Parse a class-or-namespace-name.
3309 class-or-namespace-name:
3310 class-name
3311 namespace-name
3313 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3314 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3315 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3316 TYPE_P is TRUE iff the next name should be taken as a class-name,
3317 even the same name is declared to be another entity in the same
3318 scope.
3320 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3321 specified by the class-or-namespace-name. If neither is found the
3322 ERROR_MARK_NODE is returned. */
3324 static tree
3325 cp_parser_class_or_namespace_name (cp_parser *parser,
3326 bool typename_keyword_p,
3327 bool template_keyword_p,
3328 bool check_dependency_p,
3329 bool type_p,
3330 bool is_declaration)
3332 tree saved_scope;
3333 tree saved_qualifying_scope;
3334 tree saved_object_scope;
3335 tree scope;
3336 bool only_class_p;
3338 /* Before we try to parse the class-name, we must save away the
3339 current PARSER->SCOPE since cp_parser_class_name will destroy
3340 it. */
3341 saved_scope = parser->scope;
3342 saved_qualifying_scope = parser->qualifying_scope;
3343 saved_object_scope = parser->object_scope;
3344 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3345 there is no need to look for a namespace-name. */
3346 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3347 if (!only_class_p)
3348 cp_parser_parse_tentatively (parser);
3349 scope = cp_parser_class_name (parser,
3350 typename_keyword_p,
3351 template_keyword_p,
3352 type_p,
3353 check_dependency_p,
3354 /*class_head_p=*/false,
3355 is_declaration);
3356 /* If that didn't work, try for a namespace-name. */
3357 if (!only_class_p && !cp_parser_parse_definitely (parser))
3359 /* Restore the saved scope. */
3360 parser->scope = saved_scope;
3361 parser->qualifying_scope = saved_qualifying_scope;
3362 parser->object_scope = saved_object_scope;
3363 /* If we are not looking at an identifier followed by the scope
3364 resolution operator, then this is not part of a
3365 nested-name-specifier. (Note that this function is only used
3366 to parse the components of a nested-name-specifier.) */
3367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3368 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3369 return error_mark_node;
3370 scope = cp_parser_namespace_name (parser);
3373 return scope;
3376 /* Parse a postfix-expression.
3378 postfix-expression:
3379 primary-expression
3380 postfix-expression [ expression ]
3381 postfix-expression ( expression-list [opt] )
3382 simple-type-specifier ( expression-list [opt] )
3383 typename :: [opt] nested-name-specifier identifier
3384 ( expression-list [opt] )
3385 typename :: [opt] nested-name-specifier template [opt] template-id
3386 ( expression-list [opt] )
3387 postfix-expression . template [opt] id-expression
3388 postfix-expression -> template [opt] id-expression
3389 postfix-expression . pseudo-destructor-name
3390 postfix-expression -> pseudo-destructor-name
3391 postfix-expression ++
3392 postfix-expression --
3393 dynamic_cast < type-id > ( expression )
3394 static_cast < type-id > ( expression )
3395 reinterpret_cast < type-id > ( expression )
3396 const_cast < type-id > ( expression )
3397 typeid ( expression )
3398 typeid ( type-id )
3400 GNU Extension:
3402 postfix-expression:
3403 ( type-id ) { initializer-list , [opt] }
3405 This extension is a GNU version of the C99 compound-literal
3406 construct. (The C99 grammar uses `type-name' instead of `type-id',
3407 but they are essentially the same concept.)
3409 If ADDRESS_P is true, the postfix expression is the operand of the
3410 `&' operator.
3412 Returns a representation of the expression. */
3414 static tree
3415 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3417 cp_token *token;
3418 enum rid keyword;
3419 cp_id_kind idk = CP_ID_KIND_NONE;
3420 tree postfix_expression = NULL_TREE;
3421 /* Non-NULL only if the current postfix-expression can be used to
3422 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3423 class used to qualify the member. */
3424 tree qualifying_class = NULL_TREE;
3426 /* Peek at the next token. */
3427 token = cp_lexer_peek_token (parser->lexer);
3428 /* Some of the productions are determined by keywords. */
3429 keyword = token->keyword;
3430 switch (keyword)
3432 case RID_DYNCAST:
3433 case RID_STATCAST:
3434 case RID_REINTCAST:
3435 case RID_CONSTCAST:
3437 tree type;
3438 tree expression;
3439 const char *saved_message;
3441 /* All of these can be handled in the same way from the point
3442 of view of parsing. Begin by consuming the token
3443 identifying the cast. */
3444 cp_lexer_consume_token (parser->lexer);
3446 /* New types cannot be defined in the cast. */
3447 saved_message = parser->type_definition_forbidden_message;
3448 parser->type_definition_forbidden_message
3449 = "types may not be defined in casts";
3451 /* Look for the opening `<'. */
3452 cp_parser_require (parser, CPP_LESS, "`<'");
3453 /* Parse the type to which we are casting. */
3454 type = cp_parser_type_id (parser);
3455 /* Look for the closing `>'. */
3456 cp_parser_require (parser, CPP_GREATER, "`>'");
3457 /* Restore the old message. */
3458 parser->type_definition_forbidden_message = saved_message;
3460 /* And the expression which is being cast. */
3461 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3462 expression = cp_parser_expression (parser);
3463 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3465 /* Only type conversions to integral or enumeration types
3466 can be used in constant-expressions. */
3467 if (parser->integral_constant_expression_p
3468 && !dependent_type_p (type)
3469 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3470 /* A cast to pointer or reference type is allowed in the
3471 implementation of "offsetof". */
3472 && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3473 && (cp_parser_non_integral_constant_expression
3474 (parser,
3475 "a cast to a type other than an integral or "
3476 "enumeration type")))
3477 return error_mark_node;
3479 switch (keyword)
3481 case RID_DYNCAST:
3482 postfix_expression
3483 = build_dynamic_cast (type, expression);
3484 break;
3485 case RID_STATCAST:
3486 postfix_expression
3487 = build_static_cast (type, expression);
3488 break;
3489 case RID_REINTCAST:
3490 postfix_expression
3491 = build_reinterpret_cast (type, expression);
3492 break;
3493 case RID_CONSTCAST:
3494 postfix_expression
3495 = build_const_cast (type, expression);
3496 break;
3497 default:
3498 abort ();
3501 break;
3503 case RID_TYPEID:
3505 tree type;
3506 const char *saved_message;
3507 bool saved_in_type_id_in_expr_p;
3509 /* Consume the `typeid' token. */
3510 cp_lexer_consume_token (parser->lexer);
3511 /* Look for the `(' token. */
3512 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3513 /* Types cannot be defined in a `typeid' expression. */
3514 saved_message = parser->type_definition_forbidden_message;
3515 parser->type_definition_forbidden_message
3516 = "types may not be defined in a `typeid\' expression";
3517 /* We can't be sure yet whether we're looking at a type-id or an
3518 expression. */
3519 cp_parser_parse_tentatively (parser);
3520 /* Try a type-id first. */
3521 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3522 parser->in_type_id_in_expr_p = true;
3523 type = cp_parser_type_id (parser);
3524 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3525 /* Look for the `)' token. Otherwise, we can't be sure that
3526 we're not looking at an expression: consider `typeid (int
3527 (3))', for example. */
3528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3529 /* If all went well, simply lookup the type-id. */
3530 if (cp_parser_parse_definitely (parser))
3531 postfix_expression = get_typeid (type);
3532 /* Otherwise, fall back to the expression variant. */
3533 else
3535 tree expression;
3537 /* Look for an expression. */
3538 expression = cp_parser_expression (parser);
3539 /* Compute its typeid. */
3540 postfix_expression = build_typeid (expression);
3541 /* Look for the `)' token. */
3542 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3544 /* `typeid' may not appear in an integral constant expression. */
3545 if (cp_parser_non_integral_constant_expression(parser,
3546 "`typeid' operator"))
3547 return error_mark_node;
3548 /* Restore the saved message. */
3549 parser->type_definition_forbidden_message = saved_message;
3551 break;
3553 case RID_TYPENAME:
3555 tree type;
3557 /* The syntax permitted here is the same permitted for an
3558 elaborated-type-specifier. */
3559 type = cp_parser_elaborated_type_specifier (parser,
3560 /*is_friend=*/false,
3561 /*is_declaration=*/false);
3562 postfix_expression = cp_parser_functional_cast (parser, type);
3564 break;
3566 default:
3568 tree type;
3570 /* If the next thing is a simple-type-specifier, we may be
3571 looking at a functional cast. We could also be looking at
3572 an id-expression. So, we try the functional cast, and if
3573 that doesn't work we fall back to the primary-expression. */
3574 cp_parser_parse_tentatively (parser);
3575 /* Look for the simple-type-specifier. */
3576 type = cp_parser_simple_type_specifier (parser,
3577 CP_PARSER_FLAGS_NONE,
3578 /*identifier_p=*/false);
3579 /* Parse the cast itself. */
3580 if (!cp_parser_error_occurred (parser))
3581 postfix_expression
3582 = cp_parser_functional_cast (parser, type);
3583 /* If that worked, we're done. */
3584 if (cp_parser_parse_definitely (parser))
3585 break;
3587 /* If the functional-cast didn't work out, try a
3588 compound-literal. */
3589 if (cp_parser_allow_gnu_extensions_p (parser)
3590 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3592 tree initializer_list = NULL_TREE;
3593 bool saved_in_type_id_in_expr_p;
3595 cp_parser_parse_tentatively (parser);
3596 /* Consume the `('. */
3597 cp_lexer_consume_token (parser->lexer);
3598 /* Parse the type. */
3599 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3600 parser->in_type_id_in_expr_p = true;
3601 type = cp_parser_type_id (parser);
3602 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3603 /* Look for the `)'. */
3604 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3605 /* Look for the `{'. */
3606 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3607 /* If things aren't going well, there's no need to
3608 keep going. */
3609 if (!cp_parser_error_occurred (parser))
3611 bool non_constant_p;
3612 /* Parse the initializer-list. */
3613 initializer_list
3614 = cp_parser_initializer_list (parser, &non_constant_p);
3615 /* Allow a trailing `,'. */
3616 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3617 cp_lexer_consume_token (parser->lexer);
3618 /* Look for the final `}'. */
3619 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3621 /* If that worked, we're definitely looking at a
3622 compound-literal expression. */
3623 if (cp_parser_parse_definitely (parser))
3625 /* Warn the user that a compound literal is not
3626 allowed in standard C++. */
3627 if (pedantic)
3628 pedwarn ("ISO C++ forbids compound-literals");
3629 /* Form the representation of the compound-literal. */
3630 postfix_expression
3631 = finish_compound_literal (type, initializer_list);
3632 break;
3636 /* It must be a primary-expression. */
3637 postfix_expression = cp_parser_primary_expression (parser,
3638 &idk,
3639 &qualifying_class);
3641 break;
3644 /* If we were avoiding committing to the processing of a
3645 qualified-id until we knew whether or not we had a
3646 pointer-to-member, we now know. */
3647 if (qualifying_class)
3649 bool done;
3651 /* Peek at the next token. */
3652 token = cp_lexer_peek_token (parser->lexer);
3653 done = (token->type != CPP_OPEN_SQUARE
3654 && token->type != CPP_OPEN_PAREN
3655 && token->type != CPP_DOT
3656 && token->type != CPP_DEREF
3657 && token->type != CPP_PLUS_PLUS
3658 && token->type != CPP_MINUS_MINUS);
3660 postfix_expression = finish_qualified_id_expr (qualifying_class,
3661 postfix_expression,
3662 done,
3663 address_p);
3664 if (done)
3665 return postfix_expression;
3668 /* Keep looping until the postfix-expression is complete. */
3669 while (true)
3671 if (idk == CP_ID_KIND_UNQUALIFIED
3672 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3673 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3674 /* It is not a Koenig lookup function call. */
3675 postfix_expression
3676 = unqualified_name_lookup_error (postfix_expression);
3678 /* Peek at the next token. */
3679 token = cp_lexer_peek_token (parser->lexer);
3681 switch (token->type)
3683 case CPP_OPEN_SQUARE:
3684 /* postfix-expression [ expression ] */
3686 tree index;
3688 /* Consume the `[' token. */
3689 cp_lexer_consume_token (parser->lexer);
3690 /* Parse the index expression. */
3691 index = cp_parser_expression (parser);
3692 /* Look for the closing `]'. */
3693 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3695 /* Build the ARRAY_REF. */
3696 postfix_expression
3697 = grok_array_decl (postfix_expression, index);
3698 idk = CP_ID_KIND_NONE;
3699 /* Array references are not permitted in
3700 constant-expressions (but they are allowed
3701 in offsetof). */
3702 if (!parser->in_offsetof_p
3703 && cp_parser_non_integral_constant_expression
3704 (parser, "an array reference"))
3705 postfix_expression = error_mark_node;
3707 break;
3709 case CPP_OPEN_PAREN:
3710 /* postfix-expression ( expression-list [opt] ) */
3712 bool koenig_p;
3713 tree args = (cp_parser_parenthesized_expression_list
3714 (parser, false, /*non_constant_p=*/NULL));
3716 if (args == error_mark_node)
3718 postfix_expression = error_mark_node;
3719 break;
3722 /* Function calls are not permitted in
3723 constant-expressions. */
3724 if (cp_parser_non_integral_constant_expression (parser,
3725 "a function call"))
3727 postfix_expression = error_mark_node;
3728 break;
3731 koenig_p = false;
3732 if (idk == CP_ID_KIND_UNQUALIFIED)
3734 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3736 if (args)
3738 koenig_p = true;
3739 postfix_expression
3740 = perform_koenig_lookup (postfix_expression, args);
3742 else
3743 postfix_expression
3744 = unqualified_fn_lookup_error (postfix_expression);
3746 /* We do not perform argument-dependent lookup if
3747 normal lookup finds a non-function, in accordance
3748 with the expected resolution of DR 218. */
3749 else if (args && is_overloaded_fn (postfix_expression))
3751 tree fn = get_first_fn (postfix_expression);
3752 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3753 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3754 /* Only do argument dependent lookup if regular
3755 lookup does not find a set of member functions.
3756 [basic.lookup.koenig]/2a */
3757 if (!DECL_FUNCTION_MEMBER_P (fn))
3759 koenig_p = true;
3760 postfix_expression
3761 = perform_koenig_lookup (postfix_expression, args);
3766 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3768 tree instance = TREE_OPERAND (postfix_expression, 0);
3769 tree fn = TREE_OPERAND (postfix_expression, 1);
3771 if (processing_template_decl
3772 && (type_dependent_expression_p (instance)
3773 || (!BASELINK_P (fn)
3774 && TREE_CODE (fn) != FIELD_DECL)
3775 || type_dependent_expression_p (fn)
3776 || any_type_dependent_arguments_p (args)))
3778 postfix_expression
3779 = build_min_nt (CALL_EXPR, postfix_expression, args);
3780 break;
3783 if (BASELINK_P (fn))
3784 postfix_expression
3785 = (build_new_method_call
3786 (instance, fn, args, NULL_TREE,
3787 (idk == CP_ID_KIND_QUALIFIED
3788 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3789 else
3790 postfix_expression
3791 = finish_call_expr (postfix_expression, args,
3792 /*disallow_virtual=*/false,
3793 /*koenig_p=*/false);
3795 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3796 || TREE_CODE (postfix_expression) == MEMBER_REF
3797 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3798 postfix_expression = (build_offset_ref_call_from_tree
3799 (postfix_expression, args));
3800 else if (idk == CP_ID_KIND_QUALIFIED)
3801 /* A call to a static class member, or a namespace-scope
3802 function. */
3803 postfix_expression
3804 = finish_call_expr (postfix_expression, args,
3805 /*disallow_virtual=*/true,
3806 koenig_p);
3807 else
3808 /* All other function calls. */
3809 postfix_expression
3810 = finish_call_expr (postfix_expression, args,
3811 /*disallow_virtual=*/false,
3812 koenig_p);
3814 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3815 idk = CP_ID_KIND_NONE;
3817 break;
3819 case CPP_DOT:
3820 case CPP_DEREF:
3821 /* postfix-expression . template [opt] id-expression
3822 postfix-expression . pseudo-destructor-name
3823 postfix-expression -> template [opt] id-expression
3824 postfix-expression -> pseudo-destructor-name */
3826 tree name;
3827 bool dependent_p;
3828 bool template_p;
3829 bool pseudo_destructor_p;
3830 tree scope = NULL_TREE;
3831 enum cpp_ttype token_type = token->type;
3833 /* If this is a `->' operator, dereference the pointer. */
3834 if (token->type == CPP_DEREF)
3835 postfix_expression = build_x_arrow (postfix_expression);
3836 /* Check to see whether or not the expression is
3837 type-dependent. */
3838 dependent_p = type_dependent_expression_p (postfix_expression);
3839 /* The identifier following the `->' or `.' is not
3840 qualified. */
3841 parser->scope = NULL_TREE;
3842 parser->qualifying_scope = NULL_TREE;
3843 parser->object_scope = NULL_TREE;
3844 idk = CP_ID_KIND_NONE;
3845 /* Enter the scope corresponding to the type of the object
3846 given by the POSTFIX_EXPRESSION. */
3847 if (!dependent_p
3848 && TREE_TYPE (postfix_expression) != NULL_TREE)
3850 scope = TREE_TYPE (postfix_expression);
3851 /* According to the standard, no expression should
3852 ever have reference type. Unfortunately, we do not
3853 currently match the standard in this respect in
3854 that our internal representation of an expression
3855 may have reference type even when the standard says
3856 it does not. Therefore, we have to manually obtain
3857 the underlying type here. */
3858 scope = non_reference (scope);
3859 /* The type of the POSTFIX_EXPRESSION must be
3860 complete. */
3861 scope = complete_type_or_else (scope, NULL_TREE);
3862 /* Let the name lookup machinery know that we are
3863 processing a class member access expression. */
3864 parser->context->object_type = scope;
3865 /* If something went wrong, we want to be able to
3866 discern that case, as opposed to the case where
3867 there was no SCOPE due to the type of expression
3868 being dependent. */
3869 if (!scope)
3870 scope = error_mark_node;
3871 /* If the SCOPE was erroneous, make the various
3872 semantic analysis functions exit quickly -- and
3873 without issuing additional error messages. */
3874 if (scope == error_mark_node)
3875 postfix_expression = error_mark_node;
3878 /* Consume the `.' or `->' operator. */
3879 cp_lexer_consume_token (parser->lexer);
3881 /* Assume this expression is not a pseudo-destructor access. */
3882 pseudo_destructor_p = false;
3884 /* If the SCOPE is a scalar type, then, if this is a valid program,
3885 we must be looking at a pseudo-destructor-name. */
3886 if (scope && SCALAR_TYPE_P (scope))
3888 tree s = NULL_TREE;
3889 tree type;
3891 cp_parser_parse_tentatively (parser);
3892 /* Parse the pseudo-destructor-name. */
3893 cp_parser_pseudo_destructor_name (parser, &s, &type);
3894 if (cp_parser_parse_definitely (parser))
3896 pseudo_destructor_p = true;
3897 postfix_expression
3898 = finish_pseudo_destructor_expr (postfix_expression,
3899 s, TREE_TYPE (type));
3903 if (!pseudo_destructor_p)
3905 /* If the SCOPE is not a scalar type, we are looking
3906 at an ordinary class member access expression,
3907 rather than a pseudo-destructor-name. */
3908 template_p = cp_parser_optional_template_keyword (parser);
3909 /* Parse the id-expression. */
3910 name = cp_parser_id_expression (parser,
3911 template_p,
3912 /*check_dependency_p=*/true,
3913 /*template_p=*/NULL,
3914 /*declarator_p=*/false);
3915 /* In general, build a SCOPE_REF if the member name is
3916 qualified. However, if the name was not dependent
3917 and has already been resolved; there is no need to
3918 build the SCOPE_REF. For example;
3920 struct X { void f(); };
3921 template <typename T> void f(T* t) { t->X::f(); }
3923 Even though "t" is dependent, "X::f" is not and has
3924 been resolved to a BASELINK; there is no need to
3925 include scope information. */
3927 /* But we do need to remember that there was an explicit
3928 scope for virtual function calls. */
3929 if (parser->scope)
3930 idk = CP_ID_KIND_QUALIFIED;
3932 /* If the name is a template-id that names a type, we will
3933 get a TYPE_DECL here. That is invalid code. */
3934 if (TREE_CODE (name) == TYPE_DECL)
3936 error ("invalid use of `%D'", name);
3937 postfix_expression = error_mark_node;
3939 else
3941 if (name != error_mark_node && !BASELINK_P (name)
3942 && parser->scope)
3944 name = build_nt (SCOPE_REF, parser->scope, name);
3945 parser->scope = NULL_TREE;
3946 parser->qualifying_scope = NULL_TREE;
3947 parser->object_scope = NULL_TREE;
3949 if (scope && name && BASELINK_P (name))
3950 adjust_result_of_qualified_name_lookup
3951 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3952 postfix_expression = finish_class_member_access_expr
3953 (postfix_expression, name);
3957 /* We no longer need to look up names in the scope of the
3958 object on the left-hand side of the `.' or `->'
3959 operator. */
3960 parser->context->object_type = NULL_TREE;
3961 /* These operators may not appear in constant-expressions. */
3962 if (/* The "->" operator is allowed in the implementation
3963 of "offsetof". The "." operator may appear in the
3964 name of the member. */
3965 !parser->in_offsetof_p
3966 && (cp_parser_non_integral_constant_expression
3967 (parser,
3968 token_type == CPP_DEREF ? "'->'" : "`.'")))
3969 postfix_expression = error_mark_node;
3971 break;
3973 case CPP_PLUS_PLUS:
3974 /* postfix-expression ++ */
3975 /* Consume the `++' token. */
3976 cp_lexer_consume_token (parser->lexer);
3977 /* Generate a representation for the complete expression. */
3978 postfix_expression
3979 = finish_increment_expr (postfix_expression,
3980 POSTINCREMENT_EXPR);
3981 /* Increments may not appear in constant-expressions. */
3982 if (cp_parser_non_integral_constant_expression (parser,
3983 "an increment"))
3984 postfix_expression = error_mark_node;
3985 idk = CP_ID_KIND_NONE;
3986 break;
3988 case CPP_MINUS_MINUS:
3989 /* postfix-expression -- */
3990 /* Consume the `--' token. */
3991 cp_lexer_consume_token (parser->lexer);
3992 /* Generate a representation for the complete expression. */
3993 postfix_expression
3994 = finish_increment_expr (postfix_expression,
3995 POSTDECREMENT_EXPR);
3996 /* Decrements may not appear in constant-expressions. */
3997 if (cp_parser_non_integral_constant_expression (parser,
3998 "a decrement"))
3999 postfix_expression = error_mark_node;
4000 idk = CP_ID_KIND_NONE;
4001 break;
4003 default:
4004 return postfix_expression;
4008 /* We should never get here. */
4009 abort ();
4010 return error_mark_node;
4013 /* Parse a parenthesized expression-list.
4015 expression-list:
4016 assignment-expression
4017 expression-list, assignment-expression
4019 attribute-list:
4020 expression-list
4021 identifier
4022 identifier, expression-list
4024 Returns a TREE_LIST. The TREE_VALUE of each node is a
4025 representation of an assignment-expression. Note that a TREE_LIST
4026 is returned even if there is only a single expression in the list.
4027 error_mark_node is returned if the ( and or ) are
4028 missing. NULL_TREE is returned on no expressions. The parentheses
4029 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4030 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4031 indicates whether or not all of the expressions in the list were
4032 constant. */
4034 static tree
4035 cp_parser_parenthesized_expression_list (cp_parser* parser,
4036 bool is_attribute_list,
4037 bool *non_constant_p)
4039 tree expression_list = NULL_TREE;
4040 tree identifier = NULL_TREE;
4042 /* Assume all the expressions will be constant. */
4043 if (non_constant_p)
4044 *non_constant_p = false;
4046 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4047 return error_mark_node;
4049 /* Consume expressions until there are no more. */
4050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4051 while (true)
4053 tree expr;
4055 /* At the beginning of attribute lists, check to see if the
4056 next token is an identifier. */
4057 if (is_attribute_list
4058 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4060 cp_token *token;
4062 /* Consume the identifier. */
4063 token = cp_lexer_consume_token (parser->lexer);
4064 /* Save the identifier. */
4065 identifier = token->value;
4067 else
4069 /* Parse the next assignment-expression. */
4070 if (non_constant_p)
4072 bool expr_non_constant_p;
4073 expr = (cp_parser_constant_expression
4074 (parser, /*allow_non_constant_p=*/true,
4075 &expr_non_constant_p));
4076 if (expr_non_constant_p)
4077 *non_constant_p = true;
4079 else
4080 expr = cp_parser_assignment_expression (parser);
4082 /* Add it to the list. We add error_mark_node
4083 expressions to the list, so that we can still tell if
4084 the correct form for a parenthesized expression-list
4085 is found. That gives better errors. */
4086 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4088 if (expr == error_mark_node)
4089 goto skip_comma;
4092 /* After the first item, attribute lists look the same as
4093 expression lists. */
4094 is_attribute_list = false;
4096 get_comma:;
4097 /* If the next token isn't a `,', then we are done. */
4098 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4099 break;
4101 /* Otherwise, consume the `,' and keep going. */
4102 cp_lexer_consume_token (parser->lexer);
4105 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4107 int ending;
4109 skip_comma:;
4110 /* We try and resync to an unnested comma, as that will give the
4111 user better diagnostics. */
4112 ending = cp_parser_skip_to_closing_parenthesis (parser,
4113 /*recovering=*/true,
4114 /*or_comma=*/true,
4115 /*consume_paren=*/true);
4116 if (ending < 0)
4117 goto get_comma;
4118 if (!ending)
4119 return error_mark_node;
4122 /* We built up the list in reverse order so we must reverse it now. */
4123 expression_list = nreverse (expression_list);
4124 if (identifier)
4125 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4127 return expression_list;
4130 /* Parse a pseudo-destructor-name.
4132 pseudo-destructor-name:
4133 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4134 :: [opt] nested-name-specifier template template-id :: ~ type-name
4135 :: [opt] nested-name-specifier [opt] ~ type-name
4137 If either of the first two productions is used, sets *SCOPE to the
4138 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4139 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4140 or ERROR_MARK_NODE if the parse fails. */
4142 static void
4143 cp_parser_pseudo_destructor_name (cp_parser* parser,
4144 tree* scope,
4145 tree* type)
4147 bool nested_name_specifier_p;
4149 /* Look for the optional `::' operator. */
4150 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4151 /* Look for the optional nested-name-specifier. */
4152 nested_name_specifier_p
4153 = (cp_parser_nested_name_specifier_opt (parser,
4154 /*typename_keyword_p=*/false,
4155 /*check_dependency_p=*/true,
4156 /*type_p=*/false,
4157 /*is_declaration=*/true)
4158 != NULL_TREE);
4159 /* Now, if we saw a nested-name-specifier, we might be doing the
4160 second production. */
4161 if (nested_name_specifier_p
4162 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4164 /* Consume the `template' keyword. */
4165 cp_lexer_consume_token (parser->lexer);
4166 /* Parse the template-id. */
4167 cp_parser_template_id (parser,
4168 /*template_keyword_p=*/true,
4169 /*check_dependency_p=*/false,
4170 /*is_declaration=*/true);
4171 /* Look for the `::' token. */
4172 cp_parser_require (parser, CPP_SCOPE, "`::'");
4174 /* If the next token is not a `~', then there might be some
4175 additional qualification. */
4176 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4178 /* Look for the type-name. */
4179 *scope = TREE_TYPE (cp_parser_type_name (parser));
4181 /* If we didn't get an aggregate type, or we don't have ::~,
4182 then something has gone wrong. Since the only caller of this
4183 function is looking for something after `.' or `->' after a
4184 scalar type, most likely the program is trying to get a
4185 member of a non-aggregate type. */
4186 if (*scope == error_mark_node
4187 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4188 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4190 cp_parser_error (parser, "request for member of non-aggregate type");
4191 *type = error_mark_node;
4192 return;
4195 /* Look for the `::' token. */
4196 cp_parser_require (parser, CPP_SCOPE, "`::'");
4198 else
4199 *scope = NULL_TREE;
4201 /* Look for the `~'. */
4202 cp_parser_require (parser, CPP_COMPL, "`~'");
4203 /* Look for the type-name again. We are not responsible for
4204 checking that it matches the first type-name. */
4205 *type = cp_parser_type_name (parser);
4208 /* Parse a unary-expression.
4210 unary-expression:
4211 postfix-expression
4212 ++ cast-expression
4213 -- cast-expression
4214 unary-operator cast-expression
4215 sizeof unary-expression
4216 sizeof ( type-id )
4217 new-expression
4218 delete-expression
4220 GNU Extensions:
4222 unary-expression:
4223 __extension__ cast-expression
4224 __alignof__ unary-expression
4225 __alignof__ ( type-id )
4226 __real__ cast-expression
4227 __imag__ cast-expression
4228 && identifier
4230 ADDRESS_P is true iff the unary-expression is appearing as the
4231 operand of the `&' operator.
4233 Returns a representation of the expression. */
4235 static tree
4236 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4238 cp_token *token;
4239 enum tree_code unary_operator;
4241 /* Peek at the next token. */
4242 token = cp_lexer_peek_token (parser->lexer);
4243 /* Some keywords give away the kind of expression. */
4244 if (token->type == CPP_KEYWORD)
4246 enum rid keyword = token->keyword;
4248 switch (keyword)
4250 case RID_ALIGNOF:
4251 case RID_SIZEOF:
4253 tree operand;
4254 enum tree_code op;
4256 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4257 /* Consume the token. */
4258 cp_lexer_consume_token (parser->lexer);
4259 /* Parse the operand. */
4260 operand = cp_parser_sizeof_operand (parser, keyword);
4262 if (TYPE_P (operand))
4263 return cxx_sizeof_or_alignof_type (operand, op, true);
4264 else
4265 return cxx_sizeof_or_alignof_expr (operand, op);
4268 case RID_NEW:
4269 return cp_parser_new_expression (parser);
4271 case RID_DELETE:
4272 return cp_parser_delete_expression (parser);
4274 case RID_EXTENSION:
4276 /* The saved value of the PEDANTIC flag. */
4277 int saved_pedantic;
4278 tree expr;
4280 /* Save away the PEDANTIC flag. */
4281 cp_parser_extension_opt (parser, &saved_pedantic);
4282 /* Parse the cast-expression. */
4283 expr = cp_parser_simple_cast_expression (parser);
4284 /* Restore the PEDANTIC flag. */
4285 pedantic = saved_pedantic;
4287 return expr;
4290 case RID_REALPART:
4291 case RID_IMAGPART:
4293 tree expression;
4295 /* Consume the `__real__' or `__imag__' token. */
4296 cp_lexer_consume_token (parser->lexer);
4297 /* Parse the cast-expression. */
4298 expression = cp_parser_simple_cast_expression (parser);
4299 /* Create the complete representation. */
4300 return build_x_unary_op ((keyword == RID_REALPART
4301 ? REALPART_EXPR : IMAGPART_EXPR),
4302 expression);
4304 break;
4306 default:
4307 break;
4311 /* Look for the `:: new' and `:: delete', which also signal the
4312 beginning of a new-expression, or delete-expression,
4313 respectively. If the next token is `::', then it might be one of
4314 these. */
4315 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4317 enum rid keyword;
4319 /* See if the token after the `::' is one of the keywords in
4320 which we're interested. */
4321 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4322 /* If it's `new', we have a new-expression. */
4323 if (keyword == RID_NEW)
4324 return cp_parser_new_expression (parser);
4325 /* Similarly, for `delete'. */
4326 else if (keyword == RID_DELETE)
4327 return cp_parser_delete_expression (parser);
4330 /* Look for a unary operator. */
4331 unary_operator = cp_parser_unary_operator (token);
4332 /* The `++' and `--' operators can be handled similarly, even though
4333 they are not technically unary-operators in the grammar. */
4334 if (unary_operator == ERROR_MARK)
4336 if (token->type == CPP_PLUS_PLUS)
4337 unary_operator = PREINCREMENT_EXPR;
4338 else if (token->type == CPP_MINUS_MINUS)
4339 unary_operator = PREDECREMENT_EXPR;
4340 /* Handle the GNU address-of-label extension. */
4341 else if (cp_parser_allow_gnu_extensions_p (parser)
4342 && token->type == CPP_AND_AND)
4344 tree identifier;
4346 /* Consume the '&&' token. */
4347 cp_lexer_consume_token (parser->lexer);
4348 /* Look for the identifier. */
4349 identifier = cp_parser_identifier (parser);
4350 /* Create an expression representing the address. */
4351 return finish_label_address_expr (identifier);
4354 if (unary_operator != ERROR_MARK)
4356 tree cast_expression;
4357 tree expression = error_mark_node;
4358 const char *non_constant_p = NULL;
4360 /* Consume the operator token. */
4361 token = cp_lexer_consume_token (parser->lexer);
4362 /* Parse the cast-expression. */
4363 cast_expression
4364 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4365 /* Now, build an appropriate representation. */
4366 switch (unary_operator)
4368 case INDIRECT_REF:
4369 non_constant_p = "`*'";
4370 expression = build_x_indirect_ref (cast_expression, "unary *");
4371 break;
4373 case ADDR_EXPR:
4374 /* The "&" operator is allowed in the implementation of
4375 "offsetof". */
4376 if (!parser->in_offsetof_p)
4377 non_constant_p = "`&'";
4378 /* Fall through. */
4379 case BIT_NOT_EXPR:
4380 expression = build_x_unary_op (unary_operator, cast_expression);
4381 break;
4383 case PREINCREMENT_EXPR:
4384 case PREDECREMENT_EXPR:
4385 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4386 ? "`++'" : "`--'");
4387 /* Fall through. */
4388 case CONVERT_EXPR:
4389 case NEGATE_EXPR:
4390 case TRUTH_NOT_EXPR:
4391 expression = finish_unary_op_expr (unary_operator, cast_expression);
4392 break;
4394 default:
4395 abort ();
4398 if (non_constant_p
4399 && cp_parser_non_integral_constant_expression (parser,
4400 non_constant_p))
4401 expression = error_mark_node;
4403 return expression;
4406 return cp_parser_postfix_expression (parser, address_p);
4409 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4410 unary-operator, the corresponding tree code is returned. */
4412 static enum tree_code
4413 cp_parser_unary_operator (cp_token* token)
4415 switch (token->type)
4417 case CPP_MULT:
4418 return INDIRECT_REF;
4420 case CPP_AND:
4421 return ADDR_EXPR;
4423 case CPP_PLUS:
4424 return CONVERT_EXPR;
4426 case CPP_MINUS:
4427 return NEGATE_EXPR;
4429 case CPP_NOT:
4430 return TRUTH_NOT_EXPR;
4432 case CPP_COMPL:
4433 return BIT_NOT_EXPR;
4435 default:
4436 return ERROR_MARK;
4440 /* Parse a new-expression.
4442 new-expression:
4443 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4444 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4446 Returns a representation of the expression. */
4448 static tree
4449 cp_parser_new_expression (cp_parser* parser)
4451 bool global_scope_p;
4452 tree placement;
4453 tree type;
4454 tree initializer;
4456 /* Look for the optional `::' operator. */
4457 global_scope_p
4458 = (cp_parser_global_scope_opt (parser,
4459 /*current_scope_valid_p=*/false)
4460 != NULL_TREE);
4461 /* Look for the `new' operator. */
4462 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4463 /* There's no easy way to tell a new-placement from the
4464 `( type-id )' construct. */
4465 cp_parser_parse_tentatively (parser);
4466 /* Look for a new-placement. */
4467 placement = cp_parser_new_placement (parser);
4468 /* If that didn't work out, there's no new-placement. */
4469 if (!cp_parser_parse_definitely (parser))
4470 placement = NULL_TREE;
4472 /* If the next token is a `(', then we have a parenthesized
4473 type-id. */
4474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4476 /* Consume the `('. */
4477 cp_lexer_consume_token (parser->lexer);
4478 /* Parse the type-id. */
4479 type = cp_parser_type_id (parser);
4480 /* Look for the closing `)'. */
4481 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4482 /* There should not be a direct-new-declarator in this production,
4483 but GCC used to allowed this, so we check and emit a sensible error
4484 message for this case. */
4485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4487 error ("array bound forbidden after parenthesized type-id");
4488 inform ("try removing the parentheses around the type-id");
4489 cp_parser_direct_new_declarator (parser);
4492 /* Otherwise, there must be a new-type-id. */
4493 else
4494 type = cp_parser_new_type_id (parser);
4496 /* If the next token is a `(', then we have a new-initializer. */
4497 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4498 initializer = cp_parser_new_initializer (parser);
4499 else
4500 initializer = NULL_TREE;
4502 /* A new-expression may not appear in an integral constant
4503 expression. */
4504 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4505 return error_mark_node;
4507 /* Create a representation of the new-expression. */
4508 return build_new (placement, type, initializer, global_scope_p);
4511 /* Parse a new-placement.
4513 new-placement:
4514 ( expression-list )
4516 Returns the same representation as for an expression-list. */
4518 static tree
4519 cp_parser_new_placement (cp_parser* parser)
4521 tree expression_list;
4523 /* Parse the expression-list. */
4524 expression_list = (cp_parser_parenthesized_expression_list
4525 (parser, false, /*non_constant_p=*/NULL));
4527 return expression_list;
4530 /* Parse a new-type-id.
4532 new-type-id:
4533 type-specifier-seq new-declarator [opt]
4535 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4536 and whose TREE_VALUE is the new-declarator. */
4538 static tree
4539 cp_parser_new_type_id (cp_parser* parser)
4541 tree type_specifier_seq;
4542 tree declarator;
4543 const char *saved_message;
4545 /* The type-specifier sequence must not contain type definitions.
4546 (It cannot contain declarations of new types either, but if they
4547 are not definitions we will catch that because they are not
4548 complete.) */
4549 saved_message = parser->type_definition_forbidden_message;
4550 parser->type_definition_forbidden_message
4551 = "types may not be defined in a new-type-id";
4552 /* Parse the type-specifier-seq. */
4553 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4554 /* Restore the old message. */
4555 parser->type_definition_forbidden_message = saved_message;
4556 /* Parse the new-declarator. */
4557 declarator = cp_parser_new_declarator_opt (parser);
4559 return build_tree_list (type_specifier_seq, declarator);
4562 /* Parse an (optional) new-declarator.
4564 new-declarator:
4565 ptr-operator new-declarator [opt]
4566 direct-new-declarator
4568 Returns a representation of the declarator. See
4569 cp_parser_declarator for the representations used. */
4571 static tree
4572 cp_parser_new_declarator_opt (cp_parser* parser)
4574 enum tree_code code;
4575 tree type;
4576 tree cv_qualifier_seq;
4578 /* We don't know if there's a ptr-operator next, or not. */
4579 cp_parser_parse_tentatively (parser);
4580 /* Look for a ptr-operator. */
4581 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4582 /* If that worked, look for more new-declarators. */
4583 if (cp_parser_parse_definitely (parser))
4585 tree declarator;
4587 /* Parse another optional declarator. */
4588 declarator = cp_parser_new_declarator_opt (parser);
4590 /* Create the representation of the declarator. */
4591 if (code == INDIRECT_REF)
4592 declarator = make_pointer_declarator (cv_qualifier_seq,
4593 declarator);
4594 else
4595 declarator = make_reference_declarator (cv_qualifier_seq,
4596 declarator);
4598 /* Handle the pointer-to-member case. */
4599 if (type)
4600 declarator = build_nt (SCOPE_REF, type, declarator);
4602 return declarator;
4605 /* If the next token is a `[', there is a direct-new-declarator. */
4606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4607 return cp_parser_direct_new_declarator (parser);
4609 return NULL_TREE;
4612 /* Parse a direct-new-declarator.
4614 direct-new-declarator:
4615 [ expression ]
4616 direct-new-declarator [constant-expression]
4618 Returns an ARRAY_REF, following the same conventions as are
4619 documented for cp_parser_direct_declarator. */
4621 static tree
4622 cp_parser_direct_new_declarator (cp_parser* parser)
4624 tree declarator = NULL_TREE;
4626 while (true)
4628 tree expression;
4630 /* Look for the opening `['. */
4631 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4632 /* The first expression is not required to be constant. */
4633 if (!declarator)
4635 expression = cp_parser_expression (parser);
4636 /* The standard requires that the expression have integral
4637 type. DR 74 adds enumeration types. We believe that the
4638 real intent is that these expressions be handled like the
4639 expression in a `switch' condition, which also allows
4640 classes with a single conversion to integral or
4641 enumeration type. */
4642 if (!processing_template_decl)
4644 expression
4645 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4646 expression,
4647 /*complain=*/true);
4648 if (!expression)
4650 error ("expression in new-declarator must have integral or enumeration type");
4651 expression = error_mark_node;
4655 /* But all the other expressions must be. */
4656 else
4657 expression
4658 = cp_parser_constant_expression (parser,
4659 /*allow_non_constant=*/false,
4660 NULL);
4661 /* Look for the closing `]'. */
4662 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4664 /* Add this bound to the declarator. */
4665 declarator = build_nt (ARRAY_REF, declarator, expression);
4667 /* If the next token is not a `[', then there are no more
4668 bounds. */
4669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4670 break;
4673 return declarator;
4676 /* Parse a new-initializer.
4678 new-initializer:
4679 ( expression-list [opt] )
4681 Returns a representation of the expression-list. If there is no
4682 expression-list, VOID_ZERO_NODE is returned. */
4684 static tree
4685 cp_parser_new_initializer (cp_parser* parser)
4687 tree expression_list;
4689 expression_list = (cp_parser_parenthesized_expression_list
4690 (parser, false, /*non_constant_p=*/NULL));
4691 if (!expression_list)
4692 expression_list = void_zero_node;
4694 return expression_list;
4697 /* Parse a delete-expression.
4699 delete-expression:
4700 :: [opt] delete cast-expression
4701 :: [opt] delete [ ] cast-expression
4703 Returns a representation of the expression. */
4705 static tree
4706 cp_parser_delete_expression (cp_parser* parser)
4708 bool global_scope_p;
4709 bool array_p;
4710 tree expression;
4712 /* Look for the optional `::' operator. */
4713 global_scope_p
4714 = (cp_parser_global_scope_opt (parser,
4715 /*current_scope_valid_p=*/false)
4716 != NULL_TREE);
4717 /* Look for the `delete' keyword. */
4718 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4719 /* See if the array syntax is in use. */
4720 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4722 /* Consume the `[' token. */
4723 cp_lexer_consume_token (parser->lexer);
4724 /* Look for the `]' token. */
4725 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4726 /* Remember that this is the `[]' construct. */
4727 array_p = true;
4729 else
4730 array_p = false;
4732 /* Parse the cast-expression. */
4733 expression = cp_parser_simple_cast_expression (parser);
4735 /* A delete-expression may not appear in an integral constant
4736 expression. */
4737 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4738 return error_mark_node;
4740 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4743 /* Parse a cast-expression.
4745 cast-expression:
4746 unary-expression
4747 ( type-id ) cast-expression
4749 Returns a representation of the expression. */
4751 static tree
4752 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4754 /* If it's a `(', then we might be looking at a cast. */
4755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4757 tree type = NULL_TREE;
4758 tree expr = NULL_TREE;
4759 bool compound_literal_p;
4760 const char *saved_message;
4762 /* There's no way to know yet whether or not this is a cast.
4763 For example, `(int (3))' is a unary-expression, while `(int)
4764 3' is a cast. So, we resort to parsing tentatively. */
4765 cp_parser_parse_tentatively (parser);
4766 /* Types may not be defined in a cast. */
4767 saved_message = parser->type_definition_forbidden_message;
4768 parser->type_definition_forbidden_message
4769 = "types may not be defined in casts";
4770 /* Consume the `('. */
4771 cp_lexer_consume_token (parser->lexer);
4772 /* A very tricky bit is that `(struct S) { 3 }' is a
4773 compound-literal (which we permit in C++ as an extension).
4774 But, that construct is not a cast-expression -- it is a
4775 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4776 is legal; if the compound-literal were a cast-expression,
4777 you'd need an extra set of parentheses.) But, if we parse
4778 the type-id, and it happens to be a class-specifier, then we
4779 will commit to the parse at that point, because we cannot
4780 undo the action that is done when creating a new class. So,
4781 then we cannot back up and do a postfix-expression.
4783 Therefore, we scan ahead to the closing `)', and check to see
4784 if the token after the `)' is a `{'. If so, we are not
4785 looking at a cast-expression.
4787 Save tokens so that we can put them back. */
4788 cp_lexer_save_tokens (parser->lexer);
4789 /* Skip tokens until the next token is a closing parenthesis.
4790 If we find the closing `)', and the next token is a `{', then
4791 we are looking at a compound-literal. */
4792 compound_literal_p
4793 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4794 /*consume_paren=*/true)
4795 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4796 /* Roll back the tokens we skipped. */
4797 cp_lexer_rollback_tokens (parser->lexer);
4798 /* If we were looking at a compound-literal, simulate an error
4799 so that the call to cp_parser_parse_definitely below will
4800 fail. */
4801 if (compound_literal_p)
4802 cp_parser_simulate_error (parser);
4803 else
4805 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4806 parser->in_type_id_in_expr_p = true;
4807 /* Look for the type-id. */
4808 type = cp_parser_type_id (parser);
4809 /* Look for the closing `)'. */
4810 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4811 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4814 /* Restore the saved message. */
4815 parser->type_definition_forbidden_message = saved_message;
4817 /* If ok so far, parse the dependent expression. We cannot be
4818 sure it is a cast. Consider `(T ())'. It is a parenthesized
4819 ctor of T, but looks like a cast to function returning T
4820 without a dependent expression. */
4821 if (!cp_parser_error_occurred (parser))
4822 expr = cp_parser_simple_cast_expression (parser);
4824 if (cp_parser_parse_definitely (parser))
4826 /* Warn about old-style casts, if so requested. */
4827 if (warn_old_style_cast
4828 && !in_system_header
4829 && !VOID_TYPE_P (type)
4830 && current_lang_name != lang_name_c)
4831 warning ("use of old-style cast");
4833 /* Only type conversions to integral or enumeration types
4834 can be used in constant-expressions. */
4835 if (parser->integral_constant_expression_p
4836 && !dependent_type_p (type)
4837 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4838 && (cp_parser_non_integral_constant_expression
4839 (parser,
4840 "a casts to a type other than an integral or "
4841 "enumeration type")))
4842 return error_mark_node;
4844 /* Perform the cast. */
4845 expr = build_c_cast (type, expr);
4846 return expr;
4850 /* If we get here, then it's not a cast, so it must be a
4851 unary-expression. */
4852 return cp_parser_unary_expression (parser, address_p);
4855 /* Parse a pm-expression.
4857 pm-expression:
4858 cast-expression
4859 pm-expression .* cast-expression
4860 pm-expression ->* cast-expression
4862 Returns a representation of the expression. */
4864 static tree
4865 cp_parser_pm_expression (cp_parser* parser)
4867 static const cp_parser_token_tree_map map = {
4868 { CPP_DEREF_STAR, MEMBER_REF },
4869 { CPP_DOT_STAR, DOTSTAR_EXPR },
4870 { CPP_EOF, ERROR_MARK }
4873 return cp_parser_binary_expression (parser, map,
4874 cp_parser_simple_cast_expression);
4877 /* Parse a multiplicative-expression.
4879 mulitplicative-expression:
4880 pm-expression
4881 multiplicative-expression * pm-expression
4882 multiplicative-expression / pm-expression
4883 multiplicative-expression % pm-expression
4885 Returns a representation of the expression. */
4887 static tree
4888 cp_parser_multiplicative_expression (cp_parser* parser)
4890 static const cp_parser_token_tree_map map = {
4891 { CPP_MULT, MULT_EXPR },
4892 { CPP_DIV, TRUNC_DIV_EXPR },
4893 { CPP_MOD, TRUNC_MOD_EXPR },
4894 { CPP_EOF, ERROR_MARK }
4897 return cp_parser_binary_expression (parser,
4898 map,
4899 cp_parser_pm_expression);
4902 /* Parse an additive-expression.
4904 additive-expression:
4905 multiplicative-expression
4906 additive-expression + multiplicative-expression
4907 additive-expression - multiplicative-expression
4909 Returns a representation of the expression. */
4911 static tree
4912 cp_parser_additive_expression (cp_parser* parser)
4914 static const cp_parser_token_tree_map map = {
4915 { CPP_PLUS, PLUS_EXPR },
4916 { CPP_MINUS, MINUS_EXPR },
4917 { CPP_EOF, ERROR_MARK }
4920 return cp_parser_binary_expression (parser,
4921 map,
4922 cp_parser_multiplicative_expression);
4925 /* Parse a shift-expression.
4927 shift-expression:
4928 additive-expression
4929 shift-expression << additive-expression
4930 shift-expression >> additive-expression
4932 Returns a representation of the expression. */
4934 static tree
4935 cp_parser_shift_expression (cp_parser* parser)
4937 static const cp_parser_token_tree_map map = {
4938 { CPP_LSHIFT, LSHIFT_EXPR },
4939 { CPP_RSHIFT, RSHIFT_EXPR },
4940 { CPP_EOF, ERROR_MARK }
4943 return cp_parser_binary_expression (parser,
4944 map,
4945 cp_parser_additive_expression);
4948 /* Parse a relational-expression.
4950 relational-expression:
4951 shift-expression
4952 relational-expression < shift-expression
4953 relational-expression > shift-expression
4954 relational-expression <= shift-expression
4955 relational-expression >= shift-expression
4957 GNU Extension:
4959 relational-expression:
4960 relational-expression <? shift-expression
4961 relational-expression >? shift-expression
4963 Returns a representation of the expression. */
4965 static tree
4966 cp_parser_relational_expression (cp_parser* parser)
4968 static const cp_parser_token_tree_map map = {
4969 { CPP_LESS, LT_EXPR },
4970 { CPP_GREATER, GT_EXPR },
4971 { CPP_LESS_EQ, LE_EXPR },
4972 { CPP_GREATER_EQ, GE_EXPR },
4973 { CPP_MIN, MIN_EXPR },
4974 { CPP_MAX, MAX_EXPR },
4975 { CPP_EOF, ERROR_MARK }
4978 return cp_parser_binary_expression (parser,
4979 map,
4980 cp_parser_shift_expression);
4983 /* Parse an equality-expression.
4985 equality-expression:
4986 relational-expression
4987 equality-expression == relational-expression
4988 equality-expression != relational-expression
4990 Returns a representation of the expression. */
4992 static tree
4993 cp_parser_equality_expression (cp_parser* parser)
4995 static const cp_parser_token_tree_map map = {
4996 { CPP_EQ_EQ, EQ_EXPR },
4997 { CPP_NOT_EQ, NE_EXPR },
4998 { CPP_EOF, ERROR_MARK }
5001 return cp_parser_binary_expression (parser,
5002 map,
5003 cp_parser_relational_expression);
5006 /* Parse an and-expression.
5008 and-expression:
5009 equality-expression
5010 and-expression & equality-expression
5012 Returns a representation of the expression. */
5014 static tree
5015 cp_parser_and_expression (cp_parser* parser)
5017 static const cp_parser_token_tree_map map = {
5018 { CPP_AND, BIT_AND_EXPR },
5019 { CPP_EOF, ERROR_MARK }
5022 return cp_parser_binary_expression (parser,
5023 map,
5024 cp_parser_equality_expression);
5027 /* Parse an exclusive-or-expression.
5029 exclusive-or-expression:
5030 and-expression
5031 exclusive-or-expression ^ and-expression
5033 Returns a representation of the expression. */
5035 static tree
5036 cp_parser_exclusive_or_expression (cp_parser* parser)
5038 static const cp_parser_token_tree_map map = {
5039 { CPP_XOR, BIT_XOR_EXPR },
5040 { CPP_EOF, ERROR_MARK }
5043 return cp_parser_binary_expression (parser,
5044 map,
5045 cp_parser_and_expression);
5049 /* Parse an inclusive-or-expression.
5051 inclusive-or-expression:
5052 exclusive-or-expression
5053 inclusive-or-expression | exclusive-or-expression
5055 Returns a representation of the expression. */
5057 static tree
5058 cp_parser_inclusive_or_expression (cp_parser* parser)
5060 static const cp_parser_token_tree_map map = {
5061 { CPP_OR, BIT_IOR_EXPR },
5062 { CPP_EOF, ERROR_MARK }
5065 return cp_parser_binary_expression (parser,
5066 map,
5067 cp_parser_exclusive_or_expression);
5070 /* Parse a logical-and-expression.
5072 logical-and-expression:
5073 inclusive-or-expression
5074 logical-and-expression && inclusive-or-expression
5076 Returns a representation of the expression. */
5078 static tree
5079 cp_parser_logical_and_expression (cp_parser* parser)
5081 static const cp_parser_token_tree_map map = {
5082 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5083 { CPP_EOF, ERROR_MARK }
5086 return cp_parser_binary_expression (parser,
5087 map,
5088 cp_parser_inclusive_or_expression);
5091 /* Parse a logical-or-expression.
5093 logical-or-expression:
5094 logical-and-expression
5095 logical-or-expression || logical-and-expression
5097 Returns a representation of the expression. */
5099 static tree
5100 cp_parser_logical_or_expression (cp_parser* parser)
5102 static const cp_parser_token_tree_map map = {
5103 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5104 { CPP_EOF, ERROR_MARK }
5107 return cp_parser_binary_expression (parser,
5108 map,
5109 cp_parser_logical_and_expression);
5112 /* Parse the `? expression : assignment-expression' part of a
5113 conditional-expression. The LOGICAL_OR_EXPR is the
5114 logical-or-expression that started the conditional-expression.
5115 Returns a representation of the entire conditional-expression.
5117 This routine is used by cp_parser_assignment_expression.
5119 ? expression : assignment-expression
5121 GNU Extensions:
5123 ? : assignment-expression */
5125 static tree
5126 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5128 tree expr;
5129 tree assignment_expr;
5131 /* Consume the `?' token. */
5132 cp_lexer_consume_token (parser->lexer);
5133 if (cp_parser_allow_gnu_extensions_p (parser)
5134 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5135 /* Implicit true clause. */
5136 expr = NULL_TREE;
5137 else
5138 /* Parse the expression. */
5139 expr = cp_parser_expression (parser);
5141 /* The next token should be a `:'. */
5142 cp_parser_require (parser, CPP_COLON, "`:'");
5143 /* Parse the assignment-expression. */
5144 assignment_expr = cp_parser_assignment_expression (parser);
5146 /* Build the conditional-expression. */
5147 return build_x_conditional_expr (logical_or_expr,
5148 expr,
5149 assignment_expr);
5152 /* Parse an assignment-expression.
5154 assignment-expression:
5155 conditional-expression
5156 logical-or-expression assignment-operator assignment_expression
5157 throw-expression
5159 Returns a representation for the expression. */
5161 static tree
5162 cp_parser_assignment_expression (cp_parser* parser)
5164 tree expr;
5166 /* If the next token is the `throw' keyword, then we're looking at
5167 a throw-expression. */
5168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5169 expr = cp_parser_throw_expression (parser);
5170 /* Otherwise, it must be that we are looking at a
5171 logical-or-expression. */
5172 else
5174 /* Parse the logical-or-expression. */
5175 expr = cp_parser_logical_or_expression (parser);
5176 /* If the next token is a `?' then we're actually looking at a
5177 conditional-expression. */
5178 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5179 return cp_parser_question_colon_clause (parser, expr);
5180 else
5182 enum tree_code assignment_operator;
5184 /* If it's an assignment-operator, we're using the second
5185 production. */
5186 assignment_operator
5187 = cp_parser_assignment_operator_opt (parser);
5188 if (assignment_operator != ERROR_MARK)
5190 tree rhs;
5192 /* Parse the right-hand side of the assignment. */
5193 rhs = cp_parser_assignment_expression (parser);
5194 /* An assignment may not appear in a
5195 constant-expression. */
5196 if (cp_parser_non_integral_constant_expression (parser,
5197 "an assignment"))
5198 return error_mark_node;
5199 /* Build the assignment expression. */
5200 expr = build_x_modify_expr (expr,
5201 assignment_operator,
5202 rhs);
5207 return expr;
5210 /* Parse an (optional) assignment-operator.
5212 assignment-operator: one of
5213 = *= /= %= += -= >>= <<= &= ^= |=
5215 GNU Extension:
5217 assignment-operator: one of
5218 <?= >?=
5220 If the next token is an assignment operator, the corresponding tree
5221 code is returned, and the token is consumed. For example, for
5222 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5223 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5224 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5225 operator, ERROR_MARK is returned. */
5227 static enum tree_code
5228 cp_parser_assignment_operator_opt (cp_parser* parser)
5230 enum tree_code op;
5231 cp_token *token;
5233 /* Peek at the next toen. */
5234 token = cp_lexer_peek_token (parser->lexer);
5236 switch (token->type)
5238 case CPP_EQ:
5239 op = NOP_EXPR;
5240 break;
5242 case CPP_MULT_EQ:
5243 op = MULT_EXPR;
5244 break;
5246 case CPP_DIV_EQ:
5247 op = TRUNC_DIV_EXPR;
5248 break;
5250 case CPP_MOD_EQ:
5251 op = TRUNC_MOD_EXPR;
5252 break;
5254 case CPP_PLUS_EQ:
5255 op = PLUS_EXPR;
5256 break;
5258 case CPP_MINUS_EQ:
5259 op = MINUS_EXPR;
5260 break;
5262 case CPP_RSHIFT_EQ:
5263 op = RSHIFT_EXPR;
5264 break;
5266 case CPP_LSHIFT_EQ:
5267 op = LSHIFT_EXPR;
5268 break;
5270 case CPP_AND_EQ:
5271 op = BIT_AND_EXPR;
5272 break;
5274 case CPP_XOR_EQ:
5275 op = BIT_XOR_EXPR;
5276 break;
5278 case CPP_OR_EQ:
5279 op = BIT_IOR_EXPR;
5280 break;
5282 case CPP_MIN_EQ:
5283 op = MIN_EXPR;
5284 break;
5286 case CPP_MAX_EQ:
5287 op = MAX_EXPR;
5288 break;
5290 default:
5291 /* Nothing else is an assignment operator. */
5292 op = ERROR_MARK;
5295 /* If it was an assignment operator, consume it. */
5296 if (op != ERROR_MARK)
5297 cp_lexer_consume_token (parser->lexer);
5299 return op;
5302 /* Parse an expression.
5304 expression:
5305 assignment-expression
5306 expression , assignment-expression
5308 Returns a representation of the expression. */
5310 static tree
5311 cp_parser_expression (cp_parser* parser)
5313 tree expression = NULL_TREE;
5315 while (true)
5317 tree assignment_expression;
5319 /* Parse the next assignment-expression. */
5320 assignment_expression
5321 = cp_parser_assignment_expression (parser);
5322 /* If this is the first assignment-expression, we can just
5323 save it away. */
5324 if (!expression)
5325 expression = assignment_expression;
5326 else
5327 expression = build_x_compound_expr (expression,
5328 assignment_expression);
5329 /* If the next token is not a comma, then we are done with the
5330 expression. */
5331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5332 break;
5333 /* Consume the `,'. */
5334 cp_lexer_consume_token (parser->lexer);
5335 /* A comma operator cannot appear in a constant-expression. */
5336 if (cp_parser_non_integral_constant_expression (parser,
5337 "a comma operator"))
5338 expression = error_mark_node;
5341 return expression;
5344 /* Parse a constant-expression.
5346 constant-expression:
5347 conditional-expression
5349 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5350 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5351 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5352 is false, NON_CONSTANT_P should be NULL. */
5354 static tree
5355 cp_parser_constant_expression (cp_parser* parser,
5356 bool allow_non_constant_p,
5357 bool *non_constant_p)
5359 bool saved_integral_constant_expression_p;
5360 bool saved_allow_non_integral_constant_expression_p;
5361 bool saved_non_integral_constant_expression_p;
5362 tree expression;
5364 /* It might seem that we could simply parse the
5365 conditional-expression, and then check to see if it were
5366 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5367 one that the compiler can figure out is constant, possibly after
5368 doing some simplifications or optimizations. The standard has a
5369 precise definition of constant-expression, and we must honor
5370 that, even though it is somewhat more restrictive.
5372 For example:
5374 int i[(2, 3)];
5376 is not a legal declaration, because `(2, 3)' is not a
5377 constant-expression. The `,' operator is forbidden in a
5378 constant-expression. However, GCC's constant-folding machinery
5379 will fold this operation to an INTEGER_CST for `3'. */
5381 /* Save the old settings. */
5382 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5383 saved_allow_non_integral_constant_expression_p
5384 = parser->allow_non_integral_constant_expression_p;
5385 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5386 /* We are now parsing a constant-expression. */
5387 parser->integral_constant_expression_p = true;
5388 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5389 parser->non_integral_constant_expression_p = false;
5390 /* Although the grammar says "conditional-expression", we parse an
5391 "assignment-expression", which also permits "throw-expression"
5392 and the use of assignment operators. In the case that
5393 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5394 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5395 actually essential that we look for an assignment-expression.
5396 For example, cp_parser_initializer_clauses uses this function to
5397 determine whether a particular assignment-expression is in fact
5398 constant. */
5399 expression = cp_parser_assignment_expression (parser);
5400 /* Restore the old settings. */
5401 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5402 parser->allow_non_integral_constant_expression_p
5403 = saved_allow_non_integral_constant_expression_p;
5404 if (allow_non_constant_p)
5405 *non_constant_p = parser->non_integral_constant_expression_p;
5406 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5408 return expression;
5411 /* Statements [gram.stmt.stmt] */
5413 /* Parse a statement.
5415 statement:
5416 labeled-statement
5417 expression-statement
5418 compound-statement
5419 selection-statement
5420 iteration-statement
5421 jump-statement
5422 declaration-statement
5423 try-block */
5425 static void
5426 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5428 tree statement;
5429 cp_token *token;
5430 int statement_line_number;
5432 /* There is no statement yet. */
5433 statement = NULL_TREE;
5434 /* Peek at the next token. */
5435 token = cp_lexer_peek_token (parser->lexer);
5436 /* Remember the line number of the first token in the statement. */
5437 statement_line_number = token->location.line;
5438 /* If this is a keyword, then that will often determine what kind of
5439 statement we have. */
5440 if (token->type == CPP_KEYWORD)
5442 enum rid keyword = token->keyword;
5444 switch (keyword)
5446 case RID_CASE:
5447 case RID_DEFAULT:
5448 statement = cp_parser_labeled_statement (parser,
5449 in_statement_expr_p);
5450 break;
5452 case RID_IF:
5453 case RID_SWITCH:
5454 statement = cp_parser_selection_statement (parser);
5455 break;
5457 case RID_WHILE:
5458 case RID_DO:
5459 case RID_FOR:
5460 statement = cp_parser_iteration_statement (parser);
5461 break;
5463 case RID_BREAK:
5464 case RID_CONTINUE:
5465 case RID_RETURN:
5466 case RID_GOTO:
5467 statement = cp_parser_jump_statement (parser);
5468 break;
5470 case RID_TRY:
5471 statement = cp_parser_try_block (parser);
5472 break;
5474 default:
5475 /* It might be a keyword like `int' that can start a
5476 declaration-statement. */
5477 break;
5480 else if (token->type == CPP_NAME)
5482 /* If the next token is a `:', then we are looking at a
5483 labeled-statement. */
5484 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5485 if (token->type == CPP_COLON)
5486 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5488 /* Anything that starts with a `{' must be a compound-statement. */
5489 else if (token->type == CPP_OPEN_BRACE)
5490 statement = cp_parser_compound_statement (parser, false);
5492 /* Everything else must be a declaration-statement or an
5493 expression-statement. Try for the declaration-statement
5494 first, unless we are looking at a `;', in which case we know that
5495 we have an expression-statement. */
5496 if (!statement)
5498 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5500 cp_parser_parse_tentatively (parser);
5501 /* Try to parse the declaration-statement. */
5502 cp_parser_declaration_statement (parser);
5503 /* If that worked, we're done. */
5504 if (cp_parser_parse_definitely (parser))
5505 return;
5507 /* Look for an expression-statement instead. */
5508 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5511 /* Set the line number for the statement. */
5512 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5513 STMT_LINENO (statement) = statement_line_number;
5516 /* Parse a labeled-statement.
5518 labeled-statement:
5519 identifier : statement
5520 case constant-expression : statement
5521 default : statement
5523 GNU Extension:
5525 labeled-statement:
5526 case constant-expression ... constant-expression : statement
5528 Returns the new CASE_LABEL, for a `case' or `default' label. For
5529 an ordinary label, returns a LABEL_STMT. */
5531 static tree
5532 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5534 cp_token *token;
5535 tree statement = error_mark_node;
5537 /* The next token should be an identifier. */
5538 token = cp_lexer_peek_token (parser->lexer);
5539 if (token->type != CPP_NAME
5540 && token->type != CPP_KEYWORD)
5542 cp_parser_error (parser, "expected labeled-statement");
5543 return error_mark_node;
5546 switch (token->keyword)
5548 case RID_CASE:
5550 tree expr, expr_hi;
5551 cp_token *ellipsis;
5553 /* Consume the `case' token. */
5554 cp_lexer_consume_token (parser->lexer);
5555 /* Parse the constant-expression. */
5556 expr = cp_parser_constant_expression (parser,
5557 /*allow_non_constant_p=*/false,
5558 NULL);
5560 ellipsis = cp_lexer_peek_token (parser->lexer);
5561 if (ellipsis->type == CPP_ELLIPSIS)
5563 /* Consume the `...' token. */
5564 cp_lexer_consume_token (parser->lexer);
5565 expr_hi =
5566 cp_parser_constant_expression (parser,
5567 /*allow_non_constant_p=*/false,
5568 NULL);
5569 /* We don't need to emit warnings here, as the common code
5570 will do this for us. */
5572 else
5573 expr_hi = NULL_TREE;
5575 if (!parser->in_switch_statement_p)
5576 error ("case label `%E' not within a switch statement", expr);
5577 else
5578 statement = finish_case_label (expr, expr_hi);
5580 break;
5582 case RID_DEFAULT:
5583 /* Consume the `default' token. */
5584 cp_lexer_consume_token (parser->lexer);
5585 if (!parser->in_switch_statement_p)
5586 error ("case label not within a switch statement");
5587 else
5588 statement = finish_case_label (NULL_TREE, NULL_TREE);
5589 break;
5591 default:
5592 /* Anything else must be an ordinary label. */
5593 statement = finish_label_stmt (cp_parser_identifier (parser));
5594 break;
5597 /* Require the `:' token. */
5598 cp_parser_require (parser, CPP_COLON, "`:'");
5599 /* Parse the labeled statement. */
5600 cp_parser_statement (parser, in_statement_expr_p);
5602 /* Return the label, in the case of a `case' or `default' label. */
5603 return statement;
5606 /* Parse an expression-statement.
5608 expression-statement:
5609 expression [opt] ;
5611 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5612 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5613 indicates whether this expression-statement is part of an
5614 expression statement. */
5616 static tree
5617 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5619 tree statement = NULL_TREE;
5621 /* If the next token is a ';', then there is no expression
5622 statement. */
5623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5624 statement = cp_parser_expression (parser);
5626 /* Consume the final `;'. */
5627 cp_parser_consume_semicolon_at_end_of_statement (parser);
5629 if (in_statement_expr_p
5630 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5632 /* This is the final expression statement of a statement
5633 expression. */
5634 statement = finish_stmt_expr_expr (statement);
5636 else if (statement)
5637 statement = finish_expr_stmt (statement);
5638 else
5639 finish_stmt ();
5641 return statement;
5644 /* Parse a compound-statement.
5646 compound-statement:
5647 { statement-seq [opt] }
5649 Returns a COMPOUND_STMT representing the statement. */
5651 static tree
5652 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5654 tree compound_stmt;
5656 /* Consume the `{'. */
5657 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5658 return error_mark_node;
5659 /* Begin the compound-statement. */
5660 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5661 /* Parse an (optional) statement-seq. */
5662 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5663 /* Finish the compound-statement. */
5664 finish_compound_stmt (compound_stmt);
5665 /* Consume the `}'. */
5666 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5668 return compound_stmt;
5671 /* Parse an (optional) statement-seq.
5673 statement-seq:
5674 statement
5675 statement-seq [opt] statement */
5677 static void
5678 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5680 /* Scan statements until there aren't any more. */
5681 while (true)
5683 /* If we're looking at a `}', then we've run out of statements. */
5684 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5685 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5686 break;
5688 /* Parse the statement. */
5689 cp_parser_statement (parser, in_statement_expr_p);
5693 /* Parse a selection-statement.
5695 selection-statement:
5696 if ( condition ) statement
5697 if ( condition ) statement else statement
5698 switch ( condition ) statement
5700 Returns the new IF_STMT or SWITCH_STMT. */
5702 static tree
5703 cp_parser_selection_statement (cp_parser* parser)
5705 cp_token *token;
5706 enum rid keyword;
5708 /* Peek at the next token. */
5709 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5711 /* See what kind of keyword it is. */
5712 keyword = token->keyword;
5713 switch (keyword)
5715 case RID_IF:
5716 case RID_SWITCH:
5718 tree statement;
5719 tree condition;
5721 /* Look for the `('. */
5722 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5724 cp_parser_skip_to_end_of_statement (parser);
5725 return error_mark_node;
5728 /* Begin the selection-statement. */
5729 if (keyword == RID_IF)
5730 statement = begin_if_stmt ();
5731 else
5732 statement = begin_switch_stmt ();
5734 /* Parse the condition. */
5735 condition = cp_parser_condition (parser);
5736 /* Look for the `)'. */
5737 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5738 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5739 /*consume_paren=*/true);
5741 if (keyword == RID_IF)
5743 tree then_stmt;
5745 /* Add the condition. */
5746 finish_if_stmt_cond (condition, statement);
5748 /* Parse the then-clause. */
5749 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5750 finish_then_clause (statement);
5752 /* If the next token is `else', parse the else-clause. */
5753 if (cp_lexer_next_token_is_keyword (parser->lexer,
5754 RID_ELSE))
5756 tree else_stmt;
5758 /* Consume the `else' keyword. */
5759 cp_lexer_consume_token (parser->lexer);
5760 /* Parse the else-clause. */
5761 else_stmt
5762 = cp_parser_implicitly_scoped_statement (parser);
5763 finish_else_clause (statement);
5766 /* Now we're all done with the if-statement. */
5767 finish_if_stmt ();
5769 else
5771 tree body;
5772 bool in_switch_statement_p;
5774 /* Add the condition. */
5775 finish_switch_cond (condition, statement);
5777 /* Parse the body of the switch-statement. */
5778 in_switch_statement_p = parser->in_switch_statement_p;
5779 parser->in_switch_statement_p = true;
5780 body = cp_parser_implicitly_scoped_statement (parser);
5781 parser->in_switch_statement_p = in_switch_statement_p;
5783 /* Now we're all done with the switch-statement. */
5784 finish_switch_stmt (statement);
5787 return statement;
5789 break;
5791 default:
5792 cp_parser_error (parser, "expected selection-statement");
5793 return error_mark_node;
5797 /* Parse a condition.
5799 condition:
5800 expression
5801 type-specifier-seq declarator = assignment-expression
5803 GNU Extension:
5805 condition:
5806 type-specifier-seq declarator asm-specification [opt]
5807 attributes [opt] = assignment-expression
5809 Returns the expression that should be tested. */
5811 static tree
5812 cp_parser_condition (cp_parser* parser)
5814 tree type_specifiers;
5815 const char *saved_message;
5817 /* Try the declaration first. */
5818 cp_parser_parse_tentatively (parser);
5819 /* New types are not allowed in the type-specifier-seq for a
5820 condition. */
5821 saved_message = parser->type_definition_forbidden_message;
5822 parser->type_definition_forbidden_message
5823 = "types may not be defined in conditions";
5824 /* Parse the type-specifier-seq. */
5825 type_specifiers = cp_parser_type_specifier_seq (parser);
5826 /* Restore the saved message. */
5827 parser->type_definition_forbidden_message = saved_message;
5828 /* If all is well, we might be looking at a declaration. */
5829 if (!cp_parser_error_occurred (parser))
5831 tree decl;
5832 tree asm_specification;
5833 tree attributes;
5834 tree declarator;
5835 tree initializer = NULL_TREE;
5837 /* Parse the declarator. */
5838 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5839 /*ctor_dtor_or_conv_p=*/NULL,
5840 /*parenthesized_p=*/NULL,
5841 /*member_p=*/false);
5842 /* Parse the attributes. */
5843 attributes = cp_parser_attributes_opt (parser);
5844 /* Parse the asm-specification. */
5845 asm_specification = cp_parser_asm_specification_opt (parser);
5846 /* If the next token is not an `=', then we might still be
5847 looking at an expression. For example:
5849 if (A(a).x)
5851 looks like a decl-specifier-seq and a declarator -- but then
5852 there is no `=', so this is an expression. */
5853 cp_parser_require (parser, CPP_EQ, "`='");
5854 /* If we did see an `=', then we are looking at a declaration
5855 for sure. */
5856 if (cp_parser_parse_definitely (parser))
5858 bool pop_p;
5860 /* Create the declaration. */
5861 decl = start_decl (declarator, type_specifiers,
5862 /*initialized_p=*/true,
5863 attributes, /*prefix_attributes=*/NULL_TREE,
5864 &pop_p);
5865 /* Parse the assignment-expression. */
5866 initializer = cp_parser_assignment_expression (parser);
5868 /* Process the initializer. */
5869 cp_finish_decl (decl,
5870 initializer,
5871 asm_specification,
5872 LOOKUP_ONLYCONVERTING);
5873 if (pop_p)
5874 pop_scope (DECL_CONTEXT (decl));
5876 return convert_from_reference (decl);
5879 /* If we didn't even get past the declarator successfully, we are
5880 definitely not looking at a declaration. */
5881 else
5882 cp_parser_abort_tentative_parse (parser);
5884 /* Otherwise, we are looking at an expression. */
5885 return cp_parser_expression (parser);
5888 /* Parse an iteration-statement.
5890 iteration-statement:
5891 while ( condition ) statement
5892 do statement while ( expression ) ;
5893 for ( for-init-statement condition [opt] ; expression [opt] )
5894 statement
5896 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5898 static tree
5899 cp_parser_iteration_statement (cp_parser* parser)
5901 cp_token *token;
5902 enum rid keyword;
5903 tree statement;
5904 bool in_iteration_statement_p;
5907 /* Peek at the next token. */
5908 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5909 if (!token)
5910 return error_mark_node;
5912 /* Remember whether or not we are already within an iteration
5913 statement. */
5914 in_iteration_statement_p = parser->in_iteration_statement_p;
5916 /* See what kind of keyword it is. */
5917 keyword = token->keyword;
5918 switch (keyword)
5920 case RID_WHILE:
5922 tree condition;
5924 /* Begin the while-statement. */
5925 statement = begin_while_stmt ();
5926 /* Look for the `('. */
5927 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5928 /* Parse the condition. */
5929 condition = cp_parser_condition (parser);
5930 finish_while_stmt_cond (condition, statement);
5931 /* Look for the `)'. */
5932 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5933 /* Parse the dependent statement. */
5934 parser->in_iteration_statement_p = true;
5935 cp_parser_already_scoped_statement (parser);
5936 parser->in_iteration_statement_p = in_iteration_statement_p;
5937 /* We're done with the while-statement. */
5938 finish_while_stmt (statement);
5940 break;
5942 case RID_DO:
5944 tree expression;
5946 /* Begin the do-statement. */
5947 statement = begin_do_stmt ();
5948 /* Parse the body of the do-statement. */
5949 parser->in_iteration_statement_p = true;
5950 cp_parser_implicitly_scoped_statement (parser);
5951 parser->in_iteration_statement_p = in_iteration_statement_p;
5952 finish_do_body (statement);
5953 /* Look for the `while' keyword. */
5954 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5955 /* Look for the `('. */
5956 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5957 /* Parse the expression. */
5958 expression = cp_parser_expression (parser);
5959 /* We're done with the do-statement. */
5960 finish_do_stmt (expression, statement);
5961 /* Look for the `)'. */
5962 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5963 /* Look for the `;'. */
5964 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5966 break;
5968 case RID_FOR:
5970 tree condition = NULL_TREE;
5971 tree expression = NULL_TREE;
5973 /* Begin the for-statement. */
5974 statement = begin_for_stmt ();
5975 /* Look for the `('. */
5976 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5977 /* Parse the initialization. */
5978 cp_parser_for_init_statement (parser);
5979 finish_for_init_stmt (statement);
5981 /* If there's a condition, process it. */
5982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5983 condition = cp_parser_condition (parser);
5984 finish_for_cond (condition, statement);
5985 /* Look for the `;'. */
5986 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5988 /* If there's an expression, process it. */
5989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5990 expression = cp_parser_expression (parser);
5991 finish_for_expr (expression, statement);
5992 /* Look for the `)'. */
5993 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5995 /* Parse the body of the for-statement. */
5996 parser->in_iteration_statement_p = true;
5997 cp_parser_already_scoped_statement (parser);
5998 parser->in_iteration_statement_p = in_iteration_statement_p;
6000 /* We're done with the for-statement. */
6001 finish_for_stmt (statement);
6003 break;
6005 default:
6006 cp_parser_error (parser, "expected iteration-statement");
6007 statement = error_mark_node;
6008 break;
6011 return statement;
6014 /* Parse a for-init-statement.
6016 for-init-statement:
6017 expression-statement
6018 simple-declaration */
6020 static void
6021 cp_parser_for_init_statement (cp_parser* parser)
6023 /* If the next token is a `;', then we have an empty
6024 expression-statement. Grammatically, this is also a
6025 simple-declaration, but an invalid one, because it does not
6026 declare anything. Therefore, if we did not handle this case
6027 specially, we would issue an error message about an invalid
6028 declaration. */
6029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6031 /* We're going to speculatively look for a declaration, falling back
6032 to an expression, if necessary. */
6033 cp_parser_parse_tentatively (parser);
6034 /* Parse the declaration. */
6035 cp_parser_simple_declaration (parser,
6036 /*function_definition_allowed_p=*/false);
6037 /* If the tentative parse failed, then we shall need to look for an
6038 expression-statement. */
6039 if (cp_parser_parse_definitely (parser))
6040 return;
6043 cp_parser_expression_statement (parser, false);
6046 /* Parse a jump-statement.
6048 jump-statement:
6049 break ;
6050 continue ;
6051 return expression [opt] ;
6052 goto identifier ;
6054 GNU extension:
6056 jump-statement:
6057 goto * expression ;
6059 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6060 GOTO_STMT. */
6062 static tree
6063 cp_parser_jump_statement (cp_parser* parser)
6065 tree statement = error_mark_node;
6066 cp_token *token;
6067 enum rid keyword;
6069 /* Peek at the next token. */
6070 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6071 if (!token)
6072 return error_mark_node;
6074 /* See what kind of keyword it is. */
6075 keyword = token->keyword;
6076 switch (keyword)
6078 case RID_BREAK:
6079 if (!parser->in_switch_statement_p
6080 && !parser->in_iteration_statement_p)
6082 error ("break statement not within loop or switch");
6083 statement = error_mark_node;
6085 else
6086 statement = finish_break_stmt ();
6087 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6088 break;
6090 case RID_CONTINUE:
6091 if (!parser->in_iteration_statement_p)
6093 error ("continue statement not within a loop");
6094 statement = error_mark_node;
6096 else
6097 statement = finish_continue_stmt ();
6098 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6099 break;
6101 case RID_RETURN:
6103 tree expr;
6105 /* If the next token is a `;', then there is no
6106 expression. */
6107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6108 expr = cp_parser_expression (parser);
6109 else
6110 expr = NULL_TREE;
6111 /* Build the return-statement. */
6112 statement = finish_return_stmt (expr);
6113 /* Look for the final `;'. */
6114 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6116 break;
6118 case RID_GOTO:
6119 /* Create the goto-statement. */
6120 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6122 /* Issue a warning about this use of a GNU extension. */
6123 if (pedantic)
6124 pedwarn ("ISO C++ forbids computed gotos");
6125 /* Consume the '*' token. */
6126 cp_lexer_consume_token (parser->lexer);
6127 /* Parse the dependent expression. */
6128 finish_goto_stmt (cp_parser_expression (parser));
6130 else
6131 finish_goto_stmt (cp_parser_identifier (parser));
6132 /* Look for the final `;'. */
6133 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6134 break;
6136 default:
6137 cp_parser_error (parser, "expected jump-statement");
6138 break;
6141 return statement;
6144 /* Parse a declaration-statement.
6146 declaration-statement:
6147 block-declaration */
6149 static void
6150 cp_parser_declaration_statement (cp_parser* parser)
6152 /* Parse the block-declaration. */
6153 cp_parser_block_declaration (parser, /*statement_p=*/true);
6155 /* Finish off the statement. */
6156 finish_stmt ();
6159 /* Some dependent statements (like `if (cond) statement'), are
6160 implicitly in their own scope. In other words, if the statement is
6161 a single statement (as opposed to a compound-statement), it is
6162 none-the-less treated as if it were enclosed in braces. Any
6163 declarations appearing in the dependent statement are out of scope
6164 after control passes that point. This function parses a statement,
6165 but ensures that is in its own scope, even if it is not a
6166 compound-statement.
6168 Returns the new statement. */
6170 static tree
6171 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6173 tree statement;
6175 /* If the token is not a `{', then we must take special action. */
6176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6178 /* Create a compound-statement. */
6179 statement = begin_compound_stmt (/*has_no_scope=*/false);
6180 /* Parse the dependent-statement. */
6181 cp_parser_statement (parser, false);
6182 /* Finish the dummy compound-statement. */
6183 finish_compound_stmt (statement);
6185 /* Otherwise, we simply parse the statement directly. */
6186 else
6187 statement = cp_parser_compound_statement (parser, false);
6189 /* Return the statement. */
6190 return statement;
6193 /* For some dependent statements (like `while (cond) statement'), we
6194 have already created a scope. Therefore, even if the dependent
6195 statement is a compound-statement, we do not want to create another
6196 scope. */
6198 static void
6199 cp_parser_already_scoped_statement (cp_parser* parser)
6201 /* If the token is not a `{', then we must take special action. */
6202 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6204 tree statement;
6206 /* Create a compound-statement. */
6207 statement = begin_compound_stmt (/*has_no_scope=*/true);
6208 /* Parse the dependent-statement. */
6209 cp_parser_statement (parser, false);
6210 /* Finish the dummy compound-statement. */
6211 finish_compound_stmt (statement);
6213 /* Otherwise, we simply parse the statement directly. */
6214 else
6215 cp_parser_statement (parser, false);
6218 /* Declarations [gram.dcl.dcl] */
6220 /* Parse an optional declaration-sequence.
6222 declaration-seq:
6223 declaration
6224 declaration-seq declaration */
6226 static void
6227 cp_parser_declaration_seq_opt (cp_parser* parser)
6229 while (true)
6231 cp_token *token;
6233 token = cp_lexer_peek_token (parser->lexer);
6235 if (token->type == CPP_CLOSE_BRACE
6236 || token->type == CPP_EOF)
6237 break;
6239 if (token->type == CPP_SEMICOLON)
6241 /* A declaration consisting of a single semicolon is
6242 invalid. Allow it unless we're being pedantic. */
6243 if (pedantic && !in_system_header)
6244 pedwarn ("extra `;'");
6245 cp_lexer_consume_token (parser->lexer);
6246 continue;
6249 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6250 parser to enter or exit implicit `extern "C"' blocks. */
6251 while (pending_lang_change > 0)
6253 push_lang_context (lang_name_c);
6254 --pending_lang_change;
6256 while (pending_lang_change < 0)
6258 pop_lang_context ();
6259 ++pending_lang_change;
6262 /* Parse the declaration itself. */
6263 cp_parser_declaration (parser);
6267 /* Parse a declaration.
6269 declaration:
6270 block-declaration
6271 function-definition
6272 template-declaration
6273 explicit-instantiation
6274 explicit-specialization
6275 linkage-specification
6276 namespace-definition
6278 GNU extension:
6280 declaration:
6281 __extension__ declaration */
6283 static void
6284 cp_parser_declaration (cp_parser* parser)
6286 cp_token token1;
6287 cp_token token2;
6288 int saved_pedantic;
6290 /* Check for the `__extension__' keyword. */
6291 if (cp_parser_extension_opt (parser, &saved_pedantic))
6293 /* Parse the qualified declaration. */
6294 cp_parser_declaration (parser);
6295 /* Restore the PEDANTIC flag. */
6296 pedantic = saved_pedantic;
6298 return;
6301 /* Try to figure out what kind of declaration is present. */
6302 token1 = *cp_lexer_peek_token (parser->lexer);
6303 if (token1.type != CPP_EOF)
6304 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6306 /* If the next token is `extern' and the following token is a string
6307 literal, then we have a linkage specification. */
6308 if (token1.keyword == RID_EXTERN
6309 && cp_parser_is_string_literal (&token2))
6310 cp_parser_linkage_specification (parser);
6311 /* If the next token is `template', then we have either a template
6312 declaration, an explicit instantiation, or an explicit
6313 specialization. */
6314 else if (token1.keyword == RID_TEMPLATE)
6316 /* `template <>' indicates a template specialization. */
6317 if (token2.type == CPP_LESS
6318 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6319 cp_parser_explicit_specialization (parser);
6320 /* `template <' indicates a template declaration. */
6321 else if (token2.type == CPP_LESS)
6322 cp_parser_template_declaration (parser, /*member_p=*/false);
6323 /* Anything else must be an explicit instantiation. */
6324 else
6325 cp_parser_explicit_instantiation (parser);
6327 /* If the next token is `export', then we have a template
6328 declaration. */
6329 else if (token1.keyword == RID_EXPORT)
6330 cp_parser_template_declaration (parser, /*member_p=*/false);
6331 /* If the next token is `extern', 'static' or 'inline' and the one
6332 after that is `template', we have a GNU extended explicit
6333 instantiation directive. */
6334 else if (cp_parser_allow_gnu_extensions_p (parser)
6335 && (token1.keyword == RID_EXTERN
6336 || token1.keyword == RID_STATIC
6337 || token1.keyword == RID_INLINE)
6338 && token2.keyword == RID_TEMPLATE)
6339 cp_parser_explicit_instantiation (parser);
6340 /* If the next token is `namespace', check for a named or unnamed
6341 namespace definition. */
6342 else if (token1.keyword == RID_NAMESPACE
6343 && (/* A named namespace definition. */
6344 (token2.type == CPP_NAME
6345 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6346 == CPP_OPEN_BRACE))
6347 /* An unnamed namespace definition. */
6348 || token2.type == CPP_OPEN_BRACE))
6349 cp_parser_namespace_definition (parser);
6350 /* We must have either a block declaration or a function
6351 definition. */
6352 else
6353 /* Try to parse a block-declaration, or a function-definition. */
6354 cp_parser_block_declaration (parser, /*statement_p=*/false);
6357 /* Parse a block-declaration.
6359 block-declaration:
6360 simple-declaration
6361 asm-definition
6362 namespace-alias-definition
6363 using-declaration
6364 using-directive
6366 GNU Extension:
6368 block-declaration:
6369 __extension__ block-declaration
6370 label-declaration
6372 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6373 part of a declaration-statement. */
6375 static void
6376 cp_parser_block_declaration (cp_parser *parser,
6377 bool statement_p)
6379 cp_token *token1;
6380 int saved_pedantic;
6382 /* Check for the `__extension__' keyword. */
6383 if (cp_parser_extension_opt (parser, &saved_pedantic))
6385 /* Parse the qualified declaration. */
6386 cp_parser_block_declaration (parser, statement_p);
6387 /* Restore the PEDANTIC flag. */
6388 pedantic = saved_pedantic;
6390 return;
6393 /* Peek at the next token to figure out which kind of declaration is
6394 present. */
6395 token1 = cp_lexer_peek_token (parser->lexer);
6397 /* If the next keyword is `asm', we have an asm-definition. */
6398 if (token1->keyword == RID_ASM)
6400 if (statement_p)
6401 cp_parser_commit_to_tentative_parse (parser);
6402 cp_parser_asm_definition (parser);
6404 /* If the next keyword is `namespace', we have a
6405 namespace-alias-definition. */
6406 else if (token1->keyword == RID_NAMESPACE)
6407 cp_parser_namespace_alias_definition (parser);
6408 /* If the next keyword is `using', we have either a
6409 using-declaration or a using-directive. */
6410 else if (token1->keyword == RID_USING)
6412 cp_token *token2;
6414 if (statement_p)
6415 cp_parser_commit_to_tentative_parse (parser);
6416 /* If the token after `using' is `namespace', then we have a
6417 using-directive. */
6418 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6419 if (token2->keyword == RID_NAMESPACE)
6420 cp_parser_using_directive (parser);
6421 /* Otherwise, it's a using-declaration. */
6422 else
6423 cp_parser_using_declaration (parser);
6425 /* If the next keyword is `__label__' we have a label declaration. */
6426 else if (token1->keyword == RID_LABEL)
6428 if (statement_p)
6429 cp_parser_commit_to_tentative_parse (parser);
6430 cp_parser_label_declaration (parser);
6432 /* Anything else must be a simple-declaration. */
6433 else
6434 cp_parser_simple_declaration (parser, !statement_p);
6437 /* Parse a simple-declaration.
6439 simple-declaration:
6440 decl-specifier-seq [opt] init-declarator-list [opt] ;
6442 init-declarator-list:
6443 init-declarator
6444 init-declarator-list , init-declarator
6446 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6447 function-definition as a simple-declaration. */
6449 static void
6450 cp_parser_simple_declaration (cp_parser* parser,
6451 bool function_definition_allowed_p)
6453 tree decl_specifiers;
6454 tree attributes;
6455 int declares_class_or_enum;
6456 bool saw_declarator;
6458 /* Defer access checks until we know what is being declared; the
6459 checks for names appearing in the decl-specifier-seq should be
6460 done as if we were in the scope of the thing being declared. */
6461 push_deferring_access_checks (dk_deferred);
6463 /* Parse the decl-specifier-seq. We have to keep track of whether
6464 or not the decl-specifier-seq declares a named class or
6465 enumeration type, since that is the only case in which the
6466 init-declarator-list is allowed to be empty.
6468 [dcl.dcl]
6470 In a simple-declaration, the optional init-declarator-list can be
6471 omitted only when declaring a class or enumeration, that is when
6472 the decl-specifier-seq contains either a class-specifier, an
6473 elaborated-type-specifier, or an enum-specifier. */
6474 decl_specifiers
6475 = cp_parser_decl_specifier_seq (parser,
6476 CP_PARSER_FLAGS_OPTIONAL,
6477 &attributes,
6478 &declares_class_or_enum);
6479 /* We no longer need to defer access checks. */
6480 stop_deferring_access_checks ();
6482 /* In a block scope, a valid declaration must always have a
6483 decl-specifier-seq. By not trying to parse declarators, we can
6484 resolve the declaration/expression ambiguity more quickly. */
6485 if (!function_definition_allowed_p && !decl_specifiers)
6487 cp_parser_error (parser, "expected declaration");
6488 goto done;
6491 /* If the next two tokens are both identifiers, the code is
6492 erroneous. The usual cause of this situation is code like:
6494 T t;
6496 where "T" should name a type -- but does not. */
6497 if (cp_parser_diagnose_invalid_type_name (parser))
6499 /* If parsing tentatively, we should commit; we really are
6500 looking at a declaration. */
6501 cp_parser_commit_to_tentative_parse (parser);
6502 /* Give up. */
6503 goto done;
6506 /* If we have seen at least one decl-specifier, and the next token
6507 is not a parenthesis, then we must be looking at a declaration.
6508 (After "int (" we might be looking at a functional cast.) */
6509 if (decl_specifiers
6510 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6511 cp_parser_commit_to_tentative_parse (parser);
6513 /* Keep going until we hit the `;' at the end of the simple
6514 declaration. */
6515 saw_declarator = false;
6516 while (cp_lexer_next_token_is_not (parser->lexer,
6517 CPP_SEMICOLON))
6519 cp_token *token;
6520 bool function_definition_p;
6521 tree decl;
6523 saw_declarator = true;
6524 /* Parse the init-declarator. */
6525 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6526 function_definition_allowed_p,
6527 /*member_p=*/false,
6528 declares_class_or_enum,
6529 &function_definition_p);
6530 /* If an error occurred while parsing tentatively, exit quickly.
6531 (That usually happens when in the body of a function; each
6532 statement is treated as a declaration-statement until proven
6533 otherwise.) */
6534 if (cp_parser_error_occurred (parser))
6535 goto done;
6536 /* Handle function definitions specially. */
6537 if (function_definition_p)
6539 /* If the next token is a `,', then we are probably
6540 processing something like:
6542 void f() {}, *p;
6544 which is erroneous. */
6545 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6546 error ("mixing declarations and function-definitions is forbidden");
6547 /* Otherwise, we're done with the list of declarators. */
6548 else
6550 pop_deferring_access_checks ();
6551 return;
6554 /* The next token should be either a `,' or a `;'. */
6555 token = cp_lexer_peek_token (parser->lexer);
6556 /* If it's a `,', there are more declarators to come. */
6557 if (token->type == CPP_COMMA)
6558 cp_lexer_consume_token (parser->lexer);
6559 /* If it's a `;', we are done. */
6560 else if (token->type == CPP_SEMICOLON)
6561 break;
6562 /* Anything else is an error. */
6563 else
6565 /* If we have already issued an error message we don't need
6566 to issue another one. */
6567 if (decl != error_mark_node
6568 || (cp_parser_parsing_tentatively (parser)
6569 && !cp_parser_committed_to_tentative_parse (parser)))
6570 cp_parser_error (parser, "expected `,' or `;'");
6571 /* Skip tokens until we reach the end of the statement. */
6572 cp_parser_skip_to_end_of_statement (parser);
6573 /* If the next token is now a `;', consume it. */
6574 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6575 cp_lexer_consume_token (parser->lexer);
6576 goto done;
6578 /* After the first time around, a function-definition is not
6579 allowed -- even if it was OK at first. For example:
6581 int i, f() {}
6583 is not valid. */
6584 function_definition_allowed_p = false;
6587 /* Issue an error message if no declarators are present, and the
6588 decl-specifier-seq does not itself declare a class or
6589 enumeration. */
6590 if (!saw_declarator)
6592 if (cp_parser_declares_only_class_p (parser))
6593 shadow_tag (decl_specifiers);
6594 /* Perform any deferred access checks. */
6595 perform_deferred_access_checks ();
6598 /* Consume the `;'. */
6599 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6601 done:
6602 pop_deferring_access_checks ();
6605 /* Parse a decl-specifier-seq.
6607 decl-specifier-seq:
6608 decl-specifier-seq [opt] decl-specifier
6610 decl-specifier:
6611 storage-class-specifier
6612 type-specifier
6613 function-specifier
6614 friend
6615 typedef
6617 GNU Extension:
6619 decl-specifier:
6620 attributes
6622 Returns a TREE_LIST, giving the decl-specifiers in the order they
6623 appear in the source code. The TREE_VALUE of each node is the
6624 decl-specifier. For a keyword (such as `auto' or `friend'), the
6625 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6626 representation of a type-specifier, see cp_parser_type_specifier.
6628 If there are attributes, they will be stored in *ATTRIBUTES,
6629 represented as described above cp_parser_attributes.
6631 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6632 appears, and the entity that will be a friend is not going to be a
6633 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6634 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6635 friendship is granted might not be a class.
6637 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6638 flags:
6640 1: one of the decl-specifiers is an elaborated-type-specifier
6641 (i.e., a type declaration)
6642 2: one of the decl-specifiers is an enum-specifier or a
6643 class-specifier (i.e., a type definition)
6647 static tree
6648 cp_parser_decl_specifier_seq (cp_parser* parser,
6649 cp_parser_flags flags,
6650 tree* attributes,
6651 int* declares_class_or_enum)
6653 tree decl_specs = NULL_TREE;
6654 bool friend_p = false;
6655 bool constructor_possible_p = !parser->in_declarator_p;
6657 /* Assume no class or enumeration type is declared. */
6658 *declares_class_or_enum = 0;
6660 /* Assume there are no attributes. */
6661 *attributes = NULL_TREE;
6663 /* Keep reading specifiers until there are no more to read. */
6664 while (true)
6666 tree decl_spec = NULL_TREE;
6667 bool constructor_p;
6668 cp_token *token;
6670 /* Peek at the next token. */
6671 token = cp_lexer_peek_token (parser->lexer);
6672 /* Handle attributes. */
6673 if (token->keyword == RID_ATTRIBUTE)
6675 /* Parse the attributes. */
6676 decl_spec = cp_parser_attributes_opt (parser);
6677 /* Add them to the list. */
6678 *attributes = chainon (*attributes, decl_spec);
6679 continue;
6681 /* If the next token is an appropriate keyword, we can simply
6682 add it to the list. */
6683 switch (token->keyword)
6685 case RID_FRIEND:
6686 /* decl-specifier:
6687 friend */
6688 if (friend_p)
6689 error ("duplicate `friend'");
6690 else
6691 friend_p = true;
6692 /* The representation of the specifier is simply the
6693 appropriate TREE_IDENTIFIER node. */
6694 decl_spec = token->value;
6695 /* Consume the token. */
6696 cp_lexer_consume_token (parser->lexer);
6697 break;
6699 /* function-specifier:
6700 inline
6701 virtual
6702 explicit */
6703 case RID_INLINE:
6704 case RID_VIRTUAL:
6705 case RID_EXPLICIT:
6706 decl_spec = cp_parser_function_specifier_opt (parser);
6707 break;
6709 /* decl-specifier:
6710 typedef */
6711 case RID_TYPEDEF:
6712 /* The representation of the specifier is simply the
6713 appropriate TREE_IDENTIFIER node. */
6714 decl_spec = token->value;
6715 /* Consume the token. */
6716 cp_lexer_consume_token (parser->lexer);
6717 /* A constructor declarator cannot appear in a typedef. */
6718 constructor_possible_p = false;
6719 /* The "typedef" keyword can only occur in a declaration; we
6720 may as well commit at this point. */
6721 cp_parser_commit_to_tentative_parse (parser);
6722 break;
6724 /* storage-class-specifier:
6725 auto
6726 register
6727 static
6728 extern
6729 mutable
6731 GNU Extension:
6732 thread */
6733 case RID_AUTO:
6734 case RID_REGISTER:
6735 case RID_STATIC:
6736 case RID_EXTERN:
6737 case RID_MUTABLE:
6738 case RID_THREAD:
6739 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6740 break;
6742 default:
6743 break;
6746 /* Constructors are a special case. The `S' in `S()' is not a
6747 decl-specifier; it is the beginning of the declarator. */
6748 constructor_p = (!decl_spec
6749 && constructor_possible_p
6750 && cp_parser_constructor_declarator_p (parser,
6751 friend_p));
6753 /* If we don't have a DECL_SPEC yet, then we must be looking at
6754 a type-specifier. */
6755 if (!decl_spec && !constructor_p)
6757 int decl_spec_declares_class_or_enum;
6758 bool is_cv_qualifier;
6760 decl_spec
6761 = cp_parser_type_specifier (parser, flags,
6762 friend_p,
6763 /*is_declaration=*/true,
6764 &decl_spec_declares_class_or_enum,
6765 &is_cv_qualifier);
6767 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6769 /* If this type-specifier referenced a user-defined type
6770 (a typedef, class-name, etc.), then we can't allow any
6771 more such type-specifiers henceforth.
6773 [dcl.spec]
6775 The longest sequence of decl-specifiers that could
6776 possibly be a type name is taken as the
6777 decl-specifier-seq of a declaration. The sequence shall
6778 be self-consistent as described below.
6780 [dcl.type]
6782 As a general rule, at most one type-specifier is allowed
6783 in the complete decl-specifier-seq of a declaration. The
6784 only exceptions are the following:
6786 -- const or volatile can be combined with any other
6787 type-specifier.
6789 -- signed or unsigned can be combined with char, long,
6790 short, or int.
6792 -- ..
6794 Example:
6796 typedef char* Pc;
6797 void g (const int Pc);
6799 Here, Pc is *not* part of the decl-specifier seq; it's
6800 the declarator. Therefore, once we see a type-specifier
6801 (other than a cv-qualifier), we forbid any additional
6802 user-defined types. We *do* still allow things like `int
6803 int' to be considered a decl-specifier-seq, and issue the
6804 error message later. */
6805 if (decl_spec && !is_cv_qualifier)
6806 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6807 /* A constructor declarator cannot follow a type-specifier. */
6808 if (decl_spec)
6809 constructor_possible_p = false;
6812 /* If we still do not have a DECL_SPEC, then there are no more
6813 decl-specifiers. */
6814 if (!decl_spec)
6816 /* Issue an error message, unless the entire construct was
6817 optional. */
6818 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6820 cp_parser_error (parser, "expected decl specifier");
6821 return error_mark_node;
6824 break;
6827 /* Add the DECL_SPEC to the list of specifiers. */
6828 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6829 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6831 /* After we see one decl-specifier, further decl-specifiers are
6832 always optional. */
6833 flags |= CP_PARSER_FLAGS_OPTIONAL;
6836 /* Don't allow a friend specifier with a class definition. */
6837 if (friend_p && (*declares_class_or_enum & 2))
6838 error ("class definition may not be declared a friend");
6840 /* We have built up the DECL_SPECS in reverse order. Return them in
6841 the correct order. */
6842 return nreverse (decl_specs);
6845 /* Parse an (optional) storage-class-specifier.
6847 storage-class-specifier:
6848 auto
6849 register
6850 static
6851 extern
6852 mutable
6854 GNU Extension:
6856 storage-class-specifier:
6857 thread
6859 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6861 static tree
6862 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6864 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6866 case RID_AUTO:
6867 case RID_REGISTER:
6868 case RID_STATIC:
6869 case RID_EXTERN:
6870 case RID_MUTABLE:
6871 case RID_THREAD:
6872 /* Consume the token. */
6873 return cp_lexer_consume_token (parser->lexer)->value;
6875 default:
6876 return NULL_TREE;
6880 /* Parse an (optional) function-specifier.
6882 function-specifier:
6883 inline
6884 virtual
6885 explicit
6887 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6889 static tree
6890 cp_parser_function_specifier_opt (cp_parser* parser)
6892 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6894 case RID_INLINE:
6895 case RID_VIRTUAL:
6896 case RID_EXPLICIT:
6897 /* Consume the token. */
6898 return cp_lexer_consume_token (parser->lexer)->value;
6900 default:
6901 return NULL_TREE;
6905 /* Parse a linkage-specification.
6907 linkage-specification:
6908 extern string-literal { declaration-seq [opt] }
6909 extern string-literal declaration */
6911 static void
6912 cp_parser_linkage_specification (cp_parser* parser)
6914 cp_token *token;
6915 tree linkage;
6917 /* Look for the `extern' keyword. */
6918 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6920 /* Peek at the next token. */
6921 token = cp_lexer_peek_token (parser->lexer);
6922 /* If it's not a string-literal, then there's a problem. */
6923 if (!cp_parser_is_string_literal (token))
6925 cp_parser_error (parser, "expected language-name");
6926 return;
6928 /* Consume the token. */
6929 cp_lexer_consume_token (parser->lexer);
6931 /* Transform the literal into an identifier. If the literal is a
6932 wide-character string, or contains embedded NULs, then we can't
6933 handle it as the user wants. */
6934 if (token->type == CPP_WSTRING
6935 || (strlen (TREE_STRING_POINTER (token->value))
6936 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6938 cp_parser_error (parser, "invalid linkage-specification");
6939 /* Assume C++ linkage. */
6940 linkage = get_identifier ("c++");
6942 /* If it's a simple string constant, things are easier. */
6943 else
6944 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6946 /* We're now using the new linkage. */
6947 push_lang_context (linkage);
6949 /* If the next token is a `{', then we're using the first
6950 production. */
6951 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6953 /* Consume the `{' token. */
6954 cp_lexer_consume_token (parser->lexer);
6955 /* Parse the declarations. */
6956 cp_parser_declaration_seq_opt (parser);
6957 /* Look for the closing `}'. */
6958 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6960 /* Otherwise, there's just one declaration. */
6961 else
6963 bool saved_in_unbraced_linkage_specification_p;
6965 saved_in_unbraced_linkage_specification_p
6966 = parser->in_unbraced_linkage_specification_p;
6967 parser->in_unbraced_linkage_specification_p = true;
6968 have_extern_spec = true;
6969 cp_parser_declaration (parser);
6970 have_extern_spec = false;
6971 parser->in_unbraced_linkage_specification_p
6972 = saved_in_unbraced_linkage_specification_p;
6975 /* We're done with the linkage-specification. */
6976 pop_lang_context ();
6979 /* Special member functions [gram.special] */
6981 /* Parse a conversion-function-id.
6983 conversion-function-id:
6984 operator conversion-type-id
6986 Returns an IDENTIFIER_NODE representing the operator. */
6988 static tree
6989 cp_parser_conversion_function_id (cp_parser* parser)
6991 tree type;
6992 tree saved_scope;
6993 tree saved_qualifying_scope;
6994 tree saved_object_scope;
6995 bool pop_p = false;
6997 /* Look for the `operator' token. */
6998 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6999 return error_mark_node;
7000 /* When we parse the conversion-type-id, the current scope will be
7001 reset. However, we need that information in able to look up the
7002 conversion function later, so we save it here. */
7003 saved_scope = parser->scope;
7004 saved_qualifying_scope = parser->qualifying_scope;
7005 saved_object_scope = parser->object_scope;
7006 /* We must enter the scope of the class so that the names of
7007 entities declared within the class are available in the
7008 conversion-type-id. For example, consider:
7010 struct S {
7011 typedef int I;
7012 operator I();
7015 S::operator I() { ... }
7017 In order to see that `I' is a type-name in the definition, we
7018 must be in the scope of `S'. */
7019 if (saved_scope)
7020 pop_p = push_scope (saved_scope);
7021 /* Parse the conversion-type-id. */
7022 type = cp_parser_conversion_type_id (parser);
7023 /* Leave the scope of the class, if any. */
7024 if (pop_p)
7025 pop_scope (saved_scope);
7026 /* Restore the saved scope. */
7027 parser->scope = saved_scope;
7028 parser->qualifying_scope = saved_qualifying_scope;
7029 parser->object_scope = saved_object_scope;
7030 /* If the TYPE is invalid, indicate failure. */
7031 if (type == error_mark_node)
7032 return error_mark_node;
7033 return mangle_conv_op_name_for_type (type);
7036 /* Parse a conversion-type-id:
7038 conversion-type-id:
7039 type-specifier-seq conversion-declarator [opt]
7041 Returns the TYPE specified. */
7043 static tree
7044 cp_parser_conversion_type_id (cp_parser* parser)
7046 tree attributes;
7047 tree type_specifiers;
7048 tree declarator;
7050 /* Parse the attributes. */
7051 attributes = cp_parser_attributes_opt (parser);
7052 /* Parse the type-specifiers. */
7053 type_specifiers = cp_parser_type_specifier_seq (parser);
7054 /* If that didn't work, stop. */
7055 if (type_specifiers == error_mark_node)
7056 return error_mark_node;
7057 /* Parse the conversion-declarator. */
7058 declarator = cp_parser_conversion_declarator_opt (parser);
7060 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7061 /*initialized=*/0, &attributes);
7064 /* Parse an (optional) conversion-declarator.
7066 conversion-declarator:
7067 ptr-operator conversion-declarator [opt]
7069 Returns a representation of the declarator. See
7070 cp_parser_declarator for details. */
7072 static tree
7073 cp_parser_conversion_declarator_opt (cp_parser* parser)
7075 enum tree_code code;
7076 tree class_type;
7077 tree cv_qualifier_seq;
7079 /* We don't know if there's a ptr-operator next, or not. */
7080 cp_parser_parse_tentatively (parser);
7081 /* Try the ptr-operator. */
7082 code = cp_parser_ptr_operator (parser, &class_type,
7083 &cv_qualifier_seq);
7084 /* If it worked, look for more conversion-declarators. */
7085 if (cp_parser_parse_definitely (parser))
7087 tree declarator;
7089 /* Parse another optional declarator. */
7090 declarator = cp_parser_conversion_declarator_opt (parser);
7092 /* Create the representation of the declarator. */
7093 if (code == INDIRECT_REF)
7094 declarator = make_pointer_declarator (cv_qualifier_seq,
7095 declarator);
7096 else
7097 declarator = make_reference_declarator (cv_qualifier_seq,
7098 declarator);
7100 /* Handle the pointer-to-member case. */
7101 if (class_type)
7102 declarator = build_nt (SCOPE_REF, class_type, declarator);
7104 return declarator;
7107 return NULL_TREE;
7110 /* Parse an (optional) ctor-initializer.
7112 ctor-initializer:
7113 : mem-initializer-list
7115 Returns TRUE iff the ctor-initializer was actually present. */
7117 static bool
7118 cp_parser_ctor_initializer_opt (cp_parser* parser)
7120 /* If the next token is not a `:', then there is no
7121 ctor-initializer. */
7122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7124 /* Do default initialization of any bases and members. */
7125 if (DECL_CONSTRUCTOR_P (current_function_decl))
7126 finish_mem_initializers (NULL_TREE);
7128 return false;
7131 /* Consume the `:' token. */
7132 cp_lexer_consume_token (parser->lexer);
7133 /* And the mem-initializer-list. */
7134 cp_parser_mem_initializer_list (parser);
7136 return true;
7139 /* Parse a mem-initializer-list.
7141 mem-initializer-list:
7142 mem-initializer
7143 mem-initializer , mem-initializer-list */
7145 static void
7146 cp_parser_mem_initializer_list (cp_parser* parser)
7148 tree mem_initializer_list = NULL_TREE;
7150 /* Let the semantic analysis code know that we are starting the
7151 mem-initializer-list. */
7152 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7153 error ("only constructors take base initializers");
7155 /* Loop through the list. */
7156 while (true)
7158 tree mem_initializer;
7160 /* Parse the mem-initializer. */
7161 mem_initializer = cp_parser_mem_initializer (parser);
7162 /* Add it to the list, unless it was erroneous. */
7163 if (mem_initializer)
7165 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7166 mem_initializer_list = mem_initializer;
7168 /* If the next token is not a `,', we're done. */
7169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7170 break;
7171 /* Consume the `,' token. */
7172 cp_lexer_consume_token (parser->lexer);
7175 /* Perform semantic analysis. */
7176 if (DECL_CONSTRUCTOR_P (current_function_decl))
7177 finish_mem_initializers (mem_initializer_list);
7180 /* Parse a mem-initializer.
7182 mem-initializer:
7183 mem-initializer-id ( expression-list [opt] )
7185 GNU extension:
7187 mem-initializer:
7188 ( expression-list [opt] )
7190 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7191 class) or FIELD_DECL (for a non-static data member) to initialize;
7192 the TREE_VALUE is the expression-list. */
7194 static tree
7195 cp_parser_mem_initializer (cp_parser* parser)
7197 tree mem_initializer_id;
7198 tree expression_list;
7199 tree member;
7201 /* Find out what is being initialized. */
7202 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7204 pedwarn ("anachronistic old-style base class initializer");
7205 mem_initializer_id = NULL_TREE;
7207 else
7208 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7209 member = expand_member_init (mem_initializer_id);
7210 if (member && !DECL_P (member))
7211 in_base_initializer = 1;
7213 expression_list
7214 = cp_parser_parenthesized_expression_list (parser, false,
7215 /*non_constant_p=*/NULL);
7216 if (!expression_list)
7217 expression_list = void_type_node;
7219 in_base_initializer = 0;
7221 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7224 /* Parse a mem-initializer-id.
7226 mem-initializer-id:
7227 :: [opt] nested-name-specifier [opt] class-name
7228 identifier
7230 Returns a TYPE indicating the class to be initializer for the first
7231 production. Returns an IDENTIFIER_NODE indicating the data member
7232 to be initialized for the second production. */
7234 static tree
7235 cp_parser_mem_initializer_id (cp_parser* parser)
7237 bool global_scope_p;
7238 bool nested_name_specifier_p;
7239 bool template_p = false;
7240 tree id;
7242 /* `typename' is not allowed in this context ([temp.res]). */
7243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7245 error ("keyword `typename' not allowed in this context (a qualified "
7246 "member initializer is implicitly a type)");
7247 cp_lexer_consume_token (parser->lexer);
7249 /* Look for the optional `::' operator. */
7250 global_scope_p
7251 = (cp_parser_global_scope_opt (parser,
7252 /*current_scope_valid_p=*/false)
7253 != NULL_TREE);
7254 /* Look for the optional nested-name-specifier. The simplest way to
7255 implement:
7257 [temp.res]
7259 The keyword `typename' is not permitted in a base-specifier or
7260 mem-initializer; in these contexts a qualified name that
7261 depends on a template-parameter is implicitly assumed to be a
7262 type name.
7264 is to assume that we have seen the `typename' keyword at this
7265 point. */
7266 nested_name_specifier_p
7267 = (cp_parser_nested_name_specifier_opt (parser,
7268 /*typename_keyword_p=*/true,
7269 /*check_dependency_p=*/true,
7270 /*type_p=*/true,
7271 /*is_declaration=*/true)
7272 != NULL_TREE);
7273 if (nested_name_specifier_p)
7274 template_p = cp_parser_optional_template_keyword (parser);
7275 /* If there is a `::' operator or a nested-name-specifier, then we
7276 are definitely looking for a class-name. */
7277 if (global_scope_p || nested_name_specifier_p)
7278 return cp_parser_class_name (parser,
7279 /*typename_keyword_p=*/true,
7280 /*template_keyword_p=*/template_p,
7281 /*type_p=*/false,
7282 /*check_dependency_p=*/true,
7283 /*class_head_p=*/false,
7284 /*is_declaration=*/true);
7285 /* Otherwise, we could also be looking for an ordinary identifier. */
7286 cp_parser_parse_tentatively (parser);
7287 /* Try a class-name. */
7288 id = cp_parser_class_name (parser,
7289 /*typename_keyword_p=*/true,
7290 /*template_keyword_p=*/false,
7291 /*type_p=*/false,
7292 /*check_dependency_p=*/true,
7293 /*class_head_p=*/false,
7294 /*is_declaration=*/true);
7295 /* If we found one, we're done. */
7296 if (cp_parser_parse_definitely (parser))
7297 return id;
7298 /* Otherwise, look for an ordinary identifier. */
7299 return cp_parser_identifier (parser);
7302 /* Overloading [gram.over] */
7304 /* Parse an operator-function-id.
7306 operator-function-id:
7307 operator operator
7309 Returns an IDENTIFIER_NODE for the operator which is a
7310 human-readable spelling of the identifier, e.g., `operator +'. */
7312 static tree
7313 cp_parser_operator_function_id (cp_parser* parser)
7315 /* Look for the `operator' keyword. */
7316 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7317 return error_mark_node;
7318 /* And then the name of the operator itself. */
7319 return cp_parser_operator (parser);
7322 /* Parse an operator.
7324 operator:
7325 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7326 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7327 || ++ -- , ->* -> () []
7329 GNU Extensions:
7331 operator:
7332 <? >? <?= >?=
7334 Returns an IDENTIFIER_NODE for the operator which is a
7335 human-readable spelling of the identifier, e.g., `operator +'. */
7337 static tree
7338 cp_parser_operator (cp_parser* parser)
7340 tree id = NULL_TREE;
7341 cp_token *token;
7343 /* Peek at the next token. */
7344 token = cp_lexer_peek_token (parser->lexer);
7345 /* Figure out which operator we have. */
7346 switch (token->type)
7348 case CPP_KEYWORD:
7350 enum tree_code op;
7352 /* The keyword should be either `new' or `delete'. */
7353 if (token->keyword == RID_NEW)
7354 op = NEW_EXPR;
7355 else if (token->keyword == RID_DELETE)
7356 op = DELETE_EXPR;
7357 else
7358 break;
7360 /* Consume the `new' or `delete' token. */
7361 cp_lexer_consume_token (parser->lexer);
7363 /* Peek at the next token. */
7364 token = cp_lexer_peek_token (parser->lexer);
7365 /* If it's a `[' token then this is the array variant of the
7366 operator. */
7367 if (token->type == CPP_OPEN_SQUARE)
7369 /* Consume the `[' token. */
7370 cp_lexer_consume_token (parser->lexer);
7371 /* Look for the `]' token. */
7372 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7373 id = ansi_opname (op == NEW_EXPR
7374 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7376 /* Otherwise, we have the non-array variant. */
7377 else
7378 id = ansi_opname (op);
7380 return id;
7383 case CPP_PLUS:
7384 id = ansi_opname (PLUS_EXPR);
7385 break;
7387 case CPP_MINUS:
7388 id = ansi_opname (MINUS_EXPR);
7389 break;
7391 case CPP_MULT:
7392 id = ansi_opname (MULT_EXPR);
7393 break;
7395 case CPP_DIV:
7396 id = ansi_opname (TRUNC_DIV_EXPR);
7397 break;
7399 case CPP_MOD:
7400 id = ansi_opname (TRUNC_MOD_EXPR);
7401 break;
7403 case CPP_XOR:
7404 id = ansi_opname (BIT_XOR_EXPR);
7405 break;
7407 case CPP_AND:
7408 id = ansi_opname (BIT_AND_EXPR);
7409 break;
7411 case CPP_OR:
7412 id = ansi_opname (BIT_IOR_EXPR);
7413 break;
7415 case CPP_COMPL:
7416 id = ansi_opname (BIT_NOT_EXPR);
7417 break;
7419 case CPP_NOT:
7420 id = ansi_opname (TRUTH_NOT_EXPR);
7421 break;
7423 case CPP_EQ:
7424 id = ansi_assopname (NOP_EXPR);
7425 break;
7427 case CPP_LESS:
7428 id = ansi_opname (LT_EXPR);
7429 break;
7431 case CPP_GREATER:
7432 id = ansi_opname (GT_EXPR);
7433 break;
7435 case CPP_PLUS_EQ:
7436 id = ansi_assopname (PLUS_EXPR);
7437 break;
7439 case CPP_MINUS_EQ:
7440 id = ansi_assopname (MINUS_EXPR);
7441 break;
7443 case CPP_MULT_EQ:
7444 id = ansi_assopname (MULT_EXPR);
7445 break;
7447 case CPP_DIV_EQ:
7448 id = ansi_assopname (TRUNC_DIV_EXPR);
7449 break;
7451 case CPP_MOD_EQ:
7452 id = ansi_assopname (TRUNC_MOD_EXPR);
7453 break;
7455 case CPP_XOR_EQ:
7456 id = ansi_assopname (BIT_XOR_EXPR);
7457 break;
7459 case CPP_AND_EQ:
7460 id = ansi_assopname (BIT_AND_EXPR);
7461 break;
7463 case CPP_OR_EQ:
7464 id = ansi_assopname (BIT_IOR_EXPR);
7465 break;
7467 case CPP_LSHIFT:
7468 id = ansi_opname (LSHIFT_EXPR);
7469 break;
7471 case CPP_RSHIFT:
7472 id = ansi_opname (RSHIFT_EXPR);
7473 break;
7475 case CPP_LSHIFT_EQ:
7476 id = ansi_assopname (LSHIFT_EXPR);
7477 break;
7479 case CPP_RSHIFT_EQ:
7480 id = ansi_assopname (RSHIFT_EXPR);
7481 break;
7483 case CPP_EQ_EQ:
7484 id = ansi_opname (EQ_EXPR);
7485 break;
7487 case CPP_NOT_EQ:
7488 id = ansi_opname (NE_EXPR);
7489 break;
7491 case CPP_LESS_EQ:
7492 id = ansi_opname (LE_EXPR);
7493 break;
7495 case CPP_GREATER_EQ:
7496 id = ansi_opname (GE_EXPR);
7497 break;
7499 case CPP_AND_AND:
7500 id = ansi_opname (TRUTH_ANDIF_EXPR);
7501 break;
7503 case CPP_OR_OR:
7504 id = ansi_opname (TRUTH_ORIF_EXPR);
7505 break;
7507 case CPP_PLUS_PLUS:
7508 id = ansi_opname (POSTINCREMENT_EXPR);
7509 break;
7511 case CPP_MINUS_MINUS:
7512 id = ansi_opname (PREDECREMENT_EXPR);
7513 break;
7515 case CPP_COMMA:
7516 id = ansi_opname (COMPOUND_EXPR);
7517 break;
7519 case CPP_DEREF_STAR:
7520 id = ansi_opname (MEMBER_REF);
7521 break;
7523 case CPP_DEREF:
7524 id = ansi_opname (COMPONENT_REF);
7525 break;
7527 case CPP_OPEN_PAREN:
7528 /* Consume the `('. */
7529 cp_lexer_consume_token (parser->lexer);
7530 /* Look for the matching `)'. */
7531 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7532 return ansi_opname (CALL_EXPR);
7534 case CPP_OPEN_SQUARE:
7535 /* Consume the `['. */
7536 cp_lexer_consume_token (parser->lexer);
7537 /* Look for the matching `]'. */
7538 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7539 return ansi_opname (ARRAY_REF);
7541 /* Extensions. */
7542 case CPP_MIN:
7543 id = ansi_opname (MIN_EXPR);
7544 break;
7546 case CPP_MAX:
7547 id = ansi_opname (MAX_EXPR);
7548 break;
7550 case CPP_MIN_EQ:
7551 id = ansi_assopname (MIN_EXPR);
7552 break;
7554 case CPP_MAX_EQ:
7555 id = ansi_assopname (MAX_EXPR);
7556 break;
7558 default:
7559 /* Anything else is an error. */
7560 break;
7563 /* If we have selected an identifier, we need to consume the
7564 operator token. */
7565 if (id)
7566 cp_lexer_consume_token (parser->lexer);
7567 /* Otherwise, no valid operator name was present. */
7568 else
7570 cp_parser_error (parser, "expected operator");
7571 id = error_mark_node;
7574 return id;
7577 /* Parse a template-declaration.
7579 template-declaration:
7580 export [opt] template < template-parameter-list > declaration
7582 If MEMBER_P is TRUE, this template-declaration occurs within a
7583 class-specifier.
7585 The grammar rule given by the standard isn't correct. What
7586 is really meant is:
7588 template-declaration:
7589 export [opt] template-parameter-list-seq
7590 decl-specifier-seq [opt] init-declarator [opt] ;
7591 export [opt] template-parameter-list-seq
7592 function-definition
7594 template-parameter-list-seq:
7595 template-parameter-list-seq [opt]
7596 template < template-parameter-list > */
7598 static void
7599 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7601 /* Check for `export'. */
7602 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7604 /* Consume the `export' token. */
7605 cp_lexer_consume_token (parser->lexer);
7606 /* Warn that we do not support `export'. */
7607 warning ("keyword `export' not implemented, and will be ignored");
7610 cp_parser_template_declaration_after_export (parser, member_p);
7613 /* Parse a template-parameter-list.
7615 template-parameter-list:
7616 template-parameter
7617 template-parameter-list , template-parameter
7619 Returns a TREE_LIST. Each node represents a template parameter.
7620 The nodes are connected via their TREE_CHAINs. */
7622 static tree
7623 cp_parser_template_parameter_list (cp_parser* parser)
7625 tree parameter_list = NULL_TREE;
7627 while (true)
7629 tree parameter;
7630 cp_token *token;
7632 /* Parse the template-parameter. */
7633 parameter = cp_parser_template_parameter (parser);
7634 /* Add it to the list. */
7635 parameter_list = process_template_parm (parameter_list,
7636 parameter);
7638 /* Peek at the next token. */
7639 token = cp_lexer_peek_token (parser->lexer);
7640 /* If it's not a `,', we're done. */
7641 if (token->type != CPP_COMMA)
7642 break;
7643 /* Otherwise, consume the `,' token. */
7644 cp_lexer_consume_token (parser->lexer);
7647 return parameter_list;
7650 /* Parse a template-parameter.
7652 template-parameter:
7653 type-parameter
7654 parameter-declaration
7656 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7657 TREE_PURPOSE is the default value, if any. */
7659 static tree
7660 cp_parser_template_parameter (cp_parser* parser)
7662 cp_token *token;
7664 /* Peek at the next token. */
7665 token = cp_lexer_peek_token (parser->lexer);
7666 /* If it is `class' or `template', we have a type-parameter. */
7667 if (token->keyword == RID_TEMPLATE)
7668 return cp_parser_type_parameter (parser);
7669 /* If it is `class' or `typename' we do not know yet whether it is a
7670 type parameter or a non-type parameter. Consider:
7672 template <typename T, typename T::X X> ...
7676 template <class C, class D*> ...
7678 Here, the first parameter is a type parameter, and the second is
7679 a non-type parameter. We can tell by looking at the token after
7680 the identifier -- if it is a `,', `=', or `>' then we have a type
7681 parameter. */
7682 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7684 /* Peek at the token after `class' or `typename'. */
7685 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7686 /* If it's an identifier, skip it. */
7687 if (token->type == CPP_NAME)
7688 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7689 /* Now, see if the token looks like the end of a template
7690 parameter. */
7691 if (token->type == CPP_COMMA
7692 || token->type == CPP_EQ
7693 || token->type == CPP_GREATER)
7694 return cp_parser_type_parameter (parser);
7697 /* Otherwise, it is a non-type parameter.
7699 [temp.param]
7701 When parsing a default template-argument for a non-type
7702 template-parameter, the first non-nested `>' is taken as the end
7703 of the template parameter-list rather than a greater-than
7704 operator. */
7705 return
7706 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7707 /*parenthesized_p=*/NULL);
7710 /* Parse a type-parameter.
7712 type-parameter:
7713 class identifier [opt]
7714 class identifier [opt] = type-id
7715 typename identifier [opt]
7716 typename identifier [opt] = type-id
7717 template < template-parameter-list > class identifier [opt]
7718 template < template-parameter-list > class identifier [opt]
7719 = id-expression
7721 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7722 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7723 the declaration of the parameter. */
7725 static tree
7726 cp_parser_type_parameter (cp_parser* parser)
7728 cp_token *token;
7729 tree parameter;
7731 /* Look for a keyword to tell us what kind of parameter this is. */
7732 token = cp_parser_require (parser, CPP_KEYWORD,
7733 "`class', `typename', or `template'");
7734 if (!token)
7735 return error_mark_node;
7737 switch (token->keyword)
7739 case RID_CLASS:
7740 case RID_TYPENAME:
7742 tree identifier;
7743 tree default_argument;
7745 /* If the next token is an identifier, then it names the
7746 parameter. */
7747 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7748 identifier = cp_parser_identifier (parser);
7749 else
7750 identifier = NULL_TREE;
7752 /* Create the parameter. */
7753 parameter = finish_template_type_parm (class_type_node, identifier);
7755 /* If the next token is an `=', we have a default argument. */
7756 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7758 /* Consume the `=' token. */
7759 cp_lexer_consume_token (parser->lexer);
7760 /* Parse the default-argument. */
7761 default_argument = cp_parser_type_id (parser);
7763 else
7764 default_argument = NULL_TREE;
7766 /* Create the combined representation of the parameter and the
7767 default argument. */
7768 parameter = build_tree_list (default_argument, parameter);
7770 break;
7772 case RID_TEMPLATE:
7774 tree parameter_list;
7775 tree identifier;
7776 tree default_argument;
7778 /* Look for the `<'. */
7779 cp_parser_require (parser, CPP_LESS, "`<'");
7780 /* Parse the template-parameter-list. */
7781 begin_template_parm_list ();
7782 parameter_list
7783 = cp_parser_template_parameter_list (parser);
7784 parameter_list = end_template_parm_list (parameter_list);
7785 /* Look for the `>'. */
7786 cp_parser_require (parser, CPP_GREATER, "`>'");
7787 /* Look for the `class' keyword. */
7788 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7789 /* If the next token is an `=', then there is a
7790 default-argument. If the next token is a `>', we are at
7791 the end of the parameter-list. If the next token is a `,',
7792 then we are at the end of this parameter. */
7793 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7794 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7795 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7797 identifier = cp_parser_identifier (parser);
7798 /* Treat invalid names as if the parameter were nameless. */
7799 if (identifier == error_mark_node)
7800 identifier = NULL_TREE;
7802 else
7803 identifier = NULL_TREE;
7805 /* Create the template parameter. */
7806 parameter = finish_template_template_parm (class_type_node,
7807 identifier);
7809 /* If the next token is an `=', then there is a
7810 default-argument. */
7811 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7813 bool is_template;
7815 /* Consume the `='. */
7816 cp_lexer_consume_token (parser->lexer);
7817 /* Parse the id-expression. */
7818 default_argument
7819 = cp_parser_id_expression (parser,
7820 /*template_keyword_p=*/false,
7821 /*check_dependency_p=*/true,
7822 /*template_p=*/&is_template,
7823 /*declarator_p=*/false);
7824 if (TREE_CODE (default_argument) == TYPE_DECL)
7825 /* If the id-expression was a template-id that refers to
7826 a template-class, we already have the declaration here,
7827 so no further lookup is needed. */
7829 else
7830 /* Look up the name. */
7831 default_argument
7832 = cp_parser_lookup_name (parser, default_argument,
7833 /*is_type=*/false,
7834 /*is_template=*/is_template,
7835 /*is_namespace=*/false,
7836 /*check_dependency=*/true);
7837 /* See if the default argument is valid. */
7838 default_argument
7839 = check_template_template_default_arg (default_argument);
7841 else
7842 default_argument = NULL_TREE;
7844 /* Create the combined representation of the parameter and the
7845 default argument. */
7846 parameter = build_tree_list (default_argument, parameter);
7848 break;
7850 default:
7851 abort ();
7852 break;
7855 return parameter;
7858 /* Parse a template-id.
7860 template-id:
7861 template-name < template-argument-list [opt] >
7863 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7864 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7865 returned. Otherwise, if the template-name names a function, or set
7866 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7867 names a class, returns a TYPE_DECL for the specialization.
7869 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7870 uninstantiated templates. */
7872 static tree
7873 cp_parser_template_id (cp_parser *parser,
7874 bool template_keyword_p,
7875 bool check_dependency_p,
7876 bool is_declaration)
7878 tree template;
7879 tree arguments;
7880 tree template_id;
7881 ptrdiff_t start_of_id;
7882 tree access_check = NULL_TREE;
7883 cp_token *next_token, *next_token_2;
7884 bool is_identifier;
7886 /* If the next token corresponds to a template-id, there is no need
7887 to reparse it. */
7888 next_token = cp_lexer_peek_token (parser->lexer);
7889 if (next_token->type == CPP_TEMPLATE_ID)
7891 tree value;
7892 tree check;
7894 /* Get the stored value. */
7895 value = cp_lexer_consume_token (parser->lexer)->value;
7896 /* Perform any access checks that were deferred. */
7897 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7898 perform_or_defer_access_check (TREE_PURPOSE (check),
7899 TREE_VALUE (check));
7900 /* Return the stored value. */
7901 return TREE_VALUE (value);
7904 /* Avoid performing name lookup if there is no possibility of
7905 finding a template-id. */
7906 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7907 || (next_token->type == CPP_NAME
7908 && !cp_parser_nth_token_starts_template_argument_list_p
7909 (parser, 2)))
7911 cp_parser_error (parser, "expected template-id");
7912 return error_mark_node;
7915 /* Remember where the template-id starts. */
7916 if (cp_parser_parsing_tentatively (parser)
7917 && !cp_parser_committed_to_tentative_parse (parser))
7919 next_token = cp_lexer_peek_token (parser->lexer);
7920 start_of_id = cp_lexer_token_difference (parser->lexer,
7921 parser->lexer->first_token,
7922 next_token);
7924 else
7925 start_of_id = -1;
7927 push_deferring_access_checks (dk_deferred);
7929 /* Parse the template-name. */
7930 is_identifier = false;
7931 template = cp_parser_template_name (parser, template_keyword_p,
7932 check_dependency_p,
7933 is_declaration,
7934 &is_identifier);
7935 if (template == error_mark_node || is_identifier)
7937 pop_deferring_access_checks ();
7938 return template;
7941 /* If we find the sequence `[:' after a template-name, it's probably
7942 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7943 parse correctly the argument list. */
7944 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7945 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7946 if (next_token->type == CPP_OPEN_SQUARE
7947 && next_token->flags & DIGRAPH
7948 && next_token_2->type == CPP_COLON
7949 && !(next_token_2->flags & PREV_WHITE))
7951 cp_parser_parse_tentatively (parser);
7952 /* Change `:' into `::'. */
7953 next_token_2->type = CPP_SCOPE;
7954 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7955 CPP_LESS. */
7956 cp_lexer_consume_token (parser->lexer);
7957 /* Parse the arguments. */
7958 arguments = cp_parser_enclosed_template_argument_list (parser);
7959 if (!cp_parser_parse_definitely (parser))
7961 /* If we couldn't parse an argument list, then we revert our changes
7962 and return simply an error. Maybe this is not a template-id
7963 after all. */
7964 next_token_2->type = CPP_COLON;
7965 cp_parser_error (parser, "expected `<'");
7966 pop_deferring_access_checks ();
7967 return error_mark_node;
7969 /* Otherwise, emit an error about the invalid digraph, but continue
7970 parsing because we got our argument list. */
7971 pedwarn ("`<::' cannot begin a template-argument list");
7972 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7973 "between `<' and `::'");
7974 if (!flag_permissive)
7976 static bool hint;
7977 if (!hint)
7979 inform ("(if you use `-fpermissive' G++ will accept your code)");
7980 hint = true;
7984 else
7986 /* Look for the `<' that starts the template-argument-list. */
7987 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7989 pop_deferring_access_checks ();
7990 return error_mark_node;
7992 /* Parse the arguments. */
7993 arguments = cp_parser_enclosed_template_argument_list (parser);
7996 /* Build a representation of the specialization. */
7997 if (TREE_CODE (template) == IDENTIFIER_NODE)
7998 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7999 else if (DECL_CLASS_TEMPLATE_P (template)
8000 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8001 template_id
8002 = finish_template_type (template, arguments,
8003 cp_lexer_next_token_is (parser->lexer,
8004 CPP_SCOPE));
8005 else
8007 /* If it's not a class-template or a template-template, it should be
8008 a function-template. */
8009 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8010 || TREE_CODE (template) == OVERLOAD
8011 || BASELINK_P (template)),
8012 20010716);
8014 template_id = lookup_template_function (template, arguments);
8017 /* Retrieve any deferred checks. Do not pop this access checks yet
8018 so the memory will not be reclaimed during token replacing below. */
8019 access_check = get_deferred_access_checks ();
8021 /* If parsing tentatively, replace the sequence of tokens that makes
8022 up the template-id with a CPP_TEMPLATE_ID token. That way,
8023 should we re-parse the token stream, we will not have to repeat
8024 the effort required to do the parse, nor will we issue duplicate
8025 error messages about problems during instantiation of the
8026 template. */
8027 if (start_of_id >= 0)
8029 cp_token *token;
8031 /* Find the token that corresponds to the start of the
8032 template-id. */
8033 token = cp_lexer_advance_token (parser->lexer,
8034 parser->lexer->first_token,
8035 start_of_id);
8037 /* Reset the contents of the START_OF_ID token. */
8038 token->type = CPP_TEMPLATE_ID;
8039 token->value = build_tree_list (access_check, template_id);
8040 token->keyword = RID_MAX;
8041 /* Purge all subsequent tokens. */
8042 cp_lexer_purge_tokens_after (parser->lexer, token);
8044 /* ??? Can we actually assume that, if template_id ==
8045 error_mark_node, we will have issued a diagnostic to the
8046 user, as opposed to simply marking the tentative parse as
8047 failed? */
8048 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8049 error ("parse error in template argument list");
8052 pop_deferring_access_checks ();
8053 return template_id;
8056 /* Parse a template-name.
8058 template-name:
8059 identifier
8061 The standard should actually say:
8063 template-name:
8064 identifier
8065 operator-function-id
8067 A defect report has been filed about this issue.
8069 A conversion-function-id cannot be a template name because they cannot
8070 be part of a template-id. In fact, looking at this code:
8072 a.operator K<int>()
8074 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8075 It is impossible to call a templated conversion-function-id with an
8076 explicit argument list, since the only allowed template parameter is
8077 the type to which it is converting.
8079 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8080 `template' keyword, in a construction like:
8082 T::template f<3>()
8084 In that case `f' is taken to be a template-name, even though there
8085 is no way of knowing for sure.
8087 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8088 name refers to a set of overloaded functions, at least one of which
8089 is a template, or an IDENTIFIER_NODE with the name of the template,
8090 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8091 names are looked up inside uninstantiated templates. */
8093 static tree
8094 cp_parser_template_name (cp_parser* parser,
8095 bool template_keyword_p,
8096 bool check_dependency_p,
8097 bool is_declaration,
8098 bool *is_identifier)
8100 tree identifier;
8101 tree decl;
8102 tree fns;
8104 /* If the next token is `operator', then we have either an
8105 operator-function-id or a conversion-function-id. */
8106 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8108 /* We don't know whether we're looking at an
8109 operator-function-id or a conversion-function-id. */
8110 cp_parser_parse_tentatively (parser);
8111 /* Try an operator-function-id. */
8112 identifier = cp_parser_operator_function_id (parser);
8113 /* If that didn't work, try a conversion-function-id. */
8114 if (!cp_parser_parse_definitely (parser))
8116 cp_parser_error (parser, "expected template-name");
8117 return error_mark_node;
8120 /* Look for the identifier. */
8121 else
8122 identifier = cp_parser_identifier (parser);
8124 /* If we didn't find an identifier, we don't have a template-id. */
8125 if (identifier == error_mark_node)
8126 return error_mark_node;
8128 /* If the name immediately followed the `template' keyword, then it
8129 is a template-name. However, if the next token is not `<', then
8130 we do not treat it as a template-name, since it is not being used
8131 as part of a template-id. This enables us to handle constructs
8132 like:
8134 template <typename T> struct S { S(); };
8135 template <typename T> S<T>::S();
8137 correctly. We would treat `S' as a template -- if it were `S<T>'
8138 -- but we do not if there is no `<'. */
8140 if (processing_template_decl
8141 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8143 /* In a declaration, in a dependent context, we pretend that the
8144 "template" keyword was present in order to improve error
8145 recovery. For example, given:
8147 template <typename T> void f(T::X<int>);
8149 we want to treat "X<int>" as a template-id. */
8150 if (is_declaration
8151 && !template_keyword_p
8152 && parser->scope && TYPE_P (parser->scope)
8153 && check_dependency_p
8154 && dependent_type_p (parser->scope)
8155 /* Do not do this for dtors (or ctors), since they never
8156 need the template keyword before their name. */
8157 && !constructor_name_p (identifier, parser->scope))
8159 ptrdiff_t start;
8160 cp_token* token;
8161 /* Explain what went wrong. */
8162 error ("non-template `%D' used as template", identifier);
8163 inform ("use `%T::template %D' to indicate that it is a template",
8164 parser->scope, identifier);
8165 /* If parsing tentatively, find the location of the "<"
8166 token. */
8167 if (cp_parser_parsing_tentatively (parser)
8168 && !cp_parser_committed_to_tentative_parse (parser))
8170 cp_parser_simulate_error (parser);
8171 token = cp_lexer_peek_token (parser->lexer);
8172 token = cp_lexer_prev_token (parser->lexer, token);
8173 start = cp_lexer_token_difference (parser->lexer,
8174 parser->lexer->first_token,
8175 token);
8177 else
8178 start = -1;
8179 /* Parse the template arguments so that we can issue error
8180 messages about them. */
8181 cp_lexer_consume_token (parser->lexer);
8182 cp_parser_enclosed_template_argument_list (parser);
8183 /* Skip tokens until we find a good place from which to
8184 continue parsing. */
8185 cp_parser_skip_to_closing_parenthesis (parser,
8186 /*recovering=*/true,
8187 /*or_comma=*/true,
8188 /*consume_paren=*/false);
8189 /* If parsing tentatively, permanently remove the
8190 template argument list. That will prevent duplicate
8191 error messages from being issued about the missing
8192 "template" keyword. */
8193 if (start >= 0)
8195 token = cp_lexer_advance_token (parser->lexer,
8196 parser->lexer->first_token,
8197 start);
8198 cp_lexer_purge_tokens_after (parser->lexer, token);
8200 if (is_identifier)
8201 *is_identifier = true;
8202 return identifier;
8205 /* If the "template" keyword is present, then there is generally
8206 no point in doing name-lookup, so we just return IDENTIFIER.
8207 But, if the qualifying scope is non-dependent then we can
8208 (and must) do name-lookup normally. */
8209 if (template_keyword_p
8210 && (!parser->scope
8211 || (TYPE_P (parser->scope)
8212 && dependent_type_p (parser->scope))))
8213 return identifier;
8216 /* Look up the name. */
8217 decl = cp_parser_lookup_name (parser, identifier,
8218 /*is_type=*/false,
8219 /*is_template=*/false,
8220 /*is_namespace=*/false,
8221 check_dependency_p);
8222 decl = maybe_get_template_decl_from_type_decl (decl);
8224 /* If DECL is a template, then the name was a template-name. */
8225 if (TREE_CODE (decl) == TEMPLATE_DECL)
8227 else
8229 tree fn = NULL_TREE;
8231 /* The standard does not explicitly indicate whether a name that
8232 names a set of overloaded declarations, some of which are
8233 templates, is a template-name. However, such a name should
8234 be a template-name; otherwise, there is no way to form a
8235 template-id for the overloaded templates. */
8236 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8237 if (TREE_CODE (fns) == OVERLOAD)
8238 for (fn = fns; fn; fn = OVL_NEXT (fn))
8239 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8240 break;
8242 if (!fn)
8244 /* Otherwise, the name does not name a template. */
8245 cp_parser_error (parser, "expected template-name");
8246 return error_mark_node;
8250 /* If DECL is dependent, and refers to a function, then just return
8251 its name; we will look it up again during template instantiation. */
8252 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8254 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8255 if (TYPE_P (scope) && dependent_type_p (scope))
8256 return identifier;
8259 return decl;
8262 /* Parse a template-argument-list.
8264 template-argument-list:
8265 template-argument
8266 template-argument-list , template-argument
8268 Returns a TREE_VEC containing the arguments. */
8270 static tree
8271 cp_parser_template_argument_list (cp_parser* parser)
8273 tree fixed_args[10];
8274 unsigned n_args = 0;
8275 unsigned alloced = 10;
8276 tree *arg_ary = fixed_args;
8277 tree vec;
8278 bool saved_in_template_argument_list_p;
8280 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8281 parser->in_template_argument_list_p = true;
8284 tree argument;
8286 if (n_args)
8287 /* Consume the comma. */
8288 cp_lexer_consume_token (parser->lexer);
8290 /* Parse the template-argument. */
8291 argument = cp_parser_template_argument (parser);
8292 if (n_args == alloced)
8294 alloced *= 2;
8296 if (arg_ary == fixed_args)
8298 arg_ary = xmalloc (sizeof (tree) * alloced);
8299 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8301 else
8302 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8304 arg_ary[n_args++] = argument;
8306 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8308 vec = make_tree_vec (n_args);
8310 while (n_args--)
8311 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8313 if (arg_ary != fixed_args)
8314 free (arg_ary);
8315 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8316 return vec;
8319 /* Parse a template-argument.
8321 template-argument:
8322 assignment-expression
8323 type-id
8324 id-expression
8326 The representation is that of an assignment-expression, type-id, or
8327 id-expression -- except that the qualified id-expression is
8328 evaluated, so that the value returned is either a DECL or an
8329 OVERLOAD.
8331 Although the standard says "assignment-expression", it forbids
8332 throw-expressions or assignments in the template argument.
8333 Therefore, we use "conditional-expression" instead. */
8335 static tree
8336 cp_parser_template_argument (cp_parser* parser)
8338 tree argument;
8339 bool template_p;
8340 bool address_p;
8341 bool maybe_type_id = false;
8342 cp_token *token;
8343 cp_id_kind idk;
8344 tree qualifying_class;
8346 /* There's really no way to know what we're looking at, so we just
8347 try each alternative in order.
8349 [temp.arg]
8351 In a template-argument, an ambiguity between a type-id and an
8352 expression is resolved to a type-id, regardless of the form of
8353 the corresponding template-parameter.
8355 Therefore, we try a type-id first. */
8356 cp_parser_parse_tentatively (parser);
8357 argument = cp_parser_type_id (parser);
8358 /* If there was no error parsing the type-id but the next token is a '>>',
8359 we probably found a typo for '> >'. But there are type-id which are
8360 also valid expressions. For instance:
8362 struct X { int operator >> (int); };
8363 template <int V> struct Foo {};
8364 Foo<X () >> 5> r;
8366 Here 'X()' is a valid type-id of a function type, but the user just
8367 wanted to write the expression "X() >> 5". Thus, we remember that we
8368 found a valid type-id, but we still try to parse the argument as an
8369 expression to see what happens. */
8370 if (!cp_parser_error_occurred (parser)
8371 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8373 maybe_type_id = true;
8374 cp_parser_abort_tentative_parse (parser);
8376 else
8378 /* If the next token isn't a `,' or a `>', then this argument wasn't
8379 really finished. This means that the argument is not a valid
8380 type-id. */
8381 if (!cp_parser_next_token_ends_template_argument_p (parser))
8382 cp_parser_error (parser, "expected template-argument");
8383 /* If that worked, we're done. */
8384 if (cp_parser_parse_definitely (parser))
8385 return argument;
8387 /* We're still not sure what the argument will be. */
8388 cp_parser_parse_tentatively (parser);
8389 /* Try a template. */
8390 argument = cp_parser_id_expression (parser,
8391 /*template_keyword_p=*/false,
8392 /*check_dependency_p=*/true,
8393 &template_p,
8394 /*declarator_p=*/false);
8395 /* If the next token isn't a `,' or a `>', then this argument wasn't
8396 really finished. */
8397 if (!cp_parser_next_token_ends_template_argument_p (parser))
8398 cp_parser_error (parser, "expected template-argument");
8399 if (!cp_parser_error_occurred (parser))
8401 /* Figure out what is being referred to. If the id-expression
8402 was for a class template specialization, then we will have a
8403 TYPE_DECL at this point. There is no need to do name lookup
8404 at this point in that case. */
8405 if (TREE_CODE (argument) != TYPE_DECL)
8406 argument = cp_parser_lookup_name (parser, argument,
8407 /*is_type=*/false,
8408 /*is_template=*/template_p,
8409 /*is_namespace=*/false,
8410 /*check_dependency=*/true);
8411 if (TREE_CODE (argument) != TEMPLATE_DECL
8412 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8413 cp_parser_error (parser, "expected template-name");
8415 if (cp_parser_parse_definitely (parser))
8416 return argument;
8417 /* It must be a non-type argument. There permitted cases are given
8418 in [temp.arg.nontype]:
8420 -- an integral constant-expression of integral or enumeration
8421 type; or
8423 -- the name of a non-type template-parameter; or
8425 -- the name of an object or function with external linkage...
8427 -- the address of an object or function with external linkage...
8429 -- a pointer to member... */
8430 /* Look for a non-type template parameter. */
8431 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8433 cp_parser_parse_tentatively (parser);
8434 argument = cp_parser_primary_expression (parser,
8435 &idk,
8436 &qualifying_class);
8437 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8438 || !cp_parser_next_token_ends_template_argument_p (parser))
8439 cp_parser_simulate_error (parser);
8440 if (cp_parser_parse_definitely (parser))
8441 return argument;
8443 /* If the next token is "&", the argument must be the address of an
8444 object or function with external linkage. */
8445 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8446 if (address_p)
8447 cp_lexer_consume_token (parser->lexer);
8448 /* See if we might have an id-expression. */
8449 token = cp_lexer_peek_token (parser->lexer);
8450 if (token->type == CPP_NAME
8451 || token->keyword == RID_OPERATOR
8452 || token->type == CPP_SCOPE
8453 || token->type == CPP_TEMPLATE_ID
8454 || token->type == CPP_NESTED_NAME_SPECIFIER)
8456 cp_parser_parse_tentatively (parser);
8457 argument = cp_parser_primary_expression (parser,
8458 &idk,
8459 &qualifying_class);
8460 if (cp_parser_error_occurred (parser)
8461 || !cp_parser_next_token_ends_template_argument_p (parser))
8462 cp_parser_abort_tentative_parse (parser);
8463 else
8465 if (qualifying_class)
8466 argument = finish_qualified_id_expr (qualifying_class,
8467 argument,
8468 /*done=*/true,
8469 address_p);
8470 if (TREE_CODE (argument) == VAR_DECL)
8472 /* A variable without external linkage might still be a
8473 valid constant-expression, so no error is issued here
8474 if the external-linkage check fails. */
8475 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8476 cp_parser_simulate_error (parser);
8478 else if (is_overloaded_fn (argument))
8479 /* All overloaded functions are allowed; if the external
8480 linkage test does not pass, an error will be issued
8481 later. */
8483 else if (address_p
8484 && (TREE_CODE (argument) == OFFSET_REF
8485 || TREE_CODE (argument) == SCOPE_REF))
8486 /* A pointer-to-member. */
8488 else
8489 cp_parser_simulate_error (parser);
8491 if (cp_parser_parse_definitely (parser))
8493 if (address_p)
8494 argument = build_x_unary_op (ADDR_EXPR, argument);
8495 return argument;
8499 /* If the argument started with "&", there are no other valid
8500 alternatives at this point. */
8501 if (address_p)
8503 cp_parser_error (parser, "invalid non-type template argument");
8504 return error_mark_node;
8506 /* If the argument wasn't successfully parsed as a type-id followed
8507 by '>>', the argument can only be a constant expression now.
8508 Otherwise, we try parsing the constant-expression tentatively,
8509 because the argument could really be a type-id. */
8510 if (maybe_type_id)
8511 cp_parser_parse_tentatively (parser);
8512 argument = cp_parser_constant_expression (parser,
8513 /*allow_non_constant_p=*/false,
8514 /*non_constant_p=*/NULL);
8515 argument = fold_non_dependent_expr (argument);
8516 if (!maybe_type_id)
8517 return argument;
8518 if (!cp_parser_next_token_ends_template_argument_p (parser))
8519 cp_parser_error (parser, "expected template-argument");
8520 if (cp_parser_parse_definitely (parser))
8521 return argument;
8522 /* We did our best to parse the argument as a non type-id, but that
8523 was the only alternative that matched (albeit with a '>' after
8524 it). We can assume it's just a typo from the user, and a
8525 diagnostic will then be issued. */
8526 return cp_parser_type_id (parser);
8529 /* Parse an explicit-instantiation.
8531 explicit-instantiation:
8532 template declaration
8534 Although the standard says `declaration', what it really means is:
8536 explicit-instantiation:
8537 template decl-specifier-seq [opt] declarator [opt] ;
8539 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8540 supposed to be allowed. A defect report has been filed about this
8541 issue.
8543 GNU Extension:
8545 explicit-instantiation:
8546 storage-class-specifier template
8547 decl-specifier-seq [opt] declarator [opt] ;
8548 function-specifier template
8549 decl-specifier-seq [opt] declarator [opt] ; */
8551 static void
8552 cp_parser_explicit_instantiation (cp_parser* parser)
8554 int declares_class_or_enum;
8555 tree decl_specifiers;
8556 tree attributes;
8557 tree extension_specifier = NULL_TREE;
8559 /* Look for an (optional) storage-class-specifier or
8560 function-specifier. */
8561 if (cp_parser_allow_gnu_extensions_p (parser))
8563 extension_specifier
8564 = cp_parser_storage_class_specifier_opt (parser);
8565 if (!extension_specifier)
8566 extension_specifier = cp_parser_function_specifier_opt (parser);
8569 /* Look for the `template' keyword. */
8570 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8571 /* Let the front end know that we are processing an explicit
8572 instantiation. */
8573 begin_explicit_instantiation ();
8574 /* [temp.explicit] says that we are supposed to ignore access
8575 control while processing explicit instantiation directives. */
8576 push_deferring_access_checks (dk_no_check);
8577 /* Parse a decl-specifier-seq. */
8578 decl_specifiers
8579 = cp_parser_decl_specifier_seq (parser,
8580 CP_PARSER_FLAGS_OPTIONAL,
8581 &attributes,
8582 &declares_class_or_enum);
8583 /* If there was exactly one decl-specifier, and it declared a class,
8584 and there's no declarator, then we have an explicit type
8585 instantiation. */
8586 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8588 tree type;
8590 type = check_tag_decl (decl_specifiers);
8591 /* Turn access control back on for names used during
8592 template instantiation. */
8593 pop_deferring_access_checks ();
8594 if (type)
8595 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8597 else
8599 tree declarator;
8600 tree decl;
8602 /* Parse the declarator. */
8603 declarator
8604 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8605 /*ctor_dtor_or_conv_p=*/NULL,
8606 /*parenthesized_p=*/NULL,
8607 /*member_p=*/false);
8608 if (declares_class_or_enum & 2)
8609 cp_parser_check_for_definition_in_return_type
8610 (declarator, TREE_VALUE (decl_specifiers));
8611 if (declarator != error_mark_node)
8613 decl = grokdeclarator (declarator, decl_specifiers,
8614 NORMAL, 0, NULL);
8615 /* Turn access control back on for names used during
8616 template instantiation. */
8617 pop_deferring_access_checks ();
8618 /* Do the explicit instantiation. */
8619 do_decl_instantiation (decl, extension_specifier);
8621 else
8623 pop_deferring_access_checks ();
8624 /* Skip the body of the explicit instantiation. */
8625 cp_parser_skip_to_end_of_statement (parser);
8628 /* We're done with the instantiation. */
8629 end_explicit_instantiation ();
8631 cp_parser_consume_semicolon_at_end_of_statement (parser);
8634 /* Parse an explicit-specialization.
8636 explicit-specialization:
8637 template < > declaration
8639 Although the standard says `declaration', what it really means is:
8641 explicit-specialization:
8642 template <> decl-specifier [opt] init-declarator [opt] ;
8643 template <> function-definition
8644 template <> explicit-specialization
8645 template <> template-declaration */
8647 static void
8648 cp_parser_explicit_specialization (cp_parser* parser)
8650 /* Look for the `template' keyword. */
8651 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8652 /* Look for the `<'. */
8653 cp_parser_require (parser, CPP_LESS, "`<'");
8654 /* Look for the `>'. */
8655 cp_parser_require (parser, CPP_GREATER, "`>'");
8656 /* We have processed another parameter list. */
8657 ++parser->num_template_parameter_lists;
8658 /* Let the front end know that we are beginning a specialization. */
8659 begin_specialization ();
8661 /* If the next keyword is `template', we need to figure out whether
8662 or not we're looking a template-declaration. */
8663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8665 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8666 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8667 cp_parser_template_declaration_after_export (parser,
8668 /*member_p=*/false);
8669 else
8670 cp_parser_explicit_specialization (parser);
8672 else
8673 /* Parse the dependent declaration. */
8674 cp_parser_single_declaration (parser,
8675 /*member_p=*/false,
8676 /*friend_p=*/NULL);
8678 /* We're done with the specialization. */
8679 end_specialization ();
8680 /* We're done with this parameter list. */
8681 --parser->num_template_parameter_lists;
8684 /* Parse a type-specifier.
8686 type-specifier:
8687 simple-type-specifier
8688 class-specifier
8689 enum-specifier
8690 elaborated-type-specifier
8691 cv-qualifier
8693 GNU Extension:
8695 type-specifier:
8696 __complex__
8698 Returns a representation of the type-specifier. If the
8699 type-specifier is a keyword (like `int' or `const', or
8700 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8701 For a class-specifier, enum-specifier, or elaborated-type-specifier
8702 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8704 If IS_FRIEND is TRUE then this type-specifier is being declared a
8705 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8706 appearing in a decl-specifier-seq.
8708 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8709 class-specifier, enum-specifier, or elaborated-type-specifier, then
8710 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8711 if a type is declared; 2 if it is defined. Otherwise, it is set to
8712 zero.
8714 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8715 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8716 is set to FALSE. */
8718 static tree
8719 cp_parser_type_specifier (cp_parser* parser,
8720 cp_parser_flags flags,
8721 bool is_friend,
8722 bool is_declaration,
8723 int* declares_class_or_enum,
8724 bool* is_cv_qualifier)
8726 tree type_spec = NULL_TREE;
8727 cp_token *token;
8728 enum rid keyword;
8730 /* Assume this type-specifier does not declare a new type. */
8731 if (declares_class_or_enum)
8732 *declares_class_or_enum = 0;
8733 /* And that it does not specify a cv-qualifier. */
8734 if (is_cv_qualifier)
8735 *is_cv_qualifier = false;
8736 /* Peek at the next token. */
8737 token = cp_lexer_peek_token (parser->lexer);
8739 /* If we're looking at a keyword, we can use that to guide the
8740 production we choose. */
8741 keyword = token->keyword;
8742 switch (keyword)
8744 case RID_ENUM:
8745 /* 'enum' [identifier] '{' introduces an enum-specifier;
8746 'enum' <anything else> introduces an elaborated-type-specifier. */
8747 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
8748 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
8749 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
8750 == CPP_OPEN_BRACE))
8752 if (parser->num_template_parameter_lists)
8754 error ("template declaration of `enum'");
8755 cp_parser_skip_to_end_of_block_or_statement (parser);
8756 type_spec = error_mark_node;
8758 else
8759 type_spec = cp_parser_enum_specifier (parser);
8761 if (declares_class_or_enum)
8762 *declares_class_or_enum = 2;
8763 return type_spec;
8765 else
8766 goto elaborated_type_specifier;
8768 /* Any of these indicate either a class-specifier, or an
8769 elaborated-type-specifier. */
8770 case RID_CLASS:
8771 case RID_STRUCT:
8772 case RID_UNION:
8773 /* Parse tentatively so that we can back up if we don't find a
8774 class-specifier or enum-specifier. */
8775 cp_parser_parse_tentatively (parser);
8776 /* Look for the class-specifier. */
8777 type_spec = cp_parser_class_specifier (parser);
8778 /* If that worked, we're done. */
8779 if (cp_parser_parse_definitely (parser))
8781 if (declares_class_or_enum)
8782 *declares_class_or_enum = 2;
8783 return type_spec;
8786 /* Fall through. */
8788 case RID_TYPENAME:
8789 elaborated_type_specifier:
8790 /* Look for an elaborated-type-specifier. */
8791 type_spec = cp_parser_elaborated_type_specifier (parser,
8792 is_friend,
8793 is_declaration);
8794 /* We're declaring a class or enum -- unless we're using
8795 `typename'. */
8796 if (declares_class_or_enum && keyword != RID_TYPENAME)
8797 *declares_class_or_enum = 1;
8798 return type_spec;
8800 case RID_CONST:
8801 case RID_VOLATILE:
8802 case RID_RESTRICT:
8803 type_spec = cp_parser_cv_qualifier_opt (parser);
8804 /* Even though we call a routine that looks for an optional
8805 qualifier, we know that there should be one. */
8806 my_friendly_assert (type_spec != NULL, 20000328);
8807 /* This type-specifier was a cv-qualified. */
8808 if (is_cv_qualifier)
8809 *is_cv_qualifier = true;
8811 return type_spec;
8813 case RID_COMPLEX:
8814 /* The `__complex__' keyword is a GNU extension. */
8815 return cp_lexer_consume_token (parser->lexer)->value;
8817 default:
8818 break;
8821 /* If we do not already have a type-specifier, assume we are looking
8822 at a simple-type-specifier. */
8823 type_spec = cp_parser_simple_type_specifier (parser, flags,
8824 /*identifier_p=*/true);
8826 /* If we didn't find a type-specifier, and a type-specifier was not
8827 optional in this context, issue an error message. */
8828 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8830 cp_parser_error (parser, "expected type specifier");
8831 return error_mark_node;
8834 return type_spec;
8837 /* Parse a simple-type-specifier.
8839 simple-type-specifier:
8840 :: [opt] nested-name-specifier [opt] type-name
8841 :: [opt] nested-name-specifier template template-id
8842 char
8843 wchar_t
8844 bool
8845 short
8847 long
8848 signed
8849 unsigned
8850 float
8851 double
8852 void
8854 GNU Extension:
8856 simple-type-specifier:
8857 __typeof__ unary-expression
8858 __typeof__ ( type-id )
8860 For the various keywords, the value returned is simply the
8861 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8862 For the first two productions, and if IDENTIFIER_P is false, the
8863 value returned is the indicated TYPE_DECL. */
8865 static tree
8866 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8867 bool identifier_p)
8869 tree type = NULL_TREE;
8870 cp_token *token;
8872 /* Peek at the next token. */
8873 token = cp_lexer_peek_token (parser->lexer);
8875 /* If we're looking at a keyword, things are easy. */
8876 switch (token->keyword)
8878 case RID_CHAR:
8879 type = char_type_node;
8880 break;
8881 case RID_WCHAR:
8882 type = wchar_type_node;
8883 break;
8884 case RID_BOOL:
8885 type = boolean_type_node;
8886 break;
8887 case RID_SHORT:
8888 type = short_integer_type_node;
8889 break;
8890 case RID_INT:
8891 type = integer_type_node;
8892 break;
8893 case RID_LONG:
8894 type = long_integer_type_node;
8895 break;
8896 case RID_SIGNED:
8897 type = integer_type_node;
8898 break;
8899 case RID_UNSIGNED:
8900 type = unsigned_type_node;
8901 break;
8902 case RID_FLOAT:
8903 type = float_type_node;
8904 break;
8905 case RID_DOUBLE:
8906 type = double_type_node;
8907 break;
8908 case RID_VOID:
8909 type = void_type_node;
8910 break;
8912 case RID_TYPEOF:
8914 tree operand;
8916 /* Consume the `typeof' token. */
8917 cp_lexer_consume_token (parser->lexer);
8918 /* Parse the operand to `typeof'. */
8919 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8920 /* If it is not already a TYPE, take its type. */
8921 if (!TYPE_P (operand))
8922 operand = finish_typeof (operand);
8924 return operand;
8927 default:
8928 break;
8931 /* If the type-specifier was for a built-in type, we're done. */
8932 if (type)
8934 tree id;
8936 /* Consume the token. */
8937 id = cp_lexer_consume_token (parser->lexer)->value;
8939 /* There is no valid C++ program where a non-template type is
8940 followed by a "<". That usually indicates that the user thought
8941 that the type was a template. */
8942 cp_parser_check_for_invalid_template_id (parser, type);
8944 return identifier_p ? id : TYPE_NAME (type);
8947 /* The type-specifier must be a user-defined type. */
8948 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8950 bool qualified_p;
8951 bool global_p;
8953 /* Don't gobble tokens or issue error messages if this is an
8954 optional type-specifier. */
8955 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8956 cp_parser_parse_tentatively (parser);
8958 /* Look for the optional `::' operator. */
8959 global_p
8960 = (cp_parser_global_scope_opt (parser,
8961 /*current_scope_valid_p=*/false)
8962 != NULL_TREE);
8963 /* Look for the nested-name specifier. */
8964 qualified_p
8965 = (cp_parser_nested_name_specifier_opt (parser,
8966 /*typename_keyword_p=*/false,
8967 /*check_dependency_p=*/true,
8968 /*type_p=*/false,
8969 /*is_declaration=*/false)
8970 != NULL_TREE);
8971 /* If we have seen a nested-name-specifier, and the next token
8972 is `template', then we are using the template-id production. */
8973 if (parser->scope
8974 && cp_parser_optional_template_keyword (parser))
8976 /* Look for the template-id. */
8977 type = cp_parser_template_id (parser,
8978 /*template_keyword_p=*/true,
8979 /*check_dependency_p=*/true,
8980 /*is_declaration=*/false);
8981 /* If the template-id did not name a type, we are out of
8982 luck. */
8983 if (TREE_CODE (type) != TYPE_DECL)
8985 cp_parser_error (parser, "expected template-id for type");
8986 type = NULL_TREE;
8989 /* Otherwise, look for a type-name. */
8990 else
8991 type = cp_parser_type_name (parser);
8992 /* Keep track of all name-lookups performed in class scopes. */
8993 if (type
8994 && !global_p
8995 && !qualified_p
8996 && TREE_CODE (type) == TYPE_DECL
8997 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
8998 maybe_note_name_used_in_class (DECL_NAME (type), type);
8999 /* If it didn't work out, we don't have a TYPE. */
9000 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9001 && !cp_parser_parse_definitely (parser))
9002 type = NULL_TREE;
9005 /* If we didn't get a type-name, issue an error message. */
9006 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9008 cp_parser_error (parser, "expected type-name");
9009 return error_mark_node;
9012 /* There is no valid C++ program where a non-template type is
9013 followed by a "<". That usually indicates that the user thought
9014 that the type was a template. */
9015 if (type && type != error_mark_node)
9016 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9018 return type;
9021 /* Parse a type-name.
9023 type-name:
9024 class-name
9025 enum-name
9026 typedef-name
9028 enum-name:
9029 identifier
9031 typedef-name:
9032 identifier
9034 Returns a TYPE_DECL for the the type. */
9036 static tree
9037 cp_parser_type_name (cp_parser* parser)
9039 tree type_decl;
9040 tree identifier;
9042 /* We can't know yet whether it is a class-name or not. */
9043 cp_parser_parse_tentatively (parser);
9044 /* Try a class-name. */
9045 type_decl = cp_parser_class_name (parser,
9046 /*typename_keyword_p=*/false,
9047 /*template_keyword_p=*/false,
9048 /*type_p=*/false,
9049 /*check_dependency_p=*/true,
9050 /*class_head_p=*/false,
9051 /*is_declaration=*/false);
9052 /* If it's not a class-name, keep looking. */
9053 if (!cp_parser_parse_definitely (parser))
9055 /* It must be a typedef-name or an enum-name. */
9056 identifier = cp_parser_identifier (parser);
9057 if (identifier == error_mark_node)
9058 return error_mark_node;
9060 /* Look up the type-name. */
9061 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9062 /* Issue an error if we did not find a type-name. */
9063 if (TREE_CODE (type_decl) != TYPE_DECL)
9065 if (!cp_parser_simulate_error (parser))
9066 cp_parser_name_lookup_error (parser, identifier, type_decl,
9067 "is not a type");
9068 type_decl = error_mark_node;
9070 /* Remember that the name was used in the definition of the
9071 current class so that we can check later to see if the
9072 meaning would have been different after the class was
9073 entirely defined. */
9074 else if (type_decl != error_mark_node
9075 && !parser->scope)
9076 maybe_note_name_used_in_class (identifier, type_decl);
9079 return type_decl;
9083 /* Parse an elaborated-type-specifier. Note that the grammar given
9084 here incorporates the resolution to DR68.
9086 elaborated-type-specifier:
9087 class-key :: [opt] nested-name-specifier [opt] identifier
9088 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9089 enum :: [opt] nested-name-specifier [opt] identifier
9090 typename :: [opt] nested-name-specifier identifier
9091 typename :: [opt] nested-name-specifier template [opt]
9092 template-id
9094 GNU extension:
9096 elaborated-type-specifier:
9097 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9098 class-key attributes :: [opt] nested-name-specifier [opt]
9099 template [opt] template-id
9100 enum attributes :: [opt] nested-name-specifier [opt] identifier
9102 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9103 declared `friend'. If IS_DECLARATION is TRUE, then this
9104 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9105 something is being declared.
9107 Returns the TYPE specified. */
9109 static tree
9110 cp_parser_elaborated_type_specifier (cp_parser* parser,
9111 bool is_friend,
9112 bool is_declaration)
9114 enum tag_types tag_type;
9115 tree identifier;
9116 tree type = NULL_TREE;
9117 tree attributes = NULL_TREE;
9119 /* See if we're looking at the `enum' keyword. */
9120 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9122 /* Consume the `enum' token. */
9123 cp_lexer_consume_token (parser->lexer);
9124 /* Remember that it's an enumeration type. */
9125 tag_type = enum_type;
9126 /* Parse the attributes. */
9127 attributes = cp_parser_attributes_opt (parser);
9129 /* Or, it might be `typename'. */
9130 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9131 RID_TYPENAME))
9133 /* Consume the `typename' token. */
9134 cp_lexer_consume_token (parser->lexer);
9135 /* Remember that it's a `typename' type. */
9136 tag_type = typename_type;
9137 /* The `typename' keyword is only allowed in templates. */
9138 if (!processing_template_decl)
9139 pedwarn ("using `typename' outside of template");
9141 /* Otherwise it must be a class-key. */
9142 else
9144 tag_type = cp_parser_class_key (parser);
9145 if (tag_type == none_type)
9146 return error_mark_node;
9147 /* Parse the attributes. */
9148 attributes = cp_parser_attributes_opt (parser);
9151 /* Look for the `::' operator. */
9152 cp_parser_global_scope_opt (parser,
9153 /*current_scope_valid_p=*/false);
9154 /* Look for the nested-name-specifier. */
9155 if (tag_type == typename_type)
9157 if (cp_parser_nested_name_specifier (parser,
9158 /*typename_keyword_p=*/true,
9159 /*check_dependency_p=*/true,
9160 /*type_p=*/true,
9161 is_declaration)
9162 == error_mark_node)
9163 return error_mark_node;
9165 else
9166 /* Even though `typename' is not present, the proposed resolution
9167 to Core Issue 180 says that in `class A<T>::B', `B' should be
9168 considered a type-name, even if `A<T>' is dependent. */
9169 cp_parser_nested_name_specifier_opt (parser,
9170 /*typename_keyword_p=*/true,
9171 /*check_dependency_p=*/true,
9172 /*type_p=*/true,
9173 is_declaration);
9174 /* For everything but enumeration types, consider a template-id. */
9175 if (tag_type != enum_type)
9177 bool template_p = false;
9178 tree decl;
9180 /* Allow the `template' keyword. */
9181 template_p = cp_parser_optional_template_keyword (parser);
9182 /* If we didn't see `template', we don't know if there's a
9183 template-id or not. */
9184 if (!template_p)
9185 cp_parser_parse_tentatively (parser);
9186 /* Parse the template-id. */
9187 decl = cp_parser_template_id (parser, template_p,
9188 /*check_dependency_p=*/true,
9189 is_declaration);
9190 /* If we didn't find a template-id, look for an ordinary
9191 identifier. */
9192 if (!template_p && !cp_parser_parse_definitely (parser))
9194 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9195 in effect, then we must assume that, upon instantiation, the
9196 template will correspond to a class. */
9197 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9198 && tag_type == typename_type)
9199 type = make_typename_type (parser->scope, decl,
9200 /*complain=*/1);
9201 else
9202 type = TREE_TYPE (decl);
9205 /* For an enumeration type, consider only a plain identifier. */
9206 if (!type)
9208 identifier = cp_parser_identifier (parser);
9210 if (identifier == error_mark_node)
9212 parser->scope = NULL_TREE;
9213 return error_mark_node;
9216 /* For a `typename', we needn't call xref_tag. */
9217 if (tag_type == typename_type
9218 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9219 return make_typename_type (parser->scope, identifier,
9220 /*complain=*/1);
9221 /* Look up a qualified name in the usual way. */
9222 if (parser->scope)
9224 tree decl;
9226 /* In an elaborated-type-specifier, names are assumed to name
9227 types, so we set IS_TYPE to TRUE when calling
9228 cp_parser_lookup_name. */
9229 decl = cp_parser_lookup_name (parser, identifier,
9230 /*is_type=*/true,
9231 /*is_template=*/false,
9232 /*is_namespace=*/false,
9233 /*check_dependency=*/true);
9235 /* If we are parsing friend declaration, DECL may be a
9236 TEMPLATE_DECL tree node here. However, we need to check
9237 whether this TEMPLATE_DECL results in valid code. Consider
9238 the following example:
9240 namespace N {
9241 template <class T> class C {};
9243 class X {
9244 template <class T> friend class N::C; // #1, valid code
9246 template <class T> class Y {
9247 friend class N::C; // #2, invalid code
9250 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9251 name lookup of `N::C'. We see that friend declaration must
9252 be template for the code to be valid. Note that
9253 processing_template_decl does not work here since it is
9254 always 1 for the above two cases. */
9256 decl = (cp_parser_maybe_treat_template_as_class
9257 (decl, /*tag_name_p=*/is_friend
9258 && parser->num_template_parameter_lists));
9260 if (TREE_CODE (decl) != TYPE_DECL)
9262 cp_parser_diagnose_invalid_type_name (parser);
9263 return error_mark_node;
9266 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9267 check_elaborated_type_specifier
9268 (tag_type, decl,
9269 (parser->num_template_parameter_lists
9270 || DECL_SELF_REFERENCE_P (decl)));
9272 type = TREE_TYPE (decl);
9274 else
9276 /* An elaborated-type-specifier sometimes introduces a new type and
9277 sometimes names an existing type. Normally, the rule is that it
9278 introduces a new type only if there is not an existing type of
9279 the same name already in scope. For example, given:
9281 struct S {};
9282 void f() { struct S s; }
9284 the `struct S' in the body of `f' is the same `struct S' as in
9285 the global scope; the existing definition is used. However, if
9286 there were no global declaration, this would introduce a new
9287 local class named `S'.
9289 An exception to this rule applies to the following code:
9291 namespace N { struct S; }
9293 Here, the elaborated-type-specifier names a new type
9294 unconditionally; even if there is already an `S' in the
9295 containing scope this declaration names a new type.
9296 This exception only applies if the elaborated-type-specifier
9297 forms the complete declaration:
9299 [class.name]
9301 A declaration consisting solely of `class-key identifier ;' is
9302 either a redeclaration of the name in the current scope or a
9303 forward declaration of the identifier as a class name. It
9304 introduces the name into the current scope.
9306 We are in this situation precisely when the next token is a `;'.
9308 An exception to the exception is that a `friend' declaration does
9309 *not* name a new type; i.e., given:
9311 struct S { friend struct T; };
9313 `T' is not a new type in the scope of `S'.
9315 Also, `new struct S' or `sizeof (struct S)' never results in the
9316 definition of a new type; a new type can only be declared in a
9317 declaration context. */
9319 /* Warn about attributes. They are ignored. */
9320 if (attributes)
9321 warning ("type attributes are honored only at type definition");
9323 type = xref_tag (tag_type, identifier,
9324 (is_friend
9325 || !is_declaration
9326 || cp_lexer_next_token_is_not (parser->lexer,
9327 CPP_SEMICOLON)),
9328 parser->num_template_parameter_lists);
9331 if (tag_type != enum_type)
9332 cp_parser_check_class_key (tag_type, type);
9334 /* A "<" cannot follow an elaborated type specifier. If that
9335 happens, the user was probably trying to form a template-id. */
9336 cp_parser_check_for_invalid_template_id (parser, type);
9338 return type;
9341 /* Parse an enum-specifier.
9343 enum-specifier:
9344 enum identifier [opt] { enumerator-list [opt] }
9346 Returns an ENUM_TYPE representing the enumeration. */
9348 static tree
9349 cp_parser_enum_specifier (cp_parser* parser)
9351 cp_token *token;
9352 tree identifier = NULL_TREE;
9353 tree type;
9355 /* Look for the `enum' keyword. */
9356 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9357 return error_mark_node;
9358 /* Peek at the next token. */
9359 token = cp_lexer_peek_token (parser->lexer);
9361 /* See if it is an identifier. */
9362 if (token->type == CPP_NAME)
9363 identifier = cp_parser_identifier (parser);
9365 /* Look for the `{'. */
9366 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9367 return error_mark_node;
9369 /* At this point, we're going ahead with the enum-specifier, even
9370 if some other problem occurs. */
9371 cp_parser_commit_to_tentative_parse (parser);
9373 /* Issue an error message if type-definitions are forbidden here. */
9374 cp_parser_check_type_definition (parser);
9376 /* Create the new type. */
9377 type = start_enum (identifier ? identifier : make_anon_name ());
9379 /* Peek at the next token. */
9380 token = cp_lexer_peek_token (parser->lexer);
9381 /* If it's not a `}', then there are some enumerators. */
9382 if (token->type != CPP_CLOSE_BRACE)
9383 cp_parser_enumerator_list (parser, type);
9384 /* Look for the `}'. */
9385 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9387 /* Finish up the enumeration. */
9388 finish_enum (type);
9390 return type;
9393 /* Parse an enumerator-list. The enumerators all have the indicated
9394 TYPE.
9396 enumerator-list:
9397 enumerator-definition
9398 enumerator-list , enumerator-definition */
9400 static void
9401 cp_parser_enumerator_list (cp_parser* parser, tree type)
9403 while (true)
9405 cp_token *token;
9407 /* Parse an enumerator-definition. */
9408 cp_parser_enumerator_definition (parser, type);
9409 /* Peek at the next token. */
9410 token = cp_lexer_peek_token (parser->lexer);
9411 /* If it's not a `,', then we've reached the end of the
9412 list. */
9413 if (token->type != CPP_COMMA)
9414 break;
9415 /* Otherwise, consume the `,' and keep going. */
9416 cp_lexer_consume_token (parser->lexer);
9417 /* If the next token is a `}', there is a trailing comma. */
9418 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9420 if (pedantic && !in_system_header)
9421 pedwarn ("comma at end of enumerator list");
9422 break;
9427 /* Parse an enumerator-definition. The enumerator has the indicated
9428 TYPE.
9430 enumerator-definition:
9431 enumerator
9432 enumerator = constant-expression
9434 enumerator:
9435 identifier */
9437 static void
9438 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9440 cp_token *token;
9441 tree identifier;
9442 tree value;
9444 /* Look for the identifier. */
9445 identifier = cp_parser_identifier (parser);
9446 if (identifier == error_mark_node)
9447 return;
9449 /* Peek at the next token. */
9450 token = cp_lexer_peek_token (parser->lexer);
9451 /* If it's an `=', then there's an explicit value. */
9452 if (token->type == CPP_EQ)
9454 /* Consume the `=' token. */
9455 cp_lexer_consume_token (parser->lexer);
9456 /* Parse the value. */
9457 value = cp_parser_constant_expression (parser,
9458 /*allow_non_constant_p=*/false,
9459 NULL);
9461 else
9462 value = NULL_TREE;
9464 /* Create the enumerator. */
9465 build_enumerator (identifier, value, type);
9468 /* Parse a namespace-name.
9470 namespace-name:
9471 original-namespace-name
9472 namespace-alias
9474 Returns the NAMESPACE_DECL for the namespace. */
9476 static tree
9477 cp_parser_namespace_name (cp_parser* parser)
9479 tree identifier;
9480 tree namespace_decl;
9482 /* Get the name of the namespace. */
9483 identifier = cp_parser_identifier (parser);
9484 if (identifier == error_mark_node)
9485 return error_mark_node;
9487 /* Look up the identifier in the currently active scope. Look only
9488 for namespaces, due to:
9490 [basic.lookup.udir]
9492 When looking up a namespace-name in a using-directive or alias
9493 definition, only namespace names are considered.
9495 And:
9497 [basic.lookup.qual]
9499 During the lookup of a name preceding the :: scope resolution
9500 operator, object, function, and enumerator names are ignored.
9502 (Note that cp_parser_class_or_namespace_name only calls this
9503 function if the token after the name is the scope resolution
9504 operator.) */
9505 namespace_decl = cp_parser_lookup_name (parser, identifier,
9506 /*is_type=*/false,
9507 /*is_template=*/false,
9508 /*is_namespace=*/true,
9509 /*check_dependency=*/true);
9510 /* If it's not a namespace, issue an error. */
9511 if (namespace_decl == error_mark_node
9512 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9514 if (!cp_parser_parsing_tentatively (parser)
9515 || cp_parser_committed_to_tentative_parse (parser))
9516 error ("`%D' is not a namespace-name", identifier);
9517 cp_parser_error (parser, "expected namespace-name");
9518 namespace_decl = error_mark_node;
9521 return namespace_decl;
9524 /* Parse a namespace-definition.
9526 namespace-definition:
9527 named-namespace-definition
9528 unnamed-namespace-definition
9530 named-namespace-definition:
9531 original-namespace-definition
9532 extension-namespace-definition
9534 original-namespace-definition:
9535 namespace identifier { namespace-body }
9537 extension-namespace-definition:
9538 namespace original-namespace-name { namespace-body }
9540 unnamed-namespace-definition:
9541 namespace { namespace-body } */
9543 static void
9544 cp_parser_namespace_definition (cp_parser* parser)
9546 tree identifier;
9548 /* Look for the `namespace' keyword. */
9549 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9551 /* Get the name of the namespace. We do not attempt to distinguish
9552 between an original-namespace-definition and an
9553 extension-namespace-definition at this point. The semantic
9554 analysis routines are responsible for that. */
9555 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9556 identifier = cp_parser_identifier (parser);
9557 else
9558 identifier = NULL_TREE;
9560 /* Look for the `{' to start the namespace. */
9561 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9562 /* Start the namespace. */
9563 push_namespace (identifier);
9564 /* Parse the body of the namespace. */
9565 cp_parser_namespace_body (parser);
9566 /* Finish the namespace. */
9567 pop_namespace ();
9568 /* Look for the final `}'. */
9569 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9572 /* Parse a namespace-body.
9574 namespace-body:
9575 declaration-seq [opt] */
9577 static void
9578 cp_parser_namespace_body (cp_parser* parser)
9580 cp_parser_declaration_seq_opt (parser);
9583 /* Parse a namespace-alias-definition.
9585 namespace-alias-definition:
9586 namespace identifier = qualified-namespace-specifier ; */
9588 static void
9589 cp_parser_namespace_alias_definition (cp_parser* parser)
9591 tree identifier;
9592 tree namespace_specifier;
9594 /* Look for the `namespace' keyword. */
9595 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9596 /* Look for the identifier. */
9597 identifier = cp_parser_identifier (parser);
9598 if (identifier == error_mark_node)
9599 return;
9600 /* Look for the `=' token. */
9601 cp_parser_require (parser, CPP_EQ, "`='");
9602 /* Look for the qualified-namespace-specifier. */
9603 namespace_specifier
9604 = cp_parser_qualified_namespace_specifier (parser);
9605 /* Look for the `;' token. */
9606 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9608 /* Register the alias in the symbol table. */
9609 do_namespace_alias (identifier, namespace_specifier);
9612 /* Parse a qualified-namespace-specifier.
9614 qualified-namespace-specifier:
9615 :: [opt] nested-name-specifier [opt] namespace-name
9617 Returns a NAMESPACE_DECL corresponding to the specified
9618 namespace. */
9620 static tree
9621 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9623 /* Look for the optional `::'. */
9624 cp_parser_global_scope_opt (parser,
9625 /*current_scope_valid_p=*/false);
9627 /* Look for the optional nested-name-specifier. */
9628 cp_parser_nested_name_specifier_opt (parser,
9629 /*typename_keyword_p=*/false,
9630 /*check_dependency_p=*/true,
9631 /*type_p=*/false,
9632 /*is_declaration=*/true);
9634 return cp_parser_namespace_name (parser);
9637 /* Parse a using-declaration.
9639 using-declaration:
9640 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9641 using :: unqualified-id ; */
9643 static void
9644 cp_parser_using_declaration (cp_parser* parser)
9646 cp_token *token;
9647 bool typename_p = false;
9648 bool global_scope_p;
9649 tree decl;
9650 tree identifier;
9651 tree scope;
9652 tree qscope;
9654 /* Look for the `using' keyword. */
9655 cp_parser_require_keyword (parser, RID_USING, "`using'");
9657 /* Peek at the next token. */
9658 token = cp_lexer_peek_token (parser->lexer);
9659 /* See if it's `typename'. */
9660 if (token->keyword == RID_TYPENAME)
9662 /* Remember that we've seen it. */
9663 typename_p = true;
9664 /* Consume the `typename' token. */
9665 cp_lexer_consume_token (parser->lexer);
9668 /* Look for the optional global scope qualification. */
9669 global_scope_p
9670 = (cp_parser_global_scope_opt (parser,
9671 /*current_scope_valid_p=*/false)
9672 != NULL_TREE);
9674 /* If we saw `typename', or didn't see `::', then there must be a
9675 nested-name-specifier present. */
9676 if (typename_p || !global_scope_p)
9677 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9678 /*check_dependency_p=*/true,
9679 /*type_p=*/false,
9680 /*is_declaration=*/true);
9681 /* Otherwise, we could be in either of the two productions. In that
9682 case, treat the nested-name-specifier as optional. */
9683 else
9684 qscope = cp_parser_nested_name_specifier_opt (parser,
9685 /*typename_keyword_p=*/false,
9686 /*check_dependency_p=*/true,
9687 /*type_p=*/false,
9688 /*is_declaration=*/true);
9689 if (!qscope)
9690 qscope = global_namespace;
9692 /* Parse the unqualified-id. */
9693 identifier = cp_parser_unqualified_id (parser,
9694 /*template_keyword_p=*/false,
9695 /*check_dependency_p=*/true,
9696 /*declarator_p=*/true);
9698 /* The function we call to handle a using-declaration is different
9699 depending on what scope we are in. */
9700 if (identifier == error_mark_node)
9702 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9703 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9704 /* [namespace.udecl]
9706 A using declaration shall not name a template-id. */
9707 error ("a template-id may not appear in a using-declaration");
9708 else
9710 scope = current_scope ();
9711 if (scope && TYPE_P (scope))
9713 /* Create the USING_DECL. */
9714 decl = do_class_using_decl (build_nt (SCOPE_REF,
9715 parser->scope,
9716 identifier));
9717 /* Add it to the list of members in this class. */
9718 finish_member_declaration (decl);
9720 else
9722 decl = cp_parser_lookup_name_simple (parser, identifier);
9723 if (decl == error_mark_node)
9724 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9725 else if (scope)
9726 do_local_using_decl (decl, qscope, identifier);
9727 else
9728 do_toplevel_using_decl (decl, qscope, identifier);
9732 /* Look for the final `;'. */
9733 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9736 /* Parse a using-directive.
9738 using-directive:
9739 using namespace :: [opt] nested-name-specifier [opt]
9740 namespace-name ; */
9742 static void
9743 cp_parser_using_directive (cp_parser* parser)
9745 tree namespace_decl;
9746 tree attribs;
9748 /* Look for the `using' keyword. */
9749 cp_parser_require_keyword (parser, RID_USING, "`using'");
9750 /* And the `namespace' keyword. */
9751 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9752 /* Look for the optional `::' operator. */
9753 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9754 /* And the optional nested-name-specifier. */
9755 cp_parser_nested_name_specifier_opt (parser,
9756 /*typename_keyword_p=*/false,
9757 /*check_dependency_p=*/true,
9758 /*type_p=*/false,
9759 /*is_declaration=*/true);
9760 /* Get the namespace being used. */
9761 namespace_decl = cp_parser_namespace_name (parser);
9762 /* And any specified attributes. */
9763 attribs = cp_parser_attributes_opt (parser);
9764 /* Update the symbol table. */
9765 parse_using_directive (namespace_decl, attribs);
9766 /* Look for the final `;'. */
9767 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9770 /* Parse an asm-definition.
9772 asm-definition:
9773 asm ( string-literal ) ;
9775 GNU Extension:
9777 asm-definition:
9778 asm volatile [opt] ( string-literal ) ;
9779 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9780 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9781 : asm-operand-list [opt] ) ;
9782 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9783 : asm-operand-list [opt]
9784 : asm-operand-list [opt] ) ; */
9786 static void
9787 cp_parser_asm_definition (cp_parser* parser)
9789 cp_token *token;
9790 tree string;
9791 tree outputs = NULL_TREE;
9792 tree inputs = NULL_TREE;
9793 tree clobbers = NULL_TREE;
9794 tree asm_stmt;
9795 bool volatile_p = false;
9796 bool extended_p = false;
9798 /* Look for the `asm' keyword. */
9799 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9800 /* See if the next token is `volatile'. */
9801 if (cp_parser_allow_gnu_extensions_p (parser)
9802 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9804 /* Remember that we saw the `volatile' keyword. */
9805 volatile_p = true;
9806 /* Consume the token. */
9807 cp_lexer_consume_token (parser->lexer);
9809 /* Look for the opening `('. */
9810 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9811 /* Look for the string. */
9812 token = cp_parser_require (parser, CPP_STRING, "asm body");
9813 if (!token)
9814 return;
9815 string = token->value;
9816 /* If we're allowing GNU extensions, check for the extended assembly
9817 syntax. Unfortunately, the `:' tokens need not be separated by
9818 a space in C, and so, for compatibility, we tolerate that here
9819 too. Doing that means that we have to treat the `::' operator as
9820 two `:' tokens. */
9821 if (cp_parser_allow_gnu_extensions_p (parser)
9822 && at_function_scope_p ()
9823 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9824 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9826 bool inputs_p = false;
9827 bool clobbers_p = false;
9829 /* The extended syntax was used. */
9830 extended_p = true;
9832 /* Look for outputs. */
9833 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9835 /* Consume the `:'. */
9836 cp_lexer_consume_token (parser->lexer);
9837 /* Parse the output-operands. */
9838 if (cp_lexer_next_token_is_not (parser->lexer,
9839 CPP_COLON)
9840 && cp_lexer_next_token_is_not (parser->lexer,
9841 CPP_SCOPE)
9842 && cp_lexer_next_token_is_not (parser->lexer,
9843 CPP_CLOSE_PAREN))
9844 outputs = cp_parser_asm_operand_list (parser);
9846 /* If the next token is `::', there are no outputs, and the
9847 next token is the beginning of the inputs. */
9848 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9849 /* The inputs are coming next. */
9850 inputs_p = true;
9852 /* Look for inputs. */
9853 if (inputs_p
9854 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9856 /* Consume the `:' or `::'. */
9857 cp_lexer_consume_token (parser->lexer);
9858 /* Parse the output-operands. */
9859 if (cp_lexer_next_token_is_not (parser->lexer,
9860 CPP_COLON)
9861 && cp_lexer_next_token_is_not (parser->lexer,
9862 CPP_CLOSE_PAREN))
9863 inputs = cp_parser_asm_operand_list (parser);
9865 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9866 /* The clobbers are coming next. */
9867 clobbers_p = true;
9869 /* Look for clobbers. */
9870 if (clobbers_p
9871 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9873 /* Consume the `:' or `::'. */
9874 cp_lexer_consume_token (parser->lexer);
9875 /* Parse the clobbers. */
9876 if (cp_lexer_next_token_is_not (parser->lexer,
9877 CPP_CLOSE_PAREN))
9878 clobbers = cp_parser_asm_clobber_list (parser);
9881 /* Look for the closing `)'. */
9882 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9883 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9884 /*consume_paren=*/true);
9885 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9887 /* Create the ASM_STMT. */
9888 if (at_function_scope_p ())
9890 asm_stmt =
9891 finish_asm_stmt (volatile_p
9892 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9893 string, outputs, inputs, clobbers);
9894 /* If the extended syntax was not used, mark the ASM_STMT. */
9895 if (!extended_p)
9896 ASM_INPUT_P (asm_stmt) = 1;
9898 else
9899 assemble_asm (string);
9902 /* Declarators [gram.dcl.decl] */
9904 /* Parse an init-declarator.
9906 init-declarator:
9907 declarator initializer [opt]
9909 GNU Extension:
9911 init-declarator:
9912 declarator asm-specification [opt] attributes [opt] initializer [opt]
9914 function-definition:
9915 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9916 function-body
9917 decl-specifier-seq [opt] declarator function-try-block
9919 GNU Extension:
9921 function-definition:
9922 __extension__ function-definition
9924 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9925 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9926 then this declarator appears in a class scope. The new DECL created
9927 by this declarator is returned.
9929 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9930 for a function-definition here as well. If the declarator is a
9931 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9932 be TRUE upon return. By that point, the function-definition will
9933 have been completely parsed.
9935 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9936 is FALSE. */
9938 static tree
9939 cp_parser_init_declarator (cp_parser* parser,
9940 tree decl_specifiers,
9941 tree prefix_attributes,
9942 bool function_definition_allowed_p,
9943 bool member_p,
9944 int declares_class_or_enum,
9945 bool* function_definition_p)
9947 cp_token *token;
9948 tree declarator;
9949 tree attributes;
9950 tree asm_specification;
9951 tree initializer;
9952 tree decl = NULL_TREE;
9953 tree scope;
9954 bool is_initialized;
9955 bool is_parenthesized_init;
9956 bool is_non_constant_init;
9957 int ctor_dtor_or_conv_p;
9958 bool friend_p;
9959 bool pop_p = false;
9961 /* Assume that this is not the declarator for a function
9962 definition. */
9963 if (function_definition_p)
9964 *function_definition_p = false;
9966 /* Defer access checks while parsing the declarator; we cannot know
9967 what names are accessible until we know what is being
9968 declared. */
9969 resume_deferring_access_checks ();
9971 /* Parse the declarator. */
9972 declarator
9973 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9974 &ctor_dtor_or_conv_p,
9975 /*parenthesized_p=*/NULL,
9976 /*member_p=*/false);
9977 /* Gather up the deferred checks. */
9978 stop_deferring_access_checks ();
9980 /* If the DECLARATOR was erroneous, there's no need to go
9981 further. */
9982 if (declarator == error_mark_node)
9983 return error_mark_node;
9985 if (declares_class_or_enum & 2)
9986 cp_parser_check_for_definition_in_return_type
9987 (declarator, TREE_VALUE (decl_specifiers));
9989 /* Figure out what scope the entity declared by the DECLARATOR is
9990 located in. `grokdeclarator' sometimes changes the scope, so
9991 we compute it now. */
9992 scope = get_scope_of_declarator (declarator);
9994 /* If we're allowing GNU extensions, look for an asm-specification
9995 and attributes. */
9996 if (cp_parser_allow_gnu_extensions_p (parser))
9998 /* Look for an asm-specification. */
9999 asm_specification = cp_parser_asm_specification_opt (parser);
10000 /* And attributes. */
10001 attributes = cp_parser_attributes_opt (parser);
10003 else
10005 asm_specification = NULL_TREE;
10006 attributes = NULL_TREE;
10009 /* Peek at the next token. */
10010 token = cp_lexer_peek_token (parser->lexer);
10011 /* Check to see if the token indicates the start of a
10012 function-definition. */
10013 if (cp_parser_token_starts_function_definition_p (token))
10015 if (!function_definition_allowed_p)
10017 /* If a function-definition should not appear here, issue an
10018 error message. */
10019 cp_parser_error (parser,
10020 "a function-definition is not allowed here");
10021 return error_mark_node;
10023 else
10025 /* Neither attributes nor an asm-specification are allowed
10026 on a function-definition. */
10027 if (asm_specification)
10028 error ("an asm-specification is not allowed on a function-definition");
10029 if (attributes)
10030 error ("attributes are not allowed on a function-definition");
10031 /* This is a function-definition. */
10032 *function_definition_p = true;
10034 /* Parse the function definition. */
10035 if (member_p)
10036 decl = cp_parser_save_member_function_body (parser,
10037 decl_specifiers,
10038 declarator,
10039 prefix_attributes);
10040 else
10041 decl
10042 = (cp_parser_function_definition_from_specifiers_and_declarator
10043 (parser, decl_specifiers, prefix_attributes, declarator));
10045 return decl;
10049 /* [dcl.dcl]
10051 Only in function declarations for constructors, destructors, and
10052 type conversions can the decl-specifier-seq be omitted.
10054 We explicitly postpone this check past the point where we handle
10055 function-definitions because we tolerate function-definitions
10056 that are missing their return types in some modes. */
10057 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10059 cp_parser_error (parser,
10060 "expected constructor, destructor, or type conversion");
10061 return error_mark_node;
10064 /* An `=' or an `(' indicates an initializer. */
10065 is_initialized = (token->type == CPP_EQ
10066 || token->type == CPP_OPEN_PAREN);
10067 /* If the init-declarator isn't initialized and isn't followed by a
10068 `,' or `;', it's not a valid init-declarator. */
10069 if (!is_initialized
10070 && token->type != CPP_COMMA
10071 && token->type != CPP_SEMICOLON)
10073 cp_parser_error (parser, "expected initializer");
10074 return error_mark_node;
10077 /* Because start_decl has side-effects, we should only call it if we
10078 know we're going ahead. By this point, we know that we cannot
10079 possibly be looking at any other construct. */
10080 cp_parser_commit_to_tentative_parse (parser);
10082 /* If the decl specifiers were bad, issue an error now that we're
10083 sure this was intended to be a declarator. Then continue
10084 declaring the variable(s), as int, to try to cut down on further
10085 errors. */
10086 if (decl_specifiers != NULL
10087 && TREE_VALUE (decl_specifiers) == error_mark_node)
10089 cp_parser_error (parser, "invalid type in declaration");
10090 TREE_VALUE (decl_specifiers) = integer_type_node;
10093 /* Check to see whether or not this declaration is a friend. */
10094 friend_p = cp_parser_friend_p (decl_specifiers);
10096 /* Check that the number of template-parameter-lists is OK. */
10097 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10098 return error_mark_node;
10100 /* Enter the newly declared entry in the symbol table. If we're
10101 processing a declaration in a class-specifier, we wait until
10102 after processing the initializer. */
10103 if (!member_p)
10105 if (parser->in_unbraced_linkage_specification_p)
10107 decl_specifiers = tree_cons (error_mark_node,
10108 get_identifier ("extern"),
10109 decl_specifiers);
10110 have_extern_spec = false;
10112 decl = start_decl (declarator, decl_specifiers,
10113 is_initialized, attributes, prefix_attributes,
10114 &pop_p);
10116 else if (scope)
10117 /* Enter the SCOPE. That way unqualified names appearing in the
10118 initializer will be looked up in SCOPE. */
10119 pop_p = push_scope (scope);
10121 /* Perform deferred access control checks, now that we know in which
10122 SCOPE the declared entity resides. */
10123 if (!member_p && decl)
10125 tree saved_current_function_decl = NULL_TREE;
10127 /* If the entity being declared is a function, pretend that we
10128 are in its scope. If it is a `friend', it may have access to
10129 things that would not otherwise be accessible. */
10130 if (TREE_CODE (decl) == FUNCTION_DECL)
10132 saved_current_function_decl = current_function_decl;
10133 current_function_decl = decl;
10136 /* Perform the access control checks for the declarator and the
10137 the decl-specifiers. */
10138 perform_deferred_access_checks ();
10140 /* Restore the saved value. */
10141 if (TREE_CODE (decl) == FUNCTION_DECL)
10142 current_function_decl = saved_current_function_decl;
10145 /* Parse the initializer. */
10146 if (is_initialized)
10147 initializer = cp_parser_initializer (parser,
10148 &is_parenthesized_init,
10149 &is_non_constant_init);
10150 else
10152 initializer = NULL_TREE;
10153 is_parenthesized_init = false;
10154 is_non_constant_init = true;
10157 /* The old parser allows attributes to appear after a parenthesized
10158 initializer. Mark Mitchell proposed removing this functionality
10159 on the GCC mailing lists on 2002-08-13. This parser accepts the
10160 attributes -- but ignores them. */
10161 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10162 if (cp_parser_attributes_opt (parser))
10163 warning ("attributes after parenthesized initializer ignored");
10165 /* For an in-class declaration, use `grokfield' to create the
10166 declaration. */
10167 if (member_p)
10169 if (pop_p)
10170 pop_scope (scope);
10172 decl = grokfield (declarator, decl_specifiers,
10173 initializer, /*asmspec=*/NULL_TREE,
10174 /*attributes=*/NULL_TREE);
10175 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10176 cp_parser_save_default_args (parser, decl);
10179 /* Finish processing the declaration. But, skip friend
10180 declarations. */
10181 if (!friend_p && decl)
10183 cp_finish_decl (decl,
10184 initializer,
10185 asm_specification,
10186 /* If the initializer is in parentheses, then this is
10187 a direct-initialization, which means that an
10188 `explicit' constructor is OK. Otherwise, an
10189 `explicit' constructor cannot be used. */
10190 ((is_parenthesized_init || !is_initialized)
10191 ? 0 : LOOKUP_ONLYCONVERTING));
10192 if (pop_p)
10193 pop_scope (DECL_CONTEXT (decl));
10196 /* Remember whether or not variables were initialized by
10197 constant-expressions. */
10198 if (decl && TREE_CODE (decl) == VAR_DECL
10199 && is_initialized && !is_non_constant_init)
10200 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10202 return decl;
10205 /* Parse a declarator.
10207 declarator:
10208 direct-declarator
10209 ptr-operator declarator
10211 abstract-declarator:
10212 ptr-operator abstract-declarator [opt]
10213 direct-abstract-declarator
10215 GNU Extensions:
10217 declarator:
10218 attributes [opt] direct-declarator
10219 attributes [opt] ptr-operator declarator
10221 abstract-declarator:
10222 attributes [opt] ptr-operator abstract-declarator [opt]
10223 attributes [opt] direct-abstract-declarator
10225 Returns a representation of the declarator. If the declarator has
10226 the form `* declarator', then an INDIRECT_REF is returned, whose
10227 only operand is the sub-declarator. Analogously, `& declarator' is
10228 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10229 used. The first operand is the TYPE for `X'. The second operand
10230 is an INDIRECT_REF whose operand is the sub-declarator.
10232 Otherwise, the representation is as for a direct-declarator.
10234 (It would be better to define a structure type to represent
10235 declarators, rather than abusing `tree' nodes to represent
10236 declarators. That would be much clearer and save some memory.
10237 There is no reason for declarators to be garbage-collected, for
10238 example; they are created during parser and no longer needed after
10239 `grokdeclarator' has been called.)
10241 For a ptr-operator that has the optional cv-qualifier-seq,
10242 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10243 node.
10245 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10246 detect constructor, destructor or conversion operators. It is set
10247 to -1 if the declarator is a name, and +1 if it is a
10248 function. Otherwise it is set to zero. Usually you just want to
10249 test for >0, but internally the negative value is used.
10251 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10252 a decl-specifier-seq unless it declares a constructor, destructor,
10253 or conversion. It might seem that we could check this condition in
10254 semantic analysis, rather than parsing, but that makes it difficult
10255 to handle something like `f()'. We want to notice that there are
10256 no decl-specifiers, and therefore realize that this is an
10257 expression, not a declaration.)
10259 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10260 the declarator is a direct-declarator of the form "(...)".
10262 MEMBER_P is true iff this declarator is a member-declarator. */
10264 static tree
10265 cp_parser_declarator (cp_parser* parser,
10266 cp_parser_declarator_kind dcl_kind,
10267 int* ctor_dtor_or_conv_p,
10268 bool* parenthesized_p,
10269 bool member_p)
10271 cp_token *token;
10272 tree declarator;
10273 enum tree_code code;
10274 tree cv_qualifier_seq;
10275 tree class_type;
10276 tree attributes = NULL_TREE;
10278 /* Assume this is not a constructor, destructor, or type-conversion
10279 operator. */
10280 if (ctor_dtor_or_conv_p)
10281 *ctor_dtor_or_conv_p = 0;
10283 if (cp_parser_allow_gnu_extensions_p (parser))
10284 attributes = cp_parser_attributes_opt (parser);
10286 /* Peek at the next token. */
10287 token = cp_lexer_peek_token (parser->lexer);
10289 /* Check for the ptr-operator production. */
10290 cp_parser_parse_tentatively (parser);
10291 /* Parse the ptr-operator. */
10292 code = cp_parser_ptr_operator (parser,
10293 &class_type,
10294 &cv_qualifier_seq);
10295 /* If that worked, then we have a ptr-operator. */
10296 if (cp_parser_parse_definitely (parser))
10298 /* If a ptr-operator was found, then this declarator was not
10299 parenthesized. */
10300 if (parenthesized_p)
10301 *parenthesized_p = true;
10302 /* The dependent declarator is optional if we are parsing an
10303 abstract-declarator. */
10304 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10305 cp_parser_parse_tentatively (parser);
10307 /* Parse the dependent declarator. */
10308 declarator = cp_parser_declarator (parser, dcl_kind,
10309 /*ctor_dtor_or_conv_p=*/NULL,
10310 /*parenthesized_p=*/NULL,
10311 /*member_p=*/false);
10313 /* If we are parsing an abstract-declarator, we must handle the
10314 case where the dependent declarator is absent. */
10315 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10316 && !cp_parser_parse_definitely (parser))
10317 declarator = NULL_TREE;
10319 /* Build the representation of the ptr-operator. */
10320 if (code == INDIRECT_REF)
10321 declarator = make_pointer_declarator (cv_qualifier_seq,
10322 declarator);
10323 else
10324 declarator = make_reference_declarator (cv_qualifier_seq,
10325 declarator);
10326 /* Handle the pointer-to-member case. */
10327 if (class_type)
10328 declarator = build_nt (SCOPE_REF, class_type, declarator);
10330 /* Everything else is a direct-declarator. */
10331 else
10333 if (parenthesized_p)
10334 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10335 CPP_OPEN_PAREN);
10336 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10337 ctor_dtor_or_conv_p,
10338 member_p);
10341 if (attributes && declarator != error_mark_node)
10342 declarator = tree_cons (attributes, declarator, NULL_TREE);
10344 return declarator;
10347 /* Parse a direct-declarator or direct-abstract-declarator.
10349 direct-declarator:
10350 declarator-id
10351 direct-declarator ( parameter-declaration-clause )
10352 cv-qualifier-seq [opt]
10353 exception-specification [opt]
10354 direct-declarator [ constant-expression [opt] ]
10355 ( declarator )
10357 direct-abstract-declarator:
10358 direct-abstract-declarator [opt]
10359 ( parameter-declaration-clause )
10360 cv-qualifier-seq [opt]
10361 exception-specification [opt]
10362 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10363 ( abstract-declarator )
10365 Returns a representation of the declarator. DCL_KIND is
10366 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10367 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10368 we are parsing a direct-declarator. It is
10369 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10370 of ambiguity we prefer an abstract declarator, as per
10371 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10372 cp_parser_declarator.
10374 For the declarator-id production, the representation is as for an
10375 id-expression, except that a qualified name is represented as a
10376 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10377 see the documentation of the FUNCTION_DECLARATOR_* macros for
10378 information about how to find the various declarator components.
10379 An array-declarator is represented as an ARRAY_REF. The
10380 direct-declarator is the first operand; the constant-expression
10381 indicating the size of the array is the second operand. */
10383 static tree
10384 cp_parser_direct_declarator (cp_parser* parser,
10385 cp_parser_declarator_kind dcl_kind,
10386 int* ctor_dtor_or_conv_p,
10387 bool member_p)
10389 cp_token *token;
10390 tree declarator = NULL_TREE;
10391 tree scope = NULL_TREE;
10392 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10393 bool saved_in_declarator_p = parser->in_declarator_p;
10394 bool first = true;
10395 bool pop_p = false;
10397 while (true)
10399 /* Peek at the next token. */
10400 token = cp_lexer_peek_token (parser->lexer);
10401 if (token->type == CPP_OPEN_PAREN)
10403 /* This is either a parameter-declaration-clause, or a
10404 parenthesized declarator. When we know we are parsing a
10405 named declarator, it must be a parenthesized declarator
10406 if FIRST is true. For instance, `(int)' is a
10407 parameter-declaration-clause, with an omitted
10408 direct-abstract-declarator. But `((*))', is a
10409 parenthesized abstract declarator. Finally, when T is a
10410 template parameter `(T)' is a
10411 parameter-declaration-clause, and not a parenthesized
10412 named declarator.
10414 We first try and parse a parameter-declaration-clause,
10415 and then try a nested declarator (if FIRST is true).
10417 It is not an error for it not to be a
10418 parameter-declaration-clause, even when FIRST is
10419 false. Consider,
10421 int i (int);
10422 int i (3);
10424 The first is the declaration of a function while the
10425 second is a the definition of a variable, including its
10426 initializer.
10428 Having seen only the parenthesis, we cannot know which of
10429 these two alternatives should be selected. Even more
10430 complex are examples like:
10432 int i (int (a));
10433 int i (int (3));
10435 The former is a function-declaration; the latter is a
10436 variable initialization.
10438 Thus again, we try a parameter-declaration-clause, and if
10439 that fails, we back out and return. */
10441 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10443 tree params;
10444 unsigned saved_num_template_parameter_lists;
10446 /* In a member-declarator, the only valid interpretation
10447 of a parenthesis is the start of a
10448 parameter-declaration-clause. (It is invalid to
10449 initialize a static data member with a parenthesized
10450 initializer; only the "=" form of initialization is
10451 permitted.) */
10452 if (!member_p)
10453 cp_parser_parse_tentatively (parser);
10455 /* Consume the `('. */
10456 cp_lexer_consume_token (parser->lexer);
10457 if (first)
10459 /* If this is going to be an abstract declarator, we're
10460 in a declarator and we can't have default args. */
10461 parser->default_arg_ok_p = false;
10462 parser->in_declarator_p = true;
10465 /* Inside the function parameter list, surrounding
10466 template-parameter-lists do not apply. */
10467 saved_num_template_parameter_lists
10468 = parser->num_template_parameter_lists;
10469 parser->num_template_parameter_lists = 0;
10471 /* Parse the parameter-declaration-clause. */
10472 params = cp_parser_parameter_declaration_clause (parser);
10474 parser->num_template_parameter_lists
10475 = saved_num_template_parameter_lists;
10477 /* If all went well, parse the cv-qualifier-seq and the
10478 exception-specification. */
10479 if (member_p || cp_parser_parse_definitely (parser))
10481 tree cv_qualifiers;
10482 tree exception_specification;
10484 if (ctor_dtor_or_conv_p)
10485 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10486 first = false;
10487 /* Consume the `)'. */
10488 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10490 /* Parse the cv-qualifier-seq. */
10491 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10492 /* And the exception-specification. */
10493 exception_specification
10494 = cp_parser_exception_specification_opt (parser);
10496 /* Create the function-declarator. */
10497 declarator = make_call_declarator (declarator,
10498 params,
10499 cv_qualifiers,
10500 exception_specification);
10501 /* Any subsequent parameter lists are to do with
10502 return type, so are not those of the declared
10503 function. */
10504 parser->default_arg_ok_p = false;
10506 /* Repeat the main loop. */
10507 continue;
10511 /* If this is the first, we can try a parenthesized
10512 declarator. */
10513 if (first)
10515 bool saved_in_type_id_in_expr_p;
10517 parser->default_arg_ok_p = saved_default_arg_ok_p;
10518 parser->in_declarator_p = saved_in_declarator_p;
10520 /* Consume the `('. */
10521 cp_lexer_consume_token (parser->lexer);
10522 /* Parse the nested declarator. */
10523 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10524 parser->in_type_id_in_expr_p = true;
10525 declarator
10526 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10527 /*parenthesized_p=*/NULL,
10528 member_p);
10529 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10530 first = false;
10531 /* Expect a `)'. */
10532 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10533 declarator = error_mark_node;
10534 if (declarator == error_mark_node)
10535 break;
10537 goto handle_declarator;
10539 /* Otherwise, we must be done. */
10540 else
10541 break;
10543 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10544 && token->type == CPP_OPEN_SQUARE)
10546 /* Parse an array-declarator. */
10547 tree bounds;
10549 if (ctor_dtor_or_conv_p)
10550 *ctor_dtor_or_conv_p = 0;
10552 first = false;
10553 parser->default_arg_ok_p = false;
10554 parser->in_declarator_p = true;
10555 /* Consume the `['. */
10556 cp_lexer_consume_token (parser->lexer);
10557 /* Peek at the next token. */
10558 token = cp_lexer_peek_token (parser->lexer);
10559 /* If the next token is `]', then there is no
10560 constant-expression. */
10561 if (token->type != CPP_CLOSE_SQUARE)
10563 bool non_constant_p;
10565 bounds
10566 = cp_parser_constant_expression (parser,
10567 /*allow_non_constant=*/true,
10568 &non_constant_p);
10569 if (!non_constant_p)
10570 bounds = fold_non_dependent_expr (bounds);
10572 else
10573 bounds = NULL_TREE;
10574 /* Look for the closing `]'. */
10575 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10577 declarator = error_mark_node;
10578 break;
10581 declarator = build_nt (ARRAY_REF, declarator, bounds);
10583 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10585 /* Parse a declarator-id */
10586 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10587 cp_parser_parse_tentatively (parser);
10588 declarator = cp_parser_declarator_id (parser);
10589 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10591 if (!cp_parser_parse_definitely (parser))
10592 declarator = error_mark_node;
10593 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10595 cp_parser_error (parser, "expected unqualified-id");
10596 declarator = error_mark_node;
10600 if (declarator == error_mark_node)
10601 break;
10603 if (TREE_CODE (declarator) == SCOPE_REF
10604 && !current_scope ())
10606 tree scope = TREE_OPERAND (declarator, 0);
10608 /* In the declaration of a member of a template class
10609 outside of the class itself, the SCOPE will sometimes
10610 be a TYPENAME_TYPE. For example, given:
10612 template <typename T>
10613 int S<T>::R::i = 3;
10615 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10616 this context, we must resolve S<T>::R to an ordinary
10617 type, rather than a typename type.
10619 The reason we normally avoid resolving TYPENAME_TYPEs
10620 is that a specialization of `S' might render
10621 `S<T>::R' not a type. However, if `S' is
10622 specialized, then this `i' will not be used, so there
10623 is no harm in resolving the types here. */
10624 if (TREE_CODE (scope) == TYPENAME_TYPE)
10626 tree type;
10628 /* Resolve the TYPENAME_TYPE. */
10629 type = resolve_typename_type (scope,
10630 /*only_current_p=*/false);
10631 /* If that failed, the declarator is invalid. */
10632 if (type == error_mark_node)
10633 error ("`%T::%D' is not a type",
10634 TYPE_CONTEXT (scope),
10635 TYPE_IDENTIFIER (scope));
10636 /* Build a new DECLARATOR. */
10637 declarator = build_nt (SCOPE_REF,
10638 type,
10639 TREE_OPERAND (declarator, 1));
10643 /* Check to see whether the declarator-id names a constructor,
10644 destructor, or conversion. */
10645 if (declarator && ctor_dtor_or_conv_p
10646 && ((TREE_CODE (declarator) == SCOPE_REF
10647 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10648 || (TREE_CODE (declarator) != SCOPE_REF
10649 && at_class_scope_p ())))
10651 tree unqualified_name;
10652 tree class_type;
10654 /* Get the unqualified part of the name. */
10655 if (TREE_CODE (declarator) == SCOPE_REF)
10657 class_type = TREE_OPERAND (declarator, 0);
10658 unqualified_name = TREE_OPERAND (declarator, 1);
10660 else
10662 class_type = current_class_type;
10663 unqualified_name = declarator;
10666 /* See if it names ctor, dtor or conv. */
10667 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10668 || IDENTIFIER_TYPENAME_P (unqualified_name)
10669 || constructor_name_p (unqualified_name, class_type)
10670 || (TREE_CODE (unqualified_name) == TYPE_DECL
10671 && same_type_p (TREE_TYPE (unqualified_name),
10672 class_type)))
10673 *ctor_dtor_or_conv_p = -1;
10676 handle_declarator:;
10677 scope = get_scope_of_declarator (declarator);
10678 if (scope)
10679 /* Any names that appear after the declarator-id for a
10680 member are looked up in the containing scope. */
10681 pop_p = push_scope (scope);
10682 parser->in_declarator_p = true;
10683 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10684 || (declarator
10685 && (TREE_CODE (declarator) == SCOPE_REF
10686 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10687 /* Default args are only allowed on function
10688 declarations. */
10689 parser->default_arg_ok_p = saved_default_arg_ok_p;
10690 else
10691 parser->default_arg_ok_p = false;
10693 first = false;
10695 /* We're done. */
10696 else
10697 break;
10700 /* For an abstract declarator, we might wind up with nothing at this
10701 point. That's an error; the declarator is not optional. */
10702 if (!declarator)
10703 cp_parser_error (parser, "expected declarator");
10705 /* If we entered a scope, we must exit it now. */
10706 if (pop_p)
10707 pop_scope (scope);
10709 parser->default_arg_ok_p = saved_default_arg_ok_p;
10710 parser->in_declarator_p = saved_in_declarator_p;
10712 return declarator;
10715 /* Parse a ptr-operator.
10717 ptr-operator:
10718 * cv-qualifier-seq [opt]
10720 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10722 GNU Extension:
10724 ptr-operator:
10725 & cv-qualifier-seq [opt]
10727 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10728 used. Returns ADDR_EXPR if a reference was used. In the
10729 case of a pointer-to-member, *TYPE is filled in with the
10730 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10731 with the cv-qualifier-seq, or NULL_TREE, if there are no
10732 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10734 static enum tree_code
10735 cp_parser_ptr_operator (cp_parser* parser,
10736 tree* type,
10737 tree* cv_qualifier_seq)
10739 enum tree_code code = ERROR_MARK;
10740 cp_token *token;
10742 /* Assume that it's not a pointer-to-member. */
10743 *type = NULL_TREE;
10744 /* And that there are no cv-qualifiers. */
10745 *cv_qualifier_seq = NULL_TREE;
10747 /* Peek at the next token. */
10748 token = cp_lexer_peek_token (parser->lexer);
10749 /* If it's a `*' or `&' we have a pointer or reference. */
10750 if (token->type == CPP_MULT || token->type == CPP_AND)
10752 /* Remember which ptr-operator we were processing. */
10753 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10755 /* Consume the `*' or `&'. */
10756 cp_lexer_consume_token (parser->lexer);
10758 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10759 `&', if we are allowing GNU extensions. (The only qualifier
10760 that can legally appear after `&' is `restrict', but that is
10761 enforced during semantic analysis. */
10762 if (code == INDIRECT_REF
10763 || cp_parser_allow_gnu_extensions_p (parser))
10764 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10766 else
10768 /* Try the pointer-to-member case. */
10769 cp_parser_parse_tentatively (parser);
10770 /* Look for the optional `::' operator. */
10771 cp_parser_global_scope_opt (parser,
10772 /*current_scope_valid_p=*/false);
10773 /* Look for the nested-name specifier. */
10774 cp_parser_nested_name_specifier (parser,
10775 /*typename_keyword_p=*/false,
10776 /*check_dependency_p=*/true,
10777 /*type_p=*/false,
10778 /*is_declaration=*/false);
10779 /* If we found it, and the next token is a `*', then we are
10780 indeed looking at a pointer-to-member operator. */
10781 if (!cp_parser_error_occurred (parser)
10782 && cp_parser_require (parser, CPP_MULT, "`*'"))
10784 /* The type of which the member is a member is given by the
10785 current SCOPE. */
10786 *type = parser->scope;
10787 /* The next name will not be qualified. */
10788 parser->scope = NULL_TREE;
10789 parser->qualifying_scope = NULL_TREE;
10790 parser->object_scope = NULL_TREE;
10791 /* Indicate that the `*' operator was used. */
10792 code = INDIRECT_REF;
10793 /* Look for the optional cv-qualifier-seq. */
10794 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10796 /* If that didn't work we don't have a ptr-operator. */
10797 if (!cp_parser_parse_definitely (parser))
10798 cp_parser_error (parser, "expected ptr-operator");
10801 return code;
10804 /* Parse an (optional) cv-qualifier-seq.
10806 cv-qualifier-seq:
10807 cv-qualifier cv-qualifier-seq [opt]
10809 Returns a TREE_LIST. The TREE_VALUE of each node is the
10810 representation of a cv-qualifier. */
10812 static tree
10813 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10815 tree cv_qualifiers = NULL_TREE;
10817 while (true)
10819 tree cv_qualifier;
10821 /* Look for the next cv-qualifier. */
10822 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10823 /* If we didn't find one, we're done. */
10824 if (!cv_qualifier)
10825 break;
10827 /* Add this cv-qualifier to the list. */
10828 cv_qualifiers
10829 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10832 /* We built up the list in reverse order. */
10833 return nreverse (cv_qualifiers);
10836 /* Parse an (optional) cv-qualifier.
10838 cv-qualifier:
10839 const
10840 volatile
10842 GNU Extension:
10844 cv-qualifier:
10845 __restrict__ */
10847 static tree
10848 cp_parser_cv_qualifier_opt (cp_parser* parser)
10850 cp_token *token;
10851 tree cv_qualifier = NULL_TREE;
10853 /* Peek at the next token. */
10854 token = cp_lexer_peek_token (parser->lexer);
10855 /* See if it's a cv-qualifier. */
10856 switch (token->keyword)
10858 case RID_CONST:
10859 case RID_VOLATILE:
10860 case RID_RESTRICT:
10861 /* Save the value of the token. */
10862 cv_qualifier = token->value;
10863 /* Consume the token. */
10864 cp_lexer_consume_token (parser->lexer);
10865 break;
10867 default:
10868 break;
10871 return cv_qualifier;
10874 /* Parse a declarator-id.
10876 declarator-id:
10877 id-expression
10878 :: [opt] nested-name-specifier [opt] type-name
10880 In the `id-expression' case, the value returned is as for
10881 cp_parser_id_expression if the id-expression was an unqualified-id.
10882 If the id-expression was a qualified-id, then a SCOPE_REF is
10883 returned. The first operand is the scope (either a NAMESPACE_DECL
10884 or TREE_TYPE), but the second is still just a representation of an
10885 unqualified-id. */
10887 static tree
10888 cp_parser_declarator_id (cp_parser* parser)
10890 tree id_expression;
10892 /* The expression must be an id-expression. Assume that qualified
10893 names are the names of types so that:
10895 template <class T>
10896 int S<T>::R::i = 3;
10898 will work; we must treat `S<T>::R' as the name of a type.
10899 Similarly, assume that qualified names are templates, where
10900 required, so that:
10902 template <class T>
10903 int S<T>::R<T>::i = 3;
10905 will work, too. */
10906 id_expression = cp_parser_id_expression (parser,
10907 /*template_keyword_p=*/false,
10908 /*check_dependency_p=*/false,
10909 /*template_p=*/NULL,
10910 /*declarator_p=*/true);
10911 /* If the name was qualified, create a SCOPE_REF to represent
10912 that. */
10913 if (parser->scope)
10915 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10916 parser->scope = NULL_TREE;
10919 return id_expression;
10922 /* Parse a type-id.
10924 type-id:
10925 type-specifier-seq abstract-declarator [opt]
10927 Returns the TYPE specified. */
10929 static tree
10930 cp_parser_type_id (cp_parser* parser)
10932 tree type_specifier_seq;
10933 tree abstract_declarator;
10935 /* Parse the type-specifier-seq. */
10936 type_specifier_seq
10937 = cp_parser_type_specifier_seq (parser);
10938 if (type_specifier_seq == error_mark_node)
10939 return error_mark_node;
10941 /* There might or might not be an abstract declarator. */
10942 cp_parser_parse_tentatively (parser);
10943 /* Look for the declarator. */
10944 abstract_declarator
10945 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10946 /*parenthesized_p=*/NULL,
10947 /*member_p=*/false);
10948 /* Check to see if there really was a declarator. */
10949 if (!cp_parser_parse_definitely (parser))
10950 abstract_declarator = NULL_TREE;
10952 return groktypename (build_tree_list (type_specifier_seq,
10953 abstract_declarator));
10956 /* Parse a type-specifier-seq.
10958 type-specifier-seq:
10959 type-specifier type-specifier-seq [opt]
10961 GNU extension:
10963 type-specifier-seq:
10964 attributes type-specifier-seq [opt]
10966 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10967 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10969 static tree
10970 cp_parser_type_specifier_seq (cp_parser* parser)
10972 bool seen_type_specifier = false;
10973 tree type_specifier_seq = NULL_TREE;
10975 /* Parse the type-specifiers and attributes. */
10976 while (true)
10978 tree type_specifier;
10980 /* Check for attributes first. */
10981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10983 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10984 NULL_TREE,
10985 type_specifier_seq);
10986 continue;
10989 /* After the first type-specifier, others are optional. */
10990 if (seen_type_specifier)
10991 cp_parser_parse_tentatively (parser);
10992 /* Look for the type-specifier. */
10993 type_specifier = cp_parser_type_specifier (parser,
10994 CP_PARSER_FLAGS_NONE,
10995 /*is_friend=*/false,
10996 /*is_declaration=*/false,
10997 NULL,
10998 NULL);
10999 /* If the first type-specifier could not be found, this is not a
11000 type-specifier-seq at all. */
11001 if (!seen_type_specifier && type_specifier == error_mark_node)
11002 return error_mark_node;
11003 /* If subsequent type-specifiers could not be found, the
11004 type-specifier-seq is complete. */
11005 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
11006 break;
11008 /* Add the new type-specifier to the list. */
11009 type_specifier_seq
11010 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
11011 seen_type_specifier = true;
11014 /* We built up the list in reverse order. */
11015 return nreverse (type_specifier_seq);
11018 /* Parse a parameter-declaration-clause.
11020 parameter-declaration-clause:
11021 parameter-declaration-list [opt] ... [opt]
11022 parameter-declaration-list , ...
11024 Returns a representation for the parameter declarations. Each node
11025 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
11026 representation.) If the parameter-declaration-clause ends with an
11027 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
11028 list. A return value of NULL_TREE indicates a
11029 parameter-declaration-clause consisting only of an ellipsis. */
11031 static tree
11032 cp_parser_parameter_declaration_clause (cp_parser* parser)
11034 tree parameters;
11035 cp_token *token;
11036 bool ellipsis_p;
11038 /* Peek at the next token. */
11039 token = cp_lexer_peek_token (parser->lexer);
11040 /* Check for trivial parameter-declaration-clauses. */
11041 if (token->type == CPP_ELLIPSIS)
11043 /* Consume the `...' token. */
11044 cp_lexer_consume_token (parser->lexer);
11045 return NULL_TREE;
11047 else if (token->type == CPP_CLOSE_PAREN)
11048 /* There are no parameters. */
11050 #ifndef NO_IMPLICIT_EXTERN_C
11051 if (in_system_header && current_class_type == NULL
11052 && current_lang_name == lang_name_c)
11053 return NULL_TREE;
11054 else
11055 #endif
11056 return void_list_node;
11058 /* Check for `(void)', too, which is a special case. */
11059 else if (token->keyword == RID_VOID
11060 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11061 == CPP_CLOSE_PAREN))
11063 /* Consume the `void' token. */
11064 cp_lexer_consume_token (parser->lexer);
11065 /* There are no parameters. */
11066 return void_list_node;
11069 /* Parse the parameter-declaration-list. */
11070 parameters = cp_parser_parameter_declaration_list (parser);
11071 /* If a parse error occurred while parsing the
11072 parameter-declaration-list, then the entire
11073 parameter-declaration-clause is erroneous. */
11074 if (parameters == error_mark_node)
11075 return error_mark_node;
11077 /* Peek at the next token. */
11078 token = cp_lexer_peek_token (parser->lexer);
11079 /* If it's a `,', the clause should terminate with an ellipsis. */
11080 if (token->type == CPP_COMMA)
11082 /* Consume the `,'. */
11083 cp_lexer_consume_token (parser->lexer);
11084 /* Expect an ellipsis. */
11085 ellipsis_p
11086 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11088 /* It might also be `...' if the optional trailing `,' was
11089 omitted. */
11090 else if (token->type == CPP_ELLIPSIS)
11092 /* Consume the `...' token. */
11093 cp_lexer_consume_token (parser->lexer);
11094 /* And remember that we saw it. */
11095 ellipsis_p = true;
11097 else
11098 ellipsis_p = false;
11100 /* Finish the parameter list. */
11101 return finish_parmlist (parameters, ellipsis_p);
11104 /* Parse a parameter-declaration-list.
11106 parameter-declaration-list:
11107 parameter-declaration
11108 parameter-declaration-list , parameter-declaration
11110 Returns a representation of the parameter-declaration-list, as for
11111 cp_parser_parameter_declaration_clause. However, the
11112 `void_list_node' is never appended to the list. */
11114 static tree
11115 cp_parser_parameter_declaration_list (cp_parser* parser)
11117 tree parameters = NULL_TREE;
11119 /* Look for more parameters. */
11120 while (true)
11122 tree parameter;
11123 bool parenthesized_p;
11124 /* Parse the parameter. */
11125 parameter
11126 = cp_parser_parameter_declaration (parser,
11127 /*template_parm_p=*/false,
11128 &parenthesized_p);
11130 /* If a parse error occurred parsing the parameter declaration,
11131 then the entire parameter-declaration-list is erroneous. */
11132 if (parameter == error_mark_node)
11134 parameters = error_mark_node;
11135 break;
11137 /* Add the new parameter to the list. */
11138 TREE_CHAIN (parameter) = parameters;
11139 parameters = parameter;
11141 /* Peek at the next token. */
11142 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11143 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11144 /* The parameter-declaration-list is complete. */
11145 break;
11146 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11148 cp_token *token;
11150 /* Peek at the next token. */
11151 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11152 /* If it's an ellipsis, then the list is complete. */
11153 if (token->type == CPP_ELLIPSIS)
11154 break;
11155 /* Otherwise, there must be more parameters. Consume the
11156 `,'. */
11157 cp_lexer_consume_token (parser->lexer);
11158 /* When parsing something like:
11160 int i(float f, double d)
11162 we can tell after seeing the declaration for "f" that we
11163 are not looking at an initialization of a variable "i",
11164 but rather at the declaration of a function "i".
11166 Due to the fact that the parsing of template arguments
11167 (as specified to a template-id) requires backtracking we
11168 cannot use this technique when inside a template argument
11169 list. */
11170 if (!parser->in_template_argument_list_p
11171 && !parser->in_type_id_in_expr_p
11172 && cp_parser_parsing_tentatively (parser)
11173 && !cp_parser_committed_to_tentative_parse (parser)
11174 /* However, a parameter-declaration of the form
11175 "foat(f)" (which is a valid declaration of a
11176 parameter "f") can also be interpreted as an
11177 expression (the conversion of "f" to "float"). */
11178 && !parenthesized_p)
11179 cp_parser_commit_to_tentative_parse (parser);
11181 else
11183 cp_parser_error (parser, "expected `,' or `...'");
11184 if (!cp_parser_parsing_tentatively (parser)
11185 || cp_parser_committed_to_tentative_parse (parser))
11186 cp_parser_skip_to_closing_parenthesis (parser,
11187 /*recovering=*/true,
11188 /*or_comma=*/false,
11189 /*consume_paren=*/false);
11190 break;
11194 /* We built up the list in reverse order; straighten it out now. */
11195 return nreverse (parameters);
11198 /* Parse a parameter declaration.
11200 parameter-declaration:
11201 decl-specifier-seq declarator
11202 decl-specifier-seq declarator = assignment-expression
11203 decl-specifier-seq abstract-declarator [opt]
11204 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11206 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11207 declares a template parameter. (In that case, a non-nested `>'
11208 token encountered during the parsing of the assignment-expression
11209 is not interpreted as a greater-than operator.)
11211 Returns a TREE_LIST representing the parameter-declaration. The
11212 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11213 there is no default argument. The TREE_VALUE is a representation
11214 of the decl-specifier-seq and declarator. In particular, the
11215 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11216 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11217 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11218 the declarator is of the form "(p)". */
11220 static tree
11221 cp_parser_parameter_declaration (cp_parser *parser,
11222 bool template_parm_p,
11223 bool *parenthesized_p)
11225 int declares_class_or_enum;
11226 bool greater_than_is_operator_p;
11227 tree decl_specifiers;
11228 tree attributes;
11229 tree declarator;
11230 tree default_argument;
11231 tree parameter;
11232 cp_token *token;
11233 const char *saved_message;
11235 /* In a template parameter, `>' is not an operator.
11237 [temp.param]
11239 When parsing a default template-argument for a non-type
11240 template-parameter, the first non-nested `>' is taken as the end
11241 of the template parameter-list rather than a greater-than
11242 operator. */
11243 greater_than_is_operator_p = !template_parm_p;
11245 /* Type definitions may not appear in parameter types. */
11246 saved_message = parser->type_definition_forbidden_message;
11247 parser->type_definition_forbidden_message
11248 = "types may not be defined in parameter types";
11250 /* Parse the declaration-specifiers. */
11251 decl_specifiers
11252 = cp_parser_decl_specifier_seq (parser,
11253 CP_PARSER_FLAGS_NONE,
11254 &attributes,
11255 &declares_class_or_enum);
11256 /* If an error occurred, there's no reason to attempt to parse the
11257 rest of the declaration. */
11258 if (cp_parser_error_occurred (parser))
11260 parser->type_definition_forbidden_message = saved_message;
11261 return error_mark_node;
11264 /* Peek at the next token. */
11265 token = cp_lexer_peek_token (parser->lexer);
11266 /* If the next token is a `)', `,', `=', `>', or `...', then there
11267 is no declarator. */
11268 if (token->type == CPP_CLOSE_PAREN
11269 || token->type == CPP_COMMA
11270 || token->type == CPP_EQ
11271 || token->type == CPP_ELLIPSIS
11272 || token->type == CPP_GREATER)
11274 declarator = NULL_TREE;
11275 if (parenthesized_p)
11276 *parenthesized_p = false;
11278 /* Otherwise, there should be a declarator. */
11279 else
11281 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11282 parser->default_arg_ok_p = false;
11284 /* After seeing a decl-specifier-seq, if the next token is not a
11285 "(", there is no possibility that the code is a valid
11286 expression. Therefore, if parsing tentatively, we commit at
11287 this point. */
11288 if (!parser->in_template_argument_list_p
11289 /* In an expression context, having seen:
11291 (int((char ...
11293 we cannot be sure whether we are looking at a
11294 function-type (taking a "char" as a parameter) or a cast
11295 of some object of type "char" to "int". */
11296 && !parser->in_type_id_in_expr_p
11297 && cp_parser_parsing_tentatively (parser)
11298 && !cp_parser_committed_to_tentative_parse (parser)
11299 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11300 cp_parser_commit_to_tentative_parse (parser);
11301 /* Parse the declarator. */
11302 declarator = cp_parser_declarator (parser,
11303 CP_PARSER_DECLARATOR_EITHER,
11304 /*ctor_dtor_or_conv_p=*/NULL,
11305 parenthesized_p,
11306 /*member_p=*/false);
11307 parser->default_arg_ok_p = saved_default_arg_ok_p;
11308 /* After the declarator, allow more attributes. */
11309 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11312 /* The restriction on defining new types applies only to the type
11313 of the parameter, not to the default argument. */
11314 parser->type_definition_forbidden_message = saved_message;
11316 /* If the next token is `=', then process a default argument. */
11317 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11319 bool saved_greater_than_is_operator_p;
11320 /* Consume the `='. */
11321 cp_lexer_consume_token (parser->lexer);
11323 /* If we are defining a class, then the tokens that make up the
11324 default argument must be saved and processed later. */
11325 if (!template_parm_p && at_class_scope_p ()
11326 && TYPE_BEING_DEFINED (current_class_type))
11328 unsigned depth = 0;
11330 /* Create a DEFAULT_ARG to represented the unparsed default
11331 argument. */
11332 default_argument = make_node (DEFAULT_ARG);
11333 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11335 /* Add tokens until we have processed the entire default
11336 argument. */
11337 while (true)
11339 bool done = false;
11340 cp_token *token;
11342 /* Peek at the next token. */
11343 token = cp_lexer_peek_token (parser->lexer);
11344 /* What we do depends on what token we have. */
11345 switch (token->type)
11347 /* In valid code, a default argument must be
11348 immediately followed by a `,' `)', or `...'. */
11349 case CPP_COMMA:
11350 case CPP_CLOSE_PAREN:
11351 case CPP_ELLIPSIS:
11352 /* If we run into a non-nested `;', `}', or `]',
11353 then the code is invalid -- but the default
11354 argument is certainly over. */
11355 case CPP_SEMICOLON:
11356 case CPP_CLOSE_BRACE:
11357 case CPP_CLOSE_SQUARE:
11358 if (depth == 0)
11359 done = true;
11360 /* Update DEPTH, if necessary. */
11361 else if (token->type == CPP_CLOSE_PAREN
11362 || token->type == CPP_CLOSE_BRACE
11363 || token->type == CPP_CLOSE_SQUARE)
11364 --depth;
11365 break;
11367 case CPP_OPEN_PAREN:
11368 case CPP_OPEN_SQUARE:
11369 case CPP_OPEN_BRACE:
11370 ++depth;
11371 break;
11373 case CPP_GREATER:
11374 /* If we see a non-nested `>', and `>' is not an
11375 operator, then it marks the end of the default
11376 argument. */
11377 if (!depth && !greater_than_is_operator_p)
11378 done = true;
11379 break;
11381 /* If we run out of tokens, issue an error message. */
11382 case CPP_EOF:
11383 error ("file ends in default argument");
11384 done = true;
11385 break;
11387 case CPP_NAME:
11388 case CPP_SCOPE:
11389 /* In these cases, we should look for template-ids.
11390 For example, if the default argument is
11391 `X<int, double>()', we need to do name lookup to
11392 figure out whether or not `X' is a template; if
11393 so, the `,' does not end the default argument.
11395 That is not yet done. */
11396 break;
11398 default:
11399 break;
11402 /* If we've reached the end, stop. */
11403 if (done)
11404 break;
11406 /* Add the token to the token block. */
11407 token = cp_lexer_consume_token (parser->lexer);
11408 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11409 token);
11412 /* Outside of a class definition, we can just parse the
11413 assignment-expression. */
11414 else
11416 bool saved_local_variables_forbidden_p;
11418 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11419 set correctly. */
11420 saved_greater_than_is_operator_p
11421 = parser->greater_than_is_operator_p;
11422 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11423 /* Local variable names (and the `this' keyword) may not
11424 appear in a default argument. */
11425 saved_local_variables_forbidden_p
11426 = parser->local_variables_forbidden_p;
11427 parser->local_variables_forbidden_p = true;
11428 /* Parse the assignment-expression. */
11429 default_argument = cp_parser_assignment_expression (parser);
11430 /* Restore saved state. */
11431 parser->greater_than_is_operator_p
11432 = saved_greater_than_is_operator_p;
11433 parser->local_variables_forbidden_p
11434 = saved_local_variables_forbidden_p;
11436 if (!parser->default_arg_ok_p)
11438 if (!flag_pedantic_errors)
11439 warning ("deprecated use of default argument for parameter of non-function");
11440 else
11442 error ("default arguments are only permitted for function parameters");
11443 default_argument = NULL_TREE;
11447 else
11448 default_argument = NULL_TREE;
11450 /* Create the representation of the parameter. */
11451 if (attributes)
11452 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11453 parameter = build_tree_list (default_argument,
11454 build_tree_list (decl_specifiers,
11455 declarator));
11457 return parameter;
11460 /* Parse a function-body.
11462 function-body:
11463 compound_statement */
11465 static void
11466 cp_parser_function_body (cp_parser *parser)
11468 cp_parser_compound_statement (parser, false);
11471 /* Parse a ctor-initializer-opt followed by a function-body. Return
11472 true if a ctor-initializer was present. */
11474 static bool
11475 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11477 tree body;
11478 bool ctor_initializer_p;
11480 /* Begin the function body. */
11481 body = begin_function_body ();
11482 /* Parse the optional ctor-initializer. */
11483 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11484 /* Parse the function-body. */
11485 cp_parser_function_body (parser);
11486 /* Finish the function body. */
11487 finish_function_body (body);
11489 return ctor_initializer_p;
11492 /* Parse an initializer.
11494 initializer:
11495 = initializer-clause
11496 ( expression-list )
11498 Returns a expression representing the initializer. If no
11499 initializer is present, NULL_TREE is returned.
11501 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11502 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11503 set to FALSE if there is no initializer present. If there is an
11504 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11505 is set to true; otherwise it is set to false. */
11507 static tree
11508 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11509 bool* non_constant_p)
11511 cp_token *token;
11512 tree init;
11514 /* Peek at the next token. */
11515 token = cp_lexer_peek_token (parser->lexer);
11517 /* Let our caller know whether or not this initializer was
11518 parenthesized. */
11519 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11520 /* Assume that the initializer is constant. */
11521 *non_constant_p = false;
11523 if (token->type == CPP_EQ)
11525 /* Consume the `='. */
11526 cp_lexer_consume_token (parser->lexer);
11527 /* Parse the initializer-clause. */
11528 init = cp_parser_initializer_clause (parser, non_constant_p);
11530 else if (token->type == CPP_OPEN_PAREN)
11531 init = cp_parser_parenthesized_expression_list (parser, false,
11532 non_constant_p);
11533 else
11535 /* Anything else is an error. */
11536 cp_parser_error (parser, "expected initializer");
11537 init = error_mark_node;
11540 return init;
11543 /* Parse an initializer-clause.
11545 initializer-clause:
11546 assignment-expression
11547 { initializer-list , [opt] }
11550 Returns an expression representing the initializer.
11552 If the `assignment-expression' production is used the value
11553 returned is simply a representation for the expression.
11555 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11556 the elements of the initializer-list (or NULL_TREE, if the last
11557 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11558 NULL_TREE. There is no way to detect whether or not the optional
11559 trailing `,' was provided. NON_CONSTANT_P is as for
11560 cp_parser_initializer. */
11562 static tree
11563 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11565 tree initializer = NULL_TREE;
11567 /* Assume the expression is constant. */
11568 *non_constant_p = false;
11570 /* If it is not a `{', then we are looking at an
11571 assignment-expression. */
11572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11574 /* Speed up common initializers (simply a literal). */
11575 cp_token* token = cp_lexer_peek_token (parser->lexer);
11576 cp_token* token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11578 if (token2->type == CPP_COMMA)
11579 switch (token->type)
11581 case CPP_CHAR:
11582 case CPP_WCHAR:
11583 case CPP_NUMBER:
11584 token = cp_lexer_consume_token (parser->lexer);
11585 initializer = token->value;
11586 break;
11588 case CPP_STRING:
11589 case CPP_WSTRING:
11590 token = cp_lexer_consume_token (parser->lexer);
11591 if (TREE_CHAIN (token->value))
11592 initializer = TREE_CHAIN (token->value);
11593 else
11594 initializer = token->value;
11595 break;
11597 default:
11598 break;
11601 /* Otherwise, fall back to the generic assignment expression. */
11602 if (!initializer)
11604 initializer
11605 = cp_parser_constant_expression (parser,
11606 /*allow_non_constant_p=*/true,
11607 non_constant_p);
11608 if (!*non_constant_p)
11609 initializer = fold_non_dependent_expr (initializer);
11612 else
11614 /* Consume the `{' token. */
11615 cp_lexer_consume_token (parser->lexer);
11616 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11617 initializer = make_node (CONSTRUCTOR);
11618 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11619 necessary, but check_initializer depends upon it, for
11620 now. */
11621 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11622 /* If it's not a `}', then there is a non-trivial initializer. */
11623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11625 /* Parse the initializer list. */
11626 CONSTRUCTOR_ELTS (initializer)
11627 = cp_parser_initializer_list (parser, non_constant_p);
11628 /* A trailing `,' token is allowed. */
11629 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11630 cp_lexer_consume_token (parser->lexer);
11632 /* Now, there should be a trailing `}'. */
11633 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11636 return initializer;
11639 /* Parse an initializer-list.
11641 initializer-list:
11642 initializer-clause
11643 initializer-list , initializer-clause
11645 GNU Extension:
11647 initializer-list:
11648 identifier : initializer-clause
11649 initializer-list, identifier : initializer-clause
11651 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11652 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11653 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11654 as for cp_parser_initializer. */
11656 static tree
11657 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11659 tree initializers = NULL_TREE;
11661 /* Assume all of the expressions are constant. */
11662 *non_constant_p = false;
11664 /* Parse the rest of the list. */
11665 while (true)
11667 cp_token *token;
11668 tree identifier;
11669 tree initializer;
11670 bool clause_non_constant_p;
11672 /* If the next token is an identifier and the following one is a
11673 colon, we are looking at the GNU designated-initializer
11674 syntax. */
11675 if (cp_parser_allow_gnu_extensions_p (parser)
11676 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11677 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11679 /* Consume the identifier. */
11680 identifier = cp_lexer_consume_token (parser->lexer)->value;
11681 /* Consume the `:'. */
11682 cp_lexer_consume_token (parser->lexer);
11684 else
11685 identifier = NULL_TREE;
11687 /* Parse the initializer. */
11688 initializer = cp_parser_initializer_clause (parser,
11689 &clause_non_constant_p);
11690 /* If any clause is non-constant, so is the entire initializer. */
11691 if (clause_non_constant_p)
11692 *non_constant_p = true;
11693 /* Add it to the list. */
11694 initializers = tree_cons (identifier, initializer, initializers);
11696 /* If the next token is not a comma, we have reached the end of
11697 the list. */
11698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11699 break;
11701 /* Peek at the next token. */
11702 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11703 /* If the next token is a `}', then we're still done. An
11704 initializer-clause can have a trailing `,' after the
11705 initializer-list and before the closing `}'. */
11706 if (token->type == CPP_CLOSE_BRACE)
11707 break;
11709 /* Consume the `,' token. */
11710 cp_lexer_consume_token (parser->lexer);
11713 /* The initializers were built up in reverse order, so we need to
11714 reverse them now. */
11715 return nreverse (initializers);
11718 /* Classes [gram.class] */
11720 /* Parse a class-name.
11722 class-name:
11723 identifier
11724 template-id
11726 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11727 to indicate that names looked up in dependent types should be
11728 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11729 keyword has been used to indicate that the name that appears next
11730 is a template. TYPE_P is true iff the next name should be treated
11731 as class-name, even if it is declared to be some other kind of name
11732 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11733 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11734 being defined in a class-head.
11736 Returns the TYPE_DECL representing the class. */
11738 static tree
11739 cp_parser_class_name (cp_parser *parser,
11740 bool typename_keyword_p,
11741 bool template_keyword_p,
11742 bool type_p,
11743 bool check_dependency_p,
11744 bool class_head_p,
11745 bool is_declaration)
11747 tree decl;
11748 tree scope;
11749 bool typename_p;
11750 cp_token *token;
11752 /* All class-names start with an identifier. */
11753 token = cp_lexer_peek_token (parser->lexer);
11754 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11756 cp_parser_error (parser, "expected class-name");
11757 return error_mark_node;
11760 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11761 to a template-id, so we save it here. */
11762 scope = parser->scope;
11763 if (scope == error_mark_node)
11764 return error_mark_node;
11766 /* Any name names a type if we're following the `typename' keyword
11767 in a qualified name where the enclosing scope is type-dependent. */
11768 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11769 && dependent_type_p (scope));
11770 /* Handle the common case (an identifier, but not a template-id)
11771 efficiently. */
11772 if (token->type == CPP_NAME
11773 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11775 tree identifier;
11777 /* Look for the identifier. */
11778 identifier = cp_parser_identifier (parser);
11779 /* If the next token isn't an identifier, we are certainly not
11780 looking at a class-name. */
11781 if (identifier == error_mark_node)
11782 decl = error_mark_node;
11783 /* If we know this is a type-name, there's no need to look it
11784 up. */
11785 else if (typename_p)
11786 decl = identifier;
11787 else
11789 /* If the next token is a `::', then the name must be a type
11790 name.
11792 [basic.lookup.qual]
11794 During the lookup for a name preceding the :: scope
11795 resolution operator, object, function, and enumerator
11796 names are ignored. */
11797 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11798 type_p = true;
11799 /* Look up the name. */
11800 decl = cp_parser_lookup_name (parser, identifier,
11801 type_p,
11802 /*is_template=*/false,
11803 /*is_namespace=*/false,
11804 check_dependency_p);
11807 else
11809 /* Try a template-id. */
11810 decl = cp_parser_template_id (parser, template_keyword_p,
11811 check_dependency_p,
11812 is_declaration);
11813 if (decl == error_mark_node)
11814 return error_mark_node;
11817 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11819 /* If this is a typename, create a TYPENAME_TYPE. */
11820 if (typename_p && decl != error_mark_node)
11822 decl = make_typename_type (scope, decl, /*complain=*/1);
11823 if (decl != error_mark_node)
11824 decl = TYPE_NAME (decl);
11827 /* Check to see that it is really the name of a class. */
11828 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11829 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11830 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11831 /* Situations like this:
11833 template <typename T> struct A {
11834 typename T::template X<int>::I i;
11837 are problematic. Is `T::template X<int>' a class-name? The
11838 standard does not seem to be definitive, but there is no other
11839 valid interpretation of the following `::'. Therefore, those
11840 names are considered class-names. */
11841 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11842 else if (decl == error_mark_node
11843 || TREE_CODE (decl) != TYPE_DECL
11844 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11846 cp_parser_error (parser, "expected class-name");
11847 return error_mark_node;
11850 return decl;
11853 /* Parse a class-specifier.
11855 class-specifier:
11856 class-head { member-specification [opt] }
11858 Returns the TREE_TYPE representing the class. */
11860 static tree
11861 cp_parser_class_specifier (cp_parser* parser)
11863 cp_token *token;
11864 tree type;
11865 tree attributes;
11866 int has_trailing_semicolon;
11867 bool nested_name_specifier_p;
11868 unsigned saved_num_template_parameter_lists;
11869 bool pop_p = false;
11870 tree scope = NULL_TREE;
11872 push_deferring_access_checks (dk_no_deferred);
11874 /* Parse the class-head. */
11875 type = cp_parser_class_head (parser,
11876 &nested_name_specifier_p,
11877 &attributes);
11878 /* If the class-head was a semantic disaster, skip the entire body
11879 of the class. */
11880 if (!type)
11882 cp_parser_skip_to_end_of_block_or_statement (parser);
11883 pop_deferring_access_checks ();
11884 return error_mark_node;
11887 /* Look for the `{'. */
11888 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11890 pop_deferring_access_checks ();
11891 return error_mark_node;
11894 /* Issue an error message if type-definitions are forbidden here. */
11895 cp_parser_check_type_definition (parser);
11896 /* Remember that we are defining one more class. */
11897 ++parser->num_classes_being_defined;
11898 /* Inside the class, surrounding template-parameter-lists do not
11899 apply. */
11900 saved_num_template_parameter_lists
11901 = parser->num_template_parameter_lists;
11902 parser->num_template_parameter_lists = 0;
11904 /* Start the class. */
11905 if (nested_name_specifier_p)
11907 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
11908 pop_p = push_scope (scope);
11910 type = begin_class_definition (type);
11911 if (type == error_mark_node)
11912 /* If the type is erroneous, skip the entire body of the class. */
11913 cp_parser_skip_to_closing_brace (parser);
11914 else
11915 /* Parse the member-specification. */
11916 cp_parser_member_specification_opt (parser);
11917 /* Look for the trailing `}'. */
11918 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11919 /* We get better error messages by noticing a common problem: a
11920 missing trailing `;'. */
11921 token = cp_lexer_peek_token (parser->lexer);
11922 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11923 /* Look for trailing attributes to apply to this class. */
11924 if (cp_parser_allow_gnu_extensions_p (parser))
11926 tree sub_attr = cp_parser_attributes_opt (parser);
11927 attributes = chainon (attributes, sub_attr);
11929 if (type != error_mark_node)
11930 type = finish_struct (type, attributes);
11931 if (pop_p)
11932 pop_scope (scope);
11933 /* If this class is not itself within the scope of another class,
11934 then we need to parse the bodies of all of the queued function
11935 definitions. Note that the queued functions defined in a class
11936 are not always processed immediately following the
11937 class-specifier for that class. Consider:
11939 struct A {
11940 struct B { void f() { sizeof (A); } };
11943 If `f' were processed before the processing of `A' were
11944 completed, there would be no way to compute the size of `A'.
11945 Note that the nesting we are interested in here is lexical --
11946 not the semantic nesting given by TYPE_CONTEXT. In particular,
11947 for:
11949 struct A { struct B; };
11950 struct A::B { void f() { } };
11952 there is no need to delay the parsing of `A::B::f'. */
11953 if (--parser->num_classes_being_defined == 0)
11955 tree queue_entry;
11956 tree fn;
11958 /* In a first pass, parse default arguments to the functions.
11959 Then, in a second pass, parse the bodies of the functions.
11960 This two-phased approach handles cases like:
11962 struct S {
11963 void f() { g(); }
11964 void g(int i = 3);
11968 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11969 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11970 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11971 TREE_PURPOSE (parser->unparsed_functions_queues)
11972 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11974 fn = TREE_VALUE (queue_entry);
11975 /* Make sure that any template parameters are in scope. */
11976 maybe_begin_member_template_processing (fn);
11977 /* If there are default arguments that have not yet been processed,
11978 take care of them now. */
11979 cp_parser_late_parsing_default_args (parser, fn);
11980 /* Remove any template parameters from the symbol table. */
11981 maybe_end_member_template_processing ();
11983 /* Now parse the body of the functions. */
11984 for (TREE_VALUE (parser->unparsed_functions_queues)
11985 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11986 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11987 TREE_VALUE (parser->unparsed_functions_queues)
11988 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11990 /* Figure out which function we need to process. */
11991 fn = TREE_VALUE (queue_entry);
11993 /* A hack to prevent garbage collection. */
11994 function_depth++;
11996 /* Parse the function. */
11997 cp_parser_late_parsing_for_member (parser, fn);
11998 function_depth--;
12003 /* Put back any saved access checks. */
12004 pop_deferring_access_checks ();
12006 /* Restore the count of active template-parameter-lists. */
12007 parser->num_template_parameter_lists
12008 = saved_num_template_parameter_lists;
12010 return type;
12013 /* Parse a class-head.
12015 class-head:
12016 class-key identifier [opt] base-clause [opt]
12017 class-key nested-name-specifier identifier base-clause [opt]
12018 class-key nested-name-specifier [opt] template-id
12019 base-clause [opt]
12021 GNU Extensions:
12022 class-key attributes identifier [opt] base-clause [opt]
12023 class-key attributes nested-name-specifier identifier base-clause [opt]
12024 class-key attributes nested-name-specifier [opt] template-id
12025 base-clause [opt]
12027 Returns the TYPE of the indicated class. Sets
12028 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12029 involving a nested-name-specifier was used, and FALSE otherwise.
12031 Returns NULL_TREE if the class-head is syntactically valid, but
12032 semantically invalid in a way that means we should skip the entire
12033 body of the class. */
12035 static tree
12036 cp_parser_class_head (cp_parser* parser,
12037 bool* nested_name_specifier_p,
12038 tree *attributes_p)
12040 cp_token *token;
12041 tree nested_name_specifier;
12042 enum tag_types class_key;
12043 tree id = NULL_TREE;
12044 tree type = NULL_TREE;
12045 tree attributes;
12046 bool template_id_p = false;
12047 bool qualified_p = false;
12048 bool invalid_nested_name_p = false;
12049 bool invalid_explicit_specialization_p = false;
12050 bool pop_p = false;
12051 unsigned num_templates;
12053 /* Assume no nested-name-specifier will be present. */
12054 *nested_name_specifier_p = false;
12055 /* Assume no template parameter lists will be used in defining the
12056 type. */
12057 num_templates = 0;
12059 /* Look for the class-key. */
12060 class_key = cp_parser_class_key (parser);
12061 if (class_key == none_type)
12062 return error_mark_node;
12064 /* Parse the attributes. */
12065 attributes = cp_parser_attributes_opt (parser);
12067 /* If the next token is `::', that is invalid -- but sometimes
12068 people do try to write:
12070 struct ::S {};
12072 Handle this gracefully by accepting the extra qualifier, and then
12073 issuing an error about it later if this really is a
12074 class-head. If it turns out just to be an elaborated type
12075 specifier, remain silent. */
12076 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12077 qualified_p = true;
12079 push_deferring_access_checks (dk_no_check);
12081 /* Determine the name of the class. Begin by looking for an
12082 optional nested-name-specifier. */
12083 nested_name_specifier
12084 = cp_parser_nested_name_specifier_opt (parser,
12085 /*typename_keyword_p=*/false,
12086 /*check_dependency_p=*/false,
12087 /*type_p=*/false,
12088 /*is_declaration=*/false);
12089 /* If there was a nested-name-specifier, then there *must* be an
12090 identifier. */
12091 if (nested_name_specifier)
12093 /* Although the grammar says `identifier', it really means
12094 `class-name' or `template-name'. You are only allowed to
12095 define a class that has already been declared with this
12096 syntax.
12098 The proposed resolution for Core Issue 180 says that whever
12099 you see `class T::X' you should treat `X' as a type-name.
12101 It is OK to define an inaccessible class; for example:
12103 class A { class B; };
12104 class A::B {};
12106 We do not know if we will see a class-name, or a
12107 template-name. We look for a class-name first, in case the
12108 class-name is a template-id; if we looked for the
12109 template-name first we would stop after the template-name. */
12110 cp_parser_parse_tentatively (parser);
12111 type = cp_parser_class_name (parser,
12112 /*typename_keyword_p=*/false,
12113 /*template_keyword_p=*/false,
12114 /*type_p=*/true,
12115 /*check_dependency_p=*/false,
12116 /*class_head_p=*/true,
12117 /*is_declaration=*/false);
12118 /* If that didn't work, ignore the nested-name-specifier. */
12119 if (!cp_parser_parse_definitely (parser))
12121 invalid_nested_name_p = true;
12122 id = cp_parser_identifier (parser);
12123 if (id == error_mark_node)
12124 id = NULL_TREE;
12126 /* If we could not find a corresponding TYPE, treat this
12127 declaration like an unqualified declaration. */
12128 if (type == error_mark_node)
12129 nested_name_specifier = NULL_TREE;
12130 /* Otherwise, count the number of templates used in TYPE and its
12131 containing scopes. */
12132 else
12134 tree scope;
12136 for (scope = TREE_TYPE (type);
12137 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12138 scope = (TYPE_P (scope)
12139 ? TYPE_CONTEXT (scope)
12140 : DECL_CONTEXT (scope)))
12141 if (TYPE_P (scope)
12142 && CLASS_TYPE_P (scope)
12143 && CLASSTYPE_TEMPLATE_INFO (scope)
12144 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12145 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12146 ++num_templates;
12149 /* Otherwise, the identifier is optional. */
12150 else
12152 /* We don't know whether what comes next is a template-id,
12153 an identifier, or nothing at all. */
12154 cp_parser_parse_tentatively (parser);
12155 /* Check for a template-id. */
12156 id = cp_parser_template_id (parser,
12157 /*template_keyword_p=*/false,
12158 /*check_dependency_p=*/true,
12159 /*is_declaration=*/true);
12160 /* If that didn't work, it could still be an identifier. */
12161 if (!cp_parser_parse_definitely (parser))
12163 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12164 id = cp_parser_identifier (parser);
12165 else
12166 id = NULL_TREE;
12168 else
12170 template_id_p = true;
12171 ++num_templates;
12175 pop_deferring_access_checks ();
12177 if (id)
12178 cp_parser_check_for_invalid_template_id (parser, id);
12180 /* If it's not a `:' or a `{' then we can't really be looking at a
12181 class-head, since a class-head only appears as part of a
12182 class-specifier. We have to detect this situation before calling
12183 xref_tag, since that has irreversible side-effects. */
12184 if (!cp_parser_next_token_starts_class_definition_p (parser))
12186 cp_parser_error (parser, "expected `{' or `:'");
12187 return error_mark_node;
12190 /* At this point, we're going ahead with the class-specifier, even
12191 if some other problem occurs. */
12192 cp_parser_commit_to_tentative_parse (parser);
12193 /* Issue the error about the overly-qualified name now. */
12194 if (qualified_p)
12195 cp_parser_error (parser,
12196 "global qualification of class name is invalid");
12197 else if (invalid_nested_name_p)
12198 cp_parser_error (parser,
12199 "qualified name does not name a class");
12200 else if (nested_name_specifier)
12202 tree scope;
12204 /* Reject typedef-names in class heads. */
12205 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12207 error ("invalid class name in declaration of `%D'", type);
12208 type = NULL_TREE;
12209 goto done;
12212 /* Figure out in what scope the declaration is being placed. */
12213 scope = current_scope ();
12214 if (!scope)
12215 scope = current_namespace;
12216 /* If that scope does not contain the scope in which the
12217 class was originally declared, the program is invalid. */
12218 if (scope && !is_ancestor (scope, nested_name_specifier))
12220 error ("declaration of `%D' in `%D' which does not "
12221 "enclose `%D'", type, scope, nested_name_specifier);
12222 type = NULL_TREE;
12223 goto done;
12225 /* [dcl.meaning]
12227 A declarator-id shall not be qualified exception of the
12228 definition of a ... nested class outside of its class
12229 ... [or] a the definition or explicit instantiation of a
12230 class member of a namespace outside of its namespace. */
12231 if (scope == nested_name_specifier)
12233 pedwarn ("extra qualification ignored");
12234 nested_name_specifier = NULL_TREE;
12235 num_templates = 0;
12238 /* An explicit-specialization must be preceded by "template <>". If
12239 it is not, try to recover gracefully. */
12240 if (at_namespace_scope_p ()
12241 && parser->num_template_parameter_lists == 0
12242 && template_id_p)
12244 error ("an explicit specialization must be preceded by 'template <>'");
12245 invalid_explicit_specialization_p = true;
12246 /* Take the same action that would have been taken by
12247 cp_parser_explicit_specialization. */
12248 ++parser->num_template_parameter_lists;
12249 begin_specialization ();
12251 /* There must be no "return" statements between this point and the
12252 end of this function; set "type "to the correct return value and
12253 use "goto done;" to return. */
12254 /* Make sure that the right number of template parameters were
12255 present. */
12256 if (!cp_parser_check_template_parameters (parser, num_templates))
12258 /* If something went wrong, there is no point in even trying to
12259 process the class-definition. */
12260 type = NULL_TREE;
12261 goto done;
12264 /* Look up the type. */
12265 if (template_id_p)
12267 type = TREE_TYPE (id);
12268 maybe_process_partial_specialization (type);
12270 else if (!nested_name_specifier)
12272 /* If the class was unnamed, create a dummy name. */
12273 if (!id)
12274 id = make_anon_name ();
12275 type = xref_tag (class_key, id, /*globalize=*/false,
12276 parser->num_template_parameter_lists);
12278 else
12280 tree class_type;
12281 bool pop_p = false;
12283 /* Given:
12285 template <typename T> struct S { struct T };
12286 template <typename T> struct S<T>::T { };
12288 we will get a TYPENAME_TYPE when processing the definition of
12289 `S::T'. We need to resolve it to the actual type before we
12290 try to define it. */
12291 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12293 class_type = resolve_typename_type (TREE_TYPE (type),
12294 /*only_current_p=*/false);
12295 if (class_type != error_mark_node)
12296 type = TYPE_NAME (class_type);
12297 else
12299 cp_parser_error (parser, "could not resolve typename type");
12300 type = error_mark_node;
12304 maybe_process_partial_specialization (TREE_TYPE (type));
12305 class_type = current_class_type;
12306 /* Enter the scope indicated by the nested-name-specifier. */
12307 if (nested_name_specifier)
12308 pop_p = push_scope (nested_name_specifier);
12309 /* Get the canonical version of this type. */
12310 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12311 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12312 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12313 type = push_template_decl (type);
12314 type = TREE_TYPE (type);
12315 if (nested_name_specifier)
12317 *nested_name_specifier_p = true;
12318 if (pop_p)
12319 pop_scope (nested_name_specifier);
12322 /* Indicate whether this class was declared as a `class' or as a
12323 `struct'. */
12324 if (TREE_CODE (type) == RECORD_TYPE)
12325 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12326 cp_parser_check_class_key (class_key, type);
12328 /* Enter the scope containing the class; the names of base classes
12329 should be looked up in that context. For example, given:
12331 struct A { struct B {}; struct C; };
12332 struct A::C : B {};
12334 is valid. */
12335 if (nested_name_specifier)
12336 pop_p = push_scope (nested_name_specifier);
12337 /* Now, look for the base-clause. */
12338 token = cp_lexer_peek_token (parser->lexer);
12339 if (token->type == CPP_COLON)
12341 tree bases;
12343 /* Get the list of base-classes. */
12344 bases = cp_parser_base_clause (parser);
12345 /* Process them. */
12346 xref_basetypes (type, bases);
12348 /* Leave the scope given by the nested-name-specifier. We will
12349 enter the class scope itself while processing the members. */
12350 if (pop_p)
12351 pop_scope (nested_name_specifier);
12353 done:
12354 if (invalid_explicit_specialization_p)
12356 end_specialization ();
12357 --parser->num_template_parameter_lists;
12359 *attributes_p = attributes;
12360 return type;
12363 /* Parse a class-key.
12365 class-key:
12366 class
12367 struct
12368 union
12370 Returns the kind of class-key specified, or none_type to indicate
12371 error. */
12373 static enum tag_types
12374 cp_parser_class_key (cp_parser* parser)
12376 cp_token *token;
12377 enum tag_types tag_type;
12379 /* Look for the class-key. */
12380 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12381 if (!token)
12382 return none_type;
12384 /* Check to see if the TOKEN is a class-key. */
12385 tag_type = cp_parser_token_is_class_key (token);
12386 if (!tag_type)
12387 cp_parser_error (parser, "expected class-key");
12388 return tag_type;
12391 /* Parse an (optional) member-specification.
12393 member-specification:
12394 member-declaration member-specification [opt]
12395 access-specifier : member-specification [opt] */
12397 static void
12398 cp_parser_member_specification_opt (cp_parser* parser)
12400 while (true)
12402 cp_token *token;
12403 enum rid keyword;
12405 /* Peek at the next token. */
12406 token = cp_lexer_peek_token (parser->lexer);
12407 /* If it's a `}', or EOF then we've seen all the members. */
12408 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12409 break;
12411 /* See if this token is a keyword. */
12412 keyword = token->keyword;
12413 switch (keyword)
12415 case RID_PUBLIC:
12416 case RID_PROTECTED:
12417 case RID_PRIVATE:
12418 /* Consume the access-specifier. */
12419 cp_lexer_consume_token (parser->lexer);
12420 /* Remember which access-specifier is active. */
12421 current_access_specifier = token->value;
12422 /* Look for the `:'. */
12423 cp_parser_require (parser, CPP_COLON, "`:'");
12424 break;
12426 default:
12427 /* Otherwise, the next construction must be a
12428 member-declaration. */
12429 cp_parser_member_declaration (parser);
12434 /* Parse a member-declaration.
12436 member-declaration:
12437 decl-specifier-seq [opt] member-declarator-list [opt] ;
12438 function-definition ; [opt]
12439 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12440 using-declaration
12441 template-declaration
12443 member-declarator-list:
12444 member-declarator
12445 member-declarator-list , member-declarator
12447 member-declarator:
12448 declarator pure-specifier [opt]
12449 declarator constant-initializer [opt]
12450 identifier [opt] : constant-expression
12452 GNU Extensions:
12454 member-declaration:
12455 __extension__ member-declaration
12457 member-declarator:
12458 declarator attributes [opt] pure-specifier [opt]
12459 declarator attributes [opt] constant-initializer [opt]
12460 identifier [opt] attributes [opt] : constant-expression */
12462 static void
12463 cp_parser_member_declaration (cp_parser* parser)
12465 tree decl_specifiers;
12466 tree prefix_attributes;
12467 tree decl;
12468 int declares_class_or_enum;
12469 bool friend_p;
12470 cp_token *token;
12471 int saved_pedantic;
12473 /* Check for the `__extension__' keyword. */
12474 if (cp_parser_extension_opt (parser, &saved_pedantic))
12476 /* Recurse. */
12477 cp_parser_member_declaration (parser);
12478 /* Restore the old value of the PEDANTIC flag. */
12479 pedantic = saved_pedantic;
12481 return;
12484 /* Check for a template-declaration. */
12485 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12487 /* An explicit specialization here is an error condition, and we
12488 expect the specialization handler to detect and report this. */
12489 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12490 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12491 cp_parser_explicit_specialization (parser);
12492 else
12493 cp_parser_template_declaration (parser, /*member_p=*/true);
12495 return;
12498 /* Check for a using-declaration. */
12499 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12501 /* Parse the using-declaration. */
12502 cp_parser_using_declaration (parser);
12504 return;
12507 /* Parse the decl-specifier-seq. */
12508 decl_specifiers
12509 = cp_parser_decl_specifier_seq (parser,
12510 CP_PARSER_FLAGS_OPTIONAL,
12511 &prefix_attributes,
12512 &declares_class_or_enum);
12513 /* Check for an invalid type-name. */
12514 if (cp_parser_diagnose_invalid_type_name (parser))
12515 return;
12516 /* If there is no declarator, then the decl-specifier-seq should
12517 specify a type. */
12518 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12520 /* If there was no decl-specifier-seq, and the next token is a
12521 `;', then we have something like:
12523 struct S { ; };
12525 [class.mem]
12527 Each member-declaration shall declare at least one member
12528 name of the class. */
12529 if (!decl_specifiers)
12531 if (pedantic)
12532 pedwarn ("extra semicolon");
12534 else
12536 tree type;
12538 /* See if this declaration is a friend. */
12539 friend_p = cp_parser_friend_p (decl_specifiers);
12540 /* If there were decl-specifiers, check to see if there was
12541 a class-declaration. */
12542 type = check_tag_decl (decl_specifiers);
12543 /* Nested classes have already been added to the class, but
12544 a `friend' needs to be explicitly registered. */
12545 if (friend_p)
12547 /* If the `friend' keyword was present, the friend must
12548 be introduced with a class-key. */
12549 if (!declares_class_or_enum)
12550 error ("a class-key must be used when declaring a friend");
12551 /* In this case:
12553 template <typename T> struct A {
12554 friend struct A<T>::B;
12557 A<T>::B will be represented by a TYPENAME_TYPE, and
12558 therefore not recognized by check_tag_decl. */
12559 if (!type)
12561 tree specifier;
12563 for (specifier = decl_specifiers;
12564 specifier;
12565 specifier = TREE_CHAIN (specifier))
12567 tree s = TREE_VALUE (specifier);
12569 if (TREE_CODE (s) == IDENTIFIER_NODE)
12570 get_global_value_if_present (s, &type);
12571 if (TREE_CODE (s) == TYPE_DECL)
12572 s = TREE_TYPE (s);
12573 if (TYPE_P (s))
12575 type = s;
12576 break;
12580 if (!type || !TYPE_P (type))
12581 error ("friend declaration does not name a class or "
12582 "function");
12583 else
12584 make_friend_class (current_class_type, type,
12585 /*complain=*/true);
12587 /* If there is no TYPE, an error message will already have
12588 been issued. */
12589 else if (!type)
12591 /* An anonymous aggregate has to be handled specially; such
12592 a declaration really declares a data member (with a
12593 particular type), as opposed to a nested class. */
12594 else if (ANON_AGGR_TYPE_P (type))
12596 /* Remove constructors and such from TYPE, now that we
12597 know it is an anonymous aggregate. */
12598 fixup_anonymous_aggr (type);
12599 /* And make the corresponding data member. */
12600 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12601 /* Add it to the class. */
12602 finish_member_declaration (decl);
12604 else
12605 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12608 else
12610 /* See if these declarations will be friends. */
12611 friend_p = cp_parser_friend_p (decl_specifiers);
12613 /* Keep going until we hit the `;' at the end of the
12614 declaration. */
12615 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12617 tree attributes = NULL_TREE;
12618 tree first_attribute;
12620 /* Peek at the next token. */
12621 token = cp_lexer_peek_token (parser->lexer);
12623 /* Check for a bitfield declaration. */
12624 if (token->type == CPP_COLON
12625 || (token->type == CPP_NAME
12626 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12627 == CPP_COLON))
12629 tree identifier;
12630 tree width;
12632 /* Get the name of the bitfield. Note that we cannot just
12633 check TOKEN here because it may have been invalidated by
12634 the call to cp_lexer_peek_nth_token above. */
12635 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12636 identifier = cp_parser_identifier (parser);
12637 else
12638 identifier = NULL_TREE;
12640 /* Consume the `:' token. */
12641 cp_lexer_consume_token (parser->lexer);
12642 /* Get the width of the bitfield. */
12643 width
12644 = cp_parser_constant_expression (parser,
12645 /*allow_non_constant=*/false,
12646 NULL);
12648 /* Look for attributes that apply to the bitfield. */
12649 attributes = cp_parser_attributes_opt (parser);
12650 /* Remember which attributes are prefix attributes and
12651 which are not. */
12652 first_attribute = attributes;
12653 /* Combine the attributes. */
12654 attributes = chainon (prefix_attributes, attributes);
12656 /* Create the bitfield declaration. */
12657 decl = grokbitfield (identifier,
12658 decl_specifiers,
12659 width);
12660 /* Apply the attributes. */
12661 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12663 else
12665 tree declarator;
12666 tree initializer;
12667 tree asm_specification;
12668 int ctor_dtor_or_conv_p;
12670 /* Parse the declarator. */
12671 declarator
12672 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12673 &ctor_dtor_or_conv_p,
12674 /*parenthesized_p=*/NULL,
12675 /*member_p=*/true);
12677 /* If something went wrong parsing the declarator, make sure
12678 that we at least consume some tokens. */
12679 if (declarator == error_mark_node)
12681 /* Skip to the end of the statement. */
12682 cp_parser_skip_to_end_of_statement (parser);
12683 /* If the next token is not a semicolon, that is
12684 probably because we just skipped over the body of
12685 a function. So, we consume a semicolon if
12686 present, but do not issue an error message if it
12687 is not present. */
12688 if (cp_lexer_next_token_is (parser->lexer,
12689 CPP_SEMICOLON))
12690 cp_lexer_consume_token (parser->lexer);
12691 return;
12694 if (declares_class_or_enum & 2)
12695 cp_parser_check_for_definition_in_return_type
12696 (declarator, TREE_VALUE (decl_specifiers));
12698 /* Look for an asm-specification. */
12699 asm_specification = cp_parser_asm_specification_opt (parser);
12700 /* Look for attributes that apply to the declaration. */
12701 attributes = cp_parser_attributes_opt (parser);
12702 /* Remember which attributes are prefix attributes and
12703 which are not. */
12704 first_attribute = attributes;
12705 /* Combine the attributes. */
12706 attributes = chainon (prefix_attributes, attributes);
12708 /* If it's an `=', then we have a constant-initializer or a
12709 pure-specifier. It is not correct to parse the
12710 initializer before registering the member declaration
12711 since the member declaration should be in scope while
12712 its initializer is processed. However, the rest of the
12713 front end does not yet provide an interface that allows
12714 us to handle this correctly. */
12715 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12717 /* In [class.mem]:
12719 A pure-specifier shall be used only in the declaration of
12720 a virtual function.
12722 A member-declarator can contain a constant-initializer
12723 only if it declares a static member of integral or
12724 enumeration type.
12726 Therefore, if the DECLARATOR is for a function, we look
12727 for a pure-specifier; otherwise, we look for a
12728 constant-initializer. When we call `grokfield', it will
12729 perform more stringent semantics checks. */
12730 if (TREE_CODE (declarator) == CALL_EXPR)
12731 initializer = cp_parser_pure_specifier (parser);
12732 else
12733 /* Parse the initializer. */
12734 initializer = cp_parser_constant_initializer (parser);
12736 /* Otherwise, there is no initializer. */
12737 else
12738 initializer = NULL_TREE;
12740 /* See if we are probably looking at a function
12741 definition. We are certainly not looking at at a
12742 member-declarator. Calling `grokfield' has
12743 side-effects, so we must not do it unless we are sure
12744 that we are looking at a member-declarator. */
12745 if (cp_parser_token_starts_function_definition_p
12746 (cp_lexer_peek_token (parser->lexer)))
12748 /* The grammar does not allow a pure-specifier to be
12749 used when a member function is defined. (It is
12750 possible that this fact is an oversight in the
12751 standard, since a pure function may be defined
12752 outside of the class-specifier. */
12753 if (initializer)
12754 error ("pure-specifier on function-definition");
12755 decl = cp_parser_save_member_function_body (parser,
12756 decl_specifiers,
12757 declarator,
12758 attributes);
12759 /* If the member was not a friend, declare it here. */
12760 if (!friend_p)
12761 finish_member_declaration (decl);
12762 /* Peek at the next token. */
12763 token = cp_lexer_peek_token (parser->lexer);
12764 /* If the next token is a semicolon, consume it. */
12765 if (token->type == CPP_SEMICOLON)
12766 cp_lexer_consume_token (parser->lexer);
12767 return;
12769 else
12771 /* Create the declaration. */
12772 decl = grokfield (declarator, decl_specifiers,
12773 initializer, asm_specification,
12774 attributes);
12775 /* Any initialization must have been from a
12776 constant-expression. */
12777 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12778 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12782 /* Reset PREFIX_ATTRIBUTES. */
12783 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12784 attributes = TREE_CHAIN (attributes);
12785 if (attributes)
12786 TREE_CHAIN (attributes) = NULL_TREE;
12788 /* If there is any qualification still in effect, clear it
12789 now; we will be starting fresh with the next declarator. */
12790 parser->scope = NULL_TREE;
12791 parser->qualifying_scope = NULL_TREE;
12792 parser->object_scope = NULL_TREE;
12793 /* If it's a `,', then there are more declarators. */
12794 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12795 cp_lexer_consume_token (parser->lexer);
12796 /* If the next token isn't a `;', then we have a parse error. */
12797 else if (cp_lexer_next_token_is_not (parser->lexer,
12798 CPP_SEMICOLON))
12800 cp_parser_error (parser, "expected `;'");
12801 /* Skip tokens until we find a `;'. */
12802 cp_parser_skip_to_end_of_statement (parser);
12804 break;
12807 if (decl)
12809 /* Add DECL to the list of members. */
12810 if (!friend_p)
12811 finish_member_declaration (decl);
12813 if (TREE_CODE (decl) == FUNCTION_DECL)
12814 cp_parser_save_default_args (parser, decl);
12819 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12822 /* Parse a pure-specifier.
12824 pure-specifier:
12827 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12828 Otherwise, ERROR_MARK_NODE is returned. */
12830 static tree
12831 cp_parser_pure_specifier (cp_parser* parser)
12833 cp_token *token;
12835 /* Look for the `=' token. */
12836 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12837 return error_mark_node;
12838 /* Look for the `0' token. */
12839 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12840 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12841 to get information from the lexer about how the number was
12842 spelled in order to fix this problem. */
12843 if (!token || !integer_zerop (token->value))
12844 return error_mark_node;
12846 return integer_zero_node;
12849 /* Parse a constant-initializer.
12851 constant-initializer:
12852 = constant-expression
12854 Returns a representation of the constant-expression. */
12856 static tree
12857 cp_parser_constant_initializer (cp_parser* parser)
12859 /* Look for the `=' token. */
12860 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12861 return error_mark_node;
12863 /* It is invalid to write:
12865 struct S { static const int i = { 7 }; };
12868 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12870 cp_parser_error (parser,
12871 "a brace-enclosed initializer is not allowed here");
12872 /* Consume the opening brace. */
12873 cp_lexer_consume_token (parser->lexer);
12874 /* Skip the initializer. */
12875 cp_parser_skip_to_closing_brace (parser);
12876 /* Look for the trailing `}'. */
12877 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12879 return error_mark_node;
12882 return cp_parser_constant_expression (parser,
12883 /*allow_non_constant=*/false,
12884 NULL);
12887 /* Derived classes [gram.class.derived] */
12889 /* Parse a base-clause.
12891 base-clause:
12892 : base-specifier-list
12894 base-specifier-list:
12895 base-specifier
12896 base-specifier-list , base-specifier
12898 Returns a TREE_LIST representing the base-classes, in the order in
12899 which they were declared. The representation of each node is as
12900 described by cp_parser_base_specifier.
12902 In the case that no bases are specified, this function will return
12903 NULL_TREE, not ERROR_MARK_NODE. */
12905 static tree
12906 cp_parser_base_clause (cp_parser* parser)
12908 tree bases = NULL_TREE;
12910 /* Look for the `:' that begins the list. */
12911 cp_parser_require (parser, CPP_COLON, "`:'");
12913 /* Scan the base-specifier-list. */
12914 while (true)
12916 cp_token *token;
12917 tree base;
12919 /* Look for the base-specifier. */
12920 base = cp_parser_base_specifier (parser);
12921 /* Add BASE to the front of the list. */
12922 if (base != error_mark_node)
12924 TREE_CHAIN (base) = bases;
12925 bases = base;
12927 /* Peek at the next token. */
12928 token = cp_lexer_peek_token (parser->lexer);
12929 /* If it's not a comma, then the list is complete. */
12930 if (token->type != CPP_COMMA)
12931 break;
12932 /* Consume the `,'. */
12933 cp_lexer_consume_token (parser->lexer);
12936 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12937 base class had a qualified name. However, the next name that
12938 appears is certainly not qualified. */
12939 parser->scope = NULL_TREE;
12940 parser->qualifying_scope = NULL_TREE;
12941 parser->object_scope = NULL_TREE;
12943 return nreverse (bases);
12946 /* Parse a base-specifier.
12948 base-specifier:
12949 :: [opt] nested-name-specifier [opt] class-name
12950 virtual access-specifier [opt] :: [opt] nested-name-specifier
12951 [opt] class-name
12952 access-specifier virtual [opt] :: [opt] nested-name-specifier
12953 [opt] class-name
12955 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12956 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12957 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12958 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12960 static tree
12961 cp_parser_base_specifier (cp_parser* parser)
12963 cp_token *token;
12964 bool done = false;
12965 bool virtual_p = false;
12966 bool duplicate_virtual_error_issued_p = false;
12967 bool duplicate_access_error_issued_p = false;
12968 bool class_scope_p, template_p;
12969 tree access = access_default_node;
12970 tree type;
12972 /* Process the optional `virtual' and `access-specifier'. */
12973 while (!done)
12975 /* Peek at the next token. */
12976 token = cp_lexer_peek_token (parser->lexer);
12977 /* Process `virtual'. */
12978 switch (token->keyword)
12980 case RID_VIRTUAL:
12981 /* If `virtual' appears more than once, issue an error. */
12982 if (virtual_p && !duplicate_virtual_error_issued_p)
12984 cp_parser_error (parser,
12985 "`virtual' specified more than once in base-specified");
12986 duplicate_virtual_error_issued_p = true;
12989 virtual_p = true;
12991 /* Consume the `virtual' token. */
12992 cp_lexer_consume_token (parser->lexer);
12994 break;
12996 case RID_PUBLIC:
12997 case RID_PROTECTED:
12998 case RID_PRIVATE:
12999 /* If more than one access specifier appears, issue an
13000 error. */
13001 if (access != access_default_node
13002 && !duplicate_access_error_issued_p)
13004 cp_parser_error (parser,
13005 "more than one access specifier in base-specified");
13006 duplicate_access_error_issued_p = true;
13009 access = ridpointers[(int) token->keyword];
13011 /* Consume the access-specifier. */
13012 cp_lexer_consume_token (parser->lexer);
13014 break;
13016 default:
13017 done = true;
13018 break;
13021 /* It is not uncommon to see programs mechanically, errouneously, use
13022 the 'typename' keyword to denote (dependent) qualified types
13023 as base classes. */
13024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13026 if (!processing_template_decl)
13027 error ("keyword `typename' not allowed outside of templates");
13028 else
13029 error ("keyword `typename' not allowed in this context "
13030 "(the base class is implicitly a type)");
13031 cp_lexer_consume_token (parser->lexer);
13034 /* Look for the optional `::' operator. */
13035 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13036 /* Look for the nested-name-specifier. The simplest way to
13037 implement:
13039 [temp.res]
13041 The keyword `typename' is not permitted in a base-specifier or
13042 mem-initializer; in these contexts a qualified name that
13043 depends on a template-parameter is implicitly assumed to be a
13044 type name.
13046 is to pretend that we have seen the `typename' keyword at this
13047 point. */
13048 cp_parser_nested_name_specifier_opt (parser,
13049 /*typename_keyword_p=*/true,
13050 /*check_dependency_p=*/true,
13051 /*type_p=*/true,
13052 /*is_declaration=*/true);
13053 /* If the base class is given by a qualified name, assume that names
13054 we see are type names or templates, as appropriate. */
13055 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13056 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13058 /* Finally, look for the class-name. */
13059 type = cp_parser_class_name (parser,
13060 class_scope_p,
13061 template_p,
13062 /*type_p=*/true,
13063 /*check_dependency_p=*/true,
13064 /*class_head_p=*/false,
13065 /*is_declaration=*/true);
13067 if (type == error_mark_node)
13068 return error_mark_node;
13070 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13073 /* Exception handling [gram.exception] */
13075 /* Parse an (optional) exception-specification.
13077 exception-specification:
13078 throw ( type-id-list [opt] )
13080 Returns a TREE_LIST representing the exception-specification. The
13081 TREE_VALUE of each node is a type. */
13083 static tree
13084 cp_parser_exception_specification_opt (cp_parser* parser)
13086 cp_token *token;
13087 tree type_id_list;
13089 /* Peek at the next token. */
13090 token = cp_lexer_peek_token (parser->lexer);
13091 /* If it's not `throw', then there's no exception-specification. */
13092 if (!cp_parser_is_keyword (token, RID_THROW))
13093 return NULL_TREE;
13095 /* Consume the `throw'. */
13096 cp_lexer_consume_token (parser->lexer);
13098 /* Look for the `('. */
13099 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13101 /* Peek at the next token. */
13102 token = cp_lexer_peek_token (parser->lexer);
13103 /* If it's not a `)', then there is a type-id-list. */
13104 if (token->type != CPP_CLOSE_PAREN)
13106 const char *saved_message;
13108 /* Types may not be defined in an exception-specification. */
13109 saved_message = parser->type_definition_forbidden_message;
13110 parser->type_definition_forbidden_message
13111 = "types may not be defined in an exception-specification";
13112 /* Parse the type-id-list. */
13113 type_id_list = cp_parser_type_id_list (parser);
13114 /* Restore the saved message. */
13115 parser->type_definition_forbidden_message = saved_message;
13117 else
13118 type_id_list = empty_except_spec;
13120 /* Look for the `)'. */
13121 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13123 return type_id_list;
13126 /* Parse an (optional) type-id-list.
13128 type-id-list:
13129 type-id
13130 type-id-list , type-id
13132 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13133 in the order that the types were presented. */
13135 static tree
13136 cp_parser_type_id_list (cp_parser* parser)
13138 tree types = NULL_TREE;
13140 while (true)
13142 cp_token *token;
13143 tree type;
13145 /* Get the next type-id. */
13146 type = cp_parser_type_id (parser);
13147 /* Add it to the list. */
13148 types = add_exception_specifier (types, type, /*complain=*/1);
13149 /* Peek at the next token. */
13150 token = cp_lexer_peek_token (parser->lexer);
13151 /* If it is not a `,', we are done. */
13152 if (token->type != CPP_COMMA)
13153 break;
13154 /* Consume the `,'. */
13155 cp_lexer_consume_token (parser->lexer);
13158 return nreverse (types);
13161 /* Parse a try-block.
13163 try-block:
13164 try compound-statement handler-seq */
13166 static tree
13167 cp_parser_try_block (cp_parser* parser)
13169 tree try_block;
13171 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13172 try_block = begin_try_block ();
13173 cp_parser_compound_statement (parser, false);
13174 finish_try_block (try_block);
13175 cp_parser_handler_seq (parser);
13176 finish_handler_sequence (try_block);
13178 return try_block;
13181 /* Parse a function-try-block.
13183 function-try-block:
13184 try ctor-initializer [opt] function-body handler-seq */
13186 static bool
13187 cp_parser_function_try_block (cp_parser* parser)
13189 tree try_block;
13190 bool ctor_initializer_p;
13192 /* Look for the `try' keyword. */
13193 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13194 return false;
13195 /* Let the rest of the front-end know where we are. */
13196 try_block = begin_function_try_block ();
13197 /* Parse the function-body. */
13198 ctor_initializer_p
13199 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13200 /* We're done with the `try' part. */
13201 finish_function_try_block (try_block);
13202 /* Parse the handlers. */
13203 cp_parser_handler_seq (parser);
13204 /* We're done with the handlers. */
13205 finish_function_handler_sequence (try_block);
13207 return ctor_initializer_p;
13210 /* Parse a handler-seq.
13212 handler-seq:
13213 handler handler-seq [opt] */
13215 static void
13216 cp_parser_handler_seq (cp_parser* parser)
13218 while (true)
13220 cp_token *token;
13222 /* Parse the handler. */
13223 cp_parser_handler (parser);
13224 /* Peek at the next token. */
13225 token = cp_lexer_peek_token (parser->lexer);
13226 /* If it's not `catch' then there are no more handlers. */
13227 if (!cp_parser_is_keyword (token, RID_CATCH))
13228 break;
13232 /* Parse a handler.
13234 handler:
13235 catch ( exception-declaration ) compound-statement */
13237 static void
13238 cp_parser_handler (cp_parser* parser)
13240 tree handler;
13241 tree declaration;
13243 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13244 handler = begin_handler ();
13245 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13246 declaration = cp_parser_exception_declaration (parser);
13247 finish_handler_parms (declaration, handler);
13248 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13249 cp_parser_compound_statement (parser, false);
13250 finish_handler (handler);
13253 /* Parse an exception-declaration.
13255 exception-declaration:
13256 type-specifier-seq declarator
13257 type-specifier-seq abstract-declarator
13258 type-specifier-seq
13259 ...
13261 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13262 ellipsis variant is used. */
13264 static tree
13265 cp_parser_exception_declaration (cp_parser* parser)
13267 tree type_specifiers;
13268 tree declarator;
13269 const char *saved_message;
13271 /* If it's an ellipsis, it's easy to handle. */
13272 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13274 /* Consume the `...' token. */
13275 cp_lexer_consume_token (parser->lexer);
13276 return NULL_TREE;
13279 /* Types may not be defined in exception-declarations. */
13280 saved_message = parser->type_definition_forbidden_message;
13281 parser->type_definition_forbidden_message
13282 = "types may not be defined in exception-declarations";
13284 /* Parse the type-specifier-seq. */
13285 type_specifiers = cp_parser_type_specifier_seq (parser);
13286 /* If it's a `)', then there is no declarator. */
13287 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13288 declarator = NULL_TREE;
13289 else
13290 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13291 /*ctor_dtor_or_conv_p=*/NULL,
13292 /*parenthesized_p=*/NULL,
13293 /*member_p=*/false);
13295 /* Restore the saved message. */
13296 parser->type_definition_forbidden_message = saved_message;
13298 return start_handler_parms (type_specifiers, declarator);
13301 /* Parse a throw-expression.
13303 throw-expression:
13304 throw assignment-expression [opt]
13306 Returns a THROW_EXPR representing the throw-expression. */
13308 static tree
13309 cp_parser_throw_expression (cp_parser* parser)
13311 tree expression;
13312 cp_token* token;
13314 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13315 token = cp_lexer_peek_token (parser->lexer);
13316 /* Figure out whether or not there is an assignment-expression
13317 following the "throw" keyword. */
13318 if (token->type == CPP_COMMA
13319 || token->type == CPP_SEMICOLON
13320 || token->type == CPP_CLOSE_PAREN
13321 || token->type == CPP_CLOSE_SQUARE
13322 || token->type == CPP_CLOSE_BRACE
13323 || token->type == CPP_COLON)
13324 expression = NULL_TREE;
13325 else
13326 expression = cp_parser_assignment_expression (parser);
13328 return build_throw (expression);
13331 /* GNU Extensions */
13333 /* Parse an (optional) asm-specification.
13335 asm-specification:
13336 asm ( string-literal )
13338 If the asm-specification is present, returns a STRING_CST
13339 corresponding to the string-literal. Otherwise, returns
13340 NULL_TREE. */
13342 static tree
13343 cp_parser_asm_specification_opt (cp_parser* parser)
13345 cp_token *token;
13346 tree asm_specification;
13348 /* Peek at the next token. */
13349 token = cp_lexer_peek_token (parser->lexer);
13350 /* If the next token isn't the `asm' keyword, then there's no
13351 asm-specification. */
13352 if (!cp_parser_is_keyword (token, RID_ASM))
13353 return NULL_TREE;
13355 /* Consume the `asm' token. */
13356 cp_lexer_consume_token (parser->lexer);
13357 /* Look for the `('. */
13358 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13360 /* Look for the string-literal. */
13361 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13362 if (token)
13363 asm_specification = token->value;
13364 else
13365 asm_specification = NULL_TREE;
13367 /* Look for the `)'. */
13368 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13370 return asm_specification;
13373 /* Parse an asm-operand-list.
13375 asm-operand-list:
13376 asm-operand
13377 asm-operand-list , asm-operand
13379 asm-operand:
13380 string-literal ( expression )
13381 [ string-literal ] string-literal ( expression )
13383 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13384 each node is the expression. The TREE_PURPOSE is itself a
13385 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13386 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13387 is a STRING_CST for the string literal before the parenthesis. */
13389 static tree
13390 cp_parser_asm_operand_list (cp_parser* parser)
13392 tree asm_operands = NULL_TREE;
13394 while (true)
13396 tree string_literal;
13397 tree expression;
13398 tree name;
13399 cp_token *token;
13401 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13403 /* Consume the `[' token. */
13404 cp_lexer_consume_token (parser->lexer);
13405 /* Read the operand name. */
13406 name = cp_parser_identifier (parser);
13407 if (name != error_mark_node)
13408 name = build_string (IDENTIFIER_LENGTH (name),
13409 IDENTIFIER_POINTER (name));
13410 /* Look for the closing `]'. */
13411 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13413 else
13414 name = NULL_TREE;
13415 /* Look for the string-literal. */
13416 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13417 string_literal = token ? token->value : error_mark_node;
13418 /* Look for the `('. */
13419 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13420 /* Parse the expression. */
13421 expression = cp_parser_expression (parser);
13422 /* Look for the `)'. */
13423 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13424 /* Add this operand to the list. */
13425 asm_operands = tree_cons (build_tree_list (name, string_literal),
13426 expression,
13427 asm_operands);
13428 /* If the next token is not a `,', there are no more
13429 operands. */
13430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13431 break;
13432 /* Consume the `,'. */
13433 cp_lexer_consume_token (parser->lexer);
13436 return nreverse (asm_operands);
13439 /* Parse an asm-clobber-list.
13441 asm-clobber-list:
13442 string-literal
13443 asm-clobber-list , string-literal
13445 Returns a TREE_LIST, indicating the clobbers in the order that they
13446 appeared. The TREE_VALUE of each node is a STRING_CST. */
13448 static tree
13449 cp_parser_asm_clobber_list (cp_parser* parser)
13451 tree clobbers = NULL_TREE;
13453 while (true)
13455 cp_token *token;
13456 tree string_literal;
13458 /* Look for the string literal. */
13459 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13460 string_literal = token ? token->value : error_mark_node;
13461 /* Add it to the list. */
13462 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13463 /* If the next token is not a `,', then the list is
13464 complete. */
13465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13466 break;
13467 /* Consume the `,' token. */
13468 cp_lexer_consume_token (parser->lexer);
13471 return clobbers;
13474 /* Parse an (optional) series of attributes.
13476 attributes:
13477 attributes attribute
13479 attribute:
13480 __attribute__ (( attribute-list [opt] ))
13482 The return value is as for cp_parser_attribute_list. */
13484 static tree
13485 cp_parser_attributes_opt (cp_parser* parser)
13487 tree attributes = NULL_TREE;
13489 while (true)
13491 cp_token *token;
13492 tree attribute_list;
13494 /* Peek at the next token. */
13495 token = cp_lexer_peek_token (parser->lexer);
13496 /* If it's not `__attribute__', then we're done. */
13497 if (token->keyword != RID_ATTRIBUTE)
13498 break;
13500 /* Consume the `__attribute__' keyword. */
13501 cp_lexer_consume_token (parser->lexer);
13502 /* Look for the two `(' tokens. */
13503 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13504 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13506 /* Peek at the next token. */
13507 token = cp_lexer_peek_token (parser->lexer);
13508 if (token->type != CPP_CLOSE_PAREN)
13509 /* Parse the attribute-list. */
13510 attribute_list = cp_parser_attribute_list (parser);
13511 else
13512 /* If the next token is a `)', then there is no attribute
13513 list. */
13514 attribute_list = NULL;
13516 /* Look for the two `)' tokens. */
13517 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13518 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13520 /* Add these new attributes to the list. */
13521 attributes = chainon (attributes, attribute_list);
13524 return attributes;
13527 /* Parse an attribute-list.
13529 attribute-list:
13530 attribute
13531 attribute-list , attribute
13533 attribute:
13534 identifier
13535 identifier ( identifier )
13536 identifier ( identifier , expression-list )
13537 identifier ( expression-list )
13539 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13540 TREE_PURPOSE of each node is the identifier indicating which
13541 attribute is in use. The TREE_VALUE represents the arguments, if
13542 any. */
13544 static tree
13545 cp_parser_attribute_list (cp_parser* parser)
13547 tree attribute_list = NULL_TREE;
13549 while (true)
13551 cp_token *token;
13552 tree identifier;
13553 tree attribute;
13555 /* Look for the identifier. We also allow keywords here; for
13556 example `__attribute__ ((const))' is legal. */
13557 token = cp_lexer_peek_token (parser->lexer);
13558 if (token->type == CPP_NAME
13559 || token->type == CPP_KEYWORD)
13561 /* Consume the token. */
13562 token = cp_lexer_consume_token (parser->lexer);
13564 /* Save away the identifier that indicates which attribute
13565 this is. */
13566 identifier = token->value;
13567 attribute = build_tree_list (identifier, NULL_TREE);
13569 /* Peek at the next token. */
13570 token = cp_lexer_peek_token (parser->lexer);
13571 /* If it's an `(', then parse the attribute arguments. */
13572 if (token->type == CPP_OPEN_PAREN)
13574 tree arguments;
13576 arguments = (cp_parser_parenthesized_expression_list
13577 (parser, true, /*non_constant_p=*/NULL));
13578 /* Save the identifier and arguments away. */
13579 TREE_VALUE (attribute) = arguments;
13582 /* Add this attribute to the list. */
13583 TREE_CHAIN (attribute) = attribute_list;
13584 attribute_list = attribute;
13586 /* Now, look for more attributes. */
13587 token = cp_lexer_peek_token (parser->lexer);
13589 /* Now, look for more attributes. If the next token isn't a
13590 `,', we're done. */
13591 if (token->type != CPP_COMMA)
13592 break;
13594 /* Consume the comma and keep going. */
13595 cp_lexer_consume_token (parser->lexer);
13598 /* We built up the list in reverse order. */
13599 return nreverse (attribute_list);
13602 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13603 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13604 current value of the PEDANTIC flag, regardless of whether or not
13605 the `__extension__' keyword is present. The caller is responsible
13606 for restoring the value of the PEDANTIC flag. */
13608 static bool
13609 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13611 /* Save the old value of the PEDANTIC flag. */
13612 *saved_pedantic = pedantic;
13614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13616 /* Consume the `__extension__' token. */
13617 cp_lexer_consume_token (parser->lexer);
13618 /* We're not being pedantic while the `__extension__' keyword is
13619 in effect. */
13620 pedantic = 0;
13622 return true;
13625 return false;
13628 /* Parse a label declaration.
13630 label-declaration:
13631 __label__ label-declarator-seq ;
13633 label-declarator-seq:
13634 identifier , label-declarator-seq
13635 identifier */
13637 static void
13638 cp_parser_label_declaration (cp_parser* parser)
13640 /* Look for the `__label__' keyword. */
13641 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13643 while (true)
13645 tree identifier;
13647 /* Look for an identifier. */
13648 identifier = cp_parser_identifier (parser);
13649 /* If we failed, stop. */
13650 if (identifier == error_mark_node)
13651 break;
13652 /* Declare it as a label. */
13653 finish_label_decl (identifier);
13654 /* If the next token is a `;', stop. */
13655 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13656 break;
13657 /* Look for the `,' separating the label declarations. */
13658 cp_parser_require (parser, CPP_COMMA, "`,'");
13661 /* Look for the final `;'. */
13662 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13665 /* Support Functions */
13667 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13668 NAME should have one of the representations used for an
13669 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13670 is returned. If PARSER->SCOPE is a dependent type, then a
13671 SCOPE_REF is returned.
13673 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13674 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13675 was formed. Abstractly, such entities should not be passed to this
13676 function, because they do not need to be looked up, but it is
13677 simpler to check for this special case here, rather than at the
13678 call-sites.
13680 In cases not explicitly covered above, this function returns a
13681 DECL, OVERLOAD, or baselink representing the result of the lookup.
13682 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13683 is returned.
13685 If IS_TYPE is TRUE, bindings that do not refer to types are
13686 ignored.
13688 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13689 ignored.
13691 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13692 are ignored.
13694 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13695 types. */
13697 static tree
13698 cp_parser_lookup_name (cp_parser *parser, tree name,
13699 bool is_type, bool is_template, bool is_namespace,
13700 bool check_dependency)
13702 int flags = 0;
13703 tree decl;
13704 tree object_type = parser->context->object_type;
13706 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13707 no longer valid. Note that if we are parsing tentatively, and
13708 the parse fails, OBJECT_TYPE will be automatically restored. */
13709 parser->context->object_type = NULL_TREE;
13711 if (name == error_mark_node)
13712 return error_mark_node;
13714 if (!cp_parser_parsing_tentatively (parser)
13715 || cp_parser_committed_to_tentative_parse (parser))
13716 flags |= LOOKUP_COMPLAIN;
13718 /* A template-id has already been resolved; there is no lookup to
13719 do. */
13720 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13721 return name;
13722 if (BASELINK_P (name))
13724 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13725 == TEMPLATE_ID_EXPR),
13726 20020909);
13727 return name;
13730 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13731 it should already have been checked to make sure that the name
13732 used matches the type being destroyed. */
13733 if (TREE_CODE (name) == BIT_NOT_EXPR)
13735 tree type;
13737 /* Figure out to which type this destructor applies. */
13738 if (parser->scope)
13739 type = parser->scope;
13740 else if (object_type)
13741 type = object_type;
13742 else
13743 type = current_class_type;
13744 /* If that's not a class type, there is no destructor. */
13745 if (!type || !CLASS_TYPE_P (type))
13746 return error_mark_node;
13747 if (!CLASSTYPE_DESTRUCTORS (type))
13748 return error_mark_node;
13749 /* If it was a class type, return the destructor. */
13750 return CLASSTYPE_DESTRUCTORS (type);
13753 /* By this point, the NAME should be an ordinary identifier. If
13754 the id-expression was a qualified name, the qualifying scope is
13755 stored in PARSER->SCOPE at this point. */
13756 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13757 20000619);
13759 /* Perform the lookup. */
13760 if (parser->scope)
13762 bool dependent_p;
13764 if (parser->scope == error_mark_node)
13765 return error_mark_node;
13767 /* If the SCOPE is dependent, the lookup must be deferred until
13768 the template is instantiated -- unless we are explicitly
13769 looking up names in uninstantiated templates. Even then, we
13770 cannot look up the name if the scope is not a class type; it
13771 might, for example, be a template type parameter. */
13772 dependent_p = (TYPE_P (parser->scope)
13773 && !(parser->in_declarator_p
13774 && currently_open_class (parser->scope))
13775 && dependent_type_p (parser->scope));
13776 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13777 && dependent_p)
13779 if (is_type)
13780 /* The resolution to Core Issue 180 says that `struct A::B'
13781 should be considered a type-name, even if `A' is
13782 dependent. */
13783 decl = TYPE_NAME (make_typename_type (parser->scope,
13784 name,
13785 /*complain=*/1));
13786 else if (is_template)
13787 decl = make_unbound_class_template (parser->scope,
13788 name,
13789 /*complain=*/1);
13790 else
13791 decl = build_nt (SCOPE_REF, parser->scope, name);
13793 else
13795 bool pop_p = false;
13797 /* If PARSER->SCOPE is a dependent type, then it must be a
13798 class type, and we must not be checking dependencies;
13799 otherwise, we would have processed this lookup above. So
13800 that PARSER->SCOPE is not considered a dependent base by
13801 lookup_member, we must enter the scope here. */
13802 if (dependent_p)
13803 pop_p = push_scope (parser->scope);
13804 /* If the PARSER->SCOPE is a a template specialization, it
13805 may be instantiated during name lookup. In that case,
13806 errors may be issued. Even if we rollback the current
13807 tentative parse, those errors are valid. */
13808 decl = lookup_qualified_name (parser->scope, name, is_type,
13809 /*complain=*/true);
13810 if (pop_p)
13811 pop_scope (parser->scope);
13813 parser->qualifying_scope = parser->scope;
13814 parser->object_scope = NULL_TREE;
13816 else if (object_type)
13818 tree object_decl = NULL_TREE;
13819 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13820 OBJECT_TYPE is not a class. */
13821 if (CLASS_TYPE_P (object_type))
13822 /* If the OBJECT_TYPE is a template specialization, it may
13823 be instantiated during name lookup. In that case, errors
13824 may be issued. Even if we rollback the current tentative
13825 parse, those errors are valid. */
13826 object_decl = lookup_member (object_type,
13827 name,
13828 /*protect=*/0, is_type);
13829 /* Look it up in the enclosing context, too. */
13830 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13831 is_namespace, flags);
13832 parser->object_scope = object_type;
13833 parser->qualifying_scope = NULL_TREE;
13834 if (object_decl)
13835 decl = object_decl;
13837 else
13839 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13840 is_namespace, flags);
13841 parser->qualifying_scope = NULL_TREE;
13842 parser->object_scope = NULL_TREE;
13845 /* If the lookup failed, let our caller know. */
13846 if (!decl
13847 || decl == error_mark_node
13848 || (TREE_CODE (decl) == FUNCTION_DECL
13849 && DECL_ANTICIPATED (decl)))
13850 return error_mark_node;
13852 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13853 if (TREE_CODE (decl) == TREE_LIST)
13855 /* The error message we have to print is too complicated for
13856 cp_parser_error, so we incorporate its actions directly. */
13857 if (!cp_parser_simulate_error (parser))
13859 error ("reference to `%D' is ambiguous", name);
13860 print_candidates (decl);
13862 return error_mark_node;
13865 my_friendly_assert (DECL_P (decl)
13866 || TREE_CODE (decl) == OVERLOAD
13867 || TREE_CODE (decl) == SCOPE_REF
13868 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13869 || BASELINK_P (decl),
13870 20000619);
13872 /* If we have resolved the name of a member declaration, check to
13873 see if the declaration is accessible. When the name resolves to
13874 set of overloaded functions, accessibility is checked when
13875 overload resolution is done.
13877 During an explicit instantiation, access is not checked at all,
13878 as per [temp.explicit]. */
13879 if (DECL_P (decl))
13880 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13882 return decl;
13885 /* Like cp_parser_lookup_name, but for use in the typical case where
13886 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13887 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13889 static tree
13890 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13892 return cp_parser_lookup_name (parser, name,
13893 /*is_type=*/false,
13894 /*is_template=*/false,
13895 /*is_namespace=*/false,
13896 /*check_dependency=*/true);
13899 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13900 the current context, return the TYPE_DECL. If TAG_NAME_P is
13901 true, the DECL indicates the class being defined in a class-head,
13902 or declared in an elaborated-type-specifier.
13904 Otherwise, return DECL. */
13906 static tree
13907 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13909 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13910 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13912 struct A {
13913 template <typename T> struct B;
13916 template <typename T> struct A::B {};
13918 Similarly, in a elaborated-type-specifier:
13920 namespace N { struct X{}; }
13922 struct A {
13923 template <typename T> friend struct N::X;
13926 However, if the DECL refers to a class type, and we are in
13927 the scope of the class, then the name lookup automatically
13928 finds the TYPE_DECL created by build_self_reference rather
13929 than a TEMPLATE_DECL. For example, in:
13931 template <class T> struct S {
13932 S s;
13935 there is no need to handle such case. */
13937 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13938 return DECL_TEMPLATE_RESULT (decl);
13940 return decl;
13943 /* If too many, or too few, template-parameter lists apply to the
13944 declarator, issue an error message. Returns TRUE if all went well,
13945 and FALSE otherwise. */
13947 static bool
13948 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13949 tree declarator)
13951 unsigned num_templates;
13953 /* We haven't seen any classes that involve template parameters yet. */
13954 num_templates = 0;
13956 switch (TREE_CODE (declarator))
13958 case CALL_EXPR:
13959 case ARRAY_REF:
13960 case INDIRECT_REF:
13961 case ADDR_EXPR:
13963 tree main_declarator = TREE_OPERAND (declarator, 0);
13964 return
13965 cp_parser_check_declarator_template_parameters (parser,
13966 main_declarator);
13969 case SCOPE_REF:
13971 tree scope;
13972 tree member;
13974 scope = TREE_OPERAND (declarator, 0);
13975 member = TREE_OPERAND (declarator, 1);
13977 /* If this is a pointer-to-member, then we are not interested
13978 in the SCOPE, because it does not qualify the thing that is
13979 being declared. */
13980 if (TREE_CODE (member) == INDIRECT_REF)
13981 return (cp_parser_check_declarator_template_parameters
13982 (parser, member));
13984 while (scope && CLASS_TYPE_P (scope))
13986 /* You're supposed to have one `template <...>'
13987 for every template class, but you don't need one
13988 for a full specialization. For example:
13990 template <class T> struct S{};
13991 template <> struct S<int> { void f(); };
13992 void S<int>::f () {}
13994 is correct; there shouldn't be a `template <>' for
13995 the definition of `S<int>::f'. */
13996 if (CLASSTYPE_TEMPLATE_INFO (scope)
13997 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13998 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13999 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14000 ++num_templates;
14002 scope = TYPE_CONTEXT (scope);
14006 /* Fall through. */
14008 default:
14009 /* If the DECLARATOR has the form `X<y>' then it uses one
14010 additional level of template parameters. */
14011 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
14012 ++num_templates;
14014 return cp_parser_check_template_parameters (parser,
14015 num_templates);
14019 /* NUM_TEMPLATES were used in the current declaration. If that is
14020 invalid, return FALSE and issue an error messages. Otherwise,
14021 return TRUE. */
14023 static bool
14024 cp_parser_check_template_parameters (cp_parser* parser,
14025 unsigned num_templates)
14027 /* If there are more template classes than parameter lists, we have
14028 something like:
14030 template <class T> void S<T>::R<T>::f (); */
14031 if (parser->num_template_parameter_lists < num_templates)
14033 error ("too few template-parameter-lists");
14034 return false;
14036 /* If there are the same number of template classes and parameter
14037 lists, that's OK. */
14038 if (parser->num_template_parameter_lists == num_templates)
14039 return true;
14040 /* If there are more, but only one more, then we are referring to a
14041 member template. That's OK too. */
14042 if (parser->num_template_parameter_lists == num_templates + 1)
14043 return true;
14044 /* Otherwise, there are too many template parameter lists. We have
14045 something like:
14047 template <class T> template <class U> void S::f(); */
14048 error ("too many template-parameter-lists");
14049 return false;
14052 /* Parse a binary-expression of the general form:
14054 binary-expression:
14055 <expr>
14056 binary-expression <token> <expr>
14058 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14059 to parser the <expr>s. If the first production is used, then the
14060 value returned by FN is returned directly. Otherwise, a node with
14061 the indicated EXPR_TYPE is returned, with operands corresponding to
14062 the two sub-expressions. */
14064 static tree
14065 cp_parser_binary_expression (cp_parser* parser,
14066 const cp_parser_token_tree_map token_tree_map,
14067 cp_parser_expression_fn fn)
14069 tree lhs;
14071 /* Parse the first expression. */
14072 lhs = (*fn) (parser);
14073 /* Now, look for more expressions. */
14074 while (true)
14076 cp_token *token;
14077 const cp_parser_token_tree_map_node *map_node;
14078 tree rhs;
14080 /* Peek at the next token. */
14081 token = cp_lexer_peek_token (parser->lexer);
14082 /* If the token is `>', and that's not an operator at the
14083 moment, then we're done. */
14084 if (token->type == CPP_GREATER
14085 && !parser->greater_than_is_operator_p)
14086 break;
14087 /* If we find one of the tokens we want, build the corresponding
14088 tree representation. */
14089 for (map_node = token_tree_map;
14090 map_node->token_type != CPP_EOF;
14091 ++map_node)
14092 if (map_node->token_type == token->type)
14094 /* Assume that an overloaded operator will not be used. */
14095 bool overloaded_p = false;
14097 /* Consume the operator token. */
14098 cp_lexer_consume_token (parser->lexer);
14099 /* Parse the right-hand side of the expression. */
14100 rhs = (*fn) (parser);
14101 /* Build the binary tree node. */
14102 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14103 &overloaded_p);
14104 /* If the binary operator required the use of an
14105 overloaded operator, then this expression cannot be an
14106 integral constant-expression. An overloaded operator
14107 can be used even if both operands are otherwise
14108 permissible in an integral constant-expression if at
14109 least one of the operands is of enumeration type. */
14110 if (overloaded_p
14111 && (cp_parser_non_integral_constant_expression
14112 (parser, "calls to overloaded operators")))
14113 lhs = error_mark_node;
14114 break;
14117 /* If the token wasn't one of the ones we want, we're done. */
14118 if (map_node->token_type == CPP_EOF)
14119 break;
14122 return lhs;
14125 /* Parse an optional `::' token indicating that the following name is
14126 from the global namespace. If so, PARSER->SCOPE is set to the
14127 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14128 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14129 Returns the new value of PARSER->SCOPE, if the `::' token is
14130 present, and NULL_TREE otherwise. */
14132 static tree
14133 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14135 cp_token *token;
14137 /* Peek at the next token. */
14138 token = cp_lexer_peek_token (parser->lexer);
14139 /* If we're looking at a `::' token then we're starting from the
14140 global namespace, not our current location. */
14141 if (token->type == CPP_SCOPE)
14143 /* Consume the `::' token. */
14144 cp_lexer_consume_token (parser->lexer);
14145 /* Set the SCOPE so that we know where to start the lookup. */
14146 parser->scope = global_namespace;
14147 parser->qualifying_scope = global_namespace;
14148 parser->object_scope = NULL_TREE;
14150 return parser->scope;
14152 else if (!current_scope_valid_p)
14154 parser->scope = NULL_TREE;
14155 parser->qualifying_scope = NULL_TREE;
14156 parser->object_scope = NULL_TREE;
14159 return NULL_TREE;
14162 /* Returns TRUE if the upcoming token sequence is the start of a
14163 constructor declarator. If FRIEND_P is true, the declarator is
14164 preceded by the `friend' specifier. */
14166 static bool
14167 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14169 bool constructor_p;
14170 tree type_decl = NULL_TREE;
14171 bool nested_name_p;
14172 cp_token *next_token;
14174 /* The common case is that this is not a constructor declarator, so
14175 try to avoid doing lots of work if at all possible. It's not
14176 valid declare a constructor at function scope. */
14177 if (at_function_scope_p ())
14178 return false;
14179 /* And only certain tokens can begin a constructor declarator. */
14180 next_token = cp_lexer_peek_token (parser->lexer);
14181 if (next_token->type != CPP_NAME
14182 && next_token->type != CPP_SCOPE
14183 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14184 && next_token->type != CPP_TEMPLATE_ID)
14185 return false;
14187 /* Parse tentatively; we are going to roll back all of the tokens
14188 consumed here. */
14189 cp_parser_parse_tentatively (parser);
14190 /* Assume that we are looking at a constructor declarator. */
14191 constructor_p = true;
14193 /* Look for the optional `::' operator. */
14194 cp_parser_global_scope_opt (parser,
14195 /*current_scope_valid_p=*/false);
14196 /* Look for the nested-name-specifier. */
14197 nested_name_p
14198 = (cp_parser_nested_name_specifier_opt (parser,
14199 /*typename_keyword_p=*/false,
14200 /*check_dependency_p=*/false,
14201 /*type_p=*/false,
14202 /*is_declaration=*/false)
14203 != NULL_TREE);
14204 /* Outside of a class-specifier, there must be a
14205 nested-name-specifier. */
14206 if (!nested_name_p &&
14207 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14208 || friend_p))
14209 constructor_p = false;
14210 /* If we still think that this might be a constructor-declarator,
14211 look for a class-name. */
14212 if (constructor_p)
14214 /* If we have:
14216 template <typename T> struct S { S(); };
14217 template <typename T> S<T>::S ();
14219 we must recognize that the nested `S' names a class.
14220 Similarly, for:
14222 template <typename T> S<T>::S<T> ();
14224 we must recognize that the nested `S' names a template. */
14225 type_decl = cp_parser_class_name (parser,
14226 /*typename_keyword_p=*/false,
14227 /*template_keyword_p=*/false,
14228 /*type_p=*/false,
14229 /*check_dependency_p=*/false,
14230 /*class_head_p=*/false,
14231 /*is_declaration=*/false);
14232 /* If there was no class-name, then this is not a constructor. */
14233 constructor_p = !cp_parser_error_occurred (parser);
14236 /* If we're still considering a constructor, we have to see a `(',
14237 to begin the parameter-declaration-clause, followed by either a
14238 `)', an `...', or a decl-specifier. We need to check for a
14239 type-specifier to avoid being fooled into thinking that:
14241 S::S (f) (int);
14243 is a constructor. (It is actually a function named `f' that
14244 takes one parameter (of type `int') and returns a value of type
14245 `S::S'. */
14246 if (constructor_p
14247 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14250 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14251 /* A parameter declaration begins with a decl-specifier,
14252 which is either the "attribute" keyword, a storage class
14253 specifier, or (usually) a type-specifier. */
14254 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14255 && !cp_parser_storage_class_specifier_opt (parser))
14257 tree type;
14258 bool pop_p = false;
14259 unsigned saved_num_template_parameter_lists;
14261 /* Names appearing in the type-specifier should be looked up
14262 in the scope of the class. */
14263 if (current_class_type)
14264 type = NULL_TREE;
14265 else
14267 type = TREE_TYPE (type_decl);
14268 if (TREE_CODE (type) == TYPENAME_TYPE)
14270 type = resolve_typename_type (type,
14271 /*only_current_p=*/false);
14272 if (type == error_mark_node)
14274 cp_parser_abort_tentative_parse (parser);
14275 return false;
14278 pop_p = push_scope (type);
14281 /* Inside the constructor parameter list, surrounding
14282 template-parameter-lists do not apply. */
14283 saved_num_template_parameter_lists
14284 = parser->num_template_parameter_lists;
14285 parser->num_template_parameter_lists = 0;
14287 /* Look for the type-specifier. */
14288 cp_parser_type_specifier (parser,
14289 CP_PARSER_FLAGS_NONE,
14290 /*is_friend=*/false,
14291 /*is_declarator=*/true,
14292 /*declares_class_or_enum=*/NULL,
14293 /*is_cv_qualifier=*/NULL);
14295 parser->num_template_parameter_lists
14296 = saved_num_template_parameter_lists;
14298 /* Leave the scope of the class. */
14299 if (pop_p)
14300 pop_scope (type);
14302 constructor_p = !cp_parser_error_occurred (parser);
14305 else
14306 constructor_p = false;
14307 /* We did not really want to consume any tokens. */
14308 cp_parser_abort_tentative_parse (parser);
14310 return constructor_p;
14313 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14314 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14315 they must be performed once we are in the scope of the function.
14317 Returns the function defined. */
14319 static tree
14320 cp_parser_function_definition_from_specifiers_and_declarator
14321 (cp_parser* parser,
14322 tree decl_specifiers,
14323 tree attributes,
14324 tree declarator)
14326 tree fn;
14327 bool success_p;
14329 /* Begin the function-definition. */
14330 success_p = begin_function_definition (decl_specifiers,
14331 attributes,
14332 declarator);
14334 /* If there were names looked up in the decl-specifier-seq that we
14335 did not check, check them now. We must wait until we are in the
14336 scope of the function to perform the checks, since the function
14337 might be a friend. */
14338 perform_deferred_access_checks ();
14340 if (!success_p)
14342 /* If begin_function_definition didn't like the definition, skip
14343 the entire function. */
14344 error ("invalid function declaration");
14345 cp_parser_skip_to_end_of_block_or_statement (parser);
14346 fn = error_mark_node;
14348 else
14349 fn = cp_parser_function_definition_after_declarator (parser,
14350 /*inline_p=*/false);
14352 return fn;
14355 /* Parse the part of a function-definition that follows the
14356 declarator. INLINE_P is TRUE iff this function is an inline
14357 function defined with a class-specifier.
14359 Returns the function defined. */
14361 static tree
14362 cp_parser_function_definition_after_declarator (cp_parser* parser,
14363 bool inline_p)
14365 tree fn;
14366 bool ctor_initializer_p = false;
14367 bool saved_in_unbraced_linkage_specification_p;
14368 unsigned saved_num_template_parameter_lists;
14370 /* If the next token is `return', then the code may be trying to
14371 make use of the "named return value" extension that G++ used to
14372 support. */
14373 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14375 /* Consume the `return' keyword. */
14376 cp_lexer_consume_token (parser->lexer);
14377 /* Look for the identifier that indicates what value is to be
14378 returned. */
14379 cp_parser_identifier (parser);
14380 /* Issue an error message. */
14381 error ("named return values are no longer supported");
14382 /* Skip tokens until we reach the start of the function body. */
14383 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14384 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14385 cp_lexer_consume_token (parser->lexer);
14387 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14388 anything declared inside `f'. */
14389 saved_in_unbraced_linkage_specification_p
14390 = parser->in_unbraced_linkage_specification_p;
14391 parser->in_unbraced_linkage_specification_p = false;
14392 /* Inside the function, surrounding template-parameter-lists do not
14393 apply. */
14394 saved_num_template_parameter_lists
14395 = parser->num_template_parameter_lists;
14396 parser->num_template_parameter_lists = 0;
14397 /* If the next token is `try', then we are looking at a
14398 function-try-block. */
14399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14400 ctor_initializer_p = cp_parser_function_try_block (parser);
14401 /* A function-try-block includes the function-body, so we only do
14402 this next part if we're not processing a function-try-block. */
14403 else
14404 ctor_initializer_p
14405 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14407 /* Finish the function. */
14408 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14409 (inline_p ? 2 : 0));
14410 /* Generate code for it, if necessary. */
14411 expand_or_defer_fn (fn);
14412 /* Restore the saved values. */
14413 parser->in_unbraced_linkage_specification_p
14414 = saved_in_unbraced_linkage_specification_p;
14415 parser->num_template_parameter_lists
14416 = saved_num_template_parameter_lists;
14418 return fn;
14421 /* Parse a template-declaration, assuming that the `export' (and
14422 `extern') keywords, if present, has already been scanned. MEMBER_P
14423 is as for cp_parser_template_declaration. */
14425 static void
14426 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14428 tree decl = NULL_TREE;
14429 tree parameter_list;
14430 bool friend_p = false;
14432 /* Look for the `template' keyword. */
14433 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14434 return;
14436 /* And the `<'. */
14437 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14438 return;
14440 /* If the next token is `>', then we have an invalid
14441 specialization. Rather than complain about an invalid template
14442 parameter, issue an error message here. */
14443 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14445 cp_parser_error (parser, "invalid explicit specialization");
14446 begin_specialization ();
14447 parameter_list = NULL_TREE;
14449 else
14451 /* Parse the template parameters. */
14452 begin_template_parm_list ();
14453 parameter_list = cp_parser_template_parameter_list (parser);
14454 parameter_list = end_template_parm_list (parameter_list);
14457 /* Look for the `>'. */
14458 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14459 /* We just processed one more parameter list. */
14460 ++parser->num_template_parameter_lists;
14461 /* If the next token is `template', there are more template
14462 parameters. */
14463 if (cp_lexer_next_token_is_keyword (parser->lexer,
14464 RID_TEMPLATE))
14465 cp_parser_template_declaration_after_export (parser, member_p);
14466 else
14468 decl = cp_parser_single_declaration (parser,
14469 member_p,
14470 &friend_p);
14472 /* If this is a member template declaration, let the front
14473 end know. */
14474 if (member_p && !friend_p && decl)
14476 if (TREE_CODE (decl) == TYPE_DECL)
14477 cp_parser_check_access_in_redeclaration (decl);
14479 decl = finish_member_template_decl (decl);
14481 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14482 make_friend_class (current_class_type, TREE_TYPE (decl),
14483 /*complain=*/true);
14485 /* We are done with the current parameter list. */
14486 --parser->num_template_parameter_lists;
14488 /* Finish up. */
14489 finish_template_decl (parameter_list);
14491 /* Register member declarations. */
14492 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14493 finish_member_declaration (decl);
14495 /* If DECL is a function template, we must return to parse it later.
14496 (Even though there is no definition, there might be default
14497 arguments that need handling.) */
14498 if (member_p && decl
14499 && (TREE_CODE (decl) == FUNCTION_DECL
14500 || DECL_FUNCTION_TEMPLATE_P (decl)))
14501 TREE_VALUE (parser->unparsed_functions_queues)
14502 = tree_cons (NULL_TREE, decl,
14503 TREE_VALUE (parser->unparsed_functions_queues));
14506 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14507 `function-definition' sequence. MEMBER_P is true, this declaration
14508 appears in a class scope.
14510 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14511 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14513 static tree
14514 cp_parser_single_declaration (cp_parser* parser,
14515 bool member_p,
14516 bool* friend_p)
14518 int declares_class_or_enum;
14519 tree decl = NULL_TREE;
14520 tree decl_specifiers;
14521 tree attributes;
14522 bool function_definition_p = false;
14524 /* This function is only used when processing a template
14525 declaration. */
14526 if (innermost_scope_kind () != sk_template_parms
14527 && innermost_scope_kind () != sk_template_spec)
14528 abort ();
14530 /* Defer access checks until we know what is being declared. */
14531 push_deferring_access_checks (dk_deferred);
14533 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14534 alternative. */
14535 decl_specifiers
14536 = cp_parser_decl_specifier_seq (parser,
14537 CP_PARSER_FLAGS_OPTIONAL,
14538 &attributes,
14539 &declares_class_or_enum);
14540 if (friend_p)
14541 *friend_p = cp_parser_friend_p (decl_specifiers);
14543 /* There are no template typedefs. */
14544 if (cp_parser_typedef_p (decl_specifiers))
14546 error ("template declaration of `typedef'");
14547 decl = error_mark_node;
14550 /* Gather up the access checks that occurred the
14551 decl-specifier-seq. */
14552 stop_deferring_access_checks ();
14554 /* Check for the declaration of a template class. */
14555 if (declares_class_or_enum)
14557 if (cp_parser_declares_only_class_p (parser))
14559 decl = shadow_tag (decl_specifiers);
14560 if (decl)
14561 decl = TYPE_NAME (decl);
14562 else
14563 decl = error_mark_node;
14566 /* If it's not a template class, try for a template function. If
14567 the next token is a `;', then this declaration does not declare
14568 anything. But, if there were errors in the decl-specifiers, then
14569 the error might well have come from an attempted class-specifier.
14570 In that case, there's no need to warn about a missing declarator. */
14571 if (!decl
14572 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14573 || !value_member (error_mark_node, decl_specifiers)))
14574 decl = cp_parser_init_declarator (parser,
14575 decl_specifiers,
14576 attributes,
14577 /*function_definition_allowed_p=*/true,
14578 member_p,
14579 declares_class_or_enum,
14580 &function_definition_p);
14582 pop_deferring_access_checks ();
14584 /* Clear any current qualification; whatever comes next is the start
14585 of something new. */
14586 parser->scope = NULL_TREE;
14587 parser->qualifying_scope = NULL_TREE;
14588 parser->object_scope = NULL_TREE;
14589 /* Look for a trailing `;' after the declaration. */
14590 if (!function_definition_p
14591 && (decl == error_mark_node
14592 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
14593 cp_parser_skip_to_end_of_block_or_statement (parser);
14595 return decl;
14598 /* Parse a cast-expression that is not the operand of a unary "&". */
14600 static tree
14601 cp_parser_simple_cast_expression (cp_parser *parser)
14603 return cp_parser_cast_expression (parser, /*address_p=*/false);
14606 /* Parse a functional cast to TYPE. Returns an expression
14607 representing the cast. */
14609 static tree
14610 cp_parser_functional_cast (cp_parser* parser, tree type)
14612 tree expression_list;
14613 tree cast;
14615 expression_list
14616 = cp_parser_parenthesized_expression_list (parser, false,
14617 /*non_constant_p=*/NULL);
14619 cast = build_functional_cast (type, expression_list);
14620 /* [expr.const]/1: In an integral constant expression "only type
14621 conversions to integral or enumeration type can be used". */
14622 if (TREE_CODE (type) == TYPE_DECL)
14623 type = TREE_TYPE (type);
14624 if (cast != error_mark_node && !dependent_type_p (type)
14625 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
14627 if (cp_parser_non_integral_constant_expression
14628 (parser, "a call to a constructor"))
14629 return error_mark_node;
14631 return cast;
14634 /* Save the tokens that make up the body of a member function defined
14635 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14636 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14637 specifiers applied to the declaration. Returns the FUNCTION_DECL
14638 for the member function. */
14640 static tree
14641 cp_parser_save_member_function_body (cp_parser* parser,
14642 tree decl_specifiers,
14643 tree declarator,
14644 tree attributes)
14646 cp_token_cache *cache;
14647 tree fn;
14649 /* Create the function-declaration. */
14650 fn = start_method (decl_specifiers, declarator, attributes);
14651 /* If something went badly wrong, bail out now. */
14652 if (fn == error_mark_node)
14654 /* If there's a function-body, skip it. */
14655 if (cp_parser_token_starts_function_definition_p
14656 (cp_lexer_peek_token (parser->lexer)))
14657 cp_parser_skip_to_end_of_block_or_statement (parser);
14658 return error_mark_node;
14661 /* Remember it, if there default args to post process. */
14662 cp_parser_save_default_args (parser, fn);
14664 /* Create a token cache. */
14665 cache = cp_token_cache_new ();
14666 /* Save away the tokens that make up the body of the
14667 function. */
14668 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14669 /* Handle function try blocks. */
14670 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14671 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14673 /* Save away the inline definition; we will process it when the
14674 class is complete. */
14675 DECL_PENDING_INLINE_INFO (fn) = cache;
14676 DECL_PENDING_INLINE_P (fn) = 1;
14678 /* We need to know that this was defined in the class, so that
14679 friend templates are handled correctly. */
14680 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14682 /* We're done with the inline definition. */
14683 finish_method (fn);
14685 /* Add FN to the queue of functions to be parsed later. */
14686 TREE_VALUE (parser->unparsed_functions_queues)
14687 = tree_cons (NULL_TREE, fn,
14688 TREE_VALUE (parser->unparsed_functions_queues));
14690 return fn;
14693 /* Parse a template-argument-list, as well as the trailing ">" (but
14694 not the opening ">"). See cp_parser_template_argument_list for the
14695 return value. */
14697 static tree
14698 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14700 tree arguments;
14701 tree saved_scope;
14702 tree saved_qualifying_scope;
14703 tree saved_object_scope;
14704 bool saved_greater_than_is_operator_p;
14706 /* [temp.names]
14708 When parsing a template-id, the first non-nested `>' is taken as
14709 the end of the template-argument-list rather than a greater-than
14710 operator. */
14711 saved_greater_than_is_operator_p
14712 = parser->greater_than_is_operator_p;
14713 parser->greater_than_is_operator_p = false;
14714 /* Parsing the argument list may modify SCOPE, so we save it
14715 here. */
14716 saved_scope = parser->scope;
14717 saved_qualifying_scope = parser->qualifying_scope;
14718 saved_object_scope = parser->object_scope;
14719 /* Parse the template-argument-list itself. */
14720 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14721 arguments = NULL_TREE;
14722 else
14723 arguments = cp_parser_template_argument_list (parser);
14724 /* Look for the `>' that ends the template-argument-list. If we find
14725 a '>>' instead, it's probably just a typo. */
14726 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14728 if (!saved_greater_than_is_operator_p)
14730 /* If we're in a nested template argument list, the '>>' has to be
14731 a typo for '> >'. We emit the error message, but we continue
14732 parsing and we push a '>' as next token, so that the argument
14733 list will be parsed correctly.. */
14734 cp_token* token;
14735 error ("`>>' should be `> >' within a nested template argument list");
14736 token = cp_lexer_peek_token (parser->lexer);
14737 token->type = CPP_GREATER;
14739 else
14741 /* If this is not a nested template argument list, the '>>' is
14742 a typo for '>'. Emit an error message and continue. */
14743 error ("spurious `>>', use `>' to terminate a template argument list");
14744 cp_lexer_consume_token (parser->lexer);
14747 else
14748 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14749 /* The `>' token might be a greater-than operator again now. */
14750 parser->greater_than_is_operator_p
14751 = saved_greater_than_is_operator_p;
14752 /* Restore the SAVED_SCOPE. */
14753 parser->scope = saved_scope;
14754 parser->qualifying_scope = saved_qualifying_scope;
14755 parser->object_scope = saved_object_scope;
14757 return arguments;
14760 /* MEMBER_FUNCTION is a member function, or a friend. If default
14761 arguments, or the body of the function have not yet been parsed,
14762 parse them now. */
14764 static void
14765 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14767 cp_lexer *saved_lexer;
14769 /* If this member is a template, get the underlying
14770 FUNCTION_DECL. */
14771 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14772 member_function = DECL_TEMPLATE_RESULT (member_function);
14774 /* There should not be any class definitions in progress at this
14775 point; the bodies of members are only parsed outside of all class
14776 definitions. */
14777 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14778 /* While we're parsing the member functions we might encounter more
14779 classes. We want to handle them right away, but we don't want
14780 them getting mixed up with functions that are currently in the
14781 queue. */
14782 parser->unparsed_functions_queues
14783 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14785 /* Make sure that any template parameters are in scope. */
14786 maybe_begin_member_template_processing (member_function);
14788 /* If the body of the function has not yet been parsed, parse it
14789 now. */
14790 if (DECL_PENDING_INLINE_P (member_function))
14792 tree function_scope;
14793 cp_token_cache *tokens;
14795 /* The function is no longer pending; we are processing it. */
14796 tokens = DECL_PENDING_INLINE_INFO (member_function);
14797 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14798 DECL_PENDING_INLINE_P (member_function) = 0;
14800 /* If this is a local class, enter the scope of the containing
14801 function. */
14802 function_scope = current_function_decl;
14803 if (function_scope)
14804 push_function_context_to (function_scope);
14806 /* Save away the current lexer. */
14807 saved_lexer = parser->lexer;
14808 /* Make a new lexer to feed us the tokens saved for this function. */
14809 parser->lexer = cp_lexer_new_from_tokens (tokens);
14810 parser->lexer->next = saved_lexer;
14812 /* Set the current source position to be the location of the first
14813 token in the saved inline body. */
14814 cp_lexer_peek_token (parser->lexer);
14816 /* Let the front end know that we going to be defining this
14817 function. */
14818 start_function (NULL_TREE, member_function, NULL_TREE,
14819 SF_PRE_PARSED | SF_INCLASS_INLINE);
14821 /* Now, parse the body of the function. */
14822 cp_parser_function_definition_after_declarator (parser,
14823 /*inline_p=*/true);
14825 /* Leave the scope of the containing function. */
14826 if (function_scope)
14827 pop_function_context_from (function_scope);
14828 /* Restore the lexer. */
14829 parser->lexer = saved_lexer;
14832 /* Remove any template parameters from the symbol table. */
14833 maybe_end_member_template_processing ();
14835 /* Restore the queue. */
14836 parser->unparsed_functions_queues
14837 = TREE_CHAIN (parser->unparsed_functions_queues);
14840 /* If DECL contains any default args, remember it on the unparsed
14841 functions queue. */
14843 static void
14844 cp_parser_save_default_args (cp_parser* parser, tree decl)
14846 tree probe;
14848 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14849 probe;
14850 probe = TREE_CHAIN (probe))
14851 if (TREE_PURPOSE (probe))
14853 TREE_PURPOSE (parser->unparsed_functions_queues)
14854 = tree_cons (NULL_TREE, decl,
14855 TREE_PURPOSE (parser->unparsed_functions_queues));
14856 break;
14858 return;
14861 /* FN is a FUNCTION_DECL which may contains a parameter with an
14862 unparsed DEFAULT_ARG. Parse the default args now. */
14864 static void
14865 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14867 cp_lexer *saved_lexer;
14868 cp_token_cache *tokens;
14869 bool saved_local_variables_forbidden_p;
14870 tree parameters;
14872 /* While we're parsing the default args, we might (due to the
14873 statement expression extension) encounter more classes. We want
14874 to handle them right away, but we don't want them getting mixed
14875 up with default args that are currently in the queue. */
14876 parser->unparsed_functions_queues
14877 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14879 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14880 parameters;
14881 parameters = TREE_CHAIN (parameters))
14883 tree default_arg = TREE_PURPOSE (parameters);
14884 tree parsed_arg;
14886 if (!default_arg)
14887 continue;
14889 if (TREE_CODE (default_arg) != DEFAULT_ARG)
14890 /* This can happen for a friend declaration for a function
14891 already declared with default arguments. */
14892 continue;
14894 /* Save away the current lexer. */
14895 saved_lexer = parser->lexer;
14896 /* Create a new one, using the tokens we have saved. */
14897 tokens = DEFARG_TOKENS (default_arg);
14898 parser->lexer = cp_lexer_new_from_tokens (tokens);
14900 /* Set the current source position to be the location of the
14901 first token in the default argument. */
14902 cp_lexer_peek_token (parser->lexer);
14904 /* Local variable names (and the `this' keyword) may not appear
14905 in a default argument. */
14906 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14907 parser->local_variables_forbidden_p = true;
14909 /* Parse the assignment-expression. */
14910 if (DECL_FRIEND_CONTEXT (fn))
14911 push_nested_class (DECL_FRIEND_CONTEXT (fn));
14912 else if (DECL_CLASS_SCOPE_P (fn))
14913 push_nested_class (DECL_CONTEXT (fn));
14914 parsed_arg = cp_parser_assignment_expression (parser);
14915 if (DECL_FRIEND_CONTEXT (fn) || DECL_CLASS_SCOPE_P (fn))
14916 pop_nested_class ();
14918 TREE_PURPOSE (parameters) = parsed_arg;
14920 /* Update any instantiations we've already created. */
14921 for (default_arg = TREE_CHAIN (default_arg);
14922 default_arg;
14923 default_arg = TREE_CHAIN (default_arg))
14924 TREE_PURPOSE (TREE_PURPOSE (default_arg)) = parsed_arg;
14926 /* If the token stream has not been completely used up, then
14927 there was extra junk after the end of the default
14928 argument. */
14929 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14930 cp_parser_error (parser, "expected `,'");
14932 /* Restore saved state. */
14933 parser->lexer = saved_lexer;
14934 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14937 /* Restore the queue. */
14938 parser->unparsed_functions_queues
14939 = TREE_CHAIN (parser->unparsed_functions_queues);
14942 /* Parse the operand of `sizeof' (or a similar operator). Returns
14943 either a TYPE or an expression, depending on the form of the
14944 input. The KEYWORD indicates which kind of expression we have
14945 encountered. */
14947 static tree
14948 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14950 static const char *format;
14951 tree expr = NULL_TREE;
14952 const char *saved_message;
14953 bool saved_integral_constant_expression_p;
14955 /* Initialize FORMAT the first time we get here. */
14956 if (!format)
14957 format = "types may not be defined in `%s' expressions";
14959 /* Types cannot be defined in a `sizeof' expression. Save away the
14960 old message. */
14961 saved_message = parser->type_definition_forbidden_message;
14962 /* And create the new one. */
14963 parser->type_definition_forbidden_message
14964 = xmalloc (strlen (format)
14965 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14966 + 1 /* `\0' */);
14967 sprintf ((char *) parser->type_definition_forbidden_message,
14968 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14970 /* The restrictions on constant-expressions do not apply inside
14971 sizeof expressions. */
14972 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14973 parser->integral_constant_expression_p = false;
14975 /* Do not actually evaluate the expression. */
14976 ++skip_evaluation;
14977 /* If it's a `(', then we might be looking at the type-id
14978 construction. */
14979 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14981 tree type;
14982 bool saved_in_type_id_in_expr_p;
14984 /* We can't be sure yet whether we're looking at a type-id or an
14985 expression. */
14986 cp_parser_parse_tentatively (parser);
14987 /* Consume the `('. */
14988 cp_lexer_consume_token (parser->lexer);
14989 /* Parse the type-id. */
14990 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14991 parser->in_type_id_in_expr_p = true;
14992 type = cp_parser_type_id (parser);
14993 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14994 /* Now, look for the trailing `)'. */
14995 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14996 /* If all went well, then we're done. */
14997 if (cp_parser_parse_definitely (parser))
14999 /* Build a list of decl-specifiers; right now, we have only
15000 a single type-specifier. */
15001 type = build_tree_list (NULL_TREE,
15002 type);
15004 /* Call grokdeclarator to figure out what type this is. */
15005 expr = grokdeclarator (NULL_TREE,
15006 type,
15007 TYPENAME,
15008 /*initialized=*/0,
15009 /*attrlist=*/NULL);
15013 /* If the type-id production did not work out, then we must be
15014 looking at the unary-expression production. */
15015 if (!expr)
15016 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15017 /* Go back to evaluating expressions. */
15018 --skip_evaluation;
15020 /* Free the message we created. */
15021 free ((char *) parser->type_definition_forbidden_message);
15022 /* And restore the old one. */
15023 parser->type_definition_forbidden_message = saved_message;
15024 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15026 return expr;
15029 /* If the current declaration has no declarator, return true. */
15031 static bool
15032 cp_parser_declares_only_class_p (cp_parser *parser)
15034 /* If the next token is a `;' or a `,' then there is no
15035 declarator. */
15036 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15037 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15040 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15041 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15043 static bool
15044 cp_parser_friend_p (tree decl_specifiers)
15046 while (decl_specifiers)
15048 /* See if this decl-specifier is `friend'. */
15049 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15050 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
15051 return true;
15053 /* Go on to the next decl-specifier. */
15054 decl_specifiers = TREE_CHAIN (decl_specifiers);
15057 return false;
15060 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15061 Returns TRUE iff `typedef' appears among the DECL_SPECIFIERS. */
15063 static bool
15064 cp_parser_typedef_p (tree decl_specifiers)
15066 while (decl_specifiers)
15068 /* See if this decl-specifier is `typedef'. */
15069 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15070 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_TYPEDEF)
15071 return true;
15073 /* Go on to the next decl-specifier. */
15074 decl_specifiers = TREE_CHAIN (decl_specifiers);
15077 return false;
15081 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15082 issue an error message indicating that TOKEN_DESC was expected.
15084 Returns the token consumed, if the token had the appropriate type.
15085 Otherwise, returns NULL. */
15087 static cp_token *
15088 cp_parser_require (cp_parser* parser,
15089 enum cpp_ttype type,
15090 const char* token_desc)
15092 if (cp_lexer_next_token_is (parser->lexer, type))
15093 return cp_lexer_consume_token (parser->lexer);
15094 else
15096 /* Output the MESSAGE -- unless we're parsing tentatively. */
15097 if (!cp_parser_simulate_error (parser))
15099 char *message = concat ("expected ", token_desc, NULL);
15100 cp_parser_error (parser, message);
15101 free (message);
15103 return NULL;
15107 /* Like cp_parser_require, except that tokens will be skipped until
15108 the desired token is found. An error message is still produced if
15109 the next token is not as expected. */
15111 static void
15112 cp_parser_skip_until_found (cp_parser* parser,
15113 enum cpp_ttype type,
15114 const char* token_desc)
15116 cp_token *token;
15117 unsigned nesting_depth = 0;
15119 if (cp_parser_require (parser, type, token_desc))
15120 return;
15122 /* Skip tokens until the desired token is found. */
15123 while (true)
15125 /* Peek at the next token. */
15126 token = cp_lexer_peek_token (parser->lexer);
15127 /* If we've reached the token we want, consume it and
15128 stop. */
15129 if (token->type == type && !nesting_depth)
15131 cp_lexer_consume_token (parser->lexer);
15132 return;
15134 /* If we've run out of tokens, stop. */
15135 if (token->type == CPP_EOF)
15136 return;
15137 if (token->type == CPP_OPEN_BRACE
15138 || token->type == CPP_OPEN_PAREN
15139 || token->type == CPP_OPEN_SQUARE)
15140 ++nesting_depth;
15141 else if (token->type == CPP_CLOSE_BRACE
15142 || token->type == CPP_CLOSE_PAREN
15143 || token->type == CPP_CLOSE_SQUARE)
15145 if (nesting_depth-- == 0)
15146 return;
15148 /* Consume this token. */
15149 cp_lexer_consume_token (parser->lexer);
15153 /* If the next token is the indicated keyword, consume it. Otherwise,
15154 issue an error message indicating that TOKEN_DESC was expected.
15156 Returns the token consumed, if the token had the appropriate type.
15157 Otherwise, returns NULL. */
15159 static cp_token *
15160 cp_parser_require_keyword (cp_parser* parser,
15161 enum rid keyword,
15162 const char* token_desc)
15164 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15166 if (token && token->keyword != keyword)
15168 dyn_string_t error_msg;
15170 /* Format the error message. */
15171 error_msg = dyn_string_new (0);
15172 dyn_string_append_cstr (error_msg, "expected ");
15173 dyn_string_append_cstr (error_msg, token_desc);
15174 cp_parser_error (parser, error_msg->s);
15175 dyn_string_delete (error_msg);
15176 return NULL;
15179 return token;
15182 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15183 function-definition. */
15185 static bool
15186 cp_parser_token_starts_function_definition_p (cp_token* token)
15188 return (/* An ordinary function-body begins with an `{'. */
15189 token->type == CPP_OPEN_BRACE
15190 /* A ctor-initializer begins with a `:'. */
15191 || token->type == CPP_COLON
15192 /* A function-try-block begins with `try'. */
15193 || token->keyword == RID_TRY
15194 /* The named return value extension begins with `return'. */
15195 || token->keyword == RID_RETURN);
15198 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15199 definition. */
15201 static bool
15202 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15204 cp_token *token;
15206 token = cp_lexer_peek_token (parser->lexer);
15207 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15210 /* Returns TRUE iff the next token is the "," or ">" ending a
15211 template-argument. */
15213 static bool
15214 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15216 cp_token *token;
15218 token = cp_lexer_peek_token (parser->lexer);
15219 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15222 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15223 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15225 static bool
15226 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15227 size_t n)
15229 cp_token *token;
15231 token = cp_lexer_peek_nth_token (parser->lexer, n);
15232 if (token->type == CPP_LESS)
15233 return true;
15234 /* Check for the sequence `<::' in the original code. It would be lexed as
15235 `[:', where `[' is a digraph, and there is no whitespace before
15236 `:'. */
15237 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15239 cp_token *token2;
15240 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15241 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15242 return true;
15244 return false;
15247 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15248 or none_type otherwise. */
15250 static enum tag_types
15251 cp_parser_token_is_class_key (cp_token* token)
15253 switch (token->keyword)
15255 case RID_CLASS:
15256 return class_type;
15257 case RID_STRUCT:
15258 return record_type;
15259 case RID_UNION:
15260 return union_type;
15262 default:
15263 return none_type;
15267 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15269 static void
15270 cp_parser_check_class_key (enum tag_types class_key, tree type)
15272 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15273 pedwarn ("`%s' tag used in naming `%#T'",
15274 class_key == union_type ? "union"
15275 : class_key == record_type ? "struct" : "class",
15276 type);
15279 /* Issue an error message if DECL is redeclared with different
15280 access than its original declaration [class.access.spec/3].
15281 This applies to nested classes and nested class templates.
15282 [class.mem/1]. */
15284 static void cp_parser_check_access_in_redeclaration (tree decl)
15286 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15287 return;
15289 if ((TREE_PRIVATE (decl)
15290 != (current_access_specifier == access_private_node))
15291 || (TREE_PROTECTED (decl)
15292 != (current_access_specifier == access_protected_node)))
15293 error ("%D redeclared with different access", decl);
15296 /* Look for the `template' keyword, as a syntactic disambiguator.
15297 Return TRUE iff it is present, in which case it will be
15298 consumed. */
15300 static bool
15301 cp_parser_optional_template_keyword (cp_parser *parser)
15303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15305 /* The `template' keyword can only be used within templates;
15306 outside templates the parser can always figure out what is a
15307 template and what is not. */
15308 if (!processing_template_decl)
15310 error ("`template' (as a disambiguator) is only allowed "
15311 "within templates");
15312 /* If this part of the token stream is rescanned, the same
15313 error message would be generated. So, we purge the token
15314 from the stream. */
15315 cp_lexer_purge_token (parser->lexer);
15316 return false;
15318 else
15320 /* Consume the `template' keyword. */
15321 cp_lexer_consume_token (parser->lexer);
15322 return true;
15326 return false;
15329 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15330 set PARSER->SCOPE, and perform other related actions. */
15332 static void
15333 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15335 tree value;
15336 tree check;
15338 /* Get the stored value. */
15339 value = cp_lexer_consume_token (parser->lexer)->value;
15340 /* Perform any access checks that were deferred. */
15341 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15342 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15343 /* Set the scope from the stored value. */
15344 parser->scope = TREE_VALUE (value);
15345 parser->qualifying_scope = TREE_TYPE (value);
15346 parser->object_scope = NULL_TREE;
15349 /* Add tokens to CACHE until an non-nested END token appears. */
15351 static void
15352 cp_parser_cache_group (cp_parser *parser,
15353 cp_token_cache *cache,
15354 enum cpp_ttype end,
15355 unsigned depth)
15357 while (true)
15359 cp_token *token;
15361 /* Abort a parenthesized expression if we encounter a brace. */
15362 if ((end == CPP_CLOSE_PAREN || depth == 0)
15363 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15364 return;
15365 /* If we've reached the end of the file, stop. */
15366 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15367 return;
15368 /* Consume the next token. */
15369 token = cp_lexer_consume_token (parser->lexer);
15370 /* Add this token to the tokens we are saving. */
15371 cp_token_cache_push_token (cache, token);
15372 /* See if it starts a new group. */
15373 if (token->type == CPP_OPEN_BRACE)
15375 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15376 if (depth == 0)
15377 return;
15379 else if (token->type == CPP_OPEN_PAREN)
15380 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15381 else if (token->type == end)
15382 return;
15386 /* Begin parsing tentatively. We always save tokens while parsing
15387 tentatively so that if the tentative parsing fails we can restore the
15388 tokens. */
15390 static void
15391 cp_parser_parse_tentatively (cp_parser* parser)
15393 /* Enter a new parsing context. */
15394 parser->context = cp_parser_context_new (parser->context);
15395 /* Begin saving tokens. */
15396 cp_lexer_save_tokens (parser->lexer);
15397 /* In order to avoid repetitive access control error messages,
15398 access checks are queued up until we are no longer parsing
15399 tentatively. */
15400 push_deferring_access_checks (dk_deferred);
15403 /* Commit to the currently active tentative parse. */
15405 static void
15406 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15408 cp_parser_context *context;
15409 cp_lexer *lexer;
15411 /* Mark all of the levels as committed. */
15412 lexer = parser->lexer;
15413 for (context = parser->context; context->next; context = context->next)
15415 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15416 break;
15417 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15418 while (!cp_lexer_saving_tokens (lexer))
15419 lexer = lexer->next;
15420 cp_lexer_commit_tokens (lexer);
15424 /* Abort the currently active tentative parse. All consumed tokens
15425 will be rolled back, and no diagnostics will be issued. */
15427 static void
15428 cp_parser_abort_tentative_parse (cp_parser* parser)
15430 cp_parser_simulate_error (parser);
15431 /* Now, pretend that we want to see if the construct was
15432 successfully parsed. */
15433 cp_parser_parse_definitely (parser);
15436 /* Stop parsing tentatively. If a parse error has occurred, restore the
15437 token stream. Otherwise, commit to the tokens we have consumed.
15438 Returns true if no error occurred; false otherwise. */
15440 static bool
15441 cp_parser_parse_definitely (cp_parser* parser)
15443 bool error_occurred;
15444 cp_parser_context *context;
15446 /* Remember whether or not an error occurred, since we are about to
15447 destroy that information. */
15448 error_occurred = cp_parser_error_occurred (parser);
15449 /* Remove the topmost context from the stack. */
15450 context = parser->context;
15451 parser->context = context->next;
15452 /* If no parse errors occurred, commit to the tentative parse. */
15453 if (!error_occurred)
15455 /* Commit to the tokens read tentatively, unless that was
15456 already done. */
15457 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15458 cp_lexer_commit_tokens (parser->lexer);
15460 pop_to_parent_deferring_access_checks ();
15462 /* Otherwise, if errors occurred, roll back our state so that things
15463 are just as they were before we began the tentative parse. */
15464 else
15466 cp_lexer_rollback_tokens (parser->lexer);
15467 pop_deferring_access_checks ();
15469 /* Add the context to the front of the free list. */
15470 context->next = cp_parser_context_free_list;
15471 cp_parser_context_free_list = context;
15473 return !error_occurred;
15476 /* Returns true if we are parsing tentatively -- but have decided that
15477 we will stick with this tentative parse, even if errors occur. */
15479 static bool
15480 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15482 return (cp_parser_parsing_tentatively (parser)
15483 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15486 /* Returns nonzero iff an error has occurred during the most recent
15487 tentative parse. */
15489 static bool
15490 cp_parser_error_occurred (cp_parser* parser)
15492 return (cp_parser_parsing_tentatively (parser)
15493 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15496 /* Returns nonzero if GNU extensions are allowed. */
15498 static bool
15499 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15501 return parser->allow_gnu_extensions_p;
15506 /* The parser. */
15508 static GTY (()) cp_parser *the_parser;
15510 /* External interface. */
15512 /* Parse one entire translation unit. */
15514 void
15515 c_parse_file (void)
15517 bool error_occurred;
15519 the_parser = cp_parser_new ();
15520 push_deferring_access_checks (flag_access_control
15521 ? dk_no_deferred : dk_no_check);
15522 error_occurred = cp_parser_translation_unit (the_parser);
15523 the_parser = NULL;
15526 /* This variable must be provided by every front end. */
15528 int yydebug;
15530 #include "gt-cp-parser.h"