PR c++/15064
[official-gcc.git] / gcc / cp / parser.c
blob6fb8da5d158c07a09a02063a54b57d112d9b64cb
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 declarator. */
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 void cp_parser_diagnose_invalid_type_name
1725 (cp_parser *, tree, tree);
1726 static bool cp_parser_parse_and_diagnose_invalid_type_name
1727 (cp_parser *);
1728 static int cp_parser_skip_to_closing_parenthesis
1729 (cp_parser *, bool, bool, bool);
1730 static void cp_parser_skip_to_end_of_statement
1731 (cp_parser *);
1732 static void cp_parser_consume_semicolon_at_end_of_statement
1733 (cp_parser *);
1734 static void cp_parser_skip_to_end_of_block_or_statement
1735 (cp_parser *);
1736 static void cp_parser_skip_to_closing_brace
1737 (cp_parser *);
1738 static void cp_parser_skip_until_found
1739 (cp_parser *, enum cpp_ttype, const char *);
1740 static bool cp_parser_error_occurred
1741 (cp_parser *);
1742 static bool cp_parser_allow_gnu_extensions_p
1743 (cp_parser *);
1744 static bool cp_parser_is_string_literal
1745 (cp_token *);
1746 static bool cp_parser_is_keyword
1747 (cp_token *, enum rid);
1748 static tree cp_parser_make_typename_type
1749 (cp_parser *, tree, tree);
1751 /* Returns nonzero if we are parsing tentatively. */
1753 static inline bool
1754 cp_parser_parsing_tentatively (cp_parser* parser)
1756 return parser->context->next != NULL;
1759 /* Returns nonzero if TOKEN is a string literal. */
1761 static bool
1762 cp_parser_is_string_literal (cp_token* token)
1764 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1767 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1769 static bool
1770 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1772 return token->keyword == keyword;
1775 /* Issue the indicated error MESSAGE. */
1777 static void
1778 cp_parser_error (cp_parser* parser, const char* message)
1780 /* Output the MESSAGE -- unless we're parsing tentatively. */
1781 if (!cp_parser_simulate_error (parser))
1783 cp_token *token;
1784 token = cp_lexer_peek_token (parser->lexer);
1785 c_parse_error (message,
1786 /* Because c_parser_error does not understand
1787 CPP_KEYWORD, keywords are treated like
1788 identifiers. */
1789 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1790 token->value);
1794 /* Issue an error about name-lookup failing. NAME is the
1795 IDENTIFIER_NODE DECL is the result of
1796 the lookup (as returned from cp_parser_lookup_name). DESIRED is
1797 the thing that we hoped to find. */
1799 static void
1800 cp_parser_name_lookup_error (cp_parser* parser,
1801 tree name,
1802 tree decl,
1803 const char* desired)
1805 /* If name lookup completely failed, tell the user that NAME was not
1806 declared. */
1807 if (decl == error_mark_node)
1809 if (parser->scope && parser->scope != global_namespace)
1810 error ("`%D::%D' has not been declared",
1811 parser->scope, name);
1812 else if (parser->scope == global_namespace)
1813 error ("`::%D' has not been declared", name);
1814 else
1815 error ("`%D' has not been declared", name);
1817 else if (parser->scope && parser->scope != global_namespace)
1818 error ("`%D::%D' %s", parser->scope, name, desired);
1819 else if (parser->scope == global_namespace)
1820 error ("`::%D' %s", name, desired);
1821 else
1822 error ("`%D' %s", name, desired);
1825 /* If we are parsing tentatively, remember that an error has occurred
1826 during this tentative parse. Returns true if the error was
1827 simulated; false if a message should be issued by the caller. */
1829 static bool
1830 cp_parser_simulate_error (cp_parser* parser)
1832 if (cp_parser_parsing_tentatively (parser)
1833 && !cp_parser_committed_to_tentative_parse (parser))
1835 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1836 return true;
1838 return false;
1841 /* This function is called when a type is defined. If type
1842 definitions are forbidden at this point, an error message is
1843 issued. */
1845 static void
1846 cp_parser_check_type_definition (cp_parser* parser)
1848 /* If types are forbidden here, issue a message. */
1849 if (parser->type_definition_forbidden_message)
1850 /* Use `%s' to print the string in case there are any escape
1851 characters in the message. */
1852 error ("%s", parser->type_definition_forbidden_message);
1855 /* This function is called when a declaration is parsed. If
1856 DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1857 indicates that a type was defined in the decl-specifiers for DECL,
1858 then an error is issued. */
1860 static void
1861 cp_parser_check_for_definition_in_return_type (tree declarator,
1862 int declares_class_or_enum)
1864 /* [dcl.fct] forbids type definitions in return types.
1865 Unfortunately, it's not easy to know whether or not we are
1866 processing a return type until after the fact. */
1867 while (declarator
1868 && (TREE_CODE (declarator) == INDIRECT_REF
1869 || TREE_CODE (declarator) == ADDR_EXPR))
1870 declarator = TREE_OPERAND (declarator, 0);
1871 if (declarator
1872 && TREE_CODE (declarator) == CALL_EXPR
1873 && declares_class_or_enum & 2)
1874 error ("new types may not be defined in a return type");
1877 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1878 "<" in any valid C++ program. If the next token is indeed "<",
1879 issue a message warning the user about what appears to be an
1880 invalid attempt to form a template-id. */
1882 static void
1883 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1884 tree type)
1886 ptrdiff_t start;
1887 cp_token *token;
1889 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1891 if (TYPE_P (type))
1892 error ("`%T' is not a template", type);
1893 else if (TREE_CODE (type) == IDENTIFIER_NODE)
1894 error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1895 else
1896 error ("invalid template-id");
1897 /* Remember the location of the invalid "<". */
1898 if (cp_parser_parsing_tentatively (parser)
1899 && !cp_parser_committed_to_tentative_parse (parser))
1901 token = cp_lexer_peek_token (parser->lexer);
1902 token = cp_lexer_prev_token (parser->lexer, token);
1903 start = cp_lexer_token_difference (parser->lexer,
1904 parser->lexer->first_token,
1905 token);
1907 else
1908 start = -1;
1909 /* Consume the "<". */
1910 cp_lexer_consume_token (parser->lexer);
1911 /* Parse the template arguments. */
1912 cp_parser_enclosed_template_argument_list (parser);
1913 /* Permanently remove the invalid template arguments so that
1914 this error message is not issued again. */
1915 if (start >= 0)
1917 token = cp_lexer_advance_token (parser->lexer,
1918 parser->lexer->first_token,
1919 start);
1920 cp_lexer_purge_tokens_after (parser->lexer, token);
1925 /* If parsing an integral constant-expression, issue an error message
1926 about the fact that THING appeared and return true. Otherwise,
1927 return false, marking the current expression as non-constant. */
1929 static bool
1930 cp_parser_non_integral_constant_expression (cp_parser *parser,
1931 const char *thing)
1933 if (parser->integral_constant_expression_p)
1935 if (!parser->allow_non_integral_constant_expression_p)
1937 error ("%s cannot appear in a constant-expression", thing);
1938 return true;
1940 parser->non_integral_constant_expression_p = true;
1942 return false;
1945 /* Emit a diagnostic for an invalid type name. Consider also if it is
1946 qualified or not and the result of a lookup, to provide a better
1947 message. */
1949 static void
1950 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1952 tree decl, old_scope;
1953 /* Try to lookup the identifier. */
1954 old_scope = parser->scope;
1955 parser->scope = scope;
1956 decl = cp_parser_lookup_name_simple (parser, id);
1957 parser->scope = old_scope;
1958 /* If the lookup found a template-name, it means that the user forgot
1959 to specify an argument list. Emit an useful error message. */
1960 if (TREE_CODE (decl) == TEMPLATE_DECL)
1961 error ("invalid use of template-name `%E' without an argument list",
1962 decl);
1963 else if (!parser->scope)
1965 /* Issue an error message. */
1966 error ("`%E' does not name a type", id);
1967 /* If we're in a template class, it's possible that the user was
1968 referring to a type from a base class. For example:
1970 template <typename T> struct A { typedef T X; };
1971 template <typename T> struct B : public A<T> { X x; };
1973 The user should have said "typename A<T>::X". */
1974 if (processing_template_decl && current_class_type)
1976 tree b;
1978 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1980 b = TREE_CHAIN (b))
1982 tree base_type = BINFO_TYPE (b);
1983 if (CLASS_TYPE_P (base_type)
1984 && dependent_type_p (base_type))
1986 tree field;
1987 /* Go from a particular instantiation of the
1988 template (which will have an empty TYPE_FIELDs),
1989 to the main version. */
1990 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1991 for (field = TYPE_FIELDS (base_type);
1992 field;
1993 field = TREE_CHAIN (field))
1994 if (TREE_CODE (field) == TYPE_DECL
1995 && DECL_NAME (field) == id)
1997 inform ("(perhaps `typename %T::%E' was intended)",
1998 BINFO_TYPE (b), id);
1999 break;
2001 if (field)
2002 break;
2007 /* Here we diagnose qualified-ids where the scope is actually correct,
2008 but the identifier does not resolve to a valid type name. */
2009 else
2011 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2012 error ("`%E' in namespace `%E' does not name a type",
2013 id, parser->scope);
2014 else if (TYPE_P (parser->scope))
2015 error ("`%E' in class `%T' does not name a type",
2016 id, parser->scope);
2017 else
2018 abort();
2022 /* Check for a common situation where a type-name should be present,
2023 but is not, and issue a sensible error message. Returns true if an
2024 invalid type-name was detected.
2026 The situation handled by this function are variable declarations of the
2027 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2028 Usually, `ID' should name a type, but if we got here it means that it
2029 does not. We try to emit the best possible error message depending on
2030 how exactly the id-expression looks like.
2033 static bool
2034 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2036 tree id;
2038 cp_parser_parse_tentatively (parser);
2039 id = cp_parser_id_expression (parser,
2040 /*template_keyword_p=*/false,
2041 /*check_dependency_p=*/true,
2042 /*template_p=*/NULL,
2043 /*declarator_p=*/true);
2044 /* After the id-expression, there should be a plain identifier,
2045 otherwise this is not a simple variable declaration. Also, if
2046 the scope is dependent, we cannot do much. */
2047 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2048 || (parser->scope && TYPE_P (parser->scope)
2049 && dependent_type_p (parser->scope)))
2051 cp_parser_abort_tentative_parse (parser);
2052 return false;
2054 if (!cp_parser_parse_definitely (parser))
2055 return false;
2057 /* If we got here, this cannot be a valid variable declaration, thus
2058 the cp_parser_id_expression must have resolved to a plain identifier
2059 node (not a TYPE_DECL or TEMPLATE_ID_EXPR). */
2060 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2061 /* Emit a diagnostic for the invalid type. */
2062 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2063 /* Skip to the end of the declaration; there's no point in
2064 trying to process it. */
2065 cp_parser_skip_to_end_of_block_or_statement (parser);
2066 return true;
2069 /* Consume tokens up to, and including, the next non-nested closing `)'.
2070 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2071 are doing error recovery. Returns -1 if OR_COMMA is true and we
2072 found an unnested comma. */
2074 static int
2075 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2076 bool recovering,
2077 bool or_comma,
2078 bool consume_paren)
2080 unsigned paren_depth = 0;
2081 unsigned brace_depth = 0;
2083 if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2084 && !cp_parser_committed_to_tentative_parse (parser))
2085 return 0;
2087 while (true)
2089 cp_token *token;
2091 /* If we've run out of tokens, then there is no closing `)'. */
2092 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2093 return 0;
2095 token = cp_lexer_peek_token (parser->lexer);
2097 /* This matches the processing in skip_to_end_of_statement. */
2098 if (token->type == CPP_SEMICOLON && !brace_depth)
2099 return 0;
2100 if (token->type == CPP_OPEN_BRACE)
2101 ++brace_depth;
2102 if (token->type == CPP_CLOSE_BRACE)
2104 if (!brace_depth--)
2105 return 0;
2107 if (recovering && or_comma && token->type == CPP_COMMA
2108 && !brace_depth && !paren_depth)
2109 return -1;
2111 if (!brace_depth)
2113 /* If it is an `(', we have entered another level of nesting. */
2114 if (token->type == CPP_OPEN_PAREN)
2115 ++paren_depth;
2116 /* If it is a `)', then we might be done. */
2117 else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2119 if (consume_paren)
2120 cp_lexer_consume_token (parser->lexer);
2121 return 1;
2125 /* Consume the token. */
2126 cp_lexer_consume_token (parser->lexer);
2130 /* Consume tokens until we reach the end of the current statement.
2131 Normally, that will be just before consuming a `;'. However, if a
2132 non-nested `}' comes first, then we stop before consuming that. */
2134 static void
2135 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2137 unsigned nesting_depth = 0;
2139 while (true)
2141 cp_token *token;
2143 /* Peek at the next token. */
2144 token = cp_lexer_peek_token (parser->lexer);
2145 /* If we've run out of tokens, stop. */
2146 if (token->type == CPP_EOF)
2147 break;
2148 /* If the next token is a `;', we have reached the end of the
2149 statement. */
2150 if (token->type == CPP_SEMICOLON && !nesting_depth)
2151 break;
2152 /* If the next token is a non-nested `}', then we have reached
2153 the end of the current block. */
2154 if (token->type == CPP_CLOSE_BRACE)
2156 /* If this is a non-nested `}', stop before consuming it.
2157 That way, when confronted with something like:
2159 { 3 + }
2161 we stop before consuming the closing `}', even though we
2162 have not yet reached a `;'. */
2163 if (nesting_depth == 0)
2164 break;
2165 /* If it is the closing `}' for a block that we have
2166 scanned, stop -- but only after consuming the token.
2167 That way given:
2169 void f g () { ... }
2170 typedef int I;
2172 we will stop after the body of the erroneously declared
2173 function, but before consuming the following `typedef'
2174 declaration. */
2175 if (--nesting_depth == 0)
2177 cp_lexer_consume_token (parser->lexer);
2178 break;
2181 /* If it the next token is a `{', then we are entering a new
2182 block. Consume the entire block. */
2183 else if (token->type == CPP_OPEN_BRACE)
2184 ++nesting_depth;
2185 /* Consume the token. */
2186 cp_lexer_consume_token (parser->lexer);
2190 /* This function is called at the end of a statement or declaration.
2191 If the next token is a semicolon, it is consumed; otherwise, error
2192 recovery is attempted. */
2194 static void
2195 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2197 /* Look for the trailing `;'. */
2198 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2200 /* If there is additional (erroneous) input, skip to the end of
2201 the statement. */
2202 cp_parser_skip_to_end_of_statement (parser);
2203 /* If the next token is now a `;', consume it. */
2204 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2205 cp_lexer_consume_token (parser->lexer);
2209 /* Skip tokens until we have consumed an entire block, or until we
2210 have consumed a non-nested `;'. */
2212 static void
2213 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2215 unsigned nesting_depth = 0;
2217 while (true)
2219 cp_token *token;
2221 /* Peek at the next token. */
2222 token = cp_lexer_peek_token (parser->lexer);
2223 /* If we've run out of tokens, stop. */
2224 if (token->type == CPP_EOF)
2225 break;
2226 /* If the next token is a `;', we have reached the end of the
2227 statement. */
2228 if (token->type == CPP_SEMICOLON && !nesting_depth)
2230 /* Consume the `;'. */
2231 cp_lexer_consume_token (parser->lexer);
2232 break;
2234 /* Consume the token. */
2235 token = cp_lexer_consume_token (parser->lexer);
2236 /* If the next token is a non-nested `}', then we have reached
2237 the end of the current block. */
2238 if (token->type == CPP_CLOSE_BRACE
2239 && (nesting_depth == 0 || --nesting_depth == 0))
2240 break;
2241 /* If it the next token is a `{', then we are entering a new
2242 block. Consume the entire block. */
2243 if (token->type == CPP_OPEN_BRACE)
2244 ++nesting_depth;
2248 /* Skip tokens until a non-nested closing curly brace is the next
2249 token. */
2251 static void
2252 cp_parser_skip_to_closing_brace (cp_parser *parser)
2254 unsigned nesting_depth = 0;
2256 while (true)
2258 cp_token *token;
2260 /* Peek at the next token. */
2261 token = cp_lexer_peek_token (parser->lexer);
2262 /* If we've run out of tokens, stop. */
2263 if (token->type == CPP_EOF)
2264 break;
2265 /* If the next token is a non-nested `}', then we have reached
2266 the end of the current block. */
2267 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2268 break;
2269 /* If it the next token is a `{', then we are entering a new
2270 block. Consume the entire block. */
2271 else if (token->type == CPP_OPEN_BRACE)
2272 ++nesting_depth;
2273 /* Consume the token. */
2274 cp_lexer_consume_token (parser->lexer);
2278 /* This is a simple wrapper around make_typename_type. When the id is
2279 an unresolved identifier node, we can provide a superior diagnostic
2280 using cp_parser_diagnose_invalid_type_name. */
2282 static tree
2283 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2285 tree result;
2286 if (TREE_CODE (id) == IDENTIFIER_NODE)
2288 result = make_typename_type (scope, id, /*complain=*/0);
2289 if (result == error_mark_node)
2290 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2291 return result;
2293 return make_typename_type (scope, id, tf_error);
2297 /* Create a new C++ parser. */
2299 static cp_parser *
2300 cp_parser_new (void)
2302 cp_parser *parser;
2303 cp_lexer *lexer;
2305 /* cp_lexer_new_main is called before calling ggc_alloc because
2306 cp_lexer_new_main might load a PCH file. */
2307 lexer = cp_lexer_new_main ();
2309 parser = ggc_alloc_cleared (sizeof (cp_parser));
2310 parser->lexer = lexer;
2311 parser->context = cp_parser_context_new (NULL);
2313 /* For now, we always accept GNU extensions. */
2314 parser->allow_gnu_extensions_p = 1;
2316 /* The `>' token is a greater-than operator, not the end of a
2317 template-id. */
2318 parser->greater_than_is_operator_p = true;
2320 parser->default_arg_ok_p = true;
2322 /* We are not parsing a constant-expression. */
2323 parser->integral_constant_expression_p = false;
2324 parser->allow_non_integral_constant_expression_p = false;
2325 parser->non_integral_constant_expression_p = false;
2327 /* We are not parsing offsetof. */
2328 parser->in_offsetof_p = false;
2330 /* Local variable names are not forbidden. */
2331 parser->local_variables_forbidden_p = false;
2333 /* We are not processing an `extern "C"' declaration. */
2334 parser->in_unbraced_linkage_specification_p = false;
2336 /* We are not processing a declarator. */
2337 parser->in_declarator_p = false;
2339 /* We are not processing a template-argument-list. */
2340 parser->in_template_argument_list_p = false;
2342 /* We are not in an iteration statement. */
2343 parser->in_iteration_statement_p = false;
2345 /* We are not in a switch statement. */
2346 parser->in_switch_statement_p = false;
2348 /* We are not parsing a type-id inside an expression. */
2349 parser->in_type_id_in_expr_p = false;
2351 /* The unparsed function queue is empty. */
2352 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2354 /* There are no classes being defined. */
2355 parser->num_classes_being_defined = 0;
2357 /* No template parameters apply. */
2358 parser->num_template_parameter_lists = 0;
2360 return parser;
2363 /* Lexical conventions [gram.lex] */
2365 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2366 identifier. */
2368 static tree
2369 cp_parser_identifier (cp_parser* parser)
2371 cp_token *token;
2373 /* Look for the identifier. */
2374 token = cp_parser_require (parser, CPP_NAME, "identifier");
2375 /* Return the value. */
2376 return token ? token->value : error_mark_node;
2379 /* Basic concepts [gram.basic] */
2381 /* Parse a translation-unit.
2383 translation-unit:
2384 declaration-seq [opt]
2386 Returns TRUE if all went well. */
2388 static bool
2389 cp_parser_translation_unit (cp_parser* parser)
2391 while (true)
2393 cp_parser_declaration_seq_opt (parser);
2395 /* If there are no tokens left then all went well. */
2396 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2397 break;
2399 /* Otherwise, issue an error message. */
2400 cp_parser_error (parser, "expected declaration");
2401 return false;
2404 /* Consume the EOF token. */
2405 cp_parser_require (parser, CPP_EOF, "end-of-file");
2407 /* Finish up. */
2408 finish_translation_unit ();
2410 /* All went well. */
2411 return true;
2414 /* Expressions [gram.expr] */
2416 /* Parse a primary-expression.
2418 primary-expression:
2419 literal
2420 this
2421 ( expression )
2422 id-expression
2424 GNU Extensions:
2426 primary-expression:
2427 ( compound-statement )
2428 __builtin_va_arg ( assignment-expression , type-id )
2430 literal:
2431 __null
2433 Returns a representation of the expression.
2435 *IDK indicates what kind of id-expression (if any) was present.
2437 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2438 used as the operand of a pointer-to-member. In that case,
2439 *QUALIFYING_CLASS gives the class that is used as the qualifying
2440 class in the pointer-to-member. */
2442 static tree
2443 cp_parser_primary_expression (cp_parser *parser,
2444 cp_id_kind *idk,
2445 tree *qualifying_class)
2447 cp_token *token;
2449 /* Assume the primary expression is not an id-expression. */
2450 *idk = CP_ID_KIND_NONE;
2451 /* And that it cannot be used as pointer-to-member. */
2452 *qualifying_class = NULL_TREE;
2454 /* Peek at the next token. */
2455 token = cp_lexer_peek_token (parser->lexer);
2456 switch (token->type)
2458 /* literal:
2459 integer-literal
2460 character-literal
2461 floating-literal
2462 string-literal
2463 boolean-literal */
2464 case CPP_CHAR:
2465 case CPP_WCHAR:
2466 case CPP_STRING:
2467 case CPP_WSTRING:
2468 case CPP_NUMBER:
2469 token = cp_lexer_consume_token (parser->lexer);
2470 return token->value;
2472 case CPP_OPEN_PAREN:
2474 tree expr;
2475 bool saved_greater_than_is_operator_p;
2477 /* Consume the `('. */
2478 cp_lexer_consume_token (parser->lexer);
2479 /* Within a parenthesized expression, a `>' token is always
2480 the greater-than operator. */
2481 saved_greater_than_is_operator_p
2482 = parser->greater_than_is_operator_p;
2483 parser->greater_than_is_operator_p = true;
2484 /* If we see `( { ' then we are looking at the beginning of
2485 a GNU statement-expression. */
2486 if (cp_parser_allow_gnu_extensions_p (parser)
2487 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2489 /* Statement-expressions are not allowed by the standard. */
2490 if (pedantic)
2491 pedwarn ("ISO C++ forbids braced-groups within expressions");
2493 /* And they're not allowed outside of a function-body; you
2494 cannot, for example, write:
2496 int i = ({ int j = 3; j + 1; });
2498 at class or namespace scope. */
2499 if (!at_function_scope_p ())
2500 error ("statement-expressions are allowed only inside functions");
2501 /* Start the statement-expression. */
2502 expr = begin_stmt_expr ();
2503 /* Parse the compound-statement. */
2504 cp_parser_compound_statement (parser, true);
2505 /* Finish up. */
2506 expr = finish_stmt_expr (expr, false);
2508 else
2510 /* Parse the parenthesized expression. */
2511 expr = cp_parser_expression (parser);
2512 /* Let the front end know that this expression was
2513 enclosed in parentheses. This matters in case, for
2514 example, the expression is of the form `A::B', since
2515 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2516 not. */
2517 finish_parenthesized_expr (expr);
2519 /* The `>' token might be the end of a template-id or
2520 template-parameter-list now. */
2521 parser->greater_than_is_operator_p
2522 = saved_greater_than_is_operator_p;
2523 /* Consume the `)'. */
2524 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2525 cp_parser_skip_to_end_of_statement (parser);
2527 return expr;
2530 case CPP_KEYWORD:
2531 switch (token->keyword)
2533 /* These two are the boolean literals. */
2534 case RID_TRUE:
2535 cp_lexer_consume_token (parser->lexer);
2536 return boolean_true_node;
2537 case RID_FALSE:
2538 cp_lexer_consume_token (parser->lexer);
2539 return boolean_false_node;
2541 /* The `__null' literal. */
2542 case RID_NULL:
2543 cp_lexer_consume_token (parser->lexer);
2544 return null_node;
2546 /* Recognize the `this' keyword. */
2547 case RID_THIS:
2548 cp_lexer_consume_token (parser->lexer);
2549 if (parser->local_variables_forbidden_p)
2551 error ("`this' may not be used in this context");
2552 return error_mark_node;
2554 /* Pointers cannot appear in constant-expressions. */
2555 if (cp_parser_non_integral_constant_expression (parser,
2556 "`this'"))
2557 return error_mark_node;
2558 return finish_this_expr ();
2560 /* The `operator' keyword can be the beginning of an
2561 id-expression. */
2562 case RID_OPERATOR:
2563 goto id_expression;
2565 case RID_FUNCTION_NAME:
2566 case RID_PRETTY_FUNCTION_NAME:
2567 case RID_C99_FUNCTION_NAME:
2568 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2569 __func__ are the names of variables -- but they are
2570 treated specially. Therefore, they are handled here,
2571 rather than relying on the generic id-expression logic
2572 below. Grammatically, these names are id-expressions.
2574 Consume the token. */
2575 token = cp_lexer_consume_token (parser->lexer);
2576 /* Look up the name. */
2577 return finish_fname (token->value);
2579 case RID_VA_ARG:
2581 tree expression;
2582 tree type;
2584 /* The `__builtin_va_arg' construct is used to handle
2585 `va_arg'. Consume the `__builtin_va_arg' token. */
2586 cp_lexer_consume_token (parser->lexer);
2587 /* Look for the opening `('. */
2588 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2589 /* Now, parse the assignment-expression. */
2590 expression = cp_parser_assignment_expression (parser);
2591 /* Look for the `,'. */
2592 cp_parser_require (parser, CPP_COMMA, "`,'");
2593 /* Parse the type-id. */
2594 type = cp_parser_type_id (parser);
2595 /* Look for the closing `)'. */
2596 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2597 /* Using `va_arg' in a constant-expression is not
2598 allowed. */
2599 if (cp_parser_non_integral_constant_expression (parser,
2600 "`va_arg'"))
2601 return error_mark_node;
2602 return build_x_va_arg (expression, type);
2605 case RID_OFFSETOF:
2607 tree expression;
2608 bool saved_in_offsetof_p;
2610 /* Consume the "__offsetof__" token. */
2611 cp_lexer_consume_token (parser->lexer);
2612 /* Consume the opening `('. */
2613 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2614 /* Parse the parenthesized (almost) constant-expression. */
2615 saved_in_offsetof_p = parser->in_offsetof_p;
2616 parser->in_offsetof_p = true;
2617 expression
2618 = cp_parser_constant_expression (parser,
2619 /*allow_non_constant_p=*/false,
2620 /*non_constant_p=*/NULL);
2621 parser->in_offsetof_p = saved_in_offsetof_p;
2622 /* Consume the closing ')'. */
2623 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2625 return expression;
2628 default:
2629 cp_parser_error (parser, "expected primary-expression");
2630 return error_mark_node;
2633 /* An id-expression can start with either an identifier, a
2634 `::' as the beginning of a qualified-id, or the "operator"
2635 keyword. */
2636 case CPP_NAME:
2637 case CPP_SCOPE:
2638 case CPP_TEMPLATE_ID:
2639 case CPP_NESTED_NAME_SPECIFIER:
2641 tree id_expression;
2642 tree decl;
2643 const char *error_msg;
2645 id_expression:
2646 /* Parse the id-expression. */
2647 id_expression
2648 = cp_parser_id_expression (parser,
2649 /*template_keyword_p=*/false,
2650 /*check_dependency_p=*/true,
2651 /*template_p=*/NULL,
2652 /*declarator_p=*/false);
2653 if (id_expression == error_mark_node)
2654 return error_mark_node;
2655 /* If we have a template-id, then no further lookup is
2656 required. If the template-id was for a template-class, we
2657 will sometimes have a TYPE_DECL at this point. */
2658 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2659 || TREE_CODE (id_expression) == TYPE_DECL)
2660 decl = id_expression;
2661 /* Look up the name. */
2662 else
2664 decl = cp_parser_lookup_name_simple (parser, id_expression);
2665 /* If name lookup gives us a SCOPE_REF, then the
2666 qualifying scope was dependent. Just propagate the
2667 name. */
2668 if (TREE_CODE (decl) == SCOPE_REF)
2670 if (TYPE_P (TREE_OPERAND (decl, 0)))
2671 *qualifying_class = TREE_OPERAND (decl, 0);
2672 return decl;
2674 /* Check to see if DECL is a local variable in a context
2675 where that is forbidden. */
2676 if (parser->local_variables_forbidden_p
2677 && local_variable_p (decl))
2679 /* It might be that we only found DECL because we are
2680 trying to be generous with pre-ISO scoping rules.
2681 For example, consider:
2683 int i;
2684 void g() {
2685 for (int i = 0; i < 10; ++i) {}
2686 extern void f(int j = i);
2689 Here, name look up will originally find the out
2690 of scope `i'. We need to issue a warning message,
2691 but then use the global `i'. */
2692 decl = check_for_out_of_scope_variable (decl);
2693 if (local_variable_p (decl))
2695 error ("local variable `%D' may not appear in this context",
2696 decl);
2697 return error_mark_node;
2702 decl = finish_id_expression (id_expression, decl, parser->scope,
2703 idk, qualifying_class,
2704 parser->integral_constant_expression_p,
2705 parser->allow_non_integral_constant_expression_p,
2706 &parser->non_integral_constant_expression_p,
2707 &error_msg);
2708 if (error_msg)
2709 cp_parser_error (parser, error_msg);
2710 return decl;
2713 /* Anything else is an error. */
2714 default:
2715 cp_parser_error (parser, "expected primary-expression");
2716 return error_mark_node;
2720 /* Parse an id-expression.
2722 id-expression:
2723 unqualified-id
2724 qualified-id
2726 qualified-id:
2727 :: [opt] nested-name-specifier template [opt] unqualified-id
2728 :: identifier
2729 :: operator-function-id
2730 :: template-id
2732 Return a representation of the unqualified portion of the
2733 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2734 a `::' or nested-name-specifier.
2736 Often, if the id-expression was a qualified-id, the caller will
2737 want to make a SCOPE_REF to represent the qualified-id. This
2738 function does not do this in order to avoid wastefully creating
2739 SCOPE_REFs when they are not required.
2741 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2742 `template' keyword.
2744 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2745 uninstantiated templates.
2747 If *TEMPLATE_P is non-NULL, it is set to true iff the
2748 `template' keyword is used to explicitly indicate that the entity
2749 named is a template.
2751 If DECLARATOR_P is true, the id-expression is appearing as part of
2752 a declarator, rather than as part of an expression. */
2754 static tree
2755 cp_parser_id_expression (cp_parser *parser,
2756 bool template_keyword_p,
2757 bool check_dependency_p,
2758 bool *template_p,
2759 bool declarator_p)
2761 bool global_scope_p;
2762 bool nested_name_specifier_p;
2764 /* Assume the `template' keyword was not used. */
2765 if (template_p)
2766 *template_p = false;
2768 /* Look for the optional `::' operator. */
2769 global_scope_p
2770 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2771 != NULL_TREE);
2772 /* Look for the optional nested-name-specifier. */
2773 nested_name_specifier_p
2774 = (cp_parser_nested_name_specifier_opt (parser,
2775 /*typename_keyword_p=*/false,
2776 check_dependency_p,
2777 /*type_p=*/false,
2778 /*is_declarator=*/false)
2779 != NULL_TREE);
2780 /* If there is a nested-name-specifier, then we are looking at
2781 the first qualified-id production. */
2782 if (nested_name_specifier_p)
2784 tree saved_scope;
2785 tree saved_object_scope;
2786 tree saved_qualifying_scope;
2787 tree unqualified_id;
2788 bool is_template;
2790 /* See if the next token is the `template' keyword. */
2791 if (!template_p)
2792 template_p = &is_template;
2793 *template_p = cp_parser_optional_template_keyword (parser);
2794 /* Name lookup we do during the processing of the
2795 unqualified-id might obliterate SCOPE. */
2796 saved_scope = parser->scope;
2797 saved_object_scope = parser->object_scope;
2798 saved_qualifying_scope = parser->qualifying_scope;
2799 /* Process the final unqualified-id. */
2800 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2801 check_dependency_p,
2802 declarator_p);
2803 /* Restore the SAVED_SCOPE for our caller. */
2804 parser->scope = saved_scope;
2805 parser->object_scope = saved_object_scope;
2806 parser->qualifying_scope = saved_qualifying_scope;
2808 return unqualified_id;
2810 /* Otherwise, if we are in global scope, then we are looking at one
2811 of the other qualified-id productions. */
2812 else if (global_scope_p)
2814 cp_token *token;
2815 tree id;
2817 /* Peek at the next token. */
2818 token = cp_lexer_peek_token (parser->lexer);
2820 /* If it's an identifier, and the next token is not a "<", then
2821 we can avoid the template-id case. This is an optimization
2822 for this common case. */
2823 if (token->type == CPP_NAME
2824 && !cp_parser_nth_token_starts_template_argument_list_p
2825 (parser, 2))
2826 return cp_parser_identifier (parser);
2828 cp_parser_parse_tentatively (parser);
2829 /* Try a template-id. */
2830 id = cp_parser_template_id (parser,
2831 /*template_keyword_p=*/false,
2832 /*check_dependency_p=*/true,
2833 declarator_p);
2834 /* If that worked, we're done. */
2835 if (cp_parser_parse_definitely (parser))
2836 return id;
2838 /* Peek at the next token. (Changes in the token buffer may
2839 have invalidated the pointer obtained above.) */
2840 token = cp_lexer_peek_token (parser->lexer);
2842 switch (token->type)
2844 case CPP_NAME:
2845 return cp_parser_identifier (parser);
2847 case CPP_KEYWORD:
2848 if (token->keyword == RID_OPERATOR)
2849 return cp_parser_operator_function_id (parser);
2850 /* Fall through. */
2852 default:
2853 cp_parser_error (parser, "expected id-expression");
2854 return error_mark_node;
2857 else
2858 return cp_parser_unqualified_id (parser, template_keyword_p,
2859 /*check_dependency_p=*/true,
2860 declarator_p);
2863 /* Parse an unqualified-id.
2865 unqualified-id:
2866 identifier
2867 operator-function-id
2868 conversion-function-id
2869 ~ class-name
2870 template-id
2872 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2873 keyword, in a construct like `A::template ...'.
2875 Returns a representation of unqualified-id. For the `identifier'
2876 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2877 production a BIT_NOT_EXPR is returned; the operand of the
2878 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2879 other productions, see the documentation accompanying the
2880 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2881 names are looked up in uninstantiated templates. If DECLARATOR_P
2882 is true, the unqualified-id is appearing as part of a declarator,
2883 rather than as part of an expression. */
2885 static tree
2886 cp_parser_unqualified_id (cp_parser* parser,
2887 bool template_keyword_p,
2888 bool check_dependency_p,
2889 bool declarator_p)
2891 cp_token *token;
2893 /* Peek at the next token. */
2894 token = cp_lexer_peek_token (parser->lexer);
2896 switch (token->type)
2898 case CPP_NAME:
2900 tree id;
2902 /* We don't know yet whether or not this will be a
2903 template-id. */
2904 cp_parser_parse_tentatively (parser);
2905 /* Try a template-id. */
2906 id = cp_parser_template_id (parser, template_keyword_p,
2907 check_dependency_p,
2908 declarator_p);
2909 /* If it worked, we're done. */
2910 if (cp_parser_parse_definitely (parser))
2911 return id;
2912 /* Otherwise, it's an ordinary identifier. */
2913 return cp_parser_identifier (parser);
2916 case CPP_TEMPLATE_ID:
2917 return cp_parser_template_id (parser, template_keyword_p,
2918 check_dependency_p,
2919 declarator_p);
2921 case CPP_COMPL:
2923 tree type_decl;
2924 tree qualifying_scope;
2925 tree object_scope;
2926 tree scope;
2928 /* Consume the `~' token. */
2929 cp_lexer_consume_token (parser->lexer);
2930 /* Parse the class-name. The standard, as written, seems to
2931 say that:
2933 template <typename T> struct S { ~S (); };
2934 template <typename T> S<T>::~S() {}
2936 is invalid, since `~' must be followed by a class-name, but
2937 `S<T>' is dependent, and so not known to be a class.
2938 That's not right; we need to look in uninstantiated
2939 templates. A further complication arises from:
2941 template <typename T> void f(T t) {
2942 t.T::~T();
2945 Here, it is not possible to look up `T' in the scope of `T'
2946 itself. We must look in both the current scope, and the
2947 scope of the containing complete expression.
2949 Yet another issue is:
2951 struct S {
2952 int S;
2953 ~S();
2956 S::~S() {}
2958 The standard does not seem to say that the `S' in `~S'
2959 should refer to the type `S' and not the data member
2960 `S::S'. */
2962 /* DR 244 says that we look up the name after the "~" in the
2963 same scope as we looked up the qualifying name. That idea
2964 isn't fully worked out; it's more complicated than that. */
2965 scope = parser->scope;
2966 object_scope = parser->object_scope;
2967 qualifying_scope = parser->qualifying_scope;
2969 /* If the name is of the form "X::~X" it's OK. */
2970 if (scope && TYPE_P (scope)
2971 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2972 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2973 == CPP_OPEN_PAREN)
2974 && (cp_lexer_peek_token (parser->lexer)->value
2975 == TYPE_IDENTIFIER (scope)))
2977 cp_lexer_consume_token (parser->lexer);
2978 return build_nt (BIT_NOT_EXPR, scope);
2981 /* If there was an explicit qualification (S::~T), first look
2982 in the scope given by the qualification (i.e., S). */
2983 if (scope)
2985 cp_parser_parse_tentatively (parser);
2986 type_decl = cp_parser_class_name (parser,
2987 /*typename_keyword_p=*/false,
2988 /*template_keyword_p=*/false,
2989 /*type_p=*/false,
2990 /*check_dependency=*/false,
2991 /*class_head_p=*/false,
2992 declarator_p);
2993 if (cp_parser_parse_definitely (parser))
2994 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2996 /* In "N::S::~S", look in "N" as well. */
2997 if (scope && qualifying_scope)
2999 cp_parser_parse_tentatively (parser);
3000 parser->scope = qualifying_scope;
3001 parser->object_scope = NULL_TREE;
3002 parser->qualifying_scope = NULL_TREE;
3003 type_decl
3004 = cp_parser_class_name (parser,
3005 /*typename_keyword_p=*/false,
3006 /*template_keyword_p=*/false,
3007 /*type_p=*/false,
3008 /*check_dependency=*/false,
3009 /*class_head_p=*/false,
3010 declarator_p);
3011 if (cp_parser_parse_definitely (parser))
3012 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3014 /* In "p->S::~T", look in the scope given by "*p" as well. */
3015 else if (object_scope)
3017 cp_parser_parse_tentatively (parser);
3018 parser->scope = object_scope;
3019 parser->object_scope = NULL_TREE;
3020 parser->qualifying_scope = NULL_TREE;
3021 type_decl
3022 = cp_parser_class_name (parser,
3023 /*typename_keyword_p=*/false,
3024 /*template_keyword_p=*/false,
3025 /*type_p=*/false,
3026 /*check_dependency=*/false,
3027 /*class_head_p=*/false,
3028 declarator_p);
3029 if (cp_parser_parse_definitely (parser))
3030 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3032 /* Look in the surrounding context. */
3033 parser->scope = NULL_TREE;
3034 parser->object_scope = NULL_TREE;
3035 parser->qualifying_scope = NULL_TREE;
3036 type_decl
3037 = cp_parser_class_name (parser,
3038 /*typename_keyword_p=*/false,
3039 /*template_keyword_p=*/false,
3040 /*type_p=*/false,
3041 /*check_dependency=*/false,
3042 /*class_head_p=*/false,
3043 declarator_p);
3044 /* If an error occurred, assume that the name of the
3045 destructor is the same as the name of the qualifying
3046 class. That allows us to keep parsing after running
3047 into ill-formed destructor names. */
3048 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3049 return build_nt (BIT_NOT_EXPR, scope);
3050 else if (type_decl == error_mark_node)
3051 return error_mark_node;
3053 /* [class.dtor]
3055 A typedef-name that names a class shall not be used as the
3056 identifier in the declarator for a destructor declaration. */
3057 if (declarator_p
3058 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3059 && !DECL_SELF_REFERENCE_P (type_decl))
3060 error ("typedef-name `%D' used as destructor declarator",
3061 type_decl);
3063 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3066 case CPP_KEYWORD:
3067 if (token->keyword == RID_OPERATOR)
3069 tree id;
3071 /* This could be a template-id, so we try that first. */
3072 cp_parser_parse_tentatively (parser);
3073 /* Try a template-id. */
3074 id = cp_parser_template_id (parser, template_keyword_p,
3075 /*check_dependency_p=*/true,
3076 declarator_p);
3077 /* If that worked, we're done. */
3078 if (cp_parser_parse_definitely (parser))
3079 return id;
3080 /* We still don't know whether we're looking at an
3081 operator-function-id or a conversion-function-id. */
3082 cp_parser_parse_tentatively (parser);
3083 /* Try an operator-function-id. */
3084 id = cp_parser_operator_function_id (parser);
3085 /* If that didn't work, try a conversion-function-id. */
3086 if (!cp_parser_parse_definitely (parser))
3087 id = cp_parser_conversion_function_id (parser);
3089 return id;
3091 /* Fall through. */
3093 default:
3094 cp_parser_error (parser, "expected unqualified-id");
3095 return error_mark_node;
3099 /* Parse an (optional) nested-name-specifier.
3101 nested-name-specifier:
3102 class-or-namespace-name :: nested-name-specifier [opt]
3103 class-or-namespace-name :: template nested-name-specifier [opt]
3105 PARSER->SCOPE should be set appropriately before this function is
3106 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3107 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3108 in name lookups.
3110 Sets PARSER->SCOPE to the class (TYPE) or namespace
3111 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3112 it unchanged if there is no nested-name-specifier. Returns the new
3113 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3115 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3116 part of a declaration and/or decl-specifier. */
3118 static tree
3119 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3120 bool typename_keyword_p,
3121 bool check_dependency_p,
3122 bool type_p,
3123 bool is_declaration)
3125 bool success = false;
3126 tree access_check = NULL_TREE;
3127 ptrdiff_t start;
3128 cp_token* token;
3130 /* If the next token corresponds to a nested name specifier, there
3131 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3132 false, it may have been true before, in which case something
3133 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3134 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3135 CHECK_DEPENDENCY_P is false, we have to fall through into the
3136 main loop. */
3137 if (check_dependency_p
3138 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3140 cp_parser_pre_parsed_nested_name_specifier (parser);
3141 return parser->scope;
3144 /* Remember where the nested-name-specifier starts. */
3145 if (cp_parser_parsing_tentatively (parser)
3146 && !cp_parser_committed_to_tentative_parse (parser))
3148 token = cp_lexer_peek_token (parser->lexer);
3149 start = cp_lexer_token_difference (parser->lexer,
3150 parser->lexer->first_token,
3151 token);
3153 else
3154 start = -1;
3156 push_deferring_access_checks (dk_deferred);
3158 while (true)
3160 tree new_scope;
3161 tree old_scope;
3162 tree saved_qualifying_scope;
3163 bool template_keyword_p;
3165 /* Spot cases that cannot be the beginning of a
3166 nested-name-specifier. */
3167 token = cp_lexer_peek_token (parser->lexer);
3169 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3170 the already parsed nested-name-specifier. */
3171 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3173 /* Grab the nested-name-specifier and continue the loop. */
3174 cp_parser_pre_parsed_nested_name_specifier (parser);
3175 success = true;
3176 continue;
3179 /* Spot cases that cannot be the beginning of a
3180 nested-name-specifier. On the second and subsequent times
3181 through the loop, we look for the `template' keyword. */
3182 if (success && token->keyword == RID_TEMPLATE)
3184 /* A template-id can start a nested-name-specifier. */
3185 else if (token->type == CPP_TEMPLATE_ID)
3187 else
3189 /* If the next token is not an identifier, then it is
3190 definitely not a class-or-namespace-name. */
3191 if (token->type != CPP_NAME)
3192 break;
3193 /* If the following token is neither a `<' (to begin a
3194 template-id), nor a `::', then we are not looking at a
3195 nested-name-specifier. */
3196 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3197 if (token->type != CPP_SCOPE
3198 && !cp_parser_nth_token_starts_template_argument_list_p
3199 (parser, 2))
3200 break;
3203 /* The nested-name-specifier is optional, so we parse
3204 tentatively. */
3205 cp_parser_parse_tentatively (parser);
3207 /* Look for the optional `template' keyword, if this isn't the
3208 first time through the loop. */
3209 if (success)
3210 template_keyword_p = cp_parser_optional_template_keyword (parser);
3211 else
3212 template_keyword_p = false;
3214 /* Save the old scope since the name lookup we are about to do
3215 might destroy it. */
3216 old_scope = parser->scope;
3217 saved_qualifying_scope = parser->qualifying_scope;
3218 /* Parse the qualifying entity. */
3219 new_scope
3220 = cp_parser_class_or_namespace_name (parser,
3221 typename_keyword_p,
3222 template_keyword_p,
3223 check_dependency_p,
3224 type_p,
3225 is_declaration);
3226 /* Look for the `::' token. */
3227 cp_parser_require (parser, CPP_SCOPE, "`::'");
3229 /* If we found what we wanted, we keep going; otherwise, we're
3230 done. */
3231 if (!cp_parser_parse_definitely (parser))
3233 bool error_p = false;
3235 /* Restore the OLD_SCOPE since it was valid before the
3236 failed attempt at finding the last
3237 class-or-namespace-name. */
3238 parser->scope = old_scope;
3239 parser->qualifying_scope = saved_qualifying_scope;
3240 /* If the next token is an identifier, and the one after
3241 that is a `::', then any valid interpretation would have
3242 found a class-or-namespace-name. */
3243 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3244 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3245 == CPP_SCOPE)
3246 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3247 != CPP_COMPL))
3249 token = cp_lexer_consume_token (parser->lexer);
3250 if (!error_p)
3252 tree decl;
3254 decl = cp_parser_lookup_name_simple (parser, token->value);
3255 if (TREE_CODE (decl) == TEMPLATE_DECL)
3256 error ("`%D' used without template parameters",
3257 decl);
3258 else
3259 cp_parser_name_lookup_error
3260 (parser, token->value, decl,
3261 "is not a class or namespace");
3262 parser->scope = NULL_TREE;
3263 error_p = true;
3264 /* Treat this as a successful nested-name-specifier
3265 due to:
3267 [basic.lookup.qual]
3269 If the name found is not a class-name (clause
3270 _class_) or namespace-name (_namespace.def_), the
3271 program is ill-formed. */
3272 success = true;
3274 cp_lexer_consume_token (parser->lexer);
3276 break;
3279 /* We've found one valid nested-name-specifier. */
3280 success = true;
3281 /* Make sure we look in the right scope the next time through
3282 the loop. */
3283 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3284 ? TREE_TYPE (new_scope)
3285 : new_scope);
3286 /* If it is a class scope, try to complete it; we are about to
3287 be looking up names inside the class. */
3288 if (TYPE_P (parser->scope)
3289 /* Since checking types for dependency can be expensive,
3290 avoid doing it if the type is already complete. */
3291 && !COMPLETE_TYPE_P (parser->scope)
3292 /* Do not try to complete dependent types. */
3293 && !dependent_type_p (parser->scope))
3294 complete_type (parser->scope);
3297 /* Retrieve any deferred checks. Do not pop this access checks yet
3298 so the memory will not be reclaimed during token replacing below. */
3299 access_check = get_deferred_access_checks ();
3301 /* If parsing tentatively, replace the sequence of tokens that makes
3302 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3303 token. That way, should we re-parse the token stream, we will
3304 not have to repeat the effort required to do the parse, nor will
3305 we issue duplicate error messages. */
3306 if (success && start >= 0)
3308 /* Find the token that corresponds to the start of the
3309 template-id. */
3310 token = cp_lexer_advance_token (parser->lexer,
3311 parser->lexer->first_token,
3312 start);
3314 /* Reset the contents of the START token. */
3315 token->type = CPP_NESTED_NAME_SPECIFIER;
3316 token->value = build_tree_list (access_check, parser->scope);
3317 TREE_TYPE (token->value) = parser->qualifying_scope;
3318 token->keyword = RID_MAX;
3319 /* Purge all subsequent tokens. */
3320 cp_lexer_purge_tokens_after (parser->lexer, token);
3323 pop_deferring_access_checks ();
3324 return success ? parser->scope : NULL_TREE;
3327 /* Parse a nested-name-specifier. See
3328 cp_parser_nested_name_specifier_opt for details. This function
3329 behaves identically, except that it will an issue an error if no
3330 nested-name-specifier is present, and it will return
3331 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3332 is present. */
3334 static tree
3335 cp_parser_nested_name_specifier (cp_parser *parser,
3336 bool typename_keyword_p,
3337 bool check_dependency_p,
3338 bool type_p,
3339 bool is_declaration)
3341 tree scope;
3343 /* Look for the nested-name-specifier. */
3344 scope = cp_parser_nested_name_specifier_opt (parser,
3345 typename_keyword_p,
3346 check_dependency_p,
3347 type_p,
3348 is_declaration);
3349 /* If it was not present, issue an error message. */
3350 if (!scope)
3352 cp_parser_error (parser, "expected nested-name-specifier");
3353 parser->scope = NULL_TREE;
3354 return error_mark_node;
3357 return scope;
3360 /* Parse a class-or-namespace-name.
3362 class-or-namespace-name:
3363 class-name
3364 namespace-name
3366 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3367 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3368 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3369 TYPE_P is TRUE iff the next name should be taken as a class-name,
3370 even the same name is declared to be another entity in the same
3371 scope.
3373 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3374 specified by the class-or-namespace-name. If neither is found the
3375 ERROR_MARK_NODE is returned. */
3377 static tree
3378 cp_parser_class_or_namespace_name (cp_parser *parser,
3379 bool typename_keyword_p,
3380 bool template_keyword_p,
3381 bool check_dependency_p,
3382 bool type_p,
3383 bool is_declaration)
3385 tree saved_scope;
3386 tree saved_qualifying_scope;
3387 tree saved_object_scope;
3388 tree scope;
3389 bool only_class_p;
3391 /* Before we try to parse the class-name, we must save away the
3392 current PARSER->SCOPE since cp_parser_class_name will destroy
3393 it. */
3394 saved_scope = parser->scope;
3395 saved_qualifying_scope = parser->qualifying_scope;
3396 saved_object_scope = parser->object_scope;
3397 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3398 there is no need to look for a namespace-name. */
3399 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3400 if (!only_class_p)
3401 cp_parser_parse_tentatively (parser);
3402 scope = cp_parser_class_name (parser,
3403 typename_keyword_p,
3404 template_keyword_p,
3405 type_p,
3406 check_dependency_p,
3407 /*class_head_p=*/false,
3408 is_declaration);
3409 /* If that didn't work, try for a namespace-name. */
3410 if (!only_class_p && !cp_parser_parse_definitely (parser))
3412 /* Restore the saved scope. */
3413 parser->scope = saved_scope;
3414 parser->qualifying_scope = saved_qualifying_scope;
3415 parser->object_scope = saved_object_scope;
3416 /* If we are not looking at an identifier followed by the scope
3417 resolution operator, then this is not part of a
3418 nested-name-specifier. (Note that this function is only used
3419 to parse the components of a nested-name-specifier.) */
3420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3421 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3422 return error_mark_node;
3423 scope = cp_parser_namespace_name (parser);
3426 return scope;
3429 /* Parse a postfix-expression.
3431 postfix-expression:
3432 primary-expression
3433 postfix-expression [ expression ]
3434 postfix-expression ( expression-list [opt] )
3435 simple-type-specifier ( expression-list [opt] )
3436 typename :: [opt] nested-name-specifier identifier
3437 ( expression-list [opt] )
3438 typename :: [opt] nested-name-specifier template [opt] template-id
3439 ( expression-list [opt] )
3440 postfix-expression . template [opt] id-expression
3441 postfix-expression -> template [opt] id-expression
3442 postfix-expression . pseudo-destructor-name
3443 postfix-expression -> pseudo-destructor-name
3444 postfix-expression ++
3445 postfix-expression --
3446 dynamic_cast < type-id > ( expression )
3447 static_cast < type-id > ( expression )
3448 reinterpret_cast < type-id > ( expression )
3449 const_cast < type-id > ( expression )
3450 typeid ( expression )
3451 typeid ( type-id )
3453 GNU Extension:
3455 postfix-expression:
3456 ( type-id ) { initializer-list , [opt] }
3458 This extension is a GNU version of the C99 compound-literal
3459 construct. (The C99 grammar uses `type-name' instead of `type-id',
3460 but they are essentially the same concept.)
3462 If ADDRESS_P is true, the postfix expression is the operand of the
3463 `&' operator.
3465 Returns a representation of the expression. */
3467 static tree
3468 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3470 cp_token *token;
3471 enum rid keyword;
3472 cp_id_kind idk = CP_ID_KIND_NONE;
3473 tree postfix_expression = NULL_TREE;
3474 /* Non-NULL only if the current postfix-expression can be used to
3475 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3476 class used to qualify the member. */
3477 tree qualifying_class = NULL_TREE;
3479 /* Peek at the next token. */
3480 token = cp_lexer_peek_token (parser->lexer);
3481 /* Some of the productions are determined by keywords. */
3482 keyword = token->keyword;
3483 switch (keyword)
3485 case RID_DYNCAST:
3486 case RID_STATCAST:
3487 case RID_REINTCAST:
3488 case RID_CONSTCAST:
3490 tree type;
3491 tree expression;
3492 const char *saved_message;
3494 /* All of these can be handled in the same way from the point
3495 of view of parsing. Begin by consuming the token
3496 identifying the cast. */
3497 cp_lexer_consume_token (parser->lexer);
3499 /* New types cannot be defined in the cast. */
3500 saved_message = parser->type_definition_forbidden_message;
3501 parser->type_definition_forbidden_message
3502 = "types may not be defined in casts";
3504 /* Look for the opening `<'. */
3505 cp_parser_require (parser, CPP_LESS, "`<'");
3506 /* Parse the type to which we are casting. */
3507 type = cp_parser_type_id (parser);
3508 /* Look for the closing `>'. */
3509 cp_parser_require (parser, CPP_GREATER, "`>'");
3510 /* Restore the old message. */
3511 parser->type_definition_forbidden_message = saved_message;
3513 /* And the expression which is being cast. */
3514 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3515 expression = cp_parser_expression (parser);
3516 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3518 /* Only type conversions to integral or enumeration types
3519 can be used in constant-expressions. */
3520 if (parser->integral_constant_expression_p
3521 && !dependent_type_p (type)
3522 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3523 /* A cast to pointer or reference type is allowed in the
3524 implementation of "offsetof". */
3525 && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3526 && (cp_parser_non_integral_constant_expression
3527 (parser,
3528 "a cast to a type other than an integral or "
3529 "enumeration type")))
3530 return error_mark_node;
3532 switch (keyword)
3534 case RID_DYNCAST:
3535 postfix_expression
3536 = build_dynamic_cast (type, expression);
3537 break;
3538 case RID_STATCAST:
3539 postfix_expression
3540 = build_static_cast (type, expression);
3541 break;
3542 case RID_REINTCAST:
3543 postfix_expression
3544 = build_reinterpret_cast (type, expression);
3545 break;
3546 case RID_CONSTCAST:
3547 postfix_expression
3548 = build_const_cast (type, expression);
3549 break;
3550 default:
3551 abort ();
3554 break;
3556 case RID_TYPEID:
3558 tree type;
3559 const char *saved_message;
3560 bool saved_in_type_id_in_expr_p;
3562 /* Consume the `typeid' token. */
3563 cp_lexer_consume_token (parser->lexer);
3564 /* Look for the `(' token. */
3565 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3566 /* Types cannot be defined in a `typeid' expression. */
3567 saved_message = parser->type_definition_forbidden_message;
3568 parser->type_definition_forbidden_message
3569 = "types may not be defined in a `typeid\' expression";
3570 /* We can't be sure yet whether we're looking at a type-id or an
3571 expression. */
3572 cp_parser_parse_tentatively (parser);
3573 /* Try a type-id first. */
3574 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3575 parser->in_type_id_in_expr_p = true;
3576 type = cp_parser_type_id (parser);
3577 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3578 /* Look for the `)' token. Otherwise, we can't be sure that
3579 we're not looking at an expression: consider `typeid (int
3580 (3))', for example. */
3581 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3582 /* If all went well, simply lookup the type-id. */
3583 if (cp_parser_parse_definitely (parser))
3584 postfix_expression = get_typeid (type);
3585 /* Otherwise, fall back to the expression variant. */
3586 else
3588 tree expression;
3590 /* Look for an expression. */
3591 expression = cp_parser_expression (parser);
3592 /* Compute its typeid. */
3593 postfix_expression = build_typeid (expression);
3594 /* Look for the `)' token. */
3595 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3597 /* `typeid' may not appear in an integral constant expression. */
3598 if (cp_parser_non_integral_constant_expression(parser,
3599 "`typeid' operator"))
3600 return error_mark_node;
3601 /* Restore the saved message. */
3602 parser->type_definition_forbidden_message = saved_message;
3604 break;
3606 case RID_TYPENAME:
3608 bool template_p = false;
3609 tree id;
3610 tree type;
3612 /* Consume the `typename' token. */
3613 cp_lexer_consume_token (parser->lexer);
3614 /* Look for the optional `::' operator. */
3615 cp_parser_global_scope_opt (parser,
3616 /*current_scope_valid_p=*/false);
3617 /* Look for the nested-name-specifier. */
3618 cp_parser_nested_name_specifier (parser,
3619 /*typename_keyword_p=*/true,
3620 /*check_dependency_p=*/true,
3621 /*type_p=*/true,
3622 /*is_declaration=*/true);
3623 /* Look for the optional `template' keyword. */
3624 template_p = cp_parser_optional_template_keyword (parser);
3625 /* We don't know whether we're looking at a template-id or an
3626 identifier. */
3627 cp_parser_parse_tentatively (parser);
3628 /* Try a template-id. */
3629 id = cp_parser_template_id (parser, template_p,
3630 /*check_dependency_p=*/true,
3631 /*is_declaration=*/true);
3632 /* If that didn't work, try an identifier. */
3633 if (!cp_parser_parse_definitely (parser))
3634 id = cp_parser_identifier (parser);
3635 /* If we look up a template-id in a non-dependent qualifying
3636 scope, there's no need to create a dependent type. */
3637 if (TREE_CODE (id) == TYPE_DECL
3638 && !dependent_type_p (parser->scope))
3639 type = TREE_TYPE (id);
3640 /* Create a TYPENAME_TYPE to represent the type to which the
3641 functional cast is being performed. */
3642 else
3643 type = make_typename_type (parser->scope, id,
3644 /*complain=*/1);
3646 postfix_expression = cp_parser_functional_cast (parser, type);
3648 break;
3650 default:
3652 tree type;
3654 /* If the next thing is a simple-type-specifier, we may be
3655 looking at a functional cast. We could also be looking at
3656 an id-expression. So, we try the functional cast, and if
3657 that doesn't work we fall back to the primary-expression. */
3658 cp_parser_parse_tentatively (parser);
3659 /* Look for the simple-type-specifier. */
3660 type = cp_parser_simple_type_specifier (parser,
3661 CP_PARSER_FLAGS_NONE,
3662 /*identifier_p=*/false);
3663 /* Parse the cast itself. */
3664 if (!cp_parser_error_occurred (parser))
3665 postfix_expression
3666 = cp_parser_functional_cast (parser, type);
3667 /* If that worked, we're done. */
3668 if (cp_parser_parse_definitely (parser))
3669 break;
3671 /* If the functional-cast didn't work out, try a
3672 compound-literal. */
3673 if (cp_parser_allow_gnu_extensions_p (parser)
3674 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3676 tree initializer_list = NULL_TREE;
3677 bool saved_in_type_id_in_expr_p;
3679 cp_parser_parse_tentatively (parser);
3680 /* Consume the `('. */
3681 cp_lexer_consume_token (parser->lexer);
3682 /* Parse the type. */
3683 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3684 parser->in_type_id_in_expr_p = true;
3685 type = cp_parser_type_id (parser);
3686 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3687 /* Look for the `)'. */
3688 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3689 /* Look for the `{'. */
3690 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3691 /* If things aren't going well, there's no need to
3692 keep going. */
3693 if (!cp_parser_error_occurred (parser))
3695 bool non_constant_p;
3696 /* Parse the initializer-list. */
3697 initializer_list
3698 = cp_parser_initializer_list (parser, &non_constant_p);
3699 /* Allow a trailing `,'. */
3700 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3701 cp_lexer_consume_token (parser->lexer);
3702 /* Look for the final `}'. */
3703 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3705 /* If that worked, we're definitely looking at a
3706 compound-literal expression. */
3707 if (cp_parser_parse_definitely (parser))
3709 /* Warn the user that a compound literal is not
3710 allowed in standard C++. */
3711 if (pedantic)
3712 pedwarn ("ISO C++ forbids compound-literals");
3713 /* Form the representation of the compound-literal. */
3714 postfix_expression
3715 = finish_compound_literal (type, initializer_list);
3716 break;
3720 /* It must be a primary-expression. */
3721 postfix_expression = cp_parser_primary_expression (parser,
3722 &idk,
3723 &qualifying_class);
3725 break;
3728 /* If we were avoiding committing to the processing of a
3729 qualified-id until we knew whether or not we had a
3730 pointer-to-member, we now know. */
3731 if (qualifying_class)
3733 bool done;
3735 /* Peek at the next token. */
3736 token = cp_lexer_peek_token (parser->lexer);
3737 done = (token->type != CPP_OPEN_SQUARE
3738 && token->type != CPP_OPEN_PAREN
3739 && token->type != CPP_DOT
3740 && token->type != CPP_DEREF
3741 && token->type != CPP_PLUS_PLUS
3742 && token->type != CPP_MINUS_MINUS);
3744 postfix_expression = finish_qualified_id_expr (qualifying_class,
3745 postfix_expression,
3746 done,
3747 address_p);
3748 if (done)
3749 return postfix_expression;
3752 /* Keep looping until the postfix-expression is complete. */
3753 while (true)
3755 if (idk == CP_ID_KIND_UNQUALIFIED
3756 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3757 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3758 /* It is not a Koenig lookup function call. */
3759 postfix_expression
3760 = unqualified_name_lookup_error (postfix_expression);
3762 /* Peek at the next token. */
3763 token = cp_lexer_peek_token (parser->lexer);
3765 switch (token->type)
3767 case CPP_OPEN_SQUARE:
3768 /* postfix-expression [ expression ] */
3770 tree index;
3772 /* Consume the `[' token. */
3773 cp_lexer_consume_token (parser->lexer);
3774 /* Parse the index expression. */
3775 index = cp_parser_expression (parser);
3776 /* Look for the closing `]'. */
3777 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3779 /* Build the ARRAY_REF. */
3780 postfix_expression
3781 = grok_array_decl (postfix_expression, index);
3782 idk = CP_ID_KIND_NONE;
3783 /* Array references are not permitted in
3784 constant-expressions. */
3785 if (cp_parser_non_integral_constant_expression
3786 (parser, "an array reference"))
3787 postfix_expression = error_mark_node;
3789 break;
3791 case CPP_OPEN_PAREN:
3792 /* postfix-expression ( expression-list [opt] ) */
3794 bool koenig_p;
3795 tree args = (cp_parser_parenthesized_expression_list
3796 (parser, false, /*non_constant_p=*/NULL));
3798 if (args == error_mark_node)
3800 postfix_expression = error_mark_node;
3801 break;
3804 /* Function calls are not permitted in
3805 constant-expressions. */
3806 if (cp_parser_non_integral_constant_expression (parser,
3807 "a function call"))
3809 postfix_expression = error_mark_node;
3810 break;
3813 koenig_p = false;
3814 if (idk == CP_ID_KIND_UNQUALIFIED)
3816 /* We do not perform argument-dependent lookup if
3817 normal lookup finds a non-function, in accordance
3818 with the expected resolution of DR 218. */
3819 if (args
3820 && (is_overloaded_fn (postfix_expression)
3821 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3823 koenig_p = true;
3824 postfix_expression
3825 = perform_koenig_lookup (postfix_expression, args);
3827 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3828 postfix_expression
3829 = unqualified_fn_lookup_error (postfix_expression);
3832 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3834 tree instance = TREE_OPERAND (postfix_expression, 0);
3835 tree fn = TREE_OPERAND (postfix_expression, 1);
3837 if (processing_template_decl
3838 && (type_dependent_expression_p (instance)
3839 || (!BASELINK_P (fn)
3840 && TREE_CODE (fn) != FIELD_DECL)
3841 || type_dependent_expression_p (fn)
3842 || any_type_dependent_arguments_p (args)))
3844 postfix_expression
3845 = build_min_nt (CALL_EXPR, postfix_expression, args);
3846 break;
3849 if (BASELINK_P (fn))
3850 postfix_expression
3851 = (build_new_method_call
3852 (instance, fn, args, NULL_TREE,
3853 (idk == CP_ID_KIND_QUALIFIED
3854 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3855 else
3856 postfix_expression
3857 = finish_call_expr (postfix_expression, args,
3858 /*disallow_virtual=*/false,
3859 /*koenig_p=*/false);
3861 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3862 || TREE_CODE (postfix_expression) == MEMBER_REF
3863 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3864 postfix_expression = (build_offset_ref_call_from_tree
3865 (postfix_expression, args));
3866 else if (idk == CP_ID_KIND_QUALIFIED)
3867 /* A call to a static class member, or a namespace-scope
3868 function. */
3869 postfix_expression
3870 = finish_call_expr (postfix_expression, args,
3871 /*disallow_virtual=*/true,
3872 koenig_p);
3873 else
3874 /* All other function calls. */
3875 postfix_expression
3876 = finish_call_expr (postfix_expression, args,
3877 /*disallow_virtual=*/false,
3878 koenig_p);
3880 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3881 idk = CP_ID_KIND_NONE;
3883 break;
3885 case CPP_DOT:
3886 case CPP_DEREF:
3887 /* postfix-expression . template [opt] id-expression
3888 postfix-expression . pseudo-destructor-name
3889 postfix-expression -> template [opt] id-expression
3890 postfix-expression -> pseudo-destructor-name */
3892 tree name;
3893 bool dependent_p;
3894 bool template_p;
3895 tree scope = NULL_TREE;
3896 enum cpp_ttype token_type = token->type;
3898 /* If this is a `->' operator, dereference the pointer. */
3899 if (token->type == CPP_DEREF)
3900 postfix_expression = build_x_arrow (postfix_expression);
3901 /* Check to see whether or not the expression is
3902 type-dependent. */
3903 dependent_p = type_dependent_expression_p (postfix_expression);
3904 /* The identifier following the `->' or `.' is not
3905 qualified. */
3906 parser->scope = NULL_TREE;
3907 parser->qualifying_scope = NULL_TREE;
3908 parser->object_scope = NULL_TREE;
3909 idk = CP_ID_KIND_NONE;
3910 /* Enter the scope corresponding to the type of the object
3911 given by the POSTFIX_EXPRESSION. */
3912 if (!dependent_p
3913 && TREE_TYPE (postfix_expression) != NULL_TREE)
3915 scope = TREE_TYPE (postfix_expression);
3916 /* According to the standard, no expression should
3917 ever have reference type. Unfortunately, we do not
3918 currently match the standard in this respect in
3919 that our internal representation of an expression
3920 may have reference type even when the standard says
3921 it does not. Therefore, we have to manually obtain
3922 the underlying type here. */
3923 scope = non_reference (scope);
3924 /* The type of the POSTFIX_EXPRESSION must be
3925 complete. */
3926 scope = complete_type_or_else (scope, NULL_TREE);
3927 /* Let the name lookup machinery know that we are
3928 processing a class member access expression. */
3929 parser->context->object_type = scope;
3930 /* If something went wrong, we want to be able to
3931 discern that case, as opposed to the case where
3932 there was no SCOPE due to the type of expression
3933 being dependent. */
3934 if (!scope)
3935 scope = error_mark_node;
3936 /* If the SCOPE was erroneous, make the various
3937 semantic analysis functions exit quickly -- and
3938 without issuing additional error messages. */
3939 if (scope == error_mark_node)
3940 postfix_expression = error_mark_node;
3943 /* Consume the `.' or `->' operator. */
3944 cp_lexer_consume_token (parser->lexer);
3945 /* If the SCOPE is not a scalar type, we are looking at an
3946 ordinary class member access expression, rather than a
3947 pseudo-destructor-name. */
3948 if (!scope || !SCALAR_TYPE_P (scope))
3950 template_p = cp_parser_optional_template_keyword (parser);
3951 /* Parse the id-expression. */
3952 name = cp_parser_id_expression (parser,
3953 template_p,
3954 /*check_dependency_p=*/true,
3955 /*template_p=*/NULL,
3956 /*declarator_p=*/false);
3957 /* In general, build a SCOPE_REF if the member name is
3958 qualified. However, if the name was not dependent
3959 and has already been resolved; there is no need to
3960 build the SCOPE_REF. For example;
3962 struct X { void f(); };
3963 template <typename T> void f(T* t) { t->X::f(); }
3965 Even though "t" is dependent, "X::f" is not and has
3966 been resolved to a BASELINK; there is no need to
3967 include scope information. */
3969 /* But we do need to remember that there was an explicit
3970 scope for virtual function calls. */
3971 if (parser->scope)
3972 idk = CP_ID_KIND_QUALIFIED;
3974 if (name != error_mark_node
3975 && !BASELINK_P (name)
3976 && parser->scope)
3978 name = build_nt (SCOPE_REF, parser->scope, name);
3979 parser->scope = NULL_TREE;
3980 parser->qualifying_scope = NULL_TREE;
3981 parser->object_scope = NULL_TREE;
3983 if (scope && name && BASELINK_P (name))
3984 adjust_result_of_qualified_name_lookup
3985 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3986 postfix_expression
3987 = finish_class_member_access_expr (postfix_expression, name);
3989 /* Otherwise, try the pseudo-destructor-name production. */
3990 else
3992 tree s = NULL_TREE;
3993 tree type;
3995 /* Parse the pseudo-destructor-name. */
3996 cp_parser_pseudo_destructor_name (parser, &s, &type);
3997 /* Form the call. */
3998 postfix_expression
3999 = finish_pseudo_destructor_expr (postfix_expression,
4000 s, TREE_TYPE (type));
4003 /* We no longer need to look up names in the scope of the
4004 object on the left-hand side of the `.' or `->'
4005 operator. */
4006 parser->context->object_type = NULL_TREE;
4007 /* These operators may not appear in constant-expressions. */
4008 if (/* The "->" operator is allowed in the implementation
4009 of "offsetof". The "." operator may appear in the
4010 name of the member. */
4011 !parser->in_offsetof_p
4012 && (cp_parser_non_integral_constant_expression
4013 (parser,
4014 token_type == CPP_DEREF ? "'->'" : "`.'")))
4015 postfix_expression = error_mark_node;
4017 break;
4019 case CPP_PLUS_PLUS:
4020 /* postfix-expression ++ */
4021 /* Consume the `++' token. */
4022 cp_lexer_consume_token (parser->lexer);
4023 /* Generate a representation for the complete expression. */
4024 postfix_expression
4025 = finish_increment_expr (postfix_expression,
4026 POSTINCREMENT_EXPR);
4027 /* Increments may not appear in constant-expressions. */
4028 if (cp_parser_non_integral_constant_expression (parser,
4029 "an increment"))
4030 postfix_expression = error_mark_node;
4031 idk = CP_ID_KIND_NONE;
4032 break;
4034 case CPP_MINUS_MINUS:
4035 /* postfix-expression -- */
4036 /* Consume the `--' token. */
4037 cp_lexer_consume_token (parser->lexer);
4038 /* Generate a representation for the complete expression. */
4039 postfix_expression
4040 = finish_increment_expr (postfix_expression,
4041 POSTDECREMENT_EXPR);
4042 /* Decrements may not appear in constant-expressions. */
4043 if (cp_parser_non_integral_constant_expression (parser,
4044 "a decrement"))
4045 postfix_expression = error_mark_node;
4046 idk = CP_ID_KIND_NONE;
4047 break;
4049 default:
4050 return postfix_expression;
4054 /* We should never get here. */
4055 abort ();
4056 return error_mark_node;
4059 /* Parse a parenthesized expression-list.
4061 expression-list:
4062 assignment-expression
4063 expression-list, assignment-expression
4065 attribute-list:
4066 expression-list
4067 identifier
4068 identifier, expression-list
4070 Returns a TREE_LIST. The TREE_VALUE of each node is a
4071 representation of an assignment-expression. Note that a TREE_LIST
4072 is returned even if there is only a single expression in the list.
4073 error_mark_node is returned if the ( and or ) are
4074 missing. NULL_TREE is returned on no expressions. The parentheses
4075 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4076 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4077 indicates whether or not all of the expressions in the list were
4078 constant. */
4080 static tree
4081 cp_parser_parenthesized_expression_list (cp_parser* parser,
4082 bool is_attribute_list,
4083 bool *non_constant_p)
4085 tree expression_list = NULL_TREE;
4086 tree identifier = NULL_TREE;
4088 /* Assume all the expressions will be constant. */
4089 if (non_constant_p)
4090 *non_constant_p = false;
4092 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4093 return error_mark_node;
4095 /* Consume expressions until there are no more. */
4096 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4097 while (true)
4099 tree expr;
4101 /* At the beginning of attribute lists, check to see if the
4102 next token is an identifier. */
4103 if (is_attribute_list
4104 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4106 cp_token *token;
4108 /* Consume the identifier. */
4109 token = cp_lexer_consume_token (parser->lexer);
4110 /* Save the identifier. */
4111 identifier = token->value;
4113 else
4115 /* Parse the next assignment-expression. */
4116 if (non_constant_p)
4118 bool expr_non_constant_p;
4119 expr = (cp_parser_constant_expression
4120 (parser, /*allow_non_constant_p=*/true,
4121 &expr_non_constant_p));
4122 if (expr_non_constant_p)
4123 *non_constant_p = true;
4125 else
4126 expr = cp_parser_assignment_expression (parser);
4128 /* Add it to the list. We add error_mark_node
4129 expressions to the list, so that we can still tell if
4130 the correct form for a parenthesized expression-list
4131 is found. That gives better errors. */
4132 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4134 if (expr == error_mark_node)
4135 goto skip_comma;
4138 /* After the first item, attribute lists look the same as
4139 expression lists. */
4140 is_attribute_list = false;
4142 get_comma:;
4143 /* If the next token isn't a `,', then we are done. */
4144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4145 break;
4147 /* Otherwise, consume the `,' and keep going. */
4148 cp_lexer_consume_token (parser->lexer);
4151 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4153 int ending;
4155 skip_comma:;
4156 /* We try and resync to an unnested comma, as that will give the
4157 user better diagnostics. */
4158 ending = cp_parser_skip_to_closing_parenthesis (parser,
4159 /*recovering=*/true,
4160 /*or_comma=*/true,
4161 /*consume_paren=*/true);
4162 if (ending < 0)
4163 goto get_comma;
4164 if (!ending)
4165 return error_mark_node;
4168 /* We built up the list in reverse order so we must reverse it now. */
4169 expression_list = nreverse (expression_list);
4170 if (identifier)
4171 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4173 return expression_list;
4176 /* Parse a pseudo-destructor-name.
4178 pseudo-destructor-name:
4179 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4180 :: [opt] nested-name-specifier template template-id :: ~ type-name
4181 :: [opt] nested-name-specifier [opt] ~ type-name
4183 If either of the first two productions is used, sets *SCOPE to the
4184 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4185 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4186 or ERROR_MARK_NODE if the parse fails. */
4188 static void
4189 cp_parser_pseudo_destructor_name (cp_parser* parser,
4190 tree* scope,
4191 tree* type)
4193 bool nested_name_specifier_p;
4195 /* Look for the optional `::' operator. */
4196 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4197 /* Look for the optional nested-name-specifier. */
4198 nested_name_specifier_p
4199 = (cp_parser_nested_name_specifier_opt (parser,
4200 /*typename_keyword_p=*/false,
4201 /*check_dependency_p=*/true,
4202 /*type_p=*/false,
4203 /*is_declaration=*/true)
4204 != NULL_TREE);
4205 /* Now, if we saw a nested-name-specifier, we might be doing the
4206 second production. */
4207 if (nested_name_specifier_p
4208 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4210 /* Consume the `template' keyword. */
4211 cp_lexer_consume_token (parser->lexer);
4212 /* Parse the template-id. */
4213 cp_parser_template_id (parser,
4214 /*template_keyword_p=*/true,
4215 /*check_dependency_p=*/false,
4216 /*is_declaration=*/true);
4217 /* Look for the `::' token. */
4218 cp_parser_require (parser, CPP_SCOPE, "`::'");
4220 /* If the next token is not a `~', then there might be some
4221 additional qualification. */
4222 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4224 /* Look for the type-name. */
4225 *scope = TREE_TYPE (cp_parser_type_name (parser));
4227 /* If we didn't get an aggregate type, or we don't have ::~,
4228 then something has gone wrong. Since the only caller of this
4229 function is looking for something after `.' or `->' after a
4230 scalar type, most likely the program is trying to get a
4231 member of a non-aggregate type. */
4232 if (*scope == error_mark_node
4233 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4234 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4236 cp_parser_error (parser, "request for member of non-aggregate type");
4237 *type = error_mark_node;
4238 return;
4241 /* Look for the `::' token. */
4242 cp_parser_require (parser, CPP_SCOPE, "`::'");
4244 else
4245 *scope = NULL_TREE;
4247 /* Look for the `~'. */
4248 cp_parser_require (parser, CPP_COMPL, "`~'");
4249 /* Look for the type-name again. We are not responsible for
4250 checking that it matches the first type-name. */
4251 *type = cp_parser_type_name (parser);
4254 /* Parse a unary-expression.
4256 unary-expression:
4257 postfix-expression
4258 ++ cast-expression
4259 -- cast-expression
4260 unary-operator cast-expression
4261 sizeof unary-expression
4262 sizeof ( type-id )
4263 new-expression
4264 delete-expression
4266 GNU Extensions:
4268 unary-expression:
4269 __extension__ cast-expression
4270 __alignof__ unary-expression
4271 __alignof__ ( type-id )
4272 __real__ cast-expression
4273 __imag__ cast-expression
4274 && identifier
4276 ADDRESS_P is true iff the unary-expression is appearing as the
4277 operand of the `&' operator.
4279 Returns a representation of the expression. */
4281 static tree
4282 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4284 cp_token *token;
4285 enum tree_code unary_operator;
4287 /* Peek at the next token. */
4288 token = cp_lexer_peek_token (parser->lexer);
4289 /* Some keywords give away the kind of expression. */
4290 if (token->type == CPP_KEYWORD)
4292 enum rid keyword = token->keyword;
4294 switch (keyword)
4296 case RID_ALIGNOF:
4297 case RID_SIZEOF:
4299 tree operand;
4300 enum tree_code op;
4302 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4303 /* Consume the token. */
4304 cp_lexer_consume_token (parser->lexer);
4305 /* Parse the operand. */
4306 operand = cp_parser_sizeof_operand (parser, keyword);
4308 if (TYPE_P (operand))
4309 return cxx_sizeof_or_alignof_type (operand, op, true);
4310 else
4311 return cxx_sizeof_or_alignof_expr (operand, op);
4314 case RID_NEW:
4315 return cp_parser_new_expression (parser);
4317 case RID_DELETE:
4318 return cp_parser_delete_expression (parser);
4320 case RID_EXTENSION:
4322 /* The saved value of the PEDANTIC flag. */
4323 int saved_pedantic;
4324 tree expr;
4326 /* Save away the PEDANTIC flag. */
4327 cp_parser_extension_opt (parser, &saved_pedantic);
4328 /* Parse the cast-expression. */
4329 expr = cp_parser_simple_cast_expression (parser);
4330 /* Restore the PEDANTIC flag. */
4331 pedantic = saved_pedantic;
4333 return expr;
4336 case RID_REALPART:
4337 case RID_IMAGPART:
4339 tree expression;
4341 /* Consume the `__real__' or `__imag__' token. */
4342 cp_lexer_consume_token (parser->lexer);
4343 /* Parse the cast-expression. */
4344 expression = cp_parser_simple_cast_expression (parser);
4345 /* Create the complete representation. */
4346 return build_x_unary_op ((keyword == RID_REALPART
4347 ? REALPART_EXPR : IMAGPART_EXPR),
4348 expression);
4350 break;
4352 default:
4353 break;
4357 /* Look for the `:: new' and `:: delete', which also signal the
4358 beginning of a new-expression, or delete-expression,
4359 respectively. If the next token is `::', then it might be one of
4360 these. */
4361 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4363 enum rid keyword;
4365 /* See if the token after the `::' is one of the keywords in
4366 which we're interested. */
4367 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4368 /* If it's `new', we have a new-expression. */
4369 if (keyword == RID_NEW)
4370 return cp_parser_new_expression (parser);
4371 /* Similarly, for `delete'. */
4372 else if (keyword == RID_DELETE)
4373 return cp_parser_delete_expression (parser);
4376 /* Look for a unary operator. */
4377 unary_operator = cp_parser_unary_operator (token);
4378 /* The `++' and `--' operators can be handled similarly, even though
4379 they are not technically unary-operators in the grammar. */
4380 if (unary_operator == ERROR_MARK)
4382 if (token->type == CPP_PLUS_PLUS)
4383 unary_operator = PREINCREMENT_EXPR;
4384 else if (token->type == CPP_MINUS_MINUS)
4385 unary_operator = PREDECREMENT_EXPR;
4386 /* Handle the GNU address-of-label extension. */
4387 else if (cp_parser_allow_gnu_extensions_p (parser)
4388 && token->type == CPP_AND_AND)
4390 tree identifier;
4392 /* Consume the '&&' token. */
4393 cp_lexer_consume_token (parser->lexer);
4394 /* Look for the identifier. */
4395 identifier = cp_parser_identifier (parser);
4396 /* Create an expression representing the address. */
4397 return finish_label_address_expr (identifier);
4400 if (unary_operator != ERROR_MARK)
4402 tree cast_expression;
4403 tree expression = error_mark_node;
4404 const char *non_constant_p = NULL;
4406 /* Consume the operator token. */
4407 token = cp_lexer_consume_token (parser->lexer);
4408 /* Parse the cast-expression. */
4409 cast_expression
4410 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4411 /* Now, build an appropriate representation. */
4412 switch (unary_operator)
4414 case INDIRECT_REF:
4415 non_constant_p = "`*'";
4416 expression = build_x_indirect_ref (cast_expression, "unary *");
4417 break;
4419 case ADDR_EXPR:
4420 /* The "&" operator is allowed in the implementation of
4421 "offsetof". */
4422 if (!parser->in_offsetof_p)
4423 non_constant_p = "`&'";
4424 /* Fall through. */
4425 case BIT_NOT_EXPR:
4426 expression = build_x_unary_op (unary_operator, cast_expression);
4427 break;
4429 case PREINCREMENT_EXPR:
4430 case PREDECREMENT_EXPR:
4431 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4432 ? "`++'" : "`--'");
4433 /* Fall through. */
4434 case CONVERT_EXPR:
4435 case NEGATE_EXPR:
4436 case TRUTH_NOT_EXPR:
4437 expression = finish_unary_op_expr (unary_operator, cast_expression);
4438 break;
4440 default:
4441 abort ();
4444 if (non_constant_p
4445 && cp_parser_non_integral_constant_expression (parser,
4446 non_constant_p))
4447 expression = error_mark_node;
4449 return expression;
4452 return cp_parser_postfix_expression (parser, address_p);
4455 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4456 unary-operator, the corresponding tree code is returned. */
4458 static enum tree_code
4459 cp_parser_unary_operator (cp_token* token)
4461 switch (token->type)
4463 case CPP_MULT:
4464 return INDIRECT_REF;
4466 case CPP_AND:
4467 return ADDR_EXPR;
4469 case CPP_PLUS:
4470 return CONVERT_EXPR;
4472 case CPP_MINUS:
4473 return NEGATE_EXPR;
4475 case CPP_NOT:
4476 return TRUTH_NOT_EXPR;
4478 case CPP_COMPL:
4479 return BIT_NOT_EXPR;
4481 default:
4482 return ERROR_MARK;
4486 /* Parse a new-expression.
4488 new-expression:
4489 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4490 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4492 Returns a representation of the expression. */
4494 static tree
4495 cp_parser_new_expression (cp_parser* parser)
4497 bool global_scope_p;
4498 tree placement;
4499 tree type;
4500 tree initializer;
4502 /* Look for the optional `::' operator. */
4503 global_scope_p
4504 = (cp_parser_global_scope_opt (parser,
4505 /*current_scope_valid_p=*/false)
4506 != NULL_TREE);
4507 /* Look for the `new' operator. */
4508 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4509 /* There's no easy way to tell a new-placement from the
4510 `( type-id )' construct. */
4511 cp_parser_parse_tentatively (parser);
4512 /* Look for a new-placement. */
4513 placement = cp_parser_new_placement (parser);
4514 /* If that didn't work out, there's no new-placement. */
4515 if (!cp_parser_parse_definitely (parser))
4516 placement = NULL_TREE;
4518 /* If the next token is a `(', then we have a parenthesized
4519 type-id. */
4520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4522 /* Consume the `('. */
4523 cp_lexer_consume_token (parser->lexer);
4524 /* Parse the type-id. */
4525 type = cp_parser_type_id (parser);
4526 /* Look for the closing `)'. */
4527 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4528 /* There should not be a direct-new-declarator in this production,
4529 but GCC used to allowed this, so we check and emit a sensible error
4530 message for this case. */
4531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4533 error ("array bound forbidden after parenthesized type-id");
4534 inform ("try removing the parentheses around the type-id");
4535 cp_parser_direct_new_declarator (parser);
4538 /* Otherwise, there must be a new-type-id. */
4539 else
4540 type = cp_parser_new_type_id (parser);
4542 /* If the next token is a `(', then we have a new-initializer. */
4543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4544 initializer = cp_parser_new_initializer (parser);
4545 else
4546 initializer = NULL_TREE;
4548 /* A new-expression may not appear in an integral constant
4549 expression. */
4550 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4551 return error_mark_node;
4553 /* Create a representation of the new-expression. */
4554 return build_new (placement, type, initializer, global_scope_p);
4557 /* Parse a new-placement.
4559 new-placement:
4560 ( expression-list )
4562 Returns the same representation as for an expression-list. */
4564 static tree
4565 cp_parser_new_placement (cp_parser* parser)
4567 tree expression_list;
4569 /* Parse the expression-list. */
4570 expression_list = (cp_parser_parenthesized_expression_list
4571 (parser, false, /*non_constant_p=*/NULL));
4573 return expression_list;
4576 /* Parse a new-type-id.
4578 new-type-id:
4579 type-specifier-seq new-declarator [opt]
4581 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4582 and whose TREE_VALUE is the new-declarator. */
4584 static tree
4585 cp_parser_new_type_id (cp_parser* parser)
4587 tree type_specifier_seq;
4588 tree declarator;
4589 const char *saved_message;
4591 /* The type-specifier sequence must not contain type definitions.
4592 (It cannot contain declarations of new types either, but if they
4593 are not definitions we will catch that because they are not
4594 complete.) */
4595 saved_message = parser->type_definition_forbidden_message;
4596 parser->type_definition_forbidden_message
4597 = "types may not be defined in a new-type-id";
4598 /* Parse the type-specifier-seq. */
4599 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4600 /* Restore the old message. */
4601 parser->type_definition_forbidden_message = saved_message;
4602 /* Parse the new-declarator. */
4603 declarator = cp_parser_new_declarator_opt (parser);
4605 return build_tree_list (type_specifier_seq, declarator);
4608 /* Parse an (optional) new-declarator.
4610 new-declarator:
4611 ptr-operator new-declarator [opt]
4612 direct-new-declarator
4614 Returns a representation of the declarator. See
4615 cp_parser_declarator for the representations used. */
4617 static tree
4618 cp_parser_new_declarator_opt (cp_parser* parser)
4620 enum tree_code code;
4621 tree type;
4622 tree cv_qualifier_seq;
4624 /* We don't know if there's a ptr-operator next, or not. */
4625 cp_parser_parse_tentatively (parser);
4626 /* Look for a ptr-operator. */
4627 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4628 /* If that worked, look for more new-declarators. */
4629 if (cp_parser_parse_definitely (parser))
4631 tree declarator;
4633 /* Parse another optional declarator. */
4634 declarator = cp_parser_new_declarator_opt (parser);
4636 /* Create the representation of the declarator. */
4637 if (code == INDIRECT_REF)
4638 declarator = make_pointer_declarator (cv_qualifier_seq,
4639 declarator);
4640 else
4641 declarator = make_reference_declarator (cv_qualifier_seq,
4642 declarator);
4644 /* Handle the pointer-to-member case. */
4645 if (type)
4646 declarator = build_nt (SCOPE_REF, type, declarator);
4648 return declarator;
4651 /* If the next token is a `[', there is a direct-new-declarator. */
4652 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4653 return cp_parser_direct_new_declarator (parser);
4655 return NULL_TREE;
4658 /* Parse a direct-new-declarator.
4660 direct-new-declarator:
4661 [ expression ]
4662 direct-new-declarator [constant-expression]
4664 Returns an ARRAY_REF, following the same conventions as are
4665 documented for cp_parser_direct_declarator. */
4667 static tree
4668 cp_parser_direct_new_declarator (cp_parser* parser)
4670 tree declarator = NULL_TREE;
4672 while (true)
4674 tree expression;
4676 /* Look for the opening `['. */
4677 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4678 /* The first expression is not required to be constant. */
4679 if (!declarator)
4681 expression = cp_parser_expression (parser);
4682 /* The standard requires that the expression have integral
4683 type. DR 74 adds enumeration types. We believe that the
4684 real intent is that these expressions be handled like the
4685 expression in a `switch' condition, which also allows
4686 classes with a single conversion to integral or
4687 enumeration type. */
4688 if (!processing_template_decl)
4690 expression
4691 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4692 expression,
4693 /*complain=*/true);
4694 if (!expression)
4696 error ("expression in new-declarator must have integral or enumeration type");
4697 expression = error_mark_node;
4701 /* But all the other expressions must be. */
4702 else
4703 expression
4704 = cp_parser_constant_expression (parser,
4705 /*allow_non_constant=*/false,
4706 NULL);
4707 /* Look for the closing `]'. */
4708 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4710 /* Add this bound to the declarator. */
4711 declarator = build_nt (ARRAY_REF, declarator, expression);
4713 /* If the next token is not a `[', then there are no more
4714 bounds. */
4715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4716 break;
4719 return declarator;
4722 /* Parse a new-initializer.
4724 new-initializer:
4725 ( expression-list [opt] )
4727 Returns a representation of the expression-list. If there is no
4728 expression-list, VOID_ZERO_NODE is returned. */
4730 static tree
4731 cp_parser_new_initializer (cp_parser* parser)
4733 tree expression_list;
4735 expression_list = (cp_parser_parenthesized_expression_list
4736 (parser, false, /*non_constant_p=*/NULL));
4737 if (!expression_list)
4738 expression_list = void_zero_node;
4740 return expression_list;
4743 /* Parse a delete-expression.
4745 delete-expression:
4746 :: [opt] delete cast-expression
4747 :: [opt] delete [ ] cast-expression
4749 Returns a representation of the expression. */
4751 static tree
4752 cp_parser_delete_expression (cp_parser* parser)
4754 bool global_scope_p;
4755 bool array_p;
4756 tree expression;
4758 /* Look for the optional `::' operator. */
4759 global_scope_p
4760 = (cp_parser_global_scope_opt (parser,
4761 /*current_scope_valid_p=*/false)
4762 != NULL_TREE);
4763 /* Look for the `delete' keyword. */
4764 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4765 /* See if the array syntax is in use. */
4766 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4768 /* Consume the `[' token. */
4769 cp_lexer_consume_token (parser->lexer);
4770 /* Look for the `]' token. */
4771 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4772 /* Remember that this is the `[]' construct. */
4773 array_p = true;
4775 else
4776 array_p = false;
4778 /* Parse the cast-expression. */
4779 expression = cp_parser_simple_cast_expression (parser);
4781 /* A delete-expression may not appear in an integral constant
4782 expression. */
4783 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4784 return error_mark_node;
4786 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4789 /* Parse a cast-expression.
4791 cast-expression:
4792 unary-expression
4793 ( type-id ) cast-expression
4795 Returns a representation of the expression. */
4797 static tree
4798 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4800 /* If it's a `(', then we might be looking at a cast. */
4801 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4803 tree type = NULL_TREE;
4804 tree expr = NULL_TREE;
4805 bool compound_literal_p;
4806 const char *saved_message;
4808 /* There's no way to know yet whether or not this is a cast.
4809 For example, `(int (3))' is a unary-expression, while `(int)
4810 3' is a cast. So, we resort to parsing tentatively. */
4811 cp_parser_parse_tentatively (parser);
4812 /* Types may not be defined in a cast. */
4813 saved_message = parser->type_definition_forbidden_message;
4814 parser->type_definition_forbidden_message
4815 = "types may not be defined in casts";
4816 /* Consume the `('. */
4817 cp_lexer_consume_token (parser->lexer);
4818 /* A very tricky bit is that `(struct S) { 3 }' is a
4819 compound-literal (which we permit in C++ as an extension).
4820 But, that construct is not a cast-expression -- it is a
4821 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4822 is legal; if the compound-literal were a cast-expression,
4823 you'd need an extra set of parentheses.) But, if we parse
4824 the type-id, and it happens to be a class-specifier, then we
4825 will commit to the parse at that point, because we cannot
4826 undo the action that is done when creating a new class. So,
4827 then we cannot back up and do a postfix-expression.
4829 Therefore, we scan ahead to the closing `)', and check to see
4830 if the token after the `)' is a `{'. If so, we are not
4831 looking at a cast-expression.
4833 Save tokens so that we can put them back. */
4834 cp_lexer_save_tokens (parser->lexer);
4835 /* Skip tokens until the next token is a closing parenthesis.
4836 If we find the closing `)', and the next token is a `{', then
4837 we are looking at a compound-literal. */
4838 compound_literal_p
4839 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4840 /*consume_paren=*/true)
4841 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4842 /* Roll back the tokens we skipped. */
4843 cp_lexer_rollback_tokens (parser->lexer);
4844 /* If we were looking at a compound-literal, simulate an error
4845 so that the call to cp_parser_parse_definitely below will
4846 fail. */
4847 if (compound_literal_p)
4848 cp_parser_simulate_error (parser);
4849 else
4851 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4852 parser->in_type_id_in_expr_p = true;
4853 /* Look for the type-id. */
4854 type = cp_parser_type_id (parser);
4855 /* Look for the closing `)'. */
4856 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4857 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4860 /* Restore the saved message. */
4861 parser->type_definition_forbidden_message = saved_message;
4863 /* If ok so far, parse the dependent expression. We cannot be
4864 sure it is a cast. Consider `(T ())'. It is a parenthesized
4865 ctor of T, but looks like a cast to function returning T
4866 without a dependent expression. */
4867 if (!cp_parser_error_occurred (parser))
4868 expr = cp_parser_simple_cast_expression (parser);
4870 if (cp_parser_parse_definitely (parser))
4872 /* Warn about old-style casts, if so requested. */
4873 if (warn_old_style_cast
4874 && !in_system_header
4875 && !VOID_TYPE_P (type)
4876 && current_lang_name != lang_name_c)
4877 warning ("use of old-style cast");
4879 /* Only type conversions to integral or enumeration types
4880 can be used in constant-expressions. */
4881 if (parser->integral_constant_expression_p
4882 && !dependent_type_p (type)
4883 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4884 && (cp_parser_non_integral_constant_expression
4885 (parser,
4886 "a cast to a type other than an integral or "
4887 "enumeration type")))
4888 return error_mark_node;
4890 /* Perform the cast. */
4891 expr = build_c_cast (type, expr);
4892 return expr;
4896 /* If we get here, then it's not a cast, so it must be a
4897 unary-expression. */
4898 return cp_parser_unary_expression (parser, address_p);
4901 /* Parse a pm-expression.
4903 pm-expression:
4904 cast-expression
4905 pm-expression .* cast-expression
4906 pm-expression ->* cast-expression
4908 Returns a representation of the expression. */
4910 static tree
4911 cp_parser_pm_expression (cp_parser* parser)
4913 static const cp_parser_token_tree_map map = {
4914 { CPP_DEREF_STAR, MEMBER_REF },
4915 { CPP_DOT_STAR, DOTSTAR_EXPR },
4916 { CPP_EOF, ERROR_MARK }
4919 return cp_parser_binary_expression (parser, map,
4920 cp_parser_simple_cast_expression);
4923 /* Parse a multiplicative-expression.
4925 multiplicative-expression:
4926 pm-expression
4927 multiplicative-expression * pm-expression
4928 multiplicative-expression / pm-expression
4929 multiplicative-expression % pm-expression
4931 Returns a representation of the expression. */
4933 static tree
4934 cp_parser_multiplicative_expression (cp_parser* parser)
4936 static const cp_parser_token_tree_map map = {
4937 { CPP_MULT, MULT_EXPR },
4938 { CPP_DIV, TRUNC_DIV_EXPR },
4939 { CPP_MOD, TRUNC_MOD_EXPR },
4940 { CPP_EOF, ERROR_MARK }
4943 return cp_parser_binary_expression (parser,
4944 map,
4945 cp_parser_pm_expression);
4948 /* Parse an additive-expression.
4950 additive-expression:
4951 multiplicative-expression
4952 additive-expression + multiplicative-expression
4953 additive-expression - multiplicative-expression
4955 Returns a representation of the expression. */
4957 static tree
4958 cp_parser_additive_expression (cp_parser* parser)
4960 static const cp_parser_token_tree_map map = {
4961 { CPP_PLUS, PLUS_EXPR },
4962 { CPP_MINUS, MINUS_EXPR },
4963 { CPP_EOF, ERROR_MARK }
4966 return cp_parser_binary_expression (parser,
4967 map,
4968 cp_parser_multiplicative_expression);
4971 /* Parse a shift-expression.
4973 shift-expression:
4974 additive-expression
4975 shift-expression << additive-expression
4976 shift-expression >> additive-expression
4978 Returns a representation of the expression. */
4980 static tree
4981 cp_parser_shift_expression (cp_parser* parser)
4983 static const cp_parser_token_tree_map map = {
4984 { CPP_LSHIFT, LSHIFT_EXPR },
4985 { CPP_RSHIFT, RSHIFT_EXPR },
4986 { CPP_EOF, ERROR_MARK }
4989 return cp_parser_binary_expression (parser,
4990 map,
4991 cp_parser_additive_expression);
4994 /* Parse a relational-expression.
4996 relational-expression:
4997 shift-expression
4998 relational-expression < shift-expression
4999 relational-expression > shift-expression
5000 relational-expression <= shift-expression
5001 relational-expression >= shift-expression
5003 GNU Extension:
5005 relational-expression:
5006 relational-expression <? shift-expression
5007 relational-expression >? shift-expression
5009 Returns a representation of the expression. */
5011 static tree
5012 cp_parser_relational_expression (cp_parser* parser)
5014 static const cp_parser_token_tree_map map = {
5015 { CPP_LESS, LT_EXPR },
5016 { CPP_GREATER, GT_EXPR },
5017 { CPP_LESS_EQ, LE_EXPR },
5018 { CPP_GREATER_EQ, GE_EXPR },
5019 { CPP_MIN, MIN_EXPR },
5020 { CPP_MAX, MAX_EXPR },
5021 { CPP_EOF, ERROR_MARK }
5024 return cp_parser_binary_expression (parser,
5025 map,
5026 cp_parser_shift_expression);
5029 /* Parse an equality-expression.
5031 equality-expression:
5032 relational-expression
5033 equality-expression == relational-expression
5034 equality-expression != relational-expression
5036 Returns a representation of the expression. */
5038 static tree
5039 cp_parser_equality_expression (cp_parser* parser)
5041 static const cp_parser_token_tree_map map = {
5042 { CPP_EQ_EQ, EQ_EXPR },
5043 { CPP_NOT_EQ, NE_EXPR },
5044 { CPP_EOF, ERROR_MARK }
5047 return cp_parser_binary_expression (parser,
5048 map,
5049 cp_parser_relational_expression);
5052 /* Parse an and-expression.
5054 and-expression:
5055 equality-expression
5056 and-expression & equality-expression
5058 Returns a representation of the expression. */
5060 static tree
5061 cp_parser_and_expression (cp_parser* parser)
5063 static const cp_parser_token_tree_map map = {
5064 { CPP_AND, BIT_AND_EXPR },
5065 { CPP_EOF, ERROR_MARK }
5068 return cp_parser_binary_expression (parser,
5069 map,
5070 cp_parser_equality_expression);
5073 /* Parse an exclusive-or-expression.
5075 exclusive-or-expression:
5076 and-expression
5077 exclusive-or-expression ^ and-expression
5079 Returns a representation of the expression. */
5081 static tree
5082 cp_parser_exclusive_or_expression (cp_parser* parser)
5084 static const cp_parser_token_tree_map map = {
5085 { CPP_XOR, BIT_XOR_EXPR },
5086 { CPP_EOF, ERROR_MARK }
5089 return cp_parser_binary_expression (parser,
5090 map,
5091 cp_parser_and_expression);
5095 /* Parse an inclusive-or-expression.
5097 inclusive-or-expression:
5098 exclusive-or-expression
5099 inclusive-or-expression | exclusive-or-expression
5101 Returns a representation of the expression. */
5103 static tree
5104 cp_parser_inclusive_or_expression (cp_parser* parser)
5106 static const cp_parser_token_tree_map map = {
5107 { CPP_OR, BIT_IOR_EXPR },
5108 { CPP_EOF, ERROR_MARK }
5111 return cp_parser_binary_expression (parser,
5112 map,
5113 cp_parser_exclusive_or_expression);
5116 /* Parse a logical-and-expression.
5118 logical-and-expression:
5119 inclusive-or-expression
5120 logical-and-expression && inclusive-or-expression
5122 Returns a representation of the expression. */
5124 static tree
5125 cp_parser_logical_and_expression (cp_parser* parser)
5127 static const cp_parser_token_tree_map map = {
5128 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5129 { CPP_EOF, ERROR_MARK }
5132 return cp_parser_binary_expression (parser,
5133 map,
5134 cp_parser_inclusive_or_expression);
5137 /* Parse a logical-or-expression.
5139 logical-or-expression:
5140 logical-and-expression
5141 logical-or-expression || logical-and-expression
5143 Returns a representation of the expression. */
5145 static tree
5146 cp_parser_logical_or_expression (cp_parser* parser)
5148 static const cp_parser_token_tree_map map = {
5149 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5150 { CPP_EOF, ERROR_MARK }
5153 return cp_parser_binary_expression (parser,
5154 map,
5155 cp_parser_logical_and_expression);
5158 /* Parse the `? expression : assignment-expression' part of a
5159 conditional-expression. The LOGICAL_OR_EXPR is the
5160 logical-or-expression that started the conditional-expression.
5161 Returns a representation of the entire conditional-expression.
5163 This routine is used by cp_parser_assignment_expression.
5165 ? expression : assignment-expression
5167 GNU Extensions:
5169 ? : assignment-expression */
5171 static tree
5172 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5174 tree expr;
5175 tree assignment_expr;
5177 /* Consume the `?' token. */
5178 cp_lexer_consume_token (parser->lexer);
5179 if (cp_parser_allow_gnu_extensions_p (parser)
5180 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5181 /* Implicit true clause. */
5182 expr = NULL_TREE;
5183 else
5184 /* Parse the expression. */
5185 expr = cp_parser_expression (parser);
5187 /* The next token should be a `:'. */
5188 cp_parser_require (parser, CPP_COLON, "`:'");
5189 /* Parse the assignment-expression. */
5190 assignment_expr = cp_parser_assignment_expression (parser);
5192 /* Build the conditional-expression. */
5193 return build_x_conditional_expr (logical_or_expr,
5194 expr,
5195 assignment_expr);
5198 /* Parse an assignment-expression.
5200 assignment-expression:
5201 conditional-expression
5202 logical-or-expression assignment-operator assignment_expression
5203 throw-expression
5205 Returns a representation for the expression. */
5207 static tree
5208 cp_parser_assignment_expression (cp_parser* parser)
5210 tree expr;
5212 /* If the next token is the `throw' keyword, then we're looking at
5213 a throw-expression. */
5214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5215 expr = cp_parser_throw_expression (parser);
5216 /* Otherwise, it must be that we are looking at a
5217 logical-or-expression. */
5218 else
5220 /* Parse the logical-or-expression. */
5221 expr = cp_parser_logical_or_expression (parser);
5222 /* If the next token is a `?' then we're actually looking at a
5223 conditional-expression. */
5224 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5225 return cp_parser_question_colon_clause (parser, expr);
5226 else
5228 enum tree_code assignment_operator;
5230 /* If it's an assignment-operator, we're using the second
5231 production. */
5232 assignment_operator
5233 = cp_parser_assignment_operator_opt (parser);
5234 if (assignment_operator != ERROR_MARK)
5236 tree rhs;
5238 /* Parse the right-hand side of the assignment. */
5239 rhs = cp_parser_assignment_expression (parser);
5240 /* An assignment may not appear in a
5241 constant-expression. */
5242 if (cp_parser_non_integral_constant_expression (parser,
5243 "an assignment"))
5244 return error_mark_node;
5245 /* Build the assignment expression. */
5246 expr = build_x_modify_expr (expr,
5247 assignment_operator,
5248 rhs);
5253 return expr;
5256 /* Parse an (optional) assignment-operator.
5258 assignment-operator: one of
5259 = *= /= %= += -= >>= <<= &= ^= |=
5261 GNU Extension:
5263 assignment-operator: one of
5264 <?= >?=
5266 If the next token is an assignment operator, the corresponding tree
5267 code is returned, and the token is consumed. For example, for
5268 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5269 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5270 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5271 operator, ERROR_MARK is returned. */
5273 static enum tree_code
5274 cp_parser_assignment_operator_opt (cp_parser* parser)
5276 enum tree_code op;
5277 cp_token *token;
5279 /* Peek at the next toen. */
5280 token = cp_lexer_peek_token (parser->lexer);
5282 switch (token->type)
5284 case CPP_EQ:
5285 op = NOP_EXPR;
5286 break;
5288 case CPP_MULT_EQ:
5289 op = MULT_EXPR;
5290 break;
5292 case CPP_DIV_EQ:
5293 op = TRUNC_DIV_EXPR;
5294 break;
5296 case CPP_MOD_EQ:
5297 op = TRUNC_MOD_EXPR;
5298 break;
5300 case CPP_PLUS_EQ:
5301 op = PLUS_EXPR;
5302 break;
5304 case CPP_MINUS_EQ:
5305 op = MINUS_EXPR;
5306 break;
5308 case CPP_RSHIFT_EQ:
5309 op = RSHIFT_EXPR;
5310 break;
5312 case CPP_LSHIFT_EQ:
5313 op = LSHIFT_EXPR;
5314 break;
5316 case CPP_AND_EQ:
5317 op = BIT_AND_EXPR;
5318 break;
5320 case CPP_XOR_EQ:
5321 op = BIT_XOR_EXPR;
5322 break;
5324 case CPP_OR_EQ:
5325 op = BIT_IOR_EXPR;
5326 break;
5328 case CPP_MIN_EQ:
5329 op = MIN_EXPR;
5330 break;
5332 case CPP_MAX_EQ:
5333 op = MAX_EXPR;
5334 break;
5336 default:
5337 /* Nothing else is an assignment operator. */
5338 op = ERROR_MARK;
5341 /* If it was an assignment operator, consume it. */
5342 if (op != ERROR_MARK)
5343 cp_lexer_consume_token (parser->lexer);
5345 return op;
5348 /* Parse an expression.
5350 expression:
5351 assignment-expression
5352 expression , assignment-expression
5354 Returns a representation of the expression. */
5356 static tree
5357 cp_parser_expression (cp_parser* parser)
5359 tree expression = NULL_TREE;
5361 while (true)
5363 tree assignment_expression;
5365 /* Parse the next assignment-expression. */
5366 assignment_expression
5367 = cp_parser_assignment_expression (parser);
5368 /* If this is the first assignment-expression, we can just
5369 save it away. */
5370 if (!expression)
5371 expression = assignment_expression;
5372 else
5373 expression = build_x_compound_expr (expression,
5374 assignment_expression);
5375 /* If the next token is not a comma, then we are done with the
5376 expression. */
5377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5378 break;
5379 /* Consume the `,'. */
5380 cp_lexer_consume_token (parser->lexer);
5381 /* A comma operator cannot appear in a constant-expression. */
5382 if (cp_parser_non_integral_constant_expression (parser,
5383 "a comma operator"))
5384 expression = error_mark_node;
5387 return expression;
5390 /* Parse a constant-expression.
5392 constant-expression:
5393 conditional-expression
5395 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5396 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5397 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5398 is false, NON_CONSTANT_P should be NULL. */
5400 static tree
5401 cp_parser_constant_expression (cp_parser* parser,
5402 bool allow_non_constant_p,
5403 bool *non_constant_p)
5405 bool saved_integral_constant_expression_p;
5406 bool saved_allow_non_integral_constant_expression_p;
5407 bool saved_non_integral_constant_expression_p;
5408 tree expression;
5410 /* It might seem that we could simply parse the
5411 conditional-expression, and then check to see if it were
5412 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5413 one that the compiler can figure out is constant, possibly after
5414 doing some simplifications or optimizations. The standard has a
5415 precise definition of constant-expression, and we must honor
5416 that, even though it is somewhat more restrictive.
5418 For example:
5420 int i[(2, 3)];
5422 is not a legal declaration, because `(2, 3)' is not a
5423 constant-expression. The `,' operator is forbidden in a
5424 constant-expression. However, GCC's constant-folding machinery
5425 will fold this operation to an INTEGER_CST for `3'. */
5427 /* Save the old settings. */
5428 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5429 saved_allow_non_integral_constant_expression_p
5430 = parser->allow_non_integral_constant_expression_p;
5431 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5432 /* We are now parsing a constant-expression. */
5433 parser->integral_constant_expression_p = true;
5434 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5435 parser->non_integral_constant_expression_p = false;
5436 /* Although the grammar says "conditional-expression", we parse an
5437 "assignment-expression", which also permits "throw-expression"
5438 and the use of assignment operators. In the case that
5439 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5440 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5441 actually essential that we look for an assignment-expression.
5442 For example, cp_parser_initializer_clauses uses this function to
5443 determine whether a particular assignment-expression is in fact
5444 constant. */
5445 expression = cp_parser_assignment_expression (parser);
5446 /* Restore the old settings. */
5447 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5448 parser->allow_non_integral_constant_expression_p
5449 = saved_allow_non_integral_constant_expression_p;
5450 if (allow_non_constant_p)
5451 *non_constant_p = parser->non_integral_constant_expression_p;
5452 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5454 return expression;
5457 /* Statements [gram.stmt.stmt] */
5459 /* Parse a statement.
5461 statement:
5462 labeled-statement
5463 expression-statement
5464 compound-statement
5465 selection-statement
5466 iteration-statement
5467 jump-statement
5468 declaration-statement
5469 try-block */
5471 static void
5472 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5474 tree statement;
5475 cp_token *token;
5476 int statement_line_number;
5478 /* There is no statement yet. */
5479 statement = NULL_TREE;
5480 /* Peek at the next token. */
5481 token = cp_lexer_peek_token (parser->lexer);
5482 /* Remember the line number of the first token in the statement. */
5483 statement_line_number = token->location.line;
5484 /* If this is a keyword, then that will often determine what kind of
5485 statement we have. */
5486 if (token->type == CPP_KEYWORD)
5488 enum rid keyword = token->keyword;
5490 switch (keyword)
5492 case RID_CASE:
5493 case RID_DEFAULT:
5494 statement = cp_parser_labeled_statement (parser,
5495 in_statement_expr_p);
5496 break;
5498 case RID_IF:
5499 case RID_SWITCH:
5500 statement = cp_parser_selection_statement (parser);
5501 break;
5503 case RID_WHILE:
5504 case RID_DO:
5505 case RID_FOR:
5506 statement = cp_parser_iteration_statement (parser);
5507 break;
5509 case RID_BREAK:
5510 case RID_CONTINUE:
5511 case RID_RETURN:
5512 case RID_GOTO:
5513 statement = cp_parser_jump_statement (parser);
5514 break;
5516 case RID_TRY:
5517 statement = cp_parser_try_block (parser);
5518 break;
5520 default:
5521 /* It might be a keyword like `int' that can start a
5522 declaration-statement. */
5523 break;
5526 else if (token->type == CPP_NAME)
5528 /* If the next token is a `:', then we are looking at a
5529 labeled-statement. */
5530 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5531 if (token->type == CPP_COLON)
5532 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5534 /* Anything that starts with a `{' must be a compound-statement. */
5535 else if (token->type == CPP_OPEN_BRACE)
5536 statement = cp_parser_compound_statement (parser, false);
5538 /* Everything else must be a declaration-statement or an
5539 expression-statement. Try for the declaration-statement
5540 first, unless we are looking at a `;', in which case we know that
5541 we have an expression-statement. */
5542 if (!statement)
5544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5546 cp_parser_parse_tentatively (parser);
5547 /* Try to parse the declaration-statement. */
5548 cp_parser_declaration_statement (parser);
5549 /* If that worked, we're done. */
5550 if (cp_parser_parse_definitely (parser))
5551 return;
5553 /* Look for an expression-statement instead. */
5554 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5557 /* Set the line number for the statement. */
5558 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5559 STMT_LINENO (statement) = statement_line_number;
5562 /* Parse a labeled-statement.
5564 labeled-statement:
5565 identifier : statement
5566 case constant-expression : statement
5567 default : statement
5569 GNU Extension:
5571 labeled-statement:
5572 case constant-expression ... constant-expression : statement
5574 Returns the new CASE_LABEL, for a `case' or `default' label. For
5575 an ordinary label, returns a LABEL_STMT. */
5577 static tree
5578 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5580 cp_token *token;
5581 tree statement = error_mark_node;
5583 /* The next token should be an identifier. */
5584 token = cp_lexer_peek_token (parser->lexer);
5585 if (token->type != CPP_NAME
5586 && token->type != CPP_KEYWORD)
5588 cp_parser_error (parser, "expected labeled-statement");
5589 return error_mark_node;
5592 switch (token->keyword)
5594 case RID_CASE:
5596 tree expr, expr_hi;
5597 cp_token *ellipsis;
5599 /* Consume the `case' token. */
5600 cp_lexer_consume_token (parser->lexer);
5601 /* Parse the constant-expression. */
5602 expr = cp_parser_constant_expression (parser,
5603 /*allow_non_constant_p=*/false,
5604 NULL);
5606 ellipsis = cp_lexer_peek_token (parser->lexer);
5607 if (ellipsis->type == CPP_ELLIPSIS)
5609 /* Consume the `...' token. */
5610 cp_lexer_consume_token (parser->lexer);
5611 expr_hi =
5612 cp_parser_constant_expression (parser,
5613 /*allow_non_constant_p=*/false,
5614 NULL);
5615 /* We don't need to emit warnings here, as the common code
5616 will do this for us. */
5618 else
5619 expr_hi = NULL_TREE;
5621 if (!parser->in_switch_statement_p)
5622 error ("case label `%E' not within a switch statement", expr);
5623 else
5624 statement = finish_case_label (expr, expr_hi);
5626 break;
5628 case RID_DEFAULT:
5629 /* Consume the `default' token. */
5630 cp_lexer_consume_token (parser->lexer);
5631 if (!parser->in_switch_statement_p)
5632 error ("case label not within a switch statement");
5633 else
5634 statement = finish_case_label (NULL_TREE, NULL_TREE);
5635 break;
5637 default:
5638 /* Anything else must be an ordinary label. */
5639 statement = finish_label_stmt (cp_parser_identifier (parser));
5640 break;
5643 /* Require the `:' token. */
5644 cp_parser_require (parser, CPP_COLON, "`:'");
5645 /* Parse the labeled statement. */
5646 cp_parser_statement (parser, in_statement_expr_p);
5648 /* Return the label, in the case of a `case' or `default' label. */
5649 return statement;
5652 /* Parse an expression-statement.
5654 expression-statement:
5655 expression [opt] ;
5657 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5658 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5659 indicates whether this expression-statement is part of an
5660 expression statement. */
5662 static tree
5663 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5665 tree statement = NULL_TREE;
5667 /* If the next token is a ';', then there is no expression
5668 statement. */
5669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5670 statement = cp_parser_expression (parser);
5672 /* Consume the final `;'. */
5673 cp_parser_consume_semicolon_at_end_of_statement (parser);
5675 if (in_statement_expr_p
5676 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5678 /* This is the final expression statement of a statement
5679 expression. */
5680 statement = finish_stmt_expr_expr (statement);
5682 else if (statement)
5683 statement = finish_expr_stmt (statement);
5684 else
5685 finish_stmt ();
5687 return statement;
5690 /* Parse a compound-statement.
5692 compound-statement:
5693 { statement-seq [opt] }
5695 Returns a COMPOUND_STMT representing the statement. */
5697 static tree
5698 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5700 tree compound_stmt;
5702 /* Consume the `{'. */
5703 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5704 return error_mark_node;
5705 /* Begin the compound-statement. */
5706 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5707 /* Parse an (optional) statement-seq. */
5708 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5709 /* Finish the compound-statement. */
5710 finish_compound_stmt (compound_stmt);
5711 /* Consume the `}'. */
5712 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5714 return compound_stmt;
5717 /* Parse an (optional) statement-seq.
5719 statement-seq:
5720 statement
5721 statement-seq [opt] statement */
5723 static void
5724 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5726 /* Scan statements until there aren't any more. */
5727 while (true)
5729 /* If we're looking at a `}', then we've run out of statements. */
5730 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5731 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5732 break;
5734 /* Parse the statement. */
5735 cp_parser_statement (parser, in_statement_expr_p);
5739 /* Parse a selection-statement.
5741 selection-statement:
5742 if ( condition ) statement
5743 if ( condition ) statement else statement
5744 switch ( condition ) statement
5746 Returns the new IF_STMT or SWITCH_STMT. */
5748 static tree
5749 cp_parser_selection_statement (cp_parser* parser)
5751 cp_token *token;
5752 enum rid keyword;
5754 /* Peek at the next token. */
5755 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5757 /* See what kind of keyword it is. */
5758 keyword = token->keyword;
5759 switch (keyword)
5761 case RID_IF:
5762 case RID_SWITCH:
5764 tree statement;
5765 tree condition;
5767 /* Look for the `('. */
5768 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5770 cp_parser_skip_to_end_of_statement (parser);
5771 return error_mark_node;
5774 /* Begin the selection-statement. */
5775 if (keyword == RID_IF)
5776 statement = begin_if_stmt ();
5777 else
5778 statement = begin_switch_stmt ();
5780 /* Parse the condition. */
5781 condition = cp_parser_condition (parser);
5782 /* Look for the `)'. */
5783 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5784 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5785 /*consume_paren=*/true);
5787 if (keyword == RID_IF)
5789 tree then_stmt;
5791 /* Add the condition. */
5792 finish_if_stmt_cond (condition, statement);
5794 /* Parse the then-clause. */
5795 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5796 finish_then_clause (statement);
5798 /* If the next token is `else', parse the else-clause. */
5799 if (cp_lexer_next_token_is_keyword (parser->lexer,
5800 RID_ELSE))
5802 tree else_stmt;
5804 /* Consume the `else' keyword. */
5805 cp_lexer_consume_token (parser->lexer);
5806 /* Parse the else-clause. */
5807 else_stmt
5808 = cp_parser_implicitly_scoped_statement (parser);
5809 finish_else_clause (statement);
5812 /* Now we're all done with the if-statement. */
5813 finish_if_stmt ();
5815 else
5817 tree body;
5818 bool in_switch_statement_p;
5820 /* Add the condition. */
5821 finish_switch_cond (condition, statement);
5823 /* Parse the body of the switch-statement. */
5824 in_switch_statement_p = parser->in_switch_statement_p;
5825 parser->in_switch_statement_p = true;
5826 body = cp_parser_implicitly_scoped_statement (parser);
5827 parser->in_switch_statement_p = in_switch_statement_p;
5829 /* Now we're all done with the switch-statement. */
5830 finish_switch_stmt (statement);
5833 return statement;
5835 break;
5837 default:
5838 cp_parser_error (parser, "expected selection-statement");
5839 return error_mark_node;
5843 /* Parse a condition.
5845 condition:
5846 expression
5847 type-specifier-seq declarator = assignment-expression
5849 GNU Extension:
5851 condition:
5852 type-specifier-seq declarator asm-specification [opt]
5853 attributes [opt] = assignment-expression
5855 Returns the expression that should be tested. */
5857 static tree
5858 cp_parser_condition (cp_parser* parser)
5860 tree type_specifiers;
5861 const char *saved_message;
5863 /* Try the declaration first. */
5864 cp_parser_parse_tentatively (parser);
5865 /* New types are not allowed in the type-specifier-seq for a
5866 condition. */
5867 saved_message = parser->type_definition_forbidden_message;
5868 parser->type_definition_forbidden_message
5869 = "types may not be defined in conditions";
5870 /* Parse the type-specifier-seq. */
5871 type_specifiers = cp_parser_type_specifier_seq (parser);
5872 /* Restore the saved message. */
5873 parser->type_definition_forbidden_message = saved_message;
5874 /* If all is well, we might be looking at a declaration. */
5875 if (!cp_parser_error_occurred (parser))
5877 tree decl;
5878 tree asm_specification;
5879 tree attributes;
5880 tree declarator;
5881 tree initializer = NULL_TREE;
5883 /* Parse the declarator. */
5884 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5885 /*ctor_dtor_or_conv_p=*/NULL,
5886 /*parenthesized_p=*/NULL);
5887 /* Parse the attributes. */
5888 attributes = cp_parser_attributes_opt (parser);
5889 /* Parse the asm-specification. */
5890 asm_specification = cp_parser_asm_specification_opt (parser);
5891 /* If the next token is not an `=', then we might still be
5892 looking at an expression. For example:
5894 if (A(a).x)
5896 looks like a decl-specifier-seq and a declarator -- but then
5897 there is no `=', so this is an expression. */
5898 cp_parser_require (parser, CPP_EQ, "`='");
5899 /* If we did see an `=', then we are looking at a declaration
5900 for sure. */
5901 if (cp_parser_parse_definitely (parser))
5903 /* Create the declaration. */
5904 decl = start_decl (declarator, type_specifiers,
5905 /*initialized_p=*/true,
5906 attributes, /*prefix_attributes=*/NULL_TREE);
5907 /* Parse the assignment-expression. */
5908 initializer = cp_parser_assignment_expression (parser);
5910 /* Process the initializer. */
5911 cp_finish_decl (decl,
5912 initializer,
5913 asm_specification,
5914 LOOKUP_ONLYCONVERTING);
5916 return convert_from_reference (decl);
5919 /* If we didn't even get past the declarator successfully, we are
5920 definitely not looking at a declaration. */
5921 else
5922 cp_parser_abort_tentative_parse (parser);
5924 /* Otherwise, we are looking at an expression. */
5925 return cp_parser_expression (parser);
5928 /* Parse an iteration-statement.
5930 iteration-statement:
5931 while ( condition ) statement
5932 do statement while ( expression ) ;
5933 for ( for-init-statement condition [opt] ; expression [opt] )
5934 statement
5936 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5938 static tree
5939 cp_parser_iteration_statement (cp_parser* parser)
5941 cp_token *token;
5942 enum rid keyword;
5943 tree statement;
5944 bool in_iteration_statement_p;
5947 /* Peek at the next token. */
5948 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5949 if (!token)
5950 return error_mark_node;
5952 /* Remember whether or not we are already within an iteration
5953 statement. */
5954 in_iteration_statement_p = parser->in_iteration_statement_p;
5956 /* See what kind of keyword it is. */
5957 keyword = token->keyword;
5958 switch (keyword)
5960 case RID_WHILE:
5962 tree condition;
5964 /* Begin the while-statement. */
5965 statement = begin_while_stmt ();
5966 /* Look for the `('. */
5967 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5968 /* Parse the condition. */
5969 condition = cp_parser_condition (parser);
5970 finish_while_stmt_cond (condition, statement);
5971 /* Look for the `)'. */
5972 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5973 /* Parse the dependent statement. */
5974 parser->in_iteration_statement_p = true;
5975 cp_parser_already_scoped_statement (parser);
5976 parser->in_iteration_statement_p = in_iteration_statement_p;
5977 /* We're done with the while-statement. */
5978 finish_while_stmt (statement);
5980 break;
5982 case RID_DO:
5984 tree expression;
5986 /* Begin the do-statement. */
5987 statement = begin_do_stmt ();
5988 /* Parse the body of the do-statement. */
5989 parser->in_iteration_statement_p = true;
5990 cp_parser_implicitly_scoped_statement (parser);
5991 parser->in_iteration_statement_p = in_iteration_statement_p;
5992 finish_do_body (statement);
5993 /* Look for the `while' keyword. */
5994 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5995 /* Look for the `('. */
5996 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5997 /* Parse the expression. */
5998 expression = cp_parser_expression (parser);
5999 /* We're done with the do-statement. */
6000 finish_do_stmt (expression, statement);
6001 /* Look for the `)'. */
6002 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6003 /* Look for the `;'. */
6004 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6006 break;
6008 case RID_FOR:
6010 tree condition = NULL_TREE;
6011 tree expression = NULL_TREE;
6013 /* Begin the for-statement. */
6014 statement = begin_for_stmt ();
6015 /* Look for the `('. */
6016 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6017 /* Parse the initialization. */
6018 cp_parser_for_init_statement (parser);
6019 finish_for_init_stmt (statement);
6021 /* If there's a condition, process it. */
6022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6023 condition = cp_parser_condition (parser);
6024 finish_for_cond (condition, statement);
6025 /* Look for the `;'. */
6026 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6028 /* If there's an expression, process it. */
6029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6030 expression = cp_parser_expression (parser);
6031 finish_for_expr (expression, statement);
6032 /* Look for the `)'. */
6033 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6035 /* Parse the body of the for-statement. */
6036 parser->in_iteration_statement_p = true;
6037 cp_parser_already_scoped_statement (parser);
6038 parser->in_iteration_statement_p = in_iteration_statement_p;
6040 /* We're done with the for-statement. */
6041 finish_for_stmt (statement);
6043 break;
6045 default:
6046 cp_parser_error (parser, "expected iteration-statement");
6047 statement = error_mark_node;
6048 break;
6051 return statement;
6054 /* Parse a for-init-statement.
6056 for-init-statement:
6057 expression-statement
6058 simple-declaration */
6060 static void
6061 cp_parser_for_init_statement (cp_parser* parser)
6063 /* If the next token is a `;', then we have an empty
6064 expression-statement. Grammatically, this is also a
6065 simple-declaration, but an invalid one, because it does not
6066 declare anything. Therefore, if we did not handle this case
6067 specially, we would issue an error message about an invalid
6068 declaration. */
6069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6071 /* We're going to speculatively look for a declaration, falling back
6072 to an expression, if necessary. */
6073 cp_parser_parse_tentatively (parser);
6074 /* Parse the declaration. */
6075 cp_parser_simple_declaration (parser,
6076 /*function_definition_allowed_p=*/false);
6077 /* If the tentative parse failed, then we shall need to look for an
6078 expression-statement. */
6079 if (cp_parser_parse_definitely (parser))
6080 return;
6083 cp_parser_expression_statement (parser, false);
6086 /* Parse a jump-statement.
6088 jump-statement:
6089 break ;
6090 continue ;
6091 return expression [opt] ;
6092 goto identifier ;
6094 GNU extension:
6096 jump-statement:
6097 goto * expression ;
6099 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6100 GOTO_STMT. */
6102 static tree
6103 cp_parser_jump_statement (cp_parser* parser)
6105 tree statement = error_mark_node;
6106 cp_token *token;
6107 enum rid keyword;
6109 /* Peek at the next token. */
6110 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6111 if (!token)
6112 return error_mark_node;
6114 /* See what kind of keyword it is. */
6115 keyword = token->keyword;
6116 switch (keyword)
6118 case RID_BREAK:
6119 if (!parser->in_switch_statement_p
6120 && !parser->in_iteration_statement_p)
6122 error ("break statement not within loop or switch");
6123 statement = error_mark_node;
6125 else
6126 statement = finish_break_stmt ();
6127 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6128 break;
6130 case RID_CONTINUE:
6131 if (!parser->in_iteration_statement_p)
6133 error ("continue statement not within a loop");
6134 statement = error_mark_node;
6136 else
6137 statement = finish_continue_stmt ();
6138 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6139 break;
6141 case RID_RETURN:
6143 tree expr;
6145 /* If the next token is a `;', then there is no
6146 expression. */
6147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6148 expr = cp_parser_expression (parser);
6149 else
6150 expr = NULL_TREE;
6151 /* Build the return-statement. */
6152 statement = finish_return_stmt (expr);
6153 /* Look for the final `;'. */
6154 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6156 break;
6158 case RID_GOTO:
6159 /* Create the goto-statement. */
6160 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6162 /* Issue a warning about this use of a GNU extension. */
6163 if (pedantic)
6164 pedwarn ("ISO C++ forbids computed gotos");
6165 /* Consume the '*' token. */
6166 cp_lexer_consume_token (parser->lexer);
6167 /* Parse the dependent expression. */
6168 finish_goto_stmt (cp_parser_expression (parser));
6170 else
6171 finish_goto_stmt (cp_parser_identifier (parser));
6172 /* Look for the final `;'. */
6173 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6174 break;
6176 default:
6177 cp_parser_error (parser, "expected jump-statement");
6178 break;
6181 return statement;
6184 /* Parse a declaration-statement.
6186 declaration-statement:
6187 block-declaration */
6189 static void
6190 cp_parser_declaration_statement (cp_parser* parser)
6192 /* Parse the block-declaration. */
6193 cp_parser_block_declaration (parser, /*statement_p=*/true);
6195 /* Finish off the statement. */
6196 finish_stmt ();
6199 /* Some dependent statements (like `if (cond) statement'), are
6200 implicitly in their own scope. In other words, if the statement is
6201 a single statement (as opposed to a compound-statement), it is
6202 none-the-less treated as if it were enclosed in braces. Any
6203 declarations appearing in the dependent statement are out of scope
6204 after control passes that point. This function parses a statement,
6205 but ensures that is in its own scope, even if it is not a
6206 compound-statement.
6208 Returns the new statement. */
6210 static tree
6211 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6213 tree statement;
6215 /* If the token is not a `{', then we must take special action. */
6216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6218 /* Create a compound-statement. */
6219 statement = begin_compound_stmt (/*has_no_scope=*/false);
6220 /* Parse the dependent-statement. */
6221 cp_parser_statement (parser, false);
6222 /* Finish the dummy compound-statement. */
6223 finish_compound_stmt (statement);
6225 /* Otherwise, we simply parse the statement directly. */
6226 else
6227 statement = cp_parser_compound_statement (parser, false);
6229 /* Return the statement. */
6230 return statement;
6233 /* For some dependent statements (like `while (cond) statement'), we
6234 have already created a scope. Therefore, even if the dependent
6235 statement is a compound-statement, we do not want to create another
6236 scope. */
6238 static void
6239 cp_parser_already_scoped_statement (cp_parser* parser)
6241 /* If the token is not a `{', then we must take special action. */
6242 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6244 tree statement;
6246 /* Create a compound-statement. */
6247 statement = begin_compound_stmt (/*has_no_scope=*/true);
6248 /* Parse the dependent-statement. */
6249 cp_parser_statement (parser, false);
6250 /* Finish the dummy compound-statement. */
6251 finish_compound_stmt (statement);
6253 /* Otherwise, we simply parse the statement directly. */
6254 else
6255 cp_parser_statement (parser, false);
6258 /* Declarations [gram.dcl.dcl] */
6260 /* Parse an optional declaration-sequence.
6262 declaration-seq:
6263 declaration
6264 declaration-seq declaration */
6266 static void
6267 cp_parser_declaration_seq_opt (cp_parser* parser)
6269 while (true)
6271 cp_token *token;
6273 token = cp_lexer_peek_token (parser->lexer);
6275 if (token->type == CPP_CLOSE_BRACE
6276 || token->type == CPP_EOF)
6277 break;
6279 if (token->type == CPP_SEMICOLON)
6281 /* A declaration consisting of a single semicolon is
6282 invalid. Allow it unless we're being pedantic. */
6283 if (pedantic && !in_system_header)
6284 pedwarn ("extra `;'");
6285 cp_lexer_consume_token (parser->lexer);
6286 continue;
6289 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6290 parser to enter or exit implicit `extern "C"' blocks. */
6291 while (pending_lang_change > 0)
6293 push_lang_context (lang_name_c);
6294 --pending_lang_change;
6296 while (pending_lang_change < 0)
6298 pop_lang_context ();
6299 ++pending_lang_change;
6302 /* Parse the declaration itself. */
6303 cp_parser_declaration (parser);
6307 /* Parse a declaration.
6309 declaration:
6310 block-declaration
6311 function-definition
6312 template-declaration
6313 explicit-instantiation
6314 explicit-specialization
6315 linkage-specification
6316 namespace-definition
6318 GNU extension:
6320 declaration:
6321 __extension__ declaration */
6323 static void
6324 cp_parser_declaration (cp_parser* parser)
6326 cp_token token1;
6327 cp_token token2;
6328 int saved_pedantic;
6330 /* Set this here since we can be called after
6331 pushing the linkage specification. */
6332 c_lex_string_translate = true;
6334 /* Check for the `__extension__' keyword. */
6335 if (cp_parser_extension_opt (parser, &saved_pedantic))
6337 /* Parse the qualified declaration. */
6338 cp_parser_declaration (parser);
6339 /* Restore the PEDANTIC flag. */
6340 pedantic = saved_pedantic;
6342 return;
6345 /* Try to figure out what kind of declaration is present. */
6346 token1 = *cp_lexer_peek_token (parser->lexer);
6348 /* Don't translate the CPP_STRING in extern "C". */
6349 if (token1.keyword == RID_EXTERN)
6350 c_lex_string_translate = false;
6352 if (token1.type != CPP_EOF)
6353 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6355 c_lex_string_translate = true;
6357 /* If the next token is `extern' and the following token is a string
6358 literal, then we have a linkage specification. */
6359 if (token1.keyword == RID_EXTERN
6360 && cp_parser_is_string_literal (&token2))
6361 cp_parser_linkage_specification (parser);
6362 /* If the next token is `template', then we have either a template
6363 declaration, an explicit instantiation, or an explicit
6364 specialization. */
6365 else if (token1.keyword == RID_TEMPLATE)
6367 /* `template <>' indicates a template specialization. */
6368 if (token2.type == CPP_LESS
6369 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6370 cp_parser_explicit_specialization (parser);
6371 /* `template <' indicates a template declaration. */
6372 else if (token2.type == CPP_LESS)
6373 cp_parser_template_declaration (parser, /*member_p=*/false);
6374 /* Anything else must be an explicit instantiation. */
6375 else
6376 cp_parser_explicit_instantiation (parser);
6378 /* If the next token is `export', then we have a template
6379 declaration. */
6380 else if (token1.keyword == RID_EXPORT)
6381 cp_parser_template_declaration (parser, /*member_p=*/false);
6382 /* If the next token is `extern', 'static' or 'inline' and the one
6383 after that is `template', we have a GNU extended explicit
6384 instantiation directive. */
6385 else if (cp_parser_allow_gnu_extensions_p (parser)
6386 && (token1.keyword == RID_EXTERN
6387 || token1.keyword == RID_STATIC
6388 || token1.keyword == RID_INLINE)
6389 && token2.keyword == RID_TEMPLATE)
6390 cp_parser_explicit_instantiation (parser);
6391 /* If the next token is `namespace', check for a named or unnamed
6392 namespace definition. */
6393 else if (token1.keyword == RID_NAMESPACE
6394 && (/* A named namespace definition. */
6395 (token2.type == CPP_NAME
6396 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6397 == CPP_OPEN_BRACE))
6398 /* An unnamed namespace definition. */
6399 || token2.type == CPP_OPEN_BRACE))
6400 cp_parser_namespace_definition (parser);
6401 /* We must have either a block declaration or a function
6402 definition. */
6403 else
6404 /* Try to parse a block-declaration, or a function-definition. */
6405 cp_parser_block_declaration (parser, /*statement_p=*/false);
6408 /* Parse a block-declaration.
6410 block-declaration:
6411 simple-declaration
6412 asm-definition
6413 namespace-alias-definition
6414 using-declaration
6415 using-directive
6417 GNU Extension:
6419 block-declaration:
6420 __extension__ block-declaration
6421 label-declaration
6423 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6424 part of a declaration-statement. */
6426 static void
6427 cp_parser_block_declaration (cp_parser *parser,
6428 bool statement_p)
6430 cp_token *token1;
6431 int saved_pedantic;
6433 /* Check for the `__extension__' keyword. */
6434 if (cp_parser_extension_opt (parser, &saved_pedantic))
6436 /* Parse the qualified declaration. */
6437 cp_parser_block_declaration (parser, statement_p);
6438 /* Restore the PEDANTIC flag. */
6439 pedantic = saved_pedantic;
6441 return;
6444 /* Peek at the next token to figure out which kind of declaration is
6445 present. */
6446 token1 = cp_lexer_peek_token (parser->lexer);
6448 /* If the next keyword is `asm', we have an asm-definition. */
6449 if (token1->keyword == RID_ASM)
6451 if (statement_p)
6452 cp_parser_commit_to_tentative_parse (parser);
6453 cp_parser_asm_definition (parser);
6455 /* If the next keyword is `namespace', we have a
6456 namespace-alias-definition. */
6457 else if (token1->keyword == RID_NAMESPACE)
6458 cp_parser_namespace_alias_definition (parser);
6459 /* If the next keyword is `using', we have either a
6460 using-declaration or a using-directive. */
6461 else if (token1->keyword == RID_USING)
6463 cp_token *token2;
6465 if (statement_p)
6466 cp_parser_commit_to_tentative_parse (parser);
6467 /* If the token after `using' is `namespace', then we have a
6468 using-directive. */
6469 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6470 if (token2->keyword == RID_NAMESPACE)
6471 cp_parser_using_directive (parser);
6472 /* Otherwise, it's a using-declaration. */
6473 else
6474 cp_parser_using_declaration (parser);
6476 /* If the next keyword is `__label__' we have a label declaration. */
6477 else if (token1->keyword == RID_LABEL)
6479 if (statement_p)
6480 cp_parser_commit_to_tentative_parse (parser);
6481 cp_parser_label_declaration (parser);
6483 /* Anything else must be a simple-declaration. */
6484 else
6485 cp_parser_simple_declaration (parser, !statement_p);
6488 /* Parse a simple-declaration.
6490 simple-declaration:
6491 decl-specifier-seq [opt] init-declarator-list [opt] ;
6493 init-declarator-list:
6494 init-declarator
6495 init-declarator-list , init-declarator
6497 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6498 function-definition as a simple-declaration. */
6500 static void
6501 cp_parser_simple_declaration (cp_parser* parser,
6502 bool function_definition_allowed_p)
6504 tree decl_specifiers;
6505 tree attributes;
6506 int declares_class_or_enum;
6507 bool saw_declarator;
6509 /* Defer access checks until we know what is being declared; the
6510 checks for names appearing in the decl-specifier-seq should be
6511 done as if we were in the scope of the thing being declared. */
6512 push_deferring_access_checks (dk_deferred);
6514 /* Parse the decl-specifier-seq. We have to keep track of whether
6515 or not the decl-specifier-seq declares a named class or
6516 enumeration type, since that is the only case in which the
6517 init-declarator-list is allowed to be empty.
6519 [dcl.dcl]
6521 In a simple-declaration, the optional init-declarator-list can be
6522 omitted only when declaring a class or enumeration, that is when
6523 the decl-specifier-seq contains either a class-specifier, an
6524 elaborated-type-specifier, or an enum-specifier. */
6525 decl_specifiers
6526 = cp_parser_decl_specifier_seq (parser,
6527 CP_PARSER_FLAGS_OPTIONAL,
6528 &attributes,
6529 &declares_class_or_enum);
6530 /* We no longer need to defer access checks. */
6531 stop_deferring_access_checks ();
6533 /* In a block scope, a valid declaration must always have a
6534 decl-specifier-seq. By not trying to parse declarators, we can
6535 resolve the declaration/expression ambiguity more quickly. */
6536 if (!function_definition_allowed_p && !decl_specifiers)
6538 cp_parser_error (parser, "expected declaration");
6539 goto done;
6542 /* If the next two tokens are both identifiers, the code is
6543 erroneous. The usual cause of this situation is code like:
6545 T t;
6547 where "T" should name a type -- but does not. */
6548 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6550 /* If parsing tentatively, we should commit; we really are
6551 looking at a declaration. */
6552 cp_parser_commit_to_tentative_parse (parser);
6553 /* Give up. */
6554 goto done;
6557 /* Keep going until we hit the `;' at the end of the simple
6558 declaration. */
6559 saw_declarator = false;
6560 while (cp_lexer_next_token_is_not (parser->lexer,
6561 CPP_SEMICOLON))
6563 cp_token *token;
6564 bool function_definition_p;
6565 tree decl;
6567 saw_declarator = true;
6568 /* Parse the init-declarator. */
6569 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6570 function_definition_allowed_p,
6571 /*member_p=*/false,
6572 declares_class_or_enum,
6573 &function_definition_p);
6574 /* If an error occurred while parsing tentatively, exit quickly.
6575 (That usually happens when in the body of a function; each
6576 statement is treated as a declaration-statement until proven
6577 otherwise.) */
6578 if (cp_parser_error_occurred (parser))
6579 goto done;
6580 /* Handle function definitions specially. */
6581 if (function_definition_p)
6583 /* If the next token is a `,', then we are probably
6584 processing something like:
6586 void f() {}, *p;
6588 which is erroneous. */
6589 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6590 error ("mixing declarations and function-definitions is forbidden");
6591 /* Otherwise, we're done with the list of declarators. */
6592 else
6594 pop_deferring_access_checks ();
6595 return;
6598 /* The next token should be either a `,' or a `;'. */
6599 token = cp_lexer_peek_token (parser->lexer);
6600 /* If it's a `,', there are more declarators to come. */
6601 if (token->type == CPP_COMMA)
6602 cp_lexer_consume_token (parser->lexer);
6603 /* If it's a `;', we are done. */
6604 else if (token->type == CPP_SEMICOLON)
6605 break;
6606 /* Anything else is an error. */
6607 else
6609 cp_parser_error (parser, "expected `,' or `;'");
6610 /* Skip tokens until we reach the end of the statement. */
6611 cp_parser_skip_to_end_of_statement (parser);
6612 /* If the next token is now a `;', consume it. */
6613 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6614 cp_lexer_consume_token (parser->lexer);
6615 goto done;
6617 /* After the first time around, a function-definition is not
6618 allowed -- even if it was OK at first. For example:
6620 int i, f() {}
6622 is not valid. */
6623 function_definition_allowed_p = false;
6626 /* Issue an error message if no declarators are present, and the
6627 decl-specifier-seq does not itself declare a class or
6628 enumeration. */
6629 if (!saw_declarator)
6631 if (cp_parser_declares_only_class_p (parser))
6632 shadow_tag (decl_specifiers);
6633 /* Perform any deferred access checks. */
6634 perform_deferred_access_checks ();
6637 /* Consume the `;'. */
6638 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6640 done:
6641 pop_deferring_access_checks ();
6644 /* Parse a decl-specifier-seq.
6646 decl-specifier-seq:
6647 decl-specifier-seq [opt] decl-specifier
6649 decl-specifier:
6650 storage-class-specifier
6651 type-specifier
6652 function-specifier
6653 friend
6654 typedef
6656 GNU Extension:
6658 decl-specifier-seq:
6659 decl-specifier-seq [opt] attributes
6661 Returns a TREE_LIST, giving the decl-specifiers in the order they
6662 appear in the source code. The TREE_VALUE of each node is the
6663 decl-specifier. For a keyword (such as `auto' or `friend'), the
6664 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6665 representation of a type-specifier, see cp_parser_type_specifier.
6667 If there are attributes, they will be stored in *ATTRIBUTES,
6668 represented as described above cp_parser_attributes.
6670 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6671 appears, and the entity that will be a friend is not going to be a
6672 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6673 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6674 friendship is granted might not be a class.
6676 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6677 flags:
6679 1: one of the decl-specifiers is an elaborated-type-specifier
6680 (i.e., a type declaration)
6681 2: one of the decl-specifiers is an enum-specifier or a
6682 class-specifier (i.e., a type definition)
6686 static tree
6687 cp_parser_decl_specifier_seq (cp_parser* parser,
6688 cp_parser_flags flags,
6689 tree* attributes,
6690 int* declares_class_or_enum)
6692 tree decl_specs = NULL_TREE;
6693 bool friend_p = false;
6694 bool constructor_possible_p = !parser->in_declarator_p;
6696 /* Assume no class or enumeration type is declared. */
6697 *declares_class_or_enum = 0;
6699 /* Assume there are no attributes. */
6700 *attributes = NULL_TREE;
6702 /* Keep reading specifiers until there are no more to read. */
6703 while (true)
6705 tree decl_spec = NULL_TREE;
6706 bool constructor_p;
6707 cp_token *token;
6709 /* Peek at the next token. */
6710 token = cp_lexer_peek_token (parser->lexer);
6711 /* Handle attributes. */
6712 if (token->keyword == RID_ATTRIBUTE)
6714 /* Parse the attributes. */
6715 decl_spec = cp_parser_attributes_opt (parser);
6716 /* Add them to the list. */
6717 *attributes = chainon (*attributes, decl_spec);
6718 continue;
6720 /* If the next token is an appropriate keyword, we can simply
6721 add it to the list. */
6722 switch (token->keyword)
6724 case RID_FRIEND:
6725 /* decl-specifier:
6726 friend */
6727 if (friend_p)
6728 error ("duplicate `friend'");
6729 else
6730 friend_p = true;
6731 /* The representation of the specifier is simply the
6732 appropriate TREE_IDENTIFIER node. */
6733 decl_spec = token->value;
6734 /* Consume the token. */
6735 cp_lexer_consume_token (parser->lexer);
6736 break;
6738 /* function-specifier:
6739 inline
6740 virtual
6741 explicit */
6742 case RID_INLINE:
6743 case RID_VIRTUAL:
6744 case RID_EXPLICIT:
6745 decl_spec = cp_parser_function_specifier_opt (parser);
6746 break;
6748 /* decl-specifier:
6749 typedef */
6750 case RID_TYPEDEF:
6751 /* The representation of the specifier is simply the
6752 appropriate TREE_IDENTIFIER node. */
6753 decl_spec = token->value;
6754 /* Consume the token. */
6755 cp_lexer_consume_token (parser->lexer);
6756 /* A constructor declarator cannot appear in a typedef. */
6757 constructor_possible_p = false;
6758 /* The "typedef" keyword can only occur in a declaration; we
6759 may as well commit at this point. */
6760 cp_parser_commit_to_tentative_parse (parser);
6761 break;
6763 /* storage-class-specifier:
6764 auto
6765 register
6766 static
6767 extern
6768 mutable
6770 GNU Extension:
6771 thread */
6772 case RID_AUTO:
6773 case RID_REGISTER:
6774 case RID_STATIC:
6775 case RID_EXTERN:
6776 case RID_MUTABLE:
6777 case RID_THREAD:
6778 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6779 break;
6781 default:
6782 break;
6785 /* Constructors are a special case. The `S' in `S()' is not a
6786 decl-specifier; it is the beginning of the declarator. */
6787 constructor_p = (!decl_spec
6788 && constructor_possible_p
6789 && cp_parser_constructor_declarator_p (parser,
6790 friend_p));
6792 /* If we don't have a DECL_SPEC yet, then we must be looking at
6793 a type-specifier. */
6794 if (!decl_spec && !constructor_p)
6796 int decl_spec_declares_class_or_enum;
6797 bool is_cv_qualifier;
6799 decl_spec
6800 = cp_parser_type_specifier (parser, flags,
6801 friend_p,
6802 /*is_declaration=*/true,
6803 &decl_spec_declares_class_or_enum,
6804 &is_cv_qualifier);
6806 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6808 /* If this type-specifier referenced a user-defined type
6809 (a typedef, class-name, etc.), then we can't allow any
6810 more such type-specifiers henceforth.
6812 [dcl.spec]
6814 The longest sequence of decl-specifiers that could
6815 possibly be a type name is taken as the
6816 decl-specifier-seq of a declaration. The sequence shall
6817 be self-consistent as described below.
6819 [dcl.type]
6821 As a general rule, at most one type-specifier is allowed
6822 in the complete decl-specifier-seq of a declaration. The
6823 only exceptions are the following:
6825 -- const or volatile can be combined with any other
6826 type-specifier.
6828 -- signed or unsigned can be combined with char, long,
6829 short, or int.
6831 -- ..
6833 Example:
6835 typedef char* Pc;
6836 void g (const int Pc);
6838 Here, Pc is *not* part of the decl-specifier seq; it's
6839 the declarator. Therefore, once we see a type-specifier
6840 (other than a cv-qualifier), we forbid any additional
6841 user-defined types. We *do* still allow things like `int
6842 int' to be considered a decl-specifier-seq, and issue the
6843 error message later. */
6844 if (decl_spec && !is_cv_qualifier)
6845 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6846 /* A constructor declarator cannot follow a type-specifier. */
6847 if (decl_spec)
6848 constructor_possible_p = false;
6851 /* If we still do not have a DECL_SPEC, then there are no more
6852 decl-specifiers. */
6853 if (!decl_spec)
6855 /* Issue an error message, unless the entire construct was
6856 optional. */
6857 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6859 cp_parser_error (parser, "expected decl specifier");
6860 return error_mark_node;
6863 break;
6866 /* Add the DECL_SPEC to the list of specifiers. */
6867 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6868 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6870 /* After we see one decl-specifier, further decl-specifiers are
6871 always optional. */
6872 flags |= CP_PARSER_FLAGS_OPTIONAL;
6875 /* Don't allow a friend specifier with a class definition. */
6876 if (friend_p && (*declares_class_or_enum & 2))
6877 error ("class definition may not be declared a friend");
6879 /* We have built up the DECL_SPECS in reverse order. Return them in
6880 the correct order. */
6881 return nreverse (decl_specs);
6884 /* Parse an (optional) storage-class-specifier.
6886 storage-class-specifier:
6887 auto
6888 register
6889 static
6890 extern
6891 mutable
6893 GNU Extension:
6895 storage-class-specifier:
6896 thread
6898 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6900 static tree
6901 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6903 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6905 case RID_AUTO:
6906 case RID_REGISTER:
6907 case RID_STATIC:
6908 case RID_EXTERN:
6909 case RID_MUTABLE:
6910 case RID_THREAD:
6911 /* Consume the token. */
6912 return cp_lexer_consume_token (parser->lexer)->value;
6914 default:
6915 return NULL_TREE;
6919 /* Parse an (optional) function-specifier.
6921 function-specifier:
6922 inline
6923 virtual
6924 explicit
6926 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6928 static tree
6929 cp_parser_function_specifier_opt (cp_parser* parser)
6931 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6933 case RID_INLINE:
6934 case RID_VIRTUAL:
6935 case RID_EXPLICIT:
6936 /* Consume the token. */
6937 return cp_lexer_consume_token (parser->lexer)->value;
6939 default:
6940 return NULL_TREE;
6944 /* Parse a linkage-specification.
6946 linkage-specification:
6947 extern string-literal { declaration-seq [opt] }
6948 extern string-literal declaration */
6950 static void
6951 cp_parser_linkage_specification (cp_parser* parser)
6953 cp_token *token;
6954 tree linkage;
6956 /* Look for the `extern' keyword. */
6957 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6959 /* Peek at the next token. */
6960 token = cp_lexer_peek_token (parser->lexer);
6961 /* If it's not a string-literal, then there's a problem. */
6962 if (!cp_parser_is_string_literal (token))
6964 cp_parser_error (parser, "expected language-name");
6965 return;
6967 /* Consume the token. */
6968 cp_lexer_consume_token (parser->lexer);
6970 /* Transform the literal into an identifier. If the literal is a
6971 wide-character string, or contains embedded NULs, then we can't
6972 handle it as the user wants. */
6973 if (token->type == CPP_WSTRING
6974 || (strlen (TREE_STRING_POINTER (token->value))
6975 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6977 cp_parser_error (parser, "invalid linkage-specification");
6978 /* Assume C++ linkage. */
6979 linkage = get_identifier ("c++");
6981 /* If it's a simple string constant, things are easier. */
6982 else
6983 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6985 /* We're now using the new linkage. */
6986 push_lang_context (linkage);
6988 /* If the next token is a `{', then we're using the first
6989 production. */
6990 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6992 /* Consume the `{' token. */
6993 cp_lexer_consume_token (parser->lexer);
6994 /* Parse the declarations. */
6995 cp_parser_declaration_seq_opt (parser);
6996 /* Look for the closing `}'. */
6997 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6999 /* Otherwise, there's just one declaration. */
7000 else
7002 bool saved_in_unbraced_linkage_specification_p;
7004 saved_in_unbraced_linkage_specification_p
7005 = parser->in_unbraced_linkage_specification_p;
7006 parser->in_unbraced_linkage_specification_p = true;
7007 have_extern_spec = true;
7008 cp_parser_declaration (parser);
7009 have_extern_spec = false;
7010 parser->in_unbraced_linkage_specification_p
7011 = saved_in_unbraced_linkage_specification_p;
7014 /* We're done with the linkage-specification. */
7015 pop_lang_context ();
7018 /* Special member functions [gram.special] */
7020 /* Parse a conversion-function-id.
7022 conversion-function-id:
7023 operator conversion-type-id
7025 Returns an IDENTIFIER_NODE representing the operator. */
7027 static tree
7028 cp_parser_conversion_function_id (cp_parser* parser)
7030 tree type;
7031 tree saved_scope;
7032 tree saved_qualifying_scope;
7033 tree saved_object_scope;
7034 bool pop_p = false;
7036 /* Look for the `operator' token. */
7037 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7038 return error_mark_node;
7039 /* When we parse the conversion-type-id, the current scope will be
7040 reset. However, we need that information in able to look up the
7041 conversion function later, so we save it here. */
7042 saved_scope = parser->scope;
7043 saved_qualifying_scope = parser->qualifying_scope;
7044 saved_object_scope = parser->object_scope;
7045 /* We must enter the scope of the class so that the names of
7046 entities declared within the class are available in the
7047 conversion-type-id. For example, consider:
7049 struct S {
7050 typedef int I;
7051 operator I();
7054 S::operator I() { ... }
7056 In order to see that `I' is a type-name in the definition, we
7057 must be in the scope of `S'. */
7058 if (saved_scope)
7059 pop_p = push_scope (saved_scope);
7060 /* Parse the conversion-type-id. */
7061 type = cp_parser_conversion_type_id (parser);
7062 /* Leave the scope of the class, if any. */
7063 if (pop_p)
7064 pop_scope (saved_scope);
7065 /* Restore the saved scope. */
7066 parser->scope = saved_scope;
7067 parser->qualifying_scope = saved_qualifying_scope;
7068 parser->object_scope = saved_object_scope;
7069 /* If the TYPE is invalid, indicate failure. */
7070 if (type == error_mark_node)
7071 return error_mark_node;
7072 return mangle_conv_op_name_for_type (type);
7075 /* Parse a conversion-type-id:
7077 conversion-type-id:
7078 type-specifier-seq conversion-declarator [opt]
7080 Returns the TYPE specified. */
7082 static tree
7083 cp_parser_conversion_type_id (cp_parser* parser)
7085 tree attributes;
7086 tree type_specifiers;
7087 tree declarator;
7089 /* Parse the attributes. */
7090 attributes = cp_parser_attributes_opt (parser);
7091 /* Parse the type-specifiers. */
7092 type_specifiers = cp_parser_type_specifier_seq (parser);
7093 /* If that didn't work, stop. */
7094 if (type_specifiers == error_mark_node)
7095 return error_mark_node;
7096 /* Parse the conversion-declarator. */
7097 declarator = cp_parser_conversion_declarator_opt (parser);
7099 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7100 /*initialized=*/0, &attributes);
7103 /* Parse an (optional) conversion-declarator.
7105 conversion-declarator:
7106 ptr-operator conversion-declarator [opt]
7108 Returns a representation of the declarator. See
7109 cp_parser_declarator for details. */
7111 static tree
7112 cp_parser_conversion_declarator_opt (cp_parser* parser)
7114 enum tree_code code;
7115 tree class_type;
7116 tree cv_qualifier_seq;
7118 /* We don't know if there's a ptr-operator next, or not. */
7119 cp_parser_parse_tentatively (parser);
7120 /* Try the ptr-operator. */
7121 code = cp_parser_ptr_operator (parser, &class_type,
7122 &cv_qualifier_seq);
7123 /* If it worked, look for more conversion-declarators. */
7124 if (cp_parser_parse_definitely (parser))
7126 tree declarator;
7128 /* Parse another optional declarator. */
7129 declarator = cp_parser_conversion_declarator_opt (parser);
7131 /* Create the representation of the declarator. */
7132 if (code == INDIRECT_REF)
7133 declarator = make_pointer_declarator (cv_qualifier_seq,
7134 declarator);
7135 else
7136 declarator = make_reference_declarator (cv_qualifier_seq,
7137 declarator);
7139 /* Handle the pointer-to-member case. */
7140 if (class_type)
7141 declarator = build_nt (SCOPE_REF, class_type, declarator);
7143 return declarator;
7146 return NULL_TREE;
7149 /* Parse an (optional) ctor-initializer.
7151 ctor-initializer:
7152 : mem-initializer-list
7154 Returns TRUE iff the ctor-initializer was actually present. */
7156 static bool
7157 cp_parser_ctor_initializer_opt (cp_parser* parser)
7159 /* If the next token is not a `:', then there is no
7160 ctor-initializer. */
7161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7163 /* Do default initialization of any bases and members. */
7164 if (DECL_CONSTRUCTOR_P (current_function_decl))
7165 finish_mem_initializers (NULL_TREE);
7167 return false;
7170 /* Consume the `:' token. */
7171 cp_lexer_consume_token (parser->lexer);
7172 /* And the mem-initializer-list. */
7173 cp_parser_mem_initializer_list (parser);
7175 return true;
7178 /* Parse a mem-initializer-list.
7180 mem-initializer-list:
7181 mem-initializer
7182 mem-initializer , mem-initializer-list */
7184 static void
7185 cp_parser_mem_initializer_list (cp_parser* parser)
7187 tree mem_initializer_list = NULL_TREE;
7189 /* Let the semantic analysis code know that we are starting the
7190 mem-initializer-list. */
7191 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7192 error ("only constructors take base initializers");
7194 /* Loop through the list. */
7195 while (true)
7197 tree mem_initializer;
7199 /* Parse the mem-initializer. */
7200 mem_initializer = cp_parser_mem_initializer (parser);
7201 /* Add it to the list, unless it was erroneous. */
7202 if (mem_initializer)
7204 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7205 mem_initializer_list = mem_initializer;
7207 /* If the next token is not a `,', we're done. */
7208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7209 break;
7210 /* Consume the `,' token. */
7211 cp_lexer_consume_token (parser->lexer);
7214 /* Perform semantic analysis. */
7215 if (DECL_CONSTRUCTOR_P (current_function_decl))
7216 finish_mem_initializers (mem_initializer_list);
7219 /* Parse a mem-initializer.
7221 mem-initializer:
7222 mem-initializer-id ( expression-list [opt] )
7224 GNU extension:
7226 mem-initializer:
7227 ( expression-list [opt] )
7229 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7230 class) or FIELD_DECL (for a non-static data member) to initialize;
7231 the TREE_VALUE is the expression-list. */
7233 static tree
7234 cp_parser_mem_initializer (cp_parser* parser)
7236 tree mem_initializer_id;
7237 tree expression_list;
7238 tree member;
7240 /* Find out what is being initialized. */
7241 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7243 pedwarn ("anachronistic old-style base class initializer");
7244 mem_initializer_id = NULL_TREE;
7246 else
7247 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7248 member = expand_member_init (mem_initializer_id);
7249 if (member && !DECL_P (member))
7250 in_base_initializer = 1;
7252 expression_list
7253 = cp_parser_parenthesized_expression_list (parser, false,
7254 /*non_constant_p=*/NULL);
7255 if (!expression_list)
7256 expression_list = void_type_node;
7258 in_base_initializer = 0;
7260 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7263 /* Parse a mem-initializer-id.
7265 mem-initializer-id:
7266 :: [opt] nested-name-specifier [opt] class-name
7267 identifier
7269 Returns a TYPE indicating the class to be initializer for the first
7270 production. Returns an IDENTIFIER_NODE indicating the data member
7271 to be initialized for the second production. */
7273 static tree
7274 cp_parser_mem_initializer_id (cp_parser* parser)
7276 bool global_scope_p;
7277 bool nested_name_specifier_p;
7278 tree id;
7280 /* Look for the optional `::' operator. */
7281 global_scope_p
7282 = (cp_parser_global_scope_opt (parser,
7283 /*current_scope_valid_p=*/false)
7284 != NULL_TREE);
7285 /* Look for the optional nested-name-specifier. The simplest way to
7286 implement:
7288 [temp.res]
7290 The keyword `typename' is not permitted in a base-specifier or
7291 mem-initializer; in these contexts a qualified name that
7292 depends on a template-parameter is implicitly assumed to be a
7293 type name.
7295 is to assume that we have seen the `typename' keyword at this
7296 point. */
7297 nested_name_specifier_p
7298 = (cp_parser_nested_name_specifier_opt (parser,
7299 /*typename_keyword_p=*/true,
7300 /*check_dependency_p=*/true,
7301 /*type_p=*/true,
7302 /*is_declaration=*/true)
7303 != NULL_TREE);
7304 /* If there is a `::' operator or a nested-name-specifier, then we
7305 are definitely looking for a class-name. */
7306 if (global_scope_p || nested_name_specifier_p)
7307 return cp_parser_class_name (parser,
7308 /*typename_keyword_p=*/true,
7309 /*template_keyword_p=*/false,
7310 /*type_p=*/false,
7311 /*check_dependency_p=*/true,
7312 /*class_head_p=*/false,
7313 /*is_declaration=*/true);
7314 /* Otherwise, we could also be looking for an ordinary identifier. */
7315 cp_parser_parse_tentatively (parser);
7316 /* Try a class-name. */
7317 id = cp_parser_class_name (parser,
7318 /*typename_keyword_p=*/true,
7319 /*template_keyword_p=*/false,
7320 /*type_p=*/false,
7321 /*check_dependency_p=*/true,
7322 /*class_head_p=*/false,
7323 /*is_declaration=*/true);
7324 /* If we found one, we're done. */
7325 if (cp_parser_parse_definitely (parser))
7326 return id;
7327 /* Otherwise, look for an ordinary identifier. */
7328 return cp_parser_identifier (parser);
7331 /* Overloading [gram.over] */
7333 /* Parse an operator-function-id.
7335 operator-function-id:
7336 operator operator
7338 Returns an IDENTIFIER_NODE for the operator which is a
7339 human-readable spelling of the identifier, e.g., `operator +'. */
7341 static tree
7342 cp_parser_operator_function_id (cp_parser* parser)
7344 /* Look for the `operator' keyword. */
7345 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7346 return error_mark_node;
7347 /* And then the name of the operator itself. */
7348 return cp_parser_operator (parser);
7351 /* Parse an operator.
7353 operator:
7354 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7355 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7356 || ++ -- , ->* -> () []
7358 GNU Extensions:
7360 operator:
7361 <? >? <?= >?=
7363 Returns an IDENTIFIER_NODE for the operator which is a
7364 human-readable spelling of the identifier, e.g., `operator +'. */
7366 static tree
7367 cp_parser_operator (cp_parser* parser)
7369 tree id = NULL_TREE;
7370 cp_token *token;
7372 /* Peek at the next token. */
7373 token = cp_lexer_peek_token (parser->lexer);
7374 /* Figure out which operator we have. */
7375 switch (token->type)
7377 case CPP_KEYWORD:
7379 enum tree_code op;
7381 /* The keyword should be either `new' or `delete'. */
7382 if (token->keyword == RID_NEW)
7383 op = NEW_EXPR;
7384 else if (token->keyword == RID_DELETE)
7385 op = DELETE_EXPR;
7386 else
7387 break;
7389 /* Consume the `new' or `delete' token. */
7390 cp_lexer_consume_token (parser->lexer);
7392 /* Peek at the next token. */
7393 token = cp_lexer_peek_token (parser->lexer);
7394 /* If it's a `[' token then this is the array variant of the
7395 operator. */
7396 if (token->type == CPP_OPEN_SQUARE)
7398 /* Consume the `[' token. */
7399 cp_lexer_consume_token (parser->lexer);
7400 /* Look for the `]' token. */
7401 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7402 id = ansi_opname (op == NEW_EXPR
7403 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7405 /* Otherwise, we have the non-array variant. */
7406 else
7407 id = ansi_opname (op);
7409 return id;
7412 case CPP_PLUS:
7413 id = ansi_opname (PLUS_EXPR);
7414 break;
7416 case CPP_MINUS:
7417 id = ansi_opname (MINUS_EXPR);
7418 break;
7420 case CPP_MULT:
7421 id = ansi_opname (MULT_EXPR);
7422 break;
7424 case CPP_DIV:
7425 id = ansi_opname (TRUNC_DIV_EXPR);
7426 break;
7428 case CPP_MOD:
7429 id = ansi_opname (TRUNC_MOD_EXPR);
7430 break;
7432 case CPP_XOR:
7433 id = ansi_opname (BIT_XOR_EXPR);
7434 break;
7436 case CPP_AND:
7437 id = ansi_opname (BIT_AND_EXPR);
7438 break;
7440 case CPP_OR:
7441 id = ansi_opname (BIT_IOR_EXPR);
7442 break;
7444 case CPP_COMPL:
7445 id = ansi_opname (BIT_NOT_EXPR);
7446 break;
7448 case CPP_NOT:
7449 id = ansi_opname (TRUTH_NOT_EXPR);
7450 break;
7452 case CPP_EQ:
7453 id = ansi_assopname (NOP_EXPR);
7454 break;
7456 case CPP_LESS:
7457 id = ansi_opname (LT_EXPR);
7458 break;
7460 case CPP_GREATER:
7461 id = ansi_opname (GT_EXPR);
7462 break;
7464 case CPP_PLUS_EQ:
7465 id = ansi_assopname (PLUS_EXPR);
7466 break;
7468 case CPP_MINUS_EQ:
7469 id = ansi_assopname (MINUS_EXPR);
7470 break;
7472 case CPP_MULT_EQ:
7473 id = ansi_assopname (MULT_EXPR);
7474 break;
7476 case CPP_DIV_EQ:
7477 id = ansi_assopname (TRUNC_DIV_EXPR);
7478 break;
7480 case CPP_MOD_EQ:
7481 id = ansi_assopname (TRUNC_MOD_EXPR);
7482 break;
7484 case CPP_XOR_EQ:
7485 id = ansi_assopname (BIT_XOR_EXPR);
7486 break;
7488 case CPP_AND_EQ:
7489 id = ansi_assopname (BIT_AND_EXPR);
7490 break;
7492 case CPP_OR_EQ:
7493 id = ansi_assopname (BIT_IOR_EXPR);
7494 break;
7496 case CPP_LSHIFT:
7497 id = ansi_opname (LSHIFT_EXPR);
7498 break;
7500 case CPP_RSHIFT:
7501 id = ansi_opname (RSHIFT_EXPR);
7502 break;
7504 case CPP_LSHIFT_EQ:
7505 id = ansi_assopname (LSHIFT_EXPR);
7506 break;
7508 case CPP_RSHIFT_EQ:
7509 id = ansi_assopname (RSHIFT_EXPR);
7510 break;
7512 case CPP_EQ_EQ:
7513 id = ansi_opname (EQ_EXPR);
7514 break;
7516 case CPP_NOT_EQ:
7517 id = ansi_opname (NE_EXPR);
7518 break;
7520 case CPP_LESS_EQ:
7521 id = ansi_opname (LE_EXPR);
7522 break;
7524 case CPP_GREATER_EQ:
7525 id = ansi_opname (GE_EXPR);
7526 break;
7528 case CPP_AND_AND:
7529 id = ansi_opname (TRUTH_ANDIF_EXPR);
7530 break;
7532 case CPP_OR_OR:
7533 id = ansi_opname (TRUTH_ORIF_EXPR);
7534 break;
7536 case CPP_PLUS_PLUS:
7537 id = ansi_opname (POSTINCREMENT_EXPR);
7538 break;
7540 case CPP_MINUS_MINUS:
7541 id = ansi_opname (PREDECREMENT_EXPR);
7542 break;
7544 case CPP_COMMA:
7545 id = ansi_opname (COMPOUND_EXPR);
7546 break;
7548 case CPP_DEREF_STAR:
7549 id = ansi_opname (MEMBER_REF);
7550 break;
7552 case CPP_DEREF:
7553 id = ansi_opname (COMPONENT_REF);
7554 break;
7556 case CPP_OPEN_PAREN:
7557 /* Consume the `('. */
7558 cp_lexer_consume_token (parser->lexer);
7559 /* Look for the matching `)'. */
7560 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7561 return ansi_opname (CALL_EXPR);
7563 case CPP_OPEN_SQUARE:
7564 /* Consume the `['. */
7565 cp_lexer_consume_token (parser->lexer);
7566 /* Look for the matching `]'. */
7567 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7568 return ansi_opname (ARRAY_REF);
7570 /* Extensions. */
7571 case CPP_MIN:
7572 id = ansi_opname (MIN_EXPR);
7573 break;
7575 case CPP_MAX:
7576 id = ansi_opname (MAX_EXPR);
7577 break;
7579 case CPP_MIN_EQ:
7580 id = ansi_assopname (MIN_EXPR);
7581 break;
7583 case CPP_MAX_EQ:
7584 id = ansi_assopname (MAX_EXPR);
7585 break;
7587 default:
7588 /* Anything else is an error. */
7589 break;
7592 /* If we have selected an identifier, we need to consume the
7593 operator token. */
7594 if (id)
7595 cp_lexer_consume_token (parser->lexer);
7596 /* Otherwise, no valid operator name was present. */
7597 else
7599 cp_parser_error (parser, "expected operator");
7600 id = error_mark_node;
7603 return id;
7606 /* Parse a template-declaration.
7608 template-declaration:
7609 export [opt] template < template-parameter-list > declaration
7611 If MEMBER_P is TRUE, this template-declaration occurs within a
7612 class-specifier.
7614 The grammar rule given by the standard isn't correct. What
7615 is really meant is:
7617 template-declaration:
7618 export [opt] template-parameter-list-seq
7619 decl-specifier-seq [opt] init-declarator [opt] ;
7620 export [opt] template-parameter-list-seq
7621 function-definition
7623 template-parameter-list-seq:
7624 template-parameter-list-seq [opt]
7625 template < template-parameter-list > */
7627 static void
7628 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7630 /* Check for `export'. */
7631 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7633 /* Consume the `export' token. */
7634 cp_lexer_consume_token (parser->lexer);
7635 /* Warn that we do not support `export'. */
7636 warning ("keyword `export' not implemented, and will be ignored");
7639 cp_parser_template_declaration_after_export (parser, member_p);
7642 /* Parse a template-parameter-list.
7644 template-parameter-list:
7645 template-parameter
7646 template-parameter-list , template-parameter
7648 Returns a TREE_LIST. Each node represents a template parameter.
7649 The nodes are connected via their TREE_CHAINs. */
7651 static tree
7652 cp_parser_template_parameter_list (cp_parser* parser)
7654 tree parameter_list = NULL_TREE;
7656 while (true)
7658 tree parameter;
7659 cp_token *token;
7661 /* Parse the template-parameter. */
7662 parameter = cp_parser_template_parameter (parser);
7663 /* Add it to the list. */
7664 parameter_list = process_template_parm (parameter_list,
7665 parameter);
7667 /* Peek at the next token. */
7668 token = cp_lexer_peek_token (parser->lexer);
7669 /* If it's not a `,', we're done. */
7670 if (token->type != CPP_COMMA)
7671 break;
7672 /* Otherwise, consume the `,' token. */
7673 cp_lexer_consume_token (parser->lexer);
7676 return parameter_list;
7679 /* Parse a template-parameter.
7681 template-parameter:
7682 type-parameter
7683 parameter-declaration
7685 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7686 TREE_PURPOSE is the default value, if any. */
7688 static tree
7689 cp_parser_template_parameter (cp_parser* parser)
7691 cp_token *token;
7693 /* Peek at the next token. */
7694 token = cp_lexer_peek_token (parser->lexer);
7695 /* If it is `class' or `template', we have a type-parameter. */
7696 if (token->keyword == RID_TEMPLATE)
7697 return cp_parser_type_parameter (parser);
7698 /* If it is `class' or `typename' we do not know yet whether it is a
7699 type parameter or a non-type parameter. Consider:
7701 template <typename T, typename T::X X> ...
7705 template <class C, class D*> ...
7707 Here, the first parameter is a type parameter, and the second is
7708 a non-type parameter. We can tell by looking at the token after
7709 the identifier -- if it is a `,', `=', or `>' then we have a type
7710 parameter. */
7711 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7713 /* Peek at the token after `class' or `typename'. */
7714 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7715 /* If it's an identifier, skip it. */
7716 if (token->type == CPP_NAME)
7717 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7718 /* Now, see if the token looks like the end of a template
7719 parameter. */
7720 if (token->type == CPP_COMMA
7721 || token->type == CPP_EQ
7722 || token->type == CPP_GREATER)
7723 return cp_parser_type_parameter (parser);
7726 /* Otherwise, it is a non-type parameter.
7728 [temp.param]
7730 When parsing a default template-argument for a non-type
7731 template-parameter, the first non-nested `>' is taken as the end
7732 of the template parameter-list rather than a greater-than
7733 operator. */
7734 return
7735 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7736 /*parenthesized_p=*/NULL);
7739 /* Parse a type-parameter.
7741 type-parameter:
7742 class identifier [opt]
7743 class identifier [opt] = type-id
7744 typename identifier [opt]
7745 typename identifier [opt] = type-id
7746 template < template-parameter-list > class identifier [opt]
7747 template < template-parameter-list > class identifier [opt]
7748 = id-expression
7750 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7751 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7752 the declaration of the parameter. */
7754 static tree
7755 cp_parser_type_parameter (cp_parser* parser)
7757 cp_token *token;
7758 tree parameter;
7760 /* Look for a keyword to tell us what kind of parameter this is. */
7761 token = cp_parser_require (parser, CPP_KEYWORD,
7762 "`class', `typename', or `template'");
7763 if (!token)
7764 return error_mark_node;
7766 switch (token->keyword)
7768 case RID_CLASS:
7769 case RID_TYPENAME:
7771 tree identifier;
7772 tree default_argument;
7774 /* If the next token is an identifier, then it names the
7775 parameter. */
7776 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7777 identifier = cp_parser_identifier (parser);
7778 else
7779 identifier = NULL_TREE;
7781 /* Create the parameter. */
7782 parameter = finish_template_type_parm (class_type_node, identifier);
7784 /* If the next token is an `=', we have a default argument. */
7785 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7787 /* Consume the `=' token. */
7788 cp_lexer_consume_token (parser->lexer);
7789 /* Parse the default-argument. */
7790 default_argument = cp_parser_type_id (parser);
7792 else
7793 default_argument = NULL_TREE;
7795 /* Create the combined representation of the parameter and the
7796 default argument. */
7797 parameter = build_tree_list (default_argument, parameter);
7799 break;
7801 case RID_TEMPLATE:
7803 tree parameter_list;
7804 tree identifier;
7805 tree default_argument;
7807 /* Look for the `<'. */
7808 cp_parser_require (parser, CPP_LESS, "`<'");
7809 /* Parse the template-parameter-list. */
7810 begin_template_parm_list ();
7811 parameter_list
7812 = cp_parser_template_parameter_list (parser);
7813 parameter_list = end_template_parm_list (parameter_list);
7814 /* Look for the `>'. */
7815 cp_parser_require (parser, CPP_GREATER, "`>'");
7816 /* Look for the `class' keyword. */
7817 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7818 /* If the next token is an `=', then there is a
7819 default-argument. If the next token is a `>', we are at
7820 the end of the parameter-list. If the next token is a `,',
7821 then we are at the end of this parameter. */
7822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7823 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7824 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7825 identifier = cp_parser_identifier (parser);
7826 else
7827 identifier = NULL_TREE;
7828 /* Create the template parameter. */
7829 parameter = finish_template_template_parm (class_type_node,
7830 identifier);
7832 /* If the next token is an `=', then there is a
7833 default-argument. */
7834 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7836 bool is_template;
7838 /* Consume the `='. */
7839 cp_lexer_consume_token (parser->lexer);
7840 /* Parse the id-expression. */
7841 default_argument
7842 = cp_parser_id_expression (parser,
7843 /*template_keyword_p=*/false,
7844 /*check_dependency_p=*/true,
7845 /*template_p=*/&is_template,
7846 /*declarator_p=*/false);
7847 if (TREE_CODE (default_argument) == TYPE_DECL)
7848 /* If the id-expression was a template-id that refers to
7849 a template-class, we already have the declaration here,
7850 so no further lookup is needed. */
7852 else
7853 /* Look up the name. */
7854 default_argument
7855 = cp_parser_lookup_name (parser, default_argument,
7856 /*is_type=*/false,
7857 /*is_template=*/is_template,
7858 /*is_namespace=*/false,
7859 /*check_dependency=*/true);
7860 /* See if the default argument is valid. */
7861 default_argument
7862 = check_template_template_default_arg (default_argument);
7864 else
7865 default_argument = NULL_TREE;
7867 /* Create the combined representation of the parameter and the
7868 default argument. */
7869 parameter = build_tree_list (default_argument, parameter);
7871 break;
7873 default:
7874 /* Anything else is an error. */
7875 cp_parser_error (parser,
7876 "expected `class', `typename', or `template'");
7877 parameter = error_mark_node;
7880 return parameter;
7883 /* Parse a template-id.
7885 template-id:
7886 template-name < template-argument-list [opt] >
7888 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7889 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7890 returned. Otherwise, if the template-name names a function, or set
7891 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7892 names a class, returns a TYPE_DECL for the specialization.
7894 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7895 uninstantiated templates. */
7897 static tree
7898 cp_parser_template_id (cp_parser *parser,
7899 bool template_keyword_p,
7900 bool check_dependency_p,
7901 bool is_declaration)
7903 tree template;
7904 tree arguments;
7905 tree template_id;
7906 ptrdiff_t start_of_id;
7907 tree access_check = NULL_TREE;
7908 cp_token *next_token, *next_token_2;
7909 bool is_identifier;
7911 /* If the next token corresponds to a template-id, there is no need
7912 to reparse it. */
7913 next_token = cp_lexer_peek_token (parser->lexer);
7914 if (next_token->type == CPP_TEMPLATE_ID)
7916 tree value;
7917 tree check;
7919 /* Get the stored value. */
7920 value = cp_lexer_consume_token (parser->lexer)->value;
7921 /* Perform any access checks that were deferred. */
7922 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7923 perform_or_defer_access_check (TREE_PURPOSE (check),
7924 TREE_VALUE (check));
7925 /* Return the stored value. */
7926 return TREE_VALUE (value);
7929 /* Avoid performing name lookup if there is no possibility of
7930 finding a template-id. */
7931 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7932 || (next_token->type == CPP_NAME
7933 && !cp_parser_nth_token_starts_template_argument_list_p
7934 (parser, 2)))
7936 cp_parser_error (parser, "expected template-id");
7937 return error_mark_node;
7940 /* Remember where the template-id starts. */
7941 if (cp_parser_parsing_tentatively (parser)
7942 && !cp_parser_committed_to_tentative_parse (parser))
7944 next_token = cp_lexer_peek_token (parser->lexer);
7945 start_of_id = cp_lexer_token_difference (parser->lexer,
7946 parser->lexer->first_token,
7947 next_token);
7949 else
7950 start_of_id = -1;
7952 push_deferring_access_checks (dk_deferred);
7954 /* Parse the template-name. */
7955 is_identifier = false;
7956 template = cp_parser_template_name (parser, template_keyword_p,
7957 check_dependency_p,
7958 is_declaration,
7959 &is_identifier);
7960 if (template == error_mark_node || is_identifier)
7962 pop_deferring_access_checks ();
7963 return template;
7966 /* If we find the sequence `[:' after a template-name, it's probably
7967 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7968 parse correctly the argument list. */
7969 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7970 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7971 if (next_token->type == CPP_OPEN_SQUARE
7972 && next_token->flags & DIGRAPH
7973 && next_token_2->type == CPP_COLON
7974 && !(next_token_2->flags & PREV_WHITE))
7976 cp_parser_parse_tentatively (parser);
7977 /* Change `:' into `::'. */
7978 next_token_2->type = CPP_SCOPE;
7979 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7980 CPP_LESS. */
7981 cp_lexer_consume_token (parser->lexer);
7982 /* Parse the arguments. */
7983 arguments = cp_parser_enclosed_template_argument_list (parser);
7984 if (!cp_parser_parse_definitely (parser))
7986 /* If we couldn't parse an argument list, then we revert our changes
7987 and return simply an error. Maybe this is not a template-id
7988 after all. */
7989 next_token_2->type = CPP_COLON;
7990 cp_parser_error (parser, "expected `<'");
7991 pop_deferring_access_checks ();
7992 return error_mark_node;
7994 /* Otherwise, emit an error about the invalid digraph, but continue
7995 parsing because we got our argument list. */
7996 pedwarn ("`<::' cannot begin a template-argument list");
7997 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7998 "between `<' and `::'");
7999 if (!flag_permissive)
8001 static bool hint;
8002 if (!hint)
8004 inform ("(if you use `-fpermissive' G++ will accept your code)");
8005 hint = true;
8009 else
8011 /* Look for the `<' that starts the template-argument-list. */
8012 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8014 pop_deferring_access_checks ();
8015 return error_mark_node;
8017 /* Parse the arguments. */
8018 arguments = cp_parser_enclosed_template_argument_list (parser);
8021 /* Build a representation of the specialization. */
8022 if (TREE_CODE (template) == IDENTIFIER_NODE)
8023 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8024 else if (DECL_CLASS_TEMPLATE_P (template)
8025 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8026 template_id
8027 = finish_template_type (template, arguments,
8028 cp_lexer_next_token_is (parser->lexer,
8029 CPP_SCOPE));
8030 else
8032 /* If it's not a class-template or a template-template, it should be
8033 a function-template. */
8034 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8035 || TREE_CODE (template) == OVERLOAD
8036 || BASELINK_P (template)),
8037 20010716);
8039 template_id = lookup_template_function (template, arguments);
8042 /* Retrieve any deferred checks. Do not pop this access checks yet
8043 so the memory will not be reclaimed during token replacing below. */
8044 access_check = get_deferred_access_checks ();
8046 /* If parsing tentatively, replace the sequence of tokens that makes
8047 up the template-id with a CPP_TEMPLATE_ID token. That way,
8048 should we re-parse the token stream, we will not have to repeat
8049 the effort required to do the parse, nor will we issue duplicate
8050 error messages about problems during instantiation of the
8051 template. */
8052 if (start_of_id >= 0)
8054 cp_token *token;
8056 /* Find the token that corresponds to the start of the
8057 template-id. */
8058 token = cp_lexer_advance_token (parser->lexer,
8059 parser->lexer->first_token,
8060 start_of_id);
8062 /* Reset the contents of the START_OF_ID token. */
8063 token->type = CPP_TEMPLATE_ID;
8064 token->value = build_tree_list (access_check, template_id);
8065 token->keyword = RID_MAX;
8066 /* Purge all subsequent tokens. */
8067 cp_lexer_purge_tokens_after (parser->lexer, token);
8070 pop_deferring_access_checks ();
8071 return template_id;
8074 /* Parse a template-name.
8076 template-name:
8077 identifier
8079 The standard should actually say:
8081 template-name:
8082 identifier
8083 operator-function-id
8085 A defect report has been filed about this issue.
8087 A conversion-function-id cannot be a template name because they cannot
8088 be part of a template-id. In fact, looking at this code:
8090 a.operator K<int>()
8092 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8093 It is impossible to call a templated conversion-function-id with an
8094 explicit argument list, since the only allowed template parameter is
8095 the type to which it is converting.
8097 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8098 `template' keyword, in a construction like:
8100 T::template f<3>()
8102 In that case `f' is taken to be a template-name, even though there
8103 is no way of knowing for sure.
8105 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8106 name refers to a set of overloaded functions, at least one of which
8107 is a template, or an IDENTIFIER_NODE with the name of the template,
8108 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8109 names are looked up inside uninstantiated templates. */
8111 static tree
8112 cp_parser_template_name (cp_parser* parser,
8113 bool template_keyword_p,
8114 bool check_dependency_p,
8115 bool is_declaration,
8116 bool *is_identifier)
8118 tree identifier;
8119 tree decl;
8120 tree fns;
8122 /* If the next token is `operator', then we have either an
8123 operator-function-id or a conversion-function-id. */
8124 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8126 /* We don't know whether we're looking at an
8127 operator-function-id or a conversion-function-id. */
8128 cp_parser_parse_tentatively (parser);
8129 /* Try an operator-function-id. */
8130 identifier = cp_parser_operator_function_id (parser);
8131 /* If that didn't work, try a conversion-function-id. */
8132 if (!cp_parser_parse_definitely (parser))
8134 cp_parser_error (parser, "expected template-name");
8135 return error_mark_node;
8138 /* Look for the identifier. */
8139 else
8140 identifier = cp_parser_identifier (parser);
8142 /* If we didn't find an identifier, we don't have a template-id. */
8143 if (identifier == error_mark_node)
8144 return error_mark_node;
8146 /* If the name immediately followed the `template' keyword, then it
8147 is a template-name. However, if the next token is not `<', then
8148 we do not treat it as a template-name, since it is not being used
8149 as part of a template-id. This enables us to handle constructs
8150 like:
8152 template <typename T> struct S { S(); };
8153 template <typename T> S<T>::S();
8155 correctly. We would treat `S' as a template -- if it were `S<T>'
8156 -- but we do not if there is no `<'. */
8158 if (processing_template_decl
8159 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8161 /* In a declaration, in a dependent context, we pretend that the
8162 "template" keyword was present in order to improve error
8163 recovery. For example, given:
8165 template <typename T> void f(T::X<int>);
8167 we want to treat "X<int>" as a template-id. */
8168 if (is_declaration
8169 && !template_keyword_p
8170 && parser->scope && TYPE_P (parser->scope)
8171 && dependent_type_p (parser->scope))
8173 ptrdiff_t start;
8174 cp_token* token;
8175 /* Explain what went wrong. */
8176 error ("non-template `%D' used as template", identifier);
8177 error ("(use `%T::template %D' to indicate that it is a template)",
8178 parser->scope, identifier);
8179 /* If parsing tentatively, find the location of the "<"
8180 token. */
8181 if (cp_parser_parsing_tentatively (parser)
8182 && !cp_parser_committed_to_tentative_parse (parser))
8184 cp_parser_simulate_error (parser);
8185 token = cp_lexer_peek_token (parser->lexer);
8186 token = cp_lexer_prev_token (parser->lexer, token);
8187 start = cp_lexer_token_difference (parser->lexer,
8188 parser->lexer->first_token,
8189 token);
8191 else
8192 start = -1;
8193 /* Parse the template arguments so that we can issue error
8194 messages about them. */
8195 cp_lexer_consume_token (parser->lexer);
8196 cp_parser_enclosed_template_argument_list (parser);
8197 /* Skip tokens until we find a good place from which to
8198 continue parsing. */
8199 cp_parser_skip_to_closing_parenthesis (parser,
8200 /*recovering=*/true,
8201 /*or_comma=*/true,
8202 /*consume_paren=*/false);
8203 /* If parsing tentatively, permanently remove the
8204 template argument list. That will prevent duplicate
8205 error messages from being issued about the missing
8206 "template" keyword. */
8207 if (start >= 0)
8209 token = cp_lexer_advance_token (parser->lexer,
8210 parser->lexer->first_token,
8211 start);
8212 cp_lexer_purge_tokens_after (parser->lexer, token);
8214 if (is_identifier)
8215 *is_identifier = true;
8216 return identifier;
8219 /* If the "template" keyword is present, then there is generally
8220 no point in doing name-lookup, so we just return IDENTIFIER.
8221 But, if the qualifying scope is non-dependent then we can
8222 (and must) do name-lookup normally. */
8223 if (template_keyword_p
8224 && (!parser->scope
8225 || (TYPE_P (parser->scope)
8226 && dependent_type_p (parser->scope))))
8227 return identifier;
8230 /* Look up the name. */
8231 decl = cp_parser_lookup_name (parser, identifier,
8232 /*is_type=*/false,
8233 /*is_template=*/false,
8234 /*is_namespace=*/false,
8235 check_dependency_p);
8236 decl = maybe_get_template_decl_from_type_decl (decl);
8238 /* If DECL is a template, then the name was a template-name. */
8239 if (TREE_CODE (decl) == TEMPLATE_DECL)
8241 else
8243 /* The standard does not explicitly indicate whether a name that
8244 names a set of overloaded declarations, some of which are
8245 templates, is a template-name. However, such a name should
8246 be a template-name; otherwise, there is no way to form a
8247 template-id for the overloaded templates. */
8248 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8249 if (TREE_CODE (fns) == OVERLOAD)
8251 tree fn;
8253 for (fn = fns; fn; fn = OVL_NEXT (fn))
8254 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8255 break;
8257 else
8259 /* Otherwise, the name does not name a template. */
8260 cp_parser_error (parser, "expected template-name");
8261 return error_mark_node;
8265 /* If DECL is dependent, and refers to a function, then just return
8266 its name; we will look it up again during template instantiation. */
8267 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8269 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8270 if (TYPE_P (scope) && dependent_type_p (scope))
8271 return identifier;
8274 return decl;
8277 /* Parse a template-argument-list.
8279 template-argument-list:
8280 template-argument
8281 template-argument-list , template-argument
8283 Returns a TREE_VEC containing the arguments. */
8285 static tree
8286 cp_parser_template_argument_list (cp_parser* parser)
8288 tree fixed_args[10];
8289 unsigned n_args = 0;
8290 unsigned alloced = 10;
8291 tree *arg_ary = fixed_args;
8292 tree vec;
8293 bool saved_in_template_argument_list_p;
8295 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8296 parser->in_template_argument_list_p = true;
8299 tree argument;
8301 if (n_args)
8302 /* Consume the comma. */
8303 cp_lexer_consume_token (parser->lexer);
8305 /* Parse the template-argument. */
8306 argument = cp_parser_template_argument (parser);
8307 if (n_args == alloced)
8309 alloced *= 2;
8311 if (arg_ary == fixed_args)
8313 arg_ary = xmalloc (sizeof (tree) * alloced);
8314 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8316 else
8317 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8319 arg_ary[n_args++] = argument;
8321 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8323 vec = make_tree_vec (n_args);
8325 while (n_args--)
8326 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8328 if (arg_ary != fixed_args)
8329 free (arg_ary);
8330 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8331 return vec;
8334 /* Parse a template-argument.
8336 template-argument:
8337 assignment-expression
8338 type-id
8339 id-expression
8341 The representation is that of an assignment-expression, type-id, or
8342 id-expression -- except that the qualified id-expression is
8343 evaluated, so that the value returned is either a DECL or an
8344 OVERLOAD.
8346 Although the standard says "assignment-expression", it forbids
8347 throw-expressions or assignments in the template argument.
8348 Therefore, we use "conditional-expression" instead. */
8350 static tree
8351 cp_parser_template_argument (cp_parser* parser)
8353 tree argument;
8354 bool template_p;
8355 bool address_p;
8356 bool maybe_type_id = false;
8357 cp_token *token;
8358 cp_id_kind idk;
8359 tree qualifying_class;
8361 /* There's really no way to know what we're looking at, so we just
8362 try each alternative in order.
8364 [temp.arg]
8366 In a template-argument, an ambiguity between a type-id and an
8367 expression is resolved to a type-id, regardless of the form of
8368 the corresponding template-parameter.
8370 Therefore, we try a type-id first. */
8371 cp_parser_parse_tentatively (parser);
8372 argument = cp_parser_type_id (parser);
8373 /* If there was no error parsing the type-id but the next token is a '>>',
8374 we probably found a typo for '> >'. But there are type-id which are
8375 also valid expressions. For instance:
8377 struct X { int operator >> (int); };
8378 template <int V> struct Foo {};
8379 Foo<X () >> 5> r;
8381 Here 'X()' is a valid type-id of a function type, but the user just
8382 wanted to write the expression "X() >> 5". Thus, we remember that we
8383 found a valid type-id, but we still try to parse the argument as an
8384 expression to see what happens. */
8385 if (!cp_parser_error_occurred (parser)
8386 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8388 maybe_type_id = true;
8389 cp_parser_abort_tentative_parse (parser);
8391 else
8393 /* If the next token isn't a `,' or a `>', then this argument wasn't
8394 really finished. This means that the argument is not a valid
8395 type-id. */
8396 if (!cp_parser_next_token_ends_template_argument_p (parser))
8397 cp_parser_error (parser, "expected template-argument");
8398 /* If that worked, we're done. */
8399 if (cp_parser_parse_definitely (parser))
8400 return argument;
8402 /* We're still not sure what the argument will be. */
8403 cp_parser_parse_tentatively (parser);
8404 /* Try a template. */
8405 argument = cp_parser_id_expression (parser,
8406 /*template_keyword_p=*/false,
8407 /*check_dependency_p=*/true,
8408 &template_p,
8409 /*declarator_p=*/false);
8410 /* If the next token isn't a `,' or a `>', then this argument wasn't
8411 really finished. */
8412 if (!cp_parser_next_token_ends_template_argument_p (parser))
8413 cp_parser_error (parser, "expected template-argument");
8414 if (!cp_parser_error_occurred (parser))
8416 /* Figure out what is being referred to. */
8417 argument = cp_parser_lookup_name (parser, argument,
8418 /*is_type=*/false,
8419 /*is_template=*/template_p,
8420 /*is_namespace=*/false,
8421 /*check_dependency=*/true);
8422 if (TREE_CODE (argument) != TEMPLATE_DECL
8423 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8424 cp_parser_error (parser, "expected template-name");
8426 if (cp_parser_parse_definitely (parser))
8427 return argument;
8428 /* It must be a non-type argument. There permitted cases are given
8429 in [temp.arg.nontype]:
8431 -- an integral constant-expression of integral or enumeration
8432 type; or
8434 -- the name of a non-type template-parameter; or
8436 -- the name of an object or function with external linkage...
8438 -- the address of an object or function with external linkage...
8440 -- a pointer to member... */
8441 /* Look for a non-type template parameter. */
8442 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8444 cp_parser_parse_tentatively (parser);
8445 argument = cp_parser_primary_expression (parser,
8446 &idk,
8447 &qualifying_class);
8448 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8449 || !cp_parser_next_token_ends_template_argument_p (parser))
8450 cp_parser_simulate_error (parser);
8451 if (cp_parser_parse_definitely (parser))
8452 return argument;
8454 /* If the next token is "&", the argument must be the address of an
8455 object or function with external linkage. */
8456 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8457 if (address_p)
8458 cp_lexer_consume_token (parser->lexer);
8459 /* See if we might have an id-expression. */
8460 token = cp_lexer_peek_token (parser->lexer);
8461 if (token->type == CPP_NAME
8462 || token->keyword == RID_OPERATOR
8463 || token->type == CPP_SCOPE
8464 || token->type == CPP_TEMPLATE_ID
8465 || token->type == CPP_NESTED_NAME_SPECIFIER)
8467 cp_parser_parse_tentatively (parser);
8468 argument = cp_parser_primary_expression (parser,
8469 &idk,
8470 &qualifying_class);
8471 if (cp_parser_error_occurred (parser)
8472 || !cp_parser_next_token_ends_template_argument_p (parser))
8473 cp_parser_abort_tentative_parse (parser);
8474 else
8476 if (qualifying_class)
8477 argument = finish_qualified_id_expr (qualifying_class,
8478 argument,
8479 /*done=*/true,
8480 address_p);
8481 if (TREE_CODE (argument) == VAR_DECL)
8483 /* A variable without external linkage might still be a
8484 valid constant-expression, so no error is issued here
8485 if the external-linkage check fails. */
8486 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8487 cp_parser_simulate_error (parser);
8489 else if (is_overloaded_fn (argument))
8490 /* All overloaded functions are allowed; if the external
8491 linkage test does not pass, an error will be issued
8492 later. */
8494 else if (address_p
8495 && (TREE_CODE (argument) == OFFSET_REF
8496 || TREE_CODE (argument) == SCOPE_REF))
8497 /* A pointer-to-member. */
8499 else
8500 cp_parser_simulate_error (parser);
8502 if (cp_parser_parse_definitely (parser))
8504 if (address_p)
8505 argument = build_x_unary_op (ADDR_EXPR, argument);
8506 return argument;
8510 /* If the argument started with "&", there are no other valid
8511 alternatives at this point. */
8512 if (address_p)
8514 cp_parser_error (parser, "invalid non-type template argument");
8515 return error_mark_node;
8517 /* If the argument wasn't successfully parsed as a type-id followed
8518 by '>>', the argument can only be a constant expression now.
8519 Otherwise, we try parsing the constant-expression tentatively,
8520 because the argument could really be a type-id. */
8521 if (maybe_type_id)
8522 cp_parser_parse_tentatively (parser);
8523 argument = cp_parser_constant_expression (parser,
8524 /*allow_non_constant_p=*/false,
8525 /*non_constant_p=*/NULL);
8526 argument = fold_non_dependent_expr (argument);
8527 if (!maybe_type_id)
8528 return argument;
8529 if (!cp_parser_next_token_ends_template_argument_p (parser))
8530 cp_parser_error (parser, "expected template-argument");
8531 if (cp_parser_parse_definitely (parser))
8532 return argument;
8533 /* We did our best to parse the argument as a non type-id, but that
8534 was the only alternative that matched (albeit with a '>' after
8535 it). We can assume it's just a typo from the user, and a
8536 diagnostic will then be issued. */
8537 return cp_parser_type_id (parser);
8540 /* Parse an explicit-instantiation.
8542 explicit-instantiation:
8543 template declaration
8545 Although the standard says `declaration', what it really means is:
8547 explicit-instantiation:
8548 template decl-specifier-seq [opt] declarator [opt] ;
8550 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8551 supposed to be allowed. A defect report has been filed about this
8552 issue.
8554 GNU Extension:
8556 explicit-instantiation:
8557 storage-class-specifier template
8558 decl-specifier-seq [opt] declarator [opt] ;
8559 function-specifier template
8560 decl-specifier-seq [opt] declarator [opt] ; */
8562 static void
8563 cp_parser_explicit_instantiation (cp_parser* parser)
8565 int declares_class_or_enum;
8566 tree decl_specifiers;
8567 tree attributes;
8568 tree extension_specifier = NULL_TREE;
8570 /* Look for an (optional) storage-class-specifier or
8571 function-specifier. */
8572 if (cp_parser_allow_gnu_extensions_p (parser))
8574 extension_specifier
8575 = cp_parser_storage_class_specifier_opt (parser);
8576 if (!extension_specifier)
8577 extension_specifier = cp_parser_function_specifier_opt (parser);
8580 /* Look for the `template' keyword. */
8581 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8582 /* Let the front end know that we are processing an explicit
8583 instantiation. */
8584 begin_explicit_instantiation ();
8585 /* [temp.explicit] says that we are supposed to ignore access
8586 control while processing explicit instantiation directives. */
8587 push_deferring_access_checks (dk_no_check);
8588 /* Parse a decl-specifier-seq. */
8589 decl_specifiers
8590 = cp_parser_decl_specifier_seq (parser,
8591 CP_PARSER_FLAGS_OPTIONAL,
8592 &attributes,
8593 &declares_class_or_enum);
8594 /* If there was exactly one decl-specifier, and it declared a class,
8595 and there's no declarator, then we have an explicit type
8596 instantiation. */
8597 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8599 tree type;
8601 type = check_tag_decl (decl_specifiers);
8602 /* Turn access control back on for names used during
8603 template instantiation. */
8604 pop_deferring_access_checks ();
8605 if (type)
8606 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8608 else
8610 tree declarator;
8611 tree decl;
8613 /* Parse the declarator. */
8614 declarator
8615 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8616 /*ctor_dtor_or_conv_p=*/NULL,
8617 /*parenthesized_p=*/NULL);
8618 cp_parser_check_for_definition_in_return_type (declarator,
8619 declares_class_or_enum);
8620 if (declarator != error_mark_node)
8622 decl = grokdeclarator (declarator, decl_specifiers,
8623 NORMAL, 0, NULL);
8624 /* Turn access control back on for names used during
8625 template instantiation. */
8626 pop_deferring_access_checks ();
8627 /* Do the explicit instantiation. */
8628 do_decl_instantiation (decl, extension_specifier);
8630 else
8632 pop_deferring_access_checks ();
8633 /* Skip the body of the explicit instantiation. */
8634 cp_parser_skip_to_end_of_statement (parser);
8637 /* We're done with the instantiation. */
8638 end_explicit_instantiation ();
8640 cp_parser_consume_semicolon_at_end_of_statement (parser);
8643 /* Parse an explicit-specialization.
8645 explicit-specialization:
8646 template < > declaration
8648 Although the standard says `declaration', what it really means is:
8650 explicit-specialization:
8651 template <> decl-specifier [opt] init-declarator [opt] ;
8652 template <> function-definition
8653 template <> explicit-specialization
8654 template <> template-declaration */
8656 static void
8657 cp_parser_explicit_specialization (cp_parser* parser)
8659 /* Look for the `template' keyword. */
8660 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8661 /* Look for the `<'. */
8662 cp_parser_require (parser, CPP_LESS, "`<'");
8663 /* Look for the `>'. */
8664 cp_parser_require (parser, CPP_GREATER, "`>'");
8665 /* We have processed another parameter list. */
8666 ++parser->num_template_parameter_lists;
8667 /* Let the front end know that we are beginning a specialization. */
8668 begin_specialization ();
8670 /* If the next keyword is `template', we need to figure out whether
8671 or not we're looking a template-declaration. */
8672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8674 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8675 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8676 cp_parser_template_declaration_after_export (parser,
8677 /*member_p=*/false);
8678 else
8679 cp_parser_explicit_specialization (parser);
8681 else
8682 /* Parse the dependent declaration. */
8683 cp_parser_single_declaration (parser,
8684 /*member_p=*/false,
8685 /*friend_p=*/NULL);
8687 /* We're done with the specialization. */
8688 end_specialization ();
8689 /* We're done with this parameter list. */
8690 --parser->num_template_parameter_lists;
8693 /* Parse a type-specifier.
8695 type-specifier:
8696 simple-type-specifier
8697 class-specifier
8698 enum-specifier
8699 elaborated-type-specifier
8700 cv-qualifier
8702 GNU Extension:
8704 type-specifier:
8705 __complex__
8707 Returns a representation of the type-specifier. If the
8708 type-specifier is a keyword (like `int' or `const', or
8709 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8710 For a class-specifier, enum-specifier, or elaborated-type-specifier
8711 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8713 If IS_FRIEND is TRUE then this type-specifier is being declared a
8714 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8715 appearing in a decl-specifier-seq.
8717 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8718 class-specifier, enum-specifier, or elaborated-type-specifier, then
8719 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8720 if a type is declared; 2 if it is defined. Otherwise, it is set to
8721 zero.
8723 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8724 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8725 is set to FALSE. */
8727 static tree
8728 cp_parser_type_specifier (cp_parser* parser,
8729 cp_parser_flags flags,
8730 bool is_friend,
8731 bool is_declaration,
8732 int* declares_class_or_enum,
8733 bool* is_cv_qualifier)
8735 tree type_spec = NULL_TREE;
8736 cp_token *token;
8737 enum rid keyword;
8739 /* Assume this type-specifier does not declare a new type. */
8740 if (declares_class_or_enum)
8741 *declares_class_or_enum = 0;
8742 /* And that it does not specify a cv-qualifier. */
8743 if (is_cv_qualifier)
8744 *is_cv_qualifier = false;
8745 /* Peek at the next token. */
8746 token = cp_lexer_peek_token (parser->lexer);
8748 /* If we're looking at a keyword, we can use that to guide the
8749 production we choose. */
8750 keyword = token->keyword;
8751 switch (keyword)
8753 /* Any of these indicate either a class-specifier, or an
8754 elaborated-type-specifier. */
8755 case RID_CLASS:
8756 case RID_STRUCT:
8757 case RID_UNION:
8758 case RID_ENUM:
8759 /* Parse tentatively so that we can back up if we don't find a
8760 class-specifier or enum-specifier. */
8761 cp_parser_parse_tentatively (parser);
8762 /* Look for the class-specifier or enum-specifier. */
8763 if (keyword == RID_ENUM)
8764 type_spec = cp_parser_enum_specifier (parser);
8765 else
8766 type_spec = cp_parser_class_specifier (parser);
8768 /* If that worked, we're done. */
8769 if (cp_parser_parse_definitely (parser))
8771 if (declares_class_or_enum)
8772 *declares_class_or_enum = 2;
8773 return type_spec;
8776 /* Fall through. */
8778 case RID_TYPENAME:
8779 /* Look for an elaborated-type-specifier. */
8780 type_spec = cp_parser_elaborated_type_specifier (parser,
8781 is_friend,
8782 is_declaration);
8783 /* We're declaring a class or enum -- unless we're using
8784 `typename'. */
8785 if (declares_class_or_enum && keyword != RID_TYPENAME)
8786 *declares_class_or_enum = 1;
8787 return type_spec;
8789 case RID_CONST:
8790 case RID_VOLATILE:
8791 case RID_RESTRICT:
8792 type_spec = cp_parser_cv_qualifier_opt (parser);
8793 /* Even though we call a routine that looks for an optional
8794 qualifier, we know that there should be one. */
8795 my_friendly_assert (type_spec != NULL, 20000328);
8796 /* This type-specifier was a cv-qualified. */
8797 if (is_cv_qualifier)
8798 *is_cv_qualifier = true;
8800 return type_spec;
8802 case RID_COMPLEX:
8803 /* The `__complex__' keyword is a GNU extension. */
8804 return cp_lexer_consume_token (parser->lexer)->value;
8806 default:
8807 break;
8810 /* If we do not already have a type-specifier, assume we are looking
8811 at a simple-type-specifier. */
8812 type_spec = cp_parser_simple_type_specifier (parser, flags,
8813 /*identifier_p=*/true);
8815 /* If we didn't find a type-specifier, and a type-specifier was not
8816 optional in this context, issue an error message. */
8817 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8819 cp_parser_error (parser, "expected type specifier");
8820 return error_mark_node;
8823 return type_spec;
8826 /* Parse a simple-type-specifier.
8828 simple-type-specifier:
8829 :: [opt] nested-name-specifier [opt] type-name
8830 :: [opt] nested-name-specifier template template-id
8831 char
8832 wchar_t
8833 bool
8834 short
8836 long
8837 signed
8838 unsigned
8839 float
8840 double
8841 void
8843 GNU Extension:
8845 simple-type-specifier:
8846 __typeof__ unary-expression
8847 __typeof__ ( type-id )
8849 For the various keywords, the value returned is simply the
8850 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8851 For the first two productions, and if IDENTIFIER_P is false, the
8852 value returned is the indicated TYPE_DECL. */
8854 static tree
8855 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8856 bool identifier_p)
8858 tree type = NULL_TREE;
8859 cp_token *token;
8861 /* Peek at the next token. */
8862 token = cp_lexer_peek_token (parser->lexer);
8864 /* If we're looking at a keyword, things are easy. */
8865 switch (token->keyword)
8867 case RID_CHAR:
8868 type = char_type_node;
8869 break;
8870 case RID_WCHAR:
8871 type = wchar_type_node;
8872 break;
8873 case RID_BOOL:
8874 type = boolean_type_node;
8875 break;
8876 case RID_SHORT:
8877 type = short_integer_type_node;
8878 break;
8879 case RID_INT:
8880 type = integer_type_node;
8881 break;
8882 case RID_LONG:
8883 type = long_integer_type_node;
8884 break;
8885 case RID_SIGNED:
8886 type = integer_type_node;
8887 break;
8888 case RID_UNSIGNED:
8889 type = unsigned_type_node;
8890 break;
8891 case RID_FLOAT:
8892 type = float_type_node;
8893 break;
8894 case RID_DOUBLE:
8895 type = double_type_node;
8896 break;
8897 case RID_VOID:
8898 type = void_type_node;
8899 break;
8901 case RID_TYPEOF:
8903 tree operand;
8905 /* Consume the `typeof' token. */
8906 cp_lexer_consume_token (parser->lexer);
8907 /* Parse the operand to `typeof'. */
8908 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8909 /* If it is not already a TYPE, take its type. */
8910 if (!TYPE_P (operand))
8911 operand = finish_typeof (operand);
8913 return operand;
8916 default:
8917 break;
8920 /* If the type-specifier was for a built-in type, we're done. */
8921 if (type)
8923 tree id;
8925 /* Consume the token. */
8926 id = cp_lexer_consume_token (parser->lexer)->value;
8928 /* There is no valid C++ program where a non-template type is
8929 followed by a "<". That usually indicates that the user thought
8930 that the type was a template. */
8931 cp_parser_check_for_invalid_template_id (parser, type);
8933 return identifier_p ? id : TYPE_NAME (type);
8936 /* The type-specifier must be a user-defined type. */
8937 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8939 /* Don't gobble tokens or issue error messages if this is an
8940 optional type-specifier. */
8941 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8942 cp_parser_parse_tentatively (parser);
8944 /* Look for the optional `::' operator. */
8945 cp_parser_global_scope_opt (parser,
8946 /*current_scope_valid_p=*/false);
8947 /* Look for the nested-name specifier. */
8948 cp_parser_nested_name_specifier_opt (parser,
8949 /*typename_keyword_p=*/false,
8950 /*check_dependency_p=*/true,
8951 /*type_p=*/false,
8952 /*is_declaration=*/false);
8953 /* If we have seen a nested-name-specifier, and the next token
8954 is `template', then we are using the template-id production. */
8955 if (parser->scope
8956 && cp_parser_optional_template_keyword (parser))
8958 /* Look for the template-id. */
8959 type = cp_parser_template_id (parser,
8960 /*template_keyword_p=*/true,
8961 /*check_dependency_p=*/true,
8962 /*is_declaration=*/false);
8963 /* If the template-id did not name a type, we are out of
8964 luck. */
8965 if (TREE_CODE (type) != TYPE_DECL)
8967 cp_parser_error (parser, "expected template-id for type");
8968 type = NULL_TREE;
8971 /* Otherwise, look for a type-name. */
8972 else
8973 type = cp_parser_type_name (parser);
8974 /* If it didn't work out, we don't have a TYPE. */
8975 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8976 && !cp_parser_parse_definitely (parser))
8977 type = NULL_TREE;
8980 /* If we didn't get a type-name, issue an error message. */
8981 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8983 cp_parser_error (parser, "expected type-name");
8984 return error_mark_node;
8987 /* There is no valid C++ program where a non-template type is
8988 followed by a "<". That usually indicates that the user thought
8989 that the type was a template. */
8990 if (type && type != error_mark_node)
8991 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8993 return type;
8996 /* Parse a type-name.
8998 type-name:
8999 class-name
9000 enum-name
9001 typedef-name
9003 enum-name:
9004 identifier
9006 typedef-name:
9007 identifier
9009 Returns a TYPE_DECL for the the type. */
9011 static tree
9012 cp_parser_type_name (cp_parser* parser)
9014 tree type_decl;
9015 tree identifier;
9017 /* We can't know yet whether it is a class-name or not. */
9018 cp_parser_parse_tentatively (parser);
9019 /* Try a class-name. */
9020 type_decl = cp_parser_class_name (parser,
9021 /*typename_keyword_p=*/false,
9022 /*template_keyword_p=*/false,
9023 /*type_p=*/false,
9024 /*check_dependency_p=*/true,
9025 /*class_head_p=*/false,
9026 /*is_declaration=*/false);
9027 /* If it's not a class-name, keep looking. */
9028 if (!cp_parser_parse_definitely (parser))
9030 /* It must be a typedef-name or an enum-name. */
9031 identifier = cp_parser_identifier (parser);
9032 if (identifier == error_mark_node)
9033 return error_mark_node;
9035 /* Look up the type-name. */
9036 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9037 /* Issue an error if we did not find a type-name. */
9038 if (TREE_CODE (type_decl) != TYPE_DECL)
9040 if (!cp_parser_simulate_error (parser))
9041 cp_parser_name_lookup_error (parser, identifier, type_decl,
9042 "is not a type");
9043 type_decl = error_mark_node;
9045 /* Remember that the name was used in the definition of the
9046 current class so that we can check later to see if the
9047 meaning would have been different after the class was
9048 entirely defined. */
9049 else if (type_decl != error_mark_node
9050 && !parser->scope)
9051 maybe_note_name_used_in_class (identifier, type_decl);
9054 return type_decl;
9058 /* Parse an elaborated-type-specifier. Note that the grammar given
9059 here incorporates the resolution to DR68.
9061 elaborated-type-specifier:
9062 class-key :: [opt] nested-name-specifier [opt] identifier
9063 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9064 enum :: [opt] nested-name-specifier [opt] identifier
9065 typename :: [opt] nested-name-specifier identifier
9066 typename :: [opt] nested-name-specifier template [opt]
9067 template-id
9069 GNU extension:
9071 elaborated-type-specifier:
9072 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9073 class-key attributes :: [opt] nested-name-specifier [opt]
9074 template [opt] template-id
9075 enum attributes :: [opt] nested-name-specifier [opt] identifier
9077 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9078 declared `friend'. If IS_DECLARATION is TRUE, then this
9079 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9080 something is being declared.
9082 Returns the TYPE specified. */
9084 static tree
9085 cp_parser_elaborated_type_specifier (cp_parser* parser,
9086 bool is_friend,
9087 bool is_declaration)
9089 enum tag_types tag_type;
9090 tree identifier;
9091 tree type = NULL_TREE;
9092 tree attributes = NULL_TREE;
9094 /* See if we're looking at the `enum' keyword. */
9095 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9097 /* Consume the `enum' token. */
9098 cp_lexer_consume_token (parser->lexer);
9099 /* Remember that it's an enumeration type. */
9100 tag_type = enum_type;
9101 /* Parse the attributes. */
9102 attributes = cp_parser_attributes_opt (parser);
9104 /* Or, it might be `typename'. */
9105 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9106 RID_TYPENAME))
9108 /* Consume the `typename' token. */
9109 cp_lexer_consume_token (parser->lexer);
9110 /* Remember that it's a `typename' type. */
9111 tag_type = typename_type;
9112 /* The `typename' keyword is only allowed in templates. */
9113 if (!processing_template_decl)
9114 pedwarn ("using `typename' outside of template");
9116 /* Otherwise it must be a class-key. */
9117 else
9119 tag_type = cp_parser_class_key (parser);
9120 if (tag_type == none_type)
9121 return error_mark_node;
9122 /* Parse the attributes. */
9123 attributes = cp_parser_attributes_opt (parser);
9126 /* Look for the `::' operator. */
9127 cp_parser_global_scope_opt (parser,
9128 /*current_scope_valid_p=*/false);
9129 /* Look for the nested-name-specifier. */
9130 if (tag_type == typename_type)
9132 if (cp_parser_nested_name_specifier (parser,
9133 /*typename_keyword_p=*/true,
9134 /*check_dependency_p=*/true,
9135 /*type_p=*/true,
9136 is_declaration)
9137 == error_mark_node)
9138 return error_mark_node;
9140 else
9141 /* Even though `typename' is not present, the proposed resolution
9142 to Core Issue 180 says that in `class A<T>::B', `B' should be
9143 considered a type-name, even if `A<T>' is dependent. */
9144 cp_parser_nested_name_specifier_opt (parser,
9145 /*typename_keyword_p=*/true,
9146 /*check_dependency_p=*/true,
9147 /*type_p=*/true,
9148 is_declaration);
9149 /* For everything but enumeration types, consider a template-id. */
9150 if (tag_type != enum_type)
9152 bool template_p = false;
9153 tree decl;
9155 /* Allow the `template' keyword. */
9156 template_p = cp_parser_optional_template_keyword (parser);
9157 /* If we didn't see `template', we don't know if there's a
9158 template-id or not. */
9159 if (!template_p)
9160 cp_parser_parse_tentatively (parser);
9161 /* Parse the template-id. */
9162 decl = cp_parser_template_id (parser, template_p,
9163 /*check_dependency_p=*/true,
9164 is_declaration);
9165 /* If we didn't find a template-id, look for an ordinary
9166 identifier. */
9167 if (!template_p && !cp_parser_parse_definitely (parser))
9169 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9170 in effect, then we must assume that, upon instantiation, the
9171 template will correspond to a class. */
9172 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9173 && tag_type == typename_type)
9174 type = make_typename_type (parser->scope, decl,
9175 /*complain=*/1);
9176 else
9177 type = TREE_TYPE (decl);
9180 /* For an enumeration type, consider only a plain identifier. */
9181 if (!type)
9183 identifier = cp_parser_identifier (parser);
9185 if (identifier == error_mark_node)
9187 parser->scope = NULL_TREE;
9188 return error_mark_node;
9191 /* For a `typename', we needn't call xref_tag. */
9192 if (tag_type == typename_type)
9193 return cp_parser_make_typename_type (parser, parser->scope,
9194 identifier);
9195 /* Look up a qualified name in the usual way. */
9196 if (parser->scope)
9198 tree decl;
9200 /* In an elaborated-type-specifier, names are assumed to name
9201 types, so we set IS_TYPE to TRUE when calling
9202 cp_parser_lookup_name. */
9203 decl = cp_parser_lookup_name (parser, identifier,
9204 /*is_type=*/true,
9205 /*is_template=*/false,
9206 /*is_namespace=*/false,
9207 /*check_dependency=*/true);
9209 /* If we are parsing friend declaration, DECL may be a
9210 TEMPLATE_DECL tree node here. However, we need to check
9211 whether this TEMPLATE_DECL results in valid code. Consider
9212 the following example:
9214 namespace N {
9215 template <class T> class C {};
9217 class X {
9218 template <class T> friend class N::C; // #1, valid code
9220 template <class T> class Y {
9221 friend class N::C; // #2, invalid code
9224 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9225 name lookup of `N::C'. We see that friend declaration must
9226 be template for the code to be valid. Note that
9227 processing_template_decl does not work here since it is
9228 always 1 for the above two cases. */
9230 decl = (cp_parser_maybe_treat_template_as_class
9231 (decl, /*tag_name_p=*/is_friend
9232 && parser->num_template_parameter_lists));
9234 if (TREE_CODE (decl) != TYPE_DECL)
9236 error ("expected type-name");
9237 return error_mark_node;
9240 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9241 check_elaborated_type_specifier
9242 (tag_type, decl,
9243 (parser->num_template_parameter_lists
9244 || DECL_SELF_REFERENCE_P (decl)));
9246 type = TREE_TYPE (decl);
9248 else
9250 /* An elaborated-type-specifier sometimes introduces a new type and
9251 sometimes names an existing type. Normally, the rule is that it
9252 introduces a new type only if there is not an existing type of
9253 the same name already in scope. For example, given:
9255 struct S {};
9256 void f() { struct S s; }
9258 the `struct S' in the body of `f' is the same `struct S' as in
9259 the global scope; the existing definition is used. However, if
9260 there were no global declaration, this would introduce a new
9261 local class named `S'.
9263 An exception to this rule applies to the following code:
9265 namespace N { struct S; }
9267 Here, the elaborated-type-specifier names a new type
9268 unconditionally; even if there is already an `S' in the
9269 containing scope this declaration names a new type.
9270 This exception only applies if the elaborated-type-specifier
9271 forms the complete declaration:
9273 [class.name]
9275 A declaration consisting solely of `class-key identifier ;' is
9276 either a redeclaration of the name in the current scope or a
9277 forward declaration of the identifier as a class name. It
9278 introduces the name into the current scope.
9280 We are in this situation precisely when the next token is a `;'.
9282 An exception to the exception is that a `friend' declaration does
9283 *not* name a new type; i.e., given:
9285 struct S { friend struct T; };
9287 `T' is not a new type in the scope of `S'.
9289 Also, `new struct S' or `sizeof (struct S)' never results in the
9290 definition of a new type; a new type can only be declared in a
9291 declaration context. */
9293 /* Warn about attributes. They are ignored. */
9294 if (attributes)
9295 warning ("type attributes are honored only at type definition");
9297 type = xref_tag (tag_type, identifier,
9298 (is_friend
9299 || !is_declaration
9300 || cp_lexer_next_token_is_not (parser->lexer,
9301 CPP_SEMICOLON)),
9302 parser->num_template_parameter_lists);
9305 if (tag_type != enum_type)
9306 cp_parser_check_class_key (tag_type, type);
9308 /* A "<" cannot follow an elaborated type specifier. If that
9309 happens, the user was probably trying to form a template-id. */
9310 cp_parser_check_for_invalid_template_id (parser, type);
9312 return type;
9315 /* Parse an enum-specifier.
9317 enum-specifier:
9318 enum identifier [opt] { enumerator-list [opt] }
9320 Returns an ENUM_TYPE representing the enumeration. */
9322 static tree
9323 cp_parser_enum_specifier (cp_parser* parser)
9325 cp_token *token;
9326 tree identifier = NULL_TREE;
9327 tree type;
9329 /* Look for the `enum' keyword. */
9330 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9331 return error_mark_node;
9332 /* Peek at the next token. */
9333 token = cp_lexer_peek_token (parser->lexer);
9335 /* See if it is an identifier. */
9336 if (token->type == CPP_NAME)
9337 identifier = cp_parser_identifier (parser);
9339 /* Look for the `{'. */
9340 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9341 return error_mark_node;
9343 /* At this point, we're going ahead with the enum-specifier, even
9344 if some other problem occurs. */
9345 cp_parser_commit_to_tentative_parse (parser);
9347 /* Issue an error message if type-definitions are forbidden here. */
9348 cp_parser_check_type_definition (parser);
9350 /* Create the new type. */
9351 type = start_enum (identifier ? identifier : make_anon_name ());
9353 /* Peek at the next token. */
9354 token = cp_lexer_peek_token (parser->lexer);
9355 /* If it's not a `}', then there are some enumerators. */
9356 if (token->type != CPP_CLOSE_BRACE)
9357 cp_parser_enumerator_list (parser, type);
9358 /* Look for the `}'. */
9359 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9361 /* Finish up the enumeration. */
9362 finish_enum (type);
9364 return type;
9367 /* Parse an enumerator-list. The enumerators all have the indicated
9368 TYPE.
9370 enumerator-list:
9371 enumerator-definition
9372 enumerator-list , enumerator-definition */
9374 static void
9375 cp_parser_enumerator_list (cp_parser* parser, tree type)
9377 while (true)
9379 cp_token *token;
9381 /* Parse an enumerator-definition. */
9382 cp_parser_enumerator_definition (parser, type);
9383 /* Peek at the next token. */
9384 token = cp_lexer_peek_token (parser->lexer);
9385 /* If it's not a `,', then we've reached the end of the
9386 list. */
9387 if (token->type != CPP_COMMA)
9388 break;
9389 /* Otherwise, consume the `,' and keep going. */
9390 cp_lexer_consume_token (parser->lexer);
9391 /* If the next token is a `}', there is a trailing comma. */
9392 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9394 if (pedantic && !in_system_header)
9395 pedwarn ("comma at end of enumerator list");
9396 break;
9401 /* Parse an enumerator-definition. The enumerator has the indicated
9402 TYPE.
9404 enumerator-definition:
9405 enumerator
9406 enumerator = constant-expression
9408 enumerator:
9409 identifier */
9411 static void
9412 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9414 cp_token *token;
9415 tree identifier;
9416 tree value;
9418 /* Look for the identifier. */
9419 identifier = cp_parser_identifier (parser);
9420 if (identifier == error_mark_node)
9421 return;
9423 /* Peek at the next token. */
9424 token = cp_lexer_peek_token (parser->lexer);
9425 /* If it's an `=', then there's an explicit value. */
9426 if (token->type == CPP_EQ)
9428 /* Consume the `=' token. */
9429 cp_lexer_consume_token (parser->lexer);
9430 /* Parse the value. */
9431 value = cp_parser_constant_expression (parser,
9432 /*allow_non_constant_p=*/false,
9433 NULL);
9435 else
9436 value = NULL_TREE;
9438 /* Create the enumerator. */
9439 build_enumerator (identifier, value, type);
9442 /* Parse a namespace-name.
9444 namespace-name:
9445 original-namespace-name
9446 namespace-alias
9448 Returns the NAMESPACE_DECL for the namespace. */
9450 static tree
9451 cp_parser_namespace_name (cp_parser* parser)
9453 tree identifier;
9454 tree namespace_decl;
9456 /* Get the name of the namespace. */
9457 identifier = cp_parser_identifier (parser);
9458 if (identifier == error_mark_node)
9459 return error_mark_node;
9461 /* Look up the identifier in the currently active scope. Look only
9462 for namespaces, due to:
9464 [basic.lookup.udir]
9466 When looking up a namespace-name in a using-directive or alias
9467 definition, only namespace names are considered.
9469 And:
9471 [basic.lookup.qual]
9473 During the lookup of a name preceding the :: scope resolution
9474 operator, object, function, and enumerator names are ignored.
9476 (Note that cp_parser_class_or_namespace_name only calls this
9477 function if the token after the name is the scope resolution
9478 operator.) */
9479 namespace_decl = cp_parser_lookup_name (parser, identifier,
9480 /*is_type=*/false,
9481 /*is_template=*/false,
9482 /*is_namespace=*/true,
9483 /*check_dependency=*/true);
9484 /* If it's not a namespace, issue an error. */
9485 if (namespace_decl == error_mark_node
9486 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9488 cp_parser_error (parser, "expected namespace-name");
9489 namespace_decl = error_mark_node;
9492 return namespace_decl;
9495 /* Parse a namespace-definition.
9497 namespace-definition:
9498 named-namespace-definition
9499 unnamed-namespace-definition
9501 named-namespace-definition:
9502 original-namespace-definition
9503 extension-namespace-definition
9505 original-namespace-definition:
9506 namespace identifier { namespace-body }
9508 extension-namespace-definition:
9509 namespace original-namespace-name { namespace-body }
9511 unnamed-namespace-definition:
9512 namespace { namespace-body } */
9514 static void
9515 cp_parser_namespace_definition (cp_parser* parser)
9517 tree identifier;
9519 /* Look for the `namespace' keyword. */
9520 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9522 /* Get the name of the namespace. We do not attempt to distinguish
9523 between an original-namespace-definition and an
9524 extension-namespace-definition at this point. The semantic
9525 analysis routines are responsible for that. */
9526 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9527 identifier = cp_parser_identifier (parser);
9528 else
9529 identifier = NULL_TREE;
9531 /* Look for the `{' to start the namespace. */
9532 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9533 /* Start the namespace. */
9534 push_namespace (identifier);
9535 /* Parse the body of the namespace. */
9536 cp_parser_namespace_body (parser);
9537 /* Finish the namespace. */
9538 pop_namespace ();
9539 /* Look for the final `}'. */
9540 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9543 /* Parse a namespace-body.
9545 namespace-body:
9546 declaration-seq [opt] */
9548 static void
9549 cp_parser_namespace_body (cp_parser* parser)
9551 cp_parser_declaration_seq_opt (parser);
9554 /* Parse a namespace-alias-definition.
9556 namespace-alias-definition:
9557 namespace identifier = qualified-namespace-specifier ; */
9559 static void
9560 cp_parser_namespace_alias_definition (cp_parser* parser)
9562 tree identifier;
9563 tree namespace_specifier;
9565 /* Look for the `namespace' keyword. */
9566 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9567 /* Look for the identifier. */
9568 identifier = cp_parser_identifier (parser);
9569 if (identifier == error_mark_node)
9570 return;
9571 /* Look for the `=' token. */
9572 cp_parser_require (parser, CPP_EQ, "`='");
9573 /* Look for the qualified-namespace-specifier. */
9574 namespace_specifier
9575 = cp_parser_qualified_namespace_specifier (parser);
9576 /* Look for the `;' token. */
9577 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9579 /* Register the alias in the symbol table. */
9580 do_namespace_alias (identifier, namespace_specifier);
9583 /* Parse a qualified-namespace-specifier.
9585 qualified-namespace-specifier:
9586 :: [opt] nested-name-specifier [opt] namespace-name
9588 Returns a NAMESPACE_DECL corresponding to the specified
9589 namespace. */
9591 static tree
9592 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9594 /* Look for the optional `::'. */
9595 cp_parser_global_scope_opt (parser,
9596 /*current_scope_valid_p=*/false);
9598 /* Look for the optional nested-name-specifier. */
9599 cp_parser_nested_name_specifier_opt (parser,
9600 /*typename_keyword_p=*/false,
9601 /*check_dependency_p=*/true,
9602 /*type_p=*/false,
9603 /*is_declaration=*/true);
9605 return cp_parser_namespace_name (parser);
9608 /* Parse a using-declaration.
9610 using-declaration:
9611 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9612 using :: unqualified-id ; */
9614 static void
9615 cp_parser_using_declaration (cp_parser* parser)
9617 cp_token *token;
9618 bool typename_p = false;
9619 bool global_scope_p;
9620 tree decl;
9621 tree identifier;
9622 tree scope;
9623 tree qscope;
9625 /* Look for the `using' keyword. */
9626 cp_parser_require_keyword (parser, RID_USING, "`using'");
9628 /* Peek at the next token. */
9629 token = cp_lexer_peek_token (parser->lexer);
9630 /* See if it's `typename'. */
9631 if (token->keyword == RID_TYPENAME)
9633 /* Remember that we've seen it. */
9634 typename_p = true;
9635 /* Consume the `typename' token. */
9636 cp_lexer_consume_token (parser->lexer);
9639 /* Look for the optional global scope qualification. */
9640 global_scope_p
9641 = (cp_parser_global_scope_opt (parser,
9642 /*current_scope_valid_p=*/false)
9643 != NULL_TREE);
9645 /* If we saw `typename', or didn't see `::', then there must be a
9646 nested-name-specifier present. */
9647 if (typename_p || !global_scope_p)
9648 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9649 /*check_dependency_p=*/true,
9650 /*type_p=*/false,
9651 /*is_declaration=*/true);
9652 /* Otherwise, we could be in either of the two productions. In that
9653 case, treat the nested-name-specifier as optional. */
9654 else
9655 qscope = cp_parser_nested_name_specifier_opt (parser,
9656 /*typename_keyword_p=*/false,
9657 /*check_dependency_p=*/true,
9658 /*type_p=*/false,
9659 /*is_declaration=*/true);
9660 if (!qscope)
9661 qscope = global_namespace;
9663 /* Parse the unqualified-id. */
9664 identifier = cp_parser_unqualified_id (parser,
9665 /*template_keyword_p=*/false,
9666 /*check_dependency_p=*/true,
9667 /*declarator_p=*/true);
9669 /* The function we call to handle a using-declaration is different
9670 depending on what scope we are in. */
9671 if (identifier == error_mark_node)
9673 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9674 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9675 /* [namespace.udecl]
9677 A using declaration shall not name a template-id. */
9678 error ("a template-id may not appear in a using-declaration");
9679 else
9681 scope = current_scope ();
9682 if (scope && TYPE_P (scope))
9684 /* Create the USING_DECL. */
9685 decl = do_class_using_decl (build_nt (SCOPE_REF,
9686 parser->scope,
9687 identifier));
9688 /* Add it to the list of members in this class. */
9689 finish_member_declaration (decl);
9691 else
9693 decl = cp_parser_lookup_name_simple (parser, identifier);
9694 if (decl == error_mark_node)
9695 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9696 else if (scope)
9697 do_local_using_decl (decl, qscope, identifier);
9698 else
9699 do_toplevel_using_decl (decl, qscope, identifier);
9703 /* Look for the final `;'. */
9704 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9707 /* Parse a using-directive.
9709 using-directive:
9710 using namespace :: [opt] nested-name-specifier [opt]
9711 namespace-name ; */
9713 static void
9714 cp_parser_using_directive (cp_parser* parser)
9716 tree namespace_decl;
9717 tree attribs;
9719 /* Look for the `using' keyword. */
9720 cp_parser_require_keyword (parser, RID_USING, "`using'");
9721 /* And the `namespace' keyword. */
9722 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9723 /* Look for the optional `::' operator. */
9724 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9725 /* And the optional nested-name-specifier. */
9726 cp_parser_nested_name_specifier_opt (parser,
9727 /*typename_keyword_p=*/false,
9728 /*check_dependency_p=*/true,
9729 /*type_p=*/false,
9730 /*is_declaration=*/true);
9731 /* Get the namespace being used. */
9732 namespace_decl = cp_parser_namespace_name (parser);
9733 /* And any specified attributes. */
9734 attribs = cp_parser_attributes_opt (parser);
9735 /* Update the symbol table. */
9736 parse_using_directive (namespace_decl, attribs);
9737 /* Look for the final `;'. */
9738 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9741 /* Parse an asm-definition.
9743 asm-definition:
9744 asm ( string-literal ) ;
9746 GNU Extension:
9748 asm-definition:
9749 asm volatile [opt] ( string-literal ) ;
9750 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9751 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9752 : asm-operand-list [opt] ) ;
9753 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9754 : asm-operand-list [opt]
9755 : asm-operand-list [opt] ) ; */
9757 static void
9758 cp_parser_asm_definition (cp_parser* parser)
9760 cp_token *token;
9761 tree string;
9762 tree outputs = NULL_TREE;
9763 tree inputs = NULL_TREE;
9764 tree clobbers = NULL_TREE;
9765 tree asm_stmt;
9766 bool volatile_p = false;
9767 bool extended_p = false;
9769 /* Look for the `asm' keyword. */
9770 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9771 /* See if the next token is `volatile'. */
9772 if (cp_parser_allow_gnu_extensions_p (parser)
9773 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9775 /* Remember that we saw the `volatile' keyword. */
9776 volatile_p = true;
9777 /* Consume the token. */
9778 cp_lexer_consume_token (parser->lexer);
9780 /* Look for the opening `('. */
9781 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9782 /* Look for the string. */
9783 c_lex_string_translate = false;
9784 token = cp_parser_require (parser, CPP_STRING, "asm body");
9785 if (!token)
9786 goto finish;
9787 string = token->value;
9788 /* If we're allowing GNU extensions, check for the extended assembly
9789 syntax. Unfortunately, the `:' tokens need not be separated by
9790 a space in C, and so, for compatibility, we tolerate that here
9791 too. Doing that means that we have to treat the `::' operator as
9792 two `:' tokens. */
9793 if (cp_parser_allow_gnu_extensions_p (parser)
9794 && at_function_scope_p ()
9795 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9796 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9798 bool inputs_p = false;
9799 bool clobbers_p = false;
9801 /* The extended syntax was used. */
9802 extended_p = true;
9804 /* Look for outputs. */
9805 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9807 /* Consume the `:'. */
9808 cp_lexer_consume_token (parser->lexer);
9809 /* Parse the output-operands. */
9810 if (cp_lexer_next_token_is_not (parser->lexer,
9811 CPP_COLON)
9812 && cp_lexer_next_token_is_not (parser->lexer,
9813 CPP_SCOPE)
9814 && cp_lexer_next_token_is_not (parser->lexer,
9815 CPP_CLOSE_PAREN))
9816 outputs = cp_parser_asm_operand_list (parser);
9818 /* If the next token is `::', there are no outputs, and the
9819 next token is the beginning of the inputs. */
9820 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9822 /* Consume the `::' token. */
9823 cp_lexer_consume_token (parser->lexer);
9824 /* The inputs are coming next. */
9825 inputs_p = true;
9828 /* Look for inputs. */
9829 if (inputs_p
9830 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9832 if (!inputs_p)
9833 /* Consume the `:'. */
9834 cp_lexer_consume_token (parser->lexer);
9835 /* Parse the output-operands. */
9836 if (cp_lexer_next_token_is_not (parser->lexer,
9837 CPP_COLON)
9838 && cp_lexer_next_token_is_not (parser->lexer,
9839 CPP_SCOPE)
9840 && cp_lexer_next_token_is_not (parser->lexer,
9841 CPP_CLOSE_PAREN))
9842 inputs = cp_parser_asm_operand_list (parser);
9844 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9845 /* The clobbers are coming next. */
9846 clobbers_p = true;
9848 /* Look for clobbers. */
9849 if (clobbers_p
9850 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9852 if (!clobbers_p)
9853 /* Consume the `:'. */
9854 cp_lexer_consume_token (parser->lexer);
9855 /* Parse the clobbers. */
9856 if (cp_lexer_next_token_is_not (parser->lexer,
9857 CPP_CLOSE_PAREN))
9858 clobbers = cp_parser_asm_clobber_list (parser);
9861 /* Look for the closing `)'. */
9862 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9863 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9864 /*consume_paren=*/true);
9865 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9867 /* Create the ASM_STMT. */
9868 if (at_function_scope_p ())
9870 asm_stmt =
9871 finish_asm_stmt (volatile_p
9872 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9873 string, outputs, inputs, clobbers);
9874 /* If the extended syntax was not used, mark the ASM_STMT. */
9875 if (!extended_p)
9876 ASM_INPUT_P (asm_stmt) = 1;
9878 else
9879 assemble_asm (string);
9881 finish:
9882 c_lex_string_translate = true;
9885 /* Declarators [gram.dcl.decl] */
9887 /* Parse an init-declarator.
9889 init-declarator:
9890 declarator initializer [opt]
9892 GNU Extension:
9894 init-declarator:
9895 declarator asm-specification [opt] attributes [opt] initializer [opt]
9897 function-definition:
9898 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9899 function-body
9900 decl-specifier-seq [opt] declarator function-try-block
9902 GNU Extension:
9904 function-definition:
9905 __extension__ function-definition
9907 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9908 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9909 then this declarator appears in a class scope. The new DECL created
9910 by this declarator is returned.
9912 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9913 for a function-definition here as well. If the declarator is a
9914 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9915 be TRUE upon return. By that point, the function-definition will
9916 have been completely parsed.
9918 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9919 is FALSE. */
9921 static tree
9922 cp_parser_init_declarator (cp_parser* parser,
9923 tree decl_specifiers,
9924 tree prefix_attributes,
9925 bool function_definition_allowed_p,
9926 bool member_p,
9927 int declares_class_or_enum,
9928 bool* function_definition_p)
9930 cp_token *token;
9931 tree declarator;
9932 tree attributes;
9933 tree asm_specification;
9934 tree initializer;
9935 tree decl = NULL_TREE;
9936 tree scope;
9937 bool is_initialized;
9938 bool is_parenthesized_init;
9939 bool is_non_constant_init;
9940 int ctor_dtor_or_conv_p;
9941 bool friend_p;
9942 bool pop_p = false;
9944 /* Assume that this is not the declarator for a function
9945 definition. */
9946 if (function_definition_p)
9947 *function_definition_p = false;
9949 /* Defer access checks while parsing the declarator; we cannot know
9950 what names are accessible until we know what is being
9951 declared. */
9952 resume_deferring_access_checks ();
9954 /* Parse the declarator. */
9955 declarator
9956 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9957 &ctor_dtor_or_conv_p,
9958 /*parenthesized_p=*/NULL);
9959 /* Gather up the deferred checks. */
9960 stop_deferring_access_checks ();
9962 /* If the DECLARATOR was erroneous, there's no need to go
9963 further. */
9964 if (declarator == error_mark_node)
9965 return error_mark_node;
9967 cp_parser_check_for_definition_in_return_type (declarator,
9968 declares_class_or_enum);
9970 /* Figure out what scope the entity declared by the DECLARATOR is
9971 located in. `grokdeclarator' sometimes changes the scope, so
9972 we compute it now. */
9973 scope = get_scope_of_declarator (declarator);
9975 /* If we're allowing GNU extensions, look for an asm-specification
9976 and attributes. */
9977 if (cp_parser_allow_gnu_extensions_p (parser))
9979 /* Look for an asm-specification. */
9980 asm_specification = cp_parser_asm_specification_opt (parser);
9981 /* And attributes. */
9982 attributes = cp_parser_attributes_opt (parser);
9984 else
9986 asm_specification = NULL_TREE;
9987 attributes = NULL_TREE;
9990 /* Peek at the next token. */
9991 token = cp_lexer_peek_token (parser->lexer);
9992 /* Check to see if the token indicates the start of a
9993 function-definition. */
9994 if (cp_parser_token_starts_function_definition_p (token))
9996 if (!function_definition_allowed_p)
9998 /* If a function-definition should not appear here, issue an
9999 error message. */
10000 cp_parser_error (parser,
10001 "a function-definition is not allowed here");
10002 return error_mark_node;
10004 else
10006 /* Neither attributes nor an asm-specification are allowed
10007 on a function-definition. */
10008 if (asm_specification)
10009 error ("an asm-specification is not allowed on a function-definition");
10010 if (attributes)
10011 error ("attributes are not allowed on a function-definition");
10012 /* This is a function-definition. */
10013 *function_definition_p = true;
10015 /* Parse the function definition. */
10016 if (member_p)
10017 decl = cp_parser_save_member_function_body (parser,
10018 decl_specifiers,
10019 declarator,
10020 prefix_attributes);
10021 else
10022 decl
10023 = (cp_parser_function_definition_from_specifiers_and_declarator
10024 (parser, decl_specifiers, prefix_attributes, declarator));
10026 return decl;
10030 /* [dcl.dcl]
10032 Only in function declarations for constructors, destructors, and
10033 type conversions can the decl-specifier-seq be omitted.
10035 We explicitly postpone this check past the point where we handle
10036 function-definitions because we tolerate function-definitions
10037 that are missing their return types in some modes. */
10038 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10040 cp_parser_error (parser,
10041 "expected constructor, destructor, or type conversion");
10042 return error_mark_node;
10045 /* An `=' or an `(' indicates an initializer. */
10046 is_initialized = (token->type == CPP_EQ
10047 || token->type == CPP_OPEN_PAREN);
10048 /* If the init-declarator isn't initialized and isn't followed by a
10049 `,' or `;', it's not a valid init-declarator. */
10050 if (!is_initialized
10051 && token->type != CPP_COMMA
10052 && token->type != CPP_SEMICOLON)
10054 cp_parser_error (parser, "expected init-declarator");
10055 return error_mark_node;
10058 /* Because start_decl has side-effects, we should only call it if we
10059 know we're going ahead. By this point, we know that we cannot
10060 possibly be looking at any other construct. */
10061 cp_parser_commit_to_tentative_parse (parser);
10063 /* If the decl specifiers were bad, issue an error now that we're
10064 sure this was intended to be a declarator. Then continue
10065 declaring the variable(s), as int, to try to cut down on further
10066 errors. */
10067 if (decl_specifiers != NULL
10068 && TREE_VALUE (decl_specifiers) == error_mark_node)
10070 cp_parser_error (parser, "invalid type in declaration");
10071 TREE_VALUE (decl_specifiers) = integer_type_node;
10074 /* Check to see whether or not this declaration is a friend. */
10075 friend_p = cp_parser_friend_p (decl_specifiers);
10077 /* Check that the number of template-parameter-lists is OK. */
10078 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10079 return error_mark_node;
10081 /* Enter the newly declared entry in the symbol table. If we're
10082 processing a declaration in a class-specifier, we wait until
10083 after processing the initializer. */
10084 if (!member_p)
10086 if (parser->in_unbraced_linkage_specification_p)
10088 decl_specifiers = tree_cons (error_mark_node,
10089 get_identifier ("extern"),
10090 decl_specifiers);
10091 have_extern_spec = false;
10093 decl = start_decl (declarator, decl_specifiers,
10094 is_initialized, attributes, prefix_attributes);
10097 /* Enter the SCOPE. That way unqualified names appearing in the
10098 initializer will be looked up in SCOPE. */
10099 if (scope)
10100 pop_p = push_scope (scope);
10102 /* Perform deferred access control checks, now that we know in which
10103 SCOPE the declared entity resides. */
10104 if (!member_p && decl)
10106 tree saved_current_function_decl = NULL_TREE;
10108 /* If the entity being declared is a function, pretend that we
10109 are in its scope. If it is a `friend', it may have access to
10110 things that would not otherwise be accessible. */
10111 if (TREE_CODE (decl) == FUNCTION_DECL)
10113 saved_current_function_decl = current_function_decl;
10114 current_function_decl = decl;
10117 /* Perform the access control checks for the declarator and the
10118 the decl-specifiers. */
10119 perform_deferred_access_checks ();
10121 /* Restore the saved value. */
10122 if (TREE_CODE (decl) == FUNCTION_DECL)
10123 current_function_decl = saved_current_function_decl;
10126 /* Parse the initializer. */
10127 if (is_initialized)
10128 initializer = cp_parser_initializer (parser,
10129 &is_parenthesized_init,
10130 &is_non_constant_init);
10131 else
10133 initializer = NULL_TREE;
10134 is_parenthesized_init = false;
10135 is_non_constant_init = true;
10138 /* The old parser allows attributes to appear after a parenthesized
10139 initializer. Mark Mitchell proposed removing this functionality
10140 on the GCC mailing lists on 2002-08-13. This parser accepts the
10141 attributes -- but ignores them. */
10142 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10143 if (cp_parser_attributes_opt (parser))
10144 warning ("attributes after parenthesized initializer ignored");
10146 /* Leave the SCOPE, now that we have processed the initializer. It
10147 is important to do this before calling cp_finish_decl because it
10148 makes decisions about whether to create DECL_STMTs or not based
10149 on the current scope. */
10150 if (pop_p)
10151 pop_scope (scope);
10153 /* For an in-class declaration, use `grokfield' to create the
10154 declaration. */
10155 if (member_p)
10157 decl = grokfield (declarator, decl_specifiers,
10158 initializer, /*asmspec=*/NULL_TREE,
10159 /*attributes=*/NULL_TREE);
10160 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10161 cp_parser_save_default_args (parser, decl);
10164 /* Finish processing the declaration. But, skip friend
10165 declarations. */
10166 if (!friend_p && decl)
10167 cp_finish_decl (decl,
10168 initializer,
10169 asm_specification,
10170 /* If the initializer is in parentheses, then this is
10171 a direct-initialization, which means that an
10172 `explicit' constructor is OK. Otherwise, an
10173 `explicit' constructor cannot be used. */
10174 ((is_parenthesized_init || !is_initialized)
10175 ? 0 : LOOKUP_ONLYCONVERTING));
10177 /* Remember whether or not variables were initialized by
10178 constant-expressions. */
10179 if (decl && TREE_CODE (decl) == VAR_DECL
10180 && is_initialized && !is_non_constant_init)
10181 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10183 return decl;
10186 /* Parse a declarator.
10188 declarator:
10189 direct-declarator
10190 ptr-operator declarator
10192 abstract-declarator:
10193 ptr-operator abstract-declarator [opt]
10194 direct-abstract-declarator
10196 GNU Extensions:
10198 declarator:
10199 attributes [opt] direct-declarator
10200 attributes [opt] ptr-operator declarator
10202 abstract-declarator:
10203 attributes [opt] ptr-operator abstract-declarator [opt]
10204 attributes [opt] direct-abstract-declarator
10206 Returns a representation of the declarator. If the declarator has
10207 the form `* declarator', then an INDIRECT_REF is returned, whose
10208 only operand is the sub-declarator. Analogously, `& declarator' is
10209 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10210 used. The first operand is the TYPE for `X'. The second operand
10211 is an INDIRECT_REF whose operand is the sub-declarator.
10213 Otherwise, the representation is as for a direct-declarator.
10215 (It would be better to define a structure type to represent
10216 declarators, rather than abusing `tree' nodes to represent
10217 declarators. That would be much clearer and save some memory.
10218 There is no reason for declarators to be garbage-collected, for
10219 example; they are created during parser and no longer needed after
10220 `grokdeclarator' has been called.)
10222 For a ptr-operator that has the optional cv-qualifier-seq,
10223 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10224 node.
10226 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10227 detect constructor, destructor or conversion operators. It is set
10228 to -1 if the declarator is a name, and +1 if it is a
10229 function. Otherwise it is set to zero. Usually you just want to
10230 test for >0, but internally the negative value is used.
10232 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10233 a decl-specifier-seq unless it declares a constructor, destructor,
10234 or conversion. It might seem that we could check this condition in
10235 semantic analysis, rather than parsing, but that makes it difficult
10236 to handle something like `f()'. We want to notice that there are
10237 no decl-specifiers, and therefore realize that this is an
10238 expression, not a declaration.)
10240 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10241 the declarator is a direct-declarator of the form "(...)". */
10243 static tree
10244 cp_parser_declarator (cp_parser* parser,
10245 cp_parser_declarator_kind dcl_kind,
10246 int* ctor_dtor_or_conv_p,
10247 bool* parenthesized_p)
10249 cp_token *token;
10250 tree declarator;
10251 enum tree_code code;
10252 tree cv_qualifier_seq;
10253 tree class_type;
10254 tree attributes = NULL_TREE;
10256 /* Assume this is not a constructor, destructor, or type-conversion
10257 operator. */
10258 if (ctor_dtor_or_conv_p)
10259 *ctor_dtor_or_conv_p = 0;
10261 if (cp_parser_allow_gnu_extensions_p (parser))
10262 attributes = cp_parser_attributes_opt (parser);
10264 /* Peek at the next token. */
10265 token = cp_lexer_peek_token (parser->lexer);
10267 /* Check for the ptr-operator production. */
10268 cp_parser_parse_tentatively (parser);
10269 /* Parse the ptr-operator. */
10270 code = cp_parser_ptr_operator (parser,
10271 &class_type,
10272 &cv_qualifier_seq);
10273 /* If that worked, then we have a ptr-operator. */
10274 if (cp_parser_parse_definitely (parser))
10276 /* If a ptr-operator was found, then this declarator was not
10277 parenthesized. */
10278 if (parenthesized_p)
10279 *parenthesized_p = true;
10280 /* The dependent declarator is optional if we are parsing an
10281 abstract-declarator. */
10282 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10283 cp_parser_parse_tentatively (parser);
10285 /* Parse the dependent declarator. */
10286 declarator = cp_parser_declarator (parser, dcl_kind,
10287 /*ctor_dtor_or_conv_p=*/NULL,
10288 /*parenthesized_p=*/NULL);
10290 /* If we are parsing an abstract-declarator, we must handle the
10291 case where the dependent declarator is absent. */
10292 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10293 && !cp_parser_parse_definitely (parser))
10294 declarator = NULL_TREE;
10296 /* Build the representation of the ptr-operator. */
10297 if (code == INDIRECT_REF)
10298 declarator = make_pointer_declarator (cv_qualifier_seq,
10299 declarator);
10300 else
10301 declarator = make_reference_declarator (cv_qualifier_seq,
10302 declarator);
10303 /* Handle the pointer-to-member case. */
10304 if (class_type)
10305 declarator = build_nt (SCOPE_REF, class_type, declarator);
10307 /* Everything else is a direct-declarator. */
10308 else
10310 if (parenthesized_p)
10311 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10312 CPP_OPEN_PAREN);
10313 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10314 ctor_dtor_or_conv_p);
10317 if (attributes && declarator != error_mark_node)
10318 declarator = tree_cons (attributes, declarator, NULL_TREE);
10320 return declarator;
10323 /* Parse a direct-declarator or direct-abstract-declarator.
10325 direct-declarator:
10326 declarator-id
10327 direct-declarator ( parameter-declaration-clause )
10328 cv-qualifier-seq [opt]
10329 exception-specification [opt]
10330 direct-declarator [ constant-expression [opt] ]
10331 ( declarator )
10333 direct-abstract-declarator:
10334 direct-abstract-declarator [opt]
10335 ( parameter-declaration-clause )
10336 cv-qualifier-seq [opt]
10337 exception-specification [opt]
10338 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10339 ( abstract-declarator )
10341 Returns a representation of the declarator. DCL_KIND is
10342 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10343 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10344 we are parsing a direct-declarator. It is
10345 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10346 of ambiguity we prefer an abstract declarator, as per
10347 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10348 cp_parser_declarator.
10350 For the declarator-id production, the representation is as for an
10351 id-expression, except that a qualified name is represented as a
10352 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10353 see the documentation of the FUNCTION_DECLARATOR_* macros for
10354 information about how to find the various declarator components.
10355 An array-declarator is represented as an ARRAY_REF. The
10356 direct-declarator is the first operand; the constant-expression
10357 indicating the size of the array is the second operand. */
10359 static tree
10360 cp_parser_direct_declarator (cp_parser* parser,
10361 cp_parser_declarator_kind dcl_kind,
10362 int* ctor_dtor_or_conv_p)
10364 cp_token *token;
10365 tree declarator = NULL_TREE;
10366 tree scope = NULL_TREE;
10367 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10368 bool saved_in_declarator_p = parser->in_declarator_p;
10369 bool first = true;
10370 bool pop_p = false;
10372 while (true)
10374 /* Peek at the next token. */
10375 token = cp_lexer_peek_token (parser->lexer);
10376 if (token->type == CPP_OPEN_PAREN)
10378 /* This is either a parameter-declaration-clause, or a
10379 parenthesized declarator. When we know we are parsing a
10380 named declarator, it must be a parenthesized declarator
10381 if FIRST is true. For instance, `(int)' is a
10382 parameter-declaration-clause, with an omitted
10383 direct-abstract-declarator. But `((*))', is a
10384 parenthesized abstract declarator. Finally, when T is a
10385 template parameter `(T)' is a
10386 parameter-declaration-clause, and not a parenthesized
10387 named declarator.
10389 We first try and parse a parameter-declaration-clause,
10390 and then try a nested declarator (if FIRST is true).
10392 It is not an error for it not to be a
10393 parameter-declaration-clause, even when FIRST is
10394 false. Consider,
10396 int i (int);
10397 int i (3);
10399 The first is the declaration of a function while the
10400 second is a the definition of a variable, including its
10401 initializer.
10403 Having seen only the parenthesis, we cannot know which of
10404 these two alternatives should be selected. Even more
10405 complex are examples like:
10407 int i (int (a));
10408 int i (int (3));
10410 The former is a function-declaration; the latter is a
10411 variable initialization.
10413 Thus again, we try a parameter-declaration-clause, and if
10414 that fails, we back out and return. */
10416 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10418 tree params;
10419 unsigned saved_num_template_parameter_lists;
10421 cp_parser_parse_tentatively (parser);
10423 /* Consume the `('. */
10424 cp_lexer_consume_token (parser->lexer);
10425 if (first)
10427 /* If this is going to be an abstract declarator, we're
10428 in a declarator and we can't have default args. */
10429 parser->default_arg_ok_p = false;
10430 parser->in_declarator_p = true;
10433 /* Inside the function parameter list, surrounding
10434 template-parameter-lists do not apply. */
10435 saved_num_template_parameter_lists
10436 = parser->num_template_parameter_lists;
10437 parser->num_template_parameter_lists = 0;
10439 /* Parse the parameter-declaration-clause. */
10440 params = cp_parser_parameter_declaration_clause (parser);
10442 parser->num_template_parameter_lists
10443 = saved_num_template_parameter_lists;
10445 /* If all went well, parse the cv-qualifier-seq and the
10446 exception-specification. */
10447 if (cp_parser_parse_definitely (parser))
10449 tree cv_qualifiers;
10450 tree exception_specification;
10452 if (ctor_dtor_or_conv_p)
10453 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10454 first = false;
10455 /* Consume the `)'. */
10456 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10458 /* Parse the cv-qualifier-seq. */
10459 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10460 /* And the exception-specification. */
10461 exception_specification
10462 = cp_parser_exception_specification_opt (parser);
10464 /* Create the function-declarator. */
10465 declarator = make_call_declarator (declarator,
10466 params,
10467 cv_qualifiers,
10468 exception_specification);
10469 /* Any subsequent parameter lists are to do with
10470 return type, so are not those of the declared
10471 function. */
10472 parser->default_arg_ok_p = false;
10474 /* Repeat the main loop. */
10475 continue;
10479 /* If this is the first, we can try a parenthesized
10480 declarator. */
10481 if (first)
10483 bool saved_in_type_id_in_expr_p;
10485 parser->default_arg_ok_p = saved_default_arg_ok_p;
10486 parser->in_declarator_p = saved_in_declarator_p;
10488 /* Consume the `('. */
10489 cp_lexer_consume_token (parser->lexer);
10490 /* Parse the nested declarator. */
10491 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10492 parser->in_type_id_in_expr_p = true;
10493 declarator
10494 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10495 /*parenthesized_p=*/NULL);
10496 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10497 first = false;
10498 /* Expect a `)'. */
10499 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10500 declarator = error_mark_node;
10501 if (declarator == error_mark_node)
10502 break;
10504 goto handle_declarator;
10506 /* Otherwise, we must be done. */
10507 else
10508 break;
10510 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10511 && token->type == CPP_OPEN_SQUARE)
10513 /* Parse an array-declarator. */
10514 tree bounds;
10516 if (ctor_dtor_or_conv_p)
10517 *ctor_dtor_or_conv_p = 0;
10519 first = false;
10520 parser->default_arg_ok_p = false;
10521 parser->in_declarator_p = true;
10522 /* Consume the `['. */
10523 cp_lexer_consume_token (parser->lexer);
10524 /* Peek at the next token. */
10525 token = cp_lexer_peek_token (parser->lexer);
10526 /* If the next token is `]', then there is no
10527 constant-expression. */
10528 if (token->type != CPP_CLOSE_SQUARE)
10530 bool non_constant_p;
10532 bounds
10533 = cp_parser_constant_expression (parser,
10534 /*allow_non_constant=*/true,
10535 &non_constant_p);
10536 if (!non_constant_p)
10537 bounds = fold_non_dependent_expr (bounds);
10539 else
10540 bounds = NULL_TREE;
10541 /* Look for the closing `]'. */
10542 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10544 declarator = error_mark_node;
10545 break;
10548 declarator = build_nt (ARRAY_REF, declarator, bounds);
10550 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10552 /* Parse a declarator-id */
10553 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10554 cp_parser_parse_tentatively (parser);
10555 declarator = cp_parser_declarator_id (parser);
10556 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10558 if (!cp_parser_parse_definitely (parser))
10559 declarator = error_mark_node;
10560 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10562 cp_parser_error (parser, "expected unqualified-id");
10563 declarator = error_mark_node;
10567 if (declarator == error_mark_node)
10568 break;
10570 if (TREE_CODE (declarator) == SCOPE_REF
10571 && !current_scope ())
10573 tree scope = TREE_OPERAND (declarator, 0);
10575 /* In the declaration of a member of a template class
10576 outside of the class itself, the SCOPE will sometimes
10577 be a TYPENAME_TYPE. For example, given:
10579 template <typename T>
10580 int S<T>::R::i = 3;
10582 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10583 this context, we must resolve S<T>::R to an ordinary
10584 type, rather than a typename type.
10586 The reason we normally avoid resolving TYPENAME_TYPEs
10587 is that a specialization of `S' might render
10588 `S<T>::R' not a type. However, if `S' is
10589 specialized, then this `i' will not be used, so there
10590 is no harm in resolving the types here. */
10591 if (TREE_CODE (scope) == TYPENAME_TYPE)
10593 tree type;
10595 /* Resolve the TYPENAME_TYPE. */
10596 type = resolve_typename_type (scope,
10597 /*only_current_p=*/false);
10598 /* If that failed, the declarator is invalid. */
10599 if (type != error_mark_node)
10600 scope = type;
10601 /* Build a new DECLARATOR. */
10602 declarator = build_nt (SCOPE_REF,
10603 scope,
10604 TREE_OPERAND (declarator, 1));
10608 /* Check to see whether the declarator-id names a constructor,
10609 destructor, or conversion. */
10610 if (declarator && ctor_dtor_or_conv_p
10611 && ((TREE_CODE (declarator) == SCOPE_REF
10612 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10613 || (TREE_CODE (declarator) != SCOPE_REF
10614 && at_class_scope_p ())))
10616 tree unqualified_name;
10617 tree class_type;
10619 /* Get the unqualified part of the name. */
10620 if (TREE_CODE (declarator) == SCOPE_REF)
10622 class_type = TREE_OPERAND (declarator, 0);
10623 unqualified_name = TREE_OPERAND (declarator, 1);
10625 else
10627 class_type = current_class_type;
10628 unqualified_name = declarator;
10631 /* See if it names ctor, dtor or conv. */
10632 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10633 || IDENTIFIER_TYPENAME_P (unqualified_name)
10634 || constructor_name_p (unqualified_name, class_type)
10635 || (TREE_CODE (unqualified_name) == TYPE_DECL
10636 && same_type_p (TREE_TYPE (unqualified_name),
10637 class_type)))
10638 *ctor_dtor_or_conv_p = -1;
10641 handle_declarator:;
10642 scope = get_scope_of_declarator (declarator);
10643 if (scope)
10644 /* Any names that appear after the declarator-id for a
10645 member are looked up in the containing scope. */
10646 pop_p = push_scope (scope);
10647 parser->in_declarator_p = true;
10648 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10649 || (declarator
10650 && (TREE_CODE (declarator) == SCOPE_REF
10651 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10652 /* Default args are only allowed on function
10653 declarations. */
10654 parser->default_arg_ok_p = saved_default_arg_ok_p;
10655 else
10656 parser->default_arg_ok_p = false;
10658 first = false;
10660 /* We're done. */
10661 else
10662 break;
10665 /* For an abstract declarator, we might wind up with nothing at this
10666 point. That's an error; the declarator is not optional. */
10667 if (!declarator)
10668 cp_parser_error (parser, "expected declarator");
10670 /* If we entered a scope, we must exit it now. */
10671 if (pop_p)
10672 pop_scope (scope);
10674 parser->default_arg_ok_p = saved_default_arg_ok_p;
10675 parser->in_declarator_p = saved_in_declarator_p;
10677 return declarator;
10680 /* Parse a ptr-operator.
10682 ptr-operator:
10683 * cv-qualifier-seq [opt]
10685 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10687 GNU Extension:
10689 ptr-operator:
10690 & cv-qualifier-seq [opt]
10692 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10693 used. Returns ADDR_EXPR if a reference was used. In the
10694 case of a pointer-to-member, *TYPE is filled in with the
10695 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10696 with the cv-qualifier-seq, or NULL_TREE, if there are no
10697 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10699 static enum tree_code
10700 cp_parser_ptr_operator (cp_parser* parser,
10701 tree* type,
10702 tree* cv_qualifier_seq)
10704 enum tree_code code = ERROR_MARK;
10705 cp_token *token;
10707 /* Assume that it's not a pointer-to-member. */
10708 *type = NULL_TREE;
10709 /* And that there are no cv-qualifiers. */
10710 *cv_qualifier_seq = NULL_TREE;
10712 /* Peek at the next token. */
10713 token = cp_lexer_peek_token (parser->lexer);
10714 /* If it's a `*' or `&' we have a pointer or reference. */
10715 if (token->type == CPP_MULT || token->type == CPP_AND)
10717 /* Remember which ptr-operator we were processing. */
10718 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10720 /* Consume the `*' or `&'. */
10721 cp_lexer_consume_token (parser->lexer);
10723 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10724 `&', if we are allowing GNU extensions. (The only qualifier
10725 that can legally appear after `&' is `restrict', but that is
10726 enforced during semantic analysis. */
10727 if (code == INDIRECT_REF
10728 || cp_parser_allow_gnu_extensions_p (parser))
10729 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10731 else
10733 /* Try the pointer-to-member case. */
10734 cp_parser_parse_tentatively (parser);
10735 /* Look for the optional `::' operator. */
10736 cp_parser_global_scope_opt (parser,
10737 /*current_scope_valid_p=*/false);
10738 /* Look for the nested-name specifier. */
10739 cp_parser_nested_name_specifier (parser,
10740 /*typename_keyword_p=*/false,
10741 /*check_dependency_p=*/true,
10742 /*type_p=*/false,
10743 /*is_declaration=*/false);
10744 /* If we found it, and the next token is a `*', then we are
10745 indeed looking at a pointer-to-member operator. */
10746 if (!cp_parser_error_occurred (parser)
10747 && cp_parser_require (parser, CPP_MULT, "`*'"))
10749 /* The type of which the member is a member is given by the
10750 current SCOPE. */
10751 *type = parser->scope;
10752 /* The next name will not be qualified. */
10753 parser->scope = NULL_TREE;
10754 parser->qualifying_scope = NULL_TREE;
10755 parser->object_scope = NULL_TREE;
10756 /* Indicate that the `*' operator was used. */
10757 code = INDIRECT_REF;
10758 /* Look for the optional cv-qualifier-seq. */
10759 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10761 /* If that didn't work we don't have a ptr-operator. */
10762 if (!cp_parser_parse_definitely (parser))
10763 cp_parser_error (parser, "expected ptr-operator");
10766 return code;
10769 /* Parse an (optional) cv-qualifier-seq.
10771 cv-qualifier-seq:
10772 cv-qualifier cv-qualifier-seq [opt]
10774 Returns a TREE_LIST. The TREE_VALUE of each node is the
10775 representation of a cv-qualifier. */
10777 static tree
10778 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10780 tree cv_qualifiers = NULL_TREE;
10782 while (true)
10784 tree cv_qualifier;
10786 /* Look for the next cv-qualifier. */
10787 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10788 /* If we didn't find one, we're done. */
10789 if (!cv_qualifier)
10790 break;
10792 /* Add this cv-qualifier to the list. */
10793 cv_qualifiers
10794 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10797 /* We built up the list in reverse order. */
10798 return nreverse (cv_qualifiers);
10801 /* Parse an (optional) cv-qualifier.
10803 cv-qualifier:
10804 const
10805 volatile
10807 GNU Extension:
10809 cv-qualifier:
10810 __restrict__ */
10812 static tree
10813 cp_parser_cv_qualifier_opt (cp_parser* parser)
10815 cp_token *token;
10816 tree cv_qualifier = NULL_TREE;
10818 /* Peek at the next token. */
10819 token = cp_lexer_peek_token (parser->lexer);
10820 /* See if it's a cv-qualifier. */
10821 switch (token->keyword)
10823 case RID_CONST:
10824 case RID_VOLATILE:
10825 case RID_RESTRICT:
10826 /* Save the value of the token. */
10827 cv_qualifier = token->value;
10828 /* Consume the token. */
10829 cp_lexer_consume_token (parser->lexer);
10830 break;
10832 default:
10833 break;
10836 return cv_qualifier;
10839 /* Parse a declarator-id.
10841 declarator-id:
10842 id-expression
10843 :: [opt] nested-name-specifier [opt] type-name
10845 In the `id-expression' case, the value returned is as for
10846 cp_parser_id_expression if the id-expression was an unqualified-id.
10847 If the id-expression was a qualified-id, then a SCOPE_REF is
10848 returned. The first operand is the scope (either a NAMESPACE_DECL
10849 or TREE_TYPE), but the second is still just a representation of an
10850 unqualified-id. */
10852 static tree
10853 cp_parser_declarator_id (cp_parser* parser)
10855 tree id_expression;
10857 /* The expression must be an id-expression. Assume that qualified
10858 names are the names of types so that:
10860 template <class T>
10861 int S<T>::R::i = 3;
10863 will work; we must treat `S<T>::R' as the name of a type.
10864 Similarly, assume that qualified names are templates, where
10865 required, so that:
10867 template <class T>
10868 int S<T>::R<T>::i = 3;
10870 will work, too. */
10871 id_expression = cp_parser_id_expression (parser,
10872 /*template_keyword_p=*/false,
10873 /*check_dependency_p=*/false,
10874 /*template_p=*/NULL,
10875 /*declarator_p=*/true);
10876 /* If the name was qualified, create a SCOPE_REF to represent
10877 that. */
10878 if (parser->scope)
10880 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10881 parser->scope = NULL_TREE;
10884 return id_expression;
10887 /* Parse a type-id.
10889 type-id:
10890 type-specifier-seq abstract-declarator [opt]
10892 Returns the TYPE specified. */
10894 static tree
10895 cp_parser_type_id (cp_parser* parser)
10897 tree type_specifier_seq;
10898 tree abstract_declarator;
10900 /* Parse the type-specifier-seq. */
10901 type_specifier_seq
10902 = cp_parser_type_specifier_seq (parser);
10903 if (type_specifier_seq == error_mark_node)
10904 return error_mark_node;
10906 /* There might or might not be an abstract declarator. */
10907 cp_parser_parse_tentatively (parser);
10908 /* Look for the declarator. */
10909 abstract_declarator
10910 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10911 /*parenthesized_p=*/NULL);
10912 /* Check to see if there really was a declarator. */
10913 if (!cp_parser_parse_definitely (parser))
10914 abstract_declarator = NULL_TREE;
10916 return groktypename (build_tree_list (type_specifier_seq,
10917 abstract_declarator));
10920 /* Parse a type-specifier-seq.
10922 type-specifier-seq:
10923 type-specifier type-specifier-seq [opt]
10925 GNU extension:
10927 type-specifier-seq:
10928 attributes type-specifier-seq [opt]
10930 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10931 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10933 static tree
10934 cp_parser_type_specifier_seq (cp_parser* parser)
10936 bool seen_type_specifier = false;
10937 tree type_specifier_seq = NULL_TREE;
10939 /* Parse the type-specifiers and attributes. */
10940 while (true)
10942 tree type_specifier;
10944 /* Check for attributes first. */
10945 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10947 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10948 NULL_TREE,
10949 type_specifier_seq);
10950 continue;
10953 /* After the first type-specifier, others are optional. */
10954 if (seen_type_specifier)
10955 cp_parser_parse_tentatively (parser);
10956 /* Look for the type-specifier. */
10957 type_specifier = cp_parser_type_specifier (parser,
10958 CP_PARSER_FLAGS_NONE,
10959 /*is_friend=*/false,
10960 /*is_declaration=*/false,
10961 NULL,
10962 NULL);
10963 /* If the first type-specifier could not be found, this is not a
10964 type-specifier-seq at all. */
10965 if (!seen_type_specifier && type_specifier == error_mark_node)
10966 return error_mark_node;
10967 /* If subsequent type-specifiers could not be found, the
10968 type-specifier-seq is complete. */
10969 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10970 break;
10972 /* Add the new type-specifier to the list. */
10973 type_specifier_seq
10974 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10975 seen_type_specifier = true;
10978 /* We built up the list in reverse order. */
10979 return nreverse (type_specifier_seq);
10982 /* Parse a parameter-declaration-clause.
10984 parameter-declaration-clause:
10985 parameter-declaration-list [opt] ... [opt]
10986 parameter-declaration-list , ...
10988 Returns a representation for the parameter declarations. Each node
10989 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10990 representation.) If the parameter-declaration-clause ends with an
10991 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10992 list. A return value of NULL_TREE indicates a
10993 parameter-declaration-clause consisting only of an ellipsis. */
10995 static tree
10996 cp_parser_parameter_declaration_clause (cp_parser* parser)
10998 tree parameters;
10999 cp_token *token;
11000 bool ellipsis_p;
11002 /* Peek at the next token. */
11003 token = cp_lexer_peek_token (parser->lexer);
11004 /* Check for trivial parameter-declaration-clauses. */
11005 if (token->type == CPP_ELLIPSIS)
11007 /* Consume the `...' token. */
11008 cp_lexer_consume_token (parser->lexer);
11009 return NULL_TREE;
11011 else if (token->type == CPP_CLOSE_PAREN)
11012 /* There are no parameters. */
11014 #ifndef NO_IMPLICIT_EXTERN_C
11015 if (in_system_header && current_class_type == NULL
11016 && current_lang_name == lang_name_c)
11017 return NULL_TREE;
11018 else
11019 #endif
11020 return void_list_node;
11022 /* Check for `(void)', too, which is a special case. */
11023 else if (token->keyword == RID_VOID
11024 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11025 == CPP_CLOSE_PAREN))
11027 /* Consume the `void' token. */
11028 cp_lexer_consume_token (parser->lexer);
11029 /* There are no parameters. */
11030 return void_list_node;
11033 /* Parse the parameter-declaration-list. */
11034 parameters = cp_parser_parameter_declaration_list (parser);
11035 /* If a parse error occurred while parsing the
11036 parameter-declaration-list, then the entire
11037 parameter-declaration-clause is erroneous. */
11038 if (parameters == error_mark_node)
11039 return error_mark_node;
11041 /* Peek at the next token. */
11042 token = cp_lexer_peek_token (parser->lexer);
11043 /* If it's a `,', the clause should terminate with an ellipsis. */
11044 if (token->type == CPP_COMMA)
11046 /* Consume the `,'. */
11047 cp_lexer_consume_token (parser->lexer);
11048 /* Expect an ellipsis. */
11049 ellipsis_p
11050 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11052 /* It might also be `...' if the optional trailing `,' was
11053 omitted. */
11054 else if (token->type == CPP_ELLIPSIS)
11056 /* Consume the `...' token. */
11057 cp_lexer_consume_token (parser->lexer);
11058 /* And remember that we saw it. */
11059 ellipsis_p = true;
11061 else
11062 ellipsis_p = false;
11064 /* Finish the parameter list. */
11065 return finish_parmlist (parameters, ellipsis_p);
11068 /* Parse a parameter-declaration-list.
11070 parameter-declaration-list:
11071 parameter-declaration
11072 parameter-declaration-list , parameter-declaration
11074 Returns a representation of the parameter-declaration-list, as for
11075 cp_parser_parameter_declaration_clause. However, the
11076 `void_list_node' is never appended to the list. */
11078 static tree
11079 cp_parser_parameter_declaration_list (cp_parser* parser)
11081 tree parameters = NULL_TREE;
11083 /* Look for more parameters. */
11084 while (true)
11086 tree parameter;
11087 bool parenthesized_p;
11088 /* Parse the parameter. */
11089 parameter
11090 = cp_parser_parameter_declaration (parser,
11091 /*template_parm_p=*/false,
11092 &parenthesized_p);
11094 /* If a parse error occurred parsing the parameter declaration,
11095 then the entire parameter-declaration-list is erroneous. */
11096 if (parameter == error_mark_node)
11098 parameters = error_mark_node;
11099 break;
11101 /* Add the new parameter to the list. */
11102 TREE_CHAIN (parameter) = parameters;
11103 parameters = parameter;
11105 /* Peek at the next token. */
11106 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11107 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11108 /* The parameter-declaration-list is complete. */
11109 break;
11110 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11112 cp_token *token;
11114 /* Peek at the next token. */
11115 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11116 /* If it's an ellipsis, then the list is complete. */
11117 if (token->type == CPP_ELLIPSIS)
11118 break;
11119 /* Otherwise, there must be more parameters. Consume the
11120 `,'. */
11121 cp_lexer_consume_token (parser->lexer);
11122 /* When parsing something like:
11124 int i(float f, double d)
11126 we can tell after seeing the declaration for "f" that we
11127 are not looking at an initialization of a variable "i",
11128 but rather at the declaration of a function "i".
11130 Due to the fact that the parsing of template arguments
11131 (as specified to a template-id) requires backtracking we
11132 cannot use this technique when inside a template argument
11133 list. */
11134 if (!parser->in_template_argument_list_p
11135 && !parser->in_type_id_in_expr_p
11136 && cp_parser_parsing_tentatively (parser)
11137 && !cp_parser_committed_to_tentative_parse (parser)
11138 /* However, a parameter-declaration of the form
11139 "foat(f)" (which is a valid declaration of a
11140 parameter "f") can also be interpreted as an
11141 expression (the conversion of "f" to "float"). */
11142 && !parenthesized_p)
11143 cp_parser_commit_to_tentative_parse (parser);
11145 else
11147 cp_parser_error (parser, "expected `,' or `...'");
11148 if (!cp_parser_parsing_tentatively (parser)
11149 || cp_parser_committed_to_tentative_parse (parser))
11150 cp_parser_skip_to_closing_parenthesis (parser,
11151 /*recovering=*/true,
11152 /*or_comma=*/false,
11153 /*consume_paren=*/false);
11154 break;
11158 /* We built up the list in reverse order; straighten it out now. */
11159 return nreverse (parameters);
11162 /* Parse a parameter declaration.
11164 parameter-declaration:
11165 decl-specifier-seq declarator
11166 decl-specifier-seq declarator = assignment-expression
11167 decl-specifier-seq abstract-declarator [opt]
11168 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11170 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11171 declares a template parameter. (In that case, a non-nested `>'
11172 token encountered during the parsing of the assignment-expression
11173 is not interpreted as a greater-than operator.)
11175 Returns a TREE_LIST representing the parameter-declaration. The
11176 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11177 there is no default argument. The TREE_VALUE is a representation
11178 of the decl-specifier-seq and declarator. In particular, the
11179 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11180 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11181 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11182 the declarator is of the form "(p)". */
11184 static tree
11185 cp_parser_parameter_declaration (cp_parser *parser,
11186 bool template_parm_p,
11187 bool *parenthesized_p)
11189 int declares_class_or_enum;
11190 bool greater_than_is_operator_p;
11191 tree decl_specifiers;
11192 tree attributes;
11193 tree declarator;
11194 tree default_argument;
11195 tree parameter;
11196 cp_token *token;
11197 const char *saved_message;
11199 /* In a template parameter, `>' is not an operator.
11201 [temp.param]
11203 When parsing a default template-argument for a non-type
11204 template-parameter, the first non-nested `>' is taken as the end
11205 of the template parameter-list rather than a greater-than
11206 operator. */
11207 greater_than_is_operator_p = !template_parm_p;
11209 /* Type definitions may not appear in parameter types. */
11210 saved_message = parser->type_definition_forbidden_message;
11211 parser->type_definition_forbidden_message
11212 = "types may not be defined in parameter types";
11214 /* Parse the declaration-specifiers. */
11215 decl_specifiers
11216 = cp_parser_decl_specifier_seq (parser,
11217 CP_PARSER_FLAGS_NONE,
11218 &attributes,
11219 &declares_class_or_enum);
11220 /* If an error occurred, there's no reason to attempt to parse the
11221 rest of the declaration. */
11222 if (cp_parser_error_occurred (parser))
11224 parser->type_definition_forbidden_message = saved_message;
11225 return error_mark_node;
11228 /* Peek at the next token. */
11229 token = cp_lexer_peek_token (parser->lexer);
11230 /* If the next token is a `)', `,', `=', `>', or `...', then there
11231 is no declarator. */
11232 if (token->type == CPP_CLOSE_PAREN
11233 || token->type == CPP_COMMA
11234 || token->type == CPP_EQ
11235 || token->type == CPP_ELLIPSIS
11236 || token->type == CPP_GREATER)
11238 declarator = NULL_TREE;
11239 if (parenthesized_p)
11240 *parenthesized_p = false;
11242 /* Otherwise, there should be a declarator. */
11243 else
11245 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11246 parser->default_arg_ok_p = false;
11248 /* After seeing a decl-specifier-seq, if the next token is not a
11249 "(", there is no possibility that the code is a valid
11250 expression. Therefore, if parsing tentatively, we commit at
11251 this point. */
11252 if (!parser->in_template_argument_list_p
11253 /* In an expression context, having seen:
11255 (int((char ...
11257 we cannot be sure whether we are looking at a
11258 function-type (taking a "char" as a parameter) or a cast
11259 of some object of type "char" to "int". */
11260 && !parser->in_type_id_in_expr_p
11261 && cp_parser_parsing_tentatively (parser)
11262 && !cp_parser_committed_to_tentative_parse (parser)
11263 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11264 cp_parser_commit_to_tentative_parse (parser);
11265 /* Parse the declarator. */
11266 declarator = cp_parser_declarator (parser,
11267 CP_PARSER_DECLARATOR_EITHER,
11268 /*ctor_dtor_or_conv_p=*/NULL,
11269 parenthesized_p);
11270 parser->default_arg_ok_p = saved_default_arg_ok_p;
11271 /* After the declarator, allow more attributes. */
11272 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11275 /* The restriction on defining new types applies only to the type
11276 of the parameter, not to the default argument. */
11277 parser->type_definition_forbidden_message = saved_message;
11279 /* If the next token is `=', then process a default argument. */
11280 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11282 bool saved_greater_than_is_operator_p;
11283 /* Consume the `='. */
11284 cp_lexer_consume_token (parser->lexer);
11286 /* If we are defining a class, then the tokens that make up the
11287 default argument must be saved and processed later. */
11288 if (!template_parm_p && at_class_scope_p ()
11289 && TYPE_BEING_DEFINED (current_class_type))
11291 unsigned depth = 0;
11293 /* Create a DEFAULT_ARG to represented the unparsed default
11294 argument. */
11295 default_argument = make_node (DEFAULT_ARG);
11296 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11298 /* Add tokens until we have processed the entire default
11299 argument. */
11300 while (true)
11302 bool done = false;
11303 cp_token *token;
11305 /* Peek at the next token. */
11306 token = cp_lexer_peek_token (parser->lexer);
11307 /* What we do depends on what token we have. */
11308 switch (token->type)
11310 /* In valid code, a default argument must be
11311 immediately followed by a `,' `)', or `...'. */
11312 case CPP_COMMA:
11313 case CPP_CLOSE_PAREN:
11314 case CPP_ELLIPSIS:
11315 /* If we run into a non-nested `;', `}', or `]',
11316 then the code is invalid -- but the default
11317 argument is certainly over. */
11318 case CPP_SEMICOLON:
11319 case CPP_CLOSE_BRACE:
11320 case CPP_CLOSE_SQUARE:
11321 if (depth == 0)
11322 done = true;
11323 /* Update DEPTH, if necessary. */
11324 else if (token->type == CPP_CLOSE_PAREN
11325 || token->type == CPP_CLOSE_BRACE
11326 || token->type == CPP_CLOSE_SQUARE)
11327 --depth;
11328 break;
11330 case CPP_OPEN_PAREN:
11331 case CPP_OPEN_SQUARE:
11332 case CPP_OPEN_BRACE:
11333 ++depth;
11334 break;
11336 case CPP_GREATER:
11337 /* If we see a non-nested `>', and `>' is not an
11338 operator, then it marks the end of the default
11339 argument. */
11340 if (!depth && !greater_than_is_operator_p)
11341 done = true;
11342 break;
11344 /* If we run out of tokens, issue an error message. */
11345 case CPP_EOF:
11346 error ("file ends in default argument");
11347 done = true;
11348 break;
11350 case CPP_NAME:
11351 case CPP_SCOPE:
11352 /* In these cases, we should look for template-ids.
11353 For example, if the default argument is
11354 `X<int, double>()', we need to do name lookup to
11355 figure out whether or not `X' is a template; if
11356 so, the `,' does not end the default argument.
11358 That is not yet done. */
11359 break;
11361 default:
11362 break;
11365 /* If we've reached the end, stop. */
11366 if (done)
11367 break;
11369 /* Add the token to the token block. */
11370 token = cp_lexer_consume_token (parser->lexer);
11371 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11372 token);
11375 /* Outside of a class definition, we can just parse the
11376 assignment-expression. */
11377 else
11379 bool saved_local_variables_forbidden_p;
11381 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11382 set correctly. */
11383 saved_greater_than_is_operator_p
11384 = parser->greater_than_is_operator_p;
11385 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11386 /* Local variable names (and the `this' keyword) may not
11387 appear in a default argument. */
11388 saved_local_variables_forbidden_p
11389 = parser->local_variables_forbidden_p;
11390 parser->local_variables_forbidden_p = true;
11391 /* Parse the assignment-expression. */
11392 default_argument = cp_parser_assignment_expression (parser);
11393 /* Restore saved state. */
11394 parser->greater_than_is_operator_p
11395 = saved_greater_than_is_operator_p;
11396 parser->local_variables_forbidden_p
11397 = saved_local_variables_forbidden_p;
11399 if (!parser->default_arg_ok_p)
11401 if (!flag_pedantic_errors)
11402 warning ("deprecated use of default argument for parameter of non-function");
11403 else
11405 error ("default arguments are only permitted for function parameters");
11406 default_argument = NULL_TREE;
11410 else
11411 default_argument = NULL_TREE;
11413 /* Create the representation of the parameter. */
11414 if (attributes)
11415 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11416 parameter = build_tree_list (default_argument,
11417 build_tree_list (decl_specifiers,
11418 declarator));
11420 return parameter;
11423 /* Parse a function-body.
11425 function-body:
11426 compound_statement */
11428 static void
11429 cp_parser_function_body (cp_parser *parser)
11431 cp_parser_compound_statement (parser, false);
11434 /* Parse a ctor-initializer-opt followed by a function-body. Return
11435 true if a ctor-initializer was present. */
11437 static bool
11438 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11440 tree body;
11441 bool ctor_initializer_p;
11443 /* Begin the function body. */
11444 body = begin_function_body ();
11445 /* Parse the optional ctor-initializer. */
11446 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11447 /* Parse the function-body. */
11448 cp_parser_function_body (parser);
11449 /* Finish the function body. */
11450 finish_function_body (body);
11452 return ctor_initializer_p;
11455 /* Parse an initializer.
11457 initializer:
11458 = initializer-clause
11459 ( expression-list )
11461 Returns a expression representing the initializer. If no
11462 initializer is present, NULL_TREE is returned.
11464 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11465 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11466 set to FALSE if there is no initializer present. If there is an
11467 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11468 is set to true; otherwise it is set to false. */
11470 static tree
11471 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11472 bool* non_constant_p)
11474 cp_token *token;
11475 tree init;
11477 /* Peek at the next token. */
11478 token = cp_lexer_peek_token (parser->lexer);
11480 /* Let our caller know whether or not this initializer was
11481 parenthesized. */
11482 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11483 /* Assume that the initializer is constant. */
11484 *non_constant_p = false;
11486 if (token->type == CPP_EQ)
11488 /* Consume the `='. */
11489 cp_lexer_consume_token (parser->lexer);
11490 /* Parse the initializer-clause. */
11491 init = cp_parser_initializer_clause (parser, non_constant_p);
11493 else if (token->type == CPP_OPEN_PAREN)
11494 init = cp_parser_parenthesized_expression_list (parser, false,
11495 non_constant_p);
11496 else
11498 /* Anything else is an error. */
11499 cp_parser_error (parser, "expected initializer");
11500 init = error_mark_node;
11503 return init;
11506 /* Parse an initializer-clause.
11508 initializer-clause:
11509 assignment-expression
11510 { initializer-list , [opt] }
11513 Returns an expression representing the initializer.
11515 If the `assignment-expression' production is used the value
11516 returned is simply a representation for the expression.
11518 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11519 the elements of the initializer-list (or NULL_TREE, if the last
11520 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11521 NULL_TREE. There is no way to detect whether or not the optional
11522 trailing `,' was provided. NON_CONSTANT_P is as for
11523 cp_parser_initializer. */
11525 static tree
11526 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11528 tree initializer;
11530 /* If it is not a `{', then we are looking at an
11531 assignment-expression. */
11532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11534 initializer
11535 = cp_parser_constant_expression (parser,
11536 /*allow_non_constant_p=*/true,
11537 non_constant_p);
11538 if (!*non_constant_p)
11539 initializer = fold_non_dependent_expr (initializer);
11541 else
11543 /* Consume the `{' token. */
11544 cp_lexer_consume_token (parser->lexer);
11545 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11546 initializer = make_node (CONSTRUCTOR);
11547 /* If it's not a `}', then there is a non-trivial initializer. */
11548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11550 /* Parse the initializer list. */
11551 CONSTRUCTOR_ELTS (initializer)
11552 = cp_parser_initializer_list (parser, non_constant_p);
11553 /* A trailing `,' token is allowed. */
11554 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11555 cp_lexer_consume_token (parser->lexer);
11557 /* Now, there should be a trailing `}'. */
11558 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11561 return initializer;
11564 /* Parse an initializer-list.
11566 initializer-list:
11567 initializer-clause
11568 initializer-list , initializer-clause
11570 GNU Extension:
11572 initializer-list:
11573 identifier : initializer-clause
11574 initializer-list, identifier : initializer-clause
11576 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11577 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11578 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11579 as for cp_parser_initializer. */
11581 static tree
11582 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11584 tree initializers = NULL_TREE;
11586 /* Assume all of the expressions are constant. */
11587 *non_constant_p = false;
11589 /* Parse the rest of the list. */
11590 while (true)
11592 cp_token *token;
11593 tree identifier;
11594 tree initializer;
11595 bool clause_non_constant_p;
11597 /* If the next token is an identifier and the following one is a
11598 colon, we are looking at the GNU designated-initializer
11599 syntax. */
11600 if (cp_parser_allow_gnu_extensions_p (parser)
11601 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11602 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11604 /* Consume the identifier. */
11605 identifier = cp_lexer_consume_token (parser->lexer)->value;
11606 /* Consume the `:'. */
11607 cp_lexer_consume_token (parser->lexer);
11609 else
11610 identifier = NULL_TREE;
11612 /* Parse the initializer. */
11613 initializer = cp_parser_initializer_clause (parser,
11614 &clause_non_constant_p);
11615 /* If any clause is non-constant, so is the entire initializer. */
11616 if (clause_non_constant_p)
11617 *non_constant_p = true;
11618 /* Add it to the list. */
11619 initializers = tree_cons (identifier, initializer, initializers);
11621 /* If the next token is not a comma, we have reached the end of
11622 the list. */
11623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11624 break;
11626 /* Peek at the next token. */
11627 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11628 /* If the next token is a `}', then we're still done. An
11629 initializer-clause can have a trailing `,' after the
11630 initializer-list and before the closing `}'. */
11631 if (token->type == CPP_CLOSE_BRACE)
11632 break;
11634 /* Consume the `,' token. */
11635 cp_lexer_consume_token (parser->lexer);
11638 /* The initializers were built up in reverse order, so we need to
11639 reverse them now. */
11640 return nreverse (initializers);
11643 /* Classes [gram.class] */
11645 /* Parse a class-name.
11647 class-name:
11648 identifier
11649 template-id
11651 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11652 to indicate that names looked up in dependent types should be
11653 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11654 keyword has been used to indicate that the name that appears next
11655 is a template. TYPE_P is true iff the next name should be treated
11656 as class-name, even if it is declared to be some other kind of name
11657 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11658 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11659 being defined in a class-head.
11661 Returns the TYPE_DECL representing the class. */
11663 static tree
11664 cp_parser_class_name (cp_parser *parser,
11665 bool typename_keyword_p,
11666 bool template_keyword_p,
11667 bool type_p,
11668 bool check_dependency_p,
11669 bool class_head_p,
11670 bool is_declaration)
11672 tree decl;
11673 tree scope;
11674 bool typename_p;
11675 cp_token *token;
11677 /* All class-names start with an identifier. */
11678 token = cp_lexer_peek_token (parser->lexer);
11679 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11681 cp_parser_error (parser, "expected class-name");
11682 return error_mark_node;
11685 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11686 to a template-id, so we save it here. */
11687 scope = parser->scope;
11688 if (scope == error_mark_node)
11689 return error_mark_node;
11691 /* Any name names a type if we're following the `typename' keyword
11692 in a qualified name where the enclosing scope is type-dependent. */
11693 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11694 && dependent_type_p (scope));
11695 /* Handle the common case (an identifier, but not a template-id)
11696 efficiently. */
11697 if (token->type == CPP_NAME
11698 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11700 tree identifier;
11702 /* Look for the identifier. */
11703 identifier = cp_parser_identifier (parser);
11704 /* If the next token isn't an identifier, we are certainly not
11705 looking at a class-name. */
11706 if (identifier == error_mark_node)
11707 decl = error_mark_node;
11708 /* If we know this is a type-name, there's no need to look it
11709 up. */
11710 else if (typename_p)
11711 decl = identifier;
11712 else
11714 /* If the next token is a `::', then the name must be a type
11715 name.
11717 [basic.lookup.qual]
11719 During the lookup for a name preceding the :: scope
11720 resolution operator, object, function, and enumerator
11721 names are ignored. */
11722 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11723 type_p = true;
11724 /* Look up the name. */
11725 decl = cp_parser_lookup_name (parser, identifier,
11726 type_p,
11727 /*is_template=*/false,
11728 /*is_namespace=*/false,
11729 check_dependency_p);
11732 else
11734 /* Try a template-id. */
11735 decl = cp_parser_template_id (parser, template_keyword_p,
11736 check_dependency_p,
11737 is_declaration);
11738 if (decl == error_mark_node)
11739 return error_mark_node;
11742 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11744 /* If this is a typename, create a TYPENAME_TYPE. */
11745 if (typename_p && decl != error_mark_node)
11747 decl = make_typename_type (scope, decl, /*complain=*/1);
11748 if (decl != error_mark_node)
11749 decl = TYPE_NAME (decl);
11752 /* Check to see that it is really the name of a class. */
11753 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11754 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11755 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11756 /* Situations like this:
11758 template <typename T> struct A {
11759 typename T::template X<int>::I i;
11762 are problematic. Is `T::template X<int>' a class-name? The
11763 standard does not seem to be definitive, but there is no other
11764 valid interpretation of the following `::'. Therefore, those
11765 names are considered class-names. */
11766 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11767 else if (decl == error_mark_node
11768 || TREE_CODE (decl) != TYPE_DECL
11769 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11771 cp_parser_error (parser, "expected class-name");
11772 return error_mark_node;
11775 return decl;
11778 /* Parse a class-specifier.
11780 class-specifier:
11781 class-head { member-specification [opt] }
11783 Returns the TREE_TYPE representing the class. */
11785 static tree
11786 cp_parser_class_specifier (cp_parser* parser)
11788 cp_token *token;
11789 tree type;
11790 tree attributes;
11791 int has_trailing_semicolon;
11792 bool nested_name_specifier_p;
11793 unsigned saved_num_template_parameter_lists;
11794 bool pop_p = false;
11796 push_deferring_access_checks (dk_no_deferred);
11798 /* Parse the class-head. */
11799 type = cp_parser_class_head (parser,
11800 &nested_name_specifier_p,
11801 &attributes);
11802 /* If the class-head was a semantic disaster, skip the entire body
11803 of the class. */
11804 if (!type)
11806 cp_parser_skip_to_end_of_block_or_statement (parser);
11807 pop_deferring_access_checks ();
11808 return error_mark_node;
11811 /* Look for the `{'. */
11812 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11814 pop_deferring_access_checks ();
11815 return error_mark_node;
11818 /* Issue an error message if type-definitions are forbidden here. */
11819 cp_parser_check_type_definition (parser);
11820 /* Remember that we are defining one more class. */
11821 ++parser->num_classes_being_defined;
11822 /* Inside the class, surrounding template-parameter-lists do not
11823 apply. */
11824 saved_num_template_parameter_lists
11825 = parser->num_template_parameter_lists;
11826 parser->num_template_parameter_lists = 0;
11828 /* Start the class. */
11829 if (nested_name_specifier_p)
11830 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11831 type = begin_class_definition (type);
11832 if (type == error_mark_node)
11833 /* If the type is erroneous, skip the entire body of the class. */
11834 cp_parser_skip_to_closing_brace (parser);
11835 else
11836 /* Parse the member-specification. */
11837 cp_parser_member_specification_opt (parser);
11838 /* Look for the trailing `}'. */
11839 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11840 /* We get better error messages by noticing a common problem: a
11841 missing trailing `;'. */
11842 token = cp_lexer_peek_token (parser->lexer);
11843 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11844 /* Look for trailing attributes to apply to this class. */
11845 if (cp_parser_allow_gnu_extensions_p (parser))
11847 tree sub_attr = cp_parser_attributes_opt (parser);
11848 attributes = chainon (attributes, sub_attr);
11850 if (type != error_mark_node)
11851 type = finish_struct (type, attributes);
11852 if (pop_p)
11853 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11854 /* If this class is not itself within the scope of another class,
11855 then we need to parse the bodies of all of the queued function
11856 definitions. Note that the queued functions defined in a class
11857 are not always processed immediately following the
11858 class-specifier for that class. Consider:
11860 struct A {
11861 struct B { void f() { sizeof (A); } };
11864 If `f' were processed before the processing of `A' were
11865 completed, there would be no way to compute the size of `A'.
11866 Note that the nesting we are interested in here is lexical --
11867 not the semantic nesting given by TYPE_CONTEXT. In particular,
11868 for:
11870 struct A { struct B; };
11871 struct A::B { void f() { } };
11873 there is no need to delay the parsing of `A::B::f'. */
11874 if (--parser->num_classes_being_defined == 0)
11876 tree queue_entry;
11877 tree fn;
11879 /* In a first pass, parse default arguments to the functions.
11880 Then, in a second pass, parse the bodies of the functions.
11881 This two-phased approach handles cases like:
11883 struct S {
11884 void f() { g(); }
11885 void g(int i = 3);
11889 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11890 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11891 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11892 TREE_PURPOSE (parser->unparsed_functions_queues)
11893 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11895 fn = TREE_VALUE (queue_entry);
11896 /* Make sure that any template parameters are in scope. */
11897 maybe_begin_member_template_processing (fn);
11898 /* If there are default arguments that have not yet been processed,
11899 take care of them now. */
11900 cp_parser_late_parsing_default_args (parser, fn);
11901 /* Remove any template parameters from the symbol table. */
11902 maybe_end_member_template_processing ();
11904 /* Now parse the body of the functions. */
11905 for (TREE_VALUE (parser->unparsed_functions_queues)
11906 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11907 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11908 TREE_VALUE (parser->unparsed_functions_queues)
11909 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11911 /* Figure out which function we need to process. */
11912 fn = TREE_VALUE (queue_entry);
11914 /* A hack to prevent garbage collection. */
11915 function_depth++;
11917 /* Parse the function. */
11918 cp_parser_late_parsing_for_member (parser, fn);
11919 function_depth--;
11924 /* Put back any saved access checks. */
11925 pop_deferring_access_checks ();
11927 /* Restore the count of active template-parameter-lists. */
11928 parser->num_template_parameter_lists
11929 = saved_num_template_parameter_lists;
11931 return type;
11934 /* Parse a class-head.
11936 class-head:
11937 class-key identifier [opt] base-clause [opt]
11938 class-key nested-name-specifier identifier base-clause [opt]
11939 class-key nested-name-specifier [opt] template-id
11940 base-clause [opt]
11942 GNU Extensions:
11943 class-key attributes identifier [opt] base-clause [opt]
11944 class-key attributes nested-name-specifier identifier base-clause [opt]
11945 class-key attributes nested-name-specifier [opt] template-id
11946 base-clause [opt]
11948 Returns the TYPE of the indicated class. Sets
11949 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11950 involving a nested-name-specifier was used, and FALSE otherwise.
11952 Returns NULL_TREE if the class-head is syntactically valid, but
11953 semantically invalid in a way that means we should skip the entire
11954 body of the class. */
11956 static tree
11957 cp_parser_class_head (cp_parser* parser,
11958 bool* nested_name_specifier_p,
11959 tree *attributes_p)
11961 cp_token *token;
11962 tree nested_name_specifier;
11963 enum tag_types class_key;
11964 tree id = NULL_TREE;
11965 tree type = NULL_TREE;
11966 tree attributes;
11967 bool template_id_p = false;
11968 bool qualified_p = false;
11969 bool invalid_nested_name_p = false;
11970 bool invalid_explicit_specialization_p = false;
11971 bool pop_p = false;
11972 unsigned num_templates;
11974 /* Assume no nested-name-specifier will be present. */
11975 *nested_name_specifier_p = false;
11976 /* Assume no template parameter lists will be used in defining the
11977 type. */
11978 num_templates = 0;
11980 /* Look for the class-key. */
11981 class_key = cp_parser_class_key (parser);
11982 if (class_key == none_type)
11983 return error_mark_node;
11985 /* Parse the attributes. */
11986 attributes = cp_parser_attributes_opt (parser);
11988 /* If the next token is `::', that is invalid -- but sometimes
11989 people do try to write:
11991 struct ::S {};
11993 Handle this gracefully by accepting the extra qualifier, and then
11994 issuing an error about it later if this really is a
11995 class-head. If it turns out just to be an elaborated type
11996 specifier, remain silent. */
11997 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11998 qualified_p = true;
12000 push_deferring_access_checks (dk_no_check);
12002 /* Determine the name of the class. Begin by looking for an
12003 optional nested-name-specifier. */
12004 nested_name_specifier
12005 = cp_parser_nested_name_specifier_opt (parser,
12006 /*typename_keyword_p=*/false,
12007 /*check_dependency_p=*/false,
12008 /*type_p=*/false,
12009 /*is_declaration=*/false);
12010 /* If there was a nested-name-specifier, then there *must* be an
12011 identifier. */
12012 if (nested_name_specifier)
12014 /* Although the grammar says `identifier', it really means
12015 `class-name' or `template-name'. You are only allowed to
12016 define a class that has already been declared with this
12017 syntax.
12019 The proposed resolution for Core Issue 180 says that whever
12020 you see `class T::X' you should treat `X' as a type-name.
12022 It is OK to define an inaccessible class; for example:
12024 class A { class B; };
12025 class A::B {};
12027 We do not know if we will see a class-name, or a
12028 template-name. We look for a class-name first, in case the
12029 class-name is a template-id; if we looked for the
12030 template-name first we would stop after the template-name. */
12031 cp_parser_parse_tentatively (parser);
12032 type = cp_parser_class_name (parser,
12033 /*typename_keyword_p=*/false,
12034 /*template_keyword_p=*/false,
12035 /*type_p=*/true,
12036 /*check_dependency_p=*/false,
12037 /*class_head_p=*/true,
12038 /*is_declaration=*/false);
12039 /* If that didn't work, ignore the nested-name-specifier. */
12040 if (!cp_parser_parse_definitely (parser))
12042 invalid_nested_name_p = true;
12043 id = cp_parser_identifier (parser);
12044 if (id == error_mark_node)
12045 id = NULL_TREE;
12047 /* If we could not find a corresponding TYPE, treat this
12048 declaration like an unqualified declaration. */
12049 if (type == error_mark_node)
12050 nested_name_specifier = NULL_TREE;
12051 /* Otherwise, count the number of templates used in TYPE and its
12052 containing scopes. */
12053 else
12055 tree scope;
12057 for (scope = TREE_TYPE (type);
12058 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12059 scope = (TYPE_P (scope)
12060 ? TYPE_CONTEXT (scope)
12061 : DECL_CONTEXT (scope)))
12062 if (TYPE_P (scope)
12063 && CLASS_TYPE_P (scope)
12064 && CLASSTYPE_TEMPLATE_INFO (scope)
12065 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12066 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12067 ++num_templates;
12070 /* Otherwise, the identifier is optional. */
12071 else
12073 /* We don't know whether what comes next is a template-id,
12074 an identifier, or nothing at all. */
12075 cp_parser_parse_tentatively (parser);
12076 /* Check for a template-id. */
12077 id = cp_parser_template_id (parser,
12078 /*template_keyword_p=*/false,
12079 /*check_dependency_p=*/true,
12080 /*is_declaration=*/true);
12081 /* If that didn't work, it could still be an identifier. */
12082 if (!cp_parser_parse_definitely (parser))
12084 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12085 id = cp_parser_identifier (parser);
12086 else
12087 id = NULL_TREE;
12089 else
12091 template_id_p = true;
12092 ++num_templates;
12096 pop_deferring_access_checks ();
12098 cp_parser_check_for_invalid_template_id (parser, id);
12100 /* If it's not a `:' or a `{' then we can't really be looking at a
12101 class-head, since a class-head only appears as part of a
12102 class-specifier. We have to detect this situation before calling
12103 xref_tag, since that has irreversible side-effects. */
12104 if (!cp_parser_next_token_starts_class_definition_p (parser))
12106 cp_parser_error (parser, "expected `{' or `:'");
12107 return error_mark_node;
12110 /* At this point, we're going ahead with the class-specifier, even
12111 if some other problem occurs. */
12112 cp_parser_commit_to_tentative_parse (parser);
12113 /* Issue the error about the overly-qualified name now. */
12114 if (qualified_p)
12115 cp_parser_error (parser,
12116 "global qualification of class name is invalid");
12117 else if (invalid_nested_name_p)
12118 cp_parser_error (parser,
12119 "qualified name does not name a class");
12120 else if (nested_name_specifier)
12122 tree scope;
12123 /* Figure out in what scope the declaration is being placed. */
12124 scope = current_scope ();
12125 if (!scope)
12126 scope = current_namespace;
12127 /* If that scope does not contain the scope in which the
12128 class was originally declared, the program is invalid. */
12129 if (scope && !is_ancestor (scope, nested_name_specifier))
12131 error ("declaration of `%D' in `%D' which does not "
12132 "enclose `%D'", type, scope, nested_name_specifier);
12133 type = NULL_TREE;
12134 goto done;
12136 /* [dcl.meaning]
12138 A declarator-id shall not be qualified exception of the
12139 definition of a ... nested class outside of its class
12140 ... [or] a the definition or explicit instantiation of a
12141 class member of a namespace outside of its namespace. */
12142 if (scope == nested_name_specifier)
12144 pedwarn ("extra qualification ignored");
12145 nested_name_specifier = NULL_TREE;
12146 num_templates = 0;
12149 /* An explicit-specialization must be preceded by "template <>". If
12150 it is not, try to recover gracefully. */
12151 if (at_namespace_scope_p ()
12152 && parser->num_template_parameter_lists == 0
12153 && template_id_p)
12155 error ("an explicit specialization must be preceded by 'template <>'");
12156 invalid_explicit_specialization_p = true;
12157 /* Take the same action that would have been taken by
12158 cp_parser_explicit_specialization. */
12159 ++parser->num_template_parameter_lists;
12160 begin_specialization ();
12162 /* There must be no "return" statements between this point and the
12163 end of this function; set "type "to the correct return value and
12164 use "goto done;" to return. */
12165 /* Make sure that the right number of template parameters were
12166 present. */
12167 if (!cp_parser_check_template_parameters (parser, num_templates))
12169 /* If something went wrong, there is no point in even trying to
12170 process the class-definition. */
12171 type = NULL_TREE;
12172 goto done;
12175 /* Look up the type. */
12176 if (template_id_p)
12178 type = TREE_TYPE (id);
12179 maybe_process_partial_specialization (type);
12181 else if (!nested_name_specifier)
12183 /* If the class was unnamed, create a dummy name. */
12184 if (!id)
12185 id = make_anon_name ();
12186 type = xref_tag (class_key, id, /*globalize=*/false,
12187 parser->num_template_parameter_lists);
12189 else
12191 tree class_type;
12192 bool pop_p = false;
12194 /* Given:
12196 template <typename T> struct S { struct T };
12197 template <typename T> struct S<T>::T { };
12199 we will get a TYPENAME_TYPE when processing the definition of
12200 `S::T'. We need to resolve it to the actual type before we
12201 try to define it. */
12202 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12204 class_type = resolve_typename_type (TREE_TYPE (type),
12205 /*only_current_p=*/false);
12206 if (class_type != error_mark_node)
12207 type = TYPE_NAME (class_type);
12208 else
12210 cp_parser_error (parser, "could not resolve typename type");
12211 type = error_mark_node;
12215 maybe_process_partial_specialization (TREE_TYPE (type));
12216 class_type = current_class_type;
12217 /* Enter the scope indicated by the nested-name-specifier. */
12218 if (nested_name_specifier)
12219 pop_p = push_scope (nested_name_specifier);
12220 /* Get the canonical version of this type. */
12221 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12222 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12223 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12224 type = push_template_decl (type);
12225 type = TREE_TYPE (type);
12226 if (nested_name_specifier)
12228 *nested_name_specifier_p = true;
12229 if (pop_p)
12230 pop_scope (nested_name_specifier);
12233 /* Indicate whether this class was declared as a `class' or as a
12234 `struct'. */
12235 if (TREE_CODE (type) == RECORD_TYPE)
12236 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12237 cp_parser_check_class_key (class_key, type);
12239 /* Enter the scope containing the class; the names of base classes
12240 should be looked up in that context. For example, given:
12242 struct A { struct B {}; struct C; };
12243 struct A::C : B {};
12245 is valid. */
12246 if (nested_name_specifier)
12247 pop_p = push_scope (nested_name_specifier);
12248 /* Now, look for the base-clause. */
12249 token = cp_lexer_peek_token (parser->lexer);
12250 if (token->type == CPP_COLON)
12252 tree bases;
12254 /* Get the list of base-classes. */
12255 bases = cp_parser_base_clause (parser);
12256 /* Process them. */
12257 xref_basetypes (type, bases);
12259 /* Leave the scope given by the nested-name-specifier. We will
12260 enter the class scope itself while processing the members. */
12261 if (pop_p)
12262 pop_scope (nested_name_specifier);
12264 done:
12265 if (invalid_explicit_specialization_p)
12267 end_specialization ();
12268 --parser->num_template_parameter_lists;
12270 *attributes_p = attributes;
12271 return type;
12274 /* Parse a class-key.
12276 class-key:
12277 class
12278 struct
12279 union
12281 Returns the kind of class-key specified, or none_type to indicate
12282 error. */
12284 static enum tag_types
12285 cp_parser_class_key (cp_parser* parser)
12287 cp_token *token;
12288 enum tag_types tag_type;
12290 /* Look for the class-key. */
12291 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12292 if (!token)
12293 return none_type;
12295 /* Check to see if the TOKEN is a class-key. */
12296 tag_type = cp_parser_token_is_class_key (token);
12297 if (!tag_type)
12298 cp_parser_error (parser, "expected class-key");
12299 return tag_type;
12302 /* Parse an (optional) member-specification.
12304 member-specification:
12305 member-declaration member-specification [opt]
12306 access-specifier : member-specification [opt] */
12308 static void
12309 cp_parser_member_specification_opt (cp_parser* parser)
12311 while (true)
12313 cp_token *token;
12314 enum rid keyword;
12316 /* Peek at the next token. */
12317 token = cp_lexer_peek_token (parser->lexer);
12318 /* If it's a `}', or EOF then we've seen all the members. */
12319 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12320 break;
12322 /* See if this token is a keyword. */
12323 keyword = token->keyword;
12324 switch (keyword)
12326 case RID_PUBLIC:
12327 case RID_PROTECTED:
12328 case RID_PRIVATE:
12329 /* Consume the access-specifier. */
12330 cp_lexer_consume_token (parser->lexer);
12331 /* Remember which access-specifier is active. */
12332 current_access_specifier = token->value;
12333 /* Look for the `:'. */
12334 cp_parser_require (parser, CPP_COLON, "`:'");
12335 break;
12337 default:
12338 /* Otherwise, the next construction must be a
12339 member-declaration. */
12340 cp_parser_member_declaration (parser);
12345 /* Parse a member-declaration.
12347 member-declaration:
12348 decl-specifier-seq [opt] member-declarator-list [opt] ;
12349 function-definition ; [opt]
12350 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12351 using-declaration
12352 template-declaration
12354 member-declarator-list:
12355 member-declarator
12356 member-declarator-list , member-declarator
12358 member-declarator:
12359 declarator pure-specifier [opt]
12360 declarator constant-initializer [opt]
12361 identifier [opt] : constant-expression
12363 GNU Extensions:
12365 member-declaration:
12366 __extension__ member-declaration
12368 member-declarator:
12369 declarator attributes [opt] pure-specifier [opt]
12370 declarator attributes [opt] constant-initializer [opt]
12371 identifier [opt] attributes [opt] : constant-expression */
12373 static void
12374 cp_parser_member_declaration (cp_parser* parser)
12376 tree decl_specifiers;
12377 tree prefix_attributes;
12378 tree decl;
12379 int declares_class_or_enum;
12380 bool friend_p;
12381 cp_token *token;
12382 int saved_pedantic;
12384 /* Check for the `__extension__' keyword. */
12385 if (cp_parser_extension_opt (parser, &saved_pedantic))
12387 /* Recurse. */
12388 cp_parser_member_declaration (parser);
12389 /* Restore the old value of the PEDANTIC flag. */
12390 pedantic = saved_pedantic;
12392 return;
12395 /* Check for a template-declaration. */
12396 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12398 /* Parse the template-declaration. */
12399 cp_parser_template_declaration (parser, /*member_p=*/true);
12401 return;
12404 /* Check for a using-declaration. */
12405 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12407 /* Parse the using-declaration. */
12408 cp_parser_using_declaration (parser);
12410 return;
12413 /* Parse the decl-specifier-seq. */
12414 decl_specifiers
12415 = cp_parser_decl_specifier_seq (parser,
12416 CP_PARSER_FLAGS_OPTIONAL,
12417 &prefix_attributes,
12418 &declares_class_or_enum);
12419 /* Check for an invalid type-name. */
12420 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12421 return;
12422 /* If there is no declarator, then the decl-specifier-seq should
12423 specify a type. */
12424 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12426 /* If there was no decl-specifier-seq, and the next token is a
12427 `;', then we have something like:
12429 struct S { ; };
12431 [class.mem]
12433 Each member-declaration shall declare at least one member
12434 name of the class. */
12435 if (!decl_specifiers)
12437 if (pedantic)
12438 pedwarn ("extra semicolon");
12440 else
12442 tree type;
12444 /* See if this declaration is a friend. */
12445 friend_p = cp_parser_friend_p (decl_specifiers);
12446 /* If there were decl-specifiers, check to see if there was
12447 a class-declaration. */
12448 type = check_tag_decl (decl_specifiers);
12449 /* Nested classes have already been added to the class, but
12450 a `friend' needs to be explicitly registered. */
12451 if (friend_p)
12453 /* If the `friend' keyword was present, the friend must
12454 be introduced with a class-key. */
12455 if (!declares_class_or_enum)
12456 error ("a class-key must be used when declaring a friend");
12457 /* In this case:
12459 template <typename T> struct A {
12460 friend struct A<T>::B;
12463 A<T>::B will be represented by a TYPENAME_TYPE, and
12464 therefore not recognized by check_tag_decl. */
12465 if (!type)
12467 tree specifier;
12469 for (specifier = decl_specifiers;
12470 specifier;
12471 specifier = TREE_CHAIN (specifier))
12473 tree s = TREE_VALUE (specifier);
12475 if (TREE_CODE (s) == IDENTIFIER_NODE)
12476 get_global_value_if_present (s, &type);
12477 if (TREE_CODE (s) == TYPE_DECL)
12478 s = TREE_TYPE (s);
12479 if (TYPE_P (s))
12481 type = s;
12482 break;
12486 if (!type || !TYPE_P (type))
12487 error ("friend declaration does not name a class or "
12488 "function");
12489 else
12490 make_friend_class (current_class_type, type,
12491 /*complain=*/true);
12493 /* If there is no TYPE, an error message will already have
12494 been issued. */
12495 else if (!type)
12497 /* An anonymous aggregate has to be handled specially; such
12498 a declaration really declares a data member (with a
12499 particular type), as opposed to a nested class. */
12500 else if (ANON_AGGR_TYPE_P (type))
12502 /* Remove constructors and such from TYPE, now that we
12503 know it is an anonymous aggregate. */
12504 fixup_anonymous_aggr (type);
12505 /* And make the corresponding data member. */
12506 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12507 /* Add it to the class. */
12508 finish_member_declaration (decl);
12510 else
12511 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12514 else
12516 /* See if these declarations will be friends. */
12517 friend_p = cp_parser_friend_p (decl_specifiers);
12519 /* Keep going until we hit the `;' at the end of the
12520 declaration. */
12521 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12523 tree attributes = NULL_TREE;
12524 tree first_attribute;
12526 /* Peek at the next token. */
12527 token = cp_lexer_peek_token (parser->lexer);
12529 /* Check for a bitfield declaration. */
12530 if (token->type == CPP_COLON
12531 || (token->type == CPP_NAME
12532 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12533 == CPP_COLON))
12535 tree identifier;
12536 tree width;
12538 /* Get the name of the bitfield. Note that we cannot just
12539 check TOKEN here because it may have been invalidated by
12540 the call to cp_lexer_peek_nth_token above. */
12541 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12542 identifier = cp_parser_identifier (parser);
12543 else
12544 identifier = NULL_TREE;
12546 /* Consume the `:' token. */
12547 cp_lexer_consume_token (parser->lexer);
12548 /* Get the width of the bitfield. */
12549 width
12550 = cp_parser_constant_expression (parser,
12551 /*allow_non_constant=*/false,
12552 NULL);
12554 /* Look for attributes that apply to the bitfield. */
12555 attributes = cp_parser_attributes_opt (parser);
12556 /* Remember which attributes are prefix attributes and
12557 which are not. */
12558 first_attribute = attributes;
12559 /* Combine the attributes. */
12560 attributes = chainon (prefix_attributes, attributes);
12562 /* Create the bitfield declaration. */
12563 decl = grokbitfield (identifier,
12564 decl_specifiers,
12565 width);
12566 /* Apply the attributes. */
12567 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12569 else
12571 tree declarator;
12572 tree initializer;
12573 tree asm_specification;
12574 int ctor_dtor_or_conv_p;
12576 /* Parse the declarator. */
12577 declarator
12578 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12579 &ctor_dtor_or_conv_p,
12580 /*parenthesized_p=*/NULL);
12582 /* If something went wrong parsing the declarator, make sure
12583 that we at least consume some tokens. */
12584 if (declarator == error_mark_node)
12586 /* Skip to the end of the statement. */
12587 cp_parser_skip_to_end_of_statement (parser);
12588 /* If the next token is not a semicolon, that is
12589 probably because we just skipped over the body of
12590 a function. So, we consume a semicolon if
12591 present, but do not issue an error message if it
12592 is not present. */
12593 if (cp_lexer_next_token_is (parser->lexer,
12594 CPP_SEMICOLON))
12595 cp_lexer_consume_token (parser->lexer);
12596 return;
12599 cp_parser_check_for_definition_in_return_type
12600 (declarator, declares_class_or_enum);
12602 /* Look for an asm-specification. */
12603 asm_specification = cp_parser_asm_specification_opt (parser);
12604 /* Look for attributes that apply to the declaration. */
12605 attributes = cp_parser_attributes_opt (parser);
12606 /* Remember which attributes are prefix attributes and
12607 which are not. */
12608 first_attribute = attributes;
12609 /* Combine the attributes. */
12610 attributes = chainon (prefix_attributes, attributes);
12612 /* If it's an `=', then we have a constant-initializer or a
12613 pure-specifier. It is not correct to parse the
12614 initializer before registering the member declaration
12615 since the member declaration should be in scope while
12616 its initializer is processed. However, the rest of the
12617 front end does not yet provide an interface that allows
12618 us to handle this correctly. */
12619 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12621 /* In [class.mem]:
12623 A pure-specifier shall be used only in the declaration of
12624 a virtual function.
12626 A member-declarator can contain a constant-initializer
12627 only if it declares a static member of integral or
12628 enumeration type.
12630 Therefore, if the DECLARATOR is for a function, we look
12631 for a pure-specifier; otherwise, we look for a
12632 constant-initializer. When we call `grokfield', it will
12633 perform more stringent semantics checks. */
12634 if (TREE_CODE (declarator) == CALL_EXPR)
12635 initializer = cp_parser_pure_specifier (parser);
12636 else
12637 /* Parse the initializer. */
12638 initializer = cp_parser_constant_initializer (parser);
12640 /* Otherwise, there is no initializer. */
12641 else
12642 initializer = NULL_TREE;
12644 /* See if we are probably looking at a function
12645 definition. We are certainly not looking at at a
12646 member-declarator. Calling `grokfield' has
12647 side-effects, so we must not do it unless we are sure
12648 that we are looking at a member-declarator. */
12649 if (cp_parser_token_starts_function_definition_p
12650 (cp_lexer_peek_token (parser->lexer)))
12652 /* The grammar does not allow a pure-specifier to be
12653 used when a member function is defined. (It is
12654 possible that this fact is an oversight in the
12655 standard, since a pure function may be defined
12656 outside of the class-specifier. */
12657 if (initializer)
12658 error ("pure-specifier on function-definition");
12659 decl = cp_parser_save_member_function_body (parser,
12660 decl_specifiers,
12661 declarator,
12662 attributes);
12663 /* If the member was not a friend, declare it here. */
12664 if (!friend_p)
12665 finish_member_declaration (decl);
12666 /* Peek at the next token. */
12667 token = cp_lexer_peek_token (parser->lexer);
12668 /* If the next token is a semicolon, consume it. */
12669 if (token->type == CPP_SEMICOLON)
12670 cp_lexer_consume_token (parser->lexer);
12671 return;
12673 else
12675 /* Create the declaration. */
12676 decl = grokfield (declarator, decl_specifiers,
12677 initializer, asm_specification,
12678 attributes);
12679 /* Any initialization must have been from a
12680 constant-expression. */
12681 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12682 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12686 /* Reset PREFIX_ATTRIBUTES. */
12687 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12688 attributes = TREE_CHAIN (attributes);
12689 if (attributes)
12690 TREE_CHAIN (attributes) = NULL_TREE;
12692 /* If there is any qualification still in effect, clear it
12693 now; we will be starting fresh with the next declarator. */
12694 parser->scope = NULL_TREE;
12695 parser->qualifying_scope = NULL_TREE;
12696 parser->object_scope = NULL_TREE;
12697 /* If it's a `,', then there are more declarators. */
12698 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12699 cp_lexer_consume_token (parser->lexer);
12700 /* If the next token isn't a `;', then we have a parse error. */
12701 else if (cp_lexer_next_token_is_not (parser->lexer,
12702 CPP_SEMICOLON))
12704 cp_parser_error (parser, "expected `;'");
12705 /* Skip tokens until we find a `;'. */
12706 cp_parser_skip_to_end_of_statement (parser);
12708 break;
12711 if (decl)
12713 /* Add DECL to the list of members. */
12714 if (!friend_p)
12715 finish_member_declaration (decl);
12717 if (TREE_CODE (decl) == FUNCTION_DECL)
12718 cp_parser_save_default_args (parser, decl);
12723 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12726 /* Parse a pure-specifier.
12728 pure-specifier:
12731 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12732 Otherwise, ERROR_MARK_NODE is returned. */
12734 static tree
12735 cp_parser_pure_specifier (cp_parser* parser)
12737 cp_token *token;
12739 /* Look for the `=' token. */
12740 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12741 return error_mark_node;
12742 /* Look for the `0' token. */
12743 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12744 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12745 to get information from the lexer about how the number was
12746 spelled in order to fix this problem. */
12747 if (!token || !integer_zerop (token->value))
12748 return error_mark_node;
12750 return integer_zero_node;
12753 /* Parse a constant-initializer.
12755 constant-initializer:
12756 = constant-expression
12758 Returns a representation of the constant-expression. */
12760 static tree
12761 cp_parser_constant_initializer (cp_parser* parser)
12763 /* Look for the `=' token. */
12764 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12765 return error_mark_node;
12767 /* It is invalid to write:
12769 struct S { static const int i = { 7 }; };
12772 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12774 cp_parser_error (parser,
12775 "a brace-enclosed initializer is not allowed here");
12776 /* Consume the opening brace. */
12777 cp_lexer_consume_token (parser->lexer);
12778 /* Skip the initializer. */
12779 cp_parser_skip_to_closing_brace (parser);
12780 /* Look for the trailing `}'. */
12781 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12783 return error_mark_node;
12786 return cp_parser_constant_expression (parser,
12787 /*allow_non_constant=*/false,
12788 NULL);
12791 /* Derived classes [gram.class.derived] */
12793 /* Parse a base-clause.
12795 base-clause:
12796 : base-specifier-list
12798 base-specifier-list:
12799 base-specifier
12800 base-specifier-list , base-specifier
12802 Returns a TREE_LIST representing the base-classes, in the order in
12803 which they were declared. The representation of each node is as
12804 described by cp_parser_base_specifier.
12806 In the case that no bases are specified, this function will return
12807 NULL_TREE, not ERROR_MARK_NODE. */
12809 static tree
12810 cp_parser_base_clause (cp_parser* parser)
12812 tree bases = NULL_TREE;
12814 /* Look for the `:' that begins the list. */
12815 cp_parser_require (parser, CPP_COLON, "`:'");
12817 /* Scan the base-specifier-list. */
12818 while (true)
12820 cp_token *token;
12821 tree base;
12823 /* Look for the base-specifier. */
12824 base = cp_parser_base_specifier (parser);
12825 /* Add BASE to the front of the list. */
12826 if (base != error_mark_node)
12828 TREE_CHAIN (base) = bases;
12829 bases = base;
12831 /* Peek at the next token. */
12832 token = cp_lexer_peek_token (parser->lexer);
12833 /* If it's not a comma, then the list is complete. */
12834 if (token->type != CPP_COMMA)
12835 break;
12836 /* Consume the `,'. */
12837 cp_lexer_consume_token (parser->lexer);
12840 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12841 base class had a qualified name. However, the next name that
12842 appears is certainly not qualified. */
12843 parser->scope = NULL_TREE;
12844 parser->qualifying_scope = NULL_TREE;
12845 parser->object_scope = NULL_TREE;
12847 return nreverse (bases);
12850 /* Parse a base-specifier.
12852 base-specifier:
12853 :: [opt] nested-name-specifier [opt] class-name
12854 virtual access-specifier [opt] :: [opt] nested-name-specifier
12855 [opt] class-name
12856 access-specifier virtual [opt] :: [opt] nested-name-specifier
12857 [opt] class-name
12859 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12860 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12861 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12862 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12864 static tree
12865 cp_parser_base_specifier (cp_parser* parser)
12867 cp_token *token;
12868 bool done = false;
12869 bool virtual_p = false;
12870 bool duplicate_virtual_error_issued_p = false;
12871 bool duplicate_access_error_issued_p = false;
12872 bool class_scope_p, template_p;
12873 tree access = access_default_node;
12874 tree type;
12876 /* Process the optional `virtual' and `access-specifier'. */
12877 while (!done)
12879 /* Peek at the next token. */
12880 token = cp_lexer_peek_token (parser->lexer);
12881 /* Process `virtual'. */
12882 switch (token->keyword)
12884 case RID_VIRTUAL:
12885 /* If `virtual' appears more than once, issue an error. */
12886 if (virtual_p && !duplicate_virtual_error_issued_p)
12888 cp_parser_error (parser,
12889 "`virtual' specified more than once in base-specified");
12890 duplicate_virtual_error_issued_p = true;
12893 virtual_p = true;
12895 /* Consume the `virtual' token. */
12896 cp_lexer_consume_token (parser->lexer);
12898 break;
12900 case RID_PUBLIC:
12901 case RID_PROTECTED:
12902 case RID_PRIVATE:
12903 /* If more than one access specifier appears, issue an
12904 error. */
12905 if (access != access_default_node
12906 && !duplicate_access_error_issued_p)
12908 cp_parser_error (parser,
12909 "more than one access specifier in base-specified");
12910 duplicate_access_error_issued_p = true;
12913 access = ridpointers[(int) token->keyword];
12915 /* Consume the access-specifier. */
12916 cp_lexer_consume_token (parser->lexer);
12918 break;
12920 default:
12921 done = true;
12922 break;
12925 /* It is not uncommon to see programs mechanically, erroneously, use
12926 the 'typename' keyword to denote (dependent) qualified types
12927 as base classes. */
12928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12930 if (!processing_template_decl)
12931 error ("keyword `typename' not allowed outside of templates");
12932 else
12933 error ("keyword `typename' not allowed in this context "
12934 "(the base class is implicitly a type)");
12935 cp_lexer_consume_token (parser->lexer);
12938 /* Look for the optional `::' operator. */
12939 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12940 /* Look for the nested-name-specifier. The simplest way to
12941 implement:
12943 [temp.res]
12945 The keyword `typename' is not permitted in a base-specifier or
12946 mem-initializer; in these contexts a qualified name that
12947 depends on a template-parameter is implicitly assumed to be a
12948 type name.
12950 is to pretend that we have seen the `typename' keyword at this
12951 point. */
12952 cp_parser_nested_name_specifier_opt (parser,
12953 /*typename_keyword_p=*/true,
12954 /*check_dependency_p=*/true,
12955 /*type_p=*/true,
12956 /*is_declaration=*/true);
12957 /* If the base class is given by a qualified name, assume that names
12958 we see are type names or templates, as appropriate. */
12959 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12960 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12962 /* Finally, look for the class-name. */
12963 type = cp_parser_class_name (parser,
12964 class_scope_p,
12965 template_p,
12966 /*type_p=*/true,
12967 /*check_dependency_p=*/true,
12968 /*class_head_p=*/false,
12969 /*is_declaration=*/true);
12971 if (type == error_mark_node)
12972 return error_mark_node;
12974 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12977 /* Exception handling [gram.exception] */
12979 /* Parse an (optional) exception-specification.
12981 exception-specification:
12982 throw ( type-id-list [opt] )
12984 Returns a TREE_LIST representing the exception-specification. The
12985 TREE_VALUE of each node is a type. */
12987 static tree
12988 cp_parser_exception_specification_opt (cp_parser* parser)
12990 cp_token *token;
12991 tree type_id_list;
12993 /* Peek at the next token. */
12994 token = cp_lexer_peek_token (parser->lexer);
12995 /* If it's not `throw', then there's no exception-specification. */
12996 if (!cp_parser_is_keyword (token, RID_THROW))
12997 return NULL_TREE;
12999 /* Consume the `throw'. */
13000 cp_lexer_consume_token (parser->lexer);
13002 /* Look for the `('. */
13003 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13005 /* Peek at the next token. */
13006 token = cp_lexer_peek_token (parser->lexer);
13007 /* If it's not a `)', then there is a type-id-list. */
13008 if (token->type != CPP_CLOSE_PAREN)
13010 const char *saved_message;
13012 /* Types may not be defined in an exception-specification. */
13013 saved_message = parser->type_definition_forbidden_message;
13014 parser->type_definition_forbidden_message
13015 = "types may not be defined in an exception-specification";
13016 /* Parse the type-id-list. */
13017 type_id_list = cp_parser_type_id_list (parser);
13018 /* Restore the saved message. */
13019 parser->type_definition_forbidden_message = saved_message;
13021 else
13022 type_id_list = empty_except_spec;
13024 /* Look for the `)'. */
13025 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13027 return type_id_list;
13030 /* Parse an (optional) type-id-list.
13032 type-id-list:
13033 type-id
13034 type-id-list , type-id
13036 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13037 in the order that the types were presented. */
13039 static tree
13040 cp_parser_type_id_list (cp_parser* parser)
13042 tree types = NULL_TREE;
13044 while (true)
13046 cp_token *token;
13047 tree type;
13049 /* Get the next type-id. */
13050 type = cp_parser_type_id (parser);
13051 /* Add it to the list. */
13052 types = add_exception_specifier (types, type, /*complain=*/1);
13053 /* Peek at the next token. */
13054 token = cp_lexer_peek_token (parser->lexer);
13055 /* If it is not a `,', we are done. */
13056 if (token->type != CPP_COMMA)
13057 break;
13058 /* Consume the `,'. */
13059 cp_lexer_consume_token (parser->lexer);
13062 return nreverse (types);
13065 /* Parse a try-block.
13067 try-block:
13068 try compound-statement handler-seq */
13070 static tree
13071 cp_parser_try_block (cp_parser* parser)
13073 tree try_block;
13075 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13076 try_block = begin_try_block ();
13077 cp_parser_compound_statement (parser, false);
13078 finish_try_block (try_block);
13079 cp_parser_handler_seq (parser);
13080 finish_handler_sequence (try_block);
13082 return try_block;
13085 /* Parse a function-try-block.
13087 function-try-block:
13088 try ctor-initializer [opt] function-body handler-seq */
13090 static bool
13091 cp_parser_function_try_block (cp_parser* parser)
13093 tree try_block;
13094 bool ctor_initializer_p;
13096 /* Look for the `try' keyword. */
13097 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13098 return false;
13099 /* Let the rest of the front-end know where we are. */
13100 try_block = begin_function_try_block ();
13101 /* Parse the function-body. */
13102 ctor_initializer_p
13103 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13104 /* We're done with the `try' part. */
13105 finish_function_try_block (try_block);
13106 /* Parse the handlers. */
13107 cp_parser_handler_seq (parser);
13108 /* We're done with the handlers. */
13109 finish_function_handler_sequence (try_block);
13111 return ctor_initializer_p;
13114 /* Parse a handler-seq.
13116 handler-seq:
13117 handler handler-seq [opt] */
13119 static void
13120 cp_parser_handler_seq (cp_parser* parser)
13122 while (true)
13124 cp_token *token;
13126 /* Parse the handler. */
13127 cp_parser_handler (parser);
13128 /* Peek at the next token. */
13129 token = cp_lexer_peek_token (parser->lexer);
13130 /* If it's not `catch' then there are no more handlers. */
13131 if (!cp_parser_is_keyword (token, RID_CATCH))
13132 break;
13136 /* Parse a handler.
13138 handler:
13139 catch ( exception-declaration ) compound-statement */
13141 static void
13142 cp_parser_handler (cp_parser* parser)
13144 tree handler;
13145 tree declaration;
13147 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13148 handler = begin_handler ();
13149 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13150 declaration = cp_parser_exception_declaration (parser);
13151 finish_handler_parms (declaration, handler);
13152 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13153 cp_parser_compound_statement (parser, false);
13154 finish_handler (handler);
13157 /* Parse an exception-declaration.
13159 exception-declaration:
13160 type-specifier-seq declarator
13161 type-specifier-seq abstract-declarator
13162 type-specifier-seq
13165 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13166 ellipsis variant is used. */
13168 static tree
13169 cp_parser_exception_declaration (cp_parser* parser)
13171 tree type_specifiers;
13172 tree declarator;
13173 const char *saved_message;
13175 /* If it's an ellipsis, it's easy to handle. */
13176 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13178 /* Consume the `...' token. */
13179 cp_lexer_consume_token (parser->lexer);
13180 return NULL_TREE;
13183 /* Types may not be defined in exception-declarations. */
13184 saved_message = parser->type_definition_forbidden_message;
13185 parser->type_definition_forbidden_message
13186 = "types may not be defined in exception-declarations";
13188 /* Parse the type-specifier-seq. */
13189 type_specifiers = cp_parser_type_specifier_seq (parser);
13190 /* If it's a `)', then there is no declarator. */
13191 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13192 declarator = NULL_TREE;
13193 else
13194 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13195 /*ctor_dtor_or_conv_p=*/NULL,
13196 /*parenthesized_p=*/NULL);
13198 /* Restore the saved message. */
13199 parser->type_definition_forbidden_message = saved_message;
13201 return start_handler_parms (type_specifiers, declarator);
13204 /* Parse a throw-expression.
13206 throw-expression:
13207 throw assignment-expression [opt]
13209 Returns a THROW_EXPR representing the throw-expression. */
13211 static tree
13212 cp_parser_throw_expression (cp_parser* parser)
13214 tree expression;
13215 cp_token* token;
13217 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13218 token = cp_lexer_peek_token (parser->lexer);
13219 /* Figure out whether or not there is an assignment-expression
13220 following the "throw" keyword. */
13221 if (token->type == CPP_COMMA
13222 || token->type == CPP_SEMICOLON
13223 || token->type == CPP_CLOSE_PAREN
13224 || token->type == CPP_CLOSE_SQUARE
13225 || token->type == CPP_CLOSE_BRACE
13226 || token->type == CPP_COLON)
13227 expression = NULL_TREE;
13228 else
13229 expression = cp_parser_assignment_expression (parser);
13231 return build_throw (expression);
13234 /* GNU Extensions */
13236 /* Parse an (optional) asm-specification.
13238 asm-specification:
13239 asm ( string-literal )
13241 If the asm-specification is present, returns a STRING_CST
13242 corresponding to the string-literal. Otherwise, returns
13243 NULL_TREE. */
13245 static tree
13246 cp_parser_asm_specification_opt (cp_parser* parser)
13248 cp_token *token;
13249 tree asm_specification;
13251 /* Peek at the next token. */
13252 token = cp_lexer_peek_token (parser->lexer);
13253 /* If the next token isn't the `asm' keyword, then there's no
13254 asm-specification. */
13255 if (!cp_parser_is_keyword (token, RID_ASM))
13256 return NULL_TREE;
13258 /* Consume the `asm' token. */
13259 cp_lexer_consume_token (parser->lexer);
13260 /* Look for the `('. */
13261 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13263 /* Look for the string-literal. */
13264 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13265 if (token)
13266 asm_specification = token->value;
13267 else
13268 asm_specification = NULL_TREE;
13270 /* Look for the `)'. */
13271 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13273 return asm_specification;
13276 /* Parse an asm-operand-list.
13278 asm-operand-list:
13279 asm-operand
13280 asm-operand-list , asm-operand
13282 asm-operand:
13283 string-literal ( expression )
13284 [ string-literal ] string-literal ( expression )
13286 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13287 each node is the expression. The TREE_PURPOSE is itself a
13288 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13289 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13290 is a STRING_CST for the string literal before the parenthesis. */
13292 static tree
13293 cp_parser_asm_operand_list (cp_parser* parser)
13295 tree asm_operands = NULL_TREE;
13297 while (true)
13299 tree string_literal;
13300 tree expression;
13301 tree name;
13302 cp_token *token;
13304 c_lex_string_translate = false;
13306 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13308 /* Consume the `[' token. */
13309 cp_lexer_consume_token (parser->lexer);
13310 /* Read the operand name. */
13311 name = cp_parser_identifier (parser);
13312 if (name != error_mark_node)
13313 name = build_string (IDENTIFIER_LENGTH (name),
13314 IDENTIFIER_POINTER (name));
13315 /* Look for the closing `]'. */
13316 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13318 else
13319 name = NULL_TREE;
13320 /* Look for the string-literal. */
13321 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13322 string_literal = token ? token->value : error_mark_node;
13323 c_lex_string_translate = true;
13324 /* Look for the `('. */
13325 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13326 /* Parse the expression. */
13327 expression = cp_parser_expression (parser);
13328 /* Look for the `)'. */
13329 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13330 c_lex_string_translate = false;
13331 /* Add this operand to the list. */
13332 asm_operands = tree_cons (build_tree_list (name, string_literal),
13333 expression,
13334 asm_operands);
13335 /* If the next token is not a `,', there are no more
13336 operands. */
13337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13338 break;
13339 /* Consume the `,'. */
13340 cp_lexer_consume_token (parser->lexer);
13343 return nreverse (asm_operands);
13346 /* Parse an asm-clobber-list.
13348 asm-clobber-list:
13349 string-literal
13350 asm-clobber-list , string-literal
13352 Returns a TREE_LIST, indicating the clobbers in the order that they
13353 appeared. The TREE_VALUE of each node is a STRING_CST. */
13355 static tree
13356 cp_parser_asm_clobber_list (cp_parser* parser)
13358 tree clobbers = NULL_TREE;
13360 while (true)
13362 cp_token *token;
13363 tree string_literal;
13365 /* Look for the string literal. */
13366 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13367 string_literal = token ? token->value : error_mark_node;
13368 /* Add it to the list. */
13369 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13370 /* If the next token is not a `,', then the list is
13371 complete. */
13372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13373 break;
13374 /* Consume the `,' token. */
13375 cp_lexer_consume_token (parser->lexer);
13378 return clobbers;
13381 /* Parse an (optional) series of attributes.
13383 attributes:
13384 attributes attribute
13386 attribute:
13387 __attribute__ (( attribute-list [opt] ))
13389 The return value is as for cp_parser_attribute_list. */
13391 static tree
13392 cp_parser_attributes_opt (cp_parser* parser)
13394 tree attributes = NULL_TREE;
13396 while (true)
13398 cp_token *token;
13399 tree attribute_list;
13401 /* Peek at the next token. */
13402 token = cp_lexer_peek_token (parser->lexer);
13403 /* If it's not `__attribute__', then we're done. */
13404 if (token->keyword != RID_ATTRIBUTE)
13405 break;
13407 /* Consume the `__attribute__' keyword. */
13408 cp_lexer_consume_token (parser->lexer);
13409 /* Look for the two `(' tokens. */
13410 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13411 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13413 /* Peek at the next token. */
13414 token = cp_lexer_peek_token (parser->lexer);
13415 if (token->type != CPP_CLOSE_PAREN)
13416 /* Parse the attribute-list. */
13417 attribute_list = cp_parser_attribute_list (parser);
13418 else
13419 /* If the next token is a `)', then there is no attribute
13420 list. */
13421 attribute_list = NULL;
13423 /* Look for the two `)' tokens. */
13424 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13425 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13427 /* Add these new attributes to the list. */
13428 attributes = chainon (attributes, attribute_list);
13431 return attributes;
13434 /* Parse an attribute-list.
13436 attribute-list:
13437 attribute
13438 attribute-list , attribute
13440 attribute:
13441 identifier
13442 identifier ( identifier )
13443 identifier ( identifier , expression-list )
13444 identifier ( expression-list )
13446 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13447 TREE_PURPOSE of each node is the identifier indicating which
13448 attribute is in use. The TREE_VALUE represents the arguments, if
13449 any. */
13451 static tree
13452 cp_parser_attribute_list (cp_parser* parser)
13454 tree attribute_list = NULL_TREE;
13456 c_lex_string_translate = false;
13457 while (true)
13459 cp_token *token;
13460 tree identifier;
13461 tree attribute;
13463 /* Look for the identifier. We also allow keywords here; for
13464 example `__attribute__ ((const))' is legal. */
13465 token = cp_lexer_peek_token (parser->lexer);
13466 if (token->type != CPP_NAME
13467 && token->type != CPP_KEYWORD)
13468 return error_mark_node;
13469 /* Consume the token. */
13470 token = cp_lexer_consume_token (parser->lexer);
13472 /* Save away the identifier that indicates which attribute this is. */
13473 identifier = token->value;
13474 attribute = build_tree_list (identifier, NULL_TREE);
13476 /* Peek at the next token. */
13477 token = cp_lexer_peek_token (parser->lexer);
13478 /* If it's an `(', then parse the attribute arguments. */
13479 if (token->type == CPP_OPEN_PAREN)
13481 tree arguments;
13483 arguments = (cp_parser_parenthesized_expression_list
13484 (parser, true, /*non_constant_p=*/NULL));
13485 /* Save the identifier and arguments away. */
13486 TREE_VALUE (attribute) = arguments;
13489 /* Add this attribute to the list. */
13490 TREE_CHAIN (attribute) = attribute_list;
13491 attribute_list = attribute;
13493 /* Now, look for more attributes. */
13494 token = cp_lexer_peek_token (parser->lexer);
13495 /* If the next token isn't a `,', we're done. */
13496 if (token->type != CPP_COMMA)
13497 break;
13499 /* Consume the comma and keep going. */
13500 cp_lexer_consume_token (parser->lexer);
13502 c_lex_string_translate = true;
13504 /* We built up the list in reverse order. */
13505 return nreverse (attribute_list);
13508 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13509 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13510 current value of the PEDANTIC flag, regardless of whether or not
13511 the `__extension__' keyword is present. The caller is responsible
13512 for restoring the value of the PEDANTIC flag. */
13514 static bool
13515 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13517 /* Save the old value of the PEDANTIC flag. */
13518 *saved_pedantic = pedantic;
13520 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13522 /* Consume the `__extension__' token. */
13523 cp_lexer_consume_token (parser->lexer);
13524 /* We're not being pedantic while the `__extension__' keyword is
13525 in effect. */
13526 pedantic = 0;
13528 return true;
13531 return false;
13534 /* Parse a label declaration.
13536 label-declaration:
13537 __label__ label-declarator-seq ;
13539 label-declarator-seq:
13540 identifier , label-declarator-seq
13541 identifier */
13543 static void
13544 cp_parser_label_declaration (cp_parser* parser)
13546 /* Look for the `__label__' keyword. */
13547 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13549 while (true)
13551 tree identifier;
13553 /* Look for an identifier. */
13554 identifier = cp_parser_identifier (parser);
13555 /* Declare it as a lobel. */
13556 finish_label_decl (identifier);
13557 /* If the next token is a `;', stop. */
13558 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13559 break;
13560 /* Look for the `,' separating the label declarations. */
13561 cp_parser_require (parser, CPP_COMMA, "`,'");
13564 /* Look for the final `;'. */
13565 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13568 /* Support Functions */
13570 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13571 NAME should have one of the representations used for an
13572 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13573 is returned. If PARSER->SCOPE is a dependent type, then a
13574 SCOPE_REF is returned.
13576 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13577 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13578 was formed. Abstractly, such entities should not be passed to this
13579 function, because they do not need to be looked up, but it is
13580 simpler to check for this special case here, rather than at the
13581 call-sites.
13583 In cases not explicitly covered above, this function returns a
13584 DECL, OVERLOAD, or baselink representing the result of the lookup.
13585 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13586 is returned.
13588 If IS_TYPE is TRUE, bindings that do not refer to types are
13589 ignored.
13591 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13592 ignored.
13594 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13595 are ignored.
13597 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13598 types. */
13600 static tree
13601 cp_parser_lookup_name (cp_parser *parser, tree name,
13602 bool is_type, bool is_template, bool is_namespace,
13603 bool check_dependency)
13605 tree decl;
13606 tree object_type = parser->context->object_type;
13608 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13609 no longer valid. Note that if we are parsing tentatively, and
13610 the parse fails, OBJECT_TYPE will be automatically restored. */
13611 parser->context->object_type = NULL_TREE;
13613 if (name == error_mark_node)
13614 return error_mark_node;
13616 /* A template-id has already been resolved; there is no lookup to
13617 do. */
13618 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13619 return name;
13620 if (BASELINK_P (name))
13622 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13623 == TEMPLATE_ID_EXPR),
13624 20020909);
13625 return name;
13628 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13629 it should already have been checked to make sure that the name
13630 used matches the type being destroyed. */
13631 if (TREE_CODE (name) == BIT_NOT_EXPR)
13633 tree type;
13635 /* Figure out to which type this destructor applies. */
13636 if (parser->scope)
13637 type = parser->scope;
13638 else if (object_type)
13639 type = object_type;
13640 else
13641 type = current_class_type;
13642 /* If that's not a class type, there is no destructor. */
13643 if (!type || !CLASS_TYPE_P (type))
13644 return error_mark_node;
13645 if (!CLASSTYPE_DESTRUCTORS (type))
13646 return error_mark_node;
13647 /* If it was a class type, return the destructor. */
13648 return CLASSTYPE_DESTRUCTORS (type);
13651 /* By this point, the NAME should be an ordinary identifier. If
13652 the id-expression was a qualified name, the qualifying scope is
13653 stored in PARSER->SCOPE at this point. */
13654 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13655 20000619);
13657 /* Perform the lookup. */
13658 if (parser->scope)
13660 bool dependent_p;
13662 if (parser->scope == error_mark_node)
13663 return error_mark_node;
13665 /* If the SCOPE is dependent, the lookup must be deferred until
13666 the template is instantiated -- unless we are explicitly
13667 looking up names in uninstantiated templates. Even then, we
13668 cannot look up the name if the scope is not a class type; it
13669 might, for example, be a template type parameter. */
13670 dependent_p = (TYPE_P (parser->scope)
13671 && !(parser->in_declarator_p
13672 && currently_open_class (parser->scope))
13673 && dependent_type_p (parser->scope));
13674 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13675 && dependent_p)
13677 if (is_type)
13678 /* The resolution to Core Issue 180 says that `struct A::B'
13679 should be considered a type-name, even if `A' is
13680 dependent. */
13681 decl = TYPE_NAME (make_typename_type (parser->scope,
13682 name,
13683 /*complain=*/1));
13684 else if (is_template)
13685 decl = make_unbound_class_template (parser->scope,
13686 name,
13687 /*complain=*/1);
13688 else
13689 decl = build_nt (SCOPE_REF, parser->scope, name);
13691 else
13693 bool pop_p = false;
13695 /* If PARSER->SCOPE is a dependent type, then it must be a
13696 class type, and we must not be checking dependencies;
13697 otherwise, we would have processed this lookup above. So
13698 that PARSER->SCOPE is not considered a dependent base by
13699 lookup_member, we must enter the scope here. */
13700 if (dependent_p)
13701 pop_p = push_scope (parser->scope);
13702 /* If the PARSER->SCOPE is a a template specialization, it
13703 may be instantiated during name lookup. In that case,
13704 errors may be issued. Even if we rollback the current
13705 tentative parse, those errors are valid. */
13706 decl = lookup_qualified_name (parser->scope, name, is_type,
13707 /*complain=*/true);
13708 if (pop_p)
13709 pop_scope (parser->scope);
13711 parser->qualifying_scope = parser->scope;
13712 parser->object_scope = NULL_TREE;
13714 else if (object_type)
13716 tree object_decl = NULL_TREE;
13717 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13718 OBJECT_TYPE is not a class. */
13719 if (CLASS_TYPE_P (object_type))
13720 /* If the OBJECT_TYPE is a template specialization, it may
13721 be instantiated during name lookup. In that case, errors
13722 may be issued. Even if we rollback the current tentative
13723 parse, those errors are valid. */
13724 object_decl = lookup_member (object_type,
13725 name,
13726 /*protect=*/0, is_type);
13727 /* Look it up in the enclosing context, too. */
13728 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13729 is_namespace,
13730 /*flags=*/0);
13731 parser->object_scope = object_type;
13732 parser->qualifying_scope = NULL_TREE;
13733 if (object_decl)
13734 decl = object_decl;
13736 else
13738 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13739 is_namespace,
13740 /*flags=*/0);
13741 parser->qualifying_scope = NULL_TREE;
13742 parser->object_scope = NULL_TREE;
13745 /* If the lookup failed, let our caller know. */
13746 if (!decl
13747 || decl == error_mark_node
13748 || (TREE_CODE (decl) == FUNCTION_DECL
13749 && DECL_ANTICIPATED (decl)))
13750 return error_mark_node;
13752 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13753 if (TREE_CODE (decl) == TREE_LIST)
13755 /* The error message we have to print is too complicated for
13756 cp_parser_error, so we incorporate its actions directly. */
13757 if (!cp_parser_simulate_error (parser))
13759 error ("reference to `%D' is ambiguous", name);
13760 print_candidates (decl);
13762 return error_mark_node;
13765 my_friendly_assert (DECL_P (decl)
13766 || TREE_CODE (decl) == OVERLOAD
13767 || TREE_CODE (decl) == SCOPE_REF
13768 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13769 || BASELINK_P (decl),
13770 20000619);
13772 /* If we have resolved the name of a member declaration, check to
13773 see if the declaration is accessible. When the name resolves to
13774 set of overloaded functions, accessibility is checked when
13775 overload resolution is done.
13777 During an explicit instantiation, access is not checked at all,
13778 as per [temp.explicit]. */
13779 if (DECL_P (decl))
13780 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13782 return decl;
13785 /* Like cp_parser_lookup_name, but for use in the typical case where
13786 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13787 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13789 static tree
13790 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13792 return cp_parser_lookup_name (parser, name,
13793 /*is_type=*/false,
13794 /*is_template=*/false,
13795 /*is_namespace=*/false,
13796 /*check_dependency=*/true);
13799 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13800 the current context, return the TYPE_DECL. If TAG_NAME_P is
13801 true, the DECL indicates the class being defined in a class-head,
13802 or declared in an elaborated-type-specifier.
13804 Otherwise, return DECL. */
13806 static tree
13807 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13809 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13810 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13812 struct A {
13813 template <typename T> struct B;
13816 template <typename T> struct A::B {};
13818 Similarly, in a elaborated-type-specifier:
13820 namespace N { struct X{}; }
13822 struct A {
13823 template <typename T> friend struct N::X;
13826 However, if the DECL refers to a class type, and we are in
13827 the scope of the class, then the name lookup automatically
13828 finds the TYPE_DECL created by build_self_reference rather
13829 than a TEMPLATE_DECL. For example, in:
13831 template <class T> struct S {
13832 S s;
13835 there is no need to handle such case. */
13837 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13838 return DECL_TEMPLATE_RESULT (decl);
13840 return decl;
13843 /* If too many, or too few, template-parameter lists apply to the
13844 declarator, issue an error message. Returns TRUE if all went well,
13845 and FALSE otherwise. */
13847 static bool
13848 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13849 tree declarator)
13851 unsigned num_templates;
13853 /* We haven't seen any classes that involve template parameters yet. */
13854 num_templates = 0;
13856 switch (TREE_CODE (declarator))
13858 case CALL_EXPR:
13859 case ARRAY_REF:
13860 case INDIRECT_REF:
13861 case ADDR_EXPR:
13863 tree main_declarator = TREE_OPERAND (declarator, 0);
13864 return
13865 cp_parser_check_declarator_template_parameters (parser,
13866 main_declarator);
13869 case SCOPE_REF:
13871 tree scope;
13872 tree member;
13874 scope = TREE_OPERAND (declarator, 0);
13875 member = TREE_OPERAND (declarator, 1);
13877 /* If this is a pointer-to-member, then we are not interested
13878 in the SCOPE, because it does not qualify the thing that is
13879 being declared. */
13880 if (TREE_CODE (member) == INDIRECT_REF)
13881 return (cp_parser_check_declarator_template_parameters
13882 (parser, member));
13884 while (scope && CLASS_TYPE_P (scope))
13886 /* You're supposed to have one `template <...>'
13887 for every template class, but you don't need one
13888 for a full specialization. For example:
13890 template <class T> struct S{};
13891 template <> struct S<int> { void f(); };
13892 void S<int>::f () {}
13894 is correct; there shouldn't be a `template <>' for
13895 the definition of `S<int>::f'. */
13896 if (CLASSTYPE_TEMPLATE_INFO (scope)
13897 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13898 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13899 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13900 ++num_templates;
13902 scope = TYPE_CONTEXT (scope);
13906 /* Fall through. */
13908 default:
13909 /* If the DECLARATOR has the form `X<y>' then it uses one
13910 additional level of template parameters. */
13911 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13912 ++num_templates;
13914 return cp_parser_check_template_parameters (parser,
13915 num_templates);
13919 /* NUM_TEMPLATES were used in the current declaration. If that is
13920 invalid, return FALSE and issue an error messages. Otherwise,
13921 return TRUE. */
13923 static bool
13924 cp_parser_check_template_parameters (cp_parser* parser,
13925 unsigned num_templates)
13927 /* If there are more template classes than parameter lists, we have
13928 something like:
13930 template <class T> void S<T>::R<T>::f (); */
13931 if (parser->num_template_parameter_lists < num_templates)
13933 error ("too few template-parameter-lists");
13934 return false;
13936 /* If there are the same number of template classes and parameter
13937 lists, that's OK. */
13938 if (parser->num_template_parameter_lists == num_templates)
13939 return true;
13940 /* If there are more, but only one more, then we are referring to a
13941 member template. That's OK too. */
13942 if (parser->num_template_parameter_lists == num_templates + 1)
13943 return true;
13944 /* Otherwise, there are too many template parameter lists. We have
13945 something like:
13947 template <class T> template <class U> void S::f(); */
13948 error ("too many template-parameter-lists");
13949 return false;
13952 /* Parse a binary-expression of the general form:
13954 binary-expression:
13955 <expr>
13956 binary-expression <token> <expr>
13958 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13959 to parser the <expr>s. If the first production is used, then the
13960 value returned by FN is returned directly. Otherwise, a node with
13961 the indicated EXPR_TYPE is returned, with operands corresponding to
13962 the two sub-expressions. */
13964 static tree
13965 cp_parser_binary_expression (cp_parser* parser,
13966 const cp_parser_token_tree_map token_tree_map,
13967 cp_parser_expression_fn fn)
13969 tree lhs;
13971 /* Parse the first expression. */
13972 lhs = (*fn) (parser);
13973 /* Now, look for more expressions. */
13974 while (true)
13976 cp_token *token;
13977 const cp_parser_token_tree_map_node *map_node;
13978 tree rhs;
13980 /* Peek at the next token. */
13981 token = cp_lexer_peek_token (parser->lexer);
13982 /* If the token is `>', and that's not an operator at the
13983 moment, then we're done. */
13984 if (token->type == CPP_GREATER
13985 && !parser->greater_than_is_operator_p)
13986 break;
13987 /* If we find one of the tokens we want, build the corresponding
13988 tree representation. */
13989 for (map_node = token_tree_map;
13990 map_node->token_type != CPP_EOF;
13991 ++map_node)
13992 if (map_node->token_type == token->type)
13994 /* Assume that an overloaded operator will not be used. */
13995 bool overloaded_p = false;
13997 /* Consume the operator token. */
13998 cp_lexer_consume_token (parser->lexer);
13999 /* Parse the right-hand side of the expression. */
14000 rhs = (*fn) (parser);
14001 /* Build the binary tree node. */
14002 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14003 &overloaded_p);
14004 /* If the binary operator required the use of an
14005 overloaded operator, then this expression cannot be an
14006 integral constant-expression. An overloaded operator
14007 can be used even if both operands are otherwise
14008 permissible in an integral constant-expression if at
14009 least one of the operands is of enumeration type. */
14010 if (overloaded_p
14011 && (cp_parser_non_integral_constant_expression
14012 (parser, "calls to overloaded operators")))
14013 lhs = error_mark_node;
14014 break;
14017 /* If the token wasn't one of the ones we want, we're done. */
14018 if (map_node->token_type == CPP_EOF)
14019 break;
14022 return lhs;
14025 /* Parse an optional `::' token indicating that the following name is
14026 from the global namespace. If so, PARSER->SCOPE is set to the
14027 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14028 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14029 Returns the new value of PARSER->SCOPE, if the `::' token is
14030 present, and NULL_TREE otherwise. */
14032 static tree
14033 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14035 cp_token *token;
14037 /* Peek at the next token. */
14038 token = cp_lexer_peek_token (parser->lexer);
14039 /* If we're looking at a `::' token then we're starting from the
14040 global namespace, not our current location. */
14041 if (token->type == CPP_SCOPE)
14043 /* Consume the `::' token. */
14044 cp_lexer_consume_token (parser->lexer);
14045 /* Set the SCOPE so that we know where to start the lookup. */
14046 parser->scope = global_namespace;
14047 parser->qualifying_scope = global_namespace;
14048 parser->object_scope = NULL_TREE;
14050 return parser->scope;
14052 else if (!current_scope_valid_p)
14054 parser->scope = NULL_TREE;
14055 parser->qualifying_scope = NULL_TREE;
14056 parser->object_scope = NULL_TREE;
14059 return NULL_TREE;
14062 /* Returns TRUE if the upcoming token sequence is the start of a
14063 constructor declarator. If FRIEND_P is true, the declarator is
14064 preceded by the `friend' specifier. */
14066 static bool
14067 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14069 bool constructor_p;
14070 tree type_decl = NULL_TREE;
14071 bool nested_name_p;
14072 cp_token *next_token;
14074 /* The common case is that this is not a constructor declarator, so
14075 try to avoid doing lots of work if at all possible. It's not
14076 valid declare a constructor at function scope. */
14077 if (at_function_scope_p ())
14078 return false;
14079 /* And only certain tokens can begin a constructor declarator. */
14080 next_token = cp_lexer_peek_token (parser->lexer);
14081 if (next_token->type != CPP_NAME
14082 && next_token->type != CPP_SCOPE
14083 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14084 && next_token->type != CPP_TEMPLATE_ID)
14085 return false;
14087 /* Parse tentatively; we are going to roll back all of the tokens
14088 consumed here. */
14089 cp_parser_parse_tentatively (parser);
14090 /* Assume that we are looking at a constructor declarator. */
14091 constructor_p = true;
14093 /* Look for the optional `::' operator. */
14094 cp_parser_global_scope_opt (parser,
14095 /*current_scope_valid_p=*/false);
14096 /* Look for the nested-name-specifier. */
14097 nested_name_p
14098 = (cp_parser_nested_name_specifier_opt (parser,
14099 /*typename_keyword_p=*/false,
14100 /*check_dependency_p=*/false,
14101 /*type_p=*/false,
14102 /*is_declaration=*/false)
14103 != NULL_TREE);
14104 /* Outside of a class-specifier, there must be a
14105 nested-name-specifier. */
14106 if (!nested_name_p &&
14107 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14108 || friend_p))
14109 constructor_p = false;
14110 /* If we still think that this might be a constructor-declarator,
14111 look for a class-name. */
14112 if (constructor_p)
14114 /* If we have:
14116 template <typename T> struct S { S(); };
14117 template <typename T> S<T>::S ();
14119 we must recognize that the nested `S' names a class.
14120 Similarly, for:
14122 template <typename T> S<T>::S<T> ();
14124 we must recognize that the nested `S' names a template. */
14125 type_decl = cp_parser_class_name (parser,
14126 /*typename_keyword_p=*/false,
14127 /*template_keyword_p=*/false,
14128 /*type_p=*/false,
14129 /*check_dependency_p=*/false,
14130 /*class_head_p=*/false,
14131 /*is_declaration=*/false);
14132 /* If there was no class-name, then this is not a constructor. */
14133 constructor_p = !cp_parser_error_occurred (parser);
14136 /* If we're still considering a constructor, we have to see a `(',
14137 to begin the parameter-declaration-clause, followed by either a
14138 `)', an `...', or a decl-specifier. We need to check for a
14139 type-specifier to avoid being fooled into thinking that:
14141 S::S (f) (int);
14143 is a constructor. (It is actually a function named `f' that
14144 takes one parameter (of type `int') and returns a value of type
14145 `S::S'. */
14146 if (constructor_p
14147 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14149 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14150 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14151 && !cp_parser_storage_class_specifier_opt (parser))
14153 tree type;
14154 bool pop_p = false;
14155 unsigned saved_num_template_parameter_lists;
14157 /* Names appearing in the type-specifier should be looked up
14158 in the scope of the class. */
14159 if (current_class_type)
14160 type = NULL_TREE;
14161 else
14163 type = TREE_TYPE (type_decl);
14164 if (TREE_CODE (type) == TYPENAME_TYPE)
14166 type = resolve_typename_type (type,
14167 /*only_current_p=*/false);
14168 if (type == error_mark_node)
14170 cp_parser_abort_tentative_parse (parser);
14171 return false;
14174 pop_p = push_scope (type);
14177 /* Inside the constructor parameter list, surrounding
14178 template-parameter-lists do not apply. */
14179 saved_num_template_parameter_lists
14180 = parser->num_template_parameter_lists;
14181 parser->num_template_parameter_lists = 0;
14183 /* Look for the type-specifier. */
14184 cp_parser_type_specifier (parser,
14185 CP_PARSER_FLAGS_NONE,
14186 /*is_friend=*/false,
14187 /*is_declarator=*/true,
14188 /*declares_class_or_enum=*/NULL,
14189 /*is_cv_qualifier=*/NULL);
14191 parser->num_template_parameter_lists
14192 = saved_num_template_parameter_lists;
14194 /* Leave the scope of the class. */
14195 if (pop_p)
14196 pop_scope (type);
14198 constructor_p = !cp_parser_error_occurred (parser);
14201 else
14202 constructor_p = false;
14203 /* We did not really want to consume any tokens. */
14204 cp_parser_abort_tentative_parse (parser);
14206 return constructor_p;
14209 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14210 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14211 they must be performed once we are in the scope of the function.
14213 Returns the function defined. */
14215 static tree
14216 cp_parser_function_definition_from_specifiers_and_declarator
14217 (cp_parser* parser,
14218 tree decl_specifiers,
14219 tree attributes,
14220 tree declarator)
14222 tree fn;
14223 bool success_p;
14225 /* Begin the function-definition. */
14226 success_p = begin_function_definition (decl_specifiers,
14227 attributes,
14228 declarator);
14230 /* If there were names looked up in the decl-specifier-seq that we
14231 did not check, check them now. We must wait until we are in the
14232 scope of the function to perform the checks, since the function
14233 might be a friend. */
14234 perform_deferred_access_checks ();
14236 if (!success_p)
14238 /* If begin_function_definition didn't like the definition, skip
14239 the entire function. */
14240 error ("invalid function declaration");
14241 cp_parser_skip_to_end_of_block_or_statement (parser);
14242 fn = error_mark_node;
14244 else
14245 fn = cp_parser_function_definition_after_declarator (parser,
14246 /*inline_p=*/false);
14248 return fn;
14251 /* Parse the part of a function-definition that follows the
14252 declarator. INLINE_P is TRUE iff this function is an inline
14253 function defined with a class-specifier.
14255 Returns the function defined. */
14257 static tree
14258 cp_parser_function_definition_after_declarator (cp_parser* parser,
14259 bool inline_p)
14261 tree fn;
14262 bool ctor_initializer_p = false;
14263 bool saved_in_unbraced_linkage_specification_p;
14264 unsigned saved_num_template_parameter_lists;
14266 /* If the next token is `return', then the code may be trying to
14267 make use of the "named return value" extension that G++ used to
14268 support. */
14269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14271 /* Consume the `return' keyword. */
14272 cp_lexer_consume_token (parser->lexer);
14273 /* Look for the identifier that indicates what value is to be
14274 returned. */
14275 cp_parser_identifier (parser);
14276 /* Issue an error message. */
14277 error ("named return values are no longer supported");
14278 /* Skip tokens until we reach the start of the function body. */
14279 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14280 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14281 cp_lexer_consume_token (parser->lexer);
14283 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14284 anything declared inside `f'. */
14285 saved_in_unbraced_linkage_specification_p
14286 = parser->in_unbraced_linkage_specification_p;
14287 parser->in_unbraced_linkage_specification_p = false;
14288 /* Inside the function, surrounding template-parameter-lists do not
14289 apply. */
14290 saved_num_template_parameter_lists
14291 = parser->num_template_parameter_lists;
14292 parser->num_template_parameter_lists = 0;
14293 /* If the next token is `try', then we are looking at a
14294 function-try-block. */
14295 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14296 ctor_initializer_p = cp_parser_function_try_block (parser);
14297 /* A function-try-block includes the function-body, so we only do
14298 this next part if we're not processing a function-try-block. */
14299 else
14300 ctor_initializer_p
14301 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14303 /* Finish the function. */
14304 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14305 (inline_p ? 2 : 0));
14306 /* Generate code for it, if necessary. */
14307 expand_or_defer_fn (fn);
14308 /* Restore the saved values. */
14309 parser->in_unbraced_linkage_specification_p
14310 = saved_in_unbraced_linkage_specification_p;
14311 parser->num_template_parameter_lists
14312 = saved_num_template_parameter_lists;
14314 return fn;
14317 /* Parse a template-declaration, assuming that the `export' (and
14318 `extern') keywords, if present, has already been scanned. MEMBER_P
14319 is as for cp_parser_template_declaration. */
14321 static void
14322 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14324 tree decl = NULL_TREE;
14325 tree parameter_list;
14326 bool friend_p = false;
14328 /* Look for the `template' keyword. */
14329 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14330 return;
14332 /* And the `<'. */
14333 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14334 return;
14336 /* If the next token is `>', then we have an invalid
14337 specialization. Rather than complain about an invalid template
14338 parameter, issue an error message here. */
14339 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14341 cp_parser_error (parser, "invalid explicit specialization");
14342 begin_specialization ();
14343 parameter_list = NULL_TREE;
14345 else
14347 /* Parse the template parameters. */
14348 begin_template_parm_list ();
14349 parameter_list = cp_parser_template_parameter_list (parser);
14350 parameter_list = end_template_parm_list (parameter_list);
14353 /* Look for the `>'. */
14354 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14355 /* We just processed one more parameter list. */
14356 ++parser->num_template_parameter_lists;
14357 /* If the next token is `template', there are more template
14358 parameters. */
14359 if (cp_lexer_next_token_is_keyword (parser->lexer,
14360 RID_TEMPLATE))
14361 cp_parser_template_declaration_after_export (parser, member_p);
14362 else
14364 decl = cp_parser_single_declaration (parser,
14365 member_p,
14366 &friend_p);
14368 /* If this is a member template declaration, let the front
14369 end know. */
14370 if (member_p && !friend_p && decl)
14372 if (TREE_CODE (decl) == TYPE_DECL)
14373 cp_parser_check_access_in_redeclaration (decl);
14375 decl = finish_member_template_decl (decl);
14377 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14378 make_friend_class (current_class_type, TREE_TYPE (decl),
14379 /*complain=*/true);
14381 /* We are done with the current parameter list. */
14382 --parser->num_template_parameter_lists;
14384 /* Finish up. */
14385 finish_template_decl (parameter_list);
14387 /* Register member declarations. */
14388 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14389 finish_member_declaration (decl);
14391 /* If DECL is a function template, we must return to parse it later.
14392 (Even though there is no definition, there might be default
14393 arguments that need handling.) */
14394 if (member_p && decl
14395 && (TREE_CODE (decl) == FUNCTION_DECL
14396 || DECL_FUNCTION_TEMPLATE_P (decl)))
14397 TREE_VALUE (parser->unparsed_functions_queues)
14398 = tree_cons (NULL_TREE, decl,
14399 TREE_VALUE (parser->unparsed_functions_queues));
14402 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14403 `function-definition' sequence. MEMBER_P is true, this declaration
14404 appears in a class scope.
14406 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14407 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14409 static tree
14410 cp_parser_single_declaration (cp_parser* parser,
14411 bool member_p,
14412 bool* friend_p)
14414 int declares_class_or_enum;
14415 tree decl = NULL_TREE;
14416 tree decl_specifiers;
14417 tree attributes;
14418 bool function_definition_p = false;
14420 /* Defer access checks until we know what is being declared. */
14421 push_deferring_access_checks (dk_deferred);
14423 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14424 alternative. */
14425 decl_specifiers
14426 = cp_parser_decl_specifier_seq (parser,
14427 CP_PARSER_FLAGS_OPTIONAL,
14428 &attributes,
14429 &declares_class_or_enum);
14430 if (friend_p)
14431 *friend_p = cp_parser_friend_p (decl_specifiers);
14432 /* Gather up the access checks that occurred the
14433 decl-specifier-seq. */
14434 stop_deferring_access_checks ();
14436 /* Check for the declaration of a template class. */
14437 if (declares_class_or_enum)
14439 if (cp_parser_declares_only_class_p (parser))
14441 decl = shadow_tag (decl_specifiers);
14442 if (decl)
14443 decl = TYPE_NAME (decl);
14444 else
14445 decl = error_mark_node;
14448 else
14449 decl = NULL_TREE;
14450 /* If it's not a template class, try for a template function. If
14451 the next token is a `;', then this declaration does not declare
14452 anything. But, if there were errors in the decl-specifiers, then
14453 the error might well have come from an attempted class-specifier.
14454 In that case, there's no need to warn about a missing declarator. */
14455 if (!decl
14456 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14457 || !value_member (error_mark_node, decl_specifiers)))
14458 decl = cp_parser_init_declarator (parser,
14459 decl_specifiers,
14460 attributes,
14461 /*function_definition_allowed_p=*/true,
14462 member_p,
14463 declares_class_or_enum,
14464 &function_definition_p);
14466 pop_deferring_access_checks ();
14468 /* Clear any current qualification; whatever comes next is the start
14469 of something new. */
14470 parser->scope = NULL_TREE;
14471 parser->qualifying_scope = NULL_TREE;
14472 parser->object_scope = NULL_TREE;
14473 /* Look for a trailing `;' after the declaration. */
14474 if (!function_definition_p
14475 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14476 cp_parser_skip_to_end_of_block_or_statement (parser);
14478 return decl;
14481 /* Parse a cast-expression that is not the operand of a unary "&". */
14483 static tree
14484 cp_parser_simple_cast_expression (cp_parser *parser)
14486 return cp_parser_cast_expression (parser, /*address_p=*/false);
14489 /* Parse a functional cast to TYPE. Returns an expression
14490 representing the cast. */
14492 static tree
14493 cp_parser_functional_cast (cp_parser* parser, tree type)
14495 tree expression_list;
14496 tree cast;
14498 expression_list
14499 = cp_parser_parenthesized_expression_list (parser, false,
14500 /*non_constant_p=*/NULL);
14502 cast = build_functional_cast (type, expression_list);
14503 /* [expr.const]/1: In an integral constant expression "only type
14504 conversions to integral or enumeration type can be used". */
14505 if (cast != error_mark_node && !type_dependent_expression_p (type)
14506 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14508 if (cp_parser_non_integral_constant_expression
14509 (parser, "a call to a constructor"))
14510 return error_mark_node;
14512 return cast;
14515 /* Save the tokens that make up the body of a member function defined
14516 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14517 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14518 specifiers applied to the declaration. Returns the FUNCTION_DECL
14519 for the member function. */
14521 static tree
14522 cp_parser_save_member_function_body (cp_parser* parser,
14523 tree decl_specifiers,
14524 tree declarator,
14525 tree attributes)
14527 cp_token_cache *cache;
14528 tree fn;
14530 /* Create the function-declaration. */
14531 fn = start_method (decl_specifiers, declarator, attributes);
14532 /* If something went badly wrong, bail out now. */
14533 if (fn == error_mark_node)
14535 /* If there's a function-body, skip it. */
14536 if (cp_parser_token_starts_function_definition_p
14537 (cp_lexer_peek_token (parser->lexer)))
14538 cp_parser_skip_to_end_of_block_or_statement (parser);
14539 return error_mark_node;
14542 /* Remember it, if there default args to post process. */
14543 cp_parser_save_default_args (parser, fn);
14545 /* Create a token cache. */
14546 cache = cp_token_cache_new ();
14547 /* Save away the tokens that make up the body of the
14548 function. */
14549 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14550 /* Handle function try blocks. */
14551 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14552 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14554 /* Save away the inline definition; we will process it when the
14555 class is complete. */
14556 DECL_PENDING_INLINE_INFO (fn) = cache;
14557 DECL_PENDING_INLINE_P (fn) = 1;
14559 /* We need to know that this was defined in the class, so that
14560 friend templates are handled correctly. */
14561 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14563 /* We're done with the inline definition. */
14564 finish_method (fn);
14566 /* Add FN to the queue of functions to be parsed later. */
14567 TREE_VALUE (parser->unparsed_functions_queues)
14568 = tree_cons (NULL_TREE, fn,
14569 TREE_VALUE (parser->unparsed_functions_queues));
14571 return fn;
14574 /* Parse a template-argument-list, as well as the trailing ">" (but
14575 not the opening ">"). See cp_parser_template_argument_list for the
14576 return value. */
14578 static tree
14579 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14581 tree arguments;
14582 tree saved_scope;
14583 tree saved_qualifying_scope;
14584 tree saved_object_scope;
14585 bool saved_greater_than_is_operator_p;
14587 /* [temp.names]
14589 When parsing a template-id, the first non-nested `>' is taken as
14590 the end of the template-argument-list rather than a greater-than
14591 operator. */
14592 saved_greater_than_is_operator_p
14593 = parser->greater_than_is_operator_p;
14594 parser->greater_than_is_operator_p = false;
14595 /* Parsing the argument list may modify SCOPE, so we save it
14596 here. */
14597 saved_scope = parser->scope;
14598 saved_qualifying_scope = parser->qualifying_scope;
14599 saved_object_scope = parser->object_scope;
14600 /* Parse the template-argument-list itself. */
14601 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14602 arguments = NULL_TREE;
14603 else
14604 arguments = cp_parser_template_argument_list (parser);
14605 /* Look for the `>' that ends the template-argument-list. If we find
14606 a '>>' instead, it's probably just a typo. */
14607 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14609 if (!saved_greater_than_is_operator_p)
14611 /* If we're in a nested template argument list, the '>>' has to be
14612 a typo for '> >'. We emit the error message, but we continue
14613 parsing and we push a '>' as next token, so that the argument
14614 list will be parsed correctly.. */
14615 cp_token* token;
14616 error ("`>>' should be `> >' within a nested template argument list");
14617 token = cp_lexer_peek_token (parser->lexer);
14618 token->type = CPP_GREATER;
14620 else
14622 /* If this is not a nested template argument list, the '>>' is
14623 a typo for '>'. Emit an error message and continue. */
14624 error ("spurious `>>', use `>' to terminate a template argument list");
14625 cp_lexer_consume_token (parser->lexer);
14628 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14629 error ("missing `>' to terminate the template argument list");
14630 /* The `>' token might be a greater-than operator again now. */
14631 parser->greater_than_is_operator_p
14632 = saved_greater_than_is_operator_p;
14633 /* Restore the SAVED_SCOPE. */
14634 parser->scope = saved_scope;
14635 parser->qualifying_scope = saved_qualifying_scope;
14636 parser->object_scope = saved_object_scope;
14638 return arguments;
14641 /* MEMBER_FUNCTION is a member function, or a friend. If default
14642 arguments, or the body of the function have not yet been parsed,
14643 parse them now. */
14645 static void
14646 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14648 cp_lexer *saved_lexer;
14650 /* If this member is a template, get the underlying
14651 FUNCTION_DECL. */
14652 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14653 member_function = DECL_TEMPLATE_RESULT (member_function);
14655 /* There should not be any class definitions in progress at this
14656 point; the bodies of members are only parsed outside of all class
14657 definitions. */
14658 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14659 /* While we're parsing the member functions we might encounter more
14660 classes. We want to handle them right away, but we don't want
14661 them getting mixed up with functions that are currently in the
14662 queue. */
14663 parser->unparsed_functions_queues
14664 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14666 /* Make sure that any template parameters are in scope. */
14667 maybe_begin_member_template_processing (member_function);
14669 /* If the body of the function has not yet been parsed, parse it
14670 now. */
14671 if (DECL_PENDING_INLINE_P (member_function))
14673 tree function_scope;
14674 cp_token_cache *tokens;
14676 /* The function is no longer pending; we are processing it. */
14677 tokens = DECL_PENDING_INLINE_INFO (member_function);
14678 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14679 DECL_PENDING_INLINE_P (member_function) = 0;
14680 /* If this was an inline function in a local class, enter the scope
14681 of the containing function. */
14682 function_scope = decl_function_context (member_function);
14683 if (function_scope)
14684 push_function_context_to (function_scope);
14686 /* Save away the current lexer. */
14687 saved_lexer = parser->lexer;
14688 /* Make a new lexer to feed us the tokens saved for this function. */
14689 parser->lexer = cp_lexer_new_from_tokens (tokens);
14690 parser->lexer->next = saved_lexer;
14692 /* Set the current source position to be the location of the first
14693 token in the saved inline body. */
14694 cp_lexer_peek_token (parser->lexer);
14696 /* Let the front end know that we going to be defining this
14697 function. */
14698 start_function (NULL_TREE, member_function, NULL_TREE,
14699 SF_PRE_PARSED | SF_INCLASS_INLINE);
14701 /* Now, parse the body of the function. */
14702 cp_parser_function_definition_after_declarator (parser,
14703 /*inline_p=*/true);
14705 /* Leave the scope of the containing function. */
14706 if (function_scope)
14707 pop_function_context_from (function_scope);
14708 /* Restore the lexer. */
14709 parser->lexer = saved_lexer;
14712 /* Remove any template parameters from the symbol table. */
14713 maybe_end_member_template_processing ();
14715 /* Restore the queue. */
14716 parser->unparsed_functions_queues
14717 = TREE_CHAIN (parser->unparsed_functions_queues);
14720 /* If DECL contains any default args, remember it on the unparsed
14721 functions queue. */
14723 static void
14724 cp_parser_save_default_args (cp_parser* parser, tree decl)
14726 tree probe;
14728 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14729 probe;
14730 probe = TREE_CHAIN (probe))
14731 if (TREE_PURPOSE (probe))
14733 TREE_PURPOSE (parser->unparsed_functions_queues)
14734 = tree_cons (NULL_TREE, decl,
14735 TREE_PURPOSE (parser->unparsed_functions_queues));
14736 break;
14738 return;
14741 /* FN is a FUNCTION_DECL which may contains a parameter with an
14742 unparsed DEFAULT_ARG. Parse the default args now. */
14744 static void
14745 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14747 cp_lexer *saved_lexer;
14748 cp_token_cache *tokens;
14749 bool saved_local_variables_forbidden_p;
14750 tree parameters;
14752 /* While we're parsing the default args, we might (due to the
14753 statement expression extension) encounter more classes. We want
14754 to handle them right away, but we don't want them getting mixed
14755 up with default args that are currently in the queue. */
14756 parser->unparsed_functions_queues
14757 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14759 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14760 parameters;
14761 parameters = TREE_CHAIN (parameters))
14763 if (!TREE_PURPOSE (parameters)
14764 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14765 continue;
14767 /* Save away the current lexer. */
14768 saved_lexer = parser->lexer;
14769 /* Create a new one, using the tokens we have saved. */
14770 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14771 parser->lexer = cp_lexer_new_from_tokens (tokens);
14773 /* Set the current source position to be the location of the
14774 first token in the default argument. */
14775 cp_lexer_peek_token (parser->lexer);
14777 /* Local variable names (and the `this' keyword) may not appear
14778 in a default argument. */
14779 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14780 parser->local_variables_forbidden_p = true;
14781 /* Parse the assignment-expression. */
14782 if (DECL_CLASS_SCOPE_P (fn))
14783 push_nested_class (DECL_CONTEXT (fn));
14784 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14785 if (DECL_CLASS_SCOPE_P (fn))
14786 pop_nested_class ();
14788 /* If the token stream has not been completely used up, then
14789 there was extra junk after the end of the default
14790 argument. */
14791 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14792 cp_parser_error (parser, "expected `,'");
14794 /* Restore saved state. */
14795 parser->lexer = saved_lexer;
14796 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14799 /* Restore the queue. */
14800 parser->unparsed_functions_queues
14801 = TREE_CHAIN (parser->unparsed_functions_queues);
14804 /* Parse the operand of `sizeof' (or a similar operator). Returns
14805 either a TYPE or an expression, depending on the form of the
14806 input. The KEYWORD indicates which kind of expression we have
14807 encountered. */
14809 static tree
14810 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14812 static const char *format;
14813 tree expr = NULL_TREE;
14814 const char *saved_message;
14815 bool saved_integral_constant_expression_p;
14817 /* Initialize FORMAT the first time we get here. */
14818 if (!format)
14819 format = "types may not be defined in `%s' expressions";
14821 /* Types cannot be defined in a `sizeof' expression. Save away the
14822 old message. */
14823 saved_message = parser->type_definition_forbidden_message;
14824 /* And create the new one. */
14825 parser->type_definition_forbidden_message
14826 = xmalloc (strlen (format)
14827 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14828 + 1 /* `\0' */);
14829 sprintf ((char *) parser->type_definition_forbidden_message,
14830 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14832 /* The restrictions on constant-expressions do not apply inside
14833 sizeof expressions. */
14834 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14835 parser->integral_constant_expression_p = false;
14837 /* Do not actually evaluate the expression. */
14838 ++skip_evaluation;
14839 /* If it's a `(', then we might be looking at the type-id
14840 construction. */
14841 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14843 tree type;
14844 bool saved_in_type_id_in_expr_p;
14846 /* We can't be sure yet whether we're looking at a type-id or an
14847 expression. */
14848 cp_parser_parse_tentatively (parser);
14849 /* Consume the `('. */
14850 cp_lexer_consume_token (parser->lexer);
14851 /* Parse the type-id. */
14852 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14853 parser->in_type_id_in_expr_p = true;
14854 type = cp_parser_type_id (parser);
14855 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14856 /* Now, look for the trailing `)'. */
14857 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14858 /* If all went well, then we're done. */
14859 if (cp_parser_parse_definitely (parser))
14861 /* Build a list of decl-specifiers; right now, we have only
14862 a single type-specifier. */
14863 type = build_tree_list (NULL_TREE,
14864 type);
14866 /* Call grokdeclarator to figure out what type this is. */
14867 expr = grokdeclarator (NULL_TREE,
14868 type,
14869 TYPENAME,
14870 /*initialized=*/0,
14871 /*attrlist=*/NULL);
14875 /* If the type-id production did not work out, then we must be
14876 looking at the unary-expression production. */
14877 if (!expr)
14878 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14879 /* Go back to evaluating expressions. */
14880 --skip_evaluation;
14882 /* Free the message we created. */
14883 free ((char *) parser->type_definition_forbidden_message);
14884 /* And restore the old one. */
14885 parser->type_definition_forbidden_message = saved_message;
14886 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14888 return expr;
14891 /* If the current declaration has no declarator, return true. */
14893 static bool
14894 cp_parser_declares_only_class_p (cp_parser *parser)
14896 /* If the next token is a `;' or a `,' then there is no
14897 declarator. */
14898 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14899 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14902 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14903 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14905 static bool
14906 cp_parser_friend_p (tree decl_specifiers)
14908 while (decl_specifiers)
14910 /* See if this decl-specifier is `friend'. */
14911 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14912 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14913 return true;
14915 /* Go on to the next decl-specifier. */
14916 decl_specifiers = TREE_CHAIN (decl_specifiers);
14919 return false;
14922 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14923 issue an error message indicating that TOKEN_DESC was expected.
14925 Returns the token consumed, if the token had the appropriate type.
14926 Otherwise, returns NULL. */
14928 static cp_token *
14929 cp_parser_require (cp_parser* parser,
14930 enum cpp_ttype type,
14931 const char* token_desc)
14933 if (cp_lexer_next_token_is (parser->lexer, type))
14934 return cp_lexer_consume_token (parser->lexer);
14935 else
14937 /* Output the MESSAGE -- unless we're parsing tentatively. */
14938 if (!cp_parser_simulate_error (parser))
14940 char *message = concat ("expected ", token_desc, NULL);
14941 cp_parser_error (parser, message);
14942 free (message);
14944 return NULL;
14948 /* Like cp_parser_require, except that tokens will be skipped until
14949 the desired token is found. An error message is still produced if
14950 the next token is not as expected. */
14952 static void
14953 cp_parser_skip_until_found (cp_parser* parser,
14954 enum cpp_ttype type,
14955 const char* token_desc)
14957 cp_token *token;
14958 unsigned nesting_depth = 0;
14960 if (cp_parser_require (parser, type, token_desc))
14961 return;
14963 /* Skip tokens until the desired token is found. */
14964 while (true)
14966 /* Peek at the next token. */
14967 token = cp_lexer_peek_token (parser->lexer);
14968 /* If we've reached the token we want, consume it and
14969 stop. */
14970 if (token->type == type && !nesting_depth)
14972 cp_lexer_consume_token (parser->lexer);
14973 return;
14975 /* If we've run out of tokens, stop. */
14976 if (token->type == CPP_EOF)
14977 return;
14978 if (token->type == CPP_OPEN_BRACE
14979 || token->type == CPP_OPEN_PAREN
14980 || token->type == CPP_OPEN_SQUARE)
14981 ++nesting_depth;
14982 else if (token->type == CPP_CLOSE_BRACE
14983 || token->type == CPP_CLOSE_PAREN
14984 || token->type == CPP_CLOSE_SQUARE)
14986 if (nesting_depth-- == 0)
14987 return;
14989 /* Consume this token. */
14990 cp_lexer_consume_token (parser->lexer);
14994 /* If the next token is the indicated keyword, consume it. Otherwise,
14995 issue an error message indicating that TOKEN_DESC was expected.
14997 Returns the token consumed, if the token had the appropriate type.
14998 Otherwise, returns NULL. */
15000 static cp_token *
15001 cp_parser_require_keyword (cp_parser* parser,
15002 enum rid keyword,
15003 const char* token_desc)
15005 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15007 if (token && token->keyword != keyword)
15009 dyn_string_t error_msg;
15011 /* Format the error message. */
15012 error_msg = dyn_string_new (0);
15013 dyn_string_append_cstr (error_msg, "expected ");
15014 dyn_string_append_cstr (error_msg, token_desc);
15015 cp_parser_error (parser, error_msg->s);
15016 dyn_string_delete (error_msg);
15017 return NULL;
15020 return token;
15023 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15024 function-definition. */
15026 static bool
15027 cp_parser_token_starts_function_definition_p (cp_token* token)
15029 return (/* An ordinary function-body begins with an `{'. */
15030 token->type == CPP_OPEN_BRACE
15031 /* A ctor-initializer begins with a `:'. */
15032 || token->type == CPP_COLON
15033 /* A function-try-block begins with `try'. */
15034 || token->keyword == RID_TRY
15035 /* The named return value extension begins with `return'. */
15036 || token->keyword == RID_RETURN);
15039 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15040 definition. */
15042 static bool
15043 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15045 cp_token *token;
15047 token = cp_lexer_peek_token (parser->lexer);
15048 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15051 /* Returns TRUE iff the next token is the "," or ">" ending a
15052 template-argument. ">>" is also accepted (after the full
15053 argument was parsed) because it's probably a typo for "> >",
15054 and there is a specific diagnostic for this. */
15056 static bool
15057 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15059 cp_token *token;
15061 token = cp_lexer_peek_token (parser->lexer);
15062 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15063 || token->type == CPP_RSHIFT);
15066 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15067 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15069 static bool
15070 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15071 size_t n)
15073 cp_token *token;
15075 token = cp_lexer_peek_nth_token (parser->lexer, n);
15076 if (token->type == CPP_LESS)
15077 return true;
15078 /* Check for the sequence `<::' in the original code. It would be lexed as
15079 `[:', where `[' is a digraph, and there is no whitespace before
15080 `:'. */
15081 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15083 cp_token *token2;
15084 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15085 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15086 return true;
15088 return false;
15091 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15092 or none_type otherwise. */
15094 static enum tag_types
15095 cp_parser_token_is_class_key (cp_token* token)
15097 switch (token->keyword)
15099 case RID_CLASS:
15100 return class_type;
15101 case RID_STRUCT:
15102 return record_type;
15103 case RID_UNION:
15104 return union_type;
15106 default:
15107 return none_type;
15111 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15113 static void
15114 cp_parser_check_class_key (enum tag_types class_key, tree type)
15116 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15117 pedwarn ("`%s' tag used in naming `%#T'",
15118 class_key == union_type ? "union"
15119 : class_key == record_type ? "struct" : "class",
15120 type);
15123 /* Issue an error message if DECL is redeclared with different
15124 access than its original declaration [class.access.spec/3].
15125 This applies to nested classes and nested class templates.
15126 [class.mem/1]. */
15128 static void cp_parser_check_access_in_redeclaration (tree decl)
15130 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15131 return;
15133 if ((TREE_PRIVATE (decl)
15134 != (current_access_specifier == access_private_node))
15135 || (TREE_PROTECTED (decl)
15136 != (current_access_specifier == access_protected_node)))
15137 error ("%D redeclared with different access", decl);
15140 /* Look for the `template' keyword, as a syntactic disambiguator.
15141 Return TRUE iff it is present, in which case it will be
15142 consumed. */
15144 static bool
15145 cp_parser_optional_template_keyword (cp_parser *parser)
15147 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15149 /* The `template' keyword can only be used within templates;
15150 outside templates the parser can always figure out what is a
15151 template and what is not. */
15152 if (!processing_template_decl)
15154 error ("`template' (as a disambiguator) is only allowed "
15155 "within templates");
15156 /* If this part of the token stream is rescanned, the same
15157 error message would be generated. So, we purge the token
15158 from the stream. */
15159 cp_lexer_purge_token (parser->lexer);
15160 return false;
15162 else
15164 /* Consume the `template' keyword. */
15165 cp_lexer_consume_token (parser->lexer);
15166 return true;
15170 return false;
15173 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15174 set PARSER->SCOPE, and perform other related actions. */
15176 static void
15177 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15179 tree value;
15180 tree check;
15182 /* Get the stored value. */
15183 value = cp_lexer_consume_token (parser->lexer)->value;
15184 /* Perform any access checks that were deferred. */
15185 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15186 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15187 /* Set the scope from the stored value. */
15188 parser->scope = TREE_VALUE (value);
15189 parser->qualifying_scope = TREE_TYPE (value);
15190 parser->object_scope = NULL_TREE;
15193 /* Add tokens to CACHE until a non-nested END token appears. */
15195 static void
15196 cp_parser_cache_group (cp_parser *parser,
15197 cp_token_cache *cache,
15198 enum cpp_ttype end,
15199 unsigned depth)
15201 while (true)
15203 cp_token *token;
15205 /* Abort a parenthesized expression if we encounter a brace. */
15206 if ((end == CPP_CLOSE_PAREN || depth == 0)
15207 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15208 return;
15209 /* If we've reached the end of the file, stop. */
15210 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15211 return;
15212 /* Consume the next token. */
15213 token = cp_lexer_consume_token (parser->lexer);
15214 /* Add this token to the tokens we are saving. */
15215 cp_token_cache_push_token (cache, token);
15216 /* See if it starts a new group. */
15217 if (token->type == CPP_OPEN_BRACE)
15219 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15220 if (depth == 0)
15221 return;
15223 else if (token->type == CPP_OPEN_PAREN)
15224 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15225 else if (token->type == end)
15226 return;
15230 /* Begin parsing tentatively. We always save tokens while parsing
15231 tentatively so that if the tentative parsing fails we can restore the
15232 tokens. */
15234 static void
15235 cp_parser_parse_tentatively (cp_parser* parser)
15237 /* Enter a new parsing context. */
15238 parser->context = cp_parser_context_new (parser->context);
15239 /* Begin saving tokens. */
15240 cp_lexer_save_tokens (parser->lexer);
15241 /* In order to avoid repetitive access control error messages,
15242 access checks are queued up until we are no longer parsing
15243 tentatively. */
15244 push_deferring_access_checks (dk_deferred);
15247 /* Commit to the currently active tentative parse. */
15249 static void
15250 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15252 cp_parser_context *context;
15253 cp_lexer *lexer;
15255 /* Mark all of the levels as committed. */
15256 lexer = parser->lexer;
15257 for (context = parser->context; context->next; context = context->next)
15259 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15260 break;
15261 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15262 while (!cp_lexer_saving_tokens (lexer))
15263 lexer = lexer->next;
15264 cp_lexer_commit_tokens (lexer);
15268 /* Abort the currently active tentative parse. All consumed tokens
15269 will be rolled back, and no diagnostics will be issued. */
15271 static void
15272 cp_parser_abort_tentative_parse (cp_parser* parser)
15274 cp_parser_simulate_error (parser);
15275 /* Now, pretend that we want to see if the construct was
15276 successfully parsed. */
15277 cp_parser_parse_definitely (parser);
15280 /* Stop parsing tentatively. If a parse error has occurred, restore the
15281 token stream. Otherwise, commit to the tokens we have consumed.
15282 Returns true if no error occurred; false otherwise. */
15284 static bool
15285 cp_parser_parse_definitely (cp_parser* parser)
15287 bool error_occurred;
15288 cp_parser_context *context;
15290 /* Remember whether or not an error occurred, since we are about to
15291 destroy that information. */
15292 error_occurred = cp_parser_error_occurred (parser);
15293 /* Remove the topmost context from the stack. */
15294 context = parser->context;
15295 parser->context = context->next;
15296 /* If no parse errors occurred, commit to the tentative parse. */
15297 if (!error_occurred)
15299 /* Commit to the tokens read tentatively, unless that was
15300 already done. */
15301 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15302 cp_lexer_commit_tokens (parser->lexer);
15304 pop_to_parent_deferring_access_checks ();
15306 /* Otherwise, if errors occurred, roll back our state so that things
15307 are just as they were before we began the tentative parse. */
15308 else
15310 cp_lexer_rollback_tokens (parser->lexer);
15311 pop_deferring_access_checks ();
15313 /* Add the context to the front of the free list. */
15314 context->next = cp_parser_context_free_list;
15315 cp_parser_context_free_list = context;
15317 return !error_occurred;
15320 /* Returns true if we are parsing tentatively -- but have decided that
15321 we will stick with this tentative parse, even if errors occur. */
15323 static bool
15324 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15326 return (cp_parser_parsing_tentatively (parser)
15327 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15330 /* Returns nonzero iff an error has occurred during the most recent
15331 tentative parse. */
15333 static bool
15334 cp_parser_error_occurred (cp_parser* parser)
15336 return (cp_parser_parsing_tentatively (parser)
15337 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15340 /* Returns nonzero if GNU extensions are allowed. */
15342 static bool
15343 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15345 return parser->allow_gnu_extensions_p;
15349 /* The parser. */
15351 static GTY (()) cp_parser *the_parser;
15353 /* External interface. */
15355 /* Parse one entire translation unit. */
15357 void
15358 c_parse_file (void)
15360 bool error_occurred;
15361 static bool already_called = false;
15363 if (already_called)
15365 sorry ("inter-module optimizations not implemented for C++");
15366 return;
15368 already_called = true;
15370 the_parser = cp_parser_new ();
15371 push_deferring_access_checks (flag_access_control
15372 ? dk_no_deferred : dk_no_check);
15373 error_occurred = cp_parser_translation_unit (the_parser);
15374 the_parser = NULL;
15377 /* This variable must be provided by every front end. */
15379 int yydebug;
15381 #include "gt-cp-parser.h"