* name-lookup.h (get_global_value_if_present): New function.
[official-gcc.git] / gcc / cp / parser.c
blobec5912f88d8460a21a4e9eba28a8cc506c4d9ade
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 void cp_parser_check_access_in_redeclaration
1655 (tree type);
1656 static bool cp_parser_optional_template_keyword
1657 (cp_parser *);
1658 static void cp_parser_pre_parsed_nested_name_specifier
1659 (cp_parser *);
1660 static void cp_parser_cache_group
1661 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1662 static void cp_parser_parse_tentatively
1663 (cp_parser *);
1664 static void cp_parser_commit_to_tentative_parse
1665 (cp_parser *);
1666 static void cp_parser_abort_tentative_parse
1667 (cp_parser *);
1668 static bool cp_parser_parse_definitely
1669 (cp_parser *);
1670 static inline bool cp_parser_parsing_tentatively
1671 (cp_parser *);
1672 static bool cp_parser_committed_to_tentative_parse
1673 (cp_parser *);
1674 static void cp_parser_error
1675 (cp_parser *, const char *);
1676 static bool cp_parser_simulate_error
1677 (cp_parser *);
1678 static void cp_parser_check_type_definition
1679 (cp_parser *);
1680 static void cp_parser_check_for_definition_in_return_type
1681 (tree, int);
1682 static tree cp_parser_non_constant_expression
1683 (const char *);
1684 static bool cp_parser_diagnose_invalid_type_name
1685 (cp_parser *);
1686 static int cp_parser_skip_to_closing_parenthesis
1687 (cp_parser *, bool, bool);
1688 static void cp_parser_skip_to_end_of_statement
1689 (cp_parser *);
1690 static void cp_parser_consume_semicolon_at_end_of_statement
1691 (cp_parser *);
1692 static void cp_parser_skip_to_end_of_block_or_statement
1693 (cp_parser *);
1694 static void cp_parser_skip_to_closing_brace
1695 (cp_parser *);
1696 static void cp_parser_skip_until_found
1697 (cp_parser *, enum cpp_ttype, const char *);
1698 static bool cp_parser_error_occurred
1699 (cp_parser *);
1700 static bool cp_parser_allow_gnu_extensions_p
1701 (cp_parser *);
1702 static bool cp_parser_is_string_literal
1703 (cp_token *);
1704 static bool cp_parser_is_keyword
1705 (cp_token *, enum rid);
1707 /* Returns nonzero if we are parsing tentatively. */
1709 static inline bool
1710 cp_parser_parsing_tentatively (cp_parser* parser)
1712 return parser->context->next != NULL;
1715 /* Returns nonzero if TOKEN is a string literal. */
1717 static bool
1718 cp_parser_is_string_literal (cp_token* token)
1720 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1723 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1725 static bool
1726 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1728 return token->keyword == keyword;
1731 /* Issue the indicated error MESSAGE. */
1733 static void
1734 cp_parser_error (cp_parser* parser, const char* message)
1736 /* Output the MESSAGE -- unless we're parsing tentatively. */
1737 if (!cp_parser_simulate_error (parser))
1738 error (message);
1741 /* If we are parsing tentatively, remember that an error has occurred
1742 during this tentative parse. Returns true if the error was
1743 simulated; false if a messgae should be issued by the caller. */
1745 static bool
1746 cp_parser_simulate_error (cp_parser* parser)
1748 if (cp_parser_parsing_tentatively (parser)
1749 && !cp_parser_committed_to_tentative_parse (parser))
1751 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1752 return true;
1754 return false;
1757 /* This function is called when a type is defined. If type
1758 definitions are forbidden at this point, an error message is
1759 issued. */
1761 static void
1762 cp_parser_check_type_definition (cp_parser* parser)
1764 /* If types are forbidden here, issue a message. */
1765 if (parser->type_definition_forbidden_message)
1766 /* Use `%s' to print the string in case there are any escape
1767 characters in the message. */
1768 error ("%s", parser->type_definition_forbidden_message);
1771 /* This function is called when a declaration is parsed. If
1772 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1773 indicates that a type was defined in the decl-specifiers for DECL,
1774 then an error is issued. */
1776 static void
1777 cp_parser_check_for_definition_in_return_type (tree declarator,
1778 int declares_class_or_enum)
1780 /* [dcl.fct] forbids type definitions in return types.
1781 Unfortunately, it's not easy to know whether or not we are
1782 processing a return type until after the fact. */
1783 while (declarator
1784 && (TREE_CODE (declarator) == INDIRECT_REF
1785 || TREE_CODE (declarator) == ADDR_EXPR))
1786 declarator = TREE_OPERAND (declarator, 0);
1787 if (declarator
1788 && TREE_CODE (declarator) == CALL_EXPR
1789 && declares_class_or_enum & 2)
1790 error ("new types may not be defined in a return type");
1793 /* Issue an eror message about the fact that THING appeared in a
1794 constant-expression. Returns ERROR_MARK_NODE. */
1796 static tree
1797 cp_parser_non_constant_expression (const char *thing)
1799 error ("%s cannot appear in a constant-expression", thing);
1800 return error_mark_node;
1803 /* Check for a common situation where a type-name should be present,
1804 but is not, and issue a sensible error message. Returns true if an
1805 invalid type-name was detected. */
1807 static bool
1808 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1810 /* If the next two tokens are both identifiers, the code is
1811 erroneous. The usual cause of this situation is code like:
1813 T t;
1815 where "T" should name a type -- but does not. */
1816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1817 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1819 tree name;
1821 /* If parsing tentatively, we should commit; we really are
1822 looking at a declaration. */
1823 /* Consume the first identifier. */
1824 name = cp_lexer_consume_token (parser->lexer)->value;
1825 /* Issue an error message. */
1826 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1827 /* If we're in a template class, it's possible that the user was
1828 referring to a type from a base class. For example:
1830 template <typename T> struct A { typedef T X; };
1831 template <typename T> struct B : public A<T> { X x; };
1833 The user should have said "typename A<T>::X". */
1834 if (processing_template_decl && current_class_type)
1836 tree b;
1838 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1840 b = TREE_CHAIN (b))
1842 tree base_type = BINFO_TYPE (b);
1843 if (CLASS_TYPE_P (base_type)
1844 && dependent_type_p (base_type))
1846 tree field;
1847 /* Go from a particular instantiation of the
1848 template (which will have an empty TYPE_FIELDs),
1849 to the main version. */
1850 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1851 for (field = TYPE_FIELDS (base_type);
1852 field;
1853 field = TREE_CHAIN (field))
1854 if (TREE_CODE (field) == TYPE_DECL
1855 && DECL_NAME (field) == name)
1857 error ("(perhaps `typename %T::%s' was intended)",
1858 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1859 break;
1861 if (field)
1862 break;
1866 /* Skip to the end of the declaration; there's no point in
1867 trying to process it. */
1868 cp_parser_skip_to_end_of_statement (parser);
1870 return true;
1873 return false;
1876 /* Consume tokens up to, and including, the next non-nested closing `)'.
1877 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
1878 are doing error recovery. Returns -1 if OR_COMMA is true and we
1879 found an unnested comma. */
1881 static int
1882 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
1883 bool recovering, bool or_comma)
1885 unsigned paren_depth = 0;
1886 unsigned brace_depth = 0;
1888 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
1889 && !cp_parser_committed_to_tentative_parse (parser))
1890 return 0;
1892 while (true)
1894 cp_token *token;
1896 /* If we've run out of tokens, then there is no closing `)'. */
1897 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1898 return 0;
1900 if (recovering)
1902 token = cp_lexer_peek_token (parser->lexer);
1904 /* This matches the processing in skip_to_end_of_statement */
1905 if (token->type == CPP_SEMICOLON && !brace_depth)
1906 return 0;
1907 if (token->type == CPP_OPEN_BRACE)
1908 ++brace_depth;
1909 if (token->type == CPP_CLOSE_BRACE)
1911 if (!brace_depth--)
1912 return 0;
1914 if (or_comma && token->type == CPP_COMMA
1915 && !brace_depth && !paren_depth)
1916 return -1;
1919 /* Consume the token. */
1920 token = cp_lexer_consume_token (parser->lexer);
1922 if (!brace_depth)
1924 /* If it is an `(', we have entered another level of nesting. */
1925 if (token->type == CPP_OPEN_PAREN)
1926 ++paren_depth;
1927 /* If it is a `)', then we might be done. */
1928 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
1929 return 1;
1934 /* Consume tokens until we reach the end of the current statement.
1935 Normally, that will be just before consuming a `;'. However, if a
1936 non-nested `}' comes first, then we stop before consuming that. */
1938 static void
1939 cp_parser_skip_to_end_of_statement (cp_parser* parser)
1941 unsigned nesting_depth = 0;
1943 while (true)
1945 cp_token *token;
1947 /* Peek at the next token. */
1948 token = cp_lexer_peek_token (parser->lexer);
1949 /* If we've run out of tokens, stop. */
1950 if (token->type == CPP_EOF)
1951 break;
1952 /* If the next token is a `;', we have reached the end of the
1953 statement. */
1954 if (token->type == CPP_SEMICOLON && !nesting_depth)
1955 break;
1956 /* If the next token is a non-nested `}', then we have reached
1957 the end of the current block. */
1958 if (token->type == CPP_CLOSE_BRACE)
1960 /* If this is a non-nested `}', stop before consuming it.
1961 That way, when confronted with something like:
1963 { 3 + }
1965 we stop before consuming the closing `}', even though we
1966 have not yet reached a `;'. */
1967 if (nesting_depth == 0)
1968 break;
1969 /* If it is the closing `}' for a block that we have
1970 scanned, stop -- but only after consuming the token.
1971 That way given:
1973 void f g () { ... }
1974 typedef int I;
1976 we will stop after the body of the erroneously declared
1977 function, but before consuming the following `typedef'
1978 declaration. */
1979 if (--nesting_depth == 0)
1981 cp_lexer_consume_token (parser->lexer);
1982 break;
1985 /* If it the next token is a `{', then we are entering a new
1986 block. Consume the entire block. */
1987 else if (token->type == CPP_OPEN_BRACE)
1988 ++nesting_depth;
1989 /* Consume the token. */
1990 cp_lexer_consume_token (parser->lexer);
1994 /* This function is called at the end of a statement or declaration.
1995 If the next token is a semicolon, it is consumed; otherwise, error
1996 recovery is attempted. */
1998 static void
1999 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2001 /* Look for the trailing `;'. */
2002 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2004 /* If there is additional (erroneous) input, skip to the end of
2005 the statement. */
2006 cp_parser_skip_to_end_of_statement (parser);
2007 /* If the next token is now a `;', consume it. */
2008 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2009 cp_lexer_consume_token (parser->lexer);
2013 /* Skip tokens until we have consumed an entire block, or until we
2014 have consumed a non-nested `;'. */
2016 static void
2017 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2019 unsigned nesting_depth = 0;
2021 while (true)
2023 cp_token *token;
2025 /* Peek at the next token. */
2026 token = cp_lexer_peek_token (parser->lexer);
2027 /* If we've run out of tokens, stop. */
2028 if (token->type == CPP_EOF)
2029 break;
2030 /* If the next token is a `;', we have reached the end of the
2031 statement. */
2032 if (token->type == CPP_SEMICOLON && !nesting_depth)
2034 /* Consume the `;'. */
2035 cp_lexer_consume_token (parser->lexer);
2036 break;
2038 /* Consume the token. */
2039 token = cp_lexer_consume_token (parser->lexer);
2040 /* If the next token is a non-nested `}', then we have reached
2041 the end of the current block. */
2042 if (token->type == CPP_CLOSE_BRACE
2043 && (nesting_depth == 0 || --nesting_depth == 0))
2044 break;
2045 /* If it the next token is a `{', then we are entering a new
2046 block. Consume the entire block. */
2047 if (token->type == CPP_OPEN_BRACE)
2048 ++nesting_depth;
2052 /* Skip tokens until a non-nested closing curly brace is the next
2053 token. */
2055 static void
2056 cp_parser_skip_to_closing_brace (cp_parser *parser)
2058 unsigned nesting_depth = 0;
2060 while (true)
2062 cp_token *token;
2064 /* Peek at the next token. */
2065 token = cp_lexer_peek_token (parser->lexer);
2066 /* If we've run out of tokens, stop. */
2067 if (token->type == CPP_EOF)
2068 break;
2069 /* If the next token is a non-nested `}', then we have reached
2070 the end of the current block. */
2071 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2072 break;
2073 /* If it the next token is a `{', then we are entering a new
2074 block. Consume the entire block. */
2075 else if (token->type == CPP_OPEN_BRACE)
2076 ++nesting_depth;
2077 /* Consume the token. */
2078 cp_lexer_consume_token (parser->lexer);
2082 /* Create a new C++ parser. */
2084 static cp_parser *
2085 cp_parser_new (void)
2087 cp_parser *parser;
2088 cp_lexer *lexer;
2090 /* cp_lexer_new_main is called before calling ggc_alloc because
2091 cp_lexer_new_main might load a PCH file. */
2092 lexer = cp_lexer_new_main ();
2094 parser = ggc_alloc_cleared (sizeof (cp_parser));
2095 parser->lexer = lexer;
2096 parser->context = cp_parser_context_new (NULL);
2098 /* For now, we always accept GNU extensions. */
2099 parser->allow_gnu_extensions_p = 1;
2101 /* The `>' token is a greater-than operator, not the end of a
2102 template-id. */
2103 parser->greater_than_is_operator_p = true;
2105 parser->default_arg_ok_p = true;
2107 /* We are not parsing a constant-expression. */
2108 parser->constant_expression_p = false;
2109 parser->allow_non_constant_expression_p = false;
2110 parser->non_constant_expression_p = false;
2112 /* Local variable names are not forbidden. */
2113 parser->local_variables_forbidden_p = false;
2115 /* We are not processing an `extern "C"' declaration. */
2116 parser->in_unbraced_linkage_specification_p = false;
2118 /* We are not processing a declarator. */
2119 parser->in_declarator_p = false;
2121 /* The unparsed function queue is empty. */
2122 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2124 /* There are no classes being defined. */
2125 parser->num_classes_being_defined = 0;
2127 /* No template parameters apply. */
2128 parser->num_template_parameter_lists = 0;
2130 return parser;
2133 /* Lexical conventions [gram.lex] */
2135 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2136 identifier. */
2138 static tree
2139 cp_parser_identifier (cp_parser* parser)
2141 cp_token *token;
2143 /* Look for the identifier. */
2144 token = cp_parser_require (parser, CPP_NAME, "identifier");
2145 /* Return the value. */
2146 return token ? token->value : error_mark_node;
2149 /* Basic concepts [gram.basic] */
2151 /* Parse a translation-unit.
2153 translation-unit:
2154 declaration-seq [opt]
2156 Returns TRUE if all went well. */
2158 static bool
2159 cp_parser_translation_unit (cp_parser* parser)
2161 while (true)
2163 cp_parser_declaration_seq_opt (parser);
2165 /* If there are no tokens left then all went well. */
2166 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2167 break;
2169 /* Otherwise, issue an error message. */
2170 cp_parser_error (parser, "expected declaration");
2171 return false;
2174 /* Consume the EOF token. */
2175 cp_parser_require (parser, CPP_EOF, "end-of-file");
2177 /* Finish up. */
2178 finish_translation_unit ();
2180 /* All went well. */
2181 return true;
2184 /* Expressions [gram.expr] */
2186 /* Parse a primary-expression.
2188 primary-expression:
2189 literal
2190 this
2191 ( expression )
2192 id-expression
2194 GNU Extensions:
2196 primary-expression:
2197 ( compound-statement )
2198 __builtin_va_arg ( assignment-expression , type-id )
2200 literal:
2201 __null
2203 Returns a representation of the expression.
2205 *IDK indicates what kind of id-expression (if any) was present.
2207 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2208 used as the operand of a pointer-to-member. In that case,
2209 *QUALIFYING_CLASS gives the class that is used as the qualifying
2210 class in the pointer-to-member. */
2212 static tree
2213 cp_parser_primary_expression (cp_parser *parser,
2214 cp_id_kind *idk,
2215 tree *qualifying_class)
2217 cp_token *token;
2219 /* Assume the primary expression is not an id-expression. */
2220 *idk = CP_ID_KIND_NONE;
2221 /* And that it cannot be used as pointer-to-member. */
2222 *qualifying_class = NULL_TREE;
2224 /* Peek at the next token. */
2225 token = cp_lexer_peek_token (parser->lexer);
2226 switch (token->type)
2228 /* literal:
2229 integer-literal
2230 character-literal
2231 floating-literal
2232 string-literal
2233 boolean-literal */
2234 case CPP_CHAR:
2235 case CPP_WCHAR:
2236 case CPP_STRING:
2237 case CPP_WSTRING:
2238 case CPP_NUMBER:
2239 token = cp_lexer_consume_token (parser->lexer);
2240 return token->value;
2242 case CPP_OPEN_PAREN:
2244 tree expr;
2245 bool saved_greater_than_is_operator_p;
2247 /* Consume the `('. */
2248 cp_lexer_consume_token (parser->lexer);
2249 /* Within a parenthesized expression, a `>' token is always
2250 the greater-than operator. */
2251 saved_greater_than_is_operator_p
2252 = parser->greater_than_is_operator_p;
2253 parser->greater_than_is_operator_p = true;
2254 /* If we see `( { ' then we are looking at the beginning of
2255 a GNU statement-expression. */
2256 if (cp_parser_allow_gnu_extensions_p (parser)
2257 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2259 /* Statement-expressions are not allowed by the standard. */
2260 if (pedantic)
2261 pedwarn ("ISO C++ forbids braced-groups within expressions");
2263 /* And they're not allowed outside of a function-body; you
2264 cannot, for example, write:
2266 int i = ({ int j = 3; j + 1; });
2268 at class or namespace scope. */
2269 if (!at_function_scope_p ())
2270 error ("statement-expressions are allowed only inside functions");
2271 /* Start the statement-expression. */
2272 expr = begin_stmt_expr ();
2273 /* Parse the compound-statement. */
2274 cp_parser_compound_statement (parser, true);
2275 /* Finish up. */
2276 expr = finish_stmt_expr (expr, false);
2278 else
2280 /* Parse the parenthesized expression. */
2281 expr = cp_parser_expression (parser);
2282 /* Let the front end know that this expression was
2283 enclosed in parentheses. This matters in case, for
2284 example, the expression is of the form `A::B', since
2285 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2286 not. */
2287 finish_parenthesized_expr (expr);
2289 /* The `>' token might be the end of a template-id or
2290 template-parameter-list now. */
2291 parser->greater_than_is_operator_p
2292 = saved_greater_than_is_operator_p;
2293 /* Consume the `)'. */
2294 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2295 cp_parser_skip_to_end_of_statement (parser);
2297 return expr;
2300 case CPP_KEYWORD:
2301 switch (token->keyword)
2303 /* These two are the boolean literals. */
2304 case RID_TRUE:
2305 cp_lexer_consume_token (parser->lexer);
2306 return boolean_true_node;
2307 case RID_FALSE:
2308 cp_lexer_consume_token (parser->lexer);
2309 return boolean_false_node;
2311 /* The `__null' literal. */
2312 case RID_NULL:
2313 cp_lexer_consume_token (parser->lexer);
2314 return null_node;
2316 /* Recognize the `this' keyword. */
2317 case RID_THIS:
2318 cp_lexer_consume_token (parser->lexer);
2319 if (parser->local_variables_forbidden_p)
2321 error ("`this' may not be used in this context");
2322 return error_mark_node;
2324 /* Pointers cannot appear in constant-expressions. */
2325 if (parser->constant_expression_p)
2327 if (!parser->allow_non_constant_expression_p)
2328 return cp_parser_non_constant_expression ("`this'");
2329 parser->non_constant_expression_p = true;
2331 return finish_this_expr ();
2333 /* The `operator' keyword can be the beginning of an
2334 id-expression. */
2335 case RID_OPERATOR:
2336 goto id_expression;
2338 case RID_FUNCTION_NAME:
2339 case RID_PRETTY_FUNCTION_NAME:
2340 case RID_C99_FUNCTION_NAME:
2341 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2342 __func__ are the names of variables -- but they are
2343 treated specially. Therefore, they are handled here,
2344 rather than relying on the generic id-expression logic
2345 below. Grammatically, these names are id-expressions.
2347 Consume the token. */
2348 token = cp_lexer_consume_token (parser->lexer);
2349 /* Look up the name. */
2350 return finish_fname (token->value);
2352 case RID_VA_ARG:
2354 tree expression;
2355 tree type;
2357 /* The `__builtin_va_arg' construct is used to handle
2358 `va_arg'. Consume the `__builtin_va_arg' token. */
2359 cp_lexer_consume_token (parser->lexer);
2360 /* Look for the opening `('. */
2361 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2362 /* Now, parse the assignment-expression. */
2363 expression = cp_parser_assignment_expression (parser);
2364 /* Look for the `,'. */
2365 cp_parser_require (parser, CPP_COMMA, "`,'");
2366 /* Parse the type-id. */
2367 type = cp_parser_type_id (parser);
2368 /* Look for the closing `)'. */
2369 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2370 /* Using `va_arg' in a constant-expression is not
2371 allowed. */
2372 if (parser->constant_expression_p)
2374 if (!parser->allow_non_constant_expression_p)
2375 return cp_parser_non_constant_expression ("`va_arg'");
2376 parser->non_constant_expression_p = true;
2378 return build_x_va_arg (expression, type);
2381 default:
2382 cp_parser_error (parser, "expected primary-expression");
2383 return error_mark_node;
2386 /* An id-expression can start with either an identifier, a
2387 `::' as the beginning of a qualified-id, or the "operator"
2388 keyword. */
2389 case CPP_NAME:
2390 case CPP_SCOPE:
2391 case CPP_TEMPLATE_ID:
2392 case CPP_NESTED_NAME_SPECIFIER:
2394 tree id_expression;
2395 tree decl;
2396 const char *error_msg;
2398 id_expression:
2399 /* Parse the id-expression. */
2400 id_expression
2401 = cp_parser_id_expression (parser,
2402 /*template_keyword_p=*/false,
2403 /*check_dependency_p=*/true,
2404 /*template_p=*/NULL,
2405 /*declarator_p=*/false);
2406 if (id_expression == error_mark_node)
2407 return error_mark_node;
2408 /* If we have a template-id, then no further lookup is
2409 required. If the template-id was for a template-class, we
2410 will sometimes have a TYPE_DECL at this point. */
2411 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2412 || TREE_CODE (id_expression) == TYPE_DECL)
2413 decl = id_expression;
2414 /* Look up the name. */
2415 else
2417 decl = cp_parser_lookup_name_simple (parser, id_expression);
2418 /* If name lookup gives us a SCOPE_REF, then the
2419 qualifying scope was dependent. Just propagate the
2420 name. */
2421 if (TREE_CODE (decl) == SCOPE_REF)
2423 if (TYPE_P (TREE_OPERAND (decl, 0)))
2424 *qualifying_class = TREE_OPERAND (decl, 0);
2425 return decl;
2427 /* Check to see if DECL is a local variable in a context
2428 where that is forbidden. */
2429 if (parser->local_variables_forbidden_p
2430 && local_variable_p (decl))
2432 /* It might be that we only found DECL because we are
2433 trying to be generous with pre-ISO scoping rules.
2434 For example, consider:
2436 int i;
2437 void g() {
2438 for (int i = 0; i < 10; ++i) {}
2439 extern void f(int j = i);
2442 Here, name look up will originally find the out
2443 of scope `i'. We need to issue a warning message,
2444 but then use the global `i'. */
2445 decl = check_for_out_of_scope_variable (decl);
2446 if (local_variable_p (decl))
2448 error ("local variable `%D' may not appear in this context",
2449 decl);
2450 return error_mark_node;
2455 decl = finish_id_expression (id_expression, decl, parser->scope,
2456 idk, qualifying_class,
2457 parser->constant_expression_p,
2458 parser->allow_non_constant_expression_p,
2459 &parser->non_constant_expression_p,
2460 &error_msg);
2461 if (error_msg)
2462 cp_parser_error (parser, error_msg);
2463 return decl;
2466 /* Anything else is an error. */
2467 default:
2468 cp_parser_error (parser, "expected primary-expression");
2469 return error_mark_node;
2473 /* Parse an id-expression.
2475 id-expression:
2476 unqualified-id
2477 qualified-id
2479 qualified-id:
2480 :: [opt] nested-name-specifier template [opt] unqualified-id
2481 :: identifier
2482 :: operator-function-id
2483 :: template-id
2485 Return a representation of the unqualified portion of the
2486 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2487 a `::' or nested-name-specifier.
2489 Often, if the id-expression was a qualified-id, the caller will
2490 want to make a SCOPE_REF to represent the qualified-id. This
2491 function does not do this in order to avoid wastefully creating
2492 SCOPE_REFs when they are not required.
2494 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2495 `template' keyword.
2497 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2498 uninstantiated templates.
2500 If *TEMPLATE_P is non-NULL, it is set to true iff the
2501 `template' keyword is used to explicitly indicate that the entity
2502 named is a template.
2504 If DECLARATOR_P is true, the id-expression is appearing as part of
2505 a declarator, rather than as part of an exprsesion. */
2507 static tree
2508 cp_parser_id_expression (cp_parser *parser,
2509 bool template_keyword_p,
2510 bool check_dependency_p,
2511 bool *template_p,
2512 bool declarator_p)
2514 bool global_scope_p;
2515 bool nested_name_specifier_p;
2517 /* Assume the `template' keyword was not used. */
2518 if (template_p)
2519 *template_p = false;
2521 /* Look for the optional `::' operator. */
2522 global_scope_p
2523 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2524 != NULL_TREE);
2525 /* Look for the optional nested-name-specifier. */
2526 nested_name_specifier_p
2527 = (cp_parser_nested_name_specifier_opt (parser,
2528 /*typename_keyword_p=*/false,
2529 check_dependency_p,
2530 /*type_p=*/false)
2531 != NULL_TREE);
2532 /* If there is a nested-name-specifier, then we are looking at
2533 the first qualified-id production. */
2534 if (nested_name_specifier_p)
2536 tree saved_scope;
2537 tree saved_object_scope;
2538 tree saved_qualifying_scope;
2539 tree unqualified_id;
2540 bool is_template;
2542 /* See if the next token is the `template' keyword. */
2543 if (!template_p)
2544 template_p = &is_template;
2545 *template_p = cp_parser_optional_template_keyword (parser);
2546 /* Name lookup we do during the processing of the
2547 unqualified-id might obliterate SCOPE. */
2548 saved_scope = parser->scope;
2549 saved_object_scope = parser->object_scope;
2550 saved_qualifying_scope = parser->qualifying_scope;
2551 /* Process the final unqualified-id. */
2552 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2553 check_dependency_p,
2554 declarator_p);
2555 /* Restore the SAVED_SCOPE for our caller. */
2556 parser->scope = saved_scope;
2557 parser->object_scope = saved_object_scope;
2558 parser->qualifying_scope = saved_qualifying_scope;
2560 return unqualified_id;
2562 /* Otherwise, if we are in global scope, then we are looking at one
2563 of the other qualified-id productions. */
2564 else if (global_scope_p)
2566 cp_token *token;
2567 tree id;
2569 /* Peek at the next token. */
2570 token = cp_lexer_peek_token (parser->lexer);
2572 /* If it's an identifier, and the next token is not a "<", then
2573 we can avoid the template-id case. This is an optimization
2574 for this common case. */
2575 if (token->type == CPP_NAME
2576 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2577 return cp_parser_identifier (parser);
2579 cp_parser_parse_tentatively (parser);
2580 /* Try a template-id. */
2581 id = cp_parser_template_id (parser,
2582 /*template_keyword_p=*/false,
2583 /*check_dependency_p=*/true);
2584 /* If that worked, we're done. */
2585 if (cp_parser_parse_definitely (parser))
2586 return id;
2588 /* Peek at the next token. (Changes in the token buffer may
2589 have invalidated the pointer obtained above.) */
2590 token = cp_lexer_peek_token (parser->lexer);
2592 switch (token->type)
2594 case CPP_NAME:
2595 return cp_parser_identifier (parser);
2597 case CPP_KEYWORD:
2598 if (token->keyword == RID_OPERATOR)
2599 return cp_parser_operator_function_id (parser);
2600 /* Fall through. */
2602 default:
2603 cp_parser_error (parser, "expected id-expression");
2604 return error_mark_node;
2607 else
2608 return cp_parser_unqualified_id (parser, template_keyword_p,
2609 /*check_dependency_p=*/true,
2610 declarator_p);
2613 /* Parse an unqualified-id.
2615 unqualified-id:
2616 identifier
2617 operator-function-id
2618 conversion-function-id
2619 ~ class-name
2620 template-id
2622 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2623 keyword, in a construct like `A::template ...'.
2625 Returns a representation of unqualified-id. For the `identifier'
2626 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2627 production a BIT_NOT_EXPR is returned; the operand of the
2628 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2629 other productions, see the documentation accompanying the
2630 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2631 names are looked up in uninstantiated templates. If DECLARATOR_P
2632 is true, the unqualified-id is appearing as part of a declarator,
2633 rather than as part of an expression. */
2635 static tree
2636 cp_parser_unqualified_id (cp_parser* parser,
2637 bool template_keyword_p,
2638 bool check_dependency_p,
2639 bool declarator_p)
2641 cp_token *token;
2643 /* Peek at the next token. */
2644 token = cp_lexer_peek_token (parser->lexer);
2646 switch (token->type)
2648 case CPP_NAME:
2650 tree id;
2652 /* We don't know yet whether or not this will be a
2653 template-id. */
2654 cp_parser_parse_tentatively (parser);
2655 /* Try a template-id. */
2656 id = cp_parser_template_id (parser, template_keyword_p,
2657 check_dependency_p);
2658 /* If it worked, we're done. */
2659 if (cp_parser_parse_definitely (parser))
2660 return id;
2661 /* Otherwise, it's an ordinary identifier. */
2662 return cp_parser_identifier (parser);
2665 case CPP_TEMPLATE_ID:
2666 return cp_parser_template_id (parser, template_keyword_p,
2667 check_dependency_p);
2669 case CPP_COMPL:
2671 tree type_decl;
2672 tree qualifying_scope;
2673 tree object_scope;
2674 tree scope;
2676 /* Consume the `~' token. */
2677 cp_lexer_consume_token (parser->lexer);
2678 /* Parse the class-name. The standard, as written, seems to
2679 say that:
2681 template <typename T> struct S { ~S (); };
2682 template <typename T> S<T>::~S() {}
2684 is invalid, since `~' must be followed by a class-name, but
2685 `S<T>' is dependent, and so not known to be a class.
2686 That's not right; we need to look in uninstantiated
2687 templates. A further complication arises from:
2689 template <typename T> void f(T t) {
2690 t.T::~T();
2693 Here, it is not possible to look up `T' in the scope of `T'
2694 itself. We must look in both the current scope, and the
2695 scope of the containing complete expression.
2697 Yet another issue is:
2699 struct S {
2700 int S;
2701 ~S();
2704 S::~S() {}
2706 The standard does not seem to say that the `S' in `~S'
2707 should refer to the type `S' and not the data member
2708 `S::S'. */
2710 /* DR 244 says that we look up the name after the "~" in the
2711 same scope as we looked up the qualifying name. That idea
2712 isn't fully worked out; it's more complicated than that. */
2713 scope = parser->scope;
2714 object_scope = parser->object_scope;
2715 qualifying_scope = parser->qualifying_scope;
2717 /* If the name is of the form "X::~X" it's OK. */
2718 if (scope && TYPE_P (scope)
2719 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2720 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2721 == CPP_OPEN_PAREN)
2722 && (cp_lexer_peek_token (parser->lexer)->value
2723 == TYPE_IDENTIFIER (scope)))
2725 cp_lexer_consume_token (parser->lexer);
2726 return build_nt (BIT_NOT_EXPR, scope);
2729 /* If there was an explicit qualification (S::~T), first look
2730 in the scope given by the qualification (i.e., S). */
2731 if (scope)
2733 cp_parser_parse_tentatively (parser);
2734 type_decl = cp_parser_class_name (parser,
2735 /*typename_keyword_p=*/false,
2736 /*template_keyword_p=*/false,
2737 /*type_p=*/false,
2738 /*check_dependency=*/false,
2739 /*class_head_p=*/false);
2740 if (cp_parser_parse_definitely (parser))
2741 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2743 /* In "N::S::~S", look in "N" as well. */
2744 if (scope && qualifying_scope)
2746 cp_parser_parse_tentatively (parser);
2747 parser->scope = qualifying_scope;
2748 parser->object_scope = NULL_TREE;
2749 parser->qualifying_scope = NULL_TREE;
2750 type_decl
2751 = cp_parser_class_name (parser,
2752 /*typename_keyword_p=*/false,
2753 /*template_keyword_p=*/false,
2754 /*type_p=*/false,
2755 /*check_dependency=*/false,
2756 /*class_head_p=*/false);
2757 if (cp_parser_parse_definitely (parser))
2758 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2760 /* In "p->S::~T", look in the scope given by "*p" as well. */
2761 else if (object_scope)
2763 cp_parser_parse_tentatively (parser);
2764 parser->scope = object_scope;
2765 parser->object_scope = NULL_TREE;
2766 parser->qualifying_scope = NULL_TREE;
2767 type_decl
2768 = cp_parser_class_name (parser,
2769 /*typename_keyword_p=*/false,
2770 /*template_keyword_p=*/false,
2771 /*type_p=*/false,
2772 /*check_dependency=*/false,
2773 /*class_head_p=*/false);
2774 if (cp_parser_parse_definitely (parser))
2775 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2777 /* Look in the surrounding context. */
2778 parser->scope = NULL_TREE;
2779 parser->object_scope = NULL_TREE;
2780 parser->qualifying_scope = NULL_TREE;
2781 type_decl
2782 = cp_parser_class_name (parser,
2783 /*typename_keyword_p=*/false,
2784 /*template_keyword_p=*/false,
2785 /*type_p=*/false,
2786 /*check_dependency=*/false,
2787 /*class_head_p=*/false);
2788 /* If an error occurred, assume that the name of the
2789 destructor is the same as the name of the qualifying
2790 class. That allows us to keep parsing after running
2791 into ill-formed destructor names. */
2792 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2793 return build_nt (BIT_NOT_EXPR, scope);
2794 else if (type_decl == error_mark_node)
2795 return error_mark_node;
2797 /* [class.dtor]
2799 A typedef-name that names a class shall not be used as the
2800 identifier in the declarator for a destructor declaration. */
2801 if (declarator_p
2802 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2803 && !DECL_SELF_REFERENCE_P (type_decl))
2804 error ("typedef-name `%D' used as destructor declarator",
2805 type_decl);
2807 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2810 case CPP_KEYWORD:
2811 if (token->keyword == RID_OPERATOR)
2813 tree id;
2815 /* This could be a template-id, so we try that first. */
2816 cp_parser_parse_tentatively (parser);
2817 /* Try a template-id. */
2818 id = cp_parser_template_id (parser, template_keyword_p,
2819 /*check_dependency_p=*/true);
2820 /* If that worked, we're done. */
2821 if (cp_parser_parse_definitely (parser))
2822 return id;
2823 /* We still don't know whether we're looking at an
2824 operator-function-id or a conversion-function-id. */
2825 cp_parser_parse_tentatively (parser);
2826 /* Try an operator-function-id. */
2827 id = cp_parser_operator_function_id (parser);
2828 /* If that didn't work, try a conversion-function-id. */
2829 if (!cp_parser_parse_definitely (parser))
2830 id = cp_parser_conversion_function_id (parser);
2832 return id;
2834 /* Fall through. */
2836 default:
2837 cp_parser_error (parser, "expected unqualified-id");
2838 return error_mark_node;
2842 /* Parse an (optional) nested-name-specifier.
2844 nested-name-specifier:
2845 class-or-namespace-name :: nested-name-specifier [opt]
2846 class-or-namespace-name :: template nested-name-specifier [opt]
2848 PARSER->SCOPE should be set appropriately before this function is
2849 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
2850 effect. TYPE_P is TRUE if we non-type bindings should be ignored
2851 in name lookups.
2853 Sets PARSER->SCOPE to the class (TYPE) or namespace
2854 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
2855 it unchanged if there is no nested-name-specifier. Returns the new
2856 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
2858 static tree
2859 cp_parser_nested_name_specifier_opt (cp_parser *parser,
2860 bool typename_keyword_p,
2861 bool check_dependency_p,
2862 bool type_p)
2864 bool success = false;
2865 tree access_check = NULL_TREE;
2866 ptrdiff_t start;
2867 cp_token* token;
2869 /* If the next token corresponds to a nested name specifier, there
2870 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
2871 false, it may have been true before, in which case something
2872 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
2873 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
2874 CHECK_DEPENDENCY_P is false, we have to fall through into the
2875 main loop. */
2876 if (check_dependency_p
2877 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
2879 cp_parser_pre_parsed_nested_name_specifier (parser);
2880 return parser->scope;
2883 /* Remember where the nested-name-specifier starts. */
2884 if (cp_parser_parsing_tentatively (parser)
2885 && !cp_parser_committed_to_tentative_parse (parser))
2887 token = cp_lexer_peek_token (parser->lexer);
2888 start = cp_lexer_token_difference (parser->lexer,
2889 parser->lexer->first_token,
2890 token);
2892 else
2893 start = -1;
2895 push_deferring_access_checks (dk_deferred);
2897 while (true)
2899 tree new_scope;
2900 tree old_scope;
2901 tree saved_qualifying_scope;
2902 bool template_keyword_p;
2904 /* Spot cases that cannot be the beginning of a
2905 nested-name-specifier. */
2906 token = cp_lexer_peek_token (parser->lexer);
2908 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
2909 the already parsed nested-name-specifier. */
2910 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2912 /* Grab the nested-name-specifier and continue the loop. */
2913 cp_parser_pre_parsed_nested_name_specifier (parser);
2914 success = true;
2915 continue;
2918 /* Spot cases that cannot be the beginning of a
2919 nested-name-specifier. On the second and subsequent times
2920 through the loop, we look for the `template' keyword. */
2921 if (success && token->keyword == RID_TEMPLATE)
2923 /* A template-id can start a nested-name-specifier. */
2924 else if (token->type == CPP_TEMPLATE_ID)
2926 else
2928 /* If the next token is not an identifier, then it is
2929 definitely not a class-or-namespace-name. */
2930 if (token->type != CPP_NAME)
2931 break;
2932 /* If the following token is neither a `<' (to begin a
2933 template-id), nor a `::', then we are not looking at a
2934 nested-name-specifier. */
2935 token = cp_lexer_peek_nth_token (parser->lexer, 2);
2936 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
2937 break;
2940 /* The nested-name-specifier is optional, so we parse
2941 tentatively. */
2942 cp_parser_parse_tentatively (parser);
2944 /* Look for the optional `template' keyword, if this isn't the
2945 first time through the loop. */
2946 if (success)
2947 template_keyword_p = cp_parser_optional_template_keyword (parser);
2948 else
2949 template_keyword_p = false;
2951 /* Save the old scope since the name lookup we are about to do
2952 might destroy it. */
2953 old_scope = parser->scope;
2954 saved_qualifying_scope = parser->qualifying_scope;
2955 /* Parse the qualifying entity. */
2956 new_scope
2957 = cp_parser_class_or_namespace_name (parser,
2958 typename_keyword_p,
2959 template_keyword_p,
2960 check_dependency_p,
2961 type_p);
2962 /* Look for the `::' token. */
2963 cp_parser_require (parser, CPP_SCOPE, "`::'");
2965 /* If we found what we wanted, we keep going; otherwise, we're
2966 done. */
2967 if (!cp_parser_parse_definitely (parser))
2969 bool error_p = false;
2971 /* Restore the OLD_SCOPE since it was valid before the
2972 failed attempt at finding the last
2973 class-or-namespace-name. */
2974 parser->scope = old_scope;
2975 parser->qualifying_scope = saved_qualifying_scope;
2976 /* If the next token is an identifier, and the one after
2977 that is a `::', then any valid interpretation would have
2978 found a class-or-namespace-name. */
2979 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2980 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2981 == CPP_SCOPE)
2982 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
2983 != CPP_COMPL))
2985 token = cp_lexer_consume_token (parser->lexer);
2986 if (!error_p)
2988 tree decl;
2990 decl = cp_parser_lookup_name_simple (parser, token->value);
2991 if (TREE_CODE (decl) == TEMPLATE_DECL)
2992 error ("`%D' used without template parameters",
2993 decl);
2994 else if (parser->scope)
2996 if (TYPE_P (parser->scope))
2997 error ("`%T::%D' is not a class-name or "
2998 "namespace-name",
2999 parser->scope, token->value);
3000 else if (parser->scope == global_namespace)
3001 error ("`::%D' is not a class-name or "
3002 "namespace-name",
3003 token->value);
3004 else
3005 error ("`%D::%D' is not a class-name or "
3006 "namespace-name",
3007 parser->scope, token->value);
3009 else
3010 error ("`%D' is not a class-name or namespace-name",
3011 token->value);
3012 parser->scope = NULL_TREE;
3013 error_p = true;
3014 /* Treat this as a successful nested-name-specifier
3015 due to:
3017 [basic.lookup.qual]
3019 If the name found is not a class-name (clause
3020 _class_) or namespace-name (_namespace.def_), the
3021 program is ill-formed. */
3022 success = true;
3024 cp_lexer_consume_token (parser->lexer);
3026 break;
3029 /* We've found one valid nested-name-specifier. */
3030 success = true;
3031 /* Make sure we look in the right scope the next time through
3032 the loop. */
3033 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3034 ? TREE_TYPE (new_scope)
3035 : new_scope);
3036 /* If it is a class scope, try to complete it; we are about to
3037 be looking up names inside the class. */
3038 if (TYPE_P (parser->scope)
3039 /* Since checking types for dependency can be expensive,
3040 avoid doing it if the type is already complete. */
3041 && !COMPLETE_TYPE_P (parser->scope)
3042 /* Do not try to complete dependent types. */
3043 && !dependent_type_p (parser->scope))
3044 complete_type (parser->scope);
3047 /* Retrieve any deferred checks. Do not pop this access checks yet
3048 so the memory will not be reclaimed during token replacing below. */
3049 access_check = get_deferred_access_checks ();
3051 /* If parsing tentatively, replace the sequence of tokens that makes
3052 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3053 token. That way, should we re-parse the token stream, we will
3054 not have to repeat the effort required to do the parse, nor will
3055 we issue duplicate error messages. */
3056 if (success && start >= 0)
3058 /* Find the token that corresponds to the start of the
3059 template-id. */
3060 token = cp_lexer_advance_token (parser->lexer,
3061 parser->lexer->first_token,
3062 start);
3064 /* Reset the contents of the START token. */
3065 token->type = CPP_NESTED_NAME_SPECIFIER;
3066 token->value = build_tree_list (access_check, parser->scope);
3067 TREE_TYPE (token->value) = parser->qualifying_scope;
3068 token->keyword = RID_MAX;
3069 /* Purge all subsequent tokens. */
3070 cp_lexer_purge_tokens_after (parser->lexer, token);
3073 pop_deferring_access_checks ();
3074 return success ? parser->scope : NULL_TREE;
3077 /* Parse a nested-name-specifier. See
3078 cp_parser_nested_name_specifier_opt for details. This function
3079 behaves identically, except that it will an issue an error if no
3080 nested-name-specifier is present, and it will return
3081 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3082 is present. */
3084 static tree
3085 cp_parser_nested_name_specifier (cp_parser *parser,
3086 bool typename_keyword_p,
3087 bool check_dependency_p,
3088 bool type_p)
3090 tree scope;
3092 /* Look for the nested-name-specifier. */
3093 scope = cp_parser_nested_name_specifier_opt (parser,
3094 typename_keyword_p,
3095 check_dependency_p,
3096 type_p);
3097 /* If it was not present, issue an error message. */
3098 if (!scope)
3100 cp_parser_error (parser, "expected nested-name-specifier");
3101 parser->scope = NULL_TREE;
3102 return error_mark_node;
3105 return scope;
3108 /* Parse a class-or-namespace-name.
3110 class-or-namespace-name:
3111 class-name
3112 namespace-name
3114 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3115 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3116 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3117 TYPE_P is TRUE iff the next name should be taken as a class-name,
3118 even the same name is declared to be another entity in the same
3119 scope.
3121 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3122 specified by the class-or-namespace-name. If neither is found the
3123 ERROR_MARK_NODE is returned. */
3125 static tree
3126 cp_parser_class_or_namespace_name (cp_parser *parser,
3127 bool typename_keyword_p,
3128 bool template_keyword_p,
3129 bool check_dependency_p,
3130 bool type_p)
3132 tree saved_scope;
3133 tree saved_qualifying_scope;
3134 tree saved_object_scope;
3135 tree scope;
3136 bool only_class_p;
3138 /* Before we try to parse the class-name, we must save away the
3139 current PARSER->SCOPE since cp_parser_class_name will destroy
3140 it. */
3141 saved_scope = parser->scope;
3142 saved_qualifying_scope = parser->qualifying_scope;
3143 saved_object_scope = parser->object_scope;
3144 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3145 there is no need to look for a namespace-name. */
3146 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3147 if (!only_class_p)
3148 cp_parser_parse_tentatively (parser);
3149 scope = cp_parser_class_name (parser,
3150 typename_keyword_p,
3151 template_keyword_p,
3152 type_p,
3153 check_dependency_p,
3154 /*class_head_p=*/false);
3155 /* If that didn't work, try for a namespace-name. */
3156 if (!only_class_p && !cp_parser_parse_definitely (parser))
3158 /* Restore the saved scope. */
3159 parser->scope = saved_scope;
3160 parser->qualifying_scope = saved_qualifying_scope;
3161 parser->object_scope = saved_object_scope;
3162 /* If we are not looking at an identifier followed by the scope
3163 resolution operator, then this is not part of a
3164 nested-name-specifier. (Note that this function is only used
3165 to parse the components of a nested-name-specifier.) */
3166 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3167 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3168 return error_mark_node;
3169 scope = cp_parser_namespace_name (parser);
3172 return scope;
3175 /* Parse a postfix-expression.
3177 postfix-expression:
3178 primary-expression
3179 postfix-expression [ expression ]
3180 postfix-expression ( expression-list [opt] )
3181 simple-type-specifier ( expression-list [opt] )
3182 typename :: [opt] nested-name-specifier identifier
3183 ( expression-list [opt] )
3184 typename :: [opt] nested-name-specifier template [opt] template-id
3185 ( expression-list [opt] )
3186 postfix-expression . template [opt] id-expression
3187 postfix-expression -> template [opt] id-expression
3188 postfix-expression . pseudo-destructor-name
3189 postfix-expression -> pseudo-destructor-name
3190 postfix-expression ++
3191 postfix-expression --
3192 dynamic_cast < type-id > ( expression )
3193 static_cast < type-id > ( expression )
3194 reinterpret_cast < type-id > ( expression )
3195 const_cast < type-id > ( expression )
3196 typeid ( expression )
3197 typeid ( type-id )
3199 GNU Extension:
3201 postfix-expression:
3202 ( type-id ) { initializer-list , [opt] }
3204 This extension is a GNU version of the C99 compound-literal
3205 construct. (The C99 grammar uses `type-name' instead of `type-id',
3206 but they are essentially the same concept.)
3208 If ADDRESS_P is true, the postfix expression is the operand of the
3209 `&' operator.
3211 Returns a representation of the expression. */
3213 static tree
3214 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3216 cp_token *token;
3217 enum rid keyword;
3218 cp_id_kind idk = CP_ID_KIND_NONE;
3219 tree postfix_expression = NULL_TREE;
3220 /* Non-NULL only if the current postfix-expression can be used to
3221 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3222 class used to qualify the member. */
3223 tree qualifying_class = NULL_TREE;
3225 /* Peek at the next token. */
3226 token = cp_lexer_peek_token (parser->lexer);
3227 /* Some of the productions are determined by keywords. */
3228 keyword = token->keyword;
3229 switch (keyword)
3231 case RID_DYNCAST:
3232 case RID_STATCAST:
3233 case RID_REINTCAST:
3234 case RID_CONSTCAST:
3236 tree type;
3237 tree expression;
3238 const char *saved_message;
3240 /* All of these can be handled in the same way from the point
3241 of view of parsing. Begin by consuming the token
3242 identifying the cast. */
3243 cp_lexer_consume_token (parser->lexer);
3245 /* New types cannot be defined in the cast. */
3246 saved_message = parser->type_definition_forbidden_message;
3247 parser->type_definition_forbidden_message
3248 = "types may not be defined in casts";
3250 /* Look for the opening `<'. */
3251 cp_parser_require (parser, CPP_LESS, "`<'");
3252 /* Parse the type to which we are casting. */
3253 type = cp_parser_type_id (parser);
3254 /* Look for the closing `>'. */
3255 cp_parser_require (parser, CPP_GREATER, "`>'");
3256 /* Restore the old message. */
3257 parser->type_definition_forbidden_message = saved_message;
3259 /* And the expression which is being cast. */
3260 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3261 expression = cp_parser_expression (parser);
3262 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3264 /* Only type conversions to integral or enumeration types
3265 can be used in constant-expressions. */
3266 if (parser->constant_expression_p
3267 && !dependent_type_p (type)
3268 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3270 if (!parser->allow_non_constant_expression_p)
3271 return (cp_parser_non_constant_expression
3272 ("a cast to a type other than an integral or "
3273 "enumeration type"));
3274 parser->non_constant_expression_p = true;
3277 switch (keyword)
3279 case RID_DYNCAST:
3280 postfix_expression
3281 = build_dynamic_cast (type, expression);
3282 break;
3283 case RID_STATCAST:
3284 postfix_expression
3285 = build_static_cast (type, expression);
3286 break;
3287 case RID_REINTCAST:
3288 postfix_expression
3289 = build_reinterpret_cast (type, expression);
3290 break;
3291 case RID_CONSTCAST:
3292 postfix_expression
3293 = build_const_cast (type, expression);
3294 break;
3295 default:
3296 abort ();
3299 break;
3301 case RID_TYPEID:
3303 tree type;
3304 const char *saved_message;
3306 /* Consume the `typeid' token. */
3307 cp_lexer_consume_token (parser->lexer);
3308 /* Look for the `(' token. */
3309 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3310 /* Types cannot be defined in a `typeid' expression. */
3311 saved_message = parser->type_definition_forbidden_message;
3312 parser->type_definition_forbidden_message
3313 = "types may not be defined in a `typeid\' expression";
3314 /* We can't be sure yet whether we're looking at a type-id or an
3315 expression. */
3316 cp_parser_parse_tentatively (parser);
3317 /* Try a type-id first. */
3318 type = cp_parser_type_id (parser);
3319 /* Look for the `)' token. Otherwise, we can't be sure that
3320 we're not looking at an expression: consider `typeid (int
3321 (3))', for example. */
3322 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3323 /* If all went well, simply lookup the type-id. */
3324 if (cp_parser_parse_definitely (parser))
3325 postfix_expression = get_typeid (type);
3326 /* Otherwise, fall back to the expression variant. */
3327 else
3329 tree expression;
3331 /* Look for an expression. */
3332 expression = cp_parser_expression (parser);
3333 /* Compute its typeid. */
3334 postfix_expression = build_typeid (expression);
3335 /* Look for the `)' token. */
3336 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3339 /* Restore the saved message. */
3340 parser->type_definition_forbidden_message = saved_message;
3342 break;
3344 case RID_TYPENAME:
3346 bool template_p = false;
3347 tree id;
3348 tree type;
3350 /* Consume the `typename' token. */
3351 cp_lexer_consume_token (parser->lexer);
3352 /* Look for the optional `::' operator. */
3353 cp_parser_global_scope_opt (parser,
3354 /*current_scope_valid_p=*/false);
3355 /* Look for the nested-name-specifier. */
3356 cp_parser_nested_name_specifier (parser,
3357 /*typename_keyword_p=*/true,
3358 /*check_dependency_p=*/true,
3359 /*type_p=*/true);
3360 /* Look for the optional `template' keyword. */
3361 template_p = cp_parser_optional_template_keyword (parser);
3362 /* We don't know whether we're looking at a template-id or an
3363 identifier. */
3364 cp_parser_parse_tentatively (parser);
3365 /* Try a template-id. */
3366 id = cp_parser_template_id (parser, template_p,
3367 /*check_dependency_p=*/true);
3368 /* If that didn't work, try an identifier. */
3369 if (!cp_parser_parse_definitely (parser))
3370 id = cp_parser_identifier (parser);
3371 /* Create a TYPENAME_TYPE to represent the type to which the
3372 functional cast is being performed. */
3373 type = make_typename_type (parser->scope, id,
3374 /*complain=*/1);
3376 postfix_expression = cp_parser_functional_cast (parser, type);
3378 break;
3380 default:
3382 tree type;
3384 /* If the next thing is a simple-type-specifier, we may be
3385 looking at a functional cast. We could also be looking at
3386 an id-expression. So, we try the functional cast, and if
3387 that doesn't work we fall back to the primary-expression. */
3388 cp_parser_parse_tentatively (parser);
3389 /* Look for the simple-type-specifier. */
3390 type = cp_parser_simple_type_specifier (parser,
3391 CP_PARSER_FLAGS_NONE,
3392 /*identifier_p=*/false);
3393 /* Parse the cast itself. */
3394 if (!cp_parser_error_occurred (parser))
3395 postfix_expression
3396 = cp_parser_functional_cast (parser, type);
3397 /* If that worked, we're done. */
3398 if (cp_parser_parse_definitely (parser))
3399 break;
3401 /* If the functional-cast didn't work out, try a
3402 compound-literal. */
3403 if (cp_parser_allow_gnu_extensions_p (parser)
3404 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3406 tree initializer_list = NULL_TREE;
3408 cp_parser_parse_tentatively (parser);
3409 /* Consume the `('. */
3410 cp_lexer_consume_token (parser->lexer);
3411 /* Parse the type. */
3412 type = cp_parser_type_id (parser);
3413 /* Look for the `)'. */
3414 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3415 /* Look for the `{'. */
3416 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3417 /* If things aren't going well, there's no need to
3418 keep going. */
3419 if (!cp_parser_error_occurred (parser))
3421 bool non_constant_p;
3422 /* Parse the initializer-list. */
3423 initializer_list
3424 = cp_parser_initializer_list (parser, &non_constant_p);
3425 /* Allow a trailing `,'. */
3426 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3427 cp_lexer_consume_token (parser->lexer);
3428 /* Look for the final `}'. */
3429 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3431 /* If that worked, we're definitely looking at a
3432 compound-literal expression. */
3433 if (cp_parser_parse_definitely (parser))
3435 /* Warn the user that a compound literal is not
3436 allowed in standard C++. */
3437 if (pedantic)
3438 pedwarn ("ISO C++ forbids compound-literals");
3439 /* Form the representation of the compound-literal. */
3440 postfix_expression
3441 = finish_compound_literal (type, initializer_list);
3442 break;
3446 /* It must be a primary-expression. */
3447 postfix_expression = cp_parser_primary_expression (parser,
3448 &idk,
3449 &qualifying_class);
3451 break;
3454 /* If we were avoiding committing to the processing of a
3455 qualified-id until we knew whether or not we had a
3456 pointer-to-member, we now know. */
3457 if (qualifying_class)
3459 bool done;
3461 /* Peek at the next token. */
3462 token = cp_lexer_peek_token (parser->lexer);
3463 done = (token->type != CPP_OPEN_SQUARE
3464 && token->type != CPP_OPEN_PAREN
3465 && token->type != CPP_DOT
3466 && token->type != CPP_DEREF
3467 && token->type != CPP_PLUS_PLUS
3468 && token->type != CPP_MINUS_MINUS);
3470 postfix_expression = finish_qualified_id_expr (qualifying_class,
3471 postfix_expression,
3472 done,
3473 address_p);
3474 if (done)
3475 return postfix_expression;
3478 /* Keep looping until the postfix-expression is complete. */
3479 while (true)
3481 if (idk == CP_ID_KIND_UNQUALIFIED
3482 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3483 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3484 /* It is not a Koenig lookup function call. */
3485 postfix_expression
3486 = unqualified_name_lookup_error (postfix_expression);
3488 /* Peek at the next token. */
3489 token = cp_lexer_peek_token (parser->lexer);
3491 switch (token->type)
3493 case CPP_OPEN_SQUARE:
3494 /* postfix-expression [ expression ] */
3496 tree index;
3498 /* Consume the `[' token. */
3499 cp_lexer_consume_token (parser->lexer);
3500 /* Parse the index expression. */
3501 index = cp_parser_expression (parser);
3502 /* Look for the closing `]'. */
3503 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3505 /* Build the ARRAY_REF. */
3506 postfix_expression
3507 = grok_array_decl (postfix_expression, index);
3508 idk = CP_ID_KIND_NONE;
3510 break;
3512 case CPP_OPEN_PAREN:
3513 /* postfix-expression ( expression-list [opt] ) */
3515 bool koenig_p;
3516 tree args = (cp_parser_parenthesized_expression_list
3517 (parser, false, /*non_constant_p=*/NULL));
3519 if (args == error_mark_node)
3521 postfix_expression = error_mark_node;
3522 break;
3525 /* Function calls are not permitted in
3526 constant-expressions. */
3527 if (parser->constant_expression_p)
3529 if (!parser->allow_non_constant_expression_p)
3530 return cp_parser_non_constant_expression ("a function call");
3531 parser->non_constant_expression_p = true;
3534 koenig_p = false;
3535 if (idk == CP_ID_KIND_UNQUALIFIED)
3537 if (args
3538 && (is_overloaded_fn (postfix_expression)
3539 || DECL_P (postfix_expression)
3540 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3542 koenig_p = true;
3543 postfix_expression
3544 = perform_koenig_lookup (postfix_expression, args);
3546 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3547 postfix_expression
3548 = unqualified_fn_lookup_error (postfix_expression);
3551 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3553 tree instance = TREE_OPERAND (postfix_expression, 0);
3554 tree fn = TREE_OPERAND (postfix_expression, 1);
3556 if (processing_template_decl
3557 && (type_dependent_expression_p (instance)
3558 || (!BASELINK_P (fn)
3559 && TREE_CODE (fn) != FIELD_DECL)
3560 || type_dependent_expression_p (fn)
3561 || any_type_dependent_arguments_p (args)))
3563 postfix_expression
3564 = build_min_nt (CALL_EXPR, postfix_expression, args);
3565 break;
3568 postfix_expression
3569 = (build_new_method_call
3570 (instance, fn, args, NULL_TREE,
3571 (idk == CP_ID_KIND_QUALIFIED
3572 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3574 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3575 || TREE_CODE (postfix_expression) == MEMBER_REF
3576 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3577 postfix_expression = (build_offset_ref_call_from_tree
3578 (postfix_expression, args));
3579 else if (idk == CP_ID_KIND_QUALIFIED)
3580 /* A call to a static class member, or a namespace-scope
3581 function. */
3582 postfix_expression
3583 = finish_call_expr (postfix_expression, args,
3584 /*disallow_virtual=*/true,
3585 koenig_p);
3586 else
3587 /* All other function calls. */
3588 postfix_expression
3589 = finish_call_expr (postfix_expression, args,
3590 /*disallow_virtual=*/false,
3591 koenig_p);
3593 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3594 idk = CP_ID_KIND_NONE;
3596 break;
3598 case CPP_DOT:
3599 case CPP_DEREF:
3600 /* postfix-expression . template [opt] id-expression
3601 postfix-expression . pseudo-destructor-name
3602 postfix-expression -> template [opt] id-expression
3603 postfix-expression -> pseudo-destructor-name */
3605 tree name;
3606 bool dependent_p;
3607 bool template_p;
3608 tree scope = NULL_TREE;
3610 /* If this is a `->' operator, dereference the pointer. */
3611 if (token->type == CPP_DEREF)
3612 postfix_expression = build_x_arrow (postfix_expression);
3613 /* Check to see whether or not the expression is
3614 type-dependent. */
3615 dependent_p = type_dependent_expression_p (postfix_expression);
3616 /* The identifier following the `->' or `.' is not
3617 qualified. */
3618 parser->scope = NULL_TREE;
3619 parser->qualifying_scope = NULL_TREE;
3620 parser->object_scope = NULL_TREE;
3621 idk = CP_ID_KIND_NONE;
3622 /* Enter the scope corresponding to the type of the object
3623 given by the POSTFIX_EXPRESSION. */
3624 if (!dependent_p
3625 && TREE_TYPE (postfix_expression) != NULL_TREE)
3627 scope = TREE_TYPE (postfix_expression);
3628 /* According to the standard, no expression should
3629 ever have reference type. Unfortunately, we do not
3630 currently match the standard in this respect in
3631 that our internal representation of an expression
3632 may have reference type even when the standard says
3633 it does not. Therefore, we have to manually obtain
3634 the underlying type here. */
3635 scope = non_reference (scope);
3636 /* The type of the POSTFIX_EXPRESSION must be
3637 complete. */
3638 scope = complete_type_or_else (scope, NULL_TREE);
3639 /* Let the name lookup machinery know that we are
3640 processing a class member access expression. */
3641 parser->context->object_type = scope;
3642 /* If something went wrong, we want to be able to
3643 discern that case, as opposed to the case where
3644 there was no SCOPE due to the type of expression
3645 being dependent. */
3646 if (!scope)
3647 scope = error_mark_node;
3650 /* Consume the `.' or `->' operator. */
3651 cp_lexer_consume_token (parser->lexer);
3652 /* If the SCOPE is not a scalar type, we are looking at an
3653 ordinary class member access expression, rather than a
3654 pseudo-destructor-name. */
3655 if (!scope || !SCALAR_TYPE_P (scope))
3657 template_p = cp_parser_optional_template_keyword (parser);
3658 /* Parse the id-expression. */
3659 name = cp_parser_id_expression (parser,
3660 template_p,
3661 /*check_dependency_p=*/true,
3662 /*template_p=*/NULL,
3663 /*declarator_p=*/false);
3664 /* In general, build a SCOPE_REF if the member name is
3665 qualified. However, if the name was not dependent
3666 and has already been resolved; there is no need to
3667 build the SCOPE_REF. For example;
3669 struct X { void f(); };
3670 template <typename T> void f(T* t) { t->X::f(); }
3672 Even though "t" is dependent, "X::f" is not and has
3673 been resolved to a BASELINK; there is no need to
3674 include scope information. */
3676 /* But we do need to remember that there was an explicit
3677 scope for virtual function calls. */
3678 if (parser->scope)
3679 idk = CP_ID_KIND_QUALIFIED;
3681 if (name != error_mark_node
3682 && !BASELINK_P (name)
3683 && parser->scope)
3685 name = build_nt (SCOPE_REF, parser->scope, name);
3686 parser->scope = NULL_TREE;
3687 parser->qualifying_scope = NULL_TREE;
3688 parser->object_scope = NULL_TREE;
3690 postfix_expression
3691 = finish_class_member_access_expr (postfix_expression, name);
3693 /* Otherwise, try the pseudo-destructor-name production. */
3694 else
3696 tree s;
3697 tree type;
3699 /* Parse the pseudo-destructor-name. */
3700 cp_parser_pseudo_destructor_name (parser, &s, &type);
3701 /* Form the call. */
3702 postfix_expression
3703 = finish_pseudo_destructor_expr (postfix_expression,
3704 s, TREE_TYPE (type));
3707 /* We no longer need to look up names in the scope of the
3708 object on the left-hand side of the `.' or `->'
3709 operator. */
3710 parser->context->object_type = NULL_TREE;
3712 break;
3714 case CPP_PLUS_PLUS:
3715 /* postfix-expression ++ */
3716 /* Consume the `++' token. */
3717 cp_lexer_consume_token (parser->lexer);
3718 /* Increments may not appear in constant-expressions. */
3719 if (parser->constant_expression_p)
3721 if (!parser->allow_non_constant_expression_p)
3722 return cp_parser_non_constant_expression ("an increment");
3723 parser->non_constant_expression_p = true;
3725 /* Generate a representation for the complete expression. */
3726 postfix_expression
3727 = finish_increment_expr (postfix_expression,
3728 POSTINCREMENT_EXPR);
3729 idk = CP_ID_KIND_NONE;
3730 break;
3732 case CPP_MINUS_MINUS:
3733 /* postfix-expression -- */
3734 /* Consume the `--' token. */
3735 cp_lexer_consume_token (parser->lexer);
3736 /* Decrements may not appear in constant-expressions. */
3737 if (parser->constant_expression_p)
3739 if (!parser->allow_non_constant_expression_p)
3740 return cp_parser_non_constant_expression ("a decrement");
3741 parser->non_constant_expression_p = true;
3743 /* Generate a representation for the complete expression. */
3744 postfix_expression
3745 = finish_increment_expr (postfix_expression,
3746 POSTDECREMENT_EXPR);
3747 idk = CP_ID_KIND_NONE;
3748 break;
3750 default:
3751 return postfix_expression;
3755 /* We should never get here. */
3756 abort ();
3757 return error_mark_node;
3760 /* Parse a parenthesized expression-list.
3762 expression-list:
3763 assignment-expression
3764 expression-list, assignment-expression
3766 attribute-list:
3767 expression-list
3768 identifier
3769 identifier, expression-list
3771 Returns a TREE_LIST. The TREE_VALUE of each node is a
3772 representation of an assignment-expression. Note that a TREE_LIST
3773 is returned even if there is only a single expression in the list.
3774 error_mark_node is returned if the ( and or ) are
3775 missing. NULL_TREE is returned on no expressions. The parentheses
3776 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
3777 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
3778 indicates whether or not all of the expressions in the list were
3779 constant. */
3781 static tree
3782 cp_parser_parenthesized_expression_list (cp_parser* parser,
3783 bool is_attribute_list,
3784 bool *non_constant_p)
3786 tree expression_list = NULL_TREE;
3787 tree identifier = NULL_TREE;
3789 /* Assume all the expressions will be constant. */
3790 if (non_constant_p)
3791 *non_constant_p = false;
3793 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3794 return error_mark_node;
3796 /* Consume expressions until there are no more. */
3797 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
3798 while (true)
3800 tree expr;
3802 /* At the beginning of attribute lists, check to see if the
3803 next token is an identifier. */
3804 if (is_attribute_list
3805 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
3807 cp_token *token;
3809 /* Consume the identifier. */
3810 token = cp_lexer_consume_token (parser->lexer);
3811 /* Save the identifier. */
3812 identifier = token->value;
3814 else
3816 /* Parse the next assignment-expression. */
3817 if (non_constant_p)
3819 bool expr_non_constant_p;
3820 expr = (cp_parser_constant_expression
3821 (parser, /*allow_non_constant_p=*/true,
3822 &expr_non_constant_p));
3823 if (expr_non_constant_p)
3824 *non_constant_p = true;
3826 else
3827 expr = cp_parser_assignment_expression (parser);
3829 /* Add it to the list. We add error_mark_node
3830 expressions to the list, so that we can still tell if
3831 the correct form for a parenthesized expression-list
3832 is found. That gives better errors. */
3833 expression_list = tree_cons (NULL_TREE, expr, expression_list);
3835 if (expr == error_mark_node)
3836 goto skip_comma;
3839 /* After the first item, attribute lists look the same as
3840 expression lists. */
3841 is_attribute_list = false;
3843 get_comma:;
3844 /* If the next token isn't a `,', then we are done. */
3845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
3846 break;
3848 /* Otherwise, consume the `,' and keep going. */
3849 cp_lexer_consume_token (parser->lexer);
3852 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3854 int ending;
3856 skip_comma:;
3857 /* We try and resync to an unnested comma, as that will give the
3858 user better diagnostics. */
3859 ending = cp_parser_skip_to_closing_parenthesis (parser, true, true);
3860 if (ending < 0)
3861 goto get_comma;
3862 if (!ending)
3863 return error_mark_node;
3866 /* We built up the list in reverse order so we must reverse it now. */
3867 expression_list = nreverse (expression_list);
3868 if (identifier)
3869 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
3871 return expression_list;
3874 /* Parse a pseudo-destructor-name.
3876 pseudo-destructor-name:
3877 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
3878 :: [opt] nested-name-specifier template template-id :: ~ type-name
3879 :: [opt] nested-name-specifier [opt] ~ type-name
3881 If either of the first two productions is used, sets *SCOPE to the
3882 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
3883 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
3884 or ERROR_MARK_NODE if no type-name is present. */
3886 static void
3887 cp_parser_pseudo_destructor_name (cp_parser* parser,
3888 tree* scope,
3889 tree* type)
3891 bool nested_name_specifier_p;
3893 /* Look for the optional `::' operator. */
3894 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
3895 /* Look for the optional nested-name-specifier. */
3896 nested_name_specifier_p
3897 = (cp_parser_nested_name_specifier_opt (parser,
3898 /*typename_keyword_p=*/false,
3899 /*check_dependency_p=*/true,
3900 /*type_p=*/false)
3901 != NULL_TREE);
3902 /* Now, if we saw a nested-name-specifier, we might be doing the
3903 second production. */
3904 if (nested_name_specifier_p
3905 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3907 /* Consume the `template' keyword. */
3908 cp_lexer_consume_token (parser->lexer);
3909 /* Parse the template-id. */
3910 cp_parser_template_id (parser,
3911 /*template_keyword_p=*/true,
3912 /*check_dependency_p=*/false);
3913 /* Look for the `::' token. */
3914 cp_parser_require (parser, CPP_SCOPE, "`::'");
3916 /* If the next token is not a `~', then there might be some
3917 additional qualification. */
3918 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
3920 /* Look for the type-name. */
3921 *scope = TREE_TYPE (cp_parser_type_name (parser));
3922 /* Look for the `::' token. */
3923 cp_parser_require (parser, CPP_SCOPE, "`::'");
3925 else
3926 *scope = NULL_TREE;
3928 /* Look for the `~'. */
3929 cp_parser_require (parser, CPP_COMPL, "`~'");
3930 /* Look for the type-name again. We are not responsible for
3931 checking that it matches the first type-name. */
3932 *type = cp_parser_type_name (parser);
3935 /* Parse a unary-expression.
3937 unary-expression:
3938 postfix-expression
3939 ++ cast-expression
3940 -- cast-expression
3941 unary-operator cast-expression
3942 sizeof unary-expression
3943 sizeof ( type-id )
3944 new-expression
3945 delete-expression
3947 GNU Extensions:
3949 unary-expression:
3950 __extension__ cast-expression
3951 __alignof__ unary-expression
3952 __alignof__ ( type-id )
3953 __real__ cast-expression
3954 __imag__ cast-expression
3955 && identifier
3957 ADDRESS_P is true iff the unary-expression is appearing as the
3958 operand of the `&' operator.
3960 Returns a representation of the expression. */
3962 static tree
3963 cp_parser_unary_expression (cp_parser *parser, bool address_p)
3965 cp_token *token;
3966 enum tree_code unary_operator;
3968 /* Peek at the next token. */
3969 token = cp_lexer_peek_token (parser->lexer);
3970 /* Some keywords give away the kind of expression. */
3971 if (token->type == CPP_KEYWORD)
3973 enum rid keyword = token->keyword;
3975 switch (keyword)
3977 case RID_ALIGNOF:
3978 case RID_SIZEOF:
3980 tree operand;
3981 enum tree_code op;
3983 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
3984 /* Consume the token. */
3985 cp_lexer_consume_token (parser->lexer);
3986 /* Parse the operand. */
3987 operand = cp_parser_sizeof_operand (parser, keyword);
3989 if (TYPE_P (operand))
3990 return cxx_sizeof_or_alignof_type (operand, op, true);
3991 else
3992 return cxx_sizeof_or_alignof_expr (operand, op);
3995 case RID_NEW:
3996 return cp_parser_new_expression (parser);
3998 case RID_DELETE:
3999 return cp_parser_delete_expression (parser);
4001 case RID_EXTENSION:
4003 /* The saved value of the PEDANTIC flag. */
4004 int saved_pedantic;
4005 tree expr;
4007 /* Save away the PEDANTIC flag. */
4008 cp_parser_extension_opt (parser, &saved_pedantic);
4009 /* Parse the cast-expression. */
4010 expr = cp_parser_simple_cast_expression (parser);
4011 /* Restore the PEDANTIC flag. */
4012 pedantic = saved_pedantic;
4014 return expr;
4017 case RID_REALPART:
4018 case RID_IMAGPART:
4020 tree expression;
4022 /* Consume the `__real__' or `__imag__' token. */
4023 cp_lexer_consume_token (parser->lexer);
4024 /* Parse the cast-expression. */
4025 expression = cp_parser_simple_cast_expression (parser);
4026 /* Create the complete representation. */
4027 return build_x_unary_op ((keyword == RID_REALPART
4028 ? REALPART_EXPR : IMAGPART_EXPR),
4029 expression);
4031 break;
4033 default:
4034 break;
4038 /* Look for the `:: new' and `:: delete', which also signal the
4039 beginning of a new-expression, or delete-expression,
4040 respectively. If the next token is `::', then it might be one of
4041 these. */
4042 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4044 enum rid keyword;
4046 /* See if the token after the `::' is one of the keywords in
4047 which we're interested. */
4048 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4049 /* If it's `new', we have a new-expression. */
4050 if (keyword == RID_NEW)
4051 return cp_parser_new_expression (parser);
4052 /* Similarly, for `delete'. */
4053 else if (keyword == RID_DELETE)
4054 return cp_parser_delete_expression (parser);
4057 /* Look for a unary operator. */
4058 unary_operator = cp_parser_unary_operator (token);
4059 /* The `++' and `--' operators can be handled similarly, even though
4060 they are not technically unary-operators in the grammar. */
4061 if (unary_operator == ERROR_MARK)
4063 if (token->type == CPP_PLUS_PLUS)
4064 unary_operator = PREINCREMENT_EXPR;
4065 else if (token->type == CPP_MINUS_MINUS)
4066 unary_operator = PREDECREMENT_EXPR;
4067 /* Handle the GNU address-of-label extension. */
4068 else if (cp_parser_allow_gnu_extensions_p (parser)
4069 && token->type == CPP_AND_AND)
4071 tree identifier;
4073 /* Consume the '&&' token. */
4074 cp_lexer_consume_token (parser->lexer);
4075 /* Look for the identifier. */
4076 identifier = cp_parser_identifier (parser);
4077 /* Create an expression representing the address. */
4078 return finish_label_address_expr (identifier);
4081 if (unary_operator != ERROR_MARK)
4083 tree cast_expression;
4085 /* Consume the operator token. */
4086 token = cp_lexer_consume_token (parser->lexer);
4087 /* Parse the cast-expression. */
4088 cast_expression
4089 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4090 /* Now, build an appropriate representation. */
4091 switch (unary_operator)
4093 case INDIRECT_REF:
4094 return build_x_indirect_ref (cast_expression, "unary *");
4096 case ADDR_EXPR:
4097 case BIT_NOT_EXPR:
4098 return build_x_unary_op (unary_operator, cast_expression);
4100 case PREINCREMENT_EXPR:
4101 case PREDECREMENT_EXPR:
4102 if (parser->constant_expression_p)
4104 if (!parser->allow_non_constant_expression_p)
4105 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4106 ? "an increment"
4107 : "a decrement");
4108 parser->non_constant_expression_p = true;
4110 /* Fall through. */
4111 case CONVERT_EXPR:
4112 case NEGATE_EXPR:
4113 case TRUTH_NOT_EXPR:
4114 return finish_unary_op_expr (unary_operator, cast_expression);
4116 default:
4117 abort ();
4118 return error_mark_node;
4122 return cp_parser_postfix_expression (parser, address_p);
4125 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4126 unary-operator, the corresponding tree code is returned. */
4128 static enum tree_code
4129 cp_parser_unary_operator (cp_token* token)
4131 switch (token->type)
4133 case CPP_MULT:
4134 return INDIRECT_REF;
4136 case CPP_AND:
4137 return ADDR_EXPR;
4139 case CPP_PLUS:
4140 return CONVERT_EXPR;
4142 case CPP_MINUS:
4143 return NEGATE_EXPR;
4145 case CPP_NOT:
4146 return TRUTH_NOT_EXPR;
4148 case CPP_COMPL:
4149 return BIT_NOT_EXPR;
4151 default:
4152 return ERROR_MARK;
4156 /* Parse a new-expression.
4158 new-expression:
4159 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4160 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4162 Returns a representation of the expression. */
4164 static tree
4165 cp_parser_new_expression (cp_parser* parser)
4167 bool global_scope_p;
4168 tree placement;
4169 tree type;
4170 tree initializer;
4172 /* Look for the optional `::' operator. */
4173 global_scope_p
4174 = (cp_parser_global_scope_opt (parser,
4175 /*current_scope_valid_p=*/false)
4176 != NULL_TREE);
4177 /* Look for the `new' operator. */
4178 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4179 /* There's no easy way to tell a new-placement from the
4180 `( type-id )' construct. */
4181 cp_parser_parse_tentatively (parser);
4182 /* Look for a new-placement. */
4183 placement = cp_parser_new_placement (parser);
4184 /* If that didn't work out, there's no new-placement. */
4185 if (!cp_parser_parse_definitely (parser))
4186 placement = NULL_TREE;
4188 /* If the next token is a `(', then we have a parenthesized
4189 type-id. */
4190 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4192 /* Consume the `('. */
4193 cp_lexer_consume_token (parser->lexer);
4194 /* Parse the type-id. */
4195 type = cp_parser_type_id (parser);
4196 /* Look for the closing `)'. */
4197 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4199 /* Otherwise, there must be a new-type-id. */
4200 else
4201 type = cp_parser_new_type_id (parser);
4203 /* If the next token is a `(', then we have a new-initializer. */
4204 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4205 initializer = cp_parser_new_initializer (parser);
4206 else
4207 initializer = NULL_TREE;
4209 /* Create a representation of the new-expression. */
4210 return build_new (placement, type, initializer, global_scope_p);
4213 /* Parse a new-placement.
4215 new-placement:
4216 ( expression-list )
4218 Returns the same representation as for an expression-list. */
4220 static tree
4221 cp_parser_new_placement (cp_parser* parser)
4223 tree expression_list;
4225 /* Parse the expression-list. */
4226 expression_list = (cp_parser_parenthesized_expression_list
4227 (parser, false, /*non_constant_p=*/NULL));
4229 return expression_list;
4232 /* Parse a new-type-id.
4234 new-type-id:
4235 type-specifier-seq new-declarator [opt]
4237 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4238 and whose TREE_VALUE is the new-declarator. */
4240 static tree
4241 cp_parser_new_type_id (cp_parser* parser)
4243 tree type_specifier_seq;
4244 tree declarator;
4245 const char *saved_message;
4247 /* The type-specifier sequence must not contain type definitions.
4248 (It cannot contain declarations of new types either, but if they
4249 are not definitions we will catch that because they are not
4250 complete.) */
4251 saved_message = parser->type_definition_forbidden_message;
4252 parser->type_definition_forbidden_message
4253 = "types may not be defined in a new-type-id";
4254 /* Parse the type-specifier-seq. */
4255 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4256 /* Restore the old message. */
4257 parser->type_definition_forbidden_message = saved_message;
4258 /* Parse the new-declarator. */
4259 declarator = cp_parser_new_declarator_opt (parser);
4261 return build_tree_list (type_specifier_seq, declarator);
4264 /* Parse an (optional) new-declarator.
4266 new-declarator:
4267 ptr-operator new-declarator [opt]
4268 direct-new-declarator
4270 Returns a representation of the declarator. See
4271 cp_parser_declarator for the representations used. */
4273 static tree
4274 cp_parser_new_declarator_opt (cp_parser* parser)
4276 enum tree_code code;
4277 tree type;
4278 tree cv_qualifier_seq;
4280 /* We don't know if there's a ptr-operator next, or not. */
4281 cp_parser_parse_tentatively (parser);
4282 /* Look for a ptr-operator. */
4283 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4284 /* If that worked, look for more new-declarators. */
4285 if (cp_parser_parse_definitely (parser))
4287 tree declarator;
4289 /* Parse another optional declarator. */
4290 declarator = cp_parser_new_declarator_opt (parser);
4292 /* Create the representation of the declarator. */
4293 if (code == INDIRECT_REF)
4294 declarator = make_pointer_declarator (cv_qualifier_seq,
4295 declarator);
4296 else
4297 declarator = make_reference_declarator (cv_qualifier_seq,
4298 declarator);
4300 /* Handle the pointer-to-member case. */
4301 if (type)
4302 declarator = build_nt (SCOPE_REF, type, declarator);
4304 return declarator;
4307 /* If the next token is a `[', there is a direct-new-declarator. */
4308 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4309 return cp_parser_direct_new_declarator (parser);
4311 return NULL_TREE;
4314 /* Parse a direct-new-declarator.
4316 direct-new-declarator:
4317 [ expression ]
4318 direct-new-declarator [constant-expression]
4320 Returns an ARRAY_REF, following the same conventions as are
4321 documented for cp_parser_direct_declarator. */
4323 static tree
4324 cp_parser_direct_new_declarator (cp_parser* parser)
4326 tree declarator = NULL_TREE;
4328 while (true)
4330 tree expression;
4332 /* Look for the opening `['. */
4333 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4334 /* The first expression is not required to be constant. */
4335 if (!declarator)
4337 expression = cp_parser_expression (parser);
4338 /* The standard requires that the expression have integral
4339 type. DR 74 adds enumeration types. We believe that the
4340 real intent is that these expressions be handled like the
4341 expression in a `switch' condition, which also allows
4342 classes with a single conversion to integral or
4343 enumeration type. */
4344 if (!processing_template_decl)
4346 expression
4347 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4348 expression,
4349 /*complain=*/true);
4350 if (!expression)
4352 error ("expression in new-declarator must have integral or enumeration type");
4353 expression = error_mark_node;
4357 /* But all the other expressions must be. */
4358 else
4359 expression
4360 = cp_parser_constant_expression (parser,
4361 /*allow_non_constant=*/false,
4362 NULL);
4363 /* Look for the closing `]'. */
4364 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4366 /* Add this bound to the declarator. */
4367 declarator = build_nt (ARRAY_REF, declarator, expression);
4369 /* If the next token is not a `[', then there are no more
4370 bounds. */
4371 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4372 break;
4375 return declarator;
4378 /* Parse a new-initializer.
4380 new-initializer:
4381 ( expression-list [opt] )
4383 Returns a representation of the expression-list. If there is no
4384 expression-list, VOID_ZERO_NODE is returned. */
4386 static tree
4387 cp_parser_new_initializer (cp_parser* parser)
4389 tree expression_list;
4391 expression_list = (cp_parser_parenthesized_expression_list
4392 (parser, false, /*non_constant_p=*/NULL));
4393 if (!expression_list)
4394 expression_list = void_zero_node;
4396 return expression_list;
4399 /* Parse a delete-expression.
4401 delete-expression:
4402 :: [opt] delete cast-expression
4403 :: [opt] delete [ ] cast-expression
4405 Returns a representation of the expression. */
4407 static tree
4408 cp_parser_delete_expression (cp_parser* parser)
4410 bool global_scope_p;
4411 bool array_p;
4412 tree expression;
4414 /* Look for the optional `::' operator. */
4415 global_scope_p
4416 = (cp_parser_global_scope_opt (parser,
4417 /*current_scope_valid_p=*/false)
4418 != NULL_TREE);
4419 /* Look for the `delete' keyword. */
4420 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4421 /* See if the array syntax is in use. */
4422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4424 /* Consume the `[' token. */
4425 cp_lexer_consume_token (parser->lexer);
4426 /* Look for the `]' token. */
4427 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4428 /* Remember that this is the `[]' construct. */
4429 array_p = true;
4431 else
4432 array_p = false;
4434 /* Parse the cast-expression. */
4435 expression = cp_parser_simple_cast_expression (parser);
4437 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4440 /* Parse a cast-expression.
4442 cast-expression:
4443 unary-expression
4444 ( type-id ) cast-expression
4446 Returns a representation of the expression. */
4448 static tree
4449 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4451 /* If it's a `(', then we might be looking at a cast. */
4452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4454 tree type = NULL_TREE;
4455 tree expr = NULL_TREE;
4456 bool compound_literal_p;
4457 const char *saved_message;
4459 /* There's no way to know yet whether or not this is a cast.
4460 For example, `(int (3))' is a unary-expression, while `(int)
4461 3' is a cast. So, we resort to parsing tentatively. */
4462 cp_parser_parse_tentatively (parser);
4463 /* Types may not be defined in a cast. */
4464 saved_message = parser->type_definition_forbidden_message;
4465 parser->type_definition_forbidden_message
4466 = "types may not be defined in casts";
4467 /* Consume the `('. */
4468 cp_lexer_consume_token (parser->lexer);
4469 /* A very tricky bit is that `(struct S) { 3 }' is a
4470 compound-literal (which we permit in C++ as an extension).
4471 But, that construct is not a cast-expression -- it is a
4472 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4473 is legal; if the compound-literal were a cast-expression,
4474 you'd need an extra set of parentheses.) But, if we parse
4475 the type-id, and it happens to be a class-specifier, then we
4476 will commit to the parse at that point, because we cannot
4477 undo the action that is done when creating a new class. So,
4478 then we cannot back up and do a postfix-expression.
4480 Therefore, we scan ahead to the closing `)', and check to see
4481 if the token after the `)' is a `{'. If so, we are not
4482 looking at a cast-expression.
4484 Save tokens so that we can put them back. */
4485 cp_lexer_save_tokens (parser->lexer);
4486 /* Skip tokens until the next token is a closing parenthesis.
4487 If we find the closing `)', and the next token is a `{', then
4488 we are looking at a compound-literal. */
4489 compound_literal_p
4490 = (cp_parser_skip_to_closing_parenthesis (parser, false, false)
4491 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4492 /* Roll back the tokens we skipped. */
4493 cp_lexer_rollback_tokens (parser->lexer);
4494 /* If we were looking at a compound-literal, simulate an error
4495 so that the call to cp_parser_parse_definitely below will
4496 fail. */
4497 if (compound_literal_p)
4498 cp_parser_simulate_error (parser);
4499 else
4501 /* Look for the type-id. */
4502 type = cp_parser_type_id (parser);
4503 /* Look for the closing `)'. */
4504 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4507 /* Restore the saved message. */
4508 parser->type_definition_forbidden_message = saved_message;
4510 /* If ok so far, parse the dependent expression. We cannot be
4511 sure it is a cast. Consider `(T ())'. It is a parenthesized
4512 ctor of T, but looks like a cast to function returning T
4513 without a dependent expression. */
4514 if (!cp_parser_error_occurred (parser))
4515 expr = cp_parser_simple_cast_expression (parser);
4517 if (cp_parser_parse_definitely (parser))
4519 /* Warn about old-style casts, if so requested. */
4520 if (warn_old_style_cast
4521 && !in_system_header
4522 && !VOID_TYPE_P (type)
4523 && current_lang_name != lang_name_c)
4524 warning ("use of old-style cast");
4526 /* Only type conversions to integral or enumeration types
4527 can be used in constant-expressions. */
4528 if (parser->constant_expression_p
4529 && !dependent_type_p (type)
4530 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4532 if (!parser->allow_non_constant_expression_p)
4533 return (cp_parser_non_constant_expression
4534 ("a casts to a type other than an integral or "
4535 "enumeration type"));
4536 parser->non_constant_expression_p = true;
4538 /* Perform the cast. */
4539 expr = build_c_cast (type, expr);
4540 return expr;
4544 /* If we get here, then it's not a cast, so it must be a
4545 unary-expression. */
4546 return cp_parser_unary_expression (parser, address_p);
4549 /* Parse a pm-expression.
4551 pm-expression:
4552 cast-expression
4553 pm-expression .* cast-expression
4554 pm-expression ->* cast-expression
4556 Returns a representation of the expression. */
4558 static tree
4559 cp_parser_pm_expression (cp_parser* parser)
4561 static const cp_parser_token_tree_map map = {
4562 { CPP_DEREF_STAR, MEMBER_REF },
4563 { CPP_DOT_STAR, DOTSTAR_EXPR },
4564 { CPP_EOF, ERROR_MARK }
4567 return cp_parser_binary_expression (parser, map,
4568 cp_parser_simple_cast_expression);
4571 /* Parse a multiplicative-expression.
4573 mulitplicative-expression:
4574 pm-expression
4575 multiplicative-expression * pm-expression
4576 multiplicative-expression / pm-expression
4577 multiplicative-expression % pm-expression
4579 Returns a representation of the expression. */
4581 static tree
4582 cp_parser_multiplicative_expression (cp_parser* parser)
4584 static const cp_parser_token_tree_map map = {
4585 { CPP_MULT, MULT_EXPR },
4586 { CPP_DIV, TRUNC_DIV_EXPR },
4587 { CPP_MOD, TRUNC_MOD_EXPR },
4588 { CPP_EOF, ERROR_MARK }
4591 return cp_parser_binary_expression (parser,
4592 map,
4593 cp_parser_pm_expression);
4596 /* Parse an additive-expression.
4598 additive-expression:
4599 multiplicative-expression
4600 additive-expression + multiplicative-expression
4601 additive-expression - multiplicative-expression
4603 Returns a representation of the expression. */
4605 static tree
4606 cp_parser_additive_expression (cp_parser* parser)
4608 static const cp_parser_token_tree_map map = {
4609 { CPP_PLUS, PLUS_EXPR },
4610 { CPP_MINUS, MINUS_EXPR },
4611 { CPP_EOF, ERROR_MARK }
4614 return cp_parser_binary_expression (parser,
4615 map,
4616 cp_parser_multiplicative_expression);
4619 /* Parse a shift-expression.
4621 shift-expression:
4622 additive-expression
4623 shift-expression << additive-expression
4624 shift-expression >> additive-expression
4626 Returns a representation of the expression. */
4628 static tree
4629 cp_parser_shift_expression (cp_parser* parser)
4631 static const cp_parser_token_tree_map map = {
4632 { CPP_LSHIFT, LSHIFT_EXPR },
4633 { CPP_RSHIFT, RSHIFT_EXPR },
4634 { CPP_EOF, ERROR_MARK }
4637 return cp_parser_binary_expression (parser,
4638 map,
4639 cp_parser_additive_expression);
4642 /* Parse a relational-expression.
4644 relational-expression:
4645 shift-expression
4646 relational-expression < shift-expression
4647 relational-expression > shift-expression
4648 relational-expression <= shift-expression
4649 relational-expression >= shift-expression
4651 GNU Extension:
4653 relational-expression:
4654 relational-expression <? shift-expression
4655 relational-expression >? shift-expression
4657 Returns a representation of the expression. */
4659 static tree
4660 cp_parser_relational_expression (cp_parser* parser)
4662 static const cp_parser_token_tree_map map = {
4663 { CPP_LESS, LT_EXPR },
4664 { CPP_GREATER, GT_EXPR },
4665 { CPP_LESS_EQ, LE_EXPR },
4666 { CPP_GREATER_EQ, GE_EXPR },
4667 { CPP_MIN, MIN_EXPR },
4668 { CPP_MAX, MAX_EXPR },
4669 { CPP_EOF, ERROR_MARK }
4672 return cp_parser_binary_expression (parser,
4673 map,
4674 cp_parser_shift_expression);
4677 /* Parse an equality-expression.
4679 equality-expression:
4680 relational-expression
4681 equality-expression == relational-expression
4682 equality-expression != relational-expression
4684 Returns a representation of the expression. */
4686 static tree
4687 cp_parser_equality_expression (cp_parser* parser)
4689 static const cp_parser_token_tree_map map = {
4690 { CPP_EQ_EQ, EQ_EXPR },
4691 { CPP_NOT_EQ, NE_EXPR },
4692 { CPP_EOF, ERROR_MARK }
4695 return cp_parser_binary_expression (parser,
4696 map,
4697 cp_parser_relational_expression);
4700 /* Parse an and-expression.
4702 and-expression:
4703 equality-expression
4704 and-expression & equality-expression
4706 Returns a representation of the expression. */
4708 static tree
4709 cp_parser_and_expression (cp_parser* parser)
4711 static const cp_parser_token_tree_map map = {
4712 { CPP_AND, BIT_AND_EXPR },
4713 { CPP_EOF, ERROR_MARK }
4716 return cp_parser_binary_expression (parser,
4717 map,
4718 cp_parser_equality_expression);
4721 /* Parse an exclusive-or-expression.
4723 exclusive-or-expression:
4724 and-expression
4725 exclusive-or-expression ^ and-expression
4727 Returns a representation of the expression. */
4729 static tree
4730 cp_parser_exclusive_or_expression (cp_parser* parser)
4732 static const cp_parser_token_tree_map map = {
4733 { CPP_XOR, BIT_XOR_EXPR },
4734 { CPP_EOF, ERROR_MARK }
4737 return cp_parser_binary_expression (parser,
4738 map,
4739 cp_parser_and_expression);
4743 /* Parse an inclusive-or-expression.
4745 inclusive-or-expression:
4746 exclusive-or-expression
4747 inclusive-or-expression | exclusive-or-expression
4749 Returns a representation of the expression. */
4751 static tree
4752 cp_parser_inclusive_or_expression (cp_parser* parser)
4754 static const cp_parser_token_tree_map map = {
4755 { CPP_OR, BIT_IOR_EXPR },
4756 { CPP_EOF, ERROR_MARK }
4759 return cp_parser_binary_expression (parser,
4760 map,
4761 cp_parser_exclusive_or_expression);
4764 /* Parse a logical-and-expression.
4766 logical-and-expression:
4767 inclusive-or-expression
4768 logical-and-expression && inclusive-or-expression
4770 Returns a representation of the expression. */
4772 static tree
4773 cp_parser_logical_and_expression (cp_parser* parser)
4775 static const cp_parser_token_tree_map map = {
4776 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
4777 { CPP_EOF, ERROR_MARK }
4780 return cp_parser_binary_expression (parser,
4781 map,
4782 cp_parser_inclusive_or_expression);
4785 /* Parse a logical-or-expression.
4787 logical-or-expression:
4788 logical-and-expression
4789 logical-or-expression || logical-and-expression
4791 Returns a representation of the expression. */
4793 static tree
4794 cp_parser_logical_or_expression (cp_parser* parser)
4796 static const cp_parser_token_tree_map map = {
4797 { CPP_OR_OR, TRUTH_ORIF_EXPR },
4798 { CPP_EOF, ERROR_MARK }
4801 return cp_parser_binary_expression (parser,
4802 map,
4803 cp_parser_logical_and_expression);
4806 /* Parse the `? expression : assignment-expression' part of a
4807 conditional-expression. The LOGICAL_OR_EXPR is the
4808 logical-or-expression that started the conditional-expression.
4809 Returns a representation of the entire conditional-expression.
4811 This routine is used by cp_parser_assignment_expression.
4813 ? expression : assignment-expression
4815 GNU Extensions:
4817 ? : assignment-expression */
4819 static tree
4820 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
4822 tree expr;
4823 tree assignment_expr;
4825 /* Consume the `?' token. */
4826 cp_lexer_consume_token (parser->lexer);
4827 if (cp_parser_allow_gnu_extensions_p (parser)
4828 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
4829 /* Implicit true clause. */
4830 expr = NULL_TREE;
4831 else
4832 /* Parse the expression. */
4833 expr = cp_parser_expression (parser);
4835 /* The next token should be a `:'. */
4836 cp_parser_require (parser, CPP_COLON, "`:'");
4837 /* Parse the assignment-expression. */
4838 assignment_expr = cp_parser_assignment_expression (parser);
4840 /* Build the conditional-expression. */
4841 return build_x_conditional_expr (logical_or_expr,
4842 expr,
4843 assignment_expr);
4846 /* Parse an assignment-expression.
4848 assignment-expression:
4849 conditional-expression
4850 logical-or-expression assignment-operator assignment_expression
4851 throw-expression
4853 Returns a representation for the expression. */
4855 static tree
4856 cp_parser_assignment_expression (cp_parser* parser)
4858 tree expr;
4860 /* If the next token is the `throw' keyword, then we're looking at
4861 a throw-expression. */
4862 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
4863 expr = cp_parser_throw_expression (parser);
4864 /* Otherwise, it must be that we are looking at a
4865 logical-or-expression. */
4866 else
4868 /* Parse the logical-or-expression. */
4869 expr = cp_parser_logical_or_expression (parser);
4870 /* If the next token is a `?' then we're actually looking at a
4871 conditional-expression. */
4872 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
4873 return cp_parser_question_colon_clause (parser, expr);
4874 else
4876 enum tree_code assignment_operator;
4878 /* If it's an assignment-operator, we're using the second
4879 production. */
4880 assignment_operator
4881 = cp_parser_assignment_operator_opt (parser);
4882 if (assignment_operator != ERROR_MARK)
4884 tree rhs;
4886 /* Parse the right-hand side of the assignment. */
4887 rhs = cp_parser_assignment_expression (parser);
4888 /* An assignment may not appear in a
4889 constant-expression. */
4890 if (parser->constant_expression_p)
4892 if (!parser->allow_non_constant_expression_p)
4893 return cp_parser_non_constant_expression ("an assignment");
4894 parser->non_constant_expression_p = true;
4896 /* Build the assignment expression. */
4897 expr = build_x_modify_expr (expr,
4898 assignment_operator,
4899 rhs);
4904 return expr;
4907 /* Parse an (optional) assignment-operator.
4909 assignment-operator: one of
4910 = *= /= %= += -= >>= <<= &= ^= |=
4912 GNU Extension:
4914 assignment-operator: one of
4915 <?= >?=
4917 If the next token is an assignment operator, the corresponding tree
4918 code is returned, and the token is consumed. For example, for
4919 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
4920 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
4921 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
4922 operator, ERROR_MARK is returned. */
4924 static enum tree_code
4925 cp_parser_assignment_operator_opt (cp_parser* parser)
4927 enum tree_code op;
4928 cp_token *token;
4930 /* Peek at the next toen. */
4931 token = cp_lexer_peek_token (parser->lexer);
4933 switch (token->type)
4935 case CPP_EQ:
4936 op = NOP_EXPR;
4937 break;
4939 case CPP_MULT_EQ:
4940 op = MULT_EXPR;
4941 break;
4943 case CPP_DIV_EQ:
4944 op = TRUNC_DIV_EXPR;
4945 break;
4947 case CPP_MOD_EQ:
4948 op = TRUNC_MOD_EXPR;
4949 break;
4951 case CPP_PLUS_EQ:
4952 op = PLUS_EXPR;
4953 break;
4955 case CPP_MINUS_EQ:
4956 op = MINUS_EXPR;
4957 break;
4959 case CPP_RSHIFT_EQ:
4960 op = RSHIFT_EXPR;
4961 break;
4963 case CPP_LSHIFT_EQ:
4964 op = LSHIFT_EXPR;
4965 break;
4967 case CPP_AND_EQ:
4968 op = BIT_AND_EXPR;
4969 break;
4971 case CPP_XOR_EQ:
4972 op = BIT_XOR_EXPR;
4973 break;
4975 case CPP_OR_EQ:
4976 op = BIT_IOR_EXPR;
4977 break;
4979 case CPP_MIN_EQ:
4980 op = MIN_EXPR;
4981 break;
4983 case CPP_MAX_EQ:
4984 op = MAX_EXPR;
4985 break;
4987 default:
4988 /* Nothing else is an assignment operator. */
4989 op = ERROR_MARK;
4992 /* If it was an assignment operator, consume it. */
4993 if (op != ERROR_MARK)
4994 cp_lexer_consume_token (parser->lexer);
4996 return op;
4999 /* Parse an expression.
5001 expression:
5002 assignment-expression
5003 expression , assignment-expression
5005 Returns a representation of the expression. */
5007 static tree
5008 cp_parser_expression (cp_parser* parser)
5010 tree expression = NULL_TREE;
5012 while (true)
5014 tree assignment_expression;
5016 /* Parse the next assignment-expression. */
5017 assignment_expression
5018 = cp_parser_assignment_expression (parser);
5019 /* If this is the first assignment-expression, we can just
5020 save it away. */
5021 if (!expression)
5022 expression = assignment_expression;
5023 else
5024 expression = build_x_compound_expr (expression,
5025 assignment_expression);
5026 /* If the next token is not a comma, then we are done with the
5027 expression. */
5028 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5029 break;
5030 /* Consume the `,'. */
5031 cp_lexer_consume_token (parser->lexer);
5032 /* A comma operator cannot appear in a constant-expression. */
5033 if (parser->constant_expression_p)
5035 if (!parser->allow_non_constant_expression_p)
5036 expression
5037 = cp_parser_non_constant_expression ("a comma operator");
5038 parser->non_constant_expression_p = true;
5042 return expression;
5045 /* Parse a constant-expression.
5047 constant-expression:
5048 conditional-expression
5050 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5051 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5052 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5053 is false, NON_CONSTANT_P should be NULL. */
5055 static tree
5056 cp_parser_constant_expression (cp_parser* parser,
5057 bool allow_non_constant_p,
5058 bool *non_constant_p)
5060 bool saved_constant_expression_p;
5061 bool saved_allow_non_constant_expression_p;
5062 bool saved_non_constant_expression_p;
5063 tree expression;
5065 /* It might seem that we could simply parse the
5066 conditional-expression, and then check to see if it were
5067 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5068 one that the compiler can figure out is constant, possibly after
5069 doing some simplifications or optimizations. The standard has a
5070 precise definition of constant-expression, and we must honor
5071 that, even though it is somewhat more restrictive.
5073 For example:
5075 int i[(2, 3)];
5077 is not a legal declaration, because `(2, 3)' is not a
5078 constant-expression. The `,' operator is forbidden in a
5079 constant-expression. However, GCC's constant-folding machinery
5080 will fold this operation to an INTEGER_CST for `3'. */
5082 /* Save the old settings. */
5083 saved_constant_expression_p = parser->constant_expression_p;
5084 saved_allow_non_constant_expression_p
5085 = parser->allow_non_constant_expression_p;
5086 saved_non_constant_expression_p = parser->non_constant_expression_p;
5087 /* We are now parsing a constant-expression. */
5088 parser->constant_expression_p = true;
5089 parser->allow_non_constant_expression_p = allow_non_constant_p;
5090 parser->non_constant_expression_p = false;
5091 /* Although the grammar says "conditional-expression", we parse an
5092 "assignment-expression", which also permits "throw-expression"
5093 and the use of assignment operators. In the case that
5094 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5095 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5096 actually essential that we look for an assignment-expression.
5097 For example, cp_parser_initializer_clauses uses this function to
5098 determine whether a particular assignment-expression is in fact
5099 constant. */
5100 expression = cp_parser_assignment_expression (parser);
5101 /* Restore the old settings. */
5102 parser->constant_expression_p = saved_constant_expression_p;
5103 parser->allow_non_constant_expression_p
5104 = saved_allow_non_constant_expression_p;
5105 if (allow_non_constant_p)
5106 *non_constant_p = parser->non_constant_expression_p;
5107 parser->non_constant_expression_p = saved_non_constant_expression_p;
5109 return expression;
5112 /* Statements [gram.stmt.stmt] */
5114 /* Parse a statement.
5116 statement:
5117 labeled-statement
5118 expression-statement
5119 compound-statement
5120 selection-statement
5121 iteration-statement
5122 jump-statement
5123 declaration-statement
5124 try-block */
5126 static void
5127 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5129 tree statement;
5130 cp_token *token;
5131 int statement_line_number;
5133 /* There is no statement yet. */
5134 statement = NULL_TREE;
5135 /* Peek at the next token. */
5136 token = cp_lexer_peek_token (parser->lexer);
5137 /* Remember the line number of the first token in the statement. */
5138 statement_line_number = token->location.line;
5139 /* If this is a keyword, then that will often determine what kind of
5140 statement we have. */
5141 if (token->type == CPP_KEYWORD)
5143 enum rid keyword = token->keyword;
5145 switch (keyword)
5147 case RID_CASE:
5148 case RID_DEFAULT:
5149 statement = cp_parser_labeled_statement (parser,
5150 in_statement_expr_p);
5151 break;
5153 case RID_IF:
5154 case RID_SWITCH:
5155 statement = cp_parser_selection_statement (parser);
5156 break;
5158 case RID_WHILE:
5159 case RID_DO:
5160 case RID_FOR:
5161 statement = cp_parser_iteration_statement (parser);
5162 break;
5164 case RID_BREAK:
5165 case RID_CONTINUE:
5166 case RID_RETURN:
5167 case RID_GOTO:
5168 statement = cp_parser_jump_statement (parser);
5169 break;
5171 case RID_TRY:
5172 statement = cp_parser_try_block (parser);
5173 break;
5175 default:
5176 /* It might be a keyword like `int' that can start a
5177 declaration-statement. */
5178 break;
5181 else if (token->type == CPP_NAME)
5183 /* If the next token is a `:', then we are looking at a
5184 labeled-statement. */
5185 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5186 if (token->type == CPP_COLON)
5187 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5189 /* Anything that starts with a `{' must be a compound-statement. */
5190 else if (token->type == CPP_OPEN_BRACE)
5191 statement = cp_parser_compound_statement (parser, false);
5193 /* Everything else must be a declaration-statement or an
5194 expression-statement. Try for the declaration-statement
5195 first, unless we are looking at a `;', in which case we know that
5196 we have an expression-statement. */
5197 if (!statement)
5199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5201 cp_parser_parse_tentatively (parser);
5202 /* Try to parse the declaration-statement. */
5203 cp_parser_declaration_statement (parser);
5204 /* If that worked, we're done. */
5205 if (cp_parser_parse_definitely (parser))
5206 return;
5208 /* Look for an expression-statement instead. */
5209 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5212 /* Set the line number for the statement. */
5213 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5214 STMT_LINENO (statement) = statement_line_number;
5217 /* Parse a labeled-statement.
5219 labeled-statement:
5220 identifier : statement
5221 case constant-expression : statement
5222 default : statement
5224 Returns the new CASE_LABEL, for a `case' or `default' label. For
5225 an ordinary label, returns a LABEL_STMT. */
5227 static tree
5228 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5230 cp_token *token;
5231 tree statement = NULL_TREE;
5233 /* The next token should be an identifier. */
5234 token = cp_lexer_peek_token (parser->lexer);
5235 if (token->type != CPP_NAME
5236 && token->type != CPP_KEYWORD)
5238 cp_parser_error (parser, "expected labeled-statement");
5239 return error_mark_node;
5242 switch (token->keyword)
5244 case RID_CASE:
5246 tree expr;
5248 /* Consume the `case' token. */
5249 cp_lexer_consume_token (parser->lexer);
5250 /* Parse the constant-expression. */
5251 expr = cp_parser_constant_expression (parser,
5252 /*allow_non_constant_p=*/false,
5253 NULL);
5254 /* Create the label. */
5255 statement = finish_case_label (expr, NULL_TREE);
5257 break;
5259 case RID_DEFAULT:
5260 /* Consume the `default' token. */
5261 cp_lexer_consume_token (parser->lexer);
5262 /* Create the label. */
5263 statement = finish_case_label (NULL_TREE, NULL_TREE);
5264 break;
5266 default:
5267 /* Anything else must be an ordinary label. */
5268 statement = finish_label_stmt (cp_parser_identifier (parser));
5269 break;
5272 /* Require the `:' token. */
5273 cp_parser_require (parser, CPP_COLON, "`:'");
5274 /* Parse the labeled statement. */
5275 cp_parser_statement (parser, in_statement_expr_p);
5277 /* Return the label, in the case of a `case' or `default' label. */
5278 return statement;
5281 /* Parse an expression-statement.
5283 expression-statement:
5284 expression [opt] ;
5286 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5287 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5288 indicates whether this expression-statement is part of an
5289 expression statement. */
5291 static tree
5292 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5294 tree statement = NULL_TREE;
5296 /* If the next token is a ';', then there is no expression
5297 statement. */
5298 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5299 statement = cp_parser_expression (parser);
5301 /* Consume the final `;'. */
5302 cp_parser_consume_semicolon_at_end_of_statement (parser);
5304 if (in_statement_expr_p
5305 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5307 /* This is the final expression statement of a statement
5308 expression. */
5309 statement = finish_stmt_expr_expr (statement);
5311 else if (statement)
5312 statement = finish_expr_stmt (statement);
5313 else
5314 finish_stmt ();
5316 return statement;
5319 /* Parse a compound-statement.
5321 compound-statement:
5322 { statement-seq [opt] }
5324 Returns a COMPOUND_STMT representing the statement. */
5326 static tree
5327 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5329 tree compound_stmt;
5331 /* Consume the `{'. */
5332 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5333 return error_mark_node;
5334 /* Begin the compound-statement. */
5335 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5336 /* Parse an (optional) statement-seq. */
5337 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5338 /* Finish the compound-statement. */
5339 finish_compound_stmt (compound_stmt);
5340 /* Consume the `}'. */
5341 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5343 return compound_stmt;
5346 /* Parse an (optional) statement-seq.
5348 statement-seq:
5349 statement
5350 statement-seq [opt] statement */
5352 static void
5353 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5355 /* Scan statements until there aren't any more. */
5356 while (true)
5358 /* If we're looking at a `}', then we've run out of statements. */
5359 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5360 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5361 break;
5363 /* Parse the statement. */
5364 cp_parser_statement (parser, in_statement_expr_p);
5368 /* Parse a selection-statement.
5370 selection-statement:
5371 if ( condition ) statement
5372 if ( condition ) statement else statement
5373 switch ( condition ) statement
5375 Returns the new IF_STMT or SWITCH_STMT. */
5377 static tree
5378 cp_parser_selection_statement (cp_parser* parser)
5380 cp_token *token;
5381 enum rid keyword;
5383 /* Peek at the next token. */
5384 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5386 /* See what kind of keyword it is. */
5387 keyword = token->keyword;
5388 switch (keyword)
5390 case RID_IF:
5391 case RID_SWITCH:
5393 tree statement;
5394 tree condition;
5396 /* Look for the `('. */
5397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5399 cp_parser_skip_to_end_of_statement (parser);
5400 return error_mark_node;
5403 /* Begin the selection-statement. */
5404 if (keyword == RID_IF)
5405 statement = begin_if_stmt ();
5406 else
5407 statement = begin_switch_stmt ();
5409 /* Parse the condition. */
5410 condition = cp_parser_condition (parser);
5411 /* Look for the `)'. */
5412 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5413 cp_parser_skip_to_closing_parenthesis (parser, true, false);
5415 if (keyword == RID_IF)
5417 tree then_stmt;
5419 /* Add the condition. */
5420 finish_if_stmt_cond (condition, statement);
5422 /* Parse the then-clause. */
5423 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5424 finish_then_clause (statement);
5426 /* If the next token is `else', parse the else-clause. */
5427 if (cp_lexer_next_token_is_keyword (parser->lexer,
5428 RID_ELSE))
5430 tree else_stmt;
5432 /* Consume the `else' keyword. */
5433 cp_lexer_consume_token (parser->lexer);
5434 /* Parse the else-clause. */
5435 else_stmt
5436 = cp_parser_implicitly_scoped_statement (parser);
5437 finish_else_clause (statement);
5440 /* Now we're all done with the if-statement. */
5441 finish_if_stmt ();
5443 else
5445 tree body;
5447 /* Add the condition. */
5448 finish_switch_cond (condition, statement);
5450 /* Parse the body of the switch-statement. */
5451 body = cp_parser_implicitly_scoped_statement (parser);
5453 /* Now we're all done with the switch-statement. */
5454 finish_switch_stmt (statement);
5457 return statement;
5459 break;
5461 default:
5462 cp_parser_error (parser, "expected selection-statement");
5463 return error_mark_node;
5467 /* Parse a condition.
5469 condition:
5470 expression
5471 type-specifier-seq declarator = assignment-expression
5473 GNU Extension:
5475 condition:
5476 type-specifier-seq declarator asm-specification [opt]
5477 attributes [opt] = assignment-expression
5479 Returns the expression that should be tested. */
5481 static tree
5482 cp_parser_condition (cp_parser* parser)
5484 tree type_specifiers;
5485 const char *saved_message;
5487 /* Try the declaration first. */
5488 cp_parser_parse_tentatively (parser);
5489 /* New types are not allowed in the type-specifier-seq for a
5490 condition. */
5491 saved_message = parser->type_definition_forbidden_message;
5492 parser->type_definition_forbidden_message
5493 = "types may not be defined in conditions";
5494 /* Parse the type-specifier-seq. */
5495 type_specifiers = cp_parser_type_specifier_seq (parser);
5496 /* Restore the saved message. */
5497 parser->type_definition_forbidden_message = saved_message;
5498 /* If all is well, we might be looking at a declaration. */
5499 if (!cp_parser_error_occurred (parser))
5501 tree decl;
5502 tree asm_specification;
5503 tree attributes;
5504 tree declarator;
5505 tree initializer = NULL_TREE;
5507 /* Parse the declarator. */
5508 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5509 /*ctor_dtor_or_conv_p=*/NULL);
5510 /* Parse the attributes. */
5511 attributes = cp_parser_attributes_opt (parser);
5512 /* Parse the asm-specification. */
5513 asm_specification = cp_parser_asm_specification_opt (parser);
5514 /* If the next token is not an `=', then we might still be
5515 looking at an expression. For example:
5517 if (A(a).x)
5519 looks like a decl-specifier-seq and a declarator -- but then
5520 there is no `=', so this is an expression. */
5521 cp_parser_require (parser, CPP_EQ, "`='");
5522 /* If we did see an `=', then we are looking at a declaration
5523 for sure. */
5524 if (cp_parser_parse_definitely (parser))
5526 /* Create the declaration. */
5527 decl = start_decl (declarator, type_specifiers,
5528 /*initialized_p=*/true,
5529 attributes, /*prefix_attributes=*/NULL_TREE);
5530 /* Parse the assignment-expression. */
5531 initializer = cp_parser_assignment_expression (parser);
5533 /* Process the initializer. */
5534 cp_finish_decl (decl,
5535 initializer,
5536 asm_specification,
5537 LOOKUP_ONLYCONVERTING);
5539 return convert_from_reference (decl);
5542 /* If we didn't even get past the declarator successfully, we are
5543 definitely not looking at a declaration. */
5544 else
5545 cp_parser_abort_tentative_parse (parser);
5547 /* Otherwise, we are looking at an expression. */
5548 return cp_parser_expression (parser);
5551 /* Parse an iteration-statement.
5553 iteration-statement:
5554 while ( condition ) statement
5555 do statement while ( expression ) ;
5556 for ( for-init-statement condition [opt] ; expression [opt] )
5557 statement
5559 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5561 static tree
5562 cp_parser_iteration_statement (cp_parser* parser)
5564 cp_token *token;
5565 enum rid keyword;
5566 tree statement;
5568 /* Peek at the next token. */
5569 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5570 if (!token)
5571 return error_mark_node;
5573 /* See what kind of keyword it is. */
5574 keyword = token->keyword;
5575 switch (keyword)
5577 case RID_WHILE:
5579 tree condition;
5581 /* Begin the while-statement. */
5582 statement = begin_while_stmt ();
5583 /* Look for the `('. */
5584 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5585 /* Parse the condition. */
5586 condition = cp_parser_condition (parser);
5587 finish_while_stmt_cond (condition, statement);
5588 /* Look for the `)'. */
5589 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5590 /* Parse the dependent statement. */
5591 cp_parser_already_scoped_statement (parser);
5592 /* We're done with the while-statement. */
5593 finish_while_stmt (statement);
5595 break;
5597 case RID_DO:
5599 tree expression;
5601 /* Begin the do-statement. */
5602 statement = begin_do_stmt ();
5603 /* Parse the body of the do-statement. */
5604 cp_parser_implicitly_scoped_statement (parser);
5605 finish_do_body (statement);
5606 /* Look for the `while' keyword. */
5607 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5608 /* Look for the `('. */
5609 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5610 /* Parse the expression. */
5611 expression = cp_parser_expression (parser);
5612 /* We're done with the do-statement. */
5613 finish_do_stmt (expression, statement);
5614 /* Look for the `)'. */
5615 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5616 /* Look for the `;'. */
5617 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5619 break;
5621 case RID_FOR:
5623 tree condition = NULL_TREE;
5624 tree expression = NULL_TREE;
5626 /* Begin the for-statement. */
5627 statement = begin_for_stmt ();
5628 /* Look for the `('. */
5629 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5630 /* Parse the initialization. */
5631 cp_parser_for_init_statement (parser);
5632 finish_for_init_stmt (statement);
5634 /* If there's a condition, process it. */
5635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5636 condition = cp_parser_condition (parser);
5637 finish_for_cond (condition, statement);
5638 /* Look for the `;'. */
5639 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5641 /* If there's an expression, process it. */
5642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5643 expression = cp_parser_expression (parser);
5644 finish_for_expr (expression, statement);
5645 /* Look for the `)'. */
5646 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5648 /* Parse the body of the for-statement. */
5649 cp_parser_already_scoped_statement (parser);
5651 /* We're done with the for-statement. */
5652 finish_for_stmt (statement);
5654 break;
5656 default:
5657 cp_parser_error (parser, "expected iteration-statement");
5658 statement = error_mark_node;
5659 break;
5662 return statement;
5665 /* Parse a for-init-statement.
5667 for-init-statement:
5668 expression-statement
5669 simple-declaration */
5671 static void
5672 cp_parser_for_init_statement (cp_parser* parser)
5674 /* If the next token is a `;', then we have an empty
5675 expression-statement. Grammatically, this is also a
5676 simple-declaration, but an invalid one, because it does not
5677 declare anything. Therefore, if we did not handle this case
5678 specially, we would issue an error message about an invalid
5679 declaration. */
5680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5682 /* We're going to speculatively look for a declaration, falling back
5683 to an expression, if necessary. */
5684 cp_parser_parse_tentatively (parser);
5685 /* Parse the declaration. */
5686 cp_parser_simple_declaration (parser,
5687 /*function_definition_allowed_p=*/false);
5688 /* If the tentative parse failed, then we shall need to look for an
5689 expression-statement. */
5690 if (cp_parser_parse_definitely (parser))
5691 return;
5694 cp_parser_expression_statement (parser, false);
5697 /* Parse a jump-statement.
5699 jump-statement:
5700 break ;
5701 continue ;
5702 return expression [opt] ;
5703 goto identifier ;
5705 GNU extension:
5707 jump-statement:
5708 goto * expression ;
5710 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5711 GOTO_STMT. */
5713 static tree
5714 cp_parser_jump_statement (cp_parser* parser)
5716 tree statement = error_mark_node;
5717 cp_token *token;
5718 enum rid keyword;
5720 /* Peek at the next token. */
5721 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
5722 if (!token)
5723 return error_mark_node;
5725 /* See what kind of keyword it is. */
5726 keyword = token->keyword;
5727 switch (keyword)
5729 case RID_BREAK:
5730 statement = finish_break_stmt ();
5731 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5732 break;
5734 case RID_CONTINUE:
5735 statement = finish_continue_stmt ();
5736 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5737 break;
5739 case RID_RETURN:
5741 tree expr;
5743 /* If the next token is a `;', then there is no
5744 expression. */
5745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5746 expr = cp_parser_expression (parser);
5747 else
5748 expr = NULL_TREE;
5749 /* Build the return-statement. */
5750 statement = finish_return_stmt (expr);
5751 /* Look for the final `;'. */
5752 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5754 break;
5756 case RID_GOTO:
5757 /* Create the goto-statement. */
5758 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
5760 /* Issue a warning about this use of a GNU extension. */
5761 if (pedantic)
5762 pedwarn ("ISO C++ forbids computed gotos");
5763 /* Consume the '*' token. */
5764 cp_lexer_consume_token (parser->lexer);
5765 /* Parse the dependent expression. */
5766 finish_goto_stmt (cp_parser_expression (parser));
5768 else
5769 finish_goto_stmt (cp_parser_identifier (parser));
5770 /* Look for the final `;'. */
5771 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5772 break;
5774 default:
5775 cp_parser_error (parser, "expected jump-statement");
5776 break;
5779 return statement;
5782 /* Parse a declaration-statement.
5784 declaration-statement:
5785 block-declaration */
5787 static void
5788 cp_parser_declaration_statement (cp_parser* parser)
5790 /* Parse the block-declaration. */
5791 cp_parser_block_declaration (parser, /*statement_p=*/true);
5793 /* Finish off the statement. */
5794 finish_stmt ();
5797 /* Some dependent statements (like `if (cond) statement'), are
5798 implicitly in their own scope. In other words, if the statement is
5799 a single statement (as opposed to a compound-statement), it is
5800 none-the-less treated as if it were enclosed in braces. Any
5801 declarations appearing in the dependent statement are out of scope
5802 after control passes that point. This function parses a statement,
5803 but ensures that is in its own scope, even if it is not a
5804 compound-statement.
5806 Returns the new statement. */
5808 static tree
5809 cp_parser_implicitly_scoped_statement (cp_parser* parser)
5811 tree statement;
5813 /* If the token is not a `{', then we must take special action. */
5814 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
5816 /* Create a compound-statement. */
5817 statement = begin_compound_stmt (/*has_no_scope=*/false);
5818 /* Parse the dependent-statement. */
5819 cp_parser_statement (parser, false);
5820 /* Finish the dummy compound-statement. */
5821 finish_compound_stmt (statement);
5823 /* Otherwise, we simply parse the statement directly. */
5824 else
5825 statement = cp_parser_compound_statement (parser, false);
5827 /* Return the statement. */
5828 return statement;
5831 /* For some dependent statements (like `while (cond) statement'), we
5832 have already created a scope. Therefore, even if the dependent
5833 statement is a compound-statement, we do not want to create another
5834 scope. */
5836 static void
5837 cp_parser_already_scoped_statement (cp_parser* parser)
5839 /* If the token is not a `{', then we must take special action. */
5840 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
5842 tree statement;
5844 /* Create a compound-statement. */
5845 statement = begin_compound_stmt (/*has_no_scope=*/true);
5846 /* Parse the dependent-statement. */
5847 cp_parser_statement (parser, false);
5848 /* Finish the dummy compound-statement. */
5849 finish_compound_stmt (statement);
5851 /* Otherwise, we simply parse the statement directly. */
5852 else
5853 cp_parser_statement (parser, false);
5856 /* Declarations [gram.dcl.dcl] */
5858 /* Parse an optional declaration-sequence.
5860 declaration-seq:
5861 declaration
5862 declaration-seq declaration */
5864 static void
5865 cp_parser_declaration_seq_opt (cp_parser* parser)
5867 while (true)
5869 cp_token *token;
5871 token = cp_lexer_peek_token (parser->lexer);
5873 if (token->type == CPP_CLOSE_BRACE
5874 || token->type == CPP_EOF)
5875 break;
5877 if (token->type == CPP_SEMICOLON)
5879 /* A declaration consisting of a single semicolon is
5880 invalid. Allow it unless we're being pedantic. */
5881 if (pedantic)
5882 pedwarn ("extra `;'");
5883 cp_lexer_consume_token (parser->lexer);
5884 continue;
5887 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
5888 parser to enter or exit implicit `extern "C"' blocks. */
5889 while (pending_lang_change > 0)
5891 push_lang_context (lang_name_c);
5892 --pending_lang_change;
5894 while (pending_lang_change < 0)
5896 pop_lang_context ();
5897 ++pending_lang_change;
5900 /* Parse the declaration itself. */
5901 cp_parser_declaration (parser);
5905 /* Parse a declaration.
5907 declaration:
5908 block-declaration
5909 function-definition
5910 template-declaration
5911 explicit-instantiation
5912 explicit-specialization
5913 linkage-specification
5914 namespace-definition
5916 GNU extension:
5918 declaration:
5919 __extension__ declaration */
5921 static void
5922 cp_parser_declaration (cp_parser* parser)
5924 cp_token token1;
5925 cp_token token2;
5926 int saved_pedantic;
5928 /* Check for the `__extension__' keyword. */
5929 if (cp_parser_extension_opt (parser, &saved_pedantic))
5931 /* Parse the qualified declaration. */
5932 cp_parser_declaration (parser);
5933 /* Restore the PEDANTIC flag. */
5934 pedantic = saved_pedantic;
5936 return;
5939 /* Try to figure out what kind of declaration is present. */
5940 token1 = *cp_lexer_peek_token (parser->lexer);
5941 if (token1.type != CPP_EOF)
5942 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
5944 /* If the next token is `extern' and the following token is a string
5945 literal, then we have a linkage specification. */
5946 if (token1.keyword == RID_EXTERN
5947 && cp_parser_is_string_literal (&token2))
5948 cp_parser_linkage_specification (parser);
5949 /* If the next token is `template', then we have either a template
5950 declaration, an explicit instantiation, or an explicit
5951 specialization. */
5952 else if (token1.keyword == RID_TEMPLATE)
5954 /* `template <>' indicates a template specialization. */
5955 if (token2.type == CPP_LESS
5956 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
5957 cp_parser_explicit_specialization (parser);
5958 /* `template <' indicates a template declaration. */
5959 else if (token2.type == CPP_LESS)
5960 cp_parser_template_declaration (parser, /*member_p=*/false);
5961 /* Anything else must be an explicit instantiation. */
5962 else
5963 cp_parser_explicit_instantiation (parser);
5965 /* If the next token is `export', then we have a template
5966 declaration. */
5967 else if (token1.keyword == RID_EXPORT)
5968 cp_parser_template_declaration (parser, /*member_p=*/false);
5969 /* If the next token is `extern', 'static' or 'inline' and the one
5970 after that is `template', we have a GNU extended explicit
5971 instantiation directive. */
5972 else if (cp_parser_allow_gnu_extensions_p (parser)
5973 && (token1.keyword == RID_EXTERN
5974 || token1.keyword == RID_STATIC
5975 || token1.keyword == RID_INLINE)
5976 && token2.keyword == RID_TEMPLATE)
5977 cp_parser_explicit_instantiation (parser);
5978 /* If the next token is `namespace', check for a named or unnamed
5979 namespace definition. */
5980 else if (token1.keyword == RID_NAMESPACE
5981 && (/* A named namespace definition. */
5982 (token2.type == CPP_NAME
5983 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5984 == CPP_OPEN_BRACE))
5985 /* An unnamed namespace definition. */
5986 || token2.type == CPP_OPEN_BRACE))
5987 cp_parser_namespace_definition (parser);
5988 /* We must have either a block declaration or a function
5989 definition. */
5990 else
5991 /* Try to parse a block-declaration, or a function-definition. */
5992 cp_parser_block_declaration (parser, /*statement_p=*/false);
5995 /* Parse a block-declaration.
5997 block-declaration:
5998 simple-declaration
5999 asm-definition
6000 namespace-alias-definition
6001 using-declaration
6002 using-directive
6004 GNU Extension:
6006 block-declaration:
6007 __extension__ block-declaration
6008 label-declaration
6010 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6011 part of a declaration-statement. */
6013 static void
6014 cp_parser_block_declaration (cp_parser *parser,
6015 bool statement_p)
6017 cp_token *token1;
6018 int saved_pedantic;
6020 /* Check for the `__extension__' keyword. */
6021 if (cp_parser_extension_opt (parser, &saved_pedantic))
6023 /* Parse the qualified declaration. */
6024 cp_parser_block_declaration (parser, statement_p);
6025 /* Restore the PEDANTIC flag. */
6026 pedantic = saved_pedantic;
6028 return;
6031 /* Peek at the next token to figure out which kind of declaration is
6032 present. */
6033 token1 = cp_lexer_peek_token (parser->lexer);
6035 /* If the next keyword is `asm', we have an asm-definition. */
6036 if (token1->keyword == RID_ASM)
6038 if (statement_p)
6039 cp_parser_commit_to_tentative_parse (parser);
6040 cp_parser_asm_definition (parser);
6042 /* If the next keyword is `namespace', we have a
6043 namespace-alias-definition. */
6044 else if (token1->keyword == RID_NAMESPACE)
6045 cp_parser_namespace_alias_definition (parser);
6046 /* If the next keyword is `using', we have either a
6047 using-declaration or a using-directive. */
6048 else if (token1->keyword == RID_USING)
6050 cp_token *token2;
6052 if (statement_p)
6053 cp_parser_commit_to_tentative_parse (parser);
6054 /* If the token after `using' is `namespace', then we have a
6055 using-directive. */
6056 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6057 if (token2->keyword == RID_NAMESPACE)
6058 cp_parser_using_directive (parser);
6059 /* Otherwise, it's a using-declaration. */
6060 else
6061 cp_parser_using_declaration (parser);
6063 /* If the next keyword is `__label__' we have a label declaration. */
6064 else if (token1->keyword == RID_LABEL)
6066 if (statement_p)
6067 cp_parser_commit_to_tentative_parse (parser);
6068 cp_parser_label_declaration (parser);
6070 /* Anything else must be a simple-declaration. */
6071 else
6072 cp_parser_simple_declaration (parser, !statement_p);
6075 /* Parse a simple-declaration.
6077 simple-declaration:
6078 decl-specifier-seq [opt] init-declarator-list [opt] ;
6080 init-declarator-list:
6081 init-declarator
6082 init-declarator-list , init-declarator
6084 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6085 function-definition as a simple-declaration. */
6087 static void
6088 cp_parser_simple_declaration (cp_parser* parser,
6089 bool function_definition_allowed_p)
6091 tree decl_specifiers;
6092 tree attributes;
6093 int declares_class_or_enum;
6094 bool saw_declarator;
6096 /* Defer access checks until we know what is being declared; the
6097 checks for names appearing in the decl-specifier-seq should be
6098 done as if we were in the scope of the thing being declared. */
6099 push_deferring_access_checks (dk_deferred);
6101 /* Parse the decl-specifier-seq. We have to keep track of whether
6102 or not the decl-specifier-seq declares a named class or
6103 enumeration type, since that is the only case in which the
6104 init-declarator-list is allowed to be empty.
6106 [dcl.dcl]
6108 In a simple-declaration, the optional init-declarator-list can be
6109 omitted only when declaring a class or enumeration, that is when
6110 the decl-specifier-seq contains either a class-specifier, an
6111 elaborated-type-specifier, or an enum-specifier. */
6112 decl_specifiers
6113 = cp_parser_decl_specifier_seq (parser,
6114 CP_PARSER_FLAGS_OPTIONAL,
6115 &attributes,
6116 &declares_class_or_enum);
6117 /* We no longer need to defer access checks. */
6118 stop_deferring_access_checks ();
6120 /* In a block scope, a valid declaration must always have a
6121 decl-specifier-seq. By not trying to parse declarators, we can
6122 resolve the declaration/expression ambiguity more quickly. */
6123 if (!function_definition_allowed_p && !decl_specifiers)
6125 cp_parser_error (parser, "expected declaration");
6126 goto done;
6129 /* If the next two tokens are both identifiers, the code is
6130 erroneous. The usual cause of this situation is code like:
6132 T t;
6134 where "T" should name a type -- but does not. */
6135 if (cp_parser_diagnose_invalid_type_name (parser))
6137 /* If parsing tentatively, we should commit; we really are
6138 looking at a declaration. */
6139 cp_parser_commit_to_tentative_parse (parser);
6140 /* Give up. */
6141 goto done;
6144 /* Keep going until we hit the `;' at the end of the simple
6145 declaration. */
6146 saw_declarator = false;
6147 while (cp_lexer_next_token_is_not (parser->lexer,
6148 CPP_SEMICOLON))
6150 cp_token *token;
6151 bool function_definition_p;
6152 tree decl;
6154 saw_declarator = true;
6155 /* Parse the init-declarator. */
6156 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6157 function_definition_allowed_p,
6158 /*member_p=*/false,
6159 declares_class_or_enum,
6160 &function_definition_p);
6161 /* If an error occurred while parsing tentatively, exit quickly.
6162 (That usually happens when in the body of a function; each
6163 statement is treated as a declaration-statement until proven
6164 otherwise.) */
6165 if (cp_parser_error_occurred (parser))
6166 goto done;
6167 /* Handle function definitions specially. */
6168 if (function_definition_p)
6170 /* If the next token is a `,', then we are probably
6171 processing something like:
6173 void f() {}, *p;
6175 which is erroneous. */
6176 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6177 error ("mixing declarations and function-definitions is forbidden");
6178 /* Otherwise, we're done with the list of declarators. */
6179 else
6181 pop_deferring_access_checks ();
6182 return;
6185 /* The next token should be either a `,' or a `;'. */
6186 token = cp_lexer_peek_token (parser->lexer);
6187 /* If it's a `,', there are more declarators to come. */
6188 if (token->type == CPP_COMMA)
6189 cp_lexer_consume_token (parser->lexer);
6190 /* If it's a `;', we are done. */
6191 else if (token->type == CPP_SEMICOLON)
6192 break;
6193 /* Anything else is an error. */
6194 else
6196 cp_parser_error (parser, "expected `,' or `;'");
6197 /* Skip tokens until we reach the end of the statement. */
6198 cp_parser_skip_to_end_of_statement (parser);
6199 goto done;
6201 /* After the first time around, a function-definition is not
6202 allowed -- even if it was OK at first. For example:
6204 int i, f() {}
6206 is not valid. */
6207 function_definition_allowed_p = false;
6210 /* Issue an error message if no declarators are present, and the
6211 decl-specifier-seq does not itself declare a class or
6212 enumeration. */
6213 if (!saw_declarator)
6215 if (cp_parser_declares_only_class_p (parser))
6216 shadow_tag (decl_specifiers);
6217 /* Perform any deferred access checks. */
6218 perform_deferred_access_checks ();
6221 /* Consume the `;'. */
6222 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6224 done:
6225 pop_deferring_access_checks ();
6228 /* Parse a decl-specifier-seq.
6230 decl-specifier-seq:
6231 decl-specifier-seq [opt] decl-specifier
6233 decl-specifier:
6234 storage-class-specifier
6235 type-specifier
6236 function-specifier
6237 friend
6238 typedef
6240 GNU Extension:
6242 decl-specifier-seq:
6243 decl-specifier-seq [opt] attributes
6245 Returns a TREE_LIST, giving the decl-specifiers in the order they
6246 appear in the source code. The TREE_VALUE of each node is the
6247 decl-specifier. For a keyword (such as `auto' or `friend'), the
6248 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6249 representation of a type-specifier, see cp_parser_type_specifier.
6251 If there are attributes, they will be stored in *ATTRIBUTES,
6252 represented as described above cp_parser_attributes.
6254 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6255 appears, and the entity that will be a friend is not going to be a
6256 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6257 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6258 friendship is granted might not be a class.
6260 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6261 *flags:
6263 1: one of the decl-specifiers is an elaborated-type-specifier
6264 2: one of the decl-specifiers is an enum-specifier or a
6265 class-specifier
6269 static tree
6270 cp_parser_decl_specifier_seq (cp_parser* parser,
6271 cp_parser_flags flags,
6272 tree* attributes,
6273 int* declares_class_or_enum)
6275 tree decl_specs = NULL_TREE;
6276 bool friend_p = false;
6277 bool constructor_possible_p = !parser->in_declarator_p;
6279 /* Assume no class or enumeration type is declared. */
6280 *declares_class_or_enum = 0;
6282 /* Assume there are no attributes. */
6283 *attributes = NULL_TREE;
6285 /* Keep reading specifiers until there are no more to read. */
6286 while (true)
6288 tree decl_spec = NULL_TREE;
6289 bool constructor_p;
6290 cp_token *token;
6292 /* Peek at the next token. */
6293 token = cp_lexer_peek_token (parser->lexer);
6294 /* Handle attributes. */
6295 if (token->keyword == RID_ATTRIBUTE)
6297 /* Parse the attributes. */
6298 decl_spec = cp_parser_attributes_opt (parser);
6299 /* Add them to the list. */
6300 *attributes = chainon (*attributes, decl_spec);
6301 continue;
6303 /* If the next token is an appropriate keyword, we can simply
6304 add it to the list. */
6305 switch (token->keyword)
6307 case RID_FRIEND:
6308 /* decl-specifier:
6309 friend */
6310 if (friend_p)
6311 error ("duplicate `friend'");
6312 else
6313 friend_p = true;
6314 /* The representation of the specifier is simply the
6315 appropriate TREE_IDENTIFIER node. */
6316 decl_spec = token->value;
6317 /* Consume the token. */
6318 cp_lexer_consume_token (parser->lexer);
6319 break;
6321 /* function-specifier:
6322 inline
6323 virtual
6324 explicit */
6325 case RID_INLINE:
6326 case RID_VIRTUAL:
6327 case RID_EXPLICIT:
6328 decl_spec = cp_parser_function_specifier_opt (parser);
6329 break;
6331 /* decl-specifier:
6332 typedef */
6333 case RID_TYPEDEF:
6334 /* The representation of the specifier is simply the
6335 appropriate TREE_IDENTIFIER node. */
6336 decl_spec = token->value;
6337 /* Consume the token. */
6338 cp_lexer_consume_token (parser->lexer);
6339 /* A constructor declarator cannot appear in a typedef. */
6340 constructor_possible_p = false;
6341 /* The "typedef" keyword can only occur in a declaration; we
6342 may as well commit at this point. */
6343 cp_parser_commit_to_tentative_parse (parser);
6344 break;
6346 /* storage-class-specifier:
6347 auto
6348 register
6349 static
6350 extern
6351 mutable
6353 GNU Extension:
6354 thread */
6355 case RID_AUTO:
6356 case RID_REGISTER:
6357 case RID_STATIC:
6358 case RID_EXTERN:
6359 case RID_MUTABLE:
6360 case RID_THREAD:
6361 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6362 break;
6364 default:
6365 break;
6368 /* Constructors are a special case. The `S' in `S()' is not a
6369 decl-specifier; it is the beginning of the declarator. */
6370 constructor_p = (!decl_spec
6371 && constructor_possible_p
6372 && cp_parser_constructor_declarator_p (parser,
6373 friend_p));
6375 /* If we don't have a DECL_SPEC yet, then we must be looking at
6376 a type-specifier. */
6377 if (!decl_spec && !constructor_p)
6379 int decl_spec_declares_class_or_enum;
6380 bool is_cv_qualifier;
6382 decl_spec
6383 = cp_parser_type_specifier (parser, flags,
6384 friend_p,
6385 /*is_declaration=*/true,
6386 &decl_spec_declares_class_or_enum,
6387 &is_cv_qualifier);
6389 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6391 /* If this type-specifier referenced a user-defined type
6392 (a typedef, class-name, etc.), then we can't allow any
6393 more such type-specifiers henceforth.
6395 [dcl.spec]
6397 The longest sequence of decl-specifiers that could
6398 possibly be a type name is taken as the
6399 decl-specifier-seq of a declaration. The sequence shall
6400 be self-consistent as described below.
6402 [dcl.type]
6404 As a general rule, at most one type-specifier is allowed
6405 in the complete decl-specifier-seq of a declaration. The
6406 only exceptions are the following:
6408 -- const or volatile can be combined with any other
6409 type-specifier.
6411 -- signed or unsigned can be combined with char, long,
6412 short, or int.
6414 -- ..
6416 Example:
6418 typedef char* Pc;
6419 void g (const int Pc);
6421 Here, Pc is *not* part of the decl-specifier seq; it's
6422 the declarator. Therefore, once we see a type-specifier
6423 (other than a cv-qualifier), we forbid any additional
6424 user-defined types. We *do* still allow things like `int
6425 int' to be considered a decl-specifier-seq, and issue the
6426 error message later. */
6427 if (decl_spec && !is_cv_qualifier)
6428 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6429 /* A constructor declarator cannot follow a type-specifier. */
6430 if (decl_spec)
6431 constructor_possible_p = false;
6434 /* If we still do not have a DECL_SPEC, then there are no more
6435 decl-specifiers. */
6436 if (!decl_spec)
6438 /* Issue an error message, unless the entire construct was
6439 optional. */
6440 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6442 cp_parser_error (parser, "expected decl specifier");
6443 return error_mark_node;
6446 break;
6449 /* Add the DECL_SPEC to the list of specifiers. */
6450 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6452 /* After we see one decl-specifier, further decl-specifiers are
6453 always optional. */
6454 flags |= CP_PARSER_FLAGS_OPTIONAL;
6457 /* We have built up the DECL_SPECS in reverse order. Return them in
6458 the correct order. */
6459 return nreverse (decl_specs);
6462 /* Parse an (optional) storage-class-specifier.
6464 storage-class-specifier:
6465 auto
6466 register
6467 static
6468 extern
6469 mutable
6471 GNU Extension:
6473 storage-class-specifier:
6474 thread
6476 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6478 static tree
6479 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6481 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6483 case RID_AUTO:
6484 case RID_REGISTER:
6485 case RID_STATIC:
6486 case RID_EXTERN:
6487 case RID_MUTABLE:
6488 case RID_THREAD:
6489 /* Consume the token. */
6490 return cp_lexer_consume_token (parser->lexer)->value;
6492 default:
6493 return NULL_TREE;
6497 /* Parse an (optional) function-specifier.
6499 function-specifier:
6500 inline
6501 virtual
6502 explicit
6504 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6506 static tree
6507 cp_parser_function_specifier_opt (cp_parser* parser)
6509 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6511 case RID_INLINE:
6512 case RID_VIRTUAL:
6513 case RID_EXPLICIT:
6514 /* Consume the token. */
6515 return cp_lexer_consume_token (parser->lexer)->value;
6517 default:
6518 return NULL_TREE;
6522 /* Parse a linkage-specification.
6524 linkage-specification:
6525 extern string-literal { declaration-seq [opt] }
6526 extern string-literal declaration */
6528 static void
6529 cp_parser_linkage_specification (cp_parser* parser)
6531 cp_token *token;
6532 tree linkage;
6534 /* Look for the `extern' keyword. */
6535 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6537 /* Peek at the next token. */
6538 token = cp_lexer_peek_token (parser->lexer);
6539 /* If it's not a string-literal, then there's a problem. */
6540 if (!cp_parser_is_string_literal (token))
6542 cp_parser_error (parser, "expected language-name");
6543 return;
6545 /* Consume the token. */
6546 cp_lexer_consume_token (parser->lexer);
6548 /* Transform the literal into an identifier. If the literal is a
6549 wide-character string, or contains embedded NULs, then we can't
6550 handle it as the user wants. */
6551 if (token->type == CPP_WSTRING
6552 || (strlen (TREE_STRING_POINTER (token->value))
6553 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6555 cp_parser_error (parser, "invalid linkage-specification");
6556 /* Assume C++ linkage. */
6557 linkage = get_identifier ("c++");
6559 /* If it's a simple string constant, things are easier. */
6560 else
6561 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6563 /* We're now using the new linkage. */
6564 push_lang_context (linkage);
6566 /* If the next token is a `{', then we're using the first
6567 production. */
6568 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6570 /* Consume the `{' token. */
6571 cp_lexer_consume_token (parser->lexer);
6572 /* Parse the declarations. */
6573 cp_parser_declaration_seq_opt (parser);
6574 /* Look for the closing `}'. */
6575 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6577 /* Otherwise, there's just one declaration. */
6578 else
6580 bool saved_in_unbraced_linkage_specification_p;
6582 saved_in_unbraced_linkage_specification_p
6583 = parser->in_unbraced_linkage_specification_p;
6584 parser->in_unbraced_linkage_specification_p = true;
6585 have_extern_spec = true;
6586 cp_parser_declaration (parser);
6587 have_extern_spec = false;
6588 parser->in_unbraced_linkage_specification_p
6589 = saved_in_unbraced_linkage_specification_p;
6592 /* We're done with the linkage-specification. */
6593 pop_lang_context ();
6596 /* Special member functions [gram.special] */
6598 /* Parse a conversion-function-id.
6600 conversion-function-id:
6601 operator conversion-type-id
6603 Returns an IDENTIFIER_NODE representing the operator. */
6605 static tree
6606 cp_parser_conversion_function_id (cp_parser* parser)
6608 tree type;
6609 tree saved_scope;
6610 tree saved_qualifying_scope;
6611 tree saved_object_scope;
6613 /* Look for the `operator' token. */
6614 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6615 return error_mark_node;
6616 /* When we parse the conversion-type-id, the current scope will be
6617 reset. However, we need that information in able to look up the
6618 conversion function later, so we save it here. */
6619 saved_scope = parser->scope;
6620 saved_qualifying_scope = parser->qualifying_scope;
6621 saved_object_scope = parser->object_scope;
6622 /* We must enter the scope of the class so that the names of
6623 entities declared within the class are available in the
6624 conversion-type-id. For example, consider:
6626 struct S {
6627 typedef int I;
6628 operator I();
6631 S::operator I() { ... }
6633 In order to see that `I' is a type-name in the definition, we
6634 must be in the scope of `S'. */
6635 if (saved_scope)
6636 push_scope (saved_scope);
6637 /* Parse the conversion-type-id. */
6638 type = cp_parser_conversion_type_id (parser);
6639 /* Leave the scope of the class, if any. */
6640 if (saved_scope)
6641 pop_scope (saved_scope);
6642 /* Restore the saved scope. */
6643 parser->scope = saved_scope;
6644 parser->qualifying_scope = saved_qualifying_scope;
6645 parser->object_scope = saved_object_scope;
6646 /* If the TYPE is invalid, indicate failure. */
6647 if (type == error_mark_node)
6648 return error_mark_node;
6649 return mangle_conv_op_name_for_type (type);
6652 /* Parse a conversion-type-id:
6654 conversion-type-id:
6655 type-specifier-seq conversion-declarator [opt]
6657 Returns the TYPE specified. */
6659 static tree
6660 cp_parser_conversion_type_id (cp_parser* parser)
6662 tree attributes;
6663 tree type_specifiers;
6664 tree declarator;
6666 /* Parse the attributes. */
6667 attributes = cp_parser_attributes_opt (parser);
6668 /* Parse the type-specifiers. */
6669 type_specifiers = cp_parser_type_specifier_seq (parser);
6670 /* If that didn't work, stop. */
6671 if (type_specifiers == error_mark_node)
6672 return error_mark_node;
6673 /* Parse the conversion-declarator. */
6674 declarator = cp_parser_conversion_declarator_opt (parser);
6676 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6677 /*initialized=*/0, &attributes);
6680 /* Parse an (optional) conversion-declarator.
6682 conversion-declarator:
6683 ptr-operator conversion-declarator [opt]
6685 Returns a representation of the declarator. See
6686 cp_parser_declarator for details. */
6688 static tree
6689 cp_parser_conversion_declarator_opt (cp_parser* parser)
6691 enum tree_code code;
6692 tree class_type;
6693 tree cv_qualifier_seq;
6695 /* We don't know if there's a ptr-operator next, or not. */
6696 cp_parser_parse_tentatively (parser);
6697 /* Try the ptr-operator. */
6698 code = cp_parser_ptr_operator (parser, &class_type,
6699 &cv_qualifier_seq);
6700 /* If it worked, look for more conversion-declarators. */
6701 if (cp_parser_parse_definitely (parser))
6703 tree declarator;
6705 /* Parse another optional declarator. */
6706 declarator = cp_parser_conversion_declarator_opt (parser);
6708 /* Create the representation of the declarator. */
6709 if (code == INDIRECT_REF)
6710 declarator = make_pointer_declarator (cv_qualifier_seq,
6711 declarator);
6712 else
6713 declarator = make_reference_declarator (cv_qualifier_seq,
6714 declarator);
6716 /* Handle the pointer-to-member case. */
6717 if (class_type)
6718 declarator = build_nt (SCOPE_REF, class_type, declarator);
6720 return declarator;
6723 return NULL_TREE;
6726 /* Parse an (optional) ctor-initializer.
6728 ctor-initializer:
6729 : mem-initializer-list
6731 Returns TRUE iff the ctor-initializer was actually present. */
6733 static bool
6734 cp_parser_ctor_initializer_opt (cp_parser* parser)
6736 /* If the next token is not a `:', then there is no
6737 ctor-initializer. */
6738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
6740 /* Do default initialization of any bases and members. */
6741 if (DECL_CONSTRUCTOR_P (current_function_decl))
6742 finish_mem_initializers (NULL_TREE);
6744 return false;
6747 /* Consume the `:' token. */
6748 cp_lexer_consume_token (parser->lexer);
6749 /* And the mem-initializer-list. */
6750 cp_parser_mem_initializer_list (parser);
6752 return true;
6755 /* Parse a mem-initializer-list.
6757 mem-initializer-list:
6758 mem-initializer
6759 mem-initializer , mem-initializer-list */
6761 static void
6762 cp_parser_mem_initializer_list (cp_parser* parser)
6764 tree mem_initializer_list = NULL_TREE;
6766 /* Let the semantic analysis code know that we are starting the
6767 mem-initializer-list. */
6768 if (!DECL_CONSTRUCTOR_P (current_function_decl))
6769 error ("only constructors take base initializers");
6771 /* Loop through the list. */
6772 while (true)
6774 tree mem_initializer;
6776 /* Parse the mem-initializer. */
6777 mem_initializer = cp_parser_mem_initializer (parser);
6778 /* Add it to the list, unless it was erroneous. */
6779 if (mem_initializer)
6781 TREE_CHAIN (mem_initializer) = mem_initializer_list;
6782 mem_initializer_list = mem_initializer;
6784 /* If the next token is not a `,', we're done. */
6785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6786 break;
6787 /* Consume the `,' token. */
6788 cp_lexer_consume_token (parser->lexer);
6791 /* Perform semantic analysis. */
6792 if (DECL_CONSTRUCTOR_P (current_function_decl))
6793 finish_mem_initializers (mem_initializer_list);
6796 /* Parse a mem-initializer.
6798 mem-initializer:
6799 mem-initializer-id ( expression-list [opt] )
6801 GNU extension:
6803 mem-initializer:
6804 ( expression-list [opt] )
6806 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
6807 class) or FIELD_DECL (for a non-static data member) to initialize;
6808 the TREE_VALUE is the expression-list. */
6810 static tree
6811 cp_parser_mem_initializer (cp_parser* parser)
6813 tree mem_initializer_id;
6814 tree expression_list;
6815 tree member;
6817 /* Find out what is being initialized. */
6818 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6820 pedwarn ("anachronistic old-style base class initializer");
6821 mem_initializer_id = NULL_TREE;
6823 else
6824 mem_initializer_id = cp_parser_mem_initializer_id (parser);
6825 member = expand_member_init (mem_initializer_id);
6826 if (member && !DECL_P (member))
6827 in_base_initializer = 1;
6829 expression_list
6830 = cp_parser_parenthesized_expression_list (parser, false,
6831 /*non_constant_p=*/NULL);
6832 if (!expression_list)
6833 expression_list = void_type_node;
6835 in_base_initializer = 0;
6837 return member ? build_tree_list (member, expression_list) : NULL_TREE;
6840 /* Parse a mem-initializer-id.
6842 mem-initializer-id:
6843 :: [opt] nested-name-specifier [opt] class-name
6844 identifier
6846 Returns a TYPE indicating the class to be initializer for the first
6847 production. Returns an IDENTIFIER_NODE indicating the data member
6848 to be initialized for the second production. */
6850 static tree
6851 cp_parser_mem_initializer_id (cp_parser* parser)
6853 bool global_scope_p;
6854 bool nested_name_specifier_p;
6855 tree id;
6857 /* Look for the optional `::' operator. */
6858 global_scope_p
6859 = (cp_parser_global_scope_opt (parser,
6860 /*current_scope_valid_p=*/false)
6861 != NULL_TREE);
6862 /* Look for the optional nested-name-specifier. The simplest way to
6863 implement:
6865 [temp.res]
6867 The keyword `typename' is not permitted in a base-specifier or
6868 mem-initializer; in these contexts a qualified name that
6869 depends on a template-parameter is implicitly assumed to be a
6870 type name.
6872 is to assume that we have seen the `typename' keyword at this
6873 point. */
6874 nested_name_specifier_p
6875 = (cp_parser_nested_name_specifier_opt (parser,
6876 /*typename_keyword_p=*/true,
6877 /*check_dependency_p=*/true,
6878 /*type_p=*/true)
6879 != NULL_TREE);
6880 /* If there is a `::' operator or a nested-name-specifier, then we
6881 are definitely looking for a class-name. */
6882 if (global_scope_p || nested_name_specifier_p)
6883 return cp_parser_class_name (parser,
6884 /*typename_keyword_p=*/true,
6885 /*template_keyword_p=*/false,
6886 /*type_p=*/false,
6887 /*check_dependency_p=*/true,
6888 /*class_head_p=*/false);
6889 /* Otherwise, we could also be looking for an ordinary identifier. */
6890 cp_parser_parse_tentatively (parser);
6891 /* Try a class-name. */
6892 id = cp_parser_class_name (parser,
6893 /*typename_keyword_p=*/true,
6894 /*template_keyword_p=*/false,
6895 /*type_p=*/false,
6896 /*check_dependency_p=*/true,
6897 /*class_head_p=*/false);
6898 /* If we found one, we're done. */
6899 if (cp_parser_parse_definitely (parser))
6900 return id;
6901 /* Otherwise, look for an ordinary identifier. */
6902 return cp_parser_identifier (parser);
6905 /* Overloading [gram.over] */
6907 /* Parse an operator-function-id.
6909 operator-function-id:
6910 operator operator
6912 Returns an IDENTIFIER_NODE for the operator which is a
6913 human-readable spelling of the identifier, e.g., `operator +'. */
6915 static tree
6916 cp_parser_operator_function_id (cp_parser* parser)
6918 /* Look for the `operator' keyword. */
6919 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6920 return error_mark_node;
6921 /* And then the name of the operator itself. */
6922 return cp_parser_operator (parser);
6925 /* Parse an operator.
6927 operator:
6928 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
6929 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
6930 || ++ -- , ->* -> () []
6932 GNU Extensions:
6934 operator:
6935 <? >? <?= >?=
6937 Returns an IDENTIFIER_NODE for the operator which is a
6938 human-readable spelling of the identifier, e.g., `operator +'. */
6940 static tree
6941 cp_parser_operator (cp_parser* parser)
6943 tree id = NULL_TREE;
6944 cp_token *token;
6946 /* Peek at the next token. */
6947 token = cp_lexer_peek_token (parser->lexer);
6948 /* Figure out which operator we have. */
6949 switch (token->type)
6951 case CPP_KEYWORD:
6953 enum tree_code op;
6955 /* The keyword should be either `new' or `delete'. */
6956 if (token->keyword == RID_NEW)
6957 op = NEW_EXPR;
6958 else if (token->keyword == RID_DELETE)
6959 op = DELETE_EXPR;
6960 else
6961 break;
6963 /* Consume the `new' or `delete' token. */
6964 cp_lexer_consume_token (parser->lexer);
6966 /* Peek at the next token. */
6967 token = cp_lexer_peek_token (parser->lexer);
6968 /* If it's a `[' token then this is the array variant of the
6969 operator. */
6970 if (token->type == CPP_OPEN_SQUARE)
6972 /* Consume the `[' token. */
6973 cp_lexer_consume_token (parser->lexer);
6974 /* Look for the `]' token. */
6975 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
6976 id = ansi_opname (op == NEW_EXPR
6977 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
6979 /* Otherwise, we have the non-array variant. */
6980 else
6981 id = ansi_opname (op);
6983 return id;
6986 case CPP_PLUS:
6987 id = ansi_opname (PLUS_EXPR);
6988 break;
6990 case CPP_MINUS:
6991 id = ansi_opname (MINUS_EXPR);
6992 break;
6994 case CPP_MULT:
6995 id = ansi_opname (MULT_EXPR);
6996 break;
6998 case CPP_DIV:
6999 id = ansi_opname (TRUNC_DIV_EXPR);
7000 break;
7002 case CPP_MOD:
7003 id = ansi_opname (TRUNC_MOD_EXPR);
7004 break;
7006 case CPP_XOR:
7007 id = ansi_opname (BIT_XOR_EXPR);
7008 break;
7010 case CPP_AND:
7011 id = ansi_opname (BIT_AND_EXPR);
7012 break;
7014 case CPP_OR:
7015 id = ansi_opname (BIT_IOR_EXPR);
7016 break;
7018 case CPP_COMPL:
7019 id = ansi_opname (BIT_NOT_EXPR);
7020 break;
7022 case CPP_NOT:
7023 id = ansi_opname (TRUTH_NOT_EXPR);
7024 break;
7026 case CPP_EQ:
7027 id = ansi_assopname (NOP_EXPR);
7028 break;
7030 case CPP_LESS:
7031 id = ansi_opname (LT_EXPR);
7032 break;
7034 case CPP_GREATER:
7035 id = ansi_opname (GT_EXPR);
7036 break;
7038 case CPP_PLUS_EQ:
7039 id = ansi_assopname (PLUS_EXPR);
7040 break;
7042 case CPP_MINUS_EQ:
7043 id = ansi_assopname (MINUS_EXPR);
7044 break;
7046 case CPP_MULT_EQ:
7047 id = ansi_assopname (MULT_EXPR);
7048 break;
7050 case CPP_DIV_EQ:
7051 id = ansi_assopname (TRUNC_DIV_EXPR);
7052 break;
7054 case CPP_MOD_EQ:
7055 id = ansi_assopname (TRUNC_MOD_EXPR);
7056 break;
7058 case CPP_XOR_EQ:
7059 id = ansi_assopname (BIT_XOR_EXPR);
7060 break;
7062 case CPP_AND_EQ:
7063 id = ansi_assopname (BIT_AND_EXPR);
7064 break;
7066 case CPP_OR_EQ:
7067 id = ansi_assopname (BIT_IOR_EXPR);
7068 break;
7070 case CPP_LSHIFT:
7071 id = ansi_opname (LSHIFT_EXPR);
7072 break;
7074 case CPP_RSHIFT:
7075 id = ansi_opname (RSHIFT_EXPR);
7076 break;
7078 case CPP_LSHIFT_EQ:
7079 id = ansi_assopname (LSHIFT_EXPR);
7080 break;
7082 case CPP_RSHIFT_EQ:
7083 id = ansi_assopname (RSHIFT_EXPR);
7084 break;
7086 case CPP_EQ_EQ:
7087 id = ansi_opname (EQ_EXPR);
7088 break;
7090 case CPP_NOT_EQ:
7091 id = ansi_opname (NE_EXPR);
7092 break;
7094 case CPP_LESS_EQ:
7095 id = ansi_opname (LE_EXPR);
7096 break;
7098 case CPP_GREATER_EQ:
7099 id = ansi_opname (GE_EXPR);
7100 break;
7102 case CPP_AND_AND:
7103 id = ansi_opname (TRUTH_ANDIF_EXPR);
7104 break;
7106 case CPP_OR_OR:
7107 id = ansi_opname (TRUTH_ORIF_EXPR);
7108 break;
7110 case CPP_PLUS_PLUS:
7111 id = ansi_opname (POSTINCREMENT_EXPR);
7112 break;
7114 case CPP_MINUS_MINUS:
7115 id = ansi_opname (PREDECREMENT_EXPR);
7116 break;
7118 case CPP_COMMA:
7119 id = ansi_opname (COMPOUND_EXPR);
7120 break;
7122 case CPP_DEREF_STAR:
7123 id = ansi_opname (MEMBER_REF);
7124 break;
7126 case CPP_DEREF:
7127 id = ansi_opname (COMPONENT_REF);
7128 break;
7130 case CPP_OPEN_PAREN:
7131 /* Consume the `('. */
7132 cp_lexer_consume_token (parser->lexer);
7133 /* Look for the matching `)'. */
7134 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7135 return ansi_opname (CALL_EXPR);
7137 case CPP_OPEN_SQUARE:
7138 /* Consume the `['. */
7139 cp_lexer_consume_token (parser->lexer);
7140 /* Look for the matching `]'. */
7141 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7142 return ansi_opname (ARRAY_REF);
7144 /* Extensions. */
7145 case CPP_MIN:
7146 id = ansi_opname (MIN_EXPR);
7147 break;
7149 case CPP_MAX:
7150 id = ansi_opname (MAX_EXPR);
7151 break;
7153 case CPP_MIN_EQ:
7154 id = ansi_assopname (MIN_EXPR);
7155 break;
7157 case CPP_MAX_EQ:
7158 id = ansi_assopname (MAX_EXPR);
7159 break;
7161 default:
7162 /* Anything else is an error. */
7163 break;
7166 /* If we have selected an identifier, we need to consume the
7167 operator token. */
7168 if (id)
7169 cp_lexer_consume_token (parser->lexer);
7170 /* Otherwise, no valid operator name was present. */
7171 else
7173 cp_parser_error (parser, "expected operator");
7174 id = error_mark_node;
7177 return id;
7180 /* Parse a template-declaration.
7182 template-declaration:
7183 export [opt] template < template-parameter-list > declaration
7185 If MEMBER_P is TRUE, this template-declaration occurs within a
7186 class-specifier.
7188 The grammar rule given by the standard isn't correct. What
7189 is really meant is:
7191 template-declaration:
7192 export [opt] template-parameter-list-seq
7193 decl-specifier-seq [opt] init-declarator [opt] ;
7194 export [opt] template-parameter-list-seq
7195 function-definition
7197 template-parameter-list-seq:
7198 template-parameter-list-seq [opt]
7199 template < template-parameter-list > */
7201 static void
7202 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7204 /* Check for `export'. */
7205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7207 /* Consume the `export' token. */
7208 cp_lexer_consume_token (parser->lexer);
7209 /* Warn that we do not support `export'. */
7210 warning ("keyword `export' not implemented, and will be ignored");
7213 cp_parser_template_declaration_after_export (parser, member_p);
7216 /* Parse a template-parameter-list.
7218 template-parameter-list:
7219 template-parameter
7220 template-parameter-list , template-parameter
7222 Returns a TREE_LIST. Each node represents a template parameter.
7223 The nodes are connected via their TREE_CHAINs. */
7225 static tree
7226 cp_parser_template_parameter_list (cp_parser* parser)
7228 tree parameter_list = NULL_TREE;
7230 while (true)
7232 tree parameter;
7233 cp_token *token;
7235 /* Parse the template-parameter. */
7236 parameter = cp_parser_template_parameter (parser);
7237 /* Add it to the list. */
7238 parameter_list = process_template_parm (parameter_list,
7239 parameter);
7241 /* Peek at the next token. */
7242 token = cp_lexer_peek_token (parser->lexer);
7243 /* If it's not a `,', we're done. */
7244 if (token->type != CPP_COMMA)
7245 break;
7246 /* Otherwise, consume the `,' token. */
7247 cp_lexer_consume_token (parser->lexer);
7250 return parameter_list;
7253 /* Parse a template-parameter.
7255 template-parameter:
7256 type-parameter
7257 parameter-declaration
7259 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7260 TREE_PURPOSE is the default value, if any. */
7262 static tree
7263 cp_parser_template_parameter (cp_parser* parser)
7265 cp_token *token;
7267 /* Peek at the next token. */
7268 token = cp_lexer_peek_token (parser->lexer);
7269 /* If it is `class' or `template', we have a type-parameter. */
7270 if (token->keyword == RID_TEMPLATE)
7271 return cp_parser_type_parameter (parser);
7272 /* If it is `class' or `typename' we do not know yet whether it is a
7273 type parameter or a non-type parameter. Consider:
7275 template <typename T, typename T::X X> ...
7279 template <class C, class D*> ...
7281 Here, the first parameter is a type parameter, and the second is
7282 a non-type parameter. We can tell by looking at the token after
7283 the identifier -- if it is a `,', `=', or `>' then we have a type
7284 parameter. */
7285 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7287 /* Peek at the token after `class' or `typename'. */
7288 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7289 /* If it's an identifier, skip it. */
7290 if (token->type == CPP_NAME)
7291 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7292 /* Now, see if the token looks like the end of a template
7293 parameter. */
7294 if (token->type == CPP_COMMA
7295 || token->type == CPP_EQ
7296 || token->type == CPP_GREATER)
7297 return cp_parser_type_parameter (parser);
7300 /* Otherwise, it is a non-type parameter.
7302 [temp.param]
7304 When parsing a default template-argument for a non-type
7305 template-parameter, the first non-nested `>' is taken as the end
7306 of the template parameter-list rather than a greater-than
7307 operator. */
7308 return
7309 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7312 /* Parse a type-parameter.
7314 type-parameter:
7315 class identifier [opt]
7316 class identifier [opt] = type-id
7317 typename identifier [opt]
7318 typename identifier [opt] = type-id
7319 template < template-parameter-list > class identifier [opt]
7320 template < template-parameter-list > class identifier [opt]
7321 = id-expression
7323 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7324 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7325 the declaration of the parameter. */
7327 static tree
7328 cp_parser_type_parameter (cp_parser* parser)
7330 cp_token *token;
7331 tree parameter;
7333 /* Look for a keyword to tell us what kind of parameter this is. */
7334 token = cp_parser_require (parser, CPP_KEYWORD,
7335 "`class', `typename', or `template'");
7336 if (!token)
7337 return error_mark_node;
7339 switch (token->keyword)
7341 case RID_CLASS:
7342 case RID_TYPENAME:
7344 tree identifier;
7345 tree default_argument;
7347 /* If the next token is an identifier, then it names the
7348 parameter. */
7349 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7350 identifier = cp_parser_identifier (parser);
7351 else
7352 identifier = NULL_TREE;
7354 /* Create the parameter. */
7355 parameter = finish_template_type_parm (class_type_node, identifier);
7357 /* If the next token is an `=', we have a default argument. */
7358 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7360 /* Consume the `=' token. */
7361 cp_lexer_consume_token (parser->lexer);
7362 /* Parse the default-argument. */
7363 default_argument = cp_parser_type_id (parser);
7365 else
7366 default_argument = NULL_TREE;
7368 /* Create the combined representation of the parameter and the
7369 default argument. */
7370 parameter = build_tree_list (default_argument, parameter);
7372 break;
7374 case RID_TEMPLATE:
7376 tree parameter_list;
7377 tree identifier;
7378 tree default_argument;
7380 /* Look for the `<'. */
7381 cp_parser_require (parser, CPP_LESS, "`<'");
7382 /* Parse the template-parameter-list. */
7383 begin_template_parm_list ();
7384 parameter_list
7385 = cp_parser_template_parameter_list (parser);
7386 parameter_list = end_template_parm_list (parameter_list);
7387 /* Look for the `>'. */
7388 cp_parser_require (parser, CPP_GREATER, "`>'");
7389 /* Look for the `class' keyword. */
7390 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7391 /* If the next token is an `=', then there is a
7392 default-argument. If the next token is a `>', we are at
7393 the end of the parameter-list. If the next token is a `,',
7394 then we are at the end of this parameter. */
7395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7396 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7397 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7398 identifier = cp_parser_identifier (parser);
7399 else
7400 identifier = NULL_TREE;
7401 /* Create the template parameter. */
7402 parameter = finish_template_template_parm (class_type_node,
7403 identifier);
7405 /* If the next token is an `=', then there is a
7406 default-argument. */
7407 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7409 /* Consume the `='. */
7410 cp_lexer_consume_token (parser->lexer);
7411 /* Parse the id-expression. */
7412 default_argument
7413 = cp_parser_id_expression (parser,
7414 /*template_keyword_p=*/false,
7415 /*check_dependency_p=*/true,
7416 /*template_p=*/NULL,
7417 /*declarator_p=*/false);
7418 /* Look up the name. */
7419 default_argument
7420 = cp_parser_lookup_name_simple (parser, default_argument);
7421 /* See if the default argument is valid. */
7422 default_argument
7423 = check_template_template_default_arg (default_argument);
7425 else
7426 default_argument = NULL_TREE;
7428 /* Create the combined representation of the parameter and the
7429 default argument. */
7430 parameter = build_tree_list (default_argument, parameter);
7432 break;
7434 default:
7435 /* Anything else is an error. */
7436 cp_parser_error (parser,
7437 "expected `class', `typename', or `template'");
7438 parameter = error_mark_node;
7441 return parameter;
7444 /* Parse a template-id.
7446 template-id:
7447 template-name < template-argument-list [opt] >
7449 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7450 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7451 returned. Otherwise, if the template-name names a function, or set
7452 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7453 names a class, returns a TYPE_DECL for the specialization.
7455 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7456 uninstantiated templates. */
7458 static tree
7459 cp_parser_template_id (cp_parser *parser,
7460 bool template_keyword_p,
7461 bool check_dependency_p)
7463 tree template;
7464 tree arguments;
7465 tree saved_scope;
7466 tree saved_qualifying_scope;
7467 tree saved_object_scope;
7468 tree template_id;
7469 bool saved_greater_than_is_operator_p;
7470 ptrdiff_t start_of_id;
7471 tree access_check = NULL_TREE;
7472 cp_token *next_token;
7474 /* If the next token corresponds to a template-id, there is no need
7475 to reparse it. */
7476 next_token = cp_lexer_peek_token (parser->lexer);
7477 if (next_token->type == CPP_TEMPLATE_ID)
7479 tree value;
7480 tree check;
7482 /* Get the stored value. */
7483 value = cp_lexer_consume_token (parser->lexer)->value;
7484 /* Perform any access checks that were deferred. */
7485 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7486 perform_or_defer_access_check (TREE_PURPOSE (check),
7487 TREE_VALUE (check));
7488 /* Return the stored value. */
7489 return TREE_VALUE (value);
7492 /* Avoid performing name lookup if there is no possibility of
7493 finding a template-id. */
7494 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7495 || (next_token->type == CPP_NAME
7496 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7498 cp_parser_error (parser, "expected template-id");
7499 return error_mark_node;
7502 /* Remember where the template-id starts. */
7503 if (cp_parser_parsing_tentatively (parser)
7504 && !cp_parser_committed_to_tentative_parse (parser))
7506 next_token = cp_lexer_peek_token (parser->lexer);
7507 start_of_id = cp_lexer_token_difference (parser->lexer,
7508 parser->lexer->first_token,
7509 next_token);
7511 else
7512 start_of_id = -1;
7514 push_deferring_access_checks (dk_deferred);
7516 /* Parse the template-name. */
7517 template = cp_parser_template_name (parser, template_keyword_p,
7518 check_dependency_p);
7519 if (template == error_mark_node)
7521 pop_deferring_access_checks ();
7522 return error_mark_node;
7525 /* Look for the `<' that starts the template-argument-list. */
7526 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7528 pop_deferring_access_checks ();
7529 return error_mark_node;
7532 /* [temp.names]
7534 When parsing a template-id, the first non-nested `>' is taken as
7535 the end of the template-argument-list rather than a greater-than
7536 operator. */
7537 saved_greater_than_is_operator_p
7538 = parser->greater_than_is_operator_p;
7539 parser->greater_than_is_operator_p = false;
7540 /* Parsing the argument list may modify SCOPE, so we save it
7541 here. */
7542 saved_scope = parser->scope;
7543 saved_qualifying_scope = parser->qualifying_scope;
7544 saved_object_scope = parser->object_scope;
7545 /* Parse the template-argument-list itself. */
7546 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7547 arguments = NULL_TREE;
7548 else
7549 arguments = cp_parser_template_argument_list (parser);
7550 /* Look for the `>' that ends the template-argument-list. */
7551 cp_parser_require (parser, CPP_GREATER, "`>'");
7552 /* The `>' token might be a greater-than operator again now. */
7553 parser->greater_than_is_operator_p
7554 = saved_greater_than_is_operator_p;
7555 /* Restore the SAVED_SCOPE. */
7556 parser->scope = saved_scope;
7557 parser->qualifying_scope = saved_qualifying_scope;
7558 parser->object_scope = saved_object_scope;
7560 /* Build a representation of the specialization. */
7561 if (TREE_CODE (template) == IDENTIFIER_NODE)
7562 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7563 else if (DECL_CLASS_TEMPLATE_P (template)
7564 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7565 template_id
7566 = finish_template_type (template, arguments,
7567 cp_lexer_next_token_is (parser->lexer,
7568 CPP_SCOPE));
7569 else
7571 /* If it's not a class-template or a template-template, it should be
7572 a function-template. */
7573 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7574 || TREE_CODE (template) == OVERLOAD
7575 || BASELINK_P (template)),
7576 20010716);
7578 template_id = lookup_template_function (template, arguments);
7581 /* Retrieve any deferred checks. Do not pop this access checks yet
7582 so the memory will not be reclaimed during token replacing below. */
7583 access_check = get_deferred_access_checks ();
7585 /* If parsing tentatively, replace the sequence of tokens that makes
7586 up the template-id with a CPP_TEMPLATE_ID token. That way,
7587 should we re-parse the token stream, we will not have to repeat
7588 the effort required to do the parse, nor will we issue duplicate
7589 error messages about problems during instantiation of the
7590 template. */
7591 if (start_of_id >= 0)
7593 cp_token *token;
7595 /* Find the token that corresponds to the start of the
7596 template-id. */
7597 token = cp_lexer_advance_token (parser->lexer,
7598 parser->lexer->first_token,
7599 start_of_id);
7601 /* Reset the contents of the START_OF_ID token. */
7602 token->type = CPP_TEMPLATE_ID;
7603 token->value = build_tree_list (access_check, template_id);
7604 token->keyword = RID_MAX;
7605 /* Purge all subsequent tokens. */
7606 cp_lexer_purge_tokens_after (parser->lexer, token);
7609 pop_deferring_access_checks ();
7610 return template_id;
7613 /* Parse a template-name.
7615 template-name:
7616 identifier
7618 The standard should actually say:
7620 template-name:
7621 identifier
7622 operator-function-id
7623 conversion-function-id
7625 A defect report has been filed about this issue.
7627 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7628 `template' keyword, in a construction like:
7630 T::template f<3>()
7632 In that case `f' is taken to be a template-name, even though there
7633 is no way of knowing for sure.
7635 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7636 name refers to a set of overloaded functions, at least one of which
7637 is a template, or an IDENTIFIER_NODE with the name of the template,
7638 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7639 names are looked up inside uninstantiated templates. */
7641 static tree
7642 cp_parser_template_name (cp_parser* parser,
7643 bool template_keyword_p,
7644 bool check_dependency_p)
7646 tree identifier;
7647 tree decl;
7648 tree fns;
7650 /* If the next token is `operator', then we have either an
7651 operator-function-id or a conversion-function-id. */
7652 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7654 /* We don't know whether we're looking at an
7655 operator-function-id or a conversion-function-id. */
7656 cp_parser_parse_tentatively (parser);
7657 /* Try an operator-function-id. */
7658 identifier = cp_parser_operator_function_id (parser);
7659 /* If that didn't work, try a conversion-function-id. */
7660 if (!cp_parser_parse_definitely (parser))
7661 identifier = cp_parser_conversion_function_id (parser);
7663 /* Look for the identifier. */
7664 else
7665 identifier = cp_parser_identifier (parser);
7667 /* If we didn't find an identifier, we don't have a template-id. */
7668 if (identifier == error_mark_node)
7669 return error_mark_node;
7671 /* If the name immediately followed the `template' keyword, then it
7672 is a template-name. However, if the next token is not `<', then
7673 we do not treat it as a template-name, since it is not being used
7674 as part of a template-id. This enables us to handle constructs
7675 like:
7677 template <typename T> struct S { S(); };
7678 template <typename T> S<T>::S();
7680 correctly. We would treat `S' as a template -- if it were `S<T>'
7681 -- but we do not if there is no `<'. */
7682 if (template_keyword_p && processing_template_decl
7683 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7684 return identifier;
7686 /* Look up the name. */
7687 decl = cp_parser_lookup_name (parser, identifier,
7688 /*is_type=*/false,
7689 /*is_namespace=*/false,
7690 check_dependency_p);
7691 decl = maybe_get_template_decl_from_type_decl (decl);
7693 /* If DECL is a template, then the name was a template-name. */
7694 if (TREE_CODE (decl) == TEMPLATE_DECL)
7696 else
7698 /* The standard does not explicitly indicate whether a name that
7699 names a set of overloaded declarations, some of which are
7700 templates, is a template-name. However, such a name should
7701 be a template-name; otherwise, there is no way to form a
7702 template-id for the overloaded templates. */
7703 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7704 if (TREE_CODE (fns) == OVERLOAD)
7706 tree fn;
7708 for (fn = fns; fn; fn = OVL_NEXT (fn))
7709 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7710 break;
7712 else
7714 /* Otherwise, the name does not name a template. */
7715 cp_parser_error (parser, "expected template-name");
7716 return error_mark_node;
7720 /* If DECL is dependent, and refers to a function, then just return
7721 its name; we will look it up again during template instantiation. */
7722 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
7724 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
7725 if (TYPE_P (scope) && dependent_type_p (scope))
7726 return identifier;
7729 return decl;
7732 /* Parse a template-argument-list.
7734 template-argument-list:
7735 template-argument
7736 template-argument-list , template-argument
7738 Returns a TREE_VEC containing the arguments. */
7740 static tree
7741 cp_parser_template_argument_list (cp_parser* parser)
7743 tree fixed_args[10];
7744 unsigned n_args = 0;
7745 unsigned alloced = 10;
7746 tree *arg_ary = fixed_args;
7747 tree vec;
7751 tree argument;
7753 if (n_args)
7754 /* Consume the comma. */
7755 cp_lexer_consume_token (parser->lexer);
7757 /* Parse the template-argument. */
7758 argument = cp_parser_template_argument (parser);
7759 if (n_args == alloced)
7761 alloced *= 2;
7763 if (arg_ary == fixed_args)
7765 arg_ary = xmalloc (sizeof (tree) * alloced);
7766 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
7768 else
7769 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
7771 arg_ary[n_args++] = argument;
7773 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
7775 vec = make_tree_vec (n_args);
7777 while (n_args--)
7778 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
7780 if (arg_ary != fixed_args)
7781 free (arg_ary);
7782 return vec;
7785 /* Parse a template-argument.
7787 template-argument:
7788 assignment-expression
7789 type-id
7790 id-expression
7792 The representation is that of an assignment-expression, type-id, or
7793 id-expression -- except that the qualified id-expression is
7794 evaluated, so that the value returned is either a DECL or an
7795 OVERLOAD.
7797 Although the standard says "assignment-expression", it forbids
7798 throw-expressions or assignments in the template argument.
7799 Therefore, we use "conditional-expression" instead. */
7801 static tree
7802 cp_parser_template_argument (cp_parser* parser)
7804 tree argument;
7805 bool template_p;
7806 bool address_p;
7807 cp_token *token;
7808 cp_id_kind idk;
7809 tree qualifying_class;
7811 /* There's really no way to know what we're looking at, so we just
7812 try each alternative in order.
7814 [temp.arg]
7816 In a template-argument, an ambiguity between a type-id and an
7817 expression is resolved to a type-id, regardless of the form of
7818 the corresponding template-parameter.
7820 Therefore, we try a type-id first. */
7821 cp_parser_parse_tentatively (parser);
7822 argument = cp_parser_type_id (parser);
7823 /* If the next token isn't a `,' or a `>', then this argument wasn't
7824 really finished. */
7825 if (!cp_parser_next_token_ends_template_argument_p (parser))
7826 cp_parser_error (parser, "expected template-argument");
7827 /* If that worked, we're done. */
7828 if (cp_parser_parse_definitely (parser))
7829 return argument;
7830 /* We're still not sure what the argument will be. */
7831 cp_parser_parse_tentatively (parser);
7832 /* Try a template. */
7833 argument = cp_parser_id_expression (parser,
7834 /*template_keyword_p=*/false,
7835 /*check_dependency_p=*/true,
7836 &template_p,
7837 /*declarator_p=*/false);
7838 /* If the next token isn't a `,' or a `>', then this argument wasn't
7839 really finished. */
7840 if (!cp_parser_next_token_ends_template_argument_p (parser))
7841 cp_parser_error (parser, "expected template-argument");
7842 if (!cp_parser_error_occurred (parser))
7844 /* Figure out what is being referred to. */
7845 argument = cp_parser_lookup_name_simple (parser, argument);
7846 if (template_p)
7847 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
7848 TREE_OPERAND (argument, 1),
7849 tf_error);
7850 else if (TREE_CODE (argument) != TEMPLATE_DECL)
7851 cp_parser_error (parser, "expected template-name");
7853 if (cp_parser_parse_definitely (parser))
7854 return argument;
7855 /* It must be a non-type argument. There permitted cases are given
7856 in [temp.arg.nontype]:
7858 -- an integral constant-expression of integral or enumeration
7859 type; or
7861 -- the name of a non-type template-parameter; or
7863 -- the name of an object or function with external linkage...
7865 -- the address of an object or function with external linkage...
7867 -- a pointer to member... */
7868 /* Look for a non-type template parameter. */
7869 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7871 cp_parser_parse_tentatively (parser);
7872 argument = cp_parser_primary_expression (parser,
7873 &idk,
7874 &qualifying_class);
7875 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
7876 || !cp_parser_next_token_ends_template_argument_p (parser))
7877 cp_parser_simulate_error (parser);
7878 if (cp_parser_parse_definitely (parser))
7879 return argument;
7881 /* If the next token is "&", the argument must be the address of an
7882 object or function with external linkage. */
7883 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
7884 if (address_p)
7885 cp_lexer_consume_token (parser->lexer);
7886 /* See if we might have an id-expression. */
7887 token = cp_lexer_peek_token (parser->lexer);
7888 if (token->type == CPP_NAME
7889 || token->keyword == RID_OPERATOR
7890 || token->type == CPP_SCOPE
7891 || token->type == CPP_TEMPLATE_ID
7892 || token->type == CPP_NESTED_NAME_SPECIFIER)
7894 cp_parser_parse_tentatively (parser);
7895 argument = cp_parser_primary_expression (parser,
7896 &idk,
7897 &qualifying_class);
7898 if (cp_parser_error_occurred (parser)
7899 || !cp_parser_next_token_ends_template_argument_p (parser))
7900 cp_parser_abort_tentative_parse (parser);
7901 else
7903 if (qualifying_class)
7904 argument = finish_qualified_id_expr (qualifying_class,
7905 argument,
7906 /*done=*/true,
7907 address_p);
7908 if (TREE_CODE (argument) == VAR_DECL)
7910 /* A variable without external linkage might still be a
7911 valid constant-expression, so no error is issued here
7912 if the external-linkage check fails. */
7913 if (!DECL_EXTERNAL_LINKAGE_P (argument))
7914 cp_parser_simulate_error (parser);
7916 else if (is_overloaded_fn (argument))
7917 /* All overloaded functions are allowed; if the external
7918 linkage test does not pass, an error will be issued
7919 later. */
7921 else if (address_p
7922 && (TREE_CODE (argument) == OFFSET_REF
7923 || TREE_CODE (argument) == SCOPE_REF))
7924 /* A pointer-to-member. */
7926 else
7927 cp_parser_simulate_error (parser);
7929 if (cp_parser_parse_definitely (parser))
7931 if (address_p)
7932 argument = build_x_unary_op (ADDR_EXPR, argument);
7933 return argument;
7937 /* If the argument started with "&", there are no other valid
7938 alternatives at this point. */
7939 if (address_p)
7941 cp_parser_error (parser, "invalid non-type template argument");
7942 return error_mark_node;
7944 /* The argument must be a constant-expression. */
7945 argument = cp_parser_constant_expression (parser,
7946 /*allow_non_constant_p=*/false,
7947 /*non_constant_p=*/NULL);
7948 /* If it's non-dependent, simplify it. */
7949 return cp_parser_fold_non_dependent_expr (argument);
7952 /* Parse an explicit-instantiation.
7954 explicit-instantiation:
7955 template declaration
7957 Although the standard says `declaration', what it really means is:
7959 explicit-instantiation:
7960 template decl-specifier-seq [opt] declarator [opt] ;
7962 Things like `template int S<int>::i = 5, int S<double>::j;' are not
7963 supposed to be allowed. A defect report has been filed about this
7964 issue.
7966 GNU Extension:
7968 explicit-instantiation:
7969 storage-class-specifier template
7970 decl-specifier-seq [opt] declarator [opt] ;
7971 function-specifier template
7972 decl-specifier-seq [opt] declarator [opt] ; */
7974 static void
7975 cp_parser_explicit_instantiation (cp_parser* parser)
7977 int declares_class_or_enum;
7978 tree decl_specifiers;
7979 tree attributes;
7980 tree extension_specifier = NULL_TREE;
7982 /* Look for an (optional) storage-class-specifier or
7983 function-specifier. */
7984 if (cp_parser_allow_gnu_extensions_p (parser))
7986 extension_specifier
7987 = cp_parser_storage_class_specifier_opt (parser);
7988 if (!extension_specifier)
7989 extension_specifier = cp_parser_function_specifier_opt (parser);
7992 /* Look for the `template' keyword. */
7993 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
7994 /* Let the front end know that we are processing an explicit
7995 instantiation. */
7996 begin_explicit_instantiation ();
7997 /* [temp.explicit] says that we are supposed to ignore access
7998 control while processing explicit instantiation directives. */
7999 push_deferring_access_checks (dk_no_check);
8000 /* Parse a decl-specifier-seq. */
8001 decl_specifiers
8002 = cp_parser_decl_specifier_seq (parser,
8003 CP_PARSER_FLAGS_OPTIONAL,
8004 &attributes,
8005 &declares_class_or_enum);
8006 /* If there was exactly one decl-specifier, and it declared a class,
8007 and there's no declarator, then we have an explicit type
8008 instantiation. */
8009 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8011 tree type;
8013 type = check_tag_decl (decl_specifiers);
8014 /* Turn access control back on for names used during
8015 template instantiation. */
8016 pop_deferring_access_checks ();
8017 if (type)
8018 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8020 else
8022 tree declarator;
8023 tree decl;
8025 /* Parse the declarator. */
8026 declarator
8027 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8028 /*ctor_dtor_or_conv_p=*/NULL);
8029 cp_parser_check_for_definition_in_return_type (declarator,
8030 declares_class_or_enum);
8031 decl = grokdeclarator (declarator, decl_specifiers,
8032 NORMAL, 0, NULL);
8033 /* Turn access control back on for names used during
8034 template instantiation. */
8035 pop_deferring_access_checks ();
8036 /* Do the explicit instantiation. */
8037 do_decl_instantiation (decl, extension_specifier);
8039 /* We're done with the instantiation. */
8040 end_explicit_instantiation ();
8042 cp_parser_consume_semicolon_at_end_of_statement (parser);
8045 /* Parse an explicit-specialization.
8047 explicit-specialization:
8048 template < > declaration
8050 Although the standard says `declaration', what it really means is:
8052 explicit-specialization:
8053 template <> decl-specifier [opt] init-declarator [opt] ;
8054 template <> function-definition
8055 template <> explicit-specialization
8056 template <> template-declaration */
8058 static void
8059 cp_parser_explicit_specialization (cp_parser* parser)
8061 /* Look for the `template' keyword. */
8062 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8063 /* Look for the `<'. */
8064 cp_parser_require (parser, CPP_LESS, "`<'");
8065 /* Look for the `>'. */
8066 cp_parser_require (parser, CPP_GREATER, "`>'");
8067 /* We have processed another parameter list. */
8068 ++parser->num_template_parameter_lists;
8069 /* Let the front end know that we are beginning a specialization. */
8070 begin_specialization ();
8072 /* If the next keyword is `template', we need to figure out whether
8073 or not we're looking a template-declaration. */
8074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8076 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8077 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8078 cp_parser_template_declaration_after_export (parser,
8079 /*member_p=*/false);
8080 else
8081 cp_parser_explicit_specialization (parser);
8083 else
8084 /* Parse the dependent declaration. */
8085 cp_parser_single_declaration (parser,
8086 /*member_p=*/false,
8087 /*friend_p=*/NULL);
8089 /* We're done with the specialization. */
8090 end_specialization ();
8091 /* We're done with this parameter list. */
8092 --parser->num_template_parameter_lists;
8095 /* Parse a type-specifier.
8097 type-specifier:
8098 simple-type-specifier
8099 class-specifier
8100 enum-specifier
8101 elaborated-type-specifier
8102 cv-qualifier
8104 GNU Extension:
8106 type-specifier:
8107 __complex__
8109 Returns a representation of the type-specifier. If the
8110 type-specifier is a keyword (like `int' or `const', or
8111 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8112 For a class-specifier, enum-specifier, or elaborated-type-specifier
8113 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8115 If IS_FRIEND is TRUE then this type-specifier is being declared a
8116 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8117 appearing in a decl-specifier-seq.
8119 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8120 class-specifier, enum-specifier, or elaborated-type-specifier, then
8121 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8122 if a type is declared; 2 if it is defined. Otherwise, it is set to
8123 zero.
8125 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8126 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8127 is set to FALSE. */
8129 static tree
8130 cp_parser_type_specifier (cp_parser* parser,
8131 cp_parser_flags flags,
8132 bool is_friend,
8133 bool is_declaration,
8134 int* declares_class_or_enum,
8135 bool* is_cv_qualifier)
8137 tree type_spec = NULL_TREE;
8138 cp_token *token;
8139 enum rid keyword;
8141 /* Assume this type-specifier does not declare a new type. */
8142 if (declares_class_or_enum)
8143 *declares_class_or_enum = false;
8144 /* And that it does not specify a cv-qualifier. */
8145 if (is_cv_qualifier)
8146 *is_cv_qualifier = false;
8147 /* Peek at the next token. */
8148 token = cp_lexer_peek_token (parser->lexer);
8150 /* If we're looking at a keyword, we can use that to guide the
8151 production we choose. */
8152 keyword = token->keyword;
8153 switch (keyword)
8155 /* Any of these indicate either a class-specifier, or an
8156 elaborated-type-specifier. */
8157 case RID_CLASS:
8158 case RID_STRUCT:
8159 case RID_UNION:
8160 case RID_ENUM:
8161 /* Parse tentatively so that we can back up if we don't find a
8162 class-specifier or enum-specifier. */
8163 cp_parser_parse_tentatively (parser);
8164 /* Look for the class-specifier or enum-specifier. */
8165 if (keyword == RID_ENUM)
8166 type_spec = cp_parser_enum_specifier (parser);
8167 else
8168 type_spec = cp_parser_class_specifier (parser);
8170 /* If that worked, we're done. */
8171 if (cp_parser_parse_definitely (parser))
8173 if (declares_class_or_enum)
8174 *declares_class_or_enum = 2;
8175 return type_spec;
8178 /* Fall through. */
8180 case RID_TYPENAME:
8181 /* Look for an elaborated-type-specifier. */
8182 type_spec = cp_parser_elaborated_type_specifier (parser,
8183 is_friend,
8184 is_declaration);
8185 /* We're declaring a class or enum -- unless we're using
8186 `typename'. */
8187 if (declares_class_or_enum && keyword != RID_TYPENAME)
8188 *declares_class_or_enum = 1;
8189 return type_spec;
8191 case RID_CONST:
8192 case RID_VOLATILE:
8193 case RID_RESTRICT:
8194 type_spec = cp_parser_cv_qualifier_opt (parser);
8195 /* Even though we call a routine that looks for an optional
8196 qualifier, we know that there should be one. */
8197 my_friendly_assert (type_spec != NULL, 20000328);
8198 /* This type-specifier was a cv-qualified. */
8199 if (is_cv_qualifier)
8200 *is_cv_qualifier = true;
8202 return type_spec;
8204 case RID_COMPLEX:
8205 /* The `__complex__' keyword is a GNU extension. */
8206 return cp_lexer_consume_token (parser->lexer)->value;
8208 default:
8209 break;
8212 /* If we do not already have a type-specifier, assume we are looking
8213 at a simple-type-specifier. */
8214 type_spec = cp_parser_simple_type_specifier (parser, flags,
8215 /*identifier_p=*/true);
8217 /* If we didn't find a type-specifier, and a type-specifier was not
8218 optional in this context, issue an error message. */
8219 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8221 cp_parser_error (parser, "expected type specifier");
8222 return error_mark_node;
8225 return type_spec;
8228 /* Parse a simple-type-specifier.
8230 simple-type-specifier:
8231 :: [opt] nested-name-specifier [opt] type-name
8232 :: [opt] nested-name-specifier template template-id
8233 char
8234 wchar_t
8235 bool
8236 short
8238 long
8239 signed
8240 unsigned
8241 float
8242 double
8243 void
8245 GNU Extension:
8247 simple-type-specifier:
8248 __typeof__ unary-expression
8249 __typeof__ ( type-id )
8251 For the various keywords, the value returned is simply the
8252 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8253 For the first two productions, and if IDENTIFIER_P is false, the
8254 value returned is the indicated TYPE_DECL. */
8256 static tree
8257 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8258 bool identifier_p)
8260 tree type = NULL_TREE;
8261 cp_token *token;
8263 /* Peek at the next token. */
8264 token = cp_lexer_peek_token (parser->lexer);
8266 /* If we're looking at a keyword, things are easy. */
8267 switch (token->keyword)
8269 case RID_CHAR:
8270 type = char_type_node;
8271 break;
8272 case RID_WCHAR:
8273 type = wchar_type_node;
8274 break;
8275 case RID_BOOL:
8276 type = boolean_type_node;
8277 break;
8278 case RID_SHORT:
8279 type = short_integer_type_node;
8280 break;
8281 case RID_INT:
8282 type = integer_type_node;
8283 break;
8284 case RID_LONG:
8285 type = long_integer_type_node;
8286 break;
8287 case RID_SIGNED:
8288 type = integer_type_node;
8289 break;
8290 case RID_UNSIGNED:
8291 type = unsigned_type_node;
8292 break;
8293 case RID_FLOAT:
8294 type = float_type_node;
8295 break;
8296 case RID_DOUBLE:
8297 type = double_type_node;
8298 break;
8299 case RID_VOID:
8300 type = void_type_node;
8301 break;
8303 case RID_TYPEOF:
8305 tree operand;
8307 /* Consume the `typeof' token. */
8308 cp_lexer_consume_token (parser->lexer);
8309 /* Parse the operand to `typeof' */
8310 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8311 /* If it is not already a TYPE, take its type. */
8312 if (!TYPE_P (operand))
8313 operand = finish_typeof (operand);
8315 return operand;
8318 default:
8319 break;
8322 /* If the type-specifier was for a built-in type, we're done. */
8323 if (type)
8325 tree id;
8327 /* Consume the token. */
8328 id = cp_lexer_consume_token (parser->lexer)->value;
8329 return identifier_p ? id : TYPE_NAME (type);
8332 /* The type-specifier must be a user-defined type. */
8333 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8335 /* Don't gobble tokens or issue error messages if this is an
8336 optional type-specifier. */
8337 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8338 cp_parser_parse_tentatively (parser);
8340 /* Look for the optional `::' operator. */
8341 cp_parser_global_scope_opt (parser,
8342 /*current_scope_valid_p=*/false);
8343 /* Look for the nested-name specifier. */
8344 cp_parser_nested_name_specifier_opt (parser,
8345 /*typename_keyword_p=*/false,
8346 /*check_dependency_p=*/true,
8347 /*type_p=*/false);
8348 /* If we have seen a nested-name-specifier, and the next token
8349 is `template', then we are using the template-id production. */
8350 if (parser->scope
8351 && cp_parser_optional_template_keyword (parser))
8353 /* Look for the template-id. */
8354 type = cp_parser_template_id (parser,
8355 /*template_keyword_p=*/true,
8356 /*check_dependency_p=*/true);
8357 /* If the template-id did not name a type, we are out of
8358 luck. */
8359 if (TREE_CODE (type) != TYPE_DECL)
8361 cp_parser_error (parser, "expected template-id for type");
8362 type = NULL_TREE;
8365 /* Otherwise, look for a type-name. */
8366 else
8368 type = cp_parser_type_name (parser);
8369 if (type == error_mark_node)
8370 type = NULL_TREE;
8373 /* If it didn't work out, we don't have a TYPE. */
8374 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8375 && !cp_parser_parse_definitely (parser))
8376 type = NULL_TREE;
8379 /* If we didn't get a type-name, issue an error message. */
8380 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8382 cp_parser_error (parser, "expected type-name");
8383 return error_mark_node;
8386 return type;
8389 /* Parse a type-name.
8391 type-name:
8392 class-name
8393 enum-name
8394 typedef-name
8396 enum-name:
8397 identifier
8399 typedef-name:
8400 identifier
8402 Returns a TYPE_DECL for the the type. */
8404 static tree
8405 cp_parser_type_name (cp_parser* parser)
8407 tree type_decl;
8408 tree identifier;
8410 /* We can't know yet whether it is a class-name or not. */
8411 cp_parser_parse_tentatively (parser);
8412 /* Try a class-name. */
8413 type_decl = cp_parser_class_name (parser,
8414 /*typename_keyword_p=*/false,
8415 /*template_keyword_p=*/false,
8416 /*type_p=*/false,
8417 /*check_dependency_p=*/true,
8418 /*class_head_p=*/false);
8419 /* If it's not a class-name, keep looking. */
8420 if (!cp_parser_parse_definitely (parser))
8422 /* It must be a typedef-name or an enum-name. */
8423 identifier = cp_parser_identifier (parser);
8424 if (identifier == error_mark_node)
8425 return error_mark_node;
8427 /* Look up the type-name. */
8428 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8429 /* Issue an error if we did not find a type-name. */
8430 if (TREE_CODE (type_decl) != TYPE_DECL)
8432 cp_parser_error (parser, "expected type-name");
8433 type_decl = error_mark_node;
8435 /* Remember that the name was used in the definition of the
8436 current class so that we can check later to see if the
8437 meaning would have been different after the class was
8438 entirely defined. */
8439 else if (type_decl != error_mark_node
8440 && !parser->scope)
8441 maybe_note_name_used_in_class (identifier, type_decl);
8444 return type_decl;
8448 /* Parse an elaborated-type-specifier. Note that the grammar given
8449 here incorporates the resolution to DR68.
8451 elaborated-type-specifier:
8452 class-key :: [opt] nested-name-specifier [opt] identifier
8453 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8454 enum :: [opt] nested-name-specifier [opt] identifier
8455 typename :: [opt] nested-name-specifier identifier
8456 typename :: [opt] nested-name-specifier template [opt]
8457 template-id
8459 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8460 declared `friend'. If IS_DECLARATION is TRUE, then this
8461 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8462 something is being declared.
8464 Returns the TYPE specified. */
8466 static tree
8467 cp_parser_elaborated_type_specifier (cp_parser* parser,
8468 bool is_friend,
8469 bool is_declaration)
8471 enum tag_types tag_type;
8472 tree identifier;
8473 tree type = NULL_TREE;
8475 /* See if we're looking at the `enum' keyword. */
8476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8478 /* Consume the `enum' token. */
8479 cp_lexer_consume_token (parser->lexer);
8480 /* Remember that it's an enumeration type. */
8481 tag_type = enum_type;
8483 /* Or, it might be `typename'. */
8484 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8485 RID_TYPENAME))
8487 /* Consume the `typename' token. */
8488 cp_lexer_consume_token (parser->lexer);
8489 /* Remember that it's a `typename' type. */
8490 tag_type = typename_type;
8491 /* The `typename' keyword is only allowed in templates. */
8492 if (!processing_template_decl)
8493 pedwarn ("using `typename' outside of template");
8495 /* Otherwise it must be a class-key. */
8496 else
8498 tag_type = cp_parser_class_key (parser);
8499 if (tag_type == none_type)
8500 return error_mark_node;
8503 /* Look for the `::' operator. */
8504 cp_parser_global_scope_opt (parser,
8505 /*current_scope_valid_p=*/false);
8506 /* Look for the nested-name-specifier. */
8507 if (tag_type == typename_type)
8509 if (cp_parser_nested_name_specifier (parser,
8510 /*typename_keyword_p=*/true,
8511 /*check_dependency_p=*/true,
8512 /*type_p=*/true)
8513 == error_mark_node)
8514 return error_mark_node;
8516 else
8517 /* Even though `typename' is not present, the proposed resolution
8518 to Core Issue 180 says that in `class A<T>::B', `B' should be
8519 considered a type-name, even if `A<T>' is dependent. */
8520 cp_parser_nested_name_specifier_opt (parser,
8521 /*typename_keyword_p=*/true,
8522 /*check_dependency_p=*/true,
8523 /*type_p=*/true);
8524 /* For everything but enumeration types, consider a template-id. */
8525 if (tag_type != enum_type)
8527 bool template_p = false;
8528 tree decl;
8530 /* Allow the `template' keyword. */
8531 template_p = cp_parser_optional_template_keyword (parser);
8532 /* If we didn't see `template', we don't know if there's a
8533 template-id or not. */
8534 if (!template_p)
8535 cp_parser_parse_tentatively (parser);
8536 /* Parse the template-id. */
8537 decl = cp_parser_template_id (parser, template_p,
8538 /*check_dependency_p=*/true);
8539 /* If we didn't find a template-id, look for an ordinary
8540 identifier. */
8541 if (!template_p && !cp_parser_parse_definitely (parser))
8543 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8544 in effect, then we must assume that, upon instantiation, the
8545 template will correspond to a class. */
8546 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8547 && tag_type == typename_type)
8548 type = make_typename_type (parser->scope, decl,
8549 /*complain=*/1);
8550 else
8551 type = TREE_TYPE (decl);
8554 /* For an enumeration type, consider only a plain identifier. */
8555 if (!type)
8557 identifier = cp_parser_identifier (parser);
8559 if (identifier == error_mark_node)
8561 parser->scope = NULL_TREE;
8562 return error_mark_node;
8565 /* For a `typename', we needn't call xref_tag. */
8566 if (tag_type == typename_type)
8567 return make_typename_type (parser->scope, identifier,
8568 /*complain=*/1);
8569 /* Look up a qualified name in the usual way. */
8570 if (parser->scope)
8572 tree decl;
8574 /* In an elaborated-type-specifier, names are assumed to name
8575 types, so we set IS_TYPE to TRUE when calling
8576 cp_parser_lookup_name. */
8577 decl = cp_parser_lookup_name (parser, identifier,
8578 /*is_type=*/true,
8579 /*is_namespace=*/false,
8580 /*check_dependency=*/true);
8582 /* If we are parsing friend declaration, DECL may be a
8583 TEMPLATE_DECL tree node here. However, we need to check
8584 whether this TEMPLATE_DECL results in valid code. Consider
8585 the following example:
8587 namespace N {
8588 template <class T> class C {};
8590 class X {
8591 template <class T> friend class N::C; // #1, valid code
8593 template <class T> class Y {
8594 friend class N::C; // #2, invalid code
8597 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8598 name lookup of `N::C'. We see that friend declaration must
8599 be template for the code to be valid. Note that
8600 processing_template_decl does not work here since it is
8601 always 1 for the above two cases. */
8603 decl = (cp_parser_maybe_treat_template_as_class
8604 (decl, /*tag_name_p=*/is_friend
8605 && parser->num_template_parameter_lists));
8607 if (TREE_CODE (decl) != TYPE_DECL)
8609 error ("expected type-name");
8610 return error_mark_node;
8613 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
8614 check_elaborated_type_specifier
8615 (tag_type, decl,
8616 (parser->num_template_parameter_lists
8617 || DECL_SELF_REFERENCE_P (decl)));
8619 type = TREE_TYPE (decl);
8621 else
8623 /* An elaborated-type-specifier sometimes introduces a new type and
8624 sometimes names an existing type. Normally, the rule is that it
8625 introduces a new type only if there is not an existing type of
8626 the same name already in scope. For example, given:
8628 struct S {};
8629 void f() { struct S s; }
8631 the `struct S' in the body of `f' is the same `struct S' as in
8632 the global scope; the existing definition is used. However, if
8633 there were no global declaration, this would introduce a new
8634 local class named `S'.
8636 An exception to this rule applies to the following code:
8638 namespace N { struct S; }
8640 Here, the elaborated-type-specifier names a new type
8641 unconditionally; even if there is already an `S' in the
8642 containing scope this declaration names a new type.
8643 This exception only applies if the elaborated-type-specifier
8644 forms the complete declaration:
8646 [class.name]
8648 A declaration consisting solely of `class-key identifier ;' is
8649 either a redeclaration of the name in the current scope or a
8650 forward declaration of the identifier as a class name. It
8651 introduces the name into the current scope.
8653 We are in this situation precisely when the next token is a `;'.
8655 An exception to the exception is that a `friend' declaration does
8656 *not* name a new type; i.e., given:
8658 struct S { friend struct T; };
8660 `T' is not a new type in the scope of `S'.
8662 Also, `new struct S' or `sizeof (struct S)' never results in the
8663 definition of a new type; a new type can only be declared in a
8664 declaration context. */
8666 type = xref_tag (tag_type, identifier,
8667 /*attributes=*/NULL_TREE,
8668 (is_friend
8669 || !is_declaration
8670 || cp_lexer_next_token_is_not (parser->lexer,
8671 CPP_SEMICOLON)),
8672 parser->num_template_parameter_lists);
8675 if (tag_type != enum_type)
8676 cp_parser_check_class_key (tag_type, type);
8677 return type;
8680 /* Parse an enum-specifier.
8682 enum-specifier:
8683 enum identifier [opt] { enumerator-list [opt] }
8685 Returns an ENUM_TYPE representing the enumeration. */
8687 static tree
8688 cp_parser_enum_specifier (cp_parser* parser)
8690 cp_token *token;
8691 tree identifier = NULL_TREE;
8692 tree type;
8694 /* Look for the `enum' keyword. */
8695 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8696 return error_mark_node;
8697 /* Peek at the next token. */
8698 token = cp_lexer_peek_token (parser->lexer);
8700 /* See if it is an identifier. */
8701 if (token->type == CPP_NAME)
8702 identifier = cp_parser_identifier (parser);
8704 /* Look for the `{'. */
8705 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8706 return error_mark_node;
8708 /* At this point, we're going ahead with the enum-specifier, even
8709 if some other problem occurs. */
8710 cp_parser_commit_to_tentative_parse (parser);
8712 /* Issue an error message if type-definitions are forbidden here. */
8713 cp_parser_check_type_definition (parser);
8715 /* Create the new type. */
8716 type = start_enum (identifier ? identifier : make_anon_name ());
8718 /* Peek at the next token. */
8719 token = cp_lexer_peek_token (parser->lexer);
8720 /* If it's not a `}', then there are some enumerators. */
8721 if (token->type != CPP_CLOSE_BRACE)
8722 cp_parser_enumerator_list (parser, type);
8723 /* Look for the `}'. */
8724 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8726 /* Finish up the enumeration. */
8727 finish_enum (type);
8729 return type;
8732 /* Parse an enumerator-list. The enumerators all have the indicated
8733 TYPE.
8735 enumerator-list:
8736 enumerator-definition
8737 enumerator-list , enumerator-definition */
8739 static void
8740 cp_parser_enumerator_list (cp_parser* parser, tree type)
8742 while (true)
8744 cp_token *token;
8746 /* Parse an enumerator-definition. */
8747 cp_parser_enumerator_definition (parser, type);
8748 /* Peek at the next token. */
8749 token = cp_lexer_peek_token (parser->lexer);
8750 /* If it's not a `,', then we've reached the end of the
8751 list. */
8752 if (token->type != CPP_COMMA)
8753 break;
8754 /* Otherwise, consume the `,' and keep going. */
8755 cp_lexer_consume_token (parser->lexer);
8756 /* If the next token is a `}', there is a trailing comma. */
8757 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8759 if (pedantic && !in_system_header)
8760 pedwarn ("comma at end of enumerator list");
8761 break;
8766 /* Parse an enumerator-definition. The enumerator has the indicated
8767 TYPE.
8769 enumerator-definition:
8770 enumerator
8771 enumerator = constant-expression
8773 enumerator:
8774 identifier */
8776 static void
8777 cp_parser_enumerator_definition (cp_parser* parser, tree type)
8779 cp_token *token;
8780 tree identifier;
8781 tree value;
8783 /* Look for the identifier. */
8784 identifier = cp_parser_identifier (parser);
8785 if (identifier == error_mark_node)
8786 return;
8788 /* Peek at the next token. */
8789 token = cp_lexer_peek_token (parser->lexer);
8790 /* If it's an `=', then there's an explicit value. */
8791 if (token->type == CPP_EQ)
8793 /* Consume the `=' token. */
8794 cp_lexer_consume_token (parser->lexer);
8795 /* Parse the value. */
8796 value = cp_parser_constant_expression (parser,
8797 /*allow_non_constant_p=*/false,
8798 NULL);
8800 else
8801 value = NULL_TREE;
8803 /* Create the enumerator. */
8804 build_enumerator (identifier, value, type);
8807 /* Parse a namespace-name.
8809 namespace-name:
8810 original-namespace-name
8811 namespace-alias
8813 Returns the NAMESPACE_DECL for the namespace. */
8815 static tree
8816 cp_parser_namespace_name (cp_parser* parser)
8818 tree identifier;
8819 tree namespace_decl;
8821 /* Get the name of the namespace. */
8822 identifier = cp_parser_identifier (parser);
8823 if (identifier == error_mark_node)
8824 return error_mark_node;
8826 /* Look up the identifier in the currently active scope. Look only
8827 for namespaces, due to:
8829 [basic.lookup.udir]
8831 When looking up a namespace-name in a using-directive or alias
8832 definition, only namespace names are considered.
8834 And:
8836 [basic.lookup.qual]
8838 During the lookup of a name preceding the :: scope resolution
8839 operator, object, function, and enumerator names are ignored.
8841 (Note that cp_parser_class_or_namespace_name only calls this
8842 function if the token after the name is the scope resolution
8843 operator.) */
8844 namespace_decl = cp_parser_lookup_name (parser, identifier,
8845 /*is_type=*/false,
8846 /*is_namespace=*/true,
8847 /*check_dependency=*/true);
8848 /* If it's not a namespace, issue an error. */
8849 if (namespace_decl == error_mark_node
8850 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
8852 cp_parser_error (parser, "expected namespace-name");
8853 namespace_decl = error_mark_node;
8856 return namespace_decl;
8859 /* Parse a namespace-definition.
8861 namespace-definition:
8862 named-namespace-definition
8863 unnamed-namespace-definition
8865 named-namespace-definition:
8866 original-namespace-definition
8867 extension-namespace-definition
8869 original-namespace-definition:
8870 namespace identifier { namespace-body }
8872 extension-namespace-definition:
8873 namespace original-namespace-name { namespace-body }
8875 unnamed-namespace-definition:
8876 namespace { namespace-body } */
8878 static void
8879 cp_parser_namespace_definition (cp_parser* parser)
8881 tree identifier;
8883 /* Look for the `namespace' keyword. */
8884 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8886 /* Get the name of the namespace. We do not attempt to distinguish
8887 between an original-namespace-definition and an
8888 extension-namespace-definition at this point. The semantic
8889 analysis routines are responsible for that. */
8890 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8891 identifier = cp_parser_identifier (parser);
8892 else
8893 identifier = NULL_TREE;
8895 /* Look for the `{' to start the namespace. */
8896 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
8897 /* Start the namespace. */
8898 push_namespace (identifier);
8899 /* Parse the body of the namespace. */
8900 cp_parser_namespace_body (parser);
8901 /* Finish the namespace. */
8902 pop_namespace ();
8903 /* Look for the final `}'. */
8904 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8907 /* Parse a namespace-body.
8909 namespace-body:
8910 declaration-seq [opt] */
8912 static void
8913 cp_parser_namespace_body (cp_parser* parser)
8915 cp_parser_declaration_seq_opt (parser);
8918 /* Parse a namespace-alias-definition.
8920 namespace-alias-definition:
8921 namespace identifier = qualified-namespace-specifier ; */
8923 static void
8924 cp_parser_namespace_alias_definition (cp_parser* parser)
8926 tree identifier;
8927 tree namespace_specifier;
8929 /* Look for the `namespace' keyword. */
8930 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
8931 /* Look for the identifier. */
8932 identifier = cp_parser_identifier (parser);
8933 if (identifier == error_mark_node)
8934 return;
8935 /* Look for the `=' token. */
8936 cp_parser_require (parser, CPP_EQ, "`='");
8937 /* Look for the qualified-namespace-specifier. */
8938 namespace_specifier
8939 = cp_parser_qualified_namespace_specifier (parser);
8940 /* Look for the `;' token. */
8941 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8943 /* Register the alias in the symbol table. */
8944 do_namespace_alias (identifier, namespace_specifier);
8947 /* Parse a qualified-namespace-specifier.
8949 qualified-namespace-specifier:
8950 :: [opt] nested-name-specifier [opt] namespace-name
8952 Returns a NAMESPACE_DECL corresponding to the specified
8953 namespace. */
8955 static tree
8956 cp_parser_qualified_namespace_specifier (cp_parser* parser)
8958 /* Look for the optional `::'. */
8959 cp_parser_global_scope_opt (parser,
8960 /*current_scope_valid_p=*/false);
8962 /* Look for the optional nested-name-specifier. */
8963 cp_parser_nested_name_specifier_opt (parser,
8964 /*typename_keyword_p=*/false,
8965 /*check_dependency_p=*/true,
8966 /*type_p=*/false);
8968 return cp_parser_namespace_name (parser);
8971 /* Parse a using-declaration.
8973 using-declaration:
8974 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
8975 using :: unqualified-id ; */
8977 static void
8978 cp_parser_using_declaration (cp_parser* parser)
8980 cp_token *token;
8981 bool typename_p = false;
8982 bool global_scope_p;
8983 tree decl;
8984 tree identifier;
8985 tree scope;
8987 /* Look for the `using' keyword. */
8988 cp_parser_require_keyword (parser, RID_USING, "`using'");
8990 /* Peek at the next token. */
8991 token = cp_lexer_peek_token (parser->lexer);
8992 /* See if it's `typename'. */
8993 if (token->keyword == RID_TYPENAME)
8995 /* Remember that we've seen it. */
8996 typename_p = true;
8997 /* Consume the `typename' token. */
8998 cp_lexer_consume_token (parser->lexer);
9001 /* Look for the optional global scope qualification. */
9002 global_scope_p
9003 = (cp_parser_global_scope_opt (parser,
9004 /*current_scope_valid_p=*/false)
9005 != NULL_TREE);
9007 /* If we saw `typename', or didn't see `::', then there must be a
9008 nested-name-specifier present. */
9009 if (typename_p || !global_scope_p)
9010 cp_parser_nested_name_specifier (parser, typename_p,
9011 /*check_dependency_p=*/true,
9012 /*type_p=*/false);
9013 /* Otherwise, we could be in either of the two productions. In that
9014 case, treat the nested-name-specifier as optional. */
9015 else
9016 cp_parser_nested_name_specifier_opt (parser,
9017 /*typename_keyword_p=*/false,
9018 /*check_dependency_p=*/true,
9019 /*type_p=*/false);
9021 /* Parse the unqualified-id. */
9022 identifier = cp_parser_unqualified_id (parser,
9023 /*template_keyword_p=*/false,
9024 /*check_dependency_p=*/true,
9025 /*declarator_p=*/true);
9027 /* The function we call to handle a using-declaration is different
9028 depending on what scope we are in. */
9029 if (identifier == error_mark_node)
9031 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9032 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9033 /* [namespace.udecl]
9035 A using declaration shall not name a template-id. */
9036 error ("a template-id may not appear in a using-declaration");
9037 else
9039 scope = current_scope ();
9040 if (scope && TYPE_P (scope))
9042 /* Create the USING_DECL. */
9043 decl = do_class_using_decl (build_nt (SCOPE_REF,
9044 parser->scope,
9045 identifier));
9046 /* Add it to the list of members in this class. */
9047 finish_member_declaration (decl);
9049 else
9051 decl = cp_parser_lookup_name_simple (parser, identifier);
9052 if (decl == error_mark_node)
9054 if (parser->scope && parser->scope != global_namespace)
9055 error ("`%D::%D' has not been declared",
9056 parser->scope, identifier);
9057 else
9058 error ("`::%D' has not been declared", identifier);
9060 else if (scope)
9061 do_local_using_decl (decl);
9062 else
9063 do_toplevel_using_decl (decl);
9067 /* Look for the final `;'. */
9068 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9071 /* Parse a using-directive.
9073 using-directive:
9074 using namespace :: [opt] nested-name-specifier [opt]
9075 namespace-name ; */
9077 static void
9078 cp_parser_using_directive (cp_parser* parser)
9080 tree namespace_decl;
9082 /* Look for the `using' keyword. */
9083 cp_parser_require_keyword (parser, RID_USING, "`using'");
9084 /* And the `namespace' keyword. */
9085 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9086 /* Look for the optional `::' operator. */
9087 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9088 /* And the optional nested-name-specifier. */
9089 cp_parser_nested_name_specifier_opt (parser,
9090 /*typename_keyword_p=*/false,
9091 /*check_dependency_p=*/true,
9092 /*type_p=*/false);
9093 /* Get the namespace being used. */
9094 namespace_decl = cp_parser_namespace_name (parser);
9095 /* Update the symbol table. */
9096 do_using_directive (namespace_decl);
9097 /* Look for the final `;'. */
9098 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9101 /* Parse an asm-definition.
9103 asm-definition:
9104 asm ( string-literal ) ;
9106 GNU Extension:
9108 asm-definition:
9109 asm volatile [opt] ( string-literal ) ;
9110 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9111 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9112 : asm-operand-list [opt] ) ;
9113 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9114 : asm-operand-list [opt]
9115 : asm-operand-list [opt] ) ; */
9117 static void
9118 cp_parser_asm_definition (cp_parser* parser)
9120 cp_token *token;
9121 tree string;
9122 tree outputs = NULL_TREE;
9123 tree inputs = NULL_TREE;
9124 tree clobbers = NULL_TREE;
9125 tree asm_stmt;
9126 bool volatile_p = false;
9127 bool extended_p = false;
9129 /* Look for the `asm' keyword. */
9130 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9131 /* See if the next token is `volatile'. */
9132 if (cp_parser_allow_gnu_extensions_p (parser)
9133 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9135 /* Remember that we saw the `volatile' keyword. */
9136 volatile_p = true;
9137 /* Consume the token. */
9138 cp_lexer_consume_token (parser->lexer);
9140 /* Look for the opening `('. */
9141 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9142 /* Look for the string. */
9143 token = cp_parser_require (parser, CPP_STRING, "asm body");
9144 if (!token)
9145 return;
9146 string = token->value;
9147 /* If we're allowing GNU extensions, check for the extended assembly
9148 syntax. Unfortunately, the `:' tokens need not be separated by
9149 a space in C, and so, for compatibility, we tolerate that here
9150 too. Doing that means that we have to treat the `::' operator as
9151 two `:' tokens. */
9152 if (cp_parser_allow_gnu_extensions_p (parser)
9153 && at_function_scope_p ()
9154 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9155 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9157 bool inputs_p = false;
9158 bool clobbers_p = false;
9160 /* The extended syntax was used. */
9161 extended_p = true;
9163 /* Look for outputs. */
9164 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9166 /* Consume the `:'. */
9167 cp_lexer_consume_token (parser->lexer);
9168 /* Parse the output-operands. */
9169 if (cp_lexer_next_token_is_not (parser->lexer,
9170 CPP_COLON)
9171 && cp_lexer_next_token_is_not (parser->lexer,
9172 CPP_SCOPE)
9173 && cp_lexer_next_token_is_not (parser->lexer,
9174 CPP_CLOSE_PAREN))
9175 outputs = cp_parser_asm_operand_list (parser);
9177 /* If the next token is `::', there are no outputs, and the
9178 next token is the beginning of the inputs. */
9179 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9181 /* Consume the `::' token. */
9182 cp_lexer_consume_token (parser->lexer);
9183 /* The inputs are coming next. */
9184 inputs_p = true;
9187 /* Look for inputs. */
9188 if (inputs_p
9189 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9191 if (!inputs_p)
9192 /* Consume the `:'. */
9193 cp_lexer_consume_token (parser->lexer);
9194 /* Parse the output-operands. */
9195 if (cp_lexer_next_token_is_not (parser->lexer,
9196 CPP_COLON)
9197 && cp_lexer_next_token_is_not (parser->lexer,
9198 CPP_SCOPE)
9199 && cp_lexer_next_token_is_not (parser->lexer,
9200 CPP_CLOSE_PAREN))
9201 inputs = cp_parser_asm_operand_list (parser);
9203 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9204 /* The clobbers are coming next. */
9205 clobbers_p = true;
9207 /* Look for clobbers. */
9208 if (clobbers_p
9209 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9211 if (!clobbers_p)
9212 /* Consume the `:'. */
9213 cp_lexer_consume_token (parser->lexer);
9214 /* Parse the clobbers. */
9215 if (cp_lexer_next_token_is_not (parser->lexer,
9216 CPP_CLOSE_PAREN))
9217 clobbers = cp_parser_asm_clobber_list (parser);
9220 /* Look for the closing `)'. */
9221 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9222 cp_parser_skip_to_closing_parenthesis (parser, true, false);
9223 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9225 /* Create the ASM_STMT. */
9226 if (at_function_scope_p ())
9228 asm_stmt =
9229 finish_asm_stmt (volatile_p
9230 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9231 string, outputs, inputs, clobbers);
9232 /* If the extended syntax was not used, mark the ASM_STMT. */
9233 if (!extended_p)
9234 ASM_INPUT_P (asm_stmt) = 1;
9236 else
9237 assemble_asm (string);
9240 /* Declarators [gram.dcl.decl] */
9242 /* Parse an init-declarator.
9244 init-declarator:
9245 declarator initializer [opt]
9247 GNU Extension:
9249 init-declarator:
9250 declarator asm-specification [opt] attributes [opt] initializer [opt]
9252 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9253 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9254 then this declarator appears in a class scope. The new DECL created
9255 by this declarator is returned.
9257 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9258 for a function-definition here as well. If the declarator is a
9259 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9260 be TRUE upon return. By that point, the function-definition will
9261 have been completely parsed.
9263 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9264 is FALSE. */
9266 static tree
9267 cp_parser_init_declarator (cp_parser* parser,
9268 tree decl_specifiers,
9269 tree prefix_attributes,
9270 bool function_definition_allowed_p,
9271 bool member_p,
9272 int declares_class_or_enum,
9273 bool* function_definition_p)
9275 cp_token *token;
9276 tree declarator;
9277 tree attributes;
9278 tree asm_specification;
9279 tree initializer;
9280 tree decl = NULL_TREE;
9281 tree scope;
9282 bool is_initialized;
9283 bool is_parenthesized_init;
9284 bool is_non_constant_init;
9285 int ctor_dtor_or_conv_p;
9286 bool friend_p;
9288 /* Assume that this is not the declarator for a function
9289 definition. */
9290 if (function_definition_p)
9291 *function_definition_p = false;
9293 /* Defer access checks while parsing the declarator; we cannot know
9294 what names are accessible until we know what is being
9295 declared. */
9296 resume_deferring_access_checks ();
9298 /* Parse the declarator. */
9299 declarator
9300 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9301 &ctor_dtor_or_conv_p);
9302 /* Gather up the deferred checks. */
9303 stop_deferring_access_checks ();
9305 /* If the DECLARATOR was erroneous, there's no need to go
9306 further. */
9307 if (declarator == error_mark_node)
9308 return error_mark_node;
9310 cp_parser_check_for_definition_in_return_type (declarator,
9311 declares_class_or_enum);
9313 /* Figure out what scope the entity declared by the DECLARATOR is
9314 located in. `grokdeclarator' sometimes changes the scope, so
9315 we compute it now. */
9316 scope = get_scope_of_declarator (declarator);
9318 /* If we're allowing GNU extensions, look for an asm-specification
9319 and attributes. */
9320 if (cp_parser_allow_gnu_extensions_p (parser))
9322 /* Look for an asm-specification. */
9323 asm_specification = cp_parser_asm_specification_opt (parser);
9324 /* And attributes. */
9325 attributes = cp_parser_attributes_opt (parser);
9327 else
9329 asm_specification = NULL_TREE;
9330 attributes = NULL_TREE;
9333 /* Peek at the next token. */
9334 token = cp_lexer_peek_token (parser->lexer);
9335 /* Check to see if the token indicates the start of a
9336 function-definition. */
9337 if (cp_parser_token_starts_function_definition_p (token))
9339 if (!function_definition_allowed_p)
9341 /* If a function-definition should not appear here, issue an
9342 error message. */
9343 cp_parser_error (parser,
9344 "a function-definition is not allowed here");
9345 return error_mark_node;
9347 else
9349 /* Neither attributes nor an asm-specification are allowed
9350 on a function-definition. */
9351 if (asm_specification)
9352 error ("an asm-specification is not allowed on a function-definition");
9353 if (attributes)
9354 error ("attributes are not allowed on a function-definition");
9355 /* This is a function-definition. */
9356 *function_definition_p = true;
9358 /* Parse the function definition. */
9359 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9360 (parser, decl_specifiers, prefix_attributes, declarator));
9362 return decl;
9366 /* [dcl.dcl]
9368 Only in function declarations for constructors, destructors, and
9369 type conversions can the decl-specifier-seq be omitted.
9371 We explicitly postpone this check past the point where we handle
9372 function-definitions because we tolerate function-definitions
9373 that are missing their return types in some modes. */
9374 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9376 cp_parser_error (parser,
9377 "expected constructor, destructor, or type conversion");
9378 return error_mark_node;
9381 /* An `=' or an `(' indicates an initializer. */
9382 is_initialized = (token->type == CPP_EQ
9383 || token->type == CPP_OPEN_PAREN);
9384 /* If the init-declarator isn't initialized and isn't followed by a
9385 `,' or `;', it's not a valid init-declarator. */
9386 if (!is_initialized
9387 && token->type != CPP_COMMA
9388 && token->type != CPP_SEMICOLON)
9390 cp_parser_error (parser, "expected init-declarator");
9391 return error_mark_node;
9394 /* Because start_decl has side-effects, we should only call it if we
9395 know we're going ahead. By this point, we know that we cannot
9396 possibly be looking at any other construct. */
9397 cp_parser_commit_to_tentative_parse (parser);
9399 /* Check to see whether or not this declaration is a friend. */
9400 friend_p = cp_parser_friend_p (decl_specifiers);
9402 /* Check that the number of template-parameter-lists is OK. */
9403 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9404 return error_mark_node;
9406 /* Enter the newly declared entry in the symbol table. If we're
9407 processing a declaration in a class-specifier, we wait until
9408 after processing the initializer. */
9409 if (!member_p)
9411 if (parser->in_unbraced_linkage_specification_p)
9413 decl_specifiers = tree_cons (error_mark_node,
9414 get_identifier ("extern"),
9415 decl_specifiers);
9416 have_extern_spec = false;
9418 decl = start_decl (declarator, decl_specifiers,
9419 is_initialized, attributes, prefix_attributes);
9422 /* Enter the SCOPE. That way unqualified names appearing in the
9423 initializer will be looked up in SCOPE. */
9424 if (scope)
9425 push_scope (scope);
9427 /* Perform deferred access control checks, now that we know in which
9428 SCOPE the declared entity resides. */
9429 if (!member_p && decl)
9431 tree saved_current_function_decl = NULL_TREE;
9433 /* If the entity being declared is a function, pretend that we
9434 are in its scope. If it is a `friend', it may have access to
9435 things that would not otherwise be accessible. */
9436 if (TREE_CODE (decl) == FUNCTION_DECL)
9438 saved_current_function_decl = current_function_decl;
9439 current_function_decl = decl;
9442 /* Perform the access control checks for the declarator and the
9443 the decl-specifiers. */
9444 perform_deferred_access_checks ();
9446 /* Restore the saved value. */
9447 if (TREE_CODE (decl) == FUNCTION_DECL)
9448 current_function_decl = saved_current_function_decl;
9451 /* Parse the initializer. */
9452 if (is_initialized)
9453 initializer = cp_parser_initializer (parser,
9454 &is_parenthesized_init,
9455 &is_non_constant_init);
9456 else
9458 initializer = NULL_TREE;
9459 is_parenthesized_init = false;
9460 is_non_constant_init = true;
9463 /* The old parser allows attributes to appear after a parenthesized
9464 initializer. Mark Mitchell proposed removing this functionality
9465 on the GCC mailing lists on 2002-08-13. This parser accepts the
9466 attributes -- but ignores them. */
9467 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9468 if (cp_parser_attributes_opt (parser))
9469 warning ("attributes after parenthesized initializer ignored");
9471 /* Leave the SCOPE, now that we have processed the initializer. It
9472 is important to do this before calling cp_finish_decl because it
9473 makes decisions about whether to create DECL_STMTs or not based
9474 on the current scope. */
9475 if (scope)
9476 pop_scope (scope);
9478 /* For an in-class declaration, use `grokfield' to create the
9479 declaration. */
9480 if (member_p)
9482 decl = grokfield (declarator, decl_specifiers,
9483 initializer, /*asmspec=*/NULL_TREE,
9484 /*attributes=*/NULL_TREE);
9485 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9486 cp_parser_save_default_args (parser, decl);
9489 /* Finish processing the declaration. But, skip friend
9490 declarations. */
9491 if (!friend_p && decl)
9492 cp_finish_decl (decl,
9493 initializer,
9494 asm_specification,
9495 /* If the initializer is in parentheses, then this is
9496 a direct-initialization, which means that an
9497 `explicit' constructor is OK. Otherwise, an
9498 `explicit' constructor cannot be used. */
9499 ((is_parenthesized_init || !is_initialized)
9500 ? 0 : LOOKUP_ONLYCONVERTING));
9502 /* Remember whether or not variables were initialized by
9503 constant-expressions. */
9504 if (decl && TREE_CODE (decl) == VAR_DECL
9505 && is_initialized && !is_non_constant_init)
9506 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
9508 return decl;
9511 /* Parse a declarator.
9513 declarator:
9514 direct-declarator
9515 ptr-operator declarator
9517 abstract-declarator:
9518 ptr-operator abstract-declarator [opt]
9519 direct-abstract-declarator
9521 GNU Extensions:
9523 declarator:
9524 attributes [opt] direct-declarator
9525 attributes [opt] ptr-operator declarator
9527 abstract-declarator:
9528 attributes [opt] ptr-operator abstract-declarator [opt]
9529 attributes [opt] direct-abstract-declarator
9531 Returns a representation of the declarator. If the declarator has
9532 the form `* declarator', then an INDIRECT_REF is returned, whose
9533 only operand is the sub-declarator. Analogously, `& declarator' is
9534 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9535 used. The first operand is the TYPE for `X'. The second operand
9536 is an INDIRECT_REF whose operand is the sub-declarator.
9538 Otherwise, the representation is as for a direct-declarator.
9540 (It would be better to define a structure type to represent
9541 declarators, rather than abusing `tree' nodes to represent
9542 declarators. That would be much clearer and save some memory.
9543 There is no reason for declarators to be garbage-collected, for
9544 example; they are created during parser and no longer needed after
9545 `grokdeclarator' has been called.)
9547 For a ptr-operator that has the optional cv-qualifier-seq,
9548 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9549 node.
9551 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
9552 detect constructor, destructor or conversion operators. It is set
9553 to -1 if the declarator is a name, and +1 if it is a
9554 function. Otherwise it is set to zero. Usually you just want to
9555 test for >0, but internally the negative value is used.
9557 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9558 a decl-specifier-seq unless it declares a constructor, destructor,
9559 or conversion. It might seem that we could check this condition in
9560 semantic analysis, rather than parsing, but that makes it difficult
9561 to handle something like `f()'. We want to notice that there are
9562 no decl-specifiers, and therefore realize that this is an
9563 expression, not a declaration.) */
9565 static tree
9566 cp_parser_declarator (cp_parser* parser,
9567 cp_parser_declarator_kind dcl_kind,
9568 int* ctor_dtor_or_conv_p)
9570 cp_token *token;
9571 tree declarator;
9572 enum tree_code code;
9573 tree cv_qualifier_seq;
9574 tree class_type;
9575 tree attributes = NULL_TREE;
9577 /* Assume this is not a constructor, destructor, or type-conversion
9578 operator. */
9579 if (ctor_dtor_or_conv_p)
9580 *ctor_dtor_or_conv_p = 0;
9582 if (cp_parser_allow_gnu_extensions_p (parser))
9583 attributes = cp_parser_attributes_opt (parser);
9585 /* Peek at the next token. */
9586 token = cp_lexer_peek_token (parser->lexer);
9588 /* Check for the ptr-operator production. */
9589 cp_parser_parse_tentatively (parser);
9590 /* Parse the ptr-operator. */
9591 code = cp_parser_ptr_operator (parser,
9592 &class_type,
9593 &cv_qualifier_seq);
9594 /* If that worked, then we have a ptr-operator. */
9595 if (cp_parser_parse_definitely (parser))
9597 /* The dependent declarator is optional if we are parsing an
9598 abstract-declarator. */
9599 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9600 cp_parser_parse_tentatively (parser);
9602 /* Parse the dependent declarator. */
9603 declarator = cp_parser_declarator (parser, dcl_kind,
9604 /*ctor_dtor_or_conv_p=*/NULL);
9606 /* If we are parsing an abstract-declarator, we must handle the
9607 case where the dependent declarator is absent. */
9608 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9609 && !cp_parser_parse_definitely (parser))
9610 declarator = NULL_TREE;
9612 /* Build the representation of the ptr-operator. */
9613 if (code == INDIRECT_REF)
9614 declarator = make_pointer_declarator (cv_qualifier_seq,
9615 declarator);
9616 else
9617 declarator = make_reference_declarator (cv_qualifier_seq,
9618 declarator);
9619 /* Handle the pointer-to-member case. */
9620 if (class_type)
9621 declarator = build_nt (SCOPE_REF, class_type, declarator);
9623 /* Everything else is a direct-declarator. */
9624 else
9625 declarator = cp_parser_direct_declarator (parser, dcl_kind,
9626 ctor_dtor_or_conv_p);
9628 if (attributes && declarator != error_mark_node)
9629 declarator = tree_cons (attributes, declarator, NULL_TREE);
9631 return declarator;
9634 /* Parse a direct-declarator or direct-abstract-declarator.
9636 direct-declarator:
9637 declarator-id
9638 direct-declarator ( parameter-declaration-clause )
9639 cv-qualifier-seq [opt]
9640 exception-specification [opt]
9641 direct-declarator [ constant-expression [opt] ]
9642 ( declarator )
9644 direct-abstract-declarator:
9645 direct-abstract-declarator [opt]
9646 ( parameter-declaration-clause )
9647 cv-qualifier-seq [opt]
9648 exception-specification [opt]
9649 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9650 ( abstract-declarator )
9652 Returns a representation of the declarator. DCL_KIND is
9653 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9654 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9655 we are parsing a direct-declarator. It is
9656 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9657 of ambiguity we prefer an abstract declarator, as per
9658 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9659 cp_parser_declarator.
9661 For the declarator-id production, the representation is as for an
9662 id-expression, except that a qualified name is represented as a
9663 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9664 see the documentation of the FUNCTION_DECLARATOR_* macros for
9665 information about how to find the various declarator components.
9666 An array-declarator is represented as an ARRAY_REF. The
9667 direct-declarator is the first operand; the constant-expression
9668 indicating the size of the array is the second operand. */
9670 static tree
9671 cp_parser_direct_declarator (cp_parser* parser,
9672 cp_parser_declarator_kind dcl_kind,
9673 int* ctor_dtor_or_conv_p)
9675 cp_token *token;
9676 tree declarator = NULL_TREE;
9677 tree scope = NULL_TREE;
9678 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9679 bool saved_in_declarator_p = parser->in_declarator_p;
9680 bool first = true;
9682 while (true)
9684 /* Peek at the next token. */
9685 token = cp_lexer_peek_token (parser->lexer);
9686 if (token->type == CPP_OPEN_PAREN)
9688 /* This is either a parameter-declaration-clause, or a
9689 parenthesized declarator. When we know we are parsing a
9690 named declarator, it must be a parenthesized declarator
9691 if FIRST is true. For instance, `(int)' is a
9692 parameter-declaration-clause, with an omitted
9693 direct-abstract-declarator. But `((*))', is a
9694 parenthesized abstract declarator. Finally, when T is a
9695 template parameter `(T)' is a
9696 parameter-declaration-clause, and not a parenthesized
9697 named declarator.
9699 We first try and parse a parameter-declaration-clause,
9700 and then try a nested declarator (if FIRST is true).
9702 It is not an error for it not to be a
9703 parameter-declaration-clause, even when FIRST is
9704 false. Consider,
9706 int i (int);
9707 int i (3);
9709 The first is the declaration of a function while the
9710 second is a the definition of a variable, including its
9711 initializer.
9713 Having seen only the parenthesis, we cannot know which of
9714 these two alternatives should be selected. Even more
9715 complex are examples like:
9717 int i (int (a));
9718 int i (int (3));
9720 The former is a function-declaration; the latter is a
9721 variable initialization.
9723 Thus again, we try a parameter-declaration-clause, and if
9724 that fails, we back out and return. */
9726 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9728 tree params;
9729 unsigned saved_num_template_parameter_lists;
9731 cp_parser_parse_tentatively (parser);
9733 /* Consume the `('. */
9734 cp_lexer_consume_token (parser->lexer);
9735 if (first)
9737 /* If this is going to be an abstract declarator, we're
9738 in a declarator and we can't have default args. */
9739 parser->default_arg_ok_p = false;
9740 parser->in_declarator_p = true;
9743 /* Inside the function parameter list, surrounding
9744 template-parameter-lists do not apply. */
9745 saved_num_template_parameter_lists
9746 = parser->num_template_parameter_lists;
9747 parser->num_template_parameter_lists = 0;
9749 /* Parse the parameter-declaration-clause. */
9750 params = cp_parser_parameter_declaration_clause (parser);
9752 parser->num_template_parameter_lists
9753 = saved_num_template_parameter_lists;
9755 /* If all went well, parse the cv-qualifier-seq and the
9756 exception-specification. */
9757 if (cp_parser_parse_definitely (parser))
9759 tree cv_qualifiers;
9760 tree exception_specification;
9762 if (ctor_dtor_or_conv_p)
9763 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
9764 first = false;
9765 /* Consume the `)'. */
9766 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9768 /* Parse the cv-qualifier-seq. */
9769 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9770 /* And the exception-specification. */
9771 exception_specification
9772 = cp_parser_exception_specification_opt (parser);
9774 /* Create the function-declarator. */
9775 declarator = make_call_declarator (declarator,
9776 params,
9777 cv_qualifiers,
9778 exception_specification);
9779 /* Any subsequent parameter lists are to do with
9780 return type, so are not those of the declared
9781 function. */
9782 parser->default_arg_ok_p = false;
9784 /* Repeat the main loop. */
9785 continue;
9789 /* If this is the first, we can try a parenthesized
9790 declarator. */
9791 if (first)
9793 parser->default_arg_ok_p = saved_default_arg_ok_p;
9794 parser->in_declarator_p = saved_in_declarator_p;
9796 /* Consume the `('. */
9797 cp_lexer_consume_token (parser->lexer);
9798 /* Parse the nested declarator. */
9799 declarator
9800 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
9801 first = false;
9802 /* Expect a `)'. */
9803 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9804 declarator = error_mark_node;
9805 if (declarator == error_mark_node)
9806 break;
9808 goto handle_declarator;
9810 /* Otherwise, we must be done. */
9811 else
9812 break;
9814 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9815 && token->type == CPP_OPEN_SQUARE)
9817 /* Parse an array-declarator. */
9818 tree bounds;
9820 if (ctor_dtor_or_conv_p)
9821 *ctor_dtor_or_conv_p = 0;
9823 first = false;
9824 parser->default_arg_ok_p = false;
9825 parser->in_declarator_p = true;
9826 /* Consume the `['. */
9827 cp_lexer_consume_token (parser->lexer);
9828 /* Peek at the next token. */
9829 token = cp_lexer_peek_token (parser->lexer);
9830 /* If the next token is `]', then there is no
9831 constant-expression. */
9832 if (token->type != CPP_CLOSE_SQUARE)
9834 bool non_constant_p;
9836 bounds
9837 = cp_parser_constant_expression (parser,
9838 /*allow_non_constant=*/true,
9839 &non_constant_p);
9840 if (!non_constant_p)
9841 bounds = cp_parser_fold_non_dependent_expr (bounds);
9843 else
9844 bounds = NULL_TREE;
9845 /* Look for the closing `]'. */
9846 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
9848 declarator = error_mark_node;
9849 break;
9852 declarator = build_nt (ARRAY_REF, declarator, bounds);
9854 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
9856 /* Parse a declarator_id */
9857 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9858 cp_parser_parse_tentatively (parser);
9859 declarator = cp_parser_declarator_id (parser);
9860 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9862 if (!cp_parser_parse_definitely (parser))
9863 declarator = error_mark_node;
9864 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
9866 cp_parser_error (parser, "expected unqualified-id");
9867 declarator = error_mark_node;
9871 if (declarator == error_mark_node)
9872 break;
9874 if (TREE_CODE (declarator) == SCOPE_REF)
9876 tree scope = TREE_OPERAND (declarator, 0);
9878 /* In the declaration of a member of a template class
9879 outside of the class itself, the SCOPE will sometimes
9880 be a TYPENAME_TYPE. For example, given:
9882 template <typename T>
9883 int S<T>::R::i = 3;
9885 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
9886 this context, we must resolve S<T>::R to an ordinary
9887 type, rather than a typename type.
9889 The reason we normally avoid resolving TYPENAME_TYPEs
9890 is that a specialization of `S' might render
9891 `S<T>::R' not a type. However, if `S' is
9892 specialized, then this `i' will not be used, so there
9893 is no harm in resolving the types here. */
9894 if (TREE_CODE (scope) == TYPENAME_TYPE)
9896 tree type;
9898 /* Resolve the TYPENAME_TYPE. */
9899 type = resolve_typename_type (scope,
9900 /*only_current_p=*/false);
9901 /* If that failed, the declarator is invalid. */
9902 if (type != error_mark_node)
9903 scope = type;
9904 /* Build a new DECLARATOR. */
9905 declarator = build_nt (SCOPE_REF,
9906 scope,
9907 TREE_OPERAND (declarator, 1));
9911 /* Check to see whether the declarator-id names a constructor,
9912 destructor, or conversion. */
9913 if (declarator && ctor_dtor_or_conv_p
9914 && ((TREE_CODE (declarator) == SCOPE_REF
9915 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
9916 || (TREE_CODE (declarator) != SCOPE_REF
9917 && at_class_scope_p ())))
9919 tree unqualified_name;
9920 tree class_type;
9922 /* Get the unqualified part of the name. */
9923 if (TREE_CODE (declarator) == SCOPE_REF)
9925 class_type = TREE_OPERAND (declarator, 0);
9926 unqualified_name = TREE_OPERAND (declarator, 1);
9928 else
9930 class_type = current_class_type;
9931 unqualified_name = declarator;
9934 /* See if it names ctor, dtor or conv. */
9935 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
9936 || IDENTIFIER_TYPENAME_P (unqualified_name)
9937 || constructor_name_p (unqualified_name, class_type))
9938 *ctor_dtor_or_conv_p = -1;
9941 handle_declarator:;
9942 scope = get_scope_of_declarator (declarator);
9943 if (scope)
9944 /* Any names that appear after the declarator-id for a member
9945 are looked up in the containing scope. */
9946 push_scope (scope);
9947 parser->in_declarator_p = true;
9948 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
9949 || (declarator
9950 && (TREE_CODE (declarator) == SCOPE_REF
9951 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
9952 /* Default args are only allowed on function
9953 declarations. */
9954 parser->default_arg_ok_p = saved_default_arg_ok_p;
9955 else
9956 parser->default_arg_ok_p = false;
9958 first = false;
9960 /* We're done. */
9961 else
9962 break;
9965 /* For an abstract declarator, we might wind up with nothing at this
9966 point. That's an error; the declarator is not optional. */
9967 if (!declarator)
9968 cp_parser_error (parser, "expected declarator");
9970 /* If we entered a scope, we must exit it now. */
9971 if (scope)
9972 pop_scope (scope);
9974 parser->default_arg_ok_p = saved_default_arg_ok_p;
9975 parser->in_declarator_p = saved_in_declarator_p;
9977 return declarator;
9980 /* Parse a ptr-operator.
9982 ptr-operator:
9983 * cv-qualifier-seq [opt]
9985 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
9987 GNU Extension:
9989 ptr-operator:
9990 & cv-qualifier-seq [opt]
9992 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
9993 used. Returns ADDR_EXPR if a reference was used. In the
9994 case of a pointer-to-member, *TYPE is filled in with the
9995 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
9996 with the cv-qualifier-seq, or NULL_TREE, if there are no
9997 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
9999 static enum tree_code
10000 cp_parser_ptr_operator (cp_parser* parser,
10001 tree* type,
10002 tree* cv_qualifier_seq)
10004 enum tree_code code = ERROR_MARK;
10005 cp_token *token;
10007 /* Assume that it's not a pointer-to-member. */
10008 *type = NULL_TREE;
10009 /* And that there are no cv-qualifiers. */
10010 *cv_qualifier_seq = NULL_TREE;
10012 /* Peek at the next token. */
10013 token = cp_lexer_peek_token (parser->lexer);
10014 /* If it's a `*' or `&' we have a pointer or reference. */
10015 if (token->type == CPP_MULT || token->type == CPP_AND)
10017 /* Remember which ptr-operator we were processing. */
10018 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10020 /* Consume the `*' or `&'. */
10021 cp_lexer_consume_token (parser->lexer);
10023 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10024 `&', if we are allowing GNU extensions. (The only qualifier
10025 that can legally appear after `&' is `restrict', but that is
10026 enforced during semantic analysis. */
10027 if (code == INDIRECT_REF
10028 || cp_parser_allow_gnu_extensions_p (parser))
10029 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10031 else
10033 /* Try the pointer-to-member case. */
10034 cp_parser_parse_tentatively (parser);
10035 /* Look for the optional `::' operator. */
10036 cp_parser_global_scope_opt (parser,
10037 /*current_scope_valid_p=*/false);
10038 /* Look for the nested-name specifier. */
10039 cp_parser_nested_name_specifier (parser,
10040 /*typename_keyword_p=*/false,
10041 /*check_dependency_p=*/true,
10042 /*type_p=*/false);
10043 /* If we found it, and the next token is a `*', then we are
10044 indeed looking at a pointer-to-member operator. */
10045 if (!cp_parser_error_occurred (parser)
10046 && cp_parser_require (parser, CPP_MULT, "`*'"))
10048 /* The type of which the member is a member is given by the
10049 current SCOPE. */
10050 *type = parser->scope;
10051 /* The next name will not be qualified. */
10052 parser->scope = NULL_TREE;
10053 parser->qualifying_scope = NULL_TREE;
10054 parser->object_scope = NULL_TREE;
10055 /* Indicate that the `*' operator was used. */
10056 code = INDIRECT_REF;
10057 /* Look for the optional cv-qualifier-seq. */
10058 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10060 /* If that didn't work we don't have a ptr-operator. */
10061 if (!cp_parser_parse_definitely (parser))
10062 cp_parser_error (parser, "expected ptr-operator");
10065 return code;
10068 /* Parse an (optional) cv-qualifier-seq.
10070 cv-qualifier-seq:
10071 cv-qualifier cv-qualifier-seq [opt]
10073 Returns a TREE_LIST. The TREE_VALUE of each node is the
10074 representation of a cv-qualifier. */
10076 static tree
10077 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10079 tree cv_qualifiers = NULL_TREE;
10081 while (true)
10083 tree cv_qualifier;
10085 /* Look for the next cv-qualifier. */
10086 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10087 /* If we didn't find one, we're done. */
10088 if (!cv_qualifier)
10089 break;
10091 /* Add this cv-qualifier to the list. */
10092 cv_qualifiers
10093 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10096 /* We built up the list in reverse order. */
10097 return nreverse (cv_qualifiers);
10100 /* Parse an (optional) cv-qualifier.
10102 cv-qualifier:
10103 const
10104 volatile
10106 GNU Extension:
10108 cv-qualifier:
10109 __restrict__ */
10111 static tree
10112 cp_parser_cv_qualifier_opt (cp_parser* parser)
10114 cp_token *token;
10115 tree cv_qualifier = NULL_TREE;
10117 /* Peek at the next token. */
10118 token = cp_lexer_peek_token (parser->lexer);
10119 /* See if it's a cv-qualifier. */
10120 switch (token->keyword)
10122 case RID_CONST:
10123 case RID_VOLATILE:
10124 case RID_RESTRICT:
10125 /* Save the value of the token. */
10126 cv_qualifier = token->value;
10127 /* Consume the token. */
10128 cp_lexer_consume_token (parser->lexer);
10129 break;
10131 default:
10132 break;
10135 return cv_qualifier;
10138 /* Parse a declarator-id.
10140 declarator-id:
10141 id-expression
10142 :: [opt] nested-name-specifier [opt] type-name
10144 In the `id-expression' case, the value returned is as for
10145 cp_parser_id_expression if the id-expression was an unqualified-id.
10146 If the id-expression was a qualified-id, then a SCOPE_REF is
10147 returned. The first operand is the scope (either a NAMESPACE_DECL
10148 or TREE_TYPE), but the second is still just a representation of an
10149 unqualified-id. */
10151 static tree
10152 cp_parser_declarator_id (cp_parser* parser)
10154 tree id_expression;
10156 /* The expression must be an id-expression. Assume that qualified
10157 names are the names of types so that:
10159 template <class T>
10160 int S<T>::R::i = 3;
10162 will work; we must treat `S<T>::R' as the name of a type.
10163 Similarly, assume that qualified names are templates, where
10164 required, so that:
10166 template <class T>
10167 int S<T>::R<T>::i = 3;
10169 will work, too. */
10170 id_expression = cp_parser_id_expression (parser,
10171 /*template_keyword_p=*/false,
10172 /*check_dependency_p=*/false,
10173 /*template_p=*/NULL,
10174 /*declarator_p=*/true);
10175 /* If the name was qualified, create a SCOPE_REF to represent
10176 that. */
10177 if (parser->scope)
10179 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10180 parser->scope = NULL_TREE;
10183 return id_expression;
10186 /* Parse a type-id.
10188 type-id:
10189 type-specifier-seq abstract-declarator [opt]
10191 Returns the TYPE specified. */
10193 static tree
10194 cp_parser_type_id (cp_parser* parser)
10196 tree type_specifier_seq;
10197 tree abstract_declarator;
10199 /* Parse the type-specifier-seq. */
10200 type_specifier_seq
10201 = cp_parser_type_specifier_seq (parser);
10202 if (type_specifier_seq == error_mark_node)
10203 return error_mark_node;
10205 /* There might or might not be an abstract declarator. */
10206 cp_parser_parse_tentatively (parser);
10207 /* Look for the declarator. */
10208 abstract_declarator
10209 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10210 /* Check to see if there really was a declarator. */
10211 if (!cp_parser_parse_definitely (parser))
10212 abstract_declarator = NULL_TREE;
10214 return groktypename (build_tree_list (type_specifier_seq,
10215 abstract_declarator));
10218 /* Parse a type-specifier-seq.
10220 type-specifier-seq:
10221 type-specifier type-specifier-seq [opt]
10223 GNU extension:
10225 type-specifier-seq:
10226 attributes type-specifier-seq [opt]
10228 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10229 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10231 static tree
10232 cp_parser_type_specifier_seq (cp_parser* parser)
10234 bool seen_type_specifier = false;
10235 tree type_specifier_seq = NULL_TREE;
10237 /* Parse the type-specifiers and attributes. */
10238 while (true)
10240 tree type_specifier;
10242 /* Check for attributes first. */
10243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10245 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10246 NULL_TREE,
10247 type_specifier_seq);
10248 continue;
10251 /* After the first type-specifier, others are optional. */
10252 if (seen_type_specifier)
10253 cp_parser_parse_tentatively (parser);
10254 /* Look for the type-specifier. */
10255 type_specifier = cp_parser_type_specifier (parser,
10256 CP_PARSER_FLAGS_NONE,
10257 /*is_friend=*/false,
10258 /*is_declaration=*/false,
10259 NULL,
10260 NULL);
10261 /* If the first type-specifier could not be found, this is not a
10262 type-specifier-seq at all. */
10263 if (!seen_type_specifier && type_specifier == error_mark_node)
10264 return error_mark_node;
10265 /* If subsequent type-specifiers could not be found, the
10266 type-specifier-seq is complete. */
10267 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10268 break;
10270 /* Add the new type-specifier to the list. */
10271 type_specifier_seq
10272 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10273 seen_type_specifier = true;
10276 /* We built up the list in reverse order. */
10277 return nreverse (type_specifier_seq);
10280 /* Parse a parameter-declaration-clause.
10282 parameter-declaration-clause:
10283 parameter-declaration-list [opt] ... [opt]
10284 parameter-declaration-list , ...
10286 Returns a representation for the parameter declarations. Each node
10287 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10288 representation.) If the parameter-declaration-clause ends with an
10289 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10290 list. A return value of NULL_TREE indicates a
10291 parameter-declaration-clause consisting only of an ellipsis. */
10293 static tree
10294 cp_parser_parameter_declaration_clause (cp_parser* parser)
10296 tree parameters;
10297 cp_token *token;
10298 bool ellipsis_p;
10300 /* Peek at the next token. */
10301 token = cp_lexer_peek_token (parser->lexer);
10302 /* Check for trivial parameter-declaration-clauses. */
10303 if (token->type == CPP_ELLIPSIS)
10305 /* Consume the `...' token. */
10306 cp_lexer_consume_token (parser->lexer);
10307 return NULL_TREE;
10309 else if (token->type == CPP_CLOSE_PAREN)
10310 /* There are no parameters. */
10312 #ifndef NO_IMPLICIT_EXTERN_C
10313 if (in_system_header && current_class_type == NULL
10314 && current_lang_name == lang_name_c)
10315 return NULL_TREE;
10316 else
10317 #endif
10318 return void_list_node;
10320 /* Check for `(void)', too, which is a special case. */
10321 else if (token->keyword == RID_VOID
10322 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10323 == CPP_CLOSE_PAREN))
10325 /* Consume the `void' token. */
10326 cp_lexer_consume_token (parser->lexer);
10327 /* There are no parameters. */
10328 return void_list_node;
10331 /* Parse the parameter-declaration-list. */
10332 parameters = cp_parser_parameter_declaration_list (parser);
10333 /* If a parse error occurred while parsing the
10334 parameter-declaration-list, then the entire
10335 parameter-declaration-clause is erroneous. */
10336 if (parameters == error_mark_node)
10337 return error_mark_node;
10339 /* Peek at the next token. */
10340 token = cp_lexer_peek_token (parser->lexer);
10341 /* If it's a `,', the clause should terminate with an ellipsis. */
10342 if (token->type == CPP_COMMA)
10344 /* Consume the `,'. */
10345 cp_lexer_consume_token (parser->lexer);
10346 /* Expect an ellipsis. */
10347 ellipsis_p
10348 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10350 /* It might also be `...' if the optional trailing `,' was
10351 omitted. */
10352 else if (token->type == CPP_ELLIPSIS)
10354 /* Consume the `...' token. */
10355 cp_lexer_consume_token (parser->lexer);
10356 /* And remember that we saw it. */
10357 ellipsis_p = true;
10359 else
10360 ellipsis_p = false;
10362 /* Finish the parameter list. */
10363 return finish_parmlist (parameters, ellipsis_p);
10366 /* Parse a parameter-declaration-list.
10368 parameter-declaration-list:
10369 parameter-declaration
10370 parameter-declaration-list , parameter-declaration
10372 Returns a representation of the parameter-declaration-list, as for
10373 cp_parser_parameter_declaration_clause. However, the
10374 `void_list_node' is never appended to the list. */
10376 static tree
10377 cp_parser_parameter_declaration_list (cp_parser* parser)
10379 tree parameters = NULL_TREE;
10381 /* Look for more parameters. */
10382 while (true)
10384 tree parameter;
10385 /* Parse the parameter. */
10386 parameter
10387 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10389 /* If a parse error occurred parsing the parameter declaration,
10390 then the entire parameter-declaration-list is erroneous. */
10391 if (parameter == error_mark_node)
10393 parameters = error_mark_node;
10394 break;
10396 /* Add the new parameter to the list. */
10397 TREE_CHAIN (parameter) = parameters;
10398 parameters = parameter;
10400 /* Peek at the next token. */
10401 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10402 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10403 /* The parameter-declaration-list is complete. */
10404 break;
10405 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10407 cp_token *token;
10409 /* Peek at the next token. */
10410 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10411 /* If it's an ellipsis, then the list is complete. */
10412 if (token->type == CPP_ELLIPSIS)
10413 break;
10414 /* Otherwise, there must be more parameters. Consume the
10415 `,'. */
10416 cp_lexer_consume_token (parser->lexer);
10418 else
10420 cp_parser_error (parser, "expected `,' or `...'");
10421 break;
10425 /* We built up the list in reverse order; straighten it out now. */
10426 return nreverse (parameters);
10429 /* Parse a parameter declaration.
10431 parameter-declaration:
10432 decl-specifier-seq declarator
10433 decl-specifier-seq declarator = assignment-expression
10434 decl-specifier-seq abstract-declarator [opt]
10435 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10437 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10438 declares a template parameter. (In that case, a non-nested `>'
10439 token encountered during the parsing of the assignment-expression
10440 is not interpreted as a greater-than operator.)
10442 Returns a TREE_LIST representing the parameter-declaration. The
10443 TREE_VALUE is a representation of the decl-specifier-seq and
10444 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10445 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10446 TREE_VALUE represents the declarator. */
10448 static tree
10449 cp_parser_parameter_declaration (cp_parser *parser,
10450 bool template_parm_p)
10452 int declares_class_or_enum;
10453 bool greater_than_is_operator_p;
10454 tree decl_specifiers;
10455 tree attributes;
10456 tree declarator;
10457 tree default_argument;
10458 tree parameter;
10459 cp_token *token;
10460 const char *saved_message;
10462 /* In a template parameter, `>' is not an operator.
10464 [temp.param]
10466 When parsing a default template-argument for a non-type
10467 template-parameter, the first non-nested `>' is taken as the end
10468 of the template parameter-list rather than a greater-than
10469 operator. */
10470 greater_than_is_operator_p = !template_parm_p;
10472 /* Type definitions may not appear in parameter types. */
10473 saved_message = parser->type_definition_forbidden_message;
10474 parser->type_definition_forbidden_message
10475 = "types may not be defined in parameter types";
10477 /* Parse the declaration-specifiers. */
10478 decl_specifiers
10479 = cp_parser_decl_specifier_seq (parser,
10480 CP_PARSER_FLAGS_NONE,
10481 &attributes,
10482 &declares_class_or_enum);
10483 /* If an error occurred, there's no reason to attempt to parse the
10484 rest of the declaration. */
10485 if (cp_parser_error_occurred (parser))
10487 parser->type_definition_forbidden_message = saved_message;
10488 return error_mark_node;
10491 /* Peek at the next token. */
10492 token = cp_lexer_peek_token (parser->lexer);
10493 /* If the next token is a `)', `,', `=', `>', or `...', then there
10494 is no declarator. */
10495 if (token->type == CPP_CLOSE_PAREN
10496 || token->type == CPP_COMMA
10497 || token->type == CPP_EQ
10498 || token->type == CPP_ELLIPSIS
10499 || token->type == CPP_GREATER)
10500 declarator = NULL_TREE;
10501 /* Otherwise, there should be a declarator. */
10502 else
10504 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10505 parser->default_arg_ok_p = false;
10507 declarator = cp_parser_declarator (parser,
10508 CP_PARSER_DECLARATOR_EITHER,
10509 /*ctor_dtor_or_conv_p=*/NULL);
10510 parser->default_arg_ok_p = saved_default_arg_ok_p;
10511 /* After the declarator, allow more attributes. */
10512 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10515 /* The restriction on defining new types applies only to the type
10516 of the parameter, not to the default argument. */
10517 parser->type_definition_forbidden_message = saved_message;
10519 /* If the next token is `=', then process a default argument. */
10520 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10522 bool saved_greater_than_is_operator_p;
10523 /* Consume the `='. */
10524 cp_lexer_consume_token (parser->lexer);
10526 /* If we are defining a class, then the tokens that make up the
10527 default argument must be saved and processed later. */
10528 if (!template_parm_p && at_class_scope_p ()
10529 && TYPE_BEING_DEFINED (current_class_type))
10531 unsigned depth = 0;
10533 /* Create a DEFAULT_ARG to represented the unparsed default
10534 argument. */
10535 default_argument = make_node (DEFAULT_ARG);
10536 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10538 /* Add tokens until we have processed the entire default
10539 argument. */
10540 while (true)
10542 bool done = false;
10543 cp_token *token;
10545 /* Peek at the next token. */
10546 token = cp_lexer_peek_token (parser->lexer);
10547 /* What we do depends on what token we have. */
10548 switch (token->type)
10550 /* In valid code, a default argument must be
10551 immediately followed by a `,' `)', or `...'. */
10552 case CPP_COMMA:
10553 case CPP_CLOSE_PAREN:
10554 case CPP_ELLIPSIS:
10555 /* If we run into a non-nested `;', `}', or `]',
10556 then the code is invalid -- but the default
10557 argument is certainly over. */
10558 case CPP_SEMICOLON:
10559 case CPP_CLOSE_BRACE:
10560 case CPP_CLOSE_SQUARE:
10561 if (depth == 0)
10562 done = true;
10563 /* Update DEPTH, if necessary. */
10564 else if (token->type == CPP_CLOSE_PAREN
10565 || token->type == CPP_CLOSE_BRACE
10566 || token->type == CPP_CLOSE_SQUARE)
10567 --depth;
10568 break;
10570 case CPP_OPEN_PAREN:
10571 case CPP_OPEN_SQUARE:
10572 case CPP_OPEN_BRACE:
10573 ++depth;
10574 break;
10576 case CPP_GREATER:
10577 /* If we see a non-nested `>', and `>' is not an
10578 operator, then it marks the end of the default
10579 argument. */
10580 if (!depth && !greater_than_is_operator_p)
10581 done = true;
10582 break;
10584 /* If we run out of tokens, issue an error message. */
10585 case CPP_EOF:
10586 error ("file ends in default argument");
10587 done = true;
10588 break;
10590 case CPP_NAME:
10591 case CPP_SCOPE:
10592 /* In these cases, we should look for template-ids.
10593 For example, if the default argument is
10594 `X<int, double>()', we need to do name lookup to
10595 figure out whether or not `X' is a template; if
10596 so, the `,' does not end the default argument.
10598 That is not yet done. */
10599 break;
10601 default:
10602 break;
10605 /* If we've reached the end, stop. */
10606 if (done)
10607 break;
10609 /* Add the token to the token block. */
10610 token = cp_lexer_consume_token (parser->lexer);
10611 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10612 token);
10615 /* Outside of a class definition, we can just parse the
10616 assignment-expression. */
10617 else
10619 bool saved_local_variables_forbidden_p;
10621 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10622 set correctly. */
10623 saved_greater_than_is_operator_p
10624 = parser->greater_than_is_operator_p;
10625 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10626 /* Local variable names (and the `this' keyword) may not
10627 appear in a default argument. */
10628 saved_local_variables_forbidden_p
10629 = parser->local_variables_forbidden_p;
10630 parser->local_variables_forbidden_p = true;
10631 /* Parse the assignment-expression. */
10632 default_argument = cp_parser_assignment_expression (parser);
10633 /* Restore saved state. */
10634 parser->greater_than_is_operator_p
10635 = saved_greater_than_is_operator_p;
10636 parser->local_variables_forbidden_p
10637 = saved_local_variables_forbidden_p;
10639 if (!parser->default_arg_ok_p)
10641 if (!flag_pedantic_errors)
10642 warning ("deprecated use of default argument for parameter of non-function");
10643 else
10645 error ("default arguments are only permitted for function parameters");
10646 default_argument = NULL_TREE;
10650 else
10651 default_argument = NULL_TREE;
10653 /* Create the representation of the parameter. */
10654 if (attributes)
10655 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10656 parameter = build_tree_list (default_argument,
10657 build_tree_list (decl_specifiers,
10658 declarator));
10660 return parameter;
10663 /* Parse a function-definition.
10665 function-definition:
10666 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10667 function-body
10668 decl-specifier-seq [opt] declarator function-try-block
10670 GNU Extension:
10672 function-definition:
10673 __extension__ function-definition
10675 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10676 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10677 be a `friend'. */
10679 static tree
10680 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10682 tree decl_specifiers;
10683 tree attributes;
10684 tree declarator;
10685 tree fn;
10686 cp_token *token;
10687 int declares_class_or_enum;
10688 bool member_p;
10689 /* The saved value of the PEDANTIC flag. */
10690 int saved_pedantic;
10692 /* Any pending qualification must be cleared by our caller. It is
10693 more robust to force the callers to clear PARSER->SCOPE than to
10694 do it here since if the qualification is in effect here, it might
10695 also end up in effect elsewhere that it is not intended. */
10696 my_friendly_assert (!parser->scope, 20010821);
10698 /* Handle `__extension__'. */
10699 if (cp_parser_extension_opt (parser, &saved_pedantic))
10701 /* Parse the function-definition. */
10702 fn = cp_parser_function_definition (parser, friend_p);
10703 /* Restore the PEDANTIC flag. */
10704 pedantic = saved_pedantic;
10706 return fn;
10709 /* Check to see if this definition appears in a class-specifier. */
10710 member_p = (at_class_scope_p ()
10711 && TYPE_BEING_DEFINED (current_class_type));
10712 /* Defer access checks in the decl-specifier-seq until we know what
10713 function is being defined. There is no need to do this for the
10714 definition of member functions; we cannot be defining a member
10715 from another class. */
10716 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10718 /* Parse the decl-specifier-seq. */
10719 decl_specifiers
10720 = cp_parser_decl_specifier_seq (parser,
10721 CP_PARSER_FLAGS_OPTIONAL,
10722 &attributes,
10723 &declares_class_or_enum);
10724 /* Figure out whether this declaration is a `friend'. */
10725 if (friend_p)
10726 *friend_p = cp_parser_friend_p (decl_specifiers);
10728 /* Parse the declarator. */
10729 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10730 /*ctor_dtor_or_conv_p=*/NULL);
10732 /* Gather up any access checks that occurred. */
10733 stop_deferring_access_checks ();
10735 /* If something has already gone wrong, we may as well stop now. */
10736 if (declarator == error_mark_node)
10738 /* Skip to the end of the function, or if this wasn't anything
10739 like a function-definition, to a `;' in the hopes of finding
10740 a sensible place from which to continue parsing. */
10741 cp_parser_skip_to_end_of_block_or_statement (parser);
10742 pop_deferring_access_checks ();
10743 return error_mark_node;
10746 /* The next character should be a `{' (for a simple function
10747 definition), a `:' (for a ctor-initializer), or `try' (for a
10748 function-try block). */
10749 token = cp_lexer_peek_token (parser->lexer);
10750 if (!cp_parser_token_starts_function_definition_p (token))
10752 /* Issue the error-message. */
10753 cp_parser_error (parser, "expected function-definition");
10754 /* Skip to the next `;'. */
10755 cp_parser_skip_to_end_of_block_or_statement (parser);
10757 pop_deferring_access_checks ();
10758 return error_mark_node;
10761 cp_parser_check_for_definition_in_return_type (declarator,
10762 declares_class_or_enum);
10764 /* If we are in a class scope, then we must handle
10765 function-definitions specially. In particular, we save away the
10766 tokens that make up the function body, and parse them again
10767 later, in order to handle code like:
10769 struct S {
10770 int f () { return i; }
10771 int i;
10774 Here, we cannot parse the body of `f' until after we have seen
10775 the declaration of `i'. */
10776 if (member_p)
10778 cp_token_cache *cache;
10780 /* Create the function-declaration. */
10781 fn = start_method (decl_specifiers, declarator, attributes);
10782 /* If something went badly wrong, bail out now. */
10783 if (fn == error_mark_node)
10785 /* If there's a function-body, skip it. */
10786 if (cp_parser_token_starts_function_definition_p
10787 (cp_lexer_peek_token (parser->lexer)))
10788 cp_parser_skip_to_end_of_block_or_statement (parser);
10789 pop_deferring_access_checks ();
10790 return error_mark_node;
10793 /* Remember it, if there default args to post process. */
10794 cp_parser_save_default_args (parser, fn);
10796 /* Create a token cache. */
10797 cache = cp_token_cache_new ();
10798 /* Save away the tokens that make up the body of the
10799 function. */
10800 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10801 /* Handle function try blocks. */
10802 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
10803 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10805 /* Save away the inline definition; we will process it when the
10806 class is complete. */
10807 DECL_PENDING_INLINE_INFO (fn) = cache;
10808 DECL_PENDING_INLINE_P (fn) = 1;
10810 /* We need to know that this was defined in the class, so that
10811 friend templates are handled correctly. */
10812 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
10814 /* We're done with the inline definition. */
10815 finish_method (fn);
10817 /* Add FN to the queue of functions to be parsed later. */
10818 TREE_VALUE (parser->unparsed_functions_queues)
10819 = tree_cons (NULL_TREE, fn,
10820 TREE_VALUE (parser->unparsed_functions_queues));
10822 pop_deferring_access_checks ();
10823 return fn;
10826 /* Check that the number of template-parameter-lists is OK. */
10827 if (!cp_parser_check_declarator_template_parameters (parser,
10828 declarator))
10830 cp_parser_skip_to_end_of_block_or_statement (parser);
10831 pop_deferring_access_checks ();
10832 return error_mark_node;
10835 fn = cp_parser_function_definition_from_specifiers_and_declarator
10836 (parser, decl_specifiers, attributes, declarator);
10837 pop_deferring_access_checks ();
10838 return fn;
10841 /* Parse a function-body.
10843 function-body:
10844 compound_statement */
10846 static void
10847 cp_parser_function_body (cp_parser *parser)
10849 cp_parser_compound_statement (parser, false);
10852 /* Parse a ctor-initializer-opt followed by a function-body. Return
10853 true if a ctor-initializer was present. */
10855 static bool
10856 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
10858 tree body;
10859 bool ctor_initializer_p;
10861 /* Begin the function body. */
10862 body = begin_function_body ();
10863 /* Parse the optional ctor-initializer. */
10864 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
10865 /* Parse the function-body. */
10866 cp_parser_function_body (parser);
10867 /* Finish the function body. */
10868 finish_function_body (body);
10870 return ctor_initializer_p;
10873 /* Parse an initializer.
10875 initializer:
10876 = initializer-clause
10877 ( expression-list )
10879 Returns a expression representing the initializer. If no
10880 initializer is present, NULL_TREE is returned.
10882 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10883 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
10884 set to FALSE if there is no initializer present. If there is an
10885 initializer, and it is not a constant-expression, *NON_CONSTANT_P
10886 is set to true; otherwise it is set to false. */
10888 static tree
10889 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
10890 bool* non_constant_p)
10892 cp_token *token;
10893 tree init;
10895 /* Peek at the next token. */
10896 token = cp_lexer_peek_token (parser->lexer);
10898 /* Let our caller know whether or not this initializer was
10899 parenthesized. */
10900 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
10901 /* Assume that the initializer is constant. */
10902 *non_constant_p = false;
10904 if (token->type == CPP_EQ)
10906 /* Consume the `='. */
10907 cp_lexer_consume_token (parser->lexer);
10908 /* Parse the initializer-clause. */
10909 init = cp_parser_initializer_clause (parser, non_constant_p);
10911 else if (token->type == CPP_OPEN_PAREN)
10912 init = cp_parser_parenthesized_expression_list (parser, false,
10913 non_constant_p);
10914 else
10916 /* Anything else is an error. */
10917 cp_parser_error (parser, "expected initializer");
10918 init = error_mark_node;
10921 return init;
10924 /* Parse an initializer-clause.
10926 initializer-clause:
10927 assignment-expression
10928 { initializer-list , [opt] }
10931 Returns an expression representing the initializer.
10933 If the `assignment-expression' production is used the value
10934 returned is simply a representation for the expression.
10936 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
10937 the elements of the initializer-list (or NULL_TREE, if the last
10938 production is used). The TREE_TYPE for the CONSTRUCTOR will be
10939 NULL_TREE. There is no way to detect whether or not the optional
10940 trailing `,' was provided. NON_CONSTANT_P is as for
10941 cp_parser_initializer. */
10943 static tree
10944 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
10946 tree initializer;
10948 /* If it is not a `{', then we are looking at an
10949 assignment-expression. */
10950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10951 initializer
10952 = cp_parser_constant_expression (parser,
10953 /*allow_non_constant_p=*/true,
10954 non_constant_p);
10955 else
10957 /* Consume the `{' token. */
10958 cp_lexer_consume_token (parser->lexer);
10959 /* Create a CONSTRUCTOR to represent the braced-initializer. */
10960 initializer = make_node (CONSTRUCTOR);
10961 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
10962 necessary, but check_initializer depends upon it, for
10963 now. */
10964 TREE_HAS_CONSTRUCTOR (initializer) = 1;
10965 /* If it's not a `}', then there is a non-trivial initializer. */
10966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10968 /* Parse the initializer list. */
10969 CONSTRUCTOR_ELTS (initializer)
10970 = cp_parser_initializer_list (parser, non_constant_p);
10971 /* A trailing `,' token is allowed. */
10972 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10973 cp_lexer_consume_token (parser->lexer);
10975 /* Now, there should be a trailing `}'. */
10976 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10979 return initializer;
10982 /* Parse an initializer-list.
10984 initializer-list:
10985 initializer-clause
10986 initializer-list , initializer-clause
10988 GNU Extension:
10990 initializer-list:
10991 identifier : initializer-clause
10992 initializer-list, identifier : initializer-clause
10994 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
10995 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
10996 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
10997 as for cp_parser_initializer. */
10999 static tree
11000 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11002 tree initializers = NULL_TREE;
11004 /* Assume all of the expressions are constant. */
11005 *non_constant_p = false;
11007 /* Parse the rest of the list. */
11008 while (true)
11010 cp_token *token;
11011 tree identifier;
11012 tree initializer;
11013 bool clause_non_constant_p;
11015 /* If the next token is an identifier and the following one is a
11016 colon, we are looking at the GNU designated-initializer
11017 syntax. */
11018 if (cp_parser_allow_gnu_extensions_p (parser)
11019 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11020 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11022 /* Consume the identifier. */
11023 identifier = cp_lexer_consume_token (parser->lexer)->value;
11024 /* Consume the `:'. */
11025 cp_lexer_consume_token (parser->lexer);
11027 else
11028 identifier = NULL_TREE;
11030 /* Parse the initializer. */
11031 initializer = cp_parser_initializer_clause (parser,
11032 &clause_non_constant_p);
11033 /* If any clause is non-constant, so is the entire initializer. */
11034 if (clause_non_constant_p)
11035 *non_constant_p = true;
11036 /* Add it to the list. */
11037 initializers = tree_cons (identifier, initializer, initializers);
11039 /* If the next token is not a comma, we have reached the end of
11040 the list. */
11041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11042 break;
11044 /* Peek at the next token. */
11045 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11046 /* If the next token is a `}', then we're still done. An
11047 initializer-clause can have a trailing `,' after the
11048 initializer-list and before the closing `}'. */
11049 if (token->type == CPP_CLOSE_BRACE)
11050 break;
11052 /* Consume the `,' token. */
11053 cp_lexer_consume_token (parser->lexer);
11056 /* The initializers were built up in reverse order, so we need to
11057 reverse them now. */
11058 return nreverse (initializers);
11061 /* Classes [gram.class] */
11063 /* Parse a class-name.
11065 class-name:
11066 identifier
11067 template-id
11069 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11070 to indicate that names looked up in dependent types should be
11071 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11072 keyword has been used to indicate that the name that appears next
11073 is a template. TYPE_P is true iff the next name should be treated
11074 as class-name, even if it is declared to be some other kind of name
11075 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11076 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11077 being defined in a class-head.
11079 Returns the TYPE_DECL representing the class. */
11081 static tree
11082 cp_parser_class_name (cp_parser *parser,
11083 bool typename_keyword_p,
11084 bool template_keyword_p,
11085 bool type_p,
11086 bool check_dependency_p,
11087 bool class_head_p)
11089 tree decl;
11090 tree scope;
11091 bool typename_p;
11092 cp_token *token;
11094 /* All class-names start with an identifier. */
11095 token = cp_lexer_peek_token (parser->lexer);
11096 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11098 cp_parser_error (parser, "expected class-name");
11099 return error_mark_node;
11102 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11103 to a template-id, so we save it here. */
11104 scope = parser->scope;
11105 if (scope == error_mark_node)
11106 return error_mark_node;
11108 /* Any name names a type if we're following the `typename' keyword
11109 in a qualified name where the enclosing scope is type-dependent. */
11110 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11111 && dependent_type_p (scope));
11112 /* Handle the common case (an identifier, but not a template-id)
11113 efficiently. */
11114 if (token->type == CPP_NAME
11115 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11117 tree identifier;
11119 /* Look for the identifier. */
11120 identifier = cp_parser_identifier (parser);
11121 /* If the next token isn't an identifier, we are certainly not
11122 looking at a class-name. */
11123 if (identifier == error_mark_node)
11124 decl = error_mark_node;
11125 /* If we know this is a type-name, there's no need to look it
11126 up. */
11127 else if (typename_p)
11128 decl = identifier;
11129 else
11131 /* If the next token is a `::', then the name must be a type
11132 name.
11134 [basic.lookup.qual]
11136 During the lookup for a name preceding the :: scope
11137 resolution operator, object, function, and enumerator
11138 names are ignored. */
11139 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11140 type_p = true;
11141 /* Look up the name. */
11142 decl = cp_parser_lookup_name (parser, identifier,
11143 type_p,
11144 /*is_namespace=*/false,
11145 check_dependency_p);
11148 else
11150 /* Try a template-id. */
11151 decl = cp_parser_template_id (parser, template_keyword_p,
11152 check_dependency_p);
11153 if (decl == error_mark_node)
11154 return error_mark_node;
11157 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11159 /* If this is a typename, create a TYPENAME_TYPE. */
11160 if (typename_p && decl != error_mark_node)
11161 decl = TYPE_NAME (make_typename_type (scope, decl,
11162 /*complain=*/1));
11164 /* Check to see that it is really the name of a class. */
11165 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11166 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11167 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11168 /* Situations like this:
11170 template <typename T> struct A {
11171 typename T::template X<int>::I i;
11174 are problematic. Is `T::template X<int>' a class-name? The
11175 standard does not seem to be definitive, but there is no other
11176 valid interpretation of the following `::'. Therefore, those
11177 names are considered class-names. */
11178 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11179 else if (decl == error_mark_node
11180 || TREE_CODE (decl) != TYPE_DECL
11181 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11183 cp_parser_error (parser, "expected class-name");
11184 return error_mark_node;
11187 return decl;
11190 /* Parse a class-specifier.
11192 class-specifier:
11193 class-head { member-specification [opt] }
11195 Returns the TREE_TYPE representing the class. */
11197 static tree
11198 cp_parser_class_specifier (cp_parser* parser)
11200 cp_token *token;
11201 tree type;
11202 tree attributes = NULL_TREE;
11203 int has_trailing_semicolon;
11204 bool nested_name_specifier_p;
11205 unsigned saved_num_template_parameter_lists;
11207 push_deferring_access_checks (dk_no_deferred);
11209 /* Parse the class-head. */
11210 type = cp_parser_class_head (parser,
11211 &nested_name_specifier_p);
11212 /* If the class-head was a semantic disaster, skip the entire body
11213 of the class. */
11214 if (!type)
11216 cp_parser_skip_to_end_of_block_or_statement (parser);
11217 pop_deferring_access_checks ();
11218 return error_mark_node;
11221 /* Look for the `{'. */
11222 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11224 pop_deferring_access_checks ();
11225 return error_mark_node;
11228 /* Issue an error message if type-definitions are forbidden here. */
11229 cp_parser_check_type_definition (parser);
11230 /* Remember that we are defining one more class. */
11231 ++parser->num_classes_being_defined;
11232 /* Inside the class, surrounding template-parameter-lists do not
11233 apply. */
11234 saved_num_template_parameter_lists
11235 = parser->num_template_parameter_lists;
11236 parser->num_template_parameter_lists = 0;
11238 /* Start the class. */
11239 type = begin_class_definition (type);
11240 if (type == error_mark_node)
11241 /* If the type is erroneous, skip the entire body of the class. */
11242 cp_parser_skip_to_closing_brace (parser);
11243 else
11244 /* Parse the member-specification. */
11245 cp_parser_member_specification_opt (parser);
11246 /* Look for the trailing `}'. */
11247 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11248 /* We get better error messages by noticing a common problem: a
11249 missing trailing `;'. */
11250 token = cp_lexer_peek_token (parser->lexer);
11251 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11252 /* Look for attributes to apply to this class. */
11253 if (cp_parser_allow_gnu_extensions_p (parser))
11254 attributes = cp_parser_attributes_opt (parser);
11255 /* If we got any attributes in class_head, xref_tag will stick them in
11256 TREE_TYPE of the type. Grab them now. */
11257 if (type != error_mark_node)
11259 attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11260 TYPE_ATTRIBUTES (type) = NULL_TREE;
11261 type = finish_struct (type, attributes);
11263 if (nested_name_specifier_p)
11264 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11265 /* If this class is not itself within the scope of another class,
11266 then we need to parse the bodies of all of the queued function
11267 definitions. Note that the queued functions defined in a class
11268 are not always processed immediately following the
11269 class-specifier for that class. Consider:
11271 struct A {
11272 struct B { void f() { sizeof (A); } };
11275 If `f' were processed before the processing of `A' were
11276 completed, there would be no way to compute the size of `A'.
11277 Note that the nesting we are interested in here is lexical --
11278 not the semantic nesting given by TYPE_CONTEXT. In particular,
11279 for:
11281 struct A { struct B; };
11282 struct A::B { void f() { } };
11284 there is no need to delay the parsing of `A::B::f'. */
11285 if (--parser->num_classes_being_defined == 0)
11287 tree queue_entry;
11288 tree fn;
11290 /* In a first pass, parse default arguments to the functions.
11291 Then, in a second pass, parse the bodies of the functions.
11292 This two-phased approach handles cases like:
11294 struct S {
11295 void f() { g(); }
11296 void g(int i = 3);
11300 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11301 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11302 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11303 TREE_PURPOSE (parser->unparsed_functions_queues)
11304 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11306 fn = TREE_VALUE (queue_entry);
11307 /* Make sure that any template parameters are in scope. */
11308 maybe_begin_member_template_processing (fn);
11309 /* If there are default arguments that have not yet been processed,
11310 take care of them now. */
11311 cp_parser_late_parsing_default_args (parser, fn);
11312 /* Remove any template parameters from the symbol table. */
11313 maybe_end_member_template_processing ();
11315 /* Now parse the body of the functions. */
11316 for (TREE_VALUE (parser->unparsed_functions_queues)
11317 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11318 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11319 TREE_VALUE (parser->unparsed_functions_queues)
11320 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11322 /* Figure out which function we need to process. */
11323 fn = TREE_VALUE (queue_entry);
11325 /* Parse the function. */
11326 cp_parser_late_parsing_for_member (parser, fn);
11331 /* Put back any saved access checks. */
11332 pop_deferring_access_checks ();
11334 /* Restore the count of active template-parameter-lists. */
11335 parser->num_template_parameter_lists
11336 = saved_num_template_parameter_lists;
11338 return type;
11341 /* Parse a class-head.
11343 class-head:
11344 class-key identifier [opt] base-clause [opt]
11345 class-key nested-name-specifier identifier base-clause [opt]
11346 class-key nested-name-specifier [opt] template-id
11347 base-clause [opt]
11349 GNU Extensions:
11350 class-key attributes identifier [opt] base-clause [opt]
11351 class-key attributes nested-name-specifier identifier base-clause [opt]
11352 class-key attributes nested-name-specifier [opt] template-id
11353 base-clause [opt]
11355 Returns the TYPE of the indicated class. Sets
11356 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11357 involving a nested-name-specifier was used, and FALSE otherwise.
11359 Returns NULL_TREE if the class-head is syntactically valid, but
11360 semantically invalid in a way that means we should skip the entire
11361 body of the class. */
11363 static tree
11364 cp_parser_class_head (cp_parser* parser,
11365 bool* nested_name_specifier_p)
11367 cp_token *token;
11368 tree nested_name_specifier;
11369 enum tag_types class_key;
11370 tree id = NULL_TREE;
11371 tree type = NULL_TREE;
11372 tree attributes;
11373 bool template_id_p = false;
11374 bool qualified_p = false;
11375 bool invalid_nested_name_p = false;
11376 unsigned num_templates;
11378 /* Assume no nested-name-specifier will be present. */
11379 *nested_name_specifier_p = false;
11380 /* Assume no template parameter lists will be used in defining the
11381 type. */
11382 num_templates = 0;
11384 /* Look for the class-key. */
11385 class_key = cp_parser_class_key (parser);
11386 if (class_key == none_type)
11387 return error_mark_node;
11389 /* Parse the attributes. */
11390 attributes = cp_parser_attributes_opt (parser);
11392 /* If the next token is `::', that is invalid -- but sometimes
11393 people do try to write:
11395 struct ::S {};
11397 Handle this gracefully by accepting the extra qualifier, and then
11398 issuing an error about it later if this really is a
11399 class-head. If it turns out just to be an elaborated type
11400 specifier, remain silent. */
11401 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11402 qualified_p = true;
11404 push_deferring_access_checks (dk_no_check);
11406 /* Determine the name of the class. Begin by looking for an
11407 optional nested-name-specifier. */
11408 nested_name_specifier
11409 = cp_parser_nested_name_specifier_opt (parser,
11410 /*typename_keyword_p=*/false,
11411 /*check_dependency_p=*/false,
11412 /*type_p=*/false);
11413 /* If there was a nested-name-specifier, then there *must* be an
11414 identifier. */
11415 if (nested_name_specifier)
11417 /* Although the grammar says `identifier', it really means
11418 `class-name' or `template-name'. You are only allowed to
11419 define a class that has already been declared with this
11420 syntax.
11422 The proposed resolution for Core Issue 180 says that whever
11423 you see `class T::X' you should treat `X' as a type-name.
11425 It is OK to define an inaccessible class; for example:
11427 class A { class B; };
11428 class A::B {};
11430 We do not know if we will see a class-name, or a
11431 template-name. We look for a class-name first, in case the
11432 class-name is a template-id; if we looked for the
11433 template-name first we would stop after the template-name. */
11434 cp_parser_parse_tentatively (parser);
11435 type = cp_parser_class_name (parser,
11436 /*typename_keyword_p=*/false,
11437 /*template_keyword_p=*/false,
11438 /*type_p=*/true,
11439 /*check_dependency_p=*/false,
11440 /*class_head_p=*/true);
11441 /* If that didn't work, ignore the nested-name-specifier. */
11442 if (!cp_parser_parse_definitely (parser))
11444 invalid_nested_name_p = true;
11445 id = cp_parser_identifier (parser);
11446 if (id == error_mark_node)
11447 id = NULL_TREE;
11449 /* If we could not find a corresponding TYPE, treat this
11450 declaration like an unqualified declaration. */
11451 if (type == error_mark_node)
11452 nested_name_specifier = NULL_TREE;
11453 /* Otherwise, count the number of templates used in TYPE and its
11454 containing scopes. */
11455 else
11457 tree scope;
11459 for (scope = TREE_TYPE (type);
11460 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11461 scope = (TYPE_P (scope)
11462 ? TYPE_CONTEXT (scope)
11463 : DECL_CONTEXT (scope)))
11464 if (TYPE_P (scope)
11465 && CLASS_TYPE_P (scope)
11466 && CLASSTYPE_TEMPLATE_INFO (scope)
11467 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11468 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11469 ++num_templates;
11472 /* Otherwise, the identifier is optional. */
11473 else
11475 /* We don't know whether what comes next is a template-id,
11476 an identifier, or nothing at all. */
11477 cp_parser_parse_tentatively (parser);
11478 /* Check for a template-id. */
11479 id = cp_parser_template_id (parser,
11480 /*template_keyword_p=*/false,
11481 /*check_dependency_p=*/true);
11482 /* If that didn't work, it could still be an identifier. */
11483 if (!cp_parser_parse_definitely (parser))
11485 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11486 id = cp_parser_identifier (parser);
11487 else
11488 id = NULL_TREE;
11490 else
11492 template_id_p = true;
11493 ++num_templates;
11497 pop_deferring_access_checks ();
11499 /* If it's not a `:' or a `{' then we can't really be looking at a
11500 class-head, since a class-head only appears as part of a
11501 class-specifier. We have to detect this situation before calling
11502 xref_tag, since that has irreversible side-effects. */
11503 if (!cp_parser_next_token_starts_class_definition_p (parser))
11505 cp_parser_error (parser, "expected `{' or `:'");
11506 return error_mark_node;
11509 /* At this point, we're going ahead with the class-specifier, even
11510 if some other problem occurs. */
11511 cp_parser_commit_to_tentative_parse (parser);
11512 /* Issue the error about the overly-qualified name now. */
11513 if (qualified_p)
11514 cp_parser_error (parser,
11515 "global qualification of class name is invalid");
11516 else if (invalid_nested_name_p)
11517 cp_parser_error (parser,
11518 "qualified name does not name a class");
11519 /* Make sure that the right number of template parameters were
11520 present. */
11521 if (!cp_parser_check_template_parameters (parser, num_templates))
11522 /* If something went wrong, there is no point in even trying to
11523 process the class-definition. */
11524 return NULL_TREE;
11526 /* Look up the type. */
11527 if (template_id_p)
11529 type = TREE_TYPE (id);
11530 maybe_process_partial_specialization (type);
11532 else if (!nested_name_specifier)
11534 /* If the class was unnamed, create a dummy name. */
11535 if (!id)
11536 id = make_anon_name ();
11537 type = xref_tag (class_key, id, attributes, /*globalize=*/false,
11538 parser->num_template_parameter_lists);
11540 else
11542 tree class_type;
11543 tree scope;
11545 /* Given:
11547 template <typename T> struct S { struct T };
11548 template <typename T> struct S<T>::T { };
11550 we will get a TYPENAME_TYPE when processing the definition of
11551 `S::T'. We need to resolve it to the actual type before we
11552 try to define it. */
11553 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11555 class_type = resolve_typename_type (TREE_TYPE (type),
11556 /*only_current_p=*/false);
11557 if (class_type != error_mark_node)
11558 type = TYPE_NAME (class_type);
11559 else
11561 cp_parser_error (parser, "could not resolve typename type");
11562 type = error_mark_node;
11566 /* Figure out in what scope the declaration is being placed. */
11567 scope = current_scope ();
11568 if (!scope)
11569 scope = current_namespace;
11570 /* If that scope does not contain the scope in which the
11571 class was originally declared, the program is invalid. */
11572 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11574 error ("declaration of `%D' in `%D' which does not "
11575 "enclose `%D'", type, scope, nested_name_specifier);
11576 return NULL_TREE;
11578 /* [dcl.meaning]
11580 A declarator-id shall not be qualified exception of the
11581 definition of a ... nested class outside of its class
11582 ... [or] a the definition or explicit instantiation of a
11583 class member of a namespace outside of its namespace. */
11584 if (scope == CP_DECL_CONTEXT (type))
11586 pedwarn ("extra qualification ignored");
11587 nested_name_specifier = NULL_TREE;
11590 maybe_process_partial_specialization (TREE_TYPE (type));
11591 class_type = current_class_type;
11592 /* Enter the scope indicated by the nested-name-specifier. */
11593 if (nested_name_specifier)
11594 push_scope (nested_name_specifier);
11595 /* Get the canonical version of this type. */
11596 type = TYPE_MAIN_DECL (TREE_TYPE (type));
11597 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
11598 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
11599 type = push_template_decl (type);
11600 type = TREE_TYPE (type);
11601 if (nested_name_specifier)
11602 *nested_name_specifier_p = true;
11604 /* Indicate whether this class was declared as a `class' or as a
11605 `struct'. */
11606 if (TREE_CODE (type) == RECORD_TYPE)
11607 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11608 cp_parser_check_class_key (class_key, type);
11610 /* Enter the scope containing the class; the names of base classes
11611 should be looked up in that context. For example, given:
11613 struct A { struct B {}; struct C; };
11614 struct A::C : B {};
11616 is valid. */
11617 if (nested_name_specifier)
11618 push_scope (nested_name_specifier);
11619 /* Now, look for the base-clause. */
11620 token = cp_lexer_peek_token (parser->lexer);
11621 if (token->type == CPP_COLON)
11623 tree bases;
11625 /* Get the list of base-classes. */
11626 bases = cp_parser_base_clause (parser);
11627 /* Process them. */
11628 xref_basetypes (type, bases);
11630 /* Leave the scope given by the nested-name-specifier. We will
11631 enter the class scope itself while processing the members. */
11632 if (nested_name_specifier)
11633 pop_scope (nested_name_specifier);
11635 return type;
11638 /* Parse a class-key.
11640 class-key:
11641 class
11642 struct
11643 union
11645 Returns the kind of class-key specified, or none_type to indicate
11646 error. */
11648 static enum tag_types
11649 cp_parser_class_key (cp_parser* parser)
11651 cp_token *token;
11652 enum tag_types tag_type;
11654 /* Look for the class-key. */
11655 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11656 if (!token)
11657 return none_type;
11659 /* Check to see if the TOKEN is a class-key. */
11660 tag_type = cp_parser_token_is_class_key (token);
11661 if (!tag_type)
11662 cp_parser_error (parser, "expected class-key");
11663 return tag_type;
11666 /* Parse an (optional) member-specification.
11668 member-specification:
11669 member-declaration member-specification [opt]
11670 access-specifier : member-specification [opt] */
11672 static void
11673 cp_parser_member_specification_opt (cp_parser* parser)
11675 while (true)
11677 cp_token *token;
11678 enum rid keyword;
11680 /* Peek at the next token. */
11681 token = cp_lexer_peek_token (parser->lexer);
11682 /* If it's a `}', or EOF then we've seen all the members. */
11683 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11684 break;
11686 /* See if this token is a keyword. */
11687 keyword = token->keyword;
11688 switch (keyword)
11690 case RID_PUBLIC:
11691 case RID_PROTECTED:
11692 case RID_PRIVATE:
11693 /* Consume the access-specifier. */
11694 cp_lexer_consume_token (parser->lexer);
11695 /* Remember which access-specifier is active. */
11696 current_access_specifier = token->value;
11697 /* Look for the `:'. */
11698 cp_parser_require (parser, CPP_COLON, "`:'");
11699 break;
11701 default:
11702 /* Otherwise, the next construction must be a
11703 member-declaration. */
11704 cp_parser_member_declaration (parser);
11709 /* Parse a member-declaration.
11711 member-declaration:
11712 decl-specifier-seq [opt] member-declarator-list [opt] ;
11713 function-definition ; [opt]
11714 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11715 using-declaration
11716 template-declaration
11718 member-declarator-list:
11719 member-declarator
11720 member-declarator-list , member-declarator
11722 member-declarator:
11723 declarator pure-specifier [opt]
11724 declarator constant-initializer [opt]
11725 identifier [opt] : constant-expression
11727 GNU Extensions:
11729 member-declaration:
11730 __extension__ member-declaration
11732 member-declarator:
11733 declarator attributes [opt] pure-specifier [opt]
11734 declarator attributes [opt] constant-initializer [opt]
11735 identifier [opt] attributes [opt] : constant-expression */
11737 static void
11738 cp_parser_member_declaration (cp_parser* parser)
11740 tree decl_specifiers;
11741 tree prefix_attributes;
11742 tree decl;
11743 int declares_class_or_enum;
11744 bool friend_p;
11745 cp_token *token;
11746 int saved_pedantic;
11748 /* Check for the `__extension__' keyword. */
11749 if (cp_parser_extension_opt (parser, &saved_pedantic))
11751 /* Recurse. */
11752 cp_parser_member_declaration (parser);
11753 /* Restore the old value of the PEDANTIC flag. */
11754 pedantic = saved_pedantic;
11756 return;
11759 /* Check for a template-declaration. */
11760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11762 /* Parse the template-declaration. */
11763 cp_parser_template_declaration (parser, /*member_p=*/true);
11765 return;
11768 /* Check for a using-declaration. */
11769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11771 /* Parse the using-declaration. */
11772 cp_parser_using_declaration (parser);
11774 return;
11777 /* We can't tell whether we're looking at a declaration or a
11778 function-definition. */
11779 cp_parser_parse_tentatively (parser);
11781 /* Parse the decl-specifier-seq. */
11782 decl_specifiers
11783 = cp_parser_decl_specifier_seq (parser,
11784 CP_PARSER_FLAGS_OPTIONAL,
11785 &prefix_attributes,
11786 &declares_class_or_enum);
11787 /* Check for an invalid type-name. */
11788 if (cp_parser_diagnose_invalid_type_name (parser))
11789 return;
11790 /* If there is no declarator, then the decl-specifier-seq should
11791 specify a type. */
11792 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11794 /* If there was no decl-specifier-seq, and the next token is a
11795 `;', then we have something like:
11797 struct S { ; };
11799 [class.mem]
11801 Each member-declaration shall declare at least one member
11802 name of the class. */
11803 if (!decl_specifiers)
11805 if (pedantic)
11806 pedwarn ("extra semicolon");
11808 else
11810 tree type;
11812 /* See if this declaration is a friend. */
11813 friend_p = cp_parser_friend_p (decl_specifiers);
11814 /* If there were decl-specifiers, check to see if there was
11815 a class-declaration. */
11816 type = check_tag_decl (decl_specifiers);
11817 /* Nested classes have already been added to the class, but
11818 a `friend' needs to be explicitly registered. */
11819 if (friend_p)
11821 /* If the `friend' keyword was present, the friend must
11822 be introduced with a class-key. */
11823 if (!declares_class_or_enum)
11824 error ("a class-key must be used when declaring a friend");
11825 /* In this case:
11827 template <typename T> struct A {
11828 friend struct A<T>::B;
11831 A<T>::B will be represented by a TYPENAME_TYPE, and
11832 therefore not recognized by check_tag_decl. */
11833 if (!type)
11835 tree specifier;
11837 for (specifier = decl_specifiers;
11838 specifier;
11839 specifier = TREE_CHAIN (specifier))
11841 tree s = TREE_VALUE (specifier);
11843 if (TREE_CODE (s) == IDENTIFIER_NODE)
11844 get_global_value_if_present (s, &type);
11845 if (TREE_CODE (s) == TYPE_DECL)
11846 s = TREE_TYPE (s);
11847 if (TYPE_P (s))
11849 type = s;
11850 break;
11854 if (!type)
11855 error ("friend declaration does not name a class or "
11856 "function");
11857 else
11858 make_friend_class (current_class_type, type,
11859 /*complain=*/true);
11861 /* If there is no TYPE, an error message will already have
11862 been issued. */
11863 else if (!type)
11865 /* An anonymous aggregate has to be handled specially; such
11866 a declaration really declares a data member (with a
11867 particular type), as opposed to a nested class. */
11868 else if (ANON_AGGR_TYPE_P (type))
11870 /* Remove constructors and such from TYPE, now that we
11871 know it is an anonymous aggregate. */
11872 fixup_anonymous_aggr (type);
11873 /* And make the corresponding data member. */
11874 decl = build_decl (FIELD_DECL, NULL_TREE, type);
11875 /* Add it to the class. */
11876 finish_member_declaration (decl);
11878 else
11879 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
11882 else
11884 /* See if these declarations will be friends. */
11885 friend_p = cp_parser_friend_p (decl_specifiers);
11887 /* Keep going until we hit the `;' at the end of the
11888 declaration. */
11889 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11891 tree attributes = NULL_TREE;
11892 tree first_attribute;
11894 /* Peek at the next token. */
11895 token = cp_lexer_peek_token (parser->lexer);
11897 /* Check for a bitfield declaration. */
11898 if (token->type == CPP_COLON
11899 || (token->type == CPP_NAME
11900 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
11901 == CPP_COLON))
11903 tree identifier;
11904 tree width;
11906 /* Get the name of the bitfield. Note that we cannot just
11907 check TOKEN here because it may have been invalidated by
11908 the call to cp_lexer_peek_nth_token above. */
11909 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
11910 identifier = cp_parser_identifier (parser);
11911 else
11912 identifier = NULL_TREE;
11914 /* Consume the `:' token. */
11915 cp_lexer_consume_token (parser->lexer);
11916 /* Get the width of the bitfield. */
11917 width
11918 = cp_parser_constant_expression (parser,
11919 /*allow_non_constant=*/false,
11920 NULL);
11922 /* Look for attributes that apply to the bitfield. */
11923 attributes = cp_parser_attributes_opt (parser);
11924 /* Remember which attributes are prefix attributes and
11925 which are not. */
11926 first_attribute = attributes;
11927 /* Combine the attributes. */
11928 attributes = chainon (prefix_attributes, attributes);
11930 /* Create the bitfield declaration. */
11931 decl = grokbitfield (identifier,
11932 decl_specifiers,
11933 width);
11934 /* Apply the attributes. */
11935 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
11937 else
11939 tree declarator;
11940 tree initializer;
11941 tree asm_specification;
11942 int ctor_dtor_or_conv_p;
11944 /* Parse the declarator. */
11945 declarator
11946 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11947 &ctor_dtor_or_conv_p);
11949 /* If something went wrong parsing the declarator, make sure
11950 that we at least consume some tokens. */
11951 if (declarator == error_mark_node)
11953 /* Skip to the end of the statement. */
11954 cp_parser_skip_to_end_of_statement (parser);
11955 break;
11958 cp_parser_check_for_definition_in_return_type
11959 (declarator, declares_class_or_enum);
11961 /* Look for an asm-specification. */
11962 asm_specification = cp_parser_asm_specification_opt (parser);
11963 /* Look for attributes that apply to the declaration. */
11964 attributes = cp_parser_attributes_opt (parser);
11965 /* Remember which attributes are prefix attributes and
11966 which are not. */
11967 first_attribute = attributes;
11968 /* Combine the attributes. */
11969 attributes = chainon (prefix_attributes, attributes);
11971 /* If it's an `=', then we have a constant-initializer or a
11972 pure-specifier. It is not correct to parse the
11973 initializer before registering the member declaration
11974 since the member declaration should be in scope while
11975 its initializer is processed. However, the rest of the
11976 front end does not yet provide an interface that allows
11977 us to handle this correctly. */
11978 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11980 /* In [class.mem]:
11982 A pure-specifier shall be used only in the declaration of
11983 a virtual function.
11985 A member-declarator can contain a constant-initializer
11986 only if it declares a static member of integral or
11987 enumeration type.
11989 Therefore, if the DECLARATOR is for a function, we look
11990 for a pure-specifier; otherwise, we look for a
11991 constant-initializer. When we call `grokfield', it will
11992 perform more stringent semantics checks. */
11993 if (TREE_CODE (declarator) == CALL_EXPR)
11994 initializer = cp_parser_pure_specifier (parser);
11995 else
11997 /* This declaration cannot be a function
11998 definition. */
11999 cp_parser_commit_to_tentative_parse (parser);
12000 /* Parse the initializer. */
12001 initializer = cp_parser_constant_initializer (parser);
12004 /* Otherwise, there is no initializer. */
12005 else
12006 initializer = NULL_TREE;
12008 /* See if we are probably looking at a function
12009 definition. We are certainly not looking at at a
12010 member-declarator. Calling `grokfield' has
12011 side-effects, so we must not do it unless we are sure
12012 that we are looking at a member-declarator. */
12013 if (cp_parser_token_starts_function_definition_p
12014 (cp_lexer_peek_token (parser->lexer)))
12015 decl = error_mark_node;
12016 else
12018 /* Create the declaration. */
12019 decl = grokfield (declarator, decl_specifiers,
12020 initializer, asm_specification,
12021 attributes);
12022 /* Any initialization must have been from a
12023 constant-expression. */
12024 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12025 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12029 /* Reset PREFIX_ATTRIBUTES. */
12030 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12031 attributes = TREE_CHAIN (attributes);
12032 if (attributes)
12033 TREE_CHAIN (attributes) = NULL_TREE;
12035 /* If there is any qualification still in effect, clear it
12036 now; we will be starting fresh with the next declarator. */
12037 parser->scope = NULL_TREE;
12038 parser->qualifying_scope = NULL_TREE;
12039 parser->object_scope = NULL_TREE;
12040 /* If it's a `,', then there are more declarators. */
12041 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12042 cp_lexer_consume_token (parser->lexer);
12043 /* If the next token isn't a `;', then we have a parse error. */
12044 else if (cp_lexer_next_token_is_not (parser->lexer,
12045 CPP_SEMICOLON))
12047 cp_parser_error (parser, "expected `;'");
12048 /* Skip tokens until we find a `;' */
12049 cp_parser_skip_to_end_of_statement (parser);
12051 break;
12054 if (decl)
12056 /* Add DECL to the list of members. */
12057 if (!friend_p)
12058 finish_member_declaration (decl);
12060 if (TREE_CODE (decl) == FUNCTION_DECL)
12061 cp_parser_save_default_args (parser, decl);
12066 /* If everything went well, look for the `;'. */
12067 if (cp_parser_parse_definitely (parser))
12069 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12070 return;
12073 /* Parse the function-definition. */
12074 decl = cp_parser_function_definition (parser, &friend_p);
12075 /* If the member was not a friend, declare it here. */
12076 if (!friend_p)
12077 finish_member_declaration (decl);
12078 /* Peek at the next token. */
12079 token = cp_lexer_peek_token (parser->lexer);
12080 /* If the next token is a semicolon, consume it. */
12081 if (token->type == CPP_SEMICOLON)
12082 cp_lexer_consume_token (parser->lexer);
12085 /* Parse a pure-specifier.
12087 pure-specifier:
12090 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12091 Otherwiser, ERROR_MARK_NODE is returned. */
12093 static tree
12094 cp_parser_pure_specifier (cp_parser* parser)
12096 cp_token *token;
12098 /* Look for the `=' token. */
12099 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12100 return error_mark_node;
12101 /* Look for the `0' token. */
12102 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12103 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12104 to get information from the lexer about how the number was
12105 spelled in order to fix this problem. */
12106 if (!token || !integer_zerop (token->value))
12107 return error_mark_node;
12109 return integer_zero_node;
12112 /* Parse a constant-initializer.
12114 constant-initializer:
12115 = constant-expression
12117 Returns a representation of the constant-expression. */
12119 static tree
12120 cp_parser_constant_initializer (cp_parser* parser)
12122 /* Look for the `=' token. */
12123 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12124 return error_mark_node;
12126 /* It is invalid to write:
12128 struct S { static const int i = { 7 }; };
12131 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12133 cp_parser_error (parser,
12134 "a brace-enclosed initializer is not allowed here");
12135 /* Consume the opening brace. */
12136 cp_lexer_consume_token (parser->lexer);
12137 /* Skip the initializer. */
12138 cp_parser_skip_to_closing_brace (parser);
12139 /* Look for the trailing `}'. */
12140 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12142 return error_mark_node;
12145 return cp_parser_constant_expression (parser,
12146 /*allow_non_constant=*/false,
12147 NULL);
12150 /* Derived classes [gram.class.derived] */
12152 /* Parse a base-clause.
12154 base-clause:
12155 : base-specifier-list
12157 base-specifier-list:
12158 base-specifier
12159 base-specifier-list , base-specifier
12161 Returns a TREE_LIST representing the base-classes, in the order in
12162 which they were declared. The representation of each node is as
12163 described by cp_parser_base_specifier.
12165 In the case that no bases are specified, this function will return
12166 NULL_TREE, not ERROR_MARK_NODE. */
12168 static tree
12169 cp_parser_base_clause (cp_parser* parser)
12171 tree bases = NULL_TREE;
12173 /* Look for the `:' that begins the list. */
12174 cp_parser_require (parser, CPP_COLON, "`:'");
12176 /* Scan the base-specifier-list. */
12177 while (true)
12179 cp_token *token;
12180 tree base;
12182 /* Look for the base-specifier. */
12183 base = cp_parser_base_specifier (parser);
12184 /* Add BASE to the front of the list. */
12185 if (base != error_mark_node)
12187 TREE_CHAIN (base) = bases;
12188 bases = base;
12190 /* Peek at the next token. */
12191 token = cp_lexer_peek_token (parser->lexer);
12192 /* If it's not a comma, then the list is complete. */
12193 if (token->type != CPP_COMMA)
12194 break;
12195 /* Consume the `,'. */
12196 cp_lexer_consume_token (parser->lexer);
12199 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12200 base class had a qualified name. However, the next name that
12201 appears is certainly not qualified. */
12202 parser->scope = NULL_TREE;
12203 parser->qualifying_scope = NULL_TREE;
12204 parser->object_scope = NULL_TREE;
12206 return nreverse (bases);
12209 /* Parse a base-specifier.
12211 base-specifier:
12212 :: [opt] nested-name-specifier [opt] class-name
12213 virtual access-specifier [opt] :: [opt] nested-name-specifier
12214 [opt] class-name
12215 access-specifier virtual [opt] :: [opt] nested-name-specifier
12216 [opt] class-name
12218 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12219 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12220 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12221 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12223 static tree
12224 cp_parser_base_specifier (cp_parser* parser)
12226 cp_token *token;
12227 bool done = false;
12228 bool virtual_p = false;
12229 bool duplicate_virtual_error_issued_p = false;
12230 bool duplicate_access_error_issued_p = false;
12231 bool class_scope_p, template_p;
12232 tree access = access_default_node;
12233 tree type;
12235 /* Process the optional `virtual' and `access-specifier'. */
12236 while (!done)
12238 /* Peek at the next token. */
12239 token = cp_lexer_peek_token (parser->lexer);
12240 /* Process `virtual'. */
12241 switch (token->keyword)
12243 case RID_VIRTUAL:
12244 /* If `virtual' appears more than once, issue an error. */
12245 if (virtual_p && !duplicate_virtual_error_issued_p)
12247 cp_parser_error (parser,
12248 "`virtual' specified more than once in base-specified");
12249 duplicate_virtual_error_issued_p = true;
12252 virtual_p = true;
12254 /* Consume the `virtual' token. */
12255 cp_lexer_consume_token (parser->lexer);
12257 break;
12259 case RID_PUBLIC:
12260 case RID_PROTECTED:
12261 case RID_PRIVATE:
12262 /* If more than one access specifier appears, issue an
12263 error. */
12264 if (access != access_default_node
12265 && !duplicate_access_error_issued_p)
12267 cp_parser_error (parser,
12268 "more than one access specifier in base-specified");
12269 duplicate_access_error_issued_p = true;
12272 access = ridpointers[(int) token->keyword];
12274 /* Consume the access-specifier. */
12275 cp_lexer_consume_token (parser->lexer);
12277 break;
12279 default:
12280 done = true;
12281 break;
12285 /* Look for the optional `::' operator. */
12286 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12287 /* Look for the nested-name-specifier. The simplest way to
12288 implement:
12290 [temp.res]
12292 The keyword `typename' is not permitted in a base-specifier or
12293 mem-initializer; in these contexts a qualified name that
12294 depends on a template-parameter is implicitly assumed to be a
12295 type name.
12297 is to pretend that we have seen the `typename' keyword at this
12298 point. */
12299 cp_parser_nested_name_specifier_opt (parser,
12300 /*typename_keyword_p=*/true,
12301 /*check_dependency_p=*/true,
12302 /*type_p=*/true);
12303 /* If the base class is given by a qualified name, assume that names
12304 we see are type names or templates, as appropriate. */
12305 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12306 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12308 /* Finally, look for the class-name. */
12309 type = cp_parser_class_name (parser,
12310 class_scope_p,
12311 template_p,
12312 /*type_p=*/true,
12313 /*check_dependency_p=*/true,
12314 /*class_head_p=*/false);
12316 if (type == error_mark_node)
12317 return error_mark_node;
12319 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12322 /* Exception handling [gram.exception] */
12324 /* Parse an (optional) exception-specification.
12326 exception-specification:
12327 throw ( type-id-list [opt] )
12329 Returns a TREE_LIST representing the exception-specification. The
12330 TREE_VALUE of each node is a type. */
12332 static tree
12333 cp_parser_exception_specification_opt (cp_parser* parser)
12335 cp_token *token;
12336 tree type_id_list;
12338 /* Peek at the next token. */
12339 token = cp_lexer_peek_token (parser->lexer);
12340 /* If it's not `throw', then there's no exception-specification. */
12341 if (!cp_parser_is_keyword (token, RID_THROW))
12342 return NULL_TREE;
12344 /* Consume the `throw'. */
12345 cp_lexer_consume_token (parser->lexer);
12347 /* Look for the `('. */
12348 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12350 /* Peek at the next token. */
12351 token = cp_lexer_peek_token (parser->lexer);
12352 /* If it's not a `)', then there is a type-id-list. */
12353 if (token->type != CPP_CLOSE_PAREN)
12355 const char *saved_message;
12357 /* Types may not be defined in an exception-specification. */
12358 saved_message = parser->type_definition_forbidden_message;
12359 parser->type_definition_forbidden_message
12360 = "types may not be defined in an exception-specification";
12361 /* Parse the type-id-list. */
12362 type_id_list = cp_parser_type_id_list (parser);
12363 /* Restore the saved message. */
12364 parser->type_definition_forbidden_message = saved_message;
12366 else
12367 type_id_list = empty_except_spec;
12369 /* Look for the `)'. */
12370 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12372 return type_id_list;
12375 /* Parse an (optional) type-id-list.
12377 type-id-list:
12378 type-id
12379 type-id-list , type-id
12381 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12382 in the order that the types were presented. */
12384 static tree
12385 cp_parser_type_id_list (cp_parser* parser)
12387 tree types = NULL_TREE;
12389 while (true)
12391 cp_token *token;
12392 tree type;
12394 /* Get the next type-id. */
12395 type = cp_parser_type_id (parser);
12396 /* Add it to the list. */
12397 types = add_exception_specifier (types, type, /*complain=*/1);
12398 /* Peek at the next token. */
12399 token = cp_lexer_peek_token (parser->lexer);
12400 /* If it is not a `,', we are done. */
12401 if (token->type != CPP_COMMA)
12402 break;
12403 /* Consume the `,'. */
12404 cp_lexer_consume_token (parser->lexer);
12407 return nreverse (types);
12410 /* Parse a try-block.
12412 try-block:
12413 try compound-statement handler-seq */
12415 static tree
12416 cp_parser_try_block (cp_parser* parser)
12418 tree try_block;
12420 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12421 try_block = begin_try_block ();
12422 cp_parser_compound_statement (parser, false);
12423 finish_try_block (try_block);
12424 cp_parser_handler_seq (parser);
12425 finish_handler_sequence (try_block);
12427 return try_block;
12430 /* Parse a function-try-block.
12432 function-try-block:
12433 try ctor-initializer [opt] function-body handler-seq */
12435 static bool
12436 cp_parser_function_try_block (cp_parser* parser)
12438 tree try_block;
12439 bool ctor_initializer_p;
12441 /* Look for the `try' keyword. */
12442 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12443 return false;
12444 /* Let the rest of the front-end know where we are. */
12445 try_block = begin_function_try_block ();
12446 /* Parse the function-body. */
12447 ctor_initializer_p
12448 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12449 /* We're done with the `try' part. */
12450 finish_function_try_block (try_block);
12451 /* Parse the handlers. */
12452 cp_parser_handler_seq (parser);
12453 /* We're done with the handlers. */
12454 finish_function_handler_sequence (try_block);
12456 return ctor_initializer_p;
12459 /* Parse a handler-seq.
12461 handler-seq:
12462 handler handler-seq [opt] */
12464 static void
12465 cp_parser_handler_seq (cp_parser* parser)
12467 while (true)
12469 cp_token *token;
12471 /* Parse the handler. */
12472 cp_parser_handler (parser);
12473 /* Peek at the next token. */
12474 token = cp_lexer_peek_token (parser->lexer);
12475 /* If it's not `catch' then there are no more handlers. */
12476 if (!cp_parser_is_keyword (token, RID_CATCH))
12477 break;
12481 /* Parse a handler.
12483 handler:
12484 catch ( exception-declaration ) compound-statement */
12486 static void
12487 cp_parser_handler (cp_parser* parser)
12489 tree handler;
12490 tree declaration;
12492 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12493 handler = begin_handler ();
12494 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12495 declaration = cp_parser_exception_declaration (parser);
12496 finish_handler_parms (declaration, handler);
12497 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12498 cp_parser_compound_statement (parser, false);
12499 finish_handler (handler);
12502 /* Parse an exception-declaration.
12504 exception-declaration:
12505 type-specifier-seq declarator
12506 type-specifier-seq abstract-declarator
12507 type-specifier-seq
12508 ...
12510 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12511 ellipsis variant is used. */
12513 static tree
12514 cp_parser_exception_declaration (cp_parser* parser)
12516 tree type_specifiers;
12517 tree declarator;
12518 const char *saved_message;
12520 /* If it's an ellipsis, it's easy to handle. */
12521 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12523 /* Consume the `...' token. */
12524 cp_lexer_consume_token (parser->lexer);
12525 return NULL_TREE;
12528 /* Types may not be defined in exception-declarations. */
12529 saved_message = parser->type_definition_forbidden_message;
12530 parser->type_definition_forbidden_message
12531 = "types may not be defined in exception-declarations";
12533 /* Parse the type-specifier-seq. */
12534 type_specifiers = cp_parser_type_specifier_seq (parser);
12535 /* If it's a `)', then there is no declarator. */
12536 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12537 declarator = NULL_TREE;
12538 else
12539 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12540 /*ctor_dtor_or_conv_p=*/NULL);
12542 /* Restore the saved message. */
12543 parser->type_definition_forbidden_message = saved_message;
12545 return start_handler_parms (type_specifiers, declarator);
12548 /* Parse a throw-expression.
12550 throw-expression:
12551 throw assignment-expression [opt]
12553 Returns a THROW_EXPR representing the throw-expression. */
12555 static tree
12556 cp_parser_throw_expression (cp_parser* parser)
12558 tree expression;
12560 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12561 /* We can't be sure if there is an assignment-expression or not. */
12562 cp_parser_parse_tentatively (parser);
12563 /* Try it. */
12564 expression = cp_parser_assignment_expression (parser);
12565 /* If it didn't work, this is just a rethrow. */
12566 if (!cp_parser_parse_definitely (parser))
12567 expression = NULL_TREE;
12569 return build_throw (expression);
12572 /* GNU Extensions */
12574 /* Parse an (optional) asm-specification.
12576 asm-specification:
12577 asm ( string-literal )
12579 If the asm-specification is present, returns a STRING_CST
12580 corresponding to the string-literal. Otherwise, returns
12581 NULL_TREE. */
12583 static tree
12584 cp_parser_asm_specification_opt (cp_parser* parser)
12586 cp_token *token;
12587 tree asm_specification;
12589 /* Peek at the next token. */
12590 token = cp_lexer_peek_token (parser->lexer);
12591 /* If the next token isn't the `asm' keyword, then there's no
12592 asm-specification. */
12593 if (!cp_parser_is_keyword (token, RID_ASM))
12594 return NULL_TREE;
12596 /* Consume the `asm' token. */
12597 cp_lexer_consume_token (parser->lexer);
12598 /* Look for the `('. */
12599 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12601 /* Look for the string-literal. */
12602 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12603 if (token)
12604 asm_specification = token->value;
12605 else
12606 asm_specification = NULL_TREE;
12608 /* Look for the `)'. */
12609 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12611 return asm_specification;
12614 /* Parse an asm-operand-list.
12616 asm-operand-list:
12617 asm-operand
12618 asm-operand-list , asm-operand
12620 asm-operand:
12621 string-literal ( expression )
12622 [ string-literal ] string-literal ( expression )
12624 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12625 each node is the expression. The TREE_PURPOSE is itself a
12626 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12627 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12628 is a STRING_CST for the string literal before the parenthesis. */
12630 static tree
12631 cp_parser_asm_operand_list (cp_parser* parser)
12633 tree asm_operands = NULL_TREE;
12635 while (true)
12637 tree string_literal;
12638 tree expression;
12639 tree name;
12640 cp_token *token;
12642 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12644 /* Consume the `[' token. */
12645 cp_lexer_consume_token (parser->lexer);
12646 /* Read the operand name. */
12647 name = cp_parser_identifier (parser);
12648 if (name != error_mark_node)
12649 name = build_string (IDENTIFIER_LENGTH (name),
12650 IDENTIFIER_POINTER (name));
12651 /* Look for the closing `]'. */
12652 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12654 else
12655 name = NULL_TREE;
12656 /* Look for the string-literal. */
12657 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12658 string_literal = token ? token->value : error_mark_node;
12659 /* Look for the `('. */
12660 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12661 /* Parse the expression. */
12662 expression = cp_parser_expression (parser);
12663 /* Look for the `)'. */
12664 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12665 /* Add this operand to the list. */
12666 asm_operands = tree_cons (build_tree_list (name, string_literal),
12667 expression,
12668 asm_operands);
12669 /* If the next token is not a `,', there are no more
12670 operands. */
12671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12672 break;
12673 /* Consume the `,'. */
12674 cp_lexer_consume_token (parser->lexer);
12677 return nreverse (asm_operands);
12680 /* Parse an asm-clobber-list.
12682 asm-clobber-list:
12683 string-literal
12684 asm-clobber-list , string-literal
12686 Returns a TREE_LIST, indicating the clobbers in the order that they
12687 appeared. The TREE_VALUE of each node is a STRING_CST. */
12689 static tree
12690 cp_parser_asm_clobber_list (cp_parser* parser)
12692 tree clobbers = NULL_TREE;
12694 while (true)
12696 cp_token *token;
12697 tree string_literal;
12699 /* Look for the string literal. */
12700 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12701 string_literal = token ? token->value : error_mark_node;
12702 /* Add it to the list. */
12703 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12704 /* If the next token is not a `,', then the list is
12705 complete. */
12706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12707 break;
12708 /* Consume the `,' token. */
12709 cp_lexer_consume_token (parser->lexer);
12712 return clobbers;
12715 /* Parse an (optional) series of attributes.
12717 attributes:
12718 attributes attribute
12720 attribute:
12721 __attribute__ (( attribute-list [opt] ))
12723 The return value is as for cp_parser_attribute_list. */
12725 static tree
12726 cp_parser_attributes_opt (cp_parser* parser)
12728 tree attributes = NULL_TREE;
12730 while (true)
12732 cp_token *token;
12733 tree attribute_list;
12735 /* Peek at the next token. */
12736 token = cp_lexer_peek_token (parser->lexer);
12737 /* If it's not `__attribute__', then we're done. */
12738 if (token->keyword != RID_ATTRIBUTE)
12739 break;
12741 /* Consume the `__attribute__' keyword. */
12742 cp_lexer_consume_token (parser->lexer);
12743 /* Look for the two `(' tokens. */
12744 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12745 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12747 /* Peek at the next token. */
12748 token = cp_lexer_peek_token (parser->lexer);
12749 if (token->type != CPP_CLOSE_PAREN)
12750 /* Parse the attribute-list. */
12751 attribute_list = cp_parser_attribute_list (parser);
12752 else
12753 /* If the next token is a `)', then there is no attribute
12754 list. */
12755 attribute_list = NULL;
12757 /* Look for the two `)' tokens. */
12758 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12759 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12761 /* Add these new attributes to the list. */
12762 attributes = chainon (attributes, attribute_list);
12765 return attributes;
12768 /* Parse an attribute-list.
12770 attribute-list:
12771 attribute
12772 attribute-list , attribute
12774 attribute:
12775 identifier
12776 identifier ( identifier )
12777 identifier ( identifier , expression-list )
12778 identifier ( expression-list )
12780 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12781 TREE_PURPOSE of each node is the identifier indicating which
12782 attribute is in use. The TREE_VALUE represents the arguments, if
12783 any. */
12785 static tree
12786 cp_parser_attribute_list (cp_parser* parser)
12788 tree attribute_list = NULL_TREE;
12790 while (true)
12792 cp_token *token;
12793 tree identifier;
12794 tree attribute;
12796 /* Look for the identifier. We also allow keywords here; for
12797 example `__attribute__ ((const))' is legal. */
12798 token = cp_lexer_peek_token (parser->lexer);
12799 if (token->type != CPP_NAME
12800 && token->type != CPP_KEYWORD)
12801 return error_mark_node;
12802 /* Consume the token. */
12803 token = cp_lexer_consume_token (parser->lexer);
12805 /* Save away the identifier that indicates which attribute this is. */
12806 identifier = token->value;
12807 attribute = build_tree_list (identifier, NULL_TREE);
12809 /* Peek at the next token. */
12810 token = cp_lexer_peek_token (parser->lexer);
12811 /* If it's an `(', then parse the attribute arguments. */
12812 if (token->type == CPP_OPEN_PAREN)
12814 tree arguments;
12816 arguments = (cp_parser_parenthesized_expression_list
12817 (parser, true, /*non_constant_p=*/NULL));
12818 /* Save the identifier and arguments away. */
12819 TREE_VALUE (attribute) = arguments;
12822 /* Add this attribute to the list. */
12823 TREE_CHAIN (attribute) = attribute_list;
12824 attribute_list = attribute;
12826 /* Now, look for more attributes. */
12827 token = cp_lexer_peek_token (parser->lexer);
12828 /* If the next token isn't a `,', we're done. */
12829 if (token->type != CPP_COMMA)
12830 break;
12832 /* Consume the commma and keep going. */
12833 cp_lexer_consume_token (parser->lexer);
12836 /* We built up the list in reverse order. */
12837 return nreverse (attribute_list);
12840 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
12841 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
12842 current value of the PEDANTIC flag, regardless of whether or not
12843 the `__extension__' keyword is present. The caller is responsible
12844 for restoring the value of the PEDANTIC flag. */
12846 static bool
12847 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
12849 /* Save the old value of the PEDANTIC flag. */
12850 *saved_pedantic = pedantic;
12852 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
12854 /* Consume the `__extension__' token. */
12855 cp_lexer_consume_token (parser->lexer);
12856 /* We're not being pedantic while the `__extension__' keyword is
12857 in effect. */
12858 pedantic = 0;
12860 return true;
12863 return false;
12866 /* Parse a label declaration.
12868 label-declaration:
12869 __label__ label-declarator-seq ;
12871 label-declarator-seq:
12872 identifier , label-declarator-seq
12873 identifier */
12875 static void
12876 cp_parser_label_declaration (cp_parser* parser)
12878 /* Look for the `__label__' keyword. */
12879 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
12881 while (true)
12883 tree identifier;
12885 /* Look for an identifier. */
12886 identifier = cp_parser_identifier (parser);
12887 /* Declare it as a lobel. */
12888 finish_label_decl (identifier);
12889 /* If the next token is a `;', stop. */
12890 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12891 break;
12892 /* Look for the `,' separating the label declarations. */
12893 cp_parser_require (parser, CPP_COMMA, "`,'");
12896 /* Look for the final `;'. */
12897 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12900 /* Support Functions */
12902 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12903 NAME should have one of the representations used for an
12904 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12905 is returned. If PARSER->SCOPE is a dependent type, then a
12906 SCOPE_REF is returned.
12908 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
12909 returned; the name was already resolved when the TEMPLATE_ID_EXPR
12910 was formed. Abstractly, such entities should not be passed to this
12911 function, because they do not need to be looked up, but it is
12912 simpler to check for this special case here, rather than at the
12913 call-sites.
12915 In cases not explicitly covered above, this function returns a
12916 DECL, OVERLOAD, or baselink representing the result of the lookup.
12917 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
12918 is returned.
12920 If IS_TYPE is TRUE, bindings that do not refer to types are
12921 ignored.
12923 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
12924 are ignored.
12926 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
12927 types. */
12929 static tree
12930 cp_parser_lookup_name (cp_parser *parser, tree name,
12931 bool is_type, bool is_namespace, bool check_dependency)
12933 tree decl;
12934 tree object_type = parser->context->object_type;
12936 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
12937 no longer valid. Note that if we are parsing tentatively, and
12938 the parse fails, OBJECT_TYPE will be automatically restored. */
12939 parser->context->object_type = NULL_TREE;
12941 if (name == error_mark_node)
12942 return error_mark_node;
12944 /* A template-id has already been resolved; there is no lookup to
12945 do. */
12946 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
12947 return name;
12948 if (BASELINK_P (name))
12950 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
12951 == TEMPLATE_ID_EXPR),
12952 20020909);
12953 return name;
12956 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
12957 it should already have been checked to make sure that the name
12958 used matches the type being destroyed. */
12959 if (TREE_CODE (name) == BIT_NOT_EXPR)
12961 tree type;
12963 /* Figure out to which type this destructor applies. */
12964 if (parser->scope)
12965 type = parser->scope;
12966 else if (object_type)
12967 type = object_type;
12968 else
12969 type = current_class_type;
12970 /* If that's not a class type, there is no destructor. */
12971 if (!type || !CLASS_TYPE_P (type))
12972 return error_mark_node;
12973 /* If it was a class type, return the destructor. */
12974 return CLASSTYPE_DESTRUCTORS (type);
12977 /* By this point, the NAME should be an ordinary identifier. If
12978 the id-expression was a qualified name, the qualifying scope is
12979 stored in PARSER->SCOPE at this point. */
12980 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
12981 20000619);
12983 /* Perform the lookup. */
12984 if (parser->scope)
12986 bool dependent_p;
12988 if (parser->scope == error_mark_node)
12989 return error_mark_node;
12991 /* If the SCOPE is dependent, the lookup must be deferred until
12992 the template is instantiated -- unless we are explicitly
12993 looking up names in uninstantiated templates. Even then, we
12994 cannot look up the name if the scope is not a class type; it
12995 might, for example, be a template type parameter. */
12996 dependent_p = (TYPE_P (parser->scope)
12997 && !(parser->in_declarator_p
12998 && currently_open_class (parser->scope))
12999 && dependent_type_p (parser->scope));
13000 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13001 && dependent_p)
13003 if (!is_type)
13004 decl = build_nt (SCOPE_REF, parser->scope, name);
13005 else
13006 /* The resolution to Core Issue 180 says that `struct A::B'
13007 should be considered a type-name, even if `A' is
13008 dependent. */
13009 decl = TYPE_NAME (make_typename_type (parser->scope,
13010 name,
13011 /*complain=*/1));
13013 else
13015 /* If PARSER->SCOPE is a dependent type, then it must be a
13016 class type, and we must not be checking dependencies;
13017 otherwise, we would have processed this lookup above. So
13018 that PARSER->SCOPE is not considered a dependent base by
13019 lookup_member, we must enter the scope here. */
13020 if (dependent_p)
13021 push_scope (parser->scope);
13022 /* If the PARSER->SCOPE is a a template specialization, it
13023 may be instantiated during name lookup. In that case,
13024 errors may be issued. Even if we rollback the current
13025 tentative parse, those errors are valid. */
13026 decl = lookup_qualified_name (parser->scope, name, is_type,
13027 /*complain=*/true);
13028 if (dependent_p)
13029 pop_scope (parser->scope);
13031 parser->qualifying_scope = parser->scope;
13032 parser->object_scope = NULL_TREE;
13034 else if (object_type)
13036 tree object_decl = NULL_TREE;
13037 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13038 OBJECT_TYPE is not a class. */
13039 if (CLASS_TYPE_P (object_type))
13040 /* If the OBJECT_TYPE is a template specialization, it may
13041 be instantiated during name lookup. In that case, errors
13042 may be issued. Even if we rollback the current tentative
13043 parse, those errors are valid. */
13044 object_decl = lookup_member (object_type,
13045 name,
13046 /*protect=*/0, is_type);
13047 /* Look it up in the enclosing context, too. */
13048 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13049 is_namespace,
13050 /*flags=*/0);
13051 parser->object_scope = object_type;
13052 parser->qualifying_scope = NULL_TREE;
13053 if (object_decl)
13054 decl = object_decl;
13056 else
13058 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13059 is_namespace,
13060 /*flags=*/0);
13061 parser->qualifying_scope = NULL_TREE;
13062 parser->object_scope = NULL_TREE;
13065 /* If the lookup failed, let our caller know. */
13066 if (!decl
13067 || decl == error_mark_node
13068 || (TREE_CODE (decl) == FUNCTION_DECL
13069 && DECL_ANTICIPATED (decl)))
13070 return error_mark_node;
13072 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13073 if (TREE_CODE (decl) == TREE_LIST)
13075 /* The error message we have to print is too complicated for
13076 cp_parser_error, so we incorporate its actions directly. */
13077 if (!cp_parser_simulate_error (parser))
13079 error ("reference to `%D' is ambiguous", name);
13080 print_candidates (decl);
13082 return error_mark_node;
13085 my_friendly_assert (DECL_P (decl)
13086 || TREE_CODE (decl) == OVERLOAD
13087 || TREE_CODE (decl) == SCOPE_REF
13088 || BASELINK_P (decl),
13089 20000619);
13091 /* If we have resolved the name of a member declaration, check to
13092 see if the declaration is accessible. When the name resolves to
13093 set of overloaded functions, accessibility is checked when
13094 overload resolution is done.
13096 During an explicit instantiation, access is not checked at all,
13097 as per [temp.explicit]. */
13098 if (DECL_P (decl))
13099 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13101 return decl;
13104 /* Like cp_parser_lookup_name, but for use in the typical case where
13105 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13106 TRUE. */
13108 static tree
13109 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13111 return cp_parser_lookup_name (parser, name,
13112 /*is_type=*/false,
13113 /*is_namespace=*/false,
13114 /*check_dependency=*/true);
13117 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13118 the current context, return the TYPE_DECL. If TAG_NAME_P is
13119 true, the DECL indicates the class being defined in a class-head,
13120 or declared in an elaborated-type-specifier.
13122 Otherwise, return DECL. */
13124 static tree
13125 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13127 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13128 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13130 struct A {
13131 template <typename T> struct B;
13134 template <typename T> struct A::B {};
13136 Similarly, in a elaborated-type-specifier:
13138 namespace N { struct X{}; }
13140 struct A {
13141 template <typename T> friend struct N::X;
13144 However, if the DECL refers to a class type, and we are in
13145 the scope of the class, then the name lookup automatically
13146 finds the TYPE_DECL created by build_self_reference rather
13147 than a TEMPLATE_DECL. For example, in:
13149 template <class T> struct S {
13150 S s;
13153 there is no need to handle such case. */
13155 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13156 return DECL_TEMPLATE_RESULT (decl);
13158 return decl;
13161 /* If too many, or too few, template-parameter lists apply to the
13162 declarator, issue an error message. Returns TRUE if all went well,
13163 and FALSE otherwise. */
13165 static bool
13166 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13167 tree declarator)
13169 unsigned num_templates;
13171 /* We haven't seen any classes that involve template parameters yet. */
13172 num_templates = 0;
13174 switch (TREE_CODE (declarator))
13176 case CALL_EXPR:
13177 case ARRAY_REF:
13178 case INDIRECT_REF:
13179 case ADDR_EXPR:
13181 tree main_declarator = TREE_OPERAND (declarator, 0);
13182 return
13183 cp_parser_check_declarator_template_parameters (parser,
13184 main_declarator);
13187 case SCOPE_REF:
13189 tree scope;
13190 tree member;
13192 scope = TREE_OPERAND (declarator, 0);
13193 member = TREE_OPERAND (declarator, 1);
13195 /* If this is a pointer-to-member, then we are not interested
13196 in the SCOPE, because it does not qualify the thing that is
13197 being declared. */
13198 if (TREE_CODE (member) == INDIRECT_REF)
13199 return (cp_parser_check_declarator_template_parameters
13200 (parser, member));
13202 while (scope && CLASS_TYPE_P (scope))
13204 /* You're supposed to have one `template <...>'
13205 for every template class, but you don't need one
13206 for a full specialization. For example:
13208 template <class T> struct S{};
13209 template <> struct S<int> { void f(); };
13210 void S<int>::f () {}
13212 is correct; there shouldn't be a `template <>' for
13213 the definition of `S<int>::f'. */
13214 if (CLASSTYPE_TEMPLATE_INFO (scope)
13215 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13216 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13217 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13218 ++num_templates;
13220 scope = TYPE_CONTEXT (scope);
13224 /* Fall through. */
13226 default:
13227 /* If the DECLARATOR has the form `X<y>' then it uses one
13228 additional level of template parameters. */
13229 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13230 ++num_templates;
13232 return cp_parser_check_template_parameters (parser,
13233 num_templates);
13237 /* NUM_TEMPLATES were used in the current declaration. If that is
13238 invalid, return FALSE and issue an error messages. Otherwise,
13239 return TRUE. */
13241 static bool
13242 cp_parser_check_template_parameters (cp_parser* parser,
13243 unsigned num_templates)
13245 /* If there are more template classes than parameter lists, we have
13246 something like:
13248 template <class T> void S<T>::R<T>::f (); */
13249 if (parser->num_template_parameter_lists < num_templates)
13251 error ("too few template-parameter-lists");
13252 return false;
13254 /* If there are the same number of template classes and parameter
13255 lists, that's OK. */
13256 if (parser->num_template_parameter_lists == num_templates)
13257 return true;
13258 /* If there are more, but only one more, then we are referring to a
13259 member template. That's OK too. */
13260 if (parser->num_template_parameter_lists == num_templates + 1)
13261 return true;
13262 /* Otherwise, there are too many template parameter lists. We have
13263 something like:
13265 template <class T> template <class U> void S::f(); */
13266 error ("too many template-parameter-lists");
13267 return false;
13270 /* Parse a binary-expression of the general form:
13272 binary-expression:
13273 <expr>
13274 binary-expression <token> <expr>
13276 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13277 to parser the <expr>s. If the first production is used, then the
13278 value returned by FN is returned directly. Otherwise, a node with
13279 the indicated EXPR_TYPE is returned, with operands corresponding to
13280 the two sub-expressions. */
13282 static tree
13283 cp_parser_binary_expression (cp_parser* parser,
13284 const cp_parser_token_tree_map token_tree_map,
13285 cp_parser_expression_fn fn)
13287 tree lhs;
13289 /* Parse the first expression. */
13290 lhs = (*fn) (parser);
13291 /* Now, look for more expressions. */
13292 while (true)
13294 cp_token *token;
13295 const cp_parser_token_tree_map_node *map_node;
13296 tree rhs;
13298 /* Peek at the next token. */
13299 token = cp_lexer_peek_token (parser->lexer);
13300 /* If the token is `>', and that's not an operator at the
13301 moment, then we're done. */
13302 if (token->type == CPP_GREATER
13303 && !parser->greater_than_is_operator_p)
13304 break;
13305 /* If we find one of the tokens we want, build the corresponding
13306 tree representation. */
13307 for (map_node = token_tree_map;
13308 map_node->token_type != CPP_EOF;
13309 ++map_node)
13310 if (map_node->token_type == token->type)
13312 /* Consume the operator token. */
13313 cp_lexer_consume_token (parser->lexer);
13314 /* Parse the right-hand side of the expression. */
13315 rhs = (*fn) (parser);
13316 /* Build the binary tree node. */
13317 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13318 break;
13321 /* If the token wasn't one of the ones we want, we're done. */
13322 if (map_node->token_type == CPP_EOF)
13323 break;
13326 return lhs;
13329 /* Parse an optional `::' token indicating that the following name is
13330 from the global namespace. If so, PARSER->SCOPE is set to the
13331 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13332 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13333 Returns the new value of PARSER->SCOPE, if the `::' token is
13334 present, and NULL_TREE otherwise. */
13336 static tree
13337 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13339 cp_token *token;
13341 /* Peek at the next token. */
13342 token = cp_lexer_peek_token (parser->lexer);
13343 /* If we're looking at a `::' token then we're starting from the
13344 global namespace, not our current location. */
13345 if (token->type == CPP_SCOPE)
13347 /* Consume the `::' token. */
13348 cp_lexer_consume_token (parser->lexer);
13349 /* Set the SCOPE so that we know where to start the lookup. */
13350 parser->scope = global_namespace;
13351 parser->qualifying_scope = global_namespace;
13352 parser->object_scope = NULL_TREE;
13354 return parser->scope;
13356 else if (!current_scope_valid_p)
13358 parser->scope = NULL_TREE;
13359 parser->qualifying_scope = NULL_TREE;
13360 parser->object_scope = NULL_TREE;
13363 return NULL_TREE;
13366 /* Returns TRUE if the upcoming token sequence is the start of a
13367 constructor declarator. If FRIEND_P is true, the declarator is
13368 preceded by the `friend' specifier. */
13370 static bool
13371 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13373 bool constructor_p;
13374 tree type_decl = NULL_TREE;
13375 bool nested_name_p;
13376 cp_token *next_token;
13378 /* The common case is that this is not a constructor declarator, so
13379 try to avoid doing lots of work if at all possible. It's not
13380 valid declare a constructor at function scope. */
13381 if (at_function_scope_p ())
13382 return false;
13383 /* And only certain tokens can begin a constructor declarator. */
13384 next_token = cp_lexer_peek_token (parser->lexer);
13385 if (next_token->type != CPP_NAME
13386 && next_token->type != CPP_SCOPE
13387 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13388 && next_token->type != CPP_TEMPLATE_ID)
13389 return false;
13391 /* Parse tentatively; we are going to roll back all of the tokens
13392 consumed here. */
13393 cp_parser_parse_tentatively (parser);
13394 /* Assume that we are looking at a constructor declarator. */
13395 constructor_p = true;
13397 /* Look for the optional `::' operator. */
13398 cp_parser_global_scope_opt (parser,
13399 /*current_scope_valid_p=*/false);
13400 /* Look for the nested-name-specifier. */
13401 nested_name_p
13402 = (cp_parser_nested_name_specifier_opt (parser,
13403 /*typename_keyword_p=*/false,
13404 /*check_dependency_p=*/false,
13405 /*type_p=*/false)
13406 != NULL_TREE);
13407 /* Outside of a class-specifier, there must be a
13408 nested-name-specifier. */
13409 if (!nested_name_p &&
13410 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13411 || friend_p))
13412 constructor_p = false;
13413 /* If we still think that this might be a constructor-declarator,
13414 look for a class-name. */
13415 if (constructor_p)
13417 /* If we have:
13419 template <typename T> struct S { S(); };
13420 template <typename T> S<T>::S ();
13422 we must recognize that the nested `S' names a class.
13423 Similarly, for:
13425 template <typename T> S<T>::S<T> ();
13427 we must recognize that the nested `S' names a template. */
13428 type_decl = cp_parser_class_name (parser,
13429 /*typename_keyword_p=*/false,
13430 /*template_keyword_p=*/false,
13431 /*type_p=*/false,
13432 /*check_dependency_p=*/false,
13433 /*class_head_p=*/false);
13434 /* If there was no class-name, then this is not a constructor. */
13435 constructor_p = !cp_parser_error_occurred (parser);
13438 /* If we're still considering a constructor, we have to see a `(',
13439 to begin the parameter-declaration-clause, followed by either a
13440 `)', an `...', or a decl-specifier. We need to check for a
13441 type-specifier to avoid being fooled into thinking that:
13443 S::S (f) (int);
13445 is a constructor. (It is actually a function named `f' that
13446 takes one parameter (of type `int') and returns a value of type
13447 `S::S'. */
13448 if (constructor_p
13449 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13452 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13453 && !cp_parser_storage_class_specifier_opt (parser))
13455 tree type;
13456 unsigned saved_num_template_parameter_lists;
13458 /* Names appearing in the type-specifier should be looked up
13459 in the scope of the class. */
13460 if (current_class_type)
13461 type = NULL_TREE;
13462 else
13464 type = TREE_TYPE (type_decl);
13465 if (TREE_CODE (type) == TYPENAME_TYPE)
13467 type = resolve_typename_type (type,
13468 /*only_current_p=*/false);
13469 if (type == error_mark_node)
13471 cp_parser_abort_tentative_parse (parser);
13472 return false;
13475 push_scope (type);
13478 /* Inside the constructor parameter list, surrounding
13479 template-parameter-lists do not apply. */
13480 saved_num_template_parameter_lists
13481 = parser->num_template_parameter_lists;
13482 parser->num_template_parameter_lists = 0;
13484 /* Look for the type-specifier. */
13485 cp_parser_type_specifier (parser,
13486 CP_PARSER_FLAGS_NONE,
13487 /*is_friend=*/false,
13488 /*is_declarator=*/true,
13489 /*declares_class_or_enum=*/NULL,
13490 /*is_cv_qualifier=*/NULL);
13492 parser->num_template_parameter_lists
13493 = saved_num_template_parameter_lists;
13495 /* Leave the scope of the class. */
13496 if (type)
13497 pop_scope (type);
13499 constructor_p = !cp_parser_error_occurred (parser);
13502 else
13503 constructor_p = false;
13504 /* We did not really want to consume any tokens. */
13505 cp_parser_abort_tentative_parse (parser);
13507 return constructor_p;
13510 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13511 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13512 they must be performed once we are in the scope of the function.
13514 Returns the function defined. */
13516 static tree
13517 cp_parser_function_definition_from_specifiers_and_declarator
13518 (cp_parser* parser,
13519 tree decl_specifiers,
13520 tree attributes,
13521 tree declarator)
13523 tree fn;
13524 bool success_p;
13526 /* Begin the function-definition. */
13527 success_p = begin_function_definition (decl_specifiers,
13528 attributes,
13529 declarator);
13531 /* If there were names looked up in the decl-specifier-seq that we
13532 did not check, check them now. We must wait until we are in the
13533 scope of the function to perform the checks, since the function
13534 might be a friend. */
13535 perform_deferred_access_checks ();
13537 if (!success_p)
13539 /* If begin_function_definition didn't like the definition, skip
13540 the entire function. */
13541 error ("invalid function declaration");
13542 cp_parser_skip_to_end_of_block_or_statement (parser);
13543 fn = error_mark_node;
13545 else
13546 fn = cp_parser_function_definition_after_declarator (parser,
13547 /*inline_p=*/false);
13549 return fn;
13552 /* Parse the part of a function-definition that follows the
13553 declarator. INLINE_P is TRUE iff this function is an inline
13554 function defined with a class-specifier.
13556 Returns the function defined. */
13558 static tree
13559 cp_parser_function_definition_after_declarator (cp_parser* parser,
13560 bool inline_p)
13562 tree fn;
13563 bool ctor_initializer_p = false;
13564 bool saved_in_unbraced_linkage_specification_p;
13565 unsigned saved_num_template_parameter_lists;
13567 /* If the next token is `return', then the code may be trying to
13568 make use of the "named return value" extension that G++ used to
13569 support. */
13570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13572 /* Consume the `return' keyword. */
13573 cp_lexer_consume_token (parser->lexer);
13574 /* Look for the identifier that indicates what value is to be
13575 returned. */
13576 cp_parser_identifier (parser);
13577 /* Issue an error message. */
13578 error ("named return values are no longer supported");
13579 /* Skip tokens until we reach the start of the function body. */
13580 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13581 cp_lexer_consume_token (parser->lexer);
13583 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13584 anything declared inside `f'. */
13585 saved_in_unbraced_linkage_specification_p
13586 = parser->in_unbraced_linkage_specification_p;
13587 parser->in_unbraced_linkage_specification_p = false;
13588 /* Inside the function, surrounding template-parameter-lists do not
13589 apply. */
13590 saved_num_template_parameter_lists
13591 = parser->num_template_parameter_lists;
13592 parser->num_template_parameter_lists = 0;
13593 /* If the next token is `try', then we are looking at a
13594 function-try-block. */
13595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13596 ctor_initializer_p = cp_parser_function_try_block (parser);
13597 /* A function-try-block includes the function-body, so we only do
13598 this next part if we're not processing a function-try-block. */
13599 else
13600 ctor_initializer_p
13601 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13603 /* Finish the function. */
13604 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13605 (inline_p ? 2 : 0));
13606 /* Generate code for it, if necessary. */
13607 expand_or_defer_fn (fn);
13608 /* Restore the saved values. */
13609 parser->in_unbraced_linkage_specification_p
13610 = saved_in_unbraced_linkage_specification_p;
13611 parser->num_template_parameter_lists
13612 = saved_num_template_parameter_lists;
13614 return fn;
13617 /* Parse a template-declaration, assuming that the `export' (and
13618 `extern') keywords, if present, has already been scanned. MEMBER_P
13619 is as for cp_parser_template_declaration. */
13621 static void
13622 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13624 tree decl = NULL_TREE;
13625 tree parameter_list;
13626 bool friend_p = false;
13628 /* Look for the `template' keyword. */
13629 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13630 return;
13632 /* And the `<'. */
13633 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13634 return;
13636 /* Parse the template parameters. */
13637 begin_template_parm_list ();
13638 /* If the next token is `>', then we have an invalid
13639 specialization. Rather than complain about an invalid template
13640 parameter, issue an error message here. */
13641 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13643 cp_parser_error (parser, "invalid explicit specialization");
13644 parameter_list = NULL_TREE;
13646 else
13647 parameter_list = cp_parser_template_parameter_list (parser);
13648 parameter_list = end_template_parm_list (parameter_list);
13649 /* Look for the `>'. */
13650 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13651 /* We just processed one more parameter list. */
13652 ++parser->num_template_parameter_lists;
13653 /* If the next token is `template', there are more template
13654 parameters. */
13655 if (cp_lexer_next_token_is_keyword (parser->lexer,
13656 RID_TEMPLATE))
13657 cp_parser_template_declaration_after_export (parser, member_p);
13658 else
13660 decl = cp_parser_single_declaration (parser,
13661 member_p,
13662 &friend_p);
13664 /* If this is a member template declaration, let the front
13665 end know. */
13666 if (member_p && !friend_p && decl)
13668 if (TREE_CODE (decl) == TYPE_DECL)
13669 cp_parser_check_access_in_redeclaration (decl);
13671 decl = finish_member_template_decl (decl);
13673 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13674 make_friend_class (current_class_type, TREE_TYPE (decl),
13675 /*complain=*/true);
13677 /* We are done with the current parameter list. */
13678 --parser->num_template_parameter_lists;
13680 /* Finish up. */
13681 finish_template_decl (parameter_list);
13683 /* Register member declarations. */
13684 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13685 finish_member_declaration (decl);
13687 /* If DECL is a function template, we must return to parse it later.
13688 (Even though there is no definition, there might be default
13689 arguments that need handling.) */
13690 if (member_p && decl
13691 && (TREE_CODE (decl) == FUNCTION_DECL
13692 || DECL_FUNCTION_TEMPLATE_P (decl)))
13693 TREE_VALUE (parser->unparsed_functions_queues)
13694 = tree_cons (NULL_TREE, decl,
13695 TREE_VALUE (parser->unparsed_functions_queues));
13698 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13699 `function-definition' sequence. MEMBER_P is true, this declaration
13700 appears in a class scope.
13702 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13703 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13705 static tree
13706 cp_parser_single_declaration (cp_parser* parser,
13707 bool member_p,
13708 bool* friend_p)
13710 int declares_class_or_enum;
13711 tree decl = NULL_TREE;
13712 tree decl_specifiers;
13713 tree attributes;
13715 /* Parse the dependent declaration. We don't know yet
13716 whether it will be a function-definition. */
13717 cp_parser_parse_tentatively (parser);
13718 /* Defer access checks until we know what is being declared. */
13719 push_deferring_access_checks (dk_deferred);
13721 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13722 alternative. */
13723 decl_specifiers
13724 = cp_parser_decl_specifier_seq (parser,
13725 CP_PARSER_FLAGS_OPTIONAL,
13726 &attributes,
13727 &declares_class_or_enum);
13728 /* Gather up the access checks that occurred the
13729 decl-specifier-seq. */
13730 stop_deferring_access_checks ();
13732 /* Check for the declaration of a template class. */
13733 if (declares_class_or_enum)
13735 if (cp_parser_declares_only_class_p (parser))
13737 decl = shadow_tag (decl_specifiers);
13738 if (decl)
13739 decl = TYPE_NAME (decl);
13740 else
13741 decl = error_mark_node;
13744 else
13745 decl = NULL_TREE;
13746 /* If it's not a template class, try for a template function. If
13747 the next token is a `;', then this declaration does not declare
13748 anything. But, if there were errors in the decl-specifiers, then
13749 the error might well have come from an attempted class-specifier.
13750 In that case, there's no need to warn about a missing declarator. */
13751 if (!decl
13752 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13753 || !value_member (error_mark_node, decl_specifiers)))
13754 decl = cp_parser_init_declarator (parser,
13755 decl_specifiers,
13756 attributes,
13757 /*function_definition_allowed_p=*/false,
13758 member_p,
13759 declares_class_or_enum,
13760 /*function_definition_p=*/NULL);
13762 pop_deferring_access_checks ();
13764 /* Clear any current qualification; whatever comes next is the start
13765 of something new. */
13766 parser->scope = NULL_TREE;
13767 parser->qualifying_scope = NULL_TREE;
13768 parser->object_scope = NULL_TREE;
13769 /* Look for a trailing `;' after the declaration. */
13770 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
13771 && cp_parser_committed_to_tentative_parse (parser))
13772 cp_parser_skip_to_end_of_block_or_statement (parser);
13773 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13774 if (cp_parser_parse_definitely (parser))
13776 if (friend_p)
13777 *friend_p = cp_parser_friend_p (decl_specifiers);
13779 /* Otherwise, try a function-definition. */
13780 else
13781 decl = cp_parser_function_definition (parser, friend_p);
13783 return decl;
13786 /* Parse a cast-expression that is not the operand of a unary "&". */
13788 static tree
13789 cp_parser_simple_cast_expression (cp_parser *parser)
13791 return cp_parser_cast_expression (parser, /*address_p=*/false);
13794 /* Parse a functional cast to TYPE. Returns an expression
13795 representing the cast. */
13797 static tree
13798 cp_parser_functional_cast (cp_parser* parser, tree type)
13800 tree expression_list;
13802 expression_list
13803 = cp_parser_parenthesized_expression_list (parser, false,
13804 /*non_constant_p=*/NULL);
13806 return build_functional_cast (type, expression_list);
13809 /* MEMBER_FUNCTION is a member function, or a friend. If default
13810 arguments, or the body of the function have not yet been parsed,
13811 parse them now. */
13813 static void
13814 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
13816 cp_lexer *saved_lexer;
13818 /* If this member is a template, get the underlying
13819 FUNCTION_DECL. */
13820 if (DECL_FUNCTION_TEMPLATE_P (member_function))
13821 member_function = DECL_TEMPLATE_RESULT (member_function);
13823 /* There should not be any class definitions in progress at this
13824 point; the bodies of members are only parsed outside of all class
13825 definitions. */
13826 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
13827 /* While we're parsing the member functions we might encounter more
13828 classes. We want to handle them right away, but we don't want
13829 them getting mixed up with functions that are currently in the
13830 queue. */
13831 parser->unparsed_functions_queues
13832 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13834 /* Make sure that any template parameters are in scope. */
13835 maybe_begin_member_template_processing (member_function);
13837 /* If the body of the function has not yet been parsed, parse it
13838 now. */
13839 if (DECL_PENDING_INLINE_P (member_function))
13841 tree function_scope;
13842 cp_token_cache *tokens;
13844 /* The function is no longer pending; we are processing it. */
13845 tokens = DECL_PENDING_INLINE_INFO (member_function);
13846 DECL_PENDING_INLINE_INFO (member_function) = NULL;
13847 DECL_PENDING_INLINE_P (member_function) = 0;
13848 /* If this was an inline function in a local class, enter the scope
13849 of the containing function. */
13850 function_scope = decl_function_context (member_function);
13851 if (function_scope)
13852 push_function_context_to (function_scope);
13854 /* Save away the current lexer. */
13855 saved_lexer = parser->lexer;
13856 /* Make a new lexer to feed us the tokens saved for this function. */
13857 parser->lexer = cp_lexer_new_from_tokens (tokens);
13858 parser->lexer->next = saved_lexer;
13860 /* Set the current source position to be the location of the first
13861 token in the saved inline body. */
13862 cp_lexer_peek_token (parser->lexer);
13864 /* Let the front end know that we going to be defining this
13865 function. */
13866 start_function (NULL_TREE, member_function, NULL_TREE,
13867 SF_PRE_PARSED | SF_INCLASS_INLINE);
13869 /* Now, parse the body of the function. */
13870 cp_parser_function_definition_after_declarator (parser,
13871 /*inline_p=*/true);
13873 /* Leave the scope of the containing function. */
13874 if (function_scope)
13875 pop_function_context_from (function_scope);
13876 /* Restore the lexer. */
13877 parser->lexer = saved_lexer;
13880 /* Remove any template parameters from the symbol table. */
13881 maybe_end_member_template_processing ();
13883 /* Restore the queue. */
13884 parser->unparsed_functions_queues
13885 = TREE_CHAIN (parser->unparsed_functions_queues);
13888 /* If DECL contains any default args, remeber it on the unparsed
13889 functions queue. */
13891 static void
13892 cp_parser_save_default_args (cp_parser* parser, tree decl)
13894 tree probe;
13896 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
13897 probe;
13898 probe = TREE_CHAIN (probe))
13899 if (TREE_PURPOSE (probe))
13901 TREE_PURPOSE (parser->unparsed_functions_queues)
13902 = tree_cons (NULL_TREE, decl,
13903 TREE_PURPOSE (parser->unparsed_functions_queues));
13904 break;
13906 return;
13909 /* FN is a FUNCTION_DECL which may contains a parameter with an
13910 unparsed DEFAULT_ARG. Parse the default args now. */
13912 static void
13913 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
13915 cp_lexer *saved_lexer;
13916 cp_token_cache *tokens;
13917 bool saved_local_variables_forbidden_p;
13918 tree parameters;
13920 /* While we're parsing the default args, we might (due to the
13921 statement expression extension) encounter more classes. We want
13922 to handle them right away, but we don't want them getting mixed
13923 up with default args that are currently in the queue. */
13924 parser->unparsed_functions_queues
13925 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13927 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
13928 parameters;
13929 parameters = TREE_CHAIN (parameters))
13931 if (!TREE_PURPOSE (parameters)
13932 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
13933 continue;
13935 /* Save away the current lexer. */
13936 saved_lexer = parser->lexer;
13937 /* Create a new one, using the tokens we have saved. */
13938 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
13939 parser->lexer = cp_lexer_new_from_tokens (tokens);
13941 /* Set the current source position to be the location of the
13942 first token in the default argument. */
13943 cp_lexer_peek_token (parser->lexer);
13945 /* Local variable names (and the `this' keyword) may not appear
13946 in a default argument. */
13947 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13948 parser->local_variables_forbidden_p = true;
13949 /* Parse the assignment-expression. */
13950 if (DECL_CONTEXT (fn))
13951 push_nested_class (DECL_CONTEXT (fn));
13952 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
13953 if (DECL_CONTEXT (fn))
13954 pop_nested_class ();
13956 /* Restore saved state. */
13957 parser->lexer = saved_lexer;
13958 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13961 /* Restore the queue. */
13962 parser->unparsed_functions_queues
13963 = TREE_CHAIN (parser->unparsed_functions_queues);
13966 /* Parse the operand of `sizeof' (or a similar operator). Returns
13967 either a TYPE or an expression, depending on the form of the
13968 input. The KEYWORD indicates which kind of expression we have
13969 encountered. */
13971 static tree
13972 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
13974 static const char *format;
13975 tree expr = NULL_TREE;
13976 const char *saved_message;
13977 bool saved_constant_expression_p;
13979 /* Initialize FORMAT the first time we get here. */
13980 if (!format)
13981 format = "types may not be defined in `%s' expressions";
13983 /* Types cannot be defined in a `sizeof' expression. Save away the
13984 old message. */
13985 saved_message = parser->type_definition_forbidden_message;
13986 /* And create the new one. */
13987 parser->type_definition_forbidden_message
13988 = xmalloc (strlen (format)
13989 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
13990 + 1 /* `\0' */);
13991 sprintf ((char *) parser->type_definition_forbidden_message,
13992 format, IDENTIFIER_POINTER (ridpointers[keyword]));
13994 /* The restrictions on constant-expressions do not apply inside
13995 sizeof expressions. */
13996 saved_constant_expression_p = parser->constant_expression_p;
13997 parser->constant_expression_p = false;
13999 /* Do not actually evaluate the expression. */
14000 ++skip_evaluation;
14001 /* If it's a `(', then we might be looking at the type-id
14002 construction. */
14003 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14005 tree type;
14007 /* We can't be sure yet whether we're looking at a type-id or an
14008 expression. */
14009 cp_parser_parse_tentatively (parser);
14010 /* Consume the `('. */
14011 cp_lexer_consume_token (parser->lexer);
14012 /* Parse the type-id. */
14013 type = cp_parser_type_id (parser);
14014 /* Now, look for the trailing `)'. */
14015 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14016 /* If all went well, then we're done. */
14017 if (cp_parser_parse_definitely (parser))
14019 /* Build a list of decl-specifiers; right now, we have only
14020 a single type-specifier. */
14021 type = build_tree_list (NULL_TREE,
14022 type);
14024 /* Call grokdeclarator to figure out what type this is. */
14025 expr = grokdeclarator (NULL_TREE,
14026 type,
14027 TYPENAME,
14028 /*initialized=*/0,
14029 /*attrlist=*/NULL);
14033 /* If the type-id production did not work out, then we must be
14034 looking at the unary-expression production. */
14035 if (!expr)
14036 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14037 /* Go back to evaluating expressions. */
14038 --skip_evaluation;
14040 /* Free the message we created. */
14041 free ((char *) parser->type_definition_forbidden_message);
14042 /* And restore the old one. */
14043 parser->type_definition_forbidden_message = saved_message;
14044 parser->constant_expression_p = saved_constant_expression_p;
14046 return expr;
14049 /* If the current declaration has no declarator, return true. */
14051 static bool
14052 cp_parser_declares_only_class_p (cp_parser *parser)
14054 /* If the next token is a `;' or a `,' then there is no
14055 declarator. */
14056 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14057 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14060 /* Simplify EXPR if it is a non-dependent expression. Returns the
14061 (possibly simplified) expression. */
14063 static tree
14064 cp_parser_fold_non_dependent_expr (tree expr)
14066 /* If we're in a template, but EXPR isn't value dependent, simplify
14067 it. We're supposed to treat:
14069 template <typename T> void f(T[1 + 1]);
14070 template <typename T> void f(T[2]);
14072 as two declarations of the same function, for example. */
14073 if (processing_template_decl
14074 && !type_dependent_expression_p (expr)
14075 && !value_dependent_expression_p (expr))
14077 HOST_WIDE_INT saved_processing_template_decl;
14079 saved_processing_template_decl = processing_template_decl;
14080 processing_template_decl = 0;
14081 expr = tsubst_copy_and_build (expr,
14082 /*args=*/NULL_TREE,
14083 tf_error,
14084 /*in_decl=*/NULL_TREE,
14085 /*function_p=*/false);
14086 processing_template_decl = saved_processing_template_decl;
14088 return expr;
14091 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14092 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14094 static bool
14095 cp_parser_friend_p (tree decl_specifiers)
14097 while (decl_specifiers)
14099 /* See if this decl-specifier is `friend'. */
14100 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14101 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14102 return true;
14104 /* Go on to the next decl-specifier. */
14105 decl_specifiers = TREE_CHAIN (decl_specifiers);
14108 return false;
14111 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14112 issue an error message indicating that TOKEN_DESC was expected.
14114 Returns the token consumed, if the token had the appropriate type.
14115 Otherwise, returns NULL. */
14117 static cp_token *
14118 cp_parser_require (cp_parser* parser,
14119 enum cpp_ttype type,
14120 const char* token_desc)
14122 if (cp_lexer_next_token_is (parser->lexer, type))
14123 return cp_lexer_consume_token (parser->lexer);
14124 else
14126 /* Output the MESSAGE -- unless we're parsing tentatively. */
14127 if (!cp_parser_simulate_error (parser))
14128 error ("expected %s", token_desc);
14129 return NULL;
14133 /* Like cp_parser_require, except that tokens will be skipped until
14134 the desired token is found. An error message is still produced if
14135 the next token is not as expected. */
14137 static void
14138 cp_parser_skip_until_found (cp_parser* parser,
14139 enum cpp_ttype type,
14140 const char* token_desc)
14142 cp_token *token;
14143 unsigned nesting_depth = 0;
14145 if (cp_parser_require (parser, type, token_desc))
14146 return;
14148 /* Skip tokens until the desired token is found. */
14149 while (true)
14151 /* Peek at the next token. */
14152 token = cp_lexer_peek_token (parser->lexer);
14153 /* If we've reached the token we want, consume it and
14154 stop. */
14155 if (token->type == type && !nesting_depth)
14157 cp_lexer_consume_token (parser->lexer);
14158 return;
14160 /* If we've run out of tokens, stop. */
14161 if (token->type == CPP_EOF)
14162 return;
14163 if (token->type == CPP_OPEN_BRACE
14164 || token->type == CPP_OPEN_PAREN
14165 || token->type == CPP_OPEN_SQUARE)
14166 ++nesting_depth;
14167 else if (token->type == CPP_CLOSE_BRACE
14168 || token->type == CPP_CLOSE_PAREN
14169 || token->type == CPP_CLOSE_SQUARE)
14171 if (nesting_depth-- == 0)
14172 return;
14174 /* Consume this token. */
14175 cp_lexer_consume_token (parser->lexer);
14179 /* If the next token is the indicated keyword, consume it. Otherwise,
14180 issue an error message indicating that TOKEN_DESC was expected.
14182 Returns the token consumed, if the token had the appropriate type.
14183 Otherwise, returns NULL. */
14185 static cp_token *
14186 cp_parser_require_keyword (cp_parser* parser,
14187 enum rid keyword,
14188 const char* token_desc)
14190 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14192 if (token && token->keyword != keyword)
14194 dyn_string_t error_msg;
14196 /* Format the error message. */
14197 error_msg = dyn_string_new (0);
14198 dyn_string_append_cstr (error_msg, "expected ");
14199 dyn_string_append_cstr (error_msg, token_desc);
14200 cp_parser_error (parser, error_msg->s);
14201 dyn_string_delete (error_msg);
14202 return NULL;
14205 return token;
14208 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14209 function-definition. */
14211 static bool
14212 cp_parser_token_starts_function_definition_p (cp_token* token)
14214 return (/* An ordinary function-body begins with an `{'. */
14215 token->type == CPP_OPEN_BRACE
14216 /* A ctor-initializer begins with a `:'. */
14217 || token->type == CPP_COLON
14218 /* A function-try-block begins with `try'. */
14219 || token->keyword == RID_TRY
14220 /* The named return value extension begins with `return'. */
14221 || token->keyword == RID_RETURN);
14224 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14225 definition. */
14227 static bool
14228 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14230 cp_token *token;
14232 token = cp_lexer_peek_token (parser->lexer);
14233 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14236 /* Returns TRUE iff the next token is the "," or ">" ending a
14237 template-argument. */
14239 static bool
14240 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14242 cp_token *token;
14244 token = cp_lexer_peek_token (parser->lexer);
14245 return (token->type == CPP_COMMA || token->type == CPP_GREATER);
14248 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14249 or none_type otherwise. */
14251 static enum tag_types
14252 cp_parser_token_is_class_key (cp_token* token)
14254 switch (token->keyword)
14256 case RID_CLASS:
14257 return class_type;
14258 case RID_STRUCT:
14259 return record_type;
14260 case RID_UNION:
14261 return union_type;
14263 default:
14264 return none_type;
14268 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14270 static void
14271 cp_parser_check_class_key (enum tag_types class_key, tree type)
14273 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14274 pedwarn ("`%s' tag used in naming `%#T'",
14275 class_key == union_type ? "union"
14276 : class_key == record_type ? "struct" : "class",
14277 type);
14280 /* Issue an error message if DECL is redeclared with differnt
14281 access than its original declaration [class.access.spec/3].
14282 This applies to nested classes and nested class templates.
14283 [class.mem/1]. */
14285 static void cp_parser_check_access_in_redeclaration (tree decl)
14287 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
14288 return;
14290 if ((TREE_PRIVATE (decl)
14291 != (current_access_specifier == access_private_node))
14292 || (TREE_PROTECTED (decl)
14293 != (current_access_specifier == access_protected_node)))
14294 error ("%D redeclared with different access", decl);
14297 /* Look for the `template' keyword, as a syntactic disambiguator.
14298 Return TRUE iff it is present, in which case it will be
14299 consumed. */
14301 static bool
14302 cp_parser_optional_template_keyword (cp_parser *parser)
14304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14306 /* The `template' keyword can only be used within templates;
14307 outside templates the parser can always figure out what is a
14308 template and what is not. */
14309 if (!processing_template_decl)
14311 error ("`template' (as a disambiguator) is only allowed "
14312 "within templates");
14313 /* If this part of the token stream is rescanned, the same
14314 error message would be generated. So, we purge the token
14315 from the stream. */
14316 cp_lexer_purge_token (parser->lexer);
14317 return false;
14319 else
14321 /* Consume the `template' keyword. */
14322 cp_lexer_consume_token (parser->lexer);
14323 return true;
14327 return false;
14330 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14331 set PARSER->SCOPE, and perform other related actions. */
14333 static void
14334 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14336 tree value;
14337 tree check;
14339 /* Get the stored value. */
14340 value = cp_lexer_consume_token (parser->lexer)->value;
14341 /* Perform any access checks that were deferred. */
14342 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14343 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14344 /* Set the scope from the stored value. */
14345 parser->scope = TREE_VALUE (value);
14346 parser->qualifying_scope = TREE_TYPE (value);
14347 parser->object_scope = NULL_TREE;
14350 /* Add tokens to CACHE until an non-nested END token appears. */
14352 static void
14353 cp_parser_cache_group (cp_parser *parser,
14354 cp_token_cache *cache,
14355 enum cpp_ttype end,
14356 unsigned depth)
14358 while (true)
14360 cp_token *token;
14362 /* Abort a parenthesized expression if we encounter a brace. */
14363 if ((end == CPP_CLOSE_PAREN || depth == 0)
14364 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14365 return;
14366 /* Consume the next token. */
14367 token = cp_lexer_consume_token (parser->lexer);
14368 /* If we've reached the end of the file, stop. */
14369 if (token->type == CPP_EOF)
14370 return;
14371 /* Add this token to the tokens we are saving. */
14372 cp_token_cache_push_token (cache, token);
14373 /* See if it starts a new group. */
14374 if (token->type == CPP_OPEN_BRACE)
14376 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14377 if (depth == 0)
14378 return;
14380 else if (token->type == CPP_OPEN_PAREN)
14381 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14382 else if (token->type == end)
14383 return;
14387 /* Begin parsing tentatively. We always save tokens while parsing
14388 tentatively so that if the tentative parsing fails we can restore the
14389 tokens. */
14391 static void
14392 cp_parser_parse_tentatively (cp_parser* parser)
14394 /* Enter a new parsing context. */
14395 parser->context = cp_parser_context_new (parser->context);
14396 /* Begin saving tokens. */
14397 cp_lexer_save_tokens (parser->lexer);
14398 /* In order to avoid repetitive access control error messages,
14399 access checks are queued up until we are no longer parsing
14400 tentatively. */
14401 push_deferring_access_checks (dk_deferred);
14404 /* Commit to the currently active tentative parse. */
14406 static void
14407 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14409 cp_parser_context *context;
14410 cp_lexer *lexer;
14412 /* Mark all of the levels as committed. */
14413 lexer = parser->lexer;
14414 for (context = parser->context; context->next; context = context->next)
14416 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14417 break;
14418 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14419 while (!cp_lexer_saving_tokens (lexer))
14420 lexer = lexer->next;
14421 cp_lexer_commit_tokens (lexer);
14425 /* Abort the currently active tentative parse. All consumed tokens
14426 will be rolled back, and no diagnostics will be issued. */
14428 static void
14429 cp_parser_abort_tentative_parse (cp_parser* parser)
14431 cp_parser_simulate_error (parser);
14432 /* Now, pretend that we want to see if the construct was
14433 successfully parsed. */
14434 cp_parser_parse_definitely (parser);
14437 /* Stop parsing tentatively. If a parse error has occurred, restore the
14438 token stream. Otherwise, commit to the tokens we have consumed.
14439 Returns true if no error occurred; false otherwise. */
14441 static bool
14442 cp_parser_parse_definitely (cp_parser* parser)
14444 bool error_occurred;
14445 cp_parser_context *context;
14447 /* Remember whether or not an error occurred, since we are about to
14448 destroy that information. */
14449 error_occurred = cp_parser_error_occurred (parser);
14450 /* Remove the topmost context from the stack. */
14451 context = parser->context;
14452 parser->context = context->next;
14453 /* If no parse errors occurred, commit to the tentative parse. */
14454 if (!error_occurred)
14456 /* Commit to the tokens read tentatively, unless that was
14457 already done. */
14458 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14459 cp_lexer_commit_tokens (parser->lexer);
14461 pop_to_parent_deferring_access_checks ();
14463 /* Otherwise, if errors occurred, roll back our state so that things
14464 are just as they were before we began the tentative parse. */
14465 else
14467 cp_lexer_rollback_tokens (parser->lexer);
14468 pop_deferring_access_checks ();
14470 /* Add the context to the front of the free list. */
14471 context->next = cp_parser_context_free_list;
14472 cp_parser_context_free_list = context;
14474 return !error_occurred;
14477 /* Returns true if we are parsing tentatively -- but have decided that
14478 we will stick with this tentative parse, even if errors occur. */
14480 static bool
14481 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14483 return (cp_parser_parsing_tentatively (parser)
14484 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14487 /* Returns nonzero iff an error has occurred during the most recent
14488 tentative parse. */
14490 static bool
14491 cp_parser_error_occurred (cp_parser* parser)
14493 return (cp_parser_parsing_tentatively (parser)
14494 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14497 /* Returns nonzero if GNU extensions are allowed. */
14499 static bool
14500 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14502 return parser->allow_gnu_extensions_p;
14507 /* The parser. */
14509 static GTY (()) cp_parser *the_parser;
14511 /* External interface. */
14513 /* Parse one entire translation unit. */
14515 void
14516 c_parse_file (void)
14518 bool error_occurred;
14520 the_parser = cp_parser_new ();
14521 push_deferring_access_checks (flag_access_control
14522 ? dk_no_deferred : dk_no_check);
14523 error_occurred = cp_parser_translation_unit (the_parser);
14524 the_parser = NULL;
14527 /* Clean up after parsing the entire translation unit. */
14529 void
14530 free_parser_stacks (void)
14532 /* Nothing to do. */
14535 /* This variable must be provided by every front end. */
14537 int yydebug;
14539 #include "gt-cp-parser.h"