class.c (alter_access): Use %E format specifier to print an identifier node.
[official-gcc.git] / gcc / cp / parser.c
blobd26061f87d24310c401a73eb202b22a22f7802a9
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 ("`%E' is not a template", 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,
3846 args, NULL_TREE);
3847 break;
3850 if (BASELINK_P (fn))
3851 postfix_expression
3852 = (build_new_method_call
3853 (instance, fn, args, NULL_TREE,
3854 (idk == CP_ID_KIND_QUALIFIED
3855 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3856 else
3857 postfix_expression
3858 = finish_call_expr (postfix_expression, args,
3859 /*disallow_virtual=*/false,
3860 /*koenig_p=*/false);
3862 else if (TREE_CODE (postfix_expression) == OFFSET_REF
3863 || TREE_CODE (postfix_expression) == MEMBER_REF
3864 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3865 postfix_expression = (build_offset_ref_call_from_tree
3866 (postfix_expression, args));
3867 else if (idk == CP_ID_KIND_QUALIFIED)
3868 /* A call to a static class member, or a namespace-scope
3869 function. */
3870 postfix_expression
3871 = finish_call_expr (postfix_expression, args,
3872 /*disallow_virtual=*/true,
3873 koenig_p);
3874 else
3875 /* All other function calls. */
3876 postfix_expression
3877 = finish_call_expr (postfix_expression, args,
3878 /*disallow_virtual=*/false,
3879 koenig_p);
3881 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3882 idk = CP_ID_KIND_NONE;
3884 break;
3886 case CPP_DOT:
3887 case CPP_DEREF:
3888 /* postfix-expression . template [opt] id-expression
3889 postfix-expression . pseudo-destructor-name
3890 postfix-expression -> template [opt] id-expression
3891 postfix-expression -> pseudo-destructor-name */
3893 tree name;
3894 bool dependent_p;
3895 bool template_p;
3896 tree scope = NULL_TREE;
3897 enum cpp_ttype token_type = token->type;
3899 /* If this is a `->' operator, dereference the pointer. */
3900 if (token->type == CPP_DEREF)
3901 postfix_expression = build_x_arrow (postfix_expression);
3902 /* Check to see whether or not the expression is
3903 type-dependent. */
3904 dependent_p = type_dependent_expression_p (postfix_expression);
3905 /* The identifier following the `->' or `.' is not
3906 qualified. */
3907 parser->scope = NULL_TREE;
3908 parser->qualifying_scope = NULL_TREE;
3909 parser->object_scope = NULL_TREE;
3910 idk = CP_ID_KIND_NONE;
3911 /* Enter the scope corresponding to the type of the object
3912 given by the POSTFIX_EXPRESSION. */
3913 if (!dependent_p
3914 && TREE_TYPE (postfix_expression) != NULL_TREE)
3916 scope = TREE_TYPE (postfix_expression);
3917 /* According to the standard, no expression should
3918 ever have reference type. Unfortunately, we do not
3919 currently match the standard in this respect in
3920 that our internal representation of an expression
3921 may have reference type even when the standard says
3922 it does not. Therefore, we have to manually obtain
3923 the underlying type here. */
3924 scope = non_reference (scope);
3925 /* The type of the POSTFIX_EXPRESSION must be
3926 complete. */
3927 scope = complete_type_or_else (scope, NULL_TREE);
3928 /* Let the name lookup machinery know that we are
3929 processing a class member access expression. */
3930 parser->context->object_type = scope;
3931 /* If something went wrong, we want to be able to
3932 discern that case, as opposed to the case where
3933 there was no SCOPE due to the type of expression
3934 being dependent. */
3935 if (!scope)
3936 scope = error_mark_node;
3937 /* If the SCOPE was erroneous, make the various
3938 semantic analysis functions exit quickly -- and
3939 without issuing additional error messages. */
3940 if (scope == error_mark_node)
3941 postfix_expression = error_mark_node;
3944 /* Consume the `.' or `->' operator. */
3945 cp_lexer_consume_token (parser->lexer);
3946 /* If the SCOPE is not a scalar type, we are looking at an
3947 ordinary class member access expression, rather than a
3948 pseudo-destructor-name. */
3949 if (!scope || !SCALAR_TYPE_P (scope))
3951 template_p = cp_parser_optional_template_keyword (parser);
3952 /* Parse the id-expression. */
3953 name = cp_parser_id_expression (parser,
3954 template_p,
3955 /*check_dependency_p=*/true,
3956 /*template_p=*/NULL,
3957 /*declarator_p=*/false);
3958 /* In general, build a SCOPE_REF if the member name is
3959 qualified. However, if the name was not dependent
3960 and has already been resolved; there is no need to
3961 build the SCOPE_REF. For example;
3963 struct X { void f(); };
3964 template <typename T> void f(T* t) { t->X::f(); }
3966 Even though "t" is dependent, "X::f" is not and has
3967 been resolved to a BASELINK; there is no need to
3968 include scope information. */
3970 /* But we do need to remember that there was an explicit
3971 scope for virtual function calls. */
3972 if (parser->scope)
3973 idk = CP_ID_KIND_QUALIFIED;
3975 if (name != error_mark_node
3976 && !BASELINK_P (name)
3977 && parser->scope)
3979 name = build_nt (SCOPE_REF, parser->scope, name);
3980 parser->scope = NULL_TREE;
3981 parser->qualifying_scope = NULL_TREE;
3982 parser->object_scope = NULL_TREE;
3984 if (scope && name && BASELINK_P (name))
3985 adjust_result_of_qualified_name_lookup
3986 (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3987 postfix_expression
3988 = finish_class_member_access_expr (postfix_expression, name);
3990 /* Otherwise, try the pseudo-destructor-name production. */
3991 else
3993 tree s = NULL_TREE;
3994 tree type;
3996 /* Parse the pseudo-destructor-name. */
3997 cp_parser_pseudo_destructor_name (parser, &s, &type);
3998 /* Form the call. */
3999 postfix_expression
4000 = finish_pseudo_destructor_expr (postfix_expression,
4001 s, TREE_TYPE (type));
4004 /* We no longer need to look up names in the scope of the
4005 object on the left-hand side of the `.' or `->'
4006 operator. */
4007 parser->context->object_type = NULL_TREE;
4008 /* These operators may not appear in constant-expressions. */
4009 if (/* The "->" operator is allowed in the implementation
4010 of "offsetof". The "." operator may appear in the
4011 name of the member. */
4012 !parser->in_offsetof_p
4013 && (cp_parser_non_integral_constant_expression
4014 (parser,
4015 token_type == CPP_DEREF ? "'->'" : "`.'")))
4016 postfix_expression = error_mark_node;
4018 break;
4020 case CPP_PLUS_PLUS:
4021 /* postfix-expression ++ */
4022 /* Consume the `++' token. */
4023 cp_lexer_consume_token (parser->lexer);
4024 /* Generate a representation for the complete expression. */
4025 postfix_expression
4026 = finish_increment_expr (postfix_expression,
4027 POSTINCREMENT_EXPR);
4028 /* Increments may not appear in constant-expressions. */
4029 if (cp_parser_non_integral_constant_expression (parser,
4030 "an increment"))
4031 postfix_expression = error_mark_node;
4032 idk = CP_ID_KIND_NONE;
4033 break;
4035 case CPP_MINUS_MINUS:
4036 /* postfix-expression -- */
4037 /* Consume the `--' token. */
4038 cp_lexer_consume_token (parser->lexer);
4039 /* Generate a representation for the complete expression. */
4040 postfix_expression
4041 = finish_increment_expr (postfix_expression,
4042 POSTDECREMENT_EXPR);
4043 /* Decrements may not appear in constant-expressions. */
4044 if (cp_parser_non_integral_constant_expression (parser,
4045 "a decrement"))
4046 postfix_expression = error_mark_node;
4047 idk = CP_ID_KIND_NONE;
4048 break;
4050 default:
4051 return postfix_expression;
4055 /* We should never get here. */
4056 abort ();
4057 return error_mark_node;
4060 /* Parse a parenthesized expression-list.
4062 expression-list:
4063 assignment-expression
4064 expression-list, assignment-expression
4066 attribute-list:
4067 expression-list
4068 identifier
4069 identifier, expression-list
4071 Returns a TREE_LIST. The TREE_VALUE of each node is a
4072 representation of an assignment-expression. Note that a TREE_LIST
4073 is returned even if there is only a single expression in the list.
4074 error_mark_node is returned if the ( and or ) are
4075 missing. NULL_TREE is returned on no expressions. The parentheses
4076 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4077 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4078 indicates whether or not all of the expressions in the list were
4079 constant. */
4081 static tree
4082 cp_parser_parenthesized_expression_list (cp_parser* parser,
4083 bool is_attribute_list,
4084 bool *non_constant_p)
4086 tree expression_list = NULL_TREE;
4087 tree identifier = NULL_TREE;
4089 /* Assume all the expressions will be constant. */
4090 if (non_constant_p)
4091 *non_constant_p = false;
4093 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4094 return error_mark_node;
4096 /* Consume expressions until there are no more. */
4097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4098 while (true)
4100 tree expr;
4102 /* At the beginning of attribute lists, check to see if the
4103 next token is an identifier. */
4104 if (is_attribute_list
4105 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4107 cp_token *token;
4109 /* Consume the identifier. */
4110 token = cp_lexer_consume_token (parser->lexer);
4111 /* Save the identifier. */
4112 identifier = token->value;
4114 else
4116 /* Parse the next assignment-expression. */
4117 if (non_constant_p)
4119 bool expr_non_constant_p;
4120 expr = (cp_parser_constant_expression
4121 (parser, /*allow_non_constant_p=*/true,
4122 &expr_non_constant_p));
4123 if (expr_non_constant_p)
4124 *non_constant_p = true;
4126 else
4127 expr = cp_parser_assignment_expression (parser);
4129 /* Add it to the list. We add error_mark_node
4130 expressions to the list, so that we can still tell if
4131 the correct form for a parenthesized expression-list
4132 is found. That gives better errors. */
4133 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4135 if (expr == error_mark_node)
4136 goto skip_comma;
4139 /* After the first item, attribute lists look the same as
4140 expression lists. */
4141 is_attribute_list = false;
4143 get_comma:;
4144 /* If the next token isn't a `,', then we are done. */
4145 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4146 break;
4148 /* Otherwise, consume the `,' and keep going. */
4149 cp_lexer_consume_token (parser->lexer);
4152 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4154 int ending;
4156 skip_comma:;
4157 /* We try and resync to an unnested comma, as that will give the
4158 user better diagnostics. */
4159 ending = cp_parser_skip_to_closing_parenthesis (parser,
4160 /*recovering=*/true,
4161 /*or_comma=*/true,
4162 /*consume_paren=*/true);
4163 if (ending < 0)
4164 goto get_comma;
4165 if (!ending)
4166 return error_mark_node;
4169 /* We built up the list in reverse order so we must reverse it now. */
4170 expression_list = nreverse (expression_list);
4171 if (identifier)
4172 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4174 return expression_list;
4177 /* Parse a pseudo-destructor-name.
4179 pseudo-destructor-name:
4180 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4181 :: [opt] nested-name-specifier template template-id :: ~ type-name
4182 :: [opt] nested-name-specifier [opt] ~ type-name
4184 If either of the first two productions is used, sets *SCOPE to the
4185 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4186 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4187 or ERROR_MARK_NODE if the parse fails. */
4189 static void
4190 cp_parser_pseudo_destructor_name (cp_parser* parser,
4191 tree* scope,
4192 tree* type)
4194 bool nested_name_specifier_p;
4196 /* Look for the optional `::' operator. */
4197 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4198 /* Look for the optional nested-name-specifier. */
4199 nested_name_specifier_p
4200 = (cp_parser_nested_name_specifier_opt (parser,
4201 /*typename_keyword_p=*/false,
4202 /*check_dependency_p=*/true,
4203 /*type_p=*/false,
4204 /*is_declaration=*/true)
4205 != NULL_TREE);
4206 /* Now, if we saw a nested-name-specifier, we might be doing the
4207 second production. */
4208 if (nested_name_specifier_p
4209 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4211 /* Consume the `template' keyword. */
4212 cp_lexer_consume_token (parser->lexer);
4213 /* Parse the template-id. */
4214 cp_parser_template_id (parser,
4215 /*template_keyword_p=*/true,
4216 /*check_dependency_p=*/false,
4217 /*is_declaration=*/true);
4218 /* Look for the `::' token. */
4219 cp_parser_require (parser, CPP_SCOPE, "`::'");
4221 /* If the next token is not a `~', then there might be some
4222 additional qualification. */
4223 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4225 /* Look for the type-name. */
4226 *scope = TREE_TYPE (cp_parser_type_name (parser));
4228 /* If we didn't get an aggregate type, or we don't have ::~,
4229 then something has gone wrong. Since the only caller of this
4230 function is looking for something after `.' or `->' after a
4231 scalar type, most likely the program is trying to get a
4232 member of a non-aggregate type. */
4233 if (*scope == error_mark_node
4234 || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4235 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4237 cp_parser_error (parser, "request for member of non-aggregate type");
4238 *type = error_mark_node;
4239 return;
4242 /* Look for the `::' token. */
4243 cp_parser_require (parser, CPP_SCOPE, "`::'");
4245 else
4246 *scope = NULL_TREE;
4248 /* Look for the `~'. */
4249 cp_parser_require (parser, CPP_COMPL, "`~'");
4250 /* Look for the type-name again. We are not responsible for
4251 checking that it matches the first type-name. */
4252 *type = cp_parser_type_name (parser);
4255 /* Parse a unary-expression.
4257 unary-expression:
4258 postfix-expression
4259 ++ cast-expression
4260 -- cast-expression
4261 unary-operator cast-expression
4262 sizeof unary-expression
4263 sizeof ( type-id )
4264 new-expression
4265 delete-expression
4267 GNU Extensions:
4269 unary-expression:
4270 __extension__ cast-expression
4271 __alignof__ unary-expression
4272 __alignof__ ( type-id )
4273 __real__ cast-expression
4274 __imag__ cast-expression
4275 && identifier
4277 ADDRESS_P is true iff the unary-expression is appearing as the
4278 operand of the `&' operator.
4280 Returns a representation of the expression. */
4282 static tree
4283 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4285 cp_token *token;
4286 enum tree_code unary_operator;
4288 /* Peek at the next token. */
4289 token = cp_lexer_peek_token (parser->lexer);
4290 /* Some keywords give away the kind of expression. */
4291 if (token->type == CPP_KEYWORD)
4293 enum rid keyword = token->keyword;
4295 switch (keyword)
4297 case RID_ALIGNOF:
4298 case RID_SIZEOF:
4300 tree operand;
4301 enum tree_code op;
4303 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4304 /* Consume the token. */
4305 cp_lexer_consume_token (parser->lexer);
4306 /* Parse the operand. */
4307 operand = cp_parser_sizeof_operand (parser, keyword);
4309 if (TYPE_P (operand))
4310 return cxx_sizeof_or_alignof_type (operand, op, true);
4311 else
4312 return cxx_sizeof_or_alignof_expr (operand, op);
4315 case RID_NEW:
4316 return cp_parser_new_expression (parser);
4318 case RID_DELETE:
4319 return cp_parser_delete_expression (parser);
4321 case RID_EXTENSION:
4323 /* The saved value of the PEDANTIC flag. */
4324 int saved_pedantic;
4325 tree expr;
4327 /* Save away the PEDANTIC flag. */
4328 cp_parser_extension_opt (parser, &saved_pedantic);
4329 /* Parse the cast-expression. */
4330 expr = cp_parser_simple_cast_expression (parser);
4331 /* Restore the PEDANTIC flag. */
4332 pedantic = saved_pedantic;
4334 return expr;
4337 case RID_REALPART:
4338 case RID_IMAGPART:
4340 tree expression;
4342 /* Consume the `__real__' or `__imag__' token. */
4343 cp_lexer_consume_token (parser->lexer);
4344 /* Parse the cast-expression. */
4345 expression = cp_parser_simple_cast_expression (parser);
4346 /* Create the complete representation. */
4347 return build_x_unary_op ((keyword == RID_REALPART
4348 ? REALPART_EXPR : IMAGPART_EXPR),
4349 expression);
4351 break;
4353 default:
4354 break;
4358 /* Look for the `:: new' and `:: delete', which also signal the
4359 beginning of a new-expression, or delete-expression,
4360 respectively. If the next token is `::', then it might be one of
4361 these. */
4362 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4364 enum rid keyword;
4366 /* See if the token after the `::' is one of the keywords in
4367 which we're interested. */
4368 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4369 /* If it's `new', we have a new-expression. */
4370 if (keyword == RID_NEW)
4371 return cp_parser_new_expression (parser);
4372 /* Similarly, for `delete'. */
4373 else if (keyword == RID_DELETE)
4374 return cp_parser_delete_expression (parser);
4377 /* Look for a unary operator. */
4378 unary_operator = cp_parser_unary_operator (token);
4379 /* The `++' and `--' operators can be handled similarly, even though
4380 they are not technically unary-operators in the grammar. */
4381 if (unary_operator == ERROR_MARK)
4383 if (token->type == CPP_PLUS_PLUS)
4384 unary_operator = PREINCREMENT_EXPR;
4385 else if (token->type == CPP_MINUS_MINUS)
4386 unary_operator = PREDECREMENT_EXPR;
4387 /* Handle the GNU address-of-label extension. */
4388 else if (cp_parser_allow_gnu_extensions_p (parser)
4389 && token->type == CPP_AND_AND)
4391 tree identifier;
4393 /* Consume the '&&' token. */
4394 cp_lexer_consume_token (parser->lexer);
4395 /* Look for the identifier. */
4396 identifier = cp_parser_identifier (parser);
4397 /* Create an expression representing the address. */
4398 return finish_label_address_expr (identifier);
4401 if (unary_operator != ERROR_MARK)
4403 tree cast_expression;
4404 tree expression = error_mark_node;
4405 const char *non_constant_p = NULL;
4407 /* Consume the operator token. */
4408 token = cp_lexer_consume_token (parser->lexer);
4409 /* Parse the cast-expression. */
4410 cast_expression
4411 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4412 /* Now, build an appropriate representation. */
4413 switch (unary_operator)
4415 case INDIRECT_REF:
4416 non_constant_p = "`*'";
4417 expression = build_x_indirect_ref (cast_expression, "unary *");
4418 break;
4420 case ADDR_EXPR:
4421 /* The "&" operator is allowed in the implementation of
4422 "offsetof". */
4423 if (!parser->in_offsetof_p)
4424 non_constant_p = "`&'";
4425 /* Fall through. */
4426 case BIT_NOT_EXPR:
4427 expression = build_x_unary_op (unary_operator, cast_expression);
4428 break;
4430 case PREINCREMENT_EXPR:
4431 case PREDECREMENT_EXPR:
4432 non_constant_p = (unary_operator == PREINCREMENT_EXPR
4433 ? "`++'" : "`--'");
4434 /* Fall through. */
4435 case CONVERT_EXPR:
4436 case NEGATE_EXPR:
4437 case TRUTH_NOT_EXPR:
4438 expression = finish_unary_op_expr (unary_operator, cast_expression);
4439 break;
4441 default:
4442 abort ();
4445 if (non_constant_p
4446 && cp_parser_non_integral_constant_expression (parser,
4447 non_constant_p))
4448 expression = error_mark_node;
4450 return expression;
4453 return cp_parser_postfix_expression (parser, address_p);
4456 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4457 unary-operator, the corresponding tree code is returned. */
4459 static enum tree_code
4460 cp_parser_unary_operator (cp_token* token)
4462 switch (token->type)
4464 case CPP_MULT:
4465 return INDIRECT_REF;
4467 case CPP_AND:
4468 return ADDR_EXPR;
4470 case CPP_PLUS:
4471 return CONVERT_EXPR;
4473 case CPP_MINUS:
4474 return NEGATE_EXPR;
4476 case CPP_NOT:
4477 return TRUTH_NOT_EXPR;
4479 case CPP_COMPL:
4480 return BIT_NOT_EXPR;
4482 default:
4483 return ERROR_MARK;
4487 /* Parse a new-expression.
4489 new-expression:
4490 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4491 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4493 Returns a representation of the expression. */
4495 static tree
4496 cp_parser_new_expression (cp_parser* parser)
4498 bool global_scope_p;
4499 tree placement;
4500 tree type;
4501 tree initializer;
4503 /* Look for the optional `::' operator. */
4504 global_scope_p
4505 = (cp_parser_global_scope_opt (parser,
4506 /*current_scope_valid_p=*/false)
4507 != NULL_TREE);
4508 /* Look for the `new' operator. */
4509 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4510 /* There's no easy way to tell a new-placement from the
4511 `( type-id )' construct. */
4512 cp_parser_parse_tentatively (parser);
4513 /* Look for a new-placement. */
4514 placement = cp_parser_new_placement (parser);
4515 /* If that didn't work out, there's no new-placement. */
4516 if (!cp_parser_parse_definitely (parser))
4517 placement = NULL_TREE;
4519 /* If the next token is a `(', then we have a parenthesized
4520 type-id. */
4521 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4523 /* Consume the `('. */
4524 cp_lexer_consume_token (parser->lexer);
4525 /* Parse the type-id. */
4526 type = cp_parser_type_id (parser);
4527 /* Look for the closing `)'. */
4528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4529 /* There should not be a direct-new-declarator in this production,
4530 but GCC used to allowed this, so we check and emit a sensible error
4531 message for this case. */
4532 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4534 error ("array bound forbidden after parenthesized type-id");
4535 inform ("try removing the parentheses around the type-id");
4536 cp_parser_direct_new_declarator (parser);
4539 /* Otherwise, there must be a new-type-id. */
4540 else
4541 type = cp_parser_new_type_id (parser);
4543 /* If the next token is a `(', then we have a new-initializer. */
4544 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4545 initializer = cp_parser_new_initializer (parser);
4546 else
4547 initializer = NULL_TREE;
4549 /* A new-expression may not appear in an integral constant
4550 expression. */
4551 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4552 return error_mark_node;
4554 /* Create a representation of the new-expression. */
4555 return build_new (placement, type, initializer, global_scope_p);
4558 /* Parse a new-placement.
4560 new-placement:
4561 ( expression-list )
4563 Returns the same representation as for an expression-list. */
4565 static tree
4566 cp_parser_new_placement (cp_parser* parser)
4568 tree expression_list;
4570 /* Parse the expression-list. */
4571 expression_list = (cp_parser_parenthesized_expression_list
4572 (parser, false, /*non_constant_p=*/NULL));
4574 return expression_list;
4577 /* Parse a new-type-id.
4579 new-type-id:
4580 type-specifier-seq new-declarator [opt]
4582 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4583 and whose TREE_VALUE is the new-declarator. */
4585 static tree
4586 cp_parser_new_type_id (cp_parser* parser)
4588 tree type_specifier_seq;
4589 tree declarator;
4590 const char *saved_message;
4592 /* The type-specifier sequence must not contain type definitions.
4593 (It cannot contain declarations of new types either, but if they
4594 are not definitions we will catch that because they are not
4595 complete.) */
4596 saved_message = parser->type_definition_forbidden_message;
4597 parser->type_definition_forbidden_message
4598 = "types may not be defined in a new-type-id";
4599 /* Parse the type-specifier-seq. */
4600 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4601 /* Restore the old message. */
4602 parser->type_definition_forbidden_message = saved_message;
4603 /* Parse the new-declarator. */
4604 declarator = cp_parser_new_declarator_opt (parser);
4606 return build_tree_list (type_specifier_seq, declarator);
4609 /* Parse an (optional) new-declarator.
4611 new-declarator:
4612 ptr-operator new-declarator [opt]
4613 direct-new-declarator
4615 Returns a representation of the declarator. See
4616 cp_parser_declarator for the representations used. */
4618 static tree
4619 cp_parser_new_declarator_opt (cp_parser* parser)
4621 enum tree_code code;
4622 tree type;
4623 tree cv_qualifier_seq;
4625 /* We don't know if there's a ptr-operator next, or not. */
4626 cp_parser_parse_tentatively (parser);
4627 /* Look for a ptr-operator. */
4628 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4629 /* If that worked, look for more new-declarators. */
4630 if (cp_parser_parse_definitely (parser))
4632 tree declarator;
4634 /* Parse another optional declarator. */
4635 declarator = cp_parser_new_declarator_opt (parser);
4637 /* Create the representation of the declarator. */
4638 if (code == INDIRECT_REF)
4639 declarator = make_pointer_declarator (cv_qualifier_seq,
4640 declarator);
4641 else
4642 declarator = make_reference_declarator (cv_qualifier_seq,
4643 declarator);
4645 /* Handle the pointer-to-member case. */
4646 if (type)
4647 declarator = build_nt (SCOPE_REF, type, declarator);
4649 return declarator;
4652 /* If the next token is a `[', there is a direct-new-declarator. */
4653 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4654 return cp_parser_direct_new_declarator (parser);
4656 return NULL_TREE;
4659 /* Parse a direct-new-declarator.
4661 direct-new-declarator:
4662 [ expression ]
4663 direct-new-declarator [constant-expression]
4665 Returns an ARRAY_REF, following the same conventions as are
4666 documented for cp_parser_direct_declarator. */
4668 static tree
4669 cp_parser_direct_new_declarator (cp_parser* parser)
4671 tree declarator = NULL_TREE;
4673 while (true)
4675 tree expression;
4677 /* Look for the opening `['. */
4678 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4679 /* The first expression is not required to be constant. */
4680 if (!declarator)
4682 expression = cp_parser_expression (parser);
4683 /* The standard requires that the expression have integral
4684 type. DR 74 adds enumeration types. We believe that the
4685 real intent is that these expressions be handled like the
4686 expression in a `switch' condition, which also allows
4687 classes with a single conversion to integral or
4688 enumeration type. */
4689 if (!processing_template_decl)
4691 expression
4692 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4693 expression,
4694 /*complain=*/true);
4695 if (!expression)
4697 error ("expression in new-declarator must have integral or enumeration type");
4698 expression = error_mark_node;
4702 /* But all the other expressions must be. */
4703 else
4704 expression
4705 = cp_parser_constant_expression (parser,
4706 /*allow_non_constant=*/false,
4707 NULL);
4708 /* Look for the closing `]'. */
4709 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4711 /* Add this bound to the declarator. */
4712 declarator = build_nt (ARRAY_REF, declarator, expression);
4714 /* If the next token is not a `[', then there are no more
4715 bounds. */
4716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4717 break;
4720 return declarator;
4723 /* Parse a new-initializer.
4725 new-initializer:
4726 ( expression-list [opt] )
4728 Returns a representation of the expression-list. If there is no
4729 expression-list, VOID_ZERO_NODE is returned. */
4731 static tree
4732 cp_parser_new_initializer (cp_parser* parser)
4734 tree expression_list;
4736 expression_list = (cp_parser_parenthesized_expression_list
4737 (parser, false, /*non_constant_p=*/NULL));
4738 if (!expression_list)
4739 expression_list = void_zero_node;
4741 return expression_list;
4744 /* Parse a delete-expression.
4746 delete-expression:
4747 :: [opt] delete cast-expression
4748 :: [opt] delete [ ] cast-expression
4750 Returns a representation of the expression. */
4752 static tree
4753 cp_parser_delete_expression (cp_parser* parser)
4755 bool global_scope_p;
4756 bool array_p;
4757 tree expression;
4759 /* Look for the optional `::' operator. */
4760 global_scope_p
4761 = (cp_parser_global_scope_opt (parser,
4762 /*current_scope_valid_p=*/false)
4763 != NULL_TREE);
4764 /* Look for the `delete' keyword. */
4765 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4766 /* See if the array syntax is in use. */
4767 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4769 /* Consume the `[' token. */
4770 cp_lexer_consume_token (parser->lexer);
4771 /* Look for the `]' token. */
4772 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4773 /* Remember that this is the `[]' construct. */
4774 array_p = true;
4776 else
4777 array_p = false;
4779 /* Parse the cast-expression. */
4780 expression = cp_parser_simple_cast_expression (parser);
4782 /* A delete-expression may not appear in an integral constant
4783 expression. */
4784 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4785 return error_mark_node;
4787 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4790 /* Parse a cast-expression.
4792 cast-expression:
4793 unary-expression
4794 ( type-id ) cast-expression
4796 Returns a representation of the expression. */
4798 static tree
4799 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4801 /* If it's a `(', then we might be looking at a cast. */
4802 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4804 tree type = NULL_TREE;
4805 tree expr = NULL_TREE;
4806 bool compound_literal_p;
4807 const char *saved_message;
4809 /* There's no way to know yet whether or not this is a cast.
4810 For example, `(int (3))' is a unary-expression, while `(int)
4811 3' is a cast. So, we resort to parsing tentatively. */
4812 cp_parser_parse_tentatively (parser);
4813 /* Types may not be defined in a cast. */
4814 saved_message = parser->type_definition_forbidden_message;
4815 parser->type_definition_forbidden_message
4816 = "types may not be defined in casts";
4817 /* Consume the `('. */
4818 cp_lexer_consume_token (parser->lexer);
4819 /* A very tricky bit is that `(struct S) { 3 }' is a
4820 compound-literal (which we permit in C++ as an extension).
4821 But, that construct is not a cast-expression -- it is a
4822 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4823 is legal; if the compound-literal were a cast-expression,
4824 you'd need an extra set of parentheses.) But, if we parse
4825 the type-id, and it happens to be a class-specifier, then we
4826 will commit to the parse at that point, because we cannot
4827 undo the action that is done when creating a new class. So,
4828 then we cannot back up and do a postfix-expression.
4830 Therefore, we scan ahead to the closing `)', and check to see
4831 if the token after the `)' is a `{'. If so, we are not
4832 looking at a cast-expression.
4834 Save tokens so that we can put them back. */
4835 cp_lexer_save_tokens (parser->lexer);
4836 /* Skip tokens until the next token is a closing parenthesis.
4837 If we find the closing `)', and the next token is a `{', then
4838 we are looking at a compound-literal. */
4839 compound_literal_p
4840 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4841 /*consume_paren=*/true)
4842 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4843 /* Roll back the tokens we skipped. */
4844 cp_lexer_rollback_tokens (parser->lexer);
4845 /* If we were looking at a compound-literal, simulate an error
4846 so that the call to cp_parser_parse_definitely below will
4847 fail. */
4848 if (compound_literal_p)
4849 cp_parser_simulate_error (parser);
4850 else
4852 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4853 parser->in_type_id_in_expr_p = true;
4854 /* Look for the type-id. */
4855 type = cp_parser_type_id (parser);
4856 /* Look for the closing `)'. */
4857 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4858 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4861 /* Restore the saved message. */
4862 parser->type_definition_forbidden_message = saved_message;
4864 /* If ok so far, parse the dependent expression. We cannot be
4865 sure it is a cast. Consider `(T ())'. It is a parenthesized
4866 ctor of T, but looks like a cast to function returning T
4867 without a dependent expression. */
4868 if (!cp_parser_error_occurred (parser))
4869 expr = cp_parser_simple_cast_expression (parser);
4871 if (cp_parser_parse_definitely (parser))
4873 /* Warn about old-style casts, if so requested. */
4874 if (warn_old_style_cast
4875 && !in_system_header
4876 && !VOID_TYPE_P (type)
4877 && current_lang_name != lang_name_c)
4878 warning ("use of old-style cast");
4880 /* Only type conversions to integral or enumeration types
4881 can be used in constant-expressions. */
4882 if (parser->integral_constant_expression_p
4883 && !dependent_type_p (type)
4884 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4885 && (cp_parser_non_integral_constant_expression
4886 (parser,
4887 "a cast to a type other than an integral or "
4888 "enumeration type")))
4889 return error_mark_node;
4891 /* Perform the cast. */
4892 expr = build_c_cast (type, expr);
4893 return expr;
4897 /* If we get here, then it's not a cast, so it must be a
4898 unary-expression. */
4899 return cp_parser_unary_expression (parser, address_p);
4902 /* Parse a pm-expression.
4904 pm-expression:
4905 cast-expression
4906 pm-expression .* cast-expression
4907 pm-expression ->* cast-expression
4909 Returns a representation of the expression. */
4911 static tree
4912 cp_parser_pm_expression (cp_parser* parser)
4914 static const cp_parser_token_tree_map map = {
4915 { CPP_DEREF_STAR, MEMBER_REF },
4916 { CPP_DOT_STAR, DOTSTAR_EXPR },
4917 { CPP_EOF, ERROR_MARK }
4920 return cp_parser_binary_expression (parser, map,
4921 cp_parser_simple_cast_expression);
4924 /* Parse a multiplicative-expression.
4926 multiplicative-expression:
4927 pm-expression
4928 multiplicative-expression * pm-expression
4929 multiplicative-expression / pm-expression
4930 multiplicative-expression % pm-expression
4932 Returns a representation of the expression. */
4934 static tree
4935 cp_parser_multiplicative_expression (cp_parser* parser)
4937 static const cp_parser_token_tree_map map = {
4938 { CPP_MULT, MULT_EXPR },
4939 { CPP_DIV, TRUNC_DIV_EXPR },
4940 { CPP_MOD, TRUNC_MOD_EXPR },
4941 { CPP_EOF, ERROR_MARK }
4944 return cp_parser_binary_expression (parser,
4945 map,
4946 cp_parser_pm_expression);
4949 /* Parse an additive-expression.
4951 additive-expression:
4952 multiplicative-expression
4953 additive-expression + multiplicative-expression
4954 additive-expression - multiplicative-expression
4956 Returns a representation of the expression. */
4958 static tree
4959 cp_parser_additive_expression (cp_parser* parser)
4961 static const cp_parser_token_tree_map map = {
4962 { CPP_PLUS, PLUS_EXPR },
4963 { CPP_MINUS, MINUS_EXPR },
4964 { CPP_EOF, ERROR_MARK }
4967 return cp_parser_binary_expression (parser,
4968 map,
4969 cp_parser_multiplicative_expression);
4972 /* Parse a shift-expression.
4974 shift-expression:
4975 additive-expression
4976 shift-expression << additive-expression
4977 shift-expression >> additive-expression
4979 Returns a representation of the expression. */
4981 static tree
4982 cp_parser_shift_expression (cp_parser* parser)
4984 static const cp_parser_token_tree_map map = {
4985 { CPP_LSHIFT, LSHIFT_EXPR },
4986 { CPP_RSHIFT, RSHIFT_EXPR },
4987 { CPP_EOF, ERROR_MARK }
4990 return cp_parser_binary_expression (parser,
4991 map,
4992 cp_parser_additive_expression);
4995 /* Parse a relational-expression.
4997 relational-expression:
4998 shift-expression
4999 relational-expression < shift-expression
5000 relational-expression > shift-expression
5001 relational-expression <= shift-expression
5002 relational-expression >= shift-expression
5004 GNU Extension:
5006 relational-expression:
5007 relational-expression <? shift-expression
5008 relational-expression >? shift-expression
5010 Returns a representation of the expression. */
5012 static tree
5013 cp_parser_relational_expression (cp_parser* parser)
5015 static const cp_parser_token_tree_map map = {
5016 { CPP_LESS, LT_EXPR },
5017 { CPP_GREATER, GT_EXPR },
5018 { CPP_LESS_EQ, LE_EXPR },
5019 { CPP_GREATER_EQ, GE_EXPR },
5020 { CPP_MIN, MIN_EXPR },
5021 { CPP_MAX, MAX_EXPR },
5022 { CPP_EOF, ERROR_MARK }
5025 return cp_parser_binary_expression (parser,
5026 map,
5027 cp_parser_shift_expression);
5030 /* Parse an equality-expression.
5032 equality-expression:
5033 relational-expression
5034 equality-expression == relational-expression
5035 equality-expression != relational-expression
5037 Returns a representation of the expression. */
5039 static tree
5040 cp_parser_equality_expression (cp_parser* parser)
5042 static const cp_parser_token_tree_map map = {
5043 { CPP_EQ_EQ, EQ_EXPR },
5044 { CPP_NOT_EQ, NE_EXPR },
5045 { CPP_EOF, ERROR_MARK }
5048 return cp_parser_binary_expression (parser,
5049 map,
5050 cp_parser_relational_expression);
5053 /* Parse an and-expression.
5055 and-expression:
5056 equality-expression
5057 and-expression & equality-expression
5059 Returns a representation of the expression. */
5061 static tree
5062 cp_parser_and_expression (cp_parser* parser)
5064 static const cp_parser_token_tree_map map = {
5065 { CPP_AND, BIT_AND_EXPR },
5066 { CPP_EOF, ERROR_MARK }
5069 return cp_parser_binary_expression (parser,
5070 map,
5071 cp_parser_equality_expression);
5074 /* Parse an exclusive-or-expression.
5076 exclusive-or-expression:
5077 and-expression
5078 exclusive-or-expression ^ and-expression
5080 Returns a representation of the expression. */
5082 static tree
5083 cp_parser_exclusive_or_expression (cp_parser* parser)
5085 static const cp_parser_token_tree_map map = {
5086 { CPP_XOR, BIT_XOR_EXPR },
5087 { CPP_EOF, ERROR_MARK }
5090 return cp_parser_binary_expression (parser,
5091 map,
5092 cp_parser_and_expression);
5096 /* Parse an inclusive-or-expression.
5098 inclusive-or-expression:
5099 exclusive-or-expression
5100 inclusive-or-expression | exclusive-or-expression
5102 Returns a representation of the expression. */
5104 static tree
5105 cp_parser_inclusive_or_expression (cp_parser* parser)
5107 static const cp_parser_token_tree_map map = {
5108 { CPP_OR, BIT_IOR_EXPR },
5109 { CPP_EOF, ERROR_MARK }
5112 return cp_parser_binary_expression (parser,
5113 map,
5114 cp_parser_exclusive_or_expression);
5117 /* Parse a logical-and-expression.
5119 logical-and-expression:
5120 inclusive-or-expression
5121 logical-and-expression && inclusive-or-expression
5123 Returns a representation of the expression. */
5125 static tree
5126 cp_parser_logical_and_expression (cp_parser* parser)
5128 static const cp_parser_token_tree_map map = {
5129 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5130 { CPP_EOF, ERROR_MARK }
5133 return cp_parser_binary_expression (parser,
5134 map,
5135 cp_parser_inclusive_or_expression);
5138 /* Parse a logical-or-expression.
5140 logical-or-expression:
5141 logical-and-expression
5142 logical-or-expression || logical-and-expression
5144 Returns a representation of the expression. */
5146 static tree
5147 cp_parser_logical_or_expression (cp_parser* parser)
5149 static const cp_parser_token_tree_map map = {
5150 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5151 { CPP_EOF, ERROR_MARK }
5154 return cp_parser_binary_expression (parser,
5155 map,
5156 cp_parser_logical_and_expression);
5159 /* Parse the `? expression : assignment-expression' part of a
5160 conditional-expression. The LOGICAL_OR_EXPR is the
5161 logical-or-expression that started the conditional-expression.
5162 Returns a representation of the entire conditional-expression.
5164 This routine is used by cp_parser_assignment_expression.
5166 ? expression : assignment-expression
5168 GNU Extensions:
5170 ? : assignment-expression */
5172 static tree
5173 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5175 tree expr;
5176 tree assignment_expr;
5178 /* Consume the `?' token. */
5179 cp_lexer_consume_token (parser->lexer);
5180 if (cp_parser_allow_gnu_extensions_p (parser)
5181 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5182 /* Implicit true clause. */
5183 expr = NULL_TREE;
5184 else
5185 /* Parse the expression. */
5186 expr = cp_parser_expression (parser);
5188 /* The next token should be a `:'. */
5189 cp_parser_require (parser, CPP_COLON, "`:'");
5190 /* Parse the assignment-expression. */
5191 assignment_expr = cp_parser_assignment_expression (parser);
5193 /* Build the conditional-expression. */
5194 return build_x_conditional_expr (logical_or_expr,
5195 expr,
5196 assignment_expr);
5199 /* Parse an assignment-expression.
5201 assignment-expression:
5202 conditional-expression
5203 logical-or-expression assignment-operator assignment_expression
5204 throw-expression
5206 Returns a representation for the expression. */
5208 static tree
5209 cp_parser_assignment_expression (cp_parser* parser)
5211 tree expr;
5213 /* If the next token is the `throw' keyword, then we're looking at
5214 a throw-expression. */
5215 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5216 expr = cp_parser_throw_expression (parser);
5217 /* Otherwise, it must be that we are looking at a
5218 logical-or-expression. */
5219 else
5221 /* Parse the logical-or-expression. */
5222 expr = cp_parser_logical_or_expression (parser);
5223 /* If the next token is a `?' then we're actually looking at a
5224 conditional-expression. */
5225 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5226 return cp_parser_question_colon_clause (parser, expr);
5227 else
5229 enum tree_code assignment_operator;
5231 /* If it's an assignment-operator, we're using the second
5232 production. */
5233 assignment_operator
5234 = cp_parser_assignment_operator_opt (parser);
5235 if (assignment_operator != ERROR_MARK)
5237 tree rhs;
5239 /* Parse the right-hand side of the assignment. */
5240 rhs = cp_parser_assignment_expression (parser);
5241 /* An assignment may not appear in a
5242 constant-expression. */
5243 if (cp_parser_non_integral_constant_expression (parser,
5244 "an assignment"))
5245 return error_mark_node;
5246 /* Build the assignment expression. */
5247 expr = build_x_modify_expr (expr,
5248 assignment_operator,
5249 rhs);
5254 return expr;
5257 /* Parse an (optional) assignment-operator.
5259 assignment-operator: one of
5260 = *= /= %= += -= >>= <<= &= ^= |=
5262 GNU Extension:
5264 assignment-operator: one of
5265 <?= >?=
5267 If the next token is an assignment operator, the corresponding tree
5268 code is returned, and the token is consumed. For example, for
5269 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5270 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5271 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5272 operator, ERROR_MARK is returned. */
5274 static enum tree_code
5275 cp_parser_assignment_operator_opt (cp_parser* parser)
5277 enum tree_code op;
5278 cp_token *token;
5280 /* Peek at the next toen. */
5281 token = cp_lexer_peek_token (parser->lexer);
5283 switch (token->type)
5285 case CPP_EQ:
5286 op = NOP_EXPR;
5287 break;
5289 case CPP_MULT_EQ:
5290 op = MULT_EXPR;
5291 break;
5293 case CPP_DIV_EQ:
5294 op = TRUNC_DIV_EXPR;
5295 break;
5297 case CPP_MOD_EQ:
5298 op = TRUNC_MOD_EXPR;
5299 break;
5301 case CPP_PLUS_EQ:
5302 op = PLUS_EXPR;
5303 break;
5305 case CPP_MINUS_EQ:
5306 op = MINUS_EXPR;
5307 break;
5309 case CPP_RSHIFT_EQ:
5310 op = RSHIFT_EXPR;
5311 break;
5313 case CPP_LSHIFT_EQ:
5314 op = LSHIFT_EXPR;
5315 break;
5317 case CPP_AND_EQ:
5318 op = BIT_AND_EXPR;
5319 break;
5321 case CPP_XOR_EQ:
5322 op = BIT_XOR_EXPR;
5323 break;
5325 case CPP_OR_EQ:
5326 op = BIT_IOR_EXPR;
5327 break;
5329 case CPP_MIN_EQ:
5330 op = MIN_EXPR;
5331 break;
5333 case CPP_MAX_EQ:
5334 op = MAX_EXPR;
5335 break;
5337 default:
5338 /* Nothing else is an assignment operator. */
5339 op = ERROR_MARK;
5342 /* If it was an assignment operator, consume it. */
5343 if (op != ERROR_MARK)
5344 cp_lexer_consume_token (parser->lexer);
5346 return op;
5349 /* Parse an expression.
5351 expression:
5352 assignment-expression
5353 expression , assignment-expression
5355 Returns a representation of the expression. */
5357 static tree
5358 cp_parser_expression (cp_parser* parser)
5360 tree expression = NULL_TREE;
5362 while (true)
5364 tree assignment_expression;
5366 /* Parse the next assignment-expression. */
5367 assignment_expression
5368 = cp_parser_assignment_expression (parser);
5369 /* If this is the first assignment-expression, we can just
5370 save it away. */
5371 if (!expression)
5372 expression = assignment_expression;
5373 else
5374 expression = build_x_compound_expr (expression,
5375 assignment_expression);
5376 /* If the next token is not a comma, then we are done with the
5377 expression. */
5378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5379 break;
5380 /* Consume the `,'. */
5381 cp_lexer_consume_token (parser->lexer);
5382 /* A comma operator cannot appear in a constant-expression. */
5383 if (cp_parser_non_integral_constant_expression (parser,
5384 "a comma operator"))
5385 expression = error_mark_node;
5388 return expression;
5391 /* Parse a constant-expression.
5393 constant-expression:
5394 conditional-expression
5396 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5397 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
5398 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
5399 is false, NON_CONSTANT_P should be NULL. */
5401 static tree
5402 cp_parser_constant_expression (cp_parser* parser,
5403 bool allow_non_constant_p,
5404 bool *non_constant_p)
5406 bool saved_integral_constant_expression_p;
5407 bool saved_allow_non_integral_constant_expression_p;
5408 bool saved_non_integral_constant_expression_p;
5409 tree expression;
5411 /* It might seem that we could simply parse the
5412 conditional-expression, and then check to see if it were
5413 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5414 one that the compiler can figure out is constant, possibly after
5415 doing some simplifications or optimizations. The standard has a
5416 precise definition of constant-expression, and we must honor
5417 that, even though it is somewhat more restrictive.
5419 For example:
5421 int i[(2, 3)];
5423 is not a legal declaration, because `(2, 3)' is not a
5424 constant-expression. The `,' operator is forbidden in a
5425 constant-expression. However, GCC's constant-folding machinery
5426 will fold this operation to an INTEGER_CST for `3'. */
5428 /* Save the old settings. */
5429 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5430 saved_allow_non_integral_constant_expression_p
5431 = parser->allow_non_integral_constant_expression_p;
5432 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5433 /* We are now parsing a constant-expression. */
5434 parser->integral_constant_expression_p = true;
5435 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5436 parser->non_integral_constant_expression_p = false;
5437 /* Although the grammar says "conditional-expression", we parse an
5438 "assignment-expression", which also permits "throw-expression"
5439 and the use of assignment operators. In the case that
5440 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5441 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
5442 actually essential that we look for an assignment-expression.
5443 For example, cp_parser_initializer_clauses uses this function to
5444 determine whether a particular assignment-expression is in fact
5445 constant. */
5446 expression = cp_parser_assignment_expression (parser);
5447 /* Restore the old settings. */
5448 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5449 parser->allow_non_integral_constant_expression_p
5450 = saved_allow_non_integral_constant_expression_p;
5451 if (allow_non_constant_p)
5452 *non_constant_p = parser->non_integral_constant_expression_p;
5453 parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5455 return expression;
5458 /* Statements [gram.stmt.stmt] */
5460 /* Parse a statement.
5462 statement:
5463 labeled-statement
5464 expression-statement
5465 compound-statement
5466 selection-statement
5467 iteration-statement
5468 jump-statement
5469 declaration-statement
5470 try-block */
5472 static void
5473 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5475 tree statement;
5476 cp_token *token;
5477 location_t statement_locus;
5479 /* There is no statement yet. */
5480 statement = NULL_TREE;
5481 /* Peek at the next token. */
5482 token = cp_lexer_peek_token (parser->lexer);
5483 /* Remember the location of the first token in the statement. */
5484 statement_locus = token->location;
5485 /* If this is a keyword, then that will often determine what kind of
5486 statement we have. */
5487 if (token->type == CPP_KEYWORD)
5489 enum rid keyword = token->keyword;
5491 switch (keyword)
5493 case RID_CASE:
5494 case RID_DEFAULT:
5495 statement = cp_parser_labeled_statement (parser,
5496 in_statement_expr_p);
5497 break;
5499 case RID_IF:
5500 case RID_SWITCH:
5501 statement = cp_parser_selection_statement (parser);
5502 break;
5504 case RID_WHILE:
5505 case RID_DO:
5506 case RID_FOR:
5507 statement = cp_parser_iteration_statement (parser);
5508 break;
5510 case RID_BREAK:
5511 case RID_CONTINUE:
5512 case RID_RETURN:
5513 case RID_GOTO:
5514 statement = cp_parser_jump_statement (parser);
5515 break;
5517 case RID_TRY:
5518 statement = cp_parser_try_block (parser);
5519 break;
5521 default:
5522 /* It might be a keyword like `int' that can start a
5523 declaration-statement. */
5524 break;
5527 else if (token->type == CPP_NAME)
5529 /* If the next token is a `:', then we are looking at a
5530 labeled-statement. */
5531 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5532 if (token->type == CPP_COLON)
5533 statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5535 /* Anything that starts with a `{' must be a compound-statement. */
5536 else if (token->type == CPP_OPEN_BRACE)
5537 statement = cp_parser_compound_statement (parser, false);
5539 /* Everything else must be a declaration-statement or an
5540 expression-statement. Try for the declaration-statement
5541 first, unless we are looking at a `;', in which case we know that
5542 we have an expression-statement. */
5543 if (!statement)
5545 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5547 cp_parser_parse_tentatively (parser);
5548 /* Try to parse the declaration-statement. */
5549 cp_parser_declaration_statement (parser);
5550 /* If that worked, we're done. */
5551 if (cp_parser_parse_definitely (parser))
5552 return;
5554 /* Look for an expression-statement instead. */
5555 statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5558 /* Set the line number for the statement. */
5559 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5561 SET_EXPR_LOCUS (statement, NULL);
5562 annotate_with_locus (statement, statement_locus);
5566 /* Parse a labeled-statement.
5568 labeled-statement:
5569 identifier : statement
5570 case constant-expression : statement
5571 default : statement
5573 GNU Extension:
5575 labeled-statement:
5576 case constant-expression ... constant-expression : statement
5578 Returns the new CASE_LABEL, for a `case' or `default' label. For
5579 an ordinary label, returns a LABEL_STMT. */
5581 static tree
5582 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5584 cp_token *token;
5585 tree statement = error_mark_node;
5587 /* The next token should be an identifier. */
5588 token = cp_lexer_peek_token (parser->lexer);
5589 if (token->type != CPP_NAME
5590 && token->type != CPP_KEYWORD)
5592 cp_parser_error (parser, "expected labeled-statement");
5593 return error_mark_node;
5596 switch (token->keyword)
5598 case RID_CASE:
5600 tree expr, expr_hi;
5601 cp_token *ellipsis;
5603 /* Consume the `case' token. */
5604 cp_lexer_consume_token (parser->lexer);
5605 /* Parse the constant-expression. */
5606 expr = cp_parser_constant_expression (parser,
5607 /*allow_non_constant_p=*/false,
5608 NULL);
5610 ellipsis = cp_lexer_peek_token (parser->lexer);
5611 if (ellipsis->type == CPP_ELLIPSIS)
5613 /* Consume the `...' token. */
5614 cp_lexer_consume_token (parser->lexer);
5615 expr_hi =
5616 cp_parser_constant_expression (parser,
5617 /*allow_non_constant_p=*/false,
5618 NULL);
5619 /* We don't need to emit warnings here, as the common code
5620 will do this for us. */
5622 else
5623 expr_hi = NULL_TREE;
5625 if (!parser->in_switch_statement_p)
5626 error ("case label `%E' not within a switch statement", expr);
5627 else
5628 statement = finish_case_label (expr, expr_hi);
5630 break;
5632 case RID_DEFAULT:
5633 /* Consume the `default' token. */
5634 cp_lexer_consume_token (parser->lexer);
5635 if (!parser->in_switch_statement_p)
5636 error ("case label not within a switch statement");
5637 else
5638 statement = finish_case_label (NULL_TREE, NULL_TREE);
5639 break;
5641 default:
5642 /* Anything else must be an ordinary label. */
5643 statement = finish_label_stmt (cp_parser_identifier (parser));
5644 break;
5647 /* Require the `:' token. */
5648 cp_parser_require (parser, CPP_COLON, "`:'");
5649 /* Parse the labeled statement. */
5650 cp_parser_statement (parser, in_statement_expr_p);
5652 /* Return the label, in the case of a `case' or `default' label. */
5653 return statement;
5656 /* Parse an expression-statement.
5658 expression-statement:
5659 expression [opt] ;
5661 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5662 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5663 indicates whether this expression-statement is part of an
5664 expression statement. */
5666 static tree
5667 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5669 tree statement = NULL_TREE;
5671 /* If the next token is a ';', then there is no expression
5672 statement. */
5673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5674 statement = cp_parser_expression (parser);
5676 /* Consume the final `;'. */
5677 cp_parser_consume_semicolon_at_end_of_statement (parser);
5679 if (in_statement_expr_p
5680 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5682 /* This is the final expression statement of a statement
5683 expression. */
5684 statement = finish_stmt_expr_expr (statement);
5686 else if (statement)
5687 statement = finish_expr_stmt (statement);
5688 else
5689 finish_stmt ();
5691 return statement;
5694 /* Parse a compound-statement.
5696 compound-statement:
5697 { statement-seq [opt] }
5699 Returns a COMPOUND_STMT representing the statement. */
5701 static tree
5702 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5704 tree compound_stmt;
5706 /* Consume the `{'. */
5707 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5708 return error_mark_node;
5709 /* Begin the compound-statement. */
5710 compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5711 /* Parse an (optional) statement-seq. */
5712 cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5713 /* Finish the compound-statement. */
5714 finish_compound_stmt (compound_stmt);
5715 /* Consume the `}'. */
5716 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5718 return compound_stmt;
5721 /* Parse an (optional) statement-seq.
5723 statement-seq:
5724 statement
5725 statement-seq [opt] statement */
5727 static void
5728 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5730 /* Scan statements until there aren't any more. */
5731 while (true)
5733 /* If we're looking at a `}', then we've run out of statements. */
5734 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5735 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5736 break;
5738 /* Parse the statement. */
5739 cp_parser_statement (parser, in_statement_expr_p);
5743 /* Parse a selection-statement.
5745 selection-statement:
5746 if ( condition ) statement
5747 if ( condition ) statement else statement
5748 switch ( condition ) statement
5750 Returns the new IF_STMT or SWITCH_STMT. */
5752 static tree
5753 cp_parser_selection_statement (cp_parser* parser)
5755 cp_token *token;
5756 enum rid keyword;
5758 /* Peek at the next token. */
5759 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5761 /* See what kind of keyword it is. */
5762 keyword = token->keyword;
5763 switch (keyword)
5765 case RID_IF:
5766 case RID_SWITCH:
5768 tree statement;
5769 tree condition;
5771 /* Look for the `('. */
5772 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5774 cp_parser_skip_to_end_of_statement (parser);
5775 return error_mark_node;
5778 /* Begin the selection-statement. */
5779 if (keyword == RID_IF)
5780 statement = begin_if_stmt ();
5781 else
5782 statement = begin_switch_stmt ();
5784 /* Parse the condition. */
5785 condition = cp_parser_condition (parser);
5786 /* Look for the `)'. */
5787 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5788 cp_parser_skip_to_closing_parenthesis (parser, true, false,
5789 /*consume_paren=*/true);
5791 if (keyword == RID_IF)
5793 tree then_stmt;
5795 /* Add the condition. */
5796 finish_if_stmt_cond (condition, statement);
5798 /* Parse the then-clause. */
5799 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5800 finish_then_clause (statement);
5802 /* If the next token is `else', parse the else-clause. */
5803 if (cp_lexer_next_token_is_keyword (parser->lexer,
5804 RID_ELSE))
5806 tree else_stmt;
5808 /* Consume the `else' keyword. */
5809 cp_lexer_consume_token (parser->lexer);
5810 /* Parse the else-clause. */
5811 else_stmt
5812 = cp_parser_implicitly_scoped_statement (parser);
5813 finish_else_clause (statement);
5816 /* Now we're all done with the if-statement. */
5817 finish_if_stmt ();
5819 else
5821 tree body;
5822 bool in_switch_statement_p;
5824 /* Add the condition. */
5825 finish_switch_cond (condition, statement);
5827 /* Parse the body of the switch-statement. */
5828 in_switch_statement_p = parser->in_switch_statement_p;
5829 parser->in_switch_statement_p = true;
5830 body = cp_parser_implicitly_scoped_statement (parser);
5831 parser->in_switch_statement_p = in_switch_statement_p;
5833 /* Now we're all done with the switch-statement. */
5834 finish_switch_stmt (statement);
5837 return statement;
5839 break;
5841 default:
5842 cp_parser_error (parser, "expected selection-statement");
5843 return error_mark_node;
5847 /* Parse a condition.
5849 condition:
5850 expression
5851 type-specifier-seq declarator = assignment-expression
5853 GNU Extension:
5855 condition:
5856 type-specifier-seq declarator asm-specification [opt]
5857 attributes [opt] = assignment-expression
5859 Returns the expression that should be tested. */
5861 static tree
5862 cp_parser_condition (cp_parser* parser)
5864 tree type_specifiers;
5865 const char *saved_message;
5867 /* Try the declaration first. */
5868 cp_parser_parse_tentatively (parser);
5869 /* New types are not allowed in the type-specifier-seq for a
5870 condition. */
5871 saved_message = parser->type_definition_forbidden_message;
5872 parser->type_definition_forbidden_message
5873 = "types may not be defined in conditions";
5874 /* Parse the type-specifier-seq. */
5875 type_specifiers = cp_parser_type_specifier_seq (parser);
5876 /* Restore the saved message. */
5877 parser->type_definition_forbidden_message = saved_message;
5878 /* If all is well, we might be looking at a declaration. */
5879 if (!cp_parser_error_occurred (parser))
5881 tree decl;
5882 tree asm_specification;
5883 tree attributes;
5884 tree declarator;
5885 tree initializer = NULL_TREE;
5887 /* Parse the declarator. */
5888 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5889 /*ctor_dtor_or_conv_p=*/NULL,
5890 /*parenthesized_p=*/NULL);
5891 /* Parse the attributes. */
5892 attributes = cp_parser_attributes_opt (parser);
5893 /* Parse the asm-specification. */
5894 asm_specification = cp_parser_asm_specification_opt (parser);
5895 /* If the next token is not an `=', then we might still be
5896 looking at an expression. For example:
5898 if (A(a).x)
5900 looks like a decl-specifier-seq and a declarator -- but then
5901 there is no `=', so this is an expression. */
5902 cp_parser_require (parser, CPP_EQ, "`='");
5903 /* If we did see an `=', then we are looking at a declaration
5904 for sure. */
5905 if (cp_parser_parse_definitely (parser))
5907 /* Create the declaration. */
5908 decl = start_decl (declarator, type_specifiers,
5909 /*initialized_p=*/true,
5910 attributes, /*prefix_attributes=*/NULL_TREE);
5911 /* Parse the assignment-expression. */
5912 initializer = cp_parser_assignment_expression (parser);
5914 /* Process the initializer. */
5915 cp_finish_decl (decl,
5916 initializer,
5917 asm_specification,
5918 LOOKUP_ONLYCONVERTING);
5920 return convert_from_reference (decl);
5923 /* If we didn't even get past the declarator successfully, we are
5924 definitely not looking at a declaration. */
5925 else
5926 cp_parser_abort_tentative_parse (parser);
5928 /* Otherwise, we are looking at an expression. */
5929 return cp_parser_expression (parser);
5932 /* Parse an iteration-statement.
5934 iteration-statement:
5935 while ( condition ) statement
5936 do statement while ( expression ) ;
5937 for ( for-init-statement condition [opt] ; expression [opt] )
5938 statement
5940 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5942 static tree
5943 cp_parser_iteration_statement (cp_parser* parser)
5945 cp_token *token;
5946 enum rid keyword;
5947 tree statement;
5948 bool in_iteration_statement_p;
5951 /* Peek at the next token. */
5952 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5953 if (!token)
5954 return error_mark_node;
5956 /* Remember whether or not we are already within an iteration
5957 statement. */
5958 in_iteration_statement_p = parser->in_iteration_statement_p;
5960 /* See what kind of keyword it is. */
5961 keyword = token->keyword;
5962 switch (keyword)
5964 case RID_WHILE:
5966 tree condition;
5968 /* Begin the while-statement. */
5969 statement = begin_while_stmt ();
5970 /* Look for the `('. */
5971 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5972 /* Parse the condition. */
5973 condition = cp_parser_condition (parser);
5974 finish_while_stmt_cond (condition, statement);
5975 /* Look for the `)'. */
5976 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5977 /* Parse the dependent statement. */
5978 parser->in_iteration_statement_p = true;
5979 cp_parser_already_scoped_statement (parser);
5980 parser->in_iteration_statement_p = in_iteration_statement_p;
5981 /* We're done with the while-statement. */
5982 finish_while_stmt (statement);
5984 break;
5986 case RID_DO:
5988 tree expression;
5990 /* Begin the do-statement. */
5991 statement = begin_do_stmt ();
5992 /* Parse the body of the do-statement. */
5993 parser->in_iteration_statement_p = true;
5994 cp_parser_implicitly_scoped_statement (parser);
5995 parser->in_iteration_statement_p = in_iteration_statement_p;
5996 finish_do_body (statement);
5997 /* Look for the `while' keyword. */
5998 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5999 /* Look for the `('. */
6000 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6001 /* Parse the expression. */
6002 expression = cp_parser_expression (parser);
6003 /* We're done with the do-statement. */
6004 finish_do_stmt (expression, statement);
6005 /* Look for the `)'. */
6006 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6007 /* Look for the `;'. */
6008 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6010 break;
6012 case RID_FOR:
6014 tree condition = NULL_TREE;
6015 tree expression = NULL_TREE;
6017 /* Begin the for-statement. */
6018 statement = begin_for_stmt ();
6019 /* Look for the `('. */
6020 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6021 /* Parse the initialization. */
6022 cp_parser_for_init_statement (parser);
6023 finish_for_init_stmt (statement);
6025 /* If there's a condition, process it. */
6026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6027 condition = cp_parser_condition (parser);
6028 finish_for_cond (condition, statement);
6029 /* Look for the `;'. */
6030 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6032 /* If there's an expression, process it. */
6033 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6034 expression = cp_parser_expression (parser);
6035 finish_for_expr (expression, statement);
6036 /* Look for the `)'. */
6037 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6039 /* Parse the body of the for-statement. */
6040 parser->in_iteration_statement_p = true;
6041 cp_parser_already_scoped_statement (parser);
6042 parser->in_iteration_statement_p = in_iteration_statement_p;
6044 /* We're done with the for-statement. */
6045 finish_for_stmt (statement);
6047 break;
6049 default:
6050 cp_parser_error (parser, "expected iteration-statement");
6051 statement = error_mark_node;
6052 break;
6055 return statement;
6058 /* Parse a for-init-statement.
6060 for-init-statement:
6061 expression-statement
6062 simple-declaration */
6064 static void
6065 cp_parser_for_init_statement (cp_parser* parser)
6067 /* If the next token is a `;', then we have an empty
6068 expression-statement. Grammatically, this is also a
6069 simple-declaration, but an invalid one, because it does not
6070 declare anything. Therefore, if we did not handle this case
6071 specially, we would issue an error message about an invalid
6072 declaration. */
6073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6075 /* We're going to speculatively look for a declaration, falling back
6076 to an expression, if necessary. */
6077 cp_parser_parse_tentatively (parser);
6078 /* Parse the declaration. */
6079 cp_parser_simple_declaration (parser,
6080 /*function_definition_allowed_p=*/false);
6081 /* If the tentative parse failed, then we shall need to look for an
6082 expression-statement. */
6083 if (cp_parser_parse_definitely (parser))
6084 return;
6087 cp_parser_expression_statement (parser, false);
6090 /* Parse a jump-statement.
6092 jump-statement:
6093 break ;
6094 continue ;
6095 return expression [opt] ;
6096 goto identifier ;
6098 GNU extension:
6100 jump-statement:
6101 goto * expression ;
6103 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6104 GOTO_STMT. */
6106 static tree
6107 cp_parser_jump_statement (cp_parser* parser)
6109 tree statement = error_mark_node;
6110 cp_token *token;
6111 enum rid keyword;
6113 /* Peek at the next token. */
6114 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6115 if (!token)
6116 return error_mark_node;
6118 /* See what kind of keyword it is. */
6119 keyword = token->keyword;
6120 switch (keyword)
6122 case RID_BREAK:
6123 if (!parser->in_switch_statement_p
6124 && !parser->in_iteration_statement_p)
6126 error ("break statement not within loop or switch");
6127 statement = error_mark_node;
6129 else
6130 statement = finish_break_stmt ();
6131 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6132 break;
6134 case RID_CONTINUE:
6135 if (!parser->in_iteration_statement_p)
6137 error ("continue statement not within a loop");
6138 statement = error_mark_node;
6140 else
6141 statement = finish_continue_stmt ();
6142 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6143 break;
6145 case RID_RETURN:
6147 tree expr;
6149 /* If the next token is a `;', then there is no
6150 expression. */
6151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6152 expr = cp_parser_expression (parser);
6153 else
6154 expr = NULL_TREE;
6155 /* Build the return-statement. */
6156 statement = finish_return_stmt (expr);
6157 /* Look for the final `;'. */
6158 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6160 break;
6162 case RID_GOTO:
6163 /* Create the goto-statement. */
6164 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6166 /* Issue a warning about this use of a GNU extension. */
6167 if (pedantic)
6168 pedwarn ("ISO C++ forbids computed gotos");
6169 /* Consume the '*' token. */
6170 cp_lexer_consume_token (parser->lexer);
6171 /* Parse the dependent expression. */
6172 finish_goto_stmt (cp_parser_expression (parser));
6174 else
6175 finish_goto_stmt (cp_parser_identifier (parser));
6176 /* Look for the final `;'. */
6177 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6178 break;
6180 default:
6181 cp_parser_error (parser, "expected jump-statement");
6182 break;
6185 return statement;
6188 /* Parse a declaration-statement.
6190 declaration-statement:
6191 block-declaration */
6193 static void
6194 cp_parser_declaration_statement (cp_parser* parser)
6196 /* Parse the block-declaration. */
6197 cp_parser_block_declaration (parser, /*statement_p=*/true);
6199 /* Finish off the statement. */
6200 finish_stmt ();
6203 /* Some dependent statements (like `if (cond) statement'), are
6204 implicitly in their own scope. In other words, if the statement is
6205 a single statement (as opposed to a compound-statement), it is
6206 none-the-less treated as if it were enclosed in braces. Any
6207 declarations appearing in the dependent statement are out of scope
6208 after control passes that point. This function parses a statement,
6209 but ensures that is in its own scope, even if it is not a
6210 compound-statement.
6212 Returns the new statement. */
6214 static tree
6215 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6217 tree statement;
6219 /* If the token is not a `{', then we must take special action. */
6220 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6222 /* Create a compound-statement. */
6223 statement = begin_compound_stmt (/*has_no_scope=*/false);
6224 /* Parse the dependent-statement. */
6225 cp_parser_statement (parser, false);
6226 /* Finish the dummy compound-statement. */
6227 finish_compound_stmt (statement);
6229 /* Otherwise, we simply parse the statement directly. */
6230 else
6231 statement = cp_parser_compound_statement (parser, false);
6233 /* Return the statement. */
6234 return statement;
6237 /* For some dependent statements (like `while (cond) statement'), we
6238 have already created a scope. Therefore, even if the dependent
6239 statement is a compound-statement, we do not want to create another
6240 scope. */
6242 static void
6243 cp_parser_already_scoped_statement (cp_parser* parser)
6245 /* If the token is not a `{', then we must take special action. */
6246 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6248 tree statement;
6250 /* Create a compound-statement. */
6251 statement = begin_compound_stmt (/*has_no_scope=*/true);
6252 /* Parse the dependent-statement. */
6253 cp_parser_statement (parser, false);
6254 /* Finish the dummy compound-statement. */
6255 finish_compound_stmt (statement);
6257 /* Otherwise, we simply parse the statement directly. */
6258 else
6259 cp_parser_statement (parser, false);
6262 /* Declarations [gram.dcl.dcl] */
6264 /* Parse an optional declaration-sequence.
6266 declaration-seq:
6267 declaration
6268 declaration-seq declaration */
6270 static void
6271 cp_parser_declaration_seq_opt (cp_parser* parser)
6273 while (true)
6275 cp_token *token;
6277 token = cp_lexer_peek_token (parser->lexer);
6279 if (token->type == CPP_CLOSE_BRACE
6280 || token->type == CPP_EOF)
6281 break;
6283 if (token->type == CPP_SEMICOLON)
6285 /* A declaration consisting of a single semicolon is
6286 invalid. Allow it unless we're being pedantic. */
6287 if (pedantic && !in_system_header)
6288 pedwarn ("extra `;'");
6289 cp_lexer_consume_token (parser->lexer);
6290 continue;
6293 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6294 parser to enter or exit implicit `extern "C"' blocks. */
6295 while (pending_lang_change > 0)
6297 push_lang_context (lang_name_c);
6298 --pending_lang_change;
6300 while (pending_lang_change < 0)
6302 pop_lang_context ();
6303 ++pending_lang_change;
6306 /* Parse the declaration itself. */
6307 cp_parser_declaration (parser);
6311 /* Parse a declaration.
6313 declaration:
6314 block-declaration
6315 function-definition
6316 template-declaration
6317 explicit-instantiation
6318 explicit-specialization
6319 linkage-specification
6320 namespace-definition
6322 GNU extension:
6324 declaration:
6325 __extension__ declaration */
6327 static void
6328 cp_parser_declaration (cp_parser* parser)
6330 cp_token token1;
6331 cp_token token2;
6332 int saved_pedantic;
6334 /* Set this here since we can be called after
6335 pushing the linkage specification. */
6336 c_lex_string_translate = true;
6338 /* Check for the `__extension__' keyword. */
6339 if (cp_parser_extension_opt (parser, &saved_pedantic))
6341 /* Parse the qualified declaration. */
6342 cp_parser_declaration (parser);
6343 /* Restore the PEDANTIC flag. */
6344 pedantic = saved_pedantic;
6346 return;
6349 /* Try to figure out what kind of declaration is present. */
6350 token1 = *cp_lexer_peek_token (parser->lexer);
6352 /* Don't translate the CPP_STRING in extern "C". */
6353 if (token1.keyword == RID_EXTERN)
6354 c_lex_string_translate = false;
6356 if (token1.type != CPP_EOF)
6357 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6359 c_lex_string_translate = true;
6361 /* If the next token is `extern' and the following token is a string
6362 literal, then we have a linkage specification. */
6363 if (token1.keyword == RID_EXTERN
6364 && cp_parser_is_string_literal (&token2))
6365 cp_parser_linkage_specification (parser);
6366 /* If the next token is `template', then we have either a template
6367 declaration, an explicit instantiation, or an explicit
6368 specialization. */
6369 else if (token1.keyword == RID_TEMPLATE)
6371 /* `template <>' indicates a template specialization. */
6372 if (token2.type == CPP_LESS
6373 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6374 cp_parser_explicit_specialization (parser);
6375 /* `template <' indicates a template declaration. */
6376 else if (token2.type == CPP_LESS)
6377 cp_parser_template_declaration (parser, /*member_p=*/false);
6378 /* Anything else must be an explicit instantiation. */
6379 else
6380 cp_parser_explicit_instantiation (parser);
6382 /* If the next token is `export', then we have a template
6383 declaration. */
6384 else if (token1.keyword == RID_EXPORT)
6385 cp_parser_template_declaration (parser, /*member_p=*/false);
6386 /* If the next token is `extern', 'static' or 'inline' and the one
6387 after that is `template', we have a GNU extended explicit
6388 instantiation directive. */
6389 else if (cp_parser_allow_gnu_extensions_p (parser)
6390 && (token1.keyword == RID_EXTERN
6391 || token1.keyword == RID_STATIC
6392 || token1.keyword == RID_INLINE)
6393 && token2.keyword == RID_TEMPLATE)
6394 cp_parser_explicit_instantiation (parser);
6395 /* If the next token is `namespace', check for a named or unnamed
6396 namespace definition. */
6397 else if (token1.keyword == RID_NAMESPACE
6398 && (/* A named namespace definition. */
6399 (token2.type == CPP_NAME
6400 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6401 == CPP_OPEN_BRACE))
6402 /* An unnamed namespace definition. */
6403 || token2.type == CPP_OPEN_BRACE))
6404 cp_parser_namespace_definition (parser);
6405 /* We must have either a block declaration or a function
6406 definition. */
6407 else
6408 /* Try to parse a block-declaration, or a function-definition. */
6409 cp_parser_block_declaration (parser, /*statement_p=*/false);
6412 /* Parse a block-declaration.
6414 block-declaration:
6415 simple-declaration
6416 asm-definition
6417 namespace-alias-definition
6418 using-declaration
6419 using-directive
6421 GNU Extension:
6423 block-declaration:
6424 __extension__ block-declaration
6425 label-declaration
6427 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6428 part of a declaration-statement. */
6430 static void
6431 cp_parser_block_declaration (cp_parser *parser,
6432 bool statement_p)
6434 cp_token *token1;
6435 int saved_pedantic;
6437 /* Check for the `__extension__' keyword. */
6438 if (cp_parser_extension_opt (parser, &saved_pedantic))
6440 /* Parse the qualified declaration. */
6441 cp_parser_block_declaration (parser, statement_p);
6442 /* Restore the PEDANTIC flag. */
6443 pedantic = saved_pedantic;
6445 return;
6448 /* Peek at the next token to figure out which kind of declaration is
6449 present. */
6450 token1 = cp_lexer_peek_token (parser->lexer);
6452 /* If the next keyword is `asm', we have an asm-definition. */
6453 if (token1->keyword == RID_ASM)
6455 if (statement_p)
6456 cp_parser_commit_to_tentative_parse (parser);
6457 cp_parser_asm_definition (parser);
6459 /* If the next keyword is `namespace', we have a
6460 namespace-alias-definition. */
6461 else if (token1->keyword == RID_NAMESPACE)
6462 cp_parser_namespace_alias_definition (parser);
6463 /* If the next keyword is `using', we have either a
6464 using-declaration or a using-directive. */
6465 else if (token1->keyword == RID_USING)
6467 cp_token *token2;
6469 if (statement_p)
6470 cp_parser_commit_to_tentative_parse (parser);
6471 /* If the token after `using' is `namespace', then we have a
6472 using-directive. */
6473 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6474 if (token2->keyword == RID_NAMESPACE)
6475 cp_parser_using_directive (parser);
6476 /* Otherwise, it's a using-declaration. */
6477 else
6478 cp_parser_using_declaration (parser);
6480 /* If the next keyword is `__label__' we have a label declaration. */
6481 else if (token1->keyword == RID_LABEL)
6483 if (statement_p)
6484 cp_parser_commit_to_tentative_parse (parser);
6485 cp_parser_label_declaration (parser);
6487 /* Anything else must be a simple-declaration. */
6488 else
6489 cp_parser_simple_declaration (parser, !statement_p);
6492 /* Parse a simple-declaration.
6494 simple-declaration:
6495 decl-specifier-seq [opt] init-declarator-list [opt] ;
6497 init-declarator-list:
6498 init-declarator
6499 init-declarator-list , init-declarator
6501 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6502 function-definition as a simple-declaration. */
6504 static void
6505 cp_parser_simple_declaration (cp_parser* parser,
6506 bool function_definition_allowed_p)
6508 tree decl_specifiers;
6509 tree attributes;
6510 int declares_class_or_enum;
6511 bool saw_declarator;
6513 /* Defer access checks until we know what is being declared; the
6514 checks for names appearing in the decl-specifier-seq should be
6515 done as if we were in the scope of the thing being declared. */
6516 push_deferring_access_checks (dk_deferred);
6518 /* Parse the decl-specifier-seq. We have to keep track of whether
6519 or not the decl-specifier-seq declares a named class or
6520 enumeration type, since that is the only case in which the
6521 init-declarator-list is allowed to be empty.
6523 [dcl.dcl]
6525 In a simple-declaration, the optional init-declarator-list can be
6526 omitted only when declaring a class or enumeration, that is when
6527 the decl-specifier-seq contains either a class-specifier, an
6528 elaborated-type-specifier, or an enum-specifier. */
6529 decl_specifiers
6530 = cp_parser_decl_specifier_seq (parser,
6531 CP_PARSER_FLAGS_OPTIONAL,
6532 &attributes,
6533 &declares_class_or_enum);
6534 /* We no longer need to defer access checks. */
6535 stop_deferring_access_checks ();
6537 /* In a block scope, a valid declaration must always have a
6538 decl-specifier-seq. By not trying to parse declarators, we can
6539 resolve the declaration/expression ambiguity more quickly. */
6540 if (!function_definition_allowed_p && !decl_specifiers)
6542 cp_parser_error (parser, "expected declaration");
6543 goto done;
6546 /* If the next two tokens are both identifiers, the code is
6547 erroneous. The usual cause of this situation is code like:
6549 T t;
6551 where "T" should name a type -- but does not. */
6552 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6554 /* If parsing tentatively, we should commit; we really are
6555 looking at a declaration. */
6556 cp_parser_commit_to_tentative_parse (parser);
6557 /* Give up. */
6558 goto done;
6561 /* Keep going until we hit the `;' at the end of the simple
6562 declaration. */
6563 saw_declarator = false;
6564 while (cp_lexer_next_token_is_not (parser->lexer,
6565 CPP_SEMICOLON))
6567 cp_token *token;
6568 bool function_definition_p;
6569 tree decl;
6571 saw_declarator = true;
6572 /* Parse the init-declarator. */
6573 decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6574 function_definition_allowed_p,
6575 /*member_p=*/false,
6576 declares_class_or_enum,
6577 &function_definition_p);
6578 /* If an error occurred while parsing tentatively, exit quickly.
6579 (That usually happens when in the body of a function; each
6580 statement is treated as a declaration-statement until proven
6581 otherwise.) */
6582 if (cp_parser_error_occurred (parser))
6583 goto done;
6584 /* Handle function definitions specially. */
6585 if (function_definition_p)
6587 /* If the next token is a `,', then we are probably
6588 processing something like:
6590 void f() {}, *p;
6592 which is erroneous. */
6593 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6594 error ("mixing declarations and function-definitions is forbidden");
6595 /* Otherwise, we're done with the list of declarators. */
6596 else
6598 pop_deferring_access_checks ();
6599 return;
6602 /* The next token should be either a `,' or a `;'. */
6603 token = cp_lexer_peek_token (parser->lexer);
6604 /* If it's a `,', there are more declarators to come. */
6605 if (token->type == CPP_COMMA)
6606 cp_lexer_consume_token (parser->lexer);
6607 /* If it's a `;', we are done. */
6608 else if (token->type == CPP_SEMICOLON)
6609 break;
6610 /* Anything else is an error. */
6611 else
6613 cp_parser_error (parser, "expected `,' or `;'");
6614 /* Skip tokens until we reach the end of the statement. */
6615 cp_parser_skip_to_end_of_statement (parser);
6616 /* If the next token is now a `;', consume it. */
6617 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6618 cp_lexer_consume_token (parser->lexer);
6619 goto done;
6621 /* After the first time around, a function-definition is not
6622 allowed -- even if it was OK at first. For example:
6624 int i, f() {}
6626 is not valid. */
6627 function_definition_allowed_p = false;
6630 /* Issue an error message if no declarators are present, and the
6631 decl-specifier-seq does not itself declare a class or
6632 enumeration. */
6633 if (!saw_declarator)
6635 if (cp_parser_declares_only_class_p (parser))
6636 shadow_tag (decl_specifiers);
6637 /* Perform any deferred access checks. */
6638 perform_deferred_access_checks ();
6641 /* Consume the `;'. */
6642 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6644 done:
6645 pop_deferring_access_checks ();
6648 /* Parse a decl-specifier-seq.
6650 decl-specifier-seq:
6651 decl-specifier-seq [opt] decl-specifier
6653 decl-specifier:
6654 storage-class-specifier
6655 type-specifier
6656 function-specifier
6657 friend
6658 typedef
6660 GNU Extension:
6662 decl-specifier-seq:
6663 decl-specifier-seq [opt] attributes
6665 Returns a TREE_LIST, giving the decl-specifiers in the order they
6666 appear in the source code. The TREE_VALUE of each node is the
6667 decl-specifier. For a keyword (such as `auto' or `friend'), the
6668 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6669 representation of a type-specifier, see cp_parser_type_specifier.
6671 If there are attributes, they will be stored in *ATTRIBUTES,
6672 represented as described above cp_parser_attributes.
6674 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6675 appears, and the entity that will be a friend is not going to be a
6676 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6677 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6678 friendship is granted might not be a class.
6680 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6681 flags:
6683 1: one of the decl-specifiers is an elaborated-type-specifier
6684 (i.e., a type declaration)
6685 2: one of the decl-specifiers is an enum-specifier or a
6686 class-specifier (i.e., a type definition)
6690 static tree
6691 cp_parser_decl_specifier_seq (cp_parser* parser,
6692 cp_parser_flags flags,
6693 tree* attributes,
6694 int* declares_class_or_enum)
6696 tree decl_specs = NULL_TREE;
6697 bool friend_p = false;
6698 bool constructor_possible_p = !parser->in_declarator_p;
6700 /* Assume no class or enumeration type is declared. */
6701 *declares_class_or_enum = 0;
6703 /* Assume there are no attributes. */
6704 *attributes = NULL_TREE;
6706 /* Keep reading specifiers until there are no more to read. */
6707 while (true)
6709 tree decl_spec = NULL_TREE;
6710 bool constructor_p;
6711 cp_token *token;
6713 /* Peek at the next token. */
6714 token = cp_lexer_peek_token (parser->lexer);
6715 /* Handle attributes. */
6716 if (token->keyword == RID_ATTRIBUTE)
6718 /* Parse the attributes. */
6719 decl_spec = cp_parser_attributes_opt (parser);
6720 /* Add them to the list. */
6721 *attributes = chainon (*attributes, decl_spec);
6722 continue;
6724 /* If the next token is an appropriate keyword, we can simply
6725 add it to the list. */
6726 switch (token->keyword)
6728 case RID_FRIEND:
6729 /* decl-specifier:
6730 friend */
6731 if (friend_p)
6732 error ("duplicate `friend'");
6733 else
6734 friend_p = true;
6735 /* The representation of the specifier is simply the
6736 appropriate TREE_IDENTIFIER node. */
6737 decl_spec = token->value;
6738 /* Consume the token. */
6739 cp_lexer_consume_token (parser->lexer);
6740 break;
6742 /* function-specifier:
6743 inline
6744 virtual
6745 explicit */
6746 case RID_INLINE:
6747 case RID_VIRTUAL:
6748 case RID_EXPLICIT:
6749 decl_spec = cp_parser_function_specifier_opt (parser);
6750 break;
6752 /* decl-specifier:
6753 typedef */
6754 case RID_TYPEDEF:
6755 /* The representation of the specifier is simply the
6756 appropriate TREE_IDENTIFIER node. */
6757 decl_spec = token->value;
6758 /* Consume the token. */
6759 cp_lexer_consume_token (parser->lexer);
6760 /* A constructor declarator cannot appear in a typedef. */
6761 constructor_possible_p = false;
6762 /* The "typedef" keyword can only occur in a declaration; we
6763 may as well commit at this point. */
6764 cp_parser_commit_to_tentative_parse (parser);
6765 break;
6767 /* storage-class-specifier:
6768 auto
6769 register
6770 static
6771 extern
6772 mutable
6774 GNU Extension:
6775 thread */
6776 case RID_AUTO:
6777 case RID_REGISTER:
6778 case RID_STATIC:
6779 case RID_EXTERN:
6780 case RID_MUTABLE:
6781 case RID_THREAD:
6782 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6783 break;
6785 default:
6786 break;
6789 /* Constructors are a special case. The `S' in `S()' is not a
6790 decl-specifier; it is the beginning of the declarator. */
6791 constructor_p = (!decl_spec
6792 && constructor_possible_p
6793 && cp_parser_constructor_declarator_p (parser,
6794 friend_p));
6796 /* If we don't have a DECL_SPEC yet, then we must be looking at
6797 a type-specifier. */
6798 if (!decl_spec && !constructor_p)
6800 int decl_spec_declares_class_or_enum;
6801 bool is_cv_qualifier;
6803 decl_spec
6804 = cp_parser_type_specifier (parser, flags,
6805 friend_p,
6806 /*is_declaration=*/true,
6807 &decl_spec_declares_class_or_enum,
6808 &is_cv_qualifier);
6810 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6812 /* If this type-specifier referenced a user-defined type
6813 (a typedef, class-name, etc.), then we can't allow any
6814 more such type-specifiers henceforth.
6816 [dcl.spec]
6818 The longest sequence of decl-specifiers that could
6819 possibly be a type name is taken as the
6820 decl-specifier-seq of a declaration. The sequence shall
6821 be self-consistent as described below.
6823 [dcl.type]
6825 As a general rule, at most one type-specifier is allowed
6826 in the complete decl-specifier-seq of a declaration. The
6827 only exceptions are the following:
6829 -- const or volatile can be combined with any other
6830 type-specifier.
6832 -- signed or unsigned can be combined with char, long,
6833 short, or int.
6835 -- ..
6837 Example:
6839 typedef char* Pc;
6840 void g (const int Pc);
6842 Here, Pc is *not* part of the decl-specifier seq; it's
6843 the declarator. Therefore, once we see a type-specifier
6844 (other than a cv-qualifier), we forbid any additional
6845 user-defined types. We *do* still allow things like `int
6846 int' to be considered a decl-specifier-seq, and issue the
6847 error message later. */
6848 if (decl_spec && !is_cv_qualifier)
6849 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6850 /* A constructor declarator cannot follow a type-specifier. */
6851 if (decl_spec)
6852 constructor_possible_p = false;
6855 /* If we still do not have a DECL_SPEC, then there are no more
6856 decl-specifiers. */
6857 if (!decl_spec)
6859 /* Issue an error message, unless the entire construct was
6860 optional. */
6861 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6863 cp_parser_error (parser, "expected decl specifier");
6864 return error_mark_node;
6867 break;
6870 /* Add the DECL_SPEC to the list of specifiers. */
6871 if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6872 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6874 /* After we see one decl-specifier, further decl-specifiers are
6875 always optional. */
6876 flags |= CP_PARSER_FLAGS_OPTIONAL;
6879 /* Don't allow a friend specifier with a class definition. */
6880 if (friend_p && (*declares_class_or_enum & 2))
6881 error ("class definition may not be declared a friend");
6883 /* We have built up the DECL_SPECS in reverse order. Return them in
6884 the correct order. */
6885 return nreverse (decl_specs);
6888 /* Parse an (optional) storage-class-specifier.
6890 storage-class-specifier:
6891 auto
6892 register
6893 static
6894 extern
6895 mutable
6897 GNU Extension:
6899 storage-class-specifier:
6900 thread
6902 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6904 static tree
6905 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6907 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6909 case RID_AUTO:
6910 case RID_REGISTER:
6911 case RID_STATIC:
6912 case RID_EXTERN:
6913 case RID_MUTABLE:
6914 case RID_THREAD:
6915 /* Consume the token. */
6916 return cp_lexer_consume_token (parser->lexer)->value;
6918 default:
6919 return NULL_TREE;
6923 /* Parse an (optional) function-specifier.
6925 function-specifier:
6926 inline
6927 virtual
6928 explicit
6930 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6932 static tree
6933 cp_parser_function_specifier_opt (cp_parser* parser)
6935 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6937 case RID_INLINE:
6938 case RID_VIRTUAL:
6939 case RID_EXPLICIT:
6940 /* Consume the token. */
6941 return cp_lexer_consume_token (parser->lexer)->value;
6943 default:
6944 return NULL_TREE;
6948 /* Parse a linkage-specification.
6950 linkage-specification:
6951 extern string-literal { declaration-seq [opt] }
6952 extern string-literal declaration */
6954 static void
6955 cp_parser_linkage_specification (cp_parser* parser)
6957 cp_token *token;
6958 tree linkage;
6960 /* Look for the `extern' keyword. */
6961 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6963 /* Peek at the next token. */
6964 token = cp_lexer_peek_token (parser->lexer);
6965 /* If it's not a string-literal, then there's a problem. */
6966 if (!cp_parser_is_string_literal (token))
6968 cp_parser_error (parser, "expected language-name");
6969 return;
6971 /* Consume the token. */
6972 cp_lexer_consume_token (parser->lexer);
6974 /* Transform the literal into an identifier. If the literal is a
6975 wide-character string, or contains embedded NULs, then we can't
6976 handle it as the user wants. */
6977 if (token->type == CPP_WSTRING
6978 || (strlen (TREE_STRING_POINTER (token->value))
6979 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6981 cp_parser_error (parser, "invalid linkage-specification");
6982 /* Assume C++ linkage. */
6983 linkage = get_identifier ("c++");
6985 /* If it's a simple string constant, things are easier. */
6986 else
6987 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6989 /* We're now using the new linkage. */
6990 push_lang_context (linkage);
6992 /* If the next token is a `{', then we're using the first
6993 production. */
6994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6996 /* Consume the `{' token. */
6997 cp_lexer_consume_token (parser->lexer);
6998 /* Parse the declarations. */
6999 cp_parser_declaration_seq_opt (parser);
7000 /* Look for the closing `}'. */
7001 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7003 /* Otherwise, there's just one declaration. */
7004 else
7006 bool saved_in_unbraced_linkage_specification_p;
7008 saved_in_unbraced_linkage_specification_p
7009 = parser->in_unbraced_linkage_specification_p;
7010 parser->in_unbraced_linkage_specification_p = true;
7011 have_extern_spec = true;
7012 cp_parser_declaration (parser);
7013 have_extern_spec = false;
7014 parser->in_unbraced_linkage_specification_p
7015 = saved_in_unbraced_linkage_specification_p;
7018 /* We're done with the linkage-specification. */
7019 pop_lang_context ();
7022 /* Special member functions [gram.special] */
7024 /* Parse a conversion-function-id.
7026 conversion-function-id:
7027 operator conversion-type-id
7029 Returns an IDENTIFIER_NODE representing the operator. */
7031 static tree
7032 cp_parser_conversion_function_id (cp_parser* parser)
7034 tree type;
7035 tree saved_scope;
7036 tree saved_qualifying_scope;
7037 tree saved_object_scope;
7038 bool pop_p = false;
7040 /* Look for the `operator' token. */
7041 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7042 return error_mark_node;
7043 /* When we parse the conversion-type-id, the current scope will be
7044 reset. However, we need that information in able to look up the
7045 conversion function later, so we save it here. */
7046 saved_scope = parser->scope;
7047 saved_qualifying_scope = parser->qualifying_scope;
7048 saved_object_scope = parser->object_scope;
7049 /* We must enter the scope of the class so that the names of
7050 entities declared within the class are available in the
7051 conversion-type-id. For example, consider:
7053 struct S {
7054 typedef int I;
7055 operator I();
7058 S::operator I() { ... }
7060 In order to see that `I' is a type-name in the definition, we
7061 must be in the scope of `S'. */
7062 if (saved_scope)
7063 pop_p = push_scope (saved_scope);
7064 /* Parse the conversion-type-id. */
7065 type = cp_parser_conversion_type_id (parser);
7066 /* Leave the scope of the class, if any. */
7067 if (pop_p)
7068 pop_scope (saved_scope);
7069 /* Restore the saved scope. */
7070 parser->scope = saved_scope;
7071 parser->qualifying_scope = saved_qualifying_scope;
7072 parser->object_scope = saved_object_scope;
7073 /* If the TYPE is invalid, indicate failure. */
7074 if (type == error_mark_node)
7075 return error_mark_node;
7076 return mangle_conv_op_name_for_type (type);
7079 /* Parse a conversion-type-id:
7081 conversion-type-id:
7082 type-specifier-seq conversion-declarator [opt]
7084 Returns the TYPE specified. */
7086 static tree
7087 cp_parser_conversion_type_id (cp_parser* parser)
7089 tree attributes;
7090 tree type_specifiers;
7091 tree declarator;
7093 /* Parse the attributes. */
7094 attributes = cp_parser_attributes_opt (parser);
7095 /* Parse the type-specifiers. */
7096 type_specifiers = cp_parser_type_specifier_seq (parser);
7097 /* If that didn't work, stop. */
7098 if (type_specifiers == error_mark_node)
7099 return error_mark_node;
7100 /* Parse the conversion-declarator. */
7101 declarator = cp_parser_conversion_declarator_opt (parser);
7103 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7104 /*initialized=*/0, &attributes);
7107 /* Parse an (optional) conversion-declarator.
7109 conversion-declarator:
7110 ptr-operator conversion-declarator [opt]
7112 Returns a representation of the declarator. See
7113 cp_parser_declarator for details. */
7115 static tree
7116 cp_parser_conversion_declarator_opt (cp_parser* parser)
7118 enum tree_code code;
7119 tree class_type;
7120 tree cv_qualifier_seq;
7122 /* We don't know if there's a ptr-operator next, or not. */
7123 cp_parser_parse_tentatively (parser);
7124 /* Try the ptr-operator. */
7125 code = cp_parser_ptr_operator (parser, &class_type,
7126 &cv_qualifier_seq);
7127 /* If it worked, look for more conversion-declarators. */
7128 if (cp_parser_parse_definitely (parser))
7130 tree declarator;
7132 /* Parse another optional declarator. */
7133 declarator = cp_parser_conversion_declarator_opt (parser);
7135 /* Create the representation of the declarator. */
7136 if (code == INDIRECT_REF)
7137 declarator = make_pointer_declarator (cv_qualifier_seq,
7138 declarator);
7139 else
7140 declarator = make_reference_declarator (cv_qualifier_seq,
7141 declarator);
7143 /* Handle the pointer-to-member case. */
7144 if (class_type)
7145 declarator = build_nt (SCOPE_REF, class_type, declarator);
7147 return declarator;
7150 return NULL_TREE;
7153 /* Parse an (optional) ctor-initializer.
7155 ctor-initializer:
7156 : mem-initializer-list
7158 Returns TRUE iff the ctor-initializer was actually present. */
7160 static bool
7161 cp_parser_ctor_initializer_opt (cp_parser* parser)
7163 /* If the next token is not a `:', then there is no
7164 ctor-initializer. */
7165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7167 /* Do default initialization of any bases and members. */
7168 if (DECL_CONSTRUCTOR_P (current_function_decl))
7169 finish_mem_initializers (NULL_TREE);
7171 return false;
7174 /* Consume the `:' token. */
7175 cp_lexer_consume_token (parser->lexer);
7176 /* And the mem-initializer-list. */
7177 cp_parser_mem_initializer_list (parser);
7179 return true;
7182 /* Parse a mem-initializer-list.
7184 mem-initializer-list:
7185 mem-initializer
7186 mem-initializer , mem-initializer-list */
7188 static void
7189 cp_parser_mem_initializer_list (cp_parser* parser)
7191 tree mem_initializer_list = NULL_TREE;
7193 /* Let the semantic analysis code know that we are starting the
7194 mem-initializer-list. */
7195 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7196 error ("only constructors take base initializers");
7198 /* Loop through the list. */
7199 while (true)
7201 tree mem_initializer;
7203 /* Parse the mem-initializer. */
7204 mem_initializer = cp_parser_mem_initializer (parser);
7205 /* Add it to the list, unless it was erroneous. */
7206 if (mem_initializer)
7208 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7209 mem_initializer_list = mem_initializer;
7211 /* If the next token is not a `,', we're done. */
7212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7213 break;
7214 /* Consume the `,' token. */
7215 cp_lexer_consume_token (parser->lexer);
7218 /* Perform semantic analysis. */
7219 if (DECL_CONSTRUCTOR_P (current_function_decl))
7220 finish_mem_initializers (mem_initializer_list);
7223 /* Parse a mem-initializer.
7225 mem-initializer:
7226 mem-initializer-id ( expression-list [opt] )
7228 GNU extension:
7230 mem-initializer:
7231 ( expression-list [opt] )
7233 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7234 class) or FIELD_DECL (for a non-static data member) to initialize;
7235 the TREE_VALUE is the expression-list. */
7237 static tree
7238 cp_parser_mem_initializer (cp_parser* parser)
7240 tree mem_initializer_id;
7241 tree expression_list;
7242 tree member;
7244 /* Find out what is being initialized. */
7245 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7247 pedwarn ("anachronistic old-style base class initializer");
7248 mem_initializer_id = NULL_TREE;
7250 else
7251 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7252 member = expand_member_init (mem_initializer_id);
7253 if (member && !DECL_P (member))
7254 in_base_initializer = 1;
7256 expression_list
7257 = cp_parser_parenthesized_expression_list (parser, false,
7258 /*non_constant_p=*/NULL);
7259 if (!expression_list)
7260 expression_list = void_type_node;
7262 in_base_initializer = 0;
7264 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7267 /* Parse a mem-initializer-id.
7269 mem-initializer-id:
7270 :: [opt] nested-name-specifier [opt] class-name
7271 identifier
7273 Returns a TYPE indicating the class to be initializer for the first
7274 production. Returns an IDENTIFIER_NODE indicating the data member
7275 to be initialized for the second production. */
7277 static tree
7278 cp_parser_mem_initializer_id (cp_parser* parser)
7280 bool global_scope_p;
7281 bool nested_name_specifier_p;
7282 tree id;
7284 /* Look for the optional `::' operator. */
7285 global_scope_p
7286 = (cp_parser_global_scope_opt (parser,
7287 /*current_scope_valid_p=*/false)
7288 != NULL_TREE);
7289 /* Look for the optional nested-name-specifier. The simplest way to
7290 implement:
7292 [temp.res]
7294 The keyword `typename' is not permitted in a base-specifier or
7295 mem-initializer; in these contexts a qualified name that
7296 depends on a template-parameter is implicitly assumed to be a
7297 type name.
7299 is to assume that we have seen the `typename' keyword at this
7300 point. */
7301 nested_name_specifier_p
7302 = (cp_parser_nested_name_specifier_opt (parser,
7303 /*typename_keyword_p=*/true,
7304 /*check_dependency_p=*/true,
7305 /*type_p=*/true,
7306 /*is_declaration=*/true)
7307 != NULL_TREE);
7308 /* If there is a `::' operator or a nested-name-specifier, then we
7309 are definitely looking for a class-name. */
7310 if (global_scope_p || nested_name_specifier_p)
7311 return cp_parser_class_name (parser,
7312 /*typename_keyword_p=*/true,
7313 /*template_keyword_p=*/false,
7314 /*type_p=*/false,
7315 /*check_dependency_p=*/true,
7316 /*class_head_p=*/false,
7317 /*is_declaration=*/true);
7318 /* Otherwise, we could also be looking for an ordinary identifier. */
7319 cp_parser_parse_tentatively (parser);
7320 /* Try a class-name. */
7321 id = cp_parser_class_name (parser,
7322 /*typename_keyword_p=*/true,
7323 /*template_keyword_p=*/false,
7324 /*type_p=*/false,
7325 /*check_dependency_p=*/true,
7326 /*class_head_p=*/false,
7327 /*is_declaration=*/true);
7328 /* If we found one, we're done. */
7329 if (cp_parser_parse_definitely (parser))
7330 return id;
7331 /* Otherwise, look for an ordinary identifier. */
7332 return cp_parser_identifier (parser);
7335 /* Overloading [gram.over] */
7337 /* Parse an operator-function-id.
7339 operator-function-id:
7340 operator operator
7342 Returns an IDENTIFIER_NODE for the operator which is a
7343 human-readable spelling of the identifier, e.g., `operator +'. */
7345 static tree
7346 cp_parser_operator_function_id (cp_parser* parser)
7348 /* Look for the `operator' keyword. */
7349 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7350 return error_mark_node;
7351 /* And then the name of the operator itself. */
7352 return cp_parser_operator (parser);
7355 /* Parse an operator.
7357 operator:
7358 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7359 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7360 || ++ -- , ->* -> () []
7362 GNU Extensions:
7364 operator:
7365 <? >? <?= >?=
7367 Returns an IDENTIFIER_NODE for the operator which is a
7368 human-readable spelling of the identifier, e.g., `operator +'. */
7370 static tree
7371 cp_parser_operator (cp_parser* parser)
7373 tree id = NULL_TREE;
7374 cp_token *token;
7376 /* Peek at the next token. */
7377 token = cp_lexer_peek_token (parser->lexer);
7378 /* Figure out which operator we have. */
7379 switch (token->type)
7381 case CPP_KEYWORD:
7383 enum tree_code op;
7385 /* The keyword should be either `new' or `delete'. */
7386 if (token->keyword == RID_NEW)
7387 op = NEW_EXPR;
7388 else if (token->keyword == RID_DELETE)
7389 op = DELETE_EXPR;
7390 else
7391 break;
7393 /* Consume the `new' or `delete' token. */
7394 cp_lexer_consume_token (parser->lexer);
7396 /* Peek at the next token. */
7397 token = cp_lexer_peek_token (parser->lexer);
7398 /* If it's a `[' token then this is the array variant of the
7399 operator. */
7400 if (token->type == CPP_OPEN_SQUARE)
7402 /* Consume the `[' token. */
7403 cp_lexer_consume_token (parser->lexer);
7404 /* Look for the `]' token. */
7405 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7406 id = ansi_opname (op == NEW_EXPR
7407 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7409 /* Otherwise, we have the non-array variant. */
7410 else
7411 id = ansi_opname (op);
7413 return id;
7416 case CPP_PLUS:
7417 id = ansi_opname (PLUS_EXPR);
7418 break;
7420 case CPP_MINUS:
7421 id = ansi_opname (MINUS_EXPR);
7422 break;
7424 case CPP_MULT:
7425 id = ansi_opname (MULT_EXPR);
7426 break;
7428 case CPP_DIV:
7429 id = ansi_opname (TRUNC_DIV_EXPR);
7430 break;
7432 case CPP_MOD:
7433 id = ansi_opname (TRUNC_MOD_EXPR);
7434 break;
7436 case CPP_XOR:
7437 id = ansi_opname (BIT_XOR_EXPR);
7438 break;
7440 case CPP_AND:
7441 id = ansi_opname (BIT_AND_EXPR);
7442 break;
7444 case CPP_OR:
7445 id = ansi_opname (BIT_IOR_EXPR);
7446 break;
7448 case CPP_COMPL:
7449 id = ansi_opname (BIT_NOT_EXPR);
7450 break;
7452 case CPP_NOT:
7453 id = ansi_opname (TRUTH_NOT_EXPR);
7454 break;
7456 case CPP_EQ:
7457 id = ansi_assopname (NOP_EXPR);
7458 break;
7460 case CPP_LESS:
7461 id = ansi_opname (LT_EXPR);
7462 break;
7464 case CPP_GREATER:
7465 id = ansi_opname (GT_EXPR);
7466 break;
7468 case CPP_PLUS_EQ:
7469 id = ansi_assopname (PLUS_EXPR);
7470 break;
7472 case CPP_MINUS_EQ:
7473 id = ansi_assopname (MINUS_EXPR);
7474 break;
7476 case CPP_MULT_EQ:
7477 id = ansi_assopname (MULT_EXPR);
7478 break;
7480 case CPP_DIV_EQ:
7481 id = ansi_assopname (TRUNC_DIV_EXPR);
7482 break;
7484 case CPP_MOD_EQ:
7485 id = ansi_assopname (TRUNC_MOD_EXPR);
7486 break;
7488 case CPP_XOR_EQ:
7489 id = ansi_assopname (BIT_XOR_EXPR);
7490 break;
7492 case CPP_AND_EQ:
7493 id = ansi_assopname (BIT_AND_EXPR);
7494 break;
7496 case CPP_OR_EQ:
7497 id = ansi_assopname (BIT_IOR_EXPR);
7498 break;
7500 case CPP_LSHIFT:
7501 id = ansi_opname (LSHIFT_EXPR);
7502 break;
7504 case CPP_RSHIFT:
7505 id = ansi_opname (RSHIFT_EXPR);
7506 break;
7508 case CPP_LSHIFT_EQ:
7509 id = ansi_assopname (LSHIFT_EXPR);
7510 break;
7512 case CPP_RSHIFT_EQ:
7513 id = ansi_assopname (RSHIFT_EXPR);
7514 break;
7516 case CPP_EQ_EQ:
7517 id = ansi_opname (EQ_EXPR);
7518 break;
7520 case CPP_NOT_EQ:
7521 id = ansi_opname (NE_EXPR);
7522 break;
7524 case CPP_LESS_EQ:
7525 id = ansi_opname (LE_EXPR);
7526 break;
7528 case CPP_GREATER_EQ:
7529 id = ansi_opname (GE_EXPR);
7530 break;
7532 case CPP_AND_AND:
7533 id = ansi_opname (TRUTH_ANDIF_EXPR);
7534 break;
7536 case CPP_OR_OR:
7537 id = ansi_opname (TRUTH_ORIF_EXPR);
7538 break;
7540 case CPP_PLUS_PLUS:
7541 id = ansi_opname (POSTINCREMENT_EXPR);
7542 break;
7544 case CPP_MINUS_MINUS:
7545 id = ansi_opname (PREDECREMENT_EXPR);
7546 break;
7548 case CPP_COMMA:
7549 id = ansi_opname (COMPOUND_EXPR);
7550 break;
7552 case CPP_DEREF_STAR:
7553 id = ansi_opname (MEMBER_REF);
7554 break;
7556 case CPP_DEREF:
7557 id = ansi_opname (COMPONENT_REF);
7558 break;
7560 case CPP_OPEN_PAREN:
7561 /* Consume the `('. */
7562 cp_lexer_consume_token (parser->lexer);
7563 /* Look for the matching `)'. */
7564 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7565 return ansi_opname (CALL_EXPR);
7567 case CPP_OPEN_SQUARE:
7568 /* Consume the `['. */
7569 cp_lexer_consume_token (parser->lexer);
7570 /* Look for the matching `]'. */
7571 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7572 return ansi_opname (ARRAY_REF);
7574 /* Extensions. */
7575 case CPP_MIN:
7576 id = ansi_opname (MIN_EXPR);
7577 break;
7579 case CPP_MAX:
7580 id = ansi_opname (MAX_EXPR);
7581 break;
7583 case CPP_MIN_EQ:
7584 id = ansi_assopname (MIN_EXPR);
7585 break;
7587 case CPP_MAX_EQ:
7588 id = ansi_assopname (MAX_EXPR);
7589 break;
7591 default:
7592 /* Anything else is an error. */
7593 break;
7596 /* If we have selected an identifier, we need to consume the
7597 operator token. */
7598 if (id)
7599 cp_lexer_consume_token (parser->lexer);
7600 /* Otherwise, no valid operator name was present. */
7601 else
7603 cp_parser_error (parser, "expected operator");
7604 id = error_mark_node;
7607 return id;
7610 /* Parse a template-declaration.
7612 template-declaration:
7613 export [opt] template < template-parameter-list > declaration
7615 If MEMBER_P is TRUE, this template-declaration occurs within a
7616 class-specifier.
7618 The grammar rule given by the standard isn't correct. What
7619 is really meant is:
7621 template-declaration:
7622 export [opt] template-parameter-list-seq
7623 decl-specifier-seq [opt] init-declarator [opt] ;
7624 export [opt] template-parameter-list-seq
7625 function-definition
7627 template-parameter-list-seq:
7628 template-parameter-list-seq [opt]
7629 template < template-parameter-list > */
7631 static void
7632 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7634 /* Check for `export'. */
7635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7637 /* Consume the `export' token. */
7638 cp_lexer_consume_token (parser->lexer);
7639 /* Warn that we do not support `export'. */
7640 warning ("keyword `export' not implemented, and will be ignored");
7643 cp_parser_template_declaration_after_export (parser, member_p);
7646 /* Parse a template-parameter-list.
7648 template-parameter-list:
7649 template-parameter
7650 template-parameter-list , template-parameter
7652 Returns a TREE_LIST. Each node represents a template parameter.
7653 The nodes are connected via their TREE_CHAINs. */
7655 static tree
7656 cp_parser_template_parameter_list (cp_parser* parser)
7658 tree parameter_list = NULL_TREE;
7660 while (true)
7662 tree parameter;
7663 cp_token *token;
7665 /* Parse the template-parameter. */
7666 parameter = cp_parser_template_parameter (parser);
7667 /* Add it to the list. */
7668 parameter_list = process_template_parm (parameter_list,
7669 parameter);
7671 /* Peek at the next token. */
7672 token = cp_lexer_peek_token (parser->lexer);
7673 /* If it's not a `,', we're done. */
7674 if (token->type != CPP_COMMA)
7675 break;
7676 /* Otherwise, consume the `,' token. */
7677 cp_lexer_consume_token (parser->lexer);
7680 return parameter_list;
7683 /* Parse a template-parameter.
7685 template-parameter:
7686 type-parameter
7687 parameter-declaration
7689 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7690 TREE_PURPOSE is the default value, if any. */
7692 static tree
7693 cp_parser_template_parameter (cp_parser* parser)
7695 cp_token *token;
7697 /* Peek at the next token. */
7698 token = cp_lexer_peek_token (parser->lexer);
7699 /* If it is `class' or `template', we have a type-parameter. */
7700 if (token->keyword == RID_TEMPLATE)
7701 return cp_parser_type_parameter (parser);
7702 /* If it is `class' or `typename' we do not know yet whether it is a
7703 type parameter or a non-type parameter. Consider:
7705 template <typename T, typename T::X X> ...
7709 template <class C, class D*> ...
7711 Here, the first parameter is a type parameter, and the second is
7712 a non-type parameter. We can tell by looking at the token after
7713 the identifier -- if it is a `,', `=', or `>' then we have a type
7714 parameter. */
7715 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7717 /* Peek at the token after `class' or `typename'. */
7718 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7719 /* If it's an identifier, skip it. */
7720 if (token->type == CPP_NAME)
7721 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7722 /* Now, see if the token looks like the end of a template
7723 parameter. */
7724 if (token->type == CPP_COMMA
7725 || token->type == CPP_EQ
7726 || token->type == CPP_GREATER)
7727 return cp_parser_type_parameter (parser);
7730 /* Otherwise, it is a non-type parameter.
7732 [temp.param]
7734 When parsing a default template-argument for a non-type
7735 template-parameter, the first non-nested `>' is taken as the end
7736 of the template parameter-list rather than a greater-than
7737 operator. */
7738 return
7739 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7740 /*parenthesized_p=*/NULL);
7743 /* Parse a type-parameter.
7745 type-parameter:
7746 class identifier [opt]
7747 class identifier [opt] = type-id
7748 typename identifier [opt]
7749 typename identifier [opt] = type-id
7750 template < template-parameter-list > class identifier [opt]
7751 template < template-parameter-list > class identifier [opt]
7752 = id-expression
7754 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7755 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7756 the declaration of the parameter. */
7758 static tree
7759 cp_parser_type_parameter (cp_parser* parser)
7761 cp_token *token;
7762 tree parameter;
7764 /* Look for a keyword to tell us what kind of parameter this is. */
7765 token = cp_parser_require (parser, CPP_KEYWORD,
7766 "`class', `typename', or `template'");
7767 if (!token)
7768 return error_mark_node;
7770 switch (token->keyword)
7772 case RID_CLASS:
7773 case RID_TYPENAME:
7775 tree identifier;
7776 tree default_argument;
7778 /* If the next token is an identifier, then it names the
7779 parameter. */
7780 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7781 identifier = cp_parser_identifier (parser);
7782 else
7783 identifier = NULL_TREE;
7785 /* Create the parameter. */
7786 parameter = finish_template_type_parm (class_type_node, identifier);
7788 /* If the next token is an `=', we have a default argument. */
7789 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7791 /* Consume the `=' token. */
7792 cp_lexer_consume_token (parser->lexer);
7793 /* Parse the default-argument. */
7794 default_argument = cp_parser_type_id (parser);
7796 else
7797 default_argument = NULL_TREE;
7799 /* Create the combined representation of the parameter and the
7800 default argument. */
7801 parameter = build_tree_list (default_argument, parameter);
7803 break;
7805 case RID_TEMPLATE:
7807 tree parameter_list;
7808 tree identifier;
7809 tree default_argument;
7811 /* Look for the `<'. */
7812 cp_parser_require (parser, CPP_LESS, "`<'");
7813 /* Parse the template-parameter-list. */
7814 begin_template_parm_list ();
7815 parameter_list
7816 = cp_parser_template_parameter_list (parser);
7817 parameter_list = end_template_parm_list (parameter_list);
7818 /* Look for the `>'. */
7819 cp_parser_require (parser, CPP_GREATER, "`>'");
7820 /* Look for the `class' keyword. */
7821 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7822 /* If the next token is an `=', then there is a
7823 default-argument. If the next token is a `>', we are at
7824 the end of the parameter-list. If the next token is a `,',
7825 then we are at the end of this parameter. */
7826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7827 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7828 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7829 identifier = cp_parser_identifier (parser);
7830 else
7831 identifier = NULL_TREE;
7832 /* Create the template parameter. */
7833 parameter = finish_template_template_parm (class_type_node,
7834 identifier);
7836 /* If the next token is an `=', then there is a
7837 default-argument. */
7838 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7840 bool is_template;
7842 /* Consume the `='. */
7843 cp_lexer_consume_token (parser->lexer);
7844 /* Parse the id-expression. */
7845 default_argument
7846 = cp_parser_id_expression (parser,
7847 /*template_keyword_p=*/false,
7848 /*check_dependency_p=*/true,
7849 /*template_p=*/&is_template,
7850 /*declarator_p=*/false);
7851 if (TREE_CODE (default_argument) == TYPE_DECL)
7852 /* If the id-expression was a template-id that refers to
7853 a template-class, we already have the declaration here,
7854 so no further lookup is needed. */
7856 else
7857 /* Look up the name. */
7858 default_argument
7859 = cp_parser_lookup_name (parser, default_argument,
7860 /*is_type=*/false,
7861 /*is_template=*/is_template,
7862 /*is_namespace=*/false,
7863 /*check_dependency=*/true);
7864 /* See if the default argument is valid. */
7865 default_argument
7866 = check_template_template_default_arg (default_argument);
7868 else
7869 default_argument = NULL_TREE;
7871 /* Create the combined representation of the parameter and the
7872 default argument. */
7873 parameter = build_tree_list (default_argument, parameter);
7875 break;
7877 default:
7878 /* Anything else is an error. */
7879 cp_parser_error (parser,
7880 "expected `class', `typename', or `template'");
7881 parameter = error_mark_node;
7884 return parameter;
7887 /* Parse a template-id.
7889 template-id:
7890 template-name < template-argument-list [opt] >
7892 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7893 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7894 returned. Otherwise, if the template-name names a function, or set
7895 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7896 names a class, returns a TYPE_DECL for the specialization.
7898 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7899 uninstantiated templates. */
7901 static tree
7902 cp_parser_template_id (cp_parser *parser,
7903 bool template_keyword_p,
7904 bool check_dependency_p,
7905 bool is_declaration)
7907 tree template;
7908 tree arguments;
7909 tree template_id;
7910 ptrdiff_t start_of_id;
7911 tree access_check = NULL_TREE;
7912 cp_token *next_token, *next_token_2;
7913 bool is_identifier;
7915 /* If the next token corresponds to a template-id, there is no need
7916 to reparse it. */
7917 next_token = cp_lexer_peek_token (parser->lexer);
7918 if (next_token->type == CPP_TEMPLATE_ID)
7920 tree value;
7921 tree check;
7923 /* Get the stored value. */
7924 value = cp_lexer_consume_token (parser->lexer)->value;
7925 /* Perform any access checks that were deferred. */
7926 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7927 perform_or_defer_access_check (TREE_PURPOSE (check),
7928 TREE_VALUE (check));
7929 /* Return the stored value. */
7930 return TREE_VALUE (value);
7933 /* Avoid performing name lookup if there is no possibility of
7934 finding a template-id. */
7935 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7936 || (next_token->type == CPP_NAME
7937 && !cp_parser_nth_token_starts_template_argument_list_p
7938 (parser, 2)))
7940 cp_parser_error (parser, "expected template-id");
7941 return error_mark_node;
7944 /* Remember where the template-id starts. */
7945 if (cp_parser_parsing_tentatively (parser)
7946 && !cp_parser_committed_to_tentative_parse (parser))
7948 next_token = cp_lexer_peek_token (parser->lexer);
7949 start_of_id = cp_lexer_token_difference (parser->lexer,
7950 parser->lexer->first_token,
7951 next_token);
7953 else
7954 start_of_id = -1;
7956 push_deferring_access_checks (dk_deferred);
7958 /* Parse the template-name. */
7959 is_identifier = false;
7960 template = cp_parser_template_name (parser, template_keyword_p,
7961 check_dependency_p,
7962 is_declaration,
7963 &is_identifier);
7964 if (template == error_mark_node || is_identifier)
7966 pop_deferring_access_checks ();
7967 return template;
7970 /* If we find the sequence `[:' after a template-name, it's probably
7971 a digraph-typo for `< ::'. Substitute the tokens and check if we can
7972 parse correctly the argument list. */
7973 next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7974 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7975 if (next_token->type == CPP_OPEN_SQUARE
7976 && next_token->flags & DIGRAPH
7977 && next_token_2->type == CPP_COLON
7978 && !(next_token_2->flags & PREV_WHITE))
7980 cp_parser_parse_tentatively (parser);
7981 /* Change `:' into `::'. */
7982 next_token_2->type = CPP_SCOPE;
7983 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7984 CPP_LESS. */
7985 cp_lexer_consume_token (parser->lexer);
7986 /* Parse the arguments. */
7987 arguments = cp_parser_enclosed_template_argument_list (parser);
7988 if (!cp_parser_parse_definitely (parser))
7990 /* If we couldn't parse an argument list, then we revert our changes
7991 and return simply an error. Maybe this is not a template-id
7992 after all. */
7993 next_token_2->type = CPP_COLON;
7994 cp_parser_error (parser, "expected `<'");
7995 pop_deferring_access_checks ();
7996 return error_mark_node;
7998 /* Otherwise, emit an error about the invalid digraph, but continue
7999 parsing because we got our argument list. */
8000 pedwarn ("`<::' cannot begin a template-argument list");
8001 inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8002 "between `<' and `::'");
8003 if (!flag_permissive)
8005 static bool hint;
8006 if (!hint)
8008 inform ("(if you use `-fpermissive' G++ will accept your code)");
8009 hint = true;
8013 else
8015 /* Look for the `<' that starts the template-argument-list. */
8016 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8018 pop_deferring_access_checks ();
8019 return error_mark_node;
8021 /* Parse the arguments. */
8022 arguments = cp_parser_enclosed_template_argument_list (parser);
8025 /* Build a representation of the specialization. */
8026 if (TREE_CODE (template) == IDENTIFIER_NODE)
8027 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8028 else if (DECL_CLASS_TEMPLATE_P (template)
8029 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8030 template_id
8031 = finish_template_type (template, arguments,
8032 cp_lexer_next_token_is (parser->lexer,
8033 CPP_SCOPE));
8034 else
8036 /* If it's not a class-template or a template-template, it should be
8037 a function-template. */
8038 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8039 || TREE_CODE (template) == OVERLOAD
8040 || BASELINK_P (template)),
8041 20010716);
8043 template_id = lookup_template_function (template, arguments);
8046 /* Retrieve any deferred checks. Do not pop this access checks yet
8047 so the memory will not be reclaimed during token replacing below. */
8048 access_check = get_deferred_access_checks ();
8050 /* If parsing tentatively, replace the sequence of tokens that makes
8051 up the template-id with a CPP_TEMPLATE_ID token. That way,
8052 should we re-parse the token stream, we will not have to repeat
8053 the effort required to do the parse, nor will we issue duplicate
8054 error messages about problems during instantiation of the
8055 template. */
8056 if (start_of_id >= 0)
8058 cp_token *token;
8060 /* Find the token that corresponds to the start of the
8061 template-id. */
8062 token = cp_lexer_advance_token (parser->lexer,
8063 parser->lexer->first_token,
8064 start_of_id);
8066 /* Reset the contents of the START_OF_ID token. */
8067 token->type = CPP_TEMPLATE_ID;
8068 token->value = build_tree_list (access_check, template_id);
8069 token->keyword = RID_MAX;
8070 /* Purge all subsequent tokens. */
8071 cp_lexer_purge_tokens_after (parser->lexer, token);
8074 pop_deferring_access_checks ();
8075 return template_id;
8078 /* Parse a template-name.
8080 template-name:
8081 identifier
8083 The standard should actually say:
8085 template-name:
8086 identifier
8087 operator-function-id
8089 A defect report has been filed about this issue.
8091 A conversion-function-id cannot be a template name because they cannot
8092 be part of a template-id. In fact, looking at this code:
8094 a.operator K<int>()
8096 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8097 It is impossible to call a templated conversion-function-id with an
8098 explicit argument list, since the only allowed template parameter is
8099 the type to which it is converting.
8101 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8102 `template' keyword, in a construction like:
8104 T::template f<3>()
8106 In that case `f' is taken to be a template-name, even though there
8107 is no way of knowing for sure.
8109 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8110 name refers to a set of overloaded functions, at least one of which
8111 is a template, or an IDENTIFIER_NODE with the name of the template,
8112 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8113 names are looked up inside uninstantiated templates. */
8115 static tree
8116 cp_parser_template_name (cp_parser* parser,
8117 bool template_keyword_p,
8118 bool check_dependency_p,
8119 bool is_declaration,
8120 bool *is_identifier)
8122 tree identifier;
8123 tree decl;
8124 tree fns;
8126 /* If the next token is `operator', then we have either an
8127 operator-function-id or a conversion-function-id. */
8128 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8130 /* We don't know whether we're looking at an
8131 operator-function-id or a conversion-function-id. */
8132 cp_parser_parse_tentatively (parser);
8133 /* Try an operator-function-id. */
8134 identifier = cp_parser_operator_function_id (parser);
8135 /* If that didn't work, try a conversion-function-id. */
8136 if (!cp_parser_parse_definitely (parser))
8138 cp_parser_error (parser, "expected template-name");
8139 return error_mark_node;
8142 /* Look for the identifier. */
8143 else
8144 identifier = cp_parser_identifier (parser);
8146 /* If we didn't find an identifier, we don't have a template-id. */
8147 if (identifier == error_mark_node)
8148 return error_mark_node;
8150 /* If the name immediately followed the `template' keyword, then it
8151 is a template-name. However, if the next token is not `<', then
8152 we do not treat it as a template-name, since it is not being used
8153 as part of a template-id. This enables us to handle constructs
8154 like:
8156 template <typename T> struct S { S(); };
8157 template <typename T> S<T>::S();
8159 correctly. We would treat `S' as a template -- if it were `S<T>'
8160 -- but we do not if there is no `<'. */
8162 if (processing_template_decl
8163 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8165 /* In a declaration, in a dependent context, we pretend that the
8166 "template" keyword was present in order to improve error
8167 recovery. For example, given:
8169 template <typename T> void f(T::X<int>);
8171 we want to treat "X<int>" as a template-id. */
8172 if (is_declaration
8173 && !template_keyword_p
8174 && parser->scope && TYPE_P (parser->scope)
8175 && dependent_type_p (parser->scope))
8177 ptrdiff_t start;
8178 cp_token* token;
8179 /* Explain what went wrong. */
8180 error ("non-template `%D' used as template", identifier);
8181 error ("(use `%T::template %D' to indicate that it is a template)",
8182 parser->scope, identifier);
8183 /* If parsing tentatively, find the location of the "<"
8184 token. */
8185 if (cp_parser_parsing_tentatively (parser)
8186 && !cp_parser_committed_to_tentative_parse (parser))
8188 cp_parser_simulate_error (parser);
8189 token = cp_lexer_peek_token (parser->lexer);
8190 token = cp_lexer_prev_token (parser->lexer, token);
8191 start = cp_lexer_token_difference (parser->lexer,
8192 parser->lexer->first_token,
8193 token);
8195 else
8196 start = -1;
8197 /* Parse the template arguments so that we can issue error
8198 messages about them. */
8199 cp_lexer_consume_token (parser->lexer);
8200 cp_parser_enclosed_template_argument_list (parser);
8201 /* Skip tokens until we find a good place from which to
8202 continue parsing. */
8203 cp_parser_skip_to_closing_parenthesis (parser,
8204 /*recovering=*/true,
8205 /*or_comma=*/true,
8206 /*consume_paren=*/false);
8207 /* If parsing tentatively, permanently remove the
8208 template argument list. That will prevent duplicate
8209 error messages from being issued about the missing
8210 "template" keyword. */
8211 if (start >= 0)
8213 token = cp_lexer_advance_token (parser->lexer,
8214 parser->lexer->first_token,
8215 start);
8216 cp_lexer_purge_tokens_after (parser->lexer, token);
8218 if (is_identifier)
8219 *is_identifier = true;
8220 return identifier;
8223 /* If the "template" keyword is present, then there is generally
8224 no point in doing name-lookup, so we just return IDENTIFIER.
8225 But, if the qualifying scope is non-dependent then we can
8226 (and must) do name-lookup normally. */
8227 if (template_keyword_p
8228 && (!parser->scope
8229 || (TYPE_P (parser->scope)
8230 && dependent_type_p (parser->scope))))
8231 return identifier;
8234 /* Look up the name. */
8235 decl = cp_parser_lookup_name (parser, identifier,
8236 /*is_type=*/false,
8237 /*is_template=*/false,
8238 /*is_namespace=*/false,
8239 check_dependency_p);
8240 decl = maybe_get_template_decl_from_type_decl (decl);
8242 /* If DECL is a template, then the name was a template-name. */
8243 if (TREE_CODE (decl) == TEMPLATE_DECL)
8245 else
8247 /* The standard does not explicitly indicate whether a name that
8248 names a set of overloaded declarations, some of which are
8249 templates, is a template-name. However, such a name should
8250 be a template-name; otherwise, there is no way to form a
8251 template-id for the overloaded templates. */
8252 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8253 if (TREE_CODE (fns) == OVERLOAD)
8255 tree fn;
8257 for (fn = fns; fn; fn = OVL_NEXT (fn))
8258 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8259 break;
8261 else
8263 /* Otherwise, the name does not name a template. */
8264 cp_parser_error (parser, "expected template-name");
8265 return error_mark_node;
8269 /* If DECL is dependent, and refers to a function, then just return
8270 its name; we will look it up again during template instantiation. */
8271 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8273 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8274 if (TYPE_P (scope) && dependent_type_p (scope))
8275 return identifier;
8278 return decl;
8281 /* Parse a template-argument-list.
8283 template-argument-list:
8284 template-argument
8285 template-argument-list , template-argument
8287 Returns a TREE_VEC containing the arguments. */
8289 static tree
8290 cp_parser_template_argument_list (cp_parser* parser)
8292 tree fixed_args[10];
8293 unsigned n_args = 0;
8294 unsigned alloced = 10;
8295 tree *arg_ary = fixed_args;
8296 tree vec;
8297 bool saved_in_template_argument_list_p;
8299 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8300 parser->in_template_argument_list_p = true;
8303 tree argument;
8305 if (n_args)
8306 /* Consume the comma. */
8307 cp_lexer_consume_token (parser->lexer);
8309 /* Parse the template-argument. */
8310 argument = cp_parser_template_argument (parser);
8311 if (n_args == alloced)
8313 alloced *= 2;
8315 if (arg_ary == fixed_args)
8317 arg_ary = xmalloc (sizeof (tree) * alloced);
8318 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8320 else
8321 arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8323 arg_ary[n_args++] = argument;
8325 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8327 vec = make_tree_vec (n_args);
8329 while (n_args--)
8330 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8332 if (arg_ary != fixed_args)
8333 free (arg_ary);
8334 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8335 return vec;
8338 /* Parse a template-argument.
8340 template-argument:
8341 assignment-expression
8342 type-id
8343 id-expression
8345 The representation is that of an assignment-expression, type-id, or
8346 id-expression -- except that the qualified id-expression is
8347 evaluated, so that the value returned is either a DECL or an
8348 OVERLOAD.
8350 Although the standard says "assignment-expression", it forbids
8351 throw-expressions or assignments in the template argument.
8352 Therefore, we use "conditional-expression" instead. */
8354 static tree
8355 cp_parser_template_argument (cp_parser* parser)
8357 tree argument;
8358 bool template_p;
8359 bool address_p;
8360 bool maybe_type_id = false;
8361 cp_token *token;
8362 cp_id_kind idk;
8363 tree qualifying_class;
8365 /* There's really no way to know what we're looking at, so we just
8366 try each alternative in order.
8368 [temp.arg]
8370 In a template-argument, an ambiguity between a type-id and an
8371 expression is resolved to a type-id, regardless of the form of
8372 the corresponding template-parameter.
8374 Therefore, we try a type-id first. */
8375 cp_parser_parse_tentatively (parser);
8376 argument = cp_parser_type_id (parser);
8377 /* If there was no error parsing the type-id but the next token is a '>>',
8378 we probably found a typo for '> >'. But there are type-id which are
8379 also valid expressions. For instance:
8381 struct X { int operator >> (int); };
8382 template <int V> struct Foo {};
8383 Foo<X () >> 5> r;
8385 Here 'X()' is a valid type-id of a function type, but the user just
8386 wanted to write the expression "X() >> 5". Thus, we remember that we
8387 found a valid type-id, but we still try to parse the argument as an
8388 expression to see what happens. */
8389 if (!cp_parser_error_occurred (parser)
8390 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8392 maybe_type_id = true;
8393 cp_parser_abort_tentative_parse (parser);
8395 else
8397 /* If the next token isn't a `,' or a `>', then this argument wasn't
8398 really finished. This means that the argument is not a valid
8399 type-id. */
8400 if (!cp_parser_next_token_ends_template_argument_p (parser))
8401 cp_parser_error (parser, "expected template-argument");
8402 /* If that worked, we're done. */
8403 if (cp_parser_parse_definitely (parser))
8404 return argument;
8406 /* We're still not sure what the argument will be. */
8407 cp_parser_parse_tentatively (parser);
8408 /* Try a template. */
8409 argument = cp_parser_id_expression (parser,
8410 /*template_keyword_p=*/false,
8411 /*check_dependency_p=*/true,
8412 &template_p,
8413 /*declarator_p=*/false);
8414 /* If the next token isn't a `,' or a `>', then this argument wasn't
8415 really finished. */
8416 if (!cp_parser_next_token_ends_template_argument_p (parser))
8417 cp_parser_error (parser, "expected template-argument");
8418 if (!cp_parser_error_occurred (parser))
8420 /* Figure out what is being referred to. */
8421 argument = cp_parser_lookup_name (parser, argument,
8422 /*is_type=*/false,
8423 /*is_template=*/template_p,
8424 /*is_namespace=*/false,
8425 /*check_dependency=*/true);
8426 if (TREE_CODE (argument) != TEMPLATE_DECL
8427 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8428 cp_parser_error (parser, "expected template-name");
8430 if (cp_parser_parse_definitely (parser))
8431 return argument;
8432 /* It must be a non-type argument. There permitted cases are given
8433 in [temp.arg.nontype]:
8435 -- an integral constant-expression of integral or enumeration
8436 type; or
8438 -- the name of a non-type template-parameter; or
8440 -- the name of an object or function with external linkage...
8442 -- the address of an object or function with external linkage...
8444 -- a pointer to member... */
8445 /* Look for a non-type template parameter. */
8446 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8448 cp_parser_parse_tentatively (parser);
8449 argument = cp_parser_primary_expression (parser,
8450 &idk,
8451 &qualifying_class);
8452 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8453 || !cp_parser_next_token_ends_template_argument_p (parser))
8454 cp_parser_simulate_error (parser);
8455 if (cp_parser_parse_definitely (parser))
8456 return argument;
8458 /* If the next token is "&", the argument must be the address of an
8459 object or function with external linkage. */
8460 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8461 if (address_p)
8462 cp_lexer_consume_token (parser->lexer);
8463 /* See if we might have an id-expression. */
8464 token = cp_lexer_peek_token (parser->lexer);
8465 if (token->type == CPP_NAME
8466 || token->keyword == RID_OPERATOR
8467 || token->type == CPP_SCOPE
8468 || token->type == CPP_TEMPLATE_ID
8469 || token->type == CPP_NESTED_NAME_SPECIFIER)
8471 cp_parser_parse_tentatively (parser);
8472 argument = cp_parser_primary_expression (parser,
8473 &idk,
8474 &qualifying_class);
8475 if (cp_parser_error_occurred (parser)
8476 || !cp_parser_next_token_ends_template_argument_p (parser))
8477 cp_parser_abort_tentative_parse (parser);
8478 else
8480 if (qualifying_class)
8481 argument = finish_qualified_id_expr (qualifying_class,
8482 argument,
8483 /*done=*/true,
8484 address_p);
8485 if (TREE_CODE (argument) == VAR_DECL)
8487 /* A variable without external linkage might still be a
8488 valid constant-expression, so no error is issued here
8489 if the external-linkage check fails. */
8490 if (!DECL_EXTERNAL_LINKAGE_P (argument))
8491 cp_parser_simulate_error (parser);
8493 else if (is_overloaded_fn (argument))
8494 /* All overloaded functions are allowed; if the external
8495 linkage test does not pass, an error will be issued
8496 later. */
8498 else if (address_p
8499 && (TREE_CODE (argument) == OFFSET_REF
8500 || TREE_CODE (argument) == SCOPE_REF))
8501 /* A pointer-to-member. */
8503 else
8504 cp_parser_simulate_error (parser);
8506 if (cp_parser_parse_definitely (parser))
8508 if (address_p)
8509 argument = build_x_unary_op (ADDR_EXPR, argument);
8510 return argument;
8514 /* If the argument started with "&", there are no other valid
8515 alternatives at this point. */
8516 if (address_p)
8518 cp_parser_error (parser, "invalid non-type template argument");
8519 return error_mark_node;
8521 /* If the argument wasn't successfully parsed as a type-id followed
8522 by '>>', the argument can only be a constant expression now.
8523 Otherwise, we try parsing the constant-expression tentatively,
8524 because the argument could really be a type-id. */
8525 if (maybe_type_id)
8526 cp_parser_parse_tentatively (parser);
8527 argument = cp_parser_constant_expression (parser,
8528 /*allow_non_constant_p=*/false,
8529 /*non_constant_p=*/NULL);
8530 argument = fold_non_dependent_expr (argument);
8531 if (!maybe_type_id)
8532 return argument;
8533 if (!cp_parser_next_token_ends_template_argument_p (parser))
8534 cp_parser_error (parser, "expected template-argument");
8535 if (cp_parser_parse_definitely (parser))
8536 return argument;
8537 /* We did our best to parse the argument as a non type-id, but that
8538 was the only alternative that matched (albeit with a '>' after
8539 it). We can assume it's just a typo from the user, and a
8540 diagnostic will then be issued. */
8541 return cp_parser_type_id (parser);
8544 /* Parse an explicit-instantiation.
8546 explicit-instantiation:
8547 template declaration
8549 Although the standard says `declaration', what it really means is:
8551 explicit-instantiation:
8552 template decl-specifier-seq [opt] declarator [opt] ;
8554 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8555 supposed to be allowed. A defect report has been filed about this
8556 issue.
8558 GNU Extension:
8560 explicit-instantiation:
8561 storage-class-specifier template
8562 decl-specifier-seq [opt] declarator [opt] ;
8563 function-specifier template
8564 decl-specifier-seq [opt] declarator [opt] ; */
8566 static void
8567 cp_parser_explicit_instantiation (cp_parser* parser)
8569 int declares_class_or_enum;
8570 tree decl_specifiers;
8571 tree attributes;
8572 tree extension_specifier = NULL_TREE;
8574 /* Look for an (optional) storage-class-specifier or
8575 function-specifier. */
8576 if (cp_parser_allow_gnu_extensions_p (parser))
8578 extension_specifier
8579 = cp_parser_storage_class_specifier_opt (parser);
8580 if (!extension_specifier)
8581 extension_specifier = cp_parser_function_specifier_opt (parser);
8584 /* Look for the `template' keyword. */
8585 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8586 /* Let the front end know that we are processing an explicit
8587 instantiation. */
8588 begin_explicit_instantiation ();
8589 /* [temp.explicit] says that we are supposed to ignore access
8590 control while processing explicit instantiation directives. */
8591 push_deferring_access_checks (dk_no_check);
8592 /* Parse a decl-specifier-seq. */
8593 decl_specifiers
8594 = cp_parser_decl_specifier_seq (parser,
8595 CP_PARSER_FLAGS_OPTIONAL,
8596 &attributes,
8597 &declares_class_or_enum);
8598 /* If there was exactly one decl-specifier, and it declared a class,
8599 and there's no declarator, then we have an explicit type
8600 instantiation. */
8601 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8603 tree type;
8605 type = check_tag_decl (decl_specifiers);
8606 /* Turn access control back on for names used during
8607 template instantiation. */
8608 pop_deferring_access_checks ();
8609 if (type)
8610 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8612 else
8614 tree declarator;
8615 tree decl;
8617 /* Parse the declarator. */
8618 declarator
8619 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8620 /*ctor_dtor_or_conv_p=*/NULL,
8621 /*parenthesized_p=*/NULL);
8622 cp_parser_check_for_definition_in_return_type (declarator,
8623 declares_class_or_enum);
8624 if (declarator != error_mark_node)
8626 decl = grokdeclarator (declarator, decl_specifiers,
8627 NORMAL, 0, NULL);
8628 /* Turn access control back on for names used during
8629 template instantiation. */
8630 pop_deferring_access_checks ();
8631 /* Do the explicit instantiation. */
8632 do_decl_instantiation (decl, extension_specifier);
8634 else
8636 pop_deferring_access_checks ();
8637 /* Skip the body of the explicit instantiation. */
8638 cp_parser_skip_to_end_of_statement (parser);
8641 /* We're done with the instantiation. */
8642 end_explicit_instantiation ();
8644 cp_parser_consume_semicolon_at_end_of_statement (parser);
8647 /* Parse an explicit-specialization.
8649 explicit-specialization:
8650 template < > declaration
8652 Although the standard says `declaration', what it really means is:
8654 explicit-specialization:
8655 template <> decl-specifier [opt] init-declarator [opt] ;
8656 template <> function-definition
8657 template <> explicit-specialization
8658 template <> template-declaration */
8660 static void
8661 cp_parser_explicit_specialization (cp_parser* parser)
8663 /* Look for the `template' keyword. */
8664 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8665 /* Look for the `<'. */
8666 cp_parser_require (parser, CPP_LESS, "`<'");
8667 /* Look for the `>'. */
8668 cp_parser_require (parser, CPP_GREATER, "`>'");
8669 /* We have processed another parameter list. */
8670 ++parser->num_template_parameter_lists;
8671 /* Let the front end know that we are beginning a specialization. */
8672 begin_specialization ();
8674 /* If the next keyword is `template', we need to figure out whether
8675 or not we're looking a template-declaration. */
8676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8678 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8679 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8680 cp_parser_template_declaration_after_export (parser,
8681 /*member_p=*/false);
8682 else
8683 cp_parser_explicit_specialization (parser);
8685 else
8686 /* Parse the dependent declaration. */
8687 cp_parser_single_declaration (parser,
8688 /*member_p=*/false,
8689 /*friend_p=*/NULL);
8691 /* We're done with the specialization. */
8692 end_specialization ();
8693 /* We're done with this parameter list. */
8694 --parser->num_template_parameter_lists;
8697 /* Parse a type-specifier.
8699 type-specifier:
8700 simple-type-specifier
8701 class-specifier
8702 enum-specifier
8703 elaborated-type-specifier
8704 cv-qualifier
8706 GNU Extension:
8708 type-specifier:
8709 __complex__
8711 Returns a representation of the type-specifier. If the
8712 type-specifier is a keyword (like `int' or `const', or
8713 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8714 For a class-specifier, enum-specifier, or elaborated-type-specifier
8715 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8717 If IS_FRIEND is TRUE then this type-specifier is being declared a
8718 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8719 appearing in a decl-specifier-seq.
8721 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8722 class-specifier, enum-specifier, or elaborated-type-specifier, then
8723 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8724 if a type is declared; 2 if it is defined. Otherwise, it is set to
8725 zero.
8727 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8728 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8729 is set to FALSE. */
8731 static tree
8732 cp_parser_type_specifier (cp_parser* parser,
8733 cp_parser_flags flags,
8734 bool is_friend,
8735 bool is_declaration,
8736 int* declares_class_or_enum,
8737 bool* is_cv_qualifier)
8739 tree type_spec = NULL_TREE;
8740 cp_token *token;
8741 enum rid keyword;
8743 /* Assume this type-specifier does not declare a new type. */
8744 if (declares_class_or_enum)
8745 *declares_class_or_enum = 0;
8746 /* And that it does not specify a cv-qualifier. */
8747 if (is_cv_qualifier)
8748 *is_cv_qualifier = false;
8749 /* Peek at the next token. */
8750 token = cp_lexer_peek_token (parser->lexer);
8752 /* If we're looking at a keyword, we can use that to guide the
8753 production we choose. */
8754 keyword = token->keyword;
8755 switch (keyword)
8757 /* Any of these indicate either a class-specifier, or an
8758 elaborated-type-specifier. */
8759 case RID_CLASS:
8760 case RID_STRUCT:
8761 case RID_UNION:
8762 case RID_ENUM:
8763 /* Parse tentatively so that we can back up if we don't find a
8764 class-specifier or enum-specifier. */
8765 cp_parser_parse_tentatively (parser);
8766 /* Look for the class-specifier or enum-specifier. */
8767 if (keyword == RID_ENUM)
8768 type_spec = cp_parser_enum_specifier (parser);
8769 else
8770 type_spec = cp_parser_class_specifier (parser);
8772 /* If that worked, we're done. */
8773 if (cp_parser_parse_definitely (parser))
8775 if (declares_class_or_enum)
8776 *declares_class_or_enum = 2;
8777 return type_spec;
8780 /* Fall through. */
8782 case RID_TYPENAME:
8783 /* Look for an elaborated-type-specifier. */
8784 type_spec = cp_parser_elaborated_type_specifier (parser,
8785 is_friend,
8786 is_declaration);
8787 /* We're declaring a class or enum -- unless we're using
8788 `typename'. */
8789 if (declares_class_or_enum && keyword != RID_TYPENAME)
8790 *declares_class_or_enum = 1;
8791 return type_spec;
8793 case RID_CONST:
8794 case RID_VOLATILE:
8795 case RID_RESTRICT:
8796 type_spec = cp_parser_cv_qualifier_opt (parser);
8797 /* Even though we call a routine that looks for an optional
8798 qualifier, we know that there should be one. */
8799 my_friendly_assert (type_spec != NULL, 20000328);
8800 /* This type-specifier was a cv-qualified. */
8801 if (is_cv_qualifier)
8802 *is_cv_qualifier = true;
8804 return type_spec;
8806 case RID_COMPLEX:
8807 /* The `__complex__' keyword is a GNU extension. */
8808 return cp_lexer_consume_token (parser->lexer)->value;
8810 default:
8811 break;
8814 /* If we do not already have a type-specifier, assume we are looking
8815 at a simple-type-specifier. */
8816 type_spec = cp_parser_simple_type_specifier (parser, flags,
8817 /*identifier_p=*/true);
8819 /* If we didn't find a type-specifier, and a type-specifier was not
8820 optional in this context, issue an error message. */
8821 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8823 cp_parser_error (parser, "expected type specifier");
8824 return error_mark_node;
8827 return type_spec;
8830 /* Parse a simple-type-specifier.
8832 simple-type-specifier:
8833 :: [opt] nested-name-specifier [opt] type-name
8834 :: [opt] nested-name-specifier template template-id
8835 char
8836 wchar_t
8837 bool
8838 short
8840 long
8841 signed
8842 unsigned
8843 float
8844 double
8845 void
8847 GNU Extension:
8849 simple-type-specifier:
8850 __typeof__ unary-expression
8851 __typeof__ ( type-id )
8853 For the various keywords, the value returned is simply the
8854 TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8855 For the first two productions, and if IDENTIFIER_P is false, the
8856 value returned is the indicated TYPE_DECL. */
8858 static tree
8859 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8860 bool identifier_p)
8862 tree type = NULL_TREE;
8863 cp_token *token;
8865 /* Peek at the next token. */
8866 token = cp_lexer_peek_token (parser->lexer);
8868 /* If we're looking at a keyword, things are easy. */
8869 switch (token->keyword)
8871 case RID_CHAR:
8872 type = char_type_node;
8873 break;
8874 case RID_WCHAR:
8875 type = wchar_type_node;
8876 break;
8877 case RID_BOOL:
8878 type = boolean_type_node;
8879 break;
8880 case RID_SHORT:
8881 type = short_integer_type_node;
8882 break;
8883 case RID_INT:
8884 type = integer_type_node;
8885 break;
8886 case RID_LONG:
8887 type = long_integer_type_node;
8888 break;
8889 case RID_SIGNED:
8890 type = integer_type_node;
8891 break;
8892 case RID_UNSIGNED:
8893 type = unsigned_type_node;
8894 break;
8895 case RID_FLOAT:
8896 type = float_type_node;
8897 break;
8898 case RID_DOUBLE:
8899 type = double_type_node;
8900 break;
8901 case RID_VOID:
8902 type = void_type_node;
8903 break;
8905 case RID_TYPEOF:
8907 tree operand;
8909 /* Consume the `typeof' token. */
8910 cp_lexer_consume_token (parser->lexer);
8911 /* Parse the operand to `typeof'. */
8912 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8913 /* If it is not already a TYPE, take its type. */
8914 if (!TYPE_P (operand))
8915 operand = finish_typeof (operand);
8917 return operand;
8920 default:
8921 break;
8924 /* If the type-specifier was for a built-in type, we're done. */
8925 if (type)
8927 tree id;
8929 /* Consume the token. */
8930 id = cp_lexer_consume_token (parser->lexer)->value;
8932 /* There is no valid C++ program where a non-template type is
8933 followed by a "<". That usually indicates that the user thought
8934 that the type was a template. */
8935 cp_parser_check_for_invalid_template_id (parser, type);
8937 return identifier_p ? id : TYPE_NAME (type);
8940 /* The type-specifier must be a user-defined type. */
8941 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8943 /* Don't gobble tokens or issue error messages if this is an
8944 optional type-specifier. */
8945 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8946 cp_parser_parse_tentatively (parser);
8948 /* Look for the optional `::' operator. */
8949 cp_parser_global_scope_opt (parser,
8950 /*current_scope_valid_p=*/false);
8951 /* Look for the nested-name specifier. */
8952 cp_parser_nested_name_specifier_opt (parser,
8953 /*typename_keyword_p=*/false,
8954 /*check_dependency_p=*/true,
8955 /*type_p=*/false,
8956 /*is_declaration=*/false);
8957 /* If we have seen a nested-name-specifier, and the next token
8958 is `template', then we are using the template-id production. */
8959 if (parser->scope
8960 && cp_parser_optional_template_keyword (parser))
8962 /* Look for the template-id. */
8963 type = cp_parser_template_id (parser,
8964 /*template_keyword_p=*/true,
8965 /*check_dependency_p=*/true,
8966 /*is_declaration=*/false);
8967 /* If the template-id did not name a type, we are out of
8968 luck. */
8969 if (TREE_CODE (type) != TYPE_DECL)
8971 cp_parser_error (parser, "expected template-id for type");
8972 type = NULL_TREE;
8975 /* Otherwise, look for a type-name. */
8976 else
8977 type = cp_parser_type_name (parser);
8978 /* If it didn't work out, we don't have a TYPE. */
8979 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8980 && !cp_parser_parse_definitely (parser))
8981 type = NULL_TREE;
8984 /* If we didn't get a type-name, issue an error message. */
8985 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8987 cp_parser_error (parser, "expected type-name");
8988 return error_mark_node;
8991 /* There is no valid C++ program where a non-template type is
8992 followed by a "<". That usually indicates that the user thought
8993 that the type was a template. */
8994 if (type && type != error_mark_node)
8995 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8997 return type;
9000 /* Parse a type-name.
9002 type-name:
9003 class-name
9004 enum-name
9005 typedef-name
9007 enum-name:
9008 identifier
9010 typedef-name:
9011 identifier
9013 Returns a TYPE_DECL for the the type. */
9015 static tree
9016 cp_parser_type_name (cp_parser* parser)
9018 tree type_decl;
9019 tree identifier;
9021 /* We can't know yet whether it is a class-name or not. */
9022 cp_parser_parse_tentatively (parser);
9023 /* Try a class-name. */
9024 type_decl = cp_parser_class_name (parser,
9025 /*typename_keyword_p=*/false,
9026 /*template_keyword_p=*/false,
9027 /*type_p=*/false,
9028 /*check_dependency_p=*/true,
9029 /*class_head_p=*/false,
9030 /*is_declaration=*/false);
9031 /* If it's not a class-name, keep looking. */
9032 if (!cp_parser_parse_definitely (parser))
9034 /* It must be a typedef-name or an enum-name. */
9035 identifier = cp_parser_identifier (parser);
9036 if (identifier == error_mark_node)
9037 return error_mark_node;
9039 /* Look up the type-name. */
9040 type_decl = cp_parser_lookup_name_simple (parser, identifier);
9041 /* Issue an error if we did not find a type-name. */
9042 if (TREE_CODE (type_decl) != TYPE_DECL)
9044 if (!cp_parser_simulate_error (parser))
9045 cp_parser_name_lookup_error (parser, identifier, type_decl,
9046 "is not a type");
9047 type_decl = error_mark_node;
9049 /* Remember that the name was used in the definition of the
9050 current class so that we can check later to see if the
9051 meaning would have been different after the class was
9052 entirely defined. */
9053 else if (type_decl != error_mark_node
9054 && !parser->scope)
9055 maybe_note_name_used_in_class (identifier, type_decl);
9058 return type_decl;
9062 /* Parse an elaborated-type-specifier. Note that the grammar given
9063 here incorporates the resolution to DR68.
9065 elaborated-type-specifier:
9066 class-key :: [opt] nested-name-specifier [opt] identifier
9067 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9068 enum :: [opt] nested-name-specifier [opt] identifier
9069 typename :: [opt] nested-name-specifier identifier
9070 typename :: [opt] nested-name-specifier template [opt]
9071 template-id
9073 GNU extension:
9075 elaborated-type-specifier:
9076 class-key attributes :: [opt] nested-name-specifier [opt] identifier
9077 class-key attributes :: [opt] nested-name-specifier [opt]
9078 template [opt] template-id
9079 enum attributes :: [opt] nested-name-specifier [opt] identifier
9081 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9082 declared `friend'. If IS_DECLARATION is TRUE, then this
9083 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9084 something is being declared.
9086 Returns the TYPE specified. */
9088 static tree
9089 cp_parser_elaborated_type_specifier (cp_parser* parser,
9090 bool is_friend,
9091 bool is_declaration)
9093 enum tag_types tag_type;
9094 tree identifier;
9095 tree type = NULL_TREE;
9096 tree attributes = NULL_TREE;
9098 /* See if we're looking at the `enum' keyword. */
9099 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9101 /* Consume the `enum' token. */
9102 cp_lexer_consume_token (parser->lexer);
9103 /* Remember that it's an enumeration type. */
9104 tag_type = enum_type;
9105 /* Parse the attributes. */
9106 attributes = cp_parser_attributes_opt (parser);
9108 /* Or, it might be `typename'. */
9109 else if (cp_lexer_next_token_is_keyword (parser->lexer,
9110 RID_TYPENAME))
9112 /* Consume the `typename' token. */
9113 cp_lexer_consume_token (parser->lexer);
9114 /* Remember that it's a `typename' type. */
9115 tag_type = typename_type;
9116 /* The `typename' keyword is only allowed in templates. */
9117 if (!processing_template_decl)
9118 pedwarn ("using `typename' outside of template");
9120 /* Otherwise it must be a class-key. */
9121 else
9123 tag_type = cp_parser_class_key (parser);
9124 if (tag_type == none_type)
9125 return error_mark_node;
9126 /* Parse the attributes. */
9127 attributes = cp_parser_attributes_opt (parser);
9130 /* Look for the `::' operator. */
9131 cp_parser_global_scope_opt (parser,
9132 /*current_scope_valid_p=*/false);
9133 /* Look for the nested-name-specifier. */
9134 if (tag_type == typename_type)
9136 if (cp_parser_nested_name_specifier (parser,
9137 /*typename_keyword_p=*/true,
9138 /*check_dependency_p=*/true,
9139 /*type_p=*/true,
9140 is_declaration)
9141 == error_mark_node)
9142 return error_mark_node;
9144 else
9145 /* Even though `typename' is not present, the proposed resolution
9146 to Core Issue 180 says that in `class A<T>::B', `B' should be
9147 considered a type-name, even if `A<T>' is dependent. */
9148 cp_parser_nested_name_specifier_opt (parser,
9149 /*typename_keyword_p=*/true,
9150 /*check_dependency_p=*/true,
9151 /*type_p=*/true,
9152 is_declaration);
9153 /* For everything but enumeration types, consider a template-id. */
9154 if (tag_type != enum_type)
9156 bool template_p = false;
9157 tree decl;
9159 /* Allow the `template' keyword. */
9160 template_p = cp_parser_optional_template_keyword (parser);
9161 /* If we didn't see `template', we don't know if there's a
9162 template-id or not. */
9163 if (!template_p)
9164 cp_parser_parse_tentatively (parser);
9165 /* Parse the template-id. */
9166 decl = cp_parser_template_id (parser, template_p,
9167 /*check_dependency_p=*/true,
9168 is_declaration);
9169 /* If we didn't find a template-id, look for an ordinary
9170 identifier. */
9171 if (!template_p && !cp_parser_parse_definitely (parser))
9173 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9174 in effect, then we must assume that, upon instantiation, the
9175 template will correspond to a class. */
9176 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9177 && tag_type == typename_type)
9178 type = make_typename_type (parser->scope, decl,
9179 /*complain=*/1);
9180 else
9181 type = TREE_TYPE (decl);
9184 /* For an enumeration type, consider only a plain identifier. */
9185 if (!type)
9187 identifier = cp_parser_identifier (parser);
9189 if (identifier == error_mark_node)
9191 parser->scope = NULL_TREE;
9192 return error_mark_node;
9195 /* For a `typename', we needn't call xref_tag. */
9196 if (tag_type == typename_type)
9197 return cp_parser_make_typename_type (parser, parser->scope,
9198 identifier);
9199 /* Look up a qualified name in the usual way. */
9200 if (parser->scope)
9202 tree decl;
9204 /* In an elaborated-type-specifier, names are assumed to name
9205 types, so we set IS_TYPE to TRUE when calling
9206 cp_parser_lookup_name. */
9207 decl = cp_parser_lookup_name (parser, identifier,
9208 /*is_type=*/true,
9209 /*is_template=*/false,
9210 /*is_namespace=*/false,
9211 /*check_dependency=*/true);
9213 /* If we are parsing friend declaration, DECL may be a
9214 TEMPLATE_DECL tree node here. However, we need to check
9215 whether this TEMPLATE_DECL results in valid code. Consider
9216 the following example:
9218 namespace N {
9219 template <class T> class C {};
9221 class X {
9222 template <class T> friend class N::C; // #1, valid code
9224 template <class T> class Y {
9225 friend class N::C; // #2, invalid code
9228 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9229 name lookup of `N::C'. We see that friend declaration must
9230 be template for the code to be valid. Note that
9231 processing_template_decl does not work here since it is
9232 always 1 for the above two cases. */
9234 decl = (cp_parser_maybe_treat_template_as_class
9235 (decl, /*tag_name_p=*/is_friend
9236 && parser->num_template_parameter_lists));
9238 if (TREE_CODE (decl) != TYPE_DECL)
9240 error ("expected type-name");
9241 return error_mark_node;
9244 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9245 check_elaborated_type_specifier
9246 (tag_type, decl,
9247 (parser->num_template_parameter_lists
9248 || DECL_SELF_REFERENCE_P (decl)));
9250 type = TREE_TYPE (decl);
9252 else
9254 /* An elaborated-type-specifier sometimes introduces a new type and
9255 sometimes names an existing type. Normally, the rule is that it
9256 introduces a new type only if there is not an existing type of
9257 the same name already in scope. For example, given:
9259 struct S {};
9260 void f() { struct S s; }
9262 the `struct S' in the body of `f' is the same `struct S' as in
9263 the global scope; the existing definition is used. However, if
9264 there were no global declaration, this would introduce a new
9265 local class named `S'.
9267 An exception to this rule applies to the following code:
9269 namespace N { struct S; }
9271 Here, the elaborated-type-specifier names a new type
9272 unconditionally; even if there is already an `S' in the
9273 containing scope this declaration names a new type.
9274 This exception only applies if the elaborated-type-specifier
9275 forms the complete declaration:
9277 [class.name]
9279 A declaration consisting solely of `class-key identifier ;' is
9280 either a redeclaration of the name in the current scope or a
9281 forward declaration of the identifier as a class name. It
9282 introduces the name into the current scope.
9284 We are in this situation precisely when the next token is a `;'.
9286 An exception to the exception is that a `friend' declaration does
9287 *not* name a new type; i.e., given:
9289 struct S { friend struct T; };
9291 `T' is not a new type in the scope of `S'.
9293 Also, `new struct S' or `sizeof (struct S)' never results in the
9294 definition of a new type; a new type can only be declared in a
9295 declaration context. */
9297 /* Warn about attributes. They are ignored. */
9298 if (attributes)
9299 warning ("type attributes are honored only at type definition");
9301 type = xref_tag (tag_type, identifier,
9302 (is_friend
9303 || !is_declaration
9304 || cp_lexer_next_token_is_not (parser->lexer,
9305 CPP_SEMICOLON)),
9306 parser->num_template_parameter_lists);
9309 if (tag_type != enum_type)
9310 cp_parser_check_class_key (tag_type, type);
9312 /* A "<" cannot follow an elaborated type specifier. If that
9313 happens, the user was probably trying to form a template-id. */
9314 cp_parser_check_for_invalid_template_id (parser, type);
9316 return type;
9319 /* Parse an enum-specifier.
9321 enum-specifier:
9322 enum identifier [opt] { enumerator-list [opt] }
9324 Returns an ENUM_TYPE representing the enumeration. */
9326 static tree
9327 cp_parser_enum_specifier (cp_parser* parser)
9329 cp_token *token;
9330 tree identifier = NULL_TREE;
9331 tree type;
9333 /* Look for the `enum' keyword. */
9334 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9335 return error_mark_node;
9336 /* Peek at the next token. */
9337 token = cp_lexer_peek_token (parser->lexer);
9339 /* See if it is an identifier. */
9340 if (token->type == CPP_NAME)
9341 identifier = cp_parser_identifier (parser);
9343 /* Look for the `{'. */
9344 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9345 return error_mark_node;
9347 /* At this point, we're going ahead with the enum-specifier, even
9348 if some other problem occurs. */
9349 cp_parser_commit_to_tentative_parse (parser);
9351 /* Issue an error message if type-definitions are forbidden here. */
9352 cp_parser_check_type_definition (parser);
9354 /* Create the new type. */
9355 type = start_enum (identifier ? identifier : make_anon_name ());
9357 /* Peek at the next token. */
9358 token = cp_lexer_peek_token (parser->lexer);
9359 /* If it's not a `}', then there are some enumerators. */
9360 if (token->type != CPP_CLOSE_BRACE)
9361 cp_parser_enumerator_list (parser, type);
9362 /* Look for the `}'. */
9363 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9365 /* Finish up the enumeration. */
9366 finish_enum (type);
9368 return type;
9371 /* Parse an enumerator-list. The enumerators all have the indicated
9372 TYPE.
9374 enumerator-list:
9375 enumerator-definition
9376 enumerator-list , enumerator-definition */
9378 static void
9379 cp_parser_enumerator_list (cp_parser* parser, tree type)
9381 while (true)
9383 cp_token *token;
9385 /* Parse an enumerator-definition. */
9386 cp_parser_enumerator_definition (parser, type);
9387 /* Peek at the next token. */
9388 token = cp_lexer_peek_token (parser->lexer);
9389 /* If it's not a `,', then we've reached the end of the
9390 list. */
9391 if (token->type != CPP_COMMA)
9392 break;
9393 /* Otherwise, consume the `,' and keep going. */
9394 cp_lexer_consume_token (parser->lexer);
9395 /* If the next token is a `}', there is a trailing comma. */
9396 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9398 if (pedantic && !in_system_header)
9399 pedwarn ("comma at end of enumerator list");
9400 break;
9405 /* Parse an enumerator-definition. The enumerator has the indicated
9406 TYPE.
9408 enumerator-definition:
9409 enumerator
9410 enumerator = constant-expression
9412 enumerator:
9413 identifier */
9415 static void
9416 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9418 cp_token *token;
9419 tree identifier;
9420 tree value;
9422 /* Look for the identifier. */
9423 identifier = cp_parser_identifier (parser);
9424 if (identifier == error_mark_node)
9425 return;
9427 /* Peek at the next token. */
9428 token = cp_lexer_peek_token (parser->lexer);
9429 /* If it's an `=', then there's an explicit value. */
9430 if (token->type == CPP_EQ)
9432 /* Consume the `=' token. */
9433 cp_lexer_consume_token (parser->lexer);
9434 /* Parse the value. */
9435 value = cp_parser_constant_expression (parser,
9436 /*allow_non_constant_p=*/false,
9437 NULL);
9439 else
9440 value = NULL_TREE;
9442 /* Create the enumerator. */
9443 build_enumerator (identifier, value, type);
9446 /* Parse a namespace-name.
9448 namespace-name:
9449 original-namespace-name
9450 namespace-alias
9452 Returns the NAMESPACE_DECL for the namespace. */
9454 static tree
9455 cp_parser_namespace_name (cp_parser* parser)
9457 tree identifier;
9458 tree namespace_decl;
9460 /* Get the name of the namespace. */
9461 identifier = cp_parser_identifier (parser);
9462 if (identifier == error_mark_node)
9463 return error_mark_node;
9465 /* Look up the identifier in the currently active scope. Look only
9466 for namespaces, due to:
9468 [basic.lookup.udir]
9470 When looking up a namespace-name in a using-directive or alias
9471 definition, only namespace names are considered.
9473 And:
9475 [basic.lookup.qual]
9477 During the lookup of a name preceding the :: scope resolution
9478 operator, object, function, and enumerator names are ignored.
9480 (Note that cp_parser_class_or_namespace_name only calls this
9481 function if the token after the name is the scope resolution
9482 operator.) */
9483 namespace_decl = cp_parser_lookup_name (parser, identifier,
9484 /*is_type=*/false,
9485 /*is_template=*/false,
9486 /*is_namespace=*/true,
9487 /*check_dependency=*/true);
9488 /* If it's not a namespace, issue an error. */
9489 if (namespace_decl == error_mark_node
9490 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9492 cp_parser_error (parser, "expected namespace-name");
9493 namespace_decl = error_mark_node;
9496 return namespace_decl;
9499 /* Parse a namespace-definition.
9501 namespace-definition:
9502 named-namespace-definition
9503 unnamed-namespace-definition
9505 named-namespace-definition:
9506 original-namespace-definition
9507 extension-namespace-definition
9509 original-namespace-definition:
9510 namespace identifier { namespace-body }
9512 extension-namespace-definition:
9513 namespace original-namespace-name { namespace-body }
9515 unnamed-namespace-definition:
9516 namespace { namespace-body } */
9518 static void
9519 cp_parser_namespace_definition (cp_parser* parser)
9521 tree identifier;
9523 /* Look for the `namespace' keyword. */
9524 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9526 /* Get the name of the namespace. We do not attempt to distinguish
9527 between an original-namespace-definition and an
9528 extension-namespace-definition at this point. The semantic
9529 analysis routines are responsible for that. */
9530 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9531 identifier = cp_parser_identifier (parser);
9532 else
9533 identifier = NULL_TREE;
9535 /* Look for the `{' to start the namespace. */
9536 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9537 /* Start the namespace. */
9538 push_namespace (identifier);
9539 /* Parse the body of the namespace. */
9540 cp_parser_namespace_body (parser);
9541 /* Finish the namespace. */
9542 pop_namespace ();
9543 /* Look for the final `}'. */
9544 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9547 /* Parse a namespace-body.
9549 namespace-body:
9550 declaration-seq [opt] */
9552 static void
9553 cp_parser_namespace_body (cp_parser* parser)
9555 cp_parser_declaration_seq_opt (parser);
9558 /* Parse a namespace-alias-definition.
9560 namespace-alias-definition:
9561 namespace identifier = qualified-namespace-specifier ; */
9563 static void
9564 cp_parser_namespace_alias_definition (cp_parser* parser)
9566 tree identifier;
9567 tree namespace_specifier;
9569 /* Look for the `namespace' keyword. */
9570 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9571 /* Look for the identifier. */
9572 identifier = cp_parser_identifier (parser);
9573 if (identifier == error_mark_node)
9574 return;
9575 /* Look for the `=' token. */
9576 cp_parser_require (parser, CPP_EQ, "`='");
9577 /* Look for the qualified-namespace-specifier. */
9578 namespace_specifier
9579 = cp_parser_qualified_namespace_specifier (parser);
9580 /* Look for the `;' token. */
9581 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9583 /* Register the alias in the symbol table. */
9584 do_namespace_alias (identifier, namespace_specifier);
9587 /* Parse a qualified-namespace-specifier.
9589 qualified-namespace-specifier:
9590 :: [opt] nested-name-specifier [opt] namespace-name
9592 Returns a NAMESPACE_DECL corresponding to the specified
9593 namespace. */
9595 static tree
9596 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9598 /* Look for the optional `::'. */
9599 cp_parser_global_scope_opt (parser,
9600 /*current_scope_valid_p=*/false);
9602 /* Look for the optional nested-name-specifier. */
9603 cp_parser_nested_name_specifier_opt (parser,
9604 /*typename_keyword_p=*/false,
9605 /*check_dependency_p=*/true,
9606 /*type_p=*/false,
9607 /*is_declaration=*/true);
9609 return cp_parser_namespace_name (parser);
9612 /* Parse a using-declaration.
9614 using-declaration:
9615 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9616 using :: unqualified-id ; */
9618 static void
9619 cp_parser_using_declaration (cp_parser* parser)
9621 cp_token *token;
9622 bool typename_p = false;
9623 bool global_scope_p;
9624 tree decl;
9625 tree identifier;
9626 tree scope;
9627 tree qscope;
9629 /* Look for the `using' keyword. */
9630 cp_parser_require_keyword (parser, RID_USING, "`using'");
9632 /* Peek at the next token. */
9633 token = cp_lexer_peek_token (parser->lexer);
9634 /* See if it's `typename'. */
9635 if (token->keyword == RID_TYPENAME)
9637 /* Remember that we've seen it. */
9638 typename_p = true;
9639 /* Consume the `typename' token. */
9640 cp_lexer_consume_token (parser->lexer);
9643 /* Look for the optional global scope qualification. */
9644 global_scope_p
9645 = (cp_parser_global_scope_opt (parser,
9646 /*current_scope_valid_p=*/false)
9647 != NULL_TREE);
9649 /* If we saw `typename', or didn't see `::', then there must be a
9650 nested-name-specifier present. */
9651 if (typename_p || !global_scope_p)
9652 qscope = cp_parser_nested_name_specifier (parser, typename_p,
9653 /*check_dependency_p=*/true,
9654 /*type_p=*/false,
9655 /*is_declaration=*/true);
9656 /* Otherwise, we could be in either of the two productions. In that
9657 case, treat the nested-name-specifier as optional. */
9658 else
9659 qscope = cp_parser_nested_name_specifier_opt (parser,
9660 /*typename_keyword_p=*/false,
9661 /*check_dependency_p=*/true,
9662 /*type_p=*/false,
9663 /*is_declaration=*/true);
9664 if (!qscope)
9665 qscope = global_namespace;
9667 /* Parse the unqualified-id. */
9668 identifier = cp_parser_unqualified_id (parser,
9669 /*template_keyword_p=*/false,
9670 /*check_dependency_p=*/true,
9671 /*declarator_p=*/true);
9673 /* The function we call to handle a using-declaration is different
9674 depending on what scope we are in. */
9675 if (identifier == error_mark_node)
9677 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9678 && TREE_CODE (identifier) != BIT_NOT_EXPR)
9679 /* [namespace.udecl]
9681 A using declaration shall not name a template-id. */
9682 error ("a template-id may not appear in a using-declaration");
9683 else
9685 scope = current_scope ();
9686 if (scope && TYPE_P (scope))
9688 /* Create the USING_DECL. */
9689 decl = do_class_using_decl (build_nt (SCOPE_REF,
9690 parser->scope,
9691 identifier));
9692 /* Add it to the list of members in this class. */
9693 finish_member_declaration (decl);
9695 else
9697 decl = cp_parser_lookup_name_simple (parser, identifier);
9698 if (decl == error_mark_node)
9699 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9700 else if (scope)
9701 do_local_using_decl (decl, qscope, identifier);
9702 else
9703 do_toplevel_using_decl (decl, qscope, identifier);
9707 /* Look for the final `;'. */
9708 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9711 /* Parse a using-directive.
9713 using-directive:
9714 using namespace :: [opt] nested-name-specifier [opt]
9715 namespace-name ; */
9717 static void
9718 cp_parser_using_directive (cp_parser* parser)
9720 tree namespace_decl;
9721 tree attribs;
9723 /* Look for the `using' keyword. */
9724 cp_parser_require_keyword (parser, RID_USING, "`using'");
9725 /* And the `namespace' keyword. */
9726 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9727 /* Look for the optional `::' operator. */
9728 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9729 /* And the optional nested-name-specifier. */
9730 cp_parser_nested_name_specifier_opt (parser,
9731 /*typename_keyword_p=*/false,
9732 /*check_dependency_p=*/true,
9733 /*type_p=*/false,
9734 /*is_declaration=*/true);
9735 /* Get the namespace being used. */
9736 namespace_decl = cp_parser_namespace_name (parser);
9737 /* And any specified attributes. */
9738 attribs = cp_parser_attributes_opt (parser);
9739 /* Update the symbol table. */
9740 parse_using_directive (namespace_decl, attribs);
9741 /* Look for the final `;'. */
9742 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9745 /* Parse an asm-definition.
9747 asm-definition:
9748 asm ( string-literal ) ;
9750 GNU Extension:
9752 asm-definition:
9753 asm volatile [opt] ( string-literal ) ;
9754 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9755 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9756 : asm-operand-list [opt] ) ;
9757 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9758 : asm-operand-list [opt]
9759 : asm-operand-list [opt] ) ; */
9761 static void
9762 cp_parser_asm_definition (cp_parser* parser)
9764 cp_token *token;
9765 tree string;
9766 tree outputs = NULL_TREE;
9767 tree inputs = NULL_TREE;
9768 tree clobbers = NULL_TREE;
9769 tree asm_stmt;
9770 bool volatile_p = false;
9771 bool extended_p = false;
9773 /* Look for the `asm' keyword. */
9774 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9775 /* See if the next token is `volatile'. */
9776 if (cp_parser_allow_gnu_extensions_p (parser)
9777 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9779 /* Remember that we saw the `volatile' keyword. */
9780 volatile_p = true;
9781 /* Consume the token. */
9782 cp_lexer_consume_token (parser->lexer);
9784 /* Look for the opening `('. */
9785 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9786 /* Look for the string. */
9787 c_lex_string_translate = false;
9788 token = cp_parser_require (parser, CPP_STRING, "asm body");
9789 if (!token)
9790 goto finish;
9791 string = token->value;
9792 /* If we're allowing GNU extensions, check for the extended assembly
9793 syntax. Unfortunately, the `:' tokens need not be separated by
9794 a space in C, and so, for compatibility, we tolerate that here
9795 too. Doing that means that we have to treat the `::' operator as
9796 two `:' tokens. */
9797 if (cp_parser_allow_gnu_extensions_p (parser)
9798 && at_function_scope_p ()
9799 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9800 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9802 bool inputs_p = false;
9803 bool clobbers_p = false;
9805 /* The extended syntax was used. */
9806 extended_p = true;
9808 /* Look for outputs. */
9809 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9811 /* Consume the `:'. */
9812 cp_lexer_consume_token (parser->lexer);
9813 /* Parse the output-operands. */
9814 if (cp_lexer_next_token_is_not (parser->lexer,
9815 CPP_COLON)
9816 && cp_lexer_next_token_is_not (parser->lexer,
9817 CPP_SCOPE)
9818 && cp_lexer_next_token_is_not (parser->lexer,
9819 CPP_CLOSE_PAREN))
9820 outputs = cp_parser_asm_operand_list (parser);
9822 /* If the next token is `::', there are no outputs, and the
9823 next token is the beginning of the inputs. */
9824 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9826 /* Consume the `::' token. */
9827 cp_lexer_consume_token (parser->lexer);
9828 /* The inputs are coming next. */
9829 inputs_p = true;
9832 /* Look for inputs. */
9833 if (inputs_p
9834 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9836 if (!inputs_p)
9837 /* Consume the `:'. */
9838 cp_lexer_consume_token (parser->lexer);
9839 /* Parse the output-operands. */
9840 if (cp_lexer_next_token_is_not (parser->lexer,
9841 CPP_COLON)
9842 && cp_lexer_next_token_is_not (parser->lexer,
9843 CPP_SCOPE)
9844 && cp_lexer_next_token_is_not (parser->lexer,
9845 CPP_CLOSE_PAREN))
9846 inputs = cp_parser_asm_operand_list (parser);
9848 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9849 /* The clobbers are coming next. */
9850 clobbers_p = true;
9852 /* Look for clobbers. */
9853 if (clobbers_p
9854 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9856 if (!clobbers_p)
9857 /* Consume the `:'. */
9858 cp_lexer_consume_token (parser->lexer);
9859 /* Parse the clobbers. */
9860 if (cp_lexer_next_token_is_not (parser->lexer,
9861 CPP_CLOSE_PAREN))
9862 clobbers = cp_parser_asm_clobber_list (parser);
9865 /* Look for the closing `)'. */
9866 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9867 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9868 /*consume_paren=*/true);
9869 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9871 /* Create the ASM_STMT. */
9872 if (at_function_scope_p ())
9874 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
9875 inputs, clobbers);
9876 /* If the extended syntax was not used, mark the ASM_STMT. */
9877 if (!extended_p)
9878 ASM_INPUT_P (asm_stmt) = 1;
9880 else
9881 assemble_asm (string);
9883 finish:
9884 c_lex_string_translate = true;
9887 /* Declarators [gram.dcl.decl] */
9889 /* Parse an init-declarator.
9891 init-declarator:
9892 declarator initializer [opt]
9894 GNU Extension:
9896 init-declarator:
9897 declarator asm-specification [opt] attributes [opt] initializer [opt]
9899 function-definition:
9900 decl-specifier-seq [opt] declarator ctor-initializer [opt]
9901 function-body
9902 decl-specifier-seq [opt] declarator function-try-block
9904 GNU Extension:
9906 function-definition:
9907 __extension__ function-definition
9909 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9910 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9911 then this declarator appears in a class scope. The new DECL created
9912 by this declarator is returned.
9914 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9915 for a function-definition here as well. If the declarator is a
9916 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9917 be TRUE upon return. By that point, the function-definition will
9918 have been completely parsed.
9920 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9921 is FALSE. */
9923 static tree
9924 cp_parser_init_declarator (cp_parser* parser,
9925 tree decl_specifiers,
9926 tree prefix_attributes,
9927 bool function_definition_allowed_p,
9928 bool member_p,
9929 int declares_class_or_enum,
9930 bool* function_definition_p)
9932 cp_token *token;
9933 tree declarator;
9934 tree attributes;
9935 tree asm_specification;
9936 tree initializer;
9937 tree decl = NULL_TREE;
9938 tree scope;
9939 bool is_initialized;
9940 bool is_parenthesized_init;
9941 bool is_non_constant_init;
9942 int ctor_dtor_or_conv_p;
9943 bool friend_p;
9944 bool pop_p = false;
9946 /* Assume that this is not the declarator for a function
9947 definition. */
9948 if (function_definition_p)
9949 *function_definition_p = false;
9951 /* Defer access checks while parsing the declarator; we cannot know
9952 what names are accessible until we know what is being
9953 declared. */
9954 resume_deferring_access_checks ();
9956 /* Parse the declarator. */
9957 declarator
9958 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9959 &ctor_dtor_or_conv_p,
9960 /*parenthesized_p=*/NULL);
9961 /* Gather up the deferred checks. */
9962 stop_deferring_access_checks ();
9964 /* If the DECLARATOR was erroneous, there's no need to go
9965 further. */
9966 if (declarator == error_mark_node)
9967 return error_mark_node;
9969 cp_parser_check_for_definition_in_return_type (declarator,
9970 declares_class_or_enum);
9972 /* Figure out what scope the entity declared by the DECLARATOR is
9973 located in. `grokdeclarator' sometimes changes the scope, so
9974 we compute it now. */
9975 scope = get_scope_of_declarator (declarator);
9977 /* If we're allowing GNU extensions, look for an asm-specification
9978 and attributes. */
9979 if (cp_parser_allow_gnu_extensions_p (parser))
9981 /* Look for an asm-specification. */
9982 asm_specification = cp_parser_asm_specification_opt (parser);
9983 /* And attributes. */
9984 attributes = cp_parser_attributes_opt (parser);
9986 else
9988 asm_specification = NULL_TREE;
9989 attributes = NULL_TREE;
9992 /* Peek at the next token. */
9993 token = cp_lexer_peek_token (parser->lexer);
9994 /* Check to see if the token indicates the start of a
9995 function-definition. */
9996 if (cp_parser_token_starts_function_definition_p (token))
9998 if (!function_definition_allowed_p)
10000 /* If a function-definition should not appear here, issue an
10001 error message. */
10002 cp_parser_error (parser,
10003 "a function-definition is not allowed here");
10004 return error_mark_node;
10006 else
10008 /* Neither attributes nor an asm-specification are allowed
10009 on a function-definition. */
10010 if (asm_specification)
10011 error ("an asm-specification is not allowed on a function-definition");
10012 if (attributes)
10013 error ("attributes are not allowed on a function-definition");
10014 /* This is a function-definition. */
10015 *function_definition_p = true;
10017 /* Parse the function definition. */
10018 if (member_p)
10019 decl = cp_parser_save_member_function_body (parser,
10020 decl_specifiers,
10021 declarator,
10022 prefix_attributes);
10023 else
10024 decl
10025 = (cp_parser_function_definition_from_specifiers_and_declarator
10026 (parser, decl_specifiers, prefix_attributes, declarator));
10028 return decl;
10032 /* [dcl.dcl]
10034 Only in function declarations for constructors, destructors, and
10035 type conversions can the decl-specifier-seq be omitted.
10037 We explicitly postpone this check past the point where we handle
10038 function-definitions because we tolerate function-definitions
10039 that are missing their return types in some modes. */
10040 if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10042 cp_parser_error (parser,
10043 "expected constructor, destructor, or type conversion");
10044 return error_mark_node;
10047 /* An `=' or an `(' indicates an initializer. */
10048 is_initialized = (token->type == CPP_EQ
10049 || token->type == CPP_OPEN_PAREN);
10050 /* If the init-declarator isn't initialized and isn't followed by a
10051 `,' or `;', it's not a valid init-declarator. */
10052 if (!is_initialized
10053 && token->type != CPP_COMMA
10054 && token->type != CPP_SEMICOLON)
10056 cp_parser_error (parser, "expected init-declarator");
10057 return error_mark_node;
10060 /* Because start_decl has side-effects, we should only call it if we
10061 know we're going ahead. By this point, we know that we cannot
10062 possibly be looking at any other construct. */
10063 cp_parser_commit_to_tentative_parse (parser);
10065 /* If the decl specifiers were bad, issue an error now that we're
10066 sure this was intended to be a declarator. Then continue
10067 declaring the variable(s), as int, to try to cut down on further
10068 errors. */
10069 if (decl_specifiers != NULL
10070 && TREE_VALUE (decl_specifiers) == error_mark_node)
10072 cp_parser_error (parser, "invalid type in declaration");
10073 TREE_VALUE (decl_specifiers) = integer_type_node;
10076 /* Check to see whether or not this declaration is a friend. */
10077 friend_p = cp_parser_friend_p (decl_specifiers);
10079 /* Check that the number of template-parameter-lists is OK. */
10080 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10081 return error_mark_node;
10083 /* Enter the newly declared entry in the symbol table. If we're
10084 processing a declaration in a class-specifier, we wait until
10085 after processing the initializer. */
10086 if (!member_p)
10088 if (parser->in_unbraced_linkage_specification_p)
10090 decl_specifiers = tree_cons (error_mark_node,
10091 get_identifier ("extern"),
10092 decl_specifiers);
10093 have_extern_spec = false;
10095 decl = start_decl (declarator, decl_specifiers,
10096 is_initialized, attributes, prefix_attributes);
10099 /* Enter the SCOPE. That way unqualified names appearing in the
10100 initializer will be looked up in SCOPE. */
10101 if (scope)
10102 pop_p = push_scope (scope);
10104 /* Perform deferred access control checks, now that we know in which
10105 SCOPE the declared entity resides. */
10106 if (!member_p && decl)
10108 tree saved_current_function_decl = NULL_TREE;
10110 /* If the entity being declared is a function, pretend that we
10111 are in its scope. If it is a `friend', it may have access to
10112 things that would not otherwise be accessible. */
10113 if (TREE_CODE (decl) == FUNCTION_DECL)
10115 saved_current_function_decl = current_function_decl;
10116 current_function_decl = decl;
10119 /* Perform the access control checks for the declarator and the
10120 the decl-specifiers. */
10121 perform_deferred_access_checks ();
10123 /* Restore the saved value. */
10124 if (TREE_CODE (decl) == FUNCTION_DECL)
10125 current_function_decl = saved_current_function_decl;
10128 /* Parse the initializer. */
10129 if (is_initialized)
10130 initializer = cp_parser_initializer (parser,
10131 &is_parenthesized_init,
10132 &is_non_constant_init);
10133 else
10135 initializer = NULL_TREE;
10136 is_parenthesized_init = false;
10137 is_non_constant_init = true;
10140 /* The old parser allows attributes to appear after a parenthesized
10141 initializer. Mark Mitchell proposed removing this functionality
10142 on the GCC mailing lists on 2002-08-13. This parser accepts the
10143 attributes -- but ignores them. */
10144 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10145 if (cp_parser_attributes_opt (parser))
10146 warning ("attributes after parenthesized initializer ignored");
10148 /* Leave the SCOPE, now that we have processed the initializer. It
10149 is important to do this before calling cp_finish_decl because it
10150 makes decisions about whether to create DECL_STMTs or not based
10151 on the current scope. */
10152 if (pop_p)
10153 pop_scope (scope);
10155 /* For an in-class declaration, use `grokfield' to create the
10156 declaration. */
10157 if (member_p)
10159 decl = grokfield (declarator, decl_specifiers,
10160 initializer, /*asmspec=*/NULL_TREE,
10161 /*attributes=*/NULL_TREE);
10162 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10163 cp_parser_save_default_args (parser, decl);
10166 /* Finish processing the declaration. But, skip friend
10167 declarations. */
10168 if (!friend_p && decl)
10169 cp_finish_decl (decl,
10170 initializer,
10171 asm_specification,
10172 /* If the initializer is in parentheses, then this is
10173 a direct-initialization, which means that an
10174 `explicit' constructor is OK. Otherwise, an
10175 `explicit' constructor cannot be used. */
10176 ((is_parenthesized_init || !is_initialized)
10177 ? 0 : LOOKUP_ONLYCONVERTING));
10179 /* Remember whether or not variables were initialized by
10180 constant-expressions. */
10181 if (decl && TREE_CODE (decl) == VAR_DECL
10182 && is_initialized && !is_non_constant_init)
10183 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10185 return decl;
10188 /* Parse a declarator.
10190 declarator:
10191 direct-declarator
10192 ptr-operator declarator
10194 abstract-declarator:
10195 ptr-operator abstract-declarator [opt]
10196 direct-abstract-declarator
10198 GNU Extensions:
10200 declarator:
10201 attributes [opt] direct-declarator
10202 attributes [opt] ptr-operator declarator
10204 abstract-declarator:
10205 attributes [opt] ptr-operator abstract-declarator [opt]
10206 attributes [opt] direct-abstract-declarator
10208 Returns a representation of the declarator. If the declarator has
10209 the form `* declarator', then an INDIRECT_REF is returned, whose
10210 only operand is the sub-declarator. Analogously, `& declarator' is
10211 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10212 used. The first operand is the TYPE for `X'. The second operand
10213 is an INDIRECT_REF whose operand is the sub-declarator.
10215 Otherwise, the representation is as for a direct-declarator.
10217 (It would be better to define a structure type to represent
10218 declarators, rather than abusing `tree' nodes to represent
10219 declarators. That would be much clearer and save some memory.
10220 There is no reason for declarators to be garbage-collected, for
10221 example; they are created during parser and no longer needed after
10222 `grokdeclarator' has been called.)
10224 For a ptr-operator that has the optional cv-qualifier-seq,
10225 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10226 node.
10228 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10229 detect constructor, destructor or conversion operators. It is set
10230 to -1 if the declarator is a name, and +1 if it is a
10231 function. Otherwise it is set to zero. Usually you just want to
10232 test for >0, but internally the negative value is used.
10234 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10235 a decl-specifier-seq unless it declares a constructor, destructor,
10236 or conversion. It might seem that we could check this condition in
10237 semantic analysis, rather than parsing, but that makes it difficult
10238 to handle something like `f()'. We want to notice that there are
10239 no decl-specifiers, and therefore realize that this is an
10240 expression, not a declaration.)
10242 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10243 the declarator is a direct-declarator of the form "(...)". */
10245 static tree
10246 cp_parser_declarator (cp_parser* parser,
10247 cp_parser_declarator_kind dcl_kind,
10248 int* ctor_dtor_or_conv_p,
10249 bool* parenthesized_p)
10251 cp_token *token;
10252 tree declarator;
10253 enum tree_code code;
10254 tree cv_qualifier_seq;
10255 tree class_type;
10256 tree attributes = NULL_TREE;
10258 /* Assume this is not a constructor, destructor, or type-conversion
10259 operator. */
10260 if (ctor_dtor_or_conv_p)
10261 *ctor_dtor_or_conv_p = 0;
10263 if (cp_parser_allow_gnu_extensions_p (parser))
10264 attributes = cp_parser_attributes_opt (parser);
10266 /* Peek at the next token. */
10267 token = cp_lexer_peek_token (parser->lexer);
10269 /* Check for the ptr-operator production. */
10270 cp_parser_parse_tentatively (parser);
10271 /* Parse the ptr-operator. */
10272 code = cp_parser_ptr_operator (parser,
10273 &class_type,
10274 &cv_qualifier_seq);
10275 /* If that worked, then we have a ptr-operator. */
10276 if (cp_parser_parse_definitely (parser))
10278 /* If a ptr-operator was found, then this declarator was not
10279 parenthesized. */
10280 if (parenthesized_p)
10281 *parenthesized_p = true;
10282 /* The dependent declarator is optional if we are parsing an
10283 abstract-declarator. */
10284 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10285 cp_parser_parse_tentatively (parser);
10287 /* Parse the dependent declarator. */
10288 declarator = cp_parser_declarator (parser, dcl_kind,
10289 /*ctor_dtor_or_conv_p=*/NULL,
10290 /*parenthesized_p=*/NULL);
10292 /* If we are parsing an abstract-declarator, we must handle the
10293 case where the dependent declarator is absent. */
10294 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10295 && !cp_parser_parse_definitely (parser))
10296 declarator = NULL_TREE;
10298 /* Build the representation of the ptr-operator. */
10299 if (code == INDIRECT_REF)
10300 declarator = make_pointer_declarator (cv_qualifier_seq,
10301 declarator);
10302 else
10303 declarator = make_reference_declarator (cv_qualifier_seq,
10304 declarator);
10305 /* Handle the pointer-to-member case. */
10306 if (class_type)
10307 declarator = build_nt (SCOPE_REF, class_type, declarator);
10309 /* Everything else is a direct-declarator. */
10310 else
10312 if (parenthesized_p)
10313 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10314 CPP_OPEN_PAREN);
10315 declarator = cp_parser_direct_declarator (parser, dcl_kind,
10316 ctor_dtor_or_conv_p);
10319 if (attributes && declarator != error_mark_node)
10320 declarator = tree_cons (attributes, declarator, NULL_TREE);
10322 return declarator;
10325 /* Parse a direct-declarator or direct-abstract-declarator.
10327 direct-declarator:
10328 declarator-id
10329 direct-declarator ( parameter-declaration-clause )
10330 cv-qualifier-seq [opt]
10331 exception-specification [opt]
10332 direct-declarator [ constant-expression [opt] ]
10333 ( declarator )
10335 direct-abstract-declarator:
10336 direct-abstract-declarator [opt]
10337 ( parameter-declaration-clause )
10338 cv-qualifier-seq [opt]
10339 exception-specification [opt]
10340 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10341 ( abstract-declarator )
10343 Returns a representation of the declarator. DCL_KIND is
10344 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10345 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10346 we are parsing a direct-declarator. It is
10347 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10348 of ambiguity we prefer an abstract declarator, as per
10349 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
10350 cp_parser_declarator.
10352 For the declarator-id production, the representation is as for an
10353 id-expression, except that a qualified name is represented as a
10354 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10355 see the documentation of the FUNCTION_DECLARATOR_* macros for
10356 information about how to find the various declarator components.
10357 An array-declarator is represented as an ARRAY_REF. The
10358 direct-declarator is the first operand; the constant-expression
10359 indicating the size of the array is the second operand. */
10361 static tree
10362 cp_parser_direct_declarator (cp_parser* parser,
10363 cp_parser_declarator_kind dcl_kind,
10364 int* ctor_dtor_or_conv_p)
10366 cp_token *token;
10367 tree declarator = NULL_TREE;
10368 tree scope = NULL_TREE;
10369 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10370 bool saved_in_declarator_p = parser->in_declarator_p;
10371 bool first = true;
10372 bool pop_p = false;
10374 while (true)
10376 /* Peek at the next token. */
10377 token = cp_lexer_peek_token (parser->lexer);
10378 if (token->type == CPP_OPEN_PAREN)
10380 /* This is either a parameter-declaration-clause, or a
10381 parenthesized declarator. When we know we are parsing a
10382 named declarator, it must be a parenthesized declarator
10383 if FIRST is true. For instance, `(int)' is a
10384 parameter-declaration-clause, with an omitted
10385 direct-abstract-declarator. But `((*))', is a
10386 parenthesized abstract declarator. Finally, when T is a
10387 template parameter `(T)' is a
10388 parameter-declaration-clause, and not a parenthesized
10389 named declarator.
10391 We first try and parse a parameter-declaration-clause,
10392 and then try a nested declarator (if FIRST is true).
10394 It is not an error for it not to be a
10395 parameter-declaration-clause, even when FIRST is
10396 false. Consider,
10398 int i (int);
10399 int i (3);
10401 The first is the declaration of a function while the
10402 second is a the definition of a variable, including its
10403 initializer.
10405 Having seen only the parenthesis, we cannot know which of
10406 these two alternatives should be selected. Even more
10407 complex are examples like:
10409 int i (int (a));
10410 int i (int (3));
10412 The former is a function-declaration; the latter is a
10413 variable initialization.
10415 Thus again, we try a parameter-declaration-clause, and if
10416 that fails, we back out and return. */
10418 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10420 tree params;
10421 unsigned saved_num_template_parameter_lists;
10423 cp_parser_parse_tentatively (parser);
10425 /* Consume the `('. */
10426 cp_lexer_consume_token (parser->lexer);
10427 if (first)
10429 /* If this is going to be an abstract declarator, we're
10430 in a declarator and we can't have default args. */
10431 parser->default_arg_ok_p = false;
10432 parser->in_declarator_p = true;
10435 /* Inside the function parameter list, surrounding
10436 template-parameter-lists do not apply. */
10437 saved_num_template_parameter_lists
10438 = parser->num_template_parameter_lists;
10439 parser->num_template_parameter_lists = 0;
10441 /* Parse the parameter-declaration-clause. */
10442 params = cp_parser_parameter_declaration_clause (parser);
10444 parser->num_template_parameter_lists
10445 = saved_num_template_parameter_lists;
10447 /* If all went well, parse the cv-qualifier-seq and the
10448 exception-specification. */
10449 if (cp_parser_parse_definitely (parser))
10451 tree cv_qualifiers;
10452 tree exception_specification;
10454 if (ctor_dtor_or_conv_p)
10455 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10456 first = false;
10457 /* Consume the `)'. */
10458 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10460 /* Parse the cv-qualifier-seq. */
10461 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10462 /* And the exception-specification. */
10463 exception_specification
10464 = cp_parser_exception_specification_opt (parser);
10466 /* Create the function-declarator. */
10467 declarator = make_call_declarator (declarator,
10468 params,
10469 cv_qualifiers,
10470 exception_specification);
10471 /* Any subsequent parameter lists are to do with
10472 return type, so are not those of the declared
10473 function. */
10474 parser->default_arg_ok_p = false;
10476 /* Repeat the main loop. */
10477 continue;
10481 /* If this is the first, we can try a parenthesized
10482 declarator. */
10483 if (first)
10485 bool saved_in_type_id_in_expr_p;
10487 parser->default_arg_ok_p = saved_default_arg_ok_p;
10488 parser->in_declarator_p = saved_in_declarator_p;
10490 /* Consume the `('. */
10491 cp_lexer_consume_token (parser->lexer);
10492 /* Parse the nested declarator. */
10493 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10494 parser->in_type_id_in_expr_p = true;
10495 declarator
10496 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10497 /*parenthesized_p=*/NULL);
10498 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10499 first = false;
10500 /* Expect a `)'. */
10501 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10502 declarator = error_mark_node;
10503 if (declarator == error_mark_node)
10504 break;
10506 goto handle_declarator;
10508 /* Otherwise, we must be done. */
10509 else
10510 break;
10512 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10513 && token->type == CPP_OPEN_SQUARE)
10515 /* Parse an array-declarator. */
10516 tree bounds;
10518 if (ctor_dtor_or_conv_p)
10519 *ctor_dtor_or_conv_p = 0;
10521 first = false;
10522 parser->default_arg_ok_p = false;
10523 parser->in_declarator_p = true;
10524 /* Consume the `['. */
10525 cp_lexer_consume_token (parser->lexer);
10526 /* Peek at the next token. */
10527 token = cp_lexer_peek_token (parser->lexer);
10528 /* If the next token is `]', then there is no
10529 constant-expression. */
10530 if (token->type != CPP_CLOSE_SQUARE)
10532 bool non_constant_p;
10534 bounds
10535 = cp_parser_constant_expression (parser,
10536 /*allow_non_constant=*/true,
10537 &non_constant_p);
10538 if (!non_constant_p)
10539 bounds = fold_non_dependent_expr (bounds);
10541 else
10542 bounds = NULL_TREE;
10543 /* Look for the closing `]'. */
10544 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10546 declarator = error_mark_node;
10547 break;
10550 declarator = build_nt (ARRAY_REF, declarator, bounds);
10552 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10554 /* Parse a declarator-id */
10555 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10556 cp_parser_parse_tentatively (parser);
10557 declarator = cp_parser_declarator_id (parser);
10558 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10560 if (!cp_parser_parse_definitely (parser))
10561 declarator = error_mark_node;
10562 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10564 cp_parser_error (parser, "expected unqualified-id");
10565 declarator = error_mark_node;
10569 if (declarator == error_mark_node)
10570 break;
10572 if (TREE_CODE (declarator) == SCOPE_REF
10573 && !current_scope ())
10575 tree scope = TREE_OPERAND (declarator, 0);
10577 /* In the declaration of a member of a template class
10578 outside of the class itself, the SCOPE will sometimes
10579 be a TYPENAME_TYPE. For example, given:
10581 template <typename T>
10582 int S<T>::R::i = 3;
10584 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10585 this context, we must resolve S<T>::R to an ordinary
10586 type, rather than a typename type.
10588 The reason we normally avoid resolving TYPENAME_TYPEs
10589 is that a specialization of `S' might render
10590 `S<T>::R' not a type. However, if `S' is
10591 specialized, then this `i' will not be used, so there
10592 is no harm in resolving the types here. */
10593 if (TREE_CODE (scope) == TYPENAME_TYPE)
10595 tree type;
10597 /* Resolve the TYPENAME_TYPE. */
10598 type = resolve_typename_type (scope,
10599 /*only_current_p=*/false);
10600 /* If that failed, the declarator is invalid. */
10601 if (type != error_mark_node)
10602 scope = type;
10603 /* Build a new DECLARATOR. */
10604 declarator = build_nt (SCOPE_REF,
10605 scope,
10606 TREE_OPERAND (declarator, 1));
10610 /* Check to see whether the declarator-id names a constructor,
10611 destructor, or conversion. */
10612 if (declarator && ctor_dtor_or_conv_p
10613 && ((TREE_CODE (declarator) == SCOPE_REF
10614 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10615 || (TREE_CODE (declarator) != SCOPE_REF
10616 && at_class_scope_p ())))
10618 tree unqualified_name;
10619 tree class_type;
10621 /* Get the unqualified part of the name. */
10622 if (TREE_CODE (declarator) == SCOPE_REF)
10624 class_type = TREE_OPERAND (declarator, 0);
10625 unqualified_name = TREE_OPERAND (declarator, 1);
10627 else
10629 class_type = current_class_type;
10630 unqualified_name = declarator;
10633 /* See if it names ctor, dtor or conv. */
10634 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10635 || IDENTIFIER_TYPENAME_P (unqualified_name)
10636 || constructor_name_p (unqualified_name, class_type)
10637 || (TREE_CODE (unqualified_name) == TYPE_DECL
10638 && same_type_p (TREE_TYPE (unqualified_name),
10639 class_type)))
10640 *ctor_dtor_or_conv_p = -1;
10643 handle_declarator:;
10644 scope = get_scope_of_declarator (declarator);
10645 if (scope)
10646 /* Any names that appear after the declarator-id for a
10647 member are looked up in the containing scope. */
10648 pop_p = push_scope (scope);
10649 parser->in_declarator_p = true;
10650 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10651 || (declarator
10652 && (TREE_CODE (declarator) == SCOPE_REF
10653 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10654 /* Default args are only allowed on function
10655 declarations. */
10656 parser->default_arg_ok_p = saved_default_arg_ok_p;
10657 else
10658 parser->default_arg_ok_p = false;
10660 first = false;
10662 /* We're done. */
10663 else
10664 break;
10667 /* For an abstract declarator, we might wind up with nothing at this
10668 point. That's an error; the declarator is not optional. */
10669 if (!declarator)
10670 cp_parser_error (parser, "expected declarator");
10672 /* If we entered a scope, we must exit it now. */
10673 if (pop_p)
10674 pop_scope (scope);
10676 parser->default_arg_ok_p = saved_default_arg_ok_p;
10677 parser->in_declarator_p = saved_in_declarator_p;
10679 return declarator;
10682 /* Parse a ptr-operator.
10684 ptr-operator:
10685 * cv-qualifier-seq [opt]
10687 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10689 GNU Extension:
10691 ptr-operator:
10692 & cv-qualifier-seq [opt]
10694 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10695 used. Returns ADDR_EXPR if a reference was used. In the
10696 case of a pointer-to-member, *TYPE is filled in with the
10697 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10698 with the cv-qualifier-seq, or NULL_TREE, if there are no
10699 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10701 static enum tree_code
10702 cp_parser_ptr_operator (cp_parser* parser,
10703 tree* type,
10704 tree* cv_qualifier_seq)
10706 enum tree_code code = ERROR_MARK;
10707 cp_token *token;
10709 /* Assume that it's not a pointer-to-member. */
10710 *type = NULL_TREE;
10711 /* And that there are no cv-qualifiers. */
10712 *cv_qualifier_seq = NULL_TREE;
10714 /* Peek at the next token. */
10715 token = cp_lexer_peek_token (parser->lexer);
10716 /* If it's a `*' or `&' we have a pointer or reference. */
10717 if (token->type == CPP_MULT || token->type == CPP_AND)
10719 /* Remember which ptr-operator we were processing. */
10720 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10722 /* Consume the `*' or `&'. */
10723 cp_lexer_consume_token (parser->lexer);
10725 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10726 `&', if we are allowing GNU extensions. (The only qualifier
10727 that can legally appear after `&' is `restrict', but that is
10728 enforced during semantic analysis. */
10729 if (code == INDIRECT_REF
10730 || cp_parser_allow_gnu_extensions_p (parser))
10731 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10733 else
10735 /* Try the pointer-to-member case. */
10736 cp_parser_parse_tentatively (parser);
10737 /* Look for the optional `::' operator. */
10738 cp_parser_global_scope_opt (parser,
10739 /*current_scope_valid_p=*/false);
10740 /* Look for the nested-name specifier. */
10741 cp_parser_nested_name_specifier (parser,
10742 /*typename_keyword_p=*/false,
10743 /*check_dependency_p=*/true,
10744 /*type_p=*/false,
10745 /*is_declaration=*/false);
10746 /* If we found it, and the next token is a `*', then we are
10747 indeed looking at a pointer-to-member operator. */
10748 if (!cp_parser_error_occurred (parser)
10749 && cp_parser_require (parser, CPP_MULT, "`*'"))
10751 /* The type of which the member is a member is given by the
10752 current SCOPE. */
10753 *type = parser->scope;
10754 /* The next name will not be qualified. */
10755 parser->scope = NULL_TREE;
10756 parser->qualifying_scope = NULL_TREE;
10757 parser->object_scope = NULL_TREE;
10758 /* Indicate that the `*' operator was used. */
10759 code = INDIRECT_REF;
10760 /* Look for the optional cv-qualifier-seq. */
10761 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10763 /* If that didn't work we don't have a ptr-operator. */
10764 if (!cp_parser_parse_definitely (parser))
10765 cp_parser_error (parser, "expected ptr-operator");
10768 return code;
10771 /* Parse an (optional) cv-qualifier-seq.
10773 cv-qualifier-seq:
10774 cv-qualifier cv-qualifier-seq [opt]
10776 Returns a TREE_LIST. The TREE_VALUE of each node is the
10777 representation of a cv-qualifier. */
10779 static tree
10780 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10782 tree cv_qualifiers = NULL_TREE;
10784 while (true)
10786 tree cv_qualifier;
10788 /* Look for the next cv-qualifier. */
10789 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10790 /* If we didn't find one, we're done. */
10791 if (!cv_qualifier)
10792 break;
10794 /* Add this cv-qualifier to the list. */
10795 cv_qualifiers
10796 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10799 /* We built up the list in reverse order. */
10800 return nreverse (cv_qualifiers);
10803 /* Parse an (optional) cv-qualifier.
10805 cv-qualifier:
10806 const
10807 volatile
10809 GNU Extension:
10811 cv-qualifier:
10812 __restrict__ */
10814 static tree
10815 cp_parser_cv_qualifier_opt (cp_parser* parser)
10817 cp_token *token;
10818 tree cv_qualifier = NULL_TREE;
10820 /* Peek at the next token. */
10821 token = cp_lexer_peek_token (parser->lexer);
10822 /* See if it's a cv-qualifier. */
10823 switch (token->keyword)
10825 case RID_CONST:
10826 case RID_VOLATILE:
10827 case RID_RESTRICT:
10828 /* Save the value of the token. */
10829 cv_qualifier = token->value;
10830 /* Consume the token. */
10831 cp_lexer_consume_token (parser->lexer);
10832 break;
10834 default:
10835 break;
10838 return cv_qualifier;
10841 /* Parse a declarator-id.
10843 declarator-id:
10844 id-expression
10845 :: [opt] nested-name-specifier [opt] type-name
10847 In the `id-expression' case, the value returned is as for
10848 cp_parser_id_expression if the id-expression was an unqualified-id.
10849 If the id-expression was a qualified-id, then a SCOPE_REF is
10850 returned. The first operand is the scope (either a NAMESPACE_DECL
10851 or TREE_TYPE), but the second is still just a representation of an
10852 unqualified-id. */
10854 static tree
10855 cp_parser_declarator_id (cp_parser* parser)
10857 tree id_expression;
10859 /* The expression must be an id-expression. Assume that qualified
10860 names are the names of types so that:
10862 template <class T>
10863 int S<T>::R::i = 3;
10865 will work; we must treat `S<T>::R' as the name of a type.
10866 Similarly, assume that qualified names are templates, where
10867 required, so that:
10869 template <class T>
10870 int S<T>::R<T>::i = 3;
10872 will work, too. */
10873 id_expression = cp_parser_id_expression (parser,
10874 /*template_keyword_p=*/false,
10875 /*check_dependency_p=*/false,
10876 /*template_p=*/NULL,
10877 /*declarator_p=*/true);
10878 /* If the name was qualified, create a SCOPE_REF to represent
10879 that. */
10880 if (parser->scope)
10882 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10883 parser->scope = NULL_TREE;
10886 return id_expression;
10889 /* Parse a type-id.
10891 type-id:
10892 type-specifier-seq abstract-declarator [opt]
10894 Returns the TYPE specified. */
10896 static tree
10897 cp_parser_type_id (cp_parser* parser)
10899 tree type_specifier_seq;
10900 tree abstract_declarator;
10902 /* Parse the type-specifier-seq. */
10903 type_specifier_seq
10904 = cp_parser_type_specifier_seq (parser);
10905 if (type_specifier_seq == error_mark_node)
10906 return error_mark_node;
10908 /* There might or might not be an abstract declarator. */
10909 cp_parser_parse_tentatively (parser);
10910 /* Look for the declarator. */
10911 abstract_declarator
10912 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10913 /*parenthesized_p=*/NULL);
10914 /* Check to see if there really was a declarator. */
10915 if (!cp_parser_parse_definitely (parser))
10916 abstract_declarator = NULL_TREE;
10918 return groktypename (build_tree_list (type_specifier_seq,
10919 abstract_declarator));
10922 /* Parse a type-specifier-seq.
10924 type-specifier-seq:
10925 type-specifier type-specifier-seq [opt]
10927 GNU extension:
10929 type-specifier-seq:
10930 attributes type-specifier-seq [opt]
10932 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10933 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10935 static tree
10936 cp_parser_type_specifier_seq (cp_parser* parser)
10938 bool seen_type_specifier = false;
10939 tree type_specifier_seq = NULL_TREE;
10941 /* Parse the type-specifiers and attributes. */
10942 while (true)
10944 tree type_specifier;
10946 /* Check for attributes first. */
10947 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10949 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10950 NULL_TREE,
10951 type_specifier_seq);
10952 continue;
10955 /* After the first type-specifier, others are optional. */
10956 if (seen_type_specifier)
10957 cp_parser_parse_tentatively (parser);
10958 /* Look for the type-specifier. */
10959 type_specifier = cp_parser_type_specifier (parser,
10960 CP_PARSER_FLAGS_NONE,
10961 /*is_friend=*/false,
10962 /*is_declaration=*/false,
10963 NULL,
10964 NULL);
10965 /* If the first type-specifier could not be found, this is not a
10966 type-specifier-seq at all. */
10967 if (!seen_type_specifier && type_specifier == error_mark_node)
10968 return error_mark_node;
10969 /* If subsequent type-specifiers could not be found, the
10970 type-specifier-seq is complete. */
10971 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10972 break;
10974 /* Add the new type-specifier to the list. */
10975 type_specifier_seq
10976 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10977 seen_type_specifier = true;
10980 /* We built up the list in reverse order. */
10981 return nreverse (type_specifier_seq);
10984 /* Parse a parameter-declaration-clause.
10986 parameter-declaration-clause:
10987 parameter-declaration-list [opt] ... [opt]
10988 parameter-declaration-list , ...
10990 Returns a representation for the parameter declarations. Each node
10991 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10992 representation.) If the parameter-declaration-clause ends with an
10993 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10994 list. A return value of NULL_TREE indicates a
10995 parameter-declaration-clause consisting only of an ellipsis. */
10997 static tree
10998 cp_parser_parameter_declaration_clause (cp_parser* parser)
11000 tree parameters;
11001 cp_token *token;
11002 bool ellipsis_p;
11004 /* Peek at the next token. */
11005 token = cp_lexer_peek_token (parser->lexer);
11006 /* Check for trivial parameter-declaration-clauses. */
11007 if (token->type == CPP_ELLIPSIS)
11009 /* Consume the `...' token. */
11010 cp_lexer_consume_token (parser->lexer);
11011 return NULL_TREE;
11013 else if (token->type == CPP_CLOSE_PAREN)
11014 /* There are no parameters. */
11016 #ifndef NO_IMPLICIT_EXTERN_C
11017 if (in_system_header && current_class_type == NULL
11018 && current_lang_name == lang_name_c)
11019 return NULL_TREE;
11020 else
11021 #endif
11022 return void_list_node;
11024 /* Check for `(void)', too, which is a special case. */
11025 else if (token->keyword == RID_VOID
11026 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11027 == CPP_CLOSE_PAREN))
11029 /* Consume the `void' token. */
11030 cp_lexer_consume_token (parser->lexer);
11031 /* There are no parameters. */
11032 return void_list_node;
11035 /* Parse the parameter-declaration-list. */
11036 parameters = cp_parser_parameter_declaration_list (parser);
11037 /* If a parse error occurred while parsing the
11038 parameter-declaration-list, then the entire
11039 parameter-declaration-clause is erroneous. */
11040 if (parameters == error_mark_node)
11041 return error_mark_node;
11043 /* Peek at the next token. */
11044 token = cp_lexer_peek_token (parser->lexer);
11045 /* If it's a `,', the clause should terminate with an ellipsis. */
11046 if (token->type == CPP_COMMA)
11048 /* Consume the `,'. */
11049 cp_lexer_consume_token (parser->lexer);
11050 /* Expect an ellipsis. */
11051 ellipsis_p
11052 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11054 /* It might also be `...' if the optional trailing `,' was
11055 omitted. */
11056 else if (token->type == CPP_ELLIPSIS)
11058 /* Consume the `...' token. */
11059 cp_lexer_consume_token (parser->lexer);
11060 /* And remember that we saw it. */
11061 ellipsis_p = true;
11063 else
11064 ellipsis_p = false;
11066 /* Finish the parameter list. */
11067 return finish_parmlist (parameters, ellipsis_p);
11070 /* Parse a parameter-declaration-list.
11072 parameter-declaration-list:
11073 parameter-declaration
11074 parameter-declaration-list , parameter-declaration
11076 Returns a representation of the parameter-declaration-list, as for
11077 cp_parser_parameter_declaration_clause. However, the
11078 `void_list_node' is never appended to the list. */
11080 static tree
11081 cp_parser_parameter_declaration_list (cp_parser* parser)
11083 tree parameters = NULL_TREE;
11085 /* Look for more parameters. */
11086 while (true)
11088 tree parameter;
11089 bool parenthesized_p;
11090 /* Parse the parameter. */
11091 parameter
11092 = cp_parser_parameter_declaration (parser,
11093 /*template_parm_p=*/false,
11094 &parenthesized_p);
11096 /* If a parse error occurred parsing the parameter declaration,
11097 then the entire parameter-declaration-list is erroneous. */
11098 if (parameter == error_mark_node)
11100 parameters = error_mark_node;
11101 break;
11103 /* Add the new parameter to the list. */
11104 TREE_CHAIN (parameter) = parameters;
11105 parameters = parameter;
11107 /* Peek at the next token. */
11108 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11109 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11110 /* The parameter-declaration-list is complete. */
11111 break;
11112 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11114 cp_token *token;
11116 /* Peek at the next token. */
11117 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11118 /* If it's an ellipsis, then the list is complete. */
11119 if (token->type == CPP_ELLIPSIS)
11120 break;
11121 /* Otherwise, there must be more parameters. Consume the
11122 `,'. */
11123 cp_lexer_consume_token (parser->lexer);
11124 /* When parsing something like:
11126 int i(float f, double d)
11128 we can tell after seeing the declaration for "f" that we
11129 are not looking at an initialization of a variable "i",
11130 but rather at the declaration of a function "i".
11132 Due to the fact that the parsing of template arguments
11133 (as specified to a template-id) requires backtracking we
11134 cannot use this technique when inside a template argument
11135 list. */
11136 if (!parser->in_template_argument_list_p
11137 && !parser->in_type_id_in_expr_p
11138 && cp_parser_parsing_tentatively (parser)
11139 && !cp_parser_committed_to_tentative_parse (parser)
11140 /* However, a parameter-declaration of the form
11141 "foat(f)" (which is a valid declaration of a
11142 parameter "f") can also be interpreted as an
11143 expression (the conversion of "f" to "float"). */
11144 && !parenthesized_p)
11145 cp_parser_commit_to_tentative_parse (parser);
11147 else
11149 cp_parser_error (parser, "expected `,' or `...'");
11150 if (!cp_parser_parsing_tentatively (parser)
11151 || cp_parser_committed_to_tentative_parse (parser))
11152 cp_parser_skip_to_closing_parenthesis (parser,
11153 /*recovering=*/true,
11154 /*or_comma=*/false,
11155 /*consume_paren=*/false);
11156 break;
11160 /* We built up the list in reverse order; straighten it out now. */
11161 return nreverse (parameters);
11164 /* Parse a parameter declaration.
11166 parameter-declaration:
11167 decl-specifier-seq declarator
11168 decl-specifier-seq declarator = assignment-expression
11169 decl-specifier-seq abstract-declarator [opt]
11170 decl-specifier-seq abstract-declarator [opt] = assignment-expression
11172 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11173 declares a template parameter. (In that case, a non-nested `>'
11174 token encountered during the parsing of the assignment-expression
11175 is not interpreted as a greater-than operator.)
11177 Returns a TREE_LIST representing the parameter-declaration. The
11178 TREE_PURPOSE is the default argument expression, or NULL_TREE if
11179 there is no default argument. The TREE_VALUE is a representation
11180 of the decl-specifier-seq and declarator. In particular, the
11181 TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11182 decl-specifier-seq and whose TREE_VALUE represents the declarator.
11183 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11184 the declarator is of the form "(p)". */
11186 static tree
11187 cp_parser_parameter_declaration (cp_parser *parser,
11188 bool template_parm_p,
11189 bool *parenthesized_p)
11191 int declares_class_or_enum;
11192 bool greater_than_is_operator_p;
11193 tree decl_specifiers;
11194 tree attributes;
11195 tree declarator;
11196 tree default_argument;
11197 tree parameter;
11198 cp_token *token;
11199 const char *saved_message;
11201 /* In a template parameter, `>' is not an operator.
11203 [temp.param]
11205 When parsing a default template-argument for a non-type
11206 template-parameter, the first non-nested `>' is taken as the end
11207 of the template parameter-list rather than a greater-than
11208 operator. */
11209 greater_than_is_operator_p = !template_parm_p;
11211 /* Type definitions may not appear in parameter types. */
11212 saved_message = parser->type_definition_forbidden_message;
11213 parser->type_definition_forbidden_message
11214 = "types may not be defined in parameter types";
11216 /* Parse the declaration-specifiers. */
11217 decl_specifiers
11218 = cp_parser_decl_specifier_seq (parser,
11219 CP_PARSER_FLAGS_NONE,
11220 &attributes,
11221 &declares_class_or_enum);
11222 /* If an error occurred, there's no reason to attempt to parse the
11223 rest of the declaration. */
11224 if (cp_parser_error_occurred (parser))
11226 parser->type_definition_forbidden_message = saved_message;
11227 return error_mark_node;
11230 /* Peek at the next token. */
11231 token = cp_lexer_peek_token (parser->lexer);
11232 /* If the next token is a `)', `,', `=', `>', or `...', then there
11233 is no declarator. */
11234 if (token->type == CPP_CLOSE_PAREN
11235 || token->type == CPP_COMMA
11236 || token->type == CPP_EQ
11237 || token->type == CPP_ELLIPSIS
11238 || token->type == CPP_GREATER)
11240 declarator = NULL_TREE;
11241 if (parenthesized_p)
11242 *parenthesized_p = false;
11244 /* Otherwise, there should be a declarator. */
11245 else
11247 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11248 parser->default_arg_ok_p = false;
11250 /* After seeing a decl-specifier-seq, if the next token is not a
11251 "(", there is no possibility that the code is a valid
11252 expression. Therefore, if parsing tentatively, we commit at
11253 this point. */
11254 if (!parser->in_template_argument_list_p
11255 /* In an expression context, having seen:
11257 (int((char ...
11259 we cannot be sure whether we are looking at a
11260 function-type (taking a "char" as a parameter) or a cast
11261 of some object of type "char" to "int". */
11262 && !parser->in_type_id_in_expr_p
11263 && cp_parser_parsing_tentatively (parser)
11264 && !cp_parser_committed_to_tentative_parse (parser)
11265 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11266 cp_parser_commit_to_tentative_parse (parser);
11267 /* Parse the declarator. */
11268 declarator = cp_parser_declarator (parser,
11269 CP_PARSER_DECLARATOR_EITHER,
11270 /*ctor_dtor_or_conv_p=*/NULL,
11271 parenthesized_p);
11272 parser->default_arg_ok_p = saved_default_arg_ok_p;
11273 /* After the declarator, allow more attributes. */
11274 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11277 /* The restriction on defining new types applies only to the type
11278 of the parameter, not to the default argument. */
11279 parser->type_definition_forbidden_message = saved_message;
11281 /* If the next token is `=', then process a default argument. */
11282 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11284 bool saved_greater_than_is_operator_p;
11285 /* Consume the `='. */
11286 cp_lexer_consume_token (parser->lexer);
11288 /* If we are defining a class, then the tokens that make up the
11289 default argument must be saved and processed later. */
11290 if (!template_parm_p && at_class_scope_p ()
11291 && TYPE_BEING_DEFINED (current_class_type))
11293 unsigned depth = 0;
11295 /* Create a DEFAULT_ARG to represented the unparsed default
11296 argument. */
11297 default_argument = make_node (DEFAULT_ARG);
11298 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11300 /* Add tokens until we have processed the entire default
11301 argument. */
11302 while (true)
11304 bool done = false;
11305 cp_token *token;
11307 /* Peek at the next token. */
11308 token = cp_lexer_peek_token (parser->lexer);
11309 /* What we do depends on what token we have. */
11310 switch (token->type)
11312 /* In valid code, a default argument must be
11313 immediately followed by a `,' `)', or `...'. */
11314 case CPP_COMMA:
11315 case CPP_CLOSE_PAREN:
11316 case CPP_ELLIPSIS:
11317 /* If we run into a non-nested `;', `}', or `]',
11318 then the code is invalid -- but the default
11319 argument is certainly over. */
11320 case CPP_SEMICOLON:
11321 case CPP_CLOSE_BRACE:
11322 case CPP_CLOSE_SQUARE:
11323 if (depth == 0)
11324 done = true;
11325 /* Update DEPTH, if necessary. */
11326 else if (token->type == CPP_CLOSE_PAREN
11327 || token->type == CPP_CLOSE_BRACE
11328 || token->type == CPP_CLOSE_SQUARE)
11329 --depth;
11330 break;
11332 case CPP_OPEN_PAREN:
11333 case CPP_OPEN_SQUARE:
11334 case CPP_OPEN_BRACE:
11335 ++depth;
11336 break;
11338 case CPP_GREATER:
11339 /* If we see a non-nested `>', and `>' is not an
11340 operator, then it marks the end of the default
11341 argument. */
11342 if (!depth && !greater_than_is_operator_p)
11343 done = true;
11344 break;
11346 /* If we run out of tokens, issue an error message. */
11347 case CPP_EOF:
11348 error ("file ends in default argument");
11349 done = true;
11350 break;
11352 case CPP_NAME:
11353 case CPP_SCOPE:
11354 /* In these cases, we should look for template-ids.
11355 For example, if the default argument is
11356 `X<int, double>()', we need to do name lookup to
11357 figure out whether or not `X' is a template; if
11358 so, the `,' does not end the default argument.
11360 That is not yet done. */
11361 break;
11363 default:
11364 break;
11367 /* If we've reached the end, stop. */
11368 if (done)
11369 break;
11371 /* Add the token to the token block. */
11372 token = cp_lexer_consume_token (parser->lexer);
11373 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11374 token);
11377 /* Outside of a class definition, we can just parse the
11378 assignment-expression. */
11379 else
11381 bool saved_local_variables_forbidden_p;
11383 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11384 set correctly. */
11385 saved_greater_than_is_operator_p
11386 = parser->greater_than_is_operator_p;
11387 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11388 /* Local variable names (and the `this' keyword) may not
11389 appear in a default argument. */
11390 saved_local_variables_forbidden_p
11391 = parser->local_variables_forbidden_p;
11392 parser->local_variables_forbidden_p = true;
11393 /* Parse the assignment-expression. */
11394 default_argument = cp_parser_assignment_expression (parser);
11395 /* Restore saved state. */
11396 parser->greater_than_is_operator_p
11397 = saved_greater_than_is_operator_p;
11398 parser->local_variables_forbidden_p
11399 = saved_local_variables_forbidden_p;
11401 if (!parser->default_arg_ok_p)
11403 if (!flag_pedantic_errors)
11404 warning ("deprecated use of default argument for parameter of non-function");
11405 else
11407 error ("default arguments are only permitted for function parameters");
11408 default_argument = NULL_TREE;
11412 else
11413 default_argument = NULL_TREE;
11415 /* Create the representation of the parameter. */
11416 if (attributes)
11417 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11418 parameter = build_tree_list (default_argument,
11419 build_tree_list (decl_specifiers,
11420 declarator));
11422 return parameter;
11425 /* Parse a function-body.
11427 function-body:
11428 compound_statement */
11430 static void
11431 cp_parser_function_body (cp_parser *parser)
11433 cp_parser_compound_statement (parser, false);
11436 /* Parse a ctor-initializer-opt followed by a function-body. Return
11437 true if a ctor-initializer was present. */
11439 static bool
11440 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11442 tree body;
11443 bool ctor_initializer_p;
11445 /* Begin the function body. */
11446 body = begin_function_body ();
11447 /* Parse the optional ctor-initializer. */
11448 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11449 /* Parse the function-body. */
11450 cp_parser_function_body (parser);
11451 /* Finish the function body. */
11452 finish_function_body (body);
11454 return ctor_initializer_p;
11457 /* Parse an initializer.
11459 initializer:
11460 = initializer-clause
11461 ( expression-list )
11463 Returns a expression representing the initializer. If no
11464 initializer is present, NULL_TREE is returned.
11466 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11467 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11468 set to FALSE if there is no initializer present. If there is an
11469 initializer, and it is not a constant-expression, *NON_CONSTANT_P
11470 is set to true; otherwise it is set to false. */
11472 static tree
11473 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11474 bool* non_constant_p)
11476 cp_token *token;
11477 tree init;
11479 /* Peek at the next token. */
11480 token = cp_lexer_peek_token (parser->lexer);
11482 /* Let our caller know whether or not this initializer was
11483 parenthesized. */
11484 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11485 /* Assume that the initializer is constant. */
11486 *non_constant_p = false;
11488 if (token->type == CPP_EQ)
11490 /* Consume the `='. */
11491 cp_lexer_consume_token (parser->lexer);
11492 /* Parse the initializer-clause. */
11493 init = cp_parser_initializer_clause (parser, non_constant_p);
11495 else if (token->type == CPP_OPEN_PAREN)
11496 init = cp_parser_parenthesized_expression_list (parser, false,
11497 non_constant_p);
11498 else
11500 /* Anything else is an error. */
11501 cp_parser_error (parser, "expected initializer");
11502 init = error_mark_node;
11505 return init;
11508 /* Parse an initializer-clause.
11510 initializer-clause:
11511 assignment-expression
11512 { initializer-list , [opt] }
11515 Returns an expression representing the initializer.
11517 If the `assignment-expression' production is used the value
11518 returned is simply a representation for the expression.
11520 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11521 the elements of the initializer-list (or NULL_TREE, if the last
11522 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11523 NULL_TREE. There is no way to detect whether or not the optional
11524 trailing `,' was provided. NON_CONSTANT_P is as for
11525 cp_parser_initializer. */
11527 static tree
11528 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11530 tree initializer;
11532 /* If it is not a `{', then we are looking at an
11533 assignment-expression. */
11534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11536 initializer
11537 = cp_parser_constant_expression (parser,
11538 /*allow_non_constant_p=*/true,
11539 non_constant_p);
11540 if (!*non_constant_p)
11541 initializer = fold_non_dependent_expr (initializer);
11543 else
11545 /* Consume the `{' token. */
11546 cp_lexer_consume_token (parser->lexer);
11547 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11548 initializer = make_node (CONSTRUCTOR);
11549 /* If it's not a `}', then there is a non-trivial initializer. */
11550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11552 /* Parse the initializer list. */
11553 CONSTRUCTOR_ELTS (initializer)
11554 = cp_parser_initializer_list (parser, non_constant_p);
11555 /* A trailing `,' token is allowed. */
11556 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11557 cp_lexer_consume_token (parser->lexer);
11559 /* Now, there should be a trailing `}'. */
11560 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11563 return initializer;
11566 /* Parse an initializer-list.
11568 initializer-list:
11569 initializer-clause
11570 initializer-list , initializer-clause
11572 GNU Extension:
11574 initializer-list:
11575 identifier : initializer-clause
11576 initializer-list, identifier : initializer-clause
11578 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11579 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11580 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
11581 as for cp_parser_initializer. */
11583 static tree
11584 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11586 tree initializers = NULL_TREE;
11588 /* Assume all of the expressions are constant. */
11589 *non_constant_p = false;
11591 /* Parse the rest of the list. */
11592 while (true)
11594 cp_token *token;
11595 tree identifier;
11596 tree initializer;
11597 bool clause_non_constant_p;
11599 /* If the next token is an identifier and the following one is a
11600 colon, we are looking at the GNU designated-initializer
11601 syntax. */
11602 if (cp_parser_allow_gnu_extensions_p (parser)
11603 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11604 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11606 /* Consume the identifier. */
11607 identifier = cp_lexer_consume_token (parser->lexer)->value;
11608 /* Consume the `:'. */
11609 cp_lexer_consume_token (parser->lexer);
11611 else
11612 identifier = NULL_TREE;
11614 /* Parse the initializer. */
11615 initializer = cp_parser_initializer_clause (parser,
11616 &clause_non_constant_p);
11617 /* If any clause is non-constant, so is the entire initializer. */
11618 if (clause_non_constant_p)
11619 *non_constant_p = true;
11620 /* Add it to the list. */
11621 initializers = tree_cons (identifier, initializer, initializers);
11623 /* If the next token is not a comma, we have reached the end of
11624 the list. */
11625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11626 break;
11628 /* Peek at the next token. */
11629 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11630 /* If the next token is a `}', then we're still done. An
11631 initializer-clause can have a trailing `,' after the
11632 initializer-list and before the closing `}'. */
11633 if (token->type == CPP_CLOSE_BRACE)
11634 break;
11636 /* Consume the `,' token. */
11637 cp_lexer_consume_token (parser->lexer);
11640 /* The initializers were built up in reverse order, so we need to
11641 reverse them now. */
11642 return nreverse (initializers);
11645 /* Classes [gram.class] */
11647 /* Parse a class-name.
11649 class-name:
11650 identifier
11651 template-id
11653 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11654 to indicate that names looked up in dependent types should be
11655 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11656 keyword has been used to indicate that the name that appears next
11657 is a template. TYPE_P is true iff the next name should be treated
11658 as class-name, even if it is declared to be some other kind of name
11659 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11660 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11661 being defined in a class-head.
11663 Returns the TYPE_DECL representing the class. */
11665 static tree
11666 cp_parser_class_name (cp_parser *parser,
11667 bool typename_keyword_p,
11668 bool template_keyword_p,
11669 bool type_p,
11670 bool check_dependency_p,
11671 bool class_head_p,
11672 bool is_declaration)
11674 tree decl;
11675 tree scope;
11676 bool typename_p;
11677 cp_token *token;
11679 /* All class-names start with an identifier. */
11680 token = cp_lexer_peek_token (parser->lexer);
11681 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11683 cp_parser_error (parser, "expected class-name");
11684 return error_mark_node;
11687 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11688 to a template-id, so we save it here. */
11689 scope = parser->scope;
11690 if (scope == error_mark_node)
11691 return error_mark_node;
11693 /* Any name names a type if we're following the `typename' keyword
11694 in a qualified name where the enclosing scope is type-dependent. */
11695 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11696 && dependent_type_p (scope));
11697 /* Handle the common case (an identifier, but not a template-id)
11698 efficiently. */
11699 if (token->type == CPP_NAME
11700 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11702 tree identifier;
11704 /* Look for the identifier. */
11705 identifier = cp_parser_identifier (parser);
11706 /* If the next token isn't an identifier, we are certainly not
11707 looking at a class-name. */
11708 if (identifier == error_mark_node)
11709 decl = error_mark_node;
11710 /* If we know this is a type-name, there's no need to look it
11711 up. */
11712 else if (typename_p)
11713 decl = identifier;
11714 else
11716 /* If the next token is a `::', then the name must be a type
11717 name.
11719 [basic.lookup.qual]
11721 During the lookup for a name preceding the :: scope
11722 resolution operator, object, function, and enumerator
11723 names are ignored. */
11724 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11725 type_p = true;
11726 /* Look up the name. */
11727 decl = cp_parser_lookup_name (parser, identifier,
11728 type_p,
11729 /*is_template=*/false,
11730 /*is_namespace=*/false,
11731 check_dependency_p);
11734 else
11736 /* Try a template-id. */
11737 decl = cp_parser_template_id (parser, template_keyword_p,
11738 check_dependency_p,
11739 is_declaration);
11740 if (decl == error_mark_node)
11741 return error_mark_node;
11744 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11746 /* If this is a typename, create a TYPENAME_TYPE. */
11747 if (typename_p && decl != error_mark_node)
11749 decl = make_typename_type (scope, decl, /*complain=*/1);
11750 if (decl != error_mark_node)
11751 decl = TYPE_NAME (decl);
11754 /* Check to see that it is really the name of a class. */
11755 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11756 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11757 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11758 /* Situations like this:
11760 template <typename T> struct A {
11761 typename T::template X<int>::I i;
11764 are problematic. Is `T::template X<int>' a class-name? The
11765 standard does not seem to be definitive, but there is no other
11766 valid interpretation of the following `::'. Therefore, those
11767 names are considered class-names. */
11768 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11769 else if (decl == error_mark_node
11770 || TREE_CODE (decl) != TYPE_DECL
11771 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11773 cp_parser_error (parser, "expected class-name");
11774 return error_mark_node;
11777 return decl;
11780 /* Parse a class-specifier.
11782 class-specifier:
11783 class-head { member-specification [opt] }
11785 Returns the TREE_TYPE representing the class. */
11787 static tree
11788 cp_parser_class_specifier (cp_parser* parser)
11790 cp_token *token;
11791 tree type;
11792 tree attributes = NULL_TREE;
11793 int has_trailing_semicolon;
11794 bool nested_name_specifier_p;
11795 unsigned saved_num_template_parameter_lists;
11796 bool pop_p = false;
11798 push_deferring_access_checks (dk_no_deferred);
11800 /* Parse the class-head. */
11801 type = cp_parser_class_head (parser,
11802 &nested_name_specifier_p,
11803 &attributes);
11804 /* If the class-head was a semantic disaster, skip the entire body
11805 of the class. */
11806 if (!type)
11808 cp_parser_skip_to_end_of_block_or_statement (parser);
11809 pop_deferring_access_checks ();
11810 return error_mark_node;
11813 /* Look for the `{'. */
11814 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11816 pop_deferring_access_checks ();
11817 return error_mark_node;
11820 /* Issue an error message if type-definitions are forbidden here. */
11821 cp_parser_check_type_definition (parser);
11822 /* Remember that we are defining one more class. */
11823 ++parser->num_classes_being_defined;
11824 /* Inside the class, surrounding template-parameter-lists do not
11825 apply. */
11826 saved_num_template_parameter_lists
11827 = parser->num_template_parameter_lists;
11828 parser->num_template_parameter_lists = 0;
11830 /* Start the class. */
11831 if (nested_name_specifier_p)
11832 pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11833 type = begin_class_definition (type);
11834 if (type == error_mark_node)
11835 /* If the type is erroneous, skip the entire body of the class. */
11836 cp_parser_skip_to_closing_brace (parser);
11837 else
11838 /* Parse the member-specification. */
11839 cp_parser_member_specification_opt (parser);
11840 /* Look for the trailing `}'. */
11841 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11842 /* We get better error messages by noticing a common problem: a
11843 missing trailing `;'. */
11844 token = cp_lexer_peek_token (parser->lexer);
11845 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11846 /* Look for trailing attributes to apply to this class. */
11847 if (cp_parser_allow_gnu_extensions_p (parser))
11849 tree sub_attr = cp_parser_attributes_opt (parser);
11850 attributes = chainon (attributes, sub_attr);
11852 if (type != error_mark_node)
11853 type = finish_struct (type, attributes);
11854 if (pop_p)
11855 pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11856 /* If this class is not itself within the scope of another class,
11857 then we need to parse the bodies of all of the queued function
11858 definitions. Note that the queued functions defined in a class
11859 are not always processed immediately following the
11860 class-specifier for that class. Consider:
11862 struct A {
11863 struct B { void f() { sizeof (A); } };
11866 If `f' were processed before the processing of `A' were
11867 completed, there would be no way to compute the size of `A'.
11868 Note that the nesting we are interested in here is lexical --
11869 not the semantic nesting given by TYPE_CONTEXT. In particular,
11870 for:
11872 struct A { struct B; };
11873 struct A::B { void f() { } };
11875 there is no need to delay the parsing of `A::B::f'. */
11876 if (--parser->num_classes_being_defined == 0)
11878 tree queue_entry;
11879 tree fn;
11881 /* In a first pass, parse default arguments to the functions.
11882 Then, in a second pass, parse the bodies of the functions.
11883 This two-phased approach handles cases like:
11885 struct S {
11886 void f() { g(); }
11887 void g(int i = 3);
11891 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11892 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11893 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11894 TREE_PURPOSE (parser->unparsed_functions_queues)
11895 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11897 fn = TREE_VALUE (queue_entry);
11898 /* Make sure that any template parameters are in scope. */
11899 maybe_begin_member_template_processing (fn);
11900 /* If there are default arguments that have not yet been processed,
11901 take care of them now. */
11902 cp_parser_late_parsing_default_args (parser, fn);
11903 /* Remove any template parameters from the symbol table. */
11904 maybe_end_member_template_processing ();
11906 /* Now parse the body of the functions. */
11907 for (TREE_VALUE (parser->unparsed_functions_queues)
11908 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11909 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11910 TREE_VALUE (parser->unparsed_functions_queues)
11911 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11913 /* Figure out which function we need to process. */
11914 fn = TREE_VALUE (queue_entry);
11916 /* A hack to prevent garbage collection. */
11917 function_depth++;
11919 /* Parse the function. */
11920 cp_parser_late_parsing_for_member (parser, fn);
11921 function_depth--;
11926 /* Put back any saved access checks. */
11927 pop_deferring_access_checks ();
11929 /* Restore the count of active template-parameter-lists. */
11930 parser->num_template_parameter_lists
11931 = saved_num_template_parameter_lists;
11933 return type;
11936 /* Parse a class-head.
11938 class-head:
11939 class-key identifier [opt] base-clause [opt]
11940 class-key nested-name-specifier identifier base-clause [opt]
11941 class-key nested-name-specifier [opt] template-id
11942 base-clause [opt]
11944 GNU Extensions:
11945 class-key attributes identifier [opt] base-clause [opt]
11946 class-key attributes nested-name-specifier identifier base-clause [opt]
11947 class-key attributes nested-name-specifier [opt] template-id
11948 base-clause [opt]
11950 Returns the TYPE of the indicated class. Sets
11951 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11952 involving a nested-name-specifier was used, and FALSE otherwise.
11954 Returns NULL_TREE if the class-head is syntactically valid, but
11955 semantically invalid in a way that means we should skip the entire
11956 body of the class. */
11958 static tree
11959 cp_parser_class_head (cp_parser* parser,
11960 bool* nested_name_specifier_p,
11961 tree *attributes_p)
11963 cp_token *token;
11964 tree nested_name_specifier;
11965 enum tag_types class_key;
11966 tree id = NULL_TREE;
11967 tree type = NULL_TREE;
11968 tree attributes;
11969 bool template_id_p = false;
11970 bool qualified_p = false;
11971 bool invalid_nested_name_p = false;
11972 bool invalid_explicit_specialization_p = false;
11973 bool pop_p = false;
11974 unsigned num_templates;
11976 /* Assume no nested-name-specifier will be present. */
11977 *nested_name_specifier_p = false;
11978 /* Assume no template parameter lists will be used in defining the
11979 type. */
11980 num_templates = 0;
11982 /* Look for the class-key. */
11983 class_key = cp_parser_class_key (parser);
11984 if (class_key == none_type)
11985 return error_mark_node;
11987 /* Parse the attributes. */
11988 attributes = cp_parser_attributes_opt (parser);
11990 /* If the next token is `::', that is invalid -- but sometimes
11991 people do try to write:
11993 struct ::S {};
11995 Handle this gracefully by accepting the extra qualifier, and then
11996 issuing an error about it later if this really is a
11997 class-head. If it turns out just to be an elaborated type
11998 specifier, remain silent. */
11999 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12000 qualified_p = true;
12002 push_deferring_access_checks (dk_no_check);
12004 /* Determine the name of the class. Begin by looking for an
12005 optional nested-name-specifier. */
12006 nested_name_specifier
12007 = cp_parser_nested_name_specifier_opt (parser,
12008 /*typename_keyword_p=*/false,
12009 /*check_dependency_p=*/false,
12010 /*type_p=*/false,
12011 /*is_declaration=*/false);
12012 /* If there was a nested-name-specifier, then there *must* be an
12013 identifier. */
12014 if (nested_name_specifier)
12016 /* Although the grammar says `identifier', it really means
12017 `class-name' or `template-name'. You are only allowed to
12018 define a class that has already been declared with this
12019 syntax.
12021 The proposed resolution for Core Issue 180 says that whever
12022 you see `class T::X' you should treat `X' as a type-name.
12024 It is OK to define an inaccessible class; for example:
12026 class A { class B; };
12027 class A::B {};
12029 We do not know if we will see a class-name, or a
12030 template-name. We look for a class-name first, in case the
12031 class-name is a template-id; if we looked for the
12032 template-name first we would stop after the template-name. */
12033 cp_parser_parse_tentatively (parser);
12034 type = cp_parser_class_name (parser,
12035 /*typename_keyword_p=*/false,
12036 /*template_keyword_p=*/false,
12037 /*type_p=*/true,
12038 /*check_dependency_p=*/false,
12039 /*class_head_p=*/true,
12040 /*is_declaration=*/false);
12041 /* If that didn't work, ignore the nested-name-specifier. */
12042 if (!cp_parser_parse_definitely (parser))
12044 invalid_nested_name_p = true;
12045 id = cp_parser_identifier (parser);
12046 if (id == error_mark_node)
12047 id = NULL_TREE;
12049 /* If we could not find a corresponding TYPE, treat this
12050 declaration like an unqualified declaration. */
12051 if (type == error_mark_node)
12052 nested_name_specifier = NULL_TREE;
12053 /* Otherwise, count the number of templates used in TYPE and its
12054 containing scopes. */
12055 else
12057 tree scope;
12059 for (scope = TREE_TYPE (type);
12060 scope && TREE_CODE (scope) != NAMESPACE_DECL;
12061 scope = (TYPE_P (scope)
12062 ? TYPE_CONTEXT (scope)
12063 : DECL_CONTEXT (scope)))
12064 if (TYPE_P (scope)
12065 && CLASS_TYPE_P (scope)
12066 && CLASSTYPE_TEMPLATE_INFO (scope)
12067 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12068 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12069 ++num_templates;
12072 /* Otherwise, the identifier is optional. */
12073 else
12075 /* We don't know whether what comes next is a template-id,
12076 an identifier, or nothing at all. */
12077 cp_parser_parse_tentatively (parser);
12078 /* Check for a template-id. */
12079 id = cp_parser_template_id (parser,
12080 /*template_keyword_p=*/false,
12081 /*check_dependency_p=*/true,
12082 /*is_declaration=*/true);
12083 /* If that didn't work, it could still be an identifier. */
12084 if (!cp_parser_parse_definitely (parser))
12086 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12087 id = cp_parser_identifier (parser);
12088 else
12089 id = NULL_TREE;
12091 else
12093 template_id_p = true;
12094 ++num_templates;
12098 pop_deferring_access_checks ();
12100 cp_parser_check_for_invalid_template_id (parser, id);
12102 /* If it's not a `:' or a `{' then we can't really be looking at a
12103 class-head, since a class-head only appears as part of a
12104 class-specifier. We have to detect this situation before calling
12105 xref_tag, since that has irreversible side-effects. */
12106 if (!cp_parser_next_token_starts_class_definition_p (parser))
12108 cp_parser_error (parser, "expected `{' or `:'");
12109 return error_mark_node;
12112 /* At this point, we're going ahead with the class-specifier, even
12113 if some other problem occurs. */
12114 cp_parser_commit_to_tentative_parse (parser);
12115 /* Issue the error about the overly-qualified name now. */
12116 if (qualified_p)
12117 cp_parser_error (parser,
12118 "global qualification of class name is invalid");
12119 else if (invalid_nested_name_p)
12120 cp_parser_error (parser,
12121 "qualified name does not name a class");
12122 else if (nested_name_specifier)
12124 tree scope;
12125 /* Figure out in what scope the declaration is being placed. */
12126 scope = current_scope ();
12127 if (!scope)
12128 scope = current_namespace;
12129 /* If that scope does not contain the scope in which the
12130 class was originally declared, the program is invalid. */
12131 if (scope && !is_ancestor (scope, nested_name_specifier))
12133 error ("declaration of `%D' in `%D' which does not "
12134 "enclose `%D'", type, scope, nested_name_specifier);
12135 type = NULL_TREE;
12136 goto done;
12138 /* [dcl.meaning]
12140 A declarator-id shall not be qualified exception of the
12141 definition of a ... nested class outside of its class
12142 ... [or] a the definition or explicit instantiation of a
12143 class member of a namespace outside of its namespace. */
12144 if (scope == nested_name_specifier)
12146 pedwarn ("extra qualification ignored");
12147 nested_name_specifier = NULL_TREE;
12148 num_templates = 0;
12151 /* An explicit-specialization must be preceded by "template <>". If
12152 it is not, try to recover gracefully. */
12153 if (at_namespace_scope_p ()
12154 && parser->num_template_parameter_lists == 0
12155 && template_id_p)
12157 error ("an explicit specialization must be preceded by 'template <>'");
12158 invalid_explicit_specialization_p = true;
12159 /* Take the same action that would have been taken by
12160 cp_parser_explicit_specialization. */
12161 ++parser->num_template_parameter_lists;
12162 begin_specialization ();
12164 /* There must be no "return" statements between this point and the
12165 end of this function; set "type "to the correct return value and
12166 use "goto done;" to return. */
12167 /* Make sure that the right number of template parameters were
12168 present. */
12169 if (!cp_parser_check_template_parameters (parser, num_templates))
12171 /* If something went wrong, there is no point in even trying to
12172 process the class-definition. */
12173 type = NULL_TREE;
12174 goto done;
12177 /* Look up the type. */
12178 if (template_id_p)
12180 type = TREE_TYPE (id);
12181 maybe_process_partial_specialization (type);
12183 else if (!nested_name_specifier)
12185 /* If the class was unnamed, create a dummy name. */
12186 if (!id)
12187 id = make_anon_name ();
12188 type = xref_tag (class_key, id, /*globalize=*/false,
12189 parser->num_template_parameter_lists);
12191 else
12193 tree class_type;
12194 bool pop_p = false;
12196 /* Given:
12198 template <typename T> struct S { struct T };
12199 template <typename T> struct S<T>::T { };
12201 we will get a TYPENAME_TYPE when processing the definition of
12202 `S::T'. We need to resolve it to the actual type before we
12203 try to define it. */
12204 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12206 class_type = resolve_typename_type (TREE_TYPE (type),
12207 /*only_current_p=*/false);
12208 if (class_type != error_mark_node)
12209 type = TYPE_NAME (class_type);
12210 else
12212 cp_parser_error (parser, "could not resolve typename type");
12213 type = error_mark_node;
12217 maybe_process_partial_specialization (TREE_TYPE (type));
12218 class_type = current_class_type;
12219 /* Enter the scope indicated by the nested-name-specifier. */
12220 if (nested_name_specifier)
12221 pop_p = push_scope (nested_name_specifier);
12222 /* Get the canonical version of this type. */
12223 type = TYPE_MAIN_DECL (TREE_TYPE (type));
12224 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12225 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12226 type = push_template_decl (type);
12227 type = TREE_TYPE (type);
12228 if (nested_name_specifier)
12230 *nested_name_specifier_p = true;
12231 if (pop_p)
12232 pop_scope (nested_name_specifier);
12235 /* Indicate whether this class was declared as a `class' or as a
12236 `struct'. */
12237 if (TREE_CODE (type) == RECORD_TYPE)
12238 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12239 cp_parser_check_class_key (class_key, type);
12241 /* Enter the scope containing the class; the names of base classes
12242 should be looked up in that context. For example, given:
12244 struct A { struct B {}; struct C; };
12245 struct A::C : B {};
12247 is valid. */
12248 if (nested_name_specifier)
12249 pop_p = push_scope (nested_name_specifier);
12250 /* Now, look for the base-clause. */
12251 token = cp_lexer_peek_token (parser->lexer);
12252 if (token->type == CPP_COLON)
12254 tree bases;
12256 /* Get the list of base-classes. */
12257 bases = cp_parser_base_clause (parser);
12258 /* Process them. */
12259 xref_basetypes (type, bases);
12261 /* Leave the scope given by the nested-name-specifier. We will
12262 enter the class scope itself while processing the members. */
12263 if (pop_p)
12264 pop_scope (nested_name_specifier);
12266 done:
12267 if (invalid_explicit_specialization_p)
12269 end_specialization ();
12270 --parser->num_template_parameter_lists;
12272 *attributes_p = attributes;
12273 return type;
12276 /* Parse a class-key.
12278 class-key:
12279 class
12280 struct
12281 union
12283 Returns the kind of class-key specified, or none_type to indicate
12284 error. */
12286 static enum tag_types
12287 cp_parser_class_key (cp_parser* parser)
12289 cp_token *token;
12290 enum tag_types tag_type;
12292 /* Look for the class-key. */
12293 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12294 if (!token)
12295 return none_type;
12297 /* Check to see if the TOKEN is a class-key. */
12298 tag_type = cp_parser_token_is_class_key (token);
12299 if (!tag_type)
12300 cp_parser_error (parser, "expected class-key");
12301 return tag_type;
12304 /* Parse an (optional) member-specification.
12306 member-specification:
12307 member-declaration member-specification [opt]
12308 access-specifier : member-specification [opt] */
12310 static void
12311 cp_parser_member_specification_opt (cp_parser* parser)
12313 while (true)
12315 cp_token *token;
12316 enum rid keyword;
12318 /* Peek at the next token. */
12319 token = cp_lexer_peek_token (parser->lexer);
12320 /* If it's a `}', or EOF then we've seen all the members. */
12321 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12322 break;
12324 /* See if this token is a keyword. */
12325 keyword = token->keyword;
12326 switch (keyword)
12328 case RID_PUBLIC:
12329 case RID_PROTECTED:
12330 case RID_PRIVATE:
12331 /* Consume the access-specifier. */
12332 cp_lexer_consume_token (parser->lexer);
12333 /* Remember which access-specifier is active. */
12334 current_access_specifier = token->value;
12335 /* Look for the `:'. */
12336 cp_parser_require (parser, CPP_COLON, "`:'");
12337 break;
12339 default:
12340 /* Otherwise, the next construction must be a
12341 member-declaration. */
12342 cp_parser_member_declaration (parser);
12347 /* Parse a member-declaration.
12349 member-declaration:
12350 decl-specifier-seq [opt] member-declarator-list [opt] ;
12351 function-definition ; [opt]
12352 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12353 using-declaration
12354 template-declaration
12356 member-declarator-list:
12357 member-declarator
12358 member-declarator-list , member-declarator
12360 member-declarator:
12361 declarator pure-specifier [opt]
12362 declarator constant-initializer [opt]
12363 identifier [opt] : constant-expression
12365 GNU Extensions:
12367 member-declaration:
12368 __extension__ member-declaration
12370 member-declarator:
12371 declarator attributes [opt] pure-specifier [opt]
12372 declarator attributes [opt] constant-initializer [opt]
12373 identifier [opt] attributes [opt] : constant-expression */
12375 static void
12376 cp_parser_member_declaration (cp_parser* parser)
12378 tree decl_specifiers;
12379 tree prefix_attributes;
12380 tree decl;
12381 int declares_class_or_enum;
12382 bool friend_p;
12383 cp_token *token;
12384 int saved_pedantic;
12386 /* Check for the `__extension__' keyword. */
12387 if (cp_parser_extension_opt (parser, &saved_pedantic))
12389 /* Recurse. */
12390 cp_parser_member_declaration (parser);
12391 /* Restore the old value of the PEDANTIC flag. */
12392 pedantic = saved_pedantic;
12394 return;
12397 /* Check for a template-declaration. */
12398 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12400 /* Parse the template-declaration. */
12401 cp_parser_template_declaration (parser, /*member_p=*/true);
12403 return;
12406 /* Check for a using-declaration. */
12407 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12409 /* Parse the using-declaration. */
12410 cp_parser_using_declaration (parser);
12412 return;
12415 /* Parse the decl-specifier-seq. */
12416 decl_specifiers
12417 = cp_parser_decl_specifier_seq (parser,
12418 CP_PARSER_FLAGS_OPTIONAL,
12419 &prefix_attributes,
12420 &declares_class_or_enum);
12421 /* Check for an invalid type-name. */
12422 if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12423 return;
12424 /* If there is no declarator, then the decl-specifier-seq should
12425 specify a type. */
12426 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12428 /* If there was no decl-specifier-seq, and the next token is a
12429 `;', then we have something like:
12431 struct S { ; };
12433 [class.mem]
12435 Each member-declaration shall declare at least one member
12436 name of the class. */
12437 if (!decl_specifiers)
12439 if (pedantic)
12440 pedwarn ("extra semicolon");
12442 else
12444 tree type;
12446 /* See if this declaration is a friend. */
12447 friend_p = cp_parser_friend_p (decl_specifiers);
12448 /* If there were decl-specifiers, check to see if there was
12449 a class-declaration. */
12450 type = check_tag_decl (decl_specifiers);
12451 /* Nested classes have already been added to the class, but
12452 a `friend' needs to be explicitly registered. */
12453 if (friend_p)
12455 /* If the `friend' keyword was present, the friend must
12456 be introduced with a class-key. */
12457 if (!declares_class_or_enum)
12458 error ("a class-key must be used when declaring a friend");
12459 /* In this case:
12461 template <typename T> struct A {
12462 friend struct A<T>::B;
12465 A<T>::B will be represented by a TYPENAME_TYPE, and
12466 therefore not recognized by check_tag_decl. */
12467 if (!type)
12469 tree specifier;
12471 for (specifier = decl_specifiers;
12472 specifier;
12473 specifier = TREE_CHAIN (specifier))
12475 tree s = TREE_VALUE (specifier);
12477 if (TREE_CODE (s) == IDENTIFIER_NODE)
12478 get_global_value_if_present (s, &type);
12479 if (TREE_CODE (s) == TYPE_DECL)
12480 s = TREE_TYPE (s);
12481 if (TYPE_P (s))
12483 type = s;
12484 break;
12488 if (!type || !TYPE_P (type))
12489 error ("friend declaration does not name a class or "
12490 "function");
12491 else
12492 make_friend_class (current_class_type, type,
12493 /*complain=*/true);
12495 /* If there is no TYPE, an error message will already have
12496 been issued. */
12497 else if (!type)
12499 /* An anonymous aggregate has to be handled specially; such
12500 a declaration really declares a data member (with a
12501 particular type), as opposed to a nested class. */
12502 else if (ANON_AGGR_TYPE_P (type))
12504 /* Remove constructors and such from TYPE, now that we
12505 know it is an anonymous aggregate. */
12506 fixup_anonymous_aggr (type);
12507 /* And make the corresponding data member. */
12508 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12509 /* Add it to the class. */
12510 finish_member_declaration (decl);
12512 else
12513 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12516 else
12518 /* See if these declarations will be friends. */
12519 friend_p = cp_parser_friend_p (decl_specifiers);
12521 /* Keep going until we hit the `;' at the end of the
12522 declaration. */
12523 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12525 tree attributes = NULL_TREE;
12526 tree first_attribute;
12528 /* Peek at the next token. */
12529 token = cp_lexer_peek_token (parser->lexer);
12531 /* Check for a bitfield declaration. */
12532 if (token->type == CPP_COLON
12533 || (token->type == CPP_NAME
12534 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12535 == CPP_COLON))
12537 tree identifier;
12538 tree width;
12540 /* Get the name of the bitfield. Note that we cannot just
12541 check TOKEN here because it may have been invalidated by
12542 the call to cp_lexer_peek_nth_token above. */
12543 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12544 identifier = cp_parser_identifier (parser);
12545 else
12546 identifier = NULL_TREE;
12548 /* Consume the `:' token. */
12549 cp_lexer_consume_token (parser->lexer);
12550 /* Get the width of the bitfield. */
12551 width
12552 = cp_parser_constant_expression (parser,
12553 /*allow_non_constant=*/false,
12554 NULL);
12556 /* Look for attributes that apply to the bitfield. */
12557 attributes = cp_parser_attributes_opt (parser);
12558 /* Remember which attributes are prefix attributes and
12559 which are not. */
12560 first_attribute = attributes;
12561 /* Combine the attributes. */
12562 attributes = chainon (prefix_attributes, attributes);
12564 /* Create the bitfield declaration. */
12565 decl = grokbitfield (identifier,
12566 decl_specifiers,
12567 width);
12568 /* Apply the attributes. */
12569 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12571 else
12573 tree declarator;
12574 tree initializer;
12575 tree asm_specification;
12576 int ctor_dtor_or_conv_p;
12578 /* Parse the declarator. */
12579 declarator
12580 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12581 &ctor_dtor_or_conv_p,
12582 /*parenthesized_p=*/NULL);
12584 /* If something went wrong parsing the declarator, make sure
12585 that we at least consume some tokens. */
12586 if (declarator == error_mark_node)
12588 /* Skip to the end of the statement. */
12589 cp_parser_skip_to_end_of_statement (parser);
12590 /* If the next token is not a semicolon, that is
12591 probably because we just skipped over the body of
12592 a function. So, we consume a semicolon if
12593 present, but do not issue an error message if it
12594 is not present. */
12595 if (cp_lexer_next_token_is (parser->lexer,
12596 CPP_SEMICOLON))
12597 cp_lexer_consume_token (parser->lexer);
12598 return;
12601 cp_parser_check_for_definition_in_return_type
12602 (declarator, declares_class_or_enum);
12604 /* Look for an asm-specification. */
12605 asm_specification = cp_parser_asm_specification_opt (parser);
12606 /* Look for attributes that apply to the declaration. */
12607 attributes = cp_parser_attributes_opt (parser);
12608 /* Remember which attributes are prefix attributes and
12609 which are not. */
12610 first_attribute = attributes;
12611 /* Combine the attributes. */
12612 attributes = chainon (prefix_attributes, attributes);
12614 /* If it's an `=', then we have a constant-initializer or a
12615 pure-specifier. It is not correct to parse the
12616 initializer before registering the member declaration
12617 since the member declaration should be in scope while
12618 its initializer is processed. However, the rest of the
12619 front end does not yet provide an interface that allows
12620 us to handle this correctly. */
12621 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12623 /* In [class.mem]:
12625 A pure-specifier shall be used only in the declaration of
12626 a virtual function.
12628 A member-declarator can contain a constant-initializer
12629 only if it declares a static member of integral or
12630 enumeration type.
12632 Therefore, if the DECLARATOR is for a function, we look
12633 for a pure-specifier; otherwise, we look for a
12634 constant-initializer. When we call `grokfield', it will
12635 perform more stringent semantics checks. */
12636 if (TREE_CODE (declarator) == CALL_EXPR)
12637 initializer = cp_parser_pure_specifier (parser);
12638 else
12639 /* Parse the initializer. */
12640 initializer = cp_parser_constant_initializer (parser);
12642 /* Otherwise, there is no initializer. */
12643 else
12644 initializer = NULL_TREE;
12646 /* See if we are probably looking at a function
12647 definition. We are certainly not looking at at a
12648 member-declarator. Calling `grokfield' has
12649 side-effects, so we must not do it unless we are sure
12650 that we are looking at a member-declarator. */
12651 if (cp_parser_token_starts_function_definition_p
12652 (cp_lexer_peek_token (parser->lexer)))
12654 /* The grammar does not allow a pure-specifier to be
12655 used when a member function is defined. (It is
12656 possible that this fact is an oversight in the
12657 standard, since a pure function may be defined
12658 outside of the class-specifier. */
12659 if (initializer)
12660 error ("pure-specifier on function-definition");
12661 decl = cp_parser_save_member_function_body (parser,
12662 decl_specifiers,
12663 declarator,
12664 attributes);
12665 /* If the member was not a friend, declare it here. */
12666 if (!friend_p)
12667 finish_member_declaration (decl);
12668 /* Peek at the next token. */
12669 token = cp_lexer_peek_token (parser->lexer);
12670 /* If the next token is a semicolon, consume it. */
12671 if (token->type == CPP_SEMICOLON)
12672 cp_lexer_consume_token (parser->lexer);
12673 return;
12675 else
12677 /* Create the declaration. */
12678 decl = grokfield (declarator, decl_specifiers,
12679 initializer, asm_specification,
12680 attributes);
12681 /* Any initialization must have been from a
12682 constant-expression. */
12683 if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12684 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12688 /* Reset PREFIX_ATTRIBUTES. */
12689 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12690 attributes = TREE_CHAIN (attributes);
12691 if (attributes)
12692 TREE_CHAIN (attributes) = NULL_TREE;
12694 /* If there is any qualification still in effect, clear it
12695 now; we will be starting fresh with the next declarator. */
12696 parser->scope = NULL_TREE;
12697 parser->qualifying_scope = NULL_TREE;
12698 parser->object_scope = NULL_TREE;
12699 /* If it's a `,', then there are more declarators. */
12700 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12701 cp_lexer_consume_token (parser->lexer);
12702 /* If the next token isn't a `;', then we have a parse error. */
12703 else if (cp_lexer_next_token_is_not (parser->lexer,
12704 CPP_SEMICOLON))
12706 cp_parser_error (parser, "expected `;'");
12707 /* Skip tokens until we find a `;'. */
12708 cp_parser_skip_to_end_of_statement (parser);
12710 break;
12713 if (decl)
12715 /* Add DECL to the list of members. */
12716 if (!friend_p)
12717 finish_member_declaration (decl);
12719 if (TREE_CODE (decl) == FUNCTION_DECL)
12720 cp_parser_save_default_args (parser, decl);
12725 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12728 /* Parse a pure-specifier.
12730 pure-specifier:
12733 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12734 Otherwise, ERROR_MARK_NODE is returned. */
12736 static tree
12737 cp_parser_pure_specifier (cp_parser* parser)
12739 cp_token *token;
12741 /* Look for the `=' token. */
12742 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12743 return error_mark_node;
12744 /* Look for the `0' token. */
12745 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12746 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12747 to get information from the lexer about how the number was
12748 spelled in order to fix this problem. */
12749 if (!token || !integer_zerop (token->value))
12750 return error_mark_node;
12752 return integer_zero_node;
12755 /* Parse a constant-initializer.
12757 constant-initializer:
12758 = constant-expression
12760 Returns a representation of the constant-expression. */
12762 static tree
12763 cp_parser_constant_initializer (cp_parser* parser)
12765 /* Look for the `=' token. */
12766 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12767 return error_mark_node;
12769 /* It is invalid to write:
12771 struct S { static const int i = { 7 }; };
12774 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12776 cp_parser_error (parser,
12777 "a brace-enclosed initializer is not allowed here");
12778 /* Consume the opening brace. */
12779 cp_lexer_consume_token (parser->lexer);
12780 /* Skip the initializer. */
12781 cp_parser_skip_to_closing_brace (parser);
12782 /* Look for the trailing `}'. */
12783 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12785 return error_mark_node;
12788 return cp_parser_constant_expression (parser,
12789 /*allow_non_constant=*/false,
12790 NULL);
12793 /* Derived classes [gram.class.derived] */
12795 /* Parse a base-clause.
12797 base-clause:
12798 : base-specifier-list
12800 base-specifier-list:
12801 base-specifier
12802 base-specifier-list , base-specifier
12804 Returns a TREE_LIST representing the base-classes, in the order in
12805 which they were declared. The representation of each node is as
12806 described by cp_parser_base_specifier.
12808 In the case that no bases are specified, this function will return
12809 NULL_TREE, not ERROR_MARK_NODE. */
12811 static tree
12812 cp_parser_base_clause (cp_parser* parser)
12814 tree bases = NULL_TREE;
12816 /* Look for the `:' that begins the list. */
12817 cp_parser_require (parser, CPP_COLON, "`:'");
12819 /* Scan the base-specifier-list. */
12820 while (true)
12822 cp_token *token;
12823 tree base;
12825 /* Look for the base-specifier. */
12826 base = cp_parser_base_specifier (parser);
12827 /* Add BASE to the front of the list. */
12828 if (base != error_mark_node)
12830 TREE_CHAIN (base) = bases;
12831 bases = base;
12833 /* Peek at the next token. */
12834 token = cp_lexer_peek_token (parser->lexer);
12835 /* If it's not a comma, then the list is complete. */
12836 if (token->type != CPP_COMMA)
12837 break;
12838 /* Consume the `,'. */
12839 cp_lexer_consume_token (parser->lexer);
12842 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12843 base class had a qualified name. However, the next name that
12844 appears is certainly not qualified. */
12845 parser->scope = NULL_TREE;
12846 parser->qualifying_scope = NULL_TREE;
12847 parser->object_scope = NULL_TREE;
12849 return nreverse (bases);
12852 /* Parse a base-specifier.
12854 base-specifier:
12855 :: [opt] nested-name-specifier [opt] class-name
12856 virtual access-specifier [opt] :: [opt] nested-name-specifier
12857 [opt] class-name
12858 access-specifier virtual [opt] :: [opt] nested-name-specifier
12859 [opt] class-name
12861 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12862 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12863 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12864 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12866 static tree
12867 cp_parser_base_specifier (cp_parser* parser)
12869 cp_token *token;
12870 bool done = false;
12871 bool virtual_p = false;
12872 bool duplicate_virtual_error_issued_p = false;
12873 bool duplicate_access_error_issued_p = false;
12874 bool class_scope_p, template_p;
12875 tree access = access_default_node;
12876 tree type;
12878 /* Process the optional `virtual' and `access-specifier'. */
12879 while (!done)
12881 /* Peek at the next token. */
12882 token = cp_lexer_peek_token (parser->lexer);
12883 /* Process `virtual'. */
12884 switch (token->keyword)
12886 case RID_VIRTUAL:
12887 /* If `virtual' appears more than once, issue an error. */
12888 if (virtual_p && !duplicate_virtual_error_issued_p)
12890 cp_parser_error (parser,
12891 "`virtual' specified more than once in base-specified");
12892 duplicate_virtual_error_issued_p = true;
12895 virtual_p = true;
12897 /* Consume the `virtual' token. */
12898 cp_lexer_consume_token (parser->lexer);
12900 break;
12902 case RID_PUBLIC:
12903 case RID_PROTECTED:
12904 case RID_PRIVATE:
12905 /* If more than one access specifier appears, issue an
12906 error. */
12907 if (access != access_default_node
12908 && !duplicate_access_error_issued_p)
12910 cp_parser_error (parser,
12911 "more than one access specifier in base-specified");
12912 duplicate_access_error_issued_p = true;
12915 access = ridpointers[(int) token->keyword];
12917 /* Consume the access-specifier. */
12918 cp_lexer_consume_token (parser->lexer);
12920 break;
12922 default:
12923 done = true;
12924 break;
12927 /* It is not uncommon to see programs mechanically, erroneously, use
12928 the 'typename' keyword to denote (dependent) qualified types
12929 as base classes. */
12930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12932 if (!processing_template_decl)
12933 error ("keyword `typename' not allowed outside of templates");
12934 else
12935 error ("keyword `typename' not allowed in this context "
12936 "(the base class is implicitly a type)");
12937 cp_lexer_consume_token (parser->lexer);
12940 /* Look for the optional `::' operator. */
12941 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12942 /* Look for the nested-name-specifier. The simplest way to
12943 implement:
12945 [temp.res]
12947 The keyword `typename' is not permitted in a base-specifier or
12948 mem-initializer; in these contexts a qualified name that
12949 depends on a template-parameter is implicitly assumed to be a
12950 type name.
12952 is to pretend that we have seen the `typename' keyword at this
12953 point. */
12954 cp_parser_nested_name_specifier_opt (parser,
12955 /*typename_keyword_p=*/true,
12956 /*check_dependency_p=*/true,
12957 /*type_p=*/true,
12958 /*is_declaration=*/true);
12959 /* If the base class is given by a qualified name, assume that names
12960 we see are type names or templates, as appropriate. */
12961 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12962 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12964 /* Finally, look for the class-name. */
12965 type = cp_parser_class_name (parser,
12966 class_scope_p,
12967 template_p,
12968 /*type_p=*/true,
12969 /*check_dependency_p=*/true,
12970 /*class_head_p=*/false,
12971 /*is_declaration=*/true);
12973 if (type == error_mark_node)
12974 return error_mark_node;
12976 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12979 /* Exception handling [gram.exception] */
12981 /* Parse an (optional) exception-specification.
12983 exception-specification:
12984 throw ( type-id-list [opt] )
12986 Returns a TREE_LIST representing the exception-specification. The
12987 TREE_VALUE of each node is a type. */
12989 static tree
12990 cp_parser_exception_specification_opt (cp_parser* parser)
12992 cp_token *token;
12993 tree type_id_list;
12995 /* Peek at the next token. */
12996 token = cp_lexer_peek_token (parser->lexer);
12997 /* If it's not `throw', then there's no exception-specification. */
12998 if (!cp_parser_is_keyword (token, RID_THROW))
12999 return NULL_TREE;
13001 /* Consume the `throw'. */
13002 cp_lexer_consume_token (parser->lexer);
13004 /* Look for the `('. */
13005 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13007 /* Peek at the next token. */
13008 token = cp_lexer_peek_token (parser->lexer);
13009 /* If it's not a `)', then there is a type-id-list. */
13010 if (token->type != CPP_CLOSE_PAREN)
13012 const char *saved_message;
13014 /* Types may not be defined in an exception-specification. */
13015 saved_message = parser->type_definition_forbidden_message;
13016 parser->type_definition_forbidden_message
13017 = "types may not be defined in an exception-specification";
13018 /* Parse the type-id-list. */
13019 type_id_list = cp_parser_type_id_list (parser);
13020 /* Restore the saved message. */
13021 parser->type_definition_forbidden_message = saved_message;
13023 else
13024 type_id_list = empty_except_spec;
13026 /* Look for the `)'. */
13027 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13029 return type_id_list;
13032 /* Parse an (optional) type-id-list.
13034 type-id-list:
13035 type-id
13036 type-id-list , type-id
13038 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
13039 in the order that the types were presented. */
13041 static tree
13042 cp_parser_type_id_list (cp_parser* parser)
13044 tree types = NULL_TREE;
13046 while (true)
13048 cp_token *token;
13049 tree type;
13051 /* Get the next type-id. */
13052 type = cp_parser_type_id (parser);
13053 /* Add it to the list. */
13054 types = add_exception_specifier (types, type, /*complain=*/1);
13055 /* Peek at the next token. */
13056 token = cp_lexer_peek_token (parser->lexer);
13057 /* If it is not a `,', we are done. */
13058 if (token->type != CPP_COMMA)
13059 break;
13060 /* Consume the `,'. */
13061 cp_lexer_consume_token (parser->lexer);
13064 return nreverse (types);
13067 /* Parse a try-block.
13069 try-block:
13070 try compound-statement handler-seq */
13072 static tree
13073 cp_parser_try_block (cp_parser* parser)
13075 tree try_block;
13077 cp_parser_require_keyword (parser, RID_TRY, "`try'");
13078 try_block = begin_try_block ();
13079 cp_parser_compound_statement (parser, false);
13080 finish_try_block (try_block);
13081 cp_parser_handler_seq (parser);
13082 finish_handler_sequence (try_block);
13084 return try_block;
13087 /* Parse a function-try-block.
13089 function-try-block:
13090 try ctor-initializer [opt] function-body handler-seq */
13092 static bool
13093 cp_parser_function_try_block (cp_parser* parser)
13095 tree try_block;
13096 bool ctor_initializer_p;
13098 /* Look for the `try' keyword. */
13099 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13100 return false;
13101 /* Let the rest of the front-end know where we are. */
13102 try_block = begin_function_try_block ();
13103 /* Parse the function-body. */
13104 ctor_initializer_p
13105 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13106 /* We're done with the `try' part. */
13107 finish_function_try_block (try_block);
13108 /* Parse the handlers. */
13109 cp_parser_handler_seq (parser);
13110 /* We're done with the handlers. */
13111 finish_function_handler_sequence (try_block);
13113 return ctor_initializer_p;
13116 /* Parse a handler-seq.
13118 handler-seq:
13119 handler handler-seq [opt] */
13121 static void
13122 cp_parser_handler_seq (cp_parser* parser)
13124 while (true)
13126 cp_token *token;
13128 /* Parse the handler. */
13129 cp_parser_handler (parser);
13130 /* Peek at the next token. */
13131 token = cp_lexer_peek_token (parser->lexer);
13132 /* If it's not `catch' then there are no more handlers. */
13133 if (!cp_parser_is_keyword (token, RID_CATCH))
13134 break;
13138 /* Parse a handler.
13140 handler:
13141 catch ( exception-declaration ) compound-statement */
13143 static void
13144 cp_parser_handler (cp_parser* parser)
13146 tree handler;
13147 tree declaration;
13149 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13150 handler = begin_handler ();
13151 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13152 declaration = cp_parser_exception_declaration (parser);
13153 finish_handler_parms (declaration, handler);
13154 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13155 cp_parser_compound_statement (parser, false);
13156 finish_handler (handler);
13159 /* Parse an exception-declaration.
13161 exception-declaration:
13162 type-specifier-seq declarator
13163 type-specifier-seq abstract-declarator
13164 type-specifier-seq
13167 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13168 ellipsis variant is used. */
13170 static tree
13171 cp_parser_exception_declaration (cp_parser* parser)
13173 tree type_specifiers;
13174 tree declarator;
13175 const char *saved_message;
13177 /* If it's an ellipsis, it's easy to handle. */
13178 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13180 /* Consume the `...' token. */
13181 cp_lexer_consume_token (parser->lexer);
13182 return NULL_TREE;
13185 /* Types may not be defined in exception-declarations. */
13186 saved_message = parser->type_definition_forbidden_message;
13187 parser->type_definition_forbidden_message
13188 = "types may not be defined in exception-declarations";
13190 /* Parse the type-specifier-seq. */
13191 type_specifiers = cp_parser_type_specifier_seq (parser);
13192 /* If it's a `)', then there is no declarator. */
13193 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13194 declarator = NULL_TREE;
13195 else
13196 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13197 /*ctor_dtor_or_conv_p=*/NULL,
13198 /*parenthesized_p=*/NULL);
13200 /* Restore the saved message. */
13201 parser->type_definition_forbidden_message = saved_message;
13203 return start_handler_parms (type_specifiers, declarator);
13206 /* Parse a throw-expression.
13208 throw-expression:
13209 throw assignment-expression [opt]
13211 Returns a THROW_EXPR representing the throw-expression. */
13213 static tree
13214 cp_parser_throw_expression (cp_parser* parser)
13216 tree expression;
13217 cp_token* token;
13219 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13220 token = cp_lexer_peek_token (parser->lexer);
13221 /* Figure out whether or not there is an assignment-expression
13222 following the "throw" keyword. */
13223 if (token->type == CPP_COMMA
13224 || token->type == CPP_SEMICOLON
13225 || token->type == CPP_CLOSE_PAREN
13226 || token->type == CPP_CLOSE_SQUARE
13227 || token->type == CPP_CLOSE_BRACE
13228 || token->type == CPP_COLON)
13229 expression = NULL_TREE;
13230 else
13231 expression = cp_parser_assignment_expression (parser);
13233 return build_throw (expression);
13236 /* GNU Extensions */
13238 /* Parse an (optional) asm-specification.
13240 asm-specification:
13241 asm ( string-literal )
13243 If the asm-specification is present, returns a STRING_CST
13244 corresponding to the string-literal. Otherwise, returns
13245 NULL_TREE. */
13247 static tree
13248 cp_parser_asm_specification_opt (cp_parser* parser)
13250 cp_token *token;
13251 tree asm_specification;
13253 /* Peek at the next token. */
13254 token = cp_lexer_peek_token (parser->lexer);
13255 /* If the next token isn't the `asm' keyword, then there's no
13256 asm-specification. */
13257 if (!cp_parser_is_keyword (token, RID_ASM))
13258 return NULL_TREE;
13260 /* Consume the `asm' token. */
13261 cp_lexer_consume_token (parser->lexer);
13262 /* Look for the `('. */
13263 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13265 /* Look for the string-literal. */
13266 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13267 if (token)
13268 asm_specification = token->value;
13269 else
13270 asm_specification = NULL_TREE;
13272 /* Look for the `)'. */
13273 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13275 return asm_specification;
13278 /* Parse an asm-operand-list.
13280 asm-operand-list:
13281 asm-operand
13282 asm-operand-list , asm-operand
13284 asm-operand:
13285 string-literal ( expression )
13286 [ string-literal ] string-literal ( expression )
13288 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13289 each node is the expression. The TREE_PURPOSE is itself a
13290 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13291 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13292 is a STRING_CST for the string literal before the parenthesis. */
13294 static tree
13295 cp_parser_asm_operand_list (cp_parser* parser)
13297 tree asm_operands = NULL_TREE;
13299 while (true)
13301 tree string_literal;
13302 tree expression;
13303 tree name;
13304 cp_token *token;
13306 c_lex_string_translate = false;
13308 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13310 /* Consume the `[' token. */
13311 cp_lexer_consume_token (parser->lexer);
13312 /* Read the operand name. */
13313 name = cp_parser_identifier (parser);
13314 if (name != error_mark_node)
13315 name = build_string (IDENTIFIER_LENGTH (name),
13316 IDENTIFIER_POINTER (name));
13317 /* Look for the closing `]'. */
13318 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13320 else
13321 name = NULL_TREE;
13322 /* Look for the string-literal. */
13323 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13324 string_literal = token ? token->value : error_mark_node;
13325 c_lex_string_translate = true;
13326 /* Look for the `('. */
13327 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13328 /* Parse the expression. */
13329 expression = cp_parser_expression (parser);
13330 /* Look for the `)'. */
13331 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13332 c_lex_string_translate = false;
13333 /* Add this operand to the list. */
13334 asm_operands = tree_cons (build_tree_list (name, string_literal),
13335 expression,
13336 asm_operands);
13337 /* If the next token is not a `,', there are no more
13338 operands. */
13339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13340 break;
13341 /* Consume the `,'. */
13342 cp_lexer_consume_token (parser->lexer);
13345 return nreverse (asm_operands);
13348 /* Parse an asm-clobber-list.
13350 asm-clobber-list:
13351 string-literal
13352 asm-clobber-list , string-literal
13354 Returns a TREE_LIST, indicating the clobbers in the order that they
13355 appeared. The TREE_VALUE of each node is a STRING_CST. */
13357 static tree
13358 cp_parser_asm_clobber_list (cp_parser* parser)
13360 tree clobbers = NULL_TREE;
13362 while (true)
13364 cp_token *token;
13365 tree string_literal;
13367 /* Look for the string literal. */
13368 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13369 string_literal = token ? token->value : error_mark_node;
13370 /* Add it to the list. */
13371 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13372 /* If the next token is not a `,', then the list is
13373 complete. */
13374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13375 break;
13376 /* Consume the `,' token. */
13377 cp_lexer_consume_token (parser->lexer);
13380 return clobbers;
13383 /* Parse an (optional) series of attributes.
13385 attributes:
13386 attributes attribute
13388 attribute:
13389 __attribute__ (( attribute-list [opt] ))
13391 The return value is as for cp_parser_attribute_list. */
13393 static tree
13394 cp_parser_attributes_opt (cp_parser* parser)
13396 tree attributes = NULL_TREE;
13398 while (true)
13400 cp_token *token;
13401 tree attribute_list;
13403 /* Peek at the next token. */
13404 token = cp_lexer_peek_token (parser->lexer);
13405 /* If it's not `__attribute__', then we're done. */
13406 if (token->keyword != RID_ATTRIBUTE)
13407 break;
13409 /* Consume the `__attribute__' keyword. */
13410 cp_lexer_consume_token (parser->lexer);
13411 /* Look for the two `(' tokens. */
13412 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13413 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13415 /* Peek at the next token. */
13416 token = cp_lexer_peek_token (parser->lexer);
13417 if (token->type != CPP_CLOSE_PAREN)
13418 /* Parse the attribute-list. */
13419 attribute_list = cp_parser_attribute_list (parser);
13420 else
13421 /* If the next token is a `)', then there is no attribute
13422 list. */
13423 attribute_list = NULL;
13425 /* Look for the two `)' tokens. */
13426 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13427 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13429 /* Add these new attributes to the list. */
13430 attributes = chainon (attributes, attribute_list);
13433 return attributes;
13436 /* Parse an attribute-list.
13438 attribute-list:
13439 attribute
13440 attribute-list , attribute
13442 attribute:
13443 identifier
13444 identifier ( identifier )
13445 identifier ( identifier , expression-list )
13446 identifier ( expression-list )
13448 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13449 TREE_PURPOSE of each node is the identifier indicating which
13450 attribute is in use. The TREE_VALUE represents the arguments, if
13451 any. */
13453 static tree
13454 cp_parser_attribute_list (cp_parser* parser)
13456 tree attribute_list = NULL_TREE;
13458 c_lex_string_translate = false;
13459 while (true)
13461 cp_token *token;
13462 tree identifier;
13463 tree attribute;
13465 /* Look for the identifier. We also allow keywords here; for
13466 example `__attribute__ ((const))' is legal. */
13467 token = cp_lexer_peek_token (parser->lexer);
13468 if (token->type != CPP_NAME
13469 && token->type != CPP_KEYWORD)
13470 return error_mark_node;
13471 /* Consume the token. */
13472 token = cp_lexer_consume_token (parser->lexer);
13474 /* Save away the identifier that indicates which attribute this is. */
13475 identifier = token->value;
13476 attribute = build_tree_list (identifier, NULL_TREE);
13478 /* Peek at the next token. */
13479 token = cp_lexer_peek_token (parser->lexer);
13480 /* If it's an `(', then parse the attribute arguments. */
13481 if (token->type == CPP_OPEN_PAREN)
13483 tree arguments;
13485 arguments = (cp_parser_parenthesized_expression_list
13486 (parser, true, /*non_constant_p=*/NULL));
13487 /* Save the identifier and arguments away. */
13488 TREE_VALUE (attribute) = arguments;
13491 /* Add this attribute to the list. */
13492 TREE_CHAIN (attribute) = attribute_list;
13493 attribute_list = attribute;
13495 /* Now, look for more attributes. */
13496 token = cp_lexer_peek_token (parser->lexer);
13497 /* If the next token isn't a `,', we're done. */
13498 if (token->type != CPP_COMMA)
13499 break;
13501 /* Consume the comma and keep going. */
13502 cp_lexer_consume_token (parser->lexer);
13504 c_lex_string_translate = true;
13506 /* We built up the list in reverse order. */
13507 return nreverse (attribute_list);
13510 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13511 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13512 current value of the PEDANTIC flag, regardless of whether or not
13513 the `__extension__' keyword is present. The caller is responsible
13514 for restoring the value of the PEDANTIC flag. */
13516 static bool
13517 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13519 /* Save the old value of the PEDANTIC flag. */
13520 *saved_pedantic = pedantic;
13522 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13524 /* Consume the `__extension__' token. */
13525 cp_lexer_consume_token (parser->lexer);
13526 /* We're not being pedantic while the `__extension__' keyword is
13527 in effect. */
13528 pedantic = 0;
13530 return true;
13533 return false;
13536 /* Parse a label declaration.
13538 label-declaration:
13539 __label__ label-declarator-seq ;
13541 label-declarator-seq:
13542 identifier , label-declarator-seq
13543 identifier */
13545 static void
13546 cp_parser_label_declaration (cp_parser* parser)
13548 /* Look for the `__label__' keyword. */
13549 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13551 while (true)
13553 tree identifier;
13555 /* Look for an identifier. */
13556 identifier = cp_parser_identifier (parser);
13557 /* Declare it as a lobel. */
13558 finish_label_decl (identifier);
13559 /* If the next token is a `;', stop. */
13560 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13561 break;
13562 /* Look for the `,' separating the label declarations. */
13563 cp_parser_require (parser, CPP_COMMA, "`,'");
13566 /* Look for the final `;'. */
13567 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13570 /* Support Functions */
13572 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13573 NAME should have one of the representations used for an
13574 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13575 is returned. If PARSER->SCOPE is a dependent type, then a
13576 SCOPE_REF is returned.
13578 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13579 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13580 was formed. Abstractly, such entities should not be passed to this
13581 function, because they do not need to be looked up, but it is
13582 simpler to check for this special case here, rather than at the
13583 call-sites.
13585 In cases not explicitly covered above, this function returns a
13586 DECL, OVERLOAD, or baselink representing the result of the lookup.
13587 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13588 is returned.
13590 If IS_TYPE is TRUE, bindings that do not refer to types are
13591 ignored.
13593 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13594 ignored.
13596 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13597 are ignored.
13599 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13600 types. */
13602 static tree
13603 cp_parser_lookup_name (cp_parser *parser, tree name,
13604 bool is_type, bool is_template, bool is_namespace,
13605 bool check_dependency)
13607 tree decl;
13608 tree object_type = parser->context->object_type;
13610 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13611 no longer valid. Note that if we are parsing tentatively, and
13612 the parse fails, OBJECT_TYPE will be automatically restored. */
13613 parser->context->object_type = NULL_TREE;
13615 if (name == error_mark_node)
13616 return error_mark_node;
13618 /* A template-id has already been resolved; there is no lookup to
13619 do. */
13620 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13621 return name;
13622 if (BASELINK_P (name))
13624 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13625 == TEMPLATE_ID_EXPR),
13626 20020909);
13627 return name;
13630 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13631 it should already have been checked to make sure that the name
13632 used matches the type being destroyed. */
13633 if (TREE_CODE (name) == BIT_NOT_EXPR)
13635 tree type;
13637 /* Figure out to which type this destructor applies. */
13638 if (parser->scope)
13639 type = parser->scope;
13640 else if (object_type)
13641 type = object_type;
13642 else
13643 type = current_class_type;
13644 /* If that's not a class type, there is no destructor. */
13645 if (!type || !CLASS_TYPE_P (type))
13646 return error_mark_node;
13647 if (!CLASSTYPE_DESTRUCTORS (type))
13648 return error_mark_node;
13649 /* If it was a class type, return the destructor. */
13650 return CLASSTYPE_DESTRUCTORS (type);
13653 /* By this point, the NAME should be an ordinary identifier. If
13654 the id-expression was a qualified name, the qualifying scope is
13655 stored in PARSER->SCOPE at this point. */
13656 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13657 20000619);
13659 /* Perform the lookup. */
13660 if (parser->scope)
13662 bool dependent_p;
13664 if (parser->scope == error_mark_node)
13665 return error_mark_node;
13667 /* If the SCOPE is dependent, the lookup must be deferred until
13668 the template is instantiated -- unless we are explicitly
13669 looking up names in uninstantiated templates. Even then, we
13670 cannot look up the name if the scope is not a class type; it
13671 might, for example, be a template type parameter. */
13672 dependent_p = (TYPE_P (parser->scope)
13673 && !(parser->in_declarator_p
13674 && currently_open_class (parser->scope))
13675 && dependent_type_p (parser->scope));
13676 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13677 && dependent_p)
13679 if (is_type)
13680 /* The resolution to Core Issue 180 says that `struct A::B'
13681 should be considered a type-name, even if `A' is
13682 dependent. */
13683 decl = TYPE_NAME (make_typename_type (parser->scope,
13684 name,
13685 /*complain=*/1));
13686 else if (is_template)
13687 decl = make_unbound_class_template (parser->scope,
13688 name,
13689 /*complain=*/1);
13690 else
13691 decl = build_nt (SCOPE_REF, parser->scope, name);
13693 else
13695 bool pop_p = false;
13697 /* If PARSER->SCOPE is a dependent type, then it must be a
13698 class type, and we must not be checking dependencies;
13699 otherwise, we would have processed this lookup above. So
13700 that PARSER->SCOPE is not considered a dependent base by
13701 lookup_member, we must enter the scope here. */
13702 if (dependent_p)
13703 pop_p = push_scope (parser->scope);
13704 /* If the PARSER->SCOPE is a a template specialization, it
13705 may be instantiated during name lookup. In that case,
13706 errors may be issued. Even if we rollback the current
13707 tentative parse, those errors are valid. */
13708 decl = lookup_qualified_name (parser->scope, name, is_type,
13709 /*complain=*/true);
13710 if (pop_p)
13711 pop_scope (parser->scope);
13713 parser->qualifying_scope = parser->scope;
13714 parser->object_scope = NULL_TREE;
13716 else if (object_type)
13718 tree object_decl = NULL_TREE;
13719 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13720 OBJECT_TYPE is not a class. */
13721 if (CLASS_TYPE_P (object_type))
13722 /* If the OBJECT_TYPE is a template specialization, it may
13723 be instantiated during name lookup. In that case, errors
13724 may be issued. Even if we rollback the current tentative
13725 parse, those errors are valid. */
13726 object_decl = lookup_member (object_type,
13727 name,
13728 /*protect=*/0, is_type);
13729 /* Look it up in the enclosing context, too. */
13730 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13731 is_namespace,
13732 /*flags=*/0);
13733 parser->object_scope = object_type;
13734 parser->qualifying_scope = NULL_TREE;
13735 if (object_decl)
13736 decl = object_decl;
13738 else
13740 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13741 is_namespace,
13742 /*flags=*/0);
13743 parser->qualifying_scope = NULL_TREE;
13744 parser->object_scope = NULL_TREE;
13747 /* If the lookup failed, let our caller know. */
13748 if (!decl
13749 || decl == error_mark_node
13750 || (TREE_CODE (decl) == FUNCTION_DECL
13751 && DECL_ANTICIPATED (decl)))
13752 return error_mark_node;
13754 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13755 if (TREE_CODE (decl) == TREE_LIST)
13757 /* The error message we have to print is too complicated for
13758 cp_parser_error, so we incorporate its actions directly. */
13759 if (!cp_parser_simulate_error (parser))
13761 error ("reference to `%D' is ambiguous", name);
13762 print_candidates (decl);
13764 return error_mark_node;
13767 my_friendly_assert (DECL_P (decl)
13768 || TREE_CODE (decl) == OVERLOAD
13769 || TREE_CODE (decl) == SCOPE_REF
13770 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13771 || BASELINK_P (decl),
13772 20000619);
13774 /* If we have resolved the name of a member declaration, check to
13775 see if the declaration is accessible. When the name resolves to
13776 set of overloaded functions, accessibility is checked when
13777 overload resolution is done.
13779 During an explicit instantiation, access is not checked at all,
13780 as per [temp.explicit]. */
13781 if (DECL_P (decl))
13782 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13784 return decl;
13787 /* Like cp_parser_lookup_name, but for use in the typical case where
13788 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13789 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
13791 static tree
13792 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13794 return cp_parser_lookup_name (parser, name,
13795 /*is_type=*/false,
13796 /*is_template=*/false,
13797 /*is_namespace=*/false,
13798 /*check_dependency=*/true);
13801 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13802 the current context, return the TYPE_DECL. If TAG_NAME_P is
13803 true, the DECL indicates the class being defined in a class-head,
13804 or declared in an elaborated-type-specifier.
13806 Otherwise, return DECL. */
13808 static tree
13809 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13811 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13812 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13814 struct A {
13815 template <typename T> struct B;
13818 template <typename T> struct A::B {};
13820 Similarly, in a elaborated-type-specifier:
13822 namespace N { struct X{}; }
13824 struct A {
13825 template <typename T> friend struct N::X;
13828 However, if the DECL refers to a class type, and we are in
13829 the scope of the class, then the name lookup automatically
13830 finds the TYPE_DECL created by build_self_reference rather
13831 than a TEMPLATE_DECL. For example, in:
13833 template <class T> struct S {
13834 S s;
13837 there is no need to handle such case. */
13839 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13840 return DECL_TEMPLATE_RESULT (decl);
13842 return decl;
13845 /* If too many, or too few, template-parameter lists apply to the
13846 declarator, issue an error message. Returns TRUE if all went well,
13847 and FALSE otherwise. */
13849 static bool
13850 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13851 tree declarator)
13853 unsigned num_templates;
13855 /* We haven't seen any classes that involve template parameters yet. */
13856 num_templates = 0;
13858 switch (TREE_CODE (declarator))
13860 case CALL_EXPR:
13861 case ARRAY_REF:
13862 case INDIRECT_REF:
13863 case ADDR_EXPR:
13865 tree main_declarator = TREE_OPERAND (declarator, 0);
13866 return
13867 cp_parser_check_declarator_template_parameters (parser,
13868 main_declarator);
13871 case SCOPE_REF:
13873 tree scope;
13874 tree member;
13876 scope = TREE_OPERAND (declarator, 0);
13877 member = TREE_OPERAND (declarator, 1);
13879 /* If this is a pointer-to-member, then we are not interested
13880 in the SCOPE, because it does not qualify the thing that is
13881 being declared. */
13882 if (TREE_CODE (member) == INDIRECT_REF)
13883 return (cp_parser_check_declarator_template_parameters
13884 (parser, member));
13886 while (scope && CLASS_TYPE_P (scope))
13888 /* You're supposed to have one `template <...>'
13889 for every template class, but you don't need one
13890 for a full specialization. For example:
13892 template <class T> struct S{};
13893 template <> struct S<int> { void f(); };
13894 void S<int>::f () {}
13896 is correct; there shouldn't be a `template <>' for
13897 the definition of `S<int>::f'. */
13898 if (CLASSTYPE_TEMPLATE_INFO (scope)
13899 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13900 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13901 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13902 ++num_templates;
13904 scope = TYPE_CONTEXT (scope);
13908 /* Fall through. */
13910 default:
13911 /* If the DECLARATOR has the form `X<y>' then it uses one
13912 additional level of template parameters. */
13913 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13914 ++num_templates;
13916 return cp_parser_check_template_parameters (parser,
13917 num_templates);
13921 /* NUM_TEMPLATES were used in the current declaration. If that is
13922 invalid, return FALSE and issue an error messages. Otherwise,
13923 return TRUE. */
13925 static bool
13926 cp_parser_check_template_parameters (cp_parser* parser,
13927 unsigned num_templates)
13929 /* If there are more template classes than parameter lists, we have
13930 something like:
13932 template <class T> void S<T>::R<T>::f (); */
13933 if (parser->num_template_parameter_lists < num_templates)
13935 error ("too few template-parameter-lists");
13936 return false;
13938 /* If there are the same number of template classes and parameter
13939 lists, that's OK. */
13940 if (parser->num_template_parameter_lists == num_templates)
13941 return true;
13942 /* If there are more, but only one more, then we are referring to a
13943 member template. That's OK too. */
13944 if (parser->num_template_parameter_lists == num_templates + 1)
13945 return true;
13946 /* Otherwise, there are too many template parameter lists. We have
13947 something like:
13949 template <class T> template <class U> void S::f(); */
13950 error ("too many template-parameter-lists");
13951 return false;
13954 /* Parse a binary-expression of the general form:
13956 binary-expression:
13957 <expr>
13958 binary-expression <token> <expr>
13960 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13961 to parser the <expr>s. If the first production is used, then the
13962 value returned by FN is returned directly. Otherwise, a node with
13963 the indicated EXPR_TYPE is returned, with operands corresponding to
13964 the two sub-expressions. */
13966 static tree
13967 cp_parser_binary_expression (cp_parser* parser,
13968 const cp_parser_token_tree_map token_tree_map,
13969 cp_parser_expression_fn fn)
13971 tree lhs;
13973 /* Parse the first expression. */
13974 lhs = (*fn) (parser);
13975 /* Now, look for more expressions. */
13976 while (true)
13978 cp_token *token;
13979 const cp_parser_token_tree_map_node *map_node;
13980 tree rhs;
13982 /* Peek at the next token. */
13983 token = cp_lexer_peek_token (parser->lexer);
13984 /* If the token is `>', and that's not an operator at the
13985 moment, then we're done. */
13986 if (token->type == CPP_GREATER
13987 && !parser->greater_than_is_operator_p)
13988 break;
13989 /* If we find one of the tokens we want, build the corresponding
13990 tree representation. */
13991 for (map_node = token_tree_map;
13992 map_node->token_type != CPP_EOF;
13993 ++map_node)
13994 if (map_node->token_type == token->type)
13996 /* Assume that an overloaded operator will not be used. */
13997 bool overloaded_p = false;
13999 /* Consume the operator token. */
14000 cp_lexer_consume_token (parser->lexer);
14001 /* Parse the right-hand side of the expression. */
14002 rhs = (*fn) (parser);
14003 /* Build the binary tree node. */
14004 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14005 &overloaded_p);
14006 /* If the binary operator required the use of an
14007 overloaded operator, then this expression cannot be an
14008 integral constant-expression. An overloaded operator
14009 can be used even if both operands are otherwise
14010 permissible in an integral constant-expression if at
14011 least one of the operands is of enumeration type. */
14012 if (overloaded_p
14013 && (cp_parser_non_integral_constant_expression
14014 (parser, "calls to overloaded operators")))
14015 lhs = error_mark_node;
14016 break;
14019 /* If the token wasn't one of the ones we want, we're done. */
14020 if (map_node->token_type == CPP_EOF)
14021 break;
14024 return lhs;
14027 /* Parse an optional `::' token indicating that the following name is
14028 from the global namespace. If so, PARSER->SCOPE is set to the
14029 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14030 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14031 Returns the new value of PARSER->SCOPE, if the `::' token is
14032 present, and NULL_TREE otherwise. */
14034 static tree
14035 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14037 cp_token *token;
14039 /* Peek at the next token. */
14040 token = cp_lexer_peek_token (parser->lexer);
14041 /* If we're looking at a `::' token then we're starting from the
14042 global namespace, not our current location. */
14043 if (token->type == CPP_SCOPE)
14045 /* Consume the `::' token. */
14046 cp_lexer_consume_token (parser->lexer);
14047 /* Set the SCOPE so that we know where to start the lookup. */
14048 parser->scope = global_namespace;
14049 parser->qualifying_scope = global_namespace;
14050 parser->object_scope = NULL_TREE;
14052 return parser->scope;
14054 else if (!current_scope_valid_p)
14056 parser->scope = NULL_TREE;
14057 parser->qualifying_scope = NULL_TREE;
14058 parser->object_scope = NULL_TREE;
14061 return NULL_TREE;
14064 /* Returns TRUE if the upcoming token sequence is the start of a
14065 constructor declarator. If FRIEND_P is true, the declarator is
14066 preceded by the `friend' specifier. */
14068 static bool
14069 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14071 bool constructor_p;
14072 tree type_decl = NULL_TREE;
14073 bool nested_name_p;
14074 cp_token *next_token;
14076 /* The common case is that this is not a constructor declarator, so
14077 try to avoid doing lots of work if at all possible. It's not
14078 valid declare a constructor at function scope. */
14079 if (at_function_scope_p ())
14080 return false;
14081 /* And only certain tokens can begin a constructor declarator. */
14082 next_token = cp_lexer_peek_token (parser->lexer);
14083 if (next_token->type != CPP_NAME
14084 && next_token->type != CPP_SCOPE
14085 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14086 && next_token->type != CPP_TEMPLATE_ID)
14087 return false;
14089 /* Parse tentatively; we are going to roll back all of the tokens
14090 consumed here. */
14091 cp_parser_parse_tentatively (parser);
14092 /* Assume that we are looking at a constructor declarator. */
14093 constructor_p = true;
14095 /* Look for the optional `::' operator. */
14096 cp_parser_global_scope_opt (parser,
14097 /*current_scope_valid_p=*/false);
14098 /* Look for the nested-name-specifier. */
14099 nested_name_p
14100 = (cp_parser_nested_name_specifier_opt (parser,
14101 /*typename_keyword_p=*/false,
14102 /*check_dependency_p=*/false,
14103 /*type_p=*/false,
14104 /*is_declaration=*/false)
14105 != NULL_TREE);
14106 /* Outside of a class-specifier, there must be a
14107 nested-name-specifier. */
14108 if (!nested_name_p &&
14109 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14110 || friend_p))
14111 constructor_p = false;
14112 /* If we still think that this might be a constructor-declarator,
14113 look for a class-name. */
14114 if (constructor_p)
14116 /* If we have:
14118 template <typename T> struct S { S(); };
14119 template <typename T> S<T>::S ();
14121 we must recognize that the nested `S' names a class.
14122 Similarly, for:
14124 template <typename T> S<T>::S<T> ();
14126 we must recognize that the nested `S' names a template. */
14127 type_decl = cp_parser_class_name (parser,
14128 /*typename_keyword_p=*/false,
14129 /*template_keyword_p=*/false,
14130 /*type_p=*/false,
14131 /*check_dependency_p=*/false,
14132 /*class_head_p=*/false,
14133 /*is_declaration=*/false);
14134 /* If there was no class-name, then this is not a constructor. */
14135 constructor_p = !cp_parser_error_occurred (parser);
14138 /* If we're still considering a constructor, we have to see a `(',
14139 to begin the parameter-declaration-clause, followed by either a
14140 `)', an `...', or a decl-specifier. We need to check for a
14141 type-specifier to avoid being fooled into thinking that:
14143 S::S (f) (int);
14145 is a constructor. (It is actually a function named `f' that
14146 takes one parameter (of type `int') and returns a value of type
14147 `S::S'. */
14148 if (constructor_p
14149 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14152 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14153 && !cp_parser_storage_class_specifier_opt (parser))
14155 tree type;
14156 bool pop_p = false;
14157 unsigned saved_num_template_parameter_lists;
14159 /* Names appearing in the type-specifier should be looked up
14160 in the scope of the class. */
14161 if (current_class_type)
14162 type = NULL_TREE;
14163 else
14165 type = TREE_TYPE (type_decl);
14166 if (TREE_CODE (type) == TYPENAME_TYPE)
14168 type = resolve_typename_type (type,
14169 /*only_current_p=*/false);
14170 if (type == error_mark_node)
14172 cp_parser_abort_tentative_parse (parser);
14173 return false;
14176 pop_p = push_scope (type);
14179 /* Inside the constructor parameter list, surrounding
14180 template-parameter-lists do not apply. */
14181 saved_num_template_parameter_lists
14182 = parser->num_template_parameter_lists;
14183 parser->num_template_parameter_lists = 0;
14185 /* Look for the type-specifier. */
14186 cp_parser_type_specifier (parser,
14187 CP_PARSER_FLAGS_NONE,
14188 /*is_friend=*/false,
14189 /*is_declarator=*/true,
14190 /*declares_class_or_enum=*/NULL,
14191 /*is_cv_qualifier=*/NULL);
14193 parser->num_template_parameter_lists
14194 = saved_num_template_parameter_lists;
14196 /* Leave the scope of the class. */
14197 if (pop_p)
14198 pop_scope (type);
14200 constructor_p = !cp_parser_error_occurred (parser);
14203 else
14204 constructor_p = false;
14205 /* We did not really want to consume any tokens. */
14206 cp_parser_abort_tentative_parse (parser);
14208 return constructor_p;
14211 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14212 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
14213 they must be performed once we are in the scope of the function.
14215 Returns the function defined. */
14217 static tree
14218 cp_parser_function_definition_from_specifiers_and_declarator
14219 (cp_parser* parser,
14220 tree decl_specifiers,
14221 tree attributes,
14222 tree declarator)
14224 tree fn;
14225 bool success_p;
14227 /* Begin the function-definition. */
14228 success_p = begin_function_definition (decl_specifiers,
14229 attributes,
14230 declarator);
14232 /* If there were names looked up in the decl-specifier-seq that we
14233 did not check, check them now. We must wait until we are in the
14234 scope of the function to perform the checks, since the function
14235 might be a friend. */
14236 perform_deferred_access_checks ();
14238 if (!success_p)
14240 /* If begin_function_definition didn't like the definition, skip
14241 the entire function. */
14242 error ("invalid function declaration");
14243 cp_parser_skip_to_end_of_block_or_statement (parser);
14244 fn = error_mark_node;
14246 else
14247 fn = cp_parser_function_definition_after_declarator (parser,
14248 /*inline_p=*/false);
14250 return fn;
14253 /* Parse the part of a function-definition that follows the
14254 declarator. INLINE_P is TRUE iff this function is an inline
14255 function defined with a class-specifier.
14257 Returns the function defined. */
14259 static tree
14260 cp_parser_function_definition_after_declarator (cp_parser* parser,
14261 bool inline_p)
14263 tree fn;
14264 bool ctor_initializer_p = false;
14265 bool saved_in_unbraced_linkage_specification_p;
14266 unsigned saved_num_template_parameter_lists;
14268 /* If the next token is `return', then the code may be trying to
14269 make use of the "named return value" extension that G++ used to
14270 support. */
14271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14273 /* Consume the `return' keyword. */
14274 cp_lexer_consume_token (parser->lexer);
14275 /* Look for the identifier that indicates what value is to be
14276 returned. */
14277 cp_parser_identifier (parser);
14278 /* Issue an error message. */
14279 error ("named return values are no longer supported");
14280 /* Skip tokens until we reach the start of the function body. */
14281 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14282 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14283 cp_lexer_consume_token (parser->lexer);
14285 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14286 anything declared inside `f'. */
14287 saved_in_unbraced_linkage_specification_p
14288 = parser->in_unbraced_linkage_specification_p;
14289 parser->in_unbraced_linkage_specification_p = false;
14290 /* Inside the function, surrounding template-parameter-lists do not
14291 apply. */
14292 saved_num_template_parameter_lists
14293 = parser->num_template_parameter_lists;
14294 parser->num_template_parameter_lists = 0;
14295 /* If the next token is `try', then we are looking at a
14296 function-try-block. */
14297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14298 ctor_initializer_p = cp_parser_function_try_block (parser);
14299 /* A function-try-block includes the function-body, so we only do
14300 this next part if we're not processing a function-try-block. */
14301 else
14302 ctor_initializer_p
14303 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14305 /* Finish the function. */
14306 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14307 (inline_p ? 2 : 0));
14308 /* Generate code for it, if necessary. */
14309 expand_or_defer_fn (fn);
14310 /* Restore the saved values. */
14311 parser->in_unbraced_linkage_specification_p
14312 = saved_in_unbraced_linkage_specification_p;
14313 parser->num_template_parameter_lists
14314 = saved_num_template_parameter_lists;
14316 return fn;
14319 /* Parse a template-declaration, assuming that the `export' (and
14320 `extern') keywords, if present, has already been scanned. MEMBER_P
14321 is as for cp_parser_template_declaration. */
14323 static void
14324 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14326 tree decl = NULL_TREE;
14327 tree parameter_list;
14328 bool friend_p = false;
14330 /* Look for the `template' keyword. */
14331 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14332 return;
14334 /* And the `<'. */
14335 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14336 return;
14338 /* If the next token is `>', then we have an invalid
14339 specialization. Rather than complain about an invalid template
14340 parameter, issue an error message here. */
14341 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14343 cp_parser_error (parser, "invalid explicit specialization");
14344 begin_specialization ();
14345 parameter_list = NULL_TREE;
14347 else
14349 /* Parse the template parameters. */
14350 begin_template_parm_list ();
14351 parameter_list = cp_parser_template_parameter_list (parser);
14352 parameter_list = end_template_parm_list (parameter_list);
14355 /* Look for the `>'. */
14356 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14357 /* We just processed one more parameter list. */
14358 ++parser->num_template_parameter_lists;
14359 /* If the next token is `template', there are more template
14360 parameters. */
14361 if (cp_lexer_next_token_is_keyword (parser->lexer,
14362 RID_TEMPLATE))
14363 cp_parser_template_declaration_after_export (parser, member_p);
14364 else
14366 decl = cp_parser_single_declaration (parser,
14367 member_p,
14368 &friend_p);
14370 /* If this is a member template declaration, let the front
14371 end know. */
14372 if (member_p && !friend_p && decl)
14374 if (TREE_CODE (decl) == TYPE_DECL)
14375 cp_parser_check_access_in_redeclaration (decl);
14377 decl = finish_member_template_decl (decl);
14379 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14380 make_friend_class (current_class_type, TREE_TYPE (decl),
14381 /*complain=*/true);
14383 /* We are done with the current parameter list. */
14384 --parser->num_template_parameter_lists;
14386 /* Finish up. */
14387 finish_template_decl (parameter_list);
14389 /* Register member declarations. */
14390 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14391 finish_member_declaration (decl);
14393 /* If DECL is a function template, we must return to parse it later.
14394 (Even though there is no definition, there might be default
14395 arguments that need handling.) */
14396 if (member_p && decl
14397 && (TREE_CODE (decl) == FUNCTION_DECL
14398 || DECL_FUNCTION_TEMPLATE_P (decl)))
14399 TREE_VALUE (parser->unparsed_functions_queues)
14400 = tree_cons (NULL_TREE, decl,
14401 TREE_VALUE (parser->unparsed_functions_queues));
14404 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14405 `function-definition' sequence. MEMBER_P is true, this declaration
14406 appears in a class scope.
14408 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14409 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14411 static tree
14412 cp_parser_single_declaration (cp_parser* parser,
14413 bool member_p,
14414 bool* friend_p)
14416 int declares_class_or_enum;
14417 tree decl = NULL_TREE;
14418 tree decl_specifiers;
14419 tree attributes;
14420 bool function_definition_p = false;
14422 /* Defer access checks until we know what is being declared. */
14423 push_deferring_access_checks (dk_deferred);
14425 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14426 alternative. */
14427 decl_specifiers
14428 = cp_parser_decl_specifier_seq (parser,
14429 CP_PARSER_FLAGS_OPTIONAL,
14430 &attributes,
14431 &declares_class_or_enum);
14432 if (friend_p)
14433 *friend_p = cp_parser_friend_p (decl_specifiers);
14434 /* Gather up the access checks that occurred the
14435 decl-specifier-seq. */
14436 stop_deferring_access_checks ();
14438 /* Check for the declaration of a template class. */
14439 if (declares_class_or_enum)
14441 if (cp_parser_declares_only_class_p (parser))
14443 decl = shadow_tag (decl_specifiers);
14444 if (decl)
14445 decl = TYPE_NAME (decl);
14446 else
14447 decl = error_mark_node;
14450 else
14451 decl = NULL_TREE;
14452 /* If it's not a template class, try for a template function. If
14453 the next token is a `;', then this declaration does not declare
14454 anything. But, if there were errors in the decl-specifiers, then
14455 the error might well have come from an attempted class-specifier.
14456 In that case, there's no need to warn about a missing declarator. */
14457 if (!decl
14458 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14459 || !value_member (error_mark_node, decl_specifiers)))
14460 decl = cp_parser_init_declarator (parser,
14461 decl_specifiers,
14462 attributes,
14463 /*function_definition_allowed_p=*/true,
14464 member_p,
14465 declares_class_or_enum,
14466 &function_definition_p);
14468 pop_deferring_access_checks ();
14470 /* Clear any current qualification; whatever comes next is the start
14471 of something new. */
14472 parser->scope = NULL_TREE;
14473 parser->qualifying_scope = NULL_TREE;
14474 parser->object_scope = NULL_TREE;
14475 /* Look for a trailing `;' after the declaration. */
14476 if (!function_definition_p
14477 && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14478 cp_parser_skip_to_end_of_block_or_statement (parser);
14480 return decl;
14483 /* Parse a cast-expression that is not the operand of a unary "&". */
14485 static tree
14486 cp_parser_simple_cast_expression (cp_parser *parser)
14488 return cp_parser_cast_expression (parser, /*address_p=*/false);
14491 /* Parse a functional cast to TYPE. Returns an expression
14492 representing the cast. */
14494 static tree
14495 cp_parser_functional_cast (cp_parser* parser, tree type)
14497 tree expression_list;
14498 tree cast;
14500 expression_list
14501 = cp_parser_parenthesized_expression_list (parser, false,
14502 /*non_constant_p=*/NULL);
14504 cast = build_functional_cast (type, expression_list);
14505 /* [expr.const]/1: In an integral constant expression "only type
14506 conversions to integral or enumeration type can be used". */
14507 if (cast != error_mark_node && !type_dependent_expression_p (type)
14508 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14510 if (cp_parser_non_integral_constant_expression
14511 (parser, "a call to a constructor"))
14512 return error_mark_node;
14514 return cast;
14517 /* Save the tokens that make up the body of a member function defined
14518 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
14519 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
14520 specifiers applied to the declaration. Returns the FUNCTION_DECL
14521 for the member function. */
14523 static tree
14524 cp_parser_save_member_function_body (cp_parser* parser,
14525 tree decl_specifiers,
14526 tree declarator,
14527 tree attributes)
14529 cp_token_cache *cache;
14530 tree fn;
14532 /* Create the function-declaration. */
14533 fn = start_method (decl_specifiers, declarator, attributes);
14534 /* If something went badly wrong, bail out now. */
14535 if (fn == error_mark_node)
14537 /* If there's a function-body, skip it. */
14538 if (cp_parser_token_starts_function_definition_p
14539 (cp_lexer_peek_token (parser->lexer)))
14540 cp_parser_skip_to_end_of_block_or_statement (parser);
14541 return error_mark_node;
14544 /* Remember it, if there default args to post process. */
14545 cp_parser_save_default_args (parser, fn);
14547 /* Create a token cache. */
14548 cache = cp_token_cache_new ();
14549 /* Save away the tokens that make up the body of the
14550 function. */
14551 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14552 /* Handle function try blocks. */
14553 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14554 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14556 /* Save away the inline definition; we will process it when the
14557 class is complete. */
14558 DECL_PENDING_INLINE_INFO (fn) = cache;
14559 DECL_PENDING_INLINE_P (fn) = 1;
14561 /* We need to know that this was defined in the class, so that
14562 friend templates are handled correctly. */
14563 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14565 /* We're done with the inline definition. */
14566 finish_method (fn);
14568 /* Add FN to the queue of functions to be parsed later. */
14569 TREE_VALUE (parser->unparsed_functions_queues)
14570 = tree_cons (NULL_TREE, fn,
14571 TREE_VALUE (parser->unparsed_functions_queues));
14573 return fn;
14576 /* Parse a template-argument-list, as well as the trailing ">" (but
14577 not the opening ">"). See cp_parser_template_argument_list for the
14578 return value. */
14580 static tree
14581 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14583 tree arguments;
14584 tree saved_scope;
14585 tree saved_qualifying_scope;
14586 tree saved_object_scope;
14587 bool saved_greater_than_is_operator_p;
14589 /* [temp.names]
14591 When parsing a template-id, the first non-nested `>' is taken as
14592 the end of the template-argument-list rather than a greater-than
14593 operator. */
14594 saved_greater_than_is_operator_p
14595 = parser->greater_than_is_operator_p;
14596 parser->greater_than_is_operator_p = false;
14597 /* Parsing the argument list may modify SCOPE, so we save it
14598 here. */
14599 saved_scope = parser->scope;
14600 saved_qualifying_scope = parser->qualifying_scope;
14601 saved_object_scope = parser->object_scope;
14602 /* Parse the template-argument-list itself. */
14603 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14604 arguments = NULL_TREE;
14605 else
14606 arguments = cp_parser_template_argument_list (parser);
14607 /* Look for the `>' that ends the template-argument-list. If we find
14608 a '>>' instead, it's probably just a typo. */
14609 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14611 if (!saved_greater_than_is_operator_p)
14613 /* If we're in a nested template argument list, the '>>' has to be
14614 a typo for '> >'. We emit the error message, but we continue
14615 parsing and we push a '>' as next token, so that the argument
14616 list will be parsed correctly.. */
14617 cp_token* token;
14618 error ("`>>' should be `> >' within a nested template argument list");
14619 token = cp_lexer_peek_token (parser->lexer);
14620 token->type = CPP_GREATER;
14622 else
14624 /* If this is not a nested template argument list, the '>>' is
14625 a typo for '>'. Emit an error message and continue. */
14626 error ("spurious `>>', use `>' to terminate a template argument list");
14627 cp_lexer_consume_token (parser->lexer);
14630 else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14631 error ("missing `>' to terminate the template argument list");
14632 /* The `>' token might be a greater-than operator again now. */
14633 parser->greater_than_is_operator_p
14634 = saved_greater_than_is_operator_p;
14635 /* Restore the SAVED_SCOPE. */
14636 parser->scope = saved_scope;
14637 parser->qualifying_scope = saved_qualifying_scope;
14638 parser->object_scope = saved_object_scope;
14640 return arguments;
14643 /* MEMBER_FUNCTION is a member function, or a friend. If default
14644 arguments, or the body of the function have not yet been parsed,
14645 parse them now. */
14647 static void
14648 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14650 cp_lexer *saved_lexer;
14652 /* If this member is a template, get the underlying
14653 FUNCTION_DECL. */
14654 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14655 member_function = DECL_TEMPLATE_RESULT (member_function);
14657 /* There should not be any class definitions in progress at this
14658 point; the bodies of members are only parsed outside of all class
14659 definitions. */
14660 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14661 /* While we're parsing the member functions we might encounter more
14662 classes. We want to handle them right away, but we don't want
14663 them getting mixed up with functions that are currently in the
14664 queue. */
14665 parser->unparsed_functions_queues
14666 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14668 /* Make sure that any template parameters are in scope. */
14669 maybe_begin_member_template_processing (member_function);
14671 /* If the body of the function has not yet been parsed, parse it
14672 now. */
14673 if (DECL_PENDING_INLINE_P (member_function))
14675 tree function_scope;
14676 cp_token_cache *tokens;
14678 /* The function is no longer pending; we are processing it. */
14679 tokens = DECL_PENDING_INLINE_INFO (member_function);
14680 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14681 DECL_PENDING_INLINE_P (member_function) = 0;
14682 /* If this was an inline function in a local class, enter the scope
14683 of the containing function. */
14684 function_scope = decl_function_context (member_function);
14685 if (function_scope)
14686 push_function_context_to (function_scope);
14688 /* Save away the current lexer. */
14689 saved_lexer = parser->lexer;
14690 /* Make a new lexer to feed us the tokens saved for this function. */
14691 parser->lexer = cp_lexer_new_from_tokens (tokens);
14692 parser->lexer->next = saved_lexer;
14694 /* Set the current source position to be the location of the first
14695 token in the saved inline body. */
14696 cp_lexer_peek_token (parser->lexer);
14698 /* Let the front end know that we going to be defining this
14699 function. */
14700 start_function (NULL_TREE, member_function, NULL_TREE,
14701 SF_PRE_PARSED | SF_INCLASS_INLINE);
14703 /* Now, parse the body of the function. */
14704 cp_parser_function_definition_after_declarator (parser,
14705 /*inline_p=*/true);
14707 /* Leave the scope of the containing function. */
14708 if (function_scope)
14709 pop_function_context_from (function_scope);
14710 /* Restore the lexer. */
14711 parser->lexer = saved_lexer;
14714 /* Remove any template parameters from the symbol table. */
14715 maybe_end_member_template_processing ();
14717 /* Restore the queue. */
14718 parser->unparsed_functions_queues
14719 = TREE_CHAIN (parser->unparsed_functions_queues);
14722 /* If DECL contains any default args, remember it on the unparsed
14723 functions queue. */
14725 static void
14726 cp_parser_save_default_args (cp_parser* parser, tree decl)
14728 tree probe;
14730 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14731 probe;
14732 probe = TREE_CHAIN (probe))
14733 if (TREE_PURPOSE (probe))
14735 TREE_PURPOSE (parser->unparsed_functions_queues)
14736 = tree_cons (NULL_TREE, decl,
14737 TREE_PURPOSE (parser->unparsed_functions_queues));
14738 break;
14740 return;
14743 /* FN is a FUNCTION_DECL which may contains a parameter with an
14744 unparsed DEFAULT_ARG. Parse the default args now. */
14746 static void
14747 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14749 cp_lexer *saved_lexer;
14750 cp_token_cache *tokens;
14751 bool saved_local_variables_forbidden_p;
14752 tree parameters;
14754 /* While we're parsing the default args, we might (due to the
14755 statement expression extension) encounter more classes. We want
14756 to handle them right away, but we don't want them getting mixed
14757 up with default args that are currently in the queue. */
14758 parser->unparsed_functions_queues
14759 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14761 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14762 parameters;
14763 parameters = TREE_CHAIN (parameters))
14765 if (!TREE_PURPOSE (parameters)
14766 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14767 continue;
14769 /* Save away the current lexer. */
14770 saved_lexer = parser->lexer;
14771 /* Create a new one, using the tokens we have saved. */
14772 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14773 parser->lexer = cp_lexer_new_from_tokens (tokens);
14775 /* Set the current source position to be the location of the
14776 first token in the default argument. */
14777 cp_lexer_peek_token (parser->lexer);
14779 /* Local variable names (and the `this' keyword) may not appear
14780 in a default argument. */
14781 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14782 parser->local_variables_forbidden_p = true;
14783 /* Parse the assignment-expression. */
14784 if (DECL_CLASS_SCOPE_P (fn))
14785 push_nested_class (DECL_CONTEXT (fn));
14786 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14787 if (DECL_CLASS_SCOPE_P (fn))
14788 pop_nested_class ();
14790 /* If the token stream has not been completely used up, then
14791 there was extra junk after the end of the default
14792 argument. */
14793 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14794 cp_parser_error (parser, "expected `,'");
14796 /* Restore saved state. */
14797 parser->lexer = saved_lexer;
14798 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14801 /* Restore the queue. */
14802 parser->unparsed_functions_queues
14803 = TREE_CHAIN (parser->unparsed_functions_queues);
14806 /* Parse the operand of `sizeof' (or a similar operator). Returns
14807 either a TYPE or an expression, depending on the form of the
14808 input. The KEYWORD indicates which kind of expression we have
14809 encountered. */
14811 static tree
14812 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14814 static const char *format;
14815 tree expr = NULL_TREE;
14816 const char *saved_message;
14817 bool saved_integral_constant_expression_p;
14819 /* Initialize FORMAT the first time we get here. */
14820 if (!format)
14821 format = "types may not be defined in `%s' expressions";
14823 /* Types cannot be defined in a `sizeof' expression. Save away the
14824 old message. */
14825 saved_message = parser->type_definition_forbidden_message;
14826 /* And create the new one. */
14827 parser->type_definition_forbidden_message
14828 = xmalloc (strlen (format)
14829 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14830 + 1 /* `\0' */);
14831 sprintf ((char *) parser->type_definition_forbidden_message,
14832 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14834 /* The restrictions on constant-expressions do not apply inside
14835 sizeof expressions. */
14836 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14837 parser->integral_constant_expression_p = false;
14839 /* Do not actually evaluate the expression. */
14840 ++skip_evaluation;
14841 /* If it's a `(', then we might be looking at the type-id
14842 construction. */
14843 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14845 tree type;
14846 bool saved_in_type_id_in_expr_p;
14848 /* We can't be sure yet whether we're looking at a type-id or an
14849 expression. */
14850 cp_parser_parse_tentatively (parser);
14851 /* Consume the `('. */
14852 cp_lexer_consume_token (parser->lexer);
14853 /* Parse the type-id. */
14854 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14855 parser->in_type_id_in_expr_p = true;
14856 type = cp_parser_type_id (parser);
14857 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14858 /* Now, look for the trailing `)'. */
14859 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14860 /* If all went well, then we're done. */
14861 if (cp_parser_parse_definitely (parser))
14863 /* Build a list of decl-specifiers; right now, we have only
14864 a single type-specifier. */
14865 type = build_tree_list (NULL_TREE,
14866 type);
14868 /* Call grokdeclarator to figure out what type this is. */
14869 expr = grokdeclarator (NULL_TREE,
14870 type,
14871 TYPENAME,
14872 /*initialized=*/0,
14873 /*attrlist=*/NULL);
14877 /* If the type-id production did not work out, then we must be
14878 looking at the unary-expression production. */
14879 if (!expr)
14880 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14881 /* Go back to evaluating expressions. */
14882 --skip_evaluation;
14884 /* Free the message we created. */
14885 free ((char *) parser->type_definition_forbidden_message);
14886 /* And restore the old one. */
14887 parser->type_definition_forbidden_message = saved_message;
14888 parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14890 return expr;
14893 /* If the current declaration has no declarator, return true. */
14895 static bool
14896 cp_parser_declares_only_class_p (cp_parser *parser)
14898 /* If the next token is a `;' or a `,' then there is no
14899 declarator. */
14900 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14901 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14904 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14905 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14907 static bool
14908 cp_parser_friend_p (tree decl_specifiers)
14910 while (decl_specifiers)
14912 /* See if this decl-specifier is `friend'. */
14913 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14914 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14915 return true;
14917 /* Go on to the next decl-specifier. */
14918 decl_specifiers = TREE_CHAIN (decl_specifiers);
14921 return false;
14924 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14925 issue an error message indicating that TOKEN_DESC was expected.
14927 Returns the token consumed, if the token had the appropriate type.
14928 Otherwise, returns NULL. */
14930 static cp_token *
14931 cp_parser_require (cp_parser* parser,
14932 enum cpp_ttype type,
14933 const char* token_desc)
14935 if (cp_lexer_next_token_is (parser->lexer, type))
14936 return cp_lexer_consume_token (parser->lexer);
14937 else
14939 /* Output the MESSAGE -- unless we're parsing tentatively. */
14940 if (!cp_parser_simulate_error (parser))
14942 char *message = concat ("expected ", token_desc, NULL);
14943 cp_parser_error (parser, message);
14944 free (message);
14946 return NULL;
14950 /* Like cp_parser_require, except that tokens will be skipped until
14951 the desired token is found. An error message is still produced if
14952 the next token is not as expected. */
14954 static void
14955 cp_parser_skip_until_found (cp_parser* parser,
14956 enum cpp_ttype type,
14957 const char* token_desc)
14959 cp_token *token;
14960 unsigned nesting_depth = 0;
14962 if (cp_parser_require (parser, type, token_desc))
14963 return;
14965 /* Skip tokens until the desired token is found. */
14966 while (true)
14968 /* Peek at the next token. */
14969 token = cp_lexer_peek_token (parser->lexer);
14970 /* If we've reached the token we want, consume it and
14971 stop. */
14972 if (token->type == type && !nesting_depth)
14974 cp_lexer_consume_token (parser->lexer);
14975 return;
14977 /* If we've run out of tokens, stop. */
14978 if (token->type == CPP_EOF)
14979 return;
14980 if (token->type == CPP_OPEN_BRACE
14981 || token->type == CPP_OPEN_PAREN
14982 || token->type == CPP_OPEN_SQUARE)
14983 ++nesting_depth;
14984 else if (token->type == CPP_CLOSE_BRACE
14985 || token->type == CPP_CLOSE_PAREN
14986 || token->type == CPP_CLOSE_SQUARE)
14988 if (nesting_depth-- == 0)
14989 return;
14991 /* Consume this token. */
14992 cp_lexer_consume_token (parser->lexer);
14996 /* If the next token is the indicated keyword, consume it. Otherwise,
14997 issue an error message indicating that TOKEN_DESC was expected.
14999 Returns the token consumed, if the token had the appropriate type.
15000 Otherwise, returns NULL. */
15002 static cp_token *
15003 cp_parser_require_keyword (cp_parser* parser,
15004 enum rid keyword,
15005 const char* token_desc)
15007 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15009 if (token && token->keyword != keyword)
15011 dyn_string_t error_msg;
15013 /* Format the error message. */
15014 error_msg = dyn_string_new (0);
15015 dyn_string_append_cstr (error_msg, "expected ");
15016 dyn_string_append_cstr (error_msg, token_desc);
15017 cp_parser_error (parser, error_msg->s);
15018 dyn_string_delete (error_msg);
15019 return NULL;
15022 return token;
15025 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15026 function-definition. */
15028 static bool
15029 cp_parser_token_starts_function_definition_p (cp_token* token)
15031 return (/* An ordinary function-body begins with an `{'. */
15032 token->type == CPP_OPEN_BRACE
15033 /* A ctor-initializer begins with a `:'. */
15034 || token->type == CPP_COLON
15035 /* A function-try-block begins with `try'. */
15036 || token->keyword == RID_TRY
15037 /* The named return value extension begins with `return'. */
15038 || token->keyword == RID_RETURN);
15041 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15042 definition. */
15044 static bool
15045 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15047 cp_token *token;
15049 token = cp_lexer_peek_token (parser->lexer);
15050 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15053 /* Returns TRUE iff the next token is the "," or ">" ending a
15054 template-argument. ">>" is also accepted (after the full
15055 argument was parsed) because it's probably a typo for "> >",
15056 and there is a specific diagnostic for this. */
15058 static bool
15059 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15061 cp_token *token;
15063 token = cp_lexer_peek_token (parser->lexer);
15064 return (token->type == CPP_COMMA || token->type == CPP_GREATER
15065 || token->type == CPP_RSHIFT);
15068 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15069 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
15071 static bool
15072 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15073 size_t n)
15075 cp_token *token;
15077 token = cp_lexer_peek_nth_token (parser->lexer, n);
15078 if (token->type == CPP_LESS)
15079 return true;
15080 /* Check for the sequence `<::' in the original code. It would be lexed as
15081 `[:', where `[' is a digraph, and there is no whitespace before
15082 `:'. */
15083 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15085 cp_token *token2;
15086 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15087 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15088 return true;
15090 return false;
15093 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15094 or none_type otherwise. */
15096 static enum tag_types
15097 cp_parser_token_is_class_key (cp_token* token)
15099 switch (token->keyword)
15101 case RID_CLASS:
15102 return class_type;
15103 case RID_STRUCT:
15104 return record_type;
15105 case RID_UNION:
15106 return union_type;
15108 default:
15109 return none_type;
15113 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
15115 static void
15116 cp_parser_check_class_key (enum tag_types class_key, tree type)
15118 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15119 pedwarn ("`%s' tag used in naming `%#T'",
15120 class_key == union_type ? "union"
15121 : class_key == record_type ? "struct" : "class",
15122 type);
15125 /* Issue an error message if DECL is redeclared with different
15126 access than its original declaration [class.access.spec/3].
15127 This applies to nested classes and nested class templates.
15128 [class.mem/1]. */
15130 static void cp_parser_check_access_in_redeclaration (tree decl)
15132 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15133 return;
15135 if ((TREE_PRIVATE (decl)
15136 != (current_access_specifier == access_private_node))
15137 || (TREE_PROTECTED (decl)
15138 != (current_access_specifier == access_protected_node)))
15139 error ("%D redeclared with different access", decl);
15142 /* Look for the `template' keyword, as a syntactic disambiguator.
15143 Return TRUE iff it is present, in which case it will be
15144 consumed. */
15146 static bool
15147 cp_parser_optional_template_keyword (cp_parser *parser)
15149 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15151 /* The `template' keyword can only be used within templates;
15152 outside templates the parser can always figure out what is a
15153 template and what is not. */
15154 if (!processing_template_decl)
15156 error ("`template' (as a disambiguator) is only allowed "
15157 "within templates");
15158 /* If this part of the token stream is rescanned, the same
15159 error message would be generated. So, we purge the token
15160 from the stream. */
15161 cp_lexer_purge_token (parser->lexer);
15162 return false;
15164 else
15166 /* Consume the `template' keyword. */
15167 cp_lexer_consume_token (parser->lexer);
15168 return true;
15172 return false;
15175 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
15176 set PARSER->SCOPE, and perform other related actions. */
15178 static void
15179 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15181 tree value;
15182 tree check;
15184 /* Get the stored value. */
15185 value = cp_lexer_consume_token (parser->lexer)->value;
15186 /* Perform any access checks that were deferred. */
15187 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15188 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15189 /* Set the scope from the stored value. */
15190 parser->scope = TREE_VALUE (value);
15191 parser->qualifying_scope = TREE_TYPE (value);
15192 parser->object_scope = NULL_TREE;
15195 /* Add tokens to CACHE until a non-nested END token appears. */
15197 static void
15198 cp_parser_cache_group (cp_parser *parser,
15199 cp_token_cache *cache,
15200 enum cpp_ttype end,
15201 unsigned depth)
15203 while (true)
15205 cp_token *token;
15207 /* Abort a parenthesized expression if we encounter a brace. */
15208 if ((end == CPP_CLOSE_PAREN || depth == 0)
15209 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15210 return;
15211 /* If we've reached the end of the file, stop. */
15212 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15213 return;
15214 /* Consume the next token. */
15215 token = cp_lexer_consume_token (parser->lexer);
15216 /* Add this token to the tokens we are saving. */
15217 cp_token_cache_push_token (cache, token);
15218 /* See if it starts a new group. */
15219 if (token->type == CPP_OPEN_BRACE)
15221 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15222 if (depth == 0)
15223 return;
15225 else if (token->type == CPP_OPEN_PAREN)
15226 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15227 else if (token->type == end)
15228 return;
15232 /* Begin parsing tentatively. We always save tokens while parsing
15233 tentatively so that if the tentative parsing fails we can restore the
15234 tokens. */
15236 static void
15237 cp_parser_parse_tentatively (cp_parser* parser)
15239 /* Enter a new parsing context. */
15240 parser->context = cp_parser_context_new (parser->context);
15241 /* Begin saving tokens. */
15242 cp_lexer_save_tokens (parser->lexer);
15243 /* In order to avoid repetitive access control error messages,
15244 access checks are queued up until we are no longer parsing
15245 tentatively. */
15246 push_deferring_access_checks (dk_deferred);
15249 /* Commit to the currently active tentative parse. */
15251 static void
15252 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15254 cp_parser_context *context;
15255 cp_lexer *lexer;
15257 /* Mark all of the levels as committed. */
15258 lexer = parser->lexer;
15259 for (context = parser->context; context->next; context = context->next)
15261 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15262 break;
15263 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15264 while (!cp_lexer_saving_tokens (lexer))
15265 lexer = lexer->next;
15266 cp_lexer_commit_tokens (lexer);
15270 /* Abort the currently active tentative parse. All consumed tokens
15271 will be rolled back, and no diagnostics will be issued. */
15273 static void
15274 cp_parser_abort_tentative_parse (cp_parser* parser)
15276 cp_parser_simulate_error (parser);
15277 /* Now, pretend that we want to see if the construct was
15278 successfully parsed. */
15279 cp_parser_parse_definitely (parser);
15282 /* Stop parsing tentatively. If a parse error has occurred, restore the
15283 token stream. Otherwise, commit to the tokens we have consumed.
15284 Returns true if no error occurred; false otherwise. */
15286 static bool
15287 cp_parser_parse_definitely (cp_parser* parser)
15289 bool error_occurred;
15290 cp_parser_context *context;
15292 /* Remember whether or not an error occurred, since we are about to
15293 destroy that information. */
15294 error_occurred = cp_parser_error_occurred (parser);
15295 /* Remove the topmost context from the stack. */
15296 context = parser->context;
15297 parser->context = context->next;
15298 /* If no parse errors occurred, commit to the tentative parse. */
15299 if (!error_occurred)
15301 /* Commit to the tokens read tentatively, unless that was
15302 already done. */
15303 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15304 cp_lexer_commit_tokens (parser->lexer);
15306 pop_to_parent_deferring_access_checks ();
15308 /* Otherwise, if errors occurred, roll back our state so that things
15309 are just as they were before we began the tentative parse. */
15310 else
15312 cp_lexer_rollback_tokens (parser->lexer);
15313 pop_deferring_access_checks ();
15315 /* Add the context to the front of the free list. */
15316 context->next = cp_parser_context_free_list;
15317 cp_parser_context_free_list = context;
15319 return !error_occurred;
15322 /* Returns true if we are parsing tentatively -- but have decided that
15323 we will stick with this tentative parse, even if errors occur. */
15325 static bool
15326 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15328 return (cp_parser_parsing_tentatively (parser)
15329 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15332 /* Returns nonzero iff an error has occurred during the most recent
15333 tentative parse. */
15335 static bool
15336 cp_parser_error_occurred (cp_parser* parser)
15338 return (cp_parser_parsing_tentatively (parser)
15339 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15342 /* Returns nonzero if GNU extensions are allowed. */
15344 static bool
15345 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15347 return parser->allow_gnu_extensions_p;
15351 /* The parser. */
15353 static GTY (()) cp_parser *the_parser;
15355 /* External interface. */
15357 /* Parse one entire translation unit. */
15359 void
15360 c_parse_file (void)
15362 bool error_occurred;
15363 static bool already_called = false;
15365 if (already_called)
15367 sorry ("inter-module optimizations not implemented for C++");
15368 return;
15370 already_called = true;
15372 the_parser = cp_parser_new ();
15373 push_deferring_access_checks (flag_access_control
15374 ? dk_no_deferred : dk_no_check);
15375 error_occurred = cp_parser_translation_unit (the_parser);
15376 the_parser = NULL;
15379 /* This variable must be provided by every front end. */
15381 int yydebug;
15383 #include "gt-cp-parser.h"