Update to gcc-3.4.6
[dragonfly.git] / contrib / gcc-3.4 / gcc / cp / parser.c
blob032cbd7e9b760d04efc259cd2268dbf38e2c151b
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
40 /* The lexer. */
42 /* Overview
43 --------
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
46 look-ahead.
48 Methodology
49 -----------
51 We use a circular buffer to store incoming tokens.
53 Some artifacts of the C++ language (such as the
54 expression/declaration ambiguity) require arbitrary look-ahead.
55 The strategy we adopt for dealing with these problems is to attempt
56 to parse one construct (e.g., the declaration) and fall back to the
57 other (e.g., the expression) if that attempt does not succeed.
58 Therefore, we must sometimes store an arbitrary number of tokens.
60 The parser routinely peeks at the next token, and then consumes it
61 later. That also requires a buffer in which to store the tokens.
63 In order to easily permit adding tokens to the end of the buffer,
64 while removing them from the beginning of the buffer, we use a
65 circular buffer. */
67 /* A C++ token. */
69 typedef struct cp_token GTY (())
71 /* The kind of token. */
72 ENUM_BITFIELD (cpp_ttype) type : 8;
73 /* If this token is a keyword, this value indicates which keyword.
74 Otherwise, this value is RID_MAX. */
75 ENUM_BITFIELD (rid) keyword : 8;
76 /* Token flags. */
77 unsigned char flags;
78 /* The value associated with this token, if any. */
79 tree value;
80 /* The location at which this token was found. */
81 location_t location;
82 } cp_token;
84 /* The number of tokens in a single token block.
85 Computed so that cp_token_block fits in a 512B allocation unit. */
87 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
89 /* A group of tokens. These groups are chained together to store
90 large numbers of tokens. (For example, a token block is created
91 when the body of an inline member function is first encountered;
92 the tokens are processed later after the class definition is
93 complete.)
95 This somewhat ungainly data structure (as opposed to, say, a
96 variable-length array), is used due to constraints imposed by the
97 current garbage-collection methodology. If it is made more
98 flexible, we could perhaps simplify the data structures involved. */
100 typedef struct cp_token_block GTY (())
102 /* The tokens. */
103 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
104 /* The number of tokens in this block. */
105 size_t num_tokens;
106 /* The next token block in the chain. */
107 struct cp_token_block *next;
108 /* The previous block in the chain. */
109 struct cp_token_block *prev;
110 } cp_token_block;
112 typedef struct cp_token_cache GTY (())
114 /* The first block in the cache. NULL if there are no tokens in the
115 cache. */
116 cp_token_block *first;
117 /* The last block in the cache. NULL If there are no tokens in the
118 cache. */
119 cp_token_block *last;
120 } cp_token_cache;
122 /* Prototypes. */
124 static cp_token_cache *cp_token_cache_new
125 (void);
126 static void cp_token_cache_push_token
127 (cp_token_cache *, cp_token *);
129 /* Create a new cp_token_cache. */
131 static cp_token_cache *
132 cp_token_cache_new (void)
134 return ggc_alloc_cleared (sizeof (cp_token_cache));
137 /* Add *TOKEN to *CACHE. */
139 static void
140 cp_token_cache_push_token (cp_token_cache *cache,
141 cp_token *token)
143 cp_token_block *b = cache->last;
145 /* See if we need to allocate a new token block. */
146 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
148 b = ggc_alloc_cleared (sizeof (cp_token_block));
149 b->prev = cache->last;
150 if (cache->last)
152 cache->last->next = b;
153 cache->last = b;
155 else
156 cache->first = cache->last = b;
158 /* Add this token to the current token block. */
159 b->tokens[b->num_tokens++] = *token;
162 /* The cp_lexer structure represents the C++ lexer. It is responsible
163 for managing the token stream from the preprocessor and supplying
164 it to the parser. */
166 typedef struct cp_lexer GTY (())
168 /* The memory allocated for the buffer. Never NULL. */
169 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
170 /* A pointer just past the end of the memory allocated for the buffer. */
171 cp_token * GTY ((skip (""))) buffer_end;
172 /* The first valid token in the buffer, or NULL if none. */
173 cp_token * GTY ((skip (""))) first_token;
174 /* The next available token. If NEXT_TOKEN is NULL, then there are
175 no more available tokens. */
176 cp_token * GTY ((skip (""))) next_token;
177 /* A pointer just past the last available token. If FIRST_TOKEN is
178 NULL, however, there are no available tokens, and then this
179 location is simply the place in which the next token read will be
180 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
181 When the LAST_TOKEN == BUFFER, then the last token is at the
182 highest memory address in the BUFFER. */
183 cp_token * GTY ((skip (""))) last_token;
185 /* A stack indicating positions at which cp_lexer_save_tokens was
186 called. The top entry is the most recent position at which we
187 began saving tokens. The entries are differences in token
188 position between FIRST_TOKEN and the first saved token.
190 If the stack is non-empty, we are saving tokens. When a token is
191 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
192 pointer will not. The token stream will be preserved so that it
193 can be reexamined later.
195 If the stack is empty, then we are not saving tokens. Whenever a
196 token is consumed, the FIRST_TOKEN pointer will be moved, and the
197 consumed token will be gone forever. */
198 varray_type saved_tokens;
200 /* The STRING_CST tokens encountered while processing the current
201 string literal. */
202 varray_type string_tokens;
204 /* True if we should obtain more tokens from the preprocessor; false
205 if we are processing a saved token cache. */
206 bool main_lexer_p;
208 /* True if we should output debugging information. */
209 bool debugging_p;
211 /* The next lexer in a linked list of lexers. */
212 struct cp_lexer *next;
213 } cp_lexer;
215 /* Prototypes. */
217 static cp_lexer *cp_lexer_new_main
218 (void);
219 static cp_lexer *cp_lexer_new_from_tokens
220 (struct cp_token_cache *);
221 static int cp_lexer_saving_tokens
222 (const cp_lexer *);
223 static cp_token *cp_lexer_next_token
224 (cp_lexer *, cp_token *);
225 static cp_token *cp_lexer_prev_token
226 (cp_lexer *, cp_token *);
227 static ptrdiff_t cp_lexer_token_difference
228 (cp_lexer *, cp_token *, cp_token *);
229 static cp_token *cp_lexer_read_token
230 (cp_lexer *);
231 static void cp_lexer_maybe_grow_buffer
232 (cp_lexer *);
233 static void cp_lexer_get_preprocessor_token
234 (cp_lexer *, cp_token *);
235 static cp_token *cp_lexer_peek_token
236 (cp_lexer *);
237 static cp_token *cp_lexer_peek_nth_token
238 (cp_lexer *, size_t);
239 static inline bool cp_lexer_next_token_is
240 (cp_lexer *, enum cpp_ttype);
241 static bool cp_lexer_next_token_is_not
242 (cp_lexer *, enum cpp_ttype);
243 static bool cp_lexer_next_token_is_keyword
244 (cp_lexer *, enum rid);
245 static cp_token *cp_lexer_consume_token
246 (cp_lexer *);
247 static void cp_lexer_purge_token
248 (cp_lexer *);
249 static void cp_lexer_purge_tokens_after
250 (cp_lexer *, cp_token *);
251 static void cp_lexer_save_tokens
252 (cp_lexer *);
253 static void cp_lexer_commit_tokens
254 (cp_lexer *);
255 static void cp_lexer_rollback_tokens
256 (cp_lexer *);
257 static inline void cp_lexer_set_source_position_from_token
258 (cp_lexer *, const cp_token *);
259 static void cp_lexer_print_token
260 (FILE *, cp_token *);
261 static inline bool cp_lexer_debugging_p
262 (cp_lexer *);
263 static void cp_lexer_start_debugging
264 (cp_lexer *) ATTRIBUTE_UNUSED;
265 static void cp_lexer_stop_debugging
266 (cp_lexer *) ATTRIBUTE_UNUSED;
268 /* Manifest constants. */
270 #define CP_TOKEN_BUFFER_SIZE 5
271 #define CP_SAVED_TOKENS_SIZE 5
273 /* A token type for keywords, as opposed to ordinary identifiers. */
274 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
276 /* A token type for template-ids. If a template-id is processed while
277 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
278 the value of the CPP_TEMPLATE_ID is whatever was returned by
279 cp_parser_template_id. */
280 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
282 /* A token type for nested-name-specifiers. If a
283 nested-name-specifier is processed while parsing tentatively, it is
284 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
285 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
286 cp_parser_nested_name_specifier_opt. */
287 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
289 /* A token type for tokens that are not tokens at all; these are used
290 to mark the end of a token block. */
291 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
293 /* Variables. */
295 /* The stream to which debugging output should be written. */
296 static FILE *cp_lexer_debug_stream;
298 /* Create a new main C++ lexer, the lexer that gets tokens from the
299 preprocessor. */
301 static cp_lexer *
302 cp_lexer_new_main (void)
304 cp_lexer *lexer;
305 cp_token first_token;
307 /* It's possible that lexing the first token will load a PCH file,
308 which is a GC collection point. So we have to grab the first
309 token before allocating any memory. */
310 cp_lexer_get_preprocessor_token (NULL, &first_token);
311 c_common_no_more_pch ();
313 /* Allocate the memory. */
314 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
316 /* Create the circular buffer. */
317 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
318 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
320 /* There is one token in the buffer. */
321 lexer->last_token = lexer->buffer + 1;
322 lexer->first_token = lexer->buffer;
323 lexer->next_token = lexer->buffer;
324 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
326 /* This lexer obtains more tokens by calling c_lex. */
327 lexer->main_lexer_p = true;
329 /* Create the SAVED_TOKENS stack. */
330 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
332 /* Create the STRINGS array. */
333 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
335 /* Assume we are not debugging. */
336 lexer->debugging_p = false;
338 return lexer;
341 /* Create a new lexer whose token stream is primed with the TOKENS.
342 When these tokens are exhausted, no new tokens will be read. */
344 static cp_lexer *
345 cp_lexer_new_from_tokens (cp_token_cache *tokens)
347 cp_lexer *lexer;
348 cp_token *token;
349 cp_token_block *block;
350 ptrdiff_t num_tokens;
352 /* Allocate the memory. */
353 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
355 /* Create a new buffer, appropriately sized. */
356 num_tokens = 0;
357 for (block = tokens->first; block != NULL; block = block->next)
358 num_tokens += block->num_tokens;
359 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
360 lexer->buffer_end = lexer->buffer + num_tokens;
362 /* Install the tokens. */
363 token = lexer->buffer;
364 for (block = tokens->first; block != NULL; block = block->next)
366 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
367 token += block->num_tokens;
370 /* The FIRST_TOKEN is the beginning of the buffer. */
371 lexer->first_token = lexer->buffer;
372 /* The next available token is also at the beginning of the buffer. */
373 lexer->next_token = lexer->buffer;
374 /* The buffer is full. */
375 lexer->last_token = lexer->first_token;
377 /* This lexer doesn't obtain more tokens. */
378 lexer->main_lexer_p = false;
380 /* Create the SAVED_TOKENS stack. */
381 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
383 /* Create the STRINGS array. */
384 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
386 /* Assume we are not debugging. */
387 lexer->debugging_p = false;
389 return lexer;
392 /* Returns nonzero if debugging information should be output. */
394 static inline bool
395 cp_lexer_debugging_p (cp_lexer *lexer)
397 return lexer->debugging_p;
400 /* Set the current source position from the information stored in
401 TOKEN. */
403 static inline void
404 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
405 const cp_token *token)
407 /* Ideally, the source position information would not be a global
408 variable, but it is. */
410 /* Update the line number. */
411 if (token->type != CPP_EOF)
412 input_location = token->location;
415 /* TOKEN points into the circular token buffer. Return a pointer to
416 the next token in the buffer. */
418 static inline cp_token *
419 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
421 token++;
422 if (token == lexer->buffer_end)
423 token = lexer->buffer;
424 return token;
427 /* TOKEN points into the circular token buffer. Return a pointer to
428 the previous token in the buffer. */
430 static inline cp_token *
431 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
433 if (token == lexer->buffer)
434 token = lexer->buffer_end;
435 return token - 1;
438 /* nonzero if we are presently saving tokens. */
440 static int
441 cp_lexer_saving_tokens (const cp_lexer* lexer)
443 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
446 /* Return a pointer to the token that is N tokens beyond TOKEN in the
447 buffer. */
449 static cp_token *
450 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
452 token += n;
453 if (token >= lexer->buffer_end)
454 token = lexer->buffer + (token - lexer->buffer_end);
455 return token;
458 /* Returns the number of times that START would have to be incremented
459 to reach FINISH. If START and FINISH are the same, returns zero. */
461 static ptrdiff_t
462 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
464 if (finish >= start)
465 return finish - start;
466 else
467 return ((lexer->buffer_end - lexer->buffer)
468 - (start - finish));
471 /* Obtain another token from the C preprocessor and add it to the
472 token buffer. Returns the newly read token. */
474 static cp_token *
475 cp_lexer_read_token (cp_lexer* lexer)
477 cp_token *token;
479 /* Make sure there is room in the buffer. */
480 cp_lexer_maybe_grow_buffer (lexer);
482 /* If there weren't any tokens, then this one will be the first. */
483 if (!lexer->first_token)
484 lexer->first_token = lexer->last_token;
485 /* Similarly, if there were no available tokens, there is one now. */
486 if (!lexer->next_token)
487 lexer->next_token = lexer->last_token;
489 /* Figure out where we're going to store the new token. */
490 token = lexer->last_token;
492 /* Get a new token from the preprocessor. */
493 cp_lexer_get_preprocessor_token (lexer, token);
495 /* Increment LAST_TOKEN. */
496 lexer->last_token = cp_lexer_next_token (lexer, token);
498 /* Strings should have type `const char []'. Right now, we will
499 have an ARRAY_TYPE that is constant rather than an array of
500 constant elements.
501 FIXME: Make fix_string_type get this right in the first place. */
502 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
503 && flag_const_strings)
505 tree type;
507 /* Get the current type. It will be an ARRAY_TYPE. */
508 type = TREE_TYPE (token->value);
509 /* Use build_cplus_array_type to rebuild the array, thereby
510 getting the right type. */
511 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
512 /* Reset the type of the token. */
513 TREE_TYPE (token->value) = type;
516 return token;
519 /* If the circular buffer is full, make it bigger. */
521 static void
522 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
524 /* If the buffer is full, enlarge it. */
525 if (lexer->last_token == lexer->first_token)
527 cp_token *new_buffer;
528 cp_token *old_buffer;
529 cp_token *new_first_token;
530 ptrdiff_t buffer_length;
531 size_t num_tokens_to_copy;
533 /* Remember the current buffer pointer. It will become invalid,
534 but we will need to do pointer arithmetic involving this
535 value. */
536 old_buffer = lexer->buffer;
537 /* Compute the current buffer size. */
538 buffer_length = lexer->buffer_end - lexer->buffer;
539 /* Allocate a buffer twice as big. */
540 new_buffer = ggc_realloc (lexer->buffer,
541 2 * buffer_length * sizeof (cp_token));
543 /* Because the buffer is circular, logically consecutive tokens
544 are not necessarily placed consecutively in memory.
545 Therefore, we must keep move the tokens that were before
546 FIRST_TOKEN to the second half of the newly allocated
547 buffer. */
548 num_tokens_to_copy = (lexer->first_token - old_buffer);
549 memcpy (new_buffer + buffer_length,
550 new_buffer,
551 num_tokens_to_copy * sizeof (cp_token));
552 /* Clear the rest of the buffer. We never look at this storage,
553 but the garbage collector may. */
554 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
555 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
557 /* Now recompute all of the buffer pointers. */
558 new_first_token
559 = new_buffer + (lexer->first_token - old_buffer);
560 if (lexer->next_token != NULL)
562 ptrdiff_t next_token_delta;
564 if (lexer->next_token > lexer->first_token)
565 next_token_delta = lexer->next_token - lexer->first_token;
566 else
567 next_token_delta =
568 buffer_length - (lexer->first_token - lexer->next_token);
569 lexer->next_token = new_first_token + next_token_delta;
571 lexer->last_token = new_first_token + buffer_length;
572 lexer->buffer = new_buffer;
573 lexer->buffer_end = new_buffer + buffer_length * 2;
574 lexer->first_token = new_first_token;
578 /* Store the next token from the preprocessor in *TOKEN. */
580 static void
581 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
582 cp_token *token)
584 bool done;
586 /* If this not the main lexer, return a terminating CPP_EOF token. */
587 if (lexer != NULL && !lexer->main_lexer_p)
589 token->type = CPP_EOF;
590 token->location.line = 0;
591 token->location.file = NULL;
592 token->value = NULL_TREE;
593 token->keyword = RID_MAX;
595 return;
598 done = false;
599 /* Keep going until we get a token we like. */
600 while (!done)
602 /* Get a new token from the preprocessor. */
603 token->type = c_lex_with_flags (&token->value, &token->flags);
604 /* Issue messages about tokens we cannot process. */
605 switch (token->type)
607 case CPP_ATSIGN:
608 case CPP_HASH:
609 case CPP_PASTE:
610 error ("invalid token");
611 break;
613 default:
614 /* This is a good token, so we exit the loop. */
615 done = true;
616 break;
619 /* Now we've got our token. */
620 token->location = input_location;
622 /* Check to see if this token is a keyword. */
623 if (token->type == CPP_NAME
624 && C_IS_RESERVED_WORD (token->value))
626 /* Mark this token as a keyword. */
627 token->type = CPP_KEYWORD;
628 /* Record which keyword. */
629 token->keyword = C_RID_CODE (token->value);
630 /* Update the value. Some keywords are mapped to particular
631 entities, rather than simply having the value of the
632 corresponding IDENTIFIER_NODE. For example, `__const' is
633 mapped to `const'. */
634 token->value = ridpointers[token->keyword];
636 else
637 token->keyword = RID_MAX;
640 /* Return a pointer to the next token in the token stream, but do not
641 consume it. */
643 static cp_token *
644 cp_lexer_peek_token (cp_lexer* lexer)
646 cp_token *token;
648 /* If there are no tokens, read one now. */
649 if (!lexer->next_token)
650 cp_lexer_read_token (lexer);
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer))
655 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
656 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
657 fprintf (cp_lexer_debug_stream, "\n");
660 token = lexer->next_token;
661 cp_lexer_set_source_position_from_token (lexer, token);
662 return token;
665 /* Return true if the next token has the indicated TYPE. */
667 static bool
668 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
670 cp_token *token;
672 /* Peek at the next token. */
673 token = cp_lexer_peek_token (lexer);
674 /* Check to see if it has the indicated TYPE. */
675 return token->type == type;
678 /* Return true if the next token does not have the indicated TYPE. */
680 static bool
681 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
683 return !cp_lexer_next_token_is (lexer, type);
686 /* Return true if the next token is the indicated KEYWORD. */
688 static bool
689 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
691 cp_token *token;
693 /* Peek at the next token. */
694 token = cp_lexer_peek_token (lexer);
695 /* Check to see if it is the indicated keyword. */
696 return token->keyword == keyword;
699 /* Return a pointer to the Nth token in the token stream. If N is 1,
700 then this is precisely equivalent to cp_lexer_peek_token. */
702 static cp_token *
703 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
705 cp_token *token;
707 /* N is 1-based, not zero-based. */
708 my_friendly_assert (n > 0, 20000224);
710 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
711 token = lexer->next_token;
712 /* If there are no tokens in the buffer, get one now. */
713 if (!token)
715 cp_lexer_read_token (lexer);
716 token = lexer->next_token;
719 /* Now, read tokens until we have enough. */
720 while (--n > 0)
722 /* Advance to the next token. */
723 token = cp_lexer_next_token (lexer, token);
724 /* If that's all the tokens we have, read a new one. */
725 if (token == lexer->last_token)
726 token = cp_lexer_read_token (lexer);
729 return token;
732 /* Consume the next token. The pointer returned is valid only until
733 another token is read. Callers should preserve copy the token
734 explicitly if they will need its value for a longer period of
735 time. */
737 static cp_token *
738 cp_lexer_consume_token (cp_lexer* lexer)
740 cp_token *token;
742 /* If there are no tokens, read one now. */
743 if (!lexer->next_token)
744 cp_lexer_read_token (lexer);
746 /* Remember the token we'll be returning. */
747 token = lexer->next_token;
749 /* Increment NEXT_TOKEN. */
750 lexer->next_token = cp_lexer_next_token (lexer,
751 lexer->next_token);
752 /* Check to see if we're all out of tokens. */
753 if (lexer->next_token == lexer->last_token)
754 lexer->next_token = NULL;
756 /* If we're not saving tokens, then move FIRST_TOKEN too. */
757 if (!cp_lexer_saving_tokens (lexer))
759 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
760 if (!lexer->next_token)
761 lexer->first_token = NULL;
762 else
763 lexer->first_token = lexer->next_token;
766 /* Provide debugging output. */
767 if (cp_lexer_debugging_p (lexer))
769 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
770 cp_lexer_print_token (cp_lexer_debug_stream, token);
771 fprintf (cp_lexer_debug_stream, "\n");
774 return token;
777 /* Permanently remove the next token from the token stream. There
778 must be a valid next token already; this token never reads
779 additional tokens from the preprocessor. */
781 static void
782 cp_lexer_purge_token (cp_lexer *lexer)
784 cp_token *token;
785 cp_token *next_token;
787 token = lexer->next_token;
788 while (true)
790 next_token = cp_lexer_next_token (lexer, token);
791 if (next_token == lexer->last_token)
792 break;
793 *token = *next_token;
794 token = next_token;
797 lexer->last_token = token;
798 /* The token purged may have been the only token remaining; if so,
799 clear NEXT_TOKEN. */
800 if (lexer->next_token == token)
801 lexer->next_token = NULL;
804 /* Permanently remove all tokens after TOKEN, up to, but not
805 including, the token that will be returned next by
806 cp_lexer_peek_token. */
808 static void
809 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
811 cp_token *peek;
812 cp_token *t1;
813 cp_token *t2;
815 if (lexer->next_token)
817 /* Copy the tokens that have not yet been read to the location
818 immediately following TOKEN. */
819 t1 = cp_lexer_next_token (lexer, token);
820 t2 = peek = cp_lexer_peek_token (lexer);
821 /* Move tokens into the vacant area between TOKEN and PEEK. */
822 while (t2 != lexer->last_token)
824 *t1 = *t2;
825 t1 = cp_lexer_next_token (lexer, t1);
826 t2 = cp_lexer_next_token (lexer, t2);
828 /* Now, the next available token is right after TOKEN. */
829 lexer->next_token = cp_lexer_next_token (lexer, token);
830 /* And the last token is wherever we ended up. */
831 lexer->last_token = t1;
833 else
835 /* There are no tokens in the buffer, so there is nothing to
836 copy. The last token in the buffer is TOKEN itself. */
837 lexer->last_token = cp_lexer_next_token (lexer, token);
841 /* Begin saving tokens. All tokens consumed after this point will be
842 preserved. */
844 static void
845 cp_lexer_save_tokens (cp_lexer* lexer)
847 /* Provide debugging output. */
848 if (cp_lexer_debugging_p (lexer))
849 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
851 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
852 restore the tokens if required. */
853 if (!lexer->next_token)
854 cp_lexer_read_token (lexer);
856 VARRAY_PUSH_INT (lexer->saved_tokens,
857 cp_lexer_token_difference (lexer,
858 lexer->first_token,
859 lexer->next_token));
862 /* Commit to the portion of the token stream most recently saved. */
864 static void
865 cp_lexer_commit_tokens (cp_lexer* lexer)
867 /* Provide debugging output. */
868 if (cp_lexer_debugging_p (lexer))
869 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
871 VARRAY_POP (lexer->saved_tokens);
874 /* Return all tokens saved since the last call to cp_lexer_save_tokens
875 to the token stream. Stop saving tokens. */
877 static void
878 cp_lexer_rollback_tokens (cp_lexer* lexer)
880 size_t delta;
882 /* Provide debugging output. */
883 if (cp_lexer_debugging_p (lexer))
884 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
886 /* Find the token that was the NEXT_TOKEN when we started saving
887 tokens. */
888 delta = VARRAY_TOP_INT(lexer->saved_tokens);
889 /* Make it the next token again now. */
890 lexer->next_token = cp_lexer_advance_token (lexer,
891 lexer->first_token,
892 delta);
893 /* It might be the case that there were no tokens when we started
894 saving tokens, but that there are some tokens now. */
895 if (!lexer->next_token && lexer->first_token)
896 lexer->next_token = lexer->first_token;
898 /* Stop saving tokens. */
899 VARRAY_POP (lexer->saved_tokens);
902 /* Print a representation of the TOKEN on the STREAM. */
904 static void
905 cp_lexer_print_token (FILE * stream, cp_token* token)
907 const char *token_type = NULL;
909 /* Figure out what kind of token this is. */
910 switch (token->type)
912 case CPP_EQ:
913 token_type = "EQ";
914 break;
916 case CPP_COMMA:
917 token_type = "COMMA";
918 break;
920 case CPP_OPEN_PAREN:
921 token_type = "OPEN_PAREN";
922 break;
924 case CPP_CLOSE_PAREN:
925 token_type = "CLOSE_PAREN";
926 break;
928 case CPP_OPEN_BRACE:
929 token_type = "OPEN_BRACE";
930 break;
932 case CPP_CLOSE_BRACE:
933 token_type = "CLOSE_BRACE";
934 break;
936 case CPP_SEMICOLON:
937 token_type = "SEMICOLON";
938 break;
940 case CPP_NAME:
941 token_type = "NAME";
942 break;
944 case CPP_EOF:
945 token_type = "EOF";
946 break;
948 case CPP_KEYWORD:
949 token_type = "keyword";
950 break;
952 /* This is not a token that we know how to handle yet. */
953 default:
954 break;
957 /* If we have a name for the token, print it out. Otherwise, we
958 simply give the numeric code. */
959 if (token_type)
960 fprintf (stream, "%s", token_type);
961 else
962 fprintf (stream, "%d", token->type);
963 /* And, for an identifier, print the identifier name. */
964 if (token->type == CPP_NAME
965 /* Some keywords have a value that is not an IDENTIFIER_NODE.
966 For example, `struct' is mapped to an INTEGER_CST. */
967 || (token->type == CPP_KEYWORD
968 && TREE_CODE (token->value) == IDENTIFIER_NODE))
969 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
972 /* Start emitting debugging information. */
974 static void
975 cp_lexer_start_debugging (cp_lexer* lexer)
977 ++lexer->debugging_p;
980 /* Stop emitting debugging information. */
982 static void
983 cp_lexer_stop_debugging (cp_lexer* lexer)
985 --lexer->debugging_p;
989 /* The parser. */
991 /* Overview
992 --------
994 A cp_parser parses the token stream as specified by the C++
995 grammar. Its job is purely parsing, not semantic analysis. For
996 example, the parser breaks the token stream into declarators,
997 expressions, statements, and other similar syntactic constructs.
998 It does not check that the types of the expressions on either side
999 of an assignment-statement are compatible, or that a function is
1000 not declared with a parameter of type `void'.
1002 The parser invokes routines elsewhere in the compiler to perform
1003 semantic analysis and to build up the abstract syntax tree for the
1004 code processed.
1006 The parser (and the template instantiation code, which is, in a
1007 way, a close relative of parsing) are the only parts of the
1008 compiler that should be calling push_scope and pop_scope, or
1009 related functions. The parser (and template instantiation code)
1010 keeps track of what scope is presently active; everything else
1011 should simply honor that. (The code that generates static
1012 initializers may also need to set the scope, in order to check
1013 access control correctly when emitting the initializers.)
1015 Methodology
1016 -----------
1018 The parser is of the standard recursive-descent variety. Upcoming
1019 tokens in the token stream are examined in order to determine which
1020 production to use when parsing a non-terminal. Some C++ constructs
1021 require arbitrary look ahead to disambiguate. For example, it is
1022 impossible, in the general case, to tell whether a statement is an
1023 expression or declaration without scanning the entire statement.
1024 Therefore, the parser is capable of "parsing tentatively." When the
1025 parser is not sure what construct comes next, it enters this mode.
1026 Then, while we attempt to parse the construct, the parser queues up
1027 error messages, rather than issuing them immediately, and saves the
1028 tokens it consumes. If the construct is parsed successfully, the
1029 parser "commits", i.e., it issues any queued error messages and
1030 the tokens that were being preserved are permanently discarded.
1031 If, however, the construct is not parsed successfully, the parser
1032 rolls back its state completely so that it can resume parsing using
1033 a different alternative.
1035 Future Improvements
1036 -------------------
1038 The performance of the parser could probably be improved
1039 substantially. Some possible improvements include:
1041 - The expression parser recurses through the various levels of
1042 precedence as specified in the grammar, rather than using an
1043 operator-precedence technique. Therefore, parsing a simple
1044 identifier requires multiple recursive calls.
1046 - We could often eliminate the need to parse tentatively by
1047 looking ahead a little bit. In some places, this approach
1048 might not entirely eliminate the need to parse tentatively, but
1049 it might still speed up the average case. */
1051 /* Flags that are passed to some parsing functions. These values can
1052 be bitwise-ored together. */
1054 typedef enum cp_parser_flags
1056 /* No flags. */
1057 CP_PARSER_FLAGS_NONE = 0x0,
1058 /* The construct is optional. If it is not present, then no error
1059 should be issued. */
1060 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1061 /* When parsing a type-specifier, do not allow user-defined types. */
1062 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1063 } cp_parser_flags;
1065 /* The different kinds of declarators we want to parse. */
1067 typedef enum cp_parser_declarator_kind
1069 /* We want an abstract declartor. */
1070 CP_PARSER_DECLARATOR_ABSTRACT,
1071 /* We want a named declarator. */
1072 CP_PARSER_DECLARATOR_NAMED,
1073 /* We don't mind, but the name must be an unqualified-id. */
1074 CP_PARSER_DECLARATOR_EITHER
1075 } cp_parser_declarator_kind;
1077 /* A mapping from a token type to a corresponding tree node type. */
1079 typedef struct cp_parser_token_tree_map_node
1081 /* The token type. */
1082 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1083 /* The corresponding tree code. */
1084 ENUM_BITFIELD (tree_code) tree_type : 8;
1085 } cp_parser_token_tree_map_node;
1087 /* A complete map consists of several ordinary entries, followed by a
1088 terminator. The terminating entry has a token_type of CPP_EOF. */
1090 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1092 /* The status of a tentative parse. */
1094 typedef enum cp_parser_status_kind
1096 /* No errors have occurred. */
1097 CP_PARSER_STATUS_KIND_NO_ERROR,
1098 /* An error has occurred. */
1099 CP_PARSER_STATUS_KIND_ERROR,
1100 /* We are committed to this tentative parse, whether or not an error
1101 has occurred. */
1102 CP_PARSER_STATUS_KIND_COMMITTED
1103 } cp_parser_status_kind;
1105 /* Context that is saved and restored when parsing tentatively. */
1107 typedef struct cp_parser_context GTY (())
1109 /* If this is a tentative parsing context, the status of the
1110 tentative parse. */
1111 enum cp_parser_status_kind status;
1112 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1113 that are looked up in this context must be looked up both in the
1114 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1115 the context of the containing expression. */
1116 tree object_type;
1117 /* The next parsing context in the stack. */
1118 struct cp_parser_context *next;
1119 } cp_parser_context;
1121 /* Prototypes. */
1123 /* Constructors and destructors. */
1125 static cp_parser_context *cp_parser_context_new
1126 (cp_parser_context *);
1128 /* Class variables. */
1130 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1132 /* Constructors and destructors. */
1134 /* Construct a new context. The context below this one on the stack
1135 is given by NEXT. */
1137 static cp_parser_context *
1138 cp_parser_context_new (cp_parser_context* next)
1140 cp_parser_context *context;
1142 /* Allocate the storage. */
1143 if (cp_parser_context_free_list != NULL)
1145 /* Pull the first entry from the free list. */
1146 context = cp_parser_context_free_list;
1147 cp_parser_context_free_list = context->next;
1148 memset (context, 0, sizeof (*context));
1150 else
1151 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1152 /* No errors have occurred yet in this context. */
1153 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1154 /* If this is not the bottomost context, copy information that we
1155 need from the previous context. */
1156 if (next)
1158 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1159 expression, then we are parsing one in this context, too. */
1160 context->object_type = next->object_type;
1161 /* Thread the stack. */
1162 context->next = next;
1165 return context;
1168 /* The cp_parser structure represents the C++ parser. */
1170 typedef struct cp_parser GTY(())
1172 /* The lexer from which we are obtaining tokens. */
1173 cp_lexer *lexer;
1175 /* The scope in which names should be looked up. If NULL_TREE, then
1176 we look up names in the scope that is currently open in the
1177 source program. If non-NULL, this is either a TYPE or
1178 NAMESPACE_DECL for the scope in which we should look.
1180 This value is not cleared automatically after a name is looked
1181 up, so we must be careful to clear it before starting a new look
1182 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1183 will look up `Z' in the scope of `X', rather than the current
1184 scope.) Unfortunately, it is difficult to tell when name lookup
1185 is complete, because we sometimes peek at a token, look it up,
1186 and then decide not to consume it. */
1187 tree scope;
1189 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1190 last lookup took place. OBJECT_SCOPE is used if an expression
1191 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1192 respectively. QUALIFYING_SCOPE is used for an expression of the
1193 form "X::Y"; it refers to X. */
1194 tree object_scope;
1195 tree qualifying_scope;
1197 /* A stack of parsing contexts. All but the bottom entry on the
1198 stack will be tentative contexts.
1200 We parse tentatively in order to determine which construct is in
1201 use in some situations. For example, in order to determine
1202 whether a statement is an expression-statement or a
1203 declaration-statement we parse it tentatively as a
1204 declaration-statement. If that fails, we then reparse the same
1205 token stream as an expression-statement. */
1206 cp_parser_context *context;
1208 /* True if we are parsing GNU C++. If this flag is not set, then
1209 GNU extensions are not recognized. */
1210 bool allow_gnu_extensions_p;
1212 /* TRUE if the `>' token should be interpreted as the greater-than
1213 operator. FALSE if it is the end of a template-id or
1214 template-parameter-list. */
1215 bool greater_than_is_operator_p;
1217 /* TRUE if default arguments are allowed within a parameter list
1218 that starts at this point. FALSE if only a gnu extension makes
1219 them permissible. */
1220 bool default_arg_ok_p;
1222 /* TRUE if we are parsing an integral constant-expression. See
1223 [expr.const] for a precise definition. */
1224 bool integral_constant_expression_p;
1226 /* TRUE if we are parsing an integral constant-expression -- but a
1227 non-constant expression should be permitted as well. This flag
1228 is used when parsing an array bound so that GNU variable-length
1229 arrays are tolerated. */
1230 bool allow_non_integral_constant_expression_p;
1232 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1233 been seen that makes the expression non-constant. */
1234 bool non_integral_constant_expression_p;
1236 /* TRUE if we are parsing the argument to "__offsetof__". */
1237 bool in_offsetof_p;
1239 /* TRUE if local variable names and `this' are forbidden in the
1240 current context. */
1241 bool local_variables_forbidden_p;
1243 /* TRUE if the declaration we are parsing is part of a
1244 linkage-specification of the form `extern string-literal
1245 declaration'. */
1246 bool in_unbraced_linkage_specification_p;
1248 /* TRUE if we are presently parsing a declarator, after the
1249 direct-declarator. */
1250 bool in_declarator_p;
1252 /* TRUE if we are presently parsing a template-argument-list. */
1253 bool in_template_argument_list_p;
1255 /* TRUE if we are presently parsing the body of an
1256 iteration-statement. */
1257 bool in_iteration_statement_p;
1259 /* TRUE if we are presently parsing the body of a switch
1260 statement. */
1261 bool in_switch_statement_p;
1263 /* TRUE if we are parsing a type-id in an expression context. In
1264 such a situation, both "type (expr)" and "type (type)" are valid
1265 alternatives. */
1266 bool in_type_id_in_expr_p;
1268 /* If non-NULL, then we are parsing a construct where new type
1269 definitions are not permitted. The string stored here will be
1270 issued as an error message if a type is defined. */
1271 const char *type_definition_forbidden_message;
1273 /* A list of lists. The outer list is a stack, used for member
1274 functions of local classes. At each level there are two sub-list,
1275 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1276 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1277 TREE_VALUE's. The functions are chained in reverse declaration
1278 order.
1280 The TREE_PURPOSE sublist contains those functions with default
1281 arguments that need post processing, and the TREE_VALUE sublist
1282 contains those functions with definitions that need post
1283 processing.
1285 These lists can only be processed once the outermost class being
1286 defined is complete. */
1287 tree unparsed_functions_queues;
1289 /* The number of classes whose definitions are currently in
1290 progress. */
1291 unsigned num_classes_being_defined;
1293 /* The number of template parameter lists that apply directly to the
1294 current declaration. */
1295 unsigned num_template_parameter_lists;
1296 } cp_parser;
1298 /* The type of a function that parses some kind of expression. */
1299 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1301 /* Prototypes. */
1303 /* Constructors and destructors. */
1305 static cp_parser *cp_parser_new
1306 (void);
1308 /* Routines to parse various constructs.
1310 Those that return `tree' will return the error_mark_node (rather
1311 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1312 Sometimes, they will return an ordinary node if error-recovery was
1313 attempted, even though a parse error occurred. So, to check
1314 whether or not a parse error occurred, you should always use
1315 cp_parser_error_occurred. If the construct is optional (indicated
1316 either by an `_opt' in the name of the function that does the
1317 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1318 the construct is not present. */
1320 /* Lexical conventions [gram.lex] */
1322 static tree cp_parser_identifier
1323 (cp_parser *);
1325 /* Basic concepts [gram.basic] */
1327 static bool cp_parser_translation_unit
1328 (cp_parser *);
1330 /* Expressions [gram.expr] */
1332 static tree cp_parser_primary_expression
1333 (cp_parser *, cp_id_kind *, tree *);
1334 static tree cp_parser_id_expression
1335 (cp_parser *, bool, bool, bool *, bool);
1336 static tree cp_parser_unqualified_id
1337 (cp_parser *, bool, bool, bool);
1338 static tree cp_parser_nested_name_specifier_opt
1339 (cp_parser *, bool, bool, bool, bool);
1340 static tree cp_parser_nested_name_specifier
1341 (cp_parser *, bool, bool, bool, bool);
1342 static tree cp_parser_class_or_namespace_name
1343 (cp_parser *, bool, bool, bool, bool, bool);
1344 static tree cp_parser_postfix_expression
1345 (cp_parser *, bool);
1346 static tree cp_parser_parenthesized_expression_list
1347 (cp_parser *, bool, bool *);
1348 static void cp_parser_pseudo_destructor_name
1349 (cp_parser *, tree *, tree *);
1350 static tree cp_parser_unary_expression
1351 (cp_parser *, bool);
1352 static enum tree_code cp_parser_unary_operator
1353 (cp_token *);
1354 static tree cp_parser_new_expression
1355 (cp_parser *);
1356 static tree cp_parser_new_placement
1357 (cp_parser *);
1358 static tree cp_parser_new_type_id
1359 (cp_parser *);
1360 static tree cp_parser_new_declarator_opt
1361 (cp_parser *);
1362 static tree cp_parser_direct_new_declarator
1363 (cp_parser *);
1364 static tree cp_parser_new_initializer
1365 (cp_parser *);
1366 static tree cp_parser_delete_expression
1367 (cp_parser *);
1368 static tree cp_parser_cast_expression
1369 (cp_parser *, bool);
1370 static tree cp_parser_pm_expression
1371 (cp_parser *);
1372 static tree cp_parser_multiplicative_expression
1373 (cp_parser *);
1374 static tree cp_parser_additive_expression
1375 (cp_parser *);
1376 static tree cp_parser_shift_expression
1377 (cp_parser *);
1378 static tree cp_parser_relational_expression
1379 (cp_parser *);
1380 static tree cp_parser_equality_expression
1381 (cp_parser *);
1382 static tree cp_parser_and_expression
1383 (cp_parser *);
1384 static tree cp_parser_exclusive_or_expression
1385 (cp_parser *);
1386 static tree cp_parser_inclusive_or_expression
1387 (cp_parser *);
1388 static tree cp_parser_logical_and_expression
1389 (cp_parser *);
1390 static tree cp_parser_logical_or_expression
1391 (cp_parser *);
1392 static tree cp_parser_question_colon_clause
1393 (cp_parser *, tree);
1394 static tree cp_parser_assignment_expression
1395 (cp_parser *);
1396 static enum tree_code cp_parser_assignment_operator_opt
1397 (cp_parser *);
1398 static tree cp_parser_expression
1399 (cp_parser *);
1400 static tree cp_parser_constant_expression
1401 (cp_parser *, bool, bool *);
1403 /* Statements [gram.stmt.stmt] */
1405 static void cp_parser_statement
1406 (cp_parser *, bool);
1407 static tree cp_parser_labeled_statement
1408 (cp_parser *, bool);
1409 static tree cp_parser_expression_statement
1410 (cp_parser *, bool);
1411 static tree cp_parser_compound_statement
1412 (cp_parser *, bool);
1413 static void cp_parser_statement_seq_opt
1414 (cp_parser *, bool);
1415 static tree cp_parser_selection_statement
1416 (cp_parser *);
1417 static tree cp_parser_condition
1418 (cp_parser *);
1419 static tree cp_parser_iteration_statement
1420 (cp_parser *);
1421 static void cp_parser_for_init_statement
1422 (cp_parser *);
1423 static tree cp_parser_jump_statement
1424 (cp_parser *);
1425 static void cp_parser_declaration_statement
1426 (cp_parser *);
1428 static tree cp_parser_implicitly_scoped_statement
1429 (cp_parser *);
1430 static void cp_parser_already_scoped_statement
1431 (cp_parser *);
1433 /* Declarations [gram.dcl.dcl] */
1435 static void cp_parser_declaration_seq_opt
1436 (cp_parser *);
1437 static void cp_parser_declaration
1438 (cp_parser *);
1439 static void cp_parser_block_declaration
1440 (cp_parser *, bool);
1441 static void cp_parser_simple_declaration
1442 (cp_parser *, bool);
1443 static tree cp_parser_decl_specifier_seq
1444 (cp_parser *, cp_parser_flags, tree *, int *);
1445 static tree cp_parser_storage_class_specifier_opt
1446 (cp_parser *);
1447 static tree cp_parser_function_specifier_opt
1448 (cp_parser *);
1449 static tree cp_parser_type_specifier
1450 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1451 static tree cp_parser_simple_type_specifier
1452 (cp_parser *, cp_parser_flags, bool);
1453 static tree cp_parser_type_name
1454 (cp_parser *);
1455 static tree cp_parser_elaborated_type_specifier
1456 (cp_parser *, bool, bool);
1457 static tree cp_parser_enum_specifier
1458 (cp_parser *);
1459 static void cp_parser_enumerator_list
1460 (cp_parser *, tree);
1461 static void cp_parser_enumerator_definition
1462 (cp_parser *, tree);
1463 static tree cp_parser_namespace_name
1464 (cp_parser *);
1465 static void cp_parser_namespace_definition
1466 (cp_parser *);
1467 static void cp_parser_namespace_body
1468 (cp_parser *);
1469 static tree cp_parser_qualified_namespace_specifier
1470 (cp_parser *);
1471 static void cp_parser_namespace_alias_definition
1472 (cp_parser *);
1473 static void cp_parser_using_declaration
1474 (cp_parser *);
1475 static void cp_parser_using_directive
1476 (cp_parser *);
1477 static void cp_parser_asm_definition
1478 (cp_parser *);
1479 static void cp_parser_linkage_specification
1480 (cp_parser *);
1482 /* Declarators [gram.dcl.decl] */
1484 static tree cp_parser_init_declarator
1485 (cp_parser *, tree, tree, bool, bool, int, bool *);
1486 static tree cp_parser_declarator
1487 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1488 static tree cp_parser_direct_declarator
1489 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1490 static enum tree_code cp_parser_ptr_operator
1491 (cp_parser *, tree *, tree *);
1492 static tree cp_parser_cv_qualifier_seq_opt
1493 (cp_parser *);
1494 static tree cp_parser_cv_qualifier_opt
1495 (cp_parser *);
1496 static tree cp_parser_declarator_id
1497 (cp_parser *);
1498 static tree cp_parser_type_id
1499 (cp_parser *);
1500 static tree cp_parser_type_specifier_seq
1501 (cp_parser *);
1502 static tree cp_parser_parameter_declaration_clause
1503 (cp_parser *);
1504 static tree cp_parser_parameter_declaration_list
1505 (cp_parser *);
1506 static tree cp_parser_parameter_declaration
1507 (cp_parser *, bool, bool *);
1508 static void cp_parser_function_body
1509 (cp_parser *);
1510 static tree cp_parser_initializer
1511 (cp_parser *, bool *, bool *);
1512 static tree cp_parser_initializer_clause
1513 (cp_parser *, bool *);
1514 static tree cp_parser_initializer_list
1515 (cp_parser *, bool *);
1517 static bool cp_parser_ctor_initializer_opt_and_function_body
1518 (cp_parser *);
1520 /* Classes [gram.class] */
1522 static tree cp_parser_class_name
1523 (cp_parser *, bool, bool, bool, bool, bool, bool);
1524 static tree cp_parser_class_specifier
1525 (cp_parser *);
1526 static tree cp_parser_class_head
1527 (cp_parser *, bool *, tree *);
1528 static enum tag_types cp_parser_class_key
1529 (cp_parser *);
1530 static void cp_parser_member_specification_opt
1531 (cp_parser *);
1532 static void cp_parser_member_declaration
1533 (cp_parser *);
1534 static tree cp_parser_pure_specifier
1535 (cp_parser *);
1536 static tree cp_parser_constant_initializer
1537 (cp_parser *);
1539 /* Derived classes [gram.class.derived] */
1541 static tree cp_parser_base_clause
1542 (cp_parser *);
1543 static tree cp_parser_base_specifier
1544 (cp_parser *);
1546 /* Special member functions [gram.special] */
1548 static tree cp_parser_conversion_function_id
1549 (cp_parser *);
1550 static tree cp_parser_conversion_type_id
1551 (cp_parser *);
1552 static tree cp_parser_conversion_declarator_opt
1553 (cp_parser *);
1554 static bool cp_parser_ctor_initializer_opt
1555 (cp_parser *);
1556 static void cp_parser_mem_initializer_list
1557 (cp_parser *);
1558 static tree cp_parser_mem_initializer
1559 (cp_parser *);
1560 static tree cp_parser_mem_initializer_id
1561 (cp_parser *);
1563 /* Overloading [gram.over] */
1565 static tree cp_parser_operator_function_id
1566 (cp_parser *);
1567 static tree cp_parser_operator
1568 (cp_parser *);
1570 /* Templates [gram.temp] */
1572 static void cp_parser_template_declaration
1573 (cp_parser *, bool);
1574 static tree cp_parser_template_parameter_list
1575 (cp_parser *);
1576 static tree cp_parser_template_parameter
1577 (cp_parser *);
1578 static tree cp_parser_type_parameter
1579 (cp_parser *);
1580 static tree cp_parser_template_id
1581 (cp_parser *, bool, bool, bool);
1582 static tree cp_parser_template_name
1583 (cp_parser *, bool, bool, bool, bool *);
1584 static tree cp_parser_template_argument_list
1585 (cp_parser *);
1586 static tree cp_parser_template_argument
1587 (cp_parser *);
1588 static void cp_parser_explicit_instantiation
1589 (cp_parser *);
1590 static void cp_parser_explicit_specialization
1591 (cp_parser *);
1593 /* Exception handling [gram.exception] */
1595 static tree cp_parser_try_block
1596 (cp_parser *);
1597 static bool cp_parser_function_try_block
1598 (cp_parser *);
1599 static void cp_parser_handler_seq
1600 (cp_parser *);
1601 static void cp_parser_handler
1602 (cp_parser *);
1603 static tree cp_parser_exception_declaration
1604 (cp_parser *);
1605 static tree cp_parser_throw_expression
1606 (cp_parser *);
1607 static tree cp_parser_exception_specification_opt
1608 (cp_parser *);
1609 static tree cp_parser_type_id_list
1610 (cp_parser *);
1612 /* GNU Extensions */
1614 static tree cp_parser_asm_specification_opt
1615 (cp_parser *);
1616 static tree cp_parser_asm_operand_list
1617 (cp_parser *);
1618 static tree cp_parser_asm_clobber_list
1619 (cp_parser *);
1620 static tree cp_parser_attributes_opt
1621 (cp_parser *);
1622 static tree cp_parser_attribute_list
1623 (cp_parser *);
1624 static bool cp_parser_extension_opt
1625 (cp_parser *, int *);
1626 static void cp_parser_label_declaration
1627 (cp_parser *);
1629 /* Utility Routines */
1631 static tree cp_parser_lookup_name
1632 (cp_parser *, tree, bool, bool, bool, bool);
1633 static tree cp_parser_lookup_name_simple
1634 (cp_parser *, tree);
1635 static tree cp_parser_maybe_treat_template_as_class
1636 (tree, bool);
1637 static bool cp_parser_check_declarator_template_parameters
1638 (cp_parser *, tree);
1639 static bool cp_parser_check_template_parameters
1640 (cp_parser *, unsigned);
1641 static tree cp_parser_simple_cast_expression
1642 (cp_parser *);
1643 static tree cp_parser_binary_expression
1644 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1645 static tree cp_parser_global_scope_opt
1646 (cp_parser *, bool);
1647 static bool cp_parser_constructor_declarator_p
1648 (cp_parser *, bool);
1649 static tree cp_parser_function_definition_from_specifiers_and_declarator
1650 (cp_parser *, tree, tree, tree);
1651 static tree cp_parser_function_definition_after_declarator
1652 (cp_parser *, bool);
1653 static void cp_parser_template_declaration_after_export
1654 (cp_parser *, bool);
1655 static tree cp_parser_single_declaration
1656 (cp_parser *, bool, bool *);
1657 static tree cp_parser_functional_cast
1658 (cp_parser *, tree);
1659 static tree cp_parser_save_member_function_body
1660 (cp_parser *, tree, tree, tree);
1661 static tree cp_parser_enclosed_template_argument_list
1662 (cp_parser *);
1663 static void cp_parser_save_default_args
1664 (cp_parser *, tree);
1665 static void cp_parser_late_parsing_for_member
1666 (cp_parser *, tree);
1667 static void cp_parser_late_parsing_default_args
1668 (cp_parser *, tree);
1669 static tree cp_parser_sizeof_operand
1670 (cp_parser *, enum rid);
1671 static bool cp_parser_declares_only_class_p
1672 (cp_parser *);
1673 static bool cp_parser_friend_p
1674 (tree);
1675 static bool cp_parser_typedef_p
1676 (tree);
1677 static cp_token *cp_parser_require
1678 (cp_parser *, enum cpp_ttype, const char *);
1679 static cp_token *cp_parser_require_keyword
1680 (cp_parser *, enum rid, const char *);
1681 static bool cp_parser_token_starts_function_definition_p
1682 (cp_token *);
1683 static bool cp_parser_next_token_starts_class_definition_p
1684 (cp_parser *);
1685 static bool cp_parser_next_token_ends_template_argument_p
1686 (cp_parser *);
1687 static bool cp_parser_nth_token_starts_template_argument_list_p
1688 (cp_parser *, size_t);
1689 static enum tag_types cp_parser_token_is_class_key
1690 (cp_token *);
1691 static void cp_parser_check_class_key
1692 (enum tag_types, tree type);
1693 static void cp_parser_check_access_in_redeclaration
1694 (tree type);
1695 static bool cp_parser_optional_template_keyword
1696 (cp_parser *);
1697 static void cp_parser_pre_parsed_nested_name_specifier
1698 (cp_parser *);
1699 static void cp_parser_cache_group
1700 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1701 static void cp_parser_parse_tentatively
1702 (cp_parser *);
1703 static void cp_parser_commit_to_tentative_parse
1704 (cp_parser *);
1705 static void cp_parser_abort_tentative_parse
1706 (cp_parser *);
1707 static bool cp_parser_parse_definitely
1708 (cp_parser *);
1709 static inline bool cp_parser_parsing_tentatively
1710 (cp_parser *);
1711 static bool cp_parser_committed_to_tentative_parse
1712 (cp_parser *);
1713 static void cp_parser_error
1714 (cp_parser *, const char *);
1715 static void cp_parser_name_lookup_error
1716 (cp_parser *, tree, tree, const char *);
1717 static bool cp_parser_simulate_error
1718 (cp_parser *);
1719 static void cp_parser_check_type_definition
1720 (cp_parser *);
1721 static void cp_parser_check_for_definition_in_return_type
1722 (tree, tree);
1723 static void cp_parser_check_for_invalid_template_id
1724 (cp_parser *, tree);
1725 static bool cp_parser_non_integral_constant_expression
1726 (cp_parser *, const char *);
1727 static bool cp_parser_diagnose_invalid_type_name
1728 (cp_parser *);
1729 static int cp_parser_skip_to_closing_parenthesis
1730 (cp_parser *, bool, bool, bool);
1731 static void cp_parser_skip_to_end_of_statement
1732 (cp_parser *);
1733 static void cp_parser_consume_semicolon_at_end_of_statement
1734 (cp_parser *);
1735 static void cp_parser_skip_to_end_of_block_or_statement
1736 (cp_parser *);
1737 static void cp_parser_skip_to_closing_brace
1738 (cp_parser *);
1739 static void cp_parser_skip_until_found
1740 (cp_parser *, enum cpp_ttype, const char *);
1741 static bool cp_parser_error_occurred
1742 (cp_parser *);
1743 static bool cp_parser_allow_gnu_extensions_p
1744 (cp_parser *);
1745 static bool cp_parser_is_string_literal
1746 (cp_token *);
1747 static bool cp_parser_is_keyword
1748 (cp_token *, enum rid);
1750 /* Returns nonzero if we are parsing tentatively. */
1752 static inline bool
1753 cp_parser_parsing_tentatively (cp_parser* parser)
1755 return parser->context->next != NULL;
1758 /* Returns nonzero if TOKEN is a string literal. */
1760 static bool
1761 cp_parser_is_string_literal (cp_token* token)
1763 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1766 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1768 static bool
1769 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1771 return token->keyword == keyword;
1774 /* Issue the indicated error MESSAGE. */
1776 static void
1777 cp_parser_error (cp_parser* parser, const char* message)
1779 /* Output the MESSAGE -- unless we're parsing tentatively. */
1780 if (!cp_parser_simulate_error (parser))
1782 cp_token *token;
1783 token = cp_lexer_peek_token (parser->lexer);
1784 c_parse_error (message,
1785 /* Because c_parser_error does not understand
1786 CPP_KEYWORD, keywords are treated like
1787 identifiers. */
1788 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1789 token->value);
1793 /* Issue an error about name-lookup failing. NAME is the
1794 IDENTIFIER_NODE DECL is the result of
1795 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1796 the thing that we hoped to find. */
1798 static void
1799 cp_parser_name_lookup_error (cp_parser* parser,
1800 tree name,
1801 tree decl,
1802 const char* desired)
1804 /* If name lookup completely failed, tell the user that NAME was not
1805 declared. */
1806 if (decl == error_mark_node)
1808 if (parser->scope && parser->scope != global_namespace)
1809 error ("`%D::%D' has not been declared",
1810 parser->scope, name);
1811 else if (parser->scope == global_namespace)
1812 error ("`::%D' has not been declared", name);
1813 else
1814 error ("`%D' has not been declared", name);
1816 else if (parser->scope && parser->scope != global_namespace)
1817 error ("`%D::%D' %s", parser->scope, name, desired);
1818 else if (parser->scope == global_namespace)
1819 error ("`::%D' %s", name, desired);
1820 else
1821 error ("`%D' %s", name, desired);
1824 /* If we are parsing tentatively, remember that an error has occurred
1825 during this tentative parse. Returns true if the error was
1826 simulated; false if a messgae should be issued by the caller. */
1828 static bool
1829 cp_parser_simulate_error (cp_parser* parser)
1831 if (cp_parser_parsing_tentatively (parser)
1832 && !cp_parser_committed_to_tentative_parse (parser))
1834 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1835 return true;
1837 return false;
1840 /* This function is called when a type is defined. If type
1841 definitions are forbidden at this point, an error message is
1842 issued. */
1844 static void
1845 cp_parser_check_type_definition (cp_parser* parser)
1847 /* If types are forbidden here, issue a message. */
1848 if (parser->type_definition_forbidden_message)
1849 /* Use `%s' to print the string in case there are any escape
1850 characters in the message. */
1851 error ("%s", parser->type_definition_forbidden_message);
1854 /* This function is called when the DECLARATOR is processed. The TYPE
1855 was a type defined in the decl-specifiers. If it is invalid to
1856 define a type in the decl-specifiers for DECLARATOR, an error is
1857 issued. */
1859 static void
1860 cp_parser_check_for_definition_in_return_type (tree declarator, tree type)
1862 /* [dcl.fct] forbids type definitions in return types.
1863 Unfortunately, it's not easy to know whether or not we are
1864 processing a return type until after the fact. */
1865 while (declarator
1866 && (TREE_CODE (declarator) == INDIRECT_REF
1867 || TREE_CODE (declarator) == ADDR_EXPR))
1868 declarator = TREE_OPERAND (declarator, 0);
1869 if (declarator
1870 && TREE_CODE (declarator) == CALL_EXPR)
1872 error ("new types may not be defined in a return type");
1873 inform ("(perhaps a semicolon is missing after the definition of `%T')",
1874 type);
1878 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1879 "<" in any valid C++ program. If the next token is indeed "<",
1880 issue a message warning the user about what appears to be an
1881 invalid attempt to form a template-id. */
1883 static void
1884 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1885 tree type)
1887 ptrdiff_t start;
1888 cp_token *token;
1890 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1892 if (TYPE_P (type))
1893 error ("`%T' is not a template", type);
1894 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1895 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1896 else
1897 error ("invalid template-id");
1898 /* Remember the location of the invalid "<". */
1899 if (cp_parser_parsing_tentatively (parser)
1900 && !cp_parser_committed_to_tentative_parse (parser))
1902 token = cp_lexer_peek_token (parser->lexer);
1903 token = cp_lexer_prev_token (parser->lexer, token);
1904 start = cp_lexer_token_difference (parser->lexer,
1905 parser->lexer->first_token,
1906 token);
1908 else
1909 start = -1;
1910 /* Consume the "<". */
1911 cp_lexer_consume_token (parser->lexer);
1912 /* Parse the template arguments. */
1913 cp_parser_enclosed_template_argument_list (parser);
1914 /* Permanently remove the invalid template arguments so that
1915 this error message is not issued again. */
1916 if (start >= 0)
1918 token = cp_lexer_advance_token (parser->lexer,
1919 parser->lexer->first_token,
1920 start);
1921 cp_lexer_purge_tokens_after (parser->lexer, token);
1926 /* If parsing an integral constant-expression, issue an error message
1927 about the fact that THING appeared and return true. Otherwise,
1928 return false, marking the current expression as non-constant. */
1930 static bool
1931 cp_parser_non_integral_constant_expression (cp_parser *parser,
1932 const char *thing)
1934 if (parser->integral_constant_expression_p)
1936 if (!parser->allow_non_integral_constant_expression_p)
1938 error ("%s cannot appear in a constant-expression", thing);
1939 return true;
1941 parser->non_integral_constant_expression_p = true;
1943 return false;
1946 /* Check for a common situation where a type-name should be present,
1947 but is not, and issue a sensible error message. Returns true if an
1948 invalid type-name was detected. */
1950 static bool
1951 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1953 /* If the next two tokens are both identifiers, the code is
1954 erroneous. The usual cause of this situation is code like:
1956 T t;
1958 where "T" should name a type -- but does not. */
1959 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1960 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1962 tree name;
1964 /* If parsing tentatively, we should commit; we really are
1965 looking at a declaration. */
1966 /* Consume the first identifier. */
1967 name = cp_lexer_consume_token (parser->lexer)->value;
1968 /* Issue an error message. */
1969 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1970 /* If we're in a template class, it's possible that the user was
1971 referring to a type from a base class. For example:
1973 template <typename T> struct A { typedef T X; };
1974 template <typename T> struct B : public A<T> { X x; };
1976 The user should have said "typename A<T>::X". */
1977 if (processing_template_decl && current_class_type)
1979 tree b;
1981 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1983 b = TREE_CHAIN (b))
1985 tree base_type = BINFO_TYPE (b);
1986 if (CLASS_TYPE_P (base_type)
1987 && dependent_type_p (base_type))
1989 tree field;
1990 /* Go from a particular instantiation of the
1991 template (which will have an empty TYPE_FIELDs),
1992 to the main version. */
1993 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1994 for (field = TYPE_FIELDS (base_type);
1995 field;
1996 field = TREE_CHAIN (field))
1997 if (TREE_CODE (field) == TYPE_DECL
1998 && DECL_NAME (field) == name)
2000 error ("(perhaps `typename %T::%s' was intended)",
2001 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
2002 break;
2004 if (field)
2005 break;
2009 /* Skip to the end of the declaration; there's no point in
2010 trying to process it. */
2011 cp_parser_skip_to_end_of_statement (parser);
2013 return true;
2016 return false;
2019 /* Consume tokens up to, and including, the next non-nested closing `)'.
2020 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2021 are doing error recovery. Returns -1 if OR_COMMA is true and we
2022 found an unnested comma. */
2024 static int
2025 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2026 bool recovering,
2027 bool or_comma,
2028 bool consume_paren)
2030 unsigned paren_depth = 0;
2031 unsigned brace_depth = 0;
2033 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2034 && !cp_parser_committed_to_tentative_parse (parser))
2035 return 0;
2037 while (true)
2039 cp_token *token;
2041 /* If we've run out of tokens, then there is no closing `)'. */
2042 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2043 return 0;
2045 token = cp_lexer_peek_token (parser->lexer);
2047 /* This matches the processing in skip_to_end_of_statement. */
2048 if (token->type == CPP_SEMICOLON && !brace_depth)
2049 return 0;
2050 if (token->type == CPP_OPEN_BRACE)
2051 ++brace_depth;
2052 if (token->type == CPP_CLOSE_BRACE)
2054 if (!brace_depth--)
2055 return 0;
2057 if (recovering && or_comma && token->type == CPP_COMMA
2058 && !brace_depth && !paren_depth)
2059 return -1;
2061 if (!brace_depth)
2063 /* If it is an `(', we have entered another level of nesting. */
2064 if (token->type == CPP_OPEN_PAREN)
2065 ++paren_depth;
2066 /* If it is a `)', then we might be done. */
2067 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2069 if (consume_paren)
2070 cp_lexer_consume_token (parser->lexer);
2071 return 1;
2075 /* Consume the token. */
2076 cp_lexer_consume_token (parser->lexer);
2080 /* Consume tokens until we reach the end of the current statement.
2081 Normally, that will be just before consuming a `;'. However, if a
2082 non-nested `}' comes first, then we stop before consuming that. */
2084 static void
2085 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2087 unsigned nesting_depth = 0;
2089 while (true)
2091 cp_token *token;
2093 /* Peek at the next token. */
2094 token = cp_lexer_peek_token (parser->lexer);
2095 /* If we've run out of tokens, stop. */
2096 if (token->type == CPP_EOF)
2097 break;
2098 /* If the next token is a `;', we have reached the end of the
2099 statement. */
2100 if (token->type == CPP_SEMICOLON && !nesting_depth)
2101 break;
2102 /* If the next token is a non-nested `}', then we have reached
2103 the end of the current block. */
2104 if (token->type == CPP_CLOSE_BRACE)
2106 /* If this is a non-nested `}', stop before consuming it.
2107 That way, when confronted with something like:
2109 { 3 + }
2111 we stop before consuming the closing `}', even though we
2112 have not yet reached a `;'. */
2113 if (nesting_depth == 0)
2114 break;
2115 /* If it is the closing `}' for a block that we have
2116 scanned, stop -- but only after consuming the token.
2117 That way given:
2119 void f g () { ... }
2120 typedef int I;
2122 we will stop after the body of the erroneously declared
2123 function, but before consuming the following `typedef'
2124 declaration. */
2125 if (--nesting_depth == 0)
2127 cp_lexer_consume_token (parser->lexer);
2128 break;
2131 /* If it the next token is a `{', then we are entering a new
2132 block. Consume the entire block. */
2133 else if (token->type == CPP_OPEN_BRACE)
2134 ++nesting_depth;
2135 /* Consume the token. */
2136 cp_lexer_consume_token (parser->lexer);
2140 /* This function is called at the end of a statement or declaration.
2141 If the next token is a semicolon, it is consumed; otherwise, error
2142 recovery is attempted. */
2144 static void
2145 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2147 /* Look for the trailing `;'. */
2148 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2150 /* If there is additional (erroneous) input, skip to the end of
2151 the statement. */
2152 cp_parser_skip_to_end_of_statement (parser);
2153 /* If the next token is now a `;', consume it. */
2154 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2155 cp_lexer_consume_token (parser->lexer);
2159 /* Skip tokens until we have consumed an entire block, or until we
2160 have consumed a non-nested `;'. */
2162 static void
2163 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2165 unsigned nesting_depth = 0;
2167 while (true)
2169 cp_token *token;
2171 /* Peek at the next token. */
2172 token = cp_lexer_peek_token (parser->lexer);
2173 /* If we've run out of tokens, stop. */
2174 if (token->type == CPP_EOF)
2175 break;
2176 /* If the next token is a `;', we have reached the end of the
2177 statement. */
2178 if (token->type == CPP_SEMICOLON && !nesting_depth)
2180 /* Consume the `;'. */
2181 cp_lexer_consume_token (parser->lexer);
2182 break;
2184 /* Consume the token. */
2185 token = cp_lexer_consume_token (parser->lexer);
2186 /* If the next token is a non-nested `}', then we have reached
2187 the end of the current block. */
2188 if (token->type == CPP_CLOSE_BRACE
2189 && (nesting_depth == 0 || --nesting_depth == 0))
2190 break;
2191 /* If it the next token is a `{', then we are entering a new
2192 block. Consume the entire block. */
2193 if (token->type == CPP_OPEN_BRACE)
2194 ++nesting_depth;
2198 /* Skip tokens until a non-nested closing curly brace is the next
2199 token. */
2201 static void
2202 cp_parser_skip_to_closing_brace (cp_parser *parser)
2204 unsigned nesting_depth = 0;
2206 while (true)
2208 cp_token *token;
2210 /* Peek at the next token. */
2211 token = cp_lexer_peek_token (parser->lexer);
2212 /* If we've run out of tokens, stop. */
2213 if (token->type == CPP_EOF)
2214 break;
2215 /* If the next token is a non-nested `}', then we have reached
2216 the end of the current block. */
2217 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2218 break;
2219 /* If it the next token is a `{', then we are entering a new
2220 block. Consume the entire block. */
2221 else if (token->type == CPP_OPEN_BRACE)
2222 ++nesting_depth;
2223 /* Consume the token. */
2224 cp_lexer_consume_token (parser->lexer);
2228 /* Create a new C++ parser. */
2230 static cp_parser *
2231 cp_parser_new (void)
2233 cp_parser *parser;
2234 cp_lexer *lexer;
2236 /* cp_lexer_new_main is called before calling ggc_alloc because
2237 cp_lexer_new_main might load a PCH file. */
2238 lexer = cp_lexer_new_main ();
2240 parser = ggc_alloc_cleared (sizeof (cp_parser));
2241 parser->lexer = lexer;
2242 parser->context = cp_parser_context_new (NULL);
2244 /* For now, we always accept GNU extensions. */
2245 parser->allow_gnu_extensions_p = 1;
2247 /* The `>' token is a greater-than operator, not the end of a
2248 template-id. */
2249 parser->greater_than_is_operator_p = true;
2251 parser->default_arg_ok_p = true;
2253 /* We are not parsing a constant-expression. */
2254 parser->integral_constant_expression_p = false;
2255 parser->allow_non_integral_constant_expression_p = false;
2256 parser->non_integral_constant_expression_p = false;
2258 /* We are not parsing offsetof. */
2259 parser->in_offsetof_p = false;
2261 /* Local variable names are not forbidden. */
2262 parser->local_variables_forbidden_p = false;
2264 /* We are not processing an `extern "C"' declaration. */
2265 parser->in_unbraced_linkage_specification_p = false;
2267 /* We are not processing a declarator. */
2268 parser->in_declarator_p = false;
2270 /* We are not processing a template-argument-list. */
2271 parser->in_template_argument_list_p = false;
2273 /* We are not in an iteration statement. */
2274 parser->in_iteration_statement_p = false;
2276 /* We are not in a switch statement. */
2277 parser->in_switch_statement_p = false;
2279 /* We are not parsing a type-id inside an expression. */
2280 parser->in_type_id_in_expr_p = false;
2282 /* The unparsed function queue is empty. */
2283 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2285 /* There are no classes being defined. */
2286 parser->num_classes_being_defined = 0;
2288 /* No template parameters apply. */
2289 parser->num_template_parameter_lists = 0;
2291 return parser;
2294 /* Lexical conventions [gram.lex] */
2296 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2297 identifier. */
2299 static tree
2300 cp_parser_identifier (cp_parser* parser)
2302 cp_token *token;
2304 /* Look for the identifier. */
2305 token = cp_parser_require (parser, CPP_NAME, "identifier");
2306 /* Return the value. */
2307 return token ? token->value : error_mark_node;
2310 /* Basic concepts [gram.basic] */
2312 /* Parse a translation-unit.
2314 translation-unit:
2315 declaration-seq [opt]
2317 Returns TRUE if all went well. */
2319 static bool
2320 cp_parser_translation_unit (cp_parser* parser)
2322 while (true)
2324 cp_parser_declaration_seq_opt (parser);
2326 /* If there are no tokens left then all went well. */
2327 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2328 break;
2330 /* Otherwise, issue an error message. */
2331 cp_parser_error (parser, "expected declaration");
2332 return false;
2335 /* Consume the EOF token. */
2336 cp_parser_require (parser, CPP_EOF, "end-of-file");
2338 /* Finish up. */
2339 finish_translation_unit ();
2341 /* All went well. */
2342 return true;
2345 /* Expressions [gram.expr] */
2347 /* Parse a primary-expression.
2349 primary-expression:
2350 literal
2351 this
2352 ( expression )
2353 id-expression
2355 GNU Extensions:
2357 primary-expression:
2358 ( compound-statement )
2359 __builtin_va_arg ( assignment-expression , type-id )
2361 literal:
2362 __null
2364 Returns a representation of the expression.
2366 *IDK indicates what kind of id-expression (if any) was present.
2368 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2369 used as the operand of a pointer-to-member. In that case,
2370 *QUALIFYING_CLASS gives the class that is used as the qualifying
2371 class in the pointer-to-member. */
2373 static tree
2374 cp_parser_primary_expression (cp_parser *parser,
2375 cp_id_kind *idk,
2376 tree *qualifying_class)
2378 cp_token *token;
2380 /* Assume the primary expression is not an id-expression. */
2381 *idk = CP_ID_KIND_NONE;
2382 /* And that it cannot be used as pointer-to-member. */
2383 *qualifying_class = NULL_TREE;
2385 /* Peek at the next token. */
2386 token = cp_lexer_peek_token (parser->lexer);
2387 switch (token->type)
2389 /* literal:
2390 integer-literal
2391 character-literal
2392 floating-literal
2393 string-literal
2394 boolean-literal */
2395 case CPP_CHAR:
2396 case CPP_WCHAR:
2397 case CPP_STRING:
2398 case CPP_WSTRING:
2399 case CPP_NUMBER:
2400 token = cp_lexer_consume_token (parser->lexer);
2401 return token->value;
2403 case CPP_OPEN_PAREN:
2405 tree expr;
2406 bool saved_greater_than_is_operator_p;
2408 /* Consume the `('. */
2409 cp_lexer_consume_token (parser->lexer);
2410 /* Within a parenthesized expression, a `>' token is always
2411 the greater-than operator. */
2412 saved_greater_than_is_operator_p
2413 = parser->greater_than_is_operator_p;
2414 parser->greater_than_is_operator_p = true;
2415 /* If we see `( { ' then we are looking at the beginning of
2416 a GNU statement-expression. */
2417 if (cp_parser_allow_gnu_extensions_p (parser)
2418 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2420 /* Statement-expressions are not allowed by the standard. */
2421 if (pedantic)
2422 pedwarn ("ISO C++ forbids braced-groups within expressions");
2424 /* And they're not allowed outside of a function-body; you
2425 cannot, for example, write:
2427 int i = ({ int j = 3; j + 1; });
2429 at class or namespace scope. */
2430 if (!at_function_scope_p ())
2431 error ("statement-expressions are allowed only inside functions");
2432 /* Start the statement-expression. */
2433 expr = begin_stmt_expr ();
2434 /* Parse the compound-statement. */
2435 cp_parser_compound_statement (parser, true);
2436 /* Finish up. */
2437 expr = finish_stmt_expr (expr, false);
2439 else
2441 /* Parse the parenthesized expression. */
2442 expr = cp_parser_expression (parser);
2443 /* Let the front end know that this expression was
2444 enclosed in parentheses. This matters in case, for
2445 example, the expression is of the form `A::B', since
2446 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2447 not. */
2448 finish_parenthesized_expr (expr);
2450 /* The `>' token might be the end of a template-id or
2451 template-parameter-list now. */
2452 parser->greater_than_is_operator_p
2453 = saved_greater_than_is_operator_p;
2454 /* Consume the `)'. */
2455 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2456 cp_parser_skip_to_end_of_statement (parser);
2458 return expr;
2461 case CPP_KEYWORD:
2462 switch (token->keyword)
2464 /* These two are the boolean literals. */
2465 case RID_TRUE:
2466 cp_lexer_consume_token (parser->lexer);
2467 return boolean_true_node;
2468 case RID_FALSE:
2469 cp_lexer_consume_token (parser->lexer);
2470 return boolean_false_node;
2472 /* The `__null' literal. */
2473 case RID_NULL:
2474 cp_lexer_consume_token (parser->lexer);
2475 return null_node;
2477 /* Recognize the `this' keyword. */
2478 case RID_THIS:
2479 cp_lexer_consume_token (parser->lexer);
2480 if (parser->local_variables_forbidden_p)
2482 error ("`this' may not be used in this context");
2483 return error_mark_node;
2485 /* Pointers cannot appear in constant-expressions. */
2486 if (cp_parser_non_integral_constant_expression (parser,
2487 "`this'"))
2488 return error_mark_node;
2489 return finish_this_expr ();
2491 /* The `operator' keyword can be the beginning of an
2492 id-expression. */
2493 case RID_OPERATOR:
2494 goto id_expression;
2496 case RID_FUNCTION_NAME:
2497 case RID_PRETTY_FUNCTION_NAME:
2498 case RID_C99_FUNCTION_NAME:
2499 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2500 __func__ are the names of variables -- but they are
2501 treated specially. Therefore, they are handled here,
2502 rather than relying on the generic id-expression logic
2503 below. Grammatically, these names are id-expressions.
2505 Consume the token. */
2506 token = cp_lexer_consume_token (parser->lexer);
2507 /* Look up the name. */
2508 return finish_fname (token->value);
2510 case RID_VA_ARG:
2512 tree expression;
2513 tree type;
2515 /* The `__builtin_va_arg' construct is used to handle
2516 `va_arg'. Consume the `__builtin_va_arg' token. */
2517 cp_lexer_consume_token (parser->lexer);
2518 /* Look for the opening `('. */
2519 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2520 /* Now, parse the assignment-expression. */
2521 expression = cp_parser_assignment_expression (parser);
2522 /* Look for the `,'. */
2523 cp_parser_require (parser, CPP_COMMA, "`,'");
2524 /* Parse the type-id. */
2525 type = cp_parser_type_id (parser);
2526 /* Look for the closing `)'. */
2527 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2528 /* Using `va_arg' in a constant-expression is not
2529 allowed. */
2530 if (cp_parser_non_integral_constant_expression (parser,
2531 "`va_arg'"))
2532 return error_mark_node;
2533 return build_x_va_arg (expression, type);
2536 case RID_OFFSETOF:
2538 tree expression;
2539 bool saved_in_offsetof_p;
2541 /* Consume the "__offsetof__" token. */
2542 cp_lexer_consume_token (parser->lexer);
2543 /* Consume the opening `('. */
2544 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2545 /* Parse the parenthesized (almost) constant-expression. */
2546 saved_in_offsetof_p = parser->in_offsetof_p;
2547 parser->in_offsetof_p = true;
2548 expression
2549 = cp_parser_constant_expression (parser,
2550 /*allow_non_constant_p=*/false,
2551 /*non_constant_p=*/NULL);
2552 parser->in_offsetof_p = saved_in_offsetof_p;
2553 /* Consume the closing ')'. */
2554 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2556 return expression;
2559 default:
2560 cp_parser_error (parser, "expected primary-expression");
2561 return error_mark_node;
2564 /* An id-expression can start with either an identifier, a
2565 `::' as the beginning of a qualified-id, or the "operator"
2566 keyword. */
2567 case CPP_NAME:
2568 case CPP_SCOPE:
2569 case CPP_TEMPLATE_ID:
2570 case CPP_NESTED_NAME_SPECIFIER:
2572 tree id_expression;
2573 tree decl;
2574 const char *error_msg;
2576 id_expression:
2577 /* Parse the id-expression. */
2578 id_expression
2579 = cp_parser_id_expression (parser,
2580 /*template_keyword_p=*/false,
2581 /*check_dependency_p=*/true,
2582 /*template_p=*/NULL,
2583 /*declarator_p=*/false);
2584 if (id_expression == error_mark_node)
2585 return error_mark_node;
2586 /* If we have a template-id, then no further lookup is
2587 required. If the template-id was for a template-class, we
2588 will sometimes have a TYPE_DECL at this point. */
2589 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2590 || TREE_CODE (id_expression) == TYPE_DECL)
2591 decl = id_expression;
2592 /* Look up the name. */
2593 else
2595 decl = cp_parser_lookup_name_simple (parser, id_expression);
2596 /* If name lookup gives us a SCOPE_REF, then the
2597 qualifying scope was dependent. Just propagate the
2598 name. */
2599 if (TREE_CODE (decl) == SCOPE_REF)
2601 if (TYPE_P (TREE_OPERAND (decl, 0)))
2602 *qualifying_class = TREE_OPERAND (decl, 0);
2603 return decl;
2605 /* Check to see if DECL is a local variable in a context
2606 where that is forbidden. */
2607 if (parser->local_variables_forbidden_p
2608 && local_variable_p (decl))
2610 /* It might be that we only found DECL because we are
2611 trying to be generous with pre-ISO scoping rules.
2612 For example, consider:
2614 int i;
2615 void g() {
2616 for (int i = 0; i < 10; ++i) {}
2617 extern void f(int j = i);
2620 Here, name look up will originally find the out
2621 of scope `i'. We need to issue a warning message,
2622 but then use the global `i'. */
2623 decl = check_for_out_of_scope_variable (decl);
2624 if (local_variable_p (decl))
2626 error ("local variable `%D' may not appear in this context",
2627 decl);
2628 return error_mark_node;
2633 decl = finish_id_expression (id_expression, decl, parser->scope,
2634 idk, qualifying_class,
2635 parser->integral_constant_expression_p,
2636 parser->allow_non_integral_constant_expression_p,
2637 &parser->non_integral_constant_expression_p,
2638 &error_msg);
2639 if (error_msg)
2640 cp_parser_error (parser, error_msg);
2641 return decl;
2644 /* Anything else is an error. */
2645 default:
2646 cp_parser_error (parser, "expected primary-expression");
2647 return error_mark_node;
2651 /* Parse an id-expression.
2653 id-expression:
2654 unqualified-id
2655 qualified-id
2657 qualified-id:
2658 :: [opt] nested-name-specifier template [opt] unqualified-id
2659 :: identifier
2660 :: operator-function-id
2661 :: template-id
2663 Return a representation of the unqualified portion of the
2664 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2665 a `::' or nested-name-specifier.
2667 Often, if the id-expression was a qualified-id, the caller will
2668 want to make a SCOPE_REF to represent the qualified-id. This
2669 function does not do this in order to avoid wastefully creating
2670 SCOPE_REFs when they are not required.
2672 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2673 `template' keyword.
2675 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2676 uninstantiated templates.
2678 If *TEMPLATE_P is non-NULL, it is set to true iff the
2679 `template' keyword is used to explicitly indicate that the entity
2680 named is a template.
2682 If DECLARATOR_P is true, the id-expression is appearing as part of
2683 a declarator, rather than as part of an expression. */
2685 static tree
2686 cp_parser_id_expression (cp_parser *parser,
2687 bool template_keyword_p,
2688 bool check_dependency_p,
2689 bool *template_p,
2690 bool declarator_p)
2692 bool global_scope_p;
2693 bool nested_name_specifier_p;
2695 /* Assume the `template' keyword was not used. */
2696 if (template_p)
2697 *template_p = false;
2699 /* Look for the optional `::' operator. */
2700 global_scope_p
2701 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2702 != NULL_TREE);
2703 /* Look for the optional nested-name-specifier. */
2704 nested_name_specifier_p
2705 = (cp_parser_nested_name_specifier_opt (parser,
2706 /*typename_keyword_p=*/false,
2707 check_dependency_p,
2708 /*type_p=*/false,
2709 declarator_p)
2710 != NULL_TREE);
2711 /* If there is a nested-name-specifier, then we are looking at
2712 the first qualified-id production. */
2713 if (nested_name_specifier_p)
2715 tree saved_scope;
2716 tree saved_object_scope;
2717 tree saved_qualifying_scope;
2718 tree unqualified_id;
2719 bool is_template;
2721 /* See if the next token is the `template' keyword. */
2722 if (!template_p)
2723 template_p = &is_template;
2724 *template_p = cp_parser_optional_template_keyword (parser);
2725 /* Name lookup we do during the processing of the
2726 unqualified-id might obliterate SCOPE. */
2727 saved_scope = parser->scope;
2728 saved_object_scope = parser->object_scope;
2729 saved_qualifying_scope = parser->qualifying_scope;
2730 /* Process the final unqualified-id. */
2731 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2732 check_dependency_p,
2733 declarator_p);
2734 /* Restore the SAVED_SCOPE for our caller. */
2735 parser->scope = saved_scope;
2736 parser->object_scope = saved_object_scope;
2737 parser->qualifying_scope = saved_qualifying_scope;
2739 return unqualified_id;
2741 /* Otherwise, if we are in global scope, then we are looking at one
2742 of the other qualified-id productions. */
2743 else if (global_scope_p)
2745 cp_token *token;
2746 tree id;
2748 /* Peek at the next token. */
2749 token = cp_lexer_peek_token (parser->lexer);
2751 /* If it's an identifier, and the next token is not a "<", then
2752 we can avoid the template-id case. This is an optimization
2753 for this common case. */
2754 if (token->type == CPP_NAME
2755 && !cp_parser_nth_token_starts_template_argument_list_p
2756 (parser, 2))
2757 return cp_parser_identifier (parser);
2759 cp_parser_parse_tentatively (parser);
2760 /* Try a template-id. */
2761 id = cp_parser_template_id (parser,
2762 /*template_keyword_p=*/false,
2763 /*check_dependency_p=*/true,
2764 declarator_p);
2765 /* If that worked, we're done. */
2766 if (cp_parser_parse_definitely (parser))
2767 return id;
2769 /* Peek at the next token. (Changes in the token buffer may
2770 have invalidated the pointer obtained above.) */
2771 token = cp_lexer_peek_token (parser->lexer);
2773 switch (token->type)
2775 case CPP_NAME:
2776 return cp_parser_identifier (parser);
2778 case CPP_KEYWORD:
2779 if (token->keyword == RID_OPERATOR)
2780 return cp_parser_operator_function_id (parser);
2781 /* Fall through. */
2783 default:
2784 cp_parser_error (parser, "expected id-expression");
2785 return error_mark_node;
2788 else
2789 return cp_parser_unqualified_id (parser, template_keyword_p,
2790 /*check_dependency_p=*/true,
2791 declarator_p);
2794 /* Parse an unqualified-id.
2796 unqualified-id:
2797 identifier
2798 operator-function-id
2799 conversion-function-id
2800 ~ class-name
2801 template-id
2803 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2804 keyword, in a construct like `A::template ...'.
2806 Returns a representation of unqualified-id. For the `identifier'
2807 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2808 production a BIT_NOT_EXPR is returned; the operand of the
2809 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2810 other productions, see the documentation accompanying the
2811 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2812 names are looked up in uninstantiated templates. If DECLARATOR_P
2813 is true, the unqualified-id is appearing as part of a declarator,
2814 rather than as part of an expression. */
2816 static tree
2817 cp_parser_unqualified_id (cp_parser* parser,
2818 bool template_keyword_p,
2819 bool check_dependency_p,
2820 bool declarator_p)
2822 cp_token *token;
2824 /* Peek at the next token. */
2825 token = cp_lexer_peek_token (parser->lexer);
2827 switch (token->type)
2829 case CPP_NAME:
2831 tree id;
2833 /* We don't know yet whether or not this will be a
2834 template-id. */
2835 cp_parser_parse_tentatively (parser);
2836 /* Try a template-id. */
2837 id = cp_parser_template_id (parser, template_keyword_p,
2838 check_dependency_p,
2839 declarator_p);
2840 /* If it worked, we're done. */
2841 if (cp_parser_parse_definitely (parser))
2842 return id;
2843 /* Otherwise, it's an ordinary identifier. */
2844 return cp_parser_identifier (parser);
2847 case CPP_TEMPLATE_ID:
2848 return cp_parser_template_id (parser, template_keyword_p,
2849 check_dependency_p,
2850 declarator_p);
2852 case CPP_COMPL:
2854 tree type_decl;
2855 tree qualifying_scope;
2856 tree object_scope;
2857 tree scope;
2858 bool done;
2860 /* Consume the `~' token. */
2861 cp_lexer_consume_token (parser->lexer);
2862 /* Parse the class-name. The standard, as written, seems to
2863 say that:
2865 template <typename T> struct S { ~S (); };
2866 template <typename T> S<T>::~S() {}
2868 is invalid, since `~' must be followed by a class-name, but
2869 `S<T>' is dependent, and so not known to be a class.
2870 That's not right; we need to look in uninstantiated
2871 templates. A further complication arises from:
2873 template <typename T> void f(T t) {
2874 t.T::~T();
2877 Here, it is not possible to look up `T' in the scope of `T'
2878 itself. We must look in both the current scope, and the
2879 scope of the containing complete expression.
2881 Yet another issue is:
2883 struct S {
2884 int S;
2885 ~S();
2888 S::~S() {}
2890 The standard does not seem to say that the `S' in `~S'
2891 should refer to the type `S' and not the data member
2892 `S::S'. */
2894 /* DR 244 says that we look up the name after the "~" in the
2895 same scope as we looked up the qualifying name. That idea
2896 isn't fully worked out; it's more complicated than that. */
2897 scope = parser->scope;
2898 object_scope = parser->object_scope;
2899 qualifying_scope = parser->qualifying_scope;
2901 /* If the name is of the form "X::~X" it's OK. */
2902 if (scope && TYPE_P (scope)
2903 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2904 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2905 == CPP_OPEN_PAREN)
2906 && (cp_lexer_peek_token (parser->lexer)->value
2907 == TYPE_IDENTIFIER (scope)))
2909 cp_lexer_consume_token (parser->lexer);
2910 return build_nt (BIT_NOT_EXPR, scope);
2913 /* If there was an explicit qualification (S::~T), first look
2914 in the scope given by the qualification (i.e., S). */
2915 done = false;
2916 type_decl = NULL_TREE;
2917 if (scope)
2919 cp_parser_parse_tentatively (parser);
2920 type_decl = cp_parser_class_name (parser,
2921 /*typename_keyword_p=*/false,
2922 /*template_keyword_p=*/false,
2923 /*type_p=*/false,
2924 /*check_dependency=*/false,
2925 /*class_head_p=*/false,
2926 declarator_p);
2927 if (cp_parser_parse_definitely (parser))
2928 done = true;
2930 /* In "N::S::~S", look in "N" as well. */
2931 if (!done && scope && qualifying_scope)
2933 cp_parser_parse_tentatively (parser);
2934 parser->scope = qualifying_scope;
2935 parser->object_scope = NULL_TREE;
2936 parser->qualifying_scope = NULL_TREE;
2937 type_decl
2938 = cp_parser_class_name (parser,
2939 /*typename_keyword_p=*/false,
2940 /*template_keyword_p=*/false,
2941 /*type_p=*/false,
2942 /*check_dependency=*/false,
2943 /*class_head_p=*/false,
2944 declarator_p);
2945 if (cp_parser_parse_definitely (parser))
2946 done = true;
2948 /* In "p->S::~T", look in the scope given by "*p" as well. */
2949 else if (!done && object_scope)
2951 cp_parser_parse_tentatively (parser);
2952 parser->scope = object_scope;
2953 parser->object_scope = NULL_TREE;
2954 parser->qualifying_scope = NULL_TREE;
2955 type_decl
2956 = cp_parser_class_name (parser,
2957 /*typename_keyword_p=*/false,
2958 /*template_keyword_p=*/false,
2959 /*type_p=*/false,
2960 /*check_dependency=*/false,
2961 /*class_head_p=*/false,
2962 declarator_p);
2963 if (cp_parser_parse_definitely (parser))
2964 done = true;
2966 /* Look in the surrounding context. */
2967 if (!done)
2969 parser->scope = NULL_TREE;
2970 parser->object_scope = NULL_TREE;
2971 parser->qualifying_scope = NULL_TREE;
2972 type_decl
2973 = cp_parser_class_name (parser,
2974 /*typename_keyword_p=*/false,
2975 /*template_keyword_p=*/false,
2976 /*type_p=*/false,
2977 /*check_dependency=*/false,
2978 /*class_head_p=*/false,
2979 declarator_p);
2981 /* If an error occurred, assume that the name of the
2982 destructor is the same as the name of the qualifying
2983 class. That allows us to keep parsing after running
2984 into ill-formed destructor names. */
2985 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2986 return build_nt (BIT_NOT_EXPR, scope);
2987 else if (type_decl == error_mark_node)
2988 return error_mark_node;
2990 /* [class.dtor]
2992 A typedef-name that names a class shall not be used as the
2993 identifier in the declarator for a destructor declaration. */
2994 if (declarator_p
2995 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2996 && !DECL_SELF_REFERENCE_P (type_decl))
2997 error ("typedef-name `%D' used as destructor declarator",
2998 type_decl);
3000 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3003 case CPP_KEYWORD:
3004 if (token->keyword == RID_OPERATOR)
3006 tree id;
3008 /* This could be a template-id, so we try that first. */
3009 cp_parser_parse_tentatively (parser);
3010 /* Try a template-id. */
3011 id = cp_parser_template_id (parser, template_keyword_p,
3012 /*check_dependency_p=*/true,
3013 declarator_p);
3014 /* If that worked, we're done. */
3015 if (cp_parser_parse_definitely (parser))
3016 return id;
3017 /* We still don't know whether we're looking at an
3018 operator-function-id or a conversion-function-id. */
3019 cp_parser_parse_tentatively (parser);
3020 /* Try an operator-function-id. */
3021 id = cp_parser_operator_function_id (parser);
3022 /* If that didn't work, try a conversion-function-id. */
3023 if (!cp_parser_parse_definitely (parser))
3024 id = cp_parser_conversion_function_id (parser);
3026 return id;
3028 /* Fall through. */
3030 default:
3031 cp_parser_error (parser, "expected unqualified-id");
3032 return error_mark_node;
3036 /* Parse an (optional) nested-name-specifier.
3038 nested-name-specifier:
3039 class-or-namespace-name :: nested-name-specifier [opt]
3040 class-or-namespace-name :: template nested-name-specifier [opt]
3042 PARSER->SCOPE should be set appropriately before this function is
3043 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3044 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3045 in name lookups.
3047 Sets PARSER->SCOPE to the class (TYPE) or namespace
3048 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3049 it unchanged if there is no nested-name-specifier. Returns the new
3050 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3052 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3053 part of a declaration and/or decl-specifier. */
3055 static tree
3056 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3057 bool typename_keyword_p,
3058 bool check_dependency_p,
3059 bool type_p,
3060 bool is_declaration)
3062 bool success = false;
3063 tree access_check = NULL_TREE;
3064 ptrdiff_t start;
3065 cp_token* token;
3067 /* If the next token corresponds to a nested name specifier, there
3068 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3069 false, it may have been true before, in which case something
3070 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3071 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3072 CHECK_DEPENDENCY_P is false, we have to fall through into the
3073 main loop. */
3074 if (check_dependency_p
3075 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3077 cp_parser_pre_parsed_nested_name_specifier (parser);
3078 return parser->scope;
3081 /* Remember where the nested-name-specifier starts. */
3082 if (cp_parser_parsing_tentatively (parser)
3083 && !cp_parser_committed_to_tentative_parse (parser))
3085 token = cp_lexer_peek_token (parser->lexer);
3086 start = cp_lexer_token_difference (parser->lexer,
3087 parser->lexer->first_token,
3088 token);
3090 else
3091 start = -1;
3093 push_deferring_access_checks (dk_deferred);
3095 while (true)
3097 tree new_scope;
3098 tree old_scope;
3099 tree saved_qualifying_scope;
3100 bool template_keyword_p;
3102 /* Spot cases that cannot be the beginning of a
3103 nested-name-specifier. */
3104 token = cp_lexer_peek_token (parser->lexer);
3106 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3107 the already parsed nested-name-specifier. */
3108 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3110 /* Grab the nested-name-specifier and continue the loop. */
3111 cp_parser_pre_parsed_nested_name_specifier (parser);
3112 success = true;
3113 continue;
3116 /* Spot cases that cannot be the beginning of a
3117 nested-name-specifier. On the second and subsequent times
3118 through the loop, we look for the `template' keyword. */
3119 if (success && token->keyword == RID_TEMPLATE)
3121 /* A template-id can start a nested-name-specifier. */
3122 else if (token->type == CPP_TEMPLATE_ID)
3124 else
3126 /* If the next token is not an identifier, then it is
3127 definitely not a class-or-namespace-name. */
3128 if (token->type != CPP_NAME)
3129 break;
3130 /* If the following token is neither a `<' (to begin a
3131 template-id), nor a `::', then we are not looking at a
3132 nested-name-specifier. */
3133 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3134 if (token->type != CPP_SCOPE
3135 && !cp_parser_nth_token_starts_template_argument_list_p
3136 (parser, 2))
3137 break;
3140 /* The nested-name-specifier is optional, so we parse
3141 tentatively. */
3142 cp_parser_parse_tentatively (parser);
3144 /* Look for the optional `template' keyword, if this isn't the
3145 first time through the loop. */
3146 if (success)
3147 template_keyword_p = cp_parser_optional_template_keyword (parser);
3148 else
3149 template_keyword_p = false;
3151 /* Save the old scope since the name lookup we are about to do
3152 might destroy it. */
3153 old_scope = parser->scope;
3154 saved_qualifying_scope = parser->qualifying_scope;
3155 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3156 look up names in "X<T>::I" in order to determine that "Y" is
3157 a template. So, if we have a typename at this point, we make
3158 an effort to look through it. */
3159 if (is_declaration
3160 && !typename_keyword_p
3161 && parser->scope
3162 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3163 parser->scope = resolve_typename_type (parser->scope,
3164 /*only_current_p=*/false);
3165 /* Parse the qualifying entity. */
3166 new_scope
3167 = cp_parser_class_or_namespace_name (parser,
3168 typename_keyword_p,
3169 template_keyword_p,
3170 check_dependency_p,
3171 type_p,
3172 is_declaration);
3173 /* Look for the `::' token. */
3174 cp_parser_require (parser, CPP_SCOPE, "`::'");
3176 /* If we found what we wanted, we keep going; otherwise, we're
3177 done. */
3178 if (!cp_parser_parse_definitely (parser))
3180 bool error_p = false;
3182 /* Restore the OLD_SCOPE since it was valid before the
3183 failed attempt at finding the last
3184 class-or-namespace-name. */
3185 parser->scope = old_scope;
3186 parser->qualifying_scope = saved_qualifying_scope;
3187 /* If the next token is an identifier, and the one after
3188 that is a `::', then any valid interpretation would have
3189 found a class-or-namespace-name. */
3190 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3191 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3192 == CPP_SCOPE)
3193 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3194 != CPP_COMPL))
3196 token = cp_lexer_consume_token (parser->lexer);
3197 if (!error_p)
3199 tree decl;
3201 decl = cp_parser_lookup_name_simple (parser, token->value);
3202 if (TREE_CODE (decl) == TEMPLATE_DECL)
3203 error ("`%D' used without template parameters",
3204 decl);
3205 else
3206 cp_parser_name_lookup_error
3207 (parser, token->value, decl,
3208 "is not a class or namespace");
3209 parser->scope = NULL_TREE;
3210 error_p = true;
3211 /* Treat this as a successful nested-name-specifier
3212 due to:
3214 [basic.lookup.qual]
3216 If the name found is not a class-name (clause
3217 _class_) or namespace-name (_namespace.def_), the
3218 program is ill-formed. */
3219 success = true;
3221 cp_lexer_consume_token (parser->lexer);
3223 break;
3226 /* We've found one valid nested-name-specifier. */
3227 success = true;
3228 /* Make sure we look in the right scope the next time through
3229 the loop. */
3230 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3231 ? TREE_TYPE (new_scope)
3232 : new_scope);
3233 /* If it is a class scope, try to complete it; we are about to
3234 be looking up names inside the class. */
3235 if (TYPE_P (parser->scope)
3236 /* Since checking types for dependency can be expensive,
3237 avoid doing it if the type is already complete. */
3238 && !COMPLETE_TYPE_P (parser->scope)
3239 /* Do not try to complete dependent types. */
3240 && !dependent_type_p (parser->scope))
3241 complete_type (parser->scope);
3244 /* Retrieve any deferred checks. Do not pop this access checks yet
3245 so the memory will not be reclaimed during token replacing below. */
3246 access_check = get_deferred_access_checks ();
3248 /* If parsing tentatively, replace the sequence of tokens that makes
3249 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3250 token. That way, should we re-parse the token stream, we will
3251 not have to repeat the effort required to do the parse, nor will
3252 we issue duplicate error messages. */
3253 if (success && start >= 0)
3255 /* Find the token that corresponds to the start of the
3256 template-id. */
3257 token = cp_lexer_advance_token (parser->lexer,
3258 parser->lexer->first_token,
3259 start);
3261 /* Reset the contents of the START token. */
3262 token->type = CPP_NESTED_NAME_SPECIFIER;
3263 token->value = build_tree_list (access_check, parser->scope);
3264 TREE_TYPE (token->value) = parser->qualifying_scope;
3265 token->keyword = RID_MAX;
3266 /* Purge all subsequent tokens. */
3267 cp_lexer_purge_tokens_after (parser->lexer, token);
3270 pop_deferring_access_checks ();
3271 return success ? parser->scope : NULL_TREE;
3274 /* Parse a nested-name-specifier. See
3275 cp_parser_nested_name_specifier_opt for details. This function
3276 behaves identically, except that it will an issue an error if no
3277 nested-name-specifier is present, and it will return
3278 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3279 is present. */
3281 static tree
3282 cp_parser_nested_name_specifier (cp_parser *parser,
3283 bool typename_keyword_p,
3284 bool check_dependency_p,
3285 bool type_p,
3286 bool is_declaration)
3288 tree scope;
3290 /* Look for the nested-name-specifier. */
3291 scope = cp_parser_nested_name_specifier_opt (parser,
3292 typename_keyword_p,
3293 check_dependency_p,
3294 type_p,
3295 is_declaration);
3296 /* If it was not present, issue an error message. */
3297 if (!scope)
3299 cp_parser_error (parser, "expected nested-name-specifier");
3300 parser->scope = NULL_TREE;
3301 return error_mark_node;
3304 return scope;
3307 /* Parse a class-or-namespace-name.
3309 class-or-namespace-name:
3310 class-name
3311 namespace-name
3313 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3314 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3315 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3316 TYPE_P is TRUE iff the next name should be taken as a class-name,
3317 even the same name is declared to be another entity in the same
3318 scope.
3320 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3321 specified by the class-or-namespace-name. If neither is found the
3322 ERROR_MARK_NODE is returned. */
3324 static tree
3325 cp_parser_class_or_namespace_name (cp_parser *parser,
3326 bool typename_keyword_p,
3327 bool template_keyword_p,
3328 bool check_dependency_p,
3329 bool type_p,
3330 bool is_declaration)
3332 tree saved_scope;
3333 tree saved_qualifying_scope;
3334 tree saved_object_scope;
3335 tree scope;
3336 bool only_class_p;
3338 /* Before we try to parse the class-name, we must save away the
3339 current PARSER->SCOPE since cp_parser_class_name will destroy
3340 it. */
3341 saved_scope = parser->scope;
3342 saved_qualifying_scope = parser->qualifying_scope;
3343 saved_object_scope = parser->object_scope;
3344 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3345 there is no need to look for a namespace-name. */
3346 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3347 if (!only_class_p)
3348 cp_parser_parse_tentatively (parser);
3349 scope = cp_parser_class_name (parser,
3350 typename_keyword_p,
3351 template_keyword_p,
3352 type_p,
3353 check_dependency_p,
3354 /*class_head_p=*/false,
3355 is_declaration);
3356 /* If that didn't work, try for a namespace-name. */
3357 if (!only_class_p && !cp_parser_parse_definitely (parser))
3359 /* Restore the saved scope. */
3360 parser->scope = saved_scope;
3361 parser->qualifying_scope = saved_qualifying_scope;
3362 parser->object_scope = saved_object_scope;
3363 /* If we are not looking at an identifier followed by the scope
3364 resolution operator, then this is not part of a
3365 nested-name-specifier. (Note that this function is only used
3366 to parse the components of a nested-name-specifier.) */
3367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3368 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3369 return error_mark_node;
3370 scope = cp_parser_namespace_name (parser);
3373 return scope;
3376 /* Parse a postfix-expression.
3378 postfix-expression:
3379 primary-expression
3380 postfix-expression [ expression ]
3381 postfix-expression ( expression-list [opt] )
3382 simple-type-specifier ( expression-list [opt] )
3383 typename :: [opt] nested-name-specifier identifier
3384 ( expression-list [opt] )
3385 typename :: [opt] nested-name-specifier template [opt] template-id
3386 ( expression-list [opt] )
3387 postfix-expression . template [opt] id-expression
3388 postfix-expression -> template [opt] id-expression
3389 postfix-expression . pseudo-destructor-name
3390 postfix-expression -> pseudo-destructor-name
3391 postfix-expression ++
3392 postfix-expression --
3393 dynamic_cast < type-id > ( expression )
3394 static_cast < type-id > ( expression )
3395 reinterpret_cast < type-id > ( expression )
3396 const_cast < type-id > ( expression )
3397 typeid ( expression )
3398 typeid ( type-id )
3400 GNU Extension:
3402 postfix-expression:
3403 ( type-id ) { initializer-list , [opt] }
3405 This extension is a GNU version of the C99 compound-literal
3406 construct. (The C99 grammar uses `type-name' instead of `type-id',
3407 but they are essentially the same concept.)
3409 If ADDRESS_P is true, the postfix expression is the operand of the
3410 `&' operator.
3412 Returns a representation of the expression. */
3414 static tree
3415 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3417 cp_token *token;
3418 enum rid keyword;
3419 cp_id_kind idk = CP_ID_KIND_NONE;
3420 tree postfix_expression = NULL_TREE;
3421 /* Non-NULL only if the current postfix-expression can be used to
3422 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3423 class used to qualify the member. */
3424 tree qualifying_class = NULL_TREE;
3426 /* Peek at the next token. */
3427 token = cp_lexer_peek_token (parser->lexer);
3428 /* Some of the productions are determined by keywords. */
3429 keyword = token->keyword;
3430 switch (keyword)
3432 case RID_DYNCAST:
3433 case RID_STATCAST:
3434 case RID_REINTCAST:
3435 case RID_CONSTCAST:
3437 tree type;
3438 tree expression;
3439 const char *saved_message;
3441 /* All of these can be handled in the same way from the point
3442 of view of parsing. Begin by consuming the token
3443 identifying the cast. */
3444 cp_lexer_consume_token (parser->lexer);
3446 /* New types cannot be defined in the cast. */
3447 saved_message = parser->type_definition_forbidden_message;
3448 parser->type_definition_forbidden_message
3449 = "types may not be defined in casts";
3451 /* Look for the opening `<'. */
3452 cp_parser_require (parser, CPP_LESS, "`<'");
3453 /* Parse the type to which we are casting. */
3454 type = cp_parser_type_id (parser);
3455 /* Look for the closing `>'. */
3456 cp_parser_require (parser, CPP_GREATER, "`>'");
3457 /* Restore the old message. */
3458 parser->type_definition_forbidden_message = saved_message;
3460 /* And the expression which is being cast. */
3461 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3462 expression = cp_parser_expression (parser);
3463 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3465 /* Only type conversions to integral or enumeration types
3466 can be used in constant-expressions. */
3467 if (parser->integral_constant_expression_p
3468 && !dependent_type_p (type)
3469 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3470 /* A cast to pointer or reference type is allowed in the
3471 implementation of "offsetof". */
3472 && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3473 && (cp_parser_non_integral_constant_expression
3474 (parser,
3475 "a cast to a type other than an integral or "
3476 "enumeration type")))
3477 return error_mark_node;
3479 switch (keyword)
3481 case RID_DYNCAST:
3482 postfix_expression
3483 = build_dynamic_cast (type, expression);
3484 break;
3485 case RID_STATCAST:
3486 postfix_expression
3487 = build_static_cast (type, expression);
3488 break;
3489 case RID_REINTCAST:
3490 postfix_expression
3491 = build_reinterpret_cast (type, expression);
3492 break;
3493 case RID_CONSTCAST:
3494 postfix_expression
3495 = build_const_cast (type, expression);
3496 break;
3497 default:
3498 abort ();
3501 break;
3503 case RID_TYPEID:
3505 tree type;
3506 const char *saved_message;
3507 bool saved_in_type_id_in_expr_p;
3509 /* Consume the `typeid' token. */
3510 cp_lexer_consume_token (parser->lexer);
3511 /* Look for the `(' token. */
3512 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3513 /* Types cannot be defined in a `typeid' expression. */
3514 saved_message = parser->type_definition_forbidden_message;
3515 parser->type_definition_forbidden_message
3516 = "types may not be defined in a `typeid\' expression";
3517 /* We can't be sure yet whether we're looking at a type-id or an
3518 expression. */
3519 cp_parser_parse_tentatively (parser);
3520 /* Try a type-id first. */
3521 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3522 parser->in_type_id_in_expr_p = true;
3523 type = cp_parser_type_id (parser);
3524 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3525 /* Look for the `)' token. Otherwise, we can't be sure that
3526 we're not looking at an expression: consider `typeid (int
3527 (3))', for example. */
3528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3529 /* If all went well, simply lookup the type-id. */
3530 if (cp_parser_parse_definitely (parser))
3531 postfix_expression = get_typeid (type);
3532 /* Otherwise, fall back to the expression variant. */
3533 else
3535 tree expression;
3537 /* Look for an expression. */
3538 expression = cp_parser_expression (parser);
3539 /* Compute its typeid. */
3540 postfix_expression = build_typeid (expression);
3541 /* Look for the `)' token. */
3542 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3544 /* `typeid' may not appear in an integral constant expression. */
3545 if (cp_parser_non_integral_constant_expression(parser,
3546 "`typeid' operator"))
3547 return error_mark_node;
3548 /* Restore the saved message. */
3549 parser->type_definition_forbidden_message = saved_message;
3551 break;
3553 case RID_TYPENAME:
3555 tree type;
3557 /* The syntax permitted here is the same permitted for an
3558 elaborated-type-specifier. */
3559 type = cp_parser_elaborated_type_specifier (parser,
3560 /*is_friend=*/false,
3561 /*is_declaration=*/false);
3562 postfix_expression = cp_parser_functional_cast (parser, type);
3564 break;
3566 default:
3568 tree type;
3570 /* If the next thing is a simple-type-specifier, we may be
3571 looking at a functional cast. We could also be looking at
3572 an id-expression. So, we try the functional cast, and if
3573 that doesn't work we fall back to the primary-expression. */
3574 cp_parser_parse_tentatively (parser);
3575 /* Look for the simple-type-specifier. */
3576 type = cp_parser_simple_type_specifier (parser,
3577 CP_PARSER_FLAGS_NONE,
3578 /*identifier_p=*/false);
3579 /* Parse the cast itself. */
3580 if (!cp_parser_error_occurred (parser))
3581 postfix_expression
3582 = cp_parser_functional_cast (parser, type);
3583 /* If that worked, we're done. */
3584 if (cp_parser_parse_definitely (parser))
3585 break;
3587 /* If the functional-cast didn't work out, try a
3588 compound-literal. */
3589 if (cp_parser_allow_gnu_extensions_p (parser)
3590 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3592 tree initializer_list = NULL_TREE;
3593 bool saved_in_type_id_in_expr_p;
3595 cp_parser_parse_tentatively (parser);
3596 /* Consume the `('. */
3597 cp_lexer_consume_token (parser->lexer);
3598 /* Parse the type. */
3599 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3600 parser->in_type_id_in_expr_p = true;
3601 type = cp_parser_type_id (parser);
3602 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3603 /* Look for the `)'. */
3604 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3605 /* Look for the `{'. */
3606 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3607 /* If things aren't going well, there's no need to
3608 keep going. */
3609 if (!cp_parser_error_occurred (parser))
3611 bool non_constant_p;
3612 /* Parse the initializer-list. */
3613 initializer_list
3614 = cp_parser_initializer_list (parser, &non_constant_p);
3615 /* Allow a trailing `,'. */
3616 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3617 cp_lexer_consume_token (parser->lexer);
3618 /* Look for the final `}'. */
3619 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3621 /* If that worked, we're definitely looking at a
3622 compound-literal expression. */
3623 if (cp_parser_parse_definitely (parser))
3625 /* Warn the user that a compound literal is not
3626 allowed in standard C++. */
3627 if (pedantic)
3628 pedwarn ("ISO C++ forbids compound-literals");
3629 /* Form the representation of the compound-literal. */
3630 postfix_expression
3631 = finish_compound_literal (type, initializer_list);
3632 break;
3636 /* It must be a primary-expression. */
3637 postfix_expression = cp_parser_primary_expression (parser,
3638 &idk,
3639 &qualifying_class);
3641 break;
3644 /* If we were avoiding committing to the processing of a
3645 qualified-id until we knew whether or not we had a
3646 pointer-to-member, we now know. */
3647 if (qualifying_class)
3649 bool done;
3651 /* Peek at the next token. */
3652 token = cp_lexer_peek_token (parser->lexer);
3653 done = (token->type != CPP_OPEN_SQUARE
3654 && token->type != CPP_OPEN_PAREN
3655 && token->type != CPP_DOT
3656 && token->type != CPP_DEREF
3657 && token->type != CPP_PLUS_PLUS
3658 && token->type != CPP_MINUS_MINUS);
3660 postfix_expression = finish_qualified_id_expr (qualifying_class,
3661 postfix_expression,
3662 done,
3663 address_p);
3664 if (done)
3665 return postfix_expression;
3668 /* Keep looping until the postfix-expression is complete. */
3669 while (true)
3671 if (idk == CP_ID_KIND_UNQUALIFIED
3672 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3673 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3674 /* It is not a Koenig lookup function call. */
3675 postfix_expression
3676 = unqualified_name_lookup_error (postfix_expression);
3678 /* Peek at the next token. */
3679 token = cp_lexer_peek_token (parser->lexer);
3681 switch (token->type)
3683 case CPP_OPEN_SQUARE:
3684 /* postfix-expression [ expression ] */
3686 tree index;
3688 /* Consume the `[' token. */
3689 cp_lexer_consume_token (parser->lexer);
3690 /* Parse the index expression. */
3691 index = cp_parser_expression (parser);
3692 /* Look for the closing `]'. */
3693 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3695 /* Build the ARRAY_REF. */
3696 postfix_expression
3697 = grok_array_decl (postfix_expression, index);
3698 idk = CP_ID_KIND_NONE;
3699 /* Array references are not permitted in
3700 constant-expressions (but they are allowed
3701 in offsetof). */
3702 if (!parser->in_offsetof_p
3703 && cp_parser_non_integral_constant_expression
3704 (parser, "an array reference"))
3705 postfix_expression = error_mark_node;
3707 break;
3709 case CPP_OPEN_PAREN:
3710 /* postfix-expression ( expression-list [opt] ) */
3712 bool koenig_p;
3713 tree args = (cp_parser_parenthesized_expression_list
3714 (parser, false, /*non_constant_p=*/NULL));
3716 if (args == error_mark_node)
3718 postfix_expression = error_mark_node;
3719 break;
3722 /* Function calls are not permitted in
3723 constant-expressions. */
3724 if (cp_parser_non_integral_constant_expression (parser,
3725 "a function call"))
3727 postfix_expression = error_mark_node;
3728 break;
3731 koenig_p = false;
3732 if (idk == CP_ID_KIND_UNQUALIFIED)
3734 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3736 if (args)
3738 koenig_p = true;
3739 postfix_expression
3740 = perform_koenig_lookup (postfix_expression, args);
3742 else
3743 postfix_expression
3744 = unqualified_fn_lookup_error (postfix_expression);
3746 /* We do not perform argument-dependent lookup if
3747 normal lookup finds a non-function, in accordance
3748 with the expected resolution of DR 218. */
3749 else if (args && is_overloaded_fn (postfix_expression))
3751 tree fn = get_first_fn (postfix_expression);
3752 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3753 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3754 /* Only do argument dependent lookup if regular
3755 lookup does not find a set of member functions.
3756 [basic.lookup.koenig]/2a */
3757 if (!DECL_FUNCTION_MEMBER_P (fn))
3759 koenig_p = true;
3760 postfix_expression
3761 = perform_koenig_lookup (postfix_expression, args);
3766 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3768 tree instance = TREE_OPERAND (postfix_expression, 0);
3769 tree fn = TREE_OPERAND (postfix_expression, 1);
3771 if (processing_template_decl
3772 && (type_dependent_expression_p (instance)
3773 || (!BASELINK_P (fn)
3774 && TREE_CODE (fn) != FIELD_DECL)
3775 || type_dependent_expression_p (fn)
3776 || any_type_dependent_arguments_p (args)))
3778 postfix_expression
3779 = build_min_nt (CALL_EXPR, postfix_expression, args);
3780 break;
3783 if (BASELINK_P (fn))
3784 postfix_expression
3785 = (build_new_method_call
3786 (instance, fn, args, NULL_TREE,
3787 (idk == CP_ID_KIND_QUALIFIED
3788 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3789 else
3790 postfix_expression
3791 = finish_call_expr (postfix_expression, args,
3792 /*disallow_virtual=*/false,
3793 /*koenig_p=*/false);
3795 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3796 || TREE_CODE (postfix_expression) == MEMBER_REF
3797 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3798 postfix_expression = (build_offset_ref_call_from_tree
3799 (postfix_expression, args));
3800 else if (idk == CP_ID_KIND_QUALIFIED)
3801 /* A call to a static class member, or a namespace-scope
3802 function. */
3803 postfix_expression
3804 = finish_call_expr (postfix_expression, args,
3805 /*disallow_virtual=*/true,
3806 koenig_p);
3807 else
3808 /* All other function calls. */
3809 postfix_expression
3810 = finish_call_expr (postfix_expression, args,
3811 /*disallow_virtual=*/false,
3812 koenig_p);
3814 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3815 idk = CP_ID_KIND_NONE;
3817 break;
3819 case CPP_DOT:
3820 case CPP_DEREF:
3821 /* postfix-expression . template [opt] id-expression
3822 postfix-expression . pseudo-destructor-name
3823 postfix-expression -> template [opt] id-expression
3824 postfix-expression -> pseudo-destructor-name */
3826 tree name;
3827 bool dependent_p;
3828 bool template_p;
3829 bool pseudo_destructor_p;
3830 tree scope = NULL_TREE;
3831 enum cpp_ttype token_type = token->type;
3833 /* If this is a `->' operator, dereference the pointer. */
3834 if (token->type == CPP_DEREF)
3835 postfix_expression = build_x_arrow (postfix_expression);
3836 /* Check to see whether or not the expression is
3837 type-dependent. */
3838 dependent_p = type_dependent_expression_p (postfix_expression);
3839 /* The identifier following the `->' or `.' is not
3840 qualified. */
3841 parser->scope = NULL_TREE;
3842 parser->qualifying_scope = NULL_TREE;
3843 parser->object_scope = NULL_TREE;
3844 idk = CP_ID_KIND_NONE;
3845 /* Enter the scope corresponding to the type of the object
3846 given by the POSTFIX_EXPRESSION. */
3847 if (!dependent_p
3848 && TREE_TYPE (postfix_expression) != NULL_TREE)
3850 scope = TREE_TYPE (postfix_expression);
3851 /* According to the standard, no expression should
3852 ever have reference type. Unfortunately, we do not
3853 currently match the standard in this respect in
3854 that our internal representation of an expression
3855 may have reference type even when the standard says
3856 it does not. Therefore, we have to manually obtain
3857 the underlying type here. */
3858 scope = non_reference (scope);
3859 /* The type of the POSTFIX_EXPRESSION must be
3860 complete. */
3861 scope = complete_type_or_else (scope, NULL_TREE);
3862 /* Let the name lookup machinery know that we are
3863 processing a class member access expression. */
3864 parser->context->object_type = scope;
3865 /* If something went wrong, we want to be able to
3866 discern that case, as opposed to the case where
3867 there was no SCOPE due to the type of expression
3868 being dependent. */
3869 if (!scope)
3870 scope = error_mark_node;
3871 /* If the SCOPE was erroneous, make the various
3872 semantic analysis functions exit quickly -- and
3873 without issuing additional error messages. */
3874 if (scope == error_mark_node)
3875 postfix_expression = error_mark_node;
3878 /* Consume the `.' or `->' operator. */
3879 cp_lexer_consume_token (parser->lexer);
3881 /* Assume this expression is not a pseudo-destructor access. */
3882 pseudo_destructor_p = false;
3884 /* If the SCOPE is a scalar type, then, if this is a valid program,
3885 we must be looking at a pseudo-destructor-name. */
3886 if (scope && SCALAR_TYPE_P (scope))
3888 tree s = NULL_TREE;
3889 tree type;
3891 cp_parser_parse_tentatively (parser);
3892 /* Parse the pseudo-destructor-name. */
3893 cp_parser_pseudo_destructor_name (parser, &s, &type);
3894 if (cp_parser_parse_definitely (parser))
3896 pseudo_destructor_p = true;
3897 postfix_expression
3898 = finish_pseudo_destructor_expr (postfix_expression,
3899 s, TREE_TYPE (type));
3903 if (!pseudo_destructor_p)
3905 /* If the SCOPE is not a scalar type, we are looking
3906 at an ordinary class member access expression,
3907 rather than a pseudo-destructor-name. */
3908 template_p = cp_parser_optional_template_keyword (parser);
3909 /* Parse the id-expression. */
3910 name = cp_parser_id_expression (parser,
3911 template_p,
3912 /*check_dependency_p=*/true,
3913 /*template_p=*/NULL,
3914 /*declarator_p=*/false);
3915 /* In general, build a SCOPE_REF if the member name is
3916 qualified. However, if the name was not dependent
3917 and has already been resolved; there is no need to
3918 build the SCOPE_REF. For example;
3920 struct X { void f(); };
3921 template <typename T> void f(T* t) { t->X::f(); }
3923 Even though "t" is dependent, "X::f" is not and has
3924 been resolved to a BASELINK; there is no need to
3925 include scope information. */
3927 /* But we do need to remember that there was an explicit
3928 scope for virtual function calls. */
3929 if (parser->scope)
3930 idk = CP_ID_KIND_QUALIFIED;
3932 /* If the name is a template-id that names a type, we will
3933 get a TYPE_DECL here. That is invalid code. */
3934 if (TREE_CODE (name) == TYPE_DECL)
3936 error ("invalid use of `%D'", name);
3937 postfix_expression = error_mark_node;
3939 else
3941 if (name != error_mark_node && !BASELINK_P (name)
3942 && parser->scope)
3944 name = build_nt (SCOPE_REF, parser->scope, name);
3945 parser->scope = NULL_TREE;
3946 parser->qualifying_scope = NULL_TREE;
3947 parser->object_scope = NULL_TREE;
3949 if (scope && name && BASELINK_P (name))
3950 adjust_result_of_qualified_name_lookup
3951 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3952 postfix_expression = finish_class_member_access_expr
3953 (postfix_expression, name);
3957 /* We no longer need to look up names in the scope of the
3958 object on the left-hand side of the `.' or `->'
3959 operator. */
3960 parser->context->object_type = NULL_TREE;
3961 /* These operators may not appear in constant-expressions. */
3962 if (/* The "->" operator is allowed in the implementation
3963 of "offsetof". The "." operator may appear in the
3964 name of the member. */
3965 !parser->in_offsetof_p
3966 && (cp_parser_non_integral_constant_expression
3967 (parser,
3968 token_type == CPP_DEREF ? "'->'" : "`.'")))
3969 postfix_expression = error_mark_node;
3971 break;
3973 case CPP_PLUS_PLUS:
3974 /* postfix-expression ++ */
3975 /* Consume the `++' token. */
3976 cp_lexer_consume_token (parser->lexer);
3977 /* Generate a representation for the complete expression. */
3978 postfix_expression
3979 = finish_increment_expr (postfix_expression,
3980 POSTINCREMENT_EXPR);
3981 /* Increments may not appear in constant-expressions. */
3982 if (cp_parser_non_integral_constant_expression (parser,
3983 "an increment"))
3984 postfix_expression = error_mark_node;
3985 idk = CP_ID_KIND_NONE;
3986 break;
3988 case CPP_MINUS_MINUS:
3989 /* postfix-expression -- */
3990 /* Consume the `--' token. */
3991 cp_lexer_consume_token (parser->lexer);
3992 /* Generate a representation for the complete expression. */
3993 postfix_expression
3994 = finish_increment_expr (postfix_expression,
3995 POSTDECREMENT_EXPR);
3996 /* Decrements may not appear in constant-expressions. */
3997 if (cp_parser_non_integral_constant_expression (parser,
3998 "a decrement"))
3999 postfix_expression = error_mark_node;
4000 idk = CP_ID_KIND_NONE;
4001 break;
4003 default:
4004 return postfix_expression;
4008 /* We should never get here. */
4009 abort ();
4010 return error_mark_node;
4013 /* Parse a parenthesized expression-list.
4015 expression-list:
4016 assignment-expression
4017 expression-list, assignment-expression
4019 attribute-list:
4020 expression-list
4021 identifier
4022 identifier, expression-list
4024 Returns a TREE_LIST. The TREE_VALUE of each node is a
4025 representation of an assignment-expression. Note that a TREE_LIST
4026 is returned even if there is only a single expression in the list.
4027 error_mark_node is returned if the ( and or ) are
4028 missing. NULL_TREE is returned on no expressions. The parentheses
4029 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4030 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4031 indicates whether or not all of the expressions in the list were
4032 constant. */
4034 static tree
4035 cp_parser_parenthesized_expression_list (cp_parser* parser,
4036 bool is_attribute_list,
4037 bool *non_constant_p)
4039 tree expression_list = NULL_TREE;
4040 tree identifier = NULL_TREE;
4042 /* Assume all the expressions will be constant. */
4043 if (non_constant_p)
4044 *non_constant_p = false;
4046 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4047 return error_mark_node;
4049 /* Consume expressions until there are no more. */
4050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4051 while (true)
4053 tree expr;
4055 /* At the beginning of attribute lists, check to see if the
4056 next token is an identifier. */
4057 if (is_attribute_list
4058 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4060 cp_token *token;
4062 /* Consume the identifier. */
4063 token = cp_lexer_consume_token (parser->lexer);
4064 /* Save the identifier. */
4065 identifier = token->value;
4067 else
4069 /* Parse the next assignment-expression. */
4070 if (non_constant_p)
4072 bool expr_non_constant_p;
4073 expr = (cp_parser_constant_expression
4074 (parser, /*allow_non_constant_p=*/true,
4075 &expr_non_constant_p));
4076 if (expr_non_constant_p)
4077 *non_constant_p = true;
4079 else
4080 expr = cp_parser_assignment_expression (parser);
4082 /* Add it to the list. We add error_mark_node
4083 expressions to the list, so that we can still tell if
4084 the correct form for a parenthesized expression-list
4085 is found. That gives better errors. */
4086 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4088 if (expr == error_mark_node)
4089 goto skip_comma;
4092 /* After the first item, attribute lists look the same as
4093 expression lists. */
4094 is_attribute_list = false;
4096 get_comma:;
4097 /* If the next token isn't a `,', then we are done. */
4098 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4099 break;
4101 /* Otherwise, consume the `,' and keep going. */
4102 cp_lexer_consume_token (parser->lexer);
4105 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4107 int ending;
4109 skip_comma:;
4110 /* We try and resync to an unnested comma, as that will give the
4111 user better diagnostics. */
4112 ending = cp_parser_skip_to_closing_parenthesis (parser,
4113 /*recovering=*/true,
4114 /*or_comma=*/true,
4115 /*consume_paren=*/true);
4116 if (ending < 0)
4117 goto get_comma;
4118 if (!ending)
4119 return error_mark_node;
4122 /* We built up the list in reverse order so we must reverse it now. */
4123 expression_list = nreverse (expression_list);
4124 if (identifier)
4125 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4127 return expression_list;
4130 /* Parse a pseudo-destructor-name.
4132 pseudo-destructor-name:
4133 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4134 :: [opt] nested-name-specifier template template-id :: ~ type-name
4135 :: [opt] nested-name-specifier [opt] ~ type-name
4137 If either of the first two productions is used, sets *SCOPE to the
4138 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4139 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4140 or ERROR_MARK_NODE if the parse fails. */
4142 static void
4143 cp_parser_pseudo_destructor_name (cp_parser* parser,
4144 tree* scope,
4145 tree* type)
4147 bool nested_name_specifier_p;
4149 /* Look for the optional `::' operator. */
4150 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4151 /* Look for the optional nested-name-specifier. */
4152 nested_name_specifier_p
4153 = (cp_parser_nested_name_specifier_opt (parser,
4154 /*typename_keyword_p=*/false,
4155 /*check_dependency_p=*/true,
4156 /*type_p=*/false,
4157 /*is_declaration=*/true)
4158 != NULL_TREE);
4159 /* Now, if we saw a nested-name-specifier, we might be doing the
4160 second production. */
4161 if (nested_name_specifier_p
4162 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4164 /* Consume the `template' keyword. */
4165 cp_lexer_consume_token (parser->lexer);
4166 /* Parse the template-id. */
4167 cp_parser_template_id (parser,
4168 /*template_keyword_p=*/true,
4169 /*check_dependency_p=*/false,
4170 /*is_declaration=*/true);
4171 /* Look for the `::' token. */
4172 cp_parser_require (parser, CPP_SCOPE, "`::'");
4174 /* If the next token is not a `~', then there might be some
4175 additional qualification. */
4176 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4178 /* Look for the type-name. */
4179 *scope = TREE_TYPE (cp_parser_type_name (parser));
4181 /* If we didn't get an aggregate type, or we don't have ::~,
4182 then something has gone wrong. Since the only caller of this
4183 function is looking for something after `.' or `->' after a
4184 scalar type, most likely the program is trying to get a
4185 member of a non-aggregate type. */
4186 if (*scope == error_mark_node
4187 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4188 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4190 cp_parser_error (parser, "request for member of non-aggregate type");
4191 *type = error_mark_node;
4192 return;
4195 /* Look for the `::' token. */
4196 cp_parser_require (parser, CPP_SCOPE, "`::'");
4198 else
4199 *scope = NULL_TREE;
4201 /* Look for the `~'. */
4202 cp_parser_require (parser, CPP_COMPL, "`~'");
4203 /* Look for the type-name again. We are not responsible for
4204 checking that it matches the first type-name. */
4205 *type = cp_parser_type_name (parser);
4208 /* Parse a unary-expression.
4210 unary-expression:
4211 postfix-expression
4212 ++ cast-expression
4213 -- cast-expression
4214 unary-operator cast-expression
4215 sizeof unary-expression
4216 sizeof ( type-id )
4217 new-expression
4218 delete-expression
4220 GNU Extensions:
4222 unary-expression:
4223 __extension__ cast-expression
4224 __alignof__ unary-expression
4225 __alignof__ ( type-id )
4226 __real__ cast-expression
4227 __imag__ cast-expression
4228 && identifier
4230 ADDRESS_P is true iff the unary-expression is appearing as the
4231 operand of the `&' operator.
4233 Returns a representation of the expression. */
4235 static tree
4236 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4238 cp_token *token;
4239 enum tree_code unary_operator;
4241 /* Peek at the next token. */
4242 token = cp_lexer_peek_token (parser->lexer);
4243 /* Some keywords give away the kind of expression. */
4244 if (token->type == CPP_KEYWORD)
4246 enum rid keyword = token->keyword;
4248 switch (keyword)
4250 case RID_ALIGNOF:
4251 case RID_SIZEOF:
4253 tree operand;
4254 enum tree_code op;
4256 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4257 /* Consume the token. */
4258 cp_lexer_consume_token (parser->lexer);
4259 /* Parse the operand. */
4260 operand = cp_parser_sizeof_operand (parser, keyword);
4262 if (TYPE_P (operand))
4263 return cxx_sizeof_or_alignof_type (operand, op, true);
4264 else
4265 return cxx_sizeof_or_alignof_expr (operand, op);
4268 case RID_NEW:
4269 return cp_parser_new_expression (parser);
4271 case RID_DELETE:
4272 return cp_parser_delete_expression (parser);
4274 case RID_EXTENSION:
4276 /* The saved value of the PEDANTIC flag. */
4277 int saved_pedantic;
4278 tree expr;
4280 /* Save away the PEDANTIC flag. */
4281 cp_parser_extension_opt (parser, &saved_pedantic);
4282 /* Parse the cast-expression. */
4283 expr = cp_parser_simple_cast_expression (parser);
4284 /* Restore the PEDANTIC flag. */
4285 pedantic = saved_pedantic;
4287 return expr;
4290 case RID_REALPART:
4291 case RID_IMAGPART:
4293 tree expression;
4295 /* Consume the `__real__' or `__imag__' token. */
4296 cp_lexer_consume_token (parser->lexer);
4297 /* Parse the cast-expression. */
4298 expression = cp_parser_simple_cast_expression (parser);
4299 /* Create the complete representation. */
4300 return build_x_unary_op ((keyword == RID_REALPART
4301 ? REALPART_EXPR : IMAGPART_EXPR),
4302 expression);
4304 break;
4306 default:
4307 break;
4311 /* Look for the `:: new' and `:: delete', which also signal the
4312 beginning of a new-expression, or delete-expression,
4313 respectively. If the next token is `::', then it might be one of
4314 these. */
4315 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4317 enum rid keyword;
4319 /* See if the token after the `::' is one of the keywords in
4320 which we're interested. */
4321 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4322 /* If it's `new', we have a new-expression. */
4323 if (keyword == RID_NEW)
4324 return cp_parser_new_expression (parser);
4325 /* Similarly, for `delete'. */
4326 else if (keyword == RID_DELETE)
4327 return cp_parser_delete_expression (parser);
4330 /* Look for a unary operator. */
4331 unary_operator = cp_parser_unary_operator (token);
4332 /* The `++' and `--' operators can be handled similarly, even though
4333 they are not technically unary-operators in the grammar. */
4334 if (unary_operator == ERROR_MARK)
4336 if (token->type == CPP_PLUS_PLUS)
4337 unary_operator = PREINCREMENT_EXPR;
4338 else if (token->type == CPP_MINUS_MINUS)
4339 unary_operator = PREDECREMENT_EXPR;
4340 /* Handle the GNU address-of-label extension. */
4341 else if (cp_parser_allow_gnu_extensions_p (parser)
4342 && token->type == CPP_AND_AND)
4344 tree identifier;
4346 /* Consume the '&&' token. */
4347 cp_lexer_consume_token (parser->lexer);
4348 /* Look for the identifier. */
4349 identifier = cp_parser_identifier (parser);
4350 /* Create an expression representing the address. */
4351 return finish_label_address_expr (identifier);
4354 if (unary_operator != ERROR_MARK)
4356 tree cast_expression;
4357 tree expression = error_mark_node;
4358 const char *non_constant_p = NULL;
4360 /* Consume the operator token. */
4361 token = cp_lexer_consume_token (parser->lexer);
4362 /* Parse the cast-expression. */
4363 cast_expression
4364 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4365 /* Now, build an appropriate representation. */
4366 switch (unary_operator)
4368 case INDIRECT_REF:
4369 non_constant_p = "`*'";
4370 expression = build_x_indirect_ref (cast_expression, "unary *");
4371 break;
4373 case ADDR_EXPR:
4374 /* The "&" operator is allowed in the implementation of
4375 "offsetof". */
4376 if (!parser->in_offsetof_p)
4377 non_constant_p = "`&'";
4378 /* Fall through. */
4379 case BIT_NOT_EXPR:
4380 expression = build_x_unary_op (unary_operator, cast_expression);
4381 break;
4383 case PREINCREMENT_EXPR:
4384 case PREDECREMENT_EXPR:
4385 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4386 ? "`++'" : "`--'");
4387 /* Fall through. */
4388 case CONVERT_EXPR:
4389 case NEGATE_EXPR:
4390 case TRUTH_NOT_EXPR:
4391 expression = finish_unary_op_expr (unary_operator, cast_expression);
4392 break;
4394 default:
4395 abort ();
4398 if (non_constant_p
4399 && cp_parser_non_integral_constant_expression (parser,
4400 non_constant_p))
4401 expression = error_mark_node;
4403 return expression;
4406 return cp_parser_postfix_expression (parser, address_p);
4409 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4410 unary-operator, the corresponding tree code is returned. */
4412 static enum tree_code
4413 cp_parser_unary_operator (cp_token* token)
4415 switch (token->type)
4417 case CPP_MULT:
4418 return INDIRECT_REF;
4420 case CPP_AND:
4421 return ADDR_EXPR;
4423 case CPP_PLUS:
4424 return CONVERT_EXPR;
4426 case CPP_MINUS:
4427 return NEGATE_EXPR;
4429 case CPP_NOT:
4430 return TRUTH_NOT_EXPR;
4432 case CPP_COMPL:
4433 return BIT_NOT_EXPR;
4435 default:
4436 return ERROR_MARK;
4440 /* Parse a new-expression.
4442 new-expression:
4443 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4444 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4446 Returns a representation of the expression. */
4448 static tree
4449 cp_parser_new_expression (cp_parser* parser)
4451 bool global_scope_p;
4452 tree placement;
4453 tree type;
4454 tree initializer;
4456 /* Look for the optional `::' operator. */
4457 global_scope_p
4458 = (cp_parser_global_scope_opt (parser,
4459 /*current_scope_valid_p=*/false)
4460 != NULL_TREE);
4461 /* Look for the `new' operator. */
4462 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4463 /* There's no easy way to tell a new-placement from the
4464 `( type-id )' construct. */
4465 cp_parser_parse_tentatively (parser);
4466 /* Look for a new-placement. */
4467 placement = cp_parser_new_placement (parser);
4468 /* If that didn't work out, there's no new-placement. */
4469 if (!cp_parser_parse_definitely (parser))
4470 placement = NULL_TREE;
4472 /* If the next token is a `(', then we have a parenthesized
4473 type-id. */
4474 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4476 /* Consume the `('. */
4477 cp_lexer_consume_token (parser->lexer);
4478 /* Parse the type-id. */
4479 type = cp_parser_type_id (parser);
4480 /* Look for the closing `)'. */
4481 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4482 /* There should not be a direct-new-declarator in this production,
4483 but GCC used to allowed this, so we check and emit a sensible error
4484 message for this case. */
4485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4487 error ("array bound forbidden after parenthesized type-id");
4488 inform ("try removing the parentheses around the type-id");
4489 cp_parser_direct_new_declarator (parser);
4492 /* Otherwise, there must be a new-type-id. */
4493 else
4494 type = cp_parser_new_type_id (parser);
4496 /* If the next token is a `(', then we have a new-initializer. */
4497 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4498 initializer = cp_parser_new_initializer (parser);
4499 else
4500 initializer = NULL_TREE;
4502 /* A new-expression may not appear in an integral constant
4503 expression. */
4504 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4505 return error_mark_node;
4507 /* Create a representation of the new-expression. */
4508 return build_new (placement, type, initializer, global_scope_p);
4511 /* Parse a new-placement.
4513 new-placement:
4514 ( expression-list )
4516 Returns the same representation as for an expression-list. */
4518 static tree
4519 cp_parser_new_placement (cp_parser* parser)
4521 tree expression_list;
4523 /* Parse the expression-list. */
4524 expression_list = (cp_parser_parenthesized_expression_list
4525 (parser, false, /*non_constant_p=*/NULL));
4527 return expression_list;
4530 /* Parse a new-type-id.
4532 new-type-id:
4533 type-specifier-seq new-declarator [opt]
4535 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4536 and whose TREE_VALUE is the new-declarator. */
4538 static tree
4539 cp_parser_new_type_id (cp_parser* parser)
4541 tree type_specifier_seq;
4542 tree declarator;
4543 const char *saved_message;
4545 /* The type-specifier sequence must not contain type definitions.
4546 (It cannot contain declarations of new types either, but if they
4547 are not definitions we will catch that because they are not
4548 complete.) */
4549 saved_message = parser->type_definition_forbidden_message;
4550 parser->type_definition_forbidden_message
4551 = "types may not be defined in a new-type-id";
4552 /* Parse the type-specifier-seq. */
4553 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4554 /* Restore the old message. */
4555 parser->type_definition_forbidden_message = saved_message;
4556 /* Parse the new-declarator. */
4557 declarator = cp_parser_new_declarator_opt (parser);
4559 return build_tree_list (type_specifier_seq, declarator);
4562 /* Parse an (optional) new-declarator.
4564 new-declarator:
4565 ptr-operator new-declarator [opt]
4566 direct-new-declarator
4568 Returns a representation of the declarator. See
4569 cp_parser_declarator for the representations used. */
4571 static tree
4572 cp_parser_new_declarator_opt (cp_parser* parser)
4574 enum tree_code code;
4575 tree type;
4576 tree cv_qualifier_seq;
4578 /* We don't know if there's a ptr-operator next, or not. */
4579 cp_parser_parse_tentatively (parser);
4580 /* Look for a ptr-operator. */
4581 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4582 /* If that worked, look for more new-declarators. */
4583 if (cp_parser_parse_definitely (parser))
4585 tree declarator;
4587 /* Parse another optional declarator. */
4588 declarator = cp_parser_new_declarator_opt (parser);
4590 /* Create the representation of the declarator. */
4591 if (code == INDIRECT_REF)
4592 declarator = make_pointer_declarator (cv_qualifier_seq,
4593 declarator);
4594 else
4595 declarator = make_reference_declarator (cv_qualifier_seq,
4596 declarator);
4598 /* Handle the pointer-to-member case. */
4599 if (type)
4600 declarator = build_nt (SCOPE_REF, type, declarator);
4602 return declarator;
4605 /* If the next token is a `[', there is a direct-new-declarator. */
4606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4607 return cp_parser_direct_new_declarator (parser);
4609 return NULL_TREE;
4612 /* Parse a direct-new-declarator.
4614 direct-new-declarator:
4615 [ expression ]
4616 direct-new-declarator [constant-expression]
4618 Returns an ARRAY_REF, following the same conventions as are
4619 documented for cp_parser_direct_declarator. */
4621 static tree
4622 cp_parser_direct_new_declarator (cp_parser* parser)
4624 tree declarator = NULL_TREE;
4626 while (true)
4628 tree expression;
4630 /* Look for the opening `['. */
4631 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4632 /* The first expression is not required to be constant. */
4633 if (!declarator)
4635 expression = cp_parser_expression (parser);
4636 /* The standard requires that the expression have integral
4637 type. DR 74 adds enumeration types. We believe that the
4638 real intent is that these expressions be handled like the
4639 expression in a `switch' condition, which also allows
4640 classes with a single conversion to integral or
4641 enumeration type. */
4642 if (!processing_template_decl)
4644 expression
4645 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4646 expression,
4647 /*complain=*/true);
4648 if (!expression)
4650 error ("expression in new-declarator must have integral or enumeration type");
4651 expression = error_mark_node;
4655 /* But all the other expressions must be. */
4656 else
4657 expression
4658 = cp_parser_constant_expression (parser,
4659 /*allow_non_constant=*/false,
4660 NULL);
4661 /* Look for the closing `]'. */
4662 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4664 /* Add this bound to the declarator. */
4665 declarator = build_nt (ARRAY_REF, declarator, expression);
4667 /* If the next token is not a `[', then there are no more
4668 bounds. */
4669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4670 break;
4673 return declarator;
4676 /* Parse a new-initializer.
4678 new-initializer:
4679 ( expression-list [opt] )
4681 Returns a representation of the expression-list. If there is no
4682 expression-list, VOID_ZERO_NODE is returned. */
4684 static tree
4685 cp_parser_new_initializer (cp_parser* parser)
4687 tree expression_list;
4689 expression_list = (cp_parser_parenthesized_expression_list
4690 (parser, false, /*non_constant_p=*/NULL));
4691 if (!expression_list)
4692 expression_list = void_zero_node;
4694 return expression_list;
4697 /* Parse a delete-expression.
4699 delete-expression:
4700 :: [opt] delete cast-expression
4701 :: [opt] delete [ ] cast-expression
4703 Returns a representation of the expression. */
4705 static tree
4706 cp_parser_delete_expression (cp_parser* parser)
4708 bool global_scope_p;
4709 bool array_p;
4710 tree expression;
4712 /* Look for the optional `::' operator. */
4713 global_scope_p
4714 = (cp_parser_global_scope_opt (parser,
4715 /*current_scope_valid_p=*/false)
4716 != NULL_TREE);
4717 /* Look for the `delete' keyword. */
4718 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4719 /* See if the array syntax is in use. */
4720 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4722 /* Consume the `[' token. */
4723 cp_lexer_consume_token (parser->lexer);
4724 /* Look for the `]' token. */
4725 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4726 /* Remember that this is the `[]' construct. */
4727 array_p = true;
4729 else
4730 array_p = false;
4732 /* Parse the cast-expression. */
4733 expression = cp_parser_simple_cast_expression (parser);
4735 /* A delete-expression may not appear in an integral constant
4736 expression. */
4737 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4738 return error_mark_node;
4740 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4743 /* Parse a cast-expression.
4745 cast-expression:
4746 unary-expression
4747 ( type-id ) cast-expression
4749 Returns a representation of the expression. */
4751 static tree
4752 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4754 /* If it's a `(', then we might be looking at a cast. */
4755 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4757 tree type = NULL_TREE;
4758 tree expr = NULL_TREE;
4759 bool compound_literal_p;
4760 const char *saved_message;
4762 /* There's no way to know yet whether or not this is a cast.
4763 For example, `(int (3))' is a unary-expression, while `(int)
4764 3' is a cast. So, we resort to parsing tentatively. */
4765 cp_parser_parse_tentatively (parser);
4766 /* Types may not be defined in a cast. */
4767 saved_message = parser->type_definition_forbidden_message;
4768 parser->type_definition_forbidden_message
4769 = "types may not be defined in casts";
4770 /* Consume the `('. */
4771 cp_lexer_consume_token (parser->lexer);
4772 /* A very tricky bit is that `(struct S) { 3 }' is a
4773 compound-literal (which we permit in C++ as an extension).
4774 But, that construct is not a cast-expression -- it is a
4775 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4776 is legal; if the compound-literal were a cast-expression,
4777 you'd need an extra set of parentheses.) But, if we parse
4778 the type-id, and it happens to be a class-specifier, then we
4779 will commit to the parse at that point, because we cannot
4780 undo the action that is done when creating a new class. So,
4781 then we cannot back up and do a postfix-expression.
4783 Therefore, we scan ahead to the closing `)', and check to see
4784 if the token after the `)' is a `{'. If so, we are not
4785 looking at a cast-expression.
4787 Save tokens so that we can put them back. */
4788 cp_lexer_save_tokens (parser->lexer);
4789 /* Skip tokens until the next token is a closing parenthesis.
4790 If we find the closing `)', and the next token is a `{', then
4791 we are looking at a compound-literal. */
4792 compound_literal_p
4793 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4794 /*consume_paren=*/true)
4795 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4796 /* Roll back the tokens we skipped. */
4797 cp_lexer_rollback_tokens (parser->lexer);
4798 /* If we were looking at a compound-literal, simulate an error
4799 so that the call to cp_parser_parse_definitely below will
4800 fail. */
4801 if (compound_literal_p)
4802 cp_parser_simulate_error (parser);
4803 else
4805 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4806 parser->in_type_id_in_expr_p = true;
4807 /* Look for the type-id. */
4808 type = cp_parser_type_id (parser);
4809 /* Look for the closing `)'. */
4810 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4811 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4814 /* Restore the saved message. */
4815 parser->type_definition_forbidden_message = saved_message;
4817 /* If ok so far, parse the dependent expression. We cannot be
4818 sure it is a cast. Consider `(T ())'. It is a parenthesized
4819 ctor of T, but looks like a cast to function returning T
4820 without a dependent expression. */
4821 if (!cp_parser_error_occurred (parser))
4822 expr = cp_parser_simple_cast_expression (parser);
4824 if (cp_parser_parse_definitely (parser))
4826 /* Warn about old-style casts, if so requested. */
4827 if (warn_old_style_cast
4828 && !in_system_header
4829 && !VOID_TYPE_P (type)
4830 && current_lang_name != lang_name_c)
4831 warning ("use of old-style cast");
4833 /* Only type conversions to integral or enumeration types
4834 can be used in constant-expressions. */
4835 if (parser->integral_constant_expression_p
4836 && !dependent_type_p (type)
4837 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4838 && (cp_parser_non_integral_constant_expression
4839 (parser,
4840 "a casts to a type other than an integral or "
4841 "enumeration type")))
4842 return error_mark_node;
4844 /* Perform the cast. */
4845 expr = build_c_cast (type, expr);
4846 return expr;
4850 /* If we get here, then it's not a cast, so it must be a
4851 unary-expression. */
4852 return cp_parser_unary_expression (parser, address_p);
4855 /* Parse a pm-expression.
4857 pm-expression:
4858 cast-expression
4859 pm-expression .* cast-expression
4860 pm-expression ->* cast-expression
4862 Returns a representation of the expression. */
4864 static tree
4865 cp_parser_pm_expression (cp_parser* parser)
4867 static const cp_parser_token_tree_map map = {
4868 { CPP_DEREF_STAR, MEMBER_REF },
4869 { CPP_DOT_STAR, DOTSTAR_EXPR },
4870 { CPP_EOF, ERROR_MARK }
4873 return cp_parser_binary_expression (parser, map,
4874 cp_parser_simple_cast_expression);
4877 /* Parse a multiplicative-expression.
4879 mulitplicative-expression:
4880 pm-expression
4881 multiplicative-expression * pm-expression
4882 multiplicative-expression / pm-expression
4883 multiplicative-expression % pm-expression
4885 Returns a representation of the expression. */
4887 static tree
4888 cp_parser_multiplicative_expression (cp_parser* parser)
4890 static const cp_parser_token_tree_map map = {
4891 { CPP_MULT, MULT_EXPR },
4892 { CPP_DIV, TRUNC_DIV_EXPR },
4893 { CPP_MOD, TRUNC_MOD_EXPR },
4894 { CPP_EOF, ERROR_MARK }
4897 return cp_parser_binary_expression (parser,
4898 map,
4899 cp_parser_pm_expression);
4902 /* Parse an additive-expression.
4904 additive-expression:
4905 multiplicative-expression
4906 additive-expression + multiplicative-expression
4907 additive-expression - multiplicative-expression
4909 Returns a representation of the expression. */
4911 static tree
4912 cp_parser_additive_expression (cp_parser* parser)
4914 static const cp_parser_token_tree_map map = {
4915 { CPP_PLUS, PLUS_EXPR },
4916 { CPP_MINUS, MINUS_EXPR },
4917 { CPP_EOF, ERROR_MARK }
4920 return cp_parser_binary_expression (parser,
4921 map,
4922 cp_parser_multiplicative_expression);
4925 /* Parse a shift-expression.
4927 shift-expression:
4928 additive-expression
4929 shift-expression << additive-expression
4930 shift-expression >> additive-expression
4932 Returns a representation of the expression. */
4934 static tree
4935 cp_parser_shift_expression (cp_parser* parser)
4937 static const cp_parser_token_tree_map map = {
4938 { CPP_LSHIFT, LSHIFT_EXPR },
4939 { CPP_RSHIFT, RSHIFT_EXPR },
4940 { CPP_EOF, ERROR_MARK }
4943 return cp_parser_binary_expression (parser,
4944 map,
4945 cp_parser_additive_expression);
4948 /* Parse a relational-expression.
4950 relational-expression:
4951 shift-expression
4952 relational-expression < shift-expression
4953 relational-expression > shift-expression
4954 relational-expression <= shift-expression
4955 relational-expression >= shift-expression
4957 GNU Extension:
4959 relational-expression:
4960 relational-expression <? shift-expression
4961 relational-expression >? shift-expression
4963 Returns a representation of the expression. */
4965 static tree
4966 cp_parser_relational_expression (cp_parser* parser)
4968 static const cp_parser_token_tree_map map = {
4969 { CPP_LESS, LT_EXPR },
4970 { CPP_GREATER, GT_EXPR },
4971 { CPP_LESS_EQ, LE_EXPR },
4972 { CPP_GREATER_EQ, GE_EXPR },
4973 { CPP_MIN, MIN_EXPR },
4974 { CPP_MAX, MAX_EXPR },
4975 { CPP_EOF, ERROR_MARK }
4978 return cp_parser_binary_expression (parser,
4979 map,
4980 cp_parser_shift_expression);
4983 /* Parse an equality-expression.
4985 equality-expression:
4986 relational-expression
4987 equality-expression == relational-expression
4988 equality-expression != relational-expression
4990 Returns a representation of the expression. */
4992 static tree
4993 cp_parser_equality_expression (cp_parser* parser)
4995 static const cp_parser_token_tree_map map = {
4996 { CPP_EQ_EQ, EQ_EXPR },
4997 { CPP_NOT_EQ, NE_EXPR },
4998 { CPP_EOF, ERROR_MARK }
5001 return cp_parser_binary_expression (parser,
5002 map,
5003 cp_parser_relational_expression);
5006 /* Parse an and-expression.
5008 and-expression:
5009 equality-expression
5010 and-expression & equality-expression
5012 Returns a representation of the expression. */
5014 static tree
5015 cp_parser_and_expression (cp_parser* parser)
5017 static const cp_parser_token_tree_map map = {
5018 { CPP_AND, BIT_AND_EXPR },
5019 { CPP_EOF, ERROR_MARK }
5022 return cp_parser_binary_expression (parser,
5023 map,
5024 cp_parser_equality_expression);
5027 /* Parse an exclusive-or-expression.
5029 exclusive-or-expression:
5030 and-expression
5031 exclusive-or-expression ^ and-expression
5033 Returns a representation of the expression. */
5035 static tree
5036 cp_parser_exclusive_or_expression (cp_parser* parser)
5038 static const cp_parser_token_tree_map map = {
5039 { CPP_XOR, BIT_XOR_EXPR },
5040 { CPP_EOF, ERROR_MARK }
5043 return cp_parser_binary_expression (parser,
5044 map,
5045 cp_parser_and_expression);
5049 /* Parse an inclusive-or-expression.
5051 inclusive-or-expression:
5052 exclusive-or-expression
5053 inclusive-or-expression | exclusive-or-expression
5055 Returns a representation of the expression. */
5057 static tree
5058 cp_parser_inclusive_or_expression (cp_parser* parser)
5060 static const cp_parser_token_tree_map map = {
5061 { CPP_OR, BIT_IOR_EXPR },
5062 { CPP_EOF, ERROR_MARK }
5065 return cp_parser_binary_expression (parser,
5066 map,
5067 cp_parser_exclusive_or_expression);
5070 /* Parse a logical-and-expression.
5072 logical-and-expression:
5073 inclusive-or-expression
5074 logical-and-expression && inclusive-or-expression
5076 Returns a representation of the expression. */
5078 static tree
5079 cp_parser_logical_and_expression (cp_parser* parser)
5081 static const cp_parser_token_tree_map map = {
5082 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5083 { CPP_EOF, ERROR_MARK }
5086 return cp_parser_binary_expression (parser,
5087 map,
5088 cp_parser_inclusive_or_expression);
5091 /* Parse a logical-or-expression.
5093 logical-or-expression:
5094 logical-and-expression
5095 logical-or-expression || logical-and-expression
5097 Returns a representation of the expression. */
5099 static tree
5100 cp_parser_logical_or_expression (cp_parser* parser)
5102 static const cp_parser_token_tree_map map = {
5103 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5104 { CPP_EOF, ERROR_MARK }
5107 return cp_parser_binary_expression (parser,
5108 map,
5109 cp_parser_logical_and_expression);
5112 /* Parse the `? expression : assignment-expression' part of a
5113 conditional-expression. The LOGICAL_OR_EXPR is the
5114 logical-or-expression that started the conditional-expression.
5115 Returns a representation of the entire conditional-expression.
5117 This routine is used by cp_parser_assignment_expression.
5119 ? expression : assignment-expression
5121 GNU Extensions:
5123 ? : assignment-expression */
5125 static tree
5126 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5128 tree expr;
5129 tree assignment_expr;
5131 /* Consume the `?' token. */
5132 cp_lexer_consume_token (parser->lexer);
5133 if (cp_parser_allow_gnu_extensions_p (parser)
5134 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5135 /* Implicit true clause. */
5136 expr = NULL_TREE;
5137 else
5138 /* Parse the expression. */
5139 expr = cp_parser_expression (parser);
5141 /* The next token should be a `:'. */
5142 cp_parser_require (parser, CPP_COLON, "`:'");
5143 /* Parse the assignment-expression. */
5144 assignment_expr = cp_parser_assignment_expression (parser);
5146 /* Build the conditional-expression. */
5147 return build_x_conditional_expr (logical_or_expr,
5148 expr,
5149 assignment_expr);
5152 /* Parse an assignment-expression.
5154 assignment-expression:
5155 conditional-expression
5156 logical-or-expression assignment-operator assignment_expression
5157 throw-expression
5159 Returns a representation for the expression. */
5161 static tree
5162 cp_parser_assignment_expression (cp_parser* parser)
5164 tree expr;
5166 /* If the next token is the `throw' keyword, then we're looking at
5167 a throw-expression. */
5168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5169 expr = cp_parser_throw_expression (parser);
5170 /* Otherwise, it must be that we are looking at a
5171 logical-or-expression. */
5172 else
5174 /* Parse the logical-or-expression. */
5175 expr = cp_parser_logical_or_expression (parser);
5176 /* If the next token is a `?' then we're actually looking at a
5177 conditional-expression. */
5178 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5179 return cp_parser_question_colon_clause (parser, expr);
5180 else
5182 enum tree_code assignment_operator;
5184 /* If it's an assignment-operator, we're using the second
5185 production. */
5186 assignment_operator
5187 = cp_parser_assignment_operator_opt (parser);
5188 if (assignment_operator != ERROR_MARK)
5190 tree rhs;
5192 /* Parse the right-hand side of the assignment. */
5193 rhs = cp_parser_assignment_expression (parser);
5194 /* An assignment may not appear in a
5195 constant-expression. */
5196 if (cp_parser_non_integral_constant_expression (parser,
5197 "an assignment"))
5198 return error_mark_node;
5199 /* Build the assignment expression. */
5200 expr = build_x_modify_expr (expr,
5201 assignment_operator,
5202 rhs);
5207 return expr;
5210 /* Parse an (optional) assignment-operator.
5212 assignment-operator: one of
5213 = *= /= %= += -= >>= <<= &= ^= |=
5215 GNU Extension:
5217 assignment-operator: one of
5218 <?= >?=
5220 If the next token is an assignment operator, the corresponding tree
5221 code is returned, and the token is consumed. For example, for
5222 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5223 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5224 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5225 operator, ERROR_MARK is returned. */
5227 static enum tree_code
5228 cp_parser_assignment_operator_opt (cp_parser* parser)
5230 enum tree_code op;
5231 cp_token *token;
5233 /* Peek at the next toen. */
5234 token = cp_lexer_peek_token (parser->lexer);
5236 switch (token->type)
5238 case CPP_EQ:
5239 op = NOP_EXPR;
5240 break;
5242 case CPP_MULT_EQ:
5243 op = MULT_EXPR;
5244 break;
5246 case CPP_DIV_EQ:
5247 op = TRUNC_DIV_EXPR;
5248 break;
5250 case CPP_MOD_EQ:
5251 op = TRUNC_MOD_EXPR;
5252 break;
5254 case CPP_PLUS_EQ:
5255 op = PLUS_EXPR;
5256 break;
5258 case CPP_MINUS_EQ:
5259 op = MINUS_EXPR;
5260 break;
5262 case CPP_RSHIFT_EQ:
5263 op = RSHIFT_EXPR;
5264 break;
5266 case CPP_LSHIFT_EQ:
5267 op = LSHIFT_EXPR;
5268 break;
5270 case CPP_AND_EQ:
5271 op = BIT_AND_EXPR;
5272 break;
5274 case CPP_XOR_EQ:
5275 op = BIT_XOR_EXPR;
5276 break;
5278 case CPP_OR_EQ:
5279 op = BIT_IOR_EXPR;
5280 break;
5282 case CPP_MIN_EQ:
5283 op = MIN_EXPR;
5284 break;
5286 case CPP_MAX_EQ:
5287 op = MAX_EXPR;
5288 break;
5290 default:
5291 /* Nothing else is an assignment operator. */
5292 op = ERROR_MARK;
5295 /* If it was an assignment operator, consume it. */
5296 if (op != ERROR_MARK)
5297 cp_lexer_consume_token (parser->lexer);
5299 return op;
5302 /* Parse an expression.
5304 expression:
5305 assignment-expression
5306 expression , assignment-expression
5308 Returns a representation of the expression. */
5310 static tree
5311 cp_parser_expression (cp_parser* parser)
5313 tree expression = NULL_TREE;
5315 while (true)
5317 tree assignment_expression;
5319 /* Parse the next assignment-expression. */
5320 assignment_expression
5321 = cp_parser_assignment_expression (parser);
5322 /* If this is the first assignment-expression, we can just
5323 save it away. */
5324 if (!expression)
5325 expression = assignment_expression;
5326 else
5327 expression = build_x_compound_expr (expression,
5328 assignment_expression);
5329 /* If the next token is not a comma, then we are done with the
5330 expression. */
5331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5332 break;
5333 /* Consume the `,'. */
5334 cp_lexer_consume_token (parser->lexer);
5335 /* A comma operator cannot appear in a constant-expression. */
5336 if (cp_parser_non_integral_constant_expression (parser,
5337 "a comma operator"))
5338 expression = error_mark_node;
5341 return expression;
5344 /* Parse a constant-expression.
5346 constant-expression:
5347 conditional-expression
5349 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5350 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5351 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5352 is false, NON_CONSTANT_P should be NULL. */
5354 static tree
5355 cp_parser_constant_expression (cp_parser* parser,
5356 bool allow_non_constant_p,
5357 bool *non_constant_p)
5359 bool saved_integral_constant_expression_p;
5360 bool saved_allow_non_integral_constant_expression_p;
5361 bool saved_non_integral_constant_expression_p;
5362 tree expression;
5364 /* It might seem that we could simply parse the
5365 conditional-expression, and then check to see if it were
5366 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5367 one that the compiler can figure out is constant, possibly after
5368 doing some simplifications or optimizations. The standard has a
5369 precise definition of constant-expression, and we must honor
5370 that, even though it is somewhat more restrictive.
5372 For example:
5374 int i[(2, 3)];
5376 is not a legal declaration, because `(2, 3)' is not a
5377 constant-expression. The `,' operator is forbidden in a
5378 constant-expression. However, GCC's constant-folding machinery
5379 will fold this operation to an INTEGER_CST for `3'. */
5381 /* Save the old settings. */
5382 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5383 saved_allow_non_integral_constant_expression_p
5384 = parser->allow_non_integral_constant_expression_p;
5385 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5386 /* We are now parsing a constant-expression. */
5387 parser->integral_constant_expression_p = true;
5388 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5389 parser->non_integral_constant_expression_p = false;
5390 /* Although the grammar says "conditional-expression", we parse an
5391 "assignment-expression", which also permits "throw-expression"
5392 and the use of assignment operators. In the case that
5393 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5394 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5395 actually essential that we look for an assignment-expression.
5396 For example, cp_parser_initializer_clauses uses this function to
5397 determine whether a particular assignment-expression is in fact
5398 constant. */
5399 expression = cp_parser_assignment_expression (parser);
5400 /* Restore the old settings. */
5401 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5402 parser->allow_non_integral_constant_expression_p
5403 = saved_allow_non_integral_constant_expression_p;
5404 if (allow_non_constant_p)
5405 *non_constant_p = parser->non_integral_constant_expression_p;
5406 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5408 return expression;
5411 /* Statements [gram.stmt.stmt] */
5413 /* Parse a statement.
5415 statement:
5416 labeled-statement
5417 expression-statement
5418 compound-statement
5419 selection-statement
5420 iteration-statement
5421 jump-statement
5422 declaration-statement
5423 try-block */
5425 static void
5426 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5428 tree statement;
5429 cp_token *token;
5430 int statement_line_number;
5432 /* There is no statement yet. */
5433 statement = NULL_TREE;
5434 /* Peek at the next token. */
5435 token = cp_lexer_peek_token (parser->lexer);
5436 /* Remember the line number of the first token in the statement. */
5437 statement_line_number = token->location.line;
5438 /* If this is a keyword, then that will often determine what kind of
5439 statement we have. */
5440 if (token->type == CPP_KEYWORD)
5442 enum rid keyword = token->keyword;
5444 switch (keyword)
5446 case RID_CASE:
5447 case RID_DEFAULT:
5448 statement = cp_parser_labeled_statement (parser,
5449 in_statement_expr_p);
5450 break;
5452 case RID_IF:
5453 case RID_SWITCH:
5454 statement = cp_parser_selection_statement (parser);
5455 break;
5457 case RID_WHILE:
5458 case RID_DO:
5459 case RID_FOR:
5460 statement = cp_parser_iteration_statement (parser);
5461 break;
5463 case RID_BREAK:
5464 case RID_CONTINUE:
5465 case RID_RETURN:
5466 case RID_GOTO:
5467 statement = cp_parser_jump_statement (parser);
5468 break;
5470 case RID_TRY:
5471 statement = cp_parser_try_block (parser);
5472 break;
5474 default:
5475 /* It might be a keyword like `int' that can start a
5476 declaration-statement. */
5477 break;
5480 else if (token->type == CPP_NAME)
5482 /* If the next token is a `:', then we are looking at a
5483 labeled-statement. */
5484 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5485 if (token->type == CPP_COLON)
5486 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5488 /* Anything that starts with a `{' must be a compound-statement. */
5489 else if (token->type == CPP_OPEN_BRACE)
5490 statement = cp_parser_compound_statement (parser, false);
5492 /* Everything else must be a declaration-statement or an
5493 expression-statement. Try for the declaration-statement
5494 first, unless we are looking at a `;', in which case we know that
5495 we have an expression-statement. */
5496 if (!statement)
5498 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5500 cp_parser_parse_tentatively (parser);
5501 /* Try to parse the declaration-statement. */
5502 cp_parser_declaration_statement (parser);
5503 /* If that worked, we're done. */
5504 if (cp_parser_parse_definitely (parser))
5505 return;
5507 /* Look for an expression-statement instead. */
5508 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5511 /* Set the line number for the statement. */
5512 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5513 STMT_LINENO (statement) = statement_line_number;
5516 /* Parse a labeled-statement.
5518 labeled-statement:
5519 identifier : statement
5520 case constant-expression : statement
5521 default : statement
5523 GNU Extension:
5525 labeled-statement:
5526 case constant-expression ... constant-expression : statement
5528 Returns the new CASE_LABEL, for a `case' or `default' label. For
5529 an ordinary label, returns a LABEL_STMT. */
5531 static tree
5532 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5534 cp_token *token;
5535 tree statement = error_mark_node;
5537 /* The next token should be an identifier. */
5538 token = cp_lexer_peek_token (parser->lexer);
5539 if (token->type != CPP_NAME
5540 && token->type != CPP_KEYWORD)
5542 cp_parser_error (parser, "expected labeled-statement");
5543 return error_mark_node;
5546 switch (token->keyword)
5548 case RID_CASE:
5550 tree expr, expr_hi;
5551 cp_token *ellipsis;
5553 /* Consume the `case' token. */
5554 cp_lexer_consume_token (parser->lexer);
5555 /* Parse the constant-expression. */
5556 expr = cp_parser_constant_expression (parser,
5557 /*allow_non_constant_p=*/false,
5558 NULL);
5560 ellipsis = cp_lexer_peek_token (parser->lexer);
5561 if (ellipsis->type == CPP_ELLIPSIS)
5563 /* Consume the `...' token. */
5564 cp_lexer_consume_token (parser->lexer);
5565 expr_hi =
5566 cp_parser_constant_expression (parser,
5567 /*allow_non_constant_p=*/false,
5568 NULL);
5569 /* We don't need to emit warnings here, as the common code
5570 will do this for us. */
5572 else
5573 expr_hi = NULL_TREE;
5575 if (!parser->in_switch_statement_p)
5576 error ("case label `%E' not within a switch statement", expr);
5577 else
5578 statement = finish_case_label (expr, expr_hi);
5580 break;
5582 case RID_DEFAULT:
5583 /* Consume the `default' token. */
5584 cp_lexer_consume_token (parser->lexer);
5585 if (!parser->in_switch_statement_p)
5586 error ("case label not within a switch statement");
5587 else
5588 statement = finish_case_label (NULL_TREE, NULL_TREE);
5589 break;
5591 default:
5592 /* Anything else must be an ordinary label. */
5593 statement = finish_label_stmt (cp_parser_identifier (parser));
5594 break;
5597 /* Require the `:' token. */
5598 cp_parser_require (parser, CPP_COLON, "`:'");
5599 /* Parse the labeled statement. */
5600 cp_parser_statement (parser, in_statement_expr_p);
5602 /* Return the label, in the case of a `case' or `default' label. */
5603 return statement;
5606 /* Parse an expression-statement.
5608 expression-statement:
5609 expression [opt] ;
5611 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5612 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5613 indicates whether this expression-statement is part of an
5614 expression statement. */
5616 static tree
5617 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5619 tree statement = NULL_TREE;
5621 /* If the next token is a ';', then there is no expression
5622 statement. */
5623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5624 statement = cp_parser_expression (parser);
5626 /* Consume the final `;'. */
5627 cp_parser_consume_semicolon_at_end_of_statement (parser);
5629 if (in_statement_expr_p
5630 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5632 /* This is the final expression statement of a statement
5633 expression. */
5634 statement = finish_stmt_expr_expr (statement);
5636 else if (statement)
5637 statement = finish_expr_stmt (statement);
5638 else
5639 finish_stmt ();
5641 return statement;
5644 /* Parse a compound-statement.
5646 compound-statement:
5647 { statement-seq [opt] }
5649 Returns a COMPOUND_STMT representing the statement. */
5651 static tree
5652 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5654 tree compound_stmt;
5656 /* Consume the `{'. */
5657 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5658 return error_mark_node;
5659 /* Begin the compound-statement. */
5660 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5661 /* Parse an (optional) statement-seq. */
5662 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5663 /* Finish the compound-statement. */
5664 finish_compound_stmt (compound_stmt);
5665 /* Consume the `}'. */
5666 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5668 return compound_stmt;
5671 /* Parse an (optional) statement-seq.
5673 statement-seq:
5674 statement
5675 statement-seq [opt] statement */
5677 static void
5678 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5680 /* Scan statements until there aren't any more. */
5681 while (true)
5683 /* If we're looking at a `}', then we've run out of statements. */
5684 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5685 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5686 break;
5688 /* Parse the statement. */
5689 cp_parser_statement (parser, in_statement_expr_p);
5693 /* Parse a selection-statement.
5695 selection-statement:
5696 if ( condition ) statement
5697 if ( condition ) statement else statement
5698 switch ( condition ) statement
5700 Returns the new IF_STMT or SWITCH_STMT. */
5702 static tree
5703 cp_parser_selection_statement (cp_parser* parser)
5705 cp_token *token;
5706 enum rid keyword;
5708 /* Peek at the next token. */
5709 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5711 /* See what kind of keyword it is. */
5712 keyword = token->keyword;
5713 switch (keyword)
5715 case RID_IF:
5716 case RID_SWITCH:
5718 tree statement;
5719 tree condition;
5721 /* Look for the `('. */
5722 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5724 cp_parser_skip_to_end_of_statement (parser);
5725 return error_mark_node;
5728 /* Begin the selection-statement. */
5729 if (keyword == RID_IF)
5730 statement = begin_if_stmt ();
5731 else
5732 statement = begin_switch_stmt ();
5734 /* Parse the condition. */
5735 condition = cp_parser_condition (parser);
5736 /* Look for the `)'. */
5737 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5738 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5739 /*consume_paren=*/true);
5741 if (keyword == RID_IF)
5743 tree then_stmt;
5745 /* Add the condition. */
5746 finish_if_stmt_cond (condition, statement);
5748 /* Parse the then-clause. */
5749 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5750 finish_then_clause (statement);
5752 /* If the next token is `else', parse the else-clause. */
5753 if (cp_lexer_next_token_is_keyword (parser->lexer,
5754 RID_ELSE))
5756 tree else_stmt;
5758 /* Consume the `else' keyword. */
5759 cp_lexer_consume_token (parser->lexer);
5760 /* Parse the else-clause. */
5761 else_stmt
5762 = cp_parser_implicitly_scoped_statement (parser);
5763 finish_else_clause (statement);
5766 /* Now we're all done with the if-statement. */
5767 finish_if_stmt ();
5769 else
5771 tree body;
5772 bool in_switch_statement_p;
5774 /* Add the condition. */
5775 finish_switch_cond (condition, statement);
5777 /* Parse the body of the switch-statement. */
5778 in_switch_statement_p = parser->in_switch_statement_p;
5779 parser->in_switch_statement_p = true;
5780 body = cp_parser_implicitly_scoped_statement (parser);
5781 parser->in_switch_statement_p = in_switch_statement_p;
5783 /* Now we're all done with the switch-statement. */
5784 finish_switch_stmt (statement);
5787 return statement;
5789 break;
5791 default:
5792 cp_parser_error (parser, "expected selection-statement");
5793 return error_mark_node;
5797 /* Parse a condition.
5799 condition:
5800 expression
5801 type-specifier-seq declarator = assignment-expression
5803 GNU Extension:
5805 condition:
5806 type-specifier-seq declarator asm-specification [opt]
5807 attributes [opt] = assignment-expression
5809 Returns the expression that should be tested. */
5811 static tree
5812 cp_parser_condition (cp_parser* parser)
5814 tree type_specifiers;
5815 const char *saved_message;
5817 /* Try the declaration first. */
5818 cp_parser_parse_tentatively (parser);
5819 /* New types are not allowed in the type-specifier-seq for a
5820 condition. */
5821 saved_message = parser->type_definition_forbidden_message;
5822 parser->type_definition_forbidden_message
5823 = "types may not be defined in conditions";
5824 /* Parse the type-specifier-seq. */
5825 type_specifiers = cp_parser_type_specifier_seq (parser);
5826 /* Restore the saved message. */
5827 parser->type_definition_forbidden_message = saved_message;
5828 /* If all is well, we might be looking at a declaration. */
5829 if (!cp_parser_error_occurred (parser))
5831 tree decl;
5832 tree asm_specification;
5833 tree attributes;
5834 tree declarator;
5835 tree initializer = NULL_TREE;
5837 /* Parse the declarator. */
5838 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5839 /*ctor_dtor_or_conv_p=*/NULL,
5840 /*parenthesized_p=*/NULL,
5841 /*member_p=*/false);
5842 /* Parse the attributes. */
5843 attributes = cp_parser_attributes_opt (parser);
5844 /* Parse the asm-specification. */
5845 asm_specification = cp_parser_asm_specification_opt (parser);
5846 /* If the next token is not an `=', then we might still be
5847 looking at an expression. For example:
5849 if (A(a).x)
5851 looks like a decl-specifier-seq and a declarator -- but then
5852 there is no `=', so this is an expression. */
5853 cp_parser_require (parser, CPP_EQ, "`='");
5854 /* If we did see an `=', then we are looking at a declaration
5855 for sure. */
5856 if (cp_parser_parse_definitely (parser))
5858 /* Create the declaration. */
5859 decl = start_decl (declarator, type_specifiers,
5860 /*initialized_p=*/true,
5861 attributes, /*prefix_attributes=*/NULL_TREE);
5862 /* Parse the assignment-expression. */
5863 initializer = cp_parser_assignment_expression (parser);
5865 /* Process the initializer. */
5866 cp_finish_decl (decl,
5867 initializer,
5868 asm_specification,
5869 LOOKUP_ONLYCONVERTING);
5871 return convert_from_reference (decl);
5874 /* If we didn't even get past the declarator successfully, we are
5875 definitely not looking at a declaration. */
5876 else
5877 cp_parser_abort_tentative_parse (parser);
5879 /* Otherwise, we are looking at an expression. */
5880 return cp_parser_expression (parser);
5883 /* Parse an iteration-statement.
5885 iteration-statement:
5886 while ( condition ) statement
5887 do statement while ( expression ) ;
5888 for ( for-init-statement condition [opt] ; expression [opt] )
5889 statement
5891 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5893 static tree
5894 cp_parser_iteration_statement (cp_parser* parser)
5896 cp_token *token;
5897 enum rid keyword;
5898 tree statement;
5899 bool in_iteration_statement_p;
5902 /* Peek at the next token. */
5903 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5904 if (!token)
5905 return error_mark_node;
5907 /* Remember whether or not we are already within an iteration
5908 statement. */
5909 in_iteration_statement_p = parser->in_iteration_statement_p;
5911 /* See what kind of keyword it is. */
5912 keyword = token->keyword;
5913 switch (keyword)
5915 case RID_WHILE:
5917 tree condition;
5919 /* Begin the while-statement. */
5920 statement = begin_while_stmt ();
5921 /* Look for the `('. */
5922 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5923 /* Parse the condition. */
5924 condition = cp_parser_condition (parser);
5925 finish_while_stmt_cond (condition, statement);
5926 /* Look for the `)'. */
5927 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5928 /* Parse the dependent statement. */
5929 parser->in_iteration_statement_p = true;
5930 cp_parser_already_scoped_statement (parser);
5931 parser->in_iteration_statement_p = in_iteration_statement_p;
5932 /* We're done with the while-statement. */
5933 finish_while_stmt (statement);
5935 break;
5937 case RID_DO:
5939 tree expression;
5941 /* Begin the do-statement. */
5942 statement = begin_do_stmt ();
5943 /* Parse the body of the do-statement. */
5944 parser->in_iteration_statement_p = true;
5945 cp_parser_implicitly_scoped_statement (parser);
5946 parser->in_iteration_statement_p = in_iteration_statement_p;
5947 finish_do_body (statement);
5948 /* Look for the `while' keyword. */
5949 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5950 /* Look for the `('. */
5951 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5952 /* Parse the expression. */
5953 expression = cp_parser_expression (parser);
5954 /* We're done with the do-statement. */
5955 finish_do_stmt (expression, statement);
5956 /* Look for the `)'. */
5957 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5958 /* Look for the `;'. */
5959 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5961 break;
5963 case RID_FOR:
5965 tree condition = NULL_TREE;
5966 tree expression = NULL_TREE;
5968 /* Begin the for-statement. */
5969 statement = begin_for_stmt ();
5970 /* Look for the `('. */
5971 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5972 /* Parse the initialization. */
5973 cp_parser_for_init_statement (parser);
5974 finish_for_init_stmt (statement);
5976 /* If there's a condition, process it. */
5977 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5978 condition = cp_parser_condition (parser);
5979 finish_for_cond (condition, statement);
5980 /* Look for the `;'. */
5981 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5983 /* If there's an expression, process it. */
5984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5985 expression = cp_parser_expression (parser);
5986 finish_for_expr (expression, statement);
5987 /* Look for the `)'. */
5988 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5990 /* Parse the body of the for-statement. */
5991 parser->in_iteration_statement_p = true;
5992 cp_parser_already_scoped_statement (parser);
5993 parser->in_iteration_statement_p = in_iteration_statement_p;
5995 /* We're done with the for-statement. */
5996 finish_for_stmt (statement);
5998 break;
6000 default:
6001 cp_parser_error (parser, "expected iteration-statement");
6002 statement = error_mark_node;
6003 break;
6006 return statement;
6009 /* Parse a for-init-statement.
6011 for-init-statement:
6012 expression-statement
6013 simple-declaration */
6015 static void
6016 cp_parser_for_init_statement (cp_parser* parser)
6018 /* If the next token is a `;', then we have an empty
6019 expression-statement. Grammatically, this is also a
6020 simple-declaration, but an invalid one, because it does not
6021 declare anything. Therefore, if we did not handle this case
6022 specially, we would issue an error message about an invalid
6023 declaration. */
6024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6026 /* We're going to speculatively look for a declaration, falling back
6027 to an expression, if necessary. */
6028 cp_parser_parse_tentatively (parser);
6029 /* Parse the declaration. */
6030 cp_parser_simple_declaration (parser,
6031 /*function_definition_allowed_p=*/false);
6032 /* If the tentative parse failed, then we shall need to look for an
6033 expression-statement. */
6034 if (cp_parser_parse_definitely (parser))
6035 return;
6038 cp_parser_expression_statement (parser, false);
6041 /* Parse a jump-statement.
6043 jump-statement:
6044 break ;
6045 continue ;
6046 return expression [opt] ;
6047 goto identifier ;
6049 GNU extension:
6051 jump-statement:
6052 goto * expression ;
6054 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6055 GOTO_STMT. */
6057 static tree
6058 cp_parser_jump_statement (cp_parser* parser)
6060 tree statement = error_mark_node;
6061 cp_token *token;
6062 enum rid keyword;
6064 /* Peek at the next token. */
6065 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6066 if (!token)
6067 return error_mark_node;
6069 /* See what kind of keyword it is. */
6070 keyword = token->keyword;
6071 switch (keyword)
6073 case RID_BREAK:
6074 if (!parser->in_switch_statement_p
6075 && !parser->in_iteration_statement_p)
6077 error ("break statement not within loop or switch");
6078 statement = error_mark_node;
6080 else
6081 statement = finish_break_stmt ();
6082 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6083 break;
6085 case RID_CONTINUE:
6086 if (!parser->in_iteration_statement_p)
6088 error ("continue statement not within a loop");
6089 statement = error_mark_node;
6091 else
6092 statement = finish_continue_stmt ();
6093 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6094 break;
6096 case RID_RETURN:
6098 tree expr;
6100 /* If the next token is a `;', then there is no
6101 expression. */
6102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6103 expr = cp_parser_expression (parser);
6104 else
6105 expr = NULL_TREE;
6106 /* Build the return-statement. */
6107 statement = finish_return_stmt (expr);
6108 /* Look for the final `;'. */
6109 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6111 break;
6113 case RID_GOTO:
6114 /* Create the goto-statement. */
6115 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6117 /* Issue a warning about this use of a GNU extension. */
6118 if (pedantic)
6119 pedwarn ("ISO C++ forbids computed gotos");
6120 /* Consume the '*' token. */
6121 cp_lexer_consume_token (parser->lexer);
6122 /* Parse the dependent expression. */
6123 finish_goto_stmt (cp_parser_expression (parser));
6125 else
6126 finish_goto_stmt (cp_parser_identifier (parser));
6127 /* Look for the final `;'. */
6128 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6129 break;
6131 default:
6132 cp_parser_error (parser, "expected jump-statement");
6133 break;
6136 return statement;
6139 /* Parse a declaration-statement.
6141 declaration-statement:
6142 block-declaration */
6144 static void
6145 cp_parser_declaration_statement (cp_parser* parser)
6147 /* Parse the block-declaration. */
6148 cp_parser_block_declaration (parser, /*statement_p=*/true);
6150 /* Finish off the statement. */
6151 finish_stmt ();
6154 /* Some dependent statements (like `if (cond) statement'), are
6155 implicitly in their own scope. In other words, if the statement is
6156 a single statement (as opposed to a compound-statement), it is
6157 none-the-less treated as if it were enclosed in braces. Any
6158 declarations appearing in the dependent statement are out of scope
6159 after control passes that point. This function parses a statement,
6160 but ensures that is in its own scope, even if it is not a
6161 compound-statement.
6163 Returns the new statement. */
6165 static tree
6166 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6168 tree statement;
6170 /* If the token is not a `{', then we must take special action. */
6171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6173 /* Create a compound-statement. */
6174 statement = begin_compound_stmt (/*has_no_scope=*/false);
6175 /* Parse the dependent-statement. */
6176 cp_parser_statement (parser, false);
6177 /* Finish the dummy compound-statement. */
6178 finish_compound_stmt (statement);
6180 /* Otherwise, we simply parse the statement directly. */
6181 else
6182 statement = cp_parser_compound_statement (parser, false);
6184 /* Return the statement. */
6185 return statement;
6188 /* For some dependent statements (like `while (cond) statement'), we
6189 have already created a scope. Therefore, even if the dependent
6190 statement is a compound-statement, we do not want to create another
6191 scope. */
6193 static void
6194 cp_parser_already_scoped_statement (cp_parser* parser)
6196 /* If the token is not a `{', then we must take special action. */
6197 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6199 tree statement;
6201 /* Create a compound-statement. */
6202 statement = begin_compound_stmt (/*has_no_scope=*/true);
6203 /* Parse the dependent-statement. */
6204 cp_parser_statement (parser, false);
6205 /* Finish the dummy compound-statement. */
6206 finish_compound_stmt (statement);
6208 /* Otherwise, we simply parse the statement directly. */
6209 else
6210 cp_parser_statement (parser, false);
6213 /* Declarations [gram.dcl.dcl] */
6215 /* Parse an optional declaration-sequence.
6217 declaration-seq:
6218 declaration
6219 declaration-seq declaration */
6221 static void
6222 cp_parser_declaration_seq_opt (cp_parser* parser)
6224 while (true)
6226 cp_token *token;
6228 token = cp_lexer_peek_token (parser->lexer);
6230 if (token->type == CPP_CLOSE_BRACE
6231 || token->type == CPP_EOF)
6232 break;
6234 if (token->type == CPP_SEMICOLON)
6236 /* A declaration consisting of a single semicolon is
6237 invalid. Allow it unless we're being pedantic. */
6238 if (pedantic && !in_system_header)
6239 pedwarn ("extra `;'");
6240 cp_lexer_consume_token (parser->lexer);
6241 continue;
6244 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6245 parser to enter or exit implicit `extern "C"' blocks. */
6246 while (pending_lang_change > 0)
6248 push_lang_context (lang_name_c);
6249 --pending_lang_change;
6251 while (pending_lang_change < 0)
6253 pop_lang_context ();
6254 ++pending_lang_change;
6257 /* Parse the declaration itself. */
6258 cp_parser_declaration (parser);
6262 /* Parse a declaration.
6264 declaration:
6265 block-declaration
6266 function-definition
6267 template-declaration
6268 explicit-instantiation
6269 explicit-specialization
6270 linkage-specification
6271 namespace-definition
6273 GNU extension:
6275 declaration:
6276 __extension__ declaration */
6278 static void
6279 cp_parser_declaration (cp_parser* parser)
6281 cp_token token1;
6282 cp_token token2;
6283 int saved_pedantic;
6285 /* Check for the `__extension__' keyword. */
6286 if (cp_parser_extension_opt (parser, &saved_pedantic))
6288 /* Parse the qualified declaration. */
6289 cp_parser_declaration (parser);
6290 /* Restore the PEDANTIC flag. */
6291 pedantic = saved_pedantic;
6293 return;
6296 /* Try to figure out what kind of declaration is present. */
6297 token1 = *cp_lexer_peek_token (parser->lexer);
6298 if (token1.type != CPP_EOF)
6299 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6301 /* If the next token is `extern' and the following token is a string
6302 literal, then we have a linkage specification. */
6303 if (token1.keyword == RID_EXTERN
6304 && cp_parser_is_string_literal (&token2))
6305 cp_parser_linkage_specification (parser);
6306 /* If the next token is `template', then we have either a template
6307 declaration, an explicit instantiation, or an explicit
6308 specialization. */
6309 else if (token1.keyword == RID_TEMPLATE)
6311 /* `template <>' indicates a template specialization. */
6312 if (token2.type == CPP_LESS
6313 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6314 cp_parser_explicit_specialization (parser);
6315 /* `template <' indicates a template declaration. */
6316 else if (token2.type == CPP_LESS)
6317 cp_parser_template_declaration (parser, /*member_p=*/false);
6318 /* Anything else must be an explicit instantiation. */
6319 else
6320 cp_parser_explicit_instantiation (parser);
6322 /* If the next token is `export', then we have a template
6323 declaration. */
6324 else if (token1.keyword == RID_EXPORT)
6325 cp_parser_template_declaration (parser, /*member_p=*/false);
6326 /* If the next token is `extern', 'static' or 'inline' and the one
6327 after that is `template', we have a GNU extended explicit
6328 instantiation directive. */
6329 else if (cp_parser_allow_gnu_extensions_p (parser)
6330 && (token1.keyword == RID_EXTERN
6331 || token1.keyword == RID_STATIC
6332 || token1.keyword == RID_INLINE)
6333 && token2.keyword == RID_TEMPLATE)
6334 cp_parser_explicit_instantiation (parser);
6335 /* If the next token is `namespace', check for a named or unnamed
6336 namespace definition. */
6337 else if (token1.keyword == RID_NAMESPACE
6338 && (/* A named namespace definition. */
6339 (token2.type == CPP_NAME
6340 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6341 == CPP_OPEN_BRACE))
6342 /* An unnamed namespace definition. */
6343 || token2.type == CPP_OPEN_BRACE))
6344 cp_parser_namespace_definition (parser);
6345 /* We must have either a block declaration or a function
6346 definition. */
6347 else
6348 /* Try to parse a block-declaration, or a function-definition. */
6349 cp_parser_block_declaration (parser, /*statement_p=*/false);
6352 /* Parse a block-declaration.
6354 block-declaration:
6355 simple-declaration
6356 asm-definition
6357 namespace-alias-definition
6358 using-declaration
6359 using-directive
6361 GNU Extension:
6363 block-declaration:
6364 __extension__ block-declaration
6365 label-declaration
6367 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6368 part of a declaration-statement. */
6370 static void
6371 cp_parser_block_declaration (cp_parser *parser,
6372 bool statement_p)
6374 cp_token *token1;
6375 int saved_pedantic;
6377 /* Check for the `__extension__' keyword. */
6378 if (cp_parser_extension_opt (parser, &saved_pedantic))
6380 /* Parse the qualified declaration. */
6381 cp_parser_block_declaration (parser, statement_p);
6382 /* Restore the PEDANTIC flag. */
6383 pedantic = saved_pedantic;
6385 return;
6388 /* Peek at the next token to figure out which kind of declaration is
6389 present. */
6390 token1 = cp_lexer_peek_token (parser->lexer);
6392 /* If the next keyword is `asm', we have an asm-definition. */
6393 if (token1->keyword == RID_ASM)
6395 if (statement_p)
6396 cp_parser_commit_to_tentative_parse (parser);
6397 cp_parser_asm_definition (parser);
6399 /* If the next keyword is `namespace', we have a
6400 namespace-alias-definition. */
6401 else if (token1->keyword == RID_NAMESPACE)
6402 cp_parser_namespace_alias_definition (parser);
6403 /* If the next keyword is `using', we have either a
6404 using-declaration or a using-directive. */
6405 else if (token1->keyword == RID_USING)
6407 cp_token *token2;
6409 if (statement_p)
6410 cp_parser_commit_to_tentative_parse (parser);
6411 /* If the token after `using' is `namespace', then we have a
6412 using-directive. */
6413 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6414 if (token2->keyword == RID_NAMESPACE)
6415 cp_parser_using_directive (parser);
6416 /* Otherwise, it's a using-declaration. */
6417 else
6418 cp_parser_using_declaration (parser);
6420 /* If the next keyword is `__label__' we have a label declaration. */
6421 else if (token1->keyword == RID_LABEL)
6423 if (statement_p)
6424 cp_parser_commit_to_tentative_parse (parser);
6425 cp_parser_label_declaration (parser);
6427 /* Anything else must be a simple-declaration. */
6428 else
6429 cp_parser_simple_declaration (parser, !statement_p);
6432 /* Parse a simple-declaration.
6434 simple-declaration:
6435 decl-specifier-seq [opt] init-declarator-list [opt] ;
6437 init-declarator-list:
6438 init-declarator
6439 init-declarator-list , init-declarator
6441 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6442 function-definition as a simple-declaration. */
6444 static void
6445 cp_parser_simple_declaration (cp_parser* parser,
6446 bool function_definition_allowed_p)
6448 tree decl_specifiers;
6449 tree attributes;
6450 int declares_class_or_enum;
6451 bool saw_declarator;
6453 /* Defer access checks until we know what is being declared; the
6454 checks for names appearing in the decl-specifier-seq should be
6455 done as if we were in the scope of the thing being declared. */
6456 push_deferring_access_checks (dk_deferred);
6458 /* Parse the decl-specifier-seq. We have to keep track of whether
6459 or not the decl-specifier-seq declares a named class or
6460 enumeration type, since that is the only case in which the
6461 init-declarator-list is allowed to be empty.
6463 [dcl.dcl]
6465 In a simple-declaration, the optional init-declarator-list can be
6466 omitted only when declaring a class or enumeration, that is when
6467 the decl-specifier-seq contains either a class-specifier, an
6468 elaborated-type-specifier, or an enum-specifier. */
6469 decl_specifiers
6470 = cp_parser_decl_specifier_seq (parser,
6471 CP_PARSER_FLAGS_OPTIONAL,
6472 &attributes,
6473 &declares_class_or_enum);
6474 /* We no longer need to defer access checks. */
6475 stop_deferring_access_checks ();
6477 /* In a block scope, a valid declaration must always have a
6478 decl-specifier-seq. By not trying to parse declarators, we can
6479 resolve the declaration/expression ambiguity more quickly. */
6480 if (!function_definition_allowed_p && !decl_specifiers)
6482 cp_parser_error (parser, "expected declaration");
6483 goto done;
6486 /* If the next two tokens are both identifiers, the code is
6487 erroneous. The usual cause of this situation is code like:
6489 T t;
6491 where "T" should name a type -- but does not. */
6492 if (cp_parser_diagnose_invalid_type_name (parser))
6494 /* If parsing tentatively, we should commit; we really are
6495 looking at a declaration. */
6496 cp_parser_commit_to_tentative_parse (parser);
6497 /* Give up. */
6498 goto done;
6501 /* If we have seen at least one decl-specifier, and the next token
6502 is not a parenthesis, then we must be looking at a declaration.
6503 (After "int (" we might be looking at a functional cast.) */
6504 if (decl_specifiers
6505 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6506 cp_parser_commit_to_tentative_parse (parser);
6508 /* Keep going until we hit the `;' at the end of the simple
6509 declaration. */
6510 saw_declarator = false;
6511 while (cp_lexer_next_token_is_not (parser->lexer,
6512 CPP_SEMICOLON))
6514 cp_token *token;
6515 bool function_definition_p;
6516 tree decl;
6518 saw_declarator = true;
6519 /* Parse the init-declarator. */
6520 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6521 function_definition_allowed_p,
6522 /*member_p=*/false,
6523 declares_class_or_enum,
6524 &function_definition_p);
6525 /* If an error occurred while parsing tentatively, exit quickly.
6526 (That usually happens when in the body of a function; each
6527 statement is treated as a declaration-statement until proven
6528 otherwise.) */
6529 if (cp_parser_error_occurred (parser))
6530 goto done;
6531 /* Handle function definitions specially. */
6532 if (function_definition_p)
6534 /* If the next token is a `,', then we are probably
6535 processing something like:
6537 void f() {}, *p;
6539 which is erroneous. */
6540 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6541 error ("mixing declarations and function-definitions is forbidden");
6542 /* Otherwise, we're done with the list of declarators. */
6543 else
6545 pop_deferring_access_checks ();
6546 return;
6549 /* The next token should be either a `,' or a `;'. */
6550 token = cp_lexer_peek_token (parser->lexer);
6551 /* If it's a `,', there are more declarators to come. */
6552 if (token->type == CPP_COMMA)
6553 cp_lexer_consume_token (parser->lexer);
6554 /* If it's a `;', we are done. */
6555 else if (token->type == CPP_SEMICOLON)
6556 break;
6557 /* Anything else is an error. */
6558 else
6560 /* If we have already issued an error message we don't need
6561 to issue another one. */
6562 if (decl != error_mark_node
6563 || (cp_parser_parsing_tentatively (parser)
6564 && !cp_parser_committed_to_tentative_parse (parser)))
6565 cp_parser_error (parser, "expected `,' or `;'");
6566 /* Skip tokens until we reach the end of the statement. */
6567 cp_parser_skip_to_end_of_statement (parser);
6568 /* If the next token is now a `;', consume it. */
6569 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6570 cp_lexer_consume_token (parser->lexer);
6571 goto done;
6573 /* After the first time around, a function-definition is not
6574 allowed -- even if it was OK at first. For example:
6576 int i, f() {}
6578 is not valid. */
6579 function_definition_allowed_p = false;
6582 /* Issue an error message if no declarators are present, and the
6583 decl-specifier-seq does not itself declare a class or
6584 enumeration. */
6585 if (!saw_declarator)
6587 if (cp_parser_declares_only_class_p (parser))
6588 shadow_tag (decl_specifiers);
6589 /* Perform any deferred access checks. */
6590 perform_deferred_access_checks ();
6593 /* Consume the `;'. */
6594 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6596 done:
6597 pop_deferring_access_checks ();
6600 /* Parse a decl-specifier-seq.
6602 decl-specifier-seq:
6603 decl-specifier-seq [opt] decl-specifier
6605 decl-specifier:
6606 storage-class-specifier
6607 type-specifier
6608 function-specifier
6609 friend
6610 typedef
6612 GNU Extension:
6614 decl-specifier:
6615 attributes
6617 Returns a TREE_LIST, giving the decl-specifiers in the order they
6618 appear in the source code. The TREE_VALUE of each node is the
6619 decl-specifier. For a keyword (such as `auto' or `friend'), the
6620 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6621 representation of a type-specifier, see cp_parser_type_specifier.
6623 If there are attributes, they will be stored in *ATTRIBUTES,
6624 represented as described above cp_parser_attributes.
6626 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6627 appears, and the entity that will be a friend is not going to be a
6628 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6629 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6630 friendship is granted might not be a class.
6632 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6633 flags:
6635 1: one of the decl-specifiers is an elaborated-type-specifier
6636 (i.e., a type declaration)
6637 2: one of the decl-specifiers is an enum-specifier or a
6638 class-specifier (i.e., a type definition)
6642 static tree
6643 cp_parser_decl_specifier_seq (cp_parser* parser,
6644 cp_parser_flags flags,
6645 tree* attributes,
6646 int* declares_class_or_enum)
6648 tree decl_specs = NULL_TREE;
6649 bool friend_p = false;
6650 bool constructor_possible_p = !parser->in_declarator_p;
6652 /* Assume no class or enumeration type is declared. */
6653 *declares_class_or_enum = 0;
6655 /* Assume there are no attributes. */
6656 *attributes = NULL_TREE;
6658 /* Keep reading specifiers until there are no more to read. */
6659 while (true)
6661 tree decl_spec = NULL_TREE;
6662 bool constructor_p;
6663 cp_token *token;
6665 /* Peek at the next token. */
6666 token = cp_lexer_peek_token (parser->lexer);
6667 /* Handle attributes. */
6668 if (token->keyword == RID_ATTRIBUTE)
6670 /* Parse the attributes. */
6671 decl_spec = cp_parser_attributes_opt (parser);
6672 /* Add them to the list. */
6673 *attributes = chainon (*attributes, decl_spec);
6674 continue;
6676 /* If the next token is an appropriate keyword, we can simply
6677 add it to the list. */
6678 switch (token->keyword)
6680 case RID_FRIEND:
6681 /* decl-specifier:
6682 friend */
6683 if (friend_p)
6684 error ("duplicate `friend'");
6685 else
6686 friend_p = true;
6687 /* The representation of the specifier is simply the
6688 appropriate TREE_IDENTIFIER node. */
6689 decl_spec = token->value;
6690 /* Consume the token. */
6691 cp_lexer_consume_token (parser->lexer);
6692 break;
6694 /* function-specifier:
6695 inline
6696 virtual
6697 explicit */
6698 case RID_INLINE:
6699 case RID_VIRTUAL:
6700 case RID_EXPLICIT:
6701 decl_spec = cp_parser_function_specifier_opt (parser);
6702 break;
6704 /* decl-specifier:
6705 typedef */
6706 case RID_TYPEDEF:
6707 /* The representation of the specifier is simply the
6708 appropriate TREE_IDENTIFIER node. */
6709 decl_spec = token->value;
6710 /* Consume the token. */
6711 cp_lexer_consume_token (parser->lexer);
6712 /* A constructor declarator cannot appear in a typedef. */
6713 constructor_possible_p = false;
6714 /* The "typedef" keyword can only occur in a declaration; we
6715 may as well commit at this point. */
6716 cp_parser_commit_to_tentative_parse (parser);
6717 break;
6719 /* storage-class-specifier:
6720 auto
6721 register
6722 static
6723 extern
6724 mutable
6726 GNU Extension:
6727 thread */
6728 case RID_AUTO:
6729 case RID_REGISTER:
6730 case RID_STATIC:
6731 case RID_EXTERN:
6732 case RID_MUTABLE:
6733 case RID_THREAD:
6734 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6735 break;
6737 default:
6738 break;
6741 /* Constructors are a special case. The `S' in `S()' is not a
6742 decl-specifier; it is the beginning of the declarator. */
6743 constructor_p = (!decl_spec
6744 && constructor_possible_p
6745 && cp_parser_constructor_declarator_p (parser,
6746 friend_p));
6748 /* If we don't have a DECL_SPEC yet, then we must be looking at
6749 a type-specifier. */
6750 if (!decl_spec && !constructor_p)
6752 int decl_spec_declares_class_or_enum;
6753 bool is_cv_qualifier;
6755 decl_spec
6756 = cp_parser_type_specifier (parser, flags,
6757 friend_p,
6758 /*is_declaration=*/true,
6759 &decl_spec_declares_class_or_enum,
6760 &is_cv_qualifier);
6762 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6764 /* If this type-specifier referenced a user-defined type
6765 (a typedef, class-name, etc.), then we can't allow any
6766 more such type-specifiers henceforth.
6768 [dcl.spec]
6770 The longest sequence of decl-specifiers that could
6771 possibly be a type name is taken as the
6772 decl-specifier-seq of a declaration. The sequence shall
6773 be self-consistent as described below.
6775 [dcl.type]
6777 As a general rule, at most one type-specifier is allowed
6778 in the complete decl-specifier-seq of a declaration. The
6779 only exceptions are the following:
6781 -- const or volatile can be combined with any other
6782 type-specifier.
6784 -- signed or unsigned can be combined with char, long,
6785 short, or int.
6787 -- ..
6789 Example:
6791 typedef char* Pc;
6792 void g (const int Pc);
6794 Here, Pc is *not* part of the decl-specifier seq; it's
6795 the declarator. Therefore, once we see a type-specifier
6796 (other than a cv-qualifier), we forbid any additional
6797 user-defined types. We *do* still allow things like `int
6798 int' to be considered a decl-specifier-seq, and issue the
6799 error message later. */
6800 if (decl_spec && !is_cv_qualifier)
6801 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6802 /* A constructor declarator cannot follow a type-specifier. */
6803 if (decl_spec)
6804 constructor_possible_p = false;
6807 /* If we still do not have a DECL_SPEC, then there are no more
6808 decl-specifiers. */
6809 if (!decl_spec)
6811 /* Issue an error message, unless the entire construct was
6812 optional. */
6813 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6815 cp_parser_error (parser, "expected decl specifier");
6816 return error_mark_node;
6819 break;
6822 /* Add the DECL_SPEC to the list of specifiers. */
6823 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6824 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6826 /* After we see one decl-specifier, further decl-specifiers are
6827 always optional. */
6828 flags |= CP_PARSER_FLAGS_OPTIONAL;
6831 /* Don't allow a friend specifier with a class definition. */
6832 if (friend_p && (*declares_class_or_enum & 2))
6833 error ("class definition may not be declared a friend");
6835 /* We have built up the DECL_SPECS in reverse order. Return them in
6836 the correct order. */
6837 return nreverse (decl_specs);
6840 /* Parse an (optional) storage-class-specifier.
6842 storage-class-specifier:
6843 auto
6844 register
6845 static
6846 extern
6847 mutable
6849 GNU Extension:
6851 storage-class-specifier:
6852 thread
6854 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6856 static tree
6857 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6859 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6861 case RID_AUTO:
6862 case RID_REGISTER:
6863 case RID_STATIC:
6864 case RID_EXTERN:
6865 case RID_MUTABLE:
6866 case RID_THREAD:
6867 /* Consume the token. */
6868 return cp_lexer_consume_token (parser->lexer)->value;
6870 default:
6871 return NULL_TREE;
6875 /* Parse an (optional) function-specifier.
6877 function-specifier:
6878 inline
6879 virtual
6880 explicit
6882 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6884 static tree
6885 cp_parser_function_specifier_opt (cp_parser* parser)
6887 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6889 case RID_INLINE:
6890 case RID_VIRTUAL:
6891 case RID_EXPLICIT:
6892 /* Consume the token. */
6893 return cp_lexer_consume_token (parser->lexer)->value;
6895 default:
6896 return NULL_TREE;
6900 /* Parse a linkage-specification.
6902 linkage-specification:
6903 extern string-literal { declaration-seq [opt] }
6904 extern string-literal declaration */
6906 static void
6907 cp_parser_linkage_specification (cp_parser* parser)
6909 cp_token *token;
6910 tree linkage;
6912 /* Look for the `extern' keyword. */
6913 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6915 /* Peek at the next token. */
6916 token = cp_lexer_peek_token (parser->lexer);
6917 /* If it's not a string-literal, then there's a problem. */
6918 if (!cp_parser_is_string_literal (token))
6920 cp_parser_error (parser, "expected language-name");
6921 return;
6923 /* Consume the token. */
6924 cp_lexer_consume_token (parser->lexer);
6926 /* Transform the literal into an identifier. If the literal is a
6927 wide-character string, or contains embedded NULs, then we can't
6928 handle it as the user wants. */
6929 if (token->type == CPP_WSTRING
6930 || (strlen (TREE_STRING_POINTER (token->value))
6931 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6933 cp_parser_error (parser, "invalid linkage-specification");
6934 /* Assume C++ linkage. */
6935 linkage = get_identifier ("c++");
6937 /* If it's a simple string constant, things are easier. */
6938 else
6939 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6941 /* We're now using the new linkage. */
6942 push_lang_context (linkage);
6944 /* If the next token is a `{', then we're using the first
6945 production. */
6946 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6948 /* Consume the `{' token. */
6949 cp_lexer_consume_token (parser->lexer);
6950 /* Parse the declarations. */
6951 cp_parser_declaration_seq_opt (parser);
6952 /* Look for the closing `}'. */
6953 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6955 /* Otherwise, there's just one declaration. */
6956 else
6958 bool saved_in_unbraced_linkage_specification_p;
6960 saved_in_unbraced_linkage_specification_p
6961 = parser->in_unbraced_linkage_specification_p;
6962 parser->in_unbraced_linkage_specification_p = true;
6963 have_extern_spec = true;
6964 cp_parser_declaration (parser);
6965 have_extern_spec = false;
6966 parser->in_unbraced_linkage_specification_p
6967 = saved_in_unbraced_linkage_specification_p;
6970 /* We're done with the linkage-specification. */
6971 pop_lang_context ();
6974 /* Special member functions [gram.special] */
6976 /* Parse a conversion-function-id.
6978 conversion-function-id:
6979 operator conversion-type-id
6981 Returns an IDENTIFIER_NODE representing the operator. */
6983 static tree
6984 cp_parser_conversion_function_id (cp_parser* parser)
6986 tree type;
6987 tree saved_scope;
6988 tree saved_qualifying_scope;
6989 tree saved_object_scope;
6990 bool pop_p = false;
6992 /* Look for the `operator' token. */
6993 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6994 return error_mark_node;
6995 /* When we parse the conversion-type-id, the current scope will be
6996 reset. However, we need that information in able to look up the
6997 conversion function later, so we save it here. */
6998 saved_scope = parser->scope;
6999 saved_qualifying_scope = parser->qualifying_scope;
7000 saved_object_scope = parser->object_scope;
7001 /* We must enter the scope of the class so that the names of
7002 entities declared within the class are available in the
7003 conversion-type-id. For example, consider:
7005 struct S {
7006 typedef int I;
7007 operator I();
7010 S::operator I() { ... }
7012 In order to see that `I' is a type-name in the definition, we
7013 must be in the scope of `S'. */
7014 if (saved_scope)
7015 pop_p = push_scope (saved_scope);
7016 /* Parse the conversion-type-id. */
7017 type = cp_parser_conversion_type_id (parser);
7018 /* Leave the scope of the class, if any. */
7019 if (pop_p)
7020 pop_scope (saved_scope);
7021 /* Restore the saved scope. */
7022 parser->scope = saved_scope;
7023 parser->qualifying_scope = saved_qualifying_scope;
7024 parser->object_scope = saved_object_scope;
7025 /* If the TYPE is invalid, indicate failure. */
7026 if (type == error_mark_node)
7027 return error_mark_node;
7028 return mangle_conv_op_name_for_type (type);
7031 /* Parse a conversion-type-id:
7033 conversion-type-id:
7034 type-specifier-seq conversion-declarator [opt]
7036 Returns the TYPE specified. */
7038 static tree
7039 cp_parser_conversion_type_id (cp_parser* parser)
7041 tree attributes;
7042 tree type_specifiers;
7043 tree declarator;
7045 /* Parse the attributes. */
7046 attributes = cp_parser_attributes_opt (parser);
7047 /* Parse the type-specifiers. */
7048 type_specifiers = cp_parser_type_specifier_seq (parser);
7049 /* If that didn't work, stop. */
7050 if (type_specifiers == error_mark_node)
7051 return error_mark_node;
7052 /* Parse the conversion-declarator. */
7053 declarator = cp_parser_conversion_declarator_opt (parser);
7055 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7056 /*initialized=*/0, &attributes);
7059 /* Parse an (optional) conversion-declarator.
7061 conversion-declarator:
7062 ptr-operator conversion-declarator [opt]
7064 Returns a representation of the declarator. See
7065 cp_parser_declarator for details. */
7067 static tree
7068 cp_parser_conversion_declarator_opt (cp_parser* parser)
7070 enum tree_code code;
7071 tree class_type;
7072 tree cv_qualifier_seq;
7074 /* We don't know if there's a ptr-operator next, or not. */
7075 cp_parser_parse_tentatively (parser);
7076 /* Try the ptr-operator. */
7077 code = cp_parser_ptr_operator (parser, &class_type,
7078 &cv_qualifier_seq);
7079 /* If it worked, look for more conversion-declarators. */
7080 if (cp_parser_parse_definitely (parser))
7082 tree declarator;
7084 /* Parse another optional declarator. */
7085 declarator = cp_parser_conversion_declarator_opt (parser);
7087 /* Create the representation of the declarator. */
7088 if (code == INDIRECT_REF)
7089 declarator = make_pointer_declarator (cv_qualifier_seq,
7090 declarator);
7091 else
7092 declarator = make_reference_declarator (cv_qualifier_seq,
7093 declarator);
7095 /* Handle the pointer-to-member case. */
7096 if (class_type)
7097 declarator = build_nt (SCOPE_REF, class_type, declarator);
7099 return declarator;
7102 return NULL_TREE;
7105 /* Parse an (optional) ctor-initializer.
7107 ctor-initializer:
7108 : mem-initializer-list
7110 Returns TRUE iff the ctor-initializer was actually present. */
7112 static bool
7113 cp_parser_ctor_initializer_opt (cp_parser* parser)
7115 /* If the next token is not a `:', then there is no
7116 ctor-initializer. */
7117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7119 /* Do default initialization of any bases and members. */
7120 if (DECL_CONSTRUCTOR_P (current_function_decl))
7121 finish_mem_initializers (NULL_TREE);
7123 return false;
7126 /* Consume the `:' token. */
7127 cp_lexer_consume_token (parser->lexer);
7128 /* And the mem-initializer-list. */
7129 cp_parser_mem_initializer_list (parser);
7131 return true;
7134 /* Parse a mem-initializer-list.
7136 mem-initializer-list:
7137 mem-initializer
7138 mem-initializer , mem-initializer-list */
7140 static void
7141 cp_parser_mem_initializer_list (cp_parser* parser)
7143 tree mem_initializer_list = NULL_TREE;
7145 /* Let the semantic analysis code know that we are starting the
7146 mem-initializer-list. */
7147 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7148 error ("only constructors take base initializers");
7150 /* Loop through the list. */
7151 while (true)
7153 tree mem_initializer;
7155 /* Parse the mem-initializer. */
7156 mem_initializer = cp_parser_mem_initializer (parser);
7157 /* Add it to the list, unless it was erroneous. */
7158 if (mem_initializer)
7160 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7161 mem_initializer_list = mem_initializer;
7163 /* If the next token is not a `,', we're done. */
7164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7165 break;
7166 /* Consume the `,' token. */
7167 cp_lexer_consume_token (parser->lexer);
7170 /* Perform semantic analysis. */
7171 if (DECL_CONSTRUCTOR_P (current_function_decl))
7172 finish_mem_initializers (mem_initializer_list);
7175 /* Parse a mem-initializer.
7177 mem-initializer:
7178 mem-initializer-id ( expression-list [opt] )
7180 GNU extension:
7182 mem-initializer:
7183 ( expression-list [opt] )
7185 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7186 class) or FIELD_DECL (for a non-static data member) to initialize;
7187 the TREE_VALUE is the expression-list. */
7189 static tree
7190 cp_parser_mem_initializer (cp_parser* parser)
7192 tree mem_initializer_id;
7193 tree expression_list;
7194 tree member;
7196 /* Find out what is being initialized. */
7197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7199 pedwarn ("anachronistic old-style base class initializer");
7200 mem_initializer_id = NULL_TREE;
7202 else
7203 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7204 member = expand_member_init (mem_initializer_id);
7205 if (member && !DECL_P (member))
7206 in_base_initializer = 1;
7208 expression_list
7209 = cp_parser_parenthesized_expression_list (parser, false,
7210 /*non_constant_p=*/NULL);
7211 if (!expression_list)
7212 expression_list = void_type_node;
7214 in_base_initializer = 0;
7216 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7219 /* Parse a mem-initializer-id.
7221 mem-initializer-id:
7222 :: [opt] nested-name-specifier [opt] class-name
7223 identifier
7225 Returns a TYPE indicating the class to be initializer for the first
7226 production. Returns an IDENTIFIER_NODE indicating the data member
7227 to be initialized for the second production. */
7229 static tree
7230 cp_parser_mem_initializer_id (cp_parser* parser)
7232 bool global_scope_p;
7233 bool nested_name_specifier_p;
7234 bool template_p = false;
7235 tree id;
7237 /* `typename' is not allowed in this context ([temp.res]). */
7238 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7240 error ("keyword `typename' not allowed in this context (a qualified "
7241 "member initializer is implicitly a type)");
7242 cp_lexer_consume_token (parser->lexer);
7244 /* Look for the optional `::' operator. */
7245 global_scope_p
7246 = (cp_parser_global_scope_opt (parser,
7247 /*current_scope_valid_p=*/false)
7248 != NULL_TREE);
7249 /* Look for the optional nested-name-specifier. The simplest way to
7250 implement:
7252 [temp.res]
7254 The keyword `typename' is not permitted in a base-specifier or
7255 mem-initializer; in these contexts a qualified name that
7256 depends on a template-parameter is implicitly assumed to be a
7257 type name.
7259 is to assume that we have seen the `typename' keyword at this
7260 point. */
7261 nested_name_specifier_p
7262 = (cp_parser_nested_name_specifier_opt (parser,
7263 /*typename_keyword_p=*/true,
7264 /*check_dependency_p=*/true,
7265 /*type_p=*/true,
7266 /*is_declaration=*/true)
7267 != NULL_TREE);
7268 if (nested_name_specifier_p)
7269 template_p = cp_parser_optional_template_keyword (parser);
7270 /* If there is a `::' operator or a nested-name-specifier, then we
7271 are definitely looking for a class-name. */
7272 if (global_scope_p || nested_name_specifier_p)
7273 return cp_parser_class_name (parser,
7274 /*typename_keyword_p=*/true,
7275 /*template_keyword_p=*/template_p,
7276 /*type_p=*/false,
7277 /*check_dependency_p=*/true,
7278 /*class_head_p=*/false,
7279 /*is_declaration=*/true);
7280 /* Otherwise, we could also be looking for an ordinary identifier. */
7281 cp_parser_parse_tentatively (parser);
7282 /* Try a class-name. */
7283 id = cp_parser_class_name (parser,
7284 /*typename_keyword_p=*/true,
7285 /*template_keyword_p=*/false,
7286 /*type_p=*/false,
7287 /*check_dependency_p=*/true,
7288 /*class_head_p=*/false,
7289 /*is_declaration=*/true);
7290 /* If we found one, we're done. */
7291 if (cp_parser_parse_definitely (parser))
7292 return id;
7293 /* Otherwise, look for an ordinary identifier. */
7294 return cp_parser_identifier (parser);
7297 /* Overloading [gram.over] */
7299 /* Parse an operator-function-id.
7301 operator-function-id:
7302 operator operator
7304 Returns an IDENTIFIER_NODE for the operator which is a
7305 human-readable spelling of the identifier, e.g., `operator +'. */
7307 static tree
7308 cp_parser_operator_function_id (cp_parser* parser)
7310 /* Look for the `operator' keyword. */
7311 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7312 return error_mark_node;
7313 /* And then the name of the operator itself. */
7314 return cp_parser_operator (parser);
7317 /* Parse an operator.
7319 operator:
7320 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7321 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7322 || ++ -- , ->* -> () []
7324 GNU Extensions:
7326 operator:
7327 <? >? <?= >?=
7329 Returns an IDENTIFIER_NODE for the operator which is a
7330 human-readable spelling of the identifier, e.g., `operator +'. */
7332 static tree
7333 cp_parser_operator (cp_parser* parser)
7335 tree id = NULL_TREE;
7336 cp_token *token;
7338 /* Peek at the next token. */
7339 token = cp_lexer_peek_token (parser->lexer);
7340 /* Figure out which operator we have. */
7341 switch (token->type)
7343 case CPP_KEYWORD:
7345 enum tree_code op;
7347 /* The keyword should be either `new' or `delete'. */
7348 if (token->keyword == RID_NEW)
7349 op = NEW_EXPR;
7350 else if (token->keyword == RID_DELETE)
7351 op = DELETE_EXPR;
7352 else
7353 break;
7355 /* Consume the `new' or `delete' token. */
7356 cp_lexer_consume_token (parser->lexer);
7358 /* Peek at the next token. */
7359 token = cp_lexer_peek_token (parser->lexer);
7360 /* If it's a `[' token then this is the array variant of the
7361 operator. */
7362 if (token->type == CPP_OPEN_SQUARE)
7364 /* Consume the `[' token. */
7365 cp_lexer_consume_token (parser->lexer);
7366 /* Look for the `]' token. */
7367 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7368 id = ansi_opname (op == NEW_EXPR
7369 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7371 /* Otherwise, we have the non-array variant. */
7372 else
7373 id = ansi_opname (op);
7375 return id;
7378 case CPP_PLUS:
7379 id = ansi_opname (PLUS_EXPR);
7380 break;
7382 case CPP_MINUS:
7383 id = ansi_opname (MINUS_EXPR);
7384 break;
7386 case CPP_MULT:
7387 id = ansi_opname (MULT_EXPR);
7388 break;
7390 case CPP_DIV:
7391 id = ansi_opname (TRUNC_DIV_EXPR);
7392 break;
7394 case CPP_MOD:
7395 id = ansi_opname (TRUNC_MOD_EXPR);
7396 break;
7398 case CPP_XOR:
7399 id = ansi_opname (BIT_XOR_EXPR);
7400 break;
7402 case CPP_AND:
7403 id = ansi_opname (BIT_AND_EXPR);
7404 break;
7406 case CPP_OR:
7407 id = ansi_opname (BIT_IOR_EXPR);
7408 break;
7410 case CPP_COMPL:
7411 id = ansi_opname (BIT_NOT_EXPR);
7412 break;
7414 case CPP_NOT:
7415 id = ansi_opname (TRUTH_NOT_EXPR);
7416 break;
7418 case CPP_EQ:
7419 id = ansi_assopname (NOP_EXPR);
7420 break;
7422 case CPP_LESS:
7423 id = ansi_opname (LT_EXPR);
7424 break;
7426 case CPP_GREATER:
7427 id = ansi_opname (GT_EXPR);
7428 break;
7430 case CPP_PLUS_EQ:
7431 id = ansi_assopname (PLUS_EXPR);
7432 break;
7434 case CPP_MINUS_EQ:
7435 id = ansi_assopname (MINUS_EXPR);
7436 break;
7438 case CPP_MULT_EQ:
7439 id = ansi_assopname (MULT_EXPR);
7440 break;
7442 case CPP_DIV_EQ:
7443 id = ansi_assopname (TRUNC_DIV_EXPR);
7444 break;
7446 case CPP_MOD_EQ:
7447 id = ansi_assopname (TRUNC_MOD_EXPR);
7448 break;
7450 case CPP_XOR_EQ:
7451 id = ansi_assopname (BIT_XOR_EXPR);
7452 break;
7454 case CPP_AND_EQ:
7455 id = ansi_assopname (BIT_AND_EXPR);
7456 break;
7458 case CPP_OR_EQ:
7459 id = ansi_assopname (BIT_IOR_EXPR);
7460 break;
7462 case CPP_LSHIFT:
7463 id = ansi_opname (LSHIFT_EXPR);
7464 break;
7466 case CPP_RSHIFT:
7467 id = ansi_opname (RSHIFT_EXPR);
7468 break;
7470 case CPP_LSHIFT_EQ:
7471 id = ansi_assopname (LSHIFT_EXPR);
7472 break;
7474 case CPP_RSHIFT_EQ:
7475 id = ansi_assopname (RSHIFT_EXPR);
7476 break;
7478 case CPP_EQ_EQ:
7479 id = ansi_opname (EQ_EXPR);
7480 break;
7482 case CPP_NOT_EQ:
7483 id = ansi_opname (NE_EXPR);
7484 break;
7486 case CPP_LESS_EQ:
7487 id = ansi_opname (LE_EXPR);
7488 break;
7490 case CPP_GREATER_EQ:
7491 id = ansi_opname (GE_EXPR);
7492 break;
7494 case CPP_AND_AND:
7495 id = ansi_opname (TRUTH_ANDIF_EXPR);
7496 break;
7498 case CPP_OR_OR:
7499 id = ansi_opname (TRUTH_ORIF_EXPR);
7500 break;
7502 case CPP_PLUS_PLUS:
7503 id = ansi_opname (POSTINCREMENT_EXPR);
7504 break;
7506 case CPP_MINUS_MINUS:
7507 id = ansi_opname (PREDECREMENT_EXPR);
7508 break;
7510 case CPP_COMMA:
7511 id = ansi_opname (COMPOUND_EXPR);
7512 break;
7514 case CPP_DEREF_STAR:
7515 id = ansi_opname (MEMBER_REF);
7516 break;
7518 case CPP_DEREF:
7519 id = ansi_opname (COMPONENT_REF);
7520 break;
7522 case CPP_OPEN_PAREN:
7523 /* Consume the `('. */
7524 cp_lexer_consume_token (parser->lexer);
7525 /* Look for the matching `)'. */
7526 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7527 return ansi_opname (CALL_EXPR);
7529 case CPP_OPEN_SQUARE:
7530 /* Consume the `['. */
7531 cp_lexer_consume_token (parser->lexer);
7532 /* Look for the matching `]'. */
7533 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7534 return ansi_opname (ARRAY_REF);
7536 /* Extensions. */
7537 case CPP_MIN:
7538 id = ansi_opname (MIN_EXPR);
7539 break;
7541 case CPP_MAX:
7542 id = ansi_opname (MAX_EXPR);
7543 break;
7545 case CPP_MIN_EQ:
7546 id = ansi_assopname (MIN_EXPR);
7547 break;
7549 case CPP_MAX_EQ:
7550 id = ansi_assopname (MAX_EXPR);
7551 break;
7553 default:
7554 /* Anything else is an error. */
7555 break;
7558 /* If we have selected an identifier, we need to consume the
7559 operator token. */
7560 if (id)
7561 cp_lexer_consume_token (parser->lexer);
7562 /* Otherwise, no valid operator name was present. */
7563 else
7565 cp_parser_error (parser, "expected operator");
7566 id = error_mark_node;
7569 return id;
7572 /* Parse a template-declaration.
7574 template-declaration:
7575 export [opt] template < template-parameter-list > declaration
7577 If MEMBER_P is TRUE, this template-declaration occurs within a
7578 class-specifier.
7580 The grammar rule given by the standard isn't correct. What
7581 is really meant is:
7583 template-declaration:
7584 export [opt] template-parameter-list-seq
7585 decl-specifier-seq [opt] init-declarator [opt] ;
7586 export [opt] template-parameter-list-seq
7587 function-definition
7589 template-parameter-list-seq:
7590 template-parameter-list-seq [opt]
7591 template < template-parameter-list > */
7593 static void
7594 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7596 /* Check for `export'. */
7597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7599 /* Consume the `export' token. */
7600 cp_lexer_consume_token (parser->lexer);
7601 /* Warn that we do not support `export'. */
7602 warning ("keyword `export' not implemented, and will be ignored");
7605 cp_parser_template_declaration_after_export (parser, member_p);
7608 /* Parse a template-parameter-list.
7610 template-parameter-list:
7611 template-parameter
7612 template-parameter-list , template-parameter
7614 Returns a TREE_LIST. Each node represents a template parameter.
7615 The nodes are connected via their TREE_CHAINs. */
7617 static tree
7618 cp_parser_template_parameter_list (cp_parser* parser)
7620 tree parameter_list = NULL_TREE;
7622 while (true)
7624 tree parameter;
7625 cp_token *token;
7627 /* Parse the template-parameter. */
7628 parameter = cp_parser_template_parameter (parser);
7629 /* Add it to the list. */
7630 parameter_list = process_template_parm (parameter_list,
7631 parameter);
7633 /* Peek at the next token. */
7634 token = cp_lexer_peek_token (parser->lexer);
7635 /* If it's not a `,', we're done. */
7636 if (token->type != CPP_COMMA)
7637 break;
7638 /* Otherwise, consume the `,' token. */
7639 cp_lexer_consume_token (parser->lexer);
7642 return parameter_list;
7645 /* Parse a template-parameter.
7647 template-parameter:
7648 type-parameter
7649 parameter-declaration
7651 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7652 TREE_PURPOSE is the default value, if any. */
7654 static tree
7655 cp_parser_template_parameter (cp_parser* parser)
7657 cp_token *token;
7659 /* Peek at the next token. */
7660 token = cp_lexer_peek_token (parser->lexer);
7661 /* If it is `class' or `template', we have a type-parameter. */
7662 if (token->keyword == RID_TEMPLATE)
7663 return cp_parser_type_parameter (parser);
7664 /* If it is `class' or `typename' we do not know yet whether it is a
7665 type parameter or a non-type parameter. Consider:
7667 template <typename T, typename T::X X> ...
7671 template <class C, class D*> ...
7673 Here, the first parameter is a type parameter, and the second is
7674 a non-type parameter. We can tell by looking at the token after
7675 the identifier -- if it is a `,', `=', or `>' then we have a type
7676 parameter. */
7677 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7679 /* Peek at the token after `class' or `typename'. */
7680 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7681 /* If it's an identifier, skip it. */
7682 if (token->type == CPP_NAME)
7683 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7684 /* Now, see if the token looks like the end of a template
7685 parameter. */
7686 if (token->type == CPP_COMMA
7687 || token->type == CPP_EQ
7688 || token->type == CPP_GREATER)
7689 return cp_parser_type_parameter (parser);
7692 /* Otherwise, it is a non-type parameter.
7694 [temp.param]
7696 When parsing a default template-argument for a non-type
7697 template-parameter, the first non-nested `>' is taken as the end
7698 of the template parameter-list rather than a greater-than
7699 operator. */
7700 return
7701 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7702 /*parenthesized_p=*/NULL);
7705 /* Parse a type-parameter.
7707 type-parameter:
7708 class identifier [opt]
7709 class identifier [opt] = type-id
7710 typename identifier [opt]
7711 typename identifier [opt] = type-id
7712 template < template-parameter-list > class identifier [opt]
7713 template < template-parameter-list > class identifier [opt]
7714 = id-expression
7716 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7717 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7718 the declaration of the parameter. */
7720 static tree
7721 cp_parser_type_parameter (cp_parser* parser)
7723 cp_token *token;
7724 tree parameter;
7726 /* Look for a keyword to tell us what kind of parameter this is. */
7727 token = cp_parser_require (parser, CPP_KEYWORD,
7728 "`class', `typename', or `template'");
7729 if (!token)
7730 return error_mark_node;
7732 switch (token->keyword)
7734 case RID_CLASS:
7735 case RID_TYPENAME:
7737 tree identifier;
7738 tree default_argument;
7740 /* If the next token is an identifier, then it names the
7741 parameter. */
7742 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7743 identifier = cp_parser_identifier (parser);
7744 else
7745 identifier = NULL_TREE;
7747 /* Create the parameter. */
7748 parameter = finish_template_type_parm (class_type_node, identifier);
7750 /* If the next token is an `=', we have a default argument. */
7751 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7753 /* Consume the `=' token. */
7754 cp_lexer_consume_token (parser->lexer);
7755 /* Parse the default-argument. */
7756 default_argument = cp_parser_type_id (parser);
7758 else
7759 default_argument = NULL_TREE;
7761 /* Create the combined representation of the parameter and the
7762 default argument. */
7763 parameter = build_tree_list (default_argument, parameter);
7765 break;
7767 case RID_TEMPLATE:
7769 tree parameter_list;
7770 tree identifier;
7771 tree default_argument;
7773 /* Look for the `<'. */
7774 cp_parser_require (parser, CPP_LESS, "`<'");
7775 /* Parse the template-parameter-list. */
7776 begin_template_parm_list ();
7777 parameter_list
7778 = cp_parser_template_parameter_list (parser);
7779 parameter_list = end_template_parm_list (parameter_list);
7780 /* Look for the `>'. */
7781 cp_parser_require (parser, CPP_GREATER, "`>'");
7782 /* Look for the `class' keyword. */
7783 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7784 /* If the next token is an `=', then there is a
7785 default-argument. If the next token is a `>', we are at
7786 the end of the parameter-list. If the next token is a `,',
7787 then we are at the end of this parameter. */
7788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7789 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7790 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7792 identifier = cp_parser_identifier (parser);
7793 /* Treat invalid names as if the parameter were nameless. */
7794 if (identifier == error_mark_node)
7795 identifier = NULL_TREE;
7797 else
7798 identifier = NULL_TREE;
7800 /* Create the template parameter. */
7801 parameter = finish_template_template_parm (class_type_node,
7802 identifier);
7804 /* If the next token is an `=', then there is a
7805 default-argument. */
7806 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7808 bool is_template;
7810 /* Consume the `='. */
7811 cp_lexer_consume_token (parser->lexer);
7812 /* Parse the id-expression. */
7813 default_argument
7814 = cp_parser_id_expression (parser,
7815 /*template_keyword_p=*/false,
7816 /*check_dependency_p=*/true,
7817 /*template_p=*/&is_template,
7818 /*declarator_p=*/false);
7819 if (TREE_CODE (default_argument) == TYPE_DECL)
7820 /* If the id-expression was a template-id that refers to
7821 a template-class, we already have the declaration here,
7822 so no further lookup is needed. */
7824 else
7825 /* Look up the name. */
7826 default_argument
7827 = cp_parser_lookup_name (parser, default_argument,
7828 /*is_type=*/false,
7829 /*is_template=*/is_template,
7830 /*is_namespace=*/false,
7831 /*check_dependency=*/true);
7832 /* See if the default argument is valid. */
7833 default_argument
7834 = check_template_template_default_arg (default_argument);
7836 else
7837 default_argument = NULL_TREE;
7839 /* Create the combined representation of the parameter and the
7840 default argument. */
7841 parameter = build_tree_list (default_argument, parameter);
7843 break;
7845 default:
7846 abort ();
7847 break;
7850 return parameter;
7853 /* Parse a template-id.
7855 template-id:
7856 template-name < template-argument-list [opt] >
7858 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7859 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7860 returned. Otherwise, if the template-name names a function, or set
7861 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7862 names a class, returns a TYPE_DECL for the specialization.
7864 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7865 uninstantiated templates. */
7867 static tree
7868 cp_parser_template_id (cp_parser *parser,
7869 bool template_keyword_p,
7870 bool check_dependency_p,
7871 bool is_declaration)
7873 tree template;
7874 tree arguments;
7875 tree template_id;
7876 ptrdiff_t start_of_id;
7877 tree access_check = NULL_TREE;
7878 cp_token *next_token, *next_token_2;
7879 bool is_identifier;
7881 /* If the next token corresponds to a template-id, there is no need
7882 to reparse it. */
7883 next_token = cp_lexer_peek_token (parser->lexer);
7884 if (next_token->type == CPP_TEMPLATE_ID)
7886 tree value;
7887 tree check;
7889 /* Get the stored value. */
7890 value = cp_lexer_consume_token (parser->lexer)->value;
7891 /* Perform any access checks that were deferred. */
7892 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7893 perform_or_defer_access_check (TREE_PURPOSE (check),
7894 TREE_VALUE (check));
7895 /* Return the stored value. */
7896 return TREE_VALUE (value);
7899 /* Avoid performing name lookup if there is no possibility of
7900 finding a template-id. */
7901 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7902 || (next_token->type == CPP_NAME
7903 && !cp_parser_nth_token_starts_template_argument_list_p
7904 (parser, 2)))
7906 cp_parser_error (parser, "expected template-id");
7907 return error_mark_node;
7910 /* Remember where the template-id starts. */
7911 if (cp_parser_parsing_tentatively (parser)
7912 && !cp_parser_committed_to_tentative_parse (parser))
7914 next_token = cp_lexer_peek_token (parser->lexer);
7915 start_of_id = cp_lexer_token_difference (parser->lexer,
7916 parser->lexer->first_token,
7917 next_token);
7919 else
7920 start_of_id = -1;
7922 push_deferring_access_checks (dk_deferred);
7924 /* Parse the template-name. */
7925 is_identifier = false;
7926 template = cp_parser_template_name (parser, template_keyword_p,
7927 check_dependency_p,
7928 is_declaration,
7929 &is_identifier);
7930 if (template == error_mark_node || is_identifier)
7932 pop_deferring_access_checks ();
7933 return template;
7936 /* If we find the sequence `[:' after a template-name, it's probably
7937 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7938 parse correctly the argument list. */
7939 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7940 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7941 if (next_token->type == CPP_OPEN_SQUARE
7942 && next_token->flags & DIGRAPH
7943 && next_token_2->type == CPP_COLON
7944 && !(next_token_2->flags & PREV_WHITE))
7946 cp_parser_parse_tentatively (parser);
7947 /* Change `:' into `::'. */
7948 next_token_2->type = CPP_SCOPE;
7949 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7950 CPP_LESS. */
7951 cp_lexer_consume_token (parser->lexer);
7952 /* Parse the arguments. */
7953 arguments = cp_parser_enclosed_template_argument_list (parser);
7954 if (!cp_parser_parse_definitely (parser))
7956 /* If we couldn't parse an argument list, then we revert our changes
7957 and return simply an error. Maybe this is not a template-id
7958 after all. */
7959 next_token_2->type = CPP_COLON;
7960 cp_parser_error (parser, "expected `<'");
7961 pop_deferring_access_checks ();
7962 return error_mark_node;
7964 /* Otherwise, emit an error about the invalid digraph, but continue
7965 parsing because we got our argument list. */
7966 pedwarn ("`<::' cannot begin a template-argument list");
7967 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7968 "between `<' and `::'");
7969 if (!flag_permissive)
7971 static bool hint;
7972 if (!hint)
7974 inform ("(if you use `-fpermissive' G++ will accept your code)");
7975 hint = true;
7979 else
7981 /* Look for the `<' that starts the template-argument-list. */
7982 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7984 pop_deferring_access_checks ();
7985 return error_mark_node;
7987 /* Parse the arguments. */
7988 arguments = cp_parser_enclosed_template_argument_list (parser);
7991 /* Build a representation of the specialization. */
7992 if (TREE_CODE (template) == IDENTIFIER_NODE)
7993 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7994 else if (DECL_CLASS_TEMPLATE_P (template)
7995 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7996 template_id
7997 = finish_template_type (template, arguments,
7998 cp_lexer_next_token_is (parser->lexer,
7999 CPP_SCOPE));
8000 else
8002 /* If it's not a class-template or a template-template, it should be
8003 a function-template. */
8004 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8005 || TREE_CODE (template) == OVERLOAD
8006 || BASELINK_P (template)),
8007 20010716);
8009 template_id = lookup_template_function (template, arguments);
8012 /* Retrieve any deferred checks. Do not pop this access checks yet
8013 so the memory will not be reclaimed during token replacing below. */
8014 access_check = get_deferred_access_checks ();
8016 /* If parsing tentatively, replace the sequence of tokens that makes
8017 up the template-id with a CPP_TEMPLATE_ID token. That way,
8018 should we re-parse the token stream, we will not have to repeat
8019 the effort required to do the parse, nor will we issue duplicate
8020 error messages about problems during instantiation of the
8021 template. */
8022 if (start_of_id >= 0)
8024 cp_token *token;
8026 /* Find the token that corresponds to the start of the
8027 template-id. */
8028 token = cp_lexer_advance_token (parser->lexer,
8029 parser->lexer->first_token,
8030 start_of_id);
8032 /* Reset the contents of the START_OF_ID token. */
8033 token->type = CPP_TEMPLATE_ID;
8034 token->value = build_tree_list (access_check, template_id);
8035 token->keyword = RID_MAX;
8036 /* Purge all subsequent tokens. */
8037 cp_lexer_purge_tokens_after (parser->lexer, token);
8039 /* ??? Can we actually assume that, if template_id ==
8040 error_mark_node, we will have issued a diagnostic to the
8041 user, as opposed to simply marking the tentative parse as
8042 failed? */
8043 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8044 error ("parse error in template argument list");
8047 pop_deferring_access_checks ();
8048 return template_id;
8051 /* Parse a template-name.
8053 template-name:
8054 identifier
8056 The standard should actually say:
8058 template-name:
8059 identifier
8060 operator-function-id
8062 A defect report has been filed about this issue.
8064 A conversion-function-id cannot be a template name because they cannot
8065 be part of a template-id. In fact, looking at this code:
8067 a.operator K<int>()
8069 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8070 It is impossible to call a templated conversion-function-id with an
8071 explicit argument list, since the only allowed template parameter is
8072 the type to which it is converting.
8074 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8075 `template' keyword, in a construction like:
8077 T::template f<3>()
8079 In that case `f' is taken to be a template-name, even though there
8080 is no way of knowing for sure.
8082 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8083 name refers to a set of overloaded functions, at least one of which
8084 is a template, or an IDENTIFIER_NODE with the name of the template,
8085 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8086 names are looked up inside uninstantiated templates. */
8088 static tree
8089 cp_parser_template_name (cp_parser* parser,
8090 bool template_keyword_p,
8091 bool check_dependency_p,
8092 bool is_declaration,
8093 bool *is_identifier)
8095 tree identifier;
8096 tree decl;
8097 tree fns;
8099 /* If the next token is `operator', then we have either an
8100 operator-function-id or a conversion-function-id. */
8101 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8103 /* We don't know whether we're looking at an
8104 operator-function-id or a conversion-function-id. */
8105 cp_parser_parse_tentatively (parser);
8106 /* Try an operator-function-id. */
8107 identifier = cp_parser_operator_function_id (parser);
8108 /* If that didn't work, try a conversion-function-id. */
8109 if (!cp_parser_parse_definitely (parser))
8111 cp_parser_error (parser, "expected template-name");
8112 return error_mark_node;
8115 /* Look for the identifier. */
8116 else
8117 identifier = cp_parser_identifier (parser);
8119 /* If we didn't find an identifier, we don't have a template-id. */
8120 if (identifier == error_mark_node)
8121 return error_mark_node;
8123 /* If the name immediately followed the `template' keyword, then it
8124 is a template-name. However, if the next token is not `<', then
8125 we do not treat it as a template-name, since it is not being used
8126 as part of a template-id. This enables us to handle constructs
8127 like:
8129 template <typename T> struct S { S(); };
8130 template <typename T> S<T>::S();
8132 correctly. We would treat `S' as a template -- if it were `S<T>'
8133 -- but we do not if there is no `<'. */
8135 if (processing_template_decl
8136 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8138 /* In a declaration, in a dependent context, we pretend that the
8139 "template" keyword was present in order to improve error
8140 recovery. For example, given:
8142 template <typename T> void f(T::X<int>);
8144 we want to treat "X<int>" as a template-id. */
8145 if (is_declaration
8146 && !template_keyword_p
8147 && parser->scope && TYPE_P (parser->scope)
8148 && check_dependency_p
8149 && dependent_type_p (parser->scope)
8150 /* Do not do this for dtors (or ctors), since they never
8151 need the template keyword before their name. */
8152 && !constructor_name_p (identifier, parser->scope))
8154 ptrdiff_t start;
8155 cp_token* token;
8156 /* Explain what went wrong. */
8157 error ("non-template `%D' used as template", identifier);
8158 inform ("use `%T::template %D' to indicate that it is a template",
8159 parser->scope, identifier);
8160 /* If parsing tentatively, find the location of the "<"
8161 token. */
8162 if (cp_parser_parsing_tentatively (parser)
8163 && !cp_parser_committed_to_tentative_parse (parser))
8165 cp_parser_simulate_error (parser);
8166 token = cp_lexer_peek_token (parser->lexer);
8167 token = cp_lexer_prev_token (parser->lexer, token);
8168 start = cp_lexer_token_difference (parser->lexer,
8169 parser->lexer->first_token,
8170 token);
8172 else
8173 start = -1;
8174 /* Parse the template arguments so that we can issue error
8175 messages about them. */
8176 cp_lexer_consume_token (parser->lexer);
8177 cp_parser_enclosed_template_argument_list (parser);
8178 /* Skip tokens until we find a good place from which to
8179 continue parsing. */
8180 cp_parser_skip_to_closing_parenthesis (parser,
8181 /*recovering=*/true,
8182 /*or_comma=*/true,
8183 /*consume_paren=*/false);
8184 /* If parsing tentatively, permanently remove the
8185 template argument list. That will prevent duplicate
8186 error messages from being issued about the missing
8187 "template" keyword. */
8188 if (start >= 0)
8190 token = cp_lexer_advance_token (parser->lexer,
8191 parser->lexer->first_token,
8192 start);
8193 cp_lexer_purge_tokens_after (parser->lexer, token);
8195 if (is_identifier)
8196 *is_identifier = true;
8197 return identifier;
8200 /* If the "template" keyword is present, then there is generally
8201 no point in doing name-lookup, so we just return IDENTIFIER.
8202 But, if the qualifying scope is non-dependent then we can
8203 (and must) do name-lookup normally. */
8204 if (template_keyword_p
8205 && (!parser->scope
8206 || (TYPE_P (parser->scope)
8207 && dependent_type_p (parser->scope))))
8208 return identifier;
8211 /* Look up the name. */
8212 decl = cp_parser_lookup_name (parser, identifier,
8213 /*is_type=*/false,
8214 /*is_template=*/false,
8215 /*is_namespace=*/false,
8216 check_dependency_p);
8217 decl = maybe_get_template_decl_from_type_decl (decl);
8219 /* If DECL is a template, then the name was a template-name. */
8220 if (TREE_CODE (decl) == TEMPLATE_DECL)
8222 else
8224 tree fn = NULL_TREE;
8226 /* The standard does not explicitly indicate whether a name that
8227 names a set of overloaded declarations, some of which are
8228 templates, is a template-name. However, such a name should
8229 be a template-name; otherwise, there is no way to form a
8230 template-id for the overloaded templates. */
8231 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8232 if (TREE_CODE (fns) == OVERLOAD)
8233 for (fn = fns; fn; fn = OVL_NEXT (fn))
8234 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8235 break;
8237 if (!fn)
8239 /* Otherwise, the name does not name a template. */
8240 cp_parser_error (parser, "expected template-name");
8241 return error_mark_node;
8245 /* If DECL is dependent, and refers to a function, then just return
8246 its name; we will look it up again during template instantiation. */
8247 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8249 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8250 if (TYPE_P (scope) && dependent_type_p (scope))
8251 return identifier;
8254 return decl;
8257 /* Parse a template-argument-list.
8259 template-argument-list:
8260 template-argument
8261 template-argument-list , template-argument
8263 Returns a TREE_VEC containing the arguments. */
8265 static tree
8266 cp_parser_template_argument_list (cp_parser* parser)
8268 tree fixed_args[10];
8269 unsigned n_args = 0;
8270 unsigned alloced = 10;
8271 tree *arg_ary = fixed_args;
8272 tree vec;
8273 bool saved_in_template_argument_list_p;
8275 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8276 parser->in_template_argument_list_p = true;
8279 tree argument;
8281 if (n_args)
8282 /* Consume the comma. */
8283 cp_lexer_consume_token (parser->lexer);
8285 /* Parse the template-argument. */
8286 argument = cp_parser_template_argument (parser);
8287 if (n_args == alloced)
8289 alloced *= 2;
8291 if (arg_ary == fixed_args)
8293 arg_ary = xmalloc (sizeof (tree) * alloced);
8294 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8296 else
8297 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8299 arg_ary[n_args++] = argument;
8301 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8303 vec = make_tree_vec (n_args);
8305 while (n_args--)
8306 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8308 if (arg_ary != fixed_args)
8309 free (arg_ary);
8310 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8311 return vec;
8314 /* Parse a template-argument.
8316 template-argument:
8317 assignment-expression
8318 type-id
8319 id-expression
8321 The representation is that of an assignment-expression, type-id, or
8322 id-expression -- except that the qualified id-expression is
8323 evaluated, so that the value returned is either a DECL or an
8324 OVERLOAD.
8326 Although the standard says "assignment-expression", it forbids
8327 throw-expressions or assignments in the template argument.
8328 Therefore, we use "conditional-expression" instead. */
8330 static tree
8331 cp_parser_template_argument (cp_parser* parser)
8333 tree argument;
8334 bool template_p;
8335 bool address_p;
8336 bool maybe_type_id = false;
8337 cp_token *token;
8338 cp_id_kind idk;
8339 tree qualifying_class;
8341 /* There's really no way to know what we're looking at, so we just
8342 try each alternative in order.
8344 [temp.arg]
8346 In a template-argument, an ambiguity between a type-id and an
8347 expression is resolved to a type-id, regardless of the form of
8348 the corresponding template-parameter.
8350 Therefore, we try a type-id first. */
8351 cp_parser_parse_tentatively (parser);
8352 argument = cp_parser_type_id (parser);
8353 /* If there was no error parsing the type-id but the next token is a '>>',
8354 we probably found a typo for '> >'. But there are type-id which are
8355 also valid expressions. For instance:
8357 struct X { int operator >> (int); };
8358 template <int V> struct Foo {};
8359 Foo<X () >> 5> r;
8361 Here 'X()' is a valid type-id of a function type, but the user just
8362 wanted to write the expression "X() >> 5". Thus, we remember that we
8363 found a valid type-id, but we still try to parse the argument as an
8364 expression to see what happens. */
8365 if (!cp_parser_error_occurred (parser)
8366 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8368 maybe_type_id = true;
8369 cp_parser_abort_tentative_parse (parser);
8371 else
8373 /* If the next token isn't a `,' or a `>', then this argument wasn't
8374 really finished. This means that the argument is not a valid
8375 type-id. */
8376 if (!cp_parser_next_token_ends_template_argument_p (parser))
8377 cp_parser_error (parser, "expected template-argument");
8378 /* If that worked, we're done. */
8379 if (cp_parser_parse_definitely (parser))
8380 return argument;
8382 /* We're still not sure what the argument will be. */
8383 cp_parser_parse_tentatively (parser);
8384 /* Try a template. */
8385 argument = cp_parser_id_expression (parser,
8386 /*template_keyword_p=*/false,
8387 /*check_dependency_p=*/true,
8388 &template_p,
8389 /*declarator_p=*/false);
8390 /* If the next token isn't a `,' or a `>', then this argument wasn't
8391 really finished. */
8392 if (!cp_parser_next_token_ends_template_argument_p (parser))
8393 cp_parser_error (parser, "expected template-argument");
8394 if (!cp_parser_error_occurred (parser))
8396 /* Figure out what is being referred to. If the id-expression
8397 was for a class template specialization, then we will have a
8398 TYPE_DECL at this point. There is no need to do name lookup
8399 at this point in that case. */
8400 if (TREE_CODE (argument) != TYPE_DECL)
8401 argument = cp_parser_lookup_name (parser, argument,
8402 /*is_type=*/false,
8403 /*is_template=*/template_p,
8404 /*is_namespace=*/false,
8405 /*check_dependency=*/true);
8406 if (TREE_CODE (argument) != TEMPLATE_DECL
8407 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8408 cp_parser_error (parser, "expected template-name");
8410 if (cp_parser_parse_definitely (parser))
8411 return argument;
8412 /* It must be a non-type argument. There permitted cases are given
8413 in [temp.arg.nontype]:
8415 -- an integral constant-expression of integral or enumeration
8416 type; or
8418 -- the name of a non-type template-parameter; or
8420 -- the name of an object or function with external linkage...
8422 -- the address of an object or function with external linkage...
8424 -- a pointer to member... */
8425 /* Look for a non-type template parameter. */
8426 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8428 cp_parser_parse_tentatively (parser);
8429 argument = cp_parser_primary_expression (parser,
8430 &idk,
8431 &qualifying_class);
8432 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8433 || !cp_parser_next_token_ends_template_argument_p (parser))
8434 cp_parser_simulate_error (parser);
8435 if (cp_parser_parse_definitely (parser))
8436 return argument;
8438 /* If the next token is "&", the argument must be the address of an
8439 object or function with external linkage. */
8440 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8441 if (address_p)
8442 cp_lexer_consume_token (parser->lexer);
8443 /* See if we might have an id-expression. */
8444 token = cp_lexer_peek_token (parser->lexer);
8445 if (token->type == CPP_NAME
8446 || token->keyword == RID_OPERATOR
8447 || token->type == CPP_SCOPE
8448 || token->type == CPP_TEMPLATE_ID
8449 || token->type == CPP_NESTED_NAME_SPECIFIER)
8451 cp_parser_parse_tentatively (parser);
8452 argument = cp_parser_primary_expression (parser,
8453 &idk,
8454 &qualifying_class);
8455 if (cp_parser_error_occurred (parser)
8456 || !cp_parser_next_token_ends_template_argument_p (parser))
8457 cp_parser_abort_tentative_parse (parser);
8458 else
8460 if (qualifying_class)
8461 argument = finish_qualified_id_expr (qualifying_class,
8462 argument,
8463 /*done=*/true,
8464 address_p);
8465 if (TREE_CODE (argument) == VAR_DECL)
8467 /* A variable without external linkage might still be a
8468 valid constant-expression, so no error is issued here
8469 if the external-linkage check fails. */
8470 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8471 cp_parser_simulate_error (parser);
8473 else if (is_overloaded_fn (argument))
8474 /* All overloaded functions are allowed; if the external
8475 linkage test does not pass, an error will be issued
8476 later. */
8478 else if (address_p
8479 && (TREE_CODE (argument) == OFFSET_REF
8480 || TREE_CODE (argument) == SCOPE_REF))
8481 /* A pointer-to-member. */
8483 else
8484 cp_parser_simulate_error (parser);
8486 if (cp_parser_parse_definitely (parser))
8488 if (address_p)
8489 argument = build_x_unary_op (ADDR_EXPR, argument);
8490 return argument;
8494 /* If the argument started with "&", there are no other valid
8495 alternatives at this point. */
8496 if (address_p)
8498 cp_parser_error (parser, "invalid non-type template argument");
8499 return error_mark_node;
8501 /* If the argument wasn't successfully parsed as a type-id followed
8502 by '>>', the argument can only be a constant expression now.
8503 Otherwise, we try parsing the constant-expression tentatively,
8504 because the argument could really be a type-id. */
8505 if (maybe_type_id)
8506 cp_parser_parse_tentatively (parser);
8507 argument = cp_parser_constant_expression (parser,
8508 /*allow_non_constant_p=*/false,
8509 /*non_constant_p=*/NULL);
8510 argument = fold_non_dependent_expr (argument);
8511 if (!maybe_type_id)
8512 return argument;
8513 if (!cp_parser_next_token_ends_template_argument_p (parser))
8514 cp_parser_error (parser, "expected template-argument");
8515 if (cp_parser_parse_definitely (parser))
8516 return argument;
8517 /* We did our best to parse the argument as a non type-id, but that
8518 was the only alternative that matched (albeit with a '>' after
8519 it). We can assume it's just a typo from the user, and a
8520 diagnostic will then be issued. */
8521 return cp_parser_type_id (parser);
8524 /* Parse an explicit-instantiation.
8526 explicit-instantiation:
8527 template declaration
8529 Although the standard says `declaration', what it really means is:
8531 explicit-instantiation:
8532 template decl-specifier-seq [opt] declarator [opt] ;
8534 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8535 supposed to be allowed. A defect report has been filed about this
8536 issue.
8538 GNU Extension:
8540 explicit-instantiation:
8541 storage-class-specifier template
8542 decl-specifier-seq [opt] declarator [opt] ;
8543 function-specifier template
8544 decl-specifier-seq [opt] declarator [opt] ; */
8546 static void
8547 cp_parser_explicit_instantiation (cp_parser* parser)
8549 int declares_class_or_enum;
8550 tree decl_specifiers;
8551 tree attributes;
8552 tree extension_specifier = NULL_TREE;
8554 /* Look for an (optional) storage-class-specifier or
8555 function-specifier. */
8556 if (cp_parser_allow_gnu_extensions_p (parser))
8558 extension_specifier
8559 = cp_parser_storage_class_specifier_opt (parser);
8560 if (!extension_specifier)
8561 extension_specifier = cp_parser_function_specifier_opt (parser);
8564 /* Look for the `template' keyword. */
8565 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8566 /* Let the front end know that we are processing an explicit
8567 instantiation. */
8568 begin_explicit_instantiation ();
8569 /* [temp.explicit] says that we are supposed to ignore access
8570 control while processing explicit instantiation directives. */
8571 push_deferring_access_checks (dk_no_check);
8572 /* Parse a decl-specifier-seq. */
8573 decl_specifiers
8574 = cp_parser_decl_specifier_seq (parser,
8575 CP_PARSER_FLAGS_OPTIONAL,
8576 &attributes,
8577 &declares_class_or_enum);
8578 /* If there was exactly one decl-specifier, and it declared a class,
8579 and there's no declarator, then we have an explicit type
8580 instantiation. */
8581 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8583 tree type;
8585 type = check_tag_decl (decl_specifiers);
8586 /* Turn access control back on for names used during
8587 template instantiation. */
8588 pop_deferring_access_checks ();
8589 if (type)
8590 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8592 else
8594 tree declarator;
8595 tree decl;
8597 /* Parse the declarator. */
8598 declarator
8599 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8600 /*ctor_dtor_or_conv_p=*/NULL,
8601 /*parenthesized_p=*/NULL,
8602 /*member_p=*/false);
8603 if (declares_class_or_enum & 2)
8604 cp_parser_check_for_definition_in_return_type
8605 (declarator, TREE_VALUE (decl_specifiers));
8606 if (declarator != error_mark_node)
8608 decl = grokdeclarator (declarator, decl_specifiers,
8609 NORMAL, 0, NULL);
8610 /* Turn access control back on for names used during
8611 template instantiation. */
8612 pop_deferring_access_checks ();
8613 /* Do the explicit instantiation. */
8614 do_decl_instantiation (decl, extension_specifier);
8616 else
8618 pop_deferring_access_checks ();
8619 /* Skip the body of the explicit instantiation. */
8620 cp_parser_skip_to_end_of_statement (parser);
8623 /* We're done with the instantiation. */
8624 end_explicit_instantiation ();
8626 cp_parser_consume_semicolon_at_end_of_statement (parser);
8629 /* Parse an explicit-specialization.
8631 explicit-specialization:
8632 template < > declaration
8634 Although the standard says `declaration', what it really means is:
8636 explicit-specialization:
8637 template <> decl-specifier [opt] init-declarator [opt] ;
8638 template <> function-definition
8639 template <> explicit-specialization
8640 template <> template-declaration */
8642 static void
8643 cp_parser_explicit_specialization (cp_parser* parser)
8645 /* Look for the `template' keyword. */
8646 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8647 /* Look for the `<'. */
8648 cp_parser_require (parser, CPP_LESS, "`<'");
8649 /* Look for the `>'. */
8650 cp_parser_require (parser, CPP_GREATER, "`>'");
8651 /* We have processed another parameter list. */
8652 ++parser->num_template_parameter_lists;
8653 /* Let the front end know that we are beginning a specialization. */
8654 begin_specialization ();
8656 /* If the next keyword is `template', we need to figure out whether
8657 or not we're looking a template-declaration. */
8658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8660 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8661 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8662 cp_parser_template_declaration_after_export (parser,
8663 /*member_p=*/false);
8664 else
8665 cp_parser_explicit_specialization (parser);
8667 else
8668 /* Parse the dependent declaration. */
8669 cp_parser_single_declaration (parser,
8670 /*member_p=*/false,
8671 /*friend_p=*/NULL);
8673 /* We're done with the specialization. */
8674 end_specialization ();
8675 /* We're done with this parameter list. */
8676 --parser->num_template_parameter_lists;
8679 /* Parse a type-specifier.
8681 type-specifier:
8682 simple-type-specifier
8683 class-specifier
8684 enum-specifier
8685 elaborated-type-specifier
8686 cv-qualifier
8688 GNU Extension:
8690 type-specifier:
8691 __complex__
8693 Returns a representation of the type-specifier. If the
8694 type-specifier is a keyword (like `int' or `const', or
8695 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8696 For a class-specifier, enum-specifier, or elaborated-type-specifier
8697 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8699 If IS_FRIEND is TRUE then this type-specifier is being declared a
8700 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8701 appearing in a decl-specifier-seq.
8703 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8704 class-specifier, enum-specifier, or elaborated-type-specifier, then
8705 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8706 if a type is declared; 2 if it is defined. Otherwise, it is set to
8707 zero.
8709 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8710 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8711 is set to FALSE. */
8713 static tree
8714 cp_parser_type_specifier (cp_parser* parser,
8715 cp_parser_flags flags,
8716 bool is_friend,
8717 bool is_declaration,
8718 int* declares_class_or_enum,
8719 bool* is_cv_qualifier)
8721 tree type_spec = NULL_TREE;
8722 cp_token *token;
8723 enum rid keyword;
8725 /* Assume this type-specifier does not declare a new type. */
8726 if (declares_class_or_enum)
8727 *declares_class_or_enum = 0;
8728 /* And that it does not specify a cv-qualifier. */
8729 if (is_cv_qualifier)
8730 *is_cv_qualifier = false;
8731 /* Peek at the next token. */
8732 token = cp_lexer_peek_token (parser->lexer);
8734 /* If we're looking at a keyword, we can use that to guide the
8735 production we choose. */
8736 keyword = token->keyword;
8737 switch (keyword)
8739 case RID_ENUM:
8740 /* 'enum' [identifier] '{' introduces an enum-specifier;
8741 'enum' <anything else> introduces an elaborated-type-specifier. */
8742 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
8743 || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
8744 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
8745 == CPP_OPEN_BRACE))
8747 if (parser->num_template_parameter_lists)
8749 error ("template declaration of `enum'");
8750 cp_parser_skip_to_end_of_block_or_statement (parser);
8751 type_spec = error_mark_node;
8753 else
8754 type_spec = cp_parser_enum_specifier (parser);
8756 if (declares_class_or_enum)
8757 *declares_class_or_enum = 2;
8758 return type_spec;
8760 else
8761 goto elaborated_type_specifier;
8763 /* Any of these indicate either a class-specifier, or an
8764 elaborated-type-specifier. */
8765 case RID_CLASS:
8766 case RID_STRUCT:
8767 case RID_UNION:
8768 /* Parse tentatively so that we can back up if we don't find a
8769 class-specifier or enum-specifier. */
8770 cp_parser_parse_tentatively (parser);
8771 /* Look for the class-specifier. */
8772 type_spec = cp_parser_class_specifier (parser);
8773 /* If that worked, we're done. */
8774 if (cp_parser_parse_definitely (parser))
8776 if (declares_class_or_enum)
8777 *declares_class_or_enum = 2;
8778 return type_spec;
8781 /* Fall through. */
8783 case RID_TYPENAME:
8784 elaborated_type_specifier:
8785 /* Look for an elaborated-type-specifier. */
8786 type_spec = cp_parser_elaborated_type_specifier (parser,
8787 is_friend,
8788 is_declaration);
8789 /* We're declaring a class or enum -- unless we're using
8790 `typename'. */
8791 if (declares_class_or_enum && keyword != RID_TYPENAME)
8792 *declares_class_or_enum = 1;
8793 return type_spec;
8795 case RID_CONST:
8796 case RID_VOLATILE:
8797 case RID_RESTRICT:
8798 type_spec = cp_parser_cv_qualifier_opt (parser);
8799 /* Even though we call a routine that looks for an optional
8800 qualifier, we know that there should be one. */
8801 my_friendly_assert (type_spec != NULL, 20000328);
8802 /* This type-specifier was a cv-qualified. */
8803 if (is_cv_qualifier)
8804 *is_cv_qualifier = true;
8806 return type_spec;
8808 case RID_COMPLEX:
8809 /* The `__complex__' keyword is a GNU extension. */
8810 return cp_lexer_consume_token (parser->lexer)->value;
8812 default:
8813 break;
8816 /* If we do not already have a type-specifier, assume we are looking
8817 at a simple-type-specifier. */
8818 type_spec = cp_parser_simple_type_specifier (parser, flags,
8819 /*identifier_p=*/true);
8821 /* If we didn't find a type-specifier, and a type-specifier was not
8822 optional in this context, issue an error message. */
8823 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8825 cp_parser_error (parser, "expected type specifier");
8826 return error_mark_node;
8829 return type_spec;
8832 /* Parse a simple-type-specifier.
8834 simple-type-specifier:
8835 :: [opt] nested-name-specifier [opt] type-name
8836 :: [opt] nested-name-specifier template template-id
8837 char
8838 wchar_t
8839 bool
8840 short
8842 long
8843 signed
8844 unsigned
8845 float
8846 double
8847 void
8849 GNU Extension:
8851 simple-type-specifier:
8852 __typeof__ unary-expression
8853 __typeof__ ( type-id )
8855 For the various keywords, the value returned is simply the
8856 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8857 For the first two productions, and if IDENTIFIER_P is false, the
8858 value returned is the indicated TYPE_DECL. */
8860 static tree
8861 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8862 bool identifier_p)
8864 tree type = NULL_TREE;
8865 cp_token *token;
8867 /* Peek at the next token. */
8868 token = cp_lexer_peek_token (parser->lexer);
8870 /* If we're looking at a keyword, things are easy. */
8871 switch (token->keyword)
8873 case RID_CHAR:
8874 type = char_type_node;
8875 break;
8876 case RID_WCHAR:
8877 type = wchar_type_node;
8878 break;
8879 case RID_BOOL:
8880 type = boolean_type_node;
8881 break;
8882 case RID_SHORT:
8883 type = short_integer_type_node;
8884 break;
8885 case RID_INT:
8886 type = integer_type_node;
8887 break;
8888 case RID_LONG:
8889 type = long_integer_type_node;
8890 break;
8891 case RID_SIGNED:
8892 type = integer_type_node;
8893 break;
8894 case RID_UNSIGNED:
8895 type = unsigned_type_node;
8896 break;
8897 case RID_FLOAT:
8898 type = float_type_node;
8899 break;
8900 case RID_DOUBLE:
8901 type = double_type_node;
8902 break;
8903 case RID_VOID:
8904 type = void_type_node;
8905 break;
8907 case RID_TYPEOF:
8909 tree operand;
8911 /* Consume the `typeof' token. */
8912 cp_lexer_consume_token (parser->lexer);
8913 /* Parse the operand to `typeof'. */
8914 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8915 /* If it is not already a TYPE, take its type. */
8916 if (!TYPE_P (operand))
8917 operand = finish_typeof (operand);
8919 return operand;
8922 default:
8923 break;
8926 /* If the type-specifier was for a built-in type, we're done. */
8927 if (type)
8929 tree id;
8931 /* Consume the token. */
8932 id = cp_lexer_consume_token (parser->lexer)->value;
8934 /* There is no valid C++ program where a non-template type is
8935 followed by a "<". That usually indicates that the user thought
8936 that the type was a template. */
8937 cp_parser_check_for_invalid_template_id (parser, type);
8939 return identifier_p ? id : TYPE_NAME (type);
8942 /* The type-specifier must be a user-defined type. */
8943 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8945 bool qualified_p;
8946 bool global_p;
8948 /* Don't gobble tokens or issue error messages if this is an
8949 optional type-specifier. */
8950 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8951 cp_parser_parse_tentatively (parser);
8953 /* Look for the optional `::' operator. */
8954 global_p
8955 = (cp_parser_global_scope_opt (parser,
8956 /*current_scope_valid_p=*/false)
8957 != NULL_TREE);
8958 /* Look for the nested-name specifier. */
8959 qualified_p
8960 = (cp_parser_nested_name_specifier_opt (parser,
8961 /*typename_keyword_p=*/false,
8962 /*check_dependency_p=*/true,
8963 /*type_p=*/false,
8964 /*is_declaration=*/false)
8965 != NULL_TREE);
8966 /* If we have seen a nested-name-specifier, and the next token
8967 is `template', then we are using the template-id production. */
8968 if (parser->scope
8969 && cp_parser_optional_template_keyword (parser))
8971 /* Look for the template-id. */
8972 type = cp_parser_template_id (parser,
8973 /*template_keyword_p=*/true,
8974 /*check_dependency_p=*/true,
8975 /*is_declaration=*/false);
8976 /* If the template-id did not name a type, we are out of
8977 luck. */
8978 if (TREE_CODE (type) != TYPE_DECL)
8980 cp_parser_error (parser, "expected template-id for type");
8981 type = NULL_TREE;
8984 /* Otherwise, look for a type-name. */
8985 else
8986 type = cp_parser_type_name (parser);
8987 /* Keep track of all name-lookups performed in class scopes. */
8988 if (type
8989 && !global_p
8990 && !qualified_p
8991 && TREE_CODE (type) == TYPE_DECL
8992 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
8993 maybe_note_name_used_in_class (DECL_NAME (type), type);
8994 /* If it didn't work out, we don't have a TYPE. */
8995 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8996 && !cp_parser_parse_definitely (parser))
8997 type = NULL_TREE;
9000 /* If we didn't get a type-name, issue an error message. */
9001 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9003 cp_parser_error (parser, "expected type-name");
9004 return error_mark_node;
9007 /* There is no valid C++ program where a non-template type is
9008 followed by a "<". That usually indicates that the user thought
9009 that the type was a template. */
9010 if (type && type != error_mark_node)
9011 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9013 return type;
9016 /* Parse a type-name.
9018 type-name:
9019 class-name
9020 enum-name
9021 typedef-name
9023 enum-name:
9024 identifier
9026 typedef-name:
9027 identifier
9029 Returns a TYPE_DECL for the the type. */
9031 static tree
9032 cp_parser_type_name (cp_parser* parser)
9034 tree type_decl;
9035 tree identifier;
9037 /* We can't know yet whether it is a class-name or not. */
9038 cp_parser_parse_tentatively (parser);
9039 /* Try a class-name. */
9040 type_decl = cp_parser_class_name (parser,
9041 /*typename_keyword_p=*/false,
9042 /*template_keyword_p=*/false,
9043 /*type_p=*/false,
9044 /*check_dependency_p=*/true,
9045 /*class_head_p=*/false,
9046 /*is_declaration=*/false);
9047 /* If it's not a class-name, keep looking. */
9048 if (!cp_parser_parse_definitely (parser))
9050 /* It must be a typedef-name or an enum-name. */
9051 identifier = cp_parser_identifier (parser);
9052 if (identifier == error_mark_node)
9053 return error_mark_node;
9055 /* Look up the type-name. */
9056 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9057 /* Issue an error if we did not find a type-name. */
9058 if (TREE_CODE (type_decl) != TYPE_DECL)
9060 if (!cp_parser_simulate_error (parser))
9061 cp_parser_name_lookup_error (parser, identifier, type_decl,
9062 "is not a type");
9063 type_decl = error_mark_node;
9065 /* Remember that the name was used in the definition of the
9066 current class so that we can check later to see if the
9067 meaning would have been different after the class was
9068 entirely defined. */
9069 else if (type_decl != error_mark_node
9070 && !parser->scope)
9071 maybe_note_name_used_in_class (identifier, type_decl);
9074 return type_decl;
9078 /* Parse an elaborated-type-specifier. Note that the grammar given
9079 here incorporates the resolution to DR68.
9081 elaborated-type-specifier:
9082 class-key :: [opt] nested-name-specifier [opt] identifier
9083 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9084 enum :: [opt] nested-name-specifier [opt] identifier
9085 typename :: [opt] nested-name-specifier identifier
9086 typename :: [opt] nested-name-specifier template [opt]
9087 template-id
9089 GNU extension:
9091 elaborated-type-specifier:
9092 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9093 class-key attributes :: [opt] nested-name-specifier [opt]
9094 template [opt] template-id
9095 enum attributes :: [opt] nested-name-specifier [opt] identifier
9097 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9098 declared `friend'. If IS_DECLARATION is TRUE, then this
9099 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9100 something is being declared.
9102 Returns the TYPE specified. */
9104 static tree
9105 cp_parser_elaborated_type_specifier (cp_parser* parser,
9106 bool is_friend,
9107 bool is_declaration)
9109 enum tag_types tag_type;
9110 tree identifier;
9111 tree type = NULL_TREE;
9112 tree attributes = NULL_TREE;
9114 /* See if we're looking at the `enum' keyword. */
9115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9117 /* Consume the `enum' token. */
9118 cp_lexer_consume_token (parser->lexer);
9119 /* Remember that it's an enumeration type. */
9120 tag_type = enum_type;
9121 /* Parse the attributes. */
9122 attributes = cp_parser_attributes_opt (parser);
9124 /* Or, it might be `typename'. */
9125 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9126 RID_TYPENAME))
9128 /* Consume the `typename' token. */
9129 cp_lexer_consume_token (parser->lexer);
9130 /* Remember that it's a `typename' type. */
9131 tag_type = typename_type;
9132 /* The `typename' keyword is only allowed in templates. */
9133 if (!processing_template_decl)
9134 pedwarn ("using `typename' outside of template");
9136 /* Otherwise it must be a class-key. */
9137 else
9139 tag_type = cp_parser_class_key (parser);
9140 if (tag_type == none_type)
9141 return error_mark_node;
9142 /* Parse the attributes. */
9143 attributes = cp_parser_attributes_opt (parser);
9146 /* Look for the `::' operator. */
9147 cp_parser_global_scope_opt (parser,
9148 /*current_scope_valid_p=*/false);
9149 /* Look for the nested-name-specifier. */
9150 if (tag_type == typename_type)
9152 if (cp_parser_nested_name_specifier (parser,
9153 /*typename_keyword_p=*/true,
9154 /*check_dependency_p=*/true,
9155 /*type_p=*/true,
9156 is_declaration)
9157 == error_mark_node)
9158 return error_mark_node;
9160 else
9161 /* Even though `typename' is not present, the proposed resolution
9162 to Core Issue 180 says that in `class A<T>::B', `B' should be
9163 considered a type-name, even if `A<T>' is dependent. */
9164 cp_parser_nested_name_specifier_opt (parser,
9165 /*typename_keyword_p=*/true,
9166 /*check_dependency_p=*/true,
9167 /*type_p=*/true,
9168 is_declaration);
9169 /* For everything but enumeration types, consider a template-id. */
9170 if (tag_type != enum_type)
9172 bool template_p = false;
9173 tree decl;
9175 /* Allow the `template' keyword. */
9176 template_p = cp_parser_optional_template_keyword (parser);
9177 /* If we didn't see `template', we don't know if there's a
9178 template-id or not. */
9179 if (!template_p)
9180 cp_parser_parse_tentatively (parser);
9181 /* Parse the template-id. */
9182 decl = cp_parser_template_id (parser, template_p,
9183 /*check_dependency_p=*/true,
9184 is_declaration);
9185 /* If we didn't find a template-id, look for an ordinary
9186 identifier. */
9187 if (!template_p && !cp_parser_parse_definitely (parser))
9189 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9190 in effect, then we must assume that, upon instantiation, the
9191 template will correspond to a class. */
9192 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9193 && tag_type == typename_type)
9194 type = make_typename_type (parser->scope, decl,
9195 /*complain=*/1);
9196 else
9197 type = TREE_TYPE (decl);
9200 /* For an enumeration type, consider only a plain identifier. */
9201 if (!type)
9203 identifier = cp_parser_identifier (parser);
9205 if (identifier == error_mark_node)
9207 parser->scope = NULL_TREE;
9208 return error_mark_node;
9211 /* For a `typename', we needn't call xref_tag. */
9212 if (tag_type == typename_type
9213 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9214 return make_typename_type (parser->scope, identifier,
9215 /*complain=*/1);
9216 /* Look up a qualified name in the usual way. */
9217 if (parser->scope)
9219 tree decl;
9221 /* In an elaborated-type-specifier, names are assumed to name
9222 types, so we set IS_TYPE to TRUE when calling
9223 cp_parser_lookup_name. */
9224 decl = cp_parser_lookup_name (parser, identifier,
9225 /*is_type=*/true,
9226 /*is_template=*/false,
9227 /*is_namespace=*/false,
9228 /*check_dependency=*/true);
9230 /* If we are parsing friend declaration, DECL may be a
9231 TEMPLATE_DECL tree node here. However, we need to check
9232 whether this TEMPLATE_DECL results in valid code. Consider
9233 the following example:
9235 namespace N {
9236 template <class T> class C {};
9238 class X {
9239 template <class T> friend class N::C; // #1, valid code
9241 template <class T> class Y {
9242 friend class N::C; // #2, invalid code
9245 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9246 name lookup of `N::C'. We see that friend declaration must
9247 be template for the code to be valid. Note that
9248 processing_template_decl does not work here since it is
9249 always 1 for the above two cases. */
9251 decl = (cp_parser_maybe_treat_template_as_class
9252 (decl, /*tag_name_p=*/is_friend
9253 && parser->num_template_parameter_lists));
9255 if (TREE_CODE (decl) != TYPE_DECL)
9257 cp_parser_diagnose_invalid_type_name (parser);
9258 return error_mark_node;
9261 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9262 check_elaborated_type_specifier
9263 (tag_type, decl,
9264 (parser->num_template_parameter_lists
9265 || DECL_SELF_REFERENCE_P (decl)));
9267 type = TREE_TYPE (decl);
9269 else
9271 /* An elaborated-type-specifier sometimes introduces a new type and
9272 sometimes names an existing type. Normally, the rule is that it
9273 introduces a new type only if there is not an existing type of
9274 the same name already in scope. For example, given:
9276 struct S {};
9277 void f() { struct S s; }
9279 the `struct S' in the body of `f' is the same `struct S' as in
9280 the global scope; the existing definition is used. However, if
9281 there were no global declaration, this would introduce a new
9282 local class named `S'.
9284 An exception to this rule applies to the following code:
9286 namespace N { struct S; }
9288 Here, the elaborated-type-specifier names a new type
9289 unconditionally; even if there is already an `S' in the
9290 containing scope this declaration names a new type.
9291 This exception only applies if the elaborated-type-specifier
9292 forms the complete declaration:
9294 [class.name]
9296 A declaration consisting solely of `class-key identifier ;' is
9297 either a redeclaration of the name in the current scope or a
9298 forward declaration of the identifier as a class name. It
9299 introduces the name into the current scope.
9301 We are in this situation precisely when the next token is a `;'.
9303 An exception to the exception is that a `friend' declaration does
9304 *not* name a new type; i.e., given:
9306 struct S { friend struct T; };
9308 `T' is not a new type in the scope of `S'.
9310 Also, `new struct S' or `sizeof (struct S)' never results in the
9311 definition of a new type; a new type can only be declared in a
9312 declaration context. */
9314 /* Warn about attributes. They are ignored. */
9315 if (attributes)
9316 warning ("type attributes are honored only at type definition");
9318 type = xref_tag (tag_type, identifier,
9319 (is_friend
9320 || !is_declaration
9321 || cp_lexer_next_token_is_not (parser->lexer,
9322 CPP_SEMICOLON)),
9323 parser->num_template_parameter_lists);
9326 if (tag_type != enum_type)
9327 cp_parser_check_class_key (tag_type, type);
9329 /* A "<" cannot follow an elaborated type specifier. If that
9330 happens, the user was probably trying to form a template-id. */
9331 cp_parser_check_for_invalid_template_id (parser, type);
9333 return type;
9336 /* Parse an enum-specifier.
9338 enum-specifier:
9339 enum identifier [opt] { enumerator-list [opt] }
9341 Returns an ENUM_TYPE representing the enumeration. */
9343 static tree
9344 cp_parser_enum_specifier (cp_parser* parser)
9346 cp_token *token;
9347 tree identifier = NULL_TREE;
9348 tree type;
9350 /* Look for the `enum' keyword. */
9351 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9352 return error_mark_node;
9353 /* Peek at the next token. */
9354 token = cp_lexer_peek_token (parser->lexer);
9356 /* See if it is an identifier. */
9357 if (token->type == CPP_NAME)
9358 identifier = cp_parser_identifier (parser);
9360 /* Look for the `{'. */
9361 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9362 return error_mark_node;
9364 /* At this point, we're going ahead with the enum-specifier, even
9365 if some other problem occurs. */
9366 cp_parser_commit_to_tentative_parse (parser);
9368 /* Issue an error message if type-definitions are forbidden here. */
9369 cp_parser_check_type_definition (parser);
9371 /* Create the new type. */
9372 type = start_enum (identifier ? identifier : make_anon_name ());
9374 /* Peek at the next token. */
9375 token = cp_lexer_peek_token (parser->lexer);
9376 /* If it's not a `}', then there are some enumerators. */
9377 if (token->type != CPP_CLOSE_BRACE)
9378 cp_parser_enumerator_list (parser, type);
9379 /* Look for the `}'. */
9380 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9382 /* Finish up the enumeration. */
9383 finish_enum (type);
9385 return type;
9388 /* Parse an enumerator-list. The enumerators all have the indicated
9389 TYPE.
9391 enumerator-list:
9392 enumerator-definition
9393 enumerator-list , enumerator-definition */
9395 static void
9396 cp_parser_enumerator_list (cp_parser* parser, tree type)
9398 while (true)
9400 cp_token *token;
9402 /* Parse an enumerator-definition. */
9403 cp_parser_enumerator_definition (parser, type);
9404 /* Peek at the next token. */
9405 token = cp_lexer_peek_token (parser->lexer);
9406 /* If it's not a `,', then we've reached the end of the
9407 list. */
9408 if (token->type != CPP_COMMA)
9409 break;
9410 /* Otherwise, consume the `,' and keep going. */
9411 cp_lexer_consume_token (parser->lexer);
9412 /* If the next token is a `}', there is a trailing comma. */
9413 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9415 if (pedantic && !in_system_header)
9416 pedwarn ("comma at end of enumerator list");
9417 break;
9422 /* Parse an enumerator-definition. The enumerator has the indicated
9423 TYPE.
9425 enumerator-definition:
9426 enumerator
9427 enumerator = constant-expression
9429 enumerator:
9430 identifier */
9432 static void
9433 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9435 cp_token *token;
9436 tree identifier;
9437 tree value;
9439 /* Look for the identifier. */
9440 identifier = cp_parser_identifier (parser);
9441 if (identifier == error_mark_node)
9442 return;
9444 /* Peek at the next token. */
9445 token = cp_lexer_peek_token (parser->lexer);
9446 /* If it's an `=', then there's an explicit value. */
9447 if (token->type == CPP_EQ)
9449 /* Consume the `=' token. */
9450 cp_lexer_consume_token (parser->lexer);
9451 /* Parse the value. */
9452 value = cp_parser_constant_expression (parser,
9453 /*allow_non_constant_p=*/false,
9454 NULL);
9456 else
9457 value = NULL_TREE;
9459 /* Create the enumerator. */
9460 build_enumerator (identifier, value, type);
9463 /* Parse a namespace-name.
9465 namespace-name:
9466 original-namespace-name
9467 namespace-alias
9469 Returns the NAMESPACE_DECL for the namespace. */
9471 static tree
9472 cp_parser_namespace_name (cp_parser* parser)
9474 tree identifier;
9475 tree namespace_decl;
9477 /* Get the name of the namespace. */
9478 identifier = cp_parser_identifier (parser);
9479 if (identifier == error_mark_node)
9480 return error_mark_node;
9482 /* Look up the identifier in the currently active scope. Look only
9483 for namespaces, due to:
9485 [basic.lookup.udir]
9487 When looking up a namespace-name in a using-directive or alias
9488 definition, only namespace names are considered.
9490 And:
9492 [basic.lookup.qual]
9494 During the lookup of a name preceding the :: scope resolution
9495 operator, object, function, and enumerator names are ignored.
9497 (Note that cp_parser_class_or_namespace_name only calls this
9498 function if the token after the name is the scope resolution
9499 operator.) */
9500 namespace_decl = cp_parser_lookup_name (parser, identifier,
9501 /*is_type=*/false,
9502 /*is_template=*/false,
9503 /*is_namespace=*/true,
9504 /*check_dependency=*/true);
9505 /* If it's not a namespace, issue an error. */
9506 if (namespace_decl == error_mark_node
9507 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9509 if (!cp_parser_parsing_tentatively (parser)
9510 || cp_parser_committed_to_tentative_parse (parser))
9511 error ("`%D' is not a namespace-name", identifier);
9512 cp_parser_error (parser, "expected namespace-name");
9513 namespace_decl = error_mark_node;
9516 return namespace_decl;
9519 /* Parse a namespace-definition.
9521 namespace-definition:
9522 named-namespace-definition
9523 unnamed-namespace-definition
9525 named-namespace-definition:
9526 original-namespace-definition
9527 extension-namespace-definition
9529 original-namespace-definition:
9530 namespace identifier { namespace-body }
9532 extension-namespace-definition:
9533 namespace original-namespace-name { namespace-body }
9535 unnamed-namespace-definition:
9536 namespace { namespace-body } */
9538 static void
9539 cp_parser_namespace_definition (cp_parser* parser)
9541 tree identifier;
9543 /* Look for the `namespace' keyword. */
9544 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9546 /* Get the name of the namespace. We do not attempt to distinguish
9547 between an original-namespace-definition and an
9548 extension-namespace-definition at this point. The semantic
9549 analysis routines are responsible for that. */
9550 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9551 identifier = cp_parser_identifier (parser);
9552 else
9553 identifier = NULL_TREE;
9555 /* Look for the `{' to start the namespace. */
9556 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9557 /* Start the namespace. */
9558 push_namespace (identifier);
9559 /* Parse the body of the namespace. */
9560 cp_parser_namespace_body (parser);
9561 /* Finish the namespace. */
9562 pop_namespace ();
9563 /* Look for the final `}'. */
9564 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9567 /* Parse a namespace-body.
9569 namespace-body:
9570 declaration-seq [opt] */
9572 static void
9573 cp_parser_namespace_body (cp_parser* parser)
9575 cp_parser_declaration_seq_opt (parser);
9578 /* Parse a namespace-alias-definition.
9580 namespace-alias-definition:
9581 namespace identifier = qualified-namespace-specifier ; */
9583 static void
9584 cp_parser_namespace_alias_definition (cp_parser* parser)
9586 tree identifier;
9587 tree namespace_specifier;
9589 /* Look for the `namespace' keyword. */
9590 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9591 /* Look for the identifier. */
9592 identifier = cp_parser_identifier (parser);
9593 if (identifier == error_mark_node)
9594 return;
9595 /* Look for the `=' token. */
9596 cp_parser_require (parser, CPP_EQ, "`='");
9597 /* Look for the qualified-namespace-specifier. */
9598 namespace_specifier
9599 = cp_parser_qualified_namespace_specifier (parser);
9600 /* Look for the `;' token. */
9601 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9603 /* Register the alias in the symbol table. */
9604 do_namespace_alias (identifier, namespace_specifier);
9607 /* Parse a qualified-namespace-specifier.
9609 qualified-namespace-specifier:
9610 :: [opt] nested-name-specifier [opt] namespace-name
9612 Returns a NAMESPACE_DECL corresponding to the specified
9613 namespace. */
9615 static tree
9616 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9618 /* Look for the optional `::'. */
9619 cp_parser_global_scope_opt (parser,
9620 /*current_scope_valid_p=*/false);
9622 /* Look for the optional nested-name-specifier. */
9623 cp_parser_nested_name_specifier_opt (parser,
9624 /*typename_keyword_p=*/false,
9625 /*check_dependency_p=*/true,
9626 /*type_p=*/false,
9627 /*is_declaration=*/true);
9629 return cp_parser_namespace_name (parser);
9632 /* Parse a using-declaration.
9634 using-declaration:
9635 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9636 using :: unqualified-id ; */
9638 static void
9639 cp_parser_using_declaration (cp_parser* parser)
9641 cp_token *token;
9642 bool typename_p = false;
9643 bool global_scope_p;
9644 tree decl;
9645 tree identifier;
9646 tree scope;
9647 tree qscope;
9649 /* Look for the `using' keyword. */
9650 cp_parser_require_keyword (parser, RID_USING, "`using'");
9652 /* Peek at the next token. */
9653 token = cp_lexer_peek_token (parser->lexer);
9654 /* See if it's `typename'. */
9655 if (token->keyword == RID_TYPENAME)
9657 /* Remember that we've seen it. */
9658 typename_p = true;
9659 /* Consume the `typename' token. */
9660 cp_lexer_consume_token (parser->lexer);
9663 /* Look for the optional global scope qualification. */
9664 global_scope_p
9665 = (cp_parser_global_scope_opt (parser,
9666 /*current_scope_valid_p=*/false)
9667 != NULL_TREE);
9669 /* If we saw `typename', or didn't see `::', then there must be a
9670 nested-name-specifier present. */
9671 if (typename_p || !global_scope_p)
9672 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9673 /*check_dependency_p=*/true,
9674 /*type_p=*/false,
9675 /*is_declaration=*/true);
9676 /* Otherwise, we could be in either of the two productions. In that
9677 case, treat the nested-name-specifier as optional. */
9678 else
9679 qscope = cp_parser_nested_name_specifier_opt (parser,
9680 /*typename_keyword_p=*/false,
9681 /*check_dependency_p=*/true,
9682 /*type_p=*/false,
9683 /*is_declaration=*/true);
9684 if (!qscope)
9685 qscope = global_namespace;
9687 /* Parse the unqualified-id. */
9688 identifier = cp_parser_unqualified_id (parser,
9689 /*template_keyword_p=*/false,
9690 /*check_dependency_p=*/true,
9691 /*declarator_p=*/true);
9693 /* The function we call to handle a using-declaration is different
9694 depending on what scope we are in. */
9695 if (identifier == error_mark_node)
9697 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9698 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9699 /* [namespace.udecl]
9701 A using declaration shall not name a template-id. */
9702 error ("a template-id may not appear in a using-declaration");
9703 else
9705 scope = current_scope ();
9706 if (scope && TYPE_P (scope))
9708 /* Create the USING_DECL. */
9709 decl = do_class_using_decl (build_nt (SCOPE_REF,
9710 parser->scope,
9711 identifier));
9712 /* Add it to the list of members in this class. */
9713 finish_member_declaration (decl);
9715 else
9717 decl = cp_parser_lookup_name_simple (parser, identifier);
9718 if (decl == error_mark_node)
9719 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9720 else if (scope)
9721 do_local_using_decl (decl, qscope, identifier);
9722 else
9723 do_toplevel_using_decl (decl, qscope, identifier);
9727 /* Look for the final `;'. */
9728 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9731 /* Parse a using-directive.
9733 using-directive:
9734 using namespace :: [opt] nested-name-specifier [opt]
9735 namespace-name ; */
9737 static void
9738 cp_parser_using_directive (cp_parser* parser)
9740 tree namespace_decl;
9741 tree attribs;
9743 /* Look for the `using' keyword. */
9744 cp_parser_require_keyword (parser, RID_USING, "`using'");
9745 /* And the `namespace' keyword. */
9746 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9747 /* Look for the optional `::' operator. */
9748 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9749 /* And the optional nested-name-specifier. */
9750 cp_parser_nested_name_specifier_opt (parser,
9751 /*typename_keyword_p=*/false,
9752 /*check_dependency_p=*/true,
9753 /*type_p=*/false,
9754 /*is_declaration=*/true);
9755 /* Get the namespace being used. */
9756 namespace_decl = cp_parser_namespace_name (parser);
9757 /* And any specified attributes. */
9758 attribs = cp_parser_attributes_opt (parser);
9759 /* Update the symbol table. */
9760 parse_using_directive (namespace_decl, attribs);
9761 /* Look for the final `;'. */
9762 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9765 /* Parse an asm-definition.
9767 asm-definition:
9768 asm ( string-literal ) ;
9770 GNU Extension:
9772 asm-definition:
9773 asm volatile [opt] ( string-literal ) ;
9774 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9775 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9776 : asm-operand-list [opt] ) ;
9777 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9778 : asm-operand-list [opt]
9779 : asm-operand-list [opt] ) ; */
9781 static void
9782 cp_parser_asm_definition (cp_parser* parser)
9784 cp_token *token;
9785 tree string;
9786 tree outputs = NULL_TREE;
9787 tree inputs = NULL_TREE;
9788 tree clobbers = NULL_TREE;
9789 tree asm_stmt;
9790 bool volatile_p = false;
9791 bool extended_p = false;
9793 /* Look for the `asm' keyword. */
9794 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9795 /* See if the next token is `volatile'. */
9796 if (cp_parser_allow_gnu_extensions_p (parser)
9797 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9799 /* Remember that we saw the `volatile' keyword. */
9800 volatile_p = true;
9801 /* Consume the token. */
9802 cp_lexer_consume_token (parser->lexer);
9804 /* Look for the opening `('. */
9805 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9806 /* Look for the string. */
9807 token = cp_parser_require (parser, CPP_STRING, "asm body");
9808 if (!token)
9809 return;
9810 string = token->value;
9811 /* If we're allowing GNU extensions, check for the extended assembly
9812 syntax. Unfortunately, the `:' tokens need not be separated by
9813 a space in C, and so, for compatibility, we tolerate that here
9814 too. Doing that means that we have to treat the `::' operator as
9815 two `:' tokens. */
9816 if (cp_parser_allow_gnu_extensions_p (parser)
9817 && at_function_scope_p ()
9818 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9819 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9821 bool inputs_p = false;
9822 bool clobbers_p = false;
9824 /* The extended syntax was used. */
9825 extended_p = true;
9827 /* Look for outputs. */
9828 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9830 /* Consume the `:'. */
9831 cp_lexer_consume_token (parser->lexer);
9832 /* Parse the output-operands. */
9833 if (cp_lexer_next_token_is_not (parser->lexer,
9834 CPP_COLON)
9835 && cp_lexer_next_token_is_not (parser->lexer,
9836 CPP_SCOPE)
9837 && cp_lexer_next_token_is_not (parser->lexer,
9838 CPP_CLOSE_PAREN))
9839 outputs = cp_parser_asm_operand_list (parser);
9841 /* If the next token is `::', there are no outputs, and the
9842 next token is the beginning of the inputs. */
9843 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9844 /* The inputs are coming next. */
9845 inputs_p = true;
9847 /* Look for inputs. */
9848 if (inputs_p
9849 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9851 /* Consume the `:' or `::'. */
9852 cp_lexer_consume_token (parser->lexer);
9853 /* Parse the output-operands. */
9854 if (cp_lexer_next_token_is_not (parser->lexer,
9855 CPP_COLON)
9856 && cp_lexer_next_token_is_not (parser->lexer,
9857 CPP_CLOSE_PAREN))
9858 inputs = cp_parser_asm_operand_list (parser);
9860 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9861 /* The clobbers are coming next. */
9862 clobbers_p = true;
9864 /* Look for clobbers. */
9865 if (clobbers_p
9866 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9868 /* Consume the `:' or `::'. */
9869 cp_lexer_consume_token (parser->lexer);
9870 /* Parse the clobbers. */
9871 if (cp_lexer_next_token_is_not (parser->lexer,
9872 CPP_CLOSE_PAREN))
9873 clobbers = cp_parser_asm_clobber_list (parser);
9876 /* Look for the closing `)'. */
9877 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9878 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9879 /*consume_paren=*/true);
9880 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9882 /* Create the ASM_STMT. */
9883 if (at_function_scope_p ())
9885 asm_stmt =
9886 finish_asm_stmt (volatile_p
9887 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9888 string, outputs, inputs, clobbers);
9889 /* If the extended syntax was not used, mark the ASM_STMT. */
9890 if (!extended_p)
9891 ASM_INPUT_P (asm_stmt) = 1;
9893 else
9894 assemble_asm (string);
9897 /* Declarators [gram.dcl.decl] */
9899 /* Parse an init-declarator.
9901 init-declarator:
9902 declarator initializer [opt]
9904 GNU Extension:
9906 init-declarator:
9907 declarator asm-specification [opt] attributes [opt] initializer [opt]
9909 function-definition:
9910 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9911 function-body
9912 decl-specifier-seq [opt] declarator function-try-block
9914 GNU Extension:
9916 function-definition:
9917 __extension__ function-definition
9919 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9920 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9921 then this declarator appears in a class scope. The new DECL created
9922 by this declarator is returned.
9924 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9925 for a function-definition here as well. If the declarator is a
9926 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9927 be TRUE upon return. By that point, the function-definition will
9928 have been completely parsed.
9930 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9931 is FALSE. */
9933 static tree
9934 cp_parser_init_declarator (cp_parser* parser,
9935 tree decl_specifiers,
9936 tree prefix_attributes,
9937 bool function_definition_allowed_p,
9938 bool member_p,
9939 int declares_class_or_enum,
9940 bool* function_definition_p)
9942 cp_token *token;
9943 tree declarator;
9944 tree attributes;
9945 tree asm_specification;
9946 tree initializer;
9947 tree decl = NULL_TREE;
9948 tree scope;
9949 bool is_initialized;
9950 bool is_parenthesized_init;
9951 bool is_non_constant_init;
9952 int ctor_dtor_or_conv_p;
9953 bool friend_p;
9954 bool pop_p = false;
9956 /* Assume that this is not the declarator for a function
9957 definition. */
9958 if (function_definition_p)
9959 *function_definition_p = false;
9961 /* Defer access checks while parsing the declarator; we cannot know
9962 what names are accessible until we know what is being
9963 declared. */
9964 resume_deferring_access_checks ();
9966 /* Parse the declarator. */
9967 declarator
9968 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9969 &ctor_dtor_or_conv_p,
9970 /*parenthesized_p=*/NULL,
9971 /*member_p=*/false);
9972 /* Gather up the deferred checks. */
9973 stop_deferring_access_checks ();
9975 /* If the DECLARATOR was erroneous, there's no need to go
9976 further. */
9977 if (declarator == error_mark_node)
9978 return error_mark_node;
9980 if (declares_class_or_enum & 2)
9981 cp_parser_check_for_definition_in_return_type
9982 (declarator, TREE_VALUE (decl_specifiers));
9984 /* Figure out what scope the entity declared by the DECLARATOR is
9985 located in. `grokdeclarator' sometimes changes the scope, so
9986 we compute it now. */
9987 scope = get_scope_of_declarator (declarator);
9989 /* If we're allowing GNU extensions, look for an asm-specification
9990 and attributes. */
9991 if (cp_parser_allow_gnu_extensions_p (parser))
9993 /* Look for an asm-specification. */
9994 asm_specification = cp_parser_asm_specification_opt (parser);
9995 /* And attributes. */
9996 attributes = cp_parser_attributes_opt (parser);
9998 else
10000 asm_specification = NULL_TREE;
10001 attributes = NULL_TREE;
10004 /* Peek at the next token. */
10005 token = cp_lexer_peek_token (parser->lexer);
10006 /* Check to see if the token indicates the start of a
10007 function-definition. */
10008 if (cp_parser_token_starts_function_definition_p (token))
10010 if (!function_definition_allowed_p)
10012 /* If a function-definition should not appear here, issue an
10013 error message. */
10014 cp_parser_error (parser,
10015 "a function-definition is not allowed here");
10016 return error_mark_node;
10018 else
10020 /* Neither attributes nor an asm-specification are allowed
10021 on a function-definition. */
10022 if (asm_specification)
10023 error ("an asm-specification is not allowed on a function-definition");
10024 if (attributes)
10025 error ("attributes are not allowed on a function-definition");
10026 /* This is a function-definition. */
10027 *function_definition_p = true;
10029 /* Parse the function definition. */
10030 if (member_p)
10031 decl = cp_parser_save_member_function_body (parser,
10032 decl_specifiers,
10033 declarator,
10034 prefix_attributes);
10035 else
10036 decl
10037 = (cp_parser_function_definition_from_specifiers_and_declarator
10038 (parser, decl_specifiers, prefix_attributes, declarator));
10040 return decl;
10044 /* [dcl.dcl]
10046 Only in function declarations for constructors, destructors, and
10047 type conversions can the decl-specifier-seq be omitted.
10049 We explicitly postpone this check past the point where we handle
10050 function-definitions because we tolerate function-definitions
10051 that are missing their return types in some modes. */
10052 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10054 cp_parser_error (parser,
10055 "expected constructor, destructor, or type conversion");
10056 return error_mark_node;
10059 /* An `=' or an `(' indicates an initializer. */
10060 is_initialized = (token->type == CPP_EQ
10061 || token->type == CPP_OPEN_PAREN);
10062 /* If the init-declarator isn't initialized and isn't followed by a
10063 `,' or `;', it's not a valid init-declarator. */
10064 if (!is_initialized
10065 && token->type != CPP_COMMA
10066 && token->type != CPP_SEMICOLON)
10068 cp_parser_error (parser, "expected initializer");
10069 return error_mark_node;
10072 /* Because start_decl has side-effects, we should only call it if we
10073 know we're going ahead. By this point, we know that we cannot
10074 possibly be looking at any other construct. */
10075 cp_parser_commit_to_tentative_parse (parser);
10077 /* If the decl specifiers were bad, issue an error now that we're
10078 sure this was intended to be a declarator. Then continue
10079 declaring the variable(s), as int, to try to cut down on further
10080 errors. */
10081 if (decl_specifiers != NULL
10082 && TREE_VALUE (decl_specifiers) == error_mark_node)
10084 cp_parser_error (parser, "invalid type in declaration");
10085 TREE_VALUE (decl_specifiers) = integer_type_node;
10088 /* Check to see whether or not this declaration is a friend. */
10089 friend_p = cp_parser_friend_p (decl_specifiers);
10091 /* Check that the number of template-parameter-lists is OK. */
10092 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10093 return error_mark_node;
10095 /* Enter the newly declared entry in the symbol table. If we're
10096 processing a declaration in a class-specifier, we wait until
10097 after processing the initializer. */
10098 if (!member_p)
10100 if (parser->in_unbraced_linkage_specification_p)
10102 decl_specifiers = tree_cons (error_mark_node,
10103 get_identifier ("extern"),
10104 decl_specifiers);
10105 have_extern_spec = false;
10107 decl = start_decl (declarator, decl_specifiers,
10108 is_initialized, attributes, prefix_attributes);
10111 /* Enter the SCOPE. That way unqualified names appearing in the
10112 initializer will be looked up in SCOPE. */
10113 if (scope)
10114 pop_p = push_scope (scope);
10116 /* Perform deferred access control checks, now that we know in which
10117 SCOPE the declared entity resides. */
10118 if (!member_p && decl)
10120 tree saved_current_function_decl = NULL_TREE;
10122 /* If the entity being declared is a function, pretend that we
10123 are in its scope. If it is a `friend', it may have access to
10124 things that would not otherwise be accessible. */
10125 if (TREE_CODE (decl) == FUNCTION_DECL)
10127 saved_current_function_decl = current_function_decl;
10128 current_function_decl = decl;
10131 /* Perform the access control checks for the declarator and the
10132 the decl-specifiers. */
10133 perform_deferred_access_checks ();
10135 /* Restore the saved value. */
10136 if (TREE_CODE (decl) == FUNCTION_DECL)
10137 current_function_decl = saved_current_function_decl;
10140 /* Parse the initializer. */
10141 if (is_initialized)
10142 initializer = cp_parser_initializer (parser,
10143 &is_parenthesized_init,
10144 &is_non_constant_init);
10145 else
10147 initializer = NULL_TREE;
10148 is_parenthesized_init = false;
10149 is_non_constant_init = true;
10152 /* The old parser allows attributes to appear after a parenthesized
10153 initializer. Mark Mitchell proposed removing this functionality
10154 on the GCC mailing lists on 2002-08-13. This parser accepts the
10155 attributes -- but ignores them. */
10156 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10157 if (cp_parser_attributes_opt (parser))
10158 warning ("attributes after parenthesized initializer ignored");
10160 /* Leave the SCOPE, now that we have processed the initializer. It
10161 is important to do this before calling cp_finish_decl because it
10162 makes decisions about whether to create DECL_STMTs or not based
10163 on the current scope. */
10164 if (pop_p)
10165 pop_scope (scope);
10167 /* For an in-class declaration, use `grokfield' to create the
10168 declaration. */
10169 if (member_p)
10171 decl = grokfield (declarator, decl_specifiers,
10172 initializer, /*asmspec=*/NULL_TREE,
10173 /*attributes=*/NULL_TREE);
10174 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10175 cp_parser_save_default_args (parser, decl);
10178 /* Finish processing the declaration. But, skip friend
10179 declarations. */
10180 if (!friend_p && decl)
10181 cp_finish_decl (decl,
10182 initializer,
10183 asm_specification,
10184 /* If the initializer is in parentheses, then this is
10185 a direct-initialization, which means that an
10186 `explicit' constructor is OK. Otherwise, an
10187 `explicit' constructor cannot be used. */
10188 ((is_parenthesized_init || !is_initialized)
10189 ? 0 : LOOKUP_ONLYCONVERTING));
10191 /* Remember whether or not variables were initialized by
10192 constant-expressions. */
10193 if (decl && TREE_CODE (decl) == VAR_DECL
10194 && is_initialized && !is_non_constant_init)
10195 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10197 return decl;
10200 /* Parse a declarator.
10202 declarator:
10203 direct-declarator
10204 ptr-operator declarator
10206 abstract-declarator:
10207 ptr-operator abstract-declarator [opt]
10208 direct-abstract-declarator
10210 GNU Extensions:
10212 declarator:
10213 attributes [opt] direct-declarator
10214 attributes [opt] ptr-operator declarator
10216 abstract-declarator:
10217 attributes [opt] ptr-operator abstract-declarator [opt]
10218 attributes [opt] direct-abstract-declarator
10220 Returns a representation of the declarator. If the declarator has
10221 the form `* declarator', then an INDIRECT_REF is returned, whose
10222 only operand is the sub-declarator. Analogously, `& declarator' is
10223 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10224 used. The first operand is the TYPE for `X'. The second operand
10225 is an INDIRECT_REF whose operand is the sub-declarator.
10227 Otherwise, the representation is as for a direct-declarator.
10229 (It would be better to define a structure type to represent
10230 declarators, rather than abusing `tree' nodes to represent
10231 declarators. That would be much clearer and save some memory.
10232 There is no reason for declarators to be garbage-collected, for
10233 example; they are created during parser and no longer needed after
10234 `grokdeclarator' has been called.)
10236 For a ptr-operator that has the optional cv-qualifier-seq,
10237 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10238 node.
10240 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10241 detect constructor, destructor or conversion operators. It is set
10242 to -1 if the declarator is a name, and +1 if it is a
10243 function. Otherwise it is set to zero. Usually you just want to
10244 test for >0, but internally the negative value is used.
10246 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10247 a decl-specifier-seq unless it declares a constructor, destructor,
10248 or conversion. It might seem that we could check this condition in
10249 semantic analysis, rather than parsing, but that makes it difficult
10250 to handle something like `f()'. We want to notice that there are
10251 no decl-specifiers, and therefore realize that this is an
10252 expression, not a declaration.)
10254 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10255 the declarator is a direct-declarator of the form "(...)".
10257 MEMBER_P is true iff this declarator is a member-declarator. */
10259 static tree
10260 cp_parser_declarator (cp_parser* parser,
10261 cp_parser_declarator_kind dcl_kind,
10262 int* ctor_dtor_or_conv_p,
10263 bool* parenthesized_p,
10264 bool member_p)
10266 cp_token *token;
10267 tree declarator;
10268 enum tree_code code;
10269 tree cv_qualifier_seq;
10270 tree class_type;
10271 tree attributes = NULL_TREE;
10273 /* Assume this is not a constructor, destructor, or type-conversion
10274 operator. */
10275 if (ctor_dtor_or_conv_p)
10276 *ctor_dtor_or_conv_p = 0;
10278 if (cp_parser_allow_gnu_extensions_p (parser))
10279 attributes = cp_parser_attributes_opt (parser);
10281 /* Peek at the next token. */
10282 token = cp_lexer_peek_token (parser->lexer);
10284 /* Check for the ptr-operator production. */
10285 cp_parser_parse_tentatively (parser);
10286 /* Parse the ptr-operator. */
10287 code = cp_parser_ptr_operator (parser,
10288 &class_type,
10289 &cv_qualifier_seq);
10290 /* If that worked, then we have a ptr-operator. */
10291 if (cp_parser_parse_definitely (parser))
10293 /* If a ptr-operator was found, then this declarator was not
10294 parenthesized. */
10295 if (parenthesized_p)
10296 *parenthesized_p = true;
10297 /* The dependent declarator is optional if we are parsing an
10298 abstract-declarator. */
10299 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10300 cp_parser_parse_tentatively (parser);
10302 /* Parse the dependent declarator. */
10303 declarator = cp_parser_declarator (parser, dcl_kind,
10304 /*ctor_dtor_or_conv_p=*/NULL,
10305 /*parenthesized_p=*/NULL,
10306 /*member_p=*/false);
10308 /* If we are parsing an abstract-declarator, we must handle the
10309 case where the dependent declarator is absent. */
10310 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10311 && !cp_parser_parse_definitely (parser))
10312 declarator = NULL_TREE;
10314 /* Build the representation of the ptr-operator. */
10315 if (code == INDIRECT_REF)
10316 declarator = make_pointer_declarator (cv_qualifier_seq,
10317 declarator);
10318 else
10319 declarator = make_reference_declarator (cv_qualifier_seq,
10320 declarator);
10321 /* Handle the pointer-to-member case. */
10322 if (class_type)
10323 declarator = build_nt (SCOPE_REF, class_type, declarator);
10325 /* Everything else is a direct-declarator. */
10326 else
10328 if (parenthesized_p)
10329 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10330 CPP_OPEN_PAREN);
10331 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10332 ctor_dtor_or_conv_p,
10333 member_p);
10336 if (attributes && declarator != error_mark_node)
10337 declarator = tree_cons (attributes, declarator, NULL_TREE);
10339 return declarator;
10342 /* Parse a direct-declarator or direct-abstract-declarator.
10344 direct-declarator:
10345 declarator-id
10346 direct-declarator ( parameter-declaration-clause )
10347 cv-qualifier-seq [opt]
10348 exception-specification [opt]
10349 direct-declarator [ constant-expression [opt] ]
10350 ( declarator )
10352 direct-abstract-declarator:
10353 direct-abstract-declarator [opt]
10354 ( parameter-declaration-clause )
10355 cv-qualifier-seq [opt]
10356 exception-specification [opt]
10357 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10358 ( abstract-declarator )
10360 Returns a representation of the declarator. DCL_KIND is
10361 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10362 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10363 we are parsing a direct-declarator. It is
10364 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10365 of ambiguity we prefer an abstract declarator, as per
10366 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10367 cp_parser_declarator.
10369 For the declarator-id production, the representation is as for an
10370 id-expression, except that a qualified name is represented as a
10371 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10372 see the documentation of the FUNCTION_DECLARATOR_* macros for
10373 information about how to find the various declarator components.
10374 An array-declarator is represented as an ARRAY_REF. The
10375 direct-declarator is the first operand; the constant-expression
10376 indicating the size of the array is the second operand. */
10378 static tree
10379 cp_parser_direct_declarator (cp_parser* parser,
10380 cp_parser_declarator_kind dcl_kind,
10381 int* ctor_dtor_or_conv_p,
10382 bool member_p)
10384 cp_token *token;
10385 tree declarator = NULL_TREE;
10386 tree scope = NULL_TREE;
10387 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10388 bool saved_in_declarator_p = parser->in_declarator_p;
10389 bool first = true;
10390 bool pop_p = false;
10392 while (true)
10394 /* Peek at the next token. */
10395 token = cp_lexer_peek_token (parser->lexer);
10396 if (token->type == CPP_OPEN_PAREN)
10398 /* This is either a parameter-declaration-clause, or a
10399 parenthesized declarator. When we know we are parsing a
10400 named declarator, it must be a parenthesized declarator
10401 if FIRST is true. For instance, `(int)' is a
10402 parameter-declaration-clause, with an omitted
10403 direct-abstract-declarator. But `((*))', is a
10404 parenthesized abstract declarator. Finally, when T is a
10405 template parameter `(T)' is a
10406 parameter-declaration-clause, and not a parenthesized
10407 named declarator.
10409 We first try and parse a parameter-declaration-clause,
10410 and then try a nested declarator (if FIRST is true).
10412 It is not an error for it not to be a
10413 parameter-declaration-clause, even when FIRST is
10414 false. Consider,
10416 int i (int);
10417 int i (3);
10419 The first is the declaration of a function while the
10420 second is a the definition of a variable, including its
10421 initializer.
10423 Having seen only the parenthesis, we cannot know which of
10424 these two alternatives should be selected. Even more
10425 complex are examples like:
10427 int i (int (a));
10428 int i (int (3));
10430 The former is a function-declaration; the latter is a
10431 variable initialization.
10433 Thus again, we try a parameter-declaration-clause, and if
10434 that fails, we back out and return. */
10436 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10438 tree params;
10439 unsigned saved_num_template_parameter_lists;
10441 /* In a member-declarator, the only valid interpretation
10442 of a parenthesis is the start of a
10443 parameter-declaration-clause. (It is invalid to
10444 initialize a static data member with a parenthesized
10445 initializer; only the "=" form of initialization is
10446 permitted.) */
10447 if (!member_p)
10448 cp_parser_parse_tentatively (parser);
10450 /* Consume the `('. */
10451 cp_lexer_consume_token (parser->lexer);
10452 if (first)
10454 /* If this is going to be an abstract declarator, we're
10455 in a declarator and we can't have default args. */
10456 parser->default_arg_ok_p = false;
10457 parser->in_declarator_p = true;
10460 /* Inside the function parameter list, surrounding
10461 template-parameter-lists do not apply. */
10462 saved_num_template_parameter_lists
10463 = parser->num_template_parameter_lists;
10464 parser->num_template_parameter_lists = 0;
10466 /* Parse the parameter-declaration-clause. */
10467 params = cp_parser_parameter_declaration_clause (parser);
10469 parser->num_template_parameter_lists
10470 = saved_num_template_parameter_lists;
10472 /* If all went well, parse the cv-qualifier-seq and the
10473 exception-specification. */
10474 if (member_p || cp_parser_parse_definitely (parser))
10476 tree cv_qualifiers;
10477 tree exception_specification;
10479 if (ctor_dtor_or_conv_p)
10480 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10481 first = false;
10482 /* Consume the `)'. */
10483 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10485 /* Parse the cv-qualifier-seq. */
10486 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10487 /* And the exception-specification. */
10488 exception_specification
10489 = cp_parser_exception_specification_opt (parser);
10491 /* Create the function-declarator. */
10492 declarator = make_call_declarator (declarator,
10493 params,
10494 cv_qualifiers,
10495 exception_specification);
10496 /* Any subsequent parameter lists are to do with
10497 return type, so are not those of the declared
10498 function. */
10499 parser->default_arg_ok_p = false;
10501 /* Repeat the main loop. */
10502 continue;
10506 /* If this is the first, we can try a parenthesized
10507 declarator. */
10508 if (first)
10510 bool saved_in_type_id_in_expr_p;
10512 parser->default_arg_ok_p = saved_default_arg_ok_p;
10513 parser->in_declarator_p = saved_in_declarator_p;
10515 /* Consume the `('. */
10516 cp_lexer_consume_token (parser->lexer);
10517 /* Parse the nested declarator. */
10518 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10519 parser->in_type_id_in_expr_p = true;
10520 declarator
10521 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10522 /*parenthesized_p=*/NULL,
10523 member_p);
10524 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10525 first = false;
10526 /* Expect a `)'. */
10527 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10528 declarator = error_mark_node;
10529 if (declarator == error_mark_node)
10530 break;
10532 goto handle_declarator;
10534 /* Otherwise, we must be done. */
10535 else
10536 break;
10538 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10539 && token->type == CPP_OPEN_SQUARE)
10541 /* Parse an array-declarator. */
10542 tree bounds;
10544 if (ctor_dtor_or_conv_p)
10545 *ctor_dtor_or_conv_p = 0;
10547 first = false;
10548 parser->default_arg_ok_p = false;
10549 parser->in_declarator_p = true;
10550 /* Consume the `['. */
10551 cp_lexer_consume_token (parser->lexer);
10552 /* Peek at the next token. */
10553 token = cp_lexer_peek_token (parser->lexer);
10554 /* If the next token is `]', then there is no
10555 constant-expression. */
10556 if (token->type != CPP_CLOSE_SQUARE)
10558 bool non_constant_p;
10560 bounds
10561 = cp_parser_constant_expression (parser,
10562 /*allow_non_constant=*/true,
10563 &non_constant_p);
10564 if (!non_constant_p)
10565 bounds = fold_non_dependent_expr (bounds);
10567 else
10568 bounds = NULL_TREE;
10569 /* Look for the closing `]'. */
10570 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10572 declarator = error_mark_node;
10573 break;
10576 declarator = build_nt (ARRAY_REF, declarator, bounds);
10578 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10580 /* Parse a declarator-id */
10581 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10582 cp_parser_parse_tentatively (parser);
10583 declarator = cp_parser_declarator_id (parser);
10584 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10586 if (!cp_parser_parse_definitely (parser))
10587 declarator = error_mark_node;
10588 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10590 cp_parser_error (parser, "expected unqualified-id");
10591 declarator = error_mark_node;
10595 if (declarator == error_mark_node)
10596 break;
10598 if (TREE_CODE (declarator) == SCOPE_REF
10599 && !current_scope ())
10601 tree scope = TREE_OPERAND (declarator, 0);
10603 /* In the declaration of a member of a template class
10604 outside of the class itself, the SCOPE will sometimes
10605 be a TYPENAME_TYPE. For example, given:
10607 template <typename T>
10608 int S<T>::R::i = 3;
10610 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10611 this context, we must resolve S<T>::R to an ordinary
10612 type, rather than a typename type.
10614 The reason we normally avoid resolving TYPENAME_TYPEs
10615 is that a specialization of `S' might render
10616 `S<T>::R' not a type. However, if `S' is
10617 specialized, then this `i' will not be used, so there
10618 is no harm in resolving the types here. */
10619 if (TREE_CODE (scope) == TYPENAME_TYPE)
10621 tree type;
10623 /* Resolve the TYPENAME_TYPE. */
10624 type = resolve_typename_type (scope,
10625 /*only_current_p=*/false);
10626 /* If that failed, the declarator is invalid. */
10627 if (type == error_mark_node)
10628 error ("`%T::%D' is not a type",
10629 TYPE_CONTEXT (scope),
10630 TYPE_IDENTIFIER (scope));
10631 /* Build a new DECLARATOR. */
10632 declarator = build_nt (SCOPE_REF,
10633 type,
10634 TREE_OPERAND (declarator, 1));
10638 /* Check to see whether the declarator-id names a constructor,
10639 destructor, or conversion. */
10640 if (declarator && ctor_dtor_or_conv_p
10641 && ((TREE_CODE (declarator) == SCOPE_REF
10642 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10643 || (TREE_CODE (declarator) != SCOPE_REF
10644 && at_class_scope_p ())))
10646 tree unqualified_name;
10647 tree class_type;
10649 /* Get the unqualified part of the name. */
10650 if (TREE_CODE (declarator) == SCOPE_REF)
10652 class_type = TREE_OPERAND (declarator, 0);
10653 unqualified_name = TREE_OPERAND (declarator, 1);
10655 else
10657 class_type = current_class_type;
10658 unqualified_name = declarator;
10661 /* See if it names ctor, dtor or conv. */
10662 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10663 || IDENTIFIER_TYPENAME_P (unqualified_name)
10664 || constructor_name_p (unqualified_name, class_type)
10665 || (TREE_CODE (unqualified_name) == TYPE_DECL
10666 && same_type_p (TREE_TYPE (unqualified_name),
10667 class_type)))
10668 *ctor_dtor_or_conv_p = -1;
10671 handle_declarator:;
10672 scope = get_scope_of_declarator (declarator);
10673 if (scope)
10674 /* Any names that appear after the declarator-id for a
10675 member are looked up in the containing scope. */
10676 pop_p = push_scope (scope);
10677 parser->in_declarator_p = true;
10678 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10679 || (declarator
10680 && (TREE_CODE (declarator) == SCOPE_REF
10681 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10682 /* Default args are only allowed on function
10683 declarations. */
10684 parser->default_arg_ok_p = saved_default_arg_ok_p;
10685 else
10686 parser->default_arg_ok_p = false;
10688 first = false;
10690 /* We're done. */
10691 else
10692 break;
10695 /* For an abstract declarator, we might wind up with nothing at this
10696 point. That's an error; the declarator is not optional. */
10697 if (!declarator)
10698 cp_parser_error (parser, "expected declarator");
10700 /* If we entered a scope, we must exit it now. */
10701 if (pop_p)
10702 pop_scope (scope);
10704 parser->default_arg_ok_p = saved_default_arg_ok_p;
10705 parser->in_declarator_p = saved_in_declarator_p;
10707 return declarator;
10710 /* Parse a ptr-operator.
10712 ptr-operator:
10713 * cv-qualifier-seq [opt]
10715 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10717 GNU Extension:
10719 ptr-operator:
10720 & cv-qualifier-seq [opt]
10722 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10723 used. Returns ADDR_EXPR if a reference was used. In the
10724 case of a pointer-to-member, *TYPE is filled in with the
10725 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10726 with the cv-qualifier-seq, or NULL_TREE, if there are no
10727 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10729 static enum tree_code
10730 cp_parser_ptr_operator (cp_parser* parser,
10731 tree* type,
10732 tree* cv_qualifier_seq)
10734 enum tree_code code = ERROR_MARK;
10735 cp_token *token;
10737 /* Assume that it's not a pointer-to-member. */
10738 *type = NULL_TREE;
10739 /* And that there are no cv-qualifiers. */
10740 *cv_qualifier_seq = NULL_TREE;
10742 /* Peek at the next token. */
10743 token = cp_lexer_peek_token (parser->lexer);
10744 /* If it's a `*' or `&' we have a pointer or reference. */
10745 if (token->type == CPP_MULT || token->type == CPP_AND)
10747 /* Remember which ptr-operator we were processing. */
10748 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10750 /* Consume the `*' or `&'. */
10751 cp_lexer_consume_token (parser->lexer);
10753 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10754 `&', if we are allowing GNU extensions. (The only qualifier
10755 that can legally appear after `&' is `restrict', but that is
10756 enforced during semantic analysis. */
10757 if (code == INDIRECT_REF
10758 || cp_parser_allow_gnu_extensions_p (parser))
10759 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10761 else
10763 /* Try the pointer-to-member case. */
10764 cp_parser_parse_tentatively (parser);
10765 /* Look for the optional `::' operator. */
10766 cp_parser_global_scope_opt (parser,
10767 /*current_scope_valid_p=*/false);
10768 /* Look for the nested-name specifier. */
10769 cp_parser_nested_name_specifier (parser,
10770 /*typename_keyword_p=*/false,
10771 /*check_dependency_p=*/true,
10772 /*type_p=*/false,
10773 /*is_declaration=*/false);
10774 /* If we found it, and the next token is a `*', then we are
10775 indeed looking at a pointer-to-member operator. */
10776 if (!cp_parser_error_occurred (parser)
10777 && cp_parser_require (parser, CPP_MULT, "`*'"))
10779 /* The type of which the member is a member is given by the
10780 current SCOPE. */
10781 *type = parser->scope;
10782 /* The next name will not be qualified. */
10783 parser->scope = NULL_TREE;
10784 parser->qualifying_scope = NULL_TREE;
10785 parser->object_scope = NULL_TREE;
10786 /* Indicate that the `*' operator was used. */
10787 code = INDIRECT_REF;
10788 /* Look for the optional cv-qualifier-seq. */
10789 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10791 /* If that didn't work we don't have a ptr-operator. */
10792 if (!cp_parser_parse_definitely (parser))
10793 cp_parser_error (parser, "expected ptr-operator");
10796 return code;
10799 /* Parse an (optional) cv-qualifier-seq.
10801 cv-qualifier-seq:
10802 cv-qualifier cv-qualifier-seq [opt]
10804 Returns a TREE_LIST. The TREE_VALUE of each node is the
10805 representation of a cv-qualifier. */
10807 static tree
10808 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10810 tree cv_qualifiers = NULL_TREE;
10812 while (true)
10814 tree cv_qualifier;
10816 /* Look for the next cv-qualifier. */
10817 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10818 /* If we didn't find one, we're done. */
10819 if (!cv_qualifier)
10820 break;
10822 /* Add this cv-qualifier to the list. */
10823 cv_qualifiers
10824 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10827 /* We built up the list in reverse order. */
10828 return nreverse (cv_qualifiers);
10831 /* Parse an (optional) cv-qualifier.
10833 cv-qualifier:
10834 const
10835 volatile
10837 GNU Extension:
10839 cv-qualifier:
10840 __restrict__ */
10842 static tree
10843 cp_parser_cv_qualifier_opt (cp_parser* parser)
10845 cp_token *token;
10846 tree cv_qualifier = NULL_TREE;
10848 /* Peek at the next token. */
10849 token = cp_lexer_peek_token (parser->lexer);
10850 /* See if it's a cv-qualifier. */
10851 switch (token->keyword)
10853 case RID_CONST:
10854 case RID_VOLATILE:
10855 case RID_RESTRICT:
10856 /* Save the value of the token. */
10857 cv_qualifier = token->value;
10858 /* Consume the token. */
10859 cp_lexer_consume_token (parser->lexer);
10860 break;
10862 default:
10863 break;
10866 return cv_qualifier;
10869 /* Parse a declarator-id.
10871 declarator-id:
10872 id-expression
10873 :: [opt] nested-name-specifier [opt] type-name
10875 In the `id-expression' case, the value returned is as for
10876 cp_parser_id_expression if the id-expression was an unqualified-id.
10877 If the id-expression was a qualified-id, then a SCOPE_REF is
10878 returned. The first operand is the scope (either a NAMESPACE_DECL
10879 or TREE_TYPE), but the second is still just a representation of an
10880 unqualified-id. */
10882 static tree
10883 cp_parser_declarator_id (cp_parser* parser)
10885 tree id_expression;
10887 /* The expression must be an id-expression. Assume that qualified
10888 names are the names of types so that:
10890 template <class T>
10891 int S<T>::R::i = 3;
10893 will work; we must treat `S<T>::R' as the name of a type.
10894 Similarly, assume that qualified names are templates, where
10895 required, so that:
10897 template <class T>
10898 int S<T>::R<T>::i = 3;
10900 will work, too. */
10901 id_expression = cp_parser_id_expression (parser,
10902 /*template_keyword_p=*/false,
10903 /*check_dependency_p=*/false,
10904 /*template_p=*/NULL,
10905 /*declarator_p=*/true);
10906 /* If the name was qualified, create a SCOPE_REF to represent
10907 that. */
10908 if (parser->scope && id_expression != error_mark_node)
10910 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10911 parser->scope = NULL_TREE;
10914 return id_expression;
10917 /* Parse a type-id.
10919 type-id:
10920 type-specifier-seq abstract-declarator [opt]
10922 Returns the TYPE specified. */
10924 static tree
10925 cp_parser_type_id (cp_parser* parser)
10927 tree type_specifier_seq;
10928 tree abstract_declarator;
10930 /* Parse the type-specifier-seq. */
10931 type_specifier_seq
10932 = cp_parser_type_specifier_seq (parser);
10933 if (type_specifier_seq == error_mark_node)
10934 return error_mark_node;
10936 /* There might or might not be an abstract declarator. */
10937 cp_parser_parse_tentatively (parser);
10938 /* Look for the declarator. */
10939 abstract_declarator
10940 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10941 /*parenthesized_p=*/NULL,
10942 /*member_p=*/false);
10943 /* Check to see if there really was a declarator. */
10944 if (!cp_parser_parse_definitely (parser))
10945 abstract_declarator = NULL_TREE;
10947 return groktypename (build_tree_list (type_specifier_seq,
10948 abstract_declarator));
10951 /* Parse a type-specifier-seq.
10953 type-specifier-seq:
10954 type-specifier type-specifier-seq [opt]
10956 GNU extension:
10958 type-specifier-seq:
10959 attributes type-specifier-seq [opt]
10961 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10962 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10964 static tree
10965 cp_parser_type_specifier_seq (cp_parser* parser)
10967 bool seen_type_specifier = false;
10968 tree type_specifier_seq = NULL_TREE;
10970 /* Parse the type-specifiers and attributes. */
10971 while (true)
10973 tree type_specifier;
10975 /* Check for attributes first. */
10976 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10978 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10979 NULL_TREE,
10980 type_specifier_seq);
10981 continue;
10984 /* After the first type-specifier, others are optional. */
10985 if (seen_type_specifier)
10986 cp_parser_parse_tentatively (parser);
10987 /* Look for the type-specifier. */
10988 type_specifier = cp_parser_type_specifier (parser,
10989 CP_PARSER_FLAGS_NONE,
10990 /*is_friend=*/false,
10991 /*is_declaration=*/false,
10992 NULL,
10993 NULL);
10994 /* If the first type-specifier could not be found, this is not a
10995 type-specifier-seq at all. */
10996 if (!seen_type_specifier && type_specifier == error_mark_node)
10997 return error_mark_node;
10998 /* If subsequent type-specifiers could not be found, the
10999 type-specifier-seq is complete. */
11000 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
11001 break;
11003 /* Add the new type-specifier to the list. */
11004 type_specifier_seq
11005 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
11006 seen_type_specifier = true;
11009 /* We built up the list in reverse order. */
11010 return nreverse (type_specifier_seq);
11013 /* Parse a parameter-declaration-clause.
11015 parameter-declaration-clause:
11016 parameter-declaration-list [opt] ... [opt]
11017 parameter-declaration-list , ...
11019 Returns a representation for the parameter declarations. Each node
11020 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
11021 representation.) If the parameter-declaration-clause ends with an
11022 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
11023 list. A return value of NULL_TREE indicates a
11024 parameter-declaration-clause consisting only of an ellipsis. */
11026 static tree
11027 cp_parser_parameter_declaration_clause (cp_parser* parser)
11029 tree parameters;
11030 cp_token *token;
11031 bool ellipsis_p;
11033 /* Peek at the next token. */
11034 token = cp_lexer_peek_token (parser->lexer);
11035 /* Check for trivial parameter-declaration-clauses. */
11036 if (token->type == CPP_ELLIPSIS)
11038 /* Consume the `...' token. */
11039 cp_lexer_consume_token (parser->lexer);
11040 return NULL_TREE;
11042 else if (token->type == CPP_CLOSE_PAREN)
11043 /* There are no parameters. */
11045 #ifndef NO_IMPLICIT_EXTERN_C
11046 if (in_system_header && current_class_type == NULL
11047 && current_lang_name == lang_name_c)
11048 return NULL_TREE;
11049 else
11050 #endif
11051 return void_list_node;
11053 /* Check for `(void)', too, which is a special case. */
11054 else if (token->keyword == RID_VOID
11055 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11056 == CPP_CLOSE_PAREN))
11058 /* Consume the `void' token. */
11059 cp_lexer_consume_token (parser->lexer);
11060 /* There are no parameters. */
11061 return void_list_node;
11064 /* Parse the parameter-declaration-list. */
11065 parameters = cp_parser_parameter_declaration_list (parser);
11066 /* If a parse error occurred while parsing the
11067 parameter-declaration-list, then the entire
11068 parameter-declaration-clause is erroneous. */
11069 if (parameters == error_mark_node)
11070 return error_mark_node;
11072 /* Peek at the next token. */
11073 token = cp_lexer_peek_token (parser->lexer);
11074 /* If it's a `,', the clause should terminate with an ellipsis. */
11075 if (token->type == CPP_COMMA)
11077 /* Consume the `,'. */
11078 cp_lexer_consume_token (parser->lexer);
11079 /* Expect an ellipsis. */
11080 ellipsis_p
11081 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11083 /* It might also be `...' if the optional trailing `,' was
11084 omitted. */
11085 else if (token->type == CPP_ELLIPSIS)
11087 /* Consume the `...' token. */
11088 cp_lexer_consume_token (parser->lexer);
11089 /* And remember that we saw it. */
11090 ellipsis_p = true;
11092 else
11093 ellipsis_p = false;
11095 /* Finish the parameter list. */
11096 return finish_parmlist (parameters, ellipsis_p);
11099 /* Parse a parameter-declaration-list.
11101 parameter-declaration-list:
11102 parameter-declaration
11103 parameter-declaration-list , parameter-declaration
11105 Returns a representation of the parameter-declaration-list, as for
11106 cp_parser_parameter_declaration_clause. However, the
11107 `void_list_node' is never appended to the list. */
11109 static tree
11110 cp_parser_parameter_declaration_list (cp_parser* parser)
11112 tree parameters = NULL_TREE;
11114 /* Look for more parameters. */
11115 while (true)
11117 tree parameter;
11118 bool parenthesized_p;
11119 /* Parse the parameter. */
11120 parameter
11121 = cp_parser_parameter_declaration (parser,
11122 /*template_parm_p=*/false,
11123 &parenthesized_p);
11125 /* If a parse error occurred parsing the parameter declaration,
11126 then the entire parameter-declaration-list is erroneous. */
11127 if (parameter == error_mark_node)
11129 parameters = error_mark_node;
11130 break;
11132 /* Add the new parameter to the list. */
11133 TREE_CHAIN (parameter) = parameters;
11134 parameters = parameter;
11136 /* Peek at the next token. */
11137 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11138 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11139 /* The parameter-declaration-list is complete. */
11140 break;
11141 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11143 cp_token *token;
11145 /* Peek at the next token. */
11146 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11147 /* If it's an ellipsis, then the list is complete. */
11148 if (token->type == CPP_ELLIPSIS)
11149 break;
11150 /* Otherwise, there must be more parameters. Consume the
11151 `,'. */
11152 cp_lexer_consume_token (parser->lexer);
11153 /* When parsing something like:
11155 int i(float f, double d)
11157 we can tell after seeing the declaration for "f" that we
11158 are not looking at an initialization of a variable "i",
11159 but rather at the declaration of a function "i".
11161 Due to the fact that the parsing of template arguments
11162 (as specified to a template-id) requires backtracking we
11163 cannot use this technique when inside a template argument
11164 list. */
11165 if (!parser->in_template_argument_list_p
11166 && !parser->in_type_id_in_expr_p
11167 && cp_parser_parsing_tentatively (parser)
11168 && !cp_parser_committed_to_tentative_parse (parser)
11169 /* However, a parameter-declaration of the form
11170 "foat(f)" (which is a valid declaration of a
11171 parameter "f") can also be interpreted as an
11172 expression (the conversion of "f" to "float"). */
11173 && !parenthesized_p)
11174 cp_parser_commit_to_tentative_parse (parser);
11176 else
11178 cp_parser_error (parser, "expected `,' or `...'");
11179 if (!cp_parser_parsing_tentatively (parser)
11180 || cp_parser_committed_to_tentative_parse (parser))
11181 cp_parser_skip_to_closing_parenthesis (parser,
11182 /*recovering=*/true,
11183 /*or_comma=*/false,
11184 /*consume_paren=*/false);
11185 break;
11189 /* We built up the list in reverse order; straighten it out now. */
11190 return nreverse (parameters);
11193 /* Parse a parameter declaration.
11195 parameter-declaration:
11196 decl-specifier-seq declarator
11197 decl-specifier-seq declarator = assignment-expression
11198 decl-specifier-seq abstract-declarator [opt]
11199 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11201 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11202 declares a template parameter. (In that case, a non-nested `>'
11203 token encountered during the parsing of the assignment-expression
11204 is not interpreted as a greater-than operator.)
11206 Returns a TREE_LIST representing the parameter-declaration. The
11207 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11208 there is no default argument. The TREE_VALUE is a representation
11209 of the decl-specifier-seq and declarator. In particular, the
11210 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11211 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11212 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11213 the declarator is of the form "(p)". */
11215 static tree
11216 cp_parser_parameter_declaration (cp_parser *parser,
11217 bool template_parm_p,
11218 bool *parenthesized_p)
11220 int declares_class_or_enum;
11221 bool greater_than_is_operator_p;
11222 tree decl_specifiers;
11223 tree attributes;
11224 tree declarator;
11225 tree default_argument;
11226 tree parameter;
11227 cp_token *token;
11228 const char *saved_message;
11230 /* In a template parameter, `>' is not an operator.
11232 [temp.param]
11234 When parsing a default template-argument for a non-type
11235 template-parameter, the first non-nested `>' is taken as the end
11236 of the template parameter-list rather than a greater-than
11237 operator. */
11238 greater_than_is_operator_p = !template_parm_p;
11240 /* Type definitions may not appear in parameter types. */
11241 saved_message = parser->type_definition_forbidden_message;
11242 parser->type_definition_forbidden_message
11243 = "types may not be defined in parameter types";
11245 /* Parse the declaration-specifiers. */
11246 decl_specifiers
11247 = cp_parser_decl_specifier_seq (parser,
11248 CP_PARSER_FLAGS_NONE,
11249 &attributes,
11250 &declares_class_or_enum);
11251 /* If an error occurred, there's no reason to attempt to parse the
11252 rest of the declaration. */
11253 if (cp_parser_error_occurred (parser))
11255 parser->type_definition_forbidden_message = saved_message;
11256 return error_mark_node;
11259 /* Peek at the next token. */
11260 token = cp_lexer_peek_token (parser->lexer);
11261 /* If the next token is a `)', `,', `=', `>', or `...', then there
11262 is no declarator. */
11263 if (token->type == CPP_CLOSE_PAREN
11264 || token->type == CPP_COMMA
11265 || token->type == CPP_EQ
11266 || token->type == CPP_ELLIPSIS
11267 || token->type == CPP_GREATER)
11269 declarator = NULL_TREE;
11270 if (parenthesized_p)
11271 *parenthesized_p = false;
11273 /* Otherwise, there should be a declarator. */
11274 else
11276 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11277 parser->default_arg_ok_p = false;
11279 /* After seeing a decl-specifier-seq, if the next token is not a
11280 "(", there is no possibility that the code is a valid
11281 expression. Therefore, if parsing tentatively, we commit at
11282 this point. */
11283 if (!parser->in_template_argument_list_p
11284 /* In an expression context, having seen:
11286 (int((char ...
11288 we cannot be sure whether we are looking at a
11289 function-type (taking a "char" as a parameter) or a cast
11290 of some object of type "char" to "int". */
11291 && !parser->in_type_id_in_expr_p
11292 && cp_parser_parsing_tentatively (parser)
11293 && !cp_parser_committed_to_tentative_parse (parser)
11294 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11295 cp_parser_commit_to_tentative_parse (parser);
11296 /* Parse the declarator. */
11297 declarator = cp_parser_declarator (parser,
11298 CP_PARSER_DECLARATOR_EITHER,
11299 /*ctor_dtor_or_conv_p=*/NULL,
11300 parenthesized_p,
11301 /*member_p=*/false);
11302 parser->default_arg_ok_p = saved_default_arg_ok_p;
11303 /* After the declarator, allow more attributes. */
11304 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11307 /* The restriction on defining new types applies only to the type
11308 of the parameter, not to the default argument. */
11309 parser->type_definition_forbidden_message = saved_message;
11311 /* If the next token is `=', then process a default argument. */
11312 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11314 bool saved_greater_than_is_operator_p;
11315 /* Consume the `='. */
11316 cp_lexer_consume_token (parser->lexer);
11318 /* If we are defining a class, then the tokens that make up the
11319 default argument must be saved and processed later. */
11320 if (!template_parm_p && at_class_scope_p ()
11321 && TYPE_BEING_DEFINED (current_class_type))
11323 unsigned depth = 0;
11325 /* Create a DEFAULT_ARG to represented the unparsed default
11326 argument. */
11327 default_argument = make_node (DEFAULT_ARG);
11328 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11330 /* Add tokens until we have processed the entire default
11331 argument. */
11332 while (true)
11334 bool done = false;
11335 cp_token *token;
11337 /* Peek at the next token. */
11338 token = cp_lexer_peek_token (parser->lexer);
11339 /* What we do depends on what token we have. */
11340 switch (token->type)
11342 /* In valid code, a default argument must be
11343 immediately followed by a `,' `)', or `...'. */
11344 case CPP_COMMA:
11345 case CPP_CLOSE_PAREN:
11346 case CPP_ELLIPSIS:
11347 /* If we run into a non-nested `;', `}', or `]',
11348 then the code is invalid -- but the default
11349 argument is certainly over. */
11350 case CPP_SEMICOLON:
11351 case CPP_CLOSE_BRACE:
11352 case CPP_CLOSE_SQUARE:
11353 if (depth == 0)
11354 done = true;
11355 /* Update DEPTH, if necessary. */
11356 else if (token->type == CPP_CLOSE_PAREN
11357 || token->type == CPP_CLOSE_BRACE
11358 || token->type == CPP_CLOSE_SQUARE)
11359 --depth;
11360 break;
11362 case CPP_OPEN_PAREN:
11363 case CPP_OPEN_SQUARE:
11364 case CPP_OPEN_BRACE:
11365 ++depth;
11366 break;
11368 case CPP_GREATER:
11369 /* If we see a non-nested `>', and `>' is not an
11370 operator, then it marks the end of the default
11371 argument. */
11372 if (!depth && !greater_than_is_operator_p)
11373 done = true;
11374 break;
11376 /* If we run out of tokens, issue an error message. */
11377 case CPP_EOF:
11378 error ("file ends in default argument");
11379 done = true;
11380 break;
11382 case CPP_NAME:
11383 case CPP_SCOPE:
11384 /* In these cases, we should look for template-ids.
11385 For example, if the default argument is
11386 `X<int, double>()', we need to do name lookup to
11387 figure out whether or not `X' is a template; if
11388 so, the `,' does not end the default argument.
11390 That is not yet done. */
11391 break;
11393 default:
11394 break;
11397 /* If we've reached the end, stop. */
11398 if (done)
11399 break;
11401 /* Add the token to the token block. */
11402 token = cp_lexer_consume_token (parser->lexer);
11403 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11404 token);
11407 /* Outside of a class definition, we can just parse the
11408 assignment-expression. */
11409 else
11411 bool saved_local_variables_forbidden_p;
11413 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11414 set correctly. */
11415 saved_greater_than_is_operator_p
11416 = parser->greater_than_is_operator_p;
11417 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11418 /* Local variable names (and the `this' keyword) may not
11419 appear in a default argument. */
11420 saved_local_variables_forbidden_p
11421 = parser->local_variables_forbidden_p;
11422 parser->local_variables_forbidden_p = true;
11423 /* Parse the assignment-expression. */
11424 default_argument = cp_parser_assignment_expression (parser);
11425 /* Restore saved state. */
11426 parser->greater_than_is_operator_p
11427 = saved_greater_than_is_operator_p;
11428 parser->local_variables_forbidden_p
11429 = saved_local_variables_forbidden_p;
11431 if (!parser->default_arg_ok_p)
11433 if (!flag_pedantic_errors)
11434 warning ("deprecated use of default argument for parameter of non-function");
11435 else
11437 error ("default arguments are only permitted for function parameters");
11438 default_argument = NULL_TREE;
11442 else
11443 default_argument = NULL_TREE;
11445 /* Create the representation of the parameter. */
11446 if (attributes)
11447 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11448 parameter = build_tree_list (default_argument,
11449 build_tree_list (decl_specifiers,
11450 declarator));
11452 return parameter;
11455 /* Parse a function-body.
11457 function-body:
11458 compound_statement */
11460 static void
11461 cp_parser_function_body (cp_parser *parser)
11463 cp_parser_compound_statement (parser, false);
11466 /* Parse a ctor-initializer-opt followed by a function-body. Return
11467 true if a ctor-initializer was present. */
11469 static bool
11470 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11472 tree body;
11473 bool ctor_initializer_p;
11475 /* Begin the function body. */
11476 body = begin_function_body ();
11477 /* Parse the optional ctor-initializer. */
11478 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11479 /* Parse the function-body. */
11480 cp_parser_function_body (parser);
11481 /* Finish the function body. */
11482 finish_function_body (body);
11484 return ctor_initializer_p;
11487 /* Parse an initializer.
11489 initializer:
11490 = initializer-clause
11491 ( expression-list )
11493 Returns a expression representing the initializer. If no
11494 initializer is present, NULL_TREE is returned.
11496 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11497 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11498 set to FALSE if there is no initializer present. If there is an
11499 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11500 is set to true; otherwise it is set to false. */
11502 static tree
11503 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11504 bool* non_constant_p)
11506 cp_token *token;
11507 tree init;
11509 /* Peek at the next token. */
11510 token = cp_lexer_peek_token (parser->lexer);
11512 /* Let our caller know whether or not this initializer was
11513 parenthesized. */
11514 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11515 /* Assume that the initializer is constant. */
11516 *non_constant_p = false;
11518 if (token->type == CPP_EQ)
11520 /* Consume the `='. */
11521 cp_lexer_consume_token (parser->lexer);
11522 /* Parse the initializer-clause. */
11523 init = cp_parser_initializer_clause (parser, non_constant_p);
11525 else if (token->type == CPP_OPEN_PAREN)
11526 init = cp_parser_parenthesized_expression_list (parser, false,
11527 non_constant_p);
11528 else
11530 /* Anything else is an error. */
11531 cp_parser_error (parser, "expected initializer");
11532 init = error_mark_node;
11535 return init;
11538 /* Parse an initializer-clause.
11540 initializer-clause:
11541 assignment-expression
11542 { initializer-list , [opt] }
11545 Returns an expression representing the initializer.
11547 If the `assignment-expression' production is used the value
11548 returned is simply a representation for the expression.
11550 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11551 the elements of the initializer-list (or NULL_TREE, if the last
11552 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11553 NULL_TREE. There is no way to detect whether or not the optional
11554 trailing `,' was provided. NON_CONSTANT_P is as for
11555 cp_parser_initializer. */
11557 static tree
11558 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11560 tree initializer = NULL_TREE;
11562 /* Assume the expression is constant. */
11563 *non_constant_p = false;
11565 /* If it is not a `{', then we are looking at an
11566 assignment-expression. */
11567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11569 /* Speed up common initializers (simply a literal). */
11570 cp_token* token = cp_lexer_peek_token (parser->lexer);
11571 cp_token* token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11573 if (token2->type == CPP_COMMA)
11574 switch (token->type)
11576 case CPP_CHAR:
11577 case CPP_WCHAR:
11578 case CPP_NUMBER:
11579 token = cp_lexer_consume_token (parser->lexer);
11580 initializer = token->value;
11581 break;
11583 case CPP_STRING:
11584 case CPP_WSTRING:
11585 token = cp_lexer_consume_token (parser->lexer);
11586 if (TREE_CHAIN (token->value))
11587 initializer = TREE_CHAIN (token->value);
11588 else
11589 initializer = token->value;
11590 break;
11592 default:
11593 break;
11596 /* Otherwise, fall back to the generic assignment expression. */
11597 if (!initializer)
11599 initializer
11600 = cp_parser_constant_expression (parser,
11601 /*allow_non_constant_p=*/true,
11602 non_constant_p);
11603 if (!*non_constant_p)
11604 initializer = fold_non_dependent_expr (initializer);
11607 else
11609 /* Consume the `{' token. */
11610 cp_lexer_consume_token (parser->lexer);
11611 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11612 initializer = make_node (CONSTRUCTOR);
11613 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11614 necessary, but check_initializer depends upon it, for
11615 now. */
11616 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11617 /* If it's not a `}', then there is a non-trivial initializer. */
11618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11620 /* Parse the initializer list. */
11621 CONSTRUCTOR_ELTS (initializer)
11622 = cp_parser_initializer_list (parser, non_constant_p);
11623 /* A trailing `,' token is allowed. */
11624 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11625 cp_lexer_consume_token (parser->lexer);
11627 /* Now, there should be a trailing `}'. */
11628 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11631 return initializer;
11634 /* Parse an initializer-list.
11636 initializer-list:
11637 initializer-clause
11638 initializer-list , initializer-clause
11640 GNU Extension:
11642 initializer-list:
11643 identifier : initializer-clause
11644 initializer-list, identifier : initializer-clause
11646 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11647 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11648 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11649 as for cp_parser_initializer. */
11651 static tree
11652 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11654 tree initializers = NULL_TREE;
11656 /* Assume all of the expressions are constant. */
11657 *non_constant_p = false;
11659 /* Parse the rest of the list. */
11660 while (true)
11662 cp_token *token;
11663 tree identifier;
11664 tree initializer;
11665 bool clause_non_constant_p;
11667 /* If the next token is an identifier and the following one is a
11668 colon, we are looking at the GNU designated-initializer
11669 syntax. */
11670 if (cp_parser_allow_gnu_extensions_p (parser)
11671 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11672 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11674 /* Consume the identifier. */
11675 identifier = cp_lexer_consume_token (parser->lexer)->value;
11676 /* Consume the `:'. */
11677 cp_lexer_consume_token (parser->lexer);
11679 else
11680 identifier = NULL_TREE;
11682 /* Parse the initializer. */
11683 initializer = cp_parser_initializer_clause (parser,
11684 &clause_non_constant_p);
11685 /* If any clause is non-constant, so is the entire initializer. */
11686 if (clause_non_constant_p)
11687 *non_constant_p = true;
11688 /* Add it to the list. */
11689 initializers = tree_cons (identifier, initializer, initializers);
11691 /* If the next token is not a comma, we have reached the end of
11692 the list. */
11693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11694 break;
11696 /* Peek at the next token. */
11697 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11698 /* If the next token is a `}', then we're still done. An
11699 initializer-clause can have a trailing `,' after the
11700 initializer-list and before the closing `}'. */
11701 if (token->type == CPP_CLOSE_BRACE)
11702 break;
11704 /* Consume the `,' token. */
11705 cp_lexer_consume_token (parser->lexer);
11708 /* The initializers were built up in reverse order, so we need to
11709 reverse them now. */
11710 return nreverse (initializers);
11713 /* Classes [gram.class] */
11715 /* Parse a class-name.
11717 class-name:
11718 identifier
11719 template-id
11721 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11722 to indicate that names looked up in dependent types should be
11723 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11724 keyword has been used to indicate that the name that appears next
11725 is a template. TYPE_P is true iff the next name should be treated
11726 as class-name, even if it is declared to be some other kind of name
11727 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11728 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11729 being defined in a class-head.
11731 Returns the TYPE_DECL representing the class. */
11733 static tree
11734 cp_parser_class_name (cp_parser *parser,
11735 bool typename_keyword_p,
11736 bool template_keyword_p,
11737 bool type_p,
11738 bool check_dependency_p,
11739 bool class_head_p,
11740 bool is_declaration)
11742 tree decl;
11743 tree scope;
11744 bool typename_p;
11745 cp_token *token;
11747 /* All class-names start with an identifier. */
11748 token = cp_lexer_peek_token (parser->lexer);
11749 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11751 cp_parser_error (parser, "expected class-name");
11752 return error_mark_node;
11755 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11756 to a template-id, so we save it here. */
11757 scope = parser->scope;
11758 if (scope == error_mark_node)
11759 return error_mark_node;
11761 /* Any name names a type if we're following the `typename' keyword
11762 in a qualified name where the enclosing scope is type-dependent. */
11763 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11764 && dependent_type_p (scope));
11765 /* Handle the common case (an identifier, but not a template-id)
11766 efficiently. */
11767 if (token->type == CPP_NAME
11768 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11770 tree identifier;
11772 /* Look for the identifier. */
11773 identifier = cp_parser_identifier (parser);
11774 /* If the next token isn't an identifier, we are certainly not
11775 looking at a class-name. */
11776 if (identifier == error_mark_node)
11777 decl = error_mark_node;
11778 /* If we know this is a type-name, there's no need to look it
11779 up. */
11780 else if (typename_p)
11781 decl = identifier;
11782 else
11784 /* If the next token is a `::', then the name must be a type
11785 name.
11787 [basic.lookup.qual]
11789 During the lookup for a name preceding the :: scope
11790 resolution operator, object, function, and enumerator
11791 names are ignored. */
11792 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11793 type_p = true;
11794 /* Look up the name. */
11795 decl = cp_parser_lookup_name (parser, identifier,
11796 type_p,
11797 /*is_template=*/false,
11798 /*is_namespace=*/false,
11799 check_dependency_p);
11802 else
11804 /* Try a template-id. */
11805 decl = cp_parser_template_id (parser, template_keyword_p,
11806 check_dependency_p,
11807 is_declaration);
11808 if (decl == error_mark_node)
11809 return error_mark_node;
11812 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11814 /* If this is a typename, create a TYPENAME_TYPE. */
11815 if (typename_p && decl != error_mark_node)
11817 decl = make_typename_type (scope, decl, /*complain=*/1);
11818 if (decl != error_mark_node)
11819 decl = TYPE_NAME (decl);
11822 /* Check to see that it is really the name of a class. */
11823 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11824 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11825 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11826 /* Situations like this:
11828 template <typename T> struct A {
11829 typename T::template X<int>::I i;
11832 are problematic. Is `T::template X<int>' a class-name? The
11833 standard does not seem to be definitive, but there is no other
11834 valid interpretation of the following `::'. Therefore, those
11835 names are considered class-names. */
11836 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11837 else if (decl == error_mark_node
11838 || TREE_CODE (decl) != TYPE_DECL
11839 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11841 cp_parser_error (parser, "expected class-name");
11842 return error_mark_node;
11845 return decl;
11848 /* Parse a class-specifier.
11850 class-specifier:
11851 class-head { member-specification [opt] }
11853 Returns the TREE_TYPE representing the class. */
11855 static tree
11856 cp_parser_class_specifier (cp_parser* parser)
11858 cp_token *token;
11859 tree type;
11860 tree attributes;
11861 int has_trailing_semicolon;
11862 bool nested_name_specifier_p;
11863 unsigned saved_num_template_parameter_lists;
11864 bool pop_p = false;
11865 tree scope = NULL_TREE;
11867 push_deferring_access_checks (dk_no_deferred);
11869 /* Parse the class-head. */
11870 type = cp_parser_class_head (parser,
11871 &nested_name_specifier_p,
11872 &attributes);
11873 /* If the class-head was a semantic disaster, skip the entire body
11874 of the class. */
11875 if (!type)
11877 cp_parser_skip_to_end_of_block_or_statement (parser);
11878 pop_deferring_access_checks ();
11879 return error_mark_node;
11882 /* Look for the `{'. */
11883 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11885 pop_deferring_access_checks ();
11886 return error_mark_node;
11889 /* Issue an error message if type-definitions are forbidden here. */
11890 cp_parser_check_type_definition (parser);
11891 /* Remember that we are defining one more class. */
11892 ++parser->num_classes_being_defined;
11893 /* Inside the class, surrounding template-parameter-lists do not
11894 apply. */
11895 saved_num_template_parameter_lists
11896 = parser->num_template_parameter_lists;
11897 parser->num_template_parameter_lists = 0;
11899 /* Start the class. */
11900 if (nested_name_specifier_p)
11902 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
11903 pop_p = push_scope (scope);
11905 type = begin_class_definition (type);
11906 if (type == error_mark_node)
11907 /* If the type is erroneous, skip the entire body of the class. */
11908 cp_parser_skip_to_closing_brace (parser);
11909 else
11910 /* Parse the member-specification. */
11911 cp_parser_member_specification_opt (parser);
11912 /* Look for the trailing `}'. */
11913 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11914 /* We get better error messages by noticing a common problem: a
11915 missing trailing `;'. */
11916 token = cp_lexer_peek_token (parser->lexer);
11917 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11918 /* Look for trailing attributes to apply to this class. */
11919 if (cp_parser_allow_gnu_extensions_p (parser))
11921 tree sub_attr = cp_parser_attributes_opt (parser);
11922 attributes = chainon (attributes, sub_attr);
11924 if (type != error_mark_node)
11925 type = finish_struct (type, attributes);
11926 if (pop_p)
11927 pop_scope (scope);
11928 /* If this class is not itself within the scope of another class,
11929 then we need to parse the bodies of all of the queued function
11930 definitions. Note that the queued functions defined in a class
11931 are not always processed immediately following the
11932 class-specifier for that class. Consider:
11934 struct A {
11935 struct B { void f() { sizeof (A); } };
11938 If `f' were processed before the processing of `A' were
11939 completed, there would be no way to compute the size of `A'.
11940 Note that the nesting we are interested in here is lexical --
11941 not the semantic nesting given by TYPE_CONTEXT. In particular,
11942 for:
11944 struct A { struct B; };
11945 struct A::B { void f() { } };
11947 there is no need to delay the parsing of `A::B::f'. */
11948 if (--parser->num_classes_being_defined == 0)
11950 tree queue_entry;
11951 tree fn;
11953 /* In a first pass, parse default arguments to the functions.
11954 Then, in a second pass, parse the bodies of the functions.
11955 This two-phased approach handles cases like:
11957 struct S {
11958 void f() { g(); }
11959 void g(int i = 3);
11963 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11964 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11965 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11966 TREE_PURPOSE (parser->unparsed_functions_queues)
11967 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11969 fn = TREE_VALUE (queue_entry);
11970 /* Make sure that any template parameters are in scope. */
11971 maybe_begin_member_template_processing (fn);
11972 /* If there are default arguments that have not yet been processed,
11973 take care of them now. */
11974 cp_parser_late_parsing_default_args (parser, fn);
11975 /* Remove any template parameters from the symbol table. */
11976 maybe_end_member_template_processing ();
11978 /* Now parse the body of the functions. */
11979 for (TREE_VALUE (parser->unparsed_functions_queues)
11980 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11981 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11982 TREE_VALUE (parser->unparsed_functions_queues)
11983 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11985 /* Figure out which function we need to process. */
11986 fn = TREE_VALUE (queue_entry);
11988 /* A hack to prevent garbage collection. */
11989 function_depth++;
11991 /* Parse the function. */
11992 cp_parser_late_parsing_for_member (parser, fn);
11993 function_depth--;
11998 /* Put back any saved access checks. */
11999 pop_deferring_access_checks ();
12001 /* Restore the count of active template-parameter-lists. */
12002 parser->num_template_parameter_lists
12003 = saved_num_template_parameter_lists;
12005 return type;
12008 /* Parse a class-head.
12010 class-head:
12011 class-key identifier [opt] base-clause [opt]
12012 class-key nested-name-specifier identifier base-clause [opt]
12013 class-key nested-name-specifier [opt] template-id
12014 base-clause [opt]
12016 GNU Extensions:
12017 class-key attributes identifier [opt] base-clause [opt]
12018 class-key attributes nested-name-specifier identifier base-clause [opt]
12019 class-key attributes nested-name-specifier [opt] template-id
12020 base-clause [opt]
12022 Returns the TYPE of the indicated class. Sets
12023 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12024 involving a nested-name-specifier was used, and FALSE otherwise.
12026 Returns NULL_TREE if the class-head is syntactically valid, but
12027 semantically invalid in a way that means we should skip the entire
12028 body of the class. */
12030 static tree
12031 cp_parser_class_head (cp_parser* parser,
12032 bool* nested_name_specifier_p,
12033 tree *attributes_p)
12035 cp_token *token;
12036 tree nested_name_specifier;
12037 enum tag_types class_key;
12038 tree id = NULL_TREE;
12039 tree type = NULL_TREE;
12040 tree attributes;
12041 bool template_id_p = false;
12042 bool qualified_p = false;
12043 bool invalid_nested_name_p = false;
12044 bool invalid_explicit_specialization_p = false;
12045 bool pop_p = false;
12046 unsigned num_templates;
12048 /* Assume no nested-name-specifier will be present. */
12049 *nested_name_specifier_p = false;
12050 /* Assume no template parameter lists will be used in defining the
12051 type. */
12052 num_templates = 0;
12054 /* Look for the class-key. */
12055 class_key = cp_parser_class_key (parser);
12056 if (class_key == none_type)
12057 return error_mark_node;
12059 /* Parse the attributes. */
12060 attributes = cp_parser_attributes_opt (parser);
12062 /* If the next token is `::', that is invalid -- but sometimes
12063 people do try to write:
12065 struct ::S {};
12067 Handle this gracefully by accepting the extra qualifier, and then
12068 issuing an error about it later if this really is a
12069 class-head. If it turns out just to be an elaborated type
12070 specifier, remain silent. */
12071 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12072 qualified_p = true;
12074 push_deferring_access_checks (dk_no_check);
12076 /* Determine the name of the class. Begin by looking for an
12077 optional nested-name-specifier. */
12078 nested_name_specifier
12079 = cp_parser_nested_name_specifier_opt (parser,
12080 /*typename_keyword_p=*/false,
12081 /*check_dependency_p=*/false,
12082 /*type_p=*/false,
12083 /*is_declaration=*/false);
12084 /* If there was a nested-name-specifier, then there *must* be an
12085 identifier. */
12086 if (nested_name_specifier)
12088 /* Although the grammar says `identifier', it really means
12089 `class-name' or `template-name'. You are only allowed to
12090 define a class that has already been declared with this
12091 syntax.
12093 The proposed resolution for Core Issue 180 says that whever
12094 you see `class T::X' you should treat `X' as a type-name.
12096 It is OK to define an inaccessible class; for example:
12098 class A { class B; };
12099 class A::B {};
12101 We do not know if we will see a class-name, or a
12102 template-name. We look for a class-name first, in case the
12103 class-name is a template-id; if we looked for the
12104 template-name first we would stop after the template-name. */
12105 cp_parser_parse_tentatively (parser);
12106 type = cp_parser_class_name (parser,
12107 /*typename_keyword_p=*/false,
12108 /*template_keyword_p=*/false,
12109 /*type_p=*/true,
12110 /*check_dependency_p=*/false,
12111 /*class_head_p=*/true,
12112 /*is_declaration=*/false);
12113 /* If that didn't work, ignore the nested-name-specifier. */
12114 if (!cp_parser_parse_definitely (parser))
12116 invalid_nested_name_p = true;
12117 id = cp_parser_identifier (parser);
12118 if (id == error_mark_node)
12119 id = NULL_TREE;
12121 /* If we could not find a corresponding TYPE, treat this
12122 declaration like an unqualified declaration. */
12123 if (type == error_mark_node)
12124 nested_name_specifier = NULL_TREE;
12125 /* Otherwise, count the number of templates used in TYPE and its
12126 containing scopes. */
12127 else
12129 tree scope;
12131 for (scope = TREE_TYPE (type);
12132 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12133 scope = (TYPE_P (scope)
12134 ? TYPE_CONTEXT (scope)
12135 : DECL_CONTEXT (scope)))
12136 if (TYPE_P (scope)
12137 && CLASS_TYPE_P (scope)
12138 && CLASSTYPE_TEMPLATE_INFO (scope)
12139 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12140 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12141 ++num_templates;
12144 /* Otherwise, the identifier is optional. */
12145 else
12147 /* We don't know whether what comes next is a template-id,
12148 an identifier, or nothing at all. */
12149 cp_parser_parse_tentatively (parser);
12150 /* Check for a template-id. */
12151 id = cp_parser_template_id (parser,
12152 /*template_keyword_p=*/false,
12153 /*check_dependency_p=*/true,
12154 /*is_declaration=*/true);
12155 /* If that didn't work, it could still be an identifier. */
12156 if (!cp_parser_parse_definitely (parser))
12158 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12159 id = cp_parser_identifier (parser);
12160 else
12161 id = NULL_TREE;
12163 else
12165 template_id_p = true;
12166 ++num_templates;
12170 pop_deferring_access_checks ();
12172 if (id)
12173 cp_parser_check_for_invalid_template_id (parser, id);
12175 /* If it's not a `:' or a `{' then we can't really be looking at a
12176 class-head, since a class-head only appears as part of a
12177 class-specifier. We have to detect this situation before calling
12178 xref_tag, since that has irreversible side-effects. */
12179 if (!cp_parser_next_token_starts_class_definition_p (parser))
12181 cp_parser_error (parser, "expected `{' or `:'");
12182 return error_mark_node;
12185 /* At this point, we're going ahead with the class-specifier, even
12186 if some other problem occurs. */
12187 cp_parser_commit_to_tentative_parse (parser);
12188 /* Issue the error about the overly-qualified name now. */
12189 if (qualified_p)
12190 cp_parser_error (parser,
12191 "global qualification of class name is invalid");
12192 else if (invalid_nested_name_p)
12193 cp_parser_error (parser,
12194 "qualified name does not name a class");
12195 else if (nested_name_specifier)
12197 tree scope;
12199 /* Reject typedef-names in class heads. */
12200 if (!DECL_IMPLICIT_TYPEDEF_P (type))
12202 error ("invalid class name in declaration of `%D'", type);
12203 type = NULL_TREE;
12204 goto done;
12207 /* Figure out in what scope the declaration is being placed. */
12208 scope = current_scope ();
12209 if (!scope)
12210 scope = current_namespace;
12211 /* If that scope does not contain the scope in which the
12212 class was originally declared, the program is invalid. */
12213 if (scope && !is_ancestor (scope, nested_name_specifier))
12215 error ("declaration of `%D' in `%D' which does not "
12216 "enclose `%D'", type, scope, nested_name_specifier);
12217 type = NULL_TREE;
12218 goto done;
12220 /* [dcl.meaning]
12222 A declarator-id shall not be qualified exception of the
12223 definition of a ... nested class outside of its class
12224 ... [or] a the definition or explicit instantiation of a
12225 class member of a namespace outside of its namespace. */
12226 if (scope == nested_name_specifier)
12228 pedwarn ("extra qualification ignored");
12229 nested_name_specifier = NULL_TREE;
12230 num_templates = 0;
12233 /* An explicit-specialization must be preceded by "template <>". If
12234 it is not, try to recover gracefully. */
12235 if (at_namespace_scope_p ()
12236 && parser->num_template_parameter_lists == 0
12237 && template_id_p)
12239 error ("an explicit specialization must be preceded by 'template <>'");
12240 invalid_explicit_specialization_p = true;
12241 /* Take the same action that would have been taken by
12242 cp_parser_explicit_specialization. */
12243 ++parser->num_template_parameter_lists;
12244 begin_specialization ();
12246 /* There must be no "return" statements between this point and the
12247 end of this function; set "type "to the correct return value and
12248 use "goto done;" to return. */
12249 /* Make sure that the right number of template parameters were
12250 present. */
12251 if (!cp_parser_check_template_parameters (parser, num_templates))
12253 /* If something went wrong, there is no point in even trying to
12254 process the class-definition. */
12255 type = NULL_TREE;
12256 goto done;
12259 /* Look up the type. */
12260 if (template_id_p)
12262 type = TREE_TYPE (id);
12263 maybe_process_partial_specialization (type);
12265 else if (!nested_name_specifier)
12267 /* If the class was unnamed, create a dummy name. */
12268 if (!id)
12269 id = make_anon_name ();
12270 type = xref_tag (class_key, id, /*globalize=*/false,
12271 parser->num_template_parameter_lists);
12273 else
12275 tree class_type;
12276 bool pop_p = false;
12278 /* Given:
12280 template <typename T> struct S { struct T };
12281 template <typename T> struct S<T>::T { };
12283 we will get a TYPENAME_TYPE when processing the definition of
12284 `S::T'. We need to resolve it to the actual type before we
12285 try to define it. */
12286 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12288 class_type = resolve_typename_type (TREE_TYPE (type),
12289 /*only_current_p=*/false);
12290 if (class_type != error_mark_node)
12291 type = TYPE_NAME (class_type);
12292 else
12294 cp_parser_error (parser, "could not resolve typename type");
12295 type = error_mark_node;
12299 maybe_process_partial_specialization (TREE_TYPE (type));
12300 class_type = current_class_type;
12301 /* Enter the scope indicated by the nested-name-specifier. */
12302 if (nested_name_specifier)
12303 pop_p = push_scope (nested_name_specifier);
12304 /* Get the canonical version of this type. */
12305 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12306 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12307 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12308 type = push_template_decl (type);
12309 type = TREE_TYPE (type);
12310 if (nested_name_specifier)
12312 *nested_name_specifier_p = true;
12313 if (pop_p)
12314 pop_scope (nested_name_specifier);
12317 /* Indicate whether this class was declared as a `class' or as a
12318 `struct'. */
12319 if (TREE_CODE (type) == RECORD_TYPE)
12320 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12321 cp_parser_check_class_key (class_key, type);
12323 /* Enter the scope containing the class; the names of base classes
12324 should be looked up in that context. For example, given:
12326 struct A { struct B {}; struct C; };
12327 struct A::C : B {};
12329 is valid. */
12330 if (nested_name_specifier)
12331 pop_p = push_scope (nested_name_specifier);
12332 /* Now, look for the base-clause. */
12333 token = cp_lexer_peek_token (parser->lexer);
12334 if (token->type == CPP_COLON)
12336 tree bases;
12338 /* Get the list of base-classes. */
12339 bases = cp_parser_base_clause (parser);
12340 /* Process them. */
12341 xref_basetypes (type, bases);
12343 /* Leave the scope given by the nested-name-specifier. We will
12344 enter the class scope itself while processing the members. */
12345 if (pop_p)
12346 pop_scope (nested_name_specifier);
12348 done:
12349 if (invalid_explicit_specialization_p)
12351 end_specialization ();
12352 --parser->num_template_parameter_lists;
12354 *attributes_p = attributes;
12355 return type;
12358 /* Parse a class-key.
12360 class-key:
12361 class
12362 struct
12363 union
12365 Returns the kind of class-key specified, or none_type to indicate
12366 error. */
12368 static enum tag_types
12369 cp_parser_class_key (cp_parser* parser)
12371 cp_token *token;
12372 enum tag_types tag_type;
12374 /* Look for the class-key. */
12375 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12376 if (!token)
12377 return none_type;
12379 /* Check to see if the TOKEN is a class-key. */
12380 tag_type = cp_parser_token_is_class_key (token);
12381 if (!tag_type)
12382 cp_parser_error (parser, "expected class-key");
12383 return tag_type;
12386 /* Parse an (optional) member-specification.
12388 member-specification:
12389 member-declaration member-specification [opt]
12390 access-specifier : member-specification [opt] */
12392 static void
12393 cp_parser_member_specification_opt (cp_parser* parser)
12395 while (true)
12397 cp_token *token;
12398 enum rid keyword;
12400 /* Peek at the next token. */
12401 token = cp_lexer_peek_token (parser->lexer);
12402 /* If it's a `}', or EOF then we've seen all the members. */
12403 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12404 break;
12406 /* See if this token is a keyword. */
12407 keyword = token->keyword;
12408 switch (keyword)
12410 case RID_PUBLIC:
12411 case RID_PROTECTED:
12412 case RID_PRIVATE:
12413 /* Consume the access-specifier. */
12414 cp_lexer_consume_token (parser->lexer);
12415 /* Remember which access-specifier is active. */
12416 current_access_specifier = token->value;
12417 /* Look for the `:'. */
12418 cp_parser_require (parser, CPP_COLON, "`:'");
12419 break;
12421 default:
12422 /* Otherwise, the next construction must be a
12423 member-declaration. */
12424 cp_parser_member_declaration (parser);
12429 /* Parse a member-declaration.
12431 member-declaration:
12432 decl-specifier-seq [opt] member-declarator-list [opt] ;
12433 function-definition ; [opt]
12434 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12435 using-declaration
12436 template-declaration
12438 member-declarator-list:
12439 member-declarator
12440 member-declarator-list , member-declarator
12442 member-declarator:
12443 declarator pure-specifier [opt]
12444 declarator constant-initializer [opt]
12445 identifier [opt] : constant-expression
12447 GNU Extensions:
12449 member-declaration:
12450 __extension__ member-declaration
12452 member-declarator:
12453 declarator attributes [opt] pure-specifier [opt]
12454 declarator attributes [opt] constant-initializer [opt]
12455 identifier [opt] attributes [opt] : constant-expression */
12457 static void
12458 cp_parser_member_declaration (cp_parser* parser)
12460 tree decl_specifiers;
12461 tree prefix_attributes;
12462 tree decl;
12463 int declares_class_or_enum;
12464 bool friend_p;
12465 cp_token *token;
12466 int saved_pedantic;
12468 /* Check for the `__extension__' keyword. */
12469 if (cp_parser_extension_opt (parser, &saved_pedantic))
12471 /* Recurse. */
12472 cp_parser_member_declaration (parser);
12473 /* Restore the old value of the PEDANTIC flag. */
12474 pedantic = saved_pedantic;
12476 return;
12479 /* Check for a template-declaration. */
12480 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12482 /* An explicit specialization here is an error condition, and we
12483 expect the specialization handler to detect and report this. */
12484 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12485 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12486 cp_parser_explicit_specialization (parser);
12487 else
12488 cp_parser_template_declaration (parser, /*member_p=*/true);
12490 return;
12493 /* Check for a using-declaration. */
12494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12496 /* Parse the using-declaration. */
12497 cp_parser_using_declaration (parser);
12499 return;
12502 /* Parse the decl-specifier-seq. */
12503 decl_specifiers
12504 = cp_parser_decl_specifier_seq (parser,
12505 CP_PARSER_FLAGS_OPTIONAL,
12506 &prefix_attributes,
12507 &declares_class_or_enum);
12508 /* Check for an invalid type-name. */
12509 if (cp_parser_diagnose_invalid_type_name (parser))
12510 return;
12511 /* If there is no declarator, then the decl-specifier-seq should
12512 specify a type. */
12513 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12515 /* If there was no decl-specifier-seq, and the next token is a
12516 `;', then we have something like:
12518 struct S { ; };
12520 [class.mem]
12522 Each member-declaration shall declare at least one member
12523 name of the class. */
12524 if (!decl_specifiers)
12526 if (pedantic)
12527 pedwarn ("extra semicolon");
12529 else
12531 tree type;
12533 /* See if this declaration is a friend. */
12534 friend_p = cp_parser_friend_p (decl_specifiers);
12535 /* If there were decl-specifiers, check to see if there was
12536 a class-declaration. */
12537 type = check_tag_decl (decl_specifiers);
12538 /* Nested classes have already been added to the class, but
12539 a `friend' needs to be explicitly registered. */
12540 if (friend_p)
12542 /* If the `friend' keyword was present, the friend must
12543 be introduced with a class-key. */
12544 if (!declares_class_or_enum)
12545 error ("a class-key must be used when declaring a friend");
12546 /* In this case:
12548 template <typename T> struct A {
12549 friend struct A<T>::B;
12552 A<T>::B will be represented by a TYPENAME_TYPE, and
12553 therefore not recognized by check_tag_decl. */
12554 if (!type)
12556 tree specifier;
12558 for (specifier = decl_specifiers;
12559 specifier;
12560 specifier = TREE_CHAIN (specifier))
12562 tree s = TREE_VALUE (specifier);
12564 if (TREE_CODE (s) == IDENTIFIER_NODE)
12565 get_global_value_if_present (s, &type);
12566 if (TREE_CODE (s) == TYPE_DECL)
12567 s = TREE_TYPE (s);
12568 if (TYPE_P (s))
12570 type = s;
12571 break;
12575 if (!type || !TYPE_P (type))
12576 error ("friend declaration does not name a class or "
12577 "function");
12578 else
12579 make_friend_class (current_class_type, type,
12580 /*complain=*/true);
12582 /* If there is no TYPE, an error message will already have
12583 been issued. */
12584 else if (!type)
12586 /* An anonymous aggregate has to be handled specially; such
12587 a declaration really declares a data member (with a
12588 particular type), as opposed to a nested class. */
12589 else if (ANON_AGGR_TYPE_P (type))
12591 /* Remove constructors and such from TYPE, now that we
12592 know it is an anonymous aggregate. */
12593 fixup_anonymous_aggr (type);
12594 /* And make the corresponding data member. */
12595 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12596 /* Add it to the class. */
12597 finish_member_declaration (decl);
12599 else
12600 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12603 else
12605 /* See if these declarations will be friends. */
12606 friend_p = cp_parser_friend_p (decl_specifiers);
12608 /* Keep going until we hit the `;' at the end of the
12609 declaration. */
12610 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12612 tree attributes = NULL_TREE;
12613 tree first_attribute;
12615 /* Peek at the next token. */
12616 token = cp_lexer_peek_token (parser->lexer);
12618 /* Check for a bitfield declaration. */
12619 if (token->type == CPP_COLON
12620 || (token->type == CPP_NAME
12621 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12622 == CPP_COLON))
12624 tree identifier;
12625 tree width;
12627 /* Get the name of the bitfield. Note that we cannot just
12628 check TOKEN here because it may have been invalidated by
12629 the call to cp_lexer_peek_nth_token above. */
12630 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12631 identifier = cp_parser_identifier (parser);
12632 else
12633 identifier = NULL_TREE;
12635 /* Consume the `:' token. */
12636 cp_lexer_consume_token (parser->lexer);
12637 /* Get the width of the bitfield. */
12638 width
12639 = cp_parser_constant_expression (parser,
12640 /*allow_non_constant=*/false,
12641 NULL);
12643 /* Look for attributes that apply to the bitfield. */
12644 attributes = cp_parser_attributes_opt (parser);
12645 /* Remember which attributes are prefix attributes and
12646 which are not. */
12647 first_attribute = attributes;
12648 /* Combine the attributes. */
12649 attributes = chainon (prefix_attributes, attributes);
12651 /* Create the bitfield declaration. */
12652 decl = grokbitfield (identifier,
12653 decl_specifiers,
12654 width);
12655 /* Apply the attributes. */
12656 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12658 else
12660 tree declarator;
12661 tree initializer;
12662 tree asm_specification;
12663 int ctor_dtor_or_conv_p;
12665 /* Parse the declarator. */
12666 declarator
12667 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12668 &ctor_dtor_or_conv_p,
12669 /*parenthesized_p=*/NULL,
12670 /*member_p=*/true);
12672 /* If something went wrong parsing the declarator, make sure
12673 that we at least consume some tokens. */
12674 if (declarator == error_mark_node)
12676 /* Skip to the end of the statement. */
12677 cp_parser_skip_to_end_of_statement (parser);
12678 /* If the next token is not a semicolon, that is
12679 probably because we just skipped over the body of
12680 a function. So, we consume a semicolon if
12681 present, but do not issue an error message if it
12682 is not present. */
12683 if (cp_lexer_next_token_is (parser->lexer,
12684 CPP_SEMICOLON))
12685 cp_lexer_consume_token (parser->lexer);
12686 return;
12689 if (declares_class_or_enum & 2)
12690 cp_parser_check_for_definition_in_return_type
12691 (declarator, TREE_VALUE (decl_specifiers));
12693 /* Look for an asm-specification. */
12694 asm_specification = cp_parser_asm_specification_opt (parser);
12695 /* Look for attributes that apply to the declaration. */
12696 attributes = cp_parser_attributes_opt (parser);
12697 /* Remember which attributes are prefix attributes and
12698 which are not. */
12699 first_attribute = attributes;
12700 /* Combine the attributes. */
12701 attributes = chainon (prefix_attributes, attributes);
12703 /* If it's an `=', then we have a constant-initializer or a
12704 pure-specifier. It is not correct to parse the
12705 initializer before registering the member declaration
12706 since the member declaration should be in scope while
12707 its initializer is processed. However, the rest of the
12708 front end does not yet provide an interface that allows
12709 us to handle this correctly. */
12710 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12712 /* In [class.mem]:
12714 A pure-specifier shall be used only in the declaration of
12715 a virtual function.
12717 A member-declarator can contain a constant-initializer
12718 only if it declares a static member of integral or
12719 enumeration type.
12721 Therefore, if the DECLARATOR is for a function, we look
12722 for a pure-specifier; otherwise, we look for a
12723 constant-initializer. When we call `grokfield', it will
12724 perform more stringent semantics checks. */
12725 if (TREE_CODE (declarator) == CALL_EXPR)
12726 initializer = cp_parser_pure_specifier (parser);
12727 else
12728 /* Parse the initializer. */
12729 initializer = cp_parser_constant_initializer (parser);
12731 /* Otherwise, there is no initializer. */
12732 else
12733 initializer = NULL_TREE;
12735 /* See if we are probably looking at a function
12736 definition. We are certainly not looking at at a
12737 member-declarator. Calling `grokfield' has
12738 side-effects, so we must not do it unless we are sure
12739 that we are looking at a member-declarator. */
12740 if (cp_parser_token_starts_function_definition_p
12741 (cp_lexer_peek_token (parser->lexer)))
12743 /* The grammar does not allow a pure-specifier to be
12744 used when a member function is defined. (It is
12745 possible that this fact is an oversight in the
12746 standard, since a pure function may be defined
12747 outside of the class-specifier. */
12748 if (initializer)
12749 error ("pure-specifier on function-definition");
12750 decl = cp_parser_save_member_function_body (parser,
12751 decl_specifiers,
12752 declarator,
12753 attributes);
12754 /* If the member was not a friend, declare it here. */
12755 if (!friend_p)
12756 finish_member_declaration (decl);
12757 /* Peek at the next token. */
12758 token = cp_lexer_peek_token (parser->lexer);
12759 /* If the next token is a semicolon, consume it. */
12760 if (token->type == CPP_SEMICOLON)
12761 cp_lexer_consume_token (parser->lexer);
12762 return;
12764 else
12766 /* Create the declaration. */
12767 decl = grokfield (declarator, decl_specifiers,
12768 initializer, asm_specification,
12769 attributes);
12770 /* Any initialization must have been from a
12771 constant-expression. */
12772 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12773 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12777 /* Reset PREFIX_ATTRIBUTES. */
12778 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12779 attributes = TREE_CHAIN (attributes);
12780 if (attributes)
12781 TREE_CHAIN (attributes) = NULL_TREE;
12783 /* If there is any qualification still in effect, clear it
12784 now; we will be starting fresh with the next declarator. */
12785 parser->scope = NULL_TREE;
12786 parser->qualifying_scope = NULL_TREE;
12787 parser->object_scope = NULL_TREE;
12788 /* If it's a `,', then there are more declarators. */
12789 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12790 cp_lexer_consume_token (parser->lexer);
12791 /* If the next token isn't a `;', then we have a parse error. */
12792 else if (cp_lexer_next_token_is_not (parser->lexer,
12793 CPP_SEMICOLON))
12795 cp_parser_error (parser, "expected `;'");
12796 /* Skip tokens until we find a `;'. */
12797 cp_parser_skip_to_end_of_statement (parser);
12799 break;
12802 if (decl)
12804 /* Add DECL to the list of members. */
12805 if (!friend_p)
12806 finish_member_declaration (decl);
12808 if (TREE_CODE (decl) == FUNCTION_DECL)
12809 cp_parser_save_default_args (parser, decl);
12814 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12817 /* Parse a pure-specifier.
12819 pure-specifier:
12822 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12823 Otherwise, ERROR_MARK_NODE is returned. */
12825 static tree
12826 cp_parser_pure_specifier (cp_parser* parser)
12828 cp_token *token;
12830 /* Look for the `=' token. */
12831 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12832 return error_mark_node;
12833 /* Look for the `0' token. */
12834 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12835 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12836 to get information from the lexer about how the number was
12837 spelled in order to fix this problem. */
12838 if (!token || !integer_zerop (token->value))
12839 return error_mark_node;
12841 return integer_zero_node;
12844 /* Parse a constant-initializer.
12846 constant-initializer:
12847 = constant-expression
12849 Returns a representation of the constant-expression. */
12851 static tree
12852 cp_parser_constant_initializer (cp_parser* parser)
12854 /* Look for the `=' token. */
12855 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12856 return error_mark_node;
12858 /* It is invalid to write:
12860 struct S { static const int i = { 7 }; };
12863 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12865 cp_parser_error (parser,
12866 "a brace-enclosed initializer is not allowed here");
12867 /* Consume the opening brace. */
12868 cp_lexer_consume_token (parser->lexer);
12869 /* Skip the initializer. */
12870 cp_parser_skip_to_closing_brace (parser);
12871 /* Look for the trailing `}'. */
12872 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12874 return error_mark_node;
12877 return cp_parser_constant_expression (parser,
12878 /*allow_non_constant=*/false,
12879 NULL);
12882 /* Derived classes [gram.class.derived] */
12884 /* Parse a base-clause.
12886 base-clause:
12887 : base-specifier-list
12889 base-specifier-list:
12890 base-specifier
12891 base-specifier-list , base-specifier
12893 Returns a TREE_LIST representing the base-classes, in the order in
12894 which they were declared. The representation of each node is as
12895 described by cp_parser_base_specifier.
12897 In the case that no bases are specified, this function will return
12898 NULL_TREE, not ERROR_MARK_NODE. */
12900 static tree
12901 cp_parser_base_clause (cp_parser* parser)
12903 tree bases = NULL_TREE;
12905 /* Look for the `:' that begins the list. */
12906 cp_parser_require (parser, CPP_COLON, "`:'");
12908 /* Scan the base-specifier-list. */
12909 while (true)
12911 cp_token *token;
12912 tree base;
12914 /* Look for the base-specifier. */
12915 base = cp_parser_base_specifier (parser);
12916 /* Add BASE to the front of the list. */
12917 if (base != error_mark_node)
12919 TREE_CHAIN (base) = bases;
12920 bases = base;
12922 /* Peek at the next token. */
12923 token = cp_lexer_peek_token (parser->lexer);
12924 /* If it's not a comma, then the list is complete. */
12925 if (token->type != CPP_COMMA)
12926 break;
12927 /* Consume the `,'. */
12928 cp_lexer_consume_token (parser->lexer);
12931 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12932 base class had a qualified name. However, the next name that
12933 appears is certainly not qualified. */
12934 parser->scope = NULL_TREE;
12935 parser->qualifying_scope = NULL_TREE;
12936 parser->object_scope = NULL_TREE;
12938 return nreverse (bases);
12941 /* Parse a base-specifier.
12943 base-specifier:
12944 :: [opt] nested-name-specifier [opt] class-name
12945 virtual access-specifier [opt] :: [opt] nested-name-specifier
12946 [opt] class-name
12947 access-specifier virtual [opt] :: [opt] nested-name-specifier
12948 [opt] class-name
12950 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12951 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12952 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12953 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12955 static tree
12956 cp_parser_base_specifier (cp_parser* parser)
12958 cp_token *token;
12959 bool done = false;
12960 bool virtual_p = false;
12961 bool duplicate_virtual_error_issued_p = false;
12962 bool duplicate_access_error_issued_p = false;
12963 bool class_scope_p, template_p;
12964 tree access = access_default_node;
12965 tree type;
12967 /* Process the optional `virtual' and `access-specifier'. */
12968 while (!done)
12970 /* Peek at the next token. */
12971 token = cp_lexer_peek_token (parser->lexer);
12972 /* Process `virtual'. */
12973 switch (token->keyword)
12975 case RID_VIRTUAL:
12976 /* If `virtual' appears more than once, issue an error. */
12977 if (virtual_p && !duplicate_virtual_error_issued_p)
12979 cp_parser_error (parser,
12980 "`virtual' specified more than once in base-specified");
12981 duplicate_virtual_error_issued_p = true;
12984 virtual_p = true;
12986 /* Consume the `virtual' token. */
12987 cp_lexer_consume_token (parser->lexer);
12989 break;
12991 case RID_PUBLIC:
12992 case RID_PROTECTED:
12993 case RID_PRIVATE:
12994 /* If more than one access specifier appears, issue an
12995 error. */
12996 if (access != access_default_node
12997 && !duplicate_access_error_issued_p)
12999 cp_parser_error (parser,
13000 "more than one access specifier in base-specified");
13001 duplicate_access_error_issued_p = true;
13004 access = ridpointers[(int) token->keyword];
13006 /* Consume the access-specifier. */
13007 cp_lexer_consume_token (parser->lexer);
13009 break;
13011 default:
13012 done = true;
13013 break;
13016 /* It is not uncommon to see programs mechanically, errouneously, use
13017 the 'typename' keyword to denote (dependent) qualified types
13018 as base classes. */
13019 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13021 if (!processing_template_decl)
13022 error ("keyword `typename' not allowed outside of templates");
13023 else
13024 error ("keyword `typename' not allowed in this context "
13025 "(the base class is implicitly a type)");
13026 cp_lexer_consume_token (parser->lexer);
13029 /* Look for the optional `::' operator. */
13030 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13031 /* Look for the nested-name-specifier. The simplest way to
13032 implement:
13034 [temp.res]
13036 The keyword `typename' is not permitted in a base-specifier or
13037 mem-initializer; in these contexts a qualified name that
13038 depends on a template-parameter is implicitly assumed to be a
13039 type name.
13041 is to pretend that we have seen the `typename' keyword at this
13042 point. */
13043 cp_parser_nested_name_specifier_opt (parser,
13044 /*typename_keyword_p=*/true,
13045 /*check_dependency_p=*/true,
13046 /*type_p=*/true,
13047 /*is_declaration=*/true);
13048 /* If the base class is given by a qualified name, assume that names
13049 we see are type names or templates, as appropriate. */
13050 class_scope_p = (parser->scope && TYPE_P (parser->scope));
13051 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13053 /* Finally, look for the class-name. */
13054 type = cp_parser_class_name (parser,
13055 class_scope_p,
13056 template_p,
13057 /*type_p=*/true,
13058 /*check_dependency_p=*/true,
13059 /*class_head_p=*/false,
13060 /*is_declaration=*/true);
13062 if (type == error_mark_node)
13063 return error_mark_node;
13065 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13068 /* Exception handling [gram.exception] */
13070 /* Parse an (optional) exception-specification.
13072 exception-specification:
13073 throw ( type-id-list [opt] )
13075 Returns a TREE_LIST representing the exception-specification. The
13076 TREE_VALUE of each node is a type. */
13078 static tree
13079 cp_parser_exception_specification_opt (cp_parser* parser)
13081 cp_token *token;
13082 tree type_id_list;
13084 /* Peek at the next token. */
13085 token = cp_lexer_peek_token (parser->lexer);
13086 /* If it's not `throw', then there's no exception-specification. */
13087 if (!cp_parser_is_keyword (token, RID_THROW))
13088 return NULL_TREE;
13090 /* Consume the `throw'. */
13091 cp_lexer_consume_token (parser->lexer);
13093 /* Look for the `('. */
13094 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13096 /* Peek at the next token. */
13097 token = cp_lexer_peek_token (parser->lexer);
13098 /* If it's not a `)', then there is a type-id-list. */
13099 if (token->type != CPP_CLOSE_PAREN)
13101 const char *saved_message;
13103 /* Types may not be defined in an exception-specification. */
13104 saved_message = parser->type_definition_forbidden_message;
13105 parser->type_definition_forbidden_message
13106 = "types may not be defined in an exception-specification";
13107 /* Parse the type-id-list. */
13108 type_id_list = cp_parser_type_id_list (parser);
13109 /* Restore the saved message. */
13110 parser->type_definition_forbidden_message = saved_message;
13112 else
13113 type_id_list = empty_except_spec;
13115 /* Look for the `)'. */
13116 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13118 return type_id_list;
13121 /* Parse an (optional) type-id-list.
13123 type-id-list:
13124 type-id
13125 type-id-list , type-id
13127 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13128 in the order that the types were presented. */
13130 static tree
13131 cp_parser_type_id_list (cp_parser* parser)
13133 tree types = NULL_TREE;
13135 while (true)
13137 cp_token *token;
13138 tree type;
13140 /* Get the next type-id. */
13141 type = cp_parser_type_id (parser);
13142 /* Add it to the list. */
13143 types = add_exception_specifier (types, type, /*complain=*/1);
13144 /* Peek at the next token. */
13145 token = cp_lexer_peek_token (parser->lexer);
13146 /* If it is not a `,', we are done. */
13147 if (token->type != CPP_COMMA)
13148 break;
13149 /* Consume the `,'. */
13150 cp_lexer_consume_token (parser->lexer);
13153 return nreverse (types);
13156 /* Parse a try-block.
13158 try-block:
13159 try compound-statement handler-seq */
13161 static tree
13162 cp_parser_try_block (cp_parser* parser)
13164 tree try_block;
13166 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13167 try_block = begin_try_block ();
13168 cp_parser_compound_statement (parser, false);
13169 finish_try_block (try_block);
13170 cp_parser_handler_seq (parser);
13171 finish_handler_sequence (try_block);
13173 return try_block;
13176 /* Parse a function-try-block.
13178 function-try-block:
13179 try ctor-initializer [opt] function-body handler-seq */
13181 static bool
13182 cp_parser_function_try_block (cp_parser* parser)
13184 tree try_block;
13185 bool ctor_initializer_p;
13187 /* Look for the `try' keyword. */
13188 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13189 return false;
13190 /* Let the rest of the front-end know where we are. */
13191 try_block = begin_function_try_block ();
13192 /* Parse the function-body. */
13193 ctor_initializer_p
13194 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13195 /* We're done with the `try' part. */
13196 finish_function_try_block (try_block);
13197 /* Parse the handlers. */
13198 cp_parser_handler_seq (parser);
13199 /* We're done with the handlers. */
13200 finish_function_handler_sequence (try_block);
13202 return ctor_initializer_p;
13205 /* Parse a handler-seq.
13207 handler-seq:
13208 handler handler-seq [opt] */
13210 static void
13211 cp_parser_handler_seq (cp_parser* parser)
13213 while (true)
13215 cp_token *token;
13217 /* Parse the handler. */
13218 cp_parser_handler (parser);
13219 /* Peek at the next token. */
13220 token = cp_lexer_peek_token (parser->lexer);
13221 /* If it's not `catch' then there are no more handlers. */
13222 if (!cp_parser_is_keyword (token, RID_CATCH))
13223 break;
13227 /* Parse a handler.
13229 handler:
13230 catch ( exception-declaration ) compound-statement */
13232 static void
13233 cp_parser_handler (cp_parser* parser)
13235 tree handler;
13236 tree declaration;
13238 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13239 handler = begin_handler ();
13240 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13241 declaration = cp_parser_exception_declaration (parser);
13242 finish_handler_parms (declaration, handler);
13243 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13244 cp_parser_compound_statement (parser, false);
13245 finish_handler (handler);
13248 /* Parse an exception-declaration.
13250 exception-declaration:
13251 type-specifier-seq declarator
13252 type-specifier-seq abstract-declarator
13253 type-specifier-seq
13254 ...
13256 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13257 ellipsis variant is used. */
13259 static tree
13260 cp_parser_exception_declaration (cp_parser* parser)
13262 tree type_specifiers;
13263 tree declarator;
13264 const char *saved_message;
13266 /* If it's an ellipsis, it's easy to handle. */
13267 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13269 /* Consume the `...' token. */
13270 cp_lexer_consume_token (parser->lexer);
13271 return NULL_TREE;
13274 /* Types may not be defined in exception-declarations. */
13275 saved_message = parser->type_definition_forbidden_message;
13276 parser->type_definition_forbidden_message
13277 = "types may not be defined in exception-declarations";
13279 /* Parse the type-specifier-seq. */
13280 type_specifiers = cp_parser_type_specifier_seq (parser);
13281 /* If it's a `)', then there is no declarator. */
13282 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13283 declarator = NULL_TREE;
13284 else
13285 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13286 /*ctor_dtor_or_conv_p=*/NULL,
13287 /*parenthesized_p=*/NULL,
13288 /*member_p=*/false);
13290 /* Restore the saved message. */
13291 parser->type_definition_forbidden_message = saved_message;
13293 return start_handler_parms (type_specifiers, declarator);
13296 /* Parse a throw-expression.
13298 throw-expression:
13299 throw assignment-expression [opt]
13301 Returns a THROW_EXPR representing the throw-expression. */
13303 static tree
13304 cp_parser_throw_expression (cp_parser* parser)
13306 tree expression;
13307 cp_token* token;
13309 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13310 token = cp_lexer_peek_token (parser->lexer);
13311 /* Figure out whether or not there is an assignment-expression
13312 following the "throw" keyword. */
13313 if (token->type == CPP_COMMA
13314 || token->type == CPP_SEMICOLON
13315 || token->type == CPP_CLOSE_PAREN
13316 || token->type == CPP_CLOSE_SQUARE
13317 || token->type == CPP_CLOSE_BRACE
13318 || token->type == CPP_COLON)
13319 expression = NULL_TREE;
13320 else
13321 expression = cp_parser_assignment_expression (parser);
13323 return build_throw (expression);
13326 /* GNU Extensions */
13328 /* Parse an (optional) asm-specification.
13330 asm-specification:
13331 asm ( string-literal )
13333 If the asm-specification is present, returns a STRING_CST
13334 corresponding to the string-literal. Otherwise, returns
13335 NULL_TREE. */
13337 static tree
13338 cp_parser_asm_specification_opt (cp_parser* parser)
13340 cp_token *token;
13341 tree asm_specification;
13343 /* Peek at the next token. */
13344 token = cp_lexer_peek_token (parser->lexer);
13345 /* If the next token isn't the `asm' keyword, then there's no
13346 asm-specification. */
13347 if (!cp_parser_is_keyword (token, RID_ASM))
13348 return NULL_TREE;
13350 /* Consume the `asm' token. */
13351 cp_lexer_consume_token (parser->lexer);
13352 /* Look for the `('. */
13353 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13355 /* Look for the string-literal. */
13356 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13357 if (token)
13358 asm_specification = token->value;
13359 else
13360 asm_specification = NULL_TREE;
13362 /* Look for the `)'. */
13363 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13365 return asm_specification;
13368 /* Parse an asm-operand-list.
13370 asm-operand-list:
13371 asm-operand
13372 asm-operand-list , asm-operand
13374 asm-operand:
13375 string-literal ( expression )
13376 [ string-literal ] string-literal ( expression )
13378 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13379 each node is the expression. The TREE_PURPOSE is itself a
13380 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13381 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13382 is a STRING_CST for the string literal before the parenthesis. */
13384 static tree
13385 cp_parser_asm_operand_list (cp_parser* parser)
13387 tree asm_operands = NULL_TREE;
13389 while (true)
13391 tree string_literal;
13392 tree expression;
13393 tree name;
13394 cp_token *token;
13396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13398 /* Consume the `[' token. */
13399 cp_lexer_consume_token (parser->lexer);
13400 /* Read the operand name. */
13401 name = cp_parser_identifier (parser);
13402 if (name != error_mark_node)
13403 name = build_string (IDENTIFIER_LENGTH (name),
13404 IDENTIFIER_POINTER (name));
13405 /* Look for the closing `]'. */
13406 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13408 else
13409 name = NULL_TREE;
13410 /* Look for the string-literal. */
13411 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13412 string_literal = token ? token->value : error_mark_node;
13413 /* Look for the `('. */
13414 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13415 /* Parse the expression. */
13416 expression = cp_parser_expression (parser);
13417 /* Look for the `)'. */
13418 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13419 /* Add this operand to the list. */
13420 asm_operands = tree_cons (build_tree_list (name, string_literal),
13421 expression,
13422 asm_operands);
13423 /* If the next token is not a `,', there are no more
13424 operands. */
13425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13426 break;
13427 /* Consume the `,'. */
13428 cp_lexer_consume_token (parser->lexer);
13431 return nreverse (asm_operands);
13434 /* Parse an asm-clobber-list.
13436 asm-clobber-list:
13437 string-literal
13438 asm-clobber-list , string-literal
13440 Returns a TREE_LIST, indicating the clobbers in the order that they
13441 appeared. The TREE_VALUE of each node is a STRING_CST. */
13443 static tree
13444 cp_parser_asm_clobber_list (cp_parser* parser)
13446 tree clobbers = NULL_TREE;
13448 while (true)
13450 cp_token *token;
13451 tree string_literal;
13453 /* Look for the string literal. */
13454 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13455 string_literal = token ? token->value : error_mark_node;
13456 /* Add it to the list. */
13457 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13458 /* If the next token is not a `,', then the list is
13459 complete. */
13460 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13461 break;
13462 /* Consume the `,' token. */
13463 cp_lexer_consume_token (parser->lexer);
13466 return clobbers;
13469 /* Parse an (optional) series of attributes.
13471 attributes:
13472 attributes attribute
13474 attribute:
13475 __attribute__ (( attribute-list [opt] ))
13477 The return value is as for cp_parser_attribute_list. */
13479 static tree
13480 cp_parser_attributes_opt (cp_parser* parser)
13482 tree attributes = NULL_TREE;
13484 while (true)
13486 cp_token *token;
13487 tree attribute_list;
13489 /* Peek at the next token. */
13490 token = cp_lexer_peek_token (parser->lexer);
13491 /* If it's not `__attribute__', then we're done. */
13492 if (token->keyword != RID_ATTRIBUTE)
13493 break;
13495 /* Consume the `__attribute__' keyword. */
13496 cp_lexer_consume_token (parser->lexer);
13497 /* Look for the two `(' tokens. */
13498 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13499 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13501 /* Peek at the next token. */
13502 token = cp_lexer_peek_token (parser->lexer);
13503 if (token->type != CPP_CLOSE_PAREN)
13504 /* Parse the attribute-list. */
13505 attribute_list = cp_parser_attribute_list (parser);
13506 else
13507 /* If the next token is a `)', then there is no attribute
13508 list. */
13509 attribute_list = NULL;
13511 /* Look for the two `)' tokens. */
13512 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13513 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13515 /* Add these new attributes to the list. */
13516 attributes = chainon (attributes, attribute_list);
13519 return attributes;
13522 /* Parse an attribute-list.
13524 attribute-list:
13525 attribute
13526 attribute-list , attribute
13528 attribute:
13529 identifier
13530 identifier ( identifier )
13531 identifier ( identifier , expression-list )
13532 identifier ( expression-list )
13534 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13535 TREE_PURPOSE of each node is the identifier indicating which
13536 attribute is in use. The TREE_VALUE represents the arguments, if
13537 any. */
13539 static tree
13540 cp_parser_attribute_list (cp_parser* parser)
13542 tree attribute_list = NULL_TREE;
13544 while (true)
13546 cp_token *token;
13547 tree identifier;
13548 tree attribute;
13550 /* Look for the identifier. We also allow keywords here; for
13551 example `__attribute__ ((const))' is legal. */
13552 token = cp_lexer_peek_token (parser->lexer);
13553 if (token->type == CPP_NAME
13554 || token->type == CPP_KEYWORD)
13556 /* Consume the token. */
13557 token = cp_lexer_consume_token (parser->lexer);
13559 /* Save away the identifier that indicates which attribute
13560 this is. */
13561 identifier = token->value;
13562 attribute = build_tree_list (identifier, NULL_TREE);
13564 /* Peek at the next token. */
13565 token = cp_lexer_peek_token (parser->lexer);
13566 /* If it's an `(', then parse the attribute arguments. */
13567 if (token->type == CPP_OPEN_PAREN)
13569 tree arguments;
13571 arguments = (cp_parser_parenthesized_expression_list
13572 (parser, true, /*non_constant_p=*/NULL));
13573 /* Save the identifier and arguments away. */
13574 TREE_VALUE (attribute) = arguments;
13577 /* Add this attribute to the list. */
13578 TREE_CHAIN (attribute) = attribute_list;
13579 attribute_list = attribute;
13581 /* Now, look for more attributes. */
13582 token = cp_lexer_peek_token (parser->lexer);
13584 /* Now, look for more attributes. If the next token isn't a
13585 `,', we're done. */
13586 if (token->type != CPP_COMMA)
13587 break;
13589 /* Consume the comma and keep going. */
13590 cp_lexer_consume_token (parser->lexer);
13593 /* We built up the list in reverse order. */
13594 return nreverse (attribute_list);
13597 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13598 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13599 current value of the PEDANTIC flag, regardless of whether or not
13600 the `__extension__' keyword is present. The caller is responsible
13601 for restoring the value of the PEDANTIC flag. */
13603 static bool
13604 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13606 /* Save the old value of the PEDANTIC flag. */
13607 *saved_pedantic = pedantic;
13609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13611 /* Consume the `__extension__' token. */
13612 cp_lexer_consume_token (parser->lexer);
13613 /* We're not being pedantic while the `__extension__' keyword is
13614 in effect. */
13615 pedantic = 0;
13617 return true;
13620 return false;
13623 /* Parse a label declaration.
13625 label-declaration:
13626 __label__ label-declarator-seq ;
13628 label-declarator-seq:
13629 identifier , label-declarator-seq
13630 identifier */
13632 static void
13633 cp_parser_label_declaration (cp_parser* parser)
13635 /* Look for the `__label__' keyword. */
13636 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13638 while (true)
13640 tree identifier;
13642 /* Look for an identifier. */
13643 identifier = cp_parser_identifier (parser);
13644 /* If we failed, stop. */
13645 if (identifier == error_mark_node)
13646 break;
13647 /* Declare it as a label. */
13648 finish_label_decl (identifier);
13649 /* If the next token is a `;', stop. */
13650 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13651 break;
13652 /* Look for the `,' separating the label declarations. */
13653 cp_parser_require (parser, CPP_COMMA, "`,'");
13656 /* Look for the final `;'. */
13657 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13660 /* Support Functions */
13662 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13663 NAME should have one of the representations used for an
13664 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13665 is returned. If PARSER->SCOPE is a dependent type, then a
13666 SCOPE_REF is returned.
13668 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13669 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13670 was formed. Abstractly, such entities should not be passed to this
13671 function, because they do not need to be looked up, but it is
13672 simpler to check for this special case here, rather than at the
13673 call-sites.
13675 In cases not explicitly covered above, this function returns a
13676 DECL, OVERLOAD, or baselink representing the result of the lookup.
13677 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13678 is returned.
13680 If IS_TYPE is TRUE, bindings that do not refer to types are
13681 ignored.
13683 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13684 ignored.
13686 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13687 are ignored.
13689 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13690 types. */
13692 static tree
13693 cp_parser_lookup_name (cp_parser *parser, tree name,
13694 bool is_type, bool is_template, bool is_namespace,
13695 bool check_dependency)
13697 int flags = 0;
13698 tree decl;
13699 tree object_type = parser->context->object_type;
13701 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13702 no longer valid. Note that if we are parsing tentatively, and
13703 the parse fails, OBJECT_TYPE will be automatically restored. */
13704 parser->context->object_type = NULL_TREE;
13706 if (name == error_mark_node)
13707 return error_mark_node;
13709 if (!cp_parser_parsing_tentatively (parser)
13710 || cp_parser_committed_to_tentative_parse (parser))
13711 flags |= LOOKUP_COMPLAIN;
13713 /* A template-id has already been resolved; there is no lookup to
13714 do. */
13715 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13716 return name;
13717 if (BASELINK_P (name))
13719 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13720 == TEMPLATE_ID_EXPR),
13721 20020909);
13722 return name;
13725 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13726 it should already have been checked to make sure that the name
13727 used matches the type being destroyed. */
13728 if (TREE_CODE (name) == BIT_NOT_EXPR)
13730 tree type;
13732 /* Figure out to which type this destructor applies. */
13733 if (parser->scope)
13734 type = parser->scope;
13735 else if (object_type)
13736 type = object_type;
13737 else
13738 type = current_class_type;
13739 /* If that's not a class type, there is no destructor. */
13740 if (!type || !CLASS_TYPE_P (type))
13741 return error_mark_node;
13742 if (!CLASSTYPE_DESTRUCTORS (type))
13743 return error_mark_node;
13744 /* If it was a class type, return the destructor. */
13745 return CLASSTYPE_DESTRUCTORS (type);
13748 /* By this point, the NAME should be an ordinary identifier. If
13749 the id-expression was a qualified name, the qualifying scope is
13750 stored in PARSER->SCOPE at this point. */
13751 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13752 20000619);
13754 /* Perform the lookup. */
13755 if (parser->scope)
13757 bool dependent_p;
13759 if (parser->scope == error_mark_node)
13760 return error_mark_node;
13762 /* If the SCOPE is dependent, the lookup must be deferred until
13763 the template is instantiated -- unless we are explicitly
13764 looking up names in uninstantiated templates. Even then, we
13765 cannot look up the name if the scope is not a class type; it
13766 might, for example, be a template type parameter. */
13767 dependent_p = (TYPE_P (parser->scope)
13768 && !(parser->in_declarator_p
13769 && currently_open_class (parser->scope))
13770 && dependent_type_p (parser->scope));
13771 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13772 && dependent_p)
13774 if (is_type)
13775 /* The resolution to Core Issue 180 says that `struct A::B'
13776 should be considered a type-name, even if `A' is
13777 dependent. */
13778 decl = TYPE_NAME (make_typename_type (parser->scope,
13779 name,
13780 /*complain=*/1));
13781 else if (is_template)
13782 decl = make_unbound_class_template (parser->scope,
13783 name,
13784 /*complain=*/1);
13785 else
13786 decl = build_nt (SCOPE_REF, parser->scope, name);
13788 else
13790 bool pop_p = false;
13792 /* If PARSER->SCOPE is a dependent type, then it must be a
13793 class type, and we must not be checking dependencies;
13794 otherwise, we would have processed this lookup above. So
13795 that PARSER->SCOPE is not considered a dependent base by
13796 lookup_member, we must enter the scope here. */
13797 if (dependent_p)
13798 pop_p = push_scope (parser->scope);
13799 /* If the PARSER->SCOPE is a a template specialization, it
13800 may be instantiated during name lookup. In that case,
13801 errors may be issued. Even if we rollback the current
13802 tentative parse, those errors are valid. */
13803 decl = lookup_qualified_name (parser->scope, name, is_type,
13804 /*complain=*/true);
13805 if (pop_p)
13806 pop_scope (parser->scope);
13808 parser->qualifying_scope = parser->scope;
13809 parser->object_scope = NULL_TREE;
13811 else if (object_type)
13813 tree object_decl = NULL_TREE;
13814 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13815 OBJECT_TYPE is not a class. */
13816 if (CLASS_TYPE_P (object_type))
13817 /* If the OBJECT_TYPE is a template specialization, it may
13818 be instantiated during name lookup. In that case, errors
13819 may be issued. Even if we rollback the current tentative
13820 parse, those errors are valid. */
13821 object_decl = lookup_member (object_type,
13822 name,
13823 /*protect=*/0, is_type);
13824 /* Look it up in the enclosing context, too. */
13825 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13826 is_namespace, flags);
13827 parser->object_scope = object_type;
13828 parser->qualifying_scope = NULL_TREE;
13829 if (object_decl)
13830 decl = object_decl;
13832 else
13834 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13835 is_namespace, flags);
13836 parser->qualifying_scope = NULL_TREE;
13837 parser->object_scope = NULL_TREE;
13840 /* If the lookup failed, let our caller know. */
13841 if (!decl
13842 || decl == error_mark_node
13843 || (TREE_CODE (decl) == FUNCTION_DECL
13844 && DECL_ANTICIPATED (decl)))
13845 return error_mark_node;
13847 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13848 if (TREE_CODE (decl) == TREE_LIST)
13850 /* The error message we have to print is too complicated for
13851 cp_parser_error, so we incorporate its actions directly. */
13852 if (!cp_parser_simulate_error (parser))
13854 error ("reference to `%D' is ambiguous", name);
13855 print_candidates (decl);
13857 return error_mark_node;
13860 my_friendly_assert (DECL_P (decl)
13861 || TREE_CODE (decl) == OVERLOAD
13862 || TREE_CODE (decl) == SCOPE_REF
13863 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13864 || BASELINK_P (decl),
13865 20000619);
13867 /* If we have resolved the name of a member declaration, check to
13868 see if the declaration is accessible. When the name resolves to
13869 set of overloaded functions, accessibility is checked when
13870 overload resolution is done.
13872 During an explicit instantiation, access is not checked at all,
13873 as per [temp.explicit]. */
13874 if (DECL_P (decl))
13875 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13877 return decl;
13880 /* Like cp_parser_lookup_name, but for use in the typical case where
13881 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13882 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13884 static tree
13885 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13887 return cp_parser_lookup_name (parser, name,
13888 /*is_type=*/false,
13889 /*is_template=*/false,
13890 /*is_namespace=*/false,
13891 /*check_dependency=*/true);
13894 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13895 the current context, return the TYPE_DECL. If TAG_NAME_P is
13896 true, the DECL indicates the class being defined in a class-head,
13897 or declared in an elaborated-type-specifier.
13899 Otherwise, return DECL. */
13901 static tree
13902 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13904 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13905 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13907 struct A {
13908 template <typename T> struct B;
13911 template <typename T> struct A::B {};
13913 Similarly, in a elaborated-type-specifier:
13915 namespace N { struct X{}; }
13917 struct A {
13918 template <typename T> friend struct N::X;
13921 However, if the DECL refers to a class type, and we are in
13922 the scope of the class, then the name lookup automatically
13923 finds the TYPE_DECL created by build_self_reference rather
13924 than a TEMPLATE_DECL. For example, in:
13926 template <class T> struct S {
13927 S s;
13930 there is no need to handle such case. */
13932 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13933 return DECL_TEMPLATE_RESULT (decl);
13935 return decl;
13938 /* If too many, or too few, template-parameter lists apply to the
13939 declarator, issue an error message. Returns TRUE if all went well,
13940 and FALSE otherwise. */
13942 static bool
13943 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13944 tree declarator)
13946 unsigned num_templates;
13948 /* We haven't seen any classes that involve template parameters yet. */
13949 num_templates = 0;
13951 switch (TREE_CODE (declarator))
13953 case CALL_EXPR:
13954 case ARRAY_REF:
13955 case INDIRECT_REF:
13956 case ADDR_EXPR:
13958 tree main_declarator = TREE_OPERAND (declarator, 0);
13959 return
13960 cp_parser_check_declarator_template_parameters (parser,
13961 main_declarator);
13964 case SCOPE_REF:
13966 tree scope;
13967 tree member;
13969 scope = TREE_OPERAND (declarator, 0);
13970 member = TREE_OPERAND (declarator, 1);
13972 /* If this is a pointer-to-member, then we are not interested
13973 in the SCOPE, because it does not qualify the thing that is
13974 being declared. */
13975 if (TREE_CODE (member) == INDIRECT_REF)
13976 return (cp_parser_check_declarator_template_parameters
13977 (parser, member));
13979 while (scope && CLASS_TYPE_P (scope))
13981 /* You're supposed to have one `template <...>'
13982 for every template class, but you don't need one
13983 for a full specialization. For example:
13985 template <class T> struct S{};
13986 template <> struct S<int> { void f(); };
13987 void S<int>::f () {}
13989 is correct; there shouldn't be a `template <>' for
13990 the definition of `S<int>::f'. */
13991 if (CLASSTYPE_TEMPLATE_INFO (scope)
13992 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13993 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13994 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13995 ++num_templates;
13997 scope = TYPE_CONTEXT (scope);
14001 /* Fall through. */
14003 default:
14004 /* If the DECLARATOR has the form `X<y>' then it uses one
14005 additional level of template parameters. */
14006 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
14007 ++num_templates;
14009 return cp_parser_check_template_parameters (parser,
14010 num_templates);
14014 /* NUM_TEMPLATES were used in the current declaration. If that is
14015 invalid, return FALSE and issue an error messages. Otherwise,
14016 return TRUE. */
14018 static bool
14019 cp_parser_check_template_parameters (cp_parser* parser,
14020 unsigned num_templates)
14022 /* If there are more template classes than parameter lists, we have
14023 something like:
14025 template <class T> void S<T>::R<T>::f (); */
14026 if (parser->num_template_parameter_lists < num_templates)
14028 error ("too few template-parameter-lists");
14029 return false;
14031 /* If there are the same number of template classes and parameter
14032 lists, that's OK. */
14033 if (parser->num_template_parameter_lists == num_templates)
14034 return true;
14035 /* If there are more, but only one more, then we are referring to a
14036 member template. That's OK too. */
14037 if (parser->num_template_parameter_lists == num_templates + 1)
14038 return true;
14039 /* Otherwise, there are too many template parameter lists. We have
14040 something like:
14042 template <class T> template <class U> void S::f(); */
14043 error ("too many template-parameter-lists");
14044 return false;
14047 /* Parse a binary-expression of the general form:
14049 binary-expression:
14050 <expr>
14051 binary-expression <token> <expr>
14053 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
14054 to parser the <expr>s. If the first production is used, then the
14055 value returned by FN is returned directly. Otherwise, a node with
14056 the indicated EXPR_TYPE is returned, with operands corresponding to
14057 the two sub-expressions. */
14059 static tree
14060 cp_parser_binary_expression (cp_parser* parser,
14061 const cp_parser_token_tree_map token_tree_map,
14062 cp_parser_expression_fn fn)
14064 tree lhs;
14066 /* Parse the first expression. */
14067 lhs = (*fn) (parser);
14068 /* Now, look for more expressions. */
14069 while (true)
14071 cp_token *token;
14072 const cp_parser_token_tree_map_node *map_node;
14073 tree rhs;
14075 /* Peek at the next token. */
14076 token = cp_lexer_peek_token (parser->lexer);
14077 /* If the token is `>', and that's not an operator at the
14078 moment, then we're done. */
14079 if (token->type == CPP_GREATER
14080 && !parser->greater_than_is_operator_p)
14081 break;
14082 /* If we find one of the tokens we want, build the corresponding
14083 tree representation. */
14084 for (map_node = token_tree_map;
14085 map_node->token_type != CPP_EOF;
14086 ++map_node)
14087 if (map_node->token_type == token->type)
14089 /* Assume that an overloaded operator will not be used. */
14090 bool overloaded_p = false;
14092 /* Consume the operator token. */
14093 cp_lexer_consume_token (parser->lexer);
14094 /* Parse the right-hand side of the expression. */
14095 rhs = (*fn) (parser);
14096 /* Build the binary tree node. */
14097 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14098 &overloaded_p);
14099 /* If the binary operator required the use of an
14100 overloaded operator, then this expression cannot be an
14101 integral constant-expression. An overloaded operator
14102 can be used even if both operands are otherwise
14103 permissible in an integral constant-expression if at
14104 least one of the operands is of enumeration type. */
14105 if (overloaded_p
14106 && (cp_parser_non_integral_constant_expression
14107 (parser, "calls to overloaded operators")))
14108 lhs = error_mark_node;
14109 break;
14112 /* If the token wasn't one of the ones we want, we're done. */
14113 if (map_node->token_type == CPP_EOF)
14114 break;
14117 return lhs;
14120 /* Parse an optional `::' token indicating that the following name is
14121 from the global namespace. If so, PARSER->SCOPE is set to the
14122 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14123 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14124 Returns the new value of PARSER->SCOPE, if the `::' token is
14125 present, and NULL_TREE otherwise. */
14127 static tree
14128 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14130 cp_token *token;
14132 /* Peek at the next token. */
14133 token = cp_lexer_peek_token (parser->lexer);
14134 /* If we're looking at a `::' token then we're starting from the
14135 global namespace, not our current location. */
14136 if (token->type == CPP_SCOPE)
14138 /* Consume the `::' token. */
14139 cp_lexer_consume_token (parser->lexer);
14140 /* Set the SCOPE so that we know where to start the lookup. */
14141 parser->scope = global_namespace;
14142 parser->qualifying_scope = global_namespace;
14143 parser->object_scope = NULL_TREE;
14145 return parser->scope;
14147 else if (!current_scope_valid_p)
14149 parser->scope = NULL_TREE;
14150 parser->qualifying_scope = NULL_TREE;
14151 parser->object_scope = NULL_TREE;
14154 return NULL_TREE;
14157 /* Returns TRUE if the upcoming token sequence is the start of a
14158 constructor declarator. If FRIEND_P is true, the declarator is
14159 preceded by the `friend' specifier. */
14161 static bool
14162 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14164 bool constructor_p;
14165 tree type_decl = NULL_TREE;
14166 bool nested_name_p;
14167 cp_token *next_token;
14169 /* The common case is that this is not a constructor declarator, so
14170 try to avoid doing lots of work if at all possible. It's not
14171 valid declare a constructor at function scope. */
14172 if (at_function_scope_p ())
14173 return false;
14174 /* And only certain tokens can begin a constructor declarator. */
14175 next_token = cp_lexer_peek_token (parser->lexer);
14176 if (next_token->type != CPP_NAME
14177 && next_token->type != CPP_SCOPE
14178 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14179 && next_token->type != CPP_TEMPLATE_ID)
14180 return false;
14182 /* Parse tentatively; we are going to roll back all of the tokens
14183 consumed here. */
14184 cp_parser_parse_tentatively (parser);
14185 /* Assume that we are looking at a constructor declarator. */
14186 constructor_p = true;
14188 /* Look for the optional `::' operator. */
14189 cp_parser_global_scope_opt (parser,
14190 /*current_scope_valid_p=*/false);
14191 /* Look for the nested-name-specifier. */
14192 nested_name_p
14193 = (cp_parser_nested_name_specifier_opt (parser,
14194 /*typename_keyword_p=*/false,
14195 /*check_dependency_p=*/false,
14196 /*type_p=*/false,
14197 /*is_declaration=*/false)
14198 != NULL_TREE);
14199 /* Outside of a class-specifier, there must be a
14200 nested-name-specifier. */
14201 if (!nested_name_p &&
14202 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14203 || friend_p))
14204 constructor_p = false;
14205 /* If we still think that this might be a constructor-declarator,
14206 look for a class-name. */
14207 if (constructor_p)
14209 /* If we have:
14211 template <typename T> struct S { S(); };
14212 template <typename T> S<T>::S ();
14214 we must recognize that the nested `S' names a class.
14215 Similarly, for:
14217 template <typename T> S<T>::S<T> ();
14219 we must recognize that the nested `S' names a template. */
14220 type_decl = cp_parser_class_name (parser,
14221 /*typename_keyword_p=*/false,
14222 /*template_keyword_p=*/false,
14223 /*type_p=*/false,
14224 /*check_dependency_p=*/false,
14225 /*class_head_p=*/false,
14226 /*is_declaration=*/false);
14227 /* If there was no class-name, then this is not a constructor. */
14228 constructor_p = !cp_parser_error_occurred (parser);
14231 /* If we're still considering a constructor, we have to see a `(',
14232 to begin the parameter-declaration-clause, followed by either a
14233 `)', an `...', or a decl-specifier. We need to check for a
14234 type-specifier to avoid being fooled into thinking that:
14236 S::S (f) (int);
14238 is a constructor. (It is actually a function named `f' that
14239 takes one parameter (of type `int') and returns a value of type
14240 `S::S'. */
14241 if (constructor_p
14242 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14245 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14246 /* A parameter declaration begins with a decl-specifier,
14247 which is either the "attribute" keyword, a storage class
14248 specifier, or (usually) a type-specifier. */
14249 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14250 && !cp_parser_storage_class_specifier_opt (parser))
14252 tree type;
14253 bool pop_p = false;
14254 unsigned saved_num_template_parameter_lists;
14256 /* Names appearing in the type-specifier should be looked up
14257 in the scope of the class. */
14258 if (current_class_type)
14259 type = NULL_TREE;
14260 else
14262 type = TREE_TYPE (type_decl);
14263 if (TREE_CODE (type) == TYPENAME_TYPE)
14265 type = resolve_typename_type (type,
14266 /*only_current_p=*/false);
14267 if (type == error_mark_node)
14269 cp_parser_abort_tentative_parse (parser);
14270 return false;
14273 pop_p = push_scope (type);
14276 /* Inside the constructor parameter list, surrounding
14277 template-parameter-lists do not apply. */
14278 saved_num_template_parameter_lists
14279 = parser->num_template_parameter_lists;
14280 parser->num_template_parameter_lists = 0;
14282 /* Look for the type-specifier. */
14283 cp_parser_type_specifier (parser,
14284 CP_PARSER_FLAGS_NONE,
14285 /*is_friend=*/false,
14286 /*is_declarator=*/true,
14287 /*declares_class_or_enum=*/NULL,
14288 /*is_cv_qualifier=*/NULL);
14290 parser->num_template_parameter_lists
14291 = saved_num_template_parameter_lists;
14293 /* Leave the scope of the class. */
14294 if (pop_p)
14295 pop_scope (type);
14297 constructor_p = !cp_parser_error_occurred (parser);
14300 else
14301 constructor_p = false;
14302 /* We did not really want to consume any tokens. */
14303 cp_parser_abort_tentative_parse (parser);
14305 return constructor_p;
14308 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14309 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14310 they must be performed once we are in the scope of the function.
14312 Returns the function defined. */
14314 static tree
14315 cp_parser_function_definition_from_specifiers_and_declarator
14316 (cp_parser* parser,
14317 tree decl_specifiers,
14318 tree attributes,
14319 tree declarator)
14321 tree fn;
14322 bool success_p;
14324 /* Begin the function-definition. */
14325 success_p = begin_function_definition (decl_specifiers,
14326 attributes,
14327 declarator);
14329 /* If there were names looked up in the decl-specifier-seq that we
14330 did not check, check them now. We must wait until we are in the
14331 scope of the function to perform the checks, since the function
14332 might be a friend. */
14333 perform_deferred_access_checks ();
14335 if (!success_p)
14337 /* If begin_function_definition didn't like the definition, skip
14338 the entire function. */
14339 error ("invalid function declaration");
14340 cp_parser_skip_to_end_of_block_or_statement (parser);
14341 fn = error_mark_node;
14343 else
14344 fn = cp_parser_function_definition_after_declarator (parser,
14345 /*inline_p=*/false);
14347 return fn;
14350 /* Parse the part of a function-definition that follows the
14351 declarator. INLINE_P is TRUE iff this function is an inline
14352 function defined with a class-specifier.
14354 Returns the function defined. */
14356 static tree
14357 cp_parser_function_definition_after_declarator (cp_parser* parser,
14358 bool inline_p)
14360 tree fn;
14361 bool ctor_initializer_p = false;
14362 bool saved_in_unbraced_linkage_specification_p;
14363 unsigned saved_num_template_parameter_lists;
14365 /* If the next token is `return', then the code may be trying to
14366 make use of the "named return value" extension that G++ used to
14367 support. */
14368 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14370 /* Consume the `return' keyword. */
14371 cp_lexer_consume_token (parser->lexer);
14372 /* Look for the identifier that indicates what value is to be
14373 returned. */
14374 cp_parser_identifier (parser);
14375 /* Issue an error message. */
14376 error ("named return values are no longer supported");
14377 /* Skip tokens until we reach the start of the function body. */
14378 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14379 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14380 cp_lexer_consume_token (parser->lexer);
14382 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14383 anything declared inside `f'. */
14384 saved_in_unbraced_linkage_specification_p
14385 = parser->in_unbraced_linkage_specification_p;
14386 parser->in_unbraced_linkage_specification_p = false;
14387 /* Inside the function, surrounding template-parameter-lists do not
14388 apply. */
14389 saved_num_template_parameter_lists
14390 = parser->num_template_parameter_lists;
14391 parser->num_template_parameter_lists = 0;
14392 /* If the next token is `try', then we are looking at a
14393 function-try-block. */
14394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14395 ctor_initializer_p = cp_parser_function_try_block (parser);
14396 /* A function-try-block includes the function-body, so we only do
14397 this next part if we're not processing a function-try-block. */
14398 else
14399 ctor_initializer_p
14400 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14402 /* Finish the function. */
14403 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14404 (inline_p ? 2 : 0));
14405 /* Generate code for it, if necessary. */
14406 expand_or_defer_fn (fn);
14407 /* Restore the saved values. */
14408 parser->in_unbraced_linkage_specification_p
14409 = saved_in_unbraced_linkage_specification_p;
14410 parser->num_template_parameter_lists
14411 = saved_num_template_parameter_lists;
14413 return fn;
14416 /* Parse a template-declaration, assuming that the `export' (and
14417 `extern') keywords, if present, has already been scanned. MEMBER_P
14418 is as for cp_parser_template_declaration. */
14420 static void
14421 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14423 tree decl = NULL_TREE;
14424 tree parameter_list;
14425 bool friend_p = false;
14427 /* Look for the `template' keyword. */
14428 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14429 return;
14431 /* And the `<'. */
14432 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14433 return;
14435 /* If the next token is `>', then we have an invalid
14436 specialization. Rather than complain about an invalid template
14437 parameter, issue an error message here. */
14438 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14440 cp_parser_error (parser, "invalid explicit specialization");
14441 begin_specialization ();
14442 parameter_list = NULL_TREE;
14444 else
14446 /* Parse the template parameters. */
14447 begin_template_parm_list ();
14448 parameter_list = cp_parser_template_parameter_list (parser);
14449 parameter_list = end_template_parm_list (parameter_list);
14452 /* Look for the `>'. */
14453 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14454 /* We just processed one more parameter list. */
14455 ++parser->num_template_parameter_lists;
14456 /* If the next token is `template', there are more template
14457 parameters. */
14458 if (cp_lexer_next_token_is_keyword (parser->lexer,
14459 RID_TEMPLATE))
14460 cp_parser_template_declaration_after_export (parser, member_p);
14461 else
14463 decl = cp_parser_single_declaration (parser,
14464 member_p,
14465 &friend_p);
14467 /* If this is a member template declaration, let the front
14468 end know. */
14469 if (member_p && !friend_p && decl)
14471 if (TREE_CODE (decl) == TYPE_DECL)
14472 cp_parser_check_access_in_redeclaration (decl);
14474 decl = finish_member_template_decl (decl);
14476 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14477 make_friend_class (current_class_type, TREE_TYPE (decl),
14478 /*complain=*/true);
14480 /* We are done with the current parameter list. */
14481 --parser->num_template_parameter_lists;
14483 /* Finish up. */
14484 finish_template_decl (parameter_list);
14486 /* Register member declarations. */
14487 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14488 finish_member_declaration (decl);
14490 /* If DECL is a function template, we must return to parse it later.
14491 (Even though there is no definition, there might be default
14492 arguments that need handling.) */
14493 if (member_p && decl
14494 && (TREE_CODE (decl) == FUNCTION_DECL
14495 || DECL_FUNCTION_TEMPLATE_P (decl)))
14496 TREE_VALUE (parser->unparsed_functions_queues)
14497 = tree_cons (NULL_TREE, decl,
14498 TREE_VALUE (parser->unparsed_functions_queues));
14501 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14502 `function-definition' sequence. MEMBER_P is true, this declaration
14503 appears in a class scope.
14505 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14506 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14508 static tree
14509 cp_parser_single_declaration (cp_parser* parser,
14510 bool member_p,
14511 bool* friend_p)
14513 int declares_class_or_enum;
14514 tree decl = NULL_TREE;
14515 tree decl_specifiers;
14516 tree attributes;
14517 bool function_definition_p = false;
14519 /* This function is only used when processing a template
14520 declaration. */
14521 if (innermost_scope_kind () != sk_template_parms
14522 && innermost_scope_kind () != sk_template_spec)
14523 abort ();
14525 /* Defer access checks until we know what is being declared. */
14526 push_deferring_access_checks (dk_deferred);
14528 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14529 alternative. */
14530 decl_specifiers
14531 = cp_parser_decl_specifier_seq (parser,
14532 CP_PARSER_FLAGS_OPTIONAL,
14533 &attributes,
14534 &declares_class_or_enum);
14535 if (friend_p)
14536 *friend_p = cp_parser_friend_p (decl_specifiers);
14538 /* There are no template typedefs. */
14539 if (cp_parser_typedef_p (decl_specifiers))
14541 error ("template declaration of `typedef'");
14542 decl = error_mark_node;
14545 /* Gather up the access checks that occurred the
14546 decl-specifier-seq. */
14547 stop_deferring_access_checks ();
14549 /* Check for the declaration of a template class. */
14550 if (declares_class_or_enum)
14552 if (cp_parser_declares_only_class_p (parser))
14554 decl = shadow_tag (decl_specifiers);
14555 if (decl)
14556 decl = TYPE_NAME (decl);
14557 else
14558 decl = error_mark_node;
14561 /* If it's not a template class, try for a template function. If
14562 the next token is a `;', then this declaration does not declare
14563 anything. But, if there were errors in the decl-specifiers, then
14564 the error might well have come from an attempted class-specifier.
14565 In that case, there's no need to warn about a missing declarator. */
14566 if (!decl
14567 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14568 || !value_member (error_mark_node, decl_specifiers)))
14569 decl = cp_parser_init_declarator (parser,
14570 decl_specifiers,
14571 attributes,
14572 /*function_definition_allowed_p=*/true,
14573 member_p,
14574 declares_class_or_enum,
14575 &function_definition_p);
14577 pop_deferring_access_checks ();
14579 /* Clear any current qualification; whatever comes next is the start
14580 of something new. */
14581 parser->scope = NULL_TREE;
14582 parser->qualifying_scope = NULL_TREE;
14583 parser->object_scope = NULL_TREE;
14584 /* Look for a trailing `;' after the declaration. */
14585 if (!function_definition_p
14586 && (decl == error_mark_node
14587 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
14588 cp_parser_skip_to_end_of_block_or_statement (parser);
14590 return decl;
14593 /* Parse a cast-expression that is not the operand of a unary "&". */
14595 static tree
14596 cp_parser_simple_cast_expression (cp_parser *parser)
14598 return cp_parser_cast_expression (parser, /*address_p=*/false);
14601 /* Parse a functional cast to TYPE. Returns an expression
14602 representing the cast. */
14604 static tree
14605 cp_parser_functional_cast (cp_parser* parser, tree type)
14607 tree expression_list;
14608 tree cast;
14610 expression_list
14611 = cp_parser_parenthesized_expression_list (parser, false,
14612 /*non_constant_p=*/NULL);
14614 cast = build_functional_cast (type, expression_list);
14615 /* [expr.const]/1: In an integral constant expression "only type
14616 conversions to integral or enumeration type can be used". */
14617 if (TREE_CODE (type) == TYPE_DECL)
14618 type = TREE_TYPE (type);
14619 if (cast != error_mark_node && !dependent_type_p (type)
14620 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
14622 if (cp_parser_non_integral_constant_expression
14623 (parser, "a call to a constructor"))
14624 return error_mark_node;
14626 return cast;
14629 /* Save the tokens that make up the body of a member function defined
14630 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14631 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14632 specifiers applied to the declaration. Returns the FUNCTION_DECL
14633 for the member function. */
14635 static tree
14636 cp_parser_save_member_function_body (cp_parser* parser,
14637 tree decl_specifiers,
14638 tree declarator,
14639 tree attributes)
14641 cp_token_cache *cache;
14642 tree fn;
14644 /* Create the function-declaration. */
14645 fn = start_method (decl_specifiers, declarator, attributes);
14646 /* If something went badly wrong, bail out now. */
14647 if (fn == error_mark_node)
14649 /* If there's a function-body, skip it. */
14650 if (cp_parser_token_starts_function_definition_p
14651 (cp_lexer_peek_token (parser->lexer)))
14652 cp_parser_skip_to_end_of_block_or_statement (parser);
14653 return error_mark_node;
14656 /* Remember it, if there default args to post process. */
14657 cp_parser_save_default_args (parser, fn);
14659 /* Create a token cache. */
14660 cache = cp_token_cache_new ();
14661 /* Save away the tokens that make up the body of the
14662 function. */
14663 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14664 /* Handle function try blocks. */
14665 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14666 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14668 /* Save away the inline definition; we will process it when the
14669 class is complete. */
14670 DECL_PENDING_INLINE_INFO (fn) = cache;
14671 DECL_PENDING_INLINE_P (fn) = 1;
14673 /* We need to know that this was defined in the class, so that
14674 friend templates are handled correctly. */
14675 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14677 /* We're done with the inline definition. */
14678 finish_method (fn);
14680 /* Add FN to the queue of functions to be parsed later. */
14681 TREE_VALUE (parser->unparsed_functions_queues)
14682 = tree_cons (NULL_TREE, fn,
14683 TREE_VALUE (parser->unparsed_functions_queues));
14685 return fn;
14688 /* Parse a template-argument-list, as well as the trailing ">" (but
14689 not the opening ">"). See cp_parser_template_argument_list for the
14690 return value. */
14692 static tree
14693 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14695 tree arguments;
14696 tree saved_scope;
14697 tree saved_qualifying_scope;
14698 tree saved_object_scope;
14699 bool saved_greater_than_is_operator_p;
14701 /* [temp.names]
14703 When parsing a template-id, the first non-nested `>' is taken as
14704 the end of the template-argument-list rather than a greater-than
14705 operator. */
14706 saved_greater_than_is_operator_p
14707 = parser->greater_than_is_operator_p;
14708 parser->greater_than_is_operator_p = false;
14709 /* Parsing the argument list may modify SCOPE, so we save it
14710 here. */
14711 saved_scope = parser->scope;
14712 saved_qualifying_scope = parser->qualifying_scope;
14713 saved_object_scope = parser->object_scope;
14714 /* Parse the template-argument-list itself. */
14715 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14716 arguments = NULL_TREE;
14717 else
14718 arguments = cp_parser_template_argument_list (parser);
14719 /* Look for the `>' that ends the template-argument-list. If we find
14720 a '>>' instead, it's probably just a typo. */
14721 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14723 if (!saved_greater_than_is_operator_p)
14725 /* If we're in a nested template argument list, the '>>' has to be
14726 a typo for '> >'. We emit the error message, but we continue
14727 parsing and we push a '>' as next token, so that the argument
14728 list will be parsed correctly.. */
14729 cp_token* token;
14730 error ("`>>' should be `> >' within a nested template argument list");
14731 token = cp_lexer_peek_token (parser->lexer);
14732 token->type = CPP_GREATER;
14734 else
14736 /* If this is not a nested template argument list, the '>>' is
14737 a typo for '>'. Emit an error message and continue. */
14738 error ("spurious `>>', use `>' to terminate a template argument list");
14739 cp_lexer_consume_token (parser->lexer);
14742 else
14743 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14744 /* The `>' token might be a greater-than operator again now. */
14745 parser->greater_than_is_operator_p
14746 = saved_greater_than_is_operator_p;
14747 /* Restore the SAVED_SCOPE. */
14748 parser->scope = saved_scope;
14749 parser->qualifying_scope = saved_qualifying_scope;
14750 parser->object_scope = saved_object_scope;
14752 return arguments;
14755 /* MEMBER_FUNCTION is a member function, or a friend. If default
14756 arguments, or the body of the function have not yet been parsed,
14757 parse them now. */
14759 static void
14760 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14762 cp_lexer *saved_lexer;
14764 /* If this member is a template, get the underlying
14765 FUNCTION_DECL. */
14766 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14767 member_function = DECL_TEMPLATE_RESULT (member_function);
14769 /* There should not be any class definitions in progress at this
14770 point; the bodies of members are only parsed outside of all class
14771 definitions. */
14772 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14773 /* While we're parsing the member functions we might encounter more
14774 classes. We want to handle them right away, but we don't want
14775 them getting mixed up with functions that are currently in the
14776 queue. */
14777 parser->unparsed_functions_queues
14778 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14780 /* Make sure that any template parameters are in scope. */
14781 maybe_begin_member_template_processing (member_function);
14783 /* If the body of the function has not yet been parsed, parse it
14784 now. */
14785 if (DECL_PENDING_INLINE_P (member_function))
14787 tree function_scope;
14788 cp_token_cache *tokens;
14790 /* The function is no longer pending; we are processing it. */
14791 tokens = DECL_PENDING_INLINE_INFO (member_function);
14792 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14793 DECL_PENDING_INLINE_P (member_function) = 0;
14795 /* If this is a local class, enter the scope of the containing
14796 function. */
14797 function_scope = current_function_decl;
14798 if (function_scope)
14799 push_function_context_to (function_scope);
14801 /* Save away the current lexer. */
14802 saved_lexer = parser->lexer;
14803 /* Make a new lexer to feed us the tokens saved for this function. */
14804 parser->lexer = cp_lexer_new_from_tokens (tokens);
14805 parser->lexer->next = saved_lexer;
14807 /* Set the current source position to be the location of the first
14808 token in the saved inline body. */
14809 cp_lexer_peek_token (parser->lexer);
14811 /* Let the front end know that we going to be defining this
14812 function. */
14813 start_function (NULL_TREE, member_function, NULL_TREE,
14814 SF_PRE_PARSED | SF_INCLASS_INLINE);
14816 /* Now, parse the body of the function. */
14817 cp_parser_function_definition_after_declarator (parser,
14818 /*inline_p=*/true);
14820 /* Leave the scope of the containing function. */
14821 if (function_scope)
14822 pop_function_context_from (function_scope);
14823 /* Restore the lexer. */
14824 parser->lexer = saved_lexer;
14827 /* Remove any template parameters from the symbol table. */
14828 maybe_end_member_template_processing ();
14830 /* Restore the queue. */
14831 parser->unparsed_functions_queues
14832 = TREE_CHAIN (parser->unparsed_functions_queues);
14835 /* If DECL contains any default args, remember it on the unparsed
14836 functions queue. */
14838 static void
14839 cp_parser_save_default_args (cp_parser* parser, tree decl)
14841 tree probe;
14843 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14844 probe;
14845 probe = TREE_CHAIN (probe))
14846 if (TREE_PURPOSE (probe))
14848 TREE_PURPOSE (parser->unparsed_functions_queues)
14849 = tree_cons (NULL_TREE, decl,
14850 TREE_PURPOSE (parser->unparsed_functions_queues));
14851 break;
14853 return;
14856 /* FN is a FUNCTION_DECL which may contains a parameter with an
14857 unparsed DEFAULT_ARG. Parse the default args now. */
14859 static void
14860 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14862 cp_lexer *saved_lexer;
14863 cp_token_cache *tokens;
14864 bool saved_local_variables_forbidden_p;
14865 tree parameters;
14867 /* While we're parsing the default args, we might (due to the
14868 statement expression extension) encounter more classes. We want
14869 to handle them right away, but we don't want them getting mixed
14870 up with default args that are currently in the queue. */
14871 parser->unparsed_functions_queues
14872 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14874 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14875 parameters;
14876 parameters = TREE_CHAIN (parameters))
14878 tree default_arg = TREE_PURPOSE (parameters);
14879 tree parsed_arg;
14881 if (!default_arg)
14882 continue;
14884 if (TREE_CODE (default_arg) != DEFAULT_ARG)
14885 /* This can happen for a friend declaration for a function
14886 already declared with default arguments. */
14887 continue;
14889 /* Save away the current lexer. */
14890 saved_lexer = parser->lexer;
14891 /* Create a new one, using the tokens we have saved. */
14892 tokens = DEFARG_TOKENS (default_arg);
14893 parser->lexer = cp_lexer_new_from_tokens (tokens);
14895 /* Set the current source position to be the location of the
14896 first token in the default argument. */
14897 cp_lexer_peek_token (parser->lexer);
14899 /* Local variable names (and the `this' keyword) may not appear
14900 in a default argument. */
14901 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14902 parser->local_variables_forbidden_p = true;
14904 /* Parse the assignment-expression. */
14905 if (DECL_FRIEND_CONTEXT (fn))
14906 push_nested_class (DECL_FRIEND_CONTEXT (fn));
14907 else if (DECL_CLASS_SCOPE_P (fn))
14908 push_nested_class (DECL_CONTEXT (fn));
14909 parsed_arg = cp_parser_assignment_expression (parser);
14910 if (DECL_FRIEND_CONTEXT (fn) || DECL_CLASS_SCOPE_P (fn))
14911 pop_nested_class ();
14913 TREE_PURPOSE (parameters) = parsed_arg;
14915 /* Update any instantiations we've already created. */
14916 for (default_arg = TREE_CHAIN (default_arg);
14917 default_arg;
14918 default_arg = TREE_CHAIN (default_arg))
14919 TREE_PURPOSE (TREE_PURPOSE (default_arg)) = parsed_arg;
14921 /* If the token stream has not been completely used up, then
14922 there was extra junk after the end of the default
14923 argument. */
14924 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14925 cp_parser_error (parser, "expected `,'");
14927 /* Restore saved state. */
14928 parser->lexer = saved_lexer;
14929 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14932 /* Make sure no default arg is missing. */
14933 check_default_args (fn);
14935 /* Restore the queue. */
14936 parser->unparsed_functions_queues
14937 = TREE_CHAIN (parser->unparsed_functions_queues);
14940 /* Parse the operand of `sizeof' (or a similar operator). Returns
14941 either a TYPE or an expression, depending on the form of the
14942 input. The KEYWORD indicates which kind of expression we have
14943 encountered. */
14945 static tree
14946 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14948 static const char *format;
14949 tree expr = NULL_TREE;
14950 const char *saved_message;
14951 bool saved_integral_constant_expression_p;
14953 /* Initialize FORMAT the first time we get here. */
14954 if (!format)
14955 format = "types may not be defined in `%s' expressions";
14957 /* Types cannot be defined in a `sizeof' expression. Save away the
14958 old message. */
14959 saved_message = parser->type_definition_forbidden_message;
14960 /* And create the new one. */
14961 parser->type_definition_forbidden_message
14962 = xmalloc (strlen (format)
14963 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14964 + 1 /* `\0' */);
14965 sprintf ((char *) parser->type_definition_forbidden_message,
14966 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14968 /* The restrictions on constant-expressions do not apply inside
14969 sizeof expressions. */
14970 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14971 parser->integral_constant_expression_p = false;
14973 /* Do not actually evaluate the expression. */
14974 ++skip_evaluation;
14975 /* If it's a `(', then we might be looking at the type-id
14976 construction. */
14977 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14979 tree type;
14980 bool saved_in_type_id_in_expr_p;
14982 /* We can't be sure yet whether we're looking at a type-id or an
14983 expression. */
14984 cp_parser_parse_tentatively (parser);
14985 /* Consume the `('. */
14986 cp_lexer_consume_token (parser->lexer);
14987 /* Parse the type-id. */
14988 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14989 parser->in_type_id_in_expr_p = true;
14990 type = cp_parser_type_id (parser);
14991 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14992 /* Now, look for the trailing `)'. */
14993 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14994 /* If all went well, then we're done. */
14995 if (cp_parser_parse_definitely (parser))
14997 /* Build a list of decl-specifiers; right now, we have only
14998 a single type-specifier. */
14999 type = build_tree_list (NULL_TREE,
15000 type);
15002 /* Call grokdeclarator to figure out what type this is. */
15003 expr = grokdeclarator (NULL_TREE,
15004 type,
15005 TYPENAME,
15006 /*initialized=*/0,
15007 /*attrlist=*/NULL);
15011 /* If the type-id production did not work out, then we must be
15012 looking at the unary-expression production. */
15013 if (!expr)
15014 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15015 /* Go back to evaluating expressions. */
15016 --skip_evaluation;
15018 /* Free the message we created. */
15019 free ((char *) parser->type_definition_forbidden_message);
15020 /* And restore the old one. */
15021 parser->type_definition_forbidden_message = saved_message;
15022 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15024 return expr;
15027 /* If the current declaration has no declarator, return true. */
15029 static bool
15030 cp_parser_declares_only_class_p (cp_parser *parser)
15032 /* If the next token is a `;' or a `,' then there is no
15033 declarator. */
15034 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15035 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15038 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15039 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
15041 static bool
15042 cp_parser_friend_p (tree decl_specifiers)
15044 while (decl_specifiers)
15046 /* See if this decl-specifier is `friend'. */
15047 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15048 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
15049 return true;
15051 /* Go on to the next decl-specifier. */
15052 decl_specifiers = TREE_CHAIN (decl_specifiers);
15055 return false;
15058 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15059 Returns TRUE iff `typedef' appears among the DECL_SPECIFIERS. */
15061 static bool
15062 cp_parser_typedef_p (tree decl_specifiers)
15064 while (decl_specifiers)
15066 /* See if this decl-specifier is `typedef'. */
15067 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15068 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_TYPEDEF)
15069 return true;
15071 /* Go on to the next decl-specifier. */
15072 decl_specifiers = TREE_CHAIN (decl_specifiers);
15075 return false;
15079 /* If the next token is of the indicated TYPE, consume it. Otherwise,
15080 issue an error message indicating that TOKEN_DESC was expected.
15082 Returns the token consumed, if the token had the appropriate type.
15083 Otherwise, returns NULL. */
15085 static cp_token *
15086 cp_parser_require (cp_parser* parser,
15087 enum cpp_ttype type,
15088 const char* token_desc)
15090 if (cp_lexer_next_token_is (parser->lexer, type))
15091 return cp_lexer_consume_token (parser->lexer);
15092 else
15094 /* Output the MESSAGE -- unless we're parsing tentatively. */
15095 if (!cp_parser_simulate_error (parser))
15097 char *message = concat ("expected ", token_desc, NULL);
15098 cp_parser_error (parser, message);
15099 free (message);
15101 return NULL;
15105 /* Like cp_parser_require, except that tokens will be skipped until
15106 the desired token is found. An error message is still produced if
15107 the next token is not as expected. */
15109 static void
15110 cp_parser_skip_until_found (cp_parser* parser,
15111 enum cpp_ttype type,
15112 const char* token_desc)
15114 cp_token *token;
15115 unsigned nesting_depth = 0;
15117 if (cp_parser_require (parser, type, token_desc))
15118 return;
15120 /* Skip tokens until the desired token is found. */
15121 while (true)
15123 /* Peek at the next token. */
15124 token = cp_lexer_peek_token (parser->lexer);
15125 /* If we've reached the token we want, consume it and
15126 stop. */
15127 if (token->type == type && !nesting_depth)
15129 cp_lexer_consume_token (parser->lexer);
15130 return;
15132 /* If we've run out of tokens, stop. */
15133 if (token->type == CPP_EOF)
15134 return;
15135 if (token->type == CPP_OPEN_BRACE
15136 || token->type == CPP_OPEN_PAREN
15137 || token->type == CPP_OPEN_SQUARE)
15138 ++nesting_depth;
15139 else if (token->type == CPP_CLOSE_BRACE
15140 || token->type == CPP_CLOSE_PAREN
15141 || token->type == CPP_CLOSE_SQUARE)
15143 if (nesting_depth-- == 0)
15144 return;
15146 /* Consume this token. */
15147 cp_lexer_consume_token (parser->lexer);
15151 /* If the next token is the indicated keyword, consume it. Otherwise,
15152 issue an error message indicating that TOKEN_DESC was expected.
15154 Returns the token consumed, if the token had the appropriate type.
15155 Otherwise, returns NULL. */
15157 static cp_token *
15158 cp_parser_require_keyword (cp_parser* parser,
15159 enum rid keyword,
15160 const char* token_desc)
15162 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15164 if (token && token->keyword != keyword)
15166 dyn_string_t error_msg;
15168 /* Format the error message. */
15169 error_msg = dyn_string_new (0);
15170 dyn_string_append_cstr (error_msg, "expected ");
15171 dyn_string_append_cstr (error_msg, token_desc);
15172 cp_parser_error (parser, error_msg->s);
15173 dyn_string_delete (error_msg);
15174 return NULL;
15177 return token;
15180 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15181 function-definition. */
15183 static bool
15184 cp_parser_token_starts_function_definition_p (cp_token* token)
15186 return (/* An ordinary function-body begins with an `{'. */
15187 token->type == CPP_OPEN_BRACE
15188 /* A ctor-initializer begins with a `:'. */
15189 || token->type == CPP_COLON
15190 /* A function-try-block begins with `try'. */
15191 || token->keyword == RID_TRY
15192 /* The named return value extension begins with `return'. */
15193 || token->keyword == RID_RETURN);
15196 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15197 definition. */
15199 static bool
15200 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15202 cp_token *token;
15204 token = cp_lexer_peek_token (parser->lexer);
15205 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15208 /* Returns TRUE iff the next token is the "," or ">" ending a
15209 template-argument. */
15211 static bool
15212 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15214 cp_token *token;
15216 token = cp_lexer_peek_token (parser->lexer);
15217 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15220 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15221 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15223 static bool
15224 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15225 size_t n)
15227 cp_token *token;
15229 token = cp_lexer_peek_nth_token (parser->lexer, n);
15230 if (token->type == CPP_LESS)
15231 return true;
15232 /* Check for the sequence `<::' in the original code. It would be lexed as
15233 `[:', where `[' is a digraph, and there is no whitespace before
15234 `:'. */
15235 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15237 cp_token *token2;
15238 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15239 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15240 return true;
15242 return false;
15245 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15246 or none_type otherwise. */
15248 static enum tag_types
15249 cp_parser_token_is_class_key (cp_token* token)
15251 switch (token->keyword)
15253 case RID_CLASS:
15254 return class_type;
15255 case RID_STRUCT:
15256 return record_type;
15257 case RID_UNION:
15258 return union_type;
15260 default:
15261 return none_type;
15265 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15267 static void
15268 cp_parser_check_class_key (enum tag_types class_key, tree type)
15270 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15271 pedwarn ("`%s' tag used in naming `%#T'",
15272 class_key == union_type ? "union"
15273 : class_key == record_type ? "struct" : "class",
15274 type);
15277 /* Issue an error message if DECL is redeclared with different
15278 access than its original declaration [class.access.spec/3].
15279 This applies to nested classes and nested class templates.
15280 [class.mem/1]. */
15282 static void cp_parser_check_access_in_redeclaration (tree decl)
15284 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15285 return;
15287 if ((TREE_PRIVATE (decl)
15288 != (current_access_specifier == access_private_node))
15289 || (TREE_PROTECTED (decl)
15290 != (current_access_specifier == access_protected_node)))
15291 error ("%D redeclared with different access", decl);
15294 /* Look for the `template' keyword, as a syntactic disambiguator.
15295 Return TRUE iff it is present, in which case it will be
15296 consumed. */
15298 static bool
15299 cp_parser_optional_template_keyword (cp_parser *parser)
15301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15303 /* The `template' keyword can only be used within templates;
15304 outside templates the parser can always figure out what is a
15305 template and what is not. */
15306 if (!processing_template_decl)
15308 error ("`template' (as a disambiguator) is only allowed "
15309 "within templates");
15310 /* If this part of the token stream is rescanned, the same
15311 error message would be generated. So, we purge the token
15312 from the stream. */
15313 cp_lexer_purge_token (parser->lexer);
15314 return false;
15316 else
15318 /* Consume the `template' keyword. */
15319 cp_lexer_consume_token (parser->lexer);
15320 return true;
15324 return false;
15327 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15328 set PARSER->SCOPE, and perform other related actions. */
15330 static void
15331 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15333 tree value;
15334 tree check;
15336 /* Get the stored value. */
15337 value = cp_lexer_consume_token (parser->lexer)->value;
15338 /* Perform any access checks that were deferred. */
15339 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15340 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15341 /* Set the scope from the stored value. */
15342 parser->scope = TREE_VALUE (value);
15343 parser->qualifying_scope = TREE_TYPE (value);
15344 parser->object_scope = NULL_TREE;
15347 /* Add tokens to CACHE until an non-nested END token appears. */
15349 static void
15350 cp_parser_cache_group (cp_parser *parser,
15351 cp_token_cache *cache,
15352 enum cpp_ttype end,
15353 unsigned depth)
15355 while (true)
15357 cp_token *token;
15359 /* Abort a parenthesized expression if we encounter a brace. */
15360 if ((end == CPP_CLOSE_PAREN || depth == 0)
15361 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15362 return;
15363 /* If we've reached the end of the file, stop. */
15364 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15365 return;
15366 /* Consume the next token. */
15367 token = cp_lexer_consume_token (parser->lexer);
15368 /* Add this token to the tokens we are saving. */
15369 cp_token_cache_push_token (cache, token);
15370 /* See if it starts a new group. */
15371 if (token->type == CPP_OPEN_BRACE)
15373 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15374 if (depth == 0)
15375 return;
15377 else if (token->type == CPP_OPEN_PAREN)
15378 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15379 else if (token->type == end)
15380 return;
15384 /* Begin parsing tentatively. We always save tokens while parsing
15385 tentatively so that if the tentative parsing fails we can restore the
15386 tokens. */
15388 static void
15389 cp_parser_parse_tentatively (cp_parser* parser)
15391 /* Enter a new parsing context. */
15392 parser->context = cp_parser_context_new (parser->context);
15393 /* Begin saving tokens. */
15394 cp_lexer_save_tokens (parser->lexer);
15395 /* In order to avoid repetitive access control error messages,
15396 access checks are queued up until we are no longer parsing
15397 tentatively. */
15398 push_deferring_access_checks (dk_deferred);
15401 /* Commit to the currently active tentative parse. */
15403 static void
15404 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15406 cp_parser_context *context;
15407 cp_lexer *lexer;
15409 /* Mark all of the levels as committed. */
15410 lexer = parser->lexer;
15411 for (context = parser->context; context->next; context = context->next)
15413 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15414 break;
15415 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15416 while (!cp_lexer_saving_tokens (lexer))
15417 lexer = lexer->next;
15418 cp_lexer_commit_tokens (lexer);
15422 /* Abort the currently active tentative parse. All consumed tokens
15423 will be rolled back, and no diagnostics will be issued. */
15425 static void
15426 cp_parser_abort_tentative_parse (cp_parser* parser)
15428 cp_parser_simulate_error (parser);
15429 /* Now, pretend that we want to see if the construct was
15430 successfully parsed. */
15431 cp_parser_parse_definitely (parser);
15434 /* Stop parsing tentatively. If a parse error has occurred, restore the
15435 token stream. Otherwise, commit to the tokens we have consumed.
15436 Returns true if no error occurred; false otherwise. */
15438 static bool
15439 cp_parser_parse_definitely (cp_parser* parser)
15441 bool error_occurred;
15442 cp_parser_context *context;
15444 /* Remember whether or not an error occurred, since we are about to
15445 destroy that information. */
15446 error_occurred = cp_parser_error_occurred (parser);
15447 /* Remove the topmost context from the stack. */
15448 context = parser->context;
15449 parser->context = context->next;
15450 /* If no parse errors occurred, commit to the tentative parse. */
15451 if (!error_occurred)
15453 /* Commit to the tokens read tentatively, unless that was
15454 already done. */
15455 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15456 cp_lexer_commit_tokens (parser->lexer);
15458 pop_to_parent_deferring_access_checks ();
15460 /* Otherwise, if errors occurred, roll back our state so that things
15461 are just as they were before we began the tentative parse. */
15462 else
15464 cp_lexer_rollback_tokens (parser->lexer);
15465 pop_deferring_access_checks ();
15467 /* Add the context to the front of the free list. */
15468 context->next = cp_parser_context_free_list;
15469 cp_parser_context_free_list = context;
15471 return !error_occurred;
15474 /* Returns true if we are parsing tentatively -- but have decided that
15475 we will stick with this tentative parse, even if errors occur. */
15477 static bool
15478 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15480 return (cp_parser_parsing_tentatively (parser)
15481 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15484 /* Returns nonzero iff an error has occurred during the most recent
15485 tentative parse. */
15487 static bool
15488 cp_parser_error_occurred (cp_parser* parser)
15490 return (cp_parser_parsing_tentatively (parser)
15491 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15494 /* Returns nonzero if GNU extensions are allowed. */
15496 static bool
15497 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15499 return parser->allow_gnu_extensions_p;
15504 /* The parser. */
15506 static GTY (()) cp_parser *the_parser;
15508 /* External interface. */
15510 /* Parse one entire translation unit. */
15512 void
15513 c_parse_file (void)
15515 bool error_occurred;
15517 the_parser = cp_parser_new ();
15518 push_deferring_access_checks (flag_access_control
15519 ? dk_no_deferred : dk_no_check);
15520 error_occurred = cp_parser_translation_unit (the_parser);
15521 the_parser = NULL;
15524 /* This variable must be provided by every front end. */
15526 int yydebug;
15528 #include "gt-cp-parser.h"