cp:
[official-gcc.git] / gcc / cp / parser.c
blob6155af404cdd74c57fa2008a53fe39bb03dc9bda
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
39 /* The lexer. */
41 /* Overview
42 --------
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
45 look-ahead.
47 Methodology
48 -----------
50 We use a circular buffer to store incoming tokens.
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
64 circular buffer. */
66 /* A C++ token. */
68 typedef struct cp_token GTY (())
70 /* The kind of token. */
71 enum cpp_ttype type;
72 /* The value associated with this token, if any. */
73 tree value;
74 /* If this token is a keyword, this value indicates which keyword.
75 Otherwise, this value is RID_MAX. */
76 enum rid keyword;
77 /* The location at which this token was found. */
78 location_t location;
79 } cp_token;
81 /* The number of tokens in a single token block. */
83 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
85 /* A group of tokens. These groups are chained together to store
86 large numbers of tokens. (For example, a token block is created
87 when the body of an inline member function is first encountered;
88 the tokens are processed later after the class definition is
89 complete.)
91 This somewhat ungainly data structure (as opposed to, say, a
92 variable-length array), is used due to contraints imposed by the
93 current garbage-collection methodology. If it is made more
94 flexible, we could perhaps simplify the data structures involved. */
96 typedef struct cp_token_block GTY (())
98 /* The tokens. */
99 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
100 /* The number of tokens in this block. */
101 size_t num_tokens;
102 /* The next token block in the chain. */
103 struct cp_token_block *next;
104 /* The previous block in the chain. */
105 struct cp_token_block *prev;
106 } cp_token_block;
108 typedef struct cp_token_cache GTY (())
110 /* The first block in the cache. NULL if there are no tokens in the
111 cache. */
112 cp_token_block *first;
113 /* The last block in the cache. NULL If there are no tokens in the
114 cache. */
115 cp_token_block *last;
116 } cp_token_cache;
118 /* Prototypes. */
120 static cp_token_cache *cp_token_cache_new
121 (void);
122 static void cp_token_cache_push_token
123 (cp_token_cache *, cp_token *);
125 /* Create a new cp_token_cache. */
127 static cp_token_cache *
128 cp_token_cache_new ()
130 return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
133 /* Add *TOKEN to *CACHE. */
135 static void
136 cp_token_cache_push_token (cp_token_cache *cache,
137 cp_token *token)
139 cp_token_block *b = cache->last;
141 /* See if we need to allocate a new token block. */
142 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
144 b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
145 b->prev = cache->last;
146 if (cache->last)
148 cache->last->next = b;
149 cache->last = b;
151 else
152 cache->first = cache->last = b;
154 /* Add this token to the current token block. */
155 b->tokens[b->num_tokens++] = *token;
158 /* The cp_lexer structure represents the C++ lexer. It is responsible
159 for managing the token stream from the preprocessor and supplying
160 it to the parser. */
162 typedef struct cp_lexer GTY (())
164 /* The memory allocated for the buffer. Never NULL. */
165 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
166 /* A pointer just past the end of the memory allocated for the buffer. */
167 cp_token * GTY ((skip (""))) buffer_end;
168 /* The first valid token in the buffer, or NULL if none. */
169 cp_token * GTY ((skip (""))) first_token;
170 /* The next available token. If NEXT_TOKEN is NULL, then there are
171 no more available tokens. */
172 cp_token * GTY ((skip (""))) next_token;
173 /* A pointer just past the last available token. If FIRST_TOKEN is
174 NULL, however, there are no available tokens, and then this
175 location is simply the place in which the next token read will be
176 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
177 When the LAST_TOKEN == BUFFER, then the last token is at the
178 highest memory address in the BUFFER. */
179 cp_token * GTY ((skip (""))) last_token;
181 /* A stack indicating positions at which cp_lexer_save_tokens was
182 called. The top entry is the most recent position at which we
183 began saving tokens. The entries are differences in token
184 position between FIRST_TOKEN and the first saved token.
186 If the stack is non-empty, we are saving tokens. When a token is
187 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
188 pointer will not. The token stream will be preserved so that it
189 can be reexamined later.
191 If the stack is empty, then we are not saving tokens. Whenever a
192 token is consumed, the FIRST_TOKEN pointer will be moved, and the
193 consumed token will be gone forever. */
194 varray_type saved_tokens;
196 /* The STRING_CST tokens encountered while processing the current
197 string literal. */
198 varray_type string_tokens;
200 /* True if we should obtain more tokens from the preprocessor; false
201 if we are processing a saved token cache. */
202 bool main_lexer_p;
204 /* True if we should output debugging information. */
205 bool debugging_p;
207 /* The next lexer in a linked list of lexers. */
208 struct cp_lexer *next;
209 } cp_lexer;
211 /* Prototypes. */
213 static cp_lexer *cp_lexer_new_main
214 (void);
215 static cp_lexer *cp_lexer_new_from_tokens
216 (struct cp_token_cache *);
217 static int cp_lexer_saving_tokens
218 (const cp_lexer *);
219 static cp_token *cp_lexer_next_token
220 (cp_lexer *, cp_token *);
221 static ptrdiff_t cp_lexer_token_difference
222 (cp_lexer *, cp_token *, cp_token *);
223 static cp_token *cp_lexer_read_token
224 (cp_lexer *);
225 static void cp_lexer_maybe_grow_buffer
226 (cp_lexer *);
227 static void cp_lexer_get_preprocessor_token
228 (cp_lexer *, cp_token *);
229 static cp_token *cp_lexer_peek_token
230 (cp_lexer *);
231 static cp_token *cp_lexer_peek_nth_token
232 (cp_lexer *, size_t);
233 static inline bool cp_lexer_next_token_is
234 (cp_lexer *, enum cpp_ttype);
235 static bool cp_lexer_next_token_is_not
236 (cp_lexer *, enum cpp_ttype);
237 static bool cp_lexer_next_token_is_keyword
238 (cp_lexer *, enum rid);
239 static cp_token *cp_lexer_consume_token
240 (cp_lexer *);
241 static void cp_lexer_purge_token
242 (cp_lexer *);
243 static void cp_lexer_purge_tokens_after
244 (cp_lexer *, cp_token *);
245 static void cp_lexer_save_tokens
246 (cp_lexer *);
247 static void cp_lexer_commit_tokens
248 (cp_lexer *);
249 static void cp_lexer_rollback_tokens
250 (cp_lexer *);
251 static inline void cp_lexer_set_source_position_from_token
252 (cp_lexer *, const cp_token *);
253 static void cp_lexer_print_token
254 (FILE *, cp_token *);
255 static inline bool cp_lexer_debugging_p
256 (cp_lexer *);
257 static void cp_lexer_start_debugging
258 (cp_lexer *) ATTRIBUTE_UNUSED;
259 static void cp_lexer_stop_debugging
260 (cp_lexer *) ATTRIBUTE_UNUSED;
262 /* Manifest constants. */
264 #define CP_TOKEN_BUFFER_SIZE 5
265 #define CP_SAVED_TOKENS_SIZE 5
267 /* A token type for keywords, as opposed to ordinary identifiers. */
268 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
270 /* A token type for template-ids. If a template-id is processed while
271 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
272 the value of the CPP_TEMPLATE_ID is whatever was returned by
273 cp_parser_template_id. */
274 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
276 /* A token type for nested-name-specifiers. If a
277 nested-name-specifier is processed while parsing tentatively, it is
278 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
279 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
280 cp_parser_nested_name_specifier_opt. */
281 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
283 /* A token type for tokens that are not tokens at all; these are used
284 to mark the end of a token block. */
285 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
287 /* Variables. */
289 /* The stream to which debugging output should be written. */
290 static FILE *cp_lexer_debug_stream;
292 /* Create a new main C++ lexer, the lexer that gets tokens from the
293 preprocessor. */
295 static cp_lexer *
296 cp_lexer_new_main (void)
298 cp_lexer *lexer;
299 cp_token first_token;
301 /* It's possible that lexing the first token will load a PCH file,
302 which is a GC collection point. So we have to grab the first
303 token before allocating any memory. */
304 cp_lexer_get_preprocessor_token (NULL, &first_token);
305 cpp_get_callbacks (parse_in)->valid_pch = NULL;
307 /* Allocate the memory. */
308 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
310 /* Create the circular buffer. */
311 lexer->buffer = ((cp_token *)
312 ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token)));
313 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
315 /* There is one token in the buffer. */
316 lexer->last_token = lexer->buffer + 1;
317 lexer->first_token = lexer->buffer;
318 lexer->next_token = lexer->buffer;
319 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
321 /* This lexer obtains more tokens by calling c_lex. */
322 lexer->main_lexer_p = true;
324 /* Create the SAVED_TOKENS stack. */
325 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
327 /* Create the STRINGS array. */
328 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
330 /* Assume we are not debugging. */
331 lexer->debugging_p = false;
333 return lexer;
336 /* Create a new lexer whose token stream is primed with the TOKENS.
337 When these tokens are exhausted, no new tokens will be read. */
339 static cp_lexer *
340 cp_lexer_new_from_tokens (cp_token_cache *tokens)
342 cp_lexer *lexer;
343 cp_token *token;
344 cp_token_block *block;
345 ptrdiff_t num_tokens;
347 /* Allocate the memory. */
348 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
350 /* Create a new buffer, appropriately sized. */
351 num_tokens = 0;
352 for (block = tokens->first; block != NULL; block = block->next)
353 num_tokens += block->num_tokens;
354 lexer->buffer = ((cp_token *) ggc_alloc (num_tokens * sizeof (cp_token)));
355 lexer->buffer_end = lexer->buffer + num_tokens;
357 /* Install the tokens. */
358 token = lexer->buffer;
359 for (block = tokens->first; block != NULL; block = block->next)
361 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
362 token += block->num_tokens;
365 /* The FIRST_TOKEN is the beginning of the buffer. */
366 lexer->first_token = lexer->buffer;
367 /* The next available token is also at the beginning of the buffer. */
368 lexer->next_token = lexer->buffer;
369 /* The buffer is full. */
370 lexer->last_token = lexer->first_token;
372 /* This lexer doesn't obtain more tokens. */
373 lexer->main_lexer_p = false;
375 /* Create the SAVED_TOKENS stack. */
376 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
378 /* Create the STRINGS array. */
379 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
381 /* Assume we are not debugging. */
382 lexer->debugging_p = false;
384 return lexer;
387 /* Returns nonzero if debugging information should be output. */
389 static inline bool
390 cp_lexer_debugging_p (cp_lexer *lexer)
392 return lexer->debugging_p;
395 /* Set the current source position from the information stored in
396 TOKEN. */
398 static inline void
399 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
400 const cp_token *token)
402 /* Ideally, the source position information would not be a global
403 variable, but it is. */
405 /* Update the line number. */
406 if (token->type != CPP_EOF)
407 input_location = token->location;
410 /* TOKEN points into the circular token buffer. Return a pointer to
411 the next token in the buffer. */
413 static inline cp_token *
414 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
416 token++;
417 if (token == lexer->buffer_end)
418 token = lexer->buffer;
419 return token;
422 /* nonzero if we are presently saving tokens. */
424 static int
425 cp_lexer_saving_tokens (const cp_lexer* lexer)
427 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
430 /* Return a pointer to the token that is N tokens beyond TOKEN in the
431 buffer. */
433 static cp_token *
434 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
436 token += n;
437 if (token >= lexer->buffer_end)
438 token = lexer->buffer + (token - lexer->buffer_end);
439 return token;
442 /* Returns the number of times that START would have to be incremented
443 to reach FINISH. If START and FINISH are the same, returns zero. */
445 static ptrdiff_t
446 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
448 if (finish >= start)
449 return finish - start;
450 else
451 return ((lexer->buffer_end - lexer->buffer)
452 - (start - finish));
455 /* Obtain another token from the C preprocessor and add it to the
456 token buffer. Returns the newly read token. */
458 static cp_token *
459 cp_lexer_read_token (cp_lexer* lexer)
461 cp_token *token;
463 /* Make sure there is room in the buffer. */
464 cp_lexer_maybe_grow_buffer (lexer);
466 /* If there weren't any tokens, then this one will be the first. */
467 if (!lexer->first_token)
468 lexer->first_token = lexer->last_token;
469 /* Similarly, if there were no available tokens, there is one now. */
470 if (!lexer->next_token)
471 lexer->next_token = lexer->last_token;
473 /* Figure out where we're going to store the new token. */
474 token = lexer->last_token;
476 /* Get a new token from the preprocessor. */
477 cp_lexer_get_preprocessor_token (lexer, token);
479 /* Increment LAST_TOKEN. */
480 lexer->last_token = cp_lexer_next_token (lexer, token);
482 /* The preprocessor does not yet do translation phase six, i.e., the
483 combination of adjacent string literals. Therefore, we do it
484 here. */
485 if (token->type == CPP_STRING || token->type == CPP_WSTRING)
487 ptrdiff_t delta;
488 int i;
490 /* When we grow the buffer, we may invalidate TOKEN. So, save
491 the distance from the beginning of the BUFFER so that we can
492 recaulate it. */
493 delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
494 /* Make sure there is room in the buffer for another token. */
495 cp_lexer_maybe_grow_buffer (lexer);
496 /* Restore TOKEN. */
497 token = lexer->buffer;
498 for (i = 0; i < delta; ++i)
499 token = cp_lexer_next_token (lexer, token);
501 VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
502 while (true)
504 /* Read the token after TOKEN. */
505 cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
506 /* See whether it's another string constant. */
507 if (lexer->last_token->type != token->type)
509 /* If not, then it will be the next real token. */
510 lexer->last_token = cp_lexer_next_token (lexer,
511 lexer->last_token);
512 break;
515 /* Chain the strings together. */
516 VARRAY_PUSH_TREE (lexer->string_tokens,
517 lexer->last_token->value);
520 /* Create a single STRING_CST. Curiously we have to call
521 combine_strings even if there is only a single string in
522 order to get the type set correctly. */
523 token->value = combine_strings (lexer->string_tokens);
524 VARRAY_CLEAR (lexer->string_tokens);
525 token->value = fix_string_type (token->value);
526 /* Strings should have type `const char []'. Right now, we will
527 have an ARRAY_TYPE that is constant rather than an array of
528 constant elements. */
529 if (flag_const_strings)
531 tree type;
533 /* Get the current type. It will be an ARRAY_TYPE. */
534 type = TREE_TYPE (token->value);
535 /* Use build_cplus_array_type to rebuild the array, thereby
536 getting the right type. */
537 type = build_cplus_array_type (TREE_TYPE (type),
538 TYPE_DOMAIN (type));
539 /* Reset the type of the token. */
540 TREE_TYPE (token->value) = type;
544 return token;
547 /* If the circular buffer is full, make it bigger. */
549 static void
550 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
552 /* If the buffer is full, enlarge it. */
553 if (lexer->last_token == lexer->first_token)
555 cp_token *new_buffer;
556 cp_token *old_buffer;
557 cp_token *new_first_token;
558 ptrdiff_t buffer_length;
559 size_t num_tokens_to_copy;
561 /* Remember the current buffer pointer. It will become invalid,
562 but we will need to do pointer arithmetic involving this
563 value. */
564 old_buffer = lexer->buffer;
565 /* Compute the current buffer size. */
566 buffer_length = lexer->buffer_end - lexer->buffer;
567 /* Allocate a buffer twice as big. */
568 new_buffer = ((cp_token *)
569 ggc_realloc (lexer->buffer,
570 2 * buffer_length * sizeof (cp_token)));
572 /* Because the buffer is circular, logically consecutive tokens
573 are not necessarily placed consecutively in memory.
574 Therefore, we must keep move the tokens that were before
575 FIRST_TOKEN to the second half of the newly allocated
576 buffer. */
577 num_tokens_to_copy = (lexer->first_token - old_buffer);
578 memcpy (new_buffer + buffer_length,
579 new_buffer,
580 num_tokens_to_copy * sizeof (cp_token));
581 /* Clear the rest of the buffer. We never look at this storage,
582 but the garbage collector may. */
583 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
584 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
586 /* Now recompute all of the buffer pointers. */
587 new_first_token
588 = new_buffer + (lexer->first_token - old_buffer);
589 if (lexer->next_token != NULL)
591 ptrdiff_t next_token_delta;
593 if (lexer->next_token > lexer->first_token)
594 next_token_delta = lexer->next_token - lexer->first_token;
595 else
596 next_token_delta =
597 buffer_length - (lexer->first_token - lexer->next_token);
598 lexer->next_token = new_first_token + next_token_delta;
600 lexer->last_token = new_first_token + buffer_length;
601 lexer->buffer = new_buffer;
602 lexer->buffer_end = new_buffer + buffer_length * 2;
603 lexer->first_token = new_first_token;
607 /* Store the next token from the preprocessor in *TOKEN. */
609 static void
610 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
611 cp_token *token)
613 bool done;
615 /* If this not the main lexer, return a terminating CPP_EOF token. */
616 if (lexer != NULL && !lexer->main_lexer_p)
618 token->type = CPP_EOF;
619 token->location.line = 0;
620 token->location.file = NULL;
621 token->value = NULL_TREE;
622 token->keyword = RID_MAX;
624 return;
627 done = false;
628 /* Keep going until we get a token we like. */
629 while (!done)
631 /* Get a new token from the preprocessor. */
632 token->type = c_lex (&token->value);
633 /* Issue messages about tokens we cannot process. */
634 switch (token->type)
636 case CPP_ATSIGN:
637 case CPP_HASH:
638 case CPP_PASTE:
639 error ("invalid token");
640 break;
642 default:
643 /* This is a good token, so we exit the loop. */
644 done = true;
645 break;
648 /* Now we've got our token. */
649 token->location = input_location;
651 /* Check to see if this token is a keyword. */
652 if (token->type == CPP_NAME
653 && C_IS_RESERVED_WORD (token->value))
655 /* Mark this token as a keyword. */
656 token->type = CPP_KEYWORD;
657 /* Record which keyword. */
658 token->keyword = C_RID_CODE (token->value);
659 /* Update the value. Some keywords are mapped to particular
660 entities, rather than simply having the value of the
661 corresponding IDENTIFIER_NODE. For example, `__const' is
662 mapped to `const'. */
663 token->value = ridpointers[token->keyword];
665 else
666 token->keyword = RID_MAX;
669 /* Return a pointer to the next token in the token stream, but do not
670 consume it. */
672 static cp_token *
673 cp_lexer_peek_token (cp_lexer* lexer)
675 cp_token *token;
677 /* If there are no tokens, read one now. */
678 if (!lexer->next_token)
679 cp_lexer_read_token (lexer);
681 /* Provide debugging output. */
682 if (cp_lexer_debugging_p (lexer))
684 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
685 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
686 fprintf (cp_lexer_debug_stream, "\n");
689 token = lexer->next_token;
690 cp_lexer_set_source_position_from_token (lexer, token);
691 return token;
694 /* Return true if the next token has the indicated TYPE. */
696 static bool
697 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
699 cp_token *token;
701 /* Peek at the next token. */
702 token = cp_lexer_peek_token (lexer);
703 /* Check to see if it has the indicated TYPE. */
704 return token->type == type;
707 /* Return true if the next token does not have the indicated TYPE. */
709 static bool
710 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
712 return !cp_lexer_next_token_is (lexer, type);
715 /* Return true if the next token is the indicated KEYWORD. */
717 static bool
718 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
720 cp_token *token;
722 /* Peek at the next token. */
723 token = cp_lexer_peek_token (lexer);
724 /* Check to see if it is the indicated keyword. */
725 return token->keyword == keyword;
728 /* Return a pointer to the Nth token in the token stream. If N is 1,
729 then this is precisely equivalent to cp_lexer_peek_token. */
731 static cp_token *
732 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
734 cp_token *token;
736 /* N is 1-based, not zero-based. */
737 my_friendly_assert (n > 0, 20000224);
739 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
740 token = lexer->next_token;
741 /* If there are no tokens in the buffer, get one now. */
742 if (!token)
744 cp_lexer_read_token (lexer);
745 token = lexer->next_token;
748 /* Now, read tokens until we have enough. */
749 while (--n > 0)
751 /* Advance to the next token. */
752 token = cp_lexer_next_token (lexer, token);
753 /* If that's all the tokens we have, read a new one. */
754 if (token == lexer->last_token)
755 token = cp_lexer_read_token (lexer);
758 return token;
761 /* Consume the next token. The pointer returned is valid only until
762 another token is read. Callers should preserve copy the token
763 explicitly if they will need its value for a longer period of
764 time. */
766 static cp_token *
767 cp_lexer_consume_token (cp_lexer* lexer)
769 cp_token *token;
771 /* If there are no tokens, read one now. */
772 if (!lexer->next_token)
773 cp_lexer_read_token (lexer);
775 /* Remember the token we'll be returning. */
776 token = lexer->next_token;
778 /* Increment NEXT_TOKEN. */
779 lexer->next_token = cp_lexer_next_token (lexer,
780 lexer->next_token);
781 /* Check to see if we're all out of tokens. */
782 if (lexer->next_token == lexer->last_token)
783 lexer->next_token = NULL;
785 /* If we're not saving tokens, then move FIRST_TOKEN too. */
786 if (!cp_lexer_saving_tokens (lexer))
788 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
789 if (!lexer->next_token)
790 lexer->first_token = NULL;
791 else
792 lexer->first_token = lexer->next_token;
795 /* Provide debugging output. */
796 if (cp_lexer_debugging_p (lexer))
798 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
799 cp_lexer_print_token (cp_lexer_debug_stream, token);
800 fprintf (cp_lexer_debug_stream, "\n");
803 return token;
806 /* Permanently remove the next token from the token stream. There
807 must be a valid next token already; this token never reads
808 additional tokens from the preprocessor. */
810 static void
811 cp_lexer_purge_token (cp_lexer *lexer)
813 cp_token *token;
814 cp_token *next_token;
816 token = lexer->next_token;
817 while (true)
819 next_token = cp_lexer_next_token (lexer, token);
820 if (next_token == lexer->last_token)
821 break;
822 *token = *next_token;
823 token = next_token;
826 lexer->last_token = token;
827 /* The token purged may have been the only token remaining; if so,
828 clear NEXT_TOKEN. */
829 if (lexer->next_token == token)
830 lexer->next_token = NULL;
833 /* Permanently remove all tokens after TOKEN, up to, but not
834 including, the token that will be returned next by
835 cp_lexer_peek_token. */
837 static void
838 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
840 cp_token *peek;
841 cp_token *t1;
842 cp_token *t2;
844 if (lexer->next_token)
846 /* Copy the tokens that have not yet been read to the location
847 immediately following TOKEN. */
848 t1 = cp_lexer_next_token (lexer, token);
849 t2 = peek = cp_lexer_peek_token (lexer);
850 /* Move tokens into the vacant area between TOKEN and PEEK. */
851 while (t2 != lexer->last_token)
853 *t1 = *t2;
854 t1 = cp_lexer_next_token (lexer, t1);
855 t2 = cp_lexer_next_token (lexer, t2);
857 /* Now, the next available token is right after TOKEN. */
858 lexer->next_token = cp_lexer_next_token (lexer, token);
859 /* And the last token is wherever we ended up. */
860 lexer->last_token = t1;
862 else
864 /* There are no tokens in the buffer, so there is nothing to
865 copy. The last token in the buffer is TOKEN itself. */
866 lexer->last_token = cp_lexer_next_token (lexer, token);
870 /* Begin saving tokens. All tokens consumed after this point will be
871 preserved. */
873 static void
874 cp_lexer_save_tokens (cp_lexer* lexer)
876 /* Provide debugging output. */
877 if (cp_lexer_debugging_p (lexer))
878 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
880 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
881 restore the tokens if required. */
882 if (!lexer->next_token)
883 cp_lexer_read_token (lexer);
885 VARRAY_PUSH_INT (lexer->saved_tokens,
886 cp_lexer_token_difference (lexer,
887 lexer->first_token,
888 lexer->next_token));
891 /* Commit to the portion of the token stream most recently saved. */
893 static void
894 cp_lexer_commit_tokens (cp_lexer* lexer)
896 /* Provide debugging output. */
897 if (cp_lexer_debugging_p (lexer))
898 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
900 VARRAY_POP (lexer->saved_tokens);
903 /* Return all tokens saved since the last call to cp_lexer_save_tokens
904 to the token stream. Stop saving tokens. */
906 static void
907 cp_lexer_rollback_tokens (cp_lexer* lexer)
909 size_t delta;
911 /* Provide debugging output. */
912 if (cp_lexer_debugging_p (lexer))
913 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
915 /* Find the token that was the NEXT_TOKEN when we started saving
916 tokens. */
917 delta = VARRAY_TOP_INT(lexer->saved_tokens);
918 /* Make it the next token again now. */
919 lexer->next_token = cp_lexer_advance_token (lexer,
920 lexer->first_token,
921 delta);
922 /* It might be the case that there were no tokens when we started
923 saving tokens, but that there are some tokens now. */
924 if (!lexer->next_token && lexer->first_token)
925 lexer->next_token = lexer->first_token;
927 /* Stop saving tokens. */
928 VARRAY_POP (lexer->saved_tokens);
931 /* Print a representation of the TOKEN on the STREAM. */
933 static void
934 cp_lexer_print_token (FILE * stream, cp_token* token)
936 const char *token_type = NULL;
938 /* Figure out what kind of token this is. */
939 switch (token->type)
941 case CPP_EQ:
942 token_type = "EQ";
943 break;
945 case CPP_COMMA:
946 token_type = "COMMA";
947 break;
949 case CPP_OPEN_PAREN:
950 token_type = "OPEN_PAREN";
951 break;
953 case CPP_CLOSE_PAREN:
954 token_type = "CLOSE_PAREN";
955 break;
957 case CPP_OPEN_BRACE:
958 token_type = "OPEN_BRACE";
959 break;
961 case CPP_CLOSE_BRACE:
962 token_type = "CLOSE_BRACE";
963 break;
965 case CPP_SEMICOLON:
966 token_type = "SEMICOLON";
967 break;
969 case CPP_NAME:
970 token_type = "NAME";
971 break;
973 case CPP_EOF:
974 token_type = "EOF";
975 break;
977 case CPP_KEYWORD:
978 token_type = "keyword";
979 break;
981 /* This is not a token that we know how to handle yet. */
982 default:
983 break;
986 /* If we have a name for the token, print it out. Otherwise, we
987 simply give the numeric code. */
988 if (token_type)
989 fprintf (stream, "%s", token_type);
990 else
991 fprintf (stream, "%d", token->type);
992 /* And, for an identifier, print the identifier name. */
993 if (token->type == CPP_NAME
994 /* Some keywords have a value that is not an IDENTIFIER_NODE.
995 For example, `struct' is mapped to an INTEGER_CST. */
996 || (token->type == CPP_KEYWORD
997 && TREE_CODE (token->value) == IDENTIFIER_NODE))
998 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1001 /* Start emitting debugging information. */
1003 static void
1004 cp_lexer_start_debugging (cp_lexer* lexer)
1006 ++lexer->debugging_p;
1009 /* Stop emitting debugging information. */
1011 static void
1012 cp_lexer_stop_debugging (cp_lexer* lexer)
1014 --lexer->debugging_p;
1018 /* The parser. */
1020 /* Overview
1021 --------
1023 A cp_parser parses the token stream as specified by the C++
1024 grammar. Its job is purely parsing, not semantic analysis. For
1025 example, the parser breaks the token stream into declarators,
1026 expressions, statements, and other similar syntactic constructs.
1027 It does not check that the types of the expressions on either side
1028 of an assignment-statement are compatible, or that a function is
1029 not declared with a parameter of type `void'.
1031 The parser invokes routines elsewhere in the compiler to perform
1032 semantic analysis and to build up the abstract syntax tree for the
1033 code processed.
1035 The parser (and the template instantiation code, which is, in a
1036 way, a close relative of parsing) are the only parts of the
1037 compiler that should be calling push_scope and pop_scope, or
1038 related functions. The parser (and template instantiation code)
1039 keeps track of what scope is presently active; everything else
1040 should simply honor that. (The code that generates static
1041 initializers may also need to set the scope, in order to check
1042 access control correctly when emitting the initializers.)
1044 Methodology
1045 -----------
1047 The parser is of the standard recursive-descent variety. Upcoming
1048 tokens in the token stream are examined in order to determine which
1049 production to use when parsing a non-terminal. Some C++ constructs
1050 require arbitrary look ahead to disambiguate. For example, it is
1051 impossible, in the general case, to tell whether a statement is an
1052 expression or declaration without scanning the entire statement.
1053 Therefore, the parser is capable of "parsing tentatively." When the
1054 parser is not sure what construct comes next, it enters this mode.
1055 Then, while we attempt to parse the construct, the parser queues up
1056 error messages, rather than issuing them immediately, and saves the
1057 tokens it consumes. If the construct is parsed successfully, the
1058 parser "commits", i.e., it issues any queued error messages and
1059 the tokens that were being preserved are permanently discarded.
1060 If, however, the construct is not parsed successfully, the parser
1061 rolls back its state completely so that it can resume parsing using
1062 a different alternative.
1064 Future Improvements
1065 -------------------
1067 The performance of the parser could probably be improved
1068 substantially. Some possible improvements include:
1070 - The expression parser recurses through the various levels of
1071 precedence as specified in the grammar, rather than using an
1072 operator-precedence technique. Therefore, parsing a simple
1073 identifier requires multiple recursive calls.
1075 - We could often eliminate the need to parse tentatively by
1076 looking ahead a little bit. In some places, this approach
1077 might not entirely eliminate the need to parse tentatively, but
1078 it might still speed up the average case. */
1080 /* Flags that are passed to some parsing functions. These values can
1081 be bitwise-ored together. */
1083 typedef enum cp_parser_flags
1085 /* No flags. */
1086 CP_PARSER_FLAGS_NONE = 0x0,
1087 /* The construct is optional. If it is not present, then no error
1088 should be issued. */
1089 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1090 /* When parsing a type-specifier, do not allow user-defined types. */
1091 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1092 } cp_parser_flags;
1094 /* The different kinds of ids that we ecounter. */
1096 typedef enum cp_parser_id_kind
1098 /* Not an id at all. */
1099 CP_PARSER_ID_KIND_NONE,
1100 /* An unqualified-id that is not a template-id. */
1101 CP_PARSER_ID_KIND_UNQUALIFIED,
1102 /* An unqualified template-id. */
1103 CP_PARSER_ID_KIND_TEMPLATE_ID,
1104 /* A qualified-id. */
1105 CP_PARSER_ID_KIND_QUALIFIED
1106 } cp_parser_id_kind;
1108 /* The different kinds of declarators we want to parse. */
1110 typedef enum cp_parser_declarator_kind
1112 /* We want an abstract declartor. */
1113 CP_PARSER_DECLARATOR_ABSTRACT,
1114 /* We want a named declarator. */
1115 CP_PARSER_DECLARATOR_NAMED,
1116 /* We don't mind, but the name must be an unqualified-id */
1117 CP_PARSER_DECLARATOR_EITHER
1118 } cp_parser_declarator_kind;
1120 /* A mapping from a token type to a corresponding tree node type. */
1122 typedef struct cp_parser_token_tree_map_node
1124 /* The token type. */
1125 enum cpp_ttype token_type;
1126 /* The corresponding tree code. */
1127 enum tree_code tree_type;
1128 } cp_parser_token_tree_map_node;
1130 /* A complete map consists of several ordinary entries, followed by a
1131 terminator. The terminating entry has a token_type of CPP_EOF. */
1133 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1135 /* The status of a tentative parse. */
1137 typedef enum cp_parser_status_kind
1139 /* No errors have occurred. */
1140 CP_PARSER_STATUS_KIND_NO_ERROR,
1141 /* An error has occurred. */
1142 CP_PARSER_STATUS_KIND_ERROR,
1143 /* We are committed to this tentative parse, whether or not an error
1144 has occurred. */
1145 CP_PARSER_STATUS_KIND_COMMITTED
1146 } cp_parser_status_kind;
1148 /* Context that is saved and restored when parsing tentatively. */
1150 typedef struct cp_parser_context GTY (())
1152 /* If this is a tentative parsing context, the status of the
1153 tentative parse. */
1154 enum cp_parser_status_kind status;
1155 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1156 that are looked up in this context must be looked up both in the
1157 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1158 the context of the containing expression. */
1159 tree object_type;
1160 /* The next parsing context in the stack. */
1161 struct cp_parser_context *next;
1162 } cp_parser_context;
1164 /* Prototypes. */
1166 /* Constructors and destructors. */
1168 static cp_parser_context *cp_parser_context_new
1169 (cp_parser_context *);
1171 /* Class variables. */
1173 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1175 /* Constructors and destructors. */
1177 /* Construct a new context. The context below this one on the stack
1178 is given by NEXT. */
1180 static cp_parser_context *
1181 cp_parser_context_new (cp_parser_context* next)
1183 cp_parser_context *context;
1185 /* Allocate the storage. */
1186 if (cp_parser_context_free_list != NULL)
1188 /* Pull the first entry from the free list. */
1189 context = cp_parser_context_free_list;
1190 cp_parser_context_free_list = context->next;
1191 memset ((char *)context, 0, sizeof (*context));
1193 else
1194 context = ((cp_parser_context *)
1195 ggc_alloc_cleared (sizeof (cp_parser_context)));
1196 /* No errors have occurred yet in this context. */
1197 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1198 /* If this is not the bottomost context, copy information that we
1199 need from the previous context. */
1200 if (next)
1202 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1203 expression, then we are parsing one in this context, too. */
1204 context->object_type = next->object_type;
1205 /* Thread the stack. */
1206 context->next = next;
1209 return context;
1212 /* The cp_parser structure represents the C++ parser. */
1214 typedef struct cp_parser GTY(())
1216 /* The lexer from which we are obtaining tokens. */
1217 cp_lexer *lexer;
1219 /* The scope in which names should be looked up. If NULL_TREE, then
1220 we look up names in the scope that is currently open in the
1221 source program. If non-NULL, this is either a TYPE or
1222 NAMESPACE_DECL for the scope in which we should look.
1224 This value is not cleared automatically after a name is looked
1225 up, so we must be careful to clear it before starting a new look
1226 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1227 will look up `Z' in the scope of `X', rather than the current
1228 scope.) Unfortunately, it is difficult to tell when name lookup
1229 is complete, because we sometimes peek at a token, look it up,
1230 and then decide not to consume it. */
1231 tree scope;
1233 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1234 last lookup took place. OBJECT_SCOPE is used if an expression
1235 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1236 respectively. QUALIFYING_SCOPE is used for an expression of the
1237 form "X::Y"; it refers to X. */
1238 tree object_scope;
1239 tree qualifying_scope;
1241 /* A stack of parsing contexts. All but the bottom entry on the
1242 stack will be tentative contexts.
1244 We parse tentatively in order to determine which construct is in
1245 use in some situations. For example, in order to determine
1246 whether a statement is an expression-statement or a
1247 declaration-statement we parse it tentatively as a
1248 declaration-statement. If that fails, we then reparse the same
1249 token stream as an expression-statement. */
1250 cp_parser_context *context;
1252 /* True if we are parsing GNU C++. If this flag is not set, then
1253 GNU extensions are not recognized. */
1254 bool allow_gnu_extensions_p;
1256 /* TRUE if the `>' token should be interpreted as the greater-than
1257 operator. FALSE if it is the end of a template-id or
1258 template-parameter-list. */
1259 bool greater_than_is_operator_p;
1261 /* TRUE if default arguments are allowed within a parameter list
1262 that starts at this point. FALSE if only a gnu extension makes
1263 them permissable. */
1264 bool default_arg_ok_p;
1266 /* TRUE if we are parsing an integral constant-expression. See
1267 [expr.const] for a precise definition. */
1268 bool constant_expression_p;
1270 /* TRUE if we are parsing an integral constant-expression -- but a
1271 non-constant expression should be permitted as well. This flag
1272 is used when parsing an array bound so that GNU variable-length
1273 arrays are tolerated. */
1274 bool allow_non_constant_expression_p;
1276 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1277 been seen that makes the expression non-constant. */
1278 bool non_constant_expression_p;
1280 /* TRUE if local variable names and `this' are forbidden in the
1281 current context. */
1282 bool local_variables_forbidden_p;
1284 /* TRUE if the declaration we are parsing is part of a
1285 linkage-specification of the form `extern string-literal
1286 declaration'. */
1287 bool in_unbraced_linkage_specification_p;
1289 /* TRUE if we are presently parsing a declarator, after the
1290 direct-declarator. */
1291 bool in_declarator_p;
1293 /* If non-NULL, then we are parsing a construct where new type
1294 definitions are not permitted. The string stored here will be
1295 issued as an error message if a type is defined. */
1296 const char *type_definition_forbidden_message;
1298 /* A list of lists. The outer list is a stack, used for member
1299 functions of local classes. At each level there are two sub-list,
1300 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1301 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1302 TREE_VALUE's. The functions are chained in reverse declaration
1303 order.
1305 The TREE_PURPOSE sublist contains those functions with default
1306 arguments that need post processing, and the TREE_VALUE sublist
1307 contains those functions with definitions that need post
1308 processing.
1310 These lists can only be processed once the outermost class being
1311 defined is complete. */
1312 tree unparsed_functions_queues;
1314 /* The number of classes whose definitions are currently in
1315 progress. */
1316 unsigned num_classes_being_defined;
1318 /* The number of template parameter lists that apply directly to the
1319 current declaration. */
1320 unsigned num_template_parameter_lists;
1321 } cp_parser;
1323 /* The type of a function that parses some kind of expression */
1324 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1326 /* Prototypes. */
1328 /* Constructors and destructors. */
1330 static cp_parser *cp_parser_new
1331 (void);
1333 /* Routines to parse various constructs.
1335 Those that return `tree' will return the error_mark_node (rather
1336 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1337 Sometimes, they will return an ordinary node if error-recovery was
1338 attempted, even though a parse error occurrred. So, to check
1339 whether or not a parse error occurred, you should always use
1340 cp_parser_error_occurred. If the construct is optional (indicated
1341 either by an `_opt' in the name of the function that does the
1342 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1343 the construct is not present. */
1345 /* Lexical conventions [gram.lex] */
1347 static tree cp_parser_identifier
1348 (cp_parser *);
1350 /* Basic concepts [gram.basic] */
1352 static bool cp_parser_translation_unit
1353 (cp_parser *);
1355 /* Expressions [gram.expr] */
1357 static tree cp_parser_primary_expression
1358 (cp_parser *, cp_parser_id_kind *, tree *);
1359 static tree cp_parser_id_expression
1360 (cp_parser *, bool, bool, bool *);
1361 static tree cp_parser_unqualified_id
1362 (cp_parser *, bool, bool);
1363 static tree cp_parser_nested_name_specifier_opt
1364 (cp_parser *, bool, bool, bool);
1365 static tree cp_parser_nested_name_specifier
1366 (cp_parser *, bool, bool, bool);
1367 static tree cp_parser_class_or_namespace_name
1368 (cp_parser *, bool, bool, bool, bool);
1369 static tree cp_parser_postfix_expression
1370 (cp_parser *, bool);
1371 static tree cp_parser_expression_list
1372 (cp_parser *);
1373 static void cp_parser_pseudo_destructor_name
1374 (cp_parser *, tree *, tree *);
1375 static tree cp_parser_unary_expression
1376 (cp_parser *, bool);
1377 static enum tree_code cp_parser_unary_operator
1378 (cp_token *);
1379 static tree cp_parser_new_expression
1380 (cp_parser *);
1381 static tree cp_parser_new_placement
1382 (cp_parser *);
1383 static tree cp_parser_new_type_id
1384 (cp_parser *);
1385 static tree cp_parser_new_declarator_opt
1386 (cp_parser *);
1387 static tree cp_parser_direct_new_declarator
1388 (cp_parser *);
1389 static tree cp_parser_new_initializer
1390 (cp_parser *);
1391 static tree cp_parser_delete_expression
1392 (cp_parser *);
1393 static tree cp_parser_cast_expression
1394 (cp_parser *, bool);
1395 static tree cp_parser_pm_expression
1396 (cp_parser *);
1397 static tree cp_parser_multiplicative_expression
1398 (cp_parser *);
1399 static tree cp_parser_additive_expression
1400 (cp_parser *);
1401 static tree cp_parser_shift_expression
1402 (cp_parser *);
1403 static tree cp_parser_relational_expression
1404 (cp_parser *);
1405 static tree cp_parser_equality_expression
1406 (cp_parser *);
1407 static tree cp_parser_and_expression
1408 (cp_parser *);
1409 static tree cp_parser_exclusive_or_expression
1410 (cp_parser *);
1411 static tree cp_parser_inclusive_or_expression
1412 (cp_parser *);
1413 static tree cp_parser_logical_and_expression
1414 (cp_parser *);
1415 static tree cp_parser_logical_or_expression
1416 (cp_parser *);
1417 static tree cp_parser_conditional_expression
1418 (cp_parser *);
1419 static tree cp_parser_question_colon_clause
1420 (cp_parser *, tree);
1421 static tree cp_parser_assignment_expression
1422 (cp_parser *);
1423 static enum tree_code cp_parser_assignment_operator_opt
1424 (cp_parser *);
1425 static tree cp_parser_expression
1426 (cp_parser *);
1427 static tree cp_parser_constant_expression
1428 (cp_parser *, bool, bool *);
1430 /* Statements [gram.stmt.stmt] */
1432 static void cp_parser_statement
1433 (cp_parser *);
1434 static tree cp_parser_labeled_statement
1435 (cp_parser *);
1436 static tree cp_parser_expression_statement
1437 (cp_parser *);
1438 static tree cp_parser_compound_statement
1439 (cp_parser *);
1440 static void cp_parser_statement_seq_opt
1441 (cp_parser *);
1442 static tree cp_parser_selection_statement
1443 (cp_parser *);
1444 static tree cp_parser_condition
1445 (cp_parser *);
1446 static tree cp_parser_iteration_statement
1447 (cp_parser *);
1448 static void cp_parser_for_init_statement
1449 (cp_parser *);
1450 static tree cp_parser_jump_statement
1451 (cp_parser *);
1452 static void cp_parser_declaration_statement
1453 (cp_parser *);
1455 static tree cp_parser_implicitly_scoped_statement
1456 (cp_parser *);
1457 static void cp_parser_already_scoped_statement
1458 (cp_parser *);
1460 /* Declarations [gram.dcl.dcl] */
1462 static void cp_parser_declaration_seq_opt
1463 (cp_parser *);
1464 static void cp_parser_declaration
1465 (cp_parser *);
1466 static void cp_parser_block_declaration
1467 (cp_parser *, bool);
1468 static void cp_parser_simple_declaration
1469 (cp_parser *, bool);
1470 static tree cp_parser_decl_specifier_seq
1471 (cp_parser *, cp_parser_flags, tree *, bool *);
1472 static tree cp_parser_storage_class_specifier_opt
1473 (cp_parser *);
1474 static tree cp_parser_function_specifier_opt
1475 (cp_parser *);
1476 static tree cp_parser_type_specifier
1477 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1478 static tree cp_parser_simple_type_specifier
1479 (cp_parser *, cp_parser_flags);
1480 static tree cp_parser_type_name
1481 (cp_parser *);
1482 static tree cp_parser_elaborated_type_specifier
1483 (cp_parser *, bool, bool);
1484 static tree cp_parser_enum_specifier
1485 (cp_parser *);
1486 static void cp_parser_enumerator_list
1487 (cp_parser *, tree);
1488 static void cp_parser_enumerator_definition
1489 (cp_parser *, tree);
1490 static tree cp_parser_namespace_name
1491 (cp_parser *);
1492 static void cp_parser_namespace_definition
1493 (cp_parser *);
1494 static void cp_parser_namespace_body
1495 (cp_parser *);
1496 static tree cp_parser_qualified_namespace_specifier
1497 (cp_parser *);
1498 static void cp_parser_namespace_alias_definition
1499 (cp_parser *);
1500 static void cp_parser_using_declaration
1501 (cp_parser *);
1502 static void cp_parser_using_directive
1503 (cp_parser *);
1504 static void cp_parser_asm_definition
1505 (cp_parser *);
1506 static void cp_parser_linkage_specification
1507 (cp_parser *);
1509 /* Declarators [gram.dcl.decl] */
1511 static tree cp_parser_init_declarator
1512 (cp_parser *, tree, tree, bool, bool, bool *);
1513 static tree cp_parser_declarator
1514 (cp_parser *, cp_parser_declarator_kind, bool *);
1515 static tree cp_parser_direct_declarator
1516 (cp_parser *, cp_parser_declarator_kind, bool *);
1517 static enum tree_code cp_parser_ptr_operator
1518 (cp_parser *, tree *, tree *);
1519 static tree cp_parser_cv_qualifier_seq_opt
1520 (cp_parser *);
1521 static tree cp_parser_cv_qualifier_opt
1522 (cp_parser *);
1523 static tree cp_parser_declarator_id
1524 (cp_parser *);
1525 static tree cp_parser_type_id
1526 (cp_parser *);
1527 static tree cp_parser_type_specifier_seq
1528 (cp_parser *);
1529 static tree cp_parser_parameter_declaration_clause
1530 (cp_parser *);
1531 static tree cp_parser_parameter_declaration_list
1532 (cp_parser *);
1533 static tree cp_parser_parameter_declaration
1534 (cp_parser *, bool);
1535 static tree cp_parser_function_definition
1536 (cp_parser *, bool *);
1537 static void cp_parser_function_body
1538 (cp_parser *);
1539 static tree cp_parser_initializer
1540 (cp_parser *, bool *);
1541 static tree cp_parser_initializer_clause
1542 (cp_parser *);
1543 static tree cp_parser_initializer_list
1544 (cp_parser *);
1546 static bool cp_parser_ctor_initializer_opt_and_function_body
1547 (cp_parser *);
1549 /* Classes [gram.class] */
1551 static tree cp_parser_class_name
1552 (cp_parser *, bool, bool, bool, bool, bool);
1553 static tree cp_parser_class_specifier
1554 (cp_parser *);
1555 static tree cp_parser_class_head
1556 (cp_parser *, bool *);
1557 static enum tag_types cp_parser_class_key
1558 (cp_parser *);
1559 static void cp_parser_member_specification_opt
1560 (cp_parser *);
1561 static void cp_parser_member_declaration
1562 (cp_parser *);
1563 static tree cp_parser_pure_specifier
1564 (cp_parser *);
1565 static tree cp_parser_constant_initializer
1566 (cp_parser *);
1568 /* Derived classes [gram.class.derived] */
1570 static tree cp_parser_base_clause
1571 (cp_parser *);
1572 static tree cp_parser_base_specifier
1573 (cp_parser *);
1575 /* Special member functions [gram.special] */
1577 static tree cp_parser_conversion_function_id
1578 (cp_parser *);
1579 static tree cp_parser_conversion_type_id
1580 (cp_parser *);
1581 static tree cp_parser_conversion_declarator_opt
1582 (cp_parser *);
1583 static bool cp_parser_ctor_initializer_opt
1584 (cp_parser *);
1585 static void cp_parser_mem_initializer_list
1586 (cp_parser *);
1587 static tree cp_parser_mem_initializer
1588 (cp_parser *);
1589 static tree cp_parser_mem_initializer_id
1590 (cp_parser *);
1592 /* Overloading [gram.over] */
1594 static tree cp_parser_operator_function_id
1595 (cp_parser *);
1596 static tree cp_parser_operator
1597 (cp_parser *);
1599 /* Templates [gram.temp] */
1601 static void cp_parser_template_declaration
1602 (cp_parser *, bool);
1603 static tree cp_parser_template_parameter_list
1604 (cp_parser *);
1605 static tree cp_parser_template_parameter
1606 (cp_parser *);
1607 static tree cp_parser_type_parameter
1608 (cp_parser *);
1609 static tree cp_parser_template_id
1610 (cp_parser *, bool, bool);
1611 static tree cp_parser_template_name
1612 (cp_parser *, bool, bool);
1613 static tree cp_parser_template_argument_list
1614 (cp_parser *);
1615 static tree cp_parser_template_argument
1616 (cp_parser *);
1617 static void cp_parser_explicit_instantiation
1618 (cp_parser *);
1619 static void cp_parser_explicit_specialization
1620 (cp_parser *);
1622 /* Exception handling [gram.exception] */
1624 static tree cp_parser_try_block
1625 (cp_parser *);
1626 static bool cp_parser_function_try_block
1627 (cp_parser *);
1628 static void cp_parser_handler_seq
1629 (cp_parser *);
1630 static void cp_parser_handler
1631 (cp_parser *);
1632 static tree cp_parser_exception_declaration
1633 (cp_parser *);
1634 static tree cp_parser_throw_expression
1635 (cp_parser *);
1636 static tree cp_parser_exception_specification_opt
1637 (cp_parser *);
1638 static tree cp_parser_type_id_list
1639 (cp_parser *);
1641 /* GNU Extensions */
1643 static tree cp_parser_asm_specification_opt
1644 (cp_parser *);
1645 static tree cp_parser_asm_operand_list
1646 (cp_parser *);
1647 static tree cp_parser_asm_clobber_list
1648 (cp_parser *);
1649 static tree cp_parser_attributes_opt
1650 (cp_parser *);
1651 static tree cp_parser_attribute_list
1652 (cp_parser *);
1653 static bool cp_parser_extension_opt
1654 (cp_parser *, int *);
1655 static void cp_parser_label_declaration
1656 (cp_parser *);
1658 /* Utility Routines */
1660 static tree cp_parser_lookup_name
1661 (cp_parser *, tree, bool, bool, bool);
1662 static tree cp_parser_lookup_name_simple
1663 (cp_parser *, tree);
1664 static tree cp_parser_maybe_treat_template_as_class
1665 (tree, bool);
1666 static bool cp_parser_check_declarator_template_parameters
1667 (cp_parser *, tree);
1668 static bool cp_parser_check_template_parameters
1669 (cp_parser *, unsigned);
1670 static tree cp_parser_binary_expression
1671 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1672 static tree cp_parser_global_scope_opt
1673 (cp_parser *, bool);
1674 static bool cp_parser_constructor_declarator_p
1675 (cp_parser *, bool);
1676 static tree cp_parser_function_definition_from_specifiers_and_declarator
1677 (cp_parser *, tree, tree, tree);
1678 static tree cp_parser_function_definition_after_declarator
1679 (cp_parser *, bool);
1680 static void cp_parser_template_declaration_after_export
1681 (cp_parser *, bool);
1682 static tree cp_parser_single_declaration
1683 (cp_parser *, bool, bool *);
1684 static tree cp_parser_functional_cast
1685 (cp_parser *, tree);
1686 static void cp_parser_save_default_args
1687 (cp_parser *, tree);
1688 static void cp_parser_late_parsing_for_member
1689 (cp_parser *, tree);
1690 static void cp_parser_late_parsing_default_args
1691 (cp_parser *, tree);
1692 static tree cp_parser_sizeof_operand
1693 (cp_parser *, enum rid);
1694 static bool cp_parser_declares_only_class_p
1695 (cp_parser *);
1696 static bool cp_parser_friend_p
1697 (tree);
1698 static cp_token *cp_parser_require
1699 (cp_parser *, enum cpp_ttype, const char *);
1700 static cp_token *cp_parser_require_keyword
1701 (cp_parser *, enum rid, const char *);
1702 static bool cp_parser_token_starts_function_definition_p
1703 (cp_token *);
1704 static bool cp_parser_next_token_starts_class_definition_p
1705 (cp_parser *);
1706 static enum tag_types cp_parser_token_is_class_key
1707 (cp_token *);
1708 static void cp_parser_check_class_key
1709 (enum tag_types, tree type);
1710 static bool cp_parser_optional_template_keyword
1711 (cp_parser *);
1712 static void cp_parser_pre_parsed_nested_name_specifier
1713 (cp_parser *);
1714 static void cp_parser_cache_group
1715 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1716 static void cp_parser_parse_tentatively
1717 (cp_parser *);
1718 static void cp_parser_commit_to_tentative_parse
1719 (cp_parser *);
1720 static void cp_parser_abort_tentative_parse
1721 (cp_parser *);
1722 static bool cp_parser_parse_definitely
1723 (cp_parser *);
1724 static inline bool cp_parser_parsing_tentatively
1725 (cp_parser *);
1726 static bool cp_parser_committed_to_tentative_parse
1727 (cp_parser *);
1728 static void cp_parser_error
1729 (cp_parser *, const char *);
1730 static bool cp_parser_simulate_error
1731 (cp_parser *);
1732 static void cp_parser_check_type_definition
1733 (cp_parser *);
1734 static tree cp_parser_non_constant_expression
1735 (const char *);
1736 static tree cp_parser_non_constant_id_expression
1737 (tree);
1738 static bool cp_parser_diagnose_invalid_type_name
1739 (cp_parser *);
1740 static bool cp_parser_skip_to_closing_parenthesis
1741 (cp_parser *);
1742 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1743 (cp_parser *);
1744 static void cp_parser_skip_to_end_of_statement
1745 (cp_parser *);
1746 static void cp_parser_consume_semicolon_at_end_of_statement
1747 (cp_parser *);
1748 static void cp_parser_skip_to_end_of_block_or_statement
1749 (cp_parser *);
1750 static void cp_parser_skip_to_closing_brace
1751 (cp_parser *);
1752 static void cp_parser_skip_until_found
1753 (cp_parser *, enum cpp_ttype, const char *);
1754 static bool cp_parser_error_occurred
1755 (cp_parser *);
1756 static bool cp_parser_allow_gnu_extensions_p
1757 (cp_parser *);
1758 static bool cp_parser_is_string_literal
1759 (cp_token *);
1760 static bool cp_parser_is_keyword
1761 (cp_token *, enum rid);
1762 static tree cp_parser_scope_through_which_access_occurs
1763 (tree, tree, tree);
1765 /* Returns nonzero if we are parsing tentatively. */
1767 static inline bool
1768 cp_parser_parsing_tentatively (cp_parser* parser)
1770 return parser->context->next != NULL;
1773 /* Returns nonzero if TOKEN is a string literal. */
1775 static bool
1776 cp_parser_is_string_literal (cp_token* token)
1778 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1781 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1783 static bool
1784 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1786 return token->keyword == keyword;
1789 /* Returns the scope through which DECL is being accessed, or
1790 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
1791 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
1792 or `x', respectively. If the DECL was named as `A::B' then
1793 NESTED_NAME_SPECIFIER is `A'. */
1795 static tree
1796 cp_parser_scope_through_which_access_occurs (tree decl,
1797 tree object_type,
1798 tree nested_name_specifier)
1800 tree scope;
1801 tree qualifying_type = NULL_TREE;
1803 /* Determine the SCOPE of DECL. */
1804 scope = context_for_name_lookup (decl);
1805 /* If the SCOPE is not a type, then DECL is not a member. */
1806 if (!TYPE_P (scope))
1807 return NULL_TREE;
1808 /* Figure out the type through which DECL is being accessed. */
1809 if (object_type
1810 /* OBJECT_TYPE might not be a class type; consider:
1812 class A { typedef int I; };
1813 I *p;
1814 p->A::I::~I();
1816 In this case, we will have "A::I" as the DECL, but "I" as the
1817 OBJECT_TYPE. */
1818 && CLASS_TYPE_P (object_type)
1819 && DERIVED_FROM_P (scope, object_type))
1820 /* If we are processing a `->' or `.' expression, use the type of the
1821 left-hand side. */
1822 qualifying_type = object_type;
1823 else if (nested_name_specifier)
1825 /* If the reference is to a non-static member of the
1826 current class, treat it as if it were referenced through
1827 `this'. */
1828 if (DECL_NONSTATIC_MEMBER_P (decl)
1829 && current_class_ptr
1830 && DERIVED_FROM_P (scope, current_class_type))
1831 qualifying_type = current_class_type;
1832 /* Otherwise, use the type indicated by the
1833 nested-name-specifier. */
1834 else
1835 qualifying_type = nested_name_specifier;
1837 else
1838 /* Otherwise, the name must be from the current class or one of
1839 its bases. */
1840 qualifying_type = currently_open_derived_class (scope);
1842 return qualifying_type;
1845 /* Issue the indicated error MESSAGE. */
1847 static void
1848 cp_parser_error (cp_parser* parser, const char* message)
1850 /* Output the MESSAGE -- unless we're parsing tentatively. */
1851 if (!cp_parser_simulate_error (parser))
1852 error (message);
1855 /* If we are parsing tentatively, remember that an error has occurred
1856 during this tentative parse. Returns true if the error was
1857 simulated; false if a messgae should be issued by the caller. */
1859 static bool
1860 cp_parser_simulate_error (cp_parser* parser)
1862 if (cp_parser_parsing_tentatively (parser)
1863 && !cp_parser_committed_to_tentative_parse (parser))
1865 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1866 return true;
1868 return false;
1871 /* This function is called when a type is defined. If type
1872 definitions are forbidden at this point, an error message is
1873 issued. */
1875 static void
1876 cp_parser_check_type_definition (cp_parser* parser)
1878 /* If types are forbidden here, issue a message. */
1879 if (parser->type_definition_forbidden_message)
1880 /* Use `%s' to print the string in case there are any escape
1881 characters in the message. */
1882 error ("%s", parser->type_definition_forbidden_message);
1885 /* Issue an eror message about the fact that THING appeared in a
1886 constant-expression. Returns ERROR_MARK_NODE. */
1888 static tree
1889 cp_parser_non_constant_expression (const char *thing)
1891 error ("%s cannot appear in a constant-expression", thing);
1892 return error_mark_node;
1895 /* Issue an eror message about the fact that DECL appeared in a
1896 constant-expression. Returns ERROR_MARK_NODE. */
1898 static tree
1899 cp_parser_non_constant_id_expression (tree decl)
1901 error ("`%D' cannot appear in a constant-expression", decl);
1902 return error_mark_node;
1905 /* Check for a common situation where a type-name should be present,
1906 but is not, and issue a sensible error message. Returns true if an
1907 invalid type-name was detected. */
1909 static bool
1910 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1912 /* If the next two tokens are both identifiers, the code is
1913 erroneous. The usual cause of this situation is code like:
1915 T t;
1917 where "T" should name a type -- but does not. */
1918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1919 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1921 tree name;
1923 /* If parsing tentatively, we should commit; we really are
1924 looking at a declaration. */
1925 /* Consume the first identifier. */
1926 name = cp_lexer_consume_token (parser->lexer)->value;
1927 /* Issue an error message. */
1928 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1929 /* If we're in a template class, it's possible that the user was
1930 referring to a type from a base class. For example:
1932 template <typename T> struct A { typedef T X; };
1933 template <typename T> struct B : public A<T> { X x; };
1935 The user should have said "typename A<T>::X". */
1936 if (processing_template_decl && current_class_type)
1938 tree b;
1940 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1942 b = TREE_CHAIN (b))
1944 tree base_type = BINFO_TYPE (b);
1945 if (CLASS_TYPE_P (base_type)
1946 && dependent_type_p (base_type))
1948 tree field;
1949 /* Go from a particular instantiation of the
1950 template (which will have an empty TYPE_FIELDs),
1951 to the main version. */
1952 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1953 for (field = TYPE_FIELDS (base_type);
1954 field;
1955 field = TREE_CHAIN (field))
1956 if (TREE_CODE (field) == TYPE_DECL
1957 && DECL_NAME (field) == name)
1959 error ("(perhaps `typename %T::%s' was intended)",
1960 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1961 break;
1963 if (field)
1964 break;
1968 /* Skip to the end of the declaration; there's no point in
1969 trying to process it. */
1970 cp_parser_skip_to_end_of_statement (parser);
1972 return true;
1975 return false;
1978 /* Consume tokens up to, and including, the next non-nested closing `)'.
1979 Returns TRUE iff we found a closing `)'. */
1981 static bool
1982 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
1984 unsigned nesting_depth = 0;
1986 while (true)
1988 cp_token *token;
1990 /* If we've run out of tokens, then there is no closing `)'. */
1991 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1992 return false;
1993 /* Consume the token. */
1994 token = cp_lexer_consume_token (parser->lexer);
1995 /* If it is an `(', we have entered another level of nesting. */
1996 if (token->type == CPP_OPEN_PAREN)
1997 ++nesting_depth;
1998 /* If it is a `)', then we might be done. */
1999 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2000 return true;
2004 /* Consume tokens until the next token is a `)', or a `,'. Returns
2005 TRUE if the next token is a `,'. */
2007 static bool
2008 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2010 unsigned nesting_depth = 0;
2012 while (true)
2014 cp_token *token = cp_lexer_peek_token (parser->lexer);
2016 /* If we've run out of tokens, then there is no closing `)'. */
2017 if (token->type == CPP_EOF)
2018 return false;
2019 /* If it is a `,' stop. */
2020 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2021 return true;
2022 /* If it is a `)', stop. */
2023 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2024 return false;
2025 /* If it is an `(', we have entered another level of nesting. */
2026 else if (token->type == CPP_OPEN_PAREN)
2027 ++nesting_depth;
2028 /* Consume the token. */
2029 token = cp_lexer_consume_token (parser->lexer);
2033 /* Consume tokens until we reach the end of the current statement.
2034 Normally, that will be just before consuming a `;'. However, if a
2035 non-nested `}' comes first, then we stop before consuming that. */
2037 static void
2038 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2040 unsigned nesting_depth = 0;
2042 while (true)
2044 cp_token *token;
2046 /* Peek at the next token. */
2047 token = cp_lexer_peek_token (parser->lexer);
2048 /* If we've run out of tokens, stop. */
2049 if (token->type == CPP_EOF)
2050 break;
2051 /* If the next token is a `;', we have reached the end of the
2052 statement. */
2053 if (token->type == CPP_SEMICOLON && !nesting_depth)
2054 break;
2055 /* If the next token is a non-nested `}', then we have reached
2056 the end of the current block. */
2057 if (token->type == CPP_CLOSE_BRACE)
2059 /* If this is a non-nested `}', stop before consuming it.
2060 That way, when confronted with something like:
2062 { 3 + }
2064 we stop before consuming the closing `}', even though we
2065 have not yet reached a `;'. */
2066 if (nesting_depth == 0)
2067 break;
2068 /* If it is the closing `}' for a block that we have
2069 scanned, stop -- but only after consuming the token.
2070 That way given:
2072 void f g () { ... }
2073 typedef int I;
2075 we will stop after the body of the erroneously declared
2076 function, but before consuming the following `typedef'
2077 declaration. */
2078 if (--nesting_depth == 0)
2080 cp_lexer_consume_token (parser->lexer);
2081 break;
2084 /* If it the next token is a `{', then we are entering a new
2085 block. Consume the entire block. */
2086 else if (token->type == CPP_OPEN_BRACE)
2087 ++nesting_depth;
2088 /* Consume the token. */
2089 cp_lexer_consume_token (parser->lexer);
2093 /* This function is called at the end of a statement or declaration.
2094 If the next token is a semicolon, it is consumed; otherwise, error
2095 recovery is attempted. */
2097 static void
2098 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2100 /* Look for the trailing `;'. */
2101 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2103 /* If there is additional (erroneous) input, skip to the end of
2104 the statement. */
2105 cp_parser_skip_to_end_of_statement (parser);
2106 /* If the next token is now a `;', consume it. */
2107 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2108 cp_lexer_consume_token (parser->lexer);
2112 /* Skip tokens until we have consumed an entire block, or until we
2113 have consumed a non-nested `;'. */
2115 static void
2116 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2118 unsigned nesting_depth = 0;
2120 while (true)
2122 cp_token *token;
2124 /* Peek at the next token. */
2125 token = cp_lexer_peek_token (parser->lexer);
2126 /* If we've run out of tokens, stop. */
2127 if (token->type == CPP_EOF)
2128 break;
2129 /* If the next token is a `;', we have reached the end of the
2130 statement. */
2131 if (token->type == CPP_SEMICOLON && !nesting_depth)
2133 /* Consume the `;'. */
2134 cp_lexer_consume_token (parser->lexer);
2135 break;
2137 /* Consume the token. */
2138 token = cp_lexer_consume_token (parser->lexer);
2139 /* If the next token is a non-nested `}', then we have reached
2140 the end of the current block. */
2141 if (token->type == CPP_CLOSE_BRACE
2142 && (nesting_depth == 0 || --nesting_depth == 0))
2143 break;
2144 /* If it the next token is a `{', then we are entering a new
2145 block. Consume the entire block. */
2146 if (token->type == CPP_OPEN_BRACE)
2147 ++nesting_depth;
2151 /* Skip tokens until a non-nested closing curly brace is the next
2152 token. */
2154 static void
2155 cp_parser_skip_to_closing_brace (cp_parser *parser)
2157 unsigned nesting_depth = 0;
2159 while (true)
2161 cp_token *token;
2163 /* Peek at the next token. */
2164 token = cp_lexer_peek_token (parser->lexer);
2165 /* If we've run out of tokens, stop. */
2166 if (token->type == CPP_EOF)
2167 break;
2168 /* If the next token is a non-nested `}', then we have reached
2169 the end of the current block. */
2170 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2171 break;
2172 /* If it the next token is a `{', then we are entering a new
2173 block. Consume the entire block. */
2174 else if (token->type == CPP_OPEN_BRACE)
2175 ++nesting_depth;
2176 /* Consume the token. */
2177 cp_lexer_consume_token (parser->lexer);
2181 /* Create a new C++ parser. */
2183 static cp_parser *
2184 cp_parser_new (void)
2186 cp_parser *parser;
2187 cp_lexer *lexer;
2189 /* cp_lexer_new_main is called before calling ggc_alloc because
2190 cp_lexer_new_main might load a PCH file. */
2191 lexer = cp_lexer_new_main ();
2193 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2194 parser->lexer = lexer;
2195 parser->context = cp_parser_context_new (NULL);
2197 /* For now, we always accept GNU extensions. */
2198 parser->allow_gnu_extensions_p = 1;
2200 /* The `>' token is a greater-than operator, not the end of a
2201 template-id. */
2202 parser->greater_than_is_operator_p = true;
2204 parser->default_arg_ok_p = true;
2206 /* We are not parsing a constant-expression. */
2207 parser->constant_expression_p = false;
2208 parser->allow_non_constant_expression_p = false;
2209 parser->non_constant_expression_p = false;
2211 /* Local variable names are not forbidden. */
2212 parser->local_variables_forbidden_p = false;
2214 /* We are not procesing an `extern "C"' declaration. */
2215 parser->in_unbraced_linkage_specification_p = false;
2217 /* We are not processing a declarator. */
2218 parser->in_declarator_p = false;
2220 /* The unparsed function queue is empty. */
2221 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2223 /* There are no classes being defined. */
2224 parser->num_classes_being_defined = 0;
2226 /* No template parameters apply. */
2227 parser->num_template_parameter_lists = 0;
2229 return parser;
2232 /* Lexical conventions [gram.lex] */
2234 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2235 identifier. */
2237 static tree
2238 cp_parser_identifier (cp_parser* parser)
2240 cp_token *token;
2242 /* Look for the identifier. */
2243 token = cp_parser_require (parser, CPP_NAME, "identifier");
2244 /* Return the value. */
2245 return token ? token->value : error_mark_node;
2248 /* Basic concepts [gram.basic] */
2250 /* Parse a translation-unit.
2252 translation-unit:
2253 declaration-seq [opt]
2255 Returns TRUE if all went well. */
2257 static bool
2258 cp_parser_translation_unit (cp_parser* parser)
2260 while (true)
2262 cp_parser_declaration_seq_opt (parser);
2264 /* If there are no tokens left then all went well. */
2265 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2266 break;
2268 /* Otherwise, issue an error message. */
2269 cp_parser_error (parser, "expected declaration");
2270 return false;
2273 /* Consume the EOF token. */
2274 cp_parser_require (parser, CPP_EOF, "end-of-file");
2276 /* Finish up. */
2277 finish_translation_unit ();
2279 /* All went well. */
2280 return true;
2283 /* Expressions [gram.expr] */
2285 /* Parse a primary-expression.
2287 primary-expression:
2288 literal
2289 this
2290 ( expression )
2291 id-expression
2293 GNU Extensions:
2295 primary-expression:
2296 ( compound-statement )
2297 __builtin_va_arg ( assignment-expression , type-id )
2299 literal:
2300 __null
2302 Returns a representation of the expression.
2304 *IDK indicates what kind of id-expression (if any) was present.
2306 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2307 used as the operand of a pointer-to-member. In that case,
2308 *QUALIFYING_CLASS gives the class that is used as the qualifying
2309 class in the pointer-to-member. */
2311 static tree
2312 cp_parser_primary_expression (cp_parser *parser,
2313 cp_parser_id_kind *idk,
2314 tree *qualifying_class)
2316 cp_token *token;
2318 /* Assume the primary expression is not an id-expression. */
2319 *idk = CP_PARSER_ID_KIND_NONE;
2320 /* And that it cannot be used as pointer-to-member. */
2321 *qualifying_class = NULL_TREE;
2323 /* Peek at the next token. */
2324 token = cp_lexer_peek_token (parser->lexer);
2325 switch (token->type)
2327 /* literal:
2328 integer-literal
2329 character-literal
2330 floating-literal
2331 string-literal
2332 boolean-literal */
2333 case CPP_CHAR:
2334 case CPP_WCHAR:
2335 case CPP_STRING:
2336 case CPP_WSTRING:
2337 case CPP_NUMBER:
2338 token = cp_lexer_consume_token (parser->lexer);
2339 return token->value;
2341 case CPP_OPEN_PAREN:
2343 tree expr;
2344 bool saved_greater_than_is_operator_p;
2346 /* Consume the `('. */
2347 cp_lexer_consume_token (parser->lexer);
2348 /* Within a parenthesized expression, a `>' token is always
2349 the greater-than operator. */
2350 saved_greater_than_is_operator_p
2351 = parser->greater_than_is_operator_p;
2352 parser->greater_than_is_operator_p = true;
2353 /* If we see `( { ' then we are looking at the beginning of
2354 a GNU statement-expression. */
2355 if (cp_parser_allow_gnu_extensions_p (parser)
2356 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2358 /* Statement-expressions are not allowed by the standard. */
2359 if (pedantic)
2360 pedwarn ("ISO C++ forbids braced-groups within expressions");
2362 /* And they're not allowed outside of a function-body; you
2363 cannot, for example, write:
2365 int i = ({ int j = 3; j + 1; });
2367 at class or namespace scope. */
2368 if (!at_function_scope_p ())
2369 error ("statement-expressions are allowed only inside functions");
2370 /* Start the statement-expression. */
2371 expr = begin_stmt_expr ();
2372 /* Parse the compound-statement. */
2373 cp_parser_compound_statement (parser);
2374 /* Finish up. */
2375 expr = finish_stmt_expr (expr);
2377 else
2379 /* Parse the parenthesized expression. */
2380 expr = cp_parser_expression (parser);
2381 /* Let the front end know that this expression was
2382 enclosed in parentheses. This matters in case, for
2383 example, the expression is of the form `A::B', since
2384 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2385 not. */
2386 finish_parenthesized_expr (expr);
2388 /* The `>' token might be the end of a template-id or
2389 template-parameter-list now. */
2390 parser->greater_than_is_operator_p
2391 = saved_greater_than_is_operator_p;
2392 /* Consume the `)'. */
2393 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2394 cp_parser_skip_to_end_of_statement (parser);
2396 return expr;
2399 case CPP_KEYWORD:
2400 switch (token->keyword)
2402 /* These two are the boolean literals. */
2403 case RID_TRUE:
2404 cp_lexer_consume_token (parser->lexer);
2405 return boolean_true_node;
2406 case RID_FALSE:
2407 cp_lexer_consume_token (parser->lexer);
2408 return boolean_false_node;
2410 /* The `__null' literal. */
2411 case RID_NULL:
2412 cp_lexer_consume_token (parser->lexer);
2413 return null_node;
2415 /* Recognize the `this' keyword. */
2416 case RID_THIS:
2417 cp_lexer_consume_token (parser->lexer);
2418 if (parser->local_variables_forbidden_p)
2420 error ("`this' may not be used in this context");
2421 return error_mark_node;
2423 /* Pointers cannot appear in constant-expressions. */
2424 if (parser->constant_expression_p)
2426 if (!parser->allow_non_constant_expression_p)
2427 return cp_parser_non_constant_expression ("`this'");
2428 parser->non_constant_expression_p = true;
2430 return finish_this_expr ();
2432 /* The `operator' keyword can be the beginning of an
2433 id-expression. */
2434 case RID_OPERATOR:
2435 goto id_expression;
2437 case RID_FUNCTION_NAME:
2438 case RID_PRETTY_FUNCTION_NAME:
2439 case RID_C99_FUNCTION_NAME:
2440 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2441 __func__ are the names of variables -- but they are
2442 treated specially. Therefore, they are handled here,
2443 rather than relying on the generic id-expression logic
2444 below. Gramatically, these names are id-expressions.
2446 Consume the token. */
2447 token = cp_lexer_consume_token (parser->lexer);
2448 /* Look up the name. */
2449 return finish_fname (token->value);
2451 case RID_VA_ARG:
2453 tree expression;
2454 tree type;
2456 /* The `__builtin_va_arg' construct is used to handle
2457 `va_arg'. Consume the `__builtin_va_arg' token. */
2458 cp_lexer_consume_token (parser->lexer);
2459 /* Look for the opening `('. */
2460 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2461 /* Now, parse the assignment-expression. */
2462 expression = cp_parser_assignment_expression (parser);
2463 /* Look for the `,'. */
2464 cp_parser_require (parser, CPP_COMMA, "`,'");
2465 /* Parse the type-id. */
2466 type = cp_parser_type_id (parser);
2467 /* Look for the closing `)'. */
2468 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2469 /* Using `va_arg' in a constant-expression is not
2470 allowed. */
2471 if (parser->constant_expression_p)
2473 if (!parser->allow_non_constant_expression_p)
2474 return cp_parser_non_constant_expression ("`va_arg'");
2475 parser->non_constant_expression_p = true;
2477 return build_x_va_arg (expression, type);
2480 default:
2481 cp_parser_error (parser, "expected primary-expression");
2482 return error_mark_node;
2484 /* Fall through. */
2486 /* An id-expression can start with either an identifier, a
2487 `::' as the beginning of a qualified-id, or the "operator"
2488 keyword. */
2489 case CPP_NAME:
2490 case CPP_SCOPE:
2491 case CPP_TEMPLATE_ID:
2492 case CPP_NESTED_NAME_SPECIFIER:
2494 tree id_expression;
2495 tree decl;
2497 id_expression:
2498 /* Parse the id-expression. */
2499 id_expression
2500 = cp_parser_id_expression (parser,
2501 /*template_keyword_p=*/false,
2502 /*check_dependency_p=*/true,
2503 /*template_p=*/NULL);
2504 if (id_expression == error_mark_node)
2505 return error_mark_node;
2506 /* If we have a template-id, then no further lookup is
2507 required. If the template-id was for a template-class, we
2508 will sometimes have a TYPE_DECL at this point. */
2509 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2510 || TREE_CODE (id_expression) == TYPE_DECL)
2511 decl = id_expression;
2512 /* Look up the name. */
2513 else
2515 decl = cp_parser_lookup_name_simple (parser, id_expression);
2516 /* If name lookup gives us a SCOPE_REF, then the
2517 qualifying scope was dependent. Just propagate the
2518 name. */
2519 if (TREE_CODE (decl) == SCOPE_REF)
2521 if (TYPE_P (TREE_OPERAND (decl, 0)))
2522 *qualifying_class = TREE_OPERAND (decl, 0);
2523 /* Since this name was dependent, the expression isn't
2524 constant -- yet. No error is issued because it
2525 might be constant when things are instantiated. */
2526 if (parser->constant_expression_p)
2527 parser->non_constant_expression_p = true;
2528 return decl;
2530 /* Check to see if DECL is a local variable in a context
2531 where that is forbidden. */
2532 if (parser->local_variables_forbidden_p
2533 && local_variable_p (decl))
2535 /* It might be that we only found DECL because we are
2536 trying to be generous with pre-ISO scoping rules.
2537 For example, consider:
2539 int i;
2540 void g() {
2541 for (int i = 0; i < 10; ++i) {}
2542 extern void f(int j = i);
2545 Here, name look up will originally find the out
2546 of scope `i'. We need to issue a warning message,
2547 but then use the global `i'. */
2548 decl = check_for_out_of_scope_variable (decl);
2549 if (local_variable_p (decl))
2551 error ("local variable `%D' may not appear in this context",
2552 decl);
2553 return error_mark_node;
2557 if (decl == error_mark_node)
2559 /* Name lookup failed. */
2560 if (!parser->scope
2561 && processing_template_decl)
2563 /* Unqualified name lookup failed while processing a
2564 template. */
2565 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2566 /* If the next token is a parenthesis, assume that
2567 Koenig lookup will succeed when instantiating the
2568 template. */
2569 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
2570 return build_min_nt (LOOKUP_EXPR, id_expression);
2571 /* If we're not doing Koenig lookup, issue an error. */
2572 error ("`%D' has not been declared", id_expression);
2573 return error_mark_node;
2575 else if (parser->scope
2576 && (!TYPE_P (parser->scope)
2577 || !dependent_type_p (parser->scope)))
2579 /* Qualified name lookup failed, and the
2580 qualifying name was not a dependent type. That
2581 is always an error. */
2582 if (TYPE_P (parser->scope)
2583 && !COMPLETE_TYPE_P (parser->scope))
2584 error ("incomplete type `%T' used in nested name "
2585 "specifier",
2586 parser->scope);
2587 else if (parser->scope != global_namespace)
2588 error ("`%D' is not a member of `%D'",
2589 id_expression, parser->scope);
2590 else
2591 error ("`::%D' has not been declared", id_expression);
2592 return error_mark_node;
2594 else if (!parser->scope && !processing_template_decl)
2596 /* It may be resolvable as a koenig lookup function
2597 call. */
2598 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2599 return id_expression;
2602 /* If DECL is a variable that would be out of scope under
2603 ANSI/ISO rules, but in scope in the ARM, name lookup
2604 will succeed. Issue a diagnostic here. */
2605 else
2606 decl = check_for_out_of_scope_variable (decl);
2608 /* Remember that the name was used in the definition of
2609 the current class so that we can check later to see if
2610 the meaning would have been different after the class
2611 was entirely defined. */
2612 if (!parser->scope && decl != error_mark_node)
2613 maybe_note_name_used_in_class (id_expression, decl);
2616 /* If we didn't find anything, or what we found was a type,
2617 then this wasn't really an id-expression. */
2618 if (TREE_CODE (decl) == TEMPLATE_DECL
2619 && !DECL_FUNCTION_TEMPLATE_P (decl))
2621 cp_parser_error (parser, "missing template arguments");
2622 return error_mark_node;
2624 else if (TREE_CODE (decl) == TYPE_DECL
2625 || TREE_CODE (decl) == NAMESPACE_DECL)
2627 cp_parser_error (parser,
2628 "expected primary-expression");
2629 return error_mark_node;
2632 /* If the name resolved to a template parameter, there is no
2633 need to look it up again later. Similarly, we resolve
2634 enumeration constants to their underlying values. */
2635 if (TREE_CODE (decl) == CONST_DECL)
2637 *idk = CP_PARSER_ID_KIND_NONE;
2638 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2639 return DECL_INITIAL (decl);
2640 return decl;
2642 else
2644 bool dependent_p;
2646 /* If the declaration was explicitly qualified indicate
2647 that. The semantics of `A::f(3)' are different than
2648 `f(3)' if `f' is virtual. */
2649 *idk = (parser->scope
2650 ? CP_PARSER_ID_KIND_QUALIFIED
2651 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2652 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2653 : CP_PARSER_ID_KIND_UNQUALIFIED));
2656 /* [temp.dep.expr]
2658 An id-expression is type-dependent if it contains an
2659 identifier that was declared with a dependent type.
2661 As an optimization, we could choose not to create a
2662 LOOKUP_EXPR for a name that resolved to a local
2663 variable in the template function that we are currently
2664 declaring; such a name cannot ever resolve to anything
2665 else. If we did that we would not have to look up
2666 these names at instantiation time.
2668 The standard is not very specific about an
2669 id-expression that names a set of overloaded functions.
2670 What if some of them have dependent types and some of
2671 them do not? Presumably, such a name should be treated
2672 as a dependent name. */
2673 /* Assume the name is not dependent. */
2674 dependent_p = false;
2675 if (!processing_template_decl)
2676 /* No names are dependent outside a template. */
2678 /* A template-id where the name of the template was not
2679 resolved is definitely dependent. */
2680 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2681 && (TREE_CODE (TREE_OPERAND (decl, 0))
2682 == IDENTIFIER_NODE))
2683 dependent_p = true;
2684 /* For anything except an overloaded function, just check
2685 its type. */
2686 else if (!is_overloaded_fn (decl))
2687 dependent_p
2688 = dependent_type_p (TREE_TYPE (decl));
2689 /* For a set of overloaded functions, check each of the
2690 functions. */
2691 else
2693 tree fns = decl;
2695 if (BASELINK_P (fns))
2696 fns = BASELINK_FUNCTIONS (fns);
2698 /* For a template-id, check to see if the template
2699 arguments are dependent. */
2700 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2702 tree args = TREE_OPERAND (fns, 1);
2704 if (args && TREE_CODE (args) == TREE_LIST)
2706 while (args)
2708 if (dependent_template_arg_p (TREE_VALUE (args)))
2710 dependent_p = true;
2711 break;
2713 args = TREE_CHAIN (args);
2716 else if (args && TREE_CODE (args) == TREE_VEC)
2718 int i;
2719 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2720 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2722 dependent_p = true;
2723 break;
2727 /* The functions are those referred to by the
2728 template-id. */
2729 fns = TREE_OPERAND (fns, 0);
2732 /* If there are no dependent template arguments, go
2733 through the overlaoded functions. */
2734 while (fns && !dependent_p)
2736 tree fn = OVL_CURRENT (fns);
2738 /* Member functions of dependent classes are
2739 dependent. */
2740 if (TREE_CODE (fn) == FUNCTION_DECL
2741 && type_dependent_expression_p (fn))
2742 dependent_p = true;
2743 else if (TREE_CODE (fn) == TEMPLATE_DECL
2744 && dependent_template_p (fn))
2745 dependent_p = true;
2747 fns = OVL_NEXT (fns);
2751 /* If the name was dependent on a template parameter,
2752 we will resolve the name at instantiation time. */
2753 if (dependent_p)
2755 /* Create a SCOPE_REF for qualified names. */
2756 if (parser->scope)
2758 if (TYPE_P (parser->scope))
2759 *qualifying_class = parser->scope;
2760 /* Since this name was dependent, the expression isn't
2761 constant -- yet. No error is issued because it
2762 might be constant when things are instantiated. */
2763 if (parser->constant_expression_p)
2764 parser->non_constant_expression_p = true;
2765 return build_nt (SCOPE_REF,
2766 parser->scope,
2767 id_expression);
2769 /* A TEMPLATE_ID already contains all the information
2770 we need. */
2771 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2772 return id_expression;
2773 /* Since this name was dependent, the expression isn't
2774 constant -- yet. No error is issued because it
2775 might be constant when things are instantiated. */
2776 if (parser->constant_expression_p)
2777 parser->non_constant_expression_p = true;
2778 /* Create a LOOKUP_EXPR for other unqualified names. */
2779 return build_min_nt (LOOKUP_EXPR, id_expression);
2782 /* Only certain kinds of names are allowed in constant
2783 expression. Enumerators have already been handled
2784 above. */
2785 if (parser->constant_expression_p
2786 /* Non-type template parameters of integral or
2787 enumeration type. */
2788 && !(TREE_CODE (decl) == TEMPLATE_PARM_INDEX
2789 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl)))
2790 /* Const variables or static data members of integral
2791 or enumeration types initialized with constant
2792 expressions (or dependent expressions - in this case
2793 the check will be done at instantiation time). */
2794 && !(TREE_CODE (decl) == VAR_DECL
2795 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2796 && DECL_INITIAL (decl)
2797 && (TREE_CONSTANT (DECL_INITIAL (decl))
2798 || type_dependent_expression_p
2799 (DECL_INITIAL (decl))
2800 || value_dependent_expression_p
2801 (DECL_INITIAL (decl)))))
2803 if (!parser->allow_non_constant_expression_p)
2804 return cp_parser_non_constant_id_expression (decl);
2805 parser->non_constant_expression_p = true;
2808 if (parser->scope)
2810 decl = (adjust_result_of_qualified_name_lookup
2811 (decl, parser->scope, current_class_type));
2812 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2813 *qualifying_class = parser->scope;
2814 else if (!processing_template_decl)
2815 decl = convert_from_reference (decl);
2817 else
2818 /* Transform references to non-static data members into
2819 COMPONENT_REFs. */
2820 decl = hack_identifier (decl, id_expression);
2822 /* Resolve references to variables of anonymous unions
2823 into COMPONENT_REFs. */
2824 if (TREE_CODE (decl) == ALIAS_DECL)
2825 decl = DECL_INITIAL (decl);
2828 if (TREE_DEPRECATED (decl))
2829 warn_deprecated_use (decl);
2831 return decl;
2834 /* Anything else is an error. */
2835 default:
2836 cp_parser_error (parser, "expected primary-expression");
2837 return error_mark_node;
2841 /* Parse an id-expression.
2843 id-expression:
2844 unqualified-id
2845 qualified-id
2847 qualified-id:
2848 :: [opt] nested-name-specifier template [opt] unqualified-id
2849 :: identifier
2850 :: operator-function-id
2851 :: template-id
2853 Return a representation of the unqualified portion of the
2854 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2855 a `::' or nested-name-specifier.
2857 Often, if the id-expression was a qualified-id, the caller will
2858 want to make a SCOPE_REF to represent the qualified-id. This
2859 function does not do this in order to avoid wastefully creating
2860 SCOPE_REFs when they are not required.
2862 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2863 `template' keyword.
2865 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2866 uninstantiated templates.
2868 If *TEMPLATE_P is non-NULL, it is set to true iff the
2869 `template' keyword is used to explicitly indicate that the entity
2870 named is a template. */
2872 static tree
2873 cp_parser_id_expression (cp_parser *parser,
2874 bool template_keyword_p,
2875 bool check_dependency_p,
2876 bool *template_p)
2878 bool global_scope_p;
2879 bool nested_name_specifier_p;
2881 /* Assume the `template' keyword was not used. */
2882 if (template_p)
2883 *template_p = false;
2885 /* Look for the optional `::' operator. */
2886 global_scope_p
2887 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2888 != NULL_TREE);
2889 /* Look for the optional nested-name-specifier. */
2890 nested_name_specifier_p
2891 = (cp_parser_nested_name_specifier_opt (parser,
2892 /*typename_keyword_p=*/false,
2893 check_dependency_p,
2894 /*type_p=*/false)
2895 != NULL_TREE);
2896 /* If there is a nested-name-specifier, then we are looking at
2897 the first qualified-id production. */
2898 if (nested_name_specifier_p)
2900 tree saved_scope;
2901 tree saved_object_scope;
2902 tree saved_qualifying_scope;
2903 tree unqualified_id;
2904 bool is_template;
2906 /* See if the next token is the `template' keyword. */
2907 if (!template_p)
2908 template_p = &is_template;
2909 *template_p = cp_parser_optional_template_keyword (parser);
2910 /* Name lookup we do during the processing of the
2911 unqualified-id might obliterate SCOPE. */
2912 saved_scope = parser->scope;
2913 saved_object_scope = parser->object_scope;
2914 saved_qualifying_scope = parser->qualifying_scope;
2915 /* Process the final unqualified-id. */
2916 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2917 check_dependency_p);
2918 /* Restore the SAVED_SCOPE for our caller. */
2919 parser->scope = saved_scope;
2920 parser->object_scope = saved_object_scope;
2921 parser->qualifying_scope = saved_qualifying_scope;
2923 return unqualified_id;
2925 /* Otherwise, if we are in global scope, then we are looking at one
2926 of the other qualified-id productions. */
2927 else if (global_scope_p)
2929 cp_token *token;
2930 tree id;
2932 /* Peek at the next token. */
2933 token = cp_lexer_peek_token (parser->lexer);
2935 /* If it's an identifier, and the next token is not a "<", then
2936 we can avoid the template-id case. This is an optimization
2937 for this common case. */
2938 if (token->type == CPP_NAME
2939 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2940 return cp_parser_identifier (parser);
2942 cp_parser_parse_tentatively (parser);
2943 /* Try a template-id. */
2944 id = cp_parser_template_id (parser,
2945 /*template_keyword_p=*/false,
2946 /*check_dependency_p=*/true);
2947 /* If that worked, we're done. */
2948 if (cp_parser_parse_definitely (parser))
2949 return id;
2951 /* Peek at the next token. (Changes in the token buffer may
2952 have invalidated the pointer obtained above.) */
2953 token = cp_lexer_peek_token (parser->lexer);
2955 switch (token->type)
2957 case CPP_NAME:
2958 return cp_parser_identifier (parser);
2960 case CPP_KEYWORD:
2961 if (token->keyword == RID_OPERATOR)
2962 return cp_parser_operator_function_id (parser);
2963 /* Fall through. */
2965 default:
2966 cp_parser_error (parser, "expected id-expression");
2967 return error_mark_node;
2970 else
2971 return cp_parser_unqualified_id (parser, template_keyword_p,
2972 /*check_dependency_p=*/true);
2975 /* Parse an unqualified-id.
2977 unqualified-id:
2978 identifier
2979 operator-function-id
2980 conversion-function-id
2981 ~ class-name
2982 template-id
2984 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2985 keyword, in a construct like `A::template ...'.
2987 Returns a representation of unqualified-id. For the `identifier'
2988 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2989 production a BIT_NOT_EXPR is returned; the operand of the
2990 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2991 other productions, see the documentation accompanying the
2992 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2993 names are looked up in uninstantiated templates. */
2995 static tree
2996 cp_parser_unqualified_id (cp_parser* parser,
2997 bool template_keyword_p,
2998 bool check_dependency_p)
3000 cp_token *token;
3002 /* Peek at the next token. */
3003 token = cp_lexer_peek_token (parser->lexer);
3005 switch (token->type)
3007 case CPP_NAME:
3009 tree id;
3011 /* We don't know yet whether or not this will be a
3012 template-id. */
3013 cp_parser_parse_tentatively (parser);
3014 /* Try a template-id. */
3015 id = cp_parser_template_id (parser, template_keyword_p,
3016 check_dependency_p);
3017 /* If it worked, we're done. */
3018 if (cp_parser_parse_definitely (parser))
3019 return id;
3020 /* Otherwise, it's an ordinary identifier. */
3021 return cp_parser_identifier (parser);
3024 case CPP_TEMPLATE_ID:
3025 return cp_parser_template_id (parser, template_keyword_p,
3026 check_dependency_p);
3028 case CPP_COMPL:
3030 tree type_decl;
3031 tree qualifying_scope;
3032 tree object_scope;
3033 tree scope;
3035 /* Consume the `~' token. */
3036 cp_lexer_consume_token (parser->lexer);
3037 /* Parse the class-name. The standard, as written, seems to
3038 say that:
3040 template <typename T> struct S { ~S (); };
3041 template <typename T> S<T>::~S() {}
3043 is invalid, since `~' must be followed by a class-name, but
3044 `S<T>' is dependent, and so not known to be a class.
3045 That's not right; we need to look in uninstantiated
3046 templates. A further complication arises from:
3048 template <typename T> void f(T t) {
3049 t.T::~T();
3052 Here, it is not possible to look up `T' in the scope of `T'
3053 itself. We must look in both the current scope, and the
3054 scope of the containing complete expression.
3056 Yet another issue is:
3058 struct S {
3059 int S;
3060 ~S();
3063 S::~S() {}
3065 The standard does not seem to say that the `S' in `~S'
3066 should refer to the type `S' and not the data member
3067 `S::S'. */
3069 /* DR 244 says that we look up the name after the "~" in the
3070 same scope as we looked up the qualifying name. That idea
3071 isn't fully worked out; it's more complicated than that. */
3072 scope = parser->scope;
3073 object_scope = parser->object_scope;
3074 qualifying_scope = parser->qualifying_scope;
3076 /* If the name is of the form "X::~X" it's OK. */
3077 if (scope && TYPE_P (scope)
3078 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3079 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3080 == CPP_OPEN_PAREN)
3081 && (cp_lexer_peek_token (parser->lexer)->value
3082 == TYPE_IDENTIFIER (scope)))
3084 cp_lexer_consume_token (parser->lexer);
3085 return build_nt (BIT_NOT_EXPR, scope);
3088 /* If there was an explicit qualification (S::~T), first look
3089 in the scope given by the qualification (i.e., S). */
3090 if (scope)
3092 cp_parser_parse_tentatively (parser);
3093 type_decl = cp_parser_class_name (parser,
3094 /*typename_keyword_p=*/false,
3095 /*template_keyword_p=*/false,
3096 /*type_p=*/false,
3097 /*check_dependency=*/false,
3098 /*class_head_p=*/false);
3099 if (cp_parser_parse_definitely (parser))
3100 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3102 /* In "N::S::~S", look in "N" as well. */
3103 if (scope && qualifying_scope)
3105 cp_parser_parse_tentatively (parser);
3106 parser->scope = qualifying_scope;
3107 parser->object_scope = NULL_TREE;
3108 parser->qualifying_scope = NULL_TREE;
3109 type_decl
3110 = cp_parser_class_name (parser,
3111 /*typename_keyword_p=*/false,
3112 /*template_keyword_p=*/false,
3113 /*type_p=*/false,
3114 /*check_dependency=*/false,
3115 /*class_head_p=*/false);
3116 if (cp_parser_parse_definitely (parser))
3117 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3119 /* In "p->S::~T", look in the scope given by "*p" as well. */
3120 else if (object_scope)
3122 cp_parser_parse_tentatively (parser);
3123 parser->scope = object_scope;
3124 parser->object_scope = NULL_TREE;
3125 parser->qualifying_scope = NULL_TREE;
3126 type_decl
3127 = cp_parser_class_name (parser,
3128 /*typename_keyword_p=*/false,
3129 /*template_keyword_p=*/false,
3130 /*type_p=*/false,
3131 /*check_dependency=*/false,
3132 /*class_head_p=*/false);
3133 if (cp_parser_parse_definitely (parser))
3134 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3136 /* Look in the surrounding context. */
3137 parser->scope = NULL_TREE;
3138 parser->object_scope = NULL_TREE;
3139 parser->qualifying_scope = NULL_TREE;
3140 type_decl
3141 = cp_parser_class_name (parser,
3142 /*typename_keyword_p=*/false,
3143 /*template_keyword_p=*/false,
3144 /*type_p=*/false,
3145 /*check_dependency=*/false,
3146 /*class_head_p=*/false);
3147 /* If an error occurred, assume that the name of the
3148 destructor is the same as the name of the qualifying
3149 class. That allows us to keep parsing after running
3150 into ill-formed destructor names. */
3151 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3152 return build_nt (BIT_NOT_EXPR, scope);
3153 else if (type_decl == error_mark_node)
3154 return error_mark_node;
3156 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3159 case CPP_KEYWORD:
3160 if (token->keyword == RID_OPERATOR)
3162 tree id;
3164 /* This could be a template-id, so we try that first. */
3165 cp_parser_parse_tentatively (parser);
3166 /* Try a template-id. */
3167 id = cp_parser_template_id (parser, template_keyword_p,
3168 /*check_dependency_p=*/true);
3169 /* If that worked, we're done. */
3170 if (cp_parser_parse_definitely (parser))
3171 return id;
3172 /* We still don't know whether we're looking at an
3173 operator-function-id or a conversion-function-id. */
3174 cp_parser_parse_tentatively (parser);
3175 /* Try an operator-function-id. */
3176 id = cp_parser_operator_function_id (parser);
3177 /* If that didn't work, try a conversion-function-id. */
3178 if (!cp_parser_parse_definitely (parser))
3179 id = cp_parser_conversion_function_id (parser);
3181 return id;
3183 /* Fall through. */
3185 default:
3186 cp_parser_error (parser, "expected unqualified-id");
3187 return error_mark_node;
3191 /* Parse an (optional) nested-name-specifier.
3193 nested-name-specifier:
3194 class-or-namespace-name :: nested-name-specifier [opt]
3195 class-or-namespace-name :: template nested-name-specifier [opt]
3197 PARSER->SCOPE should be set appropriately before this function is
3198 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3199 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3200 in name lookups.
3202 Sets PARSER->SCOPE to the class (TYPE) or namespace
3203 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3204 it unchanged if there is no nested-name-specifier. Returns the new
3205 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3207 static tree
3208 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3209 bool typename_keyword_p,
3210 bool check_dependency_p,
3211 bool type_p)
3213 bool success = false;
3214 tree access_check = NULL_TREE;
3215 ptrdiff_t start;
3216 cp_token* token;
3218 /* If the next token corresponds to a nested name specifier, there
3219 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3220 false, it may have been true before, in which case something
3221 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3222 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3223 CHECK_DEPENDENCY_P is false, we have to fall through into the
3224 main loop. */
3225 if (check_dependency_p
3226 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3228 cp_parser_pre_parsed_nested_name_specifier (parser);
3229 return parser->scope;
3232 /* Remember where the nested-name-specifier starts. */
3233 if (cp_parser_parsing_tentatively (parser)
3234 && !cp_parser_committed_to_tentative_parse (parser))
3236 token = cp_lexer_peek_token (parser->lexer);
3237 start = cp_lexer_token_difference (parser->lexer,
3238 parser->lexer->first_token,
3239 token);
3241 else
3242 start = -1;
3244 push_deferring_access_checks (dk_deferred);
3246 while (true)
3248 tree new_scope;
3249 tree old_scope;
3250 tree saved_qualifying_scope;
3251 bool template_keyword_p;
3253 /* Spot cases that cannot be the beginning of a
3254 nested-name-specifier. */
3255 token = cp_lexer_peek_token (parser->lexer);
3257 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3258 the already parsed nested-name-specifier. */
3259 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3261 /* Grab the nested-name-specifier and continue the loop. */
3262 cp_parser_pre_parsed_nested_name_specifier (parser);
3263 success = true;
3264 continue;
3267 /* Spot cases that cannot be the beginning of a
3268 nested-name-specifier. On the second and subsequent times
3269 through the loop, we look for the `template' keyword. */
3270 if (success && token->keyword == RID_TEMPLATE)
3272 /* A template-id can start a nested-name-specifier. */
3273 else if (token->type == CPP_TEMPLATE_ID)
3275 else
3277 /* If the next token is not an identifier, then it is
3278 definitely not a class-or-namespace-name. */
3279 if (token->type != CPP_NAME)
3280 break;
3281 /* If the following token is neither a `<' (to begin a
3282 template-id), nor a `::', then we are not looking at a
3283 nested-name-specifier. */
3284 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3285 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3286 break;
3289 /* The nested-name-specifier is optional, so we parse
3290 tentatively. */
3291 cp_parser_parse_tentatively (parser);
3293 /* Look for the optional `template' keyword, if this isn't the
3294 first time through the loop. */
3295 if (success)
3296 template_keyword_p = cp_parser_optional_template_keyword (parser);
3297 else
3298 template_keyword_p = false;
3300 /* Save the old scope since the name lookup we are about to do
3301 might destroy it. */
3302 old_scope = parser->scope;
3303 saved_qualifying_scope = parser->qualifying_scope;
3304 /* Parse the qualifying entity. */
3305 new_scope
3306 = cp_parser_class_or_namespace_name (parser,
3307 typename_keyword_p,
3308 template_keyword_p,
3309 check_dependency_p,
3310 type_p);
3311 /* Look for the `::' token. */
3312 cp_parser_require (parser, CPP_SCOPE, "`::'");
3314 /* If we found what we wanted, we keep going; otherwise, we're
3315 done. */
3316 if (!cp_parser_parse_definitely (parser))
3318 bool error_p = false;
3320 /* Restore the OLD_SCOPE since it was valid before the
3321 failed attempt at finding the last
3322 class-or-namespace-name. */
3323 parser->scope = old_scope;
3324 parser->qualifying_scope = saved_qualifying_scope;
3325 /* If the next token is an identifier, and the one after
3326 that is a `::', then any valid interpretation would have
3327 found a class-or-namespace-name. */
3328 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3329 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3330 == CPP_SCOPE)
3331 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3332 != CPP_COMPL))
3334 token = cp_lexer_consume_token (parser->lexer);
3335 if (!error_p)
3337 tree decl;
3339 decl = cp_parser_lookup_name_simple (parser, token->value);
3340 if (TREE_CODE (decl) == TEMPLATE_DECL)
3341 error ("`%D' used without template parameters",
3342 decl);
3343 else if (parser->scope)
3345 if (TYPE_P (parser->scope))
3346 error ("`%T::%D' is not a class-name or "
3347 "namespace-name",
3348 parser->scope, token->value);
3349 else
3350 error ("`%D::%D' is not a class-name or "
3351 "namespace-name",
3352 parser->scope, token->value);
3354 else
3355 error ("`%D' is not a class-name or namespace-name",
3356 token->value);
3357 parser->scope = NULL_TREE;
3358 error_p = true;
3359 /* Treat this as a successful nested-name-specifier
3360 due to:
3362 [basic.lookup.qual]
3364 If the name found is not a class-name (clause
3365 _class_) or namespace-name (_namespace.def_), the
3366 program is ill-formed. */
3367 success = true;
3369 cp_lexer_consume_token (parser->lexer);
3371 break;
3374 /* We've found one valid nested-name-specifier. */
3375 success = true;
3376 /* Make sure we look in the right scope the next time through
3377 the loop. */
3378 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3379 ? TREE_TYPE (new_scope)
3380 : new_scope);
3381 /* If it is a class scope, try to complete it; we are about to
3382 be looking up names inside the class. */
3383 if (TYPE_P (parser->scope)
3384 /* Since checking types for dependency can be expensive,
3385 avoid doing it if the type is already complete. */
3386 && !COMPLETE_TYPE_P (parser->scope)
3387 /* Do not try to complete dependent types. */
3388 && !dependent_type_p (parser->scope))
3389 complete_type (parser->scope);
3392 /* Retrieve any deferred checks. Do not pop this access checks yet
3393 so the memory will not be reclaimed during token replacing below. */
3394 access_check = get_deferred_access_checks ();
3396 /* If parsing tentatively, replace the sequence of tokens that makes
3397 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3398 token. That way, should we re-parse the token stream, we will
3399 not have to repeat the effort required to do the parse, nor will
3400 we issue duplicate error messages. */
3401 if (success && start >= 0)
3403 /* Find the token that corresponds to the start of the
3404 template-id. */
3405 token = cp_lexer_advance_token (parser->lexer,
3406 parser->lexer->first_token,
3407 start);
3409 /* Reset the contents of the START token. */
3410 token->type = CPP_NESTED_NAME_SPECIFIER;
3411 token->value = build_tree_list (access_check, parser->scope);
3412 TREE_TYPE (token->value) = parser->qualifying_scope;
3413 token->keyword = RID_MAX;
3414 /* Purge all subsequent tokens. */
3415 cp_lexer_purge_tokens_after (parser->lexer, token);
3418 pop_deferring_access_checks ();
3419 return success ? parser->scope : NULL_TREE;
3422 /* Parse a nested-name-specifier. See
3423 cp_parser_nested_name_specifier_opt for details. This function
3424 behaves identically, except that it will an issue an error if no
3425 nested-name-specifier is present, and it will return
3426 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3427 is present. */
3429 static tree
3430 cp_parser_nested_name_specifier (cp_parser *parser,
3431 bool typename_keyword_p,
3432 bool check_dependency_p,
3433 bool type_p)
3435 tree scope;
3437 /* Look for the nested-name-specifier. */
3438 scope = cp_parser_nested_name_specifier_opt (parser,
3439 typename_keyword_p,
3440 check_dependency_p,
3441 type_p);
3442 /* If it was not present, issue an error message. */
3443 if (!scope)
3445 cp_parser_error (parser, "expected nested-name-specifier");
3446 return error_mark_node;
3449 return scope;
3452 /* Parse a class-or-namespace-name.
3454 class-or-namespace-name:
3455 class-name
3456 namespace-name
3458 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3459 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3460 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3461 TYPE_P is TRUE iff the next name should be taken as a class-name,
3462 even the same name is declared to be another entity in the same
3463 scope.
3465 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3466 specified by the class-or-namespace-name. If neither is found the
3467 ERROR_MARK_NODE is returned. */
3469 static tree
3470 cp_parser_class_or_namespace_name (cp_parser *parser,
3471 bool typename_keyword_p,
3472 bool template_keyword_p,
3473 bool check_dependency_p,
3474 bool type_p)
3476 tree saved_scope;
3477 tree saved_qualifying_scope;
3478 tree saved_object_scope;
3479 tree scope;
3480 bool only_class_p;
3482 /* Before we try to parse the class-name, we must save away the
3483 current PARSER->SCOPE since cp_parser_class_name will destroy
3484 it. */
3485 saved_scope = parser->scope;
3486 saved_qualifying_scope = parser->qualifying_scope;
3487 saved_object_scope = parser->object_scope;
3488 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3489 there is no need to look for a namespace-name. */
3490 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3491 if (!only_class_p)
3492 cp_parser_parse_tentatively (parser);
3493 scope = cp_parser_class_name (parser,
3494 typename_keyword_p,
3495 template_keyword_p,
3496 type_p,
3497 check_dependency_p,
3498 /*class_head_p=*/false);
3499 /* If that didn't work, try for a namespace-name. */
3500 if (!only_class_p && !cp_parser_parse_definitely (parser))
3502 /* Restore the saved scope. */
3503 parser->scope = saved_scope;
3504 parser->qualifying_scope = saved_qualifying_scope;
3505 parser->object_scope = saved_object_scope;
3506 /* If we are not looking at an identifier followed by the scope
3507 resolution operator, then this is not part of a
3508 nested-name-specifier. (Note that this function is only used
3509 to parse the components of a nested-name-specifier.) */
3510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3511 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3512 return error_mark_node;
3513 scope = cp_parser_namespace_name (parser);
3516 return scope;
3519 /* Parse a postfix-expression.
3521 postfix-expression:
3522 primary-expression
3523 postfix-expression [ expression ]
3524 postfix-expression ( expression-list [opt] )
3525 simple-type-specifier ( expression-list [opt] )
3526 typename :: [opt] nested-name-specifier identifier
3527 ( expression-list [opt] )
3528 typename :: [opt] nested-name-specifier template [opt] template-id
3529 ( expression-list [opt] )
3530 postfix-expression . template [opt] id-expression
3531 postfix-expression -> template [opt] id-expression
3532 postfix-expression . pseudo-destructor-name
3533 postfix-expression -> pseudo-destructor-name
3534 postfix-expression ++
3535 postfix-expression --
3536 dynamic_cast < type-id > ( expression )
3537 static_cast < type-id > ( expression )
3538 reinterpret_cast < type-id > ( expression )
3539 const_cast < type-id > ( expression )
3540 typeid ( expression )
3541 typeid ( type-id )
3543 GNU Extension:
3545 postfix-expression:
3546 ( type-id ) { initializer-list , [opt] }
3548 This extension is a GNU version of the C99 compound-literal
3549 construct. (The C99 grammar uses `type-name' instead of `type-id',
3550 but they are essentially the same concept.)
3552 If ADDRESS_P is true, the postfix expression is the operand of the
3553 `&' operator.
3555 Returns a representation of the expression. */
3557 static tree
3558 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3560 cp_token *token;
3561 enum rid keyword;
3562 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3563 tree postfix_expression = NULL_TREE;
3564 /* Non-NULL only if the current postfix-expression can be used to
3565 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3566 class used to qualify the member. */
3567 tree qualifying_class = NULL_TREE;
3568 bool done;
3570 /* Peek at the next token. */
3571 token = cp_lexer_peek_token (parser->lexer);
3572 /* Some of the productions are determined by keywords. */
3573 keyword = token->keyword;
3574 switch (keyword)
3576 case RID_DYNCAST:
3577 case RID_STATCAST:
3578 case RID_REINTCAST:
3579 case RID_CONSTCAST:
3581 tree type;
3582 tree expression;
3583 const char *saved_message;
3585 /* All of these can be handled in the same way from the point
3586 of view of parsing. Begin by consuming the token
3587 identifying the cast. */
3588 cp_lexer_consume_token (parser->lexer);
3590 /* New types cannot be defined in the cast. */
3591 saved_message = parser->type_definition_forbidden_message;
3592 parser->type_definition_forbidden_message
3593 = "types may not be defined in casts";
3595 /* Look for the opening `<'. */
3596 cp_parser_require (parser, CPP_LESS, "`<'");
3597 /* Parse the type to which we are casting. */
3598 type = cp_parser_type_id (parser);
3599 /* Look for the closing `>'. */
3600 cp_parser_require (parser, CPP_GREATER, "`>'");
3601 /* Restore the old message. */
3602 parser->type_definition_forbidden_message = saved_message;
3604 /* And the expression which is being cast. */
3605 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3606 expression = cp_parser_expression (parser);
3607 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3609 /* Only type conversions to integral or enumeration types
3610 can be used in constant-expressions. */
3611 if (parser->constant_expression_p
3612 && !dependent_type_p (type)
3613 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3615 if (!parser->allow_non_constant_expression_p)
3616 return (cp_parser_non_constant_expression
3617 ("a cast to a type other than an integral or "
3618 "enumeration type"));
3619 parser->non_constant_expression_p = true;
3622 switch (keyword)
3624 case RID_DYNCAST:
3625 postfix_expression
3626 = build_dynamic_cast (type, expression);
3627 break;
3628 case RID_STATCAST:
3629 postfix_expression
3630 = build_static_cast (type, expression);
3631 break;
3632 case RID_REINTCAST:
3633 postfix_expression
3634 = build_reinterpret_cast (type, expression);
3635 break;
3636 case RID_CONSTCAST:
3637 postfix_expression
3638 = build_const_cast (type, expression);
3639 break;
3640 default:
3641 abort ();
3644 break;
3646 case RID_TYPEID:
3648 tree type;
3649 const char *saved_message;
3651 /* Consume the `typeid' token. */
3652 cp_lexer_consume_token (parser->lexer);
3653 /* Look for the `(' token. */
3654 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3655 /* Types cannot be defined in a `typeid' expression. */
3656 saved_message = parser->type_definition_forbidden_message;
3657 parser->type_definition_forbidden_message
3658 = "types may not be defined in a `typeid\' expression";
3659 /* We can't be sure yet whether we're looking at a type-id or an
3660 expression. */
3661 cp_parser_parse_tentatively (parser);
3662 /* Try a type-id first. */
3663 type = cp_parser_type_id (parser);
3664 /* Look for the `)' token. Otherwise, we can't be sure that
3665 we're not looking at an expression: consider `typeid (int
3666 (3))', for example. */
3667 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3668 /* If all went well, simply lookup the type-id. */
3669 if (cp_parser_parse_definitely (parser))
3670 postfix_expression = get_typeid (type);
3671 /* Otherwise, fall back to the expression variant. */
3672 else
3674 tree expression;
3676 /* Look for an expression. */
3677 expression = cp_parser_expression (parser);
3678 /* Compute its typeid. */
3679 postfix_expression = build_typeid (expression);
3680 /* Look for the `)' token. */
3681 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3684 /* Restore the saved message. */
3685 parser->type_definition_forbidden_message = saved_message;
3687 break;
3689 case RID_TYPENAME:
3691 bool template_p = false;
3692 tree id;
3693 tree type;
3695 /* Consume the `typename' token. */
3696 cp_lexer_consume_token (parser->lexer);
3697 /* Look for the optional `::' operator. */
3698 cp_parser_global_scope_opt (parser,
3699 /*current_scope_valid_p=*/false);
3700 /* Look for the nested-name-specifier. */
3701 cp_parser_nested_name_specifier (parser,
3702 /*typename_keyword_p=*/true,
3703 /*check_dependency_p=*/true,
3704 /*type_p=*/true);
3705 /* Look for the optional `template' keyword. */
3706 template_p = cp_parser_optional_template_keyword (parser);
3707 /* We don't know whether we're looking at a template-id or an
3708 identifier. */
3709 cp_parser_parse_tentatively (parser);
3710 /* Try a template-id. */
3711 id = cp_parser_template_id (parser, template_p,
3712 /*check_dependency_p=*/true);
3713 /* If that didn't work, try an identifier. */
3714 if (!cp_parser_parse_definitely (parser))
3715 id = cp_parser_identifier (parser);
3716 /* Create a TYPENAME_TYPE to represent the type to which the
3717 functional cast is being performed. */
3718 type = make_typename_type (parser->scope, id,
3719 /*complain=*/1);
3721 postfix_expression = cp_parser_functional_cast (parser, type);
3723 break;
3725 default:
3727 tree type;
3729 /* If the next thing is a simple-type-specifier, we may be
3730 looking at a functional cast. We could also be looking at
3731 an id-expression. So, we try the functional cast, and if
3732 that doesn't work we fall back to the primary-expression. */
3733 cp_parser_parse_tentatively (parser);
3734 /* Look for the simple-type-specifier. */
3735 type = cp_parser_simple_type_specifier (parser,
3736 CP_PARSER_FLAGS_NONE);
3737 /* Parse the cast itself. */
3738 if (!cp_parser_error_occurred (parser))
3739 postfix_expression
3740 = cp_parser_functional_cast (parser, type);
3741 /* If that worked, we're done. */
3742 if (cp_parser_parse_definitely (parser))
3743 break;
3745 /* If the functional-cast didn't work out, try a
3746 compound-literal. */
3747 if (cp_parser_allow_gnu_extensions_p (parser)
3748 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3750 tree initializer_list = NULL_TREE;
3752 cp_parser_parse_tentatively (parser);
3753 /* Consume the `('. */
3754 cp_lexer_consume_token (parser->lexer);
3755 /* Parse the type. */
3756 type = cp_parser_type_id (parser);
3757 /* Look for the `)'. */
3758 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3759 /* Look for the `{'. */
3760 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3761 /* If things aren't going well, there's no need to
3762 keep going. */
3763 if (!cp_parser_error_occurred (parser))
3765 /* Parse the initializer-list. */
3766 initializer_list
3767 = cp_parser_initializer_list (parser);
3768 /* Allow a trailing `,'. */
3769 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3770 cp_lexer_consume_token (parser->lexer);
3771 /* Look for the final `}'. */
3772 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3774 /* If that worked, we're definitely looking at a
3775 compound-literal expression. */
3776 if (cp_parser_parse_definitely (parser))
3778 /* Warn the user that a compound literal is not
3779 allowed in standard C++. */
3780 if (pedantic)
3781 pedwarn ("ISO C++ forbids compound-literals");
3782 /* Form the representation of the compound-literal. */
3783 postfix_expression
3784 = finish_compound_literal (type, initializer_list);
3785 break;
3789 /* It must be a primary-expression. */
3790 postfix_expression = cp_parser_primary_expression (parser,
3791 &idk,
3792 &qualifying_class);
3794 break;
3797 /* Peek at the next token. */
3798 token = cp_lexer_peek_token (parser->lexer);
3799 done = (token->type != CPP_OPEN_SQUARE
3800 && token->type != CPP_OPEN_PAREN
3801 && token->type != CPP_DOT
3802 && token->type != CPP_DEREF
3803 && token->type != CPP_PLUS_PLUS
3804 && token->type != CPP_MINUS_MINUS);
3806 /* If the postfix expression is complete, finish up. */
3807 if (address_p && qualifying_class && done)
3809 if (TREE_CODE (postfix_expression) == SCOPE_REF)
3810 postfix_expression = TREE_OPERAND (postfix_expression, 1);
3811 postfix_expression
3812 = build_offset_ref (qualifying_class, postfix_expression);
3813 return postfix_expression;
3816 /* Otherwise, if we were avoiding committing until we knew
3817 whether or not we had a pointer-to-member, we now know that
3818 the expression is an ordinary reference to a qualified name. */
3819 if (qualifying_class)
3821 if (TREE_CODE (postfix_expression) == FIELD_DECL)
3822 postfix_expression
3823 = finish_non_static_data_member (postfix_expression,
3824 qualifying_class);
3825 else if (BASELINK_P (postfix_expression)
3826 && !processing_template_decl)
3828 tree fn;
3829 tree fns;
3831 /* See if any of the functions are non-static members. */
3832 fns = BASELINK_FUNCTIONS (postfix_expression);
3833 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3834 fns = TREE_OPERAND (fns, 0);
3835 for (fn = fns; fn; fn = OVL_NEXT (fn))
3836 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3837 break;
3838 /* If so, the expression may be relative to the current
3839 class. */
3840 if (fn && current_class_type
3841 && DERIVED_FROM_P (qualifying_class, current_class_type))
3842 postfix_expression
3843 = (build_class_member_access_expr
3844 (maybe_dummy_object (qualifying_class, NULL),
3845 postfix_expression,
3846 BASELINK_ACCESS_BINFO (postfix_expression),
3847 /*preserve_reference=*/false));
3848 else if (done)
3849 return build_offset_ref (qualifying_class,
3850 postfix_expression);
3854 /* Remember that there was a reference to this entity. */
3855 if (DECL_P (postfix_expression))
3856 mark_used (postfix_expression);
3858 /* Keep looping until the postfix-expression is complete. */
3859 while (true)
3861 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3862 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3864 /* It is not a Koenig lookup function call. */
3865 unqualified_name_lookup_error (postfix_expression);
3866 postfix_expression = error_mark_node;
3869 /* Peek at the next token. */
3870 token = cp_lexer_peek_token (parser->lexer);
3872 switch (token->type)
3874 case CPP_OPEN_SQUARE:
3875 /* postfix-expression [ expression ] */
3877 tree index;
3879 /* Consume the `[' token. */
3880 cp_lexer_consume_token (parser->lexer);
3881 /* Parse the index expression. */
3882 index = cp_parser_expression (parser);
3883 /* Look for the closing `]'. */
3884 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3886 /* Build the ARRAY_REF. */
3887 postfix_expression
3888 = grok_array_decl (postfix_expression, index);
3889 idk = CP_PARSER_ID_KIND_NONE;
3891 break;
3893 case CPP_OPEN_PAREN:
3894 /* postfix-expression ( expression-list [opt] ) */
3896 tree args;
3898 /* Consume the `(' token. */
3899 cp_lexer_consume_token (parser->lexer);
3900 /* If the next token is not a `)', then there are some
3901 arguments. */
3902 if (cp_lexer_next_token_is_not (parser->lexer,
3903 CPP_CLOSE_PAREN))
3904 args = cp_parser_expression_list (parser);
3905 else
3906 args = NULL_TREE;
3907 /* Look for the closing `)'. */
3908 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3909 /* Function calls are not permitted in
3910 constant-expressions. */
3911 if (parser->constant_expression_p)
3913 if (!parser->allow_non_constant_expression_p)
3914 return cp_parser_non_constant_expression ("a function call");
3915 parser->non_constant_expression_p = true;
3918 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
3919 && (is_overloaded_fn (postfix_expression)
3920 || DECL_P (postfix_expression)
3921 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3922 && args)
3924 tree arg;
3925 tree identifier = NULL_TREE;
3926 tree functions = NULL_TREE;
3928 /* Find the name of the overloaded function. */
3929 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3930 identifier = postfix_expression;
3931 else if (is_overloaded_fn (postfix_expression))
3933 functions = postfix_expression;
3934 identifier = DECL_NAME (get_first_fn (functions));
3936 else if (DECL_P (postfix_expression))
3938 functions = postfix_expression;
3939 identifier = DECL_NAME (postfix_expression);
3942 /* A call to a namespace-scope function using an
3943 unqualified name.
3945 Do Koenig lookup -- unless any of the arguments are
3946 type-dependent. */
3947 for (arg = args; arg; arg = TREE_CHAIN (arg))
3948 if (type_dependent_expression_p (TREE_VALUE (arg)))
3949 break;
3950 if (!arg)
3952 postfix_expression
3953 = lookup_arg_dependent(identifier, functions, args);
3954 if (!postfix_expression)
3956 /* The unqualified name could not be resolved. */
3957 unqualified_name_lookup_error (identifier);
3958 postfix_expression = error_mark_node;
3960 postfix_expression
3961 = build_call_from_tree (postfix_expression, args,
3962 /*diallow_virtual=*/false);
3963 break;
3965 postfix_expression = build_min_nt (LOOKUP_EXPR,
3966 identifier);
3968 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
3969 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3971 /* The unqualified name could not be resolved. */
3972 unqualified_name_lookup_error (postfix_expression);
3973 postfix_expression = error_mark_node;
3974 break;
3977 /* In the body of a template, no further processing is
3978 required. */
3979 if (processing_template_decl)
3981 postfix_expression = build_nt (CALL_EXPR,
3982 postfix_expression,
3983 args);
3984 break;
3987 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3988 postfix_expression
3989 = (build_new_method_call
3990 (TREE_OPERAND (postfix_expression, 0),
3991 TREE_OPERAND (postfix_expression, 1),
3992 args, NULL_TREE,
3993 (idk == CP_PARSER_ID_KIND_QUALIFIED
3994 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3995 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
3996 postfix_expression = (build_offset_ref_call_from_tree
3997 (postfix_expression, args));
3998 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
3999 /* A call to a static class member, or a namespace-scope
4000 function. */
4001 postfix_expression
4002 = finish_call_expr (postfix_expression, args,
4003 /*disallow_virtual=*/true);
4004 else
4005 /* All other function calls. */
4006 postfix_expression
4007 = finish_call_expr (postfix_expression, args,
4008 /*disallow_virtual=*/false);
4010 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4011 idk = CP_PARSER_ID_KIND_NONE;
4013 break;
4015 case CPP_DOT:
4016 case CPP_DEREF:
4017 /* postfix-expression . template [opt] id-expression
4018 postfix-expression . pseudo-destructor-name
4019 postfix-expression -> template [opt] id-expression
4020 postfix-expression -> pseudo-destructor-name */
4022 tree name;
4023 bool dependent_p;
4024 bool template_p;
4025 tree scope = NULL_TREE;
4027 /* If this is a `->' operator, dereference the pointer. */
4028 if (token->type == CPP_DEREF)
4029 postfix_expression = build_x_arrow (postfix_expression);
4030 /* Check to see whether or not the expression is
4031 type-dependent. */
4032 dependent_p = type_dependent_expression_p (postfix_expression);
4033 /* The identifier following the `->' or `.' is not
4034 qualified. */
4035 parser->scope = NULL_TREE;
4036 parser->qualifying_scope = NULL_TREE;
4037 parser->object_scope = NULL_TREE;
4038 idk = CP_PARSER_ID_KIND_NONE;
4039 /* Enter the scope corresponding to the type of the object
4040 given by the POSTFIX_EXPRESSION. */
4041 if (!dependent_p
4042 && TREE_TYPE (postfix_expression) != NULL_TREE)
4044 scope = TREE_TYPE (postfix_expression);
4045 /* According to the standard, no expression should
4046 ever have reference type. Unfortunately, we do not
4047 currently match the standard in this respect in
4048 that our internal representation of an expression
4049 may have reference type even when the standard says
4050 it does not. Therefore, we have to manually obtain
4051 the underlying type here. */
4052 if (TREE_CODE (scope) == REFERENCE_TYPE)
4053 scope = TREE_TYPE (scope);
4054 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4055 type of the field. We get an OFFSET_TYPE for
4056 something like:
4058 S::T.a ...
4060 Probably, we should not get an OFFSET_TYPE here;
4061 that transformation should be made only if `&S::T'
4062 is written. */
4063 if (TREE_CODE (scope) == OFFSET_TYPE)
4064 scope = TREE_TYPE (scope);
4065 /* The type of the POSTFIX_EXPRESSION must be
4066 complete. */
4067 scope = complete_type_or_else (scope, NULL_TREE);
4068 /* Let the name lookup machinery know that we are
4069 processing a class member access expression. */
4070 parser->context->object_type = scope;
4071 /* If something went wrong, we want to be able to
4072 discern that case, as opposed to the case where
4073 there was no SCOPE due to the type of expression
4074 being dependent. */
4075 if (!scope)
4076 scope = error_mark_node;
4079 /* Consume the `.' or `->' operator. */
4080 cp_lexer_consume_token (parser->lexer);
4081 /* If the SCOPE is not a scalar type, we are looking at an
4082 ordinary class member access expression, rather than a
4083 pseudo-destructor-name. */
4084 if (!scope || !SCALAR_TYPE_P (scope))
4086 template_p = cp_parser_optional_template_keyword (parser);
4087 /* Parse the id-expression. */
4088 name = cp_parser_id_expression (parser,
4089 template_p,
4090 /*check_dependency_p=*/true,
4091 /*template_p=*/NULL);
4092 /* In general, build a SCOPE_REF if the member name is
4093 qualified. However, if the name was not dependent
4094 and has already been resolved; there is no need to
4095 build the SCOPE_REF. For example;
4097 struct X { void f(); };
4098 template <typename T> void f(T* t) { t->X::f(); }
4100 Even though "t" is dependent, "X::f" is not and has
4101 except that for a BASELINK there is no need to
4102 include scope information. */
4104 /* But we do need to remember that there was an explicit
4105 scope for virtual function calls. */
4106 if (parser->scope)
4107 idk = CP_PARSER_ID_KIND_QUALIFIED;
4109 if (name != error_mark_node
4110 && !BASELINK_P (name)
4111 && parser->scope)
4113 name = build_nt (SCOPE_REF, parser->scope, name);
4114 parser->scope = NULL_TREE;
4115 parser->qualifying_scope = NULL_TREE;
4116 parser->object_scope = NULL_TREE;
4118 postfix_expression
4119 = finish_class_member_access_expr (postfix_expression, name);
4121 /* Otherwise, try the pseudo-destructor-name production. */
4122 else
4124 tree s;
4125 tree type;
4127 /* Parse the pseudo-destructor-name. */
4128 cp_parser_pseudo_destructor_name (parser, &s, &type);
4129 /* Form the call. */
4130 postfix_expression
4131 = finish_pseudo_destructor_expr (postfix_expression,
4132 s, TREE_TYPE (type));
4135 /* We no longer need to look up names in the scope of the
4136 object on the left-hand side of the `.' or `->'
4137 operator. */
4138 parser->context->object_type = NULL_TREE;
4140 break;
4142 case CPP_PLUS_PLUS:
4143 /* postfix-expression ++ */
4144 /* Consume the `++' token. */
4145 cp_lexer_consume_token (parser->lexer);
4146 /* Increments may not appear in constant-expressions. */
4147 if (parser->constant_expression_p)
4149 if (!parser->allow_non_constant_expression_p)
4150 return cp_parser_non_constant_expression ("an increment");
4151 parser->non_constant_expression_p = true;
4153 /* Generate a reprsentation for the complete expression. */
4154 postfix_expression
4155 = finish_increment_expr (postfix_expression,
4156 POSTINCREMENT_EXPR);
4157 idk = CP_PARSER_ID_KIND_NONE;
4158 break;
4160 case CPP_MINUS_MINUS:
4161 /* postfix-expression -- */
4162 /* Consume the `--' token. */
4163 cp_lexer_consume_token (parser->lexer);
4164 /* Decrements may not appear in constant-expressions. */
4165 if (parser->constant_expression_p)
4167 if (!parser->allow_non_constant_expression_p)
4168 return cp_parser_non_constant_expression ("a decrement");
4169 parser->non_constant_expression_p = true;
4171 /* Generate a reprsentation for the complete expression. */
4172 postfix_expression
4173 = finish_increment_expr (postfix_expression,
4174 POSTDECREMENT_EXPR);
4175 idk = CP_PARSER_ID_KIND_NONE;
4176 break;
4178 default:
4179 return postfix_expression;
4183 /* We should never get here. */
4184 abort ();
4185 return error_mark_node;
4188 /* Parse an expression-list.
4190 expression-list:
4191 assignment-expression
4192 expression-list, assignment-expression
4194 Returns a TREE_LIST. The TREE_VALUE of each node is a
4195 representation of an assignment-expression. Note that a TREE_LIST
4196 is returned even if there is only a single expression in the list. */
4198 static tree
4199 cp_parser_expression_list (cp_parser* parser)
4201 tree expression_list = NULL_TREE;
4203 /* Consume expressions until there are no more. */
4204 while (true)
4206 tree expr;
4208 /* Parse the next assignment-expression. */
4209 expr = cp_parser_assignment_expression (parser);
4210 /* Add it to the list. */
4211 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4213 /* If the next token isn't a `,', then we are done. */
4214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4216 /* All uses of expression-list in the grammar are followed
4217 by a `)'. Therefore, if the next token is not a `)' an
4218 error will be issued, unless we are parsing tentatively.
4219 Skip ahead to see if there is another `,' before the `)';
4220 if so, we can go there and recover. */
4221 if (cp_parser_parsing_tentatively (parser)
4222 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4223 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4224 break;
4227 /* Otherwise, consume the `,' and keep going. */
4228 cp_lexer_consume_token (parser->lexer);
4231 /* We built up the list in reverse order so we must reverse it now. */
4232 return nreverse (expression_list);
4235 /* Parse a pseudo-destructor-name.
4237 pseudo-destructor-name:
4238 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4239 :: [opt] nested-name-specifier template template-id :: ~ type-name
4240 :: [opt] nested-name-specifier [opt] ~ type-name
4242 If either of the first two productions is used, sets *SCOPE to the
4243 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4244 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4245 or ERROR_MARK_NODE if no type-name is present. */
4247 static void
4248 cp_parser_pseudo_destructor_name (cp_parser* parser,
4249 tree* scope,
4250 tree* type)
4252 bool nested_name_specifier_p;
4254 /* Look for the optional `::' operator. */
4255 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4256 /* Look for the optional nested-name-specifier. */
4257 nested_name_specifier_p
4258 = (cp_parser_nested_name_specifier_opt (parser,
4259 /*typename_keyword_p=*/false,
4260 /*check_dependency_p=*/true,
4261 /*type_p=*/false)
4262 != NULL_TREE);
4263 /* Now, if we saw a nested-name-specifier, we might be doing the
4264 second production. */
4265 if (nested_name_specifier_p
4266 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4268 /* Consume the `template' keyword. */
4269 cp_lexer_consume_token (parser->lexer);
4270 /* Parse the template-id. */
4271 cp_parser_template_id (parser,
4272 /*template_keyword_p=*/true,
4273 /*check_dependency_p=*/false);
4274 /* Look for the `::' token. */
4275 cp_parser_require (parser, CPP_SCOPE, "`::'");
4277 /* If the next token is not a `~', then there might be some
4278 additional qualification. */
4279 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4281 /* Look for the type-name. */
4282 *scope = TREE_TYPE (cp_parser_type_name (parser));
4283 /* Look for the `::' token. */
4284 cp_parser_require (parser, CPP_SCOPE, "`::'");
4286 else
4287 *scope = NULL_TREE;
4289 /* Look for the `~'. */
4290 cp_parser_require (parser, CPP_COMPL, "`~'");
4291 /* Look for the type-name again. We are not responsible for
4292 checking that it matches the first type-name. */
4293 *type = cp_parser_type_name (parser);
4296 /* Parse a unary-expression.
4298 unary-expression:
4299 postfix-expression
4300 ++ cast-expression
4301 -- cast-expression
4302 unary-operator cast-expression
4303 sizeof unary-expression
4304 sizeof ( type-id )
4305 new-expression
4306 delete-expression
4308 GNU Extensions:
4310 unary-expression:
4311 __extension__ cast-expression
4312 __alignof__ unary-expression
4313 __alignof__ ( type-id )
4314 __real__ cast-expression
4315 __imag__ cast-expression
4316 && identifier
4318 ADDRESS_P is true iff the unary-expression is appearing as the
4319 operand of the `&' operator.
4321 Returns a representation of the expresion. */
4323 static tree
4324 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4326 cp_token *token;
4327 enum tree_code unary_operator;
4329 /* Peek at the next token. */
4330 token = cp_lexer_peek_token (parser->lexer);
4331 /* Some keywords give away the kind of expression. */
4332 if (token->type == CPP_KEYWORD)
4334 enum rid keyword = token->keyword;
4336 switch (keyword)
4338 case RID_ALIGNOF:
4340 /* Consume the `alignof' token. */
4341 cp_lexer_consume_token (parser->lexer);
4342 /* Parse the operand. */
4343 return finish_alignof (cp_parser_sizeof_operand
4344 (parser, keyword));
4347 case RID_SIZEOF:
4349 tree operand;
4351 /* Consume the `sizeof' token. */
4352 cp_lexer_consume_token (parser->lexer);
4353 /* Parse the operand. */
4354 operand = cp_parser_sizeof_operand (parser, keyword);
4356 /* If the type of the operand cannot be determined build a
4357 SIZEOF_EXPR. */
4358 if (TYPE_P (operand)
4359 ? dependent_type_p (operand)
4360 : type_dependent_expression_p (operand))
4361 return build_min (SIZEOF_EXPR, size_type_node, operand);
4362 /* Otherwise, compute the constant value. */
4363 else
4364 return finish_sizeof (operand);
4367 case RID_NEW:
4368 return cp_parser_new_expression (parser);
4370 case RID_DELETE:
4371 return cp_parser_delete_expression (parser);
4373 case RID_EXTENSION:
4375 /* The saved value of the PEDANTIC flag. */
4376 int saved_pedantic;
4377 tree expr;
4379 /* Save away the PEDANTIC flag. */
4380 cp_parser_extension_opt (parser, &saved_pedantic);
4381 /* Parse the cast-expression. */
4382 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4383 /* Restore the PEDANTIC flag. */
4384 pedantic = saved_pedantic;
4386 return expr;
4389 case RID_REALPART:
4390 case RID_IMAGPART:
4392 tree expression;
4394 /* Consume the `__real__' or `__imag__' token. */
4395 cp_lexer_consume_token (parser->lexer);
4396 /* Parse the cast-expression. */
4397 expression = cp_parser_cast_expression (parser,
4398 /*address_p=*/false);
4399 /* Create the complete representation. */
4400 return build_x_unary_op ((keyword == RID_REALPART
4401 ? REALPART_EXPR : IMAGPART_EXPR),
4402 expression);
4404 break;
4406 default:
4407 break;
4411 /* Look for the `:: new' and `:: delete', which also signal the
4412 beginning of a new-expression, or delete-expression,
4413 respectively. If the next token is `::', then it might be one of
4414 these. */
4415 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4417 enum rid keyword;
4419 /* See if the token after the `::' is one of the keywords in
4420 which we're interested. */
4421 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4422 /* If it's `new', we have a new-expression. */
4423 if (keyword == RID_NEW)
4424 return cp_parser_new_expression (parser);
4425 /* Similarly, for `delete'. */
4426 else if (keyword == RID_DELETE)
4427 return cp_parser_delete_expression (parser);
4430 /* Look for a unary operator. */
4431 unary_operator = cp_parser_unary_operator (token);
4432 /* The `++' and `--' operators can be handled similarly, even though
4433 they are not technically unary-operators in the grammar. */
4434 if (unary_operator == ERROR_MARK)
4436 if (token->type == CPP_PLUS_PLUS)
4437 unary_operator = PREINCREMENT_EXPR;
4438 else if (token->type == CPP_MINUS_MINUS)
4439 unary_operator = PREDECREMENT_EXPR;
4440 /* Handle the GNU address-of-label extension. */
4441 else if (cp_parser_allow_gnu_extensions_p (parser)
4442 && token->type == CPP_AND_AND)
4444 tree identifier;
4446 /* Consume the '&&' token. */
4447 cp_lexer_consume_token (parser->lexer);
4448 /* Look for the identifier. */
4449 identifier = cp_parser_identifier (parser);
4450 /* Create an expression representing the address. */
4451 return finish_label_address_expr (identifier);
4454 if (unary_operator != ERROR_MARK)
4456 tree cast_expression;
4458 /* Consume the operator token. */
4459 token = cp_lexer_consume_token (parser->lexer);
4460 /* Parse the cast-expression. */
4461 cast_expression
4462 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4463 /* Now, build an appropriate representation. */
4464 switch (unary_operator)
4466 case INDIRECT_REF:
4467 return build_x_indirect_ref (cast_expression, "unary *");
4469 case ADDR_EXPR:
4470 return build_x_unary_op (ADDR_EXPR, cast_expression);
4472 case PREINCREMENT_EXPR:
4473 case PREDECREMENT_EXPR:
4474 if (parser->constant_expression_p)
4476 if (!parser->allow_non_constant_expression_p)
4477 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4478 ? "an increment"
4479 : "a decrement");
4480 parser->non_constant_expression_p = true;
4482 /* Fall through. */
4483 case CONVERT_EXPR:
4484 case NEGATE_EXPR:
4485 case TRUTH_NOT_EXPR:
4486 return finish_unary_op_expr (unary_operator, cast_expression);
4488 case BIT_NOT_EXPR:
4489 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4491 default:
4492 abort ();
4493 return error_mark_node;
4497 return cp_parser_postfix_expression (parser, address_p);
4500 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4501 unary-operator, the corresponding tree code is returned. */
4503 static enum tree_code
4504 cp_parser_unary_operator (cp_token* token)
4506 switch (token->type)
4508 case CPP_MULT:
4509 return INDIRECT_REF;
4511 case CPP_AND:
4512 return ADDR_EXPR;
4514 case CPP_PLUS:
4515 return CONVERT_EXPR;
4517 case CPP_MINUS:
4518 return NEGATE_EXPR;
4520 case CPP_NOT:
4521 return TRUTH_NOT_EXPR;
4523 case CPP_COMPL:
4524 return BIT_NOT_EXPR;
4526 default:
4527 return ERROR_MARK;
4531 /* Parse a new-expression.
4533 new-expression:
4534 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4535 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4537 Returns a representation of the expression. */
4539 static tree
4540 cp_parser_new_expression (cp_parser* parser)
4542 bool global_scope_p;
4543 tree placement;
4544 tree type;
4545 tree initializer;
4547 /* Look for the optional `::' operator. */
4548 global_scope_p
4549 = (cp_parser_global_scope_opt (parser,
4550 /*current_scope_valid_p=*/false)
4551 != NULL_TREE);
4552 /* Look for the `new' operator. */
4553 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4554 /* There's no easy way to tell a new-placement from the
4555 `( type-id )' construct. */
4556 cp_parser_parse_tentatively (parser);
4557 /* Look for a new-placement. */
4558 placement = cp_parser_new_placement (parser);
4559 /* If that didn't work out, there's no new-placement. */
4560 if (!cp_parser_parse_definitely (parser))
4561 placement = NULL_TREE;
4563 /* If the next token is a `(', then we have a parenthesized
4564 type-id. */
4565 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4567 /* Consume the `('. */
4568 cp_lexer_consume_token (parser->lexer);
4569 /* Parse the type-id. */
4570 type = cp_parser_type_id (parser);
4571 /* Look for the closing `)'. */
4572 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4574 /* Otherwise, there must be a new-type-id. */
4575 else
4576 type = cp_parser_new_type_id (parser);
4578 /* If the next token is a `(', then we have a new-initializer. */
4579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4580 initializer = cp_parser_new_initializer (parser);
4581 else
4582 initializer = NULL_TREE;
4584 /* Create a representation of the new-expression. */
4585 return build_new (placement, type, initializer, global_scope_p);
4588 /* Parse a new-placement.
4590 new-placement:
4591 ( expression-list )
4593 Returns the same representation as for an expression-list. */
4595 static tree
4596 cp_parser_new_placement (cp_parser* parser)
4598 tree expression_list;
4600 /* Look for the opening `('. */
4601 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4602 return error_mark_node;
4603 /* Parse the expression-list. */
4604 expression_list = cp_parser_expression_list (parser);
4605 /* Look for the closing `)'. */
4606 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4608 return expression_list;
4611 /* Parse a new-type-id.
4613 new-type-id:
4614 type-specifier-seq new-declarator [opt]
4616 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4617 and whose TREE_VALUE is the new-declarator. */
4619 static tree
4620 cp_parser_new_type_id (cp_parser* parser)
4622 tree type_specifier_seq;
4623 tree declarator;
4624 const char *saved_message;
4626 /* The type-specifier sequence must not contain type definitions.
4627 (It cannot contain declarations of new types either, but if they
4628 are not definitions we will catch that because they are not
4629 complete.) */
4630 saved_message = parser->type_definition_forbidden_message;
4631 parser->type_definition_forbidden_message
4632 = "types may not be defined in a new-type-id";
4633 /* Parse the type-specifier-seq. */
4634 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4635 /* Restore the old message. */
4636 parser->type_definition_forbidden_message = saved_message;
4637 /* Parse the new-declarator. */
4638 declarator = cp_parser_new_declarator_opt (parser);
4640 return build_tree_list (type_specifier_seq, declarator);
4643 /* Parse an (optional) new-declarator.
4645 new-declarator:
4646 ptr-operator new-declarator [opt]
4647 direct-new-declarator
4649 Returns a representation of the declarator. See
4650 cp_parser_declarator for the representations used. */
4652 static tree
4653 cp_parser_new_declarator_opt (cp_parser* parser)
4655 enum tree_code code;
4656 tree type;
4657 tree cv_qualifier_seq;
4659 /* We don't know if there's a ptr-operator next, or not. */
4660 cp_parser_parse_tentatively (parser);
4661 /* Look for a ptr-operator. */
4662 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4663 /* If that worked, look for more new-declarators. */
4664 if (cp_parser_parse_definitely (parser))
4666 tree declarator;
4668 /* Parse another optional declarator. */
4669 declarator = cp_parser_new_declarator_opt (parser);
4671 /* Create the representation of the declarator. */
4672 if (code == INDIRECT_REF)
4673 declarator = make_pointer_declarator (cv_qualifier_seq,
4674 declarator);
4675 else
4676 declarator = make_reference_declarator (cv_qualifier_seq,
4677 declarator);
4679 /* Handle the pointer-to-member case. */
4680 if (type)
4681 declarator = build_nt (SCOPE_REF, type, declarator);
4683 return declarator;
4686 /* If the next token is a `[', there is a direct-new-declarator. */
4687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4688 return cp_parser_direct_new_declarator (parser);
4690 return NULL_TREE;
4693 /* Parse a direct-new-declarator.
4695 direct-new-declarator:
4696 [ expression ]
4697 direct-new-declarator [constant-expression]
4699 Returns an ARRAY_REF, following the same conventions as are
4700 documented for cp_parser_direct_declarator. */
4702 static tree
4703 cp_parser_direct_new_declarator (cp_parser* parser)
4705 tree declarator = NULL_TREE;
4707 while (true)
4709 tree expression;
4711 /* Look for the opening `['. */
4712 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4713 /* The first expression is not required to be constant. */
4714 if (!declarator)
4716 expression = cp_parser_expression (parser);
4717 /* The standard requires that the expression have integral
4718 type. DR 74 adds enumeration types. We believe that the
4719 real intent is that these expressions be handled like the
4720 expression in a `switch' condition, which also allows
4721 classes with a single conversion to integral or
4722 enumeration type. */
4723 if (!processing_template_decl)
4725 expression
4726 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4727 expression,
4728 /*complain=*/true);
4729 if (!expression)
4731 error ("expression in new-declarator must have integral or enumeration type");
4732 expression = error_mark_node;
4736 /* But all the other expressions must be. */
4737 else
4738 expression
4739 = cp_parser_constant_expression (parser,
4740 /*allow_non_constant=*/false,
4741 NULL);
4742 /* Look for the closing `]'. */
4743 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4745 /* Add this bound to the declarator. */
4746 declarator = build_nt (ARRAY_REF, declarator, expression);
4748 /* If the next token is not a `[', then there are no more
4749 bounds. */
4750 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4751 break;
4754 return declarator;
4757 /* Parse a new-initializer.
4759 new-initializer:
4760 ( expression-list [opt] )
4762 Returns a reprsentation of the expression-list. If there is no
4763 expression-list, VOID_ZERO_NODE is returned. */
4765 static tree
4766 cp_parser_new_initializer (cp_parser* parser)
4768 tree expression_list;
4770 /* Look for the opening parenthesis. */
4771 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4772 /* If the next token is not a `)', then there is an
4773 expression-list. */
4774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4775 expression_list = cp_parser_expression_list (parser);
4776 else
4777 expression_list = void_zero_node;
4778 /* Look for the closing parenthesis. */
4779 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4781 return expression_list;
4784 /* Parse a delete-expression.
4786 delete-expression:
4787 :: [opt] delete cast-expression
4788 :: [opt] delete [ ] cast-expression
4790 Returns a representation of the expression. */
4792 static tree
4793 cp_parser_delete_expression (cp_parser* parser)
4795 bool global_scope_p;
4796 bool array_p;
4797 tree expression;
4799 /* Look for the optional `::' operator. */
4800 global_scope_p
4801 = (cp_parser_global_scope_opt (parser,
4802 /*current_scope_valid_p=*/false)
4803 != NULL_TREE);
4804 /* Look for the `delete' keyword. */
4805 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4806 /* See if the array syntax is in use. */
4807 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4809 /* Consume the `[' token. */
4810 cp_lexer_consume_token (parser->lexer);
4811 /* Look for the `]' token. */
4812 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4813 /* Remember that this is the `[]' construct. */
4814 array_p = true;
4816 else
4817 array_p = false;
4819 /* Parse the cast-expression. */
4820 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
4822 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4825 /* Parse a cast-expression.
4827 cast-expression:
4828 unary-expression
4829 ( type-id ) cast-expression
4831 Returns a representation of the expression. */
4833 static tree
4834 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4836 /* If it's a `(', then we might be looking at a cast. */
4837 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4839 tree type = NULL_TREE;
4840 tree expr = NULL_TREE;
4841 bool compound_literal_p;
4842 const char *saved_message;
4844 /* There's no way to know yet whether or not this is a cast.
4845 For example, `(int (3))' is a unary-expression, while `(int)
4846 3' is a cast. So, we resort to parsing tentatively. */
4847 cp_parser_parse_tentatively (parser);
4848 /* Types may not be defined in a cast. */
4849 saved_message = parser->type_definition_forbidden_message;
4850 parser->type_definition_forbidden_message
4851 = "types may not be defined in casts";
4852 /* Consume the `('. */
4853 cp_lexer_consume_token (parser->lexer);
4854 /* A very tricky bit is that `(struct S) { 3 }' is a
4855 compound-literal (which we permit in C++ as an extension).
4856 But, that construct is not a cast-expression -- it is a
4857 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4858 is legal; if the compound-literal were a cast-expression,
4859 you'd need an extra set of parentheses.) But, if we parse
4860 the type-id, and it happens to be a class-specifier, then we
4861 will commit to the parse at that point, because we cannot
4862 undo the action that is done when creating a new class. So,
4863 then we cannot back up and do a postfix-expression.
4865 Therefore, we scan ahead to the closing `)', and check to see
4866 if the token after the `)' is a `{'. If so, we are not
4867 looking at a cast-expression.
4869 Save tokens so that we can put them back. */
4870 cp_lexer_save_tokens (parser->lexer);
4871 /* Skip tokens until the next token is a closing parenthesis.
4872 If we find the closing `)', and the next token is a `{', then
4873 we are looking at a compound-literal. */
4874 compound_literal_p
4875 = (cp_parser_skip_to_closing_parenthesis (parser)
4876 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4877 /* Roll back the tokens we skipped. */
4878 cp_lexer_rollback_tokens (parser->lexer);
4879 /* If we were looking at a compound-literal, simulate an error
4880 so that the call to cp_parser_parse_definitely below will
4881 fail. */
4882 if (compound_literal_p)
4883 cp_parser_simulate_error (parser);
4884 else
4886 /* Look for the type-id. */
4887 type = cp_parser_type_id (parser);
4888 /* Look for the closing `)'. */
4889 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4892 /* Restore the saved message. */
4893 parser->type_definition_forbidden_message = saved_message;
4895 /* If ok so far, parse the dependent expression. We cannot be
4896 sure it is a cast. Consider `(T ())'. It is a parenthesized
4897 ctor of T, but looks like a cast to function returning T
4898 without a dependent expression. */
4899 if (!cp_parser_error_occurred (parser))
4900 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4902 if (cp_parser_parse_definitely (parser))
4904 /* Warn about old-style casts, if so requested. */
4905 if (warn_old_style_cast
4906 && !in_system_header
4907 && !VOID_TYPE_P (type)
4908 && current_lang_name != lang_name_c)
4909 warning ("use of old-style cast");
4911 /* Only type conversions to integral or enumeration types
4912 can be used in constant-expressions. */
4913 if (parser->constant_expression_p
4914 && !dependent_type_p (type)
4915 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4917 if (!parser->allow_non_constant_expression_p)
4918 return (cp_parser_non_constant_expression
4919 ("a casts to a type other than an integral or "
4920 "enumeration type"));
4921 parser->non_constant_expression_p = true;
4923 /* Perform the cast. */
4924 expr = build_c_cast (type, expr);
4925 return expr;
4929 /* If we get here, then it's not a cast, so it must be a
4930 unary-expression. */
4931 return cp_parser_unary_expression (parser, address_p);
4934 /* Parse a pm-expression.
4936 pm-expression:
4937 cast-expression
4938 pm-expression .* cast-expression
4939 pm-expression ->* cast-expression
4941 Returns a representation of the expression. */
4943 static tree
4944 cp_parser_pm_expression (cp_parser* parser)
4946 tree cast_expr;
4947 tree pm_expr;
4949 /* Parse the cast-expresion. */
4950 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4951 pm_expr = cast_expr;
4952 /* Now look for pointer-to-member operators. */
4953 while (true)
4955 cp_token *token;
4956 enum cpp_ttype token_type;
4958 /* Peek at the next token. */
4959 token = cp_lexer_peek_token (parser->lexer);
4960 token_type = token->type;
4961 /* If it's not `.*' or `->*' there's no pointer-to-member
4962 operation. */
4963 if (token_type != CPP_DOT_STAR
4964 && token_type != CPP_DEREF_STAR)
4965 break;
4967 /* Consume the token. */
4968 cp_lexer_consume_token (parser->lexer);
4970 /* Parse another cast-expression. */
4971 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4973 /* Build the representation of the pointer-to-member
4974 operation. */
4975 if (token_type == CPP_DEREF_STAR)
4976 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
4977 else
4978 pm_expr = build_m_component_ref (pm_expr, cast_expr);
4981 return pm_expr;
4984 /* Parse a multiplicative-expression.
4986 mulitplicative-expression:
4987 pm-expression
4988 multiplicative-expression * pm-expression
4989 multiplicative-expression / pm-expression
4990 multiplicative-expression % pm-expression
4992 Returns a representation of the expression. */
4994 static tree
4995 cp_parser_multiplicative_expression (cp_parser* parser)
4997 static const cp_parser_token_tree_map map = {
4998 { CPP_MULT, MULT_EXPR },
4999 { CPP_DIV, TRUNC_DIV_EXPR },
5000 { CPP_MOD, TRUNC_MOD_EXPR },
5001 { CPP_EOF, ERROR_MARK }
5004 return cp_parser_binary_expression (parser,
5005 map,
5006 cp_parser_pm_expression);
5009 /* Parse an additive-expression.
5011 additive-expression:
5012 multiplicative-expression
5013 additive-expression + multiplicative-expression
5014 additive-expression - multiplicative-expression
5016 Returns a representation of the expression. */
5018 static tree
5019 cp_parser_additive_expression (cp_parser* parser)
5021 static const cp_parser_token_tree_map map = {
5022 { CPP_PLUS, PLUS_EXPR },
5023 { CPP_MINUS, MINUS_EXPR },
5024 { CPP_EOF, ERROR_MARK }
5027 return cp_parser_binary_expression (parser,
5028 map,
5029 cp_parser_multiplicative_expression);
5032 /* Parse a shift-expression.
5034 shift-expression:
5035 additive-expression
5036 shift-expression << additive-expression
5037 shift-expression >> additive-expression
5039 Returns a representation of the expression. */
5041 static tree
5042 cp_parser_shift_expression (cp_parser* parser)
5044 static const cp_parser_token_tree_map map = {
5045 { CPP_LSHIFT, LSHIFT_EXPR },
5046 { CPP_RSHIFT, RSHIFT_EXPR },
5047 { CPP_EOF, ERROR_MARK }
5050 return cp_parser_binary_expression (parser,
5051 map,
5052 cp_parser_additive_expression);
5055 /* Parse a relational-expression.
5057 relational-expression:
5058 shift-expression
5059 relational-expression < shift-expression
5060 relational-expression > shift-expression
5061 relational-expression <= shift-expression
5062 relational-expression >= shift-expression
5064 GNU Extension:
5066 relational-expression:
5067 relational-expression <? shift-expression
5068 relational-expression >? shift-expression
5070 Returns a representation of the expression. */
5072 static tree
5073 cp_parser_relational_expression (cp_parser* parser)
5075 static const cp_parser_token_tree_map map = {
5076 { CPP_LESS, LT_EXPR },
5077 { CPP_GREATER, GT_EXPR },
5078 { CPP_LESS_EQ, LE_EXPR },
5079 { CPP_GREATER_EQ, GE_EXPR },
5080 { CPP_MIN, MIN_EXPR },
5081 { CPP_MAX, MAX_EXPR },
5082 { CPP_EOF, ERROR_MARK }
5085 return cp_parser_binary_expression (parser,
5086 map,
5087 cp_parser_shift_expression);
5090 /* Parse an equality-expression.
5092 equality-expression:
5093 relational-expression
5094 equality-expression == relational-expression
5095 equality-expression != relational-expression
5097 Returns a representation of the expression. */
5099 static tree
5100 cp_parser_equality_expression (cp_parser* parser)
5102 static const cp_parser_token_tree_map map = {
5103 { CPP_EQ_EQ, EQ_EXPR },
5104 { CPP_NOT_EQ, NE_EXPR },
5105 { CPP_EOF, ERROR_MARK }
5108 return cp_parser_binary_expression (parser,
5109 map,
5110 cp_parser_relational_expression);
5113 /* Parse an and-expression.
5115 and-expression:
5116 equality-expression
5117 and-expression & equality-expression
5119 Returns a representation of the expression. */
5121 static tree
5122 cp_parser_and_expression (cp_parser* parser)
5124 static const cp_parser_token_tree_map map = {
5125 { CPP_AND, BIT_AND_EXPR },
5126 { CPP_EOF, ERROR_MARK }
5129 return cp_parser_binary_expression (parser,
5130 map,
5131 cp_parser_equality_expression);
5134 /* Parse an exclusive-or-expression.
5136 exclusive-or-expression:
5137 and-expression
5138 exclusive-or-expression ^ and-expression
5140 Returns a representation of the expression. */
5142 static tree
5143 cp_parser_exclusive_or_expression (cp_parser* parser)
5145 static const cp_parser_token_tree_map map = {
5146 { CPP_XOR, BIT_XOR_EXPR },
5147 { CPP_EOF, ERROR_MARK }
5150 return cp_parser_binary_expression (parser,
5151 map,
5152 cp_parser_and_expression);
5156 /* Parse an inclusive-or-expression.
5158 inclusive-or-expression:
5159 exclusive-or-expression
5160 inclusive-or-expression | exclusive-or-expression
5162 Returns a representation of the expression. */
5164 static tree
5165 cp_parser_inclusive_or_expression (cp_parser* parser)
5167 static const cp_parser_token_tree_map map = {
5168 { CPP_OR, BIT_IOR_EXPR },
5169 { CPP_EOF, ERROR_MARK }
5172 return cp_parser_binary_expression (parser,
5173 map,
5174 cp_parser_exclusive_or_expression);
5177 /* Parse a logical-and-expression.
5179 logical-and-expression:
5180 inclusive-or-expression
5181 logical-and-expression && inclusive-or-expression
5183 Returns a representation of the expression. */
5185 static tree
5186 cp_parser_logical_and_expression (cp_parser* parser)
5188 static const cp_parser_token_tree_map map = {
5189 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5190 { CPP_EOF, ERROR_MARK }
5193 return cp_parser_binary_expression (parser,
5194 map,
5195 cp_parser_inclusive_or_expression);
5198 /* Parse a logical-or-expression.
5200 logical-or-expression:
5201 logical-and-expresion
5202 logical-or-expression || logical-and-expression
5204 Returns a representation of the expression. */
5206 static tree
5207 cp_parser_logical_or_expression (cp_parser* parser)
5209 static const cp_parser_token_tree_map map = {
5210 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5211 { CPP_EOF, ERROR_MARK }
5214 return cp_parser_binary_expression (parser,
5215 map,
5216 cp_parser_logical_and_expression);
5219 /* Parse a conditional-expression.
5221 conditional-expression:
5222 logical-or-expression
5223 logical-or-expression ? expression : assignment-expression
5225 GNU Extensions:
5227 conditional-expression:
5228 logical-or-expression ? : assignment-expression
5230 Returns a representation of the expression. */
5232 static tree
5233 cp_parser_conditional_expression (cp_parser* parser)
5235 tree logical_or_expr;
5237 /* Parse the logical-or-expression. */
5238 logical_or_expr = cp_parser_logical_or_expression (parser);
5239 /* If the next token is a `?', then we have a real conditional
5240 expression. */
5241 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5242 return cp_parser_question_colon_clause (parser, logical_or_expr);
5243 /* Otherwise, the value is simply the logical-or-expression. */
5244 else
5245 return logical_or_expr;
5248 /* Parse the `? expression : assignment-expression' part of a
5249 conditional-expression. The LOGICAL_OR_EXPR is the
5250 logical-or-expression that started the conditional-expression.
5251 Returns a representation of the entire conditional-expression.
5253 This routine exists only so that it can be shared between
5254 cp_parser_conditional_expression and
5255 cp_parser_assignment_expression.
5257 ? expression : assignment-expression
5259 GNU Extensions:
5261 ? : assignment-expression */
5263 static tree
5264 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5266 tree expr;
5267 tree assignment_expr;
5269 /* Consume the `?' token. */
5270 cp_lexer_consume_token (parser->lexer);
5271 if (cp_parser_allow_gnu_extensions_p (parser)
5272 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5273 /* Implicit true clause. */
5274 expr = NULL_TREE;
5275 else
5276 /* Parse the expression. */
5277 expr = cp_parser_expression (parser);
5279 /* The next token should be a `:'. */
5280 cp_parser_require (parser, CPP_COLON, "`:'");
5281 /* Parse the assignment-expression. */
5282 assignment_expr = cp_parser_assignment_expression (parser);
5284 /* Build the conditional-expression. */
5285 return build_x_conditional_expr (logical_or_expr,
5286 expr,
5287 assignment_expr);
5290 /* Parse an assignment-expression.
5292 assignment-expression:
5293 conditional-expression
5294 logical-or-expression assignment-operator assignment_expression
5295 throw-expression
5297 Returns a representation for the expression. */
5299 static tree
5300 cp_parser_assignment_expression (cp_parser* parser)
5302 tree expr;
5304 /* If the next token is the `throw' keyword, then we're looking at
5305 a throw-expression. */
5306 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5307 expr = cp_parser_throw_expression (parser);
5308 /* Otherwise, it must be that we are looking at a
5309 logical-or-expression. */
5310 else
5312 /* Parse the logical-or-expression. */
5313 expr = cp_parser_logical_or_expression (parser);
5314 /* If the next token is a `?' then we're actually looking at a
5315 conditional-expression. */
5316 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5317 return cp_parser_question_colon_clause (parser, expr);
5318 else
5320 enum tree_code assignment_operator;
5322 /* If it's an assignment-operator, we're using the second
5323 production. */
5324 assignment_operator
5325 = cp_parser_assignment_operator_opt (parser);
5326 if (assignment_operator != ERROR_MARK)
5328 tree rhs;
5330 /* Parse the right-hand side of the assignment. */
5331 rhs = cp_parser_assignment_expression (parser);
5332 /* An assignment may not appear in a
5333 constant-expression. */
5334 if (parser->constant_expression_p)
5336 if (!parser->allow_non_constant_expression_p)
5337 return cp_parser_non_constant_expression ("an assignment");
5338 parser->non_constant_expression_p = true;
5340 /* Build the asignment expression. */
5341 expr = build_x_modify_expr (expr,
5342 assignment_operator,
5343 rhs);
5348 return expr;
5351 /* Parse an (optional) assignment-operator.
5353 assignment-operator: one of
5354 = *= /= %= += -= >>= <<= &= ^= |=
5356 GNU Extension:
5358 assignment-operator: one of
5359 <?= >?=
5361 If the next token is an assignment operator, the corresponding tree
5362 code is returned, and the token is consumed. For example, for
5363 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5364 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5365 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5366 operator, ERROR_MARK is returned. */
5368 static enum tree_code
5369 cp_parser_assignment_operator_opt (cp_parser* parser)
5371 enum tree_code op;
5372 cp_token *token;
5374 /* Peek at the next toen. */
5375 token = cp_lexer_peek_token (parser->lexer);
5377 switch (token->type)
5379 case CPP_EQ:
5380 op = NOP_EXPR;
5381 break;
5383 case CPP_MULT_EQ:
5384 op = MULT_EXPR;
5385 break;
5387 case CPP_DIV_EQ:
5388 op = TRUNC_DIV_EXPR;
5389 break;
5391 case CPP_MOD_EQ:
5392 op = TRUNC_MOD_EXPR;
5393 break;
5395 case CPP_PLUS_EQ:
5396 op = PLUS_EXPR;
5397 break;
5399 case CPP_MINUS_EQ:
5400 op = MINUS_EXPR;
5401 break;
5403 case CPP_RSHIFT_EQ:
5404 op = RSHIFT_EXPR;
5405 break;
5407 case CPP_LSHIFT_EQ:
5408 op = LSHIFT_EXPR;
5409 break;
5411 case CPP_AND_EQ:
5412 op = BIT_AND_EXPR;
5413 break;
5415 case CPP_XOR_EQ:
5416 op = BIT_XOR_EXPR;
5417 break;
5419 case CPP_OR_EQ:
5420 op = BIT_IOR_EXPR;
5421 break;
5423 case CPP_MIN_EQ:
5424 op = MIN_EXPR;
5425 break;
5427 case CPP_MAX_EQ:
5428 op = MAX_EXPR;
5429 break;
5431 default:
5432 /* Nothing else is an assignment operator. */
5433 op = ERROR_MARK;
5436 /* If it was an assignment operator, consume it. */
5437 if (op != ERROR_MARK)
5438 cp_lexer_consume_token (parser->lexer);
5440 return op;
5443 /* Parse an expression.
5445 expression:
5446 assignment-expression
5447 expression , assignment-expression
5449 Returns a representation of the expression. */
5451 static tree
5452 cp_parser_expression (cp_parser* parser)
5454 tree expression = NULL_TREE;
5455 bool saw_comma_p = false;
5457 while (true)
5459 tree assignment_expression;
5461 /* Parse the next assignment-expression. */
5462 assignment_expression
5463 = cp_parser_assignment_expression (parser);
5464 /* If this is the first assignment-expression, we can just
5465 save it away. */
5466 if (!expression)
5467 expression = assignment_expression;
5468 /* Otherwise, chain the expressions together. It is unclear why
5469 we do not simply build COMPOUND_EXPRs as we go. */
5470 else
5471 expression = tree_cons (NULL_TREE,
5472 assignment_expression,
5473 expression);
5474 /* If the next token is not a comma, then we are done with the
5475 expression. */
5476 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5477 break;
5478 /* Consume the `,'. */
5479 cp_lexer_consume_token (parser->lexer);
5480 /* The first time we see a `,', we must take special action
5481 because the representation used for a single expression is
5482 different from that used for a list containing the single
5483 expression. */
5484 if (!saw_comma_p)
5486 /* Remember that this expression has a `,' in it. */
5487 saw_comma_p = true;
5488 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5489 additional expressions to it. */
5490 expression = build_tree_list (NULL_TREE, expression);
5494 /* Build a COMPOUND_EXPR to represent the entire expression, if
5495 necessary. We built up the list in reverse order, so we must
5496 straighten it out here. */
5497 if (saw_comma_p)
5499 /* A comma operator cannot appear in a constant-expression. */
5500 if (parser->constant_expression_p)
5502 if (!parser->allow_non_constant_expression_p)
5503 return cp_parser_non_constant_expression ("a comma operator");
5504 parser->non_constant_expression_p = true;
5506 expression = build_x_compound_expr (nreverse (expression));
5509 return expression;
5512 /* Parse a constant-expression.
5514 constant-expression:
5515 conditional-expression
5517 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5518 accepted. In that case *NON_CONSTANT_P is set to TRUE. If
5519 ALLOW_NON_CONSTANT_P is false, NON_CONSTANT_P should be NULL. */
5521 static tree
5522 cp_parser_constant_expression (cp_parser* parser,
5523 bool allow_non_constant_p,
5524 bool *non_constant_p)
5526 bool saved_constant_expression_p;
5527 bool saved_allow_non_constant_expression_p;
5528 bool saved_non_constant_expression_p;
5529 tree expression;
5531 /* It might seem that we could simply parse the
5532 conditional-expression, and then check to see if it were
5533 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5534 one that the compiler can figure out is constant, possibly after
5535 doing some simplifications or optimizations. The standard has a
5536 precise definition of constant-expression, and we must honor
5537 that, even though it is somewhat more restrictive.
5539 For example:
5541 int i[(2, 3)];
5543 is not a legal declaration, because `(2, 3)' is not a
5544 constant-expression. The `,' operator is forbidden in a
5545 constant-expression. However, GCC's constant-folding machinery
5546 will fold this operation to an INTEGER_CST for `3'. */
5548 /* Save the old settings. */
5549 saved_constant_expression_p = parser->constant_expression_p;
5550 saved_allow_non_constant_expression_p
5551 = parser->allow_non_constant_expression_p;
5552 saved_non_constant_expression_p = parser->non_constant_expression_p;
5553 /* We are now parsing a constant-expression. */
5554 parser->constant_expression_p = true;
5555 parser->allow_non_constant_expression_p = allow_non_constant_p;
5556 parser->non_constant_expression_p = false;
5557 /* Parse the conditional-expression. */
5558 expression = cp_parser_conditional_expression (parser);
5559 /* Restore the old settings. */
5560 parser->constant_expression_p = saved_constant_expression_p;
5561 parser->allow_non_constant_expression_p
5562 = saved_allow_non_constant_expression_p;
5563 if (allow_non_constant_p)
5564 *non_constant_p = parser->non_constant_expression_p;
5565 parser->non_constant_expression_p = saved_non_constant_expression_p;
5567 return expression;
5570 /* Statements [gram.stmt.stmt] */
5572 /* Parse a statement.
5574 statement:
5575 labeled-statement
5576 expression-statement
5577 compound-statement
5578 selection-statement
5579 iteration-statement
5580 jump-statement
5581 declaration-statement
5582 try-block */
5584 static void
5585 cp_parser_statement (cp_parser* parser)
5587 tree statement;
5588 cp_token *token;
5589 int statement_line_number;
5591 /* There is no statement yet. */
5592 statement = NULL_TREE;
5593 /* Peek at the next token. */
5594 token = cp_lexer_peek_token (parser->lexer);
5595 /* Remember the line number of the first token in the statement. */
5596 statement_line_number = token->location.line;
5597 /* If this is a keyword, then that will often determine what kind of
5598 statement we have. */
5599 if (token->type == CPP_KEYWORD)
5601 enum rid keyword = token->keyword;
5603 switch (keyword)
5605 case RID_CASE:
5606 case RID_DEFAULT:
5607 statement = cp_parser_labeled_statement (parser);
5608 break;
5610 case RID_IF:
5611 case RID_SWITCH:
5612 statement = cp_parser_selection_statement (parser);
5613 break;
5615 case RID_WHILE:
5616 case RID_DO:
5617 case RID_FOR:
5618 statement = cp_parser_iteration_statement (parser);
5619 break;
5621 case RID_BREAK:
5622 case RID_CONTINUE:
5623 case RID_RETURN:
5624 case RID_GOTO:
5625 statement = cp_parser_jump_statement (parser);
5626 break;
5628 case RID_TRY:
5629 statement = cp_parser_try_block (parser);
5630 break;
5632 default:
5633 /* It might be a keyword like `int' that can start a
5634 declaration-statement. */
5635 break;
5638 else if (token->type == CPP_NAME)
5640 /* If the next token is a `:', then we are looking at a
5641 labeled-statement. */
5642 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5643 if (token->type == CPP_COLON)
5644 statement = cp_parser_labeled_statement (parser);
5646 /* Anything that starts with a `{' must be a compound-statement. */
5647 else if (token->type == CPP_OPEN_BRACE)
5648 statement = cp_parser_compound_statement (parser);
5650 /* Everything else must be a declaration-statement or an
5651 expression-statement. Try for the declaration-statement
5652 first, unless we are looking at a `;', in which case we know that
5653 we have an expression-statement. */
5654 if (!statement)
5656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5658 cp_parser_parse_tentatively (parser);
5659 /* Try to parse the declaration-statement. */
5660 cp_parser_declaration_statement (parser);
5661 /* If that worked, we're done. */
5662 if (cp_parser_parse_definitely (parser))
5663 return;
5665 /* Look for an expression-statement instead. */
5666 statement = cp_parser_expression_statement (parser);
5669 /* Set the line number for the statement. */
5670 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5671 STMT_LINENO (statement) = statement_line_number;
5674 /* Parse a labeled-statement.
5676 labeled-statement:
5677 identifier : statement
5678 case constant-expression : statement
5679 default : statement
5681 Returns the new CASE_LABEL, for a `case' or `default' label. For
5682 an ordinary label, returns a LABEL_STMT. */
5684 static tree
5685 cp_parser_labeled_statement (cp_parser* parser)
5687 cp_token *token;
5688 tree statement = NULL_TREE;
5690 /* The next token should be an identifier. */
5691 token = cp_lexer_peek_token (parser->lexer);
5692 if (token->type != CPP_NAME
5693 && token->type != CPP_KEYWORD)
5695 cp_parser_error (parser, "expected labeled-statement");
5696 return error_mark_node;
5699 switch (token->keyword)
5701 case RID_CASE:
5703 tree expr;
5705 /* Consume the `case' token. */
5706 cp_lexer_consume_token (parser->lexer);
5707 /* Parse the constant-expression. */
5708 expr = cp_parser_constant_expression (parser,
5709 /*allow_non_constant=*/false,
5710 NULL);
5711 /* Create the label. */
5712 statement = finish_case_label (expr, NULL_TREE);
5714 break;
5716 case RID_DEFAULT:
5717 /* Consume the `default' token. */
5718 cp_lexer_consume_token (parser->lexer);
5719 /* Create the label. */
5720 statement = finish_case_label (NULL_TREE, NULL_TREE);
5721 break;
5723 default:
5724 /* Anything else must be an ordinary label. */
5725 statement = finish_label_stmt (cp_parser_identifier (parser));
5726 break;
5729 /* Require the `:' token. */
5730 cp_parser_require (parser, CPP_COLON, "`:'");
5731 /* Parse the labeled statement. */
5732 cp_parser_statement (parser);
5734 /* Return the label, in the case of a `case' or `default' label. */
5735 return statement;
5738 /* Parse an expression-statement.
5740 expression-statement:
5741 expression [opt] ;
5743 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5744 statement consists of nothing more than an `;'. */
5746 static tree
5747 cp_parser_expression_statement (cp_parser* parser)
5749 tree statement;
5751 /* If the next token is not a `;', then there is an expression to parse. */
5752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5753 statement = finish_expr_stmt (cp_parser_expression (parser));
5754 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5755 else
5757 finish_stmt ();
5758 statement = NULL_TREE;
5760 /* Consume the final `;'. */
5761 cp_parser_consume_semicolon_at_end_of_statement (parser);
5763 return statement;
5766 /* Parse a compound-statement.
5768 compound-statement:
5769 { statement-seq [opt] }
5771 Returns a COMPOUND_STMT representing the statement. */
5773 static tree
5774 cp_parser_compound_statement (cp_parser *parser)
5776 tree compound_stmt;
5778 /* Consume the `{'. */
5779 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5780 return error_mark_node;
5781 /* Begin the compound-statement. */
5782 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5783 /* Parse an (optional) statement-seq. */
5784 cp_parser_statement_seq_opt (parser);
5785 /* Finish the compound-statement. */
5786 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5787 /* Consume the `}'. */
5788 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5790 return compound_stmt;
5793 /* Parse an (optional) statement-seq.
5795 statement-seq:
5796 statement
5797 statement-seq [opt] statement */
5799 static void
5800 cp_parser_statement_seq_opt (cp_parser* parser)
5802 /* Scan statements until there aren't any more. */
5803 while (true)
5805 /* If we're looking at a `}', then we've run out of statements. */
5806 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5807 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5808 break;
5810 /* Parse the statement. */
5811 cp_parser_statement (parser);
5815 /* Parse a selection-statement.
5817 selection-statement:
5818 if ( condition ) statement
5819 if ( condition ) statement else statement
5820 switch ( condition ) statement
5822 Returns the new IF_STMT or SWITCH_STMT. */
5824 static tree
5825 cp_parser_selection_statement (cp_parser* parser)
5827 cp_token *token;
5828 enum rid keyword;
5830 /* Peek at the next token. */
5831 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5833 /* See what kind of keyword it is. */
5834 keyword = token->keyword;
5835 switch (keyword)
5837 case RID_IF:
5838 case RID_SWITCH:
5840 tree statement;
5841 tree condition;
5843 /* Look for the `('. */
5844 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5846 cp_parser_skip_to_end_of_statement (parser);
5847 return error_mark_node;
5850 /* Begin the selection-statement. */
5851 if (keyword == RID_IF)
5852 statement = begin_if_stmt ();
5853 else
5854 statement = begin_switch_stmt ();
5856 /* Parse the condition. */
5857 condition = cp_parser_condition (parser);
5858 /* Look for the `)'. */
5859 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5860 cp_parser_skip_to_closing_parenthesis (parser);
5862 if (keyword == RID_IF)
5864 tree then_stmt;
5866 /* Add the condition. */
5867 finish_if_stmt_cond (condition, statement);
5869 /* Parse the then-clause. */
5870 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5871 finish_then_clause (statement);
5873 /* If the next token is `else', parse the else-clause. */
5874 if (cp_lexer_next_token_is_keyword (parser->lexer,
5875 RID_ELSE))
5877 tree else_stmt;
5879 /* Consume the `else' keyword. */
5880 cp_lexer_consume_token (parser->lexer);
5881 /* Parse the else-clause. */
5882 else_stmt
5883 = cp_parser_implicitly_scoped_statement (parser);
5884 finish_else_clause (statement);
5887 /* Now we're all done with the if-statement. */
5888 finish_if_stmt ();
5890 else
5892 tree body;
5894 /* Add the condition. */
5895 finish_switch_cond (condition, statement);
5897 /* Parse the body of the switch-statement. */
5898 body = cp_parser_implicitly_scoped_statement (parser);
5900 /* Now we're all done with the switch-statement. */
5901 finish_switch_stmt (statement);
5904 return statement;
5906 break;
5908 default:
5909 cp_parser_error (parser, "expected selection-statement");
5910 return error_mark_node;
5914 /* Parse a condition.
5916 condition:
5917 expression
5918 type-specifier-seq declarator = assignment-expression
5920 GNU Extension:
5922 condition:
5923 type-specifier-seq declarator asm-specification [opt]
5924 attributes [opt] = assignment-expression
5926 Returns the expression that should be tested. */
5928 static tree
5929 cp_parser_condition (cp_parser* parser)
5931 tree type_specifiers;
5932 const char *saved_message;
5934 /* Try the declaration first. */
5935 cp_parser_parse_tentatively (parser);
5936 /* New types are not allowed in the type-specifier-seq for a
5937 condition. */
5938 saved_message = parser->type_definition_forbidden_message;
5939 parser->type_definition_forbidden_message
5940 = "types may not be defined in conditions";
5941 /* Parse the type-specifier-seq. */
5942 type_specifiers = cp_parser_type_specifier_seq (parser);
5943 /* Restore the saved message. */
5944 parser->type_definition_forbidden_message = saved_message;
5945 /* If all is well, we might be looking at a declaration. */
5946 if (!cp_parser_error_occurred (parser))
5948 tree decl;
5949 tree asm_specification;
5950 tree attributes;
5951 tree declarator;
5952 tree initializer = NULL_TREE;
5954 /* Parse the declarator. */
5955 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5956 /*ctor_dtor_or_conv_p=*/NULL);
5957 /* Parse the attributes. */
5958 attributes = cp_parser_attributes_opt (parser);
5959 /* Parse the asm-specification. */
5960 asm_specification = cp_parser_asm_specification_opt (parser);
5961 /* If the next token is not an `=', then we might still be
5962 looking at an expression. For example:
5964 if (A(a).x)
5966 looks like a decl-specifier-seq and a declarator -- but then
5967 there is no `=', so this is an expression. */
5968 cp_parser_require (parser, CPP_EQ, "`='");
5969 /* If we did see an `=', then we are looking at a declaration
5970 for sure. */
5971 if (cp_parser_parse_definitely (parser))
5973 /* Create the declaration. */
5974 decl = start_decl (declarator, type_specifiers,
5975 /*initialized_p=*/true,
5976 attributes, /*prefix_attributes=*/NULL_TREE);
5977 /* Parse the assignment-expression. */
5978 initializer = cp_parser_assignment_expression (parser);
5980 /* Process the initializer. */
5981 cp_finish_decl (decl,
5982 initializer,
5983 asm_specification,
5984 LOOKUP_ONLYCONVERTING);
5986 return convert_from_reference (decl);
5989 /* If we didn't even get past the declarator successfully, we are
5990 definitely not looking at a declaration. */
5991 else
5992 cp_parser_abort_tentative_parse (parser);
5994 /* Otherwise, we are looking at an expression. */
5995 return cp_parser_expression (parser);
5998 /* Parse an iteration-statement.
6000 iteration-statement:
6001 while ( condition ) statement
6002 do statement while ( expression ) ;
6003 for ( for-init-statement condition [opt] ; expression [opt] )
6004 statement
6006 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6008 static tree
6009 cp_parser_iteration_statement (cp_parser* parser)
6011 cp_token *token;
6012 enum rid keyword;
6013 tree statement;
6015 /* Peek at the next token. */
6016 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6017 if (!token)
6018 return error_mark_node;
6020 /* See what kind of keyword it is. */
6021 keyword = token->keyword;
6022 switch (keyword)
6024 case RID_WHILE:
6026 tree condition;
6028 /* Begin the while-statement. */
6029 statement = begin_while_stmt ();
6030 /* Look for the `('. */
6031 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6032 /* Parse the condition. */
6033 condition = cp_parser_condition (parser);
6034 finish_while_stmt_cond (condition, statement);
6035 /* Look for the `)'. */
6036 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6037 /* Parse the dependent statement. */
6038 cp_parser_already_scoped_statement (parser);
6039 /* We're done with the while-statement. */
6040 finish_while_stmt (statement);
6042 break;
6044 case RID_DO:
6046 tree expression;
6048 /* Begin the do-statement. */
6049 statement = begin_do_stmt ();
6050 /* Parse the body of the do-statement. */
6051 cp_parser_implicitly_scoped_statement (parser);
6052 finish_do_body (statement);
6053 /* Look for the `while' keyword. */
6054 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6055 /* Look for the `('. */
6056 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6057 /* Parse the expression. */
6058 expression = cp_parser_expression (parser);
6059 /* We're done with the do-statement. */
6060 finish_do_stmt (expression, statement);
6061 /* Look for the `)'. */
6062 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6063 /* Look for the `;'. */
6064 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6066 break;
6068 case RID_FOR:
6070 tree condition = NULL_TREE;
6071 tree expression = NULL_TREE;
6073 /* Begin the for-statement. */
6074 statement = begin_for_stmt ();
6075 /* Look for the `('. */
6076 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6077 /* Parse the initialization. */
6078 cp_parser_for_init_statement (parser);
6079 finish_for_init_stmt (statement);
6081 /* If there's a condition, process it. */
6082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6083 condition = cp_parser_condition (parser);
6084 finish_for_cond (condition, statement);
6085 /* Look for the `;'. */
6086 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6088 /* If there's an expression, process it. */
6089 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6090 expression = cp_parser_expression (parser);
6091 finish_for_expr (expression, statement);
6092 /* Look for the `)'. */
6093 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6095 /* Parse the body of the for-statement. */
6096 cp_parser_already_scoped_statement (parser);
6098 /* We're done with the for-statement. */
6099 finish_for_stmt (statement);
6101 break;
6103 default:
6104 cp_parser_error (parser, "expected iteration-statement");
6105 statement = error_mark_node;
6106 break;
6109 return statement;
6112 /* Parse a for-init-statement.
6114 for-init-statement:
6115 expression-statement
6116 simple-declaration */
6118 static void
6119 cp_parser_for_init_statement (cp_parser* parser)
6121 /* If the next token is a `;', then we have an empty
6122 expression-statement. Gramatically, this is also a
6123 simple-declaration, but an invalid one, because it does not
6124 declare anything. Therefore, if we did not handle this case
6125 specially, we would issue an error message about an invalid
6126 declaration. */
6127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6129 /* We're going to speculatively look for a declaration, falling back
6130 to an expression, if necessary. */
6131 cp_parser_parse_tentatively (parser);
6132 /* Parse the declaration. */
6133 cp_parser_simple_declaration (parser,
6134 /*function_definition_allowed_p=*/false);
6135 /* If the tentative parse failed, then we shall need to look for an
6136 expression-statement. */
6137 if (cp_parser_parse_definitely (parser))
6138 return;
6141 cp_parser_expression_statement (parser);
6144 /* Parse a jump-statement.
6146 jump-statement:
6147 break ;
6148 continue ;
6149 return expression [opt] ;
6150 goto identifier ;
6152 GNU extension:
6154 jump-statement:
6155 goto * expression ;
6157 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6158 GOTO_STMT. */
6160 static tree
6161 cp_parser_jump_statement (cp_parser* parser)
6163 tree statement = error_mark_node;
6164 cp_token *token;
6165 enum rid keyword;
6167 /* Peek at the next token. */
6168 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6169 if (!token)
6170 return error_mark_node;
6172 /* See what kind of keyword it is. */
6173 keyword = token->keyword;
6174 switch (keyword)
6176 case RID_BREAK:
6177 statement = finish_break_stmt ();
6178 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6179 break;
6181 case RID_CONTINUE:
6182 statement = finish_continue_stmt ();
6183 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6184 break;
6186 case RID_RETURN:
6188 tree expr;
6190 /* If the next token is a `;', then there is no
6191 expression. */
6192 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6193 expr = cp_parser_expression (parser);
6194 else
6195 expr = NULL_TREE;
6196 /* Build the return-statement. */
6197 statement = finish_return_stmt (expr);
6198 /* Look for the final `;'. */
6199 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6201 break;
6203 case RID_GOTO:
6204 /* Create the goto-statement. */
6205 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6207 /* Issue a warning about this use of a GNU extension. */
6208 if (pedantic)
6209 pedwarn ("ISO C++ forbids computed gotos");
6210 /* Consume the '*' token. */
6211 cp_lexer_consume_token (parser->lexer);
6212 /* Parse the dependent expression. */
6213 finish_goto_stmt (cp_parser_expression (parser));
6215 else
6216 finish_goto_stmt (cp_parser_identifier (parser));
6217 /* Look for the final `;'. */
6218 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6219 break;
6221 default:
6222 cp_parser_error (parser, "expected jump-statement");
6223 break;
6226 return statement;
6229 /* Parse a declaration-statement.
6231 declaration-statement:
6232 block-declaration */
6234 static void
6235 cp_parser_declaration_statement (cp_parser* parser)
6237 /* Parse the block-declaration. */
6238 cp_parser_block_declaration (parser, /*statement_p=*/true);
6240 /* Finish off the statement. */
6241 finish_stmt ();
6244 /* Some dependent statements (like `if (cond) statement'), are
6245 implicitly in their own scope. In other words, if the statement is
6246 a single statement (as opposed to a compound-statement), it is
6247 none-the-less treated as if it were enclosed in braces. Any
6248 declarations appearing in the dependent statement are out of scope
6249 after control passes that point. This function parses a statement,
6250 but ensures that is in its own scope, even if it is not a
6251 compound-statement.
6253 Returns the new statement. */
6255 static tree
6256 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6258 tree statement;
6260 /* If the token is not a `{', then we must take special action. */
6261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6263 /* Create a compound-statement. */
6264 statement = begin_compound_stmt (/*has_no_scope=*/0);
6265 /* Parse the dependent-statement. */
6266 cp_parser_statement (parser);
6267 /* Finish the dummy compound-statement. */
6268 finish_compound_stmt (/*has_no_scope=*/0, statement);
6270 /* Otherwise, we simply parse the statement directly. */
6271 else
6272 statement = cp_parser_compound_statement (parser);
6274 /* Return the statement. */
6275 return statement;
6278 /* For some dependent statements (like `while (cond) statement'), we
6279 have already created a scope. Therefore, even if the dependent
6280 statement is a compound-statement, we do not want to create another
6281 scope. */
6283 static void
6284 cp_parser_already_scoped_statement (cp_parser* parser)
6286 /* If the token is not a `{', then we must take special action. */
6287 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6289 tree statement;
6291 /* Create a compound-statement. */
6292 statement = begin_compound_stmt (/*has_no_scope=*/1);
6293 /* Parse the dependent-statement. */
6294 cp_parser_statement (parser);
6295 /* Finish the dummy compound-statement. */
6296 finish_compound_stmt (/*has_no_scope=*/1, statement);
6298 /* Otherwise, we simply parse the statement directly. */
6299 else
6300 cp_parser_statement (parser);
6303 /* Declarations [gram.dcl.dcl] */
6305 /* Parse an optional declaration-sequence.
6307 declaration-seq:
6308 declaration
6309 declaration-seq declaration */
6311 static void
6312 cp_parser_declaration_seq_opt (cp_parser* parser)
6314 while (true)
6316 cp_token *token;
6318 token = cp_lexer_peek_token (parser->lexer);
6320 if (token->type == CPP_CLOSE_BRACE
6321 || token->type == CPP_EOF)
6322 break;
6324 if (token->type == CPP_SEMICOLON)
6326 /* A declaration consisting of a single semicolon is
6327 invalid. Allow it unless we're being pedantic. */
6328 if (pedantic)
6329 pedwarn ("extra `;'");
6330 cp_lexer_consume_token (parser->lexer);
6331 continue;
6334 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6335 parser to enter or exit implict `extern "C"' blocks. */
6336 while (pending_lang_change > 0)
6338 push_lang_context (lang_name_c);
6339 --pending_lang_change;
6341 while (pending_lang_change < 0)
6343 pop_lang_context ();
6344 ++pending_lang_change;
6347 /* Parse the declaration itself. */
6348 cp_parser_declaration (parser);
6352 /* Parse a declaration.
6354 declaration:
6355 block-declaration
6356 function-definition
6357 template-declaration
6358 explicit-instantiation
6359 explicit-specialization
6360 linkage-specification
6361 namespace-definition
6363 GNU extension:
6365 declaration:
6366 __extension__ declaration */
6368 static void
6369 cp_parser_declaration (cp_parser* parser)
6371 cp_token token1;
6372 cp_token token2;
6373 int saved_pedantic;
6375 /* Check for the `__extension__' keyword. */
6376 if (cp_parser_extension_opt (parser, &saved_pedantic))
6378 /* Parse the qualified declaration. */
6379 cp_parser_declaration (parser);
6380 /* Restore the PEDANTIC flag. */
6381 pedantic = saved_pedantic;
6383 return;
6386 /* Try to figure out what kind of declaration is present. */
6387 token1 = *cp_lexer_peek_token (parser->lexer);
6388 if (token1.type != CPP_EOF)
6389 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6391 /* If the next token is `extern' and the following token is a string
6392 literal, then we have a linkage specification. */
6393 if (token1.keyword == RID_EXTERN
6394 && cp_parser_is_string_literal (&token2))
6395 cp_parser_linkage_specification (parser);
6396 /* If the next token is `template', then we have either a template
6397 declaration, an explicit instantiation, or an explicit
6398 specialization. */
6399 else if (token1.keyword == RID_TEMPLATE)
6401 /* `template <>' indicates a template specialization. */
6402 if (token2.type == CPP_LESS
6403 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6404 cp_parser_explicit_specialization (parser);
6405 /* `template <' indicates a template declaration. */
6406 else if (token2.type == CPP_LESS)
6407 cp_parser_template_declaration (parser, /*member_p=*/false);
6408 /* Anything else must be an explicit instantiation. */
6409 else
6410 cp_parser_explicit_instantiation (parser);
6412 /* If the next token is `export', then we have a template
6413 declaration. */
6414 else if (token1.keyword == RID_EXPORT)
6415 cp_parser_template_declaration (parser, /*member_p=*/false);
6416 /* If the next token is `extern', 'static' or 'inline' and the one
6417 after that is `template', we have a GNU extended explicit
6418 instantiation directive. */
6419 else if (cp_parser_allow_gnu_extensions_p (parser)
6420 && (token1.keyword == RID_EXTERN
6421 || token1.keyword == RID_STATIC
6422 || token1.keyword == RID_INLINE)
6423 && token2.keyword == RID_TEMPLATE)
6424 cp_parser_explicit_instantiation (parser);
6425 /* If the next token is `namespace', check for a named or unnamed
6426 namespace definition. */
6427 else if (token1.keyword == RID_NAMESPACE
6428 && (/* A named namespace definition. */
6429 (token2.type == CPP_NAME
6430 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6431 == CPP_OPEN_BRACE))
6432 /* An unnamed namespace definition. */
6433 || token2.type == CPP_OPEN_BRACE))
6434 cp_parser_namespace_definition (parser);
6435 /* We must have either a block declaration or a function
6436 definition. */
6437 else
6438 /* Try to parse a block-declaration, or a function-definition. */
6439 cp_parser_block_declaration (parser, /*statement_p=*/false);
6442 /* Parse a block-declaration.
6444 block-declaration:
6445 simple-declaration
6446 asm-definition
6447 namespace-alias-definition
6448 using-declaration
6449 using-directive
6451 GNU Extension:
6453 block-declaration:
6454 __extension__ block-declaration
6455 label-declaration
6457 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6458 part of a declaration-statement. */
6460 static void
6461 cp_parser_block_declaration (cp_parser *parser,
6462 bool statement_p)
6464 cp_token *token1;
6465 int saved_pedantic;
6467 /* Check for the `__extension__' keyword. */
6468 if (cp_parser_extension_opt (parser, &saved_pedantic))
6470 /* Parse the qualified declaration. */
6471 cp_parser_block_declaration (parser, statement_p);
6472 /* Restore the PEDANTIC flag. */
6473 pedantic = saved_pedantic;
6475 return;
6478 /* Peek at the next token to figure out which kind of declaration is
6479 present. */
6480 token1 = cp_lexer_peek_token (parser->lexer);
6482 /* If the next keyword is `asm', we have an asm-definition. */
6483 if (token1->keyword == RID_ASM)
6485 if (statement_p)
6486 cp_parser_commit_to_tentative_parse (parser);
6487 cp_parser_asm_definition (parser);
6489 /* If the next keyword is `namespace', we have a
6490 namespace-alias-definition. */
6491 else if (token1->keyword == RID_NAMESPACE)
6492 cp_parser_namespace_alias_definition (parser);
6493 /* If the next keyword is `using', we have either a
6494 using-declaration or a using-directive. */
6495 else if (token1->keyword == RID_USING)
6497 cp_token *token2;
6499 if (statement_p)
6500 cp_parser_commit_to_tentative_parse (parser);
6501 /* If the token after `using' is `namespace', then we have a
6502 using-directive. */
6503 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6504 if (token2->keyword == RID_NAMESPACE)
6505 cp_parser_using_directive (parser);
6506 /* Otherwise, it's a using-declaration. */
6507 else
6508 cp_parser_using_declaration (parser);
6510 /* If the next keyword is `__label__' we have a label declaration. */
6511 else if (token1->keyword == RID_LABEL)
6513 if (statement_p)
6514 cp_parser_commit_to_tentative_parse (parser);
6515 cp_parser_label_declaration (parser);
6517 /* Anything else must be a simple-declaration. */
6518 else
6519 cp_parser_simple_declaration (parser, !statement_p);
6522 /* Parse a simple-declaration.
6524 simple-declaration:
6525 decl-specifier-seq [opt] init-declarator-list [opt] ;
6527 init-declarator-list:
6528 init-declarator
6529 init-declarator-list , init-declarator
6531 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6532 function-definition as a simple-declaration. */
6534 static void
6535 cp_parser_simple_declaration (cp_parser* parser,
6536 bool function_definition_allowed_p)
6538 tree decl_specifiers;
6539 tree attributes;
6540 bool declares_class_or_enum;
6541 bool saw_declarator;
6543 /* Defer access checks until we know what is being declared; the
6544 checks for names appearing in the decl-specifier-seq should be
6545 done as if we were in the scope of the thing being declared. */
6546 push_deferring_access_checks (dk_deferred);
6548 /* Parse the decl-specifier-seq. We have to keep track of whether
6549 or not the decl-specifier-seq declares a named class or
6550 enumeration type, since that is the only case in which the
6551 init-declarator-list is allowed to be empty.
6553 [dcl.dcl]
6555 In a simple-declaration, the optional init-declarator-list can be
6556 omitted only when declaring a class or enumeration, that is when
6557 the decl-specifier-seq contains either a class-specifier, an
6558 elaborated-type-specifier, or an enum-specifier. */
6559 decl_specifiers
6560 = cp_parser_decl_specifier_seq (parser,
6561 CP_PARSER_FLAGS_OPTIONAL,
6562 &attributes,
6563 &declares_class_or_enum);
6564 /* We no longer need to defer access checks. */
6565 stop_deferring_access_checks ();
6567 /* If the next two tokens are both identifiers, the code is
6568 erroneous. The usual cause of this situation is code like:
6570 T t;
6572 where "T" should name a type -- but does not. */
6573 if (cp_parser_diagnose_invalid_type_name (parser))
6575 /* If parsing tentatively, we should commit; we really are
6576 looking at a declaration. */
6577 cp_parser_commit_to_tentative_parse (parser);
6578 /* Give up. */
6579 return;
6582 /* Keep going until we hit the `;' at the end of the simple
6583 declaration. */
6584 saw_declarator = false;
6585 while (cp_lexer_next_token_is_not (parser->lexer,
6586 CPP_SEMICOLON))
6588 cp_token *token;
6589 bool function_definition_p;
6591 saw_declarator = true;
6592 /* Parse the init-declarator. */
6593 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6594 function_definition_allowed_p,
6595 /*member_p=*/false,
6596 &function_definition_p);
6597 /* If an error occurred while parsing tentatively, exit quickly.
6598 (That usually happens when in the body of a function; each
6599 statement is treated as a declaration-statement until proven
6600 otherwise.) */
6601 if (cp_parser_error_occurred (parser))
6603 pop_deferring_access_checks ();
6604 return;
6606 /* Handle function definitions specially. */
6607 if (function_definition_p)
6609 /* If the next token is a `,', then we are probably
6610 processing something like:
6612 void f() {}, *p;
6614 which is erroneous. */
6615 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6616 error ("mixing declarations and function-definitions is forbidden");
6617 /* Otherwise, we're done with the list of declarators. */
6618 else
6620 pop_deferring_access_checks ();
6621 return;
6624 /* The next token should be either a `,' or a `;'. */
6625 token = cp_lexer_peek_token (parser->lexer);
6626 /* If it's a `,', there are more declarators to come. */
6627 if (token->type == CPP_COMMA)
6628 cp_lexer_consume_token (parser->lexer);
6629 /* If it's a `;', we are done. */
6630 else if (token->type == CPP_SEMICOLON)
6631 break;
6632 /* Anything else is an error. */
6633 else
6635 cp_parser_error (parser, "expected `,' or `;'");
6636 /* Skip tokens until we reach the end of the statement. */
6637 cp_parser_skip_to_end_of_statement (parser);
6638 pop_deferring_access_checks ();
6639 return;
6641 /* After the first time around, a function-definition is not
6642 allowed -- even if it was OK at first. For example:
6644 int i, f() {}
6646 is not valid. */
6647 function_definition_allowed_p = false;
6650 /* Issue an error message if no declarators are present, and the
6651 decl-specifier-seq does not itself declare a class or
6652 enumeration. */
6653 if (!saw_declarator)
6655 if (cp_parser_declares_only_class_p (parser))
6656 shadow_tag (decl_specifiers);
6657 /* Perform any deferred access checks. */
6658 perform_deferred_access_checks ();
6661 pop_deferring_access_checks ();
6663 /* Consume the `;'. */
6664 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6666 /* Mark all the classes that appeared in the decl-specifier-seq as
6667 having received a `;'. */
6668 note_list_got_semicolon (decl_specifiers);
6671 /* Parse a decl-specifier-seq.
6673 decl-specifier-seq:
6674 decl-specifier-seq [opt] decl-specifier
6676 decl-specifier:
6677 storage-class-specifier
6678 type-specifier
6679 function-specifier
6680 friend
6681 typedef
6683 GNU Extension:
6685 decl-specifier-seq:
6686 decl-specifier-seq [opt] attributes
6688 Returns a TREE_LIST, giving the decl-specifiers in the order they
6689 appear in the source code. The TREE_VALUE of each node is the
6690 decl-specifier. For a keyword (such as `auto' or `friend'), the
6691 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6692 representation of a type-specifier, see cp_parser_type_specifier.
6694 If there are attributes, they will be stored in *ATTRIBUTES,
6695 represented as described above cp_parser_attributes.
6697 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6698 appears, and the entity that will be a friend is not going to be a
6699 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6700 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6701 friendship is granted might not be a class. */
6703 static tree
6704 cp_parser_decl_specifier_seq (cp_parser* parser,
6705 cp_parser_flags flags,
6706 tree* attributes,
6707 bool* declares_class_or_enum)
6709 tree decl_specs = NULL_TREE;
6710 bool friend_p = false;
6711 bool constructor_possible_p = !parser->in_declarator_p;
6713 /* Assume no class or enumeration type is declared. */
6714 *declares_class_or_enum = false;
6716 /* Assume there are no attributes. */
6717 *attributes = NULL_TREE;
6719 /* Keep reading specifiers until there are no more to read. */
6720 while (true)
6722 tree decl_spec = NULL_TREE;
6723 bool constructor_p;
6724 cp_token *token;
6726 /* Peek at the next token. */
6727 token = cp_lexer_peek_token (parser->lexer);
6728 /* Handle attributes. */
6729 if (token->keyword == RID_ATTRIBUTE)
6731 /* Parse the attributes. */
6732 decl_spec = cp_parser_attributes_opt (parser);
6733 /* Add them to the list. */
6734 *attributes = chainon (*attributes, decl_spec);
6735 continue;
6737 /* If the next token is an appropriate keyword, we can simply
6738 add it to the list. */
6739 switch (token->keyword)
6741 case RID_FRIEND:
6742 /* decl-specifier:
6743 friend */
6744 friend_p = true;
6745 /* The representation of the specifier is simply the
6746 appropriate TREE_IDENTIFIER node. */
6747 decl_spec = token->value;
6748 /* Consume the token. */
6749 cp_lexer_consume_token (parser->lexer);
6750 break;
6752 /* function-specifier:
6753 inline
6754 virtual
6755 explicit */
6756 case RID_INLINE:
6757 case RID_VIRTUAL:
6758 case RID_EXPLICIT:
6759 decl_spec = cp_parser_function_specifier_opt (parser);
6760 break;
6762 /* decl-specifier:
6763 typedef */
6764 case RID_TYPEDEF:
6765 /* The representation of the specifier is simply the
6766 appropriate TREE_IDENTIFIER node. */
6767 decl_spec = token->value;
6768 /* Consume the token. */
6769 cp_lexer_consume_token (parser->lexer);
6770 /* A constructor declarator cannot appear in a typedef. */
6771 constructor_possible_p = false;
6772 /* The "typedef" keyword can only occur in a declaration; we
6773 may as well commit at this point. */
6774 cp_parser_commit_to_tentative_parse (parser);
6775 break;
6777 /* storage-class-specifier:
6778 auto
6779 register
6780 static
6781 extern
6782 mutable
6784 GNU Extension:
6785 thread */
6786 case RID_AUTO:
6787 case RID_REGISTER:
6788 case RID_STATIC:
6789 case RID_EXTERN:
6790 case RID_MUTABLE:
6791 case RID_THREAD:
6792 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6793 break;
6795 default:
6796 break;
6799 /* Constructors are a special case. The `S' in `S()' is not a
6800 decl-specifier; it is the beginning of the declarator. */
6801 constructor_p = (!decl_spec
6802 && constructor_possible_p
6803 && cp_parser_constructor_declarator_p (parser,
6804 friend_p));
6806 /* If we don't have a DECL_SPEC yet, then we must be looking at
6807 a type-specifier. */
6808 if (!decl_spec && !constructor_p)
6810 bool decl_spec_declares_class_or_enum;
6811 bool is_cv_qualifier;
6813 decl_spec
6814 = cp_parser_type_specifier (parser, flags,
6815 friend_p,
6816 /*is_declaration=*/true,
6817 &decl_spec_declares_class_or_enum,
6818 &is_cv_qualifier);
6820 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6822 /* If this type-specifier referenced a user-defined type
6823 (a typedef, class-name, etc.), then we can't allow any
6824 more such type-specifiers henceforth.
6826 [dcl.spec]
6828 The longest sequence of decl-specifiers that could
6829 possibly be a type name is taken as the
6830 decl-specifier-seq of a declaration. The sequence shall
6831 be self-consistent as described below.
6833 [dcl.type]
6835 As a general rule, at most one type-specifier is allowed
6836 in the complete decl-specifier-seq of a declaration. The
6837 only exceptions are the following:
6839 -- const or volatile can be combined with any other
6840 type-specifier.
6842 -- signed or unsigned can be combined with char, long,
6843 short, or int.
6845 -- ..
6847 Example:
6849 typedef char* Pc;
6850 void g (const int Pc);
6852 Here, Pc is *not* part of the decl-specifier seq; it's
6853 the declarator. Therefore, once we see a type-specifier
6854 (other than a cv-qualifier), we forbid any additional
6855 user-defined types. We *do* still allow things like `int
6856 int' to be considered a decl-specifier-seq, and issue the
6857 error message later. */
6858 if (decl_spec && !is_cv_qualifier)
6859 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6860 /* A constructor declarator cannot follow a type-specifier. */
6861 if (decl_spec)
6862 constructor_possible_p = false;
6865 /* If we still do not have a DECL_SPEC, then there are no more
6866 decl-specifiers. */
6867 if (!decl_spec)
6869 /* Issue an error message, unless the entire construct was
6870 optional. */
6871 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6873 cp_parser_error (parser, "expected decl specifier");
6874 return error_mark_node;
6877 break;
6880 /* Add the DECL_SPEC to the list of specifiers. */
6881 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6883 /* After we see one decl-specifier, further decl-specifiers are
6884 always optional. */
6885 flags |= CP_PARSER_FLAGS_OPTIONAL;
6888 /* We have built up the DECL_SPECS in reverse order. Return them in
6889 the correct order. */
6890 return nreverse (decl_specs);
6893 /* Parse an (optional) storage-class-specifier.
6895 storage-class-specifier:
6896 auto
6897 register
6898 static
6899 extern
6900 mutable
6902 GNU Extension:
6904 storage-class-specifier:
6905 thread
6907 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6909 static tree
6910 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6912 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6914 case RID_AUTO:
6915 case RID_REGISTER:
6916 case RID_STATIC:
6917 case RID_EXTERN:
6918 case RID_MUTABLE:
6919 case RID_THREAD:
6920 /* Consume the token. */
6921 return cp_lexer_consume_token (parser->lexer)->value;
6923 default:
6924 return NULL_TREE;
6928 /* Parse an (optional) function-specifier.
6930 function-specifier:
6931 inline
6932 virtual
6933 explicit
6935 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6937 static tree
6938 cp_parser_function_specifier_opt (cp_parser* parser)
6940 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6942 case RID_INLINE:
6943 case RID_VIRTUAL:
6944 case RID_EXPLICIT:
6945 /* Consume the token. */
6946 return cp_lexer_consume_token (parser->lexer)->value;
6948 default:
6949 return NULL_TREE;
6953 /* Parse a linkage-specification.
6955 linkage-specification:
6956 extern string-literal { declaration-seq [opt] }
6957 extern string-literal declaration */
6959 static void
6960 cp_parser_linkage_specification (cp_parser* parser)
6962 cp_token *token;
6963 tree linkage;
6965 /* Look for the `extern' keyword. */
6966 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6968 /* Peek at the next token. */
6969 token = cp_lexer_peek_token (parser->lexer);
6970 /* If it's not a string-literal, then there's a problem. */
6971 if (!cp_parser_is_string_literal (token))
6973 cp_parser_error (parser, "expected language-name");
6974 return;
6976 /* Consume the token. */
6977 cp_lexer_consume_token (parser->lexer);
6979 /* Transform the literal into an identifier. If the literal is a
6980 wide-character string, or contains embedded NULs, then we can't
6981 handle it as the user wants. */
6982 if (token->type == CPP_WSTRING
6983 || (strlen (TREE_STRING_POINTER (token->value))
6984 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6986 cp_parser_error (parser, "invalid linkage-specification");
6987 /* Assume C++ linkage. */
6988 linkage = get_identifier ("c++");
6990 /* If it's a simple string constant, things are easier. */
6991 else
6992 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6994 /* We're now using the new linkage. */
6995 push_lang_context (linkage);
6997 /* If the next token is a `{', then we're using the first
6998 production. */
6999 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7001 /* Consume the `{' token. */
7002 cp_lexer_consume_token (parser->lexer);
7003 /* Parse the declarations. */
7004 cp_parser_declaration_seq_opt (parser);
7005 /* Look for the closing `}'. */
7006 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7008 /* Otherwise, there's just one declaration. */
7009 else
7011 bool saved_in_unbraced_linkage_specification_p;
7013 saved_in_unbraced_linkage_specification_p
7014 = parser->in_unbraced_linkage_specification_p;
7015 parser->in_unbraced_linkage_specification_p = true;
7016 have_extern_spec = true;
7017 cp_parser_declaration (parser);
7018 have_extern_spec = false;
7019 parser->in_unbraced_linkage_specification_p
7020 = saved_in_unbraced_linkage_specification_p;
7023 /* We're done with the linkage-specification. */
7024 pop_lang_context ();
7027 /* Special member functions [gram.special] */
7029 /* Parse a conversion-function-id.
7031 conversion-function-id:
7032 operator conversion-type-id
7034 Returns an IDENTIFIER_NODE representing the operator. */
7036 static tree
7037 cp_parser_conversion_function_id (cp_parser* parser)
7039 tree type;
7040 tree saved_scope;
7041 tree saved_qualifying_scope;
7042 tree saved_object_scope;
7044 /* Look for the `operator' token. */
7045 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7046 return error_mark_node;
7047 /* When we parse the conversion-type-id, the current scope will be
7048 reset. However, we need that information in able to look up the
7049 conversion function later, so we save it here. */
7050 saved_scope = parser->scope;
7051 saved_qualifying_scope = parser->qualifying_scope;
7052 saved_object_scope = parser->object_scope;
7053 /* We must enter the scope of the class so that the names of
7054 entities declared within the class are available in the
7055 conversion-type-id. For example, consider:
7057 struct S {
7058 typedef int I;
7059 operator I();
7062 S::operator I() { ... }
7064 In order to see that `I' is a type-name in the definition, we
7065 must be in the scope of `S'. */
7066 if (saved_scope)
7067 push_scope (saved_scope);
7068 /* Parse the conversion-type-id. */
7069 type = cp_parser_conversion_type_id (parser);
7070 /* Leave the scope of the class, if any. */
7071 if (saved_scope)
7072 pop_scope (saved_scope);
7073 /* Restore the saved scope. */
7074 parser->scope = saved_scope;
7075 parser->qualifying_scope = saved_qualifying_scope;
7076 parser->object_scope = saved_object_scope;
7077 /* If the TYPE is invalid, indicate failure. */
7078 if (type == error_mark_node)
7079 return error_mark_node;
7080 return mangle_conv_op_name_for_type (type);
7083 /* Parse a conversion-type-id:
7085 conversion-type-id:
7086 type-specifier-seq conversion-declarator [opt]
7088 Returns the TYPE specified. */
7090 static tree
7091 cp_parser_conversion_type_id (cp_parser* parser)
7093 tree attributes;
7094 tree type_specifiers;
7095 tree declarator;
7097 /* Parse the attributes. */
7098 attributes = cp_parser_attributes_opt (parser);
7099 /* Parse the type-specifiers. */
7100 type_specifiers = cp_parser_type_specifier_seq (parser);
7101 /* If that didn't work, stop. */
7102 if (type_specifiers == error_mark_node)
7103 return error_mark_node;
7104 /* Parse the conversion-declarator. */
7105 declarator = cp_parser_conversion_declarator_opt (parser);
7107 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7108 /*initialized=*/0, &attributes);
7111 /* Parse an (optional) conversion-declarator.
7113 conversion-declarator:
7114 ptr-operator conversion-declarator [opt]
7116 Returns a representation of the declarator. See
7117 cp_parser_declarator for details. */
7119 static tree
7120 cp_parser_conversion_declarator_opt (cp_parser* parser)
7122 enum tree_code code;
7123 tree class_type;
7124 tree cv_qualifier_seq;
7126 /* We don't know if there's a ptr-operator next, or not. */
7127 cp_parser_parse_tentatively (parser);
7128 /* Try the ptr-operator. */
7129 code = cp_parser_ptr_operator (parser, &class_type,
7130 &cv_qualifier_seq);
7131 /* If it worked, look for more conversion-declarators. */
7132 if (cp_parser_parse_definitely (parser))
7134 tree declarator;
7136 /* Parse another optional declarator. */
7137 declarator = cp_parser_conversion_declarator_opt (parser);
7139 /* Create the representation of the declarator. */
7140 if (code == INDIRECT_REF)
7141 declarator = make_pointer_declarator (cv_qualifier_seq,
7142 declarator);
7143 else
7144 declarator = make_reference_declarator (cv_qualifier_seq,
7145 declarator);
7147 /* Handle the pointer-to-member case. */
7148 if (class_type)
7149 declarator = build_nt (SCOPE_REF, class_type, declarator);
7151 return declarator;
7154 return NULL_TREE;
7157 /* Parse an (optional) ctor-initializer.
7159 ctor-initializer:
7160 : mem-initializer-list
7162 Returns TRUE iff the ctor-initializer was actually present. */
7164 static bool
7165 cp_parser_ctor_initializer_opt (cp_parser* parser)
7167 /* If the next token is not a `:', then there is no
7168 ctor-initializer. */
7169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7171 /* Do default initialization of any bases and members. */
7172 if (DECL_CONSTRUCTOR_P (current_function_decl))
7173 finish_mem_initializers (NULL_TREE);
7175 return false;
7178 /* Consume the `:' token. */
7179 cp_lexer_consume_token (parser->lexer);
7180 /* And the mem-initializer-list. */
7181 cp_parser_mem_initializer_list (parser);
7183 return true;
7186 /* Parse a mem-initializer-list.
7188 mem-initializer-list:
7189 mem-initializer
7190 mem-initializer , mem-initializer-list */
7192 static void
7193 cp_parser_mem_initializer_list (cp_parser* parser)
7195 tree mem_initializer_list = NULL_TREE;
7197 /* Let the semantic analysis code know that we are starting the
7198 mem-initializer-list. */
7199 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7200 error ("only constructors take base initializers");
7202 /* Loop through the list. */
7203 while (true)
7205 tree mem_initializer;
7207 /* Parse the mem-initializer. */
7208 mem_initializer = cp_parser_mem_initializer (parser);
7209 /* Add it to the list, unless it was erroneous. */
7210 if (mem_initializer)
7212 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7213 mem_initializer_list = mem_initializer;
7215 /* If the next token is not a `,', we're done. */
7216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7217 break;
7218 /* Consume the `,' token. */
7219 cp_lexer_consume_token (parser->lexer);
7222 /* Perform semantic analysis. */
7223 if (DECL_CONSTRUCTOR_P (current_function_decl))
7224 finish_mem_initializers (mem_initializer_list);
7227 /* Parse a mem-initializer.
7229 mem-initializer:
7230 mem-initializer-id ( expression-list [opt] )
7232 GNU extension:
7234 mem-initializer:
7235 ( expresion-list [opt] )
7237 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7238 class) or FIELD_DECL (for a non-static data member) to initialize;
7239 the TREE_VALUE is the expression-list. */
7241 static tree
7242 cp_parser_mem_initializer (cp_parser* parser)
7244 tree mem_initializer_id;
7245 tree expression_list;
7246 tree member;
7248 /* Find out what is being initialized. */
7249 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7251 pedwarn ("anachronistic old-style base class initializer");
7252 mem_initializer_id = NULL_TREE;
7254 else
7255 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7256 member = expand_member_init (mem_initializer_id);
7257 if (member && !DECL_P (member))
7258 in_base_initializer = 1;
7260 /* Look for the opening `('. */
7261 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7262 /* Parse the expression-list. */
7263 if (cp_lexer_next_token_is_not (parser->lexer,
7264 CPP_CLOSE_PAREN))
7265 expression_list = cp_parser_expression_list (parser);
7266 else
7267 expression_list = void_type_node;
7268 /* Look for the closing `)'. */
7269 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7271 in_base_initializer = 0;
7273 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7276 /* Parse a mem-initializer-id.
7278 mem-initializer-id:
7279 :: [opt] nested-name-specifier [opt] class-name
7280 identifier
7282 Returns a TYPE indicating the class to be initializer for the first
7283 production. Returns an IDENTIFIER_NODE indicating the data member
7284 to be initialized for the second production. */
7286 static tree
7287 cp_parser_mem_initializer_id (cp_parser* parser)
7289 bool global_scope_p;
7290 bool nested_name_specifier_p;
7291 tree id;
7293 /* Look for the optional `::' operator. */
7294 global_scope_p
7295 = (cp_parser_global_scope_opt (parser,
7296 /*current_scope_valid_p=*/false)
7297 != NULL_TREE);
7298 /* Look for the optional nested-name-specifier. The simplest way to
7299 implement:
7301 [temp.res]
7303 The keyword `typename' is not permitted in a base-specifier or
7304 mem-initializer; in these contexts a qualified name that
7305 depends on a template-parameter is implicitly assumed to be a
7306 type name.
7308 is to assume that we have seen the `typename' keyword at this
7309 point. */
7310 nested_name_specifier_p
7311 = (cp_parser_nested_name_specifier_opt (parser,
7312 /*typename_keyword_p=*/true,
7313 /*check_dependency_p=*/true,
7314 /*type_p=*/true)
7315 != NULL_TREE);
7316 /* If there is a `::' operator or a nested-name-specifier, then we
7317 are definitely looking for a class-name. */
7318 if (global_scope_p || nested_name_specifier_p)
7319 return cp_parser_class_name (parser,
7320 /*typename_keyword_p=*/true,
7321 /*template_keyword_p=*/false,
7322 /*type_p=*/false,
7323 /*check_dependency_p=*/true,
7324 /*class_head_p=*/false);
7325 /* Otherwise, we could also be looking for an ordinary identifier. */
7326 cp_parser_parse_tentatively (parser);
7327 /* Try a class-name. */
7328 id = cp_parser_class_name (parser,
7329 /*typename_keyword_p=*/true,
7330 /*template_keyword_p=*/false,
7331 /*type_p=*/false,
7332 /*check_dependency_p=*/true,
7333 /*class_head_p=*/false);
7334 /* If we found one, we're done. */
7335 if (cp_parser_parse_definitely (parser))
7336 return id;
7337 /* Otherwise, look for an ordinary identifier. */
7338 return cp_parser_identifier (parser);
7341 /* Overloading [gram.over] */
7343 /* Parse an operator-function-id.
7345 operator-function-id:
7346 operator operator
7348 Returns an IDENTIFIER_NODE for the operator which is a
7349 human-readable spelling of the identifier, e.g., `operator +'. */
7351 static tree
7352 cp_parser_operator_function_id (cp_parser* parser)
7354 /* Look for the `operator' keyword. */
7355 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7356 return error_mark_node;
7357 /* And then the name of the operator itself. */
7358 return cp_parser_operator (parser);
7361 /* Parse an operator.
7363 operator:
7364 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7365 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7366 || ++ -- , ->* -> () []
7368 GNU Extensions:
7370 operator:
7371 <? >? <?= >?=
7373 Returns an IDENTIFIER_NODE for the operator which is a
7374 human-readable spelling of the identifier, e.g., `operator +'. */
7376 static tree
7377 cp_parser_operator (cp_parser* parser)
7379 tree id = NULL_TREE;
7380 cp_token *token;
7382 /* Peek at the next token. */
7383 token = cp_lexer_peek_token (parser->lexer);
7384 /* Figure out which operator we have. */
7385 switch (token->type)
7387 case CPP_KEYWORD:
7389 enum tree_code op;
7391 /* The keyword should be either `new' or `delete'. */
7392 if (token->keyword == RID_NEW)
7393 op = NEW_EXPR;
7394 else if (token->keyword == RID_DELETE)
7395 op = DELETE_EXPR;
7396 else
7397 break;
7399 /* Consume the `new' or `delete' token. */
7400 cp_lexer_consume_token (parser->lexer);
7402 /* Peek at the next token. */
7403 token = cp_lexer_peek_token (parser->lexer);
7404 /* If it's a `[' token then this is the array variant of the
7405 operator. */
7406 if (token->type == CPP_OPEN_SQUARE)
7408 /* Consume the `[' token. */
7409 cp_lexer_consume_token (parser->lexer);
7410 /* Look for the `]' token. */
7411 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7412 id = ansi_opname (op == NEW_EXPR
7413 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7415 /* Otherwise, we have the non-array variant. */
7416 else
7417 id = ansi_opname (op);
7419 return id;
7422 case CPP_PLUS:
7423 id = ansi_opname (PLUS_EXPR);
7424 break;
7426 case CPP_MINUS:
7427 id = ansi_opname (MINUS_EXPR);
7428 break;
7430 case CPP_MULT:
7431 id = ansi_opname (MULT_EXPR);
7432 break;
7434 case CPP_DIV:
7435 id = ansi_opname (TRUNC_DIV_EXPR);
7436 break;
7438 case CPP_MOD:
7439 id = ansi_opname (TRUNC_MOD_EXPR);
7440 break;
7442 case CPP_XOR:
7443 id = ansi_opname (BIT_XOR_EXPR);
7444 break;
7446 case CPP_AND:
7447 id = ansi_opname (BIT_AND_EXPR);
7448 break;
7450 case CPP_OR:
7451 id = ansi_opname (BIT_IOR_EXPR);
7452 break;
7454 case CPP_COMPL:
7455 id = ansi_opname (BIT_NOT_EXPR);
7456 break;
7458 case CPP_NOT:
7459 id = ansi_opname (TRUTH_NOT_EXPR);
7460 break;
7462 case CPP_EQ:
7463 id = ansi_assopname (NOP_EXPR);
7464 break;
7466 case CPP_LESS:
7467 id = ansi_opname (LT_EXPR);
7468 break;
7470 case CPP_GREATER:
7471 id = ansi_opname (GT_EXPR);
7472 break;
7474 case CPP_PLUS_EQ:
7475 id = ansi_assopname (PLUS_EXPR);
7476 break;
7478 case CPP_MINUS_EQ:
7479 id = ansi_assopname (MINUS_EXPR);
7480 break;
7482 case CPP_MULT_EQ:
7483 id = ansi_assopname (MULT_EXPR);
7484 break;
7486 case CPP_DIV_EQ:
7487 id = ansi_assopname (TRUNC_DIV_EXPR);
7488 break;
7490 case CPP_MOD_EQ:
7491 id = ansi_assopname (TRUNC_MOD_EXPR);
7492 break;
7494 case CPP_XOR_EQ:
7495 id = ansi_assopname (BIT_XOR_EXPR);
7496 break;
7498 case CPP_AND_EQ:
7499 id = ansi_assopname (BIT_AND_EXPR);
7500 break;
7502 case CPP_OR_EQ:
7503 id = ansi_assopname (BIT_IOR_EXPR);
7504 break;
7506 case CPP_LSHIFT:
7507 id = ansi_opname (LSHIFT_EXPR);
7508 break;
7510 case CPP_RSHIFT:
7511 id = ansi_opname (RSHIFT_EXPR);
7512 break;
7514 case CPP_LSHIFT_EQ:
7515 id = ansi_assopname (LSHIFT_EXPR);
7516 break;
7518 case CPP_RSHIFT_EQ:
7519 id = ansi_assopname (RSHIFT_EXPR);
7520 break;
7522 case CPP_EQ_EQ:
7523 id = ansi_opname (EQ_EXPR);
7524 break;
7526 case CPP_NOT_EQ:
7527 id = ansi_opname (NE_EXPR);
7528 break;
7530 case CPP_LESS_EQ:
7531 id = ansi_opname (LE_EXPR);
7532 break;
7534 case CPP_GREATER_EQ:
7535 id = ansi_opname (GE_EXPR);
7536 break;
7538 case CPP_AND_AND:
7539 id = ansi_opname (TRUTH_ANDIF_EXPR);
7540 break;
7542 case CPP_OR_OR:
7543 id = ansi_opname (TRUTH_ORIF_EXPR);
7544 break;
7546 case CPP_PLUS_PLUS:
7547 id = ansi_opname (POSTINCREMENT_EXPR);
7548 break;
7550 case CPP_MINUS_MINUS:
7551 id = ansi_opname (PREDECREMENT_EXPR);
7552 break;
7554 case CPP_COMMA:
7555 id = ansi_opname (COMPOUND_EXPR);
7556 break;
7558 case CPP_DEREF_STAR:
7559 id = ansi_opname (MEMBER_REF);
7560 break;
7562 case CPP_DEREF:
7563 id = ansi_opname (COMPONENT_REF);
7564 break;
7566 case CPP_OPEN_PAREN:
7567 /* Consume the `('. */
7568 cp_lexer_consume_token (parser->lexer);
7569 /* Look for the matching `)'. */
7570 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7571 return ansi_opname (CALL_EXPR);
7573 case CPP_OPEN_SQUARE:
7574 /* Consume the `['. */
7575 cp_lexer_consume_token (parser->lexer);
7576 /* Look for the matching `]'. */
7577 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7578 return ansi_opname (ARRAY_REF);
7580 /* Extensions. */
7581 case CPP_MIN:
7582 id = ansi_opname (MIN_EXPR);
7583 break;
7585 case CPP_MAX:
7586 id = ansi_opname (MAX_EXPR);
7587 break;
7589 case CPP_MIN_EQ:
7590 id = ansi_assopname (MIN_EXPR);
7591 break;
7593 case CPP_MAX_EQ:
7594 id = ansi_assopname (MAX_EXPR);
7595 break;
7597 default:
7598 /* Anything else is an error. */
7599 break;
7602 /* If we have selected an identifier, we need to consume the
7603 operator token. */
7604 if (id)
7605 cp_lexer_consume_token (parser->lexer);
7606 /* Otherwise, no valid operator name was present. */
7607 else
7609 cp_parser_error (parser, "expected operator");
7610 id = error_mark_node;
7613 return id;
7616 /* Parse a template-declaration.
7618 template-declaration:
7619 export [opt] template < template-parameter-list > declaration
7621 If MEMBER_P is TRUE, this template-declaration occurs within a
7622 class-specifier.
7624 The grammar rule given by the standard isn't correct. What
7625 is really meant is:
7627 template-declaration:
7628 export [opt] template-parameter-list-seq
7629 decl-specifier-seq [opt] init-declarator [opt] ;
7630 export [opt] template-parameter-list-seq
7631 function-definition
7633 template-parameter-list-seq:
7634 template-parameter-list-seq [opt]
7635 template < template-parameter-list > */
7637 static void
7638 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7640 /* Check for `export'. */
7641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7643 /* Consume the `export' token. */
7644 cp_lexer_consume_token (parser->lexer);
7645 /* Warn that we do not support `export'. */
7646 warning ("keyword `export' not implemented, and will be ignored");
7649 cp_parser_template_declaration_after_export (parser, member_p);
7652 /* Parse a template-parameter-list.
7654 template-parameter-list:
7655 template-parameter
7656 template-parameter-list , template-parameter
7658 Returns a TREE_LIST. Each node represents a template parameter.
7659 The nodes are connected via their TREE_CHAINs. */
7661 static tree
7662 cp_parser_template_parameter_list (cp_parser* parser)
7664 tree parameter_list = NULL_TREE;
7666 while (true)
7668 tree parameter;
7669 cp_token *token;
7671 /* Parse the template-parameter. */
7672 parameter = cp_parser_template_parameter (parser);
7673 /* Add it to the list. */
7674 parameter_list = process_template_parm (parameter_list,
7675 parameter);
7677 /* Peek at the next token. */
7678 token = cp_lexer_peek_token (parser->lexer);
7679 /* If it's not a `,', we're done. */
7680 if (token->type != CPP_COMMA)
7681 break;
7682 /* Otherwise, consume the `,' token. */
7683 cp_lexer_consume_token (parser->lexer);
7686 return parameter_list;
7689 /* Parse a template-parameter.
7691 template-parameter:
7692 type-parameter
7693 parameter-declaration
7695 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7696 TREE_PURPOSE is the default value, if any. */
7698 static tree
7699 cp_parser_template_parameter (cp_parser* parser)
7701 cp_token *token;
7703 /* Peek at the next token. */
7704 token = cp_lexer_peek_token (parser->lexer);
7705 /* If it is `class' or `template', we have a type-parameter. */
7706 if (token->keyword == RID_TEMPLATE)
7707 return cp_parser_type_parameter (parser);
7708 /* If it is `class' or `typename' we do not know yet whether it is a
7709 type parameter or a non-type parameter. Consider:
7711 template <typename T, typename T::X X> ...
7715 template <class C, class D*> ...
7717 Here, the first parameter is a type parameter, and the second is
7718 a non-type parameter. We can tell by looking at the token after
7719 the identifier -- if it is a `,', `=', or `>' then we have a type
7720 parameter. */
7721 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7723 /* Peek at the token after `class' or `typename'. */
7724 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7725 /* If it's an identifier, skip it. */
7726 if (token->type == CPP_NAME)
7727 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7728 /* Now, see if the token looks like the end of a template
7729 parameter. */
7730 if (token->type == CPP_COMMA
7731 || token->type == CPP_EQ
7732 || token->type == CPP_GREATER)
7733 return cp_parser_type_parameter (parser);
7736 /* Otherwise, it is a non-type parameter.
7738 [temp.param]
7740 When parsing a default template-argument for a non-type
7741 template-parameter, the first non-nested `>' is taken as the end
7742 of the template parameter-list rather than a greater-than
7743 operator. */
7744 return
7745 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7748 /* Parse a type-parameter.
7750 type-parameter:
7751 class identifier [opt]
7752 class identifier [opt] = type-id
7753 typename identifier [opt]
7754 typename identifier [opt] = type-id
7755 template < template-parameter-list > class identifier [opt]
7756 template < template-parameter-list > class identifier [opt]
7757 = id-expression
7759 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7760 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7761 the declaration of the parameter. */
7763 static tree
7764 cp_parser_type_parameter (cp_parser* parser)
7766 cp_token *token;
7767 tree parameter;
7769 /* Look for a keyword to tell us what kind of parameter this is. */
7770 token = cp_parser_require (parser, CPP_KEYWORD,
7771 "`class', `typename', or `template'");
7772 if (!token)
7773 return error_mark_node;
7775 switch (token->keyword)
7777 case RID_CLASS:
7778 case RID_TYPENAME:
7780 tree identifier;
7781 tree default_argument;
7783 /* If the next token is an identifier, then it names the
7784 parameter. */
7785 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7786 identifier = cp_parser_identifier (parser);
7787 else
7788 identifier = NULL_TREE;
7790 /* Create the parameter. */
7791 parameter = finish_template_type_parm (class_type_node, identifier);
7793 /* If the next token is an `=', we have a default argument. */
7794 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7796 /* Consume the `=' token. */
7797 cp_lexer_consume_token (parser->lexer);
7798 /* Parse the default-argumen. */
7799 default_argument = cp_parser_type_id (parser);
7801 else
7802 default_argument = NULL_TREE;
7804 /* Create the combined representation of the parameter and the
7805 default argument. */
7806 parameter = build_tree_list (default_argument,
7807 parameter);
7809 break;
7811 case RID_TEMPLATE:
7813 tree parameter_list;
7814 tree identifier;
7815 tree default_argument;
7817 /* Look for the `<'. */
7818 cp_parser_require (parser, CPP_LESS, "`<'");
7819 /* Parse the template-parameter-list. */
7820 begin_template_parm_list ();
7821 parameter_list
7822 = cp_parser_template_parameter_list (parser);
7823 parameter_list = end_template_parm_list (parameter_list);
7824 /* Look for the `>'. */
7825 cp_parser_require (parser, CPP_GREATER, "`>'");
7826 /* Look for the `class' keyword. */
7827 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7828 /* If the next token is an `=', then there is a
7829 default-argument. If the next token is a `>', we are at
7830 the end of the parameter-list. If the next token is a `,',
7831 then we are at the end of this parameter. */
7832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7833 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7834 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7835 identifier = cp_parser_identifier (parser);
7836 else
7837 identifier = NULL_TREE;
7838 /* Create the template parameter. */
7839 parameter = finish_template_template_parm (class_type_node,
7840 identifier);
7842 /* If the next token is an `=', then there is a
7843 default-argument. */
7844 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7846 /* Consume the `='. */
7847 cp_lexer_consume_token (parser->lexer);
7848 /* Parse the id-expression. */
7849 default_argument
7850 = cp_parser_id_expression (parser,
7851 /*template_keyword_p=*/false,
7852 /*check_dependency_p=*/true,
7853 /*template_p=*/NULL);
7854 /* Look up the name. */
7855 default_argument
7856 = cp_parser_lookup_name_simple (parser, default_argument);
7857 /* See if the default argument is valid. */
7858 default_argument
7859 = check_template_template_default_arg (default_argument);
7861 else
7862 default_argument = NULL_TREE;
7864 /* Create the combined representation of the parameter and the
7865 default argument. */
7866 parameter = build_tree_list (default_argument,
7867 parameter);
7869 break;
7871 default:
7872 /* Anything else is an error. */
7873 cp_parser_error (parser,
7874 "expected `class', `typename', or `template'");
7875 parameter = error_mark_node;
7878 return parameter;
7881 /* Parse a template-id.
7883 template-id:
7884 template-name < template-argument-list [opt] >
7886 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7887 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7888 returned. Otherwise, if the template-name names a function, or set
7889 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7890 names a class, returns a TYPE_DECL for the specialization.
7892 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7893 uninstantiated templates. */
7895 static tree
7896 cp_parser_template_id (cp_parser *parser,
7897 bool template_keyword_p,
7898 bool check_dependency_p)
7900 tree template;
7901 tree arguments;
7902 tree saved_scope;
7903 tree saved_qualifying_scope;
7904 tree saved_object_scope;
7905 tree template_id;
7906 bool saved_greater_than_is_operator_p;
7907 ptrdiff_t start_of_id;
7908 tree access_check = NULL_TREE;
7909 cp_token *next_token;
7911 /* If the next token corresponds to a template-id, there is no need
7912 to reparse it. */
7913 next_token = cp_lexer_peek_token (parser->lexer);
7914 if (next_token->type == CPP_TEMPLATE_ID)
7916 tree value;
7917 tree check;
7919 /* Get the stored value. */
7920 value = cp_lexer_consume_token (parser->lexer)->value;
7921 /* Perform any access checks that were deferred. */
7922 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7923 perform_or_defer_access_check (TREE_PURPOSE (check),
7924 TREE_VALUE (check));
7925 /* Return the stored value. */
7926 return TREE_VALUE (value);
7929 /* Avoid performing name lookup if there is no possibility of
7930 finding a template-id. */
7931 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7932 || (next_token->type == CPP_NAME
7933 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7935 cp_parser_error (parser, "expected template-id");
7936 return error_mark_node;
7939 /* Remember where the template-id starts. */
7940 if (cp_parser_parsing_tentatively (parser)
7941 && !cp_parser_committed_to_tentative_parse (parser))
7943 next_token = cp_lexer_peek_token (parser->lexer);
7944 start_of_id = cp_lexer_token_difference (parser->lexer,
7945 parser->lexer->first_token,
7946 next_token);
7948 else
7949 start_of_id = -1;
7951 push_deferring_access_checks (dk_deferred);
7953 /* Parse the template-name. */
7954 template = cp_parser_template_name (parser, template_keyword_p,
7955 check_dependency_p);
7956 if (template == error_mark_node)
7958 pop_deferring_access_checks ();
7959 return error_mark_node;
7962 /* Look for the `<' that starts the template-argument-list. */
7963 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7965 pop_deferring_access_checks ();
7966 return error_mark_node;
7969 /* [temp.names]
7971 When parsing a template-id, the first non-nested `>' is taken as
7972 the end of the template-argument-list rather than a greater-than
7973 operator. */
7974 saved_greater_than_is_operator_p
7975 = parser->greater_than_is_operator_p;
7976 parser->greater_than_is_operator_p = false;
7977 /* Parsing the argument list may modify SCOPE, so we save it
7978 here. */
7979 saved_scope = parser->scope;
7980 saved_qualifying_scope = parser->qualifying_scope;
7981 saved_object_scope = parser->object_scope;
7982 /* Parse the template-argument-list itself. */
7983 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7984 arguments = NULL_TREE;
7985 else
7986 arguments = cp_parser_template_argument_list (parser);
7987 /* Look for the `>' that ends the template-argument-list. */
7988 cp_parser_require (parser, CPP_GREATER, "`>'");
7989 /* The `>' token might be a greater-than operator again now. */
7990 parser->greater_than_is_operator_p
7991 = saved_greater_than_is_operator_p;
7992 /* Restore the SAVED_SCOPE. */
7993 parser->scope = saved_scope;
7994 parser->qualifying_scope = saved_qualifying_scope;
7995 parser->object_scope = saved_object_scope;
7997 /* Build a representation of the specialization. */
7998 if (TREE_CODE (template) == IDENTIFIER_NODE)
7999 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8000 else if (DECL_CLASS_TEMPLATE_P (template)
8001 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8002 template_id
8003 = finish_template_type (template, arguments,
8004 cp_lexer_next_token_is (parser->lexer,
8005 CPP_SCOPE));
8006 else
8008 /* If it's not a class-template or a template-template, it should be
8009 a function-template. */
8010 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8011 || TREE_CODE (template) == OVERLOAD
8012 || BASELINK_P (template)),
8013 20010716);
8015 template_id = lookup_template_function (template, arguments);
8018 /* Retrieve any deferred checks. Do not pop this access checks yet
8019 so the memory will not be reclaimed during token replacing below. */
8020 access_check = get_deferred_access_checks ();
8022 /* If parsing tentatively, replace the sequence of tokens that makes
8023 up the template-id with a CPP_TEMPLATE_ID token. That way,
8024 should we re-parse the token stream, we will not have to repeat
8025 the effort required to do the parse, nor will we issue duplicate
8026 error messages about problems during instantiation of the
8027 template. */
8028 if (start_of_id >= 0)
8030 cp_token *token;
8032 /* Find the token that corresponds to the start of the
8033 template-id. */
8034 token = cp_lexer_advance_token (parser->lexer,
8035 parser->lexer->first_token,
8036 start_of_id);
8038 /* Reset the contents of the START_OF_ID token. */
8039 token->type = CPP_TEMPLATE_ID;
8040 token->value = build_tree_list (access_check, template_id);
8041 token->keyword = RID_MAX;
8042 /* Purge all subsequent tokens. */
8043 cp_lexer_purge_tokens_after (parser->lexer, token);
8046 pop_deferring_access_checks ();
8047 return template_id;
8050 /* Parse a template-name.
8052 template-name:
8053 identifier
8055 The standard should actually say:
8057 template-name:
8058 identifier
8059 operator-function-id
8060 conversion-function-id
8062 A defect report has been filed about this issue.
8064 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8065 `template' keyword, in a construction like:
8067 T::template f<3>()
8069 In that case `f' is taken to be a template-name, even though there
8070 is no way of knowing for sure.
8072 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8073 name refers to a set of overloaded functions, at least one of which
8074 is a template, or an IDENTIFIER_NODE with the name of the template,
8075 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8076 names are looked up inside uninstantiated templates. */
8078 static tree
8079 cp_parser_template_name (cp_parser* parser,
8080 bool template_keyword_p,
8081 bool check_dependency_p)
8083 tree identifier;
8084 tree decl;
8085 tree fns;
8087 /* If the next token is `operator', then we have either an
8088 operator-function-id or a conversion-function-id. */
8089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8091 /* We don't know whether we're looking at an
8092 operator-function-id or a conversion-function-id. */
8093 cp_parser_parse_tentatively (parser);
8094 /* Try an operator-function-id. */
8095 identifier = cp_parser_operator_function_id (parser);
8096 /* If that didn't work, try a conversion-function-id. */
8097 if (!cp_parser_parse_definitely (parser))
8098 identifier = cp_parser_conversion_function_id (parser);
8100 /* Look for the identifier. */
8101 else
8102 identifier = cp_parser_identifier (parser);
8104 /* If we didn't find an identifier, we don't have a template-id. */
8105 if (identifier == error_mark_node)
8106 return error_mark_node;
8108 /* If the name immediately followed the `template' keyword, then it
8109 is a template-name. However, if the next token is not `<', then
8110 we do not treat it as a template-name, since it is not being used
8111 as part of a template-id. This enables us to handle constructs
8112 like:
8114 template <typename T> struct S { S(); };
8115 template <typename T> S<T>::S();
8117 correctly. We would treat `S' as a template -- if it were `S<T>'
8118 -- but we do not if there is no `<'. */
8119 if (template_keyword_p && processing_template_decl
8120 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8121 return identifier;
8123 /* Look up the name. */
8124 decl = cp_parser_lookup_name (parser, identifier,
8125 /*is_type=*/false,
8126 /*is_namespace=*/false,
8127 check_dependency_p);
8128 decl = maybe_get_template_decl_from_type_decl (decl);
8130 /* If DECL is a template, then the name was a template-name. */
8131 if (TREE_CODE (decl) == TEMPLATE_DECL)
8133 else
8135 /* The standard does not explicitly indicate whether a name that
8136 names a set of overloaded declarations, some of which are
8137 templates, is a template-name. However, such a name should
8138 be a template-name; otherwise, there is no way to form a
8139 template-id for the overloaded templates. */
8140 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8141 if (TREE_CODE (fns) == OVERLOAD)
8143 tree fn;
8145 for (fn = fns; fn; fn = OVL_NEXT (fn))
8146 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8147 break;
8149 else
8151 /* Otherwise, the name does not name a template. */
8152 cp_parser_error (parser, "expected template-name");
8153 return error_mark_node;
8157 /* If DECL is dependent, and refers to a function, then just return
8158 its name; we will look it up again during template instantiation. */
8159 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8161 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8162 if (TYPE_P (scope) && dependent_type_p (scope))
8163 return identifier;
8166 return decl;
8169 /* Parse a template-argument-list.
8171 template-argument-list:
8172 template-argument
8173 template-argument-list , template-argument
8175 Returns a TREE_LIST representing the arguments, in the order they
8176 appeared. The TREE_VALUE of each node is a representation of the
8177 argument. */
8179 static tree
8180 cp_parser_template_argument_list (cp_parser* parser)
8182 tree arguments = NULL_TREE;
8184 while (true)
8186 tree argument;
8188 /* Parse the template-argument. */
8189 argument = cp_parser_template_argument (parser);
8190 /* Add it to the list. */
8191 arguments = tree_cons (NULL_TREE, argument, arguments);
8192 /* If it is not a `,', then there are no more arguments. */
8193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8194 break;
8195 /* Otherwise, consume the ','. */
8196 cp_lexer_consume_token (parser->lexer);
8199 /* We built up the arguments in reverse order. */
8200 return nreverse (arguments);
8203 /* Parse a template-argument.
8205 template-argument:
8206 assignment-expression
8207 type-id
8208 id-expression
8210 The representation is that of an assignment-expression, type-id, or
8211 id-expression -- except that the qualified id-expression is
8212 evaluated, so that the value returned is either a DECL or an
8213 OVERLOAD. */
8215 static tree
8216 cp_parser_template_argument (cp_parser* parser)
8218 tree argument;
8219 bool template_p;
8221 /* There's really no way to know what we're looking at, so we just
8222 try each alternative in order.
8224 [temp.arg]
8226 In a template-argument, an ambiguity between a type-id and an
8227 expression is resolved to a type-id, regardless of the form of
8228 the corresponding template-parameter.
8230 Therefore, we try a type-id first. */
8231 cp_parser_parse_tentatively (parser);
8232 argument = cp_parser_type_id (parser);
8233 /* If the next token isn't a `,' or a `>', then this argument wasn't
8234 really finished. */
8235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8236 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8237 cp_parser_error (parser, "expected template-argument");
8238 /* If that worked, we're done. */
8239 if (cp_parser_parse_definitely (parser))
8240 return argument;
8241 /* We're still not sure what the argument will be. */
8242 cp_parser_parse_tentatively (parser);
8243 /* Try a template. */
8244 argument = cp_parser_id_expression (parser,
8245 /*template_keyword_p=*/false,
8246 /*check_dependency_p=*/true,
8247 &template_p);
8248 /* If the next token isn't a `,' or a `>', then this argument wasn't
8249 really finished. */
8250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8251 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8252 cp_parser_error (parser, "expected template-argument");
8253 if (!cp_parser_error_occurred (parser))
8255 /* Figure out what is being referred to. */
8256 argument = cp_parser_lookup_name_simple (parser, argument);
8257 if (template_p)
8258 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8259 TREE_OPERAND (argument, 1),
8260 tf_error);
8261 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8262 cp_parser_error (parser, "expected template-name");
8264 if (cp_parser_parse_definitely (parser))
8265 return argument;
8266 /* It must be an assignment-expression. */
8267 return cp_parser_assignment_expression (parser);
8270 /* Parse an explicit-instantiation.
8272 explicit-instantiation:
8273 template declaration
8275 Although the standard says `declaration', what it really means is:
8277 explicit-instantiation:
8278 template decl-specifier-seq [opt] declarator [opt] ;
8280 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8281 supposed to be allowed. A defect report has been filed about this
8282 issue.
8284 GNU Extension:
8286 explicit-instantiation:
8287 storage-class-specifier template
8288 decl-specifier-seq [opt] declarator [opt] ;
8289 function-specifier template
8290 decl-specifier-seq [opt] declarator [opt] ; */
8292 static void
8293 cp_parser_explicit_instantiation (cp_parser* parser)
8295 bool declares_class_or_enum;
8296 tree decl_specifiers;
8297 tree attributes;
8298 tree extension_specifier = NULL_TREE;
8300 /* Look for an (optional) storage-class-specifier or
8301 function-specifier. */
8302 if (cp_parser_allow_gnu_extensions_p (parser))
8304 extension_specifier
8305 = cp_parser_storage_class_specifier_opt (parser);
8306 if (!extension_specifier)
8307 extension_specifier = cp_parser_function_specifier_opt (parser);
8310 /* Look for the `template' keyword. */
8311 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8312 /* Let the front end know that we are processing an explicit
8313 instantiation. */
8314 begin_explicit_instantiation ();
8315 /* [temp.explicit] says that we are supposed to ignore access
8316 control while processing explicit instantiation directives. */
8317 push_deferring_access_checks (dk_no_check);
8318 /* Parse a decl-specifier-seq. */
8319 decl_specifiers
8320 = cp_parser_decl_specifier_seq (parser,
8321 CP_PARSER_FLAGS_OPTIONAL,
8322 &attributes,
8323 &declares_class_or_enum);
8324 /* If there was exactly one decl-specifier, and it declared a class,
8325 and there's no declarator, then we have an explicit type
8326 instantiation. */
8327 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8329 tree type;
8331 type = check_tag_decl (decl_specifiers);
8332 /* Turn access control back on for names used during
8333 template instantiation. */
8334 pop_deferring_access_checks ();
8335 if (type)
8336 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8338 else
8340 tree declarator;
8341 tree decl;
8343 /* Parse the declarator. */
8344 declarator
8345 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8346 /*ctor_dtor_or_conv_p=*/NULL);
8347 decl = grokdeclarator (declarator, decl_specifiers,
8348 NORMAL, 0, NULL);
8349 /* Turn access control back on for names used during
8350 template instantiation. */
8351 pop_deferring_access_checks ();
8352 /* Do the explicit instantiation. */
8353 do_decl_instantiation (decl, extension_specifier);
8355 /* We're done with the instantiation. */
8356 end_explicit_instantiation ();
8358 cp_parser_consume_semicolon_at_end_of_statement (parser);
8361 /* Parse an explicit-specialization.
8363 explicit-specialization:
8364 template < > declaration
8366 Although the standard says `declaration', what it really means is:
8368 explicit-specialization:
8369 template <> decl-specifier [opt] init-declarator [opt] ;
8370 template <> function-definition
8371 template <> explicit-specialization
8372 template <> template-declaration */
8374 static void
8375 cp_parser_explicit_specialization (cp_parser* parser)
8377 /* Look for the `template' keyword. */
8378 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8379 /* Look for the `<'. */
8380 cp_parser_require (parser, CPP_LESS, "`<'");
8381 /* Look for the `>'. */
8382 cp_parser_require (parser, CPP_GREATER, "`>'");
8383 /* We have processed another parameter list. */
8384 ++parser->num_template_parameter_lists;
8385 /* Let the front end know that we are beginning a specialization. */
8386 begin_specialization ();
8388 /* If the next keyword is `template', we need to figure out whether
8389 or not we're looking a template-declaration. */
8390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8392 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8393 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8394 cp_parser_template_declaration_after_export (parser,
8395 /*member_p=*/false);
8396 else
8397 cp_parser_explicit_specialization (parser);
8399 else
8400 /* Parse the dependent declaration. */
8401 cp_parser_single_declaration (parser,
8402 /*member_p=*/false,
8403 /*friend_p=*/NULL);
8405 /* We're done with the specialization. */
8406 end_specialization ();
8407 /* We're done with this parameter list. */
8408 --parser->num_template_parameter_lists;
8411 /* Parse a type-specifier.
8413 type-specifier:
8414 simple-type-specifier
8415 class-specifier
8416 enum-specifier
8417 elaborated-type-specifier
8418 cv-qualifier
8420 GNU Extension:
8422 type-specifier:
8423 __complex__
8425 Returns a representation of the type-specifier. If the
8426 type-specifier is a keyword (like `int' or `const', or
8427 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8428 For a class-specifier, enum-specifier, or elaborated-type-specifier
8429 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8431 If IS_FRIEND is TRUE then this type-specifier is being declared a
8432 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8433 appearing in a decl-specifier-seq.
8435 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8436 class-specifier, enum-specifier, or elaborated-type-specifier, then
8437 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8438 FALSE.
8440 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8441 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8442 is set to FALSE. */
8444 static tree
8445 cp_parser_type_specifier (cp_parser* parser,
8446 cp_parser_flags flags,
8447 bool is_friend,
8448 bool is_declaration,
8449 bool* declares_class_or_enum,
8450 bool* is_cv_qualifier)
8452 tree type_spec = NULL_TREE;
8453 cp_token *token;
8454 enum rid keyword;
8456 /* Assume this type-specifier does not declare a new type. */
8457 if (declares_class_or_enum)
8458 *declares_class_or_enum = false;
8459 /* And that it does not specify a cv-qualifier. */
8460 if (is_cv_qualifier)
8461 *is_cv_qualifier = false;
8462 /* Peek at the next token. */
8463 token = cp_lexer_peek_token (parser->lexer);
8465 /* If we're looking at a keyword, we can use that to guide the
8466 production we choose. */
8467 keyword = token->keyword;
8468 switch (keyword)
8470 /* Any of these indicate either a class-specifier, or an
8471 elaborated-type-specifier. */
8472 case RID_CLASS:
8473 case RID_STRUCT:
8474 case RID_UNION:
8475 case RID_ENUM:
8476 /* Parse tentatively so that we can back up if we don't find a
8477 class-specifier or enum-specifier. */
8478 cp_parser_parse_tentatively (parser);
8479 /* Look for the class-specifier or enum-specifier. */
8480 if (keyword == RID_ENUM)
8481 type_spec = cp_parser_enum_specifier (parser);
8482 else
8483 type_spec = cp_parser_class_specifier (parser);
8485 /* If that worked, we're done. */
8486 if (cp_parser_parse_definitely (parser))
8488 if (declares_class_or_enum)
8489 *declares_class_or_enum = true;
8490 return type_spec;
8493 /* Fall through. */
8495 case RID_TYPENAME:
8496 /* Look for an elaborated-type-specifier. */
8497 type_spec = cp_parser_elaborated_type_specifier (parser,
8498 is_friend,
8499 is_declaration);
8500 /* We're declaring a class or enum -- unless we're using
8501 `typename'. */
8502 if (declares_class_or_enum && keyword != RID_TYPENAME)
8503 *declares_class_or_enum = true;
8504 return type_spec;
8506 case RID_CONST:
8507 case RID_VOLATILE:
8508 case RID_RESTRICT:
8509 type_spec = cp_parser_cv_qualifier_opt (parser);
8510 /* Even though we call a routine that looks for an optional
8511 qualifier, we know that there should be one. */
8512 my_friendly_assert (type_spec != NULL, 20000328);
8513 /* This type-specifier was a cv-qualified. */
8514 if (is_cv_qualifier)
8515 *is_cv_qualifier = true;
8517 return type_spec;
8519 case RID_COMPLEX:
8520 /* The `__complex__' keyword is a GNU extension. */
8521 return cp_lexer_consume_token (parser->lexer)->value;
8523 default:
8524 break;
8527 /* If we do not already have a type-specifier, assume we are looking
8528 at a simple-type-specifier. */
8529 type_spec = cp_parser_simple_type_specifier (parser, flags);
8531 /* If we didn't find a type-specifier, and a type-specifier was not
8532 optional in this context, issue an error message. */
8533 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8535 cp_parser_error (parser, "expected type specifier");
8536 return error_mark_node;
8539 return type_spec;
8542 /* Parse a simple-type-specifier.
8544 simple-type-specifier:
8545 :: [opt] nested-name-specifier [opt] type-name
8546 :: [opt] nested-name-specifier template template-id
8547 char
8548 wchar_t
8549 bool
8550 short
8552 long
8553 signed
8554 unsigned
8555 float
8556 double
8557 void
8559 GNU Extension:
8561 simple-type-specifier:
8562 __typeof__ unary-expression
8563 __typeof__ ( type-id )
8565 For the various keywords, the value returned is simply the
8566 TREE_IDENTIFIER representing the keyword. For the first two
8567 productions, the value returned is the indicated TYPE_DECL. */
8569 static tree
8570 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags)
8572 tree type = NULL_TREE;
8573 cp_token *token;
8575 /* Peek at the next token. */
8576 token = cp_lexer_peek_token (parser->lexer);
8578 /* If we're looking at a keyword, things are easy. */
8579 switch (token->keyword)
8581 case RID_CHAR:
8582 case RID_WCHAR:
8583 case RID_BOOL:
8584 case RID_SHORT:
8585 case RID_INT:
8586 case RID_LONG:
8587 case RID_SIGNED:
8588 case RID_UNSIGNED:
8589 case RID_FLOAT:
8590 case RID_DOUBLE:
8591 case RID_VOID:
8592 /* Consume the token. */
8593 return cp_lexer_consume_token (parser->lexer)->value;
8595 case RID_TYPEOF:
8597 tree operand;
8599 /* Consume the `typeof' token. */
8600 cp_lexer_consume_token (parser->lexer);
8601 /* Parse the operand to `typeof' */
8602 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8603 /* If it is not already a TYPE, take its type. */
8604 if (!TYPE_P (operand))
8605 operand = finish_typeof (operand);
8607 return operand;
8610 default:
8611 break;
8614 /* The type-specifier must be a user-defined type. */
8615 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8617 /* Don't gobble tokens or issue error messages if this is an
8618 optional type-specifier. */
8619 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8620 cp_parser_parse_tentatively (parser);
8622 /* Look for the optional `::' operator. */
8623 cp_parser_global_scope_opt (parser,
8624 /*current_scope_valid_p=*/false);
8625 /* Look for the nested-name specifier. */
8626 cp_parser_nested_name_specifier_opt (parser,
8627 /*typename_keyword_p=*/false,
8628 /*check_dependency_p=*/true,
8629 /*type_p=*/false);
8630 /* If we have seen a nested-name-specifier, and the next token
8631 is `template', then we are using the template-id production. */
8632 if (parser->scope
8633 && cp_parser_optional_template_keyword (parser))
8635 /* Look for the template-id. */
8636 type = cp_parser_template_id (parser,
8637 /*template_keyword_p=*/true,
8638 /*check_dependency_p=*/true);
8639 /* If the template-id did not name a type, we are out of
8640 luck. */
8641 if (TREE_CODE (type) != TYPE_DECL)
8643 cp_parser_error (parser, "expected template-id for type");
8644 type = NULL_TREE;
8647 /* Otherwise, look for a type-name. */
8648 else
8650 type = cp_parser_type_name (parser);
8651 if (type == error_mark_node)
8652 type = NULL_TREE;
8655 /* If it didn't work out, we don't have a TYPE. */
8656 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8657 && !cp_parser_parse_definitely (parser))
8658 type = NULL_TREE;
8661 /* If we didn't get a type-name, issue an error message. */
8662 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8664 cp_parser_error (parser, "expected type-name");
8665 return error_mark_node;
8668 return type;
8671 /* Parse a type-name.
8673 type-name:
8674 class-name
8675 enum-name
8676 typedef-name
8678 enum-name:
8679 identifier
8681 typedef-name:
8682 identifier
8684 Returns a TYPE_DECL for the the type. */
8686 static tree
8687 cp_parser_type_name (cp_parser* parser)
8689 tree type_decl;
8690 tree identifier;
8692 /* We can't know yet whether it is a class-name or not. */
8693 cp_parser_parse_tentatively (parser);
8694 /* Try a class-name. */
8695 type_decl = cp_parser_class_name (parser,
8696 /*typename_keyword_p=*/false,
8697 /*template_keyword_p=*/false,
8698 /*type_p=*/false,
8699 /*check_dependency_p=*/true,
8700 /*class_head_p=*/false);
8701 /* If it's not a class-name, keep looking. */
8702 if (!cp_parser_parse_definitely (parser))
8704 /* It must be a typedef-name or an enum-name. */
8705 identifier = cp_parser_identifier (parser);
8706 if (identifier == error_mark_node)
8707 return error_mark_node;
8709 /* Look up the type-name. */
8710 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8711 /* Issue an error if we did not find a type-name. */
8712 if (TREE_CODE (type_decl) != TYPE_DECL)
8714 cp_parser_error (parser, "expected type-name");
8715 type_decl = error_mark_node;
8717 /* Remember that the name was used in the definition of the
8718 current class so that we can check later to see if the
8719 meaning would have been different after the class was
8720 entirely defined. */
8721 else if (type_decl != error_mark_node
8722 && !parser->scope)
8723 maybe_note_name_used_in_class (identifier, type_decl);
8726 return type_decl;
8730 /* Parse an elaborated-type-specifier. Note that the grammar given
8731 here incorporates the resolution to DR68.
8733 elaborated-type-specifier:
8734 class-key :: [opt] nested-name-specifier [opt] identifier
8735 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8736 enum :: [opt] nested-name-specifier [opt] identifier
8737 typename :: [opt] nested-name-specifier identifier
8738 typename :: [opt] nested-name-specifier template [opt]
8739 template-id
8741 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8742 declared `friend'. If IS_DECLARATION is TRUE, then this
8743 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8744 something is being declared.
8746 Returns the TYPE specified. */
8748 static tree
8749 cp_parser_elaborated_type_specifier (cp_parser* parser,
8750 bool is_friend,
8751 bool is_declaration)
8753 enum tag_types tag_type;
8754 tree identifier;
8755 tree type = NULL_TREE;
8757 /* See if we're looking at the `enum' keyword. */
8758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8760 /* Consume the `enum' token. */
8761 cp_lexer_consume_token (parser->lexer);
8762 /* Remember that it's an enumeration type. */
8763 tag_type = enum_type;
8765 /* Or, it might be `typename'. */
8766 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8767 RID_TYPENAME))
8769 /* Consume the `typename' token. */
8770 cp_lexer_consume_token (parser->lexer);
8771 /* Remember that it's a `typename' type. */
8772 tag_type = typename_type;
8773 /* The `typename' keyword is only allowed in templates. */
8774 if (!processing_template_decl)
8775 pedwarn ("using `typename' outside of template");
8777 /* Otherwise it must be a class-key. */
8778 else
8780 tag_type = cp_parser_class_key (parser);
8781 if (tag_type == none_type)
8782 return error_mark_node;
8785 /* Look for the `::' operator. */
8786 cp_parser_global_scope_opt (parser,
8787 /*current_scope_valid_p=*/false);
8788 /* Look for the nested-name-specifier. */
8789 if (tag_type == typename_type)
8791 if (cp_parser_nested_name_specifier (parser,
8792 /*typename_keyword_p=*/true,
8793 /*check_dependency_p=*/true,
8794 /*type_p=*/true)
8795 == error_mark_node)
8796 return error_mark_node;
8798 else
8799 /* Even though `typename' is not present, the proposed resolution
8800 to Core Issue 180 says that in `class A<T>::B', `B' should be
8801 considered a type-name, even if `A<T>' is dependent. */
8802 cp_parser_nested_name_specifier_opt (parser,
8803 /*typename_keyword_p=*/true,
8804 /*check_dependency_p=*/true,
8805 /*type_p=*/true);
8806 /* For everything but enumeration types, consider a template-id. */
8807 if (tag_type != enum_type)
8809 bool template_p = false;
8810 tree decl;
8812 /* Allow the `template' keyword. */
8813 template_p = cp_parser_optional_template_keyword (parser);
8814 /* If we didn't see `template', we don't know if there's a
8815 template-id or not. */
8816 if (!template_p)
8817 cp_parser_parse_tentatively (parser);
8818 /* Parse the template-id. */
8819 decl = cp_parser_template_id (parser, template_p,
8820 /*check_dependency_p=*/true);
8821 /* If we didn't find a template-id, look for an ordinary
8822 identifier. */
8823 if (!template_p && !cp_parser_parse_definitely (parser))
8825 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8826 in effect, then we must assume that, upon instantiation, the
8827 template will correspond to a class. */
8828 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8829 && tag_type == typename_type)
8830 type = make_typename_type (parser->scope, decl,
8831 /*complain=*/1);
8832 else
8833 type = TREE_TYPE (decl);
8836 /* For an enumeration type, consider only a plain identifier. */
8837 if (!type)
8839 identifier = cp_parser_identifier (parser);
8841 if (identifier == error_mark_node)
8842 return error_mark_node;
8844 /* For a `typename', we needn't call xref_tag. */
8845 if (tag_type == typename_type)
8846 return make_typename_type (parser->scope, identifier,
8847 /*complain=*/1);
8848 /* Look up a qualified name in the usual way. */
8849 if (parser->scope)
8851 tree decl;
8853 /* In an elaborated-type-specifier, names are assumed to name
8854 types, so we set IS_TYPE to TRUE when calling
8855 cp_parser_lookup_name. */
8856 decl = cp_parser_lookup_name (parser, identifier,
8857 /*is_type=*/true,
8858 /*is_namespace=*/false,
8859 /*check_dependency=*/true);
8861 /* If we are parsing friend declaration, DECL may be a
8862 TEMPLATE_DECL tree node here. However, we need to check
8863 whether this TEMPLATE_DECL results in valid code. Consider
8864 the following example:
8866 namespace N {
8867 template <class T> class C {};
8869 class X {
8870 template <class T> friend class N::C; // #1, valid code
8872 template <class T> class Y {
8873 friend class N::C; // #2, invalid code
8876 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8877 name lookup of `N::C'. We see that friend declaration must
8878 be template for the code to be valid. Note that
8879 processing_template_decl does not work here since it is
8880 always 1 for the above two cases. */
8882 decl = (cp_parser_maybe_treat_template_as_class
8883 (decl, /*tag_name_p=*/is_friend
8884 && parser->num_template_parameter_lists));
8886 if (TREE_CODE (decl) != TYPE_DECL)
8888 error ("expected type-name");
8889 return error_mark_node;
8891 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8892 && tag_type != enum_type)
8893 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8894 tag_type == record_type ? "struct" : "class");
8895 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8896 && tag_type == enum_type)
8897 error ("`%T' referred to as enum", TREE_TYPE (decl));
8899 type = TREE_TYPE (decl);
8901 else
8903 /* An elaborated-type-specifier sometimes introduces a new type and
8904 sometimes names an existing type. Normally, the rule is that it
8905 introduces a new type only if there is not an existing type of
8906 the same name already in scope. For example, given:
8908 struct S {};
8909 void f() { struct S s; }
8911 the `struct S' in the body of `f' is the same `struct S' as in
8912 the global scope; the existing definition is used. However, if
8913 there were no global declaration, this would introduce a new
8914 local class named `S'.
8916 An exception to this rule applies to the following code:
8918 namespace N { struct S; }
8920 Here, the elaborated-type-specifier names a new type
8921 unconditionally; even if there is already an `S' in the
8922 containing scope this declaration names a new type.
8923 This exception only applies if the elaborated-type-specifier
8924 forms the complete declaration:
8926 [class.name]
8928 A declaration consisting solely of `class-key identifier ;' is
8929 either a redeclaration of the name in the current scope or a
8930 forward declaration of the identifier as a class name. It
8931 introduces the name into the current scope.
8933 We are in this situation precisely when the next token is a `;'.
8935 An exception to the exception is that a `friend' declaration does
8936 *not* name a new type; i.e., given:
8938 struct S { friend struct T; };
8940 `T' is not a new type in the scope of `S'.
8942 Also, `new struct S' or `sizeof (struct S)' never results in the
8943 definition of a new type; a new type can only be declared in a
8944 declaration context. */
8946 type = xref_tag (tag_type, identifier,
8947 /*attributes=*/NULL_TREE,
8948 (is_friend
8949 || !is_declaration
8950 || cp_lexer_next_token_is_not (parser->lexer,
8951 CPP_SEMICOLON)));
8954 if (tag_type != enum_type)
8955 cp_parser_check_class_key (tag_type, type);
8956 return type;
8959 /* Parse an enum-specifier.
8961 enum-specifier:
8962 enum identifier [opt] { enumerator-list [opt] }
8964 Returns an ENUM_TYPE representing the enumeration. */
8966 static tree
8967 cp_parser_enum_specifier (cp_parser* parser)
8969 cp_token *token;
8970 tree identifier = NULL_TREE;
8971 tree type;
8973 /* Look for the `enum' keyword. */
8974 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8975 return error_mark_node;
8976 /* Peek at the next token. */
8977 token = cp_lexer_peek_token (parser->lexer);
8979 /* See if it is an identifier. */
8980 if (token->type == CPP_NAME)
8981 identifier = cp_parser_identifier (parser);
8983 /* Look for the `{'. */
8984 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8985 return error_mark_node;
8987 /* At this point, we're going ahead with the enum-specifier, even
8988 if some other problem occurs. */
8989 cp_parser_commit_to_tentative_parse (parser);
8991 /* Issue an error message if type-definitions are forbidden here. */
8992 cp_parser_check_type_definition (parser);
8994 /* Create the new type. */
8995 type = start_enum (identifier ? identifier : make_anon_name ());
8997 /* Peek at the next token. */
8998 token = cp_lexer_peek_token (parser->lexer);
8999 /* If it's not a `}', then there are some enumerators. */
9000 if (token->type != CPP_CLOSE_BRACE)
9001 cp_parser_enumerator_list (parser, type);
9002 /* Look for the `}'. */
9003 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9005 /* Finish up the enumeration. */
9006 finish_enum (type);
9008 return type;
9011 /* Parse an enumerator-list. The enumerators all have the indicated
9012 TYPE.
9014 enumerator-list:
9015 enumerator-definition
9016 enumerator-list , enumerator-definition */
9018 static void
9019 cp_parser_enumerator_list (cp_parser* parser, tree type)
9021 while (true)
9023 cp_token *token;
9025 /* Parse an enumerator-definition. */
9026 cp_parser_enumerator_definition (parser, type);
9027 /* Peek at the next token. */
9028 token = cp_lexer_peek_token (parser->lexer);
9029 /* If it's not a `,', then we've reached the end of the
9030 list. */
9031 if (token->type != CPP_COMMA)
9032 break;
9033 /* Otherwise, consume the `,' and keep going. */
9034 cp_lexer_consume_token (parser->lexer);
9035 /* If the next token is a `}', there is a trailing comma. */
9036 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9038 if (pedantic && !in_system_header)
9039 pedwarn ("comma at end of enumerator list");
9040 break;
9045 /* Parse an enumerator-definition. The enumerator has the indicated
9046 TYPE.
9048 enumerator-definition:
9049 enumerator
9050 enumerator = constant-expression
9052 enumerator:
9053 identifier */
9055 static void
9056 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9058 cp_token *token;
9059 tree identifier;
9060 tree value;
9062 /* Look for the identifier. */
9063 identifier = cp_parser_identifier (parser);
9064 if (identifier == error_mark_node)
9065 return;
9067 /* Peek at the next token. */
9068 token = cp_lexer_peek_token (parser->lexer);
9069 /* If it's an `=', then there's an explicit value. */
9070 if (token->type == CPP_EQ)
9072 /* Consume the `=' token. */
9073 cp_lexer_consume_token (parser->lexer);
9074 /* Parse the value. */
9075 value = cp_parser_constant_expression (parser,
9076 /*allow_non_constant=*/false,
9077 NULL);
9079 else
9080 value = NULL_TREE;
9082 /* Create the enumerator. */
9083 build_enumerator (identifier, value, type);
9086 /* Parse a namespace-name.
9088 namespace-name:
9089 original-namespace-name
9090 namespace-alias
9092 Returns the NAMESPACE_DECL for the namespace. */
9094 static tree
9095 cp_parser_namespace_name (cp_parser* parser)
9097 tree identifier;
9098 tree namespace_decl;
9100 /* Get the name of the namespace. */
9101 identifier = cp_parser_identifier (parser);
9102 if (identifier == error_mark_node)
9103 return error_mark_node;
9105 /* Look up the identifier in the currently active scope. Look only
9106 for namespaces, due to:
9108 [basic.lookup.udir]
9110 When looking up a namespace-name in a using-directive or alias
9111 definition, only namespace names are considered.
9113 And:
9115 [basic.lookup.qual]
9117 During the lookup of a name preceding the :: scope resolution
9118 operator, object, function, and enumerator names are ignored.
9120 (Note that cp_parser_class_or_namespace_name only calls this
9121 function if the token after the name is the scope resolution
9122 operator.) */
9123 namespace_decl = cp_parser_lookup_name (parser, identifier,
9124 /*is_type=*/false,
9125 /*is_namespace=*/true,
9126 /*check_dependency=*/true);
9127 /* If it's not a namespace, issue an error. */
9128 if (namespace_decl == error_mark_node
9129 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9131 cp_parser_error (parser, "expected namespace-name");
9132 namespace_decl = error_mark_node;
9135 return namespace_decl;
9138 /* Parse a namespace-definition.
9140 namespace-definition:
9141 named-namespace-definition
9142 unnamed-namespace-definition
9144 named-namespace-definition:
9145 original-namespace-definition
9146 extension-namespace-definition
9148 original-namespace-definition:
9149 namespace identifier { namespace-body }
9151 extension-namespace-definition:
9152 namespace original-namespace-name { namespace-body }
9154 unnamed-namespace-definition:
9155 namespace { namespace-body } */
9157 static void
9158 cp_parser_namespace_definition (cp_parser* parser)
9160 tree identifier;
9162 /* Look for the `namespace' keyword. */
9163 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9165 /* Get the name of the namespace. We do not attempt to distinguish
9166 between an original-namespace-definition and an
9167 extension-namespace-definition at this point. The semantic
9168 analysis routines are responsible for that. */
9169 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9170 identifier = cp_parser_identifier (parser);
9171 else
9172 identifier = NULL_TREE;
9174 /* Look for the `{' to start the namespace. */
9175 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9176 /* Start the namespace. */
9177 push_namespace (identifier);
9178 /* Parse the body of the namespace. */
9179 cp_parser_namespace_body (parser);
9180 /* Finish the namespace. */
9181 pop_namespace ();
9182 /* Look for the final `}'. */
9183 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9186 /* Parse a namespace-body.
9188 namespace-body:
9189 declaration-seq [opt] */
9191 static void
9192 cp_parser_namespace_body (cp_parser* parser)
9194 cp_parser_declaration_seq_opt (parser);
9197 /* Parse a namespace-alias-definition.
9199 namespace-alias-definition:
9200 namespace identifier = qualified-namespace-specifier ; */
9202 static void
9203 cp_parser_namespace_alias_definition (cp_parser* parser)
9205 tree identifier;
9206 tree namespace_specifier;
9208 /* Look for the `namespace' keyword. */
9209 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9210 /* Look for the identifier. */
9211 identifier = cp_parser_identifier (parser);
9212 if (identifier == error_mark_node)
9213 return;
9214 /* Look for the `=' token. */
9215 cp_parser_require (parser, CPP_EQ, "`='");
9216 /* Look for the qualified-namespace-specifier. */
9217 namespace_specifier
9218 = cp_parser_qualified_namespace_specifier (parser);
9219 /* Look for the `;' token. */
9220 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9222 /* Register the alias in the symbol table. */
9223 do_namespace_alias (identifier, namespace_specifier);
9226 /* Parse a qualified-namespace-specifier.
9228 qualified-namespace-specifier:
9229 :: [opt] nested-name-specifier [opt] namespace-name
9231 Returns a NAMESPACE_DECL corresponding to the specified
9232 namespace. */
9234 static tree
9235 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9237 /* Look for the optional `::'. */
9238 cp_parser_global_scope_opt (parser,
9239 /*current_scope_valid_p=*/false);
9241 /* Look for the optional nested-name-specifier. */
9242 cp_parser_nested_name_specifier_opt (parser,
9243 /*typename_keyword_p=*/false,
9244 /*check_dependency_p=*/true,
9245 /*type_p=*/false);
9247 return cp_parser_namespace_name (parser);
9250 /* Parse a using-declaration.
9252 using-declaration:
9253 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9254 using :: unqualified-id ; */
9256 static void
9257 cp_parser_using_declaration (cp_parser* parser)
9259 cp_token *token;
9260 bool typename_p = false;
9261 bool global_scope_p;
9262 tree decl;
9263 tree identifier;
9264 tree scope;
9266 /* Look for the `using' keyword. */
9267 cp_parser_require_keyword (parser, RID_USING, "`using'");
9269 /* Peek at the next token. */
9270 token = cp_lexer_peek_token (parser->lexer);
9271 /* See if it's `typename'. */
9272 if (token->keyword == RID_TYPENAME)
9274 /* Remember that we've seen it. */
9275 typename_p = true;
9276 /* Consume the `typename' token. */
9277 cp_lexer_consume_token (parser->lexer);
9280 /* Look for the optional global scope qualification. */
9281 global_scope_p
9282 = (cp_parser_global_scope_opt (parser,
9283 /*current_scope_valid_p=*/false)
9284 != NULL_TREE);
9286 /* If we saw `typename', or didn't see `::', then there must be a
9287 nested-name-specifier present. */
9288 if (typename_p || !global_scope_p)
9289 cp_parser_nested_name_specifier (parser, typename_p,
9290 /*check_dependency_p=*/true,
9291 /*type_p=*/false);
9292 /* Otherwise, we could be in either of the two productions. In that
9293 case, treat the nested-name-specifier as optional. */
9294 else
9295 cp_parser_nested_name_specifier_opt (parser,
9296 /*typename_keyword_p=*/false,
9297 /*check_dependency_p=*/true,
9298 /*type_p=*/false);
9300 /* Parse the unqualified-id. */
9301 identifier = cp_parser_unqualified_id (parser,
9302 /*template_keyword_p=*/false,
9303 /*check_dependency_p=*/true);
9305 /* The function we call to handle a using-declaration is different
9306 depending on what scope we are in. */
9307 scope = current_scope ();
9308 if (scope && TYPE_P (scope))
9310 /* Create the USING_DECL. */
9311 decl = do_class_using_decl (build_nt (SCOPE_REF,
9312 parser->scope,
9313 identifier));
9314 /* Add it to the list of members in this class. */
9315 finish_member_declaration (decl);
9317 else
9319 decl = cp_parser_lookup_name_simple (parser, identifier);
9320 if (decl == error_mark_node)
9322 if (parser->scope && parser->scope != global_namespace)
9323 error ("`%D::%D' has not been declared",
9324 parser->scope, identifier);
9325 else
9326 error ("`::%D' has not been declared", identifier);
9328 else if (scope)
9329 do_local_using_decl (decl);
9330 else
9331 do_toplevel_using_decl (decl);
9334 /* Look for the final `;'. */
9335 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9338 /* Parse a using-directive.
9340 using-directive:
9341 using namespace :: [opt] nested-name-specifier [opt]
9342 namespace-name ; */
9344 static void
9345 cp_parser_using_directive (cp_parser* parser)
9347 tree namespace_decl;
9349 /* Look for the `using' keyword. */
9350 cp_parser_require_keyword (parser, RID_USING, "`using'");
9351 /* And the `namespace' keyword. */
9352 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9353 /* Look for the optional `::' operator. */
9354 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9355 /* And the optional nested-name-sepcifier. */
9356 cp_parser_nested_name_specifier_opt (parser,
9357 /*typename_keyword_p=*/false,
9358 /*check_dependency_p=*/true,
9359 /*type_p=*/false);
9360 /* Get the namespace being used. */
9361 namespace_decl = cp_parser_namespace_name (parser);
9362 /* Update the symbol table. */
9363 do_using_directive (namespace_decl);
9364 /* Look for the final `;'. */
9365 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9368 /* Parse an asm-definition.
9370 asm-definition:
9371 asm ( string-literal ) ;
9373 GNU Extension:
9375 asm-definition:
9376 asm volatile [opt] ( string-literal ) ;
9377 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9378 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9379 : asm-operand-list [opt] ) ;
9380 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9381 : asm-operand-list [opt]
9382 : asm-operand-list [opt] ) ; */
9384 static void
9385 cp_parser_asm_definition (cp_parser* parser)
9387 cp_token *token;
9388 tree string;
9389 tree outputs = NULL_TREE;
9390 tree inputs = NULL_TREE;
9391 tree clobbers = NULL_TREE;
9392 tree asm_stmt;
9393 bool volatile_p = false;
9394 bool extended_p = false;
9396 /* Look for the `asm' keyword. */
9397 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9398 /* See if the next token is `volatile'. */
9399 if (cp_parser_allow_gnu_extensions_p (parser)
9400 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9402 /* Remember that we saw the `volatile' keyword. */
9403 volatile_p = true;
9404 /* Consume the token. */
9405 cp_lexer_consume_token (parser->lexer);
9407 /* Look for the opening `('. */
9408 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9409 /* Look for the string. */
9410 token = cp_parser_require (parser, CPP_STRING, "asm body");
9411 if (!token)
9412 return;
9413 string = token->value;
9414 /* If we're allowing GNU extensions, check for the extended assembly
9415 syntax. Unfortunately, the `:' tokens need not be separated by
9416 a space in C, and so, for compatibility, we tolerate that here
9417 too. Doing that means that we have to treat the `::' operator as
9418 two `:' tokens. */
9419 if (cp_parser_allow_gnu_extensions_p (parser)
9420 && at_function_scope_p ()
9421 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9422 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9424 bool inputs_p = false;
9425 bool clobbers_p = false;
9427 /* The extended syntax was used. */
9428 extended_p = true;
9430 /* Look for outputs. */
9431 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9433 /* Consume the `:'. */
9434 cp_lexer_consume_token (parser->lexer);
9435 /* Parse the output-operands. */
9436 if (cp_lexer_next_token_is_not (parser->lexer,
9437 CPP_COLON)
9438 && cp_lexer_next_token_is_not (parser->lexer,
9439 CPP_SCOPE)
9440 && cp_lexer_next_token_is_not (parser->lexer,
9441 CPP_CLOSE_PAREN))
9442 outputs = cp_parser_asm_operand_list (parser);
9444 /* If the next token is `::', there are no outputs, and the
9445 next token is the beginning of the inputs. */
9446 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9448 /* Consume the `::' token. */
9449 cp_lexer_consume_token (parser->lexer);
9450 /* The inputs are coming next. */
9451 inputs_p = true;
9454 /* Look for inputs. */
9455 if (inputs_p
9456 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9458 if (!inputs_p)
9459 /* Consume the `:'. */
9460 cp_lexer_consume_token (parser->lexer);
9461 /* Parse the output-operands. */
9462 if (cp_lexer_next_token_is_not (parser->lexer,
9463 CPP_COLON)
9464 && cp_lexer_next_token_is_not (parser->lexer,
9465 CPP_SCOPE)
9466 && cp_lexer_next_token_is_not (parser->lexer,
9467 CPP_CLOSE_PAREN))
9468 inputs = cp_parser_asm_operand_list (parser);
9470 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9471 /* The clobbers are coming next. */
9472 clobbers_p = true;
9474 /* Look for clobbers. */
9475 if (clobbers_p
9476 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9478 if (!clobbers_p)
9479 /* Consume the `:'. */
9480 cp_lexer_consume_token (parser->lexer);
9481 /* Parse the clobbers. */
9482 if (cp_lexer_next_token_is_not (parser->lexer,
9483 CPP_CLOSE_PAREN))
9484 clobbers = cp_parser_asm_clobber_list (parser);
9487 /* Look for the closing `)'. */
9488 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9489 cp_parser_skip_to_closing_parenthesis (parser);
9490 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9492 /* Create the ASM_STMT. */
9493 if (at_function_scope_p ())
9495 asm_stmt =
9496 finish_asm_stmt (volatile_p
9497 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9498 string, outputs, inputs, clobbers);
9499 /* If the extended syntax was not used, mark the ASM_STMT. */
9500 if (!extended_p)
9501 ASM_INPUT_P (asm_stmt) = 1;
9503 else
9504 assemble_asm (string);
9507 /* Declarators [gram.dcl.decl] */
9509 /* Parse an init-declarator.
9511 init-declarator:
9512 declarator initializer [opt]
9514 GNU Extension:
9516 init-declarator:
9517 declarator asm-specification [opt] attributes [opt] initializer [opt]
9519 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9520 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9521 then this declarator appears in a class scope. The new DECL created
9522 by this declarator is returned.
9524 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9525 for a function-definition here as well. If the declarator is a
9526 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9527 be TRUE upon return. By that point, the function-definition will
9528 have been completely parsed.
9530 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9531 is FALSE. */
9533 static tree
9534 cp_parser_init_declarator (cp_parser* parser,
9535 tree decl_specifiers,
9536 tree prefix_attributes,
9537 bool function_definition_allowed_p,
9538 bool member_p,
9539 bool* function_definition_p)
9541 cp_token *token;
9542 tree declarator;
9543 tree attributes;
9544 tree asm_specification;
9545 tree initializer;
9546 tree decl = NULL_TREE;
9547 tree scope;
9548 bool is_initialized;
9549 bool is_parenthesized_init;
9550 bool ctor_dtor_or_conv_p;
9551 bool friend_p;
9553 /* Assume that this is not the declarator for a function
9554 definition. */
9555 if (function_definition_p)
9556 *function_definition_p = false;
9558 /* Defer access checks while parsing the declarator; we cannot know
9559 what names are accessible until we know what is being
9560 declared. */
9561 resume_deferring_access_checks ();
9563 /* Parse the declarator. */
9564 declarator
9565 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9566 &ctor_dtor_or_conv_p);
9567 /* Gather up the deferred checks. */
9568 stop_deferring_access_checks ();
9570 /* If the DECLARATOR was erroneous, there's no need to go
9571 further. */
9572 if (declarator == error_mark_node)
9573 return error_mark_node;
9575 /* Figure out what scope the entity declared by the DECLARATOR is
9576 located in. `grokdeclarator' sometimes changes the scope, so
9577 we compute it now. */
9578 scope = get_scope_of_declarator (declarator);
9580 /* If we're allowing GNU extensions, look for an asm-specification
9581 and attributes. */
9582 if (cp_parser_allow_gnu_extensions_p (parser))
9584 /* Look for an asm-specification. */
9585 asm_specification = cp_parser_asm_specification_opt (parser);
9586 /* And attributes. */
9587 attributes = cp_parser_attributes_opt (parser);
9589 else
9591 asm_specification = NULL_TREE;
9592 attributes = NULL_TREE;
9595 /* Peek at the next token. */
9596 token = cp_lexer_peek_token (parser->lexer);
9597 /* Check to see if the token indicates the start of a
9598 function-definition. */
9599 if (cp_parser_token_starts_function_definition_p (token))
9601 if (!function_definition_allowed_p)
9603 /* If a function-definition should not appear here, issue an
9604 error message. */
9605 cp_parser_error (parser,
9606 "a function-definition is not allowed here");
9607 return error_mark_node;
9609 else
9611 /* Neither attributes nor an asm-specification are allowed
9612 on a function-definition. */
9613 if (asm_specification)
9614 error ("an asm-specification is not allowed on a function-definition");
9615 if (attributes)
9616 error ("attributes are not allowed on a function-definition");
9617 /* This is a function-definition. */
9618 *function_definition_p = true;
9620 /* Parse the function definition. */
9621 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9622 (parser, decl_specifiers, prefix_attributes, declarator));
9624 return decl;
9628 /* [dcl.dcl]
9630 Only in function declarations for constructors, destructors, and
9631 type conversions can the decl-specifier-seq be omitted.
9633 We explicitly postpone this check past the point where we handle
9634 function-definitions because we tolerate function-definitions
9635 that are missing their return types in some modes. */
9636 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9638 cp_parser_error (parser,
9639 "expected constructor, destructor, or type conversion");
9640 return error_mark_node;
9643 /* An `=' or an `(' indicates an initializer. */
9644 is_initialized = (token->type == CPP_EQ
9645 || token->type == CPP_OPEN_PAREN);
9646 /* If the init-declarator isn't initialized and isn't followed by a
9647 `,' or `;', it's not a valid init-declarator. */
9648 if (!is_initialized
9649 && token->type != CPP_COMMA
9650 && token->type != CPP_SEMICOLON)
9652 cp_parser_error (parser, "expected init-declarator");
9653 return error_mark_node;
9656 /* Because start_decl has side-effects, we should only call it if we
9657 know we're going ahead. By this point, we know that we cannot
9658 possibly be looking at any other construct. */
9659 cp_parser_commit_to_tentative_parse (parser);
9661 /* Check to see whether or not this declaration is a friend. */
9662 friend_p = cp_parser_friend_p (decl_specifiers);
9664 /* Check that the number of template-parameter-lists is OK. */
9665 if (!cp_parser_check_declarator_template_parameters (parser,
9666 declarator))
9667 return error_mark_node;
9669 /* Enter the newly declared entry in the symbol table. If we're
9670 processing a declaration in a class-specifier, we wait until
9671 after processing the initializer. */
9672 if (!member_p)
9674 if (parser->in_unbraced_linkage_specification_p)
9676 decl_specifiers = tree_cons (error_mark_node,
9677 get_identifier ("extern"),
9678 decl_specifiers);
9679 have_extern_spec = false;
9681 decl = start_decl (declarator,
9682 decl_specifiers,
9683 is_initialized,
9684 attributes,
9685 prefix_attributes);
9688 /* Enter the SCOPE. That way unqualified names appearing in the
9689 initializer will be looked up in SCOPE. */
9690 if (scope)
9691 push_scope (scope);
9693 /* Perform deferred access control checks, now that we know in which
9694 SCOPE the declared entity resides. */
9695 if (!member_p && decl)
9697 tree saved_current_function_decl = NULL_TREE;
9699 /* If the entity being declared is a function, pretend that we
9700 are in its scope. If it is a `friend', it may have access to
9701 things that would not otherwise be accessible. */
9702 if (TREE_CODE (decl) == FUNCTION_DECL)
9704 saved_current_function_decl = current_function_decl;
9705 current_function_decl = decl;
9708 /* Perform the access control checks for the declarator and the
9709 the decl-specifiers. */
9710 perform_deferred_access_checks ();
9712 /* Restore the saved value. */
9713 if (TREE_CODE (decl) == FUNCTION_DECL)
9714 current_function_decl = saved_current_function_decl;
9717 /* Parse the initializer. */
9718 if (is_initialized)
9719 initializer = cp_parser_initializer (parser, &is_parenthesized_init);
9720 else
9722 initializer = NULL_TREE;
9723 is_parenthesized_init = false;
9726 /* The old parser allows attributes to appear after a parenthesized
9727 initializer. Mark Mitchell proposed removing this functionality
9728 on the GCC mailing lists on 2002-08-13. This parser accepts the
9729 attributes -- but ignores them. */
9730 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9731 if (cp_parser_attributes_opt (parser))
9732 warning ("attributes after parenthesized initializer ignored");
9734 /* Leave the SCOPE, now that we have processed the initializer. It
9735 is important to do this before calling cp_finish_decl because it
9736 makes decisions about whether to create DECL_STMTs or not based
9737 on the current scope. */
9738 if (scope)
9739 pop_scope (scope);
9741 /* For an in-class declaration, use `grokfield' to create the
9742 declaration. */
9743 if (member_p)
9745 decl = grokfield (declarator, decl_specifiers,
9746 initializer, /*asmspec=*/NULL_TREE,
9747 /*attributes=*/NULL_TREE);
9748 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9749 cp_parser_save_default_args (parser, decl);
9752 /* Finish processing the declaration. But, skip friend
9753 declarations. */
9754 if (!friend_p && decl)
9755 cp_finish_decl (decl,
9756 initializer,
9757 asm_specification,
9758 /* If the initializer is in parentheses, then this is
9759 a direct-initialization, which means that an
9760 `explicit' constructor is OK. Otherwise, an
9761 `explicit' constructor cannot be used. */
9762 ((is_parenthesized_init || !is_initialized)
9763 ? 0 : LOOKUP_ONLYCONVERTING));
9765 return decl;
9768 /* Parse a declarator.
9770 declarator:
9771 direct-declarator
9772 ptr-operator declarator
9774 abstract-declarator:
9775 ptr-operator abstract-declarator [opt]
9776 direct-abstract-declarator
9778 GNU Extensions:
9780 declarator:
9781 attributes [opt] direct-declarator
9782 attributes [opt] ptr-operator declarator
9784 abstract-declarator:
9785 attributes [opt] ptr-operator abstract-declarator [opt]
9786 attributes [opt] direct-abstract-declarator
9788 Returns a representation of the declarator. If the declarator has
9789 the form `* declarator', then an INDIRECT_REF is returned, whose
9790 only operand is the sub-declarator. Analagously, `& declarator' is
9791 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9792 used. The first operand is the TYPE for `X'. The second operand
9793 is an INDIRECT_REF whose operand is the sub-declarator.
9795 Otherwise, the reprsentation is as for a direct-declarator.
9797 (It would be better to define a structure type to represent
9798 declarators, rather than abusing `tree' nodes to represent
9799 declarators. That would be much clearer and save some memory.
9800 There is no reason for declarators to be garbage-collected, for
9801 example; they are created during parser and no longer needed after
9802 `grokdeclarator' has been called.)
9804 For a ptr-operator that has the optional cv-qualifier-seq,
9805 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9806 node.
9808 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9809 true if this declarator represents a constructor, destructor, or
9810 type conversion operator. Otherwise, it is set to false.
9812 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9813 a decl-specifier-seq unless it declares a constructor, destructor,
9814 or conversion. It might seem that we could check this condition in
9815 semantic analysis, rather than parsing, but that makes it difficult
9816 to handle something like `f()'. We want to notice that there are
9817 no decl-specifiers, and therefore realize that this is an
9818 expression, not a declaration.) */
9820 static tree
9821 cp_parser_declarator (cp_parser* parser,
9822 cp_parser_declarator_kind dcl_kind,
9823 bool* ctor_dtor_or_conv_p)
9825 cp_token *token;
9826 tree declarator;
9827 enum tree_code code;
9828 tree cv_qualifier_seq;
9829 tree class_type;
9830 tree attributes = NULL_TREE;
9832 /* Assume this is not a constructor, destructor, or type-conversion
9833 operator. */
9834 if (ctor_dtor_or_conv_p)
9835 *ctor_dtor_or_conv_p = false;
9837 if (cp_parser_allow_gnu_extensions_p (parser))
9838 attributes = cp_parser_attributes_opt (parser);
9840 /* Peek at the next token. */
9841 token = cp_lexer_peek_token (parser->lexer);
9843 /* Check for the ptr-operator production. */
9844 cp_parser_parse_tentatively (parser);
9845 /* Parse the ptr-operator. */
9846 code = cp_parser_ptr_operator (parser,
9847 &class_type,
9848 &cv_qualifier_seq);
9849 /* If that worked, then we have a ptr-operator. */
9850 if (cp_parser_parse_definitely (parser))
9852 /* The dependent declarator is optional if we are parsing an
9853 abstract-declarator. */
9854 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9855 cp_parser_parse_tentatively (parser);
9857 /* Parse the dependent declarator. */
9858 declarator = cp_parser_declarator (parser, dcl_kind,
9859 /*ctor_dtor_or_conv_p=*/NULL);
9861 /* If we are parsing an abstract-declarator, we must handle the
9862 case where the dependent declarator is absent. */
9863 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9864 && !cp_parser_parse_definitely (parser))
9865 declarator = NULL_TREE;
9867 /* Build the representation of the ptr-operator. */
9868 if (code == INDIRECT_REF)
9869 declarator = make_pointer_declarator (cv_qualifier_seq,
9870 declarator);
9871 else
9872 declarator = make_reference_declarator (cv_qualifier_seq,
9873 declarator);
9874 /* Handle the pointer-to-member case. */
9875 if (class_type)
9876 declarator = build_nt (SCOPE_REF, class_type, declarator);
9878 /* Everything else is a direct-declarator. */
9879 else
9880 declarator = cp_parser_direct_declarator (parser,
9881 dcl_kind,
9882 ctor_dtor_or_conv_p);
9884 if (attributes && declarator != error_mark_node)
9885 declarator = tree_cons (attributes, declarator, NULL_TREE);
9887 return declarator;
9890 /* Parse a direct-declarator or direct-abstract-declarator.
9892 direct-declarator:
9893 declarator-id
9894 direct-declarator ( parameter-declaration-clause )
9895 cv-qualifier-seq [opt]
9896 exception-specification [opt]
9897 direct-declarator [ constant-expression [opt] ]
9898 ( declarator )
9900 direct-abstract-declarator:
9901 direct-abstract-declarator [opt]
9902 ( parameter-declaration-clause )
9903 cv-qualifier-seq [opt]
9904 exception-specification [opt]
9905 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9906 ( abstract-declarator )
9908 Returns a representation of the declarator. DCL_KIND is
9909 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9910 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9911 we are parsing a direct-declarator. It is
9912 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9913 of ambiguity we prefer an abstract declarator, as per
9914 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9915 cp_parser_declarator.
9917 For the declarator-id production, the representation is as for an
9918 id-expression, except that a qualified name is represented as a
9919 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9920 see the documentation of the FUNCTION_DECLARATOR_* macros for
9921 information about how to find the various declarator components.
9922 An array-declarator is represented as an ARRAY_REF. The
9923 direct-declarator is the first operand; the constant-expression
9924 indicating the size of the array is the second operand. */
9926 static tree
9927 cp_parser_direct_declarator (cp_parser* parser,
9928 cp_parser_declarator_kind dcl_kind,
9929 bool* ctor_dtor_or_conv_p)
9931 cp_token *token;
9932 tree declarator = NULL_TREE;
9933 tree scope = NULL_TREE;
9934 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9935 bool saved_in_declarator_p = parser->in_declarator_p;
9936 bool first = true;
9938 while (true)
9940 /* Peek at the next token. */
9941 token = cp_lexer_peek_token (parser->lexer);
9942 if (token->type == CPP_OPEN_PAREN)
9944 /* This is either a parameter-declaration-clause, or a
9945 parenthesized declarator. When we know we are parsing a
9946 named declarator, it must be a paranthesized declarator
9947 if FIRST is true. For instance, `(int)' is a
9948 parameter-declaration-clause, with an omitted
9949 direct-abstract-declarator. But `((*))', is a
9950 parenthesized abstract declarator. Finally, when T is a
9951 template parameter `(T)' is a
9952 paremeter-declaration-clause, and not a parenthesized
9953 named declarator.
9955 We first try and parse a parameter-declaration-clause,
9956 and then try a nested declarator (if FIRST is true).
9958 It is not an error for it not to be a
9959 parameter-declaration-clause, even when FIRST is
9960 false. Consider,
9962 int i (int);
9963 int i (3);
9965 The first is the declaration of a function while the
9966 second is a the definition of a variable, including its
9967 initializer.
9969 Having seen only the parenthesis, we cannot know which of
9970 these two alternatives should be selected. Even more
9971 complex are examples like:
9973 int i (int (a));
9974 int i (int (3));
9976 The former is a function-declaration; the latter is a
9977 variable initialization.
9979 Thus again, we try a parameter-declation-clause, and if
9980 that fails, we back out and return. */
9982 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9984 tree params;
9986 cp_parser_parse_tentatively (parser);
9988 /* Consume the `('. */
9989 cp_lexer_consume_token (parser->lexer);
9990 if (first)
9992 /* If this is going to be an abstract declarator, we're
9993 in a declarator and we can't have default args. */
9994 parser->default_arg_ok_p = false;
9995 parser->in_declarator_p = true;
9998 /* Parse the parameter-declaration-clause. */
9999 params = cp_parser_parameter_declaration_clause (parser);
10001 /* If all went well, parse the cv-qualifier-seq and the
10002 exception-specfication. */
10003 if (cp_parser_parse_definitely (parser))
10005 tree cv_qualifiers;
10006 tree exception_specification;
10008 first = false;
10009 /* Consume the `)'. */
10010 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10012 /* Parse the cv-qualifier-seq. */
10013 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10014 /* And the exception-specification. */
10015 exception_specification
10016 = cp_parser_exception_specification_opt (parser);
10018 /* Create the function-declarator. */
10019 declarator = make_call_declarator (declarator,
10020 params,
10021 cv_qualifiers,
10022 exception_specification);
10023 /* Any subsequent parameter lists are to do with
10024 return type, so are not those of the declared
10025 function. */
10026 parser->default_arg_ok_p = false;
10028 /* Repeat the main loop. */
10029 continue;
10033 /* If this is the first, we can try a parenthesized
10034 declarator. */
10035 if (first)
10037 parser->default_arg_ok_p = saved_default_arg_ok_p;
10038 parser->in_declarator_p = saved_in_declarator_p;
10040 /* Consume the `('. */
10041 cp_lexer_consume_token (parser->lexer);
10042 /* Parse the nested declarator. */
10043 declarator
10044 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10045 first = false;
10046 /* Expect a `)'. */
10047 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10048 declarator = error_mark_node;
10049 if (declarator == error_mark_node)
10050 break;
10052 goto handle_declarator;
10054 /* Otherwise, we must be done. */
10055 else
10056 break;
10058 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10059 && token->type == CPP_OPEN_SQUARE)
10061 /* Parse an array-declarator. */
10062 tree bounds;
10064 first = false;
10065 parser->default_arg_ok_p = false;
10066 parser->in_declarator_p = true;
10067 /* Consume the `['. */
10068 cp_lexer_consume_token (parser->lexer);
10069 /* Peek at the next token. */
10070 token = cp_lexer_peek_token (parser->lexer);
10071 /* If the next token is `]', then there is no
10072 constant-expression. */
10073 if (token->type != CPP_CLOSE_SQUARE)
10075 bool non_constant_p;
10077 bounds
10078 = cp_parser_constant_expression (parser,
10079 /*allow_non_constant=*/true,
10080 &non_constant_p);
10081 /* If we're in a template, but the constant-expression
10082 isn't value dependent, simplify it. We're supposed
10083 to treat:
10085 template <typename T> void f(T[1 + 1]);
10086 template <typename T> void f(T[2]);
10088 as two declarations of the same function, for
10089 example. */
10090 if (processing_template_decl
10091 && !non_constant_p
10092 && !value_dependent_expression_p (bounds))
10094 HOST_WIDE_INT saved_processing_template_decl;
10096 saved_processing_template_decl = processing_template_decl;
10097 processing_template_decl = 0;
10098 bounds = build_expr_from_tree (bounds);
10099 processing_template_decl = saved_processing_template_decl;
10102 else
10103 bounds = NULL_TREE;
10104 /* Look for the closing `]'. */
10105 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10107 declarator = error_mark_node;
10108 break;
10111 declarator = build_nt (ARRAY_REF, declarator, bounds);
10113 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10115 /* Parse a declarator_id */
10116 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10117 cp_parser_parse_tentatively (parser);
10118 declarator = cp_parser_declarator_id (parser);
10119 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10121 if (!cp_parser_parse_definitely (parser))
10122 declarator = error_mark_node;
10123 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10125 cp_parser_error (parser, "expected unqualified-id");
10126 declarator = error_mark_node;
10130 if (declarator == error_mark_node)
10131 break;
10133 if (TREE_CODE (declarator) == SCOPE_REF)
10135 tree scope = TREE_OPERAND (declarator, 0);
10137 /* In the declaration of a member of a template class
10138 outside of the class itself, the SCOPE will sometimes
10139 be a TYPENAME_TYPE. For example, given:
10141 template <typename T>
10142 int S<T>::R::i = 3;
10144 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10145 this context, we must resolve S<T>::R to an ordinary
10146 type, rather than a typename type.
10148 The reason we normally avoid resolving TYPENAME_TYPEs
10149 is that a specialization of `S' might render
10150 `S<T>::R' not a type. However, if `S' is
10151 specialized, then this `i' will not be used, so there
10152 is no harm in resolving the types here. */
10153 if (TREE_CODE (scope) == TYPENAME_TYPE)
10155 tree type;
10157 /* Resolve the TYPENAME_TYPE. */
10158 type = resolve_typename_type (scope,
10159 /*only_current_p=*/false);
10160 /* If that failed, the declarator is invalid. */
10161 if (type != error_mark_node)
10162 scope = type;
10163 /* Build a new DECLARATOR. */
10164 declarator = build_nt (SCOPE_REF,
10165 scope,
10166 TREE_OPERAND (declarator, 1));
10170 /* Check to see whether the declarator-id names a constructor,
10171 destructor, or conversion. */
10172 if (declarator && ctor_dtor_or_conv_p
10173 && ((TREE_CODE (declarator) == SCOPE_REF
10174 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10175 || (TREE_CODE (declarator) != SCOPE_REF
10176 && at_class_scope_p ())))
10178 tree unqualified_name;
10179 tree class_type;
10181 /* Get the unqualified part of the name. */
10182 if (TREE_CODE (declarator) == SCOPE_REF)
10184 class_type = TREE_OPERAND (declarator, 0);
10185 unqualified_name = TREE_OPERAND (declarator, 1);
10187 else
10189 class_type = current_class_type;
10190 unqualified_name = declarator;
10193 /* See if it names ctor, dtor or conv. */
10194 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10195 || IDENTIFIER_TYPENAME_P (unqualified_name)
10196 || constructor_name_p (unqualified_name, class_type))
10197 *ctor_dtor_or_conv_p = true;
10200 handle_declarator:;
10201 scope = get_scope_of_declarator (declarator);
10202 if (scope)
10203 /* Any names that appear after the declarator-id for a member
10204 are looked up in the containing scope. */
10205 push_scope (scope);
10206 parser->in_declarator_p = true;
10207 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10208 || (declarator
10209 && (TREE_CODE (declarator) == SCOPE_REF
10210 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10211 /* Default args are only allowed on function
10212 declarations. */
10213 parser->default_arg_ok_p = saved_default_arg_ok_p;
10214 else
10215 parser->default_arg_ok_p = false;
10217 first = false;
10219 /* We're done. */
10220 else
10221 break;
10224 /* For an abstract declarator, we might wind up with nothing at this
10225 point. That's an error; the declarator is not optional. */
10226 if (!declarator)
10227 cp_parser_error (parser, "expected declarator");
10229 /* If we entered a scope, we must exit it now. */
10230 if (scope)
10231 pop_scope (scope);
10233 parser->default_arg_ok_p = saved_default_arg_ok_p;
10234 parser->in_declarator_p = saved_in_declarator_p;
10236 return declarator;
10239 /* Parse a ptr-operator.
10241 ptr-operator:
10242 * cv-qualifier-seq [opt]
10244 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10246 GNU Extension:
10248 ptr-operator:
10249 & cv-qualifier-seq [opt]
10251 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10252 used. Returns ADDR_EXPR if a reference was used. In the
10253 case of a pointer-to-member, *TYPE is filled in with the
10254 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10255 with the cv-qualifier-seq, or NULL_TREE, if there are no
10256 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10258 static enum tree_code
10259 cp_parser_ptr_operator (cp_parser* parser,
10260 tree* type,
10261 tree* cv_qualifier_seq)
10263 enum tree_code code = ERROR_MARK;
10264 cp_token *token;
10266 /* Assume that it's not a pointer-to-member. */
10267 *type = NULL_TREE;
10268 /* And that there are no cv-qualifiers. */
10269 *cv_qualifier_seq = NULL_TREE;
10271 /* Peek at the next token. */
10272 token = cp_lexer_peek_token (parser->lexer);
10273 /* If it's a `*' or `&' we have a pointer or reference. */
10274 if (token->type == CPP_MULT || token->type == CPP_AND)
10276 /* Remember which ptr-operator we were processing. */
10277 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10279 /* Consume the `*' or `&'. */
10280 cp_lexer_consume_token (parser->lexer);
10282 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10283 `&', if we are allowing GNU extensions. (The only qualifier
10284 that can legally appear after `&' is `restrict', but that is
10285 enforced during semantic analysis. */
10286 if (code == INDIRECT_REF
10287 || cp_parser_allow_gnu_extensions_p (parser))
10288 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10290 else
10292 /* Try the pointer-to-member case. */
10293 cp_parser_parse_tentatively (parser);
10294 /* Look for the optional `::' operator. */
10295 cp_parser_global_scope_opt (parser,
10296 /*current_scope_valid_p=*/false);
10297 /* Look for the nested-name specifier. */
10298 cp_parser_nested_name_specifier (parser,
10299 /*typename_keyword_p=*/false,
10300 /*check_dependency_p=*/true,
10301 /*type_p=*/false);
10302 /* If we found it, and the next token is a `*', then we are
10303 indeed looking at a pointer-to-member operator. */
10304 if (!cp_parser_error_occurred (parser)
10305 && cp_parser_require (parser, CPP_MULT, "`*'"))
10307 /* The type of which the member is a member is given by the
10308 current SCOPE. */
10309 *type = parser->scope;
10310 /* The next name will not be qualified. */
10311 parser->scope = NULL_TREE;
10312 parser->qualifying_scope = NULL_TREE;
10313 parser->object_scope = NULL_TREE;
10314 /* Indicate that the `*' operator was used. */
10315 code = INDIRECT_REF;
10316 /* Look for the optional cv-qualifier-seq. */
10317 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10319 /* If that didn't work we don't have a ptr-operator. */
10320 if (!cp_parser_parse_definitely (parser))
10321 cp_parser_error (parser, "expected ptr-operator");
10324 return code;
10327 /* Parse an (optional) cv-qualifier-seq.
10329 cv-qualifier-seq:
10330 cv-qualifier cv-qualifier-seq [opt]
10332 Returns a TREE_LIST. The TREE_VALUE of each node is the
10333 representation of a cv-qualifier. */
10335 static tree
10336 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10338 tree cv_qualifiers = NULL_TREE;
10340 while (true)
10342 tree cv_qualifier;
10344 /* Look for the next cv-qualifier. */
10345 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10346 /* If we didn't find one, we're done. */
10347 if (!cv_qualifier)
10348 break;
10350 /* Add this cv-qualifier to the list. */
10351 cv_qualifiers
10352 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10355 /* We built up the list in reverse order. */
10356 return nreverse (cv_qualifiers);
10359 /* Parse an (optional) cv-qualifier.
10361 cv-qualifier:
10362 const
10363 volatile
10365 GNU Extension:
10367 cv-qualifier:
10368 __restrict__ */
10370 static tree
10371 cp_parser_cv_qualifier_opt (cp_parser* parser)
10373 cp_token *token;
10374 tree cv_qualifier = NULL_TREE;
10376 /* Peek at the next token. */
10377 token = cp_lexer_peek_token (parser->lexer);
10378 /* See if it's a cv-qualifier. */
10379 switch (token->keyword)
10381 case RID_CONST:
10382 case RID_VOLATILE:
10383 case RID_RESTRICT:
10384 /* Save the value of the token. */
10385 cv_qualifier = token->value;
10386 /* Consume the token. */
10387 cp_lexer_consume_token (parser->lexer);
10388 break;
10390 default:
10391 break;
10394 return cv_qualifier;
10397 /* Parse a declarator-id.
10399 declarator-id:
10400 id-expression
10401 :: [opt] nested-name-specifier [opt] type-name
10403 In the `id-expression' case, the value returned is as for
10404 cp_parser_id_expression if the id-expression was an unqualified-id.
10405 If the id-expression was a qualified-id, then a SCOPE_REF is
10406 returned. The first operand is the scope (either a NAMESPACE_DECL
10407 or TREE_TYPE), but the second is still just a representation of an
10408 unqualified-id. */
10410 static tree
10411 cp_parser_declarator_id (cp_parser* parser)
10413 tree id_expression;
10415 /* The expression must be an id-expression. Assume that qualified
10416 names are the names of types so that:
10418 template <class T>
10419 int S<T>::R::i = 3;
10421 will work; we must treat `S<T>::R' as the name of a type.
10422 Similarly, assume that qualified names are templates, where
10423 required, so that:
10425 template <class T>
10426 int S<T>::R<T>::i = 3;
10428 will work, too. */
10429 id_expression = cp_parser_id_expression (parser,
10430 /*template_keyword_p=*/false,
10431 /*check_dependency_p=*/false,
10432 /*template_p=*/NULL);
10433 /* If the name was qualified, create a SCOPE_REF to represent
10434 that. */
10435 if (parser->scope)
10437 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10438 parser->scope = NULL_TREE;
10441 return id_expression;
10444 /* Parse a type-id.
10446 type-id:
10447 type-specifier-seq abstract-declarator [opt]
10449 Returns the TYPE specified. */
10451 static tree
10452 cp_parser_type_id (cp_parser* parser)
10454 tree type_specifier_seq;
10455 tree abstract_declarator;
10457 /* Parse the type-specifier-seq. */
10458 type_specifier_seq
10459 = cp_parser_type_specifier_seq (parser);
10460 if (type_specifier_seq == error_mark_node)
10461 return error_mark_node;
10463 /* There might or might not be an abstract declarator. */
10464 cp_parser_parse_tentatively (parser);
10465 /* Look for the declarator. */
10466 abstract_declarator
10467 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10468 /* Check to see if there really was a declarator. */
10469 if (!cp_parser_parse_definitely (parser))
10470 abstract_declarator = NULL_TREE;
10472 return groktypename (build_tree_list (type_specifier_seq,
10473 abstract_declarator));
10476 /* Parse a type-specifier-seq.
10478 type-specifier-seq:
10479 type-specifier type-specifier-seq [opt]
10481 GNU extension:
10483 type-specifier-seq:
10484 attributes type-specifier-seq [opt]
10486 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10487 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10489 static tree
10490 cp_parser_type_specifier_seq (cp_parser* parser)
10492 bool seen_type_specifier = false;
10493 tree type_specifier_seq = NULL_TREE;
10495 /* Parse the type-specifiers and attributes. */
10496 while (true)
10498 tree type_specifier;
10500 /* Check for attributes first. */
10501 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10503 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10504 NULL_TREE,
10505 type_specifier_seq);
10506 continue;
10509 /* After the first type-specifier, others are optional. */
10510 if (seen_type_specifier)
10511 cp_parser_parse_tentatively (parser);
10512 /* Look for the type-specifier. */
10513 type_specifier = cp_parser_type_specifier (parser,
10514 CP_PARSER_FLAGS_NONE,
10515 /*is_friend=*/false,
10516 /*is_declaration=*/false,
10517 NULL,
10518 NULL);
10519 /* If the first type-specifier could not be found, this is not a
10520 type-specifier-seq at all. */
10521 if (!seen_type_specifier && type_specifier == error_mark_node)
10522 return error_mark_node;
10523 /* If subsequent type-specifiers could not be found, the
10524 type-specifier-seq is complete. */
10525 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10526 break;
10528 /* Add the new type-specifier to the list. */
10529 type_specifier_seq
10530 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10531 seen_type_specifier = true;
10534 /* We built up the list in reverse order. */
10535 return nreverse (type_specifier_seq);
10538 /* Parse a parameter-declaration-clause.
10540 parameter-declaration-clause:
10541 parameter-declaration-list [opt] ... [opt]
10542 parameter-declaration-list , ...
10544 Returns a representation for the parameter declarations. Each node
10545 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10546 representation.) If the parameter-declaration-clause ends with an
10547 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10548 list. A return value of NULL_TREE indicates a
10549 parameter-declaration-clause consisting only of an ellipsis. */
10551 static tree
10552 cp_parser_parameter_declaration_clause (cp_parser* parser)
10554 tree parameters;
10555 cp_token *token;
10556 bool ellipsis_p;
10558 /* Peek at the next token. */
10559 token = cp_lexer_peek_token (parser->lexer);
10560 /* Check for trivial parameter-declaration-clauses. */
10561 if (token->type == CPP_ELLIPSIS)
10563 /* Consume the `...' token. */
10564 cp_lexer_consume_token (parser->lexer);
10565 return NULL_TREE;
10567 else if (token->type == CPP_CLOSE_PAREN)
10568 /* There are no parameters. */
10570 #ifndef NO_IMPLICIT_EXTERN_C
10571 if (in_system_header && current_class_type == NULL
10572 && current_lang_name == lang_name_c)
10573 return NULL_TREE;
10574 else
10575 #endif
10576 return void_list_node;
10578 /* Check for `(void)', too, which is a special case. */
10579 else if (token->keyword == RID_VOID
10580 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10581 == CPP_CLOSE_PAREN))
10583 /* Consume the `void' token. */
10584 cp_lexer_consume_token (parser->lexer);
10585 /* There are no parameters. */
10586 return void_list_node;
10589 /* Parse the parameter-declaration-list. */
10590 parameters = cp_parser_parameter_declaration_list (parser);
10591 /* If a parse error occurred while parsing the
10592 parameter-declaration-list, then the entire
10593 parameter-declaration-clause is erroneous. */
10594 if (parameters == error_mark_node)
10595 return error_mark_node;
10597 /* Peek at the next token. */
10598 token = cp_lexer_peek_token (parser->lexer);
10599 /* If it's a `,', the clause should terminate with an ellipsis. */
10600 if (token->type == CPP_COMMA)
10602 /* Consume the `,'. */
10603 cp_lexer_consume_token (parser->lexer);
10604 /* Expect an ellipsis. */
10605 ellipsis_p
10606 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10608 /* It might also be `...' if the optional trailing `,' was
10609 omitted. */
10610 else if (token->type == CPP_ELLIPSIS)
10612 /* Consume the `...' token. */
10613 cp_lexer_consume_token (parser->lexer);
10614 /* And remember that we saw it. */
10615 ellipsis_p = true;
10617 else
10618 ellipsis_p = false;
10620 /* Finish the parameter list. */
10621 return finish_parmlist (parameters, ellipsis_p);
10624 /* Parse a parameter-declaration-list.
10626 parameter-declaration-list:
10627 parameter-declaration
10628 parameter-declaration-list , parameter-declaration
10630 Returns a representation of the parameter-declaration-list, as for
10631 cp_parser_parameter_declaration_clause. However, the
10632 `void_list_node' is never appended to the list. */
10634 static tree
10635 cp_parser_parameter_declaration_list (cp_parser* parser)
10637 tree parameters = NULL_TREE;
10639 /* Look for more parameters. */
10640 while (true)
10642 tree parameter;
10643 /* Parse the parameter. */
10644 parameter
10645 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10647 /* If a parse error ocurred parsing the parameter declaration,
10648 then the entire parameter-declaration-list is erroneous. */
10649 if (parameter == error_mark_node)
10651 parameters = error_mark_node;
10652 break;
10654 /* Add the new parameter to the list. */
10655 TREE_CHAIN (parameter) = parameters;
10656 parameters = parameter;
10658 /* Peek at the next token. */
10659 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10660 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10661 /* The parameter-declaration-list is complete. */
10662 break;
10663 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10665 cp_token *token;
10667 /* Peek at the next token. */
10668 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10669 /* If it's an ellipsis, then the list is complete. */
10670 if (token->type == CPP_ELLIPSIS)
10671 break;
10672 /* Otherwise, there must be more parameters. Consume the
10673 `,'. */
10674 cp_lexer_consume_token (parser->lexer);
10676 else
10678 cp_parser_error (parser, "expected `,' or `...'");
10679 break;
10683 /* We built up the list in reverse order; straighten it out now. */
10684 return nreverse (parameters);
10687 /* Parse a parameter declaration.
10689 parameter-declaration:
10690 decl-specifier-seq declarator
10691 decl-specifier-seq declarator = assignment-expression
10692 decl-specifier-seq abstract-declarator [opt]
10693 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10695 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10696 declares a template parameter. (In that case, a non-nested `>'
10697 token encountered during the parsing of the assignment-expression
10698 is not interpreted as a greater-than operator.)
10700 Returns a TREE_LIST representing the parameter-declaration. The
10701 TREE_VALUE is a representation of the decl-specifier-seq and
10702 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10703 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10704 TREE_VALUE represents the declarator. */
10706 static tree
10707 cp_parser_parameter_declaration (cp_parser *parser,
10708 bool template_parm_p)
10710 bool declares_class_or_enum;
10711 bool greater_than_is_operator_p;
10712 tree decl_specifiers;
10713 tree attributes;
10714 tree declarator;
10715 tree default_argument;
10716 tree parameter;
10717 cp_token *token;
10718 const char *saved_message;
10720 /* In a template parameter, `>' is not an operator.
10722 [temp.param]
10724 When parsing a default template-argument for a non-type
10725 template-parameter, the first non-nested `>' is taken as the end
10726 of the template parameter-list rather than a greater-than
10727 operator. */
10728 greater_than_is_operator_p = !template_parm_p;
10730 /* Type definitions may not appear in parameter types. */
10731 saved_message = parser->type_definition_forbidden_message;
10732 parser->type_definition_forbidden_message
10733 = "types may not be defined in parameter types";
10735 /* Parse the declaration-specifiers. */
10736 decl_specifiers
10737 = cp_parser_decl_specifier_seq (parser,
10738 CP_PARSER_FLAGS_NONE,
10739 &attributes,
10740 &declares_class_or_enum);
10741 /* If an error occurred, there's no reason to attempt to parse the
10742 rest of the declaration. */
10743 if (cp_parser_error_occurred (parser))
10745 parser->type_definition_forbidden_message = saved_message;
10746 return error_mark_node;
10749 /* Peek at the next token. */
10750 token = cp_lexer_peek_token (parser->lexer);
10751 /* If the next token is a `)', `,', `=', `>', or `...', then there
10752 is no declarator. */
10753 if (token->type == CPP_CLOSE_PAREN
10754 || token->type == CPP_COMMA
10755 || token->type == CPP_EQ
10756 || token->type == CPP_ELLIPSIS
10757 || token->type == CPP_GREATER)
10758 declarator = NULL_TREE;
10759 /* Otherwise, there should be a declarator. */
10760 else
10762 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10763 parser->default_arg_ok_p = false;
10765 declarator = cp_parser_declarator (parser,
10766 CP_PARSER_DECLARATOR_EITHER,
10767 /*ctor_dtor_or_conv_p=*/NULL);
10768 parser->default_arg_ok_p = saved_default_arg_ok_p;
10769 /* After the declarator, allow more attributes. */
10770 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10773 /* The restriction on defining new types applies only to the type
10774 of the parameter, not to the default argument. */
10775 parser->type_definition_forbidden_message = saved_message;
10777 /* If the next token is `=', then process a default argument. */
10778 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10780 bool saved_greater_than_is_operator_p;
10781 /* Consume the `='. */
10782 cp_lexer_consume_token (parser->lexer);
10784 /* If we are defining a class, then the tokens that make up the
10785 default argument must be saved and processed later. */
10786 if (!template_parm_p && at_class_scope_p ()
10787 && TYPE_BEING_DEFINED (current_class_type))
10789 unsigned depth = 0;
10791 /* Create a DEFAULT_ARG to represented the unparsed default
10792 argument. */
10793 default_argument = make_node (DEFAULT_ARG);
10794 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10796 /* Add tokens until we have processed the entire default
10797 argument. */
10798 while (true)
10800 bool done = false;
10801 cp_token *token;
10803 /* Peek at the next token. */
10804 token = cp_lexer_peek_token (parser->lexer);
10805 /* What we do depends on what token we have. */
10806 switch (token->type)
10808 /* In valid code, a default argument must be
10809 immediately followed by a `,' `)', or `...'. */
10810 case CPP_COMMA:
10811 case CPP_CLOSE_PAREN:
10812 case CPP_ELLIPSIS:
10813 /* If we run into a non-nested `;', `}', or `]',
10814 then the code is invalid -- but the default
10815 argument is certainly over. */
10816 case CPP_SEMICOLON:
10817 case CPP_CLOSE_BRACE:
10818 case CPP_CLOSE_SQUARE:
10819 if (depth == 0)
10820 done = true;
10821 /* Update DEPTH, if necessary. */
10822 else if (token->type == CPP_CLOSE_PAREN
10823 || token->type == CPP_CLOSE_BRACE
10824 || token->type == CPP_CLOSE_SQUARE)
10825 --depth;
10826 break;
10828 case CPP_OPEN_PAREN:
10829 case CPP_OPEN_SQUARE:
10830 case CPP_OPEN_BRACE:
10831 ++depth;
10832 break;
10834 case CPP_GREATER:
10835 /* If we see a non-nested `>', and `>' is not an
10836 operator, then it marks the end of the default
10837 argument. */
10838 if (!depth && !greater_than_is_operator_p)
10839 done = true;
10840 break;
10842 /* If we run out of tokens, issue an error message. */
10843 case CPP_EOF:
10844 error ("file ends in default argument");
10845 done = true;
10846 break;
10848 case CPP_NAME:
10849 case CPP_SCOPE:
10850 /* In these cases, we should look for template-ids.
10851 For example, if the default argument is
10852 `X<int, double>()', we need to do name lookup to
10853 figure out whether or not `X' is a template; if
10854 so, the `,' does not end the deault argument.
10856 That is not yet done. */
10857 break;
10859 default:
10860 break;
10863 /* If we've reached the end, stop. */
10864 if (done)
10865 break;
10867 /* Add the token to the token block. */
10868 token = cp_lexer_consume_token (parser->lexer);
10869 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10870 token);
10873 /* Outside of a class definition, we can just parse the
10874 assignment-expression. */
10875 else
10877 bool saved_local_variables_forbidden_p;
10879 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10880 set correctly. */
10881 saved_greater_than_is_operator_p
10882 = parser->greater_than_is_operator_p;
10883 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10884 /* Local variable names (and the `this' keyword) may not
10885 appear in a default argument. */
10886 saved_local_variables_forbidden_p
10887 = parser->local_variables_forbidden_p;
10888 parser->local_variables_forbidden_p = true;
10889 /* Parse the assignment-expression. */
10890 default_argument = cp_parser_assignment_expression (parser);
10891 /* Restore saved state. */
10892 parser->greater_than_is_operator_p
10893 = saved_greater_than_is_operator_p;
10894 parser->local_variables_forbidden_p
10895 = saved_local_variables_forbidden_p;
10897 if (!parser->default_arg_ok_p)
10899 pedwarn ("default arguments are only permitted on functions");
10900 if (flag_pedantic_errors)
10901 default_argument = NULL_TREE;
10904 else
10905 default_argument = NULL_TREE;
10907 /* Create the representation of the parameter. */
10908 if (attributes)
10909 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10910 parameter = build_tree_list (default_argument,
10911 build_tree_list (decl_specifiers,
10912 declarator));
10914 return parameter;
10917 /* Parse a function-definition.
10919 function-definition:
10920 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10921 function-body
10922 decl-specifier-seq [opt] declarator function-try-block
10924 GNU Extension:
10926 function-definition:
10927 __extension__ function-definition
10929 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10930 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10931 be a `friend'. */
10933 static tree
10934 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10936 tree decl_specifiers;
10937 tree attributes;
10938 tree declarator;
10939 tree fn;
10940 cp_token *token;
10941 bool declares_class_or_enum;
10942 bool member_p;
10943 /* The saved value of the PEDANTIC flag. */
10944 int saved_pedantic;
10946 /* Any pending qualification must be cleared by our caller. It is
10947 more robust to force the callers to clear PARSER->SCOPE than to
10948 do it here since if the qualification is in effect here, it might
10949 also end up in effect elsewhere that it is not intended. */
10950 my_friendly_assert (!parser->scope, 20010821);
10952 /* Handle `__extension__'. */
10953 if (cp_parser_extension_opt (parser, &saved_pedantic))
10955 /* Parse the function-definition. */
10956 fn = cp_parser_function_definition (parser, friend_p);
10957 /* Restore the PEDANTIC flag. */
10958 pedantic = saved_pedantic;
10960 return fn;
10963 /* Check to see if this definition appears in a class-specifier. */
10964 member_p = (at_class_scope_p ()
10965 && TYPE_BEING_DEFINED (current_class_type));
10966 /* Defer access checks in the decl-specifier-seq until we know what
10967 function is being defined. There is no need to do this for the
10968 definition of member functions; we cannot be defining a member
10969 from another class. */
10970 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10972 /* Parse the decl-specifier-seq. */
10973 decl_specifiers
10974 = cp_parser_decl_specifier_seq (parser,
10975 CP_PARSER_FLAGS_OPTIONAL,
10976 &attributes,
10977 &declares_class_or_enum);
10978 /* Figure out whether this declaration is a `friend'. */
10979 if (friend_p)
10980 *friend_p = cp_parser_friend_p (decl_specifiers);
10982 /* Parse the declarator. */
10983 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10984 /*ctor_dtor_or_conv_p=*/NULL);
10986 /* Gather up any access checks that occurred. */
10987 stop_deferring_access_checks ();
10989 /* If something has already gone wrong, we may as well stop now. */
10990 if (declarator == error_mark_node)
10992 /* Skip to the end of the function, or if this wasn't anything
10993 like a function-definition, to a `;' in the hopes of finding
10994 a sensible place from which to continue parsing. */
10995 cp_parser_skip_to_end_of_block_or_statement (parser);
10996 pop_deferring_access_checks ();
10997 return error_mark_node;
11000 /* The next character should be a `{' (for a simple function
11001 definition), a `:' (for a ctor-initializer), or `try' (for a
11002 function-try block). */
11003 token = cp_lexer_peek_token (parser->lexer);
11004 if (!cp_parser_token_starts_function_definition_p (token))
11006 /* Issue the error-message. */
11007 cp_parser_error (parser, "expected function-definition");
11008 /* Skip to the next `;'. */
11009 cp_parser_skip_to_end_of_block_or_statement (parser);
11011 pop_deferring_access_checks ();
11012 return error_mark_node;
11015 /* If we are in a class scope, then we must handle
11016 function-definitions specially. In particular, we save away the
11017 tokens that make up the function body, and parse them again
11018 later, in order to handle code like:
11020 struct S {
11021 int f () { return i; }
11022 int i;
11025 Here, we cannot parse the body of `f' until after we have seen
11026 the declaration of `i'. */
11027 if (member_p)
11029 cp_token_cache *cache;
11031 /* Create the function-declaration. */
11032 fn = start_method (decl_specifiers, declarator, attributes);
11033 /* If something went badly wrong, bail out now. */
11034 if (fn == error_mark_node)
11036 /* If there's a function-body, skip it. */
11037 if (cp_parser_token_starts_function_definition_p
11038 (cp_lexer_peek_token (parser->lexer)))
11039 cp_parser_skip_to_end_of_block_or_statement (parser);
11040 pop_deferring_access_checks ();
11041 return error_mark_node;
11044 /* Remember it, if there default args to post process. */
11045 cp_parser_save_default_args (parser, fn);
11047 /* Create a token cache. */
11048 cache = cp_token_cache_new ();
11049 /* Save away the tokens that make up the body of the
11050 function. */
11051 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11052 /* Handle function try blocks. */
11053 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11054 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11056 /* Save away the inline definition; we will process it when the
11057 class is complete. */
11058 DECL_PENDING_INLINE_INFO (fn) = cache;
11059 DECL_PENDING_INLINE_P (fn) = 1;
11061 /* We need to know that this was defined in the class, so that
11062 friend templates are handled correctly. */
11063 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
11065 /* We're done with the inline definition. */
11066 finish_method (fn);
11068 /* Add FN to the queue of functions to be parsed later. */
11069 TREE_VALUE (parser->unparsed_functions_queues)
11070 = tree_cons (NULL_TREE, fn,
11071 TREE_VALUE (parser->unparsed_functions_queues));
11073 pop_deferring_access_checks ();
11074 return fn;
11077 /* Check that the number of template-parameter-lists is OK. */
11078 if (!cp_parser_check_declarator_template_parameters (parser,
11079 declarator))
11081 cp_parser_skip_to_end_of_block_or_statement (parser);
11082 pop_deferring_access_checks ();
11083 return error_mark_node;
11086 fn = cp_parser_function_definition_from_specifiers_and_declarator
11087 (parser, decl_specifiers, attributes, declarator);
11088 pop_deferring_access_checks ();
11089 return fn;
11092 /* Parse a function-body.
11094 function-body:
11095 compound_statement */
11097 static void
11098 cp_parser_function_body (cp_parser *parser)
11100 cp_parser_compound_statement (parser);
11103 /* Parse a ctor-initializer-opt followed by a function-body. Return
11104 true if a ctor-initializer was present. */
11106 static bool
11107 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11109 tree body;
11110 bool ctor_initializer_p;
11112 /* Begin the function body. */
11113 body = begin_function_body ();
11114 /* Parse the optional ctor-initializer. */
11115 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11116 /* Parse the function-body. */
11117 cp_parser_function_body (parser);
11118 /* Finish the function body. */
11119 finish_function_body (body);
11121 return ctor_initializer_p;
11124 /* Parse an initializer.
11126 initializer:
11127 = initializer-clause
11128 ( expression-list )
11130 Returns a expression representing the initializer. If no
11131 initializer is present, NULL_TREE is returned.
11133 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11134 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11135 set to FALSE if there is no initializer present. */
11137 static tree
11138 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init)
11140 cp_token *token;
11141 tree init;
11143 /* Peek at the next token. */
11144 token = cp_lexer_peek_token (parser->lexer);
11146 /* Let our caller know whether or not this initializer was
11147 parenthesized. */
11148 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11150 if (token->type == CPP_EQ)
11152 /* Consume the `='. */
11153 cp_lexer_consume_token (parser->lexer);
11154 /* Parse the initializer-clause. */
11155 init = cp_parser_initializer_clause (parser);
11157 else if (token->type == CPP_OPEN_PAREN)
11159 /* Consume the `('. */
11160 cp_lexer_consume_token (parser->lexer);
11161 /* Parse the expression-list. */
11162 init = cp_parser_expression_list (parser);
11163 /* Consume the `)' token. */
11164 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11165 cp_parser_skip_to_closing_parenthesis (parser);
11167 else
11169 /* Anything else is an error. */
11170 cp_parser_error (parser, "expected initializer");
11171 init = error_mark_node;
11174 return init;
11177 /* Parse an initializer-clause.
11179 initializer-clause:
11180 assignment-expression
11181 { initializer-list , [opt] }
11184 Returns an expression representing the initializer.
11186 If the `assignment-expression' production is used the value
11187 returned is simply a reprsentation for the expression.
11189 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11190 the elements of the initializer-list (or NULL_TREE, if the last
11191 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11192 NULL_TREE. There is no way to detect whether or not the optional
11193 trailing `,' was provided. */
11195 static tree
11196 cp_parser_initializer_clause (cp_parser* parser)
11198 tree initializer;
11200 /* If it is not a `{', then we are looking at an
11201 assignment-expression. */
11202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11203 initializer = cp_parser_assignment_expression (parser);
11204 else
11206 /* Consume the `{' token. */
11207 cp_lexer_consume_token (parser->lexer);
11208 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11209 initializer = make_node (CONSTRUCTOR);
11210 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11211 necessary, but check_initializer depends upon it, for
11212 now. */
11213 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11214 /* If it's not a `}', then there is a non-trivial initializer. */
11215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11217 /* Parse the initializer list. */
11218 CONSTRUCTOR_ELTS (initializer)
11219 = cp_parser_initializer_list (parser);
11220 /* A trailing `,' token is allowed. */
11221 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11222 cp_lexer_consume_token (parser->lexer);
11225 /* Now, there should be a trailing `}'. */
11226 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11229 return initializer;
11232 /* Parse an initializer-list.
11234 initializer-list:
11235 initializer-clause
11236 initializer-list , initializer-clause
11238 GNU Extension:
11240 initializer-list:
11241 identifier : initializer-clause
11242 initializer-list, identifier : initializer-clause
11244 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11245 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11246 IDENTIFIER_NODE naming the field to initialize. */
11248 static tree
11249 cp_parser_initializer_list (cp_parser* parser)
11251 tree initializers = NULL_TREE;
11253 /* Parse the rest of the list. */
11254 while (true)
11256 cp_token *token;
11257 tree identifier;
11258 tree initializer;
11260 /* If the next token is an identifier and the following one is a
11261 colon, we are looking at the GNU designated-initializer
11262 syntax. */
11263 if (cp_parser_allow_gnu_extensions_p (parser)
11264 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11265 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11267 /* Consume the identifier. */
11268 identifier = cp_lexer_consume_token (parser->lexer)->value;
11269 /* Consume the `:'. */
11270 cp_lexer_consume_token (parser->lexer);
11272 else
11273 identifier = NULL_TREE;
11275 /* Parse the initializer. */
11276 initializer = cp_parser_initializer_clause (parser);
11278 /* Add it to the list. */
11279 initializers = tree_cons (identifier, initializer, initializers);
11281 /* If the next token is not a comma, we have reached the end of
11282 the list. */
11283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11284 break;
11286 /* Peek at the next token. */
11287 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11288 /* If the next token is a `}', then we're still done. An
11289 initializer-clause can have a trailing `,' after the
11290 initializer-list and before the closing `}'. */
11291 if (token->type == CPP_CLOSE_BRACE)
11292 break;
11294 /* Consume the `,' token. */
11295 cp_lexer_consume_token (parser->lexer);
11298 /* The initializers were built up in reverse order, so we need to
11299 reverse them now. */
11300 return nreverse (initializers);
11303 /* Classes [gram.class] */
11305 /* Parse a class-name.
11307 class-name:
11308 identifier
11309 template-id
11311 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11312 to indicate that names looked up in dependent types should be
11313 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11314 keyword has been used to indicate that the name that appears next
11315 is a template. TYPE_P is true iff the next name should be treated
11316 as class-name, even if it is declared to be some other kind of name
11317 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11318 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11319 being defined in a class-head.
11321 Returns the TYPE_DECL representing the class. */
11323 static tree
11324 cp_parser_class_name (cp_parser *parser,
11325 bool typename_keyword_p,
11326 bool template_keyword_p,
11327 bool type_p,
11328 bool check_dependency_p,
11329 bool class_head_p)
11331 tree decl;
11332 tree scope;
11333 bool typename_p;
11334 cp_token *token;
11336 /* All class-names start with an identifier. */
11337 token = cp_lexer_peek_token (parser->lexer);
11338 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11340 cp_parser_error (parser, "expected class-name");
11341 return error_mark_node;
11344 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11345 to a template-id, so we save it here. */
11346 scope = parser->scope;
11347 /* Any name names a type if we're following the `typename' keyword
11348 in a qualified name where the enclosing scope is type-dependent. */
11349 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11350 && dependent_type_p (scope));
11351 /* Handle the common case (an identifier, but not a template-id)
11352 efficiently. */
11353 if (token->type == CPP_NAME
11354 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11356 tree identifier;
11358 /* Look for the identifier. */
11359 identifier = cp_parser_identifier (parser);
11360 /* If the next token isn't an identifier, we are certainly not
11361 looking at a class-name. */
11362 if (identifier == error_mark_node)
11363 decl = error_mark_node;
11364 /* If we know this is a type-name, there's no need to look it
11365 up. */
11366 else if (typename_p)
11367 decl = identifier;
11368 else
11370 /* If the next token is a `::', then the name must be a type
11371 name.
11373 [basic.lookup.qual]
11375 During the lookup for a name preceding the :: scope
11376 resolution operator, object, function, and enumerator
11377 names are ignored. */
11378 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11379 type_p = true;
11380 /* Look up the name. */
11381 decl = cp_parser_lookup_name (parser, identifier,
11382 type_p,
11383 /*is_namespace=*/false,
11384 check_dependency_p);
11387 else
11389 /* Try a template-id. */
11390 decl = cp_parser_template_id (parser, template_keyword_p,
11391 check_dependency_p);
11392 if (decl == error_mark_node)
11393 return error_mark_node;
11396 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11398 /* If this is a typename, create a TYPENAME_TYPE. */
11399 if (typename_p && decl != error_mark_node)
11400 decl = TYPE_NAME (make_typename_type (scope, decl,
11401 /*complain=*/1));
11403 /* Check to see that it is really the name of a class. */
11404 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11405 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11406 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11407 /* Situations like this:
11409 template <typename T> struct A {
11410 typename T::template X<int>::I i;
11413 are problematic. Is `T::template X<int>' a class-name? The
11414 standard does not seem to be definitive, but there is no other
11415 valid interpretation of the following `::'. Therefore, those
11416 names are considered class-names. */
11417 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11418 else if (decl == error_mark_node
11419 || TREE_CODE (decl) != TYPE_DECL
11420 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11422 cp_parser_error (parser, "expected class-name");
11423 return error_mark_node;
11426 return decl;
11429 /* Parse a class-specifier.
11431 class-specifier:
11432 class-head { member-specification [opt] }
11434 Returns the TREE_TYPE representing the class. */
11436 static tree
11437 cp_parser_class_specifier (cp_parser* parser)
11439 cp_token *token;
11440 tree type;
11441 tree attributes = NULL_TREE;
11442 int has_trailing_semicolon;
11443 bool nested_name_specifier_p;
11444 unsigned saved_num_template_parameter_lists;
11446 push_deferring_access_checks (dk_no_deferred);
11448 /* Parse the class-head. */
11449 type = cp_parser_class_head (parser,
11450 &nested_name_specifier_p);
11451 /* If the class-head was a semantic disaster, skip the entire body
11452 of the class. */
11453 if (!type)
11455 cp_parser_skip_to_end_of_block_or_statement (parser);
11456 pop_deferring_access_checks ();
11457 return error_mark_node;
11460 /* Look for the `{'. */
11461 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11463 pop_deferring_access_checks ();
11464 return error_mark_node;
11467 /* Issue an error message if type-definitions are forbidden here. */
11468 cp_parser_check_type_definition (parser);
11469 /* Remember that we are defining one more class. */
11470 ++parser->num_classes_being_defined;
11471 /* Inside the class, surrounding template-parameter-lists do not
11472 apply. */
11473 saved_num_template_parameter_lists
11474 = parser->num_template_parameter_lists;
11475 parser->num_template_parameter_lists = 0;
11477 /* Start the class. */
11478 type = begin_class_definition (type);
11479 if (type == error_mark_node)
11480 /* If the type is erroneous, skip the entire body of the class. */
11481 cp_parser_skip_to_closing_brace (parser);
11482 else
11483 /* Parse the member-specification. */
11484 cp_parser_member_specification_opt (parser);
11485 /* Look for the trailing `}'. */
11486 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11487 /* We get better error messages by noticing a common problem: a
11488 missing trailing `;'. */
11489 token = cp_lexer_peek_token (parser->lexer);
11490 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11491 /* Look for attributes to apply to this class. */
11492 if (cp_parser_allow_gnu_extensions_p (parser))
11493 attributes = cp_parser_attributes_opt (parser);
11494 /* Finish the class definition. */
11495 type = finish_class_definition (type,
11496 attributes,
11497 has_trailing_semicolon,
11498 nested_name_specifier_p);
11499 /* If this class is not itself within the scope of another class,
11500 then we need to parse the bodies of all of the queued function
11501 definitions. Note that the queued functions defined in a class
11502 are not always processed immediately following the
11503 class-specifier for that class. Consider:
11505 struct A {
11506 struct B { void f() { sizeof (A); } };
11509 If `f' were processed before the processing of `A' were
11510 completed, there would be no way to compute the size of `A'.
11511 Note that the nesting we are interested in here is lexical --
11512 not the semantic nesting given by TYPE_CONTEXT. In particular,
11513 for:
11515 struct A { struct B; };
11516 struct A::B { void f() { } };
11518 there is no need to delay the parsing of `A::B::f'. */
11519 if (--parser->num_classes_being_defined == 0)
11521 tree queue_entry;
11522 tree fn;
11524 /* In a first pass, parse default arguments to the functions.
11525 Then, in a second pass, parse the bodies of the functions.
11526 This two-phased approach handles cases like:
11528 struct S {
11529 void f() { g(); }
11530 void g(int i = 3);
11534 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11535 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11536 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11537 TREE_PURPOSE (parser->unparsed_functions_queues)
11538 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11540 fn = TREE_VALUE (queue_entry);
11541 /* Make sure that any template parameters are in scope. */
11542 maybe_begin_member_template_processing (fn);
11543 /* If there are default arguments that have not yet been processed,
11544 take care of them now. */
11545 cp_parser_late_parsing_default_args (parser, fn);
11546 /* Remove any template parameters from the symbol table. */
11547 maybe_end_member_template_processing ();
11549 /* Now parse the body of the functions. */
11550 for (TREE_VALUE (parser->unparsed_functions_queues)
11551 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11552 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11553 TREE_VALUE (parser->unparsed_functions_queues)
11554 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11556 /* Figure out which function we need to process. */
11557 fn = TREE_VALUE (queue_entry);
11559 /* Parse the function. */
11560 cp_parser_late_parsing_for_member (parser, fn);
11565 /* Put back any saved access checks. */
11566 pop_deferring_access_checks ();
11568 /* Restore the count of active template-parameter-lists. */
11569 parser->num_template_parameter_lists
11570 = saved_num_template_parameter_lists;
11572 return type;
11575 /* Parse a class-head.
11577 class-head:
11578 class-key identifier [opt] base-clause [opt]
11579 class-key nested-name-specifier identifier base-clause [opt]
11580 class-key nested-name-specifier [opt] template-id
11581 base-clause [opt]
11583 GNU Extensions:
11584 class-key attributes identifier [opt] base-clause [opt]
11585 class-key attributes nested-name-specifier identifier base-clause [opt]
11586 class-key attributes nested-name-specifier [opt] template-id
11587 base-clause [opt]
11589 Returns the TYPE of the indicated class. Sets
11590 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11591 involving a nested-name-specifier was used, and FALSE otherwise.
11593 Returns NULL_TREE if the class-head is syntactically valid, but
11594 semantically invalid in a way that means we should skip the entire
11595 body of the class. */
11597 static tree
11598 cp_parser_class_head (cp_parser* parser,
11599 bool* nested_name_specifier_p)
11601 cp_token *token;
11602 tree nested_name_specifier;
11603 enum tag_types class_key;
11604 tree id = NULL_TREE;
11605 tree type = NULL_TREE;
11606 tree attributes;
11607 bool template_id_p = false;
11608 bool qualified_p = false;
11609 bool invalid_nested_name_p = false;
11610 unsigned num_templates;
11612 /* Assume no nested-name-specifier will be present. */
11613 *nested_name_specifier_p = false;
11614 /* Assume no template parameter lists will be used in defining the
11615 type. */
11616 num_templates = 0;
11618 /* Look for the class-key. */
11619 class_key = cp_parser_class_key (parser);
11620 if (class_key == none_type)
11621 return error_mark_node;
11623 /* Parse the attributes. */
11624 attributes = cp_parser_attributes_opt (parser);
11626 /* If the next token is `::', that is invalid -- but sometimes
11627 people do try to write:
11629 struct ::S {};
11631 Handle this gracefully by accepting the extra qualifier, and then
11632 issuing an error about it later if this really is a
11633 class-head. If it turns out just to be an elaborated type
11634 specifier, remain silent. */
11635 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11636 qualified_p = true;
11638 push_deferring_access_checks (dk_no_check);
11640 /* Determine the name of the class. Begin by looking for an
11641 optional nested-name-specifier. */
11642 nested_name_specifier
11643 = cp_parser_nested_name_specifier_opt (parser,
11644 /*typename_keyword_p=*/false,
11645 /*check_dependency_p=*/false,
11646 /*type_p=*/false);
11647 /* If there was a nested-name-specifier, then there *must* be an
11648 identifier. */
11649 if (nested_name_specifier)
11651 /* Although the grammar says `identifier', it really means
11652 `class-name' or `template-name'. You are only allowed to
11653 define a class that has already been declared with this
11654 syntax.
11656 The proposed resolution for Core Issue 180 says that whever
11657 you see `class T::X' you should treat `X' as a type-name.
11659 It is OK to define an inaccessible class; for example:
11661 class A { class B; };
11662 class A::B {};
11664 We do not know if we will see a class-name, or a
11665 template-name. We look for a class-name first, in case the
11666 class-name is a template-id; if we looked for the
11667 template-name first we would stop after the template-name. */
11668 cp_parser_parse_tentatively (parser);
11669 type = cp_parser_class_name (parser,
11670 /*typename_keyword_p=*/false,
11671 /*template_keyword_p=*/false,
11672 /*type_p=*/true,
11673 /*check_dependency_p=*/false,
11674 /*class_head_p=*/true);
11675 /* If that didn't work, ignore the nested-name-specifier. */
11676 if (!cp_parser_parse_definitely (parser))
11678 invalid_nested_name_p = true;
11679 id = cp_parser_identifier (parser);
11680 if (id == error_mark_node)
11681 id = NULL_TREE;
11683 /* If we could not find a corresponding TYPE, treat this
11684 declaration like an unqualified declaration. */
11685 if (type == error_mark_node)
11686 nested_name_specifier = NULL_TREE;
11687 /* Otherwise, count the number of templates used in TYPE and its
11688 containing scopes. */
11689 else
11691 tree scope;
11693 for (scope = TREE_TYPE (type);
11694 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11695 scope = (TYPE_P (scope)
11696 ? TYPE_CONTEXT (scope)
11697 : DECL_CONTEXT (scope)))
11698 if (TYPE_P (scope)
11699 && CLASS_TYPE_P (scope)
11700 && CLASSTYPE_TEMPLATE_INFO (scope)
11701 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11702 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11703 ++num_templates;
11706 /* Otherwise, the identifier is optional. */
11707 else
11709 /* We don't know whether what comes next is a template-id,
11710 an identifier, or nothing at all. */
11711 cp_parser_parse_tentatively (parser);
11712 /* Check for a template-id. */
11713 id = cp_parser_template_id (parser,
11714 /*template_keyword_p=*/false,
11715 /*check_dependency_p=*/true);
11716 /* If that didn't work, it could still be an identifier. */
11717 if (!cp_parser_parse_definitely (parser))
11719 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11720 id = cp_parser_identifier (parser);
11721 else
11722 id = NULL_TREE;
11724 else
11726 template_id_p = true;
11727 ++num_templates;
11731 pop_deferring_access_checks ();
11733 /* If it's not a `:' or a `{' then we can't really be looking at a
11734 class-head, since a class-head only appears as part of a
11735 class-specifier. We have to detect this situation before calling
11736 xref_tag, since that has irreversible side-effects. */
11737 if (!cp_parser_next_token_starts_class_definition_p (parser))
11739 cp_parser_error (parser, "expected `{' or `:'");
11740 return error_mark_node;
11743 /* At this point, we're going ahead with the class-specifier, even
11744 if some other problem occurs. */
11745 cp_parser_commit_to_tentative_parse (parser);
11746 /* Issue the error about the overly-qualified name now. */
11747 if (qualified_p)
11748 cp_parser_error (parser,
11749 "global qualification of class name is invalid");
11750 else if (invalid_nested_name_p)
11751 cp_parser_error (parser,
11752 "qualified name does not name a class");
11753 /* Make sure that the right number of template parameters were
11754 present. */
11755 if (!cp_parser_check_template_parameters (parser, num_templates))
11756 /* If something went wrong, there is no point in even trying to
11757 process the class-definition. */
11758 return NULL_TREE;
11760 /* Look up the type. */
11761 if (template_id_p)
11763 type = TREE_TYPE (id);
11764 maybe_process_partial_specialization (type);
11766 else if (!nested_name_specifier)
11768 /* If the class was unnamed, create a dummy name. */
11769 if (!id)
11770 id = make_anon_name ();
11771 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11773 else
11775 tree class_type;
11776 tree scope;
11778 /* Given:
11780 template <typename T> struct S { struct T };
11781 template <typename T> struct S<T>::T { };
11783 we will get a TYPENAME_TYPE when processing the definition of
11784 `S::T'. We need to resolve it to the actual type before we
11785 try to define it. */
11786 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11788 class_type = resolve_typename_type (TREE_TYPE (type),
11789 /*only_current_p=*/false);
11790 if (class_type != error_mark_node)
11791 type = TYPE_NAME (class_type);
11792 else
11794 cp_parser_error (parser, "could not resolve typename type");
11795 type = error_mark_node;
11799 /* Figure out in what scope the declaration is being placed. */
11800 scope = current_scope ();
11801 if (!scope)
11802 scope = current_namespace;
11803 /* If that scope does not contain the scope in which the
11804 class was originally declared, the program is invalid. */
11805 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11807 error ("declaration of `%D' in `%D' which does not "
11808 "enclose `%D'", type, scope, nested_name_specifier);
11809 return NULL_TREE;
11812 maybe_process_partial_specialization (TREE_TYPE (type));
11813 class_type = current_class_type;
11814 type = TREE_TYPE (handle_class_head (class_key,
11815 nested_name_specifier,
11816 type,
11817 attributes));
11818 if (type != error_mark_node)
11820 if (!class_type && TYPE_CONTEXT (type))
11821 *nested_name_specifier_p = true;
11822 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11823 class_type))
11824 *nested_name_specifier_p = true;
11827 /* Indicate whether this class was declared as a `class' or as a
11828 `struct'. */
11829 if (TREE_CODE (type) == RECORD_TYPE)
11830 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11831 cp_parser_check_class_key (class_key, type);
11833 /* Enter the scope containing the class; the names of base classes
11834 should be looked up in that context. For example, given:
11836 struct A { struct B {}; struct C; };
11837 struct A::C : B {};
11839 is valid. */
11840 if (nested_name_specifier)
11841 push_scope (nested_name_specifier);
11842 /* Now, look for the base-clause. */
11843 token = cp_lexer_peek_token (parser->lexer);
11844 if (token->type == CPP_COLON)
11846 tree bases;
11848 /* Get the list of base-classes. */
11849 bases = cp_parser_base_clause (parser);
11850 /* Process them. */
11851 xref_basetypes (type, bases);
11853 /* Leave the scope given by the nested-name-specifier. We will
11854 enter the class scope itself while processing the members. */
11855 if (nested_name_specifier)
11856 pop_scope (nested_name_specifier);
11858 return type;
11861 /* Parse a class-key.
11863 class-key:
11864 class
11865 struct
11866 union
11868 Returns the kind of class-key specified, or none_type to indicate
11869 error. */
11871 static enum tag_types
11872 cp_parser_class_key (cp_parser* parser)
11874 cp_token *token;
11875 enum tag_types tag_type;
11877 /* Look for the class-key. */
11878 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11879 if (!token)
11880 return none_type;
11882 /* Check to see if the TOKEN is a class-key. */
11883 tag_type = cp_parser_token_is_class_key (token);
11884 if (!tag_type)
11885 cp_parser_error (parser, "expected class-key");
11886 return tag_type;
11889 /* Parse an (optional) member-specification.
11891 member-specification:
11892 member-declaration member-specification [opt]
11893 access-specifier : member-specification [opt] */
11895 static void
11896 cp_parser_member_specification_opt (cp_parser* parser)
11898 while (true)
11900 cp_token *token;
11901 enum rid keyword;
11903 /* Peek at the next token. */
11904 token = cp_lexer_peek_token (parser->lexer);
11905 /* If it's a `}', or EOF then we've seen all the members. */
11906 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11907 break;
11909 /* See if this token is a keyword. */
11910 keyword = token->keyword;
11911 switch (keyword)
11913 case RID_PUBLIC:
11914 case RID_PROTECTED:
11915 case RID_PRIVATE:
11916 /* Consume the access-specifier. */
11917 cp_lexer_consume_token (parser->lexer);
11918 /* Remember which access-specifier is active. */
11919 current_access_specifier = token->value;
11920 /* Look for the `:'. */
11921 cp_parser_require (parser, CPP_COLON, "`:'");
11922 break;
11924 default:
11925 /* Otherwise, the next construction must be a
11926 member-declaration. */
11927 cp_parser_member_declaration (parser);
11932 /* Parse a member-declaration.
11934 member-declaration:
11935 decl-specifier-seq [opt] member-declarator-list [opt] ;
11936 function-definition ; [opt]
11937 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11938 using-declaration
11939 template-declaration
11941 member-declarator-list:
11942 member-declarator
11943 member-declarator-list , member-declarator
11945 member-declarator:
11946 declarator pure-specifier [opt]
11947 declarator constant-initializer [opt]
11948 identifier [opt] : constant-expression
11950 GNU Extensions:
11952 member-declaration:
11953 __extension__ member-declaration
11955 member-declarator:
11956 declarator attributes [opt] pure-specifier [opt]
11957 declarator attributes [opt] constant-initializer [opt]
11958 identifier [opt] attributes [opt] : constant-expression */
11960 static void
11961 cp_parser_member_declaration (cp_parser* parser)
11963 tree decl_specifiers;
11964 tree prefix_attributes;
11965 tree decl;
11966 bool declares_class_or_enum;
11967 bool friend_p;
11968 cp_token *token;
11969 int saved_pedantic;
11971 /* Check for the `__extension__' keyword. */
11972 if (cp_parser_extension_opt (parser, &saved_pedantic))
11974 /* Recurse. */
11975 cp_parser_member_declaration (parser);
11976 /* Restore the old value of the PEDANTIC flag. */
11977 pedantic = saved_pedantic;
11979 return;
11982 /* Check for a template-declaration. */
11983 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11985 /* Parse the template-declaration. */
11986 cp_parser_template_declaration (parser, /*member_p=*/true);
11988 return;
11991 /* Check for a using-declaration. */
11992 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11994 /* Parse the using-declaration. */
11995 cp_parser_using_declaration (parser);
11997 return;
12000 /* We can't tell whether we're looking at a declaration or a
12001 function-definition. */
12002 cp_parser_parse_tentatively (parser);
12004 /* Parse the decl-specifier-seq. */
12005 decl_specifiers
12006 = cp_parser_decl_specifier_seq (parser,
12007 CP_PARSER_FLAGS_OPTIONAL,
12008 &prefix_attributes,
12009 &declares_class_or_enum);
12010 /* Check for an invalid type-name. */
12011 if (cp_parser_diagnose_invalid_type_name (parser))
12012 return;
12013 /* If there is no declarator, then the decl-specifier-seq should
12014 specify a type. */
12015 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12017 /* If there was no decl-specifier-seq, and the next token is a
12018 `;', then we have something like:
12020 struct S { ; };
12022 [class.mem]
12024 Each member-declaration shall declare at least one member
12025 name of the class. */
12026 if (!decl_specifiers)
12028 if (pedantic)
12029 pedwarn ("extra semicolon");
12031 else
12033 tree type;
12035 /* See if this declaration is a friend. */
12036 friend_p = cp_parser_friend_p (decl_specifiers);
12037 /* If there were decl-specifiers, check to see if there was
12038 a class-declaration. */
12039 type = check_tag_decl (decl_specifiers);
12040 /* Nested classes have already been added to the class, but
12041 a `friend' needs to be explicitly registered. */
12042 if (friend_p)
12044 /* If the `friend' keyword was present, the friend must
12045 be introduced with a class-key. */
12046 if (!declares_class_or_enum)
12047 error ("a class-key must be used when declaring a friend");
12048 /* In this case:
12050 template <typename T> struct A {
12051 friend struct A<T>::B;
12054 A<T>::B will be represented by a TYPENAME_TYPE, and
12055 therefore not recognized by check_tag_decl. */
12056 if (!type)
12058 tree specifier;
12060 for (specifier = decl_specifiers;
12061 specifier;
12062 specifier = TREE_CHAIN (specifier))
12064 tree s = TREE_VALUE (specifier);
12066 if (TREE_CODE (s) == IDENTIFIER_NODE
12067 && IDENTIFIER_GLOBAL_VALUE (s))
12068 type = IDENTIFIER_GLOBAL_VALUE (s);
12069 if (TREE_CODE (s) == TYPE_DECL)
12070 s = TREE_TYPE (s);
12071 if (TYPE_P (s))
12073 type = s;
12074 break;
12078 if (!type)
12079 error ("friend declaration does not name a class or "
12080 "function");
12081 else
12082 make_friend_class (current_class_type, type);
12084 /* If there is no TYPE, an error message will already have
12085 been issued. */
12086 else if (!type)
12088 /* An anonymous aggregate has to be handled specially; such
12089 a declaration really declares a data member (with a
12090 particular type), as opposed to a nested class. */
12091 else if (ANON_AGGR_TYPE_P (type))
12093 /* Remove constructors and such from TYPE, now that we
12094 know it is an anoymous aggregate. */
12095 fixup_anonymous_aggr (type);
12096 /* And make the corresponding data member. */
12097 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12098 /* Add it to the class. */
12099 finish_member_declaration (decl);
12103 else
12105 /* See if these declarations will be friends. */
12106 friend_p = cp_parser_friend_p (decl_specifiers);
12108 /* Keep going until we hit the `;' at the end of the
12109 declaration. */
12110 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12112 tree attributes = NULL_TREE;
12113 tree first_attribute;
12115 /* Peek at the next token. */
12116 token = cp_lexer_peek_token (parser->lexer);
12118 /* Check for a bitfield declaration. */
12119 if (token->type == CPP_COLON
12120 || (token->type == CPP_NAME
12121 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12122 == CPP_COLON))
12124 tree identifier;
12125 tree width;
12127 /* Get the name of the bitfield. Note that we cannot just
12128 check TOKEN here because it may have been invalidated by
12129 the call to cp_lexer_peek_nth_token above. */
12130 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12131 identifier = cp_parser_identifier (parser);
12132 else
12133 identifier = NULL_TREE;
12135 /* Consume the `:' token. */
12136 cp_lexer_consume_token (parser->lexer);
12137 /* Get the width of the bitfield. */
12138 width
12139 = cp_parser_constant_expression (parser,
12140 /*allow_non_constant=*/false,
12141 NULL);
12143 /* Look for attributes that apply to the bitfield. */
12144 attributes = cp_parser_attributes_opt (parser);
12145 /* Remember which attributes are prefix attributes and
12146 which are not. */
12147 first_attribute = attributes;
12148 /* Combine the attributes. */
12149 attributes = chainon (prefix_attributes, attributes);
12151 /* Create the bitfield declaration. */
12152 decl = grokbitfield (identifier,
12153 decl_specifiers,
12154 width);
12155 /* Apply the attributes. */
12156 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12158 else
12160 tree declarator;
12161 tree initializer;
12162 tree asm_specification;
12163 bool ctor_dtor_or_conv_p;
12165 /* Parse the declarator. */
12166 declarator
12167 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12168 &ctor_dtor_or_conv_p);
12170 /* If something went wrong parsing the declarator, make sure
12171 that we at least consume some tokens. */
12172 if (declarator == error_mark_node)
12174 /* Skip to the end of the statement. */
12175 cp_parser_skip_to_end_of_statement (parser);
12176 break;
12179 /* Look for an asm-specification. */
12180 asm_specification = cp_parser_asm_specification_opt (parser);
12181 /* Look for attributes that apply to the declaration. */
12182 attributes = cp_parser_attributes_opt (parser);
12183 /* Remember which attributes are prefix attributes and
12184 which are not. */
12185 first_attribute = attributes;
12186 /* Combine the attributes. */
12187 attributes = chainon (prefix_attributes, attributes);
12189 /* If it's an `=', then we have a constant-initializer or a
12190 pure-specifier. It is not correct to parse the
12191 initializer before registering the member declaration
12192 since the member declaration should be in scope while
12193 its initializer is processed. However, the rest of the
12194 front end does not yet provide an interface that allows
12195 us to handle this correctly. */
12196 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12198 /* In [class.mem]:
12200 A pure-specifier shall be used only in the declaration of
12201 a virtual function.
12203 A member-declarator can contain a constant-initializer
12204 only if it declares a static member of integral or
12205 enumeration type.
12207 Therefore, if the DECLARATOR is for a function, we look
12208 for a pure-specifier; otherwise, we look for a
12209 constant-initializer. When we call `grokfield', it will
12210 perform more stringent semantics checks. */
12211 if (TREE_CODE (declarator) == CALL_EXPR)
12212 initializer = cp_parser_pure_specifier (parser);
12213 else
12215 /* This declaration cannot be a function
12216 definition. */
12217 cp_parser_commit_to_tentative_parse (parser);
12218 /* Parse the initializer. */
12219 initializer = cp_parser_constant_initializer (parser);
12222 /* Otherwise, there is no initializer. */
12223 else
12224 initializer = NULL_TREE;
12226 /* See if we are probably looking at a function
12227 definition. We are certainly not looking at at a
12228 member-declarator. Calling `grokfield' has
12229 side-effects, so we must not do it unless we are sure
12230 that we are looking at a member-declarator. */
12231 if (cp_parser_token_starts_function_definition_p
12232 (cp_lexer_peek_token (parser->lexer)))
12233 decl = error_mark_node;
12234 else
12235 /* Create the declaration. */
12236 decl = grokfield (declarator,
12237 decl_specifiers,
12238 initializer,
12239 asm_specification,
12240 attributes);
12243 /* Reset PREFIX_ATTRIBUTES. */
12244 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12245 attributes = TREE_CHAIN (attributes);
12246 if (attributes)
12247 TREE_CHAIN (attributes) = NULL_TREE;
12249 /* If there is any qualification still in effect, clear it
12250 now; we will be starting fresh with the next declarator. */
12251 parser->scope = NULL_TREE;
12252 parser->qualifying_scope = NULL_TREE;
12253 parser->object_scope = NULL_TREE;
12254 /* If it's a `,', then there are more declarators. */
12255 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12256 cp_lexer_consume_token (parser->lexer);
12257 /* If the next token isn't a `;', then we have a parse error. */
12258 else if (cp_lexer_next_token_is_not (parser->lexer,
12259 CPP_SEMICOLON))
12261 cp_parser_error (parser, "expected `;'");
12262 /* Skip tokens until we find a `;' */
12263 cp_parser_skip_to_end_of_statement (parser);
12265 break;
12268 if (decl)
12270 /* Add DECL to the list of members. */
12271 if (!friend_p)
12272 finish_member_declaration (decl);
12274 if (TREE_CODE (decl) == FUNCTION_DECL)
12275 cp_parser_save_default_args (parser, decl);
12280 /* If everything went well, look for the `;'. */
12281 if (cp_parser_parse_definitely (parser))
12283 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12284 return;
12287 /* Parse the function-definition. */
12288 decl = cp_parser_function_definition (parser, &friend_p);
12289 /* If the member was not a friend, declare it here. */
12290 if (!friend_p)
12291 finish_member_declaration (decl);
12292 /* Peek at the next token. */
12293 token = cp_lexer_peek_token (parser->lexer);
12294 /* If the next token is a semicolon, consume it. */
12295 if (token->type == CPP_SEMICOLON)
12296 cp_lexer_consume_token (parser->lexer);
12299 /* Parse a pure-specifier.
12301 pure-specifier:
12304 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12305 Otherwiser, ERROR_MARK_NODE is returned. */
12307 static tree
12308 cp_parser_pure_specifier (cp_parser* parser)
12310 cp_token *token;
12312 /* Look for the `=' token. */
12313 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12314 return error_mark_node;
12315 /* Look for the `0' token. */
12316 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12317 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12318 to get information from the lexer about how the number was
12319 spelled in order to fix this problem. */
12320 if (!token || !integer_zerop (token->value))
12321 return error_mark_node;
12323 return integer_zero_node;
12326 /* Parse a constant-initializer.
12328 constant-initializer:
12329 = constant-expression
12331 Returns a representation of the constant-expression. */
12333 static tree
12334 cp_parser_constant_initializer (cp_parser* parser)
12336 /* Look for the `=' token. */
12337 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12338 return error_mark_node;
12340 /* It is invalid to write:
12342 struct S { static const int i = { 7 }; };
12345 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12347 cp_parser_error (parser,
12348 "a brace-enclosed initializer is not allowed here");
12349 /* Consume the opening brace. */
12350 cp_lexer_consume_token (parser->lexer);
12351 /* Skip the initializer. */
12352 cp_parser_skip_to_closing_brace (parser);
12353 /* Look for the trailing `}'. */
12354 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12356 return error_mark_node;
12359 return cp_parser_constant_expression (parser,
12360 /*allow_non_constant=*/false,
12361 NULL);
12364 /* Derived classes [gram.class.derived] */
12366 /* Parse a base-clause.
12368 base-clause:
12369 : base-specifier-list
12371 base-specifier-list:
12372 base-specifier
12373 base-specifier-list , base-specifier
12375 Returns a TREE_LIST representing the base-classes, in the order in
12376 which they were declared. The representation of each node is as
12377 described by cp_parser_base_specifier.
12379 In the case that no bases are specified, this function will return
12380 NULL_TREE, not ERROR_MARK_NODE. */
12382 static tree
12383 cp_parser_base_clause (cp_parser* parser)
12385 tree bases = NULL_TREE;
12387 /* Look for the `:' that begins the list. */
12388 cp_parser_require (parser, CPP_COLON, "`:'");
12390 /* Scan the base-specifier-list. */
12391 while (true)
12393 cp_token *token;
12394 tree base;
12396 /* Look for the base-specifier. */
12397 base = cp_parser_base_specifier (parser);
12398 /* Add BASE to the front of the list. */
12399 if (base != error_mark_node)
12401 TREE_CHAIN (base) = bases;
12402 bases = base;
12404 /* Peek at the next token. */
12405 token = cp_lexer_peek_token (parser->lexer);
12406 /* If it's not a comma, then the list is complete. */
12407 if (token->type != CPP_COMMA)
12408 break;
12409 /* Consume the `,'. */
12410 cp_lexer_consume_token (parser->lexer);
12413 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12414 base class had a qualified name. However, the next name that
12415 appears is certainly not qualified. */
12416 parser->scope = NULL_TREE;
12417 parser->qualifying_scope = NULL_TREE;
12418 parser->object_scope = NULL_TREE;
12420 return nreverse (bases);
12423 /* Parse a base-specifier.
12425 base-specifier:
12426 :: [opt] nested-name-specifier [opt] class-name
12427 virtual access-specifier [opt] :: [opt] nested-name-specifier
12428 [opt] class-name
12429 access-specifier virtual [opt] :: [opt] nested-name-specifier
12430 [opt] class-name
12432 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12433 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12434 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12435 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12437 static tree
12438 cp_parser_base_specifier (cp_parser* parser)
12440 cp_token *token;
12441 bool done = false;
12442 bool virtual_p = false;
12443 bool duplicate_virtual_error_issued_p = false;
12444 bool duplicate_access_error_issued_p = false;
12445 bool class_scope_p, template_p;
12446 tree access = access_default_node;
12447 tree type;
12449 /* Process the optional `virtual' and `access-specifier'. */
12450 while (!done)
12452 /* Peek at the next token. */
12453 token = cp_lexer_peek_token (parser->lexer);
12454 /* Process `virtual'. */
12455 switch (token->keyword)
12457 case RID_VIRTUAL:
12458 /* If `virtual' appears more than once, issue an error. */
12459 if (virtual_p && !duplicate_virtual_error_issued_p)
12461 cp_parser_error (parser,
12462 "`virtual' specified more than once in base-specified");
12463 duplicate_virtual_error_issued_p = true;
12466 virtual_p = true;
12468 /* Consume the `virtual' token. */
12469 cp_lexer_consume_token (parser->lexer);
12471 break;
12473 case RID_PUBLIC:
12474 case RID_PROTECTED:
12475 case RID_PRIVATE:
12476 /* If more than one access specifier appears, issue an
12477 error. */
12478 if (access != access_default_node
12479 && !duplicate_access_error_issued_p)
12481 cp_parser_error (parser,
12482 "more than one access specifier in base-specified");
12483 duplicate_access_error_issued_p = true;
12486 access = ridpointers[(int) token->keyword];
12488 /* Consume the access-specifier. */
12489 cp_lexer_consume_token (parser->lexer);
12491 break;
12493 default:
12494 done = true;
12495 break;
12499 /* Look for the optional `::' operator. */
12500 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12501 /* Look for the nested-name-specifier. The simplest way to
12502 implement:
12504 [temp.res]
12506 The keyword `typename' is not permitted in a base-specifier or
12507 mem-initializer; in these contexts a qualified name that
12508 depends on a template-parameter is implicitly assumed to be a
12509 type name.
12511 is to pretend that we have seen the `typename' keyword at this
12512 point. */
12513 cp_parser_nested_name_specifier_opt (parser,
12514 /*typename_keyword_p=*/true,
12515 /*check_dependency_p=*/true,
12516 /*type_p=*/true);
12517 /* If the base class is given by a qualified name, assume that names
12518 we see are type names or templates, as appropriate. */
12519 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12520 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12522 /* Finally, look for the class-name. */
12523 type = cp_parser_class_name (parser,
12524 class_scope_p,
12525 template_p,
12526 /*type_p=*/true,
12527 /*check_dependency_p=*/true,
12528 /*class_head_p=*/false);
12530 if (type == error_mark_node)
12531 return error_mark_node;
12533 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12536 /* Exception handling [gram.exception] */
12538 /* Parse an (optional) exception-specification.
12540 exception-specification:
12541 throw ( type-id-list [opt] )
12543 Returns a TREE_LIST representing the exception-specification. The
12544 TREE_VALUE of each node is a type. */
12546 static tree
12547 cp_parser_exception_specification_opt (cp_parser* parser)
12549 cp_token *token;
12550 tree type_id_list;
12552 /* Peek at the next token. */
12553 token = cp_lexer_peek_token (parser->lexer);
12554 /* If it's not `throw', then there's no exception-specification. */
12555 if (!cp_parser_is_keyword (token, RID_THROW))
12556 return NULL_TREE;
12558 /* Consume the `throw'. */
12559 cp_lexer_consume_token (parser->lexer);
12561 /* Look for the `('. */
12562 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12564 /* Peek at the next token. */
12565 token = cp_lexer_peek_token (parser->lexer);
12566 /* If it's not a `)', then there is a type-id-list. */
12567 if (token->type != CPP_CLOSE_PAREN)
12569 const char *saved_message;
12571 /* Types may not be defined in an exception-specification. */
12572 saved_message = parser->type_definition_forbidden_message;
12573 parser->type_definition_forbidden_message
12574 = "types may not be defined in an exception-specification";
12575 /* Parse the type-id-list. */
12576 type_id_list = cp_parser_type_id_list (parser);
12577 /* Restore the saved message. */
12578 parser->type_definition_forbidden_message = saved_message;
12580 else
12581 type_id_list = empty_except_spec;
12583 /* Look for the `)'. */
12584 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12586 return type_id_list;
12589 /* Parse an (optional) type-id-list.
12591 type-id-list:
12592 type-id
12593 type-id-list , type-id
12595 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12596 in the order that the types were presented. */
12598 static tree
12599 cp_parser_type_id_list (cp_parser* parser)
12601 tree types = NULL_TREE;
12603 while (true)
12605 cp_token *token;
12606 tree type;
12608 /* Get the next type-id. */
12609 type = cp_parser_type_id (parser);
12610 /* Add it to the list. */
12611 types = add_exception_specifier (types, type, /*complain=*/1);
12612 /* Peek at the next token. */
12613 token = cp_lexer_peek_token (parser->lexer);
12614 /* If it is not a `,', we are done. */
12615 if (token->type != CPP_COMMA)
12616 break;
12617 /* Consume the `,'. */
12618 cp_lexer_consume_token (parser->lexer);
12621 return nreverse (types);
12624 /* Parse a try-block.
12626 try-block:
12627 try compound-statement handler-seq */
12629 static tree
12630 cp_parser_try_block (cp_parser* parser)
12632 tree try_block;
12634 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12635 try_block = begin_try_block ();
12636 cp_parser_compound_statement (parser);
12637 finish_try_block (try_block);
12638 cp_parser_handler_seq (parser);
12639 finish_handler_sequence (try_block);
12641 return try_block;
12644 /* Parse a function-try-block.
12646 function-try-block:
12647 try ctor-initializer [opt] function-body handler-seq */
12649 static bool
12650 cp_parser_function_try_block (cp_parser* parser)
12652 tree try_block;
12653 bool ctor_initializer_p;
12655 /* Look for the `try' keyword. */
12656 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12657 return false;
12658 /* Let the rest of the front-end know where we are. */
12659 try_block = begin_function_try_block ();
12660 /* Parse the function-body. */
12661 ctor_initializer_p
12662 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12663 /* We're done with the `try' part. */
12664 finish_function_try_block (try_block);
12665 /* Parse the handlers. */
12666 cp_parser_handler_seq (parser);
12667 /* We're done with the handlers. */
12668 finish_function_handler_sequence (try_block);
12670 return ctor_initializer_p;
12673 /* Parse a handler-seq.
12675 handler-seq:
12676 handler handler-seq [opt] */
12678 static void
12679 cp_parser_handler_seq (cp_parser* parser)
12681 while (true)
12683 cp_token *token;
12685 /* Parse the handler. */
12686 cp_parser_handler (parser);
12687 /* Peek at the next token. */
12688 token = cp_lexer_peek_token (parser->lexer);
12689 /* If it's not `catch' then there are no more handlers. */
12690 if (!cp_parser_is_keyword (token, RID_CATCH))
12691 break;
12695 /* Parse a handler.
12697 handler:
12698 catch ( exception-declaration ) compound-statement */
12700 static void
12701 cp_parser_handler (cp_parser* parser)
12703 tree handler;
12704 tree declaration;
12706 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12707 handler = begin_handler ();
12708 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12709 declaration = cp_parser_exception_declaration (parser);
12710 finish_handler_parms (declaration, handler);
12711 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12712 cp_parser_compound_statement (parser);
12713 finish_handler (handler);
12716 /* Parse an exception-declaration.
12718 exception-declaration:
12719 type-specifier-seq declarator
12720 type-specifier-seq abstract-declarator
12721 type-specifier-seq
12722 ...
12724 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12725 ellipsis variant is used. */
12727 static tree
12728 cp_parser_exception_declaration (cp_parser* parser)
12730 tree type_specifiers;
12731 tree declarator;
12732 const char *saved_message;
12734 /* If it's an ellipsis, it's easy to handle. */
12735 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12737 /* Consume the `...' token. */
12738 cp_lexer_consume_token (parser->lexer);
12739 return NULL_TREE;
12742 /* Types may not be defined in exception-declarations. */
12743 saved_message = parser->type_definition_forbidden_message;
12744 parser->type_definition_forbidden_message
12745 = "types may not be defined in exception-declarations";
12747 /* Parse the type-specifier-seq. */
12748 type_specifiers = cp_parser_type_specifier_seq (parser);
12749 /* If it's a `)', then there is no declarator. */
12750 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12751 declarator = NULL_TREE;
12752 else
12753 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12754 /*ctor_dtor_or_conv_p=*/NULL);
12756 /* Restore the saved message. */
12757 parser->type_definition_forbidden_message = saved_message;
12759 return start_handler_parms (type_specifiers, declarator);
12762 /* Parse a throw-expression.
12764 throw-expression:
12765 throw assignment-expresion [opt]
12767 Returns a THROW_EXPR representing the throw-expression. */
12769 static tree
12770 cp_parser_throw_expression (cp_parser* parser)
12772 tree expression;
12774 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12775 /* We can't be sure if there is an assignment-expression or not. */
12776 cp_parser_parse_tentatively (parser);
12777 /* Try it. */
12778 expression = cp_parser_assignment_expression (parser);
12779 /* If it didn't work, this is just a rethrow. */
12780 if (!cp_parser_parse_definitely (parser))
12781 expression = NULL_TREE;
12783 return build_throw (expression);
12786 /* GNU Extensions */
12788 /* Parse an (optional) asm-specification.
12790 asm-specification:
12791 asm ( string-literal )
12793 If the asm-specification is present, returns a STRING_CST
12794 corresponding to the string-literal. Otherwise, returns
12795 NULL_TREE. */
12797 static tree
12798 cp_parser_asm_specification_opt (cp_parser* parser)
12800 cp_token *token;
12801 tree asm_specification;
12803 /* Peek at the next token. */
12804 token = cp_lexer_peek_token (parser->lexer);
12805 /* If the next token isn't the `asm' keyword, then there's no
12806 asm-specification. */
12807 if (!cp_parser_is_keyword (token, RID_ASM))
12808 return NULL_TREE;
12810 /* Consume the `asm' token. */
12811 cp_lexer_consume_token (parser->lexer);
12812 /* Look for the `('. */
12813 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12815 /* Look for the string-literal. */
12816 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12817 if (token)
12818 asm_specification = token->value;
12819 else
12820 asm_specification = NULL_TREE;
12822 /* Look for the `)'. */
12823 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12825 return asm_specification;
12828 /* Parse an asm-operand-list.
12830 asm-operand-list:
12831 asm-operand
12832 asm-operand-list , asm-operand
12834 asm-operand:
12835 string-literal ( expression )
12836 [ string-literal ] string-literal ( expression )
12838 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12839 each node is the expression. The TREE_PURPOSE is itself a
12840 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12841 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12842 is a STRING_CST for the string literal before the parenthesis. */
12844 static tree
12845 cp_parser_asm_operand_list (cp_parser* parser)
12847 tree asm_operands = NULL_TREE;
12849 while (true)
12851 tree string_literal;
12852 tree expression;
12853 tree name;
12854 cp_token *token;
12856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12858 /* Consume the `[' token. */
12859 cp_lexer_consume_token (parser->lexer);
12860 /* Read the operand name. */
12861 name = cp_parser_identifier (parser);
12862 if (name != error_mark_node)
12863 name = build_string (IDENTIFIER_LENGTH (name),
12864 IDENTIFIER_POINTER (name));
12865 /* Look for the closing `]'. */
12866 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12868 else
12869 name = NULL_TREE;
12870 /* Look for the string-literal. */
12871 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12872 string_literal = token ? token->value : error_mark_node;
12873 /* Look for the `('. */
12874 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12875 /* Parse the expression. */
12876 expression = cp_parser_expression (parser);
12877 /* Look for the `)'. */
12878 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12879 /* Add this operand to the list. */
12880 asm_operands = tree_cons (build_tree_list (name, string_literal),
12881 expression,
12882 asm_operands);
12883 /* If the next token is not a `,', there are no more
12884 operands. */
12885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12886 break;
12887 /* Consume the `,'. */
12888 cp_lexer_consume_token (parser->lexer);
12891 return nreverse (asm_operands);
12894 /* Parse an asm-clobber-list.
12896 asm-clobber-list:
12897 string-literal
12898 asm-clobber-list , string-literal
12900 Returns a TREE_LIST, indicating the clobbers in the order that they
12901 appeared. The TREE_VALUE of each node is a STRING_CST. */
12903 static tree
12904 cp_parser_asm_clobber_list (cp_parser* parser)
12906 tree clobbers = NULL_TREE;
12908 while (true)
12910 cp_token *token;
12911 tree string_literal;
12913 /* Look for the string literal. */
12914 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12915 string_literal = token ? token->value : error_mark_node;
12916 /* Add it to the list. */
12917 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12918 /* If the next token is not a `,', then the list is
12919 complete. */
12920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12921 break;
12922 /* Consume the `,' token. */
12923 cp_lexer_consume_token (parser->lexer);
12926 return clobbers;
12929 /* Parse an (optional) series of attributes.
12931 attributes:
12932 attributes attribute
12934 attribute:
12935 __attribute__ (( attribute-list [opt] ))
12937 The return value is as for cp_parser_attribute_list. */
12939 static tree
12940 cp_parser_attributes_opt (cp_parser* parser)
12942 tree attributes = NULL_TREE;
12944 while (true)
12946 cp_token *token;
12947 tree attribute_list;
12949 /* Peek at the next token. */
12950 token = cp_lexer_peek_token (parser->lexer);
12951 /* If it's not `__attribute__', then we're done. */
12952 if (token->keyword != RID_ATTRIBUTE)
12953 break;
12955 /* Consume the `__attribute__' keyword. */
12956 cp_lexer_consume_token (parser->lexer);
12957 /* Look for the two `(' tokens. */
12958 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12959 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12961 /* Peek at the next token. */
12962 token = cp_lexer_peek_token (parser->lexer);
12963 if (token->type != CPP_CLOSE_PAREN)
12964 /* Parse the attribute-list. */
12965 attribute_list = cp_parser_attribute_list (parser);
12966 else
12967 /* If the next token is a `)', then there is no attribute
12968 list. */
12969 attribute_list = NULL;
12971 /* Look for the two `)' tokens. */
12972 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12973 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12975 /* Add these new attributes to the list. */
12976 attributes = chainon (attributes, attribute_list);
12979 return attributes;
12982 /* Parse an attribute-list.
12984 attribute-list:
12985 attribute
12986 attribute-list , attribute
12988 attribute:
12989 identifier
12990 identifier ( identifier )
12991 identifier ( identifier , expression-list )
12992 identifier ( expression-list )
12994 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12995 TREE_PURPOSE of each node is the identifier indicating which
12996 attribute is in use. The TREE_VALUE represents the arguments, if
12997 any. */
12999 static tree
13000 cp_parser_attribute_list (cp_parser* parser)
13002 tree attribute_list = NULL_TREE;
13004 while (true)
13006 cp_token *token;
13007 tree identifier;
13008 tree attribute;
13010 /* Look for the identifier. We also allow keywords here; for
13011 example `__attribute__ ((const))' is legal. */
13012 token = cp_lexer_peek_token (parser->lexer);
13013 if (token->type != CPP_NAME
13014 && token->type != CPP_KEYWORD)
13015 return error_mark_node;
13016 /* Consume the token. */
13017 token = cp_lexer_consume_token (parser->lexer);
13019 /* Save away the identifier that indicates which attribute this is. */
13020 identifier = token->value;
13021 attribute = build_tree_list (identifier, NULL_TREE);
13023 /* Peek at the next token. */
13024 token = cp_lexer_peek_token (parser->lexer);
13025 /* If it's an `(', then parse the attribute arguments. */
13026 if (token->type == CPP_OPEN_PAREN)
13028 tree arguments;
13029 int arguments_allowed_p = 1;
13031 /* Consume the `('. */
13032 cp_lexer_consume_token (parser->lexer);
13033 /* Peek at the next token. */
13034 token = cp_lexer_peek_token (parser->lexer);
13035 /* Check to see if the next token is an identifier. */
13036 if (token->type == CPP_NAME)
13038 /* Save the identifier. */
13039 identifier = token->value;
13040 /* Consume the identifier. */
13041 cp_lexer_consume_token (parser->lexer);
13042 /* Peek at the next token. */
13043 token = cp_lexer_peek_token (parser->lexer);
13044 /* If the next token is a `,', then there are some other
13045 expressions as well. */
13046 if (token->type == CPP_COMMA)
13047 /* Consume the comma. */
13048 cp_lexer_consume_token (parser->lexer);
13049 else
13050 arguments_allowed_p = 0;
13052 else
13053 identifier = NULL_TREE;
13055 /* If there are arguments, parse them too. */
13056 if (arguments_allowed_p)
13057 arguments = cp_parser_expression_list (parser);
13058 else
13059 arguments = NULL_TREE;
13061 /* Combine the identifier and the arguments. */
13062 if (identifier)
13063 arguments = tree_cons (NULL_TREE, identifier, arguments);
13065 /* Save the identifier and arguments away. */
13066 TREE_VALUE (attribute) = arguments;
13068 /* Look for the closing `)'. */
13069 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13072 /* Add this attribute to the list. */
13073 TREE_CHAIN (attribute) = attribute_list;
13074 attribute_list = attribute;
13076 /* Now, look for more attributes. */
13077 token = cp_lexer_peek_token (parser->lexer);
13078 /* If the next token isn't a `,', we're done. */
13079 if (token->type != CPP_COMMA)
13080 break;
13082 /* Consume the commma and keep going. */
13083 cp_lexer_consume_token (parser->lexer);
13086 /* We built up the list in reverse order. */
13087 return nreverse (attribute_list);
13090 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13091 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13092 current value of the PEDANTIC flag, regardless of whether or not
13093 the `__extension__' keyword is present. The caller is responsible
13094 for restoring the value of the PEDANTIC flag. */
13096 static bool
13097 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13099 /* Save the old value of the PEDANTIC flag. */
13100 *saved_pedantic = pedantic;
13102 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13104 /* Consume the `__extension__' token. */
13105 cp_lexer_consume_token (parser->lexer);
13106 /* We're not being pedantic while the `__extension__' keyword is
13107 in effect. */
13108 pedantic = 0;
13110 return true;
13113 return false;
13116 /* Parse a label declaration.
13118 label-declaration:
13119 __label__ label-declarator-seq ;
13121 label-declarator-seq:
13122 identifier , label-declarator-seq
13123 identifier */
13125 static void
13126 cp_parser_label_declaration (cp_parser* parser)
13128 /* Look for the `__label__' keyword. */
13129 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13131 while (true)
13133 tree identifier;
13135 /* Look for an identifier. */
13136 identifier = cp_parser_identifier (parser);
13137 /* Declare it as a lobel. */
13138 finish_label_decl (identifier);
13139 /* If the next token is a `;', stop. */
13140 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13141 break;
13142 /* Look for the `,' separating the label declarations. */
13143 cp_parser_require (parser, CPP_COMMA, "`,'");
13146 /* Look for the final `;'. */
13147 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13150 /* Support Functions */
13152 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13153 NAME should have one of the representations used for an
13154 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13155 is returned. If PARSER->SCOPE is a dependent type, then a
13156 SCOPE_REF is returned.
13158 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13159 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13160 was formed. Abstractly, such entities should not be passed to this
13161 function, because they do not need to be looked up, but it is
13162 simpler to check for this special case here, rather than at the
13163 call-sites.
13165 In cases not explicitly covered above, this function returns a
13166 DECL, OVERLOAD, or baselink representing the result of the lookup.
13167 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13168 is returned.
13170 If IS_TYPE is TRUE, bindings that do not refer to types are
13171 ignored.
13173 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13174 are ignored.
13176 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13177 types. */
13179 static tree
13180 cp_parser_lookup_name (cp_parser *parser, tree name,
13181 bool is_type, bool is_namespace, bool check_dependency)
13183 tree decl;
13184 tree object_type = parser->context->object_type;
13186 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13187 no longer valid. Note that if we are parsing tentatively, and
13188 the parse fails, OBJECT_TYPE will be automatically restored. */
13189 parser->context->object_type = NULL_TREE;
13191 if (name == error_mark_node)
13192 return error_mark_node;
13194 /* A template-id has already been resolved; there is no lookup to
13195 do. */
13196 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13197 return name;
13198 if (BASELINK_P (name))
13200 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13201 == TEMPLATE_ID_EXPR),
13202 20020909);
13203 return name;
13206 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13207 it should already have been checked to make sure that the name
13208 used matches the type being destroyed. */
13209 if (TREE_CODE (name) == BIT_NOT_EXPR)
13211 tree type;
13213 /* Figure out to which type this destructor applies. */
13214 if (parser->scope)
13215 type = parser->scope;
13216 else if (object_type)
13217 type = object_type;
13218 else
13219 type = current_class_type;
13220 /* If that's not a class type, there is no destructor. */
13221 if (!type || !CLASS_TYPE_P (type))
13222 return error_mark_node;
13223 /* If it was a class type, return the destructor. */
13224 return CLASSTYPE_DESTRUCTORS (type);
13227 /* By this point, the NAME should be an ordinary identifier. If
13228 the id-expression was a qualified name, the qualifying scope is
13229 stored in PARSER->SCOPE at this point. */
13230 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13231 20000619);
13233 /* Perform the lookup. */
13234 if (parser->scope)
13236 bool dependent_p;
13238 if (parser->scope == error_mark_node)
13239 return error_mark_node;
13241 /* If the SCOPE is dependent, the lookup must be deferred until
13242 the template is instantiated -- unless we are explicitly
13243 looking up names in uninstantiated templates. Even then, we
13244 cannot look up the name if the scope is not a class type; it
13245 might, for example, be a template type parameter. */
13246 dependent_p = (TYPE_P (parser->scope)
13247 && !(parser->in_declarator_p
13248 && currently_open_class (parser->scope))
13249 && dependent_type_p (parser->scope));
13250 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13251 && dependent_p)
13253 if (!is_type)
13254 decl = build_nt (SCOPE_REF, parser->scope, name);
13255 else
13256 /* The resolution to Core Issue 180 says that `struct A::B'
13257 should be considered a type-name, even if `A' is
13258 dependent. */
13259 decl = TYPE_NAME (make_typename_type (parser->scope,
13260 name,
13261 /*complain=*/1));
13263 else
13265 /* If PARSER->SCOPE is a dependent type, then it must be a
13266 class type, and we must not be checking dependencies;
13267 otherwise, we would have processed this lookup above. So
13268 that PARSER->SCOPE is not considered a dependent base by
13269 lookup_member, we must enter the scope here. */
13270 if (dependent_p)
13271 push_scope (parser->scope);
13272 /* If the PARSER->SCOPE is a a template specialization, it
13273 may be instantiated during name lookup. In that case,
13274 errors may be issued. Even if we rollback the current
13275 tentative parse, those errors are valid. */
13276 decl = lookup_qualified_name (parser->scope, name, is_type,
13277 /*flags=*/0);
13278 if (dependent_p)
13279 pop_scope (parser->scope);
13281 parser->qualifying_scope = parser->scope;
13282 parser->object_scope = NULL_TREE;
13284 else if (object_type)
13286 tree object_decl = NULL_TREE;
13287 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13288 OBJECT_TYPE is not a class. */
13289 if (CLASS_TYPE_P (object_type))
13290 /* If the OBJECT_TYPE is a template specialization, it may
13291 be instantiated during name lookup. In that case, errors
13292 may be issued. Even if we rollback the current tentative
13293 parse, those errors are valid. */
13294 object_decl = lookup_member (object_type,
13295 name,
13296 /*protect=*/0, is_type);
13297 /* Look it up in the enclosing context, too. */
13298 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13299 is_namespace,
13300 /*flags=*/0);
13301 parser->object_scope = object_type;
13302 parser->qualifying_scope = NULL_TREE;
13303 if (object_decl)
13304 decl = object_decl;
13306 else
13308 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13309 is_namespace,
13310 /*flags=*/0);
13311 parser->qualifying_scope = NULL_TREE;
13312 parser->object_scope = NULL_TREE;
13315 /* If the lookup failed, let our caller know. */
13316 if (!decl
13317 || decl == error_mark_node
13318 || (TREE_CODE (decl) == FUNCTION_DECL
13319 && DECL_ANTICIPATED (decl)))
13320 return error_mark_node;
13322 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13323 if (TREE_CODE (decl) == TREE_LIST)
13325 /* The error message we have to print is too complicated for
13326 cp_parser_error, so we incorporate its actions directly. */
13327 if (!cp_parser_simulate_error (parser))
13329 error ("reference to `%D' is ambiguous", name);
13330 print_candidates (decl);
13332 return error_mark_node;
13335 my_friendly_assert (DECL_P (decl)
13336 || TREE_CODE (decl) == OVERLOAD
13337 || TREE_CODE (decl) == SCOPE_REF
13338 || BASELINK_P (decl),
13339 20000619);
13341 /* If we have resolved the name of a member declaration, check to
13342 see if the declaration is accessible. When the name resolves to
13343 set of overloaded functions, accesibility is checked when
13344 overload resolution is done.
13346 During an explicit instantiation, access is not checked at all,
13347 as per [temp.explicit]. */
13348 if (DECL_P (decl))
13350 tree qualifying_type;
13352 /* Figure out the type through which DECL is being
13353 accessed. */
13354 qualifying_type
13355 = cp_parser_scope_through_which_access_occurs (decl,
13356 object_type,
13357 parser->scope);
13358 if (qualifying_type)
13359 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
13362 return decl;
13365 /* Like cp_parser_lookup_name, but for use in the typical case where
13366 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13367 TRUE. */
13369 static tree
13370 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13372 return cp_parser_lookup_name (parser, name,
13373 /*is_type=*/false,
13374 /*is_namespace=*/false,
13375 /*check_dependency=*/true);
13378 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13379 the current context, return the TYPE_DECL. If TAG_NAME_P is
13380 true, the DECL indicates the class being defined in a class-head,
13381 or declared in an elaborated-type-specifier.
13383 Otherwise, return DECL. */
13385 static tree
13386 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13388 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13389 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13391 struct A {
13392 template <typename T> struct B;
13395 template <typename T> struct A::B {};
13397 Similarly, in a elaborated-type-specifier:
13399 namespace N { struct X{}; }
13401 struct A {
13402 template <typename T> friend struct N::X;
13405 However, if the DECL refers to a class type, and we are in
13406 the scope of the class, then the name lookup automatically
13407 finds the TYPE_DECL created by build_self_reference rather
13408 than a TEMPLATE_DECL. For example, in:
13410 template <class T> struct S {
13411 S s;
13414 there is no need to handle such case. */
13416 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13417 return DECL_TEMPLATE_RESULT (decl);
13419 return decl;
13422 /* If too many, or too few, template-parameter lists apply to the
13423 declarator, issue an error message. Returns TRUE if all went well,
13424 and FALSE otherwise. */
13426 static bool
13427 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13428 tree declarator)
13430 unsigned num_templates;
13432 /* We haven't seen any classes that involve template parameters yet. */
13433 num_templates = 0;
13435 switch (TREE_CODE (declarator))
13437 case CALL_EXPR:
13438 case ARRAY_REF:
13439 case INDIRECT_REF:
13440 case ADDR_EXPR:
13442 tree main_declarator = TREE_OPERAND (declarator, 0);
13443 return
13444 cp_parser_check_declarator_template_parameters (parser,
13445 main_declarator);
13448 case SCOPE_REF:
13450 tree scope;
13451 tree member;
13453 scope = TREE_OPERAND (declarator, 0);
13454 member = TREE_OPERAND (declarator, 1);
13456 /* If this is a pointer-to-member, then we are not interested
13457 in the SCOPE, because it does not qualify the thing that is
13458 being declared. */
13459 if (TREE_CODE (member) == INDIRECT_REF)
13460 return (cp_parser_check_declarator_template_parameters
13461 (parser, member));
13463 while (scope && CLASS_TYPE_P (scope))
13465 /* You're supposed to have one `template <...>'
13466 for every template class, but you don't need one
13467 for a full specialization. For example:
13469 template <class T> struct S{};
13470 template <> struct S<int> { void f(); };
13471 void S<int>::f () {}
13473 is correct; there shouldn't be a `template <>' for
13474 the definition of `S<int>::f'. */
13475 if (CLASSTYPE_TEMPLATE_INFO (scope)
13476 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13477 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13478 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13479 ++num_templates;
13481 scope = TYPE_CONTEXT (scope);
13485 /* Fall through. */
13487 default:
13488 /* If the DECLARATOR has the form `X<y>' then it uses one
13489 additional level of template parameters. */
13490 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13491 ++num_templates;
13493 return cp_parser_check_template_parameters (parser,
13494 num_templates);
13498 /* NUM_TEMPLATES were used in the current declaration. If that is
13499 invalid, return FALSE and issue an error messages. Otherwise,
13500 return TRUE. */
13502 static bool
13503 cp_parser_check_template_parameters (cp_parser* parser,
13504 unsigned num_templates)
13506 /* If there are more template classes than parameter lists, we have
13507 something like:
13509 template <class T> void S<T>::R<T>::f (); */
13510 if (parser->num_template_parameter_lists < num_templates)
13512 error ("too few template-parameter-lists");
13513 return false;
13515 /* If there are the same number of template classes and parameter
13516 lists, that's OK. */
13517 if (parser->num_template_parameter_lists == num_templates)
13518 return true;
13519 /* If there are more, but only one more, then we are referring to a
13520 member template. That's OK too. */
13521 if (parser->num_template_parameter_lists == num_templates + 1)
13522 return true;
13523 /* Otherwise, there are too many template parameter lists. We have
13524 something like:
13526 template <class T> template <class U> void S::f(); */
13527 error ("too many template-parameter-lists");
13528 return false;
13531 /* Parse a binary-expression of the general form:
13533 binary-expression:
13534 <expr>
13535 binary-expression <token> <expr>
13537 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13538 to parser the <expr>s. If the first production is used, then the
13539 value returned by FN is returned directly. Otherwise, a node with
13540 the indicated EXPR_TYPE is returned, with operands corresponding to
13541 the two sub-expressions. */
13543 static tree
13544 cp_parser_binary_expression (cp_parser* parser,
13545 const cp_parser_token_tree_map token_tree_map,
13546 cp_parser_expression_fn fn)
13548 tree lhs;
13550 /* Parse the first expression. */
13551 lhs = (*fn) (parser);
13552 /* Now, look for more expressions. */
13553 while (true)
13555 cp_token *token;
13556 const cp_parser_token_tree_map_node *map_node;
13557 tree rhs;
13559 /* Peek at the next token. */
13560 token = cp_lexer_peek_token (parser->lexer);
13561 /* If the token is `>', and that's not an operator at the
13562 moment, then we're done. */
13563 if (token->type == CPP_GREATER
13564 && !parser->greater_than_is_operator_p)
13565 break;
13566 /* If we find one of the tokens we want, build the correspoding
13567 tree representation. */
13568 for (map_node = token_tree_map;
13569 map_node->token_type != CPP_EOF;
13570 ++map_node)
13571 if (map_node->token_type == token->type)
13573 /* Consume the operator token. */
13574 cp_lexer_consume_token (parser->lexer);
13575 /* Parse the right-hand side of the expression. */
13576 rhs = (*fn) (parser);
13577 /* Build the binary tree node. */
13578 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13579 break;
13582 /* If the token wasn't one of the ones we want, we're done. */
13583 if (map_node->token_type == CPP_EOF)
13584 break;
13587 return lhs;
13590 /* Parse an optional `::' token indicating that the following name is
13591 from the global namespace. If so, PARSER->SCOPE is set to the
13592 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13593 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13594 Returns the new value of PARSER->SCOPE, if the `::' token is
13595 present, and NULL_TREE otherwise. */
13597 static tree
13598 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13600 cp_token *token;
13602 /* Peek at the next token. */
13603 token = cp_lexer_peek_token (parser->lexer);
13604 /* If we're looking at a `::' token then we're starting from the
13605 global namespace, not our current location. */
13606 if (token->type == CPP_SCOPE)
13608 /* Consume the `::' token. */
13609 cp_lexer_consume_token (parser->lexer);
13610 /* Set the SCOPE so that we know where to start the lookup. */
13611 parser->scope = global_namespace;
13612 parser->qualifying_scope = global_namespace;
13613 parser->object_scope = NULL_TREE;
13615 return parser->scope;
13617 else if (!current_scope_valid_p)
13619 parser->scope = NULL_TREE;
13620 parser->qualifying_scope = NULL_TREE;
13621 parser->object_scope = NULL_TREE;
13624 return NULL_TREE;
13627 /* Returns TRUE if the upcoming token sequence is the start of a
13628 constructor declarator. If FRIEND_P is true, the declarator is
13629 preceded by the `friend' specifier. */
13631 static bool
13632 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13634 bool constructor_p;
13635 tree type_decl = NULL_TREE;
13636 bool nested_name_p;
13637 cp_token *next_token;
13639 /* The common case is that this is not a constructor declarator, so
13640 try to avoid doing lots of work if at all possible. It's not
13641 valid declare a constructor at function scope. */
13642 if (at_function_scope_p ())
13643 return false;
13644 /* And only certain tokens can begin a constructor declarator. */
13645 next_token = cp_lexer_peek_token (parser->lexer);
13646 if (next_token->type != CPP_NAME
13647 && next_token->type != CPP_SCOPE
13648 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13649 && next_token->type != CPP_TEMPLATE_ID)
13650 return false;
13652 /* Parse tentatively; we are going to roll back all of the tokens
13653 consumed here. */
13654 cp_parser_parse_tentatively (parser);
13655 /* Assume that we are looking at a constructor declarator. */
13656 constructor_p = true;
13658 /* Look for the optional `::' operator. */
13659 cp_parser_global_scope_opt (parser,
13660 /*current_scope_valid_p=*/false);
13661 /* Look for the nested-name-specifier. */
13662 nested_name_p
13663 = (cp_parser_nested_name_specifier_opt (parser,
13664 /*typename_keyword_p=*/false,
13665 /*check_dependency_p=*/false,
13666 /*type_p=*/false)
13667 != NULL_TREE);
13668 /* Outside of a class-specifier, there must be a
13669 nested-name-specifier. */
13670 if (!nested_name_p &&
13671 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13672 || friend_p))
13673 constructor_p = false;
13674 /* If we still think that this might be a constructor-declarator,
13675 look for a class-name. */
13676 if (constructor_p)
13678 /* If we have:
13680 template <typename T> struct S { S(); };
13681 template <typename T> S<T>::S ();
13683 we must recognize that the nested `S' names a class.
13684 Similarly, for:
13686 template <typename T> S<T>::S<T> ();
13688 we must recognize that the nested `S' names a template. */
13689 type_decl = cp_parser_class_name (parser,
13690 /*typename_keyword_p=*/false,
13691 /*template_keyword_p=*/false,
13692 /*type_p=*/false,
13693 /*check_dependency_p=*/false,
13694 /*class_head_p=*/false);
13695 /* If there was no class-name, then this is not a constructor. */
13696 constructor_p = !cp_parser_error_occurred (parser);
13699 /* If we're still considering a constructor, we have to see a `(',
13700 to begin the parameter-declaration-clause, followed by either a
13701 `)', an `...', or a decl-specifier. We need to check for a
13702 type-specifier to avoid being fooled into thinking that:
13704 S::S (f) (int);
13706 is a constructor. (It is actually a function named `f' that
13707 takes one parameter (of type `int') and returns a value of type
13708 `S::S'. */
13709 if (constructor_p
13710 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13713 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13714 && !cp_parser_storage_class_specifier_opt (parser))
13716 tree type;
13718 /* Names appearing in the type-specifier should be looked up
13719 in the scope of the class. */
13720 if (current_class_type)
13721 type = NULL_TREE;
13722 else
13724 type = TREE_TYPE (type_decl);
13725 if (TREE_CODE (type) == TYPENAME_TYPE)
13727 type = resolve_typename_type (type,
13728 /*only_current_p=*/false);
13729 if (type == error_mark_node)
13731 cp_parser_abort_tentative_parse (parser);
13732 return false;
13735 push_scope (type);
13737 /* Look for the type-specifier. */
13738 cp_parser_type_specifier (parser,
13739 CP_PARSER_FLAGS_NONE,
13740 /*is_friend=*/false,
13741 /*is_declarator=*/true,
13742 /*declares_class_or_enum=*/NULL,
13743 /*is_cv_qualifier=*/NULL);
13744 /* Leave the scope of the class. */
13745 if (type)
13746 pop_scope (type);
13748 constructor_p = !cp_parser_error_occurred (parser);
13751 else
13752 constructor_p = false;
13753 /* We did not really want to consume any tokens. */
13754 cp_parser_abort_tentative_parse (parser);
13756 return constructor_p;
13759 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13760 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13761 they must be performed once we are in the scope of the function.
13763 Returns the function defined. */
13765 static tree
13766 cp_parser_function_definition_from_specifiers_and_declarator
13767 (cp_parser* parser,
13768 tree decl_specifiers,
13769 tree attributes,
13770 tree declarator)
13772 tree fn;
13773 bool success_p;
13775 /* Begin the function-definition. */
13776 success_p = begin_function_definition (decl_specifiers,
13777 attributes,
13778 declarator);
13780 /* If there were names looked up in the decl-specifier-seq that we
13781 did not check, check them now. We must wait until we are in the
13782 scope of the function to perform the checks, since the function
13783 might be a friend. */
13784 perform_deferred_access_checks ();
13786 if (!success_p)
13788 /* If begin_function_definition didn't like the definition, skip
13789 the entire function. */
13790 error ("invalid function declaration");
13791 cp_parser_skip_to_end_of_block_or_statement (parser);
13792 fn = error_mark_node;
13794 else
13795 fn = cp_parser_function_definition_after_declarator (parser,
13796 /*inline_p=*/false);
13798 return fn;
13801 /* Parse the part of a function-definition that follows the
13802 declarator. INLINE_P is TRUE iff this function is an inline
13803 function defined with a class-specifier.
13805 Returns the function defined. */
13807 static tree
13808 cp_parser_function_definition_after_declarator (cp_parser* parser,
13809 bool inline_p)
13811 tree fn;
13812 bool ctor_initializer_p = false;
13813 bool saved_in_unbraced_linkage_specification_p;
13814 unsigned saved_num_template_parameter_lists;
13816 /* If the next token is `return', then the code may be trying to
13817 make use of the "named return value" extension that G++ used to
13818 support. */
13819 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13821 /* Consume the `return' keyword. */
13822 cp_lexer_consume_token (parser->lexer);
13823 /* Look for the identifier that indicates what value is to be
13824 returned. */
13825 cp_parser_identifier (parser);
13826 /* Issue an error message. */
13827 error ("named return values are no longer supported");
13828 /* Skip tokens until we reach the start of the function body. */
13829 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13830 cp_lexer_consume_token (parser->lexer);
13832 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13833 anything declared inside `f'. */
13834 saved_in_unbraced_linkage_specification_p
13835 = parser->in_unbraced_linkage_specification_p;
13836 parser->in_unbraced_linkage_specification_p = false;
13837 /* Inside the function, surrounding template-parameter-lists do not
13838 apply. */
13839 saved_num_template_parameter_lists
13840 = parser->num_template_parameter_lists;
13841 parser->num_template_parameter_lists = 0;
13842 /* If the next token is `try', then we are looking at a
13843 function-try-block. */
13844 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13845 ctor_initializer_p = cp_parser_function_try_block (parser);
13846 /* A function-try-block includes the function-body, so we only do
13847 this next part if we're not processing a function-try-block. */
13848 else
13849 ctor_initializer_p
13850 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13852 /* Finish the function. */
13853 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13854 (inline_p ? 2 : 0));
13855 /* Generate code for it, if necessary. */
13856 expand_or_defer_fn (fn);
13857 /* Restore the saved values. */
13858 parser->in_unbraced_linkage_specification_p
13859 = saved_in_unbraced_linkage_specification_p;
13860 parser->num_template_parameter_lists
13861 = saved_num_template_parameter_lists;
13863 return fn;
13866 /* Parse a template-declaration, assuming that the `export' (and
13867 `extern') keywords, if present, has already been scanned. MEMBER_P
13868 is as for cp_parser_template_declaration. */
13870 static void
13871 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13873 tree decl = NULL_TREE;
13874 tree parameter_list;
13875 bool friend_p = false;
13877 /* Look for the `template' keyword. */
13878 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13879 return;
13881 /* And the `<'. */
13882 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13883 return;
13885 /* Parse the template parameters. */
13886 begin_template_parm_list ();
13887 /* If the next token is `>', then we have an invalid
13888 specialization. Rather than complain about an invalid template
13889 parameter, issue an error message here. */
13890 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13892 cp_parser_error (parser, "invalid explicit specialization");
13893 parameter_list = NULL_TREE;
13895 else
13896 parameter_list = cp_parser_template_parameter_list (parser);
13897 parameter_list = end_template_parm_list (parameter_list);
13898 /* Look for the `>'. */
13899 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13900 /* We just processed one more parameter list. */
13901 ++parser->num_template_parameter_lists;
13902 /* If the next token is `template', there are more template
13903 parameters. */
13904 if (cp_lexer_next_token_is_keyword (parser->lexer,
13905 RID_TEMPLATE))
13906 cp_parser_template_declaration_after_export (parser, member_p);
13907 else
13909 decl = cp_parser_single_declaration (parser,
13910 member_p,
13911 &friend_p);
13913 /* If this is a member template declaration, let the front
13914 end know. */
13915 if (member_p && !friend_p && decl)
13916 decl = finish_member_template_decl (decl);
13917 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13918 make_friend_class (current_class_type, TREE_TYPE (decl));
13920 /* We are done with the current parameter list. */
13921 --parser->num_template_parameter_lists;
13923 /* Finish up. */
13924 finish_template_decl (parameter_list);
13926 /* Register member declarations. */
13927 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13928 finish_member_declaration (decl);
13930 /* If DECL is a function template, we must return to parse it later.
13931 (Even though there is no definition, there might be default
13932 arguments that need handling.) */
13933 if (member_p && decl
13934 && (TREE_CODE (decl) == FUNCTION_DECL
13935 || DECL_FUNCTION_TEMPLATE_P (decl)))
13936 TREE_VALUE (parser->unparsed_functions_queues)
13937 = tree_cons (NULL_TREE, decl,
13938 TREE_VALUE (parser->unparsed_functions_queues));
13941 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13942 `function-definition' sequence. MEMBER_P is true, this declaration
13943 appears in a class scope.
13945 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13946 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13948 static tree
13949 cp_parser_single_declaration (cp_parser* parser,
13950 bool member_p,
13951 bool* friend_p)
13953 bool declares_class_or_enum;
13954 tree decl = NULL_TREE;
13955 tree decl_specifiers;
13956 tree attributes;
13958 /* Parse the dependent declaration. We don't know yet
13959 whether it will be a function-definition. */
13960 cp_parser_parse_tentatively (parser);
13961 /* Defer access checks until we know what is being declared. */
13962 push_deferring_access_checks (dk_deferred);
13964 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13965 alternative. */
13966 decl_specifiers
13967 = cp_parser_decl_specifier_seq (parser,
13968 CP_PARSER_FLAGS_OPTIONAL,
13969 &attributes,
13970 &declares_class_or_enum);
13971 /* Gather up the access checks that occurred the
13972 decl-specifier-seq. */
13973 stop_deferring_access_checks ();
13975 /* Check for the declaration of a template class. */
13976 if (declares_class_or_enum)
13978 if (cp_parser_declares_only_class_p (parser))
13980 decl = shadow_tag (decl_specifiers);
13981 if (decl)
13982 decl = TYPE_NAME (decl);
13983 else
13984 decl = error_mark_node;
13987 else
13988 decl = NULL_TREE;
13989 /* If it's not a template class, try for a template function. If
13990 the next token is a `;', then this declaration does not declare
13991 anything. But, if there were errors in the decl-specifiers, then
13992 the error might well have come from an attempted class-specifier.
13993 In that case, there's no need to warn about a missing declarator. */
13994 if (!decl
13995 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13996 || !value_member (error_mark_node, decl_specifiers)))
13997 decl = cp_parser_init_declarator (parser,
13998 decl_specifiers,
13999 attributes,
14000 /*function_definition_allowed_p=*/false,
14001 member_p,
14002 /*function_definition_p=*/NULL);
14004 pop_deferring_access_checks ();
14006 /* Clear any current qualification; whatever comes next is the start
14007 of something new. */
14008 parser->scope = NULL_TREE;
14009 parser->qualifying_scope = NULL_TREE;
14010 parser->object_scope = NULL_TREE;
14011 /* Look for a trailing `;' after the declaration. */
14012 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
14013 && cp_parser_committed_to_tentative_parse (parser))
14014 cp_parser_skip_to_end_of_block_or_statement (parser);
14015 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14016 if (cp_parser_parse_definitely (parser))
14018 if (friend_p)
14019 *friend_p = cp_parser_friend_p (decl_specifiers);
14021 /* Otherwise, try a function-definition. */
14022 else
14023 decl = cp_parser_function_definition (parser, friend_p);
14025 return decl;
14028 /* Parse a functional cast to TYPE. Returns an expression
14029 representing the cast. */
14031 static tree
14032 cp_parser_functional_cast (cp_parser* parser, tree type)
14034 tree expression_list;
14036 /* Look for the opening `('. */
14037 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14038 return error_mark_node;
14039 /* If the next token is not an `)', there are arguments to the
14040 cast. */
14041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14042 expression_list = cp_parser_expression_list (parser);
14043 else
14044 expression_list = NULL_TREE;
14045 /* Look for the closing `)'. */
14046 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14048 return build_functional_cast (type, expression_list);
14051 /* MEMBER_FUNCTION is a member function, or a friend. If default
14052 arguments, or the body of the function have not yet been parsed,
14053 parse them now. */
14055 static void
14056 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14058 cp_lexer *saved_lexer;
14060 /* If this member is a template, get the underlying
14061 FUNCTION_DECL. */
14062 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14063 member_function = DECL_TEMPLATE_RESULT (member_function);
14065 /* There should not be any class definitions in progress at this
14066 point; the bodies of members are only parsed outside of all class
14067 definitions. */
14068 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14069 /* While we're parsing the member functions we might encounter more
14070 classes. We want to handle them right away, but we don't want
14071 them getting mixed up with functions that are currently in the
14072 queue. */
14073 parser->unparsed_functions_queues
14074 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14076 /* Make sure that any template parameters are in scope. */
14077 maybe_begin_member_template_processing (member_function);
14079 /* If the body of the function has not yet been parsed, parse it
14080 now. */
14081 if (DECL_PENDING_INLINE_P (member_function))
14083 tree function_scope;
14084 cp_token_cache *tokens;
14086 /* The function is no longer pending; we are processing it. */
14087 tokens = DECL_PENDING_INLINE_INFO (member_function);
14088 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14089 DECL_PENDING_INLINE_P (member_function) = 0;
14090 /* If this was an inline function in a local class, enter the scope
14091 of the containing function. */
14092 function_scope = decl_function_context (member_function);
14093 if (function_scope)
14094 push_function_context_to (function_scope);
14096 /* Save away the current lexer. */
14097 saved_lexer = parser->lexer;
14098 /* Make a new lexer to feed us the tokens saved for this function. */
14099 parser->lexer = cp_lexer_new_from_tokens (tokens);
14100 parser->lexer->next = saved_lexer;
14102 /* Set the current source position to be the location of the first
14103 token in the saved inline body. */
14104 cp_lexer_peek_token (parser->lexer);
14106 /* Let the front end know that we going to be defining this
14107 function. */
14108 start_function (NULL_TREE, member_function, NULL_TREE,
14109 SF_PRE_PARSED | SF_INCLASS_INLINE);
14111 /* Now, parse the body of the function. */
14112 cp_parser_function_definition_after_declarator (parser,
14113 /*inline_p=*/true);
14115 /* Leave the scope of the containing function. */
14116 if (function_scope)
14117 pop_function_context_from (function_scope);
14118 /* Restore the lexer. */
14119 parser->lexer = saved_lexer;
14122 /* Remove any template parameters from the symbol table. */
14123 maybe_end_member_template_processing ();
14125 /* Restore the queue. */
14126 parser->unparsed_functions_queues
14127 = TREE_CHAIN (parser->unparsed_functions_queues);
14130 /* If DECL contains any default args, remeber it on the unparsed
14131 functions queue. */
14133 static void
14134 cp_parser_save_default_args (cp_parser* parser, tree decl)
14136 tree probe;
14138 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14139 probe;
14140 probe = TREE_CHAIN (probe))
14141 if (TREE_PURPOSE (probe))
14143 TREE_PURPOSE (parser->unparsed_functions_queues)
14144 = tree_cons (NULL_TREE, decl,
14145 TREE_PURPOSE (parser->unparsed_functions_queues));
14146 break;
14148 return;
14151 /* FN is a FUNCTION_DECL which may contains a parameter with an
14152 unparsed DEFAULT_ARG. Parse the default args now. */
14154 static void
14155 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14157 cp_lexer *saved_lexer;
14158 cp_token_cache *tokens;
14159 bool saved_local_variables_forbidden_p;
14160 tree parameters;
14162 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14163 parameters;
14164 parameters = TREE_CHAIN (parameters))
14166 if (!TREE_PURPOSE (parameters)
14167 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14168 continue;
14170 /* Save away the current lexer. */
14171 saved_lexer = parser->lexer;
14172 /* Create a new one, using the tokens we have saved. */
14173 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14174 parser->lexer = cp_lexer_new_from_tokens (tokens);
14176 /* Set the current source position to be the location of the
14177 first token in the default argument. */
14178 cp_lexer_peek_token (parser->lexer);
14180 /* Local variable names (and the `this' keyword) may not appear
14181 in a default argument. */
14182 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14183 parser->local_variables_forbidden_p = true;
14184 /* Parse the assignment-expression. */
14185 if (DECL_CONTEXT (fn))
14186 push_nested_class (DECL_CONTEXT (fn));
14187 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14188 if (DECL_CONTEXT (fn))
14189 pop_nested_class ();
14191 /* Restore saved state. */
14192 parser->lexer = saved_lexer;
14193 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14197 /* Parse the operand of `sizeof' (or a similar operator). Returns
14198 either a TYPE or an expression, depending on the form of the
14199 input. The KEYWORD indicates which kind of expression we have
14200 encountered. */
14202 static tree
14203 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14205 static const char *format;
14206 tree expr = NULL_TREE;
14207 const char *saved_message;
14208 bool saved_constant_expression_p;
14210 /* Initialize FORMAT the first time we get here. */
14211 if (!format)
14212 format = "types may not be defined in `%s' expressions";
14214 /* Types cannot be defined in a `sizeof' expression. Save away the
14215 old message. */
14216 saved_message = parser->type_definition_forbidden_message;
14217 /* And create the new one. */
14218 parser->type_definition_forbidden_message
14219 = ((const char *)
14220 xmalloc (strlen (format)
14221 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14222 + 1 /* `\0' */));
14223 sprintf ((char *) parser->type_definition_forbidden_message,
14224 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14226 /* The restrictions on constant-expressions do not apply inside
14227 sizeof expressions. */
14228 saved_constant_expression_p = parser->constant_expression_p;
14229 parser->constant_expression_p = false;
14231 /* Do not actually evaluate the expression. */
14232 ++skip_evaluation;
14233 /* If it's a `(', then we might be looking at the type-id
14234 construction. */
14235 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14237 tree type;
14239 /* We can't be sure yet whether we're looking at a type-id or an
14240 expression. */
14241 cp_parser_parse_tentatively (parser);
14242 /* Consume the `('. */
14243 cp_lexer_consume_token (parser->lexer);
14244 /* Parse the type-id. */
14245 type = cp_parser_type_id (parser);
14246 /* Now, look for the trailing `)'. */
14247 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14248 /* If all went well, then we're done. */
14249 if (cp_parser_parse_definitely (parser))
14251 /* Build a list of decl-specifiers; right now, we have only
14252 a single type-specifier. */
14253 type = build_tree_list (NULL_TREE,
14254 type);
14256 /* Call grokdeclarator to figure out what type this is. */
14257 expr = grokdeclarator (NULL_TREE,
14258 type,
14259 TYPENAME,
14260 /*initialized=*/0,
14261 /*attrlist=*/NULL);
14265 /* If the type-id production did not work out, then we must be
14266 looking at the unary-expression production. */
14267 if (!expr)
14268 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14269 /* Go back to evaluating expressions. */
14270 --skip_evaluation;
14272 /* Free the message we created. */
14273 free ((char *) parser->type_definition_forbidden_message);
14274 /* And restore the old one. */
14275 parser->type_definition_forbidden_message = saved_message;
14276 parser->constant_expression_p = saved_constant_expression_p;
14278 return expr;
14281 /* If the current declaration has no declarator, return true. */
14283 static bool
14284 cp_parser_declares_only_class_p (cp_parser *parser)
14286 /* If the next token is a `;' or a `,' then there is no
14287 declarator. */
14288 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14289 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14292 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14293 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14295 static bool
14296 cp_parser_friend_p (tree decl_specifiers)
14298 while (decl_specifiers)
14300 /* See if this decl-specifier is `friend'. */
14301 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14302 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14303 return true;
14305 /* Go on to the next decl-specifier. */
14306 decl_specifiers = TREE_CHAIN (decl_specifiers);
14309 return false;
14312 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14313 issue an error message indicating that TOKEN_DESC was expected.
14315 Returns the token consumed, if the token had the appropriate type.
14316 Otherwise, returns NULL. */
14318 static cp_token *
14319 cp_parser_require (cp_parser* parser,
14320 enum cpp_ttype type,
14321 const char* token_desc)
14323 if (cp_lexer_next_token_is (parser->lexer, type))
14324 return cp_lexer_consume_token (parser->lexer);
14325 else
14327 /* Output the MESSAGE -- unless we're parsing tentatively. */
14328 if (!cp_parser_simulate_error (parser))
14329 error ("expected %s", token_desc);
14330 return NULL;
14334 /* Like cp_parser_require, except that tokens will be skipped until
14335 the desired token is found. An error message is still produced if
14336 the next token is not as expected. */
14338 static void
14339 cp_parser_skip_until_found (cp_parser* parser,
14340 enum cpp_ttype type,
14341 const char* token_desc)
14343 cp_token *token;
14344 unsigned nesting_depth = 0;
14346 if (cp_parser_require (parser, type, token_desc))
14347 return;
14349 /* Skip tokens until the desired token is found. */
14350 while (true)
14352 /* Peek at the next token. */
14353 token = cp_lexer_peek_token (parser->lexer);
14354 /* If we've reached the token we want, consume it and
14355 stop. */
14356 if (token->type == type && !nesting_depth)
14358 cp_lexer_consume_token (parser->lexer);
14359 return;
14361 /* If we've run out of tokens, stop. */
14362 if (token->type == CPP_EOF)
14363 return;
14364 if (token->type == CPP_OPEN_BRACE
14365 || token->type == CPP_OPEN_PAREN
14366 || token->type == CPP_OPEN_SQUARE)
14367 ++nesting_depth;
14368 else if (token->type == CPP_CLOSE_BRACE
14369 || token->type == CPP_CLOSE_PAREN
14370 || token->type == CPP_CLOSE_SQUARE)
14372 if (nesting_depth-- == 0)
14373 return;
14375 /* Consume this token. */
14376 cp_lexer_consume_token (parser->lexer);
14380 /* If the next token is the indicated keyword, consume it. Otherwise,
14381 issue an error message indicating that TOKEN_DESC was expected.
14383 Returns the token consumed, if the token had the appropriate type.
14384 Otherwise, returns NULL. */
14386 static cp_token *
14387 cp_parser_require_keyword (cp_parser* parser,
14388 enum rid keyword,
14389 const char* token_desc)
14391 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14393 if (token && token->keyword != keyword)
14395 dyn_string_t error_msg;
14397 /* Format the error message. */
14398 error_msg = dyn_string_new (0);
14399 dyn_string_append_cstr (error_msg, "expected ");
14400 dyn_string_append_cstr (error_msg, token_desc);
14401 cp_parser_error (parser, error_msg->s);
14402 dyn_string_delete (error_msg);
14403 return NULL;
14406 return token;
14409 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14410 function-definition. */
14412 static bool
14413 cp_parser_token_starts_function_definition_p (cp_token* token)
14415 return (/* An ordinary function-body begins with an `{'. */
14416 token->type == CPP_OPEN_BRACE
14417 /* A ctor-initializer begins with a `:'. */
14418 || token->type == CPP_COLON
14419 /* A function-try-block begins with `try'. */
14420 || token->keyword == RID_TRY
14421 /* The named return value extension begins with `return'. */
14422 || token->keyword == RID_RETURN);
14425 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14426 definition. */
14428 static bool
14429 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14431 cp_token *token;
14433 token = cp_lexer_peek_token (parser->lexer);
14434 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14437 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14438 or none_type otherwise. */
14440 static enum tag_types
14441 cp_parser_token_is_class_key (cp_token* token)
14443 switch (token->keyword)
14445 case RID_CLASS:
14446 return class_type;
14447 case RID_STRUCT:
14448 return record_type;
14449 case RID_UNION:
14450 return union_type;
14452 default:
14453 return none_type;
14457 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14459 static void
14460 cp_parser_check_class_key (enum tag_types class_key, tree type)
14462 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14463 pedwarn ("`%s' tag used in naming `%#T'",
14464 class_key == union_type ? "union"
14465 : class_key == record_type ? "struct" : "class",
14466 type);
14469 /* Look for the `template' keyword, as a syntactic disambiguator.
14470 Return TRUE iff it is present, in which case it will be
14471 consumed. */
14473 static bool
14474 cp_parser_optional_template_keyword (cp_parser *parser)
14476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14478 /* The `template' keyword can only be used within templates;
14479 outside templates the parser can always figure out what is a
14480 template and what is not. */
14481 if (!processing_template_decl)
14483 error ("`template' (as a disambiguator) is only allowed "
14484 "within templates");
14485 /* If this part of the token stream is rescanned, the same
14486 error message would be generated. So, we purge the token
14487 from the stream. */
14488 cp_lexer_purge_token (parser->lexer);
14489 return false;
14491 else
14493 /* Consume the `template' keyword. */
14494 cp_lexer_consume_token (parser->lexer);
14495 return true;
14499 return false;
14502 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14503 set PARSER->SCOPE, and perform other related actions. */
14505 static void
14506 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14508 tree value;
14509 tree check;
14511 /* Get the stored value. */
14512 value = cp_lexer_consume_token (parser->lexer)->value;
14513 /* Perform any access checks that were deferred. */
14514 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14515 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14516 /* Set the scope from the stored value. */
14517 parser->scope = TREE_VALUE (value);
14518 parser->qualifying_scope = TREE_TYPE (value);
14519 parser->object_scope = NULL_TREE;
14522 /* Add tokens to CACHE until an non-nested END token appears. */
14524 static void
14525 cp_parser_cache_group (cp_parser *parser,
14526 cp_token_cache *cache,
14527 enum cpp_ttype end,
14528 unsigned depth)
14530 while (true)
14532 cp_token *token;
14534 /* Abort a parenthesized expression if we encounter a brace. */
14535 if ((end == CPP_CLOSE_PAREN || depth == 0)
14536 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14537 return;
14538 /* Consume the next token. */
14539 token = cp_lexer_consume_token (parser->lexer);
14540 /* If we've reached the end of the file, stop. */
14541 if (token->type == CPP_EOF)
14542 return;
14543 /* Add this token to the tokens we are saving. */
14544 cp_token_cache_push_token (cache, token);
14545 /* See if it starts a new group. */
14546 if (token->type == CPP_OPEN_BRACE)
14548 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14549 if (depth == 0)
14550 return;
14552 else if (token->type == CPP_OPEN_PAREN)
14553 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14554 else if (token->type == end)
14555 return;
14559 /* Begin parsing tentatively. We always save tokens while parsing
14560 tentatively so that if the tentative parsing fails we can restore the
14561 tokens. */
14563 static void
14564 cp_parser_parse_tentatively (cp_parser* parser)
14566 /* Enter a new parsing context. */
14567 parser->context = cp_parser_context_new (parser->context);
14568 /* Begin saving tokens. */
14569 cp_lexer_save_tokens (parser->lexer);
14570 /* In order to avoid repetitive access control error messages,
14571 access checks are queued up until we are no longer parsing
14572 tentatively. */
14573 push_deferring_access_checks (dk_deferred);
14576 /* Commit to the currently active tentative parse. */
14578 static void
14579 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14581 cp_parser_context *context;
14582 cp_lexer *lexer;
14584 /* Mark all of the levels as committed. */
14585 lexer = parser->lexer;
14586 for (context = parser->context; context->next; context = context->next)
14588 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14589 break;
14590 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14591 while (!cp_lexer_saving_tokens (lexer))
14592 lexer = lexer->next;
14593 cp_lexer_commit_tokens (lexer);
14597 /* Abort the currently active tentative parse. All consumed tokens
14598 will be rolled back, and no diagnostics will be issued. */
14600 static void
14601 cp_parser_abort_tentative_parse (cp_parser* parser)
14603 cp_parser_simulate_error (parser);
14604 /* Now, pretend that we want to see if the construct was
14605 successfully parsed. */
14606 cp_parser_parse_definitely (parser);
14609 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14610 token stream. Otherwise, commit to the tokens we have consumed.
14611 Returns true if no error occurred; false otherwise. */
14613 static bool
14614 cp_parser_parse_definitely (cp_parser* parser)
14616 bool error_occurred;
14617 cp_parser_context *context;
14619 /* Remember whether or not an error ocurred, since we are about to
14620 destroy that information. */
14621 error_occurred = cp_parser_error_occurred (parser);
14622 /* Remove the topmost context from the stack. */
14623 context = parser->context;
14624 parser->context = context->next;
14625 /* If no parse errors occurred, commit to the tentative parse. */
14626 if (!error_occurred)
14628 /* Commit to the tokens read tentatively, unless that was
14629 already done. */
14630 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14631 cp_lexer_commit_tokens (parser->lexer);
14633 pop_to_parent_deferring_access_checks ();
14635 /* Otherwise, if errors occurred, roll back our state so that things
14636 are just as they were before we began the tentative parse. */
14637 else
14639 cp_lexer_rollback_tokens (parser->lexer);
14640 pop_deferring_access_checks ();
14642 /* Add the context to the front of the free list. */
14643 context->next = cp_parser_context_free_list;
14644 cp_parser_context_free_list = context;
14646 return !error_occurred;
14649 /* Returns true if we are parsing tentatively -- but have decided that
14650 we will stick with this tentative parse, even if errors occur. */
14652 static bool
14653 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14655 return (cp_parser_parsing_tentatively (parser)
14656 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14659 /* Returns nonzero iff an error has occurred during the most recent
14660 tentative parse. */
14662 static bool
14663 cp_parser_error_occurred (cp_parser* parser)
14665 return (cp_parser_parsing_tentatively (parser)
14666 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14669 /* Returns nonzero if GNU extensions are allowed. */
14671 static bool
14672 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14674 return parser->allow_gnu_extensions_p;
14679 /* The parser. */
14681 static GTY (()) cp_parser *the_parser;
14683 /* External interface. */
14685 /* Parse the entire translation unit. */
14688 yyparse (void)
14690 bool error_occurred;
14692 the_parser = cp_parser_new ();
14693 push_deferring_access_checks (flag_access_control
14694 ? dk_no_deferred : dk_no_check);
14695 error_occurred = cp_parser_translation_unit (the_parser);
14696 the_parser = NULL;
14698 finish_file ();
14700 return error_occurred;
14703 /* Clean up after parsing the entire translation unit. */
14705 void
14706 free_parser_stacks (void)
14708 /* Nothing to do. */
14711 /* This variable must be provided by every front end. */
14713 int yydebug;
14715 #include "gt-cp-parser.h"