PR c++/3478
[official-gcc.git] / gcc / cp / parser.c
blob2f5c9d91d74d524530d08889c3e66b16cb293529
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
39 /* The lexer. */
41 /* Overview
42 --------
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
45 look-ahead.
47 Methodology
48 -----------
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
64 circular buffer. */
66 /* A C++ token. */
68 typedef struct cp_token GTY (())
70 /* The kind of token. */
71 ENUM_BITFIELD (cpp_ttype) type : 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 ENUM_BITFIELD (rid) keyword : 8;
75 /* The value associated with this token, if any. */
76 tree value;
77 /* The location at which this token was found. */
78 location_t location;
79 } cp_token;
81 /* The number of tokens in a single token block.
82 Computed so that cp_token_block fits in a 512B allocation unit. */
84 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
86 /* A group of tokens. These groups are chained together to store
87 large numbers of tokens. (For example, a token block is created
88 when the body of an inline member function is first encountered;
89 the tokens are processed later after the class definition is
90 complete.)
92 This somewhat ungainly data structure (as opposed to, say, a
93 variable-length array), is used due to constraints imposed by the
94 current garbage-collection methodology. If it is made more
95 flexible, we could perhaps simplify the data structures involved. */
97 typedef struct cp_token_block GTY (())
99 /* The tokens. */
100 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
101 /* The number of tokens in this block. */
102 size_t num_tokens;
103 /* The next token block in the chain. */
104 struct cp_token_block *next;
105 /* The previous block in the chain. */
106 struct cp_token_block *prev;
107 } cp_token_block;
109 typedef struct cp_token_cache GTY (())
111 /* The first block in the cache. NULL if there are no tokens in the
112 cache. */
113 cp_token_block *first;
114 /* The last block in the cache. NULL If there are no tokens in the
115 cache. */
116 cp_token_block *last;
117 } cp_token_cache;
119 /* Prototypes. */
121 static cp_token_cache *cp_token_cache_new
122 (void);
123 static void cp_token_cache_push_token
124 (cp_token_cache *, cp_token *);
126 /* Create a new cp_token_cache. */
128 static cp_token_cache *
129 cp_token_cache_new (void)
131 return ggc_alloc_cleared (sizeof (cp_token_cache));
134 /* Add *TOKEN to *CACHE. */
136 static void
137 cp_token_cache_push_token (cp_token_cache *cache,
138 cp_token *token)
140 cp_token_block *b = cache->last;
142 /* See if we need to allocate a new token block. */
143 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
145 b = ggc_alloc_cleared (sizeof (cp_token_block));
146 b->prev = cache->last;
147 if (cache->last)
149 cache->last->next = b;
150 cache->last = b;
152 else
153 cache->first = cache->last = b;
155 /* Add this token to the current token block. */
156 b->tokens[b->num_tokens++] = *token;
159 /* The cp_lexer structure represents the C++ lexer. It is responsible
160 for managing the token stream from the preprocessor and supplying
161 it to the parser. */
163 typedef struct cp_lexer GTY (())
165 /* The memory allocated for the buffer. Never NULL. */
166 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
167 /* A pointer just past the end of the memory allocated for the buffer. */
168 cp_token * GTY ((skip (""))) buffer_end;
169 /* The first valid token in the buffer, or NULL if none. */
170 cp_token * GTY ((skip (""))) first_token;
171 /* The next available token. If NEXT_TOKEN is NULL, then there are
172 no more available tokens. */
173 cp_token * GTY ((skip (""))) next_token;
174 /* A pointer just past the last available token. If FIRST_TOKEN is
175 NULL, however, there are no available tokens, and then this
176 location is simply the place in which the next token read will be
177 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
178 When the LAST_TOKEN == BUFFER, then the last token is at the
179 highest memory address in the BUFFER. */
180 cp_token * GTY ((skip (""))) last_token;
182 /* A stack indicating positions at which cp_lexer_save_tokens was
183 called. The top entry is the most recent position at which we
184 began saving tokens. The entries are differences in token
185 position between FIRST_TOKEN and the first saved token.
187 If the stack is non-empty, we are saving tokens. When a token is
188 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
189 pointer will not. The token stream will be preserved so that it
190 can be reexamined later.
192 If the stack is empty, then we are not saving tokens. Whenever a
193 token is consumed, the FIRST_TOKEN pointer will be moved, and the
194 consumed token will be gone forever. */
195 varray_type saved_tokens;
197 /* The STRING_CST tokens encountered while processing the current
198 string literal. */
199 varray_type string_tokens;
201 /* True if we should obtain more tokens from the preprocessor; false
202 if we are processing a saved token cache. */
203 bool main_lexer_p;
205 /* True if we should output debugging information. */
206 bool debugging_p;
208 /* The next lexer in a linked list of lexers. */
209 struct cp_lexer *next;
210 } cp_lexer;
212 /* Prototypes. */
214 static cp_lexer *cp_lexer_new_main
215 (void);
216 static cp_lexer *cp_lexer_new_from_tokens
217 (struct cp_token_cache *);
218 static int cp_lexer_saving_tokens
219 (const cp_lexer *);
220 static cp_token *cp_lexer_next_token
221 (cp_lexer *, cp_token *);
222 static cp_token *cp_lexer_prev_token
223 (cp_lexer *, cp_token *);
224 static ptrdiff_t cp_lexer_token_difference
225 (cp_lexer *, cp_token *, cp_token *);
226 static cp_token *cp_lexer_read_token
227 (cp_lexer *);
228 static void cp_lexer_maybe_grow_buffer
229 (cp_lexer *);
230 static void cp_lexer_get_preprocessor_token
231 (cp_lexer *, cp_token *);
232 static cp_token *cp_lexer_peek_token
233 (cp_lexer *);
234 static cp_token *cp_lexer_peek_nth_token
235 (cp_lexer *, size_t);
236 static inline bool cp_lexer_next_token_is
237 (cp_lexer *, enum cpp_ttype);
238 static bool cp_lexer_next_token_is_not
239 (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_keyword
241 (cp_lexer *, enum rid);
242 static cp_token *cp_lexer_consume_token
243 (cp_lexer *);
244 static void cp_lexer_purge_token
245 (cp_lexer *);
246 static void cp_lexer_purge_tokens_after
247 (cp_lexer *, cp_token *);
248 static void cp_lexer_save_tokens
249 (cp_lexer *);
250 static void cp_lexer_commit_tokens
251 (cp_lexer *);
252 static void cp_lexer_rollback_tokens
253 (cp_lexer *);
254 static inline void cp_lexer_set_source_position_from_token
255 (cp_lexer *, const cp_token *);
256 static void cp_lexer_print_token
257 (FILE *, cp_token *);
258 static inline bool cp_lexer_debugging_p
259 (cp_lexer *);
260 static void cp_lexer_start_debugging
261 (cp_lexer *) ATTRIBUTE_UNUSED;
262 static void cp_lexer_stop_debugging
263 (cp_lexer *) ATTRIBUTE_UNUSED;
265 /* Manifest constants. */
267 #define CP_TOKEN_BUFFER_SIZE 5
268 #define CP_SAVED_TOKENS_SIZE 5
270 /* A token type for keywords, as opposed to ordinary identifiers. */
271 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
273 /* A token type for template-ids. If a template-id is processed while
274 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275 the value of the CPP_TEMPLATE_ID is whatever was returned by
276 cp_parser_template_id. */
277 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
279 /* A token type for nested-name-specifiers. If a
280 nested-name-specifier is processed while parsing tentatively, it is
281 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283 cp_parser_nested_name_specifier_opt. */
284 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
286 /* A token type for tokens that are not tokens at all; these are used
287 to mark the end of a token block. */
288 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
290 /* Variables. */
292 /* The stream to which debugging output should be written. */
293 static FILE *cp_lexer_debug_stream;
295 /* Create a new main C++ lexer, the lexer that gets tokens from the
296 preprocessor. */
298 static cp_lexer *
299 cp_lexer_new_main (void)
301 cp_lexer *lexer;
302 cp_token first_token;
304 /* It's possible that lexing the first token will load a PCH file,
305 which is a GC collection point. So we have to grab the first
306 token before allocating any memory. */
307 cp_lexer_get_preprocessor_token (NULL, &first_token);
308 c_common_no_more_pch ();
310 /* Allocate the memory. */
311 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
313 /* Create the circular buffer. */
314 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
315 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
317 /* There is one token in the buffer. */
318 lexer->last_token = lexer->buffer + 1;
319 lexer->first_token = lexer->buffer;
320 lexer->next_token = lexer->buffer;
321 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
323 /* This lexer obtains more tokens by calling c_lex. */
324 lexer->main_lexer_p = true;
326 /* Create the SAVED_TOKENS stack. */
327 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
329 /* Create the STRINGS array. */
330 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
332 /* Assume we are not debugging. */
333 lexer->debugging_p = false;
335 return lexer;
338 /* Create a new lexer whose token stream is primed with the TOKENS.
339 When these tokens are exhausted, no new tokens will be read. */
341 static cp_lexer *
342 cp_lexer_new_from_tokens (cp_token_cache *tokens)
344 cp_lexer *lexer;
345 cp_token *token;
346 cp_token_block *block;
347 ptrdiff_t num_tokens;
349 /* Allocate the memory. */
350 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
352 /* Create a new buffer, appropriately sized. */
353 num_tokens = 0;
354 for (block = tokens->first; block != NULL; block = block->next)
355 num_tokens += block->num_tokens;
356 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
357 lexer->buffer_end = lexer->buffer + num_tokens;
359 /* Install the tokens. */
360 token = lexer->buffer;
361 for (block = tokens->first; block != NULL; block = block->next)
363 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
364 token += block->num_tokens;
367 /* The FIRST_TOKEN is the beginning of the buffer. */
368 lexer->first_token = lexer->buffer;
369 /* The next available token is also at the beginning of the buffer. */
370 lexer->next_token = lexer->buffer;
371 /* The buffer is full. */
372 lexer->last_token = lexer->first_token;
374 /* This lexer doesn't obtain more tokens. */
375 lexer->main_lexer_p = false;
377 /* Create the SAVED_TOKENS stack. */
378 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
380 /* Create the STRINGS array. */
381 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
383 /* Assume we are not debugging. */
384 lexer->debugging_p = false;
386 return lexer;
389 /* Returns nonzero if debugging information should be output. */
391 static inline bool
392 cp_lexer_debugging_p (cp_lexer *lexer)
394 return lexer->debugging_p;
397 /* Set the current source position from the information stored in
398 TOKEN. */
400 static inline void
401 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
402 const cp_token *token)
404 /* Ideally, the source position information would not be a global
405 variable, but it is. */
407 /* Update the line number. */
408 if (token->type != CPP_EOF)
409 input_location = token->location;
412 /* TOKEN points into the circular token buffer. Return a pointer to
413 the next token in the buffer. */
415 static inline cp_token *
416 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
418 token++;
419 if (token == lexer->buffer_end)
420 token = lexer->buffer;
421 return token;
424 /* TOKEN points into the circular token buffer. Return a pointer to
425 the previous token in the buffer. */
427 static inline cp_token *
428 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
430 if (token == lexer->buffer)
431 token = lexer->buffer_end;
432 return token - 1;
435 /* nonzero if we are presently saving tokens. */
437 static int
438 cp_lexer_saving_tokens (const cp_lexer* lexer)
440 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
443 /* Return a pointer to the token that is N tokens beyond TOKEN in the
444 buffer. */
446 static cp_token *
447 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
449 token += n;
450 if (token >= lexer->buffer_end)
451 token = lexer->buffer + (token - lexer->buffer_end);
452 return token;
455 /* Returns the number of times that START would have to be incremented
456 to reach FINISH. If START and FINISH are the same, returns zero. */
458 static ptrdiff_t
459 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
461 if (finish >= start)
462 return finish - start;
463 else
464 return ((lexer->buffer_end - lexer->buffer)
465 - (start - finish));
468 /* Obtain another token from the C preprocessor and add it to the
469 token buffer. Returns the newly read token. */
471 static cp_token *
472 cp_lexer_read_token (cp_lexer* lexer)
474 cp_token *token;
476 /* Make sure there is room in the buffer. */
477 cp_lexer_maybe_grow_buffer (lexer);
479 /* If there weren't any tokens, then this one will be the first. */
480 if (!lexer->first_token)
481 lexer->first_token = lexer->last_token;
482 /* Similarly, if there were no available tokens, there is one now. */
483 if (!lexer->next_token)
484 lexer->next_token = lexer->last_token;
486 /* Figure out where we're going to store the new token. */
487 token = lexer->last_token;
489 /* Get a new token from the preprocessor. */
490 cp_lexer_get_preprocessor_token (lexer, token);
492 /* Increment LAST_TOKEN. */
493 lexer->last_token = cp_lexer_next_token (lexer, token);
495 /* Strings should have type `const char []'. Right now, we will
496 have an ARRAY_TYPE that is constant rather than an array of
497 constant elements.
498 FIXME: Make fix_string_type get this right in the first place. */
499 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
500 && flag_const_strings)
502 tree type;
504 /* Get the current type. It will be an ARRAY_TYPE. */
505 type = TREE_TYPE (token->value);
506 /* Use build_cplus_array_type to rebuild the array, thereby
507 getting the right type. */
508 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
509 /* Reset the type of the token. */
510 TREE_TYPE (token->value) = type;
513 return token;
516 /* If the circular buffer is full, make it bigger. */
518 static void
519 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
521 /* If the buffer is full, enlarge it. */
522 if (lexer->last_token == lexer->first_token)
524 cp_token *new_buffer;
525 cp_token *old_buffer;
526 cp_token *new_first_token;
527 ptrdiff_t buffer_length;
528 size_t num_tokens_to_copy;
530 /* Remember the current buffer pointer. It will become invalid,
531 but we will need to do pointer arithmetic involving this
532 value. */
533 old_buffer = lexer->buffer;
534 /* Compute the current buffer size. */
535 buffer_length = lexer->buffer_end - lexer->buffer;
536 /* Allocate a buffer twice as big. */
537 new_buffer = ggc_realloc (lexer->buffer,
538 2 * buffer_length * sizeof (cp_token));
540 /* Because the buffer is circular, logically consecutive tokens
541 are not necessarily placed consecutively in memory.
542 Therefore, we must keep move the tokens that were before
543 FIRST_TOKEN to the second half of the newly allocated
544 buffer. */
545 num_tokens_to_copy = (lexer->first_token - old_buffer);
546 memcpy (new_buffer + buffer_length,
547 new_buffer,
548 num_tokens_to_copy * sizeof (cp_token));
549 /* Clear the rest of the buffer. We never look at this storage,
550 but the garbage collector may. */
551 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
552 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
554 /* Now recompute all of the buffer pointers. */
555 new_first_token
556 = new_buffer + (lexer->first_token - old_buffer);
557 if (lexer->next_token != NULL)
559 ptrdiff_t next_token_delta;
561 if (lexer->next_token > lexer->first_token)
562 next_token_delta = lexer->next_token - lexer->first_token;
563 else
564 next_token_delta =
565 buffer_length - (lexer->first_token - lexer->next_token);
566 lexer->next_token = new_first_token + next_token_delta;
568 lexer->last_token = new_first_token + buffer_length;
569 lexer->buffer = new_buffer;
570 lexer->buffer_end = new_buffer + buffer_length * 2;
571 lexer->first_token = new_first_token;
575 /* Store the next token from the preprocessor in *TOKEN. */
577 static void
578 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
579 cp_token *token)
581 bool done;
583 /* If this not the main lexer, return a terminating CPP_EOF token. */
584 if (lexer != NULL && !lexer->main_lexer_p)
586 token->type = CPP_EOF;
587 token->location.line = 0;
588 token->location.file = NULL;
589 token->value = NULL_TREE;
590 token->keyword = RID_MAX;
592 return;
595 done = false;
596 /* Keep going until we get a token we like. */
597 while (!done)
599 /* Get a new token from the preprocessor. */
600 token->type = c_lex (&token->value);
601 /* Issue messages about tokens we cannot process. */
602 switch (token->type)
604 case CPP_ATSIGN:
605 case CPP_HASH:
606 case CPP_PASTE:
607 error ("invalid token");
608 break;
610 default:
611 /* This is a good token, so we exit the loop. */
612 done = true;
613 break;
616 /* Now we've got our token. */
617 token->location = input_location;
619 /* Check to see if this token is a keyword. */
620 if (token->type == CPP_NAME
621 && C_IS_RESERVED_WORD (token->value))
623 /* Mark this token as a keyword. */
624 token->type = CPP_KEYWORD;
625 /* Record which keyword. */
626 token->keyword = C_RID_CODE (token->value);
627 /* Update the value. Some keywords are mapped to particular
628 entities, rather than simply having the value of the
629 corresponding IDENTIFIER_NODE. For example, `__const' is
630 mapped to `const'. */
631 token->value = ridpointers[token->keyword];
633 else
634 token->keyword = RID_MAX;
637 /* Return a pointer to the next token in the token stream, but do not
638 consume it. */
640 static cp_token *
641 cp_lexer_peek_token (cp_lexer* lexer)
643 cp_token *token;
645 /* If there are no tokens, read one now. */
646 if (!lexer->next_token)
647 cp_lexer_read_token (lexer);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
652 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
653 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
654 fprintf (cp_lexer_debug_stream, "\n");
657 token = lexer->next_token;
658 cp_lexer_set_source_position_from_token (lexer, token);
659 return token;
662 /* Return true if the next token has the indicated TYPE. */
664 static bool
665 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
667 cp_token *token;
669 /* Peek at the next token. */
670 token = cp_lexer_peek_token (lexer);
671 /* Check to see if it has the indicated TYPE. */
672 return token->type == type;
675 /* Return true if the next token does not have the indicated TYPE. */
677 static bool
678 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
680 return !cp_lexer_next_token_is (lexer, type);
683 /* Return true if the next token is the indicated KEYWORD. */
685 static bool
686 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
688 cp_token *token;
690 /* Peek at the next token. */
691 token = cp_lexer_peek_token (lexer);
692 /* Check to see if it is the indicated keyword. */
693 return token->keyword == keyword;
696 /* Return a pointer to the Nth token in the token stream. If N is 1,
697 then this is precisely equivalent to cp_lexer_peek_token. */
699 static cp_token *
700 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
702 cp_token *token;
704 /* N is 1-based, not zero-based. */
705 my_friendly_assert (n > 0, 20000224);
707 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
708 token = lexer->next_token;
709 /* If there are no tokens in the buffer, get one now. */
710 if (!token)
712 cp_lexer_read_token (lexer);
713 token = lexer->next_token;
716 /* Now, read tokens until we have enough. */
717 while (--n > 0)
719 /* Advance to the next token. */
720 token = cp_lexer_next_token (lexer, token);
721 /* If that's all the tokens we have, read a new one. */
722 if (token == lexer->last_token)
723 token = cp_lexer_read_token (lexer);
726 return token;
729 /* Consume the next token. The pointer returned is valid only until
730 another token is read. Callers should preserve copy the token
731 explicitly if they will need its value for a longer period of
732 time. */
734 static cp_token *
735 cp_lexer_consume_token (cp_lexer* lexer)
737 cp_token *token;
739 /* If there are no tokens, read one now. */
740 if (!lexer->next_token)
741 cp_lexer_read_token (lexer);
743 /* Remember the token we'll be returning. */
744 token = lexer->next_token;
746 /* Increment NEXT_TOKEN. */
747 lexer->next_token = cp_lexer_next_token (lexer,
748 lexer->next_token);
749 /* Check to see if we're all out of tokens. */
750 if (lexer->next_token == lexer->last_token)
751 lexer->next_token = NULL;
753 /* If we're not saving tokens, then move FIRST_TOKEN too. */
754 if (!cp_lexer_saving_tokens (lexer))
756 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
757 if (!lexer->next_token)
758 lexer->first_token = NULL;
759 else
760 lexer->first_token = lexer->next_token;
763 /* Provide debugging output. */
764 if (cp_lexer_debugging_p (lexer))
766 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
767 cp_lexer_print_token (cp_lexer_debug_stream, token);
768 fprintf (cp_lexer_debug_stream, "\n");
771 return token;
774 /* Permanently remove the next token from the token stream. There
775 must be a valid next token already; this token never reads
776 additional tokens from the preprocessor. */
778 static void
779 cp_lexer_purge_token (cp_lexer *lexer)
781 cp_token *token;
782 cp_token *next_token;
784 token = lexer->next_token;
785 while (true)
787 next_token = cp_lexer_next_token (lexer, token);
788 if (next_token == lexer->last_token)
789 break;
790 *token = *next_token;
791 token = next_token;
794 lexer->last_token = token;
795 /* The token purged may have been the only token remaining; if so,
796 clear NEXT_TOKEN. */
797 if (lexer->next_token == token)
798 lexer->next_token = NULL;
801 /* Permanently remove all tokens after TOKEN, up to, but not
802 including, the token that will be returned next by
803 cp_lexer_peek_token. */
805 static void
806 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
808 cp_token *peek;
809 cp_token *t1;
810 cp_token *t2;
812 if (lexer->next_token)
814 /* Copy the tokens that have not yet been read to the location
815 immediately following TOKEN. */
816 t1 = cp_lexer_next_token (lexer, token);
817 t2 = peek = cp_lexer_peek_token (lexer);
818 /* Move tokens into the vacant area between TOKEN and PEEK. */
819 while (t2 != lexer->last_token)
821 *t1 = *t2;
822 t1 = cp_lexer_next_token (lexer, t1);
823 t2 = cp_lexer_next_token (lexer, t2);
825 /* Now, the next available token is right after TOKEN. */
826 lexer->next_token = cp_lexer_next_token (lexer, token);
827 /* And the last token is wherever we ended up. */
828 lexer->last_token = t1;
830 else
832 /* There are no tokens in the buffer, so there is nothing to
833 copy. The last token in the buffer is TOKEN itself. */
834 lexer->last_token = cp_lexer_next_token (lexer, token);
838 /* Begin saving tokens. All tokens consumed after this point will be
839 preserved. */
841 static void
842 cp_lexer_save_tokens (cp_lexer* lexer)
844 /* Provide debugging output. */
845 if (cp_lexer_debugging_p (lexer))
846 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
848 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
849 restore the tokens if required. */
850 if (!lexer->next_token)
851 cp_lexer_read_token (lexer);
853 VARRAY_PUSH_INT (lexer->saved_tokens,
854 cp_lexer_token_difference (lexer,
855 lexer->first_token,
856 lexer->next_token));
859 /* Commit to the portion of the token stream most recently saved. */
861 static void
862 cp_lexer_commit_tokens (cp_lexer* lexer)
864 /* Provide debugging output. */
865 if (cp_lexer_debugging_p (lexer))
866 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
868 VARRAY_POP (lexer->saved_tokens);
871 /* Return all tokens saved since the last call to cp_lexer_save_tokens
872 to the token stream. Stop saving tokens. */
874 static void
875 cp_lexer_rollback_tokens (cp_lexer* lexer)
877 size_t delta;
879 /* Provide debugging output. */
880 if (cp_lexer_debugging_p (lexer))
881 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
883 /* Find the token that was the NEXT_TOKEN when we started saving
884 tokens. */
885 delta = VARRAY_TOP_INT(lexer->saved_tokens);
886 /* Make it the next token again now. */
887 lexer->next_token = cp_lexer_advance_token (lexer,
888 lexer->first_token,
889 delta);
890 /* It might be the case that there were no tokens when we started
891 saving tokens, but that there are some tokens now. */
892 if (!lexer->next_token && lexer->first_token)
893 lexer->next_token = lexer->first_token;
895 /* Stop saving tokens. */
896 VARRAY_POP (lexer->saved_tokens);
899 /* Print a representation of the TOKEN on the STREAM. */
901 static void
902 cp_lexer_print_token (FILE * stream, cp_token* token)
904 const char *token_type = NULL;
906 /* Figure out what kind of token this is. */
907 switch (token->type)
909 case CPP_EQ:
910 token_type = "EQ";
911 break;
913 case CPP_COMMA:
914 token_type = "COMMA";
915 break;
917 case CPP_OPEN_PAREN:
918 token_type = "OPEN_PAREN";
919 break;
921 case CPP_CLOSE_PAREN:
922 token_type = "CLOSE_PAREN";
923 break;
925 case CPP_OPEN_BRACE:
926 token_type = "OPEN_BRACE";
927 break;
929 case CPP_CLOSE_BRACE:
930 token_type = "CLOSE_BRACE";
931 break;
933 case CPP_SEMICOLON:
934 token_type = "SEMICOLON";
935 break;
937 case CPP_NAME:
938 token_type = "NAME";
939 break;
941 case CPP_EOF:
942 token_type = "EOF";
943 break;
945 case CPP_KEYWORD:
946 token_type = "keyword";
947 break;
949 /* This is not a token that we know how to handle yet. */
950 default:
951 break;
954 /* If we have a name for the token, print it out. Otherwise, we
955 simply give the numeric code. */
956 if (token_type)
957 fprintf (stream, "%s", token_type);
958 else
959 fprintf (stream, "%d", token->type);
960 /* And, for an identifier, print the identifier name. */
961 if (token->type == CPP_NAME
962 /* Some keywords have a value that is not an IDENTIFIER_NODE.
963 For example, `struct' is mapped to an INTEGER_CST. */
964 || (token->type == CPP_KEYWORD
965 && TREE_CODE (token->value) == IDENTIFIER_NODE))
966 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
969 /* Start emitting debugging information. */
971 static void
972 cp_lexer_start_debugging (cp_lexer* lexer)
974 ++lexer->debugging_p;
977 /* Stop emitting debugging information. */
979 static void
980 cp_lexer_stop_debugging (cp_lexer* lexer)
982 --lexer->debugging_p;
986 /* The parser. */
988 /* Overview
989 --------
991 A cp_parser parses the token stream as specified by the C++
992 grammar. Its job is purely parsing, not semantic analysis. For
993 example, the parser breaks the token stream into declarators,
994 expressions, statements, and other similar syntactic constructs.
995 It does not check that the types of the expressions on either side
996 of an assignment-statement are compatible, or that a function is
997 not declared with a parameter of type `void'.
999 The parser invokes routines elsewhere in the compiler to perform
1000 semantic analysis and to build up the abstract syntax tree for the
1001 code processed.
1003 The parser (and the template instantiation code, which is, in a
1004 way, a close relative of parsing) are the only parts of the
1005 compiler that should be calling push_scope and pop_scope, or
1006 related functions. The parser (and template instantiation code)
1007 keeps track of what scope is presently active; everything else
1008 should simply honor that. (The code that generates static
1009 initializers may also need to set the scope, in order to check
1010 access control correctly when emitting the initializers.)
1012 Methodology
1013 -----------
1015 The parser is of the standard recursive-descent variety. Upcoming
1016 tokens in the token stream are examined in order to determine which
1017 production to use when parsing a non-terminal. Some C++ constructs
1018 require arbitrary look ahead to disambiguate. For example, it is
1019 impossible, in the general case, to tell whether a statement is an
1020 expression or declaration without scanning the entire statement.
1021 Therefore, the parser is capable of "parsing tentatively." When the
1022 parser is not sure what construct comes next, it enters this mode.
1023 Then, while we attempt to parse the construct, the parser queues up
1024 error messages, rather than issuing them immediately, and saves the
1025 tokens it consumes. If the construct is parsed successfully, the
1026 parser "commits", i.e., it issues any queued error messages and
1027 the tokens that were being preserved are permanently discarded.
1028 If, however, the construct is not parsed successfully, the parser
1029 rolls back its state completely so that it can resume parsing using
1030 a different alternative.
1032 Future Improvements
1033 -------------------
1035 The performance of the parser could probably be improved
1036 substantially. Some possible improvements include:
1038 - The expression parser recurses through the various levels of
1039 precedence as specified in the grammar, rather than using an
1040 operator-precedence technique. Therefore, parsing a simple
1041 identifier requires multiple recursive calls.
1043 - We could often eliminate the need to parse tentatively by
1044 looking ahead a little bit. In some places, this approach
1045 might not entirely eliminate the need to parse tentatively, but
1046 it might still speed up the average case. */
1048 /* Flags that are passed to some parsing functions. These values can
1049 be bitwise-ored together. */
1051 typedef enum cp_parser_flags
1053 /* No flags. */
1054 CP_PARSER_FLAGS_NONE = 0x0,
1055 /* The construct is optional. If it is not present, then no error
1056 should be issued. */
1057 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1058 /* When parsing a type-specifier, do not allow user-defined types. */
1059 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1060 } cp_parser_flags;
1062 /* The different kinds of declarators we want to parse. */
1064 typedef enum cp_parser_declarator_kind
1066 /* We want an abstract declartor. */
1067 CP_PARSER_DECLARATOR_ABSTRACT,
1068 /* We want a named declarator. */
1069 CP_PARSER_DECLARATOR_NAMED,
1070 /* We don't mind, but the name must be an unqualified-id. */
1071 CP_PARSER_DECLARATOR_EITHER
1072 } cp_parser_declarator_kind;
1074 /* A mapping from a token type to a corresponding tree node type. */
1076 typedef struct cp_parser_token_tree_map_node
1078 /* The token type. */
1079 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1080 /* The corresponding tree code. */
1081 ENUM_BITFIELD (tree_code) tree_type : 8;
1082 } cp_parser_token_tree_map_node;
1084 /* A complete map consists of several ordinary entries, followed by a
1085 terminator. The terminating entry has a token_type of CPP_EOF. */
1087 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1089 /* The status of a tentative parse. */
1091 typedef enum cp_parser_status_kind
1093 /* No errors have occurred. */
1094 CP_PARSER_STATUS_KIND_NO_ERROR,
1095 /* An error has occurred. */
1096 CP_PARSER_STATUS_KIND_ERROR,
1097 /* We are committed to this tentative parse, whether or not an error
1098 has occurred. */
1099 CP_PARSER_STATUS_KIND_COMMITTED
1100 } cp_parser_status_kind;
1102 /* Context that is saved and restored when parsing tentatively. */
1104 typedef struct cp_parser_context GTY (())
1106 /* If this is a tentative parsing context, the status of the
1107 tentative parse. */
1108 enum cp_parser_status_kind status;
1109 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1110 that are looked up in this context must be looked up both in the
1111 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1112 the context of the containing expression. */
1113 tree object_type;
1114 /* The next parsing context in the stack. */
1115 struct cp_parser_context *next;
1116 } cp_parser_context;
1118 /* Prototypes. */
1120 /* Constructors and destructors. */
1122 static cp_parser_context *cp_parser_context_new
1123 (cp_parser_context *);
1125 /* Class variables. */
1127 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1129 /* Constructors and destructors. */
1131 /* Construct a new context. The context below this one on the stack
1132 is given by NEXT. */
1134 static cp_parser_context *
1135 cp_parser_context_new (cp_parser_context* next)
1137 cp_parser_context *context;
1139 /* Allocate the storage. */
1140 if (cp_parser_context_free_list != NULL)
1142 /* Pull the first entry from the free list. */
1143 context = cp_parser_context_free_list;
1144 cp_parser_context_free_list = context->next;
1145 memset (context, 0, sizeof (*context));
1147 else
1148 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1149 /* No errors have occurred yet in this context. */
1150 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1151 /* If this is not the bottomost context, copy information that we
1152 need from the previous context. */
1153 if (next)
1155 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1156 expression, then we are parsing one in this context, too. */
1157 context->object_type = next->object_type;
1158 /* Thread the stack. */
1159 context->next = next;
1162 return context;
1165 /* The cp_parser structure represents the C++ parser. */
1167 typedef struct cp_parser GTY(())
1169 /* The lexer from which we are obtaining tokens. */
1170 cp_lexer *lexer;
1172 /* The scope in which names should be looked up. If NULL_TREE, then
1173 we look up names in the scope that is currently open in the
1174 source program. If non-NULL, this is either a TYPE or
1175 NAMESPACE_DECL for the scope in which we should look.
1177 This value is not cleared automatically after a name is looked
1178 up, so we must be careful to clear it before starting a new look
1179 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1180 will look up `Z' in the scope of `X', rather than the current
1181 scope.) Unfortunately, it is difficult to tell when name lookup
1182 is complete, because we sometimes peek at a token, look it up,
1183 and then decide not to consume it. */
1184 tree scope;
1186 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1187 last lookup took place. OBJECT_SCOPE is used if an expression
1188 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1189 respectively. QUALIFYING_SCOPE is used for an expression of the
1190 form "X::Y"; it refers to X. */
1191 tree object_scope;
1192 tree qualifying_scope;
1194 /* A stack of parsing contexts. All but the bottom entry on the
1195 stack will be tentative contexts.
1197 We parse tentatively in order to determine which construct is in
1198 use in some situations. For example, in order to determine
1199 whether a statement is an expression-statement or a
1200 declaration-statement we parse it tentatively as a
1201 declaration-statement. If that fails, we then reparse the same
1202 token stream as an expression-statement. */
1203 cp_parser_context *context;
1205 /* True if we are parsing GNU C++. If this flag is not set, then
1206 GNU extensions are not recognized. */
1207 bool allow_gnu_extensions_p;
1209 /* TRUE if the `>' token should be interpreted as the greater-than
1210 operator. FALSE if it is the end of a template-id or
1211 template-parameter-list. */
1212 bool greater_than_is_operator_p;
1214 /* TRUE if default arguments are allowed within a parameter list
1215 that starts at this point. FALSE if only a gnu extension makes
1216 them permissible. */
1217 bool default_arg_ok_p;
1219 /* TRUE if we are parsing an integral constant-expression. See
1220 [expr.const] for a precise definition. */
1221 bool integral_constant_expression_p;
1223 /* TRUE if we are parsing an integral constant-expression -- but a
1224 non-constant expression should be permitted as well. This flag
1225 is used when parsing an array bound so that GNU variable-length
1226 arrays are tolerated. */
1227 bool allow_non_integral_constant_expression_p;
1229 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1230 been seen that makes the expression non-constant. */
1231 bool non_integral_constant_expression_p;
1233 /* TRUE if we are parsing the argument to "__offsetof__". */
1234 bool in_offsetof_p;
1236 /* TRUE if local variable names and `this' are forbidden in the
1237 current context. */
1238 bool local_variables_forbidden_p;
1240 /* TRUE if the declaration we are parsing is part of a
1241 linkage-specification of the form `extern string-literal
1242 declaration'. */
1243 bool in_unbraced_linkage_specification_p;
1245 /* TRUE if we are presently parsing a declarator, after the
1246 direct-declarator. */
1247 bool in_declarator_p;
1249 /* TRUE if we are presently parsing a template-argument-list. */
1250 bool in_template_argument_list_p;
1252 /* TRUE if we are presently parsing the body of an
1253 iteration-statement. */
1254 bool in_iteration_statement_p;
1256 /* TRUE if we are presently parsing the body of a switch
1257 statement. */
1258 bool in_switch_statement_p;
1260 /* TRUE if we are parsing a type-id in an expression context. In
1261 such a situation, both "type (expr)" and "type (type)" are valid
1262 alternatives. */
1263 bool in_type_id_in_expr_p;
1265 /* If non-NULL, then we are parsing a construct where new type
1266 definitions are not permitted. The string stored here will be
1267 issued as an error message if a type is defined. */
1268 const char *type_definition_forbidden_message;
1270 /* A list of lists. The outer list is a stack, used for member
1271 functions of local classes. At each level there are two sub-list,
1272 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1273 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1274 TREE_VALUE's. The functions are chained in reverse declaration
1275 order.
1277 The TREE_PURPOSE sublist contains those functions with default
1278 arguments that need post processing, and the TREE_VALUE sublist
1279 contains those functions with definitions that need post
1280 processing.
1282 These lists can only be processed once the outermost class being
1283 defined is complete. */
1284 tree unparsed_functions_queues;
1286 /* The number of classes whose definitions are currently in
1287 progress. */
1288 unsigned num_classes_being_defined;
1290 /* The number of template parameter lists that apply directly to the
1291 current declaration. */
1292 unsigned num_template_parameter_lists;
1293 } cp_parser;
1295 /* The type of a function that parses some kind of expression. */
1296 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1298 /* Prototypes. */
1300 /* Constructors and destructors. */
1302 static cp_parser *cp_parser_new
1303 (void);
1305 /* Routines to parse various constructs.
1307 Those that return `tree' will return the error_mark_node (rather
1308 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1309 Sometimes, they will return an ordinary node if error-recovery was
1310 attempted, even though a parse error occurred. So, to check
1311 whether or not a parse error occurred, you should always use
1312 cp_parser_error_occurred. If the construct is optional (indicated
1313 either by an `_opt' in the name of the function that does the
1314 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1315 the construct is not present. */
1317 /* Lexical conventions [gram.lex] */
1319 static tree cp_parser_identifier
1320 (cp_parser *);
1322 /* Basic concepts [gram.basic] */
1324 static bool cp_parser_translation_unit
1325 (cp_parser *);
1327 /* Expressions [gram.expr] */
1329 static tree cp_parser_primary_expression
1330 (cp_parser *, cp_id_kind *, tree *);
1331 static tree cp_parser_id_expression
1332 (cp_parser *, bool, bool, bool *, bool);
1333 static tree cp_parser_unqualified_id
1334 (cp_parser *, bool, bool, bool);
1335 static tree cp_parser_nested_name_specifier_opt
1336 (cp_parser *, bool, bool, bool, bool);
1337 static tree cp_parser_nested_name_specifier
1338 (cp_parser *, bool, bool, bool, bool);
1339 static tree cp_parser_class_or_namespace_name
1340 (cp_parser *, bool, bool, bool, bool, bool);
1341 static tree cp_parser_postfix_expression
1342 (cp_parser *, bool);
1343 static tree cp_parser_parenthesized_expression_list
1344 (cp_parser *, bool, bool *);
1345 static void cp_parser_pseudo_destructor_name
1346 (cp_parser *, tree *, tree *);
1347 static tree cp_parser_unary_expression
1348 (cp_parser *, bool);
1349 static enum tree_code cp_parser_unary_operator
1350 (cp_token *);
1351 static tree cp_parser_new_expression
1352 (cp_parser *);
1353 static tree cp_parser_new_placement
1354 (cp_parser *);
1355 static tree cp_parser_new_type_id
1356 (cp_parser *);
1357 static tree cp_parser_new_declarator_opt
1358 (cp_parser *);
1359 static tree cp_parser_direct_new_declarator
1360 (cp_parser *);
1361 static tree cp_parser_new_initializer
1362 (cp_parser *);
1363 static tree cp_parser_delete_expression
1364 (cp_parser *);
1365 static tree cp_parser_cast_expression
1366 (cp_parser *, bool);
1367 static tree cp_parser_pm_expression
1368 (cp_parser *);
1369 static tree cp_parser_multiplicative_expression
1370 (cp_parser *);
1371 static tree cp_parser_additive_expression
1372 (cp_parser *);
1373 static tree cp_parser_shift_expression
1374 (cp_parser *);
1375 static tree cp_parser_relational_expression
1376 (cp_parser *);
1377 static tree cp_parser_equality_expression
1378 (cp_parser *);
1379 static tree cp_parser_and_expression
1380 (cp_parser *);
1381 static tree cp_parser_exclusive_or_expression
1382 (cp_parser *);
1383 static tree cp_parser_inclusive_or_expression
1384 (cp_parser *);
1385 static tree cp_parser_logical_and_expression
1386 (cp_parser *);
1387 static tree cp_parser_logical_or_expression
1388 (cp_parser *);
1389 static tree cp_parser_question_colon_clause
1390 (cp_parser *, tree);
1391 static tree cp_parser_assignment_expression
1392 (cp_parser *);
1393 static enum tree_code cp_parser_assignment_operator_opt
1394 (cp_parser *);
1395 static tree cp_parser_expression
1396 (cp_parser *);
1397 static tree cp_parser_constant_expression
1398 (cp_parser *, bool, bool *);
1400 /* Statements [gram.stmt.stmt] */
1402 static void cp_parser_statement
1403 (cp_parser *, bool);
1404 static tree cp_parser_labeled_statement
1405 (cp_parser *, bool);
1406 static tree cp_parser_expression_statement
1407 (cp_parser *, bool);
1408 static tree cp_parser_compound_statement
1409 (cp_parser *, bool);
1410 static void cp_parser_statement_seq_opt
1411 (cp_parser *, bool);
1412 static tree cp_parser_selection_statement
1413 (cp_parser *);
1414 static tree cp_parser_condition
1415 (cp_parser *);
1416 static tree cp_parser_iteration_statement
1417 (cp_parser *);
1418 static void cp_parser_for_init_statement
1419 (cp_parser *);
1420 static tree cp_parser_jump_statement
1421 (cp_parser *);
1422 static void cp_parser_declaration_statement
1423 (cp_parser *);
1425 static tree cp_parser_implicitly_scoped_statement
1426 (cp_parser *);
1427 static void cp_parser_already_scoped_statement
1428 (cp_parser *);
1430 /* Declarations [gram.dcl.dcl] */
1432 static void cp_parser_declaration_seq_opt
1433 (cp_parser *);
1434 static void cp_parser_declaration
1435 (cp_parser *);
1436 static void cp_parser_block_declaration
1437 (cp_parser *, bool);
1438 static void cp_parser_simple_declaration
1439 (cp_parser *, bool);
1440 static tree cp_parser_decl_specifier_seq
1441 (cp_parser *, cp_parser_flags, tree *, int *);
1442 static tree cp_parser_storage_class_specifier_opt
1443 (cp_parser *);
1444 static tree cp_parser_function_specifier_opt
1445 (cp_parser *);
1446 static tree cp_parser_type_specifier
1447 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1448 static tree cp_parser_simple_type_specifier
1449 (cp_parser *, cp_parser_flags, bool);
1450 static tree cp_parser_type_name
1451 (cp_parser *);
1452 static tree cp_parser_elaborated_type_specifier
1453 (cp_parser *, bool, bool);
1454 static tree cp_parser_enum_specifier
1455 (cp_parser *);
1456 static void cp_parser_enumerator_list
1457 (cp_parser *, tree);
1458 static void cp_parser_enumerator_definition
1459 (cp_parser *, tree);
1460 static tree cp_parser_namespace_name
1461 (cp_parser *);
1462 static void cp_parser_namespace_definition
1463 (cp_parser *);
1464 static void cp_parser_namespace_body
1465 (cp_parser *);
1466 static tree cp_parser_qualified_namespace_specifier
1467 (cp_parser *);
1468 static void cp_parser_namespace_alias_definition
1469 (cp_parser *);
1470 static void cp_parser_using_declaration
1471 (cp_parser *);
1472 static void cp_parser_using_directive
1473 (cp_parser *);
1474 static void cp_parser_asm_definition
1475 (cp_parser *);
1476 static void cp_parser_linkage_specification
1477 (cp_parser *);
1479 /* Declarators [gram.dcl.decl] */
1481 static tree cp_parser_init_declarator
1482 (cp_parser *, tree, tree, bool, bool, int, bool *);
1483 static tree cp_parser_declarator
1484 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1485 static tree cp_parser_direct_declarator
1486 (cp_parser *, cp_parser_declarator_kind, int *);
1487 static enum tree_code cp_parser_ptr_operator
1488 (cp_parser *, tree *, tree *);
1489 static tree cp_parser_cv_qualifier_seq_opt
1490 (cp_parser *);
1491 static tree cp_parser_cv_qualifier_opt
1492 (cp_parser *);
1493 static tree cp_parser_declarator_id
1494 (cp_parser *);
1495 static tree cp_parser_type_id
1496 (cp_parser *);
1497 static tree cp_parser_type_specifier_seq
1498 (cp_parser *);
1499 static tree cp_parser_parameter_declaration_clause
1500 (cp_parser *);
1501 static tree cp_parser_parameter_declaration_list
1502 (cp_parser *);
1503 static tree cp_parser_parameter_declaration
1504 (cp_parser *, bool, bool *);
1505 static void cp_parser_function_body
1506 (cp_parser *);
1507 static tree cp_parser_initializer
1508 (cp_parser *, bool *, bool *);
1509 static tree cp_parser_initializer_clause
1510 (cp_parser *, bool *);
1511 static tree cp_parser_initializer_list
1512 (cp_parser *, bool *);
1514 static bool cp_parser_ctor_initializer_opt_and_function_body
1515 (cp_parser *);
1517 /* Classes [gram.class] */
1519 static tree cp_parser_class_name
1520 (cp_parser *, bool, bool, bool, bool, bool, bool);
1521 static tree cp_parser_class_specifier
1522 (cp_parser *);
1523 static tree cp_parser_class_head
1524 (cp_parser *, bool *);
1525 static enum tag_types cp_parser_class_key
1526 (cp_parser *);
1527 static void cp_parser_member_specification_opt
1528 (cp_parser *);
1529 static void cp_parser_member_declaration
1530 (cp_parser *);
1531 static tree cp_parser_pure_specifier
1532 (cp_parser *);
1533 static tree cp_parser_constant_initializer
1534 (cp_parser *);
1536 /* Derived classes [gram.class.derived] */
1538 static tree cp_parser_base_clause
1539 (cp_parser *);
1540 static tree cp_parser_base_specifier
1541 (cp_parser *);
1543 /* Special member functions [gram.special] */
1545 static tree cp_parser_conversion_function_id
1546 (cp_parser *);
1547 static tree cp_parser_conversion_type_id
1548 (cp_parser *);
1549 static tree cp_parser_conversion_declarator_opt
1550 (cp_parser *);
1551 static bool cp_parser_ctor_initializer_opt
1552 (cp_parser *);
1553 static void cp_parser_mem_initializer_list
1554 (cp_parser *);
1555 static tree cp_parser_mem_initializer
1556 (cp_parser *);
1557 static tree cp_parser_mem_initializer_id
1558 (cp_parser *);
1560 /* Overloading [gram.over] */
1562 static tree cp_parser_operator_function_id
1563 (cp_parser *);
1564 static tree cp_parser_operator
1565 (cp_parser *);
1567 /* Templates [gram.temp] */
1569 static void cp_parser_template_declaration
1570 (cp_parser *, bool);
1571 static tree cp_parser_template_parameter_list
1572 (cp_parser *);
1573 static tree cp_parser_template_parameter
1574 (cp_parser *);
1575 static tree cp_parser_type_parameter
1576 (cp_parser *);
1577 static tree cp_parser_template_id
1578 (cp_parser *, bool, bool, bool);
1579 static tree cp_parser_template_name
1580 (cp_parser *, bool, bool, bool, bool *);
1581 static tree cp_parser_template_argument_list
1582 (cp_parser *);
1583 static tree cp_parser_template_argument
1584 (cp_parser *);
1585 static void cp_parser_explicit_instantiation
1586 (cp_parser *);
1587 static void cp_parser_explicit_specialization
1588 (cp_parser *);
1590 /* Exception handling [gram.exception] */
1592 static tree cp_parser_try_block
1593 (cp_parser *);
1594 static bool cp_parser_function_try_block
1595 (cp_parser *);
1596 static void cp_parser_handler_seq
1597 (cp_parser *);
1598 static void cp_parser_handler
1599 (cp_parser *);
1600 static tree cp_parser_exception_declaration
1601 (cp_parser *);
1602 static tree cp_parser_throw_expression
1603 (cp_parser *);
1604 static tree cp_parser_exception_specification_opt
1605 (cp_parser *);
1606 static tree cp_parser_type_id_list
1607 (cp_parser *);
1609 /* GNU Extensions */
1611 static tree cp_parser_asm_specification_opt
1612 (cp_parser *);
1613 static tree cp_parser_asm_operand_list
1614 (cp_parser *);
1615 static tree cp_parser_asm_clobber_list
1616 (cp_parser *);
1617 static tree cp_parser_attributes_opt
1618 (cp_parser *);
1619 static tree cp_parser_attribute_list
1620 (cp_parser *);
1621 static bool cp_parser_extension_opt
1622 (cp_parser *, int *);
1623 static void cp_parser_label_declaration
1624 (cp_parser *);
1626 /* Utility Routines */
1628 static tree cp_parser_lookup_name
1629 (cp_parser *, tree, bool, bool, bool, bool);
1630 static tree cp_parser_lookup_name_simple
1631 (cp_parser *, tree);
1632 static tree cp_parser_maybe_treat_template_as_class
1633 (tree, bool);
1634 static bool cp_parser_check_declarator_template_parameters
1635 (cp_parser *, tree);
1636 static bool cp_parser_check_template_parameters
1637 (cp_parser *, unsigned);
1638 static tree cp_parser_simple_cast_expression
1639 (cp_parser *);
1640 static tree cp_parser_binary_expression
1641 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1642 static tree cp_parser_global_scope_opt
1643 (cp_parser *, bool);
1644 static bool cp_parser_constructor_declarator_p
1645 (cp_parser *, bool);
1646 static tree cp_parser_function_definition_from_specifiers_and_declarator
1647 (cp_parser *, tree, tree, tree);
1648 static tree cp_parser_function_definition_after_declarator
1649 (cp_parser *, bool);
1650 static void cp_parser_template_declaration_after_export
1651 (cp_parser *, bool);
1652 static tree cp_parser_single_declaration
1653 (cp_parser *, bool, bool *);
1654 static tree cp_parser_functional_cast
1655 (cp_parser *, tree);
1656 static tree cp_parser_save_member_function_body
1657 (cp_parser *, tree, tree, tree);
1658 static tree cp_parser_enclosed_template_argument_list
1659 (cp_parser *);
1660 static void cp_parser_save_default_args
1661 (cp_parser *, tree);
1662 static void cp_parser_late_parsing_for_member
1663 (cp_parser *, tree);
1664 static void cp_parser_late_parsing_default_args
1665 (cp_parser *, tree);
1666 static tree cp_parser_sizeof_operand
1667 (cp_parser *, enum rid);
1668 static bool cp_parser_declares_only_class_p
1669 (cp_parser *);
1670 static tree cp_parser_fold_non_dependent_expr
1671 (tree);
1672 static bool cp_parser_friend_p
1673 (tree);
1674 static cp_token *cp_parser_require
1675 (cp_parser *, enum cpp_ttype, const char *);
1676 static cp_token *cp_parser_require_keyword
1677 (cp_parser *, enum rid, const char *);
1678 static bool cp_parser_token_starts_function_definition_p
1679 (cp_token *);
1680 static bool cp_parser_next_token_starts_class_definition_p
1681 (cp_parser *);
1682 static bool cp_parser_next_token_ends_template_argument_p
1683 (cp_parser *);
1684 static enum tag_types cp_parser_token_is_class_key
1685 (cp_token *);
1686 static void cp_parser_check_class_key
1687 (enum tag_types, tree type);
1688 static void cp_parser_check_access_in_redeclaration
1689 (tree type);
1690 static bool cp_parser_optional_template_keyword
1691 (cp_parser *);
1692 static void cp_parser_pre_parsed_nested_name_specifier
1693 (cp_parser *);
1694 static void cp_parser_cache_group
1695 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1696 static void cp_parser_parse_tentatively
1697 (cp_parser *);
1698 static void cp_parser_commit_to_tentative_parse
1699 (cp_parser *);
1700 static void cp_parser_abort_tentative_parse
1701 (cp_parser *);
1702 static bool cp_parser_parse_definitely
1703 (cp_parser *);
1704 static inline bool cp_parser_parsing_tentatively
1705 (cp_parser *);
1706 static bool cp_parser_committed_to_tentative_parse
1707 (cp_parser *);
1708 static void cp_parser_error
1709 (cp_parser *, const char *);
1710 static void cp_parser_name_lookup_error
1711 (cp_parser *, tree, tree, const char *);
1712 static bool cp_parser_simulate_error
1713 (cp_parser *);
1714 static void cp_parser_check_type_definition
1715 (cp_parser *);
1716 static void cp_parser_check_for_definition_in_return_type
1717 (tree, int);
1718 static void cp_parser_check_for_invalid_template_id
1719 (cp_parser *, tree);
1720 static tree cp_parser_non_integral_constant_expression
1721 (const char *);
1722 static bool cp_parser_diagnose_invalid_type_name
1723 (cp_parser *);
1724 static int cp_parser_skip_to_closing_parenthesis
1725 (cp_parser *, bool, bool, bool);
1726 static void cp_parser_skip_to_end_of_statement
1727 (cp_parser *);
1728 static void cp_parser_consume_semicolon_at_end_of_statement
1729 (cp_parser *);
1730 static void cp_parser_skip_to_end_of_block_or_statement
1731 (cp_parser *);
1732 static void cp_parser_skip_to_closing_brace
1733 (cp_parser *);
1734 static void cp_parser_skip_until_found
1735 (cp_parser *, enum cpp_ttype, const char *);
1736 static bool cp_parser_error_occurred
1737 (cp_parser *);
1738 static bool cp_parser_allow_gnu_extensions_p
1739 (cp_parser *);
1740 static bool cp_parser_is_string_literal
1741 (cp_token *);
1742 static bool cp_parser_is_keyword
1743 (cp_token *, enum rid);
1745 /* Returns nonzero if we are parsing tentatively. */
1747 static inline bool
1748 cp_parser_parsing_tentatively (cp_parser* parser)
1750 return parser->context->next != NULL;
1753 /* Returns nonzero if TOKEN is a string literal. */
1755 static bool
1756 cp_parser_is_string_literal (cp_token* token)
1758 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1761 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1763 static bool
1764 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1766 return token->keyword == keyword;
1769 /* Issue the indicated error MESSAGE. */
1771 static void
1772 cp_parser_error (cp_parser* parser, const char* message)
1774 /* Output the MESSAGE -- unless we're parsing tentatively. */
1775 if (!cp_parser_simulate_error (parser))
1777 cp_token *token;
1778 token = cp_lexer_peek_token (parser->lexer);
1779 c_parse_error (message,
1780 /* Because c_parser_error does not understand
1781 CPP_KEYWORD, keywords are treated like
1782 identifiers. */
1783 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1784 token->value);
1788 /* Issue an error about name-lookup failing. NAME is the
1789 IDENTIFIER_NODE DECL is the result of
1790 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1791 the thing that we hoped to find. */
1793 static void
1794 cp_parser_name_lookup_error (cp_parser* parser,
1795 tree name,
1796 tree decl,
1797 const char* desired)
1799 /* If name lookup completely failed, tell the user that NAME was not
1800 declared. */
1801 if (decl == error_mark_node)
1803 if (parser->scope && parser->scope != global_namespace)
1804 error ("`%D::%D' has not been declared",
1805 parser->scope, name);
1806 else if (parser->scope == global_namespace)
1807 error ("`::%D' has not been declared", name);
1808 else
1809 error ("`%D' has not been declared", name);
1811 else if (parser->scope && parser->scope != global_namespace)
1812 error ("`%D::%D' %s", parser->scope, name, desired);
1813 else if (parser->scope == global_namespace)
1814 error ("`::%D' %s", name, desired);
1815 else
1816 error ("`%D' %s", name, desired);
1819 /* If we are parsing tentatively, remember that an error has occurred
1820 during this tentative parse. Returns true if the error was
1821 simulated; false if a messgae should be issued by the caller. */
1823 static bool
1824 cp_parser_simulate_error (cp_parser* parser)
1826 if (cp_parser_parsing_tentatively (parser)
1827 && !cp_parser_committed_to_tentative_parse (parser))
1829 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1830 return true;
1832 return false;
1835 /* This function is called when a type is defined. If type
1836 definitions are forbidden at this point, an error message is
1837 issued. */
1839 static void
1840 cp_parser_check_type_definition (cp_parser* parser)
1842 /* If types are forbidden here, issue a message. */
1843 if (parser->type_definition_forbidden_message)
1844 /* Use `%s' to print the string in case there are any escape
1845 characters in the message. */
1846 error ("%s", parser->type_definition_forbidden_message);
1849 /* This function is called when a declaration is parsed. If
1850 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1851 indicates that a type was defined in the decl-specifiers for DECL,
1852 then an error is issued. */
1854 static void
1855 cp_parser_check_for_definition_in_return_type (tree declarator,
1856 int declares_class_or_enum)
1858 /* [dcl.fct] forbids type definitions in return types.
1859 Unfortunately, it's not easy to know whether or not we are
1860 processing a return type until after the fact. */
1861 while (declarator
1862 && (TREE_CODE (declarator) == INDIRECT_REF
1863 || TREE_CODE (declarator) == ADDR_EXPR))
1864 declarator = TREE_OPERAND (declarator, 0);
1865 if (declarator
1866 && TREE_CODE (declarator) == CALL_EXPR
1867 && declares_class_or_enum & 2)
1868 error ("new types may not be defined in a return type");
1871 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1872 "<" in any valid C++ program. If the next token is indeed "<",
1873 issue a message warning the user about what appears to be an
1874 invalid attempt to form a template-id. */
1876 static void
1877 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1878 tree type)
1880 ptrdiff_t start;
1881 cp_token *token;
1883 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1885 if (TYPE_P (type))
1886 error ("`%T' is not a template", type);
1887 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1888 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1889 else
1890 error ("invalid template-id");
1891 /* Remember the location of the invalid "<". */
1892 if (cp_parser_parsing_tentatively (parser)
1893 && !cp_parser_committed_to_tentative_parse (parser))
1895 token = cp_lexer_peek_token (parser->lexer);
1896 token = cp_lexer_prev_token (parser->lexer, token);
1897 start = cp_lexer_token_difference (parser->lexer,
1898 parser->lexer->first_token,
1899 token);
1901 else
1902 start = -1;
1903 /* Consume the "<". */
1904 cp_lexer_consume_token (parser->lexer);
1905 /* Parse the template arguments. */
1906 cp_parser_enclosed_template_argument_list (parser);
1907 /* Permanently remove the invalid template arguments so that
1908 this error message is not issued again. */
1909 if (start >= 0)
1911 token = cp_lexer_advance_token (parser->lexer,
1912 parser->lexer->first_token,
1913 start);
1914 cp_lexer_purge_tokens_after (parser->lexer, token);
1919 /* Issue an error message about the fact that THING appeared in a
1920 constant-expression. Returns ERROR_MARK_NODE. */
1922 static tree
1923 cp_parser_non_integral_constant_expression (const char *thing)
1925 error ("%s cannot appear in a constant-expression", thing);
1926 return error_mark_node;
1929 /* Check for a common situation where a type-name should be present,
1930 but is not, and issue a sensible error message. Returns true if an
1931 invalid type-name was detected. */
1933 static bool
1934 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1936 /* If the next two tokens are both identifiers, the code is
1937 erroneous. The usual cause of this situation is code like:
1939 T t;
1941 where "T" should name a type -- but does not. */
1942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1943 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1945 tree name;
1947 /* If parsing tentatively, we should commit; we really are
1948 looking at a declaration. */
1949 /* Consume the first identifier. */
1950 name = cp_lexer_consume_token (parser->lexer)->value;
1951 /* Issue an error message. */
1952 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1953 /* If we're in a template class, it's possible that the user was
1954 referring to a type from a base class. For example:
1956 template <typename T> struct A { typedef T X; };
1957 template <typename T> struct B : public A<T> { X x; };
1959 The user should have said "typename A<T>::X". */
1960 if (processing_template_decl && current_class_type)
1962 tree b;
1964 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1966 b = TREE_CHAIN (b))
1968 tree base_type = BINFO_TYPE (b);
1969 if (CLASS_TYPE_P (base_type)
1970 && dependent_type_p (base_type))
1972 tree field;
1973 /* Go from a particular instantiation of the
1974 template (which will have an empty TYPE_FIELDs),
1975 to the main version. */
1976 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1977 for (field = TYPE_FIELDS (base_type);
1978 field;
1979 field = TREE_CHAIN (field))
1980 if (TREE_CODE (field) == TYPE_DECL
1981 && DECL_NAME (field) == name)
1983 error ("(perhaps `typename %T::%s' was intended)",
1984 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1985 break;
1987 if (field)
1988 break;
1992 /* Skip to the end of the declaration; there's no point in
1993 trying to process it. */
1994 cp_parser_skip_to_end_of_statement (parser);
1996 return true;
1999 return false;
2002 /* Consume tokens up to, and including, the next non-nested closing `)'.
2003 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2004 are doing error recovery. Returns -1 if OR_COMMA is true and we
2005 found an unnested comma. */
2007 static int
2008 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2009 bool recovering,
2010 bool or_comma,
2011 bool consume_paren)
2013 unsigned paren_depth = 0;
2014 unsigned brace_depth = 0;
2016 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2017 && !cp_parser_committed_to_tentative_parse (parser))
2018 return 0;
2020 while (true)
2022 cp_token *token;
2024 /* If we've run out of tokens, then there is no closing `)'. */
2025 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2026 return 0;
2028 token = cp_lexer_peek_token (parser->lexer);
2030 /* This matches the processing in skip_to_end_of_statement. */
2031 if (token->type == CPP_SEMICOLON && !brace_depth)
2032 return 0;
2033 if (token->type == CPP_OPEN_BRACE)
2034 ++brace_depth;
2035 if (token->type == CPP_CLOSE_BRACE)
2037 if (!brace_depth--)
2038 return 0;
2040 if (recovering && or_comma && token->type == CPP_COMMA
2041 && !brace_depth && !paren_depth)
2042 return -1;
2044 if (!brace_depth)
2046 /* If it is an `(', we have entered another level of nesting. */
2047 if (token->type == CPP_OPEN_PAREN)
2048 ++paren_depth;
2049 /* If it is a `)', then we might be done. */
2050 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2052 if (consume_paren)
2053 cp_lexer_consume_token (parser->lexer);
2054 return 1;
2058 /* Consume the token. */
2059 cp_lexer_consume_token (parser->lexer);
2063 /* Consume tokens until we reach the end of the current statement.
2064 Normally, that will be just before consuming a `;'. However, if a
2065 non-nested `}' comes first, then we stop before consuming that. */
2067 static void
2068 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2070 unsigned nesting_depth = 0;
2072 while (true)
2074 cp_token *token;
2076 /* Peek at the next token. */
2077 token = cp_lexer_peek_token (parser->lexer);
2078 /* If we've run out of tokens, stop. */
2079 if (token->type == CPP_EOF)
2080 break;
2081 /* If the next token is a `;', we have reached the end of the
2082 statement. */
2083 if (token->type == CPP_SEMICOLON && !nesting_depth)
2084 break;
2085 /* If the next token is a non-nested `}', then we have reached
2086 the end of the current block. */
2087 if (token->type == CPP_CLOSE_BRACE)
2089 /* If this is a non-nested `}', stop before consuming it.
2090 That way, when confronted with something like:
2092 { 3 + }
2094 we stop before consuming the closing `}', even though we
2095 have not yet reached a `;'. */
2096 if (nesting_depth == 0)
2097 break;
2098 /* If it is the closing `}' for a block that we have
2099 scanned, stop -- but only after consuming the token.
2100 That way given:
2102 void f g () { ... }
2103 typedef int I;
2105 we will stop after the body of the erroneously declared
2106 function, but before consuming the following `typedef'
2107 declaration. */
2108 if (--nesting_depth == 0)
2110 cp_lexer_consume_token (parser->lexer);
2111 break;
2114 /* If it the next token is a `{', then we are entering a new
2115 block. Consume the entire block. */
2116 else if (token->type == CPP_OPEN_BRACE)
2117 ++nesting_depth;
2118 /* Consume the token. */
2119 cp_lexer_consume_token (parser->lexer);
2123 /* This function is called at the end of a statement or declaration.
2124 If the next token is a semicolon, it is consumed; otherwise, error
2125 recovery is attempted. */
2127 static void
2128 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2130 /* Look for the trailing `;'. */
2131 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2133 /* If there is additional (erroneous) input, skip to the end of
2134 the statement. */
2135 cp_parser_skip_to_end_of_statement (parser);
2136 /* If the next token is now a `;', consume it. */
2137 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2138 cp_lexer_consume_token (parser->lexer);
2142 /* Skip tokens until we have consumed an entire block, or until we
2143 have consumed a non-nested `;'. */
2145 static void
2146 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2148 unsigned nesting_depth = 0;
2150 while (true)
2152 cp_token *token;
2154 /* Peek at the next token. */
2155 token = cp_lexer_peek_token (parser->lexer);
2156 /* If we've run out of tokens, stop. */
2157 if (token->type == CPP_EOF)
2158 break;
2159 /* If the next token is a `;', we have reached the end of the
2160 statement. */
2161 if (token->type == CPP_SEMICOLON && !nesting_depth)
2163 /* Consume the `;'. */
2164 cp_lexer_consume_token (parser->lexer);
2165 break;
2167 /* Consume the token. */
2168 token = cp_lexer_consume_token (parser->lexer);
2169 /* If the next token is a non-nested `}', then we have reached
2170 the end of the current block. */
2171 if (token->type == CPP_CLOSE_BRACE
2172 && (nesting_depth == 0 || --nesting_depth == 0))
2173 break;
2174 /* If it the next token is a `{', then we are entering a new
2175 block. Consume the entire block. */
2176 if (token->type == CPP_OPEN_BRACE)
2177 ++nesting_depth;
2181 /* Skip tokens until a non-nested closing curly brace is the next
2182 token. */
2184 static void
2185 cp_parser_skip_to_closing_brace (cp_parser *parser)
2187 unsigned nesting_depth = 0;
2189 while (true)
2191 cp_token *token;
2193 /* Peek at the next token. */
2194 token = cp_lexer_peek_token (parser->lexer);
2195 /* If we've run out of tokens, stop. */
2196 if (token->type == CPP_EOF)
2197 break;
2198 /* If the next token is a non-nested `}', then we have reached
2199 the end of the current block. */
2200 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2201 break;
2202 /* If it the next token is a `{', then we are entering a new
2203 block. Consume the entire block. */
2204 else if (token->type == CPP_OPEN_BRACE)
2205 ++nesting_depth;
2206 /* Consume the token. */
2207 cp_lexer_consume_token (parser->lexer);
2211 /* Create a new C++ parser. */
2213 static cp_parser *
2214 cp_parser_new (void)
2216 cp_parser *parser;
2217 cp_lexer *lexer;
2219 /* cp_lexer_new_main is called before calling ggc_alloc because
2220 cp_lexer_new_main might load a PCH file. */
2221 lexer = cp_lexer_new_main ();
2223 parser = ggc_alloc_cleared (sizeof (cp_parser));
2224 parser->lexer = lexer;
2225 parser->context = cp_parser_context_new (NULL);
2227 /* For now, we always accept GNU extensions. */
2228 parser->allow_gnu_extensions_p = 1;
2230 /* The `>' token is a greater-than operator, not the end of a
2231 template-id. */
2232 parser->greater_than_is_operator_p = true;
2234 parser->default_arg_ok_p = true;
2236 /* We are not parsing a constant-expression. */
2237 parser->integral_constant_expression_p = false;
2238 parser->allow_non_integral_constant_expression_p = false;
2239 parser->non_integral_constant_expression_p = false;
2241 /* We are not parsing offsetof. */
2242 parser->in_offsetof_p = false;
2244 /* Local variable names are not forbidden. */
2245 parser->local_variables_forbidden_p = false;
2247 /* We are not processing an `extern "C"' declaration. */
2248 parser->in_unbraced_linkage_specification_p = false;
2250 /* We are not processing a declarator. */
2251 parser->in_declarator_p = false;
2253 /* We are not processing a template-argument-list. */
2254 parser->in_template_argument_list_p = false;
2256 /* We are not in an iteration statement. */
2257 parser->in_iteration_statement_p = false;
2259 /* We are not in a switch statement. */
2260 parser->in_switch_statement_p = false;
2262 /* We are not parsing a type-id inside an expression. */
2263 parser->in_type_id_in_expr_p = false;
2265 /* The unparsed function queue is empty. */
2266 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2268 /* There are no classes being defined. */
2269 parser->num_classes_being_defined = 0;
2271 /* No template parameters apply. */
2272 parser->num_template_parameter_lists = 0;
2274 return parser;
2277 /* Lexical conventions [gram.lex] */
2279 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2280 identifier. */
2282 static tree
2283 cp_parser_identifier (cp_parser* parser)
2285 cp_token *token;
2287 /* Look for the identifier. */
2288 token = cp_parser_require (parser, CPP_NAME, "identifier");
2289 /* Return the value. */
2290 return token ? token->value : error_mark_node;
2293 /* Basic concepts [gram.basic] */
2295 /* Parse a translation-unit.
2297 translation-unit:
2298 declaration-seq [opt]
2300 Returns TRUE if all went well. */
2302 static bool
2303 cp_parser_translation_unit (cp_parser* parser)
2305 while (true)
2307 cp_parser_declaration_seq_opt (parser);
2309 /* If there are no tokens left then all went well. */
2310 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2311 break;
2313 /* Otherwise, issue an error message. */
2314 cp_parser_error (parser, "expected declaration");
2315 return false;
2318 /* Consume the EOF token. */
2319 cp_parser_require (parser, CPP_EOF, "end-of-file");
2321 /* Finish up. */
2322 finish_translation_unit ();
2324 /* All went well. */
2325 return true;
2328 /* Expressions [gram.expr] */
2330 /* Parse a primary-expression.
2332 primary-expression:
2333 literal
2334 this
2335 ( expression )
2336 id-expression
2338 GNU Extensions:
2340 primary-expression:
2341 ( compound-statement )
2342 __builtin_va_arg ( assignment-expression , type-id )
2344 literal:
2345 __null
2347 Returns a representation of the expression.
2349 *IDK indicates what kind of id-expression (if any) was present.
2351 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2352 used as the operand of a pointer-to-member. In that case,
2353 *QUALIFYING_CLASS gives the class that is used as the qualifying
2354 class in the pointer-to-member. */
2356 static tree
2357 cp_parser_primary_expression (cp_parser *parser,
2358 cp_id_kind *idk,
2359 tree *qualifying_class)
2361 cp_token *token;
2363 /* Assume the primary expression is not an id-expression. */
2364 *idk = CP_ID_KIND_NONE;
2365 /* And that it cannot be used as pointer-to-member. */
2366 *qualifying_class = NULL_TREE;
2368 /* Peek at the next token. */
2369 token = cp_lexer_peek_token (parser->lexer);
2370 switch (token->type)
2372 /* literal:
2373 integer-literal
2374 character-literal
2375 floating-literal
2376 string-literal
2377 boolean-literal */
2378 case CPP_CHAR:
2379 case CPP_WCHAR:
2380 case CPP_STRING:
2381 case CPP_WSTRING:
2382 case CPP_NUMBER:
2383 token = cp_lexer_consume_token (parser->lexer);
2384 return token->value;
2386 case CPP_OPEN_PAREN:
2388 tree expr;
2389 bool saved_greater_than_is_operator_p;
2391 /* Consume the `('. */
2392 cp_lexer_consume_token (parser->lexer);
2393 /* Within a parenthesized expression, a `>' token is always
2394 the greater-than operator. */
2395 saved_greater_than_is_operator_p
2396 = parser->greater_than_is_operator_p;
2397 parser->greater_than_is_operator_p = true;
2398 /* If we see `( { ' then we are looking at the beginning of
2399 a GNU statement-expression. */
2400 if (cp_parser_allow_gnu_extensions_p (parser)
2401 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2403 /* Statement-expressions are not allowed by the standard. */
2404 if (pedantic)
2405 pedwarn ("ISO C++ forbids braced-groups within expressions");
2407 /* And they're not allowed outside of a function-body; you
2408 cannot, for example, write:
2410 int i = ({ int j = 3; j + 1; });
2412 at class or namespace scope. */
2413 if (!at_function_scope_p ())
2414 error ("statement-expressions are allowed only inside functions");
2415 /* Start the statement-expression. */
2416 expr = begin_stmt_expr ();
2417 /* Parse the compound-statement. */
2418 cp_parser_compound_statement (parser, true);
2419 /* Finish up. */
2420 expr = finish_stmt_expr (expr, false);
2422 else
2424 /* Parse the parenthesized expression. */
2425 expr = cp_parser_expression (parser);
2426 /* Let the front end know that this expression was
2427 enclosed in parentheses. This matters in case, for
2428 example, the expression is of the form `A::B', since
2429 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2430 not. */
2431 finish_parenthesized_expr (expr);
2433 /* The `>' token might be the end of a template-id or
2434 template-parameter-list now. */
2435 parser->greater_than_is_operator_p
2436 = saved_greater_than_is_operator_p;
2437 /* Consume the `)'. */
2438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2439 cp_parser_skip_to_end_of_statement (parser);
2441 return expr;
2444 case CPP_KEYWORD:
2445 switch (token->keyword)
2447 /* These two are the boolean literals. */
2448 case RID_TRUE:
2449 cp_lexer_consume_token (parser->lexer);
2450 return boolean_true_node;
2451 case RID_FALSE:
2452 cp_lexer_consume_token (parser->lexer);
2453 return boolean_false_node;
2455 /* The `__null' literal. */
2456 case RID_NULL:
2457 cp_lexer_consume_token (parser->lexer);
2458 return null_node;
2460 /* Recognize the `this' keyword. */
2461 case RID_THIS:
2462 cp_lexer_consume_token (parser->lexer);
2463 if (parser->local_variables_forbidden_p)
2465 error ("`this' may not be used in this context");
2466 return error_mark_node;
2468 /* Pointers cannot appear in constant-expressions. */
2469 if (parser->integral_constant_expression_p)
2471 if (!parser->allow_non_integral_constant_expression_p)
2472 return cp_parser_non_integral_constant_expression ("`this'");
2473 parser->non_integral_constant_expression_p = true;
2475 return finish_this_expr ();
2477 /* The `operator' keyword can be the beginning of an
2478 id-expression. */
2479 case RID_OPERATOR:
2480 goto id_expression;
2482 case RID_FUNCTION_NAME:
2483 case RID_PRETTY_FUNCTION_NAME:
2484 case RID_C99_FUNCTION_NAME:
2485 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2486 __func__ are the names of variables -- but they are
2487 treated specially. Therefore, they are handled here,
2488 rather than relying on the generic id-expression logic
2489 below. Grammatically, these names are id-expressions.
2491 Consume the token. */
2492 token = cp_lexer_consume_token (parser->lexer);
2493 /* Look up the name. */
2494 return finish_fname (token->value);
2496 case RID_VA_ARG:
2498 tree expression;
2499 tree type;
2501 /* The `__builtin_va_arg' construct is used to handle
2502 `va_arg'. Consume the `__builtin_va_arg' token. */
2503 cp_lexer_consume_token (parser->lexer);
2504 /* Look for the opening `('. */
2505 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2506 /* Now, parse the assignment-expression. */
2507 expression = cp_parser_assignment_expression (parser);
2508 /* Look for the `,'. */
2509 cp_parser_require (parser, CPP_COMMA, "`,'");
2510 /* Parse the type-id. */
2511 type = cp_parser_type_id (parser);
2512 /* Look for the closing `)'. */
2513 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2514 /* Using `va_arg' in a constant-expression is not
2515 allowed. */
2516 if (parser->integral_constant_expression_p)
2518 if (!parser->allow_non_integral_constant_expression_p)
2519 return cp_parser_non_integral_constant_expression ("`va_arg'");
2520 parser->non_integral_constant_expression_p = true;
2522 return build_x_va_arg (expression, type);
2525 case RID_OFFSETOF:
2527 tree expression;
2528 bool saved_in_offsetof_p;
2530 /* Consume the "__offsetof__" token. */
2531 cp_lexer_consume_token (parser->lexer);
2532 /* Consume the opening `('. */
2533 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2534 /* Parse the parenthesized (almost) constant-expression. */
2535 saved_in_offsetof_p = parser->in_offsetof_p;
2536 parser->in_offsetof_p = true;
2537 expression
2538 = cp_parser_constant_expression (parser,
2539 /*allow_non_constant_p=*/false,
2540 /*non_constant_p=*/NULL);
2541 parser->in_offsetof_p = saved_in_offsetof_p;
2542 /* Consume the closing ')'. */
2543 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2545 return expression;
2548 default:
2549 cp_parser_error (parser, "expected primary-expression");
2550 return error_mark_node;
2553 /* An id-expression can start with either an identifier, a
2554 `::' as the beginning of a qualified-id, or the "operator"
2555 keyword. */
2556 case CPP_NAME:
2557 case CPP_SCOPE:
2558 case CPP_TEMPLATE_ID:
2559 case CPP_NESTED_NAME_SPECIFIER:
2561 tree id_expression;
2562 tree decl;
2563 const char *error_msg;
2565 id_expression:
2566 /* Parse the id-expression. */
2567 id_expression
2568 = cp_parser_id_expression (parser,
2569 /*template_keyword_p=*/false,
2570 /*check_dependency_p=*/true,
2571 /*template_p=*/NULL,
2572 /*declarator_p=*/false);
2573 if (id_expression == error_mark_node)
2574 return error_mark_node;
2575 /* If we have a template-id, then no further lookup is
2576 required. If the template-id was for a template-class, we
2577 will sometimes have a TYPE_DECL at this point. */
2578 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2579 || TREE_CODE (id_expression) == TYPE_DECL)
2580 decl = id_expression;
2581 /* Look up the name. */
2582 else
2584 decl = cp_parser_lookup_name_simple (parser, id_expression);
2585 /* If name lookup gives us a SCOPE_REF, then the
2586 qualifying scope was dependent. Just propagate the
2587 name. */
2588 if (TREE_CODE (decl) == SCOPE_REF)
2590 if (TYPE_P (TREE_OPERAND (decl, 0)))
2591 *qualifying_class = TREE_OPERAND (decl, 0);
2592 return decl;
2594 /* Check to see if DECL is a local variable in a context
2595 where that is forbidden. */
2596 if (parser->local_variables_forbidden_p
2597 && local_variable_p (decl))
2599 /* It might be that we only found DECL because we are
2600 trying to be generous with pre-ISO scoping rules.
2601 For example, consider:
2603 int i;
2604 void g() {
2605 for (int i = 0; i < 10; ++i) {}
2606 extern void f(int j = i);
2609 Here, name look up will originally find the out
2610 of scope `i'. We need to issue a warning message,
2611 but then use the global `i'. */
2612 decl = check_for_out_of_scope_variable (decl);
2613 if (local_variable_p (decl))
2615 error ("local variable `%D' may not appear in this context",
2616 decl);
2617 return error_mark_node;
2622 decl = finish_id_expression (id_expression, decl, parser->scope,
2623 idk, qualifying_class,
2624 parser->integral_constant_expression_p,
2625 parser->allow_non_integral_constant_expression_p,
2626 &parser->non_integral_constant_expression_p,
2627 &error_msg);
2628 if (error_msg)
2629 cp_parser_error (parser, error_msg);
2630 return decl;
2633 /* Anything else is an error. */
2634 default:
2635 cp_parser_error (parser, "expected primary-expression");
2636 return error_mark_node;
2640 /* Parse an id-expression.
2642 id-expression:
2643 unqualified-id
2644 qualified-id
2646 qualified-id:
2647 :: [opt] nested-name-specifier template [opt] unqualified-id
2648 :: identifier
2649 :: operator-function-id
2650 :: template-id
2652 Return a representation of the unqualified portion of the
2653 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2654 a `::' or nested-name-specifier.
2656 Often, if the id-expression was a qualified-id, the caller will
2657 want to make a SCOPE_REF to represent the qualified-id. This
2658 function does not do this in order to avoid wastefully creating
2659 SCOPE_REFs when they are not required.
2661 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2662 `template' keyword.
2664 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2665 uninstantiated templates.
2667 If *TEMPLATE_P is non-NULL, it is set to true iff the
2668 `template' keyword is used to explicitly indicate that the entity
2669 named is a template.
2671 If DECLARATOR_P is true, the id-expression is appearing as part of
2672 a declarator, rather than as part of an expression. */
2674 static tree
2675 cp_parser_id_expression (cp_parser *parser,
2676 bool template_keyword_p,
2677 bool check_dependency_p,
2678 bool *template_p,
2679 bool declarator_p)
2681 bool global_scope_p;
2682 bool nested_name_specifier_p;
2684 /* Assume the `template' keyword was not used. */
2685 if (template_p)
2686 *template_p = false;
2688 /* Look for the optional `::' operator. */
2689 global_scope_p
2690 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2691 != NULL_TREE);
2692 /* Look for the optional nested-name-specifier. */
2693 nested_name_specifier_p
2694 = (cp_parser_nested_name_specifier_opt (parser,
2695 /*typename_keyword_p=*/false,
2696 check_dependency_p,
2697 /*type_p=*/false,
2698 /*is_declarator=*/false)
2699 != NULL_TREE);
2700 /* If there is a nested-name-specifier, then we are looking at
2701 the first qualified-id production. */
2702 if (nested_name_specifier_p)
2704 tree saved_scope;
2705 tree saved_object_scope;
2706 tree saved_qualifying_scope;
2707 tree unqualified_id;
2708 bool is_template;
2710 /* See if the next token is the `template' keyword. */
2711 if (!template_p)
2712 template_p = &is_template;
2713 *template_p = cp_parser_optional_template_keyword (parser);
2714 /* Name lookup we do during the processing of the
2715 unqualified-id might obliterate SCOPE. */
2716 saved_scope = parser->scope;
2717 saved_object_scope = parser->object_scope;
2718 saved_qualifying_scope = parser->qualifying_scope;
2719 /* Process the final unqualified-id. */
2720 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2721 check_dependency_p,
2722 declarator_p);
2723 /* Restore the SAVED_SCOPE for our caller. */
2724 parser->scope = saved_scope;
2725 parser->object_scope = saved_object_scope;
2726 parser->qualifying_scope = saved_qualifying_scope;
2728 return unqualified_id;
2730 /* Otherwise, if we are in global scope, then we are looking at one
2731 of the other qualified-id productions. */
2732 else if (global_scope_p)
2734 cp_token *token;
2735 tree id;
2737 /* Peek at the next token. */
2738 token = cp_lexer_peek_token (parser->lexer);
2740 /* If it's an identifier, and the next token is not a "<", then
2741 we can avoid the template-id case. This is an optimization
2742 for this common case. */
2743 if (token->type == CPP_NAME
2744 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2745 return cp_parser_identifier (parser);
2747 cp_parser_parse_tentatively (parser);
2748 /* Try a template-id. */
2749 id = cp_parser_template_id (parser,
2750 /*template_keyword_p=*/false,
2751 /*check_dependency_p=*/true,
2752 declarator_p);
2753 /* If that worked, we're done. */
2754 if (cp_parser_parse_definitely (parser))
2755 return id;
2757 /* Peek at the next token. (Changes in the token buffer may
2758 have invalidated the pointer obtained above.) */
2759 token = cp_lexer_peek_token (parser->lexer);
2761 switch (token->type)
2763 case CPP_NAME:
2764 return cp_parser_identifier (parser);
2766 case CPP_KEYWORD:
2767 if (token->keyword == RID_OPERATOR)
2768 return cp_parser_operator_function_id (parser);
2769 /* Fall through. */
2771 default:
2772 cp_parser_error (parser, "expected id-expression");
2773 return error_mark_node;
2776 else
2777 return cp_parser_unqualified_id (parser, template_keyword_p,
2778 /*check_dependency_p=*/true,
2779 declarator_p);
2782 /* Parse an unqualified-id.
2784 unqualified-id:
2785 identifier
2786 operator-function-id
2787 conversion-function-id
2788 ~ class-name
2789 template-id
2791 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2792 keyword, in a construct like `A::template ...'.
2794 Returns a representation of unqualified-id. For the `identifier'
2795 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2796 production a BIT_NOT_EXPR is returned; the operand of the
2797 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2798 other productions, see the documentation accompanying the
2799 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2800 names are looked up in uninstantiated templates. If DECLARATOR_P
2801 is true, the unqualified-id is appearing as part of a declarator,
2802 rather than as part of an expression. */
2804 static tree
2805 cp_parser_unqualified_id (cp_parser* parser,
2806 bool template_keyword_p,
2807 bool check_dependency_p,
2808 bool declarator_p)
2810 cp_token *token;
2812 /* Peek at the next token. */
2813 token = cp_lexer_peek_token (parser->lexer);
2815 switch (token->type)
2817 case CPP_NAME:
2819 tree id;
2821 /* We don't know yet whether or not this will be a
2822 template-id. */
2823 cp_parser_parse_tentatively (parser);
2824 /* Try a template-id. */
2825 id = cp_parser_template_id (parser, template_keyword_p,
2826 check_dependency_p,
2827 declarator_p);
2828 /* If it worked, we're done. */
2829 if (cp_parser_parse_definitely (parser))
2830 return id;
2831 /* Otherwise, it's an ordinary identifier. */
2832 return cp_parser_identifier (parser);
2835 case CPP_TEMPLATE_ID:
2836 return cp_parser_template_id (parser, template_keyword_p,
2837 check_dependency_p,
2838 declarator_p);
2840 case CPP_COMPL:
2842 tree type_decl;
2843 tree qualifying_scope;
2844 tree object_scope;
2845 tree scope;
2847 /* Consume the `~' token. */
2848 cp_lexer_consume_token (parser->lexer);
2849 /* Parse the class-name. The standard, as written, seems to
2850 say that:
2852 template <typename T> struct S { ~S (); };
2853 template <typename T> S<T>::~S() {}
2855 is invalid, since `~' must be followed by a class-name, but
2856 `S<T>' is dependent, and so not known to be a class.
2857 That's not right; we need to look in uninstantiated
2858 templates. A further complication arises from:
2860 template <typename T> void f(T t) {
2861 t.T::~T();
2864 Here, it is not possible to look up `T' in the scope of `T'
2865 itself. We must look in both the current scope, and the
2866 scope of the containing complete expression.
2868 Yet another issue is:
2870 struct S {
2871 int S;
2872 ~S();
2875 S::~S() {}
2877 The standard does not seem to say that the `S' in `~S'
2878 should refer to the type `S' and not the data member
2879 `S::S'. */
2881 /* DR 244 says that we look up the name after the "~" in the
2882 same scope as we looked up the qualifying name. That idea
2883 isn't fully worked out; it's more complicated than that. */
2884 scope = parser->scope;
2885 object_scope = parser->object_scope;
2886 qualifying_scope = parser->qualifying_scope;
2888 /* If the name is of the form "X::~X" it's OK. */
2889 if (scope && TYPE_P (scope)
2890 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2891 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2892 == CPP_OPEN_PAREN)
2893 && (cp_lexer_peek_token (parser->lexer)->value
2894 == TYPE_IDENTIFIER (scope)))
2896 cp_lexer_consume_token (parser->lexer);
2897 return build_nt (BIT_NOT_EXPR, scope);
2900 /* If there was an explicit qualification (S::~T), first look
2901 in the scope given by the qualification (i.e., S). */
2902 if (scope)
2904 cp_parser_parse_tentatively (parser);
2905 type_decl = cp_parser_class_name (parser,
2906 /*typename_keyword_p=*/false,
2907 /*template_keyword_p=*/false,
2908 /*type_p=*/false,
2909 /*check_dependency=*/false,
2910 /*class_head_p=*/false,
2911 declarator_p);
2912 if (cp_parser_parse_definitely (parser))
2913 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2915 /* In "N::S::~S", look in "N" as well. */
2916 if (scope && qualifying_scope)
2918 cp_parser_parse_tentatively (parser);
2919 parser->scope = qualifying_scope;
2920 parser->object_scope = NULL_TREE;
2921 parser->qualifying_scope = NULL_TREE;
2922 type_decl
2923 = cp_parser_class_name (parser,
2924 /*typename_keyword_p=*/false,
2925 /*template_keyword_p=*/false,
2926 /*type_p=*/false,
2927 /*check_dependency=*/false,
2928 /*class_head_p=*/false,
2929 declarator_p);
2930 if (cp_parser_parse_definitely (parser))
2931 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2933 /* In "p->S::~T", look in the scope given by "*p" as well. */
2934 else if (object_scope)
2936 cp_parser_parse_tentatively (parser);
2937 parser->scope = object_scope;
2938 parser->object_scope = NULL_TREE;
2939 parser->qualifying_scope = NULL_TREE;
2940 type_decl
2941 = cp_parser_class_name (parser,
2942 /*typename_keyword_p=*/false,
2943 /*template_keyword_p=*/false,
2944 /*type_p=*/false,
2945 /*check_dependency=*/false,
2946 /*class_head_p=*/false,
2947 declarator_p);
2948 if (cp_parser_parse_definitely (parser))
2949 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2951 /* Look in the surrounding context. */
2952 parser->scope = NULL_TREE;
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 an error occurred, assume that the name of the
2964 destructor is the same as the name of the qualifying
2965 class. That allows us to keep parsing after running
2966 into ill-formed destructor names. */
2967 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2968 return build_nt (BIT_NOT_EXPR, scope);
2969 else if (type_decl == error_mark_node)
2970 return error_mark_node;
2972 /* [class.dtor]
2974 A typedef-name that names a class shall not be used as the
2975 identifier in the declarator for a destructor declaration. */
2976 if (declarator_p
2977 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2978 && !DECL_SELF_REFERENCE_P (type_decl))
2979 error ("typedef-name `%D' used as destructor declarator",
2980 type_decl);
2982 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2985 case CPP_KEYWORD:
2986 if (token->keyword == RID_OPERATOR)
2988 tree id;
2990 /* This could be a template-id, so we try that first. */
2991 cp_parser_parse_tentatively (parser);
2992 /* Try a template-id. */
2993 id = cp_parser_template_id (parser, template_keyword_p,
2994 /*check_dependency_p=*/true,
2995 declarator_p);
2996 /* If that worked, we're done. */
2997 if (cp_parser_parse_definitely (parser))
2998 return id;
2999 /* We still don't know whether we're looking at an
3000 operator-function-id or a conversion-function-id. */
3001 cp_parser_parse_tentatively (parser);
3002 /* Try an operator-function-id. */
3003 id = cp_parser_operator_function_id (parser);
3004 /* If that didn't work, try a conversion-function-id. */
3005 if (!cp_parser_parse_definitely (parser))
3006 id = cp_parser_conversion_function_id (parser);
3008 return id;
3010 /* Fall through. */
3012 default:
3013 cp_parser_error (parser, "expected unqualified-id");
3014 return error_mark_node;
3018 /* Parse an (optional) nested-name-specifier.
3020 nested-name-specifier:
3021 class-or-namespace-name :: nested-name-specifier [opt]
3022 class-or-namespace-name :: template nested-name-specifier [opt]
3024 PARSER->SCOPE should be set appropriately before this function is
3025 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3026 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3027 in name lookups.
3029 Sets PARSER->SCOPE to the class (TYPE) or namespace
3030 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3031 it unchanged if there is no nested-name-specifier. Returns the new
3032 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3034 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3035 part of a declaration and/or decl-specifier. */
3037 static tree
3038 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3039 bool typename_keyword_p,
3040 bool check_dependency_p,
3041 bool type_p,
3042 bool is_declaration)
3044 bool success = false;
3045 tree access_check = NULL_TREE;
3046 ptrdiff_t start;
3047 cp_token* token;
3049 /* If the next token corresponds to a nested name specifier, there
3050 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3051 false, it may have been true before, in which case something
3052 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3053 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3054 CHECK_DEPENDENCY_P is false, we have to fall through into the
3055 main loop. */
3056 if (check_dependency_p
3057 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3059 cp_parser_pre_parsed_nested_name_specifier (parser);
3060 return parser->scope;
3063 /* Remember where the nested-name-specifier starts. */
3064 if (cp_parser_parsing_tentatively (parser)
3065 && !cp_parser_committed_to_tentative_parse (parser))
3067 token = cp_lexer_peek_token (parser->lexer);
3068 start = cp_lexer_token_difference (parser->lexer,
3069 parser->lexer->first_token,
3070 token);
3072 else
3073 start = -1;
3075 push_deferring_access_checks (dk_deferred);
3077 while (true)
3079 tree new_scope;
3080 tree old_scope;
3081 tree saved_qualifying_scope;
3082 bool template_keyword_p;
3084 /* Spot cases that cannot be the beginning of a
3085 nested-name-specifier. */
3086 token = cp_lexer_peek_token (parser->lexer);
3088 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3089 the already parsed nested-name-specifier. */
3090 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3092 /* Grab the nested-name-specifier and continue the loop. */
3093 cp_parser_pre_parsed_nested_name_specifier (parser);
3094 success = true;
3095 continue;
3098 /* Spot cases that cannot be the beginning of a
3099 nested-name-specifier. On the second and subsequent times
3100 through the loop, we look for the `template' keyword. */
3101 if (success && token->keyword == RID_TEMPLATE)
3103 /* A template-id can start a nested-name-specifier. */
3104 else if (token->type == CPP_TEMPLATE_ID)
3106 else
3108 /* If the next token is not an identifier, then it is
3109 definitely not a class-or-namespace-name. */
3110 if (token->type != CPP_NAME)
3111 break;
3112 /* If the following token is neither a `<' (to begin a
3113 template-id), nor a `::', then we are not looking at a
3114 nested-name-specifier. */
3115 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3116 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3117 break;
3120 /* The nested-name-specifier is optional, so we parse
3121 tentatively. */
3122 cp_parser_parse_tentatively (parser);
3124 /* Look for the optional `template' keyword, if this isn't the
3125 first time through the loop. */
3126 if (success)
3127 template_keyword_p = cp_parser_optional_template_keyword (parser);
3128 else
3129 template_keyword_p = false;
3131 /* Save the old scope since the name lookup we are about to do
3132 might destroy it. */
3133 old_scope = parser->scope;
3134 saved_qualifying_scope = parser->qualifying_scope;
3135 /* Parse the qualifying entity. */
3136 new_scope
3137 = cp_parser_class_or_namespace_name (parser,
3138 typename_keyword_p,
3139 template_keyword_p,
3140 check_dependency_p,
3141 type_p,
3142 is_declaration);
3143 /* Look for the `::' token. */
3144 cp_parser_require (parser, CPP_SCOPE, "`::'");
3146 /* If we found what we wanted, we keep going; otherwise, we're
3147 done. */
3148 if (!cp_parser_parse_definitely (parser))
3150 bool error_p = false;
3152 /* Restore the OLD_SCOPE since it was valid before the
3153 failed attempt at finding the last
3154 class-or-namespace-name. */
3155 parser->scope = old_scope;
3156 parser->qualifying_scope = saved_qualifying_scope;
3157 /* If the next token is an identifier, and the one after
3158 that is a `::', then any valid interpretation would have
3159 found a class-or-namespace-name. */
3160 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3161 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3162 == CPP_SCOPE)
3163 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3164 != CPP_COMPL))
3166 token = cp_lexer_consume_token (parser->lexer);
3167 if (!error_p)
3169 tree decl;
3171 decl = cp_parser_lookup_name_simple (parser, token->value);
3172 if (TREE_CODE (decl) == TEMPLATE_DECL)
3173 error ("`%D' used without template parameters",
3174 decl);
3175 else
3176 cp_parser_name_lookup_error
3177 (parser, token->value, decl,
3178 "is not a class or namespace");
3179 parser->scope = NULL_TREE;
3180 error_p = true;
3181 /* Treat this as a successful nested-name-specifier
3182 due to:
3184 [basic.lookup.qual]
3186 If the name found is not a class-name (clause
3187 _class_) or namespace-name (_namespace.def_), the
3188 program is ill-formed. */
3189 success = true;
3191 cp_lexer_consume_token (parser->lexer);
3193 break;
3196 /* We've found one valid nested-name-specifier. */
3197 success = true;
3198 /* Make sure we look in the right scope the next time through
3199 the loop. */
3200 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3201 ? TREE_TYPE (new_scope)
3202 : new_scope);
3203 /* If it is a class scope, try to complete it; we are about to
3204 be looking up names inside the class. */
3205 if (TYPE_P (parser->scope)
3206 /* Since checking types for dependency can be expensive,
3207 avoid doing it if the type is already complete. */
3208 && !COMPLETE_TYPE_P (parser->scope)
3209 /* Do not try to complete dependent types. */
3210 && !dependent_type_p (parser->scope))
3211 complete_type (parser->scope);
3214 /* Retrieve any deferred checks. Do not pop this access checks yet
3215 so the memory will not be reclaimed during token replacing below. */
3216 access_check = get_deferred_access_checks ();
3218 /* If parsing tentatively, replace the sequence of tokens that makes
3219 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3220 token. That way, should we re-parse the token stream, we will
3221 not have to repeat the effort required to do the parse, nor will
3222 we issue duplicate error messages. */
3223 if (success && start >= 0)
3225 /* Find the token that corresponds to the start of the
3226 template-id. */
3227 token = cp_lexer_advance_token (parser->lexer,
3228 parser->lexer->first_token,
3229 start);
3231 /* Reset the contents of the START token. */
3232 token->type = CPP_NESTED_NAME_SPECIFIER;
3233 token->value = build_tree_list (access_check, parser->scope);
3234 TREE_TYPE (token->value) = parser->qualifying_scope;
3235 token->keyword = RID_MAX;
3236 /* Purge all subsequent tokens. */
3237 cp_lexer_purge_tokens_after (parser->lexer, token);
3240 pop_deferring_access_checks ();
3241 return success ? parser->scope : NULL_TREE;
3244 /* Parse a nested-name-specifier. See
3245 cp_parser_nested_name_specifier_opt for details. This function
3246 behaves identically, except that it will an issue an error if no
3247 nested-name-specifier is present, and it will return
3248 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3249 is present. */
3251 static tree
3252 cp_parser_nested_name_specifier (cp_parser *parser,
3253 bool typename_keyword_p,
3254 bool check_dependency_p,
3255 bool type_p,
3256 bool is_declaration)
3258 tree scope;
3260 /* Look for the nested-name-specifier. */
3261 scope = cp_parser_nested_name_specifier_opt (parser,
3262 typename_keyword_p,
3263 check_dependency_p,
3264 type_p,
3265 is_declaration);
3266 /* If it was not present, issue an error message. */
3267 if (!scope)
3269 cp_parser_error (parser, "expected nested-name-specifier");
3270 parser->scope = NULL_TREE;
3271 return error_mark_node;
3274 return scope;
3277 /* Parse a class-or-namespace-name.
3279 class-or-namespace-name:
3280 class-name
3281 namespace-name
3283 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3284 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3285 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3286 TYPE_P is TRUE iff the next name should be taken as a class-name,
3287 even the same name is declared to be another entity in the same
3288 scope.
3290 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3291 specified by the class-or-namespace-name. If neither is found the
3292 ERROR_MARK_NODE is returned. */
3294 static tree
3295 cp_parser_class_or_namespace_name (cp_parser *parser,
3296 bool typename_keyword_p,
3297 bool template_keyword_p,
3298 bool check_dependency_p,
3299 bool type_p,
3300 bool is_declaration)
3302 tree saved_scope;
3303 tree saved_qualifying_scope;
3304 tree saved_object_scope;
3305 tree scope;
3306 bool only_class_p;
3308 /* Before we try to parse the class-name, we must save away the
3309 current PARSER->SCOPE since cp_parser_class_name will destroy
3310 it. */
3311 saved_scope = parser->scope;
3312 saved_qualifying_scope = parser->qualifying_scope;
3313 saved_object_scope = parser->object_scope;
3314 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3315 there is no need to look for a namespace-name. */
3316 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3317 if (!only_class_p)
3318 cp_parser_parse_tentatively (parser);
3319 scope = cp_parser_class_name (parser,
3320 typename_keyword_p,
3321 template_keyword_p,
3322 type_p,
3323 check_dependency_p,
3324 /*class_head_p=*/false,
3325 is_declaration);
3326 /* If that didn't work, try for a namespace-name. */
3327 if (!only_class_p && !cp_parser_parse_definitely (parser))
3329 /* Restore the saved scope. */
3330 parser->scope = saved_scope;
3331 parser->qualifying_scope = saved_qualifying_scope;
3332 parser->object_scope = saved_object_scope;
3333 /* If we are not looking at an identifier followed by the scope
3334 resolution operator, then this is not part of a
3335 nested-name-specifier. (Note that this function is only used
3336 to parse the components of a nested-name-specifier.) */
3337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3338 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3339 return error_mark_node;
3340 scope = cp_parser_namespace_name (parser);
3343 return scope;
3346 /* Parse a postfix-expression.
3348 postfix-expression:
3349 primary-expression
3350 postfix-expression [ expression ]
3351 postfix-expression ( expression-list [opt] )
3352 simple-type-specifier ( expression-list [opt] )
3353 typename :: [opt] nested-name-specifier identifier
3354 ( expression-list [opt] )
3355 typename :: [opt] nested-name-specifier template [opt] template-id
3356 ( expression-list [opt] )
3357 postfix-expression . template [opt] id-expression
3358 postfix-expression -> template [opt] id-expression
3359 postfix-expression . pseudo-destructor-name
3360 postfix-expression -> pseudo-destructor-name
3361 postfix-expression ++
3362 postfix-expression --
3363 dynamic_cast < type-id > ( expression )
3364 static_cast < type-id > ( expression )
3365 reinterpret_cast < type-id > ( expression )
3366 const_cast < type-id > ( expression )
3367 typeid ( expression )
3368 typeid ( type-id )
3370 GNU Extension:
3372 postfix-expression:
3373 ( type-id ) { initializer-list , [opt] }
3375 This extension is a GNU version of the C99 compound-literal
3376 construct. (The C99 grammar uses `type-name' instead of `type-id',
3377 but they are essentially the same concept.)
3379 If ADDRESS_P is true, the postfix expression is the operand of the
3380 `&' operator.
3382 Returns a representation of the expression. */
3384 static tree
3385 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3387 cp_token *token;
3388 enum rid keyword;
3389 cp_id_kind idk = CP_ID_KIND_NONE;
3390 tree postfix_expression = NULL_TREE;
3391 /* Non-NULL only if the current postfix-expression can be used to
3392 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3393 class used to qualify the member. */
3394 tree qualifying_class = NULL_TREE;
3396 /* Peek at the next token. */
3397 token = cp_lexer_peek_token (parser->lexer);
3398 /* Some of the productions are determined by keywords. */
3399 keyword = token->keyword;
3400 switch (keyword)
3402 case RID_DYNCAST:
3403 case RID_STATCAST:
3404 case RID_REINTCAST:
3405 case RID_CONSTCAST:
3407 tree type;
3408 tree expression;
3409 const char *saved_message;
3411 /* All of these can be handled in the same way from the point
3412 of view of parsing. Begin by consuming the token
3413 identifying the cast. */
3414 cp_lexer_consume_token (parser->lexer);
3416 /* New types cannot be defined in the cast. */
3417 saved_message = parser->type_definition_forbidden_message;
3418 parser->type_definition_forbidden_message
3419 = "types may not be defined in casts";
3421 /* Look for the opening `<'. */
3422 cp_parser_require (parser, CPP_LESS, "`<'");
3423 /* Parse the type to which we are casting. */
3424 type = cp_parser_type_id (parser);
3425 /* Look for the closing `>'. */
3426 cp_parser_require (parser, CPP_GREATER, "`>'");
3427 /* Restore the old message. */
3428 parser->type_definition_forbidden_message = saved_message;
3430 /* And the expression which is being cast. */
3431 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3432 expression = cp_parser_expression (parser);
3433 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3435 /* Only type conversions to integral or enumeration types
3436 can be used in constant-expressions. */
3437 if (parser->integral_constant_expression_p
3438 && !dependent_type_p (type)
3439 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3440 /* A cast to pointer or reference type is allowed in the
3441 implementation of "offsetof". */
3442 && !(parser->in_offsetof_p && POINTER_TYPE_P (type)))
3444 if (!parser->allow_non_integral_constant_expression_p)
3445 return (cp_parser_non_integral_constant_expression
3446 ("a cast to a type other than an integral or "
3447 "enumeration type"));
3448 parser->non_integral_constant_expression_p = true;
3451 switch (keyword)
3453 case RID_DYNCAST:
3454 postfix_expression
3455 = build_dynamic_cast (type, expression);
3456 break;
3457 case RID_STATCAST:
3458 postfix_expression
3459 = build_static_cast (type, expression);
3460 break;
3461 case RID_REINTCAST:
3462 postfix_expression
3463 = build_reinterpret_cast (type, expression);
3464 break;
3465 case RID_CONSTCAST:
3466 postfix_expression
3467 = build_const_cast (type, expression);
3468 break;
3469 default:
3470 abort ();
3473 break;
3475 case RID_TYPEID:
3477 tree type;
3478 const char *saved_message;
3479 bool saved_in_type_id_in_expr_p;
3481 /* Consume the `typeid' token. */
3482 cp_lexer_consume_token (parser->lexer);
3483 /* Look for the `(' token. */
3484 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3485 /* Types cannot be defined in a `typeid' expression. */
3486 saved_message = parser->type_definition_forbidden_message;
3487 parser->type_definition_forbidden_message
3488 = "types may not be defined in a `typeid\' expression";
3489 /* We can't be sure yet whether we're looking at a type-id or an
3490 expression. */
3491 cp_parser_parse_tentatively (parser);
3492 /* Try a type-id first. */
3493 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3494 parser->in_type_id_in_expr_p = true;
3495 type = cp_parser_type_id (parser);
3496 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3497 /* Look for the `)' token. Otherwise, we can't be sure that
3498 we're not looking at an expression: consider `typeid (int
3499 (3))', for example. */
3500 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3501 /* If all went well, simply lookup the type-id. */
3502 if (cp_parser_parse_definitely (parser))
3503 postfix_expression = get_typeid (type);
3504 /* Otherwise, fall back to the expression variant. */
3505 else
3507 tree expression;
3509 /* Look for an expression. */
3510 expression = cp_parser_expression (parser);
3511 /* Compute its typeid. */
3512 postfix_expression = build_typeid (expression);
3513 /* Look for the `)' token. */
3514 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3517 /* Restore the saved message. */
3518 parser->type_definition_forbidden_message = saved_message;
3520 break;
3522 case RID_TYPENAME:
3524 bool template_p = false;
3525 tree id;
3526 tree type;
3528 /* Consume the `typename' token. */
3529 cp_lexer_consume_token (parser->lexer);
3530 /* Look for the optional `::' operator. */
3531 cp_parser_global_scope_opt (parser,
3532 /*current_scope_valid_p=*/false);
3533 /* Look for the nested-name-specifier. */
3534 cp_parser_nested_name_specifier (parser,
3535 /*typename_keyword_p=*/true,
3536 /*check_dependency_p=*/true,
3537 /*type_p=*/true,
3538 /*is_declaration=*/true);
3539 /* Look for the optional `template' keyword. */
3540 template_p = cp_parser_optional_template_keyword (parser);
3541 /* We don't know whether we're looking at a template-id or an
3542 identifier. */
3543 cp_parser_parse_tentatively (parser);
3544 /* Try a template-id. */
3545 id = cp_parser_template_id (parser, template_p,
3546 /*check_dependency_p=*/true,
3547 /*is_declaration=*/true);
3548 /* If that didn't work, try an identifier. */
3549 if (!cp_parser_parse_definitely (parser))
3550 id = cp_parser_identifier (parser);
3551 /* Create a TYPENAME_TYPE to represent the type to which the
3552 functional cast is being performed. */
3553 type = make_typename_type (parser->scope, id,
3554 /*complain=*/1);
3556 postfix_expression = cp_parser_functional_cast (parser, type);
3558 break;
3560 default:
3562 tree type;
3564 /* If the next thing is a simple-type-specifier, we may be
3565 looking at a functional cast. We could also be looking at
3566 an id-expression. So, we try the functional cast, and if
3567 that doesn't work we fall back to the primary-expression. */
3568 cp_parser_parse_tentatively (parser);
3569 /* Look for the simple-type-specifier. */
3570 type = cp_parser_simple_type_specifier (parser,
3571 CP_PARSER_FLAGS_NONE,
3572 /*identifier_p=*/false);
3573 /* Parse the cast itself. */
3574 if (!cp_parser_error_occurred (parser))
3575 postfix_expression
3576 = cp_parser_functional_cast (parser, type);
3577 /* If that worked, we're done. */
3578 if (cp_parser_parse_definitely (parser))
3579 break;
3581 /* If the functional-cast didn't work out, try a
3582 compound-literal. */
3583 if (cp_parser_allow_gnu_extensions_p (parser)
3584 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3586 tree initializer_list = NULL_TREE;
3587 bool saved_in_type_id_in_expr_p;
3589 cp_parser_parse_tentatively (parser);
3590 /* Consume the `('. */
3591 cp_lexer_consume_token (parser->lexer);
3592 /* Parse the type. */
3593 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3594 parser->in_type_id_in_expr_p = true;
3595 type = cp_parser_type_id (parser);
3596 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3597 /* Look for the `)'. */
3598 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3599 /* Look for the `{'. */
3600 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3601 /* If things aren't going well, there's no need to
3602 keep going. */
3603 if (!cp_parser_error_occurred (parser))
3605 bool non_constant_p;
3606 /* Parse the initializer-list. */
3607 initializer_list
3608 = cp_parser_initializer_list (parser, &non_constant_p);
3609 /* Allow a trailing `,'. */
3610 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3611 cp_lexer_consume_token (parser->lexer);
3612 /* Look for the final `}'. */
3613 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3615 /* If that worked, we're definitely looking at a
3616 compound-literal expression. */
3617 if (cp_parser_parse_definitely (parser))
3619 /* Warn the user that a compound literal is not
3620 allowed in standard C++. */
3621 if (pedantic)
3622 pedwarn ("ISO C++ forbids compound-literals");
3623 /* Form the representation of the compound-literal. */
3624 postfix_expression
3625 = finish_compound_literal (type, initializer_list);
3626 break;
3630 /* It must be a primary-expression. */
3631 postfix_expression = cp_parser_primary_expression (parser,
3632 &idk,
3633 &qualifying_class);
3635 break;
3638 /* If we were avoiding committing to the processing of a
3639 qualified-id until we knew whether or not we had a
3640 pointer-to-member, we now know. */
3641 if (qualifying_class)
3643 bool done;
3645 /* Peek at the next token. */
3646 token = cp_lexer_peek_token (parser->lexer);
3647 done = (token->type != CPP_OPEN_SQUARE
3648 && token->type != CPP_OPEN_PAREN
3649 && token->type != CPP_DOT
3650 && token->type != CPP_DEREF
3651 && token->type != CPP_PLUS_PLUS
3652 && token->type != CPP_MINUS_MINUS);
3654 postfix_expression = finish_qualified_id_expr (qualifying_class,
3655 postfix_expression,
3656 done,
3657 address_p);
3658 if (done)
3659 return postfix_expression;
3662 /* Keep looping until the postfix-expression is complete. */
3663 while (true)
3665 if (idk == CP_ID_KIND_UNQUALIFIED
3666 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3667 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3668 /* It is not a Koenig lookup function call. */
3669 postfix_expression
3670 = unqualified_name_lookup_error (postfix_expression);
3672 /* Peek at the next token. */
3673 token = cp_lexer_peek_token (parser->lexer);
3675 switch (token->type)
3677 case CPP_OPEN_SQUARE:
3678 /* postfix-expression [ expression ] */
3680 tree index;
3682 /* Consume the `[' token. */
3683 cp_lexer_consume_token (parser->lexer);
3684 /* Parse the index expression. */
3685 index = cp_parser_expression (parser);
3686 /* Look for the closing `]'. */
3687 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3689 /* Build the ARRAY_REF. */
3690 postfix_expression
3691 = grok_array_decl (postfix_expression, index);
3692 idk = CP_ID_KIND_NONE;
3693 /* Array references are not permitted in
3694 constant-expressions. */
3695 if (parser->integral_constant_expression_p)
3697 if (!parser->allow_non_integral_constant_expression_p)
3698 postfix_expression
3699 = cp_parser_non_integral_constant_expression ("an array reference");
3700 parser->non_integral_constant_expression_p = true;
3703 break;
3705 case CPP_OPEN_PAREN:
3706 /* postfix-expression ( expression-list [opt] ) */
3708 bool koenig_p;
3709 tree args = (cp_parser_parenthesized_expression_list
3710 (parser, false, /*non_constant_p=*/NULL));
3712 if (args == error_mark_node)
3714 postfix_expression = error_mark_node;
3715 break;
3718 /* Function calls are not permitted in
3719 constant-expressions. */
3720 if (parser->integral_constant_expression_p)
3722 if (!parser->allow_non_integral_constant_expression_p)
3724 postfix_expression
3725 = cp_parser_non_integral_constant_expression ("a function call");
3726 break;
3728 parser->non_integral_constant_expression_p = true;
3731 koenig_p = false;
3732 if (idk == CP_ID_KIND_UNQUALIFIED)
3734 if (args
3735 && (is_overloaded_fn (postfix_expression)
3736 || DECL_P (postfix_expression)
3737 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3739 koenig_p = true;
3740 postfix_expression
3741 = perform_koenig_lookup (postfix_expression, args);
3743 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3744 postfix_expression
3745 = unqualified_fn_lookup_error (postfix_expression);
3748 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3750 tree instance = TREE_OPERAND (postfix_expression, 0);
3751 tree fn = TREE_OPERAND (postfix_expression, 1);
3753 if (processing_template_decl
3754 && (type_dependent_expression_p (instance)
3755 || (!BASELINK_P (fn)
3756 && TREE_CODE (fn) != FIELD_DECL)
3757 || type_dependent_expression_p (fn)
3758 || any_type_dependent_arguments_p (args)))
3760 postfix_expression
3761 = build_min_nt (CALL_EXPR, postfix_expression, args);
3762 break;
3765 postfix_expression
3766 = (build_new_method_call
3767 (instance, fn, args, NULL_TREE,
3768 (idk == CP_ID_KIND_QUALIFIED
3769 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3771 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3772 || TREE_CODE (postfix_expression) == MEMBER_REF
3773 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3774 postfix_expression = (build_offset_ref_call_from_tree
3775 (postfix_expression, args));
3776 else if (idk == CP_ID_KIND_QUALIFIED)
3777 /* A call to a static class member, or a namespace-scope
3778 function. */
3779 postfix_expression
3780 = finish_call_expr (postfix_expression, args,
3781 /*disallow_virtual=*/true,
3782 koenig_p);
3783 else
3784 /* All other function calls. */
3785 postfix_expression
3786 = finish_call_expr (postfix_expression, args,
3787 /*disallow_virtual=*/false,
3788 koenig_p);
3790 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3791 idk = CP_ID_KIND_NONE;
3793 break;
3795 case CPP_DOT:
3796 case CPP_DEREF:
3797 /* postfix-expression . template [opt] id-expression
3798 postfix-expression . pseudo-destructor-name
3799 postfix-expression -> template [opt] id-expression
3800 postfix-expression -> pseudo-destructor-name */
3802 tree name;
3803 bool dependent_p;
3804 bool template_p;
3805 tree scope = NULL_TREE;
3806 enum cpp_ttype token_type = token->type;
3808 /* If this is a `->' operator, dereference the pointer. */
3809 if (token->type == CPP_DEREF)
3810 postfix_expression = build_x_arrow (postfix_expression);
3811 /* Check to see whether or not the expression is
3812 type-dependent. */
3813 dependent_p = type_dependent_expression_p (postfix_expression);
3814 /* The identifier following the `->' or `.' is not
3815 qualified. */
3816 parser->scope = NULL_TREE;
3817 parser->qualifying_scope = NULL_TREE;
3818 parser->object_scope = NULL_TREE;
3819 idk = CP_ID_KIND_NONE;
3820 /* Enter the scope corresponding to the type of the object
3821 given by the POSTFIX_EXPRESSION. */
3822 if (!dependent_p
3823 && TREE_TYPE (postfix_expression) != NULL_TREE)
3825 scope = TREE_TYPE (postfix_expression);
3826 /* According to the standard, no expression should
3827 ever have reference type. Unfortunately, we do not
3828 currently match the standard in this respect in
3829 that our internal representation of an expression
3830 may have reference type even when the standard says
3831 it does not. Therefore, we have to manually obtain
3832 the underlying type here. */
3833 scope = non_reference (scope);
3834 /* The type of the POSTFIX_EXPRESSION must be
3835 complete. */
3836 scope = complete_type_or_else (scope, NULL_TREE);
3837 /* Let the name lookup machinery know that we are
3838 processing a class member access expression. */
3839 parser->context->object_type = scope;
3840 /* If something went wrong, we want to be able to
3841 discern that case, as opposed to the case where
3842 there was no SCOPE due to the type of expression
3843 being dependent. */
3844 if (!scope)
3845 scope = error_mark_node;
3848 /* Consume the `.' or `->' operator. */
3849 cp_lexer_consume_token (parser->lexer);
3850 /* If the SCOPE is not a scalar type, we are looking at an
3851 ordinary class member access expression, rather than a
3852 pseudo-destructor-name. */
3853 if (!scope || !SCALAR_TYPE_P (scope))
3855 template_p = cp_parser_optional_template_keyword (parser);
3856 /* Parse the id-expression. */
3857 name = cp_parser_id_expression (parser,
3858 template_p,
3859 /*check_dependency_p=*/true,
3860 /*template_p=*/NULL,
3861 /*declarator_p=*/false);
3862 /* In general, build a SCOPE_REF if the member name is
3863 qualified. However, if the name was not dependent
3864 and has already been resolved; there is no need to
3865 build the SCOPE_REF. For example;
3867 struct X { void f(); };
3868 template <typename T> void f(T* t) { t->X::f(); }
3870 Even though "t" is dependent, "X::f" is not and has
3871 been resolved to a BASELINK; there is no need to
3872 include scope information. */
3874 /* But we do need to remember that there was an explicit
3875 scope for virtual function calls. */
3876 if (parser->scope)
3877 idk = CP_ID_KIND_QUALIFIED;
3879 if (name != error_mark_node
3880 && !BASELINK_P (name)
3881 && parser->scope)
3883 name = build_nt (SCOPE_REF, parser->scope, name);
3884 parser->scope = NULL_TREE;
3885 parser->qualifying_scope = NULL_TREE;
3886 parser->object_scope = NULL_TREE;
3888 postfix_expression
3889 = finish_class_member_access_expr (postfix_expression, name);
3891 /* Otherwise, try the pseudo-destructor-name production. */
3892 else
3894 tree s = NULL_TREE;
3895 tree type;
3897 /* Parse the pseudo-destructor-name. */
3898 cp_parser_pseudo_destructor_name (parser, &s, &type);
3899 /* Form the call. */
3900 postfix_expression
3901 = finish_pseudo_destructor_expr (postfix_expression,
3902 s, TREE_TYPE (type));
3905 /* We no longer need to look up names in the scope of the
3906 object on the left-hand side of the `.' or `->'
3907 operator. */
3908 parser->context->object_type = NULL_TREE;
3909 /* These operators may not appear in constant-expressions. */
3910 if (parser->integral_constant_expression_p
3911 /* The "->" operator is allowed in the implementation
3912 of "offsetof". The "." operator may appear in the
3913 name of the member. */
3914 && !parser->in_offsetof_p)
3916 if (!parser->allow_non_integral_constant_expression_p)
3917 postfix_expression
3918 = (cp_parser_non_integral_constant_expression
3919 (token_type == CPP_DEREF ? "'->'" : "`.'"));
3920 parser->non_integral_constant_expression_p = true;
3923 break;
3925 case CPP_PLUS_PLUS:
3926 /* postfix-expression ++ */
3927 /* Consume the `++' token. */
3928 cp_lexer_consume_token (parser->lexer);
3929 /* Generate a representation for the complete expression. */
3930 postfix_expression
3931 = finish_increment_expr (postfix_expression,
3932 POSTINCREMENT_EXPR);
3933 /* Increments may not appear in constant-expressions. */
3934 if (parser->integral_constant_expression_p)
3936 if (!parser->allow_non_integral_constant_expression_p)
3937 postfix_expression
3938 = cp_parser_non_integral_constant_expression ("an increment");
3939 parser->non_integral_constant_expression_p = true;
3941 idk = CP_ID_KIND_NONE;
3942 break;
3944 case CPP_MINUS_MINUS:
3945 /* postfix-expression -- */
3946 /* Consume the `--' token. */
3947 cp_lexer_consume_token (parser->lexer);
3948 /* Generate a representation for the complete expression. */
3949 postfix_expression
3950 = finish_increment_expr (postfix_expression,
3951 POSTDECREMENT_EXPR);
3952 /* Decrements may not appear in constant-expressions. */
3953 if (parser->integral_constant_expression_p)
3955 if (!parser->allow_non_integral_constant_expression_p)
3956 postfix_expression
3957 = cp_parser_non_integral_constant_expression ("a decrement");
3958 parser->non_integral_constant_expression_p = true;
3960 idk = CP_ID_KIND_NONE;
3961 break;
3963 default:
3964 return postfix_expression;
3968 /* We should never get here. */
3969 abort ();
3970 return error_mark_node;
3973 /* Parse a parenthesized expression-list.
3975 expression-list:
3976 assignment-expression
3977 expression-list, assignment-expression
3979 attribute-list:
3980 expression-list
3981 identifier
3982 identifier, expression-list
3984 Returns a TREE_LIST. The TREE_VALUE of each node is a
3985 representation of an assignment-expression. Note that a TREE_LIST
3986 is returned even if there is only a single expression in the list.
3987 error_mark_node is returned if the ( and or ) are
3988 missing. NULL_TREE is returned on no expressions. The parentheses
3989 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3990 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3991 indicates whether or not all of the expressions in the list were
3992 constant. */
3994 static tree
3995 cp_parser_parenthesized_expression_list (cp_parser* parser,
3996 bool is_attribute_list,
3997 bool *non_constant_p)
3999 tree expression_list = NULL_TREE;
4000 tree identifier = NULL_TREE;
4002 /* Assume all the expressions will be constant. */
4003 if (non_constant_p)
4004 *non_constant_p = false;
4006 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4007 return error_mark_node;
4009 /* Consume expressions until there are no more. */
4010 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4011 while (true)
4013 tree expr;
4015 /* At the beginning of attribute lists, check to see if the
4016 next token is an identifier. */
4017 if (is_attribute_list
4018 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4020 cp_token *token;
4022 /* Consume the identifier. */
4023 token = cp_lexer_consume_token (parser->lexer);
4024 /* Save the identifier. */
4025 identifier = token->value;
4027 else
4029 /* Parse the next assignment-expression. */
4030 if (non_constant_p)
4032 bool expr_non_constant_p;
4033 expr = (cp_parser_constant_expression
4034 (parser, /*allow_non_constant_p=*/true,
4035 &expr_non_constant_p));
4036 if (expr_non_constant_p)
4037 *non_constant_p = true;
4039 else
4040 expr = cp_parser_assignment_expression (parser);
4042 /* Add it to the list. We add error_mark_node
4043 expressions to the list, so that we can still tell if
4044 the correct form for a parenthesized expression-list
4045 is found. That gives better errors. */
4046 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4048 if (expr == error_mark_node)
4049 goto skip_comma;
4052 /* After the first item, attribute lists look the same as
4053 expression lists. */
4054 is_attribute_list = false;
4056 get_comma:;
4057 /* If the next token isn't a `,', then we are done. */
4058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4059 break;
4061 /* Otherwise, consume the `,' and keep going. */
4062 cp_lexer_consume_token (parser->lexer);
4065 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4067 int ending;
4069 skip_comma:;
4070 /* We try and resync to an unnested comma, as that will give the
4071 user better diagnostics. */
4072 ending = cp_parser_skip_to_closing_parenthesis (parser,
4073 /*recovering=*/true,
4074 /*or_comma=*/true,
4075 /*consume_paren=*/true);
4076 if (ending < 0)
4077 goto get_comma;
4078 if (!ending)
4079 return error_mark_node;
4082 /* We built up the list in reverse order so we must reverse it now. */
4083 expression_list = nreverse (expression_list);
4084 if (identifier)
4085 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4087 return expression_list;
4090 /* Parse a pseudo-destructor-name.
4092 pseudo-destructor-name:
4093 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4094 :: [opt] nested-name-specifier template template-id :: ~ type-name
4095 :: [opt] nested-name-specifier [opt] ~ type-name
4097 If either of the first two productions is used, sets *SCOPE to the
4098 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4099 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4100 or ERROR_MARK_NODE if no type-name is present. */
4102 static void
4103 cp_parser_pseudo_destructor_name (cp_parser* parser,
4104 tree* scope,
4105 tree* type)
4107 bool nested_name_specifier_p;
4109 /* Look for the optional `::' operator. */
4110 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4111 /* Look for the optional nested-name-specifier. */
4112 nested_name_specifier_p
4113 = (cp_parser_nested_name_specifier_opt (parser,
4114 /*typename_keyword_p=*/false,
4115 /*check_dependency_p=*/true,
4116 /*type_p=*/false,
4117 /*is_declaration=*/true)
4118 != NULL_TREE);
4119 /* Now, if we saw a nested-name-specifier, we might be doing the
4120 second production. */
4121 if (nested_name_specifier_p
4122 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4124 /* Consume the `template' keyword. */
4125 cp_lexer_consume_token (parser->lexer);
4126 /* Parse the template-id. */
4127 cp_parser_template_id (parser,
4128 /*template_keyword_p=*/true,
4129 /*check_dependency_p=*/false,
4130 /*is_declaration=*/true);
4131 /* Look for the `::' token. */
4132 cp_parser_require (parser, CPP_SCOPE, "`::'");
4134 /* If the next token is not a `~', then there might be some
4135 additional qualification. */
4136 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4138 /* Look for the type-name. */
4139 *scope = TREE_TYPE (cp_parser_type_name (parser));
4140 /* Look for the `::' token. */
4141 cp_parser_require (parser, CPP_SCOPE, "`::'");
4143 else
4144 *scope = NULL_TREE;
4146 /* Look for the `~'. */
4147 cp_parser_require (parser, CPP_COMPL, "`~'");
4148 /* Look for the type-name again. We are not responsible for
4149 checking that it matches the first type-name. */
4150 *type = cp_parser_type_name (parser);
4153 /* Parse a unary-expression.
4155 unary-expression:
4156 postfix-expression
4157 ++ cast-expression
4158 -- cast-expression
4159 unary-operator cast-expression
4160 sizeof unary-expression
4161 sizeof ( type-id )
4162 new-expression
4163 delete-expression
4165 GNU Extensions:
4167 unary-expression:
4168 __extension__ cast-expression
4169 __alignof__ unary-expression
4170 __alignof__ ( type-id )
4171 __real__ cast-expression
4172 __imag__ cast-expression
4173 && identifier
4175 ADDRESS_P is true iff the unary-expression is appearing as the
4176 operand of the `&' operator.
4178 Returns a representation of the expression. */
4180 static tree
4181 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4183 cp_token *token;
4184 enum tree_code unary_operator;
4186 /* Peek at the next token. */
4187 token = cp_lexer_peek_token (parser->lexer);
4188 /* Some keywords give away the kind of expression. */
4189 if (token->type == CPP_KEYWORD)
4191 enum rid keyword = token->keyword;
4193 switch (keyword)
4195 case RID_ALIGNOF:
4196 case RID_SIZEOF:
4198 tree operand;
4199 enum tree_code op;
4201 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4202 /* Consume the token. */
4203 cp_lexer_consume_token (parser->lexer);
4204 /* Parse the operand. */
4205 operand = cp_parser_sizeof_operand (parser, keyword);
4207 if (TYPE_P (operand))
4208 return cxx_sizeof_or_alignof_type (operand, op, true);
4209 else
4210 return cxx_sizeof_or_alignof_expr (operand, op);
4213 case RID_NEW:
4214 return cp_parser_new_expression (parser);
4216 case RID_DELETE:
4217 return cp_parser_delete_expression (parser);
4219 case RID_EXTENSION:
4221 /* The saved value of the PEDANTIC flag. */
4222 int saved_pedantic;
4223 tree expr;
4225 /* Save away the PEDANTIC flag. */
4226 cp_parser_extension_opt (parser, &saved_pedantic);
4227 /* Parse the cast-expression. */
4228 expr = cp_parser_simple_cast_expression (parser);
4229 /* Restore the PEDANTIC flag. */
4230 pedantic = saved_pedantic;
4232 return expr;
4235 case RID_REALPART:
4236 case RID_IMAGPART:
4238 tree expression;
4240 /* Consume the `__real__' or `__imag__' token. */
4241 cp_lexer_consume_token (parser->lexer);
4242 /* Parse the cast-expression. */
4243 expression = cp_parser_simple_cast_expression (parser);
4244 /* Create the complete representation. */
4245 return build_x_unary_op ((keyword == RID_REALPART
4246 ? REALPART_EXPR : IMAGPART_EXPR),
4247 expression);
4249 break;
4251 default:
4252 break;
4256 /* Look for the `:: new' and `:: delete', which also signal the
4257 beginning of a new-expression, or delete-expression,
4258 respectively. If the next token is `::', then it might be one of
4259 these. */
4260 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4262 enum rid keyword;
4264 /* See if the token after the `::' is one of the keywords in
4265 which we're interested. */
4266 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4267 /* If it's `new', we have a new-expression. */
4268 if (keyword == RID_NEW)
4269 return cp_parser_new_expression (parser);
4270 /* Similarly, for `delete'. */
4271 else if (keyword == RID_DELETE)
4272 return cp_parser_delete_expression (parser);
4275 /* Look for a unary operator. */
4276 unary_operator = cp_parser_unary_operator (token);
4277 /* The `++' and `--' operators can be handled similarly, even though
4278 they are not technically unary-operators in the grammar. */
4279 if (unary_operator == ERROR_MARK)
4281 if (token->type == CPP_PLUS_PLUS)
4282 unary_operator = PREINCREMENT_EXPR;
4283 else if (token->type == CPP_MINUS_MINUS)
4284 unary_operator = PREDECREMENT_EXPR;
4285 /* Handle the GNU address-of-label extension. */
4286 else if (cp_parser_allow_gnu_extensions_p (parser)
4287 && token->type == CPP_AND_AND)
4289 tree identifier;
4291 /* Consume the '&&' token. */
4292 cp_lexer_consume_token (parser->lexer);
4293 /* Look for the identifier. */
4294 identifier = cp_parser_identifier (parser);
4295 /* Create an expression representing the address. */
4296 return finish_label_address_expr (identifier);
4299 if (unary_operator != ERROR_MARK)
4301 tree cast_expression;
4302 tree expression = error_mark_node;
4303 const char *non_constant_p = NULL;
4305 /* Consume the operator token. */
4306 token = cp_lexer_consume_token (parser->lexer);
4307 /* Parse the cast-expression. */
4308 cast_expression
4309 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4310 /* Now, build an appropriate representation. */
4311 switch (unary_operator)
4313 case INDIRECT_REF:
4314 non_constant_p = "`*'";
4315 expression = build_x_indirect_ref (cast_expression, "unary *");
4316 break;
4318 case ADDR_EXPR:
4319 /* The "&" operator is allowed in the implementation of
4320 "offsetof". */
4321 if (!parser->in_offsetof_p)
4322 non_constant_p = "`&'";
4323 /* Fall through. */
4324 case BIT_NOT_EXPR:
4325 expression = build_x_unary_op (unary_operator, cast_expression);
4326 break;
4328 case PREINCREMENT_EXPR:
4329 case PREDECREMENT_EXPR:
4330 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4331 ? "`++'" : "`--'");
4332 /* Fall through. */
4333 case CONVERT_EXPR:
4334 case NEGATE_EXPR:
4335 case TRUTH_NOT_EXPR:
4336 expression = finish_unary_op_expr (unary_operator, cast_expression);
4337 break;
4339 default:
4340 abort ();
4343 if (non_constant_p && parser->integral_constant_expression_p)
4345 if (!parser->allow_non_integral_constant_expression_p)
4346 return cp_parser_non_integral_constant_expression (non_constant_p);
4347 parser->non_integral_constant_expression_p = true;
4350 return expression;
4353 return cp_parser_postfix_expression (parser, address_p);
4356 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4357 unary-operator, the corresponding tree code is returned. */
4359 static enum tree_code
4360 cp_parser_unary_operator (cp_token* token)
4362 switch (token->type)
4364 case CPP_MULT:
4365 return INDIRECT_REF;
4367 case CPP_AND:
4368 return ADDR_EXPR;
4370 case CPP_PLUS:
4371 return CONVERT_EXPR;
4373 case CPP_MINUS:
4374 return NEGATE_EXPR;
4376 case CPP_NOT:
4377 return TRUTH_NOT_EXPR;
4379 case CPP_COMPL:
4380 return BIT_NOT_EXPR;
4382 default:
4383 return ERROR_MARK;
4387 /* Parse a new-expression.
4389 new-expression:
4390 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4391 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4393 Returns a representation of the expression. */
4395 static tree
4396 cp_parser_new_expression (cp_parser* parser)
4398 bool global_scope_p;
4399 tree placement;
4400 tree type;
4401 tree initializer;
4403 /* Look for the optional `::' operator. */
4404 global_scope_p
4405 = (cp_parser_global_scope_opt (parser,
4406 /*current_scope_valid_p=*/false)
4407 != NULL_TREE);
4408 /* Look for the `new' operator. */
4409 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4410 /* There's no easy way to tell a new-placement from the
4411 `( type-id )' construct. */
4412 cp_parser_parse_tentatively (parser);
4413 /* Look for a new-placement. */
4414 placement = cp_parser_new_placement (parser);
4415 /* If that didn't work out, there's no new-placement. */
4416 if (!cp_parser_parse_definitely (parser))
4417 placement = NULL_TREE;
4419 /* If the next token is a `(', then we have a parenthesized
4420 type-id. */
4421 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4423 /* Consume the `('. */
4424 cp_lexer_consume_token (parser->lexer);
4425 /* Parse the type-id. */
4426 type = cp_parser_type_id (parser);
4427 /* Look for the closing `)'. */
4428 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4430 /* Otherwise, there must be a new-type-id. */
4431 else
4432 type = cp_parser_new_type_id (parser);
4434 /* If the next token is a `(', then we have a new-initializer. */
4435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4436 initializer = cp_parser_new_initializer (parser);
4437 else
4438 initializer = NULL_TREE;
4440 /* Create a representation of the new-expression. */
4441 return build_new (placement, type, initializer, global_scope_p);
4444 /* Parse a new-placement.
4446 new-placement:
4447 ( expression-list )
4449 Returns the same representation as for an expression-list. */
4451 static tree
4452 cp_parser_new_placement (cp_parser* parser)
4454 tree expression_list;
4456 /* Parse the expression-list. */
4457 expression_list = (cp_parser_parenthesized_expression_list
4458 (parser, false, /*non_constant_p=*/NULL));
4460 return expression_list;
4463 /* Parse a new-type-id.
4465 new-type-id:
4466 type-specifier-seq new-declarator [opt]
4468 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4469 and whose TREE_VALUE is the new-declarator. */
4471 static tree
4472 cp_parser_new_type_id (cp_parser* parser)
4474 tree type_specifier_seq;
4475 tree declarator;
4476 const char *saved_message;
4478 /* The type-specifier sequence must not contain type definitions.
4479 (It cannot contain declarations of new types either, but if they
4480 are not definitions we will catch that because they are not
4481 complete.) */
4482 saved_message = parser->type_definition_forbidden_message;
4483 parser->type_definition_forbidden_message
4484 = "types may not be defined in a new-type-id";
4485 /* Parse the type-specifier-seq. */
4486 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4487 /* Restore the old message. */
4488 parser->type_definition_forbidden_message = saved_message;
4489 /* Parse the new-declarator. */
4490 declarator = cp_parser_new_declarator_opt (parser);
4492 return build_tree_list (type_specifier_seq, declarator);
4495 /* Parse an (optional) new-declarator.
4497 new-declarator:
4498 ptr-operator new-declarator [opt]
4499 direct-new-declarator
4501 Returns a representation of the declarator. See
4502 cp_parser_declarator for the representations used. */
4504 static tree
4505 cp_parser_new_declarator_opt (cp_parser* parser)
4507 enum tree_code code;
4508 tree type;
4509 tree cv_qualifier_seq;
4511 /* We don't know if there's a ptr-operator next, or not. */
4512 cp_parser_parse_tentatively (parser);
4513 /* Look for a ptr-operator. */
4514 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4515 /* If that worked, look for more new-declarators. */
4516 if (cp_parser_parse_definitely (parser))
4518 tree declarator;
4520 /* Parse another optional declarator. */
4521 declarator = cp_parser_new_declarator_opt (parser);
4523 /* Create the representation of the declarator. */
4524 if (code == INDIRECT_REF)
4525 declarator = make_pointer_declarator (cv_qualifier_seq,
4526 declarator);
4527 else
4528 declarator = make_reference_declarator (cv_qualifier_seq,
4529 declarator);
4531 /* Handle the pointer-to-member case. */
4532 if (type)
4533 declarator = build_nt (SCOPE_REF, type, declarator);
4535 return declarator;
4538 /* If the next token is a `[', there is a direct-new-declarator. */
4539 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4540 return cp_parser_direct_new_declarator (parser);
4542 return NULL_TREE;
4545 /* Parse a direct-new-declarator.
4547 direct-new-declarator:
4548 [ expression ]
4549 direct-new-declarator [constant-expression]
4551 Returns an ARRAY_REF, following the same conventions as are
4552 documented for cp_parser_direct_declarator. */
4554 static tree
4555 cp_parser_direct_new_declarator (cp_parser* parser)
4557 tree declarator = NULL_TREE;
4559 while (true)
4561 tree expression;
4563 /* Look for the opening `['. */
4564 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4565 /* The first expression is not required to be constant. */
4566 if (!declarator)
4568 expression = cp_parser_expression (parser);
4569 /* The standard requires that the expression have integral
4570 type. DR 74 adds enumeration types. We believe that the
4571 real intent is that these expressions be handled like the
4572 expression in a `switch' condition, which also allows
4573 classes with a single conversion to integral or
4574 enumeration type. */
4575 if (!processing_template_decl)
4577 expression
4578 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4579 expression,
4580 /*complain=*/true);
4581 if (!expression)
4583 error ("expression in new-declarator must have integral or enumeration type");
4584 expression = error_mark_node;
4588 /* But all the other expressions must be. */
4589 else
4590 expression
4591 = cp_parser_constant_expression (parser,
4592 /*allow_non_constant=*/false,
4593 NULL);
4594 /* Look for the closing `]'. */
4595 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4597 /* Add this bound to the declarator. */
4598 declarator = build_nt (ARRAY_REF, declarator, expression);
4600 /* If the next token is not a `[', then there are no more
4601 bounds. */
4602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4603 break;
4606 return declarator;
4609 /* Parse a new-initializer.
4611 new-initializer:
4612 ( expression-list [opt] )
4614 Returns a representation of the expression-list. If there is no
4615 expression-list, VOID_ZERO_NODE is returned. */
4617 static tree
4618 cp_parser_new_initializer (cp_parser* parser)
4620 tree expression_list;
4622 expression_list = (cp_parser_parenthesized_expression_list
4623 (parser, false, /*non_constant_p=*/NULL));
4624 if (!expression_list)
4625 expression_list = void_zero_node;
4627 return expression_list;
4630 /* Parse a delete-expression.
4632 delete-expression:
4633 :: [opt] delete cast-expression
4634 :: [opt] delete [ ] cast-expression
4636 Returns a representation of the expression. */
4638 static tree
4639 cp_parser_delete_expression (cp_parser* parser)
4641 bool global_scope_p;
4642 bool array_p;
4643 tree expression;
4645 /* Look for the optional `::' operator. */
4646 global_scope_p
4647 = (cp_parser_global_scope_opt (parser,
4648 /*current_scope_valid_p=*/false)
4649 != NULL_TREE);
4650 /* Look for the `delete' keyword. */
4651 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4652 /* See if the array syntax is in use. */
4653 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4655 /* Consume the `[' token. */
4656 cp_lexer_consume_token (parser->lexer);
4657 /* Look for the `]' token. */
4658 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4659 /* Remember that this is the `[]' construct. */
4660 array_p = true;
4662 else
4663 array_p = false;
4665 /* Parse the cast-expression. */
4666 expression = cp_parser_simple_cast_expression (parser);
4668 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4671 /* Parse a cast-expression.
4673 cast-expression:
4674 unary-expression
4675 ( type-id ) cast-expression
4677 Returns a representation of the expression. */
4679 static tree
4680 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4682 /* If it's a `(', then we might be looking at a cast. */
4683 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4685 tree type = NULL_TREE;
4686 tree expr = NULL_TREE;
4687 bool compound_literal_p;
4688 const char *saved_message;
4690 /* There's no way to know yet whether or not this is a cast.
4691 For example, `(int (3))' is a unary-expression, while `(int)
4692 3' is a cast. So, we resort to parsing tentatively. */
4693 cp_parser_parse_tentatively (parser);
4694 /* Types may not be defined in a cast. */
4695 saved_message = parser->type_definition_forbidden_message;
4696 parser->type_definition_forbidden_message
4697 = "types may not be defined in casts";
4698 /* Consume the `('. */
4699 cp_lexer_consume_token (parser->lexer);
4700 /* A very tricky bit is that `(struct S) { 3 }' is a
4701 compound-literal (which we permit in C++ as an extension).
4702 But, that construct is not a cast-expression -- it is a
4703 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4704 is legal; if the compound-literal were a cast-expression,
4705 you'd need an extra set of parentheses.) But, if we parse
4706 the type-id, and it happens to be a class-specifier, then we
4707 will commit to the parse at that point, because we cannot
4708 undo the action that is done when creating a new class. So,
4709 then we cannot back up and do a postfix-expression.
4711 Therefore, we scan ahead to the closing `)', and check to see
4712 if the token after the `)' is a `{'. If so, we are not
4713 looking at a cast-expression.
4715 Save tokens so that we can put them back. */
4716 cp_lexer_save_tokens (parser->lexer);
4717 /* Skip tokens until the next token is a closing parenthesis.
4718 If we find the closing `)', and the next token is a `{', then
4719 we are looking at a compound-literal. */
4720 compound_literal_p
4721 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4722 /*consume_paren=*/true)
4723 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4724 /* Roll back the tokens we skipped. */
4725 cp_lexer_rollback_tokens (parser->lexer);
4726 /* If we were looking at a compound-literal, simulate an error
4727 so that the call to cp_parser_parse_definitely below will
4728 fail. */
4729 if (compound_literal_p)
4730 cp_parser_simulate_error (parser);
4731 else
4733 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4734 parser->in_type_id_in_expr_p = true;
4735 /* Look for the type-id. */
4736 type = cp_parser_type_id (parser);
4737 /* Look for the closing `)'. */
4738 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4739 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4742 /* Restore the saved message. */
4743 parser->type_definition_forbidden_message = saved_message;
4745 /* If ok so far, parse the dependent expression. We cannot be
4746 sure it is a cast. Consider `(T ())'. It is a parenthesized
4747 ctor of T, but looks like a cast to function returning T
4748 without a dependent expression. */
4749 if (!cp_parser_error_occurred (parser))
4750 expr = cp_parser_simple_cast_expression (parser);
4752 if (cp_parser_parse_definitely (parser))
4754 /* Warn about old-style casts, if so requested. */
4755 if (warn_old_style_cast
4756 && !in_system_header
4757 && !VOID_TYPE_P (type)
4758 && current_lang_name != lang_name_c)
4759 warning ("use of old-style cast");
4761 /* Only type conversions to integral or enumeration types
4762 can be used in constant-expressions. */
4763 if (parser->integral_constant_expression_p
4764 && !dependent_type_p (type)
4765 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4767 if (!parser->allow_non_integral_constant_expression_p)
4768 return (cp_parser_non_integral_constant_expression
4769 ("a casts to a type other than an integral or "
4770 "enumeration type"));
4771 parser->non_integral_constant_expression_p = true;
4773 /* Perform the cast. */
4774 expr = build_c_cast (type, expr);
4775 return expr;
4779 /* If we get here, then it's not a cast, so it must be a
4780 unary-expression. */
4781 return cp_parser_unary_expression (parser, address_p);
4784 /* Parse a pm-expression.
4786 pm-expression:
4787 cast-expression
4788 pm-expression .* cast-expression
4789 pm-expression ->* cast-expression
4791 Returns a representation of the expression. */
4793 static tree
4794 cp_parser_pm_expression (cp_parser* parser)
4796 static const cp_parser_token_tree_map map = {
4797 { CPP_DEREF_STAR, MEMBER_REF },
4798 { CPP_DOT_STAR, DOTSTAR_EXPR },
4799 { CPP_EOF, ERROR_MARK }
4802 return cp_parser_binary_expression (parser, map,
4803 cp_parser_simple_cast_expression);
4806 /* Parse a multiplicative-expression.
4808 mulitplicative-expression:
4809 pm-expression
4810 multiplicative-expression * pm-expression
4811 multiplicative-expression / pm-expression
4812 multiplicative-expression % pm-expression
4814 Returns a representation of the expression. */
4816 static tree
4817 cp_parser_multiplicative_expression (cp_parser* parser)
4819 static const cp_parser_token_tree_map map = {
4820 { CPP_MULT, MULT_EXPR },
4821 { CPP_DIV, TRUNC_DIV_EXPR },
4822 { CPP_MOD, TRUNC_MOD_EXPR },
4823 { CPP_EOF, ERROR_MARK }
4826 return cp_parser_binary_expression (parser,
4827 map,
4828 cp_parser_pm_expression);
4831 /* Parse an additive-expression.
4833 additive-expression:
4834 multiplicative-expression
4835 additive-expression + multiplicative-expression
4836 additive-expression - multiplicative-expression
4838 Returns a representation of the expression. */
4840 static tree
4841 cp_parser_additive_expression (cp_parser* parser)
4843 static const cp_parser_token_tree_map map = {
4844 { CPP_PLUS, PLUS_EXPR },
4845 { CPP_MINUS, MINUS_EXPR },
4846 { CPP_EOF, ERROR_MARK }
4849 return cp_parser_binary_expression (parser,
4850 map,
4851 cp_parser_multiplicative_expression);
4854 /* Parse a shift-expression.
4856 shift-expression:
4857 additive-expression
4858 shift-expression << additive-expression
4859 shift-expression >> additive-expression
4861 Returns a representation of the expression. */
4863 static tree
4864 cp_parser_shift_expression (cp_parser* parser)
4866 static const cp_parser_token_tree_map map = {
4867 { CPP_LSHIFT, LSHIFT_EXPR },
4868 { CPP_RSHIFT, RSHIFT_EXPR },
4869 { CPP_EOF, ERROR_MARK }
4872 return cp_parser_binary_expression (parser,
4873 map,
4874 cp_parser_additive_expression);
4877 /* Parse a relational-expression.
4879 relational-expression:
4880 shift-expression
4881 relational-expression < shift-expression
4882 relational-expression > shift-expression
4883 relational-expression <= shift-expression
4884 relational-expression >= shift-expression
4886 GNU Extension:
4888 relational-expression:
4889 relational-expression <? shift-expression
4890 relational-expression >? shift-expression
4892 Returns a representation of the expression. */
4894 static tree
4895 cp_parser_relational_expression (cp_parser* parser)
4897 static const cp_parser_token_tree_map map = {
4898 { CPP_LESS, LT_EXPR },
4899 { CPP_GREATER, GT_EXPR },
4900 { CPP_LESS_EQ, LE_EXPR },
4901 { CPP_GREATER_EQ, GE_EXPR },
4902 { CPP_MIN, MIN_EXPR },
4903 { CPP_MAX, MAX_EXPR },
4904 { CPP_EOF, ERROR_MARK }
4907 return cp_parser_binary_expression (parser,
4908 map,
4909 cp_parser_shift_expression);
4912 /* Parse an equality-expression.
4914 equality-expression:
4915 relational-expression
4916 equality-expression == relational-expression
4917 equality-expression != relational-expression
4919 Returns a representation of the expression. */
4921 static tree
4922 cp_parser_equality_expression (cp_parser* parser)
4924 static const cp_parser_token_tree_map map = {
4925 { CPP_EQ_EQ, EQ_EXPR },
4926 { CPP_NOT_EQ, NE_EXPR },
4927 { CPP_EOF, ERROR_MARK }
4930 return cp_parser_binary_expression (parser,
4931 map,
4932 cp_parser_relational_expression);
4935 /* Parse an and-expression.
4937 and-expression:
4938 equality-expression
4939 and-expression & equality-expression
4941 Returns a representation of the expression. */
4943 static tree
4944 cp_parser_and_expression (cp_parser* parser)
4946 static const cp_parser_token_tree_map map = {
4947 { CPP_AND, BIT_AND_EXPR },
4948 { CPP_EOF, ERROR_MARK }
4951 return cp_parser_binary_expression (parser,
4952 map,
4953 cp_parser_equality_expression);
4956 /* Parse an exclusive-or-expression.
4958 exclusive-or-expression:
4959 and-expression
4960 exclusive-or-expression ^ and-expression
4962 Returns a representation of the expression. */
4964 static tree
4965 cp_parser_exclusive_or_expression (cp_parser* parser)
4967 static const cp_parser_token_tree_map map = {
4968 { CPP_XOR, BIT_XOR_EXPR },
4969 { CPP_EOF, ERROR_MARK }
4972 return cp_parser_binary_expression (parser,
4973 map,
4974 cp_parser_and_expression);
4978 /* Parse an inclusive-or-expression.
4980 inclusive-or-expression:
4981 exclusive-or-expression
4982 inclusive-or-expression | exclusive-or-expression
4984 Returns a representation of the expression. */
4986 static tree
4987 cp_parser_inclusive_or_expression (cp_parser* parser)
4989 static const cp_parser_token_tree_map map = {
4990 { CPP_OR, BIT_IOR_EXPR },
4991 { CPP_EOF, ERROR_MARK }
4994 return cp_parser_binary_expression (parser,
4995 map,
4996 cp_parser_exclusive_or_expression);
4999 /* Parse a logical-and-expression.
5001 logical-and-expression:
5002 inclusive-or-expression
5003 logical-and-expression && inclusive-or-expression
5005 Returns a representation of the expression. */
5007 static tree
5008 cp_parser_logical_and_expression (cp_parser* parser)
5010 static const cp_parser_token_tree_map map = {
5011 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5012 { CPP_EOF, ERROR_MARK }
5015 return cp_parser_binary_expression (parser,
5016 map,
5017 cp_parser_inclusive_or_expression);
5020 /* Parse a logical-or-expression.
5022 logical-or-expression:
5023 logical-and-expression
5024 logical-or-expression || logical-and-expression
5026 Returns a representation of the expression. */
5028 static tree
5029 cp_parser_logical_or_expression (cp_parser* parser)
5031 static const cp_parser_token_tree_map map = {
5032 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5033 { CPP_EOF, ERROR_MARK }
5036 return cp_parser_binary_expression (parser,
5037 map,
5038 cp_parser_logical_and_expression);
5041 /* Parse the `? expression : assignment-expression' part of a
5042 conditional-expression. The LOGICAL_OR_EXPR is the
5043 logical-or-expression that started the conditional-expression.
5044 Returns a representation of the entire conditional-expression.
5046 This routine is used by cp_parser_assignment_expression.
5048 ? expression : assignment-expression
5050 GNU Extensions:
5052 ? : assignment-expression */
5054 static tree
5055 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5057 tree expr;
5058 tree assignment_expr;
5060 /* Consume the `?' token. */
5061 cp_lexer_consume_token (parser->lexer);
5062 if (cp_parser_allow_gnu_extensions_p (parser)
5063 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5064 /* Implicit true clause. */
5065 expr = NULL_TREE;
5066 else
5067 /* Parse the expression. */
5068 expr = cp_parser_expression (parser);
5070 /* The next token should be a `:'. */
5071 cp_parser_require (parser, CPP_COLON, "`:'");
5072 /* Parse the assignment-expression. */
5073 assignment_expr = cp_parser_assignment_expression (parser);
5075 /* Build the conditional-expression. */
5076 return build_x_conditional_expr (logical_or_expr,
5077 expr,
5078 assignment_expr);
5081 /* Parse an assignment-expression.
5083 assignment-expression:
5084 conditional-expression
5085 logical-or-expression assignment-operator assignment_expression
5086 throw-expression
5088 Returns a representation for the expression. */
5090 static tree
5091 cp_parser_assignment_expression (cp_parser* parser)
5093 tree expr;
5095 /* If the next token is the `throw' keyword, then we're looking at
5096 a throw-expression. */
5097 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5098 expr = cp_parser_throw_expression (parser);
5099 /* Otherwise, it must be that we are looking at a
5100 logical-or-expression. */
5101 else
5103 /* Parse the logical-or-expression. */
5104 expr = cp_parser_logical_or_expression (parser);
5105 /* If the next token is a `?' then we're actually looking at a
5106 conditional-expression. */
5107 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5108 return cp_parser_question_colon_clause (parser, expr);
5109 else
5111 enum tree_code assignment_operator;
5113 /* If it's an assignment-operator, we're using the second
5114 production. */
5115 assignment_operator
5116 = cp_parser_assignment_operator_opt (parser);
5117 if (assignment_operator != ERROR_MARK)
5119 tree rhs;
5121 /* Parse the right-hand side of the assignment. */
5122 rhs = cp_parser_assignment_expression (parser);
5123 /* An assignment may not appear in a
5124 constant-expression. */
5125 if (parser->integral_constant_expression_p)
5127 if (!parser->allow_non_integral_constant_expression_p)
5128 return cp_parser_non_integral_constant_expression ("an assignment");
5129 parser->non_integral_constant_expression_p = true;
5131 /* Build the assignment expression. */
5132 expr = build_x_modify_expr (expr,
5133 assignment_operator,
5134 rhs);
5139 return expr;
5142 /* Parse an (optional) assignment-operator.
5144 assignment-operator: one of
5145 = *= /= %= += -= >>= <<= &= ^= |=
5147 GNU Extension:
5149 assignment-operator: one of
5150 <?= >?=
5152 If the next token is an assignment operator, the corresponding tree
5153 code is returned, and the token is consumed. For example, for
5154 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5155 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5156 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5157 operator, ERROR_MARK is returned. */
5159 static enum tree_code
5160 cp_parser_assignment_operator_opt (cp_parser* parser)
5162 enum tree_code op;
5163 cp_token *token;
5165 /* Peek at the next toen. */
5166 token = cp_lexer_peek_token (parser->lexer);
5168 switch (token->type)
5170 case CPP_EQ:
5171 op = NOP_EXPR;
5172 break;
5174 case CPP_MULT_EQ:
5175 op = MULT_EXPR;
5176 break;
5178 case CPP_DIV_EQ:
5179 op = TRUNC_DIV_EXPR;
5180 break;
5182 case CPP_MOD_EQ:
5183 op = TRUNC_MOD_EXPR;
5184 break;
5186 case CPP_PLUS_EQ:
5187 op = PLUS_EXPR;
5188 break;
5190 case CPP_MINUS_EQ:
5191 op = MINUS_EXPR;
5192 break;
5194 case CPP_RSHIFT_EQ:
5195 op = RSHIFT_EXPR;
5196 break;
5198 case CPP_LSHIFT_EQ:
5199 op = LSHIFT_EXPR;
5200 break;
5202 case CPP_AND_EQ:
5203 op = BIT_AND_EXPR;
5204 break;
5206 case CPP_XOR_EQ:
5207 op = BIT_XOR_EXPR;
5208 break;
5210 case CPP_OR_EQ:
5211 op = BIT_IOR_EXPR;
5212 break;
5214 case CPP_MIN_EQ:
5215 op = MIN_EXPR;
5216 break;
5218 case CPP_MAX_EQ:
5219 op = MAX_EXPR;
5220 break;
5222 default:
5223 /* Nothing else is an assignment operator. */
5224 op = ERROR_MARK;
5227 /* If it was an assignment operator, consume it. */
5228 if (op != ERROR_MARK)
5229 cp_lexer_consume_token (parser->lexer);
5231 return op;
5234 /* Parse an expression.
5236 expression:
5237 assignment-expression
5238 expression , assignment-expression
5240 Returns a representation of the expression. */
5242 static tree
5243 cp_parser_expression (cp_parser* parser)
5245 tree expression = NULL_TREE;
5247 while (true)
5249 tree assignment_expression;
5251 /* Parse the next assignment-expression. */
5252 assignment_expression
5253 = cp_parser_assignment_expression (parser);
5254 /* If this is the first assignment-expression, we can just
5255 save it away. */
5256 if (!expression)
5257 expression = assignment_expression;
5258 else
5259 expression = build_x_compound_expr (expression,
5260 assignment_expression);
5261 /* If the next token is not a comma, then we are done with the
5262 expression. */
5263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5264 break;
5265 /* Consume the `,'. */
5266 cp_lexer_consume_token (parser->lexer);
5267 /* A comma operator cannot appear in a constant-expression. */
5268 if (parser->integral_constant_expression_p)
5270 if (!parser->allow_non_integral_constant_expression_p)
5271 expression
5272 = cp_parser_non_integral_constant_expression ("a comma operator");
5273 parser->non_integral_constant_expression_p = true;
5277 return expression;
5280 /* Parse a constant-expression.
5282 constant-expression:
5283 conditional-expression
5285 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5286 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5287 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5288 is false, NON_CONSTANT_P should be NULL. */
5290 static tree
5291 cp_parser_constant_expression (cp_parser* parser,
5292 bool allow_non_constant_p,
5293 bool *non_constant_p)
5295 bool saved_integral_constant_expression_p;
5296 bool saved_allow_non_integral_constant_expression_p;
5297 bool saved_non_integral_constant_expression_p;
5298 tree expression;
5300 /* It might seem that we could simply parse the
5301 conditional-expression, and then check to see if it were
5302 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5303 one that the compiler can figure out is constant, possibly after
5304 doing some simplifications or optimizations. The standard has a
5305 precise definition of constant-expression, and we must honor
5306 that, even though it is somewhat more restrictive.
5308 For example:
5310 int i[(2, 3)];
5312 is not a legal declaration, because `(2, 3)' is not a
5313 constant-expression. The `,' operator is forbidden in a
5314 constant-expression. However, GCC's constant-folding machinery
5315 will fold this operation to an INTEGER_CST for `3'. */
5317 /* Save the old settings. */
5318 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5319 saved_allow_non_integral_constant_expression_p
5320 = parser->allow_non_integral_constant_expression_p;
5321 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5322 /* We are now parsing a constant-expression. */
5323 parser->integral_constant_expression_p = true;
5324 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5325 parser->non_integral_constant_expression_p = false;
5326 /* Although the grammar says "conditional-expression", we parse an
5327 "assignment-expression", which also permits "throw-expression"
5328 and the use of assignment operators. In the case that
5329 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5330 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5331 actually essential that we look for an assignment-expression.
5332 For example, cp_parser_initializer_clauses uses this function to
5333 determine whether a particular assignment-expression is in fact
5334 constant. */
5335 expression = cp_parser_assignment_expression (parser);
5336 /* Restore the old settings. */
5337 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5338 parser->allow_non_integral_constant_expression_p
5339 = saved_allow_non_integral_constant_expression_p;
5340 if (allow_non_constant_p)
5341 *non_constant_p = parser->non_integral_constant_expression_p;
5342 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5344 return expression;
5347 /* Statements [gram.stmt.stmt] */
5349 /* Parse a statement.
5351 statement:
5352 labeled-statement
5353 expression-statement
5354 compound-statement
5355 selection-statement
5356 iteration-statement
5357 jump-statement
5358 declaration-statement
5359 try-block */
5361 static void
5362 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5364 tree statement;
5365 cp_token *token;
5366 int statement_line_number;
5368 /* There is no statement yet. */
5369 statement = NULL_TREE;
5370 /* Peek at the next token. */
5371 token = cp_lexer_peek_token (parser->lexer);
5372 /* Remember the line number of the first token in the statement. */
5373 statement_line_number = token->location.line;
5374 /* If this is a keyword, then that will often determine what kind of
5375 statement we have. */
5376 if (token->type == CPP_KEYWORD)
5378 enum rid keyword = token->keyword;
5380 switch (keyword)
5382 case RID_CASE:
5383 case RID_DEFAULT:
5384 statement = cp_parser_labeled_statement (parser,
5385 in_statement_expr_p);
5386 break;
5388 case RID_IF:
5389 case RID_SWITCH:
5390 statement = cp_parser_selection_statement (parser);
5391 break;
5393 case RID_WHILE:
5394 case RID_DO:
5395 case RID_FOR:
5396 statement = cp_parser_iteration_statement (parser);
5397 break;
5399 case RID_BREAK:
5400 case RID_CONTINUE:
5401 case RID_RETURN:
5402 case RID_GOTO:
5403 statement = cp_parser_jump_statement (parser);
5404 break;
5406 case RID_TRY:
5407 statement = cp_parser_try_block (parser);
5408 break;
5410 default:
5411 /* It might be a keyword like `int' that can start a
5412 declaration-statement. */
5413 break;
5416 else if (token->type == CPP_NAME)
5418 /* If the next token is a `:', then we are looking at a
5419 labeled-statement. */
5420 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5421 if (token->type == CPP_COLON)
5422 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5424 /* Anything that starts with a `{' must be a compound-statement. */
5425 else if (token->type == CPP_OPEN_BRACE)
5426 statement = cp_parser_compound_statement (parser, false);
5428 /* Everything else must be a declaration-statement or an
5429 expression-statement. Try for the declaration-statement
5430 first, unless we are looking at a `;', in which case we know that
5431 we have an expression-statement. */
5432 if (!statement)
5434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5436 cp_parser_parse_tentatively (parser);
5437 /* Try to parse the declaration-statement. */
5438 cp_parser_declaration_statement (parser);
5439 /* If that worked, we're done. */
5440 if (cp_parser_parse_definitely (parser))
5441 return;
5443 /* Look for an expression-statement instead. */
5444 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5447 /* Set the line number for the statement. */
5448 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5449 STMT_LINENO (statement) = statement_line_number;
5452 /* Parse a labeled-statement.
5454 labeled-statement:
5455 identifier : statement
5456 case constant-expression : statement
5457 default : statement
5459 Returns the new CASE_LABEL, for a `case' or `default' label. For
5460 an ordinary label, returns a LABEL_STMT. */
5462 static tree
5463 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5465 cp_token *token;
5466 tree statement = error_mark_node;
5468 /* The next token should be an identifier. */
5469 token = cp_lexer_peek_token (parser->lexer);
5470 if (token->type != CPP_NAME
5471 && token->type != CPP_KEYWORD)
5473 cp_parser_error (parser, "expected labeled-statement");
5474 return error_mark_node;
5477 switch (token->keyword)
5479 case RID_CASE:
5481 tree expr;
5483 /* Consume the `case' token. */
5484 cp_lexer_consume_token (parser->lexer);
5485 /* Parse the constant-expression. */
5486 expr = cp_parser_constant_expression (parser,
5487 /*allow_non_constant_p=*/false,
5488 NULL);
5489 if (!parser->in_switch_statement_p)
5490 error ("case label `%E' not within a switch statement", expr);
5491 else
5492 statement = finish_case_label (expr, NULL_TREE);
5494 break;
5496 case RID_DEFAULT:
5497 /* Consume the `default' token. */
5498 cp_lexer_consume_token (parser->lexer);
5499 if (!parser->in_switch_statement_p)
5500 error ("case label not within a switch statement");
5501 else
5502 statement = finish_case_label (NULL_TREE, NULL_TREE);
5503 break;
5505 default:
5506 /* Anything else must be an ordinary label. */
5507 statement = finish_label_stmt (cp_parser_identifier (parser));
5508 break;
5511 /* Require the `:' token. */
5512 cp_parser_require (parser, CPP_COLON, "`:'");
5513 /* Parse the labeled statement. */
5514 cp_parser_statement (parser, in_statement_expr_p);
5516 /* Return the label, in the case of a `case' or `default' label. */
5517 return statement;
5520 /* Parse an expression-statement.
5522 expression-statement:
5523 expression [opt] ;
5525 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5526 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5527 indicates whether this expression-statement is part of an
5528 expression statement. */
5530 static tree
5531 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5533 tree statement = NULL_TREE;
5535 /* If the next token is a ';', then there is no expression
5536 statement. */
5537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5538 statement = cp_parser_expression (parser);
5540 /* Consume the final `;'. */
5541 cp_parser_consume_semicolon_at_end_of_statement (parser);
5543 if (in_statement_expr_p
5544 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5546 /* This is the final expression statement of a statement
5547 expression. */
5548 statement = finish_stmt_expr_expr (statement);
5550 else if (statement)
5551 statement = finish_expr_stmt (statement);
5552 else
5553 finish_stmt ();
5555 return statement;
5558 /* Parse a compound-statement.
5560 compound-statement:
5561 { statement-seq [opt] }
5563 Returns a COMPOUND_STMT representing the statement. */
5565 static tree
5566 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5568 tree compound_stmt;
5570 /* Consume the `{'. */
5571 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5572 return error_mark_node;
5573 /* Begin the compound-statement. */
5574 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5575 /* Parse an (optional) statement-seq. */
5576 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5577 /* Finish the compound-statement. */
5578 finish_compound_stmt (compound_stmt);
5579 /* Consume the `}'. */
5580 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5582 return compound_stmt;
5585 /* Parse an (optional) statement-seq.
5587 statement-seq:
5588 statement
5589 statement-seq [opt] statement */
5591 static void
5592 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5594 /* Scan statements until there aren't any more. */
5595 while (true)
5597 /* If we're looking at a `}', then we've run out of statements. */
5598 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5599 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5600 break;
5602 /* Parse the statement. */
5603 cp_parser_statement (parser, in_statement_expr_p);
5607 /* Parse a selection-statement.
5609 selection-statement:
5610 if ( condition ) statement
5611 if ( condition ) statement else statement
5612 switch ( condition ) statement
5614 Returns the new IF_STMT or SWITCH_STMT. */
5616 static tree
5617 cp_parser_selection_statement (cp_parser* parser)
5619 cp_token *token;
5620 enum rid keyword;
5622 /* Peek at the next token. */
5623 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5625 /* See what kind of keyword it is. */
5626 keyword = token->keyword;
5627 switch (keyword)
5629 case RID_IF:
5630 case RID_SWITCH:
5632 tree statement;
5633 tree condition;
5635 /* Look for the `('. */
5636 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5638 cp_parser_skip_to_end_of_statement (parser);
5639 return error_mark_node;
5642 /* Begin the selection-statement. */
5643 if (keyword == RID_IF)
5644 statement = begin_if_stmt ();
5645 else
5646 statement = begin_switch_stmt ();
5648 /* Parse the condition. */
5649 condition = cp_parser_condition (parser);
5650 /* Look for the `)'. */
5651 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5652 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5653 /*consume_paren=*/true);
5655 if (keyword == RID_IF)
5657 tree then_stmt;
5659 /* Add the condition. */
5660 finish_if_stmt_cond (condition, statement);
5662 /* Parse the then-clause. */
5663 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5664 finish_then_clause (statement);
5666 /* If the next token is `else', parse the else-clause. */
5667 if (cp_lexer_next_token_is_keyword (parser->lexer,
5668 RID_ELSE))
5670 tree else_stmt;
5672 /* Consume the `else' keyword. */
5673 cp_lexer_consume_token (parser->lexer);
5674 /* Parse the else-clause. */
5675 else_stmt
5676 = cp_parser_implicitly_scoped_statement (parser);
5677 finish_else_clause (statement);
5680 /* Now we're all done with the if-statement. */
5681 finish_if_stmt ();
5683 else
5685 tree body;
5686 bool in_switch_statement_p;
5688 /* Add the condition. */
5689 finish_switch_cond (condition, statement);
5691 /* Parse the body of the switch-statement. */
5692 in_switch_statement_p = parser->in_switch_statement_p;
5693 parser->in_switch_statement_p = true;
5694 body = cp_parser_implicitly_scoped_statement (parser);
5695 parser->in_switch_statement_p = in_switch_statement_p;
5697 /* Now we're all done with the switch-statement. */
5698 finish_switch_stmt (statement);
5701 return statement;
5703 break;
5705 default:
5706 cp_parser_error (parser, "expected selection-statement");
5707 return error_mark_node;
5711 /* Parse a condition.
5713 condition:
5714 expression
5715 type-specifier-seq declarator = assignment-expression
5717 GNU Extension:
5719 condition:
5720 type-specifier-seq declarator asm-specification [opt]
5721 attributes [opt] = assignment-expression
5723 Returns the expression that should be tested. */
5725 static tree
5726 cp_parser_condition (cp_parser* parser)
5728 tree type_specifiers;
5729 const char *saved_message;
5731 /* Try the declaration first. */
5732 cp_parser_parse_tentatively (parser);
5733 /* New types are not allowed in the type-specifier-seq for a
5734 condition. */
5735 saved_message = parser->type_definition_forbidden_message;
5736 parser->type_definition_forbidden_message
5737 = "types may not be defined in conditions";
5738 /* Parse the type-specifier-seq. */
5739 type_specifiers = cp_parser_type_specifier_seq (parser);
5740 /* Restore the saved message. */
5741 parser->type_definition_forbidden_message = saved_message;
5742 /* If all is well, we might be looking at a declaration. */
5743 if (!cp_parser_error_occurred (parser))
5745 tree decl;
5746 tree asm_specification;
5747 tree attributes;
5748 tree declarator;
5749 tree initializer = NULL_TREE;
5751 /* Parse the declarator. */
5752 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5753 /*ctor_dtor_or_conv_p=*/NULL,
5754 /*parenthesized_p=*/NULL);
5755 /* Parse the attributes. */
5756 attributes = cp_parser_attributes_opt (parser);
5757 /* Parse the asm-specification. */
5758 asm_specification = cp_parser_asm_specification_opt (parser);
5759 /* If the next token is not an `=', then we might still be
5760 looking at an expression. For example:
5762 if (A(a).x)
5764 looks like a decl-specifier-seq and a declarator -- but then
5765 there is no `=', so this is an expression. */
5766 cp_parser_require (parser, CPP_EQ, "`='");
5767 /* If we did see an `=', then we are looking at a declaration
5768 for sure. */
5769 if (cp_parser_parse_definitely (parser))
5771 /* Create the declaration. */
5772 decl = start_decl (declarator, type_specifiers,
5773 /*initialized_p=*/true,
5774 attributes, /*prefix_attributes=*/NULL_TREE);
5775 /* Parse the assignment-expression. */
5776 initializer = cp_parser_assignment_expression (parser);
5778 /* Process the initializer. */
5779 cp_finish_decl (decl,
5780 initializer,
5781 asm_specification,
5782 LOOKUP_ONLYCONVERTING);
5784 return convert_from_reference (decl);
5787 /* If we didn't even get past the declarator successfully, we are
5788 definitely not looking at a declaration. */
5789 else
5790 cp_parser_abort_tentative_parse (parser);
5792 /* Otherwise, we are looking at an expression. */
5793 return cp_parser_expression (parser);
5796 /* Parse an iteration-statement.
5798 iteration-statement:
5799 while ( condition ) statement
5800 do statement while ( expression ) ;
5801 for ( for-init-statement condition [opt] ; expression [opt] )
5802 statement
5804 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5806 static tree
5807 cp_parser_iteration_statement (cp_parser* parser)
5809 cp_token *token;
5810 enum rid keyword;
5811 tree statement;
5812 bool in_iteration_statement_p;
5815 /* Peek at the next token. */
5816 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5817 if (!token)
5818 return error_mark_node;
5820 /* Remember whether or not we are already within an iteration
5821 statement. */
5822 in_iteration_statement_p = parser->in_iteration_statement_p;
5824 /* See what kind of keyword it is. */
5825 keyword = token->keyword;
5826 switch (keyword)
5828 case RID_WHILE:
5830 tree condition;
5832 /* Begin the while-statement. */
5833 statement = begin_while_stmt ();
5834 /* Look for the `('. */
5835 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5836 /* Parse the condition. */
5837 condition = cp_parser_condition (parser);
5838 finish_while_stmt_cond (condition, statement);
5839 /* Look for the `)'. */
5840 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5841 /* Parse the dependent statement. */
5842 parser->in_iteration_statement_p = true;
5843 cp_parser_already_scoped_statement (parser);
5844 parser->in_iteration_statement_p = in_iteration_statement_p;
5845 /* We're done with the while-statement. */
5846 finish_while_stmt (statement);
5848 break;
5850 case RID_DO:
5852 tree expression;
5854 /* Begin the do-statement. */
5855 statement = begin_do_stmt ();
5856 /* Parse the body of the do-statement. */
5857 parser->in_iteration_statement_p = true;
5858 cp_parser_implicitly_scoped_statement (parser);
5859 parser->in_iteration_statement_p = in_iteration_statement_p;
5860 finish_do_body (statement);
5861 /* Look for the `while' keyword. */
5862 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5863 /* Look for the `('. */
5864 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5865 /* Parse the expression. */
5866 expression = cp_parser_expression (parser);
5867 /* We're done with the do-statement. */
5868 finish_do_stmt (expression, statement);
5869 /* Look for the `)'. */
5870 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5871 /* Look for the `;'. */
5872 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5874 break;
5876 case RID_FOR:
5878 tree condition = NULL_TREE;
5879 tree expression = NULL_TREE;
5881 /* Begin the for-statement. */
5882 statement = begin_for_stmt ();
5883 /* Look for the `('. */
5884 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5885 /* Parse the initialization. */
5886 cp_parser_for_init_statement (parser);
5887 finish_for_init_stmt (statement);
5889 /* If there's a condition, process it. */
5890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5891 condition = cp_parser_condition (parser);
5892 finish_for_cond (condition, statement);
5893 /* Look for the `;'. */
5894 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5896 /* If there's an expression, process it. */
5897 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5898 expression = cp_parser_expression (parser);
5899 finish_for_expr (expression, statement);
5900 /* Look for the `)'. */
5901 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5903 /* Parse the body of the for-statement. */
5904 parser->in_iteration_statement_p = true;
5905 cp_parser_already_scoped_statement (parser);
5906 parser->in_iteration_statement_p = in_iteration_statement_p;
5908 /* We're done with the for-statement. */
5909 finish_for_stmt (statement);
5911 break;
5913 default:
5914 cp_parser_error (parser, "expected iteration-statement");
5915 statement = error_mark_node;
5916 break;
5919 return statement;
5922 /* Parse a for-init-statement.
5924 for-init-statement:
5925 expression-statement
5926 simple-declaration */
5928 static void
5929 cp_parser_for_init_statement (cp_parser* parser)
5931 /* If the next token is a `;', then we have an empty
5932 expression-statement. Grammatically, this is also a
5933 simple-declaration, but an invalid one, because it does not
5934 declare anything. Therefore, if we did not handle this case
5935 specially, we would issue an error message about an invalid
5936 declaration. */
5937 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5939 /* We're going to speculatively look for a declaration, falling back
5940 to an expression, if necessary. */
5941 cp_parser_parse_tentatively (parser);
5942 /* Parse the declaration. */
5943 cp_parser_simple_declaration (parser,
5944 /*function_definition_allowed_p=*/false);
5945 /* If the tentative parse failed, then we shall need to look for an
5946 expression-statement. */
5947 if (cp_parser_parse_definitely (parser))
5948 return;
5951 cp_parser_expression_statement (parser, false);
5954 /* Parse a jump-statement.
5956 jump-statement:
5957 break ;
5958 continue ;
5959 return expression [opt] ;
5960 goto identifier ;
5962 GNU extension:
5964 jump-statement:
5965 goto * expression ;
5967 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5968 GOTO_STMT. */
5970 static tree
5971 cp_parser_jump_statement (cp_parser* parser)
5973 tree statement = error_mark_node;
5974 cp_token *token;
5975 enum rid keyword;
5977 /* Peek at the next token. */
5978 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5979 if (!token)
5980 return error_mark_node;
5982 /* See what kind of keyword it is. */
5983 keyword = token->keyword;
5984 switch (keyword)
5986 case RID_BREAK:
5987 if (!parser->in_switch_statement_p
5988 && !parser->in_iteration_statement_p)
5990 error ("break statement not within loop or switch");
5991 statement = error_mark_node;
5993 else
5994 statement = finish_break_stmt ();
5995 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5996 break;
5998 case RID_CONTINUE:
5999 if (!parser->in_iteration_statement_p)
6001 error ("continue statement not within a loop");
6002 statement = error_mark_node;
6004 else
6005 statement = finish_continue_stmt ();
6006 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6007 break;
6009 case RID_RETURN:
6011 tree expr;
6013 /* If the next token is a `;', then there is no
6014 expression. */
6015 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6016 expr = cp_parser_expression (parser);
6017 else
6018 expr = NULL_TREE;
6019 /* Build the return-statement. */
6020 statement = finish_return_stmt (expr);
6021 /* Look for the final `;'. */
6022 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6024 break;
6026 case RID_GOTO:
6027 /* Create the goto-statement. */
6028 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6030 /* Issue a warning about this use of a GNU extension. */
6031 if (pedantic)
6032 pedwarn ("ISO C++ forbids computed gotos");
6033 /* Consume the '*' token. */
6034 cp_lexer_consume_token (parser->lexer);
6035 /* Parse the dependent expression. */
6036 finish_goto_stmt (cp_parser_expression (parser));
6038 else
6039 finish_goto_stmt (cp_parser_identifier (parser));
6040 /* Look for the final `;'. */
6041 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6042 break;
6044 default:
6045 cp_parser_error (parser, "expected jump-statement");
6046 break;
6049 return statement;
6052 /* Parse a declaration-statement.
6054 declaration-statement:
6055 block-declaration */
6057 static void
6058 cp_parser_declaration_statement (cp_parser* parser)
6060 /* Parse the block-declaration. */
6061 cp_parser_block_declaration (parser, /*statement_p=*/true);
6063 /* Finish off the statement. */
6064 finish_stmt ();
6067 /* Some dependent statements (like `if (cond) statement'), are
6068 implicitly in their own scope. In other words, if the statement is
6069 a single statement (as opposed to a compound-statement), it is
6070 none-the-less treated as if it were enclosed in braces. Any
6071 declarations appearing in the dependent statement are out of scope
6072 after control passes that point. This function parses a statement,
6073 but ensures that is in its own scope, even if it is not a
6074 compound-statement.
6076 Returns the new statement. */
6078 static tree
6079 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6081 tree statement;
6083 /* If the token is not a `{', then we must take special action. */
6084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6086 /* Create a compound-statement. */
6087 statement = begin_compound_stmt (/*has_no_scope=*/false);
6088 /* Parse the dependent-statement. */
6089 cp_parser_statement (parser, false);
6090 /* Finish the dummy compound-statement. */
6091 finish_compound_stmt (statement);
6093 /* Otherwise, we simply parse the statement directly. */
6094 else
6095 statement = cp_parser_compound_statement (parser, false);
6097 /* Return the statement. */
6098 return statement;
6101 /* For some dependent statements (like `while (cond) statement'), we
6102 have already created a scope. Therefore, even if the dependent
6103 statement is a compound-statement, we do not want to create another
6104 scope. */
6106 static void
6107 cp_parser_already_scoped_statement (cp_parser* parser)
6109 /* If the token is not a `{', then we must take special action. */
6110 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6112 tree statement;
6114 /* Create a compound-statement. */
6115 statement = begin_compound_stmt (/*has_no_scope=*/true);
6116 /* Parse the dependent-statement. */
6117 cp_parser_statement (parser, false);
6118 /* Finish the dummy compound-statement. */
6119 finish_compound_stmt (statement);
6121 /* Otherwise, we simply parse the statement directly. */
6122 else
6123 cp_parser_statement (parser, false);
6126 /* Declarations [gram.dcl.dcl] */
6128 /* Parse an optional declaration-sequence.
6130 declaration-seq:
6131 declaration
6132 declaration-seq declaration */
6134 static void
6135 cp_parser_declaration_seq_opt (cp_parser* parser)
6137 while (true)
6139 cp_token *token;
6141 token = cp_lexer_peek_token (parser->lexer);
6143 if (token->type == CPP_CLOSE_BRACE
6144 || token->type == CPP_EOF)
6145 break;
6147 if (token->type == CPP_SEMICOLON)
6149 /* A declaration consisting of a single semicolon is
6150 invalid. Allow it unless we're being pedantic. */
6151 if (pedantic && !in_system_header)
6152 pedwarn ("extra `;'");
6153 cp_lexer_consume_token (parser->lexer);
6154 continue;
6157 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6158 parser to enter or exit implicit `extern "C"' blocks. */
6159 while (pending_lang_change > 0)
6161 push_lang_context (lang_name_c);
6162 --pending_lang_change;
6164 while (pending_lang_change < 0)
6166 pop_lang_context ();
6167 ++pending_lang_change;
6170 /* Parse the declaration itself. */
6171 cp_parser_declaration (parser);
6175 /* Parse a declaration.
6177 declaration:
6178 block-declaration
6179 function-definition
6180 template-declaration
6181 explicit-instantiation
6182 explicit-specialization
6183 linkage-specification
6184 namespace-definition
6186 GNU extension:
6188 declaration:
6189 __extension__ declaration */
6191 static void
6192 cp_parser_declaration (cp_parser* parser)
6194 cp_token token1;
6195 cp_token token2;
6196 int saved_pedantic;
6198 /* Check for the `__extension__' keyword. */
6199 if (cp_parser_extension_opt (parser, &saved_pedantic))
6201 /* Parse the qualified declaration. */
6202 cp_parser_declaration (parser);
6203 /* Restore the PEDANTIC flag. */
6204 pedantic = saved_pedantic;
6206 return;
6209 /* Try to figure out what kind of declaration is present. */
6210 token1 = *cp_lexer_peek_token (parser->lexer);
6211 if (token1.type != CPP_EOF)
6212 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6214 /* If the next token is `extern' and the following token is a string
6215 literal, then we have a linkage specification. */
6216 if (token1.keyword == RID_EXTERN
6217 && cp_parser_is_string_literal (&token2))
6218 cp_parser_linkage_specification (parser);
6219 /* If the next token is `template', then we have either a template
6220 declaration, an explicit instantiation, or an explicit
6221 specialization. */
6222 else if (token1.keyword == RID_TEMPLATE)
6224 /* `template <>' indicates a template specialization. */
6225 if (token2.type == CPP_LESS
6226 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6227 cp_parser_explicit_specialization (parser);
6228 /* `template <' indicates a template declaration. */
6229 else if (token2.type == CPP_LESS)
6230 cp_parser_template_declaration (parser, /*member_p=*/false);
6231 /* Anything else must be an explicit instantiation. */
6232 else
6233 cp_parser_explicit_instantiation (parser);
6235 /* If the next token is `export', then we have a template
6236 declaration. */
6237 else if (token1.keyword == RID_EXPORT)
6238 cp_parser_template_declaration (parser, /*member_p=*/false);
6239 /* If the next token is `extern', 'static' or 'inline' and the one
6240 after that is `template', we have a GNU extended explicit
6241 instantiation directive. */
6242 else if (cp_parser_allow_gnu_extensions_p (parser)
6243 && (token1.keyword == RID_EXTERN
6244 || token1.keyword == RID_STATIC
6245 || token1.keyword == RID_INLINE)
6246 && token2.keyword == RID_TEMPLATE)
6247 cp_parser_explicit_instantiation (parser);
6248 /* If the next token is `namespace', check for a named or unnamed
6249 namespace definition. */
6250 else if (token1.keyword == RID_NAMESPACE
6251 && (/* A named namespace definition. */
6252 (token2.type == CPP_NAME
6253 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6254 == CPP_OPEN_BRACE))
6255 /* An unnamed namespace definition. */
6256 || token2.type == CPP_OPEN_BRACE))
6257 cp_parser_namespace_definition (parser);
6258 /* We must have either a block declaration or a function
6259 definition. */
6260 else
6261 /* Try to parse a block-declaration, or a function-definition. */
6262 cp_parser_block_declaration (parser, /*statement_p=*/false);
6265 /* Parse a block-declaration.
6267 block-declaration:
6268 simple-declaration
6269 asm-definition
6270 namespace-alias-definition
6271 using-declaration
6272 using-directive
6274 GNU Extension:
6276 block-declaration:
6277 __extension__ block-declaration
6278 label-declaration
6280 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6281 part of a declaration-statement. */
6283 static void
6284 cp_parser_block_declaration (cp_parser *parser,
6285 bool statement_p)
6287 cp_token *token1;
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_block_declaration (parser, statement_p);
6295 /* Restore the PEDANTIC flag. */
6296 pedantic = saved_pedantic;
6298 return;
6301 /* Peek at the next token to figure out which kind of declaration is
6302 present. */
6303 token1 = cp_lexer_peek_token (parser->lexer);
6305 /* If the next keyword is `asm', we have an asm-definition. */
6306 if (token1->keyword == RID_ASM)
6308 if (statement_p)
6309 cp_parser_commit_to_tentative_parse (parser);
6310 cp_parser_asm_definition (parser);
6312 /* If the next keyword is `namespace', we have a
6313 namespace-alias-definition. */
6314 else if (token1->keyword == RID_NAMESPACE)
6315 cp_parser_namespace_alias_definition (parser);
6316 /* If the next keyword is `using', we have either a
6317 using-declaration or a using-directive. */
6318 else if (token1->keyword == RID_USING)
6320 cp_token *token2;
6322 if (statement_p)
6323 cp_parser_commit_to_tentative_parse (parser);
6324 /* If the token after `using' is `namespace', then we have a
6325 using-directive. */
6326 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6327 if (token2->keyword == RID_NAMESPACE)
6328 cp_parser_using_directive (parser);
6329 /* Otherwise, it's a using-declaration. */
6330 else
6331 cp_parser_using_declaration (parser);
6333 /* If the next keyword is `__label__' we have a label declaration. */
6334 else if (token1->keyword == RID_LABEL)
6336 if (statement_p)
6337 cp_parser_commit_to_tentative_parse (parser);
6338 cp_parser_label_declaration (parser);
6340 /* Anything else must be a simple-declaration. */
6341 else
6342 cp_parser_simple_declaration (parser, !statement_p);
6345 /* Parse a simple-declaration.
6347 simple-declaration:
6348 decl-specifier-seq [opt] init-declarator-list [opt] ;
6350 init-declarator-list:
6351 init-declarator
6352 init-declarator-list , init-declarator
6354 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6355 function-definition as a simple-declaration. */
6357 static void
6358 cp_parser_simple_declaration (cp_parser* parser,
6359 bool function_definition_allowed_p)
6361 tree decl_specifiers;
6362 tree attributes;
6363 int declares_class_or_enum;
6364 bool saw_declarator;
6366 /* Defer access checks until we know what is being declared; the
6367 checks for names appearing in the decl-specifier-seq should be
6368 done as if we were in the scope of the thing being declared. */
6369 push_deferring_access_checks (dk_deferred);
6371 /* Parse the decl-specifier-seq. We have to keep track of whether
6372 or not the decl-specifier-seq declares a named class or
6373 enumeration type, since that is the only case in which the
6374 init-declarator-list is allowed to be empty.
6376 [dcl.dcl]
6378 In a simple-declaration, the optional init-declarator-list can be
6379 omitted only when declaring a class or enumeration, that is when
6380 the decl-specifier-seq contains either a class-specifier, an
6381 elaborated-type-specifier, or an enum-specifier. */
6382 decl_specifiers
6383 = cp_parser_decl_specifier_seq (parser,
6384 CP_PARSER_FLAGS_OPTIONAL,
6385 &attributes,
6386 &declares_class_or_enum);
6387 /* We no longer need to defer access checks. */
6388 stop_deferring_access_checks ();
6390 /* In a block scope, a valid declaration must always have a
6391 decl-specifier-seq. By not trying to parse declarators, we can
6392 resolve the declaration/expression ambiguity more quickly. */
6393 if (!function_definition_allowed_p && !decl_specifiers)
6395 cp_parser_error (parser, "expected declaration");
6396 goto done;
6399 /* If the next two tokens are both identifiers, the code is
6400 erroneous. The usual cause of this situation is code like:
6402 T t;
6404 where "T" should name a type -- but does not. */
6405 if (cp_parser_diagnose_invalid_type_name (parser))
6407 /* If parsing tentatively, we should commit; we really are
6408 looking at a declaration. */
6409 cp_parser_commit_to_tentative_parse (parser);
6410 /* Give up. */
6411 goto done;
6414 /* Keep going until we hit the `;' at the end of the simple
6415 declaration. */
6416 saw_declarator = false;
6417 while (cp_lexer_next_token_is_not (parser->lexer,
6418 CPP_SEMICOLON))
6420 cp_token *token;
6421 bool function_definition_p;
6422 tree decl;
6424 saw_declarator = true;
6425 /* Parse the init-declarator. */
6426 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6427 function_definition_allowed_p,
6428 /*member_p=*/false,
6429 declares_class_or_enum,
6430 &function_definition_p);
6431 /* If an error occurred while parsing tentatively, exit quickly.
6432 (That usually happens when in the body of a function; each
6433 statement is treated as a declaration-statement until proven
6434 otherwise.) */
6435 if (cp_parser_error_occurred (parser))
6436 goto done;
6437 /* Handle function definitions specially. */
6438 if (function_definition_p)
6440 /* If the next token is a `,', then we are probably
6441 processing something like:
6443 void f() {}, *p;
6445 which is erroneous. */
6446 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6447 error ("mixing declarations and function-definitions is forbidden");
6448 /* Otherwise, we're done with the list of declarators. */
6449 else
6451 pop_deferring_access_checks ();
6452 return;
6455 /* The next token should be either a `,' or a `;'. */
6456 token = cp_lexer_peek_token (parser->lexer);
6457 /* If it's a `,', there are more declarators to come. */
6458 if (token->type == CPP_COMMA)
6459 cp_lexer_consume_token (parser->lexer);
6460 /* If it's a `;', we are done. */
6461 else if (token->type == CPP_SEMICOLON)
6462 break;
6463 /* Anything else is an error. */
6464 else
6466 cp_parser_error (parser, "expected `,' or `;'");
6467 /* Skip tokens until we reach the end of the statement. */
6468 cp_parser_skip_to_end_of_statement (parser);
6469 goto done;
6471 /* After the first time around, a function-definition is not
6472 allowed -- even if it was OK at first. For example:
6474 int i, f() {}
6476 is not valid. */
6477 function_definition_allowed_p = false;
6480 /* Issue an error message if no declarators are present, and the
6481 decl-specifier-seq does not itself declare a class or
6482 enumeration. */
6483 if (!saw_declarator)
6485 if (cp_parser_declares_only_class_p (parser))
6486 shadow_tag (decl_specifiers);
6487 /* Perform any deferred access checks. */
6488 perform_deferred_access_checks ();
6491 /* Consume the `;'. */
6492 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6494 done:
6495 pop_deferring_access_checks ();
6498 /* Parse a decl-specifier-seq.
6500 decl-specifier-seq:
6501 decl-specifier-seq [opt] decl-specifier
6503 decl-specifier:
6504 storage-class-specifier
6505 type-specifier
6506 function-specifier
6507 friend
6508 typedef
6510 GNU Extension:
6512 decl-specifier-seq:
6513 decl-specifier-seq [opt] attributes
6515 Returns a TREE_LIST, giving the decl-specifiers in the order they
6516 appear in the source code. The TREE_VALUE of each node is the
6517 decl-specifier. For a keyword (such as `auto' or `friend'), the
6518 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6519 representation of a type-specifier, see cp_parser_type_specifier.
6521 If there are attributes, they will be stored in *ATTRIBUTES,
6522 represented as described above cp_parser_attributes.
6524 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6525 appears, and the entity that will be a friend is not going to be a
6526 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6527 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6528 friendship is granted might not be a class.
6530 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6531 *flags:
6533 1: one of the decl-specifiers is an elaborated-type-specifier
6534 2: one of the decl-specifiers is an enum-specifier or a
6535 class-specifier
6539 static tree
6540 cp_parser_decl_specifier_seq (cp_parser* parser,
6541 cp_parser_flags flags,
6542 tree* attributes,
6543 int* declares_class_or_enum)
6545 tree decl_specs = NULL_TREE;
6546 bool friend_p = false;
6547 bool constructor_possible_p = !parser->in_declarator_p;
6549 /* Assume no class or enumeration type is declared. */
6550 *declares_class_or_enum = 0;
6552 /* Assume there are no attributes. */
6553 *attributes = NULL_TREE;
6555 /* Keep reading specifiers until there are no more to read. */
6556 while (true)
6558 tree decl_spec = NULL_TREE;
6559 bool constructor_p;
6560 cp_token *token;
6562 /* Peek at the next token. */
6563 token = cp_lexer_peek_token (parser->lexer);
6564 /* Handle attributes. */
6565 if (token->keyword == RID_ATTRIBUTE)
6567 /* Parse the attributes. */
6568 decl_spec = cp_parser_attributes_opt (parser);
6569 /* Add them to the list. */
6570 *attributes = chainon (*attributes, decl_spec);
6571 continue;
6573 /* If the next token is an appropriate keyword, we can simply
6574 add it to the list. */
6575 switch (token->keyword)
6577 case RID_FRIEND:
6578 /* decl-specifier:
6579 friend */
6580 if (friend_p)
6581 error ("duplicate `friend'");
6582 else
6583 friend_p = true;
6584 /* The representation of the specifier is simply the
6585 appropriate TREE_IDENTIFIER node. */
6586 decl_spec = token->value;
6587 /* Consume the token. */
6588 cp_lexer_consume_token (parser->lexer);
6589 break;
6591 /* function-specifier:
6592 inline
6593 virtual
6594 explicit */
6595 case RID_INLINE:
6596 case RID_VIRTUAL:
6597 case RID_EXPLICIT:
6598 decl_spec = cp_parser_function_specifier_opt (parser);
6599 break;
6601 /* decl-specifier:
6602 typedef */
6603 case RID_TYPEDEF:
6604 /* The representation of the specifier is simply the
6605 appropriate TREE_IDENTIFIER node. */
6606 decl_spec = token->value;
6607 /* Consume the token. */
6608 cp_lexer_consume_token (parser->lexer);
6609 /* A constructor declarator cannot appear in a typedef. */
6610 constructor_possible_p = false;
6611 /* The "typedef" keyword can only occur in a declaration; we
6612 may as well commit at this point. */
6613 cp_parser_commit_to_tentative_parse (parser);
6614 break;
6616 /* storage-class-specifier:
6617 auto
6618 register
6619 static
6620 extern
6621 mutable
6623 GNU Extension:
6624 thread */
6625 case RID_AUTO:
6626 case RID_REGISTER:
6627 case RID_STATIC:
6628 case RID_EXTERN:
6629 case RID_MUTABLE:
6630 case RID_THREAD:
6631 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6632 break;
6634 default:
6635 break;
6638 /* Constructors are a special case. The `S' in `S()' is not a
6639 decl-specifier; it is the beginning of the declarator. */
6640 constructor_p = (!decl_spec
6641 && constructor_possible_p
6642 && cp_parser_constructor_declarator_p (parser,
6643 friend_p));
6645 /* If we don't have a DECL_SPEC yet, then we must be looking at
6646 a type-specifier. */
6647 if (!decl_spec && !constructor_p)
6649 int decl_spec_declares_class_or_enum;
6650 bool is_cv_qualifier;
6652 decl_spec
6653 = cp_parser_type_specifier (parser, flags,
6654 friend_p,
6655 /*is_declaration=*/true,
6656 &decl_spec_declares_class_or_enum,
6657 &is_cv_qualifier);
6659 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6661 /* If this type-specifier referenced a user-defined type
6662 (a typedef, class-name, etc.), then we can't allow any
6663 more such type-specifiers henceforth.
6665 [dcl.spec]
6667 The longest sequence of decl-specifiers that could
6668 possibly be a type name is taken as the
6669 decl-specifier-seq of a declaration. The sequence shall
6670 be self-consistent as described below.
6672 [dcl.type]
6674 As a general rule, at most one type-specifier is allowed
6675 in the complete decl-specifier-seq of a declaration. The
6676 only exceptions are the following:
6678 -- const or volatile can be combined with any other
6679 type-specifier.
6681 -- signed or unsigned can be combined with char, long,
6682 short, or int.
6684 -- ..
6686 Example:
6688 typedef char* Pc;
6689 void g (const int Pc);
6691 Here, Pc is *not* part of the decl-specifier seq; it's
6692 the declarator. Therefore, once we see a type-specifier
6693 (other than a cv-qualifier), we forbid any additional
6694 user-defined types. We *do* still allow things like `int
6695 int' to be considered a decl-specifier-seq, and issue the
6696 error message later. */
6697 if (decl_spec && !is_cv_qualifier)
6698 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6699 /* A constructor declarator cannot follow a type-specifier. */
6700 if (decl_spec)
6701 constructor_possible_p = false;
6704 /* If we still do not have a DECL_SPEC, then there are no more
6705 decl-specifiers. */
6706 if (!decl_spec)
6708 /* Issue an error message, unless the entire construct was
6709 optional. */
6710 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6712 cp_parser_error (parser, "expected decl specifier");
6713 return error_mark_node;
6716 break;
6719 /* Add the DECL_SPEC to the list of specifiers. */
6720 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6721 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6723 /* After we see one decl-specifier, further decl-specifiers are
6724 always optional. */
6725 flags |= CP_PARSER_FLAGS_OPTIONAL;
6728 /* We have built up the DECL_SPECS in reverse order. Return them in
6729 the correct order. */
6730 return nreverse (decl_specs);
6733 /* Parse an (optional) storage-class-specifier.
6735 storage-class-specifier:
6736 auto
6737 register
6738 static
6739 extern
6740 mutable
6742 GNU Extension:
6744 storage-class-specifier:
6745 thread
6747 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6749 static tree
6750 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6752 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6754 case RID_AUTO:
6755 case RID_REGISTER:
6756 case RID_STATIC:
6757 case RID_EXTERN:
6758 case RID_MUTABLE:
6759 case RID_THREAD:
6760 /* Consume the token. */
6761 return cp_lexer_consume_token (parser->lexer)->value;
6763 default:
6764 return NULL_TREE;
6768 /* Parse an (optional) function-specifier.
6770 function-specifier:
6771 inline
6772 virtual
6773 explicit
6775 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6777 static tree
6778 cp_parser_function_specifier_opt (cp_parser* parser)
6780 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6782 case RID_INLINE:
6783 case RID_VIRTUAL:
6784 case RID_EXPLICIT:
6785 /* Consume the token. */
6786 return cp_lexer_consume_token (parser->lexer)->value;
6788 default:
6789 return NULL_TREE;
6793 /* Parse a linkage-specification.
6795 linkage-specification:
6796 extern string-literal { declaration-seq [opt] }
6797 extern string-literal declaration */
6799 static void
6800 cp_parser_linkage_specification (cp_parser* parser)
6802 cp_token *token;
6803 tree linkage;
6805 /* Look for the `extern' keyword. */
6806 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6808 /* Peek at the next token. */
6809 token = cp_lexer_peek_token (parser->lexer);
6810 /* If it's not a string-literal, then there's a problem. */
6811 if (!cp_parser_is_string_literal (token))
6813 cp_parser_error (parser, "expected language-name");
6814 return;
6816 /* Consume the token. */
6817 cp_lexer_consume_token (parser->lexer);
6819 /* Transform the literal into an identifier. If the literal is a
6820 wide-character string, or contains embedded NULs, then we can't
6821 handle it as the user wants. */
6822 if (token->type == CPP_WSTRING
6823 || (strlen (TREE_STRING_POINTER (token->value))
6824 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6826 cp_parser_error (parser, "invalid linkage-specification");
6827 /* Assume C++ linkage. */
6828 linkage = get_identifier ("c++");
6830 /* If it's a simple string constant, things are easier. */
6831 else
6832 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6834 /* We're now using the new linkage. */
6835 push_lang_context (linkage);
6837 /* If the next token is a `{', then we're using the first
6838 production. */
6839 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6841 /* Consume the `{' token. */
6842 cp_lexer_consume_token (parser->lexer);
6843 /* Parse the declarations. */
6844 cp_parser_declaration_seq_opt (parser);
6845 /* Look for the closing `}'. */
6846 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6848 /* Otherwise, there's just one declaration. */
6849 else
6851 bool saved_in_unbraced_linkage_specification_p;
6853 saved_in_unbraced_linkage_specification_p
6854 = parser->in_unbraced_linkage_specification_p;
6855 parser->in_unbraced_linkage_specification_p = true;
6856 have_extern_spec = true;
6857 cp_parser_declaration (parser);
6858 have_extern_spec = false;
6859 parser->in_unbraced_linkage_specification_p
6860 = saved_in_unbraced_linkage_specification_p;
6863 /* We're done with the linkage-specification. */
6864 pop_lang_context ();
6867 /* Special member functions [gram.special] */
6869 /* Parse a conversion-function-id.
6871 conversion-function-id:
6872 operator conversion-type-id
6874 Returns an IDENTIFIER_NODE representing the operator. */
6876 static tree
6877 cp_parser_conversion_function_id (cp_parser* parser)
6879 tree type;
6880 tree saved_scope;
6881 tree saved_qualifying_scope;
6882 tree saved_object_scope;
6884 /* Look for the `operator' token. */
6885 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6886 return error_mark_node;
6887 /* When we parse the conversion-type-id, the current scope will be
6888 reset. However, we need that information in able to look up the
6889 conversion function later, so we save it here. */
6890 saved_scope = parser->scope;
6891 saved_qualifying_scope = parser->qualifying_scope;
6892 saved_object_scope = parser->object_scope;
6893 /* We must enter the scope of the class so that the names of
6894 entities declared within the class are available in the
6895 conversion-type-id. For example, consider:
6897 struct S {
6898 typedef int I;
6899 operator I();
6902 S::operator I() { ... }
6904 In order to see that `I' is a type-name in the definition, we
6905 must be in the scope of `S'. */
6906 if (saved_scope)
6907 push_scope (saved_scope);
6908 /* Parse the conversion-type-id. */
6909 type = cp_parser_conversion_type_id (parser);
6910 /* Leave the scope of the class, if any. */
6911 if (saved_scope)
6912 pop_scope (saved_scope);
6913 /* Restore the saved scope. */
6914 parser->scope = saved_scope;
6915 parser->qualifying_scope = saved_qualifying_scope;
6916 parser->object_scope = saved_object_scope;
6917 /* If the TYPE is invalid, indicate failure. */
6918 if (type == error_mark_node)
6919 return error_mark_node;
6920 return mangle_conv_op_name_for_type (type);
6923 /* Parse a conversion-type-id:
6925 conversion-type-id:
6926 type-specifier-seq conversion-declarator [opt]
6928 Returns the TYPE specified. */
6930 static tree
6931 cp_parser_conversion_type_id (cp_parser* parser)
6933 tree attributes;
6934 tree type_specifiers;
6935 tree declarator;
6937 /* Parse the attributes. */
6938 attributes = cp_parser_attributes_opt (parser);
6939 /* Parse the type-specifiers. */
6940 type_specifiers = cp_parser_type_specifier_seq (parser);
6941 /* If that didn't work, stop. */
6942 if (type_specifiers == error_mark_node)
6943 return error_mark_node;
6944 /* Parse the conversion-declarator. */
6945 declarator = cp_parser_conversion_declarator_opt (parser);
6947 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6948 /*initialized=*/0, &attributes);
6951 /* Parse an (optional) conversion-declarator.
6953 conversion-declarator:
6954 ptr-operator conversion-declarator [opt]
6956 Returns a representation of the declarator. See
6957 cp_parser_declarator for details. */
6959 static tree
6960 cp_parser_conversion_declarator_opt (cp_parser* parser)
6962 enum tree_code code;
6963 tree class_type;
6964 tree cv_qualifier_seq;
6966 /* We don't know if there's a ptr-operator next, or not. */
6967 cp_parser_parse_tentatively (parser);
6968 /* Try the ptr-operator. */
6969 code = cp_parser_ptr_operator (parser, &class_type,
6970 &cv_qualifier_seq);
6971 /* If it worked, look for more conversion-declarators. */
6972 if (cp_parser_parse_definitely (parser))
6974 tree declarator;
6976 /* Parse another optional declarator. */
6977 declarator = cp_parser_conversion_declarator_opt (parser);
6979 /* Create the representation of the declarator. */
6980 if (code == INDIRECT_REF)
6981 declarator = make_pointer_declarator (cv_qualifier_seq,
6982 declarator);
6983 else
6984 declarator = make_reference_declarator (cv_qualifier_seq,
6985 declarator);
6987 /* Handle the pointer-to-member case. */
6988 if (class_type)
6989 declarator = build_nt (SCOPE_REF, class_type, declarator);
6991 return declarator;
6994 return NULL_TREE;
6997 /* Parse an (optional) ctor-initializer.
6999 ctor-initializer:
7000 : mem-initializer-list
7002 Returns TRUE iff the ctor-initializer was actually present. */
7004 static bool
7005 cp_parser_ctor_initializer_opt (cp_parser* parser)
7007 /* If the next token is not a `:', then there is no
7008 ctor-initializer. */
7009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7011 /* Do default initialization of any bases and members. */
7012 if (DECL_CONSTRUCTOR_P (current_function_decl))
7013 finish_mem_initializers (NULL_TREE);
7015 return false;
7018 /* Consume the `:' token. */
7019 cp_lexer_consume_token (parser->lexer);
7020 /* And the mem-initializer-list. */
7021 cp_parser_mem_initializer_list (parser);
7023 return true;
7026 /* Parse a mem-initializer-list.
7028 mem-initializer-list:
7029 mem-initializer
7030 mem-initializer , mem-initializer-list */
7032 static void
7033 cp_parser_mem_initializer_list (cp_parser* parser)
7035 tree mem_initializer_list = NULL_TREE;
7037 /* Let the semantic analysis code know that we are starting the
7038 mem-initializer-list. */
7039 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7040 error ("only constructors take base initializers");
7042 /* Loop through the list. */
7043 while (true)
7045 tree mem_initializer;
7047 /* Parse the mem-initializer. */
7048 mem_initializer = cp_parser_mem_initializer (parser);
7049 /* Add it to the list, unless it was erroneous. */
7050 if (mem_initializer)
7052 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7053 mem_initializer_list = mem_initializer;
7055 /* If the next token is not a `,', we're done. */
7056 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7057 break;
7058 /* Consume the `,' token. */
7059 cp_lexer_consume_token (parser->lexer);
7062 /* Perform semantic analysis. */
7063 if (DECL_CONSTRUCTOR_P (current_function_decl))
7064 finish_mem_initializers (mem_initializer_list);
7067 /* Parse a mem-initializer.
7069 mem-initializer:
7070 mem-initializer-id ( expression-list [opt] )
7072 GNU extension:
7074 mem-initializer:
7075 ( expression-list [opt] )
7077 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7078 class) or FIELD_DECL (for a non-static data member) to initialize;
7079 the TREE_VALUE is the expression-list. */
7081 static tree
7082 cp_parser_mem_initializer (cp_parser* parser)
7084 tree mem_initializer_id;
7085 tree expression_list;
7086 tree member;
7088 /* Find out what is being initialized. */
7089 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7091 pedwarn ("anachronistic old-style base class initializer");
7092 mem_initializer_id = NULL_TREE;
7094 else
7095 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7096 member = expand_member_init (mem_initializer_id);
7097 if (member && !DECL_P (member))
7098 in_base_initializer = 1;
7100 expression_list
7101 = cp_parser_parenthesized_expression_list (parser, false,
7102 /*non_constant_p=*/NULL);
7103 if (!expression_list)
7104 expression_list = void_type_node;
7106 in_base_initializer = 0;
7108 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7111 /* Parse a mem-initializer-id.
7113 mem-initializer-id:
7114 :: [opt] nested-name-specifier [opt] class-name
7115 identifier
7117 Returns a TYPE indicating the class to be initializer for the first
7118 production. Returns an IDENTIFIER_NODE indicating the data member
7119 to be initialized for the second production. */
7121 static tree
7122 cp_parser_mem_initializer_id (cp_parser* parser)
7124 bool global_scope_p;
7125 bool nested_name_specifier_p;
7126 tree id;
7128 /* Look for the optional `::' operator. */
7129 global_scope_p
7130 = (cp_parser_global_scope_opt (parser,
7131 /*current_scope_valid_p=*/false)
7132 != NULL_TREE);
7133 /* Look for the optional nested-name-specifier. The simplest way to
7134 implement:
7136 [temp.res]
7138 The keyword `typename' is not permitted in a base-specifier or
7139 mem-initializer; in these contexts a qualified name that
7140 depends on a template-parameter is implicitly assumed to be a
7141 type name.
7143 is to assume that we have seen the `typename' keyword at this
7144 point. */
7145 nested_name_specifier_p
7146 = (cp_parser_nested_name_specifier_opt (parser,
7147 /*typename_keyword_p=*/true,
7148 /*check_dependency_p=*/true,
7149 /*type_p=*/true,
7150 /*is_declaration=*/true)
7151 != NULL_TREE);
7152 /* If there is a `::' operator or a nested-name-specifier, then we
7153 are definitely looking for a class-name. */
7154 if (global_scope_p || nested_name_specifier_p)
7155 return cp_parser_class_name (parser,
7156 /*typename_keyword_p=*/true,
7157 /*template_keyword_p=*/false,
7158 /*type_p=*/false,
7159 /*check_dependency_p=*/true,
7160 /*class_head_p=*/false,
7161 /*is_declaration=*/true);
7162 /* Otherwise, we could also be looking for an ordinary identifier. */
7163 cp_parser_parse_tentatively (parser);
7164 /* Try a class-name. */
7165 id = cp_parser_class_name (parser,
7166 /*typename_keyword_p=*/true,
7167 /*template_keyword_p=*/false,
7168 /*type_p=*/false,
7169 /*check_dependency_p=*/true,
7170 /*class_head_p=*/false,
7171 /*is_declaration=*/true);
7172 /* If we found one, we're done. */
7173 if (cp_parser_parse_definitely (parser))
7174 return id;
7175 /* Otherwise, look for an ordinary identifier. */
7176 return cp_parser_identifier (parser);
7179 /* Overloading [gram.over] */
7181 /* Parse an operator-function-id.
7183 operator-function-id:
7184 operator operator
7186 Returns an IDENTIFIER_NODE for the operator which is a
7187 human-readable spelling of the identifier, e.g., `operator +'. */
7189 static tree
7190 cp_parser_operator_function_id (cp_parser* parser)
7192 /* Look for the `operator' keyword. */
7193 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7194 return error_mark_node;
7195 /* And then the name of the operator itself. */
7196 return cp_parser_operator (parser);
7199 /* Parse an operator.
7201 operator:
7202 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7203 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7204 || ++ -- , ->* -> () []
7206 GNU Extensions:
7208 operator:
7209 <? >? <?= >?=
7211 Returns an IDENTIFIER_NODE for the operator which is a
7212 human-readable spelling of the identifier, e.g., `operator +'. */
7214 static tree
7215 cp_parser_operator (cp_parser* parser)
7217 tree id = NULL_TREE;
7218 cp_token *token;
7220 /* Peek at the next token. */
7221 token = cp_lexer_peek_token (parser->lexer);
7222 /* Figure out which operator we have. */
7223 switch (token->type)
7225 case CPP_KEYWORD:
7227 enum tree_code op;
7229 /* The keyword should be either `new' or `delete'. */
7230 if (token->keyword == RID_NEW)
7231 op = NEW_EXPR;
7232 else if (token->keyword == RID_DELETE)
7233 op = DELETE_EXPR;
7234 else
7235 break;
7237 /* Consume the `new' or `delete' token. */
7238 cp_lexer_consume_token (parser->lexer);
7240 /* Peek at the next token. */
7241 token = cp_lexer_peek_token (parser->lexer);
7242 /* If it's a `[' token then this is the array variant of the
7243 operator. */
7244 if (token->type == CPP_OPEN_SQUARE)
7246 /* Consume the `[' token. */
7247 cp_lexer_consume_token (parser->lexer);
7248 /* Look for the `]' token. */
7249 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7250 id = ansi_opname (op == NEW_EXPR
7251 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7253 /* Otherwise, we have the non-array variant. */
7254 else
7255 id = ansi_opname (op);
7257 return id;
7260 case CPP_PLUS:
7261 id = ansi_opname (PLUS_EXPR);
7262 break;
7264 case CPP_MINUS:
7265 id = ansi_opname (MINUS_EXPR);
7266 break;
7268 case CPP_MULT:
7269 id = ansi_opname (MULT_EXPR);
7270 break;
7272 case CPP_DIV:
7273 id = ansi_opname (TRUNC_DIV_EXPR);
7274 break;
7276 case CPP_MOD:
7277 id = ansi_opname (TRUNC_MOD_EXPR);
7278 break;
7280 case CPP_XOR:
7281 id = ansi_opname (BIT_XOR_EXPR);
7282 break;
7284 case CPP_AND:
7285 id = ansi_opname (BIT_AND_EXPR);
7286 break;
7288 case CPP_OR:
7289 id = ansi_opname (BIT_IOR_EXPR);
7290 break;
7292 case CPP_COMPL:
7293 id = ansi_opname (BIT_NOT_EXPR);
7294 break;
7296 case CPP_NOT:
7297 id = ansi_opname (TRUTH_NOT_EXPR);
7298 break;
7300 case CPP_EQ:
7301 id = ansi_assopname (NOP_EXPR);
7302 break;
7304 case CPP_LESS:
7305 id = ansi_opname (LT_EXPR);
7306 break;
7308 case CPP_GREATER:
7309 id = ansi_opname (GT_EXPR);
7310 break;
7312 case CPP_PLUS_EQ:
7313 id = ansi_assopname (PLUS_EXPR);
7314 break;
7316 case CPP_MINUS_EQ:
7317 id = ansi_assopname (MINUS_EXPR);
7318 break;
7320 case CPP_MULT_EQ:
7321 id = ansi_assopname (MULT_EXPR);
7322 break;
7324 case CPP_DIV_EQ:
7325 id = ansi_assopname (TRUNC_DIV_EXPR);
7326 break;
7328 case CPP_MOD_EQ:
7329 id = ansi_assopname (TRUNC_MOD_EXPR);
7330 break;
7332 case CPP_XOR_EQ:
7333 id = ansi_assopname (BIT_XOR_EXPR);
7334 break;
7336 case CPP_AND_EQ:
7337 id = ansi_assopname (BIT_AND_EXPR);
7338 break;
7340 case CPP_OR_EQ:
7341 id = ansi_assopname (BIT_IOR_EXPR);
7342 break;
7344 case CPP_LSHIFT:
7345 id = ansi_opname (LSHIFT_EXPR);
7346 break;
7348 case CPP_RSHIFT:
7349 id = ansi_opname (RSHIFT_EXPR);
7350 break;
7352 case CPP_LSHIFT_EQ:
7353 id = ansi_assopname (LSHIFT_EXPR);
7354 break;
7356 case CPP_RSHIFT_EQ:
7357 id = ansi_assopname (RSHIFT_EXPR);
7358 break;
7360 case CPP_EQ_EQ:
7361 id = ansi_opname (EQ_EXPR);
7362 break;
7364 case CPP_NOT_EQ:
7365 id = ansi_opname (NE_EXPR);
7366 break;
7368 case CPP_LESS_EQ:
7369 id = ansi_opname (LE_EXPR);
7370 break;
7372 case CPP_GREATER_EQ:
7373 id = ansi_opname (GE_EXPR);
7374 break;
7376 case CPP_AND_AND:
7377 id = ansi_opname (TRUTH_ANDIF_EXPR);
7378 break;
7380 case CPP_OR_OR:
7381 id = ansi_opname (TRUTH_ORIF_EXPR);
7382 break;
7384 case CPP_PLUS_PLUS:
7385 id = ansi_opname (POSTINCREMENT_EXPR);
7386 break;
7388 case CPP_MINUS_MINUS:
7389 id = ansi_opname (PREDECREMENT_EXPR);
7390 break;
7392 case CPP_COMMA:
7393 id = ansi_opname (COMPOUND_EXPR);
7394 break;
7396 case CPP_DEREF_STAR:
7397 id = ansi_opname (MEMBER_REF);
7398 break;
7400 case CPP_DEREF:
7401 id = ansi_opname (COMPONENT_REF);
7402 break;
7404 case CPP_OPEN_PAREN:
7405 /* Consume the `('. */
7406 cp_lexer_consume_token (parser->lexer);
7407 /* Look for the matching `)'. */
7408 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7409 return ansi_opname (CALL_EXPR);
7411 case CPP_OPEN_SQUARE:
7412 /* Consume the `['. */
7413 cp_lexer_consume_token (parser->lexer);
7414 /* Look for the matching `]'. */
7415 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7416 return ansi_opname (ARRAY_REF);
7418 /* Extensions. */
7419 case CPP_MIN:
7420 id = ansi_opname (MIN_EXPR);
7421 break;
7423 case CPP_MAX:
7424 id = ansi_opname (MAX_EXPR);
7425 break;
7427 case CPP_MIN_EQ:
7428 id = ansi_assopname (MIN_EXPR);
7429 break;
7431 case CPP_MAX_EQ:
7432 id = ansi_assopname (MAX_EXPR);
7433 break;
7435 default:
7436 /* Anything else is an error. */
7437 break;
7440 /* If we have selected an identifier, we need to consume the
7441 operator token. */
7442 if (id)
7443 cp_lexer_consume_token (parser->lexer);
7444 /* Otherwise, no valid operator name was present. */
7445 else
7447 cp_parser_error (parser, "expected operator");
7448 id = error_mark_node;
7451 return id;
7454 /* Parse a template-declaration.
7456 template-declaration:
7457 export [opt] template < template-parameter-list > declaration
7459 If MEMBER_P is TRUE, this template-declaration occurs within a
7460 class-specifier.
7462 The grammar rule given by the standard isn't correct. What
7463 is really meant is:
7465 template-declaration:
7466 export [opt] template-parameter-list-seq
7467 decl-specifier-seq [opt] init-declarator [opt] ;
7468 export [opt] template-parameter-list-seq
7469 function-definition
7471 template-parameter-list-seq:
7472 template-parameter-list-seq [opt]
7473 template < template-parameter-list > */
7475 static void
7476 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7478 /* Check for `export'. */
7479 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7481 /* Consume the `export' token. */
7482 cp_lexer_consume_token (parser->lexer);
7483 /* Warn that we do not support `export'. */
7484 warning ("keyword `export' not implemented, and will be ignored");
7487 cp_parser_template_declaration_after_export (parser, member_p);
7490 /* Parse a template-parameter-list.
7492 template-parameter-list:
7493 template-parameter
7494 template-parameter-list , template-parameter
7496 Returns a TREE_LIST. Each node represents a template parameter.
7497 The nodes are connected via their TREE_CHAINs. */
7499 static tree
7500 cp_parser_template_parameter_list (cp_parser* parser)
7502 tree parameter_list = NULL_TREE;
7504 while (true)
7506 tree parameter;
7507 cp_token *token;
7509 /* Parse the template-parameter. */
7510 parameter = cp_parser_template_parameter (parser);
7511 /* Add it to the list. */
7512 parameter_list = process_template_parm (parameter_list,
7513 parameter);
7515 /* Peek at the next token. */
7516 token = cp_lexer_peek_token (parser->lexer);
7517 /* If it's not a `,', we're done. */
7518 if (token->type != CPP_COMMA)
7519 break;
7520 /* Otherwise, consume the `,' token. */
7521 cp_lexer_consume_token (parser->lexer);
7524 return parameter_list;
7527 /* Parse a template-parameter.
7529 template-parameter:
7530 type-parameter
7531 parameter-declaration
7533 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7534 TREE_PURPOSE is the default value, if any. */
7536 static tree
7537 cp_parser_template_parameter (cp_parser* parser)
7539 cp_token *token;
7541 /* Peek at the next token. */
7542 token = cp_lexer_peek_token (parser->lexer);
7543 /* If it is `class' or `template', we have a type-parameter. */
7544 if (token->keyword == RID_TEMPLATE)
7545 return cp_parser_type_parameter (parser);
7546 /* If it is `class' or `typename' we do not know yet whether it is a
7547 type parameter or a non-type parameter. Consider:
7549 template <typename T, typename T::X X> ...
7553 template <class C, class D*> ...
7555 Here, the first parameter is a type parameter, and the second is
7556 a non-type parameter. We can tell by looking at the token after
7557 the identifier -- if it is a `,', `=', or `>' then we have a type
7558 parameter. */
7559 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7561 /* Peek at the token after `class' or `typename'. */
7562 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7563 /* If it's an identifier, skip it. */
7564 if (token->type == CPP_NAME)
7565 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7566 /* Now, see if the token looks like the end of a template
7567 parameter. */
7568 if (token->type == CPP_COMMA
7569 || token->type == CPP_EQ
7570 || token->type == CPP_GREATER)
7571 return cp_parser_type_parameter (parser);
7574 /* Otherwise, it is a non-type parameter.
7576 [temp.param]
7578 When parsing a default template-argument for a non-type
7579 template-parameter, the first non-nested `>' is taken as the end
7580 of the template parameter-list rather than a greater-than
7581 operator. */
7582 return
7583 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7584 /*parenthesized_p=*/NULL);
7587 /* Parse a type-parameter.
7589 type-parameter:
7590 class identifier [opt]
7591 class identifier [opt] = type-id
7592 typename identifier [opt]
7593 typename identifier [opt] = type-id
7594 template < template-parameter-list > class identifier [opt]
7595 template < template-parameter-list > class identifier [opt]
7596 = id-expression
7598 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7599 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7600 the declaration of the parameter. */
7602 static tree
7603 cp_parser_type_parameter (cp_parser* parser)
7605 cp_token *token;
7606 tree parameter;
7608 /* Look for a keyword to tell us what kind of parameter this is. */
7609 token = cp_parser_require (parser, CPP_KEYWORD,
7610 "`class', `typename', or `template'");
7611 if (!token)
7612 return error_mark_node;
7614 switch (token->keyword)
7616 case RID_CLASS:
7617 case RID_TYPENAME:
7619 tree identifier;
7620 tree default_argument;
7622 /* If the next token is an identifier, then it names the
7623 parameter. */
7624 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7625 identifier = cp_parser_identifier (parser);
7626 else
7627 identifier = NULL_TREE;
7629 /* Create the parameter. */
7630 parameter = finish_template_type_parm (class_type_node, identifier);
7632 /* If the next token is an `=', we have a default argument. */
7633 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7635 /* Consume the `=' token. */
7636 cp_lexer_consume_token (parser->lexer);
7637 /* Parse the default-argument. */
7638 default_argument = cp_parser_type_id (parser);
7640 else
7641 default_argument = NULL_TREE;
7643 /* Create the combined representation of the parameter and the
7644 default argument. */
7645 parameter = build_tree_list (default_argument, parameter);
7647 break;
7649 case RID_TEMPLATE:
7651 tree parameter_list;
7652 tree identifier;
7653 tree default_argument;
7655 /* Look for the `<'. */
7656 cp_parser_require (parser, CPP_LESS, "`<'");
7657 /* Parse the template-parameter-list. */
7658 begin_template_parm_list ();
7659 parameter_list
7660 = cp_parser_template_parameter_list (parser);
7661 parameter_list = end_template_parm_list (parameter_list);
7662 /* Look for the `>'. */
7663 cp_parser_require (parser, CPP_GREATER, "`>'");
7664 /* Look for the `class' keyword. */
7665 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7666 /* If the next token is an `=', then there is a
7667 default-argument. If the next token is a `>', we are at
7668 the end of the parameter-list. If the next token is a `,',
7669 then we are at the end of this parameter. */
7670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7671 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7672 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7673 identifier = cp_parser_identifier (parser);
7674 else
7675 identifier = NULL_TREE;
7676 /* Create the template parameter. */
7677 parameter = finish_template_template_parm (class_type_node,
7678 identifier);
7680 /* If the next token is an `=', then there is a
7681 default-argument. */
7682 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7684 bool is_template;
7686 /* Consume the `='. */
7687 cp_lexer_consume_token (parser->lexer);
7688 /* Parse the id-expression. */
7689 default_argument
7690 = cp_parser_id_expression (parser,
7691 /*template_keyword_p=*/false,
7692 /*check_dependency_p=*/true,
7693 /*template_p=*/&is_template,
7694 /*declarator_p=*/false);
7695 /* Look up the name. */
7696 default_argument
7697 = cp_parser_lookup_name (parser, default_argument,
7698 /*is_type=*/false,
7699 /*is_template=*/is_template,
7700 /*is_namespace=*/false,
7701 /*check_dependency=*/true);
7702 /* See if the default argument is valid. */
7703 default_argument
7704 = check_template_template_default_arg (default_argument);
7706 else
7707 default_argument = NULL_TREE;
7709 /* Create the combined representation of the parameter and the
7710 default argument. */
7711 parameter = build_tree_list (default_argument, parameter);
7713 break;
7715 default:
7716 /* Anything else is an error. */
7717 cp_parser_error (parser,
7718 "expected `class', `typename', or `template'");
7719 parameter = error_mark_node;
7722 return parameter;
7725 /* Parse a template-id.
7727 template-id:
7728 template-name < template-argument-list [opt] >
7730 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7731 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7732 returned. Otherwise, if the template-name names a function, or set
7733 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7734 names a class, returns a TYPE_DECL for the specialization.
7736 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7737 uninstantiated templates. */
7739 static tree
7740 cp_parser_template_id (cp_parser *parser,
7741 bool template_keyword_p,
7742 bool check_dependency_p,
7743 bool is_declaration)
7745 tree template;
7746 tree arguments;
7747 tree template_id;
7748 ptrdiff_t start_of_id;
7749 tree access_check = NULL_TREE;
7750 cp_token *next_token;
7751 bool is_identifier;
7753 /* If the next token corresponds to a template-id, there is no need
7754 to reparse it. */
7755 next_token = cp_lexer_peek_token (parser->lexer);
7756 if (next_token->type == CPP_TEMPLATE_ID)
7758 tree value;
7759 tree check;
7761 /* Get the stored value. */
7762 value = cp_lexer_consume_token (parser->lexer)->value;
7763 /* Perform any access checks that were deferred. */
7764 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7765 perform_or_defer_access_check (TREE_PURPOSE (check),
7766 TREE_VALUE (check));
7767 /* Return the stored value. */
7768 return TREE_VALUE (value);
7771 /* Avoid performing name lookup if there is no possibility of
7772 finding a template-id. */
7773 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7774 || (next_token->type == CPP_NAME
7775 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7777 cp_parser_error (parser, "expected template-id");
7778 return error_mark_node;
7781 /* Remember where the template-id starts. */
7782 if (cp_parser_parsing_tentatively (parser)
7783 && !cp_parser_committed_to_tentative_parse (parser))
7785 next_token = cp_lexer_peek_token (parser->lexer);
7786 start_of_id = cp_lexer_token_difference (parser->lexer,
7787 parser->lexer->first_token,
7788 next_token);
7790 else
7791 start_of_id = -1;
7793 push_deferring_access_checks (dk_deferred);
7795 /* Parse the template-name. */
7796 is_identifier = false;
7797 template = cp_parser_template_name (parser, template_keyword_p,
7798 check_dependency_p,
7799 is_declaration,
7800 &is_identifier);
7801 if (template == error_mark_node || is_identifier)
7803 pop_deferring_access_checks ();
7804 return template;
7807 /* Look for the `<' that starts the template-argument-list. */
7808 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7810 pop_deferring_access_checks ();
7811 return error_mark_node;
7814 /* Parse the arguments. */
7815 arguments = cp_parser_enclosed_template_argument_list (parser);
7817 /* Build a representation of the specialization. */
7818 if (TREE_CODE (template) == IDENTIFIER_NODE)
7819 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7820 else if (DECL_CLASS_TEMPLATE_P (template)
7821 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7822 template_id
7823 = finish_template_type (template, arguments,
7824 cp_lexer_next_token_is (parser->lexer,
7825 CPP_SCOPE));
7826 else
7828 /* If it's not a class-template or a template-template, it should be
7829 a function-template. */
7830 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7831 || TREE_CODE (template) == OVERLOAD
7832 || BASELINK_P (template)),
7833 20010716);
7835 template_id = lookup_template_function (template, arguments);
7838 /* Retrieve any deferred checks. Do not pop this access checks yet
7839 so the memory will not be reclaimed during token replacing below. */
7840 access_check = get_deferred_access_checks ();
7842 /* If parsing tentatively, replace the sequence of tokens that makes
7843 up the template-id with a CPP_TEMPLATE_ID token. That way,
7844 should we re-parse the token stream, we will not have to repeat
7845 the effort required to do the parse, nor will we issue duplicate
7846 error messages about problems during instantiation of the
7847 template. */
7848 if (start_of_id >= 0)
7850 cp_token *token;
7852 /* Find the token that corresponds to the start of the
7853 template-id. */
7854 token = cp_lexer_advance_token (parser->lexer,
7855 parser->lexer->first_token,
7856 start_of_id);
7858 /* Reset the contents of the START_OF_ID token. */
7859 token->type = CPP_TEMPLATE_ID;
7860 token->value = build_tree_list (access_check, template_id);
7861 token->keyword = RID_MAX;
7862 /* Purge all subsequent tokens. */
7863 cp_lexer_purge_tokens_after (parser->lexer, token);
7866 pop_deferring_access_checks ();
7867 return template_id;
7870 /* Parse a template-name.
7872 template-name:
7873 identifier
7875 The standard should actually say:
7877 template-name:
7878 identifier
7879 operator-function-id
7880 conversion-function-id
7882 A defect report has been filed about this issue.
7884 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7885 `template' keyword, in a construction like:
7887 T::template f<3>()
7889 In that case `f' is taken to be a template-name, even though there
7890 is no way of knowing for sure.
7892 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7893 name refers to a set of overloaded functions, at least one of which
7894 is a template, or an IDENTIFIER_NODE with the name of the template,
7895 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7896 names are looked up inside uninstantiated templates. */
7898 static tree
7899 cp_parser_template_name (cp_parser* parser,
7900 bool template_keyword_p,
7901 bool check_dependency_p,
7902 bool is_declaration,
7903 bool *is_identifier)
7905 tree identifier;
7906 tree decl;
7907 tree fns;
7909 /* If the next token is `operator', then we have either an
7910 operator-function-id or a conversion-function-id. */
7911 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7913 /* We don't know whether we're looking at an
7914 operator-function-id or a conversion-function-id. */
7915 cp_parser_parse_tentatively (parser);
7916 /* Try an operator-function-id. */
7917 identifier = cp_parser_operator_function_id (parser);
7918 /* If that didn't work, try a conversion-function-id. */
7919 if (!cp_parser_parse_definitely (parser))
7920 identifier = cp_parser_conversion_function_id (parser);
7922 /* Look for the identifier. */
7923 else
7924 identifier = cp_parser_identifier (parser);
7926 /* If we didn't find an identifier, we don't have a template-id. */
7927 if (identifier == error_mark_node)
7928 return error_mark_node;
7930 /* If the name immediately followed the `template' keyword, then it
7931 is a template-name. However, if the next token is not `<', then
7932 we do not treat it as a template-name, since it is not being used
7933 as part of a template-id. This enables us to handle constructs
7934 like:
7936 template <typename T> struct S { S(); };
7937 template <typename T> S<T>::S();
7939 correctly. We would treat `S' as a template -- if it were `S<T>'
7940 -- but we do not if there is no `<'. */
7942 if (processing_template_decl
7943 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7945 /* In a declaration, in a dependent context, we pretend that the
7946 "template" keyword was present in order to improve error
7947 recovery. For example, given:
7949 template <typename T> void f(T::X<int>);
7951 we want to treat "X<int>" as a template-id. */
7952 if (is_declaration
7953 && !template_keyword_p
7954 && parser->scope && TYPE_P (parser->scope)
7955 && dependent_type_p (parser->scope))
7957 ptrdiff_t start;
7958 cp_token* token;
7959 /* Explain what went wrong. */
7960 error ("non-template `%D' used as template", identifier);
7961 error ("(use `%T::template %D' to indicate that it is a template)",
7962 parser->scope, identifier);
7963 /* If parsing tentatively, find the location of the "<"
7964 token. */
7965 if (cp_parser_parsing_tentatively (parser)
7966 && !cp_parser_committed_to_tentative_parse (parser))
7968 cp_parser_simulate_error (parser);
7969 token = cp_lexer_peek_token (parser->lexer);
7970 token = cp_lexer_prev_token (parser->lexer, token);
7971 start = cp_lexer_token_difference (parser->lexer,
7972 parser->lexer->first_token,
7973 token);
7975 else
7976 start = -1;
7977 /* Parse the template arguments so that we can issue error
7978 messages about them. */
7979 cp_lexer_consume_token (parser->lexer);
7980 cp_parser_enclosed_template_argument_list (parser);
7981 /* Skip tokens until we find a good place from which to
7982 continue parsing. */
7983 cp_parser_skip_to_closing_parenthesis (parser,
7984 /*recovering=*/true,
7985 /*or_comma=*/true,
7986 /*consume_paren=*/false);
7987 /* If parsing tentatively, permanently remove the
7988 template argument list. That will prevent duplicate
7989 error messages from being issued about the missing
7990 "template" keyword. */
7991 if (start >= 0)
7993 token = cp_lexer_advance_token (parser->lexer,
7994 parser->lexer->first_token,
7995 start);
7996 cp_lexer_purge_tokens_after (parser->lexer, token);
7998 if (is_identifier)
7999 *is_identifier = true;
8000 return identifier;
8002 if (template_keyword_p)
8003 return identifier;
8006 /* Look up the name. */
8007 decl = cp_parser_lookup_name (parser, identifier,
8008 /*is_type=*/false,
8009 /*is_template=*/false,
8010 /*is_namespace=*/false,
8011 check_dependency_p);
8012 decl = maybe_get_template_decl_from_type_decl (decl);
8014 /* If DECL is a template, then the name was a template-name. */
8015 if (TREE_CODE (decl) == TEMPLATE_DECL)
8017 else
8019 /* The standard does not explicitly indicate whether a name that
8020 names a set of overloaded declarations, some of which are
8021 templates, is a template-name. However, such a name should
8022 be a template-name; otherwise, there is no way to form a
8023 template-id for the overloaded templates. */
8024 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8025 if (TREE_CODE (fns) == OVERLOAD)
8027 tree fn;
8029 for (fn = fns; fn; fn = OVL_NEXT (fn))
8030 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8031 break;
8033 else
8035 /* Otherwise, the name does not name a template. */
8036 cp_parser_error (parser, "expected template-name");
8037 return error_mark_node;
8041 /* If DECL is dependent, and refers to a function, then just return
8042 its name; we will look it up again during template instantiation. */
8043 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8045 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8046 if (TYPE_P (scope) && dependent_type_p (scope))
8047 return identifier;
8050 return decl;
8053 /* Parse a template-argument-list.
8055 template-argument-list:
8056 template-argument
8057 template-argument-list , template-argument
8059 Returns a TREE_VEC containing the arguments. */
8061 static tree
8062 cp_parser_template_argument_list (cp_parser* parser)
8064 tree fixed_args[10];
8065 unsigned n_args = 0;
8066 unsigned alloced = 10;
8067 tree *arg_ary = fixed_args;
8068 tree vec;
8069 bool saved_in_template_argument_list_p;
8071 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8072 parser->in_template_argument_list_p = true;
8075 tree argument;
8077 if (n_args)
8078 /* Consume the comma. */
8079 cp_lexer_consume_token (parser->lexer);
8081 /* Parse the template-argument. */
8082 argument = cp_parser_template_argument (parser);
8083 if (n_args == alloced)
8085 alloced *= 2;
8087 if (arg_ary == fixed_args)
8089 arg_ary = xmalloc (sizeof (tree) * alloced);
8090 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8092 else
8093 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8095 arg_ary[n_args++] = argument;
8097 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8099 vec = make_tree_vec (n_args);
8101 while (n_args--)
8102 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8104 if (arg_ary != fixed_args)
8105 free (arg_ary);
8106 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8107 return vec;
8110 /* Parse a template-argument.
8112 template-argument:
8113 assignment-expression
8114 type-id
8115 id-expression
8117 The representation is that of an assignment-expression, type-id, or
8118 id-expression -- except that the qualified id-expression is
8119 evaluated, so that the value returned is either a DECL or an
8120 OVERLOAD.
8122 Although the standard says "assignment-expression", it forbids
8123 throw-expressions or assignments in the template argument.
8124 Therefore, we use "conditional-expression" instead. */
8126 static tree
8127 cp_parser_template_argument (cp_parser* parser)
8129 tree argument;
8130 bool template_p;
8131 bool address_p;
8132 bool maybe_type_id = false;
8133 cp_token *token;
8134 cp_id_kind idk;
8135 tree qualifying_class;
8137 /* There's really no way to know what we're looking at, so we just
8138 try each alternative in order.
8140 [temp.arg]
8142 In a template-argument, an ambiguity between a type-id and an
8143 expression is resolved to a type-id, regardless of the form of
8144 the corresponding template-parameter.
8146 Therefore, we try a type-id first. */
8147 cp_parser_parse_tentatively (parser);
8148 argument = cp_parser_type_id (parser);
8149 /* If there was no error parsing the type-id but the next token is a '>>',
8150 we probably found a typo for '> >'. But there are type-id which are
8151 also valid expressions. For instance:
8153 struct X { int operator >> (int); };
8154 template <int V> struct Foo {};
8155 Foo<X () >> 5> r;
8157 Here 'X()' is a valid type-id of a function type, but the user just
8158 wanted to write the expression "X() >> 5". Thus, we remember that we
8159 found a valid type-id, but we still try to parse the argument as an
8160 expression to see what happens. */
8161 if (!cp_parser_error_occurred (parser)
8162 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8164 maybe_type_id = true;
8165 cp_parser_abort_tentative_parse (parser);
8167 else
8169 /* If the next token isn't a `,' or a `>', then this argument wasn't
8170 really finished. This means that the argument is not a valid
8171 type-id. */
8172 if (!cp_parser_next_token_ends_template_argument_p (parser))
8173 cp_parser_error (parser, "expected template-argument");
8174 /* If that worked, we're done. */
8175 if (cp_parser_parse_definitely (parser))
8176 return argument;
8178 /* We're still not sure what the argument will be. */
8179 cp_parser_parse_tentatively (parser);
8180 /* Try a template. */
8181 argument = cp_parser_id_expression (parser,
8182 /*template_keyword_p=*/false,
8183 /*check_dependency_p=*/true,
8184 &template_p,
8185 /*declarator_p=*/false);
8186 /* If the next token isn't a `,' or a `>', then this argument wasn't
8187 really finished. */
8188 if (!cp_parser_next_token_ends_template_argument_p (parser))
8189 cp_parser_error (parser, "expected template-argument");
8190 if (!cp_parser_error_occurred (parser))
8192 /* Figure out what is being referred to. */
8193 argument = cp_parser_lookup_name (parser, argument,
8194 /*is_type=*/false,
8195 /*is_template=*/template_p,
8196 /*is_namespace=*/false,
8197 /*check_dependency=*/true);
8198 if (TREE_CODE (argument) != TEMPLATE_DECL
8199 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8200 cp_parser_error (parser, "expected template-name");
8202 if (cp_parser_parse_definitely (parser))
8203 return argument;
8204 /* It must be a non-type argument. There permitted cases are given
8205 in [temp.arg.nontype]:
8207 -- an integral constant-expression of integral or enumeration
8208 type; or
8210 -- the name of a non-type template-parameter; or
8212 -- the name of an object or function with external linkage...
8214 -- the address of an object or function with external linkage...
8216 -- a pointer to member... */
8217 /* Look for a non-type template parameter. */
8218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8220 cp_parser_parse_tentatively (parser);
8221 argument = cp_parser_primary_expression (parser,
8222 &idk,
8223 &qualifying_class);
8224 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8225 || !cp_parser_next_token_ends_template_argument_p (parser))
8226 cp_parser_simulate_error (parser);
8227 if (cp_parser_parse_definitely (parser))
8228 return argument;
8230 /* If the next token is "&", the argument must be the address of an
8231 object or function with external linkage. */
8232 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8233 if (address_p)
8234 cp_lexer_consume_token (parser->lexer);
8235 /* See if we might have an id-expression. */
8236 token = cp_lexer_peek_token (parser->lexer);
8237 if (token->type == CPP_NAME
8238 || token->keyword == RID_OPERATOR
8239 || token->type == CPP_SCOPE
8240 || token->type == CPP_TEMPLATE_ID
8241 || token->type == CPP_NESTED_NAME_SPECIFIER)
8243 cp_parser_parse_tentatively (parser);
8244 argument = cp_parser_primary_expression (parser,
8245 &idk,
8246 &qualifying_class);
8247 if (cp_parser_error_occurred (parser)
8248 || !cp_parser_next_token_ends_template_argument_p (parser))
8249 cp_parser_abort_tentative_parse (parser);
8250 else
8252 if (qualifying_class)
8253 argument = finish_qualified_id_expr (qualifying_class,
8254 argument,
8255 /*done=*/true,
8256 address_p);
8257 if (TREE_CODE (argument) == VAR_DECL)
8259 /* A variable without external linkage might still be a
8260 valid constant-expression, so no error is issued here
8261 if the external-linkage check fails. */
8262 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8263 cp_parser_simulate_error (parser);
8265 else if (is_overloaded_fn (argument))
8266 /* All overloaded functions are allowed; if the external
8267 linkage test does not pass, an error will be issued
8268 later. */
8270 else if (address_p
8271 && (TREE_CODE (argument) == OFFSET_REF
8272 || TREE_CODE (argument) == SCOPE_REF))
8273 /* A pointer-to-member. */
8275 else
8276 cp_parser_simulate_error (parser);
8278 if (cp_parser_parse_definitely (parser))
8280 if (address_p)
8281 argument = build_x_unary_op (ADDR_EXPR, argument);
8282 return argument;
8286 /* If the argument started with "&", there are no other valid
8287 alternatives at this point. */
8288 if (address_p)
8290 cp_parser_error (parser, "invalid non-type template argument");
8291 return error_mark_node;
8293 /* If the argument wasn't successfully parsed as a type-id followed
8294 by '>>', the argument can only be a constant expression now.
8295 Otherwise, we try parsing the constant-expression tentatively,
8296 because the argument could really be a type-id. */
8297 if (maybe_type_id)
8298 cp_parser_parse_tentatively (parser);
8299 argument = cp_parser_constant_expression (parser,
8300 /*allow_non_constant_p=*/false,
8301 /*non_constant_p=*/NULL);
8302 argument = cp_parser_fold_non_dependent_expr (argument);
8303 if (!maybe_type_id)
8304 return argument;
8305 if (!cp_parser_next_token_ends_template_argument_p (parser))
8306 cp_parser_error (parser, "expected template-argument");
8307 if (cp_parser_parse_definitely (parser))
8308 return argument;
8309 /* We did our best to parse the argument as a non type-id, but that
8310 was the only alternative that matched (albeit with a '>' after
8311 it). We can assume it's just a typo from the user, and a
8312 diagnostic will then be issued. */
8313 return cp_parser_type_id (parser);
8316 /* Parse an explicit-instantiation.
8318 explicit-instantiation:
8319 template declaration
8321 Although the standard says `declaration', what it really means is:
8323 explicit-instantiation:
8324 template decl-specifier-seq [opt] declarator [opt] ;
8326 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8327 supposed to be allowed. A defect report has been filed about this
8328 issue.
8330 GNU Extension:
8332 explicit-instantiation:
8333 storage-class-specifier template
8334 decl-specifier-seq [opt] declarator [opt] ;
8335 function-specifier template
8336 decl-specifier-seq [opt] declarator [opt] ; */
8338 static void
8339 cp_parser_explicit_instantiation (cp_parser* parser)
8341 int declares_class_or_enum;
8342 tree decl_specifiers;
8343 tree attributes;
8344 tree extension_specifier = NULL_TREE;
8346 /* Look for an (optional) storage-class-specifier or
8347 function-specifier. */
8348 if (cp_parser_allow_gnu_extensions_p (parser))
8350 extension_specifier
8351 = cp_parser_storage_class_specifier_opt (parser);
8352 if (!extension_specifier)
8353 extension_specifier = cp_parser_function_specifier_opt (parser);
8356 /* Look for the `template' keyword. */
8357 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8358 /* Let the front end know that we are processing an explicit
8359 instantiation. */
8360 begin_explicit_instantiation ();
8361 /* [temp.explicit] says that we are supposed to ignore access
8362 control while processing explicit instantiation directives. */
8363 push_deferring_access_checks (dk_no_check);
8364 /* Parse a decl-specifier-seq. */
8365 decl_specifiers
8366 = cp_parser_decl_specifier_seq (parser,
8367 CP_PARSER_FLAGS_OPTIONAL,
8368 &attributes,
8369 &declares_class_or_enum);
8370 /* If there was exactly one decl-specifier, and it declared a class,
8371 and there's no declarator, then we have an explicit type
8372 instantiation. */
8373 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8375 tree type;
8377 type = check_tag_decl (decl_specifiers);
8378 /* Turn access control back on for names used during
8379 template instantiation. */
8380 pop_deferring_access_checks ();
8381 if (type)
8382 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8384 else
8386 tree declarator;
8387 tree decl;
8389 /* Parse the declarator. */
8390 declarator
8391 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8392 /*ctor_dtor_or_conv_p=*/NULL,
8393 /*parenthesized_p=*/NULL);
8394 cp_parser_check_for_definition_in_return_type (declarator,
8395 declares_class_or_enum);
8396 if (declarator != error_mark_node)
8398 decl = grokdeclarator (declarator, decl_specifiers,
8399 NORMAL, 0, NULL);
8400 /* Turn access control back on for names used during
8401 template instantiation. */
8402 pop_deferring_access_checks ();
8403 /* Do the explicit instantiation. */
8404 do_decl_instantiation (decl, extension_specifier);
8406 else
8408 pop_deferring_access_checks ();
8409 /* Skip the body of the explicit instantiation. */
8410 cp_parser_skip_to_end_of_statement (parser);
8413 /* We're done with the instantiation. */
8414 end_explicit_instantiation ();
8416 cp_parser_consume_semicolon_at_end_of_statement (parser);
8419 /* Parse an explicit-specialization.
8421 explicit-specialization:
8422 template < > declaration
8424 Although the standard says `declaration', what it really means is:
8426 explicit-specialization:
8427 template <> decl-specifier [opt] init-declarator [opt] ;
8428 template <> function-definition
8429 template <> explicit-specialization
8430 template <> template-declaration */
8432 static void
8433 cp_parser_explicit_specialization (cp_parser* parser)
8435 /* Look for the `template' keyword. */
8436 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8437 /* Look for the `<'. */
8438 cp_parser_require (parser, CPP_LESS, "`<'");
8439 /* Look for the `>'. */
8440 cp_parser_require (parser, CPP_GREATER, "`>'");
8441 /* We have processed another parameter list. */
8442 ++parser->num_template_parameter_lists;
8443 /* Let the front end know that we are beginning a specialization. */
8444 begin_specialization ();
8446 /* If the next keyword is `template', we need to figure out whether
8447 or not we're looking a template-declaration. */
8448 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8450 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8451 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8452 cp_parser_template_declaration_after_export (parser,
8453 /*member_p=*/false);
8454 else
8455 cp_parser_explicit_specialization (parser);
8457 else
8458 /* Parse the dependent declaration. */
8459 cp_parser_single_declaration (parser,
8460 /*member_p=*/false,
8461 /*friend_p=*/NULL);
8463 /* We're done with the specialization. */
8464 end_specialization ();
8465 /* We're done with this parameter list. */
8466 --parser->num_template_parameter_lists;
8469 /* Parse a type-specifier.
8471 type-specifier:
8472 simple-type-specifier
8473 class-specifier
8474 enum-specifier
8475 elaborated-type-specifier
8476 cv-qualifier
8478 GNU Extension:
8480 type-specifier:
8481 __complex__
8483 Returns a representation of the type-specifier. If the
8484 type-specifier is a keyword (like `int' or `const', or
8485 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8486 For a class-specifier, enum-specifier, or elaborated-type-specifier
8487 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8489 If IS_FRIEND is TRUE then this type-specifier is being declared a
8490 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8491 appearing in a decl-specifier-seq.
8493 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8494 class-specifier, enum-specifier, or elaborated-type-specifier, then
8495 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8496 if a type is declared; 2 if it is defined. Otherwise, it is set to
8497 zero.
8499 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8500 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8501 is set to FALSE. */
8503 static tree
8504 cp_parser_type_specifier (cp_parser* parser,
8505 cp_parser_flags flags,
8506 bool is_friend,
8507 bool is_declaration,
8508 int* declares_class_or_enum,
8509 bool* is_cv_qualifier)
8511 tree type_spec = NULL_TREE;
8512 cp_token *token;
8513 enum rid keyword;
8515 /* Assume this type-specifier does not declare a new type. */
8516 if (declares_class_or_enum)
8517 *declares_class_or_enum = false;
8518 /* And that it does not specify a cv-qualifier. */
8519 if (is_cv_qualifier)
8520 *is_cv_qualifier = false;
8521 /* Peek at the next token. */
8522 token = cp_lexer_peek_token (parser->lexer);
8524 /* If we're looking at a keyword, we can use that to guide the
8525 production we choose. */
8526 keyword = token->keyword;
8527 switch (keyword)
8529 /* Any of these indicate either a class-specifier, or an
8530 elaborated-type-specifier. */
8531 case RID_CLASS:
8532 case RID_STRUCT:
8533 case RID_UNION:
8534 case RID_ENUM:
8535 /* Parse tentatively so that we can back up if we don't find a
8536 class-specifier or enum-specifier. */
8537 cp_parser_parse_tentatively (parser);
8538 /* Look for the class-specifier or enum-specifier. */
8539 if (keyword == RID_ENUM)
8540 type_spec = cp_parser_enum_specifier (parser);
8541 else
8542 type_spec = cp_parser_class_specifier (parser);
8544 /* If that worked, we're done. */
8545 if (cp_parser_parse_definitely (parser))
8547 if (declares_class_or_enum)
8548 *declares_class_or_enum = 2;
8549 return type_spec;
8552 /* Fall through. */
8554 case RID_TYPENAME:
8555 /* Look for an elaborated-type-specifier. */
8556 type_spec = cp_parser_elaborated_type_specifier (parser,
8557 is_friend,
8558 is_declaration);
8559 /* We're declaring a class or enum -- unless we're using
8560 `typename'. */
8561 if (declares_class_or_enum && keyword != RID_TYPENAME)
8562 *declares_class_or_enum = 1;
8563 return type_spec;
8565 case RID_CONST:
8566 case RID_VOLATILE:
8567 case RID_RESTRICT:
8568 type_spec = cp_parser_cv_qualifier_opt (parser);
8569 /* Even though we call a routine that looks for an optional
8570 qualifier, we know that there should be one. */
8571 my_friendly_assert (type_spec != NULL, 20000328);
8572 /* This type-specifier was a cv-qualified. */
8573 if (is_cv_qualifier)
8574 *is_cv_qualifier = true;
8576 return type_spec;
8578 case RID_COMPLEX:
8579 /* The `__complex__' keyword is a GNU extension. */
8580 return cp_lexer_consume_token (parser->lexer)->value;
8582 default:
8583 break;
8586 /* If we do not already have a type-specifier, assume we are looking
8587 at a simple-type-specifier. */
8588 type_spec = cp_parser_simple_type_specifier (parser, flags,
8589 /*identifier_p=*/true);
8591 /* If we didn't find a type-specifier, and a type-specifier was not
8592 optional in this context, issue an error message. */
8593 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8595 cp_parser_error (parser, "expected type specifier");
8596 return error_mark_node;
8599 return type_spec;
8602 /* Parse a simple-type-specifier.
8604 simple-type-specifier:
8605 :: [opt] nested-name-specifier [opt] type-name
8606 :: [opt] nested-name-specifier template template-id
8607 char
8608 wchar_t
8609 bool
8610 short
8612 long
8613 signed
8614 unsigned
8615 float
8616 double
8617 void
8619 GNU Extension:
8621 simple-type-specifier:
8622 __typeof__ unary-expression
8623 __typeof__ ( type-id )
8625 For the various keywords, the value returned is simply the
8626 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8627 For the first two productions, and if IDENTIFIER_P is false, the
8628 value returned is the indicated TYPE_DECL. */
8630 static tree
8631 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8632 bool identifier_p)
8634 tree type = NULL_TREE;
8635 cp_token *token;
8637 /* Peek at the next token. */
8638 token = cp_lexer_peek_token (parser->lexer);
8640 /* If we're looking at a keyword, things are easy. */
8641 switch (token->keyword)
8643 case RID_CHAR:
8644 type = char_type_node;
8645 break;
8646 case RID_WCHAR:
8647 type = wchar_type_node;
8648 break;
8649 case RID_BOOL:
8650 type = boolean_type_node;
8651 break;
8652 case RID_SHORT:
8653 type = short_integer_type_node;
8654 break;
8655 case RID_INT:
8656 type = integer_type_node;
8657 break;
8658 case RID_LONG:
8659 type = long_integer_type_node;
8660 break;
8661 case RID_SIGNED:
8662 type = integer_type_node;
8663 break;
8664 case RID_UNSIGNED:
8665 type = unsigned_type_node;
8666 break;
8667 case RID_FLOAT:
8668 type = float_type_node;
8669 break;
8670 case RID_DOUBLE:
8671 type = double_type_node;
8672 break;
8673 case RID_VOID:
8674 type = void_type_node;
8675 break;
8677 case RID_TYPEOF:
8679 tree operand;
8681 /* Consume the `typeof' token. */
8682 cp_lexer_consume_token (parser->lexer);
8683 /* Parse the operand to `typeof'. */
8684 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8685 /* If it is not already a TYPE, take its type. */
8686 if (!TYPE_P (operand))
8687 operand = finish_typeof (operand);
8689 return operand;
8692 default:
8693 break;
8696 /* If the type-specifier was for a built-in type, we're done. */
8697 if (type)
8699 tree id;
8701 /* Consume the token. */
8702 id = cp_lexer_consume_token (parser->lexer)->value;
8703 return identifier_p ? id : TYPE_NAME (type);
8706 /* The type-specifier must be a user-defined type. */
8707 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8709 /* Don't gobble tokens or issue error messages if this is an
8710 optional type-specifier. */
8711 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8712 cp_parser_parse_tentatively (parser);
8714 /* Look for the optional `::' operator. */
8715 cp_parser_global_scope_opt (parser,
8716 /*current_scope_valid_p=*/false);
8717 /* Look for the nested-name specifier. */
8718 cp_parser_nested_name_specifier_opt (parser,
8719 /*typename_keyword_p=*/false,
8720 /*check_dependency_p=*/true,
8721 /*type_p=*/false,
8722 /*is_declaration=*/false);
8723 /* If we have seen a nested-name-specifier, and the next token
8724 is `template', then we are using the template-id production. */
8725 if (parser->scope
8726 && cp_parser_optional_template_keyword (parser))
8728 /* Look for the template-id. */
8729 type = cp_parser_template_id (parser,
8730 /*template_keyword_p=*/true,
8731 /*check_dependency_p=*/true,
8732 /*is_declaration=*/false);
8733 /* If the template-id did not name a type, we are out of
8734 luck. */
8735 if (TREE_CODE (type) != TYPE_DECL)
8737 cp_parser_error (parser, "expected template-id for type");
8738 type = NULL_TREE;
8741 /* Otherwise, look for a type-name. */
8742 else
8743 type = cp_parser_type_name (parser);
8744 /* If it didn't work out, we don't have a TYPE. */
8745 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8746 && !cp_parser_parse_definitely (parser))
8747 type = NULL_TREE;
8750 /* If we didn't get a type-name, issue an error message. */
8751 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8753 cp_parser_error (parser, "expected type-name");
8754 return error_mark_node;
8757 /* There is no valid C++ program where a non-template type is
8758 followed by a "<". That usually indicates that the user thought
8759 that the type was a template. */
8760 if (type && type != error_mark_node)
8761 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8763 return type;
8766 /* Parse a type-name.
8768 type-name:
8769 class-name
8770 enum-name
8771 typedef-name
8773 enum-name:
8774 identifier
8776 typedef-name:
8777 identifier
8779 Returns a TYPE_DECL for the the type. */
8781 static tree
8782 cp_parser_type_name (cp_parser* parser)
8784 tree type_decl;
8785 tree identifier;
8787 /* We can't know yet whether it is a class-name or not. */
8788 cp_parser_parse_tentatively (parser);
8789 /* Try a class-name. */
8790 type_decl = cp_parser_class_name (parser,
8791 /*typename_keyword_p=*/false,
8792 /*template_keyword_p=*/false,
8793 /*type_p=*/false,
8794 /*check_dependency_p=*/true,
8795 /*class_head_p=*/false,
8796 /*is_declaration=*/false);
8797 /* If it's not a class-name, keep looking. */
8798 if (!cp_parser_parse_definitely (parser))
8800 /* It must be a typedef-name or an enum-name. */
8801 identifier = cp_parser_identifier (parser);
8802 if (identifier == error_mark_node)
8803 return error_mark_node;
8805 /* Look up the type-name. */
8806 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8807 /* Issue an error if we did not find a type-name. */
8808 if (TREE_CODE (type_decl) != TYPE_DECL)
8810 if (!cp_parser_simulate_error (parser))
8811 cp_parser_name_lookup_error (parser, identifier, type_decl,
8812 "is not a type");
8813 type_decl = error_mark_node;
8815 /* Remember that the name was used in the definition of the
8816 current class so that we can check later to see if the
8817 meaning would have been different after the class was
8818 entirely defined. */
8819 else if (type_decl != error_mark_node
8820 && !parser->scope)
8821 maybe_note_name_used_in_class (identifier, type_decl);
8824 return type_decl;
8828 /* Parse an elaborated-type-specifier. Note that the grammar given
8829 here incorporates the resolution to DR68.
8831 elaborated-type-specifier:
8832 class-key :: [opt] nested-name-specifier [opt] identifier
8833 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8834 enum :: [opt] nested-name-specifier [opt] identifier
8835 typename :: [opt] nested-name-specifier identifier
8836 typename :: [opt] nested-name-specifier template [opt]
8837 template-id
8839 GNU extension:
8841 elaborated-type-specifier:
8842 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8843 class-key attributes :: [opt] nested-name-specifier [opt]
8844 template [opt] template-id
8845 enum attributes :: [opt] nested-name-specifier [opt] identifier
8847 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8848 declared `friend'. If IS_DECLARATION is TRUE, then this
8849 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8850 something is being declared.
8852 Returns the TYPE specified. */
8854 static tree
8855 cp_parser_elaborated_type_specifier (cp_parser* parser,
8856 bool is_friend,
8857 bool is_declaration)
8859 enum tag_types tag_type;
8860 tree identifier;
8861 tree type = NULL_TREE;
8862 tree attributes = NULL_TREE;
8864 /* See if we're looking at the `enum' keyword. */
8865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8867 /* Consume the `enum' token. */
8868 cp_lexer_consume_token (parser->lexer);
8869 /* Remember that it's an enumeration type. */
8870 tag_type = enum_type;
8871 /* Parse the attributes. */
8872 attributes = cp_parser_attributes_opt (parser);
8874 /* Or, it might be `typename'. */
8875 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8876 RID_TYPENAME))
8878 /* Consume the `typename' token. */
8879 cp_lexer_consume_token (parser->lexer);
8880 /* Remember that it's a `typename' type. */
8881 tag_type = typename_type;
8882 /* The `typename' keyword is only allowed in templates. */
8883 if (!processing_template_decl)
8884 pedwarn ("using `typename' outside of template");
8886 /* Otherwise it must be a class-key. */
8887 else
8889 tag_type = cp_parser_class_key (parser);
8890 if (tag_type == none_type)
8891 return error_mark_node;
8892 /* Parse the attributes. */
8893 attributes = cp_parser_attributes_opt (parser);
8896 /* Look for the `::' operator. */
8897 cp_parser_global_scope_opt (parser,
8898 /*current_scope_valid_p=*/false);
8899 /* Look for the nested-name-specifier. */
8900 if (tag_type == typename_type)
8902 if (cp_parser_nested_name_specifier (parser,
8903 /*typename_keyword_p=*/true,
8904 /*check_dependency_p=*/true,
8905 /*type_p=*/true,
8906 is_declaration)
8907 == error_mark_node)
8908 return error_mark_node;
8910 else
8911 /* Even though `typename' is not present, the proposed resolution
8912 to Core Issue 180 says that in `class A<T>::B', `B' should be
8913 considered a type-name, even if `A<T>' is dependent. */
8914 cp_parser_nested_name_specifier_opt (parser,
8915 /*typename_keyword_p=*/true,
8916 /*check_dependency_p=*/true,
8917 /*type_p=*/true,
8918 is_declaration);
8919 /* For everything but enumeration types, consider a template-id. */
8920 if (tag_type != enum_type)
8922 bool template_p = false;
8923 tree decl;
8925 /* Allow the `template' keyword. */
8926 template_p = cp_parser_optional_template_keyword (parser);
8927 /* If we didn't see `template', we don't know if there's a
8928 template-id or not. */
8929 if (!template_p)
8930 cp_parser_parse_tentatively (parser);
8931 /* Parse the template-id. */
8932 decl = cp_parser_template_id (parser, template_p,
8933 /*check_dependency_p=*/true,
8934 is_declaration);
8935 /* If we didn't find a template-id, look for an ordinary
8936 identifier. */
8937 if (!template_p && !cp_parser_parse_definitely (parser))
8939 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8940 in effect, then we must assume that, upon instantiation, the
8941 template will correspond to a class. */
8942 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8943 && tag_type == typename_type)
8944 type = make_typename_type (parser->scope, decl,
8945 /*complain=*/1);
8946 else
8947 type = TREE_TYPE (decl);
8950 /* For an enumeration type, consider only a plain identifier. */
8951 if (!type)
8953 identifier = cp_parser_identifier (parser);
8955 if (identifier == error_mark_node)
8957 parser->scope = NULL_TREE;
8958 return error_mark_node;
8961 /* For a `typename', we needn't call xref_tag. */
8962 if (tag_type == typename_type)
8963 return make_typename_type (parser->scope, identifier,
8964 /*complain=*/1);
8965 /* Look up a qualified name in the usual way. */
8966 if (parser->scope)
8968 tree decl;
8970 /* In an elaborated-type-specifier, names are assumed to name
8971 types, so we set IS_TYPE to TRUE when calling
8972 cp_parser_lookup_name. */
8973 decl = cp_parser_lookup_name (parser, identifier,
8974 /*is_type=*/true,
8975 /*is_template=*/false,
8976 /*is_namespace=*/false,
8977 /*check_dependency=*/true);
8979 /* If we are parsing friend declaration, DECL may be a
8980 TEMPLATE_DECL tree node here. However, we need to check
8981 whether this TEMPLATE_DECL results in valid code. Consider
8982 the following example:
8984 namespace N {
8985 template <class T> class C {};
8987 class X {
8988 template <class T> friend class N::C; // #1, valid code
8990 template <class T> class Y {
8991 friend class N::C; // #2, invalid code
8994 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8995 name lookup of `N::C'. We see that friend declaration must
8996 be template for the code to be valid. Note that
8997 processing_template_decl does not work here since it is
8998 always 1 for the above two cases. */
9000 decl = (cp_parser_maybe_treat_template_as_class
9001 (decl, /*tag_name_p=*/is_friend
9002 && parser->num_template_parameter_lists));
9004 if (TREE_CODE (decl) != TYPE_DECL)
9006 error ("expected type-name");
9007 return error_mark_node;
9010 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9011 check_elaborated_type_specifier
9012 (tag_type, decl,
9013 (parser->num_template_parameter_lists
9014 || DECL_SELF_REFERENCE_P (decl)));
9016 type = TREE_TYPE (decl);
9018 else
9020 /* An elaborated-type-specifier sometimes introduces a new type and
9021 sometimes names an existing type. Normally, the rule is that it
9022 introduces a new type only if there is not an existing type of
9023 the same name already in scope. For example, given:
9025 struct S {};
9026 void f() { struct S s; }
9028 the `struct S' in the body of `f' is the same `struct S' as in
9029 the global scope; the existing definition is used. However, if
9030 there were no global declaration, this would introduce a new
9031 local class named `S'.
9033 An exception to this rule applies to the following code:
9035 namespace N { struct S; }
9037 Here, the elaborated-type-specifier names a new type
9038 unconditionally; even if there is already an `S' in the
9039 containing scope this declaration names a new type.
9040 This exception only applies if the elaborated-type-specifier
9041 forms the complete declaration:
9043 [class.name]
9045 A declaration consisting solely of `class-key identifier ;' is
9046 either a redeclaration of the name in the current scope or a
9047 forward declaration of the identifier as a class name. It
9048 introduces the name into the current scope.
9050 We are in this situation precisely when the next token is a `;'.
9052 An exception to the exception is that a `friend' declaration does
9053 *not* name a new type; i.e., given:
9055 struct S { friend struct T; };
9057 `T' is not a new type in the scope of `S'.
9059 Also, `new struct S' or `sizeof (struct S)' never results in the
9060 definition of a new type; a new type can only be declared in a
9061 declaration context. */
9063 type = xref_tag (tag_type, identifier,
9064 attributes,
9065 (is_friend
9066 || !is_declaration
9067 || cp_lexer_next_token_is_not (parser->lexer,
9068 CPP_SEMICOLON)),
9069 parser->num_template_parameter_lists);
9072 if (tag_type != enum_type)
9073 cp_parser_check_class_key (tag_type, type);
9075 /* A "<" cannot follow an elaborated type specifier. If that
9076 happens, the user was probably trying to form a template-id. */
9077 cp_parser_check_for_invalid_template_id (parser, type);
9079 return type;
9082 /* Parse an enum-specifier.
9084 enum-specifier:
9085 enum identifier [opt] { enumerator-list [opt] }
9087 Returns an ENUM_TYPE representing the enumeration. */
9089 static tree
9090 cp_parser_enum_specifier (cp_parser* parser)
9092 cp_token *token;
9093 tree identifier = NULL_TREE;
9094 tree type;
9096 /* Look for the `enum' keyword. */
9097 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9098 return error_mark_node;
9099 /* Peek at the next token. */
9100 token = cp_lexer_peek_token (parser->lexer);
9102 /* See if it is an identifier. */
9103 if (token->type == CPP_NAME)
9104 identifier = cp_parser_identifier (parser);
9106 /* Look for the `{'. */
9107 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9108 return error_mark_node;
9110 /* At this point, we're going ahead with the enum-specifier, even
9111 if some other problem occurs. */
9112 cp_parser_commit_to_tentative_parse (parser);
9114 /* Issue an error message if type-definitions are forbidden here. */
9115 cp_parser_check_type_definition (parser);
9117 /* Create the new type. */
9118 type = start_enum (identifier ? identifier : make_anon_name ());
9120 /* Peek at the next token. */
9121 token = cp_lexer_peek_token (parser->lexer);
9122 /* If it's not a `}', then there are some enumerators. */
9123 if (token->type != CPP_CLOSE_BRACE)
9124 cp_parser_enumerator_list (parser, type);
9125 /* Look for the `}'. */
9126 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9128 /* Finish up the enumeration. */
9129 finish_enum (type);
9131 return type;
9134 /* Parse an enumerator-list. The enumerators all have the indicated
9135 TYPE.
9137 enumerator-list:
9138 enumerator-definition
9139 enumerator-list , enumerator-definition */
9141 static void
9142 cp_parser_enumerator_list (cp_parser* parser, tree type)
9144 while (true)
9146 cp_token *token;
9148 /* Parse an enumerator-definition. */
9149 cp_parser_enumerator_definition (parser, type);
9150 /* Peek at the next token. */
9151 token = cp_lexer_peek_token (parser->lexer);
9152 /* If it's not a `,', then we've reached the end of the
9153 list. */
9154 if (token->type != CPP_COMMA)
9155 break;
9156 /* Otherwise, consume the `,' and keep going. */
9157 cp_lexer_consume_token (parser->lexer);
9158 /* If the next token is a `}', there is a trailing comma. */
9159 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9161 if (pedantic && !in_system_header)
9162 pedwarn ("comma at end of enumerator list");
9163 break;
9168 /* Parse an enumerator-definition. The enumerator has the indicated
9169 TYPE.
9171 enumerator-definition:
9172 enumerator
9173 enumerator = constant-expression
9175 enumerator:
9176 identifier */
9178 static void
9179 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9181 cp_token *token;
9182 tree identifier;
9183 tree value;
9185 /* Look for the identifier. */
9186 identifier = cp_parser_identifier (parser);
9187 if (identifier == error_mark_node)
9188 return;
9190 /* Peek at the next token. */
9191 token = cp_lexer_peek_token (parser->lexer);
9192 /* If it's an `=', then there's an explicit value. */
9193 if (token->type == CPP_EQ)
9195 /* Consume the `=' token. */
9196 cp_lexer_consume_token (parser->lexer);
9197 /* Parse the value. */
9198 value = cp_parser_constant_expression (parser,
9199 /*allow_non_constant_p=*/false,
9200 NULL);
9202 else
9203 value = NULL_TREE;
9205 /* Create the enumerator. */
9206 build_enumerator (identifier, value, type);
9209 /* Parse a namespace-name.
9211 namespace-name:
9212 original-namespace-name
9213 namespace-alias
9215 Returns the NAMESPACE_DECL for the namespace. */
9217 static tree
9218 cp_parser_namespace_name (cp_parser* parser)
9220 tree identifier;
9221 tree namespace_decl;
9223 /* Get the name of the namespace. */
9224 identifier = cp_parser_identifier (parser);
9225 if (identifier == error_mark_node)
9226 return error_mark_node;
9228 /* Look up the identifier in the currently active scope. Look only
9229 for namespaces, due to:
9231 [basic.lookup.udir]
9233 When looking up a namespace-name in a using-directive or alias
9234 definition, only namespace names are considered.
9236 And:
9238 [basic.lookup.qual]
9240 During the lookup of a name preceding the :: scope resolution
9241 operator, object, function, and enumerator names are ignored.
9243 (Note that cp_parser_class_or_namespace_name only calls this
9244 function if the token after the name is the scope resolution
9245 operator.) */
9246 namespace_decl = cp_parser_lookup_name (parser, identifier,
9247 /*is_type=*/false,
9248 /*is_template=*/false,
9249 /*is_namespace=*/true,
9250 /*check_dependency=*/true);
9251 /* If it's not a namespace, issue an error. */
9252 if (namespace_decl == error_mark_node
9253 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9255 cp_parser_error (parser, "expected namespace-name");
9256 namespace_decl = error_mark_node;
9259 return namespace_decl;
9262 /* Parse a namespace-definition.
9264 namespace-definition:
9265 named-namespace-definition
9266 unnamed-namespace-definition
9268 named-namespace-definition:
9269 original-namespace-definition
9270 extension-namespace-definition
9272 original-namespace-definition:
9273 namespace identifier { namespace-body }
9275 extension-namespace-definition:
9276 namespace original-namespace-name { namespace-body }
9278 unnamed-namespace-definition:
9279 namespace { namespace-body } */
9281 static void
9282 cp_parser_namespace_definition (cp_parser* parser)
9284 tree identifier;
9286 /* Look for the `namespace' keyword. */
9287 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9289 /* Get the name of the namespace. We do not attempt to distinguish
9290 between an original-namespace-definition and an
9291 extension-namespace-definition at this point. The semantic
9292 analysis routines are responsible for that. */
9293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9294 identifier = cp_parser_identifier (parser);
9295 else
9296 identifier = NULL_TREE;
9298 /* Look for the `{' to start the namespace. */
9299 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9300 /* Start the namespace. */
9301 push_namespace (identifier);
9302 /* Parse the body of the namespace. */
9303 cp_parser_namespace_body (parser);
9304 /* Finish the namespace. */
9305 pop_namespace ();
9306 /* Look for the final `}'. */
9307 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9310 /* Parse a namespace-body.
9312 namespace-body:
9313 declaration-seq [opt] */
9315 static void
9316 cp_parser_namespace_body (cp_parser* parser)
9318 cp_parser_declaration_seq_opt (parser);
9321 /* Parse a namespace-alias-definition.
9323 namespace-alias-definition:
9324 namespace identifier = qualified-namespace-specifier ; */
9326 static void
9327 cp_parser_namespace_alias_definition (cp_parser* parser)
9329 tree identifier;
9330 tree namespace_specifier;
9332 /* Look for the `namespace' keyword. */
9333 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9334 /* Look for the identifier. */
9335 identifier = cp_parser_identifier (parser);
9336 if (identifier == error_mark_node)
9337 return;
9338 /* Look for the `=' token. */
9339 cp_parser_require (parser, CPP_EQ, "`='");
9340 /* Look for the qualified-namespace-specifier. */
9341 namespace_specifier
9342 = cp_parser_qualified_namespace_specifier (parser);
9343 /* Look for the `;' token. */
9344 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9346 /* Register the alias in the symbol table. */
9347 do_namespace_alias (identifier, namespace_specifier);
9350 /* Parse a qualified-namespace-specifier.
9352 qualified-namespace-specifier:
9353 :: [opt] nested-name-specifier [opt] namespace-name
9355 Returns a NAMESPACE_DECL corresponding to the specified
9356 namespace. */
9358 static tree
9359 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9361 /* Look for the optional `::'. */
9362 cp_parser_global_scope_opt (parser,
9363 /*current_scope_valid_p=*/false);
9365 /* Look for the optional nested-name-specifier. */
9366 cp_parser_nested_name_specifier_opt (parser,
9367 /*typename_keyword_p=*/false,
9368 /*check_dependency_p=*/true,
9369 /*type_p=*/false,
9370 /*is_declaration=*/true);
9372 return cp_parser_namespace_name (parser);
9375 /* Parse a using-declaration.
9377 using-declaration:
9378 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9379 using :: unqualified-id ; */
9381 static void
9382 cp_parser_using_declaration (cp_parser* parser)
9384 cp_token *token;
9385 bool typename_p = false;
9386 bool global_scope_p;
9387 tree decl;
9388 tree identifier;
9389 tree scope;
9391 /* Look for the `using' keyword. */
9392 cp_parser_require_keyword (parser, RID_USING, "`using'");
9394 /* Peek at the next token. */
9395 token = cp_lexer_peek_token (parser->lexer);
9396 /* See if it's `typename'. */
9397 if (token->keyword == RID_TYPENAME)
9399 /* Remember that we've seen it. */
9400 typename_p = true;
9401 /* Consume the `typename' token. */
9402 cp_lexer_consume_token (parser->lexer);
9405 /* Look for the optional global scope qualification. */
9406 global_scope_p
9407 = (cp_parser_global_scope_opt (parser,
9408 /*current_scope_valid_p=*/false)
9409 != NULL_TREE);
9411 /* If we saw `typename', or didn't see `::', then there must be a
9412 nested-name-specifier present. */
9413 if (typename_p || !global_scope_p)
9414 cp_parser_nested_name_specifier (parser, typename_p,
9415 /*check_dependency_p=*/true,
9416 /*type_p=*/false,
9417 /*is_declaration=*/true);
9418 /* Otherwise, we could be in either of the two productions. In that
9419 case, treat the nested-name-specifier as optional. */
9420 else
9421 cp_parser_nested_name_specifier_opt (parser,
9422 /*typename_keyword_p=*/false,
9423 /*check_dependency_p=*/true,
9424 /*type_p=*/false,
9425 /*is_declaration=*/true);
9427 /* Parse the unqualified-id. */
9428 identifier = cp_parser_unqualified_id (parser,
9429 /*template_keyword_p=*/false,
9430 /*check_dependency_p=*/true,
9431 /*declarator_p=*/true);
9433 /* The function we call to handle a using-declaration is different
9434 depending on what scope we are in. */
9435 if (identifier == error_mark_node)
9437 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9438 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9439 /* [namespace.udecl]
9441 A using declaration shall not name a template-id. */
9442 error ("a template-id may not appear in a using-declaration");
9443 else
9445 scope = current_scope ();
9446 if (scope && TYPE_P (scope))
9448 /* Create the USING_DECL. */
9449 decl = do_class_using_decl (build_nt (SCOPE_REF,
9450 parser->scope,
9451 identifier));
9452 /* Add it to the list of members in this class. */
9453 finish_member_declaration (decl);
9455 else
9457 decl = cp_parser_lookup_name_simple (parser, identifier);
9458 if (decl == error_mark_node)
9459 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9460 else if (scope)
9461 do_local_using_decl (decl);
9462 else
9463 do_toplevel_using_decl (decl);
9467 /* Look for the final `;'. */
9468 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9471 /* Parse a using-directive.
9473 using-directive:
9474 using namespace :: [opt] nested-name-specifier [opt]
9475 namespace-name ; */
9477 static void
9478 cp_parser_using_directive (cp_parser* parser)
9480 tree namespace_decl;
9481 tree attribs;
9483 /* Look for the `using' keyword. */
9484 cp_parser_require_keyword (parser, RID_USING, "`using'");
9485 /* And the `namespace' keyword. */
9486 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9487 /* Look for the optional `::' operator. */
9488 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9489 /* And the optional nested-name-specifier. */
9490 cp_parser_nested_name_specifier_opt (parser,
9491 /*typename_keyword_p=*/false,
9492 /*check_dependency_p=*/true,
9493 /*type_p=*/false,
9494 /*is_declaration=*/true);
9495 /* Get the namespace being used. */
9496 namespace_decl = cp_parser_namespace_name (parser);
9497 /* And any specified attributes. */
9498 attribs = cp_parser_attributes_opt (parser);
9499 /* Update the symbol table. */
9500 parse_using_directive (namespace_decl, attribs);
9501 /* Look for the final `;'. */
9502 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9505 /* Parse an asm-definition.
9507 asm-definition:
9508 asm ( string-literal ) ;
9510 GNU Extension:
9512 asm-definition:
9513 asm volatile [opt] ( string-literal ) ;
9514 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9515 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9516 : asm-operand-list [opt] ) ;
9517 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9518 : asm-operand-list [opt]
9519 : asm-operand-list [opt] ) ; */
9521 static void
9522 cp_parser_asm_definition (cp_parser* parser)
9524 cp_token *token;
9525 tree string;
9526 tree outputs = NULL_TREE;
9527 tree inputs = NULL_TREE;
9528 tree clobbers = NULL_TREE;
9529 tree asm_stmt;
9530 bool volatile_p = false;
9531 bool extended_p = false;
9533 /* Look for the `asm' keyword. */
9534 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9535 /* See if the next token is `volatile'. */
9536 if (cp_parser_allow_gnu_extensions_p (parser)
9537 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9539 /* Remember that we saw the `volatile' keyword. */
9540 volatile_p = true;
9541 /* Consume the token. */
9542 cp_lexer_consume_token (parser->lexer);
9544 /* Look for the opening `('. */
9545 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9546 /* Look for the string. */
9547 token = cp_parser_require (parser, CPP_STRING, "asm body");
9548 if (!token)
9549 return;
9550 string = token->value;
9551 /* If we're allowing GNU extensions, check for the extended assembly
9552 syntax. Unfortunately, the `:' tokens need not be separated by
9553 a space in C, and so, for compatibility, we tolerate that here
9554 too. Doing that means that we have to treat the `::' operator as
9555 two `:' tokens. */
9556 if (cp_parser_allow_gnu_extensions_p (parser)
9557 && at_function_scope_p ()
9558 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9559 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9561 bool inputs_p = false;
9562 bool clobbers_p = false;
9564 /* The extended syntax was used. */
9565 extended_p = true;
9567 /* Look for outputs. */
9568 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9570 /* Consume the `:'. */
9571 cp_lexer_consume_token (parser->lexer);
9572 /* Parse the output-operands. */
9573 if (cp_lexer_next_token_is_not (parser->lexer,
9574 CPP_COLON)
9575 && cp_lexer_next_token_is_not (parser->lexer,
9576 CPP_SCOPE)
9577 && cp_lexer_next_token_is_not (parser->lexer,
9578 CPP_CLOSE_PAREN))
9579 outputs = cp_parser_asm_operand_list (parser);
9581 /* If the next token is `::', there are no outputs, and the
9582 next token is the beginning of the inputs. */
9583 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9585 /* Consume the `::' token. */
9586 cp_lexer_consume_token (parser->lexer);
9587 /* The inputs are coming next. */
9588 inputs_p = true;
9591 /* Look for inputs. */
9592 if (inputs_p
9593 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9595 if (!inputs_p)
9596 /* Consume the `:'. */
9597 cp_lexer_consume_token (parser->lexer);
9598 /* Parse the output-operands. */
9599 if (cp_lexer_next_token_is_not (parser->lexer,
9600 CPP_COLON)
9601 && cp_lexer_next_token_is_not (parser->lexer,
9602 CPP_SCOPE)
9603 && cp_lexer_next_token_is_not (parser->lexer,
9604 CPP_CLOSE_PAREN))
9605 inputs = cp_parser_asm_operand_list (parser);
9607 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9608 /* The clobbers are coming next. */
9609 clobbers_p = true;
9611 /* Look for clobbers. */
9612 if (clobbers_p
9613 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9615 if (!clobbers_p)
9616 /* Consume the `:'. */
9617 cp_lexer_consume_token (parser->lexer);
9618 /* Parse the clobbers. */
9619 if (cp_lexer_next_token_is_not (parser->lexer,
9620 CPP_CLOSE_PAREN))
9621 clobbers = cp_parser_asm_clobber_list (parser);
9624 /* Look for the closing `)'. */
9625 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9626 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9627 /*consume_paren=*/true);
9628 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9630 /* Create the ASM_STMT. */
9631 if (at_function_scope_p ())
9633 asm_stmt =
9634 finish_asm_stmt (volatile_p
9635 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9636 string, outputs, inputs, clobbers);
9637 /* If the extended syntax was not used, mark the ASM_STMT. */
9638 if (!extended_p)
9639 ASM_INPUT_P (asm_stmt) = 1;
9641 else
9642 assemble_asm (string);
9645 /* Declarators [gram.dcl.decl] */
9647 /* Parse an init-declarator.
9649 init-declarator:
9650 declarator initializer [opt]
9652 GNU Extension:
9654 init-declarator:
9655 declarator asm-specification [opt] attributes [opt] initializer [opt]
9657 function-definition:
9658 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9659 function-body
9660 decl-specifier-seq [opt] declarator function-try-block
9662 GNU Extension:
9664 function-definition:
9665 __extension__ function-definition
9667 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9668 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9669 then this declarator appears in a class scope. The new DECL created
9670 by this declarator is returned.
9672 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9673 for a function-definition here as well. If the declarator is a
9674 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9675 be TRUE upon return. By that point, the function-definition will
9676 have been completely parsed.
9678 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9679 is FALSE. */
9681 static tree
9682 cp_parser_init_declarator (cp_parser* parser,
9683 tree decl_specifiers,
9684 tree prefix_attributes,
9685 bool function_definition_allowed_p,
9686 bool member_p,
9687 int declares_class_or_enum,
9688 bool* function_definition_p)
9690 cp_token *token;
9691 tree declarator;
9692 tree attributes;
9693 tree asm_specification;
9694 tree initializer;
9695 tree decl = NULL_TREE;
9696 tree scope;
9697 bool is_initialized;
9698 bool is_parenthesized_init;
9699 bool is_non_constant_init;
9700 int ctor_dtor_or_conv_p;
9701 bool friend_p;
9703 /* Assume that this is not the declarator for a function
9704 definition. */
9705 if (function_definition_p)
9706 *function_definition_p = false;
9708 /* Defer access checks while parsing the declarator; we cannot know
9709 what names are accessible until we know what is being
9710 declared. */
9711 resume_deferring_access_checks ();
9713 /* Parse the declarator. */
9714 declarator
9715 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9716 &ctor_dtor_or_conv_p,
9717 /*parenthesized_p=*/NULL);
9718 /* Gather up the deferred checks. */
9719 stop_deferring_access_checks ();
9721 /* If the DECLARATOR was erroneous, there's no need to go
9722 further. */
9723 if (declarator == error_mark_node)
9724 return error_mark_node;
9726 cp_parser_check_for_definition_in_return_type (declarator,
9727 declares_class_or_enum);
9729 /* Figure out what scope the entity declared by the DECLARATOR is
9730 located in. `grokdeclarator' sometimes changes the scope, so
9731 we compute it now. */
9732 scope = get_scope_of_declarator (declarator);
9734 /* If we're allowing GNU extensions, look for an asm-specification
9735 and attributes. */
9736 if (cp_parser_allow_gnu_extensions_p (parser))
9738 /* Look for an asm-specification. */
9739 asm_specification = cp_parser_asm_specification_opt (parser);
9740 /* And attributes. */
9741 attributes = cp_parser_attributes_opt (parser);
9743 else
9745 asm_specification = NULL_TREE;
9746 attributes = NULL_TREE;
9749 /* Peek at the next token. */
9750 token = cp_lexer_peek_token (parser->lexer);
9751 /* Check to see if the token indicates the start of a
9752 function-definition. */
9753 if (cp_parser_token_starts_function_definition_p (token))
9755 if (!function_definition_allowed_p)
9757 /* If a function-definition should not appear here, issue an
9758 error message. */
9759 cp_parser_error (parser,
9760 "a function-definition is not allowed here");
9761 return error_mark_node;
9763 else
9765 /* Neither attributes nor an asm-specification are allowed
9766 on a function-definition. */
9767 if (asm_specification)
9768 error ("an asm-specification is not allowed on a function-definition");
9769 if (attributes)
9770 error ("attributes are not allowed on a function-definition");
9771 /* This is a function-definition. */
9772 *function_definition_p = true;
9774 /* Parse the function definition. */
9775 if (member_p)
9776 decl = cp_parser_save_member_function_body (parser,
9777 decl_specifiers,
9778 declarator,
9779 prefix_attributes);
9780 else
9781 decl
9782 = (cp_parser_function_definition_from_specifiers_and_declarator
9783 (parser, decl_specifiers, prefix_attributes, declarator));
9785 return decl;
9789 /* [dcl.dcl]
9791 Only in function declarations for constructors, destructors, and
9792 type conversions can the decl-specifier-seq be omitted.
9794 We explicitly postpone this check past the point where we handle
9795 function-definitions because we tolerate function-definitions
9796 that are missing their return types in some modes. */
9797 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9799 cp_parser_error (parser,
9800 "expected constructor, destructor, or type conversion");
9801 return error_mark_node;
9804 /* An `=' or an `(' indicates an initializer. */
9805 is_initialized = (token->type == CPP_EQ
9806 || token->type == CPP_OPEN_PAREN);
9807 /* If the init-declarator isn't initialized and isn't followed by a
9808 `,' or `;', it's not a valid init-declarator. */
9809 if (!is_initialized
9810 && token->type != CPP_COMMA
9811 && token->type != CPP_SEMICOLON)
9813 cp_parser_error (parser, "expected init-declarator");
9814 return error_mark_node;
9817 /* Because start_decl has side-effects, we should only call it if we
9818 know we're going ahead. By this point, we know that we cannot
9819 possibly be looking at any other construct. */
9820 cp_parser_commit_to_tentative_parse (parser);
9822 /* If the decl specifiers were bad, issue an error now that we're
9823 sure this was intended to be a declarator. Then continue
9824 declaring the variable(s), as int, to try to cut down on further
9825 errors. */
9826 if (decl_specifiers != NULL
9827 && TREE_VALUE (decl_specifiers) == error_mark_node)
9829 cp_parser_error (parser, "invalid type in declaration");
9830 TREE_VALUE (decl_specifiers) = integer_type_node;
9833 /* Check to see whether or not this declaration is a friend. */
9834 friend_p = cp_parser_friend_p (decl_specifiers);
9836 /* Check that the number of template-parameter-lists is OK. */
9837 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9838 return error_mark_node;
9840 /* Enter the newly declared entry in the symbol table. If we're
9841 processing a declaration in a class-specifier, we wait until
9842 after processing the initializer. */
9843 if (!member_p)
9845 if (parser->in_unbraced_linkage_specification_p)
9847 decl_specifiers = tree_cons (error_mark_node,
9848 get_identifier ("extern"),
9849 decl_specifiers);
9850 have_extern_spec = false;
9852 decl = start_decl (declarator, decl_specifiers,
9853 is_initialized, attributes, prefix_attributes);
9856 /* Enter the SCOPE. That way unqualified names appearing in the
9857 initializer will be looked up in SCOPE. */
9858 if (scope)
9859 push_scope (scope);
9861 /* Perform deferred access control checks, now that we know in which
9862 SCOPE the declared entity resides. */
9863 if (!member_p && decl)
9865 tree saved_current_function_decl = NULL_TREE;
9867 /* If the entity being declared is a function, pretend that we
9868 are in its scope. If it is a `friend', it may have access to
9869 things that would not otherwise be accessible. */
9870 if (TREE_CODE (decl) == FUNCTION_DECL)
9872 saved_current_function_decl = current_function_decl;
9873 current_function_decl = decl;
9876 /* Perform the access control checks for the declarator and the
9877 the decl-specifiers. */
9878 perform_deferred_access_checks ();
9880 /* Restore the saved value. */
9881 if (TREE_CODE (decl) == FUNCTION_DECL)
9882 current_function_decl = saved_current_function_decl;
9885 /* Parse the initializer. */
9886 if (is_initialized)
9887 initializer = cp_parser_initializer (parser,
9888 &is_parenthesized_init,
9889 &is_non_constant_init);
9890 else
9892 initializer = NULL_TREE;
9893 is_parenthesized_init = false;
9894 is_non_constant_init = true;
9897 /* The old parser allows attributes to appear after a parenthesized
9898 initializer. Mark Mitchell proposed removing this functionality
9899 on the GCC mailing lists on 2002-08-13. This parser accepts the
9900 attributes -- but ignores them. */
9901 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9902 if (cp_parser_attributes_opt (parser))
9903 warning ("attributes after parenthesized initializer ignored");
9905 /* Leave the SCOPE, now that we have processed the initializer. It
9906 is important to do this before calling cp_finish_decl because it
9907 makes decisions about whether to create DECL_STMTs or not based
9908 on the current scope. */
9909 if (scope)
9910 pop_scope (scope);
9912 /* For an in-class declaration, use `grokfield' to create the
9913 declaration. */
9914 if (member_p)
9916 decl = grokfield (declarator, decl_specifiers,
9917 initializer, /*asmspec=*/NULL_TREE,
9918 /*attributes=*/NULL_TREE);
9919 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9920 cp_parser_save_default_args (parser, decl);
9923 /* Finish processing the declaration. But, skip friend
9924 declarations. */
9925 if (!friend_p && decl)
9926 cp_finish_decl (decl,
9927 initializer,
9928 asm_specification,
9929 /* If the initializer is in parentheses, then this is
9930 a direct-initialization, which means that an
9931 `explicit' constructor is OK. Otherwise, an
9932 `explicit' constructor cannot be used. */
9933 ((is_parenthesized_init || !is_initialized)
9934 ? 0 : LOOKUP_ONLYCONVERTING));
9936 /* Remember whether or not variables were initialized by
9937 constant-expressions. */
9938 if (decl && TREE_CODE (decl) == VAR_DECL
9939 && is_initialized && !is_non_constant_init)
9940 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9942 return decl;
9945 /* Parse a declarator.
9947 declarator:
9948 direct-declarator
9949 ptr-operator declarator
9951 abstract-declarator:
9952 ptr-operator abstract-declarator [opt]
9953 direct-abstract-declarator
9955 GNU Extensions:
9957 declarator:
9958 attributes [opt] direct-declarator
9959 attributes [opt] ptr-operator declarator
9961 abstract-declarator:
9962 attributes [opt] ptr-operator abstract-declarator [opt]
9963 attributes [opt] direct-abstract-declarator
9965 Returns a representation of the declarator. If the declarator has
9966 the form `* declarator', then an INDIRECT_REF is returned, whose
9967 only operand is the sub-declarator. Analogously, `& declarator' is
9968 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9969 used. The first operand is the TYPE for `X'. The second operand
9970 is an INDIRECT_REF whose operand is the sub-declarator.
9972 Otherwise, the representation is as for a direct-declarator.
9974 (It would be better to define a structure type to represent
9975 declarators, rather than abusing `tree' nodes to represent
9976 declarators. That would be much clearer and save some memory.
9977 There is no reason for declarators to be garbage-collected, for
9978 example; they are created during parser and no longer needed after
9979 `grokdeclarator' has been called.)
9981 For a ptr-operator that has the optional cv-qualifier-seq,
9982 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9983 node.
9985 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9986 detect constructor, destructor or conversion operators. It is set
9987 to -1 if the declarator is a name, and +1 if it is a
9988 function. Otherwise it is set to zero. Usually you just want to
9989 test for >0, but internally the negative value is used.
9991 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9992 a decl-specifier-seq unless it declares a constructor, destructor,
9993 or conversion. It might seem that we could check this condition in
9994 semantic analysis, rather than parsing, but that makes it difficult
9995 to handle something like `f()'. We want to notice that there are
9996 no decl-specifiers, and therefore realize that this is an
9997 expression, not a declaration.)
9999 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10000 the declarator is a direct-declarator of the form "(...)". */
10002 static tree
10003 cp_parser_declarator (cp_parser* parser,
10004 cp_parser_declarator_kind dcl_kind,
10005 int* ctor_dtor_or_conv_p,
10006 bool* parenthesized_p)
10008 cp_token *token;
10009 tree declarator;
10010 enum tree_code code;
10011 tree cv_qualifier_seq;
10012 tree class_type;
10013 tree attributes = NULL_TREE;
10015 /* Assume this is not a constructor, destructor, or type-conversion
10016 operator. */
10017 if (ctor_dtor_or_conv_p)
10018 *ctor_dtor_or_conv_p = 0;
10020 if (cp_parser_allow_gnu_extensions_p (parser))
10021 attributes = cp_parser_attributes_opt (parser);
10023 /* Peek at the next token. */
10024 token = cp_lexer_peek_token (parser->lexer);
10026 /* Check for the ptr-operator production. */
10027 cp_parser_parse_tentatively (parser);
10028 /* Parse the ptr-operator. */
10029 code = cp_parser_ptr_operator (parser,
10030 &class_type,
10031 &cv_qualifier_seq);
10032 /* If that worked, then we have a ptr-operator. */
10033 if (cp_parser_parse_definitely (parser))
10035 /* If a ptr-operator was found, then this declarator was not
10036 parenthesized. */
10037 if (parenthesized_p)
10038 *parenthesized_p = true;
10039 /* The dependent declarator is optional if we are parsing an
10040 abstract-declarator. */
10041 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10042 cp_parser_parse_tentatively (parser);
10044 /* Parse the dependent declarator. */
10045 declarator = cp_parser_declarator (parser, dcl_kind,
10046 /*ctor_dtor_or_conv_p=*/NULL,
10047 /*parenthesized_p=*/NULL);
10049 /* If we are parsing an abstract-declarator, we must handle the
10050 case where the dependent declarator is absent. */
10051 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10052 && !cp_parser_parse_definitely (parser))
10053 declarator = NULL_TREE;
10055 /* Build the representation of the ptr-operator. */
10056 if (code == INDIRECT_REF)
10057 declarator = make_pointer_declarator (cv_qualifier_seq,
10058 declarator);
10059 else
10060 declarator = make_reference_declarator (cv_qualifier_seq,
10061 declarator);
10062 /* Handle the pointer-to-member case. */
10063 if (class_type)
10064 declarator = build_nt (SCOPE_REF, class_type, declarator);
10066 /* Everything else is a direct-declarator. */
10067 else
10069 if (parenthesized_p)
10070 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10071 CPP_OPEN_PAREN);
10072 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10073 ctor_dtor_or_conv_p);
10076 if (attributes && declarator != error_mark_node)
10077 declarator = tree_cons (attributes, declarator, NULL_TREE);
10079 return declarator;
10082 /* Parse a direct-declarator or direct-abstract-declarator.
10084 direct-declarator:
10085 declarator-id
10086 direct-declarator ( parameter-declaration-clause )
10087 cv-qualifier-seq [opt]
10088 exception-specification [opt]
10089 direct-declarator [ constant-expression [opt] ]
10090 ( declarator )
10092 direct-abstract-declarator:
10093 direct-abstract-declarator [opt]
10094 ( parameter-declaration-clause )
10095 cv-qualifier-seq [opt]
10096 exception-specification [opt]
10097 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10098 ( abstract-declarator )
10100 Returns a representation of the declarator. DCL_KIND is
10101 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10102 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10103 we are parsing a direct-declarator. It is
10104 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10105 of ambiguity we prefer an abstract declarator, as per
10106 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10107 cp_parser_declarator.
10109 For the declarator-id production, the representation is as for an
10110 id-expression, except that a qualified name is represented as a
10111 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10112 see the documentation of the FUNCTION_DECLARATOR_* macros for
10113 information about how to find the various declarator components.
10114 An array-declarator is represented as an ARRAY_REF. The
10115 direct-declarator is the first operand; the constant-expression
10116 indicating the size of the array is the second operand. */
10118 static tree
10119 cp_parser_direct_declarator (cp_parser* parser,
10120 cp_parser_declarator_kind dcl_kind,
10121 int* ctor_dtor_or_conv_p)
10123 cp_token *token;
10124 tree declarator = NULL_TREE;
10125 tree scope = NULL_TREE;
10126 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10127 bool saved_in_declarator_p = parser->in_declarator_p;
10128 bool first = true;
10130 while (true)
10132 /* Peek at the next token. */
10133 token = cp_lexer_peek_token (parser->lexer);
10134 if (token->type == CPP_OPEN_PAREN)
10136 /* This is either a parameter-declaration-clause, or a
10137 parenthesized declarator. When we know we are parsing a
10138 named declarator, it must be a parenthesized declarator
10139 if FIRST is true. For instance, `(int)' is a
10140 parameter-declaration-clause, with an omitted
10141 direct-abstract-declarator. But `((*))', is a
10142 parenthesized abstract declarator. Finally, when T is a
10143 template parameter `(T)' is a
10144 parameter-declaration-clause, and not a parenthesized
10145 named declarator.
10147 We first try and parse a parameter-declaration-clause,
10148 and then try a nested declarator (if FIRST is true).
10150 It is not an error for it not to be a
10151 parameter-declaration-clause, even when FIRST is
10152 false. Consider,
10154 int i (int);
10155 int i (3);
10157 The first is the declaration of a function while the
10158 second is a the definition of a variable, including its
10159 initializer.
10161 Having seen only the parenthesis, we cannot know which of
10162 these two alternatives should be selected. Even more
10163 complex are examples like:
10165 int i (int (a));
10166 int i (int (3));
10168 The former is a function-declaration; the latter is a
10169 variable initialization.
10171 Thus again, we try a parameter-declaration-clause, and if
10172 that fails, we back out and return. */
10174 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10176 tree params;
10177 unsigned saved_num_template_parameter_lists;
10179 cp_parser_parse_tentatively (parser);
10181 /* Consume the `('. */
10182 cp_lexer_consume_token (parser->lexer);
10183 if (first)
10185 /* If this is going to be an abstract declarator, we're
10186 in a declarator and we can't have default args. */
10187 parser->default_arg_ok_p = false;
10188 parser->in_declarator_p = true;
10191 /* Inside the function parameter list, surrounding
10192 template-parameter-lists do not apply. */
10193 saved_num_template_parameter_lists
10194 = parser->num_template_parameter_lists;
10195 parser->num_template_parameter_lists = 0;
10197 /* Parse the parameter-declaration-clause. */
10198 params = cp_parser_parameter_declaration_clause (parser);
10200 parser->num_template_parameter_lists
10201 = saved_num_template_parameter_lists;
10203 /* If all went well, parse the cv-qualifier-seq and the
10204 exception-specification. */
10205 if (cp_parser_parse_definitely (parser))
10207 tree cv_qualifiers;
10208 tree exception_specification;
10210 if (ctor_dtor_or_conv_p)
10211 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10212 first = false;
10213 /* Consume the `)'. */
10214 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10216 /* Parse the cv-qualifier-seq. */
10217 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10218 /* And the exception-specification. */
10219 exception_specification
10220 = cp_parser_exception_specification_opt (parser);
10222 /* Create the function-declarator. */
10223 declarator = make_call_declarator (declarator,
10224 params,
10225 cv_qualifiers,
10226 exception_specification);
10227 /* Any subsequent parameter lists are to do with
10228 return type, so are not those of the declared
10229 function. */
10230 parser->default_arg_ok_p = false;
10232 /* Repeat the main loop. */
10233 continue;
10237 /* If this is the first, we can try a parenthesized
10238 declarator. */
10239 if (first)
10241 parser->default_arg_ok_p = saved_default_arg_ok_p;
10242 parser->in_declarator_p = saved_in_declarator_p;
10244 /* Consume the `('. */
10245 cp_lexer_consume_token (parser->lexer);
10246 /* Parse the nested declarator. */
10247 declarator
10248 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10249 /*parenthesized_p=*/NULL);
10250 first = false;
10251 /* Expect a `)'. */
10252 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10253 declarator = error_mark_node;
10254 if (declarator == error_mark_node)
10255 break;
10257 goto handle_declarator;
10259 /* Otherwise, we must be done. */
10260 else
10261 break;
10263 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10264 && token->type == CPP_OPEN_SQUARE)
10266 /* Parse an array-declarator. */
10267 tree bounds;
10269 if (ctor_dtor_or_conv_p)
10270 *ctor_dtor_or_conv_p = 0;
10272 first = false;
10273 parser->default_arg_ok_p = false;
10274 parser->in_declarator_p = true;
10275 /* Consume the `['. */
10276 cp_lexer_consume_token (parser->lexer);
10277 /* Peek at the next token. */
10278 token = cp_lexer_peek_token (parser->lexer);
10279 /* If the next token is `]', then there is no
10280 constant-expression. */
10281 if (token->type != CPP_CLOSE_SQUARE)
10283 bool non_constant_p;
10285 bounds
10286 = cp_parser_constant_expression (parser,
10287 /*allow_non_constant=*/true,
10288 &non_constant_p);
10289 if (!non_constant_p)
10290 bounds = cp_parser_fold_non_dependent_expr (bounds);
10292 else
10293 bounds = NULL_TREE;
10294 /* Look for the closing `]'. */
10295 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10297 declarator = error_mark_node;
10298 break;
10301 declarator = build_nt (ARRAY_REF, declarator, bounds);
10303 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10305 /* Parse a declarator-id */
10306 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10307 cp_parser_parse_tentatively (parser);
10308 declarator = cp_parser_declarator_id (parser);
10309 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10311 if (!cp_parser_parse_definitely (parser))
10312 declarator = error_mark_node;
10313 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10315 cp_parser_error (parser, "expected unqualified-id");
10316 declarator = error_mark_node;
10320 if (declarator == error_mark_node)
10321 break;
10323 if (TREE_CODE (declarator) == SCOPE_REF
10324 && !current_scope ())
10326 tree scope = TREE_OPERAND (declarator, 0);
10328 /* In the declaration of a member of a template class
10329 outside of the class itself, the SCOPE will sometimes
10330 be a TYPENAME_TYPE. For example, given:
10332 template <typename T>
10333 int S<T>::R::i = 3;
10335 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10336 this context, we must resolve S<T>::R to an ordinary
10337 type, rather than a typename type.
10339 The reason we normally avoid resolving TYPENAME_TYPEs
10340 is that a specialization of `S' might render
10341 `S<T>::R' not a type. However, if `S' is
10342 specialized, then this `i' will not be used, so there
10343 is no harm in resolving the types here. */
10344 if (TREE_CODE (scope) == TYPENAME_TYPE)
10346 tree type;
10348 /* Resolve the TYPENAME_TYPE. */
10349 type = resolve_typename_type (scope,
10350 /*only_current_p=*/false);
10351 /* If that failed, the declarator is invalid. */
10352 if (type != error_mark_node)
10353 scope = type;
10354 /* Build a new DECLARATOR. */
10355 declarator = build_nt (SCOPE_REF,
10356 scope,
10357 TREE_OPERAND (declarator, 1));
10361 /* Check to see whether the declarator-id names a constructor,
10362 destructor, or conversion. */
10363 if (declarator && ctor_dtor_or_conv_p
10364 && ((TREE_CODE (declarator) == SCOPE_REF
10365 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10366 || (TREE_CODE (declarator) != SCOPE_REF
10367 && at_class_scope_p ())))
10369 tree unqualified_name;
10370 tree class_type;
10372 /* Get the unqualified part of the name. */
10373 if (TREE_CODE (declarator) == SCOPE_REF)
10375 class_type = TREE_OPERAND (declarator, 0);
10376 unqualified_name = TREE_OPERAND (declarator, 1);
10378 else
10380 class_type = current_class_type;
10381 unqualified_name = declarator;
10384 /* See if it names ctor, dtor or conv. */
10385 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10386 || IDENTIFIER_TYPENAME_P (unqualified_name)
10387 || constructor_name_p (unqualified_name, class_type))
10388 *ctor_dtor_or_conv_p = -1;
10391 handle_declarator:;
10392 scope = get_scope_of_declarator (declarator);
10393 if (scope)
10394 /* Any names that appear after the declarator-id for a member
10395 are looked up in the containing scope. */
10396 push_scope (scope);
10397 parser->in_declarator_p = true;
10398 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10399 || (declarator
10400 && (TREE_CODE (declarator) == SCOPE_REF
10401 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10402 /* Default args are only allowed on function
10403 declarations. */
10404 parser->default_arg_ok_p = saved_default_arg_ok_p;
10405 else
10406 parser->default_arg_ok_p = false;
10408 first = false;
10410 /* We're done. */
10411 else
10412 break;
10415 /* For an abstract declarator, we might wind up with nothing at this
10416 point. That's an error; the declarator is not optional. */
10417 if (!declarator)
10418 cp_parser_error (parser, "expected declarator");
10420 /* If we entered a scope, we must exit it now. */
10421 if (scope)
10422 pop_scope (scope);
10424 parser->default_arg_ok_p = saved_default_arg_ok_p;
10425 parser->in_declarator_p = saved_in_declarator_p;
10427 return declarator;
10430 /* Parse a ptr-operator.
10432 ptr-operator:
10433 * cv-qualifier-seq [opt]
10435 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10437 GNU Extension:
10439 ptr-operator:
10440 & cv-qualifier-seq [opt]
10442 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10443 used. Returns ADDR_EXPR if a reference was used. In the
10444 case of a pointer-to-member, *TYPE is filled in with the
10445 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10446 with the cv-qualifier-seq, or NULL_TREE, if there are no
10447 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10449 static enum tree_code
10450 cp_parser_ptr_operator (cp_parser* parser,
10451 tree* type,
10452 tree* cv_qualifier_seq)
10454 enum tree_code code = ERROR_MARK;
10455 cp_token *token;
10457 /* Assume that it's not a pointer-to-member. */
10458 *type = NULL_TREE;
10459 /* And that there are no cv-qualifiers. */
10460 *cv_qualifier_seq = NULL_TREE;
10462 /* Peek at the next token. */
10463 token = cp_lexer_peek_token (parser->lexer);
10464 /* If it's a `*' or `&' we have a pointer or reference. */
10465 if (token->type == CPP_MULT || token->type == CPP_AND)
10467 /* Remember which ptr-operator we were processing. */
10468 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10470 /* Consume the `*' or `&'. */
10471 cp_lexer_consume_token (parser->lexer);
10473 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10474 `&', if we are allowing GNU extensions. (The only qualifier
10475 that can legally appear after `&' is `restrict', but that is
10476 enforced during semantic analysis. */
10477 if (code == INDIRECT_REF
10478 || cp_parser_allow_gnu_extensions_p (parser))
10479 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10481 else
10483 /* Try the pointer-to-member case. */
10484 cp_parser_parse_tentatively (parser);
10485 /* Look for the optional `::' operator. */
10486 cp_parser_global_scope_opt (parser,
10487 /*current_scope_valid_p=*/false);
10488 /* Look for the nested-name specifier. */
10489 cp_parser_nested_name_specifier (parser,
10490 /*typename_keyword_p=*/false,
10491 /*check_dependency_p=*/true,
10492 /*type_p=*/false,
10493 /*is_declaration=*/false);
10494 /* If we found it, and the next token is a `*', then we are
10495 indeed looking at a pointer-to-member operator. */
10496 if (!cp_parser_error_occurred (parser)
10497 && cp_parser_require (parser, CPP_MULT, "`*'"))
10499 /* The type of which the member is a member is given by the
10500 current SCOPE. */
10501 *type = parser->scope;
10502 /* The next name will not be qualified. */
10503 parser->scope = NULL_TREE;
10504 parser->qualifying_scope = NULL_TREE;
10505 parser->object_scope = NULL_TREE;
10506 /* Indicate that the `*' operator was used. */
10507 code = INDIRECT_REF;
10508 /* Look for the optional cv-qualifier-seq. */
10509 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10511 /* If that didn't work we don't have a ptr-operator. */
10512 if (!cp_parser_parse_definitely (parser))
10513 cp_parser_error (parser, "expected ptr-operator");
10516 return code;
10519 /* Parse an (optional) cv-qualifier-seq.
10521 cv-qualifier-seq:
10522 cv-qualifier cv-qualifier-seq [opt]
10524 Returns a TREE_LIST. The TREE_VALUE of each node is the
10525 representation of a cv-qualifier. */
10527 static tree
10528 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10530 tree cv_qualifiers = NULL_TREE;
10532 while (true)
10534 tree cv_qualifier;
10536 /* Look for the next cv-qualifier. */
10537 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10538 /* If we didn't find one, we're done. */
10539 if (!cv_qualifier)
10540 break;
10542 /* Add this cv-qualifier to the list. */
10543 cv_qualifiers
10544 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10547 /* We built up the list in reverse order. */
10548 return nreverse (cv_qualifiers);
10551 /* Parse an (optional) cv-qualifier.
10553 cv-qualifier:
10554 const
10555 volatile
10557 GNU Extension:
10559 cv-qualifier:
10560 __restrict__ */
10562 static tree
10563 cp_parser_cv_qualifier_opt (cp_parser* parser)
10565 cp_token *token;
10566 tree cv_qualifier = NULL_TREE;
10568 /* Peek at the next token. */
10569 token = cp_lexer_peek_token (parser->lexer);
10570 /* See if it's a cv-qualifier. */
10571 switch (token->keyword)
10573 case RID_CONST:
10574 case RID_VOLATILE:
10575 case RID_RESTRICT:
10576 /* Save the value of the token. */
10577 cv_qualifier = token->value;
10578 /* Consume the token. */
10579 cp_lexer_consume_token (parser->lexer);
10580 break;
10582 default:
10583 break;
10586 return cv_qualifier;
10589 /* Parse a declarator-id.
10591 declarator-id:
10592 id-expression
10593 :: [opt] nested-name-specifier [opt] type-name
10595 In the `id-expression' case, the value returned is as for
10596 cp_parser_id_expression if the id-expression was an unqualified-id.
10597 If the id-expression was a qualified-id, then a SCOPE_REF is
10598 returned. The first operand is the scope (either a NAMESPACE_DECL
10599 or TREE_TYPE), but the second is still just a representation of an
10600 unqualified-id. */
10602 static tree
10603 cp_parser_declarator_id (cp_parser* parser)
10605 tree id_expression;
10607 /* The expression must be an id-expression. Assume that qualified
10608 names are the names of types so that:
10610 template <class T>
10611 int S<T>::R::i = 3;
10613 will work; we must treat `S<T>::R' as the name of a type.
10614 Similarly, assume that qualified names are templates, where
10615 required, so that:
10617 template <class T>
10618 int S<T>::R<T>::i = 3;
10620 will work, too. */
10621 id_expression = cp_parser_id_expression (parser,
10622 /*template_keyword_p=*/false,
10623 /*check_dependency_p=*/false,
10624 /*template_p=*/NULL,
10625 /*declarator_p=*/true);
10626 /* If the name was qualified, create a SCOPE_REF to represent
10627 that. */
10628 if (parser->scope)
10630 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10631 parser->scope = NULL_TREE;
10634 return id_expression;
10637 /* Parse a type-id.
10639 type-id:
10640 type-specifier-seq abstract-declarator [opt]
10642 Returns the TYPE specified. */
10644 static tree
10645 cp_parser_type_id (cp_parser* parser)
10647 tree type_specifier_seq;
10648 tree abstract_declarator;
10650 /* Parse the type-specifier-seq. */
10651 type_specifier_seq
10652 = cp_parser_type_specifier_seq (parser);
10653 if (type_specifier_seq == error_mark_node)
10654 return error_mark_node;
10656 /* There might or might not be an abstract declarator. */
10657 cp_parser_parse_tentatively (parser);
10658 /* Look for the declarator. */
10659 abstract_declarator
10660 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10661 /*parenthesized_p=*/NULL);
10662 /* Check to see if there really was a declarator. */
10663 if (!cp_parser_parse_definitely (parser))
10664 abstract_declarator = NULL_TREE;
10666 return groktypename (build_tree_list (type_specifier_seq,
10667 abstract_declarator));
10670 /* Parse a type-specifier-seq.
10672 type-specifier-seq:
10673 type-specifier type-specifier-seq [opt]
10675 GNU extension:
10677 type-specifier-seq:
10678 attributes type-specifier-seq [opt]
10680 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10681 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10683 static tree
10684 cp_parser_type_specifier_seq (cp_parser* parser)
10686 bool seen_type_specifier = false;
10687 tree type_specifier_seq = NULL_TREE;
10689 /* Parse the type-specifiers and attributes. */
10690 while (true)
10692 tree type_specifier;
10694 /* Check for attributes first. */
10695 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10697 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10698 NULL_TREE,
10699 type_specifier_seq);
10700 continue;
10703 /* After the first type-specifier, others are optional. */
10704 if (seen_type_specifier)
10705 cp_parser_parse_tentatively (parser);
10706 /* Look for the type-specifier. */
10707 type_specifier = cp_parser_type_specifier (parser,
10708 CP_PARSER_FLAGS_NONE,
10709 /*is_friend=*/false,
10710 /*is_declaration=*/false,
10711 NULL,
10712 NULL);
10713 /* If the first type-specifier could not be found, this is not a
10714 type-specifier-seq at all. */
10715 if (!seen_type_specifier && type_specifier == error_mark_node)
10716 return error_mark_node;
10717 /* If subsequent type-specifiers could not be found, the
10718 type-specifier-seq is complete. */
10719 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10720 break;
10722 /* Add the new type-specifier to the list. */
10723 type_specifier_seq
10724 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10725 seen_type_specifier = true;
10728 /* We built up the list in reverse order. */
10729 return nreverse (type_specifier_seq);
10732 /* Parse a parameter-declaration-clause.
10734 parameter-declaration-clause:
10735 parameter-declaration-list [opt] ... [opt]
10736 parameter-declaration-list , ...
10738 Returns a representation for the parameter declarations. Each node
10739 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10740 representation.) If the parameter-declaration-clause ends with an
10741 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10742 list. A return value of NULL_TREE indicates a
10743 parameter-declaration-clause consisting only of an ellipsis. */
10745 static tree
10746 cp_parser_parameter_declaration_clause (cp_parser* parser)
10748 tree parameters;
10749 cp_token *token;
10750 bool ellipsis_p;
10752 /* Peek at the next token. */
10753 token = cp_lexer_peek_token (parser->lexer);
10754 /* Check for trivial parameter-declaration-clauses. */
10755 if (token->type == CPP_ELLIPSIS)
10757 /* Consume the `...' token. */
10758 cp_lexer_consume_token (parser->lexer);
10759 return NULL_TREE;
10761 else if (token->type == CPP_CLOSE_PAREN)
10762 /* There are no parameters. */
10764 #ifndef NO_IMPLICIT_EXTERN_C
10765 if (in_system_header && current_class_type == NULL
10766 && current_lang_name == lang_name_c)
10767 return NULL_TREE;
10768 else
10769 #endif
10770 return void_list_node;
10772 /* Check for `(void)', too, which is a special case. */
10773 else if (token->keyword == RID_VOID
10774 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10775 == CPP_CLOSE_PAREN))
10777 /* Consume the `void' token. */
10778 cp_lexer_consume_token (parser->lexer);
10779 /* There are no parameters. */
10780 return void_list_node;
10783 /* Parse the parameter-declaration-list. */
10784 parameters = cp_parser_parameter_declaration_list (parser);
10785 /* If a parse error occurred while parsing the
10786 parameter-declaration-list, then the entire
10787 parameter-declaration-clause is erroneous. */
10788 if (parameters == error_mark_node)
10789 return error_mark_node;
10791 /* Peek at the next token. */
10792 token = cp_lexer_peek_token (parser->lexer);
10793 /* If it's a `,', the clause should terminate with an ellipsis. */
10794 if (token->type == CPP_COMMA)
10796 /* Consume the `,'. */
10797 cp_lexer_consume_token (parser->lexer);
10798 /* Expect an ellipsis. */
10799 ellipsis_p
10800 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10802 /* It might also be `...' if the optional trailing `,' was
10803 omitted. */
10804 else if (token->type == CPP_ELLIPSIS)
10806 /* Consume the `...' token. */
10807 cp_lexer_consume_token (parser->lexer);
10808 /* And remember that we saw it. */
10809 ellipsis_p = true;
10811 else
10812 ellipsis_p = false;
10814 /* Finish the parameter list. */
10815 return finish_parmlist (parameters, ellipsis_p);
10818 /* Parse a parameter-declaration-list.
10820 parameter-declaration-list:
10821 parameter-declaration
10822 parameter-declaration-list , parameter-declaration
10824 Returns a representation of the parameter-declaration-list, as for
10825 cp_parser_parameter_declaration_clause. However, the
10826 `void_list_node' is never appended to the list. */
10828 static tree
10829 cp_parser_parameter_declaration_list (cp_parser* parser)
10831 tree parameters = NULL_TREE;
10833 /* Look for more parameters. */
10834 while (true)
10836 tree parameter;
10837 bool parenthesized_p;
10838 /* Parse the parameter. */
10839 parameter
10840 = cp_parser_parameter_declaration (parser,
10841 /*template_parm_p=*/false,
10842 &parenthesized_p);
10844 /* If a parse error occurred parsing the parameter declaration,
10845 then the entire parameter-declaration-list is erroneous. */
10846 if (parameter == error_mark_node)
10848 parameters = error_mark_node;
10849 break;
10851 /* Add the new parameter to the list. */
10852 TREE_CHAIN (parameter) = parameters;
10853 parameters = parameter;
10855 /* Peek at the next token. */
10856 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10857 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10858 /* The parameter-declaration-list is complete. */
10859 break;
10860 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10862 cp_token *token;
10864 /* Peek at the next token. */
10865 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10866 /* If it's an ellipsis, then the list is complete. */
10867 if (token->type == CPP_ELLIPSIS)
10868 break;
10869 /* Otherwise, there must be more parameters. Consume the
10870 `,'. */
10871 cp_lexer_consume_token (parser->lexer);
10872 /* When parsing something like:
10874 int i(float f, double d)
10876 we can tell after seeing the declaration for "f" that we
10877 are not looking at an initialization of a variable "i",
10878 but rather at the declaration of a function "i".
10880 Due to the fact that the parsing of template arguments
10881 (as specified to a template-id) requires backtracking we
10882 cannot use this technique when inside a template argument
10883 list. */
10884 if (!parser->in_template_argument_list_p
10885 && cp_parser_parsing_tentatively (parser)
10886 && !cp_parser_committed_to_tentative_parse (parser)
10887 /* However, a parameter-declaration of the form
10888 "foat(f)" (which is a valid declaration of a
10889 parameter "f") can also be interpreted as an
10890 expression (the conversion of "f" to "float"). */
10891 && !parenthesized_p)
10892 cp_parser_commit_to_tentative_parse (parser);
10894 else
10896 cp_parser_error (parser, "expected `,' or `...'");
10897 if (!cp_parser_parsing_tentatively (parser)
10898 || cp_parser_committed_to_tentative_parse (parser))
10899 cp_parser_skip_to_closing_parenthesis (parser,
10900 /*recovering=*/true,
10901 /*or_comma=*/false,
10902 /*consume_paren=*/false);
10903 break;
10907 /* We built up the list in reverse order; straighten it out now. */
10908 return nreverse (parameters);
10911 /* Parse a parameter declaration.
10913 parameter-declaration:
10914 decl-specifier-seq declarator
10915 decl-specifier-seq declarator = assignment-expression
10916 decl-specifier-seq abstract-declarator [opt]
10917 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10919 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10920 declares a template parameter. (In that case, a non-nested `>'
10921 token encountered during the parsing of the assignment-expression
10922 is not interpreted as a greater-than operator.)
10924 Returns a TREE_LIST representing the parameter-declaration. The
10925 TREE_PURPOSE is the default argument expression, or NULL_TREE if
10926 there is no default argument. The TREE_VALUE is a representation
10927 of the decl-specifier-seq and declarator. In particular, the
10928 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
10929 decl-specifier-seq and whose TREE_VALUE represents the declarator.
10930 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10931 the declarator is of the form "(p)". */
10933 static tree
10934 cp_parser_parameter_declaration (cp_parser *parser,
10935 bool template_parm_p,
10936 bool *parenthesized_p)
10938 int declares_class_or_enum;
10939 bool greater_than_is_operator_p;
10940 tree decl_specifiers;
10941 tree attributes;
10942 tree declarator;
10943 tree default_argument;
10944 tree parameter;
10945 cp_token *token;
10946 const char *saved_message;
10948 /* In a template parameter, `>' is not an operator.
10950 [temp.param]
10952 When parsing a default template-argument for a non-type
10953 template-parameter, the first non-nested `>' is taken as the end
10954 of the template parameter-list rather than a greater-than
10955 operator. */
10956 greater_than_is_operator_p = !template_parm_p;
10958 /* Type definitions may not appear in parameter types. */
10959 saved_message = parser->type_definition_forbidden_message;
10960 parser->type_definition_forbidden_message
10961 = "types may not be defined in parameter types";
10963 /* Parse the declaration-specifiers. */
10964 decl_specifiers
10965 = cp_parser_decl_specifier_seq (parser,
10966 CP_PARSER_FLAGS_NONE,
10967 &attributes,
10968 &declares_class_or_enum);
10969 /* If an error occurred, there's no reason to attempt to parse the
10970 rest of the declaration. */
10971 if (cp_parser_error_occurred (parser))
10973 parser->type_definition_forbidden_message = saved_message;
10974 return error_mark_node;
10977 /* Peek at the next token. */
10978 token = cp_lexer_peek_token (parser->lexer);
10979 /* If the next token is a `)', `,', `=', `>', or `...', then there
10980 is no declarator. */
10981 if (token->type == CPP_CLOSE_PAREN
10982 || token->type == CPP_COMMA
10983 || token->type == CPP_EQ
10984 || token->type == CPP_ELLIPSIS
10985 || token->type == CPP_GREATER)
10987 declarator = NULL_TREE;
10988 if (parenthesized_p)
10989 *parenthesized_p = false;
10991 /* Otherwise, there should be a declarator. */
10992 else
10994 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10995 parser->default_arg_ok_p = false;
10997 /* After seeing a decl-specifier-seq, if the next token is not a
10998 "(", there is no possibility that the code is a valid
10999 expression. Therefore, if parsing tentatively, we commit at
11000 this point. */
11001 if (!parser->in_template_argument_list_p
11002 /* In an expression context, having seen:
11004 (int((char *)...
11006 we cannot be sure whether we are looking at a
11007 function-type (taking a "char*" as a parameter) or a cast
11008 of some object of type "char*" to "int". */
11009 && !parser->in_type_id_in_expr_p
11010 && cp_parser_parsing_tentatively (parser)
11011 && !cp_parser_committed_to_tentative_parse (parser)
11012 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11013 cp_parser_commit_to_tentative_parse (parser);
11014 /* Parse the declarator. */
11015 declarator = cp_parser_declarator (parser,
11016 CP_PARSER_DECLARATOR_EITHER,
11017 /*ctor_dtor_or_conv_p=*/NULL,
11018 parenthesized_p);
11019 parser->default_arg_ok_p = saved_default_arg_ok_p;
11020 /* After the declarator, allow more attributes. */
11021 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11024 /* The restriction on defining new types applies only to the type
11025 of the parameter, not to the default argument. */
11026 parser->type_definition_forbidden_message = saved_message;
11028 /* If the next token is `=', then process a default argument. */
11029 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11031 bool saved_greater_than_is_operator_p;
11032 /* Consume the `='. */
11033 cp_lexer_consume_token (parser->lexer);
11035 /* If we are defining a class, then the tokens that make up the
11036 default argument must be saved and processed later. */
11037 if (!template_parm_p && at_class_scope_p ()
11038 && TYPE_BEING_DEFINED (current_class_type))
11040 unsigned depth = 0;
11042 /* Create a DEFAULT_ARG to represented the unparsed default
11043 argument. */
11044 default_argument = make_node (DEFAULT_ARG);
11045 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11047 /* Add tokens until we have processed the entire default
11048 argument. */
11049 while (true)
11051 bool done = false;
11052 cp_token *token;
11054 /* Peek at the next token. */
11055 token = cp_lexer_peek_token (parser->lexer);
11056 /* What we do depends on what token we have. */
11057 switch (token->type)
11059 /* In valid code, a default argument must be
11060 immediately followed by a `,' `)', or `...'. */
11061 case CPP_COMMA:
11062 case CPP_CLOSE_PAREN:
11063 case CPP_ELLIPSIS:
11064 /* If we run into a non-nested `;', `}', or `]',
11065 then the code is invalid -- but the default
11066 argument is certainly over. */
11067 case CPP_SEMICOLON:
11068 case CPP_CLOSE_BRACE:
11069 case CPP_CLOSE_SQUARE:
11070 if (depth == 0)
11071 done = true;
11072 /* Update DEPTH, if necessary. */
11073 else if (token->type == CPP_CLOSE_PAREN
11074 || token->type == CPP_CLOSE_BRACE
11075 || token->type == CPP_CLOSE_SQUARE)
11076 --depth;
11077 break;
11079 case CPP_OPEN_PAREN:
11080 case CPP_OPEN_SQUARE:
11081 case CPP_OPEN_BRACE:
11082 ++depth;
11083 break;
11085 case CPP_GREATER:
11086 /* If we see a non-nested `>', and `>' is not an
11087 operator, then it marks the end of the default
11088 argument. */
11089 if (!depth && !greater_than_is_operator_p)
11090 done = true;
11091 break;
11093 /* If we run out of tokens, issue an error message. */
11094 case CPP_EOF:
11095 error ("file ends in default argument");
11096 done = true;
11097 break;
11099 case CPP_NAME:
11100 case CPP_SCOPE:
11101 /* In these cases, we should look for template-ids.
11102 For example, if the default argument is
11103 `X<int, double>()', we need to do name lookup to
11104 figure out whether or not `X' is a template; if
11105 so, the `,' does not end the default argument.
11107 That is not yet done. */
11108 break;
11110 default:
11111 break;
11114 /* If we've reached the end, stop. */
11115 if (done)
11116 break;
11118 /* Add the token to the token block. */
11119 token = cp_lexer_consume_token (parser->lexer);
11120 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11121 token);
11124 /* Outside of a class definition, we can just parse the
11125 assignment-expression. */
11126 else
11128 bool saved_local_variables_forbidden_p;
11130 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11131 set correctly. */
11132 saved_greater_than_is_operator_p
11133 = parser->greater_than_is_operator_p;
11134 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11135 /* Local variable names (and the `this' keyword) may not
11136 appear in a default argument. */
11137 saved_local_variables_forbidden_p
11138 = parser->local_variables_forbidden_p;
11139 parser->local_variables_forbidden_p = true;
11140 /* Parse the assignment-expression. */
11141 default_argument = cp_parser_assignment_expression (parser);
11142 /* Restore saved state. */
11143 parser->greater_than_is_operator_p
11144 = saved_greater_than_is_operator_p;
11145 parser->local_variables_forbidden_p
11146 = saved_local_variables_forbidden_p;
11148 if (!parser->default_arg_ok_p)
11150 if (!flag_pedantic_errors)
11151 warning ("deprecated use of default argument for parameter of non-function");
11152 else
11154 error ("default arguments are only permitted for function parameters");
11155 default_argument = NULL_TREE;
11159 else
11160 default_argument = NULL_TREE;
11162 /* Create the representation of the parameter. */
11163 if (attributes)
11164 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11165 parameter = build_tree_list (default_argument,
11166 build_tree_list (decl_specifiers,
11167 declarator));
11169 return parameter;
11172 /* Parse a function-body.
11174 function-body:
11175 compound_statement */
11177 static void
11178 cp_parser_function_body (cp_parser *parser)
11180 cp_parser_compound_statement (parser, false);
11183 /* Parse a ctor-initializer-opt followed by a function-body. Return
11184 true if a ctor-initializer was present. */
11186 static bool
11187 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11189 tree body;
11190 bool ctor_initializer_p;
11192 /* Begin the function body. */
11193 body = begin_function_body ();
11194 /* Parse the optional ctor-initializer. */
11195 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11196 /* Parse the function-body. */
11197 cp_parser_function_body (parser);
11198 /* Finish the function body. */
11199 finish_function_body (body);
11201 return ctor_initializer_p;
11204 /* Parse an initializer.
11206 initializer:
11207 = initializer-clause
11208 ( expression-list )
11210 Returns a expression representing the initializer. If no
11211 initializer is present, NULL_TREE is returned.
11213 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11214 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11215 set to FALSE if there is no initializer present. If there is an
11216 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11217 is set to true; otherwise it is set to false. */
11219 static tree
11220 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11221 bool* non_constant_p)
11223 cp_token *token;
11224 tree init;
11226 /* Peek at the next token. */
11227 token = cp_lexer_peek_token (parser->lexer);
11229 /* Let our caller know whether or not this initializer was
11230 parenthesized. */
11231 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11232 /* Assume that the initializer is constant. */
11233 *non_constant_p = false;
11235 if (token->type == CPP_EQ)
11237 /* Consume the `='. */
11238 cp_lexer_consume_token (parser->lexer);
11239 /* Parse the initializer-clause. */
11240 init = cp_parser_initializer_clause (parser, non_constant_p);
11242 else if (token->type == CPP_OPEN_PAREN)
11243 init = cp_parser_parenthesized_expression_list (parser, false,
11244 non_constant_p);
11245 else
11247 /* Anything else is an error. */
11248 cp_parser_error (parser, "expected initializer");
11249 init = error_mark_node;
11252 return init;
11255 /* Parse an initializer-clause.
11257 initializer-clause:
11258 assignment-expression
11259 { initializer-list , [opt] }
11262 Returns an expression representing the initializer.
11264 If the `assignment-expression' production is used the value
11265 returned is simply a representation for the expression.
11267 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11268 the elements of the initializer-list (or NULL_TREE, if the last
11269 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11270 NULL_TREE. There is no way to detect whether or not the optional
11271 trailing `,' was provided. NON_CONSTANT_P is as for
11272 cp_parser_initializer. */
11274 static tree
11275 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11277 tree initializer;
11279 /* If it is not a `{', then we are looking at an
11280 assignment-expression. */
11281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11282 initializer
11283 = cp_parser_constant_expression (parser,
11284 /*allow_non_constant_p=*/true,
11285 non_constant_p);
11286 else
11288 /* Consume the `{' token. */
11289 cp_lexer_consume_token (parser->lexer);
11290 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11291 initializer = make_node (CONSTRUCTOR);
11292 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11293 necessary, but check_initializer depends upon it, for
11294 now. */
11295 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11296 /* If it's not a `}', then there is a non-trivial initializer. */
11297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11299 /* Parse the initializer list. */
11300 CONSTRUCTOR_ELTS (initializer)
11301 = cp_parser_initializer_list (parser, non_constant_p);
11302 /* A trailing `,' token is allowed. */
11303 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11304 cp_lexer_consume_token (parser->lexer);
11306 /* Now, there should be a trailing `}'. */
11307 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11310 return initializer;
11313 /* Parse an initializer-list.
11315 initializer-list:
11316 initializer-clause
11317 initializer-list , initializer-clause
11319 GNU Extension:
11321 initializer-list:
11322 identifier : initializer-clause
11323 initializer-list, identifier : initializer-clause
11325 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11326 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11327 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11328 as for cp_parser_initializer. */
11330 static tree
11331 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11333 tree initializers = NULL_TREE;
11335 /* Assume all of the expressions are constant. */
11336 *non_constant_p = false;
11338 /* Parse the rest of the list. */
11339 while (true)
11341 cp_token *token;
11342 tree identifier;
11343 tree initializer;
11344 bool clause_non_constant_p;
11346 /* If the next token is an identifier and the following one is a
11347 colon, we are looking at the GNU designated-initializer
11348 syntax. */
11349 if (cp_parser_allow_gnu_extensions_p (parser)
11350 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11351 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11353 /* Consume the identifier. */
11354 identifier = cp_lexer_consume_token (parser->lexer)->value;
11355 /* Consume the `:'. */
11356 cp_lexer_consume_token (parser->lexer);
11358 else
11359 identifier = NULL_TREE;
11361 /* Parse the initializer. */
11362 initializer = cp_parser_initializer_clause (parser,
11363 &clause_non_constant_p);
11364 /* If any clause is non-constant, so is the entire initializer. */
11365 if (clause_non_constant_p)
11366 *non_constant_p = true;
11367 /* Add it to the list. */
11368 initializers = tree_cons (identifier, initializer, initializers);
11370 /* If the next token is not a comma, we have reached the end of
11371 the list. */
11372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11373 break;
11375 /* Peek at the next token. */
11376 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11377 /* If the next token is a `}', then we're still done. An
11378 initializer-clause can have a trailing `,' after the
11379 initializer-list and before the closing `}'. */
11380 if (token->type == CPP_CLOSE_BRACE)
11381 break;
11383 /* Consume the `,' token. */
11384 cp_lexer_consume_token (parser->lexer);
11387 /* The initializers were built up in reverse order, so we need to
11388 reverse them now. */
11389 return nreverse (initializers);
11392 /* Classes [gram.class] */
11394 /* Parse a class-name.
11396 class-name:
11397 identifier
11398 template-id
11400 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11401 to indicate that names looked up in dependent types should be
11402 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11403 keyword has been used to indicate that the name that appears next
11404 is a template. TYPE_P is true iff the next name should be treated
11405 as class-name, even if it is declared to be some other kind of name
11406 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11407 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11408 being defined in a class-head.
11410 Returns the TYPE_DECL representing the class. */
11412 static tree
11413 cp_parser_class_name (cp_parser *parser,
11414 bool typename_keyword_p,
11415 bool template_keyword_p,
11416 bool type_p,
11417 bool check_dependency_p,
11418 bool class_head_p,
11419 bool is_declaration)
11421 tree decl;
11422 tree scope;
11423 bool typename_p;
11424 cp_token *token;
11426 /* All class-names start with an identifier. */
11427 token = cp_lexer_peek_token (parser->lexer);
11428 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11430 cp_parser_error (parser, "expected class-name");
11431 return error_mark_node;
11434 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11435 to a template-id, so we save it here. */
11436 scope = parser->scope;
11437 if (scope == error_mark_node)
11438 return error_mark_node;
11440 /* Any name names a type if we're following the `typename' keyword
11441 in a qualified name where the enclosing scope is type-dependent. */
11442 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11443 && dependent_type_p (scope));
11444 /* Handle the common case (an identifier, but not a template-id)
11445 efficiently. */
11446 if (token->type == CPP_NAME
11447 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11449 tree identifier;
11451 /* Look for the identifier. */
11452 identifier = cp_parser_identifier (parser);
11453 /* If the next token isn't an identifier, we are certainly not
11454 looking at a class-name. */
11455 if (identifier == error_mark_node)
11456 decl = error_mark_node;
11457 /* If we know this is a type-name, there's no need to look it
11458 up. */
11459 else if (typename_p)
11460 decl = identifier;
11461 else
11463 /* If the next token is a `::', then the name must be a type
11464 name.
11466 [basic.lookup.qual]
11468 During the lookup for a name preceding the :: scope
11469 resolution operator, object, function, and enumerator
11470 names are ignored. */
11471 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11472 type_p = true;
11473 /* Look up the name. */
11474 decl = cp_parser_lookup_name (parser, identifier,
11475 type_p,
11476 /*is_template=*/false,
11477 /*is_namespace=*/false,
11478 check_dependency_p);
11481 else
11483 /* Try a template-id. */
11484 decl = cp_parser_template_id (parser, template_keyword_p,
11485 check_dependency_p,
11486 is_declaration);
11487 if (decl == error_mark_node)
11488 return error_mark_node;
11491 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11493 /* If this is a typename, create a TYPENAME_TYPE. */
11494 if (typename_p && decl != error_mark_node)
11495 decl = TYPE_NAME (make_typename_type (scope, decl,
11496 /*complain=*/1));
11498 /* Check to see that it is really the name of a class. */
11499 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11500 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11501 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11502 /* Situations like this:
11504 template <typename T> struct A {
11505 typename T::template X<int>::I i;
11508 are problematic. Is `T::template X<int>' a class-name? The
11509 standard does not seem to be definitive, but there is no other
11510 valid interpretation of the following `::'. Therefore, those
11511 names are considered class-names. */
11512 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11513 else if (decl == error_mark_node
11514 || TREE_CODE (decl) != TYPE_DECL
11515 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11517 cp_parser_error (parser, "expected class-name");
11518 return error_mark_node;
11521 return decl;
11524 /* Parse a class-specifier.
11526 class-specifier:
11527 class-head { member-specification [opt] }
11529 Returns the TREE_TYPE representing the class. */
11531 static tree
11532 cp_parser_class_specifier (cp_parser* parser)
11534 cp_token *token;
11535 tree type;
11536 tree attributes = NULL_TREE;
11537 int has_trailing_semicolon;
11538 bool nested_name_specifier_p;
11539 unsigned saved_num_template_parameter_lists;
11541 push_deferring_access_checks (dk_no_deferred);
11543 /* Parse the class-head. */
11544 type = cp_parser_class_head (parser,
11545 &nested_name_specifier_p);
11546 /* If the class-head was a semantic disaster, skip the entire body
11547 of the class. */
11548 if (!type)
11550 cp_parser_skip_to_end_of_block_or_statement (parser);
11551 pop_deferring_access_checks ();
11552 return error_mark_node;
11555 /* Look for the `{'. */
11556 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11558 pop_deferring_access_checks ();
11559 return error_mark_node;
11562 /* Issue an error message if type-definitions are forbidden here. */
11563 cp_parser_check_type_definition (parser);
11564 /* Remember that we are defining one more class. */
11565 ++parser->num_classes_being_defined;
11566 /* Inside the class, surrounding template-parameter-lists do not
11567 apply. */
11568 saved_num_template_parameter_lists
11569 = parser->num_template_parameter_lists;
11570 parser->num_template_parameter_lists = 0;
11572 /* Start the class. */
11573 if (nested_name_specifier_p)
11574 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11575 type = begin_class_definition (type);
11576 if (type == error_mark_node)
11577 /* If the type is erroneous, skip the entire body of the class. */
11578 cp_parser_skip_to_closing_brace (parser);
11579 else
11580 /* Parse the member-specification. */
11581 cp_parser_member_specification_opt (parser);
11582 /* Look for the trailing `}'. */
11583 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11584 /* We get better error messages by noticing a common problem: a
11585 missing trailing `;'. */
11586 token = cp_lexer_peek_token (parser->lexer);
11587 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11588 /* Look for attributes to apply to this class. */
11589 if (cp_parser_allow_gnu_extensions_p (parser))
11590 attributes = cp_parser_attributes_opt (parser);
11591 /* If we got any attributes in class_head, xref_tag will stick them in
11592 TREE_TYPE of the type. Grab them now. */
11593 if (type != error_mark_node)
11595 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11596 TYPE_ATTRIBUTES (type) = NULL_TREE;
11597 type = finish_struct (type, attributes);
11599 if (nested_name_specifier_p)
11600 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11601 /* If this class is not itself within the scope of another class,
11602 then we need to parse the bodies of all of the queued function
11603 definitions. Note that the queued functions defined in a class
11604 are not always processed immediately following the
11605 class-specifier for that class. Consider:
11607 struct A {
11608 struct B { void f() { sizeof (A); } };
11611 If `f' were processed before the processing of `A' were
11612 completed, there would be no way to compute the size of `A'.
11613 Note that the nesting we are interested in here is lexical --
11614 not the semantic nesting given by TYPE_CONTEXT. In particular,
11615 for:
11617 struct A { struct B; };
11618 struct A::B { void f() { } };
11620 there is no need to delay the parsing of `A::B::f'. */
11621 if (--parser->num_classes_being_defined == 0)
11623 tree queue_entry;
11624 tree fn;
11626 /* In a first pass, parse default arguments to the functions.
11627 Then, in a second pass, parse the bodies of the functions.
11628 This two-phased approach handles cases like:
11630 struct S {
11631 void f() { g(); }
11632 void g(int i = 3);
11636 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11637 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11638 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11639 TREE_PURPOSE (parser->unparsed_functions_queues)
11640 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11642 fn = TREE_VALUE (queue_entry);
11643 /* Make sure that any template parameters are in scope. */
11644 maybe_begin_member_template_processing (fn);
11645 /* If there are default arguments that have not yet been processed,
11646 take care of them now. */
11647 cp_parser_late_parsing_default_args (parser, fn);
11648 /* Remove any template parameters from the symbol table. */
11649 maybe_end_member_template_processing ();
11651 /* Now parse the body of the functions. */
11652 for (TREE_VALUE (parser->unparsed_functions_queues)
11653 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11654 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11655 TREE_VALUE (parser->unparsed_functions_queues)
11656 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11658 /* Figure out which function we need to process. */
11659 fn = TREE_VALUE (queue_entry);
11661 /* Parse the function. */
11662 cp_parser_late_parsing_for_member (parser, fn);
11667 /* Put back any saved access checks. */
11668 pop_deferring_access_checks ();
11670 /* Restore the count of active template-parameter-lists. */
11671 parser->num_template_parameter_lists
11672 = saved_num_template_parameter_lists;
11674 return type;
11677 /* Parse a class-head.
11679 class-head:
11680 class-key identifier [opt] base-clause [opt]
11681 class-key nested-name-specifier identifier base-clause [opt]
11682 class-key nested-name-specifier [opt] template-id
11683 base-clause [opt]
11685 GNU Extensions:
11686 class-key attributes identifier [opt] base-clause [opt]
11687 class-key attributes nested-name-specifier identifier base-clause [opt]
11688 class-key attributes nested-name-specifier [opt] template-id
11689 base-clause [opt]
11691 Returns the TYPE of the indicated class. Sets
11692 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11693 involving a nested-name-specifier was used, and FALSE otherwise.
11695 Returns NULL_TREE if the class-head is syntactically valid, but
11696 semantically invalid in a way that means we should skip the entire
11697 body of the class. */
11699 static tree
11700 cp_parser_class_head (cp_parser* parser,
11701 bool* nested_name_specifier_p)
11703 cp_token *token;
11704 tree nested_name_specifier;
11705 enum tag_types class_key;
11706 tree id = NULL_TREE;
11707 tree type = NULL_TREE;
11708 tree attributes;
11709 bool template_id_p = false;
11710 bool qualified_p = false;
11711 bool invalid_nested_name_p = false;
11712 bool invalid_explicit_specialization_p = false;
11713 unsigned num_templates;
11715 /* Assume no nested-name-specifier will be present. */
11716 *nested_name_specifier_p = false;
11717 /* Assume no template parameter lists will be used in defining the
11718 type. */
11719 num_templates = 0;
11721 /* Look for the class-key. */
11722 class_key = cp_parser_class_key (parser);
11723 if (class_key == none_type)
11724 return error_mark_node;
11726 /* Parse the attributes. */
11727 attributes = cp_parser_attributes_opt (parser);
11729 /* If the next token is `::', that is invalid -- but sometimes
11730 people do try to write:
11732 struct ::S {};
11734 Handle this gracefully by accepting the extra qualifier, and then
11735 issuing an error about it later if this really is a
11736 class-head. If it turns out just to be an elaborated type
11737 specifier, remain silent. */
11738 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11739 qualified_p = true;
11741 push_deferring_access_checks (dk_no_check);
11743 /* Determine the name of the class. Begin by looking for an
11744 optional nested-name-specifier. */
11745 nested_name_specifier
11746 = cp_parser_nested_name_specifier_opt (parser,
11747 /*typename_keyword_p=*/false,
11748 /*check_dependency_p=*/false,
11749 /*type_p=*/false,
11750 /*is_declaration=*/false);
11751 /* If there was a nested-name-specifier, then there *must* be an
11752 identifier. */
11753 if (nested_name_specifier)
11755 /* Although the grammar says `identifier', it really means
11756 `class-name' or `template-name'. You are only allowed to
11757 define a class that has already been declared with this
11758 syntax.
11760 The proposed resolution for Core Issue 180 says that whever
11761 you see `class T::X' you should treat `X' as a type-name.
11763 It is OK to define an inaccessible class; for example:
11765 class A { class B; };
11766 class A::B {};
11768 We do not know if we will see a class-name, or a
11769 template-name. We look for a class-name first, in case the
11770 class-name is a template-id; if we looked for the
11771 template-name first we would stop after the template-name. */
11772 cp_parser_parse_tentatively (parser);
11773 type = cp_parser_class_name (parser,
11774 /*typename_keyword_p=*/false,
11775 /*template_keyword_p=*/false,
11776 /*type_p=*/true,
11777 /*check_dependency_p=*/false,
11778 /*class_head_p=*/true,
11779 /*is_declaration=*/false);
11780 /* If that didn't work, ignore the nested-name-specifier. */
11781 if (!cp_parser_parse_definitely (parser))
11783 invalid_nested_name_p = true;
11784 id = cp_parser_identifier (parser);
11785 if (id == error_mark_node)
11786 id = NULL_TREE;
11788 /* If we could not find a corresponding TYPE, treat this
11789 declaration like an unqualified declaration. */
11790 if (type == error_mark_node)
11791 nested_name_specifier = NULL_TREE;
11792 /* Otherwise, count the number of templates used in TYPE and its
11793 containing scopes. */
11794 else
11796 tree scope;
11798 for (scope = TREE_TYPE (type);
11799 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11800 scope = (TYPE_P (scope)
11801 ? TYPE_CONTEXT (scope)
11802 : DECL_CONTEXT (scope)))
11803 if (TYPE_P (scope)
11804 && CLASS_TYPE_P (scope)
11805 && CLASSTYPE_TEMPLATE_INFO (scope)
11806 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11807 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11808 ++num_templates;
11811 /* Otherwise, the identifier is optional. */
11812 else
11814 /* We don't know whether what comes next is a template-id,
11815 an identifier, or nothing at all. */
11816 cp_parser_parse_tentatively (parser);
11817 /* Check for a template-id. */
11818 id = cp_parser_template_id (parser,
11819 /*template_keyword_p=*/false,
11820 /*check_dependency_p=*/true,
11821 /*is_declaration=*/true);
11822 /* If that didn't work, it could still be an identifier. */
11823 if (!cp_parser_parse_definitely (parser))
11825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11826 id = cp_parser_identifier (parser);
11827 else
11828 id = NULL_TREE;
11830 else
11832 template_id_p = true;
11833 ++num_templates;
11837 pop_deferring_access_checks ();
11839 cp_parser_check_for_invalid_template_id (parser, id);
11841 /* If it's not a `:' or a `{' then we can't really be looking at a
11842 class-head, since a class-head only appears as part of a
11843 class-specifier. We have to detect this situation before calling
11844 xref_tag, since that has irreversible side-effects. */
11845 if (!cp_parser_next_token_starts_class_definition_p (parser))
11847 cp_parser_error (parser, "expected `{' or `:'");
11848 return error_mark_node;
11851 /* At this point, we're going ahead with the class-specifier, even
11852 if some other problem occurs. */
11853 cp_parser_commit_to_tentative_parse (parser);
11854 /* Issue the error about the overly-qualified name now. */
11855 if (qualified_p)
11856 cp_parser_error (parser,
11857 "global qualification of class name is invalid");
11858 else if (invalid_nested_name_p)
11859 cp_parser_error (parser,
11860 "qualified name does not name a class");
11861 else if (nested_name_specifier)
11863 tree scope;
11864 /* Figure out in what scope the declaration is being placed. */
11865 scope = current_scope ();
11866 if (!scope)
11867 scope = current_namespace;
11868 /* If that scope does not contain the scope in which the
11869 class was originally declared, the program is invalid. */
11870 if (scope && !is_ancestor (scope, nested_name_specifier))
11872 error ("declaration of `%D' in `%D' which does not "
11873 "enclose `%D'", type, scope, nested_name_specifier);
11874 type = NULL_TREE;
11875 goto done;
11877 /* [dcl.meaning]
11879 A declarator-id shall not be qualified exception of the
11880 definition of a ... nested class outside of its class
11881 ... [or] a the definition or explicit instantiation of a
11882 class member of a namespace outside of its namespace. */
11883 if (scope == nested_name_specifier)
11885 pedwarn ("extra qualification ignored");
11886 nested_name_specifier = NULL_TREE;
11887 num_templates = 0;
11890 /* An explicit-specialization must be preceded by "template <>". If
11891 it is not, try to recover gracefully. */
11892 if (at_namespace_scope_p ()
11893 && parser->num_template_parameter_lists == 0
11894 && template_id_p)
11896 error ("an explicit specialization must be preceded by 'template <>'");
11897 invalid_explicit_specialization_p = true;
11898 /* Take the same action that would have been taken by
11899 cp_parser_explicit_specialization. */
11900 ++parser->num_template_parameter_lists;
11901 begin_specialization ();
11903 /* There must be no "return" statements between this point and the
11904 end of this function; set "type "to the correct return value and
11905 use "goto done;" to return. */
11906 /* Make sure that the right number of template parameters were
11907 present. */
11908 if (!cp_parser_check_template_parameters (parser, num_templates))
11910 /* If something went wrong, there is no point in even trying to
11911 process the class-definition. */
11912 type = NULL_TREE;
11913 goto done;
11916 /* Look up the type. */
11917 if (template_id_p)
11919 type = TREE_TYPE (id);
11920 maybe_process_partial_specialization (type);
11922 else if (!nested_name_specifier)
11924 /* If the class was unnamed, create a dummy name. */
11925 if (!id)
11926 id = make_anon_name ();
11927 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11928 parser->num_template_parameter_lists);
11930 else
11932 tree class_type;
11934 /* Given:
11936 template <typename T> struct S { struct T };
11937 template <typename T> struct S<T>::T { };
11939 we will get a TYPENAME_TYPE when processing the definition of
11940 `S::T'. We need to resolve it to the actual type before we
11941 try to define it. */
11942 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11944 class_type = resolve_typename_type (TREE_TYPE (type),
11945 /*only_current_p=*/false);
11946 if (class_type != error_mark_node)
11947 type = TYPE_NAME (class_type);
11948 else
11950 cp_parser_error (parser, "could not resolve typename type");
11951 type = error_mark_node;
11955 maybe_process_partial_specialization (TREE_TYPE (type));
11956 class_type = current_class_type;
11957 /* Enter the scope indicated by the nested-name-specifier. */
11958 if (nested_name_specifier)
11959 push_scope (nested_name_specifier);
11960 /* Get the canonical version of this type. */
11961 type = TYPE_MAIN_DECL (TREE_TYPE (type));
11962 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11963 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11964 type = push_template_decl (type);
11965 type = TREE_TYPE (type);
11966 if (nested_name_specifier)
11968 *nested_name_specifier_p = true;
11969 pop_scope (nested_name_specifier);
11972 /* Indicate whether this class was declared as a `class' or as a
11973 `struct'. */
11974 if (TREE_CODE (type) == RECORD_TYPE)
11975 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11976 cp_parser_check_class_key (class_key, type);
11978 /* Enter the scope containing the class; the names of base classes
11979 should be looked up in that context. For example, given:
11981 struct A { struct B {}; struct C; };
11982 struct A::C : B {};
11984 is valid. */
11985 if (nested_name_specifier)
11986 push_scope (nested_name_specifier);
11987 /* Now, look for the base-clause. */
11988 token = cp_lexer_peek_token (parser->lexer);
11989 if (token->type == CPP_COLON)
11991 tree bases;
11993 /* Get the list of base-classes. */
11994 bases = cp_parser_base_clause (parser);
11995 /* Process them. */
11996 xref_basetypes (type, bases);
11998 /* Leave the scope given by the nested-name-specifier. We will
11999 enter the class scope itself while processing the members. */
12000 if (nested_name_specifier)
12001 pop_scope (nested_name_specifier);
12003 done:
12004 if (invalid_explicit_specialization_p)
12006 end_specialization ();
12007 --parser->num_template_parameter_lists;
12009 return type;
12012 /* Parse a class-key.
12014 class-key:
12015 class
12016 struct
12017 union
12019 Returns the kind of class-key specified, or none_type to indicate
12020 error. */
12022 static enum tag_types
12023 cp_parser_class_key (cp_parser* parser)
12025 cp_token *token;
12026 enum tag_types tag_type;
12028 /* Look for the class-key. */
12029 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12030 if (!token)
12031 return none_type;
12033 /* Check to see if the TOKEN is a class-key. */
12034 tag_type = cp_parser_token_is_class_key (token);
12035 if (!tag_type)
12036 cp_parser_error (parser, "expected class-key");
12037 return tag_type;
12040 /* Parse an (optional) member-specification.
12042 member-specification:
12043 member-declaration member-specification [opt]
12044 access-specifier : member-specification [opt] */
12046 static void
12047 cp_parser_member_specification_opt (cp_parser* parser)
12049 while (true)
12051 cp_token *token;
12052 enum rid keyword;
12054 /* Peek at the next token. */
12055 token = cp_lexer_peek_token (parser->lexer);
12056 /* If it's a `}', or EOF then we've seen all the members. */
12057 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12058 break;
12060 /* See if this token is a keyword. */
12061 keyword = token->keyword;
12062 switch (keyword)
12064 case RID_PUBLIC:
12065 case RID_PROTECTED:
12066 case RID_PRIVATE:
12067 /* Consume the access-specifier. */
12068 cp_lexer_consume_token (parser->lexer);
12069 /* Remember which access-specifier is active. */
12070 current_access_specifier = token->value;
12071 /* Look for the `:'. */
12072 cp_parser_require (parser, CPP_COLON, "`:'");
12073 break;
12075 default:
12076 /* Otherwise, the next construction must be a
12077 member-declaration. */
12078 cp_parser_member_declaration (parser);
12083 /* Parse a member-declaration.
12085 member-declaration:
12086 decl-specifier-seq [opt] member-declarator-list [opt] ;
12087 function-definition ; [opt]
12088 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12089 using-declaration
12090 template-declaration
12092 member-declarator-list:
12093 member-declarator
12094 member-declarator-list , member-declarator
12096 member-declarator:
12097 declarator pure-specifier [opt]
12098 declarator constant-initializer [opt]
12099 identifier [opt] : constant-expression
12101 GNU Extensions:
12103 member-declaration:
12104 __extension__ member-declaration
12106 member-declarator:
12107 declarator attributes [opt] pure-specifier [opt]
12108 declarator attributes [opt] constant-initializer [opt]
12109 identifier [opt] attributes [opt] : constant-expression */
12111 static void
12112 cp_parser_member_declaration (cp_parser* parser)
12114 tree decl_specifiers;
12115 tree prefix_attributes;
12116 tree decl;
12117 int declares_class_or_enum;
12118 bool friend_p;
12119 cp_token *token;
12120 int saved_pedantic;
12122 /* Check for the `__extension__' keyword. */
12123 if (cp_parser_extension_opt (parser, &saved_pedantic))
12125 /* Recurse. */
12126 cp_parser_member_declaration (parser);
12127 /* Restore the old value of the PEDANTIC flag. */
12128 pedantic = saved_pedantic;
12130 return;
12133 /* Check for a template-declaration. */
12134 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12136 /* Parse the template-declaration. */
12137 cp_parser_template_declaration (parser, /*member_p=*/true);
12139 return;
12142 /* Check for a using-declaration. */
12143 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12145 /* Parse the using-declaration. */
12146 cp_parser_using_declaration (parser);
12148 return;
12151 /* Parse the decl-specifier-seq. */
12152 decl_specifiers
12153 = cp_parser_decl_specifier_seq (parser,
12154 CP_PARSER_FLAGS_OPTIONAL,
12155 &prefix_attributes,
12156 &declares_class_or_enum);
12157 /* Check for an invalid type-name. */
12158 if (cp_parser_diagnose_invalid_type_name (parser))
12159 return;
12160 /* If there is no declarator, then the decl-specifier-seq should
12161 specify a type. */
12162 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12164 /* If there was no decl-specifier-seq, and the next token is a
12165 `;', then we have something like:
12167 struct S { ; };
12169 [class.mem]
12171 Each member-declaration shall declare at least one member
12172 name of the class. */
12173 if (!decl_specifiers)
12175 if (pedantic)
12176 pedwarn ("extra semicolon");
12178 else
12180 tree type;
12182 /* See if this declaration is a friend. */
12183 friend_p = cp_parser_friend_p (decl_specifiers);
12184 /* If there were decl-specifiers, check to see if there was
12185 a class-declaration. */
12186 type = check_tag_decl (decl_specifiers);
12187 /* Nested classes have already been added to the class, but
12188 a `friend' needs to be explicitly registered. */
12189 if (friend_p)
12191 /* If the `friend' keyword was present, the friend must
12192 be introduced with a class-key. */
12193 if (!declares_class_or_enum)
12194 error ("a class-key must be used when declaring a friend");
12195 /* In this case:
12197 template <typename T> struct A {
12198 friend struct A<T>::B;
12201 A<T>::B will be represented by a TYPENAME_TYPE, and
12202 therefore not recognized by check_tag_decl. */
12203 if (!type)
12205 tree specifier;
12207 for (specifier = decl_specifiers;
12208 specifier;
12209 specifier = TREE_CHAIN (specifier))
12211 tree s = TREE_VALUE (specifier);
12213 if (TREE_CODE (s) == IDENTIFIER_NODE)
12214 get_global_value_if_present (s, &type);
12215 if (TREE_CODE (s) == TYPE_DECL)
12216 s = TREE_TYPE (s);
12217 if (TYPE_P (s))
12219 type = s;
12220 break;
12224 if (!type)
12225 error ("friend declaration does not name a class or "
12226 "function");
12227 else
12228 make_friend_class (current_class_type, type,
12229 /*complain=*/true);
12231 /* If there is no TYPE, an error message will already have
12232 been issued. */
12233 else if (!type)
12235 /* An anonymous aggregate has to be handled specially; such
12236 a declaration really declares a data member (with a
12237 particular type), as opposed to a nested class. */
12238 else if (ANON_AGGR_TYPE_P (type))
12240 /* Remove constructors and such from TYPE, now that we
12241 know it is an anonymous aggregate. */
12242 fixup_anonymous_aggr (type);
12243 /* And make the corresponding data member. */
12244 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12245 /* Add it to the class. */
12246 finish_member_declaration (decl);
12248 else
12249 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12252 else
12254 /* See if these declarations will be friends. */
12255 friend_p = cp_parser_friend_p (decl_specifiers);
12257 /* Keep going until we hit the `;' at the end of the
12258 declaration. */
12259 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12261 tree attributes = NULL_TREE;
12262 tree first_attribute;
12264 /* Peek at the next token. */
12265 token = cp_lexer_peek_token (parser->lexer);
12267 /* Check for a bitfield declaration. */
12268 if (token->type == CPP_COLON
12269 || (token->type == CPP_NAME
12270 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12271 == CPP_COLON))
12273 tree identifier;
12274 tree width;
12276 /* Get the name of the bitfield. Note that we cannot just
12277 check TOKEN here because it may have been invalidated by
12278 the call to cp_lexer_peek_nth_token above. */
12279 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12280 identifier = cp_parser_identifier (parser);
12281 else
12282 identifier = NULL_TREE;
12284 /* Consume the `:' token. */
12285 cp_lexer_consume_token (parser->lexer);
12286 /* Get the width of the bitfield. */
12287 width
12288 = cp_parser_constant_expression (parser,
12289 /*allow_non_constant=*/false,
12290 NULL);
12292 /* Look for attributes that apply to the bitfield. */
12293 attributes = cp_parser_attributes_opt (parser);
12294 /* Remember which attributes are prefix attributes and
12295 which are not. */
12296 first_attribute = attributes;
12297 /* Combine the attributes. */
12298 attributes = chainon (prefix_attributes, attributes);
12300 /* Create the bitfield declaration. */
12301 decl = grokbitfield (identifier,
12302 decl_specifiers,
12303 width);
12304 /* Apply the attributes. */
12305 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12307 else
12309 tree declarator;
12310 tree initializer;
12311 tree asm_specification;
12312 int ctor_dtor_or_conv_p;
12314 /* Parse the declarator. */
12315 declarator
12316 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12317 &ctor_dtor_or_conv_p,
12318 /*parenthesized_p=*/NULL);
12320 /* If something went wrong parsing the declarator, make sure
12321 that we at least consume some tokens. */
12322 if (declarator == error_mark_node)
12324 /* Skip to the end of the statement. */
12325 cp_parser_skip_to_end_of_statement (parser);
12326 /* If the next token is not a semicolon, that is
12327 probably because we just skipped over the body of
12328 a function. So, we consume a semicolon if
12329 present, but do not issue an error message if it
12330 is not present. */
12331 if (cp_lexer_next_token_is (parser->lexer,
12332 CPP_SEMICOLON))
12333 cp_lexer_consume_token (parser->lexer);
12334 return;
12337 cp_parser_check_for_definition_in_return_type
12338 (declarator, declares_class_or_enum);
12340 /* Look for an asm-specification. */
12341 asm_specification = cp_parser_asm_specification_opt (parser);
12342 /* Look for attributes that apply to the declaration. */
12343 attributes = cp_parser_attributes_opt (parser);
12344 /* Remember which attributes are prefix attributes and
12345 which are not. */
12346 first_attribute = attributes;
12347 /* Combine the attributes. */
12348 attributes = chainon (prefix_attributes, attributes);
12350 /* If it's an `=', then we have a constant-initializer or a
12351 pure-specifier. It is not correct to parse the
12352 initializer before registering the member declaration
12353 since the member declaration should be in scope while
12354 its initializer is processed. However, the rest of the
12355 front end does not yet provide an interface that allows
12356 us to handle this correctly. */
12357 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12359 /* In [class.mem]:
12361 A pure-specifier shall be used only in the declaration of
12362 a virtual function.
12364 A member-declarator can contain a constant-initializer
12365 only if it declares a static member of integral or
12366 enumeration type.
12368 Therefore, if the DECLARATOR is for a function, we look
12369 for a pure-specifier; otherwise, we look for a
12370 constant-initializer. When we call `grokfield', it will
12371 perform more stringent semantics checks. */
12372 if (TREE_CODE (declarator) == CALL_EXPR)
12373 initializer = cp_parser_pure_specifier (parser);
12374 else
12375 /* Parse the initializer. */
12376 initializer = cp_parser_constant_initializer (parser);
12378 /* Otherwise, there is no initializer. */
12379 else
12380 initializer = NULL_TREE;
12382 /* See if we are probably looking at a function
12383 definition. We are certainly not looking at at a
12384 member-declarator. Calling `grokfield' has
12385 side-effects, so we must not do it unless we are sure
12386 that we are looking at a member-declarator. */
12387 if (cp_parser_token_starts_function_definition_p
12388 (cp_lexer_peek_token (parser->lexer)))
12390 /* The grammar does not allow a pure-specifier to be
12391 used when a member function is defined. (It is
12392 possible that this fact is an oversight in the
12393 standard, since a pure function may be defined
12394 outside of the class-specifier. */
12395 if (initializer)
12396 error ("pure-specifier on function-definition");
12397 decl = cp_parser_save_member_function_body (parser,
12398 decl_specifiers,
12399 declarator,
12400 attributes);
12401 /* If the member was not a friend, declare it here. */
12402 if (!friend_p)
12403 finish_member_declaration (decl);
12404 /* Peek at the next token. */
12405 token = cp_lexer_peek_token (parser->lexer);
12406 /* If the next token is a semicolon, consume it. */
12407 if (token->type == CPP_SEMICOLON)
12408 cp_lexer_consume_token (parser->lexer);
12409 return;
12411 else
12413 /* Create the declaration. */
12414 decl = grokfield (declarator, decl_specifiers,
12415 initializer, asm_specification,
12416 attributes);
12417 /* Any initialization must have been from a
12418 constant-expression. */
12419 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12420 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12424 /* Reset PREFIX_ATTRIBUTES. */
12425 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12426 attributes = TREE_CHAIN (attributes);
12427 if (attributes)
12428 TREE_CHAIN (attributes) = NULL_TREE;
12430 /* If there is any qualification still in effect, clear it
12431 now; we will be starting fresh with the next declarator. */
12432 parser->scope = NULL_TREE;
12433 parser->qualifying_scope = NULL_TREE;
12434 parser->object_scope = NULL_TREE;
12435 /* If it's a `,', then there are more declarators. */
12436 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12437 cp_lexer_consume_token (parser->lexer);
12438 /* If the next token isn't a `;', then we have a parse error. */
12439 else if (cp_lexer_next_token_is_not (parser->lexer,
12440 CPP_SEMICOLON))
12442 cp_parser_error (parser, "expected `;'");
12443 /* Skip tokens until we find a `;'. */
12444 cp_parser_skip_to_end_of_statement (parser);
12446 break;
12449 if (decl)
12451 /* Add DECL to the list of members. */
12452 if (!friend_p)
12453 finish_member_declaration (decl);
12455 if (TREE_CODE (decl) == FUNCTION_DECL)
12456 cp_parser_save_default_args (parser, decl);
12461 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12464 /* Parse a pure-specifier.
12466 pure-specifier:
12469 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12470 Otherwise, ERROR_MARK_NODE is returned. */
12472 static tree
12473 cp_parser_pure_specifier (cp_parser* parser)
12475 cp_token *token;
12477 /* Look for the `=' token. */
12478 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12479 return error_mark_node;
12480 /* Look for the `0' token. */
12481 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12482 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12483 to get information from the lexer about how the number was
12484 spelled in order to fix this problem. */
12485 if (!token || !integer_zerop (token->value))
12486 return error_mark_node;
12488 return integer_zero_node;
12491 /* Parse a constant-initializer.
12493 constant-initializer:
12494 = constant-expression
12496 Returns a representation of the constant-expression. */
12498 static tree
12499 cp_parser_constant_initializer (cp_parser* parser)
12501 /* Look for the `=' token. */
12502 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12503 return error_mark_node;
12505 /* It is invalid to write:
12507 struct S { static const int i = { 7 }; };
12510 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12512 cp_parser_error (parser,
12513 "a brace-enclosed initializer is not allowed here");
12514 /* Consume the opening brace. */
12515 cp_lexer_consume_token (parser->lexer);
12516 /* Skip the initializer. */
12517 cp_parser_skip_to_closing_brace (parser);
12518 /* Look for the trailing `}'. */
12519 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12521 return error_mark_node;
12524 return cp_parser_constant_expression (parser,
12525 /*allow_non_constant=*/false,
12526 NULL);
12529 /* Derived classes [gram.class.derived] */
12531 /* Parse a base-clause.
12533 base-clause:
12534 : base-specifier-list
12536 base-specifier-list:
12537 base-specifier
12538 base-specifier-list , base-specifier
12540 Returns a TREE_LIST representing the base-classes, in the order in
12541 which they were declared. The representation of each node is as
12542 described by cp_parser_base_specifier.
12544 In the case that no bases are specified, this function will return
12545 NULL_TREE, not ERROR_MARK_NODE. */
12547 static tree
12548 cp_parser_base_clause (cp_parser* parser)
12550 tree bases = NULL_TREE;
12552 /* Look for the `:' that begins the list. */
12553 cp_parser_require (parser, CPP_COLON, "`:'");
12555 /* Scan the base-specifier-list. */
12556 while (true)
12558 cp_token *token;
12559 tree base;
12561 /* Look for the base-specifier. */
12562 base = cp_parser_base_specifier (parser);
12563 /* Add BASE to the front of the list. */
12564 if (base != error_mark_node)
12566 TREE_CHAIN (base) = bases;
12567 bases = base;
12569 /* Peek at the next token. */
12570 token = cp_lexer_peek_token (parser->lexer);
12571 /* If it's not a comma, then the list is complete. */
12572 if (token->type != CPP_COMMA)
12573 break;
12574 /* Consume the `,'. */
12575 cp_lexer_consume_token (parser->lexer);
12578 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12579 base class had a qualified name. However, the next name that
12580 appears is certainly not qualified. */
12581 parser->scope = NULL_TREE;
12582 parser->qualifying_scope = NULL_TREE;
12583 parser->object_scope = NULL_TREE;
12585 return nreverse (bases);
12588 /* Parse a base-specifier.
12590 base-specifier:
12591 :: [opt] nested-name-specifier [opt] class-name
12592 virtual access-specifier [opt] :: [opt] nested-name-specifier
12593 [opt] class-name
12594 access-specifier virtual [opt] :: [opt] nested-name-specifier
12595 [opt] class-name
12597 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12598 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12599 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12600 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12602 static tree
12603 cp_parser_base_specifier (cp_parser* parser)
12605 cp_token *token;
12606 bool done = false;
12607 bool virtual_p = false;
12608 bool duplicate_virtual_error_issued_p = false;
12609 bool duplicate_access_error_issued_p = false;
12610 bool class_scope_p, template_p;
12611 tree access = access_default_node;
12612 tree type;
12614 /* Process the optional `virtual' and `access-specifier'. */
12615 while (!done)
12617 /* Peek at the next token. */
12618 token = cp_lexer_peek_token (parser->lexer);
12619 /* Process `virtual'. */
12620 switch (token->keyword)
12622 case RID_VIRTUAL:
12623 /* If `virtual' appears more than once, issue an error. */
12624 if (virtual_p && !duplicate_virtual_error_issued_p)
12626 cp_parser_error (parser,
12627 "`virtual' specified more than once in base-specified");
12628 duplicate_virtual_error_issued_p = true;
12631 virtual_p = true;
12633 /* Consume the `virtual' token. */
12634 cp_lexer_consume_token (parser->lexer);
12636 break;
12638 case RID_PUBLIC:
12639 case RID_PROTECTED:
12640 case RID_PRIVATE:
12641 /* If more than one access specifier appears, issue an
12642 error. */
12643 if (access != access_default_node
12644 && !duplicate_access_error_issued_p)
12646 cp_parser_error (parser,
12647 "more than one access specifier in base-specified");
12648 duplicate_access_error_issued_p = true;
12651 access = ridpointers[(int) token->keyword];
12653 /* Consume the access-specifier. */
12654 cp_lexer_consume_token (parser->lexer);
12656 break;
12658 default:
12659 done = true;
12660 break;
12664 /* Look for the optional `::' operator. */
12665 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12666 /* Look for the nested-name-specifier. The simplest way to
12667 implement:
12669 [temp.res]
12671 The keyword `typename' is not permitted in a base-specifier or
12672 mem-initializer; in these contexts a qualified name that
12673 depends on a template-parameter is implicitly assumed to be a
12674 type name.
12676 is to pretend that we have seen the `typename' keyword at this
12677 point. */
12678 cp_parser_nested_name_specifier_opt (parser,
12679 /*typename_keyword_p=*/true,
12680 /*check_dependency_p=*/true,
12681 /*type_p=*/true,
12682 /*is_declaration=*/true);
12683 /* If the base class is given by a qualified name, assume that names
12684 we see are type names or templates, as appropriate. */
12685 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12686 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12688 /* Finally, look for the class-name. */
12689 type = cp_parser_class_name (parser,
12690 class_scope_p,
12691 template_p,
12692 /*type_p=*/true,
12693 /*check_dependency_p=*/true,
12694 /*class_head_p=*/false,
12695 /*is_declaration=*/true);
12697 if (type == error_mark_node)
12698 return error_mark_node;
12700 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12703 /* Exception handling [gram.exception] */
12705 /* Parse an (optional) exception-specification.
12707 exception-specification:
12708 throw ( type-id-list [opt] )
12710 Returns a TREE_LIST representing the exception-specification. The
12711 TREE_VALUE of each node is a type. */
12713 static tree
12714 cp_parser_exception_specification_opt (cp_parser* parser)
12716 cp_token *token;
12717 tree type_id_list;
12719 /* Peek at the next token. */
12720 token = cp_lexer_peek_token (parser->lexer);
12721 /* If it's not `throw', then there's no exception-specification. */
12722 if (!cp_parser_is_keyword (token, RID_THROW))
12723 return NULL_TREE;
12725 /* Consume the `throw'. */
12726 cp_lexer_consume_token (parser->lexer);
12728 /* Look for the `('. */
12729 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12731 /* Peek at the next token. */
12732 token = cp_lexer_peek_token (parser->lexer);
12733 /* If it's not a `)', then there is a type-id-list. */
12734 if (token->type != CPP_CLOSE_PAREN)
12736 const char *saved_message;
12738 /* Types may not be defined in an exception-specification. */
12739 saved_message = parser->type_definition_forbidden_message;
12740 parser->type_definition_forbidden_message
12741 = "types may not be defined in an exception-specification";
12742 /* Parse the type-id-list. */
12743 type_id_list = cp_parser_type_id_list (parser);
12744 /* Restore the saved message. */
12745 parser->type_definition_forbidden_message = saved_message;
12747 else
12748 type_id_list = empty_except_spec;
12750 /* Look for the `)'. */
12751 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12753 return type_id_list;
12756 /* Parse an (optional) type-id-list.
12758 type-id-list:
12759 type-id
12760 type-id-list , type-id
12762 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12763 in the order that the types were presented. */
12765 static tree
12766 cp_parser_type_id_list (cp_parser* parser)
12768 tree types = NULL_TREE;
12770 while (true)
12772 cp_token *token;
12773 tree type;
12775 /* Get the next type-id. */
12776 type = cp_parser_type_id (parser);
12777 /* Add it to the list. */
12778 types = add_exception_specifier (types, type, /*complain=*/1);
12779 /* Peek at the next token. */
12780 token = cp_lexer_peek_token (parser->lexer);
12781 /* If it is not a `,', we are done. */
12782 if (token->type != CPP_COMMA)
12783 break;
12784 /* Consume the `,'. */
12785 cp_lexer_consume_token (parser->lexer);
12788 return nreverse (types);
12791 /* Parse a try-block.
12793 try-block:
12794 try compound-statement handler-seq */
12796 static tree
12797 cp_parser_try_block (cp_parser* parser)
12799 tree try_block;
12801 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12802 try_block = begin_try_block ();
12803 cp_parser_compound_statement (parser, false);
12804 finish_try_block (try_block);
12805 cp_parser_handler_seq (parser);
12806 finish_handler_sequence (try_block);
12808 return try_block;
12811 /* Parse a function-try-block.
12813 function-try-block:
12814 try ctor-initializer [opt] function-body handler-seq */
12816 static bool
12817 cp_parser_function_try_block (cp_parser* parser)
12819 tree try_block;
12820 bool ctor_initializer_p;
12822 /* Look for the `try' keyword. */
12823 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12824 return false;
12825 /* Let the rest of the front-end know where we are. */
12826 try_block = begin_function_try_block ();
12827 /* Parse the function-body. */
12828 ctor_initializer_p
12829 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12830 /* We're done with the `try' part. */
12831 finish_function_try_block (try_block);
12832 /* Parse the handlers. */
12833 cp_parser_handler_seq (parser);
12834 /* We're done with the handlers. */
12835 finish_function_handler_sequence (try_block);
12837 return ctor_initializer_p;
12840 /* Parse a handler-seq.
12842 handler-seq:
12843 handler handler-seq [opt] */
12845 static void
12846 cp_parser_handler_seq (cp_parser* parser)
12848 while (true)
12850 cp_token *token;
12852 /* Parse the handler. */
12853 cp_parser_handler (parser);
12854 /* Peek at the next token. */
12855 token = cp_lexer_peek_token (parser->lexer);
12856 /* If it's not `catch' then there are no more handlers. */
12857 if (!cp_parser_is_keyword (token, RID_CATCH))
12858 break;
12862 /* Parse a handler.
12864 handler:
12865 catch ( exception-declaration ) compound-statement */
12867 static void
12868 cp_parser_handler (cp_parser* parser)
12870 tree handler;
12871 tree declaration;
12873 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12874 handler = begin_handler ();
12875 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12876 declaration = cp_parser_exception_declaration (parser);
12877 finish_handler_parms (declaration, handler);
12878 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12879 cp_parser_compound_statement (parser, false);
12880 finish_handler (handler);
12883 /* Parse an exception-declaration.
12885 exception-declaration:
12886 type-specifier-seq declarator
12887 type-specifier-seq abstract-declarator
12888 type-specifier-seq
12889 ...
12891 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12892 ellipsis variant is used. */
12894 static tree
12895 cp_parser_exception_declaration (cp_parser* parser)
12897 tree type_specifiers;
12898 tree declarator;
12899 const char *saved_message;
12901 /* If it's an ellipsis, it's easy to handle. */
12902 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12904 /* Consume the `...' token. */
12905 cp_lexer_consume_token (parser->lexer);
12906 return NULL_TREE;
12909 /* Types may not be defined in exception-declarations. */
12910 saved_message = parser->type_definition_forbidden_message;
12911 parser->type_definition_forbidden_message
12912 = "types may not be defined in exception-declarations";
12914 /* Parse the type-specifier-seq. */
12915 type_specifiers = cp_parser_type_specifier_seq (parser);
12916 /* If it's a `)', then there is no declarator. */
12917 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12918 declarator = NULL_TREE;
12919 else
12920 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12921 /*ctor_dtor_or_conv_p=*/NULL,
12922 /*parenthesized_p=*/NULL);
12924 /* Restore the saved message. */
12925 parser->type_definition_forbidden_message = saved_message;
12927 return start_handler_parms (type_specifiers, declarator);
12930 /* Parse a throw-expression.
12932 throw-expression:
12933 throw assignment-expression [opt]
12935 Returns a THROW_EXPR representing the throw-expression. */
12937 static tree
12938 cp_parser_throw_expression (cp_parser* parser)
12940 tree expression;
12941 cp_token* token;
12943 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12944 token = cp_lexer_peek_token (parser->lexer);
12945 /* Figure out whether or not there is an assignment-expression
12946 following the "throw" keyword. */
12947 if (token->type == CPP_COMMA
12948 || token->type == CPP_SEMICOLON
12949 || token->type == CPP_CLOSE_PAREN
12950 || token->type == CPP_CLOSE_SQUARE
12951 || token->type == CPP_CLOSE_BRACE
12952 || token->type == CPP_COLON)
12953 expression = NULL_TREE;
12954 else
12955 expression = cp_parser_assignment_expression (parser);
12957 return build_throw (expression);
12960 /* GNU Extensions */
12962 /* Parse an (optional) asm-specification.
12964 asm-specification:
12965 asm ( string-literal )
12967 If the asm-specification is present, returns a STRING_CST
12968 corresponding to the string-literal. Otherwise, returns
12969 NULL_TREE. */
12971 static tree
12972 cp_parser_asm_specification_opt (cp_parser* parser)
12974 cp_token *token;
12975 tree asm_specification;
12977 /* Peek at the next token. */
12978 token = cp_lexer_peek_token (parser->lexer);
12979 /* If the next token isn't the `asm' keyword, then there's no
12980 asm-specification. */
12981 if (!cp_parser_is_keyword (token, RID_ASM))
12982 return NULL_TREE;
12984 /* Consume the `asm' token. */
12985 cp_lexer_consume_token (parser->lexer);
12986 /* Look for the `('. */
12987 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12989 /* Look for the string-literal. */
12990 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12991 if (token)
12992 asm_specification = token->value;
12993 else
12994 asm_specification = NULL_TREE;
12996 /* Look for the `)'. */
12997 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12999 return asm_specification;
13002 /* Parse an asm-operand-list.
13004 asm-operand-list:
13005 asm-operand
13006 asm-operand-list , asm-operand
13008 asm-operand:
13009 string-literal ( expression )
13010 [ string-literal ] string-literal ( expression )
13012 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13013 each node is the expression. The TREE_PURPOSE is itself a
13014 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13015 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13016 is a STRING_CST for the string literal before the parenthesis. */
13018 static tree
13019 cp_parser_asm_operand_list (cp_parser* parser)
13021 tree asm_operands = NULL_TREE;
13023 while (true)
13025 tree string_literal;
13026 tree expression;
13027 tree name;
13028 cp_token *token;
13030 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13032 /* Consume the `[' token. */
13033 cp_lexer_consume_token (parser->lexer);
13034 /* Read the operand name. */
13035 name = cp_parser_identifier (parser);
13036 if (name != error_mark_node)
13037 name = build_string (IDENTIFIER_LENGTH (name),
13038 IDENTIFIER_POINTER (name));
13039 /* Look for the closing `]'. */
13040 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13042 else
13043 name = NULL_TREE;
13044 /* Look for the string-literal. */
13045 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13046 string_literal = token ? token->value : error_mark_node;
13047 /* Look for the `('. */
13048 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13049 /* Parse the expression. */
13050 expression = cp_parser_expression (parser);
13051 /* Look for the `)'. */
13052 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13053 /* Add this operand to the list. */
13054 asm_operands = tree_cons (build_tree_list (name, string_literal),
13055 expression,
13056 asm_operands);
13057 /* If the next token is not a `,', there are no more
13058 operands. */
13059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13060 break;
13061 /* Consume the `,'. */
13062 cp_lexer_consume_token (parser->lexer);
13065 return nreverse (asm_operands);
13068 /* Parse an asm-clobber-list.
13070 asm-clobber-list:
13071 string-literal
13072 asm-clobber-list , string-literal
13074 Returns a TREE_LIST, indicating the clobbers in the order that they
13075 appeared. The TREE_VALUE of each node is a STRING_CST. */
13077 static tree
13078 cp_parser_asm_clobber_list (cp_parser* parser)
13080 tree clobbers = NULL_TREE;
13082 while (true)
13084 cp_token *token;
13085 tree string_literal;
13087 /* Look for the string literal. */
13088 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13089 string_literal = token ? token->value : error_mark_node;
13090 /* Add it to the list. */
13091 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13092 /* If the next token is not a `,', then the list is
13093 complete. */
13094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13095 break;
13096 /* Consume the `,' token. */
13097 cp_lexer_consume_token (parser->lexer);
13100 return clobbers;
13103 /* Parse an (optional) series of attributes.
13105 attributes:
13106 attributes attribute
13108 attribute:
13109 __attribute__ (( attribute-list [opt] ))
13111 The return value is as for cp_parser_attribute_list. */
13113 static tree
13114 cp_parser_attributes_opt (cp_parser* parser)
13116 tree attributes = NULL_TREE;
13118 while (true)
13120 cp_token *token;
13121 tree attribute_list;
13123 /* Peek at the next token. */
13124 token = cp_lexer_peek_token (parser->lexer);
13125 /* If it's not `__attribute__', then we're done. */
13126 if (token->keyword != RID_ATTRIBUTE)
13127 break;
13129 /* Consume the `__attribute__' keyword. */
13130 cp_lexer_consume_token (parser->lexer);
13131 /* Look for the two `(' tokens. */
13132 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13133 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13135 /* Peek at the next token. */
13136 token = cp_lexer_peek_token (parser->lexer);
13137 if (token->type != CPP_CLOSE_PAREN)
13138 /* Parse the attribute-list. */
13139 attribute_list = cp_parser_attribute_list (parser);
13140 else
13141 /* If the next token is a `)', then there is no attribute
13142 list. */
13143 attribute_list = NULL;
13145 /* Look for the two `)' tokens. */
13146 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13147 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13149 /* Add these new attributes to the list. */
13150 attributes = chainon (attributes, attribute_list);
13153 return attributes;
13156 /* Parse an attribute-list.
13158 attribute-list:
13159 attribute
13160 attribute-list , attribute
13162 attribute:
13163 identifier
13164 identifier ( identifier )
13165 identifier ( identifier , expression-list )
13166 identifier ( expression-list )
13168 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13169 TREE_PURPOSE of each node is the identifier indicating which
13170 attribute is in use. The TREE_VALUE represents the arguments, if
13171 any. */
13173 static tree
13174 cp_parser_attribute_list (cp_parser* parser)
13176 tree attribute_list = NULL_TREE;
13178 while (true)
13180 cp_token *token;
13181 tree identifier;
13182 tree attribute;
13184 /* Look for the identifier. We also allow keywords here; for
13185 example `__attribute__ ((const))' is legal. */
13186 token = cp_lexer_peek_token (parser->lexer);
13187 if (token->type != CPP_NAME
13188 && token->type != CPP_KEYWORD)
13189 return error_mark_node;
13190 /* Consume the token. */
13191 token = cp_lexer_consume_token (parser->lexer);
13193 /* Save away the identifier that indicates which attribute this is. */
13194 identifier = token->value;
13195 attribute = build_tree_list (identifier, NULL_TREE);
13197 /* Peek at the next token. */
13198 token = cp_lexer_peek_token (parser->lexer);
13199 /* If it's an `(', then parse the attribute arguments. */
13200 if (token->type == CPP_OPEN_PAREN)
13202 tree arguments;
13204 arguments = (cp_parser_parenthesized_expression_list
13205 (parser, true, /*non_constant_p=*/NULL));
13206 /* Save the identifier and arguments away. */
13207 TREE_VALUE (attribute) = arguments;
13210 /* Add this attribute to the list. */
13211 TREE_CHAIN (attribute) = attribute_list;
13212 attribute_list = attribute;
13214 /* Now, look for more attributes. */
13215 token = cp_lexer_peek_token (parser->lexer);
13216 /* If the next token isn't a `,', we're done. */
13217 if (token->type != CPP_COMMA)
13218 break;
13220 /* Consume the comma and keep going. */
13221 cp_lexer_consume_token (parser->lexer);
13224 /* We built up the list in reverse order. */
13225 return nreverse (attribute_list);
13228 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13229 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13230 current value of the PEDANTIC flag, regardless of whether or not
13231 the `__extension__' keyword is present. The caller is responsible
13232 for restoring the value of the PEDANTIC flag. */
13234 static bool
13235 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13237 /* Save the old value of the PEDANTIC flag. */
13238 *saved_pedantic = pedantic;
13240 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13242 /* Consume the `__extension__' token. */
13243 cp_lexer_consume_token (parser->lexer);
13244 /* We're not being pedantic while the `__extension__' keyword is
13245 in effect. */
13246 pedantic = 0;
13248 return true;
13251 return false;
13254 /* Parse a label declaration.
13256 label-declaration:
13257 __label__ label-declarator-seq ;
13259 label-declarator-seq:
13260 identifier , label-declarator-seq
13261 identifier */
13263 static void
13264 cp_parser_label_declaration (cp_parser* parser)
13266 /* Look for the `__label__' keyword. */
13267 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13269 while (true)
13271 tree identifier;
13273 /* Look for an identifier. */
13274 identifier = cp_parser_identifier (parser);
13275 /* Declare it as a lobel. */
13276 finish_label_decl (identifier);
13277 /* If the next token is a `;', stop. */
13278 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13279 break;
13280 /* Look for the `,' separating the label declarations. */
13281 cp_parser_require (parser, CPP_COMMA, "`,'");
13284 /* Look for the final `;'. */
13285 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13288 /* Support Functions */
13290 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13291 NAME should have one of the representations used for an
13292 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13293 is returned. If PARSER->SCOPE is a dependent type, then a
13294 SCOPE_REF is returned.
13296 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13297 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13298 was formed. Abstractly, such entities should not be passed to this
13299 function, because they do not need to be looked up, but it is
13300 simpler to check for this special case here, rather than at the
13301 call-sites.
13303 In cases not explicitly covered above, this function returns a
13304 DECL, OVERLOAD, or baselink representing the result of the lookup.
13305 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13306 is returned.
13308 If IS_TYPE is TRUE, bindings that do not refer to types are
13309 ignored.
13311 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13312 ignored.
13314 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13315 are ignored.
13317 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13318 types. */
13320 static tree
13321 cp_parser_lookup_name (cp_parser *parser, tree name,
13322 bool is_type, bool is_template, bool is_namespace,
13323 bool check_dependency)
13325 tree decl;
13326 tree object_type = parser->context->object_type;
13328 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13329 no longer valid. Note that if we are parsing tentatively, and
13330 the parse fails, OBJECT_TYPE will be automatically restored. */
13331 parser->context->object_type = NULL_TREE;
13333 if (name == error_mark_node)
13334 return error_mark_node;
13336 /* A template-id has already been resolved; there is no lookup to
13337 do. */
13338 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13339 return name;
13340 if (BASELINK_P (name))
13342 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13343 == TEMPLATE_ID_EXPR),
13344 20020909);
13345 return name;
13348 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13349 it should already have been checked to make sure that the name
13350 used matches the type being destroyed. */
13351 if (TREE_CODE (name) == BIT_NOT_EXPR)
13353 tree type;
13355 /* Figure out to which type this destructor applies. */
13356 if (parser->scope)
13357 type = parser->scope;
13358 else if (object_type)
13359 type = object_type;
13360 else
13361 type = current_class_type;
13362 /* If that's not a class type, there is no destructor. */
13363 if (!type || !CLASS_TYPE_P (type))
13364 return error_mark_node;
13365 /* If it was a class type, return the destructor. */
13366 return CLASSTYPE_DESTRUCTORS (type);
13369 /* By this point, the NAME should be an ordinary identifier. If
13370 the id-expression was a qualified name, the qualifying scope is
13371 stored in PARSER->SCOPE at this point. */
13372 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13373 20000619);
13375 /* Perform the lookup. */
13376 if (parser->scope)
13378 bool dependent_p;
13380 if (parser->scope == error_mark_node)
13381 return error_mark_node;
13383 /* If the SCOPE is dependent, the lookup must be deferred until
13384 the template is instantiated -- unless we are explicitly
13385 looking up names in uninstantiated templates. Even then, we
13386 cannot look up the name if the scope is not a class type; it
13387 might, for example, be a template type parameter. */
13388 dependent_p = (TYPE_P (parser->scope)
13389 && !(parser->in_declarator_p
13390 && currently_open_class (parser->scope))
13391 && dependent_type_p (parser->scope));
13392 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13393 && dependent_p)
13395 if (is_type)
13396 /* The resolution to Core Issue 180 says that `struct A::B'
13397 should be considered a type-name, even if `A' is
13398 dependent. */
13399 decl = TYPE_NAME (make_typename_type (parser->scope,
13400 name,
13401 /*complain=*/1));
13402 else if (is_template)
13403 decl = make_unbound_class_template (parser->scope,
13404 name,
13405 /*complain=*/1);
13406 else
13407 decl = build_nt (SCOPE_REF, parser->scope, name);
13409 else
13411 /* If PARSER->SCOPE is a dependent type, then it must be a
13412 class type, and we must not be checking dependencies;
13413 otherwise, we would have processed this lookup above. So
13414 that PARSER->SCOPE is not considered a dependent base by
13415 lookup_member, we must enter the scope here. */
13416 if (dependent_p)
13417 push_scope (parser->scope);
13418 /* If the PARSER->SCOPE is a a template specialization, it
13419 may be instantiated during name lookup. In that case,
13420 errors may be issued. Even if we rollback the current
13421 tentative parse, those errors are valid. */
13422 decl = lookup_qualified_name (parser->scope, name, is_type,
13423 /*complain=*/true);
13424 if (dependent_p)
13425 pop_scope (parser->scope);
13427 parser->qualifying_scope = parser->scope;
13428 parser->object_scope = NULL_TREE;
13430 else if (object_type)
13432 tree object_decl = NULL_TREE;
13433 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13434 OBJECT_TYPE is not a class. */
13435 if (CLASS_TYPE_P (object_type))
13436 /* If the OBJECT_TYPE is a template specialization, it may
13437 be instantiated during name lookup. In that case, errors
13438 may be issued. Even if we rollback the current tentative
13439 parse, those errors are valid. */
13440 object_decl = lookup_member (object_type,
13441 name,
13442 /*protect=*/0, is_type);
13443 /* Look it up in the enclosing context, too. */
13444 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13445 is_namespace,
13446 /*flags=*/0);
13447 parser->object_scope = object_type;
13448 parser->qualifying_scope = NULL_TREE;
13449 if (object_decl)
13450 decl = object_decl;
13452 else
13454 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13455 is_namespace,
13456 /*flags=*/0);
13457 parser->qualifying_scope = NULL_TREE;
13458 parser->object_scope = NULL_TREE;
13461 /* If the lookup failed, let our caller know. */
13462 if (!decl
13463 || decl == error_mark_node
13464 || (TREE_CODE (decl) == FUNCTION_DECL
13465 && DECL_ANTICIPATED (decl)))
13466 return error_mark_node;
13468 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13469 if (TREE_CODE (decl) == TREE_LIST)
13471 /* The error message we have to print is too complicated for
13472 cp_parser_error, so we incorporate its actions directly. */
13473 if (!cp_parser_simulate_error (parser))
13475 error ("reference to `%D' is ambiguous", name);
13476 print_candidates (decl);
13478 return error_mark_node;
13481 my_friendly_assert (DECL_P (decl)
13482 || TREE_CODE (decl) == OVERLOAD
13483 || TREE_CODE (decl) == SCOPE_REF
13484 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13485 || BASELINK_P (decl),
13486 20000619);
13488 /* If we have resolved the name of a member declaration, check to
13489 see if the declaration is accessible. When the name resolves to
13490 set of overloaded functions, accessibility is checked when
13491 overload resolution is done.
13493 During an explicit instantiation, access is not checked at all,
13494 as per [temp.explicit]. */
13495 if (DECL_P (decl))
13496 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13498 return decl;
13501 /* Like cp_parser_lookup_name, but for use in the typical case where
13502 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13503 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13505 static tree
13506 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13508 return cp_parser_lookup_name (parser, name,
13509 /*is_type=*/false,
13510 /*is_template=*/false,
13511 /*is_namespace=*/false,
13512 /*check_dependency=*/true);
13515 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13516 the current context, return the TYPE_DECL. If TAG_NAME_P is
13517 true, the DECL indicates the class being defined in a class-head,
13518 or declared in an elaborated-type-specifier.
13520 Otherwise, return DECL. */
13522 static tree
13523 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13525 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13526 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13528 struct A {
13529 template <typename T> struct B;
13532 template <typename T> struct A::B {};
13534 Similarly, in a elaborated-type-specifier:
13536 namespace N { struct X{}; }
13538 struct A {
13539 template <typename T> friend struct N::X;
13542 However, if the DECL refers to a class type, and we are in
13543 the scope of the class, then the name lookup automatically
13544 finds the TYPE_DECL created by build_self_reference rather
13545 than a TEMPLATE_DECL. For example, in:
13547 template <class T> struct S {
13548 S s;
13551 there is no need to handle such case. */
13553 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13554 return DECL_TEMPLATE_RESULT (decl);
13556 return decl;
13559 /* If too many, or too few, template-parameter lists apply to the
13560 declarator, issue an error message. Returns TRUE if all went well,
13561 and FALSE otherwise. */
13563 static bool
13564 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13565 tree declarator)
13567 unsigned num_templates;
13569 /* We haven't seen any classes that involve template parameters yet. */
13570 num_templates = 0;
13572 switch (TREE_CODE (declarator))
13574 case CALL_EXPR:
13575 case ARRAY_REF:
13576 case INDIRECT_REF:
13577 case ADDR_EXPR:
13579 tree main_declarator = TREE_OPERAND (declarator, 0);
13580 return
13581 cp_parser_check_declarator_template_parameters (parser,
13582 main_declarator);
13585 case SCOPE_REF:
13587 tree scope;
13588 tree member;
13590 scope = TREE_OPERAND (declarator, 0);
13591 member = TREE_OPERAND (declarator, 1);
13593 /* If this is a pointer-to-member, then we are not interested
13594 in the SCOPE, because it does not qualify the thing that is
13595 being declared. */
13596 if (TREE_CODE (member) == INDIRECT_REF)
13597 return (cp_parser_check_declarator_template_parameters
13598 (parser, member));
13600 while (scope && CLASS_TYPE_P (scope))
13602 /* You're supposed to have one `template <...>'
13603 for every template class, but you don't need one
13604 for a full specialization. For example:
13606 template <class T> struct S{};
13607 template <> struct S<int> { void f(); };
13608 void S<int>::f () {}
13610 is correct; there shouldn't be a `template <>' for
13611 the definition of `S<int>::f'. */
13612 if (CLASSTYPE_TEMPLATE_INFO (scope)
13613 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13614 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13615 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13616 ++num_templates;
13618 scope = TYPE_CONTEXT (scope);
13622 /* Fall through. */
13624 default:
13625 /* If the DECLARATOR has the form `X<y>' then it uses one
13626 additional level of template parameters. */
13627 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13628 ++num_templates;
13630 return cp_parser_check_template_parameters (parser,
13631 num_templates);
13635 /* NUM_TEMPLATES were used in the current declaration. If that is
13636 invalid, return FALSE and issue an error messages. Otherwise,
13637 return TRUE. */
13639 static bool
13640 cp_parser_check_template_parameters (cp_parser* parser,
13641 unsigned num_templates)
13643 /* If there are more template classes than parameter lists, we have
13644 something like:
13646 template <class T> void S<T>::R<T>::f (); */
13647 if (parser->num_template_parameter_lists < num_templates)
13649 error ("too few template-parameter-lists");
13650 return false;
13652 /* If there are the same number of template classes and parameter
13653 lists, that's OK. */
13654 if (parser->num_template_parameter_lists == num_templates)
13655 return true;
13656 /* If there are more, but only one more, then we are referring to a
13657 member template. That's OK too. */
13658 if (parser->num_template_parameter_lists == num_templates + 1)
13659 return true;
13660 /* Otherwise, there are too many template parameter lists. We have
13661 something like:
13663 template <class T> template <class U> void S::f(); */
13664 error ("too many template-parameter-lists");
13665 return false;
13668 /* Parse a binary-expression of the general form:
13670 binary-expression:
13671 <expr>
13672 binary-expression <token> <expr>
13674 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13675 to parser the <expr>s. If the first production is used, then the
13676 value returned by FN is returned directly. Otherwise, a node with
13677 the indicated EXPR_TYPE is returned, with operands corresponding to
13678 the two sub-expressions. */
13680 static tree
13681 cp_parser_binary_expression (cp_parser* parser,
13682 const cp_parser_token_tree_map token_tree_map,
13683 cp_parser_expression_fn fn)
13685 tree lhs;
13687 /* Parse the first expression. */
13688 lhs = (*fn) (parser);
13689 /* Now, look for more expressions. */
13690 while (true)
13692 cp_token *token;
13693 const cp_parser_token_tree_map_node *map_node;
13694 tree rhs;
13696 /* Peek at the next token. */
13697 token = cp_lexer_peek_token (parser->lexer);
13698 /* If the token is `>', and that's not an operator at the
13699 moment, then we're done. */
13700 if (token->type == CPP_GREATER
13701 && !parser->greater_than_is_operator_p)
13702 break;
13703 /* If we find one of the tokens we want, build the corresponding
13704 tree representation. */
13705 for (map_node = token_tree_map;
13706 map_node->token_type != CPP_EOF;
13707 ++map_node)
13708 if (map_node->token_type == token->type)
13710 /* Consume the operator token. */
13711 cp_lexer_consume_token (parser->lexer);
13712 /* Parse the right-hand side of the expression. */
13713 rhs = (*fn) (parser);
13714 /* Build the binary tree node. */
13715 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13716 break;
13719 /* If the token wasn't one of the ones we want, we're done. */
13720 if (map_node->token_type == CPP_EOF)
13721 break;
13724 return lhs;
13727 /* Parse an optional `::' token indicating that the following name is
13728 from the global namespace. If so, PARSER->SCOPE is set to the
13729 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13730 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13731 Returns the new value of PARSER->SCOPE, if the `::' token is
13732 present, and NULL_TREE otherwise. */
13734 static tree
13735 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13737 cp_token *token;
13739 /* Peek at the next token. */
13740 token = cp_lexer_peek_token (parser->lexer);
13741 /* If we're looking at a `::' token then we're starting from the
13742 global namespace, not our current location. */
13743 if (token->type == CPP_SCOPE)
13745 /* Consume the `::' token. */
13746 cp_lexer_consume_token (parser->lexer);
13747 /* Set the SCOPE so that we know where to start the lookup. */
13748 parser->scope = global_namespace;
13749 parser->qualifying_scope = global_namespace;
13750 parser->object_scope = NULL_TREE;
13752 return parser->scope;
13754 else if (!current_scope_valid_p)
13756 parser->scope = NULL_TREE;
13757 parser->qualifying_scope = NULL_TREE;
13758 parser->object_scope = NULL_TREE;
13761 return NULL_TREE;
13764 /* Returns TRUE if the upcoming token sequence is the start of a
13765 constructor declarator. If FRIEND_P is true, the declarator is
13766 preceded by the `friend' specifier. */
13768 static bool
13769 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13771 bool constructor_p;
13772 tree type_decl = NULL_TREE;
13773 bool nested_name_p;
13774 cp_token *next_token;
13776 /* The common case is that this is not a constructor declarator, so
13777 try to avoid doing lots of work if at all possible. It's not
13778 valid declare a constructor at function scope. */
13779 if (at_function_scope_p ())
13780 return false;
13781 /* And only certain tokens can begin a constructor declarator. */
13782 next_token = cp_lexer_peek_token (parser->lexer);
13783 if (next_token->type != CPP_NAME
13784 && next_token->type != CPP_SCOPE
13785 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13786 && next_token->type != CPP_TEMPLATE_ID)
13787 return false;
13789 /* Parse tentatively; we are going to roll back all of the tokens
13790 consumed here. */
13791 cp_parser_parse_tentatively (parser);
13792 /* Assume that we are looking at a constructor declarator. */
13793 constructor_p = true;
13795 /* Look for the optional `::' operator. */
13796 cp_parser_global_scope_opt (parser,
13797 /*current_scope_valid_p=*/false);
13798 /* Look for the nested-name-specifier. */
13799 nested_name_p
13800 = (cp_parser_nested_name_specifier_opt (parser,
13801 /*typename_keyword_p=*/false,
13802 /*check_dependency_p=*/false,
13803 /*type_p=*/false,
13804 /*is_declaration=*/false)
13805 != NULL_TREE);
13806 /* Outside of a class-specifier, there must be a
13807 nested-name-specifier. */
13808 if (!nested_name_p &&
13809 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13810 || friend_p))
13811 constructor_p = false;
13812 /* If we still think that this might be a constructor-declarator,
13813 look for a class-name. */
13814 if (constructor_p)
13816 /* If we have:
13818 template <typename T> struct S { S(); };
13819 template <typename T> S<T>::S ();
13821 we must recognize that the nested `S' names a class.
13822 Similarly, for:
13824 template <typename T> S<T>::S<T> ();
13826 we must recognize that the nested `S' names a template. */
13827 type_decl = cp_parser_class_name (parser,
13828 /*typename_keyword_p=*/false,
13829 /*template_keyword_p=*/false,
13830 /*type_p=*/false,
13831 /*check_dependency_p=*/false,
13832 /*class_head_p=*/false,
13833 /*is_declaration=*/false);
13834 /* If there was no class-name, then this is not a constructor. */
13835 constructor_p = !cp_parser_error_occurred (parser);
13838 /* If we're still considering a constructor, we have to see a `(',
13839 to begin the parameter-declaration-clause, followed by either a
13840 `)', an `...', or a decl-specifier. We need to check for a
13841 type-specifier to avoid being fooled into thinking that:
13843 S::S (f) (int);
13845 is a constructor. (It is actually a function named `f' that
13846 takes one parameter (of type `int') and returns a value of type
13847 `S::S'. */
13848 if (constructor_p
13849 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13852 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13853 && !cp_parser_storage_class_specifier_opt (parser))
13855 tree type;
13856 unsigned saved_num_template_parameter_lists;
13858 /* Names appearing in the type-specifier should be looked up
13859 in the scope of the class. */
13860 if (current_class_type)
13861 type = NULL_TREE;
13862 else
13864 type = TREE_TYPE (type_decl);
13865 if (TREE_CODE (type) == TYPENAME_TYPE)
13867 type = resolve_typename_type (type,
13868 /*only_current_p=*/false);
13869 if (type == error_mark_node)
13871 cp_parser_abort_tentative_parse (parser);
13872 return false;
13875 push_scope (type);
13878 /* Inside the constructor parameter list, surrounding
13879 template-parameter-lists do not apply. */
13880 saved_num_template_parameter_lists
13881 = parser->num_template_parameter_lists;
13882 parser->num_template_parameter_lists = 0;
13884 /* Look for the type-specifier. */
13885 cp_parser_type_specifier (parser,
13886 CP_PARSER_FLAGS_NONE,
13887 /*is_friend=*/false,
13888 /*is_declarator=*/true,
13889 /*declares_class_or_enum=*/NULL,
13890 /*is_cv_qualifier=*/NULL);
13892 parser->num_template_parameter_lists
13893 = saved_num_template_parameter_lists;
13895 /* Leave the scope of the class. */
13896 if (type)
13897 pop_scope (type);
13899 constructor_p = !cp_parser_error_occurred (parser);
13902 else
13903 constructor_p = false;
13904 /* We did not really want to consume any tokens. */
13905 cp_parser_abort_tentative_parse (parser);
13907 return constructor_p;
13910 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13911 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13912 they must be performed once we are in the scope of the function.
13914 Returns the function defined. */
13916 static tree
13917 cp_parser_function_definition_from_specifiers_and_declarator
13918 (cp_parser* parser,
13919 tree decl_specifiers,
13920 tree attributes,
13921 tree declarator)
13923 tree fn;
13924 bool success_p;
13926 /* Begin the function-definition. */
13927 success_p = begin_function_definition (decl_specifiers,
13928 attributes,
13929 declarator);
13931 /* If there were names looked up in the decl-specifier-seq that we
13932 did not check, check them now. We must wait until we are in the
13933 scope of the function to perform the checks, since the function
13934 might be a friend. */
13935 perform_deferred_access_checks ();
13937 if (!success_p)
13939 /* If begin_function_definition didn't like the definition, skip
13940 the entire function. */
13941 error ("invalid function declaration");
13942 cp_parser_skip_to_end_of_block_or_statement (parser);
13943 fn = error_mark_node;
13945 else
13946 fn = cp_parser_function_definition_after_declarator (parser,
13947 /*inline_p=*/false);
13949 return fn;
13952 /* Parse the part of a function-definition that follows the
13953 declarator. INLINE_P is TRUE iff this function is an inline
13954 function defined with a class-specifier.
13956 Returns the function defined. */
13958 static tree
13959 cp_parser_function_definition_after_declarator (cp_parser* parser,
13960 bool inline_p)
13962 tree fn;
13963 bool ctor_initializer_p = false;
13964 bool saved_in_unbraced_linkage_specification_p;
13965 unsigned saved_num_template_parameter_lists;
13967 /* If the next token is `return', then the code may be trying to
13968 make use of the "named return value" extension that G++ used to
13969 support. */
13970 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13972 /* Consume the `return' keyword. */
13973 cp_lexer_consume_token (parser->lexer);
13974 /* Look for the identifier that indicates what value is to be
13975 returned. */
13976 cp_parser_identifier (parser);
13977 /* Issue an error message. */
13978 error ("named return values are no longer supported");
13979 /* Skip tokens until we reach the start of the function body. */
13980 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13981 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
13982 cp_lexer_consume_token (parser->lexer);
13984 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13985 anything declared inside `f'. */
13986 saved_in_unbraced_linkage_specification_p
13987 = parser->in_unbraced_linkage_specification_p;
13988 parser->in_unbraced_linkage_specification_p = false;
13989 /* Inside the function, surrounding template-parameter-lists do not
13990 apply. */
13991 saved_num_template_parameter_lists
13992 = parser->num_template_parameter_lists;
13993 parser->num_template_parameter_lists = 0;
13994 /* If the next token is `try', then we are looking at a
13995 function-try-block. */
13996 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13997 ctor_initializer_p = cp_parser_function_try_block (parser);
13998 /* A function-try-block includes the function-body, so we only do
13999 this next part if we're not processing a function-try-block. */
14000 else
14001 ctor_initializer_p
14002 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14004 /* Finish the function. */
14005 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14006 (inline_p ? 2 : 0));
14007 /* Generate code for it, if necessary. */
14008 expand_or_defer_fn (fn);
14009 /* Restore the saved values. */
14010 parser->in_unbraced_linkage_specification_p
14011 = saved_in_unbraced_linkage_specification_p;
14012 parser->num_template_parameter_lists
14013 = saved_num_template_parameter_lists;
14015 return fn;
14018 /* Parse a template-declaration, assuming that the `export' (and
14019 `extern') keywords, if present, has already been scanned. MEMBER_P
14020 is as for cp_parser_template_declaration. */
14022 static void
14023 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14025 tree decl = NULL_TREE;
14026 tree parameter_list;
14027 bool friend_p = false;
14029 /* Look for the `template' keyword. */
14030 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14031 return;
14033 /* And the `<'. */
14034 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14035 return;
14037 /* If the next token is `>', then we have an invalid
14038 specialization. Rather than complain about an invalid template
14039 parameter, issue an error message here. */
14040 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14042 cp_parser_error (parser, "invalid explicit specialization");
14043 begin_specialization ();
14044 parameter_list = NULL_TREE;
14046 else
14048 /* Parse the template parameters. */
14049 begin_template_parm_list ();
14050 parameter_list = cp_parser_template_parameter_list (parser);
14051 parameter_list = end_template_parm_list (parameter_list);
14054 /* Look for the `>'. */
14055 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14056 /* We just processed one more parameter list. */
14057 ++parser->num_template_parameter_lists;
14058 /* If the next token is `template', there are more template
14059 parameters. */
14060 if (cp_lexer_next_token_is_keyword (parser->lexer,
14061 RID_TEMPLATE))
14062 cp_parser_template_declaration_after_export (parser, member_p);
14063 else
14065 decl = cp_parser_single_declaration (parser,
14066 member_p,
14067 &friend_p);
14069 /* If this is a member template declaration, let the front
14070 end know. */
14071 if (member_p && !friend_p && decl)
14073 if (TREE_CODE (decl) == TYPE_DECL)
14074 cp_parser_check_access_in_redeclaration (decl);
14076 decl = finish_member_template_decl (decl);
14078 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14079 make_friend_class (current_class_type, TREE_TYPE (decl),
14080 /*complain=*/true);
14082 /* We are done with the current parameter list. */
14083 --parser->num_template_parameter_lists;
14085 /* Finish up. */
14086 finish_template_decl (parameter_list);
14088 /* Register member declarations. */
14089 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14090 finish_member_declaration (decl);
14092 /* If DECL is a function template, we must return to parse it later.
14093 (Even though there is no definition, there might be default
14094 arguments that need handling.) */
14095 if (member_p && decl
14096 && (TREE_CODE (decl) == FUNCTION_DECL
14097 || DECL_FUNCTION_TEMPLATE_P (decl)))
14098 TREE_VALUE (parser->unparsed_functions_queues)
14099 = tree_cons (NULL_TREE, decl,
14100 TREE_VALUE (parser->unparsed_functions_queues));
14103 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14104 `function-definition' sequence. MEMBER_P is true, this declaration
14105 appears in a class scope.
14107 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14108 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14110 static tree
14111 cp_parser_single_declaration (cp_parser* parser,
14112 bool member_p,
14113 bool* friend_p)
14115 int declares_class_or_enum;
14116 tree decl = NULL_TREE;
14117 tree decl_specifiers;
14118 tree attributes;
14119 bool function_definition_p = false;
14121 /* Defer access checks until we know what is being declared. */
14122 push_deferring_access_checks (dk_deferred);
14124 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14125 alternative. */
14126 decl_specifiers
14127 = cp_parser_decl_specifier_seq (parser,
14128 CP_PARSER_FLAGS_OPTIONAL,
14129 &attributes,
14130 &declares_class_or_enum);
14131 if (friend_p)
14132 *friend_p = cp_parser_friend_p (decl_specifiers);
14133 /* Gather up the access checks that occurred the
14134 decl-specifier-seq. */
14135 stop_deferring_access_checks ();
14137 /* Check for the declaration of a template class. */
14138 if (declares_class_or_enum)
14140 if (cp_parser_declares_only_class_p (parser))
14142 decl = shadow_tag (decl_specifiers);
14143 if (decl)
14144 decl = TYPE_NAME (decl);
14145 else
14146 decl = error_mark_node;
14149 else
14150 decl = NULL_TREE;
14151 /* If it's not a template class, try for a template function. If
14152 the next token is a `;', then this declaration does not declare
14153 anything. But, if there were errors in the decl-specifiers, then
14154 the error might well have come from an attempted class-specifier.
14155 In that case, there's no need to warn about a missing declarator. */
14156 if (!decl
14157 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14158 || !value_member (error_mark_node, decl_specifiers)))
14159 decl = cp_parser_init_declarator (parser,
14160 decl_specifiers,
14161 attributes,
14162 /*function_definition_allowed_p=*/true,
14163 member_p,
14164 declares_class_or_enum,
14165 &function_definition_p);
14167 pop_deferring_access_checks ();
14169 /* Clear any current qualification; whatever comes next is the start
14170 of something new. */
14171 parser->scope = NULL_TREE;
14172 parser->qualifying_scope = NULL_TREE;
14173 parser->object_scope = NULL_TREE;
14174 /* Look for a trailing `;' after the declaration. */
14175 if (!function_definition_p
14176 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14177 cp_parser_skip_to_end_of_block_or_statement (parser);
14179 return decl;
14182 /* Parse a cast-expression that is not the operand of a unary "&". */
14184 static tree
14185 cp_parser_simple_cast_expression (cp_parser *parser)
14187 return cp_parser_cast_expression (parser, /*address_p=*/false);
14190 /* Parse a functional cast to TYPE. Returns an expression
14191 representing the cast. */
14193 static tree
14194 cp_parser_functional_cast (cp_parser* parser, tree type)
14196 tree expression_list;
14198 expression_list
14199 = cp_parser_parenthesized_expression_list (parser, false,
14200 /*non_constant_p=*/NULL);
14202 return build_functional_cast (type, expression_list);
14205 /* Save the tokens that make up the body of a member function defined
14206 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14207 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14208 specifiers applied to the declaration. Returns the FUNCTION_DECL
14209 for the member function. */
14211 static tree
14212 cp_parser_save_member_function_body (cp_parser* parser,
14213 tree decl_specifiers,
14214 tree declarator,
14215 tree attributes)
14217 cp_token_cache *cache;
14218 tree fn;
14220 /* Create the function-declaration. */
14221 fn = start_method (decl_specifiers, declarator, attributes);
14222 /* If something went badly wrong, bail out now. */
14223 if (fn == error_mark_node)
14225 /* If there's a function-body, skip it. */
14226 if (cp_parser_token_starts_function_definition_p
14227 (cp_lexer_peek_token (parser->lexer)))
14228 cp_parser_skip_to_end_of_block_or_statement (parser);
14229 return error_mark_node;
14232 /* Remember it, if there default args to post process. */
14233 cp_parser_save_default_args (parser, fn);
14235 /* Create a token cache. */
14236 cache = cp_token_cache_new ();
14237 /* Save away the tokens that make up the body of the
14238 function. */
14239 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14240 /* Handle function try blocks. */
14241 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14242 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14244 /* Save away the inline definition; we will process it when the
14245 class is complete. */
14246 DECL_PENDING_INLINE_INFO (fn) = cache;
14247 DECL_PENDING_INLINE_P (fn) = 1;
14249 /* We need to know that this was defined in the class, so that
14250 friend templates are handled correctly. */
14251 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14253 /* We're done with the inline definition. */
14254 finish_method (fn);
14256 /* Add FN to the queue of functions to be parsed later. */
14257 TREE_VALUE (parser->unparsed_functions_queues)
14258 = tree_cons (NULL_TREE, fn,
14259 TREE_VALUE (parser->unparsed_functions_queues));
14261 return fn;
14264 /* Parse a template-argument-list, as well as the trailing ">" (but
14265 not the opening ">"). See cp_parser_template_argument_list for the
14266 return value. */
14268 static tree
14269 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14271 tree arguments;
14272 tree saved_scope;
14273 tree saved_qualifying_scope;
14274 tree saved_object_scope;
14275 bool saved_greater_than_is_operator_p;
14277 /* [temp.names]
14279 When parsing a template-id, the first non-nested `>' is taken as
14280 the end of the template-argument-list rather than a greater-than
14281 operator. */
14282 saved_greater_than_is_operator_p
14283 = parser->greater_than_is_operator_p;
14284 parser->greater_than_is_operator_p = false;
14285 /* Parsing the argument list may modify SCOPE, so we save it
14286 here. */
14287 saved_scope = parser->scope;
14288 saved_qualifying_scope = parser->qualifying_scope;
14289 saved_object_scope = parser->object_scope;
14290 /* Parse the template-argument-list itself. */
14291 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14292 arguments = NULL_TREE;
14293 else
14294 arguments = cp_parser_template_argument_list (parser);
14295 /* Look for the `>' that ends the template-argument-list. If we find
14296 a '>>' instead, it's probably just a typo. */
14297 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14299 if (!saved_greater_than_is_operator_p)
14301 /* If we're in a nested template argument list, the '>>' has to be
14302 a typo for '> >'. We emit the error message, but we continue
14303 parsing and we push a '>' as next token, so that the argument
14304 list will be parsed correctly.. */
14305 cp_token* token;
14306 error ("`>>' should be `> >' within a nested template argument list");
14307 token = cp_lexer_peek_token (parser->lexer);
14308 token->type = CPP_GREATER;
14310 else
14312 /* If this is not a nested template argument list, the '>>' is
14313 a typo for '>'. Emit an error message and continue. */
14314 error ("spurious `>>', use `>' to terminate a template argument list");
14315 cp_lexer_consume_token (parser->lexer);
14318 else
14319 cp_parser_require (parser, CPP_GREATER, "`>'");
14320 /* The `>' token might be a greater-than operator again now. */
14321 parser->greater_than_is_operator_p
14322 = saved_greater_than_is_operator_p;
14323 /* Restore the SAVED_SCOPE. */
14324 parser->scope = saved_scope;
14325 parser->qualifying_scope = saved_qualifying_scope;
14326 parser->object_scope = saved_object_scope;
14328 return arguments;
14331 /* MEMBER_FUNCTION is a member function, or a friend. If default
14332 arguments, or the body of the function have not yet been parsed,
14333 parse them now. */
14335 static void
14336 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14338 cp_lexer *saved_lexer;
14340 /* If this member is a template, get the underlying
14341 FUNCTION_DECL. */
14342 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14343 member_function = DECL_TEMPLATE_RESULT (member_function);
14345 /* There should not be any class definitions in progress at this
14346 point; the bodies of members are only parsed outside of all class
14347 definitions. */
14348 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14349 /* While we're parsing the member functions we might encounter more
14350 classes. We want to handle them right away, but we don't want
14351 them getting mixed up with functions that are currently in the
14352 queue. */
14353 parser->unparsed_functions_queues
14354 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14356 /* Make sure that any template parameters are in scope. */
14357 maybe_begin_member_template_processing (member_function);
14359 /* If the body of the function has not yet been parsed, parse it
14360 now. */
14361 if (DECL_PENDING_INLINE_P (member_function))
14363 tree function_scope;
14364 cp_token_cache *tokens;
14366 /* The function is no longer pending; we are processing it. */
14367 tokens = DECL_PENDING_INLINE_INFO (member_function);
14368 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14369 DECL_PENDING_INLINE_P (member_function) = 0;
14370 /* If this was an inline function in a local class, enter the scope
14371 of the containing function. */
14372 function_scope = decl_function_context (member_function);
14373 if (function_scope)
14374 push_function_context_to (function_scope);
14376 /* Save away the current lexer. */
14377 saved_lexer = parser->lexer;
14378 /* Make a new lexer to feed us the tokens saved for this function. */
14379 parser->lexer = cp_lexer_new_from_tokens (tokens);
14380 parser->lexer->next = saved_lexer;
14382 /* Set the current source position to be the location of the first
14383 token in the saved inline body. */
14384 cp_lexer_peek_token (parser->lexer);
14386 /* Let the front end know that we going to be defining this
14387 function. */
14388 start_function (NULL_TREE, member_function, NULL_TREE,
14389 SF_PRE_PARSED | SF_INCLASS_INLINE);
14391 /* Now, parse the body of the function. */
14392 cp_parser_function_definition_after_declarator (parser,
14393 /*inline_p=*/true);
14395 /* Leave the scope of the containing function. */
14396 if (function_scope)
14397 pop_function_context_from (function_scope);
14398 /* Restore the lexer. */
14399 parser->lexer = saved_lexer;
14402 /* Remove any template parameters from the symbol table. */
14403 maybe_end_member_template_processing ();
14405 /* Restore the queue. */
14406 parser->unparsed_functions_queues
14407 = TREE_CHAIN (parser->unparsed_functions_queues);
14410 /* If DECL contains any default args, remember it on the unparsed
14411 functions queue. */
14413 static void
14414 cp_parser_save_default_args (cp_parser* parser, tree decl)
14416 tree probe;
14418 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14419 probe;
14420 probe = TREE_CHAIN (probe))
14421 if (TREE_PURPOSE (probe))
14423 TREE_PURPOSE (parser->unparsed_functions_queues)
14424 = tree_cons (NULL_TREE, decl,
14425 TREE_PURPOSE (parser->unparsed_functions_queues));
14426 break;
14428 return;
14431 /* FN is a FUNCTION_DECL which may contains a parameter with an
14432 unparsed DEFAULT_ARG. Parse the default args now. */
14434 static void
14435 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14437 cp_lexer *saved_lexer;
14438 cp_token_cache *tokens;
14439 bool saved_local_variables_forbidden_p;
14440 tree parameters;
14442 /* While we're parsing the default args, we might (due to the
14443 statement expression extension) encounter more classes. We want
14444 to handle them right away, but we don't want them getting mixed
14445 up with default args that are currently in the queue. */
14446 parser->unparsed_functions_queues
14447 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14449 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14450 parameters;
14451 parameters = TREE_CHAIN (parameters))
14453 if (!TREE_PURPOSE (parameters)
14454 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14455 continue;
14457 /* Save away the current lexer. */
14458 saved_lexer = parser->lexer;
14459 /* Create a new one, using the tokens we have saved. */
14460 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14461 parser->lexer = cp_lexer_new_from_tokens (tokens);
14463 /* Set the current source position to be the location of the
14464 first token in the default argument. */
14465 cp_lexer_peek_token (parser->lexer);
14467 /* Local variable names (and the `this' keyword) may not appear
14468 in a default argument. */
14469 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14470 parser->local_variables_forbidden_p = true;
14471 /* Parse the assignment-expression. */
14472 if (DECL_CLASS_SCOPE_P (fn))
14473 push_nested_class (DECL_CONTEXT (fn));
14474 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14475 if (DECL_CLASS_SCOPE_P (fn))
14476 pop_nested_class ();
14478 /* Restore saved state. */
14479 parser->lexer = saved_lexer;
14480 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14483 /* Restore the queue. */
14484 parser->unparsed_functions_queues
14485 = TREE_CHAIN (parser->unparsed_functions_queues);
14488 /* Parse the operand of `sizeof' (or a similar operator). Returns
14489 either a TYPE or an expression, depending on the form of the
14490 input. The KEYWORD indicates which kind of expression we have
14491 encountered. */
14493 static tree
14494 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14496 static const char *format;
14497 tree expr = NULL_TREE;
14498 const char *saved_message;
14499 bool saved_integral_constant_expression_p;
14501 /* Initialize FORMAT the first time we get here. */
14502 if (!format)
14503 format = "types may not be defined in `%s' expressions";
14505 /* Types cannot be defined in a `sizeof' expression. Save away the
14506 old message. */
14507 saved_message = parser->type_definition_forbidden_message;
14508 /* And create the new one. */
14509 parser->type_definition_forbidden_message
14510 = xmalloc (strlen (format)
14511 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14512 + 1 /* `\0' */);
14513 sprintf ((char *) parser->type_definition_forbidden_message,
14514 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14516 /* The restrictions on constant-expressions do not apply inside
14517 sizeof expressions. */
14518 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14519 parser->integral_constant_expression_p = false;
14521 /* Do not actually evaluate the expression. */
14522 ++skip_evaluation;
14523 /* If it's a `(', then we might be looking at the type-id
14524 construction. */
14525 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14527 tree type;
14528 bool saved_in_type_id_in_expr_p;
14530 /* We can't be sure yet whether we're looking at a type-id or an
14531 expression. */
14532 cp_parser_parse_tentatively (parser);
14533 /* Consume the `('. */
14534 cp_lexer_consume_token (parser->lexer);
14535 /* Parse the type-id. */
14536 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14537 parser->in_type_id_in_expr_p = true;
14538 type = cp_parser_type_id (parser);
14539 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14540 /* Now, look for the trailing `)'. */
14541 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14542 /* If all went well, then we're done. */
14543 if (cp_parser_parse_definitely (parser))
14545 /* Build a list of decl-specifiers; right now, we have only
14546 a single type-specifier. */
14547 type = build_tree_list (NULL_TREE,
14548 type);
14550 /* Call grokdeclarator to figure out what type this is. */
14551 expr = grokdeclarator (NULL_TREE,
14552 type,
14553 TYPENAME,
14554 /*initialized=*/0,
14555 /*attrlist=*/NULL);
14559 /* If the type-id production did not work out, then we must be
14560 looking at the unary-expression production. */
14561 if (!expr)
14562 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14563 /* Go back to evaluating expressions. */
14564 --skip_evaluation;
14566 /* Free the message we created. */
14567 free ((char *) parser->type_definition_forbidden_message);
14568 /* And restore the old one. */
14569 parser->type_definition_forbidden_message = saved_message;
14570 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14572 return expr;
14575 /* If the current declaration has no declarator, return true. */
14577 static bool
14578 cp_parser_declares_only_class_p (cp_parser *parser)
14580 /* If the next token is a `;' or a `,' then there is no
14581 declarator. */
14582 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14583 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14586 /* Simplify EXPR if it is a non-dependent expression. Returns the
14587 (possibly simplified) expression. */
14589 static tree
14590 cp_parser_fold_non_dependent_expr (tree expr)
14592 /* If we're in a template, but EXPR isn't value dependent, simplify
14593 it. We're supposed to treat:
14595 template <typename T> void f(T[1 + 1]);
14596 template <typename T> void f(T[2]);
14598 as two declarations of the same function, for example. */
14599 if (processing_template_decl
14600 && !type_dependent_expression_p (expr)
14601 && !value_dependent_expression_p (expr))
14603 HOST_WIDE_INT saved_processing_template_decl;
14605 saved_processing_template_decl = processing_template_decl;
14606 processing_template_decl = 0;
14607 expr = tsubst_copy_and_build (expr,
14608 /*args=*/NULL_TREE,
14609 tf_error,
14610 /*in_decl=*/NULL_TREE,
14611 /*function_p=*/false);
14612 processing_template_decl = saved_processing_template_decl;
14614 return expr;
14617 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14618 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14620 static bool
14621 cp_parser_friend_p (tree decl_specifiers)
14623 while (decl_specifiers)
14625 /* See if this decl-specifier is `friend'. */
14626 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14627 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14628 return true;
14630 /* Go on to the next decl-specifier. */
14631 decl_specifiers = TREE_CHAIN (decl_specifiers);
14634 return false;
14637 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14638 issue an error message indicating that TOKEN_DESC was expected.
14640 Returns the token consumed, if the token had the appropriate type.
14641 Otherwise, returns NULL. */
14643 static cp_token *
14644 cp_parser_require (cp_parser* parser,
14645 enum cpp_ttype type,
14646 const char* token_desc)
14648 if (cp_lexer_next_token_is (parser->lexer, type))
14649 return cp_lexer_consume_token (parser->lexer);
14650 else
14652 /* Output the MESSAGE -- unless we're parsing tentatively. */
14653 if (!cp_parser_simulate_error (parser))
14655 char *message = concat ("expected ", token_desc, NULL);
14656 cp_parser_error (parser, message);
14657 free (message);
14659 return NULL;
14663 /* Like cp_parser_require, except that tokens will be skipped until
14664 the desired token is found. An error message is still produced if
14665 the next token is not as expected. */
14667 static void
14668 cp_parser_skip_until_found (cp_parser* parser,
14669 enum cpp_ttype type,
14670 const char* token_desc)
14672 cp_token *token;
14673 unsigned nesting_depth = 0;
14675 if (cp_parser_require (parser, type, token_desc))
14676 return;
14678 /* Skip tokens until the desired token is found. */
14679 while (true)
14681 /* Peek at the next token. */
14682 token = cp_lexer_peek_token (parser->lexer);
14683 /* If we've reached the token we want, consume it and
14684 stop. */
14685 if (token->type == type && !nesting_depth)
14687 cp_lexer_consume_token (parser->lexer);
14688 return;
14690 /* If we've run out of tokens, stop. */
14691 if (token->type == CPP_EOF)
14692 return;
14693 if (token->type == CPP_OPEN_BRACE
14694 || token->type == CPP_OPEN_PAREN
14695 || token->type == CPP_OPEN_SQUARE)
14696 ++nesting_depth;
14697 else if (token->type == CPP_CLOSE_BRACE
14698 || token->type == CPP_CLOSE_PAREN
14699 || token->type == CPP_CLOSE_SQUARE)
14701 if (nesting_depth-- == 0)
14702 return;
14704 /* Consume this token. */
14705 cp_lexer_consume_token (parser->lexer);
14709 /* If the next token is the indicated keyword, consume it. Otherwise,
14710 issue an error message indicating that TOKEN_DESC was expected.
14712 Returns the token consumed, if the token had the appropriate type.
14713 Otherwise, returns NULL. */
14715 static cp_token *
14716 cp_parser_require_keyword (cp_parser* parser,
14717 enum rid keyword,
14718 const char* token_desc)
14720 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14722 if (token && token->keyword != keyword)
14724 dyn_string_t error_msg;
14726 /* Format the error message. */
14727 error_msg = dyn_string_new (0);
14728 dyn_string_append_cstr (error_msg, "expected ");
14729 dyn_string_append_cstr (error_msg, token_desc);
14730 cp_parser_error (parser, error_msg->s);
14731 dyn_string_delete (error_msg);
14732 return NULL;
14735 return token;
14738 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14739 function-definition. */
14741 static bool
14742 cp_parser_token_starts_function_definition_p (cp_token* token)
14744 return (/* An ordinary function-body begins with an `{'. */
14745 token->type == CPP_OPEN_BRACE
14746 /* A ctor-initializer begins with a `:'. */
14747 || token->type == CPP_COLON
14748 /* A function-try-block begins with `try'. */
14749 || token->keyword == RID_TRY
14750 /* The named return value extension begins with `return'. */
14751 || token->keyword == RID_RETURN);
14754 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14755 definition. */
14757 static bool
14758 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14760 cp_token *token;
14762 token = cp_lexer_peek_token (parser->lexer);
14763 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14766 /* Returns TRUE iff the next token is the "," or ">" ending a
14767 template-argument. ">>" is also accepted (after the full
14768 argument was parsed) because it's probably a typo for "> >",
14769 and there is a specific diagnostic for this. */
14771 static bool
14772 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14774 cp_token *token;
14776 token = cp_lexer_peek_token (parser->lexer);
14777 return (token->type == CPP_COMMA || token->type == CPP_GREATER
14778 || token->type == CPP_RSHIFT);
14781 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14782 or none_type otherwise. */
14784 static enum tag_types
14785 cp_parser_token_is_class_key (cp_token* token)
14787 switch (token->keyword)
14789 case RID_CLASS:
14790 return class_type;
14791 case RID_STRUCT:
14792 return record_type;
14793 case RID_UNION:
14794 return union_type;
14796 default:
14797 return none_type;
14801 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14803 static void
14804 cp_parser_check_class_key (enum tag_types class_key, tree type)
14806 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14807 pedwarn ("`%s' tag used in naming `%#T'",
14808 class_key == union_type ? "union"
14809 : class_key == record_type ? "struct" : "class",
14810 type);
14813 /* Issue an error message if DECL is redeclared with different
14814 access than its original declaration [class.access.spec/3].
14815 This applies to nested classes and nested class templates.
14816 [class.mem/1]. */
14818 static void cp_parser_check_access_in_redeclaration (tree decl)
14820 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14821 return;
14823 if ((TREE_PRIVATE (decl)
14824 != (current_access_specifier == access_private_node))
14825 || (TREE_PROTECTED (decl)
14826 != (current_access_specifier == access_protected_node)))
14827 error ("%D redeclared with different access", decl);
14830 /* Look for the `template' keyword, as a syntactic disambiguator.
14831 Return TRUE iff it is present, in which case it will be
14832 consumed. */
14834 static bool
14835 cp_parser_optional_template_keyword (cp_parser *parser)
14837 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14839 /* The `template' keyword can only be used within templates;
14840 outside templates the parser can always figure out what is a
14841 template and what is not. */
14842 if (!processing_template_decl)
14844 error ("`template' (as a disambiguator) is only allowed "
14845 "within templates");
14846 /* If this part of the token stream is rescanned, the same
14847 error message would be generated. So, we purge the token
14848 from the stream. */
14849 cp_lexer_purge_token (parser->lexer);
14850 return false;
14852 else
14854 /* Consume the `template' keyword. */
14855 cp_lexer_consume_token (parser->lexer);
14856 return true;
14860 return false;
14863 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14864 set PARSER->SCOPE, and perform other related actions. */
14866 static void
14867 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14869 tree value;
14870 tree check;
14872 /* Get the stored value. */
14873 value = cp_lexer_consume_token (parser->lexer)->value;
14874 /* Perform any access checks that were deferred. */
14875 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14876 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14877 /* Set the scope from the stored value. */
14878 parser->scope = TREE_VALUE (value);
14879 parser->qualifying_scope = TREE_TYPE (value);
14880 parser->object_scope = NULL_TREE;
14883 /* Add tokens to CACHE until an non-nested END token appears. */
14885 static void
14886 cp_parser_cache_group (cp_parser *parser,
14887 cp_token_cache *cache,
14888 enum cpp_ttype end,
14889 unsigned depth)
14891 while (true)
14893 cp_token *token;
14895 /* Abort a parenthesized expression if we encounter a brace. */
14896 if ((end == CPP_CLOSE_PAREN || depth == 0)
14897 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14898 return;
14899 /* Consume the next token. */
14900 token = cp_lexer_consume_token (parser->lexer);
14901 /* If we've reached the end of the file, stop. */
14902 if (token->type == CPP_EOF)
14903 return;
14904 /* Add this token to the tokens we are saving. */
14905 cp_token_cache_push_token (cache, token);
14906 /* See if it starts a new group. */
14907 if (token->type == CPP_OPEN_BRACE)
14909 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14910 if (depth == 0)
14911 return;
14913 else if (token->type == CPP_OPEN_PAREN)
14914 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14915 else if (token->type == end)
14916 return;
14920 /* Begin parsing tentatively. We always save tokens while parsing
14921 tentatively so that if the tentative parsing fails we can restore the
14922 tokens. */
14924 static void
14925 cp_parser_parse_tentatively (cp_parser* parser)
14927 /* Enter a new parsing context. */
14928 parser->context = cp_parser_context_new (parser->context);
14929 /* Begin saving tokens. */
14930 cp_lexer_save_tokens (parser->lexer);
14931 /* In order to avoid repetitive access control error messages,
14932 access checks are queued up until we are no longer parsing
14933 tentatively. */
14934 push_deferring_access_checks (dk_deferred);
14937 /* Commit to the currently active tentative parse. */
14939 static void
14940 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14942 cp_parser_context *context;
14943 cp_lexer *lexer;
14945 /* Mark all of the levels as committed. */
14946 lexer = parser->lexer;
14947 for (context = parser->context; context->next; context = context->next)
14949 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14950 break;
14951 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14952 while (!cp_lexer_saving_tokens (lexer))
14953 lexer = lexer->next;
14954 cp_lexer_commit_tokens (lexer);
14958 /* Abort the currently active tentative parse. All consumed tokens
14959 will be rolled back, and no diagnostics will be issued. */
14961 static void
14962 cp_parser_abort_tentative_parse (cp_parser* parser)
14964 cp_parser_simulate_error (parser);
14965 /* Now, pretend that we want to see if the construct was
14966 successfully parsed. */
14967 cp_parser_parse_definitely (parser);
14970 /* Stop parsing tentatively. If a parse error has occurred, restore the
14971 token stream. Otherwise, commit to the tokens we have consumed.
14972 Returns true if no error occurred; false otherwise. */
14974 static bool
14975 cp_parser_parse_definitely (cp_parser* parser)
14977 bool error_occurred;
14978 cp_parser_context *context;
14980 /* Remember whether or not an error occurred, since we are about to
14981 destroy that information. */
14982 error_occurred = cp_parser_error_occurred (parser);
14983 /* Remove the topmost context from the stack. */
14984 context = parser->context;
14985 parser->context = context->next;
14986 /* If no parse errors occurred, commit to the tentative parse. */
14987 if (!error_occurred)
14989 /* Commit to the tokens read tentatively, unless that was
14990 already done. */
14991 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14992 cp_lexer_commit_tokens (parser->lexer);
14994 pop_to_parent_deferring_access_checks ();
14996 /* Otherwise, if errors occurred, roll back our state so that things
14997 are just as they were before we began the tentative parse. */
14998 else
15000 cp_lexer_rollback_tokens (parser->lexer);
15001 pop_deferring_access_checks ();
15003 /* Add the context to the front of the free list. */
15004 context->next = cp_parser_context_free_list;
15005 cp_parser_context_free_list = context;
15007 return !error_occurred;
15010 /* Returns true if we are parsing tentatively -- but have decided that
15011 we will stick with this tentative parse, even if errors occur. */
15013 static bool
15014 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15016 return (cp_parser_parsing_tentatively (parser)
15017 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15020 /* Returns nonzero iff an error has occurred during the most recent
15021 tentative parse. */
15023 static bool
15024 cp_parser_error_occurred (cp_parser* parser)
15026 return (cp_parser_parsing_tentatively (parser)
15027 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15030 /* Returns nonzero if GNU extensions are allowed. */
15032 static bool
15033 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15035 return parser->allow_gnu_extensions_p;
15040 /* The parser. */
15042 static GTY (()) cp_parser *the_parser;
15044 /* External interface. */
15046 /* Parse one entire translation unit. */
15048 void
15049 c_parse_file (void)
15051 bool error_occurred;
15053 the_parser = cp_parser_new ();
15054 push_deferring_access_checks (flag_access_control
15055 ? dk_no_deferred : dk_no_check);
15056 error_occurred = cp_parser_translation_unit (the_parser);
15057 the_parser = NULL;
15060 /* Clean up after parsing the entire translation unit. */
15062 void
15063 free_parser_stacks (void)
15065 /* Nothing to do. */
15068 /* This variable must be provided by every front end. */
15070 int yydebug;
15072 #include "gt-cp-parser.h"