PR c++/15044
[official-gcc.git] / gcc / cp / parser.c
blob2093ad89db1907d7c822f669c74da196384a7297
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
39 /* The lexer. */
41 /* Overview
42 --------
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
45 look-ahead.
47 Methodology
48 -----------
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
64 circular buffer. */
66 /* A C++ token. */
68 typedef struct cp_token GTY (())
70 /* The kind of token. */
71 ENUM_BITFIELD (cpp_ttype) type : 8;
72 /* If this token is a keyword, this value indicates which keyword.
73 Otherwise, this value is RID_MAX. */
74 ENUM_BITFIELD (rid) keyword : 8;
75 /* Token flags. */
76 unsigned char flags;
77 /* The value associated with this token, if any. */
78 tree value;
79 /* The location at which this token was found. */
80 location_t location;
81 } cp_token;
83 /* The number of tokens in a single token block.
84 Computed so that cp_token_block fits in a 512B allocation unit. */
86 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
88 /* A group of tokens. These groups are chained together to store
89 large numbers of tokens. (For example, a token block is created
90 when the body of an inline member function is first encountered;
91 the tokens are processed later after the class definition is
92 complete.)
94 This somewhat ungainly data structure (as opposed to, say, a
95 variable-length array), is used due to constraints imposed by the
96 current garbage-collection methodology. If it is made more
97 flexible, we could perhaps simplify the data structures involved. */
99 typedef struct cp_token_block GTY (())
101 /* The tokens. */
102 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103 /* The number of tokens in this block. */
104 size_t num_tokens;
105 /* The next token block in the chain. */
106 struct cp_token_block *next;
107 /* The previous block in the chain. */
108 struct cp_token_block *prev;
109 } cp_token_block;
111 typedef struct cp_token_cache GTY (())
113 /* The first block in the cache. NULL if there are no tokens in the
114 cache. */
115 cp_token_block *first;
116 /* The last block in the cache. NULL If there are no tokens in the
117 cache. */
118 cp_token_block *last;
119 } cp_token_cache;
121 /* Prototypes. */
123 static cp_token_cache *cp_token_cache_new
124 (void);
125 static void cp_token_cache_push_token
126 (cp_token_cache *, cp_token *);
128 /* Create a new cp_token_cache. */
130 static cp_token_cache *
131 cp_token_cache_new (void)
133 return ggc_alloc_cleared (sizeof (cp_token_cache));
136 /* Add *TOKEN to *CACHE. */
138 static void
139 cp_token_cache_push_token (cp_token_cache *cache,
140 cp_token *token)
142 cp_token_block *b = cache->last;
144 /* See if we need to allocate a new token block. */
145 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
147 b = ggc_alloc_cleared (sizeof (cp_token_block));
148 b->prev = cache->last;
149 if (cache->last)
151 cache->last->next = b;
152 cache->last = b;
154 else
155 cache->first = cache->last = b;
157 /* Add this token to the current token block. */
158 b->tokens[b->num_tokens++] = *token;
161 /* The cp_lexer structure represents the C++ lexer. It is responsible
162 for managing the token stream from the preprocessor and supplying
163 it to the parser. */
165 typedef struct cp_lexer GTY (())
167 /* The memory allocated for the buffer. Never NULL. */
168 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169 /* A pointer just past the end of the memory allocated for the buffer. */
170 cp_token * GTY ((skip (""))) buffer_end;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token * GTY ((skip (""))) first_token;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token * GTY ((skip (""))) next_token;
176 /* A pointer just past the last available token. If FIRST_TOKEN is
177 NULL, however, there are no available tokens, and then this
178 location is simply the place in which the next token read will be
179 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180 When the LAST_TOKEN == BUFFER, then the last token is at the
181 highest memory address in the BUFFER. */
182 cp_token * GTY ((skip (""))) last_token;
184 /* A stack indicating positions at which cp_lexer_save_tokens was
185 called. The top entry is the most recent position at which we
186 began saving tokens. The entries are differences in token
187 position between FIRST_TOKEN and the first saved token.
189 If the stack is non-empty, we are saving tokens. When a token is
190 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191 pointer will not. The token stream will be preserved so that it
192 can be reexamined later.
194 If the stack is empty, then we are not saving tokens. Whenever a
195 token is consumed, the FIRST_TOKEN pointer will be moved, and the
196 consumed token will be gone forever. */
197 varray_type saved_tokens;
199 /* The STRING_CST tokens encountered while processing the current
200 string literal. */
201 varray_type string_tokens;
203 /* True if we should obtain more tokens from the preprocessor; false
204 if we are processing a saved token cache. */
205 bool main_lexer_p;
207 /* True if we should output debugging information. */
208 bool debugging_p;
210 /* The next lexer in a linked list of lexers. */
211 struct cp_lexer *next;
212 } cp_lexer;
214 /* Prototypes. */
216 static cp_lexer *cp_lexer_new_main
217 (void);
218 static cp_lexer *cp_lexer_new_from_tokens
219 (struct cp_token_cache *);
220 static int cp_lexer_saving_tokens
221 (const cp_lexer *);
222 static cp_token *cp_lexer_next_token
223 (cp_lexer *, cp_token *);
224 static cp_token *cp_lexer_prev_token
225 (cp_lexer *, cp_token *);
226 static ptrdiff_t cp_lexer_token_difference
227 (cp_lexer *, cp_token *, cp_token *);
228 static cp_token *cp_lexer_read_token
229 (cp_lexer *);
230 static void cp_lexer_maybe_grow_buffer
231 (cp_lexer *);
232 static void cp_lexer_get_preprocessor_token
233 (cp_lexer *, cp_token *);
234 static cp_token *cp_lexer_peek_token
235 (cp_lexer *);
236 static cp_token *cp_lexer_peek_nth_token
237 (cp_lexer *, size_t);
238 static inline bool cp_lexer_next_token_is
239 (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_not
241 (cp_lexer *, enum cpp_ttype);
242 static bool cp_lexer_next_token_is_keyword
243 (cp_lexer *, enum rid);
244 static cp_token *cp_lexer_consume_token
245 (cp_lexer *);
246 static void cp_lexer_purge_token
247 (cp_lexer *);
248 static void cp_lexer_purge_tokens_after
249 (cp_lexer *, cp_token *);
250 static void cp_lexer_save_tokens
251 (cp_lexer *);
252 static void cp_lexer_commit_tokens
253 (cp_lexer *);
254 static void cp_lexer_rollback_tokens
255 (cp_lexer *);
256 static inline void cp_lexer_set_source_position_from_token
257 (cp_lexer *, const cp_token *);
258 static void cp_lexer_print_token
259 (FILE *, cp_token *);
260 static inline bool cp_lexer_debugging_p
261 (cp_lexer *);
262 static void cp_lexer_start_debugging
263 (cp_lexer *) ATTRIBUTE_UNUSED;
264 static void cp_lexer_stop_debugging
265 (cp_lexer *) ATTRIBUTE_UNUSED;
267 /* Manifest constants. */
269 #define CP_TOKEN_BUFFER_SIZE 5
270 #define CP_SAVED_TOKENS_SIZE 5
272 /* A token type for keywords, as opposed to ordinary identifiers. */
273 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
275 /* A token type for template-ids. If a template-id is processed while
276 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277 the value of the CPP_TEMPLATE_ID is whatever was returned by
278 cp_parser_template_id. */
279 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
281 /* A token type for nested-name-specifiers. If a
282 nested-name-specifier is processed while parsing tentatively, it is
283 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285 cp_parser_nested_name_specifier_opt. */
286 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
288 /* A token type for tokens that are not tokens at all; these are used
289 to mark the end of a token block. */
290 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
292 /* Variables. */
294 /* The stream to which debugging output should be written. */
295 static FILE *cp_lexer_debug_stream;
297 /* Create a new main C++ lexer, the lexer that gets tokens from the
298 preprocessor. */
300 static cp_lexer *
301 cp_lexer_new_main (void)
303 cp_lexer *lexer;
304 cp_token first_token;
306 /* It's possible that lexing the first token will load a PCH file,
307 which is a GC collection point. So we have to grab the first
308 token before allocating any memory. */
309 cp_lexer_get_preprocessor_token (NULL, &first_token);
310 c_common_no_more_pch ();
312 /* Allocate the memory. */
313 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
315 /* Create the circular buffer. */
316 lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
317 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
319 /* There is one token in the buffer. */
320 lexer->last_token = lexer->buffer + 1;
321 lexer->first_token = lexer->buffer;
322 lexer->next_token = lexer->buffer;
323 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
325 /* This lexer obtains more tokens by calling c_lex. */
326 lexer->main_lexer_p = true;
328 /* Create the SAVED_TOKENS stack. */
329 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
331 /* Create the STRINGS array. */
332 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
334 /* Assume we are not debugging. */
335 lexer->debugging_p = false;
337 return lexer;
340 /* Create a new lexer whose token stream is primed with the TOKENS.
341 When these tokens are exhausted, no new tokens will be read. */
343 static cp_lexer *
344 cp_lexer_new_from_tokens (cp_token_cache *tokens)
346 cp_lexer *lexer;
347 cp_token *token;
348 cp_token_block *block;
349 ptrdiff_t num_tokens;
351 /* Allocate the memory. */
352 lexer = ggc_alloc_cleared (sizeof (cp_lexer));
354 /* Create a new buffer, appropriately sized. */
355 num_tokens = 0;
356 for (block = tokens->first; block != NULL; block = block->next)
357 num_tokens += block->num_tokens;
358 lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
359 lexer->buffer_end = lexer->buffer + num_tokens;
361 /* Install the tokens. */
362 token = lexer->buffer;
363 for (block = tokens->first; block != NULL; block = block->next)
365 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
366 token += block->num_tokens;
369 /* The FIRST_TOKEN is the beginning of the buffer. */
370 lexer->first_token = lexer->buffer;
371 /* The next available token is also at the beginning of the buffer. */
372 lexer->next_token = lexer->buffer;
373 /* The buffer is full. */
374 lexer->last_token = lexer->first_token;
376 /* This lexer doesn't obtain more tokens. */
377 lexer->main_lexer_p = false;
379 /* Create the SAVED_TOKENS stack. */
380 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
382 /* Create the STRINGS array. */
383 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
385 /* Assume we are not debugging. */
386 lexer->debugging_p = false;
388 return lexer;
391 /* Returns nonzero if debugging information should be output. */
393 static inline bool
394 cp_lexer_debugging_p (cp_lexer *lexer)
396 return lexer->debugging_p;
399 /* Set the current source position from the information stored in
400 TOKEN. */
402 static inline void
403 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
404 const cp_token *token)
406 /* Ideally, the source position information would not be a global
407 variable, but it is. */
409 /* Update the line number. */
410 if (token->type != CPP_EOF)
411 input_location = token->location;
414 /* TOKEN points into the circular token buffer. Return a pointer to
415 the next token in the buffer. */
417 static inline cp_token *
418 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
420 token++;
421 if (token == lexer->buffer_end)
422 token = lexer->buffer;
423 return token;
426 /* TOKEN points into the circular token buffer. Return a pointer to
427 the previous token in the buffer. */
429 static inline cp_token *
430 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
432 if (token == lexer->buffer)
433 token = lexer->buffer_end;
434 return token - 1;
437 /* nonzero if we are presently saving tokens. */
439 static int
440 cp_lexer_saving_tokens (const cp_lexer* lexer)
442 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
445 /* Return a pointer to the token that is N tokens beyond TOKEN in the
446 buffer. */
448 static cp_token *
449 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
451 token += n;
452 if (token >= lexer->buffer_end)
453 token = lexer->buffer + (token - lexer->buffer_end);
454 return token;
457 /* Returns the number of times that START would have to be incremented
458 to reach FINISH. If START and FINISH are the same, returns zero. */
460 static ptrdiff_t
461 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
463 if (finish >= start)
464 return finish - start;
465 else
466 return ((lexer->buffer_end - lexer->buffer)
467 - (start - finish));
470 /* Obtain another token from the C preprocessor and add it to the
471 token buffer. Returns the newly read token. */
473 static cp_token *
474 cp_lexer_read_token (cp_lexer* lexer)
476 cp_token *token;
478 /* Make sure there is room in the buffer. */
479 cp_lexer_maybe_grow_buffer (lexer);
481 /* If there weren't any tokens, then this one will be the first. */
482 if (!lexer->first_token)
483 lexer->first_token = lexer->last_token;
484 /* Similarly, if there were no available tokens, there is one now. */
485 if (!lexer->next_token)
486 lexer->next_token = lexer->last_token;
488 /* Figure out where we're going to store the new token. */
489 token = lexer->last_token;
491 /* Get a new token from the preprocessor. */
492 cp_lexer_get_preprocessor_token (lexer, token);
494 /* Increment LAST_TOKEN. */
495 lexer->last_token = cp_lexer_next_token (lexer, token);
497 /* Strings should have type `const char []'. Right now, we will
498 have an ARRAY_TYPE that is constant rather than an array of
499 constant elements.
500 FIXME: Make fix_string_type get this right in the first place. */
501 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
502 && flag_const_strings)
504 tree type;
506 /* Get the current type. It will be an ARRAY_TYPE. */
507 type = TREE_TYPE (token->value);
508 /* Use build_cplus_array_type to rebuild the array, thereby
509 getting the right type. */
510 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511 /* Reset the type of the token. */
512 TREE_TYPE (token->value) = type;
515 return token;
518 /* If the circular buffer is full, make it bigger. */
520 static void
521 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
523 /* If the buffer is full, enlarge it. */
524 if (lexer->last_token == lexer->first_token)
526 cp_token *new_buffer;
527 cp_token *old_buffer;
528 cp_token *new_first_token;
529 ptrdiff_t buffer_length;
530 size_t num_tokens_to_copy;
532 /* Remember the current buffer pointer. It will become invalid,
533 but we will need to do pointer arithmetic involving this
534 value. */
535 old_buffer = lexer->buffer;
536 /* Compute the current buffer size. */
537 buffer_length = lexer->buffer_end - lexer->buffer;
538 /* Allocate a buffer twice as big. */
539 new_buffer = ggc_realloc (lexer->buffer,
540 2 * buffer_length * sizeof (cp_token));
542 /* Because the buffer is circular, logically consecutive tokens
543 are not necessarily placed consecutively in memory.
544 Therefore, we must keep move the tokens that were before
545 FIRST_TOKEN to the second half of the newly allocated
546 buffer. */
547 num_tokens_to_copy = (lexer->first_token - old_buffer);
548 memcpy (new_buffer + buffer_length,
549 new_buffer,
550 num_tokens_to_copy * sizeof (cp_token));
551 /* Clear the rest of the buffer. We never look at this storage,
552 but the garbage collector may. */
553 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
554 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
556 /* Now recompute all of the buffer pointers. */
557 new_first_token
558 = new_buffer + (lexer->first_token - old_buffer);
559 if (lexer->next_token != NULL)
561 ptrdiff_t next_token_delta;
563 if (lexer->next_token > lexer->first_token)
564 next_token_delta = lexer->next_token - lexer->first_token;
565 else
566 next_token_delta =
567 buffer_length - (lexer->first_token - lexer->next_token);
568 lexer->next_token = new_first_token + next_token_delta;
570 lexer->last_token = new_first_token + buffer_length;
571 lexer->buffer = new_buffer;
572 lexer->buffer_end = new_buffer + buffer_length * 2;
573 lexer->first_token = new_first_token;
577 /* Store the next token from the preprocessor in *TOKEN. */
579 static void
580 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
581 cp_token *token)
583 bool done;
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
586 if (lexer != NULL && !lexer->main_lexer_p)
588 token->type = CPP_EOF;
589 token->location.line = 0;
590 token->location.file = NULL;
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
594 return;
597 done = false;
598 /* Keep going until we get a token we like. */
599 while (!done)
601 /* Get a new token from the preprocessor. */
602 token->type = c_lex_with_flags (&token->value, &token->flags);
603 /* Issue messages about tokens we cannot process. */
604 switch (token->type)
606 case CPP_ATSIGN:
607 case CPP_HASH:
608 case CPP_PASTE:
609 error ("invalid token");
610 break;
612 default:
613 /* This is a good token, so we exit the loop. */
614 done = true;
615 break;
618 /* Now we've got our token. */
619 token->location = input_location;
621 /* Check to see if this token is a keyword. */
622 if (token->type == CPP_NAME
623 && C_IS_RESERVED_WORD (token->value))
625 /* Mark this token as a keyword. */
626 token->type = CPP_KEYWORD;
627 /* Record which keyword. */
628 token->keyword = C_RID_CODE (token->value);
629 /* Update the value. Some keywords are mapped to particular
630 entities, rather than simply having the value of the
631 corresponding IDENTIFIER_NODE. For example, `__const' is
632 mapped to `const'. */
633 token->value = ridpointers[token->keyword];
635 else
636 token->keyword = RID_MAX;
639 /* Return a pointer to the next token in the token stream, but do not
640 consume it. */
642 static cp_token *
643 cp_lexer_peek_token (cp_lexer* lexer)
645 cp_token *token;
647 /* If there are no tokens, read one now. */
648 if (!lexer->next_token)
649 cp_lexer_read_token (lexer);
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
654 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656 fprintf (cp_lexer_debug_stream, "\n");
659 token = lexer->next_token;
660 cp_lexer_set_source_position_from_token (lexer, token);
661 return token;
664 /* Return true if the next token has the indicated TYPE. */
666 static bool
667 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
669 cp_token *token;
671 /* Peek at the next token. */
672 token = cp_lexer_peek_token (lexer);
673 /* Check to see if it has the indicated TYPE. */
674 return token->type == type;
677 /* Return true if the next token does not have the indicated TYPE. */
679 static bool
680 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
682 return !cp_lexer_next_token_is (lexer, type);
685 /* Return true if the next token is the indicated KEYWORD. */
687 static bool
688 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
690 cp_token *token;
692 /* Peek at the next token. */
693 token = cp_lexer_peek_token (lexer);
694 /* Check to see if it is the indicated keyword. */
695 return token->keyword == keyword;
698 /* Return a pointer to the Nth token in the token stream. If N is 1,
699 then this is precisely equivalent to cp_lexer_peek_token. */
701 static cp_token *
702 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
704 cp_token *token;
706 /* N is 1-based, not zero-based. */
707 my_friendly_assert (n > 0, 20000224);
709 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
710 token = lexer->next_token;
711 /* If there are no tokens in the buffer, get one now. */
712 if (!token)
714 cp_lexer_read_token (lexer);
715 token = lexer->next_token;
718 /* Now, read tokens until we have enough. */
719 while (--n > 0)
721 /* Advance to the next token. */
722 token = cp_lexer_next_token (lexer, token);
723 /* If that's all the tokens we have, read a new one. */
724 if (token == lexer->last_token)
725 token = cp_lexer_read_token (lexer);
728 return token;
731 /* Consume the next token. The pointer returned is valid only until
732 another token is read. Callers should preserve copy the token
733 explicitly if they will need its value for a longer period of
734 time. */
736 static cp_token *
737 cp_lexer_consume_token (cp_lexer* lexer)
739 cp_token *token;
741 /* If there are no tokens, read one now. */
742 if (!lexer->next_token)
743 cp_lexer_read_token (lexer);
745 /* Remember the token we'll be returning. */
746 token = lexer->next_token;
748 /* Increment NEXT_TOKEN. */
749 lexer->next_token = cp_lexer_next_token (lexer,
750 lexer->next_token);
751 /* Check to see if we're all out of tokens. */
752 if (lexer->next_token == lexer->last_token)
753 lexer->next_token = NULL;
755 /* If we're not saving tokens, then move FIRST_TOKEN too. */
756 if (!cp_lexer_saving_tokens (lexer))
758 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
759 if (!lexer->next_token)
760 lexer->first_token = NULL;
761 else
762 lexer->first_token = lexer->next_token;
765 /* Provide debugging output. */
766 if (cp_lexer_debugging_p (lexer))
768 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769 cp_lexer_print_token (cp_lexer_debug_stream, token);
770 fprintf (cp_lexer_debug_stream, "\n");
773 return token;
776 /* Permanently remove the next token from the token stream. There
777 must be a valid next token already; this token never reads
778 additional tokens from the preprocessor. */
780 static void
781 cp_lexer_purge_token (cp_lexer *lexer)
783 cp_token *token;
784 cp_token *next_token;
786 token = lexer->next_token;
787 while (true)
789 next_token = cp_lexer_next_token (lexer, token);
790 if (next_token == lexer->last_token)
791 break;
792 *token = *next_token;
793 token = next_token;
796 lexer->last_token = token;
797 /* The token purged may have been the only token remaining; if so,
798 clear NEXT_TOKEN. */
799 if (lexer->next_token == token)
800 lexer->next_token = NULL;
803 /* Permanently remove all tokens after TOKEN, up to, but not
804 including, the token that will be returned next by
805 cp_lexer_peek_token. */
807 static void
808 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
810 cp_token *peek;
811 cp_token *t1;
812 cp_token *t2;
814 if (lexer->next_token)
816 /* Copy the tokens that have not yet been read to the location
817 immediately following TOKEN. */
818 t1 = cp_lexer_next_token (lexer, token);
819 t2 = peek = cp_lexer_peek_token (lexer);
820 /* Move tokens into the vacant area between TOKEN and PEEK. */
821 while (t2 != lexer->last_token)
823 *t1 = *t2;
824 t1 = cp_lexer_next_token (lexer, t1);
825 t2 = cp_lexer_next_token (lexer, t2);
827 /* Now, the next available token is right after TOKEN. */
828 lexer->next_token = cp_lexer_next_token (lexer, token);
829 /* And the last token is wherever we ended up. */
830 lexer->last_token = t1;
832 else
834 /* There are no tokens in the buffer, so there is nothing to
835 copy. The last token in the buffer is TOKEN itself. */
836 lexer->last_token = cp_lexer_next_token (lexer, token);
840 /* Begin saving tokens. All tokens consumed after this point will be
841 preserved. */
843 static void
844 cp_lexer_save_tokens (cp_lexer* lexer)
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
850 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851 restore the tokens if required. */
852 if (!lexer->next_token)
853 cp_lexer_read_token (lexer);
855 VARRAY_PUSH_INT (lexer->saved_tokens,
856 cp_lexer_token_difference (lexer,
857 lexer->first_token,
858 lexer->next_token));
861 /* Commit to the portion of the token stream most recently saved. */
863 static void
864 cp_lexer_commit_tokens (cp_lexer* lexer)
866 /* Provide debugging output. */
867 if (cp_lexer_debugging_p (lexer))
868 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
870 VARRAY_POP (lexer->saved_tokens);
873 /* Return all tokens saved since the last call to cp_lexer_save_tokens
874 to the token stream. Stop saving tokens. */
876 static void
877 cp_lexer_rollback_tokens (cp_lexer* lexer)
879 size_t delta;
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer))
883 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
885 /* Find the token that was the NEXT_TOKEN when we started saving
886 tokens. */
887 delta = VARRAY_TOP_INT(lexer->saved_tokens);
888 /* Make it the next token again now. */
889 lexer->next_token = cp_lexer_advance_token (lexer,
890 lexer->first_token,
891 delta);
892 /* It might be the case that there were no tokens when we started
893 saving tokens, but that there are some tokens now. */
894 if (!lexer->next_token && lexer->first_token)
895 lexer->next_token = lexer->first_token;
897 /* Stop saving tokens. */
898 VARRAY_POP (lexer->saved_tokens);
901 /* Print a representation of the TOKEN on the STREAM. */
903 static void
904 cp_lexer_print_token (FILE * stream, cp_token* token)
906 const char *token_type = NULL;
908 /* Figure out what kind of token this is. */
909 switch (token->type)
911 case CPP_EQ:
912 token_type = "EQ";
913 break;
915 case CPP_COMMA:
916 token_type = "COMMA";
917 break;
919 case CPP_OPEN_PAREN:
920 token_type = "OPEN_PAREN";
921 break;
923 case CPP_CLOSE_PAREN:
924 token_type = "CLOSE_PAREN";
925 break;
927 case CPP_OPEN_BRACE:
928 token_type = "OPEN_BRACE";
929 break;
931 case CPP_CLOSE_BRACE:
932 token_type = "CLOSE_BRACE";
933 break;
935 case CPP_SEMICOLON:
936 token_type = "SEMICOLON";
937 break;
939 case CPP_NAME:
940 token_type = "NAME";
941 break;
943 case CPP_EOF:
944 token_type = "EOF";
945 break;
947 case CPP_KEYWORD:
948 token_type = "keyword";
949 break;
951 /* This is not a token that we know how to handle yet. */
952 default:
953 break;
956 /* If we have a name for the token, print it out. Otherwise, we
957 simply give the numeric code. */
958 if (token_type)
959 fprintf (stream, "%s", token_type);
960 else
961 fprintf (stream, "%d", token->type);
962 /* And, for an identifier, print the identifier name. */
963 if (token->type == CPP_NAME
964 /* Some keywords have a value that is not an IDENTIFIER_NODE.
965 For example, `struct' is mapped to an INTEGER_CST. */
966 || (token->type == CPP_KEYWORD
967 && TREE_CODE (token->value) == IDENTIFIER_NODE))
968 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
971 /* Start emitting debugging information. */
973 static void
974 cp_lexer_start_debugging (cp_lexer* lexer)
976 ++lexer->debugging_p;
979 /* Stop emitting debugging information. */
981 static void
982 cp_lexer_stop_debugging (cp_lexer* lexer)
984 --lexer->debugging_p;
988 /* The parser. */
990 /* Overview
991 --------
993 A cp_parser parses the token stream as specified by the C++
994 grammar. Its job is purely parsing, not semantic analysis. For
995 example, the parser breaks the token stream into declarators,
996 expressions, statements, and other similar syntactic constructs.
997 It does not check that the types of the expressions on either side
998 of an assignment-statement are compatible, or that a function is
999 not declared with a parameter of type `void'.
1001 The parser invokes routines elsewhere in the compiler to perform
1002 semantic analysis and to build up the abstract syntax tree for the
1003 code processed.
1005 The parser (and the template instantiation code, which is, in a
1006 way, a close relative of parsing) are the only parts of the
1007 compiler that should be calling push_scope and pop_scope, or
1008 related functions. The parser (and template instantiation code)
1009 keeps track of what scope is presently active; everything else
1010 should simply honor that. (The code that generates static
1011 initializers may also need to set the scope, in order to check
1012 access control correctly when emitting the initializers.)
1014 Methodology
1015 -----------
1017 The parser is of the standard recursive-descent variety. Upcoming
1018 tokens in the token stream are examined in order to determine which
1019 production to use when parsing a non-terminal. Some C++ constructs
1020 require arbitrary look ahead to disambiguate. For example, it is
1021 impossible, in the general case, to tell whether a statement is an
1022 expression or declaration without scanning the entire statement.
1023 Therefore, the parser is capable of "parsing tentatively." When the
1024 parser is not sure what construct comes next, it enters this mode.
1025 Then, while we attempt to parse the construct, the parser queues up
1026 error messages, rather than issuing them immediately, and saves the
1027 tokens it consumes. If the construct is parsed successfully, the
1028 parser "commits", i.e., it issues any queued error messages and
1029 the tokens that were being preserved are permanently discarded.
1030 If, however, the construct is not parsed successfully, the parser
1031 rolls back its state completely so that it can resume parsing using
1032 a different alternative.
1034 Future Improvements
1035 -------------------
1037 The performance of the parser could probably be improved
1038 substantially. Some possible improvements include:
1040 - The expression parser recurses through the various levels of
1041 precedence as specified in the grammar, rather than using an
1042 operator-precedence technique. Therefore, parsing a simple
1043 identifier requires multiple recursive calls.
1045 - We could often eliminate the need to parse tentatively by
1046 looking ahead a little bit. In some places, this approach
1047 might not entirely eliminate the need to parse tentatively, but
1048 it might still speed up the average case. */
1050 /* Flags that are passed to some parsing functions. These values can
1051 be bitwise-ored together. */
1053 typedef enum cp_parser_flags
1055 /* No flags. */
1056 CP_PARSER_FLAGS_NONE = 0x0,
1057 /* The construct is optional. If it is not present, then no error
1058 should be issued. */
1059 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060 /* When parsing a type-specifier, do not allow user-defined types. */
1061 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1062 } cp_parser_flags;
1064 /* The different kinds of declarators we want to parse. */
1066 typedef enum cp_parser_declarator_kind
1068 /* We want an abstract declartor. */
1069 CP_PARSER_DECLARATOR_ABSTRACT,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED,
1072 /* We don't mind, but the name must be an unqualified-id. */
1073 CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind;
1076 /* A mapping from a token type to a corresponding tree node type. */
1078 typedef struct cp_parser_token_tree_map_node
1080 /* The token type. */
1081 ENUM_BITFIELD (cpp_ttype) token_type : 8;
1082 /* The corresponding tree code. */
1083 ENUM_BITFIELD (tree_code) tree_type : 8;
1084 } cp_parser_token_tree_map_node;
1086 /* A complete map consists of several ordinary entries, followed by a
1087 terminator. The terminating entry has a token_type of CPP_EOF. */
1089 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1091 /* The status of a tentative parse. */
1093 typedef enum cp_parser_status_kind
1095 /* No errors have occurred. */
1096 CP_PARSER_STATUS_KIND_NO_ERROR,
1097 /* An error has occurred. */
1098 CP_PARSER_STATUS_KIND_ERROR,
1099 /* We are committed to this tentative parse, whether or not an error
1100 has occurred. */
1101 CP_PARSER_STATUS_KIND_COMMITTED
1102 } cp_parser_status_kind;
1104 /* Context that is saved and restored when parsing tentatively. */
1106 typedef struct cp_parser_context GTY (())
1108 /* If this is a tentative parsing context, the status of the
1109 tentative parse. */
1110 enum cp_parser_status_kind status;
1111 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1112 that are looked up in this context must be looked up both in the
1113 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114 the context of the containing expression. */
1115 tree object_type;
1116 /* The next parsing context in the stack. */
1117 struct cp_parser_context *next;
1118 } cp_parser_context;
1120 /* Prototypes. */
1122 /* Constructors and destructors. */
1124 static cp_parser_context *cp_parser_context_new
1125 (cp_parser_context *);
1127 /* Class variables. */
1129 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1131 /* Constructors and destructors. */
1133 /* Construct a new context. The context below this one on the stack
1134 is given by NEXT. */
1136 static cp_parser_context *
1137 cp_parser_context_new (cp_parser_context* next)
1139 cp_parser_context *context;
1141 /* Allocate the storage. */
1142 if (cp_parser_context_free_list != NULL)
1144 /* Pull the first entry from the free list. */
1145 context = cp_parser_context_free_list;
1146 cp_parser_context_free_list = context->next;
1147 memset (context, 0, sizeof (*context));
1149 else
1150 context = ggc_alloc_cleared (sizeof (cp_parser_context));
1151 /* No errors have occurred yet in this context. */
1152 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153 /* If this is not the bottomost context, copy information that we
1154 need from the previous context. */
1155 if (next)
1157 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158 expression, then we are parsing one in this context, too. */
1159 context->object_type = next->object_type;
1160 /* Thread the stack. */
1161 context->next = next;
1164 return context;
1167 /* The cp_parser structure represents the C++ parser. */
1169 typedef struct cp_parser GTY(())
1171 /* The lexer from which we are obtaining tokens. */
1172 cp_lexer *lexer;
1174 /* The scope in which names should be looked up. If NULL_TREE, then
1175 we look up names in the scope that is currently open in the
1176 source program. If non-NULL, this is either a TYPE or
1177 NAMESPACE_DECL for the scope in which we should look.
1179 This value is not cleared automatically after a name is looked
1180 up, so we must be careful to clear it before starting a new look
1181 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1182 will look up `Z' in the scope of `X', rather than the current
1183 scope.) Unfortunately, it is difficult to tell when name lookup
1184 is complete, because we sometimes peek at a token, look it up,
1185 and then decide not to consume it. */
1186 tree scope;
1188 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189 last lookup took place. OBJECT_SCOPE is used if an expression
1190 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191 respectively. QUALIFYING_SCOPE is used for an expression of the
1192 form "X::Y"; it refers to X. */
1193 tree object_scope;
1194 tree qualifying_scope;
1196 /* A stack of parsing contexts. All but the bottom entry on the
1197 stack will be tentative contexts.
1199 We parse tentatively in order to determine which construct is in
1200 use in some situations. For example, in order to determine
1201 whether a statement is an expression-statement or a
1202 declaration-statement we parse it tentatively as a
1203 declaration-statement. If that fails, we then reparse the same
1204 token stream as an expression-statement. */
1205 cp_parser_context *context;
1207 /* True if we are parsing GNU C++. If this flag is not set, then
1208 GNU extensions are not recognized. */
1209 bool allow_gnu_extensions_p;
1211 /* TRUE if the `>' token should be interpreted as the greater-than
1212 operator. FALSE if it is the end of a template-id or
1213 template-parameter-list. */
1214 bool greater_than_is_operator_p;
1216 /* TRUE if default arguments are allowed within a parameter list
1217 that starts at this point. FALSE if only a gnu extension makes
1218 them permissible. */
1219 bool default_arg_ok_p;
1221 /* TRUE if we are parsing an integral constant-expression. See
1222 [expr.const] for a precise definition. */
1223 bool integral_constant_expression_p;
1225 /* TRUE if we are parsing an integral constant-expression -- but a
1226 non-constant expression should be permitted as well. This flag
1227 is used when parsing an array bound so that GNU variable-length
1228 arrays are tolerated. */
1229 bool allow_non_integral_constant_expression_p;
1231 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232 been seen that makes the expression non-constant. */
1233 bool non_integral_constant_expression_p;
1235 /* TRUE if we are parsing the argument to "__offsetof__". */
1236 bool in_offsetof_p;
1238 /* TRUE if local variable names and `this' are forbidden in the
1239 current context. */
1240 bool local_variables_forbidden_p;
1242 /* TRUE if the declaration we are parsing is part of a
1243 linkage-specification of the form `extern string-literal
1244 declaration'. */
1245 bool in_unbraced_linkage_specification_p;
1247 /* TRUE if we are presently parsing a declarator, after the
1248 direct-declarator. */
1249 bool in_declarator_p;
1251 /* TRUE if we are presently parsing a template-argument-list. */
1252 bool in_template_argument_list_p;
1254 /* TRUE if we are presently parsing the body of an
1255 iteration-statement. */
1256 bool in_iteration_statement_p;
1258 /* TRUE if we are presently parsing the body of a switch
1259 statement. */
1260 bool in_switch_statement_p;
1262 /* TRUE if we are parsing a type-id in an expression context. In
1263 such a situation, both "type (expr)" and "type (type)" are valid
1264 alternatives. */
1265 bool in_type_id_in_expr_p;
1267 /* If non-NULL, then we are parsing a construct where new type
1268 definitions are not permitted. The string stored here will be
1269 issued as an error message if a type is defined. */
1270 const char *type_definition_forbidden_message;
1272 /* A list of lists. The outer list is a stack, used for member
1273 functions of local classes. At each level there are two sub-list,
1274 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1275 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1276 TREE_VALUE's. The functions are chained in reverse declaration
1277 order.
1279 The TREE_PURPOSE sublist contains those functions with default
1280 arguments that need post processing, and the TREE_VALUE sublist
1281 contains those functions with definitions that need post
1282 processing.
1284 These lists can only be processed once the outermost class being
1285 defined is complete. */
1286 tree unparsed_functions_queues;
1288 /* The number of classes whose definitions are currently in
1289 progress. */
1290 unsigned num_classes_being_defined;
1292 /* The number of template parameter lists that apply directly to the
1293 current declaration. */
1294 unsigned num_template_parameter_lists;
1295 } cp_parser;
1297 /* The type of a function that parses some kind of expression. */
1298 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1300 /* Prototypes. */
1302 /* Constructors and destructors. */
1304 static cp_parser *cp_parser_new
1305 (void);
1307 /* Routines to parse various constructs.
1309 Those that return `tree' will return the error_mark_node (rather
1310 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1311 Sometimes, they will return an ordinary node if error-recovery was
1312 attempted, even though a parse error occurred. So, to check
1313 whether or not a parse error occurred, you should always use
1314 cp_parser_error_occurred. If the construct is optional (indicated
1315 either by an `_opt' in the name of the function that does the
1316 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1317 the construct is not present. */
1319 /* Lexical conventions [gram.lex] */
1321 static tree cp_parser_identifier
1322 (cp_parser *);
1324 /* Basic concepts [gram.basic] */
1326 static bool cp_parser_translation_unit
1327 (cp_parser *);
1329 /* Expressions [gram.expr] */
1331 static tree cp_parser_primary_expression
1332 (cp_parser *, cp_id_kind *, tree *);
1333 static tree cp_parser_id_expression
1334 (cp_parser *, bool, bool, bool *, bool);
1335 static tree cp_parser_unqualified_id
1336 (cp_parser *, bool, bool, bool);
1337 static tree cp_parser_nested_name_specifier_opt
1338 (cp_parser *, bool, bool, bool, bool);
1339 static tree cp_parser_nested_name_specifier
1340 (cp_parser *, bool, bool, bool, bool);
1341 static tree cp_parser_class_or_namespace_name
1342 (cp_parser *, bool, bool, bool, bool, bool);
1343 static tree cp_parser_postfix_expression
1344 (cp_parser *, bool);
1345 static tree cp_parser_parenthesized_expression_list
1346 (cp_parser *, bool, bool *);
1347 static void cp_parser_pseudo_destructor_name
1348 (cp_parser *, tree *, tree *);
1349 static tree cp_parser_unary_expression
1350 (cp_parser *, bool);
1351 static enum tree_code cp_parser_unary_operator
1352 (cp_token *);
1353 static tree cp_parser_new_expression
1354 (cp_parser *);
1355 static tree cp_parser_new_placement
1356 (cp_parser *);
1357 static tree cp_parser_new_type_id
1358 (cp_parser *);
1359 static tree cp_parser_new_declarator_opt
1360 (cp_parser *);
1361 static tree cp_parser_direct_new_declarator
1362 (cp_parser *);
1363 static tree cp_parser_new_initializer
1364 (cp_parser *);
1365 static tree cp_parser_delete_expression
1366 (cp_parser *);
1367 static tree cp_parser_cast_expression
1368 (cp_parser *, bool);
1369 static tree cp_parser_pm_expression
1370 (cp_parser *);
1371 static tree cp_parser_multiplicative_expression
1372 (cp_parser *);
1373 static tree cp_parser_additive_expression
1374 (cp_parser *);
1375 static tree cp_parser_shift_expression
1376 (cp_parser *);
1377 static tree cp_parser_relational_expression
1378 (cp_parser *);
1379 static tree cp_parser_equality_expression
1380 (cp_parser *);
1381 static tree cp_parser_and_expression
1382 (cp_parser *);
1383 static tree cp_parser_exclusive_or_expression
1384 (cp_parser *);
1385 static tree cp_parser_inclusive_or_expression
1386 (cp_parser *);
1387 static tree cp_parser_logical_and_expression
1388 (cp_parser *);
1389 static tree cp_parser_logical_or_expression
1390 (cp_parser *);
1391 static tree cp_parser_question_colon_clause
1392 (cp_parser *, tree);
1393 static tree cp_parser_assignment_expression
1394 (cp_parser *);
1395 static enum tree_code cp_parser_assignment_operator_opt
1396 (cp_parser *);
1397 static tree cp_parser_expression
1398 (cp_parser *);
1399 static tree cp_parser_constant_expression
1400 (cp_parser *, bool, bool *);
1402 /* Statements [gram.stmt.stmt] */
1404 static void cp_parser_statement
1405 (cp_parser *, bool);
1406 static tree cp_parser_labeled_statement
1407 (cp_parser *, bool);
1408 static tree cp_parser_expression_statement
1409 (cp_parser *, bool);
1410 static tree cp_parser_compound_statement
1411 (cp_parser *, bool);
1412 static void cp_parser_statement_seq_opt
1413 (cp_parser *, bool);
1414 static tree cp_parser_selection_statement
1415 (cp_parser *);
1416 static tree cp_parser_condition
1417 (cp_parser *);
1418 static tree cp_parser_iteration_statement
1419 (cp_parser *);
1420 static void cp_parser_for_init_statement
1421 (cp_parser *);
1422 static tree cp_parser_jump_statement
1423 (cp_parser *);
1424 static void cp_parser_declaration_statement
1425 (cp_parser *);
1427 static tree cp_parser_implicitly_scoped_statement
1428 (cp_parser *);
1429 static void cp_parser_already_scoped_statement
1430 (cp_parser *);
1432 /* Declarations [gram.dcl.dcl] */
1434 static void cp_parser_declaration_seq_opt
1435 (cp_parser *);
1436 static void cp_parser_declaration
1437 (cp_parser *);
1438 static void cp_parser_block_declaration
1439 (cp_parser *, bool);
1440 static void cp_parser_simple_declaration
1441 (cp_parser *, bool);
1442 static tree cp_parser_decl_specifier_seq
1443 (cp_parser *, cp_parser_flags, tree *, int *);
1444 static tree cp_parser_storage_class_specifier_opt
1445 (cp_parser *);
1446 static tree cp_parser_function_specifier_opt
1447 (cp_parser *);
1448 static tree cp_parser_type_specifier
1449 (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1450 static tree cp_parser_simple_type_specifier
1451 (cp_parser *, cp_parser_flags, bool);
1452 static tree cp_parser_type_name
1453 (cp_parser *);
1454 static tree cp_parser_elaborated_type_specifier
1455 (cp_parser *, bool, bool);
1456 static tree cp_parser_enum_specifier
1457 (cp_parser *);
1458 static void cp_parser_enumerator_list
1459 (cp_parser *, tree);
1460 static void cp_parser_enumerator_definition
1461 (cp_parser *, tree);
1462 static tree cp_parser_namespace_name
1463 (cp_parser *);
1464 static void cp_parser_namespace_definition
1465 (cp_parser *);
1466 static void cp_parser_namespace_body
1467 (cp_parser *);
1468 static tree cp_parser_qualified_namespace_specifier
1469 (cp_parser *);
1470 static void cp_parser_namespace_alias_definition
1471 (cp_parser *);
1472 static void cp_parser_using_declaration
1473 (cp_parser *);
1474 static void cp_parser_using_directive
1475 (cp_parser *);
1476 static void cp_parser_asm_definition
1477 (cp_parser *);
1478 static void cp_parser_linkage_specification
1479 (cp_parser *);
1481 /* Declarators [gram.dcl.decl] */
1483 static tree cp_parser_init_declarator
1484 (cp_parser *, tree, tree, bool, bool, int, bool *);
1485 static tree cp_parser_declarator
1486 (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1487 static tree cp_parser_direct_declarator
1488 (cp_parser *, cp_parser_declarator_kind, int *);
1489 static enum tree_code cp_parser_ptr_operator
1490 (cp_parser *, tree *, tree *);
1491 static tree cp_parser_cv_qualifier_seq_opt
1492 (cp_parser *);
1493 static tree cp_parser_cv_qualifier_opt
1494 (cp_parser *);
1495 static tree cp_parser_declarator_id
1496 (cp_parser *);
1497 static tree cp_parser_type_id
1498 (cp_parser *);
1499 static tree cp_parser_type_specifier_seq
1500 (cp_parser *);
1501 static tree cp_parser_parameter_declaration_clause
1502 (cp_parser *);
1503 static tree cp_parser_parameter_declaration_list
1504 (cp_parser *);
1505 static tree cp_parser_parameter_declaration
1506 (cp_parser *, bool, bool *);
1507 static void cp_parser_function_body
1508 (cp_parser *);
1509 static tree cp_parser_initializer
1510 (cp_parser *, bool *, bool *);
1511 static tree cp_parser_initializer_clause
1512 (cp_parser *, bool *);
1513 static tree cp_parser_initializer_list
1514 (cp_parser *, bool *);
1516 static bool cp_parser_ctor_initializer_opt_and_function_body
1517 (cp_parser *);
1519 /* Classes [gram.class] */
1521 static tree cp_parser_class_name
1522 (cp_parser *, bool, bool, bool, bool, bool, bool);
1523 static tree cp_parser_class_specifier
1524 (cp_parser *);
1525 static tree cp_parser_class_head
1526 (cp_parser *, bool *, tree *);
1527 static enum tag_types cp_parser_class_key
1528 (cp_parser *);
1529 static void cp_parser_member_specification_opt
1530 (cp_parser *);
1531 static void cp_parser_member_declaration
1532 (cp_parser *);
1533 static tree cp_parser_pure_specifier
1534 (cp_parser *);
1535 static tree cp_parser_constant_initializer
1536 (cp_parser *);
1538 /* Derived classes [gram.class.derived] */
1540 static tree cp_parser_base_clause
1541 (cp_parser *);
1542 static tree cp_parser_base_specifier
1543 (cp_parser *);
1545 /* Special member functions [gram.special] */
1547 static tree cp_parser_conversion_function_id
1548 (cp_parser *);
1549 static tree cp_parser_conversion_type_id
1550 (cp_parser *);
1551 static tree cp_parser_conversion_declarator_opt
1552 (cp_parser *);
1553 static bool cp_parser_ctor_initializer_opt
1554 (cp_parser *);
1555 static void cp_parser_mem_initializer_list
1556 (cp_parser *);
1557 static tree cp_parser_mem_initializer
1558 (cp_parser *);
1559 static tree cp_parser_mem_initializer_id
1560 (cp_parser *);
1562 /* Overloading [gram.over] */
1564 static tree cp_parser_operator_function_id
1565 (cp_parser *);
1566 static tree cp_parser_operator
1567 (cp_parser *);
1569 /* Templates [gram.temp] */
1571 static void cp_parser_template_declaration
1572 (cp_parser *, bool);
1573 static tree cp_parser_template_parameter_list
1574 (cp_parser *);
1575 static tree cp_parser_template_parameter
1576 (cp_parser *);
1577 static tree cp_parser_type_parameter
1578 (cp_parser *);
1579 static tree cp_parser_template_id
1580 (cp_parser *, bool, bool, bool);
1581 static tree cp_parser_template_name
1582 (cp_parser *, bool, bool, bool, bool *);
1583 static tree cp_parser_template_argument_list
1584 (cp_parser *);
1585 static tree cp_parser_template_argument
1586 (cp_parser *);
1587 static void cp_parser_explicit_instantiation
1588 (cp_parser *);
1589 static void cp_parser_explicit_specialization
1590 (cp_parser *);
1592 /* Exception handling [gram.exception] */
1594 static tree cp_parser_try_block
1595 (cp_parser *);
1596 static bool cp_parser_function_try_block
1597 (cp_parser *);
1598 static void cp_parser_handler_seq
1599 (cp_parser *);
1600 static void cp_parser_handler
1601 (cp_parser *);
1602 static tree cp_parser_exception_declaration
1603 (cp_parser *);
1604 static tree cp_parser_throw_expression
1605 (cp_parser *);
1606 static tree cp_parser_exception_specification_opt
1607 (cp_parser *);
1608 static tree cp_parser_type_id_list
1609 (cp_parser *);
1611 /* GNU Extensions */
1613 static tree cp_parser_asm_specification_opt
1614 (cp_parser *);
1615 static tree cp_parser_asm_operand_list
1616 (cp_parser *);
1617 static tree cp_parser_asm_clobber_list
1618 (cp_parser *);
1619 static tree cp_parser_attributes_opt
1620 (cp_parser *);
1621 static tree cp_parser_attribute_list
1622 (cp_parser *);
1623 static bool cp_parser_extension_opt
1624 (cp_parser *, int *);
1625 static void cp_parser_label_declaration
1626 (cp_parser *);
1628 /* Utility Routines */
1630 static tree cp_parser_lookup_name
1631 (cp_parser *, tree, bool, bool, bool, bool);
1632 static tree cp_parser_lookup_name_simple
1633 (cp_parser *, tree);
1634 static tree cp_parser_maybe_treat_template_as_class
1635 (tree, bool);
1636 static bool cp_parser_check_declarator_template_parameters
1637 (cp_parser *, tree);
1638 static bool cp_parser_check_template_parameters
1639 (cp_parser *, unsigned);
1640 static tree cp_parser_simple_cast_expression
1641 (cp_parser *);
1642 static tree cp_parser_binary_expression
1643 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1644 static tree cp_parser_global_scope_opt
1645 (cp_parser *, bool);
1646 static bool cp_parser_constructor_declarator_p
1647 (cp_parser *, bool);
1648 static tree cp_parser_function_definition_from_specifiers_and_declarator
1649 (cp_parser *, tree, tree, tree);
1650 static tree cp_parser_function_definition_after_declarator
1651 (cp_parser *, bool);
1652 static void cp_parser_template_declaration_after_export
1653 (cp_parser *, bool);
1654 static tree cp_parser_single_declaration
1655 (cp_parser *, bool, bool *);
1656 static tree cp_parser_functional_cast
1657 (cp_parser *, tree);
1658 static tree cp_parser_save_member_function_body
1659 (cp_parser *, tree, tree, tree);
1660 static tree cp_parser_enclosed_template_argument_list
1661 (cp_parser *);
1662 static void cp_parser_save_default_args
1663 (cp_parser *, tree);
1664 static void cp_parser_late_parsing_for_member
1665 (cp_parser *, tree);
1666 static void cp_parser_late_parsing_default_args
1667 (cp_parser *, tree);
1668 static tree cp_parser_sizeof_operand
1669 (cp_parser *, enum rid);
1670 static bool cp_parser_declares_only_class_p
1671 (cp_parser *);
1672 static bool cp_parser_friend_p
1673 (tree);
1674 static cp_token *cp_parser_require
1675 (cp_parser *, enum cpp_ttype, const char *);
1676 static cp_token *cp_parser_require_keyword
1677 (cp_parser *, enum rid, const char *);
1678 static bool cp_parser_token_starts_function_definition_p
1679 (cp_token *);
1680 static bool cp_parser_next_token_starts_class_definition_p
1681 (cp_parser *);
1682 static bool cp_parser_next_token_ends_template_argument_p
1683 (cp_parser *);
1684 static bool cp_parser_nth_token_starts_template_argument_list_p
1685 (cp_parser *, size_t);
1686 static enum tag_types cp_parser_token_is_class_key
1687 (cp_token *);
1688 static void cp_parser_check_class_key
1689 (enum tag_types, tree type);
1690 static void cp_parser_check_access_in_redeclaration
1691 (tree type);
1692 static bool cp_parser_optional_template_keyword
1693 (cp_parser *);
1694 static void cp_parser_pre_parsed_nested_name_specifier
1695 (cp_parser *);
1696 static void cp_parser_cache_group
1697 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1698 static void cp_parser_parse_tentatively
1699 (cp_parser *);
1700 static void cp_parser_commit_to_tentative_parse
1701 (cp_parser *);
1702 static void cp_parser_abort_tentative_parse
1703 (cp_parser *);
1704 static bool cp_parser_parse_definitely
1705 (cp_parser *);
1706 static inline bool cp_parser_parsing_tentatively
1707 (cp_parser *);
1708 static bool cp_parser_committed_to_tentative_parse
1709 (cp_parser *);
1710 static void cp_parser_error
1711 (cp_parser *, const char *);
1712 static void cp_parser_name_lookup_error
1713 (cp_parser *, tree, tree, const char *);
1714 static bool cp_parser_simulate_error
1715 (cp_parser *);
1716 static void cp_parser_check_type_definition
1717 (cp_parser *);
1718 static void cp_parser_check_for_definition_in_return_type
1719 (tree, int);
1720 static void cp_parser_check_for_invalid_template_id
1721 (cp_parser *, tree);
1722 static bool cp_parser_non_integral_constant_expression
1723 (cp_parser *, const char *);
1724 static bool cp_parser_diagnose_invalid_type_name
1725 (cp_parser *);
1726 static int cp_parser_skip_to_closing_parenthesis
1727 (cp_parser *, bool, bool, bool);
1728 static void cp_parser_skip_to_end_of_statement
1729 (cp_parser *);
1730 static void cp_parser_consume_semicolon_at_end_of_statement
1731 (cp_parser *);
1732 static void cp_parser_skip_to_end_of_block_or_statement
1733 (cp_parser *);
1734 static void cp_parser_skip_to_closing_brace
1735 (cp_parser *);
1736 static void cp_parser_skip_until_found
1737 (cp_parser *, enum cpp_ttype, const char *);
1738 static bool cp_parser_error_occurred
1739 (cp_parser *);
1740 static bool cp_parser_allow_gnu_extensions_p
1741 (cp_parser *);
1742 static bool cp_parser_is_string_literal
1743 (cp_token *);
1744 static bool cp_parser_is_keyword
1745 (cp_token *, enum rid);
1747 /* Returns nonzero if we are parsing tentatively. */
1749 static inline bool
1750 cp_parser_parsing_tentatively (cp_parser* parser)
1752 return parser->context->next != NULL;
1755 /* Returns nonzero if TOKEN is a string literal. */
1757 static bool
1758 cp_parser_is_string_literal (cp_token* token)
1760 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1763 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1765 static bool
1766 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1768 return token->keyword == keyword;
1771 /* Issue the indicated error MESSAGE. */
1773 static void
1774 cp_parser_error (cp_parser* parser, const char* message)
1776 /* Output the MESSAGE -- unless we're parsing tentatively. */
1777 if (!cp_parser_simulate_error (parser))
1779 cp_token *token;
1780 token = cp_lexer_peek_token (parser->lexer);
1781 c_parse_error (message,
1782 /* Because c_parser_error does not understand
1783 CPP_KEYWORD, keywords are treated like
1784 identifiers. */
1785 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1786 token->value);
1790 /* Issue an error about name-lookup failing. NAME is the
1791 IDENTIFIER_NODE DECL is the result of
1792 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1793 the thing that we hoped to find. */
1795 static void
1796 cp_parser_name_lookup_error (cp_parser* parser,
1797 tree name,
1798 tree decl,
1799 const char* desired)
1801 /* If name lookup completely failed, tell the user that NAME was not
1802 declared. */
1803 if (decl == error_mark_node)
1805 if (parser->scope && parser->scope != global_namespace)
1806 error ("`%D::%D' has not been declared",
1807 parser->scope, name);
1808 else if (parser->scope == global_namespace)
1809 error ("`::%D' has not been declared", name);
1810 else
1811 error ("`%D' has not been declared", name);
1813 else if (parser->scope && parser->scope != global_namespace)
1814 error ("`%D::%D' %s", parser->scope, name, desired);
1815 else if (parser->scope == global_namespace)
1816 error ("`::%D' %s", name, desired);
1817 else
1818 error ("`%D' %s", name, desired);
1821 /* If we are parsing tentatively, remember that an error has occurred
1822 during this tentative parse. Returns true if the error was
1823 simulated; false if a messgae should be issued by the caller. */
1825 static bool
1826 cp_parser_simulate_error (cp_parser* parser)
1828 if (cp_parser_parsing_tentatively (parser)
1829 && !cp_parser_committed_to_tentative_parse (parser))
1831 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1832 return true;
1834 return false;
1837 /* This function is called when a type is defined. If type
1838 definitions are forbidden at this point, an error message is
1839 issued. */
1841 static void
1842 cp_parser_check_type_definition (cp_parser* parser)
1844 /* If types are forbidden here, issue a message. */
1845 if (parser->type_definition_forbidden_message)
1846 /* Use `%s' to print the string in case there are any escape
1847 characters in the message. */
1848 error ("%s", parser->type_definition_forbidden_message);
1851 /* This function is called when a declaration is parsed. If
1852 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1853 indicates that a type was defined in the decl-specifiers for DECL,
1854 then an error is issued. */
1856 static void
1857 cp_parser_check_for_definition_in_return_type (tree declarator,
1858 int declares_class_or_enum)
1860 /* [dcl.fct] forbids type definitions in return types.
1861 Unfortunately, it's not easy to know whether or not we are
1862 processing a return type until after the fact. */
1863 while (declarator
1864 && (TREE_CODE (declarator) == INDIRECT_REF
1865 || TREE_CODE (declarator) == ADDR_EXPR))
1866 declarator = TREE_OPERAND (declarator, 0);
1867 if (declarator
1868 && TREE_CODE (declarator) == CALL_EXPR
1869 && declares_class_or_enum & 2)
1870 error ("new types may not be defined in a return type");
1873 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1874 "<" in any valid C++ program. If the next token is indeed "<",
1875 issue a message warning the user about what appears to be an
1876 invalid attempt to form a template-id. */
1878 static void
1879 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1880 tree type)
1882 ptrdiff_t start;
1883 cp_token *token;
1885 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1887 if (TYPE_P (type))
1888 error ("`%T' is not a template", type);
1889 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1890 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1891 else
1892 error ("invalid template-id");
1893 /* Remember the location of the invalid "<". */
1894 if (cp_parser_parsing_tentatively (parser)
1895 && !cp_parser_committed_to_tentative_parse (parser))
1897 token = cp_lexer_peek_token (parser->lexer);
1898 token = cp_lexer_prev_token (parser->lexer, token);
1899 start = cp_lexer_token_difference (parser->lexer,
1900 parser->lexer->first_token,
1901 token);
1903 else
1904 start = -1;
1905 /* Consume the "<". */
1906 cp_lexer_consume_token (parser->lexer);
1907 /* Parse the template arguments. */
1908 cp_parser_enclosed_template_argument_list (parser);
1909 /* Permanently remove the invalid template arguments so that
1910 this error message is not issued again. */
1911 if (start >= 0)
1913 token = cp_lexer_advance_token (parser->lexer,
1914 parser->lexer->first_token,
1915 start);
1916 cp_lexer_purge_tokens_after (parser->lexer, token);
1921 /* If parsing an integral constant-expression, issue an error message
1922 about the fact that THING appeared and return true. Otherwise,
1923 return false, marking the current expression as non-constant. */
1925 static bool
1926 cp_parser_non_integral_constant_expression (cp_parser *parser,
1927 const char *thing)
1929 if (parser->integral_constant_expression_p)
1931 if (!parser->allow_non_integral_constant_expression_p)
1933 error ("%s cannot appear in a constant-expression", thing);
1934 return true;
1936 parser->non_integral_constant_expression_p = true;
1938 return false;
1941 /* Check for a common situation where a type-name should be present,
1942 but is not, and issue a sensible error message. Returns true if an
1943 invalid type-name was detected. */
1945 static bool
1946 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1948 /* If the next two tokens are both identifiers, the code is
1949 erroneous. The usual cause of this situation is code like:
1951 T t;
1953 where "T" should name a type -- but does not. */
1954 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1955 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1957 tree name;
1959 /* If parsing tentatively, we should commit; we really are
1960 looking at a declaration. */
1961 /* Consume the first identifier. */
1962 name = cp_lexer_consume_token (parser->lexer)->value;
1963 /* Issue an error message. */
1964 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1965 /* If we're in a template class, it's possible that the user was
1966 referring to a type from a base class. For example:
1968 template <typename T> struct A { typedef T X; };
1969 template <typename T> struct B : public A<T> { X x; };
1971 The user should have said "typename A<T>::X". */
1972 if (processing_template_decl && current_class_type)
1974 tree b;
1976 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1978 b = TREE_CHAIN (b))
1980 tree base_type = BINFO_TYPE (b);
1981 if (CLASS_TYPE_P (base_type)
1982 && dependent_type_p (base_type))
1984 tree field;
1985 /* Go from a particular instantiation of the
1986 template (which will have an empty TYPE_FIELDs),
1987 to the main version. */
1988 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1989 for (field = TYPE_FIELDS (base_type);
1990 field;
1991 field = TREE_CHAIN (field))
1992 if (TREE_CODE (field) == TYPE_DECL
1993 && DECL_NAME (field) == name)
1995 error ("(perhaps `typename %T::%s' was intended)",
1996 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1997 break;
1999 if (field)
2000 break;
2004 /* Skip to the end of the declaration; there's no point in
2005 trying to process it. */
2006 cp_parser_skip_to_end_of_statement (parser);
2008 return true;
2011 return false;
2014 /* Consume tokens up to, and including, the next non-nested closing `)'.
2015 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2016 are doing error recovery. Returns -1 if OR_COMMA is true and we
2017 found an unnested comma. */
2019 static int
2020 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2021 bool recovering,
2022 bool or_comma,
2023 bool consume_paren)
2025 unsigned paren_depth = 0;
2026 unsigned brace_depth = 0;
2028 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2029 && !cp_parser_committed_to_tentative_parse (parser))
2030 return 0;
2032 while (true)
2034 cp_token *token;
2036 /* If we've run out of tokens, then there is no closing `)'. */
2037 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2038 return 0;
2040 token = cp_lexer_peek_token (parser->lexer);
2042 /* This matches the processing in skip_to_end_of_statement. */
2043 if (token->type == CPP_SEMICOLON && !brace_depth)
2044 return 0;
2045 if (token->type == CPP_OPEN_BRACE)
2046 ++brace_depth;
2047 if (token->type == CPP_CLOSE_BRACE)
2049 if (!brace_depth--)
2050 return 0;
2052 if (recovering && or_comma && token->type == CPP_COMMA
2053 && !brace_depth && !paren_depth)
2054 return -1;
2056 if (!brace_depth)
2058 /* If it is an `(', we have entered another level of nesting. */
2059 if (token->type == CPP_OPEN_PAREN)
2060 ++paren_depth;
2061 /* If it is a `)', then we might be done. */
2062 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2064 if (consume_paren)
2065 cp_lexer_consume_token (parser->lexer);
2066 return 1;
2070 /* Consume the token. */
2071 cp_lexer_consume_token (parser->lexer);
2075 /* Consume tokens until we reach the end of the current statement.
2076 Normally, that will be just before consuming a `;'. However, if a
2077 non-nested `}' comes first, then we stop before consuming that. */
2079 static void
2080 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2082 unsigned nesting_depth = 0;
2084 while (true)
2086 cp_token *token;
2088 /* Peek at the next token. */
2089 token = cp_lexer_peek_token (parser->lexer);
2090 /* If we've run out of tokens, stop. */
2091 if (token->type == CPP_EOF)
2092 break;
2093 /* If the next token is a `;', we have reached the end of the
2094 statement. */
2095 if (token->type == CPP_SEMICOLON && !nesting_depth)
2096 break;
2097 /* If the next token is a non-nested `}', then we have reached
2098 the end of the current block. */
2099 if (token->type == CPP_CLOSE_BRACE)
2101 /* If this is a non-nested `}', stop before consuming it.
2102 That way, when confronted with something like:
2104 { 3 + }
2106 we stop before consuming the closing `}', even though we
2107 have not yet reached a `;'. */
2108 if (nesting_depth == 0)
2109 break;
2110 /* If it is the closing `}' for a block that we have
2111 scanned, stop -- but only after consuming the token.
2112 That way given:
2114 void f g () { ... }
2115 typedef int I;
2117 we will stop after the body of the erroneously declared
2118 function, but before consuming the following `typedef'
2119 declaration. */
2120 if (--nesting_depth == 0)
2122 cp_lexer_consume_token (parser->lexer);
2123 break;
2126 /* If it the next token is a `{', then we are entering a new
2127 block. Consume the entire block. */
2128 else if (token->type == CPP_OPEN_BRACE)
2129 ++nesting_depth;
2130 /* Consume the token. */
2131 cp_lexer_consume_token (parser->lexer);
2135 /* This function is called at the end of a statement or declaration.
2136 If the next token is a semicolon, it is consumed; otherwise, error
2137 recovery is attempted. */
2139 static void
2140 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2142 /* Look for the trailing `;'. */
2143 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2145 /* If there is additional (erroneous) input, skip to the end of
2146 the statement. */
2147 cp_parser_skip_to_end_of_statement (parser);
2148 /* If the next token is now a `;', consume it. */
2149 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2150 cp_lexer_consume_token (parser->lexer);
2154 /* Skip tokens until we have consumed an entire block, or until we
2155 have consumed a non-nested `;'. */
2157 static void
2158 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2160 unsigned nesting_depth = 0;
2162 while (true)
2164 cp_token *token;
2166 /* Peek at the next token. */
2167 token = cp_lexer_peek_token (parser->lexer);
2168 /* If we've run out of tokens, stop. */
2169 if (token->type == CPP_EOF)
2170 break;
2171 /* If the next token is a `;', we have reached the end of the
2172 statement. */
2173 if (token->type == CPP_SEMICOLON && !nesting_depth)
2175 /* Consume the `;'. */
2176 cp_lexer_consume_token (parser->lexer);
2177 break;
2179 /* Consume the token. */
2180 token = cp_lexer_consume_token (parser->lexer);
2181 /* If the next token is a non-nested `}', then we have reached
2182 the end of the current block. */
2183 if (token->type == CPP_CLOSE_BRACE
2184 && (nesting_depth == 0 || --nesting_depth == 0))
2185 break;
2186 /* If it the next token is a `{', then we are entering a new
2187 block. Consume the entire block. */
2188 if (token->type == CPP_OPEN_BRACE)
2189 ++nesting_depth;
2193 /* Skip tokens until a non-nested closing curly brace is the next
2194 token. */
2196 static void
2197 cp_parser_skip_to_closing_brace (cp_parser *parser)
2199 unsigned nesting_depth = 0;
2201 while (true)
2203 cp_token *token;
2205 /* Peek at the next token. */
2206 token = cp_lexer_peek_token (parser->lexer);
2207 /* If we've run out of tokens, stop. */
2208 if (token->type == CPP_EOF)
2209 break;
2210 /* If the next token is a non-nested `}', then we have reached
2211 the end of the current block. */
2212 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2213 break;
2214 /* If it the next token is a `{', then we are entering a new
2215 block. Consume the entire block. */
2216 else if (token->type == CPP_OPEN_BRACE)
2217 ++nesting_depth;
2218 /* Consume the token. */
2219 cp_lexer_consume_token (parser->lexer);
2223 /* Create a new C++ parser. */
2225 static cp_parser *
2226 cp_parser_new (void)
2228 cp_parser *parser;
2229 cp_lexer *lexer;
2231 /* cp_lexer_new_main is called before calling ggc_alloc because
2232 cp_lexer_new_main might load a PCH file. */
2233 lexer = cp_lexer_new_main ();
2235 parser = ggc_alloc_cleared (sizeof (cp_parser));
2236 parser->lexer = lexer;
2237 parser->context = cp_parser_context_new (NULL);
2239 /* For now, we always accept GNU extensions. */
2240 parser->allow_gnu_extensions_p = 1;
2242 /* The `>' token is a greater-than operator, not the end of a
2243 template-id. */
2244 parser->greater_than_is_operator_p = true;
2246 parser->default_arg_ok_p = true;
2248 /* We are not parsing a constant-expression. */
2249 parser->integral_constant_expression_p = false;
2250 parser->allow_non_integral_constant_expression_p = false;
2251 parser->non_integral_constant_expression_p = false;
2253 /* We are not parsing offsetof. */
2254 parser->in_offsetof_p = false;
2256 /* Local variable names are not forbidden. */
2257 parser->local_variables_forbidden_p = false;
2259 /* We are not processing an `extern "C"' declaration. */
2260 parser->in_unbraced_linkage_specification_p = false;
2262 /* We are not processing a declarator. */
2263 parser->in_declarator_p = false;
2265 /* We are not processing a template-argument-list. */
2266 parser->in_template_argument_list_p = false;
2268 /* We are not in an iteration statement. */
2269 parser->in_iteration_statement_p = false;
2271 /* We are not in a switch statement. */
2272 parser->in_switch_statement_p = false;
2274 /* We are not parsing a type-id inside an expression. */
2275 parser->in_type_id_in_expr_p = false;
2277 /* The unparsed function queue is empty. */
2278 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2280 /* There are no classes being defined. */
2281 parser->num_classes_being_defined = 0;
2283 /* No template parameters apply. */
2284 parser->num_template_parameter_lists = 0;
2286 return parser;
2289 /* Lexical conventions [gram.lex] */
2291 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2292 identifier. */
2294 static tree
2295 cp_parser_identifier (cp_parser* parser)
2297 cp_token *token;
2299 /* Look for the identifier. */
2300 token = cp_parser_require (parser, CPP_NAME, "identifier");
2301 /* Return the value. */
2302 return token ? token->value : error_mark_node;
2305 /* Basic concepts [gram.basic] */
2307 /* Parse a translation-unit.
2309 translation-unit:
2310 declaration-seq [opt]
2312 Returns TRUE if all went well. */
2314 static bool
2315 cp_parser_translation_unit (cp_parser* parser)
2317 while (true)
2319 cp_parser_declaration_seq_opt (parser);
2321 /* If there are no tokens left then all went well. */
2322 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2323 break;
2325 /* Otherwise, issue an error message. */
2326 cp_parser_error (parser, "expected declaration");
2327 return false;
2330 /* Consume the EOF token. */
2331 cp_parser_require (parser, CPP_EOF, "end-of-file");
2333 /* Finish up. */
2334 finish_translation_unit ();
2336 /* All went well. */
2337 return true;
2340 /* Expressions [gram.expr] */
2342 /* Parse a primary-expression.
2344 primary-expression:
2345 literal
2346 this
2347 ( expression )
2348 id-expression
2350 GNU Extensions:
2352 primary-expression:
2353 ( compound-statement )
2354 __builtin_va_arg ( assignment-expression , type-id )
2356 literal:
2357 __null
2359 Returns a representation of the expression.
2361 *IDK indicates what kind of id-expression (if any) was present.
2363 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2364 used as the operand of a pointer-to-member. In that case,
2365 *QUALIFYING_CLASS gives the class that is used as the qualifying
2366 class in the pointer-to-member. */
2368 static tree
2369 cp_parser_primary_expression (cp_parser *parser,
2370 cp_id_kind *idk,
2371 tree *qualifying_class)
2373 cp_token *token;
2375 /* Assume the primary expression is not an id-expression. */
2376 *idk = CP_ID_KIND_NONE;
2377 /* And that it cannot be used as pointer-to-member. */
2378 *qualifying_class = NULL_TREE;
2380 /* Peek at the next token. */
2381 token = cp_lexer_peek_token (parser->lexer);
2382 switch (token->type)
2384 /* literal:
2385 integer-literal
2386 character-literal
2387 floating-literal
2388 string-literal
2389 boolean-literal */
2390 case CPP_CHAR:
2391 case CPP_WCHAR:
2392 case CPP_STRING:
2393 case CPP_WSTRING:
2394 case CPP_NUMBER:
2395 token = cp_lexer_consume_token (parser->lexer);
2396 return token->value;
2398 case CPP_OPEN_PAREN:
2400 tree expr;
2401 bool saved_greater_than_is_operator_p;
2403 /* Consume the `('. */
2404 cp_lexer_consume_token (parser->lexer);
2405 /* Within a parenthesized expression, a `>' token is always
2406 the greater-than operator. */
2407 saved_greater_than_is_operator_p
2408 = parser->greater_than_is_operator_p;
2409 parser->greater_than_is_operator_p = true;
2410 /* If we see `( { ' then we are looking at the beginning of
2411 a GNU statement-expression. */
2412 if (cp_parser_allow_gnu_extensions_p (parser)
2413 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2415 /* Statement-expressions are not allowed by the standard. */
2416 if (pedantic)
2417 pedwarn ("ISO C++ forbids braced-groups within expressions");
2419 /* And they're not allowed outside of a function-body; you
2420 cannot, for example, write:
2422 int i = ({ int j = 3; j + 1; });
2424 at class or namespace scope. */
2425 if (!at_function_scope_p ())
2426 error ("statement-expressions are allowed only inside functions");
2427 /* Start the statement-expression. */
2428 expr = begin_stmt_expr ();
2429 /* Parse the compound-statement. */
2430 cp_parser_compound_statement (parser, true);
2431 /* Finish up. */
2432 expr = finish_stmt_expr (expr, false);
2434 else
2436 /* Parse the parenthesized expression. */
2437 expr = cp_parser_expression (parser);
2438 /* Let the front end know that this expression was
2439 enclosed in parentheses. This matters in case, for
2440 example, the expression is of the form `A::B', since
2441 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2442 not. */
2443 finish_parenthesized_expr (expr);
2445 /* The `>' token might be the end of a template-id or
2446 template-parameter-list now. */
2447 parser->greater_than_is_operator_p
2448 = saved_greater_than_is_operator_p;
2449 /* Consume the `)'. */
2450 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2451 cp_parser_skip_to_end_of_statement (parser);
2453 return expr;
2456 case CPP_KEYWORD:
2457 switch (token->keyword)
2459 /* These two are the boolean literals. */
2460 case RID_TRUE:
2461 cp_lexer_consume_token (parser->lexer);
2462 return boolean_true_node;
2463 case RID_FALSE:
2464 cp_lexer_consume_token (parser->lexer);
2465 return boolean_false_node;
2467 /* The `__null' literal. */
2468 case RID_NULL:
2469 cp_lexer_consume_token (parser->lexer);
2470 return null_node;
2472 /* Recognize the `this' keyword. */
2473 case RID_THIS:
2474 cp_lexer_consume_token (parser->lexer);
2475 if (parser->local_variables_forbidden_p)
2477 error ("`this' may not be used in this context");
2478 return error_mark_node;
2480 /* Pointers cannot appear in constant-expressions. */
2481 if (cp_parser_non_integral_constant_expression (parser,
2482 "`this'"))
2483 return error_mark_node;
2484 return finish_this_expr ();
2486 /* The `operator' keyword can be the beginning of an
2487 id-expression. */
2488 case RID_OPERATOR:
2489 goto id_expression;
2491 case RID_FUNCTION_NAME:
2492 case RID_PRETTY_FUNCTION_NAME:
2493 case RID_C99_FUNCTION_NAME:
2494 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2495 __func__ are the names of variables -- but they are
2496 treated specially. Therefore, they are handled here,
2497 rather than relying on the generic id-expression logic
2498 below. Grammatically, these names are id-expressions.
2500 Consume the token. */
2501 token = cp_lexer_consume_token (parser->lexer);
2502 /* Look up the name. */
2503 return finish_fname (token->value);
2505 case RID_VA_ARG:
2507 tree expression;
2508 tree type;
2510 /* The `__builtin_va_arg' construct is used to handle
2511 `va_arg'. Consume the `__builtin_va_arg' token. */
2512 cp_lexer_consume_token (parser->lexer);
2513 /* Look for the opening `('. */
2514 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2515 /* Now, parse the assignment-expression. */
2516 expression = cp_parser_assignment_expression (parser);
2517 /* Look for the `,'. */
2518 cp_parser_require (parser, CPP_COMMA, "`,'");
2519 /* Parse the type-id. */
2520 type = cp_parser_type_id (parser);
2521 /* Look for the closing `)'. */
2522 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2523 /* Using `va_arg' in a constant-expression is not
2524 allowed. */
2525 if (cp_parser_non_integral_constant_expression (parser,
2526 "`va_arg'"))
2527 return error_mark_node;
2528 return build_x_va_arg (expression, type);
2531 case RID_OFFSETOF:
2533 tree expression;
2534 bool saved_in_offsetof_p;
2536 /* Consume the "__offsetof__" token. */
2537 cp_lexer_consume_token (parser->lexer);
2538 /* Consume the opening `('. */
2539 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2540 /* Parse the parenthesized (almost) constant-expression. */
2541 saved_in_offsetof_p = parser->in_offsetof_p;
2542 parser->in_offsetof_p = true;
2543 expression
2544 = cp_parser_constant_expression (parser,
2545 /*allow_non_constant_p=*/false,
2546 /*non_constant_p=*/NULL);
2547 parser->in_offsetof_p = saved_in_offsetof_p;
2548 /* Consume the closing ')'. */
2549 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2551 return expression;
2554 default:
2555 cp_parser_error (parser, "expected primary-expression");
2556 return error_mark_node;
2559 /* An id-expression can start with either an identifier, a
2560 `::' as the beginning of a qualified-id, or the "operator"
2561 keyword. */
2562 case CPP_NAME:
2563 case CPP_SCOPE:
2564 case CPP_TEMPLATE_ID:
2565 case CPP_NESTED_NAME_SPECIFIER:
2567 tree id_expression;
2568 tree decl;
2569 const char *error_msg;
2571 id_expression:
2572 /* Parse the id-expression. */
2573 id_expression
2574 = cp_parser_id_expression (parser,
2575 /*template_keyword_p=*/false,
2576 /*check_dependency_p=*/true,
2577 /*template_p=*/NULL,
2578 /*declarator_p=*/false);
2579 if (id_expression == error_mark_node)
2580 return error_mark_node;
2581 /* If we have a template-id, then no further lookup is
2582 required. If the template-id was for a template-class, we
2583 will sometimes have a TYPE_DECL at this point. */
2584 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2585 || TREE_CODE (id_expression) == TYPE_DECL)
2586 decl = id_expression;
2587 /* Look up the name. */
2588 else
2590 decl = cp_parser_lookup_name_simple (parser, id_expression);
2591 /* If name lookup gives us a SCOPE_REF, then the
2592 qualifying scope was dependent. Just propagate the
2593 name. */
2594 if (TREE_CODE (decl) == SCOPE_REF)
2596 if (TYPE_P (TREE_OPERAND (decl, 0)))
2597 *qualifying_class = TREE_OPERAND (decl, 0);
2598 return decl;
2600 /* Check to see if DECL is a local variable in a context
2601 where that is forbidden. */
2602 if (parser->local_variables_forbidden_p
2603 && local_variable_p (decl))
2605 /* It might be that we only found DECL because we are
2606 trying to be generous with pre-ISO scoping rules.
2607 For example, consider:
2609 int i;
2610 void g() {
2611 for (int i = 0; i < 10; ++i) {}
2612 extern void f(int j = i);
2615 Here, name look up will originally find the out
2616 of scope `i'. We need to issue a warning message,
2617 but then use the global `i'. */
2618 decl = check_for_out_of_scope_variable (decl);
2619 if (local_variable_p (decl))
2621 error ("local variable `%D' may not appear in this context",
2622 decl);
2623 return error_mark_node;
2628 decl = finish_id_expression (id_expression, decl, parser->scope,
2629 idk, qualifying_class,
2630 parser->integral_constant_expression_p,
2631 parser->allow_non_integral_constant_expression_p,
2632 &parser->non_integral_constant_expression_p,
2633 &error_msg);
2634 if (error_msg)
2635 cp_parser_error (parser, error_msg);
2636 return decl;
2639 /* Anything else is an error. */
2640 default:
2641 cp_parser_error (parser, "expected primary-expression");
2642 return error_mark_node;
2646 /* Parse an id-expression.
2648 id-expression:
2649 unqualified-id
2650 qualified-id
2652 qualified-id:
2653 :: [opt] nested-name-specifier template [opt] unqualified-id
2654 :: identifier
2655 :: operator-function-id
2656 :: template-id
2658 Return a representation of the unqualified portion of the
2659 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2660 a `::' or nested-name-specifier.
2662 Often, if the id-expression was a qualified-id, the caller will
2663 want to make a SCOPE_REF to represent the qualified-id. This
2664 function does not do this in order to avoid wastefully creating
2665 SCOPE_REFs when they are not required.
2667 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2668 `template' keyword.
2670 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2671 uninstantiated templates.
2673 If *TEMPLATE_P is non-NULL, it is set to true iff the
2674 `template' keyword is used to explicitly indicate that the entity
2675 named is a template.
2677 If DECLARATOR_P is true, the id-expression is appearing as part of
2678 a declarator, rather than as part of an expression. */
2680 static tree
2681 cp_parser_id_expression (cp_parser *parser,
2682 bool template_keyword_p,
2683 bool check_dependency_p,
2684 bool *template_p,
2685 bool declarator_p)
2687 bool global_scope_p;
2688 bool nested_name_specifier_p;
2690 /* Assume the `template' keyword was not used. */
2691 if (template_p)
2692 *template_p = false;
2694 /* Look for the optional `::' operator. */
2695 global_scope_p
2696 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2697 != NULL_TREE);
2698 /* Look for the optional nested-name-specifier. */
2699 nested_name_specifier_p
2700 = (cp_parser_nested_name_specifier_opt (parser,
2701 /*typename_keyword_p=*/false,
2702 check_dependency_p,
2703 /*type_p=*/false,
2704 /*is_declarator=*/false)
2705 != NULL_TREE);
2706 /* If there is a nested-name-specifier, then we are looking at
2707 the first qualified-id production. */
2708 if (nested_name_specifier_p)
2710 tree saved_scope;
2711 tree saved_object_scope;
2712 tree saved_qualifying_scope;
2713 tree unqualified_id;
2714 bool is_template;
2716 /* See if the next token is the `template' keyword. */
2717 if (!template_p)
2718 template_p = &is_template;
2719 *template_p = cp_parser_optional_template_keyword (parser);
2720 /* Name lookup we do during the processing of the
2721 unqualified-id might obliterate SCOPE. */
2722 saved_scope = parser->scope;
2723 saved_object_scope = parser->object_scope;
2724 saved_qualifying_scope = parser->qualifying_scope;
2725 /* Process the final unqualified-id. */
2726 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2727 check_dependency_p,
2728 declarator_p);
2729 /* Restore the SAVED_SCOPE for our caller. */
2730 parser->scope = saved_scope;
2731 parser->object_scope = saved_object_scope;
2732 parser->qualifying_scope = saved_qualifying_scope;
2734 return unqualified_id;
2736 /* Otherwise, if we are in global scope, then we are looking at one
2737 of the other qualified-id productions. */
2738 else if (global_scope_p)
2740 cp_token *token;
2741 tree id;
2743 /* Peek at the next token. */
2744 token = cp_lexer_peek_token (parser->lexer);
2746 /* If it's an identifier, and the next token is not a "<", then
2747 we can avoid the template-id case. This is an optimization
2748 for this common case. */
2749 if (token->type == CPP_NAME
2750 && !cp_parser_nth_token_starts_template_argument_list_p
2751 (parser, 2))
2752 return cp_parser_identifier (parser);
2754 cp_parser_parse_tentatively (parser);
2755 /* Try a template-id. */
2756 id = cp_parser_template_id (parser,
2757 /*template_keyword_p=*/false,
2758 /*check_dependency_p=*/true,
2759 declarator_p);
2760 /* If that worked, we're done. */
2761 if (cp_parser_parse_definitely (parser))
2762 return id;
2764 /* Peek at the next token. (Changes in the token buffer may
2765 have invalidated the pointer obtained above.) */
2766 token = cp_lexer_peek_token (parser->lexer);
2768 switch (token->type)
2770 case CPP_NAME:
2771 return cp_parser_identifier (parser);
2773 case CPP_KEYWORD:
2774 if (token->keyword == RID_OPERATOR)
2775 return cp_parser_operator_function_id (parser);
2776 /* Fall through. */
2778 default:
2779 cp_parser_error (parser, "expected id-expression");
2780 return error_mark_node;
2783 else
2784 return cp_parser_unqualified_id (parser, template_keyword_p,
2785 /*check_dependency_p=*/true,
2786 declarator_p);
2789 /* Parse an unqualified-id.
2791 unqualified-id:
2792 identifier
2793 operator-function-id
2794 conversion-function-id
2795 ~ class-name
2796 template-id
2798 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2799 keyword, in a construct like `A::template ...'.
2801 Returns a representation of unqualified-id. For the `identifier'
2802 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2803 production a BIT_NOT_EXPR is returned; the operand of the
2804 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2805 other productions, see the documentation accompanying the
2806 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2807 names are looked up in uninstantiated templates. If DECLARATOR_P
2808 is true, the unqualified-id is appearing as part of a declarator,
2809 rather than as part of an expression. */
2811 static tree
2812 cp_parser_unqualified_id (cp_parser* parser,
2813 bool template_keyword_p,
2814 bool check_dependency_p,
2815 bool declarator_p)
2817 cp_token *token;
2819 /* Peek at the next token. */
2820 token = cp_lexer_peek_token (parser->lexer);
2822 switch (token->type)
2824 case CPP_NAME:
2826 tree id;
2828 /* We don't know yet whether or not this will be a
2829 template-id. */
2830 cp_parser_parse_tentatively (parser);
2831 /* Try a template-id. */
2832 id = cp_parser_template_id (parser, template_keyword_p,
2833 check_dependency_p,
2834 declarator_p);
2835 /* If it worked, we're done. */
2836 if (cp_parser_parse_definitely (parser))
2837 return id;
2838 /* Otherwise, it's an ordinary identifier. */
2839 return cp_parser_identifier (parser);
2842 case CPP_TEMPLATE_ID:
2843 return cp_parser_template_id (parser, template_keyword_p,
2844 check_dependency_p,
2845 declarator_p);
2847 case CPP_COMPL:
2849 tree type_decl;
2850 tree qualifying_scope;
2851 tree object_scope;
2852 tree scope;
2854 /* Consume the `~' token. */
2855 cp_lexer_consume_token (parser->lexer);
2856 /* Parse the class-name. The standard, as written, seems to
2857 say that:
2859 template <typename T> struct S { ~S (); };
2860 template <typename T> S<T>::~S() {}
2862 is invalid, since `~' must be followed by a class-name, but
2863 `S<T>' is dependent, and so not known to be a class.
2864 That's not right; we need to look in uninstantiated
2865 templates. A further complication arises from:
2867 template <typename T> void f(T t) {
2868 t.T::~T();
2871 Here, it is not possible to look up `T' in the scope of `T'
2872 itself. We must look in both the current scope, and the
2873 scope of the containing complete expression.
2875 Yet another issue is:
2877 struct S {
2878 int S;
2879 ~S();
2882 S::~S() {}
2884 The standard does not seem to say that the `S' in `~S'
2885 should refer to the type `S' and not the data member
2886 `S::S'. */
2888 /* DR 244 says that we look up the name after the "~" in the
2889 same scope as we looked up the qualifying name. That idea
2890 isn't fully worked out; it's more complicated than that. */
2891 scope = parser->scope;
2892 object_scope = parser->object_scope;
2893 qualifying_scope = parser->qualifying_scope;
2895 /* If the name is of the form "X::~X" it's OK. */
2896 if (scope && TYPE_P (scope)
2897 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2898 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2899 == CPP_OPEN_PAREN)
2900 && (cp_lexer_peek_token (parser->lexer)->value
2901 == TYPE_IDENTIFIER (scope)))
2903 cp_lexer_consume_token (parser->lexer);
2904 return build_nt (BIT_NOT_EXPR, scope);
2907 /* If there was an explicit qualification (S::~T), first look
2908 in the scope given by the qualification (i.e., S). */
2909 if (scope)
2911 cp_parser_parse_tentatively (parser);
2912 type_decl = cp_parser_class_name (parser,
2913 /*typename_keyword_p=*/false,
2914 /*template_keyword_p=*/false,
2915 /*type_p=*/false,
2916 /*check_dependency=*/false,
2917 /*class_head_p=*/false,
2918 declarator_p);
2919 if (cp_parser_parse_definitely (parser))
2920 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2922 /* In "N::S::~S", look in "N" as well. */
2923 if (scope && qualifying_scope)
2925 cp_parser_parse_tentatively (parser);
2926 parser->scope = qualifying_scope;
2927 parser->object_scope = NULL_TREE;
2928 parser->qualifying_scope = NULL_TREE;
2929 type_decl
2930 = cp_parser_class_name (parser,
2931 /*typename_keyword_p=*/false,
2932 /*template_keyword_p=*/false,
2933 /*type_p=*/false,
2934 /*check_dependency=*/false,
2935 /*class_head_p=*/false,
2936 declarator_p);
2937 if (cp_parser_parse_definitely (parser))
2938 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2940 /* In "p->S::~T", look in the scope given by "*p" as well. */
2941 else if (object_scope)
2943 cp_parser_parse_tentatively (parser);
2944 parser->scope = object_scope;
2945 parser->object_scope = NULL_TREE;
2946 parser->qualifying_scope = NULL_TREE;
2947 type_decl
2948 = cp_parser_class_name (parser,
2949 /*typename_keyword_p=*/false,
2950 /*template_keyword_p=*/false,
2951 /*type_p=*/false,
2952 /*check_dependency=*/false,
2953 /*class_head_p=*/false,
2954 declarator_p);
2955 if (cp_parser_parse_definitely (parser))
2956 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2958 /* Look in the surrounding context. */
2959 parser->scope = NULL_TREE;
2960 parser->object_scope = NULL_TREE;
2961 parser->qualifying_scope = NULL_TREE;
2962 type_decl
2963 = cp_parser_class_name (parser,
2964 /*typename_keyword_p=*/false,
2965 /*template_keyword_p=*/false,
2966 /*type_p=*/false,
2967 /*check_dependency=*/false,
2968 /*class_head_p=*/false,
2969 declarator_p);
2970 /* If an error occurred, assume that the name of the
2971 destructor is the same as the name of the qualifying
2972 class. That allows us to keep parsing after running
2973 into ill-formed destructor names. */
2974 if (type_decl == error_mark_node && scope && TYPE_P (scope))
2975 return build_nt (BIT_NOT_EXPR, scope);
2976 else if (type_decl == error_mark_node)
2977 return error_mark_node;
2979 /* [class.dtor]
2981 A typedef-name that names a class shall not be used as the
2982 identifier in the declarator for a destructor declaration. */
2983 if (declarator_p
2984 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2985 && !DECL_SELF_REFERENCE_P (type_decl))
2986 error ("typedef-name `%D' used as destructor declarator",
2987 type_decl);
2989 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2992 case CPP_KEYWORD:
2993 if (token->keyword == RID_OPERATOR)
2995 tree id;
2997 /* This could be a template-id, so we try that first. */
2998 cp_parser_parse_tentatively (parser);
2999 /* Try a template-id. */
3000 id = cp_parser_template_id (parser, template_keyword_p,
3001 /*check_dependency_p=*/true,
3002 declarator_p);
3003 /* If that worked, we're done. */
3004 if (cp_parser_parse_definitely (parser))
3005 return id;
3006 /* We still don't know whether we're looking at an
3007 operator-function-id or a conversion-function-id. */
3008 cp_parser_parse_tentatively (parser);
3009 /* Try an operator-function-id. */
3010 id = cp_parser_operator_function_id (parser);
3011 /* If that didn't work, try a conversion-function-id. */
3012 if (!cp_parser_parse_definitely (parser))
3013 id = cp_parser_conversion_function_id (parser);
3015 return id;
3017 /* Fall through. */
3019 default:
3020 cp_parser_error (parser, "expected unqualified-id");
3021 return error_mark_node;
3025 /* Parse an (optional) nested-name-specifier.
3027 nested-name-specifier:
3028 class-or-namespace-name :: nested-name-specifier [opt]
3029 class-or-namespace-name :: template nested-name-specifier [opt]
3031 PARSER->SCOPE should be set appropriately before this function is
3032 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3033 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3034 in name lookups.
3036 Sets PARSER->SCOPE to the class (TYPE) or namespace
3037 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3038 it unchanged if there is no nested-name-specifier. Returns the new
3039 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3041 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3042 part of a declaration and/or decl-specifier. */
3044 static tree
3045 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3046 bool typename_keyword_p,
3047 bool check_dependency_p,
3048 bool type_p,
3049 bool is_declaration)
3051 bool success = false;
3052 tree access_check = NULL_TREE;
3053 ptrdiff_t start;
3054 cp_token* token;
3056 /* If the next token corresponds to a nested name specifier, there
3057 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3058 false, it may have been true before, in which case something
3059 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3060 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3061 CHECK_DEPENDENCY_P is false, we have to fall through into the
3062 main loop. */
3063 if (check_dependency_p
3064 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3066 cp_parser_pre_parsed_nested_name_specifier (parser);
3067 return parser->scope;
3070 /* Remember where the nested-name-specifier starts. */
3071 if (cp_parser_parsing_tentatively (parser)
3072 && !cp_parser_committed_to_tentative_parse (parser))
3074 token = cp_lexer_peek_token (parser->lexer);
3075 start = cp_lexer_token_difference (parser->lexer,
3076 parser->lexer->first_token,
3077 token);
3079 else
3080 start = -1;
3082 push_deferring_access_checks (dk_deferred);
3084 while (true)
3086 tree new_scope;
3087 tree old_scope;
3088 tree saved_qualifying_scope;
3089 bool template_keyword_p;
3091 /* Spot cases that cannot be the beginning of a
3092 nested-name-specifier. */
3093 token = cp_lexer_peek_token (parser->lexer);
3095 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3096 the already parsed nested-name-specifier. */
3097 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3099 /* Grab the nested-name-specifier and continue the loop. */
3100 cp_parser_pre_parsed_nested_name_specifier (parser);
3101 success = true;
3102 continue;
3105 /* Spot cases that cannot be the beginning of a
3106 nested-name-specifier. On the second and subsequent times
3107 through the loop, we look for the `template' keyword. */
3108 if (success && token->keyword == RID_TEMPLATE)
3110 /* A template-id can start a nested-name-specifier. */
3111 else if (token->type == CPP_TEMPLATE_ID)
3113 else
3115 /* If the next token is not an identifier, then it is
3116 definitely not a class-or-namespace-name. */
3117 if (token->type != CPP_NAME)
3118 break;
3119 /* If the following token is neither a `<' (to begin a
3120 template-id), nor a `::', then we are not looking at a
3121 nested-name-specifier. */
3122 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3123 if (token->type != CPP_SCOPE
3124 && !cp_parser_nth_token_starts_template_argument_list_p
3125 (parser, 2))
3126 break;
3129 /* The nested-name-specifier is optional, so we parse
3130 tentatively. */
3131 cp_parser_parse_tentatively (parser);
3133 /* Look for the optional `template' keyword, if this isn't the
3134 first time through the loop. */
3135 if (success)
3136 template_keyword_p = cp_parser_optional_template_keyword (parser);
3137 else
3138 template_keyword_p = false;
3140 /* Save the old scope since the name lookup we are about to do
3141 might destroy it. */
3142 old_scope = parser->scope;
3143 saved_qualifying_scope = parser->qualifying_scope;
3144 /* Parse the qualifying entity. */
3145 new_scope
3146 = cp_parser_class_or_namespace_name (parser,
3147 typename_keyword_p,
3148 template_keyword_p,
3149 check_dependency_p,
3150 type_p,
3151 is_declaration);
3152 /* Look for the `::' token. */
3153 cp_parser_require (parser, CPP_SCOPE, "`::'");
3155 /* If we found what we wanted, we keep going; otherwise, we're
3156 done. */
3157 if (!cp_parser_parse_definitely (parser))
3159 bool error_p = false;
3161 /* Restore the OLD_SCOPE since it was valid before the
3162 failed attempt at finding the last
3163 class-or-namespace-name. */
3164 parser->scope = old_scope;
3165 parser->qualifying_scope = saved_qualifying_scope;
3166 /* If the next token is an identifier, and the one after
3167 that is a `::', then any valid interpretation would have
3168 found a class-or-namespace-name. */
3169 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3170 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3171 == CPP_SCOPE)
3172 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3173 != CPP_COMPL))
3175 token = cp_lexer_consume_token (parser->lexer);
3176 if (!error_p)
3178 tree decl;
3180 decl = cp_parser_lookup_name_simple (parser, token->value);
3181 if (TREE_CODE (decl) == TEMPLATE_DECL)
3182 error ("`%D' used without template parameters",
3183 decl);
3184 else
3185 cp_parser_name_lookup_error
3186 (parser, token->value, decl,
3187 "is not a class or namespace");
3188 parser->scope = NULL_TREE;
3189 error_p = true;
3190 /* Treat this as a successful nested-name-specifier
3191 due to:
3193 [basic.lookup.qual]
3195 If the name found is not a class-name (clause
3196 _class_) or namespace-name (_namespace.def_), the
3197 program is ill-formed. */
3198 success = true;
3200 cp_lexer_consume_token (parser->lexer);
3202 break;
3205 /* We've found one valid nested-name-specifier. */
3206 success = true;
3207 /* Make sure we look in the right scope the next time through
3208 the loop. */
3209 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3210 ? TREE_TYPE (new_scope)
3211 : new_scope);
3212 /* If it is a class scope, try to complete it; we are about to
3213 be looking up names inside the class. */
3214 if (TYPE_P (parser->scope)
3215 /* Since checking types for dependency can be expensive,
3216 avoid doing it if the type is already complete. */
3217 && !COMPLETE_TYPE_P (parser->scope)
3218 /* Do not try to complete dependent types. */
3219 && !dependent_type_p (parser->scope))
3220 complete_type (parser->scope);
3223 /* Retrieve any deferred checks. Do not pop this access checks yet
3224 so the memory will not be reclaimed during token replacing below. */
3225 access_check = get_deferred_access_checks ();
3227 /* If parsing tentatively, replace the sequence of tokens that makes
3228 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3229 token. That way, should we re-parse the token stream, we will
3230 not have to repeat the effort required to do the parse, nor will
3231 we issue duplicate error messages. */
3232 if (success && start >= 0)
3234 /* Find the token that corresponds to the start of the
3235 template-id. */
3236 token = cp_lexer_advance_token (parser->lexer,
3237 parser->lexer->first_token,
3238 start);
3240 /* Reset the contents of the START token. */
3241 token->type = CPP_NESTED_NAME_SPECIFIER;
3242 token->value = build_tree_list (access_check, parser->scope);
3243 TREE_TYPE (token->value) = parser->qualifying_scope;
3244 token->keyword = RID_MAX;
3245 /* Purge all subsequent tokens. */
3246 cp_lexer_purge_tokens_after (parser->lexer, token);
3249 pop_deferring_access_checks ();
3250 return success ? parser->scope : NULL_TREE;
3253 /* Parse a nested-name-specifier. See
3254 cp_parser_nested_name_specifier_opt for details. This function
3255 behaves identically, except that it will an issue an error if no
3256 nested-name-specifier is present, and it will return
3257 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3258 is present. */
3260 static tree
3261 cp_parser_nested_name_specifier (cp_parser *parser,
3262 bool typename_keyword_p,
3263 bool check_dependency_p,
3264 bool type_p,
3265 bool is_declaration)
3267 tree scope;
3269 /* Look for the nested-name-specifier. */
3270 scope = cp_parser_nested_name_specifier_opt (parser,
3271 typename_keyword_p,
3272 check_dependency_p,
3273 type_p,
3274 is_declaration);
3275 /* If it was not present, issue an error message. */
3276 if (!scope)
3278 cp_parser_error (parser, "expected nested-name-specifier");
3279 parser->scope = NULL_TREE;
3280 return error_mark_node;
3283 return scope;
3286 /* Parse a class-or-namespace-name.
3288 class-or-namespace-name:
3289 class-name
3290 namespace-name
3292 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3293 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3294 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3295 TYPE_P is TRUE iff the next name should be taken as a class-name,
3296 even the same name is declared to be another entity in the same
3297 scope.
3299 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3300 specified by the class-or-namespace-name. If neither is found the
3301 ERROR_MARK_NODE is returned. */
3303 static tree
3304 cp_parser_class_or_namespace_name (cp_parser *parser,
3305 bool typename_keyword_p,
3306 bool template_keyword_p,
3307 bool check_dependency_p,
3308 bool type_p,
3309 bool is_declaration)
3311 tree saved_scope;
3312 tree saved_qualifying_scope;
3313 tree saved_object_scope;
3314 tree scope;
3315 bool only_class_p;
3317 /* Before we try to parse the class-name, we must save away the
3318 current PARSER->SCOPE since cp_parser_class_name will destroy
3319 it. */
3320 saved_scope = parser->scope;
3321 saved_qualifying_scope = parser->qualifying_scope;
3322 saved_object_scope = parser->object_scope;
3323 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3324 there is no need to look for a namespace-name. */
3325 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3326 if (!only_class_p)
3327 cp_parser_parse_tentatively (parser);
3328 scope = cp_parser_class_name (parser,
3329 typename_keyword_p,
3330 template_keyword_p,
3331 type_p,
3332 check_dependency_p,
3333 /*class_head_p=*/false,
3334 is_declaration);
3335 /* If that didn't work, try for a namespace-name. */
3336 if (!only_class_p && !cp_parser_parse_definitely (parser))
3338 /* Restore the saved scope. */
3339 parser->scope = saved_scope;
3340 parser->qualifying_scope = saved_qualifying_scope;
3341 parser->object_scope = saved_object_scope;
3342 /* If we are not looking at an identifier followed by the scope
3343 resolution operator, then this is not part of a
3344 nested-name-specifier. (Note that this function is only used
3345 to parse the components of a nested-name-specifier.) */
3346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3347 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3348 return error_mark_node;
3349 scope = cp_parser_namespace_name (parser);
3352 return scope;
3355 /* Parse a postfix-expression.
3357 postfix-expression:
3358 primary-expression
3359 postfix-expression [ expression ]
3360 postfix-expression ( expression-list [opt] )
3361 simple-type-specifier ( expression-list [opt] )
3362 typename :: [opt] nested-name-specifier identifier
3363 ( expression-list [opt] )
3364 typename :: [opt] nested-name-specifier template [opt] template-id
3365 ( expression-list [opt] )
3366 postfix-expression . template [opt] id-expression
3367 postfix-expression -> template [opt] id-expression
3368 postfix-expression . pseudo-destructor-name
3369 postfix-expression -> pseudo-destructor-name
3370 postfix-expression ++
3371 postfix-expression --
3372 dynamic_cast < type-id > ( expression )
3373 static_cast < type-id > ( expression )
3374 reinterpret_cast < type-id > ( expression )
3375 const_cast < type-id > ( expression )
3376 typeid ( expression )
3377 typeid ( type-id )
3379 GNU Extension:
3381 postfix-expression:
3382 ( type-id ) { initializer-list , [opt] }
3384 This extension is a GNU version of the C99 compound-literal
3385 construct. (The C99 grammar uses `type-name' instead of `type-id',
3386 but they are essentially the same concept.)
3388 If ADDRESS_P is true, the postfix expression is the operand of the
3389 `&' operator.
3391 Returns a representation of the expression. */
3393 static tree
3394 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3396 cp_token *token;
3397 enum rid keyword;
3398 cp_id_kind idk = CP_ID_KIND_NONE;
3399 tree postfix_expression = NULL_TREE;
3400 /* Non-NULL only if the current postfix-expression can be used to
3401 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3402 class used to qualify the member. */
3403 tree qualifying_class = NULL_TREE;
3405 /* Peek at the next token. */
3406 token = cp_lexer_peek_token (parser->lexer);
3407 /* Some of the productions are determined by keywords. */
3408 keyword = token->keyword;
3409 switch (keyword)
3411 case RID_DYNCAST:
3412 case RID_STATCAST:
3413 case RID_REINTCAST:
3414 case RID_CONSTCAST:
3416 tree type;
3417 tree expression;
3418 const char *saved_message;
3420 /* All of these can be handled in the same way from the point
3421 of view of parsing. Begin by consuming the token
3422 identifying the cast. */
3423 cp_lexer_consume_token (parser->lexer);
3425 /* New types cannot be defined in the cast. */
3426 saved_message = parser->type_definition_forbidden_message;
3427 parser->type_definition_forbidden_message
3428 = "types may not be defined in casts";
3430 /* Look for the opening `<'. */
3431 cp_parser_require (parser, CPP_LESS, "`<'");
3432 /* Parse the type to which we are casting. */
3433 type = cp_parser_type_id (parser);
3434 /* Look for the closing `>'. */
3435 cp_parser_require (parser, CPP_GREATER, "`>'");
3436 /* Restore the old message. */
3437 parser->type_definition_forbidden_message = saved_message;
3439 /* And the expression which is being cast. */
3440 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3441 expression = cp_parser_expression (parser);
3442 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3444 /* Only type conversions to integral or enumeration types
3445 can be used in constant-expressions. */
3446 if (parser->integral_constant_expression_p
3447 && !dependent_type_p (type)
3448 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3449 /* A cast to pointer or reference type is allowed in the
3450 implementation of "offsetof". */
3451 && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3452 && (cp_parser_non_integral_constant_expression
3453 (parser,
3454 "a cast to a type other than an integral or "
3455 "enumeration type")))
3456 return error_mark_node;
3458 switch (keyword)
3460 case RID_DYNCAST:
3461 postfix_expression
3462 = build_dynamic_cast (type, expression);
3463 break;
3464 case RID_STATCAST:
3465 postfix_expression
3466 = build_static_cast (type, expression);
3467 break;
3468 case RID_REINTCAST:
3469 postfix_expression
3470 = build_reinterpret_cast (type, expression);
3471 break;
3472 case RID_CONSTCAST:
3473 postfix_expression
3474 = build_const_cast (type, expression);
3475 break;
3476 default:
3477 abort ();
3480 break;
3482 case RID_TYPEID:
3484 tree type;
3485 const char *saved_message;
3486 bool saved_in_type_id_in_expr_p;
3488 /* Consume the `typeid' token. */
3489 cp_lexer_consume_token (parser->lexer);
3490 /* Look for the `(' token. */
3491 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3492 /* Types cannot be defined in a `typeid' expression. */
3493 saved_message = parser->type_definition_forbidden_message;
3494 parser->type_definition_forbidden_message
3495 = "types may not be defined in a `typeid\' expression";
3496 /* We can't be sure yet whether we're looking at a type-id or an
3497 expression. */
3498 cp_parser_parse_tentatively (parser);
3499 /* Try a type-id first. */
3500 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3501 parser->in_type_id_in_expr_p = true;
3502 type = cp_parser_type_id (parser);
3503 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3504 /* Look for the `)' token. Otherwise, we can't be sure that
3505 we're not looking at an expression: consider `typeid (int
3506 (3))', for example. */
3507 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3508 /* If all went well, simply lookup the type-id. */
3509 if (cp_parser_parse_definitely (parser))
3510 postfix_expression = get_typeid (type);
3511 /* Otherwise, fall back to the expression variant. */
3512 else
3514 tree expression;
3516 /* Look for an expression. */
3517 expression = cp_parser_expression (parser);
3518 /* Compute its typeid. */
3519 postfix_expression = build_typeid (expression);
3520 /* Look for the `)' token. */
3521 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3523 /* `typeid' may not appear in an integral constant expression. */
3524 if (cp_parser_non_integral_constant_expression(parser,
3525 "`typeid' operator"))
3526 return error_mark_node;
3527 /* Restore the saved message. */
3528 parser->type_definition_forbidden_message = saved_message;
3530 break;
3532 case RID_TYPENAME:
3534 bool template_p = false;
3535 tree id;
3536 tree type;
3538 /* Consume the `typename' token. */
3539 cp_lexer_consume_token (parser->lexer);
3540 /* Look for the optional `::' operator. */
3541 cp_parser_global_scope_opt (parser,
3542 /*current_scope_valid_p=*/false);
3543 /* Look for the nested-name-specifier. */
3544 cp_parser_nested_name_specifier (parser,
3545 /*typename_keyword_p=*/true,
3546 /*check_dependency_p=*/true,
3547 /*type_p=*/true,
3548 /*is_declaration=*/true);
3549 /* Look for the optional `template' keyword. */
3550 template_p = cp_parser_optional_template_keyword (parser);
3551 /* We don't know whether we're looking at a template-id or an
3552 identifier. */
3553 cp_parser_parse_tentatively (parser);
3554 /* Try a template-id. */
3555 id = cp_parser_template_id (parser, template_p,
3556 /*check_dependency_p=*/true,
3557 /*is_declaration=*/true);
3558 /* If that didn't work, try an identifier. */
3559 if (!cp_parser_parse_definitely (parser))
3560 id = cp_parser_identifier (parser);
3561 /* If we look up a template-id in a non-dependent qualifying
3562 scope, there's no need to create a dependent type. */
3563 if (TREE_CODE (id) == TYPE_DECL
3564 && !dependent_type_p (parser->scope))
3565 type = TREE_TYPE (id);
3566 /* Create a TYPENAME_TYPE to represent the type to which the
3567 functional cast is being performed. */
3568 else
3569 type = make_typename_type (parser->scope, id,
3570 /*complain=*/1);
3572 postfix_expression = cp_parser_functional_cast (parser, type);
3574 break;
3576 default:
3578 tree type;
3580 /* If the next thing is a simple-type-specifier, we may be
3581 looking at a functional cast. We could also be looking at
3582 an id-expression. So, we try the functional cast, and if
3583 that doesn't work we fall back to the primary-expression. */
3584 cp_parser_parse_tentatively (parser);
3585 /* Look for the simple-type-specifier. */
3586 type = cp_parser_simple_type_specifier (parser,
3587 CP_PARSER_FLAGS_NONE,
3588 /*identifier_p=*/false);
3589 /* Parse the cast itself. */
3590 if (!cp_parser_error_occurred (parser))
3591 postfix_expression
3592 = cp_parser_functional_cast (parser, type);
3593 /* If that worked, we're done. */
3594 if (cp_parser_parse_definitely (parser))
3595 break;
3597 /* If the functional-cast didn't work out, try a
3598 compound-literal. */
3599 if (cp_parser_allow_gnu_extensions_p (parser)
3600 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3602 tree initializer_list = NULL_TREE;
3603 bool saved_in_type_id_in_expr_p;
3605 cp_parser_parse_tentatively (parser);
3606 /* Consume the `('. */
3607 cp_lexer_consume_token (parser->lexer);
3608 /* Parse the type. */
3609 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3610 parser->in_type_id_in_expr_p = true;
3611 type = cp_parser_type_id (parser);
3612 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3613 /* Look for the `)'. */
3614 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3615 /* Look for the `{'. */
3616 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3617 /* If things aren't going well, there's no need to
3618 keep going. */
3619 if (!cp_parser_error_occurred (parser))
3621 bool non_constant_p;
3622 /* Parse the initializer-list. */
3623 initializer_list
3624 = cp_parser_initializer_list (parser, &non_constant_p);
3625 /* Allow a trailing `,'. */
3626 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3627 cp_lexer_consume_token (parser->lexer);
3628 /* Look for the final `}'. */
3629 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3631 /* If that worked, we're definitely looking at a
3632 compound-literal expression. */
3633 if (cp_parser_parse_definitely (parser))
3635 /* Warn the user that a compound literal is not
3636 allowed in standard C++. */
3637 if (pedantic)
3638 pedwarn ("ISO C++ forbids compound-literals");
3639 /* Form the representation of the compound-literal. */
3640 postfix_expression
3641 = finish_compound_literal (type, initializer_list);
3642 break;
3646 /* It must be a primary-expression. */
3647 postfix_expression = cp_parser_primary_expression (parser,
3648 &idk,
3649 &qualifying_class);
3651 break;
3654 /* If we were avoiding committing to the processing of a
3655 qualified-id until we knew whether or not we had a
3656 pointer-to-member, we now know. */
3657 if (qualifying_class)
3659 bool done;
3661 /* Peek at the next token. */
3662 token = cp_lexer_peek_token (parser->lexer);
3663 done = (token->type != CPP_OPEN_SQUARE
3664 && token->type != CPP_OPEN_PAREN
3665 && token->type != CPP_DOT
3666 && token->type != CPP_DEREF
3667 && token->type != CPP_PLUS_PLUS
3668 && token->type != CPP_MINUS_MINUS);
3670 postfix_expression = finish_qualified_id_expr (qualifying_class,
3671 postfix_expression,
3672 done,
3673 address_p);
3674 if (done)
3675 return postfix_expression;
3678 /* Keep looping until the postfix-expression is complete. */
3679 while (true)
3681 if (idk == CP_ID_KIND_UNQUALIFIED
3682 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3683 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3684 /* It is not a Koenig lookup function call. */
3685 postfix_expression
3686 = unqualified_name_lookup_error (postfix_expression);
3688 /* Peek at the next token. */
3689 token = cp_lexer_peek_token (parser->lexer);
3691 switch (token->type)
3693 case CPP_OPEN_SQUARE:
3694 /* postfix-expression [ expression ] */
3696 tree index;
3698 /* Consume the `[' token. */
3699 cp_lexer_consume_token (parser->lexer);
3700 /* Parse the index expression. */
3701 index = cp_parser_expression (parser);
3702 /* Look for the closing `]'. */
3703 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3705 /* Build the ARRAY_REF. */
3706 postfix_expression
3707 = grok_array_decl (postfix_expression, index);
3708 idk = CP_ID_KIND_NONE;
3709 /* Array references are not permitted in
3710 constant-expressions. */
3711 if (cp_parser_non_integral_constant_expression
3712 (parser, "an array reference"))
3713 postfix_expression = error_mark_node;
3715 break;
3717 case CPP_OPEN_PAREN:
3718 /* postfix-expression ( expression-list [opt] ) */
3720 bool koenig_p;
3721 tree args = (cp_parser_parenthesized_expression_list
3722 (parser, false, /*non_constant_p=*/NULL));
3724 if (args == error_mark_node)
3726 postfix_expression = error_mark_node;
3727 break;
3730 /* Function calls are not permitted in
3731 constant-expressions. */
3732 if (cp_parser_non_integral_constant_expression (parser,
3733 "a function call"))
3735 postfix_expression = error_mark_node;
3736 break;
3739 koenig_p = false;
3740 if (idk == CP_ID_KIND_UNQUALIFIED)
3742 /* We do not perform argument-dependent lookup if
3743 normal lookup finds a non-function, in accordance
3744 with the expected resolution of DR 218. */
3745 if (args
3746 && (is_overloaded_fn (postfix_expression)
3747 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3749 koenig_p = true;
3750 postfix_expression
3751 = perform_koenig_lookup (postfix_expression, args);
3753 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3754 postfix_expression
3755 = unqualified_fn_lookup_error (postfix_expression);
3758 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3760 tree instance = TREE_OPERAND (postfix_expression, 0);
3761 tree fn = TREE_OPERAND (postfix_expression, 1);
3763 if (processing_template_decl
3764 && (type_dependent_expression_p (instance)
3765 || (!BASELINK_P (fn)
3766 && TREE_CODE (fn) != FIELD_DECL)
3767 || type_dependent_expression_p (fn)
3768 || any_type_dependent_arguments_p (args)))
3770 postfix_expression
3771 = build_min_nt (CALL_EXPR, postfix_expression, args);
3772 break;
3775 if (BASELINK_P (fn))
3776 postfix_expression
3777 = (build_new_method_call
3778 (instance, fn, args, NULL_TREE,
3779 (idk == CP_ID_KIND_QUALIFIED
3780 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3781 else
3782 postfix_expression
3783 = finish_call_expr (postfix_expression, args,
3784 /*disallow_virtual=*/false,
3785 /*koenig_p=*/false);
3787 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3788 || TREE_CODE (postfix_expression) == MEMBER_REF
3789 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3790 postfix_expression = (build_offset_ref_call_from_tree
3791 (postfix_expression, args));
3792 else if (idk == CP_ID_KIND_QUALIFIED)
3793 /* A call to a static class member, or a namespace-scope
3794 function. */
3795 postfix_expression
3796 = finish_call_expr (postfix_expression, args,
3797 /*disallow_virtual=*/true,
3798 koenig_p);
3799 else
3800 /* All other function calls. */
3801 postfix_expression
3802 = finish_call_expr (postfix_expression, args,
3803 /*disallow_virtual=*/false,
3804 koenig_p);
3806 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3807 idk = CP_ID_KIND_NONE;
3809 break;
3811 case CPP_DOT:
3812 case CPP_DEREF:
3813 /* postfix-expression . template [opt] id-expression
3814 postfix-expression . pseudo-destructor-name
3815 postfix-expression -> template [opt] id-expression
3816 postfix-expression -> pseudo-destructor-name */
3818 tree name;
3819 bool dependent_p;
3820 bool template_p;
3821 tree scope = NULL_TREE;
3822 enum cpp_ttype token_type = token->type;
3824 /* If this is a `->' operator, dereference the pointer. */
3825 if (token->type == CPP_DEREF)
3826 postfix_expression = build_x_arrow (postfix_expression);
3827 /* Check to see whether or not the expression is
3828 type-dependent. */
3829 dependent_p = type_dependent_expression_p (postfix_expression);
3830 /* The identifier following the `->' or `.' is not
3831 qualified. */
3832 parser->scope = NULL_TREE;
3833 parser->qualifying_scope = NULL_TREE;
3834 parser->object_scope = NULL_TREE;
3835 idk = CP_ID_KIND_NONE;
3836 /* Enter the scope corresponding to the type of the object
3837 given by the POSTFIX_EXPRESSION. */
3838 if (!dependent_p
3839 && TREE_TYPE (postfix_expression) != NULL_TREE)
3841 scope = TREE_TYPE (postfix_expression);
3842 /* According to the standard, no expression should
3843 ever have reference type. Unfortunately, we do not
3844 currently match the standard in this respect in
3845 that our internal representation of an expression
3846 may have reference type even when the standard says
3847 it does not. Therefore, we have to manually obtain
3848 the underlying type here. */
3849 scope = non_reference (scope);
3850 /* The type of the POSTFIX_EXPRESSION must be
3851 complete. */
3852 scope = complete_type_or_else (scope, NULL_TREE);
3853 /* Let the name lookup machinery know that we are
3854 processing a class member access expression. */
3855 parser->context->object_type = scope;
3856 /* If something went wrong, we want to be able to
3857 discern that case, as opposed to the case where
3858 there was no SCOPE due to the type of expression
3859 being dependent. */
3860 if (!scope)
3861 scope = error_mark_node;
3862 /* If the SCOPE was erroneous, make the various
3863 semantic analysis functions exit quickly -- and
3864 without issuing additional error messages. */
3865 if (scope == error_mark_node)
3866 postfix_expression = error_mark_node;
3869 /* Consume the `.' or `->' operator. */
3870 cp_lexer_consume_token (parser->lexer);
3871 /* If the SCOPE is not a scalar type, we are looking at an
3872 ordinary class member access expression, rather than a
3873 pseudo-destructor-name. */
3874 if (!scope || !SCALAR_TYPE_P (scope))
3876 template_p = cp_parser_optional_template_keyword (parser);
3877 /* Parse the id-expression. */
3878 name = cp_parser_id_expression (parser,
3879 template_p,
3880 /*check_dependency_p=*/true,
3881 /*template_p=*/NULL,
3882 /*declarator_p=*/false);
3883 /* In general, build a SCOPE_REF if the member name is
3884 qualified. However, if the name was not dependent
3885 and has already been resolved; there is no need to
3886 build the SCOPE_REF. For example;
3888 struct X { void f(); };
3889 template <typename T> void f(T* t) { t->X::f(); }
3891 Even though "t" is dependent, "X::f" is not and has
3892 been resolved to a BASELINK; there is no need to
3893 include scope information. */
3895 /* But we do need to remember that there was an explicit
3896 scope for virtual function calls. */
3897 if (parser->scope)
3898 idk = CP_ID_KIND_QUALIFIED;
3900 if (name != error_mark_node
3901 && !BASELINK_P (name)
3902 && parser->scope)
3904 name = build_nt (SCOPE_REF, parser->scope, name);
3905 parser->scope = NULL_TREE;
3906 parser->qualifying_scope = NULL_TREE;
3907 parser->object_scope = NULL_TREE;
3909 if (scope && name && BASELINK_P (name))
3910 adjust_result_of_qualified_name_lookup
3911 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3912 postfix_expression
3913 = finish_class_member_access_expr (postfix_expression, name);
3915 /* Otherwise, try the pseudo-destructor-name production. */
3916 else
3918 tree s = NULL_TREE;
3919 tree type;
3921 /* Parse the pseudo-destructor-name. */
3922 cp_parser_pseudo_destructor_name (parser, &s, &type);
3923 /* Form the call. */
3924 postfix_expression
3925 = finish_pseudo_destructor_expr (postfix_expression,
3926 s, TREE_TYPE (type));
3929 /* We no longer need to look up names in the scope of the
3930 object on the left-hand side of the `.' or `->'
3931 operator. */
3932 parser->context->object_type = NULL_TREE;
3933 /* These operators may not appear in constant-expressions. */
3934 if (/* The "->" operator is allowed in the implementation
3935 of "offsetof". The "." operator may appear in the
3936 name of the member. */
3937 !parser->in_offsetof_p
3938 && (cp_parser_non_integral_constant_expression
3939 (parser,
3940 token_type == CPP_DEREF ? "'->'" : "`.'")))
3941 postfix_expression = error_mark_node;
3943 break;
3945 case CPP_PLUS_PLUS:
3946 /* postfix-expression ++ */
3947 /* Consume the `++' token. */
3948 cp_lexer_consume_token (parser->lexer);
3949 /* Generate a representation for the complete expression. */
3950 postfix_expression
3951 = finish_increment_expr (postfix_expression,
3952 POSTINCREMENT_EXPR);
3953 /* Increments may not appear in constant-expressions. */
3954 if (cp_parser_non_integral_constant_expression (parser,
3955 "an increment"))
3956 postfix_expression = error_mark_node;
3957 idk = CP_ID_KIND_NONE;
3958 break;
3960 case CPP_MINUS_MINUS:
3961 /* postfix-expression -- */
3962 /* Consume the `--' token. */
3963 cp_lexer_consume_token (parser->lexer);
3964 /* Generate a representation for the complete expression. */
3965 postfix_expression
3966 = finish_increment_expr (postfix_expression,
3967 POSTDECREMENT_EXPR);
3968 /* Decrements may not appear in constant-expressions. */
3969 if (cp_parser_non_integral_constant_expression (parser,
3970 "a decrement"))
3971 postfix_expression = error_mark_node;
3972 idk = CP_ID_KIND_NONE;
3973 break;
3975 default:
3976 return postfix_expression;
3980 /* We should never get here. */
3981 abort ();
3982 return error_mark_node;
3985 /* Parse a parenthesized expression-list.
3987 expression-list:
3988 assignment-expression
3989 expression-list, assignment-expression
3991 attribute-list:
3992 expression-list
3993 identifier
3994 identifier, expression-list
3996 Returns a TREE_LIST. The TREE_VALUE of each node is a
3997 representation of an assignment-expression. Note that a TREE_LIST
3998 is returned even if there is only a single expression in the list.
3999 error_mark_node is returned if the ( and or ) are
4000 missing. NULL_TREE is returned on no expressions. The parentheses
4001 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4002 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4003 indicates whether or not all of the expressions in the list were
4004 constant. */
4006 static tree
4007 cp_parser_parenthesized_expression_list (cp_parser* parser,
4008 bool is_attribute_list,
4009 bool *non_constant_p)
4011 tree expression_list = NULL_TREE;
4012 tree identifier = NULL_TREE;
4014 /* Assume all the expressions will be constant. */
4015 if (non_constant_p)
4016 *non_constant_p = false;
4018 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4019 return error_mark_node;
4021 /* Consume expressions until there are no more. */
4022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4023 while (true)
4025 tree expr;
4027 /* At the beginning of attribute lists, check to see if the
4028 next token is an identifier. */
4029 if (is_attribute_list
4030 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4032 cp_token *token;
4034 /* Consume the identifier. */
4035 token = cp_lexer_consume_token (parser->lexer);
4036 /* Save the identifier. */
4037 identifier = token->value;
4039 else
4041 /* Parse the next assignment-expression. */
4042 if (non_constant_p)
4044 bool expr_non_constant_p;
4045 expr = (cp_parser_constant_expression
4046 (parser, /*allow_non_constant_p=*/true,
4047 &expr_non_constant_p));
4048 if (expr_non_constant_p)
4049 *non_constant_p = true;
4051 else
4052 expr = cp_parser_assignment_expression (parser);
4054 /* Add it to the list. We add error_mark_node
4055 expressions to the list, so that we can still tell if
4056 the correct form for a parenthesized expression-list
4057 is found. That gives better errors. */
4058 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4060 if (expr == error_mark_node)
4061 goto skip_comma;
4064 /* After the first item, attribute lists look the same as
4065 expression lists. */
4066 is_attribute_list = false;
4068 get_comma:;
4069 /* If the next token isn't a `,', then we are done. */
4070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4071 break;
4073 /* Otherwise, consume the `,' and keep going. */
4074 cp_lexer_consume_token (parser->lexer);
4077 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4079 int ending;
4081 skip_comma:;
4082 /* We try and resync to an unnested comma, as that will give the
4083 user better diagnostics. */
4084 ending = cp_parser_skip_to_closing_parenthesis (parser,
4085 /*recovering=*/true,
4086 /*or_comma=*/true,
4087 /*consume_paren=*/true);
4088 if (ending < 0)
4089 goto get_comma;
4090 if (!ending)
4091 return error_mark_node;
4094 /* We built up the list in reverse order so we must reverse it now. */
4095 expression_list = nreverse (expression_list);
4096 if (identifier)
4097 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4099 return expression_list;
4102 /* Parse a pseudo-destructor-name.
4104 pseudo-destructor-name:
4105 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4106 :: [opt] nested-name-specifier template template-id :: ~ type-name
4107 :: [opt] nested-name-specifier [opt] ~ type-name
4109 If either of the first two productions is used, sets *SCOPE to the
4110 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4111 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4112 or ERROR_MARK_NODE if the parse fails. */
4114 static void
4115 cp_parser_pseudo_destructor_name (cp_parser* parser,
4116 tree* scope,
4117 tree* type)
4119 bool nested_name_specifier_p;
4121 /* Look for the optional `::' operator. */
4122 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4123 /* Look for the optional nested-name-specifier. */
4124 nested_name_specifier_p
4125 = (cp_parser_nested_name_specifier_opt (parser,
4126 /*typename_keyword_p=*/false,
4127 /*check_dependency_p=*/true,
4128 /*type_p=*/false,
4129 /*is_declaration=*/true)
4130 != NULL_TREE);
4131 /* Now, if we saw a nested-name-specifier, we might be doing the
4132 second production. */
4133 if (nested_name_specifier_p
4134 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4136 /* Consume the `template' keyword. */
4137 cp_lexer_consume_token (parser->lexer);
4138 /* Parse the template-id. */
4139 cp_parser_template_id (parser,
4140 /*template_keyword_p=*/true,
4141 /*check_dependency_p=*/false,
4142 /*is_declaration=*/true);
4143 /* Look for the `::' token. */
4144 cp_parser_require (parser, CPP_SCOPE, "`::'");
4146 /* If the next token is not a `~', then there might be some
4147 additional qualification. */
4148 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4150 /* Look for the type-name. */
4151 *scope = TREE_TYPE (cp_parser_type_name (parser));
4153 /* If we didn't get an aggregate type, or we don't have ::~,
4154 then something has gone wrong. Since the only caller of this
4155 function is looking for something after `.' or `->' after a
4156 scalar type, most likely the program is trying to get a
4157 member of a non-aggregate type. */
4158 if (*scope == error_mark_node
4159 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4160 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4162 cp_parser_error (parser, "request for member of non-aggregate type");
4163 *type = error_mark_node;
4164 return;
4167 /* Look for the `::' token. */
4168 cp_parser_require (parser, CPP_SCOPE, "`::'");
4170 else
4171 *scope = NULL_TREE;
4173 /* Look for the `~'. */
4174 cp_parser_require (parser, CPP_COMPL, "`~'");
4175 /* Look for the type-name again. We are not responsible for
4176 checking that it matches the first type-name. */
4177 *type = cp_parser_type_name (parser);
4180 /* Parse a unary-expression.
4182 unary-expression:
4183 postfix-expression
4184 ++ cast-expression
4185 -- cast-expression
4186 unary-operator cast-expression
4187 sizeof unary-expression
4188 sizeof ( type-id )
4189 new-expression
4190 delete-expression
4192 GNU Extensions:
4194 unary-expression:
4195 __extension__ cast-expression
4196 __alignof__ unary-expression
4197 __alignof__ ( type-id )
4198 __real__ cast-expression
4199 __imag__ cast-expression
4200 && identifier
4202 ADDRESS_P is true iff the unary-expression is appearing as the
4203 operand of the `&' operator.
4205 Returns a representation of the expression. */
4207 static tree
4208 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4210 cp_token *token;
4211 enum tree_code unary_operator;
4213 /* Peek at the next token. */
4214 token = cp_lexer_peek_token (parser->lexer);
4215 /* Some keywords give away the kind of expression. */
4216 if (token->type == CPP_KEYWORD)
4218 enum rid keyword = token->keyword;
4220 switch (keyword)
4222 case RID_ALIGNOF:
4223 case RID_SIZEOF:
4225 tree operand;
4226 enum tree_code op;
4228 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4229 /* Consume the token. */
4230 cp_lexer_consume_token (parser->lexer);
4231 /* Parse the operand. */
4232 operand = cp_parser_sizeof_operand (parser, keyword);
4234 if (TYPE_P (operand))
4235 return cxx_sizeof_or_alignof_type (operand, op, true);
4236 else
4237 return cxx_sizeof_or_alignof_expr (operand, op);
4240 case RID_NEW:
4241 return cp_parser_new_expression (parser);
4243 case RID_DELETE:
4244 return cp_parser_delete_expression (parser);
4246 case RID_EXTENSION:
4248 /* The saved value of the PEDANTIC flag. */
4249 int saved_pedantic;
4250 tree expr;
4252 /* Save away the PEDANTIC flag. */
4253 cp_parser_extension_opt (parser, &saved_pedantic);
4254 /* Parse the cast-expression. */
4255 expr = cp_parser_simple_cast_expression (parser);
4256 /* Restore the PEDANTIC flag. */
4257 pedantic = saved_pedantic;
4259 return expr;
4262 case RID_REALPART:
4263 case RID_IMAGPART:
4265 tree expression;
4267 /* Consume the `__real__' or `__imag__' token. */
4268 cp_lexer_consume_token (parser->lexer);
4269 /* Parse the cast-expression. */
4270 expression = cp_parser_simple_cast_expression (parser);
4271 /* Create the complete representation. */
4272 return build_x_unary_op ((keyword == RID_REALPART
4273 ? REALPART_EXPR : IMAGPART_EXPR),
4274 expression);
4276 break;
4278 default:
4279 break;
4283 /* Look for the `:: new' and `:: delete', which also signal the
4284 beginning of a new-expression, or delete-expression,
4285 respectively. If the next token is `::', then it might be one of
4286 these. */
4287 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4289 enum rid keyword;
4291 /* See if the token after the `::' is one of the keywords in
4292 which we're interested. */
4293 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4294 /* If it's `new', we have a new-expression. */
4295 if (keyword == RID_NEW)
4296 return cp_parser_new_expression (parser);
4297 /* Similarly, for `delete'. */
4298 else if (keyword == RID_DELETE)
4299 return cp_parser_delete_expression (parser);
4302 /* Look for a unary operator. */
4303 unary_operator = cp_parser_unary_operator (token);
4304 /* The `++' and `--' operators can be handled similarly, even though
4305 they are not technically unary-operators in the grammar. */
4306 if (unary_operator == ERROR_MARK)
4308 if (token->type == CPP_PLUS_PLUS)
4309 unary_operator = PREINCREMENT_EXPR;
4310 else if (token->type == CPP_MINUS_MINUS)
4311 unary_operator = PREDECREMENT_EXPR;
4312 /* Handle the GNU address-of-label extension. */
4313 else if (cp_parser_allow_gnu_extensions_p (parser)
4314 && token->type == CPP_AND_AND)
4316 tree identifier;
4318 /* Consume the '&&' token. */
4319 cp_lexer_consume_token (parser->lexer);
4320 /* Look for the identifier. */
4321 identifier = cp_parser_identifier (parser);
4322 /* Create an expression representing the address. */
4323 return finish_label_address_expr (identifier);
4326 if (unary_operator != ERROR_MARK)
4328 tree cast_expression;
4329 tree expression = error_mark_node;
4330 const char *non_constant_p = NULL;
4332 /* Consume the operator token. */
4333 token = cp_lexer_consume_token (parser->lexer);
4334 /* Parse the cast-expression. */
4335 cast_expression
4336 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4337 /* Now, build an appropriate representation. */
4338 switch (unary_operator)
4340 case INDIRECT_REF:
4341 non_constant_p = "`*'";
4342 expression = build_x_indirect_ref (cast_expression, "unary *");
4343 break;
4345 case ADDR_EXPR:
4346 /* The "&" operator is allowed in the implementation of
4347 "offsetof". */
4348 if (!parser->in_offsetof_p)
4349 non_constant_p = "`&'";
4350 /* Fall through. */
4351 case BIT_NOT_EXPR:
4352 expression = build_x_unary_op (unary_operator, cast_expression);
4353 break;
4355 case PREINCREMENT_EXPR:
4356 case PREDECREMENT_EXPR:
4357 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4358 ? "`++'" : "`--'");
4359 /* Fall through. */
4360 case CONVERT_EXPR:
4361 case NEGATE_EXPR:
4362 case TRUTH_NOT_EXPR:
4363 expression = finish_unary_op_expr (unary_operator, cast_expression);
4364 break;
4366 default:
4367 abort ();
4370 if (non_constant_p
4371 && cp_parser_non_integral_constant_expression (parser,
4372 non_constant_p))
4373 expression = error_mark_node;
4375 return expression;
4378 return cp_parser_postfix_expression (parser, address_p);
4381 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4382 unary-operator, the corresponding tree code is returned. */
4384 static enum tree_code
4385 cp_parser_unary_operator (cp_token* token)
4387 switch (token->type)
4389 case CPP_MULT:
4390 return INDIRECT_REF;
4392 case CPP_AND:
4393 return ADDR_EXPR;
4395 case CPP_PLUS:
4396 return CONVERT_EXPR;
4398 case CPP_MINUS:
4399 return NEGATE_EXPR;
4401 case CPP_NOT:
4402 return TRUTH_NOT_EXPR;
4404 case CPP_COMPL:
4405 return BIT_NOT_EXPR;
4407 default:
4408 return ERROR_MARK;
4412 /* Parse a new-expression.
4414 new-expression:
4415 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4416 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4418 Returns a representation of the expression. */
4420 static tree
4421 cp_parser_new_expression (cp_parser* parser)
4423 bool global_scope_p;
4424 tree placement;
4425 tree type;
4426 tree initializer;
4428 /* Look for the optional `::' operator. */
4429 global_scope_p
4430 = (cp_parser_global_scope_opt (parser,
4431 /*current_scope_valid_p=*/false)
4432 != NULL_TREE);
4433 /* Look for the `new' operator. */
4434 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4435 /* There's no easy way to tell a new-placement from the
4436 `( type-id )' construct. */
4437 cp_parser_parse_tentatively (parser);
4438 /* Look for a new-placement. */
4439 placement = cp_parser_new_placement (parser);
4440 /* If that didn't work out, there's no new-placement. */
4441 if (!cp_parser_parse_definitely (parser))
4442 placement = NULL_TREE;
4444 /* If the next token is a `(', then we have a parenthesized
4445 type-id. */
4446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4448 /* Consume the `('. */
4449 cp_lexer_consume_token (parser->lexer);
4450 /* Parse the type-id. */
4451 type = cp_parser_type_id (parser);
4452 /* Look for the closing `)'. */
4453 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4454 /* There should not be a direct-new-declarator in this production,
4455 but GCC used to allowed this, so we check and emit a sensible error
4456 message for this case. */
4457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4459 error ("array bound forbidden after parenthesized type-id");
4460 inform ("try removing the parentheses around the type-id");
4461 cp_parser_direct_new_declarator (parser);
4464 /* Otherwise, there must be a new-type-id. */
4465 else
4466 type = cp_parser_new_type_id (parser);
4468 /* If the next token is a `(', then we have a new-initializer. */
4469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4470 initializer = cp_parser_new_initializer (parser);
4471 else
4472 initializer = NULL_TREE;
4474 /* A new-expression may not appear in an integral constant
4475 expression. */
4476 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4477 return error_mark_node;
4479 /* Create a representation of the new-expression. */
4480 return build_new (placement, type, initializer, global_scope_p);
4483 /* Parse a new-placement.
4485 new-placement:
4486 ( expression-list )
4488 Returns the same representation as for an expression-list. */
4490 static tree
4491 cp_parser_new_placement (cp_parser* parser)
4493 tree expression_list;
4495 /* Parse the expression-list. */
4496 expression_list = (cp_parser_parenthesized_expression_list
4497 (parser, false, /*non_constant_p=*/NULL));
4499 return expression_list;
4502 /* Parse a new-type-id.
4504 new-type-id:
4505 type-specifier-seq new-declarator [opt]
4507 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4508 and whose TREE_VALUE is the new-declarator. */
4510 static tree
4511 cp_parser_new_type_id (cp_parser* parser)
4513 tree type_specifier_seq;
4514 tree declarator;
4515 const char *saved_message;
4517 /* The type-specifier sequence must not contain type definitions.
4518 (It cannot contain declarations of new types either, but if they
4519 are not definitions we will catch that because they are not
4520 complete.) */
4521 saved_message = parser->type_definition_forbidden_message;
4522 parser->type_definition_forbidden_message
4523 = "types may not be defined in a new-type-id";
4524 /* Parse the type-specifier-seq. */
4525 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4526 /* Restore the old message. */
4527 parser->type_definition_forbidden_message = saved_message;
4528 /* Parse the new-declarator. */
4529 declarator = cp_parser_new_declarator_opt (parser);
4531 return build_tree_list (type_specifier_seq, declarator);
4534 /* Parse an (optional) new-declarator.
4536 new-declarator:
4537 ptr-operator new-declarator [opt]
4538 direct-new-declarator
4540 Returns a representation of the declarator. See
4541 cp_parser_declarator for the representations used. */
4543 static tree
4544 cp_parser_new_declarator_opt (cp_parser* parser)
4546 enum tree_code code;
4547 tree type;
4548 tree cv_qualifier_seq;
4550 /* We don't know if there's a ptr-operator next, or not. */
4551 cp_parser_parse_tentatively (parser);
4552 /* Look for a ptr-operator. */
4553 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4554 /* If that worked, look for more new-declarators. */
4555 if (cp_parser_parse_definitely (parser))
4557 tree declarator;
4559 /* Parse another optional declarator. */
4560 declarator = cp_parser_new_declarator_opt (parser);
4562 /* Create the representation of the declarator. */
4563 if (code == INDIRECT_REF)
4564 declarator = make_pointer_declarator (cv_qualifier_seq,
4565 declarator);
4566 else
4567 declarator = make_reference_declarator (cv_qualifier_seq,
4568 declarator);
4570 /* Handle the pointer-to-member case. */
4571 if (type)
4572 declarator = build_nt (SCOPE_REF, type, declarator);
4574 return declarator;
4577 /* If the next token is a `[', there is a direct-new-declarator. */
4578 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4579 return cp_parser_direct_new_declarator (parser);
4581 return NULL_TREE;
4584 /* Parse a direct-new-declarator.
4586 direct-new-declarator:
4587 [ expression ]
4588 direct-new-declarator [constant-expression]
4590 Returns an ARRAY_REF, following the same conventions as are
4591 documented for cp_parser_direct_declarator. */
4593 static tree
4594 cp_parser_direct_new_declarator (cp_parser* parser)
4596 tree declarator = NULL_TREE;
4598 while (true)
4600 tree expression;
4602 /* Look for the opening `['. */
4603 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4604 /* The first expression is not required to be constant. */
4605 if (!declarator)
4607 expression = cp_parser_expression (parser);
4608 /* The standard requires that the expression have integral
4609 type. DR 74 adds enumeration types. We believe that the
4610 real intent is that these expressions be handled like the
4611 expression in a `switch' condition, which also allows
4612 classes with a single conversion to integral or
4613 enumeration type. */
4614 if (!processing_template_decl)
4616 expression
4617 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4618 expression,
4619 /*complain=*/true);
4620 if (!expression)
4622 error ("expression in new-declarator must have integral or enumeration type");
4623 expression = error_mark_node;
4627 /* But all the other expressions must be. */
4628 else
4629 expression
4630 = cp_parser_constant_expression (parser,
4631 /*allow_non_constant=*/false,
4632 NULL);
4633 /* Look for the closing `]'. */
4634 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4636 /* Add this bound to the declarator. */
4637 declarator = build_nt (ARRAY_REF, declarator, expression);
4639 /* If the next token is not a `[', then there are no more
4640 bounds. */
4641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4642 break;
4645 return declarator;
4648 /* Parse a new-initializer.
4650 new-initializer:
4651 ( expression-list [opt] )
4653 Returns a representation of the expression-list. If there is no
4654 expression-list, VOID_ZERO_NODE is returned. */
4656 static tree
4657 cp_parser_new_initializer (cp_parser* parser)
4659 tree expression_list;
4661 expression_list = (cp_parser_parenthesized_expression_list
4662 (parser, false, /*non_constant_p=*/NULL));
4663 if (!expression_list)
4664 expression_list = void_zero_node;
4666 return expression_list;
4669 /* Parse a delete-expression.
4671 delete-expression:
4672 :: [opt] delete cast-expression
4673 :: [opt] delete [ ] cast-expression
4675 Returns a representation of the expression. */
4677 static tree
4678 cp_parser_delete_expression (cp_parser* parser)
4680 bool global_scope_p;
4681 bool array_p;
4682 tree expression;
4684 /* Look for the optional `::' operator. */
4685 global_scope_p
4686 = (cp_parser_global_scope_opt (parser,
4687 /*current_scope_valid_p=*/false)
4688 != NULL_TREE);
4689 /* Look for the `delete' keyword. */
4690 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4691 /* See if the array syntax is in use. */
4692 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4694 /* Consume the `[' token. */
4695 cp_lexer_consume_token (parser->lexer);
4696 /* Look for the `]' token. */
4697 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4698 /* Remember that this is the `[]' construct. */
4699 array_p = true;
4701 else
4702 array_p = false;
4704 /* Parse the cast-expression. */
4705 expression = cp_parser_simple_cast_expression (parser);
4707 /* A delete-expression may not appear in an integral constant
4708 expression. */
4709 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4710 return error_mark_node;
4712 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4715 /* Parse a cast-expression.
4717 cast-expression:
4718 unary-expression
4719 ( type-id ) cast-expression
4721 Returns a representation of the expression. */
4723 static tree
4724 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4726 /* If it's a `(', then we might be looking at a cast. */
4727 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4729 tree type = NULL_TREE;
4730 tree expr = NULL_TREE;
4731 bool compound_literal_p;
4732 const char *saved_message;
4734 /* There's no way to know yet whether or not this is a cast.
4735 For example, `(int (3))' is a unary-expression, while `(int)
4736 3' is a cast. So, we resort to parsing tentatively. */
4737 cp_parser_parse_tentatively (parser);
4738 /* Types may not be defined in a cast. */
4739 saved_message = parser->type_definition_forbidden_message;
4740 parser->type_definition_forbidden_message
4741 = "types may not be defined in casts";
4742 /* Consume the `('. */
4743 cp_lexer_consume_token (parser->lexer);
4744 /* A very tricky bit is that `(struct S) { 3 }' is a
4745 compound-literal (which we permit in C++ as an extension).
4746 But, that construct is not a cast-expression -- it is a
4747 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4748 is legal; if the compound-literal were a cast-expression,
4749 you'd need an extra set of parentheses.) But, if we parse
4750 the type-id, and it happens to be a class-specifier, then we
4751 will commit to the parse at that point, because we cannot
4752 undo the action that is done when creating a new class. So,
4753 then we cannot back up and do a postfix-expression.
4755 Therefore, we scan ahead to the closing `)', and check to see
4756 if the token after the `)' is a `{'. If so, we are not
4757 looking at a cast-expression.
4759 Save tokens so that we can put them back. */
4760 cp_lexer_save_tokens (parser->lexer);
4761 /* Skip tokens until the next token is a closing parenthesis.
4762 If we find the closing `)', and the next token is a `{', then
4763 we are looking at a compound-literal. */
4764 compound_literal_p
4765 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4766 /*consume_paren=*/true)
4767 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4768 /* Roll back the tokens we skipped. */
4769 cp_lexer_rollback_tokens (parser->lexer);
4770 /* If we were looking at a compound-literal, simulate an error
4771 so that the call to cp_parser_parse_definitely below will
4772 fail. */
4773 if (compound_literal_p)
4774 cp_parser_simulate_error (parser);
4775 else
4777 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4778 parser->in_type_id_in_expr_p = true;
4779 /* Look for the type-id. */
4780 type = cp_parser_type_id (parser);
4781 /* Look for the closing `)'. */
4782 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4783 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4786 /* Restore the saved message. */
4787 parser->type_definition_forbidden_message = saved_message;
4789 /* If ok so far, parse the dependent expression. We cannot be
4790 sure it is a cast. Consider `(T ())'. It is a parenthesized
4791 ctor of T, but looks like a cast to function returning T
4792 without a dependent expression. */
4793 if (!cp_parser_error_occurred (parser))
4794 expr = cp_parser_simple_cast_expression (parser);
4796 if (cp_parser_parse_definitely (parser))
4798 /* Warn about old-style casts, if so requested. */
4799 if (warn_old_style_cast
4800 && !in_system_header
4801 && !VOID_TYPE_P (type)
4802 && current_lang_name != lang_name_c)
4803 warning ("use of old-style cast");
4805 /* Only type conversions to integral or enumeration types
4806 can be used in constant-expressions. */
4807 if (parser->integral_constant_expression_p
4808 && !dependent_type_p (type)
4809 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4810 && (cp_parser_non_integral_constant_expression
4811 (parser,
4812 "a casts to a type other than an integral or "
4813 "enumeration type")))
4814 return error_mark_node;
4816 /* Perform the cast. */
4817 expr = build_c_cast (type, expr);
4818 return expr;
4822 /* If we get here, then it's not a cast, so it must be a
4823 unary-expression. */
4824 return cp_parser_unary_expression (parser, address_p);
4827 /* Parse a pm-expression.
4829 pm-expression:
4830 cast-expression
4831 pm-expression .* cast-expression
4832 pm-expression ->* cast-expression
4834 Returns a representation of the expression. */
4836 static tree
4837 cp_parser_pm_expression (cp_parser* parser)
4839 static const cp_parser_token_tree_map map = {
4840 { CPP_DEREF_STAR, MEMBER_REF },
4841 { CPP_DOT_STAR, DOTSTAR_EXPR },
4842 { CPP_EOF, ERROR_MARK }
4845 return cp_parser_binary_expression (parser, map,
4846 cp_parser_simple_cast_expression);
4849 /* Parse a multiplicative-expression.
4851 mulitplicative-expression:
4852 pm-expression
4853 multiplicative-expression * pm-expression
4854 multiplicative-expression / pm-expression
4855 multiplicative-expression % pm-expression
4857 Returns a representation of the expression. */
4859 static tree
4860 cp_parser_multiplicative_expression (cp_parser* parser)
4862 static const cp_parser_token_tree_map map = {
4863 { CPP_MULT, MULT_EXPR },
4864 { CPP_DIV, TRUNC_DIV_EXPR },
4865 { CPP_MOD, TRUNC_MOD_EXPR },
4866 { CPP_EOF, ERROR_MARK }
4869 return cp_parser_binary_expression (parser,
4870 map,
4871 cp_parser_pm_expression);
4874 /* Parse an additive-expression.
4876 additive-expression:
4877 multiplicative-expression
4878 additive-expression + multiplicative-expression
4879 additive-expression - multiplicative-expression
4881 Returns a representation of the expression. */
4883 static tree
4884 cp_parser_additive_expression (cp_parser* parser)
4886 static const cp_parser_token_tree_map map = {
4887 { CPP_PLUS, PLUS_EXPR },
4888 { CPP_MINUS, MINUS_EXPR },
4889 { CPP_EOF, ERROR_MARK }
4892 return cp_parser_binary_expression (parser,
4893 map,
4894 cp_parser_multiplicative_expression);
4897 /* Parse a shift-expression.
4899 shift-expression:
4900 additive-expression
4901 shift-expression << additive-expression
4902 shift-expression >> additive-expression
4904 Returns a representation of the expression. */
4906 static tree
4907 cp_parser_shift_expression (cp_parser* parser)
4909 static const cp_parser_token_tree_map map = {
4910 { CPP_LSHIFT, LSHIFT_EXPR },
4911 { CPP_RSHIFT, RSHIFT_EXPR },
4912 { CPP_EOF, ERROR_MARK }
4915 return cp_parser_binary_expression (parser,
4916 map,
4917 cp_parser_additive_expression);
4920 /* Parse a relational-expression.
4922 relational-expression:
4923 shift-expression
4924 relational-expression < shift-expression
4925 relational-expression > shift-expression
4926 relational-expression <= shift-expression
4927 relational-expression >= shift-expression
4929 GNU Extension:
4931 relational-expression:
4932 relational-expression <? shift-expression
4933 relational-expression >? shift-expression
4935 Returns a representation of the expression. */
4937 static tree
4938 cp_parser_relational_expression (cp_parser* parser)
4940 static const cp_parser_token_tree_map map = {
4941 { CPP_LESS, LT_EXPR },
4942 { CPP_GREATER, GT_EXPR },
4943 { CPP_LESS_EQ, LE_EXPR },
4944 { CPP_GREATER_EQ, GE_EXPR },
4945 { CPP_MIN, MIN_EXPR },
4946 { CPP_MAX, MAX_EXPR },
4947 { CPP_EOF, ERROR_MARK }
4950 return cp_parser_binary_expression (parser,
4951 map,
4952 cp_parser_shift_expression);
4955 /* Parse an equality-expression.
4957 equality-expression:
4958 relational-expression
4959 equality-expression == relational-expression
4960 equality-expression != relational-expression
4962 Returns a representation of the expression. */
4964 static tree
4965 cp_parser_equality_expression (cp_parser* parser)
4967 static const cp_parser_token_tree_map map = {
4968 { CPP_EQ_EQ, EQ_EXPR },
4969 { CPP_NOT_EQ, NE_EXPR },
4970 { CPP_EOF, ERROR_MARK }
4973 return cp_parser_binary_expression (parser,
4974 map,
4975 cp_parser_relational_expression);
4978 /* Parse an and-expression.
4980 and-expression:
4981 equality-expression
4982 and-expression & equality-expression
4984 Returns a representation of the expression. */
4986 static tree
4987 cp_parser_and_expression (cp_parser* parser)
4989 static const cp_parser_token_tree_map map = {
4990 { CPP_AND, BIT_AND_EXPR },
4991 { CPP_EOF, ERROR_MARK }
4994 return cp_parser_binary_expression (parser,
4995 map,
4996 cp_parser_equality_expression);
4999 /* Parse an exclusive-or-expression.
5001 exclusive-or-expression:
5002 and-expression
5003 exclusive-or-expression ^ and-expression
5005 Returns a representation of the expression. */
5007 static tree
5008 cp_parser_exclusive_or_expression (cp_parser* parser)
5010 static const cp_parser_token_tree_map map = {
5011 { CPP_XOR, BIT_XOR_EXPR },
5012 { CPP_EOF, ERROR_MARK }
5015 return cp_parser_binary_expression (parser,
5016 map,
5017 cp_parser_and_expression);
5021 /* Parse an inclusive-or-expression.
5023 inclusive-or-expression:
5024 exclusive-or-expression
5025 inclusive-or-expression | exclusive-or-expression
5027 Returns a representation of the expression. */
5029 static tree
5030 cp_parser_inclusive_or_expression (cp_parser* parser)
5032 static const cp_parser_token_tree_map map = {
5033 { CPP_OR, BIT_IOR_EXPR },
5034 { CPP_EOF, ERROR_MARK }
5037 return cp_parser_binary_expression (parser,
5038 map,
5039 cp_parser_exclusive_or_expression);
5042 /* Parse a logical-and-expression.
5044 logical-and-expression:
5045 inclusive-or-expression
5046 logical-and-expression && inclusive-or-expression
5048 Returns a representation of the expression. */
5050 static tree
5051 cp_parser_logical_and_expression (cp_parser* parser)
5053 static const cp_parser_token_tree_map map = {
5054 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5055 { CPP_EOF, ERROR_MARK }
5058 return cp_parser_binary_expression (parser,
5059 map,
5060 cp_parser_inclusive_or_expression);
5063 /* Parse a logical-or-expression.
5065 logical-or-expression:
5066 logical-and-expression
5067 logical-or-expression || logical-and-expression
5069 Returns a representation of the expression. */
5071 static tree
5072 cp_parser_logical_or_expression (cp_parser* parser)
5074 static const cp_parser_token_tree_map map = {
5075 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5076 { CPP_EOF, ERROR_MARK }
5079 return cp_parser_binary_expression (parser,
5080 map,
5081 cp_parser_logical_and_expression);
5084 /* Parse the `? expression : assignment-expression' part of a
5085 conditional-expression. The LOGICAL_OR_EXPR is the
5086 logical-or-expression that started the conditional-expression.
5087 Returns a representation of the entire conditional-expression.
5089 This routine is used by cp_parser_assignment_expression.
5091 ? expression : assignment-expression
5093 GNU Extensions:
5095 ? : assignment-expression */
5097 static tree
5098 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5100 tree expr;
5101 tree assignment_expr;
5103 /* Consume the `?' token. */
5104 cp_lexer_consume_token (parser->lexer);
5105 if (cp_parser_allow_gnu_extensions_p (parser)
5106 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5107 /* Implicit true clause. */
5108 expr = NULL_TREE;
5109 else
5110 /* Parse the expression. */
5111 expr = cp_parser_expression (parser);
5113 /* The next token should be a `:'. */
5114 cp_parser_require (parser, CPP_COLON, "`:'");
5115 /* Parse the assignment-expression. */
5116 assignment_expr = cp_parser_assignment_expression (parser);
5118 /* Build the conditional-expression. */
5119 return build_x_conditional_expr (logical_or_expr,
5120 expr,
5121 assignment_expr);
5124 /* Parse an assignment-expression.
5126 assignment-expression:
5127 conditional-expression
5128 logical-or-expression assignment-operator assignment_expression
5129 throw-expression
5131 Returns a representation for the expression. */
5133 static tree
5134 cp_parser_assignment_expression (cp_parser* parser)
5136 tree expr;
5138 /* If the next token is the `throw' keyword, then we're looking at
5139 a throw-expression. */
5140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5141 expr = cp_parser_throw_expression (parser);
5142 /* Otherwise, it must be that we are looking at a
5143 logical-or-expression. */
5144 else
5146 /* Parse the logical-or-expression. */
5147 expr = cp_parser_logical_or_expression (parser);
5148 /* If the next token is a `?' then we're actually looking at a
5149 conditional-expression. */
5150 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5151 return cp_parser_question_colon_clause (parser, expr);
5152 else
5154 enum tree_code assignment_operator;
5156 /* If it's an assignment-operator, we're using the second
5157 production. */
5158 assignment_operator
5159 = cp_parser_assignment_operator_opt (parser);
5160 if (assignment_operator != ERROR_MARK)
5162 tree rhs;
5164 /* Parse the right-hand side of the assignment. */
5165 rhs = cp_parser_assignment_expression (parser);
5166 /* An assignment may not appear in a
5167 constant-expression. */
5168 if (cp_parser_non_integral_constant_expression (parser,
5169 "an assignment"))
5170 return error_mark_node;
5171 /* Build the assignment expression. */
5172 expr = build_x_modify_expr (expr,
5173 assignment_operator,
5174 rhs);
5179 return expr;
5182 /* Parse an (optional) assignment-operator.
5184 assignment-operator: one of
5185 = *= /= %= += -= >>= <<= &= ^= |=
5187 GNU Extension:
5189 assignment-operator: one of
5190 <?= >?=
5192 If the next token is an assignment operator, the corresponding tree
5193 code is returned, and the token is consumed. For example, for
5194 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5195 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5196 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5197 operator, ERROR_MARK is returned. */
5199 static enum tree_code
5200 cp_parser_assignment_operator_opt (cp_parser* parser)
5202 enum tree_code op;
5203 cp_token *token;
5205 /* Peek at the next toen. */
5206 token = cp_lexer_peek_token (parser->lexer);
5208 switch (token->type)
5210 case CPP_EQ:
5211 op = NOP_EXPR;
5212 break;
5214 case CPP_MULT_EQ:
5215 op = MULT_EXPR;
5216 break;
5218 case CPP_DIV_EQ:
5219 op = TRUNC_DIV_EXPR;
5220 break;
5222 case CPP_MOD_EQ:
5223 op = TRUNC_MOD_EXPR;
5224 break;
5226 case CPP_PLUS_EQ:
5227 op = PLUS_EXPR;
5228 break;
5230 case CPP_MINUS_EQ:
5231 op = MINUS_EXPR;
5232 break;
5234 case CPP_RSHIFT_EQ:
5235 op = RSHIFT_EXPR;
5236 break;
5238 case CPP_LSHIFT_EQ:
5239 op = LSHIFT_EXPR;
5240 break;
5242 case CPP_AND_EQ:
5243 op = BIT_AND_EXPR;
5244 break;
5246 case CPP_XOR_EQ:
5247 op = BIT_XOR_EXPR;
5248 break;
5250 case CPP_OR_EQ:
5251 op = BIT_IOR_EXPR;
5252 break;
5254 case CPP_MIN_EQ:
5255 op = MIN_EXPR;
5256 break;
5258 case CPP_MAX_EQ:
5259 op = MAX_EXPR;
5260 break;
5262 default:
5263 /* Nothing else is an assignment operator. */
5264 op = ERROR_MARK;
5267 /* If it was an assignment operator, consume it. */
5268 if (op != ERROR_MARK)
5269 cp_lexer_consume_token (parser->lexer);
5271 return op;
5274 /* Parse an expression.
5276 expression:
5277 assignment-expression
5278 expression , assignment-expression
5280 Returns a representation of the expression. */
5282 static tree
5283 cp_parser_expression (cp_parser* parser)
5285 tree expression = NULL_TREE;
5287 while (true)
5289 tree assignment_expression;
5291 /* Parse the next assignment-expression. */
5292 assignment_expression
5293 = cp_parser_assignment_expression (parser);
5294 /* If this is the first assignment-expression, we can just
5295 save it away. */
5296 if (!expression)
5297 expression = assignment_expression;
5298 else
5299 expression = build_x_compound_expr (expression,
5300 assignment_expression);
5301 /* If the next token is not a comma, then we are done with the
5302 expression. */
5303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5304 break;
5305 /* Consume the `,'. */
5306 cp_lexer_consume_token (parser->lexer);
5307 /* A comma operator cannot appear in a constant-expression. */
5308 if (cp_parser_non_integral_constant_expression (parser,
5309 "a comma operator"))
5310 expression = error_mark_node;
5313 return expression;
5316 /* Parse a constant-expression.
5318 constant-expression:
5319 conditional-expression
5321 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5322 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5323 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5324 is false, NON_CONSTANT_P should be NULL. */
5326 static tree
5327 cp_parser_constant_expression (cp_parser* parser,
5328 bool allow_non_constant_p,
5329 bool *non_constant_p)
5331 bool saved_integral_constant_expression_p;
5332 bool saved_allow_non_integral_constant_expression_p;
5333 bool saved_non_integral_constant_expression_p;
5334 tree expression;
5336 /* It might seem that we could simply parse the
5337 conditional-expression, and then check to see if it were
5338 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5339 one that the compiler can figure out is constant, possibly after
5340 doing some simplifications or optimizations. The standard has a
5341 precise definition of constant-expression, and we must honor
5342 that, even though it is somewhat more restrictive.
5344 For example:
5346 int i[(2, 3)];
5348 is not a legal declaration, because `(2, 3)' is not a
5349 constant-expression. The `,' operator is forbidden in a
5350 constant-expression. However, GCC's constant-folding machinery
5351 will fold this operation to an INTEGER_CST for `3'. */
5353 /* Save the old settings. */
5354 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5355 saved_allow_non_integral_constant_expression_p
5356 = parser->allow_non_integral_constant_expression_p;
5357 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5358 /* We are now parsing a constant-expression. */
5359 parser->integral_constant_expression_p = true;
5360 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5361 parser->non_integral_constant_expression_p = false;
5362 /* Although the grammar says "conditional-expression", we parse an
5363 "assignment-expression", which also permits "throw-expression"
5364 and the use of assignment operators. In the case that
5365 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5366 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5367 actually essential that we look for an assignment-expression.
5368 For example, cp_parser_initializer_clauses uses this function to
5369 determine whether a particular assignment-expression is in fact
5370 constant. */
5371 expression = cp_parser_assignment_expression (parser);
5372 /* Restore the old settings. */
5373 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5374 parser->allow_non_integral_constant_expression_p
5375 = saved_allow_non_integral_constant_expression_p;
5376 if (allow_non_constant_p)
5377 *non_constant_p = parser->non_integral_constant_expression_p;
5378 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5380 return expression;
5383 /* Statements [gram.stmt.stmt] */
5385 /* Parse a statement.
5387 statement:
5388 labeled-statement
5389 expression-statement
5390 compound-statement
5391 selection-statement
5392 iteration-statement
5393 jump-statement
5394 declaration-statement
5395 try-block */
5397 static void
5398 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5400 tree statement;
5401 cp_token *token;
5402 int statement_line_number;
5404 /* There is no statement yet. */
5405 statement = NULL_TREE;
5406 /* Peek at the next token. */
5407 token = cp_lexer_peek_token (parser->lexer);
5408 /* Remember the line number of the first token in the statement. */
5409 statement_line_number = token->location.line;
5410 /* If this is a keyword, then that will often determine what kind of
5411 statement we have. */
5412 if (token->type == CPP_KEYWORD)
5414 enum rid keyword = token->keyword;
5416 switch (keyword)
5418 case RID_CASE:
5419 case RID_DEFAULT:
5420 statement = cp_parser_labeled_statement (parser,
5421 in_statement_expr_p);
5422 break;
5424 case RID_IF:
5425 case RID_SWITCH:
5426 statement = cp_parser_selection_statement (parser);
5427 break;
5429 case RID_WHILE:
5430 case RID_DO:
5431 case RID_FOR:
5432 statement = cp_parser_iteration_statement (parser);
5433 break;
5435 case RID_BREAK:
5436 case RID_CONTINUE:
5437 case RID_RETURN:
5438 case RID_GOTO:
5439 statement = cp_parser_jump_statement (parser);
5440 break;
5442 case RID_TRY:
5443 statement = cp_parser_try_block (parser);
5444 break;
5446 default:
5447 /* It might be a keyword like `int' that can start a
5448 declaration-statement. */
5449 break;
5452 else if (token->type == CPP_NAME)
5454 /* If the next token is a `:', then we are looking at a
5455 labeled-statement. */
5456 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5457 if (token->type == CPP_COLON)
5458 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5460 /* Anything that starts with a `{' must be a compound-statement. */
5461 else if (token->type == CPP_OPEN_BRACE)
5462 statement = cp_parser_compound_statement (parser, false);
5464 /* Everything else must be a declaration-statement or an
5465 expression-statement. Try for the declaration-statement
5466 first, unless we are looking at a `;', in which case we know that
5467 we have an expression-statement. */
5468 if (!statement)
5470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5472 cp_parser_parse_tentatively (parser);
5473 /* Try to parse the declaration-statement. */
5474 cp_parser_declaration_statement (parser);
5475 /* If that worked, we're done. */
5476 if (cp_parser_parse_definitely (parser))
5477 return;
5479 /* Look for an expression-statement instead. */
5480 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5483 /* Set the line number for the statement. */
5484 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5485 STMT_LINENO (statement) = statement_line_number;
5488 /* Parse a labeled-statement.
5490 labeled-statement:
5491 identifier : statement
5492 case constant-expression : statement
5493 default : statement
5495 GNU Extension:
5497 labeled-statement:
5498 case constant-expression ... constant-expression : statement
5500 Returns the new CASE_LABEL, for a `case' or `default' label. For
5501 an ordinary label, returns a LABEL_STMT. */
5503 static tree
5504 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5506 cp_token *token;
5507 tree statement = error_mark_node;
5509 /* The next token should be an identifier. */
5510 token = cp_lexer_peek_token (parser->lexer);
5511 if (token->type != CPP_NAME
5512 && token->type != CPP_KEYWORD)
5514 cp_parser_error (parser, "expected labeled-statement");
5515 return error_mark_node;
5518 switch (token->keyword)
5520 case RID_CASE:
5522 tree expr, expr_hi;
5523 cp_token *ellipsis;
5525 /* Consume the `case' token. */
5526 cp_lexer_consume_token (parser->lexer);
5527 /* Parse the constant-expression. */
5528 expr = cp_parser_constant_expression (parser,
5529 /*allow_non_constant_p=*/false,
5530 NULL);
5532 ellipsis = cp_lexer_peek_token (parser->lexer);
5533 if (ellipsis->type == CPP_ELLIPSIS)
5535 /* Consume the `...' token. */
5536 cp_lexer_consume_token (parser->lexer);
5537 expr_hi =
5538 cp_parser_constant_expression (parser,
5539 /*allow_non_constant_p=*/false,
5540 NULL);
5541 /* We don't need to emit warnings here, as the common code
5542 will do this for us. */
5544 else
5545 expr_hi = NULL_TREE;
5547 if (!parser->in_switch_statement_p)
5548 error ("case label `%E' not within a switch statement", expr);
5549 else
5550 statement = finish_case_label (expr, expr_hi);
5552 break;
5554 case RID_DEFAULT:
5555 /* Consume the `default' token. */
5556 cp_lexer_consume_token (parser->lexer);
5557 if (!parser->in_switch_statement_p)
5558 error ("case label not within a switch statement");
5559 else
5560 statement = finish_case_label (NULL_TREE, NULL_TREE);
5561 break;
5563 default:
5564 /* Anything else must be an ordinary label. */
5565 statement = finish_label_stmt (cp_parser_identifier (parser));
5566 break;
5569 /* Require the `:' token. */
5570 cp_parser_require (parser, CPP_COLON, "`:'");
5571 /* Parse the labeled statement. */
5572 cp_parser_statement (parser, in_statement_expr_p);
5574 /* Return the label, in the case of a `case' or `default' label. */
5575 return statement;
5578 /* Parse an expression-statement.
5580 expression-statement:
5581 expression [opt] ;
5583 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5584 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5585 indicates whether this expression-statement is part of an
5586 expression statement. */
5588 static tree
5589 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5591 tree statement = NULL_TREE;
5593 /* If the next token is a ';', then there is no expression
5594 statement. */
5595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5596 statement = cp_parser_expression (parser);
5598 /* Consume the final `;'. */
5599 cp_parser_consume_semicolon_at_end_of_statement (parser);
5601 if (in_statement_expr_p
5602 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5604 /* This is the final expression statement of a statement
5605 expression. */
5606 statement = finish_stmt_expr_expr (statement);
5608 else if (statement)
5609 statement = finish_expr_stmt (statement);
5610 else
5611 finish_stmt ();
5613 return statement;
5616 /* Parse a compound-statement.
5618 compound-statement:
5619 { statement-seq [opt] }
5621 Returns a COMPOUND_STMT representing the statement. */
5623 static tree
5624 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5626 tree compound_stmt;
5628 /* Consume the `{'. */
5629 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5630 return error_mark_node;
5631 /* Begin the compound-statement. */
5632 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5633 /* Parse an (optional) statement-seq. */
5634 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5635 /* Finish the compound-statement. */
5636 finish_compound_stmt (compound_stmt);
5637 /* Consume the `}'. */
5638 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5640 return compound_stmt;
5643 /* Parse an (optional) statement-seq.
5645 statement-seq:
5646 statement
5647 statement-seq [opt] statement */
5649 static void
5650 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5652 /* Scan statements until there aren't any more. */
5653 while (true)
5655 /* If we're looking at a `}', then we've run out of statements. */
5656 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5657 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5658 break;
5660 /* Parse the statement. */
5661 cp_parser_statement (parser, in_statement_expr_p);
5665 /* Parse a selection-statement.
5667 selection-statement:
5668 if ( condition ) statement
5669 if ( condition ) statement else statement
5670 switch ( condition ) statement
5672 Returns the new IF_STMT or SWITCH_STMT. */
5674 static tree
5675 cp_parser_selection_statement (cp_parser* parser)
5677 cp_token *token;
5678 enum rid keyword;
5680 /* Peek at the next token. */
5681 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5683 /* See what kind of keyword it is. */
5684 keyword = token->keyword;
5685 switch (keyword)
5687 case RID_IF:
5688 case RID_SWITCH:
5690 tree statement;
5691 tree condition;
5693 /* Look for the `('. */
5694 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5696 cp_parser_skip_to_end_of_statement (parser);
5697 return error_mark_node;
5700 /* Begin the selection-statement. */
5701 if (keyword == RID_IF)
5702 statement = begin_if_stmt ();
5703 else
5704 statement = begin_switch_stmt ();
5706 /* Parse the condition. */
5707 condition = cp_parser_condition (parser);
5708 /* Look for the `)'. */
5709 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5710 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5711 /*consume_paren=*/true);
5713 if (keyword == RID_IF)
5715 tree then_stmt;
5717 /* Add the condition. */
5718 finish_if_stmt_cond (condition, statement);
5720 /* Parse the then-clause. */
5721 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5722 finish_then_clause (statement);
5724 /* If the next token is `else', parse the else-clause. */
5725 if (cp_lexer_next_token_is_keyword (parser->lexer,
5726 RID_ELSE))
5728 tree else_stmt;
5730 /* Consume the `else' keyword. */
5731 cp_lexer_consume_token (parser->lexer);
5732 /* Parse the else-clause. */
5733 else_stmt
5734 = cp_parser_implicitly_scoped_statement (parser);
5735 finish_else_clause (statement);
5738 /* Now we're all done with the if-statement. */
5739 finish_if_stmt ();
5741 else
5743 tree body;
5744 bool in_switch_statement_p;
5746 /* Add the condition. */
5747 finish_switch_cond (condition, statement);
5749 /* Parse the body of the switch-statement. */
5750 in_switch_statement_p = parser->in_switch_statement_p;
5751 parser->in_switch_statement_p = true;
5752 body = cp_parser_implicitly_scoped_statement (parser);
5753 parser->in_switch_statement_p = in_switch_statement_p;
5755 /* Now we're all done with the switch-statement. */
5756 finish_switch_stmt (statement);
5759 return statement;
5761 break;
5763 default:
5764 cp_parser_error (parser, "expected selection-statement");
5765 return error_mark_node;
5769 /* Parse a condition.
5771 condition:
5772 expression
5773 type-specifier-seq declarator = assignment-expression
5775 GNU Extension:
5777 condition:
5778 type-specifier-seq declarator asm-specification [opt]
5779 attributes [opt] = assignment-expression
5781 Returns the expression that should be tested. */
5783 static tree
5784 cp_parser_condition (cp_parser* parser)
5786 tree type_specifiers;
5787 const char *saved_message;
5789 /* Try the declaration first. */
5790 cp_parser_parse_tentatively (parser);
5791 /* New types are not allowed in the type-specifier-seq for a
5792 condition. */
5793 saved_message = parser->type_definition_forbidden_message;
5794 parser->type_definition_forbidden_message
5795 = "types may not be defined in conditions";
5796 /* Parse the type-specifier-seq. */
5797 type_specifiers = cp_parser_type_specifier_seq (parser);
5798 /* Restore the saved message. */
5799 parser->type_definition_forbidden_message = saved_message;
5800 /* If all is well, we might be looking at a declaration. */
5801 if (!cp_parser_error_occurred (parser))
5803 tree decl;
5804 tree asm_specification;
5805 tree attributes;
5806 tree declarator;
5807 tree initializer = NULL_TREE;
5809 /* Parse the declarator. */
5810 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5811 /*ctor_dtor_or_conv_p=*/NULL,
5812 /*parenthesized_p=*/NULL);
5813 /* Parse the attributes. */
5814 attributes = cp_parser_attributes_opt (parser);
5815 /* Parse the asm-specification. */
5816 asm_specification = cp_parser_asm_specification_opt (parser);
5817 /* If the next token is not an `=', then we might still be
5818 looking at an expression. For example:
5820 if (A(a).x)
5822 looks like a decl-specifier-seq and a declarator -- but then
5823 there is no `=', so this is an expression. */
5824 cp_parser_require (parser, CPP_EQ, "`='");
5825 /* If we did see an `=', then we are looking at a declaration
5826 for sure. */
5827 if (cp_parser_parse_definitely (parser))
5829 /* Create the declaration. */
5830 decl = start_decl (declarator, type_specifiers,
5831 /*initialized_p=*/true,
5832 attributes, /*prefix_attributes=*/NULL_TREE);
5833 /* Parse the assignment-expression. */
5834 initializer = cp_parser_assignment_expression (parser);
5836 /* Process the initializer. */
5837 cp_finish_decl (decl,
5838 initializer,
5839 asm_specification,
5840 LOOKUP_ONLYCONVERTING);
5842 return convert_from_reference (decl);
5845 /* If we didn't even get past the declarator successfully, we are
5846 definitely not looking at a declaration. */
5847 else
5848 cp_parser_abort_tentative_parse (parser);
5850 /* Otherwise, we are looking at an expression. */
5851 return cp_parser_expression (parser);
5854 /* Parse an iteration-statement.
5856 iteration-statement:
5857 while ( condition ) statement
5858 do statement while ( expression ) ;
5859 for ( for-init-statement condition [opt] ; expression [opt] )
5860 statement
5862 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5864 static tree
5865 cp_parser_iteration_statement (cp_parser* parser)
5867 cp_token *token;
5868 enum rid keyword;
5869 tree statement;
5870 bool in_iteration_statement_p;
5873 /* Peek at the next token. */
5874 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5875 if (!token)
5876 return error_mark_node;
5878 /* Remember whether or not we are already within an iteration
5879 statement. */
5880 in_iteration_statement_p = parser->in_iteration_statement_p;
5882 /* See what kind of keyword it is. */
5883 keyword = token->keyword;
5884 switch (keyword)
5886 case RID_WHILE:
5888 tree condition;
5890 /* Begin the while-statement. */
5891 statement = begin_while_stmt ();
5892 /* Look for the `('. */
5893 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5894 /* Parse the condition. */
5895 condition = cp_parser_condition (parser);
5896 finish_while_stmt_cond (condition, statement);
5897 /* Look for the `)'. */
5898 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5899 /* Parse the dependent statement. */
5900 parser->in_iteration_statement_p = true;
5901 cp_parser_already_scoped_statement (parser);
5902 parser->in_iteration_statement_p = in_iteration_statement_p;
5903 /* We're done with the while-statement. */
5904 finish_while_stmt (statement);
5906 break;
5908 case RID_DO:
5910 tree expression;
5912 /* Begin the do-statement. */
5913 statement = begin_do_stmt ();
5914 /* Parse the body of the do-statement. */
5915 parser->in_iteration_statement_p = true;
5916 cp_parser_implicitly_scoped_statement (parser);
5917 parser->in_iteration_statement_p = in_iteration_statement_p;
5918 finish_do_body (statement);
5919 /* Look for the `while' keyword. */
5920 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5921 /* Look for the `('. */
5922 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5923 /* Parse the expression. */
5924 expression = cp_parser_expression (parser);
5925 /* We're done with the do-statement. */
5926 finish_do_stmt (expression, statement);
5927 /* Look for the `)'. */
5928 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5929 /* Look for the `;'. */
5930 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5932 break;
5934 case RID_FOR:
5936 tree condition = NULL_TREE;
5937 tree expression = NULL_TREE;
5939 /* Begin the for-statement. */
5940 statement = begin_for_stmt ();
5941 /* Look for the `('. */
5942 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5943 /* Parse the initialization. */
5944 cp_parser_for_init_statement (parser);
5945 finish_for_init_stmt (statement);
5947 /* If there's a condition, process it. */
5948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5949 condition = cp_parser_condition (parser);
5950 finish_for_cond (condition, statement);
5951 /* Look for the `;'. */
5952 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5954 /* If there's an expression, process it. */
5955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5956 expression = cp_parser_expression (parser);
5957 finish_for_expr (expression, statement);
5958 /* Look for the `)'. */
5959 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5961 /* Parse the body of the for-statement. */
5962 parser->in_iteration_statement_p = true;
5963 cp_parser_already_scoped_statement (parser);
5964 parser->in_iteration_statement_p = in_iteration_statement_p;
5966 /* We're done with the for-statement. */
5967 finish_for_stmt (statement);
5969 break;
5971 default:
5972 cp_parser_error (parser, "expected iteration-statement");
5973 statement = error_mark_node;
5974 break;
5977 return statement;
5980 /* Parse a for-init-statement.
5982 for-init-statement:
5983 expression-statement
5984 simple-declaration */
5986 static void
5987 cp_parser_for_init_statement (cp_parser* parser)
5989 /* If the next token is a `;', then we have an empty
5990 expression-statement. Grammatically, this is also a
5991 simple-declaration, but an invalid one, because it does not
5992 declare anything. Therefore, if we did not handle this case
5993 specially, we would issue an error message about an invalid
5994 declaration. */
5995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5997 /* We're going to speculatively look for a declaration, falling back
5998 to an expression, if necessary. */
5999 cp_parser_parse_tentatively (parser);
6000 /* Parse the declaration. */
6001 cp_parser_simple_declaration (parser,
6002 /*function_definition_allowed_p=*/false);
6003 /* If the tentative parse failed, then we shall need to look for an
6004 expression-statement. */
6005 if (cp_parser_parse_definitely (parser))
6006 return;
6009 cp_parser_expression_statement (parser, false);
6012 /* Parse a jump-statement.
6014 jump-statement:
6015 break ;
6016 continue ;
6017 return expression [opt] ;
6018 goto identifier ;
6020 GNU extension:
6022 jump-statement:
6023 goto * expression ;
6025 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6026 GOTO_STMT. */
6028 static tree
6029 cp_parser_jump_statement (cp_parser* parser)
6031 tree statement = error_mark_node;
6032 cp_token *token;
6033 enum rid keyword;
6035 /* Peek at the next token. */
6036 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6037 if (!token)
6038 return error_mark_node;
6040 /* See what kind of keyword it is. */
6041 keyword = token->keyword;
6042 switch (keyword)
6044 case RID_BREAK:
6045 if (!parser->in_switch_statement_p
6046 && !parser->in_iteration_statement_p)
6048 error ("break statement not within loop or switch");
6049 statement = error_mark_node;
6051 else
6052 statement = finish_break_stmt ();
6053 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6054 break;
6056 case RID_CONTINUE:
6057 if (!parser->in_iteration_statement_p)
6059 error ("continue statement not within a loop");
6060 statement = error_mark_node;
6062 else
6063 statement = finish_continue_stmt ();
6064 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6065 break;
6067 case RID_RETURN:
6069 tree expr;
6071 /* If the next token is a `;', then there is no
6072 expression. */
6073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6074 expr = cp_parser_expression (parser);
6075 else
6076 expr = NULL_TREE;
6077 /* Build the return-statement. */
6078 statement = finish_return_stmt (expr);
6079 /* Look for the final `;'. */
6080 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6082 break;
6084 case RID_GOTO:
6085 /* Create the goto-statement. */
6086 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6088 /* Issue a warning about this use of a GNU extension. */
6089 if (pedantic)
6090 pedwarn ("ISO C++ forbids computed gotos");
6091 /* Consume the '*' token. */
6092 cp_lexer_consume_token (parser->lexer);
6093 /* Parse the dependent expression. */
6094 finish_goto_stmt (cp_parser_expression (parser));
6096 else
6097 finish_goto_stmt (cp_parser_identifier (parser));
6098 /* Look for the final `;'. */
6099 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6100 break;
6102 default:
6103 cp_parser_error (parser, "expected jump-statement");
6104 break;
6107 return statement;
6110 /* Parse a declaration-statement.
6112 declaration-statement:
6113 block-declaration */
6115 static void
6116 cp_parser_declaration_statement (cp_parser* parser)
6118 /* Parse the block-declaration. */
6119 cp_parser_block_declaration (parser, /*statement_p=*/true);
6121 /* Finish off the statement. */
6122 finish_stmt ();
6125 /* Some dependent statements (like `if (cond) statement'), are
6126 implicitly in their own scope. In other words, if the statement is
6127 a single statement (as opposed to a compound-statement), it is
6128 none-the-less treated as if it were enclosed in braces. Any
6129 declarations appearing in the dependent statement are out of scope
6130 after control passes that point. This function parses a statement,
6131 but ensures that is in its own scope, even if it is not a
6132 compound-statement.
6134 Returns the new statement. */
6136 static tree
6137 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6139 tree statement;
6141 /* If the token is not a `{', then we must take special action. */
6142 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6144 /* Create a compound-statement. */
6145 statement = begin_compound_stmt (/*has_no_scope=*/false);
6146 /* Parse the dependent-statement. */
6147 cp_parser_statement (parser, false);
6148 /* Finish the dummy compound-statement. */
6149 finish_compound_stmt (statement);
6151 /* Otherwise, we simply parse the statement directly. */
6152 else
6153 statement = cp_parser_compound_statement (parser, false);
6155 /* Return the statement. */
6156 return statement;
6159 /* For some dependent statements (like `while (cond) statement'), we
6160 have already created a scope. Therefore, even if the dependent
6161 statement is a compound-statement, we do not want to create another
6162 scope. */
6164 static void
6165 cp_parser_already_scoped_statement (cp_parser* parser)
6167 /* If the token is not a `{', then we must take special action. */
6168 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6170 tree statement;
6172 /* Create a compound-statement. */
6173 statement = begin_compound_stmt (/*has_no_scope=*/true);
6174 /* Parse the dependent-statement. */
6175 cp_parser_statement (parser, false);
6176 /* Finish the dummy compound-statement. */
6177 finish_compound_stmt (statement);
6179 /* Otherwise, we simply parse the statement directly. */
6180 else
6181 cp_parser_statement (parser, false);
6184 /* Declarations [gram.dcl.dcl] */
6186 /* Parse an optional declaration-sequence.
6188 declaration-seq:
6189 declaration
6190 declaration-seq declaration */
6192 static void
6193 cp_parser_declaration_seq_opt (cp_parser* parser)
6195 while (true)
6197 cp_token *token;
6199 token = cp_lexer_peek_token (parser->lexer);
6201 if (token->type == CPP_CLOSE_BRACE
6202 || token->type == CPP_EOF)
6203 break;
6205 if (token->type == CPP_SEMICOLON)
6207 /* A declaration consisting of a single semicolon is
6208 invalid. Allow it unless we're being pedantic. */
6209 if (pedantic && !in_system_header)
6210 pedwarn ("extra `;'");
6211 cp_lexer_consume_token (parser->lexer);
6212 continue;
6215 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6216 parser to enter or exit implicit `extern "C"' blocks. */
6217 while (pending_lang_change > 0)
6219 push_lang_context (lang_name_c);
6220 --pending_lang_change;
6222 while (pending_lang_change < 0)
6224 pop_lang_context ();
6225 ++pending_lang_change;
6228 /* Parse the declaration itself. */
6229 cp_parser_declaration (parser);
6233 /* Parse a declaration.
6235 declaration:
6236 block-declaration
6237 function-definition
6238 template-declaration
6239 explicit-instantiation
6240 explicit-specialization
6241 linkage-specification
6242 namespace-definition
6244 GNU extension:
6246 declaration:
6247 __extension__ declaration */
6249 static void
6250 cp_parser_declaration (cp_parser* parser)
6252 cp_token token1;
6253 cp_token token2;
6254 int saved_pedantic;
6256 /* Check for the `__extension__' keyword. */
6257 if (cp_parser_extension_opt (parser, &saved_pedantic))
6259 /* Parse the qualified declaration. */
6260 cp_parser_declaration (parser);
6261 /* Restore the PEDANTIC flag. */
6262 pedantic = saved_pedantic;
6264 return;
6267 /* Try to figure out what kind of declaration is present. */
6268 token1 = *cp_lexer_peek_token (parser->lexer);
6269 if (token1.type != CPP_EOF)
6270 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6272 /* If the next token is `extern' and the following token is a string
6273 literal, then we have a linkage specification. */
6274 if (token1.keyword == RID_EXTERN
6275 && cp_parser_is_string_literal (&token2))
6276 cp_parser_linkage_specification (parser);
6277 /* If the next token is `template', then we have either a template
6278 declaration, an explicit instantiation, or an explicit
6279 specialization. */
6280 else if (token1.keyword == RID_TEMPLATE)
6282 /* `template <>' indicates a template specialization. */
6283 if (token2.type == CPP_LESS
6284 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6285 cp_parser_explicit_specialization (parser);
6286 /* `template <' indicates a template declaration. */
6287 else if (token2.type == CPP_LESS)
6288 cp_parser_template_declaration (parser, /*member_p=*/false);
6289 /* Anything else must be an explicit instantiation. */
6290 else
6291 cp_parser_explicit_instantiation (parser);
6293 /* If the next token is `export', then we have a template
6294 declaration. */
6295 else if (token1.keyword == RID_EXPORT)
6296 cp_parser_template_declaration (parser, /*member_p=*/false);
6297 /* If the next token is `extern', 'static' or 'inline' and the one
6298 after that is `template', we have a GNU extended explicit
6299 instantiation directive. */
6300 else if (cp_parser_allow_gnu_extensions_p (parser)
6301 && (token1.keyword == RID_EXTERN
6302 || token1.keyword == RID_STATIC
6303 || token1.keyword == RID_INLINE)
6304 && token2.keyword == RID_TEMPLATE)
6305 cp_parser_explicit_instantiation (parser);
6306 /* If the next token is `namespace', check for a named or unnamed
6307 namespace definition. */
6308 else if (token1.keyword == RID_NAMESPACE
6309 && (/* A named namespace definition. */
6310 (token2.type == CPP_NAME
6311 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6312 == CPP_OPEN_BRACE))
6313 /* An unnamed namespace definition. */
6314 || token2.type == CPP_OPEN_BRACE))
6315 cp_parser_namespace_definition (parser);
6316 /* We must have either a block declaration or a function
6317 definition. */
6318 else
6319 /* Try to parse a block-declaration, or a function-definition. */
6320 cp_parser_block_declaration (parser, /*statement_p=*/false);
6323 /* Parse a block-declaration.
6325 block-declaration:
6326 simple-declaration
6327 asm-definition
6328 namespace-alias-definition
6329 using-declaration
6330 using-directive
6332 GNU Extension:
6334 block-declaration:
6335 __extension__ block-declaration
6336 label-declaration
6338 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6339 part of a declaration-statement. */
6341 static void
6342 cp_parser_block_declaration (cp_parser *parser,
6343 bool statement_p)
6345 cp_token *token1;
6346 int saved_pedantic;
6348 /* Check for the `__extension__' keyword. */
6349 if (cp_parser_extension_opt (parser, &saved_pedantic))
6351 /* Parse the qualified declaration. */
6352 cp_parser_block_declaration (parser, statement_p);
6353 /* Restore the PEDANTIC flag. */
6354 pedantic = saved_pedantic;
6356 return;
6359 /* Peek at the next token to figure out which kind of declaration is
6360 present. */
6361 token1 = cp_lexer_peek_token (parser->lexer);
6363 /* If the next keyword is `asm', we have an asm-definition. */
6364 if (token1->keyword == RID_ASM)
6366 if (statement_p)
6367 cp_parser_commit_to_tentative_parse (parser);
6368 cp_parser_asm_definition (parser);
6370 /* If the next keyword is `namespace', we have a
6371 namespace-alias-definition. */
6372 else if (token1->keyword == RID_NAMESPACE)
6373 cp_parser_namespace_alias_definition (parser);
6374 /* If the next keyword is `using', we have either a
6375 using-declaration or a using-directive. */
6376 else if (token1->keyword == RID_USING)
6378 cp_token *token2;
6380 if (statement_p)
6381 cp_parser_commit_to_tentative_parse (parser);
6382 /* If the token after `using' is `namespace', then we have a
6383 using-directive. */
6384 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6385 if (token2->keyword == RID_NAMESPACE)
6386 cp_parser_using_directive (parser);
6387 /* Otherwise, it's a using-declaration. */
6388 else
6389 cp_parser_using_declaration (parser);
6391 /* If the next keyword is `__label__' we have a label declaration. */
6392 else if (token1->keyword == RID_LABEL)
6394 if (statement_p)
6395 cp_parser_commit_to_tentative_parse (parser);
6396 cp_parser_label_declaration (parser);
6398 /* Anything else must be a simple-declaration. */
6399 else
6400 cp_parser_simple_declaration (parser, !statement_p);
6403 /* Parse a simple-declaration.
6405 simple-declaration:
6406 decl-specifier-seq [opt] init-declarator-list [opt] ;
6408 init-declarator-list:
6409 init-declarator
6410 init-declarator-list , init-declarator
6412 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6413 function-definition as a simple-declaration. */
6415 static void
6416 cp_parser_simple_declaration (cp_parser* parser,
6417 bool function_definition_allowed_p)
6419 tree decl_specifiers;
6420 tree attributes;
6421 int declares_class_or_enum;
6422 bool saw_declarator;
6424 /* Defer access checks until we know what is being declared; the
6425 checks for names appearing in the decl-specifier-seq should be
6426 done as if we were in the scope of the thing being declared. */
6427 push_deferring_access_checks (dk_deferred);
6429 /* Parse the decl-specifier-seq. We have to keep track of whether
6430 or not the decl-specifier-seq declares a named class or
6431 enumeration type, since that is the only case in which the
6432 init-declarator-list is allowed to be empty.
6434 [dcl.dcl]
6436 In a simple-declaration, the optional init-declarator-list can be
6437 omitted only when declaring a class or enumeration, that is when
6438 the decl-specifier-seq contains either a class-specifier, an
6439 elaborated-type-specifier, or an enum-specifier. */
6440 decl_specifiers
6441 = cp_parser_decl_specifier_seq (parser,
6442 CP_PARSER_FLAGS_OPTIONAL,
6443 &attributes,
6444 &declares_class_or_enum);
6445 /* We no longer need to defer access checks. */
6446 stop_deferring_access_checks ();
6448 /* In a block scope, a valid declaration must always have a
6449 decl-specifier-seq. By not trying to parse declarators, we can
6450 resolve the declaration/expression ambiguity more quickly. */
6451 if (!function_definition_allowed_p && !decl_specifiers)
6453 cp_parser_error (parser, "expected declaration");
6454 goto done;
6457 /* If the next two tokens are both identifiers, the code is
6458 erroneous. The usual cause of this situation is code like:
6460 T t;
6462 where "T" should name a type -- but does not. */
6463 if (cp_parser_diagnose_invalid_type_name (parser))
6465 /* If parsing tentatively, we should commit; we really are
6466 looking at a declaration. */
6467 cp_parser_commit_to_tentative_parse (parser);
6468 /* Give up. */
6469 goto done;
6472 /* Keep going until we hit the `;' at the end of the simple
6473 declaration. */
6474 saw_declarator = false;
6475 while (cp_lexer_next_token_is_not (parser->lexer,
6476 CPP_SEMICOLON))
6478 cp_token *token;
6479 bool function_definition_p;
6480 tree decl;
6482 saw_declarator = true;
6483 /* Parse the init-declarator. */
6484 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6485 function_definition_allowed_p,
6486 /*member_p=*/false,
6487 declares_class_or_enum,
6488 &function_definition_p);
6489 /* If an error occurred while parsing tentatively, exit quickly.
6490 (That usually happens when in the body of a function; each
6491 statement is treated as a declaration-statement until proven
6492 otherwise.) */
6493 if (cp_parser_error_occurred (parser))
6494 goto done;
6495 /* Handle function definitions specially. */
6496 if (function_definition_p)
6498 /* If the next token is a `,', then we are probably
6499 processing something like:
6501 void f() {}, *p;
6503 which is erroneous. */
6504 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6505 error ("mixing declarations and function-definitions is forbidden");
6506 /* Otherwise, we're done with the list of declarators. */
6507 else
6509 pop_deferring_access_checks ();
6510 return;
6513 /* The next token should be either a `,' or a `;'. */
6514 token = cp_lexer_peek_token (parser->lexer);
6515 /* If it's a `,', there are more declarators to come. */
6516 if (token->type == CPP_COMMA)
6517 cp_lexer_consume_token (parser->lexer);
6518 /* If it's a `;', we are done. */
6519 else if (token->type == CPP_SEMICOLON)
6520 break;
6521 /* Anything else is an error. */
6522 else
6524 cp_parser_error (parser, "expected `,' or `;'");
6525 /* Skip tokens until we reach the end of the statement. */
6526 cp_parser_skip_to_end_of_statement (parser);
6527 /* If the next token is now a `;', consume it. */
6528 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6529 cp_lexer_consume_token (parser->lexer);
6530 goto done;
6532 /* After the first time around, a function-definition is not
6533 allowed -- even if it was OK at first. For example:
6535 int i, f() {}
6537 is not valid. */
6538 function_definition_allowed_p = false;
6541 /* Issue an error message if no declarators are present, and the
6542 decl-specifier-seq does not itself declare a class or
6543 enumeration. */
6544 if (!saw_declarator)
6546 if (cp_parser_declares_only_class_p (parser))
6547 shadow_tag (decl_specifiers);
6548 /* Perform any deferred access checks. */
6549 perform_deferred_access_checks ();
6552 /* Consume the `;'. */
6553 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6555 done:
6556 pop_deferring_access_checks ();
6559 /* Parse a decl-specifier-seq.
6561 decl-specifier-seq:
6562 decl-specifier-seq [opt] decl-specifier
6564 decl-specifier:
6565 storage-class-specifier
6566 type-specifier
6567 function-specifier
6568 friend
6569 typedef
6571 GNU Extension:
6573 decl-specifier:
6574 attributes
6576 Returns a TREE_LIST, giving the decl-specifiers in the order they
6577 appear in the source code. The TREE_VALUE of each node is the
6578 decl-specifier. For a keyword (such as `auto' or `friend'), the
6579 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6580 representation of a type-specifier, see cp_parser_type_specifier.
6582 If there are attributes, they will be stored in *ATTRIBUTES,
6583 represented as described above cp_parser_attributes.
6585 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6586 appears, and the entity that will be a friend is not going to be a
6587 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6588 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6589 friendship is granted might not be a class.
6591 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6592 flags:
6594 1: one of the decl-specifiers is an elaborated-type-specifier
6595 (i.e., a type declaration)
6596 2: one of the decl-specifiers is an enum-specifier or a
6597 class-specifier (i.e., a type definition)
6601 static tree
6602 cp_parser_decl_specifier_seq (cp_parser* parser,
6603 cp_parser_flags flags,
6604 tree* attributes,
6605 int* declares_class_or_enum)
6607 tree decl_specs = NULL_TREE;
6608 bool friend_p = false;
6609 bool constructor_possible_p = !parser->in_declarator_p;
6611 /* Assume no class or enumeration type is declared. */
6612 *declares_class_or_enum = 0;
6614 /* Assume there are no attributes. */
6615 *attributes = NULL_TREE;
6617 /* Keep reading specifiers until there are no more to read. */
6618 while (true)
6620 tree decl_spec = NULL_TREE;
6621 bool constructor_p;
6622 cp_token *token;
6624 /* Peek at the next token. */
6625 token = cp_lexer_peek_token (parser->lexer);
6626 /* Handle attributes. */
6627 if (token->keyword == RID_ATTRIBUTE)
6629 /* Parse the attributes. */
6630 decl_spec = cp_parser_attributes_opt (parser);
6631 /* Add them to the list. */
6632 *attributes = chainon (*attributes, decl_spec);
6633 continue;
6635 /* If the next token is an appropriate keyword, we can simply
6636 add it to the list. */
6637 switch (token->keyword)
6639 case RID_FRIEND:
6640 /* decl-specifier:
6641 friend */
6642 if (friend_p)
6643 error ("duplicate `friend'");
6644 else
6645 friend_p = true;
6646 /* The representation of the specifier is simply the
6647 appropriate TREE_IDENTIFIER node. */
6648 decl_spec = token->value;
6649 /* Consume the token. */
6650 cp_lexer_consume_token (parser->lexer);
6651 break;
6653 /* function-specifier:
6654 inline
6655 virtual
6656 explicit */
6657 case RID_INLINE:
6658 case RID_VIRTUAL:
6659 case RID_EXPLICIT:
6660 decl_spec = cp_parser_function_specifier_opt (parser);
6661 break;
6663 /* decl-specifier:
6664 typedef */
6665 case RID_TYPEDEF:
6666 /* The representation of the specifier is simply the
6667 appropriate TREE_IDENTIFIER node. */
6668 decl_spec = token->value;
6669 /* Consume the token. */
6670 cp_lexer_consume_token (parser->lexer);
6671 /* A constructor declarator cannot appear in a typedef. */
6672 constructor_possible_p = false;
6673 /* The "typedef" keyword can only occur in a declaration; we
6674 may as well commit at this point. */
6675 cp_parser_commit_to_tentative_parse (parser);
6676 break;
6678 /* storage-class-specifier:
6679 auto
6680 register
6681 static
6682 extern
6683 mutable
6685 GNU Extension:
6686 thread */
6687 case RID_AUTO:
6688 case RID_REGISTER:
6689 case RID_STATIC:
6690 case RID_EXTERN:
6691 case RID_MUTABLE:
6692 case RID_THREAD:
6693 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6694 break;
6696 default:
6697 break;
6700 /* Constructors are a special case. The `S' in `S()' is not a
6701 decl-specifier; it is the beginning of the declarator. */
6702 constructor_p = (!decl_spec
6703 && constructor_possible_p
6704 && cp_parser_constructor_declarator_p (parser,
6705 friend_p));
6707 /* If we don't have a DECL_SPEC yet, then we must be looking at
6708 a type-specifier. */
6709 if (!decl_spec && !constructor_p)
6711 int decl_spec_declares_class_or_enum;
6712 bool is_cv_qualifier;
6714 decl_spec
6715 = cp_parser_type_specifier (parser, flags,
6716 friend_p,
6717 /*is_declaration=*/true,
6718 &decl_spec_declares_class_or_enum,
6719 &is_cv_qualifier);
6721 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6723 /* If this type-specifier referenced a user-defined type
6724 (a typedef, class-name, etc.), then we can't allow any
6725 more such type-specifiers henceforth.
6727 [dcl.spec]
6729 The longest sequence of decl-specifiers that could
6730 possibly be a type name is taken as the
6731 decl-specifier-seq of a declaration. The sequence shall
6732 be self-consistent as described below.
6734 [dcl.type]
6736 As a general rule, at most one type-specifier is allowed
6737 in the complete decl-specifier-seq of a declaration. The
6738 only exceptions are the following:
6740 -- const or volatile can be combined with any other
6741 type-specifier.
6743 -- signed or unsigned can be combined with char, long,
6744 short, or int.
6746 -- ..
6748 Example:
6750 typedef char* Pc;
6751 void g (const int Pc);
6753 Here, Pc is *not* part of the decl-specifier seq; it's
6754 the declarator. Therefore, once we see a type-specifier
6755 (other than a cv-qualifier), we forbid any additional
6756 user-defined types. We *do* still allow things like `int
6757 int' to be considered a decl-specifier-seq, and issue the
6758 error message later. */
6759 if (decl_spec && !is_cv_qualifier)
6760 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6761 /* A constructor declarator cannot follow a type-specifier. */
6762 if (decl_spec)
6763 constructor_possible_p = false;
6766 /* If we still do not have a DECL_SPEC, then there are no more
6767 decl-specifiers. */
6768 if (!decl_spec)
6770 /* Issue an error message, unless the entire construct was
6771 optional. */
6772 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6774 cp_parser_error (parser, "expected decl specifier");
6775 return error_mark_node;
6778 break;
6781 /* Add the DECL_SPEC to the list of specifiers. */
6782 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6783 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6785 /* After we see one decl-specifier, further decl-specifiers are
6786 always optional. */
6787 flags |= CP_PARSER_FLAGS_OPTIONAL;
6790 /* Don't allow a friend specifier with a class definition. */
6791 if (friend_p && (*declares_class_or_enum & 2))
6792 error ("class definition may not be declared a friend");
6794 /* We have built up the DECL_SPECS in reverse order. Return them in
6795 the correct order. */
6796 return nreverse (decl_specs);
6799 /* Parse an (optional) storage-class-specifier.
6801 storage-class-specifier:
6802 auto
6803 register
6804 static
6805 extern
6806 mutable
6808 GNU Extension:
6810 storage-class-specifier:
6811 thread
6813 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6815 static tree
6816 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6818 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6820 case RID_AUTO:
6821 case RID_REGISTER:
6822 case RID_STATIC:
6823 case RID_EXTERN:
6824 case RID_MUTABLE:
6825 case RID_THREAD:
6826 /* Consume the token. */
6827 return cp_lexer_consume_token (parser->lexer)->value;
6829 default:
6830 return NULL_TREE;
6834 /* Parse an (optional) function-specifier.
6836 function-specifier:
6837 inline
6838 virtual
6839 explicit
6841 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6843 static tree
6844 cp_parser_function_specifier_opt (cp_parser* parser)
6846 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6848 case RID_INLINE:
6849 case RID_VIRTUAL:
6850 case RID_EXPLICIT:
6851 /* Consume the token. */
6852 return cp_lexer_consume_token (parser->lexer)->value;
6854 default:
6855 return NULL_TREE;
6859 /* Parse a linkage-specification.
6861 linkage-specification:
6862 extern string-literal { declaration-seq [opt] }
6863 extern string-literal declaration */
6865 static void
6866 cp_parser_linkage_specification (cp_parser* parser)
6868 cp_token *token;
6869 tree linkage;
6871 /* Look for the `extern' keyword. */
6872 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6874 /* Peek at the next token. */
6875 token = cp_lexer_peek_token (parser->lexer);
6876 /* If it's not a string-literal, then there's a problem. */
6877 if (!cp_parser_is_string_literal (token))
6879 cp_parser_error (parser, "expected language-name");
6880 return;
6882 /* Consume the token. */
6883 cp_lexer_consume_token (parser->lexer);
6885 /* Transform the literal into an identifier. If the literal is a
6886 wide-character string, or contains embedded NULs, then we can't
6887 handle it as the user wants. */
6888 if (token->type == CPP_WSTRING
6889 || (strlen (TREE_STRING_POINTER (token->value))
6890 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6892 cp_parser_error (parser, "invalid linkage-specification");
6893 /* Assume C++ linkage. */
6894 linkage = get_identifier ("c++");
6896 /* If it's a simple string constant, things are easier. */
6897 else
6898 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6900 /* We're now using the new linkage. */
6901 push_lang_context (linkage);
6903 /* If the next token is a `{', then we're using the first
6904 production. */
6905 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6907 /* Consume the `{' token. */
6908 cp_lexer_consume_token (parser->lexer);
6909 /* Parse the declarations. */
6910 cp_parser_declaration_seq_opt (parser);
6911 /* Look for the closing `}'. */
6912 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6914 /* Otherwise, there's just one declaration. */
6915 else
6917 bool saved_in_unbraced_linkage_specification_p;
6919 saved_in_unbraced_linkage_specification_p
6920 = parser->in_unbraced_linkage_specification_p;
6921 parser->in_unbraced_linkage_specification_p = true;
6922 have_extern_spec = true;
6923 cp_parser_declaration (parser);
6924 have_extern_spec = false;
6925 parser->in_unbraced_linkage_specification_p
6926 = saved_in_unbraced_linkage_specification_p;
6929 /* We're done with the linkage-specification. */
6930 pop_lang_context ();
6933 /* Special member functions [gram.special] */
6935 /* Parse a conversion-function-id.
6937 conversion-function-id:
6938 operator conversion-type-id
6940 Returns an IDENTIFIER_NODE representing the operator. */
6942 static tree
6943 cp_parser_conversion_function_id (cp_parser* parser)
6945 tree type;
6946 tree saved_scope;
6947 tree saved_qualifying_scope;
6948 tree saved_object_scope;
6949 bool pop_p = false;
6951 /* Look for the `operator' token. */
6952 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6953 return error_mark_node;
6954 /* When we parse the conversion-type-id, the current scope will be
6955 reset. However, we need that information in able to look up the
6956 conversion function later, so we save it here. */
6957 saved_scope = parser->scope;
6958 saved_qualifying_scope = parser->qualifying_scope;
6959 saved_object_scope = parser->object_scope;
6960 /* We must enter the scope of the class so that the names of
6961 entities declared within the class are available in the
6962 conversion-type-id. For example, consider:
6964 struct S {
6965 typedef int I;
6966 operator I();
6969 S::operator I() { ... }
6971 In order to see that `I' is a type-name in the definition, we
6972 must be in the scope of `S'. */
6973 if (saved_scope)
6974 pop_p = push_scope (saved_scope);
6975 /* Parse the conversion-type-id. */
6976 type = cp_parser_conversion_type_id (parser);
6977 /* Leave the scope of the class, if any. */
6978 if (pop_p)
6979 pop_scope (saved_scope);
6980 /* Restore the saved scope. */
6981 parser->scope = saved_scope;
6982 parser->qualifying_scope = saved_qualifying_scope;
6983 parser->object_scope = saved_object_scope;
6984 /* If the TYPE is invalid, indicate failure. */
6985 if (type == error_mark_node)
6986 return error_mark_node;
6987 return mangle_conv_op_name_for_type (type);
6990 /* Parse a conversion-type-id:
6992 conversion-type-id:
6993 type-specifier-seq conversion-declarator [opt]
6995 Returns the TYPE specified. */
6997 static tree
6998 cp_parser_conversion_type_id (cp_parser* parser)
7000 tree attributes;
7001 tree type_specifiers;
7002 tree declarator;
7004 /* Parse the attributes. */
7005 attributes = cp_parser_attributes_opt (parser);
7006 /* Parse the type-specifiers. */
7007 type_specifiers = cp_parser_type_specifier_seq (parser);
7008 /* If that didn't work, stop. */
7009 if (type_specifiers == error_mark_node)
7010 return error_mark_node;
7011 /* Parse the conversion-declarator. */
7012 declarator = cp_parser_conversion_declarator_opt (parser);
7014 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7015 /*initialized=*/0, &attributes);
7018 /* Parse an (optional) conversion-declarator.
7020 conversion-declarator:
7021 ptr-operator conversion-declarator [opt]
7023 Returns a representation of the declarator. See
7024 cp_parser_declarator for details. */
7026 static tree
7027 cp_parser_conversion_declarator_opt (cp_parser* parser)
7029 enum tree_code code;
7030 tree class_type;
7031 tree cv_qualifier_seq;
7033 /* We don't know if there's a ptr-operator next, or not. */
7034 cp_parser_parse_tentatively (parser);
7035 /* Try the ptr-operator. */
7036 code = cp_parser_ptr_operator (parser, &class_type,
7037 &cv_qualifier_seq);
7038 /* If it worked, look for more conversion-declarators. */
7039 if (cp_parser_parse_definitely (parser))
7041 tree declarator;
7043 /* Parse another optional declarator. */
7044 declarator = cp_parser_conversion_declarator_opt (parser);
7046 /* Create the representation of the declarator. */
7047 if (code == INDIRECT_REF)
7048 declarator = make_pointer_declarator (cv_qualifier_seq,
7049 declarator);
7050 else
7051 declarator = make_reference_declarator (cv_qualifier_seq,
7052 declarator);
7054 /* Handle the pointer-to-member case. */
7055 if (class_type)
7056 declarator = build_nt (SCOPE_REF, class_type, declarator);
7058 return declarator;
7061 return NULL_TREE;
7064 /* Parse an (optional) ctor-initializer.
7066 ctor-initializer:
7067 : mem-initializer-list
7069 Returns TRUE iff the ctor-initializer was actually present. */
7071 static bool
7072 cp_parser_ctor_initializer_opt (cp_parser* parser)
7074 /* If the next token is not a `:', then there is no
7075 ctor-initializer. */
7076 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7078 /* Do default initialization of any bases and members. */
7079 if (DECL_CONSTRUCTOR_P (current_function_decl))
7080 finish_mem_initializers (NULL_TREE);
7082 return false;
7085 /* Consume the `:' token. */
7086 cp_lexer_consume_token (parser->lexer);
7087 /* And the mem-initializer-list. */
7088 cp_parser_mem_initializer_list (parser);
7090 return true;
7093 /* Parse a mem-initializer-list.
7095 mem-initializer-list:
7096 mem-initializer
7097 mem-initializer , mem-initializer-list */
7099 static void
7100 cp_parser_mem_initializer_list (cp_parser* parser)
7102 tree mem_initializer_list = NULL_TREE;
7104 /* Let the semantic analysis code know that we are starting the
7105 mem-initializer-list. */
7106 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7107 error ("only constructors take base initializers");
7109 /* Loop through the list. */
7110 while (true)
7112 tree mem_initializer;
7114 /* Parse the mem-initializer. */
7115 mem_initializer = cp_parser_mem_initializer (parser);
7116 /* Add it to the list, unless it was erroneous. */
7117 if (mem_initializer)
7119 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7120 mem_initializer_list = mem_initializer;
7122 /* If the next token is not a `,', we're done. */
7123 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7124 break;
7125 /* Consume the `,' token. */
7126 cp_lexer_consume_token (parser->lexer);
7129 /* Perform semantic analysis. */
7130 if (DECL_CONSTRUCTOR_P (current_function_decl))
7131 finish_mem_initializers (mem_initializer_list);
7134 /* Parse a mem-initializer.
7136 mem-initializer:
7137 mem-initializer-id ( expression-list [opt] )
7139 GNU extension:
7141 mem-initializer:
7142 ( expression-list [opt] )
7144 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7145 class) or FIELD_DECL (for a non-static data member) to initialize;
7146 the TREE_VALUE is the expression-list. */
7148 static tree
7149 cp_parser_mem_initializer (cp_parser* parser)
7151 tree mem_initializer_id;
7152 tree expression_list;
7153 tree member;
7155 /* Find out what is being initialized. */
7156 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7158 pedwarn ("anachronistic old-style base class initializer");
7159 mem_initializer_id = NULL_TREE;
7161 else
7162 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7163 member = expand_member_init (mem_initializer_id);
7164 if (member && !DECL_P (member))
7165 in_base_initializer = 1;
7167 expression_list
7168 = cp_parser_parenthesized_expression_list (parser, false,
7169 /*non_constant_p=*/NULL);
7170 if (!expression_list)
7171 expression_list = void_type_node;
7173 in_base_initializer = 0;
7175 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7178 /* Parse a mem-initializer-id.
7180 mem-initializer-id:
7181 :: [opt] nested-name-specifier [opt] class-name
7182 identifier
7184 Returns a TYPE indicating the class to be initializer for the first
7185 production. Returns an IDENTIFIER_NODE indicating the data member
7186 to be initialized for the second production. */
7188 static tree
7189 cp_parser_mem_initializer_id (cp_parser* parser)
7191 bool global_scope_p;
7192 bool nested_name_specifier_p;
7193 tree id;
7195 /* Look for the optional `::' operator. */
7196 global_scope_p
7197 = (cp_parser_global_scope_opt (parser,
7198 /*current_scope_valid_p=*/false)
7199 != NULL_TREE);
7200 /* Look for the optional nested-name-specifier. The simplest way to
7201 implement:
7203 [temp.res]
7205 The keyword `typename' is not permitted in a base-specifier or
7206 mem-initializer; in these contexts a qualified name that
7207 depends on a template-parameter is implicitly assumed to be a
7208 type name.
7210 is to assume that we have seen the `typename' keyword at this
7211 point. */
7212 nested_name_specifier_p
7213 = (cp_parser_nested_name_specifier_opt (parser,
7214 /*typename_keyword_p=*/true,
7215 /*check_dependency_p=*/true,
7216 /*type_p=*/true,
7217 /*is_declaration=*/true)
7218 != NULL_TREE);
7219 /* If there is a `::' operator or a nested-name-specifier, then we
7220 are definitely looking for a class-name. */
7221 if (global_scope_p || nested_name_specifier_p)
7222 return cp_parser_class_name (parser,
7223 /*typename_keyword_p=*/true,
7224 /*template_keyword_p=*/false,
7225 /*type_p=*/false,
7226 /*check_dependency_p=*/true,
7227 /*class_head_p=*/false,
7228 /*is_declaration=*/true);
7229 /* Otherwise, we could also be looking for an ordinary identifier. */
7230 cp_parser_parse_tentatively (parser);
7231 /* Try a class-name. */
7232 id = cp_parser_class_name (parser,
7233 /*typename_keyword_p=*/true,
7234 /*template_keyword_p=*/false,
7235 /*type_p=*/false,
7236 /*check_dependency_p=*/true,
7237 /*class_head_p=*/false,
7238 /*is_declaration=*/true);
7239 /* If we found one, we're done. */
7240 if (cp_parser_parse_definitely (parser))
7241 return id;
7242 /* Otherwise, look for an ordinary identifier. */
7243 return cp_parser_identifier (parser);
7246 /* Overloading [gram.over] */
7248 /* Parse an operator-function-id.
7250 operator-function-id:
7251 operator operator
7253 Returns an IDENTIFIER_NODE for the operator which is a
7254 human-readable spelling of the identifier, e.g., `operator +'. */
7256 static tree
7257 cp_parser_operator_function_id (cp_parser* parser)
7259 /* Look for the `operator' keyword. */
7260 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7261 return error_mark_node;
7262 /* And then the name of the operator itself. */
7263 return cp_parser_operator (parser);
7266 /* Parse an operator.
7268 operator:
7269 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7270 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7271 || ++ -- , ->* -> () []
7273 GNU Extensions:
7275 operator:
7276 <? >? <?= >?=
7278 Returns an IDENTIFIER_NODE for the operator which is a
7279 human-readable spelling of the identifier, e.g., `operator +'. */
7281 static tree
7282 cp_parser_operator (cp_parser* parser)
7284 tree id = NULL_TREE;
7285 cp_token *token;
7287 /* Peek at the next token. */
7288 token = cp_lexer_peek_token (parser->lexer);
7289 /* Figure out which operator we have. */
7290 switch (token->type)
7292 case CPP_KEYWORD:
7294 enum tree_code op;
7296 /* The keyword should be either `new' or `delete'. */
7297 if (token->keyword == RID_NEW)
7298 op = NEW_EXPR;
7299 else if (token->keyword == RID_DELETE)
7300 op = DELETE_EXPR;
7301 else
7302 break;
7304 /* Consume the `new' or `delete' token. */
7305 cp_lexer_consume_token (parser->lexer);
7307 /* Peek at the next token. */
7308 token = cp_lexer_peek_token (parser->lexer);
7309 /* If it's a `[' token then this is the array variant of the
7310 operator. */
7311 if (token->type == CPP_OPEN_SQUARE)
7313 /* Consume the `[' token. */
7314 cp_lexer_consume_token (parser->lexer);
7315 /* Look for the `]' token. */
7316 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7317 id = ansi_opname (op == NEW_EXPR
7318 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7320 /* Otherwise, we have the non-array variant. */
7321 else
7322 id = ansi_opname (op);
7324 return id;
7327 case CPP_PLUS:
7328 id = ansi_opname (PLUS_EXPR);
7329 break;
7331 case CPP_MINUS:
7332 id = ansi_opname (MINUS_EXPR);
7333 break;
7335 case CPP_MULT:
7336 id = ansi_opname (MULT_EXPR);
7337 break;
7339 case CPP_DIV:
7340 id = ansi_opname (TRUNC_DIV_EXPR);
7341 break;
7343 case CPP_MOD:
7344 id = ansi_opname (TRUNC_MOD_EXPR);
7345 break;
7347 case CPP_XOR:
7348 id = ansi_opname (BIT_XOR_EXPR);
7349 break;
7351 case CPP_AND:
7352 id = ansi_opname (BIT_AND_EXPR);
7353 break;
7355 case CPP_OR:
7356 id = ansi_opname (BIT_IOR_EXPR);
7357 break;
7359 case CPP_COMPL:
7360 id = ansi_opname (BIT_NOT_EXPR);
7361 break;
7363 case CPP_NOT:
7364 id = ansi_opname (TRUTH_NOT_EXPR);
7365 break;
7367 case CPP_EQ:
7368 id = ansi_assopname (NOP_EXPR);
7369 break;
7371 case CPP_LESS:
7372 id = ansi_opname (LT_EXPR);
7373 break;
7375 case CPP_GREATER:
7376 id = ansi_opname (GT_EXPR);
7377 break;
7379 case CPP_PLUS_EQ:
7380 id = ansi_assopname (PLUS_EXPR);
7381 break;
7383 case CPP_MINUS_EQ:
7384 id = ansi_assopname (MINUS_EXPR);
7385 break;
7387 case CPP_MULT_EQ:
7388 id = ansi_assopname (MULT_EXPR);
7389 break;
7391 case CPP_DIV_EQ:
7392 id = ansi_assopname (TRUNC_DIV_EXPR);
7393 break;
7395 case CPP_MOD_EQ:
7396 id = ansi_assopname (TRUNC_MOD_EXPR);
7397 break;
7399 case CPP_XOR_EQ:
7400 id = ansi_assopname (BIT_XOR_EXPR);
7401 break;
7403 case CPP_AND_EQ:
7404 id = ansi_assopname (BIT_AND_EXPR);
7405 break;
7407 case CPP_OR_EQ:
7408 id = ansi_assopname (BIT_IOR_EXPR);
7409 break;
7411 case CPP_LSHIFT:
7412 id = ansi_opname (LSHIFT_EXPR);
7413 break;
7415 case CPP_RSHIFT:
7416 id = ansi_opname (RSHIFT_EXPR);
7417 break;
7419 case CPP_LSHIFT_EQ:
7420 id = ansi_assopname (LSHIFT_EXPR);
7421 break;
7423 case CPP_RSHIFT_EQ:
7424 id = ansi_assopname (RSHIFT_EXPR);
7425 break;
7427 case CPP_EQ_EQ:
7428 id = ansi_opname (EQ_EXPR);
7429 break;
7431 case CPP_NOT_EQ:
7432 id = ansi_opname (NE_EXPR);
7433 break;
7435 case CPP_LESS_EQ:
7436 id = ansi_opname (LE_EXPR);
7437 break;
7439 case CPP_GREATER_EQ:
7440 id = ansi_opname (GE_EXPR);
7441 break;
7443 case CPP_AND_AND:
7444 id = ansi_opname (TRUTH_ANDIF_EXPR);
7445 break;
7447 case CPP_OR_OR:
7448 id = ansi_opname (TRUTH_ORIF_EXPR);
7449 break;
7451 case CPP_PLUS_PLUS:
7452 id = ansi_opname (POSTINCREMENT_EXPR);
7453 break;
7455 case CPP_MINUS_MINUS:
7456 id = ansi_opname (PREDECREMENT_EXPR);
7457 break;
7459 case CPP_COMMA:
7460 id = ansi_opname (COMPOUND_EXPR);
7461 break;
7463 case CPP_DEREF_STAR:
7464 id = ansi_opname (MEMBER_REF);
7465 break;
7467 case CPP_DEREF:
7468 id = ansi_opname (COMPONENT_REF);
7469 break;
7471 case CPP_OPEN_PAREN:
7472 /* Consume the `('. */
7473 cp_lexer_consume_token (parser->lexer);
7474 /* Look for the matching `)'. */
7475 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7476 return ansi_opname (CALL_EXPR);
7478 case CPP_OPEN_SQUARE:
7479 /* Consume the `['. */
7480 cp_lexer_consume_token (parser->lexer);
7481 /* Look for the matching `]'. */
7482 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7483 return ansi_opname (ARRAY_REF);
7485 /* Extensions. */
7486 case CPP_MIN:
7487 id = ansi_opname (MIN_EXPR);
7488 break;
7490 case CPP_MAX:
7491 id = ansi_opname (MAX_EXPR);
7492 break;
7494 case CPP_MIN_EQ:
7495 id = ansi_assopname (MIN_EXPR);
7496 break;
7498 case CPP_MAX_EQ:
7499 id = ansi_assopname (MAX_EXPR);
7500 break;
7502 default:
7503 /* Anything else is an error. */
7504 break;
7507 /* If we have selected an identifier, we need to consume the
7508 operator token. */
7509 if (id)
7510 cp_lexer_consume_token (parser->lexer);
7511 /* Otherwise, no valid operator name was present. */
7512 else
7514 cp_parser_error (parser, "expected operator");
7515 id = error_mark_node;
7518 return id;
7521 /* Parse a template-declaration.
7523 template-declaration:
7524 export [opt] template < template-parameter-list > declaration
7526 If MEMBER_P is TRUE, this template-declaration occurs within a
7527 class-specifier.
7529 The grammar rule given by the standard isn't correct. What
7530 is really meant is:
7532 template-declaration:
7533 export [opt] template-parameter-list-seq
7534 decl-specifier-seq [opt] init-declarator [opt] ;
7535 export [opt] template-parameter-list-seq
7536 function-definition
7538 template-parameter-list-seq:
7539 template-parameter-list-seq [opt]
7540 template < template-parameter-list > */
7542 static void
7543 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7545 /* Check for `export'. */
7546 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7548 /* Consume the `export' token. */
7549 cp_lexer_consume_token (parser->lexer);
7550 /* Warn that we do not support `export'. */
7551 warning ("keyword `export' not implemented, and will be ignored");
7554 cp_parser_template_declaration_after_export (parser, member_p);
7557 /* Parse a template-parameter-list.
7559 template-parameter-list:
7560 template-parameter
7561 template-parameter-list , template-parameter
7563 Returns a TREE_LIST. Each node represents a template parameter.
7564 The nodes are connected via their TREE_CHAINs. */
7566 static tree
7567 cp_parser_template_parameter_list (cp_parser* parser)
7569 tree parameter_list = NULL_TREE;
7571 while (true)
7573 tree parameter;
7574 cp_token *token;
7576 /* Parse the template-parameter. */
7577 parameter = cp_parser_template_parameter (parser);
7578 /* Add it to the list. */
7579 parameter_list = process_template_parm (parameter_list,
7580 parameter);
7582 /* Peek at the next token. */
7583 token = cp_lexer_peek_token (parser->lexer);
7584 /* If it's not a `,', we're done. */
7585 if (token->type != CPP_COMMA)
7586 break;
7587 /* Otherwise, consume the `,' token. */
7588 cp_lexer_consume_token (parser->lexer);
7591 return parameter_list;
7594 /* Parse a template-parameter.
7596 template-parameter:
7597 type-parameter
7598 parameter-declaration
7600 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7601 TREE_PURPOSE is the default value, if any. */
7603 static tree
7604 cp_parser_template_parameter (cp_parser* parser)
7606 cp_token *token;
7608 /* Peek at the next token. */
7609 token = cp_lexer_peek_token (parser->lexer);
7610 /* If it is `class' or `template', we have a type-parameter. */
7611 if (token->keyword == RID_TEMPLATE)
7612 return cp_parser_type_parameter (parser);
7613 /* If it is `class' or `typename' we do not know yet whether it is a
7614 type parameter or a non-type parameter. Consider:
7616 template <typename T, typename T::X X> ...
7620 template <class C, class D*> ...
7622 Here, the first parameter is a type parameter, and the second is
7623 a non-type parameter. We can tell by looking at the token after
7624 the identifier -- if it is a `,', `=', or `>' then we have a type
7625 parameter. */
7626 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7628 /* Peek at the token after `class' or `typename'. */
7629 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7630 /* If it's an identifier, skip it. */
7631 if (token->type == CPP_NAME)
7632 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7633 /* Now, see if the token looks like the end of a template
7634 parameter. */
7635 if (token->type == CPP_COMMA
7636 || token->type == CPP_EQ
7637 || token->type == CPP_GREATER)
7638 return cp_parser_type_parameter (parser);
7641 /* Otherwise, it is a non-type parameter.
7643 [temp.param]
7645 When parsing a default template-argument for a non-type
7646 template-parameter, the first non-nested `>' is taken as the end
7647 of the template parameter-list rather than a greater-than
7648 operator. */
7649 return
7650 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7651 /*parenthesized_p=*/NULL);
7654 /* Parse a type-parameter.
7656 type-parameter:
7657 class identifier [opt]
7658 class identifier [opt] = type-id
7659 typename identifier [opt]
7660 typename identifier [opt] = type-id
7661 template < template-parameter-list > class identifier [opt]
7662 template < template-parameter-list > class identifier [opt]
7663 = id-expression
7665 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7666 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7667 the declaration of the parameter. */
7669 static tree
7670 cp_parser_type_parameter (cp_parser* parser)
7672 cp_token *token;
7673 tree parameter;
7675 /* Look for a keyword to tell us what kind of parameter this is. */
7676 token = cp_parser_require (parser, CPP_KEYWORD,
7677 "`class', `typename', or `template'");
7678 if (!token)
7679 return error_mark_node;
7681 switch (token->keyword)
7683 case RID_CLASS:
7684 case RID_TYPENAME:
7686 tree identifier;
7687 tree default_argument;
7689 /* If the next token is an identifier, then it names the
7690 parameter. */
7691 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7692 identifier = cp_parser_identifier (parser);
7693 else
7694 identifier = NULL_TREE;
7696 /* Create the parameter. */
7697 parameter = finish_template_type_parm (class_type_node, identifier);
7699 /* If the next token is an `=', we have a default argument. */
7700 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7702 /* Consume the `=' token. */
7703 cp_lexer_consume_token (parser->lexer);
7704 /* Parse the default-argument. */
7705 default_argument = cp_parser_type_id (parser);
7707 else
7708 default_argument = NULL_TREE;
7710 /* Create the combined representation of the parameter and the
7711 default argument. */
7712 parameter = build_tree_list (default_argument, parameter);
7714 break;
7716 case RID_TEMPLATE:
7718 tree parameter_list;
7719 tree identifier;
7720 tree default_argument;
7722 /* Look for the `<'. */
7723 cp_parser_require (parser, CPP_LESS, "`<'");
7724 /* Parse the template-parameter-list. */
7725 begin_template_parm_list ();
7726 parameter_list
7727 = cp_parser_template_parameter_list (parser);
7728 parameter_list = end_template_parm_list (parameter_list);
7729 /* Look for the `>'. */
7730 cp_parser_require (parser, CPP_GREATER, "`>'");
7731 /* Look for the `class' keyword. */
7732 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7733 /* If the next token is an `=', then there is a
7734 default-argument. If the next token is a `>', we are at
7735 the end of the parameter-list. If the next token is a `,',
7736 then we are at the end of this parameter. */
7737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7738 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7739 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7740 identifier = cp_parser_identifier (parser);
7741 else
7742 identifier = NULL_TREE;
7743 /* Create the template parameter. */
7744 parameter = finish_template_template_parm (class_type_node,
7745 identifier);
7747 /* If the next token is an `=', then there is a
7748 default-argument. */
7749 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7751 bool is_template;
7753 /* Consume the `='. */
7754 cp_lexer_consume_token (parser->lexer);
7755 /* Parse the id-expression. */
7756 default_argument
7757 = cp_parser_id_expression (parser,
7758 /*template_keyword_p=*/false,
7759 /*check_dependency_p=*/true,
7760 /*template_p=*/&is_template,
7761 /*declarator_p=*/false);
7762 if (TREE_CODE (default_argument) == TYPE_DECL)
7763 /* If the id-expression was a template-id that refers to
7764 a template-class, we already have the declaration here,
7765 so no further lookup is needed. */
7767 else
7768 /* Look up the name. */
7769 default_argument
7770 = cp_parser_lookup_name (parser, default_argument,
7771 /*is_type=*/false,
7772 /*is_template=*/is_template,
7773 /*is_namespace=*/false,
7774 /*check_dependency=*/true);
7775 /* See if the default argument is valid. */
7776 default_argument
7777 = check_template_template_default_arg (default_argument);
7779 else
7780 default_argument = NULL_TREE;
7782 /* Create the combined representation of the parameter and the
7783 default argument. */
7784 parameter = build_tree_list (default_argument, parameter);
7786 break;
7788 default:
7789 /* Anything else is an error. */
7790 cp_parser_error (parser,
7791 "expected `class', `typename', or `template'");
7792 parameter = error_mark_node;
7795 return parameter;
7798 /* Parse a template-id.
7800 template-id:
7801 template-name < template-argument-list [opt] >
7803 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7804 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7805 returned. Otherwise, if the template-name names a function, or set
7806 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7807 names a class, returns a TYPE_DECL for the specialization.
7809 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7810 uninstantiated templates. */
7812 static tree
7813 cp_parser_template_id (cp_parser *parser,
7814 bool template_keyword_p,
7815 bool check_dependency_p,
7816 bool is_declaration)
7818 tree template;
7819 tree arguments;
7820 tree template_id;
7821 ptrdiff_t start_of_id;
7822 tree access_check = NULL_TREE;
7823 cp_token *next_token, *next_token_2;
7824 bool is_identifier;
7826 /* If the next token corresponds to a template-id, there is no need
7827 to reparse it. */
7828 next_token = cp_lexer_peek_token (parser->lexer);
7829 if (next_token->type == CPP_TEMPLATE_ID)
7831 tree value;
7832 tree check;
7834 /* Get the stored value. */
7835 value = cp_lexer_consume_token (parser->lexer)->value;
7836 /* Perform any access checks that were deferred. */
7837 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7838 perform_or_defer_access_check (TREE_PURPOSE (check),
7839 TREE_VALUE (check));
7840 /* Return the stored value. */
7841 return TREE_VALUE (value);
7844 /* Avoid performing name lookup if there is no possibility of
7845 finding a template-id. */
7846 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7847 || (next_token->type == CPP_NAME
7848 && !cp_parser_nth_token_starts_template_argument_list_p
7849 (parser, 2)))
7851 cp_parser_error (parser, "expected template-id");
7852 return error_mark_node;
7855 /* Remember where the template-id starts. */
7856 if (cp_parser_parsing_tentatively (parser)
7857 && !cp_parser_committed_to_tentative_parse (parser))
7859 next_token = cp_lexer_peek_token (parser->lexer);
7860 start_of_id = cp_lexer_token_difference (parser->lexer,
7861 parser->lexer->first_token,
7862 next_token);
7864 else
7865 start_of_id = -1;
7867 push_deferring_access_checks (dk_deferred);
7869 /* Parse the template-name. */
7870 is_identifier = false;
7871 template = cp_parser_template_name (parser, template_keyword_p,
7872 check_dependency_p,
7873 is_declaration,
7874 &is_identifier);
7875 if (template == error_mark_node || is_identifier)
7877 pop_deferring_access_checks ();
7878 return template;
7881 /* If we find the sequence `[:' after a template-name, it's probably
7882 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7883 parse correctly the argument list. */
7884 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7885 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7886 if (next_token->type == CPP_OPEN_SQUARE
7887 && next_token->flags & DIGRAPH
7888 && next_token_2->type == CPP_COLON
7889 && !(next_token_2->flags & PREV_WHITE))
7891 cp_parser_parse_tentatively (parser);
7892 /* Change `:' into `::'. */
7893 next_token_2->type = CPP_SCOPE;
7894 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7895 CPP_LESS. */
7896 cp_lexer_consume_token (parser->lexer);
7897 /* Parse the arguments. */
7898 arguments = cp_parser_enclosed_template_argument_list (parser);
7899 if (!cp_parser_parse_definitely (parser))
7901 /* If we couldn't parse an argument list, then we revert our changes
7902 and return simply an error. Maybe this is not a template-id
7903 after all. */
7904 next_token_2->type = CPP_COLON;
7905 cp_parser_error (parser, "expected `<'");
7906 pop_deferring_access_checks ();
7907 return error_mark_node;
7909 /* Otherwise, emit an error about the invalid digraph, but continue
7910 parsing because we got our argument list. */
7911 pedwarn ("`<::' cannot begin a template-argument list");
7912 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7913 "between `<' and `::'");
7914 if (!flag_permissive)
7916 static bool hint;
7917 if (!hint)
7919 inform ("(if you use `-fpermissive' G++ will accept your code)");
7920 hint = true;
7924 else
7926 /* Look for the `<' that starts the template-argument-list. */
7927 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7929 pop_deferring_access_checks ();
7930 return error_mark_node;
7932 /* Parse the arguments. */
7933 arguments = cp_parser_enclosed_template_argument_list (parser);
7936 /* Build a representation of the specialization. */
7937 if (TREE_CODE (template) == IDENTIFIER_NODE)
7938 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7939 else if (DECL_CLASS_TEMPLATE_P (template)
7940 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7941 template_id
7942 = finish_template_type (template, arguments,
7943 cp_lexer_next_token_is (parser->lexer,
7944 CPP_SCOPE));
7945 else
7947 /* If it's not a class-template or a template-template, it should be
7948 a function-template. */
7949 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7950 || TREE_CODE (template) == OVERLOAD
7951 || BASELINK_P (template)),
7952 20010716);
7954 template_id = lookup_template_function (template, arguments);
7957 /* Retrieve any deferred checks. Do not pop this access checks yet
7958 so the memory will not be reclaimed during token replacing below. */
7959 access_check = get_deferred_access_checks ();
7961 /* If parsing tentatively, replace the sequence of tokens that makes
7962 up the template-id with a CPP_TEMPLATE_ID token. That way,
7963 should we re-parse the token stream, we will not have to repeat
7964 the effort required to do the parse, nor will we issue duplicate
7965 error messages about problems during instantiation of the
7966 template. */
7967 if (start_of_id >= 0)
7969 cp_token *token;
7971 /* Find the token that corresponds to the start of the
7972 template-id. */
7973 token = cp_lexer_advance_token (parser->lexer,
7974 parser->lexer->first_token,
7975 start_of_id);
7977 /* Reset the contents of the START_OF_ID token. */
7978 token->type = CPP_TEMPLATE_ID;
7979 token->value = build_tree_list (access_check, template_id);
7980 token->keyword = RID_MAX;
7981 /* Purge all subsequent tokens. */
7982 cp_lexer_purge_tokens_after (parser->lexer, token);
7985 pop_deferring_access_checks ();
7986 return template_id;
7989 /* Parse a template-name.
7991 template-name:
7992 identifier
7994 The standard should actually say:
7996 template-name:
7997 identifier
7998 operator-function-id
8000 A defect report has been filed about this issue.
8002 A conversion-function-id cannot be a template name because they cannot
8003 be part of a template-id. In fact, looking at this code:
8005 a.operator K<int>()
8007 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8008 It is impossible to call a templated conversion-function-id with an
8009 explicit argument list, since the only allowed template parameter is
8010 the type to which it is converting.
8012 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8013 `template' keyword, in a construction like:
8015 T::template f<3>()
8017 In that case `f' is taken to be a template-name, even though there
8018 is no way of knowing for sure.
8020 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8021 name refers to a set of overloaded functions, at least one of which
8022 is a template, or an IDENTIFIER_NODE with the name of the template,
8023 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8024 names are looked up inside uninstantiated templates. */
8026 static tree
8027 cp_parser_template_name (cp_parser* parser,
8028 bool template_keyword_p,
8029 bool check_dependency_p,
8030 bool is_declaration,
8031 bool *is_identifier)
8033 tree identifier;
8034 tree decl;
8035 tree fns;
8037 /* If the next token is `operator', then we have either an
8038 operator-function-id or a conversion-function-id. */
8039 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8041 /* We don't know whether we're looking at an
8042 operator-function-id or a conversion-function-id. */
8043 cp_parser_parse_tentatively (parser);
8044 /* Try an operator-function-id. */
8045 identifier = cp_parser_operator_function_id (parser);
8046 /* If that didn't work, try a conversion-function-id. */
8047 if (!cp_parser_parse_definitely (parser))
8049 cp_parser_error (parser, "expected template-name");
8050 return error_mark_node;
8053 /* Look for the identifier. */
8054 else
8055 identifier = cp_parser_identifier (parser);
8057 /* If we didn't find an identifier, we don't have a template-id. */
8058 if (identifier == error_mark_node)
8059 return error_mark_node;
8061 /* If the name immediately followed the `template' keyword, then it
8062 is a template-name. However, if the next token is not `<', then
8063 we do not treat it as a template-name, since it is not being used
8064 as part of a template-id. This enables us to handle constructs
8065 like:
8067 template <typename T> struct S { S(); };
8068 template <typename T> S<T>::S();
8070 correctly. We would treat `S' as a template -- if it were `S<T>'
8071 -- but we do not if there is no `<'. */
8073 if (processing_template_decl
8074 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8076 /* In a declaration, in a dependent context, we pretend that the
8077 "template" keyword was present in order to improve error
8078 recovery. For example, given:
8080 template <typename T> void f(T::X<int>);
8082 we want to treat "X<int>" as a template-id. */
8083 if (is_declaration
8084 && !template_keyword_p
8085 && parser->scope && TYPE_P (parser->scope)
8086 && dependent_type_p (parser->scope))
8088 ptrdiff_t start;
8089 cp_token* token;
8090 /* Explain what went wrong. */
8091 error ("non-template `%D' used as template", identifier);
8092 error ("(use `%T::template %D' to indicate that it is a template)",
8093 parser->scope, identifier);
8094 /* If parsing tentatively, find the location of the "<"
8095 token. */
8096 if (cp_parser_parsing_tentatively (parser)
8097 && !cp_parser_committed_to_tentative_parse (parser))
8099 cp_parser_simulate_error (parser);
8100 token = cp_lexer_peek_token (parser->lexer);
8101 token = cp_lexer_prev_token (parser->lexer, token);
8102 start = cp_lexer_token_difference (parser->lexer,
8103 parser->lexer->first_token,
8104 token);
8106 else
8107 start = -1;
8108 /* Parse the template arguments so that we can issue error
8109 messages about them. */
8110 cp_lexer_consume_token (parser->lexer);
8111 cp_parser_enclosed_template_argument_list (parser);
8112 /* Skip tokens until we find a good place from which to
8113 continue parsing. */
8114 cp_parser_skip_to_closing_parenthesis (parser,
8115 /*recovering=*/true,
8116 /*or_comma=*/true,
8117 /*consume_paren=*/false);
8118 /* If parsing tentatively, permanently remove the
8119 template argument list. That will prevent duplicate
8120 error messages from being issued about the missing
8121 "template" keyword. */
8122 if (start >= 0)
8124 token = cp_lexer_advance_token (parser->lexer,
8125 parser->lexer->first_token,
8126 start);
8127 cp_lexer_purge_tokens_after (parser->lexer, token);
8129 if (is_identifier)
8130 *is_identifier = true;
8131 return identifier;
8134 /* If the "template" keyword is present, then there is generally
8135 no point in doing name-lookup, so we just return IDENTIFIER.
8136 But, if the qualifying scope is non-dependent then we can
8137 (and must) do name-lookup normally. */
8138 if (template_keyword_p
8139 && (!parser->scope
8140 || (TYPE_P (parser->scope)
8141 && dependent_type_p (parser->scope))))
8142 return identifier;
8145 /* Look up the name. */
8146 decl = cp_parser_lookup_name (parser, identifier,
8147 /*is_type=*/false,
8148 /*is_template=*/false,
8149 /*is_namespace=*/false,
8150 check_dependency_p);
8151 decl = maybe_get_template_decl_from_type_decl (decl);
8153 /* If DECL is a template, then the name was a template-name. */
8154 if (TREE_CODE (decl) == TEMPLATE_DECL)
8156 else
8158 /* The standard does not explicitly indicate whether a name that
8159 names a set of overloaded declarations, some of which are
8160 templates, is a template-name. However, such a name should
8161 be a template-name; otherwise, there is no way to form a
8162 template-id for the overloaded templates. */
8163 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8164 if (TREE_CODE (fns) == OVERLOAD)
8166 tree fn;
8168 for (fn = fns; fn; fn = OVL_NEXT (fn))
8169 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8170 break;
8172 else
8174 /* Otherwise, the name does not name a template. */
8175 cp_parser_error (parser, "expected template-name");
8176 return error_mark_node;
8180 /* If DECL is dependent, and refers to a function, then just return
8181 its name; we will look it up again during template instantiation. */
8182 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8184 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8185 if (TYPE_P (scope) && dependent_type_p (scope))
8186 return identifier;
8189 return decl;
8192 /* Parse a template-argument-list.
8194 template-argument-list:
8195 template-argument
8196 template-argument-list , template-argument
8198 Returns a TREE_VEC containing the arguments. */
8200 static tree
8201 cp_parser_template_argument_list (cp_parser* parser)
8203 tree fixed_args[10];
8204 unsigned n_args = 0;
8205 unsigned alloced = 10;
8206 tree *arg_ary = fixed_args;
8207 tree vec;
8208 bool saved_in_template_argument_list_p;
8210 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8211 parser->in_template_argument_list_p = true;
8214 tree argument;
8216 if (n_args)
8217 /* Consume the comma. */
8218 cp_lexer_consume_token (parser->lexer);
8220 /* Parse the template-argument. */
8221 argument = cp_parser_template_argument (parser);
8222 if (n_args == alloced)
8224 alloced *= 2;
8226 if (arg_ary == fixed_args)
8228 arg_ary = xmalloc (sizeof (tree) * alloced);
8229 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8231 else
8232 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8234 arg_ary[n_args++] = argument;
8236 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8238 vec = make_tree_vec (n_args);
8240 while (n_args--)
8241 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8243 if (arg_ary != fixed_args)
8244 free (arg_ary);
8245 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8246 return vec;
8249 /* Parse a template-argument.
8251 template-argument:
8252 assignment-expression
8253 type-id
8254 id-expression
8256 The representation is that of an assignment-expression, type-id, or
8257 id-expression -- except that the qualified id-expression is
8258 evaluated, so that the value returned is either a DECL or an
8259 OVERLOAD.
8261 Although the standard says "assignment-expression", it forbids
8262 throw-expressions or assignments in the template argument.
8263 Therefore, we use "conditional-expression" instead. */
8265 static tree
8266 cp_parser_template_argument (cp_parser* parser)
8268 tree argument;
8269 bool template_p;
8270 bool address_p;
8271 bool maybe_type_id = false;
8272 cp_token *token;
8273 cp_id_kind idk;
8274 tree qualifying_class;
8276 /* There's really no way to know what we're looking at, so we just
8277 try each alternative in order.
8279 [temp.arg]
8281 In a template-argument, an ambiguity between a type-id and an
8282 expression is resolved to a type-id, regardless of the form of
8283 the corresponding template-parameter.
8285 Therefore, we try a type-id first. */
8286 cp_parser_parse_tentatively (parser);
8287 argument = cp_parser_type_id (parser);
8288 /* If there was no error parsing the type-id but the next token is a '>>',
8289 we probably found a typo for '> >'. But there are type-id which are
8290 also valid expressions. For instance:
8292 struct X { int operator >> (int); };
8293 template <int V> struct Foo {};
8294 Foo<X () >> 5> r;
8296 Here 'X()' is a valid type-id of a function type, but the user just
8297 wanted to write the expression "X() >> 5". Thus, we remember that we
8298 found a valid type-id, but we still try to parse the argument as an
8299 expression to see what happens. */
8300 if (!cp_parser_error_occurred (parser)
8301 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8303 maybe_type_id = true;
8304 cp_parser_abort_tentative_parse (parser);
8306 else
8308 /* If the next token isn't a `,' or a `>', then this argument wasn't
8309 really finished. This means that the argument is not a valid
8310 type-id. */
8311 if (!cp_parser_next_token_ends_template_argument_p (parser))
8312 cp_parser_error (parser, "expected template-argument");
8313 /* If that worked, we're done. */
8314 if (cp_parser_parse_definitely (parser))
8315 return argument;
8317 /* We're still not sure what the argument will be. */
8318 cp_parser_parse_tentatively (parser);
8319 /* Try a template. */
8320 argument = cp_parser_id_expression (parser,
8321 /*template_keyword_p=*/false,
8322 /*check_dependency_p=*/true,
8323 &template_p,
8324 /*declarator_p=*/false);
8325 /* If the next token isn't a `,' or a `>', then this argument wasn't
8326 really finished. */
8327 if (!cp_parser_next_token_ends_template_argument_p (parser))
8328 cp_parser_error (parser, "expected template-argument");
8329 if (!cp_parser_error_occurred (parser))
8331 /* Figure out what is being referred to. If the id-expression
8332 was for a class template specialization, then we will have a
8333 TYPE_DECL at this point. There is no need to do name lookup
8334 at this point in that case. */
8335 if (TREE_CODE (argument) != TYPE_DECL)
8336 argument = cp_parser_lookup_name (parser, argument,
8337 /*is_type=*/false,
8338 /*is_template=*/template_p,
8339 /*is_namespace=*/false,
8340 /*check_dependency=*/true);
8341 if (TREE_CODE (argument) != TEMPLATE_DECL
8342 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8343 cp_parser_error (parser, "expected template-name");
8345 if (cp_parser_parse_definitely (parser))
8346 return argument;
8347 /* It must be a non-type argument. There permitted cases are given
8348 in [temp.arg.nontype]:
8350 -- an integral constant-expression of integral or enumeration
8351 type; or
8353 -- the name of a non-type template-parameter; or
8355 -- the name of an object or function with external linkage...
8357 -- the address of an object or function with external linkage...
8359 -- a pointer to member... */
8360 /* Look for a non-type template parameter. */
8361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8363 cp_parser_parse_tentatively (parser);
8364 argument = cp_parser_primary_expression (parser,
8365 &idk,
8366 &qualifying_class);
8367 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8368 || !cp_parser_next_token_ends_template_argument_p (parser))
8369 cp_parser_simulate_error (parser);
8370 if (cp_parser_parse_definitely (parser))
8371 return argument;
8373 /* If the next token is "&", the argument must be the address of an
8374 object or function with external linkage. */
8375 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8376 if (address_p)
8377 cp_lexer_consume_token (parser->lexer);
8378 /* See if we might have an id-expression. */
8379 token = cp_lexer_peek_token (parser->lexer);
8380 if (token->type == CPP_NAME
8381 || token->keyword == RID_OPERATOR
8382 || token->type == CPP_SCOPE
8383 || token->type == CPP_TEMPLATE_ID
8384 || token->type == CPP_NESTED_NAME_SPECIFIER)
8386 cp_parser_parse_tentatively (parser);
8387 argument = cp_parser_primary_expression (parser,
8388 &idk,
8389 &qualifying_class);
8390 if (cp_parser_error_occurred (parser)
8391 || !cp_parser_next_token_ends_template_argument_p (parser))
8392 cp_parser_abort_tentative_parse (parser);
8393 else
8395 if (qualifying_class)
8396 argument = finish_qualified_id_expr (qualifying_class,
8397 argument,
8398 /*done=*/true,
8399 address_p);
8400 if (TREE_CODE (argument) == VAR_DECL)
8402 /* A variable without external linkage might still be a
8403 valid constant-expression, so no error is issued here
8404 if the external-linkage check fails. */
8405 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8406 cp_parser_simulate_error (parser);
8408 else if (is_overloaded_fn (argument))
8409 /* All overloaded functions are allowed; if the external
8410 linkage test does not pass, an error will be issued
8411 later. */
8413 else if (address_p
8414 && (TREE_CODE (argument) == OFFSET_REF
8415 || TREE_CODE (argument) == SCOPE_REF))
8416 /* A pointer-to-member. */
8418 else
8419 cp_parser_simulate_error (parser);
8421 if (cp_parser_parse_definitely (parser))
8423 if (address_p)
8424 argument = build_x_unary_op (ADDR_EXPR, argument);
8425 return argument;
8429 /* If the argument started with "&", there are no other valid
8430 alternatives at this point. */
8431 if (address_p)
8433 cp_parser_error (parser, "invalid non-type template argument");
8434 return error_mark_node;
8436 /* If the argument wasn't successfully parsed as a type-id followed
8437 by '>>', the argument can only be a constant expression now.
8438 Otherwise, we try parsing the constant-expression tentatively,
8439 because the argument could really be a type-id. */
8440 if (maybe_type_id)
8441 cp_parser_parse_tentatively (parser);
8442 argument = cp_parser_constant_expression (parser,
8443 /*allow_non_constant_p=*/false,
8444 /*non_constant_p=*/NULL);
8445 argument = fold_non_dependent_expr (argument);
8446 if (!maybe_type_id)
8447 return argument;
8448 if (!cp_parser_next_token_ends_template_argument_p (parser))
8449 cp_parser_error (parser, "expected template-argument");
8450 if (cp_parser_parse_definitely (parser))
8451 return argument;
8452 /* We did our best to parse the argument as a non type-id, but that
8453 was the only alternative that matched (albeit with a '>' after
8454 it). We can assume it's just a typo from the user, and a
8455 diagnostic will then be issued. */
8456 return cp_parser_type_id (parser);
8459 /* Parse an explicit-instantiation.
8461 explicit-instantiation:
8462 template declaration
8464 Although the standard says `declaration', what it really means is:
8466 explicit-instantiation:
8467 template decl-specifier-seq [opt] declarator [opt] ;
8469 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8470 supposed to be allowed. A defect report has been filed about this
8471 issue.
8473 GNU Extension:
8475 explicit-instantiation:
8476 storage-class-specifier template
8477 decl-specifier-seq [opt] declarator [opt] ;
8478 function-specifier template
8479 decl-specifier-seq [opt] declarator [opt] ; */
8481 static void
8482 cp_parser_explicit_instantiation (cp_parser* parser)
8484 int declares_class_or_enum;
8485 tree decl_specifiers;
8486 tree attributes;
8487 tree extension_specifier = NULL_TREE;
8489 /* Look for an (optional) storage-class-specifier or
8490 function-specifier. */
8491 if (cp_parser_allow_gnu_extensions_p (parser))
8493 extension_specifier
8494 = cp_parser_storage_class_specifier_opt (parser);
8495 if (!extension_specifier)
8496 extension_specifier = cp_parser_function_specifier_opt (parser);
8499 /* Look for the `template' keyword. */
8500 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8501 /* Let the front end know that we are processing an explicit
8502 instantiation. */
8503 begin_explicit_instantiation ();
8504 /* [temp.explicit] says that we are supposed to ignore access
8505 control while processing explicit instantiation directives. */
8506 push_deferring_access_checks (dk_no_check);
8507 /* Parse a decl-specifier-seq. */
8508 decl_specifiers
8509 = cp_parser_decl_specifier_seq (parser,
8510 CP_PARSER_FLAGS_OPTIONAL,
8511 &attributes,
8512 &declares_class_or_enum);
8513 /* If there was exactly one decl-specifier, and it declared a class,
8514 and there's no declarator, then we have an explicit type
8515 instantiation. */
8516 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8518 tree type;
8520 type = check_tag_decl (decl_specifiers);
8521 /* Turn access control back on for names used during
8522 template instantiation. */
8523 pop_deferring_access_checks ();
8524 if (type)
8525 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8527 else
8529 tree declarator;
8530 tree decl;
8532 /* Parse the declarator. */
8533 declarator
8534 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8535 /*ctor_dtor_or_conv_p=*/NULL,
8536 /*parenthesized_p=*/NULL);
8537 cp_parser_check_for_definition_in_return_type (declarator,
8538 declares_class_or_enum);
8539 if (declarator != error_mark_node)
8541 decl = grokdeclarator (declarator, decl_specifiers,
8542 NORMAL, 0, NULL);
8543 /* Turn access control back on for names used during
8544 template instantiation. */
8545 pop_deferring_access_checks ();
8546 /* Do the explicit instantiation. */
8547 do_decl_instantiation (decl, extension_specifier);
8549 else
8551 pop_deferring_access_checks ();
8552 /* Skip the body of the explicit instantiation. */
8553 cp_parser_skip_to_end_of_statement (parser);
8556 /* We're done with the instantiation. */
8557 end_explicit_instantiation ();
8559 cp_parser_consume_semicolon_at_end_of_statement (parser);
8562 /* Parse an explicit-specialization.
8564 explicit-specialization:
8565 template < > declaration
8567 Although the standard says `declaration', what it really means is:
8569 explicit-specialization:
8570 template <> decl-specifier [opt] init-declarator [opt] ;
8571 template <> function-definition
8572 template <> explicit-specialization
8573 template <> template-declaration */
8575 static void
8576 cp_parser_explicit_specialization (cp_parser* parser)
8578 /* Look for the `template' keyword. */
8579 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8580 /* Look for the `<'. */
8581 cp_parser_require (parser, CPP_LESS, "`<'");
8582 /* Look for the `>'. */
8583 cp_parser_require (parser, CPP_GREATER, "`>'");
8584 /* We have processed another parameter list. */
8585 ++parser->num_template_parameter_lists;
8586 /* Let the front end know that we are beginning a specialization. */
8587 begin_specialization ();
8589 /* If the next keyword is `template', we need to figure out whether
8590 or not we're looking a template-declaration. */
8591 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8593 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8594 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8595 cp_parser_template_declaration_after_export (parser,
8596 /*member_p=*/false);
8597 else
8598 cp_parser_explicit_specialization (parser);
8600 else
8601 /* Parse the dependent declaration. */
8602 cp_parser_single_declaration (parser,
8603 /*member_p=*/false,
8604 /*friend_p=*/NULL);
8606 /* We're done with the specialization. */
8607 end_specialization ();
8608 /* We're done with this parameter list. */
8609 --parser->num_template_parameter_lists;
8612 /* Parse a type-specifier.
8614 type-specifier:
8615 simple-type-specifier
8616 class-specifier
8617 enum-specifier
8618 elaborated-type-specifier
8619 cv-qualifier
8621 GNU Extension:
8623 type-specifier:
8624 __complex__
8626 Returns a representation of the type-specifier. If the
8627 type-specifier is a keyword (like `int' or `const', or
8628 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8629 For a class-specifier, enum-specifier, or elaborated-type-specifier
8630 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8632 If IS_FRIEND is TRUE then this type-specifier is being declared a
8633 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8634 appearing in a decl-specifier-seq.
8636 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8637 class-specifier, enum-specifier, or elaborated-type-specifier, then
8638 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8639 if a type is declared; 2 if it is defined. Otherwise, it is set to
8640 zero.
8642 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8643 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8644 is set to FALSE. */
8646 static tree
8647 cp_parser_type_specifier (cp_parser* parser,
8648 cp_parser_flags flags,
8649 bool is_friend,
8650 bool is_declaration,
8651 int* declares_class_or_enum,
8652 bool* is_cv_qualifier)
8654 tree type_spec = NULL_TREE;
8655 cp_token *token;
8656 enum rid keyword;
8658 /* Assume this type-specifier does not declare a new type. */
8659 if (declares_class_or_enum)
8660 *declares_class_or_enum = 0;
8661 /* And that it does not specify a cv-qualifier. */
8662 if (is_cv_qualifier)
8663 *is_cv_qualifier = false;
8664 /* Peek at the next token. */
8665 token = cp_lexer_peek_token (parser->lexer);
8667 /* If we're looking at a keyword, we can use that to guide the
8668 production we choose. */
8669 keyword = token->keyword;
8670 switch (keyword)
8672 /* Any of these indicate either a class-specifier, or an
8673 elaborated-type-specifier. */
8674 case RID_CLASS:
8675 case RID_STRUCT:
8676 case RID_UNION:
8677 case RID_ENUM:
8678 /* Parse tentatively so that we can back up if we don't find a
8679 class-specifier or enum-specifier. */
8680 cp_parser_parse_tentatively (parser);
8681 /* Look for the class-specifier or enum-specifier. */
8682 if (keyword == RID_ENUM)
8683 type_spec = cp_parser_enum_specifier (parser);
8684 else
8685 type_spec = cp_parser_class_specifier (parser);
8687 /* If that worked, we're done. */
8688 if (cp_parser_parse_definitely (parser))
8690 if (declares_class_or_enum)
8691 *declares_class_or_enum = 2;
8692 return type_spec;
8695 /* Fall through. */
8697 case RID_TYPENAME:
8698 /* Look for an elaborated-type-specifier. */
8699 type_spec = cp_parser_elaborated_type_specifier (parser,
8700 is_friend,
8701 is_declaration);
8702 /* We're declaring a class or enum -- unless we're using
8703 `typename'. */
8704 if (declares_class_or_enum && keyword != RID_TYPENAME)
8705 *declares_class_or_enum = 1;
8706 return type_spec;
8708 case RID_CONST:
8709 case RID_VOLATILE:
8710 case RID_RESTRICT:
8711 type_spec = cp_parser_cv_qualifier_opt (parser);
8712 /* Even though we call a routine that looks for an optional
8713 qualifier, we know that there should be one. */
8714 my_friendly_assert (type_spec != NULL, 20000328);
8715 /* This type-specifier was a cv-qualified. */
8716 if (is_cv_qualifier)
8717 *is_cv_qualifier = true;
8719 return type_spec;
8721 case RID_COMPLEX:
8722 /* The `__complex__' keyword is a GNU extension. */
8723 return cp_lexer_consume_token (parser->lexer)->value;
8725 default:
8726 break;
8729 /* If we do not already have a type-specifier, assume we are looking
8730 at a simple-type-specifier. */
8731 type_spec = cp_parser_simple_type_specifier (parser, flags,
8732 /*identifier_p=*/true);
8734 /* If we didn't find a type-specifier, and a type-specifier was not
8735 optional in this context, issue an error message. */
8736 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8738 cp_parser_error (parser, "expected type specifier");
8739 return error_mark_node;
8742 return type_spec;
8745 /* Parse a simple-type-specifier.
8747 simple-type-specifier:
8748 :: [opt] nested-name-specifier [opt] type-name
8749 :: [opt] nested-name-specifier template template-id
8750 char
8751 wchar_t
8752 bool
8753 short
8755 long
8756 signed
8757 unsigned
8758 float
8759 double
8760 void
8762 GNU Extension:
8764 simple-type-specifier:
8765 __typeof__ unary-expression
8766 __typeof__ ( type-id )
8768 For the various keywords, the value returned is simply the
8769 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8770 For the first two productions, and if IDENTIFIER_P is false, the
8771 value returned is the indicated TYPE_DECL. */
8773 static tree
8774 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8775 bool identifier_p)
8777 tree type = NULL_TREE;
8778 cp_token *token;
8780 /* Peek at the next token. */
8781 token = cp_lexer_peek_token (parser->lexer);
8783 /* If we're looking at a keyword, things are easy. */
8784 switch (token->keyword)
8786 case RID_CHAR:
8787 type = char_type_node;
8788 break;
8789 case RID_WCHAR:
8790 type = wchar_type_node;
8791 break;
8792 case RID_BOOL:
8793 type = boolean_type_node;
8794 break;
8795 case RID_SHORT:
8796 type = short_integer_type_node;
8797 break;
8798 case RID_INT:
8799 type = integer_type_node;
8800 break;
8801 case RID_LONG:
8802 type = long_integer_type_node;
8803 break;
8804 case RID_SIGNED:
8805 type = integer_type_node;
8806 break;
8807 case RID_UNSIGNED:
8808 type = unsigned_type_node;
8809 break;
8810 case RID_FLOAT:
8811 type = float_type_node;
8812 break;
8813 case RID_DOUBLE:
8814 type = double_type_node;
8815 break;
8816 case RID_VOID:
8817 type = void_type_node;
8818 break;
8820 case RID_TYPEOF:
8822 tree operand;
8824 /* Consume the `typeof' token. */
8825 cp_lexer_consume_token (parser->lexer);
8826 /* Parse the operand to `typeof'. */
8827 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8828 /* If it is not already a TYPE, take its type. */
8829 if (!TYPE_P (operand))
8830 operand = finish_typeof (operand);
8832 return operand;
8835 default:
8836 break;
8839 /* If the type-specifier was for a built-in type, we're done. */
8840 if (type)
8842 tree id;
8844 /* Consume the token. */
8845 id = cp_lexer_consume_token (parser->lexer)->value;
8847 /* There is no valid C++ program where a non-template type is
8848 followed by a "<". That usually indicates that the user thought
8849 that the type was a template. */
8850 cp_parser_check_for_invalid_template_id (parser, type);
8852 return identifier_p ? id : TYPE_NAME (type);
8855 /* The type-specifier must be a user-defined type. */
8856 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8858 /* Don't gobble tokens or issue error messages if this is an
8859 optional type-specifier. */
8860 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8861 cp_parser_parse_tentatively (parser);
8863 /* Look for the optional `::' operator. */
8864 cp_parser_global_scope_opt (parser,
8865 /*current_scope_valid_p=*/false);
8866 /* Look for the nested-name specifier. */
8867 cp_parser_nested_name_specifier_opt (parser,
8868 /*typename_keyword_p=*/false,
8869 /*check_dependency_p=*/true,
8870 /*type_p=*/false,
8871 /*is_declaration=*/false);
8872 /* If we have seen a nested-name-specifier, and the next token
8873 is `template', then we are using the template-id production. */
8874 if (parser->scope
8875 && cp_parser_optional_template_keyword (parser))
8877 /* Look for the template-id. */
8878 type = cp_parser_template_id (parser,
8879 /*template_keyword_p=*/true,
8880 /*check_dependency_p=*/true,
8881 /*is_declaration=*/false);
8882 /* If the template-id did not name a type, we are out of
8883 luck. */
8884 if (TREE_CODE (type) != TYPE_DECL)
8886 cp_parser_error (parser, "expected template-id for type");
8887 type = NULL_TREE;
8890 /* Otherwise, look for a type-name. */
8891 else
8892 type = cp_parser_type_name (parser);
8893 /* If it didn't work out, we don't have a TYPE. */
8894 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8895 && !cp_parser_parse_definitely (parser))
8896 type = NULL_TREE;
8899 /* If we didn't get a type-name, issue an error message. */
8900 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8902 cp_parser_error (parser, "expected type-name");
8903 return error_mark_node;
8906 /* There is no valid C++ program where a non-template type is
8907 followed by a "<". That usually indicates that the user thought
8908 that the type was a template. */
8909 if (type && type != error_mark_node)
8910 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8912 return type;
8915 /* Parse a type-name.
8917 type-name:
8918 class-name
8919 enum-name
8920 typedef-name
8922 enum-name:
8923 identifier
8925 typedef-name:
8926 identifier
8928 Returns a TYPE_DECL for the the type. */
8930 static tree
8931 cp_parser_type_name (cp_parser* parser)
8933 tree type_decl;
8934 tree identifier;
8936 /* We can't know yet whether it is a class-name or not. */
8937 cp_parser_parse_tentatively (parser);
8938 /* Try a class-name. */
8939 type_decl = cp_parser_class_name (parser,
8940 /*typename_keyword_p=*/false,
8941 /*template_keyword_p=*/false,
8942 /*type_p=*/false,
8943 /*check_dependency_p=*/true,
8944 /*class_head_p=*/false,
8945 /*is_declaration=*/false);
8946 /* If it's not a class-name, keep looking. */
8947 if (!cp_parser_parse_definitely (parser))
8949 /* It must be a typedef-name or an enum-name. */
8950 identifier = cp_parser_identifier (parser);
8951 if (identifier == error_mark_node)
8952 return error_mark_node;
8954 /* Look up the type-name. */
8955 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8956 /* Issue an error if we did not find a type-name. */
8957 if (TREE_CODE (type_decl) != TYPE_DECL)
8959 if (!cp_parser_simulate_error (parser))
8960 cp_parser_name_lookup_error (parser, identifier, type_decl,
8961 "is not a type");
8962 type_decl = error_mark_node;
8964 /* Remember that the name was used in the definition of the
8965 current class so that we can check later to see if the
8966 meaning would have been different after the class was
8967 entirely defined. */
8968 else if (type_decl != error_mark_node
8969 && !parser->scope)
8970 maybe_note_name_used_in_class (identifier, type_decl);
8973 return type_decl;
8977 /* Parse an elaborated-type-specifier. Note that the grammar given
8978 here incorporates the resolution to DR68.
8980 elaborated-type-specifier:
8981 class-key :: [opt] nested-name-specifier [opt] identifier
8982 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8983 enum :: [opt] nested-name-specifier [opt] identifier
8984 typename :: [opt] nested-name-specifier identifier
8985 typename :: [opt] nested-name-specifier template [opt]
8986 template-id
8988 GNU extension:
8990 elaborated-type-specifier:
8991 class-key attributes :: [opt] nested-name-specifier [opt] identifier
8992 class-key attributes :: [opt] nested-name-specifier [opt]
8993 template [opt] template-id
8994 enum attributes :: [opt] nested-name-specifier [opt] identifier
8996 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8997 declared `friend'. If IS_DECLARATION is TRUE, then this
8998 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8999 something is being declared.
9001 Returns the TYPE specified. */
9003 static tree
9004 cp_parser_elaborated_type_specifier (cp_parser* parser,
9005 bool is_friend,
9006 bool is_declaration)
9008 enum tag_types tag_type;
9009 tree identifier;
9010 tree type = NULL_TREE;
9011 tree attributes = NULL_TREE;
9013 /* See if we're looking at the `enum' keyword. */
9014 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9016 /* Consume the `enum' token. */
9017 cp_lexer_consume_token (parser->lexer);
9018 /* Remember that it's an enumeration type. */
9019 tag_type = enum_type;
9020 /* Parse the attributes. */
9021 attributes = cp_parser_attributes_opt (parser);
9023 /* Or, it might be `typename'. */
9024 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9025 RID_TYPENAME))
9027 /* Consume the `typename' token. */
9028 cp_lexer_consume_token (parser->lexer);
9029 /* Remember that it's a `typename' type. */
9030 tag_type = typename_type;
9031 /* The `typename' keyword is only allowed in templates. */
9032 if (!processing_template_decl)
9033 pedwarn ("using `typename' outside of template");
9035 /* Otherwise it must be a class-key. */
9036 else
9038 tag_type = cp_parser_class_key (parser);
9039 if (tag_type == none_type)
9040 return error_mark_node;
9041 /* Parse the attributes. */
9042 attributes = cp_parser_attributes_opt (parser);
9045 /* Look for the `::' operator. */
9046 cp_parser_global_scope_opt (parser,
9047 /*current_scope_valid_p=*/false);
9048 /* Look for the nested-name-specifier. */
9049 if (tag_type == typename_type)
9051 if (cp_parser_nested_name_specifier (parser,
9052 /*typename_keyword_p=*/true,
9053 /*check_dependency_p=*/true,
9054 /*type_p=*/true,
9055 is_declaration)
9056 == error_mark_node)
9057 return error_mark_node;
9059 else
9060 /* Even though `typename' is not present, the proposed resolution
9061 to Core Issue 180 says that in `class A<T>::B', `B' should be
9062 considered a type-name, even if `A<T>' is dependent. */
9063 cp_parser_nested_name_specifier_opt (parser,
9064 /*typename_keyword_p=*/true,
9065 /*check_dependency_p=*/true,
9066 /*type_p=*/true,
9067 is_declaration);
9068 /* For everything but enumeration types, consider a template-id. */
9069 if (tag_type != enum_type)
9071 bool template_p = false;
9072 tree decl;
9074 /* Allow the `template' keyword. */
9075 template_p = cp_parser_optional_template_keyword (parser);
9076 /* If we didn't see `template', we don't know if there's a
9077 template-id or not. */
9078 if (!template_p)
9079 cp_parser_parse_tentatively (parser);
9080 /* Parse the template-id. */
9081 decl = cp_parser_template_id (parser, template_p,
9082 /*check_dependency_p=*/true,
9083 is_declaration);
9084 /* If we didn't find a template-id, look for an ordinary
9085 identifier. */
9086 if (!template_p && !cp_parser_parse_definitely (parser))
9088 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9089 in effect, then we must assume that, upon instantiation, the
9090 template will correspond to a class. */
9091 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9092 && tag_type == typename_type)
9093 type = make_typename_type (parser->scope, decl,
9094 /*complain=*/1);
9095 else
9096 type = TREE_TYPE (decl);
9099 /* For an enumeration type, consider only a plain identifier. */
9100 if (!type)
9102 identifier = cp_parser_identifier (parser);
9104 if (identifier == error_mark_node)
9106 parser->scope = NULL_TREE;
9107 return error_mark_node;
9110 /* For a `typename', we needn't call xref_tag. */
9111 if (tag_type == typename_type)
9112 return make_typename_type (parser->scope, identifier,
9113 /*complain=*/1);
9114 /* Look up a qualified name in the usual way. */
9115 if (parser->scope)
9117 tree decl;
9119 /* In an elaborated-type-specifier, names are assumed to name
9120 types, so we set IS_TYPE to TRUE when calling
9121 cp_parser_lookup_name. */
9122 decl = cp_parser_lookup_name (parser, identifier,
9123 /*is_type=*/true,
9124 /*is_template=*/false,
9125 /*is_namespace=*/false,
9126 /*check_dependency=*/true);
9128 /* If we are parsing friend declaration, DECL may be a
9129 TEMPLATE_DECL tree node here. However, we need to check
9130 whether this TEMPLATE_DECL results in valid code. Consider
9131 the following example:
9133 namespace N {
9134 template <class T> class C {};
9136 class X {
9137 template <class T> friend class N::C; // #1, valid code
9139 template <class T> class Y {
9140 friend class N::C; // #2, invalid code
9143 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9144 name lookup of `N::C'. We see that friend declaration must
9145 be template for the code to be valid. Note that
9146 processing_template_decl does not work here since it is
9147 always 1 for the above two cases. */
9149 decl = (cp_parser_maybe_treat_template_as_class
9150 (decl, /*tag_name_p=*/is_friend
9151 && parser->num_template_parameter_lists));
9153 if (TREE_CODE (decl) != TYPE_DECL)
9155 error ("expected type-name");
9156 return error_mark_node;
9159 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9160 check_elaborated_type_specifier
9161 (tag_type, decl,
9162 (parser->num_template_parameter_lists
9163 || DECL_SELF_REFERENCE_P (decl)));
9165 type = TREE_TYPE (decl);
9167 else
9169 /* An elaborated-type-specifier sometimes introduces a new type and
9170 sometimes names an existing type. Normally, the rule is that it
9171 introduces a new type only if there is not an existing type of
9172 the same name already in scope. For example, given:
9174 struct S {};
9175 void f() { struct S s; }
9177 the `struct S' in the body of `f' is the same `struct S' as in
9178 the global scope; the existing definition is used. However, if
9179 there were no global declaration, this would introduce a new
9180 local class named `S'.
9182 An exception to this rule applies to the following code:
9184 namespace N { struct S; }
9186 Here, the elaborated-type-specifier names a new type
9187 unconditionally; even if there is already an `S' in the
9188 containing scope this declaration names a new type.
9189 This exception only applies if the elaborated-type-specifier
9190 forms the complete declaration:
9192 [class.name]
9194 A declaration consisting solely of `class-key identifier ;' is
9195 either a redeclaration of the name in the current scope or a
9196 forward declaration of the identifier as a class name. It
9197 introduces the name into the current scope.
9199 We are in this situation precisely when the next token is a `;'.
9201 An exception to the exception is that a `friend' declaration does
9202 *not* name a new type; i.e., given:
9204 struct S { friend struct T; };
9206 `T' is not a new type in the scope of `S'.
9208 Also, `new struct S' or `sizeof (struct S)' never results in the
9209 definition of a new type; a new type can only be declared in a
9210 declaration context. */
9212 /* Warn about attributes. They are ignored. */
9213 if (attributes)
9214 warning ("type attributes are honored only at type definition");
9216 type = xref_tag (tag_type, identifier,
9217 (is_friend
9218 || !is_declaration
9219 || cp_lexer_next_token_is_not (parser->lexer,
9220 CPP_SEMICOLON)),
9221 parser->num_template_parameter_lists);
9224 if (tag_type != enum_type)
9225 cp_parser_check_class_key (tag_type, type);
9227 /* A "<" cannot follow an elaborated type specifier. If that
9228 happens, the user was probably trying to form a template-id. */
9229 cp_parser_check_for_invalid_template_id (parser, type);
9231 return type;
9234 /* Parse an enum-specifier.
9236 enum-specifier:
9237 enum identifier [opt] { enumerator-list [opt] }
9239 Returns an ENUM_TYPE representing the enumeration. */
9241 static tree
9242 cp_parser_enum_specifier (cp_parser* parser)
9244 cp_token *token;
9245 tree identifier = NULL_TREE;
9246 tree type;
9248 /* Look for the `enum' keyword. */
9249 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9250 return error_mark_node;
9251 /* Peek at the next token. */
9252 token = cp_lexer_peek_token (parser->lexer);
9254 /* See if it is an identifier. */
9255 if (token->type == CPP_NAME)
9256 identifier = cp_parser_identifier (parser);
9258 /* Look for the `{'. */
9259 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9260 return error_mark_node;
9262 /* At this point, we're going ahead with the enum-specifier, even
9263 if some other problem occurs. */
9264 cp_parser_commit_to_tentative_parse (parser);
9266 /* Issue an error message if type-definitions are forbidden here. */
9267 cp_parser_check_type_definition (parser);
9269 /* Create the new type. */
9270 type = start_enum (identifier ? identifier : make_anon_name ());
9272 /* Peek at the next token. */
9273 token = cp_lexer_peek_token (parser->lexer);
9274 /* If it's not a `}', then there are some enumerators. */
9275 if (token->type != CPP_CLOSE_BRACE)
9276 cp_parser_enumerator_list (parser, type);
9277 /* Look for the `}'. */
9278 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9280 /* Finish up the enumeration. */
9281 finish_enum (type);
9283 return type;
9286 /* Parse an enumerator-list. The enumerators all have the indicated
9287 TYPE.
9289 enumerator-list:
9290 enumerator-definition
9291 enumerator-list , enumerator-definition */
9293 static void
9294 cp_parser_enumerator_list (cp_parser* parser, tree type)
9296 while (true)
9298 cp_token *token;
9300 /* Parse an enumerator-definition. */
9301 cp_parser_enumerator_definition (parser, type);
9302 /* Peek at the next token. */
9303 token = cp_lexer_peek_token (parser->lexer);
9304 /* If it's not a `,', then we've reached the end of the
9305 list. */
9306 if (token->type != CPP_COMMA)
9307 break;
9308 /* Otherwise, consume the `,' and keep going. */
9309 cp_lexer_consume_token (parser->lexer);
9310 /* If the next token is a `}', there is a trailing comma. */
9311 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9313 if (pedantic && !in_system_header)
9314 pedwarn ("comma at end of enumerator list");
9315 break;
9320 /* Parse an enumerator-definition. The enumerator has the indicated
9321 TYPE.
9323 enumerator-definition:
9324 enumerator
9325 enumerator = constant-expression
9327 enumerator:
9328 identifier */
9330 static void
9331 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9333 cp_token *token;
9334 tree identifier;
9335 tree value;
9337 /* Look for the identifier. */
9338 identifier = cp_parser_identifier (parser);
9339 if (identifier == error_mark_node)
9340 return;
9342 /* Peek at the next token. */
9343 token = cp_lexer_peek_token (parser->lexer);
9344 /* If it's an `=', then there's an explicit value. */
9345 if (token->type == CPP_EQ)
9347 /* Consume the `=' token. */
9348 cp_lexer_consume_token (parser->lexer);
9349 /* Parse the value. */
9350 value = cp_parser_constant_expression (parser,
9351 /*allow_non_constant_p=*/false,
9352 NULL);
9354 else
9355 value = NULL_TREE;
9357 /* Create the enumerator. */
9358 build_enumerator (identifier, value, type);
9361 /* Parse a namespace-name.
9363 namespace-name:
9364 original-namespace-name
9365 namespace-alias
9367 Returns the NAMESPACE_DECL for the namespace. */
9369 static tree
9370 cp_parser_namespace_name (cp_parser* parser)
9372 tree identifier;
9373 tree namespace_decl;
9375 /* Get the name of the namespace. */
9376 identifier = cp_parser_identifier (parser);
9377 if (identifier == error_mark_node)
9378 return error_mark_node;
9380 /* Look up the identifier in the currently active scope. Look only
9381 for namespaces, due to:
9383 [basic.lookup.udir]
9385 When looking up a namespace-name in a using-directive or alias
9386 definition, only namespace names are considered.
9388 And:
9390 [basic.lookup.qual]
9392 During the lookup of a name preceding the :: scope resolution
9393 operator, object, function, and enumerator names are ignored.
9395 (Note that cp_parser_class_or_namespace_name only calls this
9396 function if the token after the name is the scope resolution
9397 operator.) */
9398 namespace_decl = cp_parser_lookup_name (parser, identifier,
9399 /*is_type=*/false,
9400 /*is_template=*/false,
9401 /*is_namespace=*/true,
9402 /*check_dependency=*/true);
9403 /* If it's not a namespace, issue an error. */
9404 if (namespace_decl == error_mark_node
9405 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9407 cp_parser_error (parser, "expected namespace-name");
9408 namespace_decl = error_mark_node;
9411 return namespace_decl;
9414 /* Parse a namespace-definition.
9416 namespace-definition:
9417 named-namespace-definition
9418 unnamed-namespace-definition
9420 named-namespace-definition:
9421 original-namespace-definition
9422 extension-namespace-definition
9424 original-namespace-definition:
9425 namespace identifier { namespace-body }
9427 extension-namespace-definition:
9428 namespace original-namespace-name { namespace-body }
9430 unnamed-namespace-definition:
9431 namespace { namespace-body } */
9433 static void
9434 cp_parser_namespace_definition (cp_parser* parser)
9436 tree identifier;
9438 /* Look for the `namespace' keyword. */
9439 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9441 /* Get the name of the namespace. We do not attempt to distinguish
9442 between an original-namespace-definition and an
9443 extension-namespace-definition at this point. The semantic
9444 analysis routines are responsible for that. */
9445 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9446 identifier = cp_parser_identifier (parser);
9447 else
9448 identifier = NULL_TREE;
9450 /* Look for the `{' to start the namespace. */
9451 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9452 /* Start the namespace. */
9453 push_namespace (identifier);
9454 /* Parse the body of the namespace. */
9455 cp_parser_namespace_body (parser);
9456 /* Finish the namespace. */
9457 pop_namespace ();
9458 /* Look for the final `}'. */
9459 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9462 /* Parse a namespace-body.
9464 namespace-body:
9465 declaration-seq [opt] */
9467 static void
9468 cp_parser_namespace_body (cp_parser* parser)
9470 cp_parser_declaration_seq_opt (parser);
9473 /* Parse a namespace-alias-definition.
9475 namespace-alias-definition:
9476 namespace identifier = qualified-namespace-specifier ; */
9478 static void
9479 cp_parser_namespace_alias_definition (cp_parser* parser)
9481 tree identifier;
9482 tree namespace_specifier;
9484 /* Look for the `namespace' keyword. */
9485 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9486 /* Look for the identifier. */
9487 identifier = cp_parser_identifier (parser);
9488 if (identifier == error_mark_node)
9489 return;
9490 /* Look for the `=' token. */
9491 cp_parser_require (parser, CPP_EQ, "`='");
9492 /* Look for the qualified-namespace-specifier. */
9493 namespace_specifier
9494 = cp_parser_qualified_namespace_specifier (parser);
9495 /* Look for the `;' token. */
9496 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9498 /* Register the alias in the symbol table. */
9499 do_namespace_alias (identifier, namespace_specifier);
9502 /* Parse a qualified-namespace-specifier.
9504 qualified-namespace-specifier:
9505 :: [opt] nested-name-specifier [opt] namespace-name
9507 Returns a NAMESPACE_DECL corresponding to the specified
9508 namespace. */
9510 static tree
9511 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9513 /* Look for the optional `::'. */
9514 cp_parser_global_scope_opt (parser,
9515 /*current_scope_valid_p=*/false);
9517 /* Look for the optional nested-name-specifier. */
9518 cp_parser_nested_name_specifier_opt (parser,
9519 /*typename_keyword_p=*/false,
9520 /*check_dependency_p=*/true,
9521 /*type_p=*/false,
9522 /*is_declaration=*/true);
9524 return cp_parser_namespace_name (parser);
9527 /* Parse a using-declaration.
9529 using-declaration:
9530 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9531 using :: unqualified-id ; */
9533 static void
9534 cp_parser_using_declaration (cp_parser* parser)
9536 cp_token *token;
9537 bool typename_p = false;
9538 bool global_scope_p;
9539 tree decl;
9540 tree identifier;
9541 tree scope;
9542 tree qscope;
9544 /* Look for the `using' keyword. */
9545 cp_parser_require_keyword (parser, RID_USING, "`using'");
9547 /* Peek at the next token. */
9548 token = cp_lexer_peek_token (parser->lexer);
9549 /* See if it's `typename'. */
9550 if (token->keyword == RID_TYPENAME)
9552 /* Remember that we've seen it. */
9553 typename_p = true;
9554 /* Consume the `typename' token. */
9555 cp_lexer_consume_token (parser->lexer);
9558 /* Look for the optional global scope qualification. */
9559 global_scope_p
9560 = (cp_parser_global_scope_opt (parser,
9561 /*current_scope_valid_p=*/false)
9562 != NULL_TREE);
9564 /* If we saw `typename', or didn't see `::', then there must be a
9565 nested-name-specifier present. */
9566 if (typename_p || !global_scope_p)
9567 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9568 /*check_dependency_p=*/true,
9569 /*type_p=*/false,
9570 /*is_declaration=*/true);
9571 /* Otherwise, we could be in either of the two productions. In that
9572 case, treat the nested-name-specifier as optional. */
9573 else
9574 qscope = cp_parser_nested_name_specifier_opt (parser,
9575 /*typename_keyword_p=*/false,
9576 /*check_dependency_p=*/true,
9577 /*type_p=*/false,
9578 /*is_declaration=*/true);
9579 if (!qscope)
9580 qscope = global_namespace;
9582 /* Parse the unqualified-id. */
9583 identifier = cp_parser_unqualified_id (parser,
9584 /*template_keyword_p=*/false,
9585 /*check_dependency_p=*/true,
9586 /*declarator_p=*/true);
9588 /* The function we call to handle a using-declaration is different
9589 depending on what scope we are in. */
9590 if (identifier == error_mark_node)
9592 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9593 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9594 /* [namespace.udecl]
9596 A using declaration shall not name a template-id. */
9597 error ("a template-id may not appear in a using-declaration");
9598 else
9600 scope = current_scope ();
9601 if (scope && TYPE_P (scope))
9603 /* Create the USING_DECL. */
9604 decl = do_class_using_decl (build_nt (SCOPE_REF,
9605 parser->scope,
9606 identifier));
9607 /* Add it to the list of members in this class. */
9608 finish_member_declaration (decl);
9610 else
9612 decl = cp_parser_lookup_name_simple (parser, identifier);
9613 if (decl == error_mark_node)
9614 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9615 else if (scope)
9616 do_local_using_decl (decl, qscope, identifier);
9617 else
9618 do_toplevel_using_decl (decl, qscope, identifier);
9622 /* Look for the final `;'. */
9623 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9626 /* Parse a using-directive.
9628 using-directive:
9629 using namespace :: [opt] nested-name-specifier [opt]
9630 namespace-name ; */
9632 static void
9633 cp_parser_using_directive (cp_parser* parser)
9635 tree namespace_decl;
9636 tree attribs;
9638 /* Look for the `using' keyword. */
9639 cp_parser_require_keyword (parser, RID_USING, "`using'");
9640 /* And the `namespace' keyword. */
9641 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9642 /* Look for the optional `::' operator. */
9643 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9644 /* And the optional nested-name-specifier. */
9645 cp_parser_nested_name_specifier_opt (parser,
9646 /*typename_keyword_p=*/false,
9647 /*check_dependency_p=*/true,
9648 /*type_p=*/false,
9649 /*is_declaration=*/true);
9650 /* Get the namespace being used. */
9651 namespace_decl = cp_parser_namespace_name (parser);
9652 /* And any specified attributes. */
9653 attribs = cp_parser_attributes_opt (parser);
9654 /* Update the symbol table. */
9655 parse_using_directive (namespace_decl, attribs);
9656 /* Look for the final `;'. */
9657 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9660 /* Parse an asm-definition.
9662 asm-definition:
9663 asm ( string-literal ) ;
9665 GNU Extension:
9667 asm-definition:
9668 asm volatile [opt] ( string-literal ) ;
9669 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9670 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9671 : asm-operand-list [opt] ) ;
9672 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9673 : asm-operand-list [opt]
9674 : asm-operand-list [opt] ) ; */
9676 static void
9677 cp_parser_asm_definition (cp_parser* parser)
9679 cp_token *token;
9680 tree string;
9681 tree outputs = NULL_TREE;
9682 tree inputs = NULL_TREE;
9683 tree clobbers = NULL_TREE;
9684 tree asm_stmt;
9685 bool volatile_p = false;
9686 bool extended_p = false;
9688 /* Look for the `asm' keyword. */
9689 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9690 /* See if the next token is `volatile'. */
9691 if (cp_parser_allow_gnu_extensions_p (parser)
9692 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9694 /* Remember that we saw the `volatile' keyword. */
9695 volatile_p = true;
9696 /* Consume the token. */
9697 cp_lexer_consume_token (parser->lexer);
9699 /* Look for the opening `('. */
9700 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9701 /* Look for the string. */
9702 token = cp_parser_require (parser, CPP_STRING, "asm body");
9703 if (!token)
9704 return;
9705 string = token->value;
9706 /* If we're allowing GNU extensions, check for the extended assembly
9707 syntax. Unfortunately, the `:' tokens need not be separated by
9708 a space in C, and so, for compatibility, we tolerate that here
9709 too. Doing that means that we have to treat the `::' operator as
9710 two `:' tokens. */
9711 if (cp_parser_allow_gnu_extensions_p (parser)
9712 && at_function_scope_p ()
9713 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9714 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9716 bool inputs_p = false;
9717 bool clobbers_p = false;
9719 /* The extended syntax was used. */
9720 extended_p = true;
9722 /* Look for outputs. */
9723 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9725 /* Consume the `:'. */
9726 cp_lexer_consume_token (parser->lexer);
9727 /* Parse the output-operands. */
9728 if (cp_lexer_next_token_is_not (parser->lexer,
9729 CPP_COLON)
9730 && cp_lexer_next_token_is_not (parser->lexer,
9731 CPP_SCOPE)
9732 && cp_lexer_next_token_is_not (parser->lexer,
9733 CPP_CLOSE_PAREN))
9734 outputs = cp_parser_asm_operand_list (parser);
9736 /* If the next token is `::', there are no outputs, and the
9737 next token is the beginning of the inputs. */
9738 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9740 /* Consume the `::' token. */
9741 cp_lexer_consume_token (parser->lexer);
9742 /* The inputs are coming next. */
9743 inputs_p = true;
9746 /* Look for inputs. */
9747 if (inputs_p
9748 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9750 if (!inputs_p)
9751 /* Consume the `:'. */
9752 cp_lexer_consume_token (parser->lexer);
9753 /* Parse the output-operands. */
9754 if (cp_lexer_next_token_is_not (parser->lexer,
9755 CPP_COLON)
9756 && cp_lexer_next_token_is_not (parser->lexer,
9757 CPP_SCOPE)
9758 && cp_lexer_next_token_is_not (parser->lexer,
9759 CPP_CLOSE_PAREN))
9760 inputs = cp_parser_asm_operand_list (parser);
9762 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9763 /* The clobbers are coming next. */
9764 clobbers_p = true;
9766 /* Look for clobbers. */
9767 if (clobbers_p
9768 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9770 if (!clobbers_p)
9771 /* Consume the `:'. */
9772 cp_lexer_consume_token (parser->lexer);
9773 /* Parse the clobbers. */
9774 if (cp_lexer_next_token_is_not (parser->lexer,
9775 CPP_CLOSE_PAREN))
9776 clobbers = cp_parser_asm_clobber_list (parser);
9779 /* Look for the closing `)'. */
9780 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9781 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9782 /*consume_paren=*/true);
9783 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9785 /* Create the ASM_STMT. */
9786 if (at_function_scope_p ())
9788 asm_stmt =
9789 finish_asm_stmt (volatile_p
9790 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9791 string, outputs, inputs, clobbers);
9792 /* If the extended syntax was not used, mark the ASM_STMT. */
9793 if (!extended_p)
9794 ASM_INPUT_P (asm_stmt) = 1;
9796 else
9797 assemble_asm (string);
9800 /* Declarators [gram.dcl.decl] */
9802 /* Parse an init-declarator.
9804 init-declarator:
9805 declarator initializer [opt]
9807 GNU Extension:
9809 init-declarator:
9810 declarator asm-specification [opt] attributes [opt] initializer [opt]
9812 function-definition:
9813 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9814 function-body
9815 decl-specifier-seq [opt] declarator function-try-block
9817 GNU Extension:
9819 function-definition:
9820 __extension__ function-definition
9822 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9823 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9824 then this declarator appears in a class scope. The new DECL created
9825 by this declarator is returned.
9827 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9828 for a function-definition here as well. If the declarator is a
9829 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9830 be TRUE upon return. By that point, the function-definition will
9831 have been completely parsed.
9833 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9834 is FALSE. */
9836 static tree
9837 cp_parser_init_declarator (cp_parser* parser,
9838 tree decl_specifiers,
9839 tree prefix_attributes,
9840 bool function_definition_allowed_p,
9841 bool member_p,
9842 int declares_class_or_enum,
9843 bool* function_definition_p)
9845 cp_token *token;
9846 tree declarator;
9847 tree attributes;
9848 tree asm_specification;
9849 tree initializer;
9850 tree decl = NULL_TREE;
9851 tree scope;
9852 bool is_initialized;
9853 bool is_parenthesized_init;
9854 bool is_non_constant_init;
9855 int ctor_dtor_or_conv_p;
9856 bool friend_p;
9857 bool pop_p = false;
9859 /* Assume that this is not the declarator for a function
9860 definition. */
9861 if (function_definition_p)
9862 *function_definition_p = false;
9864 /* Defer access checks while parsing the declarator; we cannot know
9865 what names are accessible until we know what is being
9866 declared. */
9867 resume_deferring_access_checks ();
9869 /* Parse the declarator. */
9870 declarator
9871 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9872 &ctor_dtor_or_conv_p,
9873 /*parenthesized_p=*/NULL);
9874 /* Gather up the deferred checks. */
9875 stop_deferring_access_checks ();
9877 /* If the DECLARATOR was erroneous, there's no need to go
9878 further. */
9879 if (declarator == error_mark_node)
9880 return error_mark_node;
9882 cp_parser_check_for_definition_in_return_type (declarator,
9883 declares_class_or_enum);
9885 /* Figure out what scope the entity declared by the DECLARATOR is
9886 located in. `grokdeclarator' sometimes changes the scope, so
9887 we compute it now. */
9888 scope = get_scope_of_declarator (declarator);
9890 /* If we're allowing GNU extensions, look for an asm-specification
9891 and attributes. */
9892 if (cp_parser_allow_gnu_extensions_p (parser))
9894 /* Look for an asm-specification. */
9895 asm_specification = cp_parser_asm_specification_opt (parser);
9896 /* And attributes. */
9897 attributes = cp_parser_attributes_opt (parser);
9899 else
9901 asm_specification = NULL_TREE;
9902 attributes = NULL_TREE;
9905 /* Peek at the next token. */
9906 token = cp_lexer_peek_token (parser->lexer);
9907 /* Check to see if the token indicates the start of a
9908 function-definition. */
9909 if (cp_parser_token_starts_function_definition_p (token))
9911 if (!function_definition_allowed_p)
9913 /* If a function-definition should not appear here, issue an
9914 error message. */
9915 cp_parser_error (parser,
9916 "a function-definition is not allowed here");
9917 return error_mark_node;
9919 else
9921 /* Neither attributes nor an asm-specification are allowed
9922 on a function-definition. */
9923 if (asm_specification)
9924 error ("an asm-specification is not allowed on a function-definition");
9925 if (attributes)
9926 error ("attributes are not allowed on a function-definition");
9927 /* This is a function-definition. */
9928 *function_definition_p = true;
9930 /* Parse the function definition. */
9931 if (member_p)
9932 decl = cp_parser_save_member_function_body (parser,
9933 decl_specifiers,
9934 declarator,
9935 prefix_attributes);
9936 else
9937 decl
9938 = (cp_parser_function_definition_from_specifiers_and_declarator
9939 (parser, decl_specifiers, prefix_attributes, declarator));
9941 return decl;
9945 /* [dcl.dcl]
9947 Only in function declarations for constructors, destructors, and
9948 type conversions can the decl-specifier-seq be omitted.
9950 We explicitly postpone this check past the point where we handle
9951 function-definitions because we tolerate function-definitions
9952 that are missing their return types in some modes. */
9953 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
9955 cp_parser_error (parser,
9956 "expected constructor, destructor, or type conversion");
9957 return error_mark_node;
9960 /* An `=' or an `(' indicates an initializer. */
9961 is_initialized = (token->type == CPP_EQ
9962 || token->type == CPP_OPEN_PAREN);
9963 /* If the init-declarator isn't initialized and isn't followed by a
9964 `,' or `;', it's not a valid init-declarator. */
9965 if (!is_initialized
9966 && token->type != CPP_COMMA
9967 && token->type != CPP_SEMICOLON)
9969 cp_parser_error (parser, "expected init-declarator");
9970 return error_mark_node;
9973 /* Because start_decl has side-effects, we should only call it if we
9974 know we're going ahead. By this point, we know that we cannot
9975 possibly be looking at any other construct. */
9976 cp_parser_commit_to_tentative_parse (parser);
9978 /* If the decl specifiers were bad, issue an error now that we're
9979 sure this was intended to be a declarator. Then continue
9980 declaring the variable(s), as int, to try to cut down on further
9981 errors. */
9982 if (decl_specifiers != NULL
9983 && TREE_VALUE (decl_specifiers) == error_mark_node)
9985 cp_parser_error (parser, "invalid type in declaration");
9986 TREE_VALUE (decl_specifiers) = integer_type_node;
9989 /* Check to see whether or not this declaration is a friend. */
9990 friend_p = cp_parser_friend_p (decl_specifiers);
9992 /* Check that the number of template-parameter-lists is OK. */
9993 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
9994 return error_mark_node;
9996 /* Enter the newly declared entry in the symbol table. If we're
9997 processing a declaration in a class-specifier, we wait until
9998 after processing the initializer. */
9999 if (!member_p)
10001 if (parser->in_unbraced_linkage_specification_p)
10003 decl_specifiers = tree_cons (error_mark_node,
10004 get_identifier ("extern"),
10005 decl_specifiers);
10006 have_extern_spec = false;
10008 decl = start_decl (declarator, decl_specifiers,
10009 is_initialized, attributes, prefix_attributes);
10012 /* Enter the SCOPE. That way unqualified names appearing in the
10013 initializer will be looked up in SCOPE. */
10014 if (scope)
10015 pop_p = push_scope (scope);
10017 /* Perform deferred access control checks, now that we know in which
10018 SCOPE the declared entity resides. */
10019 if (!member_p && decl)
10021 tree saved_current_function_decl = NULL_TREE;
10023 /* If the entity being declared is a function, pretend that we
10024 are in its scope. If it is a `friend', it may have access to
10025 things that would not otherwise be accessible. */
10026 if (TREE_CODE (decl) == FUNCTION_DECL)
10028 saved_current_function_decl = current_function_decl;
10029 current_function_decl = decl;
10032 /* Perform the access control checks for the declarator and the
10033 the decl-specifiers. */
10034 perform_deferred_access_checks ();
10036 /* Restore the saved value. */
10037 if (TREE_CODE (decl) == FUNCTION_DECL)
10038 current_function_decl = saved_current_function_decl;
10041 /* Parse the initializer. */
10042 if (is_initialized)
10043 initializer = cp_parser_initializer (parser,
10044 &is_parenthesized_init,
10045 &is_non_constant_init);
10046 else
10048 initializer = NULL_TREE;
10049 is_parenthesized_init = false;
10050 is_non_constant_init = true;
10053 /* The old parser allows attributes to appear after a parenthesized
10054 initializer. Mark Mitchell proposed removing this functionality
10055 on the GCC mailing lists on 2002-08-13. This parser accepts the
10056 attributes -- but ignores them. */
10057 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10058 if (cp_parser_attributes_opt (parser))
10059 warning ("attributes after parenthesized initializer ignored");
10061 /* Leave the SCOPE, now that we have processed the initializer. It
10062 is important to do this before calling cp_finish_decl because it
10063 makes decisions about whether to create DECL_STMTs or not based
10064 on the current scope. */
10065 if (pop_p)
10066 pop_scope (scope);
10068 /* For an in-class declaration, use `grokfield' to create the
10069 declaration. */
10070 if (member_p)
10072 decl = grokfield (declarator, decl_specifiers,
10073 initializer, /*asmspec=*/NULL_TREE,
10074 /*attributes=*/NULL_TREE);
10075 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10076 cp_parser_save_default_args (parser, decl);
10079 /* Finish processing the declaration. But, skip friend
10080 declarations. */
10081 if (!friend_p && decl)
10082 cp_finish_decl (decl,
10083 initializer,
10084 asm_specification,
10085 /* If the initializer is in parentheses, then this is
10086 a direct-initialization, which means that an
10087 `explicit' constructor is OK. Otherwise, an
10088 `explicit' constructor cannot be used. */
10089 ((is_parenthesized_init || !is_initialized)
10090 ? 0 : LOOKUP_ONLYCONVERTING));
10092 /* Remember whether or not variables were initialized by
10093 constant-expressions. */
10094 if (decl && TREE_CODE (decl) == VAR_DECL
10095 && is_initialized && !is_non_constant_init)
10096 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10098 return decl;
10101 /* Parse a declarator.
10103 declarator:
10104 direct-declarator
10105 ptr-operator declarator
10107 abstract-declarator:
10108 ptr-operator abstract-declarator [opt]
10109 direct-abstract-declarator
10111 GNU Extensions:
10113 declarator:
10114 attributes [opt] direct-declarator
10115 attributes [opt] ptr-operator declarator
10117 abstract-declarator:
10118 attributes [opt] ptr-operator abstract-declarator [opt]
10119 attributes [opt] direct-abstract-declarator
10121 Returns a representation of the declarator. If the declarator has
10122 the form `* declarator', then an INDIRECT_REF is returned, whose
10123 only operand is the sub-declarator. Analogously, `& declarator' is
10124 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10125 used. The first operand is the TYPE for `X'. The second operand
10126 is an INDIRECT_REF whose operand is the sub-declarator.
10128 Otherwise, the representation is as for a direct-declarator.
10130 (It would be better to define a structure type to represent
10131 declarators, rather than abusing `tree' nodes to represent
10132 declarators. That would be much clearer and save some memory.
10133 There is no reason for declarators to be garbage-collected, for
10134 example; they are created during parser and no longer needed after
10135 `grokdeclarator' has been called.)
10137 For a ptr-operator that has the optional cv-qualifier-seq,
10138 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10139 node.
10141 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10142 detect constructor, destructor or conversion operators. It is set
10143 to -1 if the declarator is a name, and +1 if it is a
10144 function. Otherwise it is set to zero. Usually you just want to
10145 test for >0, but internally the negative value is used.
10147 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10148 a decl-specifier-seq unless it declares a constructor, destructor,
10149 or conversion. It might seem that we could check this condition in
10150 semantic analysis, rather than parsing, but that makes it difficult
10151 to handle something like `f()'. We want to notice that there are
10152 no decl-specifiers, and therefore realize that this is an
10153 expression, not a declaration.)
10155 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10156 the declarator is a direct-declarator of the form "(...)". */
10158 static tree
10159 cp_parser_declarator (cp_parser* parser,
10160 cp_parser_declarator_kind dcl_kind,
10161 int* ctor_dtor_or_conv_p,
10162 bool* parenthesized_p)
10164 cp_token *token;
10165 tree declarator;
10166 enum tree_code code;
10167 tree cv_qualifier_seq;
10168 tree class_type;
10169 tree attributes = NULL_TREE;
10171 /* Assume this is not a constructor, destructor, or type-conversion
10172 operator. */
10173 if (ctor_dtor_or_conv_p)
10174 *ctor_dtor_or_conv_p = 0;
10176 if (cp_parser_allow_gnu_extensions_p (parser))
10177 attributes = cp_parser_attributes_opt (parser);
10179 /* Peek at the next token. */
10180 token = cp_lexer_peek_token (parser->lexer);
10182 /* Check for the ptr-operator production. */
10183 cp_parser_parse_tentatively (parser);
10184 /* Parse the ptr-operator. */
10185 code = cp_parser_ptr_operator (parser,
10186 &class_type,
10187 &cv_qualifier_seq);
10188 /* If that worked, then we have a ptr-operator. */
10189 if (cp_parser_parse_definitely (parser))
10191 /* If a ptr-operator was found, then this declarator was not
10192 parenthesized. */
10193 if (parenthesized_p)
10194 *parenthesized_p = true;
10195 /* The dependent declarator is optional if we are parsing an
10196 abstract-declarator. */
10197 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10198 cp_parser_parse_tentatively (parser);
10200 /* Parse the dependent declarator. */
10201 declarator = cp_parser_declarator (parser, dcl_kind,
10202 /*ctor_dtor_or_conv_p=*/NULL,
10203 /*parenthesized_p=*/NULL);
10205 /* If we are parsing an abstract-declarator, we must handle the
10206 case where the dependent declarator is absent. */
10207 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10208 && !cp_parser_parse_definitely (parser))
10209 declarator = NULL_TREE;
10211 /* Build the representation of the ptr-operator. */
10212 if (code == INDIRECT_REF)
10213 declarator = make_pointer_declarator (cv_qualifier_seq,
10214 declarator);
10215 else
10216 declarator = make_reference_declarator (cv_qualifier_seq,
10217 declarator);
10218 /* Handle the pointer-to-member case. */
10219 if (class_type)
10220 declarator = build_nt (SCOPE_REF, class_type, declarator);
10222 /* Everything else is a direct-declarator. */
10223 else
10225 if (parenthesized_p)
10226 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10227 CPP_OPEN_PAREN);
10228 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10229 ctor_dtor_or_conv_p);
10232 if (attributes && declarator != error_mark_node)
10233 declarator = tree_cons (attributes, declarator, NULL_TREE);
10235 return declarator;
10238 /* Parse a direct-declarator or direct-abstract-declarator.
10240 direct-declarator:
10241 declarator-id
10242 direct-declarator ( parameter-declaration-clause )
10243 cv-qualifier-seq [opt]
10244 exception-specification [opt]
10245 direct-declarator [ constant-expression [opt] ]
10246 ( declarator )
10248 direct-abstract-declarator:
10249 direct-abstract-declarator [opt]
10250 ( parameter-declaration-clause )
10251 cv-qualifier-seq [opt]
10252 exception-specification [opt]
10253 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10254 ( abstract-declarator )
10256 Returns a representation of the declarator. DCL_KIND is
10257 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10258 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10259 we are parsing a direct-declarator. It is
10260 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10261 of ambiguity we prefer an abstract declarator, as per
10262 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10263 cp_parser_declarator.
10265 For the declarator-id production, the representation is as for an
10266 id-expression, except that a qualified name is represented as a
10267 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10268 see the documentation of the FUNCTION_DECLARATOR_* macros for
10269 information about how to find the various declarator components.
10270 An array-declarator is represented as an ARRAY_REF. The
10271 direct-declarator is the first operand; the constant-expression
10272 indicating the size of the array is the second operand. */
10274 static tree
10275 cp_parser_direct_declarator (cp_parser* parser,
10276 cp_parser_declarator_kind dcl_kind,
10277 int* ctor_dtor_or_conv_p)
10279 cp_token *token;
10280 tree declarator = NULL_TREE;
10281 tree scope = NULL_TREE;
10282 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10283 bool saved_in_declarator_p = parser->in_declarator_p;
10284 bool first = true;
10285 bool pop_p = false;
10287 while (true)
10289 /* Peek at the next token. */
10290 token = cp_lexer_peek_token (parser->lexer);
10291 if (token->type == CPP_OPEN_PAREN)
10293 /* This is either a parameter-declaration-clause, or a
10294 parenthesized declarator. When we know we are parsing a
10295 named declarator, it must be a parenthesized declarator
10296 if FIRST is true. For instance, `(int)' is a
10297 parameter-declaration-clause, with an omitted
10298 direct-abstract-declarator. But `((*))', is a
10299 parenthesized abstract declarator. Finally, when T is a
10300 template parameter `(T)' is a
10301 parameter-declaration-clause, and not a parenthesized
10302 named declarator.
10304 We first try and parse a parameter-declaration-clause,
10305 and then try a nested declarator (if FIRST is true).
10307 It is not an error for it not to be a
10308 parameter-declaration-clause, even when FIRST is
10309 false. Consider,
10311 int i (int);
10312 int i (3);
10314 The first is the declaration of a function while the
10315 second is a the definition of a variable, including its
10316 initializer.
10318 Having seen only the parenthesis, we cannot know which of
10319 these two alternatives should be selected. Even more
10320 complex are examples like:
10322 int i (int (a));
10323 int i (int (3));
10325 The former is a function-declaration; the latter is a
10326 variable initialization.
10328 Thus again, we try a parameter-declaration-clause, and if
10329 that fails, we back out and return. */
10331 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10333 tree params;
10334 unsigned saved_num_template_parameter_lists;
10336 cp_parser_parse_tentatively (parser);
10338 /* Consume the `('. */
10339 cp_lexer_consume_token (parser->lexer);
10340 if (first)
10342 /* If this is going to be an abstract declarator, we're
10343 in a declarator and we can't have default args. */
10344 parser->default_arg_ok_p = false;
10345 parser->in_declarator_p = true;
10348 /* Inside the function parameter list, surrounding
10349 template-parameter-lists do not apply. */
10350 saved_num_template_parameter_lists
10351 = parser->num_template_parameter_lists;
10352 parser->num_template_parameter_lists = 0;
10354 /* Parse the parameter-declaration-clause. */
10355 params = cp_parser_parameter_declaration_clause (parser);
10357 parser->num_template_parameter_lists
10358 = saved_num_template_parameter_lists;
10360 /* If all went well, parse the cv-qualifier-seq and the
10361 exception-specification. */
10362 if (cp_parser_parse_definitely (parser))
10364 tree cv_qualifiers;
10365 tree exception_specification;
10367 if (ctor_dtor_or_conv_p)
10368 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10369 first = false;
10370 /* Consume the `)'. */
10371 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10373 /* Parse the cv-qualifier-seq. */
10374 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10375 /* And the exception-specification. */
10376 exception_specification
10377 = cp_parser_exception_specification_opt (parser);
10379 /* Create the function-declarator. */
10380 declarator = make_call_declarator (declarator,
10381 params,
10382 cv_qualifiers,
10383 exception_specification);
10384 /* Any subsequent parameter lists are to do with
10385 return type, so are not those of the declared
10386 function. */
10387 parser->default_arg_ok_p = false;
10389 /* Repeat the main loop. */
10390 continue;
10394 /* If this is the first, we can try a parenthesized
10395 declarator. */
10396 if (first)
10398 bool saved_in_type_id_in_expr_p;
10400 parser->default_arg_ok_p = saved_default_arg_ok_p;
10401 parser->in_declarator_p = saved_in_declarator_p;
10403 /* Consume the `('. */
10404 cp_lexer_consume_token (parser->lexer);
10405 /* Parse the nested declarator. */
10406 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10407 parser->in_type_id_in_expr_p = true;
10408 declarator
10409 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10410 /*parenthesized_p=*/NULL);
10411 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10412 first = false;
10413 /* Expect a `)'. */
10414 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10415 declarator = error_mark_node;
10416 if (declarator == error_mark_node)
10417 break;
10419 goto handle_declarator;
10421 /* Otherwise, we must be done. */
10422 else
10423 break;
10425 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10426 && token->type == CPP_OPEN_SQUARE)
10428 /* Parse an array-declarator. */
10429 tree bounds;
10431 if (ctor_dtor_or_conv_p)
10432 *ctor_dtor_or_conv_p = 0;
10434 first = false;
10435 parser->default_arg_ok_p = false;
10436 parser->in_declarator_p = true;
10437 /* Consume the `['. */
10438 cp_lexer_consume_token (parser->lexer);
10439 /* Peek at the next token. */
10440 token = cp_lexer_peek_token (parser->lexer);
10441 /* If the next token is `]', then there is no
10442 constant-expression. */
10443 if (token->type != CPP_CLOSE_SQUARE)
10445 bool non_constant_p;
10447 bounds
10448 = cp_parser_constant_expression (parser,
10449 /*allow_non_constant=*/true,
10450 &non_constant_p);
10451 if (!non_constant_p)
10452 bounds = fold_non_dependent_expr (bounds);
10454 else
10455 bounds = NULL_TREE;
10456 /* Look for the closing `]'. */
10457 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10459 declarator = error_mark_node;
10460 break;
10463 declarator = build_nt (ARRAY_REF, declarator, bounds);
10465 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10467 /* Parse a declarator-id */
10468 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10469 cp_parser_parse_tentatively (parser);
10470 declarator = cp_parser_declarator_id (parser);
10471 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10473 if (!cp_parser_parse_definitely (parser))
10474 declarator = error_mark_node;
10475 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10477 cp_parser_error (parser, "expected unqualified-id");
10478 declarator = error_mark_node;
10482 if (declarator == error_mark_node)
10483 break;
10485 if (TREE_CODE (declarator) == SCOPE_REF
10486 && !current_scope ())
10488 tree scope = TREE_OPERAND (declarator, 0);
10490 /* In the declaration of a member of a template class
10491 outside of the class itself, the SCOPE will sometimes
10492 be a TYPENAME_TYPE. For example, given:
10494 template <typename T>
10495 int S<T>::R::i = 3;
10497 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10498 this context, we must resolve S<T>::R to an ordinary
10499 type, rather than a typename type.
10501 The reason we normally avoid resolving TYPENAME_TYPEs
10502 is that a specialization of `S' might render
10503 `S<T>::R' not a type. However, if `S' is
10504 specialized, then this `i' will not be used, so there
10505 is no harm in resolving the types here. */
10506 if (TREE_CODE (scope) == TYPENAME_TYPE)
10508 tree type;
10510 /* Resolve the TYPENAME_TYPE. */
10511 type = resolve_typename_type (scope,
10512 /*only_current_p=*/false);
10513 /* If that failed, the declarator is invalid. */
10514 if (type != error_mark_node)
10515 scope = type;
10516 /* Build a new DECLARATOR. */
10517 declarator = build_nt (SCOPE_REF,
10518 scope,
10519 TREE_OPERAND (declarator, 1));
10523 /* Check to see whether the declarator-id names a constructor,
10524 destructor, or conversion. */
10525 if (declarator && ctor_dtor_or_conv_p
10526 && ((TREE_CODE (declarator) == SCOPE_REF
10527 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10528 || (TREE_CODE (declarator) != SCOPE_REF
10529 && at_class_scope_p ())))
10531 tree unqualified_name;
10532 tree class_type;
10534 /* Get the unqualified part of the name. */
10535 if (TREE_CODE (declarator) == SCOPE_REF)
10537 class_type = TREE_OPERAND (declarator, 0);
10538 unqualified_name = TREE_OPERAND (declarator, 1);
10540 else
10542 class_type = current_class_type;
10543 unqualified_name = declarator;
10546 /* See if it names ctor, dtor or conv. */
10547 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10548 || IDENTIFIER_TYPENAME_P (unqualified_name)
10549 || constructor_name_p (unqualified_name, class_type)
10550 || (TREE_CODE (unqualified_name) == TYPE_DECL
10551 && same_type_p (TREE_TYPE (unqualified_name),
10552 class_type)))
10553 *ctor_dtor_or_conv_p = -1;
10556 handle_declarator:;
10557 scope = get_scope_of_declarator (declarator);
10558 if (scope)
10559 /* Any names that appear after the declarator-id for a
10560 member are looked up in the containing scope. */
10561 pop_p = push_scope (scope);
10562 parser->in_declarator_p = true;
10563 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10564 || (declarator
10565 && (TREE_CODE (declarator) == SCOPE_REF
10566 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10567 /* Default args are only allowed on function
10568 declarations. */
10569 parser->default_arg_ok_p = saved_default_arg_ok_p;
10570 else
10571 parser->default_arg_ok_p = false;
10573 first = false;
10575 /* We're done. */
10576 else
10577 break;
10580 /* For an abstract declarator, we might wind up with nothing at this
10581 point. That's an error; the declarator is not optional. */
10582 if (!declarator)
10583 cp_parser_error (parser, "expected declarator");
10585 /* If we entered a scope, we must exit it now. */
10586 if (pop_p)
10587 pop_scope (scope);
10589 parser->default_arg_ok_p = saved_default_arg_ok_p;
10590 parser->in_declarator_p = saved_in_declarator_p;
10592 return declarator;
10595 /* Parse a ptr-operator.
10597 ptr-operator:
10598 * cv-qualifier-seq [opt]
10600 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10602 GNU Extension:
10604 ptr-operator:
10605 & cv-qualifier-seq [opt]
10607 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10608 used. Returns ADDR_EXPR if a reference was used. In the
10609 case of a pointer-to-member, *TYPE is filled in with the
10610 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10611 with the cv-qualifier-seq, or NULL_TREE, if there are no
10612 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10614 static enum tree_code
10615 cp_parser_ptr_operator (cp_parser* parser,
10616 tree* type,
10617 tree* cv_qualifier_seq)
10619 enum tree_code code = ERROR_MARK;
10620 cp_token *token;
10622 /* Assume that it's not a pointer-to-member. */
10623 *type = NULL_TREE;
10624 /* And that there are no cv-qualifiers. */
10625 *cv_qualifier_seq = NULL_TREE;
10627 /* Peek at the next token. */
10628 token = cp_lexer_peek_token (parser->lexer);
10629 /* If it's a `*' or `&' we have a pointer or reference. */
10630 if (token->type == CPP_MULT || token->type == CPP_AND)
10632 /* Remember which ptr-operator we were processing. */
10633 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10635 /* Consume the `*' or `&'. */
10636 cp_lexer_consume_token (parser->lexer);
10638 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10639 `&', if we are allowing GNU extensions. (The only qualifier
10640 that can legally appear after `&' is `restrict', but that is
10641 enforced during semantic analysis. */
10642 if (code == INDIRECT_REF
10643 || cp_parser_allow_gnu_extensions_p (parser))
10644 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10646 else
10648 /* Try the pointer-to-member case. */
10649 cp_parser_parse_tentatively (parser);
10650 /* Look for the optional `::' operator. */
10651 cp_parser_global_scope_opt (parser,
10652 /*current_scope_valid_p=*/false);
10653 /* Look for the nested-name specifier. */
10654 cp_parser_nested_name_specifier (parser,
10655 /*typename_keyword_p=*/false,
10656 /*check_dependency_p=*/true,
10657 /*type_p=*/false,
10658 /*is_declaration=*/false);
10659 /* If we found it, and the next token is a `*', then we are
10660 indeed looking at a pointer-to-member operator. */
10661 if (!cp_parser_error_occurred (parser)
10662 && cp_parser_require (parser, CPP_MULT, "`*'"))
10664 /* The type of which the member is a member is given by the
10665 current SCOPE. */
10666 *type = parser->scope;
10667 /* The next name will not be qualified. */
10668 parser->scope = NULL_TREE;
10669 parser->qualifying_scope = NULL_TREE;
10670 parser->object_scope = NULL_TREE;
10671 /* Indicate that the `*' operator was used. */
10672 code = INDIRECT_REF;
10673 /* Look for the optional cv-qualifier-seq. */
10674 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10676 /* If that didn't work we don't have a ptr-operator. */
10677 if (!cp_parser_parse_definitely (parser))
10678 cp_parser_error (parser, "expected ptr-operator");
10681 return code;
10684 /* Parse an (optional) cv-qualifier-seq.
10686 cv-qualifier-seq:
10687 cv-qualifier cv-qualifier-seq [opt]
10689 Returns a TREE_LIST. The TREE_VALUE of each node is the
10690 representation of a cv-qualifier. */
10692 static tree
10693 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10695 tree cv_qualifiers = NULL_TREE;
10697 while (true)
10699 tree cv_qualifier;
10701 /* Look for the next cv-qualifier. */
10702 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10703 /* If we didn't find one, we're done. */
10704 if (!cv_qualifier)
10705 break;
10707 /* Add this cv-qualifier to the list. */
10708 cv_qualifiers
10709 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10712 /* We built up the list in reverse order. */
10713 return nreverse (cv_qualifiers);
10716 /* Parse an (optional) cv-qualifier.
10718 cv-qualifier:
10719 const
10720 volatile
10722 GNU Extension:
10724 cv-qualifier:
10725 __restrict__ */
10727 static tree
10728 cp_parser_cv_qualifier_opt (cp_parser* parser)
10730 cp_token *token;
10731 tree cv_qualifier = NULL_TREE;
10733 /* Peek at the next token. */
10734 token = cp_lexer_peek_token (parser->lexer);
10735 /* See if it's a cv-qualifier. */
10736 switch (token->keyword)
10738 case RID_CONST:
10739 case RID_VOLATILE:
10740 case RID_RESTRICT:
10741 /* Save the value of the token. */
10742 cv_qualifier = token->value;
10743 /* Consume the token. */
10744 cp_lexer_consume_token (parser->lexer);
10745 break;
10747 default:
10748 break;
10751 return cv_qualifier;
10754 /* Parse a declarator-id.
10756 declarator-id:
10757 id-expression
10758 :: [opt] nested-name-specifier [opt] type-name
10760 In the `id-expression' case, the value returned is as for
10761 cp_parser_id_expression if the id-expression was an unqualified-id.
10762 If the id-expression was a qualified-id, then a SCOPE_REF is
10763 returned. The first operand is the scope (either a NAMESPACE_DECL
10764 or TREE_TYPE), but the second is still just a representation of an
10765 unqualified-id. */
10767 static tree
10768 cp_parser_declarator_id (cp_parser* parser)
10770 tree id_expression;
10772 /* The expression must be an id-expression. Assume that qualified
10773 names are the names of types so that:
10775 template <class T>
10776 int S<T>::R::i = 3;
10778 will work; we must treat `S<T>::R' as the name of a type.
10779 Similarly, assume that qualified names are templates, where
10780 required, so that:
10782 template <class T>
10783 int S<T>::R<T>::i = 3;
10785 will work, too. */
10786 id_expression = cp_parser_id_expression (parser,
10787 /*template_keyword_p=*/false,
10788 /*check_dependency_p=*/false,
10789 /*template_p=*/NULL,
10790 /*declarator_p=*/true);
10791 /* If the name was qualified, create a SCOPE_REF to represent
10792 that. */
10793 if (parser->scope)
10795 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10796 parser->scope = NULL_TREE;
10799 return id_expression;
10802 /* Parse a type-id.
10804 type-id:
10805 type-specifier-seq abstract-declarator [opt]
10807 Returns the TYPE specified. */
10809 static tree
10810 cp_parser_type_id (cp_parser* parser)
10812 tree type_specifier_seq;
10813 tree abstract_declarator;
10815 /* Parse the type-specifier-seq. */
10816 type_specifier_seq
10817 = cp_parser_type_specifier_seq (parser);
10818 if (type_specifier_seq == error_mark_node)
10819 return error_mark_node;
10821 /* There might or might not be an abstract declarator. */
10822 cp_parser_parse_tentatively (parser);
10823 /* Look for the declarator. */
10824 abstract_declarator
10825 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10826 /*parenthesized_p=*/NULL);
10827 /* Check to see if there really was a declarator. */
10828 if (!cp_parser_parse_definitely (parser))
10829 abstract_declarator = NULL_TREE;
10831 return groktypename (build_tree_list (type_specifier_seq,
10832 abstract_declarator));
10835 /* Parse a type-specifier-seq.
10837 type-specifier-seq:
10838 type-specifier type-specifier-seq [opt]
10840 GNU extension:
10842 type-specifier-seq:
10843 attributes type-specifier-seq [opt]
10845 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10846 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10848 static tree
10849 cp_parser_type_specifier_seq (cp_parser* parser)
10851 bool seen_type_specifier = false;
10852 tree type_specifier_seq = NULL_TREE;
10854 /* Parse the type-specifiers and attributes. */
10855 while (true)
10857 tree type_specifier;
10859 /* Check for attributes first. */
10860 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10862 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10863 NULL_TREE,
10864 type_specifier_seq);
10865 continue;
10868 /* After the first type-specifier, others are optional. */
10869 if (seen_type_specifier)
10870 cp_parser_parse_tentatively (parser);
10871 /* Look for the type-specifier. */
10872 type_specifier = cp_parser_type_specifier (parser,
10873 CP_PARSER_FLAGS_NONE,
10874 /*is_friend=*/false,
10875 /*is_declaration=*/false,
10876 NULL,
10877 NULL);
10878 /* If the first type-specifier could not be found, this is not a
10879 type-specifier-seq at all. */
10880 if (!seen_type_specifier && type_specifier == error_mark_node)
10881 return error_mark_node;
10882 /* If subsequent type-specifiers could not be found, the
10883 type-specifier-seq is complete. */
10884 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10885 break;
10887 /* Add the new type-specifier to the list. */
10888 type_specifier_seq
10889 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10890 seen_type_specifier = true;
10893 /* We built up the list in reverse order. */
10894 return nreverse (type_specifier_seq);
10897 /* Parse a parameter-declaration-clause.
10899 parameter-declaration-clause:
10900 parameter-declaration-list [opt] ... [opt]
10901 parameter-declaration-list , ...
10903 Returns a representation for the parameter declarations. Each node
10904 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10905 representation.) If the parameter-declaration-clause ends with an
10906 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10907 list. A return value of NULL_TREE indicates a
10908 parameter-declaration-clause consisting only of an ellipsis. */
10910 static tree
10911 cp_parser_parameter_declaration_clause (cp_parser* parser)
10913 tree parameters;
10914 cp_token *token;
10915 bool ellipsis_p;
10917 /* Peek at the next token. */
10918 token = cp_lexer_peek_token (parser->lexer);
10919 /* Check for trivial parameter-declaration-clauses. */
10920 if (token->type == CPP_ELLIPSIS)
10922 /* Consume the `...' token. */
10923 cp_lexer_consume_token (parser->lexer);
10924 return NULL_TREE;
10926 else if (token->type == CPP_CLOSE_PAREN)
10927 /* There are no parameters. */
10929 #ifndef NO_IMPLICIT_EXTERN_C
10930 if (in_system_header && current_class_type == NULL
10931 && current_lang_name == lang_name_c)
10932 return NULL_TREE;
10933 else
10934 #endif
10935 return void_list_node;
10937 /* Check for `(void)', too, which is a special case. */
10938 else if (token->keyword == RID_VOID
10939 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10940 == CPP_CLOSE_PAREN))
10942 /* Consume the `void' token. */
10943 cp_lexer_consume_token (parser->lexer);
10944 /* There are no parameters. */
10945 return void_list_node;
10948 /* Parse the parameter-declaration-list. */
10949 parameters = cp_parser_parameter_declaration_list (parser);
10950 /* If a parse error occurred while parsing the
10951 parameter-declaration-list, then the entire
10952 parameter-declaration-clause is erroneous. */
10953 if (parameters == error_mark_node)
10954 return error_mark_node;
10956 /* Peek at the next token. */
10957 token = cp_lexer_peek_token (parser->lexer);
10958 /* If it's a `,', the clause should terminate with an ellipsis. */
10959 if (token->type == CPP_COMMA)
10961 /* Consume the `,'. */
10962 cp_lexer_consume_token (parser->lexer);
10963 /* Expect an ellipsis. */
10964 ellipsis_p
10965 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10967 /* It might also be `...' if the optional trailing `,' was
10968 omitted. */
10969 else if (token->type == CPP_ELLIPSIS)
10971 /* Consume the `...' token. */
10972 cp_lexer_consume_token (parser->lexer);
10973 /* And remember that we saw it. */
10974 ellipsis_p = true;
10976 else
10977 ellipsis_p = false;
10979 /* Finish the parameter list. */
10980 return finish_parmlist (parameters, ellipsis_p);
10983 /* Parse a parameter-declaration-list.
10985 parameter-declaration-list:
10986 parameter-declaration
10987 parameter-declaration-list , parameter-declaration
10989 Returns a representation of the parameter-declaration-list, as for
10990 cp_parser_parameter_declaration_clause. However, the
10991 `void_list_node' is never appended to the list. */
10993 static tree
10994 cp_parser_parameter_declaration_list (cp_parser* parser)
10996 tree parameters = NULL_TREE;
10998 /* Look for more parameters. */
10999 while (true)
11001 tree parameter;
11002 bool parenthesized_p;
11003 /* Parse the parameter. */
11004 parameter
11005 = cp_parser_parameter_declaration (parser,
11006 /*template_parm_p=*/false,
11007 &parenthesized_p);
11009 /* If a parse error occurred parsing the parameter declaration,
11010 then the entire parameter-declaration-list is erroneous. */
11011 if (parameter == error_mark_node)
11013 parameters = error_mark_node;
11014 break;
11016 /* Add the new parameter to the list. */
11017 TREE_CHAIN (parameter) = parameters;
11018 parameters = parameter;
11020 /* Peek at the next token. */
11021 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11022 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11023 /* The parameter-declaration-list is complete. */
11024 break;
11025 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11027 cp_token *token;
11029 /* Peek at the next token. */
11030 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11031 /* If it's an ellipsis, then the list is complete. */
11032 if (token->type == CPP_ELLIPSIS)
11033 break;
11034 /* Otherwise, there must be more parameters. Consume the
11035 `,'. */
11036 cp_lexer_consume_token (parser->lexer);
11037 /* When parsing something like:
11039 int i(float f, double d)
11041 we can tell after seeing the declaration for "f" that we
11042 are not looking at an initialization of a variable "i",
11043 but rather at the declaration of a function "i".
11045 Due to the fact that the parsing of template arguments
11046 (as specified to a template-id) requires backtracking we
11047 cannot use this technique when inside a template argument
11048 list. */
11049 if (!parser->in_template_argument_list_p
11050 && !parser->in_type_id_in_expr_p
11051 && cp_parser_parsing_tentatively (parser)
11052 && !cp_parser_committed_to_tentative_parse (parser)
11053 /* However, a parameter-declaration of the form
11054 "foat(f)" (which is a valid declaration of a
11055 parameter "f") can also be interpreted as an
11056 expression (the conversion of "f" to "float"). */
11057 && !parenthesized_p)
11058 cp_parser_commit_to_tentative_parse (parser);
11060 else
11062 cp_parser_error (parser, "expected `,' or `...'");
11063 if (!cp_parser_parsing_tentatively (parser)
11064 || cp_parser_committed_to_tentative_parse (parser))
11065 cp_parser_skip_to_closing_parenthesis (parser,
11066 /*recovering=*/true,
11067 /*or_comma=*/false,
11068 /*consume_paren=*/false);
11069 break;
11073 /* We built up the list in reverse order; straighten it out now. */
11074 return nreverse (parameters);
11077 /* Parse a parameter declaration.
11079 parameter-declaration:
11080 decl-specifier-seq declarator
11081 decl-specifier-seq declarator = assignment-expression
11082 decl-specifier-seq abstract-declarator [opt]
11083 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11085 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11086 declares a template parameter. (In that case, a non-nested `>'
11087 token encountered during the parsing of the assignment-expression
11088 is not interpreted as a greater-than operator.)
11090 Returns a TREE_LIST representing the parameter-declaration. The
11091 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11092 there is no default argument. The TREE_VALUE is a representation
11093 of the decl-specifier-seq and declarator. In particular, the
11094 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11095 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11096 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11097 the declarator is of the form "(p)". */
11099 static tree
11100 cp_parser_parameter_declaration (cp_parser *parser,
11101 bool template_parm_p,
11102 bool *parenthesized_p)
11104 int declares_class_or_enum;
11105 bool greater_than_is_operator_p;
11106 tree decl_specifiers;
11107 tree attributes;
11108 tree declarator;
11109 tree default_argument;
11110 tree parameter;
11111 cp_token *token;
11112 const char *saved_message;
11114 /* In a template parameter, `>' is not an operator.
11116 [temp.param]
11118 When parsing a default template-argument for a non-type
11119 template-parameter, the first non-nested `>' is taken as the end
11120 of the template parameter-list rather than a greater-than
11121 operator. */
11122 greater_than_is_operator_p = !template_parm_p;
11124 /* Type definitions may not appear in parameter types. */
11125 saved_message = parser->type_definition_forbidden_message;
11126 parser->type_definition_forbidden_message
11127 = "types may not be defined in parameter types";
11129 /* Parse the declaration-specifiers. */
11130 decl_specifiers
11131 = cp_parser_decl_specifier_seq (parser,
11132 CP_PARSER_FLAGS_NONE,
11133 &attributes,
11134 &declares_class_or_enum);
11135 /* If an error occurred, there's no reason to attempt to parse the
11136 rest of the declaration. */
11137 if (cp_parser_error_occurred (parser))
11139 parser->type_definition_forbidden_message = saved_message;
11140 return error_mark_node;
11143 /* Peek at the next token. */
11144 token = cp_lexer_peek_token (parser->lexer);
11145 /* If the next token is a `)', `,', `=', `>', or `...', then there
11146 is no declarator. */
11147 if (token->type == CPP_CLOSE_PAREN
11148 || token->type == CPP_COMMA
11149 || token->type == CPP_EQ
11150 || token->type == CPP_ELLIPSIS
11151 || token->type == CPP_GREATER)
11153 declarator = NULL_TREE;
11154 if (parenthesized_p)
11155 *parenthesized_p = false;
11157 /* Otherwise, there should be a declarator. */
11158 else
11160 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11161 parser->default_arg_ok_p = false;
11163 /* After seeing a decl-specifier-seq, if the next token is not a
11164 "(", there is no possibility that the code is a valid
11165 expression. Therefore, if parsing tentatively, we commit at
11166 this point. */
11167 if (!parser->in_template_argument_list_p
11168 /* In an expression context, having seen:
11170 (int((char ...
11172 we cannot be sure whether we are looking at a
11173 function-type (taking a "char" as a parameter) or a cast
11174 of some object of type "char" to "int". */
11175 && !parser->in_type_id_in_expr_p
11176 && cp_parser_parsing_tentatively (parser)
11177 && !cp_parser_committed_to_tentative_parse (parser)
11178 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11179 cp_parser_commit_to_tentative_parse (parser);
11180 /* Parse the declarator. */
11181 declarator = cp_parser_declarator (parser,
11182 CP_PARSER_DECLARATOR_EITHER,
11183 /*ctor_dtor_or_conv_p=*/NULL,
11184 parenthesized_p);
11185 parser->default_arg_ok_p = saved_default_arg_ok_p;
11186 /* After the declarator, allow more attributes. */
11187 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11190 /* The restriction on defining new types applies only to the type
11191 of the parameter, not to the default argument. */
11192 parser->type_definition_forbidden_message = saved_message;
11194 /* If the next token is `=', then process a default argument. */
11195 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11197 bool saved_greater_than_is_operator_p;
11198 /* Consume the `='. */
11199 cp_lexer_consume_token (parser->lexer);
11201 /* If we are defining a class, then the tokens that make up the
11202 default argument must be saved and processed later. */
11203 if (!template_parm_p && at_class_scope_p ()
11204 && TYPE_BEING_DEFINED (current_class_type))
11206 unsigned depth = 0;
11208 /* Create a DEFAULT_ARG to represented the unparsed default
11209 argument. */
11210 default_argument = make_node (DEFAULT_ARG);
11211 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11213 /* Add tokens until we have processed the entire default
11214 argument. */
11215 while (true)
11217 bool done = false;
11218 cp_token *token;
11220 /* Peek at the next token. */
11221 token = cp_lexer_peek_token (parser->lexer);
11222 /* What we do depends on what token we have. */
11223 switch (token->type)
11225 /* In valid code, a default argument must be
11226 immediately followed by a `,' `)', or `...'. */
11227 case CPP_COMMA:
11228 case CPP_CLOSE_PAREN:
11229 case CPP_ELLIPSIS:
11230 /* If we run into a non-nested `;', `}', or `]',
11231 then the code is invalid -- but the default
11232 argument is certainly over. */
11233 case CPP_SEMICOLON:
11234 case CPP_CLOSE_BRACE:
11235 case CPP_CLOSE_SQUARE:
11236 if (depth == 0)
11237 done = true;
11238 /* Update DEPTH, if necessary. */
11239 else if (token->type == CPP_CLOSE_PAREN
11240 || token->type == CPP_CLOSE_BRACE
11241 || token->type == CPP_CLOSE_SQUARE)
11242 --depth;
11243 break;
11245 case CPP_OPEN_PAREN:
11246 case CPP_OPEN_SQUARE:
11247 case CPP_OPEN_BRACE:
11248 ++depth;
11249 break;
11251 case CPP_GREATER:
11252 /* If we see a non-nested `>', and `>' is not an
11253 operator, then it marks the end of the default
11254 argument. */
11255 if (!depth && !greater_than_is_operator_p)
11256 done = true;
11257 break;
11259 /* If we run out of tokens, issue an error message. */
11260 case CPP_EOF:
11261 error ("file ends in default argument");
11262 done = true;
11263 break;
11265 case CPP_NAME:
11266 case CPP_SCOPE:
11267 /* In these cases, we should look for template-ids.
11268 For example, if the default argument is
11269 `X<int, double>()', we need to do name lookup to
11270 figure out whether or not `X' is a template; if
11271 so, the `,' does not end the default argument.
11273 That is not yet done. */
11274 break;
11276 default:
11277 break;
11280 /* If we've reached the end, stop. */
11281 if (done)
11282 break;
11284 /* Add the token to the token block. */
11285 token = cp_lexer_consume_token (parser->lexer);
11286 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11287 token);
11290 /* Outside of a class definition, we can just parse the
11291 assignment-expression. */
11292 else
11294 bool saved_local_variables_forbidden_p;
11296 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11297 set correctly. */
11298 saved_greater_than_is_operator_p
11299 = parser->greater_than_is_operator_p;
11300 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11301 /* Local variable names (and the `this' keyword) may not
11302 appear in a default argument. */
11303 saved_local_variables_forbidden_p
11304 = parser->local_variables_forbidden_p;
11305 parser->local_variables_forbidden_p = true;
11306 /* Parse the assignment-expression. */
11307 default_argument = cp_parser_assignment_expression (parser);
11308 /* Restore saved state. */
11309 parser->greater_than_is_operator_p
11310 = saved_greater_than_is_operator_p;
11311 parser->local_variables_forbidden_p
11312 = saved_local_variables_forbidden_p;
11314 if (!parser->default_arg_ok_p)
11316 if (!flag_pedantic_errors)
11317 warning ("deprecated use of default argument for parameter of non-function");
11318 else
11320 error ("default arguments are only permitted for function parameters");
11321 default_argument = NULL_TREE;
11325 else
11326 default_argument = NULL_TREE;
11328 /* Create the representation of the parameter. */
11329 if (attributes)
11330 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11331 parameter = build_tree_list (default_argument,
11332 build_tree_list (decl_specifiers,
11333 declarator));
11335 return parameter;
11338 /* Parse a function-body.
11340 function-body:
11341 compound_statement */
11343 static void
11344 cp_parser_function_body (cp_parser *parser)
11346 cp_parser_compound_statement (parser, false);
11349 /* Parse a ctor-initializer-opt followed by a function-body. Return
11350 true if a ctor-initializer was present. */
11352 static bool
11353 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11355 tree body;
11356 bool ctor_initializer_p;
11358 /* Begin the function body. */
11359 body = begin_function_body ();
11360 /* Parse the optional ctor-initializer. */
11361 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11362 /* Parse the function-body. */
11363 cp_parser_function_body (parser);
11364 /* Finish the function body. */
11365 finish_function_body (body);
11367 return ctor_initializer_p;
11370 /* Parse an initializer.
11372 initializer:
11373 = initializer-clause
11374 ( expression-list )
11376 Returns a expression representing the initializer. If no
11377 initializer is present, NULL_TREE is returned.
11379 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11380 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11381 set to FALSE if there is no initializer present. If there is an
11382 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11383 is set to true; otherwise it is set to false. */
11385 static tree
11386 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11387 bool* non_constant_p)
11389 cp_token *token;
11390 tree init;
11392 /* Peek at the next token. */
11393 token = cp_lexer_peek_token (parser->lexer);
11395 /* Let our caller know whether or not this initializer was
11396 parenthesized. */
11397 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11398 /* Assume that the initializer is constant. */
11399 *non_constant_p = false;
11401 if (token->type == CPP_EQ)
11403 /* Consume the `='. */
11404 cp_lexer_consume_token (parser->lexer);
11405 /* Parse the initializer-clause. */
11406 init = cp_parser_initializer_clause (parser, non_constant_p);
11408 else if (token->type == CPP_OPEN_PAREN)
11409 init = cp_parser_parenthesized_expression_list (parser, false,
11410 non_constant_p);
11411 else
11413 /* Anything else is an error. */
11414 cp_parser_error (parser, "expected initializer");
11415 init = error_mark_node;
11418 return init;
11421 /* Parse an initializer-clause.
11423 initializer-clause:
11424 assignment-expression
11425 { initializer-list , [opt] }
11428 Returns an expression representing the initializer.
11430 If the `assignment-expression' production is used the value
11431 returned is simply a representation for the expression.
11433 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11434 the elements of the initializer-list (or NULL_TREE, if the last
11435 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11436 NULL_TREE. There is no way to detect whether or not the optional
11437 trailing `,' was provided. NON_CONSTANT_P is as for
11438 cp_parser_initializer. */
11440 static tree
11441 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11443 tree initializer;
11445 /* If it is not a `{', then we are looking at an
11446 assignment-expression. */
11447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11449 initializer
11450 = cp_parser_constant_expression (parser,
11451 /*allow_non_constant_p=*/true,
11452 non_constant_p);
11453 if (!*non_constant_p)
11454 initializer = fold_non_dependent_expr (initializer);
11456 else
11458 /* Consume the `{' token. */
11459 cp_lexer_consume_token (parser->lexer);
11460 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11461 initializer = make_node (CONSTRUCTOR);
11462 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11463 necessary, but check_initializer depends upon it, for
11464 now. */
11465 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11466 /* If it's not a `}', then there is a non-trivial initializer. */
11467 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11469 /* Parse the initializer list. */
11470 CONSTRUCTOR_ELTS (initializer)
11471 = cp_parser_initializer_list (parser, non_constant_p);
11472 /* A trailing `,' token is allowed. */
11473 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11474 cp_lexer_consume_token (parser->lexer);
11476 /* Now, there should be a trailing `}'. */
11477 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11480 return initializer;
11483 /* Parse an initializer-list.
11485 initializer-list:
11486 initializer-clause
11487 initializer-list , initializer-clause
11489 GNU Extension:
11491 initializer-list:
11492 identifier : initializer-clause
11493 initializer-list, identifier : initializer-clause
11495 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11496 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11497 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11498 as for cp_parser_initializer. */
11500 static tree
11501 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11503 tree initializers = NULL_TREE;
11505 /* Assume all of the expressions are constant. */
11506 *non_constant_p = false;
11508 /* Parse the rest of the list. */
11509 while (true)
11511 cp_token *token;
11512 tree identifier;
11513 tree initializer;
11514 bool clause_non_constant_p;
11516 /* If the next token is an identifier and the following one is a
11517 colon, we are looking at the GNU designated-initializer
11518 syntax. */
11519 if (cp_parser_allow_gnu_extensions_p (parser)
11520 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11521 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11523 /* Consume the identifier. */
11524 identifier = cp_lexer_consume_token (parser->lexer)->value;
11525 /* Consume the `:'. */
11526 cp_lexer_consume_token (parser->lexer);
11528 else
11529 identifier = NULL_TREE;
11531 /* Parse the initializer. */
11532 initializer = cp_parser_initializer_clause (parser,
11533 &clause_non_constant_p);
11534 /* If any clause is non-constant, so is the entire initializer. */
11535 if (clause_non_constant_p)
11536 *non_constant_p = true;
11537 /* Add it to the list. */
11538 initializers = tree_cons (identifier, initializer, initializers);
11540 /* If the next token is not a comma, we have reached the end of
11541 the list. */
11542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11543 break;
11545 /* Peek at the next token. */
11546 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11547 /* If the next token is a `}', then we're still done. An
11548 initializer-clause can have a trailing `,' after the
11549 initializer-list and before the closing `}'. */
11550 if (token->type == CPP_CLOSE_BRACE)
11551 break;
11553 /* Consume the `,' token. */
11554 cp_lexer_consume_token (parser->lexer);
11557 /* The initializers were built up in reverse order, so we need to
11558 reverse them now. */
11559 return nreverse (initializers);
11562 /* Classes [gram.class] */
11564 /* Parse a class-name.
11566 class-name:
11567 identifier
11568 template-id
11570 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11571 to indicate that names looked up in dependent types should be
11572 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11573 keyword has been used to indicate that the name that appears next
11574 is a template. TYPE_P is true iff the next name should be treated
11575 as class-name, even if it is declared to be some other kind of name
11576 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11577 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11578 being defined in a class-head.
11580 Returns the TYPE_DECL representing the class. */
11582 static tree
11583 cp_parser_class_name (cp_parser *parser,
11584 bool typename_keyword_p,
11585 bool template_keyword_p,
11586 bool type_p,
11587 bool check_dependency_p,
11588 bool class_head_p,
11589 bool is_declaration)
11591 tree decl;
11592 tree scope;
11593 bool typename_p;
11594 cp_token *token;
11596 /* All class-names start with an identifier. */
11597 token = cp_lexer_peek_token (parser->lexer);
11598 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11600 cp_parser_error (parser, "expected class-name");
11601 return error_mark_node;
11604 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11605 to a template-id, so we save it here. */
11606 scope = parser->scope;
11607 if (scope == error_mark_node)
11608 return error_mark_node;
11610 /* Any name names a type if we're following the `typename' keyword
11611 in a qualified name where the enclosing scope is type-dependent. */
11612 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11613 && dependent_type_p (scope));
11614 /* Handle the common case (an identifier, but not a template-id)
11615 efficiently. */
11616 if (token->type == CPP_NAME
11617 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11619 tree identifier;
11621 /* Look for the identifier. */
11622 identifier = cp_parser_identifier (parser);
11623 /* If the next token isn't an identifier, we are certainly not
11624 looking at a class-name. */
11625 if (identifier == error_mark_node)
11626 decl = error_mark_node;
11627 /* If we know this is a type-name, there's no need to look it
11628 up. */
11629 else if (typename_p)
11630 decl = identifier;
11631 else
11633 /* If the next token is a `::', then the name must be a type
11634 name.
11636 [basic.lookup.qual]
11638 During the lookup for a name preceding the :: scope
11639 resolution operator, object, function, and enumerator
11640 names are ignored. */
11641 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11642 type_p = true;
11643 /* Look up the name. */
11644 decl = cp_parser_lookup_name (parser, identifier,
11645 type_p,
11646 /*is_template=*/false,
11647 /*is_namespace=*/false,
11648 check_dependency_p);
11651 else
11653 /* Try a template-id. */
11654 decl = cp_parser_template_id (parser, template_keyword_p,
11655 check_dependency_p,
11656 is_declaration);
11657 if (decl == error_mark_node)
11658 return error_mark_node;
11661 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11663 /* If this is a typename, create a TYPENAME_TYPE. */
11664 if (typename_p && decl != error_mark_node)
11666 decl = make_typename_type (scope, decl, /*complain=*/1);
11667 if (decl != error_mark_node)
11668 decl = TYPE_NAME (decl);
11671 /* Check to see that it is really the name of a class. */
11672 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11673 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11674 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11675 /* Situations like this:
11677 template <typename T> struct A {
11678 typename T::template X<int>::I i;
11681 are problematic. Is `T::template X<int>' a class-name? The
11682 standard does not seem to be definitive, but there is no other
11683 valid interpretation of the following `::'. Therefore, those
11684 names are considered class-names. */
11685 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11686 else if (decl == error_mark_node
11687 || TREE_CODE (decl) != TYPE_DECL
11688 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11690 cp_parser_error (parser, "expected class-name");
11691 return error_mark_node;
11694 return decl;
11697 /* Parse a class-specifier.
11699 class-specifier:
11700 class-head { member-specification [opt] }
11702 Returns the TREE_TYPE representing the class. */
11704 static tree
11705 cp_parser_class_specifier (cp_parser* parser)
11707 cp_token *token;
11708 tree type;
11709 tree attributes;
11710 int has_trailing_semicolon;
11711 bool nested_name_specifier_p;
11712 unsigned saved_num_template_parameter_lists;
11713 bool pop_p = false;
11715 push_deferring_access_checks (dk_no_deferred);
11717 /* Parse the class-head. */
11718 type = cp_parser_class_head (parser,
11719 &nested_name_specifier_p,
11720 &attributes);
11721 /* If the class-head was a semantic disaster, skip the entire body
11722 of the class. */
11723 if (!type)
11725 cp_parser_skip_to_end_of_block_or_statement (parser);
11726 pop_deferring_access_checks ();
11727 return error_mark_node;
11730 /* Look for the `{'. */
11731 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11733 pop_deferring_access_checks ();
11734 return error_mark_node;
11737 /* Issue an error message if type-definitions are forbidden here. */
11738 cp_parser_check_type_definition (parser);
11739 /* Remember that we are defining one more class. */
11740 ++parser->num_classes_being_defined;
11741 /* Inside the class, surrounding template-parameter-lists do not
11742 apply. */
11743 saved_num_template_parameter_lists
11744 = parser->num_template_parameter_lists;
11745 parser->num_template_parameter_lists = 0;
11747 /* Start the class. */
11748 if (nested_name_specifier_p)
11749 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11750 type = begin_class_definition (type);
11751 if (type == error_mark_node)
11752 /* If the type is erroneous, skip the entire body of the class. */
11753 cp_parser_skip_to_closing_brace (parser);
11754 else
11755 /* Parse the member-specification. */
11756 cp_parser_member_specification_opt (parser);
11757 /* Look for the trailing `}'. */
11758 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11759 /* We get better error messages by noticing a common problem: a
11760 missing trailing `;'. */
11761 token = cp_lexer_peek_token (parser->lexer);
11762 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11763 /* Look for trailing attributes to apply to this class. */
11764 if (cp_parser_allow_gnu_extensions_p (parser))
11766 tree sub_attr = cp_parser_attributes_opt (parser);
11767 attributes = chainon (attributes, sub_attr);
11769 if (type != error_mark_node)
11770 type = finish_struct (type, attributes);
11771 if (pop_p)
11772 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11773 /* If this class is not itself within the scope of another class,
11774 then we need to parse the bodies of all of the queued function
11775 definitions. Note that the queued functions defined in a class
11776 are not always processed immediately following the
11777 class-specifier for that class. Consider:
11779 struct A {
11780 struct B { void f() { sizeof (A); } };
11783 If `f' were processed before the processing of `A' were
11784 completed, there would be no way to compute the size of `A'.
11785 Note that the nesting we are interested in here is lexical --
11786 not the semantic nesting given by TYPE_CONTEXT. In particular,
11787 for:
11789 struct A { struct B; };
11790 struct A::B { void f() { } };
11792 there is no need to delay the parsing of `A::B::f'. */
11793 if (--parser->num_classes_being_defined == 0)
11795 tree queue_entry;
11796 tree fn;
11798 /* In a first pass, parse default arguments to the functions.
11799 Then, in a second pass, parse the bodies of the functions.
11800 This two-phased approach handles cases like:
11802 struct S {
11803 void f() { g(); }
11804 void g(int i = 3);
11808 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11809 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11810 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11811 TREE_PURPOSE (parser->unparsed_functions_queues)
11812 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11814 fn = TREE_VALUE (queue_entry);
11815 /* Make sure that any template parameters are in scope. */
11816 maybe_begin_member_template_processing (fn);
11817 /* If there are default arguments that have not yet been processed,
11818 take care of them now. */
11819 cp_parser_late_parsing_default_args (parser, fn);
11820 /* Remove any template parameters from the symbol table. */
11821 maybe_end_member_template_processing ();
11823 /* Now parse the body of the functions. */
11824 for (TREE_VALUE (parser->unparsed_functions_queues)
11825 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11826 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11827 TREE_VALUE (parser->unparsed_functions_queues)
11828 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11830 /* Figure out which function we need to process. */
11831 fn = TREE_VALUE (queue_entry);
11833 /* A hack to prevent garbage collection. */
11834 function_depth++;
11836 /* Parse the function. */
11837 cp_parser_late_parsing_for_member (parser, fn);
11838 function_depth--;
11843 /* Put back any saved access checks. */
11844 pop_deferring_access_checks ();
11846 /* Restore the count of active template-parameter-lists. */
11847 parser->num_template_parameter_lists
11848 = saved_num_template_parameter_lists;
11850 return type;
11853 /* Parse a class-head.
11855 class-head:
11856 class-key identifier [opt] base-clause [opt]
11857 class-key nested-name-specifier identifier base-clause [opt]
11858 class-key nested-name-specifier [opt] template-id
11859 base-clause [opt]
11861 GNU Extensions:
11862 class-key attributes identifier [opt] base-clause [opt]
11863 class-key attributes nested-name-specifier identifier base-clause [opt]
11864 class-key attributes nested-name-specifier [opt] template-id
11865 base-clause [opt]
11867 Returns the TYPE of the indicated class. Sets
11868 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11869 involving a nested-name-specifier was used, and FALSE otherwise.
11871 Returns NULL_TREE if the class-head is syntactically valid, but
11872 semantically invalid in a way that means we should skip the entire
11873 body of the class. */
11875 static tree
11876 cp_parser_class_head (cp_parser* parser,
11877 bool* nested_name_specifier_p,
11878 tree *attributes_p)
11880 cp_token *token;
11881 tree nested_name_specifier;
11882 enum tag_types class_key;
11883 tree id = NULL_TREE;
11884 tree type = NULL_TREE;
11885 tree attributes;
11886 bool template_id_p = false;
11887 bool qualified_p = false;
11888 bool invalid_nested_name_p = false;
11889 bool invalid_explicit_specialization_p = false;
11890 bool pop_p = false;
11891 unsigned num_templates;
11893 /* Assume no nested-name-specifier will be present. */
11894 *nested_name_specifier_p = false;
11895 /* Assume no template parameter lists will be used in defining the
11896 type. */
11897 num_templates = 0;
11899 /* Look for the class-key. */
11900 class_key = cp_parser_class_key (parser);
11901 if (class_key == none_type)
11902 return error_mark_node;
11904 /* Parse the attributes. */
11905 attributes = cp_parser_attributes_opt (parser);
11907 /* If the next token is `::', that is invalid -- but sometimes
11908 people do try to write:
11910 struct ::S {};
11912 Handle this gracefully by accepting the extra qualifier, and then
11913 issuing an error about it later if this really is a
11914 class-head. If it turns out just to be an elaborated type
11915 specifier, remain silent. */
11916 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11917 qualified_p = true;
11919 push_deferring_access_checks (dk_no_check);
11921 /* Determine the name of the class. Begin by looking for an
11922 optional nested-name-specifier. */
11923 nested_name_specifier
11924 = cp_parser_nested_name_specifier_opt (parser,
11925 /*typename_keyword_p=*/false,
11926 /*check_dependency_p=*/false,
11927 /*type_p=*/false,
11928 /*is_declaration=*/false);
11929 /* If there was a nested-name-specifier, then there *must* be an
11930 identifier. */
11931 if (nested_name_specifier)
11933 /* Although the grammar says `identifier', it really means
11934 `class-name' or `template-name'. You are only allowed to
11935 define a class that has already been declared with this
11936 syntax.
11938 The proposed resolution for Core Issue 180 says that whever
11939 you see `class T::X' you should treat `X' as a type-name.
11941 It is OK to define an inaccessible class; for example:
11943 class A { class B; };
11944 class A::B {};
11946 We do not know if we will see a class-name, or a
11947 template-name. We look for a class-name first, in case the
11948 class-name is a template-id; if we looked for the
11949 template-name first we would stop after the template-name. */
11950 cp_parser_parse_tentatively (parser);
11951 type = cp_parser_class_name (parser,
11952 /*typename_keyword_p=*/false,
11953 /*template_keyword_p=*/false,
11954 /*type_p=*/true,
11955 /*check_dependency_p=*/false,
11956 /*class_head_p=*/true,
11957 /*is_declaration=*/false);
11958 /* If that didn't work, ignore the nested-name-specifier. */
11959 if (!cp_parser_parse_definitely (parser))
11961 invalid_nested_name_p = true;
11962 id = cp_parser_identifier (parser);
11963 if (id == error_mark_node)
11964 id = NULL_TREE;
11966 /* If we could not find a corresponding TYPE, treat this
11967 declaration like an unqualified declaration. */
11968 if (type == error_mark_node)
11969 nested_name_specifier = NULL_TREE;
11970 /* Otherwise, count the number of templates used in TYPE and its
11971 containing scopes. */
11972 else
11974 tree scope;
11976 for (scope = TREE_TYPE (type);
11977 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11978 scope = (TYPE_P (scope)
11979 ? TYPE_CONTEXT (scope)
11980 : DECL_CONTEXT (scope)))
11981 if (TYPE_P (scope)
11982 && CLASS_TYPE_P (scope)
11983 && CLASSTYPE_TEMPLATE_INFO (scope)
11984 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11985 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11986 ++num_templates;
11989 /* Otherwise, the identifier is optional. */
11990 else
11992 /* We don't know whether what comes next is a template-id,
11993 an identifier, or nothing at all. */
11994 cp_parser_parse_tentatively (parser);
11995 /* Check for a template-id. */
11996 id = cp_parser_template_id (parser,
11997 /*template_keyword_p=*/false,
11998 /*check_dependency_p=*/true,
11999 /*is_declaration=*/true);
12000 /* If that didn't work, it could still be an identifier. */
12001 if (!cp_parser_parse_definitely (parser))
12003 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12004 id = cp_parser_identifier (parser);
12005 else
12006 id = NULL_TREE;
12008 else
12010 template_id_p = true;
12011 ++num_templates;
12015 pop_deferring_access_checks ();
12017 if (id)
12018 cp_parser_check_for_invalid_template_id (parser, id);
12020 /* If it's not a `:' or a `{' then we can't really be looking at a
12021 class-head, since a class-head only appears as part of a
12022 class-specifier. We have to detect this situation before calling
12023 xref_tag, since that has irreversible side-effects. */
12024 if (!cp_parser_next_token_starts_class_definition_p (parser))
12026 cp_parser_error (parser, "expected `{' or `:'");
12027 return error_mark_node;
12030 /* At this point, we're going ahead with the class-specifier, even
12031 if some other problem occurs. */
12032 cp_parser_commit_to_tentative_parse (parser);
12033 /* Issue the error about the overly-qualified name now. */
12034 if (qualified_p)
12035 cp_parser_error (parser,
12036 "global qualification of class name is invalid");
12037 else if (invalid_nested_name_p)
12038 cp_parser_error (parser,
12039 "qualified name does not name a class");
12040 else if (nested_name_specifier)
12042 tree scope;
12043 /* Figure out in what scope the declaration is being placed. */
12044 scope = current_scope ();
12045 if (!scope)
12046 scope = current_namespace;
12047 /* If that scope does not contain the scope in which the
12048 class was originally declared, the program is invalid. */
12049 if (scope && !is_ancestor (scope, nested_name_specifier))
12051 error ("declaration of `%D' in `%D' which does not "
12052 "enclose `%D'", type, scope, nested_name_specifier);
12053 type = NULL_TREE;
12054 goto done;
12056 /* [dcl.meaning]
12058 A declarator-id shall not be qualified exception of the
12059 definition of a ... nested class outside of its class
12060 ... [or] a the definition or explicit instantiation of a
12061 class member of a namespace outside of its namespace. */
12062 if (scope == nested_name_specifier)
12064 pedwarn ("extra qualification ignored");
12065 nested_name_specifier = NULL_TREE;
12066 num_templates = 0;
12069 /* An explicit-specialization must be preceded by "template <>". If
12070 it is not, try to recover gracefully. */
12071 if (at_namespace_scope_p ()
12072 && parser->num_template_parameter_lists == 0
12073 && template_id_p)
12075 error ("an explicit specialization must be preceded by 'template <>'");
12076 invalid_explicit_specialization_p = true;
12077 /* Take the same action that would have been taken by
12078 cp_parser_explicit_specialization. */
12079 ++parser->num_template_parameter_lists;
12080 begin_specialization ();
12082 /* There must be no "return" statements between this point and the
12083 end of this function; set "type "to the correct return value and
12084 use "goto done;" to return. */
12085 /* Make sure that the right number of template parameters were
12086 present. */
12087 if (!cp_parser_check_template_parameters (parser, num_templates))
12089 /* If something went wrong, there is no point in even trying to
12090 process the class-definition. */
12091 type = NULL_TREE;
12092 goto done;
12095 /* Look up the type. */
12096 if (template_id_p)
12098 type = TREE_TYPE (id);
12099 maybe_process_partial_specialization (type);
12101 else if (!nested_name_specifier)
12103 /* If the class was unnamed, create a dummy name. */
12104 if (!id)
12105 id = make_anon_name ();
12106 type = xref_tag (class_key, id, /*globalize=*/false,
12107 parser->num_template_parameter_lists);
12109 else
12111 tree class_type;
12112 bool pop_p = false;
12114 /* Given:
12116 template <typename T> struct S { struct T };
12117 template <typename T> struct S<T>::T { };
12119 we will get a TYPENAME_TYPE when processing the definition of
12120 `S::T'. We need to resolve it to the actual type before we
12121 try to define it. */
12122 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12124 class_type = resolve_typename_type (TREE_TYPE (type),
12125 /*only_current_p=*/false);
12126 if (class_type != error_mark_node)
12127 type = TYPE_NAME (class_type);
12128 else
12130 cp_parser_error (parser, "could not resolve typename type");
12131 type = error_mark_node;
12135 maybe_process_partial_specialization (TREE_TYPE (type));
12136 class_type = current_class_type;
12137 /* Enter the scope indicated by the nested-name-specifier. */
12138 if (nested_name_specifier)
12139 pop_p = push_scope (nested_name_specifier);
12140 /* Get the canonical version of this type. */
12141 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12142 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12143 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12144 type = push_template_decl (type);
12145 type = TREE_TYPE (type);
12146 if (nested_name_specifier)
12148 *nested_name_specifier_p = true;
12149 if (pop_p)
12150 pop_scope (nested_name_specifier);
12153 /* Indicate whether this class was declared as a `class' or as a
12154 `struct'. */
12155 if (TREE_CODE (type) == RECORD_TYPE)
12156 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12157 cp_parser_check_class_key (class_key, type);
12159 /* Enter the scope containing the class; the names of base classes
12160 should be looked up in that context. For example, given:
12162 struct A { struct B {}; struct C; };
12163 struct A::C : B {};
12165 is valid. */
12166 if (nested_name_specifier)
12167 pop_p = push_scope (nested_name_specifier);
12168 /* Now, look for the base-clause. */
12169 token = cp_lexer_peek_token (parser->lexer);
12170 if (token->type == CPP_COLON)
12172 tree bases;
12174 /* Get the list of base-classes. */
12175 bases = cp_parser_base_clause (parser);
12176 /* Process them. */
12177 xref_basetypes (type, bases);
12179 /* Leave the scope given by the nested-name-specifier. We will
12180 enter the class scope itself while processing the members. */
12181 if (pop_p)
12182 pop_scope (nested_name_specifier);
12184 done:
12185 if (invalid_explicit_specialization_p)
12187 end_specialization ();
12188 --parser->num_template_parameter_lists;
12190 *attributes_p = attributes;
12191 return type;
12194 /* Parse a class-key.
12196 class-key:
12197 class
12198 struct
12199 union
12201 Returns the kind of class-key specified, or none_type to indicate
12202 error. */
12204 static enum tag_types
12205 cp_parser_class_key (cp_parser* parser)
12207 cp_token *token;
12208 enum tag_types tag_type;
12210 /* Look for the class-key. */
12211 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12212 if (!token)
12213 return none_type;
12215 /* Check to see if the TOKEN is a class-key. */
12216 tag_type = cp_parser_token_is_class_key (token);
12217 if (!tag_type)
12218 cp_parser_error (parser, "expected class-key");
12219 return tag_type;
12222 /* Parse an (optional) member-specification.
12224 member-specification:
12225 member-declaration member-specification [opt]
12226 access-specifier : member-specification [opt] */
12228 static void
12229 cp_parser_member_specification_opt (cp_parser* parser)
12231 while (true)
12233 cp_token *token;
12234 enum rid keyword;
12236 /* Peek at the next token. */
12237 token = cp_lexer_peek_token (parser->lexer);
12238 /* If it's a `}', or EOF then we've seen all the members. */
12239 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12240 break;
12242 /* See if this token is a keyword. */
12243 keyword = token->keyword;
12244 switch (keyword)
12246 case RID_PUBLIC:
12247 case RID_PROTECTED:
12248 case RID_PRIVATE:
12249 /* Consume the access-specifier. */
12250 cp_lexer_consume_token (parser->lexer);
12251 /* Remember which access-specifier is active. */
12252 current_access_specifier = token->value;
12253 /* Look for the `:'. */
12254 cp_parser_require (parser, CPP_COLON, "`:'");
12255 break;
12257 default:
12258 /* Otherwise, the next construction must be a
12259 member-declaration. */
12260 cp_parser_member_declaration (parser);
12265 /* Parse a member-declaration.
12267 member-declaration:
12268 decl-specifier-seq [opt] member-declarator-list [opt] ;
12269 function-definition ; [opt]
12270 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12271 using-declaration
12272 template-declaration
12274 member-declarator-list:
12275 member-declarator
12276 member-declarator-list , member-declarator
12278 member-declarator:
12279 declarator pure-specifier [opt]
12280 declarator constant-initializer [opt]
12281 identifier [opt] : constant-expression
12283 GNU Extensions:
12285 member-declaration:
12286 __extension__ member-declaration
12288 member-declarator:
12289 declarator attributes [opt] pure-specifier [opt]
12290 declarator attributes [opt] constant-initializer [opt]
12291 identifier [opt] attributes [opt] : constant-expression */
12293 static void
12294 cp_parser_member_declaration (cp_parser* parser)
12296 tree decl_specifiers;
12297 tree prefix_attributes;
12298 tree decl;
12299 int declares_class_or_enum;
12300 bool friend_p;
12301 cp_token *token;
12302 int saved_pedantic;
12304 /* Check for the `__extension__' keyword. */
12305 if (cp_parser_extension_opt (parser, &saved_pedantic))
12307 /* Recurse. */
12308 cp_parser_member_declaration (parser);
12309 /* Restore the old value of the PEDANTIC flag. */
12310 pedantic = saved_pedantic;
12312 return;
12315 /* Check for a template-declaration. */
12316 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12318 /* Parse the template-declaration. */
12319 cp_parser_template_declaration (parser, /*member_p=*/true);
12321 return;
12324 /* Check for a using-declaration. */
12325 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12327 /* Parse the using-declaration. */
12328 cp_parser_using_declaration (parser);
12330 return;
12333 /* Parse the decl-specifier-seq. */
12334 decl_specifiers
12335 = cp_parser_decl_specifier_seq (parser,
12336 CP_PARSER_FLAGS_OPTIONAL,
12337 &prefix_attributes,
12338 &declares_class_or_enum);
12339 /* Check for an invalid type-name. */
12340 if (cp_parser_diagnose_invalid_type_name (parser))
12341 return;
12342 /* If there is no declarator, then the decl-specifier-seq should
12343 specify a type. */
12344 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12346 /* If there was no decl-specifier-seq, and the next token is a
12347 `;', then we have something like:
12349 struct S { ; };
12351 [class.mem]
12353 Each member-declaration shall declare at least one member
12354 name of the class. */
12355 if (!decl_specifiers)
12357 if (pedantic)
12358 pedwarn ("extra semicolon");
12360 else
12362 tree type;
12364 /* See if this declaration is a friend. */
12365 friend_p = cp_parser_friend_p (decl_specifiers);
12366 /* If there were decl-specifiers, check to see if there was
12367 a class-declaration. */
12368 type = check_tag_decl (decl_specifiers);
12369 /* Nested classes have already been added to the class, but
12370 a `friend' needs to be explicitly registered. */
12371 if (friend_p)
12373 /* If the `friend' keyword was present, the friend must
12374 be introduced with a class-key. */
12375 if (!declares_class_or_enum)
12376 error ("a class-key must be used when declaring a friend");
12377 /* In this case:
12379 template <typename T> struct A {
12380 friend struct A<T>::B;
12383 A<T>::B will be represented by a TYPENAME_TYPE, and
12384 therefore not recognized by check_tag_decl. */
12385 if (!type)
12387 tree specifier;
12389 for (specifier = decl_specifiers;
12390 specifier;
12391 specifier = TREE_CHAIN (specifier))
12393 tree s = TREE_VALUE (specifier);
12395 if (TREE_CODE (s) == IDENTIFIER_NODE)
12396 get_global_value_if_present (s, &type);
12397 if (TREE_CODE (s) == TYPE_DECL)
12398 s = TREE_TYPE (s);
12399 if (TYPE_P (s))
12401 type = s;
12402 break;
12406 if (!type || !TYPE_P (type))
12407 error ("friend declaration does not name a class or "
12408 "function");
12409 else
12410 make_friend_class (current_class_type, type,
12411 /*complain=*/true);
12413 /* If there is no TYPE, an error message will already have
12414 been issued. */
12415 else if (!type)
12417 /* An anonymous aggregate has to be handled specially; such
12418 a declaration really declares a data member (with a
12419 particular type), as opposed to a nested class. */
12420 else if (ANON_AGGR_TYPE_P (type))
12422 /* Remove constructors and such from TYPE, now that we
12423 know it is an anonymous aggregate. */
12424 fixup_anonymous_aggr (type);
12425 /* And make the corresponding data member. */
12426 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12427 /* Add it to the class. */
12428 finish_member_declaration (decl);
12430 else
12431 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12434 else
12436 /* See if these declarations will be friends. */
12437 friend_p = cp_parser_friend_p (decl_specifiers);
12439 /* Keep going until we hit the `;' at the end of the
12440 declaration. */
12441 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12443 tree attributes = NULL_TREE;
12444 tree first_attribute;
12446 /* Peek at the next token. */
12447 token = cp_lexer_peek_token (parser->lexer);
12449 /* Check for a bitfield declaration. */
12450 if (token->type == CPP_COLON
12451 || (token->type == CPP_NAME
12452 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12453 == CPP_COLON))
12455 tree identifier;
12456 tree width;
12458 /* Get the name of the bitfield. Note that we cannot just
12459 check TOKEN here because it may have been invalidated by
12460 the call to cp_lexer_peek_nth_token above. */
12461 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12462 identifier = cp_parser_identifier (parser);
12463 else
12464 identifier = NULL_TREE;
12466 /* Consume the `:' token. */
12467 cp_lexer_consume_token (parser->lexer);
12468 /* Get the width of the bitfield. */
12469 width
12470 = cp_parser_constant_expression (parser,
12471 /*allow_non_constant=*/false,
12472 NULL);
12474 /* Look for attributes that apply to the bitfield. */
12475 attributes = cp_parser_attributes_opt (parser);
12476 /* Remember which attributes are prefix attributes and
12477 which are not. */
12478 first_attribute = attributes;
12479 /* Combine the attributes. */
12480 attributes = chainon (prefix_attributes, attributes);
12482 /* Create the bitfield declaration. */
12483 decl = grokbitfield (identifier,
12484 decl_specifiers,
12485 width);
12486 /* Apply the attributes. */
12487 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12489 else
12491 tree declarator;
12492 tree initializer;
12493 tree asm_specification;
12494 int ctor_dtor_or_conv_p;
12496 /* Parse the declarator. */
12497 declarator
12498 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12499 &ctor_dtor_or_conv_p,
12500 /*parenthesized_p=*/NULL);
12502 /* If something went wrong parsing the declarator, make sure
12503 that we at least consume some tokens. */
12504 if (declarator == error_mark_node)
12506 /* Skip to the end of the statement. */
12507 cp_parser_skip_to_end_of_statement (parser);
12508 /* If the next token is not a semicolon, that is
12509 probably because we just skipped over the body of
12510 a function. So, we consume a semicolon if
12511 present, but do not issue an error message if it
12512 is not present. */
12513 if (cp_lexer_next_token_is (parser->lexer,
12514 CPP_SEMICOLON))
12515 cp_lexer_consume_token (parser->lexer);
12516 return;
12519 cp_parser_check_for_definition_in_return_type
12520 (declarator, declares_class_or_enum);
12522 /* Look for an asm-specification. */
12523 asm_specification = cp_parser_asm_specification_opt (parser);
12524 /* Look for attributes that apply to the declaration. */
12525 attributes = cp_parser_attributes_opt (parser);
12526 /* Remember which attributes are prefix attributes and
12527 which are not. */
12528 first_attribute = attributes;
12529 /* Combine the attributes. */
12530 attributes = chainon (prefix_attributes, attributes);
12532 /* If it's an `=', then we have a constant-initializer or a
12533 pure-specifier. It is not correct to parse the
12534 initializer before registering the member declaration
12535 since the member declaration should be in scope while
12536 its initializer is processed. However, the rest of the
12537 front end does not yet provide an interface that allows
12538 us to handle this correctly. */
12539 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12541 /* In [class.mem]:
12543 A pure-specifier shall be used only in the declaration of
12544 a virtual function.
12546 A member-declarator can contain a constant-initializer
12547 only if it declares a static member of integral or
12548 enumeration type.
12550 Therefore, if the DECLARATOR is for a function, we look
12551 for a pure-specifier; otherwise, we look for a
12552 constant-initializer. When we call `grokfield', it will
12553 perform more stringent semantics checks. */
12554 if (TREE_CODE (declarator) == CALL_EXPR)
12555 initializer = cp_parser_pure_specifier (parser);
12556 else
12557 /* Parse the initializer. */
12558 initializer = cp_parser_constant_initializer (parser);
12560 /* Otherwise, there is no initializer. */
12561 else
12562 initializer = NULL_TREE;
12564 /* See if we are probably looking at a function
12565 definition. We are certainly not looking at at a
12566 member-declarator. Calling `grokfield' has
12567 side-effects, so we must not do it unless we are sure
12568 that we are looking at a member-declarator. */
12569 if (cp_parser_token_starts_function_definition_p
12570 (cp_lexer_peek_token (parser->lexer)))
12572 /* The grammar does not allow a pure-specifier to be
12573 used when a member function is defined. (It is
12574 possible that this fact is an oversight in the
12575 standard, since a pure function may be defined
12576 outside of the class-specifier. */
12577 if (initializer)
12578 error ("pure-specifier on function-definition");
12579 decl = cp_parser_save_member_function_body (parser,
12580 decl_specifiers,
12581 declarator,
12582 attributes);
12583 /* If the member was not a friend, declare it here. */
12584 if (!friend_p)
12585 finish_member_declaration (decl);
12586 /* Peek at the next token. */
12587 token = cp_lexer_peek_token (parser->lexer);
12588 /* If the next token is a semicolon, consume it. */
12589 if (token->type == CPP_SEMICOLON)
12590 cp_lexer_consume_token (parser->lexer);
12591 return;
12593 else
12595 /* Create the declaration. */
12596 decl = grokfield (declarator, decl_specifiers,
12597 initializer, asm_specification,
12598 attributes);
12599 /* Any initialization must have been from a
12600 constant-expression. */
12601 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12602 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12606 /* Reset PREFIX_ATTRIBUTES. */
12607 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12608 attributes = TREE_CHAIN (attributes);
12609 if (attributes)
12610 TREE_CHAIN (attributes) = NULL_TREE;
12612 /* If there is any qualification still in effect, clear it
12613 now; we will be starting fresh with the next declarator. */
12614 parser->scope = NULL_TREE;
12615 parser->qualifying_scope = NULL_TREE;
12616 parser->object_scope = NULL_TREE;
12617 /* If it's a `,', then there are more declarators. */
12618 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12619 cp_lexer_consume_token (parser->lexer);
12620 /* If the next token isn't a `;', then we have a parse error. */
12621 else if (cp_lexer_next_token_is_not (parser->lexer,
12622 CPP_SEMICOLON))
12624 cp_parser_error (parser, "expected `;'");
12625 /* Skip tokens until we find a `;'. */
12626 cp_parser_skip_to_end_of_statement (parser);
12628 break;
12631 if (decl)
12633 /* Add DECL to the list of members. */
12634 if (!friend_p)
12635 finish_member_declaration (decl);
12637 if (TREE_CODE (decl) == FUNCTION_DECL)
12638 cp_parser_save_default_args (parser, decl);
12643 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12646 /* Parse a pure-specifier.
12648 pure-specifier:
12651 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12652 Otherwise, ERROR_MARK_NODE is returned. */
12654 static tree
12655 cp_parser_pure_specifier (cp_parser* parser)
12657 cp_token *token;
12659 /* Look for the `=' token. */
12660 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12661 return error_mark_node;
12662 /* Look for the `0' token. */
12663 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12664 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12665 to get information from the lexer about how the number was
12666 spelled in order to fix this problem. */
12667 if (!token || !integer_zerop (token->value))
12668 return error_mark_node;
12670 return integer_zero_node;
12673 /* Parse a constant-initializer.
12675 constant-initializer:
12676 = constant-expression
12678 Returns a representation of the constant-expression. */
12680 static tree
12681 cp_parser_constant_initializer (cp_parser* parser)
12683 /* Look for the `=' token. */
12684 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12685 return error_mark_node;
12687 /* It is invalid to write:
12689 struct S { static const int i = { 7 }; };
12692 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12694 cp_parser_error (parser,
12695 "a brace-enclosed initializer is not allowed here");
12696 /* Consume the opening brace. */
12697 cp_lexer_consume_token (parser->lexer);
12698 /* Skip the initializer. */
12699 cp_parser_skip_to_closing_brace (parser);
12700 /* Look for the trailing `}'. */
12701 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12703 return error_mark_node;
12706 return cp_parser_constant_expression (parser,
12707 /*allow_non_constant=*/false,
12708 NULL);
12711 /* Derived classes [gram.class.derived] */
12713 /* Parse a base-clause.
12715 base-clause:
12716 : base-specifier-list
12718 base-specifier-list:
12719 base-specifier
12720 base-specifier-list , base-specifier
12722 Returns a TREE_LIST representing the base-classes, in the order in
12723 which they were declared. The representation of each node is as
12724 described by cp_parser_base_specifier.
12726 In the case that no bases are specified, this function will return
12727 NULL_TREE, not ERROR_MARK_NODE. */
12729 static tree
12730 cp_parser_base_clause (cp_parser* parser)
12732 tree bases = NULL_TREE;
12734 /* Look for the `:' that begins the list. */
12735 cp_parser_require (parser, CPP_COLON, "`:'");
12737 /* Scan the base-specifier-list. */
12738 while (true)
12740 cp_token *token;
12741 tree base;
12743 /* Look for the base-specifier. */
12744 base = cp_parser_base_specifier (parser);
12745 /* Add BASE to the front of the list. */
12746 if (base != error_mark_node)
12748 TREE_CHAIN (base) = bases;
12749 bases = base;
12751 /* Peek at the next token. */
12752 token = cp_lexer_peek_token (parser->lexer);
12753 /* If it's not a comma, then the list is complete. */
12754 if (token->type != CPP_COMMA)
12755 break;
12756 /* Consume the `,'. */
12757 cp_lexer_consume_token (parser->lexer);
12760 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12761 base class had a qualified name. However, the next name that
12762 appears is certainly not qualified. */
12763 parser->scope = NULL_TREE;
12764 parser->qualifying_scope = NULL_TREE;
12765 parser->object_scope = NULL_TREE;
12767 return nreverse (bases);
12770 /* Parse a base-specifier.
12772 base-specifier:
12773 :: [opt] nested-name-specifier [opt] class-name
12774 virtual access-specifier [opt] :: [opt] nested-name-specifier
12775 [opt] class-name
12776 access-specifier virtual [opt] :: [opt] nested-name-specifier
12777 [opt] class-name
12779 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12780 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12781 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12782 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12784 static tree
12785 cp_parser_base_specifier (cp_parser* parser)
12787 cp_token *token;
12788 bool done = false;
12789 bool virtual_p = false;
12790 bool duplicate_virtual_error_issued_p = false;
12791 bool duplicate_access_error_issued_p = false;
12792 bool class_scope_p, template_p;
12793 tree access = access_default_node;
12794 tree type;
12796 /* Process the optional `virtual' and `access-specifier'. */
12797 while (!done)
12799 /* Peek at the next token. */
12800 token = cp_lexer_peek_token (parser->lexer);
12801 /* Process `virtual'. */
12802 switch (token->keyword)
12804 case RID_VIRTUAL:
12805 /* If `virtual' appears more than once, issue an error. */
12806 if (virtual_p && !duplicate_virtual_error_issued_p)
12808 cp_parser_error (parser,
12809 "`virtual' specified more than once in base-specified");
12810 duplicate_virtual_error_issued_p = true;
12813 virtual_p = true;
12815 /* Consume the `virtual' token. */
12816 cp_lexer_consume_token (parser->lexer);
12818 break;
12820 case RID_PUBLIC:
12821 case RID_PROTECTED:
12822 case RID_PRIVATE:
12823 /* If more than one access specifier appears, issue an
12824 error. */
12825 if (access != access_default_node
12826 && !duplicate_access_error_issued_p)
12828 cp_parser_error (parser,
12829 "more than one access specifier in base-specified");
12830 duplicate_access_error_issued_p = true;
12833 access = ridpointers[(int) token->keyword];
12835 /* Consume the access-specifier. */
12836 cp_lexer_consume_token (parser->lexer);
12838 break;
12840 default:
12841 done = true;
12842 break;
12845 /* It is not uncommon to see programs mechanically, errouneously, use
12846 the 'typename' keyword to denote (dependent) qualified types
12847 as base classes. */
12848 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12850 if (!processing_template_decl)
12851 error ("keyword `typename' not allowed outside of templates");
12852 else
12853 error ("keyword `typename' not allowed in this context "
12854 "(the base class is implicitly a type)");
12855 cp_lexer_consume_token (parser->lexer);
12858 /* Look for the optional `::' operator. */
12859 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12860 /* Look for the nested-name-specifier. The simplest way to
12861 implement:
12863 [temp.res]
12865 The keyword `typename' is not permitted in a base-specifier or
12866 mem-initializer; in these contexts a qualified name that
12867 depends on a template-parameter is implicitly assumed to be a
12868 type name.
12870 is to pretend that we have seen the `typename' keyword at this
12871 point. */
12872 cp_parser_nested_name_specifier_opt (parser,
12873 /*typename_keyword_p=*/true,
12874 /*check_dependency_p=*/true,
12875 /*type_p=*/true,
12876 /*is_declaration=*/true);
12877 /* If the base class is given by a qualified name, assume that names
12878 we see are type names or templates, as appropriate. */
12879 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12880 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12882 /* Finally, look for the class-name. */
12883 type = cp_parser_class_name (parser,
12884 class_scope_p,
12885 template_p,
12886 /*type_p=*/true,
12887 /*check_dependency_p=*/true,
12888 /*class_head_p=*/false,
12889 /*is_declaration=*/true);
12891 if (type == error_mark_node)
12892 return error_mark_node;
12894 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12897 /* Exception handling [gram.exception] */
12899 /* Parse an (optional) exception-specification.
12901 exception-specification:
12902 throw ( type-id-list [opt] )
12904 Returns a TREE_LIST representing the exception-specification. The
12905 TREE_VALUE of each node is a type. */
12907 static tree
12908 cp_parser_exception_specification_opt (cp_parser* parser)
12910 cp_token *token;
12911 tree type_id_list;
12913 /* Peek at the next token. */
12914 token = cp_lexer_peek_token (parser->lexer);
12915 /* If it's not `throw', then there's no exception-specification. */
12916 if (!cp_parser_is_keyword (token, RID_THROW))
12917 return NULL_TREE;
12919 /* Consume the `throw'. */
12920 cp_lexer_consume_token (parser->lexer);
12922 /* Look for the `('. */
12923 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12925 /* Peek at the next token. */
12926 token = cp_lexer_peek_token (parser->lexer);
12927 /* If it's not a `)', then there is a type-id-list. */
12928 if (token->type != CPP_CLOSE_PAREN)
12930 const char *saved_message;
12932 /* Types may not be defined in an exception-specification. */
12933 saved_message = parser->type_definition_forbidden_message;
12934 parser->type_definition_forbidden_message
12935 = "types may not be defined in an exception-specification";
12936 /* Parse the type-id-list. */
12937 type_id_list = cp_parser_type_id_list (parser);
12938 /* Restore the saved message. */
12939 parser->type_definition_forbidden_message = saved_message;
12941 else
12942 type_id_list = empty_except_spec;
12944 /* Look for the `)'. */
12945 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12947 return type_id_list;
12950 /* Parse an (optional) type-id-list.
12952 type-id-list:
12953 type-id
12954 type-id-list , type-id
12956 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12957 in the order that the types were presented. */
12959 static tree
12960 cp_parser_type_id_list (cp_parser* parser)
12962 tree types = NULL_TREE;
12964 while (true)
12966 cp_token *token;
12967 tree type;
12969 /* Get the next type-id. */
12970 type = cp_parser_type_id (parser);
12971 /* Add it to the list. */
12972 types = add_exception_specifier (types, type, /*complain=*/1);
12973 /* Peek at the next token. */
12974 token = cp_lexer_peek_token (parser->lexer);
12975 /* If it is not a `,', we are done. */
12976 if (token->type != CPP_COMMA)
12977 break;
12978 /* Consume the `,'. */
12979 cp_lexer_consume_token (parser->lexer);
12982 return nreverse (types);
12985 /* Parse a try-block.
12987 try-block:
12988 try compound-statement handler-seq */
12990 static tree
12991 cp_parser_try_block (cp_parser* parser)
12993 tree try_block;
12995 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12996 try_block = begin_try_block ();
12997 cp_parser_compound_statement (parser, false);
12998 finish_try_block (try_block);
12999 cp_parser_handler_seq (parser);
13000 finish_handler_sequence (try_block);
13002 return try_block;
13005 /* Parse a function-try-block.
13007 function-try-block:
13008 try ctor-initializer [opt] function-body handler-seq */
13010 static bool
13011 cp_parser_function_try_block (cp_parser* parser)
13013 tree try_block;
13014 bool ctor_initializer_p;
13016 /* Look for the `try' keyword. */
13017 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13018 return false;
13019 /* Let the rest of the front-end know where we are. */
13020 try_block = begin_function_try_block ();
13021 /* Parse the function-body. */
13022 ctor_initializer_p
13023 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13024 /* We're done with the `try' part. */
13025 finish_function_try_block (try_block);
13026 /* Parse the handlers. */
13027 cp_parser_handler_seq (parser);
13028 /* We're done with the handlers. */
13029 finish_function_handler_sequence (try_block);
13031 return ctor_initializer_p;
13034 /* Parse a handler-seq.
13036 handler-seq:
13037 handler handler-seq [opt] */
13039 static void
13040 cp_parser_handler_seq (cp_parser* parser)
13042 while (true)
13044 cp_token *token;
13046 /* Parse the handler. */
13047 cp_parser_handler (parser);
13048 /* Peek at the next token. */
13049 token = cp_lexer_peek_token (parser->lexer);
13050 /* If it's not `catch' then there are no more handlers. */
13051 if (!cp_parser_is_keyword (token, RID_CATCH))
13052 break;
13056 /* Parse a handler.
13058 handler:
13059 catch ( exception-declaration ) compound-statement */
13061 static void
13062 cp_parser_handler (cp_parser* parser)
13064 tree handler;
13065 tree declaration;
13067 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13068 handler = begin_handler ();
13069 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13070 declaration = cp_parser_exception_declaration (parser);
13071 finish_handler_parms (declaration, handler);
13072 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13073 cp_parser_compound_statement (parser, false);
13074 finish_handler (handler);
13077 /* Parse an exception-declaration.
13079 exception-declaration:
13080 type-specifier-seq declarator
13081 type-specifier-seq abstract-declarator
13082 type-specifier-seq
13083 ...
13085 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13086 ellipsis variant is used. */
13088 static tree
13089 cp_parser_exception_declaration (cp_parser* parser)
13091 tree type_specifiers;
13092 tree declarator;
13093 const char *saved_message;
13095 /* If it's an ellipsis, it's easy to handle. */
13096 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13098 /* Consume the `...' token. */
13099 cp_lexer_consume_token (parser->lexer);
13100 return NULL_TREE;
13103 /* Types may not be defined in exception-declarations. */
13104 saved_message = parser->type_definition_forbidden_message;
13105 parser->type_definition_forbidden_message
13106 = "types may not be defined in exception-declarations";
13108 /* Parse the type-specifier-seq. */
13109 type_specifiers = cp_parser_type_specifier_seq (parser);
13110 /* If it's a `)', then there is no declarator. */
13111 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13112 declarator = NULL_TREE;
13113 else
13114 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13115 /*ctor_dtor_or_conv_p=*/NULL,
13116 /*parenthesized_p=*/NULL);
13118 /* Restore the saved message. */
13119 parser->type_definition_forbidden_message = saved_message;
13121 return start_handler_parms (type_specifiers, declarator);
13124 /* Parse a throw-expression.
13126 throw-expression:
13127 throw assignment-expression [opt]
13129 Returns a THROW_EXPR representing the throw-expression. */
13131 static tree
13132 cp_parser_throw_expression (cp_parser* parser)
13134 tree expression;
13135 cp_token* token;
13137 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13138 token = cp_lexer_peek_token (parser->lexer);
13139 /* Figure out whether or not there is an assignment-expression
13140 following the "throw" keyword. */
13141 if (token->type == CPP_COMMA
13142 || token->type == CPP_SEMICOLON
13143 || token->type == CPP_CLOSE_PAREN
13144 || token->type == CPP_CLOSE_SQUARE
13145 || token->type == CPP_CLOSE_BRACE
13146 || token->type == CPP_COLON)
13147 expression = NULL_TREE;
13148 else
13149 expression = cp_parser_assignment_expression (parser);
13151 return build_throw (expression);
13154 /* GNU Extensions */
13156 /* Parse an (optional) asm-specification.
13158 asm-specification:
13159 asm ( string-literal )
13161 If the asm-specification is present, returns a STRING_CST
13162 corresponding to the string-literal. Otherwise, returns
13163 NULL_TREE. */
13165 static tree
13166 cp_parser_asm_specification_opt (cp_parser* parser)
13168 cp_token *token;
13169 tree asm_specification;
13171 /* Peek at the next token. */
13172 token = cp_lexer_peek_token (parser->lexer);
13173 /* If the next token isn't the `asm' keyword, then there's no
13174 asm-specification. */
13175 if (!cp_parser_is_keyword (token, RID_ASM))
13176 return NULL_TREE;
13178 /* Consume the `asm' token. */
13179 cp_lexer_consume_token (parser->lexer);
13180 /* Look for the `('. */
13181 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13183 /* Look for the string-literal. */
13184 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13185 if (token)
13186 asm_specification = token->value;
13187 else
13188 asm_specification = NULL_TREE;
13190 /* Look for the `)'. */
13191 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13193 return asm_specification;
13196 /* Parse an asm-operand-list.
13198 asm-operand-list:
13199 asm-operand
13200 asm-operand-list , asm-operand
13202 asm-operand:
13203 string-literal ( expression )
13204 [ string-literal ] string-literal ( expression )
13206 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13207 each node is the expression. The TREE_PURPOSE is itself a
13208 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13209 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13210 is a STRING_CST for the string literal before the parenthesis. */
13212 static tree
13213 cp_parser_asm_operand_list (cp_parser* parser)
13215 tree asm_operands = NULL_TREE;
13217 while (true)
13219 tree string_literal;
13220 tree expression;
13221 tree name;
13222 cp_token *token;
13224 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13226 /* Consume the `[' token. */
13227 cp_lexer_consume_token (parser->lexer);
13228 /* Read the operand name. */
13229 name = cp_parser_identifier (parser);
13230 if (name != error_mark_node)
13231 name = build_string (IDENTIFIER_LENGTH (name),
13232 IDENTIFIER_POINTER (name));
13233 /* Look for the closing `]'. */
13234 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13236 else
13237 name = NULL_TREE;
13238 /* Look for the string-literal. */
13239 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13240 string_literal = token ? token->value : error_mark_node;
13241 /* Look for the `('. */
13242 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13243 /* Parse the expression. */
13244 expression = cp_parser_expression (parser);
13245 /* Look for the `)'. */
13246 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13247 /* Add this operand to the list. */
13248 asm_operands = tree_cons (build_tree_list (name, string_literal),
13249 expression,
13250 asm_operands);
13251 /* If the next token is not a `,', there are no more
13252 operands. */
13253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13254 break;
13255 /* Consume the `,'. */
13256 cp_lexer_consume_token (parser->lexer);
13259 return nreverse (asm_operands);
13262 /* Parse an asm-clobber-list.
13264 asm-clobber-list:
13265 string-literal
13266 asm-clobber-list , string-literal
13268 Returns a TREE_LIST, indicating the clobbers in the order that they
13269 appeared. The TREE_VALUE of each node is a STRING_CST. */
13271 static tree
13272 cp_parser_asm_clobber_list (cp_parser* parser)
13274 tree clobbers = NULL_TREE;
13276 while (true)
13278 cp_token *token;
13279 tree string_literal;
13281 /* Look for the string literal. */
13282 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13283 string_literal = token ? token->value : error_mark_node;
13284 /* Add it to the list. */
13285 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13286 /* If the next token is not a `,', then the list is
13287 complete. */
13288 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13289 break;
13290 /* Consume the `,' token. */
13291 cp_lexer_consume_token (parser->lexer);
13294 return clobbers;
13297 /* Parse an (optional) series of attributes.
13299 attributes:
13300 attributes attribute
13302 attribute:
13303 __attribute__ (( attribute-list [opt] ))
13305 The return value is as for cp_parser_attribute_list. */
13307 static tree
13308 cp_parser_attributes_opt (cp_parser* parser)
13310 tree attributes = NULL_TREE;
13312 while (true)
13314 cp_token *token;
13315 tree attribute_list;
13317 /* Peek at the next token. */
13318 token = cp_lexer_peek_token (parser->lexer);
13319 /* If it's not `__attribute__', then we're done. */
13320 if (token->keyword != RID_ATTRIBUTE)
13321 break;
13323 /* Consume the `__attribute__' keyword. */
13324 cp_lexer_consume_token (parser->lexer);
13325 /* Look for the two `(' tokens. */
13326 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13327 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13329 /* Peek at the next token. */
13330 token = cp_lexer_peek_token (parser->lexer);
13331 if (token->type != CPP_CLOSE_PAREN)
13332 /* Parse the attribute-list. */
13333 attribute_list = cp_parser_attribute_list (parser);
13334 else
13335 /* If the next token is a `)', then there is no attribute
13336 list. */
13337 attribute_list = NULL;
13339 /* Look for the two `)' tokens. */
13340 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13341 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13343 /* Add these new attributes to the list. */
13344 attributes = chainon (attributes, attribute_list);
13347 return attributes;
13350 /* Parse an attribute-list.
13352 attribute-list:
13353 attribute
13354 attribute-list , attribute
13356 attribute:
13357 identifier
13358 identifier ( identifier )
13359 identifier ( identifier , expression-list )
13360 identifier ( expression-list )
13362 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13363 TREE_PURPOSE of each node is the identifier indicating which
13364 attribute is in use. The TREE_VALUE represents the arguments, if
13365 any. */
13367 static tree
13368 cp_parser_attribute_list (cp_parser* parser)
13370 tree attribute_list = NULL_TREE;
13372 while (true)
13374 cp_token *token;
13375 tree identifier;
13376 tree attribute;
13378 /* Look for the identifier. We also allow keywords here; for
13379 example `__attribute__ ((const))' is legal. */
13380 token = cp_lexer_peek_token (parser->lexer);
13381 if (token->type != CPP_NAME
13382 && token->type != CPP_KEYWORD)
13383 return error_mark_node;
13384 /* Consume the token. */
13385 token = cp_lexer_consume_token (parser->lexer);
13387 /* Save away the identifier that indicates which attribute this is. */
13388 identifier = token->value;
13389 attribute = build_tree_list (identifier, NULL_TREE);
13391 /* Peek at the next token. */
13392 token = cp_lexer_peek_token (parser->lexer);
13393 /* If it's an `(', then parse the attribute arguments. */
13394 if (token->type == CPP_OPEN_PAREN)
13396 tree arguments;
13398 arguments = (cp_parser_parenthesized_expression_list
13399 (parser, true, /*non_constant_p=*/NULL));
13400 /* Save the identifier and arguments away. */
13401 TREE_VALUE (attribute) = arguments;
13404 /* Add this attribute to the list. */
13405 TREE_CHAIN (attribute) = attribute_list;
13406 attribute_list = attribute;
13408 /* Now, look for more attributes. */
13409 token = cp_lexer_peek_token (parser->lexer);
13410 /* If the next token isn't a `,', we're done. */
13411 if (token->type != CPP_COMMA)
13412 break;
13414 /* Consume the comma and keep going. */
13415 cp_lexer_consume_token (parser->lexer);
13418 /* We built up the list in reverse order. */
13419 return nreverse (attribute_list);
13422 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13423 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13424 current value of the PEDANTIC flag, regardless of whether or not
13425 the `__extension__' keyword is present. The caller is responsible
13426 for restoring the value of the PEDANTIC flag. */
13428 static bool
13429 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13431 /* Save the old value of the PEDANTIC flag. */
13432 *saved_pedantic = pedantic;
13434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13436 /* Consume the `__extension__' token. */
13437 cp_lexer_consume_token (parser->lexer);
13438 /* We're not being pedantic while the `__extension__' keyword is
13439 in effect. */
13440 pedantic = 0;
13442 return true;
13445 return false;
13448 /* Parse a label declaration.
13450 label-declaration:
13451 __label__ label-declarator-seq ;
13453 label-declarator-seq:
13454 identifier , label-declarator-seq
13455 identifier */
13457 static void
13458 cp_parser_label_declaration (cp_parser* parser)
13460 /* Look for the `__label__' keyword. */
13461 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13463 while (true)
13465 tree identifier;
13467 /* Look for an identifier. */
13468 identifier = cp_parser_identifier (parser);
13469 /* Declare it as a lobel. */
13470 finish_label_decl (identifier);
13471 /* If the next token is a `;', stop. */
13472 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13473 break;
13474 /* Look for the `,' separating the label declarations. */
13475 cp_parser_require (parser, CPP_COMMA, "`,'");
13478 /* Look for the final `;'. */
13479 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13482 /* Support Functions */
13484 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13485 NAME should have one of the representations used for an
13486 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13487 is returned. If PARSER->SCOPE is a dependent type, then a
13488 SCOPE_REF is returned.
13490 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13491 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13492 was formed. Abstractly, such entities should not be passed to this
13493 function, because they do not need to be looked up, but it is
13494 simpler to check for this special case here, rather than at the
13495 call-sites.
13497 In cases not explicitly covered above, this function returns a
13498 DECL, OVERLOAD, or baselink representing the result of the lookup.
13499 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13500 is returned.
13502 If IS_TYPE is TRUE, bindings that do not refer to types are
13503 ignored.
13505 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13506 ignored.
13508 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13509 are ignored.
13511 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13512 types. */
13514 static tree
13515 cp_parser_lookup_name (cp_parser *parser, tree name,
13516 bool is_type, bool is_template, bool is_namespace,
13517 bool check_dependency)
13519 tree decl;
13520 tree object_type = parser->context->object_type;
13522 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13523 no longer valid. Note that if we are parsing tentatively, and
13524 the parse fails, OBJECT_TYPE will be automatically restored. */
13525 parser->context->object_type = NULL_TREE;
13527 if (name == error_mark_node)
13528 return error_mark_node;
13530 /* A template-id has already been resolved; there is no lookup to
13531 do. */
13532 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13533 return name;
13534 if (BASELINK_P (name))
13536 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13537 == TEMPLATE_ID_EXPR),
13538 20020909);
13539 return name;
13542 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13543 it should already have been checked to make sure that the name
13544 used matches the type being destroyed. */
13545 if (TREE_CODE (name) == BIT_NOT_EXPR)
13547 tree type;
13549 /* Figure out to which type this destructor applies. */
13550 if (parser->scope)
13551 type = parser->scope;
13552 else if (object_type)
13553 type = object_type;
13554 else
13555 type = current_class_type;
13556 /* If that's not a class type, there is no destructor. */
13557 if (!type || !CLASS_TYPE_P (type))
13558 return error_mark_node;
13559 if (!CLASSTYPE_DESTRUCTORS (type))
13560 return error_mark_node;
13561 /* If it was a class type, return the destructor. */
13562 return CLASSTYPE_DESTRUCTORS (type);
13565 /* By this point, the NAME should be an ordinary identifier. If
13566 the id-expression was a qualified name, the qualifying scope is
13567 stored in PARSER->SCOPE at this point. */
13568 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13569 20000619);
13571 /* Perform the lookup. */
13572 if (parser->scope)
13574 bool dependent_p;
13576 if (parser->scope == error_mark_node)
13577 return error_mark_node;
13579 /* If the SCOPE is dependent, the lookup must be deferred until
13580 the template is instantiated -- unless we are explicitly
13581 looking up names in uninstantiated templates. Even then, we
13582 cannot look up the name if the scope is not a class type; it
13583 might, for example, be a template type parameter. */
13584 dependent_p = (TYPE_P (parser->scope)
13585 && !(parser->in_declarator_p
13586 && currently_open_class (parser->scope))
13587 && dependent_type_p (parser->scope));
13588 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13589 && dependent_p)
13591 if (is_type)
13592 /* The resolution to Core Issue 180 says that `struct A::B'
13593 should be considered a type-name, even if `A' is
13594 dependent. */
13595 decl = TYPE_NAME (make_typename_type (parser->scope,
13596 name,
13597 /*complain=*/1));
13598 else if (is_template)
13599 decl = make_unbound_class_template (parser->scope,
13600 name,
13601 /*complain=*/1);
13602 else
13603 decl = build_nt (SCOPE_REF, parser->scope, name);
13605 else
13607 bool pop_p = false;
13609 /* If PARSER->SCOPE is a dependent type, then it must be a
13610 class type, and we must not be checking dependencies;
13611 otherwise, we would have processed this lookup above. So
13612 that PARSER->SCOPE is not considered a dependent base by
13613 lookup_member, we must enter the scope here. */
13614 if (dependent_p)
13615 pop_p = push_scope (parser->scope);
13616 /* If the PARSER->SCOPE is a a template specialization, it
13617 may be instantiated during name lookup. In that case,
13618 errors may be issued. Even if we rollback the current
13619 tentative parse, those errors are valid. */
13620 decl = lookup_qualified_name (parser->scope, name, is_type,
13621 /*complain=*/true);
13622 if (pop_p)
13623 pop_scope (parser->scope);
13625 parser->qualifying_scope = parser->scope;
13626 parser->object_scope = NULL_TREE;
13628 else if (object_type)
13630 tree object_decl = NULL_TREE;
13631 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13632 OBJECT_TYPE is not a class. */
13633 if (CLASS_TYPE_P (object_type))
13634 /* If the OBJECT_TYPE is a template specialization, it may
13635 be instantiated during name lookup. In that case, errors
13636 may be issued. Even if we rollback the current tentative
13637 parse, those errors are valid. */
13638 object_decl = lookup_member (object_type,
13639 name,
13640 /*protect=*/0, is_type);
13641 /* Look it up in the enclosing context, too. */
13642 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13643 is_namespace,
13644 /*flags=*/0);
13645 parser->object_scope = object_type;
13646 parser->qualifying_scope = NULL_TREE;
13647 if (object_decl)
13648 decl = object_decl;
13650 else
13652 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13653 is_namespace,
13654 /*flags=*/0);
13655 parser->qualifying_scope = NULL_TREE;
13656 parser->object_scope = NULL_TREE;
13659 /* If the lookup failed, let our caller know. */
13660 if (!decl
13661 || decl == error_mark_node
13662 || (TREE_CODE (decl) == FUNCTION_DECL
13663 && DECL_ANTICIPATED (decl)))
13664 return error_mark_node;
13666 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13667 if (TREE_CODE (decl) == TREE_LIST)
13669 /* The error message we have to print is too complicated for
13670 cp_parser_error, so we incorporate its actions directly. */
13671 if (!cp_parser_simulate_error (parser))
13673 error ("reference to `%D' is ambiguous", name);
13674 print_candidates (decl);
13676 return error_mark_node;
13679 my_friendly_assert (DECL_P (decl)
13680 || TREE_CODE (decl) == OVERLOAD
13681 || TREE_CODE (decl) == SCOPE_REF
13682 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13683 || BASELINK_P (decl),
13684 20000619);
13686 /* If we have resolved the name of a member declaration, check to
13687 see if the declaration is accessible. When the name resolves to
13688 set of overloaded functions, accessibility is checked when
13689 overload resolution is done.
13691 During an explicit instantiation, access is not checked at all,
13692 as per [temp.explicit]. */
13693 if (DECL_P (decl))
13694 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13696 return decl;
13699 /* Like cp_parser_lookup_name, but for use in the typical case where
13700 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13701 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13703 static tree
13704 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13706 return cp_parser_lookup_name (parser, name,
13707 /*is_type=*/false,
13708 /*is_template=*/false,
13709 /*is_namespace=*/false,
13710 /*check_dependency=*/true);
13713 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13714 the current context, return the TYPE_DECL. If TAG_NAME_P is
13715 true, the DECL indicates the class being defined in a class-head,
13716 or declared in an elaborated-type-specifier.
13718 Otherwise, return DECL. */
13720 static tree
13721 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13723 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13724 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13726 struct A {
13727 template <typename T> struct B;
13730 template <typename T> struct A::B {};
13732 Similarly, in a elaborated-type-specifier:
13734 namespace N { struct X{}; }
13736 struct A {
13737 template <typename T> friend struct N::X;
13740 However, if the DECL refers to a class type, and we are in
13741 the scope of the class, then the name lookup automatically
13742 finds the TYPE_DECL created by build_self_reference rather
13743 than a TEMPLATE_DECL. For example, in:
13745 template <class T> struct S {
13746 S s;
13749 there is no need to handle such case. */
13751 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13752 return DECL_TEMPLATE_RESULT (decl);
13754 return decl;
13757 /* If too many, or too few, template-parameter lists apply to the
13758 declarator, issue an error message. Returns TRUE if all went well,
13759 and FALSE otherwise. */
13761 static bool
13762 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13763 tree declarator)
13765 unsigned num_templates;
13767 /* We haven't seen any classes that involve template parameters yet. */
13768 num_templates = 0;
13770 switch (TREE_CODE (declarator))
13772 case CALL_EXPR:
13773 case ARRAY_REF:
13774 case INDIRECT_REF:
13775 case ADDR_EXPR:
13777 tree main_declarator = TREE_OPERAND (declarator, 0);
13778 return
13779 cp_parser_check_declarator_template_parameters (parser,
13780 main_declarator);
13783 case SCOPE_REF:
13785 tree scope;
13786 tree member;
13788 scope = TREE_OPERAND (declarator, 0);
13789 member = TREE_OPERAND (declarator, 1);
13791 /* If this is a pointer-to-member, then we are not interested
13792 in the SCOPE, because it does not qualify the thing that is
13793 being declared. */
13794 if (TREE_CODE (member) == INDIRECT_REF)
13795 return (cp_parser_check_declarator_template_parameters
13796 (parser, member));
13798 while (scope && CLASS_TYPE_P (scope))
13800 /* You're supposed to have one `template <...>'
13801 for every template class, but you don't need one
13802 for a full specialization. For example:
13804 template <class T> struct S{};
13805 template <> struct S<int> { void f(); };
13806 void S<int>::f () {}
13808 is correct; there shouldn't be a `template <>' for
13809 the definition of `S<int>::f'. */
13810 if (CLASSTYPE_TEMPLATE_INFO (scope)
13811 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13812 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13813 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13814 ++num_templates;
13816 scope = TYPE_CONTEXT (scope);
13820 /* Fall through. */
13822 default:
13823 /* If the DECLARATOR has the form `X<y>' then it uses one
13824 additional level of template parameters. */
13825 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13826 ++num_templates;
13828 return cp_parser_check_template_parameters (parser,
13829 num_templates);
13833 /* NUM_TEMPLATES were used in the current declaration. If that is
13834 invalid, return FALSE and issue an error messages. Otherwise,
13835 return TRUE. */
13837 static bool
13838 cp_parser_check_template_parameters (cp_parser* parser,
13839 unsigned num_templates)
13841 /* If there are more template classes than parameter lists, we have
13842 something like:
13844 template <class T> void S<T>::R<T>::f (); */
13845 if (parser->num_template_parameter_lists < num_templates)
13847 error ("too few template-parameter-lists");
13848 return false;
13850 /* If there are the same number of template classes and parameter
13851 lists, that's OK. */
13852 if (parser->num_template_parameter_lists == num_templates)
13853 return true;
13854 /* If there are more, but only one more, then we are referring to a
13855 member template. That's OK too. */
13856 if (parser->num_template_parameter_lists == num_templates + 1)
13857 return true;
13858 /* Otherwise, there are too many template parameter lists. We have
13859 something like:
13861 template <class T> template <class U> void S::f(); */
13862 error ("too many template-parameter-lists");
13863 return false;
13866 /* Parse a binary-expression of the general form:
13868 binary-expression:
13869 <expr>
13870 binary-expression <token> <expr>
13872 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13873 to parser the <expr>s. If the first production is used, then the
13874 value returned by FN is returned directly. Otherwise, a node with
13875 the indicated EXPR_TYPE is returned, with operands corresponding to
13876 the two sub-expressions. */
13878 static tree
13879 cp_parser_binary_expression (cp_parser* parser,
13880 const cp_parser_token_tree_map token_tree_map,
13881 cp_parser_expression_fn fn)
13883 tree lhs;
13885 /* Parse the first expression. */
13886 lhs = (*fn) (parser);
13887 /* Now, look for more expressions. */
13888 while (true)
13890 cp_token *token;
13891 const cp_parser_token_tree_map_node *map_node;
13892 tree rhs;
13894 /* Peek at the next token. */
13895 token = cp_lexer_peek_token (parser->lexer);
13896 /* If the token is `>', and that's not an operator at the
13897 moment, then we're done. */
13898 if (token->type == CPP_GREATER
13899 && !parser->greater_than_is_operator_p)
13900 break;
13901 /* If we find one of the tokens we want, build the corresponding
13902 tree representation. */
13903 for (map_node = token_tree_map;
13904 map_node->token_type != CPP_EOF;
13905 ++map_node)
13906 if (map_node->token_type == token->type)
13908 /* Assume that an overloaded operator will not be used. */
13909 bool overloaded_p = false;
13911 /* Consume the operator token. */
13912 cp_lexer_consume_token (parser->lexer);
13913 /* Parse the right-hand side of the expression. */
13914 rhs = (*fn) (parser);
13915 /* Build the binary tree node. */
13916 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
13917 &overloaded_p);
13918 /* If the binary operator required the use of an
13919 overloaded operator, then this expression cannot be an
13920 integral constant-expression. An overloaded operator
13921 can be used even if both operands are otherwise
13922 permissible in an integral constant-expression if at
13923 least one of the operands is of enumeration type. */
13924 if (overloaded_p
13925 && (cp_parser_non_integral_constant_expression
13926 (parser, "calls to overloaded operators")))
13927 lhs = error_mark_node;
13928 break;
13931 /* If the token wasn't one of the ones we want, we're done. */
13932 if (map_node->token_type == CPP_EOF)
13933 break;
13936 return lhs;
13939 /* Parse an optional `::' token indicating that the following name is
13940 from the global namespace. If so, PARSER->SCOPE is set to the
13941 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13942 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13943 Returns the new value of PARSER->SCOPE, if the `::' token is
13944 present, and NULL_TREE otherwise. */
13946 static tree
13947 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13949 cp_token *token;
13951 /* Peek at the next token. */
13952 token = cp_lexer_peek_token (parser->lexer);
13953 /* If we're looking at a `::' token then we're starting from the
13954 global namespace, not our current location. */
13955 if (token->type == CPP_SCOPE)
13957 /* Consume the `::' token. */
13958 cp_lexer_consume_token (parser->lexer);
13959 /* Set the SCOPE so that we know where to start the lookup. */
13960 parser->scope = global_namespace;
13961 parser->qualifying_scope = global_namespace;
13962 parser->object_scope = NULL_TREE;
13964 return parser->scope;
13966 else if (!current_scope_valid_p)
13968 parser->scope = NULL_TREE;
13969 parser->qualifying_scope = NULL_TREE;
13970 parser->object_scope = NULL_TREE;
13973 return NULL_TREE;
13976 /* Returns TRUE if the upcoming token sequence is the start of a
13977 constructor declarator. If FRIEND_P is true, the declarator is
13978 preceded by the `friend' specifier. */
13980 static bool
13981 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13983 bool constructor_p;
13984 tree type_decl = NULL_TREE;
13985 bool nested_name_p;
13986 cp_token *next_token;
13988 /* The common case is that this is not a constructor declarator, so
13989 try to avoid doing lots of work if at all possible. It's not
13990 valid declare a constructor at function scope. */
13991 if (at_function_scope_p ())
13992 return false;
13993 /* And only certain tokens can begin a constructor declarator. */
13994 next_token = cp_lexer_peek_token (parser->lexer);
13995 if (next_token->type != CPP_NAME
13996 && next_token->type != CPP_SCOPE
13997 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13998 && next_token->type != CPP_TEMPLATE_ID)
13999 return false;
14001 /* Parse tentatively; we are going to roll back all of the tokens
14002 consumed here. */
14003 cp_parser_parse_tentatively (parser);
14004 /* Assume that we are looking at a constructor declarator. */
14005 constructor_p = true;
14007 /* Look for the optional `::' operator. */
14008 cp_parser_global_scope_opt (parser,
14009 /*current_scope_valid_p=*/false);
14010 /* Look for the nested-name-specifier. */
14011 nested_name_p
14012 = (cp_parser_nested_name_specifier_opt (parser,
14013 /*typename_keyword_p=*/false,
14014 /*check_dependency_p=*/false,
14015 /*type_p=*/false,
14016 /*is_declaration=*/false)
14017 != NULL_TREE);
14018 /* Outside of a class-specifier, there must be a
14019 nested-name-specifier. */
14020 if (!nested_name_p &&
14021 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14022 || friend_p))
14023 constructor_p = false;
14024 /* If we still think that this might be a constructor-declarator,
14025 look for a class-name. */
14026 if (constructor_p)
14028 /* If we have:
14030 template <typename T> struct S { S(); };
14031 template <typename T> S<T>::S ();
14033 we must recognize that the nested `S' names a class.
14034 Similarly, for:
14036 template <typename T> S<T>::S<T> ();
14038 we must recognize that the nested `S' names a template. */
14039 type_decl = cp_parser_class_name (parser,
14040 /*typename_keyword_p=*/false,
14041 /*template_keyword_p=*/false,
14042 /*type_p=*/false,
14043 /*check_dependency_p=*/false,
14044 /*class_head_p=*/false,
14045 /*is_declaration=*/false);
14046 /* If there was no class-name, then this is not a constructor. */
14047 constructor_p = !cp_parser_error_occurred (parser);
14050 /* If we're still considering a constructor, we have to see a `(',
14051 to begin the parameter-declaration-clause, followed by either a
14052 `)', an `...', or a decl-specifier. We need to check for a
14053 type-specifier to avoid being fooled into thinking that:
14055 S::S (f) (int);
14057 is a constructor. (It is actually a function named `f' that
14058 takes one parameter (of type `int') and returns a value of type
14059 `S::S'. */
14060 if (constructor_p
14061 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14063 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14064 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14065 /* A parameter declaration begins with a decl-specifier,
14066 which is either the "attribute" keyword, a storage class
14067 specifier, or (usually) a type-specifier. */
14068 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14069 && !cp_parser_storage_class_specifier_opt (parser))
14071 tree type;
14072 bool pop_p = false;
14073 unsigned saved_num_template_parameter_lists;
14075 /* Names appearing in the type-specifier should be looked up
14076 in the scope of the class. */
14077 if (current_class_type)
14078 type = NULL_TREE;
14079 else
14081 type = TREE_TYPE (type_decl);
14082 if (TREE_CODE (type) == TYPENAME_TYPE)
14084 type = resolve_typename_type (type,
14085 /*only_current_p=*/false);
14086 if (type == error_mark_node)
14088 cp_parser_abort_tentative_parse (parser);
14089 return false;
14092 pop_p = push_scope (type);
14095 /* Inside the constructor parameter list, surrounding
14096 template-parameter-lists do not apply. */
14097 saved_num_template_parameter_lists
14098 = parser->num_template_parameter_lists;
14099 parser->num_template_parameter_lists = 0;
14101 /* Look for the type-specifier. */
14102 cp_parser_type_specifier (parser,
14103 CP_PARSER_FLAGS_NONE,
14104 /*is_friend=*/false,
14105 /*is_declarator=*/true,
14106 /*declares_class_or_enum=*/NULL,
14107 /*is_cv_qualifier=*/NULL);
14109 parser->num_template_parameter_lists
14110 = saved_num_template_parameter_lists;
14112 /* Leave the scope of the class. */
14113 if (pop_p)
14114 pop_scope (type);
14116 constructor_p = !cp_parser_error_occurred (parser);
14119 else
14120 constructor_p = false;
14121 /* We did not really want to consume any tokens. */
14122 cp_parser_abort_tentative_parse (parser);
14124 return constructor_p;
14127 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14128 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14129 they must be performed once we are in the scope of the function.
14131 Returns the function defined. */
14133 static tree
14134 cp_parser_function_definition_from_specifiers_and_declarator
14135 (cp_parser* parser,
14136 tree decl_specifiers,
14137 tree attributes,
14138 tree declarator)
14140 tree fn;
14141 bool success_p;
14143 /* Begin the function-definition. */
14144 success_p = begin_function_definition (decl_specifiers,
14145 attributes,
14146 declarator);
14148 /* If there were names looked up in the decl-specifier-seq that we
14149 did not check, check them now. We must wait until we are in the
14150 scope of the function to perform the checks, since the function
14151 might be a friend. */
14152 perform_deferred_access_checks ();
14154 if (!success_p)
14156 /* If begin_function_definition didn't like the definition, skip
14157 the entire function. */
14158 error ("invalid function declaration");
14159 cp_parser_skip_to_end_of_block_or_statement (parser);
14160 fn = error_mark_node;
14162 else
14163 fn = cp_parser_function_definition_after_declarator (parser,
14164 /*inline_p=*/false);
14166 return fn;
14169 /* Parse the part of a function-definition that follows the
14170 declarator. INLINE_P is TRUE iff this function is an inline
14171 function defined with a class-specifier.
14173 Returns the function defined. */
14175 static tree
14176 cp_parser_function_definition_after_declarator (cp_parser* parser,
14177 bool inline_p)
14179 tree fn;
14180 bool ctor_initializer_p = false;
14181 bool saved_in_unbraced_linkage_specification_p;
14182 unsigned saved_num_template_parameter_lists;
14184 /* If the next token is `return', then the code may be trying to
14185 make use of the "named return value" extension that G++ used to
14186 support. */
14187 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14189 /* Consume the `return' keyword. */
14190 cp_lexer_consume_token (parser->lexer);
14191 /* Look for the identifier that indicates what value is to be
14192 returned. */
14193 cp_parser_identifier (parser);
14194 /* Issue an error message. */
14195 error ("named return values are no longer supported");
14196 /* Skip tokens until we reach the start of the function body. */
14197 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14198 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14199 cp_lexer_consume_token (parser->lexer);
14201 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14202 anything declared inside `f'. */
14203 saved_in_unbraced_linkage_specification_p
14204 = parser->in_unbraced_linkage_specification_p;
14205 parser->in_unbraced_linkage_specification_p = false;
14206 /* Inside the function, surrounding template-parameter-lists do not
14207 apply. */
14208 saved_num_template_parameter_lists
14209 = parser->num_template_parameter_lists;
14210 parser->num_template_parameter_lists = 0;
14211 /* If the next token is `try', then we are looking at a
14212 function-try-block. */
14213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14214 ctor_initializer_p = cp_parser_function_try_block (parser);
14215 /* A function-try-block includes the function-body, so we only do
14216 this next part if we're not processing a function-try-block. */
14217 else
14218 ctor_initializer_p
14219 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14221 /* Finish the function. */
14222 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14223 (inline_p ? 2 : 0));
14224 /* Generate code for it, if necessary. */
14225 expand_or_defer_fn (fn);
14226 /* Restore the saved values. */
14227 parser->in_unbraced_linkage_specification_p
14228 = saved_in_unbraced_linkage_specification_p;
14229 parser->num_template_parameter_lists
14230 = saved_num_template_parameter_lists;
14232 return fn;
14235 /* Parse a template-declaration, assuming that the `export' (and
14236 `extern') keywords, if present, has already been scanned. MEMBER_P
14237 is as for cp_parser_template_declaration. */
14239 static void
14240 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14242 tree decl = NULL_TREE;
14243 tree parameter_list;
14244 bool friend_p = false;
14246 /* Look for the `template' keyword. */
14247 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14248 return;
14250 /* And the `<'. */
14251 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14252 return;
14254 /* If the next token is `>', then we have an invalid
14255 specialization. Rather than complain about an invalid template
14256 parameter, issue an error message here. */
14257 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14259 cp_parser_error (parser, "invalid explicit specialization");
14260 begin_specialization ();
14261 parameter_list = NULL_TREE;
14263 else
14265 /* Parse the template parameters. */
14266 begin_template_parm_list ();
14267 parameter_list = cp_parser_template_parameter_list (parser);
14268 parameter_list = end_template_parm_list (parameter_list);
14271 /* Look for the `>'. */
14272 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14273 /* We just processed one more parameter list. */
14274 ++parser->num_template_parameter_lists;
14275 /* If the next token is `template', there are more template
14276 parameters. */
14277 if (cp_lexer_next_token_is_keyword (parser->lexer,
14278 RID_TEMPLATE))
14279 cp_parser_template_declaration_after_export (parser, member_p);
14280 else
14282 decl = cp_parser_single_declaration (parser,
14283 member_p,
14284 &friend_p);
14286 /* If this is a member template declaration, let the front
14287 end know. */
14288 if (member_p && !friend_p && decl)
14290 if (TREE_CODE (decl) == TYPE_DECL)
14291 cp_parser_check_access_in_redeclaration (decl);
14293 decl = finish_member_template_decl (decl);
14295 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14296 make_friend_class (current_class_type, TREE_TYPE (decl),
14297 /*complain=*/true);
14299 /* We are done with the current parameter list. */
14300 --parser->num_template_parameter_lists;
14302 /* Finish up. */
14303 finish_template_decl (parameter_list);
14305 /* Register member declarations. */
14306 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14307 finish_member_declaration (decl);
14309 /* If DECL is a function template, we must return to parse it later.
14310 (Even though there is no definition, there might be default
14311 arguments that need handling.) */
14312 if (member_p && decl
14313 && (TREE_CODE (decl) == FUNCTION_DECL
14314 || DECL_FUNCTION_TEMPLATE_P (decl)))
14315 TREE_VALUE (parser->unparsed_functions_queues)
14316 = tree_cons (NULL_TREE, decl,
14317 TREE_VALUE (parser->unparsed_functions_queues));
14320 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14321 `function-definition' sequence. MEMBER_P is true, this declaration
14322 appears in a class scope.
14324 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14325 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14327 static tree
14328 cp_parser_single_declaration (cp_parser* parser,
14329 bool member_p,
14330 bool* friend_p)
14332 int declares_class_or_enum;
14333 tree decl = NULL_TREE;
14334 tree decl_specifiers;
14335 tree attributes;
14336 bool function_definition_p = false;
14338 /* Defer access checks until we know what is being declared. */
14339 push_deferring_access_checks (dk_deferred);
14341 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14342 alternative. */
14343 decl_specifiers
14344 = cp_parser_decl_specifier_seq (parser,
14345 CP_PARSER_FLAGS_OPTIONAL,
14346 &attributes,
14347 &declares_class_or_enum);
14348 if (friend_p)
14349 *friend_p = cp_parser_friend_p (decl_specifiers);
14350 /* Gather up the access checks that occurred the
14351 decl-specifier-seq. */
14352 stop_deferring_access_checks ();
14354 /* Check for the declaration of a template class. */
14355 if (declares_class_or_enum)
14357 if (cp_parser_declares_only_class_p (parser))
14359 decl = shadow_tag (decl_specifiers);
14360 if (decl)
14361 decl = TYPE_NAME (decl);
14362 else
14363 decl = error_mark_node;
14366 else
14367 decl = NULL_TREE;
14368 /* If it's not a template class, try for a template function. If
14369 the next token is a `;', then this declaration does not declare
14370 anything. But, if there were errors in the decl-specifiers, then
14371 the error might well have come from an attempted class-specifier.
14372 In that case, there's no need to warn about a missing declarator. */
14373 if (!decl
14374 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14375 || !value_member (error_mark_node, decl_specifiers)))
14376 decl = cp_parser_init_declarator (parser,
14377 decl_specifiers,
14378 attributes,
14379 /*function_definition_allowed_p=*/true,
14380 member_p,
14381 declares_class_or_enum,
14382 &function_definition_p);
14384 pop_deferring_access_checks ();
14386 /* Clear any current qualification; whatever comes next is the start
14387 of something new. */
14388 parser->scope = NULL_TREE;
14389 parser->qualifying_scope = NULL_TREE;
14390 parser->object_scope = NULL_TREE;
14391 /* Look for a trailing `;' after the declaration. */
14392 if (!function_definition_p
14393 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14394 cp_parser_skip_to_end_of_block_or_statement (parser);
14396 return decl;
14399 /* Parse a cast-expression that is not the operand of a unary "&". */
14401 static tree
14402 cp_parser_simple_cast_expression (cp_parser *parser)
14404 return cp_parser_cast_expression (parser, /*address_p=*/false);
14407 /* Parse a functional cast to TYPE. Returns an expression
14408 representing the cast. */
14410 static tree
14411 cp_parser_functional_cast (cp_parser* parser, tree type)
14413 tree expression_list;
14414 tree cast;
14416 expression_list
14417 = cp_parser_parenthesized_expression_list (parser, false,
14418 /*non_constant_p=*/NULL);
14420 cast = build_functional_cast (type, expression_list);
14421 /* [expr.const]/1: In an integral constant expression "only type
14422 conversions to integral or enumeration type can be used". */
14423 if (cast != error_mark_node && !type_dependent_expression_p (type)
14424 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14426 if (cp_parser_non_integral_constant_expression
14427 (parser, "a call to a constructor"))
14428 return error_mark_node;
14430 return cast;
14433 /* Save the tokens that make up the body of a member function defined
14434 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14435 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14436 specifiers applied to the declaration. Returns the FUNCTION_DECL
14437 for the member function. */
14439 static tree
14440 cp_parser_save_member_function_body (cp_parser* parser,
14441 tree decl_specifiers,
14442 tree declarator,
14443 tree attributes)
14445 cp_token_cache *cache;
14446 tree fn;
14448 /* Create the function-declaration. */
14449 fn = start_method (decl_specifiers, declarator, attributes);
14450 /* If something went badly wrong, bail out now. */
14451 if (fn == error_mark_node)
14453 /* If there's a function-body, skip it. */
14454 if (cp_parser_token_starts_function_definition_p
14455 (cp_lexer_peek_token (parser->lexer)))
14456 cp_parser_skip_to_end_of_block_or_statement (parser);
14457 return error_mark_node;
14460 /* Remember it, if there default args to post process. */
14461 cp_parser_save_default_args (parser, fn);
14463 /* Create a token cache. */
14464 cache = cp_token_cache_new ();
14465 /* Save away the tokens that make up the body of the
14466 function. */
14467 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14468 /* Handle function try blocks. */
14469 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14470 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14472 /* Save away the inline definition; we will process it when the
14473 class is complete. */
14474 DECL_PENDING_INLINE_INFO (fn) = cache;
14475 DECL_PENDING_INLINE_P (fn) = 1;
14477 /* We need to know that this was defined in the class, so that
14478 friend templates are handled correctly. */
14479 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14481 /* We're done with the inline definition. */
14482 finish_method (fn);
14484 /* Add FN to the queue of functions to be parsed later. */
14485 TREE_VALUE (parser->unparsed_functions_queues)
14486 = tree_cons (NULL_TREE, fn,
14487 TREE_VALUE (parser->unparsed_functions_queues));
14489 return fn;
14492 /* Parse a template-argument-list, as well as the trailing ">" (but
14493 not the opening ">"). See cp_parser_template_argument_list for the
14494 return value. */
14496 static tree
14497 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14499 tree arguments;
14500 tree saved_scope;
14501 tree saved_qualifying_scope;
14502 tree saved_object_scope;
14503 bool saved_greater_than_is_operator_p;
14505 /* [temp.names]
14507 When parsing a template-id, the first non-nested `>' is taken as
14508 the end of the template-argument-list rather than a greater-than
14509 operator. */
14510 saved_greater_than_is_operator_p
14511 = parser->greater_than_is_operator_p;
14512 parser->greater_than_is_operator_p = false;
14513 /* Parsing the argument list may modify SCOPE, so we save it
14514 here. */
14515 saved_scope = parser->scope;
14516 saved_qualifying_scope = parser->qualifying_scope;
14517 saved_object_scope = parser->object_scope;
14518 /* Parse the template-argument-list itself. */
14519 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14520 arguments = NULL_TREE;
14521 else
14522 arguments = cp_parser_template_argument_list (parser);
14523 /* Look for the `>' that ends the template-argument-list. If we find
14524 a '>>' instead, it's probably just a typo. */
14525 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14527 if (!saved_greater_than_is_operator_p)
14529 /* If we're in a nested template argument list, the '>>' has to be
14530 a typo for '> >'. We emit the error message, but we continue
14531 parsing and we push a '>' as next token, so that the argument
14532 list will be parsed correctly.. */
14533 cp_token* token;
14534 error ("`>>' should be `> >' within a nested template argument list");
14535 token = cp_lexer_peek_token (parser->lexer);
14536 token->type = CPP_GREATER;
14538 else
14540 /* If this is not a nested template argument list, the '>>' is
14541 a typo for '>'. Emit an error message and continue. */
14542 error ("spurious `>>', use `>' to terminate a template argument list");
14543 cp_lexer_consume_token (parser->lexer);
14546 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14547 error ("missing `>' to terminate the template argument list");
14548 /* The `>' token might be a greater-than operator again now. */
14549 parser->greater_than_is_operator_p
14550 = saved_greater_than_is_operator_p;
14551 /* Restore the SAVED_SCOPE. */
14552 parser->scope = saved_scope;
14553 parser->qualifying_scope = saved_qualifying_scope;
14554 parser->object_scope = saved_object_scope;
14556 return arguments;
14559 /* MEMBER_FUNCTION is a member function, or a friend. If default
14560 arguments, or the body of the function have not yet been parsed,
14561 parse them now. */
14563 static void
14564 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14566 cp_lexer *saved_lexer;
14568 /* If this member is a template, get the underlying
14569 FUNCTION_DECL. */
14570 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14571 member_function = DECL_TEMPLATE_RESULT (member_function);
14573 /* There should not be any class definitions in progress at this
14574 point; the bodies of members are only parsed outside of all class
14575 definitions. */
14576 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14577 /* While we're parsing the member functions we might encounter more
14578 classes. We want to handle them right away, but we don't want
14579 them getting mixed up with functions that are currently in the
14580 queue. */
14581 parser->unparsed_functions_queues
14582 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14584 /* Make sure that any template parameters are in scope. */
14585 maybe_begin_member_template_processing (member_function);
14587 /* If the body of the function has not yet been parsed, parse it
14588 now. */
14589 if (DECL_PENDING_INLINE_P (member_function))
14591 tree function_scope;
14592 cp_token_cache *tokens;
14594 /* The function is no longer pending; we are processing it. */
14595 tokens = DECL_PENDING_INLINE_INFO (member_function);
14596 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14597 DECL_PENDING_INLINE_P (member_function) = 0;
14598 /* If this was an inline function in a local class, enter the scope
14599 of the containing function. */
14600 function_scope = decl_function_context (member_function);
14601 if (function_scope)
14602 push_function_context_to (function_scope);
14604 /* Save away the current lexer. */
14605 saved_lexer = parser->lexer;
14606 /* Make a new lexer to feed us the tokens saved for this function. */
14607 parser->lexer = cp_lexer_new_from_tokens (tokens);
14608 parser->lexer->next = saved_lexer;
14610 /* Set the current source position to be the location of the first
14611 token in the saved inline body. */
14612 cp_lexer_peek_token (parser->lexer);
14614 /* Let the front end know that we going to be defining this
14615 function. */
14616 start_function (NULL_TREE, member_function, NULL_TREE,
14617 SF_PRE_PARSED | SF_INCLASS_INLINE);
14619 /* Now, parse the body of the function. */
14620 cp_parser_function_definition_after_declarator (parser,
14621 /*inline_p=*/true);
14623 /* Leave the scope of the containing function. */
14624 if (function_scope)
14625 pop_function_context_from (function_scope);
14626 /* Restore the lexer. */
14627 parser->lexer = saved_lexer;
14630 /* Remove any template parameters from the symbol table. */
14631 maybe_end_member_template_processing ();
14633 /* Restore the queue. */
14634 parser->unparsed_functions_queues
14635 = TREE_CHAIN (parser->unparsed_functions_queues);
14638 /* If DECL contains any default args, remember it on the unparsed
14639 functions queue. */
14641 static void
14642 cp_parser_save_default_args (cp_parser* parser, tree decl)
14644 tree probe;
14646 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14647 probe;
14648 probe = TREE_CHAIN (probe))
14649 if (TREE_PURPOSE (probe))
14651 TREE_PURPOSE (parser->unparsed_functions_queues)
14652 = tree_cons (NULL_TREE, decl,
14653 TREE_PURPOSE (parser->unparsed_functions_queues));
14654 break;
14656 return;
14659 /* FN is a FUNCTION_DECL which may contains a parameter with an
14660 unparsed DEFAULT_ARG. Parse the default args now. */
14662 static void
14663 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14665 cp_lexer *saved_lexer;
14666 cp_token_cache *tokens;
14667 bool saved_local_variables_forbidden_p;
14668 tree parameters;
14670 /* While we're parsing the default args, we might (due to the
14671 statement expression extension) encounter more classes. We want
14672 to handle them right away, but we don't want them getting mixed
14673 up with default args that are currently in the queue. */
14674 parser->unparsed_functions_queues
14675 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14677 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14678 parameters;
14679 parameters = TREE_CHAIN (parameters))
14681 if (!TREE_PURPOSE (parameters)
14682 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14683 continue;
14685 /* Save away the current lexer. */
14686 saved_lexer = parser->lexer;
14687 /* Create a new one, using the tokens we have saved. */
14688 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14689 parser->lexer = cp_lexer_new_from_tokens (tokens);
14691 /* Set the current source position to be the location of the
14692 first token in the default argument. */
14693 cp_lexer_peek_token (parser->lexer);
14695 /* Local variable names (and the `this' keyword) may not appear
14696 in a default argument. */
14697 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14698 parser->local_variables_forbidden_p = true;
14699 /* Parse the assignment-expression. */
14700 if (DECL_CLASS_SCOPE_P (fn))
14701 push_nested_class (DECL_CONTEXT (fn));
14702 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14703 if (DECL_CLASS_SCOPE_P (fn))
14704 pop_nested_class ();
14706 /* If the token stream has not been completely used up, then
14707 there was extra junk after the end of the default
14708 argument. */
14709 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14710 cp_parser_error (parser, "expected `,'");
14712 /* Restore saved state. */
14713 parser->lexer = saved_lexer;
14714 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14717 /* Restore the queue. */
14718 parser->unparsed_functions_queues
14719 = TREE_CHAIN (parser->unparsed_functions_queues);
14722 /* Parse the operand of `sizeof' (or a similar operator). Returns
14723 either a TYPE or an expression, depending on the form of the
14724 input. The KEYWORD indicates which kind of expression we have
14725 encountered. */
14727 static tree
14728 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14730 static const char *format;
14731 tree expr = NULL_TREE;
14732 const char *saved_message;
14733 bool saved_integral_constant_expression_p;
14735 /* Initialize FORMAT the first time we get here. */
14736 if (!format)
14737 format = "types may not be defined in `%s' expressions";
14739 /* Types cannot be defined in a `sizeof' expression. Save away the
14740 old message. */
14741 saved_message = parser->type_definition_forbidden_message;
14742 /* And create the new one. */
14743 parser->type_definition_forbidden_message
14744 = xmalloc (strlen (format)
14745 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14746 + 1 /* `\0' */);
14747 sprintf ((char *) parser->type_definition_forbidden_message,
14748 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14750 /* The restrictions on constant-expressions do not apply inside
14751 sizeof expressions. */
14752 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14753 parser->integral_constant_expression_p = false;
14755 /* Do not actually evaluate the expression. */
14756 ++skip_evaluation;
14757 /* If it's a `(', then we might be looking at the type-id
14758 construction. */
14759 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14761 tree type;
14762 bool saved_in_type_id_in_expr_p;
14764 /* We can't be sure yet whether we're looking at a type-id or an
14765 expression. */
14766 cp_parser_parse_tentatively (parser);
14767 /* Consume the `('. */
14768 cp_lexer_consume_token (parser->lexer);
14769 /* Parse the type-id. */
14770 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14771 parser->in_type_id_in_expr_p = true;
14772 type = cp_parser_type_id (parser);
14773 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14774 /* Now, look for the trailing `)'. */
14775 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14776 /* If all went well, then we're done. */
14777 if (cp_parser_parse_definitely (parser))
14779 /* Build a list of decl-specifiers; right now, we have only
14780 a single type-specifier. */
14781 type = build_tree_list (NULL_TREE,
14782 type);
14784 /* Call grokdeclarator to figure out what type this is. */
14785 expr = grokdeclarator (NULL_TREE,
14786 type,
14787 TYPENAME,
14788 /*initialized=*/0,
14789 /*attrlist=*/NULL);
14793 /* If the type-id production did not work out, then we must be
14794 looking at the unary-expression production. */
14795 if (!expr)
14796 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14797 /* Go back to evaluating expressions. */
14798 --skip_evaluation;
14800 /* Free the message we created. */
14801 free ((char *) parser->type_definition_forbidden_message);
14802 /* And restore the old one. */
14803 parser->type_definition_forbidden_message = saved_message;
14804 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14806 return expr;
14809 /* If the current declaration has no declarator, return true. */
14811 static bool
14812 cp_parser_declares_only_class_p (cp_parser *parser)
14814 /* If the next token is a `;' or a `,' then there is no
14815 declarator. */
14816 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14817 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14820 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14821 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14823 static bool
14824 cp_parser_friend_p (tree decl_specifiers)
14826 while (decl_specifiers)
14828 /* See if this decl-specifier is `friend'. */
14829 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14830 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14831 return true;
14833 /* Go on to the next decl-specifier. */
14834 decl_specifiers = TREE_CHAIN (decl_specifiers);
14837 return false;
14840 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14841 issue an error message indicating that TOKEN_DESC was expected.
14843 Returns the token consumed, if the token had the appropriate type.
14844 Otherwise, returns NULL. */
14846 static cp_token *
14847 cp_parser_require (cp_parser* parser,
14848 enum cpp_ttype type,
14849 const char* token_desc)
14851 if (cp_lexer_next_token_is (parser->lexer, type))
14852 return cp_lexer_consume_token (parser->lexer);
14853 else
14855 /* Output the MESSAGE -- unless we're parsing tentatively. */
14856 if (!cp_parser_simulate_error (parser))
14858 char *message = concat ("expected ", token_desc, NULL);
14859 cp_parser_error (parser, message);
14860 free (message);
14862 return NULL;
14866 /* Like cp_parser_require, except that tokens will be skipped until
14867 the desired token is found. An error message is still produced if
14868 the next token is not as expected. */
14870 static void
14871 cp_parser_skip_until_found (cp_parser* parser,
14872 enum cpp_ttype type,
14873 const char* token_desc)
14875 cp_token *token;
14876 unsigned nesting_depth = 0;
14878 if (cp_parser_require (parser, type, token_desc))
14879 return;
14881 /* Skip tokens until the desired token is found. */
14882 while (true)
14884 /* Peek at the next token. */
14885 token = cp_lexer_peek_token (parser->lexer);
14886 /* If we've reached the token we want, consume it and
14887 stop. */
14888 if (token->type == type && !nesting_depth)
14890 cp_lexer_consume_token (parser->lexer);
14891 return;
14893 /* If we've run out of tokens, stop. */
14894 if (token->type == CPP_EOF)
14895 return;
14896 if (token->type == CPP_OPEN_BRACE
14897 || token->type == CPP_OPEN_PAREN
14898 || token->type == CPP_OPEN_SQUARE)
14899 ++nesting_depth;
14900 else if (token->type == CPP_CLOSE_BRACE
14901 || token->type == CPP_CLOSE_PAREN
14902 || token->type == CPP_CLOSE_SQUARE)
14904 if (nesting_depth-- == 0)
14905 return;
14907 /* Consume this token. */
14908 cp_lexer_consume_token (parser->lexer);
14912 /* If the next token is the indicated keyword, consume it. Otherwise,
14913 issue an error message indicating that TOKEN_DESC was expected.
14915 Returns the token consumed, if the token had the appropriate type.
14916 Otherwise, returns NULL. */
14918 static cp_token *
14919 cp_parser_require_keyword (cp_parser* parser,
14920 enum rid keyword,
14921 const char* token_desc)
14923 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14925 if (token && token->keyword != keyword)
14927 dyn_string_t error_msg;
14929 /* Format the error message. */
14930 error_msg = dyn_string_new (0);
14931 dyn_string_append_cstr (error_msg, "expected ");
14932 dyn_string_append_cstr (error_msg, token_desc);
14933 cp_parser_error (parser, error_msg->s);
14934 dyn_string_delete (error_msg);
14935 return NULL;
14938 return token;
14941 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14942 function-definition. */
14944 static bool
14945 cp_parser_token_starts_function_definition_p (cp_token* token)
14947 return (/* An ordinary function-body begins with an `{'. */
14948 token->type == CPP_OPEN_BRACE
14949 /* A ctor-initializer begins with a `:'. */
14950 || token->type == CPP_COLON
14951 /* A function-try-block begins with `try'. */
14952 || token->keyword == RID_TRY
14953 /* The named return value extension begins with `return'. */
14954 || token->keyword == RID_RETURN);
14957 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14958 definition. */
14960 static bool
14961 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14963 cp_token *token;
14965 token = cp_lexer_peek_token (parser->lexer);
14966 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14969 /* Returns TRUE iff the next token is the "," or ">" ending a
14970 template-argument. ">>" is also accepted (after the full
14971 argument was parsed) because it's probably a typo for "> >",
14972 and there is a specific diagnostic for this. */
14974 static bool
14975 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
14977 cp_token *token;
14979 token = cp_lexer_peek_token (parser->lexer);
14980 return (token->type == CPP_COMMA || token->type == CPP_GREATER
14981 || token->type == CPP_RSHIFT);
14984 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
14985 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
14987 static bool
14988 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
14989 size_t n)
14991 cp_token *token;
14993 token = cp_lexer_peek_nth_token (parser->lexer, n);
14994 if (token->type == CPP_LESS)
14995 return true;
14996 /* Check for the sequence `<::' in the original code. It would be lexed as
14997 `[:', where `[' is a digraph, and there is no whitespace before
14998 `:'. */
14999 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15001 cp_token *token2;
15002 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15003 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15004 return true;
15006 return false;
15009 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15010 or none_type otherwise. */
15012 static enum tag_types
15013 cp_parser_token_is_class_key (cp_token* token)
15015 switch (token->keyword)
15017 case RID_CLASS:
15018 return class_type;
15019 case RID_STRUCT:
15020 return record_type;
15021 case RID_UNION:
15022 return union_type;
15024 default:
15025 return none_type;
15029 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15031 static void
15032 cp_parser_check_class_key (enum tag_types class_key, tree type)
15034 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15035 pedwarn ("`%s' tag used in naming `%#T'",
15036 class_key == union_type ? "union"
15037 : class_key == record_type ? "struct" : "class",
15038 type);
15041 /* Issue an error message if DECL is redeclared with different
15042 access than its original declaration [class.access.spec/3].
15043 This applies to nested classes and nested class templates.
15044 [class.mem/1]. */
15046 static void cp_parser_check_access_in_redeclaration (tree decl)
15048 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15049 return;
15051 if ((TREE_PRIVATE (decl)
15052 != (current_access_specifier == access_private_node))
15053 || (TREE_PROTECTED (decl)
15054 != (current_access_specifier == access_protected_node)))
15055 error ("%D redeclared with different access", decl);
15058 /* Look for the `template' keyword, as a syntactic disambiguator.
15059 Return TRUE iff it is present, in which case it will be
15060 consumed. */
15062 static bool
15063 cp_parser_optional_template_keyword (cp_parser *parser)
15065 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15067 /* The `template' keyword can only be used within templates;
15068 outside templates the parser can always figure out what is a
15069 template and what is not. */
15070 if (!processing_template_decl)
15072 error ("`template' (as a disambiguator) is only allowed "
15073 "within templates");
15074 /* If this part of the token stream is rescanned, the same
15075 error message would be generated. So, we purge the token
15076 from the stream. */
15077 cp_lexer_purge_token (parser->lexer);
15078 return false;
15080 else
15082 /* Consume the `template' keyword. */
15083 cp_lexer_consume_token (parser->lexer);
15084 return true;
15088 return false;
15091 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15092 set PARSER->SCOPE, and perform other related actions. */
15094 static void
15095 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15097 tree value;
15098 tree check;
15100 /* Get the stored value. */
15101 value = cp_lexer_consume_token (parser->lexer)->value;
15102 /* Perform any access checks that were deferred. */
15103 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15104 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15105 /* Set the scope from the stored value. */
15106 parser->scope = TREE_VALUE (value);
15107 parser->qualifying_scope = TREE_TYPE (value);
15108 parser->object_scope = NULL_TREE;
15111 /* Add tokens to CACHE until an non-nested END token appears. */
15113 static void
15114 cp_parser_cache_group (cp_parser *parser,
15115 cp_token_cache *cache,
15116 enum cpp_ttype end,
15117 unsigned depth)
15119 while (true)
15121 cp_token *token;
15123 /* Abort a parenthesized expression if we encounter a brace. */
15124 if ((end == CPP_CLOSE_PAREN || depth == 0)
15125 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15126 return;
15127 /* If we've reached the end of the file, stop. */
15128 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15129 return;
15130 /* Consume the next token. */
15131 token = cp_lexer_consume_token (parser->lexer);
15132 /* Add this token to the tokens we are saving. */
15133 cp_token_cache_push_token (cache, token);
15134 /* See if it starts a new group. */
15135 if (token->type == CPP_OPEN_BRACE)
15137 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15138 if (depth == 0)
15139 return;
15141 else if (token->type == CPP_OPEN_PAREN)
15142 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15143 else if (token->type == end)
15144 return;
15148 /* Begin parsing tentatively. We always save tokens while parsing
15149 tentatively so that if the tentative parsing fails we can restore the
15150 tokens. */
15152 static void
15153 cp_parser_parse_tentatively (cp_parser* parser)
15155 /* Enter a new parsing context. */
15156 parser->context = cp_parser_context_new (parser->context);
15157 /* Begin saving tokens. */
15158 cp_lexer_save_tokens (parser->lexer);
15159 /* In order to avoid repetitive access control error messages,
15160 access checks are queued up until we are no longer parsing
15161 tentatively. */
15162 push_deferring_access_checks (dk_deferred);
15165 /* Commit to the currently active tentative parse. */
15167 static void
15168 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15170 cp_parser_context *context;
15171 cp_lexer *lexer;
15173 /* Mark all of the levels as committed. */
15174 lexer = parser->lexer;
15175 for (context = parser->context; context->next; context = context->next)
15177 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15178 break;
15179 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15180 while (!cp_lexer_saving_tokens (lexer))
15181 lexer = lexer->next;
15182 cp_lexer_commit_tokens (lexer);
15186 /* Abort the currently active tentative parse. All consumed tokens
15187 will be rolled back, and no diagnostics will be issued. */
15189 static void
15190 cp_parser_abort_tentative_parse (cp_parser* parser)
15192 cp_parser_simulate_error (parser);
15193 /* Now, pretend that we want to see if the construct was
15194 successfully parsed. */
15195 cp_parser_parse_definitely (parser);
15198 /* Stop parsing tentatively. If a parse error has occurred, restore the
15199 token stream. Otherwise, commit to the tokens we have consumed.
15200 Returns true if no error occurred; false otherwise. */
15202 static bool
15203 cp_parser_parse_definitely (cp_parser* parser)
15205 bool error_occurred;
15206 cp_parser_context *context;
15208 /* Remember whether or not an error occurred, since we are about to
15209 destroy that information. */
15210 error_occurred = cp_parser_error_occurred (parser);
15211 /* Remove the topmost context from the stack. */
15212 context = parser->context;
15213 parser->context = context->next;
15214 /* If no parse errors occurred, commit to the tentative parse. */
15215 if (!error_occurred)
15217 /* Commit to the tokens read tentatively, unless that was
15218 already done. */
15219 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15220 cp_lexer_commit_tokens (parser->lexer);
15222 pop_to_parent_deferring_access_checks ();
15224 /* Otherwise, if errors occurred, roll back our state so that things
15225 are just as they were before we began the tentative parse. */
15226 else
15228 cp_lexer_rollback_tokens (parser->lexer);
15229 pop_deferring_access_checks ();
15231 /* Add the context to the front of the free list. */
15232 context->next = cp_parser_context_free_list;
15233 cp_parser_context_free_list = context;
15235 return !error_occurred;
15238 /* Returns true if we are parsing tentatively -- but have decided that
15239 we will stick with this tentative parse, even if errors occur. */
15241 static bool
15242 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15244 return (cp_parser_parsing_tentatively (parser)
15245 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15248 /* Returns nonzero iff an error has occurred during the most recent
15249 tentative parse. */
15251 static bool
15252 cp_parser_error_occurred (cp_parser* parser)
15254 return (cp_parser_parsing_tentatively (parser)
15255 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15258 /* Returns nonzero if GNU extensions are allowed. */
15260 static bool
15261 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15263 return parser->allow_gnu_extensions_p;
15268 /* The parser. */
15270 static GTY (()) cp_parser *the_parser;
15272 /* External interface. */
15274 /* Parse one entire translation unit. */
15276 void
15277 c_parse_file (void)
15279 bool error_occurred;
15281 the_parser = cp_parser_new ();
15282 push_deferring_access_checks (flag_access_control
15283 ? dk_no_deferred : dk_no_check);
15284 error_occurred = cp_parser_translation_unit (the_parser);
15285 the_parser = NULL;
15288 /* This variable must be provided by every front end. */
15290 int yydebug;
15292 #include "gt-cp-parser.h"