PR c++/4100
[official-gcc.git] / gcc / cp / parser.c
blobfe3a3dec9651c0fa1cab08aff90de40768a3e0a6
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 (i.e., a type declaration)
6535 2: one of the decl-specifiers is an enum-specifier or a
6536 class-specifier (i.e., a type definition)
6540 static tree
6541 cp_parser_decl_specifier_seq (cp_parser* parser,
6542 cp_parser_flags flags,
6543 tree* attributes,
6544 int* declares_class_or_enum)
6546 tree decl_specs = NULL_TREE;
6547 bool friend_p = false;
6548 bool constructor_possible_p = !parser->in_declarator_p;
6550 /* Assume no class or enumeration type is declared. */
6551 *declares_class_or_enum = 0;
6553 /* Assume there are no attributes. */
6554 *attributes = NULL_TREE;
6556 /* Keep reading specifiers until there are no more to read. */
6557 while (true)
6559 tree decl_spec = NULL_TREE;
6560 bool constructor_p;
6561 cp_token *token;
6563 /* Peek at the next token. */
6564 token = cp_lexer_peek_token (parser->lexer);
6565 /* Handle attributes. */
6566 if (token->keyword == RID_ATTRIBUTE)
6568 /* Parse the attributes. */
6569 decl_spec = cp_parser_attributes_opt (parser);
6570 /* Add them to the list. */
6571 *attributes = chainon (*attributes, decl_spec);
6572 continue;
6574 /* If the next token is an appropriate keyword, we can simply
6575 add it to the list. */
6576 switch (token->keyword)
6578 case RID_FRIEND:
6579 /* decl-specifier:
6580 friend */
6581 if (friend_p)
6582 error ("duplicate `friend'");
6583 else
6584 friend_p = true;
6585 /* The representation of the specifier is simply the
6586 appropriate TREE_IDENTIFIER node. */
6587 decl_spec = token->value;
6588 /* Consume the token. */
6589 cp_lexer_consume_token (parser->lexer);
6590 break;
6592 /* function-specifier:
6593 inline
6594 virtual
6595 explicit */
6596 case RID_INLINE:
6597 case RID_VIRTUAL:
6598 case RID_EXPLICIT:
6599 decl_spec = cp_parser_function_specifier_opt (parser);
6600 break;
6602 /* decl-specifier:
6603 typedef */
6604 case RID_TYPEDEF:
6605 /* The representation of the specifier is simply the
6606 appropriate TREE_IDENTIFIER node. */
6607 decl_spec = token->value;
6608 /* Consume the token. */
6609 cp_lexer_consume_token (parser->lexer);
6610 /* A constructor declarator cannot appear in a typedef. */
6611 constructor_possible_p = false;
6612 /* The "typedef" keyword can only occur in a declaration; we
6613 may as well commit at this point. */
6614 cp_parser_commit_to_tentative_parse (parser);
6615 break;
6617 /* storage-class-specifier:
6618 auto
6619 register
6620 static
6621 extern
6622 mutable
6624 GNU Extension:
6625 thread */
6626 case RID_AUTO:
6627 case RID_REGISTER:
6628 case RID_STATIC:
6629 case RID_EXTERN:
6630 case RID_MUTABLE:
6631 case RID_THREAD:
6632 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6633 break;
6635 default:
6636 break;
6639 /* Constructors are a special case. The `S' in `S()' is not a
6640 decl-specifier; it is the beginning of the declarator. */
6641 constructor_p = (!decl_spec
6642 && constructor_possible_p
6643 && cp_parser_constructor_declarator_p (parser,
6644 friend_p));
6646 /* If we don't have a DECL_SPEC yet, then we must be looking at
6647 a type-specifier. */
6648 if (!decl_spec && !constructor_p)
6650 int decl_spec_declares_class_or_enum;
6651 bool is_cv_qualifier;
6653 decl_spec
6654 = cp_parser_type_specifier (parser, flags,
6655 friend_p,
6656 /*is_declaration=*/true,
6657 &decl_spec_declares_class_or_enum,
6658 &is_cv_qualifier);
6660 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6662 /* If this type-specifier referenced a user-defined type
6663 (a typedef, class-name, etc.), then we can't allow any
6664 more such type-specifiers henceforth.
6666 [dcl.spec]
6668 The longest sequence of decl-specifiers that could
6669 possibly be a type name is taken as the
6670 decl-specifier-seq of a declaration. The sequence shall
6671 be self-consistent as described below.
6673 [dcl.type]
6675 As a general rule, at most one type-specifier is allowed
6676 in the complete decl-specifier-seq of a declaration. The
6677 only exceptions are the following:
6679 -- const or volatile can be combined with any other
6680 type-specifier.
6682 -- signed or unsigned can be combined with char, long,
6683 short, or int.
6685 -- ..
6687 Example:
6689 typedef char* Pc;
6690 void g (const int Pc);
6692 Here, Pc is *not* part of the decl-specifier seq; it's
6693 the declarator. Therefore, once we see a type-specifier
6694 (other than a cv-qualifier), we forbid any additional
6695 user-defined types. We *do* still allow things like `int
6696 int' to be considered a decl-specifier-seq, and issue the
6697 error message later. */
6698 if (decl_spec && !is_cv_qualifier)
6699 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6700 /* A constructor declarator cannot follow a type-specifier. */
6701 if (decl_spec)
6702 constructor_possible_p = false;
6705 /* If we still do not have a DECL_SPEC, then there are no more
6706 decl-specifiers. */
6707 if (!decl_spec)
6709 /* Issue an error message, unless the entire construct was
6710 optional. */
6711 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6713 cp_parser_error (parser, "expected decl specifier");
6714 return error_mark_node;
6717 break;
6720 /* Add the DECL_SPEC to the list of specifiers. */
6721 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6722 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6724 /* After we see one decl-specifier, further decl-specifiers are
6725 always optional. */
6726 flags |= CP_PARSER_FLAGS_OPTIONAL;
6729 /* Don't allow a friend specifier with a class definition. */
6730 if (friend_p && (*declares_class_or_enum & 2))
6731 error ("class definition may not be declared a friend");
6733 /* We have built up the DECL_SPECS in reverse order. Return them in
6734 the correct order. */
6735 return nreverse (decl_specs);
6738 /* Parse an (optional) storage-class-specifier.
6740 storage-class-specifier:
6741 auto
6742 register
6743 static
6744 extern
6745 mutable
6747 GNU Extension:
6749 storage-class-specifier:
6750 thread
6752 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6754 static tree
6755 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6757 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6759 case RID_AUTO:
6760 case RID_REGISTER:
6761 case RID_STATIC:
6762 case RID_EXTERN:
6763 case RID_MUTABLE:
6764 case RID_THREAD:
6765 /* Consume the token. */
6766 return cp_lexer_consume_token (parser->lexer)->value;
6768 default:
6769 return NULL_TREE;
6773 /* Parse an (optional) function-specifier.
6775 function-specifier:
6776 inline
6777 virtual
6778 explicit
6780 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6782 static tree
6783 cp_parser_function_specifier_opt (cp_parser* parser)
6785 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6787 case RID_INLINE:
6788 case RID_VIRTUAL:
6789 case RID_EXPLICIT:
6790 /* Consume the token. */
6791 return cp_lexer_consume_token (parser->lexer)->value;
6793 default:
6794 return NULL_TREE;
6798 /* Parse a linkage-specification.
6800 linkage-specification:
6801 extern string-literal { declaration-seq [opt] }
6802 extern string-literal declaration */
6804 static void
6805 cp_parser_linkage_specification (cp_parser* parser)
6807 cp_token *token;
6808 tree linkage;
6810 /* Look for the `extern' keyword. */
6811 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6813 /* Peek at the next token. */
6814 token = cp_lexer_peek_token (parser->lexer);
6815 /* If it's not a string-literal, then there's a problem. */
6816 if (!cp_parser_is_string_literal (token))
6818 cp_parser_error (parser, "expected language-name");
6819 return;
6821 /* Consume the token. */
6822 cp_lexer_consume_token (parser->lexer);
6824 /* Transform the literal into an identifier. If the literal is a
6825 wide-character string, or contains embedded NULs, then we can't
6826 handle it as the user wants. */
6827 if (token->type == CPP_WSTRING
6828 || (strlen (TREE_STRING_POINTER (token->value))
6829 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6831 cp_parser_error (parser, "invalid linkage-specification");
6832 /* Assume C++ linkage. */
6833 linkage = get_identifier ("c++");
6835 /* If it's a simple string constant, things are easier. */
6836 else
6837 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6839 /* We're now using the new linkage. */
6840 push_lang_context (linkage);
6842 /* If the next token is a `{', then we're using the first
6843 production. */
6844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6846 /* Consume the `{' token. */
6847 cp_lexer_consume_token (parser->lexer);
6848 /* Parse the declarations. */
6849 cp_parser_declaration_seq_opt (parser);
6850 /* Look for the closing `}'. */
6851 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6853 /* Otherwise, there's just one declaration. */
6854 else
6856 bool saved_in_unbraced_linkage_specification_p;
6858 saved_in_unbraced_linkage_specification_p
6859 = parser->in_unbraced_linkage_specification_p;
6860 parser->in_unbraced_linkage_specification_p = true;
6861 have_extern_spec = true;
6862 cp_parser_declaration (parser);
6863 have_extern_spec = false;
6864 parser->in_unbraced_linkage_specification_p
6865 = saved_in_unbraced_linkage_specification_p;
6868 /* We're done with the linkage-specification. */
6869 pop_lang_context ();
6872 /* Special member functions [gram.special] */
6874 /* Parse a conversion-function-id.
6876 conversion-function-id:
6877 operator conversion-type-id
6879 Returns an IDENTIFIER_NODE representing the operator. */
6881 static tree
6882 cp_parser_conversion_function_id (cp_parser* parser)
6884 tree type;
6885 tree saved_scope;
6886 tree saved_qualifying_scope;
6887 tree saved_object_scope;
6889 /* Look for the `operator' token. */
6890 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6891 return error_mark_node;
6892 /* When we parse the conversion-type-id, the current scope will be
6893 reset. However, we need that information in able to look up the
6894 conversion function later, so we save it here. */
6895 saved_scope = parser->scope;
6896 saved_qualifying_scope = parser->qualifying_scope;
6897 saved_object_scope = parser->object_scope;
6898 /* We must enter the scope of the class so that the names of
6899 entities declared within the class are available in the
6900 conversion-type-id. For example, consider:
6902 struct S {
6903 typedef int I;
6904 operator I();
6907 S::operator I() { ... }
6909 In order to see that `I' is a type-name in the definition, we
6910 must be in the scope of `S'. */
6911 if (saved_scope)
6912 push_scope (saved_scope);
6913 /* Parse the conversion-type-id. */
6914 type = cp_parser_conversion_type_id (parser);
6915 /* Leave the scope of the class, if any. */
6916 if (saved_scope)
6917 pop_scope (saved_scope);
6918 /* Restore the saved scope. */
6919 parser->scope = saved_scope;
6920 parser->qualifying_scope = saved_qualifying_scope;
6921 parser->object_scope = saved_object_scope;
6922 /* If the TYPE is invalid, indicate failure. */
6923 if (type == error_mark_node)
6924 return error_mark_node;
6925 return mangle_conv_op_name_for_type (type);
6928 /* Parse a conversion-type-id:
6930 conversion-type-id:
6931 type-specifier-seq conversion-declarator [opt]
6933 Returns the TYPE specified. */
6935 static tree
6936 cp_parser_conversion_type_id (cp_parser* parser)
6938 tree attributes;
6939 tree type_specifiers;
6940 tree declarator;
6942 /* Parse the attributes. */
6943 attributes = cp_parser_attributes_opt (parser);
6944 /* Parse the type-specifiers. */
6945 type_specifiers = cp_parser_type_specifier_seq (parser);
6946 /* If that didn't work, stop. */
6947 if (type_specifiers == error_mark_node)
6948 return error_mark_node;
6949 /* Parse the conversion-declarator. */
6950 declarator = cp_parser_conversion_declarator_opt (parser);
6952 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6953 /*initialized=*/0, &attributes);
6956 /* Parse an (optional) conversion-declarator.
6958 conversion-declarator:
6959 ptr-operator conversion-declarator [opt]
6961 Returns a representation of the declarator. See
6962 cp_parser_declarator for details. */
6964 static tree
6965 cp_parser_conversion_declarator_opt (cp_parser* parser)
6967 enum tree_code code;
6968 tree class_type;
6969 tree cv_qualifier_seq;
6971 /* We don't know if there's a ptr-operator next, or not. */
6972 cp_parser_parse_tentatively (parser);
6973 /* Try the ptr-operator. */
6974 code = cp_parser_ptr_operator (parser, &class_type,
6975 &cv_qualifier_seq);
6976 /* If it worked, look for more conversion-declarators. */
6977 if (cp_parser_parse_definitely (parser))
6979 tree declarator;
6981 /* Parse another optional declarator. */
6982 declarator = cp_parser_conversion_declarator_opt (parser);
6984 /* Create the representation of the declarator. */
6985 if (code == INDIRECT_REF)
6986 declarator = make_pointer_declarator (cv_qualifier_seq,
6987 declarator);
6988 else
6989 declarator = make_reference_declarator (cv_qualifier_seq,
6990 declarator);
6992 /* Handle the pointer-to-member case. */
6993 if (class_type)
6994 declarator = build_nt (SCOPE_REF, class_type, declarator);
6996 return declarator;
6999 return NULL_TREE;
7002 /* Parse an (optional) ctor-initializer.
7004 ctor-initializer:
7005 : mem-initializer-list
7007 Returns TRUE iff the ctor-initializer was actually present. */
7009 static bool
7010 cp_parser_ctor_initializer_opt (cp_parser* parser)
7012 /* If the next token is not a `:', then there is no
7013 ctor-initializer. */
7014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7016 /* Do default initialization of any bases and members. */
7017 if (DECL_CONSTRUCTOR_P (current_function_decl))
7018 finish_mem_initializers (NULL_TREE);
7020 return false;
7023 /* Consume the `:' token. */
7024 cp_lexer_consume_token (parser->lexer);
7025 /* And the mem-initializer-list. */
7026 cp_parser_mem_initializer_list (parser);
7028 return true;
7031 /* Parse a mem-initializer-list.
7033 mem-initializer-list:
7034 mem-initializer
7035 mem-initializer , mem-initializer-list */
7037 static void
7038 cp_parser_mem_initializer_list (cp_parser* parser)
7040 tree mem_initializer_list = NULL_TREE;
7042 /* Let the semantic analysis code know that we are starting the
7043 mem-initializer-list. */
7044 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7045 error ("only constructors take base initializers");
7047 /* Loop through the list. */
7048 while (true)
7050 tree mem_initializer;
7052 /* Parse the mem-initializer. */
7053 mem_initializer = cp_parser_mem_initializer (parser);
7054 /* Add it to the list, unless it was erroneous. */
7055 if (mem_initializer)
7057 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7058 mem_initializer_list = mem_initializer;
7060 /* If the next token is not a `,', we're done. */
7061 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7062 break;
7063 /* Consume the `,' token. */
7064 cp_lexer_consume_token (parser->lexer);
7067 /* Perform semantic analysis. */
7068 if (DECL_CONSTRUCTOR_P (current_function_decl))
7069 finish_mem_initializers (mem_initializer_list);
7072 /* Parse a mem-initializer.
7074 mem-initializer:
7075 mem-initializer-id ( expression-list [opt] )
7077 GNU extension:
7079 mem-initializer:
7080 ( expression-list [opt] )
7082 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7083 class) or FIELD_DECL (for a non-static data member) to initialize;
7084 the TREE_VALUE is the expression-list. */
7086 static tree
7087 cp_parser_mem_initializer (cp_parser* parser)
7089 tree mem_initializer_id;
7090 tree expression_list;
7091 tree member;
7093 /* Find out what is being initialized. */
7094 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7096 pedwarn ("anachronistic old-style base class initializer");
7097 mem_initializer_id = NULL_TREE;
7099 else
7100 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7101 member = expand_member_init (mem_initializer_id);
7102 if (member && !DECL_P (member))
7103 in_base_initializer = 1;
7105 expression_list
7106 = cp_parser_parenthesized_expression_list (parser, false,
7107 /*non_constant_p=*/NULL);
7108 if (!expression_list)
7109 expression_list = void_type_node;
7111 in_base_initializer = 0;
7113 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7116 /* Parse a mem-initializer-id.
7118 mem-initializer-id:
7119 :: [opt] nested-name-specifier [opt] class-name
7120 identifier
7122 Returns a TYPE indicating the class to be initializer for the first
7123 production. Returns an IDENTIFIER_NODE indicating the data member
7124 to be initialized for the second production. */
7126 static tree
7127 cp_parser_mem_initializer_id (cp_parser* parser)
7129 bool global_scope_p;
7130 bool nested_name_specifier_p;
7131 tree id;
7133 /* Look for the optional `::' operator. */
7134 global_scope_p
7135 = (cp_parser_global_scope_opt (parser,
7136 /*current_scope_valid_p=*/false)
7137 != NULL_TREE);
7138 /* Look for the optional nested-name-specifier. The simplest way to
7139 implement:
7141 [temp.res]
7143 The keyword `typename' is not permitted in a base-specifier or
7144 mem-initializer; in these contexts a qualified name that
7145 depends on a template-parameter is implicitly assumed to be a
7146 type name.
7148 is to assume that we have seen the `typename' keyword at this
7149 point. */
7150 nested_name_specifier_p
7151 = (cp_parser_nested_name_specifier_opt (parser,
7152 /*typename_keyword_p=*/true,
7153 /*check_dependency_p=*/true,
7154 /*type_p=*/true,
7155 /*is_declaration=*/true)
7156 != NULL_TREE);
7157 /* If there is a `::' operator or a nested-name-specifier, then we
7158 are definitely looking for a class-name. */
7159 if (global_scope_p || nested_name_specifier_p)
7160 return cp_parser_class_name (parser,
7161 /*typename_keyword_p=*/true,
7162 /*template_keyword_p=*/false,
7163 /*type_p=*/false,
7164 /*check_dependency_p=*/true,
7165 /*class_head_p=*/false,
7166 /*is_declaration=*/true);
7167 /* Otherwise, we could also be looking for an ordinary identifier. */
7168 cp_parser_parse_tentatively (parser);
7169 /* Try a class-name. */
7170 id = cp_parser_class_name (parser,
7171 /*typename_keyword_p=*/true,
7172 /*template_keyword_p=*/false,
7173 /*type_p=*/false,
7174 /*check_dependency_p=*/true,
7175 /*class_head_p=*/false,
7176 /*is_declaration=*/true);
7177 /* If we found one, we're done. */
7178 if (cp_parser_parse_definitely (parser))
7179 return id;
7180 /* Otherwise, look for an ordinary identifier. */
7181 return cp_parser_identifier (parser);
7184 /* Overloading [gram.over] */
7186 /* Parse an operator-function-id.
7188 operator-function-id:
7189 operator operator
7191 Returns an IDENTIFIER_NODE for the operator which is a
7192 human-readable spelling of the identifier, e.g., `operator +'. */
7194 static tree
7195 cp_parser_operator_function_id (cp_parser* parser)
7197 /* Look for the `operator' keyword. */
7198 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7199 return error_mark_node;
7200 /* And then the name of the operator itself. */
7201 return cp_parser_operator (parser);
7204 /* Parse an operator.
7206 operator:
7207 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7208 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7209 || ++ -- , ->* -> () []
7211 GNU Extensions:
7213 operator:
7214 <? >? <?= >?=
7216 Returns an IDENTIFIER_NODE for the operator which is a
7217 human-readable spelling of the identifier, e.g., `operator +'. */
7219 static tree
7220 cp_parser_operator (cp_parser* parser)
7222 tree id = NULL_TREE;
7223 cp_token *token;
7225 /* Peek at the next token. */
7226 token = cp_lexer_peek_token (parser->lexer);
7227 /* Figure out which operator we have. */
7228 switch (token->type)
7230 case CPP_KEYWORD:
7232 enum tree_code op;
7234 /* The keyword should be either `new' or `delete'. */
7235 if (token->keyword == RID_NEW)
7236 op = NEW_EXPR;
7237 else if (token->keyword == RID_DELETE)
7238 op = DELETE_EXPR;
7239 else
7240 break;
7242 /* Consume the `new' or `delete' token. */
7243 cp_lexer_consume_token (parser->lexer);
7245 /* Peek at the next token. */
7246 token = cp_lexer_peek_token (parser->lexer);
7247 /* If it's a `[' token then this is the array variant of the
7248 operator. */
7249 if (token->type == CPP_OPEN_SQUARE)
7251 /* Consume the `[' token. */
7252 cp_lexer_consume_token (parser->lexer);
7253 /* Look for the `]' token. */
7254 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7255 id = ansi_opname (op == NEW_EXPR
7256 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7258 /* Otherwise, we have the non-array variant. */
7259 else
7260 id = ansi_opname (op);
7262 return id;
7265 case CPP_PLUS:
7266 id = ansi_opname (PLUS_EXPR);
7267 break;
7269 case CPP_MINUS:
7270 id = ansi_opname (MINUS_EXPR);
7271 break;
7273 case CPP_MULT:
7274 id = ansi_opname (MULT_EXPR);
7275 break;
7277 case CPP_DIV:
7278 id = ansi_opname (TRUNC_DIV_EXPR);
7279 break;
7281 case CPP_MOD:
7282 id = ansi_opname (TRUNC_MOD_EXPR);
7283 break;
7285 case CPP_XOR:
7286 id = ansi_opname (BIT_XOR_EXPR);
7287 break;
7289 case CPP_AND:
7290 id = ansi_opname (BIT_AND_EXPR);
7291 break;
7293 case CPP_OR:
7294 id = ansi_opname (BIT_IOR_EXPR);
7295 break;
7297 case CPP_COMPL:
7298 id = ansi_opname (BIT_NOT_EXPR);
7299 break;
7301 case CPP_NOT:
7302 id = ansi_opname (TRUTH_NOT_EXPR);
7303 break;
7305 case CPP_EQ:
7306 id = ansi_assopname (NOP_EXPR);
7307 break;
7309 case CPP_LESS:
7310 id = ansi_opname (LT_EXPR);
7311 break;
7313 case CPP_GREATER:
7314 id = ansi_opname (GT_EXPR);
7315 break;
7317 case CPP_PLUS_EQ:
7318 id = ansi_assopname (PLUS_EXPR);
7319 break;
7321 case CPP_MINUS_EQ:
7322 id = ansi_assopname (MINUS_EXPR);
7323 break;
7325 case CPP_MULT_EQ:
7326 id = ansi_assopname (MULT_EXPR);
7327 break;
7329 case CPP_DIV_EQ:
7330 id = ansi_assopname (TRUNC_DIV_EXPR);
7331 break;
7333 case CPP_MOD_EQ:
7334 id = ansi_assopname (TRUNC_MOD_EXPR);
7335 break;
7337 case CPP_XOR_EQ:
7338 id = ansi_assopname (BIT_XOR_EXPR);
7339 break;
7341 case CPP_AND_EQ:
7342 id = ansi_assopname (BIT_AND_EXPR);
7343 break;
7345 case CPP_OR_EQ:
7346 id = ansi_assopname (BIT_IOR_EXPR);
7347 break;
7349 case CPP_LSHIFT:
7350 id = ansi_opname (LSHIFT_EXPR);
7351 break;
7353 case CPP_RSHIFT:
7354 id = ansi_opname (RSHIFT_EXPR);
7355 break;
7357 case CPP_LSHIFT_EQ:
7358 id = ansi_assopname (LSHIFT_EXPR);
7359 break;
7361 case CPP_RSHIFT_EQ:
7362 id = ansi_assopname (RSHIFT_EXPR);
7363 break;
7365 case CPP_EQ_EQ:
7366 id = ansi_opname (EQ_EXPR);
7367 break;
7369 case CPP_NOT_EQ:
7370 id = ansi_opname (NE_EXPR);
7371 break;
7373 case CPP_LESS_EQ:
7374 id = ansi_opname (LE_EXPR);
7375 break;
7377 case CPP_GREATER_EQ:
7378 id = ansi_opname (GE_EXPR);
7379 break;
7381 case CPP_AND_AND:
7382 id = ansi_opname (TRUTH_ANDIF_EXPR);
7383 break;
7385 case CPP_OR_OR:
7386 id = ansi_opname (TRUTH_ORIF_EXPR);
7387 break;
7389 case CPP_PLUS_PLUS:
7390 id = ansi_opname (POSTINCREMENT_EXPR);
7391 break;
7393 case CPP_MINUS_MINUS:
7394 id = ansi_opname (PREDECREMENT_EXPR);
7395 break;
7397 case CPP_COMMA:
7398 id = ansi_opname (COMPOUND_EXPR);
7399 break;
7401 case CPP_DEREF_STAR:
7402 id = ansi_opname (MEMBER_REF);
7403 break;
7405 case CPP_DEREF:
7406 id = ansi_opname (COMPONENT_REF);
7407 break;
7409 case CPP_OPEN_PAREN:
7410 /* Consume the `('. */
7411 cp_lexer_consume_token (parser->lexer);
7412 /* Look for the matching `)'. */
7413 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7414 return ansi_opname (CALL_EXPR);
7416 case CPP_OPEN_SQUARE:
7417 /* Consume the `['. */
7418 cp_lexer_consume_token (parser->lexer);
7419 /* Look for the matching `]'. */
7420 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7421 return ansi_opname (ARRAY_REF);
7423 /* Extensions. */
7424 case CPP_MIN:
7425 id = ansi_opname (MIN_EXPR);
7426 break;
7428 case CPP_MAX:
7429 id = ansi_opname (MAX_EXPR);
7430 break;
7432 case CPP_MIN_EQ:
7433 id = ansi_assopname (MIN_EXPR);
7434 break;
7436 case CPP_MAX_EQ:
7437 id = ansi_assopname (MAX_EXPR);
7438 break;
7440 default:
7441 /* Anything else is an error. */
7442 break;
7445 /* If we have selected an identifier, we need to consume the
7446 operator token. */
7447 if (id)
7448 cp_lexer_consume_token (parser->lexer);
7449 /* Otherwise, no valid operator name was present. */
7450 else
7452 cp_parser_error (parser, "expected operator");
7453 id = error_mark_node;
7456 return id;
7459 /* Parse a template-declaration.
7461 template-declaration:
7462 export [opt] template < template-parameter-list > declaration
7464 If MEMBER_P is TRUE, this template-declaration occurs within a
7465 class-specifier.
7467 The grammar rule given by the standard isn't correct. What
7468 is really meant is:
7470 template-declaration:
7471 export [opt] template-parameter-list-seq
7472 decl-specifier-seq [opt] init-declarator [opt] ;
7473 export [opt] template-parameter-list-seq
7474 function-definition
7476 template-parameter-list-seq:
7477 template-parameter-list-seq [opt]
7478 template < template-parameter-list > */
7480 static void
7481 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7483 /* Check for `export'. */
7484 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7486 /* Consume the `export' token. */
7487 cp_lexer_consume_token (parser->lexer);
7488 /* Warn that we do not support `export'. */
7489 warning ("keyword `export' not implemented, and will be ignored");
7492 cp_parser_template_declaration_after_export (parser, member_p);
7495 /* Parse a template-parameter-list.
7497 template-parameter-list:
7498 template-parameter
7499 template-parameter-list , template-parameter
7501 Returns a TREE_LIST. Each node represents a template parameter.
7502 The nodes are connected via their TREE_CHAINs. */
7504 static tree
7505 cp_parser_template_parameter_list (cp_parser* parser)
7507 tree parameter_list = NULL_TREE;
7509 while (true)
7511 tree parameter;
7512 cp_token *token;
7514 /* Parse the template-parameter. */
7515 parameter = cp_parser_template_parameter (parser);
7516 /* Add it to the list. */
7517 parameter_list = process_template_parm (parameter_list,
7518 parameter);
7520 /* Peek at the next token. */
7521 token = cp_lexer_peek_token (parser->lexer);
7522 /* If it's not a `,', we're done. */
7523 if (token->type != CPP_COMMA)
7524 break;
7525 /* Otherwise, consume the `,' token. */
7526 cp_lexer_consume_token (parser->lexer);
7529 return parameter_list;
7532 /* Parse a template-parameter.
7534 template-parameter:
7535 type-parameter
7536 parameter-declaration
7538 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7539 TREE_PURPOSE is the default value, if any. */
7541 static tree
7542 cp_parser_template_parameter (cp_parser* parser)
7544 cp_token *token;
7546 /* Peek at the next token. */
7547 token = cp_lexer_peek_token (parser->lexer);
7548 /* If it is `class' or `template', we have a type-parameter. */
7549 if (token->keyword == RID_TEMPLATE)
7550 return cp_parser_type_parameter (parser);
7551 /* If it is `class' or `typename' we do not know yet whether it is a
7552 type parameter or a non-type parameter. Consider:
7554 template <typename T, typename T::X X> ...
7558 template <class C, class D*> ...
7560 Here, the first parameter is a type parameter, and the second is
7561 a non-type parameter. We can tell by looking at the token after
7562 the identifier -- if it is a `,', `=', or `>' then we have a type
7563 parameter. */
7564 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7566 /* Peek at the token after `class' or `typename'. */
7567 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7568 /* If it's an identifier, skip it. */
7569 if (token->type == CPP_NAME)
7570 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7571 /* Now, see if the token looks like the end of a template
7572 parameter. */
7573 if (token->type == CPP_COMMA
7574 || token->type == CPP_EQ
7575 || token->type == CPP_GREATER)
7576 return cp_parser_type_parameter (parser);
7579 /* Otherwise, it is a non-type parameter.
7581 [temp.param]
7583 When parsing a default template-argument for a non-type
7584 template-parameter, the first non-nested `>' is taken as the end
7585 of the template parameter-list rather than a greater-than
7586 operator. */
7587 return
7588 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7589 /*parenthesized_p=*/NULL);
7592 /* Parse a type-parameter.
7594 type-parameter:
7595 class identifier [opt]
7596 class identifier [opt] = type-id
7597 typename identifier [opt]
7598 typename identifier [opt] = type-id
7599 template < template-parameter-list > class identifier [opt]
7600 template < template-parameter-list > class identifier [opt]
7601 = id-expression
7603 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7604 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7605 the declaration of the parameter. */
7607 static tree
7608 cp_parser_type_parameter (cp_parser* parser)
7610 cp_token *token;
7611 tree parameter;
7613 /* Look for a keyword to tell us what kind of parameter this is. */
7614 token = cp_parser_require (parser, CPP_KEYWORD,
7615 "`class', `typename', or `template'");
7616 if (!token)
7617 return error_mark_node;
7619 switch (token->keyword)
7621 case RID_CLASS:
7622 case RID_TYPENAME:
7624 tree identifier;
7625 tree default_argument;
7627 /* If the next token is an identifier, then it names the
7628 parameter. */
7629 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7630 identifier = cp_parser_identifier (parser);
7631 else
7632 identifier = NULL_TREE;
7634 /* Create the parameter. */
7635 parameter = finish_template_type_parm (class_type_node, identifier);
7637 /* If the next token is an `=', we have a default argument. */
7638 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7640 /* Consume the `=' token. */
7641 cp_lexer_consume_token (parser->lexer);
7642 /* Parse the default-argument. */
7643 default_argument = cp_parser_type_id (parser);
7645 else
7646 default_argument = NULL_TREE;
7648 /* Create the combined representation of the parameter and the
7649 default argument. */
7650 parameter = build_tree_list (default_argument, parameter);
7652 break;
7654 case RID_TEMPLATE:
7656 tree parameter_list;
7657 tree identifier;
7658 tree default_argument;
7660 /* Look for the `<'. */
7661 cp_parser_require (parser, CPP_LESS, "`<'");
7662 /* Parse the template-parameter-list. */
7663 begin_template_parm_list ();
7664 parameter_list
7665 = cp_parser_template_parameter_list (parser);
7666 parameter_list = end_template_parm_list (parameter_list);
7667 /* Look for the `>'. */
7668 cp_parser_require (parser, CPP_GREATER, "`>'");
7669 /* Look for the `class' keyword. */
7670 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7671 /* If the next token is an `=', then there is a
7672 default-argument. If the next token is a `>', we are at
7673 the end of the parameter-list. If the next token is a `,',
7674 then we are at the end of this parameter. */
7675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7676 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7677 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7678 identifier = cp_parser_identifier (parser);
7679 else
7680 identifier = NULL_TREE;
7681 /* Create the template parameter. */
7682 parameter = finish_template_template_parm (class_type_node,
7683 identifier);
7685 /* If the next token is an `=', then there is a
7686 default-argument. */
7687 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7689 bool is_template;
7691 /* Consume the `='. */
7692 cp_lexer_consume_token (parser->lexer);
7693 /* Parse the id-expression. */
7694 default_argument
7695 = cp_parser_id_expression (parser,
7696 /*template_keyword_p=*/false,
7697 /*check_dependency_p=*/true,
7698 /*template_p=*/&is_template,
7699 /*declarator_p=*/false);
7700 /* Look up the name. */
7701 default_argument
7702 = cp_parser_lookup_name (parser, default_argument,
7703 /*is_type=*/false,
7704 /*is_template=*/is_template,
7705 /*is_namespace=*/false,
7706 /*check_dependency=*/true);
7707 /* See if the default argument is valid. */
7708 default_argument
7709 = check_template_template_default_arg (default_argument);
7711 else
7712 default_argument = NULL_TREE;
7714 /* Create the combined representation of the parameter and the
7715 default argument. */
7716 parameter = build_tree_list (default_argument, parameter);
7718 break;
7720 default:
7721 /* Anything else is an error. */
7722 cp_parser_error (parser,
7723 "expected `class', `typename', or `template'");
7724 parameter = error_mark_node;
7727 return parameter;
7730 /* Parse a template-id.
7732 template-id:
7733 template-name < template-argument-list [opt] >
7735 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7736 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7737 returned. Otherwise, if the template-name names a function, or set
7738 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7739 names a class, returns a TYPE_DECL for the specialization.
7741 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7742 uninstantiated templates. */
7744 static tree
7745 cp_parser_template_id (cp_parser *parser,
7746 bool template_keyword_p,
7747 bool check_dependency_p,
7748 bool is_declaration)
7750 tree template;
7751 tree arguments;
7752 tree template_id;
7753 ptrdiff_t start_of_id;
7754 tree access_check = NULL_TREE;
7755 cp_token *next_token;
7756 bool is_identifier;
7758 /* If the next token corresponds to a template-id, there is no need
7759 to reparse it. */
7760 next_token = cp_lexer_peek_token (parser->lexer);
7761 if (next_token->type == CPP_TEMPLATE_ID)
7763 tree value;
7764 tree check;
7766 /* Get the stored value. */
7767 value = cp_lexer_consume_token (parser->lexer)->value;
7768 /* Perform any access checks that were deferred. */
7769 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7770 perform_or_defer_access_check (TREE_PURPOSE (check),
7771 TREE_VALUE (check));
7772 /* Return the stored value. */
7773 return TREE_VALUE (value);
7776 /* Avoid performing name lookup if there is no possibility of
7777 finding a template-id. */
7778 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7779 || (next_token->type == CPP_NAME
7780 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7782 cp_parser_error (parser, "expected template-id");
7783 return error_mark_node;
7786 /* Remember where the template-id starts. */
7787 if (cp_parser_parsing_tentatively (parser)
7788 && !cp_parser_committed_to_tentative_parse (parser))
7790 next_token = cp_lexer_peek_token (parser->lexer);
7791 start_of_id = cp_lexer_token_difference (parser->lexer,
7792 parser->lexer->first_token,
7793 next_token);
7795 else
7796 start_of_id = -1;
7798 push_deferring_access_checks (dk_deferred);
7800 /* Parse the template-name. */
7801 is_identifier = false;
7802 template = cp_parser_template_name (parser, template_keyword_p,
7803 check_dependency_p,
7804 is_declaration,
7805 &is_identifier);
7806 if (template == error_mark_node || is_identifier)
7808 pop_deferring_access_checks ();
7809 return template;
7812 /* Look for the `<' that starts the template-argument-list. */
7813 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7815 pop_deferring_access_checks ();
7816 return error_mark_node;
7819 /* Parse the arguments. */
7820 arguments = cp_parser_enclosed_template_argument_list (parser);
7822 /* Build a representation of the specialization. */
7823 if (TREE_CODE (template) == IDENTIFIER_NODE)
7824 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7825 else if (DECL_CLASS_TEMPLATE_P (template)
7826 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7827 template_id
7828 = finish_template_type (template, arguments,
7829 cp_lexer_next_token_is (parser->lexer,
7830 CPP_SCOPE));
7831 else
7833 /* If it's not a class-template or a template-template, it should be
7834 a function-template. */
7835 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7836 || TREE_CODE (template) == OVERLOAD
7837 || BASELINK_P (template)),
7838 20010716);
7840 template_id = lookup_template_function (template, arguments);
7843 /* Retrieve any deferred checks. Do not pop this access checks yet
7844 so the memory will not be reclaimed during token replacing below. */
7845 access_check = get_deferred_access_checks ();
7847 /* If parsing tentatively, replace the sequence of tokens that makes
7848 up the template-id with a CPP_TEMPLATE_ID token. That way,
7849 should we re-parse the token stream, we will not have to repeat
7850 the effort required to do the parse, nor will we issue duplicate
7851 error messages about problems during instantiation of the
7852 template. */
7853 if (start_of_id >= 0)
7855 cp_token *token;
7857 /* Find the token that corresponds to the start of the
7858 template-id. */
7859 token = cp_lexer_advance_token (parser->lexer,
7860 parser->lexer->first_token,
7861 start_of_id);
7863 /* Reset the contents of the START_OF_ID token. */
7864 token->type = CPP_TEMPLATE_ID;
7865 token->value = build_tree_list (access_check, template_id);
7866 token->keyword = RID_MAX;
7867 /* Purge all subsequent tokens. */
7868 cp_lexer_purge_tokens_after (parser->lexer, token);
7871 pop_deferring_access_checks ();
7872 return template_id;
7875 /* Parse a template-name.
7877 template-name:
7878 identifier
7880 The standard should actually say:
7882 template-name:
7883 identifier
7884 operator-function-id
7885 conversion-function-id
7887 A defect report has been filed about this issue.
7889 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7890 `template' keyword, in a construction like:
7892 T::template f<3>()
7894 In that case `f' is taken to be a template-name, even though there
7895 is no way of knowing for sure.
7897 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7898 name refers to a set of overloaded functions, at least one of which
7899 is a template, or an IDENTIFIER_NODE with the name of the template,
7900 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7901 names are looked up inside uninstantiated templates. */
7903 static tree
7904 cp_parser_template_name (cp_parser* parser,
7905 bool template_keyword_p,
7906 bool check_dependency_p,
7907 bool is_declaration,
7908 bool *is_identifier)
7910 tree identifier;
7911 tree decl;
7912 tree fns;
7914 /* If the next token is `operator', then we have either an
7915 operator-function-id or a conversion-function-id. */
7916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7918 /* We don't know whether we're looking at an
7919 operator-function-id or a conversion-function-id. */
7920 cp_parser_parse_tentatively (parser);
7921 /* Try an operator-function-id. */
7922 identifier = cp_parser_operator_function_id (parser);
7923 /* If that didn't work, try a conversion-function-id. */
7924 if (!cp_parser_parse_definitely (parser))
7925 identifier = cp_parser_conversion_function_id (parser);
7927 /* Look for the identifier. */
7928 else
7929 identifier = cp_parser_identifier (parser);
7931 /* If we didn't find an identifier, we don't have a template-id. */
7932 if (identifier == error_mark_node)
7933 return error_mark_node;
7935 /* If the name immediately followed the `template' keyword, then it
7936 is a template-name. However, if the next token is not `<', then
7937 we do not treat it as a template-name, since it is not being used
7938 as part of a template-id. This enables us to handle constructs
7939 like:
7941 template <typename T> struct S { S(); };
7942 template <typename T> S<T>::S();
7944 correctly. We would treat `S' as a template -- if it were `S<T>'
7945 -- but we do not if there is no `<'. */
7947 if (processing_template_decl
7948 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7950 /* In a declaration, in a dependent context, we pretend that the
7951 "template" keyword was present in order to improve error
7952 recovery. For example, given:
7954 template <typename T> void f(T::X<int>);
7956 we want to treat "X<int>" as a template-id. */
7957 if (is_declaration
7958 && !template_keyword_p
7959 && parser->scope && TYPE_P (parser->scope)
7960 && dependent_type_p (parser->scope))
7962 ptrdiff_t start;
7963 cp_token* token;
7964 /* Explain what went wrong. */
7965 error ("non-template `%D' used as template", identifier);
7966 error ("(use `%T::template %D' to indicate that it is a template)",
7967 parser->scope, identifier);
7968 /* If parsing tentatively, find the location of the "<"
7969 token. */
7970 if (cp_parser_parsing_tentatively (parser)
7971 && !cp_parser_committed_to_tentative_parse (parser))
7973 cp_parser_simulate_error (parser);
7974 token = cp_lexer_peek_token (parser->lexer);
7975 token = cp_lexer_prev_token (parser->lexer, token);
7976 start = cp_lexer_token_difference (parser->lexer,
7977 parser->lexer->first_token,
7978 token);
7980 else
7981 start = -1;
7982 /* Parse the template arguments so that we can issue error
7983 messages about them. */
7984 cp_lexer_consume_token (parser->lexer);
7985 cp_parser_enclosed_template_argument_list (parser);
7986 /* Skip tokens until we find a good place from which to
7987 continue parsing. */
7988 cp_parser_skip_to_closing_parenthesis (parser,
7989 /*recovering=*/true,
7990 /*or_comma=*/true,
7991 /*consume_paren=*/false);
7992 /* If parsing tentatively, permanently remove the
7993 template argument list. That will prevent duplicate
7994 error messages from being issued about the missing
7995 "template" keyword. */
7996 if (start >= 0)
7998 token = cp_lexer_advance_token (parser->lexer,
7999 parser->lexer->first_token,
8000 start);
8001 cp_lexer_purge_tokens_after (parser->lexer, token);
8003 if (is_identifier)
8004 *is_identifier = true;
8005 return identifier;
8007 if (template_keyword_p)
8008 return identifier;
8011 /* Look up the name. */
8012 decl = cp_parser_lookup_name (parser, identifier,
8013 /*is_type=*/false,
8014 /*is_template=*/false,
8015 /*is_namespace=*/false,
8016 check_dependency_p);
8017 decl = maybe_get_template_decl_from_type_decl (decl);
8019 /* If DECL is a template, then the name was a template-name. */
8020 if (TREE_CODE (decl) == TEMPLATE_DECL)
8022 else
8024 /* The standard does not explicitly indicate whether a name that
8025 names a set of overloaded declarations, some of which are
8026 templates, is a template-name. However, such a name should
8027 be a template-name; otherwise, there is no way to form a
8028 template-id for the overloaded templates. */
8029 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8030 if (TREE_CODE (fns) == OVERLOAD)
8032 tree fn;
8034 for (fn = fns; fn; fn = OVL_NEXT (fn))
8035 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8036 break;
8038 else
8040 /* Otherwise, the name does not name a template. */
8041 cp_parser_error (parser, "expected template-name");
8042 return error_mark_node;
8046 /* If DECL is dependent, and refers to a function, then just return
8047 its name; we will look it up again during template instantiation. */
8048 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8050 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8051 if (TYPE_P (scope) && dependent_type_p (scope))
8052 return identifier;
8055 return decl;
8058 /* Parse a template-argument-list.
8060 template-argument-list:
8061 template-argument
8062 template-argument-list , template-argument
8064 Returns a TREE_VEC containing the arguments. */
8066 static tree
8067 cp_parser_template_argument_list (cp_parser* parser)
8069 tree fixed_args[10];
8070 unsigned n_args = 0;
8071 unsigned alloced = 10;
8072 tree *arg_ary = fixed_args;
8073 tree vec;
8074 bool saved_in_template_argument_list_p;
8076 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8077 parser->in_template_argument_list_p = true;
8080 tree argument;
8082 if (n_args)
8083 /* Consume the comma. */
8084 cp_lexer_consume_token (parser->lexer);
8086 /* Parse the template-argument. */
8087 argument = cp_parser_template_argument (parser);
8088 if (n_args == alloced)
8090 alloced *= 2;
8092 if (arg_ary == fixed_args)
8094 arg_ary = xmalloc (sizeof (tree) * alloced);
8095 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8097 else
8098 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8100 arg_ary[n_args++] = argument;
8102 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8104 vec = make_tree_vec (n_args);
8106 while (n_args--)
8107 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8109 if (arg_ary != fixed_args)
8110 free (arg_ary);
8111 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8112 return vec;
8115 /* Parse a template-argument.
8117 template-argument:
8118 assignment-expression
8119 type-id
8120 id-expression
8122 The representation is that of an assignment-expression, type-id, or
8123 id-expression -- except that the qualified id-expression is
8124 evaluated, so that the value returned is either a DECL or an
8125 OVERLOAD.
8127 Although the standard says "assignment-expression", it forbids
8128 throw-expressions or assignments in the template argument.
8129 Therefore, we use "conditional-expression" instead. */
8131 static tree
8132 cp_parser_template_argument (cp_parser* parser)
8134 tree argument;
8135 bool template_p;
8136 bool address_p;
8137 bool maybe_type_id = false;
8138 cp_token *token;
8139 cp_id_kind idk;
8140 tree qualifying_class;
8142 /* There's really no way to know what we're looking at, so we just
8143 try each alternative in order.
8145 [temp.arg]
8147 In a template-argument, an ambiguity between a type-id and an
8148 expression is resolved to a type-id, regardless of the form of
8149 the corresponding template-parameter.
8151 Therefore, we try a type-id first. */
8152 cp_parser_parse_tentatively (parser);
8153 argument = cp_parser_type_id (parser);
8154 /* If there was no error parsing the type-id but the next token is a '>>',
8155 we probably found a typo for '> >'. But there are type-id which are
8156 also valid expressions. For instance:
8158 struct X { int operator >> (int); };
8159 template <int V> struct Foo {};
8160 Foo<X () >> 5> r;
8162 Here 'X()' is a valid type-id of a function type, but the user just
8163 wanted to write the expression "X() >> 5". Thus, we remember that we
8164 found a valid type-id, but we still try to parse the argument as an
8165 expression to see what happens. */
8166 if (!cp_parser_error_occurred (parser)
8167 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8169 maybe_type_id = true;
8170 cp_parser_abort_tentative_parse (parser);
8172 else
8174 /* If the next token isn't a `,' or a `>', then this argument wasn't
8175 really finished. This means that the argument is not a valid
8176 type-id. */
8177 if (!cp_parser_next_token_ends_template_argument_p (parser))
8178 cp_parser_error (parser, "expected template-argument");
8179 /* If that worked, we're done. */
8180 if (cp_parser_parse_definitely (parser))
8181 return argument;
8183 /* We're still not sure what the argument will be. */
8184 cp_parser_parse_tentatively (parser);
8185 /* Try a template. */
8186 argument = cp_parser_id_expression (parser,
8187 /*template_keyword_p=*/false,
8188 /*check_dependency_p=*/true,
8189 &template_p,
8190 /*declarator_p=*/false);
8191 /* If the next token isn't a `,' or a `>', then this argument wasn't
8192 really finished. */
8193 if (!cp_parser_next_token_ends_template_argument_p (parser))
8194 cp_parser_error (parser, "expected template-argument");
8195 if (!cp_parser_error_occurred (parser))
8197 /* Figure out what is being referred to. */
8198 argument = cp_parser_lookup_name (parser, argument,
8199 /*is_type=*/false,
8200 /*is_template=*/template_p,
8201 /*is_namespace=*/false,
8202 /*check_dependency=*/true);
8203 if (TREE_CODE (argument) != TEMPLATE_DECL
8204 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8205 cp_parser_error (parser, "expected template-name");
8207 if (cp_parser_parse_definitely (parser))
8208 return argument;
8209 /* It must be a non-type argument. There permitted cases are given
8210 in [temp.arg.nontype]:
8212 -- an integral constant-expression of integral or enumeration
8213 type; or
8215 -- the name of a non-type template-parameter; or
8217 -- the name of an object or function with external linkage...
8219 -- the address of an object or function with external linkage...
8221 -- a pointer to member... */
8222 /* Look for a non-type template parameter. */
8223 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8225 cp_parser_parse_tentatively (parser);
8226 argument = cp_parser_primary_expression (parser,
8227 &idk,
8228 &qualifying_class);
8229 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8230 || !cp_parser_next_token_ends_template_argument_p (parser))
8231 cp_parser_simulate_error (parser);
8232 if (cp_parser_parse_definitely (parser))
8233 return argument;
8235 /* If the next token is "&", the argument must be the address of an
8236 object or function with external linkage. */
8237 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8238 if (address_p)
8239 cp_lexer_consume_token (parser->lexer);
8240 /* See if we might have an id-expression. */
8241 token = cp_lexer_peek_token (parser->lexer);
8242 if (token->type == CPP_NAME
8243 || token->keyword == RID_OPERATOR
8244 || token->type == CPP_SCOPE
8245 || token->type == CPP_TEMPLATE_ID
8246 || token->type == CPP_NESTED_NAME_SPECIFIER)
8248 cp_parser_parse_tentatively (parser);
8249 argument = cp_parser_primary_expression (parser,
8250 &idk,
8251 &qualifying_class);
8252 if (cp_parser_error_occurred (parser)
8253 || !cp_parser_next_token_ends_template_argument_p (parser))
8254 cp_parser_abort_tentative_parse (parser);
8255 else
8257 if (qualifying_class)
8258 argument = finish_qualified_id_expr (qualifying_class,
8259 argument,
8260 /*done=*/true,
8261 address_p);
8262 if (TREE_CODE (argument) == VAR_DECL)
8264 /* A variable without external linkage might still be a
8265 valid constant-expression, so no error is issued here
8266 if the external-linkage check fails. */
8267 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8268 cp_parser_simulate_error (parser);
8270 else if (is_overloaded_fn (argument))
8271 /* All overloaded functions are allowed; if the external
8272 linkage test does not pass, an error will be issued
8273 later. */
8275 else if (address_p
8276 && (TREE_CODE (argument) == OFFSET_REF
8277 || TREE_CODE (argument) == SCOPE_REF))
8278 /* A pointer-to-member. */
8280 else
8281 cp_parser_simulate_error (parser);
8283 if (cp_parser_parse_definitely (parser))
8285 if (address_p)
8286 argument = build_x_unary_op (ADDR_EXPR, argument);
8287 return argument;
8291 /* If the argument started with "&", there are no other valid
8292 alternatives at this point. */
8293 if (address_p)
8295 cp_parser_error (parser, "invalid non-type template argument");
8296 return error_mark_node;
8298 /* If the argument wasn't successfully parsed as a type-id followed
8299 by '>>', the argument can only be a constant expression now.
8300 Otherwise, we try parsing the constant-expression tentatively,
8301 because the argument could really be a type-id. */
8302 if (maybe_type_id)
8303 cp_parser_parse_tentatively (parser);
8304 argument = cp_parser_constant_expression (parser,
8305 /*allow_non_constant_p=*/false,
8306 /*non_constant_p=*/NULL);
8307 argument = cp_parser_fold_non_dependent_expr (argument);
8308 if (!maybe_type_id)
8309 return argument;
8310 if (!cp_parser_next_token_ends_template_argument_p (parser))
8311 cp_parser_error (parser, "expected template-argument");
8312 if (cp_parser_parse_definitely (parser))
8313 return argument;
8314 /* We did our best to parse the argument as a non type-id, but that
8315 was the only alternative that matched (albeit with a '>' after
8316 it). We can assume it's just a typo from the user, and a
8317 diagnostic will then be issued. */
8318 return cp_parser_type_id (parser);
8321 /* Parse an explicit-instantiation.
8323 explicit-instantiation:
8324 template declaration
8326 Although the standard says `declaration', what it really means is:
8328 explicit-instantiation:
8329 template decl-specifier-seq [opt] declarator [opt] ;
8331 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8332 supposed to be allowed. A defect report has been filed about this
8333 issue.
8335 GNU Extension:
8337 explicit-instantiation:
8338 storage-class-specifier template
8339 decl-specifier-seq [opt] declarator [opt] ;
8340 function-specifier template
8341 decl-specifier-seq [opt] declarator [opt] ; */
8343 static void
8344 cp_parser_explicit_instantiation (cp_parser* parser)
8346 int declares_class_or_enum;
8347 tree decl_specifiers;
8348 tree attributes;
8349 tree extension_specifier = NULL_TREE;
8351 /* Look for an (optional) storage-class-specifier or
8352 function-specifier. */
8353 if (cp_parser_allow_gnu_extensions_p (parser))
8355 extension_specifier
8356 = cp_parser_storage_class_specifier_opt (parser);
8357 if (!extension_specifier)
8358 extension_specifier = cp_parser_function_specifier_opt (parser);
8361 /* Look for the `template' keyword. */
8362 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8363 /* Let the front end know that we are processing an explicit
8364 instantiation. */
8365 begin_explicit_instantiation ();
8366 /* [temp.explicit] says that we are supposed to ignore access
8367 control while processing explicit instantiation directives. */
8368 push_deferring_access_checks (dk_no_check);
8369 /* Parse a decl-specifier-seq. */
8370 decl_specifiers
8371 = cp_parser_decl_specifier_seq (parser,
8372 CP_PARSER_FLAGS_OPTIONAL,
8373 &attributes,
8374 &declares_class_or_enum);
8375 /* If there was exactly one decl-specifier, and it declared a class,
8376 and there's no declarator, then we have an explicit type
8377 instantiation. */
8378 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8380 tree type;
8382 type = check_tag_decl (decl_specifiers);
8383 /* Turn access control back on for names used during
8384 template instantiation. */
8385 pop_deferring_access_checks ();
8386 if (type)
8387 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8389 else
8391 tree declarator;
8392 tree decl;
8394 /* Parse the declarator. */
8395 declarator
8396 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8397 /*ctor_dtor_or_conv_p=*/NULL,
8398 /*parenthesized_p=*/NULL);
8399 cp_parser_check_for_definition_in_return_type (declarator,
8400 declares_class_or_enum);
8401 if (declarator != error_mark_node)
8403 decl = grokdeclarator (declarator, decl_specifiers,
8404 NORMAL, 0, NULL);
8405 /* Turn access control back on for names used during
8406 template instantiation. */
8407 pop_deferring_access_checks ();
8408 /* Do the explicit instantiation. */
8409 do_decl_instantiation (decl, extension_specifier);
8411 else
8413 pop_deferring_access_checks ();
8414 /* Skip the body of the explicit instantiation. */
8415 cp_parser_skip_to_end_of_statement (parser);
8418 /* We're done with the instantiation. */
8419 end_explicit_instantiation ();
8421 cp_parser_consume_semicolon_at_end_of_statement (parser);
8424 /* Parse an explicit-specialization.
8426 explicit-specialization:
8427 template < > declaration
8429 Although the standard says `declaration', what it really means is:
8431 explicit-specialization:
8432 template <> decl-specifier [opt] init-declarator [opt] ;
8433 template <> function-definition
8434 template <> explicit-specialization
8435 template <> template-declaration */
8437 static void
8438 cp_parser_explicit_specialization (cp_parser* parser)
8440 /* Look for the `template' keyword. */
8441 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8442 /* Look for the `<'. */
8443 cp_parser_require (parser, CPP_LESS, "`<'");
8444 /* Look for the `>'. */
8445 cp_parser_require (parser, CPP_GREATER, "`>'");
8446 /* We have processed another parameter list. */
8447 ++parser->num_template_parameter_lists;
8448 /* Let the front end know that we are beginning a specialization. */
8449 begin_specialization ();
8451 /* If the next keyword is `template', we need to figure out whether
8452 or not we're looking a template-declaration. */
8453 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8455 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8456 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8457 cp_parser_template_declaration_after_export (parser,
8458 /*member_p=*/false);
8459 else
8460 cp_parser_explicit_specialization (parser);
8462 else
8463 /* Parse the dependent declaration. */
8464 cp_parser_single_declaration (parser,
8465 /*member_p=*/false,
8466 /*friend_p=*/NULL);
8468 /* We're done with the specialization. */
8469 end_specialization ();
8470 /* We're done with this parameter list. */
8471 --parser->num_template_parameter_lists;
8474 /* Parse a type-specifier.
8476 type-specifier:
8477 simple-type-specifier
8478 class-specifier
8479 enum-specifier
8480 elaborated-type-specifier
8481 cv-qualifier
8483 GNU Extension:
8485 type-specifier:
8486 __complex__
8488 Returns a representation of the type-specifier. If the
8489 type-specifier is a keyword (like `int' or `const', or
8490 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8491 For a class-specifier, enum-specifier, or elaborated-type-specifier
8492 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8494 If IS_FRIEND is TRUE then this type-specifier is being declared a
8495 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8496 appearing in a decl-specifier-seq.
8498 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8499 class-specifier, enum-specifier, or elaborated-type-specifier, then
8500 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8501 if a type is declared; 2 if it is defined. Otherwise, it is set to
8502 zero.
8504 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8505 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8506 is set to FALSE. */
8508 static tree
8509 cp_parser_type_specifier (cp_parser* parser,
8510 cp_parser_flags flags,
8511 bool is_friend,
8512 bool is_declaration,
8513 int* declares_class_or_enum,
8514 bool* is_cv_qualifier)
8516 tree type_spec = NULL_TREE;
8517 cp_token *token;
8518 enum rid keyword;
8520 /* Assume this type-specifier does not declare a new type. */
8521 if (declares_class_or_enum)
8522 *declares_class_or_enum = 0;
8523 /* And that it does not specify a cv-qualifier. */
8524 if (is_cv_qualifier)
8525 *is_cv_qualifier = false;
8526 /* Peek at the next token. */
8527 token = cp_lexer_peek_token (parser->lexer);
8529 /* If we're looking at a keyword, we can use that to guide the
8530 production we choose. */
8531 keyword = token->keyword;
8532 switch (keyword)
8534 /* Any of these indicate either a class-specifier, or an
8535 elaborated-type-specifier. */
8536 case RID_CLASS:
8537 case RID_STRUCT:
8538 case RID_UNION:
8539 case RID_ENUM:
8540 /* Parse tentatively so that we can back up if we don't find a
8541 class-specifier or enum-specifier. */
8542 cp_parser_parse_tentatively (parser);
8543 /* Look for the class-specifier or enum-specifier. */
8544 if (keyword == RID_ENUM)
8545 type_spec = cp_parser_enum_specifier (parser);
8546 else
8547 type_spec = cp_parser_class_specifier (parser);
8549 /* If that worked, we're done. */
8550 if (cp_parser_parse_definitely (parser))
8552 if (declares_class_or_enum)
8553 *declares_class_or_enum = 2;
8554 return type_spec;
8557 /* Fall through. */
8559 case RID_TYPENAME:
8560 /* Look for an elaborated-type-specifier. */
8561 type_spec = cp_parser_elaborated_type_specifier (parser,
8562 is_friend,
8563 is_declaration);
8564 /* We're declaring a class or enum -- unless we're using
8565 `typename'. */
8566 if (declares_class_or_enum && keyword != RID_TYPENAME)
8567 *declares_class_or_enum = 1;
8568 return type_spec;
8570 case RID_CONST:
8571 case RID_VOLATILE:
8572 case RID_RESTRICT:
8573 type_spec = cp_parser_cv_qualifier_opt (parser);
8574 /* Even though we call a routine that looks for an optional
8575 qualifier, we know that there should be one. */
8576 my_friendly_assert (type_spec != NULL, 20000328);
8577 /* This type-specifier was a cv-qualified. */
8578 if (is_cv_qualifier)
8579 *is_cv_qualifier = true;
8581 return type_spec;
8583 case RID_COMPLEX:
8584 /* The `__complex__' keyword is a GNU extension. */
8585 return cp_lexer_consume_token (parser->lexer)->value;
8587 default:
8588 break;
8591 /* If we do not already have a type-specifier, assume we are looking
8592 at a simple-type-specifier. */
8593 type_spec = cp_parser_simple_type_specifier (parser, flags,
8594 /*identifier_p=*/true);
8596 /* If we didn't find a type-specifier, and a type-specifier was not
8597 optional in this context, issue an error message. */
8598 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8600 cp_parser_error (parser, "expected type specifier");
8601 return error_mark_node;
8604 return type_spec;
8607 /* Parse a simple-type-specifier.
8609 simple-type-specifier:
8610 :: [opt] nested-name-specifier [opt] type-name
8611 :: [opt] nested-name-specifier template template-id
8612 char
8613 wchar_t
8614 bool
8615 short
8617 long
8618 signed
8619 unsigned
8620 float
8621 double
8622 void
8624 GNU Extension:
8626 simple-type-specifier:
8627 __typeof__ unary-expression
8628 __typeof__ ( type-id )
8630 For the various keywords, the value returned is simply the
8631 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8632 For the first two productions, and if IDENTIFIER_P is false, the
8633 value returned is the indicated TYPE_DECL. */
8635 static tree
8636 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8637 bool identifier_p)
8639 tree type = NULL_TREE;
8640 cp_token *token;
8642 /* Peek at the next token. */
8643 token = cp_lexer_peek_token (parser->lexer);
8645 /* If we're looking at a keyword, things are easy. */
8646 switch (token->keyword)
8648 case RID_CHAR:
8649 type = char_type_node;
8650 break;
8651 case RID_WCHAR:
8652 type = wchar_type_node;
8653 break;
8654 case RID_BOOL:
8655 type = boolean_type_node;
8656 break;
8657 case RID_SHORT:
8658 type = short_integer_type_node;
8659 break;
8660 case RID_INT:
8661 type = integer_type_node;
8662 break;
8663 case RID_LONG:
8664 type = long_integer_type_node;
8665 break;
8666 case RID_SIGNED:
8667 type = integer_type_node;
8668 break;
8669 case RID_UNSIGNED:
8670 type = unsigned_type_node;
8671 break;
8672 case RID_FLOAT:
8673 type = float_type_node;
8674 break;
8675 case RID_DOUBLE:
8676 type = double_type_node;
8677 break;
8678 case RID_VOID:
8679 type = void_type_node;
8680 break;
8682 case RID_TYPEOF:
8684 tree operand;
8686 /* Consume the `typeof' token. */
8687 cp_lexer_consume_token (parser->lexer);
8688 /* Parse the operand to `typeof'. */
8689 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8690 /* If it is not already a TYPE, take its type. */
8691 if (!TYPE_P (operand))
8692 operand = finish_typeof (operand);
8694 return operand;
8697 default:
8698 break;
8701 /* If the type-specifier was for a built-in type, we're done. */
8702 if (type)
8704 tree id;
8706 /* Consume the token. */
8707 id = cp_lexer_consume_token (parser->lexer)->value;
8708 return identifier_p ? id : TYPE_NAME (type);
8711 /* The type-specifier must be a user-defined type. */
8712 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8714 /* Don't gobble tokens or issue error messages if this is an
8715 optional type-specifier. */
8716 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8717 cp_parser_parse_tentatively (parser);
8719 /* Look for the optional `::' operator. */
8720 cp_parser_global_scope_opt (parser,
8721 /*current_scope_valid_p=*/false);
8722 /* Look for the nested-name specifier. */
8723 cp_parser_nested_name_specifier_opt (parser,
8724 /*typename_keyword_p=*/false,
8725 /*check_dependency_p=*/true,
8726 /*type_p=*/false,
8727 /*is_declaration=*/false);
8728 /* If we have seen a nested-name-specifier, and the next token
8729 is `template', then we are using the template-id production. */
8730 if (parser->scope
8731 && cp_parser_optional_template_keyword (parser))
8733 /* Look for the template-id. */
8734 type = cp_parser_template_id (parser,
8735 /*template_keyword_p=*/true,
8736 /*check_dependency_p=*/true,
8737 /*is_declaration=*/false);
8738 /* If the template-id did not name a type, we are out of
8739 luck. */
8740 if (TREE_CODE (type) != TYPE_DECL)
8742 cp_parser_error (parser, "expected template-id for type");
8743 type = NULL_TREE;
8746 /* Otherwise, look for a type-name. */
8747 else
8748 type = cp_parser_type_name (parser);
8749 /* If it didn't work out, we don't have a TYPE. */
8750 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8751 && !cp_parser_parse_definitely (parser))
8752 type = NULL_TREE;
8755 /* If we didn't get a type-name, issue an error message. */
8756 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8758 cp_parser_error (parser, "expected type-name");
8759 return error_mark_node;
8762 /* There is no valid C++ program where a non-template type is
8763 followed by a "<". That usually indicates that the user thought
8764 that the type was a template. */
8765 if (type && type != error_mark_node)
8766 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8768 return type;
8771 /* Parse a type-name.
8773 type-name:
8774 class-name
8775 enum-name
8776 typedef-name
8778 enum-name:
8779 identifier
8781 typedef-name:
8782 identifier
8784 Returns a TYPE_DECL for the the type. */
8786 static tree
8787 cp_parser_type_name (cp_parser* parser)
8789 tree type_decl;
8790 tree identifier;
8792 /* We can't know yet whether it is a class-name or not. */
8793 cp_parser_parse_tentatively (parser);
8794 /* Try a class-name. */
8795 type_decl = cp_parser_class_name (parser,
8796 /*typename_keyword_p=*/false,
8797 /*template_keyword_p=*/false,
8798 /*type_p=*/false,
8799 /*check_dependency_p=*/true,
8800 /*class_head_p=*/false,
8801 /*is_declaration=*/false);
8802 /* If it's not a class-name, keep looking. */
8803 if (!cp_parser_parse_definitely (parser))
8805 /* It must be a typedef-name or an enum-name. */
8806 identifier = cp_parser_identifier (parser);
8807 if (identifier == error_mark_node)
8808 return error_mark_node;
8810 /* Look up the type-name. */
8811 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8812 /* Issue an error if we did not find a type-name. */
8813 if (TREE_CODE (type_decl) != TYPE_DECL)
8815 if (!cp_parser_simulate_error (parser))
8816 cp_parser_name_lookup_error (parser, identifier, type_decl,
8817 "is not a type");
8818 type_decl = error_mark_node;
8820 /* Remember that the name was used in the definition of the
8821 current class so that we can check later to see if the
8822 meaning would have been different after the class was
8823 entirely defined. */
8824 else if (type_decl != error_mark_node
8825 && !parser->scope)
8826 maybe_note_name_used_in_class (identifier, type_decl);
8829 return type_decl;
8833 /* Parse an elaborated-type-specifier. Note that the grammar given
8834 here incorporates the resolution to DR68.
8836 elaborated-type-specifier:
8837 class-key :: [opt] nested-name-specifier [opt] identifier
8838 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8839 enum :: [opt] nested-name-specifier [opt] identifier
8840 typename :: [opt] nested-name-specifier identifier
8841 typename :: [opt] nested-name-specifier template [opt]
8842 template-id
8844 GNU extension:
8846 elaborated-type-specifier:
8847 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8848 class-key attributes :: [opt] nested-name-specifier [opt]
8849 template [opt] template-id
8850 enum attributes :: [opt] nested-name-specifier [opt] identifier
8852 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8853 declared `friend'. If IS_DECLARATION is TRUE, then this
8854 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8855 something is being declared.
8857 Returns the TYPE specified. */
8859 static tree
8860 cp_parser_elaborated_type_specifier (cp_parser* parser,
8861 bool is_friend,
8862 bool is_declaration)
8864 enum tag_types tag_type;
8865 tree identifier;
8866 tree type = NULL_TREE;
8867 tree attributes = NULL_TREE;
8869 /* See if we're looking at the `enum' keyword. */
8870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8872 /* Consume the `enum' token. */
8873 cp_lexer_consume_token (parser->lexer);
8874 /* Remember that it's an enumeration type. */
8875 tag_type = enum_type;
8876 /* Parse the attributes. */
8877 attributes = cp_parser_attributes_opt (parser);
8879 /* Or, it might be `typename'. */
8880 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8881 RID_TYPENAME))
8883 /* Consume the `typename' token. */
8884 cp_lexer_consume_token (parser->lexer);
8885 /* Remember that it's a `typename' type. */
8886 tag_type = typename_type;
8887 /* The `typename' keyword is only allowed in templates. */
8888 if (!processing_template_decl)
8889 pedwarn ("using `typename' outside of template");
8891 /* Otherwise it must be a class-key. */
8892 else
8894 tag_type = cp_parser_class_key (parser);
8895 if (tag_type == none_type)
8896 return error_mark_node;
8897 /* Parse the attributes. */
8898 attributes = cp_parser_attributes_opt (parser);
8901 /* Look for the `::' operator. */
8902 cp_parser_global_scope_opt (parser,
8903 /*current_scope_valid_p=*/false);
8904 /* Look for the nested-name-specifier. */
8905 if (tag_type == typename_type)
8907 if (cp_parser_nested_name_specifier (parser,
8908 /*typename_keyword_p=*/true,
8909 /*check_dependency_p=*/true,
8910 /*type_p=*/true,
8911 is_declaration)
8912 == error_mark_node)
8913 return error_mark_node;
8915 else
8916 /* Even though `typename' is not present, the proposed resolution
8917 to Core Issue 180 says that in `class A<T>::B', `B' should be
8918 considered a type-name, even if `A<T>' is dependent. */
8919 cp_parser_nested_name_specifier_opt (parser,
8920 /*typename_keyword_p=*/true,
8921 /*check_dependency_p=*/true,
8922 /*type_p=*/true,
8923 is_declaration);
8924 /* For everything but enumeration types, consider a template-id. */
8925 if (tag_type != enum_type)
8927 bool template_p = false;
8928 tree decl;
8930 /* Allow the `template' keyword. */
8931 template_p = cp_parser_optional_template_keyword (parser);
8932 /* If we didn't see `template', we don't know if there's a
8933 template-id or not. */
8934 if (!template_p)
8935 cp_parser_parse_tentatively (parser);
8936 /* Parse the template-id. */
8937 decl = cp_parser_template_id (parser, template_p,
8938 /*check_dependency_p=*/true,
8939 is_declaration);
8940 /* If we didn't find a template-id, look for an ordinary
8941 identifier. */
8942 if (!template_p && !cp_parser_parse_definitely (parser))
8944 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8945 in effect, then we must assume that, upon instantiation, the
8946 template will correspond to a class. */
8947 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8948 && tag_type == typename_type)
8949 type = make_typename_type (parser->scope, decl,
8950 /*complain=*/1);
8951 else
8952 type = TREE_TYPE (decl);
8955 /* For an enumeration type, consider only a plain identifier. */
8956 if (!type)
8958 identifier = cp_parser_identifier (parser);
8960 if (identifier == error_mark_node)
8962 parser->scope = NULL_TREE;
8963 return error_mark_node;
8966 /* For a `typename', we needn't call xref_tag. */
8967 if (tag_type == typename_type)
8968 return make_typename_type (parser->scope, identifier,
8969 /*complain=*/1);
8970 /* Look up a qualified name in the usual way. */
8971 if (parser->scope)
8973 tree decl;
8975 /* In an elaborated-type-specifier, names are assumed to name
8976 types, so we set IS_TYPE to TRUE when calling
8977 cp_parser_lookup_name. */
8978 decl = cp_parser_lookup_name (parser, identifier,
8979 /*is_type=*/true,
8980 /*is_template=*/false,
8981 /*is_namespace=*/false,
8982 /*check_dependency=*/true);
8984 /* If we are parsing friend declaration, DECL may be a
8985 TEMPLATE_DECL tree node here. However, we need to check
8986 whether this TEMPLATE_DECL results in valid code. Consider
8987 the following example:
8989 namespace N {
8990 template <class T> class C {};
8992 class X {
8993 template <class T> friend class N::C; // #1, valid code
8995 template <class T> class Y {
8996 friend class N::C; // #2, invalid code
8999 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9000 name lookup of `N::C'. We see that friend declaration must
9001 be template for the code to be valid. Note that
9002 processing_template_decl does not work here since it is
9003 always 1 for the above two cases. */
9005 decl = (cp_parser_maybe_treat_template_as_class
9006 (decl, /*tag_name_p=*/is_friend
9007 && parser->num_template_parameter_lists));
9009 if (TREE_CODE (decl) != TYPE_DECL)
9011 error ("expected type-name");
9012 return error_mark_node;
9015 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9016 check_elaborated_type_specifier
9017 (tag_type, decl,
9018 (parser->num_template_parameter_lists
9019 || DECL_SELF_REFERENCE_P (decl)));
9021 type = TREE_TYPE (decl);
9023 else
9025 /* An elaborated-type-specifier sometimes introduces a new type and
9026 sometimes names an existing type. Normally, the rule is that it
9027 introduces a new type only if there is not an existing type of
9028 the same name already in scope. For example, given:
9030 struct S {};
9031 void f() { struct S s; }
9033 the `struct S' in the body of `f' is the same `struct S' as in
9034 the global scope; the existing definition is used. However, if
9035 there were no global declaration, this would introduce a new
9036 local class named `S'.
9038 An exception to this rule applies to the following code:
9040 namespace N { struct S; }
9042 Here, the elaborated-type-specifier names a new type
9043 unconditionally; even if there is already an `S' in the
9044 containing scope this declaration names a new type.
9045 This exception only applies if the elaborated-type-specifier
9046 forms the complete declaration:
9048 [class.name]
9050 A declaration consisting solely of `class-key identifier ;' is
9051 either a redeclaration of the name in the current scope or a
9052 forward declaration of the identifier as a class name. It
9053 introduces the name into the current scope.
9055 We are in this situation precisely when the next token is a `;'.
9057 An exception to the exception is that a `friend' declaration does
9058 *not* name a new type; i.e., given:
9060 struct S { friend struct T; };
9062 `T' is not a new type in the scope of `S'.
9064 Also, `new struct S' or `sizeof (struct S)' never results in the
9065 definition of a new type; a new type can only be declared in a
9066 declaration context. */
9068 type = xref_tag (tag_type, identifier,
9069 attributes,
9070 (is_friend
9071 || !is_declaration
9072 || cp_lexer_next_token_is_not (parser->lexer,
9073 CPP_SEMICOLON)),
9074 parser->num_template_parameter_lists);
9077 if (tag_type != enum_type)
9078 cp_parser_check_class_key (tag_type, type);
9080 /* A "<" cannot follow an elaborated type specifier. If that
9081 happens, the user was probably trying to form a template-id. */
9082 cp_parser_check_for_invalid_template_id (parser, type);
9084 return type;
9087 /* Parse an enum-specifier.
9089 enum-specifier:
9090 enum identifier [opt] { enumerator-list [opt] }
9092 Returns an ENUM_TYPE representing the enumeration. */
9094 static tree
9095 cp_parser_enum_specifier (cp_parser* parser)
9097 cp_token *token;
9098 tree identifier = NULL_TREE;
9099 tree type;
9101 /* Look for the `enum' keyword. */
9102 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9103 return error_mark_node;
9104 /* Peek at the next token. */
9105 token = cp_lexer_peek_token (parser->lexer);
9107 /* See if it is an identifier. */
9108 if (token->type == CPP_NAME)
9109 identifier = cp_parser_identifier (parser);
9111 /* Look for the `{'. */
9112 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9113 return error_mark_node;
9115 /* At this point, we're going ahead with the enum-specifier, even
9116 if some other problem occurs. */
9117 cp_parser_commit_to_tentative_parse (parser);
9119 /* Issue an error message if type-definitions are forbidden here. */
9120 cp_parser_check_type_definition (parser);
9122 /* Create the new type. */
9123 type = start_enum (identifier ? identifier : make_anon_name ());
9125 /* Peek at the next token. */
9126 token = cp_lexer_peek_token (parser->lexer);
9127 /* If it's not a `}', then there are some enumerators. */
9128 if (token->type != CPP_CLOSE_BRACE)
9129 cp_parser_enumerator_list (parser, type);
9130 /* Look for the `}'. */
9131 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9133 /* Finish up the enumeration. */
9134 finish_enum (type);
9136 return type;
9139 /* Parse an enumerator-list. The enumerators all have the indicated
9140 TYPE.
9142 enumerator-list:
9143 enumerator-definition
9144 enumerator-list , enumerator-definition */
9146 static void
9147 cp_parser_enumerator_list (cp_parser* parser, tree type)
9149 while (true)
9151 cp_token *token;
9153 /* Parse an enumerator-definition. */
9154 cp_parser_enumerator_definition (parser, type);
9155 /* Peek at the next token. */
9156 token = cp_lexer_peek_token (parser->lexer);
9157 /* If it's not a `,', then we've reached the end of the
9158 list. */
9159 if (token->type != CPP_COMMA)
9160 break;
9161 /* Otherwise, consume the `,' and keep going. */
9162 cp_lexer_consume_token (parser->lexer);
9163 /* If the next token is a `}', there is a trailing comma. */
9164 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9166 if (pedantic && !in_system_header)
9167 pedwarn ("comma at end of enumerator list");
9168 break;
9173 /* Parse an enumerator-definition. The enumerator has the indicated
9174 TYPE.
9176 enumerator-definition:
9177 enumerator
9178 enumerator = constant-expression
9180 enumerator:
9181 identifier */
9183 static void
9184 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9186 cp_token *token;
9187 tree identifier;
9188 tree value;
9190 /* Look for the identifier. */
9191 identifier = cp_parser_identifier (parser);
9192 if (identifier == error_mark_node)
9193 return;
9195 /* Peek at the next token. */
9196 token = cp_lexer_peek_token (parser->lexer);
9197 /* If it's an `=', then there's an explicit value. */
9198 if (token->type == CPP_EQ)
9200 /* Consume the `=' token. */
9201 cp_lexer_consume_token (parser->lexer);
9202 /* Parse the value. */
9203 value = cp_parser_constant_expression (parser,
9204 /*allow_non_constant_p=*/false,
9205 NULL);
9207 else
9208 value = NULL_TREE;
9210 /* Create the enumerator. */
9211 build_enumerator (identifier, value, type);
9214 /* Parse a namespace-name.
9216 namespace-name:
9217 original-namespace-name
9218 namespace-alias
9220 Returns the NAMESPACE_DECL for the namespace. */
9222 static tree
9223 cp_parser_namespace_name (cp_parser* parser)
9225 tree identifier;
9226 tree namespace_decl;
9228 /* Get the name of the namespace. */
9229 identifier = cp_parser_identifier (parser);
9230 if (identifier == error_mark_node)
9231 return error_mark_node;
9233 /* Look up the identifier in the currently active scope. Look only
9234 for namespaces, due to:
9236 [basic.lookup.udir]
9238 When looking up a namespace-name in a using-directive or alias
9239 definition, only namespace names are considered.
9241 And:
9243 [basic.lookup.qual]
9245 During the lookup of a name preceding the :: scope resolution
9246 operator, object, function, and enumerator names are ignored.
9248 (Note that cp_parser_class_or_namespace_name only calls this
9249 function if the token after the name is the scope resolution
9250 operator.) */
9251 namespace_decl = cp_parser_lookup_name (parser, identifier,
9252 /*is_type=*/false,
9253 /*is_template=*/false,
9254 /*is_namespace=*/true,
9255 /*check_dependency=*/true);
9256 /* If it's not a namespace, issue an error. */
9257 if (namespace_decl == error_mark_node
9258 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9260 cp_parser_error (parser, "expected namespace-name");
9261 namespace_decl = error_mark_node;
9264 return namespace_decl;
9267 /* Parse a namespace-definition.
9269 namespace-definition:
9270 named-namespace-definition
9271 unnamed-namespace-definition
9273 named-namespace-definition:
9274 original-namespace-definition
9275 extension-namespace-definition
9277 original-namespace-definition:
9278 namespace identifier { namespace-body }
9280 extension-namespace-definition:
9281 namespace original-namespace-name { namespace-body }
9283 unnamed-namespace-definition:
9284 namespace { namespace-body } */
9286 static void
9287 cp_parser_namespace_definition (cp_parser* parser)
9289 tree identifier;
9291 /* Look for the `namespace' keyword. */
9292 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9294 /* Get the name of the namespace. We do not attempt to distinguish
9295 between an original-namespace-definition and an
9296 extension-namespace-definition at this point. The semantic
9297 analysis routines are responsible for that. */
9298 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9299 identifier = cp_parser_identifier (parser);
9300 else
9301 identifier = NULL_TREE;
9303 /* Look for the `{' to start the namespace. */
9304 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9305 /* Start the namespace. */
9306 push_namespace (identifier);
9307 /* Parse the body of the namespace. */
9308 cp_parser_namespace_body (parser);
9309 /* Finish the namespace. */
9310 pop_namespace ();
9311 /* Look for the final `}'. */
9312 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9315 /* Parse a namespace-body.
9317 namespace-body:
9318 declaration-seq [opt] */
9320 static void
9321 cp_parser_namespace_body (cp_parser* parser)
9323 cp_parser_declaration_seq_opt (parser);
9326 /* Parse a namespace-alias-definition.
9328 namespace-alias-definition:
9329 namespace identifier = qualified-namespace-specifier ; */
9331 static void
9332 cp_parser_namespace_alias_definition (cp_parser* parser)
9334 tree identifier;
9335 tree namespace_specifier;
9337 /* Look for the `namespace' keyword. */
9338 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9339 /* Look for the identifier. */
9340 identifier = cp_parser_identifier (parser);
9341 if (identifier == error_mark_node)
9342 return;
9343 /* Look for the `=' token. */
9344 cp_parser_require (parser, CPP_EQ, "`='");
9345 /* Look for the qualified-namespace-specifier. */
9346 namespace_specifier
9347 = cp_parser_qualified_namespace_specifier (parser);
9348 /* Look for the `;' token. */
9349 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9351 /* Register the alias in the symbol table. */
9352 do_namespace_alias (identifier, namespace_specifier);
9355 /* Parse a qualified-namespace-specifier.
9357 qualified-namespace-specifier:
9358 :: [opt] nested-name-specifier [opt] namespace-name
9360 Returns a NAMESPACE_DECL corresponding to the specified
9361 namespace. */
9363 static tree
9364 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9366 /* Look for the optional `::'. */
9367 cp_parser_global_scope_opt (parser,
9368 /*current_scope_valid_p=*/false);
9370 /* Look for the optional nested-name-specifier. */
9371 cp_parser_nested_name_specifier_opt (parser,
9372 /*typename_keyword_p=*/false,
9373 /*check_dependency_p=*/true,
9374 /*type_p=*/false,
9375 /*is_declaration=*/true);
9377 return cp_parser_namespace_name (parser);
9380 /* Parse a using-declaration.
9382 using-declaration:
9383 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9384 using :: unqualified-id ; */
9386 static void
9387 cp_parser_using_declaration (cp_parser* parser)
9389 cp_token *token;
9390 bool typename_p = false;
9391 bool global_scope_p;
9392 tree decl;
9393 tree identifier;
9394 tree scope;
9396 /* Look for the `using' keyword. */
9397 cp_parser_require_keyword (parser, RID_USING, "`using'");
9399 /* Peek at the next token. */
9400 token = cp_lexer_peek_token (parser->lexer);
9401 /* See if it's `typename'. */
9402 if (token->keyword == RID_TYPENAME)
9404 /* Remember that we've seen it. */
9405 typename_p = true;
9406 /* Consume the `typename' token. */
9407 cp_lexer_consume_token (parser->lexer);
9410 /* Look for the optional global scope qualification. */
9411 global_scope_p
9412 = (cp_parser_global_scope_opt (parser,
9413 /*current_scope_valid_p=*/false)
9414 != NULL_TREE);
9416 /* If we saw `typename', or didn't see `::', then there must be a
9417 nested-name-specifier present. */
9418 if (typename_p || !global_scope_p)
9419 cp_parser_nested_name_specifier (parser, typename_p,
9420 /*check_dependency_p=*/true,
9421 /*type_p=*/false,
9422 /*is_declaration=*/true);
9423 /* Otherwise, we could be in either of the two productions. In that
9424 case, treat the nested-name-specifier as optional. */
9425 else
9426 cp_parser_nested_name_specifier_opt (parser,
9427 /*typename_keyword_p=*/false,
9428 /*check_dependency_p=*/true,
9429 /*type_p=*/false,
9430 /*is_declaration=*/true);
9432 /* Parse the unqualified-id. */
9433 identifier = cp_parser_unqualified_id (parser,
9434 /*template_keyword_p=*/false,
9435 /*check_dependency_p=*/true,
9436 /*declarator_p=*/true);
9438 /* The function we call to handle a using-declaration is different
9439 depending on what scope we are in. */
9440 if (identifier == error_mark_node)
9442 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9443 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9444 /* [namespace.udecl]
9446 A using declaration shall not name a template-id. */
9447 error ("a template-id may not appear in a using-declaration");
9448 else
9450 scope = current_scope ();
9451 if (scope && TYPE_P (scope))
9453 /* Create the USING_DECL. */
9454 decl = do_class_using_decl (build_nt (SCOPE_REF,
9455 parser->scope,
9456 identifier));
9457 /* Add it to the list of members in this class. */
9458 finish_member_declaration (decl);
9460 else
9462 decl = cp_parser_lookup_name_simple (parser, identifier);
9463 if (decl == error_mark_node)
9464 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9465 else if (scope)
9466 do_local_using_decl (decl);
9467 else
9468 do_toplevel_using_decl (decl);
9472 /* Look for the final `;'. */
9473 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9476 /* Parse a using-directive.
9478 using-directive:
9479 using namespace :: [opt] nested-name-specifier [opt]
9480 namespace-name ; */
9482 static void
9483 cp_parser_using_directive (cp_parser* parser)
9485 tree namespace_decl;
9486 tree attribs;
9488 /* Look for the `using' keyword. */
9489 cp_parser_require_keyword (parser, RID_USING, "`using'");
9490 /* And the `namespace' keyword. */
9491 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9492 /* Look for the optional `::' operator. */
9493 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9494 /* And the optional nested-name-specifier. */
9495 cp_parser_nested_name_specifier_opt (parser,
9496 /*typename_keyword_p=*/false,
9497 /*check_dependency_p=*/true,
9498 /*type_p=*/false,
9499 /*is_declaration=*/true);
9500 /* Get the namespace being used. */
9501 namespace_decl = cp_parser_namespace_name (parser);
9502 /* And any specified attributes. */
9503 attribs = cp_parser_attributes_opt (parser);
9504 /* Update the symbol table. */
9505 parse_using_directive (namespace_decl, attribs);
9506 /* Look for the final `;'. */
9507 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9510 /* Parse an asm-definition.
9512 asm-definition:
9513 asm ( string-literal ) ;
9515 GNU Extension:
9517 asm-definition:
9518 asm volatile [opt] ( string-literal ) ;
9519 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9520 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9521 : asm-operand-list [opt] ) ;
9522 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9523 : asm-operand-list [opt]
9524 : asm-operand-list [opt] ) ; */
9526 static void
9527 cp_parser_asm_definition (cp_parser* parser)
9529 cp_token *token;
9530 tree string;
9531 tree outputs = NULL_TREE;
9532 tree inputs = NULL_TREE;
9533 tree clobbers = NULL_TREE;
9534 tree asm_stmt;
9535 bool volatile_p = false;
9536 bool extended_p = false;
9538 /* Look for the `asm' keyword. */
9539 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9540 /* See if the next token is `volatile'. */
9541 if (cp_parser_allow_gnu_extensions_p (parser)
9542 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9544 /* Remember that we saw the `volatile' keyword. */
9545 volatile_p = true;
9546 /* Consume the token. */
9547 cp_lexer_consume_token (parser->lexer);
9549 /* Look for the opening `('. */
9550 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9551 /* Look for the string. */
9552 token = cp_parser_require (parser, CPP_STRING, "asm body");
9553 if (!token)
9554 return;
9555 string = token->value;
9556 /* If we're allowing GNU extensions, check for the extended assembly
9557 syntax. Unfortunately, the `:' tokens need not be separated by
9558 a space in C, and so, for compatibility, we tolerate that here
9559 too. Doing that means that we have to treat the `::' operator as
9560 two `:' tokens. */
9561 if (cp_parser_allow_gnu_extensions_p (parser)
9562 && at_function_scope_p ()
9563 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9564 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9566 bool inputs_p = false;
9567 bool clobbers_p = false;
9569 /* The extended syntax was used. */
9570 extended_p = true;
9572 /* Look for outputs. */
9573 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9575 /* Consume the `:'. */
9576 cp_lexer_consume_token (parser->lexer);
9577 /* Parse the output-operands. */
9578 if (cp_lexer_next_token_is_not (parser->lexer,
9579 CPP_COLON)
9580 && cp_lexer_next_token_is_not (parser->lexer,
9581 CPP_SCOPE)
9582 && cp_lexer_next_token_is_not (parser->lexer,
9583 CPP_CLOSE_PAREN))
9584 outputs = cp_parser_asm_operand_list (parser);
9586 /* If the next token is `::', there are no outputs, and the
9587 next token is the beginning of the inputs. */
9588 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9590 /* Consume the `::' token. */
9591 cp_lexer_consume_token (parser->lexer);
9592 /* The inputs are coming next. */
9593 inputs_p = true;
9596 /* Look for inputs. */
9597 if (inputs_p
9598 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9600 if (!inputs_p)
9601 /* Consume the `:'. */
9602 cp_lexer_consume_token (parser->lexer);
9603 /* Parse the output-operands. */
9604 if (cp_lexer_next_token_is_not (parser->lexer,
9605 CPP_COLON)
9606 && cp_lexer_next_token_is_not (parser->lexer,
9607 CPP_SCOPE)
9608 && cp_lexer_next_token_is_not (parser->lexer,
9609 CPP_CLOSE_PAREN))
9610 inputs = cp_parser_asm_operand_list (parser);
9612 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9613 /* The clobbers are coming next. */
9614 clobbers_p = true;
9616 /* Look for clobbers. */
9617 if (clobbers_p
9618 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9620 if (!clobbers_p)
9621 /* Consume the `:'. */
9622 cp_lexer_consume_token (parser->lexer);
9623 /* Parse the clobbers. */
9624 if (cp_lexer_next_token_is_not (parser->lexer,
9625 CPP_CLOSE_PAREN))
9626 clobbers = cp_parser_asm_clobber_list (parser);
9629 /* Look for the closing `)'. */
9630 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9631 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9632 /*consume_paren=*/true);
9633 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9635 /* Create the ASM_STMT. */
9636 if (at_function_scope_p ())
9638 asm_stmt =
9639 finish_asm_stmt (volatile_p
9640 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9641 string, outputs, inputs, clobbers);
9642 /* If the extended syntax was not used, mark the ASM_STMT. */
9643 if (!extended_p)
9644 ASM_INPUT_P (asm_stmt) = 1;
9646 else
9647 assemble_asm (string);
9650 /* Declarators [gram.dcl.decl] */
9652 /* Parse an init-declarator.
9654 init-declarator:
9655 declarator initializer [opt]
9657 GNU Extension:
9659 init-declarator:
9660 declarator asm-specification [opt] attributes [opt] initializer [opt]
9662 function-definition:
9663 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9664 function-body
9665 decl-specifier-seq [opt] declarator function-try-block
9667 GNU Extension:
9669 function-definition:
9670 __extension__ function-definition
9672 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9673 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9674 then this declarator appears in a class scope. The new DECL created
9675 by this declarator is returned.
9677 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9678 for a function-definition here as well. If the declarator is a
9679 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9680 be TRUE upon return. By that point, the function-definition will
9681 have been completely parsed.
9683 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9684 is FALSE. */
9686 static tree
9687 cp_parser_init_declarator (cp_parser* parser,
9688 tree decl_specifiers,
9689 tree prefix_attributes,
9690 bool function_definition_allowed_p,
9691 bool member_p,
9692 int declares_class_or_enum,
9693 bool* function_definition_p)
9695 cp_token *token;
9696 tree declarator;
9697 tree attributes;
9698 tree asm_specification;
9699 tree initializer;
9700 tree decl = NULL_TREE;
9701 tree scope;
9702 bool is_initialized;
9703 bool is_parenthesized_init;
9704 bool is_non_constant_init;
9705 int ctor_dtor_or_conv_p;
9706 bool friend_p;
9708 /* Assume that this is not the declarator for a function
9709 definition. */
9710 if (function_definition_p)
9711 *function_definition_p = false;
9713 /* Defer access checks while parsing the declarator; we cannot know
9714 what names are accessible until we know what is being
9715 declared. */
9716 resume_deferring_access_checks ();
9718 /* Parse the declarator. */
9719 declarator
9720 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9721 &ctor_dtor_or_conv_p,
9722 /*parenthesized_p=*/NULL);
9723 /* Gather up the deferred checks. */
9724 stop_deferring_access_checks ();
9726 /* If the DECLARATOR was erroneous, there's no need to go
9727 further. */
9728 if (declarator == error_mark_node)
9729 return error_mark_node;
9731 cp_parser_check_for_definition_in_return_type (declarator,
9732 declares_class_or_enum);
9734 /* Figure out what scope the entity declared by the DECLARATOR is
9735 located in. `grokdeclarator' sometimes changes the scope, so
9736 we compute it now. */
9737 scope = get_scope_of_declarator (declarator);
9739 /* If we're allowing GNU extensions, look for an asm-specification
9740 and attributes. */
9741 if (cp_parser_allow_gnu_extensions_p (parser))
9743 /* Look for an asm-specification. */
9744 asm_specification = cp_parser_asm_specification_opt (parser);
9745 /* And attributes. */
9746 attributes = cp_parser_attributes_opt (parser);
9748 else
9750 asm_specification = NULL_TREE;
9751 attributes = NULL_TREE;
9754 /* Peek at the next token. */
9755 token = cp_lexer_peek_token (parser->lexer);
9756 /* Check to see if the token indicates the start of a
9757 function-definition. */
9758 if (cp_parser_token_starts_function_definition_p (token))
9760 if (!function_definition_allowed_p)
9762 /* If a function-definition should not appear here, issue an
9763 error message. */
9764 cp_parser_error (parser,
9765 "a function-definition is not allowed here");
9766 return error_mark_node;
9768 else
9770 /* Neither attributes nor an asm-specification are allowed
9771 on a function-definition. */
9772 if (asm_specification)
9773 error ("an asm-specification is not allowed on a function-definition");
9774 if (attributes)
9775 error ("attributes are not allowed on a function-definition");
9776 /* This is a function-definition. */
9777 *function_definition_p = true;
9779 /* Parse the function definition. */
9780 if (member_p)
9781 decl = cp_parser_save_member_function_body (parser,
9782 decl_specifiers,
9783 declarator,
9784 prefix_attributes);
9785 else
9786 decl
9787 = (cp_parser_function_definition_from_specifiers_and_declarator
9788 (parser, decl_specifiers, prefix_attributes, declarator));
9790 return decl;
9794 /* [dcl.dcl]
9796 Only in function declarations for constructors, destructors, and
9797 type conversions can the decl-specifier-seq be omitted.
9799 We explicitly postpone this check past the point where we handle
9800 function-definitions because we tolerate function-definitions
9801 that are missing their return types in some modes. */
9802 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9804 cp_parser_error (parser,
9805 "expected constructor, destructor, or type conversion");
9806 return error_mark_node;
9809 /* An `=' or an `(' indicates an initializer. */
9810 is_initialized = (token->type == CPP_EQ
9811 || token->type == CPP_OPEN_PAREN);
9812 /* If the init-declarator isn't initialized and isn't followed by a
9813 `,' or `;', it's not a valid init-declarator. */
9814 if (!is_initialized
9815 && token->type != CPP_COMMA
9816 && token->type != CPP_SEMICOLON)
9818 cp_parser_error (parser, "expected init-declarator");
9819 return error_mark_node;
9822 /* Because start_decl has side-effects, we should only call it if we
9823 know we're going ahead. By this point, we know that we cannot
9824 possibly be looking at any other construct. */
9825 cp_parser_commit_to_tentative_parse (parser);
9827 /* If the decl specifiers were bad, issue an error now that we're
9828 sure this was intended to be a declarator. Then continue
9829 declaring the variable(s), as int, to try to cut down on further
9830 errors. */
9831 if (decl_specifiers != NULL
9832 && TREE_VALUE (decl_specifiers) == error_mark_node)
9834 cp_parser_error (parser, "invalid type in declaration");
9835 TREE_VALUE (decl_specifiers) = integer_type_node;
9838 /* Check to see whether or not this declaration is a friend. */
9839 friend_p = cp_parser_friend_p (decl_specifiers);
9841 /* Check that the number of template-parameter-lists is OK. */
9842 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9843 return error_mark_node;
9845 /* Enter the newly declared entry in the symbol table. If we're
9846 processing a declaration in a class-specifier, we wait until
9847 after processing the initializer. */
9848 if (!member_p)
9850 if (parser->in_unbraced_linkage_specification_p)
9852 decl_specifiers = tree_cons (error_mark_node,
9853 get_identifier ("extern"),
9854 decl_specifiers);
9855 have_extern_spec = false;
9857 decl = start_decl (declarator, decl_specifiers,
9858 is_initialized, attributes, prefix_attributes);
9861 /* Enter the SCOPE. That way unqualified names appearing in the
9862 initializer will be looked up in SCOPE. */
9863 if (scope)
9864 push_scope (scope);
9866 /* Perform deferred access control checks, now that we know in which
9867 SCOPE the declared entity resides. */
9868 if (!member_p && decl)
9870 tree saved_current_function_decl = NULL_TREE;
9872 /* If the entity being declared is a function, pretend that we
9873 are in its scope. If it is a `friend', it may have access to
9874 things that would not otherwise be accessible. */
9875 if (TREE_CODE (decl) == FUNCTION_DECL)
9877 saved_current_function_decl = current_function_decl;
9878 current_function_decl = decl;
9881 /* Perform the access control checks for the declarator and the
9882 the decl-specifiers. */
9883 perform_deferred_access_checks ();
9885 /* Restore the saved value. */
9886 if (TREE_CODE (decl) == FUNCTION_DECL)
9887 current_function_decl = saved_current_function_decl;
9890 /* Parse the initializer. */
9891 if (is_initialized)
9892 initializer = cp_parser_initializer (parser,
9893 &is_parenthesized_init,
9894 &is_non_constant_init);
9895 else
9897 initializer = NULL_TREE;
9898 is_parenthesized_init = false;
9899 is_non_constant_init = true;
9902 /* The old parser allows attributes to appear after a parenthesized
9903 initializer. Mark Mitchell proposed removing this functionality
9904 on the GCC mailing lists on 2002-08-13. This parser accepts the
9905 attributes -- but ignores them. */
9906 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9907 if (cp_parser_attributes_opt (parser))
9908 warning ("attributes after parenthesized initializer ignored");
9910 /* Leave the SCOPE, now that we have processed the initializer. It
9911 is important to do this before calling cp_finish_decl because it
9912 makes decisions about whether to create DECL_STMTs or not based
9913 on the current scope. */
9914 if (scope)
9915 pop_scope (scope);
9917 /* For an in-class declaration, use `grokfield' to create the
9918 declaration. */
9919 if (member_p)
9921 decl = grokfield (declarator, decl_specifiers,
9922 initializer, /*asmspec=*/NULL_TREE,
9923 /*attributes=*/NULL_TREE);
9924 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9925 cp_parser_save_default_args (parser, decl);
9928 /* Finish processing the declaration. But, skip friend
9929 declarations. */
9930 if (!friend_p && decl)
9931 cp_finish_decl (decl,
9932 initializer,
9933 asm_specification,
9934 /* If the initializer is in parentheses, then this is
9935 a direct-initialization, which means that an
9936 `explicit' constructor is OK. Otherwise, an
9937 `explicit' constructor cannot be used. */
9938 ((is_parenthesized_init || !is_initialized)
9939 ? 0 : LOOKUP_ONLYCONVERTING));
9941 /* Remember whether or not variables were initialized by
9942 constant-expressions. */
9943 if (decl && TREE_CODE (decl) == VAR_DECL
9944 && is_initialized && !is_non_constant_init)
9945 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9947 return decl;
9950 /* Parse a declarator.
9952 declarator:
9953 direct-declarator
9954 ptr-operator declarator
9956 abstract-declarator:
9957 ptr-operator abstract-declarator [opt]
9958 direct-abstract-declarator
9960 GNU Extensions:
9962 declarator:
9963 attributes [opt] direct-declarator
9964 attributes [opt] ptr-operator declarator
9966 abstract-declarator:
9967 attributes [opt] ptr-operator abstract-declarator [opt]
9968 attributes [opt] direct-abstract-declarator
9970 Returns a representation of the declarator. If the declarator has
9971 the form `* declarator', then an INDIRECT_REF is returned, whose
9972 only operand is the sub-declarator. Analogously, `& declarator' is
9973 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9974 used. The first operand is the TYPE for `X'. The second operand
9975 is an INDIRECT_REF whose operand is the sub-declarator.
9977 Otherwise, the representation is as for a direct-declarator.
9979 (It would be better to define a structure type to represent
9980 declarators, rather than abusing `tree' nodes to represent
9981 declarators. That would be much clearer and save some memory.
9982 There is no reason for declarators to be garbage-collected, for
9983 example; they are created during parser and no longer needed after
9984 `grokdeclarator' has been called.)
9986 For a ptr-operator that has the optional cv-qualifier-seq,
9987 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9988 node.
9990 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9991 detect constructor, destructor or conversion operators. It is set
9992 to -1 if the declarator is a name, and +1 if it is a
9993 function. Otherwise it is set to zero. Usually you just want to
9994 test for >0, but internally the negative value is used.
9996 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9997 a decl-specifier-seq unless it declares a constructor, destructor,
9998 or conversion. It might seem that we could check this condition in
9999 semantic analysis, rather than parsing, but that makes it difficult
10000 to handle something like `f()'. We want to notice that there are
10001 no decl-specifiers, and therefore realize that this is an
10002 expression, not a declaration.)
10004 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10005 the declarator is a direct-declarator of the form "(...)". */
10007 static tree
10008 cp_parser_declarator (cp_parser* parser,
10009 cp_parser_declarator_kind dcl_kind,
10010 int* ctor_dtor_or_conv_p,
10011 bool* parenthesized_p)
10013 cp_token *token;
10014 tree declarator;
10015 enum tree_code code;
10016 tree cv_qualifier_seq;
10017 tree class_type;
10018 tree attributes = NULL_TREE;
10020 /* Assume this is not a constructor, destructor, or type-conversion
10021 operator. */
10022 if (ctor_dtor_or_conv_p)
10023 *ctor_dtor_or_conv_p = 0;
10025 if (cp_parser_allow_gnu_extensions_p (parser))
10026 attributes = cp_parser_attributes_opt (parser);
10028 /* Peek at the next token. */
10029 token = cp_lexer_peek_token (parser->lexer);
10031 /* Check for the ptr-operator production. */
10032 cp_parser_parse_tentatively (parser);
10033 /* Parse the ptr-operator. */
10034 code = cp_parser_ptr_operator (parser,
10035 &class_type,
10036 &cv_qualifier_seq);
10037 /* If that worked, then we have a ptr-operator. */
10038 if (cp_parser_parse_definitely (parser))
10040 /* If a ptr-operator was found, then this declarator was not
10041 parenthesized. */
10042 if (parenthesized_p)
10043 *parenthesized_p = true;
10044 /* The dependent declarator is optional if we are parsing an
10045 abstract-declarator. */
10046 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10047 cp_parser_parse_tentatively (parser);
10049 /* Parse the dependent declarator. */
10050 declarator = cp_parser_declarator (parser, dcl_kind,
10051 /*ctor_dtor_or_conv_p=*/NULL,
10052 /*parenthesized_p=*/NULL);
10054 /* If we are parsing an abstract-declarator, we must handle the
10055 case where the dependent declarator is absent. */
10056 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10057 && !cp_parser_parse_definitely (parser))
10058 declarator = NULL_TREE;
10060 /* Build the representation of the ptr-operator. */
10061 if (code == INDIRECT_REF)
10062 declarator = make_pointer_declarator (cv_qualifier_seq,
10063 declarator);
10064 else
10065 declarator = make_reference_declarator (cv_qualifier_seq,
10066 declarator);
10067 /* Handle the pointer-to-member case. */
10068 if (class_type)
10069 declarator = build_nt (SCOPE_REF, class_type, declarator);
10071 /* Everything else is a direct-declarator. */
10072 else
10074 if (parenthesized_p)
10075 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10076 CPP_OPEN_PAREN);
10077 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10078 ctor_dtor_or_conv_p);
10081 if (attributes && declarator != error_mark_node)
10082 declarator = tree_cons (attributes, declarator, NULL_TREE);
10084 return declarator;
10087 /* Parse a direct-declarator or direct-abstract-declarator.
10089 direct-declarator:
10090 declarator-id
10091 direct-declarator ( parameter-declaration-clause )
10092 cv-qualifier-seq [opt]
10093 exception-specification [opt]
10094 direct-declarator [ constant-expression [opt] ]
10095 ( declarator )
10097 direct-abstract-declarator:
10098 direct-abstract-declarator [opt]
10099 ( parameter-declaration-clause )
10100 cv-qualifier-seq [opt]
10101 exception-specification [opt]
10102 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10103 ( abstract-declarator )
10105 Returns a representation of the declarator. DCL_KIND is
10106 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10107 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10108 we are parsing a direct-declarator. It is
10109 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10110 of ambiguity we prefer an abstract declarator, as per
10111 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10112 cp_parser_declarator.
10114 For the declarator-id production, the representation is as for an
10115 id-expression, except that a qualified name is represented as a
10116 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10117 see the documentation of the FUNCTION_DECLARATOR_* macros for
10118 information about how to find the various declarator components.
10119 An array-declarator is represented as an ARRAY_REF. The
10120 direct-declarator is the first operand; the constant-expression
10121 indicating the size of the array is the second operand. */
10123 static tree
10124 cp_parser_direct_declarator (cp_parser* parser,
10125 cp_parser_declarator_kind dcl_kind,
10126 int* ctor_dtor_or_conv_p)
10128 cp_token *token;
10129 tree declarator = NULL_TREE;
10130 tree scope = NULL_TREE;
10131 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10132 bool saved_in_declarator_p = parser->in_declarator_p;
10133 bool first = true;
10135 while (true)
10137 /* Peek at the next token. */
10138 token = cp_lexer_peek_token (parser->lexer);
10139 if (token->type == CPP_OPEN_PAREN)
10141 /* This is either a parameter-declaration-clause, or a
10142 parenthesized declarator. When we know we are parsing a
10143 named declarator, it must be a parenthesized declarator
10144 if FIRST is true. For instance, `(int)' is a
10145 parameter-declaration-clause, with an omitted
10146 direct-abstract-declarator. But `((*))', is a
10147 parenthesized abstract declarator. Finally, when T is a
10148 template parameter `(T)' is a
10149 parameter-declaration-clause, and not a parenthesized
10150 named declarator.
10152 We first try and parse a parameter-declaration-clause,
10153 and then try a nested declarator (if FIRST is true).
10155 It is not an error for it not to be a
10156 parameter-declaration-clause, even when FIRST is
10157 false. Consider,
10159 int i (int);
10160 int i (3);
10162 The first is the declaration of a function while the
10163 second is a the definition of a variable, including its
10164 initializer.
10166 Having seen only the parenthesis, we cannot know which of
10167 these two alternatives should be selected. Even more
10168 complex are examples like:
10170 int i (int (a));
10171 int i (int (3));
10173 The former is a function-declaration; the latter is a
10174 variable initialization.
10176 Thus again, we try a parameter-declaration-clause, and if
10177 that fails, we back out and return. */
10179 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10181 tree params;
10182 unsigned saved_num_template_parameter_lists;
10184 cp_parser_parse_tentatively (parser);
10186 /* Consume the `('. */
10187 cp_lexer_consume_token (parser->lexer);
10188 if (first)
10190 /* If this is going to be an abstract declarator, we're
10191 in a declarator and we can't have default args. */
10192 parser->default_arg_ok_p = false;
10193 parser->in_declarator_p = true;
10196 /* Inside the function parameter list, surrounding
10197 template-parameter-lists do not apply. */
10198 saved_num_template_parameter_lists
10199 = parser->num_template_parameter_lists;
10200 parser->num_template_parameter_lists = 0;
10202 /* Parse the parameter-declaration-clause. */
10203 params = cp_parser_parameter_declaration_clause (parser);
10205 parser->num_template_parameter_lists
10206 = saved_num_template_parameter_lists;
10208 /* If all went well, parse the cv-qualifier-seq and the
10209 exception-specification. */
10210 if (cp_parser_parse_definitely (parser))
10212 tree cv_qualifiers;
10213 tree exception_specification;
10215 if (ctor_dtor_or_conv_p)
10216 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10217 first = false;
10218 /* Consume the `)'. */
10219 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10221 /* Parse the cv-qualifier-seq. */
10222 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10223 /* And the exception-specification. */
10224 exception_specification
10225 = cp_parser_exception_specification_opt (parser);
10227 /* Create the function-declarator. */
10228 declarator = make_call_declarator (declarator,
10229 params,
10230 cv_qualifiers,
10231 exception_specification);
10232 /* Any subsequent parameter lists are to do with
10233 return type, so are not those of the declared
10234 function. */
10235 parser->default_arg_ok_p = false;
10237 /* Repeat the main loop. */
10238 continue;
10242 /* If this is the first, we can try a parenthesized
10243 declarator. */
10244 if (first)
10246 parser->default_arg_ok_p = saved_default_arg_ok_p;
10247 parser->in_declarator_p = saved_in_declarator_p;
10249 /* Consume the `('. */
10250 cp_lexer_consume_token (parser->lexer);
10251 /* Parse the nested declarator. */
10252 declarator
10253 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10254 /*parenthesized_p=*/NULL);
10255 first = false;
10256 /* Expect a `)'. */
10257 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10258 declarator = error_mark_node;
10259 if (declarator == error_mark_node)
10260 break;
10262 goto handle_declarator;
10264 /* Otherwise, we must be done. */
10265 else
10266 break;
10268 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10269 && token->type == CPP_OPEN_SQUARE)
10271 /* Parse an array-declarator. */
10272 tree bounds;
10274 if (ctor_dtor_or_conv_p)
10275 *ctor_dtor_or_conv_p = 0;
10277 first = false;
10278 parser->default_arg_ok_p = false;
10279 parser->in_declarator_p = true;
10280 /* Consume the `['. */
10281 cp_lexer_consume_token (parser->lexer);
10282 /* Peek at the next token. */
10283 token = cp_lexer_peek_token (parser->lexer);
10284 /* If the next token is `]', then there is no
10285 constant-expression. */
10286 if (token->type != CPP_CLOSE_SQUARE)
10288 bool non_constant_p;
10290 bounds
10291 = cp_parser_constant_expression (parser,
10292 /*allow_non_constant=*/true,
10293 &non_constant_p);
10294 if (!non_constant_p)
10295 bounds = cp_parser_fold_non_dependent_expr (bounds);
10297 else
10298 bounds = NULL_TREE;
10299 /* Look for the closing `]'. */
10300 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10302 declarator = error_mark_node;
10303 break;
10306 declarator = build_nt (ARRAY_REF, declarator, bounds);
10308 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10310 /* Parse a declarator-id */
10311 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10312 cp_parser_parse_tentatively (parser);
10313 declarator = cp_parser_declarator_id (parser);
10314 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10316 if (!cp_parser_parse_definitely (parser))
10317 declarator = error_mark_node;
10318 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10320 cp_parser_error (parser, "expected unqualified-id");
10321 declarator = error_mark_node;
10325 if (declarator == error_mark_node)
10326 break;
10328 if (TREE_CODE (declarator) == SCOPE_REF
10329 && !current_scope ())
10331 tree scope = TREE_OPERAND (declarator, 0);
10333 /* In the declaration of a member of a template class
10334 outside of the class itself, the SCOPE will sometimes
10335 be a TYPENAME_TYPE. For example, given:
10337 template <typename T>
10338 int S<T>::R::i = 3;
10340 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10341 this context, we must resolve S<T>::R to an ordinary
10342 type, rather than a typename type.
10344 The reason we normally avoid resolving TYPENAME_TYPEs
10345 is that a specialization of `S' might render
10346 `S<T>::R' not a type. However, if `S' is
10347 specialized, then this `i' will not be used, so there
10348 is no harm in resolving the types here. */
10349 if (TREE_CODE (scope) == TYPENAME_TYPE)
10351 tree type;
10353 /* Resolve the TYPENAME_TYPE. */
10354 type = resolve_typename_type (scope,
10355 /*only_current_p=*/false);
10356 /* If that failed, the declarator is invalid. */
10357 if (type != error_mark_node)
10358 scope = type;
10359 /* Build a new DECLARATOR. */
10360 declarator = build_nt (SCOPE_REF,
10361 scope,
10362 TREE_OPERAND (declarator, 1));
10366 /* Check to see whether the declarator-id names a constructor,
10367 destructor, or conversion. */
10368 if (declarator && ctor_dtor_or_conv_p
10369 && ((TREE_CODE (declarator) == SCOPE_REF
10370 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10371 || (TREE_CODE (declarator) != SCOPE_REF
10372 && at_class_scope_p ())))
10374 tree unqualified_name;
10375 tree class_type;
10377 /* Get the unqualified part of the name. */
10378 if (TREE_CODE (declarator) == SCOPE_REF)
10380 class_type = TREE_OPERAND (declarator, 0);
10381 unqualified_name = TREE_OPERAND (declarator, 1);
10383 else
10385 class_type = current_class_type;
10386 unqualified_name = declarator;
10389 /* See if it names ctor, dtor or conv. */
10390 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10391 || IDENTIFIER_TYPENAME_P (unqualified_name)
10392 || constructor_name_p (unqualified_name, class_type))
10393 *ctor_dtor_or_conv_p = -1;
10396 handle_declarator:;
10397 scope = get_scope_of_declarator (declarator);
10398 if (scope)
10399 /* Any names that appear after the declarator-id for a member
10400 are looked up in the containing scope. */
10401 push_scope (scope);
10402 parser->in_declarator_p = true;
10403 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10404 || (declarator
10405 && (TREE_CODE (declarator) == SCOPE_REF
10406 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10407 /* Default args are only allowed on function
10408 declarations. */
10409 parser->default_arg_ok_p = saved_default_arg_ok_p;
10410 else
10411 parser->default_arg_ok_p = false;
10413 first = false;
10415 /* We're done. */
10416 else
10417 break;
10420 /* For an abstract declarator, we might wind up with nothing at this
10421 point. That's an error; the declarator is not optional. */
10422 if (!declarator)
10423 cp_parser_error (parser, "expected declarator");
10425 /* If we entered a scope, we must exit it now. */
10426 if (scope)
10427 pop_scope (scope);
10429 parser->default_arg_ok_p = saved_default_arg_ok_p;
10430 parser->in_declarator_p = saved_in_declarator_p;
10432 return declarator;
10435 /* Parse a ptr-operator.
10437 ptr-operator:
10438 * cv-qualifier-seq [opt]
10440 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10442 GNU Extension:
10444 ptr-operator:
10445 & cv-qualifier-seq [opt]
10447 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10448 used. Returns ADDR_EXPR if a reference was used. In the
10449 case of a pointer-to-member, *TYPE is filled in with the
10450 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10451 with the cv-qualifier-seq, or NULL_TREE, if there are no
10452 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10454 static enum tree_code
10455 cp_parser_ptr_operator (cp_parser* parser,
10456 tree* type,
10457 tree* cv_qualifier_seq)
10459 enum tree_code code = ERROR_MARK;
10460 cp_token *token;
10462 /* Assume that it's not a pointer-to-member. */
10463 *type = NULL_TREE;
10464 /* And that there are no cv-qualifiers. */
10465 *cv_qualifier_seq = NULL_TREE;
10467 /* Peek at the next token. */
10468 token = cp_lexer_peek_token (parser->lexer);
10469 /* If it's a `*' or `&' we have a pointer or reference. */
10470 if (token->type == CPP_MULT || token->type == CPP_AND)
10472 /* Remember which ptr-operator we were processing. */
10473 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10475 /* Consume the `*' or `&'. */
10476 cp_lexer_consume_token (parser->lexer);
10478 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10479 `&', if we are allowing GNU extensions. (The only qualifier
10480 that can legally appear after `&' is `restrict', but that is
10481 enforced during semantic analysis. */
10482 if (code == INDIRECT_REF
10483 || cp_parser_allow_gnu_extensions_p (parser))
10484 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10486 else
10488 /* Try the pointer-to-member case. */
10489 cp_parser_parse_tentatively (parser);
10490 /* Look for the optional `::' operator. */
10491 cp_parser_global_scope_opt (parser,
10492 /*current_scope_valid_p=*/false);
10493 /* Look for the nested-name specifier. */
10494 cp_parser_nested_name_specifier (parser,
10495 /*typename_keyword_p=*/false,
10496 /*check_dependency_p=*/true,
10497 /*type_p=*/false,
10498 /*is_declaration=*/false);
10499 /* If we found it, and the next token is a `*', then we are
10500 indeed looking at a pointer-to-member operator. */
10501 if (!cp_parser_error_occurred (parser)
10502 && cp_parser_require (parser, CPP_MULT, "`*'"))
10504 /* The type of which the member is a member is given by the
10505 current SCOPE. */
10506 *type = parser->scope;
10507 /* The next name will not be qualified. */
10508 parser->scope = NULL_TREE;
10509 parser->qualifying_scope = NULL_TREE;
10510 parser->object_scope = NULL_TREE;
10511 /* Indicate that the `*' operator was used. */
10512 code = INDIRECT_REF;
10513 /* Look for the optional cv-qualifier-seq. */
10514 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10516 /* If that didn't work we don't have a ptr-operator. */
10517 if (!cp_parser_parse_definitely (parser))
10518 cp_parser_error (parser, "expected ptr-operator");
10521 return code;
10524 /* Parse an (optional) cv-qualifier-seq.
10526 cv-qualifier-seq:
10527 cv-qualifier cv-qualifier-seq [opt]
10529 Returns a TREE_LIST. The TREE_VALUE of each node is the
10530 representation of a cv-qualifier. */
10532 static tree
10533 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10535 tree cv_qualifiers = NULL_TREE;
10537 while (true)
10539 tree cv_qualifier;
10541 /* Look for the next cv-qualifier. */
10542 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10543 /* If we didn't find one, we're done. */
10544 if (!cv_qualifier)
10545 break;
10547 /* Add this cv-qualifier to the list. */
10548 cv_qualifiers
10549 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10552 /* We built up the list in reverse order. */
10553 return nreverse (cv_qualifiers);
10556 /* Parse an (optional) cv-qualifier.
10558 cv-qualifier:
10559 const
10560 volatile
10562 GNU Extension:
10564 cv-qualifier:
10565 __restrict__ */
10567 static tree
10568 cp_parser_cv_qualifier_opt (cp_parser* parser)
10570 cp_token *token;
10571 tree cv_qualifier = NULL_TREE;
10573 /* Peek at the next token. */
10574 token = cp_lexer_peek_token (parser->lexer);
10575 /* See if it's a cv-qualifier. */
10576 switch (token->keyword)
10578 case RID_CONST:
10579 case RID_VOLATILE:
10580 case RID_RESTRICT:
10581 /* Save the value of the token. */
10582 cv_qualifier = token->value;
10583 /* Consume the token. */
10584 cp_lexer_consume_token (parser->lexer);
10585 break;
10587 default:
10588 break;
10591 return cv_qualifier;
10594 /* Parse a declarator-id.
10596 declarator-id:
10597 id-expression
10598 :: [opt] nested-name-specifier [opt] type-name
10600 In the `id-expression' case, the value returned is as for
10601 cp_parser_id_expression if the id-expression was an unqualified-id.
10602 If the id-expression was a qualified-id, then a SCOPE_REF is
10603 returned. The first operand is the scope (either a NAMESPACE_DECL
10604 or TREE_TYPE), but the second is still just a representation of an
10605 unqualified-id. */
10607 static tree
10608 cp_parser_declarator_id (cp_parser* parser)
10610 tree id_expression;
10612 /* The expression must be an id-expression. Assume that qualified
10613 names are the names of types so that:
10615 template <class T>
10616 int S<T>::R::i = 3;
10618 will work; we must treat `S<T>::R' as the name of a type.
10619 Similarly, assume that qualified names are templates, where
10620 required, so that:
10622 template <class T>
10623 int S<T>::R<T>::i = 3;
10625 will work, too. */
10626 id_expression = cp_parser_id_expression (parser,
10627 /*template_keyword_p=*/false,
10628 /*check_dependency_p=*/false,
10629 /*template_p=*/NULL,
10630 /*declarator_p=*/true);
10631 /* If the name was qualified, create a SCOPE_REF to represent
10632 that. */
10633 if (parser->scope)
10635 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10636 parser->scope = NULL_TREE;
10639 return id_expression;
10642 /* Parse a type-id.
10644 type-id:
10645 type-specifier-seq abstract-declarator [opt]
10647 Returns the TYPE specified. */
10649 static tree
10650 cp_parser_type_id (cp_parser* parser)
10652 tree type_specifier_seq;
10653 tree abstract_declarator;
10655 /* Parse the type-specifier-seq. */
10656 type_specifier_seq
10657 = cp_parser_type_specifier_seq (parser);
10658 if (type_specifier_seq == error_mark_node)
10659 return error_mark_node;
10661 /* There might or might not be an abstract declarator. */
10662 cp_parser_parse_tentatively (parser);
10663 /* Look for the declarator. */
10664 abstract_declarator
10665 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10666 /*parenthesized_p=*/NULL);
10667 /* Check to see if there really was a declarator. */
10668 if (!cp_parser_parse_definitely (parser))
10669 abstract_declarator = NULL_TREE;
10671 return groktypename (build_tree_list (type_specifier_seq,
10672 abstract_declarator));
10675 /* Parse a type-specifier-seq.
10677 type-specifier-seq:
10678 type-specifier type-specifier-seq [opt]
10680 GNU extension:
10682 type-specifier-seq:
10683 attributes type-specifier-seq [opt]
10685 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10686 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10688 static tree
10689 cp_parser_type_specifier_seq (cp_parser* parser)
10691 bool seen_type_specifier = false;
10692 tree type_specifier_seq = NULL_TREE;
10694 /* Parse the type-specifiers and attributes. */
10695 while (true)
10697 tree type_specifier;
10699 /* Check for attributes first. */
10700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10702 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10703 NULL_TREE,
10704 type_specifier_seq);
10705 continue;
10708 /* After the first type-specifier, others are optional. */
10709 if (seen_type_specifier)
10710 cp_parser_parse_tentatively (parser);
10711 /* Look for the type-specifier. */
10712 type_specifier = cp_parser_type_specifier (parser,
10713 CP_PARSER_FLAGS_NONE,
10714 /*is_friend=*/false,
10715 /*is_declaration=*/false,
10716 NULL,
10717 NULL);
10718 /* If the first type-specifier could not be found, this is not a
10719 type-specifier-seq at all. */
10720 if (!seen_type_specifier && type_specifier == error_mark_node)
10721 return error_mark_node;
10722 /* If subsequent type-specifiers could not be found, the
10723 type-specifier-seq is complete. */
10724 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10725 break;
10727 /* Add the new type-specifier to the list. */
10728 type_specifier_seq
10729 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10730 seen_type_specifier = true;
10733 /* We built up the list in reverse order. */
10734 return nreverse (type_specifier_seq);
10737 /* Parse a parameter-declaration-clause.
10739 parameter-declaration-clause:
10740 parameter-declaration-list [opt] ... [opt]
10741 parameter-declaration-list , ...
10743 Returns a representation for the parameter declarations. Each node
10744 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10745 representation.) If the parameter-declaration-clause ends with an
10746 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10747 list. A return value of NULL_TREE indicates a
10748 parameter-declaration-clause consisting only of an ellipsis. */
10750 static tree
10751 cp_parser_parameter_declaration_clause (cp_parser* parser)
10753 tree parameters;
10754 cp_token *token;
10755 bool ellipsis_p;
10757 /* Peek at the next token. */
10758 token = cp_lexer_peek_token (parser->lexer);
10759 /* Check for trivial parameter-declaration-clauses. */
10760 if (token->type == CPP_ELLIPSIS)
10762 /* Consume the `...' token. */
10763 cp_lexer_consume_token (parser->lexer);
10764 return NULL_TREE;
10766 else if (token->type == CPP_CLOSE_PAREN)
10767 /* There are no parameters. */
10769 #ifndef NO_IMPLICIT_EXTERN_C
10770 if (in_system_header && current_class_type == NULL
10771 && current_lang_name == lang_name_c)
10772 return NULL_TREE;
10773 else
10774 #endif
10775 return void_list_node;
10777 /* Check for `(void)', too, which is a special case. */
10778 else if (token->keyword == RID_VOID
10779 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10780 == CPP_CLOSE_PAREN))
10782 /* Consume the `void' token. */
10783 cp_lexer_consume_token (parser->lexer);
10784 /* There are no parameters. */
10785 return void_list_node;
10788 /* Parse the parameter-declaration-list. */
10789 parameters = cp_parser_parameter_declaration_list (parser);
10790 /* If a parse error occurred while parsing the
10791 parameter-declaration-list, then the entire
10792 parameter-declaration-clause is erroneous. */
10793 if (parameters == error_mark_node)
10794 return error_mark_node;
10796 /* Peek at the next token. */
10797 token = cp_lexer_peek_token (parser->lexer);
10798 /* If it's a `,', the clause should terminate with an ellipsis. */
10799 if (token->type == CPP_COMMA)
10801 /* Consume the `,'. */
10802 cp_lexer_consume_token (parser->lexer);
10803 /* Expect an ellipsis. */
10804 ellipsis_p
10805 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10807 /* It might also be `...' if the optional trailing `,' was
10808 omitted. */
10809 else if (token->type == CPP_ELLIPSIS)
10811 /* Consume the `...' token. */
10812 cp_lexer_consume_token (parser->lexer);
10813 /* And remember that we saw it. */
10814 ellipsis_p = true;
10816 else
10817 ellipsis_p = false;
10819 /* Finish the parameter list. */
10820 return finish_parmlist (parameters, ellipsis_p);
10823 /* Parse a parameter-declaration-list.
10825 parameter-declaration-list:
10826 parameter-declaration
10827 parameter-declaration-list , parameter-declaration
10829 Returns a representation of the parameter-declaration-list, as for
10830 cp_parser_parameter_declaration_clause. However, the
10831 `void_list_node' is never appended to the list. */
10833 static tree
10834 cp_parser_parameter_declaration_list (cp_parser* parser)
10836 tree parameters = NULL_TREE;
10838 /* Look for more parameters. */
10839 while (true)
10841 tree parameter;
10842 bool parenthesized_p;
10843 /* Parse the parameter. */
10844 parameter
10845 = cp_parser_parameter_declaration (parser,
10846 /*template_parm_p=*/false,
10847 &parenthesized_p);
10849 /* If a parse error occurred parsing the parameter declaration,
10850 then the entire parameter-declaration-list is erroneous. */
10851 if (parameter == error_mark_node)
10853 parameters = error_mark_node;
10854 break;
10856 /* Add the new parameter to the list. */
10857 TREE_CHAIN (parameter) = parameters;
10858 parameters = parameter;
10860 /* Peek at the next token. */
10861 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10862 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10863 /* The parameter-declaration-list is complete. */
10864 break;
10865 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10867 cp_token *token;
10869 /* Peek at the next token. */
10870 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10871 /* If it's an ellipsis, then the list is complete. */
10872 if (token->type == CPP_ELLIPSIS)
10873 break;
10874 /* Otherwise, there must be more parameters. Consume the
10875 `,'. */
10876 cp_lexer_consume_token (parser->lexer);
10877 /* When parsing something like:
10879 int i(float f, double d)
10881 we can tell after seeing the declaration for "f" that we
10882 are not looking at an initialization of a variable "i",
10883 but rather at the declaration of a function "i".
10885 Due to the fact that the parsing of template arguments
10886 (as specified to a template-id) requires backtracking we
10887 cannot use this technique when inside a template argument
10888 list. */
10889 if (!parser->in_template_argument_list_p
10890 && cp_parser_parsing_tentatively (parser)
10891 && !cp_parser_committed_to_tentative_parse (parser)
10892 /* However, a parameter-declaration of the form
10893 "foat(f)" (which is a valid declaration of a
10894 parameter "f") can also be interpreted as an
10895 expression (the conversion of "f" to "float"). */
10896 && !parenthesized_p)
10897 cp_parser_commit_to_tentative_parse (parser);
10899 else
10901 cp_parser_error (parser, "expected `,' or `...'");
10902 if (!cp_parser_parsing_tentatively (parser)
10903 || cp_parser_committed_to_tentative_parse (parser))
10904 cp_parser_skip_to_closing_parenthesis (parser,
10905 /*recovering=*/true,
10906 /*or_comma=*/false,
10907 /*consume_paren=*/false);
10908 break;
10912 /* We built up the list in reverse order; straighten it out now. */
10913 return nreverse (parameters);
10916 /* Parse a parameter declaration.
10918 parameter-declaration:
10919 decl-specifier-seq declarator
10920 decl-specifier-seq declarator = assignment-expression
10921 decl-specifier-seq abstract-declarator [opt]
10922 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10924 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10925 declares a template parameter. (In that case, a non-nested `>'
10926 token encountered during the parsing of the assignment-expression
10927 is not interpreted as a greater-than operator.)
10929 Returns a TREE_LIST representing the parameter-declaration. The
10930 TREE_PURPOSE is the default argument expression, or NULL_TREE if
10931 there is no default argument. The TREE_VALUE is a representation
10932 of the decl-specifier-seq and declarator. In particular, the
10933 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
10934 decl-specifier-seq and whose TREE_VALUE represents the declarator.
10935 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10936 the declarator is of the form "(p)". */
10938 static tree
10939 cp_parser_parameter_declaration (cp_parser *parser,
10940 bool template_parm_p,
10941 bool *parenthesized_p)
10943 int declares_class_or_enum;
10944 bool greater_than_is_operator_p;
10945 tree decl_specifiers;
10946 tree attributes;
10947 tree declarator;
10948 tree default_argument;
10949 tree parameter;
10950 cp_token *token;
10951 const char *saved_message;
10953 /* In a template parameter, `>' is not an operator.
10955 [temp.param]
10957 When parsing a default template-argument for a non-type
10958 template-parameter, the first non-nested `>' is taken as the end
10959 of the template parameter-list rather than a greater-than
10960 operator. */
10961 greater_than_is_operator_p = !template_parm_p;
10963 /* Type definitions may not appear in parameter types. */
10964 saved_message = parser->type_definition_forbidden_message;
10965 parser->type_definition_forbidden_message
10966 = "types may not be defined in parameter types";
10968 /* Parse the declaration-specifiers. */
10969 decl_specifiers
10970 = cp_parser_decl_specifier_seq (parser,
10971 CP_PARSER_FLAGS_NONE,
10972 &attributes,
10973 &declares_class_or_enum);
10974 /* If an error occurred, there's no reason to attempt to parse the
10975 rest of the declaration. */
10976 if (cp_parser_error_occurred (parser))
10978 parser->type_definition_forbidden_message = saved_message;
10979 return error_mark_node;
10982 /* Peek at the next token. */
10983 token = cp_lexer_peek_token (parser->lexer);
10984 /* If the next token is a `)', `,', `=', `>', or `...', then there
10985 is no declarator. */
10986 if (token->type == CPP_CLOSE_PAREN
10987 || token->type == CPP_COMMA
10988 || token->type == CPP_EQ
10989 || token->type == CPP_ELLIPSIS
10990 || token->type == CPP_GREATER)
10992 declarator = NULL_TREE;
10993 if (parenthesized_p)
10994 *parenthesized_p = false;
10996 /* Otherwise, there should be a declarator. */
10997 else
10999 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11000 parser->default_arg_ok_p = false;
11002 /* After seeing a decl-specifier-seq, if the next token is not a
11003 "(", there is no possibility that the code is a valid
11004 expression. Therefore, if parsing tentatively, we commit at
11005 this point. */
11006 if (!parser->in_template_argument_list_p
11007 /* In an expression context, having seen:
11009 (int((char *)...
11011 we cannot be sure whether we are looking at a
11012 function-type (taking a "char*" as a parameter) or a cast
11013 of some object of type "char*" to "int". */
11014 && !parser->in_type_id_in_expr_p
11015 && cp_parser_parsing_tentatively (parser)
11016 && !cp_parser_committed_to_tentative_parse (parser)
11017 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11018 cp_parser_commit_to_tentative_parse (parser);
11019 /* Parse the declarator. */
11020 declarator = cp_parser_declarator (parser,
11021 CP_PARSER_DECLARATOR_EITHER,
11022 /*ctor_dtor_or_conv_p=*/NULL,
11023 parenthesized_p);
11024 parser->default_arg_ok_p = saved_default_arg_ok_p;
11025 /* After the declarator, allow more attributes. */
11026 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11029 /* The restriction on defining new types applies only to the type
11030 of the parameter, not to the default argument. */
11031 parser->type_definition_forbidden_message = saved_message;
11033 /* If the next token is `=', then process a default argument. */
11034 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11036 bool saved_greater_than_is_operator_p;
11037 /* Consume the `='. */
11038 cp_lexer_consume_token (parser->lexer);
11040 /* If we are defining a class, then the tokens that make up the
11041 default argument must be saved and processed later. */
11042 if (!template_parm_p && at_class_scope_p ()
11043 && TYPE_BEING_DEFINED (current_class_type))
11045 unsigned depth = 0;
11047 /* Create a DEFAULT_ARG to represented the unparsed default
11048 argument. */
11049 default_argument = make_node (DEFAULT_ARG);
11050 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11052 /* Add tokens until we have processed the entire default
11053 argument. */
11054 while (true)
11056 bool done = false;
11057 cp_token *token;
11059 /* Peek at the next token. */
11060 token = cp_lexer_peek_token (parser->lexer);
11061 /* What we do depends on what token we have. */
11062 switch (token->type)
11064 /* In valid code, a default argument must be
11065 immediately followed by a `,' `)', or `...'. */
11066 case CPP_COMMA:
11067 case CPP_CLOSE_PAREN:
11068 case CPP_ELLIPSIS:
11069 /* If we run into a non-nested `;', `}', or `]',
11070 then the code is invalid -- but the default
11071 argument is certainly over. */
11072 case CPP_SEMICOLON:
11073 case CPP_CLOSE_BRACE:
11074 case CPP_CLOSE_SQUARE:
11075 if (depth == 0)
11076 done = true;
11077 /* Update DEPTH, if necessary. */
11078 else if (token->type == CPP_CLOSE_PAREN
11079 || token->type == CPP_CLOSE_BRACE
11080 || token->type == CPP_CLOSE_SQUARE)
11081 --depth;
11082 break;
11084 case CPP_OPEN_PAREN:
11085 case CPP_OPEN_SQUARE:
11086 case CPP_OPEN_BRACE:
11087 ++depth;
11088 break;
11090 case CPP_GREATER:
11091 /* If we see a non-nested `>', and `>' is not an
11092 operator, then it marks the end of the default
11093 argument. */
11094 if (!depth && !greater_than_is_operator_p)
11095 done = true;
11096 break;
11098 /* If we run out of tokens, issue an error message. */
11099 case CPP_EOF:
11100 error ("file ends in default argument");
11101 done = true;
11102 break;
11104 case CPP_NAME:
11105 case CPP_SCOPE:
11106 /* In these cases, we should look for template-ids.
11107 For example, if the default argument is
11108 `X<int, double>()', we need to do name lookup to
11109 figure out whether or not `X' is a template; if
11110 so, the `,' does not end the default argument.
11112 That is not yet done. */
11113 break;
11115 default:
11116 break;
11119 /* If we've reached the end, stop. */
11120 if (done)
11121 break;
11123 /* Add the token to the token block. */
11124 token = cp_lexer_consume_token (parser->lexer);
11125 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11126 token);
11129 /* Outside of a class definition, we can just parse the
11130 assignment-expression. */
11131 else
11133 bool saved_local_variables_forbidden_p;
11135 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11136 set correctly. */
11137 saved_greater_than_is_operator_p
11138 = parser->greater_than_is_operator_p;
11139 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11140 /* Local variable names (and the `this' keyword) may not
11141 appear in a default argument. */
11142 saved_local_variables_forbidden_p
11143 = parser->local_variables_forbidden_p;
11144 parser->local_variables_forbidden_p = true;
11145 /* Parse the assignment-expression. */
11146 default_argument = cp_parser_assignment_expression (parser);
11147 /* Restore saved state. */
11148 parser->greater_than_is_operator_p
11149 = saved_greater_than_is_operator_p;
11150 parser->local_variables_forbidden_p
11151 = saved_local_variables_forbidden_p;
11153 if (!parser->default_arg_ok_p)
11155 if (!flag_pedantic_errors)
11156 warning ("deprecated use of default argument for parameter of non-function");
11157 else
11159 error ("default arguments are only permitted for function parameters");
11160 default_argument = NULL_TREE;
11164 else
11165 default_argument = NULL_TREE;
11167 /* Create the representation of the parameter. */
11168 if (attributes)
11169 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11170 parameter = build_tree_list (default_argument,
11171 build_tree_list (decl_specifiers,
11172 declarator));
11174 return parameter;
11177 /* Parse a function-body.
11179 function-body:
11180 compound_statement */
11182 static void
11183 cp_parser_function_body (cp_parser *parser)
11185 cp_parser_compound_statement (parser, false);
11188 /* Parse a ctor-initializer-opt followed by a function-body. Return
11189 true if a ctor-initializer was present. */
11191 static bool
11192 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11194 tree body;
11195 bool ctor_initializer_p;
11197 /* Begin the function body. */
11198 body = begin_function_body ();
11199 /* Parse the optional ctor-initializer. */
11200 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11201 /* Parse the function-body. */
11202 cp_parser_function_body (parser);
11203 /* Finish the function body. */
11204 finish_function_body (body);
11206 return ctor_initializer_p;
11209 /* Parse an initializer.
11211 initializer:
11212 = initializer-clause
11213 ( expression-list )
11215 Returns a expression representing the initializer. If no
11216 initializer is present, NULL_TREE is returned.
11218 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11219 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11220 set to FALSE if there is no initializer present. If there is an
11221 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11222 is set to true; otherwise it is set to false. */
11224 static tree
11225 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11226 bool* non_constant_p)
11228 cp_token *token;
11229 tree init;
11231 /* Peek at the next token. */
11232 token = cp_lexer_peek_token (parser->lexer);
11234 /* Let our caller know whether or not this initializer was
11235 parenthesized. */
11236 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11237 /* Assume that the initializer is constant. */
11238 *non_constant_p = false;
11240 if (token->type == CPP_EQ)
11242 /* Consume the `='. */
11243 cp_lexer_consume_token (parser->lexer);
11244 /* Parse the initializer-clause. */
11245 init = cp_parser_initializer_clause (parser, non_constant_p);
11247 else if (token->type == CPP_OPEN_PAREN)
11248 init = cp_parser_parenthesized_expression_list (parser, false,
11249 non_constant_p);
11250 else
11252 /* Anything else is an error. */
11253 cp_parser_error (parser, "expected initializer");
11254 init = error_mark_node;
11257 return init;
11260 /* Parse an initializer-clause.
11262 initializer-clause:
11263 assignment-expression
11264 { initializer-list , [opt] }
11267 Returns an expression representing the initializer.
11269 If the `assignment-expression' production is used the value
11270 returned is simply a representation for the expression.
11272 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11273 the elements of the initializer-list (or NULL_TREE, if the last
11274 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11275 NULL_TREE. There is no way to detect whether or not the optional
11276 trailing `,' was provided. NON_CONSTANT_P is as for
11277 cp_parser_initializer. */
11279 static tree
11280 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11282 tree initializer;
11284 /* If it is not a `{', then we are looking at an
11285 assignment-expression. */
11286 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11287 initializer
11288 = cp_parser_constant_expression (parser,
11289 /*allow_non_constant_p=*/true,
11290 non_constant_p);
11291 else
11293 /* Consume the `{' token. */
11294 cp_lexer_consume_token (parser->lexer);
11295 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11296 initializer = make_node (CONSTRUCTOR);
11297 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11298 necessary, but check_initializer depends upon it, for
11299 now. */
11300 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11301 /* If it's not a `}', then there is a non-trivial initializer. */
11302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11304 /* Parse the initializer list. */
11305 CONSTRUCTOR_ELTS (initializer)
11306 = cp_parser_initializer_list (parser, non_constant_p);
11307 /* A trailing `,' token is allowed. */
11308 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11309 cp_lexer_consume_token (parser->lexer);
11311 /* Now, there should be a trailing `}'. */
11312 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11315 return initializer;
11318 /* Parse an initializer-list.
11320 initializer-list:
11321 initializer-clause
11322 initializer-list , initializer-clause
11324 GNU Extension:
11326 initializer-list:
11327 identifier : initializer-clause
11328 initializer-list, identifier : initializer-clause
11330 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11331 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11332 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11333 as for cp_parser_initializer. */
11335 static tree
11336 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11338 tree initializers = NULL_TREE;
11340 /* Assume all of the expressions are constant. */
11341 *non_constant_p = false;
11343 /* Parse the rest of the list. */
11344 while (true)
11346 cp_token *token;
11347 tree identifier;
11348 tree initializer;
11349 bool clause_non_constant_p;
11351 /* If the next token is an identifier and the following one is a
11352 colon, we are looking at the GNU designated-initializer
11353 syntax. */
11354 if (cp_parser_allow_gnu_extensions_p (parser)
11355 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11356 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11358 /* Consume the identifier. */
11359 identifier = cp_lexer_consume_token (parser->lexer)->value;
11360 /* Consume the `:'. */
11361 cp_lexer_consume_token (parser->lexer);
11363 else
11364 identifier = NULL_TREE;
11366 /* Parse the initializer. */
11367 initializer = cp_parser_initializer_clause (parser,
11368 &clause_non_constant_p);
11369 /* If any clause is non-constant, so is the entire initializer. */
11370 if (clause_non_constant_p)
11371 *non_constant_p = true;
11372 /* Add it to the list. */
11373 initializers = tree_cons (identifier, initializer, initializers);
11375 /* If the next token is not a comma, we have reached the end of
11376 the list. */
11377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11378 break;
11380 /* Peek at the next token. */
11381 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11382 /* If the next token is a `}', then we're still done. An
11383 initializer-clause can have a trailing `,' after the
11384 initializer-list and before the closing `}'. */
11385 if (token->type == CPP_CLOSE_BRACE)
11386 break;
11388 /* Consume the `,' token. */
11389 cp_lexer_consume_token (parser->lexer);
11392 /* The initializers were built up in reverse order, so we need to
11393 reverse them now. */
11394 return nreverse (initializers);
11397 /* Classes [gram.class] */
11399 /* Parse a class-name.
11401 class-name:
11402 identifier
11403 template-id
11405 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11406 to indicate that names looked up in dependent types should be
11407 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11408 keyword has been used to indicate that the name that appears next
11409 is a template. TYPE_P is true iff the next name should be treated
11410 as class-name, even if it is declared to be some other kind of name
11411 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11412 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11413 being defined in a class-head.
11415 Returns the TYPE_DECL representing the class. */
11417 static tree
11418 cp_parser_class_name (cp_parser *parser,
11419 bool typename_keyword_p,
11420 bool template_keyword_p,
11421 bool type_p,
11422 bool check_dependency_p,
11423 bool class_head_p,
11424 bool is_declaration)
11426 tree decl;
11427 tree scope;
11428 bool typename_p;
11429 cp_token *token;
11431 /* All class-names start with an identifier. */
11432 token = cp_lexer_peek_token (parser->lexer);
11433 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11435 cp_parser_error (parser, "expected class-name");
11436 return error_mark_node;
11439 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11440 to a template-id, so we save it here. */
11441 scope = parser->scope;
11442 if (scope == error_mark_node)
11443 return error_mark_node;
11445 /* Any name names a type if we're following the `typename' keyword
11446 in a qualified name where the enclosing scope is type-dependent. */
11447 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11448 && dependent_type_p (scope));
11449 /* Handle the common case (an identifier, but not a template-id)
11450 efficiently. */
11451 if (token->type == CPP_NAME
11452 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11454 tree identifier;
11456 /* Look for the identifier. */
11457 identifier = cp_parser_identifier (parser);
11458 /* If the next token isn't an identifier, we are certainly not
11459 looking at a class-name. */
11460 if (identifier == error_mark_node)
11461 decl = error_mark_node;
11462 /* If we know this is a type-name, there's no need to look it
11463 up. */
11464 else if (typename_p)
11465 decl = identifier;
11466 else
11468 /* If the next token is a `::', then the name must be a type
11469 name.
11471 [basic.lookup.qual]
11473 During the lookup for a name preceding the :: scope
11474 resolution operator, object, function, and enumerator
11475 names are ignored. */
11476 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11477 type_p = true;
11478 /* Look up the name. */
11479 decl = cp_parser_lookup_name (parser, identifier,
11480 type_p,
11481 /*is_template=*/false,
11482 /*is_namespace=*/false,
11483 check_dependency_p);
11486 else
11488 /* Try a template-id. */
11489 decl = cp_parser_template_id (parser, template_keyword_p,
11490 check_dependency_p,
11491 is_declaration);
11492 if (decl == error_mark_node)
11493 return error_mark_node;
11496 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11498 /* If this is a typename, create a TYPENAME_TYPE. */
11499 if (typename_p && decl != error_mark_node)
11500 decl = TYPE_NAME (make_typename_type (scope, decl,
11501 /*complain=*/1));
11503 /* Check to see that it is really the name of a class. */
11504 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11505 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11506 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11507 /* Situations like this:
11509 template <typename T> struct A {
11510 typename T::template X<int>::I i;
11513 are problematic. Is `T::template X<int>' a class-name? The
11514 standard does not seem to be definitive, but there is no other
11515 valid interpretation of the following `::'. Therefore, those
11516 names are considered class-names. */
11517 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11518 else if (decl == error_mark_node
11519 || TREE_CODE (decl) != TYPE_DECL
11520 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11522 cp_parser_error (parser, "expected class-name");
11523 return error_mark_node;
11526 return decl;
11529 /* Parse a class-specifier.
11531 class-specifier:
11532 class-head { member-specification [opt] }
11534 Returns the TREE_TYPE representing the class. */
11536 static tree
11537 cp_parser_class_specifier (cp_parser* parser)
11539 cp_token *token;
11540 tree type;
11541 tree attributes = NULL_TREE;
11542 int has_trailing_semicolon;
11543 bool nested_name_specifier_p;
11544 unsigned saved_num_template_parameter_lists;
11546 push_deferring_access_checks (dk_no_deferred);
11548 /* Parse the class-head. */
11549 type = cp_parser_class_head (parser,
11550 &nested_name_specifier_p);
11551 /* If the class-head was a semantic disaster, skip the entire body
11552 of the class. */
11553 if (!type)
11555 cp_parser_skip_to_end_of_block_or_statement (parser);
11556 pop_deferring_access_checks ();
11557 return error_mark_node;
11560 /* Look for the `{'. */
11561 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11563 pop_deferring_access_checks ();
11564 return error_mark_node;
11567 /* Issue an error message if type-definitions are forbidden here. */
11568 cp_parser_check_type_definition (parser);
11569 /* Remember that we are defining one more class. */
11570 ++parser->num_classes_being_defined;
11571 /* Inside the class, surrounding template-parameter-lists do not
11572 apply. */
11573 saved_num_template_parameter_lists
11574 = parser->num_template_parameter_lists;
11575 parser->num_template_parameter_lists = 0;
11577 /* Start the class. */
11578 if (nested_name_specifier_p)
11579 push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11580 type = begin_class_definition (type);
11581 if (type == error_mark_node)
11582 /* If the type is erroneous, skip the entire body of the class. */
11583 cp_parser_skip_to_closing_brace (parser);
11584 else
11585 /* Parse the member-specification. */
11586 cp_parser_member_specification_opt (parser);
11587 /* Look for the trailing `}'. */
11588 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11589 /* We get better error messages by noticing a common problem: a
11590 missing trailing `;'. */
11591 token = cp_lexer_peek_token (parser->lexer);
11592 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11593 /* Look for attributes to apply to this class. */
11594 if (cp_parser_allow_gnu_extensions_p (parser))
11595 attributes = cp_parser_attributes_opt (parser);
11596 /* If we got any attributes in class_head, xref_tag will stick them in
11597 TREE_TYPE of the type. Grab them now. */
11598 if (type != error_mark_node)
11600 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11601 TYPE_ATTRIBUTES (type) = NULL_TREE;
11602 type = finish_struct (type, attributes);
11604 if (nested_name_specifier_p)
11605 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11606 /* If this class is not itself within the scope of another class,
11607 then we need to parse the bodies of all of the queued function
11608 definitions. Note that the queued functions defined in a class
11609 are not always processed immediately following the
11610 class-specifier for that class. Consider:
11612 struct A {
11613 struct B { void f() { sizeof (A); } };
11616 If `f' were processed before the processing of `A' were
11617 completed, there would be no way to compute the size of `A'.
11618 Note that the nesting we are interested in here is lexical --
11619 not the semantic nesting given by TYPE_CONTEXT. In particular,
11620 for:
11622 struct A { struct B; };
11623 struct A::B { void f() { } };
11625 there is no need to delay the parsing of `A::B::f'. */
11626 if (--parser->num_classes_being_defined == 0)
11628 tree queue_entry;
11629 tree fn;
11631 /* In a first pass, parse default arguments to the functions.
11632 Then, in a second pass, parse the bodies of the functions.
11633 This two-phased approach handles cases like:
11635 struct S {
11636 void f() { g(); }
11637 void g(int i = 3);
11641 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11642 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11643 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11644 TREE_PURPOSE (parser->unparsed_functions_queues)
11645 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11647 fn = TREE_VALUE (queue_entry);
11648 /* Make sure that any template parameters are in scope. */
11649 maybe_begin_member_template_processing (fn);
11650 /* If there are default arguments that have not yet been processed,
11651 take care of them now. */
11652 cp_parser_late_parsing_default_args (parser, fn);
11653 /* Remove any template parameters from the symbol table. */
11654 maybe_end_member_template_processing ();
11656 /* Now parse the body of the functions. */
11657 for (TREE_VALUE (parser->unparsed_functions_queues)
11658 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11659 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11660 TREE_VALUE (parser->unparsed_functions_queues)
11661 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11663 /* Figure out which function we need to process. */
11664 fn = TREE_VALUE (queue_entry);
11666 /* Parse the function. */
11667 cp_parser_late_parsing_for_member (parser, fn);
11672 /* Put back any saved access checks. */
11673 pop_deferring_access_checks ();
11675 /* Restore the count of active template-parameter-lists. */
11676 parser->num_template_parameter_lists
11677 = saved_num_template_parameter_lists;
11679 return type;
11682 /* Parse a class-head.
11684 class-head:
11685 class-key identifier [opt] base-clause [opt]
11686 class-key nested-name-specifier identifier base-clause [opt]
11687 class-key nested-name-specifier [opt] template-id
11688 base-clause [opt]
11690 GNU Extensions:
11691 class-key attributes identifier [opt] base-clause [opt]
11692 class-key attributes nested-name-specifier identifier base-clause [opt]
11693 class-key attributes nested-name-specifier [opt] template-id
11694 base-clause [opt]
11696 Returns the TYPE of the indicated class. Sets
11697 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11698 involving a nested-name-specifier was used, and FALSE otherwise.
11700 Returns NULL_TREE if the class-head is syntactically valid, but
11701 semantically invalid in a way that means we should skip the entire
11702 body of the class. */
11704 static tree
11705 cp_parser_class_head (cp_parser* parser,
11706 bool* nested_name_specifier_p)
11708 cp_token *token;
11709 tree nested_name_specifier;
11710 enum tag_types class_key;
11711 tree id = NULL_TREE;
11712 tree type = NULL_TREE;
11713 tree attributes;
11714 bool template_id_p = false;
11715 bool qualified_p = false;
11716 bool invalid_nested_name_p = false;
11717 bool invalid_explicit_specialization_p = false;
11718 unsigned num_templates;
11720 /* Assume no nested-name-specifier will be present. */
11721 *nested_name_specifier_p = false;
11722 /* Assume no template parameter lists will be used in defining the
11723 type. */
11724 num_templates = 0;
11726 /* Look for the class-key. */
11727 class_key = cp_parser_class_key (parser);
11728 if (class_key == none_type)
11729 return error_mark_node;
11731 /* Parse the attributes. */
11732 attributes = cp_parser_attributes_opt (parser);
11734 /* If the next token is `::', that is invalid -- but sometimes
11735 people do try to write:
11737 struct ::S {};
11739 Handle this gracefully by accepting the extra qualifier, and then
11740 issuing an error about it later if this really is a
11741 class-head. If it turns out just to be an elaborated type
11742 specifier, remain silent. */
11743 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11744 qualified_p = true;
11746 push_deferring_access_checks (dk_no_check);
11748 /* Determine the name of the class. Begin by looking for an
11749 optional nested-name-specifier. */
11750 nested_name_specifier
11751 = cp_parser_nested_name_specifier_opt (parser,
11752 /*typename_keyword_p=*/false,
11753 /*check_dependency_p=*/false,
11754 /*type_p=*/false,
11755 /*is_declaration=*/false);
11756 /* If there was a nested-name-specifier, then there *must* be an
11757 identifier. */
11758 if (nested_name_specifier)
11760 /* Although the grammar says `identifier', it really means
11761 `class-name' or `template-name'. You are only allowed to
11762 define a class that has already been declared with this
11763 syntax.
11765 The proposed resolution for Core Issue 180 says that whever
11766 you see `class T::X' you should treat `X' as a type-name.
11768 It is OK to define an inaccessible class; for example:
11770 class A { class B; };
11771 class A::B {};
11773 We do not know if we will see a class-name, or a
11774 template-name. We look for a class-name first, in case the
11775 class-name is a template-id; if we looked for the
11776 template-name first we would stop after the template-name. */
11777 cp_parser_parse_tentatively (parser);
11778 type = cp_parser_class_name (parser,
11779 /*typename_keyword_p=*/false,
11780 /*template_keyword_p=*/false,
11781 /*type_p=*/true,
11782 /*check_dependency_p=*/false,
11783 /*class_head_p=*/true,
11784 /*is_declaration=*/false);
11785 /* If that didn't work, ignore the nested-name-specifier. */
11786 if (!cp_parser_parse_definitely (parser))
11788 invalid_nested_name_p = true;
11789 id = cp_parser_identifier (parser);
11790 if (id == error_mark_node)
11791 id = NULL_TREE;
11793 /* If we could not find a corresponding TYPE, treat this
11794 declaration like an unqualified declaration. */
11795 if (type == error_mark_node)
11796 nested_name_specifier = NULL_TREE;
11797 /* Otherwise, count the number of templates used in TYPE and its
11798 containing scopes. */
11799 else
11801 tree scope;
11803 for (scope = TREE_TYPE (type);
11804 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11805 scope = (TYPE_P (scope)
11806 ? TYPE_CONTEXT (scope)
11807 : DECL_CONTEXT (scope)))
11808 if (TYPE_P (scope)
11809 && CLASS_TYPE_P (scope)
11810 && CLASSTYPE_TEMPLATE_INFO (scope)
11811 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11812 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11813 ++num_templates;
11816 /* Otherwise, the identifier is optional. */
11817 else
11819 /* We don't know whether what comes next is a template-id,
11820 an identifier, or nothing at all. */
11821 cp_parser_parse_tentatively (parser);
11822 /* Check for a template-id. */
11823 id = cp_parser_template_id (parser,
11824 /*template_keyword_p=*/false,
11825 /*check_dependency_p=*/true,
11826 /*is_declaration=*/true);
11827 /* If that didn't work, it could still be an identifier. */
11828 if (!cp_parser_parse_definitely (parser))
11830 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11831 id = cp_parser_identifier (parser);
11832 else
11833 id = NULL_TREE;
11835 else
11837 template_id_p = true;
11838 ++num_templates;
11842 pop_deferring_access_checks ();
11844 cp_parser_check_for_invalid_template_id (parser, id);
11846 /* If it's not a `:' or a `{' then we can't really be looking at a
11847 class-head, since a class-head only appears as part of a
11848 class-specifier. We have to detect this situation before calling
11849 xref_tag, since that has irreversible side-effects. */
11850 if (!cp_parser_next_token_starts_class_definition_p (parser))
11852 cp_parser_error (parser, "expected `{' or `:'");
11853 return error_mark_node;
11856 /* At this point, we're going ahead with the class-specifier, even
11857 if some other problem occurs. */
11858 cp_parser_commit_to_tentative_parse (parser);
11859 /* Issue the error about the overly-qualified name now. */
11860 if (qualified_p)
11861 cp_parser_error (parser,
11862 "global qualification of class name is invalid");
11863 else if (invalid_nested_name_p)
11864 cp_parser_error (parser,
11865 "qualified name does not name a class");
11866 else if (nested_name_specifier)
11868 tree scope;
11869 /* Figure out in what scope the declaration is being placed. */
11870 scope = current_scope ();
11871 if (!scope)
11872 scope = current_namespace;
11873 /* If that scope does not contain the scope in which the
11874 class was originally declared, the program is invalid. */
11875 if (scope && !is_ancestor (scope, nested_name_specifier))
11877 error ("declaration of `%D' in `%D' which does not "
11878 "enclose `%D'", type, scope, nested_name_specifier);
11879 type = NULL_TREE;
11880 goto done;
11882 /* [dcl.meaning]
11884 A declarator-id shall not be qualified exception of the
11885 definition of a ... nested class outside of its class
11886 ... [or] a the definition or explicit instantiation of a
11887 class member of a namespace outside of its namespace. */
11888 if (scope == nested_name_specifier)
11890 pedwarn ("extra qualification ignored");
11891 nested_name_specifier = NULL_TREE;
11892 num_templates = 0;
11895 /* An explicit-specialization must be preceded by "template <>". If
11896 it is not, try to recover gracefully. */
11897 if (at_namespace_scope_p ()
11898 && parser->num_template_parameter_lists == 0
11899 && template_id_p)
11901 error ("an explicit specialization must be preceded by 'template <>'");
11902 invalid_explicit_specialization_p = true;
11903 /* Take the same action that would have been taken by
11904 cp_parser_explicit_specialization. */
11905 ++parser->num_template_parameter_lists;
11906 begin_specialization ();
11908 /* There must be no "return" statements between this point and the
11909 end of this function; set "type "to the correct return value and
11910 use "goto done;" to return. */
11911 /* Make sure that the right number of template parameters were
11912 present. */
11913 if (!cp_parser_check_template_parameters (parser, num_templates))
11915 /* If something went wrong, there is no point in even trying to
11916 process the class-definition. */
11917 type = NULL_TREE;
11918 goto done;
11921 /* Look up the type. */
11922 if (template_id_p)
11924 type = TREE_TYPE (id);
11925 maybe_process_partial_specialization (type);
11927 else if (!nested_name_specifier)
11929 /* If the class was unnamed, create a dummy name. */
11930 if (!id)
11931 id = make_anon_name ();
11932 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11933 parser->num_template_parameter_lists);
11935 else
11937 tree class_type;
11939 /* Given:
11941 template <typename T> struct S { struct T };
11942 template <typename T> struct S<T>::T { };
11944 we will get a TYPENAME_TYPE when processing the definition of
11945 `S::T'. We need to resolve it to the actual type before we
11946 try to define it. */
11947 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11949 class_type = resolve_typename_type (TREE_TYPE (type),
11950 /*only_current_p=*/false);
11951 if (class_type != error_mark_node)
11952 type = TYPE_NAME (class_type);
11953 else
11955 cp_parser_error (parser, "could not resolve typename type");
11956 type = error_mark_node;
11960 maybe_process_partial_specialization (TREE_TYPE (type));
11961 class_type = current_class_type;
11962 /* Enter the scope indicated by the nested-name-specifier. */
11963 if (nested_name_specifier)
11964 push_scope (nested_name_specifier);
11965 /* Get the canonical version of this type. */
11966 type = TYPE_MAIN_DECL (TREE_TYPE (type));
11967 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11968 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11969 type = push_template_decl (type);
11970 type = TREE_TYPE (type);
11971 if (nested_name_specifier)
11973 *nested_name_specifier_p = true;
11974 pop_scope (nested_name_specifier);
11977 /* Indicate whether this class was declared as a `class' or as a
11978 `struct'. */
11979 if (TREE_CODE (type) == RECORD_TYPE)
11980 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11981 cp_parser_check_class_key (class_key, type);
11983 /* Enter the scope containing the class; the names of base classes
11984 should be looked up in that context. For example, given:
11986 struct A { struct B {}; struct C; };
11987 struct A::C : B {};
11989 is valid. */
11990 if (nested_name_specifier)
11991 push_scope (nested_name_specifier);
11992 /* Now, look for the base-clause. */
11993 token = cp_lexer_peek_token (parser->lexer);
11994 if (token->type == CPP_COLON)
11996 tree bases;
11998 /* Get the list of base-classes. */
11999 bases = cp_parser_base_clause (parser);
12000 /* Process them. */
12001 xref_basetypes (type, bases);
12003 /* Leave the scope given by the nested-name-specifier. We will
12004 enter the class scope itself while processing the members. */
12005 if (nested_name_specifier)
12006 pop_scope (nested_name_specifier);
12008 done:
12009 if (invalid_explicit_specialization_p)
12011 end_specialization ();
12012 --parser->num_template_parameter_lists;
12014 return type;
12017 /* Parse a class-key.
12019 class-key:
12020 class
12021 struct
12022 union
12024 Returns the kind of class-key specified, or none_type to indicate
12025 error. */
12027 static enum tag_types
12028 cp_parser_class_key (cp_parser* parser)
12030 cp_token *token;
12031 enum tag_types tag_type;
12033 /* Look for the class-key. */
12034 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12035 if (!token)
12036 return none_type;
12038 /* Check to see if the TOKEN is a class-key. */
12039 tag_type = cp_parser_token_is_class_key (token);
12040 if (!tag_type)
12041 cp_parser_error (parser, "expected class-key");
12042 return tag_type;
12045 /* Parse an (optional) member-specification.
12047 member-specification:
12048 member-declaration member-specification [opt]
12049 access-specifier : member-specification [opt] */
12051 static void
12052 cp_parser_member_specification_opt (cp_parser* parser)
12054 while (true)
12056 cp_token *token;
12057 enum rid keyword;
12059 /* Peek at the next token. */
12060 token = cp_lexer_peek_token (parser->lexer);
12061 /* If it's a `}', or EOF then we've seen all the members. */
12062 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12063 break;
12065 /* See if this token is a keyword. */
12066 keyword = token->keyword;
12067 switch (keyword)
12069 case RID_PUBLIC:
12070 case RID_PROTECTED:
12071 case RID_PRIVATE:
12072 /* Consume the access-specifier. */
12073 cp_lexer_consume_token (parser->lexer);
12074 /* Remember which access-specifier is active. */
12075 current_access_specifier = token->value;
12076 /* Look for the `:'. */
12077 cp_parser_require (parser, CPP_COLON, "`:'");
12078 break;
12080 default:
12081 /* Otherwise, the next construction must be a
12082 member-declaration. */
12083 cp_parser_member_declaration (parser);
12088 /* Parse a member-declaration.
12090 member-declaration:
12091 decl-specifier-seq [opt] member-declarator-list [opt] ;
12092 function-definition ; [opt]
12093 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12094 using-declaration
12095 template-declaration
12097 member-declarator-list:
12098 member-declarator
12099 member-declarator-list , member-declarator
12101 member-declarator:
12102 declarator pure-specifier [opt]
12103 declarator constant-initializer [opt]
12104 identifier [opt] : constant-expression
12106 GNU Extensions:
12108 member-declaration:
12109 __extension__ member-declaration
12111 member-declarator:
12112 declarator attributes [opt] pure-specifier [opt]
12113 declarator attributes [opt] constant-initializer [opt]
12114 identifier [opt] attributes [opt] : constant-expression */
12116 static void
12117 cp_parser_member_declaration (cp_parser* parser)
12119 tree decl_specifiers;
12120 tree prefix_attributes;
12121 tree decl;
12122 int declares_class_or_enum;
12123 bool friend_p;
12124 cp_token *token;
12125 int saved_pedantic;
12127 /* Check for the `__extension__' keyword. */
12128 if (cp_parser_extension_opt (parser, &saved_pedantic))
12130 /* Recurse. */
12131 cp_parser_member_declaration (parser);
12132 /* Restore the old value of the PEDANTIC flag. */
12133 pedantic = saved_pedantic;
12135 return;
12138 /* Check for a template-declaration. */
12139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12141 /* Parse the template-declaration. */
12142 cp_parser_template_declaration (parser, /*member_p=*/true);
12144 return;
12147 /* Check for a using-declaration. */
12148 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12150 /* Parse the using-declaration. */
12151 cp_parser_using_declaration (parser);
12153 return;
12156 /* Parse the decl-specifier-seq. */
12157 decl_specifiers
12158 = cp_parser_decl_specifier_seq (parser,
12159 CP_PARSER_FLAGS_OPTIONAL,
12160 &prefix_attributes,
12161 &declares_class_or_enum);
12162 /* Check for an invalid type-name. */
12163 if (cp_parser_diagnose_invalid_type_name (parser))
12164 return;
12165 /* If there is no declarator, then the decl-specifier-seq should
12166 specify a type. */
12167 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12169 /* If there was no decl-specifier-seq, and the next token is a
12170 `;', then we have something like:
12172 struct S { ; };
12174 [class.mem]
12176 Each member-declaration shall declare at least one member
12177 name of the class. */
12178 if (!decl_specifiers)
12180 if (pedantic)
12181 pedwarn ("extra semicolon");
12183 else
12185 tree type;
12187 /* See if this declaration is a friend. */
12188 friend_p = cp_parser_friend_p (decl_specifiers);
12189 /* If there were decl-specifiers, check to see if there was
12190 a class-declaration. */
12191 type = check_tag_decl (decl_specifiers);
12192 /* Nested classes have already been added to the class, but
12193 a `friend' needs to be explicitly registered. */
12194 if (friend_p)
12196 /* If the `friend' keyword was present, the friend must
12197 be introduced with a class-key. */
12198 if (!declares_class_or_enum)
12199 error ("a class-key must be used when declaring a friend");
12200 /* In this case:
12202 template <typename T> struct A {
12203 friend struct A<T>::B;
12206 A<T>::B will be represented by a TYPENAME_TYPE, and
12207 therefore not recognized by check_tag_decl. */
12208 if (!type)
12210 tree specifier;
12212 for (specifier = decl_specifiers;
12213 specifier;
12214 specifier = TREE_CHAIN (specifier))
12216 tree s = TREE_VALUE (specifier);
12218 if (TREE_CODE (s) == IDENTIFIER_NODE)
12219 get_global_value_if_present (s, &type);
12220 if (TREE_CODE (s) == TYPE_DECL)
12221 s = TREE_TYPE (s);
12222 if (TYPE_P (s))
12224 type = s;
12225 break;
12229 if (!type)
12230 error ("friend declaration does not name a class or "
12231 "function");
12232 else
12233 make_friend_class (current_class_type, type,
12234 /*complain=*/true);
12236 /* If there is no TYPE, an error message will already have
12237 been issued. */
12238 else if (!type)
12240 /* An anonymous aggregate has to be handled specially; such
12241 a declaration really declares a data member (with a
12242 particular type), as opposed to a nested class. */
12243 else if (ANON_AGGR_TYPE_P (type))
12245 /* Remove constructors and such from TYPE, now that we
12246 know it is an anonymous aggregate. */
12247 fixup_anonymous_aggr (type);
12248 /* And make the corresponding data member. */
12249 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12250 /* Add it to the class. */
12251 finish_member_declaration (decl);
12253 else
12254 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12257 else
12259 /* See if these declarations will be friends. */
12260 friend_p = cp_parser_friend_p (decl_specifiers);
12262 /* Keep going until we hit the `;' at the end of the
12263 declaration. */
12264 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12266 tree attributes = NULL_TREE;
12267 tree first_attribute;
12269 /* Peek at the next token. */
12270 token = cp_lexer_peek_token (parser->lexer);
12272 /* Check for a bitfield declaration. */
12273 if (token->type == CPP_COLON
12274 || (token->type == CPP_NAME
12275 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12276 == CPP_COLON))
12278 tree identifier;
12279 tree width;
12281 /* Get the name of the bitfield. Note that we cannot just
12282 check TOKEN here because it may have been invalidated by
12283 the call to cp_lexer_peek_nth_token above. */
12284 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12285 identifier = cp_parser_identifier (parser);
12286 else
12287 identifier = NULL_TREE;
12289 /* Consume the `:' token. */
12290 cp_lexer_consume_token (parser->lexer);
12291 /* Get the width of the bitfield. */
12292 width
12293 = cp_parser_constant_expression (parser,
12294 /*allow_non_constant=*/false,
12295 NULL);
12297 /* Look for attributes that apply to the bitfield. */
12298 attributes = cp_parser_attributes_opt (parser);
12299 /* Remember which attributes are prefix attributes and
12300 which are not. */
12301 first_attribute = attributes;
12302 /* Combine the attributes. */
12303 attributes = chainon (prefix_attributes, attributes);
12305 /* Create the bitfield declaration. */
12306 decl = grokbitfield (identifier,
12307 decl_specifiers,
12308 width);
12309 /* Apply the attributes. */
12310 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12312 else
12314 tree declarator;
12315 tree initializer;
12316 tree asm_specification;
12317 int ctor_dtor_or_conv_p;
12319 /* Parse the declarator. */
12320 declarator
12321 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12322 &ctor_dtor_or_conv_p,
12323 /*parenthesized_p=*/NULL);
12325 /* If something went wrong parsing the declarator, make sure
12326 that we at least consume some tokens. */
12327 if (declarator == error_mark_node)
12329 /* Skip to the end of the statement. */
12330 cp_parser_skip_to_end_of_statement (parser);
12331 /* If the next token is not a semicolon, that is
12332 probably because we just skipped over the body of
12333 a function. So, we consume a semicolon if
12334 present, but do not issue an error message if it
12335 is not present. */
12336 if (cp_lexer_next_token_is (parser->lexer,
12337 CPP_SEMICOLON))
12338 cp_lexer_consume_token (parser->lexer);
12339 return;
12342 cp_parser_check_for_definition_in_return_type
12343 (declarator, declares_class_or_enum);
12345 /* Look for an asm-specification. */
12346 asm_specification = cp_parser_asm_specification_opt (parser);
12347 /* Look for attributes that apply to the declaration. */
12348 attributes = cp_parser_attributes_opt (parser);
12349 /* Remember which attributes are prefix attributes and
12350 which are not. */
12351 first_attribute = attributes;
12352 /* Combine the attributes. */
12353 attributes = chainon (prefix_attributes, attributes);
12355 /* If it's an `=', then we have a constant-initializer or a
12356 pure-specifier. It is not correct to parse the
12357 initializer before registering the member declaration
12358 since the member declaration should be in scope while
12359 its initializer is processed. However, the rest of the
12360 front end does not yet provide an interface that allows
12361 us to handle this correctly. */
12362 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12364 /* In [class.mem]:
12366 A pure-specifier shall be used only in the declaration of
12367 a virtual function.
12369 A member-declarator can contain a constant-initializer
12370 only if it declares a static member of integral or
12371 enumeration type.
12373 Therefore, if the DECLARATOR is for a function, we look
12374 for a pure-specifier; otherwise, we look for a
12375 constant-initializer. When we call `grokfield', it will
12376 perform more stringent semantics checks. */
12377 if (TREE_CODE (declarator) == CALL_EXPR)
12378 initializer = cp_parser_pure_specifier (parser);
12379 else
12380 /* Parse the initializer. */
12381 initializer = cp_parser_constant_initializer (parser);
12383 /* Otherwise, there is no initializer. */
12384 else
12385 initializer = NULL_TREE;
12387 /* See if we are probably looking at a function
12388 definition. We are certainly not looking at at a
12389 member-declarator. Calling `grokfield' has
12390 side-effects, so we must not do it unless we are sure
12391 that we are looking at a member-declarator. */
12392 if (cp_parser_token_starts_function_definition_p
12393 (cp_lexer_peek_token (parser->lexer)))
12395 /* The grammar does not allow a pure-specifier to be
12396 used when a member function is defined. (It is
12397 possible that this fact is an oversight in the
12398 standard, since a pure function may be defined
12399 outside of the class-specifier. */
12400 if (initializer)
12401 error ("pure-specifier on function-definition");
12402 decl = cp_parser_save_member_function_body (parser,
12403 decl_specifiers,
12404 declarator,
12405 attributes);
12406 /* If the member was not a friend, declare it here. */
12407 if (!friend_p)
12408 finish_member_declaration (decl);
12409 /* Peek at the next token. */
12410 token = cp_lexer_peek_token (parser->lexer);
12411 /* If the next token is a semicolon, consume it. */
12412 if (token->type == CPP_SEMICOLON)
12413 cp_lexer_consume_token (parser->lexer);
12414 return;
12416 else
12418 /* Create the declaration. */
12419 decl = grokfield (declarator, decl_specifiers,
12420 initializer, asm_specification,
12421 attributes);
12422 /* Any initialization must have been from a
12423 constant-expression. */
12424 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12425 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12429 /* Reset PREFIX_ATTRIBUTES. */
12430 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12431 attributes = TREE_CHAIN (attributes);
12432 if (attributes)
12433 TREE_CHAIN (attributes) = NULL_TREE;
12435 /* If there is any qualification still in effect, clear it
12436 now; we will be starting fresh with the next declarator. */
12437 parser->scope = NULL_TREE;
12438 parser->qualifying_scope = NULL_TREE;
12439 parser->object_scope = NULL_TREE;
12440 /* If it's a `,', then there are more declarators. */
12441 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12442 cp_lexer_consume_token (parser->lexer);
12443 /* If the next token isn't a `;', then we have a parse error. */
12444 else if (cp_lexer_next_token_is_not (parser->lexer,
12445 CPP_SEMICOLON))
12447 cp_parser_error (parser, "expected `;'");
12448 /* Skip tokens until we find a `;'. */
12449 cp_parser_skip_to_end_of_statement (parser);
12451 break;
12454 if (decl)
12456 /* Add DECL to the list of members. */
12457 if (!friend_p)
12458 finish_member_declaration (decl);
12460 if (TREE_CODE (decl) == FUNCTION_DECL)
12461 cp_parser_save_default_args (parser, decl);
12466 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12469 /* Parse a pure-specifier.
12471 pure-specifier:
12474 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12475 Otherwise, ERROR_MARK_NODE is returned. */
12477 static tree
12478 cp_parser_pure_specifier (cp_parser* parser)
12480 cp_token *token;
12482 /* Look for the `=' token. */
12483 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12484 return error_mark_node;
12485 /* Look for the `0' token. */
12486 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12487 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12488 to get information from the lexer about how the number was
12489 spelled in order to fix this problem. */
12490 if (!token || !integer_zerop (token->value))
12491 return error_mark_node;
12493 return integer_zero_node;
12496 /* Parse a constant-initializer.
12498 constant-initializer:
12499 = constant-expression
12501 Returns a representation of the constant-expression. */
12503 static tree
12504 cp_parser_constant_initializer (cp_parser* parser)
12506 /* Look for the `=' token. */
12507 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12508 return error_mark_node;
12510 /* It is invalid to write:
12512 struct S { static const int i = { 7 }; };
12515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12517 cp_parser_error (parser,
12518 "a brace-enclosed initializer is not allowed here");
12519 /* Consume the opening brace. */
12520 cp_lexer_consume_token (parser->lexer);
12521 /* Skip the initializer. */
12522 cp_parser_skip_to_closing_brace (parser);
12523 /* Look for the trailing `}'. */
12524 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12526 return error_mark_node;
12529 return cp_parser_constant_expression (parser,
12530 /*allow_non_constant=*/false,
12531 NULL);
12534 /* Derived classes [gram.class.derived] */
12536 /* Parse a base-clause.
12538 base-clause:
12539 : base-specifier-list
12541 base-specifier-list:
12542 base-specifier
12543 base-specifier-list , base-specifier
12545 Returns a TREE_LIST representing the base-classes, in the order in
12546 which they were declared. The representation of each node is as
12547 described by cp_parser_base_specifier.
12549 In the case that no bases are specified, this function will return
12550 NULL_TREE, not ERROR_MARK_NODE. */
12552 static tree
12553 cp_parser_base_clause (cp_parser* parser)
12555 tree bases = NULL_TREE;
12557 /* Look for the `:' that begins the list. */
12558 cp_parser_require (parser, CPP_COLON, "`:'");
12560 /* Scan the base-specifier-list. */
12561 while (true)
12563 cp_token *token;
12564 tree base;
12566 /* Look for the base-specifier. */
12567 base = cp_parser_base_specifier (parser);
12568 /* Add BASE to the front of the list. */
12569 if (base != error_mark_node)
12571 TREE_CHAIN (base) = bases;
12572 bases = base;
12574 /* Peek at the next token. */
12575 token = cp_lexer_peek_token (parser->lexer);
12576 /* If it's not a comma, then the list is complete. */
12577 if (token->type != CPP_COMMA)
12578 break;
12579 /* Consume the `,'. */
12580 cp_lexer_consume_token (parser->lexer);
12583 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12584 base class had a qualified name. However, the next name that
12585 appears is certainly not qualified. */
12586 parser->scope = NULL_TREE;
12587 parser->qualifying_scope = NULL_TREE;
12588 parser->object_scope = NULL_TREE;
12590 return nreverse (bases);
12593 /* Parse a base-specifier.
12595 base-specifier:
12596 :: [opt] nested-name-specifier [opt] class-name
12597 virtual access-specifier [opt] :: [opt] nested-name-specifier
12598 [opt] class-name
12599 access-specifier virtual [opt] :: [opt] nested-name-specifier
12600 [opt] class-name
12602 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12603 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12604 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12605 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12607 static tree
12608 cp_parser_base_specifier (cp_parser* parser)
12610 cp_token *token;
12611 bool done = false;
12612 bool virtual_p = false;
12613 bool duplicate_virtual_error_issued_p = false;
12614 bool duplicate_access_error_issued_p = false;
12615 bool class_scope_p, template_p;
12616 tree access = access_default_node;
12617 tree type;
12619 /* Process the optional `virtual' and `access-specifier'. */
12620 while (!done)
12622 /* Peek at the next token. */
12623 token = cp_lexer_peek_token (parser->lexer);
12624 /* Process `virtual'. */
12625 switch (token->keyword)
12627 case RID_VIRTUAL:
12628 /* If `virtual' appears more than once, issue an error. */
12629 if (virtual_p && !duplicate_virtual_error_issued_p)
12631 cp_parser_error (parser,
12632 "`virtual' specified more than once in base-specified");
12633 duplicate_virtual_error_issued_p = true;
12636 virtual_p = true;
12638 /* Consume the `virtual' token. */
12639 cp_lexer_consume_token (parser->lexer);
12641 break;
12643 case RID_PUBLIC:
12644 case RID_PROTECTED:
12645 case RID_PRIVATE:
12646 /* If more than one access specifier appears, issue an
12647 error. */
12648 if (access != access_default_node
12649 && !duplicate_access_error_issued_p)
12651 cp_parser_error (parser,
12652 "more than one access specifier in base-specified");
12653 duplicate_access_error_issued_p = true;
12656 access = ridpointers[(int) token->keyword];
12658 /* Consume the access-specifier. */
12659 cp_lexer_consume_token (parser->lexer);
12661 break;
12663 default:
12664 done = true;
12665 break;
12669 /* Look for the optional `::' operator. */
12670 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12671 /* Look for the nested-name-specifier. The simplest way to
12672 implement:
12674 [temp.res]
12676 The keyword `typename' is not permitted in a base-specifier or
12677 mem-initializer; in these contexts a qualified name that
12678 depends on a template-parameter is implicitly assumed to be a
12679 type name.
12681 is to pretend that we have seen the `typename' keyword at this
12682 point. */
12683 cp_parser_nested_name_specifier_opt (parser,
12684 /*typename_keyword_p=*/true,
12685 /*check_dependency_p=*/true,
12686 /*type_p=*/true,
12687 /*is_declaration=*/true);
12688 /* If the base class is given by a qualified name, assume that names
12689 we see are type names or templates, as appropriate. */
12690 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12691 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12693 /* Finally, look for the class-name. */
12694 type = cp_parser_class_name (parser,
12695 class_scope_p,
12696 template_p,
12697 /*type_p=*/true,
12698 /*check_dependency_p=*/true,
12699 /*class_head_p=*/false,
12700 /*is_declaration=*/true);
12702 if (type == error_mark_node)
12703 return error_mark_node;
12705 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12708 /* Exception handling [gram.exception] */
12710 /* Parse an (optional) exception-specification.
12712 exception-specification:
12713 throw ( type-id-list [opt] )
12715 Returns a TREE_LIST representing the exception-specification. The
12716 TREE_VALUE of each node is a type. */
12718 static tree
12719 cp_parser_exception_specification_opt (cp_parser* parser)
12721 cp_token *token;
12722 tree type_id_list;
12724 /* Peek at the next token. */
12725 token = cp_lexer_peek_token (parser->lexer);
12726 /* If it's not `throw', then there's no exception-specification. */
12727 if (!cp_parser_is_keyword (token, RID_THROW))
12728 return NULL_TREE;
12730 /* Consume the `throw'. */
12731 cp_lexer_consume_token (parser->lexer);
12733 /* Look for the `('. */
12734 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12736 /* Peek at the next token. */
12737 token = cp_lexer_peek_token (parser->lexer);
12738 /* If it's not a `)', then there is a type-id-list. */
12739 if (token->type != CPP_CLOSE_PAREN)
12741 const char *saved_message;
12743 /* Types may not be defined in an exception-specification. */
12744 saved_message = parser->type_definition_forbidden_message;
12745 parser->type_definition_forbidden_message
12746 = "types may not be defined in an exception-specification";
12747 /* Parse the type-id-list. */
12748 type_id_list = cp_parser_type_id_list (parser);
12749 /* Restore the saved message. */
12750 parser->type_definition_forbidden_message = saved_message;
12752 else
12753 type_id_list = empty_except_spec;
12755 /* Look for the `)'. */
12756 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12758 return type_id_list;
12761 /* Parse an (optional) type-id-list.
12763 type-id-list:
12764 type-id
12765 type-id-list , type-id
12767 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12768 in the order that the types were presented. */
12770 static tree
12771 cp_parser_type_id_list (cp_parser* parser)
12773 tree types = NULL_TREE;
12775 while (true)
12777 cp_token *token;
12778 tree type;
12780 /* Get the next type-id. */
12781 type = cp_parser_type_id (parser);
12782 /* Add it to the list. */
12783 types = add_exception_specifier (types, type, /*complain=*/1);
12784 /* Peek at the next token. */
12785 token = cp_lexer_peek_token (parser->lexer);
12786 /* If it is not a `,', we are done. */
12787 if (token->type != CPP_COMMA)
12788 break;
12789 /* Consume the `,'. */
12790 cp_lexer_consume_token (parser->lexer);
12793 return nreverse (types);
12796 /* Parse a try-block.
12798 try-block:
12799 try compound-statement handler-seq */
12801 static tree
12802 cp_parser_try_block (cp_parser* parser)
12804 tree try_block;
12806 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12807 try_block = begin_try_block ();
12808 cp_parser_compound_statement (parser, false);
12809 finish_try_block (try_block);
12810 cp_parser_handler_seq (parser);
12811 finish_handler_sequence (try_block);
12813 return try_block;
12816 /* Parse a function-try-block.
12818 function-try-block:
12819 try ctor-initializer [opt] function-body handler-seq */
12821 static bool
12822 cp_parser_function_try_block (cp_parser* parser)
12824 tree try_block;
12825 bool ctor_initializer_p;
12827 /* Look for the `try' keyword. */
12828 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12829 return false;
12830 /* Let the rest of the front-end know where we are. */
12831 try_block = begin_function_try_block ();
12832 /* Parse the function-body. */
12833 ctor_initializer_p
12834 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12835 /* We're done with the `try' part. */
12836 finish_function_try_block (try_block);
12837 /* Parse the handlers. */
12838 cp_parser_handler_seq (parser);
12839 /* We're done with the handlers. */
12840 finish_function_handler_sequence (try_block);
12842 return ctor_initializer_p;
12845 /* Parse a handler-seq.
12847 handler-seq:
12848 handler handler-seq [opt] */
12850 static void
12851 cp_parser_handler_seq (cp_parser* parser)
12853 while (true)
12855 cp_token *token;
12857 /* Parse the handler. */
12858 cp_parser_handler (parser);
12859 /* Peek at the next token. */
12860 token = cp_lexer_peek_token (parser->lexer);
12861 /* If it's not `catch' then there are no more handlers. */
12862 if (!cp_parser_is_keyword (token, RID_CATCH))
12863 break;
12867 /* Parse a handler.
12869 handler:
12870 catch ( exception-declaration ) compound-statement */
12872 static void
12873 cp_parser_handler (cp_parser* parser)
12875 tree handler;
12876 tree declaration;
12878 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12879 handler = begin_handler ();
12880 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12881 declaration = cp_parser_exception_declaration (parser);
12882 finish_handler_parms (declaration, handler);
12883 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12884 cp_parser_compound_statement (parser, false);
12885 finish_handler (handler);
12888 /* Parse an exception-declaration.
12890 exception-declaration:
12891 type-specifier-seq declarator
12892 type-specifier-seq abstract-declarator
12893 type-specifier-seq
12894 ...
12896 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12897 ellipsis variant is used. */
12899 static tree
12900 cp_parser_exception_declaration (cp_parser* parser)
12902 tree type_specifiers;
12903 tree declarator;
12904 const char *saved_message;
12906 /* If it's an ellipsis, it's easy to handle. */
12907 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12909 /* Consume the `...' token. */
12910 cp_lexer_consume_token (parser->lexer);
12911 return NULL_TREE;
12914 /* Types may not be defined in exception-declarations. */
12915 saved_message = parser->type_definition_forbidden_message;
12916 parser->type_definition_forbidden_message
12917 = "types may not be defined in exception-declarations";
12919 /* Parse the type-specifier-seq. */
12920 type_specifiers = cp_parser_type_specifier_seq (parser);
12921 /* If it's a `)', then there is no declarator. */
12922 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12923 declarator = NULL_TREE;
12924 else
12925 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12926 /*ctor_dtor_or_conv_p=*/NULL,
12927 /*parenthesized_p=*/NULL);
12929 /* Restore the saved message. */
12930 parser->type_definition_forbidden_message = saved_message;
12932 return start_handler_parms (type_specifiers, declarator);
12935 /* Parse a throw-expression.
12937 throw-expression:
12938 throw assignment-expression [opt]
12940 Returns a THROW_EXPR representing the throw-expression. */
12942 static tree
12943 cp_parser_throw_expression (cp_parser* parser)
12945 tree expression;
12946 cp_token* token;
12948 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12949 token = cp_lexer_peek_token (parser->lexer);
12950 /* Figure out whether or not there is an assignment-expression
12951 following the "throw" keyword. */
12952 if (token->type == CPP_COMMA
12953 || token->type == CPP_SEMICOLON
12954 || token->type == CPP_CLOSE_PAREN
12955 || token->type == CPP_CLOSE_SQUARE
12956 || token->type == CPP_CLOSE_BRACE
12957 || token->type == CPP_COLON)
12958 expression = NULL_TREE;
12959 else
12960 expression = cp_parser_assignment_expression (parser);
12962 return build_throw (expression);
12965 /* GNU Extensions */
12967 /* Parse an (optional) asm-specification.
12969 asm-specification:
12970 asm ( string-literal )
12972 If the asm-specification is present, returns a STRING_CST
12973 corresponding to the string-literal. Otherwise, returns
12974 NULL_TREE. */
12976 static tree
12977 cp_parser_asm_specification_opt (cp_parser* parser)
12979 cp_token *token;
12980 tree asm_specification;
12982 /* Peek at the next token. */
12983 token = cp_lexer_peek_token (parser->lexer);
12984 /* If the next token isn't the `asm' keyword, then there's no
12985 asm-specification. */
12986 if (!cp_parser_is_keyword (token, RID_ASM))
12987 return NULL_TREE;
12989 /* Consume the `asm' token. */
12990 cp_lexer_consume_token (parser->lexer);
12991 /* Look for the `('. */
12992 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12994 /* Look for the string-literal. */
12995 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12996 if (token)
12997 asm_specification = token->value;
12998 else
12999 asm_specification = NULL_TREE;
13001 /* Look for the `)'. */
13002 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13004 return asm_specification;
13007 /* Parse an asm-operand-list.
13009 asm-operand-list:
13010 asm-operand
13011 asm-operand-list , asm-operand
13013 asm-operand:
13014 string-literal ( expression )
13015 [ string-literal ] string-literal ( expression )
13017 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13018 each node is the expression. The TREE_PURPOSE is itself a
13019 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13020 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13021 is a STRING_CST for the string literal before the parenthesis. */
13023 static tree
13024 cp_parser_asm_operand_list (cp_parser* parser)
13026 tree asm_operands = NULL_TREE;
13028 while (true)
13030 tree string_literal;
13031 tree expression;
13032 tree name;
13033 cp_token *token;
13035 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13037 /* Consume the `[' token. */
13038 cp_lexer_consume_token (parser->lexer);
13039 /* Read the operand name. */
13040 name = cp_parser_identifier (parser);
13041 if (name != error_mark_node)
13042 name = build_string (IDENTIFIER_LENGTH (name),
13043 IDENTIFIER_POINTER (name));
13044 /* Look for the closing `]'. */
13045 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13047 else
13048 name = NULL_TREE;
13049 /* Look for the string-literal. */
13050 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13051 string_literal = token ? token->value : error_mark_node;
13052 /* Look for the `('. */
13053 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13054 /* Parse the expression. */
13055 expression = cp_parser_expression (parser);
13056 /* Look for the `)'. */
13057 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13058 /* Add this operand to the list. */
13059 asm_operands = tree_cons (build_tree_list (name, string_literal),
13060 expression,
13061 asm_operands);
13062 /* If the next token is not a `,', there are no more
13063 operands. */
13064 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13065 break;
13066 /* Consume the `,'. */
13067 cp_lexer_consume_token (parser->lexer);
13070 return nreverse (asm_operands);
13073 /* Parse an asm-clobber-list.
13075 asm-clobber-list:
13076 string-literal
13077 asm-clobber-list , string-literal
13079 Returns a TREE_LIST, indicating the clobbers in the order that they
13080 appeared. The TREE_VALUE of each node is a STRING_CST. */
13082 static tree
13083 cp_parser_asm_clobber_list (cp_parser* parser)
13085 tree clobbers = NULL_TREE;
13087 while (true)
13089 cp_token *token;
13090 tree string_literal;
13092 /* Look for the string literal. */
13093 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13094 string_literal = token ? token->value : error_mark_node;
13095 /* Add it to the list. */
13096 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13097 /* If the next token is not a `,', then the list is
13098 complete. */
13099 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13100 break;
13101 /* Consume the `,' token. */
13102 cp_lexer_consume_token (parser->lexer);
13105 return clobbers;
13108 /* Parse an (optional) series of attributes.
13110 attributes:
13111 attributes attribute
13113 attribute:
13114 __attribute__ (( attribute-list [opt] ))
13116 The return value is as for cp_parser_attribute_list. */
13118 static tree
13119 cp_parser_attributes_opt (cp_parser* parser)
13121 tree attributes = NULL_TREE;
13123 while (true)
13125 cp_token *token;
13126 tree attribute_list;
13128 /* Peek at the next token. */
13129 token = cp_lexer_peek_token (parser->lexer);
13130 /* If it's not `__attribute__', then we're done. */
13131 if (token->keyword != RID_ATTRIBUTE)
13132 break;
13134 /* Consume the `__attribute__' keyword. */
13135 cp_lexer_consume_token (parser->lexer);
13136 /* Look for the two `(' tokens. */
13137 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13138 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13140 /* Peek at the next token. */
13141 token = cp_lexer_peek_token (parser->lexer);
13142 if (token->type != CPP_CLOSE_PAREN)
13143 /* Parse the attribute-list. */
13144 attribute_list = cp_parser_attribute_list (parser);
13145 else
13146 /* If the next token is a `)', then there is no attribute
13147 list. */
13148 attribute_list = NULL;
13150 /* Look for the two `)' tokens. */
13151 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13152 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13154 /* Add these new attributes to the list. */
13155 attributes = chainon (attributes, attribute_list);
13158 return attributes;
13161 /* Parse an attribute-list.
13163 attribute-list:
13164 attribute
13165 attribute-list , attribute
13167 attribute:
13168 identifier
13169 identifier ( identifier )
13170 identifier ( identifier , expression-list )
13171 identifier ( expression-list )
13173 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13174 TREE_PURPOSE of each node is the identifier indicating which
13175 attribute is in use. The TREE_VALUE represents the arguments, if
13176 any. */
13178 static tree
13179 cp_parser_attribute_list (cp_parser* parser)
13181 tree attribute_list = NULL_TREE;
13183 while (true)
13185 cp_token *token;
13186 tree identifier;
13187 tree attribute;
13189 /* Look for the identifier. We also allow keywords here; for
13190 example `__attribute__ ((const))' is legal. */
13191 token = cp_lexer_peek_token (parser->lexer);
13192 if (token->type != CPP_NAME
13193 && token->type != CPP_KEYWORD)
13194 return error_mark_node;
13195 /* Consume the token. */
13196 token = cp_lexer_consume_token (parser->lexer);
13198 /* Save away the identifier that indicates which attribute this is. */
13199 identifier = token->value;
13200 attribute = build_tree_list (identifier, NULL_TREE);
13202 /* Peek at the next token. */
13203 token = cp_lexer_peek_token (parser->lexer);
13204 /* If it's an `(', then parse the attribute arguments. */
13205 if (token->type == CPP_OPEN_PAREN)
13207 tree arguments;
13209 arguments = (cp_parser_parenthesized_expression_list
13210 (parser, true, /*non_constant_p=*/NULL));
13211 /* Save the identifier and arguments away. */
13212 TREE_VALUE (attribute) = arguments;
13215 /* Add this attribute to the list. */
13216 TREE_CHAIN (attribute) = attribute_list;
13217 attribute_list = attribute;
13219 /* Now, look for more attributes. */
13220 token = cp_lexer_peek_token (parser->lexer);
13221 /* If the next token isn't a `,', we're done. */
13222 if (token->type != CPP_COMMA)
13223 break;
13225 /* Consume the comma and keep going. */
13226 cp_lexer_consume_token (parser->lexer);
13229 /* We built up the list in reverse order. */
13230 return nreverse (attribute_list);
13233 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13234 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13235 current value of the PEDANTIC flag, regardless of whether or not
13236 the `__extension__' keyword is present. The caller is responsible
13237 for restoring the value of the PEDANTIC flag. */
13239 static bool
13240 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13242 /* Save the old value of the PEDANTIC flag. */
13243 *saved_pedantic = pedantic;
13245 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13247 /* Consume the `__extension__' token. */
13248 cp_lexer_consume_token (parser->lexer);
13249 /* We're not being pedantic while the `__extension__' keyword is
13250 in effect. */
13251 pedantic = 0;
13253 return true;
13256 return false;
13259 /* Parse a label declaration.
13261 label-declaration:
13262 __label__ label-declarator-seq ;
13264 label-declarator-seq:
13265 identifier , label-declarator-seq
13266 identifier */
13268 static void
13269 cp_parser_label_declaration (cp_parser* parser)
13271 /* Look for the `__label__' keyword. */
13272 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13274 while (true)
13276 tree identifier;
13278 /* Look for an identifier. */
13279 identifier = cp_parser_identifier (parser);
13280 /* Declare it as a lobel. */
13281 finish_label_decl (identifier);
13282 /* If the next token is a `;', stop. */
13283 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13284 break;
13285 /* Look for the `,' separating the label declarations. */
13286 cp_parser_require (parser, CPP_COMMA, "`,'");
13289 /* Look for the final `;'. */
13290 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13293 /* Support Functions */
13295 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13296 NAME should have one of the representations used for an
13297 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13298 is returned. If PARSER->SCOPE is a dependent type, then a
13299 SCOPE_REF is returned.
13301 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13302 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13303 was formed. Abstractly, such entities should not be passed to this
13304 function, because they do not need to be looked up, but it is
13305 simpler to check for this special case here, rather than at the
13306 call-sites.
13308 In cases not explicitly covered above, this function returns a
13309 DECL, OVERLOAD, or baselink representing the result of the lookup.
13310 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13311 is returned.
13313 If IS_TYPE is TRUE, bindings that do not refer to types are
13314 ignored.
13316 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13317 ignored.
13319 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13320 are ignored.
13322 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13323 types. */
13325 static tree
13326 cp_parser_lookup_name (cp_parser *parser, tree name,
13327 bool is_type, bool is_template, bool is_namespace,
13328 bool check_dependency)
13330 tree decl;
13331 tree object_type = parser->context->object_type;
13333 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13334 no longer valid. Note that if we are parsing tentatively, and
13335 the parse fails, OBJECT_TYPE will be automatically restored. */
13336 parser->context->object_type = NULL_TREE;
13338 if (name == error_mark_node)
13339 return error_mark_node;
13341 /* A template-id has already been resolved; there is no lookup to
13342 do. */
13343 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13344 return name;
13345 if (BASELINK_P (name))
13347 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13348 == TEMPLATE_ID_EXPR),
13349 20020909);
13350 return name;
13353 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13354 it should already have been checked to make sure that the name
13355 used matches the type being destroyed. */
13356 if (TREE_CODE (name) == BIT_NOT_EXPR)
13358 tree type;
13360 /* Figure out to which type this destructor applies. */
13361 if (parser->scope)
13362 type = parser->scope;
13363 else if (object_type)
13364 type = object_type;
13365 else
13366 type = current_class_type;
13367 /* If that's not a class type, there is no destructor. */
13368 if (!type || !CLASS_TYPE_P (type))
13369 return error_mark_node;
13370 /* If it was a class type, return the destructor. */
13371 return CLASSTYPE_DESTRUCTORS (type);
13374 /* By this point, the NAME should be an ordinary identifier. If
13375 the id-expression was a qualified name, the qualifying scope is
13376 stored in PARSER->SCOPE at this point. */
13377 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13378 20000619);
13380 /* Perform the lookup. */
13381 if (parser->scope)
13383 bool dependent_p;
13385 if (parser->scope == error_mark_node)
13386 return error_mark_node;
13388 /* If the SCOPE is dependent, the lookup must be deferred until
13389 the template is instantiated -- unless we are explicitly
13390 looking up names in uninstantiated templates. Even then, we
13391 cannot look up the name if the scope is not a class type; it
13392 might, for example, be a template type parameter. */
13393 dependent_p = (TYPE_P (parser->scope)
13394 && !(parser->in_declarator_p
13395 && currently_open_class (parser->scope))
13396 && dependent_type_p (parser->scope));
13397 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13398 && dependent_p)
13400 if (is_type)
13401 /* The resolution to Core Issue 180 says that `struct A::B'
13402 should be considered a type-name, even if `A' is
13403 dependent. */
13404 decl = TYPE_NAME (make_typename_type (parser->scope,
13405 name,
13406 /*complain=*/1));
13407 else if (is_template)
13408 decl = make_unbound_class_template (parser->scope,
13409 name,
13410 /*complain=*/1);
13411 else
13412 decl = build_nt (SCOPE_REF, parser->scope, name);
13414 else
13416 /* If PARSER->SCOPE is a dependent type, then it must be a
13417 class type, and we must not be checking dependencies;
13418 otherwise, we would have processed this lookup above. So
13419 that PARSER->SCOPE is not considered a dependent base by
13420 lookup_member, we must enter the scope here. */
13421 if (dependent_p)
13422 push_scope (parser->scope);
13423 /* If the PARSER->SCOPE is a a template specialization, it
13424 may be instantiated during name lookup. In that case,
13425 errors may be issued. Even if we rollback the current
13426 tentative parse, those errors are valid. */
13427 decl = lookup_qualified_name (parser->scope, name, is_type,
13428 /*complain=*/true);
13429 if (dependent_p)
13430 pop_scope (parser->scope);
13432 parser->qualifying_scope = parser->scope;
13433 parser->object_scope = NULL_TREE;
13435 else if (object_type)
13437 tree object_decl = NULL_TREE;
13438 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13439 OBJECT_TYPE is not a class. */
13440 if (CLASS_TYPE_P (object_type))
13441 /* If the OBJECT_TYPE is a template specialization, it may
13442 be instantiated during name lookup. In that case, errors
13443 may be issued. Even if we rollback the current tentative
13444 parse, those errors are valid. */
13445 object_decl = lookup_member (object_type,
13446 name,
13447 /*protect=*/0, is_type);
13448 /* Look it up in the enclosing context, too. */
13449 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13450 is_namespace,
13451 /*flags=*/0);
13452 parser->object_scope = object_type;
13453 parser->qualifying_scope = NULL_TREE;
13454 if (object_decl)
13455 decl = object_decl;
13457 else
13459 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13460 is_namespace,
13461 /*flags=*/0);
13462 parser->qualifying_scope = NULL_TREE;
13463 parser->object_scope = NULL_TREE;
13466 /* If the lookup failed, let our caller know. */
13467 if (!decl
13468 || decl == error_mark_node
13469 || (TREE_CODE (decl) == FUNCTION_DECL
13470 && DECL_ANTICIPATED (decl)))
13471 return error_mark_node;
13473 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13474 if (TREE_CODE (decl) == TREE_LIST)
13476 /* The error message we have to print is too complicated for
13477 cp_parser_error, so we incorporate its actions directly. */
13478 if (!cp_parser_simulate_error (parser))
13480 error ("reference to `%D' is ambiguous", name);
13481 print_candidates (decl);
13483 return error_mark_node;
13486 my_friendly_assert (DECL_P (decl)
13487 || TREE_CODE (decl) == OVERLOAD
13488 || TREE_CODE (decl) == SCOPE_REF
13489 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13490 || BASELINK_P (decl),
13491 20000619);
13493 /* If we have resolved the name of a member declaration, check to
13494 see if the declaration is accessible. When the name resolves to
13495 set of overloaded functions, accessibility is checked when
13496 overload resolution is done.
13498 During an explicit instantiation, access is not checked at all,
13499 as per [temp.explicit]. */
13500 if (DECL_P (decl))
13501 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13503 return decl;
13506 /* Like cp_parser_lookup_name, but for use in the typical case where
13507 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13508 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13510 static tree
13511 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13513 return cp_parser_lookup_name (parser, name,
13514 /*is_type=*/false,
13515 /*is_template=*/false,
13516 /*is_namespace=*/false,
13517 /*check_dependency=*/true);
13520 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13521 the current context, return the TYPE_DECL. If TAG_NAME_P is
13522 true, the DECL indicates the class being defined in a class-head,
13523 or declared in an elaborated-type-specifier.
13525 Otherwise, return DECL. */
13527 static tree
13528 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13530 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13531 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13533 struct A {
13534 template <typename T> struct B;
13537 template <typename T> struct A::B {};
13539 Similarly, in a elaborated-type-specifier:
13541 namespace N { struct X{}; }
13543 struct A {
13544 template <typename T> friend struct N::X;
13547 However, if the DECL refers to a class type, and we are in
13548 the scope of the class, then the name lookup automatically
13549 finds the TYPE_DECL created by build_self_reference rather
13550 than a TEMPLATE_DECL. For example, in:
13552 template <class T> struct S {
13553 S s;
13556 there is no need to handle such case. */
13558 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13559 return DECL_TEMPLATE_RESULT (decl);
13561 return decl;
13564 /* If too many, or too few, template-parameter lists apply to the
13565 declarator, issue an error message. Returns TRUE if all went well,
13566 and FALSE otherwise. */
13568 static bool
13569 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13570 tree declarator)
13572 unsigned num_templates;
13574 /* We haven't seen any classes that involve template parameters yet. */
13575 num_templates = 0;
13577 switch (TREE_CODE (declarator))
13579 case CALL_EXPR:
13580 case ARRAY_REF:
13581 case INDIRECT_REF:
13582 case ADDR_EXPR:
13584 tree main_declarator = TREE_OPERAND (declarator, 0);
13585 return
13586 cp_parser_check_declarator_template_parameters (parser,
13587 main_declarator);
13590 case SCOPE_REF:
13592 tree scope;
13593 tree member;
13595 scope = TREE_OPERAND (declarator, 0);
13596 member = TREE_OPERAND (declarator, 1);
13598 /* If this is a pointer-to-member, then we are not interested
13599 in the SCOPE, because it does not qualify the thing that is
13600 being declared. */
13601 if (TREE_CODE (member) == INDIRECT_REF)
13602 return (cp_parser_check_declarator_template_parameters
13603 (parser, member));
13605 while (scope && CLASS_TYPE_P (scope))
13607 /* You're supposed to have one `template <...>'
13608 for every template class, but you don't need one
13609 for a full specialization. For example:
13611 template <class T> struct S{};
13612 template <> struct S<int> { void f(); };
13613 void S<int>::f () {}
13615 is correct; there shouldn't be a `template <>' for
13616 the definition of `S<int>::f'. */
13617 if (CLASSTYPE_TEMPLATE_INFO (scope)
13618 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13619 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13620 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13621 ++num_templates;
13623 scope = TYPE_CONTEXT (scope);
13627 /* Fall through. */
13629 default:
13630 /* If the DECLARATOR has the form `X<y>' then it uses one
13631 additional level of template parameters. */
13632 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13633 ++num_templates;
13635 return cp_parser_check_template_parameters (parser,
13636 num_templates);
13640 /* NUM_TEMPLATES were used in the current declaration. If that is
13641 invalid, return FALSE and issue an error messages. Otherwise,
13642 return TRUE. */
13644 static bool
13645 cp_parser_check_template_parameters (cp_parser* parser,
13646 unsigned num_templates)
13648 /* If there are more template classes than parameter lists, we have
13649 something like:
13651 template <class T> void S<T>::R<T>::f (); */
13652 if (parser->num_template_parameter_lists < num_templates)
13654 error ("too few template-parameter-lists");
13655 return false;
13657 /* If there are the same number of template classes and parameter
13658 lists, that's OK. */
13659 if (parser->num_template_parameter_lists == num_templates)
13660 return true;
13661 /* If there are more, but only one more, then we are referring to a
13662 member template. That's OK too. */
13663 if (parser->num_template_parameter_lists == num_templates + 1)
13664 return true;
13665 /* Otherwise, there are too many template parameter lists. We have
13666 something like:
13668 template <class T> template <class U> void S::f(); */
13669 error ("too many template-parameter-lists");
13670 return false;
13673 /* Parse a binary-expression of the general form:
13675 binary-expression:
13676 <expr>
13677 binary-expression <token> <expr>
13679 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13680 to parser the <expr>s. If the first production is used, then the
13681 value returned by FN is returned directly. Otherwise, a node with
13682 the indicated EXPR_TYPE is returned, with operands corresponding to
13683 the two sub-expressions. */
13685 static tree
13686 cp_parser_binary_expression (cp_parser* parser,
13687 const cp_parser_token_tree_map token_tree_map,
13688 cp_parser_expression_fn fn)
13690 tree lhs;
13692 /* Parse the first expression. */
13693 lhs = (*fn) (parser);
13694 /* Now, look for more expressions. */
13695 while (true)
13697 cp_token *token;
13698 const cp_parser_token_tree_map_node *map_node;
13699 tree rhs;
13701 /* Peek at the next token. */
13702 token = cp_lexer_peek_token (parser->lexer);
13703 /* If the token is `>', and that's not an operator at the
13704 moment, then we're done. */
13705 if (token->type == CPP_GREATER
13706 && !parser->greater_than_is_operator_p)
13707 break;
13708 /* If we find one of the tokens we want, build the corresponding
13709 tree representation. */
13710 for (map_node = token_tree_map;
13711 map_node->token_type != CPP_EOF;
13712 ++map_node)
13713 if (map_node->token_type == token->type)
13715 /* Consume the operator token. */
13716 cp_lexer_consume_token (parser->lexer);
13717 /* Parse the right-hand side of the expression. */
13718 rhs = (*fn) (parser);
13719 /* Build the binary tree node. */
13720 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13721 break;
13724 /* If the token wasn't one of the ones we want, we're done. */
13725 if (map_node->token_type == CPP_EOF)
13726 break;
13729 return lhs;
13732 /* Parse an optional `::' token indicating that the following name is
13733 from the global namespace. If so, PARSER->SCOPE is set to the
13734 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13735 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13736 Returns the new value of PARSER->SCOPE, if the `::' token is
13737 present, and NULL_TREE otherwise. */
13739 static tree
13740 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13742 cp_token *token;
13744 /* Peek at the next token. */
13745 token = cp_lexer_peek_token (parser->lexer);
13746 /* If we're looking at a `::' token then we're starting from the
13747 global namespace, not our current location. */
13748 if (token->type == CPP_SCOPE)
13750 /* Consume the `::' token. */
13751 cp_lexer_consume_token (parser->lexer);
13752 /* Set the SCOPE so that we know where to start the lookup. */
13753 parser->scope = global_namespace;
13754 parser->qualifying_scope = global_namespace;
13755 parser->object_scope = NULL_TREE;
13757 return parser->scope;
13759 else if (!current_scope_valid_p)
13761 parser->scope = NULL_TREE;
13762 parser->qualifying_scope = NULL_TREE;
13763 parser->object_scope = NULL_TREE;
13766 return NULL_TREE;
13769 /* Returns TRUE if the upcoming token sequence is the start of a
13770 constructor declarator. If FRIEND_P is true, the declarator is
13771 preceded by the `friend' specifier. */
13773 static bool
13774 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13776 bool constructor_p;
13777 tree type_decl = NULL_TREE;
13778 bool nested_name_p;
13779 cp_token *next_token;
13781 /* The common case is that this is not a constructor declarator, so
13782 try to avoid doing lots of work if at all possible. It's not
13783 valid declare a constructor at function scope. */
13784 if (at_function_scope_p ())
13785 return false;
13786 /* And only certain tokens can begin a constructor declarator. */
13787 next_token = cp_lexer_peek_token (parser->lexer);
13788 if (next_token->type != CPP_NAME
13789 && next_token->type != CPP_SCOPE
13790 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13791 && next_token->type != CPP_TEMPLATE_ID)
13792 return false;
13794 /* Parse tentatively; we are going to roll back all of the tokens
13795 consumed here. */
13796 cp_parser_parse_tentatively (parser);
13797 /* Assume that we are looking at a constructor declarator. */
13798 constructor_p = true;
13800 /* Look for the optional `::' operator. */
13801 cp_parser_global_scope_opt (parser,
13802 /*current_scope_valid_p=*/false);
13803 /* Look for the nested-name-specifier. */
13804 nested_name_p
13805 = (cp_parser_nested_name_specifier_opt (parser,
13806 /*typename_keyword_p=*/false,
13807 /*check_dependency_p=*/false,
13808 /*type_p=*/false,
13809 /*is_declaration=*/false)
13810 != NULL_TREE);
13811 /* Outside of a class-specifier, there must be a
13812 nested-name-specifier. */
13813 if (!nested_name_p &&
13814 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13815 || friend_p))
13816 constructor_p = false;
13817 /* If we still think that this might be a constructor-declarator,
13818 look for a class-name. */
13819 if (constructor_p)
13821 /* If we have:
13823 template <typename T> struct S { S(); };
13824 template <typename T> S<T>::S ();
13826 we must recognize that the nested `S' names a class.
13827 Similarly, for:
13829 template <typename T> S<T>::S<T> ();
13831 we must recognize that the nested `S' names a template. */
13832 type_decl = cp_parser_class_name (parser,
13833 /*typename_keyword_p=*/false,
13834 /*template_keyword_p=*/false,
13835 /*type_p=*/false,
13836 /*check_dependency_p=*/false,
13837 /*class_head_p=*/false,
13838 /*is_declaration=*/false);
13839 /* If there was no class-name, then this is not a constructor. */
13840 constructor_p = !cp_parser_error_occurred (parser);
13843 /* If we're still considering a constructor, we have to see a `(',
13844 to begin the parameter-declaration-clause, followed by either a
13845 `)', an `...', or a decl-specifier. We need to check for a
13846 type-specifier to avoid being fooled into thinking that:
13848 S::S (f) (int);
13850 is a constructor. (It is actually a function named `f' that
13851 takes one parameter (of type `int') and returns a value of type
13852 `S::S'. */
13853 if (constructor_p
13854 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13856 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13857 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13858 && !cp_parser_storage_class_specifier_opt (parser))
13860 tree type;
13861 unsigned saved_num_template_parameter_lists;
13863 /* Names appearing in the type-specifier should be looked up
13864 in the scope of the class. */
13865 if (current_class_type)
13866 type = NULL_TREE;
13867 else
13869 type = TREE_TYPE (type_decl);
13870 if (TREE_CODE (type) == TYPENAME_TYPE)
13872 type = resolve_typename_type (type,
13873 /*only_current_p=*/false);
13874 if (type == error_mark_node)
13876 cp_parser_abort_tentative_parse (parser);
13877 return false;
13880 push_scope (type);
13883 /* Inside the constructor parameter list, surrounding
13884 template-parameter-lists do not apply. */
13885 saved_num_template_parameter_lists
13886 = parser->num_template_parameter_lists;
13887 parser->num_template_parameter_lists = 0;
13889 /* Look for the type-specifier. */
13890 cp_parser_type_specifier (parser,
13891 CP_PARSER_FLAGS_NONE,
13892 /*is_friend=*/false,
13893 /*is_declarator=*/true,
13894 /*declares_class_or_enum=*/NULL,
13895 /*is_cv_qualifier=*/NULL);
13897 parser->num_template_parameter_lists
13898 = saved_num_template_parameter_lists;
13900 /* Leave the scope of the class. */
13901 if (type)
13902 pop_scope (type);
13904 constructor_p = !cp_parser_error_occurred (parser);
13907 else
13908 constructor_p = false;
13909 /* We did not really want to consume any tokens. */
13910 cp_parser_abort_tentative_parse (parser);
13912 return constructor_p;
13915 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13916 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13917 they must be performed once we are in the scope of the function.
13919 Returns the function defined. */
13921 static tree
13922 cp_parser_function_definition_from_specifiers_and_declarator
13923 (cp_parser* parser,
13924 tree decl_specifiers,
13925 tree attributes,
13926 tree declarator)
13928 tree fn;
13929 bool success_p;
13931 /* Begin the function-definition. */
13932 success_p = begin_function_definition (decl_specifiers,
13933 attributes,
13934 declarator);
13936 /* If there were names looked up in the decl-specifier-seq that we
13937 did not check, check them now. We must wait until we are in the
13938 scope of the function to perform the checks, since the function
13939 might be a friend. */
13940 perform_deferred_access_checks ();
13942 if (!success_p)
13944 /* If begin_function_definition didn't like the definition, skip
13945 the entire function. */
13946 error ("invalid function declaration");
13947 cp_parser_skip_to_end_of_block_or_statement (parser);
13948 fn = error_mark_node;
13950 else
13951 fn = cp_parser_function_definition_after_declarator (parser,
13952 /*inline_p=*/false);
13954 return fn;
13957 /* Parse the part of a function-definition that follows the
13958 declarator. INLINE_P is TRUE iff this function is an inline
13959 function defined with a class-specifier.
13961 Returns the function defined. */
13963 static tree
13964 cp_parser_function_definition_after_declarator (cp_parser* parser,
13965 bool inline_p)
13967 tree fn;
13968 bool ctor_initializer_p = false;
13969 bool saved_in_unbraced_linkage_specification_p;
13970 unsigned saved_num_template_parameter_lists;
13972 /* If the next token is `return', then the code may be trying to
13973 make use of the "named return value" extension that G++ used to
13974 support. */
13975 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13977 /* Consume the `return' keyword. */
13978 cp_lexer_consume_token (parser->lexer);
13979 /* Look for the identifier that indicates what value is to be
13980 returned. */
13981 cp_parser_identifier (parser);
13982 /* Issue an error message. */
13983 error ("named return values are no longer supported");
13984 /* Skip tokens until we reach the start of the function body. */
13985 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13986 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
13987 cp_lexer_consume_token (parser->lexer);
13989 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13990 anything declared inside `f'. */
13991 saved_in_unbraced_linkage_specification_p
13992 = parser->in_unbraced_linkage_specification_p;
13993 parser->in_unbraced_linkage_specification_p = false;
13994 /* Inside the function, surrounding template-parameter-lists do not
13995 apply. */
13996 saved_num_template_parameter_lists
13997 = parser->num_template_parameter_lists;
13998 parser->num_template_parameter_lists = 0;
13999 /* If the next token is `try', then we are looking at a
14000 function-try-block. */
14001 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14002 ctor_initializer_p = cp_parser_function_try_block (parser);
14003 /* A function-try-block includes the function-body, so we only do
14004 this next part if we're not processing a function-try-block. */
14005 else
14006 ctor_initializer_p
14007 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14009 /* Finish the function. */
14010 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14011 (inline_p ? 2 : 0));
14012 /* Generate code for it, if necessary. */
14013 expand_or_defer_fn (fn);
14014 /* Restore the saved values. */
14015 parser->in_unbraced_linkage_specification_p
14016 = saved_in_unbraced_linkage_specification_p;
14017 parser->num_template_parameter_lists
14018 = saved_num_template_parameter_lists;
14020 return fn;
14023 /* Parse a template-declaration, assuming that the `export' (and
14024 `extern') keywords, if present, has already been scanned. MEMBER_P
14025 is as for cp_parser_template_declaration. */
14027 static void
14028 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14030 tree decl = NULL_TREE;
14031 tree parameter_list;
14032 bool friend_p = false;
14034 /* Look for the `template' keyword. */
14035 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14036 return;
14038 /* And the `<'. */
14039 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14040 return;
14042 /* If the next token is `>', then we have an invalid
14043 specialization. Rather than complain about an invalid template
14044 parameter, issue an error message here. */
14045 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14047 cp_parser_error (parser, "invalid explicit specialization");
14048 begin_specialization ();
14049 parameter_list = NULL_TREE;
14051 else
14053 /* Parse the template parameters. */
14054 begin_template_parm_list ();
14055 parameter_list = cp_parser_template_parameter_list (parser);
14056 parameter_list = end_template_parm_list (parameter_list);
14059 /* Look for the `>'. */
14060 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14061 /* We just processed one more parameter list. */
14062 ++parser->num_template_parameter_lists;
14063 /* If the next token is `template', there are more template
14064 parameters. */
14065 if (cp_lexer_next_token_is_keyword (parser->lexer,
14066 RID_TEMPLATE))
14067 cp_parser_template_declaration_after_export (parser, member_p);
14068 else
14070 decl = cp_parser_single_declaration (parser,
14071 member_p,
14072 &friend_p);
14074 /* If this is a member template declaration, let the front
14075 end know. */
14076 if (member_p && !friend_p && decl)
14078 if (TREE_CODE (decl) == TYPE_DECL)
14079 cp_parser_check_access_in_redeclaration (decl);
14081 decl = finish_member_template_decl (decl);
14083 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14084 make_friend_class (current_class_type, TREE_TYPE (decl),
14085 /*complain=*/true);
14087 /* We are done with the current parameter list. */
14088 --parser->num_template_parameter_lists;
14090 /* Finish up. */
14091 finish_template_decl (parameter_list);
14093 /* Register member declarations. */
14094 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14095 finish_member_declaration (decl);
14097 /* If DECL is a function template, we must return to parse it later.
14098 (Even though there is no definition, there might be default
14099 arguments that need handling.) */
14100 if (member_p && decl
14101 && (TREE_CODE (decl) == FUNCTION_DECL
14102 || DECL_FUNCTION_TEMPLATE_P (decl)))
14103 TREE_VALUE (parser->unparsed_functions_queues)
14104 = tree_cons (NULL_TREE, decl,
14105 TREE_VALUE (parser->unparsed_functions_queues));
14108 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14109 `function-definition' sequence. MEMBER_P is true, this declaration
14110 appears in a class scope.
14112 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14113 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14115 static tree
14116 cp_parser_single_declaration (cp_parser* parser,
14117 bool member_p,
14118 bool* friend_p)
14120 int declares_class_or_enum;
14121 tree decl = NULL_TREE;
14122 tree decl_specifiers;
14123 tree attributes;
14124 bool function_definition_p = false;
14126 /* Defer access checks until we know what is being declared. */
14127 push_deferring_access_checks (dk_deferred);
14129 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14130 alternative. */
14131 decl_specifiers
14132 = cp_parser_decl_specifier_seq (parser,
14133 CP_PARSER_FLAGS_OPTIONAL,
14134 &attributes,
14135 &declares_class_or_enum);
14136 if (friend_p)
14137 *friend_p = cp_parser_friend_p (decl_specifiers);
14138 /* Gather up the access checks that occurred the
14139 decl-specifier-seq. */
14140 stop_deferring_access_checks ();
14142 /* Check for the declaration of a template class. */
14143 if (declares_class_or_enum)
14145 if (cp_parser_declares_only_class_p (parser))
14147 decl = shadow_tag (decl_specifiers);
14148 if (decl)
14149 decl = TYPE_NAME (decl);
14150 else
14151 decl = error_mark_node;
14154 else
14155 decl = NULL_TREE;
14156 /* If it's not a template class, try for a template function. If
14157 the next token is a `;', then this declaration does not declare
14158 anything. But, if there were errors in the decl-specifiers, then
14159 the error might well have come from an attempted class-specifier.
14160 In that case, there's no need to warn about a missing declarator. */
14161 if (!decl
14162 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14163 || !value_member (error_mark_node, decl_specifiers)))
14164 decl = cp_parser_init_declarator (parser,
14165 decl_specifiers,
14166 attributes,
14167 /*function_definition_allowed_p=*/true,
14168 member_p,
14169 declares_class_or_enum,
14170 &function_definition_p);
14172 pop_deferring_access_checks ();
14174 /* Clear any current qualification; whatever comes next is the start
14175 of something new. */
14176 parser->scope = NULL_TREE;
14177 parser->qualifying_scope = NULL_TREE;
14178 parser->object_scope = NULL_TREE;
14179 /* Look for a trailing `;' after the declaration. */
14180 if (!function_definition_p
14181 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14182 cp_parser_skip_to_end_of_block_or_statement (parser);
14184 return decl;
14187 /* Parse a cast-expression that is not the operand of a unary "&". */
14189 static tree
14190 cp_parser_simple_cast_expression (cp_parser *parser)
14192 return cp_parser_cast_expression (parser, /*address_p=*/false);
14195 /* Parse a functional cast to TYPE. Returns an expression
14196 representing the cast. */
14198 static tree
14199 cp_parser_functional_cast (cp_parser* parser, tree type)
14201 tree expression_list;
14203 expression_list
14204 = cp_parser_parenthesized_expression_list (parser, false,
14205 /*non_constant_p=*/NULL);
14207 return build_functional_cast (type, expression_list);
14210 /* Save the tokens that make up the body of a member function defined
14211 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14212 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14213 specifiers applied to the declaration. Returns the FUNCTION_DECL
14214 for the member function. */
14216 static tree
14217 cp_parser_save_member_function_body (cp_parser* parser,
14218 tree decl_specifiers,
14219 tree declarator,
14220 tree attributes)
14222 cp_token_cache *cache;
14223 tree fn;
14225 /* Create the function-declaration. */
14226 fn = start_method (decl_specifiers, declarator, attributes);
14227 /* If something went badly wrong, bail out now. */
14228 if (fn == error_mark_node)
14230 /* If there's a function-body, skip it. */
14231 if (cp_parser_token_starts_function_definition_p
14232 (cp_lexer_peek_token (parser->lexer)))
14233 cp_parser_skip_to_end_of_block_or_statement (parser);
14234 return error_mark_node;
14237 /* Remember it, if there default args to post process. */
14238 cp_parser_save_default_args (parser, fn);
14240 /* Create a token cache. */
14241 cache = cp_token_cache_new ();
14242 /* Save away the tokens that make up the body of the
14243 function. */
14244 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14245 /* Handle function try blocks. */
14246 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14247 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14249 /* Save away the inline definition; we will process it when the
14250 class is complete. */
14251 DECL_PENDING_INLINE_INFO (fn) = cache;
14252 DECL_PENDING_INLINE_P (fn) = 1;
14254 /* We need to know that this was defined in the class, so that
14255 friend templates are handled correctly. */
14256 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14258 /* We're done with the inline definition. */
14259 finish_method (fn);
14261 /* Add FN to the queue of functions to be parsed later. */
14262 TREE_VALUE (parser->unparsed_functions_queues)
14263 = tree_cons (NULL_TREE, fn,
14264 TREE_VALUE (parser->unparsed_functions_queues));
14266 return fn;
14269 /* Parse a template-argument-list, as well as the trailing ">" (but
14270 not the opening ">"). See cp_parser_template_argument_list for the
14271 return value. */
14273 static tree
14274 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14276 tree arguments;
14277 tree saved_scope;
14278 tree saved_qualifying_scope;
14279 tree saved_object_scope;
14280 bool saved_greater_than_is_operator_p;
14282 /* [temp.names]
14284 When parsing a template-id, the first non-nested `>' is taken as
14285 the end of the template-argument-list rather than a greater-than
14286 operator. */
14287 saved_greater_than_is_operator_p
14288 = parser->greater_than_is_operator_p;
14289 parser->greater_than_is_operator_p = false;
14290 /* Parsing the argument list may modify SCOPE, so we save it
14291 here. */
14292 saved_scope = parser->scope;
14293 saved_qualifying_scope = parser->qualifying_scope;
14294 saved_object_scope = parser->object_scope;
14295 /* Parse the template-argument-list itself. */
14296 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14297 arguments = NULL_TREE;
14298 else
14299 arguments = cp_parser_template_argument_list (parser);
14300 /* Look for the `>' that ends the template-argument-list. If we find
14301 a '>>' instead, it's probably just a typo. */
14302 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14304 if (!saved_greater_than_is_operator_p)
14306 /* If we're in a nested template argument list, the '>>' has to be
14307 a typo for '> >'. We emit the error message, but we continue
14308 parsing and we push a '>' as next token, so that the argument
14309 list will be parsed correctly.. */
14310 cp_token* token;
14311 error ("`>>' should be `> >' within a nested template argument list");
14312 token = cp_lexer_peek_token (parser->lexer);
14313 token->type = CPP_GREATER;
14315 else
14317 /* If this is not a nested template argument list, the '>>' is
14318 a typo for '>'. Emit an error message and continue. */
14319 error ("spurious `>>', use `>' to terminate a template argument list");
14320 cp_lexer_consume_token (parser->lexer);
14323 else
14324 cp_parser_require (parser, CPP_GREATER, "`>'");
14325 /* The `>' token might be a greater-than operator again now. */
14326 parser->greater_than_is_operator_p
14327 = saved_greater_than_is_operator_p;
14328 /* Restore the SAVED_SCOPE. */
14329 parser->scope = saved_scope;
14330 parser->qualifying_scope = saved_qualifying_scope;
14331 parser->object_scope = saved_object_scope;
14333 return arguments;
14336 /* MEMBER_FUNCTION is a member function, or a friend. If default
14337 arguments, or the body of the function have not yet been parsed,
14338 parse them now. */
14340 static void
14341 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14343 cp_lexer *saved_lexer;
14345 /* If this member is a template, get the underlying
14346 FUNCTION_DECL. */
14347 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14348 member_function = DECL_TEMPLATE_RESULT (member_function);
14350 /* There should not be any class definitions in progress at this
14351 point; the bodies of members are only parsed outside of all class
14352 definitions. */
14353 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14354 /* While we're parsing the member functions we might encounter more
14355 classes. We want to handle them right away, but we don't want
14356 them getting mixed up with functions that are currently in the
14357 queue. */
14358 parser->unparsed_functions_queues
14359 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14361 /* Make sure that any template parameters are in scope. */
14362 maybe_begin_member_template_processing (member_function);
14364 /* If the body of the function has not yet been parsed, parse it
14365 now. */
14366 if (DECL_PENDING_INLINE_P (member_function))
14368 tree function_scope;
14369 cp_token_cache *tokens;
14371 /* The function is no longer pending; we are processing it. */
14372 tokens = DECL_PENDING_INLINE_INFO (member_function);
14373 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14374 DECL_PENDING_INLINE_P (member_function) = 0;
14375 /* If this was an inline function in a local class, enter the scope
14376 of the containing function. */
14377 function_scope = decl_function_context (member_function);
14378 if (function_scope)
14379 push_function_context_to (function_scope);
14381 /* Save away the current lexer. */
14382 saved_lexer = parser->lexer;
14383 /* Make a new lexer to feed us the tokens saved for this function. */
14384 parser->lexer = cp_lexer_new_from_tokens (tokens);
14385 parser->lexer->next = saved_lexer;
14387 /* Set the current source position to be the location of the first
14388 token in the saved inline body. */
14389 cp_lexer_peek_token (parser->lexer);
14391 /* Let the front end know that we going to be defining this
14392 function. */
14393 start_function (NULL_TREE, member_function, NULL_TREE,
14394 SF_PRE_PARSED | SF_INCLASS_INLINE);
14396 /* Now, parse the body of the function. */
14397 cp_parser_function_definition_after_declarator (parser,
14398 /*inline_p=*/true);
14400 /* Leave the scope of the containing function. */
14401 if (function_scope)
14402 pop_function_context_from (function_scope);
14403 /* Restore the lexer. */
14404 parser->lexer = saved_lexer;
14407 /* Remove any template parameters from the symbol table. */
14408 maybe_end_member_template_processing ();
14410 /* Restore the queue. */
14411 parser->unparsed_functions_queues
14412 = TREE_CHAIN (parser->unparsed_functions_queues);
14415 /* If DECL contains any default args, remember it on the unparsed
14416 functions queue. */
14418 static void
14419 cp_parser_save_default_args (cp_parser* parser, tree decl)
14421 tree probe;
14423 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14424 probe;
14425 probe = TREE_CHAIN (probe))
14426 if (TREE_PURPOSE (probe))
14428 TREE_PURPOSE (parser->unparsed_functions_queues)
14429 = tree_cons (NULL_TREE, decl,
14430 TREE_PURPOSE (parser->unparsed_functions_queues));
14431 break;
14433 return;
14436 /* FN is a FUNCTION_DECL which may contains a parameter with an
14437 unparsed DEFAULT_ARG. Parse the default args now. */
14439 static void
14440 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14442 cp_lexer *saved_lexer;
14443 cp_token_cache *tokens;
14444 bool saved_local_variables_forbidden_p;
14445 tree parameters;
14447 /* While we're parsing the default args, we might (due to the
14448 statement expression extension) encounter more classes. We want
14449 to handle them right away, but we don't want them getting mixed
14450 up with default args that are currently in the queue. */
14451 parser->unparsed_functions_queues
14452 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14454 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14455 parameters;
14456 parameters = TREE_CHAIN (parameters))
14458 if (!TREE_PURPOSE (parameters)
14459 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14460 continue;
14462 /* Save away the current lexer. */
14463 saved_lexer = parser->lexer;
14464 /* Create a new one, using the tokens we have saved. */
14465 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14466 parser->lexer = cp_lexer_new_from_tokens (tokens);
14468 /* Set the current source position to be the location of the
14469 first token in the default argument. */
14470 cp_lexer_peek_token (parser->lexer);
14472 /* Local variable names (and the `this' keyword) may not appear
14473 in a default argument. */
14474 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14475 parser->local_variables_forbidden_p = true;
14476 /* Parse the assignment-expression. */
14477 if (DECL_CLASS_SCOPE_P (fn))
14478 push_nested_class (DECL_CONTEXT (fn));
14479 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14480 if (DECL_CLASS_SCOPE_P (fn))
14481 pop_nested_class ();
14483 /* Restore saved state. */
14484 parser->lexer = saved_lexer;
14485 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14488 /* Restore the queue. */
14489 parser->unparsed_functions_queues
14490 = TREE_CHAIN (parser->unparsed_functions_queues);
14493 /* Parse the operand of `sizeof' (or a similar operator). Returns
14494 either a TYPE or an expression, depending on the form of the
14495 input. The KEYWORD indicates which kind of expression we have
14496 encountered. */
14498 static tree
14499 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14501 static const char *format;
14502 tree expr = NULL_TREE;
14503 const char *saved_message;
14504 bool saved_integral_constant_expression_p;
14506 /* Initialize FORMAT the first time we get here. */
14507 if (!format)
14508 format = "types may not be defined in `%s' expressions";
14510 /* Types cannot be defined in a `sizeof' expression. Save away the
14511 old message. */
14512 saved_message = parser->type_definition_forbidden_message;
14513 /* And create the new one. */
14514 parser->type_definition_forbidden_message
14515 = xmalloc (strlen (format)
14516 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14517 + 1 /* `\0' */);
14518 sprintf ((char *) parser->type_definition_forbidden_message,
14519 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14521 /* The restrictions on constant-expressions do not apply inside
14522 sizeof expressions. */
14523 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14524 parser->integral_constant_expression_p = false;
14526 /* Do not actually evaluate the expression. */
14527 ++skip_evaluation;
14528 /* If it's a `(', then we might be looking at the type-id
14529 construction. */
14530 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14532 tree type;
14533 bool saved_in_type_id_in_expr_p;
14535 /* We can't be sure yet whether we're looking at a type-id or an
14536 expression. */
14537 cp_parser_parse_tentatively (parser);
14538 /* Consume the `('. */
14539 cp_lexer_consume_token (parser->lexer);
14540 /* Parse the type-id. */
14541 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14542 parser->in_type_id_in_expr_p = true;
14543 type = cp_parser_type_id (parser);
14544 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14545 /* Now, look for the trailing `)'. */
14546 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14547 /* If all went well, then we're done. */
14548 if (cp_parser_parse_definitely (parser))
14550 /* Build a list of decl-specifiers; right now, we have only
14551 a single type-specifier. */
14552 type = build_tree_list (NULL_TREE,
14553 type);
14555 /* Call grokdeclarator to figure out what type this is. */
14556 expr = grokdeclarator (NULL_TREE,
14557 type,
14558 TYPENAME,
14559 /*initialized=*/0,
14560 /*attrlist=*/NULL);
14564 /* If the type-id production did not work out, then we must be
14565 looking at the unary-expression production. */
14566 if (!expr)
14567 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14568 /* Go back to evaluating expressions. */
14569 --skip_evaluation;
14571 /* Free the message we created. */
14572 free ((char *) parser->type_definition_forbidden_message);
14573 /* And restore the old one. */
14574 parser->type_definition_forbidden_message = saved_message;
14575 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14577 return expr;
14580 /* If the current declaration has no declarator, return true. */
14582 static bool
14583 cp_parser_declares_only_class_p (cp_parser *parser)
14585 /* If the next token is a `;' or a `,' then there is no
14586 declarator. */
14587 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14588 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14591 /* Simplify EXPR if it is a non-dependent expression. Returns the
14592 (possibly simplified) expression. */
14594 static tree
14595 cp_parser_fold_non_dependent_expr (tree expr)
14597 /* If we're in a template, but EXPR isn't value dependent, simplify
14598 it. We're supposed to treat:
14600 template <typename T> void f(T[1 + 1]);
14601 template <typename T> void f(T[2]);
14603 as two declarations of the same function, for example. */
14604 if (processing_template_decl
14605 && !type_dependent_expression_p (expr)
14606 && !value_dependent_expression_p (expr))
14608 HOST_WIDE_INT saved_processing_template_decl;
14610 saved_processing_template_decl = processing_template_decl;
14611 processing_template_decl = 0;
14612 expr = tsubst_copy_and_build (expr,
14613 /*args=*/NULL_TREE,
14614 tf_error,
14615 /*in_decl=*/NULL_TREE,
14616 /*function_p=*/false);
14617 processing_template_decl = saved_processing_template_decl;
14619 return expr;
14622 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14623 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14625 static bool
14626 cp_parser_friend_p (tree decl_specifiers)
14628 while (decl_specifiers)
14630 /* See if this decl-specifier is `friend'. */
14631 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14632 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14633 return true;
14635 /* Go on to the next decl-specifier. */
14636 decl_specifiers = TREE_CHAIN (decl_specifiers);
14639 return false;
14642 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14643 issue an error message indicating that TOKEN_DESC was expected.
14645 Returns the token consumed, if the token had the appropriate type.
14646 Otherwise, returns NULL. */
14648 static cp_token *
14649 cp_parser_require (cp_parser* parser,
14650 enum cpp_ttype type,
14651 const char* token_desc)
14653 if (cp_lexer_next_token_is (parser->lexer, type))
14654 return cp_lexer_consume_token (parser->lexer);
14655 else
14657 /* Output the MESSAGE -- unless we're parsing tentatively. */
14658 if (!cp_parser_simulate_error (parser))
14660 char *message = concat ("expected ", token_desc, NULL);
14661 cp_parser_error (parser, message);
14662 free (message);
14664 return NULL;
14668 /* Like cp_parser_require, except that tokens will be skipped until
14669 the desired token is found. An error message is still produced if
14670 the next token is not as expected. */
14672 static void
14673 cp_parser_skip_until_found (cp_parser* parser,
14674 enum cpp_ttype type,
14675 const char* token_desc)
14677 cp_token *token;
14678 unsigned nesting_depth = 0;
14680 if (cp_parser_require (parser, type, token_desc))
14681 return;
14683 /* Skip tokens until the desired token is found. */
14684 while (true)
14686 /* Peek at the next token. */
14687 token = cp_lexer_peek_token (parser->lexer);
14688 /* If we've reached the token we want, consume it and
14689 stop. */
14690 if (token->type == type && !nesting_depth)
14692 cp_lexer_consume_token (parser->lexer);
14693 return;
14695 /* If we've run out of tokens, stop. */
14696 if (token->type == CPP_EOF)
14697 return;
14698 if (token->type == CPP_OPEN_BRACE
14699 || token->type == CPP_OPEN_PAREN
14700 || token->type == CPP_OPEN_SQUARE)
14701 ++nesting_depth;
14702 else if (token->type == CPP_CLOSE_BRACE
14703 || token->type == CPP_CLOSE_PAREN
14704 || token->type == CPP_CLOSE_SQUARE)
14706 if (nesting_depth-- == 0)
14707 return;
14709 /* Consume this token. */
14710 cp_lexer_consume_token (parser->lexer);
14714 /* If the next token is the indicated keyword, consume it. Otherwise,
14715 issue an error message indicating that TOKEN_DESC was expected.
14717 Returns the token consumed, if the token had the appropriate type.
14718 Otherwise, returns NULL. */
14720 static cp_token *
14721 cp_parser_require_keyword (cp_parser* parser,
14722 enum rid keyword,
14723 const char* token_desc)
14725 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14727 if (token && token->keyword != keyword)
14729 dyn_string_t error_msg;
14731 /* Format the error message. */
14732 error_msg = dyn_string_new (0);
14733 dyn_string_append_cstr (error_msg, "expected ");
14734 dyn_string_append_cstr (error_msg, token_desc);
14735 cp_parser_error (parser, error_msg->s);
14736 dyn_string_delete (error_msg);
14737 return NULL;
14740 return token;
14743 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14744 function-definition. */
14746 static bool
14747 cp_parser_token_starts_function_definition_p (cp_token* token)
14749 return (/* An ordinary function-body begins with an `{'. */
14750 token->type == CPP_OPEN_BRACE
14751 /* A ctor-initializer begins with a `:'. */
14752 || token->type == CPP_COLON
14753 /* A function-try-block begins with `try'. */
14754 || token->keyword == RID_TRY
14755 /* The named return value extension begins with `return'. */
14756 || token->keyword == RID_RETURN);
14759 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14760 definition. */
14762 static bool
14763 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14765 cp_token *token;
14767 token = cp_lexer_peek_token (parser->lexer);
14768 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14771 /* Returns TRUE iff the next token is the "," or ">" ending a
14772 template-argument. ">>" is also accepted (after the full
14773 argument was parsed) because it's probably a typo for "> >",
14774 and there is a specific diagnostic for this. */
14776 static bool
14777 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14779 cp_token *token;
14781 token = cp_lexer_peek_token (parser->lexer);
14782 return (token->type == CPP_COMMA || token->type == CPP_GREATER
14783 || token->type == CPP_RSHIFT);
14786 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14787 or none_type otherwise. */
14789 static enum tag_types
14790 cp_parser_token_is_class_key (cp_token* token)
14792 switch (token->keyword)
14794 case RID_CLASS:
14795 return class_type;
14796 case RID_STRUCT:
14797 return record_type;
14798 case RID_UNION:
14799 return union_type;
14801 default:
14802 return none_type;
14806 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14808 static void
14809 cp_parser_check_class_key (enum tag_types class_key, tree type)
14811 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14812 pedwarn ("`%s' tag used in naming `%#T'",
14813 class_key == union_type ? "union"
14814 : class_key == record_type ? "struct" : "class",
14815 type);
14818 /* Issue an error message if DECL is redeclared with different
14819 access than its original declaration [class.access.spec/3].
14820 This applies to nested classes and nested class templates.
14821 [class.mem/1]. */
14823 static void cp_parser_check_access_in_redeclaration (tree decl)
14825 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14826 return;
14828 if ((TREE_PRIVATE (decl)
14829 != (current_access_specifier == access_private_node))
14830 || (TREE_PROTECTED (decl)
14831 != (current_access_specifier == access_protected_node)))
14832 error ("%D redeclared with different access", decl);
14835 /* Look for the `template' keyword, as a syntactic disambiguator.
14836 Return TRUE iff it is present, in which case it will be
14837 consumed. */
14839 static bool
14840 cp_parser_optional_template_keyword (cp_parser *parser)
14842 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14844 /* The `template' keyword can only be used within templates;
14845 outside templates the parser can always figure out what is a
14846 template and what is not. */
14847 if (!processing_template_decl)
14849 error ("`template' (as a disambiguator) is only allowed "
14850 "within templates");
14851 /* If this part of the token stream is rescanned, the same
14852 error message would be generated. So, we purge the token
14853 from the stream. */
14854 cp_lexer_purge_token (parser->lexer);
14855 return false;
14857 else
14859 /* Consume the `template' keyword. */
14860 cp_lexer_consume_token (parser->lexer);
14861 return true;
14865 return false;
14868 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14869 set PARSER->SCOPE, and perform other related actions. */
14871 static void
14872 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14874 tree value;
14875 tree check;
14877 /* Get the stored value. */
14878 value = cp_lexer_consume_token (parser->lexer)->value;
14879 /* Perform any access checks that were deferred. */
14880 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14881 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14882 /* Set the scope from the stored value. */
14883 parser->scope = TREE_VALUE (value);
14884 parser->qualifying_scope = TREE_TYPE (value);
14885 parser->object_scope = NULL_TREE;
14888 /* Add tokens to CACHE until an non-nested END token appears. */
14890 static void
14891 cp_parser_cache_group (cp_parser *parser,
14892 cp_token_cache *cache,
14893 enum cpp_ttype end,
14894 unsigned depth)
14896 while (true)
14898 cp_token *token;
14900 /* Abort a parenthesized expression if we encounter a brace. */
14901 if ((end == CPP_CLOSE_PAREN || depth == 0)
14902 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14903 return;
14904 /* Consume the next token. */
14905 token = cp_lexer_consume_token (parser->lexer);
14906 /* If we've reached the end of the file, stop. */
14907 if (token->type == CPP_EOF)
14908 return;
14909 /* Add this token to the tokens we are saving. */
14910 cp_token_cache_push_token (cache, token);
14911 /* See if it starts a new group. */
14912 if (token->type == CPP_OPEN_BRACE)
14914 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14915 if (depth == 0)
14916 return;
14918 else if (token->type == CPP_OPEN_PAREN)
14919 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14920 else if (token->type == end)
14921 return;
14925 /* Begin parsing tentatively. We always save tokens while parsing
14926 tentatively so that if the tentative parsing fails we can restore the
14927 tokens. */
14929 static void
14930 cp_parser_parse_tentatively (cp_parser* parser)
14932 /* Enter a new parsing context. */
14933 parser->context = cp_parser_context_new (parser->context);
14934 /* Begin saving tokens. */
14935 cp_lexer_save_tokens (parser->lexer);
14936 /* In order to avoid repetitive access control error messages,
14937 access checks are queued up until we are no longer parsing
14938 tentatively. */
14939 push_deferring_access_checks (dk_deferred);
14942 /* Commit to the currently active tentative parse. */
14944 static void
14945 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14947 cp_parser_context *context;
14948 cp_lexer *lexer;
14950 /* Mark all of the levels as committed. */
14951 lexer = parser->lexer;
14952 for (context = parser->context; context->next; context = context->next)
14954 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14955 break;
14956 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14957 while (!cp_lexer_saving_tokens (lexer))
14958 lexer = lexer->next;
14959 cp_lexer_commit_tokens (lexer);
14963 /* Abort the currently active tentative parse. All consumed tokens
14964 will be rolled back, and no diagnostics will be issued. */
14966 static void
14967 cp_parser_abort_tentative_parse (cp_parser* parser)
14969 cp_parser_simulate_error (parser);
14970 /* Now, pretend that we want to see if the construct was
14971 successfully parsed. */
14972 cp_parser_parse_definitely (parser);
14975 /* Stop parsing tentatively. If a parse error has occurred, restore the
14976 token stream. Otherwise, commit to the tokens we have consumed.
14977 Returns true if no error occurred; false otherwise. */
14979 static bool
14980 cp_parser_parse_definitely (cp_parser* parser)
14982 bool error_occurred;
14983 cp_parser_context *context;
14985 /* Remember whether or not an error occurred, since we are about to
14986 destroy that information. */
14987 error_occurred = cp_parser_error_occurred (parser);
14988 /* Remove the topmost context from the stack. */
14989 context = parser->context;
14990 parser->context = context->next;
14991 /* If no parse errors occurred, commit to the tentative parse. */
14992 if (!error_occurred)
14994 /* Commit to the tokens read tentatively, unless that was
14995 already done. */
14996 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14997 cp_lexer_commit_tokens (parser->lexer);
14999 pop_to_parent_deferring_access_checks ();
15001 /* Otherwise, if errors occurred, roll back our state so that things
15002 are just as they were before we began the tentative parse. */
15003 else
15005 cp_lexer_rollback_tokens (parser->lexer);
15006 pop_deferring_access_checks ();
15008 /* Add the context to the front of the free list. */
15009 context->next = cp_parser_context_free_list;
15010 cp_parser_context_free_list = context;
15012 return !error_occurred;
15015 /* Returns true if we are parsing tentatively -- but have decided that
15016 we will stick with this tentative parse, even if errors occur. */
15018 static bool
15019 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15021 return (cp_parser_parsing_tentatively (parser)
15022 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15025 /* Returns nonzero iff an error has occurred during the most recent
15026 tentative parse. */
15028 static bool
15029 cp_parser_error_occurred (cp_parser* parser)
15031 return (cp_parser_parsing_tentatively (parser)
15032 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15035 /* Returns nonzero if GNU extensions are allowed. */
15037 static bool
15038 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15040 return parser->allow_gnu_extensions_p;
15045 /* The parser. */
15047 static GTY (()) cp_parser *the_parser;
15049 /* External interface. */
15051 /* Parse one entire translation unit. */
15053 void
15054 c_parse_file (void)
15056 bool error_occurred;
15058 the_parser = cp_parser_new ();
15059 push_deferring_access_checks (flag_access_control
15060 ? dk_no_deferred : dk_no_check);
15061 error_occurred = cp_parser_translation_unit (the_parser);
15062 the_parser = NULL;
15065 /* Clean up after parsing the entire translation unit. */
15067 void
15068 free_parser_stacks (void)
15070 /* Nothing to do. */
15073 /* This variable must be provided by every front end. */
15075 int yydebug;
15077 #include "gt-cp-parser.h"