2003-08-26 Dan Nicolaescu <dann@ics.uci.edu>
[official-gcc.git] / gcc / cp / parser.c
blob735d592651d6f97e5e00d08252d343e3dab7f0e4
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
39 /* The lexer. */
41 /* Overview
42 --------
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
45 look-ahead.
47 Methodology
48 -----------
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
64 circular buffer. */
66 /* A C++ token. */
68 typedef struct cp_token GTY (())
70 /* The kind of token. */
71 enum cpp_ttype type : 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 enum rid keyword : 8;
75 /* The value associated with this token, if any. */
76 tree value;
77 /* The location at which this token was found. */
78 location_t location;
79 } cp_token;
81 /* The number of tokens in a single token block.
82 Computed so that cp_token_block fits in a 512B allocation unit. */
84 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
86 /* A group of tokens. These groups are chained together to store
87 large numbers of tokens. (For example, a token block is created
88 when the body of an inline member function is first encountered;
89 the tokens are processed later after the class definition is
90 complete.)
92 This somewhat ungainly data structure (as opposed to, say, a
93 variable-length array), is used due to constraints imposed by the
94 current garbage-collection methodology. If it is made more
95 flexible, we could perhaps simplify the data structures involved. */
97 typedef struct cp_token_block GTY (())
99 /* The tokens. */
100 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
101 /* The number of tokens in this block. */
102 size_t num_tokens;
103 /* The next token block in the chain. */
104 struct cp_token_block *next;
105 /* The previous block in the chain. */
106 struct cp_token_block *prev;
107 } cp_token_block;
109 typedef struct cp_token_cache GTY (())
111 /* The first block in the cache. NULL if there are no tokens in the
112 cache. */
113 cp_token_block *first;
114 /* The last block in the cache. NULL If there are no tokens in the
115 cache. */
116 cp_token_block *last;
117 } cp_token_cache;
119 /* Prototypes. */
121 static cp_token_cache *cp_token_cache_new
122 (void);
123 static void cp_token_cache_push_token
124 (cp_token_cache *, cp_token *);
126 /* Create a new cp_token_cache. */
128 static cp_token_cache *
129 cp_token_cache_new ()
131 return ggc_alloc_cleared (sizeof (cp_token_cache));
134 /* Add *TOKEN to *CACHE. */
136 static void
137 cp_token_cache_push_token (cp_token_cache *cache,
138 cp_token *token)
140 cp_token_block *b = cache->last;
142 /* See if we need to allocate a new token block. */
143 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
145 b = ggc_alloc_cleared (sizeof (cp_token_block));
146 b->prev = cache->last;
147 if (cache->last)
149 cache->last->next = b;
150 cache->last = b;
152 else
153 cache->first = cache->last = b;
155 /* Add this token to the current token block. */
156 b->tokens[b->num_tokens++] = *token;
159 /* The cp_lexer structure represents the C++ lexer. It is responsible
160 for managing the token stream from the preprocessor and supplying
161 it to the parser. */
163 typedef struct cp_lexer GTY (())
165 /* The memory allocated for the buffer. Never NULL. */
166 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
167 /* A pointer just past the end of the memory allocated for the buffer. */
168 cp_token * GTY ((skip (""))) buffer_end;
169 /* The first valid token in the buffer, or NULL if none. */
170 cp_token * GTY ((skip (""))) first_token;
171 /* The next available token. If NEXT_TOKEN is NULL, then there are
172 no more available tokens. */
173 cp_token * GTY ((skip (""))) next_token;
174 /* A pointer just past the last available token. If FIRST_TOKEN is
175 NULL, however, there are no available tokens, and then this
176 location is simply the place in which the next token read will be
177 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
178 When the LAST_TOKEN == BUFFER, then the last token is at the
179 highest memory address in the BUFFER. */
180 cp_token * GTY ((skip (""))) last_token;
182 /* A stack indicating positions at which cp_lexer_save_tokens was
183 called. The top entry is the most recent position at which we
184 began saving tokens. The entries are differences in token
185 position between FIRST_TOKEN and the first saved token.
187 If the stack is non-empty, we are saving tokens. When a token is
188 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
189 pointer will not. The token stream will be preserved so that it
190 can be reexamined later.
192 If the stack is empty, then we are not saving tokens. Whenever a
193 token is consumed, the FIRST_TOKEN pointer will be moved, and the
194 consumed token will be gone forever. */
195 varray_type saved_tokens;
197 /* The STRING_CST tokens encountered while processing the current
198 string literal. */
199 varray_type string_tokens;
201 /* True if we should obtain more tokens from the preprocessor; false
202 if we are processing a saved token cache. */
203 bool main_lexer_p;
205 /* True if we should output debugging information. */
206 bool debugging_p;
208 /* The next lexer in a linked list of lexers. */
209 struct cp_lexer *next;
210 } cp_lexer;
212 /* Prototypes. */
214 static cp_lexer *cp_lexer_new_main
215 (void);
216 static cp_lexer *cp_lexer_new_from_tokens
217 (struct cp_token_cache *);
218 static int cp_lexer_saving_tokens
219 (const cp_lexer *);
220 static cp_token *cp_lexer_next_token
221 (cp_lexer *, cp_token *);
222 static ptrdiff_t cp_lexer_token_difference
223 (cp_lexer *, cp_token *, cp_token *);
224 static cp_token *cp_lexer_read_token
225 (cp_lexer *);
226 static void cp_lexer_maybe_grow_buffer
227 (cp_lexer *);
228 static void cp_lexer_get_preprocessor_token
229 (cp_lexer *, cp_token *);
230 static cp_token *cp_lexer_peek_token
231 (cp_lexer *);
232 static cp_token *cp_lexer_peek_nth_token
233 (cp_lexer *, size_t);
234 static inline bool cp_lexer_next_token_is
235 (cp_lexer *, enum cpp_ttype);
236 static bool cp_lexer_next_token_is_not
237 (cp_lexer *, enum cpp_ttype);
238 static bool cp_lexer_next_token_is_keyword
239 (cp_lexer *, enum rid);
240 static cp_token *cp_lexer_consume_token
241 (cp_lexer *);
242 static void cp_lexer_purge_token
243 (cp_lexer *);
244 static void cp_lexer_purge_tokens_after
245 (cp_lexer *, cp_token *);
246 static void cp_lexer_save_tokens
247 (cp_lexer *);
248 static void cp_lexer_commit_tokens
249 (cp_lexer *);
250 static void cp_lexer_rollback_tokens
251 (cp_lexer *);
252 static inline void cp_lexer_set_source_position_from_token
253 (cp_lexer *, const cp_token *);
254 static void cp_lexer_print_token
255 (FILE *, cp_token *);
256 static inline bool cp_lexer_debugging_p
257 (cp_lexer *);
258 static void cp_lexer_start_debugging
259 (cp_lexer *) ATTRIBUTE_UNUSED;
260 static void cp_lexer_stop_debugging
261 (cp_lexer *) ATTRIBUTE_UNUSED;
263 /* Manifest constants. */
265 #define CP_TOKEN_BUFFER_SIZE 5
266 #define CP_SAVED_TOKENS_SIZE 5
268 /* A token type for keywords, as opposed to ordinary identifiers. */
269 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
271 /* A token type for template-ids. If a template-id is processed while
272 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
273 the value of the CPP_TEMPLATE_ID is whatever was returned by
274 cp_parser_template_id. */
275 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
277 /* A token type for nested-name-specifiers. If a
278 nested-name-specifier is processed while parsing tentatively, it is
279 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
280 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
281 cp_parser_nested_name_specifier_opt. */
282 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
284 /* A token type for tokens that are not tokens at all; these are used
285 to mark the end of a token block. */
286 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
288 /* Variables. */
290 /* The stream to which debugging output should be written. */
291 static FILE *cp_lexer_debug_stream;
293 /* Create a new main C++ lexer, the lexer that gets tokens from the
294 preprocessor. */
296 static cp_lexer *
297 cp_lexer_new_main (void)
299 cp_lexer *lexer;
300 cp_token first_token;
302 /* It's possible that lexing the first token will load a PCH file,
303 which is a GC collection point. So we have to grab the first
304 token before allocating any memory. */
305 cp_lexer_get_preprocessor_token (NULL, &first_token);
306 c_common_no_more_pch ();
308 /* Allocate the memory. */
309 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
311 /* Create the circular buffer. */
312 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
313 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
315 /* There is one token in the buffer. */
316 lexer->last_token = lexer->buffer + 1;
317 lexer->first_token = lexer->buffer;
318 lexer->next_token = lexer->buffer;
319 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
321 /* This lexer obtains more tokens by calling c_lex. */
322 lexer->main_lexer_p = true;
324 /* Create the SAVED_TOKENS stack. */
325 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
327 /* Create the STRINGS array. */
328 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
330 /* Assume we are not debugging. */
331 lexer->debugging_p = false;
333 return lexer;
336 /* Create a new lexer whose token stream is primed with the TOKENS.
337 When these tokens are exhausted, no new tokens will be read. */
339 static cp_lexer *
340 cp_lexer_new_from_tokens (cp_token_cache *tokens)
342 cp_lexer *lexer;
343 cp_token *token;
344 cp_token_block *block;
345 ptrdiff_t num_tokens;
347 /* Allocate the memory. */
348 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
350 /* Create a new buffer, appropriately sized. */
351 num_tokens = 0;
352 for (block = tokens->first; block != NULL; block = block->next)
353 num_tokens += block->num_tokens;
354 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
355 lexer->buffer_end = lexer->buffer + num_tokens;
357 /* Install the tokens. */
358 token = lexer->buffer;
359 for (block = tokens->first; block != NULL; block = block->next)
361 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
362 token += block->num_tokens;
365 /* The FIRST_TOKEN is the beginning of the buffer. */
366 lexer->first_token = lexer->buffer;
367 /* The next available token is also at the beginning of the buffer. */
368 lexer->next_token = lexer->buffer;
369 /* The buffer is full. */
370 lexer->last_token = lexer->first_token;
372 /* This lexer doesn't obtain more tokens. */
373 lexer->main_lexer_p = false;
375 /* Create the SAVED_TOKENS stack. */
376 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
378 /* Create the STRINGS array. */
379 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
381 /* Assume we are not debugging. */
382 lexer->debugging_p = false;
384 return lexer;
387 /* Returns nonzero if debugging information should be output. */
389 static inline bool
390 cp_lexer_debugging_p (cp_lexer *lexer)
392 return lexer->debugging_p;
395 /* Set the current source position from the information stored in
396 TOKEN. */
398 static inline void
399 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
400 const cp_token *token)
402 /* Ideally, the source position information would not be a global
403 variable, but it is. */
405 /* Update the line number. */
406 if (token->type != CPP_EOF)
407 input_location = token->location;
410 /* TOKEN points into the circular token buffer. Return a pointer to
411 the next token in the buffer. */
413 static inline cp_token *
414 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
416 token++;
417 if (token == lexer->buffer_end)
418 token = lexer->buffer;
419 return token;
422 /* nonzero if we are presently saving tokens. */
424 static int
425 cp_lexer_saving_tokens (const cp_lexer* lexer)
427 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
430 /* Return a pointer to the token that is N tokens beyond TOKEN in the
431 buffer. */
433 static cp_token *
434 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
436 token += n;
437 if (token >= lexer->buffer_end)
438 token = lexer->buffer + (token - lexer->buffer_end);
439 return token;
442 /* Returns the number of times that START would have to be incremented
443 to reach FINISH. If START and FINISH are the same, returns zero. */
445 static ptrdiff_t
446 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
448 if (finish >= start)
449 return finish - start;
450 else
451 return ((lexer->buffer_end - lexer->buffer)
452 - (start - finish));
455 /* Obtain another token from the C preprocessor and add it to the
456 token buffer. Returns the newly read token. */
458 static cp_token *
459 cp_lexer_read_token (cp_lexer* lexer)
461 cp_token *token;
463 /* Make sure there is room in the buffer. */
464 cp_lexer_maybe_grow_buffer (lexer);
466 /* If there weren't any tokens, then this one will be the first. */
467 if (!lexer->first_token)
468 lexer->first_token = lexer->last_token;
469 /* Similarly, if there were no available tokens, there is one now. */
470 if (!lexer->next_token)
471 lexer->next_token = lexer->last_token;
473 /* Figure out where we're going to store the new token. */
474 token = lexer->last_token;
476 /* Get a new token from the preprocessor. */
477 cp_lexer_get_preprocessor_token (lexer, token);
479 /* Increment LAST_TOKEN. */
480 lexer->last_token = cp_lexer_next_token (lexer, token);
482 /* Strings should have type `const char []'. Right now, we will
483 have an ARRAY_TYPE that is constant rather than an array of
484 constant elements.
485 FIXME: Make fix_string_type get this right in the first place. */
486 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
487 && flag_const_strings)
489 tree type;
491 /* Get the current type. It will be an ARRAY_TYPE. */
492 type = TREE_TYPE (token->value);
493 /* Use build_cplus_array_type to rebuild the array, thereby
494 getting the right type. */
495 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
496 /* Reset the type of the token. */
497 TREE_TYPE (token->value) = type;
500 return token;
503 /* If the circular buffer is full, make it bigger. */
505 static void
506 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
508 /* If the buffer is full, enlarge it. */
509 if (lexer->last_token == lexer->first_token)
511 cp_token *new_buffer;
512 cp_token *old_buffer;
513 cp_token *new_first_token;
514 ptrdiff_t buffer_length;
515 size_t num_tokens_to_copy;
517 /* Remember the current buffer pointer. It will become invalid,
518 but we will need to do pointer arithmetic involving this
519 value. */
520 old_buffer = lexer->buffer;
521 /* Compute the current buffer size. */
522 buffer_length = lexer->buffer_end - lexer->buffer;
523 /* Allocate a buffer twice as big. */
524 new_buffer = ggc_realloc (lexer->buffer,
525 2 * buffer_length * sizeof (cp_token));
527 /* Because the buffer is circular, logically consecutive tokens
528 are not necessarily placed consecutively in memory.
529 Therefore, we must keep move the tokens that were before
530 FIRST_TOKEN to the second half of the newly allocated
531 buffer. */
532 num_tokens_to_copy = (lexer->first_token - old_buffer);
533 memcpy (new_buffer + buffer_length,
534 new_buffer,
535 num_tokens_to_copy * sizeof (cp_token));
536 /* Clear the rest of the buffer. We never look at this storage,
537 but the garbage collector may. */
538 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
539 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
541 /* Now recompute all of the buffer pointers. */
542 new_first_token
543 = new_buffer + (lexer->first_token - old_buffer);
544 if (lexer->next_token != NULL)
546 ptrdiff_t next_token_delta;
548 if (lexer->next_token > lexer->first_token)
549 next_token_delta = lexer->next_token - lexer->first_token;
550 else
551 next_token_delta =
552 buffer_length - (lexer->first_token - lexer->next_token);
553 lexer->next_token = new_first_token + next_token_delta;
555 lexer->last_token = new_first_token + buffer_length;
556 lexer->buffer = new_buffer;
557 lexer->buffer_end = new_buffer + buffer_length * 2;
558 lexer->first_token = new_first_token;
562 /* Store the next token from the preprocessor in *TOKEN. */
564 static void
565 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
566 cp_token *token)
568 bool done;
570 /* If this not the main lexer, return a terminating CPP_EOF token. */
571 if (lexer != NULL && !lexer->main_lexer_p)
573 token->type = CPP_EOF;
574 token->location.line = 0;
575 token->location.file = NULL;
576 token->value = NULL_TREE;
577 token->keyword = RID_MAX;
579 return;
582 done = false;
583 /* Keep going until we get a token we like. */
584 while (!done)
586 /* Get a new token from the preprocessor. */
587 token->type = c_lex (&token->value);
588 /* Issue messages about tokens we cannot process. */
589 switch (token->type)
591 case CPP_ATSIGN:
592 case CPP_HASH:
593 case CPP_PASTE:
594 error ("invalid token");
595 break;
597 default:
598 /* This is a good token, so we exit the loop. */
599 done = true;
600 break;
603 /* Now we've got our token. */
604 token->location = input_location;
606 /* Check to see if this token is a keyword. */
607 if (token->type == CPP_NAME
608 && C_IS_RESERVED_WORD (token->value))
610 /* Mark this token as a keyword. */
611 token->type = CPP_KEYWORD;
612 /* Record which keyword. */
613 token->keyword = C_RID_CODE (token->value);
614 /* Update the value. Some keywords are mapped to particular
615 entities, rather than simply having the value of the
616 corresponding IDENTIFIER_NODE. For example, `__const' is
617 mapped to `const'. */
618 token->value = ridpointers[token->keyword];
620 else
621 token->keyword = RID_MAX;
624 /* Return a pointer to the next token in the token stream, but do not
625 consume it. */
627 static cp_token *
628 cp_lexer_peek_token (cp_lexer* lexer)
630 cp_token *token;
632 /* If there are no tokens, read one now. */
633 if (!lexer->next_token)
634 cp_lexer_read_token (lexer);
636 /* Provide debugging output. */
637 if (cp_lexer_debugging_p (lexer))
639 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
640 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
641 fprintf (cp_lexer_debug_stream, "\n");
644 token = lexer->next_token;
645 cp_lexer_set_source_position_from_token (lexer, token);
646 return token;
649 /* Return true if the next token has the indicated TYPE. */
651 static bool
652 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
654 cp_token *token;
656 /* Peek at the next token. */
657 token = cp_lexer_peek_token (lexer);
658 /* Check to see if it has the indicated TYPE. */
659 return token->type == type;
662 /* Return true if the next token does not have the indicated TYPE. */
664 static bool
665 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
667 return !cp_lexer_next_token_is (lexer, type);
670 /* Return true if the next token is the indicated KEYWORD. */
672 static bool
673 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
675 cp_token *token;
677 /* Peek at the next token. */
678 token = cp_lexer_peek_token (lexer);
679 /* Check to see if it is the indicated keyword. */
680 return token->keyword == keyword;
683 /* Return a pointer to the Nth token in the token stream. If N is 1,
684 then this is precisely equivalent to cp_lexer_peek_token. */
686 static cp_token *
687 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
689 cp_token *token;
691 /* N is 1-based, not zero-based. */
692 my_friendly_assert (n > 0, 20000224);
694 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
695 token = lexer->next_token;
696 /* If there are no tokens in the buffer, get one now. */
697 if (!token)
699 cp_lexer_read_token (lexer);
700 token = lexer->next_token;
703 /* Now, read tokens until we have enough. */
704 while (--n > 0)
706 /* Advance to the next token. */
707 token = cp_lexer_next_token (lexer, token);
708 /* If that's all the tokens we have, read a new one. */
709 if (token == lexer->last_token)
710 token = cp_lexer_read_token (lexer);
713 return token;
716 /* Consume the next token. The pointer returned is valid only until
717 another token is read. Callers should preserve copy the token
718 explicitly if they will need its value for a longer period of
719 time. */
721 static cp_token *
722 cp_lexer_consume_token (cp_lexer* lexer)
724 cp_token *token;
726 /* If there are no tokens, read one now. */
727 if (!lexer->next_token)
728 cp_lexer_read_token (lexer);
730 /* Remember the token we'll be returning. */
731 token = lexer->next_token;
733 /* Increment NEXT_TOKEN. */
734 lexer->next_token = cp_lexer_next_token (lexer,
735 lexer->next_token);
736 /* Check to see if we're all out of tokens. */
737 if (lexer->next_token == lexer->last_token)
738 lexer->next_token = NULL;
740 /* If we're not saving tokens, then move FIRST_TOKEN too. */
741 if (!cp_lexer_saving_tokens (lexer))
743 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
744 if (!lexer->next_token)
745 lexer->first_token = NULL;
746 else
747 lexer->first_token = lexer->next_token;
750 /* Provide debugging output. */
751 if (cp_lexer_debugging_p (lexer))
753 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
754 cp_lexer_print_token (cp_lexer_debug_stream, token);
755 fprintf (cp_lexer_debug_stream, "\n");
758 return token;
761 /* Permanently remove the next token from the token stream. There
762 must be a valid next token already; this token never reads
763 additional tokens from the preprocessor. */
765 static void
766 cp_lexer_purge_token (cp_lexer *lexer)
768 cp_token *token;
769 cp_token *next_token;
771 token = lexer->next_token;
772 while (true)
774 next_token = cp_lexer_next_token (lexer, token);
775 if (next_token == lexer->last_token)
776 break;
777 *token = *next_token;
778 token = next_token;
781 lexer->last_token = token;
782 /* The token purged may have been the only token remaining; if so,
783 clear NEXT_TOKEN. */
784 if (lexer->next_token == token)
785 lexer->next_token = NULL;
788 /* Permanently remove all tokens after TOKEN, up to, but not
789 including, the token that will be returned next by
790 cp_lexer_peek_token. */
792 static void
793 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
795 cp_token *peek;
796 cp_token *t1;
797 cp_token *t2;
799 if (lexer->next_token)
801 /* Copy the tokens that have not yet been read to the location
802 immediately following TOKEN. */
803 t1 = cp_lexer_next_token (lexer, token);
804 t2 = peek = cp_lexer_peek_token (lexer);
805 /* Move tokens into the vacant area between TOKEN and PEEK. */
806 while (t2 != lexer->last_token)
808 *t1 = *t2;
809 t1 = cp_lexer_next_token (lexer, t1);
810 t2 = cp_lexer_next_token (lexer, t2);
812 /* Now, the next available token is right after TOKEN. */
813 lexer->next_token = cp_lexer_next_token (lexer, token);
814 /* And the last token is wherever we ended up. */
815 lexer->last_token = t1;
817 else
819 /* There are no tokens in the buffer, so there is nothing to
820 copy. The last token in the buffer is TOKEN itself. */
821 lexer->last_token = cp_lexer_next_token (lexer, token);
825 /* Begin saving tokens. All tokens consumed after this point will be
826 preserved. */
828 static void
829 cp_lexer_save_tokens (cp_lexer* lexer)
831 /* Provide debugging output. */
832 if (cp_lexer_debugging_p (lexer))
833 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
835 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
836 restore the tokens if required. */
837 if (!lexer->next_token)
838 cp_lexer_read_token (lexer);
840 VARRAY_PUSH_INT (lexer->saved_tokens,
841 cp_lexer_token_difference (lexer,
842 lexer->first_token,
843 lexer->next_token));
846 /* Commit to the portion of the token stream most recently saved. */
848 static void
849 cp_lexer_commit_tokens (cp_lexer* lexer)
851 /* Provide debugging output. */
852 if (cp_lexer_debugging_p (lexer))
853 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
855 VARRAY_POP (lexer->saved_tokens);
858 /* Return all tokens saved since the last call to cp_lexer_save_tokens
859 to the token stream. Stop saving tokens. */
861 static void
862 cp_lexer_rollback_tokens (cp_lexer* lexer)
864 size_t delta;
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
870 /* Find the token that was the NEXT_TOKEN when we started saving
871 tokens. */
872 delta = VARRAY_TOP_INT(lexer->saved_tokens);
873 /* Make it the next token again now. */
874 lexer->next_token = cp_lexer_advance_token (lexer,
875 lexer->first_token,
876 delta);
877 /* It might be the case that there were no tokens when we started
878 saving tokens, but that there are some tokens now. */
879 if (!lexer->next_token && lexer->first_token)
880 lexer->next_token = lexer->first_token;
882 /* Stop saving tokens. */
883 VARRAY_POP (lexer->saved_tokens);
886 /* Print a representation of the TOKEN on the STREAM. */
888 static void
889 cp_lexer_print_token (FILE * stream, cp_token* token)
891 const char *token_type = NULL;
893 /* Figure out what kind of token this is. */
894 switch (token->type)
896 case CPP_EQ:
897 token_type = "EQ";
898 break;
900 case CPP_COMMA:
901 token_type = "COMMA";
902 break;
904 case CPP_OPEN_PAREN:
905 token_type = "OPEN_PAREN";
906 break;
908 case CPP_CLOSE_PAREN:
909 token_type = "CLOSE_PAREN";
910 break;
912 case CPP_OPEN_BRACE:
913 token_type = "OPEN_BRACE";
914 break;
916 case CPP_CLOSE_BRACE:
917 token_type = "CLOSE_BRACE";
918 break;
920 case CPP_SEMICOLON:
921 token_type = "SEMICOLON";
922 break;
924 case CPP_NAME:
925 token_type = "NAME";
926 break;
928 case CPP_EOF:
929 token_type = "EOF";
930 break;
932 case CPP_KEYWORD:
933 token_type = "keyword";
934 break;
936 /* This is not a token that we know how to handle yet. */
937 default:
938 break;
941 /* If we have a name for the token, print it out. Otherwise, we
942 simply give the numeric code. */
943 if (token_type)
944 fprintf (stream, "%s", token_type);
945 else
946 fprintf (stream, "%d", token->type);
947 /* And, for an identifier, print the identifier name. */
948 if (token->type == CPP_NAME
949 /* Some keywords have a value that is not an IDENTIFIER_NODE.
950 For example, `struct' is mapped to an INTEGER_CST. */
951 || (token->type == CPP_KEYWORD
952 && TREE_CODE (token->value) == IDENTIFIER_NODE))
953 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
956 /* Start emitting debugging information. */
958 static void
959 cp_lexer_start_debugging (cp_lexer* lexer)
961 ++lexer->debugging_p;
964 /* Stop emitting debugging information. */
966 static void
967 cp_lexer_stop_debugging (cp_lexer* lexer)
969 --lexer->debugging_p;
973 /* The parser. */
975 /* Overview
976 --------
978 A cp_parser parses the token stream as specified by the C++
979 grammar. Its job is purely parsing, not semantic analysis. For
980 example, the parser breaks the token stream into declarators,
981 expressions, statements, and other similar syntactic constructs.
982 It does not check that the types of the expressions on either side
983 of an assignment-statement are compatible, or that a function is
984 not declared with a parameter of type `void'.
986 The parser invokes routines elsewhere in the compiler to perform
987 semantic analysis and to build up the abstract syntax tree for the
988 code processed.
990 The parser (and the template instantiation code, which is, in a
991 way, a close relative of parsing) are the only parts of the
992 compiler that should be calling push_scope and pop_scope, or
993 related functions. The parser (and template instantiation code)
994 keeps track of what scope is presently active; everything else
995 should simply honor that. (The code that generates static
996 initializers may also need to set the scope, in order to check
997 access control correctly when emitting the initializers.)
999 Methodology
1000 -----------
1002 The parser is of the standard recursive-descent variety. Upcoming
1003 tokens in the token stream are examined in order to determine which
1004 production to use when parsing a non-terminal. Some C++ constructs
1005 require arbitrary look ahead to disambiguate. For example, it is
1006 impossible, in the general case, to tell whether a statement is an
1007 expression or declaration without scanning the entire statement.
1008 Therefore, the parser is capable of "parsing tentatively." When the
1009 parser is not sure what construct comes next, it enters this mode.
1010 Then, while we attempt to parse the construct, the parser queues up
1011 error messages, rather than issuing them immediately, and saves the
1012 tokens it consumes. If the construct is parsed successfully, the
1013 parser "commits", i.e., it issues any queued error messages and
1014 the tokens that were being preserved are permanently discarded.
1015 If, however, the construct is not parsed successfully, the parser
1016 rolls back its state completely so that it can resume parsing using
1017 a different alternative.
1019 Future Improvements
1020 -------------------
1022 The performance of the parser could probably be improved
1023 substantially. Some possible improvements include:
1025 - The expression parser recurses through the various levels of
1026 precedence as specified in the grammar, rather than using an
1027 operator-precedence technique. Therefore, parsing a simple
1028 identifier requires multiple recursive calls.
1030 - We could often eliminate the need to parse tentatively by
1031 looking ahead a little bit. In some places, this approach
1032 might not entirely eliminate the need to parse tentatively, but
1033 it might still speed up the average case. */
1035 /* Flags that are passed to some parsing functions. These values can
1036 be bitwise-ored together. */
1038 typedef enum cp_parser_flags
1040 /* No flags. */
1041 CP_PARSER_FLAGS_NONE = 0x0,
1042 /* The construct is optional. If it is not present, then no error
1043 should be issued. */
1044 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1045 /* When parsing a type-specifier, do not allow user-defined types. */
1046 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1047 } cp_parser_flags;
1049 /* The different kinds of declarators we want to parse. */
1051 typedef enum cp_parser_declarator_kind
1053 /* We want an abstract declartor. */
1054 CP_PARSER_DECLARATOR_ABSTRACT,
1055 /* We want a named declarator. */
1056 CP_PARSER_DECLARATOR_NAMED,
1057 /* We don't mind, but the name must be an unqualified-id */
1058 CP_PARSER_DECLARATOR_EITHER
1059 } cp_parser_declarator_kind;
1061 /* A mapping from a token type to a corresponding tree node type. */
1063 typedef struct cp_parser_token_tree_map_node
1065 /* The token type. */
1066 enum cpp_ttype token_type : 8;
1067 /* The corresponding tree code. */
1068 enum tree_code tree_type : 8;
1069 } cp_parser_token_tree_map_node;
1071 /* A complete map consists of several ordinary entries, followed by a
1072 terminator. The terminating entry has a token_type of CPP_EOF. */
1074 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1076 /* The status of a tentative parse. */
1078 typedef enum cp_parser_status_kind
1080 /* No errors have occurred. */
1081 CP_PARSER_STATUS_KIND_NO_ERROR,
1082 /* An error has occurred. */
1083 CP_PARSER_STATUS_KIND_ERROR,
1084 /* We are committed to this tentative parse, whether or not an error
1085 has occurred. */
1086 CP_PARSER_STATUS_KIND_COMMITTED
1087 } cp_parser_status_kind;
1089 /* Context that is saved and restored when parsing tentatively. */
1091 typedef struct cp_parser_context GTY (())
1093 /* If this is a tentative parsing context, the status of the
1094 tentative parse. */
1095 enum cp_parser_status_kind status;
1096 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1097 that are looked up in this context must be looked up both in the
1098 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1099 the context of the containing expression. */
1100 tree object_type;
1101 /* The next parsing context in the stack. */
1102 struct cp_parser_context *next;
1103 } cp_parser_context;
1105 /* Prototypes. */
1107 /* Constructors and destructors. */
1109 static cp_parser_context *cp_parser_context_new
1110 (cp_parser_context *);
1112 /* Class variables. */
1114 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1116 /* Constructors and destructors. */
1118 /* Construct a new context. The context below this one on the stack
1119 is given by NEXT. */
1121 static cp_parser_context *
1122 cp_parser_context_new (cp_parser_context* next)
1124 cp_parser_context *context;
1126 /* Allocate the storage. */
1127 if (cp_parser_context_free_list != NULL)
1129 /* Pull the first entry from the free list. */
1130 context = cp_parser_context_free_list;
1131 cp_parser_context_free_list = context->next;
1132 memset (context, 0, sizeof (*context));
1134 else
1135 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1136 /* No errors have occurred yet in this context. */
1137 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1138 /* If this is not the bottomost context, copy information that we
1139 need from the previous context. */
1140 if (next)
1142 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1143 expression, then we are parsing one in this context, too. */
1144 context->object_type = next->object_type;
1145 /* Thread the stack. */
1146 context->next = next;
1149 return context;
1152 /* The cp_parser structure represents the C++ parser. */
1154 typedef struct cp_parser GTY(())
1156 /* The lexer from which we are obtaining tokens. */
1157 cp_lexer *lexer;
1159 /* The scope in which names should be looked up. If NULL_TREE, then
1160 we look up names in the scope that is currently open in the
1161 source program. If non-NULL, this is either a TYPE or
1162 NAMESPACE_DECL for the scope in which we should look.
1164 This value is not cleared automatically after a name is looked
1165 up, so we must be careful to clear it before starting a new look
1166 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1167 will look up `Z' in the scope of `X', rather than the current
1168 scope.) Unfortunately, it is difficult to tell when name lookup
1169 is complete, because we sometimes peek at a token, look it up,
1170 and then decide not to consume it. */
1171 tree scope;
1173 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1174 last lookup took place. OBJECT_SCOPE is used if an expression
1175 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1176 respectively. QUALIFYING_SCOPE is used for an expression of the
1177 form "X::Y"; it refers to X. */
1178 tree object_scope;
1179 tree qualifying_scope;
1181 /* A stack of parsing contexts. All but the bottom entry on the
1182 stack will be tentative contexts.
1184 We parse tentatively in order to determine which construct is in
1185 use in some situations. For example, in order to determine
1186 whether a statement is an expression-statement or a
1187 declaration-statement we parse it tentatively as a
1188 declaration-statement. If that fails, we then reparse the same
1189 token stream as an expression-statement. */
1190 cp_parser_context *context;
1192 /* True if we are parsing GNU C++. If this flag is not set, then
1193 GNU extensions are not recognized. */
1194 bool allow_gnu_extensions_p;
1196 /* TRUE if the `>' token should be interpreted as the greater-than
1197 operator. FALSE if it is the end of a template-id or
1198 template-parameter-list. */
1199 bool greater_than_is_operator_p;
1201 /* TRUE if default arguments are allowed within a parameter list
1202 that starts at this point. FALSE if only a gnu extension makes
1203 them permissable. */
1204 bool default_arg_ok_p;
1206 /* TRUE if we are parsing an integral constant-expression. See
1207 [expr.const] for a precise definition. */
1208 bool constant_expression_p;
1210 /* TRUE if we are parsing an integral constant-expression -- but a
1211 non-constant expression should be permitted as well. This flag
1212 is used when parsing an array bound so that GNU variable-length
1213 arrays are tolerated. */
1214 bool allow_non_constant_expression_p;
1216 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1217 been seen that makes the expression non-constant. */
1218 bool non_constant_expression_p;
1220 /* TRUE if local variable names and `this' are forbidden in the
1221 current context. */
1222 bool local_variables_forbidden_p;
1224 /* TRUE if the declaration we are parsing is part of a
1225 linkage-specification of the form `extern string-literal
1226 declaration'. */
1227 bool in_unbraced_linkage_specification_p;
1229 /* TRUE if we are presently parsing a declarator, after the
1230 direct-declarator. */
1231 bool in_declarator_p;
1233 /* If non-NULL, then we are parsing a construct where new type
1234 definitions are not permitted. The string stored here will be
1235 issued as an error message if a type is defined. */
1236 const char *type_definition_forbidden_message;
1238 /* A list of lists. The outer list is a stack, used for member
1239 functions of local classes. At each level there are two sub-list,
1240 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1241 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1242 TREE_VALUE's. The functions are chained in reverse declaration
1243 order.
1245 The TREE_PURPOSE sublist contains those functions with default
1246 arguments that need post processing, and the TREE_VALUE sublist
1247 contains those functions with definitions that need post
1248 processing.
1250 These lists can only be processed once the outermost class being
1251 defined is complete. */
1252 tree unparsed_functions_queues;
1254 /* The number of classes whose definitions are currently in
1255 progress. */
1256 unsigned num_classes_being_defined;
1258 /* The number of template parameter lists that apply directly to the
1259 current declaration. */
1260 unsigned num_template_parameter_lists;
1261 } cp_parser;
1263 /* The type of a function that parses some kind of expression */
1264 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1266 /* Prototypes. */
1268 /* Constructors and destructors. */
1270 static cp_parser *cp_parser_new
1271 (void);
1273 /* Routines to parse various constructs.
1275 Those that return `tree' will return the error_mark_node (rather
1276 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1277 Sometimes, they will return an ordinary node if error-recovery was
1278 attempted, even though a parse error occurred. So, to check
1279 whether or not a parse error occurred, you should always use
1280 cp_parser_error_occurred. If the construct is optional (indicated
1281 either by an `_opt' in the name of the function that does the
1282 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1283 the construct is not present. */
1285 /* Lexical conventions [gram.lex] */
1287 static tree cp_parser_identifier
1288 (cp_parser *);
1290 /* Basic concepts [gram.basic] */
1292 static bool cp_parser_translation_unit
1293 (cp_parser *);
1295 /* Expressions [gram.expr] */
1297 static tree cp_parser_primary_expression
1298 (cp_parser *, cp_id_kind *, tree *);
1299 static tree cp_parser_id_expression
1300 (cp_parser *, bool, bool, bool *, bool);
1301 static tree cp_parser_unqualified_id
1302 (cp_parser *, bool, bool, bool);
1303 static tree cp_parser_nested_name_specifier_opt
1304 (cp_parser *, bool, bool, bool);
1305 static tree cp_parser_nested_name_specifier
1306 (cp_parser *, bool, bool, bool);
1307 static tree cp_parser_class_or_namespace_name
1308 (cp_parser *, bool, bool, bool, bool);
1309 static tree cp_parser_postfix_expression
1310 (cp_parser *, bool);
1311 static tree cp_parser_parenthesized_expression_list
1312 (cp_parser *, bool, bool *);
1313 static void cp_parser_pseudo_destructor_name
1314 (cp_parser *, tree *, tree *);
1315 static tree cp_parser_unary_expression
1316 (cp_parser *, bool);
1317 static enum tree_code cp_parser_unary_operator
1318 (cp_token *);
1319 static tree cp_parser_new_expression
1320 (cp_parser *);
1321 static tree cp_parser_new_placement
1322 (cp_parser *);
1323 static tree cp_parser_new_type_id
1324 (cp_parser *);
1325 static tree cp_parser_new_declarator_opt
1326 (cp_parser *);
1327 static tree cp_parser_direct_new_declarator
1328 (cp_parser *);
1329 static tree cp_parser_new_initializer
1330 (cp_parser *);
1331 static tree cp_parser_delete_expression
1332 (cp_parser *);
1333 static tree cp_parser_cast_expression
1334 (cp_parser *, bool);
1335 static tree cp_parser_pm_expression
1336 (cp_parser *);
1337 static tree cp_parser_multiplicative_expression
1338 (cp_parser *);
1339 static tree cp_parser_additive_expression
1340 (cp_parser *);
1341 static tree cp_parser_shift_expression
1342 (cp_parser *);
1343 static tree cp_parser_relational_expression
1344 (cp_parser *);
1345 static tree cp_parser_equality_expression
1346 (cp_parser *);
1347 static tree cp_parser_and_expression
1348 (cp_parser *);
1349 static tree cp_parser_exclusive_or_expression
1350 (cp_parser *);
1351 static tree cp_parser_inclusive_or_expression
1352 (cp_parser *);
1353 static tree cp_parser_logical_and_expression
1354 (cp_parser *);
1355 static tree cp_parser_logical_or_expression
1356 (cp_parser *);
1357 static tree cp_parser_question_colon_clause
1358 (cp_parser *, tree);
1359 static tree cp_parser_assignment_expression
1360 (cp_parser *);
1361 static enum tree_code cp_parser_assignment_operator_opt
1362 (cp_parser *);
1363 static tree cp_parser_expression
1364 (cp_parser *);
1365 static tree cp_parser_constant_expression
1366 (cp_parser *, bool, bool *);
1368 /* Statements [gram.stmt.stmt] */
1370 static void cp_parser_statement
1371 (cp_parser *, bool);
1372 static tree cp_parser_labeled_statement
1373 (cp_parser *, bool);
1374 static tree cp_parser_expression_statement
1375 (cp_parser *, bool);
1376 static tree cp_parser_compound_statement
1377 (cp_parser *, bool);
1378 static void cp_parser_statement_seq_opt
1379 (cp_parser *, bool);
1380 static tree cp_parser_selection_statement
1381 (cp_parser *);
1382 static tree cp_parser_condition
1383 (cp_parser *);
1384 static tree cp_parser_iteration_statement
1385 (cp_parser *);
1386 static void cp_parser_for_init_statement
1387 (cp_parser *);
1388 static tree cp_parser_jump_statement
1389 (cp_parser *);
1390 static void cp_parser_declaration_statement
1391 (cp_parser *);
1393 static tree cp_parser_implicitly_scoped_statement
1394 (cp_parser *);
1395 static void cp_parser_already_scoped_statement
1396 (cp_parser *);
1398 /* Declarations [gram.dcl.dcl] */
1400 static void cp_parser_declaration_seq_opt
1401 (cp_parser *);
1402 static void cp_parser_declaration
1403 (cp_parser *);
1404 static void cp_parser_block_declaration
1405 (cp_parser *, bool);
1406 static void cp_parser_simple_declaration
1407 (cp_parser *, bool);
1408 static tree cp_parser_decl_specifier_seq
1409 (cp_parser *, cp_parser_flags, tree *, int *);
1410 static tree cp_parser_storage_class_specifier_opt
1411 (cp_parser *);
1412 static tree cp_parser_function_specifier_opt
1413 (cp_parser *);
1414 static tree cp_parser_type_specifier
1415 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1416 static tree cp_parser_simple_type_specifier
1417 (cp_parser *, cp_parser_flags, bool);
1418 static tree cp_parser_type_name
1419 (cp_parser *);
1420 static tree cp_parser_elaborated_type_specifier
1421 (cp_parser *, bool, bool);
1422 static tree cp_parser_enum_specifier
1423 (cp_parser *);
1424 static void cp_parser_enumerator_list
1425 (cp_parser *, tree);
1426 static void cp_parser_enumerator_definition
1427 (cp_parser *, tree);
1428 static tree cp_parser_namespace_name
1429 (cp_parser *);
1430 static void cp_parser_namespace_definition
1431 (cp_parser *);
1432 static void cp_parser_namespace_body
1433 (cp_parser *);
1434 static tree cp_parser_qualified_namespace_specifier
1435 (cp_parser *);
1436 static void cp_parser_namespace_alias_definition
1437 (cp_parser *);
1438 static void cp_parser_using_declaration
1439 (cp_parser *);
1440 static void cp_parser_using_directive
1441 (cp_parser *);
1442 static void cp_parser_asm_definition
1443 (cp_parser *);
1444 static void cp_parser_linkage_specification
1445 (cp_parser *);
1447 /* Declarators [gram.dcl.decl] */
1449 static tree cp_parser_init_declarator
1450 (cp_parser *, tree, tree, bool, bool, int, bool *);
1451 static tree cp_parser_declarator
1452 (cp_parser *, cp_parser_declarator_kind, int *);
1453 static tree cp_parser_direct_declarator
1454 (cp_parser *, cp_parser_declarator_kind, int *);
1455 static enum tree_code cp_parser_ptr_operator
1456 (cp_parser *, tree *, tree *);
1457 static tree cp_parser_cv_qualifier_seq_opt
1458 (cp_parser *);
1459 static tree cp_parser_cv_qualifier_opt
1460 (cp_parser *);
1461 static tree cp_parser_declarator_id
1462 (cp_parser *);
1463 static tree cp_parser_type_id
1464 (cp_parser *);
1465 static tree cp_parser_type_specifier_seq
1466 (cp_parser *);
1467 static tree cp_parser_parameter_declaration_clause
1468 (cp_parser *);
1469 static tree cp_parser_parameter_declaration_list
1470 (cp_parser *);
1471 static tree cp_parser_parameter_declaration
1472 (cp_parser *, bool);
1473 static tree cp_parser_function_definition
1474 (cp_parser *, bool *);
1475 static void cp_parser_function_body
1476 (cp_parser *);
1477 static tree cp_parser_initializer
1478 (cp_parser *, bool *, bool *);
1479 static tree cp_parser_initializer_clause
1480 (cp_parser *, bool *);
1481 static tree cp_parser_initializer_list
1482 (cp_parser *, bool *);
1484 static bool cp_parser_ctor_initializer_opt_and_function_body
1485 (cp_parser *);
1487 /* Classes [gram.class] */
1489 static tree cp_parser_class_name
1490 (cp_parser *, bool, bool, bool, bool, bool);
1491 static tree cp_parser_class_specifier
1492 (cp_parser *);
1493 static tree cp_parser_class_head
1494 (cp_parser *, bool *);
1495 static enum tag_types cp_parser_class_key
1496 (cp_parser *);
1497 static void cp_parser_member_specification_opt
1498 (cp_parser *);
1499 static void cp_parser_member_declaration
1500 (cp_parser *);
1501 static tree cp_parser_pure_specifier
1502 (cp_parser *);
1503 static tree cp_parser_constant_initializer
1504 (cp_parser *);
1506 /* Derived classes [gram.class.derived] */
1508 static tree cp_parser_base_clause
1509 (cp_parser *);
1510 static tree cp_parser_base_specifier
1511 (cp_parser *);
1513 /* Special member functions [gram.special] */
1515 static tree cp_parser_conversion_function_id
1516 (cp_parser *);
1517 static tree cp_parser_conversion_type_id
1518 (cp_parser *);
1519 static tree cp_parser_conversion_declarator_opt
1520 (cp_parser *);
1521 static bool cp_parser_ctor_initializer_opt
1522 (cp_parser *);
1523 static void cp_parser_mem_initializer_list
1524 (cp_parser *);
1525 static tree cp_parser_mem_initializer
1526 (cp_parser *);
1527 static tree cp_parser_mem_initializer_id
1528 (cp_parser *);
1530 /* Overloading [gram.over] */
1532 static tree cp_parser_operator_function_id
1533 (cp_parser *);
1534 static tree cp_parser_operator
1535 (cp_parser *);
1537 /* Templates [gram.temp] */
1539 static void cp_parser_template_declaration
1540 (cp_parser *, bool);
1541 static tree cp_parser_template_parameter_list
1542 (cp_parser *);
1543 static tree cp_parser_template_parameter
1544 (cp_parser *);
1545 static tree cp_parser_type_parameter
1546 (cp_parser *);
1547 static tree cp_parser_template_id
1548 (cp_parser *, bool, bool);
1549 static tree cp_parser_template_name
1550 (cp_parser *, bool, bool);
1551 static tree cp_parser_template_argument_list
1552 (cp_parser *);
1553 static tree cp_parser_template_argument
1554 (cp_parser *);
1555 static void cp_parser_explicit_instantiation
1556 (cp_parser *);
1557 static void cp_parser_explicit_specialization
1558 (cp_parser *);
1560 /* Exception handling [gram.exception] */
1562 static tree cp_parser_try_block
1563 (cp_parser *);
1564 static bool cp_parser_function_try_block
1565 (cp_parser *);
1566 static void cp_parser_handler_seq
1567 (cp_parser *);
1568 static void cp_parser_handler
1569 (cp_parser *);
1570 static tree cp_parser_exception_declaration
1571 (cp_parser *);
1572 static tree cp_parser_throw_expression
1573 (cp_parser *);
1574 static tree cp_parser_exception_specification_opt
1575 (cp_parser *);
1576 static tree cp_parser_type_id_list
1577 (cp_parser *);
1579 /* GNU Extensions */
1581 static tree cp_parser_asm_specification_opt
1582 (cp_parser *);
1583 static tree cp_parser_asm_operand_list
1584 (cp_parser *);
1585 static tree cp_parser_asm_clobber_list
1586 (cp_parser *);
1587 static tree cp_parser_attributes_opt
1588 (cp_parser *);
1589 static tree cp_parser_attribute_list
1590 (cp_parser *);
1591 static bool cp_parser_extension_opt
1592 (cp_parser *, int *);
1593 static void cp_parser_label_declaration
1594 (cp_parser *);
1596 /* Utility Routines */
1598 static tree cp_parser_lookup_name
1599 (cp_parser *, tree, bool, bool, bool);
1600 static tree cp_parser_lookup_name_simple
1601 (cp_parser *, tree);
1602 static tree cp_parser_maybe_treat_template_as_class
1603 (tree, bool);
1604 static bool cp_parser_check_declarator_template_parameters
1605 (cp_parser *, tree);
1606 static bool cp_parser_check_template_parameters
1607 (cp_parser *, unsigned);
1608 static tree cp_parser_simple_cast_expression
1609 (cp_parser *);
1610 static tree cp_parser_binary_expression
1611 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1612 static tree cp_parser_global_scope_opt
1613 (cp_parser *, bool);
1614 static bool cp_parser_constructor_declarator_p
1615 (cp_parser *, bool);
1616 static tree cp_parser_function_definition_from_specifiers_and_declarator
1617 (cp_parser *, tree, tree, tree);
1618 static tree cp_parser_function_definition_after_declarator
1619 (cp_parser *, bool);
1620 static void cp_parser_template_declaration_after_export
1621 (cp_parser *, bool);
1622 static tree cp_parser_single_declaration
1623 (cp_parser *, bool, bool *);
1624 static tree cp_parser_functional_cast
1625 (cp_parser *, tree);
1626 static void cp_parser_save_default_args
1627 (cp_parser *, tree);
1628 static void cp_parser_late_parsing_for_member
1629 (cp_parser *, tree);
1630 static void cp_parser_late_parsing_default_args
1631 (cp_parser *, tree);
1632 static tree cp_parser_sizeof_operand
1633 (cp_parser *, enum rid);
1634 static bool cp_parser_declares_only_class_p
1635 (cp_parser *);
1636 static tree cp_parser_fold_non_dependent_expr
1637 (tree);
1638 static bool cp_parser_friend_p
1639 (tree);
1640 static cp_token *cp_parser_require
1641 (cp_parser *, enum cpp_ttype, const char *);
1642 static cp_token *cp_parser_require_keyword
1643 (cp_parser *, enum rid, const char *);
1644 static bool cp_parser_token_starts_function_definition_p
1645 (cp_token *);
1646 static bool cp_parser_next_token_starts_class_definition_p
1647 (cp_parser *);
1648 static bool cp_parser_next_token_ends_template_argument_p
1649 (cp_parser *);
1650 static enum tag_types cp_parser_token_is_class_key
1651 (cp_token *);
1652 static void cp_parser_check_class_key
1653 (enum tag_types, tree type);
1654 static bool cp_parser_optional_template_keyword
1655 (cp_parser *);
1656 static void cp_parser_pre_parsed_nested_name_specifier
1657 (cp_parser *);
1658 static void cp_parser_cache_group
1659 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1660 static void cp_parser_parse_tentatively
1661 (cp_parser *);
1662 static void cp_parser_commit_to_tentative_parse
1663 (cp_parser *);
1664 static void cp_parser_abort_tentative_parse
1665 (cp_parser *);
1666 static bool cp_parser_parse_definitely
1667 (cp_parser *);
1668 static inline bool cp_parser_parsing_tentatively
1669 (cp_parser *);
1670 static bool cp_parser_committed_to_tentative_parse
1671 (cp_parser *);
1672 static void cp_parser_error
1673 (cp_parser *, const char *);
1674 static bool cp_parser_simulate_error
1675 (cp_parser *);
1676 static void cp_parser_check_type_definition
1677 (cp_parser *);
1678 static void cp_parser_check_for_definition_in_return_type
1679 (tree, int);
1680 static tree cp_parser_non_constant_expression
1681 (const char *);
1682 static bool cp_parser_diagnose_invalid_type_name
1683 (cp_parser *);
1684 static int cp_parser_skip_to_closing_parenthesis
1685 (cp_parser *, bool, bool);
1686 static void cp_parser_skip_to_end_of_statement
1687 (cp_parser *);
1688 static void cp_parser_consume_semicolon_at_end_of_statement
1689 (cp_parser *);
1690 static void cp_parser_skip_to_end_of_block_or_statement
1691 (cp_parser *);
1692 static void cp_parser_skip_to_closing_brace
1693 (cp_parser *);
1694 static void cp_parser_skip_until_found
1695 (cp_parser *, enum cpp_ttype, const char *);
1696 static bool cp_parser_error_occurred
1697 (cp_parser *);
1698 static bool cp_parser_allow_gnu_extensions_p
1699 (cp_parser *);
1700 static bool cp_parser_is_string_literal
1701 (cp_token *);
1702 static bool cp_parser_is_keyword
1703 (cp_token *, enum rid);
1705 /* Returns nonzero if we are parsing tentatively. */
1707 static inline bool
1708 cp_parser_parsing_tentatively (cp_parser* parser)
1710 return parser->context->next != NULL;
1713 /* Returns nonzero if TOKEN is a string literal. */
1715 static bool
1716 cp_parser_is_string_literal (cp_token* token)
1718 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1721 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1723 static bool
1724 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1726 return token->keyword == keyword;
1729 /* Issue the indicated error MESSAGE. */
1731 static void
1732 cp_parser_error (cp_parser* parser, const char* message)
1734 /* Output the MESSAGE -- unless we're parsing tentatively. */
1735 if (!cp_parser_simulate_error (parser))
1736 error (message);
1739 /* If we are parsing tentatively, remember that an error has occurred
1740 during this tentative parse. Returns true if the error was
1741 simulated; false if a messgae should be issued by the caller. */
1743 static bool
1744 cp_parser_simulate_error (cp_parser* parser)
1746 if (cp_parser_parsing_tentatively (parser)
1747 && !cp_parser_committed_to_tentative_parse (parser))
1749 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1750 return true;
1752 return false;
1755 /* This function is called when a type is defined. If type
1756 definitions are forbidden at this point, an error message is
1757 issued. */
1759 static void
1760 cp_parser_check_type_definition (cp_parser* parser)
1762 /* If types are forbidden here, issue a message. */
1763 if (parser->type_definition_forbidden_message)
1764 /* Use `%s' to print the string in case there are any escape
1765 characters in the message. */
1766 error ("%s", parser->type_definition_forbidden_message);
1769 /* This function is called when a declaration is parsed. If
1770 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1771 indicates that a type was defined in the decl-specifiers for DECL,
1772 then an error is issued. */
1774 static void
1775 cp_parser_check_for_definition_in_return_type (tree declarator,
1776 int declares_class_or_enum)
1778 /* [dcl.fct] forbids type definitions in return types.
1779 Unfortunately, it's not easy to know whether or not we are
1780 processing a return type until after the fact. */
1781 while (declarator
1782 && (TREE_CODE (declarator) == INDIRECT_REF
1783 || TREE_CODE (declarator) == ADDR_EXPR))
1784 declarator = TREE_OPERAND (declarator, 0);
1785 if (declarator
1786 && TREE_CODE (declarator) == CALL_EXPR
1787 && declares_class_or_enum & 2)
1788 error ("new types may not be defined in a return type");
1791 /* Issue an eror message about the fact that THING appeared in a
1792 constant-expression. Returns ERROR_MARK_NODE. */
1794 static tree
1795 cp_parser_non_constant_expression (const char *thing)
1797 error ("%s cannot appear in a constant-expression", thing);
1798 return error_mark_node;
1801 /* Check for a common situation where a type-name should be present,
1802 but is not, and issue a sensible error message. Returns true if an
1803 invalid type-name was detected. */
1805 static bool
1806 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1808 /* If the next two tokens are both identifiers, the code is
1809 erroneous. The usual cause of this situation is code like:
1811 T t;
1813 where "T" should name a type -- but does not. */
1814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1815 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1817 tree name;
1819 /* If parsing tentatively, we should commit; we really are
1820 looking at a declaration. */
1821 /* Consume the first identifier. */
1822 name = cp_lexer_consume_token (parser->lexer)->value;
1823 /* Issue an error message. */
1824 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1825 /* If we're in a template class, it's possible that the user was
1826 referring to a type from a base class. For example:
1828 template <typename T> struct A { typedef T X; };
1829 template <typename T> struct B : public A<T> { X x; };
1831 The user should have said "typename A<T>::X". */
1832 if (processing_template_decl && current_class_type)
1834 tree b;
1836 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1838 b = TREE_CHAIN (b))
1840 tree base_type = BINFO_TYPE (b);
1841 if (CLASS_TYPE_P (base_type)
1842 && dependent_type_p (base_type))
1844 tree field;
1845 /* Go from a particular instantiation of the
1846 template (which will have an empty TYPE_FIELDs),
1847 to the main version. */
1848 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1849 for (field = TYPE_FIELDS (base_type);
1850 field;
1851 field = TREE_CHAIN (field))
1852 if (TREE_CODE (field) == TYPE_DECL
1853 && DECL_NAME (field) == name)
1855 error ("(perhaps `typename %T::%s' was intended)",
1856 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1857 break;
1859 if (field)
1860 break;
1864 /* Skip to the end of the declaration; there's no point in
1865 trying to process it. */
1866 cp_parser_skip_to_end_of_statement (parser);
1868 return true;
1871 return false;
1874 /* Consume tokens up to, and including, the next non-nested closing `)'.
1875 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1876 are doing error recovery. Returns -1 if OR_COMMA is true and we
1877 found an unnested comma. */
1879 static int
1880 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
1881 bool recovering, bool or_comma)
1883 unsigned paren_depth = 0;
1884 unsigned brace_depth = 0;
1886 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
1887 && !cp_parser_committed_to_tentative_parse (parser))
1888 return 0;
1890 while (true)
1892 cp_token *token;
1894 /* If we've run out of tokens, then there is no closing `)'. */
1895 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1896 return 0;
1898 if (recovering)
1900 token = cp_lexer_peek_token (parser->lexer);
1902 /* This matches the processing in skip_to_end_of_statement */
1903 if (token->type == CPP_SEMICOLON && !brace_depth)
1904 return 0;
1905 if (token->type == CPP_OPEN_BRACE)
1906 ++brace_depth;
1907 if (token->type == CPP_CLOSE_BRACE)
1909 if (!brace_depth--)
1910 return 0;
1912 if (or_comma && token->type == CPP_COMMA
1913 && !brace_depth && !paren_depth)
1914 return -1;
1917 /* Consume the token. */
1918 token = cp_lexer_consume_token (parser->lexer);
1920 if (!brace_depth)
1922 /* If it is an `(', we have entered another level of nesting. */
1923 if (token->type == CPP_OPEN_PAREN)
1924 ++paren_depth;
1925 /* If it is a `)', then we might be done. */
1926 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
1927 return 1;
1932 /* Consume tokens until we reach the end of the current statement.
1933 Normally, that will be just before consuming a `;'. However, if a
1934 non-nested `}' comes first, then we stop before consuming that. */
1936 static void
1937 cp_parser_skip_to_end_of_statement (cp_parser* parser)
1939 unsigned nesting_depth = 0;
1941 while (true)
1943 cp_token *token;
1945 /* Peek at the next token. */
1946 token = cp_lexer_peek_token (parser->lexer);
1947 /* If we've run out of tokens, stop. */
1948 if (token->type == CPP_EOF)
1949 break;
1950 /* If the next token is a `;', we have reached the end of the
1951 statement. */
1952 if (token->type == CPP_SEMICOLON && !nesting_depth)
1953 break;
1954 /* If the next token is a non-nested `}', then we have reached
1955 the end of the current block. */
1956 if (token->type == CPP_CLOSE_BRACE)
1958 /* If this is a non-nested `}', stop before consuming it.
1959 That way, when confronted with something like:
1961 { 3 + }
1963 we stop before consuming the closing `}', even though we
1964 have not yet reached a `;'. */
1965 if (nesting_depth == 0)
1966 break;
1967 /* If it is the closing `}' for a block that we have
1968 scanned, stop -- but only after consuming the token.
1969 That way given:
1971 void f g () { ... }
1972 typedef int I;
1974 we will stop after the body of the erroneously declared
1975 function, but before consuming the following `typedef'
1976 declaration. */
1977 if (--nesting_depth == 0)
1979 cp_lexer_consume_token (parser->lexer);
1980 break;
1983 /* If it the next token is a `{', then we are entering a new
1984 block. Consume the entire block. */
1985 else if (token->type == CPP_OPEN_BRACE)
1986 ++nesting_depth;
1987 /* Consume the token. */
1988 cp_lexer_consume_token (parser->lexer);
1992 /* This function is called at the end of a statement or declaration.
1993 If the next token is a semicolon, it is consumed; otherwise, error
1994 recovery is attempted. */
1996 static void
1997 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
1999 /* Look for the trailing `;'. */
2000 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2002 /* If there is additional (erroneous) input, skip to the end of
2003 the statement. */
2004 cp_parser_skip_to_end_of_statement (parser);
2005 /* If the next token is now a `;', consume it. */
2006 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2007 cp_lexer_consume_token (parser->lexer);
2011 /* Skip tokens until we have consumed an entire block, or until we
2012 have consumed a non-nested `;'. */
2014 static void
2015 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2017 unsigned nesting_depth = 0;
2019 while (true)
2021 cp_token *token;
2023 /* Peek at the next token. */
2024 token = cp_lexer_peek_token (parser->lexer);
2025 /* If we've run out of tokens, stop. */
2026 if (token->type == CPP_EOF)
2027 break;
2028 /* If the next token is a `;', we have reached the end of the
2029 statement. */
2030 if (token->type == CPP_SEMICOLON && !nesting_depth)
2032 /* Consume the `;'. */
2033 cp_lexer_consume_token (parser->lexer);
2034 break;
2036 /* Consume the token. */
2037 token = cp_lexer_consume_token (parser->lexer);
2038 /* If the next token is a non-nested `}', then we have reached
2039 the end of the current block. */
2040 if (token->type == CPP_CLOSE_BRACE
2041 && (nesting_depth == 0 || --nesting_depth == 0))
2042 break;
2043 /* If it the next token is a `{', then we are entering a new
2044 block. Consume the entire block. */
2045 if (token->type == CPP_OPEN_BRACE)
2046 ++nesting_depth;
2050 /* Skip tokens until a non-nested closing curly brace is the next
2051 token. */
2053 static void
2054 cp_parser_skip_to_closing_brace (cp_parser *parser)
2056 unsigned nesting_depth = 0;
2058 while (true)
2060 cp_token *token;
2062 /* Peek at the next token. */
2063 token = cp_lexer_peek_token (parser->lexer);
2064 /* If we've run out of tokens, stop. */
2065 if (token->type == CPP_EOF)
2066 break;
2067 /* If the next token is a non-nested `}', then we have reached
2068 the end of the current block. */
2069 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2070 break;
2071 /* If it the next token is a `{', then we are entering a new
2072 block. Consume the entire block. */
2073 else if (token->type == CPP_OPEN_BRACE)
2074 ++nesting_depth;
2075 /* Consume the token. */
2076 cp_lexer_consume_token (parser->lexer);
2080 /* Create a new C++ parser. */
2082 static cp_parser *
2083 cp_parser_new (void)
2085 cp_parser *parser;
2086 cp_lexer *lexer;
2088 /* cp_lexer_new_main is called before calling ggc_alloc because
2089 cp_lexer_new_main might load a PCH file. */
2090 lexer = cp_lexer_new_main ();
2092 parser = ggc_alloc_cleared (sizeof (cp_parser));
2093 parser->lexer = lexer;
2094 parser->context = cp_parser_context_new (NULL);
2096 /* For now, we always accept GNU extensions. */
2097 parser->allow_gnu_extensions_p = 1;
2099 /* The `>' token is a greater-than operator, not the end of a
2100 template-id. */
2101 parser->greater_than_is_operator_p = true;
2103 parser->default_arg_ok_p = true;
2105 /* We are not parsing a constant-expression. */
2106 parser->constant_expression_p = false;
2107 parser->allow_non_constant_expression_p = false;
2108 parser->non_constant_expression_p = false;
2110 /* Local variable names are not forbidden. */
2111 parser->local_variables_forbidden_p = false;
2113 /* We are not processing an `extern "C"' declaration. */
2114 parser->in_unbraced_linkage_specification_p = false;
2116 /* We are not processing a declarator. */
2117 parser->in_declarator_p = false;
2119 /* The unparsed function queue is empty. */
2120 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2122 /* There are no classes being defined. */
2123 parser->num_classes_being_defined = 0;
2125 /* No template parameters apply. */
2126 parser->num_template_parameter_lists = 0;
2128 return parser;
2131 /* Lexical conventions [gram.lex] */
2133 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2134 identifier. */
2136 static tree
2137 cp_parser_identifier (cp_parser* parser)
2139 cp_token *token;
2141 /* Look for the identifier. */
2142 token = cp_parser_require (parser, CPP_NAME, "identifier");
2143 /* Return the value. */
2144 return token ? token->value : error_mark_node;
2147 /* Basic concepts [gram.basic] */
2149 /* Parse a translation-unit.
2151 translation-unit:
2152 declaration-seq [opt]
2154 Returns TRUE if all went well. */
2156 static bool
2157 cp_parser_translation_unit (cp_parser* parser)
2159 while (true)
2161 cp_parser_declaration_seq_opt (parser);
2163 /* If there are no tokens left then all went well. */
2164 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2165 break;
2167 /* Otherwise, issue an error message. */
2168 cp_parser_error (parser, "expected declaration");
2169 return false;
2172 /* Consume the EOF token. */
2173 cp_parser_require (parser, CPP_EOF, "end-of-file");
2175 /* Finish up. */
2176 finish_translation_unit ();
2178 /* All went well. */
2179 return true;
2182 /* Expressions [gram.expr] */
2184 /* Parse a primary-expression.
2186 primary-expression:
2187 literal
2188 this
2189 ( expression )
2190 id-expression
2192 GNU Extensions:
2194 primary-expression:
2195 ( compound-statement )
2196 __builtin_va_arg ( assignment-expression , type-id )
2198 literal:
2199 __null
2201 Returns a representation of the expression.
2203 *IDK indicates what kind of id-expression (if any) was present.
2205 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2206 used as the operand of a pointer-to-member. In that case,
2207 *QUALIFYING_CLASS gives the class that is used as the qualifying
2208 class in the pointer-to-member. */
2210 static tree
2211 cp_parser_primary_expression (cp_parser *parser,
2212 cp_id_kind *idk,
2213 tree *qualifying_class)
2215 cp_token *token;
2217 /* Assume the primary expression is not an id-expression. */
2218 *idk = CP_ID_KIND_NONE;
2219 /* And that it cannot be used as pointer-to-member. */
2220 *qualifying_class = NULL_TREE;
2222 /* Peek at the next token. */
2223 token = cp_lexer_peek_token (parser->lexer);
2224 switch (token->type)
2226 /* literal:
2227 integer-literal
2228 character-literal
2229 floating-literal
2230 string-literal
2231 boolean-literal */
2232 case CPP_CHAR:
2233 case CPP_WCHAR:
2234 case CPP_STRING:
2235 case CPP_WSTRING:
2236 case CPP_NUMBER:
2237 token = cp_lexer_consume_token (parser->lexer);
2238 return token->value;
2240 case CPP_OPEN_PAREN:
2242 tree expr;
2243 bool saved_greater_than_is_operator_p;
2245 /* Consume the `('. */
2246 cp_lexer_consume_token (parser->lexer);
2247 /* Within a parenthesized expression, a `>' token is always
2248 the greater-than operator. */
2249 saved_greater_than_is_operator_p
2250 = parser->greater_than_is_operator_p;
2251 parser->greater_than_is_operator_p = true;
2252 /* If we see `( { ' then we are looking at the beginning of
2253 a GNU statement-expression. */
2254 if (cp_parser_allow_gnu_extensions_p (parser)
2255 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2257 /* Statement-expressions are not allowed by the standard. */
2258 if (pedantic)
2259 pedwarn ("ISO C++ forbids braced-groups within expressions");
2261 /* And they're not allowed outside of a function-body; you
2262 cannot, for example, write:
2264 int i = ({ int j = 3; j + 1; });
2266 at class or namespace scope. */
2267 if (!at_function_scope_p ())
2268 error ("statement-expressions are allowed only inside functions");
2269 /* Start the statement-expression. */
2270 expr = begin_stmt_expr ();
2271 /* Parse the compound-statement. */
2272 cp_parser_compound_statement (parser, true);
2273 /* Finish up. */
2274 expr = finish_stmt_expr (expr, false);
2276 else
2278 /* Parse the parenthesized expression. */
2279 expr = cp_parser_expression (parser);
2280 /* Let the front end know that this expression was
2281 enclosed in parentheses. This matters in case, for
2282 example, the expression is of the form `A::B', since
2283 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2284 not. */
2285 finish_parenthesized_expr (expr);
2287 /* The `>' token might be the end of a template-id or
2288 template-parameter-list now. */
2289 parser->greater_than_is_operator_p
2290 = saved_greater_than_is_operator_p;
2291 /* Consume the `)'. */
2292 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2293 cp_parser_skip_to_end_of_statement (parser);
2295 return expr;
2298 case CPP_KEYWORD:
2299 switch (token->keyword)
2301 /* These two are the boolean literals. */
2302 case RID_TRUE:
2303 cp_lexer_consume_token (parser->lexer);
2304 return boolean_true_node;
2305 case RID_FALSE:
2306 cp_lexer_consume_token (parser->lexer);
2307 return boolean_false_node;
2309 /* The `__null' literal. */
2310 case RID_NULL:
2311 cp_lexer_consume_token (parser->lexer);
2312 return null_node;
2314 /* Recognize the `this' keyword. */
2315 case RID_THIS:
2316 cp_lexer_consume_token (parser->lexer);
2317 if (parser->local_variables_forbidden_p)
2319 error ("`this' may not be used in this context");
2320 return error_mark_node;
2322 /* Pointers cannot appear in constant-expressions. */
2323 if (parser->constant_expression_p)
2325 if (!parser->allow_non_constant_expression_p)
2326 return cp_parser_non_constant_expression ("`this'");
2327 parser->non_constant_expression_p = true;
2329 return finish_this_expr ();
2331 /* The `operator' keyword can be the beginning of an
2332 id-expression. */
2333 case RID_OPERATOR:
2334 goto id_expression;
2336 case RID_FUNCTION_NAME:
2337 case RID_PRETTY_FUNCTION_NAME:
2338 case RID_C99_FUNCTION_NAME:
2339 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2340 __func__ are the names of variables -- but they are
2341 treated specially. Therefore, they are handled here,
2342 rather than relying on the generic id-expression logic
2343 below. Grammatically, these names are id-expressions.
2345 Consume the token. */
2346 token = cp_lexer_consume_token (parser->lexer);
2347 /* Look up the name. */
2348 return finish_fname (token->value);
2350 case RID_VA_ARG:
2352 tree expression;
2353 tree type;
2355 /* The `__builtin_va_arg' construct is used to handle
2356 `va_arg'. Consume the `__builtin_va_arg' token. */
2357 cp_lexer_consume_token (parser->lexer);
2358 /* Look for the opening `('. */
2359 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2360 /* Now, parse the assignment-expression. */
2361 expression = cp_parser_assignment_expression (parser);
2362 /* Look for the `,'. */
2363 cp_parser_require (parser, CPP_COMMA, "`,'");
2364 /* Parse the type-id. */
2365 type = cp_parser_type_id (parser);
2366 /* Look for the closing `)'. */
2367 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2368 /* Using `va_arg' in a constant-expression is not
2369 allowed. */
2370 if (parser->constant_expression_p)
2372 if (!parser->allow_non_constant_expression_p)
2373 return cp_parser_non_constant_expression ("`va_arg'");
2374 parser->non_constant_expression_p = true;
2376 return build_x_va_arg (expression, type);
2379 default:
2380 cp_parser_error (parser, "expected primary-expression");
2381 return error_mark_node;
2384 /* An id-expression can start with either an identifier, a
2385 `::' as the beginning of a qualified-id, or the "operator"
2386 keyword. */
2387 case CPP_NAME:
2388 case CPP_SCOPE:
2389 case CPP_TEMPLATE_ID:
2390 case CPP_NESTED_NAME_SPECIFIER:
2392 tree id_expression;
2393 tree decl;
2394 const char *error_msg;
2396 id_expression:
2397 /* Parse the id-expression. */
2398 id_expression
2399 = cp_parser_id_expression (parser,
2400 /*template_keyword_p=*/false,
2401 /*check_dependency_p=*/true,
2402 /*template_p=*/NULL,
2403 /*declarator_p=*/false);
2404 if (id_expression == error_mark_node)
2405 return error_mark_node;
2406 /* If we have a template-id, then no further lookup is
2407 required. If the template-id was for a template-class, we
2408 will sometimes have a TYPE_DECL at this point. */
2409 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2410 || TREE_CODE (id_expression) == TYPE_DECL)
2411 decl = id_expression;
2412 /* Look up the name. */
2413 else
2415 decl = cp_parser_lookup_name_simple (parser, id_expression);
2416 /* If name lookup gives us a SCOPE_REF, then the
2417 qualifying scope was dependent. Just propagate the
2418 name. */
2419 if (TREE_CODE (decl) == SCOPE_REF)
2421 if (TYPE_P (TREE_OPERAND (decl, 0)))
2422 *qualifying_class = TREE_OPERAND (decl, 0);
2423 return decl;
2425 /* Check to see if DECL is a local variable in a context
2426 where that is forbidden. */
2427 if (parser->local_variables_forbidden_p
2428 && local_variable_p (decl))
2430 /* It might be that we only found DECL because we are
2431 trying to be generous with pre-ISO scoping rules.
2432 For example, consider:
2434 int i;
2435 void g() {
2436 for (int i = 0; i < 10; ++i) {}
2437 extern void f(int j = i);
2440 Here, name look up will originally find the out
2441 of scope `i'. We need to issue a warning message,
2442 but then use the global `i'. */
2443 decl = check_for_out_of_scope_variable (decl);
2444 if (local_variable_p (decl))
2446 error ("local variable `%D' may not appear in this context",
2447 decl);
2448 return error_mark_node;
2453 decl = finish_id_expression (id_expression, decl, parser->scope,
2454 idk, qualifying_class,
2455 parser->constant_expression_p,
2456 parser->allow_non_constant_expression_p,
2457 &parser->non_constant_expression_p,
2458 &error_msg);
2459 if (error_msg)
2460 cp_parser_error (parser, error_msg);
2461 return decl;
2464 /* Anything else is an error. */
2465 default:
2466 cp_parser_error (parser, "expected primary-expression");
2467 return error_mark_node;
2471 /* Parse an id-expression.
2473 id-expression:
2474 unqualified-id
2475 qualified-id
2477 qualified-id:
2478 :: [opt] nested-name-specifier template [opt] unqualified-id
2479 :: identifier
2480 :: operator-function-id
2481 :: template-id
2483 Return a representation of the unqualified portion of the
2484 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2485 a `::' or nested-name-specifier.
2487 Often, if the id-expression was a qualified-id, the caller will
2488 want to make a SCOPE_REF to represent the qualified-id. This
2489 function does not do this in order to avoid wastefully creating
2490 SCOPE_REFs when they are not required.
2492 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2493 `template' keyword.
2495 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2496 uninstantiated templates.
2498 If *TEMPLATE_P is non-NULL, it is set to true iff the
2499 `template' keyword is used to explicitly indicate that the entity
2500 named is a template.
2502 If DECLARATOR_P is true, the id-expression is appearing as part of
2503 a declarator, rather than as part of an exprsesion. */
2505 static tree
2506 cp_parser_id_expression (cp_parser *parser,
2507 bool template_keyword_p,
2508 bool check_dependency_p,
2509 bool *template_p,
2510 bool declarator_p)
2512 bool global_scope_p;
2513 bool nested_name_specifier_p;
2515 /* Assume the `template' keyword was not used. */
2516 if (template_p)
2517 *template_p = false;
2519 /* Look for the optional `::' operator. */
2520 global_scope_p
2521 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2522 != NULL_TREE);
2523 /* Look for the optional nested-name-specifier. */
2524 nested_name_specifier_p
2525 = (cp_parser_nested_name_specifier_opt (parser,
2526 /*typename_keyword_p=*/false,
2527 check_dependency_p,
2528 /*type_p=*/false)
2529 != NULL_TREE);
2530 /* If there is a nested-name-specifier, then we are looking at
2531 the first qualified-id production. */
2532 if (nested_name_specifier_p)
2534 tree saved_scope;
2535 tree saved_object_scope;
2536 tree saved_qualifying_scope;
2537 tree unqualified_id;
2538 bool is_template;
2540 /* See if the next token is the `template' keyword. */
2541 if (!template_p)
2542 template_p = &is_template;
2543 *template_p = cp_parser_optional_template_keyword (parser);
2544 /* Name lookup we do during the processing of the
2545 unqualified-id might obliterate SCOPE. */
2546 saved_scope = parser->scope;
2547 saved_object_scope = parser->object_scope;
2548 saved_qualifying_scope = parser->qualifying_scope;
2549 /* Process the final unqualified-id. */
2550 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2551 check_dependency_p,
2552 declarator_p);
2553 /* Restore the SAVED_SCOPE for our caller. */
2554 parser->scope = saved_scope;
2555 parser->object_scope = saved_object_scope;
2556 parser->qualifying_scope = saved_qualifying_scope;
2558 return unqualified_id;
2560 /* Otherwise, if we are in global scope, then we are looking at one
2561 of the other qualified-id productions. */
2562 else if (global_scope_p)
2564 cp_token *token;
2565 tree id;
2567 /* Peek at the next token. */
2568 token = cp_lexer_peek_token (parser->lexer);
2570 /* If it's an identifier, and the next token is not a "<", then
2571 we can avoid the template-id case. This is an optimization
2572 for this common case. */
2573 if (token->type == CPP_NAME
2574 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2575 return cp_parser_identifier (parser);
2577 cp_parser_parse_tentatively (parser);
2578 /* Try a template-id. */
2579 id = cp_parser_template_id (parser,
2580 /*template_keyword_p=*/false,
2581 /*check_dependency_p=*/true);
2582 /* If that worked, we're done. */
2583 if (cp_parser_parse_definitely (parser))
2584 return id;
2586 /* Peek at the next token. (Changes in the token buffer may
2587 have invalidated the pointer obtained above.) */
2588 token = cp_lexer_peek_token (parser->lexer);
2590 switch (token->type)
2592 case CPP_NAME:
2593 return cp_parser_identifier (parser);
2595 case CPP_KEYWORD:
2596 if (token->keyword == RID_OPERATOR)
2597 return cp_parser_operator_function_id (parser);
2598 /* Fall through. */
2600 default:
2601 cp_parser_error (parser, "expected id-expression");
2602 return error_mark_node;
2605 else
2606 return cp_parser_unqualified_id (parser, template_keyword_p,
2607 /*check_dependency_p=*/true,
2608 declarator_p);
2611 /* Parse an unqualified-id.
2613 unqualified-id:
2614 identifier
2615 operator-function-id
2616 conversion-function-id
2617 ~ class-name
2618 template-id
2620 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2621 keyword, in a construct like `A::template ...'.
2623 Returns a representation of unqualified-id. For the `identifier'
2624 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2625 production a BIT_NOT_EXPR is returned; the operand of the
2626 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2627 other productions, see the documentation accompanying the
2628 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2629 names are looked up in uninstantiated templates. If DECLARATOR_P
2630 is true, the unqualified-id is appearing as part of a declarator,
2631 rather than as part of an expression. */
2633 static tree
2634 cp_parser_unqualified_id (cp_parser* parser,
2635 bool template_keyword_p,
2636 bool check_dependency_p,
2637 bool declarator_p)
2639 cp_token *token;
2641 /* Peek at the next token. */
2642 token = cp_lexer_peek_token (parser->lexer);
2644 switch (token->type)
2646 case CPP_NAME:
2648 tree id;
2650 /* We don't know yet whether or not this will be a
2651 template-id. */
2652 cp_parser_parse_tentatively (parser);
2653 /* Try a template-id. */
2654 id = cp_parser_template_id (parser, template_keyword_p,
2655 check_dependency_p);
2656 /* If it worked, we're done. */
2657 if (cp_parser_parse_definitely (parser))
2658 return id;
2659 /* Otherwise, it's an ordinary identifier. */
2660 return cp_parser_identifier (parser);
2663 case CPP_TEMPLATE_ID:
2664 return cp_parser_template_id (parser, template_keyword_p,
2665 check_dependency_p);
2667 case CPP_COMPL:
2669 tree type_decl;
2670 tree qualifying_scope;
2671 tree object_scope;
2672 tree scope;
2674 /* Consume the `~' token. */
2675 cp_lexer_consume_token (parser->lexer);
2676 /* Parse the class-name. The standard, as written, seems to
2677 say that:
2679 template <typename T> struct S { ~S (); };
2680 template <typename T> S<T>::~S() {}
2682 is invalid, since `~' must be followed by a class-name, but
2683 `S<T>' is dependent, and so not known to be a class.
2684 That's not right; we need to look in uninstantiated
2685 templates. A further complication arises from:
2687 template <typename T> void f(T t) {
2688 t.T::~T();
2691 Here, it is not possible to look up `T' in the scope of `T'
2692 itself. We must look in both the current scope, and the
2693 scope of the containing complete expression.
2695 Yet another issue is:
2697 struct S {
2698 int S;
2699 ~S();
2702 S::~S() {}
2704 The standard does not seem to say that the `S' in `~S'
2705 should refer to the type `S' and not the data member
2706 `S::S'. */
2708 /* DR 244 says that we look up the name after the "~" in the
2709 same scope as we looked up the qualifying name. That idea
2710 isn't fully worked out; it's more complicated than that. */
2711 scope = parser->scope;
2712 object_scope = parser->object_scope;
2713 qualifying_scope = parser->qualifying_scope;
2715 /* If the name is of the form "X::~X" it's OK. */
2716 if (scope && TYPE_P (scope)
2717 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2718 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2719 == CPP_OPEN_PAREN)
2720 && (cp_lexer_peek_token (parser->lexer)->value
2721 == TYPE_IDENTIFIER (scope)))
2723 cp_lexer_consume_token (parser->lexer);
2724 return build_nt (BIT_NOT_EXPR, scope);
2727 /* If there was an explicit qualification (S::~T), first look
2728 in the scope given by the qualification (i.e., S). */
2729 if (scope)
2731 cp_parser_parse_tentatively (parser);
2732 type_decl = cp_parser_class_name (parser,
2733 /*typename_keyword_p=*/false,
2734 /*template_keyword_p=*/false,
2735 /*type_p=*/false,
2736 /*check_dependency=*/false,
2737 /*class_head_p=*/false);
2738 if (cp_parser_parse_definitely (parser))
2739 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2741 /* In "N::S::~S", look in "N" as well. */
2742 if (scope && qualifying_scope)
2744 cp_parser_parse_tentatively (parser);
2745 parser->scope = qualifying_scope;
2746 parser->object_scope = NULL_TREE;
2747 parser->qualifying_scope = NULL_TREE;
2748 type_decl
2749 = cp_parser_class_name (parser,
2750 /*typename_keyword_p=*/false,
2751 /*template_keyword_p=*/false,
2752 /*type_p=*/false,
2753 /*check_dependency=*/false,
2754 /*class_head_p=*/false);
2755 if (cp_parser_parse_definitely (parser))
2756 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2758 /* In "p->S::~T", look in the scope given by "*p" as well. */
2759 else if (object_scope)
2761 cp_parser_parse_tentatively (parser);
2762 parser->scope = object_scope;
2763 parser->object_scope = NULL_TREE;
2764 parser->qualifying_scope = NULL_TREE;
2765 type_decl
2766 = cp_parser_class_name (parser,
2767 /*typename_keyword_p=*/false,
2768 /*template_keyword_p=*/false,
2769 /*type_p=*/false,
2770 /*check_dependency=*/false,
2771 /*class_head_p=*/false);
2772 if (cp_parser_parse_definitely (parser))
2773 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2775 /* Look in the surrounding context. */
2776 parser->scope = NULL_TREE;
2777 parser->object_scope = NULL_TREE;
2778 parser->qualifying_scope = NULL_TREE;
2779 type_decl
2780 = cp_parser_class_name (parser,
2781 /*typename_keyword_p=*/false,
2782 /*template_keyword_p=*/false,
2783 /*type_p=*/false,
2784 /*check_dependency=*/false,
2785 /*class_head_p=*/false);
2786 /* If an error occurred, assume that the name of the
2787 destructor is the same as the name of the qualifying
2788 class. That allows us to keep parsing after running
2789 into ill-formed destructor names. */
2790 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2791 return build_nt (BIT_NOT_EXPR, scope);
2792 else if (type_decl == error_mark_node)
2793 return error_mark_node;
2795 /* [class.dtor]
2797 A typedef-name that names a class shall not be used as the
2798 identifier in the declarator for a destructor declaration. */
2799 if (declarator_p
2800 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2801 && !DECL_SELF_REFERENCE_P (type_decl))
2802 error ("typedef-name `%D' used as destructor declarator",
2803 type_decl);
2805 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2808 case CPP_KEYWORD:
2809 if (token->keyword == RID_OPERATOR)
2811 tree id;
2813 /* This could be a template-id, so we try that first. */
2814 cp_parser_parse_tentatively (parser);
2815 /* Try a template-id. */
2816 id = cp_parser_template_id (parser, template_keyword_p,
2817 /*check_dependency_p=*/true);
2818 /* If that worked, we're done. */
2819 if (cp_parser_parse_definitely (parser))
2820 return id;
2821 /* We still don't know whether we're looking at an
2822 operator-function-id or a conversion-function-id. */
2823 cp_parser_parse_tentatively (parser);
2824 /* Try an operator-function-id. */
2825 id = cp_parser_operator_function_id (parser);
2826 /* If that didn't work, try a conversion-function-id. */
2827 if (!cp_parser_parse_definitely (parser))
2828 id = cp_parser_conversion_function_id (parser);
2830 return id;
2832 /* Fall through. */
2834 default:
2835 cp_parser_error (parser, "expected unqualified-id");
2836 return error_mark_node;
2840 /* Parse an (optional) nested-name-specifier.
2842 nested-name-specifier:
2843 class-or-namespace-name :: nested-name-specifier [opt]
2844 class-or-namespace-name :: template nested-name-specifier [opt]
2846 PARSER->SCOPE should be set appropriately before this function is
2847 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2848 effect. TYPE_P is TRUE if we non-type bindings should be ignored
2849 in name lookups.
2851 Sets PARSER->SCOPE to the class (TYPE) or namespace
2852 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2853 it unchanged if there is no nested-name-specifier. Returns the new
2854 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
2856 static tree
2857 cp_parser_nested_name_specifier_opt (cp_parser *parser,
2858 bool typename_keyword_p,
2859 bool check_dependency_p,
2860 bool type_p)
2862 bool success = false;
2863 tree access_check = NULL_TREE;
2864 ptrdiff_t start;
2865 cp_token* token;
2867 /* If the next token corresponds to a nested name specifier, there
2868 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
2869 false, it may have been true before, in which case something
2870 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2871 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
2872 CHECK_DEPENDENCY_P is false, we have to fall through into the
2873 main loop. */
2874 if (check_dependency_p
2875 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
2877 cp_parser_pre_parsed_nested_name_specifier (parser);
2878 return parser->scope;
2881 /* Remember where the nested-name-specifier starts. */
2882 if (cp_parser_parsing_tentatively (parser)
2883 && !cp_parser_committed_to_tentative_parse (parser))
2885 token = cp_lexer_peek_token (parser->lexer);
2886 start = cp_lexer_token_difference (parser->lexer,
2887 parser->lexer->first_token,
2888 token);
2890 else
2891 start = -1;
2893 push_deferring_access_checks (dk_deferred);
2895 while (true)
2897 tree new_scope;
2898 tree old_scope;
2899 tree saved_qualifying_scope;
2900 bool template_keyword_p;
2902 /* Spot cases that cannot be the beginning of a
2903 nested-name-specifier. */
2904 token = cp_lexer_peek_token (parser->lexer);
2906 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
2907 the already parsed nested-name-specifier. */
2908 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2910 /* Grab the nested-name-specifier and continue the loop. */
2911 cp_parser_pre_parsed_nested_name_specifier (parser);
2912 success = true;
2913 continue;
2916 /* Spot cases that cannot be the beginning of a
2917 nested-name-specifier. On the second and subsequent times
2918 through the loop, we look for the `template' keyword. */
2919 if (success && token->keyword == RID_TEMPLATE)
2921 /* A template-id can start a nested-name-specifier. */
2922 else if (token->type == CPP_TEMPLATE_ID)
2924 else
2926 /* If the next token is not an identifier, then it is
2927 definitely not a class-or-namespace-name. */
2928 if (token->type != CPP_NAME)
2929 break;
2930 /* If the following token is neither a `<' (to begin a
2931 template-id), nor a `::', then we are not looking at a
2932 nested-name-specifier. */
2933 token = cp_lexer_peek_nth_token (parser->lexer, 2);
2934 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
2935 break;
2938 /* The nested-name-specifier is optional, so we parse
2939 tentatively. */
2940 cp_parser_parse_tentatively (parser);
2942 /* Look for the optional `template' keyword, if this isn't the
2943 first time through the loop. */
2944 if (success)
2945 template_keyword_p = cp_parser_optional_template_keyword (parser);
2946 else
2947 template_keyword_p = false;
2949 /* Save the old scope since the name lookup we are about to do
2950 might destroy it. */
2951 old_scope = parser->scope;
2952 saved_qualifying_scope = parser->qualifying_scope;
2953 /* Parse the qualifying entity. */
2954 new_scope
2955 = cp_parser_class_or_namespace_name (parser,
2956 typename_keyword_p,
2957 template_keyword_p,
2958 check_dependency_p,
2959 type_p);
2960 /* Look for the `::' token. */
2961 cp_parser_require (parser, CPP_SCOPE, "`::'");
2963 /* If we found what we wanted, we keep going; otherwise, we're
2964 done. */
2965 if (!cp_parser_parse_definitely (parser))
2967 bool error_p = false;
2969 /* Restore the OLD_SCOPE since it was valid before the
2970 failed attempt at finding the last
2971 class-or-namespace-name. */
2972 parser->scope = old_scope;
2973 parser->qualifying_scope = saved_qualifying_scope;
2974 /* If the next token is an identifier, and the one after
2975 that is a `::', then any valid interpretation would have
2976 found a class-or-namespace-name. */
2977 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2978 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2979 == CPP_SCOPE)
2980 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
2981 != CPP_COMPL))
2983 token = cp_lexer_consume_token (parser->lexer);
2984 if (!error_p)
2986 tree decl;
2988 decl = cp_parser_lookup_name_simple (parser, token->value);
2989 if (TREE_CODE (decl) == TEMPLATE_DECL)
2990 error ("`%D' used without template parameters",
2991 decl);
2992 else if (parser->scope)
2994 if (TYPE_P (parser->scope))
2995 error ("`%T::%D' is not a class-name or "
2996 "namespace-name",
2997 parser->scope, token->value);
2998 else
2999 error ("`%D::%D' is not a class-name or "
3000 "namespace-name",
3001 parser->scope, token->value);
3003 else
3004 error ("`%D' is not a class-name or namespace-name",
3005 token->value);
3006 parser->scope = NULL_TREE;
3007 error_p = true;
3008 /* Treat this as a successful nested-name-specifier
3009 due to:
3011 [basic.lookup.qual]
3013 If the name found is not a class-name (clause
3014 _class_) or namespace-name (_namespace.def_), the
3015 program is ill-formed. */
3016 success = true;
3018 cp_lexer_consume_token (parser->lexer);
3020 break;
3023 /* We've found one valid nested-name-specifier. */
3024 success = true;
3025 /* Make sure we look in the right scope the next time through
3026 the loop. */
3027 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3028 ? TREE_TYPE (new_scope)
3029 : new_scope);
3030 /* If it is a class scope, try to complete it; we are about to
3031 be looking up names inside the class. */
3032 if (TYPE_P (parser->scope)
3033 /* Since checking types for dependency can be expensive,
3034 avoid doing it if the type is already complete. */
3035 && !COMPLETE_TYPE_P (parser->scope)
3036 /* Do not try to complete dependent types. */
3037 && !dependent_type_p (parser->scope))
3038 complete_type (parser->scope);
3041 /* Retrieve any deferred checks. Do not pop this access checks yet
3042 so the memory will not be reclaimed during token replacing below. */
3043 access_check = get_deferred_access_checks ();
3045 /* If parsing tentatively, replace the sequence of tokens that makes
3046 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3047 token. That way, should we re-parse the token stream, we will
3048 not have to repeat the effort required to do the parse, nor will
3049 we issue duplicate error messages. */
3050 if (success && start >= 0)
3052 /* Find the token that corresponds to the start of the
3053 template-id. */
3054 token = cp_lexer_advance_token (parser->lexer,
3055 parser->lexer->first_token,
3056 start);
3058 /* Reset the contents of the START token. */
3059 token->type = CPP_NESTED_NAME_SPECIFIER;
3060 token->value = build_tree_list (access_check, parser->scope);
3061 TREE_TYPE (token->value) = parser->qualifying_scope;
3062 token->keyword = RID_MAX;
3063 /* Purge all subsequent tokens. */
3064 cp_lexer_purge_tokens_after (parser->lexer, token);
3067 pop_deferring_access_checks ();
3068 return success ? parser->scope : NULL_TREE;
3071 /* Parse a nested-name-specifier. See
3072 cp_parser_nested_name_specifier_opt for details. This function
3073 behaves identically, except that it will an issue an error if no
3074 nested-name-specifier is present, and it will return
3075 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3076 is present. */
3078 static tree
3079 cp_parser_nested_name_specifier (cp_parser *parser,
3080 bool typename_keyword_p,
3081 bool check_dependency_p,
3082 bool type_p)
3084 tree scope;
3086 /* Look for the nested-name-specifier. */
3087 scope = cp_parser_nested_name_specifier_opt (parser,
3088 typename_keyword_p,
3089 check_dependency_p,
3090 type_p);
3091 /* If it was not present, issue an error message. */
3092 if (!scope)
3094 cp_parser_error (parser, "expected nested-name-specifier");
3095 parser->scope = NULL_TREE;
3096 return error_mark_node;
3099 return scope;
3102 /* Parse a class-or-namespace-name.
3104 class-or-namespace-name:
3105 class-name
3106 namespace-name
3108 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3109 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3110 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3111 TYPE_P is TRUE iff the next name should be taken as a class-name,
3112 even the same name is declared to be another entity in the same
3113 scope.
3115 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3116 specified by the class-or-namespace-name. If neither is found the
3117 ERROR_MARK_NODE is returned. */
3119 static tree
3120 cp_parser_class_or_namespace_name (cp_parser *parser,
3121 bool typename_keyword_p,
3122 bool template_keyword_p,
3123 bool check_dependency_p,
3124 bool type_p)
3126 tree saved_scope;
3127 tree saved_qualifying_scope;
3128 tree saved_object_scope;
3129 tree scope;
3130 bool only_class_p;
3132 /* Before we try to parse the class-name, we must save away the
3133 current PARSER->SCOPE since cp_parser_class_name will destroy
3134 it. */
3135 saved_scope = parser->scope;
3136 saved_qualifying_scope = parser->qualifying_scope;
3137 saved_object_scope = parser->object_scope;
3138 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3139 there is no need to look for a namespace-name. */
3140 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3141 if (!only_class_p)
3142 cp_parser_parse_tentatively (parser);
3143 scope = cp_parser_class_name (parser,
3144 typename_keyword_p,
3145 template_keyword_p,
3146 type_p,
3147 check_dependency_p,
3148 /*class_head_p=*/false);
3149 /* If that didn't work, try for a namespace-name. */
3150 if (!only_class_p && !cp_parser_parse_definitely (parser))
3152 /* Restore the saved scope. */
3153 parser->scope = saved_scope;
3154 parser->qualifying_scope = saved_qualifying_scope;
3155 parser->object_scope = saved_object_scope;
3156 /* If we are not looking at an identifier followed by the scope
3157 resolution operator, then this is not part of a
3158 nested-name-specifier. (Note that this function is only used
3159 to parse the components of a nested-name-specifier.) */
3160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3161 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3162 return error_mark_node;
3163 scope = cp_parser_namespace_name (parser);
3166 return scope;
3169 /* Parse a postfix-expression.
3171 postfix-expression:
3172 primary-expression
3173 postfix-expression [ expression ]
3174 postfix-expression ( expression-list [opt] )
3175 simple-type-specifier ( expression-list [opt] )
3176 typename :: [opt] nested-name-specifier identifier
3177 ( expression-list [opt] )
3178 typename :: [opt] nested-name-specifier template [opt] template-id
3179 ( expression-list [opt] )
3180 postfix-expression . template [opt] id-expression
3181 postfix-expression -> template [opt] id-expression
3182 postfix-expression . pseudo-destructor-name
3183 postfix-expression -> pseudo-destructor-name
3184 postfix-expression ++
3185 postfix-expression --
3186 dynamic_cast < type-id > ( expression )
3187 static_cast < type-id > ( expression )
3188 reinterpret_cast < type-id > ( expression )
3189 const_cast < type-id > ( expression )
3190 typeid ( expression )
3191 typeid ( type-id )
3193 GNU Extension:
3195 postfix-expression:
3196 ( type-id ) { initializer-list , [opt] }
3198 This extension is a GNU version of the C99 compound-literal
3199 construct. (The C99 grammar uses `type-name' instead of `type-id',
3200 but they are essentially the same concept.)
3202 If ADDRESS_P is true, the postfix expression is the operand of the
3203 `&' operator.
3205 Returns a representation of the expression. */
3207 static tree
3208 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3210 cp_token *token;
3211 enum rid keyword;
3212 cp_id_kind idk = CP_ID_KIND_NONE;
3213 tree postfix_expression = NULL_TREE;
3214 /* Non-NULL only if the current postfix-expression can be used to
3215 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3216 class used to qualify the member. */
3217 tree qualifying_class = NULL_TREE;
3219 /* Peek at the next token. */
3220 token = cp_lexer_peek_token (parser->lexer);
3221 /* Some of the productions are determined by keywords. */
3222 keyword = token->keyword;
3223 switch (keyword)
3225 case RID_DYNCAST:
3226 case RID_STATCAST:
3227 case RID_REINTCAST:
3228 case RID_CONSTCAST:
3230 tree type;
3231 tree expression;
3232 const char *saved_message;
3234 /* All of these can be handled in the same way from the point
3235 of view of parsing. Begin by consuming the token
3236 identifying the cast. */
3237 cp_lexer_consume_token (parser->lexer);
3239 /* New types cannot be defined in the cast. */
3240 saved_message = parser->type_definition_forbidden_message;
3241 parser->type_definition_forbidden_message
3242 = "types may not be defined in casts";
3244 /* Look for the opening `<'. */
3245 cp_parser_require (parser, CPP_LESS, "`<'");
3246 /* Parse the type to which we are casting. */
3247 type = cp_parser_type_id (parser);
3248 /* Look for the closing `>'. */
3249 cp_parser_require (parser, CPP_GREATER, "`>'");
3250 /* Restore the old message. */
3251 parser->type_definition_forbidden_message = saved_message;
3253 /* And the expression which is being cast. */
3254 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3255 expression = cp_parser_expression (parser);
3256 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3258 /* Only type conversions to integral or enumeration types
3259 can be used in constant-expressions. */
3260 if (parser->constant_expression_p
3261 && !dependent_type_p (type)
3262 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3264 if (!parser->allow_non_constant_expression_p)
3265 return (cp_parser_non_constant_expression
3266 ("a cast to a type other than an integral or "
3267 "enumeration type"));
3268 parser->non_constant_expression_p = true;
3271 switch (keyword)
3273 case RID_DYNCAST:
3274 postfix_expression
3275 = build_dynamic_cast (type, expression);
3276 break;
3277 case RID_STATCAST:
3278 postfix_expression
3279 = build_static_cast (type, expression);
3280 break;
3281 case RID_REINTCAST:
3282 postfix_expression
3283 = build_reinterpret_cast (type, expression);
3284 break;
3285 case RID_CONSTCAST:
3286 postfix_expression
3287 = build_const_cast (type, expression);
3288 break;
3289 default:
3290 abort ();
3293 break;
3295 case RID_TYPEID:
3297 tree type;
3298 const char *saved_message;
3300 /* Consume the `typeid' token. */
3301 cp_lexer_consume_token (parser->lexer);
3302 /* Look for the `(' token. */
3303 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3304 /* Types cannot be defined in a `typeid' expression. */
3305 saved_message = parser->type_definition_forbidden_message;
3306 parser->type_definition_forbidden_message
3307 = "types may not be defined in a `typeid\' expression";
3308 /* We can't be sure yet whether we're looking at a type-id or an
3309 expression. */
3310 cp_parser_parse_tentatively (parser);
3311 /* Try a type-id first. */
3312 type = cp_parser_type_id (parser);
3313 /* Look for the `)' token. Otherwise, we can't be sure that
3314 we're not looking at an expression: consider `typeid (int
3315 (3))', for example. */
3316 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3317 /* If all went well, simply lookup the type-id. */
3318 if (cp_parser_parse_definitely (parser))
3319 postfix_expression = get_typeid (type);
3320 /* Otherwise, fall back to the expression variant. */
3321 else
3323 tree expression;
3325 /* Look for an expression. */
3326 expression = cp_parser_expression (parser);
3327 /* Compute its typeid. */
3328 postfix_expression = build_typeid (expression);
3329 /* Look for the `)' token. */
3330 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3333 /* Restore the saved message. */
3334 parser->type_definition_forbidden_message = saved_message;
3336 break;
3338 case RID_TYPENAME:
3340 bool template_p = false;
3341 tree id;
3342 tree type;
3344 /* Consume the `typename' token. */
3345 cp_lexer_consume_token (parser->lexer);
3346 /* Look for the optional `::' operator. */
3347 cp_parser_global_scope_opt (parser,
3348 /*current_scope_valid_p=*/false);
3349 /* Look for the nested-name-specifier. */
3350 cp_parser_nested_name_specifier (parser,
3351 /*typename_keyword_p=*/true,
3352 /*check_dependency_p=*/true,
3353 /*type_p=*/true);
3354 /* Look for the optional `template' keyword. */
3355 template_p = cp_parser_optional_template_keyword (parser);
3356 /* We don't know whether we're looking at a template-id or an
3357 identifier. */
3358 cp_parser_parse_tentatively (parser);
3359 /* Try a template-id. */
3360 id = cp_parser_template_id (parser, template_p,
3361 /*check_dependency_p=*/true);
3362 /* If that didn't work, try an identifier. */
3363 if (!cp_parser_parse_definitely (parser))
3364 id = cp_parser_identifier (parser);
3365 /* Create a TYPENAME_TYPE to represent the type to which the
3366 functional cast is being performed. */
3367 type = make_typename_type (parser->scope, id,
3368 /*complain=*/1);
3370 postfix_expression = cp_parser_functional_cast (parser, type);
3372 break;
3374 default:
3376 tree type;
3378 /* If the next thing is a simple-type-specifier, we may be
3379 looking at a functional cast. We could also be looking at
3380 an id-expression. So, we try the functional cast, and if
3381 that doesn't work we fall back to the primary-expression. */
3382 cp_parser_parse_tentatively (parser);
3383 /* Look for the simple-type-specifier. */
3384 type = cp_parser_simple_type_specifier (parser,
3385 CP_PARSER_FLAGS_NONE,
3386 /*identifier_p=*/false);
3387 /* Parse the cast itself. */
3388 if (!cp_parser_error_occurred (parser))
3389 postfix_expression
3390 = cp_parser_functional_cast (parser, type);
3391 /* If that worked, we're done. */
3392 if (cp_parser_parse_definitely (parser))
3393 break;
3395 /* If the functional-cast didn't work out, try a
3396 compound-literal. */
3397 if (cp_parser_allow_gnu_extensions_p (parser)
3398 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3400 tree initializer_list = NULL_TREE;
3402 cp_parser_parse_tentatively (parser);
3403 /* Consume the `('. */
3404 cp_lexer_consume_token (parser->lexer);
3405 /* Parse the type. */
3406 type = cp_parser_type_id (parser);
3407 /* Look for the `)'. */
3408 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3409 /* Look for the `{'. */
3410 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3411 /* If things aren't going well, there's no need to
3412 keep going. */
3413 if (!cp_parser_error_occurred (parser))
3415 bool non_constant_p;
3416 /* Parse the initializer-list. */
3417 initializer_list
3418 = cp_parser_initializer_list (parser, &non_constant_p);
3419 /* Allow a trailing `,'. */
3420 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3421 cp_lexer_consume_token (parser->lexer);
3422 /* Look for the final `}'. */
3423 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3425 /* If that worked, we're definitely looking at a
3426 compound-literal expression. */
3427 if (cp_parser_parse_definitely (parser))
3429 /* Warn the user that a compound literal is not
3430 allowed in standard C++. */
3431 if (pedantic)
3432 pedwarn ("ISO C++ forbids compound-literals");
3433 /* Form the representation of the compound-literal. */
3434 postfix_expression
3435 = finish_compound_literal (type, initializer_list);
3436 break;
3440 /* It must be a primary-expression. */
3441 postfix_expression = cp_parser_primary_expression (parser,
3442 &idk,
3443 &qualifying_class);
3445 break;
3448 /* If we were avoiding committing to the processing of a
3449 qualified-id until we knew whether or not we had a
3450 pointer-to-member, we now know. */
3451 if (qualifying_class)
3453 bool done;
3455 /* Peek at the next token. */
3456 token = cp_lexer_peek_token (parser->lexer);
3457 done = (token->type != CPP_OPEN_SQUARE
3458 && token->type != CPP_OPEN_PAREN
3459 && token->type != CPP_DOT
3460 && token->type != CPP_DEREF
3461 && token->type != CPP_PLUS_PLUS
3462 && token->type != CPP_MINUS_MINUS);
3464 postfix_expression = finish_qualified_id_expr (qualifying_class,
3465 postfix_expression,
3466 done,
3467 address_p);
3468 if (done)
3469 return postfix_expression;
3472 /* Keep looping until the postfix-expression is complete. */
3473 while (true)
3475 if (idk == CP_ID_KIND_UNQUALIFIED
3476 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3477 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3478 /* It is not a Koenig lookup function call. */
3479 postfix_expression
3480 = unqualified_name_lookup_error (postfix_expression);
3482 /* Peek at the next token. */
3483 token = cp_lexer_peek_token (parser->lexer);
3485 switch (token->type)
3487 case CPP_OPEN_SQUARE:
3488 /* postfix-expression [ expression ] */
3490 tree index;
3492 /* Consume the `[' token. */
3493 cp_lexer_consume_token (parser->lexer);
3494 /* Parse the index expression. */
3495 index = cp_parser_expression (parser);
3496 /* Look for the closing `]'. */
3497 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3499 /* Build the ARRAY_REF. */
3500 postfix_expression
3501 = grok_array_decl (postfix_expression, index);
3502 idk = CP_ID_KIND_NONE;
3504 break;
3506 case CPP_OPEN_PAREN:
3507 /* postfix-expression ( expression-list [opt] ) */
3509 tree args = (cp_parser_parenthesized_expression_list
3510 (parser, false, /*non_constant_p=*/NULL));
3512 if (args == error_mark_node)
3514 postfix_expression = error_mark_node;
3515 break;
3518 /* Function calls are not permitted in
3519 constant-expressions. */
3520 if (parser->constant_expression_p)
3522 if (!parser->allow_non_constant_expression_p)
3523 return cp_parser_non_constant_expression ("a function call");
3524 parser->non_constant_expression_p = true;
3527 if (idk == CP_ID_KIND_UNQUALIFIED)
3529 if (args
3530 && (is_overloaded_fn (postfix_expression)
3531 || DECL_P (postfix_expression)
3532 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3533 postfix_expression
3534 = perform_koenig_lookup (postfix_expression, args);
3535 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3536 postfix_expression
3537 = unqualified_fn_lookup_error (postfix_expression);
3540 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3542 tree instance = TREE_OPERAND (postfix_expression, 0);
3543 tree fn = TREE_OPERAND (postfix_expression, 1);
3545 if (processing_template_decl
3546 && (type_dependent_expression_p (instance)
3547 || (!BASELINK_P (fn)
3548 && TREE_CODE (fn) != FIELD_DECL)
3549 || type_dependent_expression_p (fn)
3550 || any_type_dependent_arguments_p (args)))
3552 postfix_expression
3553 = build_min_nt (CALL_EXPR, postfix_expression, args);
3554 break;
3557 postfix_expression
3558 = (build_new_method_call
3559 (instance, fn, args, NULL_TREE,
3560 (idk == CP_ID_KIND_QUALIFIED
3561 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3563 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3564 || TREE_CODE (postfix_expression) == MEMBER_REF
3565 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3566 postfix_expression = (build_offset_ref_call_from_tree
3567 (postfix_expression, args));
3568 else if (idk == CP_ID_KIND_QUALIFIED)
3569 /* A call to a static class member, or a namespace-scope
3570 function. */
3571 postfix_expression
3572 = finish_call_expr (postfix_expression, args,
3573 /*disallow_virtual=*/true);
3574 else
3575 /* All other function calls. */
3576 postfix_expression
3577 = finish_call_expr (postfix_expression, args,
3578 /*disallow_virtual=*/false);
3580 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3581 idk = CP_ID_KIND_NONE;
3583 break;
3585 case CPP_DOT:
3586 case CPP_DEREF:
3587 /* postfix-expression . template [opt] id-expression
3588 postfix-expression . pseudo-destructor-name
3589 postfix-expression -> template [opt] id-expression
3590 postfix-expression -> pseudo-destructor-name */
3592 tree name;
3593 bool dependent_p;
3594 bool template_p;
3595 tree scope = NULL_TREE;
3597 /* If this is a `->' operator, dereference the pointer. */
3598 if (token->type == CPP_DEREF)
3599 postfix_expression = build_x_arrow (postfix_expression);
3600 /* Check to see whether or not the expression is
3601 type-dependent. */
3602 dependent_p = type_dependent_expression_p (postfix_expression);
3603 /* The identifier following the `->' or `.' is not
3604 qualified. */
3605 parser->scope = NULL_TREE;
3606 parser->qualifying_scope = NULL_TREE;
3607 parser->object_scope = NULL_TREE;
3608 idk = CP_ID_KIND_NONE;
3609 /* Enter the scope corresponding to the type of the object
3610 given by the POSTFIX_EXPRESSION. */
3611 if (!dependent_p
3612 && TREE_TYPE (postfix_expression) != NULL_TREE)
3614 scope = TREE_TYPE (postfix_expression);
3615 /* According to the standard, no expression should
3616 ever have reference type. Unfortunately, we do not
3617 currently match the standard in this respect in
3618 that our internal representation of an expression
3619 may have reference type even when the standard says
3620 it does not. Therefore, we have to manually obtain
3621 the underlying type here. */
3622 scope = non_reference (scope);
3623 /* The type of the POSTFIX_EXPRESSION must be
3624 complete. */
3625 scope = complete_type_or_else (scope, NULL_TREE);
3626 /* Let the name lookup machinery know that we are
3627 processing a class member access expression. */
3628 parser->context->object_type = scope;
3629 /* If something went wrong, we want to be able to
3630 discern that case, as opposed to the case where
3631 there was no SCOPE due to the type of expression
3632 being dependent. */
3633 if (!scope)
3634 scope = error_mark_node;
3637 /* Consume the `.' or `->' operator. */
3638 cp_lexer_consume_token (parser->lexer);
3639 /* If the SCOPE is not a scalar type, we are looking at an
3640 ordinary class member access expression, rather than a
3641 pseudo-destructor-name. */
3642 if (!scope || !SCALAR_TYPE_P (scope))
3644 template_p = cp_parser_optional_template_keyword (parser);
3645 /* Parse the id-expression. */
3646 name = cp_parser_id_expression (parser,
3647 template_p,
3648 /*check_dependency_p=*/true,
3649 /*template_p=*/NULL,
3650 /*declarator_p=*/false);
3651 /* In general, build a SCOPE_REF if the member name is
3652 qualified. However, if the name was not dependent
3653 and has already been resolved; there is no need to
3654 build the SCOPE_REF. For example;
3656 struct X { void f(); };
3657 template <typename T> void f(T* t) { t->X::f(); }
3659 Even though "t" is dependent, "X::f" is not and has
3660 been resolved to a BASELINK; there is no need to
3661 include scope information. */
3663 /* But we do need to remember that there was an explicit
3664 scope for virtual function calls. */
3665 if (parser->scope)
3666 idk = CP_ID_KIND_QUALIFIED;
3668 if (name != error_mark_node
3669 && !BASELINK_P (name)
3670 && parser->scope)
3672 name = build_nt (SCOPE_REF, parser->scope, name);
3673 parser->scope = NULL_TREE;
3674 parser->qualifying_scope = NULL_TREE;
3675 parser->object_scope = NULL_TREE;
3677 postfix_expression
3678 = finish_class_member_access_expr (postfix_expression, name);
3680 /* Otherwise, try the pseudo-destructor-name production. */
3681 else
3683 tree s;
3684 tree type;
3686 /* Parse the pseudo-destructor-name. */
3687 cp_parser_pseudo_destructor_name (parser, &s, &type);
3688 /* Form the call. */
3689 postfix_expression
3690 = finish_pseudo_destructor_expr (postfix_expression,
3691 s, TREE_TYPE (type));
3694 /* We no longer need to look up names in the scope of the
3695 object on the left-hand side of the `.' or `->'
3696 operator. */
3697 parser->context->object_type = NULL_TREE;
3699 break;
3701 case CPP_PLUS_PLUS:
3702 /* postfix-expression ++ */
3703 /* Consume the `++' token. */
3704 cp_lexer_consume_token (parser->lexer);
3705 /* Increments may not appear in constant-expressions. */
3706 if (parser->constant_expression_p)
3708 if (!parser->allow_non_constant_expression_p)
3709 return cp_parser_non_constant_expression ("an increment");
3710 parser->non_constant_expression_p = true;
3712 /* Generate a representation for the complete expression. */
3713 postfix_expression
3714 = finish_increment_expr (postfix_expression,
3715 POSTINCREMENT_EXPR);
3716 idk = CP_ID_KIND_NONE;
3717 break;
3719 case CPP_MINUS_MINUS:
3720 /* postfix-expression -- */
3721 /* Consume the `--' token. */
3722 cp_lexer_consume_token (parser->lexer);
3723 /* Decrements may not appear in constant-expressions. */
3724 if (parser->constant_expression_p)
3726 if (!parser->allow_non_constant_expression_p)
3727 return cp_parser_non_constant_expression ("a decrement");
3728 parser->non_constant_expression_p = true;
3730 /* Generate a representation for the complete expression. */
3731 postfix_expression
3732 = finish_increment_expr (postfix_expression,
3733 POSTDECREMENT_EXPR);
3734 idk = CP_ID_KIND_NONE;
3735 break;
3737 default:
3738 return postfix_expression;
3742 /* We should never get here. */
3743 abort ();
3744 return error_mark_node;
3747 /* Parse a parenthesized expression-list.
3749 expression-list:
3750 assignment-expression
3751 expression-list, assignment-expression
3753 attribute-list:
3754 expression-list
3755 identifier
3756 identifier, expression-list
3758 Returns a TREE_LIST. The TREE_VALUE of each node is a
3759 representation of an assignment-expression. Note that a TREE_LIST
3760 is returned even if there is only a single expression in the list.
3761 error_mark_node is returned if the ( and or ) are
3762 missing. NULL_TREE is returned on no expressions. The parentheses
3763 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3764 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3765 indicates whether or not all of the expressions in the list were
3766 constant. */
3768 static tree
3769 cp_parser_parenthesized_expression_list (cp_parser* parser,
3770 bool is_attribute_list,
3771 bool *non_constant_p)
3773 tree expression_list = NULL_TREE;
3774 tree identifier = NULL_TREE;
3776 /* Assume all the expressions will be constant. */
3777 if (non_constant_p)
3778 *non_constant_p = false;
3780 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3781 return error_mark_node;
3783 /* Consume expressions until there are no more. */
3784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3785 while (true)
3787 tree expr;
3789 /* At the beginning of attribute lists, check to see if the
3790 next token is an identifier. */
3791 if (is_attribute_list
3792 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3794 cp_token *token;
3796 /* Consume the identifier. */
3797 token = cp_lexer_consume_token (parser->lexer);
3798 /* Save the identifier. */
3799 identifier = token->value;
3801 else
3803 /* Parse the next assignment-expression. */
3804 if (non_constant_p)
3806 bool expr_non_constant_p;
3807 expr = (cp_parser_constant_expression
3808 (parser, /*allow_non_constant_p=*/true,
3809 &expr_non_constant_p));
3810 if (expr_non_constant_p)
3811 *non_constant_p = true;
3813 else
3814 expr = cp_parser_assignment_expression (parser);
3816 /* Add it to the list. We add error_mark_node
3817 expressions to the list, so that we can still tell if
3818 the correct form for a parenthesized expression-list
3819 is found. That gives better errors. */
3820 expression_list = tree_cons (NULL_TREE, expr, expression_list);
3822 if (expr == error_mark_node)
3823 goto skip_comma;
3826 /* After the first item, attribute lists look the same as
3827 expression lists. */
3828 is_attribute_list = false;
3830 get_comma:;
3831 /* If the next token isn't a `,', then we are done. */
3832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
3833 break;
3835 /* Otherwise, consume the `,' and keep going. */
3836 cp_lexer_consume_token (parser->lexer);
3839 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3841 int ending;
3843 skip_comma:;
3844 /* We try and resync to an unnested comma, as that will give the
3845 user better diagnostics. */
3846 ending = cp_parser_skip_to_closing_parenthesis (parser, true, true);
3847 if (ending < 0)
3848 goto get_comma;
3849 if (!ending)
3850 return error_mark_node;
3853 /* We built up the list in reverse order so we must reverse it now. */
3854 expression_list = nreverse (expression_list);
3855 if (identifier)
3856 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
3858 return expression_list;
3861 /* Parse a pseudo-destructor-name.
3863 pseudo-destructor-name:
3864 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3865 :: [opt] nested-name-specifier template template-id :: ~ type-name
3866 :: [opt] nested-name-specifier [opt] ~ type-name
3868 If either of the first two productions is used, sets *SCOPE to the
3869 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
3870 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
3871 or ERROR_MARK_NODE if no type-name is present. */
3873 static void
3874 cp_parser_pseudo_destructor_name (cp_parser* parser,
3875 tree* scope,
3876 tree* type)
3878 bool nested_name_specifier_p;
3880 /* Look for the optional `::' operator. */
3881 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
3882 /* Look for the optional nested-name-specifier. */
3883 nested_name_specifier_p
3884 = (cp_parser_nested_name_specifier_opt (parser,
3885 /*typename_keyword_p=*/false,
3886 /*check_dependency_p=*/true,
3887 /*type_p=*/false)
3888 != NULL_TREE);
3889 /* Now, if we saw a nested-name-specifier, we might be doing the
3890 second production. */
3891 if (nested_name_specifier_p
3892 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3894 /* Consume the `template' keyword. */
3895 cp_lexer_consume_token (parser->lexer);
3896 /* Parse the template-id. */
3897 cp_parser_template_id (parser,
3898 /*template_keyword_p=*/true,
3899 /*check_dependency_p=*/false);
3900 /* Look for the `::' token. */
3901 cp_parser_require (parser, CPP_SCOPE, "`::'");
3903 /* If the next token is not a `~', then there might be some
3904 additional qualification. */
3905 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
3907 /* Look for the type-name. */
3908 *scope = TREE_TYPE (cp_parser_type_name (parser));
3909 /* Look for the `::' token. */
3910 cp_parser_require (parser, CPP_SCOPE, "`::'");
3912 else
3913 *scope = NULL_TREE;
3915 /* Look for the `~'. */
3916 cp_parser_require (parser, CPP_COMPL, "`~'");
3917 /* Look for the type-name again. We are not responsible for
3918 checking that it matches the first type-name. */
3919 *type = cp_parser_type_name (parser);
3922 /* Parse a unary-expression.
3924 unary-expression:
3925 postfix-expression
3926 ++ cast-expression
3927 -- cast-expression
3928 unary-operator cast-expression
3929 sizeof unary-expression
3930 sizeof ( type-id )
3931 new-expression
3932 delete-expression
3934 GNU Extensions:
3936 unary-expression:
3937 __extension__ cast-expression
3938 __alignof__ unary-expression
3939 __alignof__ ( type-id )
3940 __real__ cast-expression
3941 __imag__ cast-expression
3942 && identifier
3944 ADDRESS_P is true iff the unary-expression is appearing as the
3945 operand of the `&' operator.
3947 Returns a representation of the expression. */
3949 static tree
3950 cp_parser_unary_expression (cp_parser *parser, bool address_p)
3952 cp_token *token;
3953 enum tree_code unary_operator;
3955 /* Peek at the next token. */
3956 token = cp_lexer_peek_token (parser->lexer);
3957 /* Some keywords give away the kind of expression. */
3958 if (token->type == CPP_KEYWORD)
3960 enum rid keyword = token->keyword;
3962 switch (keyword)
3964 case RID_ALIGNOF:
3966 /* Consume the `alignof' token. */
3967 cp_lexer_consume_token (parser->lexer);
3968 /* Parse the operand. */
3969 return finish_alignof (cp_parser_sizeof_operand
3970 (parser, keyword));
3973 case RID_SIZEOF:
3975 tree operand;
3977 /* Consume the `sizeof' token. */
3978 cp_lexer_consume_token (parser->lexer);
3979 /* Parse the operand. */
3980 operand = cp_parser_sizeof_operand (parser, keyword);
3982 /* If the type of the operand cannot be determined build a
3983 SIZEOF_EXPR. */
3984 if (TYPE_P (operand)
3985 ? dependent_type_p (operand)
3986 : type_dependent_expression_p (operand))
3987 return build_min (SIZEOF_EXPR, size_type_node, operand);
3988 /* Otherwise, compute the constant value. */
3989 else
3990 return finish_sizeof (operand);
3993 case RID_NEW:
3994 return cp_parser_new_expression (parser);
3996 case RID_DELETE:
3997 return cp_parser_delete_expression (parser);
3999 case RID_EXTENSION:
4001 /* The saved value of the PEDANTIC flag. */
4002 int saved_pedantic;
4003 tree expr;
4005 /* Save away the PEDANTIC flag. */
4006 cp_parser_extension_opt (parser, &saved_pedantic);
4007 /* Parse the cast-expression. */
4008 expr = cp_parser_simple_cast_expression (parser);
4009 /* Restore the PEDANTIC flag. */
4010 pedantic = saved_pedantic;
4012 return expr;
4015 case RID_REALPART:
4016 case RID_IMAGPART:
4018 tree expression;
4020 /* Consume the `__real__' or `__imag__' token. */
4021 cp_lexer_consume_token (parser->lexer);
4022 /* Parse the cast-expression. */
4023 expression = cp_parser_simple_cast_expression (parser);
4024 /* Create the complete representation. */
4025 return build_x_unary_op ((keyword == RID_REALPART
4026 ? REALPART_EXPR : IMAGPART_EXPR),
4027 expression);
4029 break;
4031 default:
4032 break;
4036 /* Look for the `:: new' and `:: delete', which also signal the
4037 beginning of a new-expression, or delete-expression,
4038 respectively. If the next token is `::', then it might be one of
4039 these. */
4040 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4042 enum rid keyword;
4044 /* See if the token after the `::' is one of the keywords in
4045 which we're interested. */
4046 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4047 /* If it's `new', we have a new-expression. */
4048 if (keyword == RID_NEW)
4049 return cp_parser_new_expression (parser);
4050 /* Similarly, for `delete'. */
4051 else if (keyword == RID_DELETE)
4052 return cp_parser_delete_expression (parser);
4055 /* Look for a unary operator. */
4056 unary_operator = cp_parser_unary_operator (token);
4057 /* The `++' and `--' operators can be handled similarly, even though
4058 they are not technically unary-operators in the grammar. */
4059 if (unary_operator == ERROR_MARK)
4061 if (token->type == CPP_PLUS_PLUS)
4062 unary_operator = PREINCREMENT_EXPR;
4063 else if (token->type == CPP_MINUS_MINUS)
4064 unary_operator = PREDECREMENT_EXPR;
4065 /* Handle the GNU address-of-label extension. */
4066 else if (cp_parser_allow_gnu_extensions_p (parser)
4067 && token->type == CPP_AND_AND)
4069 tree identifier;
4071 /* Consume the '&&' token. */
4072 cp_lexer_consume_token (parser->lexer);
4073 /* Look for the identifier. */
4074 identifier = cp_parser_identifier (parser);
4075 /* Create an expression representing the address. */
4076 return finish_label_address_expr (identifier);
4079 if (unary_operator != ERROR_MARK)
4081 tree cast_expression;
4083 /* Consume the operator token. */
4084 token = cp_lexer_consume_token (parser->lexer);
4085 /* Parse the cast-expression. */
4086 cast_expression
4087 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4088 /* Now, build an appropriate representation. */
4089 switch (unary_operator)
4091 case INDIRECT_REF:
4092 return build_x_indirect_ref (cast_expression, "unary *");
4094 case ADDR_EXPR:
4095 case BIT_NOT_EXPR:
4096 return build_x_unary_op (unary_operator, cast_expression);
4098 case PREINCREMENT_EXPR:
4099 case PREDECREMENT_EXPR:
4100 if (parser->constant_expression_p)
4102 if (!parser->allow_non_constant_expression_p)
4103 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4104 ? "an increment"
4105 : "a decrement");
4106 parser->non_constant_expression_p = true;
4108 /* Fall through. */
4109 case CONVERT_EXPR:
4110 case NEGATE_EXPR:
4111 case TRUTH_NOT_EXPR:
4112 return finish_unary_op_expr (unary_operator, cast_expression);
4114 default:
4115 abort ();
4116 return error_mark_node;
4120 return cp_parser_postfix_expression (parser, address_p);
4123 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4124 unary-operator, the corresponding tree code is returned. */
4126 static enum tree_code
4127 cp_parser_unary_operator (cp_token* token)
4129 switch (token->type)
4131 case CPP_MULT:
4132 return INDIRECT_REF;
4134 case CPP_AND:
4135 return ADDR_EXPR;
4137 case CPP_PLUS:
4138 return CONVERT_EXPR;
4140 case CPP_MINUS:
4141 return NEGATE_EXPR;
4143 case CPP_NOT:
4144 return TRUTH_NOT_EXPR;
4146 case CPP_COMPL:
4147 return BIT_NOT_EXPR;
4149 default:
4150 return ERROR_MARK;
4154 /* Parse a new-expression.
4156 new-expression:
4157 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4158 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4160 Returns a representation of the expression. */
4162 static tree
4163 cp_parser_new_expression (cp_parser* parser)
4165 bool global_scope_p;
4166 tree placement;
4167 tree type;
4168 tree initializer;
4170 /* Look for the optional `::' operator. */
4171 global_scope_p
4172 = (cp_parser_global_scope_opt (parser,
4173 /*current_scope_valid_p=*/false)
4174 != NULL_TREE);
4175 /* Look for the `new' operator. */
4176 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4177 /* There's no easy way to tell a new-placement from the
4178 `( type-id )' construct. */
4179 cp_parser_parse_tentatively (parser);
4180 /* Look for a new-placement. */
4181 placement = cp_parser_new_placement (parser);
4182 /* If that didn't work out, there's no new-placement. */
4183 if (!cp_parser_parse_definitely (parser))
4184 placement = NULL_TREE;
4186 /* If the next token is a `(', then we have a parenthesized
4187 type-id. */
4188 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4190 /* Consume the `('. */
4191 cp_lexer_consume_token (parser->lexer);
4192 /* Parse the type-id. */
4193 type = cp_parser_type_id (parser);
4194 /* Look for the closing `)'. */
4195 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4197 /* Otherwise, there must be a new-type-id. */
4198 else
4199 type = cp_parser_new_type_id (parser);
4201 /* If the next token is a `(', then we have a new-initializer. */
4202 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4203 initializer = cp_parser_new_initializer (parser);
4204 else
4205 initializer = NULL_TREE;
4207 /* Create a representation of the new-expression. */
4208 return build_new (placement, type, initializer, global_scope_p);
4211 /* Parse a new-placement.
4213 new-placement:
4214 ( expression-list )
4216 Returns the same representation as for an expression-list. */
4218 static tree
4219 cp_parser_new_placement (cp_parser* parser)
4221 tree expression_list;
4223 /* Parse the expression-list. */
4224 expression_list = (cp_parser_parenthesized_expression_list
4225 (parser, false, /*non_constant_p=*/NULL));
4227 return expression_list;
4230 /* Parse a new-type-id.
4232 new-type-id:
4233 type-specifier-seq new-declarator [opt]
4235 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4236 and whose TREE_VALUE is the new-declarator. */
4238 static tree
4239 cp_parser_new_type_id (cp_parser* parser)
4241 tree type_specifier_seq;
4242 tree declarator;
4243 const char *saved_message;
4245 /* The type-specifier sequence must not contain type definitions.
4246 (It cannot contain declarations of new types either, but if they
4247 are not definitions we will catch that because they are not
4248 complete.) */
4249 saved_message = parser->type_definition_forbidden_message;
4250 parser->type_definition_forbidden_message
4251 = "types may not be defined in a new-type-id";
4252 /* Parse the type-specifier-seq. */
4253 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4254 /* Restore the old message. */
4255 parser->type_definition_forbidden_message = saved_message;
4256 /* Parse the new-declarator. */
4257 declarator = cp_parser_new_declarator_opt (parser);
4259 return build_tree_list (type_specifier_seq, declarator);
4262 /* Parse an (optional) new-declarator.
4264 new-declarator:
4265 ptr-operator new-declarator [opt]
4266 direct-new-declarator
4268 Returns a representation of the declarator. See
4269 cp_parser_declarator for the representations used. */
4271 static tree
4272 cp_parser_new_declarator_opt (cp_parser* parser)
4274 enum tree_code code;
4275 tree type;
4276 tree cv_qualifier_seq;
4278 /* We don't know if there's a ptr-operator next, or not. */
4279 cp_parser_parse_tentatively (parser);
4280 /* Look for a ptr-operator. */
4281 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4282 /* If that worked, look for more new-declarators. */
4283 if (cp_parser_parse_definitely (parser))
4285 tree declarator;
4287 /* Parse another optional declarator. */
4288 declarator = cp_parser_new_declarator_opt (parser);
4290 /* Create the representation of the declarator. */
4291 if (code == INDIRECT_REF)
4292 declarator = make_pointer_declarator (cv_qualifier_seq,
4293 declarator);
4294 else
4295 declarator = make_reference_declarator (cv_qualifier_seq,
4296 declarator);
4298 /* Handle the pointer-to-member case. */
4299 if (type)
4300 declarator = build_nt (SCOPE_REF, type, declarator);
4302 return declarator;
4305 /* If the next token is a `[', there is a direct-new-declarator. */
4306 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4307 return cp_parser_direct_new_declarator (parser);
4309 return NULL_TREE;
4312 /* Parse a direct-new-declarator.
4314 direct-new-declarator:
4315 [ expression ]
4316 direct-new-declarator [constant-expression]
4318 Returns an ARRAY_REF, following the same conventions as are
4319 documented for cp_parser_direct_declarator. */
4321 static tree
4322 cp_parser_direct_new_declarator (cp_parser* parser)
4324 tree declarator = NULL_TREE;
4326 while (true)
4328 tree expression;
4330 /* Look for the opening `['. */
4331 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4332 /* The first expression is not required to be constant. */
4333 if (!declarator)
4335 expression = cp_parser_expression (parser);
4336 /* The standard requires that the expression have integral
4337 type. DR 74 adds enumeration types. We believe that the
4338 real intent is that these expressions be handled like the
4339 expression in a `switch' condition, which also allows
4340 classes with a single conversion to integral or
4341 enumeration type. */
4342 if (!processing_template_decl)
4344 expression
4345 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4346 expression,
4347 /*complain=*/true);
4348 if (!expression)
4350 error ("expression in new-declarator must have integral or enumeration type");
4351 expression = error_mark_node;
4355 /* But all the other expressions must be. */
4356 else
4357 expression
4358 = cp_parser_constant_expression (parser,
4359 /*allow_non_constant=*/false,
4360 NULL);
4361 /* Look for the closing `]'. */
4362 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4364 /* Add this bound to the declarator. */
4365 declarator = build_nt (ARRAY_REF, declarator, expression);
4367 /* If the next token is not a `[', then there are no more
4368 bounds. */
4369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4370 break;
4373 return declarator;
4376 /* Parse a new-initializer.
4378 new-initializer:
4379 ( expression-list [opt] )
4381 Returns a representation of the expression-list. If there is no
4382 expression-list, VOID_ZERO_NODE is returned. */
4384 static tree
4385 cp_parser_new_initializer (cp_parser* parser)
4387 tree expression_list;
4389 expression_list = (cp_parser_parenthesized_expression_list
4390 (parser, false, /*non_constant_p=*/NULL));
4391 if (!expression_list)
4392 expression_list = void_zero_node;
4394 return expression_list;
4397 /* Parse a delete-expression.
4399 delete-expression:
4400 :: [opt] delete cast-expression
4401 :: [opt] delete [ ] cast-expression
4403 Returns a representation of the expression. */
4405 static tree
4406 cp_parser_delete_expression (cp_parser* parser)
4408 bool global_scope_p;
4409 bool array_p;
4410 tree expression;
4412 /* Look for the optional `::' operator. */
4413 global_scope_p
4414 = (cp_parser_global_scope_opt (parser,
4415 /*current_scope_valid_p=*/false)
4416 != NULL_TREE);
4417 /* Look for the `delete' keyword. */
4418 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4419 /* See if the array syntax is in use. */
4420 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4422 /* Consume the `[' token. */
4423 cp_lexer_consume_token (parser->lexer);
4424 /* Look for the `]' token. */
4425 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4426 /* Remember that this is the `[]' construct. */
4427 array_p = true;
4429 else
4430 array_p = false;
4432 /* Parse the cast-expression. */
4433 expression = cp_parser_simple_cast_expression (parser);
4435 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4438 /* Parse a cast-expression.
4440 cast-expression:
4441 unary-expression
4442 ( type-id ) cast-expression
4444 Returns a representation of the expression. */
4446 static tree
4447 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4449 /* If it's a `(', then we might be looking at a cast. */
4450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4452 tree type = NULL_TREE;
4453 tree expr = NULL_TREE;
4454 bool compound_literal_p;
4455 const char *saved_message;
4457 /* There's no way to know yet whether or not this is a cast.
4458 For example, `(int (3))' is a unary-expression, while `(int)
4459 3' is a cast. So, we resort to parsing tentatively. */
4460 cp_parser_parse_tentatively (parser);
4461 /* Types may not be defined in a cast. */
4462 saved_message = parser->type_definition_forbidden_message;
4463 parser->type_definition_forbidden_message
4464 = "types may not be defined in casts";
4465 /* Consume the `('. */
4466 cp_lexer_consume_token (parser->lexer);
4467 /* A very tricky bit is that `(struct S) { 3 }' is a
4468 compound-literal (which we permit in C++ as an extension).
4469 But, that construct is not a cast-expression -- it is a
4470 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4471 is legal; if the compound-literal were a cast-expression,
4472 you'd need an extra set of parentheses.) But, if we parse
4473 the type-id, and it happens to be a class-specifier, then we
4474 will commit to the parse at that point, because we cannot
4475 undo the action that is done when creating a new class. So,
4476 then we cannot back up and do a postfix-expression.
4478 Therefore, we scan ahead to the closing `)', and check to see
4479 if the token after the `)' is a `{'. If so, we are not
4480 looking at a cast-expression.
4482 Save tokens so that we can put them back. */
4483 cp_lexer_save_tokens (parser->lexer);
4484 /* Skip tokens until the next token is a closing parenthesis.
4485 If we find the closing `)', and the next token is a `{', then
4486 we are looking at a compound-literal. */
4487 compound_literal_p
4488 = (cp_parser_skip_to_closing_parenthesis (parser, false, false)
4489 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4490 /* Roll back the tokens we skipped. */
4491 cp_lexer_rollback_tokens (parser->lexer);
4492 /* If we were looking at a compound-literal, simulate an error
4493 so that the call to cp_parser_parse_definitely below will
4494 fail. */
4495 if (compound_literal_p)
4496 cp_parser_simulate_error (parser);
4497 else
4499 /* Look for the type-id. */
4500 type = cp_parser_type_id (parser);
4501 /* Look for the closing `)'. */
4502 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4505 /* Restore the saved message. */
4506 parser->type_definition_forbidden_message = saved_message;
4508 /* If ok so far, parse the dependent expression. We cannot be
4509 sure it is a cast. Consider `(T ())'. It is a parenthesized
4510 ctor of T, but looks like a cast to function returning T
4511 without a dependent expression. */
4512 if (!cp_parser_error_occurred (parser))
4513 expr = cp_parser_simple_cast_expression (parser);
4515 if (cp_parser_parse_definitely (parser))
4517 /* Warn about old-style casts, if so requested. */
4518 if (warn_old_style_cast
4519 && !in_system_header
4520 && !VOID_TYPE_P (type)
4521 && current_lang_name != lang_name_c)
4522 warning ("use of old-style cast");
4524 /* Only type conversions to integral or enumeration types
4525 can be used in constant-expressions. */
4526 if (parser->constant_expression_p
4527 && !dependent_type_p (type)
4528 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4530 if (!parser->allow_non_constant_expression_p)
4531 return (cp_parser_non_constant_expression
4532 ("a casts to a type other than an integral or "
4533 "enumeration type"));
4534 parser->non_constant_expression_p = true;
4536 /* Perform the cast. */
4537 expr = build_c_cast (type, expr);
4538 return expr;
4542 /* If we get here, then it's not a cast, so it must be a
4543 unary-expression. */
4544 return cp_parser_unary_expression (parser, address_p);
4547 /* Parse a pm-expression.
4549 pm-expression:
4550 cast-expression
4551 pm-expression .* cast-expression
4552 pm-expression ->* cast-expression
4554 Returns a representation of the expression. */
4556 static tree
4557 cp_parser_pm_expression (cp_parser* parser)
4559 static const cp_parser_token_tree_map map = {
4560 { CPP_DEREF_STAR, MEMBER_REF },
4561 { CPP_DOT_STAR, DOTSTAR_EXPR },
4562 { CPP_EOF, ERROR_MARK }
4565 return cp_parser_binary_expression (parser, map,
4566 cp_parser_simple_cast_expression);
4569 /* Parse a multiplicative-expression.
4571 mulitplicative-expression:
4572 pm-expression
4573 multiplicative-expression * pm-expression
4574 multiplicative-expression / pm-expression
4575 multiplicative-expression % pm-expression
4577 Returns a representation of the expression. */
4579 static tree
4580 cp_parser_multiplicative_expression (cp_parser* parser)
4582 static const cp_parser_token_tree_map map = {
4583 { CPP_MULT, MULT_EXPR },
4584 { CPP_DIV, TRUNC_DIV_EXPR },
4585 { CPP_MOD, TRUNC_MOD_EXPR },
4586 { CPP_EOF, ERROR_MARK }
4589 return cp_parser_binary_expression (parser,
4590 map,
4591 cp_parser_pm_expression);
4594 /* Parse an additive-expression.
4596 additive-expression:
4597 multiplicative-expression
4598 additive-expression + multiplicative-expression
4599 additive-expression - multiplicative-expression
4601 Returns a representation of the expression. */
4603 static tree
4604 cp_parser_additive_expression (cp_parser* parser)
4606 static const cp_parser_token_tree_map map = {
4607 { CPP_PLUS, PLUS_EXPR },
4608 { CPP_MINUS, MINUS_EXPR },
4609 { CPP_EOF, ERROR_MARK }
4612 return cp_parser_binary_expression (parser,
4613 map,
4614 cp_parser_multiplicative_expression);
4617 /* Parse a shift-expression.
4619 shift-expression:
4620 additive-expression
4621 shift-expression << additive-expression
4622 shift-expression >> additive-expression
4624 Returns a representation of the expression. */
4626 static tree
4627 cp_parser_shift_expression (cp_parser* parser)
4629 static const cp_parser_token_tree_map map = {
4630 { CPP_LSHIFT, LSHIFT_EXPR },
4631 { CPP_RSHIFT, RSHIFT_EXPR },
4632 { CPP_EOF, ERROR_MARK }
4635 return cp_parser_binary_expression (parser,
4636 map,
4637 cp_parser_additive_expression);
4640 /* Parse a relational-expression.
4642 relational-expression:
4643 shift-expression
4644 relational-expression < shift-expression
4645 relational-expression > shift-expression
4646 relational-expression <= shift-expression
4647 relational-expression >= shift-expression
4649 GNU Extension:
4651 relational-expression:
4652 relational-expression <? shift-expression
4653 relational-expression >? shift-expression
4655 Returns a representation of the expression. */
4657 static tree
4658 cp_parser_relational_expression (cp_parser* parser)
4660 static const cp_parser_token_tree_map map = {
4661 { CPP_LESS, LT_EXPR },
4662 { CPP_GREATER, GT_EXPR },
4663 { CPP_LESS_EQ, LE_EXPR },
4664 { CPP_GREATER_EQ, GE_EXPR },
4665 { CPP_MIN, MIN_EXPR },
4666 { CPP_MAX, MAX_EXPR },
4667 { CPP_EOF, ERROR_MARK }
4670 return cp_parser_binary_expression (parser,
4671 map,
4672 cp_parser_shift_expression);
4675 /* Parse an equality-expression.
4677 equality-expression:
4678 relational-expression
4679 equality-expression == relational-expression
4680 equality-expression != relational-expression
4682 Returns a representation of the expression. */
4684 static tree
4685 cp_parser_equality_expression (cp_parser* parser)
4687 static const cp_parser_token_tree_map map = {
4688 { CPP_EQ_EQ, EQ_EXPR },
4689 { CPP_NOT_EQ, NE_EXPR },
4690 { CPP_EOF, ERROR_MARK }
4693 return cp_parser_binary_expression (parser,
4694 map,
4695 cp_parser_relational_expression);
4698 /* Parse an and-expression.
4700 and-expression:
4701 equality-expression
4702 and-expression & equality-expression
4704 Returns a representation of the expression. */
4706 static tree
4707 cp_parser_and_expression (cp_parser* parser)
4709 static const cp_parser_token_tree_map map = {
4710 { CPP_AND, BIT_AND_EXPR },
4711 { CPP_EOF, ERROR_MARK }
4714 return cp_parser_binary_expression (parser,
4715 map,
4716 cp_parser_equality_expression);
4719 /* Parse an exclusive-or-expression.
4721 exclusive-or-expression:
4722 and-expression
4723 exclusive-or-expression ^ and-expression
4725 Returns a representation of the expression. */
4727 static tree
4728 cp_parser_exclusive_or_expression (cp_parser* parser)
4730 static const cp_parser_token_tree_map map = {
4731 { CPP_XOR, BIT_XOR_EXPR },
4732 { CPP_EOF, ERROR_MARK }
4735 return cp_parser_binary_expression (parser,
4736 map,
4737 cp_parser_and_expression);
4741 /* Parse an inclusive-or-expression.
4743 inclusive-or-expression:
4744 exclusive-or-expression
4745 inclusive-or-expression | exclusive-or-expression
4747 Returns a representation of the expression. */
4749 static tree
4750 cp_parser_inclusive_or_expression (cp_parser* parser)
4752 static const cp_parser_token_tree_map map = {
4753 { CPP_OR, BIT_IOR_EXPR },
4754 { CPP_EOF, ERROR_MARK }
4757 return cp_parser_binary_expression (parser,
4758 map,
4759 cp_parser_exclusive_or_expression);
4762 /* Parse a logical-and-expression.
4764 logical-and-expression:
4765 inclusive-or-expression
4766 logical-and-expression && inclusive-or-expression
4768 Returns a representation of the expression. */
4770 static tree
4771 cp_parser_logical_and_expression (cp_parser* parser)
4773 static const cp_parser_token_tree_map map = {
4774 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4775 { CPP_EOF, ERROR_MARK }
4778 return cp_parser_binary_expression (parser,
4779 map,
4780 cp_parser_inclusive_or_expression);
4783 /* Parse a logical-or-expression.
4785 logical-or-expression:
4786 logical-and-expression
4787 logical-or-expression || logical-and-expression
4789 Returns a representation of the expression. */
4791 static tree
4792 cp_parser_logical_or_expression (cp_parser* parser)
4794 static const cp_parser_token_tree_map map = {
4795 { CPP_OR_OR, TRUTH_ORIF_EXPR },
4796 { CPP_EOF, ERROR_MARK }
4799 return cp_parser_binary_expression (parser,
4800 map,
4801 cp_parser_logical_and_expression);
4804 /* Parse the `? expression : assignment-expression' part of a
4805 conditional-expression. The LOGICAL_OR_EXPR is the
4806 logical-or-expression that started the conditional-expression.
4807 Returns a representation of the entire conditional-expression.
4809 This routine is used by cp_parser_assignment_expression.
4811 ? expression : assignment-expression
4813 GNU Extensions:
4815 ? : assignment-expression */
4817 static tree
4818 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
4820 tree expr;
4821 tree assignment_expr;
4823 /* Consume the `?' token. */
4824 cp_lexer_consume_token (parser->lexer);
4825 if (cp_parser_allow_gnu_extensions_p (parser)
4826 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
4827 /* Implicit true clause. */
4828 expr = NULL_TREE;
4829 else
4830 /* Parse the expression. */
4831 expr = cp_parser_expression (parser);
4833 /* The next token should be a `:'. */
4834 cp_parser_require (parser, CPP_COLON, "`:'");
4835 /* Parse the assignment-expression. */
4836 assignment_expr = cp_parser_assignment_expression (parser);
4838 /* Build the conditional-expression. */
4839 return build_x_conditional_expr (logical_or_expr,
4840 expr,
4841 assignment_expr);
4844 /* Parse an assignment-expression.
4846 assignment-expression:
4847 conditional-expression
4848 logical-or-expression assignment-operator assignment_expression
4849 throw-expression
4851 Returns a representation for the expression. */
4853 static tree
4854 cp_parser_assignment_expression (cp_parser* parser)
4856 tree expr;
4858 /* If the next token is the `throw' keyword, then we're looking at
4859 a throw-expression. */
4860 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
4861 expr = cp_parser_throw_expression (parser);
4862 /* Otherwise, it must be that we are looking at a
4863 logical-or-expression. */
4864 else
4866 /* Parse the logical-or-expression. */
4867 expr = cp_parser_logical_or_expression (parser);
4868 /* If the next token is a `?' then we're actually looking at a
4869 conditional-expression. */
4870 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
4871 return cp_parser_question_colon_clause (parser, expr);
4872 else
4874 enum tree_code assignment_operator;
4876 /* If it's an assignment-operator, we're using the second
4877 production. */
4878 assignment_operator
4879 = cp_parser_assignment_operator_opt (parser);
4880 if (assignment_operator != ERROR_MARK)
4882 tree rhs;
4884 /* Parse the right-hand side of the assignment. */
4885 rhs = cp_parser_assignment_expression (parser);
4886 /* An assignment may not appear in a
4887 constant-expression. */
4888 if (parser->constant_expression_p)
4890 if (!parser->allow_non_constant_expression_p)
4891 return cp_parser_non_constant_expression ("an assignment");
4892 parser->non_constant_expression_p = true;
4894 /* Build the assignment expression. */
4895 expr = build_x_modify_expr (expr,
4896 assignment_operator,
4897 rhs);
4902 return expr;
4905 /* Parse an (optional) assignment-operator.
4907 assignment-operator: one of
4908 = *= /= %= += -= >>= <<= &= ^= |=
4910 GNU Extension:
4912 assignment-operator: one of
4913 <?= >?=
4915 If the next token is an assignment operator, the corresponding tree
4916 code is returned, and the token is consumed. For example, for
4917 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
4918 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
4919 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
4920 operator, ERROR_MARK is returned. */
4922 static enum tree_code
4923 cp_parser_assignment_operator_opt (cp_parser* parser)
4925 enum tree_code op;
4926 cp_token *token;
4928 /* Peek at the next toen. */
4929 token = cp_lexer_peek_token (parser->lexer);
4931 switch (token->type)
4933 case CPP_EQ:
4934 op = NOP_EXPR;
4935 break;
4937 case CPP_MULT_EQ:
4938 op = MULT_EXPR;
4939 break;
4941 case CPP_DIV_EQ:
4942 op = TRUNC_DIV_EXPR;
4943 break;
4945 case CPP_MOD_EQ:
4946 op = TRUNC_MOD_EXPR;
4947 break;
4949 case CPP_PLUS_EQ:
4950 op = PLUS_EXPR;
4951 break;
4953 case CPP_MINUS_EQ:
4954 op = MINUS_EXPR;
4955 break;
4957 case CPP_RSHIFT_EQ:
4958 op = RSHIFT_EXPR;
4959 break;
4961 case CPP_LSHIFT_EQ:
4962 op = LSHIFT_EXPR;
4963 break;
4965 case CPP_AND_EQ:
4966 op = BIT_AND_EXPR;
4967 break;
4969 case CPP_XOR_EQ:
4970 op = BIT_XOR_EXPR;
4971 break;
4973 case CPP_OR_EQ:
4974 op = BIT_IOR_EXPR;
4975 break;
4977 case CPP_MIN_EQ:
4978 op = MIN_EXPR;
4979 break;
4981 case CPP_MAX_EQ:
4982 op = MAX_EXPR;
4983 break;
4985 default:
4986 /* Nothing else is an assignment operator. */
4987 op = ERROR_MARK;
4990 /* If it was an assignment operator, consume it. */
4991 if (op != ERROR_MARK)
4992 cp_lexer_consume_token (parser->lexer);
4994 return op;
4997 /* Parse an expression.
4999 expression:
5000 assignment-expression
5001 expression , assignment-expression
5003 Returns a representation of the expression. */
5005 static tree
5006 cp_parser_expression (cp_parser* parser)
5008 tree expression = NULL_TREE;
5010 while (true)
5012 tree assignment_expression;
5014 /* Parse the next assignment-expression. */
5015 assignment_expression
5016 = cp_parser_assignment_expression (parser);
5017 /* If this is the first assignment-expression, we can just
5018 save it away. */
5019 if (!expression)
5020 expression = assignment_expression;
5021 else
5022 expression = build_x_compound_expr (expression,
5023 assignment_expression);
5024 /* If the next token is not a comma, then we are done with the
5025 expression. */
5026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5027 break;
5028 /* Consume the `,'. */
5029 cp_lexer_consume_token (parser->lexer);
5030 /* A comma operator cannot appear in a constant-expression. */
5031 if (parser->constant_expression_p)
5033 if (!parser->allow_non_constant_expression_p)
5034 expression
5035 = cp_parser_non_constant_expression ("a comma operator");
5036 parser->non_constant_expression_p = true;
5040 return expression;
5043 /* Parse a constant-expression.
5045 constant-expression:
5046 conditional-expression
5048 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5049 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5050 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5051 is false, NON_CONSTANT_P should be NULL. */
5053 static tree
5054 cp_parser_constant_expression (cp_parser* parser,
5055 bool allow_non_constant_p,
5056 bool *non_constant_p)
5058 bool saved_constant_expression_p;
5059 bool saved_allow_non_constant_expression_p;
5060 bool saved_non_constant_expression_p;
5061 tree expression;
5063 /* It might seem that we could simply parse the
5064 conditional-expression, and then check to see if it were
5065 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5066 one that the compiler can figure out is constant, possibly after
5067 doing some simplifications or optimizations. The standard has a
5068 precise definition of constant-expression, and we must honor
5069 that, even though it is somewhat more restrictive.
5071 For example:
5073 int i[(2, 3)];
5075 is not a legal declaration, because `(2, 3)' is not a
5076 constant-expression. The `,' operator is forbidden in a
5077 constant-expression. However, GCC's constant-folding machinery
5078 will fold this operation to an INTEGER_CST for `3'. */
5080 /* Save the old settings. */
5081 saved_constant_expression_p = parser->constant_expression_p;
5082 saved_allow_non_constant_expression_p
5083 = parser->allow_non_constant_expression_p;
5084 saved_non_constant_expression_p = parser->non_constant_expression_p;
5085 /* We are now parsing a constant-expression. */
5086 parser->constant_expression_p = true;
5087 parser->allow_non_constant_expression_p = allow_non_constant_p;
5088 parser->non_constant_expression_p = false;
5089 /* Although the grammar says "conditional-expression", we parse an
5090 "assignment-expression", which also permits "throw-expression"
5091 and the use of assignment operators. In the case that
5092 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5093 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5094 actually essential that we look for an assignment-expression.
5095 For example, cp_parser_initializer_clauses uses this function to
5096 determine whether a particular assignment-expression is in fact
5097 constant. */
5098 expression = cp_parser_assignment_expression (parser);
5099 /* Restore the old settings. */
5100 parser->constant_expression_p = saved_constant_expression_p;
5101 parser->allow_non_constant_expression_p
5102 = saved_allow_non_constant_expression_p;
5103 if (allow_non_constant_p)
5104 *non_constant_p = parser->non_constant_expression_p;
5105 parser->non_constant_expression_p = saved_non_constant_expression_p;
5107 return expression;
5110 /* Statements [gram.stmt.stmt] */
5112 /* Parse a statement.
5114 statement:
5115 labeled-statement
5116 expression-statement
5117 compound-statement
5118 selection-statement
5119 iteration-statement
5120 jump-statement
5121 declaration-statement
5122 try-block */
5124 static void
5125 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5127 tree statement;
5128 cp_token *token;
5129 int statement_line_number;
5131 /* There is no statement yet. */
5132 statement = NULL_TREE;
5133 /* Peek at the next token. */
5134 token = cp_lexer_peek_token (parser->lexer);
5135 /* Remember the line number of the first token in the statement. */
5136 statement_line_number = token->location.line;
5137 /* If this is a keyword, then that will often determine what kind of
5138 statement we have. */
5139 if (token->type == CPP_KEYWORD)
5141 enum rid keyword = token->keyword;
5143 switch (keyword)
5145 case RID_CASE:
5146 case RID_DEFAULT:
5147 statement = cp_parser_labeled_statement (parser,
5148 in_statement_expr_p);
5149 break;
5151 case RID_IF:
5152 case RID_SWITCH:
5153 statement = cp_parser_selection_statement (parser);
5154 break;
5156 case RID_WHILE:
5157 case RID_DO:
5158 case RID_FOR:
5159 statement = cp_parser_iteration_statement (parser);
5160 break;
5162 case RID_BREAK:
5163 case RID_CONTINUE:
5164 case RID_RETURN:
5165 case RID_GOTO:
5166 statement = cp_parser_jump_statement (parser);
5167 break;
5169 case RID_TRY:
5170 statement = cp_parser_try_block (parser);
5171 break;
5173 default:
5174 /* It might be a keyword like `int' that can start a
5175 declaration-statement. */
5176 break;
5179 else if (token->type == CPP_NAME)
5181 /* If the next token is a `:', then we are looking at a
5182 labeled-statement. */
5183 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5184 if (token->type == CPP_COLON)
5185 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5187 /* Anything that starts with a `{' must be a compound-statement. */
5188 else if (token->type == CPP_OPEN_BRACE)
5189 statement = cp_parser_compound_statement (parser, false);
5191 /* Everything else must be a declaration-statement or an
5192 expression-statement. Try for the declaration-statement
5193 first, unless we are looking at a `;', in which case we know that
5194 we have an expression-statement. */
5195 if (!statement)
5197 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5199 cp_parser_parse_tentatively (parser);
5200 /* Try to parse the declaration-statement. */
5201 cp_parser_declaration_statement (parser);
5202 /* If that worked, we're done. */
5203 if (cp_parser_parse_definitely (parser))
5204 return;
5206 /* Look for an expression-statement instead. */
5207 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5210 /* Set the line number for the statement. */
5211 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5212 STMT_LINENO (statement) = statement_line_number;
5215 /* Parse a labeled-statement.
5217 labeled-statement:
5218 identifier : statement
5219 case constant-expression : statement
5220 default : statement
5222 Returns the new CASE_LABEL, for a `case' or `default' label. For
5223 an ordinary label, returns a LABEL_STMT. */
5225 static tree
5226 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5228 cp_token *token;
5229 tree statement = NULL_TREE;
5231 /* The next token should be an identifier. */
5232 token = cp_lexer_peek_token (parser->lexer);
5233 if (token->type != CPP_NAME
5234 && token->type != CPP_KEYWORD)
5236 cp_parser_error (parser, "expected labeled-statement");
5237 return error_mark_node;
5240 switch (token->keyword)
5242 case RID_CASE:
5244 tree expr;
5246 /* Consume the `case' token. */
5247 cp_lexer_consume_token (parser->lexer);
5248 /* Parse the constant-expression. */
5249 expr = cp_parser_constant_expression (parser,
5250 /*allow_non_constant_p=*/false,
5251 NULL);
5252 /* Create the label. */
5253 statement = finish_case_label (expr, NULL_TREE);
5255 break;
5257 case RID_DEFAULT:
5258 /* Consume the `default' token. */
5259 cp_lexer_consume_token (parser->lexer);
5260 /* Create the label. */
5261 statement = finish_case_label (NULL_TREE, NULL_TREE);
5262 break;
5264 default:
5265 /* Anything else must be an ordinary label. */
5266 statement = finish_label_stmt (cp_parser_identifier (parser));
5267 break;
5270 /* Require the `:' token. */
5271 cp_parser_require (parser, CPP_COLON, "`:'");
5272 /* Parse the labeled statement. */
5273 cp_parser_statement (parser, in_statement_expr_p);
5275 /* Return the label, in the case of a `case' or `default' label. */
5276 return statement;
5279 /* Parse an expression-statement.
5281 expression-statement:
5282 expression [opt] ;
5284 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5285 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5286 indicates whether this expression-statement is part of an
5287 expression statement. */
5289 static tree
5290 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5292 tree statement = NULL_TREE;
5294 /* If the next token is a ';', then there is no expression
5295 statement. */
5296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5297 statement = cp_parser_expression (parser);
5299 /* Consume the final `;'. */
5300 cp_parser_consume_semicolon_at_end_of_statement (parser);
5302 if (in_statement_expr_p
5303 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5305 /* This is the final expression statement of a statement
5306 expression. */
5307 statement = finish_stmt_expr_expr (statement);
5309 else if (statement)
5310 statement = finish_expr_stmt (statement);
5311 else
5312 finish_stmt ();
5314 return statement;
5317 /* Parse a compound-statement.
5319 compound-statement:
5320 { statement-seq [opt] }
5322 Returns a COMPOUND_STMT representing the statement. */
5324 static tree
5325 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5327 tree compound_stmt;
5329 /* Consume the `{'. */
5330 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5331 return error_mark_node;
5332 /* Begin the compound-statement. */
5333 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5334 /* Parse an (optional) statement-seq. */
5335 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5336 /* Finish the compound-statement. */
5337 finish_compound_stmt (compound_stmt);
5338 /* Consume the `}'. */
5339 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5341 return compound_stmt;
5344 /* Parse an (optional) statement-seq.
5346 statement-seq:
5347 statement
5348 statement-seq [opt] statement */
5350 static void
5351 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5353 /* Scan statements until there aren't any more. */
5354 while (true)
5356 /* If we're looking at a `}', then we've run out of statements. */
5357 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5358 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5359 break;
5361 /* Parse the statement. */
5362 cp_parser_statement (parser, in_statement_expr_p);
5366 /* Parse a selection-statement.
5368 selection-statement:
5369 if ( condition ) statement
5370 if ( condition ) statement else statement
5371 switch ( condition ) statement
5373 Returns the new IF_STMT or SWITCH_STMT. */
5375 static tree
5376 cp_parser_selection_statement (cp_parser* parser)
5378 cp_token *token;
5379 enum rid keyword;
5381 /* Peek at the next token. */
5382 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5384 /* See what kind of keyword it is. */
5385 keyword = token->keyword;
5386 switch (keyword)
5388 case RID_IF:
5389 case RID_SWITCH:
5391 tree statement;
5392 tree condition;
5394 /* Look for the `('. */
5395 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5397 cp_parser_skip_to_end_of_statement (parser);
5398 return error_mark_node;
5401 /* Begin the selection-statement. */
5402 if (keyword == RID_IF)
5403 statement = begin_if_stmt ();
5404 else
5405 statement = begin_switch_stmt ();
5407 /* Parse the condition. */
5408 condition = cp_parser_condition (parser);
5409 /* Look for the `)'. */
5410 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5411 cp_parser_skip_to_closing_parenthesis (parser, true, false);
5413 if (keyword == RID_IF)
5415 tree then_stmt;
5417 /* Add the condition. */
5418 finish_if_stmt_cond (condition, statement);
5420 /* Parse the then-clause. */
5421 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5422 finish_then_clause (statement);
5424 /* If the next token is `else', parse the else-clause. */
5425 if (cp_lexer_next_token_is_keyword (parser->lexer,
5426 RID_ELSE))
5428 tree else_stmt;
5430 /* Consume the `else' keyword. */
5431 cp_lexer_consume_token (parser->lexer);
5432 /* Parse the else-clause. */
5433 else_stmt
5434 = cp_parser_implicitly_scoped_statement (parser);
5435 finish_else_clause (statement);
5438 /* Now we're all done with the if-statement. */
5439 finish_if_stmt ();
5441 else
5443 tree body;
5445 /* Add the condition. */
5446 finish_switch_cond (condition, statement);
5448 /* Parse the body of the switch-statement. */
5449 body = cp_parser_implicitly_scoped_statement (parser);
5451 /* Now we're all done with the switch-statement. */
5452 finish_switch_stmt (statement);
5455 return statement;
5457 break;
5459 default:
5460 cp_parser_error (parser, "expected selection-statement");
5461 return error_mark_node;
5465 /* Parse a condition.
5467 condition:
5468 expression
5469 type-specifier-seq declarator = assignment-expression
5471 GNU Extension:
5473 condition:
5474 type-specifier-seq declarator asm-specification [opt]
5475 attributes [opt] = assignment-expression
5477 Returns the expression that should be tested. */
5479 static tree
5480 cp_parser_condition (cp_parser* parser)
5482 tree type_specifiers;
5483 const char *saved_message;
5485 /* Try the declaration first. */
5486 cp_parser_parse_tentatively (parser);
5487 /* New types are not allowed in the type-specifier-seq for a
5488 condition. */
5489 saved_message = parser->type_definition_forbidden_message;
5490 parser->type_definition_forbidden_message
5491 = "types may not be defined in conditions";
5492 /* Parse the type-specifier-seq. */
5493 type_specifiers = cp_parser_type_specifier_seq (parser);
5494 /* Restore the saved message. */
5495 parser->type_definition_forbidden_message = saved_message;
5496 /* If all is well, we might be looking at a declaration. */
5497 if (!cp_parser_error_occurred (parser))
5499 tree decl;
5500 tree asm_specification;
5501 tree attributes;
5502 tree declarator;
5503 tree initializer = NULL_TREE;
5505 /* Parse the declarator. */
5506 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5507 /*ctor_dtor_or_conv_p=*/NULL);
5508 /* Parse the attributes. */
5509 attributes = cp_parser_attributes_opt (parser);
5510 /* Parse the asm-specification. */
5511 asm_specification = cp_parser_asm_specification_opt (parser);
5512 /* If the next token is not an `=', then we might still be
5513 looking at an expression. For example:
5515 if (A(a).x)
5517 looks like a decl-specifier-seq and a declarator -- but then
5518 there is no `=', so this is an expression. */
5519 cp_parser_require (parser, CPP_EQ, "`='");
5520 /* If we did see an `=', then we are looking at a declaration
5521 for sure. */
5522 if (cp_parser_parse_definitely (parser))
5524 /* Create the declaration. */
5525 decl = start_decl (declarator, type_specifiers,
5526 /*initialized_p=*/true,
5527 attributes, /*prefix_attributes=*/NULL_TREE);
5528 /* Parse the assignment-expression. */
5529 initializer = cp_parser_assignment_expression (parser);
5531 /* Process the initializer. */
5532 cp_finish_decl (decl,
5533 initializer,
5534 asm_specification,
5535 LOOKUP_ONLYCONVERTING);
5537 return convert_from_reference (decl);
5540 /* If we didn't even get past the declarator successfully, we are
5541 definitely not looking at a declaration. */
5542 else
5543 cp_parser_abort_tentative_parse (parser);
5545 /* Otherwise, we are looking at an expression. */
5546 return cp_parser_expression (parser);
5549 /* Parse an iteration-statement.
5551 iteration-statement:
5552 while ( condition ) statement
5553 do statement while ( expression ) ;
5554 for ( for-init-statement condition [opt] ; expression [opt] )
5555 statement
5557 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5559 static tree
5560 cp_parser_iteration_statement (cp_parser* parser)
5562 cp_token *token;
5563 enum rid keyword;
5564 tree statement;
5566 /* Peek at the next token. */
5567 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5568 if (!token)
5569 return error_mark_node;
5571 /* See what kind of keyword it is. */
5572 keyword = token->keyword;
5573 switch (keyword)
5575 case RID_WHILE:
5577 tree condition;
5579 /* Begin the while-statement. */
5580 statement = begin_while_stmt ();
5581 /* Look for the `('. */
5582 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5583 /* Parse the condition. */
5584 condition = cp_parser_condition (parser);
5585 finish_while_stmt_cond (condition, statement);
5586 /* Look for the `)'. */
5587 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5588 /* Parse the dependent statement. */
5589 cp_parser_already_scoped_statement (parser);
5590 /* We're done with the while-statement. */
5591 finish_while_stmt (statement);
5593 break;
5595 case RID_DO:
5597 tree expression;
5599 /* Begin the do-statement. */
5600 statement = begin_do_stmt ();
5601 /* Parse the body of the do-statement. */
5602 cp_parser_implicitly_scoped_statement (parser);
5603 finish_do_body (statement);
5604 /* Look for the `while' keyword. */
5605 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5606 /* Look for the `('. */
5607 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5608 /* Parse the expression. */
5609 expression = cp_parser_expression (parser);
5610 /* We're done with the do-statement. */
5611 finish_do_stmt (expression, statement);
5612 /* Look for the `)'. */
5613 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5614 /* Look for the `;'. */
5615 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5617 break;
5619 case RID_FOR:
5621 tree condition = NULL_TREE;
5622 tree expression = NULL_TREE;
5624 /* Begin the for-statement. */
5625 statement = begin_for_stmt ();
5626 /* Look for the `('. */
5627 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5628 /* Parse the initialization. */
5629 cp_parser_for_init_statement (parser);
5630 finish_for_init_stmt (statement);
5632 /* If there's a condition, process it. */
5633 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5634 condition = cp_parser_condition (parser);
5635 finish_for_cond (condition, statement);
5636 /* Look for the `;'. */
5637 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5639 /* If there's an expression, process it. */
5640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5641 expression = cp_parser_expression (parser);
5642 finish_for_expr (expression, statement);
5643 /* Look for the `)'. */
5644 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5646 /* Parse the body of the for-statement. */
5647 cp_parser_already_scoped_statement (parser);
5649 /* We're done with the for-statement. */
5650 finish_for_stmt (statement);
5652 break;
5654 default:
5655 cp_parser_error (parser, "expected iteration-statement");
5656 statement = error_mark_node;
5657 break;
5660 return statement;
5663 /* Parse a for-init-statement.
5665 for-init-statement:
5666 expression-statement
5667 simple-declaration */
5669 static void
5670 cp_parser_for_init_statement (cp_parser* parser)
5672 /* If the next token is a `;', then we have an empty
5673 expression-statement. Grammatically, this is also a
5674 simple-declaration, but an invalid one, because it does not
5675 declare anything. Therefore, if we did not handle this case
5676 specially, we would issue an error message about an invalid
5677 declaration. */
5678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5680 /* We're going to speculatively look for a declaration, falling back
5681 to an expression, if necessary. */
5682 cp_parser_parse_tentatively (parser);
5683 /* Parse the declaration. */
5684 cp_parser_simple_declaration (parser,
5685 /*function_definition_allowed_p=*/false);
5686 /* If the tentative parse failed, then we shall need to look for an
5687 expression-statement. */
5688 if (cp_parser_parse_definitely (parser))
5689 return;
5692 cp_parser_expression_statement (parser, false);
5695 /* Parse a jump-statement.
5697 jump-statement:
5698 break ;
5699 continue ;
5700 return expression [opt] ;
5701 goto identifier ;
5703 GNU extension:
5705 jump-statement:
5706 goto * expression ;
5708 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5709 GOTO_STMT. */
5711 static tree
5712 cp_parser_jump_statement (cp_parser* parser)
5714 tree statement = error_mark_node;
5715 cp_token *token;
5716 enum rid keyword;
5718 /* Peek at the next token. */
5719 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5720 if (!token)
5721 return error_mark_node;
5723 /* See what kind of keyword it is. */
5724 keyword = token->keyword;
5725 switch (keyword)
5727 case RID_BREAK:
5728 statement = finish_break_stmt ();
5729 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5730 break;
5732 case RID_CONTINUE:
5733 statement = finish_continue_stmt ();
5734 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5735 break;
5737 case RID_RETURN:
5739 tree expr;
5741 /* If the next token is a `;', then there is no
5742 expression. */
5743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5744 expr = cp_parser_expression (parser);
5745 else
5746 expr = NULL_TREE;
5747 /* Build the return-statement. */
5748 statement = finish_return_stmt (expr);
5749 /* Look for the final `;'. */
5750 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5752 break;
5754 case RID_GOTO:
5755 /* Create the goto-statement. */
5756 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
5758 /* Issue a warning about this use of a GNU extension. */
5759 if (pedantic)
5760 pedwarn ("ISO C++ forbids computed gotos");
5761 /* Consume the '*' token. */
5762 cp_lexer_consume_token (parser->lexer);
5763 /* Parse the dependent expression. */
5764 finish_goto_stmt (cp_parser_expression (parser));
5766 else
5767 finish_goto_stmt (cp_parser_identifier (parser));
5768 /* Look for the final `;'. */
5769 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5770 break;
5772 default:
5773 cp_parser_error (parser, "expected jump-statement");
5774 break;
5777 return statement;
5780 /* Parse a declaration-statement.
5782 declaration-statement:
5783 block-declaration */
5785 static void
5786 cp_parser_declaration_statement (cp_parser* parser)
5788 /* Parse the block-declaration. */
5789 cp_parser_block_declaration (parser, /*statement_p=*/true);
5791 /* Finish off the statement. */
5792 finish_stmt ();
5795 /* Some dependent statements (like `if (cond) statement'), are
5796 implicitly in their own scope. In other words, if the statement is
5797 a single statement (as opposed to a compound-statement), it is
5798 none-the-less treated as if it were enclosed in braces. Any
5799 declarations appearing in the dependent statement are out of scope
5800 after control passes that point. This function parses a statement,
5801 but ensures that is in its own scope, even if it is not a
5802 compound-statement.
5804 Returns the new statement. */
5806 static tree
5807 cp_parser_implicitly_scoped_statement (cp_parser* parser)
5809 tree statement;
5811 /* If the token is not a `{', then we must take special action. */
5812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
5814 /* Create a compound-statement. */
5815 statement = begin_compound_stmt (/*has_no_scope=*/false);
5816 /* Parse the dependent-statement. */
5817 cp_parser_statement (parser, false);
5818 /* Finish the dummy compound-statement. */
5819 finish_compound_stmt (statement);
5821 /* Otherwise, we simply parse the statement directly. */
5822 else
5823 statement = cp_parser_compound_statement (parser, false);
5825 /* Return the statement. */
5826 return statement;
5829 /* For some dependent statements (like `while (cond) statement'), we
5830 have already created a scope. Therefore, even if the dependent
5831 statement is a compound-statement, we do not want to create another
5832 scope. */
5834 static void
5835 cp_parser_already_scoped_statement (cp_parser* parser)
5837 /* If the token is not a `{', then we must take special action. */
5838 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
5840 tree statement;
5842 /* Create a compound-statement. */
5843 statement = begin_compound_stmt (/*has_no_scope=*/true);
5844 /* Parse the dependent-statement. */
5845 cp_parser_statement (parser, false);
5846 /* Finish the dummy compound-statement. */
5847 finish_compound_stmt (statement);
5849 /* Otherwise, we simply parse the statement directly. */
5850 else
5851 cp_parser_statement (parser, false);
5854 /* Declarations [gram.dcl.dcl] */
5856 /* Parse an optional declaration-sequence.
5858 declaration-seq:
5859 declaration
5860 declaration-seq declaration */
5862 static void
5863 cp_parser_declaration_seq_opt (cp_parser* parser)
5865 while (true)
5867 cp_token *token;
5869 token = cp_lexer_peek_token (parser->lexer);
5871 if (token->type == CPP_CLOSE_BRACE
5872 || token->type == CPP_EOF)
5873 break;
5875 if (token->type == CPP_SEMICOLON)
5877 /* A declaration consisting of a single semicolon is
5878 invalid. Allow it unless we're being pedantic. */
5879 if (pedantic)
5880 pedwarn ("extra `;'");
5881 cp_lexer_consume_token (parser->lexer);
5882 continue;
5885 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
5886 parser to enter or exit implicit `extern "C"' blocks. */
5887 while (pending_lang_change > 0)
5889 push_lang_context (lang_name_c);
5890 --pending_lang_change;
5892 while (pending_lang_change < 0)
5894 pop_lang_context ();
5895 ++pending_lang_change;
5898 /* Parse the declaration itself. */
5899 cp_parser_declaration (parser);
5903 /* Parse a declaration.
5905 declaration:
5906 block-declaration
5907 function-definition
5908 template-declaration
5909 explicit-instantiation
5910 explicit-specialization
5911 linkage-specification
5912 namespace-definition
5914 GNU extension:
5916 declaration:
5917 __extension__ declaration */
5919 static void
5920 cp_parser_declaration (cp_parser* parser)
5922 cp_token token1;
5923 cp_token token2;
5924 int saved_pedantic;
5926 /* Check for the `__extension__' keyword. */
5927 if (cp_parser_extension_opt (parser, &saved_pedantic))
5929 /* Parse the qualified declaration. */
5930 cp_parser_declaration (parser);
5931 /* Restore the PEDANTIC flag. */
5932 pedantic = saved_pedantic;
5934 return;
5937 /* Try to figure out what kind of declaration is present. */
5938 token1 = *cp_lexer_peek_token (parser->lexer);
5939 if (token1.type != CPP_EOF)
5940 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
5942 /* If the next token is `extern' and the following token is a string
5943 literal, then we have a linkage specification. */
5944 if (token1.keyword == RID_EXTERN
5945 && cp_parser_is_string_literal (&token2))
5946 cp_parser_linkage_specification (parser);
5947 /* If the next token is `template', then we have either a template
5948 declaration, an explicit instantiation, or an explicit
5949 specialization. */
5950 else if (token1.keyword == RID_TEMPLATE)
5952 /* `template <>' indicates a template specialization. */
5953 if (token2.type == CPP_LESS
5954 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
5955 cp_parser_explicit_specialization (parser);
5956 /* `template <' indicates a template declaration. */
5957 else if (token2.type == CPP_LESS)
5958 cp_parser_template_declaration (parser, /*member_p=*/false);
5959 /* Anything else must be an explicit instantiation. */
5960 else
5961 cp_parser_explicit_instantiation (parser);
5963 /* If the next token is `export', then we have a template
5964 declaration. */
5965 else if (token1.keyword == RID_EXPORT)
5966 cp_parser_template_declaration (parser, /*member_p=*/false);
5967 /* If the next token is `extern', 'static' or 'inline' and the one
5968 after that is `template', we have a GNU extended explicit
5969 instantiation directive. */
5970 else if (cp_parser_allow_gnu_extensions_p (parser)
5971 && (token1.keyword == RID_EXTERN
5972 || token1.keyword == RID_STATIC
5973 || token1.keyword == RID_INLINE)
5974 && token2.keyword == RID_TEMPLATE)
5975 cp_parser_explicit_instantiation (parser);
5976 /* If the next token is `namespace', check for a named or unnamed
5977 namespace definition. */
5978 else if (token1.keyword == RID_NAMESPACE
5979 && (/* A named namespace definition. */
5980 (token2.type == CPP_NAME
5981 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5982 == CPP_OPEN_BRACE))
5983 /* An unnamed namespace definition. */
5984 || token2.type == CPP_OPEN_BRACE))
5985 cp_parser_namespace_definition (parser);
5986 /* We must have either a block declaration or a function
5987 definition. */
5988 else
5989 /* Try to parse a block-declaration, or a function-definition. */
5990 cp_parser_block_declaration (parser, /*statement_p=*/false);
5993 /* Parse a block-declaration.
5995 block-declaration:
5996 simple-declaration
5997 asm-definition
5998 namespace-alias-definition
5999 using-declaration
6000 using-directive
6002 GNU Extension:
6004 block-declaration:
6005 __extension__ block-declaration
6006 label-declaration
6008 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6009 part of a declaration-statement. */
6011 static void
6012 cp_parser_block_declaration (cp_parser *parser,
6013 bool statement_p)
6015 cp_token *token1;
6016 int saved_pedantic;
6018 /* Check for the `__extension__' keyword. */
6019 if (cp_parser_extension_opt (parser, &saved_pedantic))
6021 /* Parse the qualified declaration. */
6022 cp_parser_block_declaration (parser, statement_p);
6023 /* Restore the PEDANTIC flag. */
6024 pedantic = saved_pedantic;
6026 return;
6029 /* Peek at the next token to figure out which kind of declaration is
6030 present. */
6031 token1 = cp_lexer_peek_token (parser->lexer);
6033 /* If the next keyword is `asm', we have an asm-definition. */
6034 if (token1->keyword == RID_ASM)
6036 if (statement_p)
6037 cp_parser_commit_to_tentative_parse (parser);
6038 cp_parser_asm_definition (parser);
6040 /* If the next keyword is `namespace', we have a
6041 namespace-alias-definition. */
6042 else if (token1->keyword == RID_NAMESPACE)
6043 cp_parser_namespace_alias_definition (parser);
6044 /* If the next keyword is `using', we have either a
6045 using-declaration or a using-directive. */
6046 else if (token1->keyword == RID_USING)
6048 cp_token *token2;
6050 if (statement_p)
6051 cp_parser_commit_to_tentative_parse (parser);
6052 /* If the token after `using' is `namespace', then we have a
6053 using-directive. */
6054 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6055 if (token2->keyword == RID_NAMESPACE)
6056 cp_parser_using_directive (parser);
6057 /* Otherwise, it's a using-declaration. */
6058 else
6059 cp_parser_using_declaration (parser);
6061 /* If the next keyword is `__label__' we have a label declaration. */
6062 else if (token1->keyword == RID_LABEL)
6064 if (statement_p)
6065 cp_parser_commit_to_tentative_parse (parser);
6066 cp_parser_label_declaration (parser);
6068 /* Anything else must be a simple-declaration. */
6069 else
6070 cp_parser_simple_declaration (parser, !statement_p);
6073 /* Parse a simple-declaration.
6075 simple-declaration:
6076 decl-specifier-seq [opt] init-declarator-list [opt] ;
6078 init-declarator-list:
6079 init-declarator
6080 init-declarator-list , init-declarator
6082 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6083 function-definition as a simple-declaration. */
6085 static void
6086 cp_parser_simple_declaration (cp_parser* parser,
6087 bool function_definition_allowed_p)
6089 tree decl_specifiers;
6090 tree attributes;
6091 int declares_class_or_enum;
6092 bool saw_declarator;
6094 /* Defer access checks until we know what is being declared; the
6095 checks for names appearing in the decl-specifier-seq should be
6096 done as if we were in the scope of the thing being declared. */
6097 push_deferring_access_checks (dk_deferred);
6099 /* Parse the decl-specifier-seq. We have to keep track of whether
6100 or not the decl-specifier-seq declares a named class or
6101 enumeration type, since that is the only case in which the
6102 init-declarator-list is allowed to be empty.
6104 [dcl.dcl]
6106 In a simple-declaration, the optional init-declarator-list can be
6107 omitted only when declaring a class or enumeration, that is when
6108 the decl-specifier-seq contains either a class-specifier, an
6109 elaborated-type-specifier, or an enum-specifier. */
6110 decl_specifiers
6111 = cp_parser_decl_specifier_seq (parser,
6112 CP_PARSER_FLAGS_OPTIONAL,
6113 &attributes,
6114 &declares_class_or_enum);
6115 /* We no longer need to defer access checks. */
6116 stop_deferring_access_checks ();
6118 /* In a block scope, a valid declaration must always have a
6119 decl-specifier-seq. By not trying to parse declarators, we can
6120 resolve the declaration/expression ambiguity more quickly. */
6121 if (!function_definition_allowed_p && !decl_specifiers)
6123 cp_parser_error (parser, "expected declaration");
6124 goto done;
6127 /* If the next two tokens are both identifiers, the code is
6128 erroneous. The usual cause of this situation is code like:
6130 T t;
6132 where "T" should name a type -- but does not. */
6133 if (cp_parser_diagnose_invalid_type_name (parser))
6135 /* If parsing tentatively, we should commit; we really are
6136 looking at a declaration. */
6137 cp_parser_commit_to_tentative_parse (parser);
6138 /* Give up. */
6139 goto done;
6142 /* Keep going until we hit the `;' at the end of the simple
6143 declaration. */
6144 saw_declarator = false;
6145 while (cp_lexer_next_token_is_not (parser->lexer,
6146 CPP_SEMICOLON))
6148 cp_token *token;
6149 bool function_definition_p;
6150 tree decl;
6152 saw_declarator = true;
6153 /* Parse the init-declarator. */
6154 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6155 function_definition_allowed_p,
6156 /*member_p=*/false,
6157 declares_class_or_enum,
6158 &function_definition_p);
6159 /* If an error occurred while parsing tentatively, exit quickly.
6160 (That usually happens when in the body of a function; each
6161 statement is treated as a declaration-statement until proven
6162 otherwise.) */
6163 if (cp_parser_error_occurred (parser))
6164 goto done;
6165 /* Handle function definitions specially. */
6166 if (function_definition_p)
6168 /* If the next token is a `,', then we are probably
6169 processing something like:
6171 void f() {}, *p;
6173 which is erroneous. */
6174 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6175 error ("mixing declarations and function-definitions is forbidden");
6176 /* Otherwise, we're done with the list of declarators. */
6177 else
6179 pop_deferring_access_checks ();
6180 return;
6183 /* The next token should be either a `,' or a `;'. */
6184 token = cp_lexer_peek_token (parser->lexer);
6185 /* If it's a `,', there are more declarators to come. */
6186 if (token->type == CPP_COMMA)
6187 cp_lexer_consume_token (parser->lexer);
6188 /* If it's a `;', we are done. */
6189 else if (token->type == CPP_SEMICOLON)
6190 break;
6191 /* Anything else is an error. */
6192 else
6194 cp_parser_error (parser, "expected `,' or `;'");
6195 /* Skip tokens until we reach the end of the statement. */
6196 cp_parser_skip_to_end_of_statement (parser);
6197 goto done;
6199 /* After the first time around, a function-definition is not
6200 allowed -- even if it was OK at first. For example:
6202 int i, f() {}
6204 is not valid. */
6205 function_definition_allowed_p = false;
6208 /* Issue an error message if no declarators are present, and the
6209 decl-specifier-seq does not itself declare a class or
6210 enumeration. */
6211 if (!saw_declarator)
6213 if (cp_parser_declares_only_class_p (parser))
6214 shadow_tag (decl_specifiers);
6215 /* Perform any deferred access checks. */
6216 perform_deferred_access_checks ();
6219 /* Consume the `;'. */
6220 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6222 done:
6223 pop_deferring_access_checks ();
6226 /* Parse a decl-specifier-seq.
6228 decl-specifier-seq:
6229 decl-specifier-seq [opt] decl-specifier
6231 decl-specifier:
6232 storage-class-specifier
6233 type-specifier
6234 function-specifier
6235 friend
6236 typedef
6238 GNU Extension:
6240 decl-specifier-seq:
6241 decl-specifier-seq [opt] attributes
6243 Returns a TREE_LIST, giving the decl-specifiers in the order they
6244 appear in the source code. The TREE_VALUE of each node is the
6245 decl-specifier. For a keyword (such as `auto' or `friend'), the
6246 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6247 representation of a type-specifier, see cp_parser_type_specifier.
6249 If there are attributes, they will be stored in *ATTRIBUTES,
6250 represented as described above cp_parser_attributes.
6252 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6253 appears, and the entity that will be a friend is not going to be a
6254 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6255 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6256 friendship is granted might not be a class.
6258 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6259 *flags:
6261 1: one of the decl-specifiers is an elaborated-type-specifier
6262 2: one of the decl-specifiers is an enum-specifier or a
6263 class-specifier
6267 static tree
6268 cp_parser_decl_specifier_seq (cp_parser* parser,
6269 cp_parser_flags flags,
6270 tree* attributes,
6271 int* declares_class_or_enum)
6273 tree decl_specs = NULL_TREE;
6274 bool friend_p = false;
6275 bool constructor_possible_p = !parser->in_declarator_p;
6277 /* Assume no class or enumeration type is declared. */
6278 *declares_class_or_enum = 0;
6280 /* Assume there are no attributes. */
6281 *attributes = NULL_TREE;
6283 /* Keep reading specifiers until there are no more to read. */
6284 while (true)
6286 tree decl_spec = NULL_TREE;
6287 bool constructor_p;
6288 cp_token *token;
6290 /* Peek at the next token. */
6291 token = cp_lexer_peek_token (parser->lexer);
6292 /* Handle attributes. */
6293 if (token->keyword == RID_ATTRIBUTE)
6295 /* Parse the attributes. */
6296 decl_spec = cp_parser_attributes_opt (parser);
6297 /* Add them to the list. */
6298 *attributes = chainon (*attributes, decl_spec);
6299 continue;
6301 /* If the next token is an appropriate keyword, we can simply
6302 add it to the list. */
6303 switch (token->keyword)
6305 case RID_FRIEND:
6306 /* decl-specifier:
6307 friend */
6308 friend_p = true;
6309 /* The representation of the specifier is simply the
6310 appropriate TREE_IDENTIFIER node. */
6311 decl_spec = token->value;
6312 /* Consume the token. */
6313 cp_lexer_consume_token (parser->lexer);
6314 break;
6316 /* function-specifier:
6317 inline
6318 virtual
6319 explicit */
6320 case RID_INLINE:
6321 case RID_VIRTUAL:
6322 case RID_EXPLICIT:
6323 decl_spec = cp_parser_function_specifier_opt (parser);
6324 break;
6326 /* decl-specifier:
6327 typedef */
6328 case RID_TYPEDEF:
6329 /* The representation of the specifier is simply the
6330 appropriate TREE_IDENTIFIER node. */
6331 decl_spec = token->value;
6332 /* Consume the token. */
6333 cp_lexer_consume_token (parser->lexer);
6334 /* A constructor declarator cannot appear in a typedef. */
6335 constructor_possible_p = false;
6336 /* The "typedef" keyword can only occur in a declaration; we
6337 may as well commit at this point. */
6338 cp_parser_commit_to_tentative_parse (parser);
6339 break;
6341 /* storage-class-specifier:
6342 auto
6343 register
6344 static
6345 extern
6346 mutable
6348 GNU Extension:
6349 thread */
6350 case RID_AUTO:
6351 case RID_REGISTER:
6352 case RID_STATIC:
6353 case RID_EXTERN:
6354 case RID_MUTABLE:
6355 case RID_THREAD:
6356 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6357 break;
6359 default:
6360 break;
6363 /* Constructors are a special case. The `S' in `S()' is not a
6364 decl-specifier; it is the beginning of the declarator. */
6365 constructor_p = (!decl_spec
6366 && constructor_possible_p
6367 && cp_parser_constructor_declarator_p (parser,
6368 friend_p));
6370 /* If we don't have a DECL_SPEC yet, then we must be looking at
6371 a type-specifier. */
6372 if (!decl_spec && !constructor_p)
6374 int decl_spec_declares_class_or_enum;
6375 bool is_cv_qualifier;
6377 decl_spec
6378 = cp_parser_type_specifier (parser, flags,
6379 friend_p,
6380 /*is_declaration=*/true,
6381 &decl_spec_declares_class_or_enum,
6382 &is_cv_qualifier);
6384 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6386 /* If this type-specifier referenced a user-defined type
6387 (a typedef, class-name, etc.), then we can't allow any
6388 more such type-specifiers henceforth.
6390 [dcl.spec]
6392 The longest sequence of decl-specifiers that could
6393 possibly be a type name is taken as the
6394 decl-specifier-seq of a declaration. The sequence shall
6395 be self-consistent as described below.
6397 [dcl.type]
6399 As a general rule, at most one type-specifier is allowed
6400 in the complete decl-specifier-seq of a declaration. The
6401 only exceptions are the following:
6403 -- const or volatile can be combined with any other
6404 type-specifier.
6406 -- signed or unsigned can be combined with char, long,
6407 short, or int.
6409 -- ..
6411 Example:
6413 typedef char* Pc;
6414 void g (const int Pc);
6416 Here, Pc is *not* part of the decl-specifier seq; it's
6417 the declarator. Therefore, once we see a type-specifier
6418 (other than a cv-qualifier), we forbid any additional
6419 user-defined types. We *do* still allow things like `int
6420 int' to be considered a decl-specifier-seq, and issue the
6421 error message later. */
6422 if (decl_spec && !is_cv_qualifier)
6423 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6424 /* A constructor declarator cannot follow a type-specifier. */
6425 if (decl_spec)
6426 constructor_possible_p = false;
6429 /* If we still do not have a DECL_SPEC, then there are no more
6430 decl-specifiers. */
6431 if (!decl_spec)
6433 /* Issue an error message, unless the entire construct was
6434 optional. */
6435 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6437 cp_parser_error (parser, "expected decl specifier");
6438 return error_mark_node;
6441 break;
6444 /* Add the DECL_SPEC to the list of specifiers. */
6445 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6447 /* After we see one decl-specifier, further decl-specifiers are
6448 always optional. */
6449 flags |= CP_PARSER_FLAGS_OPTIONAL;
6452 /* We have built up the DECL_SPECS in reverse order. Return them in
6453 the correct order. */
6454 return nreverse (decl_specs);
6457 /* Parse an (optional) storage-class-specifier.
6459 storage-class-specifier:
6460 auto
6461 register
6462 static
6463 extern
6464 mutable
6466 GNU Extension:
6468 storage-class-specifier:
6469 thread
6471 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6473 static tree
6474 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6476 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6478 case RID_AUTO:
6479 case RID_REGISTER:
6480 case RID_STATIC:
6481 case RID_EXTERN:
6482 case RID_MUTABLE:
6483 case RID_THREAD:
6484 /* Consume the token. */
6485 return cp_lexer_consume_token (parser->lexer)->value;
6487 default:
6488 return NULL_TREE;
6492 /* Parse an (optional) function-specifier.
6494 function-specifier:
6495 inline
6496 virtual
6497 explicit
6499 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6501 static tree
6502 cp_parser_function_specifier_opt (cp_parser* parser)
6504 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6506 case RID_INLINE:
6507 case RID_VIRTUAL:
6508 case RID_EXPLICIT:
6509 /* Consume the token. */
6510 return cp_lexer_consume_token (parser->lexer)->value;
6512 default:
6513 return NULL_TREE;
6517 /* Parse a linkage-specification.
6519 linkage-specification:
6520 extern string-literal { declaration-seq [opt] }
6521 extern string-literal declaration */
6523 static void
6524 cp_parser_linkage_specification (cp_parser* parser)
6526 cp_token *token;
6527 tree linkage;
6529 /* Look for the `extern' keyword. */
6530 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6532 /* Peek at the next token. */
6533 token = cp_lexer_peek_token (parser->lexer);
6534 /* If it's not a string-literal, then there's a problem. */
6535 if (!cp_parser_is_string_literal (token))
6537 cp_parser_error (parser, "expected language-name");
6538 return;
6540 /* Consume the token. */
6541 cp_lexer_consume_token (parser->lexer);
6543 /* Transform the literal into an identifier. If the literal is a
6544 wide-character string, or contains embedded NULs, then we can't
6545 handle it as the user wants. */
6546 if (token->type == CPP_WSTRING
6547 || (strlen (TREE_STRING_POINTER (token->value))
6548 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6550 cp_parser_error (parser, "invalid linkage-specification");
6551 /* Assume C++ linkage. */
6552 linkage = get_identifier ("c++");
6554 /* If it's a simple string constant, things are easier. */
6555 else
6556 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6558 /* We're now using the new linkage. */
6559 push_lang_context (linkage);
6561 /* If the next token is a `{', then we're using the first
6562 production. */
6563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6565 /* Consume the `{' token. */
6566 cp_lexer_consume_token (parser->lexer);
6567 /* Parse the declarations. */
6568 cp_parser_declaration_seq_opt (parser);
6569 /* Look for the closing `}'. */
6570 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6572 /* Otherwise, there's just one declaration. */
6573 else
6575 bool saved_in_unbraced_linkage_specification_p;
6577 saved_in_unbraced_linkage_specification_p
6578 = parser->in_unbraced_linkage_specification_p;
6579 parser->in_unbraced_linkage_specification_p = true;
6580 have_extern_spec = true;
6581 cp_parser_declaration (parser);
6582 have_extern_spec = false;
6583 parser->in_unbraced_linkage_specification_p
6584 = saved_in_unbraced_linkage_specification_p;
6587 /* We're done with the linkage-specification. */
6588 pop_lang_context ();
6591 /* Special member functions [gram.special] */
6593 /* Parse a conversion-function-id.
6595 conversion-function-id:
6596 operator conversion-type-id
6598 Returns an IDENTIFIER_NODE representing the operator. */
6600 static tree
6601 cp_parser_conversion_function_id (cp_parser* parser)
6603 tree type;
6604 tree saved_scope;
6605 tree saved_qualifying_scope;
6606 tree saved_object_scope;
6608 /* Look for the `operator' token. */
6609 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6610 return error_mark_node;
6611 /* When we parse the conversion-type-id, the current scope will be
6612 reset. However, we need that information in able to look up the
6613 conversion function later, so we save it here. */
6614 saved_scope = parser->scope;
6615 saved_qualifying_scope = parser->qualifying_scope;
6616 saved_object_scope = parser->object_scope;
6617 /* We must enter the scope of the class so that the names of
6618 entities declared within the class are available in the
6619 conversion-type-id. For example, consider:
6621 struct S {
6622 typedef int I;
6623 operator I();
6626 S::operator I() { ... }
6628 In order to see that `I' is a type-name in the definition, we
6629 must be in the scope of `S'. */
6630 if (saved_scope)
6631 push_scope (saved_scope);
6632 /* Parse the conversion-type-id. */
6633 type = cp_parser_conversion_type_id (parser);
6634 /* Leave the scope of the class, if any. */
6635 if (saved_scope)
6636 pop_scope (saved_scope);
6637 /* Restore the saved scope. */
6638 parser->scope = saved_scope;
6639 parser->qualifying_scope = saved_qualifying_scope;
6640 parser->object_scope = saved_object_scope;
6641 /* If the TYPE is invalid, indicate failure. */
6642 if (type == error_mark_node)
6643 return error_mark_node;
6644 return mangle_conv_op_name_for_type (type);
6647 /* Parse a conversion-type-id:
6649 conversion-type-id:
6650 type-specifier-seq conversion-declarator [opt]
6652 Returns the TYPE specified. */
6654 static tree
6655 cp_parser_conversion_type_id (cp_parser* parser)
6657 tree attributes;
6658 tree type_specifiers;
6659 tree declarator;
6661 /* Parse the attributes. */
6662 attributes = cp_parser_attributes_opt (parser);
6663 /* Parse the type-specifiers. */
6664 type_specifiers = cp_parser_type_specifier_seq (parser);
6665 /* If that didn't work, stop. */
6666 if (type_specifiers == error_mark_node)
6667 return error_mark_node;
6668 /* Parse the conversion-declarator. */
6669 declarator = cp_parser_conversion_declarator_opt (parser);
6671 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6672 /*initialized=*/0, &attributes);
6675 /* Parse an (optional) conversion-declarator.
6677 conversion-declarator:
6678 ptr-operator conversion-declarator [opt]
6680 Returns a representation of the declarator. See
6681 cp_parser_declarator for details. */
6683 static tree
6684 cp_parser_conversion_declarator_opt (cp_parser* parser)
6686 enum tree_code code;
6687 tree class_type;
6688 tree cv_qualifier_seq;
6690 /* We don't know if there's a ptr-operator next, or not. */
6691 cp_parser_parse_tentatively (parser);
6692 /* Try the ptr-operator. */
6693 code = cp_parser_ptr_operator (parser, &class_type,
6694 &cv_qualifier_seq);
6695 /* If it worked, look for more conversion-declarators. */
6696 if (cp_parser_parse_definitely (parser))
6698 tree declarator;
6700 /* Parse another optional declarator. */
6701 declarator = cp_parser_conversion_declarator_opt (parser);
6703 /* Create the representation of the declarator. */
6704 if (code == INDIRECT_REF)
6705 declarator = make_pointer_declarator (cv_qualifier_seq,
6706 declarator);
6707 else
6708 declarator = make_reference_declarator (cv_qualifier_seq,
6709 declarator);
6711 /* Handle the pointer-to-member case. */
6712 if (class_type)
6713 declarator = build_nt (SCOPE_REF, class_type, declarator);
6715 return declarator;
6718 return NULL_TREE;
6721 /* Parse an (optional) ctor-initializer.
6723 ctor-initializer:
6724 : mem-initializer-list
6726 Returns TRUE iff the ctor-initializer was actually present. */
6728 static bool
6729 cp_parser_ctor_initializer_opt (cp_parser* parser)
6731 /* If the next token is not a `:', then there is no
6732 ctor-initializer. */
6733 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6735 /* Do default initialization of any bases and members. */
6736 if (DECL_CONSTRUCTOR_P (current_function_decl))
6737 finish_mem_initializers (NULL_TREE);
6739 return false;
6742 /* Consume the `:' token. */
6743 cp_lexer_consume_token (parser->lexer);
6744 /* And the mem-initializer-list. */
6745 cp_parser_mem_initializer_list (parser);
6747 return true;
6750 /* Parse a mem-initializer-list.
6752 mem-initializer-list:
6753 mem-initializer
6754 mem-initializer , mem-initializer-list */
6756 static void
6757 cp_parser_mem_initializer_list (cp_parser* parser)
6759 tree mem_initializer_list = NULL_TREE;
6761 /* Let the semantic analysis code know that we are starting the
6762 mem-initializer-list. */
6763 if (!DECL_CONSTRUCTOR_P (current_function_decl))
6764 error ("only constructors take base initializers");
6766 /* Loop through the list. */
6767 while (true)
6769 tree mem_initializer;
6771 /* Parse the mem-initializer. */
6772 mem_initializer = cp_parser_mem_initializer (parser);
6773 /* Add it to the list, unless it was erroneous. */
6774 if (mem_initializer)
6776 TREE_CHAIN (mem_initializer) = mem_initializer_list;
6777 mem_initializer_list = mem_initializer;
6779 /* If the next token is not a `,', we're done. */
6780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6781 break;
6782 /* Consume the `,' token. */
6783 cp_lexer_consume_token (parser->lexer);
6786 /* Perform semantic analysis. */
6787 if (DECL_CONSTRUCTOR_P (current_function_decl))
6788 finish_mem_initializers (mem_initializer_list);
6791 /* Parse a mem-initializer.
6793 mem-initializer:
6794 mem-initializer-id ( expression-list [opt] )
6796 GNU extension:
6798 mem-initializer:
6799 ( expression-list [opt] )
6801 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
6802 class) or FIELD_DECL (for a non-static data member) to initialize;
6803 the TREE_VALUE is the expression-list. */
6805 static tree
6806 cp_parser_mem_initializer (cp_parser* parser)
6808 tree mem_initializer_id;
6809 tree expression_list;
6810 tree member;
6812 /* Find out what is being initialized. */
6813 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6815 pedwarn ("anachronistic old-style base class initializer");
6816 mem_initializer_id = NULL_TREE;
6818 else
6819 mem_initializer_id = cp_parser_mem_initializer_id (parser);
6820 member = expand_member_init (mem_initializer_id);
6821 if (member && !DECL_P (member))
6822 in_base_initializer = 1;
6824 expression_list
6825 = cp_parser_parenthesized_expression_list (parser, false,
6826 /*non_constant_p=*/NULL);
6827 if (!expression_list)
6828 expression_list = void_type_node;
6830 in_base_initializer = 0;
6832 return member ? build_tree_list (member, expression_list) : NULL_TREE;
6835 /* Parse a mem-initializer-id.
6837 mem-initializer-id:
6838 :: [opt] nested-name-specifier [opt] class-name
6839 identifier
6841 Returns a TYPE indicating the class to be initializer for the first
6842 production. Returns an IDENTIFIER_NODE indicating the data member
6843 to be initialized for the second production. */
6845 static tree
6846 cp_parser_mem_initializer_id (cp_parser* parser)
6848 bool global_scope_p;
6849 bool nested_name_specifier_p;
6850 tree id;
6852 /* Look for the optional `::' operator. */
6853 global_scope_p
6854 = (cp_parser_global_scope_opt (parser,
6855 /*current_scope_valid_p=*/false)
6856 != NULL_TREE);
6857 /* Look for the optional nested-name-specifier. The simplest way to
6858 implement:
6860 [temp.res]
6862 The keyword `typename' is not permitted in a base-specifier or
6863 mem-initializer; in these contexts a qualified name that
6864 depends on a template-parameter is implicitly assumed to be a
6865 type name.
6867 is to assume that we have seen the `typename' keyword at this
6868 point. */
6869 nested_name_specifier_p
6870 = (cp_parser_nested_name_specifier_opt (parser,
6871 /*typename_keyword_p=*/true,
6872 /*check_dependency_p=*/true,
6873 /*type_p=*/true)
6874 != NULL_TREE);
6875 /* If there is a `::' operator or a nested-name-specifier, then we
6876 are definitely looking for a class-name. */
6877 if (global_scope_p || nested_name_specifier_p)
6878 return cp_parser_class_name (parser,
6879 /*typename_keyword_p=*/true,
6880 /*template_keyword_p=*/false,
6881 /*type_p=*/false,
6882 /*check_dependency_p=*/true,
6883 /*class_head_p=*/false);
6884 /* Otherwise, we could also be looking for an ordinary identifier. */
6885 cp_parser_parse_tentatively (parser);
6886 /* Try a class-name. */
6887 id = cp_parser_class_name (parser,
6888 /*typename_keyword_p=*/true,
6889 /*template_keyword_p=*/false,
6890 /*type_p=*/false,
6891 /*check_dependency_p=*/true,
6892 /*class_head_p=*/false);
6893 /* If we found one, we're done. */
6894 if (cp_parser_parse_definitely (parser))
6895 return id;
6896 /* Otherwise, look for an ordinary identifier. */
6897 return cp_parser_identifier (parser);
6900 /* Overloading [gram.over] */
6902 /* Parse an operator-function-id.
6904 operator-function-id:
6905 operator operator
6907 Returns an IDENTIFIER_NODE for the operator which is a
6908 human-readable spelling of the identifier, e.g., `operator +'. */
6910 static tree
6911 cp_parser_operator_function_id (cp_parser* parser)
6913 /* Look for the `operator' keyword. */
6914 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6915 return error_mark_node;
6916 /* And then the name of the operator itself. */
6917 return cp_parser_operator (parser);
6920 /* Parse an operator.
6922 operator:
6923 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
6924 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
6925 || ++ -- , ->* -> () []
6927 GNU Extensions:
6929 operator:
6930 <? >? <?= >?=
6932 Returns an IDENTIFIER_NODE for the operator which is a
6933 human-readable spelling of the identifier, e.g., `operator +'. */
6935 static tree
6936 cp_parser_operator (cp_parser* parser)
6938 tree id = NULL_TREE;
6939 cp_token *token;
6941 /* Peek at the next token. */
6942 token = cp_lexer_peek_token (parser->lexer);
6943 /* Figure out which operator we have. */
6944 switch (token->type)
6946 case CPP_KEYWORD:
6948 enum tree_code op;
6950 /* The keyword should be either `new' or `delete'. */
6951 if (token->keyword == RID_NEW)
6952 op = NEW_EXPR;
6953 else if (token->keyword == RID_DELETE)
6954 op = DELETE_EXPR;
6955 else
6956 break;
6958 /* Consume the `new' or `delete' token. */
6959 cp_lexer_consume_token (parser->lexer);
6961 /* Peek at the next token. */
6962 token = cp_lexer_peek_token (parser->lexer);
6963 /* If it's a `[' token then this is the array variant of the
6964 operator. */
6965 if (token->type == CPP_OPEN_SQUARE)
6967 /* Consume the `[' token. */
6968 cp_lexer_consume_token (parser->lexer);
6969 /* Look for the `]' token. */
6970 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
6971 id = ansi_opname (op == NEW_EXPR
6972 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
6974 /* Otherwise, we have the non-array variant. */
6975 else
6976 id = ansi_opname (op);
6978 return id;
6981 case CPP_PLUS:
6982 id = ansi_opname (PLUS_EXPR);
6983 break;
6985 case CPP_MINUS:
6986 id = ansi_opname (MINUS_EXPR);
6987 break;
6989 case CPP_MULT:
6990 id = ansi_opname (MULT_EXPR);
6991 break;
6993 case CPP_DIV:
6994 id = ansi_opname (TRUNC_DIV_EXPR);
6995 break;
6997 case CPP_MOD:
6998 id = ansi_opname (TRUNC_MOD_EXPR);
6999 break;
7001 case CPP_XOR:
7002 id = ansi_opname (BIT_XOR_EXPR);
7003 break;
7005 case CPP_AND:
7006 id = ansi_opname (BIT_AND_EXPR);
7007 break;
7009 case CPP_OR:
7010 id = ansi_opname (BIT_IOR_EXPR);
7011 break;
7013 case CPP_COMPL:
7014 id = ansi_opname (BIT_NOT_EXPR);
7015 break;
7017 case CPP_NOT:
7018 id = ansi_opname (TRUTH_NOT_EXPR);
7019 break;
7021 case CPP_EQ:
7022 id = ansi_assopname (NOP_EXPR);
7023 break;
7025 case CPP_LESS:
7026 id = ansi_opname (LT_EXPR);
7027 break;
7029 case CPP_GREATER:
7030 id = ansi_opname (GT_EXPR);
7031 break;
7033 case CPP_PLUS_EQ:
7034 id = ansi_assopname (PLUS_EXPR);
7035 break;
7037 case CPP_MINUS_EQ:
7038 id = ansi_assopname (MINUS_EXPR);
7039 break;
7041 case CPP_MULT_EQ:
7042 id = ansi_assopname (MULT_EXPR);
7043 break;
7045 case CPP_DIV_EQ:
7046 id = ansi_assopname (TRUNC_DIV_EXPR);
7047 break;
7049 case CPP_MOD_EQ:
7050 id = ansi_assopname (TRUNC_MOD_EXPR);
7051 break;
7053 case CPP_XOR_EQ:
7054 id = ansi_assopname (BIT_XOR_EXPR);
7055 break;
7057 case CPP_AND_EQ:
7058 id = ansi_assopname (BIT_AND_EXPR);
7059 break;
7061 case CPP_OR_EQ:
7062 id = ansi_assopname (BIT_IOR_EXPR);
7063 break;
7065 case CPP_LSHIFT:
7066 id = ansi_opname (LSHIFT_EXPR);
7067 break;
7069 case CPP_RSHIFT:
7070 id = ansi_opname (RSHIFT_EXPR);
7071 break;
7073 case CPP_LSHIFT_EQ:
7074 id = ansi_assopname (LSHIFT_EXPR);
7075 break;
7077 case CPP_RSHIFT_EQ:
7078 id = ansi_assopname (RSHIFT_EXPR);
7079 break;
7081 case CPP_EQ_EQ:
7082 id = ansi_opname (EQ_EXPR);
7083 break;
7085 case CPP_NOT_EQ:
7086 id = ansi_opname (NE_EXPR);
7087 break;
7089 case CPP_LESS_EQ:
7090 id = ansi_opname (LE_EXPR);
7091 break;
7093 case CPP_GREATER_EQ:
7094 id = ansi_opname (GE_EXPR);
7095 break;
7097 case CPP_AND_AND:
7098 id = ansi_opname (TRUTH_ANDIF_EXPR);
7099 break;
7101 case CPP_OR_OR:
7102 id = ansi_opname (TRUTH_ORIF_EXPR);
7103 break;
7105 case CPP_PLUS_PLUS:
7106 id = ansi_opname (POSTINCREMENT_EXPR);
7107 break;
7109 case CPP_MINUS_MINUS:
7110 id = ansi_opname (PREDECREMENT_EXPR);
7111 break;
7113 case CPP_COMMA:
7114 id = ansi_opname (COMPOUND_EXPR);
7115 break;
7117 case CPP_DEREF_STAR:
7118 id = ansi_opname (MEMBER_REF);
7119 break;
7121 case CPP_DEREF:
7122 id = ansi_opname (COMPONENT_REF);
7123 break;
7125 case CPP_OPEN_PAREN:
7126 /* Consume the `('. */
7127 cp_lexer_consume_token (parser->lexer);
7128 /* Look for the matching `)'. */
7129 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7130 return ansi_opname (CALL_EXPR);
7132 case CPP_OPEN_SQUARE:
7133 /* Consume the `['. */
7134 cp_lexer_consume_token (parser->lexer);
7135 /* Look for the matching `]'. */
7136 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7137 return ansi_opname (ARRAY_REF);
7139 /* Extensions. */
7140 case CPP_MIN:
7141 id = ansi_opname (MIN_EXPR);
7142 break;
7144 case CPP_MAX:
7145 id = ansi_opname (MAX_EXPR);
7146 break;
7148 case CPP_MIN_EQ:
7149 id = ansi_assopname (MIN_EXPR);
7150 break;
7152 case CPP_MAX_EQ:
7153 id = ansi_assopname (MAX_EXPR);
7154 break;
7156 default:
7157 /* Anything else is an error. */
7158 break;
7161 /* If we have selected an identifier, we need to consume the
7162 operator token. */
7163 if (id)
7164 cp_lexer_consume_token (parser->lexer);
7165 /* Otherwise, no valid operator name was present. */
7166 else
7168 cp_parser_error (parser, "expected operator");
7169 id = error_mark_node;
7172 return id;
7175 /* Parse a template-declaration.
7177 template-declaration:
7178 export [opt] template < template-parameter-list > declaration
7180 If MEMBER_P is TRUE, this template-declaration occurs within a
7181 class-specifier.
7183 The grammar rule given by the standard isn't correct. What
7184 is really meant is:
7186 template-declaration:
7187 export [opt] template-parameter-list-seq
7188 decl-specifier-seq [opt] init-declarator [opt] ;
7189 export [opt] template-parameter-list-seq
7190 function-definition
7192 template-parameter-list-seq:
7193 template-parameter-list-seq [opt]
7194 template < template-parameter-list > */
7196 static void
7197 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7199 /* Check for `export'. */
7200 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7202 /* Consume the `export' token. */
7203 cp_lexer_consume_token (parser->lexer);
7204 /* Warn that we do not support `export'. */
7205 warning ("keyword `export' not implemented, and will be ignored");
7208 cp_parser_template_declaration_after_export (parser, member_p);
7211 /* Parse a template-parameter-list.
7213 template-parameter-list:
7214 template-parameter
7215 template-parameter-list , template-parameter
7217 Returns a TREE_LIST. Each node represents a template parameter.
7218 The nodes are connected via their TREE_CHAINs. */
7220 static tree
7221 cp_parser_template_parameter_list (cp_parser* parser)
7223 tree parameter_list = NULL_TREE;
7225 while (true)
7227 tree parameter;
7228 cp_token *token;
7230 /* Parse the template-parameter. */
7231 parameter = cp_parser_template_parameter (parser);
7232 /* Add it to the list. */
7233 parameter_list = process_template_parm (parameter_list,
7234 parameter);
7236 /* Peek at the next token. */
7237 token = cp_lexer_peek_token (parser->lexer);
7238 /* If it's not a `,', we're done. */
7239 if (token->type != CPP_COMMA)
7240 break;
7241 /* Otherwise, consume the `,' token. */
7242 cp_lexer_consume_token (parser->lexer);
7245 return parameter_list;
7248 /* Parse a template-parameter.
7250 template-parameter:
7251 type-parameter
7252 parameter-declaration
7254 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7255 TREE_PURPOSE is the default value, if any. */
7257 static tree
7258 cp_parser_template_parameter (cp_parser* parser)
7260 cp_token *token;
7262 /* Peek at the next token. */
7263 token = cp_lexer_peek_token (parser->lexer);
7264 /* If it is `class' or `template', we have a type-parameter. */
7265 if (token->keyword == RID_TEMPLATE)
7266 return cp_parser_type_parameter (parser);
7267 /* If it is `class' or `typename' we do not know yet whether it is a
7268 type parameter or a non-type parameter. Consider:
7270 template <typename T, typename T::X X> ...
7274 template <class C, class D*> ...
7276 Here, the first parameter is a type parameter, and the second is
7277 a non-type parameter. We can tell by looking at the token after
7278 the identifier -- if it is a `,', `=', or `>' then we have a type
7279 parameter. */
7280 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7282 /* Peek at the token after `class' or `typename'. */
7283 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7284 /* If it's an identifier, skip it. */
7285 if (token->type == CPP_NAME)
7286 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7287 /* Now, see if the token looks like the end of a template
7288 parameter. */
7289 if (token->type == CPP_COMMA
7290 || token->type == CPP_EQ
7291 || token->type == CPP_GREATER)
7292 return cp_parser_type_parameter (parser);
7295 /* Otherwise, it is a non-type parameter.
7297 [temp.param]
7299 When parsing a default template-argument for a non-type
7300 template-parameter, the first non-nested `>' is taken as the end
7301 of the template parameter-list rather than a greater-than
7302 operator. */
7303 return
7304 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7307 /* Parse a type-parameter.
7309 type-parameter:
7310 class identifier [opt]
7311 class identifier [opt] = type-id
7312 typename identifier [opt]
7313 typename identifier [opt] = type-id
7314 template < template-parameter-list > class identifier [opt]
7315 template < template-parameter-list > class identifier [opt]
7316 = id-expression
7318 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7319 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7320 the declaration of the parameter. */
7322 static tree
7323 cp_parser_type_parameter (cp_parser* parser)
7325 cp_token *token;
7326 tree parameter;
7328 /* Look for a keyword to tell us what kind of parameter this is. */
7329 token = cp_parser_require (parser, CPP_KEYWORD,
7330 "`class', `typename', or `template'");
7331 if (!token)
7332 return error_mark_node;
7334 switch (token->keyword)
7336 case RID_CLASS:
7337 case RID_TYPENAME:
7339 tree identifier;
7340 tree default_argument;
7342 /* If the next token is an identifier, then it names the
7343 parameter. */
7344 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7345 identifier = cp_parser_identifier (parser);
7346 else
7347 identifier = NULL_TREE;
7349 /* Create the parameter. */
7350 parameter = finish_template_type_parm (class_type_node, identifier);
7352 /* If the next token is an `=', we have a default argument. */
7353 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7355 /* Consume the `=' token. */
7356 cp_lexer_consume_token (parser->lexer);
7357 /* Parse the default-argument. */
7358 default_argument = cp_parser_type_id (parser);
7360 else
7361 default_argument = NULL_TREE;
7363 /* Create the combined representation of the parameter and the
7364 default argument. */
7365 parameter = build_tree_list (default_argument, parameter);
7367 break;
7369 case RID_TEMPLATE:
7371 tree parameter_list;
7372 tree identifier;
7373 tree default_argument;
7375 /* Look for the `<'. */
7376 cp_parser_require (parser, CPP_LESS, "`<'");
7377 /* Parse the template-parameter-list. */
7378 begin_template_parm_list ();
7379 parameter_list
7380 = cp_parser_template_parameter_list (parser);
7381 parameter_list = end_template_parm_list (parameter_list);
7382 /* Look for the `>'. */
7383 cp_parser_require (parser, CPP_GREATER, "`>'");
7384 /* Look for the `class' keyword. */
7385 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7386 /* If the next token is an `=', then there is a
7387 default-argument. If the next token is a `>', we are at
7388 the end of the parameter-list. If the next token is a `,',
7389 then we are at the end of this parameter. */
7390 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7391 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7392 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7393 identifier = cp_parser_identifier (parser);
7394 else
7395 identifier = NULL_TREE;
7396 /* Create the template parameter. */
7397 parameter = finish_template_template_parm (class_type_node,
7398 identifier);
7400 /* If the next token is an `=', then there is a
7401 default-argument. */
7402 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7404 /* Consume the `='. */
7405 cp_lexer_consume_token (parser->lexer);
7406 /* Parse the id-expression. */
7407 default_argument
7408 = cp_parser_id_expression (parser,
7409 /*template_keyword_p=*/false,
7410 /*check_dependency_p=*/true,
7411 /*template_p=*/NULL,
7412 /*declarator_p=*/false);
7413 /* Look up the name. */
7414 default_argument
7415 = cp_parser_lookup_name_simple (parser, default_argument);
7416 /* See if the default argument is valid. */
7417 default_argument
7418 = check_template_template_default_arg (default_argument);
7420 else
7421 default_argument = NULL_TREE;
7423 /* Create the combined representation of the parameter and the
7424 default argument. */
7425 parameter = build_tree_list (default_argument, parameter);
7427 break;
7429 default:
7430 /* Anything else is an error. */
7431 cp_parser_error (parser,
7432 "expected `class', `typename', or `template'");
7433 parameter = error_mark_node;
7436 return parameter;
7439 /* Parse a template-id.
7441 template-id:
7442 template-name < template-argument-list [opt] >
7444 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7445 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7446 returned. Otherwise, if the template-name names a function, or set
7447 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7448 names a class, returns a TYPE_DECL for the specialization.
7450 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7451 uninstantiated templates. */
7453 static tree
7454 cp_parser_template_id (cp_parser *parser,
7455 bool template_keyword_p,
7456 bool check_dependency_p)
7458 tree template;
7459 tree arguments;
7460 tree saved_scope;
7461 tree saved_qualifying_scope;
7462 tree saved_object_scope;
7463 tree template_id;
7464 bool saved_greater_than_is_operator_p;
7465 ptrdiff_t start_of_id;
7466 tree access_check = NULL_TREE;
7467 cp_token *next_token;
7469 /* If the next token corresponds to a template-id, there is no need
7470 to reparse it. */
7471 next_token = cp_lexer_peek_token (parser->lexer);
7472 if (next_token->type == CPP_TEMPLATE_ID)
7474 tree value;
7475 tree check;
7477 /* Get the stored value. */
7478 value = cp_lexer_consume_token (parser->lexer)->value;
7479 /* Perform any access checks that were deferred. */
7480 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7481 perform_or_defer_access_check (TREE_PURPOSE (check),
7482 TREE_VALUE (check));
7483 /* Return the stored value. */
7484 return TREE_VALUE (value);
7487 /* Avoid performing name lookup if there is no possibility of
7488 finding a template-id. */
7489 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7490 || (next_token->type == CPP_NAME
7491 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7493 cp_parser_error (parser, "expected template-id");
7494 return error_mark_node;
7497 /* Remember where the template-id starts. */
7498 if (cp_parser_parsing_tentatively (parser)
7499 && !cp_parser_committed_to_tentative_parse (parser))
7501 next_token = cp_lexer_peek_token (parser->lexer);
7502 start_of_id = cp_lexer_token_difference (parser->lexer,
7503 parser->lexer->first_token,
7504 next_token);
7506 else
7507 start_of_id = -1;
7509 push_deferring_access_checks (dk_deferred);
7511 /* Parse the template-name. */
7512 template = cp_parser_template_name (parser, template_keyword_p,
7513 check_dependency_p);
7514 if (template == error_mark_node)
7516 pop_deferring_access_checks ();
7517 return error_mark_node;
7520 /* Look for the `<' that starts the template-argument-list. */
7521 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7523 pop_deferring_access_checks ();
7524 return error_mark_node;
7527 /* [temp.names]
7529 When parsing a template-id, the first non-nested `>' is taken as
7530 the end of the template-argument-list rather than a greater-than
7531 operator. */
7532 saved_greater_than_is_operator_p
7533 = parser->greater_than_is_operator_p;
7534 parser->greater_than_is_operator_p = false;
7535 /* Parsing the argument list may modify SCOPE, so we save it
7536 here. */
7537 saved_scope = parser->scope;
7538 saved_qualifying_scope = parser->qualifying_scope;
7539 saved_object_scope = parser->object_scope;
7540 /* Parse the template-argument-list itself. */
7541 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7542 arguments = NULL_TREE;
7543 else
7544 arguments = cp_parser_template_argument_list (parser);
7545 /* Look for the `>' that ends the template-argument-list. */
7546 cp_parser_require (parser, CPP_GREATER, "`>'");
7547 /* The `>' token might be a greater-than operator again now. */
7548 parser->greater_than_is_operator_p
7549 = saved_greater_than_is_operator_p;
7550 /* Restore the SAVED_SCOPE. */
7551 parser->scope = saved_scope;
7552 parser->qualifying_scope = saved_qualifying_scope;
7553 parser->object_scope = saved_object_scope;
7555 /* Build a representation of the specialization. */
7556 if (TREE_CODE (template) == IDENTIFIER_NODE)
7557 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7558 else if (DECL_CLASS_TEMPLATE_P (template)
7559 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7560 template_id
7561 = finish_template_type (template, arguments,
7562 cp_lexer_next_token_is (parser->lexer,
7563 CPP_SCOPE));
7564 else
7566 /* If it's not a class-template or a template-template, it should be
7567 a function-template. */
7568 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7569 || TREE_CODE (template) == OVERLOAD
7570 || BASELINK_P (template)),
7571 20010716);
7573 template_id = lookup_template_function (template, arguments);
7576 /* Retrieve any deferred checks. Do not pop this access checks yet
7577 so the memory will not be reclaimed during token replacing below. */
7578 access_check = get_deferred_access_checks ();
7580 /* If parsing tentatively, replace the sequence of tokens that makes
7581 up the template-id with a CPP_TEMPLATE_ID token. That way,
7582 should we re-parse the token stream, we will not have to repeat
7583 the effort required to do the parse, nor will we issue duplicate
7584 error messages about problems during instantiation of the
7585 template. */
7586 if (start_of_id >= 0)
7588 cp_token *token;
7590 /* Find the token that corresponds to the start of the
7591 template-id. */
7592 token = cp_lexer_advance_token (parser->lexer,
7593 parser->lexer->first_token,
7594 start_of_id);
7596 /* Reset the contents of the START_OF_ID token. */
7597 token->type = CPP_TEMPLATE_ID;
7598 token->value = build_tree_list (access_check, template_id);
7599 token->keyword = RID_MAX;
7600 /* Purge all subsequent tokens. */
7601 cp_lexer_purge_tokens_after (parser->lexer, token);
7604 pop_deferring_access_checks ();
7605 return template_id;
7608 /* Parse a template-name.
7610 template-name:
7611 identifier
7613 The standard should actually say:
7615 template-name:
7616 identifier
7617 operator-function-id
7618 conversion-function-id
7620 A defect report has been filed about this issue.
7622 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7623 `template' keyword, in a construction like:
7625 T::template f<3>()
7627 In that case `f' is taken to be a template-name, even though there
7628 is no way of knowing for sure.
7630 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7631 name refers to a set of overloaded functions, at least one of which
7632 is a template, or an IDENTIFIER_NODE with the name of the template,
7633 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7634 names are looked up inside uninstantiated templates. */
7636 static tree
7637 cp_parser_template_name (cp_parser* parser,
7638 bool template_keyword_p,
7639 bool check_dependency_p)
7641 tree identifier;
7642 tree decl;
7643 tree fns;
7645 /* If the next token is `operator', then we have either an
7646 operator-function-id or a conversion-function-id. */
7647 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7649 /* We don't know whether we're looking at an
7650 operator-function-id or a conversion-function-id. */
7651 cp_parser_parse_tentatively (parser);
7652 /* Try an operator-function-id. */
7653 identifier = cp_parser_operator_function_id (parser);
7654 /* If that didn't work, try a conversion-function-id. */
7655 if (!cp_parser_parse_definitely (parser))
7656 identifier = cp_parser_conversion_function_id (parser);
7658 /* Look for the identifier. */
7659 else
7660 identifier = cp_parser_identifier (parser);
7662 /* If we didn't find an identifier, we don't have a template-id. */
7663 if (identifier == error_mark_node)
7664 return error_mark_node;
7666 /* If the name immediately followed the `template' keyword, then it
7667 is a template-name. However, if the next token is not `<', then
7668 we do not treat it as a template-name, since it is not being used
7669 as part of a template-id. This enables us to handle constructs
7670 like:
7672 template <typename T> struct S { S(); };
7673 template <typename T> S<T>::S();
7675 correctly. We would treat `S' as a template -- if it were `S<T>'
7676 -- but we do not if there is no `<'. */
7677 if (template_keyword_p && processing_template_decl
7678 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7679 return identifier;
7681 /* Look up the name. */
7682 decl = cp_parser_lookup_name (parser, identifier,
7683 /*is_type=*/false,
7684 /*is_namespace=*/false,
7685 check_dependency_p);
7686 decl = maybe_get_template_decl_from_type_decl (decl);
7688 /* If DECL is a template, then the name was a template-name. */
7689 if (TREE_CODE (decl) == TEMPLATE_DECL)
7691 else
7693 /* The standard does not explicitly indicate whether a name that
7694 names a set of overloaded declarations, some of which are
7695 templates, is a template-name. However, such a name should
7696 be a template-name; otherwise, there is no way to form a
7697 template-id for the overloaded templates. */
7698 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7699 if (TREE_CODE (fns) == OVERLOAD)
7701 tree fn;
7703 for (fn = fns; fn; fn = OVL_NEXT (fn))
7704 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7705 break;
7707 else
7709 /* Otherwise, the name does not name a template. */
7710 cp_parser_error (parser, "expected template-name");
7711 return error_mark_node;
7715 /* If DECL is dependent, and refers to a function, then just return
7716 its name; we will look it up again during template instantiation. */
7717 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
7719 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
7720 if (TYPE_P (scope) && dependent_type_p (scope))
7721 return identifier;
7724 return decl;
7727 /* Parse a template-argument-list.
7729 template-argument-list:
7730 template-argument
7731 template-argument-list , template-argument
7733 Returns a TREE_VEC containing the arguments. */
7735 static tree
7736 cp_parser_template_argument_list (cp_parser* parser)
7738 tree fixed_args[10];
7739 unsigned n_args = 0;
7740 unsigned alloced = 10;
7741 tree *arg_ary = fixed_args;
7742 tree vec;
7746 tree argument;
7748 if (n_args)
7749 /* Consume the comma. */
7750 cp_lexer_consume_token (parser->lexer);
7752 /* Parse the template-argument. */
7753 argument = cp_parser_template_argument (parser);
7754 if (n_args == alloced)
7756 alloced *= 2;
7758 if (arg_ary == fixed_args)
7760 arg_ary = xmalloc (sizeof (tree) * alloced);
7761 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
7763 else
7764 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
7766 arg_ary[n_args++] = argument;
7768 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
7770 vec = make_tree_vec (n_args);
7772 while (n_args--)
7773 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
7775 if (arg_ary != fixed_args)
7776 free (arg_ary);
7777 return vec;
7780 /* Parse a template-argument.
7782 template-argument:
7783 assignment-expression
7784 type-id
7785 id-expression
7787 The representation is that of an assignment-expression, type-id, or
7788 id-expression -- except that the qualified id-expression is
7789 evaluated, so that the value returned is either a DECL or an
7790 OVERLOAD.
7792 Although the standard says "assignment-expression", it forbids
7793 throw-expressions or assignments in the template argument.
7794 Therefore, we use "conditional-expression" instead. */
7796 static tree
7797 cp_parser_template_argument (cp_parser* parser)
7799 tree argument;
7800 bool template_p;
7801 bool address_p;
7802 cp_token *token;
7803 cp_id_kind idk;
7804 tree qualifying_class;
7806 /* There's really no way to know what we're looking at, so we just
7807 try each alternative in order.
7809 [temp.arg]
7811 In a template-argument, an ambiguity between a type-id and an
7812 expression is resolved to a type-id, regardless of the form of
7813 the corresponding template-parameter.
7815 Therefore, we try a type-id first. */
7816 cp_parser_parse_tentatively (parser);
7817 argument = cp_parser_type_id (parser);
7818 /* If the next token isn't a `,' or a `>', then this argument wasn't
7819 really finished. */
7820 if (!cp_parser_next_token_ends_template_argument_p (parser))
7821 cp_parser_error (parser, "expected template-argument");
7822 /* If that worked, we're done. */
7823 if (cp_parser_parse_definitely (parser))
7824 return argument;
7825 /* We're still not sure what the argument will be. */
7826 cp_parser_parse_tentatively (parser);
7827 /* Try a template. */
7828 argument = cp_parser_id_expression (parser,
7829 /*template_keyword_p=*/false,
7830 /*check_dependency_p=*/true,
7831 &template_p,
7832 /*declarator_p=*/false);
7833 /* If the next token isn't a `,' or a `>', then this argument wasn't
7834 really finished. */
7835 if (!cp_parser_next_token_ends_template_argument_p (parser))
7836 cp_parser_error (parser, "expected template-argument");
7837 if (!cp_parser_error_occurred (parser))
7839 /* Figure out what is being referred to. */
7840 argument = cp_parser_lookup_name_simple (parser, argument);
7841 if (template_p)
7842 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
7843 TREE_OPERAND (argument, 1),
7844 tf_error);
7845 else if (TREE_CODE (argument) != TEMPLATE_DECL)
7846 cp_parser_error (parser, "expected template-name");
7848 if (cp_parser_parse_definitely (parser))
7849 return argument;
7850 /* It must be a non-type argument. There permitted cases are given
7851 in [temp.arg.nontype]:
7853 -- an integral constant-expression of integral or enumeration
7854 type; or
7856 -- the name of a non-type template-parameter; or
7858 -- the name of an object or function with external linkage...
7860 -- the address of an object or function with external linkage...
7862 -- a pointer to member... */
7863 /* Look for a non-type template parameter. */
7864 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7866 cp_parser_parse_tentatively (parser);
7867 argument = cp_parser_primary_expression (parser,
7868 &idk,
7869 &qualifying_class);
7870 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
7871 || !cp_parser_next_token_ends_template_argument_p (parser))
7872 cp_parser_simulate_error (parser);
7873 if (cp_parser_parse_definitely (parser))
7874 return argument;
7876 /* If the next token is "&", the argument must be the address of an
7877 object or function with external linkage. */
7878 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
7879 if (address_p)
7880 cp_lexer_consume_token (parser->lexer);
7881 /* See if we might have an id-expression. */
7882 token = cp_lexer_peek_token (parser->lexer);
7883 if (token->type == CPP_NAME
7884 || token->keyword == RID_OPERATOR
7885 || token->type == CPP_SCOPE
7886 || token->type == CPP_TEMPLATE_ID
7887 || token->type == CPP_NESTED_NAME_SPECIFIER)
7889 cp_parser_parse_tentatively (parser);
7890 argument = cp_parser_primary_expression (parser,
7891 &idk,
7892 &qualifying_class);
7893 if (cp_parser_error_occurred (parser)
7894 || !cp_parser_next_token_ends_template_argument_p (parser))
7895 cp_parser_abort_tentative_parse (parser);
7896 else
7898 if (qualifying_class)
7899 argument = finish_qualified_id_expr (qualifying_class,
7900 argument,
7901 /*done=*/true,
7902 address_p);
7903 if (TREE_CODE (argument) == VAR_DECL)
7905 /* A variable without external linkage might still be a
7906 valid constant-expression, so no error is issued here
7907 if the external-linkage check fails. */
7908 if (!DECL_EXTERNAL_LINKAGE_P (argument))
7909 cp_parser_simulate_error (parser);
7911 else if (is_overloaded_fn (argument))
7912 /* All overloaded functions are allowed; if the external
7913 linkage test does not pass, an error will be issued
7914 later. */
7916 else if (address_p
7917 && (TREE_CODE (argument) == OFFSET_REF
7918 || TREE_CODE (argument) == SCOPE_REF))
7919 /* A pointer-to-member. */
7921 else
7922 cp_parser_simulate_error (parser);
7924 if (cp_parser_parse_definitely (parser))
7926 if (address_p)
7927 argument = build_x_unary_op (ADDR_EXPR, argument);
7928 return argument;
7932 /* If the argument started with "&", there are no other valid
7933 alternatives at this point. */
7934 if (address_p)
7936 cp_parser_error (parser, "invalid non-type template argument");
7937 return error_mark_node;
7939 /* The argument must be a constant-expression. */
7940 argument = cp_parser_constant_expression (parser,
7941 /*allow_non_constant_p=*/false,
7942 /*non_constant_p=*/NULL);
7943 /* If it's non-dependent, simplify it. */
7944 return cp_parser_fold_non_dependent_expr (argument);
7947 /* Parse an explicit-instantiation.
7949 explicit-instantiation:
7950 template declaration
7952 Although the standard says `declaration', what it really means is:
7954 explicit-instantiation:
7955 template decl-specifier-seq [opt] declarator [opt] ;
7957 Things like `template int S<int>::i = 5, int S<double>::j;' are not
7958 supposed to be allowed. A defect report has been filed about this
7959 issue.
7961 GNU Extension:
7963 explicit-instantiation:
7964 storage-class-specifier template
7965 decl-specifier-seq [opt] declarator [opt] ;
7966 function-specifier template
7967 decl-specifier-seq [opt] declarator [opt] ; */
7969 static void
7970 cp_parser_explicit_instantiation (cp_parser* parser)
7972 int declares_class_or_enum;
7973 tree decl_specifiers;
7974 tree attributes;
7975 tree extension_specifier = NULL_TREE;
7977 /* Look for an (optional) storage-class-specifier or
7978 function-specifier. */
7979 if (cp_parser_allow_gnu_extensions_p (parser))
7981 extension_specifier
7982 = cp_parser_storage_class_specifier_opt (parser);
7983 if (!extension_specifier)
7984 extension_specifier = cp_parser_function_specifier_opt (parser);
7987 /* Look for the `template' keyword. */
7988 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
7989 /* Let the front end know that we are processing an explicit
7990 instantiation. */
7991 begin_explicit_instantiation ();
7992 /* [temp.explicit] says that we are supposed to ignore access
7993 control while processing explicit instantiation directives. */
7994 push_deferring_access_checks (dk_no_check);
7995 /* Parse a decl-specifier-seq. */
7996 decl_specifiers
7997 = cp_parser_decl_specifier_seq (parser,
7998 CP_PARSER_FLAGS_OPTIONAL,
7999 &attributes,
8000 &declares_class_or_enum);
8001 /* If there was exactly one decl-specifier, and it declared a class,
8002 and there's no declarator, then we have an explicit type
8003 instantiation. */
8004 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8006 tree type;
8008 type = check_tag_decl (decl_specifiers);
8009 /* Turn access control back on for names used during
8010 template instantiation. */
8011 pop_deferring_access_checks ();
8012 if (type)
8013 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8015 else
8017 tree declarator;
8018 tree decl;
8020 /* Parse the declarator. */
8021 declarator
8022 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8023 /*ctor_dtor_or_conv_p=*/NULL);
8024 cp_parser_check_for_definition_in_return_type (declarator,
8025 declares_class_or_enum);
8026 decl = grokdeclarator (declarator, decl_specifiers,
8027 NORMAL, 0, NULL);
8028 /* Turn access control back on for names used during
8029 template instantiation. */
8030 pop_deferring_access_checks ();
8031 /* Do the explicit instantiation. */
8032 do_decl_instantiation (decl, extension_specifier);
8034 /* We're done with the instantiation. */
8035 end_explicit_instantiation ();
8037 cp_parser_consume_semicolon_at_end_of_statement (parser);
8040 /* Parse an explicit-specialization.
8042 explicit-specialization:
8043 template < > declaration
8045 Although the standard says `declaration', what it really means is:
8047 explicit-specialization:
8048 template <> decl-specifier [opt] init-declarator [opt] ;
8049 template <> function-definition
8050 template <> explicit-specialization
8051 template <> template-declaration */
8053 static void
8054 cp_parser_explicit_specialization (cp_parser* parser)
8056 /* Look for the `template' keyword. */
8057 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8058 /* Look for the `<'. */
8059 cp_parser_require (parser, CPP_LESS, "`<'");
8060 /* Look for the `>'. */
8061 cp_parser_require (parser, CPP_GREATER, "`>'");
8062 /* We have processed another parameter list. */
8063 ++parser->num_template_parameter_lists;
8064 /* Let the front end know that we are beginning a specialization. */
8065 begin_specialization ();
8067 /* If the next keyword is `template', we need to figure out whether
8068 or not we're looking a template-declaration. */
8069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8071 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8072 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8073 cp_parser_template_declaration_after_export (parser,
8074 /*member_p=*/false);
8075 else
8076 cp_parser_explicit_specialization (parser);
8078 else
8079 /* Parse the dependent declaration. */
8080 cp_parser_single_declaration (parser,
8081 /*member_p=*/false,
8082 /*friend_p=*/NULL);
8084 /* We're done with the specialization. */
8085 end_specialization ();
8086 /* We're done with this parameter list. */
8087 --parser->num_template_parameter_lists;
8090 /* Parse a type-specifier.
8092 type-specifier:
8093 simple-type-specifier
8094 class-specifier
8095 enum-specifier
8096 elaborated-type-specifier
8097 cv-qualifier
8099 GNU Extension:
8101 type-specifier:
8102 __complex__
8104 Returns a representation of the type-specifier. If the
8105 type-specifier is a keyword (like `int' or `const', or
8106 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8107 For a class-specifier, enum-specifier, or elaborated-type-specifier
8108 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8110 If IS_FRIEND is TRUE then this type-specifier is being declared a
8111 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8112 appearing in a decl-specifier-seq.
8114 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8115 class-specifier, enum-specifier, or elaborated-type-specifier, then
8116 *DECLARES_CLASS_OR_ENUM is set to a non-zero value. The value is 1
8117 if a type is declared; 2 if it is defined. Otherwise, it is set to
8118 zero.
8120 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8121 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8122 is set to FALSE. */
8124 static tree
8125 cp_parser_type_specifier (cp_parser* parser,
8126 cp_parser_flags flags,
8127 bool is_friend,
8128 bool is_declaration,
8129 int* declares_class_or_enum,
8130 bool* is_cv_qualifier)
8132 tree type_spec = NULL_TREE;
8133 cp_token *token;
8134 enum rid keyword;
8136 /* Assume this type-specifier does not declare a new type. */
8137 if (declares_class_or_enum)
8138 *declares_class_or_enum = false;
8139 /* And that it does not specify a cv-qualifier. */
8140 if (is_cv_qualifier)
8141 *is_cv_qualifier = false;
8142 /* Peek at the next token. */
8143 token = cp_lexer_peek_token (parser->lexer);
8145 /* If we're looking at a keyword, we can use that to guide the
8146 production we choose. */
8147 keyword = token->keyword;
8148 switch (keyword)
8150 /* Any of these indicate either a class-specifier, or an
8151 elaborated-type-specifier. */
8152 case RID_CLASS:
8153 case RID_STRUCT:
8154 case RID_UNION:
8155 case RID_ENUM:
8156 /* Parse tentatively so that we can back up if we don't find a
8157 class-specifier or enum-specifier. */
8158 cp_parser_parse_tentatively (parser);
8159 /* Look for the class-specifier or enum-specifier. */
8160 if (keyword == RID_ENUM)
8161 type_spec = cp_parser_enum_specifier (parser);
8162 else
8163 type_spec = cp_parser_class_specifier (parser);
8165 /* If that worked, we're done. */
8166 if (cp_parser_parse_definitely (parser))
8168 if (declares_class_or_enum)
8169 *declares_class_or_enum = 2;
8170 return type_spec;
8173 /* Fall through. */
8175 case RID_TYPENAME:
8176 /* Look for an elaborated-type-specifier. */
8177 type_spec = cp_parser_elaborated_type_specifier (parser,
8178 is_friend,
8179 is_declaration);
8180 /* We're declaring a class or enum -- unless we're using
8181 `typename'. */
8182 if (declares_class_or_enum && keyword != RID_TYPENAME)
8183 *declares_class_or_enum = 1;
8184 return type_spec;
8186 case RID_CONST:
8187 case RID_VOLATILE:
8188 case RID_RESTRICT:
8189 type_spec = cp_parser_cv_qualifier_opt (parser);
8190 /* Even though we call a routine that looks for an optional
8191 qualifier, we know that there should be one. */
8192 my_friendly_assert (type_spec != NULL, 20000328);
8193 /* This type-specifier was a cv-qualified. */
8194 if (is_cv_qualifier)
8195 *is_cv_qualifier = true;
8197 return type_spec;
8199 case RID_COMPLEX:
8200 /* The `__complex__' keyword is a GNU extension. */
8201 return cp_lexer_consume_token (parser->lexer)->value;
8203 default:
8204 break;
8207 /* If we do not already have a type-specifier, assume we are looking
8208 at a simple-type-specifier. */
8209 type_spec = cp_parser_simple_type_specifier (parser, flags,
8210 /*identifier_p=*/true);
8212 /* If we didn't find a type-specifier, and a type-specifier was not
8213 optional in this context, issue an error message. */
8214 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8216 cp_parser_error (parser, "expected type specifier");
8217 return error_mark_node;
8220 return type_spec;
8223 /* Parse a simple-type-specifier.
8225 simple-type-specifier:
8226 :: [opt] nested-name-specifier [opt] type-name
8227 :: [opt] nested-name-specifier template template-id
8228 char
8229 wchar_t
8230 bool
8231 short
8233 long
8234 signed
8235 unsigned
8236 float
8237 double
8238 void
8240 GNU Extension:
8242 simple-type-specifier:
8243 __typeof__ unary-expression
8244 __typeof__ ( type-id )
8246 For the various keywords, the value returned is simply the
8247 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8248 For the first two productions, and if IDENTIFIER_P is false, the
8249 value returned is the indicated TYPE_DECL. */
8251 static tree
8252 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8253 bool identifier_p)
8255 tree type = NULL_TREE;
8256 cp_token *token;
8258 /* Peek at the next token. */
8259 token = cp_lexer_peek_token (parser->lexer);
8261 /* If we're looking at a keyword, things are easy. */
8262 switch (token->keyword)
8264 case RID_CHAR:
8265 type = char_type_node;
8266 break;
8267 case RID_WCHAR:
8268 type = wchar_type_node;
8269 break;
8270 case RID_BOOL:
8271 type = boolean_type_node;
8272 break;
8273 case RID_SHORT:
8274 type = short_integer_type_node;
8275 break;
8276 case RID_INT:
8277 type = integer_type_node;
8278 break;
8279 case RID_LONG:
8280 type = long_integer_type_node;
8281 break;
8282 case RID_SIGNED:
8283 type = integer_type_node;
8284 break;
8285 case RID_UNSIGNED:
8286 type = unsigned_type_node;
8287 break;
8288 case RID_FLOAT:
8289 type = float_type_node;
8290 break;
8291 case RID_DOUBLE:
8292 type = double_type_node;
8293 break;
8294 case RID_VOID:
8295 type = void_type_node;
8296 break;
8298 case RID_TYPEOF:
8300 tree operand;
8302 /* Consume the `typeof' token. */
8303 cp_lexer_consume_token (parser->lexer);
8304 /* Parse the operand to `typeof' */
8305 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8306 /* If it is not already a TYPE, take its type. */
8307 if (!TYPE_P (operand))
8308 operand = finish_typeof (operand);
8310 return operand;
8313 default:
8314 break;
8317 /* If the type-specifier was for a built-in type, we're done. */
8318 if (type)
8320 tree id;
8322 /* Consume the token. */
8323 id = cp_lexer_consume_token (parser->lexer)->value;
8324 return identifier_p ? id : TYPE_NAME (type);
8327 /* The type-specifier must be a user-defined type. */
8328 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8330 /* Don't gobble tokens or issue error messages if this is an
8331 optional type-specifier. */
8332 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8333 cp_parser_parse_tentatively (parser);
8335 /* Look for the optional `::' operator. */
8336 cp_parser_global_scope_opt (parser,
8337 /*current_scope_valid_p=*/false);
8338 /* Look for the nested-name specifier. */
8339 cp_parser_nested_name_specifier_opt (parser,
8340 /*typename_keyword_p=*/false,
8341 /*check_dependency_p=*/true,
8342 /*type_p=*/false);
8343 /* If we have seen a nested-name-specifier, and the next token
8344 is `template', then we are using the template-id production. */
8345 if (parser->scope
8346 && cp_parser_optional_template_keyword (parser))
8348 /* Look for the template-id. */
8349 type = cp_parser_template_id (parser,
8350 /*template_keyword_p=*/true,
8351 /*check_dependency_p=*/true);
8352 /* If the template-id did not name a type, we are out of
8353 luck. */
8354 if (TREE_CODE (type) != TYPE_DECL)
8356 cp_parser_error (parser, "expected template-id for type");
8357 type = NULL_TREE;
8360 /* Otherwise, look for a type-name. */
8361 else
8363 type = cp_parser_type_name (parser);
8364 if (type == error_mark_node)
8365 type = NULL_TREE;
8368 /* If it didn't work out, we don't have a TYPE. */
8369 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8370 && !cp_parser_parse_definitely (parser))
8371 type = NULL_TREE;
8374 /* If we didn't get a type-name, issue an error message. */
8375 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8377 cp_parser_error (parser, "expected type-name");
8378 return error_mark_node;
8381 return type;
8384 /* Parse a type-name.
8386 type-name:
8387 class-name
8388 enum-name
8389 typedef-name
8391 enum-name:
8392 identifier
8394 typedef-name:
8395 identifier
8397 Returns a TYPE_DECL for the the type. */
8399 static tree
8400 cp_parser_type_name (cp_parser* parser)
8402 tree type_decl;
8403 tree identifier;
8405 /* We can't know yet whether it is a class-name or not. */
8406 cp_parser_parse_tentatively (parser);
8407 /* Try a class-name. */
8408 type_decl = cp_parser_class_name (parser,
8409 /*typename_keyword_p=*/false,
8410 /*template_keyword_p=*/false,
8411 /*type_p=*/false,
8412 /*check_dependency_p=*/true,
8413 /*class_head_p=*/false);
8414 /* If it's not a class-name, keep looking. */
8415 if (!cp_parser_parse_definitely (parser))
8417 /* It must be a typedef-name or an enum-name. */
8418 identifier = cp_parser_identifier (parser);
8419 if (identifier == error_mark_node)
8420 return error_mark_node;
8422 /* Look up the type-name. */
8423 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8424 /* Issue an error if we did not find a type-name. */
8425 if (TREE_CODE (type_decl) != TYPE_DECL)
8427 cp_parser_error (parser, "expected type-name");
8428 type_decl = error_mark_node;
8430 /* Remember that the name was used in the definition of the
8431 current class so that we can check later to see if the
8432 meaning would have been different after the class was
8433 entirely defined. */
8434 else if (type_decl != error_mark_node
8435 && !parser->scope)
8436 maybe_note_name_used_in_class (identifier, type_decl);
8439 return type_decl;
8443 /* Parse an elaborated-type-specifier. Note that the grammar given
8444 here incorporates the resolution to DR68.
8446 elaborated-type-specifier:
8447 class-key :: [opt] nested-name-specifier [opt] identifier
8448 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8449 enum :: [opt] nested-name-specifier [opt] identifier
8450 typename :: [opt] nested-name-specifier identifier
8451 typename :: [opt] nested-name-specifier template [opt]
8452 template-id
8454 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8455 declared `friend'. If IS_DECLARATION is TRUE, then this
8456 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8457 something is being declared.
8459 Returns the TYPE specified. */
8461 static tree
8462 cp_parser_elaborated_type_specifier (cp_parser* parser,
8463 bool is_friend,
8464 bool is_declaration)
8466 enum tag_types tag_type;
8467 tree identifier;
8468 tree type = NULL_TREE;
8470 /* See if we're looking at the `enum' keyword. */
8471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8473 /* Consume the `enum' token. */
8474 cp_lexer_consume_token (parser->lexer);
8475 /* Remember that it's an enumeration type. */
8476 tag_type = enum_type;
8478 /* Or, it might be `typename'. */
8479 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8480 RID_TYPENAME))
8482 /* Consume the `typename' token. */
8483 cp_lexer_consume_token (parser->lexer);
8484 /* Remember that it's a `typename' type. */
8485 tag_type = typename_type;
8486 /* The `typename' keyword is only allowed in templates. */
8487 if (!processing_template_decl)
8488 pedwarn ("using `typename' outside of template");
8490 /* Otherwise it must be a class-key. */
8491 else
8493 tag_type = cp_parser_class_key (parser);
8494 if (tag_type == none_type)
8495 return error_mark_node;
8498 /* Look for the `::' operator. */
8499 cp_parser_global_scope_opt (parser,
8500 /*current_scope_valid_p=*/false);
8501 /* Look for the nested-name-specifier. */
8502 if (tag_type == typename_type)
8504 if (cp_parser_nested_name_specifier (parser,
8505 /*typename_keyword_p=*/true,
8506 /*check_dependency_p=*/true,
8507 /*type_p=*/true)
8508 == error_mark_node)
8509 return error_mark_node;
8511 else
8512 /* Even though `typename' is not present, the proposed resolution
8513 to Core Issue 180 says that in `class A<T>::B', `B' should be
8514 considered a type-name, even if `A<T>' is dependent. */
8515 cp_parser_nested_name_specifier_opt (parser,
8516 /*typename_keyword_p=*/true,
8517 /*check_dependency_p=*/true,
8518 /*type_p=*/true);
8519 /* For everything but enumeration types, consider a template-id. */
8520 if (tag_type != enum_type)
8522 bool template_p = false;
8523 tree decl;
8525 /* Allow the `template' keyword. */
8526 template_p = cp_parser_optional_template_keyword (parser);
8527 /* If we didn't see `template', we don't know if there's a
8528 template-id or not. */
8529 if (!template_p)
8530 cp_parser_parse_tentatively (parser);
8531 /* Parse the template-id. */
8532 decl = cp_parser_template_id (parser, template_p,
8533 /*check_dependency_p=*/true);
8534 /* If we didn't find a template-id, look for an ordinary
8535 identifier. */
8536 if (!template_p && !cp_parser_parse_definitely (parser))
8538 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8539 in effect, then we must assume that, upon instantiation, the
8540 template will correspond to a class. */
8541 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8542 && tag_type == typename_type)
8543 type = make_typename_type (parser->scope, decl,
8544 /*complain=*/1);
8545 else
8546 type = TREE_TYPE (decl);
8549 /* For an enumeration type, consider only a plain identifier. */
8550 if (!type)
8552 identifier = cp_parser_identifier (parser);
8554 if (identifier == error_mark_node)
8556 parser->scope = NULL_TREE;
8557 return error_mark_node;
8560 /* For a `typename', we needn't call xref_tag. */
8561 if (tag_type == typename_type)
8562 return make_typename_type (parser->scope, identifier,
8563 /*complain=*/1);
8564 /* Look up a qualified name in the usual way. */
8565 if (parser->scope)
8567 tree decl;
8569 /* In an elaborated-type-specifier, names are assumed to name
8570 types, so we set IS_TYPE to TRUE when calling
8571 cp_parser_lookup_name. */
8572 decl = cp_parser_lookup_name (parser, identifier,
8573 /*is_type=*/true,
8574 /*is_namespace=*/false,
8575 /*check_dependency=*/true);
8577 /* If we are parsing friend declaration, DECL may be a
8578 TEMPLATE_DECL tree node here. However, we need to check
8579 whether this TEMPLATE_DECL results in valid code. Consider
8580 the following example:
8582 namespace N {
8583 template <class T> class C {};
8585 class X {
8586 template <class T> friend class N::C; // #1, valid code
8588 template <class T> class Y {
8589 friend class N::C; // #2, invalid code
8592 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8593 name lookup of `N::C'. We see that friend declaration must
8594 be template for the code to be valid. Note that
8595 processing_template_decl does not work here since it is
8596 always 1 for the above two cases. */
8598 decl = (cp_parser_maybe_treat_template_as_class
8599 (decl, /*tag_name_p=*/is_friend
8600 && parser->num_template_parameter_lists));
8602 if (TREE_CODE (decl) != TYPE_DECL)
8604 error ("expected type-name");
8605 return error_mark_node;
8608 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
8609 check_elaborated_type_specifier
8610 (tag_type, decl,
8611 (parser->num_template_parameter_lists
8612 || DECL_SELF_REFERENCE_P (decl)));
8614 type = TREE_TYPE (decl);
8616 else
8618 /* An elaborated-type-specifier sometimes introduces a new type and
8619 sometimes names an existing type. Normally, the rule is that it
8620 introduces a new type only if there is not an existing type of
8621 the same name already in scope. For example, given:
8623 struct S {};
8624 void f() { struct S s; }
8626 the `struct S' in the body of `f' is the same `struct S' as in
8627 the global scope; the existing definition is used. However, if
8628 there were no global declaration, this would introduce a new
8629 local class named `S'.
8631 An exception to this rule applies to the following code:
8633 namespace N { struct S; }
8635 Here, the elaborated-type-specifier names a new type
8636 unconditionally; even if there is already an `S' in the
8637 containing scope this declaration names a new type.
8638 This exception only applies if the elaborated-type-specifier
8639 forms the complete declaration:
8641 [class.name]
8643 A declaration consisting solely of `class-key identifier ;' is
8644 either a redeclaration of the name in the current scope or a
8645 forward declaration of the identifier as a class name. It
8646 introduces the name into the current scope.
8648 We are in this situation precisely when the next token is a `;'.
8650 An exception to the exception is that a `friend' declaration does
8651 *not* name a new type; i.e., given:
8653 struct S { friend struct T; };
8655 `T' is not a new type in the scope of `S'.
8657 Also, `new struct S' or `sizeof (struct S)' never results in the
8658 definition of a new type; a new type can only be declared in a
8659 declaration context. */
8661 type = xref_tag (tag_type, identifier,
8662 /*attributes=*/NULL_TREE,
8663 (is_friend
8664 || !is_declaration
8665 || cp_lexer_next_token_is_not (parser->lexer,
8666 CPP_SEMICOLON)),
8667 parser->num_template_parameter_lists);
8670 if (tag_type != enum_type)
8671 cp_parser_check_class_key (tag_type, type);
8672 return type;
8675 /* Parse an enum-specifier.
8677 enum-specifier:
8678 enum identifier [opt] { enumerator-list [opt] }
8680 Returns an ENUM_TYPE representing the enumeration. */
8682 static tree
8683 cp_parser_enum_specifier (cp_parser* parser)
8685 cp_token *token;
8686 tree identifier = NULL_TREE;
8687 tree type;
8689 /* Look for the `enum' keyword. */
8690 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8691 return error_mark_node;
8692 /* Peek at the next token. */
8693 token = cp_lexer_peek_token (parser->lexer);
8695 /* See if it is an identifier. */
8696 if (token->type == CPP_NAME)
8697 identifier = cp_parser_identifier (parser);
8699 /* Look for the `{'. */
8700 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8701 return error_mark_node;
8703 /* At this point, we're going ahead with the enum-specifier, even
8704 if some other problem occurs. */
8705 cp_parser_commit_to_tentative_parse (parser);
8707 /* Issue an error message if type-definitions are forbidden here. */
8708 cp_parser_check_type_definition (parser);
8710 /* Create the new type. */
8711 type = start_enum (identifier ? identifier : make_anon_name ());
8713 /* Peek at the next token. */
8714 token = cp_lexer_peek_token (parser->lexer);
8715 /* If it's not a `}', then there are some enumerators. */
8716 if (token->type != CPP_CLOSE_BRACE)
8717 cp_parser_enumerator_list (parser, type);
8718 /* Look for the `}'. */
8719 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8721 /* Finish up the enumeration. */
8722 finish_enum (type);
8724 return type;
8727 /* Parse an enumerator-list. The enumerators all have the indicated
8728 TYPE.
8730 enumerator-list:
8731 enumerator-definition
8732 enumerator-list , enumerator-definition */
8734 static void
8735 cp_parser_enumerator_list (cp_parser* parser, tree type)
8737 while (true)
8739 cp_token *token;
8741 /* Parse an enumerator-definition. */
8742 cp_parser_enumerator_definition (parser, type);
8743 /* Peek at the next token. */
8744 token = cp_lexer_peek_token (parser->lexer);
8745 /* If it's not a `,', then we've reached the end of the
8746 list. */
8747 if (token->type != CPP_COMMA)
8748 break;
8749 /* Otherwise, consume the `,' and keep going. */
8750 cp_lexer_consume_token (parser->lexer);
8751 /* If the next token is a `}', there is a trailing comma. */
8752 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8754 if (pedantic && !in_system_header)
8755 pedwarn ("comma at end of enumerator list");
8756 break;
8761 /* Parse an enumerator-definition. The enumerator has the indicated
8762 TYPE.
8764 enumerator-definition:
8765 enumerator
8766 enumerator = constant-expression
8768 enumerator:
8769 identifier */
8771 static void
8772 cp_parser_enumerator_definition (cp_parser* parser, tree type)
8774 cp_token *token;
8775 tree identifier;
8776 tree value;
8778 /* Look for the identifier. */
8779 identifier = cp_parser_identifier (parser);
8780 if (identifier == error_mark_node)
8781 return;
8783 /* Peek at the next token. */
8784 token = cp_lexer_peek_token (parser->lexer);
8785 /* If it's an `=', then there's an explicit value. */
8786 if (token->type == CPP_EQ)
8788 /* Consume the `=' token. */
8789 cp_lexer_consume_token (parser->lexer);
8790 /* Parse the value. */
8791 value = cp_parser_constant_expression (parser,
8792 /*allow_non_constant_p=*/false,
8793 NULL);
8795 else
8796 value = NULL_TREE;
8798 /* Create the enumerator. */
8799 build_enumerator (identifier, value, type);
8802 /* Parse a namespace-name.
8804 namespace-name:
8805 original-namespace-name
8806 namespace-alias
8808 Returns the NAMESPACE_DECL for the namespace. */
8810 static tree
8811 cp_parser_namespace_name (cp_parser* parser)
8813 tree identifier;
8814 tree namespace_decl;
8816 /* Get the name of the namespace. */
8817 identifier = cp_parser_identifier (parser);
8818 if (identifier == error_mark_node)
8819 return error_mark_node;
8821 /* Look up the identifier in the currently active scope. Look only
8822 for namespaces, due to:
8824 [basic.lookup.udir]
8826 When looking up a namespace-name in a using-directive or alias
8827 definition, only namespace names are considered.
8829 And:
8831 [basic.lookup.qual]
8833 During the lookup of a name preceding the :: scope resolution
8834 operator, object, function, and enumerator names are ignored.
8836 (Note that cp_parser_class_or_namespace_name only calls this
8837 function if the token after the name is the scope resolution
8838 operator.) */
8839 namespace_decl = cp_parser_lookup_name (parser, identifier,
8840 /*is_type=*/false,
8841 /*is_namespace=*/true,
8842 /*check_dependency=*/true);
8843 /* If it's not a namespace, issue an error. */
8844 if (namespace_decl == error_mark_node
8845 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
8847 cp_parser_error (parser, "expected namespace-name");
8848 namespace_decl = error_mark_node;
8851 return namespace_decl;
8854 /* Parse a namespace-definition.
8856 namespace-definition:
8857 named-namespace-definition
8858 unnamed-namespace-definition
8860 named-namespace-definition:
8861 original-namespace-definition
8862 extension-namespace-definition
8864 original-namespace-definition:
8865 namespace identifier { namespace-body }
8867 extension-namespace-definition:
8868 namespace original-namespace-name { namespace-body }
8870 unnamed-namespace-definition:
8871 namespace { namespace-body } */
8873 static void
8874 cp_parser_namespace_definition (cp_parser* parser)
8876 tree identifier;
8878 /* Look for the `namespace' keyword. */
8879 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8881 /* Get the name of the namespace. We do not attempt to distinguish
8882 between an original-namespace-definition and an
8883 extension-namespace-definition at this point. The semantic
8884 analysis routines are responsible for that. */
8885 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8886 identifier = cp_parser_identifier (parser);
8887 else
8888 identifier = NULL_TREE;
8890 /* Look for the `{' to start the namespace. */
8891 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
8892 /* Start the namespace. */
8893 push_namespace (identifier);
8894 /* Parse the body of the namespace. */
8895 cp_parser_namespace_body (parser);
8896 /* Finish the namespace. */
8897 pop_namespace ();
8898 /* Look for the final `}'. */
8899 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8902 /* Parse a namespace-body.
8904 namespace-body:
8905 declaration-seq [opt] */
8907 static void
8908 cp_parser_namespace_body (cp_parser* parser)
8910 cp_parser_declaration_seq_opt (parser);
8913 /* Parse a namespace-alias-definition.
8915 namespace-alias-definition:
8916 namespace identifier = qualified-namespace-specifier ; */
8918 static void
8919 cp_parser_namespace_alias_definition (cp_parser* parser)
8921 tree identifier;
8922 tree namespace_specifier;
8924 /* Look for the `namespace' keyword. */
8925 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8926 /* Look for the identifier. */
8927 identifier = cp_parser_identifier (parser);
8928 if (identifier == error_mark_node)
8929 return;
8930 /* Look for the `=' token. */
8931 cp_parser_require (parser, CPP_EQ, "`='");
8932 /* Look for the qualified-namespace-specifier. */
8933 namespace_specifier
8934 = cp_parser_qualified_namespace_specifier (parser);
8935 /* Look for the `;' token. */
8936 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8938 /* Register the alias in the symbol table. */
8939 do_namespace_alias (identifier, namespace_specifier);
8942 /* Parse a qualified-namespace-specifier.
8944 qualified-namespace-specifier:
8945 :: [opt] nested-name-specifier [opt] namespace-name
8947 Returns a NAMESPACE_DECL corresponding to the specified
8948 namespace. */
8950 static tree
8951 cp_parser_qualified_namespace_specifier (cp_parser* parser)
8953 /* Look for the optional `::'. */
8954 cp_parser_global_scope_opt (parser,
8955 /*current_scope_valid_p=*/false);
8957 /* Look for the optional nested-name-specifier. */
8958 cp_parser_nested_name_specifier_opt (parser,
8959 /*typename_keyword_p=*/false,
8960 /*check_dependency_p=*/true,
8961 /*type_p=*/false);
8963 return cp_parser_namespace_name (parser);
8966 /* Parse a using-declaration.
8968 using-declaration:
8969 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
8970 using :: unqualified-id ; */
8972 static void
8973 cp_parser_using_declaration (cp_parser* parser)
8975 cp_token *token;
8976 bool typename_p = false;
8977 bool global_scope_p;
8978 tree decl;
8979 tree identifier;
8980 tree scope;
8982 /* Look for the `using' keyword. */
8983 cp_parser_require_keyword (parser, RID_USING, "`using'");
8985 /* Peek at the next token. */
8986 token = cp_lexer_peek_token (parser->lexer);
8987 /* See if it's `typename'. */
8988 if (token->keyword == RID_TYPENAME)
8990 /* Remember that we've seen it. */
8991 typename_p = true;
8992 /* Consume the `typename' token. */
8993 cp_lexer_consume_token (parser->lexer);
8996 /* Look for the optional global scope qualification. */
8997 global_scope_p
8998 = (cp_parser_global_scope_opt (parser,
8999 /*current_scope_valid_p=*/false)
9000 != NULL_TREE);
9002 /* If we saw `typename', or didn't see `::', then there must be a
9003 nested-name-specifier present. */
9004 if (typename_p || !global_scope_p)
9005 cp_parser_nested_name_specifier (parser, typename_p,
9006 /*check_dependency_p=*/true,
9007 /*type_p=*/false);
9008 /* Otherwise, we could be in either of the two productions. In that
9009 case, treat the nested-name-specifier as optional. */
9010 else
9011 cp_parser_nested_name_specifier_opt (parser,
9012 /*typename_keyword_p=*/false,
9013 /*check_dependency_p=*/true,
9014 /*type_p=*/false);
9016 /* Parse the unqualified-id. */
9017 identifier = cp_parser_unqualified_id (parser,
9018 /*template_keyword_p=*/false,
9019 /*check_dependency_p=*/true,
9020 /*declarator_p=*/true);
9022 /* The function we call to handle a using-declaration is different
9023 depending on what scope we are in. */
9024 if (identifier == error_mark_node)
9026 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9027 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9028 /* [namespace.udecl]
9030 A using declaration shall not name a template-id. */
9031 error ("a template-id may not appear in a using-declaration");
9032 else
9034 scope = current_scope ();
9035 if (scope && TYPE_P (scope))
9037 /* Create the USING_DECL. */
9038 decl = do_class_using_decl (build_nt (SCOPE_REF,
9039 parser->scope,
9040 identifier));
9041 /* Add it to the list of members in this class. */
9042 finish_member_declaration (decl);
9044 else
9046 decl = cp_parser_lookup_name_simple (parser, identifier);
9047 if (decl == error_mark_node)
9049 if (parser->scope && parser->scope != global_namespace)
9050 error ("`%D::%D' has not been declared",
9051 parser->scope, identifier);
9052 else
9053 error ("`::%D' has not been declared", identifier);
9055 else if (scope)
9056 do_local_using_decl (decl);
9057 else
9058 do_toplevel_using_decl (decl);
9062 /* Look for the final `;'. */
9063 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9066 /* Parse a using-directive.
9068 using-directive:
9069 using namespace :: [opt] nested-name-specifier [opt]
9070 namespace-name ; */
9072 static void
9073 cp_parser_using_directive (cp_parser* parser)
9075 tree namespace_decl;
9077 /* Look for the `using' keyword. */
9078 cp_parser_require_keyword (parser, RID_USING, "`using'");
9079 /* And the `namespace' keyword. */
9080 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9081 /* Look for the optional `::' operator. */
9082 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9083 /* And the optional nested-name-specifier. */
9084 cp_parser_nested_name_specifier_opt (parser,
9085 /*typename_keyword_p=*/false,
9086 /*check_dependency_p=*/true,
9087 /*type_p=*/false);
9088 /* Get the namespace being used. */
9089 namespace_decl = cp_parser_namespace_name (parser);
9090 /* Update the symbol table. */
9091 do_using_directive (namespace_decl);
9092 /* Look for the final `;'. */
9093 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9096 /* Parse an asm-definition.
9098 asm-definition:
9099 asm ( string-literal ) ;
9101 GNU Extension:
9103 asm-definition:
9104 asm volatile [opt] ( string-literal ) ;
9105 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9106 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9107 : asm-operand-list [opt] ) ;
9108 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9109 : asm-operand-list [opt]
9110 : asm-operand-list [opt] ) ; */
9112 static void
9113 cp_parser_asm_definition (cp_parser* parser)
9115 cp_token *token;
9116 tree string;
9117 tree outputs = NULL_TREE;
9118 tree inputs = NULL_TREE;
9119 tree clobbers = NULL_TREE;
9120 tree asm_stmt;
9121 bool volatile_p = false;
9122 bool extended_p = false;
9124 /* Look for the `asm' keyword. */
9125 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9126 /* See if the next token is `volatile'. */
9127 if (cp_parser_allow_gnu_extensions_p (parser)
9128 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9130 /* Remember that we saw the `volatile' keyword. */
9131 volatile_p = true;
9132 /* Consume the token. */
9133 cp_lexer_consume_token (parser->lexer);
9135 /* Look for the opening `('. */
9136 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9137 /* Look for the string. */
9138 token = cp_parser_require (parser, CPP_STRING, "asm body");
9139 if (!token)
9140 return;
9141 string = token->value;
9142 /* If we're allowing GNU extensions, check for the extended assembly
9143 syntax. Unfortunately, the `:' tokens need not be separated by
9144 a space in C, and so, for compatibility, we tolerate that here
9145 too. Doing that means that we have to treat the `::' operator as
9146 two `:' tokens. */
9147 if (cp_parser_allow_gnu_extensions_p (parser)
9148 && at_function_scope_p ()
9149 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9150 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9152 bool inputs_p = false;
9153 bool clobbers_p = false;
9155 /* The extended syntax was used. */
9156 extended_p = true;
9158 /* Look for outputs. */
9159 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9161 /* Consume the `:'. */
9162 cp_lexer_consume_token (parser->lexer);
9163 /* Parse the output-operands. */
9164 if (cp_lexer_next_token_is_not (parser->lexer,
9165 CPP_COLON)
9166 && cp_lexer_next_token_is_not (parser->lexer,
9167 CPP_SCOPE)
9168 && cp_lexer_next_token_is_not (parser->lexer,
9169 CPP_CLOSE_PAREN))
9170 outputs = cp_parser_asm_operand_list (parser);
9172 /* If the next token is `::', there are no outputs, and the
9173 next token is the beginning of the inputs. */
9174 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9176 /* Consume the `::' token. */
9177 cp_lexer_consume_token (parser->lexer);
9178 /* The inputs are coming next. */
9179 inputs_p = true;
9182 /* Look for inputs. */
9183 if (inputs_p
9184 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9186 if (!inputs_p)
9187 /* Consume the `:'. */
9188 cp_lexer_consume_token (parser->lexer);
9189 /* Parse the output-operands. */
9190 if (cp_lexer_next_token_is_not (parser->lexer,
9191 CPP_COLON)
9192 && cp_lexer_next_token_is_not (parser->lexer,
9193 CPP_SCOPE)
9194 && cp_lexer_next_token_is_not (parser->lexer,
9195 CPP_CLOSE_PAREN))
9196 inputs = cp_parser_asm_operand_list (parser);
9198 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9199 /* The clobbers are coming next. */
9200 clobbers_p = true;
9202 /* Look for clobbers. */
9203 if (clobbers_p
9204 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9206 if (!clobbers_p)
9207 /* Consume the `:'. */
9208 cp_lexer_consume_token (parser->lexer);
9209 /* Parse the clobbers. */
9210 if (cp_lexer_next_token_is_not (parser->lexer,
9211 CPP_CLOSE_PAREN))
9212 clobbers = cp_parser_asm_clobber_list (parser);
9215 /* Look for the closing `)'. */
9216 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9217 cp_parser_skip_to_closing_parenthesis (parser, true, false);
9218 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9220 /* Create the ASM_STMT. */
9221 if (at_function_scope_p ())
9223 asm_stmt =
9224 finish_asm_stmt (volatile_p
9225 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9226 string, outputs, inputs, clobbers);
9227 /* If the extended syntax was not used, mark the ASM_STMT. */
9228 if (!extended_p)
9229 ASM_INPUT_P (asm_stmt) = 1;
9231 else
9232 assemble_asm (string);
9235 /* Declarators [gram.dcl.decl] */
9237 /* Parse an init-declarator.
9239 init-declarator:
9240 declarator initializer [opt]
9242 GNU Extension:
9244 init-declarator:
9245 declarator asm-specification [opt] attributes [opt] initializer [opt]
9247 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9248 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9249 then this declarator appears in a class scope. The new DECL created
9250 by this declarator is returned.
9252 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9253 for a function-definition here as well. If the declarator is a
9254 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9255 be TRUE upon return. By that point, the function-definition will
9256 have been completely parsed.
9258 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9259 is FALSE. */
9261 static tree
9262 cp_parser_init_declarator (cp_parser* parser,
9263 tree decl_specifiers,
9264 tree prefix_attributes,
9265 bool function_definition_allowed_p,
9266 bool member_p,
9267 int declares_class_or_enum,
9268 bool* function_definition_p)
9270 cp_token *token;
9271 tree declarator;
9272 tree attributes;
9273 tree asm_specification;
9274 tree initializer;
9275 tree decl = NULL_TREE;
9276 tree scope;
9277 bool is_initialized;
9278 bool is_parenthesized_init;
9279 bool is_non_constant_init;
9280 int ctor_dtor_or_conv_p;
9281 bool friend_p;
9283 /* Assume that this is not the declarator for a function
9284 definition. */
9285 if (function_definition_p)
9286 *function_definition_p = false;
9288 /* Defer access checks while parsing the declarator; we cannot know
9289 what names are accessible until we know what is being
9290 declared. */
9291 resume_deferring_access_checks ();
9293 /* Parse the declarator. */
9294 declarator
9295 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9296 &ctor_dtor_or_conv_p);
9297 /* Gather up the deferred checks. */
9298 stop_deferring_access_checks ();
9300 /* If the DECLARATOR was erroneous, there's no need to go
9301 further. */
9302 if (declarator == error_mark_node)
9303 return error_mark_node;
9305 cp_parser_check_for_definition_in_return_type (declarator,
9306 declares_class_or_enum);
9308 /* Figure out what scope the entity declared by the DECLARATOR is
9309 located in. `grokdeclarator' sometimes changes the scope, so
9310 we compute it now. */
9311 scope = get_scope_of_declarator (declarator);
9313 /* If we're allowing GNU extensions, look for an asm-specification
9314 and attributes. */
9315 if (cp_parser_allow_gnu_extensions_p (parser))
9317 /* Look for an asm-specification. */
9318 asm_specification = cp_parser_asm_specification_opt (parser);
9319 /* And attributes. */
9320 attributes = cp_parser_attributes_opt (parser);
9322 else
9324 asm_specification = NULL_TREE;
9325 attributes = NULL_TREE;
9328 /* Peek at the next token. */
9329 token = cp_lexer_peek_token (parser->lexer);
9330 /* Check to see if the token indicates the start of a
9331 function-definition. */
9332 if (cp_parser_token_starts_function_definition_p (token))
9334 if (!function_definition_allowed_p)
9336 /* If a function-definition should not appear here, issue an
9337 error message. */
9338 cp_parser_error (parser,
9339 "a function-definition is not allowed here");
9340 return error_mark_node;
9342 else
9344 /* Neither attributes nor an asm-specification are allowed
9345 on a function-definition. */
9346 if (asm_specification)
9347 error ("an asm-specification is not allowed on a function-definition");
9348 if (attributes)
9349 error ("attributes are not allowed on a function-definition");
9350 /* This is a function-definition. */
9351 *function_definition_p = true;
9353 /* Parse the function definition. */
9354 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9355 (parser, decl_specifiers, prefix_attributes, declarator));
9357 return decl;
9361 /* [dcl.dcl]
9363 Only in function declarations for constructors, destructors, and
9364 type conversions can the decl-specifier-seq be omitted.
9366 We explicitly postpone this check past the point where we handle
9367 function-definitions because we tolerate function-definitions
9368 that are missing their return types in some modes. */
9369 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9371 cp_parser_error (parser,
9372 "expected constructor, destructor, or type conversion");
9373 return error_mark_node;
9376 /* An `=' or an `(' indicates an initializer. */
9377 is_initialized = (token->type == CPP_EQ
9378 || token->type == CPP_OPEN_PAREN);
9379 /* If the init-declarator isn't initialized and isn't followed by a
9380 `,' or `;', it's not a valid init-declarator. */
9381 if (!is_initialized
9382 && token->type != CPP_COMMA
9383 && token->type != CPP_SEMICOLON)
9385 cp_parser_error (parser, "expected init-declarator");
9386 return error_mark_node;
9389 /* Because start_decl has side-effects, we should only call it if we
9390 know we're going ahead. By this point, we know that we cannot
9391 possibly be looking at any other construct. */
9392 cp_parser_commit_to_tentative_parse (parser);
9394 /* Check to see whether or not this declaration is a friend. */
9395 friend_p = cp_parser_friend_p (decl_specifiers);
9397 /* Check that the number of template-parameter-lists is OK. */
9398 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9399 return error_mark_node;
9401 /* Enter the newly declared entry in the symbol table. If we're
9402 processing a declaration in a class-specifier, we wait until
9403 after processing the initializer. */
9404 if (!member_p)
9406 if (parser->in_unbraced_linkage_specification_p)
9408 decl_specifiers = tree_cons (error_mark_node,
9409 get_identifier ("extern"),
9410 decl_specifiers);
9411 have_extern_spec = false;
9413 decl = start_decl (declarator, decl_specifiers,
9414 is_initialized, attributes, prefix_attributes);
9417 /* Enter the SCOPE. That way unqualified names appearing in the
9418 initializer will be looked up in SCOPE. */
9419 if (scope)
9420 push_scope (scope);
9422 /* Perform deferred access control checks, now that we know in which
9423 SCOPE the declared entity resides. */
9424 if (!member_p && decl)
9426 tree saved_current_function_decl = NULL_TREE;
9428 /* If the entity being declared is a function, pretend that we
9429 are in its scope. If it is a `friend', it may have access to
9430 things that would not otherwise be accessible. */
9431 if (TREE_CODE (decl) == FUNCTION_DECL)
9433 saved_current_function_decl = current_function_decl;
9434 current_function_decl = decl;
9437 /* Perform the access control checks for the declarator and the
9438 the decl-specifiers. */
9439 perform_deferred_access_checks ();
9441 /* Restore the saved value. */
9442 if (TREE_CODE (decl) == FUNCTION_DECL)
9443 current_function_decl = saved_current_function_decl;
9446 /* Parse the initializer. */
9447 if (is_initialized)
9448 initializer = cp_parser_initializer (parser,
9449 &is_parenthesized_init,
9450 &is_non_constant_init);
9451 else
9453 initializer = NULL_TREE;
9454 is_parenthesized_init = false;
9455 is_non_constant_init = true;
9458 /* The old parser allows attributes to appear after a parenthesized
9459 initializer. Mark Mitchell proposed removing this functionality
9460 on the GCC mailing lists on 2002-08-13. This parser accepts the
9461 attributes -- but ignores them. */
9462 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9463 if (cp_parser_attributes_opt (parser))
9464 warning ("attributes after parenthesized initializer ignored");
9466 /* Leave the SCOPE, now that we have processed the initializer. It
9467 is important to do this before calling cp_finish_decl because it
9468 makes decisions about whether to create DECL_STMTs or not based
9469 on the current scope. */
9470 if (scope)
9471 pop_scope (scope);
9473 /* For an in-class declaration, use `grokfield' to create the
9474 declaration. */
9475 if (member_p)
9477 decl = grokfield (declarator, decl_specifiers,
9478 initializer, /*asmspec=*/NULL_TREE,
9479 /*attributes=*/NULL_TREE);
9480 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9481 cp_parser_save_default_args (parser, decl);
9484 /* Finish processing the declaration. But, skip friend
9485 declarations. */
9486 if (!friend_p && decl)
9487 cp_finish_decl (decl,
9488 initializer,
9489 asm_specification,
9490 /* If the initializer is in parentheses, then this is
9491 a direct-initialization, which means that an
9492 `explicit' constructor is OK. Otherwise, an
9493 `explicit' constructor cannot be used. */
9494 ((is_parenthesized_init || !is_initialized)
9495 ? 0 : LOOKUP_ONLYCONVERTING));
9497 /* Remember whether or not variables were initialized by
9498 constant-expressions. */
9499 if (decl && TREE_CODE (decl) == VAR_DECL
9500 && is_initialized && !is_non_constant_init)
9501 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9503 return decl;
9506 /* Parse a declarator.
9508 declarator:
9509 direct-declarator
9510 ptr-operator declarator
9512 abstract-declarator:
9513 ptr-operator abstract-declarator [opt]
9514 direct-abstract-declarator
9516 GNU Extensions:
9518 declarator:
9519 attributes [opt] direct-declarator
9520 attributes [opt] ptr-operator declarator
9522 abstract-declarator:
9523 attributes [opt] ptr-operator abstract-declarator [opt]
9524 attributes [opt] direct-abstract-declarator
9526 Returns a representation of the declarator. If the declarator has
9527 the form `* declarator', then an INDIRECT_REF is returned, whose
9528 only operand is the sub-declarator. Analogously, `& declarator' is
9529 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9530 used. The first operand is the TYPE for `X'. The second operand
9531 is an INDIRECT_REF whose operand is the sub-declarator.
9533 Otherwise, the representation is as for a direct-declarator.
9535 (It would be better to define a structure type to represent
9536 declarators, rather than abusing `tree' nodes to represent
9537 declarators. That would be much clearer and save some memory.
9538 There is no reason for declarators to be garbage-collected, for
9539 example; they are created during parser and no longer needed after
9540 `grokdeclarator' has been called.)
9542 For a ptr-operator that has the optional cv-qualifier-seq,
9543 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9544 node.
9546 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9547 detect constructor, destructor or conversion operators. It is set
9548 to -1 if the declarator is a name, and +1 if it is a
9549 function. Otherwise it is set to zero. Usually you just want to
9550 test for >0, but internally the negative value is used.
9552 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9553 a decl-specifier-seq unless it declares a constructor, destructor,
9554 or conversion. It might seem that we could check this condition in
9555 semantic analysis, rather than parsing, but that makes it difficult
9556 to handle something like `f()'. We want to notice that there are
9557 no decl-specifiers, and therefore realize that this is an
9558 expression, not a declaration.) */
9560 static tree
9561 cp_parser_declarator (cp_parser* parser,
9562 cp_parser_declarator_kind dcl_kind,
9563 int* ctor_dtor_or_conv_p)
9565 cp_token *token;
9566 tree declarator;
9567 enum tree_code code;
9568 tree cv_qualifier_seq;
9569 tree class_type;
9570 tree attributes = NULL_TREE;
9572 /* Assume this is not a constructor, destructor, or type-conversion
9573 operator. */
9574 if (ctor_dtor_or_conv_p)
9575 *ctor_dtor_or_conv_p = 0;
9577 if (cp_parser_allow_gnu_extensions_p (parser))
9578 attributes = cp_parser_attributes_opt (parser);
9580 /* Peek at the next token. */
9581 token = cp_lexer_peek_token (parser->lexer);
9583 /* Check for the ptr-operator production. */
9584 cp_parser_parse_tentatively (parser);
9585 /* Parse the ptr-operator. */
9586 code = cp_parser_ptr_operator (parser,
9587 &class_type,
9588 &cv_qualifier_seq);
9589 /* If that worked, then we have a ptr-operator. */
9590 if (cp_parser_parse_definitely (parser))
9592 /* The dependent declarator is optional if we are parsing an
9593 abstract-declarator. */
9594 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9595 cp_parser_parse_tentatively (parser);
9597 /* Parse the dependent declarator. */
9598 declarator = cp_parser_declarator (parser, dcl_kind,
9599 /*ctor_dtor_or_conv_p=*/NULL);
9601 /* If we are parsing an abstract-declarator, we must handle the
9602 case where the dependent declarator is absent. */
9603 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9604 && !cp_parser_parse_definitely (parser))
9605 declarator = NULL_TREE;
9607 /* Build the representation of the ptr-operator. */
9608 if (code == INDIRECT_REF)
9609 declarator = make_pointer_declarator (cv_qualifier_seq,
9610 declarator);
9611 else
9612 declarator = make_reference_declarator (cv_qualifier_seq,
9613 declarator);
9614 /* Handle the pointer-to-member case. */
9615 if (class_type)
9616 declarator = build_nt (SCOPE_REF, class_type, declarator);
9618 /* Everything else is a direct-declarator. */
9619 else
9620 declarator = cp_parser_direct_declarator (parser, dcl_kind,
9621 ctor_dtor_or_conv_p);
9623 if (attributes && declarator != error_mark_node)
9624 declarator = tree_cons (attributes, declarator, NULL_TREE);
9626 return declarator;
9629 /* Parse a direct-declarator or direct-abstract-declarator.
9631 direct-declarator:
9632 declarator-id
9633 direct-declarator ( parameter-declaration-clause )
9634 cv-qualifier-seq [opt]
9635 exception-specification [opt]
9636 direct-declarator [ constant-expression [opt] ]
9637 ( declarator )
9639 direct-abstract-declarator:
9640 direct-abstract-declarator [opt]
9641 ( parameter-declaration-clause )
9642 cv-qualifier-seq [opt]
9643 exception-specification [opt]
9644 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9645 ( abstract-declarator )
9647 Returns a representation of the declarator. DCL_KIND is
9648 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9649 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9650 we are parsing a direct-declarator. It is
9651 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9652 of ambiguity we prefer an abstract declarator, as per
9653 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9654 cp_parser_declarator.
9656 For the declarator-id production, the representation is as for an
9657 id-expression, except that a qualified name is represented as a
9658 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9659 see the documentation of the FUNCTION_DECLARATOR_* macros for
9660 information about how to find the various declarator components.
9661 An array-declarator is represented as an ARRAY_REF. The
9662 direct-declarator is the first operand; the constant-expression
9663 indicating the size of the array is the second operand. */
9665 static tree
9666 cp_parser_direct_declarator (cp_parser* parser,
9667 cp_parser_declarator_kind dcl_kind,
9668 int* ctor_dtor_or_conv_p)
9670 cp_token *token;
9671 tree declarator = NULL_TREE;
9672 tree scope = NULL_TREE;
9673 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9674 bool saved_in_declarator_p = parser->in_declarator_p;
9675 bool first = true;
9677 while (true)
9679 /* Peek at the next token. */
9680 token = cp_lexer_peek_token (parser->lexer);
9681 if (token->type == CPP_OPEN_PAREN)
9683 /* This is either a parameter-declaration-clause, or a
9684 parenthesized declarator. When we know we are parsing a
9685 named declarator, it must be a parenthesized declarator
9686 if FIRST is true. For instance, `(int)' is a
9687 parameter-declaration-clause, with an omitted
9688 direct-abstract-declarator. But `((*))', is a
9689 parenthesized abstract declarator. Finally, when T is a
9690 template parameter `(T)' is a
9691 parameter-declaration-clause, and not a parenthesized
9692 named declarator.
9694 We first try and parse a parameter-declaration-clause,
9695 and then try a nested declarator (if FIRST is true).
9697 It is not an error for it not to be a
9698 parameter-declaration-clause, even when FIRST is
9699 false. Consider,
9701 int i (int);
9702 int i (3);
9704 The first is the declaration of a function while the
9705 second is a the definition of a variable, including its
9706 initializer.
9708 Having seen only the parenthesis, we cannot know which of
9709 these two alternatives should be selected. Even more
9710 complex are examples like:
9712 int i (int (a));
9713 int i (int (3));
9715 The former is a function-declaration; the latter is a
9716 variable initialization.
9718 Thus again, we try a parameter-declaration-clause, and if
9719 that fails, we back out and return. */
9721 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9723 tree params;
9725 cp_parser_parse_tentatively (parser);
9727 /* Consume the `('. */
9728 cp_lexer_consume_token (parser->lexer);
9729 if (first)
9731 /* If this is going to be an abstract declarator, we're
9732 in a declarator and we can't have default args. */
9733 parser->default_arg_ok_p = false;
9734 parser->in_declarator_p = true;
9737 /* Parse the parameter-declaration-clause. */
9738 params = cp_parser_parameter_declaration_clause (parser);
9740 /* If all went well, parse the cv-qualifier-seq and the
9741 exception-specification. */
9742 if (cp_parser_parse_definitely (parser))
9744 tree cv_qualifiers;
9745 tree exception_specification;
9747 if (ctor_dtor_or_conv_p)
9748 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
9749 first = false;
9750 /* Consume the `)'. */
9751 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9753 /* Parse the cv-qualifier-seq. */
9754 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9755 /* And the exception-specification. */
9756 exception_specification
9757 = cp_parser_exception_specification_opt (parser);
9759 /* Create the function-declarator. */
9760 declarator = make_call_declarator (declarator,
9761 params,
9762 cv_qualifiers,
9763 exception_specification);
9764 /* Any subsequent parameter lists are to do with
9765 return type, so are not those of the declared
9766 function. */
9767 parser->default_arg_ok_p = false;
9769 /* Repeat the main loop. */
9770 continue;
9774 /* If this is the first, we can try a parenthesized
9775 declarator. */
9776 if (first)
9778 parser->default_arg_ok_p = saved_default_arg_ok_p;
9779 parser->in_declarator_p = saved_in_declarator_p;
9781 /* Consume the `('. */
9782 cp_lexer_consume_token (parser->lexer);
9783 /* Parse the nested declarator. */
9784 declarator
9785 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
9786 first = false;
9787 /* Expect a `)'. */
9788 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9789 declarator = error_mark_node;
9790 if (declarator == error_mark_node)
9791 break;
9793 goto handle_declarator;
9795 /* Otherwise, we must be done. */
9796 else
9797 break;
9799 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9800 && token->type == CPP_OPEN_SQUARE)
9802 /* Parse an array-declarator. */
9803 tree bounds;
9805 if (ctor_dtor_or_conv_p)
9806 *ctor_dtor_or_conv_p = 0;
9808 first = false;
9809 parser->default_arg_ok_p = false;
9810 parser->in_declarator_p = true;
9811 /* Consume the `['. */
9812 cp_lexer_consume_token (parser->lexer);
9813 /* Peek at the next token. */
9814 token = cp_lexer_peek_token (parser->lexer);
9815 /* If the next token is `]', then there is no
9816 constant-expression. */
9817 if (token->type != CPP_CLOSE_SQUARE)
9819 bool non_constant_p;
9821 bounds
9822 = cp_parser_constant_expression (parser,
9823 /*allow_non_constant=*/true,
9824 &non_constant_p);
9825 if (!non_constant_p)
9826 bounds = cp_parser_fold_non_dependent_expr (bounds);
9828 else
9829 bounds = NULL_TREE;
9830 /* Look for the closing `]'. */
9831 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
9833 declarator = error_mark_node;
9834 break;
9837 declarator = build_nt (ARRAY_REF, declarator, bounds);
9839 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
9841 /* Parse a declarator_id */
9842 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9843 cp_parser_parse_tentatively (parser);
9844 declarator = cp_parser_declarator_id (parser);
9845 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9847 if (!cp_parser_parse_definitely (parser))
9848 declarator = error_mark_node;
9849 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
9851 cp_parser_error (parser, "expected unqualified-id");
9852 declarator = error_mark_node;
9856 if (declarator == error_mark_node)
9857 break;
9859 if (TREE_CODE (declarator) == SCOPE_REF)
9861 tree scope = TREE_OPERAND (declarator, 0);
9863 /* In the declaration of a member of a template class
9864 outside of the class itself, the SCOPE will sometimes
9865 be a TYPENAME_TYPE. For example, given:
9867 template <typename T>
9868 int S<T>::R::i = 3;
9870 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
9871 this context, we must resolve S<T>::R to an ordinary
9872 type, rather than a typename type.
9874 The reason we normally avoid resolving TYPENAME_TYPEs
9875 is that a specialization of `S' might render
9876 `S<T>::R' not a type. However, if `S' is
9877 specialized, then this `i' will not be used, so there
9878 is no harm in resolving the types here. */
9879 if (TREE_CODE (scope) == TYPENAME_TYPE)
9881 tree type;
9883 /* Resolve the TYPENAME_TYPE. */
9884 type = resolve_typename_type (scope,
9885 /*only_current_p=*/false);
9886 /* If that failed, the declarator is invalid. */
9887 if (type != error_mark_node)
9888 scope = type;
9889 /* Build a new DECLARATOR. */
9890 declarator = build_nt (SCOPE_REF,
9891 scope,
9892 TREE_OPERAND (declarator, 1));
9896 /* Check to see whether the declarator-id names a constructor,
9897 destructor, or conversion. */
9898 if (declarator && ctor_dtor_or_conv_p
9899 && ((TREE_CODE (declarator) == SCOPE_REF
9900 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
9901 || (TREE_CODE (declarator) != SCOPE_REF
9902 && at_class_scope_p ())))
9904 tree unqualified_name;
9905 tree class_type;
9907 /* Get the unqualified part of the name. */
9908 if (TREE_CODE (declarator) == SCOPE_REF)
9910 class_type = TREE_OPERAND (declarator, 0);
9911 unqualified_name = TREE_OPERAND (declarator, 1);
9913 else
9915 class_type = current_class_type;
9916 unqualified_name = declarator;
9919 /* See if it names ctor, dtor or conv. */
9920 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
9921 || IDENTIFIER_TYPENAME_P (unqualified_name)
9922 || constructor_name_p (unqualified_name, class_type))
9923 *ctor_dtor_or_conv_p = -1;
9926 handle_declarator:;
9927 scope = get_scope_of_declarator (declarator);
9928 if (scope)
9929 /* Any names that appear after the declarator-id for a member
9930 are looked up in the containing scope. */
9931 push_scope (scope);
9932 parser->in_declarator_p = true;
9933 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
9934 || (declarator
9935 && (TREE_CODE (declarator) == SCOPE_REF
9936 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
9937 /* Default args are only allowed on function
9938 declarations. */
9939 parser->default_arg_ok_p = saved_default_arg_ok_p;
9940 else
9941 parser->default_arg_ok_p = false;
9943 first = false;
9945 /* We're done. */
9946 else
9947 break;
9950 /* For an abstract declarator, we might wind up with nothing at this
9951 point. That's an error; the declarator is not optional. */
9952 if (!declarator)
9953 cp_parser_error (parser, "expected declarator");
9955 /* If we entered a scope, we must exit it now. */
9956 if (scope)
9957 pop_scope (scope);
9959 parser->default_arg_ok_p = saved_default_arg_ok_p;
9960 parser->in_declarator_p = saved_in_declarator_p;
9962 return declarator;
9965 /* Parse a ptr-operator.
9967 ptr-operator:
9968 * cv-qualifier-seq [opt]
9970 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
9972 GNU Extension:
9974 ptr-operator:
9975 & cv-qualifier-seq [opt]
9977 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
9978 used. Returns ADDR_EXPR if a reference was used. In the
9979 case of a pointer-to-member, *TYPE is filled in with the
9980 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
9981 with the cv-qualifier-seq, or NULL_TREE, if there are no
9982 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
9984 static enum tree_code
9985 cp_parser_ptr_operator (cp_parser* parser,
9986 tree* type,
9987 tree* cv_qualifier_seq)
9989 enum tree_code code = ERROR_MARK;
9990 cp_token *token;
9992 /* Assume that it's not a pointer-to-member. */
9993 *type = NULL_TREE;
9994 /* And that there are no cv-qualifiers. */
9995 *cv_qualifier_seq = NULL_TREE;
9997 /* Peek at the next token. */
9998 token = cp_lexer_peek_token (parser->lexer);
9999 /* If it's a `*' or `&' we have a pointer or reference. */
10000 if (token->type == CPP_MULT || token->type == CPP_AND)
10002 /* Remember which ptr-operator we were processing. */
10003 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10005 /* Consume the `*' or `&'. */
10006 cp_lexer_consume_token (parser->lexer);
10008 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10009 `&', if we are allowing GNU extensions. (The only qualifier
10010 that can legally appear after `&' is `restrict', but that is
10011 enforced during semantic analysis. */
10012 if (code == INDIRECT_REF
10013 || cp_parser_allow_gnu_extensions_p (parser))
10014 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10016 else
10018 /* Try the pointer-to-member case. */
10019 cp_parser_parse_tentatively (parser);
10020 /* Look for the optional `::' operator. */
10021 cp_parser_global_scope_opt (parser,
10022 /*current_scope_valid_p=*/false);
10023 /* Look for the nested-name specifier. */
10024 cp_parser_nested_name_specifier (parser,
10025 /*typename_keyword_p=*/false,
10026 /*check_dependency_p=*/true,
10027 /*type_p=*/false);
10028 /* If we found it, and the next token is a `*', then we are
10029 indeed looking at a pointer-to-member operator. */
10030 if (!cp_parser_error_occurred (parser)
10031 && cp_parser_require (parser, CPP_MULT, "`*'"))
10033 /* The type of which the member is a member is given by the
10034 current SCOPE. */
10035 *type = parser->scope;
10036 /* The next name will not be qualified. */
10037 parser->scope = NULL_TREE;
10038 parser->qualifying_scope = NULL_TREE;
10039 parser->object_scope = NULL_TREE;
10040 /* Indicate that the `*' operator was used. */
10041 code = INDIRECT_REF;
10042 /* Look for the optional cv-qualifier-seq. */
10043 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10045 /* If that didn't work we don't have a ptr-operator. */
10046 if (!cp_parser_parse_definitely (parser))
10047 cp_parser_error (parser, "expected ptr-operator");
10050 return code;
10053 /* Parse an (optional) cv-qualifier-seq.
10055 cv-qualifier-seq:
10056 cv-qualifier cv-qualifier-seq [opt]
10058 Returns a TREE_LIST. The TREE_VALUE of each node is the
10059 representation of a cv-qualifier. */
10061 static tree
10062 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10064 tree cv_qualifiers = NULL_TREE;
10066 while (true)
10068 tree cv_qualifier;
10070 /* Look for the next cv-qualifier. */
10071 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10072 /* If we didn't find one, we're done. */
10073 if (!cv_qualifier)
10074 break;
10076 /* Add this cv-qualifier to the list. */
10077 cv_qualifiers
10078 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10081 /* We built up the list in reverse order. */
10082 return nreverse (cv_qualifiers);
10085 /* Parse an (optional) cv-qualifier.
10087 cv-qualifier:
10088 const
10089 volatile
10091 GNU Extension:
10093 cv-qualifier:
10094 __restrict__ */
10096 static tree
10097 cp_parser_cv_qualifier_opt (cp_parser* parser)
10099 cp_token *token;
10100 tree cv_qualifier = NULL_TREE;
10102 /* Peek at the next token. */
10103 token = cp_lexer_peek_token (parser->lexer);
10104 /* See if it's a cv-qualifier. */
10105 switch (token->keyword)
10107 case RID_CONST:
10108 case RID_VOLATILE:
10109 case RID_RESTRICT:
10110 /* Save the value of the token. */
10111 cv_qualifier = token->value;
10112 /* Consume the token. */
10113 cp_lexer_consume_token (parser->lexer);
10114 break;
10116 default:
10117 break;
10120 return cv_qualifier;
10123 /* Parse a declarator-id.
10125 declarator-id:
10126 id-expression
10127 :: [opt] nested-name-specifier [opt] type-name
10129 In the `id-expression' case, the value returned is as for
10130 cp_parser_id_expression if the id-expression was an unqualified-id.
10131 If the id-expression was a qualified-id, then a SCOPE_REF is
10132 returned. The first operand is the scope (either a NAMESPACE_DECL
10133 or TREE_TYPE), but the second is still just a representation of an
10134 unqualified-id. */
10136 static tree
10137 cp_parser_declarator_id (cp_parser* parser)
10139 tree id_expression;
10141 /* The expression must be an id-expression. Assume that qualified
10142 names are the names of types so that:
10144 template <class T>
10145 int S<T>::R::i = 3;
10147 will work; we must treat `S<T>::R' as the name of a type.
10148 Similarly, assume that qualified names are templates, where
10149 required, so that:
10151 template <class T>
10152 int S<T>::R<T>::i = 3;
10154 will work, too. */
10155 id_expression = cp_parser_id_expression (parser,
10156 /*template_keyword_p=*/false,
10157 /*check_dependency_p=*/false,
10158 /*template_p=*/NULL,
10159 /*declarator_p=*/true);
10160 /* If the name was qualified, create a SCOPE_REF to represent
10161 that. */
10162 if (parser->scope)
10164 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10165 parser->scope = NULL_TREE;
10168 return id_expression;
10171 /* Parse a type-id.
10173 type-id:
10174 type-specifier-seq abstract-declarator [opt]
10176 Returns the TYPE specified. */
10178 static tree
10179 cp_parser_type_id (cp_parser* parser)
10181 tree type_specifier_seq;
10182 tree abstract_declarator;
10184 /* Parse the type-specifier-seq. */
10185 type_specifier_seq
10186 = cp_parser_type_specifier_seq (parser);
10187 if (type_specifier_seq == error_mark_node)
10188 return error_mark_node;
10190 /* There might or might not be an abstract declarator. */
10191 cp_parser_parse_tentatively (parser);
10192 /* Look for the declarator. */
10193 abstract_declarator
10194 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10195 /* Check to see if there really was a declarator. */
10196 if (!cp_parser_parse_definitely (parser))
10197 abstract_declarator = NULL_TREE;
10199 return groktypename (build_tree_list (type_specifier_seq,
10200 abstract_declarator));
10203 /* Parse a type-specifier-seq.
10205 type-specifier-seq:
10206 type-specifier type-specifier-seq [opt]
10208 GNU extension:
10210 type-specifier-seq:
10211 attributes type-specifier-seq [opt]
10213 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10214 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10216 static tree
10217 cp_parser_type_specifier_seq (cp_parser* parser)
10219 bool seen_type_specifier = false;
10220 tree type_specifier_seq = NULL_TREE;
10222 /* Parse the type-specifiers and attributes. */
10223 while (true)
10225 tree type_specifier;
10227 /* Check for attributes first. */
10228 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10230 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10231 NULL_TREE,
10232 type_specifier_seq);
10233 continue;
10236 /* After the first type-specifier, others are optional. */
10237 if (seen_type_specifier)
10238 cp_parser_parse_tentatively (parser);
10239 /* Look for the type-specifier. */
10240 type_specifier = cp_parser_type_specifier (parser,
10241 CP_PARSER_FLAGS_NONE,
10242 /*is_friend=*/false,
10243 /*is_declaration=*/false,
10244 NULL,
10245 NULL);
10246 /* If the first type-specifier could not be found, this is not a
10247 type-specifier-seq at all. */
10248 if (!seen_type_specifier && type_specifier == error_mark_node)
10249 return error_mark_node;
10250 /* If subsequent type-specifiers could not be found, the
10251 type-specifier-seq is complete. */
10252 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10253 break;
10255 /* Add the new type-specifier to the list. */
10256 type_specifier_seq
10257 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10258 seen_type_specifier = true;
10261 /* We built up the list in reverse order. */
10262 return nreverse (type_specifier_seq);
10265 /* Parse a parameter-declaration-clause.
10267 parameter-declaration-clause:
10268 parameter-declaration-list [opt] ... [opt]
10269 parameter-declaration-list , ...
10271 Returns a representation for the parameter declarations. Each node
10272 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10273 representation.) If the parameter-declaration-clause ends with an
10274 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10275 list. A return value of NULL_TREE indicates a
10276 parameter-declaration-clause consisting only of an ellipsis. */
10278 static tree
10279 cp_parser_parameter_declaration_clause (cp_parser* parser)
10281 tree parameters;
10282 cp_token *token;
10283 bool ellipsis_p;
10285 /* Peek at the next token. */
10286 token = cp_lexer_peek_token (parser->lexer);
10287 /* Check for trivial parameter-declaration-clauses. */
10288 if (token->type == CPP_ELLIPSIS)
10290 /* Consume the `...' token. */
10291 cp_lexer_consume_token (parser->lexer);
10292 return NULL_TREE;
10294 else if (token->type == CPP_CLOSE_PAREN)
10295 /* There are no parameters. */
10297 #ifndef NO_IMPLICIT_EXTERN_C
10298 if (in_system_header && current_class_type == NULL
10299 && current_lang_name == lang_name_c)
10300 return NULL_TREE;
10301 else
10302 #endif
10303 return void_list_node;
10305 /* Check for `(void)', too, which is a special case. */
10306 else if (token->keyword == RID_VOID
10307 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10308 == CPP_CLOSE_PAREN))
10310 /* Consume the `void' token. */
10311 cp_lexer_consume_token (parser->lexer);
10312 /* There are no parameters. */
10313 return void_list_node;
10316 /* Parse the parameter-declaration-list. */
10317 parameters = cp_parser_parameter_declaration_list (parser);
10318 /* If a parse error occurred while parsing the
10319 parameter-declaration-list, then the entire
10320 parameter-declaration-clause is erroneous. */
10321 if (parameters == error_mark_node)
10322 return error_mark_node;
10324 /* Peek at the next token. */
10325 token = cp_lexer_peek_token (parser->lexer);
10326 /* If it's a `,', the clause should terminate with an ellipsis. */
10327 if (token->type == CPP_COMMA)
10329 /* Consume the `,'. */
10330 cp_lexer_consume_token (parser->lexer);
10331 /* Expect an ellipsis. */
10332 ellipsis_p
10333 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10335 /* It might also be `...' if the optional trailing `,' was
10336 omitted. */
10337 else if (token->type == CPP_ELLIPSIS)
10339 /* Consume the `...' token. */
10340 cp_lexer_consume_token (parser->lexer);
10341 /* And remember that we saw it. */
10342 ellipsis_p = true;
10344 else
10345 ellipsis_p = false;
10347 /* Finish the parameter list. */
10348 return finish_parmlist (parameters, ellipsis_p);
10351 /* Parse a parameter-declaration-list.
10353 parameter-declaration-list:
10354 parameter-declaration
10355 parameter-declaration-list , parameter-declaration
10357 Returns a representation of the parameter-declaration-list, as for
10358 cp_parser_parameter_declaration_clause. However, the
10359 `void_list_node' is never appended to the list. */
10361 static tree
10362 cp_parser_parameter_declaration_list (cp_parser* parser)
10364 tree parameters = NULL_TREE;
10366 /* Look for more parameters. */
10367 while (true)
10369 tree parameter;
10370 /* Parse the parameter. */
10371 parameter
10372 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10374 /* If a parse error occurred parsing the parameter declaration,
10375 then the entire parameter-declaration-list is erroneous. */
10376 if (parameter == error_mark_node)
10378 parameters = error_mark_node;
10379 break;
10381 /* Add the new parameter to the list. */
10382 TREE_CHAIN (parameter) = parameters;
10383 parameters = parameter;
10385 /* Peek at the next token. */
10386 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10387 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10388 /* The parameter-declaration-list is complete. */
10389 break;
10390 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10392 cp_token *token;
10394 /* Peek at the next token. */
10395 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10396 /* If it's an ellipsis, then the list is complete. */
10397 if (token->type == CPP_ELLIPSIS)
10398 break;
10399 /* Otherwise, there must be more parameters. Consume the
10400 `,'. */
10401 cp_lexer_consume_token (parser->lexer);
10403 else
10405 cp_parser_error (parser, "expected `,' or `...'");
10406 break;
10410 /* We built up the list in reverse order; straighten it out now. */
10411 return nreverse (parameters);
10414 /* Parse a parameter declaration.
10416 parameter-declaration:
10417 decl-specifier-seq declarator
10418 decl-specifier-seq declarator = assignment-expression
10419 decl-specifier-seq abstract-declarator [opt]
10420 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10422 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10423 declares a template parameter. (In that case, a non-nested `>'
10424 token encountered during the parsing of the assignment-expression
10425 is not interpreted as a greater-than operator.)
10427 Returns a TREE_LIST representing the parameter-declaration. The
10428 TREE_VALUE is a representation of the decl-specifier-seq and
10429 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10430 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10431 TREE_VALUE represents the declarator. */
10433 static tree
10434 cp_parser_parameter_declaration (cp_parser *parser,
10435 bool template_parm_p)
10437 int declares_class_or_enum;
10438 bool greater_than_is_operator_p;
10439 tree decl_specifiers;
10440 tree attributes;
10441 tree declarator;
10442 tree default_argument;
10443 tree parameter;
10444 cp_token *token;
10445 const char *saved_message;
10447 /* In a template parameter, `>' is not an operator.
10449 [temp.param]
10451 When parsing a default template-argument for a non-type
10452 template-parameter, the first non-nested `>' is taken as the end
10453 of the template parameter-list rather than a greater-than
10454 operator. */
10455 greater_than_is_operator_p = !template_parm_p;
10457 /* Type definitions may not appear in parameter types. */
10458 saved_message = parser->type_definition_forbidden_message;
10459 parser->type_definition_forbidden_message
10460 = "types may not be defined in parameter types";
10462 /* Parse the declaration-specifiers. */
10463 decl_specifiers
10464 = cp_parser_decl_specifier_seq (parser,
10465 CP_PARSER_FLAGS_NONE,
10466 &attributes,
10467 &declares_class_or_enum);
10468 /* If an error occurred, there's no reason to attempt to parse the
10469 rest of the declaration. */
10470 if (cp_parser_error_occurred (parser))
10472 parser->type_definition_forbidden_message = saved_message;
10473 return error_mark_node;
10476 /* Peek at the next token. */
10477 token = cp_lexer_peek_token (parser->lexer);
10478 /* If the next token is a `)', `,', `=', `>', or `...', then there
10479 is no declarator. */
10480 if (token->type == CPP_CLOSE_PAREN
10481 || token->type == CPP_COMMA
10482 || token->type == CPP_EQ
10483 || token->type == CPP_ELLIPSIS
10484 || token->type == CPP_GREATER)
10485 declarator = NULL_TREE;
10486 /* Otherwise, there should be a declarator. */
10487 else
10489 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10490 parser->default_arg_ok_p = false;
10492 declarator = cp_parser_declarator (parser,
10493 CP_PARSER_DECLARATOR_EITHER,
10494 /*ctor_dtor_or_conv_p=*/NULL);
10495 parser->default_arg_ok_p = saved_default_arg_ok_p;
10496 /* After the declarator, allow more attributes. */
10497 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10500 /* The restriction on defining new types applies only to the type
10501 of the parameter, not to the default argument. */
10502 parser->type_definition_forbidden_message = saved_message;
10504 /* If the next token is `=', then process a default argument. */
10505 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10507 bool saved_greater_than_is_operator_p;
10508 /* Consume the `='. */
10509 cp_lexer_consume_token (parser->lexer);
10511 /* If we are defining a class, then the tokens that make up the
10512 default argument must be saved and processed later. */
10513 if (!template_parm_p && at_class_scope_p ()
10514 && TYPE_BEING_DEFINED (current_class_type))
10516 unsigned depth = 0;
10518 /* Create a DEFAULT_ARG to represented the unparsed default
10519 argument. */
10520 default_argument = make_node (DEFAULT_ARG);
10521 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10523 /* Add tokens until we have processed the entire default
10524 argument. */
10525 while (true)
10527 bool done = false;
10528 cp_token *token;
10530 /* Peek at the next token. */
10531 token = cp_lexer_peek_token (parser->lexer);
10532 /* What we do depends on what token we have. */
10533 switch (token->type)
10535 /* In valid code, a default argument must be
10536 immediately followed by a `,' `)', or `...'. */
10537 case CPP_COMMA:
10538 case CPP_CLOSE_PAREN:
10539 case CPP_ELLIPSIS:
10540 /* If we run into a non-nested `;', `}', or `]',
10541 then the code is invalid -- but the default
10542 argument is certainly over. */
10543 case CPP_SEMICOLON:
10544 case CPP_CLOSE_BRACE:
10545 case CPP_CLOSE_SQUARE:
10546 if (depth == 0)
10547 done = true;
10548 /* Update DEPTH, if necessary. */
10549 else if (token->type == CPP_CLOSE_PAREN
10550 || token->type == CPP_CLOSE_BRACE
10551 || token->type == CPP_CLOSE_SQUARE)
10552 --depth;
10553 break;
10555 case CPP_OPEN_PAREN:
10556 case CPP_OPEN_SQUARE:
10557 case CPP_OPEN_BRACE:
10558 ++depth;
10559 break;
10561 case CPP_GREATER:
10562 /* If we see a non-nested `>', and `>' is not an
10563 operator, then it marks the end of the default
10564 argument. */
10565 if (!depth && !greater_than_is_operator_p)
10566 done = true;
10567 break;
10569 /* If we run out of tokens, issue an error message. */
10570 case CPP_EOF:
10571 error ("file ends in default argument");
10572 done = true;
10573 break;
10575 case CPP_NAME:
10576 case CPP_SCOPE:
10577 /* In these cases, we should look for template-ids.
10578 For example, if the default argument is
10579 `X<int, double>()', we need to do name lookup to
10580 figure out whether or not `X' is a template; if
10581 so, the `,' does not end the default argument.
10583 That is not yet done. */
10584 break;
10586 default:
10587 break;
10590 /* If we've reached the end, stop. */
10591 if (done)
10592 break;
10594 /* Add the token to the token block. */
10595 token = cp_lexer_consume_token (parser->lexer);
10596 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10597 token);
10600 /* Outside of a class definition, we can just parse the
10601 assignment-expression. */
10602 else
10604 bool saved_local_variables_forbidden_p;
10606 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10607 set correctly. */
10608 saved_greater_than_is_operator_p
10609 = parser->greater_than_is_operator_p;
10610 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10611 /* Local variable names (and the `this' keyword) may not
10612 appear in a default argument. */
10613 saved_local_variables_forbidden_p
10614 = parser->local_variables_forbidden_p;
10615 parser->local_variables_forbidden_p = true;
10616 /* Parse the assignment-expression. */
10617 default_argument = cp_parser_assignment_expression (parser);
10618 /* Restore saved state. */
10619 parser->greater_than_is_operator_p
10620 = saved_greater_than_is_operator_p;
10621 parser->local_variables_forbidden_p
10622 = saved_local_variables_forbidden_p;
10624 if (!parser->default_arg_ok_p)
10626 if (!flag_pedantic_errors)
10627 warning ("deprecated use of default argument for parameter of non-function");
10628 else
10630 error ("default arguments are only permitted for function parameters");
10631 default_argument = NULL_TREE;
10635 else
10636 default_argument = NULL_TREE;
10638 /* Create the representation of the parameter. */
10639 if (attributes)
10640 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10641 parameter = build_tree_list (default_argument,
10642 build_tree_list (decl_specifiers,
10643 declarator));
10645 return parameter;
10648 /* Parse a function-definition.
10650 function-definition:
10651 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10652 function-body
10653 decl-specifier-seq [opt] declarator function-try-block
10655 GNU Extension:
10657 function-definition:
10658 __extension__ function-definition
10660 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10661 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10662 be a `friend'. */
10664 static tree
10665 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10667 tree decl_specifiers;
10668 tree attributes;
10669 tree declarator;
10670 tree fn;
10671 cp_token *token;
10672 int declares_class_or_enum;
10673 bool member_p;
10674 /* The saved value of the PEDANTIC flag. */
10675 int saved_pedantic;
10677 /* Any pending qualification must be cleared by our caller. It is
10678 more robust to force the callers to clear PARSER->SCOPE than to
10679 do it here since if the qualification is in effect here, it might
10680 also end up in effect elsewhere that it is not intended. */
10681 my_friendly_assert (!parser->scope, 20010821);
10683 /* Handle `__extension__'. */
10684 if (cp_parser_extension_opt (parser, &saved_pedantic))
10686 /* Parse the function-definition. */
10687 fn = cp_parser_function_definition (parser, friend_p);
10688 /* Restore the PEDANTIC flag. */
10689 pedantic = saved_pedantic;
10691 return fn;
10694 /* Check to see if this definition appears in a class-specifier. */
10695 member_p = (at_class_scope_p ()
10696 && TYPE_BEING_DEFINED (current_class_type));
10697 /* Defer access checks in the decl-specifier-seq until we know what
10698 function is being defined. There is no need to do this for the
10699 definition of member functions; we cannot be defining a member
10700 from another class. */
10701 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10703 /* Parse the decl-specifier-seq. */
10704 decl_specifiers
10705 = cp_parser_decl_specifier_seq (parser,
10706 CP_PARSER_FLAGS_OPTIONAL,
10707 &attributes,
10708 &declares_class_or_enum);
10709 /* Figure out whether this declaration is a `friend'. */
10710 if (friend_p)
10711 *friend_p = cp_parser_friend_p (decl_specifiers);
10713 /* Parse the declarator. */
10714 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10715 /*ctor_dtor_or_conv_p=*/NULL);
10717 /* Gather up any access checks that occurred. */
10718 stop_deferring_access_checks ();
10720 /* If something has already gone wrong, we may as well stop now. */
10721 if (declarator == error_mark_node)
10723 /* Skip to the end of the function, or if this wasn't anything
10724 like a function-definition, to a `;' in the hopes of finding
10725 a sensible place from which to continue parsing. */
10726 cp_parser_skip_to_end_of_block_or_statement (parser);
10727 pop_deferring_access_checks ();
10728 return error_mark_node;
10731 /* The next character should be a `{' (for a simple function
10732 definition), a `:' (for a ctor-initializer), or `try' (for a
10733 function-try block). */
10734 token = cp_lexer_peek_token (parser->lexer);
10735 if (!cp_parser_token_starts_function_definition_p (token))
10737 /* Issue the error-message. */
10738 cp_parser_error (parser, "expected function-definition");
10739 /* Skip to the next `;'. */
10740 cp_parser_skip_to_end_of_block_or_statement (parser);
10742 pop_deferring_access_checks ();
10743 return error_mark_node;
10746 cp_parser_check_for_definition_in_return_type (declarator,
10747 declares_class_or_enum);
10749 /* If we are in a class scope, then we must handle
10750 function-definitions specially. In particular, we save away the
10751 tokens that make up the function body, and parse them again
10752 later, in order to handle code like:
10754 struct S {
10755 int f () { return i; }
10756 int i;
10759 Here, we cannot parse the body of `f' until after we have seen
10760 the declaration of `i'. */
10761 if (member_p)
10763 cp_token_cache *cache;
10765 /* Create the function-declaration. */
10766 fn = start_method (decl_specifiers, declarator, attributes);
10767 /* If something went badly wrong, bail out now. */
10768 if (fn == error_mark_node)
10770 /* If there's a function-body, skip it. */
10771 if (cp_parser_token_starts_function_definition_p
10772 (cp_lexer_peek_token (parser->lexer)))
10773 cp_parser_skip_to_end_of_block_or_statement (parser);
10774 pop_deferring_access_checks ();
10775 return error_mark_node;
10778 /* Remember it, if there default args to post process. */
10779 cp_parser_save_default_args (parser, fn);
10781 /* Create a token cache. */
10782 cache = cp_token_cache_new ();
10783 /* Save away the tokens that make up the body of the
10784 function. */
10785 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10786 /* Handle function try blocks. */
10787 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
10788 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10790 /* Save away the inline definition; we will process it when the
10791 class is complete. */
10792 DECL_PENDING_INLINE_INFO (fn) = cache;
10793 DECL_PENDING_INLINE_P (fn) = 1;
10795 /* We need to know that this was defined in the class, so that
10796 friend templates are handled correctly. */
10797 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
10799 /* We're done with the inline definition. */
10800 finish_method (fn);
10802 /* Add FN to the queue of functions to be parsed later. */
10803 TREE_VALUE (parser->unparsed_functions_queues)
10804 = tree_cons (NULL_TREE, fn,
10805 TREE_VALUE (parser->unparsed_functions_queues));
10807 pop_deferring_access_checks ();
10808 return fn;
10811 /* Check that the number of template-parameter-lists is OK. */
10812 if (!cp_parser_check_declarator_template_parameters (parser,
10813 declarator))
10815 cp_parser_skip_to_end_of_block_or_statement (parser);
10816 pop_deferring_access_checks ();
10817 return error_mark_node;
10820 fn = cp_parser_function_definition_from_specifiers_and_declarator
10821 (parser, decl_specifiers, attributes, declarator);
10822 pop_deferring_access_checks ();
10823 return fn;
10826 /* Parse a function-body.
10828 function-body:
10829 compound_statement */
10831 static void
10832 cp_parser_function_body (cp_parser *parser)
10834 cp_parser_compound_statement (parser, false);
10837 /* Parse a ctor-initializer-opt followed by a function-body. Return
10838 true if a ctor-initializer was present. */
10840 static bool
10841 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
10843 tree body;
10844 bool ctor_initializer_p;
10846 /* Begin the function body. */
10847 body = begin_function_body ();
10848 /* Parse the optional ctor-initializer. */
10849 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
10850 /* Parse the function-body. */
10851 cp_parser_function_body (parser);
10852 /* Finish the function body. */
10853 finish_function_body (body);
10855 return ctor_initializer_p;
10858 /* Parse an initializer.
10860 initializer:
10861 = initializer-clause
10862 ( expression-list )
10864 Returns a expression representing the initializer. If no
10865 initializer is present, NULL_TREE is returned.
10867 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10868 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
10869 set to FALSE if there is no initializer present. If there is an
10870 initializer, and it is not a constant-expression, *NON_CONSTANT_P
10871 is set to true; otherwise it is set to false. */
10873 static tree
10874 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
10875 bool* non_constant_p)
10877 cp_token *token;
10878 tree init;
10880 /* Peek at the next token. */
10881 token = cp_lexer_peek_token (parser->lexer);
10883 /* Let our caller know whether or not this initializer was
10884 parenthesized. */
10885 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
10886 /* Assume that the initializer is constant. */
10887 *non_constant_p = false;
10889 if (token->type == CPP_EQ)
10891 /* Consume the `='. */
10892 cp_lexer_consume_token (parser->lexer);
10893 /* Parse the initializer-clause. */
10894 init = cp_parser_initializer_clause (parser, non_constant_p);
10896 else if (token->type == CPP_OPEN_PAREN)
10897 init = cp_parser_parenthesized_expression_list (parser, false,
10898 non_constant_p);
10899 else
10901 /* Anything else is an error. */
10902 cp_parser_error (parser, "expected initializer");
10903 init = error_mark_node;
10906 return init;
10909 /* Parse an initializer-clause.
10911 initializer-clause:
10912 assignment-expression
10913 { initializer-list , [opt] }
10916 Returns an expression representing the initializer.
10918 If the `assignment-expression' production is used the value
10919 returned is simply a representation for the expression.
10921 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
10922 the elements of the initializer-list (or NULL_TREE, if the last
10923 production is used). The TREE_TYPE for the CONSTRUCTOR will be
10924 NULL_TREE. There is no way to detect whether or not the optional
10925 trailing `,' was provided. NON_CONSTANT_P is as for
10926 cp_parser_initializer. */
10928 static tree
10929 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
10931 tree initializer;
10933 /* If it is not a `{', then we are looking at an
10934 assignment-expression. */
10935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10936 initializer
10937 = cp_parser_constant_expression (parser,
10938 /*allow_non_constant_p=*/true,
10939 non_constant_p);
10940 else
10942 /* Consume the `{' token. */
10943 cp_lexer_consume_token (parser->lexer);
10944 /* Create a CONSTRUCTOR to represent the braced-initializer. */
10945 initializer = make_node (CONSTRUCTOR);
10946 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
10947 necessary, but check_initializer depends upon it, for
10948 now. */
10949 TREE_HAS_CONSTRUCTOR (initializer) = 1;
10950 /* If it's not a `}', then there is a non-trivial initializer. */
10951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10953 /* Parse the initializer list. */
10954 CONSTRUCTOR_ELTS (initializer)
10955 = cp_parser_initializer_list (parser, non_constant_p);
10956 /* A trailing `,' token is allowed. */
10957 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10958 cp_lexer_consume_token (parser->lexer);
10960 /* Now, there should be a trailing `}'. */
10961 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10964 return initializer;
10967 /* Parse an initializer-list.
10969 initializer-list:
10970 initializer-clause
10971 initializer-list , initializer-clause
10973 GNU Extension:
10975 initializer-list:
10976 identifier : initializer-clause
10977 initializer-list, identifier : initializer-clause
10979 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
10980 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
10981 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
10982 as for cp_parser_initializer. */
10984 static tree
10985 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
10987 tree initializers = NULL_TREE;
10989 /* Assume all of the expressions are constant. */
10990 *non_constant_p = false;
10992 /* Parse the rest of the list. */
10993 while (true)
10995 cp_token *token;
10996 tree identifier;
10997 tree initializer;
10998 bool clause_non_constant_p;
11000 /* If the next token is an identifier and the following one is a
11001 colon, we are looking at the GNU designated-initializer
11002 syntax. */
11003 if (cp_parser_allow_gnu_extensions_p (parser)
11004 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11005 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11007 /* Consume the identifier. */
11008 identifier = cp_lexer_consume_token (parser->lexer)->value;
11009 /* Consume the `:'. */
11010 cp_lexer_consume_token (parser->lexer);
11012 else
11013 identifier = NULL_TREE;
11015 /* Parse the initializer. */
11016 initializer = cp_parser_initializer_clause (parser,
11017 &clause_non_constant_p);
11018 /* If any clause is non-constant, so is the entire initializer. */
11019 if (clause_non_constant_p)
11020 *non_constant_p = true;
11021 /* Add it to the list. */
11022 initializers = tree_cons (identifier, initializer, initializers);
11024 /* If the next token is not a comma, we have reached the end of
11025 the list. */
11026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11027 break;
11029 /* Peek at the next token. */
11030 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11031 /* If the next token is a `}', then we're still done. An
11032 initializer-clause can have a trailing `,' after the
11033 initializer-list and before the closing `}'. */
11034 if (token->type == CPP_CLOSE_BRACE)
11035 break;
11037 /* Consume the `,' token. */
11038 cp_lexer_consume_token (parser->lexer);
11041 /* The initializers were built up in reverse order, so we need to
11042 reverse them now. */
11043 return nreverse (initializers);
11046 /* Classes [gram.class] */
11048 /* Parse a class-name.
11050 class-name:
11051 identifier
11052 template-id
11054 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11055 to indicate that names looked up in dependent types should be
11056 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11057 keyword has been used to indicate that the name that appears next
11058 is a template. TYPE_P is true iff the next name should be treated
11059 as class-name, even if it is declared to be some other kind of name
11060 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11061 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11062 being defined in a class-head.
11064 Returns the TYPE_DECL representing the class. */
11066 static tree
11067 cp_parser_class_name (cp_parser *parser,
11068 bool typename_keyword_p,
11069 bool template_keyword_p,
11070 bool type_p,
11071 bool check_dependency_p,
11072 bool class_head_p)
11074 tree decl;
11075 tree scope;
11076 bool typename_p;
11077 cp_token *token;
11079 /* All class-names start with an identifier. */
11080 token = cp_lexer_peek_token (parser->lexer);
11081 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11083 cp_parser_error (parser, "expected class-name");
11084 return error_mark_node;
11087 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11088 to a template-id, so we save it here. */
11089 scope = parser->scope;
11090 if (scope == error_mark_node)
11091 return error_mark_node;
11093 /* Any name names a type if we're following the `typename' keyword
11094 in a qualified name where the enclosing scope is type-dependent. */
11095 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11096 && dependent_type_p (scope));
11097 /* Handle the common case (an identifier, but not a template-id)
11098 efficiently. */
11099 if (token->type == CPP_NAME
11100 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11102 tree identifier;
11104 /* Look for the identifier. */
11105 identifier = cp_parser_identifier (parser);
11106 /* If the next token isn't an identifier, we are certainly not
11107 looking at a class-name. */
11108 if (identifier == error_mark_node)
11109 decl = error_mark_node;
11110 /* If we know this is a type-name, there's no need to look it
11111 up. */
11112 else if (typename_p)
11113 decl = identifier;
11114 else
11116 /* If the next token is a `::', then the name must be a type
11117 name.
11119 [basic.lookup.qual]
11121 During the lookup for a name preceding the :: scope
11122 resolution operator, object, function, and enumerator
11123 names are ignored. */
11124 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11125 type_p = true;
11126 /* Look up the name. */
11127 decl = cp_parser_lookup_name (parser, identifier,
11128 type_p,
11129 /*is_namespace=*/false,
11130 check_dependency_p);
11133 else
11135 /* Try a template-id. */
11136 decl = cp_parser_template_id (parser, template_keyword_p,
11137 check_dependency_p);
11138 if (decl == error_mark_node)
11139 return error_mark_node;
11142 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11144 /* If this is a typename, create a TYPENAME_TYPE. */
11145 if (typename_p && decl != error_mark_node)
11146 decl = TYPE_NAME (make_typename_type (scope, decl,
11147 /*complain=*/1));
11149 /* Check to see that it is really the name of a class. */
11150 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11151 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11152 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11153 /* Situations like this:
11155 template <typename T> struct A {
11156 typename T::template X<int>::I i;
11159 are problematic. Is `T::template X<int>' a class-name? The
11160 standard does not seem to be definitive, but there is no other
11161 valid interpretation of the following `::'. Therefore, those
11162 names are considered class-names. */
11163 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11164 else if (decl == error_mark_node
11165 || TREE_CODE (decl) != TYPE_DECL
11166 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11168 cp_parser_error (parser, "expected class-name");
11169 return error_mark_node;
11172 return decl;
11175 /* Parse a class-specifier.
11177 class-specifier:
11178 class-head { member-specification [opt] }
11180 Returns the TREE_TYPE representing the class. */
11182 static tree
11183 cp_parser_class_specifier (cp_parser* parser)
11185 cp_token *token;
11186 tree type;
11187 tree attributes = NULL_TREE;
11188 int has_trailing_semicolon;
11189 bool nested_name_specifier_p;
11190 unsigned saved_num_template_parameter_lists;
11192 push_deferring_access_checks (dk_no_deferred);
11194 /* Parse the class-head. */
11195 type = cp_parser_class_head (parser,
11196 &nested_name_specifier_p);
11197 /* If the class-head was a semantic disaster, skip the entire body
11198 of the class. */
11199 if (!type)
11201 cp_parser_skip_to_end_of_block_or_statement (parser);
11202 pop_deferring_access_checks ();
11203 return error_mark_node;
11206 /* Look for the `{'. */
11207 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11209 pop_deferring_access_checks ();
11210 return error_mark_node;
11213 /* Issue an error message if type-definitions are forbidden here. */
11214 cp_parser_check_type_definition (parser);
11215 /* Remember that we are defining one more class. */
11216 ++parser->num_classes_being_defined;
11217 /* Inside the class, surrounding template-parameter-lists do not
11218 apply. */
11219 saved_num_template_parameter_lists
11220 = parser->num_template_parameter_lists;
11221 parser->num_template_parameter_lists = 0;
11223 /* Start the class. */
11224 type = begin_class_definition (type);
11225 if (type == error_mark_node)
11226 /* If the type is erroneous, skip the entire body of the class. */
11227 cp_parser_skip_to_closing_brace (parser);
11228 else
11229 /* Parse the member-specification. */
11230 cp_parser_member_specification_opt (parser);
11231 /* Look for the trailing `}'. */
11232 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11233 /* We get better error messages by noticing a common problem: a
11234 missing trailing `;'. */
11235 token = cp_lexer_peek_token (parser->lexer);
11236 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11237 /* Look for attributes to apply to this class. */
11238 if (cp_parser_allow_gnu_extensions_p (parser))
11239 attributes = cp_parser_attributes_opt (parser);
11240 /* If we got any attributes in class_head, xref_tag will stick them in
11241 TREE_TYPE of the type. Grab them now. */
11242 if (type != error_mark_node)
11244 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11245 TYPE_ATTRIBUTES (type) = NULL_TREE;
11246 type = finish_struct (type, attributes);
11248 if (nested_name_specifier_p)
11249 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11250 /* If this class is not itself within the scope of another class,
11251 then we need to parse the bodies of all of the queued function
11252 definitions. Note that the queued functions defined in a class
11253 are not always processed immediately following the
11254 class-specifier for that class. Consider:
11256 struct A {
11257 struct B { void f() { sizeof (A); } };
11260 If `f' were processed before the processing of `A' were
11261 completed, there would be no way to compute the size of `A'.
11262 Note that the nesting we are interested in here is lexical --
11263 not the semantic nesting given by TYPE_CONTEXT. In particular,
11264 for:
11266 struct A { struct B; };
11267 struct A::B { void f() { } };
11269 there is no need to delay the parsing of `A::B::f'. */
11270 if (--parser->num_classes_being_defined == 0)
11272 tree queue_entry;
11273 tree fn;
11275 /* In a first pass, parse default arguments to the functions.
11276 Then, in a second pass, parse the bodies of the functions.
11277 This two-phased approach handles cases like:
11279 struct S {
11280 void f() { g(); }
11281 void g(int i = 3);
11285 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11286 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11287 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11288 TREE_PURPOSE (parser->unparsed_functions_queues)
11289 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11291 fn = TREE_VALUE (queue_entry);
11292 /* Make sure that any template parameters are in scope. */
11293 maybe_begin_member_template_processing (fn);
11294 /* If there are default arguments that have not yet been processed,
11295 take care of them now. */
11296 cp_parser_late_parsing_default_args (parser, fn);
11297 /* Remove any template parameters from the symbol table. */
11298 maybe_end_member_template_processing ();
11300 /* Now parse the body of the functions. */
11301 for (TREE_VALUE (parser->unparsed_functions_queues)
11302 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11303 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11304 TREE_VALUE (parser->unparsed_functions_queues)
11305 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11307 /* Figure out which function we need to process. */
11308 fn = TREE_VALUE (queue_entry);
11310 /* Parse the function. */
11311 cp_parser_late_parsing_for_member (parser, fn);
11316 /* Put back any saved access checks. */
11317 pop_deferring_access_checks ();
11319 /* Restore the count of active template-parameter-lists. */
11320 parser->num_template_parameter_lists
11321 = saved_num_template_parameter_lists;
11323 return type;
11326 /* Parse a class-head.
11328 class-head:
11329 class-key identifier [opt] base-clause [opt]
11330 class-key nested-name-specifier identifier base-clause [opt]
11331 class-key nested-name-specifier [opt] template-id
11332 base-clause [opt]
11334 GNU Extensions:
11335 class-key attributes identifier [opt] base-clause [opt]
11336 class-key attributes nested-name-specifier identifier base-clause [opt]
11337 class-key attributes nested-name-specifier [opt] template-id
11338 base-clause [opt]
11340 Returns the TYPE of the indicated class. Sets
11341 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11342 involving a nested-name-specifier was used, and FALSE otherwise.
11344 Returns NULL_TREE if the class-head is syntactically valid, but
11345 semantically invalid in a way that means we should skip the entire
11346 body of the class. */
11348 static tree
11349 cp_parser_class_head (cp_parser* parser,
11350 bool* nested_name_specifier_p)
11352 cp_token *token;
11353 tree nested_name_specifier;
11354 enum tag_types class_key;
11355 tree id = NULL_TREE;
11356 tree type = NULL_TREE;
11357 tree attributes;
11358 bool template_id_p = false;
11359 bool qualified_p = false;
11360 bool invalid_nested_name_p = false;
11361 unsigned num_templates;
11363 /* Assume no nested-name-specifier will be present. */
11364 *nested_name_specifier_p = false;
11365 /* Assume no template parameter lists will be used in defining the
11366 type. */
11367 num_templates = 0;
11369 /* Look for the class-key. */
11370 class_key = cp_parser_class_key (parser);
11371 if (class_key == none_type)
11372 return error_mark_node;
11374 /* Parse the attributes. */
11375 attributes = cp_parser_attributes_opt (parser);
11377 /* If the next token is `::', that is invalid -- but sometimes
11378 people do try to write:
11380 struct ::S {};
11382 Handle this gracefully by accepting the extra qualifier, and then
11383 issuing an error about it later if this really is a
11384 class-head. If it turns out just to be an elaborated type
11385 specifier, remain silent. */
11386 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11387 qualified_p = true;
11389 push_deferring_access_checks (dk_no_check);
11391 /* Determine the name of the class. Begin by looking for an
11392 optional nested-name-specifier. */
11393 nested_name_specifier
11394 = cp_parser_nested_name_specifier_opt (parser,
11395 /*typename_keyword_p=*/false,
11396 /*check_dependency_p=*/false,
11397 /*type_p=*/false);
11398 /* If there was a nested-name-specifier, then there *must* be an
11399 identifier. */
11400 if (nested_name_specifier)
11402 /* Although the grammar says `identifier', it really means
11403 `class-name' or `template-name'. You are only allowed to
11404 define a class that has already been declared with this
11405 syntax.
11407 The proposed resolution for Core Issue 180 says that whever
11408 you see `class T::X' you should treat `X' as a type-name.
11410 It is OK to define an inaccessible class; for example:
11412 class A { class B; };
11413 class A::B {};
11415 We do not know if we will see a class-name, or a
11416 template-name. We look for a class-name first, in case the
11417 class-name is a template-id; if we looked for the
11418 template-name first we would stop after the template-name. */
11419 cp_parser_parse_tentatively (parser);
11420 type = cp_parser_class_name (parser,
11421 /*typename_keyword_p=*/false,
11422 /*template_keyword_p=*/false,
11423 /*type_p=*/true,
11424 /*check_dependency_p=*/false,
11425 /*class_head_p=*/true);
11426 /* If that didn't work, ignore the nested-name-specifier. */
11427 if (!cp_parser_parse_definitely (parser))
11429 invalid_nested_name_p = true;
11430 id = cp_parser_identifier (parser);
11431 if (id == error_mark_node)
11432 id = NULL_TREE;
11434 /* If we could not find a corresponding TYPE, treat this
11435 declaration like an unqualified declaration. */
11436 if (type == error_mark_node)
11437 nested_name_specifier = NULL_TREE;
11438 /* Otherwise, count the number of templates used in TYPE and its
11439 containing scopes. */
11440 else
11442 tree scope;
11444 for (scope = TREE_TYPE (type);
11445 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11446 scope = (TYPE_P (scope)
11447 ? TYPE_CONTEXT (scope)
11448 : DECL_CONTEXT (scope)))
11449 if (TYPE_P (scope)
11450 && CLASS_TYPE_P (scope)
11451 && CLASSTYPE_TEMPLATE_INFO (scope)
11452 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11453 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11454 ++num_templates;
11457 /* Otherwise, the identifier is optional. */
11458 else
11460 /* We don't know whether what comes next is a template-id,
11461 an identifier, or nothing at all. */
11462 cp_parser_parse_tentatively (parser);
11463 /* Check for a template-id. */
11464 id = cp_parser_template_id (parser,
11465 /*template_keyword_p=*/false,
11466 /*check_dependency_p=*/true);
11467 /* If that didn't work, it could still be an identifier. */
11468 if (!cp_parser_parse_definitely (parser))
11470 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11471 id = cp_parser_identifier (parser);
11472 else
11473 id = NULL_TREE;
11475 else
11477 template_id_p = true;
11478 ++num_templates;
11482 pop_deferring_access_checks ();
11484 /* If it's not a `:' or a `{' then we can't really be looking at a
11485 class-head, since a class-head only appears as part of a
11486 class-specifier. We have to detect this situation before calling
11487 xref_tag, since that has irreversible side-effects. */
11488 if (!cp_parser_next_token_starts_class_definition_p (parser))
11490 cp_parser_error (parser, "expected `{' or `:'");
11491 return error_mark_node;
11494 /* At this point, we're going ahead with the class-specifier, even
11495 if some other problem occurs. */
11496 cp_parser_commit_to_tentative_parse (parser);
11497 /* Issue the error about the overly-qualified name now. */
11498 if (qualified_p)
11499 cp_parser_error (parser,
11500 "global qualification of class name is invalid");
11501 else if (invalid_nested_name_p)
11502 cp_parser_error (parser,
11503 "qualified name does not name a class");
11504 /* Make sure that the right number of template parameters were
11505 present. */
11506 if (!cp_parser_check_template_parameters (parser, num_templates))
11507 /* If something went wrong, there is no point in even trying to
11508 process the class-definition. */
11509 return NULL_TREE;
11511 /* Look up the type. */
11512 if (template_id_p)
11514 type = TREE_TYPE (id);
11515 maybe_process_partial_specialization (type);
11517 else if (!nested_name_specifier)
11519 /* If the class was unnamed, create a dummy name. */
11520 if (!id)
11521 id = make_anon_name ();
11522 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11523 parser->num_template_parameter_lists);
11525 else
11527 tree class_type;
11528 tree scope;
11530 /* Given:
11532 template <typename T> struct S { struct T };
11533 template <typename T> struct S<T>::T { };
11535 we will get a TYPENAME_TYPE when processing the definition of
11536 `S::T'. We need to resolve it to the actual type before we
11537 try to define it. */
11538 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11540 class_type = resolve_typename_type (TREE_TYPE (type),
11541 /*only_current_p=*/false);
11542 if (class_type != error_mark_node)
11543 type = TYPE_NAME (class_type);
11544 else
11546 cp_parser_error (parser, "could not resolve typename type");
11547 type = error_mark_node;
11551 /* Figure out in what scope the declaration is being placed. */
11552 scope = current_scope ();
11553 if (!scope)
11554 scope = current_namespace;
11555 /* If that scope does not contain the scope in which the
11556 class was originally declared, the program is invalid. */
11557 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11559 error ("declaration of `%D' in `%D' which does not "
11560 "enclose `%D'", type, scope, nested_name_specifier);
11561 return NULL_TREE;
11563 /* [dcl.meaning]
11565 A declarator-id shall not be qualified exception of the
11566 definition of a ... nested class outside of its class
11567 ... [or] a the definition or explicit instantiation of a
11568 class member of a namespace outside of its namespace. */
11569 if (scope == CP_DECL_CONTEXT (type))
11571 pedwarn ("extra qualification ignored");
11572 nested_name_specifier = NULL_TREE;
11575 maybe_process_partial_specialization (TREE_TYPE (type));
11576 class_type = current_class_type;
11577 /* Enter the scope indicated by the nested-name-specifier. */
11578 if (nested_name_specifier)
11579 push_scope (nested_name_specifier);
11580 /* Get the canonical version of this type. */
11581 type = TYPE_MAIN_DECL (TREE_TYPE (type));
11582 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11583 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11584 type = push_template_decl (type);
11585 type = TREE_TYPE (type);
11586 if (nested_name_specifier)
11587 *nested_name_specifier_p = true;
11589 /* Indicate whether this class was declared as a `class' or as a
11590 `struct'. */
11591 if (TREE_CODE (type) == RECORD_TYPE)
11592 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11593 cp_parser_check_class_key (class_key, type);
11595 /* Enter the scope containing the class; the names of base classes
11596 should be looked up in that context. For example, given:
11598 struct A { struct B {}; struct C; };
11599 struct A::C : B {};
11601 is valid. */
11602 if (nested_name_specifier)
11603 push_scope (nested_name_specifier);
11604 /* Now, look for the base-clause. */
11605 token = cp_lexer_peek_token (parser->lexer);
11606 if (token->type == CPP_COLON)
11608 tree bases;
11610 /* Get the list of base-classes. */
11611 bases = cp_parser_base_clause (parser);
11612 /* Process them. */
11613 xref_basetypes (type, bases);
11615 /* Leave the scope given by the nested-name-specifier. We will
11616 enter the class scope itself while processing the members. */
11617 if (nested_name_specifier)
11618 pop_scope (nested_name_specifier);
11620 return type;
11623 /* Parse a class-key.
11625 class-key:
11626 class
11627 struct
11628 union
11630 Returns the kind of class-key specified, or none_type to indicate
11631 error. */
11633 static enum tag_types
11634 cp_parser_class_key (cp_parser* parser)
11636 cp_token *token;
11637 enum tag_types tag_type;
11639 /* Look for the class-key. */
11640 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11641 if (!token)
11642 return none_type;
11644 /* Check to see if the TOKEN is a class-key. */
11645 tag_type = cp_parser_token_is_class_key (token);
11646 if (!tag_type)
11647 cp_parser_error (parser, "expected class-key");
11648 return tag_type;
11651 /* Parse an (optional) member-specification.
11653 member-specification:
11654 member-declaration member-specification [opt]
11655 access-specifier : member-specification [opt] */
11657 static void
11658 cp_parser_member_specification_opt (cp_parser* parser)
11660 while (true)
11662 cp_token *token;
11663 enum rid keyword;
11665 /* Peek at the next token. */
11666 token = cp_lexer_peek_token (parser->lexer);
11667 /* If it's a `}', or EOF then we've seen all the members. */
11668 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11669 break;
11671 /* See if this token is a keyword. */
11672 keyword = token->keyword;
11673 switch (keyword)
11675 case RID_PUBLIC:
11676 case RID_PROTECTED:
11677 case RID_PRIVATE:
11678 /* Consume the access-specifier. */
11679 cp_lexer_consume_token (parser->lexer);
11680 /* Remember which access-specifier is active. */
11681 current_access_specifier = token->value;
11682 /* Look for the `:'. */
11683 cp_parser_require (parser, CPP_COLON, "`:'");
11684 break;
11686 default:
11687 /* Otherwise, the next construction must be a
11688 member-declaration. */
11689 cp_parser_member_declaration (parser);
11694 /* Parse a member-declaration.
11696 member-declaration:
11697 decl-specifier-seq [opt] member-declarator-list [opt] ;
11698 function-definition ; [opt]
11699 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11700 using-declaration
11701 template-declaration
11703 member-declarator-list:
11704 member-declarator
11705 member-declarator-list , member-declarator
11707 member-declarator:
11708 declarator pure-specifier [opt]
11709 declarator constant-initializer [opt]
11710 identifier [opt] : constant-expression
11712 GNU Extensions:
11714 member-declaration:
11715 __extension__ member-declaration
11717 member-declarator:
11718 declarator attributes [opt] pure-specifier [opt]
11719 declarator attributes [opt] constant-initializer [opt]
11720 identifier [opt] attributes [opt] : constant-expression */
11722 static void
11723 cp_parser_member_declaration (cp_parser* parser)
11725 tree decl_specifiers;
11726 tree prefix_attributes;
11727 tree decl;
11728 int declares_class_or_enum;
11729 bool friend_p;
11730 cp_token *token;
11731 int saved_pedantic;
11733 /* Check for the `__extension__' keyword. */
11734 if (cp_parser_extension_opt (parser, &saved_pedantic))
11736 /* Recurse. */
11737 cp_parser_member_declaration (parser);
11738 /* Restore the old value of the PEDANTIC flag. */
11739 pedantic = saved_pedantic;
11741 return;
11744 /* Check for a template-declaration. */
11745 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11747 /* Parse the template-declaration. */
11748 cp_parser_template_declaration (parser, /*member_p=*/true);
11750 return;
11753 /* Check for a using-declaration. */
11754 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11756 /* Parse the using-declaration. */
11757 cp_parser_using_declaration (parser);
11759 return;
11762 /* We can't tell whether we're looking at a declaration or a
11763 function-definition. */
11764 cp_parser_parse_tentatively (parser);
11766 /* Parse the decl-specifier-seq. */
11767 decl_specifiers
11768 = cp_parser_decl_specifier_seq (parser,
11769 CP_PARSER_FLAGS_OPTIONAL,
11770 &prefix_attributes,
11771 &declares_class_or_enum);
11772 /* Check for an invalid type-name. */
11773 if (cp_parser_diagnose_invalid_type_name (parser))
11774 return;
11775 /* If there is no declarator, then the decl-specifier-seq should
11776 specify a type. */
11777 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11779 /* If there was no decl-specifier-seq, and the next token is a
11780 `;', then we have something like:
11782 struct S { ; };
11784 [class.mem]
11786 Each member-declaration shall declare at least one member
11787 name of the class. */
11788 if (!decl_specifiers)
11790 if (pedantic)
11791 pedwarn ("extra semicolon");
11793 else
11795 tree type;
11797 /* See if this declaration is a friend. */
11798 friend_p = cp_parser_friend_p (decl_specifiers);
11799 /* If there were decl-specifiers, check to see if there was
11800 a class-declaration. */
11801 type = check_tag_decl (decl_specifiers);
11802 /* Nested classes have already been added to the class, but
11803 a `friend' needs to be explicitly registered. */
11804 if (friend_p)
11806 /* If the `friend' keyword was present, the friend must
11807 be introduced with a class-key. */
11808 if (!declares_class_or_enum)
11809 error ("a class-key must be used when declaring a friend");
11810 /* In this case:
11812 template <typename T> struct A {
11813 friend struct A<T>::B;
11816 A<T>::B will be represented by a TYPENAME_TYPE, and
11817 therefore not recognized by check_tag_decl. */
11818 if (!type)
11820 tree specifier;
11822 for (specifier = decl_specifiers;
11823 specifier;
11824 specifier = TREE_CHAIN (specifier))
11826 tree s = TREE_VALUE (specifier);
11828 if (TREE_CODE (s) == IDENTIFIER_NODE
11829 && IDENTIFIER_GLOBAL_VALUE (s))
11830 type = IDENTIFIER_GLOBAL_VALUE (s);
11831 if (TREE_CODE (s) == TYPE_DECL)
11832 s = TREE_TYPE (s);
11833 if (TYPE_P (s))
11835 type = s;
11836 break;
11840 if (!type)
11841 error ("friend declaration does not name a class or "
11842 "function");
11843 else
11844 make_friend_class (current_class_type, type,
11845 /*complain=*/true);
11847 /* If there is no TYPE, an error message will already have
11848 been issued. */
11849 else if (!type)
11851 /* An anonymous aggregate has to be handled specially; such
11852 a declaration really declares a data member (with a
11853 particular type), as opposed to a nested class. */
11854 else if (ANON_AGGR_TYPE_P (type))
11856 /* Remove constructors and such from TYPE, now that we
11857 know it is an anonymous aggregate. */
11858 fixup_anonymous_aggr (type);
11859 /* And make the corresponding data member. */
11860 decl = build_decl (FIELD_DECL, NULL_TREE, type);
11861 /* Add it to the class. */
11862 finish_member_declaration (decl);
11866 else
11868 /* See if these declarations will be friends. */
11869 friend_p = cp_parser_friend_p (decl_specifiers);
11871 /* Keep going until we hit the `;' at the end of the
11872 declaration. */
11873 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11875 tree attributes = NULL_TREE;
11876 tree first_attribute;
11878 /* Peek at the next token. */
11879 token = cp_lexer_peek_token (parser->lexer);
11881 /* Check for a bitfield declaration. */
11882 if (token->type == CPP_COLON
11883 || (token->type == CPP_NAME
11884 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
11885 == CPP_COLON))
11887 tree identifier;
11888 tree width;
11890 /* Get the name of the bitfield. Note that we cannot just
11891 check TOKEN here because it may have been invalidated by
11892 the call to cp_lexer_peek_nth_token above. */
11893 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
11894 identifier = cp_parser_identifier (parser);
11895 else
11896 identifier = NULL_TREE;
11898 /* Consume the `:' token. */
11899 cp_lexer_consume_token (parser->lexer);
11900 /* Get the width of the bitfield. */
11901 width
11902 = cp_parser_constant_expression (parser,
11903 /*allow_non_constant=*/false,
11904 NULL);
11906 /* Look for attributes that apply to the bitfield. */
11907 attributes = cp_parser_attributes_opt (parser);
11908 /* Remember which attributes are prefix attributes and
11909 which are not. */
11910 first_attribute = attributes;
11911 /* Combine the attributes. */
11912 attributes = chainon (prefix_attributes, attributes);
11914 /* Create the bitfield declaration. */
11915 decl = grokbitfield (identifier,
11916 decl_specifiers,
11917 width);
11918 /* Apply the attributes. */
11919 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
11921 else
11923 tree declarator;
11924 tree initializer;
11925 tree asm_specification;
11926 int ctor_dtor_or_conv_p;
11928 /* Parse the declarator. */
11929 declarator
11930 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11931 &ctor_dtor_or_conv_p);
11933 /* If something went wrong parsing the declarator, make sure
11934 that we at least consume some tokens. */
11935 if (declarator == error_mark_node)
11937 /* Skip to the end of the statement. */
11938 cp_parser_skip_to_end_of_statement (parser);
11939 break;
11942 cp_parser_check_for_definition_in_return_type
11943 (declarator, declares_class_or_enum);
11945 /* Look for an asm-specification. */
11946 asm_specification = cp_parser_asm_specification_opt (parser);
11947 /* Look for attributes that apply to the declaration. */
11948 attributes = cp_parser_attributes_opt (parser);
11949 /* Remember which attributes are prefix attributes and
11950 which are not. */
11951 first_attribute = attributes;
11952 /* Combine the attributes. */
11953 attributes = chainon (prefix_attributes, attributes);
11955 /* If it's an `=', then we have a constant-initializer or a
11956 pure-specifier. It is not correct to parse the
11957 initializer before registering the member declaration
11958 since the member declaration should be in scope while
11959 its initializer is processed. However, the rest of the
11960 front end does not yet provide an interface that allows
11961 us to handle this correctly. */
11962 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11964 /* In [class.mem]:
11966 A pure-specifier shall be used only in the declaration of
11967 a virtual function.
11969 A member-declarator can contain a constant-initializer
11970 only if it declares a static member of integral or
11971 enumeration type.
11973 Therefore, if the DECLARATOR is for a function, we look
11974 for a pure-specifier; otherwise, we look for a
11975 constant-initializer. When we call `grokfield', it will
11976 perform more stringent semantics checks. */
11977 if (TREE_CODE (declarator) == CALL_EXPR)
11978 initializer = cp_parser_pure_specifier (parser);
11979 else
11981 /* This declaration cannot be a function
11982 definition. */
11983 cp_parser_commit_to_tentative_parse (parser);
11984 /* Parse the initializer. */
11985 initializer = cp_parser_constant_initializer (parser);
11988 /* Otherwise, there is no initializer. */
11989 else
11990 initializer = NULL_TREE;
11992 /* See if we are probably looking at a function
11993 definition. We are certainly not looking at at a
11994 member-declarator. Calling `grokfield' has
11995 side-effects, so we must not do it unless we are sure
11996 that we are looking at a member-declarator. */
11997 if (cp_parser_token_starts_function_definition_p
11998 (cp_lexer_peek_token (parser->lexer)))
11999 decl = error_mark_node;
12000 else
12002 /* Create the declaration. */
12003 decl = grokfield (declarator, decl_specifiers,
12004 initializer, asm_specification,
12005 attributes);
12006 /* Any initialization must have been from a
12007 constant-expression. */
12008 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12009 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12013 /* Reset PREFIX_ATTRIBUTES. */
12014 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12015 attributes = TREE_CHAIN (attributes);
12016 if (attributes)
12017 TREE_CHAIN (attributes) = NULL_TREE;
12019 /* If there is any qualification still in effect, clear it
12020 now; we will be starting fresh with the next declarator. */
12021 parser->scope = NULL_TREE;
12022 parser->qualifying_scope = NULL_TREE;
12023 parser->object_scope = NULL_TREE;
12024 /* If it's a `,', then there are more declarators. */
12025 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12026 cp_lexer_consume_token (parser->lexer);
12027 /* If the next token isn't a `;', then we have a parse error. */
12028 else if (cp_lexer_next_token_is_not (parser->lexer,
12029 CPP_SEMICOLON))
12031 cp_parser_error (parser, "expected `;'");
12032 /* Skip tokens until we find a `;' */
12033 cp_parser_skip_to_end_of_statement (parser);
12035 break;
12038 if (decl)
12040 /* Add DECL to the list of members. */
12041 if (!friend_p)
12042 finish_member_declaration (decl);
12044 if (TREE_CODE (decl) == FUNCTION_DECL)
12045 cp_parser_save_default_args (parser, decl);
12050 /* If everything went well, look for the `;'. */
12051 if (cp_parser_parse_definitely (parser))
12053 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12054 return;
12057 /* Parse the function-definition. */
12058 decl = cp_parser_function_definition (parser, &friend_p);
12059 /* If the member was not a friend, declare it here. */
12060 if (!friend_p)
12061 finish_member_declaration (decl);
12062 /* Peek at the next token. */
12063 token = cp_lexer_peek_token (parser->lexer);
12064 /* If the next token is a semicolon, consume it. */
12065 if (token->type == CPP_SEMICOLON)
12066 cp_lexer_consume_token (parser->lexer);
12069 /* Parse a pure-specifier.
12071 pure-specifier:
12074 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12075 Otherwiser, ERROR_MARK_NODE is returned. */
12077 static tree
12078 cp_parser_pure_specifier (cp_parser* parser)
12080 cp_token *token;
12082 /* Look for the `=' token. */
12083 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12084 return error_mark_node;
12085 /* Look for the `0' token. */
12086 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12087 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12088 to get information from the lexer about how the number was
12089 spelled in order to fix this problem. */
12090 if (!token || !integer_zerop (token->value))
12091 return error_mark_node;
12093 return integer_zero_node;
12096 /* Parse a constant-initializer.
12098 constant-initializer:
12099 = constant-expression
12101 Returns a representation of the constant-expression. */
12103 static tree
12104 cp_parser_constant_initializer (cp_parser* parser)
12106 /* Look for the `=' token. */
12107 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12108 return error_mark_node;
12110 /* It is invalid to write:
12112 struct S { static const int i = { 7 }; };
12115 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12117 cp_parser_error (parser,
12118 "a brace-enclosed initializer is not allowed here");
12119 /* Consume the opening brace. */
12120 cp_lexer_consume_token (parser->lexer);
12121 /* Skip the initializer. */
12122 cp_parser_skip_to_closing_brace (parser);
12123 /* Look for the trailing `}'. */
12124 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12126 return error_mark_node;
12129 return cp_parser_constant_expression (parser,
12130 /*allow_non_constant=*/false,
12131 NULL);
12134 /* Derived classes [gram.class.derived] */
12136 /* Parse a base-clause.
12138 base-clause:
12139 : base-specifier-list
12141 base-specifier-list:
12142 base-specifier
12143 base-specifier-list , base-specifier
12145 Returns a TREE_LIST representing the base-classes, in the order in
12146 which they were declared. The representation of each node is as
12147 described by cp_parser_base_specifier.
12149 In the case that no bases are specified, this function will return
12150 NULL_TREE, not ERROR_MARK_NODE. */
12152 static tree
12153 cp_parser_base_clause (cp_parser* parser)
12155 tree bases = NULL_TREE;
12157 /* Look for the `:' that begins the list. */
12158 cp_parser_require (parser, CPP_COLON, "`:'");
12160 /* Scan the base-specifier-list. */
12161 while (true)
12163 cp_token *token;
12164 tree base;
12166 /* Look for the base-specifier. */
12167 base = cp_parser_base_specifier (parser);
12168 /* Add BASE to the front of the list. */
12169 if (base != error_mark_node)
12171 TREE_CHAIN (base) = bases;
12172 bases = base;
12174 /* Peek at the next token. */
12175 token = cp_lexer_peek_token (parser->lexer);
12176 /* If it's not a comma, then the list is complete. */
12177 if (token->type != CPP_COMMA)
12178 break;
12179 /* Consume the `,'. */
12180 cp_lexer_consume_token (parser->lexer);
12183 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12184 base class had a qualified name. However, the next name that
12185 appears is certainly not qualified. */
12186 parser->scope = NULL_TREE;
12187 parser->qualifying_scope = NULL_TREE;
12188 parser->object_scope = NULL_TREE;
12190 return nreverse (bases);
12193 /* Parse a base-specifier.
12195 base-specifier:
12196 :: [opt] nested-name-specifier [opt] class-name
12197 virtual access-specifier [opt] :: [opt] nested-name-specifier
12198 [opt] class-name
12199 access-specifier virtual [opt] :: [opt] nested-name-specifier
12200 [opt] class-name
12202 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12203 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12204 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12205 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12207 static tree
12208 cp_parser_base_specifier (cp_parser* parser)
12210 cp_token *token;
12211 bool done = false;
12212 bool virtual_p = false;
12213 bool duplicate_virtual_error_issued_p = false;
12214 bool duplicate_access_error_issued_p = false;
12215 bool class_scope_p, template_p;
12216 tree access = access_default_node;
12217 tree type;
12219 /* Process the optional `virtual' and `access-specifier'. */
12220 while (!done)
12222 /* Peek at the next token. */
12223 token = cp_lexer_peek_token (parser->lexer);
12224 /* Process `virtual'. */
12225 switch (token->keyword)
12227 case RID_VIRTUAL:
12228 /* If `virtual' appears more than once, issue an error. */
12229 if (virtual_p && !duplicate_virtual_error_issued_p)
12231 cp_parser_error (parser,
12232 "`virtual' specified more than once in base-specified");
12233 duplicate_virtual_error_issued_p = true;
12236 virtual_p = true;
12238 /* Consume the `virtual' token. */
12239 cp_lexer_consume_token (parser->lexer);
12241 break;
12243 case RID_PUBLIC:
12244 case RID_PROTECTED:
12245 case RID_PRIVATE:
12246 /* If more than one access specifier appears, issue an
12247 error. */
12248 if (access != access_default_node
12249 && !duplicate_access_error_issued_p)
12251 cp_parser_error (parser,
12252 "more than one access specifier in base-specified");
12253 duplicate_access_error_issued_p = true;
12256 access = ridpointers[(int) token->keyword];
12258 /* Consume the access-specifier. */
12259 cp_lexer_consume_token (parser->lexer);
12261 break;
12263 default:
12264 done = true;
12265 break;
12269 /* Look for the optional `::' operator. */
12270 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12271 /* Look for the nested-name-specifier. The simplest way to
12272 implement:
12274 [temp.res]
12276 The keyword `typename' is not permitted in a base-specifier or
12277 mem-initializer; in these contexts a qualified name that
12278 depends on a template-parameter is implicitly assumed to be a
12279 type name.
12281 is to pretend that we have seen the `typename' keyword at this
12282 point. */
12283 cp_parser_nested_name_specifier_opt (parser,
12284 /*typename_keyword_p=*/true,
12285 /*check_dependency_p=*/true,
12286 /*type_p=*/true);
12287 /* If the base class is given by a qualified name, assume that names
12288 we see are type names or templates, as appropriate. */
12289 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12290 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12292 /* Finally, look for the class-name. */
12293 type = cp_parser_class_name (parser,
12294 class_scope_p,
12295 template_p,
12296 /*type_p=*/true,
12297 /*check_dependency_p=*/true,
12298 /*class_head_p=*/false);
12300 if (type == error_mark_node)
12301 return error_mark_node;
12303 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12306 /* Exception handling [gram.exception] */
12308 /* Parse an (optional) exception-specification.
12310 exception-specification:
12311 throw ( type-id-list [opt] )
12313 Returns a TREE_LIST representing the exception-specification. The
12314 TREE_VALUE of each node is a type. */
12316 static tree
12317 cp_parser_exception_specification_opt (cp_parser* parser)
12319 cp_token *token;
12320 tree type_id_list;
12322 /* Peek at the next token. */
12323 token = cp_lexer_peek_token (parser->lexer);
12324 /* If it's not `throw', then there's no exception-specification. */
12325 if (!cp_parser_is_keyword (token, RID_THROW))
12326 return NULL_TREE;
12328 /* Consume the `throw'. */
12329 cp_lexer_consume_token (parser->lexer);
12331 /* Look for the `('. */
12332 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12334 /* Peek at the next token. */
12335 token = cp_lexer_peek_token (parser->lexer);
12336 /* If it's not a `)', then there is a type-id-list. */
12337 if (token->type != CPP_CLOSE_PAREN)
12339 const char *saved_message;
12341 /* Types may not be defined in an exception-specification. */
12342 saved_message = parser->type_definition_forbidden_message;
12343 parser->type_definition_forbidden_message
12344 = "types may not be defined in an exception-specification";
12345 /* Parse the type-id-list. */
12346 type_id_list = cp_parser_type_id_list (parser);
12347 /* Restore the saved message. */
12348 parser->type_definition_forbidden_message = saved_message;
12350 else
12351 type_id_list = empty_except_spec;
12353 /* Look for the `)'. */
12354 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12356 return type_id_list;
12359 /* Parse an (optional) type-id-list.
12361 type-id-list:
12362 type-id
12363 type-id-list , type-id
12365 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12366 in the order that the types were presented. */
12368 static tree
12369 cp_parser_type_id_list (cp_parser* parser)
12371 tree types = NULL_TREE;
12373 while (true)
12375 cp_token *token;
12376 tree type;
12378 /* Get the next type-id. */
12379 type = cp_parser_type_id (parser);
12380 /* Add it to the list. */
12381 types = add_exception_specifier (types, type, /*complain=*/1);
12382 /* Peek at the next token. */
12383 token = cp_lexer_peek_token (parser->lexer);
12384 /* If it is not a `,', we are done. */
12385 if (token->type != CPP_COMMA)
12386 break;
12387 /* Consume the `,'. */
12388 cp_lexer_consume_token (parser->lexer);
12391 return nreverse (types);
12394 /* Parse a try-block.
12396 try-block:
12397 try compound-statement handler-seq */
12399 static tree
12400 cp_parser_try_block (cp_parser* parser)
12402 tree try_block;
12404 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12405 try_block = begin_try_block ();
12406 cp_parser_compound_statement (parser, false);
12407 finish_try_block (try_block);
12408 cp_parser_handler_seq (parser);
12409 finish_handler_sequence (try_block);
12411 return try_block;
12414 /* Parse a function-try-block.
12416 function-try-block:
12417 try ctor-initializer [opt] function-body handler-seq */
12419 static bool
12420 cp_parser_function_try_block (cp_parser* parser)
12422 tree try_block;
12423 bool ctor_initializer_p;
12425 /* Look for the `try' keyword. */
12426 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12427 return false;
12428 /* Let the rest of the front-end know where we are. */
12429 try_block = begin_function_try_block ();
12430 /* Parse the function-body. */
12431 ctor_initializer_p
12432 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12433 /* We're done with the `try' part. */
12434 finish_function_try_block (try_block);
12435 /* Parse the handlers. */
12436 cp_parser_handler_seq (parser);
12437 /* We're done with the handlers. */
12438 finish_function_handler_sequence (try_block);
12440 return ctor_initializer_p;
12443 /* Parse a handler-seq.
12445 handler-seq:
12446 handler handler-seq [opt] */
12448 static void
12449 cp_parser_handler_seq (cp_parser* parser)
12451 while (true)
12453 cp_token *token;
12455 /* Parse the handler. */
12456 cp_parser_handler (parser);
12457 /* Peek at the next token. */
12458 token = cp_lexer_peek_token (parser->lexer);
12459 /* If it's not `catch' then there are no more handlers. */
12460 if (!cp_parser_is_keyword (token, RID_CATCH))
12461 break;
12465 /* Parse a handler.
12467 handler:
12468 catch ( exception-declaration ) compound-statement */
12470 static void
12471 cp_parser_handler (cp_parser* parser)
12473 tree handler;
12474 tree declaration;
12476 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12477 handler = begin_handler ();
12478 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12479 declaration = cp_parser_exception_declaration (parser);
12480 finish_handler_parms (declaration, handler);
12481 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12482 cp_parser_compound_statement (parser, false);
12483 finish_handler (handler);
12486 /* Parse an exception-declaration.
12488 exception-declaration:
12489 type-specifier-seq declarator
12490 type-specifier-seq abstract-declarator
12491 type-specifier-seq
12492 ...
12494 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12495 ellipsis variant is used. */
12497 static tree
12498 cp_parser_exception_declaration (cp_parser* parser)
12500 tree type_specifiers;
12501 tree declarator;
12502 const char *saved_message;
12504 /* If it's an ellipsis, it's easy to handle. */
12505 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12507 /* Consume the `...' token. */
12508 cp_lexer_consume_token (parser->lexer);
12509 return NULL_TREE;
12512 /* Types may not be defined in exception-declarations. */
12513 saved_message = parser->type_definition_forbidden_message;
12514 parser->type_definition_forbidden_message
12515 = "types may not be defined in exception-declarations";
12517 /* Parse the type-specifier-seq. */
12518 type_specifiers = cp_parser_type_specifier_seq (parser);
12519 /* If it's a `)', then there is no declarator. */
12520 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12521 declarator = NULL_TREE;
12522 else
12523 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12524 /*ctor_dtor_or_conv_p=*/NULL);
12526 /* Restore the saved message. */
12527 parser->type_definition_forbidden_message = saved_message;
12529 return start_handler_parms (type_specifiers, declarator);
12532 /* Parse a throw-expression.
12534 throw-expression:
12535 throw assignment-expression [opt]
12537 Returns a THROW_EXPR representing the throw-expression. */
12539 static tree
12540 cp_parser_throw_expression (cp_parser* parser)
12542 tree expression;
12544 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12545 /* We can't be sure if there is an assignment-expression or not. */
12546 cp_parser_parse_tentatively (parser);
12547 /* Try it. */
12548 expression = cp_parser_assignment_expression (parser);
12549 /* If it didn't work, this is just a rethrow. */
12550 if (!cp_parser_parse_definitely (parser))
12551 expression = NULL_TREE;
12553 return build_throw (expression);
12556 /* GNU Extensions */
12558 /* Parse an (optional) asm-specification.
12560 asm-specification:
12561 asm ( string-literal )
12563 If the asm-specification is present, returns a STRING_CST
12564 corresponding to the string-literal. Otherwise, returns
12565 NULL_TREE. */
12567 static tree
12568 cp_parser_asm_specification_opt (cp_parser* parser)
12570 cp_token *token;
12571 tree asm_specification;
12573 /* Peek at the next token. */
12574 token = cp_lexer_peek_token (parser->lexer);
12575 /* If the next token isn't the `asm' keyword, then there's no
12576 asm-specification. */
12577 if (!cp_parser_is_keyword (token, RID_ASM))
12578 return NULL_TREE;
12580 /* Consume the `asm' token. */
12581 cp_lexer_consume_token (parser->lexer);
12582 /* Look for the `('. */
12583 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12585 /* Look for the string-literal. */
12586 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12587 if (token)
12588 asm_specification = token->value;
12589 else
12590 asm_specification = NULL_TREE;
12592 /* Look for the `)'. */
12593 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12595 return asm_specification;
12598 /* Parse an asm-operand-list.
12600 asm-operand-list:
12601 asm-operand
12602 asm-operand-list , asm-operand
12604 asm-operand:
12605 string-literal ( expression )
12606 [ string-literal ] string-literal ( expression )
12608 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12609 each node is the expression. The TREE_PURPOSE is itself a
12610 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12611 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12612 is a STRING_CST for the string literal before the parenthesis. */
12614 static tree
12615 cp_parser_asm_operand_list (cp_parser* parser)
12617 tree asm_operands = NULL_TREE;
12619 while (true)
12621 tree string_literal;
12622 tree expression;
12623 tree name;
12624 cp_token *token;
12626 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12628 /* Consume the `[' token. */
12629 cp_lexer_consume_token (parser->lexer);
12630 /* Read the operand name. */
12631 name = cp_parser_identifier (parser);
12632 if (name != error_mark_node)
12633 name = build_string (IDENTIFIER_LENGTH (name),
12634 IDENTIFIER_POINTER (name));
12635 /* Look for the closing `]'. */
12636 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12638 else
12639 name = NULL_TREE;
12640 /* Look for the string-literal. */
12641 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12642 string_literal = token ? token->value : error_mark_node;
12643 /* Look for the `('. */
12644 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12645 /* Parse the expression. */
12646 expression = cp_parser_expression (parser);
12647 /* Look for the `)'. */
12648 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12649 /* Add this operand to the list. */
12650 asm_operands = tree_cons (build_tree_list (name, string_literal),
12651 expression,
12652 asm_operands);
12653 /* If the next token is not a `,', there are no more
12654 operands. */
12655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12656 break;
12657 /* Consume the `,'. */
12658 cp_lexer_consume_token (parser->lexer);
12661 return nreverse (asm_operands);
12664 /* Parse an asm-clobber-list.
12666 asm-clobber-list:
12667 string-literal
12668 asm-clobber-list , string-literal
12670 Returns a TREE_LIST, indicating the clobbers in the order that they
12671 appeared. The TREE_VALUE of each node is a STRING_CST. */
12673 static tree
12674 cp_parser_asm_clobber_list (cp_parser* parser)
12676 tree clobbers = NULL_TREE;
12678 while (true)
12680 cp_token *token;
12681 tree string_literal;
12683 /* Look for the string literal. */
12684 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12685 string_literal = token ? token->value : error_mark_node;
12686 /* Add it to the list. */
12687 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12688 /* If the next token is not a `,', then the list is
12689 complete. */
12690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12691 break;
12692 /* Consume the `,' token. */
12693 cp_lexer_consume_token (parser->lexer);
12696 return clobbers;
12699 /* Parse an (optional) series of attributes.
12701 attributes:
12702 attributes attribute
12704 attribute:
12705 __attribute__ (( attribute-list [opt] ))
12707 The return value is as for cp_parser_attribute_list. */
12709 static tree
12710 cp_parser_attributes_opt (cp_parser* parser)
12712 tree attributes = NULL_TREE;
12714 while (true)
12716 cp_token *token;
12717 tree attribute_list;
12719 /* Peek at the next token. */
12720 token = cp_lexer_peek_token (parser->lexer);
12721 /* If it's not `__attribute__', then we're done. */
12722 if (token->keyword != RID_ATTRIBUTE)
12723 break;
12725 /* Consume the `__attribute__' keyword. */
12726 cp_lexer_consume_token (parser->lexer);
12727 /* Look for the two `(' tokens. */
12728 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12729 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12731 /* Peek at the next token. */
12732 token = cp_lexer_peek_token (parser->lexer);
12733 if (token->type != CPP_CLOSE_PAREN)
12734 /* Parse the attribute-list. */
12735 attribute_list = cp_parser_attribute_list (parser);
12736 else
12737 /* If the next token is a `)', then there is no attribute
12738 list. */
12739 attribute_list = NULL;
12741 /* Look for the two `)' tokens. */
12742 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12743 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12745 /* Add these new attributes to the list. */
12746 attributes = chainon (attributes, attribute_list);
12749 return attributes;
12752 /* Parse an attribute-list.
12754 attribute-list:
12755 attribute
12756 attribute-list , attribute
12758 attribute:
12759 identifier
12760 identifier ( identifier )
12761 identifier ( identifier , expression-list )
12762 identifier ( expression-list )
12764 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12765 TREE_PURPOSE of each node is the identifier indicating which
12766 attribute is in use. The TREE_VALUE represents the arguments, if
12767 any. */
12769 static tree
12770 cp_parser_attribute_list (cp_parser* parser)
12772 tree attribute_list = NULL_TREE;
12774 while (true)
12776 cp_token *token;
12777 tree identifier;
12778 tree attribute;
12780 /* Look for the identifier. We also allow keywords here; for
12781 example `__attribute__ ((const))' is legal. */
12782 token = cp_lexer_peek_token (parser->lexer);
12783 if (token->type != CPP_NAME
12784 && token->type != CPP_KEYWORD)
12785 return error_mark_node;
12786 /* Consume the token. */
12787 token = cp_lexer_consume_token (parser->lexer);
12789 /* Save away the identifier that indicates which attribute this is. */
12790 identifier = token->value;
12791 attribute = build_tree_list (identifier, NULL_TREE);
12793 /* Peek at the next token. */
12794 token = cp_lexer_peek_token (parser->lexer);
12795 /* If it's an `(', then parse the attribute arguments. */
12796 if (token->type == CPP_OPEN_PAREN)
12798 tree arguments;
12800 arguments = (cp_parser_parenthesized_expression_list
12801 (parser, true, /*non_constant_p=*/NULL));
12802 /* Save the identifier and arguments away. */
12803 TREE_VALUE (attribute) = arguments;
12806 /* Add this attribute to the list. */
12807 TREE_CHAIN (attribute) = attribute_list;
12808 attribute_list = attribute;
12810 /* Now, look for more attributes. */
12811 token = cp_lexer_peek_token (parser->lexer);
12812 /* If the next token isn't a `,', we're done. */
12813 if (token->type != CPP_COMMA)
12814 break;
12816 /* Consume the commma and keep going. */
12817 cp_lexer_consume_token (parser->lexer);
12820 /* We built up the list in reverse order. */
12821 return nreverse (attribute_list);
12824 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
12825 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
12826 current value of the PEDANTIC flag, regardless of whether or not
12827 the `__extension__' keyword is present. The caller is responsible
12828 for restoring the value of the PEDANTIC flag. */
12830 static bool
12831 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
12833 /* Save the old value of the PEDANTIC flag. */
12834 *saved_pedantic = pedantic;
12836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
12838 /* Consume the `__extension__' token. */
12839 cp_lexer_consume_token (parser->lexer);
12840 /* We're not being pedantic while the `__extension__' keyword is
12841 in effect. */
12842 pedantic = 0;
12844 return true;
12847 return false;
12850 /* Parse a label declaration.
12852 label-declaration:
12853 __label__ label-declarator-seq ;
12855 label-declarator-seq:
12856 identifier , label-declarator-seq
12857 identifier */
12859 static void
12860 cp_parser_label_declaration (cp_parser* parser)
12862 /* Look for the `__label__' keyword. */
12863 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
12865 while (true)
12867 tree identifier;
12869 /* Look for an identifier. */
12870 identifier = cp_parser_identifier (parser);
12871 /* Declare it as a lobel. */
12872 finish_label_decl (identifier);
12873 /* If the next token is a `;', stop. */
12874 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12875 break;
12876 /* Look for the `,' separating the label declarations. */
12877 cp_parser_require (parser, CPP_COMMA, "`,'");
12880 /* Look for the final `;'. */
12881 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12884 /* Support Functions */
12886 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12887 NAME should have one of the representations used for an
12888 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12889 is returned. If PARSER->SCOPE is a dependent type, then a
12890 SCOPE_REF is returned.
12892 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
12893 returned; the name was already resolved when the TEMPLATE_ID_EXPR
12894 was formed. Abstractly, such entities should not be passed to this
12895 function, because they do not need to be looked up, but it is
12896 simpler to check for this special case here, rather than at the
12897 call-sites.
12899 In cases not explicitly covered above, this function returns a
12900 DECL, OVERLOAD, or baselink representing the result of the lookup.
12901 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
12902 is returned.
12904 If IS_TYPE is TRUE, bindings that do not refer to types are
12905 ignored.
12907 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
12908 are ignored.
12910 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
12911 types. */
12913 static tree
12914 cp_parser_lookup_name (cp_parser *parser, tree name,
12915 bool is_type, bool is_namespace, bool check_dependency)
12917 tree decl;
12918 tree object_type = parser->context->object_type;
12920 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
12921 no longer valid. Note that if we are parsing tentatively, and
12922 the parse fails, OBJECT_TYPE will be automatically restored. */
12923 parser->context->object_type = NULL_TREE;
12925 if (name == error_mark_node)
12926 return error_mark_node;
12928 /* A template-id has already been resolved; there is no lookup to
12929 do. */
12930 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12931 return name;
12932 if (BASELINK_P (name))
12934 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
12935 == TEMPLATE_ID_EXPR),
12936 20020909);
12937 return name;
12940 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
12941 it should already have been checked to make sure that the name
12942 used matches the type being destroyed. */
12943 if (TREE_CODE (name) == BIT_NOT_EXPR)
12945 tree type;
12947 /* Figure out to which type this destructor applies. */
12948 if (parser->scope)
12949 type = parser->scope;
12950 else if (object_type)
12951 type = object_type;
12952 else
12953 type = current_class_type;
12954 /* If that's not a class type, there is no destructor. */
12955 if (!type || !CLASS_TYPE_P (type))
12956 return error_mark_node;
12957 /* If it was a class type, return the destructor. */
12958 return CLASSTYPE_DESTRUCTORS (type);
12961 /* By this point, the NAME should be an ordinary identifier. If
12962 the id-expression was a qualified name, the qualifying scope is
12963 stored in PARSER->SCOPE at this point. */
12964 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
12965 20000619);
12967 /* Perform the lookup. */
12968 if (parser->scope)
12970 bool dependent_p;
12972 if (parser->scope == error_mark_node)
12973 return error_mark_node;
12975 /* If the SCOPE is dependent, the lookup must be deferred until
12976 the template is instantiated -- unless we are explicitly
12977 looking up names in uninstantiated templates. Even then, we
12978 cannot look up the name if the scope is not a class type; it
12979 might, for example, be a template type parameter. */
12980 dependent_p = (TYPE_P (parser->scope)
12981 && !(parser->in_declarator_p
12982 && currently_open_class (parser->scope))
12983 && dependent_type_p (parser->scope));
12984 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
12985 && dependent_p)
12987 if (!is_type)
12988 decl = build_nt (SCOPE_REF, parser->scope, name);
12989 else
12990 /* The resolution to Core Issue 180 says that `struct A::B'
12991 should be considered a type-name, even if `A' is
12992 dependent. */
12993 decl = TYPE_NAME (make_typename_type (parser->scope,
12994 name,
12995 /*complain=*/1));
12997 else
12999 /* If PARSER->SCOPE is a dependent type, then it must be a
13000 class type, and we must not be checking dependencies;
13001 otherwise, we would have processed this lookup above. So
13002 that PARSER->SCOPE is not considered a dependent base by
13003 lookup_member, we must enter the scope here. */
13004 if (dependent_p)
13005 push_scope (parser->scope);
13006 /* If the PARSER->SCOPE is a a template specialization, it
13007 may be instantiated during name lookup. In that case,
13008 errors may be issued. Even if we rollback the current
13009 tentative parse, those errors are valid. */
13010 decl = lookup_qualified_name (parser->scope, name, is_type,
13011 /*complain=*/true);
13012 if (dependent_p)
13013 pop_scope (parser->scope);
13015 parser->qualifying_scope = parser->scope;
13016 parser->object_scope = NULL_TREE;
13018 else if (object_type)
13020 tree object_decl = NULL_TREE;
13021 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13022 OBJECT_TYPE is not a class. */
13023 if (CLASS_TYPE_P (object_type))
13024 /* If the OBJECT_TYPE is a template specialization, it may
13025 be instantiated during name lookup. In that case, errors
13026 may be issued. Even if we rollback the current tentative
13027 parse, those errors are valid. */
13028 object_decl = lookup_member (object_type,
13029 name,
13030 /*protect=*/0, is_type);
13031 /* Look it up in the enclosing context, too. */
13032 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13033 is_namespace,
13034 /*flags=*/0);
13035 parser->object_scope = object_type;
13036 parser->qualifying_scope = NULL_TREE;
13037 if (object_decl)
13038 decl = object_decl;
13040 else
13042 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13043 is_namespace,
13044 /*flags=*/0);
13045 parser->qualifying_scope = NULL_TREE;
13046 parser->object_scope = NULL_TREE;
13049 /* If the lookup failed, let our caller know. */
13050 if (!decl
13051 || decl == error_mark_node
13052 || (TREE_CODE (decl) == FUNCTION_DECL
13053 && DECL_ANTICIPATED (decl)))
13054 return error_mark_node;
13056 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13057 if (TREE_CODE (decl) == TREE_LIST)
13059 /* The error message we have to print is too complicated for
13060 cp_parser_error, so we incorporate its actions directly. */
13061 if (!cp_parser_simulate_error (parser))
13063 error ("reference to `%D' is ambiguous", name);
13064 print_candidates (decl);
13066 return error_mark_node;
13069 my_friendly_assert (DECL_P (decl)
13070 || TREE_CODE (decl) == OVERLOAD
13071 || TREE_CODE (decl) == SCOPE_REF
13072 || BASELINK_P (decl),
13073 20000619);
13075 /* If we have resolved the name of a member declaration, check to
13076 see if the declaration is accessible. When the name resolves to
13077 set of overloaded functions, accessibility is checked when
13078 overload resolution is done.
13080 During an explicit instantiation, access is not checked at all,
13081 as per [temp.explicit]. */
13082 if (DECL_P (decl))
13083 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13085 return decl;
13088 /* Like cp_parser_lookup_name, but for use in the typical case where
13089 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13090 TRUE. */
13092 static tree
13093 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13095 return cp_parser_lookup_name (parser, name,
13096 /*is_type=*/false,
13097 /*is_namespace=*/false,
13098 /*check_dependency=*/true);
13101 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13102 the current context, return the TYPE_DECL. If TAG_NAME_P is
13103 true, the DECL indicates the class being defined in a class-head,
13104 or declared in an elaborated-type-specifier.
13106 Otherwise, return DECL. */
13108 static tree
13109 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13111 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13112 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13114 struct A {
13115 template <typename T> struct B;
13118 template <typename T> struct A::B {};
13120 Similarly, in a elaborated-type-specifier:
13122 namespace N { struct X{}; }
13124 struct A {
13125 template <typename T> friend struct N::X;
13128 However, if the DECL refers to a class type, and we are in
13129 the scope of the class, then the name lookup automatically
13130 finds the TYPE_DECL created by build_self_reference rather
13131 than a TEMPLATE_DECL. For example, in:
13133 template <class T> struct S {
13134 S s;
13137 there is no need to handle such case. */
13139 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13140 return DECL_TEMPLATE_RESULT (decl);
13142 return decl;
13145 /* If too many, or too few, template-parameter lists apply to the
13146 declarator, issue an error message. Returns TRUE if all went well,
13147 and FALSE otherwise. */
13149 static bool
13150 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13151 tree declarator)
13153 unsigned num_templates;
13155 /* We haven't seen any classes that involve template parameters yet. */
13156 num_templates = 0;
13158 switch (TREE_CODE (declarator))
13160 case CALL_EXPR:
13161 case ARRAY_REF:
13162 case INDIRECT_REF:
13163 case ADDR_EXPR:
13165 tree main_declarator = TREE_OPERAND (declarator, 0);
13166 return
13167 cp_parser_check_declarator_template_parameters (parser,
13168 main_declarator);
13171 case SCOPE_REF:
13173 tree scope;
13174 tree member;
13176 scope = TREE_OPERAND (declarator, 0);
13177 member = TREE_OPERAND (declarator, 1);
13179 /* If this is a pointer-to-member, then we are not interested
13180 in the SCOPE, because it does not qualify the thing that is
13181 being declared. */
13182 if (TREE_CODE (member) == INDIRECT_REF)
13183 return (cp_parser_check_declarator_template_parameters
13184 (parser, member));
13186 while (scope && CLASS_TYPE_P (scope))
13188 /* You're supposed to have one `template <...>'
13189 for every template class, but you don't need one
13190 for a full specialization. For example:
13192 template <class T> struct S{};
13193 template <> struct S<int> { void f(); };
13194 void S<int>::f () {}
13196 is correct; there shouldn't be a `template <>' for
13197 the definition of `S<int>::f'. */
13198 if (CLASSTYPE_TEMPLATE_INFO (scope)
13199 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13200 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13201 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13202 ++num_templates;
13204 scope = TYPE_CONTEXT (scope);
13208 /* Fall through. */
13210 default:
13211 /* If the DECLARATOR has the form `X<y>' then it uses one
13212 additional level of template parameters. */
13213 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13214 ++num_templates;
13216 return cp_parser_check_template_parameters (parser,
13217 num_templates);
13221 /* NUM_TEMPLATES were used in the current declaration. If that is
13222 invalid, return FALSE and issue an error messages. Otherwise,
13223 return TRUE. */
13225 static bool
13226 cp_parser_check_template_parameters (cp_parser* parser,
13227 unsigned num_templates)
13229 /* If there are more template classes than parameter lists, we have
13230 something like:
13232 template <class T> void S<T>::R<T>::f (); */
13233 if (parser->num_template_parameter_lists < num_templates)
13235 error ("too few template-parameter-lists");
13236 return false;
13238 /* If there are the same number of template classes and parameter
13239 lists, that's OK. */
13240 if (parser->num_template_parameter_lists == num_templates)
13241 return true;
13242 /* If there are more, but only one more, then we are referring to a
13243 member template. That's OK too. */
13244 if (parser->num_template_parameter_lists == num_templates + 1)
13245 return true;
13246 /* Otherwise, there are too many template parameter lists. We have
13247 something like:
13249 template <class T> template <class U> void S::f(); */
13250 error ("too many template-parameter-lists");
13251 return false;
13254 /* Parse a binary-expression of the general form:
13256 binary-expression:
13257 <expr>
13258 binary-expression <token> <expr>
13260 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13261 to parser the <expr>s. If the first production is used, then the
13262 value returned by FN is returned directly. Otherwise, a node with
13263 the indicated EXPR_TYPE is returned, with operands corresponding to
13264 the two sub-expressions. */
13266 static tree
13267 cp_parser_binary_expression (cp_parser* parser,
13268 const cp_parser_token_tree_map token_tree_map,
13269 cp_parser_expression_fn fn)
13271 tree lhs;
13273 /* Parse the first expression. */
13274 lhs = (*fn) (parser);
13275 /* Now, look for more expressions. */
13276 while (true)
13278 cp_token *token;
13279 const cp_parser_token_tree_map_node *map_node;
13280 tree rhs;
13282 /* Peek at the next token. */
13283 token = cp_lexer_peek_token (parser->lexer);
13284 /* If the token is `>', and that's not an operator at the
13285 moment, then we're done. */
13286 if (token->type == CPP_GREATER
13287 && !parser->greater_than_is_operator_p)
13288 break;
13289 /* If we find one of the tokens we want, build the corresponding
13290 tree representation. */
13291 for (map_node = token_tree_map;
13292 map_node->token_type != CPP_EOF;
13293 ++map_node)
13294 if (map_node->token_type == token->type)
13296 /* Consume the operator token. */
13297 cp_lexer_consume_token (parser->lexer);
13298 /* Parse the right-hand side of the expression. */
13299 rhs = (*fn) (parser);
13300 /* Build the binary tree node. */
13301 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13302 break;
13305 /* If the token wasn't one of the ones we want, we're done. */
13306 if (map_node->token_type == CPP_EOF)
13307 break;
13310 return lhs;
13313 /* Parse an optional `::' token indicating that the following name is
13314 from the global namespace. If so, PARSER->SCOPE is set to the
13315 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13316 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13317 Returns the new value of PARSER->SCOPE, if the `::' token is
13318 present, and NULL_TREE otherwise. */
13320 static tree
13321 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13323 cp_token *token;
13325 /* Peek at the next token. */
13326 token = cp_lexer_peek_token (parser->lexer);
13327 /* If we're looking at a `::' token then we're starting from the
13328 global namespace, not our current location. */
13329 if (token->type == CPP_SCOPE)
13331 /* Consume the `::' token. */
13332 cp_lexer_consume_token (parser->lexer);
13333 /* Set the SCOPE so that we know where to start the lookup. */
13334 parser->scope = global_namespace;
13335 parser->qualifying_scope = global_namespace;
13336 parser->object_scope = NULL_TREE;
13338 return parser->scope;
13340 else if (!current_scope_valid_p)
13342 parser->scope = NULL_TREE;
13343 parser->qualifying_scope = NULL_TREE;
13344 parser->object_scope = NULL_TREE;
13347 return NULL_TREE;
13350 /* Returns TRUE if the upcoming token sequence is the start of a
13351 constructor declarator. If FRIEND_P is true, the declarator is
13352 preceded by the `friend' specifier. */
13354 static bool
13355 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13357 bool constructor_p;
13358 tree type_decl = NULL_TREE;
13359 bool nested_name_p;
13360 cp_token *next_token;
13362 /* The common case is that this is not a constructor declarator, so
13363 try to avoid doing lots of work if at all possible. It's not
13364 valid declare a constructor at function scope. */
13365 if (at_function_scope_p ())
13366 return false;
13367 /* And only certain tokens can begin a constructor declarator. */
13368 next_token = cp_lexer_peek_token (parser->lexer);
13369 if (next_token->type != CPP_NAME
13370 && next_token->type != CPP_SCOPE
13371 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13372 && next_token->type != CPP_TEMPLATE_ID)
13373 return false;
13375 /* Parse tentatively; we are going to roll back all of the tokens
13376 consumed here. */
13377 cp_parser_parse_tentatively (parser);
13378 /* Assume that we are looking at a constructor declarator. */
13379 constructor_p = true;
13381 /* Look for the optional `::' operator. */
13382 cp_parser_global_scope_opt (parser,
13383 /*current_scope_valid_p=*/false);
13384 /* Look for the nested-name-specifier. */
13385 nested_name_p
13386 = (cp_parser_nested_name_specifier_opt (parser,
13387 /*typename_keyword_p=*/false,
13388 /*check_dependency_p=*/false,
13389 /*type_p=*/false)
13390 != NULL_TREE);
13391 /* Outside of a class-specifier, there must be a
13392 nested-name-specifier. */
13393 if (!nested_name_p &&
13394 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13395 || friend_p))
13396 constructor_p = false;
13397 /* If we still think that this might be a constructor-declarator,
13398 look for a class-name. */
13399 if (constructor_p)
13401 /* If we have:
13403 template <typename T> struct S { S(); };
13404 template <typename T> S<T>::S ();
13406 we must recognize that the nested `S' names a class.
13407 Similarly, for:
13409 template <typename T> S<T>::S<T> ();
13411 we must recognize that the nested `S' names a template. */
13412 type_decl = cp_parser_class_name (parser,
13413 /*typename_keyword_p=*/false,
13414 /*template_keyword_p=*/false,
13415 /*type_p=*/false,
13416 /*check_dependency_p=*/false,
13417 /*class_head_p=*/false);
13418 /* If there was no class-name, then this is not a constructor. */
13419 constructor_p = !cp_parser_error_occurred (parser);
13422 /* If we're still considering a constructor, we have to see a `(',
13423 to begin the parameter-declaration-clause, followed by either a
13424 `)', an `...', or a decl-specifier. We need to check for a
13425 type-specifier to avoid being fooled into thinking that:
13427 S::S (f) (int);
13429 is a constructor. (It is actually a function named `f' that
13430 takes one parameter (of type `int') and returns a value of type
13431 `S::S'. */
13432 if (constructor_p
13433 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13436 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13437 && !cp_parser_storage_class_specifier_opt (parser))
13439 tree type;
13441 /* Names appearing in the type-specifier should be looked up
13442 in the scope of the class. */
13443 if (current_class_type)
13444 type = NULL_TREE;
13445 else
13447 type = TREE_TYPE (type_decl);
13448 if (TREE_CODE (type) == TYPENAME_TYPE)
13450 type = resolve_typename_type (type,
13451 /*only_current_p=*/false);
13452 if (type == error_mark_node)
13454 cp_parser_abort_tentative_parse (parser);
13455 return false;
13458 push_scope (type);
13460 /* Look for the type-specifier. */
13461 cp_parser_type_specifier (parser,
13462 CP_PARSER_FLAGS_NONE,
13463 /*is_friend=*/false,
13464 /*is_declarator=*/true,
13465 /*declares_class_or_enum=*/NULL,
13466 /*is_cv_qualifier=*/NULL);
13467 /* Leave the scope of the class. */
13468 if (type)
13469 pop_scope (type);
13471 constructor_p = !cp_parser_error_occurred (parser);
13474 else
13475 constructor_p = false;
13476 /* We did not really want to consume any tokens. */
13477 cp_parser_abort_tentative_parse (parser);
13479 return constructor_p;
13482 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13483 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13484 they must be performed once we are in the scope of the function.
13486 Returns the function defined. */
13488 static tree
13489 cp_parser_function_definition_from_specifiers_and_declarator
13490 (cp_parser* parser,
13491 tree decl_specifiers,
13492 tree attributes,
13493 tree declarator)
13495 tree fn;
13496 bool success_p;
13498 /* Begin the function-definition. */
13499 success_p = begin_function_definition (decl_specifiers,
13500 attributes,
13501 declarator);
13503 /* If there were names looked up in the decl-specifier-seq that we
13504 did not check, check them now. We must wait until we are in the
13505 scope of the function to perform the checks, since the function
13506 might be a friend. */
13507 perform_deferred_access_checks ();
13509 if (!success_p)
13511 /* If begin_function_definition didn't like the definition, skip
13512 the entire function. */
13513 error ("invalid function declaration");
13514 cp_parser_skip_to_end_of_block_or_statement (parser);
13515 fn = error_mark_node;
13517 else
13518 fn = cp_parser_function_definition_after_declarator (parser,
13519 /*inline_p=*/false);
13521 return fn;
13524 /* Parse the part of a function-definition that follows the
13525 declarator. INLINE_P is TRUE iff this function is an inline
13526 function defined with a class-specifier.
13528 Returns the function defined. */
13530 static tree
13531 cp_parser_function_definition_after_declarator (cp_parser* parser,
13532 bool inline_p)
13534 tree fn;
13535 bool ctor_initializer_p = false;
13536 bool saved_in_unbraced_linkage_specification_p;
13537 unsigned saved_num_template_parameter_lists;
13539 /* If the next token is `return', then the code may be trying to
13540 make use of the "named return value" extension that G++ used to
13541 support. */
13542 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13544 /* Consume the `return' keyword. */
13545 cp_lexer_consume_token (parser->lexer);
13546 /* Look for the identifier that indicates what value is to be
13547 returned. */
13548 cp_parser_identifier (parser);
13549 /* Issue an error message. */
13550 error ("named return values are no longer supported");
13551 /* Skip tokens until we reach the start of the function body. */
13552 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13553 cp_lexer_consume_token (parser->lexer);
13555 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13556 anything declared inside `f'. */
13557 saved_in_unbraced_linkage_specification_p
13558 = parser->in_unbraced_linkage_specification_p;
13559 parser->in_unbraced_linkage_specification_p = false;
13560 /* Inside the function, surrounding template-parameter-lists do not
13561 apply. */
13562 saved_num_template_parameter_lists
13563 = parser->num_template_parameter_lists;
13564 parser->num_template_parameter_lists = 0;
13565 /* If the next token is `try', then we are looking at a
13566 function-try-block. */
13567 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13568 ctor_initializer_p = cp_parser_function_try_block (parser);
13569 /* A function-try-block includes the function-body, so we only do
13570 this next part if we're not processing a function-try-block. */
13571 else
13572 ctor_initializer_p
13573 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13575 /* Finish the function. */
13576 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13577 (inline_p ? 2 : 0));
13578 /* Generate code for it, if necessary. */
13579 expand_or_defer_fn (fn);
13580 /* Restore the saved values. */
13581 parser->in_unbraced_linkage_specification_p
13582 = saved_in_unbraced_linkage_specification_p;
13583 parser->num_template_parameter_lists
13584 = saved_num_template_parameter_lists;
13586 return fn;
13589 /* Parse a template-declaration, assuming that the `export' (and
13590 `extern') keywords, if present, has already been scanned. MEMBER_P
13591 is as for cp_parser_template_declaration. */
13593 static void
13594 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13596 tree decl = NULL_TREE;
13597 tree parameter_list;
13598 bool friend_p = false;
13600 /* Look for the `template' keyword. */
13601 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13602 return;
13604 /* And the `<'. */
13605 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13606 return;
13608 /* Parse the template parameters. */
13609 begin_template_parm_list ();
13610 /* If the next token is `>', then we have an invalid
13611 specialization. Rather than complain about an invalid template
13612 parameter, issue an error message here. */
13613 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13615 cp_parser_error (parser, "invalid explicit specialization");
13616 parameter_list = NULL_TREE;
13618 else
13619 parameter_list = cp_parser_template_parameter_list (parser);
13620 parameter_list = end_template_parm_list (parameter_list);
13621 /* Look for the `>'. */
13622 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13623 /* We just processed one more parameter list. */
13624 ++parser->num_template_parameter_lists;
13625 /* If the next token is `template', there are more template
13626 parameters. */
13627 if (cp_lexer_next_token_is_keyword (parser->lexer,
13628 RID_TEMPLATE))
13629 cp_parser_template_declaration_after_export (parser, member_p);
13630 else
13632 decl = cp_parser_single_declaration (parser,
13633 member_p,
13634 &friend_p);
13636 /* If this is a member template declaration, let the front
13637 end know. */
13638 if (member_p && !friend_p && decl)
13639 decl = finish_member_template_decl (decl);
13640 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13641 make_friend_class (current_class_type, TREE_TYPE (decl),
13642 /*complain=*/true);
13644 /* We are done with the current parameter list. */
13645 --parser->num_template_parameter_lists;
13647 /* Finish up. */
13648 finish_template_decl (parameter_list);
13650 /* Register member declarations. */
13651 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13652 finish_member_declaration (decl);
13654 /* If DECL is a function template, we must return to parse it later.
13655 (Even though there is no definition, there might be default
13656 arguments that need handling.) */
13657 if (member_p && decl
13658 && (TREE_CODE (decl) == FUNCTION_DECL
13659 || DECL_FUNCTION_TEMPLATE_P (decl)))
13660 TREE_VALUE (parser->unparsed_functions_queues)
13661 = tree_cons (NULL_TREE, decl,
13662 TREE_VALUE (parser->unparsed_functions_queues));
13665 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13666 `function-definition' sequence. MEMBER_P is true, this declaration
13667 appears in a class scope.
13669 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13670 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13672 static tree
13673 cp_parser_single_declaration (cp_parser* parser,
13674 bool member_p,
13675 bool* friend_p)
13677 int declares_class_or_enum;
13678 tree decl = NULL_TREE;
13679 tree decl_specifiers;
13680 tree attributes;
13682 /* Parse the dependent declaration. We don't know yet
13683 whether it will be a function-definition. */
13684 cp_parser_parse_tentatively (parser);
13685 /* Defer access checks until we know what is being declared. */
13686 push_deferring_access_checks (dk_deferred);
13688 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13689 alternative. */
13690 decl_specifiers
13691 = cp_parser_decl_specifier_seq (parser,
13692 CP_PARSER_FLAGS_OPTIONAL,
13693 &attributes,
13694 &declares_class_or_enum);
13695 /* Gather up the access checks that occurred the
13696 decl-specifier-seq. */
13697 stop_deferring_access_checks ();
13699 /* Check for the declaration of a template class. */
13700 if (declares_class_or_enum)
13702 if (cp_parser_declares_only_class_p (parser))
13704 decl = shadow_tag (decl_specifiers);
13705 if (decl)
13706 decl = TYPE_NAME (decl);
13707 else
13708 decl = error_mark_node;
13711 else
13712 decl = NULL_TREE;
13713 /* If it's not a template class, try for a template function. If
13714 the next token is a `;', then this declaration does not declare
13715 anything. But, if there were errors in the decl-specifiers, then
13716 the error might well have come from an attempted class-specifier.
13717 In that case, there's no need to warn about a missing declarator. */
13718 if (!decl
13719 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13720 || !value_member (error_mark_node, decl_specifiers)))
13721 decl = cp_parser_init_declarator (parser,
13722 decl_specifiers,
13723 attributes,
13724 /*function_definition_allowed_p=*/false,
13725 member_p,
13726 declares_class_or_enum,
13727 /*function_definition_p=*/NULL);
13729 pop_deferring_access_checks ();
13731 /* Clear any current qualification; whatever comes next is the start
13732 of something new. */
13733 parser->scope = NULL_TREE;
13734 parser->qualifying_scope = NULL_TREE;
13735 parser->object_scope = NULL_TREE;
13736 /* Look for a trailing `;' after the declaration. */
13737 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
13738 && cp_parser_committed_to_tentative_parse (parser))
13739 cp_parser_skip_to_end_of_block_or_statement (parser);
13740 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13741 if (cp_parser_parse_definitely (parser))
13743 if (friend_p)
13744 *friend_p = cp_parser_friend_p (decl_specifiers);
13746 /* Otherwise, try a function-definition. */
13747 else
13748 decl = cp_parser_function_definition (parser, friend_p);
13750 return decl;
13753 /* Parse a cast-expression that is not the operand of a unary "&". */
13755 static tree
13756 cp_parser_simple_cast_expression (cp_parser *parser)
13758 return cp_parser_cast_expression (parser, /*address_p=*/false);
13761 /* Parse a functional cast to TYPE. Returns an expression
13762 representing the cast. */
13764 static tree
13765 cp_parser_functional_cast (cp_parser* parser, tree type)
13767 tree expression_list;
13769 expression_list
13770 = cp_parser_parenthesized_expression_list (parser, false,
13771 /*non_constant_p=*/NULL);
13773 return build_functional_cast (type, expression_list);
13776 /* MEMBER_FUNCTION is a member function, or a friend. If default
13777 arguments, or the body of the function have not yet been parsed,
13778 parse them now. */
13780 static void
13781 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
13783 cp_lexer *saved_lexer;
13785 /* If this member is a template, get the underlying
13786 FUNCTION_DECL. */
13787 if (DECL_FUNCTION_TEMPLATE_P (member_function))
13788 member_function = DECL_TEMPLATE_RESULT (member_function);
13790 /* There should not be any class definitions in progress at this
13791 point; the bodies of members are only parsed outside of all class
13792 definitions. */
13793 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
13794 /* While we're parsing the member functions we might encounter more
13795 classes. We want to handle them right away, but we don't want
13796 them getting mixed up with functions that are currently in the
13797 queue. */
13798 parser->unparsed_functions_queues
13799 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13801 /* Make sure that any template parameters are in scope. */
13802 maybe_begin_member_template_processing (member_function);
13804 /* If the body of the function has not yet been parsed, parse it
13805 now. */
13806 if (DECL_PENDING_INLINE_P (member_function))
13808 tree function_scope;
13809 cp_token_cache *tokens;
13811 /* The function is no longer pending; we are processing it. */
13812 tokens = DECL_PENDING_INLINE_INFO (member_function);
13813 DECL_PENDING_INLINE_INFO (member_function) = NULL;
13814 DECL_PENDING_INLINE_P (member_function) = 0;
13815 /* If this was an inline function in a local class, enter the scope
13816 of the containing function. */
13817 function_scope = decl_function_context (member_function);
13818 if (function_scope)
13819 push_function_context_to (function_scope);
13821 /* Save away the current lexer. */
13822 saved_lexer = parser->lexer;
13823 /* Make a new lexer to feed us the tokens saved for this function. */
13824 parser->lexer = cp_lexer_new_from_tokens (tokens);
13825 parser->lexer->next = saved_lexer;
13827 /* Set the current source position to be the location of the first
13828 token in the saved inline body. */
13829 cp_lexer_peek_token (parser->lexer);
13831 /* Let the front end know that we going to be defining this
13832 function. */
13833 start_function (NULL_TREE, member_function, NULL_TREE,
13834 SF_PRE_PARSED | SF_INCLASS_INLINE);
13836 /* Now, parse the body of the function. */
13837 cp_parser_function_definition_after_declarator (parser,
13838 /*inline_p=*/true);
13840 /* Leave the scope of the containing function. */
13841 if (function_scope)
13842 pop_function_context_from (function_scope);
13843 /* Restore the lexer. */
13844 parser->lexer = saved_lexer;
13847 /* Remove any template parameters from the symbol table. */
13848 maybe_end_member_template_processing ();
13850 /* Restore the queue. */
13851 parser->unparsed_functions_queues
13852 = TREE_CHAIN (parser->unparsed_functions_queues);
13855 /* If DECL contains any default args, remeber it on the unparsed
13856 functions queue. */
13858 static void
13859 cp_parser_save_default_args (cp_parser* parser, tree decl)
13861 tree probe;
13863 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
13864 probe;
13865 probe = TREE_CHAIN (probe))
13866 if (TREE_PURPOSE (probe))
13868 TREE_PURPOSE (parser->unparsed_functions_queues)
13869 = tree_cons (NULL_TREE, decl,
13870 TREE_PURPOSE (parser->unparsed_functions_queues));
13871 break;
13873 return;
13876 /* FN is a FUNCTION_DECL which may contains a parameter with an
13877 unparsed DEFAULT_ARG. Parse the default args now. */
13879 static void
13880 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
13882 cp_lexer *saved_lexer;
13883 cp_token_cache *tokens;
13884 bool saved_local_variables_forbidden_p;
13885 tree parameters;
13887 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
13888 parameters;
13889 parameters = TREE_CHAIN (parameters))
13891 if (!TREE_PURPOSE (parameters)
13892 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
13893 continue;
13895 /* Save away the current lexer. */
13896 saved_lexer = parser->lexer;
13897 /* Create a new one, using the tokens we have saved. */
13898 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
13899 parser->lexer = cp_lexer_new_from_tokens (tokens);
13901 /* Set the current source position to be the location of the
13902 first token in the default argument. */
13903 cp_lexer_peek_token (parser->lexer);
13905 /* Local variable names (and the `this' keyword) may not appear
13906 in a default argument. */
13907 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13908 parser->local_variables_forbidden_p = true;
13909 /* Parse the assignment-expression. */
13910 if (DECL_CONTEXT (fn))
13911 push_nested_class (DECL_CONTEXT (fn));
13912 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
13913 if (DECL_CONTEXT (fn))
13914 pop_nested_class ();
13916 /* Restore saved state. */
13917 parser->lexer = saved_lexer;
13918 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13922 /* Parse the operand of `sizeof' (or a similar operator). Returns
13923 either a TYPE or an expression, depending on the form of the
13924 input. The KEYWORD indicates which kind of expression we have
13925 encountered. */
13927 static tree
13928 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
13930 static const char *format;
13931 tree expr = NULL_TREE;
13932 const char *saved_message;
13933 bool saved_constant_expression_p;
13935 /* Initialize FORMAT the first time we get here. */
13936 if (!format)
13937 format = "types may not be defined in `%s' expressions";
13939 /* Types cannot be defined in a `sizeof' expression. Save away the
13940 old message. */
13941 saved_message = parser->type_definition_forbidden_message;
13942 /* And create the new one. */
13943 parser->type_definition_forbidden_message
13944 = xmalloc (strlen (format)
13945 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
13946 + 1 /* `\0' */);
13947 sprintf ((char *) parser->type_definition_forbidden_message,
13948 format, IDENTIFIER_POINTER (ridpointers[keyword]));
13950 /* The restrictions on constant-expressions do not apply inside
13951 sizeof expressions. */
13952 saved_constant_expression_p = parser->constant_expression_p;
13953 parser->constant_expression_p = false;
13955 /* Do not actually evaluate the expression. */
13956 ++skip_evaluation;
13957 /* If it's a `(', then we might be looking at the type-id
13958 construction. */
13959 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13961 tree type;
13963 /* We can't be sure yet whether we're looking at a type-id or an
13964 expression. */
13965 cp_parser_parse_tentatively (parser);
13966 /* Consume the `('. */
13967 cp_lexer_consume_token (parser->lexer);
13968 /* Parse the type-id. */
13969 type = cp_parser_type_id (parser);
13970 /* Now, look for the trailing `)'. */
13971 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13972 /* If all went well, then we're done. */
13973 if (cp_parser_parse_definitely (parser))
13975 /* Build a list of decl-specifiers; right now, we have only
13976 a single type-specifier. */
13977 type = build_tree_list (NULL_TREE,
13978 type);
13980 /* Call grokdeclarator to figure out what type this is. */
13981 expr = grokdeclarator (NULL_TREE,
13982 type,
13983 TYPENAME,
13984 /*initialized=*/0,
13985 /*attrlist=*/NULL);
13989 /* If the type-id production did not work out, then we must be
13990 looking at the unary-expression production. */
13991 if (!expr)
13992 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
13993 /* Go back to evaluating expressions. */
13994 --skip_evaluation;
13996 /* Free the message we created. */
13997 free ((char *) parser->type_definition_forbidden_message);
13998 /* And restore the old one. */
13999 parser->type_definition_forbidden_message = saved_message;
14000 parser->constant_expression_p = saved_constant_expression_p;
14002 return expr;
14005 /* If the current declaration has no declarator, return true. */
14007 static bool
14008 cp_parser_declares_only_class_p (cp_parser *parser)
14010 /* If the next token is a `;' or a `,' then there is no
14011 declarator. */
14012 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14013 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14016 /* Simplify EXPR if it is a non-dependent expression. Returns the
14017 (possibly simplified) expression. */
14019 static tree
14020 cp_parser_fold_non_dependent_expr (tree expr)
14022 /* If we're in a template, but EXPR isn't value dependent, simplify
14023 it. We're supposed to treat:
14025 template <typename T> void f(T[1 + 1]);
14026 template <typename T> void f(T[2]);
14028 as two declarations of the same function, for example. */
14029 if (processing_template_decl
14030 && !type_dependent_expression_p (expr)
14031 && !value_dependent_expression_p (expr))
14033 HOST_WIDE_INT saved_processing_template_decl;
14035 saved_processing_template_decl = processing_template_decl;
14036 processing_template_decl = 0;
14037 expr = tsubst_copy_and_build (expr,
14038 /*args=*/NULL_TREE,
14039 tf_error,
14040 /*in_decl=*/NULL_TREE,
14041 /*function_p=*/false);
14042 processing_template_decl = saved_processing_template_decl;
14044 return expr;
14047 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14048 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14050 static bool
14051 cp_parser_friend_p (tree decl_specifiers)
14053 while (decl_specifiers)
14055 /* See if this decl-specifier is `friend'. */
14056 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14057 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14058 return true;
14060 /* Go on to the next decl-specifier. */
14061 decl_specifiers = TREE_CHAIN (decl_specifiers);
14064 return false;
14067 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14068 issue an error message indicating that TOKEN_DESC was expected.
14070 Returns the token consumed, if the token had the appropriate type.
14071 Otherwise, returns NULL. */
14073 static cp_token *
14074 cp_parser_require (cp_parser* parser,
14075 enum cpp_ttype type,
14076 const char* token_desc)
14078 if (cp_lexer_next_token_is (parser->lexer, type))
14079 return cp_lexer_consume_token (parser->lexer);
14080 else
14082 /* Output the MESSAGE -- unless we're parsing tentatively. */
14083 if (!cp_parser_simulate_error (parser))
14084 error ("expected %s", token_desc);
14085 return NULL;
14089 /* Like cp_parser_require, except that tokens will be skipped until
14090 the desired token is found. An error message is still produced if
14091 the next token is not as expected. */
14093 static void
14094 cp_parser_skip_until_found (cp_parser* parser,
14095 enum cpp_ttype type,
14096 const char* token_desc)
14098 cp_token *token;
14099 unsigned nesting_depth = 0;
14101 if (cp_parser_require (parser, type, token_desc))
14102 return;
14104 /* Skip tokens until the desired token is found. */
14105 while (true)
14107 /* Peek at the next token. */
14108 token = cp_lexer_peek_token (parser->lexer);
14109 /* If we've reached the token we want, consume it and
14110 stop. */
14111 if (token->type == type && !nesting_depth)
14113 cp_lexer_consume_token (parser->lexer);
14114 return;
14116 /* If we've run out of tokens, stop. */
14117 if (token->type == CPP_EOF)
14118 return;
14119 if (token->type == CPP_OPEN_BRACE
14120 || token->type == CPP_OPEN_PAREN
14121 || token->type == CPP_OPEN_SQUARE)
14122 ++nesting_depth;
14123 else if (token->type == CPP_CLOSE_BRACE
14124 || token->type == CPP_CLOSE_PAREN
14125 || token->type == CPP_CLOSE_SQUARE)
14127 if (nesting_depth-- == 0)
14128 return;
14130 /* Consume this token. */
14131 cp_lexer_consume_token (parser->lexer);
14135 /* If the next token is the indicated keyword, consume it. Otherwise,
14136 issue an error message indicating that TOKEN_DESC was expected.
14138 Returns the token consumed, if the token had the appropriate type.
14139 Otherwise, returns NULL. */
14141 static cp_token *
14142 cp_parser_require_keyword (cp_parser* parser,
14143 enum rid keyword,
14144 const char* token_desc)
14146 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14148 if (token && token->keyword != keyword)
14150 dyn_string_t error_msg;
14152 /* Format the error message. */
14153 error_msg = dyn_string_new (0);
14154 dyn_string_append_cstr (error_msg, "expected ");
14155 dyn_string_append_cstr (error_msg, token_desc);
14156 cp_parser_error (parser, error_msg->s);
14157 dyn_string_delete (error_msg);
14158 return NULL;
14161 return token;
14164 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14165 function-definition. */
14167 static bool
14168 cp_parser_token_starts_function_definition_p (cp_token* token)
14170 return (/* An ordinary function-body begins with an `{'. */
14171 token->type == CPP_OPEN_BRACE
14172 /* A ctor-initializer begins with a `:'. */
14173 || token->type == CPP_COLON
14174 /* A function-try-block begins with `try'. */
14175 || token->keyword == RID_TRY
14176 /* The named return value extension begins with `return'. */
14177 || token->keyword == RID_RETURN);
14180 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14181 definition. */
14183 static bool
14184 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14186 cp_token *token;
14188 token = cp_lexer_peek_token (parser->lexer);
14189 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14192 /* Returns TRUE iff the next token is the "," or ">" ending a
14193 template-argument. */
14195 static bool
14196 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14198 cp_token *token;
14200 token = cp_lexer_peek_token (parser->lexer);
14201 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14204 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14205 or none_type otherwise. */
14207 static enum tag_types
14208 cp_parser_token_is_class_key (cp_token* token)
14210 switch (token->keyword)
14212 case RID_CLASS:
14213 return class_type;
14214 case RID_STRUCT:
14215 return record_type;
14216 case RID_UNION:
14217 return union_type;
14219 default:
14220 return none_type;
14224 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14226 static void
14227 cp_parser_check_class_key (enum tag_types class_key, tree type)
14229 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14230 pedwarn ("`%s' tag used in naming `%#T'",
14231 class_key == union_type ? "union"
14232 : class_key == record_type ? "struct" : "class",
14233 type);
14236 /* Look for the `template' keyword, as a syntactic disambiguator.
14237 Return TRUE iff it is present, in which case it will be
14238 consumed. */
14240 static bool
14241 cp_parser_optional_template_keyword (cp_parser *parser)
14243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14245 /* The `template' keyword can only be used within templates;
14246 outside templates the parser can always figure out what is a
14247 template and what is not. */
14248 if (!processing_template_decl)
14250 error ("`template' (as a disambiguator) is only allowed "
14251 "within templates");
14252 /* If this part of the token stream is rescanned, the same
14253 error message would be generated. So, we purge the token
14254 from the stream. */
14255 cp_lexer_purge_token (parser->lexer);
14256 return false;
14258 else
14260 /* Consume the `template' keyword. */
14261 cp_lexer_consume_token (parser->lexer);
14262 return true;
14266 return false;
14269 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14270 set PARSER->SCOPE, and perform other related actions. */
14272 static void
14273 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14275 tree value;
14276 tree check;
14278 /* Get the stored value. */
14279 value = cp_lexer_consume_token (parser->lexer)->value;
14280 /* Perform any access checks that were deferred. */
14281 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14282 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14283 /* Set the scope from the stored value. */
14284 parser->scope = TREE_VALUE (value);
14285 parser->qualifying_scope = TREE_TYPE (value);
14286 parser->object_scope = NULL_TREE;
14289 /* Add tokens to CACHE until an non-nested END token appears. */
14291 static void
14292 cp_parser_cache_group (cp_parser *parser,
14293 cp_token_cache *cache,
14294 enum cpp_ttype end,
14295 unsigned depth)
14297 while (true)
14299 cp_token *token;
14301 /* Abort a parenthesized expression if we encounter a brace. */
14302 if ((end == CPP_CLOSE_PAREN || depth == 0)
14303 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14304 return;
14305 /* Consume the next token. */
14306 token = cp_lexer_consume_token (parser->lexer);
14307 /* If we've reached the end of the file, stop. */
14308 if (token->type == CPP_EOF)
14309 return;
14310 /* Add this token to the tokens we are saving. */
14311 cp_token_cache_push_token (cache, token);
14312 /* See if it starts a new group. */
14313 if (token->type == CPP_OPEN_BRACE)
14315 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14316 if (depth == 0)
14317 return;
14319 else if (token->type == CPP_OPEN_PAREN)
14320 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14321 else if (token->type == end)
14322 return;
14326 /* Begin parsing tentatively. We always save tokens while parsing
14327 tentatively so that if the tentative parsing fails we can restore the
14328 tokens. */
14330 static void
14331 cp_parser_parse_tentatively (cp_parser* parser)
14333 /* Enter a new parsing context. */
14334 parser->context = cp_parser_context_new (parser->context);
14335 /* Begin saving tokens. */
14336 cp_lexer_save_tokens (parser->lexer);
14337 /* In order to avoid repetitive access control error messages,
14338 access checks are queued up until we are no longer parsing
14339 tentatively. */
14340 push_deferring_access_checks (dk_deferred);
14343 /* Commit to the currently active tentative parse. */
14345 static void
14346 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14348 cp_parser_context *context;
14349 cp_lexer *lexer;
14351 /* Mark all of the levels as committed. */
14352 lexer = parser->lexer;
14353 for (context = parser->context; context->next; context = context->next)
14355 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14356 break;
14357 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14358 while (!cp_lexer_saving_tokens (lexer))
14359 lexer = lexer->next;
14360 cp_lexer_commit_tokens (lexer);
14364 /* Abort the currently active tentative parse. All consumed tokens
14365 will be rolled back, and no diagnostics will be issued. */
14367 static void
14368 cp_parser_abort_tentative_parse (cp_parser* parser)
14370 cp_parser_simulate_error (parser);
14371 /* Now, pretend that we want to see if the construct was
14372 successfully parsed. */
14373 cp_parser_parse_definitely (parser);
14376 /* Stop parsing tentatively. If a parse error has occurred, restore the
14377 token stream. Otherwise, commit to the tokens we have consumed.
14378 Returns true if no error occurred; false otherwise. */
14380 static bool
14381 cp_parser_parse_definitely (cp_parser* parser)
14383 bool error_occurred;
14384 cp_parser_context *context;
14386 /* Remember whether or not an error occurred, since we are about to
14387 destroy that information. */
14388 error_occurred = cp_parser_error_occurred (parser);
14389 /* Remove the topmost context from the stack. */
14390 context = parser->context;
14391 parser->context = context->next;
14392 /* If no parse errors occurred, commit to the tentative parse. */
14393 if (!error_occurred)
14395 /* Commit to the tokens read tentatively, unless that was
14396 already done. */
14397 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14398 cp_lexer_commit_tokens (parser->lexer);
14400 pop_to_parent_deferring_access_checks ();
14402 /* Otherwise, if errors occurred, roll back our state so that things
14403 are just as they were before we began the tentative parse. */
14404 else
14406 cp_lexer_rollback_tokens (parser->lexer);
14407 pop_deferring_access_checks ();
14409 /* Add the context to the front of the free list. */
14410 context->next = cp_parser_context_free_list;
14411 cp_parser_context_free_list = context;
14413 return !error_occurred;
14416 /* Returns true if we are parsing tentatively -- but have decided that
14417 we will stick with this tentative parse, even if errors occur. */
14419 static bool
14420 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14422 return (cp_parser_parsing_tentatively (parser)
14423 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14426 /* Returns nonzero iff an error has occurred during the most recent
14427 tentative parse. */
14429 static bool
14430 cp_parser_error_occurred (cp_parser* parser)
14432 return (cp_parser_parsing_tentatively (parser)
14433 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14436 /* Returns nonzero if GNU extensions are allowed. */
14438 static bool
14439 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14441 return parser->allow_gnu_extensions_p;
14446 /* The parser. */
14448 static GTY (()) cp_parser *the_parser;
14450 /* External interface. */
14452 /* Parse one entire translation unit. */
14454 void
14455 c_parse_file (void)
14457 bool error_occurred;
14459 the_parser = cp_parser_new ();
14460 push_deferring_access_checks (flag_access_control
14461 ? dk_no_deferred : dk_no_check);
14462 error_occurred = cp_parser_translation_unit (the_parser);
14463 the_parser = NULL;
14466 /* Clean up after parsing the entire translation unit. */
14468 void
14469 free_parser_stacks (void)
14471 /* Nothing to do. */
14474 /* This variable must be provided by every front end. */
14476 int yydebug;
14478 #include "gt-cp-parser.h"