* decl2.c (defer_fn): Set DECL_DEFER_OUTPUT.
[official-gcc.git] / gcc / cp / parser.c
blob3654adbe0a84509d9a41eb4cd2e199a821e6a7a5
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 TREE_LIST of queues of functions whose bodies have been lexed,
1299 but may not have been parsed. These functions are friends of
1300 members defined within a class-specification; they are not
1301 procssed until the class is complete. The active queue is at the
1302 front of the list.
1304 Within each queue, functions appear in the reverse order that
1305 they appeared in the source. Each TREE_VALUE is a
1306 FUNCTION_DECL of TEMPLATE_DECL corresponding to a member
1307 function. */
1308 tree unparsed_functions_queues;
1310 /* The number of classes whose definitions are currently in
1311 progress. */
1312 unsigned num_classes_being_defined;
1314 /* The number of template parameter lists that apply directly to the
1315 current declaration. */
1316 unsigned num_template_parameter_lists;
1317 } cp_parser;
1319 /* The type of a function that parses some kind of expression */
1320 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1322 /* Prototypes. */
1324 /* Constructors and destructors. */
1326 static cp_parser *cp_parser_new
1327 (void);
1329 /* Routines to parse various constructs.
1331 Those that return `tree' will return the error_mark_node (rather
1332 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1333 Sometimes, they will return an ordinary node if error-recovery was
1334 attempted, even though a parse error occurrred. So, to check
1335 whether or not a parse error occurred, you should always use
1336 cp_parser_error_occurred. If the construct is optional (indicated
1337 either by an `_opt' in the name of the function that does the
1338 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1339 the construct is not present. */
1341 /* Lexical conventions [gram.lex] */
1343 static tree cp_parser_identifier
1344 (cp_parser *);
1346 /* Basic concepts [gram.basic] */
1348 static bool cp_parser_translation_unit
1349 (cp_parser *);
1351 /* Expressions [gram.expr] */
1353 static tree cp_parser_primary_expression
1354 (cp_parser *, cp_parser_id_kind *, tree *);
1355 static tree cp_parser_id_expression
1356 (cp_parser *, bool, bool, bool *);
1357 static tree cp_parser_unqualified_id
1358 (cp_parser *, bool, bool);
1359 static tree cp_parser_nested_name_specifier_opt
1360 (cp_parser *, bool, bool, bool);
1361 static tree cp_parser_nested_name_specifier
1362 (cp_parser *, bool, bool, bool);
1363 static tree cp_parser_class_or_namespace_name
1364 (cp_parser *, bool, bool, bool, bool);
1365 static tree cp_parser_postfix_expression
1366 (cp_parser *, bool);
1367 static tree cp_parser_expression_list
1368 (cp_parser *);
1369 static void cp_parser_pseudo_destructor_name
1370 (cp_parser *, tree *, tree *);
1371 static tree cp_parser_unary_expression
1372 (cp_parser *, bool);
1373 static enum tree_code cp_parser_unary_operator
1374 (cp_token *);
1375 static tree cp_parser_new_expression
1376 (cp_parser *);
1377 static tree cp_parser_new_placement
1378 (cp_parser *);
1379 static tree cp_parser_new_type_id
1380 (cp_parser *);
1381 static tree cp_parser_new_declarator_opt
1382 (cp_parser *);
1383 static tree cp_parser_direct_new_declarator
1384 (cp_parser *);
1385 static tree cp_parser_new_initializer
1386 (cp_parser *);
1387 static tree cp_parser_delete_expression
1388 (cp_parser *);
1389 static tree cp_parser_cast_expression
1390 (cp_parser *, bool);
1391 static tree cp_parser_pm_expression
1392 (cp_parser *);
1393 static tree cp_parser_multiplicative_expression
1394 (cp_parser *);
1395 static tree cp_parser_additive_expression
1396 (cp_parser *);
1397 static tree cp_parser_shift_expression
1398 (cp_parser *);
1399 static tree cp_parser_relational_expression
1400 (cp_parser *);
1401 static tree cp_parser_equality_expression
1402 (cp_parser *);
1403 static tree cp_parser_and_expression
1404 (cp_parser *);
1405 static tree cp_parser_exclusive_or_expression
1406 (cp_parser *);
1407 static tree cp_parser_inclusive_or_expression
1408 (cp_parser *);
1409 static tree cp_parser_logical_and_expression
1410 (cp_parser *);
1411 static tree cp_parser_logical_or_expression
1412 (cp_parser *);
1413 static tree cp_parser_conditional_expression
1414 (cp_parser *);
1415 static tree cp_parser_question_colon_clause
1416 (cp_parser *, tree);
1417 static tree cp_parser_assignment_expression
1418 (cp_parser *);
1419 static enum tree_code cp_parser_assignment_operator_opt
1420 (cp_parser *);
1421 static tree cp_parser_expression
1422 (cp_parser *);
1423 static tree cp_parser_constant_expression
1424 (cp_parser *, bool, bool *);
1426 /* Statements [gram.stmt.stmt] */
1428 static void cp_parser_statement
1429 (cp_parser *);
1430 static tree cp_parser_labeled_statement
1431 (cp_parser *);
1432 static tree cp_parser_expression_statement
1433 (cp_parser *);
1434 static tree cp_parser_compound_statement
1435 (cp_parser *);
1436 static void cp_parser_statement_seq_opt
1437 (cp_parser *);
1438 static tree cp_parser_selection_statement
1439 (cp_parser *);
1440 static tree cp_parser_condition
1441 (cp_parser *);
1442 static tree cp_parser_iteration_statement
1443 (cp_parser *);
1444 static void cp_parser_for_init_statement
1445 (cp_parser *);
1446 static tree cp_parser_jump_statement
1447 (cp_parser *);
1448 static void cp_parser_declaration_statement
1449 (cp_parser *);
1451 static tree cp_parser_implicitly_scoped_statement
1452 (cp_parser *);
1453 static void cp_parser_already_scoped_statement
1454 (cp_parser *);
1456 /* Declarations [gram.dcl.dcl] */
1458 static void cp_parser_declaration_seq_opt
1459 (cp_parser *);
1460 static void cp_parser_declaration
1461 (cp_parser *);
1462 static void cp_parser_block_declaration
1463 (cp_parser *, bool);
1464 static void cp_parser_simple_declaration
1465 (cp_parser *, bool);
1466 static tree cp_parser_decl_specifier_seq
1467 (cp_parser *, cp_parser_flags, tree *, bool *);
1468 static tree cp_parser_storage_class_specifier_opt
1469 (cp_parser *);
1470 static tree cp_parser_function_specifier_opt
1471 (cp_parser *);
1472 static tree cp_parser_type_specifier
1473 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1474 static tree cp_parser_simple_type_specifier
1475 (cp_parser *, cp_parser_flags);
1476 static tree cp_parser_type_name
1477 (cp_parser *);
1478 static tree cp_parser_elaborated_type_specifier
1479 (cp_parser *, bool, bool);
1480 static tree cp_parser_enum_specifier
1481 (cp_parser *);
1482 static void cp_parser_enumerator_list
1483 (cp_parser *, tree);
1484 static void cp_parser_enumerator_definition
1485 (cp_parser *, tree);
1486 static tree cp_parser_namespace_name
1487 (cp_parser *);
1488 static void cp_parser_namespace_definition
1489 (cp_parser *);
1490 static void cp_parser_namespace_body
1491 (cp_parser *);
1492 static tree cp_parser_qualified_namespace_specifier
1493 (cp_parser *);
1494 static void cp_parser_namespace_alias_definition
1495 (cp_parser *);
1496 static void cp_parser_using_declaration
1497 (cp_parser *);
1498 static void cp_parser_using_directive
1499 (cp_parser *);
1500 static void cp_parser_asm_definition
1501 (cp_parser *);
1502 static void cp_parser_linkage_specification
1503 (cp_parser *);
1505 /* Declarators [gram.dcl.decl] */
1507 static tree cp_parser_init_declarator
1508 (cp_parser *, tree, tree, bool, bool, bool *);
1509 static tree cp_parser_declarator
1510 (cp_parser *, cp_parser_declarator_kind, bool *);
1511 static tree cp_parser_direct_declarator
1512 (cp_parser *, cp_parser_declarator_kind, bool *);
1513 static enum tree_code cp_parser_ptr_operator
1514 (cp_parser *, tree *, tree *);
1515 static tree cp_parser_cv_qualifier_seq_opt
1516 (cp_parser *);
1517 static tree cp_parser_cv_qualifier_opt
1518 (cp_parser *);
1519 static tree cp_parser_declarator_id
1520 (cp_parser *);
1521 static tree cp_parser_type_id
1522 (cp_parser *);
1523 static tree cp_parser_type_specifier_seq
1524 (cp_parser *);
1525 static tree cp_parser_parameter_declaration_clause
1526 (cp_parser *);
1527 static tree cp_parser_parameter_declaration_list
1528 (cp_parser *);
1529 static tree cp_parser_parameter_declaration
1530 (cp_parser *, bool);
1531 static tree cp_parser_function_definition
1532 (cp_parser *, bool *);
1533 static void cp_parser_function_body
1534 (cp_parser *);
1535 static tree cp_parser_initializer
1536 (cp_parser *, bool *);
1537 static tree cp_parser_initializer_clause
1538 (cp_parser *);
1539 static tree cp_parser_initializer_list
1540 (cp_parser *);
1542 static bool cp_parser_ctor_initializer_opt_and_function_body
1543 (cp_parser *);
1545 /* Classes [gram.class] */
1547 static tree cp_parser_class_name
1548 (cp_parser *, bool, bool, bool, bool, bool);
1549 static tree cp_parser_class_specifier
1550 (cp_parser *);
1551 static tree cp_parser_class_head
1552 (cp_parser *, bool *);
1553 static enum tag_types cp_parser_class_key
1554 (cp_parser *);
1555 static void cp_parser_member_specification_opt
1556 (cp_parser *);
1557 static void cp_parser_member_declaration
1558 (cp_parser *);
1559 static tree cp_parser_pure_specifier
1560 (cp_parser *);
1561 static tree cp_parser_constant_initializer
1562 (cp_parser *);
1564 /* Derived classes [gram.class.derived] */
1566 static tree cp_parser_base_clause
1567 (cp_parser *);
1568 static tree cp_parser_base_specifier
1569 (cp_parser *);
1571 /* Special member functions [gram.special] */
1573 static tree cp_parser_conversion_function_id
1574 (cp_parser *);
1575 static tree cp_parser_conversion_type_id
1576 (cp_parser *);
1577 static tree cp_parser_conversion_declarator_opt
1578 (cp_parser *);
1579 static bool cp_parser_ctor_initializer_opt
1580 (cp_parser *);
1581 static void cp_parser_mem_initializer_list
1582 (cp_parser *);
1583 static tree cp_parser_mem_initializer
1584 (cp_parser *);
1585 static tree cp_parser_mem_initializer_id
1586 (cp_parser *);
1588 /* Overloading [gram.over] */
1590 static tree cp_parser_operator_function_id
1591 (cp_parser *);
1592 static tree cp_parser_operator
1593 (cp_parser *);
1595 /* Templates [gram.temp] */
1597 static void cp_parser_template_declaration
1598 (cp_parser *, bool);
1599 static tree cp_parser_template_parameter_list
1600 (cp_parser *);
1601 static tree cp_parser_template_parameter
1602 (cp_parser *);
1603 static tree cp_parser_type_parameter
1604 (cp_parser *);
1605 static tree cp_parser_template_id
1606 (cp_parser *, bool, bool);
1607 static tree cp_parser_template_name
1608 (cp_parser *, bool, bool);
1609 static tree cp_parser_template_argument_list
1610 (cp_parser *);
1611 static tree cp_parser_template_argument
1612 (cp_parser *);
1613 static void cp_parser_explicit_instantiation
1614 (cp_parser *);
1615 static void cp_parser_explicit_specialization
1616 (cp_parser *);
1618 /* Exception handling [gram.exception] */
1620 static tree cp_parser_try_block
1621 (cp_parser *);
1622 static bool cp_parser_function_try_block
1623 (cp_parser *);
1624 static void cp_parser_handler_seq
1625 (cp_parser *);
1626 static void cp_parser_handler
1627 (cp_parser *);
1628 static tree cp_parser_exception_declaration
1629 (cp_parser *);
1630 static tree cp_parser_throw_expression
1631 (cp_parser *);
1632 static tree cp_parser_exception_specification_opt
1633 (cp_parser *);
1634 static tree cp_parser_type_id_list
1635 (cp_parser *);
1637 /* GNU Extensions */
1639 static tree cp_parser_asm_specification_opt
1640 (cp_parser *);
1641 static tree cp_parser_asm_operand_list
1642 (cp_parser *);
1643 static tree cp_parser_asm_clobber_list
1644 (cp_parser *);
1645 static tree cp_parser_attributes_opt
1646 (cp_parser *);
1647 static tree cp_parser_attribute_list
1648 (cp_parser *);
1649 static bool cp_parser_extension_opt
1650 (cp_parser *, int *);
1651 static void cp_parser_label_declaration
1652 (cp_parser *);
1654 /* Utility Routines */
1656 static tree cp_parser_lookup_name
1657 (cp_parser *, tree, bool, bool, bool);
1658 static tree cp_parser_lookup_name_simple
1659 (cp_parser *, tree);
1660 static tree cp_parser_maybe_treat_template_as_class
1661 (tree, bool);
1662 static bool cp_parser_check_declarator_template_parameters
1663 (cp_parser *, tree);
1664 static bool cp_parser_check_template_parameters
1665 (cp_parser *, unsigned);
1666 static tree cp_parser_binary_expression
1667 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1668 static tree cp_parser_global_scope_opt
1669 (cp_parser *, bool);
1670 static bool cp_parser_constructor_declarator_p
1671 (cp_parser *, bool);
1672 static tree cp_parser_function_definition_from_specifiers_and_declarator
1673 (cp_parser *, tree, tree, tree);
1674 static tree cp_parser_function_definition_after_declarator
1675 (cp_parser *, bool);
1676 static void cp_parser_template_declaration_after_export
1677 (cp_parser *, bool);
1678 static tree cp_parser_single_declaration
1679 (cp_parser *, bool, bool *);
1680 static tree cp_parser_functional_cast
1681 (cp_parser *, tree);
1682 static void cp_parser_late_parsing_for_member
1683 (cp_parser *, tree);
1684 static void cp_parser_late_parsing_default_args
1685 (cp_parser *, tree);
1686 static tree cp_parser_sizeof_operand
1687 (cp_parser *, enum rid);
1688 static bool cp_parser_declares_only_class_p
1689 (cp_parser *);
1690 static bool cp_parser_friend_p
1691 (tree);
1692 static cp_token *cp_parser_require
1693 (cp_parser *, enum cpp_ttype, const char *);
1694 static cp_token *cp_parser_require_keyword
1695 (cp_parser *, enum rid, const char *);
1696 static bool cp_parser_token_starts_function_definition_p
1697 (cp_token *);
1698 static bool cp_parser_next_token_starts_class_definition_p
1699 (cp_parser *);
1700 static enum tag_types cp_parser_token_is_class_key
1701 (cp_token *);
1702 static void cp_parser_check_class_key
1703 (enum tag_types, tree type);
1704 static bool cp_parser_optional_template_keyword
1705 (cp_parser *);
1706 static void cp_parser_pre_parsed_nested_name_specifier
1707 (cp_parser *);
1708 static void cp_parser_cache_group
1709 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1710 static void cp_parser_parse_tentatively
1711 (cp_parser *);
1712 static void cp_parser_commit_to_tentative_parse
1713 (cp_parser *);
1714 static void cp_parser_abort_tentative_parse
1715 (cp_parser *);
1716 static bool cp_parser_parse_definitely
1717 (cp_parser *);
1718 static inline bool cp_parser_parsing_tentatively
1719 (cp_parser *);
1720 static bool cp_parser_committed_to_tentative_parse
1721 (cp_parser *);
1722 static void cp_parser_error
1723 (cp_parser *, const char *);
1724 static bool cp_parser_simulate_error
1725 (cp_parser *);
1726 static void cp_parser_check_type_definition
1727 (cp_parser *);
1728 static tree cp_parser_non_constant_expression
1729 (const char *);
1730 static tree cp_parser_non_constant_id_expression
1731 (tree);
1732 static bool cp_parser_diagnose_invalid_type_name
1733 (cp_parser *);
1734 static bool cp_parser_skip_to_closing_parenthesis
1735 (cp_parser *);
1736 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1737 (cp_parser *);
1738 static void cp_parser_skip_to_end_of_statement
1739 (cp_parser *);
1740 static void cp_parser_consume_semicolon_at_end_of_statement
1741 (cp_parser *);
1742 static void cp_parser_skip_to_end_of_block_or_statement
1743 (cp_parser *);
1744 static void cp_parser_skip_to_closing_brace
1745 (cp_parser *);
1746 static void cp_parser_skip_until_found
1747 (cp_parser *, enum cpp_ttype, const char *);
1748 static bool cp_parser_error_occurred
1749 (cp_parser *);
1750 static bool cp_parser_allow_gnu_extensions_p
1751 (cp_parser *);
1752 static bool cp_parser_is_string_literal
1753 (cp_token *);
1754 static bool cp_parser_is_keyword
1755 (cp_token *, enum rid);
1756 static tree cp_parser_scope_through_which_access_occurs
1757 (tree, tree, tree);
1759 /* Returns nonzero if we are parsing tentatively. */
1761 static inline bool
1762 cp_parser_parsing_tentatively (cp_parser* parser)
1764 return parser->context->next != NULL;
1767 /* Returns nonzero if TOKEN is a string literal. */
1769 static bool
1770 cp_parser_is_string_literal (cp_token* token)
1772 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1775 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1777 static bool
1778 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1780 return token->keyword == keyword;
1783 /* Returns the scope through which DECL is being accessed, or
1784 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
1785 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
1786 or `x', respectively. If the DECL was named as `A::B' then
1787 NESTED_NAME_SPECIFIER is `A'. */
1789 static tree
1790 cp_parser_scope_through_which_access_occurs (tree decl,
1791 tree object_type,
1792 tree nested_name_specifier)
1794 tree scope;
1795 tree qualifying_type = NULL_TREE;
1797 /* Determine the SCOPE of DECL. */
1798 scope = context_for_name_lookup (decl);
1799 /* If the SCOPE is not a type, then DECL is not a member. */
1800 if (!TYPE_P (scope))
1801 return NULL_TREE;
1802 /* Figure out the type through which DECL is being accessed. */
1803 if (object_type
1804 /* OBJECT_TYPE might not be a class type; consider:
1806 class A { typedef int I; };
1807 I *p;
1808 p->A::I::~I();
1810 In this case, we will have "A::I" as the DECL, but "I" as the
1811 OBJECT_TYPE. */
1812 && CLASS_TYPE_P (object_type)
1813 && DERIVED_FROM_P (scope, object_type))
1814 /* If we are processing a `->' or `.' expression, use the type of the
1815 left-hand side. */
1816 qualifying_type = object_type;
1817 else if (nested_name_specifier)
1819 /* If the reference is to a non-static member of the
1820 current class, treat it as if it were referenced through
1821 `this'. */
1822 if (DECL_NONSTATIC_MEMBER_P (decl)
1823 && current_class_ptr
1824 && DERIVED_FROM_P (scope, current_class_type))
1825 qualifying_type = current_class_type;
1826 /* Otherwise, use the type indicated by the
1827 nested-name-specifier. */
1828 else
1829 qualifying_type = nested_name_specifier;
1831 else
1832 /* Otherwise, the name must be from the current class or one of
1833 its bases. */
1834 qualifying_type = currently_open_derived_class (scope);
1836 return qualifying_type;
1839 /* Issue the indicated error MESSAGE. */
1841 static void
1842 cp_parser_error (cp_parser* parser, const char* message)
1844 /* Output the MESSAGE -- unless we're parsing tentatively. */
1845 if (!cp_parser_simulate_error (parser))
1846 error (message);
1849 /* If we are parsing tentatively, remember that an error has occurred
1850 during this tentative parse. Returns true if the error was
1851 simulated; false if a messgae should be issued by the caller. */
1853 static bool
1854 cp_parser_simulate_error (cp_parser* parser)
1856 if (cp_parser_parsing_tentatively (parser)
1857 && !cp_parser_committed_to_tentative_parse (parser))
1859 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1860 return true;
1862 return false;
1865 /* This function is called when a type is defined. If type
1866 definitions are forbidden at this point, an error message is
1867 issued. */
1869 static void
1870 cp_parser_check_type_definition (cp_parser* parser)
1872 /* If types are forbidden here, issue a message. */
1873 if (parser->type_definition_forbidden_message)
1874 /* Use `%s' to print the string in case there are any escape
1875 characters in the message. */
1876 error ("%s", parser->type_definition_forbidden_message);
1879 /* Issue an eror message about the fact that THING appeared in a
1880 constant-expression. Returns ERROR_MARK_NODE. */
1882 static tree
1883 cp_parser_non_constant_expression (const char *thing)
1885 error ("%s cannot appear in a constant-expression", thing);
1886 return error_mark_node;
1889 /* Issue an eror message about the fact that DECL appeared in a
1890 constant-expression. Returns ERROR_MARK_NODE. */
1892 static tree
1893 cp_parser_non_constant_id_expression (tree decl)
1895 error ("`%D' cannot appear in a constant-expression", decl);
1896 return error_mark_node;
1899 /* Check for a common situation where a type-name should be present,
1900 but is not, and issue a sensible error message. Returns true if an
1901 invalid type-name was detected. */
1903 static bool
1904 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1906 /* If the next two tokens are both identifiers, the code is
1907 erroneous. The usual cause of this situation is code like:
1909 T t;
1911 where "T" should name a type -- but does not. */
1912 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1913 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1915 tree name;
1917 /* If parsing tentatively, we should commit; we really are
1918 looking at a declaration. */
1919 /* Consume the first identifier. */
1920 name = cp_lexer_consume_token (parser->lexer)->value;
1921 /* Issue an error message. */
1922 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1923 /* If we're in a template class, it's possible that the user was
1924 referring to a type from a base class. For example:
1926 template <typename T> struct A { typedef T X; };
1927 template <typename T> struct B : public A<T> { X x; };
1929 The user should have said "typename A<T>::X". */
1930 if (processing_template_decl && current_class_type)
1932 tree b;
1934 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1936 b = TREE_CHAIN (b))
1938 tree base_type = BINFO_TYPE (b);
1939 if (CLASS_TYPE_P (base_type)
1940 && dependent_type_p (base_type))
1942 tree field;
1943 /* Go from a particular instantiation of the
1944 template (which will have an empty TYPE_FIELDs),
1945 to the main version. */
1946 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1947 for (field = TYPE_FIELDS (base_type);
1948 field;
1949 field = TREE_CHAIN (field))
1950 if (TREE_CODE (field) == TYPE_DECL
1951 && DECL_NAME (field) == name)
1953 error ("(perhaps `typename %T::%s' was intended)",
1954 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1955 break;
1957 if (field)
1958 break;
1962 /* Skip to the end of the declaration; there's no point in
1963 trying to process it. */
1964 cp_parser_skip_to_end_of_statement (parser);
1966 return true;
1969 return false;
1972 /* Consume tokens up to, and including, the next non-nested closing `)'.
1973 Returns TRUE iff we found a closing `)'. */
1975 static bool
1976 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
1978 unsigned nesting_depth = 0;
1980 while (true)
1982 cp_token *token;
1984 /* If we've run out of tokens, then there is no closing `)'. */
1985 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1986 return false;
1987 /* Consume the token. */
1988 token = cp_lexer_consume_token (parser->lexer);
1989 /* If it is an `(', we have entered another level of nesting. */
1990 if (token->type == CPP_OPEN_PAREN)
1991 ++nesting_depth;
1992 /* If it is a `)', then we might be done. */
1993 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
1994 return true;
1998 /* Consume tokens until the next token is a `)', or a `,'. Returns
1999 TRUE if the next token is a `,'. */
2001 static bool
2002 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2004 unsigned nesting_depth = 0;
2006 while (true)
2008 cp_token *token = cp_lexer_peek_token (parser->lexer);
2010 /* If we've run out of tokens, then there is no closing `)'. */
2011 if (token->type == CPP_EOF)
2012 return false;
2013 /* If it is a `,' stop. */
2014 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2015 return true;
2016 /* If it is a `)', stop. */
2017 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2018 return false;
2019 /* If it is an `(', we have entered another level of nesting. */
2020 else if (token->type == CPP_OPEN_PAREN)
2021 ++nesting_depth;
2022 /* Consume the token. */
2023 token = cp_lexer_consume_token (parser->lexer);
2027 /* Consume tokens until we reach the end of the current statement.
2028 Normally, that will be just before consuming a `;'. However, if a
2029 non-nested `}' comes first, then we stop before consuming that. */
2031 static void
2032 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2034 unsigned nesting_depth = 0;
2036 while (true)
2038 cp_token *token;
2040 /* Peek at the next token. */
2041 token = cp_lexer_peek_token (parser->lexer);
2042 /* If we've run out of tokens, stop. */
2043 if (token->type == CPP_EOF)
2044 break;
2045 /* If the next token is a `;', we have reached the end of the
2046 statement. */
2047 if (token->type == CPP_SEMICOLON && !nesting_depth)
2048 break;
2049 /* If the next token is a non-nested `}', then we have reached
2050 the end of the current block. */
2051 if (token->type == CPP_CLOSE_BRACE)
2053 /* If this is a non-nested `}', stop before consuming it.
2054 That way, when confronted with something like:
2056 { 3 + }
2058 we stop before consuming the closing `}', even though we
2059 have not yet reached a `;'. */
2060 if (nesting_depth == 0)
2061 break;
2062 /* If it is the closing `}' for a block that we have
2063 scanned, stop -- but only after consuming the token.
2064 That way given:
2066 void f g () { ... }
2067 typedef int I;
2069 we will stop after the body of the erroneously declared
2070 function, but before consuming the following `typedef'
2071 declaration. */
2072 if (--nesting_depth == 0)
2074 cp_lexer_consume_token (parser->lexer);
2075 break;
2078 /* If it the next token is a `{', then we are entering a new
2079 block. Consume the entire block. */
2080 else if (token->type == CPP_OPEN_BRACE)
2081 ++nesting_depth;
2082 /* Consume the token. */
2083 cp_lexer_consume_token (parser->lexer);
2087 /* This function is called at the end of a statement or declaration.
2088 If the next token is a semicolon, it is consumed; otherwise, error
2089 recovery is attempted. */
2091 static void
2092 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2094 /* Look for the trailing `;'. */
2095 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2097 /* If there is additional (erroneous) input, skip to the end of
2098 the statement. */
2099 cp_parser_skip_to_end_of_statement (parser);
2100 /* If the next token is now a `;', consume it. */
2101 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2102 cp_lexer_consume_token (parser->lexer);
2106 /* Skip tokens until we have consumed an entire block, or until we
2107 have consumed a non-nested `;'. */
2109 static void
2110 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2112 unsigned nesting_depth = 0;
2114 while (true)
2116 cp_token *token;
2118 /* Peek at the next token. */
2119 token = cp_lexer_peek_token (parser->lexer);
2120 /* If we've run out of tokens, stop. */
2121 if (token->type == CPP_EOF)
2122 break;
2123 /* If the next token is a `;', we have reached the end of the
2124 statement. */
2125 if (token->type == CPP_SEMICOLON && !nesting_depth)
2127 /* Consume the `;'. */
2128 cp_lexer_consume_token (parser->lexer);
2129 break;
2131 /* Consume the token. */
2132 token = cp_lexer_consume_token (parser->lexer);
2133 /* If the next token is a non-nested `}', then we have reached
2134 the end of the current block. */
2135 if (token->type == CPP_CLOSE_BRACE
2136 && (nesting_depth == 0 || --nesting_depth == 0))
2137 break;
2138 /* If it the next token is a `{', then we are entering a new
2139 block. Consume the entire block. */
2140 if (token->type == CPP_OPEN_BRACE)
2141 ++nesting_depth;
2145 /* Skip tokens until a non-nested closing curly brace is the next
2146 token. */
2148 static void
2149 cp_parser_skip_to_closing_brace (cp_parser *parser)
2151 unsigned nesting_depth = 0;
2153 while (true)
2155 cp_token *token;
2157 /* Peek at the next token. */
2158 token = cp_lexer_peek_token (parser->lexer);
2159 /* If we've run out of tokens, stop. */
2160 if (token->type == CPP_EOF)
2161 break;
2162 /* If the next token is a non-nested `}', then we have reached
2163 the end of the current block. */
2164 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2165 break;
2166 /* If it the next token is a `{', then we are entering a new
2167 block. Consume the entire block. */
2168 else if (token->type == CPP_OPEN_BRACE)
2169 ++nesting_depth;
2170 /* Consume the token. */
2171 cp_lexer_consume_token (parser->lexer);
2175 /* Create a new C++ parser. */
2177 static cp_parser *
2178 cp_parser_new (void)
2180 cp_parser *parser;
2181 cp_lexer *lexer;
2183 /* cp_lexer_new_main is called before calling ggc_alloc because
2184 cp_lexer_new_main might load a PCH file. */
2185 lexer = cp_lexer_new_main ();
2187 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2188 parser->lexer = lexer;
2189 parser->context = cp_parser_context_new (NULL);
2191 /* For now, we always accept GNU extensions. */
2192 parser->allow_gnu_extensions_p = 1;
2194 /* The `>' token is a greater-than operator, not the end of a
2195 template-id. */
2196 parser->greater_than_is_operator_p = true;
2198 parser->default_arg_ok_p = true;
2200 /* We are not parsing a constant-expression. */
2201 parser->constant_expression_p = false;
2202 parser->allow_non_constant_expression_p = false;
2203 parser->non_constant_expression_p = false;
2205 /* Local variable names are not forbidden. */
2206 parser->local_variables_forbidden_p = false;
2208 /* We are not procesing an `extern "C"' declaration. */
2209 parser->in_unbraced_linkage_specification_p = false;
2211 /* We are not processing a declarator. */
2212 parser->in_declarator_p = false;
2214 /* The unparsed function queue is empty. */
2215 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2217 /* There are no classes being defined. */
2218 parser->num_classes_being_defined = 0;
2220 /* No template parameters apply. */
2221 parser->num_template_parameter_lists = 0;
2223 return parser;
2226 /* Lexical conventions [gram.lex] */
2228 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2229 identifier. */
2231 static tree
2232 cp_parser_identifier (cp_parser* parser)
2234 cp_token *token;
2236 /* Look for the identifier. */
2237 token = cp_parser_require (parser, CPP_NAME, "identifier");
2238 /* Return the value. */
2239 return token ? token->value : error_mark_node;
2242 /* Basic concepts [gram.basic] */
2244 /* Parse a translation-unit.
2246 translation-unit:
2247 declaration-seq [opt]
2249 Returns TRUE if all went well. */
2251 static bool
2252 cp_parser_translation_unit (cp_parser* parser)
2254 while (true)
2256 cp_parser_declaration_seq_opt (parser);
2258 /* If there are no tokens left then all went well. */
2259 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2260 break;
2262 /* Otherwise, issue an error message. */
2263 cp_parser_error (parser, "expected declaration");
2264 return false;
2267 /* Consume the EOF token. */
2268 cp_parser_require (parser, CPP_EOF, "end-of-file");
2270 /* Finish up. */
2271 finish_translation_unit ();
2273 /* All went well. */
2274 return true;
2277 /* Expressions [gram.expr] */
2279 /* Parse a primary-expression.
2281 primary-expression:
2282 literal
2283 this
2284 ( expression )
2285 id-expression
2287 GNU Extensions:
2289 primary-expression:
2290 ( compound-statement )
2291 __builtin_va_arg ( assignment-expression , type-id )
2293 literal:
2294 __null
2296 Returns a representation of the expression.
2298 *IDK indicates what kind of id-expression (if any) was present.
2300 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2301 used as the operand of a pointer-to-member. In that case,
2302 *QUALIFYING_CLASS gives the class that is used as the qualifying
2303 class in the pointer-to-member. */
2305 static tree
2306 cp_parser_primary_expression (cp_parser *parser,
2307 cp_parser_id_kind *idk,
2308 tree *qualifying_class)
2310 cp_token *token;
2312 /* Assume the primary expression is not an id-expression. */
2313 *idk = CP_PARSER_ID_KIND_NONE;
2314 /* And that it cannot be used as pointer-to-member. */
2315 *qualifying_class = NULL_TREE;
2317 /* Peek at the next token. */
2318 token = cp_lexer_peek_token (parser->lexer);
2319 switch (token->type)
2321 /* literal:
2322 integer-literal
2323 character-literal
2324 floating-literal
2325 string-literal
2326 boolean-literal */
2327 case CPP_CHAR:
2328 case CPP_WCHAR:
2329 case CPP_STRING:
2330 case CPP_WSTRING:
2331 case CPP_NUMBER:
2332 token = cp_lexer_consume_token (parser->lexer);
2333 return token->value;
2335 case CPP_OPEN_PAREN:
2337 tree expr;
2338 bool saved_greater_than_is_operator_p;
2340 /* Consume the `('. */
2341 cp_lexer_consume_token (parser->lexer);
2342 /* Within a parenthesized expression, a `>' token is always
2343 the greater-than operator. */
2344 saved_greater_than_is_operator_p
2345 = parser->greater_than_is_operator_p;
2346 parser->greater_than_is_operator_p = true;
2347 /* If we see `( { ' then we are looking at the beginning of
2348 a GNU statement-expression. */
2349 if (cp_parser_allow_gnu_extensions_p (parser)
2350 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2352 /* Statement-expressions are not allowed by the standard. */
2353 if (pedantic)
2354 pedwarn ("ISO C++ forbids braced-groups within expressions");
2356 /* And they're not allowed outside of a function-body; you
2357 cannot, for example, write:
2359 int i = ({ int j = 3; j + 1; });
2361 at class or namespace scope. */
2362 if (!at_function_scope_p ())
2363 error ("statement-expressions are allowed only inside functions");
2364 /* Start the statement-expression. */
2365 expr = begin_stmt_expr ();
2366 /* Parse the compound-statement. */
2367 cp_parser_compound_statement (parser);
2368 /* Finish up. */
2369 expr = finish_stmt_expr (expr);
2371 else
2373 /* Parse the parenthesized expression. */
2374 expr = cp_parser_expression (parser);
2375 /* Let the front end know that this expression was
2376 enclosed in parentheses. This matters in case, for
2377 example, the expression is of the form `A::B', since
2378 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2379 not. */
2380 finish_parenthesized_expr (expr);
2382 /* The `>' token might be the end of a template-id or
2383 template-parameter-list now. */
2384 parser->greater_than_is_operator_p
2385 = saved_greater_than_is_operator_p;
2386 /* Consume the `)'. */
2387 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2388 cp_parser_skip_to_end_of_statement (parser);
2390 return expr;
2393 case CPP_KEYWORD:
2394 switch (token->keyword)
2396 /* These two are the boolean literals. */
2397 case RID_TRUE:
2398 cp_lexer_consume_token (parser->lexer);
2399 return boolean_true_node;
2400 case RID_FALSE:
2401 cp_lexer_consume_token (parser->lexer);
2402 return boolean_false_node;
2404 /* The `__null' literal. */
2405 case RID_NULL:
2406 cp_lexer_consume_token (parser->lexer);
2407 return null_node;
2409 /* Recognize the `this' keyword. */
2410 case RID_THIS:
2411 cp_lexer_consume_token (parser->lexer);
2412 if (parser->local_variables_forbidden_p)
2414 error ("`this' may not be used in this context");
2415 return error_mark_node;
2417 /* Pointers cannot appear in constant-expressions. */
2418 if (parser->constant_expression_p)
2420 if (!parser->allow_non_constant_expression_p)
2421 return cp_parser_non_constant_expression ("`this'");
2422 parser->non_constant_expression_p = true;
2424 return finish_this_expr ();
2426 /* The `operator' keyword can be the beginning of an
2427 id-expression. */
2428 case RID_OPERATOR:
2429 goto id_expression;
2431 case RID_FUNCTION_NAME:
2432 case RID_PRETTY_FUNCTION_NAME:
2433 case RID_C99_FUNCTION_NAME:
2434 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2435 __func__ are the names of variables -- but they are
2436 treated specially. Therefore, they are handled here,
2437 rather than relying on the generic id-expression logic
2438 below. Gramatically, these names are id-expressions.
2440 Consume the token. */
2441 token = cp_lexer_consume_token (parser->lexer);
2442 /* Look up the name. */
2443 return finish_fname (token->value);
2445 case RID_VA_ARG:
2447 tree expression;
2448 tree type;
2450 /* The `__builtin_va_arg' construct is used to handle
2451 `va_arg'. Consume the `__builtin_va_arg' token. */
2452 cp_lexer_consume_token (parser->lexer);
2453 /* Look for the opening `('. */
2454 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2455 /* Now, parse the assignment-expression. */
2456 expression = cp_parser_assignment_expression (parser);
2457 /* Look for the `,'. */
2458 cp_parser_require (parser, CPP_COMMA, "`,'");
2459 /* Parse the type-id. */
2460 type = cp_parser_type_id (parser);
2461 /* Look for the closing `)'. */
2462 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2463 /* Using `va_arg' in a constant-expression is not
2464 allowed. */
2465 if (parser->constant_expression_p)
2467 if (!parser->allow_non_constant_expression_p)
2468 return cp_parser_non_constant_expression ("`va_arg'");
2469 parser->non_constant_expression_p = true;
2471 return build_x_va_arg (expression, type);
2474 default:
2475 cp_parser_error (parser, "expected primary-expression");
2476 return error_mark_node;
2478 /* Fall through. */
2480 /* An id-expression can start with either an identifier, a
2481 `::' as the beginning of a qualified-id, or the "operator"
2482 keyword. */
2483 case CPP_NAME:
2484 case CPP_SCOPE:
2485 case CPP_TEMPLATE_ID:
2486 case CPP_NESTED_NAME_SPECIFIER:
2488 tree id_expression;
2489 tree decl;
2491 id_expression:
2492 /* Parse the id-expression. */
2493 id_expression
2494 = cp_parser_id_expression (parser,
2495 /*template_keyword_p=*/false,
2496 /*check_dependency_p=*/true,
2497 /*template_p=*/NULL);
2498 if (id_expression == error_mark_node)
2499 return error_mark_node;
2500 /* If we have a template-id, then no further lookup is
2501 required. If the template-id was for a template-class, we
2502 will sometimes have a TYPE_DECL at this point. */
2503 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2504 || TREE_CODE (id_expression) == TYPE_DECL)
2505 decl = id_expression;
2506 /* Look up the name. */
2507 else
2509 decl = cp_parser_lookup_name_simple (parser, id_expression);
2510 /* If name lookup gives us a SCOPE_REF, then the
2511 qualifying scope was dependent. Just propagate the
2512 name. */
2513 if (TREE_CODE (decl) == SCOPE_REF)
2515 if (TYPE_P (TREE_OPERAND (decl, 0)))
2516 *qualifying_class = TREE_OPERAND (decl, 0);
2517 /* Since this name was dependent, the expression isn't
2518 constant -- yet. No error is issued because it
2519 might be constant when things are instantiated. */
2520 if (parser->constant_expression_p)
2521 parser->non_constant_expression_p = true;
2522 return decl;
2524 /* Check to see if DECL is a local variable in a context
2525 where that is forbidden. */
2526 if (parser->local_variables_forbidden_p
2527 && local_variable_p (decl))
2529 /* It might be that we only found DECL because we are
2530 trying to be generous with pre-ISO scoping rules.
2531 For example, consider:
2533 int i;
2534 void g() {
2535 for (int i = 0; i < 10; ++i) {}
2536 extern void f(int j = i);
2539 Here, name look up will originally find the out
2540 of scope `i'. We need to issue a warning message,
2541 but then use the global `i'. */
2542 decl = check_for_out_of_scope_variable (decl);
2543 if (local_variable_p (decl))
2545 error ("local variable `%D' may not appear in this context",
2546 decl);
2547 return error_mark_node;
2551 if (decl == error_mark_node)
2553 /* Name lookup failed. */
2554 if (!parser->scope
2555 && processing_template_decl)
2557 /* Unqualified name lookup failed while processing a
2558 template. */
2559 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2560 /* If the next token is a parenthesis, assume that
2561 Koenig lookup will succeed when instantiating the
2562 template. */
2563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
2564 return build_min_nt (LOOKUP_EXPR, id_expression);
2565 /* If we're not doing Koenig lookup, issue an error. */
2566 error ("`%D' has not been declared", id_expression);
2567 return error_mark_node;
2569 else if (parser->scope
2570 && (!TYPE_P (parser->scope)
2571 || !dependent_type_p (parser->scope)))
2573 /* Qualified name lookup failed, and the
2574 qualifying name was not a dependent type. That
2575 is always an error. */
2576 if (TYPE_P (parser->scope)
2577 && !COMPLETE_TYPE_P (parser->scope))
2578 error ("incomplete type `%T' used in nested name "
2579 "specifier",
2580 parser->scope);
2581 else if (parser->scope != global_namespace)
2582 error ("`%D' is not a member of `%D'",
2583 id_expression, parser->scope);
2584 else
2585 error ("`::%D' has not been declared", id_expression);
2586 return error_mark_node;
2588 else if (!parser->scope && !processing_template_decl)
2590 /* It may be resolvable as a koenig lookup function
2591 call. */
2592 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2593 return id_expression;
2596 /* If DECL is a variable that would be out of scope under
2597 ANSI/ISO rules, but in scope in the ARM, name lookup
2598 will succeed. Issue a diagnostic here. */
2599 else
2600 decl = check_for_out_of_scope_variable (decl);
2602 /* Remember that the name was used in the definition of
2603 the current class so that we can check later to see if
2604 the meaning would have been different after the class
2605 was entirely defined. */
2606 if (!parser->scope && decl != error_mark_node)
2607 maybe_note_name_used_in_class (id_expression, decl);
2610 /* If we didn't find anything, or what we found was a type,
2611 then this wasn't really an id-expression. */
2612 if (TREE_CODE (decl) == TEMPLATE_DECL
2613 && !DECL_FUNCTION_TEMPLATE_P (decl))
2615 cp_parser_error (parser, "missing template arguments");
2616 return error_mark_node;
2618 else if (TREE_CODE (decl) == TYPE_DECL
2619 || TREE_CODE (decl) == NAMESPACE_DECL)
2621 cp_parser_error (parser,
2622 "expected primary-expression");
2623 return error_mark_node;
2626 /* If the name resolved to a template parameter, there is no
2627 need to look it up again later. Similarly, we resolve
2628 enumeration constants to their underlying values. */
2629 if (TREE_CODE (decl) == CONST_DECL)
2631 *idk = CP_PARSER_ID_KIND_NONE;
2632 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2633 return DECL_INITIAL (decl);
2634 return decl;
2636 else
2638 bool dependent_p;
2640 /* If the declaration was explicitly qualified indicate
2641 that. The semantics of `A::f(3)' are different than
2642 `f(3)' if `f' is virtual. */
2643 *idk = (parser->scope
2644 ? CP_PARSER_ID_KIND_QUALIFIED
2645 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2646 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2647 : CP_PARSER_ID_KIND_UNQUALIFIED));
2650 /* [temp.dep.expr]
2652 An id-expression is type-dependent if it contains an
2653 identifier that was declared with a dependent type.
2655 As an optimization, we could choose not to create a
2656 LOOKUP_EXPR for a name that resolved to a local
2657 variable in the template function that we are currently
2658 declaring; such a name cannot ever resolve to anything
2659 else. If we did that we would not have to look up
2660 these names at instantiation time.
2662 The standard is not very specific about an
2663 id-expression that names a set of overloaded functions.
2664 What if some of them have dependent types and some of
2665 them do not? Presumably, such a name should be treated
2666 as a dependent name. */
2667 /* Assume the name is not dependent. */
2668 dependent_p = false;
2669 if (!processing_template_decl)
2670 /* No names are dependent outside a template. */
2672 /* A template-id where the name of the template was not
2673 resolved is definitely dependent. */
2674 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2675 && (TREE_CODE (TREE_OPERAND (decl, 0))
2676 == IDENTIFIER_NODE))
2677 dependent_p = true;
2678 /* For anything except an overloaded function, just check
2679 its type. */
2680 else if (!is_overloaded_fn (decl))
2681 dependent_p
2682 = dependent_type_p (TREE_TYPE (decl));
2683 /* For a set of overloaded functions, check each of the
2684 functions. */
2685 else
2687 tree fns = decl;
2689 if (BASELINK_P (fns))
2690 fns = BASELINK_FUNCTIONS (fns);
2692 /* For a template-id, check to see if the template
2693 arguments are dependent. */
2694 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2696 tree args = TREE_OPERAND (fns, 1);
2698 if (args && TREE_CODE (args) == TREE_LIST)
2700 while (args)
2702 if (dependent_template_arg_p (TREE_VALUE (args)))
2704 dependent_p = true;
2705 break;
2707 args = TREE_CHAIN (args);
2710 else if (args && TREE_CODE (args) == TREE_VEC)
2712 int i;
2713 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2714 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2716 dependent_p = true;
2717 break;
2721 /* The functions are those referred to by the
2722 template-id. */
2723 fns = TREE_OPERAND (fns, 0);
2726 /* If there are no dependent template arguments, go
2727 through the overlaoded functions. */
2728 while (fns && !dependent_p)
2730 tree fn = OVL_CURRENT (fns);
2732 /* Member functions of dependent classes are
2733 dependent. */
2734 if (TREE_CODE (fn) == FUNCTION_DECL
2735 && type_dependent_expression_p (fn))
2736 dependent_p = true;
2737 else if (TREE_CODE (fn) == TEMPLATE_DECL
2738 && dependent_template_p (fn))
2739 dependent_p = true;
2741 fns = OVL_NEXT (fns);
2745 /* If the name was dependent on a template parameter,
2746 we will resolve the name at instantiation time. */
2747 if (dependent_p)
2749 /* Create a SCOPE_REF for qualified names. */
2750 if (parser->scope)
2752 if (TYPE_P (parser->scope))
2753 *qualifying_class = parser->scope;
2754 /* Since this name was dependent, the expression isn't
2755 constant -- yet. No error is issued because it
2756 might be constant when things are instantiated. */
2757 if (parser->constant_expression_p)
2758 parser->non_constant_expression_p = true;
2759 return build_nt (SCOPE_REF,
2760 parser->scope,
2761 id_expression);
2763 /* A TEMPLATE_ID already contains all the information
2764 we need. */
2765 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2766 return id_expression;
2767 /* Since this name was dependent, the expression isn't
2768 constant -- yet. No error is issued because it
2769 might be constant when things are instantiated. */
2770 if (parser->constant_expression_p)
2771 parser->non_constant_expression_p = true;
2772 /* Create a LOOKUP_EXPR for other unqualified names. */
2773 return build_min_nt (LOOKUP_EXPR, id_expression);
2776 /* Only certain kinds of names are allowed in constant
2777 expression. Enumerators have already been handled
2778 above. */
2779 if (parser->constant_expression_p
2780 /* Non-type template parameters of integral or
2781 enumeration type. */
2782 && !(TREE_CODE (decl) == TEMPLATE_PARM_INDEX
2783 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl)))
2784 /* Const variables or static data members of integral
2785 or enumeration types initialized with constant
2786 expressions (or dependent expressions - in this case
2787 the check will be done at instantiation time). */
2788 && !(TREE_CODE (decl) == VAR_DECL
2789 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2790 && DECL_INITIAL (decl)
2791 && (TREE_CONSTANT (DECL_INITIAL (decl))
2792 || type_dependent_expression_p
2793 (DECL_INITIAL (decl))
2794 || value_dependent_expression_p
2795 (DECL_INITIAL (decl)))))
2797 if (!parser->allow_non_constant_expression_p)
2798 return cp_parser_non_constant_id_expression (decl);
2799 parser->non_constant_expression_p = true;
2802 if (parser->scope)
2804 decl = (adjust_result_of_qualified_name_lookup
2805 (decl, parser->scope, current_class_type));
2806 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2807 *qualifying_class = parser->scope;
2808 else if (!processing_template_decl)
2809 decl = convert_from_reference (decl);
2811 else
2812 /* Transform references to non-static data members into
2813 COMPONENT_REFs. */
2814 decl = hack_identifier (decl, id_expression);
2816 /* Resolve references to variables of anonymous unions
2817 into COMPONENT_REFs. */
2818 if (TREE_CODE (decl) == ALIAS_DECL)
2819 decl = DECL_INITIAL (decl);
2822 if (TREE_DEPRECATED (decl))
2823 warn_deprecated_use (decl);
2825 return decl;
2828 /* Anything else is an error. */
2829 default:
2830 cp_parser_error (parser, "expected primary-expression");
2831 return error_mark_node;
2835 /* Parse an id-expression.
2837 id-expression:
2838 unqualified-id
2839 qualified-id
2841 qualified-id:
2842 :: [opt] nested-name-specifier template [opt] unqualified-id
2843 :: identifier
2844 :: operator-function-id
2845 :: template-id
2847 Return a representation of the unqualified portion of the
2848 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2849 a `::' or nested-name-specifier.
2851 Often, if the id-expression was a qualified-id, the caller will
2852 want to make a SCOPE_REF to represent the qualified-id. This
2853 function does not do this in order to avoid wastefully creating
2854 SCOPE_REFs when they are not required.
2856 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2857 `template' keyword.
2859 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2860 uninstantiated templates.
2862 If *TEMPLATE_P is non-NULL, it is set to true iff the
2863 `template' keyword is used to explicitly indicate that the entity
2864 named is a template. */
2866 static tree
2867 cp_parser_id_expression (cp_parser *parser,
2868 bool template_keyword_p,
2869 bool check_dependency_p,
2870 bool *template_p)
2872 bool global_scope_p;
2873 bool nested_name_specifier_p;
2875 /* Assume the `template' keyword was not used. */
2876 if (template_p)
2877 *template_p = false;
2879 /* Look for the optional `::' operator. */
2880 global_scope_p
2881 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2882 != NULL_TREE);
2883 /* Look for the optional nested-name-specifier. */
2884 nested_name_specifier_p
2885 = (cp_parser_nested_name_specifier_opt (parser,
2886 /*typename_keyword_p=*/false,
2887 check_dependency_p,
2888 /*type_p=*/false)
2889 != NULL_TREE);
2890 /* If there is a nested-name-specifier, then we are looking at
2891 the first qualified-id production. */
2892 if (nested_name_specifier_p)
2894 tree saved_scope;
2895 tree saved_object_scope;
2896 tree saved_qualifying_scope;
2897 tree unqualified_id;
2898 bool is_template;
2900 /* See if the next token is the `template' keyword. */
2901 if (!template_p)
2902 template_p = &is_template;
2903 *template_p = cp_parser_optional_template_keyword (parser);
2904 /* Name lookup we do during the processing of the
2905 unqualified-id might obliterate SCOPE. */
2906 saved_scope = parser->scope;
2907 saved_object_scope = parser->object_scope;
2908 saved_qualifying_scope = parser->qualifying_scope;
2909 /* Process the final unqualified-id. */
2910 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2911 check_dependency_p);
2912 /* Restore the SAVED_SCOPE for our caller. */
2913 parser->scope = saved_scope;
2914 parser->object_scope = saved_object_scope;
2915 parser->qualifying_scope = saved_qualifying_scope;
2917 return unqualified_id;
2919 /* Otherwise, if we are in global scope, then we are looking at one
2920 of the other qualified-id productions. */
2921 else if (global_scope_p)
2923 cp_token *token;
2924 tree id;
2926 /* Peek at the next token. */
2927 token = cp_lexer_peek_token (parser->lexer);
2929 /* If it's an identifier, and the next token is not a "<", then
2930 we can avoid the template-id case. This is an optimization
2931 for this common case. */
2932 if (token->type == CPP_NAME
2933 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2934 return cp_parser_identifier (parser);
2936 cp_parser_parse_tentatively (parser);
2937 /* Try a template-id. */
2938 id = cp_parser_template_id (parser,
2939 /*template_keyword_p=*/false,
2940 /*check_dependency_p=*/true);
2941 /* If that worked, we're done. */
2942 if (cp_parser_parse_definitely (parser))
2943 return id;
2945 /* Peek at the next token. (Changes in the token buffer may
2946 have invalidated the pointer obtained above.) */
2947 token = cp_lexer_peek_token (parser->lexer);
2949 switch (token->type)
2951 case CPP_NAME:
2952 return cp_parser_identifier (parser);
2954 case CPP_KEYWORD:
2955 if (token->keyword == RID_OPERATOR)
2956 return cp_parser_operator_function_id (parser);
2957 /* Fall through. */
2959 default:
2960 cp_parser_error (parser, "expected id-expression");
2961 return error_mark_node;
2964 else
2965 return cp_parser_unqualified_id (parser, template_keyword_p,
2966 /*check_dependency_p=*/true);
2969 /* Parse an unqualified-id.
2971 unqualified-id:
2972 identifier
2973 operator-function-id
2974 conversion-function-id
2975 ~ class-name
2976 template-id
2978 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2979 keyword, in a construct like `A::template ...'.
2981 Returns a representation of unqualified-id. For the `identifier'
2982 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2983 production a BIT_NOT_EXPR is returned; the operand of the
2984 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2985 other productions, see the documentation accompanying the
2986 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2987 names are looked up in uninstantiated templates. */
2989 static tree
2990 cp_parser_unqualified_id (cp_parser* parser,
2991 bool template_keyword_p,
2992 bool check_dependency_p)
2994 cp_token *token;
2996 /* Peek at the next token. */
2997 token = cp_lexer_peek_token (parser->lexer);
2999 switch (token->type)
3001 case CPP_NAME:
3003 tree id;
3005 /* We don't know yet whether or not this will be a
3006 template-id. */
3007 cp_parser_parse_tentatively (parser);
3008 /* Try a template-id. */
3009 id = cp_parser_template_id (parser, template_keyword_p,
3010 check_dependency_p);
3011 /* If it worked, we're done. */
3012 if (cp_parser_parse_definitely (parser))
3013 return id;
3014 /* Otherwise, it's an ordinary identifier. */
3015 return cp_parser_identifier (parser);
3018 case CPP_TEMPLATE_ID:
3019 return cp_parser_template_id (parser, template_keyword_p,
3020 check_dependency_p);
3022 case CPP_COMPL:
3024 tree type_decl;
3025 tree qualifying_scope;
3026 tree object_scope;
3027 tree scope;
3029 /* Consume the `~' token. */
3030 cp_lexer_consume_token (parser->lexer);
3031 /* Parse the class-name. The standard, as written, seems to
3032 say that:
3034 template <typename T> struct S { ~S (); };
3035 template <typename T> S<T>::~S() {}
3037 is invalid, since `~' must be followed by a class-name, but
3038 `S<T>' is dependent, and so not known to be a class.
3039 That's not right; we need to look in uninstantiated
3040 templates. A further complication arises from:
3042 template <typename T> void f(T t) {
3043 t.T::~T();
3046 Here, it is not possible to look up `T' in the scope of `T'
3047 itself. We must look in both the current scope, and the
3048 scope of the containing complete expression.
3050 Yet another issue is:
3052 struct S {
3053 int S;
3054 ~S();
3057 S::~S() {}
3059 The standard does not seem to say that the `S' in `~S'
3060 should refer to the type `S' and not the data member
3061 `S::S'. */
3063 /* DR 244 says that we look up the name after the "~" in the
3064 same scope as we looked up the qualifying name. That idea
3065 isn't fully worked out; it's more complicated than that. */
3066 scope = parser->scope;
3067 object_scope = parser->object_scope;
3068 qualifying_scope = parser->qualifying_scope;
3070 /* If the name is of the form "X::~X" it's OK. */
3071 if (scope && TYPE_P (scope)
3072 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3073 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3074 == CPP_OPEN_PAREN)
3075 && (cp_lexer_peek_token (parser->lexer)->value
3076 == TYPE_IDENTIFIER (scope)))
3078 cp_lexer_consume_token (parser->lexer);
3079 return build_nt (BIT_NOT_EXPR, scope);
3082 /* If there was an explicit qualification (S::~T), first look
3083 in the scope given by the qualification (i.e., S). */
3084 if (scope)
3086 cp_parser_parse_tentatively (parser);
3087 type_decl = cp_parser_class_name (parser,
3088 /*typename_keyword_p=*/false,
3089 /*template_keyword_p=*/false,
3090 /*type_p=*/false,
3091 /*check_dependency=*/false,
3092 /*class_head_p=*/false);
3093 if (cp_parser_parse_definitely (parser))
3094 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3096 /* In "N::S::~S", look in "N" as well. */
3097 if (scope && qualifying_scope)
3099 cp_parser_parse_tentatively (parser);
3100 parser->scope = qualifying_scope;
3101 parser->object_scope = NULL_TREE;
3102 parser->qualifying_scope = NULL_TREE;
3103 type_decl
3104 = cp_parser_class_name (parser,
3105 /*typename_keyword_p=*/false,
3106 /*template_keyword_p=*/false,
3107 /*type_p=*/false,
3108 /*check_dependency=*/false,
3109 /*class_head_p=*/false);
3110 if (cp_parser_parse_definitely (parser))
3111 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3113 /* In "p->S::~T", look in the scope given by "*p" as well. */
3114 else if (object_scope)
3116 cp_parser_parse_tentatively (parser);
3117 parser->scope = object_scope;
3118 parser->object_scope = NULL_TREE;
3119 parser->qualifying_scope = NULL_TREE;
3120 type_decl
3121 = cp_parser_class_name (parser,
3122 /*typename_keyword_p=*/false,
3123 /*template_keyword_p=*/false,
3124 /*type_p=*/false,
3125 /*check_dependency=*/false,
3126 /*class_head_p=*/false);
3127 if (cp_parser_parse_definitely (parser))
3128 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3130 /* Look in the surrounding context. */
3131 parser->scope = NULL_TREE;
3132 parser->object_scope = NULL_TREE;
3133 parser->qualifying_scope = NULL_TREE;
3134 type_decl
3135 = cp_parser_class_name (parser,
3136 /*typename_keyword_p=*/false,
3137 /*template_keyword_p=*/false,
3138 /*type_p=*/false,
3139 /*check_dependency=*/false,
3140 /*class_head_p=*/false);
3141 /* If an error occurred, assume that the name of the
3142 destructor is the same as the name of the qualifying
3143 class. That allows us to keep parsing after running
3144 into ill-formed destructor names. */
3145 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3146 return build_nt (BIT_NOT_EXPR, scope);
3147 else if (type_decl == error_mark_node)
3148 return error_mark_node;
3150 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3153 case CPP_KEYWORD:
3154 if (token->keyword == RID_OPERATOR)
3156 tree id;
3158 /* This could be a template-id, so we try that first. */
3159 cp_parser_parse_tentatively (parser);
3160 /* Try a template-id. */
3161 id = cp_parser_template_id (parser, template_keyword_p,
3162 /*check_dependency_p=*/true);
3163 /* If that worked, we're done. */
3164 if (cp_parser_parse_definitely (parser))
3165 return id;
3166 /* We still don't know whether we're looking at an
3167 operator-function-id or a conversion-function-id. */
3168 cp_parser_parse_tentatively (parser);
3169 /* Try an operator-function-id. */
3170 id = cp_parser_operator_function_id (parser);
3171 /* If that didn't work, try a conversion-function-id. */
3172 if (!cp_parser_parse_definitely (parser))
3173 id = cp_parser_conversion_function_id (parser);
3175 return id;
3177 /* Fall through. */
3179 default:
3180 cp_parser_error (parser, "expected unqualified-id");
3181 return error_mark_node;
3185 /* Parse an (optional) nested-name-specifier.
3187 nested-name-specifier:
3188 class-or-namespace-name :: nested-name-specifier [opt]
3189 class-or-namespace-name :: template nested-name-specifier [opt]
3191 PARSER->SCOPE should be set appropriately before this function is
3192 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3193 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3194 in name lookups.
3196 Sets PARSER->SCOPE to the class (TYPE) or namespace
3197 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3198 it unchanged if there is no nested-name-specifier. Returns the new
3199 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3201 static tree
3202 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3203 bool typename_keyword_p,
3204 bool check_dependency_p,
3205 bool type_p)
3207 bool success = false;
3208 tree access_check = NULL_TREE;
3209 ptrdiff_t start;
3210 cp_token* token;
3212 /* If the next token corresponds to a nested name specifier, there
3213 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3214 false, it may have been true before, in which case something
3215 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3216 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3217 CHECK_DEPENDENCY_P is false, we have to fall through into the
3218 main loop. */
3219 if (check_dependency_p
3220 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3222 cp_parser_pre_parsed_nested_name_specifier (parser);
3223 return parser->scope;
3226 /* Remember where the nested-name-specifier starts. */
3227 if (cp_parser_parsing_tentatively (parser)
3228 && !cp_parser_committed_to_tentative_parse (parser))
3230 token = cp_lexer_peek_token (parser->lexer);
3231 start = cp_lexer_token_difference (parser->lexer,
3232 parser->lexer->first_token,
3233 token);
3235 else
3236 start = -1;
3238 push_deferring_access_checks (dk_deferred);
3240 while (true)
3242 tree new_scope;
3243 tree old_scope;
3244 tree saved_qualifying_scope;
3245 bool template_keyword_p;
3247 /* Spot cases that cannot be the beginning of a
3248 nested-name-specifier. */
3249 token = cp_lexer_peek_token (parser->lexer);
3251 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3252 the already parsed nested-name-specifier. */
3253 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3255 /* Grab the nested-name-specifier and continue the loop. */
3256 cp_parser_pre_parsed_nested_name_specifier (parser);
3257 success = true;
3258 continue;
3261 /* Spot cases that cannot be the beginning of a
3262 nested-name-specifier. On the second and subsequent times
3263 through the loop, we look for the `template' keyword. */
3264 if (success && token->keyword == RID_TEMPLATE)
3266 /* A template-id can start a nested-name-specifier. */
3267 else if (token->type == CPP_TEMPLATE_ID)
3269 else
3271 /* If the next token is not an identifier, then it is
3272 definitely not a class-or-namespace-name. */
3273 if (token->type != CPP_NAME)
3274 break;
3275 /* If the following token is neither a `<' (to begin a
3276 template-id), nor a `::', then we are not looking at a
3277 nested-name-specifier. */
3278 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3279 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3280 break;
3283 /* The nested-name-specifier is optional, so we parse
3284 tentatively. */
3285 cp_parser_parse_tentatively (parser);
3287 /* Look for the optional `template' keyword, if this isn't the
3288 first time through the loop. */
3289 if (success)
3290 template_keyword_p = cp_parser_optional_template_keyword (parser);
3291 else
3292 template_keyword_p = false;
3294 /* Save the old scope since the name lookup we are about to do
3295 might destroy it. */
3296 old_scope = parser->scope;
3297 saved_qualifying_scope = parser->qualifying_scope;
3298 /* Parse the qualifying entity. */
3299 new_scope
3300 = cp_parser_class_or_namespace_name (parser,
3301 typename_keyword_p,
3302 template_keyword_p,
3303 check_dependency_p,
3304 type_p);
3305 /* Look for the `::' token. */
3306 cp_parser_require (parser, CPP_SCOPE, "`::'");
3308 /* If we found what we wanted, we keep going; otherwise, we're
3309 done. */
3310 if (!cp_parser_parse_definitely (parser))
3312 bool error_p = false;
3314 /* Restore the OLD_SCOPE since it was valid before the
3315 failed attempt at finding the last
3316 class-or-namespace-name. */
3317 parser->scope = old_scope;
3318 parser->qualifying_scope = saved_qualifying_scope;
3319 /* If the next token is an identifier, and the one after
3320 that is a `::', then any valid interpretation would have
3321 found a class-or-namespace-name. */
3322 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3323 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3324 == CPP_SCOPE)
3325 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3326 != CPP_COMPL))
3328 token = cp_lexer_consume_token (parser->lexer);
3329 if (!error_p)
3331 tree decl;
3333 decl = cp_parser_lookup_name_simple (parser, token->value);
3334 if (TREE_CODE (decl) == TEMPLATE_DECL)
3335 error ("`%D' used without template parameters",
3336 decl);
3337 else if (parser->scope)
3339 if (TYPE_P (parser->scope))
3340 error ("`%T::%D' is not a class-name or "
3341 "namespace-name",
3342 parser->scope, token->value);
3343 else
3344 error ("`%D::%D' is not a class-name or "
3345 "namespace-name",
3346 parser->scope, token->value);
3348 else
3349 error ("`%D' is not a class-name or namespace-name",
3350 token->value);
3351 parser->scope = NULL_TREE;
3352 error_p = true;
3353 /* Treat this as a successful nested-name-specifier
3354 due to:
3356 [basic.lookup.qual]
3358 If the name found is not a class-name (clause
3359 _class_) or namespace-name (_namespace.def_), the
3360 program is ill-formed. */
3361 success = true;
3363 cp_lexer_consume_token (parser->lexer);
3365 break;
3368 /* We've found one valid nested-name-specifier. */
3369 success = true;
3370 /* Make sure we look in the right scope the next time through
3371 the loop. */
3372 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3373 ? TREE_TYPE (new_scope)
3374 : new_scope);
3375 /* If it is a class scope, try to complete it; we are about to
3376 be looking up names inside the class. */
3377 if (TYPE_P (parser->scope)
3378 /* Since checking types for dependency can be expensive,
3379 avoid doing it if the type is already complete. */
3380 && !COMPLETE_TYPE_P (parser->scope)
3381 /* Do not try to complete dependent types. */
3382 && !dependent_type_p (parser->scope))
3383 complete_type (parser->scope);
3386 /* Retrieve any deferred checks. Do not pop this access checks yet
3387 so the memory will not be reclaimed during token replacing below. */
3388 access_check = get_deferred_access_checks ();
3390 /* If parsing tentatively, replace the sequence of tokens that makes
3391 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3392 token. That way, should we re-parse the token stream, we will
3393 not have to repeat the effort required to do the parse, nor will
3394 we issue duplicate error messages. */
3395 if (success && start >= 0)
3397 /* Find the token that corresponds to the start of the
3398 template-id. */
3399 token = cp_lexer_advance_token (parser->lexer,
3400 parser->lexer->first_token,
3401 start);
3403 /* Reset the contents of the START token. */
3404 token->type = CPP_NESTED_NAME_SPECIFIER;
3405 token->value = build_tree_list (access_check, parser->scope);
3406 TREE_TYPE (token->value) = parser->qualifying_scope;
3407 token->keyword = RID_MAX;
3408 /* Purge all subsequent tokens. */
3409 cp_lexer_purge_tokens_after (parser->lexer, token);
3412 pop_deferring_access_checks ();
3413 return success ? parser->scope : NULL_TREE;
3416 /* Parse a nested-name-specifier. See
3417 cp_parser_nested_name_specifier_opt for details. This function
3418 behaves identically, except that it will an issue an error if no
3419 nested-name-specifier is present, and it will return
3420 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3421 is present. */
3423 static tree
3424 cp_parser_nested_name_specifier (cp_parser *parser,
3425 bool typename_keyword_p,
3426 bool check_dependency_p,
3427 bool type_p)
3429 tree scope;
3431 /* Look for the nested-name-specifier. */
3432 scope = cp_parser_nested_name_specifier_opt (parser,
3433 typename_keyword_p,
3434 check_dependency_p,
3435 type_p);
3436 /* If it was not present, issue an error message. */
3437 if (!scope)
3439 cp_parser_error (parser, "expected nested-name-specifier");
3440 return error_mark_node;
3443 return scope;
3446 /* Parse a class-or-namespace-name.
3448 class-or-namespace-name:
3449 class-name
3450 namespace-name
3452 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3453 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3454 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3455 TYPE_P is TRUE iff the next name should be taken as a class-name,
3456 even the same name is declared to be another entity in the same
3457 scope.
3459 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3460 specified by the class-or-namespace-name. If neither is found the
3461 ERROR_MARK_NODE is returned. */
3463 static tree
3464 cp_parser_class_or_namespace_name (cp_parser *parser,
3465 bool typename_keyword_p,
3466 bool template_keyword_p,
3467 bool check_dependency_p,
3468 bool type_p)
3470 tree saved_scope;
3471 tree saved_qualifying_scope;
3472 tree saved_object_scope;
3473 tree scope;
3474 bool only_class_p;
3476 /* Before we try to parse the class-name, we must save away the
3477 current PARSER->SCOPE since cp_parser_class_name will destroy
3478 it. */
3479 saved_scope = parser->scope;
3480 saved_qualifying_scope = parser->qualifying_scope;
3481 saved_object_scope = parser->object_scope;
3482 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3483 there is no need to look for a namespace-name. */
3484 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3485 if (!only_class_p)
3486 cp_parser_parse_tentatively (parser);
3487 scope = cp_parser_class_name (parser,
3488 typename_keyword_p,
3489 template_keyword_p,
3490 type_p,
3491 check_dependency_p,
3492 /*class_head_p=*/false);
3493 /* If that didn't work, try for a namespace-name. */
3494 if (!only_class_p && !cp_parser_parse_definitely (parser))
3496 /* Restore the saved scope. */
3497 parser->scope = saved_scope;
3498 parser->qualifying_scope = saved_qualifying_scope;
3499 parser->object_scope = saved_object_scope;
3500 /* If we are not looking at an identifier followed by the scope
3501 resolution operator, then this is not part of a
3502 nested-name-specifier. (Note that this function is only used
3503 to parse the components of a nested-name-specifier.) */
3504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3505 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3506 return error_mark_node;
3507 scope = cp_parser_namespace_name (parser);
3510 return scope;
3513 /* Parse a postfix-expression.
3515 postfix-expression:
3516 primary-expression
3517 postfix-expression [ expression ]
3518 postfix-expression ( expression-list [opt] )
3519 simple-type-specifier ( expression-list [opt] )
3520 typename :: [opt] nested-name-specifier identifier
3521 ( expression-list [opt] )
3522 typename :: [opt] nested-name-specifier template [opt] template-id
3523 ( expression-list [opt] )
3524 postfix-expression . template [opt] id-expression
3525 postfix-expression -> template [opt] id-expression
3526 postfix-expression . pseudo-destructor-name
3527 postfix-expression -> pseudo-destructor-name
3528 postfix-expression ++
3529 postfix-expression --
3530 dynamic_cast < type-id > ( expression )
3531 static_cast < type-id > ( expression )
3532 reinterpret_cast < type-id > ( expression )
3533 const_cast < type-id > ( expression )
3534 typeid ( expression )
3535 typeid ( type-id )
3537 GNU Extension:
3539 postfix-expression:
3540 ( type-id ) { initializer-list , [opt] }
3542 This extension is a GNU version of the C99 compound-literal
3543 construct. (The C99 grammar uses `type-name' instead of `type-id',
3544 but they are essentially the same concept.)
3546 If ADDRESS_P is true, the postfix expression is the operand of the
3547 `&' operator.
3549 Returns a representation of the expression. */
3551 static tree
3552 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3554 cp_token *token;
3555 enum rid keyword;
3556 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3557 tree postfix_expression = NULL_TREE;
3558 /* Non-NULL only if the current postfix-expression can be used to
3559 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3560 class used to qualify the member. */
3561 tree qualifying_class = NULL_TREE;
3562 bool done;
3564 /* Peek at the next token. */
3565 token = cp_lexer_peek_token (parser->lexer);
3566 /* Some of the productions are determined by keywords. */
3567 keyword = token->keyword;
3568 switch (keyword)
3570 case RID_DYNCAST:
3571 case RID_STATCAST:
3572 case RID_REINTCAST:
3573 case RID_CONSTCAST:
3575 tree type;
3576 tree expression;
3577 const char *saved_message;
3579 /* All of these can be handled in the same way from the point
3580 of view of parsing. Begin by consuming the token
3581 identifying the cast. */
3582 cp_lexer_consume_token (parser->lexer);
3584 /* New types cannot be defined in the cast. */
3585 saved_message = parser->type_definition_forbidden_message;
3586 parser->type_definition_forbidden_message
3587 = "types may not be defined in casts";
3589 /* Look for the opening `<'. */
3590 cp_parser_require (parser, CPP_LESS, "`<'");
3591 /* Parse the type to which we are casting. */
3592 type = cp_parser_type_id (parser);
3593 /* Look for the closing `>'. */
3594 cp_parser_require (parser, CPP_GREATER, "`>'");
3595 /* Restore the old message. */
3596 parser->type_definition_forbidden_message = saved_message;
3598 /* And the expression which is being cast. */
3599 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3600 expression = cp_parser_expression (parser);
3601 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3603 /* Only type conversions to integral or enumeration types
3604 can be used in constant-expressions. */
3605 if (parser->constant_expression_p
3606 && !dependent_type_p (type)
3607 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3609 if (!parser->allow_non_constant_expression_p)
3610 return (cp_parser_non_constant_expression
3611 ("a cast to a type other than an integral or "
3612 "enumeration type"));
3613 parser->non_constant_expression_p = true;
3616 switch (keyword)
3618 case RID_DYNCAST:
3619 postfix_expression
3620 = build_dynamic_cast (type, expression);
3621 break;
3622 case RID_STATCAST:
3623 postfix_expression
3624 = build_static_cast (type, expression);
3625 break;
3626 case RID_REINTCAST:
3627 postfix_expression
3628 = build_reinterpret_cast (type, expression);
3629 break;
3630 case RID_CONSTCAST:
3631 postfix_expression
3632 = build_const_cast (type, expression);
3633 break;
3634 default:
3635 abort ();
3638 break;
3640 case RID_TYPEID:
3642 tree type;
3643 const char *saved_message;
3645 /* Consume the `typeid' token. */
3646 cp_lexer_consume_token (parser->lexer);
3647 /* Look for the `(' token. */
3648 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3649 /* Types cannot be defined in a `typeid' expression. */
3650 saved_message = parser->type_definition_forbidden_message;
3651 parser->type_definition_forbidden_message
3652 = "types may not be defined in a `typeid\' expression";
3653 /* We can't be sure yet whether we're looking at a type-id or an
3654 expression. */
3655 cp_parser_parse_tentatively (parser);
3656 /* Try a type-id first. */
3657 type = cp_parser_type_id (parser);
3658 /* Look for the `)' token. Otherwise, we can't be sure that
3659 we're not looking at an expression: consider `typeid (int
3660 (3))', for example. */
3661 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3662 /* If all went well, simply lookup the type-id. */
3663 if (cp_parser_parse_definitely (parser))
3664 postfix_expression = get_typeid (type);
3665 /* Otherwise, fall back to the expression variant. */
3666 else
3668 tree expression;
3670 /* Look for an expression. */
3671 expression = cp_parser_expression (parser);
3672 /* Compute its typeid. */
3673 postfix_expression = build_typeid (expression);
3674 /* Look for the `)' token. */
3675 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3678 /* Restore the saved message. */
3679 parser->type_definition_forbidden_message = saved_message;
3681 break;
3683 case RID_TYPENAME:
3685 bool template_p = false;
3686 tree id;
3687 tree type;
3689 /* Consume the `typename' token. */
3690 cp_lexer_consume_token (parser->lexer);
3691 /* Look for the optional `::' operator. */
3692 cp_parser_global_scope_opt (parser,
3693 /*current_scope_valid_p=*/false);
3694 /* Look for the nested-name-specifier. */
3695 cp_parser_nested_name_specifier (parser,
3696 /*typename_keyword_p=*/true,
3697 /*check_dependency_p=*/true,
3698 /*type_p=*/true);
3699 /* Look for the optional `template' keyword. */
3700 template_p = cp_parser_optional_template_keyword (parser);
3701 /* We don't know whether we're looking at a template-id or an
3702 identifier. */
3703 cp_parser_parse_tentatively (parser);
3704 /* Try a template-id. */
3705 id = cp_parser_template_id (parser, template_p,
3706 /*check_dependency_p=*/true);
3707 /* If that didn't work, try an identifier. */
3708 if (!cp_parser_parse_definitely (parser))
3709 id = cp_parser_identifier (parser);
3710 /* Create a TYPENAME_TYPE to represent the type to which the
3711 functional cast is being performed. */
3712 type = make_typename_type (parser->scope, id,
3713 /*complain=*/1);
3715 postfix_expression = cp_parser_functional_cast (parser, type);
3717 break;
3719 default:
3721 tree type;
3723 /* If the next thing is a simple-type-specifier, we may be
3724 looking at a functional cast. We could also be looking at
3725 an id-expression. So, we try the functional cast, and if
3726 that doesn't work we fall back to the primary-expression. */
3727 cp_parser_parse_tentatively (parser);
3728 /* Look for the simple-type-specifier. */
3729 type = cp_parser_simple_type_specifier (parser,
3730 CP_PARSER_FLAGS_NONE);
3731 /* Parse the cast itself. */
3732 if (!cp_parser_error_occurred (parser))
3733 postfix_expression
3734 = cp_parser_functional_cast (parser, type);
3735 /* If that worked, we're done. */
3736 if (cp_parser_parse_definitely (parser))
3737 break;
3739 /* If the functional-cast didn't work out, try a
3740 compound-literal. */
3741 if (cp_parser_allow_gnu_extensions_p (parser)
3742 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3744 tree initializer_list = NULL_TREE;
3746 cp_parser_parse_tentatively (parser);
3747 /* Consume the `('. */
3748 cp_lexer_consume_token (parser->lexer);
3749 /* Parse the type. */
3750 type = cp_parser_type_id (parser);
3751 /* Look for the `)'. */
3752 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3753 /* Look for the `{'. */
3754 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3755 /* If things aren't going well, there's no need to
3756 keep going. */
3757 if (!cp_parser_error_occurred (parser))
3759 /* Parse the initializer-list. */
3760 initializer_list
3761 = cp_parser_initializer_list (parser);
3762 /* Allow a trailing `,'. */
3763 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3764 cp_lexer_consume_token (parser->lexer);
3765 /* Look for the final `}'. */
3766 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3768 /* If that worked, we're definitely looking at a
3769 compound-literal expression. */
3770 if (cp_parser_parse_definitely (parser))
3772 /* Warn the user that a compound literal is not
3773 allowed in standard C++. */
3774 if (pedantic)
3775 pedwarn ("ISO C++ forbids compound-literals");
3776 /* Form the representation of the compound-literal. */
3777 postfix_expression
3778 = finish_compound_literal (type, initializer_list);
3779 break;
3783 /* It must be a primary-expression. */
3784 postfix_expression = cp_parser_primary_expression (parser,
3785 &idk,
3786 &qualifying_class);
3788 break;
3791 /* Peek at the next token. */
3792 token = cp_lexer_peek_token (parser->lexer);
3793 done = (token->type != CPP_OPEN_SQUARE
3794 && token->type != CPP_OPEN_PAREN
3795 && token->type != CPP_DOT
3796 && token->type != CPP_DEREF
3797 && token->type != CPP_PLUS_PLUS
3798 && token->type != CPP_MINUS_MINUS);
3800 /* If the postfix expression is complete, finish up. */
3801 if (address_p && qualifying_class && done)
3803 if (TREE_CODE (postfix_expression) == SCOPE_REF)
3804 postfix_expression = TREE_OPERAND (postfix_expression, 1);
3805 postfix_expression
3806 = build_offset_ref (qualifying_class, postfix_expression);
3807 return postfix_expression;
3810 /* Otherwise, if we were avoiding committing until we knew
3811 whether or not we had a pointer-to-member, we now know that
3812 the expression is an ordinary reference to a qualified name. */
3813 if (qualifying_class)
3815 if (TREE_CODE (postfix_expression) == FIELD_DECL)
3816 postfix_expression
3817 = finish_non_static_data_member (postfix_expression,
3818 qualifying_class);
3819 else if (BASELINK_P (postfix_expression)
3820 && !processing_template_decl)
3822 tree fn;
3823 tree fns;
3825 /* See if any of the functions are non-static members. */
3826 fns = BASELINK_FUNCTIONS (postfix_expression);
3827 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3828 fns = TREE_OPERAND (fns, 0);
3829 for (fn = fns; fn; fn = OVL_NEXT (fn))
3830 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3831 break;
3832 /* If so, the expression may be relative to the current
3833 class. */
3834 if (fn && current_class_type
3835 && DERIVED_FROM_P (qualifying_class, current_class_type))
3836 postfix_expression
3837 = (build_class_member_access_expr
3838 (maybe_dummy_object (qualifying_class, NULL),
3839 postfix_expression,
3840 BASELINK_ACCESS_BINFO (postfix_expression),
3841 /*preserve_reference=*/false));
3842 else if (done)
3843 return build_offset_ref (qualifying_class,
3844 postfix_expression);
3848 /* Remember that there was a reference to this entity. */
3849 if (DECL_P (postfix_expression))
3850 mark_used (postfix_expression);
3852 /* Keep looping until the postfix-expression is complete. */
3853 while (true)
3855 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3856 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3858 /* It is not a Koenig lookup function call. */
3859 unqualified_name_lookup_error (postfix_expression);
3860 postfix_expression = error_mark_node;
3863 /* Peek at the next token. */
3864 token = cp_lexer_peek_token (parser->lexer);
3866 switch (token->type)
3868 case CPP_OPEN_SQUARE:
3869 /* postfix-expression [ expression ] */
3871 tree index;
3873 /* Consume the `[' token. */
3874 cp_lexer_consume_token (parser->lexer);
3875 /* Parse the index expression. */
3876 index = cp_parser_expression (parser);
3877 /* Look for the closing `]'. */
3878 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3880 /* Build the ARRAY_REF. */
3881 postfix_expression
3882 = grok_array_decl (postfix_expression, index);
3883 idk = CP_PARSER_ID_KIND_NONE;
3885 break;
3887 case CPP_OPEN_PAREN:
3888 /* postfix-expression ( expression-list [opt] ) */
3890 tree args;
3892 /* Consume the `(' token. */
3893 cp_lexer_consume_token (parser->lexer);
3894 /* If the next token is not a `)', then there are some
3895 arguments. */
3896 if (cp_lexer_next_token_is_not (parser->lexer,
3897 CPP_CLOSE_PAREN))
3898 args = cp_parser_expression_list (parser);
3899 else
3900 args = NULL_TREE;
3901 /* Look for the closing `)'. */
3902 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3903 /* Function calls are not permitted in
3904 constant-expressions. */
3905 if (parser->constant_expression_p)
3907 if (!parser->allow_non_constant_expression_p)
3908 return cp_parser_non_constant_expression ("a function call");
3909 parser->non_constant_expression_p = true;
3912 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
3913 && (is_overloaded_fn (postfix_expression)
3914 || DECL_P (postfix_expression)
3915 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3916 && args)
3918 tree arg;
3919 tree identifier = NULL_TREE;
3920 tree functions = NULL_TREE;
3922 /* Find the name of the overloaded function. */
3923 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3924 identifier = postfix_expression;
3925 else if (is_overloaded_fn (postfix_expression))
3927 functions = postfix_expression;
3928 identifier = DECL_NAME (get_first_fn (functions));
3930 else if (DECL_P (postfix_expression))
3932 functions = postfix_expression;
3933 identifier = DECL_NAME (postfix_expression);
3936 /* A call to a namespace-scope function using an
3937 unqualified name.
3939 Do Koenig lookup -- unless any of the arguments are
3940 type-dependent. */
3941 for (arg = args; arg; arg = TREE_CHAIN (arg))
3942 if (type_dependent_expression_p (TREE_VALUE (arg)))
3943 break;
3944 if (!arg)
3946 postfix_expression
3947 = lookup_arg_dependent(identifier, functions, args);
3948 if (!postfix_expression)
3950 /* The unqualified name could not be resolved. */
3951 unqualified_name_lookup_error (identifier);
3952 postfix_expression = error_mark_node;
3954 postfix_expression
3955 = build_call_from_tree (postfix_expression, args,
3956 /*diallow_virtual=*/false);
3957 break;
3959 postfix_expression = build_min_nt (LOOKUP_EXPR,
3960 identifier);
3962 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
3963 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3965 /* The unqualified name could not be resolved. */
3966 unqualified_name_lookup_error (postfix_expression);
3967 postfix_expression = error_mark_node;
3968 break;
3971 /* In the body of a template, no further processing is
3972 required. */
3973 if (processing_template_decl)
3975 postfix_expression = build_nt (CALL_EXPR,
3976 postfix_expression,
3977 args);
3978 break;
3981 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3982 postfix_expression
3983 = (build_new_method_call
3984 (TREE_OPERAND (postfix_expression, 0),
3985 TREE_OPERAND (postfix_expression, 1),
3986 args, NULL_TREE,
3987 (idk == CP_PARSER_ID_KIND_QUALIFIED
3988 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3989 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
3990 postfix_expression = (build_offset_ref_call_from_tree
3991 (postfix_expression, args));
3992 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
3993 /* A call to a static class member, or a namespace-scope
3994 function. */
3995 postfix_expression
3996 = finish_call_expr (postfix_expression, args,
3997 /*disallow_virtual=*/true);
3998 else
3999 /* All other function calls. */
4000 postfix_expression
4001 = finish_call_expr (postfix_expression, args,
4002 /*disallow_virtual=*/false);
4004 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4005 idk = CP_PARSER_ID_KIND_NONE;
4007 break;
4009 case CPP_DOT:
4010 case CPP_DEREF:
4011 /* postfix-expression . template [opt] id-expression
4012 postfix-expression . pseudo-destructor-name
4013 postfix-expression -> template [opt] id-expression
4014 postfix-expression -> pseudo-destructor-name */
4016 tree name;
4017 bool dependent_p;
4018 bool template_p;
4019 tree scope = NULL_TREE;
4021 /* If this is a `->' operator, dereference the pointer. */
4022 if (token->type == CPP_DEREF)
4023 postfix_expression = build_x_arrow (postfix_expression);
4024 /* Check to see whether or not the expression is
4025 type-dependent. */
4026 dependent_p = type_dependent_expression_p (postfix_expression);
4027 /* The identifier following the `->' or `.' is not
4028 qualified. */
4029 parser->scope = NULL_TREE;
4030 parser->qualifying_scope = NULL_TREE;
4031 parser->object_scope = NULL_TREE;
4032 idk = CP_PARSER_ID_KIND_NONE;
4033 /* Enter the scope corresponding to the type of the object
4034 given by the POSTFIX_EXPRESSION. */
4035 if (!dependent_p
4036 && TREE_TYPE (postfix_expression) != NULL_TREE)
4038 scope = TREE_TYPE (postfix_expression);
4039 /* According to the standard, no expression should
4040 ever have reference type. Unfortunately, we do not
4041 currently match the standard in this respect in
4042 that our internal representation of an expression
4043 may have reference type even when the standard says
4044 it does not. Therefore, we have to manually obtain
4045 the underlying type here. */
4046 if (TREE_CODE (scope) == REFERENCE_TYPE)
4047 scope = TREE_TYPE (scope);
4048 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4049 type of the field. We get an OFFSET_TYPE for
4050 something like:
4052 S::T.a ...
4054 Probably, we should not get an OFFSET_TYPE here;
4055 that transformation should be made only if `&S::T'
4056 is written. */
4057 if (TREE_CODE (scope) == OFFSET_TYPE)
4058 scope = TREE_TYPE (scope);
4059 /* The type of the POSTFIX_EXPRESSION must be
4060 complete. */
4061 scope = complete_type_or_else (scope, NULL_TREE);
4062 /* Let the name lookup machinery know that we are
4063 processing a class member access expression. */
4064 parser->context->object_type = scope;
4065 /* If something went wrong, we want to be able to
4066 discern that case, as opposed to the case where
4067 there was no SCOPE due to the type of expression
4068 being dependent. */
4069 if (!scope)
4070 scope = error_mark_node;
4073 /* Consume the `.' or `->' operator. */
4074 cp_lexer_consume_token (parser->lexer);
4075 /* If the SCOPE is not a scalar type, we are looking at an
4076 ordinary class member access expression, rather than a
4077 pseudo-destructor-name. */
4078 if (!scope || !SCALAR_TYPE_P (scope))
4080 template_p = cp_parser_optional_template_keyword (parser);
4081 /* Parse the id-expression. */
4082 name = cp_parser_id_expression (parser,
4083 template_p,
4084 /*check_dependency_p=*/true,
4085 /*template_p=*/NULL);
4086 /* In general, build a SCOPE_REF if the member name is
4087 qualified. However, if the name was not dependent
4088 and has already been resolved; there is no need to
4089 build the SCOPE_REF. For example;
4091 struct X { void f(); };
4092 template <typename T> void f(T* t) { t->X::f(); }
4094 Even though "t" is dependent, "X::f" is not and has
4095 except that for a BASELINK there is no need to
4096 include scope information. */
4098 /* But we do need to remember that there was an explicit
4099 scope for virtual function calls. */
4100 if (parser->scope)
4101 idk = CP_PARSER_ID_KIND_QUALIFIED;
4103 if (name != error_mark_node
4104 && !BASELINK_P (name)
4105 && parser->scope)
4107 name = build_nt (SCOPE_REF, parser->scope, name);
4108 parser->scope = NULL_TREE;
4109 parser->qualifying_scope = NULL_TREE;
4110 parser->object_scope = NULL_TREE;
4112 postfix_expression
4113 = finish_class_member_access_expr (postfix_expression, name);
4115 /* Otherwise, try the pseudo-destructor-name production. */
4116 else
4118 tree s;
4119 tree type;
4121 /* Parse the pseudo-destructor-name. */
4122 cp_parser_pseudo_destructor_name (parser, &s, &type);
4123 /* Form the call. */
4124 postfix_expression
4125 = finish_pseudo_destructor_expr (postfix_expression,
4126 s, TREE_TYPE (type));
4129 /* We no longer need to look up names in the scope of the
4130 object on the left-hand side of the `.' or `->'
4131 operator. */
4132 parser->context->object_type = NULL_TREE;
4134 break;
4136 case CPP_PLUS_PLUS:
4137 /* postfix-expression ++ */
4138 /* Consume the `++' token. */
4139 cp_lexer_consume_token (parser->lexer);
4140 /* Increments may not appear in constant-expressions. */
4141 if (parser->constant_expression_p)
4143 if (!parser->allow_non_constant_expression_p)
4144 return cp_parser_non_constant_expression ("an increment");
4145 parser->non_constant_expression_p = true;
4147 /* Generate a reprsentation for the complete expression. */
4148 postfix_expression
4149 = finish_increment_expr (postfix_expression,
4150 POSTINCREMENT_EXPR);
4151 idk = CP_PARSER_ID_KIND_NONE;
4152 break;
4154 case CPP_MINUS_MINUS:
4155 /* postfix-expression -- */
4156 /* Consume the `--' token. */
4157 cp_lexer_consume_token (parser->lexer);
4158 /* Decrements may not appear in constant-expressions. */
4159 if (parser->constant_expression_p)
4161 if (!parser->allow_non_constant_expression_p)
4162 return cp_parser_non_constant_expression ("a decrement");
4163 parser->non_constant_expression_p = true;
4165 /* Generate a reprsentation for the complete expression. */
4166 postfix_expression
4167 = finish_increment_expr (postfix_expression,
4168 POSTDECREMENT_EXPR);
4169 idk = CP_PARSER_ID_KIND_NONE;
4170 break;
4172 default:
4173 return postfix_expression;
4177 /* We should never get here. */
4178 abort ();
4179 return error_mark_node;
4182 /* Parse an expression-list.
4184 expression-list:
4185 assignment-expression
4186 expression-list, assignment-expression
4188 Returns a TREE_LIST. The TREE_VALUE of each node is a
4189 representation of an assignment-expression. Note that a TREE_LIST
4190 is returned even if there is only a single expression in the list. */
4192 static tree
4193 cp_parser_expression_list (cp_parser* parser)
4195 tree expression_list = NULL_TREE;
4197 /* Consume expressions until there are no more. */
4198 while (true)
4200 tree expr;
4202 /* Parse the next assignment-expression. */
4203 expr = cp_parser_assignment_expression (parser);
4204 /* Add it to the list. */
4205 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4207 /* If the next token isn't a `,', then we are done. */
4208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4210 /* All uses of expression-list in the grammar are followed
4211 by a `)'. Therefore, if the next token is not a `)' an
4212 error will be issued, unless we are parsing tentatively.
4213 Skip ahead to see if there is another `,' before the `)';
4214 if so, we can go there and recover. */
4215 if (cp_parser_parsing_tentatively (parser)
4216 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4217 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4218 break;
4221 /* Otherwise, consume the `,' and keep going. */
4222 cp_lexer_consume_token (parser->lexer);
4225 /* We built up the list in reverse order so we must reverse it now. */
4226 return nreverse (expression_list);
4229 /* Parse a pseudo-destructor-name.
4231 pseudo-destructor-name:
4232 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4233 :: [opt] nested-name-specifier template template-id :: ~ type-name
4234 :: [opt] nested-name-specifier [opt] ~ type-name
4236 If either of the first two productions is used, sets *SCOPE to the
4237 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4238 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4239 or ERROR_MARK_NODE if no type-name is present. */
4241 static void
4242 cp_parser_pseudo_destructor_name (cp_parser* parser,
4243 tree* scope,
4244 tree* type)
4246 bool nested_name_specifier_p;
4248 /* Look for the optional `::' operator. */
4249 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4250 /* Look for the optional nested-name-specifier. */
4251 nested_name_specifier_p
4252 = (cp_parser_nested_name_specifier_opt (parser,
4253 /*typename_keyword_p=*/false,
4254 /*check_dependency_p=*/true,
4255 /*type_p=*/false)
4256 != NULL_TREE);
4257 /* Now, if we saw a nested-name-specifier, we might be doing the
4258 second production. */
4259 if (nested_name_specifier_p
4260 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4262 /* Consume the `template' keyword. */
4263 cp_lexer_consume_token (parser->lexer);
4264 /* Parse the template-id. */
4265 cp_parser_template_id (parser,
4266 /*template_keyword_p=*/true,
4267 /*check_dependency_p=*/false);
4268 /* Look for the `::' token. */
4269 cp_parser_require (parser, CPP_SCOPE, "`::'");
4271 /* If the next token is not a `~', then there might be some
4272 additional qualification. */
4273 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4275 /* Look for the type-name. */
4276 *scope = TREE_TYPE (cp_parser_type_name (parser));
4277 /* Look for the `::' token. */
4278 cp_parser_require (parser, CPP_SCOPE, "`::'");
4280 else
4281 *scope = NULL_TREE;
4283 /* Look for the `~'. */
4284 cp_parser_require (parser, CPP_COMPL, "`~'");
4285 /* Look for the type-name again. We are not responsible for
4286 checking that it matches the first type-name. */
4287 *type = cp_parser_type_name (parser);
4290 /* Parse a unary-expression.
4292 unary-expression:
4293 postfix-expression
4294 ++ cast-expression
4295 -- cast-expression
4296 unary-operator cast-expression
4297 sizeof unary-expression
4298 sizeof ( type-id )
4299 new-expression
4300 delete-expression
4302 GNU Extensions:
4304 unary-expression:
4305 __extension__ cast-expression
4306 __alignof__ unary-expression
4307 __alignof__ ( type-id )
4308 __real__ cast-expression
4309 __imag__ cast-expression
4310 && identifier
4312 ADDRESS_P is true iff the unary-expression is appearing as the
4313 operand of the `&' operator.
4315 Returns a representation of the expresion. */
4317 static tree
4318 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4320 cp_token *token;
4321 enum tree_code unary_operator;
4323 /* Peek at the next token. */
4324 token = cp_lexer_peek_token (parser->lexer);
4325 /* Some keywords give away the kind of expression. */
4326 if (token->type == CPP_KEYWORD)
4328 enum rid keyword = token->keyword;
4330 switch (keyword)
4332 case RID_ALIGNOF:
4334 /* Consume the `alignof' token. */
4335 cp_lexer_consume_token (parser->lexer);
4336 /* Parse the operand. */
4337 return finish_alignof (cp_parser_sizeof_operand
4338 (parser, keyword));
4341 case RID_SIZEOF:
4343 tree operand;
4345 /* Consume the `sizeof' token. */
4346 cp_lexer_consume_token (parser->lexer);
4347 /* Parse the operand. */
4348 operand = cp_parser_sizeof_operand (parser, keyword);
4350 /* If the type of the operand cannot be determined build a
4351 SIZEOF_EXPR. */
4352 if (TYPE_P (operand)
4353 ? dependent_type_p (operand)
4354 : type_dependent_expression_p (operand))
4355 return build_min (SIZEOF_EXPR, size_type_node, operand);
4356 /* Otherwise, compute the constant value. */
4357 else
4358 return finish_sizeof (operand);
4361 case RID_NEW:
4362 return cp_parser_new_expression (parser);
4364 case RID_DELETE:
4365 return cp_parser_delete_expression (parser);
4367 case RID_EXTENSION:
4369 /* The saved value of the PEDANTIC flag. */
4370 int saved_pedantic;
4371 tree expr;
4373 /* Save away the PEDANTIC flag. */
4374 cp_parser_extension_opt (parser, &saved_pedantic);
4375 /* Parse the cast-expression. */
4376 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4377 /* Restore the PEDANTIC flag. */
4378 pedantic = saved_pedantic;
4380 return expr;
4383 case RID_REALPART:
4384 case RID_IMAGPART:
4386 tree expression;
4388 /* Consume the `__real__' or `__imag__' token. */
4389 cp_lexer_consume_token (parser->lexer);
4390 /* Parse the cast-expression. */
4391 expression = cp_parser_cast_expression (parser,
4392 /*address_p=*/false);
4393 /* Create the complete representation. */
4394 return build_x_unary_op ((keyword == RID_REALPART
4395 ? REALPART_EXPR : IMAGPART_EXPR),
4396 expression);
4398 break;
4400 default:
4401 break;
4405 /* Look for the `:: new' and `:: delete', which also signal the
4406 beginning of a new-expression, or delete-expression,
4407 respectively. If the next token is `::', then it might be one of
4408 these. */
4409 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4411 enum rid keyword;
4413 /* See if the token after the `::' is one of the keywords in
4414 which we're interested. */
4415 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4416 /* If it's `new', we have a new-expression. */
4417 if (keyword == RID_NEW)
4418 return cp_parser_new_expression (parser);
4419 /* Similarly, for `delete'. */
4420 else if (keyword == RID_DELETE)
4421 return cp_parser_delete_expression (parser);
4424 /* Look for a unary operator. */
4425 unary_operator = cp_parser_unary_operator (token);
4426 /* The `++' and `--' operators can be handled similarly, even though
4427 they are not technically unary-operators in the grammar. */
4428 if (unary_operator == ERROR_MARK)
4430 if (token->type == CPP_PLUS_PLUS)
4431 unary_operator = PREINCREMENT_EXPR;
4432 else if (token->type == CPP_MINUS_MINUS)
4433 unary_operator = PREDECREMENT_EXPR;
4434 /* Handle the GNU address-of-label extension. */
4435 else if (cp_parser_allow_gnu_extensions_p (parser)
4436 && token->type == CPP_AND_AND)
4438 tree identifier;
4440 /* Consume the '&&' token. */
4441 cp_lexer_consume_token (parser->lexer);
4442 /* Look for the identifier. */
4443 identifier = cp_parser_identifier (parser);
4444 /* Create an expression representing the address. */
4445 return finish_label_address_expr (identifier);
4448 if (unary_operator != ERROR_MARK)
4450 tree cast_expression;
4452 /* Consume the operator token. */
4453 token = cp_lexer_consume_token (parser->lexer);
4454 /* Parse the cast-expression. */
4455 cast_expression
4456 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4457 /* Now, build an appropriate representation. */
4458 switch (unary_operator)
4460 case INDIRECT_REF:
4461 return build_x_indirect_ref (cast_expression, "unary *");
4463 case ADDR_EXPR:
4464 return build_x_unary_op (ADDR_EXPR, cast_expression);
4466 case PREINCREMENT_EXPR:
4467 case PREDECREMENT_EXPR:
4468 if (parser->constant_expression_p)
4470 if (!parser->allow_non_constant_expression_p)
4471 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4472 ? "an increment"
4473 : "a decrement");
4474 parser->non_constant_expression_p = true;
4476 /* Fall through. */
4477 case CONVERT_EXPR:
4478 case NEGATE_EXPR:
4479 case TRUTH_NOT_EXPR:
4480 return finish_unary_op_expr (unary_operator, cast_expression);
4482 case BIT_NOT_EXPR:
4483 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4485 default:
4486 abort ();
4487 return error_mark_node;
4491 return cp_parser_postfix_expression (parser, address_p);
4494 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4495 unary-operator, the corresponding tree code is returned. */
4497 static enum tree_code
4498 cp_parser_unary_operator (cp_token* token)
4500 switch (token->type)
4502 case CPP_MULT:
4503 return INDIRECT_REF;
4505 case CPP_AND:
4506 return ADDR_EXPR;
4508 case CPP_PLUS:
4509 return CONVERT_EXPR;
4511 case CPP_MINUS:
4512 return NEGATE_EXPR;
4514 case CPP_NOT:
4515 return TRUTH_NOT_EXPR;
4517 case CPP_COMPL:
4518 return BIT_NOT_EXPR;
4520 default:
4521 return ERROR_MARK;
4525 /* Parse a new-expression.
4527 new-expression:
4528 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4529 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4531 Returns a representation of the expression. */
4533 static tree
4534 cp_parser_new_expression (cp_parser* parser)
4536 bool global_scope_p;
4537 tree placement;
4538 tree type;
4539 tree initializer;
4541 /* Look for the optional `::' operator. */
4542 global_scope_p
4543 = (cp_parser_global_scope_opt (parser,
4544 /*current_scope_valid_p=*/false)
4545 != NULL_TREE);
4546 /* Look for the `new' operator. */
4547 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4548 /* There's no easy way to tell a new-placement from the
4549 `( type-id )' construct. */
4550 cp_parser_parse_tentatively (parser);
4551 /* Look for a new-placement. */
4552 placement = cp_parser_new_placement (parser);
4553 /* If that didn't work out, there's no new-placement. */
4554 if (!cp_parser_parse_definitely (parser))
4555 placement = NULL_TREE;
4557 /* If the next token is a `(', then we have a parenthesized
4558 type-id. */
4559 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4561 /* Consume the `('. */
4562 cp_lexer_consume_token (parser->lexer);
4563 /* Parse the type-id. */
4564 type = cp_parser_type_id (parser);
4565 /* Look for the closing `)'. */
4566 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4568 /* Otherwise, there must be a new-type-id. */
4569 else
4570 type = cp_parser_new_type_id (parser);
4572 /* If the next token is a `(', then we have a new-initializer. */
4573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4574 initializer = cp_parser_new_initializer (parser);
4575 else
4576 initializer = NULL_TREE;
4578 /* Create a representation of the new-expression. */
4579 return build_new (placement, type, initializer, global_scope_p);
4582 /* Parse a new-placement.
4584 new-placement:
4585 ( expression-list )
4587 Returns the same representation as for an expression-list. */
4589 static tree
4590 cp_parser_new_placement (cp_parser* parser)
4592 tree expression_list;
4594 /* Look for the opening `('. */
4595 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4596 return error_mark_node;
4597 /* Parse the expression-list. */
4598 expression_list = cp_parser_expression_list (parser);
4599 /* Look for the closing `)'. */
4600 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4602 return expression_list;
4605 /* Parse a new-type-id.
4607 new-type-id:
4608 type-specifier-seq new-declarator [opt]
4610 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4611 and whose TREE_VALUE is the new-declarator. */
4613 static tree
4614 cp_parser_new_type_id (cp_parser* parser)
4616 tree type_specifier_seq;
4617 tree declarator;
4618 const char *saved_message;
4620 /* The type-specifier sequence must not contain type definitions.
4621 (It cannot contain declarations of new types either, but if they
4622 are not definitions we will catch that because they are not
4623 complete.) */
4624 saved_message = parser->type_definition_forbidden_message;
4625 parser->type_definition_forbidden_message
4626 = "types may not be defined in a new-type-id";
4627 /* Parse the type-specifier-seq. */
4628 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4629 /* Restore the old message. */
4630 parser->type_definition_forbidden_message = saved_message;
4631 /* Parse the new-declarator. */
4632 declarator = cp_parser_new_declarator_opt (parser);
4634 return build_tree_list (type_specifier_seq, declarator);
4637 /* Parse an (optional) new-declarator.
4639 new-declarator:
4640 ptr-operator new-declarator [opt]
4641 direct-new-declarator
4643 Returns a representation of the declarator. See
4644 cp_parser_declarator for the representations used. */
4646 static tree
4647 cp_parser_new_declarator_opt (cp_parser* parser)
4649 enum tree_code code;
4650 tree type;
4651 tree cv_qualifier_seq;
4653 /* We don't know if there's a ptr-operator next, or not. */
4654 cp_parser_parse_tentatively (parser);
4655 /* Look for a ptr-operator. */
4656 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4657 /* If that worked, look for more new-declarators. */
4658 if (cp_parser_parse_definitely (parser))
4660 tree declarator;
4662 /* Parse another optional declarator. */
4663 declarator = cp_parser_new_declarator_opt (parser);
4665 /* Create the representation of the declarator. */
4666 if (code == INDIRECT_REF)
4667 declarator = make_pointer_declarator (cv_qualifier_seq,
4668 declarator);
4669 else
4670 declarator = make_reference_declarator (cv_qualifier_seq,
4671 declarator);
4673 /* Handle the pointer-to-member case. */
4674 if (type)
4675 declarator = build_nt (SCOPE_REF, type, declarator);
4677 return declarator;
4680 /* If the next token is a `[', there is a direct-new-declarator. */
4681 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4682 return cp_parser_direct_new_declarator (parser);
4684 return NULL_TREE;
4687 /* Parse a direct-new-declarator.
4689 direct-new-declarator:
4690 [ expression ]
4691 direct-new-declarator [constant-expression]
4693 Returns an ARRAY_REF, following the same conventions as are
4694 documented for cp_parser_direct_declarator. */
4696 static tree
4697 cp_parser_direct_new_declarator (cp_parser* parser)
4699 tree declarator = NULL_TREE;
4701 while (true)
4703 tree expression;
4705 /* Look for the opening `['. */
4706 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4707 /* The first expression is not required to be constant. */
4708 if (!declarator)
4710 expression = cp_parser_expression (parser);
4711 /* The standard requires that the expression have integral
4712 type. DR 74 adds enumeration types. We believe that the
4713 real intent is that these expressions be handled like the
4714 expression in a `switch' condition, which also allows
4715 classes with a single conversion to integral or
4716 enumeration type. */
4717 if (!processing_template_decl)
4719 expression
4720 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4721 expression,
4722 /*complain=*/true);
4723 if (!expression)
4725 error ("expression in new-declarator must have integral or enumeration type");
4726 expression = error_mark_node;
4730 /* But all the other expressions must be. */
4731 else
4732 expression
4733 = cp_parser_constant_expression (parser,
4734 /*allow_non_constant=*/false,
4735 NULL);
4736 /* Look for the closing `]'. */
4737 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4739 /* Add this bound to the declarator. */
4740 declarator = build_nt (ARRAY_REF, declarator, expression);
4742 /* If the next token is not a `[', then there are no more
4743 bounds. */
4744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4745 break;
4748 return declarator;
4751 /* Parse a new-initializer.
4753 new-initializer:
4754 ( expression-list [opt] )
4756 Returns a reprsentation of the expression-list. If there is no
4757 expression-list, VOID_ZERO_NODE is returned. */
4759 static tree
4760 cp_parser_new_initializer (cp_parser* parser)
4762 tree expression_list;
4764 /* Look for the opening parenthesis. */
4765 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4766 /* If the next token is not a `)', then there is an
4767 expression-list. */
4768 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4769 expression_list = cp_parser_expression_list (parser);
4770 else
4771 expression_list = void_zero_node;
4772 /* Look for the closing parenthesis. */
4773 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4775 return expression_list;
4778 /* Parse a delete-expression.
4780 delete-expression:
4781 :: [opt] delete cast-expression
4782 :: [opt] delete [ ] cast-expression
4784 Returns a representation of the expression. */
4786 static tree
4787 cp_parser_delete_expression (cp_parser* parser)
4789 bool global_scope_p;
4790 bool array_p;
4791 tree expression;
4793 /* Look for the optional `::' operator. */
4794 global_scope_p
4795 = (cp_parser_global_scope_opt (parser,
4796 /*current_scope_valid_p=*/false)
4797 != NULL_TREE);
4798 /* Look for the `delete' keyword. */
4799 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4800 /* See if the array syntax is in use. */
4801 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4803 /* Consume the `[' token. */
4804 cp_lexer_consume_token (parser->lexer);
4805 /* Look for the `]' token. */
4806 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4807 /* Remember that this is the `[]' construct. */
4808 array_p = true;
4810 else
4811 array_p = false;
4813 /* Parse the cast-expression. */
4814 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
4816 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4819 /* Parse a cast-expression.
4821 cast-expression:
4822 unary-expression
4823 ( type-id ) cast-expression
4825 Returns a representation of the expression. */
4827 static tree
4828 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4830 /* If it's a `(', then we might be looking at a cast. */
4831 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4833 tree type = NULL_TREE;
4834 tree expr = NULL_TREE;
4835 bool compound_literal_p;
4836 const char *saved_message;
4838 /* There's no way to know yet whether or not this is a cast.
4839 For example, `(int (3))' is a unary-expression, while `(int)
4840 3' is a cast. So, we resort to parsing tentatively. */
4841 cp_parser_parse_tentatively (parser);
4842 /* Types may not be defined in a cast. */
4843 saved_message = parser->type_definition_forbidden_message;
4844 parser->type_definition_forbidden_message
4845 = "types may not be defined in casts";
4846 /* Consume the `('. */
4847 cp_lexer_consume_token (parser->lexer);
4848 /* A very tricky bit is that `(struct S) { 3 }' is a
4849 compound-literal (which we permit in C++ as an extension).
4850 But, that construct is not a cast-expression -- it is a
4851 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4852 is legal; if the compound-literal were a cast-expression,
4853 you'd need an extra set of parentheses.) But, if we parse
4854 the type-id, and it happens to be a class-specifier, then we
4855 will commit to the parse at that point, because we cannot
4856 undo the action that is done when creating a new class. So,
4857 then we cannot back up and do a postfix-expression.
4859 Therefore, we scan ahead to the closing `)', and check to see
4860 if the token after the `)' is a `{'. If so, we are not
4861 looking at a cast-expression.
4863 Save tokens so that we can put them back. */
4864 cp_lexer_save_tokens (parser->lexer);
4865 /* Skip tokens until the next token is a closing parenthesis.
4866 If we find the closing `)', and the next token is a `{', then
4867 we are looking at a compound-literal. */
4868 compound_literal_p
4869 = (cp_parser_skip_to_closing_parenthesis (parser)
4870 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4871 /* Roll back the tokens we skipped. */
4872 cp_lexer_rollback_tokens (parser->lexer);
4873 /* If we were looking at a compound-literal, simulate an error
4874 so that the call to cp_parser_parse_definitely below will
4875 fail. */
4876 if (compound_literal_p)
4877 cp_parser_simulate_error (parser);
4878 else
4880 /* Look for the type-id. */
4881 type = cp_parser_type_id (parser);
4882 /* Look for the closing `)'. */
4883 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4886 /* Restore the saved message. */
4887 parser->type_definition_forbidden_message = saved_message;
4889 /* If ok so far, parse the dependent expression. We cannot be
4890 sure it is a cast. Consider `(T ())'. It is a parenthesized
4891 ctor of T, but looks like a cast to function returning T
4892 without a dependent expression. */
4893 if (!cp_parser_error_occurred (parser))
4894 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4896 if (cp_parser_parse_definitely (parser))
4898 /* Warn about old-style casts, if so requested. */
4899 if (warn_old_style_cast
4900 && !in_system_header
4901 && !VOID_TYPE_P (type)
4902 && current_lang_name != lang_name_c)
4903 warning ("use of old-style cast");
4905 /* Only type conversions to integral or enumeration types
4906 can be used in constant-expressions. */
4907 if (parser->constant_expression_p
4908 && !dependent_type_p (type)
4909 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4911 if (!parser->allow_non_constant_expression_p)
4912 return (cp_parser_non_constant_expression
4913 ("a casts to a type other than an integral or "
4914 "enumeration type"));
4915 parser->non_constant_expression_p = true;
4917 /* Perform the cast. */
4918 expr = build_c_cast (type, expr);
4919 return expr;
4923 /* If we get here, then it's not a cast, so it must be a
4924 unary-expression. */
4925 return cp_parser_unary_expression (parser, address_p);
4928 /* Parse a pm-expression.
4930 pm-expression:
4931 cast-expression
4932 pm-expression .* cast-expression
4933 pm-expression ->* cast-expression
4935 Returns a representation of the expression. */
4937 static tree
4938 cp_parser_pm_expression (cp_parser* parser)
4940 tree cast_expr;
4941 tree pm_expr;
4943 /* Parse the cast-expresion. */
4944 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4945 pm_expr = cast_expr;
4946 /* Now look for pointer-to-member operators. */
4947 while (true)
4949 cp_token *token;
4950 enum cpp_ttype token_type;
4952 /* Peek at the next token. */
4953 token = cp_lexer_peek_token (parser->lexer);
4954 token_type = token->type;
4955 /* If it's not `.*' or `->*' there's no pointer-to-member
4956 operation. */
4957 if (token_type != CPP_DOT_STAR
4958 && token_type != CPP_DEREF_STAR)
4959 break;
4961 /* Consume the token. */
4962 cp_lexer_consume_token (parser->lexer);
4964 /* Parse another cast-expression. */
4965 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4967 /* Build the representation of the pointer-to-member
4968 operation. */
4969 if (token_type == CPP_DEREF_STAR)
4970 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
4971 else
4972 pm_expr = build_m_component_ref (pm_expr, cast_expr);
4975 return pm_expr;
4978 /* Parse a multiplicative-expression.
4980 mulitplicative-expression:
4981 pm-expression
4982 multiplicative-expression * pm-expression
4983 multiplicative-expression / pm-expression
4984 multiplicative-expression % pm-expression
4986 Returns a representation of the expression. */
4988 static tree
4989 cp_parser_multiplicative_expression (cp_parser* parser)
4991 static const cp_parser_token_tree_map map = {
4992 { CPP_MULT, MULT_EXPR },
4993 { CPP_DIV, TRUNC_DIV_EXPR },
4994 { CPP_MOD, TRUNC_MOD_EXPR },
4995 { CPP_EOF, ERROR_MARK }
4998 return cp_parser_binary_expression (parser,
4999 map,
5000 cp_parser_pm_expression);
5003 /* Parse an additive-expression.
5005 additive-expression:
5006 multiplicative-expression
5007 additive-expression + multiplicative-expression
5008 additive-expression - multiplicative-expression
5010 Returns a representation of the expression. */
5012 static tree
5013 cp_parser_additive_expression (cp_parser* parser)
5015 static const cp_parser_token_tree_map map = {
5016 { CPP_PLUS, PLUS_EXPR },
5017 { CPP_MINUS, MINUS_EXPR },
5018 { CPP_EOF, ERROR_MARK }
5021 return cp_parser_binary_expression (parser,
5022 map,
5023 cp_parser_multiplicative_expression);
5026 /* Parse a shift-expression.
5028 shift-expression:
5029 additive-expression
5030 shift-expression << additive-expression
5031 shift-expression >> additive-expression
5033 Returns a representation of the expression. */
5035 static tree
5036 cp_parser_shift_expression (cp_parser* parser)
5038 static const cp_parser_token_tree_map map = {
5039 { CPP_LSHIFT, LSHIFT_EXPR },
5040 { CPP_RSHIFT, RSHIFT_EXPR },
5041 { CPP_EOF, ERROR_MARK }
5044 return cp_parser_binary_expression (parser,
5045 map,
5046 cp_parser_additive_expression);
5049 /* Parse a relational-expression.
5051 relational-expression:
5052 shift-expression
5053 relational-expression < shift-expression
5054 relational-expression > shift-expression
5055 relational-expression <= shift-expression
5056 relational-expression >= shift-expression
5058 GNU Extension:
5060 relational-expression:
5061 relational-expression <? shift-expression
5062 relational-expression >? shift-expression
5064 Returns a representation of the expression. */
5066 static tree
5067 cp_parser_relational_expression (cp_parser* parser)
5069 static const cp_parser_token_tree_map map = {
5070 { CPP_LESS, LT_EXPR },
5071 { CPP_GREATER, GT_EXPR },
5072 { CPP_LESS_EQ, LE_EXPR },
5073 { CPP_GREATER_EQ, GE_EXPR },
5074 { CPP_MIN, MIN_EXPR },
5075 { CPP_MAX, MAX_EXPR },
5076 { CPP_EOF, ERROR_MARK }
5079 return cp_parser_binary_expression (parser,
5080 map,
5081 cp_parser_shift_expression);
5084 /* Parse an equality-expression.
5086 equality-expression:
5087 relational-expression
5088 equality-expression == relational-expression
5089 equality-expression != relational-expression
5091 Returns a representation of the expression. */
5093 static tree
5094 cp_parser_equality_expression (cp_parser* parser)
5096 static const cp_parser_token_tree_map map = {
5097 { CPP_EQ_EQ, EQ_EXPR },
5098 { CPP_NOT_EQ, NE_EXPR },
5099 { CPP_EOF, ERROR_MARK }
5102 return cp_parser_binary_expression (parser,
5103 map,
5104 cp_parser_relational_expression);
5107 /* Parse an and-expression.
5109 and-expression:
5110 equality-expression
5111 and-expression & equality-expression
5113 Returns a representation of the expression. */
5115 static tree
5116 cp_parser_and_expression (cp_parser* parser)
5118 static const cp_parser_token_tree_map map = {
5119 { CPP_AND, BIT_AND_EXPR },
5120 { CPP_EOF, ERROR_MARK }
5123 return cp_parser_binary_expression (parser,
5124 map,
5125 cp_parser_equality_expression);
5128 /* Parse an exclusive-or-expression.
5130 exclusive-or-expression:
5131 and-expression
5132 exclusive-or-expression ^ and-expression
5134 Returns a representation of the expression. */
5136 static tree
5137 cp_parser_exclusive_or_expression (cp_parser* parser)
5139 static const cp_parser_token_tree_map map = {
5140 { CPP_XOR, BIT_XOR_EXPR },
5141 { CPP_EOF, ERROR_MARK }
5144 return cp_parser_binary_expression (parser,
5145 map,
5146 cp_parser_and_expression);
5150 /* Parse an inclusive-or-expression.
5152 inclusive-or-expression:
5153 exclusive-or-expression
5154 inclusive-or-expression | exclusive-or-expression
5156 Returns a representation of the expression. */
5158 static tree
5159 cp_parser_inclusive_or_expression (cp_parser* parser)
5161 static const cp_parser_token_tree_map map = {
5162 { CPP_OR, BIT_IOR_EXPR },
5163 { CPP_EOF, ERROR_MARK }
5166 return cp_parser_binary_expression (parser,
5167 map,
5168 cp_parser_exclusive_or_expression);
5171 /* Parse a logical-and-expression.
5173 logical-and-expression:
5174 inclusive-or-expression
5175 logical-and-expression && inclusive-or-expression
5177 Returns a representation of the expression. */
5179 static tree
5180 cp_parser_logical_and_expression (cp_parser* parser)
5182 static const cp_parser_token_tree_map map = {
5183 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5184 { CPP_EOF, ERROR_MARK }
5187 return cp_parser_binary_expression (parser,
5188 map,
5189 cp_parser_inclusive_or_expression);
5192 /* Parse a logical-or-expression.
5194 logical-or-expression:
5195 logical-and-expresion
5196 logical-or-expression || logical-and-expression
5198 Returns a representation of the expression. */
5200 static tree
5201 cp_parser_logical_or_expression (cp_parser* parser)
5203 static const cp_parser_token_tree_map map = {
5204 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5205 { CPP_EOF, ERROR_MARK }
5208 return cp_parser_binary_expression (parser,
5209 map,
5210 cp_parser_logical_and_expression);
5213 /* Parse a conditional-expression.
5215 conditional-expression:
5216 logical-or-expression
5217 logical-or-expression ? expression : assignment-expression
5219 GNU Extensions:
5221 conditional-expression:
5222 logical-or-expression ? : assignment-expression
5224 Returns a representation of the expression. */
5226 static tree
5227 cp_parser_conditional_expression (cp_parser* parser)
5229 tree logical_or_expr;
5231 /* Parse the logical-or-expression. */
5232 logical_or_expr = cp_parser_logical_or_expression (parser);
5233 /* If the next token is a `?', then we have a real conditional
5234 expression. */
5235 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5236 return cp_parser_question_colon_clause (parser, logical_or_expr);
5237 /* Otherwise, the value is simply the logical-or-expression. */
5238 else
5239 return logical_or_expr;
5242 /* Parse the `? expression : assignment-expression' part of a
5243 conditional-expression. The LOGICAL_OR_EXPR is the
5244 logical-or-expression that started the conditional-expression.
5245 Returns a representation of the entire conditional-expression.
5247 This routine exists only so that it can be shared between
5248 cp_parser_conditional_expression and
5249 cp_parser_assignment_expression.
5251 ? expression : assignment-expression
5253 GNU Extensions:
5255 ? : assignment-expression */
5257 static tree
5258 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5260 tree expr;
5261 tree assignment_expr;
5263 /* Consume the `?' token. */
5264 cp_lexer_consume_token (parser->lexer);
5265 if (cp_parser_allow_gnu_extensions_p (parser)
5266 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5267 /* Implicit true clause. */
5268 expr = NULL_TREE;
5269 else
5270 /* Parse the expression. */
5271 expr = cp_parser_expression (parser);
5273 /* The next token should be a `:'. */
5274 cp_parser_require (parser, CPP_COLON, "`:'");
5275 /* Parse the assignment-expression. */
5276 assignment_expr = cp_parser_assignment_expression (parser);
5278 /* Build the conditional-expression. */
5279 return build_x_conditional_expr (logical_or_expr,
5280 expr,
5281 assignment_expr);
5284 /* Parse an assignment-expression.
5286 assignment-expression:
5287 conditional-expression
5288 logical-or-expression assignment-operator assignment_expression
5289 throw-expression
5291 Returns a representation for the expression. */
5293 static tree
5294 cp_parser_assignment_expression (cp_parser* parser)
5296 tree expr;
5298 /* If the next token is the `throw' keyword, then we're looking at
5299 a throw-expression. */
5300 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5301 expr = cp_parser_throw_expression (parser);
5302 /* Otherwise, it must be that we are looking at a
5303 logical-or-expression. */
5304 else
5306 /* Parse the logical-or-expression. */
5307 expr = cp_parser_logical_or_expression (parser);
5308 /* If the next token is a `?' then we're actually looking at a
5309 conditional-expression. */
5310 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5311 return cp_parser_question_colon_clause (parser, expr);
5312 else
5314 enum tree_code assignment_operator;
5316 /* If it's an assignment-operator, we're using the second
5317 production. */
5318 assignment_operator
5319 = cp_parser_assignment_operator_opt (parser);
5320 if (assignment_operator != ERROR_MARK)
5322 tree rhs;
5324 /* Parse the right-hand side of the assignment. */
5325 rhs = cp_parser_assignment_expression (parser);
5326 /* An assignment may not appear in a
5327 constant-expression. */
5328 if (parser->constant_expression_p)
5330 if (!parser->allow_non_constant_expression_p)
5331 return cp_parser_non_constant_expression ("an assignment");
5332 parser->non_constant_expression_p = true;
5334 /* Build the asignment expression. */
5335 expr = build_x_modify_expr (expr,
5336 assignment_operator,
5337 rhs);
5342 return expr;
5345 /* Parse an (optional) assignment-operator.
5347 assignment-operator: one of
5348 = *= /= %= += -= >>= <<= &= ^= |=
5350 GNU Extension:
5352 assignment-operator: one of
5353 <?= >?=
5355 If the next token is an assignment operator, the corresponding tree
5356 code is returned, and the token is consumed. For example, for
5357 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5358 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5359 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5360 operator, ERROR_MARK is returned. */
5362 static enum tree_code
5363 cp_parser_assignment_operator_opt (cp_parser* parser)
5365 enum tree_code op;
5366 cp_token *token;
5368 /* Peek at the next toen. */
5369 token = cp_lexer_peek_token (parser->lexer);
5371 switch (token->type)
5373 case CPP_EQ:
5374 op = NOP_EXPR;
5375 break;
5377 case CPP_MULT_EQ:
5378 op = MULT_EXPR;
5379 break;
5381 case CPP_DIV_EQ:
5382 op = TRUNC_DIV_EXPR;
5383 break;
5385 case CPP_MOD_EQ:
5386 op = TRUNC_MOD_EXPR;
5387 break;
5389 case CPP_PLUS_EQ:
5390 op = PLUS_EXPR;
5391 break;
5393 case CPP_MINUS_EQ:
5394 op = MINUS_EXPR;
5395 break;
5397 case CPP_RSHIFT_EQ:
5398 op = RSHIFT_EXPR;
5399 break;
5401 case CPP_LSHIFT_EQ:
5402 op = LSHIFT_EXPR;
5403 break;
5405 case CPP_AND_EQ:
5406 op = BIT_AND_EXPR;
5407 break;
5409 case CPP_XOR_EQ:
5410 op = BIT_XOR_EXPR;
5411 break;
5413 case CPP_OR_EQ:
5414 op = BIT_IOR_EXPR;
5415 break;
5417 case CPP_MIN_EQ:
5418 op = MIN_EXPR;
5419 break;
5421 case CPP_MAX_EQ:
5422 op = MAX_EXPR;
5423 break;
5425 default:
5426 /* Nothing else is an assignment operator. */
5427 op = ERROR_MARK;
5430 /* If it was an assignment operator, consume it. */
5431 if (op != ERROR_MARK)
5432 cp_lexer_consume_token (parser->lexer);
5434 return op;
5437 /* Parse an expression.
5439 expression:
5440 assignment-expression
5441 expression , assignment-expression
5443 Returns a representation of the expression. */
5445 static tree
5446 cp_parser_expression (cp_parser* parser)
5448 tree expression = NULL_TREE;
5449 bool saw_comma_p = false;
5451 while (true)
5453 tree assignment_expression;
5455 /* Parse the next assignment-expression. */
5456 assignment_expression
5457 = cp_parser_assignment_expression (parser);
5458 /* If this is the first assignment-expression, we can just
5459 save it away. */
5460 if (!expression)
5461 expression = assignment_expression;
5462 /* Otherwise, chain the expressions together. It is unclear why
5463 we do not simply build COMPOUND_EXPRs as we go. */
5464 else
5465 expression = tree_cons (NULL_TREE,
5466 assignment_expression,
5467 expression);
5468 /* If the next token is not a comma, then we are done with the
5469 expression. */
5470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5471 break;
5472 /* Consume the `,'. */
5473 cp_lexer_consume_token (parser->lexer);
5474 /* The first time we see a `,', we must take special action
5475 because the representation used for a single expression is
5476 different from that used for a list containing the single
5477 expression. */
5478 if (!saw_comma_p)
5480 /* Remember that this expression has a `,' in it. */
5481 saw_comma_p = true;
5482 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5483 additional expressions to it. */
5484 expression = build_tree_list (NULL_TREE, expression);
5488 /* Build a COMPOUND_EXPR to represent the entire expression, if
5489 necessary. We built up the list in reverse order, so we must
5490 straighten it out here. */
5491 if (saw_comma_p)
5493 /* A comma operator cannot appear in a constant-expression. */
5494 if (parser->constant_expression_p)
5496 if (!parser->allow_non_constant_expression_p)
5497 return cp_parser_non_constant_expression ("a comma operator");
5498 parser->non_constant_expression_p = true;
5500 expression = build_x_compound_expr (nreverse (expression));
5503 return expression;
5506 /* Parse a constant-expression.
5508 constant-expression:
5509 conditional-expression
5511 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5512 accepted. In that case *NON_CONSTANT_P is set to TRUE. If
5513 ALLOW_NON_CONSTANT_P is false, NON_CONSTANT_P should be NULL. */
5515 static tree
5516 cp_parser_constant_expression (cp_parser* parser,
5517 bool allow_non_constant_p,
5518 bool *non_constant_p)
5520 bool saved_constant_expression_p;
5521 bool saved_allow_non_constant_expression_p;
5522 bool saved_non_constant_expression_p;
5523 tree expression;
5525 /* It might seem that we could simply parse the
5526 conditional-expression, and then check to see if it were
5527 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5528 one that the compiler can figure out is constant, possibly after
5529 doing some simplifications or optimizations. The standard has a
5530 precise definition of constant-expression, and we must honor
5531 that, even though it is somewhat more restrictive.
5533 For example:
5535 int i[(2, 3)];
5537 is not a legal declaration, because `(2, 3)' is not a
5538 constant-expression. The `,' operator is forbidden in a
5539 constant-expression. However, GCC's constant-folding machinery
5540 will fold this operation to an INTEGER_CST for `3'. */
5542 /* Save the old settings. */
5543 saved_constant_expression_p = parser->constant_expression_p;
5544 saved_allow_non_constant_expression_p
5545 = parser->allow_non_constant_expression_p;
5546 saved_non_constant_expression_p = parser->non_constant_expression_p;
5547 /* We are now parsing a constant-expression. */
5548 parser->constant_expression_p = true;
5549 parser->allow_non_constant_expression_p = allow_non_constant_p;
5550 parser->non_constant_expression_p = false;
5551 /* Parse the conditional-expression. */
5552 expression = cp_parser_conditional_expression (parser);
5553 /* Restore the old settings. */
5554 parser->constant_expression_p = saved_constant_expression_p;
5555 parser->allow_non_constant_expression_p
5556 = saved_allow_non_constant_expression_p;
5557 if (allow_non_constant_p)
5558 *non_constant_p = parser->non_constant_expression_p;
5559 parser->non_constant_expression_p = saved_non_constant_expression_p;
5561 return expression;
5564 /* Statements [gram.stmt.stmt] */
5566 /* Parse a statement.
5568 statement:
5569 labeled-statement
5570 expression-statement
5571 compound-statement
5572 selection-statement
5573 iteration-statement
5574 jump-statement
5575 declaration-statement
5576 try-block */
5578 static void
5579 cp_parser_statement (cp_parser* parser)
5581 tree statement;
5582 cp_token *token;
5583 int statement_line_number;
5585 /* There is no statement yet. */
5586 statement = NULL_TREE;
5587 /* Peek at the next token. */
5588 token = cp_lexer_peek_token (parser->lexer);
5589 /* Remember the line number of the first token in the statement. */
5590 statement_line_number = token->location.line;
5591 /* If this is a keyword, then that will often determine what kind of
5592 statement we have. */
5593 if (token->type == CPP_KEYWORD)
5595 enum rid keyword = token->keyword;
5597 switch (keyword)
5599 case RID_CASE:
5600 case RID_DEFAULT:
5601 statement = cp_parser_labeled_statement (parser);
5602 break;
5604 case RID_IF:
5605 case RID_SWITCH:
5606 statement = cp_parser_selection_statement (parser);
5607 break;
5609 case RID_WHILE:
5610 case RID_DO:
5611 case RID_FOR:
5612 statement = cp_parser_iteration_statement (parser);
5613 break;
5615 case RID_BREAK:
5616 case RID_CONTINUE:
5617 case RID_RETURN:
5618 case RID_GOTO:
5619 statement = cp_parser_jump_statement (parser);
5620 break;
5622 case RID_TRY:
5623 statement = cp_parser_try_block (parser);
5624 break;
5626 default:
5627 /* It might be a keyword like `int' that can start a
5628 declaration-statement. */
5629 break;
5632 else if (token->type == CPP_NAME)
5634 /* If the next token is a `:', then we are looking at a
5635 labeled-statement. */
5636 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5637 if (token->type == CPP_COLON)
5638 statement = cp_parser_labeled_statement (parser);
5640 /* Anything that starts with a `{' must be a compound-statement. */
5641 else if (token->type == CPP_OPEN_BRACE)
5642 statement = cp_parser_compound_statement (parser);
5644 /* Everything else must be a declaration-statement or an
5645 expression-statement. Try for the declaration-statement
5646 first, unless we are looking at a `;', in which case we know that
5647 we have an expression-statement. */
5648 if (!statement)
5650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5652 cp_parser_parse_tentatively (parser);
5653 /* Try to parse the declaration-statement. */
5654 cp_parser_declaration_statement (parser);
5655 /* If that worked, we're done. */
5656 if (cp_parser_parse_definitely (parser))
5657 return;
5659 /* Look for an expression-statement instead. */
5660 statement = cp_parser_expression_statement (parser);
5663 /* Set the line number for the statement. */
5664 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5665 STMT_LINENO (statement) = statement_line_number;
5668 /* Parse a labeled-statement.
5670 labeled-statement:
5671 identifier : statement
5672 case constant-expression : statement
5673 default : statement
5675 Returns the new CASE_LABEL, for a `case' or `default' label. For
5676 an ordinary label, returns a LABEL_STMT. */
5678 static tree
5679 cp_parser_labeled_statement (cp_parser* parser)
5681 cp_token *token;
5682 tree statement = NULL_TREE;
5684 /* The next token should be an identifier. */
5685 token = cp_lexer_peek_token (parser->lexer);
5686 if (token->type != CPP_NAME
5687 && token->type != CPP_KEYWORD)
5689 cp_parser_error (parser, "expected labeled-statement");
5690 return error_mark_node;
5693 switch (token->keyword)
5695 case RID_CASE:
5697 tree expr;
5699 /* Consume the `case' token. */
5700 cp_lexer_consume_token (parser->lexer);
5701 /* Parse the constant-expression. */
5702 expr = cp_parser_constant_expression (parser,
5703 /*allow_non_constant=*/false,
5704 NULL);
5705 /* Create the label. */
5706 statement = finish_case_label (expr, NULL_TREE);
5708 break;
5710 case RID_DEFAULT:
5711 /* Consume the `default' token. */
5712 cp_lexer_consume_token (parser->lexer);
5713 /* Create the label. */
5714 statement = finish_case_label (NULL_TREE, NULL_TREE);
5715 break;
5717 default:
5718 /* Anything else must be an ordinary label. */
5719 statement = finish_label_stmt (cp_parser_identifier (parser));
5720 break;
5723 /* Require the `:' token. */
5724 cp_parser_require (parser, CPP_COLON, "`:'");
5725 /* Parse the labeled statement. */
5726 cp_parser_statement (parser);
5728 /* Return the label, in the case of a `case' or `default' label. */
5729 return statement;
5732 /* Parse an expression-statement.
5734 expression-statement:
5735 expression [opt] ;
5737 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5738 statement consists of nothing more than an `;'. */
5740 static tree
5741 cp_parser_expression_statement (cp_parser* parser)
5743 tree statement;
5745 /* If the next token is not a `;', then there is an expression to parse. */
5746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5747 statement = finish_expr_stmt (cp_parser_expression (parser));
5748 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5749 else
5751 finish_stmt ();
5752 statement = NULL_TREE;
5754 /* Consume the final `;'. */
5755 cp_parser_consume_semicolon_at_end_of_statement (parser);
5757 return statement;
5760 /* Parse a compound-statement.
5762 compound-statement:
5763 { statement-seq [opt] }
5765 Returns a COMPOUND_STMT representing the statement. */
5767 static tree
5768 cp_parser_compound_statement (cp_parser *parser)
5770 tree compound_stmt;
5772 /* Consume the `{'. */
5773 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5774 return error_mark_node;
5775 /* Begin the compound-statement. */
5776 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5777 /* Parse an (optional) statement-seq. */
5778 cp_parser_statement_seq_opt (parser);
5779 /* Finish the compound-statement. */
5780 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5781 /* Consume the `}'. */
5782 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5784 return compound_stmt;
5787 /* Parse an (optional) statement-seq.
5789 statement-seq:
5790 statement
5791 statement-seq [opt] statement */
5793 static void
5794 cp_parser_statement_seq_opt (cp_parser* parser)
5796 /* Scan statements until there aren't any more. */
5797 while (true)
5799 /* If we're looking at a `}', then we've run out of statements. */
5800 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5801 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5802 break;
5804 /* Parse the statement. */
5805 cp_parser_statement (parser);
5809 /* Parse a selection-statement.
5811 selection-statement:
5812 if ( condition ) statement
5813 if ( condition ) statement else statement
5814 switch ( condition ) statement
5816 Returns the new IF_STMT or SWITCH_STMT. */
5818 static tree
5819 cp_parser_selection_statement (cp_parser* parser)
5821 cp_token *token;
5822 enum rid keyword;
5824 /* Peek at the next token. */
5825 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5827 /* See what kind of keyword it is. */
5828 keyword = token->keyword;
5829 switch (keyword)
5831 case RID_IF:
5832 case RID_SWITCH:
5834 tree statement;
5835 tree condition;
5837 /* Look for the `('. */
5838 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5840 cp_parser_skip_to_end_of_statement (parser);
5841 return error_mark_node;
5844 /* Begin the selection-statement. */
5845 if (keyword == RID_IF)
5846 statement = begin_if_stmt ();
5847 else
5848 statement = begin_switch_stmt ();
5850 /* Parse the condition. */
5851 condition = cp_parser_condition (parser);
5852 /* Look for the `)'. */
5853 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5854 cp_parser_skip_to_closing_parenthesis (parser);
5856 if (keyword == RID_IF)
5858 tree then_stmt;
5860 /* Add the condition. */
5861 finish_if_stmt_cond (condition, statement);
5863 /* Parse the then-clause. */
5864 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5865 finish_then_clause (statement);
5867 /* If the next token is `else', parse the else-clause. */
5868 if (cp_lexer_next_token_is_keyword (parser->lexer,
5869 RID_ELSE))
5871 tree else_stmt;
5873 /* Consume the `else' keyword. */
5874 cp_lexer_consume_token (parser->lexer);
5875 /* Parse the else-clause. */
5876 else_stmt
5877 = cp_parser_implicitly_scoped_statement (parser);
5878 finish_else_clause (statement);
5881 /* Now we're all done with the if-statement. */
5882 finish_if_stmt ();
5884 else
5886 tree body;
5888 /* Add the condition. */
5889 finish_switch_cond (condition, statement);
5891 /* Parse the body of the switch-statement. */
5892 body = cp_parser_implicitly_scoped_statement (parser);
5894 /* Now we're all done with the switch-statement. */
5895 finish_switch_stmt (statement);
5898 return statement;
5900 break;
5902 default:
5903 cp_parser_error (parser, "expected selection-statement");
5904 return error_mark_node;
5908 /* Parse a condition.
5910 condition:
5911 expression
5912 type-specifier-seq declarator = assignment-expression
5914 GNU Extension:
5916 condition:
5917 type-specifier-seq declarator asm-specification [opt]
5918 attributes [opt] = assignment-expression
5920 Returns the expression that should be tested. */
5922 static tree
5923 cp_parser_condition (cp_parser* parser)
5925 tree type_specifiers;
5926 const char *saved_message;
5928 /* Try the declaration first. */
5929 cp_parser_parse_tentatively (parser);
5930 /* New types are not allowed in the type-specifier-seq for a
5931 condition. */
5932 saved_message = parser->type_definition_forbidden_message;
5933 parser->type_definition_forbidden_message
5934 = "types may not be defined in conditions";
5935 /* Parse the type-specifier-seq. */
5936 type_specifiers = cp_parser_type_specifier_seq (parser);
5937 /* Restore the saved message. */
5938 parser->type_definition_forbidden_message = saved_message;
5939 /* If all is well, we might be looking at a declaration. */
5940 if (!cp_parser_error_occurred (parser))
5942 tree decl;
5943 tree asm_specification;
5944 tree attributes;
5945 tree declarator;
5946 tree initializer = NULL_TREE;
5948 /* Parse the declarator. */
5949 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5950 /*ctor_dtor_or_conv_p=*/NULL);
5951 /* Parse the attributes. */
5952 attributes = cp_parser_attributes_opt (parser);
5953 /* Parse the asm-specification. */
5954 asm_specification = cp_parser_asm_specification_opt (parser);
5955 /* If the next token is not an `=', then we might still be
5956 looking at an expression. For example:
5958 if (A(a).x)
5960 looks like a decl-specifier-seq and a declarator -- but then
5961 there is no `=', so this is an expression. */
5962 cp_parser_require (parser, CPP_EQ, "`='");
5963 /* If we did see an `=', then we are looking at a declaration
5964 for sure. */
5965 if (cp_parser_parse_definitely (parser))
5967 /* Create the declaration. */
5968 decl = start_decl (declarator, type_specifiers,
5969 /*initialized_p=*/true,
5970 attributes, /*prefix_attributes=*/NULL_TREE);
5971 /* Parse the assignment-expression. */
5972 initializer = cp_parser_assignment_expression (parser);
5974 /* Process the initializer. */
5975 cp_finish_decl (decl,
5976 initializer,
5977 asm_specification,
5978 LOOKUP_ONLYCONVERTING);
5980 return convert_from_reference (decl);
5983 /* If we didn't even get past the declarator successfully, we are
5984 definitely not looking at a declaration. */
5985 else
5986 cp_parser_abort_tentative_parse (parser);
5988 /* Otherwise, we are looking at an expression. */
5989 return cp_parser_expression (parser);
5992 /* Parse an iteration-statement.
5994 iteration-statement:
5995 while ( condition ) statement
5996 do statement while ( expression ) ;
5997 for ( for-init-statement condition [opt] ; expression [opt] )
5998 statement
6000 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6002 static tree
6003 cp_parser_iteration_statement (cp_parser* parser)
6005 cp_token *token;
6006 enum rid keyword;
6007 tree statement;
6009 /* Peek at the next token. */
6010 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6011 if (!token)
6012 return error_mark_node;
6014 /* See what kind of keyword it is. */
6015 keyword = token->keyword;
6016 switch (keyword)
6018 case RID_WHILE:
6020 tree condition;
6022 /* Begin the while-statement. */
6023 statement = begin_while_stmt ();
6024 /* Look for the `('. */
6025 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6026 /* Parse the condition. */
6027 condition = cp_parser_condition (parser);
6028 finish_while_stmt_cond (condition, statement);
6029 /* Look for the `)'. */
6030 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6031 /* Parse the dependent statement. */
6032 cp_parser_already_scoped_statement (parser);
6033 /* We're done with the while-statement. */
6034 finish_while_stmt (statement);
6036 break;
6038 case RID_DO:
6040 tree expression;
6042 /* Begin the do-statement. */
6043 statement = begin_do_stmt ();
6044 /* Parse the body of the do-statement. */
6045 cp_parser_implicitly_scoped_statement (parser);
6046 finish_do_body (statement);
6047 /* Look for the `while' keyword. */
6048 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6049 /* Look for the `('. */
6050 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6051 /* Parse the expression. */
6052 expression = cp_parser_expression (parser);
6053 /* We're done with the do-statement. */
6054 finish_do_stmt (expression, statement);
6055 /* Look for the `)'. */
6056 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6057 /* Look for the `;'. */
6058 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6060 break;
6062 case RID_FOR:
6064 tree condition = NULL_TREE;
6065 tree expression = NULL_TREE;
6067 /* Begin the for-statement. */
6068 statement = begin_for_stmt ();
6069 /* Look for the `('. */
6070 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6071 /* Parse the initialization. */
6072 cp_parser_for_init_statement (parser);
6073 finish_for_init_stmt (statement);
6075 /* If there's a condition, process it. */
6076 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6077 condition = cp_parser_condition (parser);
6078 finish_for_cond (condition, statement);
6079 /* Look for the `;'. */
6080 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6082 /* If there's an expression, process it. */
6083 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6084 expression = cp_parser_expression (parser);
6085 finish_for_expr (expression, statement);
6086 /* Look for the `)'. */
6087 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6089 /* Parse the body of the for-statement. */
6090 cp_parser_already_scoped_statement (parser);
6092 /* We're done with the for-statement. */
6093 finish_for_stmt (statement);
6095 break;
6097 default:
6098 cp_parser_error (parser, "expected iteration-statement");
6099 statement = error_mark_node;
6100 break;
6103 return statement;
6106 /* Parse a for-init-statement.
6108 for-init-statement:
6109 expression-statement
6110 simple-declaration */
6112 static void
6113 cp_parser_for_init_statement (cp_parser* parser)
6115 /* If the next token is a `;', then we have an empty
6116 expression-statement. Gramatically, this is also a
6117 simple-declaration, but an invalid one, because it does not
6118 declare anything. Therefore, if we did not handle this case
6119 specially, we would issue an error message about an invalid
6120 declaration. */
6121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6123 /* We're going to speculatively look for a declaration, falling back
6124 to an expression, if necessary. */
6125 cp_parser_parse_tentatively (parser);
6126 /* Parse the declaration. */
6127 cp_parser_simple_declaration (parser,
6128 /*function_definition_allowed_p=*/false);
6129 /* If the tentative parse failed, then we shall need to look for an
6130 expression-statement. */
6131 if (cp_parser_parse_definitely (parser))
6132 return;
6135 cp_parser_expression_statement (parser);
6138 /* Parse a jump-statement.
6140 jump-statement:
6141 break ;
6142 continue ;
6143 return expression [opt] ;
6144 goto identifier ;
6146 GNU extension:
6148 jump-statement:
6149 goto * expression ;
6151 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6152 GOTO_STMT. */
6154 static tree
6155 cp_parser_jump_statement (cp_parser* parser)
6157 tree statement = error_mark_node;
6158 cp_token *token;
6159 enum rid keyword;
6161 /* Peek at the next token. */
6162 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6163 if (!token)
6164 return error_mark_node;
6166 /* See what kind of keyword it is. */
6167 keyword = token->keyword;
6168 switch (keyword)
6170 case RID_BREAK:
6171 statement = finish_break_stmt ();
6172 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6173 break;
6175 case RID_CONTINUE:
6176 statement = finish_continue_stmt ();
6177 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6178 break;
6180 case RID_RETURN:
6182 tree expr;
6184 /* If the next token is a `;', then there is no
6185 expression. */
6186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6187 expr = cp_parser_expression (parser);
6188 else
6189 expr = NULL_TREE;
6190 /* Build the return-statement. */
6191 statement = finish_return_stmt (expr);
6192 /* Look for the final `;'. */
6193 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6195 break;
6197 case RID_GOTO:
6198 /* Create the goto-statement. */
6199 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6201 /* Issue a warning about this use of a GNU extension. */
6202 if (pedantic)
6203 pedwarn ("ISO C++ forbids computed gotos");
6204 /* Consume the '*' token. */
6205 cp_lexer_consume_token (parser->lexer);
6206 /* Parse the dependent expression. */
6207 finish_goto_stmt (cp_parser_expression (parser));
6209 else
6210 finish_goto_stmt (cp_parser_identifier (parser));
6211 /* Look for the final `;'. */
6212 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6213 break;
6215 default:
6216 cp_parser_error (parser, "expected jump-statement");
6217 break;
6220 return statement;
6223 /* Parse a declaration-statement.
6225 declaration-statement:
6226 block-declaration */
6228 static void
6229 cp_parser_declaration_statement (cp_parser* parser)
6231 /* Parse the block-declaration. */
6232 cp_parser_block_declaration (parser, /*statement_p=*/true);
6234 /* Finish off the statement. */
6235 finish_stmt ();
6238 /* Some dependent statements (like `if (cond) statement'), are
6239 implicitly in their own scope. In other words, if the statement is
6240 a single statement (as opposed to a compound-statement), it is
6241 none-the-less treated as if it were enclosed in braces. Any
6242 declarations appearing in the dependent statement are out of scope
6243 after control passes that point. This function parses a statement,
6244 but ensures that is in its own scope, even if it is not a
6245 compound-statement.
6247 Returns the new statement. */
6249 static tree
6250 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6252 tree statement;
6254 /* If the token is not a `{', then we must take special action. */
6255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6257 /* Create a compound-statement. */
6258 statement = begin_compound_stmt (/*has_no_scope=*/0);
6259 /* Parse the dependent-statement. */
6260 cp_parser_statement (parser);
6261 /* Finish the dummy compound-statement. */
6262 finish_compound_stmt (/*has_no_scope=*/0, statement);
6264 /* Otherwise, we simply parse the statement directly. */
6265 else
6266 statement = cp_parser_compound_statement (parser);
6268 /* Return the statement. */
6269 return statement;
6272 /* For some dependent statements (like `while (cond) statement'), we
6273 have already created a scope. Therefore, even if the dependent
6274 statement is a compound-statement, we do not want to create another
6275 scope. */
6277 static void
6278 cp_parser_already_scoped_statement (cp_parser* parser)
6280 /* If the token is not a `{', then we must take special action. */
6281 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6283 tree statement;
6285 /* Create a compound-statement. */
6286 statement = begin_compound_stmt (/*has_no_scope=*/1);
6287 /* Parse the dependent-statement. */
6288 cp_parser_statement (parser);
6289 /* Finish the dummy compound-statement. */
6290 finish_compound_stmt (/*has_no_scope=*/1, statement);
6292 /* Otherwise, we simply parse the statement directly. */
6293 else
6294 cp_parser_statement (parser);
6297 /* Declarations [gram.dcl.dcl] */
6299 /* Parse an optional declaration-sequence.
6301 declaration-seq:
6302 declaration
6303 declaration-seq declaration */
6305 static void
6306 cp_parser_declaration_seq_opt (cp_parser* parser)
6308 while (true)
6310 cp_token *token;
6312 token = cp_lexer_peek_token (parser->lexer);
6314 if (token->type == CPP_CLOSE_BRACE
6315 || token->type == CPP_EOF)
6316 break;
6318 if (token->type == CPP_SEMICOLON)
6320 /* A declaration consisting of a single semicolon is
6321 invalid. Allow it unless we're being pedantic. */
6322 if (pedantic)
6323 pedwarn ("extra `;'");
6324 cp_lexer_consume_token (parser->lexer);
6325 continue;
6328 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6329 parser to enter or exit implict `extern "C"' blocks. */
6330 while (pending_lang_change > 0)
6332 push_lang_context (lang_name_c);
6333 --pending_lang_change;
6335 while (pending_lang_change < 0)
6337 pop_lang_context ();
6338 ++pending_lang_change;
6341 /* Parse the declaration itself. */
6342 cp_parser_declaration (parser);
6346 /* Parse a declaration.
6348 declaration:
6349 block-declaration
6350 function-definition
6351 template-declaration
6352 explicit-instantiation
6353 explicit-specialization
6354 linkage-specification
6355 namespace-definition
6357 GNU extension:
6359 declaration:
6360 __extension__ declaration */
6362 static void
6363 cp_parser_declaration (cp_parser* parser)
6365 cp_token token1;
6366 cp_token token2;
6367 int saved_pedantic;
6369 /* Check for the `__extension__' keyword. */
6370 if (cp_parser_extension_opt (parser, &saved_pedantic))
6372 /* Parse the qualified declaration. */
6373 cp_parser_declaration (parser);
6374 /* Restore the PEDANTIC flag. */
6375 pedantic = saved_pedantic;
6377 return;
6380 /* Try to figure out what kind of declaration is present. */
6381 token1 = *cp_lexer_peek_token (parser->lexer);
6382 if (token1.type != CPP_EOF)
6383 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6385 /* If the next token is `extern' and the following token is a string
6386 literal, then we have a linkage specification. */
6387 if (token1.keyword == RID_EXTERN
6388 && cp_parser_is_string_literal (&token2))
6389 cp_parser_linkage_specification (parser);
6390 /* If the next token is `template', then we have either a template
6391 declaration, an explicit instantiation, or an explicit
6392 specialization. */
6393 else if (token1.keyword == RID_TEMPLATE)
6395 /* `template <>' indicates a template specialization. */
6396 if (token2.type == CPP_LESS
6397 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6398 cp_parser_explicit_specialization (parser);
6399 /* `template <' indicates a template declaration. */
6400 else if (token2.type == CPP_LESS)
6401 cp_parser_template_declaration (parser, /*member_p=*/false);
6402 /* Anything else must be an explicit instantiation. */
6403 else
6404 cp_parser_explicit_instantiation (parser);
6406 /* If the next token is `export', then we have a template
6407 declaration. */
6408 else if (token1.keyword == RID_EXPORT)
6409 cp_parser_template_declaration (parser, /*member_p=*/false);
6410 /* If the next token is `extern', 'static' or 'inline' and the one
6411 after that is `template', we have a GNU extended explicit
6412 instantiation directive. */
6413 else if (cp_parser_allow_gnu_extensions_p (parser)
6414 && (token1.keyword == RID_EXTERN
6415 || token1.keyword == RID_STATIC
6416 || token1.keyword == RID_INLINE)
6417 && token2.keyword == RID_TEMPLATE)
6418 cp_parser_explicit_instantiation (parser);
6419 /* If the next token is `namespace', check for a named or unnamed
6420 namespace definition. */
6421 else if (token1.keyword == RID_NAMESPACE
6422 && (/* A named namespace definition. */
6423 (token2.type == CPP_NAME
6424 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6425 == CPP_OPEN_BRACE))
6426 /* An unnamed namespace definition. */
6427 || token2.type == CPP_OPEN_BRACE))
6428 cp_parser_namespace_definition (parser);
6429 /* We must have either a block declaration or a function
6430 definition. */
6431 else
6432 /* Try to parse a block-declaration, or a function-definition. */
6433 cp_parser_block_declaration (parser, /*statement_p=*/false);
6436 /* Parse a block-declaration.
6438 block-declaration:
6439 simple-declaration
6440 asm-definition
6441 namespace-alias-definition
6442 using-declaration
6443 using-directive
6445 GNU Extension:
6447 block-declaration:
6448 __extension__ block-declaration
6449 label-declaration
6451 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6452 part of a declaration-statement. */
6454 static void
6455 cp_parser_block_declaration (cp_parser *parser,
6456 bool statement_p)
6458 cp_token *token1;
6459 int saved_pedantic;
6461 /* Check for the `__extension__' keyword. */
6462 if (cp_parser_extension_opt (parser, &saved_pedantic))
6464 /* Parse the qualified declaration. */
6465 cp_parser_block_declaration (parser, statement_p);
6466 /* Restore the PEDANTIC flag. */
6467 pedantic = saved_pedantic;
6469 return;
6472 /* Peek at the next token to figure out which kind of declaration is
6473 present. */
6474 token1 = cp_lexer_peek_token (parser->lexer);
6476 /* If the next keyword is `asm', we have an asm-definition. */
6477 if (token1->keyword == RID_ASM)
6479 if (statement_p)
6480 cp_parser_commit_to_tentative_parse (parser);
6481 cp_parser_asm_definition (parser);
6483 /* If the next keyword is `namespace', we have a
6484 namespace-alias-definition. */
6485 else if (token1->keyword == RID_NAMESPACE)
6486 cp_parser_namespace_alias_definition (parser);
6487 /* If the next keyword is `using', we have either a
6488 using-declaration or a using-directive. */
6489 else if (token1->keyword == RID_USING)
6491 cp_token *token2;
6493 if (statement_p)
6494 cp_parser_commit_to_tentative_parse (parser);
6495 /* If the token after `using' is `namespace', then we have a
6496 using-directive. */
6497 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6498 if (token2->keyword == RID_NAMESPACE)
6499 cp_parser_using_directive (parser);
6500 /* Otherwise, it's a using-declaration. */
6501 else
6502 cp_parser_using_declaration (parser);
6504 /* If the next keyword is `__label__' we have a label declaration. */
6505 else if (token1->keyword == RID_LABEL)
6507 if (statement_p)
6508 cp_parser_commit_to_tentative_parse (parser);
6509 cp_parser_label_declaration (parser);
6511 /* Anything else must be a simple-declaration. */
6512 else
6513 cp_parser_simple_declaration (parser, !statement_p);
6516 /* Parse a simple-declaration.
6518 simple-declaration:
6519 decl-specifier-seq [opt] init-declarator-list [opt] ;
6521 init-declarator-list:
6522 init-declarator
6523 init-declarator-list , init-declarator
6525 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6526 function-definition as a simple-declaration. */
6528 static void
6529 cp_parser_simple_declaration (cp_parser* parser,
6530 bool function_definition_allowed_p)
6532 tree decl_specifiers;
6533 tree attributes;
6534 bool declares_class_or_enum;
6535 bool saw_declarator;
6537 /* Defer access checks until we know what is being declared; the
6538 checks for names appearing in the decl-specifier-seq should be
6539 done as if we were in the scope of the thing being declared. */
6540 push_deferring_access_checks (dk_deferred);
6542 /* Parse the decl-specifier-seq. We have to keep track of whether
6543 or not the decl-specifier-seq declares a named class or
6544 enumeration type, since that is the only case in which the
6545 init-declarator-list is allowed to be empty.
6547 [dcl.dcl]
6549 In a simple-declaration, the optional init-declarator-list can be
6550 omitted only when declaring a class or enumeration, that is when
6551 the decl-specifier-seq contains either a class-specifier, an
6552 elaborated-type-specifier, or an enum-specifier. */
6553 decl_specifiers
6554 = cp_parser_decl_specifier_seq (parser,
6555 CP_PARSER_FLAGS_OPTIONAL,
6556 &attributes,
6557 &declares_class_or_enum);
6558 /* We no longer need to defer access checks. */
6559 stop_deferring_access_checks ();
6561 /* If the next two tokens are both identifiers, the code is
6562 erroneous. The usual cause of this situation is code like:
6564 T t;
6566 where "T" should name a type -- but does not. */
6567 if (cp_parser_diagnose_invalid_type_name (parser))
6569 /* If parsing tentatively, we should commit; we really are
6570 looking at a declaration. */
6571 cp_parser_commit_to_tentative_parse (parser);
6572 /* Give up. */
6573 return;
6576 /* Keep going until we hit the `;' at the end of the simple
6577 declaration. */
6578 saw_declarator = false;
6579 while (cp_lexer_next_token_is_not (parser->lexer,
6580 CPP_SEMICOLON))
6582 cp_token *token;
6583 bool function_definition_p;
6585 saw_declarator = true;
6586 /* Parse the init-declarator. */
6587 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6588 function_definition_allowed_p,
6589 /*member_p=*/false,
6590 &function_definition_p);
6591 /* If an error occurred while parsing tentatively, exit quickly.
6592 (That usually happens when in the body of a function; each
6593 statement is treated as a declaration-statement until proven
6594 otherwise.) */
6595 if (cp_parser_error_occurred (parser))
6597 pop_deferring_access_checks ();
6598 return;
6600 /* Handle function definitions specially. */
6601 if (function_definition_p)
6603 /* If the next token is a `,', then we are probably
6604 processing something like:
6606 void f() {}, *p;
6608 which is erroneous. */
6609 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6610 error ("mixing declarations and function-definitions is forbidden");
6611 /* Otherwise, we're done with the list of declarators. */
6612 else
6614 pop_deferring_access_checks ();
6615 return;
6618 /* The next token should be either a `,' or a `;'. */
6619 token = cp_lexer_peek_token (parser->lexer);
6620 /* If it's a `,', there are more declarators to come. */
6621 if (token->type == CPP_COMMA)
6622 cp_lexer_consume_token (parser->lexer);
6623 /* If it's a `;', we are done. */
6624 else if (token->type == CPP_SEMICOLON)
6625 break;
6626 /* Anything else is an error. */
6627 else
6629 cp_parser_error (parser, "expected `,' or `;'");
6630 /* Skip tokens until we reach the end of the statement. */
6631 cp_parser_skip_to_end_of_statement (parser);
6632 pop_deferring_access_checks ();
6633 return;
6635 /* After the first time around, a function-definition is not
6636 allowed -- even if it was OK at first. For example:
6638 int i, f() {}
6640 is not valid. */
6641 function_definition_allowed_p = false;
6644 /* Issue an error message if no declarators are present, and the
6645 decl-specifier-seq does not itself declare a class or
6646 enumeration. */
6647 if (!saw_declarator)
6649 if (cp_parser_declares_only_class_p (parser))
6650 shadow_tag (decl_specifiers);
6651 /* Perform any deferred access checks. */
6652 perform_deferred_access_checks ();
6655 pop_deferring_access_checks ();
6657 /* Consume the `;'. */
6658 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6660 /* Mark all the classes that appeared in the decl-specifier-seq as
6661 having received a `;'. */
6662 note_list_got_semicolon (decl_specifiers);
6665 /* Parse a decl-specifier-seq.
6667 decl-specifier-seq:
6668 decl-specifier-seq [opt] decl-specifier
6670 decl-specifier:
6671 storage-class-specifier
6672 type-specifier
6673 function-specifier
6674 friend
6675 typedef
6677 GNU Extension:
6679 decl-specifier-seq:
6680 decl-specifier-seq [opt] attributes
6682 Returns a TREE_LIST, giving the decl-specifiers in the order they
6683 appear in the source code. The TREE_VALUE of each node is the
6684 decl-specifier. For a keyword (such as `auto' or `friend'), the
6685 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6686 representation of a type-specifier, see cp_parser_type_specifier.
6688 If there are attributes, they will be stored in *ATTRIBUTES,
6689 represented as described above cp_parser_attributes.
6691 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6692 appears, and the entity that will be a friend is not going to be a
6693 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6694 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6695 friendship is granted might not be a class. */
6697 static tree
6698 cp_parser_decl_specifier_seq (cp_parser* parser,
6699 cp_parser_flags flags,
6700 tree* attributes,
6701 bool* declares_class_or_enum)
6703 tree decl_specs = NULL_TREE;
6704 bool friend_p = false;
6705 bool constructor_possible_p = !parser->in_declarator_p;
6707 /* Assume no class or enumeration type is declared. */
6708 *declares_class_or_enum = false;
6710 /* Assume there are no attributes. */
6711 *attributes = NULL_TREE;
6713 /* Keep reading specifiers until there are no more to read. */
6714 while (true)
6716 tree decl_spec = NULL_TREE;
6717 bool constructor_p;
6718 cp_token *token;
6720 /* Peek at the next token. */
6721 token = cp_lexer_peek_token (parser->lexer);
6722 /* Handle attributes. */
6723 if (token->keyword == RID_ATTRIBUTE)
6725 /* Parse the attributes. */
6726 decl_spec = cp_parser_attributes_opt (parser);
6727 /* Add them to the list. */
6728 *attributes = chainon (*attributes, decl_spec);
6729 continue;
6731 /* If the next token is an appropriate keyword, we can simply
6732 add it to the list. */
6733 switch (token->keyword)
6735 case RID_FRIEND:
6736 /* decl-specifier:
6737 friend */
6738 friend_p = true;
6739 /* The representation of the specifier is simply the
6740 appropriate TREE_IDENTIFIER node. */
6741 decl_spec = token->value;
6742 /* Consume the token. */
6743 cp_lexer_consume_token (parser->lexer);
6744 break;
6746 /* function-specifier:
6747 inline
6748 virtual
6749 explicit */
6750 case RID_INLINE:
6751 case RID_VIRTUAL:
6752 case RID_EXPLICIT:
6753 decl_spec = cp_parser_function_specifier_opt (parser);
6754 break;
6756 /* decl-specifier:
6757 typedef */
6758 case RID_TYPEDEF:
6759 /* The representation of the specifier is simply the
6760 appropriate TREE_IDENTIFIER node. */
6761 decl_spec = token->value;
6762 /* Consume the token. */
6763 cp_lexer_consume_token (parser->lexer);
6764 /* A constructor declarator cannot appear in a typedef. */
6765 constructor_possible_p = false;
6766 /* The "typedef" keyword can only occur in a declaration; we
6767 may as well commit at this point. */
6768 cp_parser_commit_to_tentative_parse (parser);
6769 break;
6771 /* storage-class-specifier:
6772 auto
6773 register
6774 static
6775 extern
6776 mutable
6778 GNU Extension:
6779 thread */
6780 case RID_AUTO:
6781 case RID_REGISTER:
6782 case RID_STATIC:
6783 case RID_EXTERN:
6784 case RID_MUTABLE:
6785 case RID_THREAD:
6786 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6787 break;
6789 default:
6790 break;
6793 /* Constructors are a special case. The `S' in `S()' is not a
6794 decl-specifier; it is the beginning of the declarator. */
6795 constructor_p = (!decl_spec
6796 && constructor_possible_p
6797 && cp_parser_constructor_declarator_p (parser,
6798 friend_p));
6800 /* If we don't have a DECL_SPEC yet, then we must be looking at
6801 a type-specifier. */
6802 if (!decl_spec && !constructor_p)
6804 bool decl_spec_declares_class_or_enum;
6805 bool is_cv_qualifier;
6807 decl_spec
6808 = cp_parser_type_specifier (parser, flags,
6809 friend_p,
6810 /*is_declaration=*/true,
6811 &decl_spec_declares_class_or_enum,
6812 &is_cv_qualifier);
6814 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6816 /* If this type-specifier referenced a user-defined type
6817 (a typedef, class-name, etc.), then we can't allow any
6818 more such type-specifiers henceforth.
6820 [dcl.spec]
6822 The longest sequence of decl-specifiers that could
6823 possibly be a type name is taken as the
6824 decl-specifier-seq of a declaration. The sequence shall
6825 be self-consistent as described below.
6827 [dcl.type]
6829 As a general rule, at most one type-specifier is allowed
6830 in the complete decl-specifier-seq of a declaration. The
6831 only exceptions are the following:
6833 -- const or volatile can be combined with any other
6834 type-specifier.
6836 -- signed or unsigned can be combined with char, long,
6837 short, or int.
6839 -- ..
6841 Example:
6843 typedef char* Pc;
6844 void g (const int Pc);
6846 Here, Pc is *not* part of the decl-specifier seq; it's
6847 the declarator. Therefore, once we see a type-specifier
6848 (other than a cv-qualifier), we forbid any additional
6849 user-defined types. We *do* still allow things like `int
6850 int' to be considered a decl-specifier-seq, and issue the
6851 error message later. */
6852 if (decl_spec && !is_cv_qualifier)
6853 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6854 /* A constructor declarator cannot follow a type-specifier. */
6855 if (decl_spec)
6856 constructor_possible_p = false;
6859 /* If we still do not have a DECL_SPEC, then there are no more
6860 decl-specifiers. */
6861 if (!decl_spec)
6863 /* Issue an error message, unless the entire construct was
6864 optional. */
6865 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6867 cp_parser_error (parser, "expected decl specifier");
6868 return error_mark_node;
6871 break;
6874 /* Add the DECL_SPEC to the list of specifiers. */
6875 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6877 /* After we see one decl-specifier, further decl-specifiers are
6878 always optional. */
6879 flags |= CP_PARSER_FLAGS_OPTIONAL;
6882 /* We have built up the DECL_SPECS in reverse order. Return them in
6883 the correct order. */
6884 return nreverse (decl_specs);
6887 /* Parse an (optional) storage-class-specifier.
6889 storage-class-specifier:
6890 auto
6891 register
6892 static
6893 extern
6894 mutable
6896 GNU Extension:
6898 storage-class-specifier:
6899 thread
6901 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6903 static tree
6904 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6906 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6908 case RID_AUTO:
6909 case RID_REGISTER:
6910 case RID_STATIC:
6911 case RID_EXTERN:
6912 case RID_MUTABLE:
6913 case RID_THREAD:
6914 /* Consume the token. */
6915 return cp_lexer_consume_token (parser->lexer)->value;
6917 default:
6918 return NULL_TREE;
6922 /* Parse an (optional) function-specifier.
6924 function-specifier:
6925 inline
6926 virtual
6927 explicit
6929 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6931 static tree
6932 cp_parser_function_specifier_opt (cp_parser* parser)
6934 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6936 case RID_INLINE:
6937 case RID_VIRTUAL:
6938 case RID_EXPLICIT:
6939 /* Consume the token. */
6940 return cp_lexer_consume_token (parser->lexer)->value;
6942 default:
6943 return NULL_TREE;
6947 /* Parse a linkage-specification.
6949 linkage-specification:
6950 extern string-literal { declaration-seq [opt] }
6951 extern string-literal declaration */
6953 static void
6954 cp_parser_linkage_specification (cp_parser* parser)
6956 cp_token *token;
6957 tree linkage;
6959 /* Look for the `extern' keyword. */
6960 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6962 /* Peek at the next token. */
6963 token = cp_lexer_peek_token (parser->lexer);
6964 /* If it's not a string-literal, then there's a problem. */
6965 if (!cp_parser_is_string_literal (token))
6967 cp_parser_error (parser, "expected language-name");
6968 return;
6970 /* Consume the token. */
6971 cp_lexer_consume_token (parser->lexer);
6973 /* Transform the literal into an identifier. If the literal is a
6974 wide-character string, or contains embedded NULs, then we can't
6975 handle it as the user wants. */
6976 if (token->type == CPP_WSTRING
6977 || (strlen (TREE_STRING_POINTER (token->value))
6978 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6980 cp_parser_error (parser, "invalid linkage-specification");
6981 /* Assume C++ linkage. */
6982 linkage = get_identifier ("c++");
6984 /* If it's a simple string constant, things are easier. */
6985 else
6986 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6988 /* We're now using the new linkage. */
6989 push_lang_context (linkage);
6991 /* If the next token is a `{', then we're using the first
6992 production. */
6993 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6995 /* Consume the `{' token. */
6996 cp_lexer_consume_token (parser->lexer);
6997 /* Parse the declarations. */
6998 cp_parser_declaration_seq_opt (parser);
6999 /* Look for the closing `}'. */
7000 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7002 /* Otherwise, there's just one declaration. */
7003 else
7005 bool saved_in_unbraced_linkage_specification_p;
7007 saved_in_unbraced_linkage_specification_p
7008 = parser->in_unbraced_linkage_specification_p;
7009 parser->in_unbraced_linkage_specification_p = true;
7010 have_extern_spec = true;
7011 cp_parser_declaration (parser);
7012 have_extern_spec = false;
7013 parser->in_unbraced_linkage_specification_p
7014 = saved_in_unbraced_linkage_specification_p;
7017 /* We're done with the linkage-specification. */
7018 pop_lang_context ();
7021 /* Special member functions [gram.special] */
7023 /* Parse a conversion-function-id.
7025 conversion-function-id:
7026 operator conversion-type-id
7028 Returns an IDENTIFIER_NODE representing the operator. */
7030 static tree
7031 cp_parser_conversion_function_id (cp_parser* parser)
7033 tree type;
7034 tree saved_scope;
7035 tree saved_qualifying_scope;
7036 tree saved_object_scope;
7038 /* Look for the `operator' token. */
7039 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7040 return error_mark_node;
7041 /* When we parse the conversion-type-id, the current scope will be
7042 reset. However, we need that information in able to look up the
7043 conversion function later, so we save it here. */
7044 saved_scope = parser->scope;
7045 saved_qualifying_scope = parser->qualifying_scope;
7046 saved_object_scope = parser->object_scope;
7047 /* We must enter the scope of the class so that the names of
7048 entities declared within the class are available in the
7049 conversion-type-id. For example, consider:
7051 struct S {
7052 typedef int I;
7053 operator I();
7056 S::operator I() { ... }
7058 In order to see that `I' is a type-name in the definition, we
7059 must be in the scope of `S'. */
7060 if (saved_scope)
7061 push_scope (saved_scope);
7062 /* Parse the conversion-type-id. */
7063 type = cp_parser_conversion_type_id (parser);
7064 /* Leave the scope of the class, if any. */
7065 if (saved_scope)
7066 pop_scope (saved_scope);
7067 /* Restore the saved scope. */
7068 parser->scope = saved_scope;
7069 parser->qualifying_scope = saved_qualifying_scope;
7070 parser->object_scope = saved_object_scope;
7071 /* If the TYPE is invalid, indicate failure. */
7072 if (type == error_mark_node)
7073 return error_mark_node;
7074 return mangle_conv_op_name_for_type (type);
7077 /* Parse a conversion-type-id:
7079 conversion-type-id:
7080 type-specifier-seq conversion-declarator [opt]
7082 Returns the TYPE specified. */
7084 static tree
7085 cp_parser_conversion_type_id (cp_parser* parser)
7087 tree attributes;
7088 tree type_specifiers;
7089 tree declarator;
7091 /* Parse the attributes. */
7092 attributes = cp_parser_attributes_opt (parser);
7093 /* Parse the type-specifiers. */
7094 type_specifiers = cp_parser_type_specifier_seq (parser);
7095 /* If that didn't work, stop. */
7096 if (type_specifiers == error_mark_node)
7097 return error_mark_node;
7098 /* Parse the conversion-declarator. */
7099 declarator = cp_parser_conversion_declarator_opt (parser);
7101 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7102 /*initialized=*/0, &attributes);
7105 /* Parse an (optional) conversion-declarator.
7107 conversion-declarator:
7108 ptr-operator conversion-declarator [opt]
7110 Returns a representation of the declarator. See
7111 cp_parser_declarator for details. */
7113 static tree
7114 cp_parser_conversion_declarator_opt (cp_parser* parser)
7116 enum tree_code code;
7117 tree class_type;
7118 tree cv_qualifier_seq;
7120 /* We don't know if there's a ptr-operator next, or not. */
7121 cp_parser_parse_tentatively (parser);
7122 /* Try the ptr-operator. */
7123 code = cp_parser_ptr_operator (parser, &class_type,
7124 &cv_qualifier_seq);
7125 /* If it worked, look for more conversion-declarators. */
7126 if (cp_parser_parse_definitely (parser))
7128 tree declarator;
7130 /* Parse another optional declarator. */
7131 declarator = cp_parser_conversion_declarator_opt (parser);
7133 /* Create the representation of the declarator. */
7134 if (code == INDIRECT_REF)
7135 declarator = make_pointer_declarator (cv_qualifier_seq,
7136 declarator);
7137 else
7138 declarator = make_reference_declarator (cv_qualifier_seq,
7139 declarator);
7141 /* Handle the pointer-to-member case. */
7142 if (class_type)
7143 declarator = build_nt (SCOPE_REF, class_type, declarator);
7145 return declarator;
7148 return NULL_TREE;
7151 /* Parse an (optional) ctor-initializer.
7153 ctor-initializer:
7154 : mem-initializer-list
7156 Returns TRUE iff the ctor-initializer was actually present. */
7158 static bool
7159 cp_parser_ctor_initializer_opt (cp_parser* parser)
7161 /* If the next token is not a `:', then there is no
7162 ctor-initializer. */
7163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7165 /* Do default initialization of any bases and members. */
7166 if (DECL_CONSTRUCTOR_P (current_function_decl))
7167 finish_mem_initializers (NULL_TREE);
7169 return false;
7172 /* Consume the `:' token. */
7173 cp_lexer_consume_token (parser->lexer);
7174 /* And the mem-initializer-list. */
7175 cp_parser_mem_initializer_list (parser);
7177 return true;
7180 /* Parse a mem-initializer-list.
7182 mem-initializer-list:
7183 mem-initializer
7184 mem-initializer , mem-initializer-list */
7186 static void
7187 cp_parser_mem_initializer_list (cp_parser* parser)
7189 tree mem_initializer_list = NULL_TREE;
7191 /* Let the semantic analysis code know that we are starting the
7192 mem-initializer-list. */
7193 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7194 error ("only constructors take base initializers");
7196 /* Loop through the list. */
7197 while (true)
7199 tree mem_initializer;
7201 /* Parse the mem-initializer. */
7202 mem_initializer = cp_parser_mem_initializer (parser);
7203 /* Add it to the list, unless it was erroneous. */
7204 if (mem_initializer)
7206 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7207 mem_initializer_list = mem_initializer;
7209 /* If the next token is not a `,', we're done. */
7210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7211 break;
7212 /* Consume the `,' token. */
7213 cp_lexer_consume_token (parser->lexer);
7216 /* Perform semantic analysis. */
7217 if (DECL_CONSTRUCTOR_P (current_function_decl))
7218 finish_mem_initializers (mem_initializer_list);
7221 /* Parse a mem-initializer.
7223 mem-initializer:
7224 mem-initializer-id ( expression-list [opt] )
7226 GNU extension:
7228 mem-initializer:
7229 ( expresion-list [opt] )
7231 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7232 class) or FIELD_DECL (for a non-static data member) to initialize;
7233 the TREE_VALUE is the expression-list. */
7235 static tree
7236 cp_parser_mem_initializer (cp_parser* parser)
7238 tree mem_initializer_id;
7239 tree expression_list;
7240 tree member;
7242 /* Find out what is being initialized. */
7243 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7245 pedwarn ("anachronistic old-style base class initializer");
7246 mem_initializer_id = NULL_TREE;
7248 else
7249 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7250 member = expand_member_init (mem_initializer_id);
7251 if (member && !DECL_P (member))
7252 in_base_initializer = 1;
7254 /* Look for the opening `('. */
7255 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7256 /* Parse the expression-list. */
7257 if (cp_lexer_next_token_is_not (parser->lexer,
7258 CPP_CLOSE_PAREN))
7259 expression_list = cp_parser_expression_list (parser);
7260 else
7261 expression_list = void_type_node;
7262 /* Look for the closing `)'. */
7263 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7265 in_base_initializer = 0;
7267 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7270 /* Parse a mem-initializer-id.
7272 mem-initializer-id:
7273 :: [opt] nested-name-specifier [opt] class-name
7274 identifier
7276 Returns a TYPE indicating the class to be initializer for the first
7277 production. Returns an IDENTIFIER_NODE indicating the data member
7278 to be initialized for the second production. */
7280 static tree
7281 cp_parser_mem_initializer_id (cp_parser* parser)
7283 bool global_scope_p;
7284 bool nested_name_specifier_p;
7285 tree id;
7287 /* Look for the optional `::' operator. */
7288 global_scope_p
7289 = (cp_parser_global_scope_opt (parser,
7290 /*current_scope_valid_p=*/false)
7291 != NULL_TREE);
7292 /* Look for the optional nested-name-specifier. The simplest way to
7293 implement:
7295 [temp.res]
7297 The keyword `typename' is not permitted in a base-specifier or
7298 mem-initializer; in these contexts a qualified name that
7299 depends on a template-parameter is implicitly assumed to be a
7300 type name.
7302 is to assume that we have seen the `typename' keyword at this
7303 point. */
7304 nested_name_specifier_p
7305 = (cp_parser_nested_name_specifier_opt (parser,
7306 /*typename_keyword_p=*/true,
7307 /*check_dependency_p=*/true,
7308 /*type_p=*/true)
7309 != NULL_TREE);
7310 /* If there is a `::' operator or a nested-name-specifier, then we
7311 are definitely looking for a class-name. */
7312 if (global_scope_p || nested_name_specifier_p)
7313 return cp_parser_class_name (parser,
7314 /*typename_keyword_p=*/true,
7315 /*template_keyword_p=*/false,
7316 /*type_p=*/false,
7317 /*check_dependency_p=*/true,
7318 /*class_head_p=*/false);
7319 /* Otherwise, we could also be looking for an ordinary identifier. */
7320 cp_parser_parse_tentatively (parser);
7321 /* Try a class-name. */
7322 id = cp_parser_class_name (parser,
7323 /*typename_keyword_p=*/true,
7324 /*template_keyword_p=*/false,
7325 /*type_p=*/false,
7326 /*check_dependency_p=*/true,
7327 /*class_head_p=*/false);
7328 /* If we found one, we're done. */
7329 if (cp_parser_parse_definitely (parser))
7330 return id;
7331 /* Otherwise, look for an ordinary identifier. */
7332 return cp_parser_identifier (parser);
7335 /* Overloading [gram.over] */
7337 /* Parse an operator-function-id.
7339 operator-function-id:
7340 operator operator
7342 Returns an IDENTIFIER_NODE for the operator which is a
7343 human-readable spelling of the identifier, e.g., `operator +'. */
7345 static tree
7346 cp_parser_operator_function_id (cp_parser* parser)
7348 /* Look for the `operator' keyword. */
7349 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7350 return error_mark_node;
7351 /* And then the name of the operator itself. */
7352 return cp_parser_operator (parser);
7355 /* Parse an operator.
7357 operator:
7358 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7359 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7360 || ++ -- , ->* -> () []
7362 GNU Extensions:
7364 operator:
7365 <? >? <?= >?=
7367 Returns an IDENTIFIER_NODE for the operator which is a
7368 human-readable spelling of the identifier, e.g., `operator +'. */
7370 static tree
7371 cp_parser_operator (cp_parser* parser)
7373 tree id = NULL_TREE;
7374 cp_token *token;
7376 /* Peek at the next token. */
7377 token = cp_lexer_peek_token (parser->lexer);
7378 /* Figure out which operator we have. */
7379 switch (token->type)
7381 case CPP_KEYWORD:
7383 enum tree_code op;
7385 /* The keyword should be either `new' or `delete'. */
7386 if (token->keyword == RID_NEW)
7387 op = NEW_EXPR;
7388 else if (token->keyword == RID_DELETE)
7389 op = DELETE_EXPR;
7390 else
7391 break;
7393 /* Consume the `new' or `delete' token. */
7394 cp_lexer_consume_token (parser->lexer);
7396 /* Peek at the next token. */
7397 token = cp_lexer_peek_token (parser->lexer);
7398 /* If it's a `[' token then this is the array variant of the
7399 operator. */
7400 if (token->type == CPP_OPEN_SQUARE)
7402 /* Consume the `[' token. */
7403 cp_lexer_consume_token (parser->lexer);
7404 /* Look for the `]' token. */
7405 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7406 id = ansi_opname (op == NEW_EXPR
7407 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7409 /* Otherwise, we have the non-array variant. */
7410 else
7411 id = ansi_opname (op);
7413 return id;
7416 case CPP_PLUS:
7417 id = ansi_opname (PLUS_EXPR);
7418 break;
7420 case CPP_MINUS:
7421 id = ansi_opname (MINUS_EXPR);
7422 break;
7424 case CPP_MULT:
7425 id = ansi_opname (MULT_EXPR);
7426 break;
7428 case CPP_DIV:
7429 id = ansi_opname (TRUNC_DIV_EXPR);
7430 break;
7432 case CPP_MOD:
7433 id = ansi_opname (TRUNC_MOD_EXPR);
7434 break;
7436 case CPP_XOR:
7437 id = ansi_opname (BIT_XOR_EXPR);
7438 break;
7440 case CPP_AND:
7441 id = ansi_opname (BIT_AND_EXPR);
7442 break;
7444 case CPP_OR:
7445 id = ansi_opname (BIT_IOR_EXPR);
7446 break;
7448 case CPP_COMPL:
7449 id = ansi_opname (BIT_NOT_EXPR);
7450 break;
7452 case CPP_NOT:
7453 id = ansi_opname (TRUTH_NOT_EXPR);
7454 break;
7456 case CPP_EQ:
7457 id = ansi_assopname (NOP_EXPR);
7458 break;
7460 case CPP_LESS:
7461 id = ansi_opname (LT_EXPR);
7462 break;
7464 case CPP_GREATER:
7465 id = ansi_opname (GT_EXPR);
7466 break;
7468 case CPP_PLUS_EQ:
7469 id = ansi_assopname (PLUS_EXPR);
7470 break;
7472 case CPP_MINUS_EQ:
7473 id = ansi_assopname (MINUS_EXPR);
7474 break;
7476 case CPP_MULT_EQ:
7477 id = ansi_assopname (MULT_EXPR);
7478 break;
7480 case CPP_DIV_EQ:
7481 id = ansi_assopname (TRUNC_DIV_EXPR);
7482 break;
7484 case CPP_MOD_EQ:
7485 id = ansi_assopname (TRUNC_MOD_EXPR);
7486 break;
7488 case CPP_XOR_EQ:
7489 id = ansi_assopname (BIT_XOR_EXPR);
7490 break;
7492 case CPP_AND_EQ:
7493 id = ansi_assopname (BIT_AND_EXPR);
7494 break;
7496 case CPP_OR_EQ:
7497 id = ansi_assopname (BIT_IOR_EXPR);
7498 break;
7500 case CPP_LSHIFT:
7501 id = ansi_opname (LSHIFT_EXPR);
7502 break;
7504 case CPP_RSHIFT:
7505 id = ansi_opname (RSHIFT_EXPR);
7506 break;
7508 case CPP_LSHIFT_EQ:
7509 id = ansi_assopname (LSHIFT_EXPR);
7510 break;
7512 case CPP_RSHIFT_EQ:
7513 id = ansi_assopname (RSHIFT_EXPR);
7514 break;
7516 case CPP_EQ_EQ:
7517 id = ansi_opname (EQ_EXPR);
7518 break;
7520 case CPP_NOT_EQ:
7521 id = ansi_opname (NE_EXPR);
7522 break;
7524 case CPP_LESS_EQ:
7525 id = ansi_opname (LE_EXPR);
7526 break;
7528 case CPP_GREATER_EQ:
7529 id = ansi_opname (GE_EXPR);
7530 break;
7532 case CPP_AND_AND:
7533 id = ansi_opname (TRUTH_ANDIF_EXPR);
7534 break;
7536 case CPP_OR_OR:
7537 id = ansi_opname (TRUTH_ORIF_EXPR);
7538 break;
7540 case CPP_PLUS_PLUS:
7541 id = ansi_opname (POSTINCREMENT_EXPR);
7542 break;
7544 case CPP_MINUS_MINUS:
7545 id = ansi_opname (PREDECREMENT_EXPR);
7546 break;
7548 case CPP_COMMA:
7549 id = ansi_opname (COMPOUND_EXPR);
7550 break;
7552 case CPP_DEREF_STAR:
7553 id = ansi_opname (MEMBER_REF);
7554 break;
7556 case CPP_DEREF:
7557 id = ansi_opname (COMPONENT_REF);
7558 break;
7560 case CPP_OPEN_PAREN:
7561 /* Consume the `('. */
7562 cp_lexer_consume_token (parser->lexer);
7563 /* Look for the matching `)'. */
7564 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7565 return ansi_opname (CALL_EXPR);
7567 case CPP_OPEN_SQUARE:
7568 /* Consume the `['. */
7569 cp_lexer_consume_token (parser->lexer);
7570 /* Look for the matching `]'. */
7571 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7572 return ansi_opname (ARRAY_REF);
7574 /* Extensions. */
7575 case CPP_MIN:
7576 id = ansi_opname (MIN_EXPR);
7577 break;
7579 case CPP_MAX:
7580 id = ansi_opname (MAX_EXPR);
7581 break;
7583 case CPP_MIN_EQ:
7584 id = ansi_assopname (MIN_EXPR);
7585 break;
7587 case CPP_MAX_EQ:
7588 id = ansi_assopname (MAX_EXPR);
7589 break;
7591 default:
7592 /* Anything else is an error. */
7593 break;
7596 /* If we have selected an identifier, we need to consume the
7597 operator token. */
7598 if (id)
7599 cp_lexer_consume_token (parser->lexer);
7600 /* Otherwise, no valid operator name was present. */
7601 else
7603 cp_parser_error (parser, "expected operator");
7604 id = error_mark_node;
7607 return id;
7610 /* Parse a template-declaration.
7612 template-declaration:
7613 export [opt] template < template-parameter-list > declaration
7615 If MEMBER_P is TRUE, this template-declaration occurs within a
7616 class-specifier.
7618 The grammar rule given by the standard isn't correct. What
7619 is really meant is:
7621 template-declaration:
7622 export [opt] template-parameter-list-seq
7623 decl-specifier-seq [opt] init-declarator [opt] ;
7624 export [opt] template-parameter-list-seq
7625 function-definition
7627 template-parameter-list-seq:
7628 template-parameter-list-seq [opt]
7629 template < template-parameter-list > */
7631 static void
7632 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7634 /* Check for `export'. */
7635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7637 /* Consume the `export' token. */
7638 cp_lexer_consume_token (parser->lexer);
7639 /* Warn that we do not support `export'. */
7640 warning ("keyword `export' not implemented, and will be ignored");
7643 cp_parser_template_declaration_after_export (parser, member_p);
7646 /* Parse a template-parameter-list.
7648 template-parameter-list:
7649 template-parameter
7650 template-parameter-list , template-parameter
7652 Returns a TREE_LIST. Each node represents a template parameter.
7653 The nodes are connected via their TREE_CHAINs. */
7655 static tree
7656 cp_parser_template_parameter_list (cp_parser* parser)
7658 tree parameter_list = NULL_TREE;
7660 while (true)
7662 tree parameter;
7663 cp_token *token;
7665 /* Parse the template-parameter. */
7666 parameter = cp_parser_template_parameter (parser);
7667 /* Add it to the list. */
7668 parameter_list = process_template_parm (parameter_list,
7669 parameter);
7671 /* Peek at the next token. */
7672 token = cp_lexer_peek_token (parser->lexer);
7673 /* If it's not a `,', we're done. */
7674 if (token->type != CPP_COMMA)
7675 break;
7676 /* Otherwise, consume the `,' token. */
7677 cp_lexer_consume_token (parser->lexer);
7680 return parameter_list;
7683 /* Parse a template-parameter.
7685 template-parameter:
7686 type-parameter
7687 parameter-declaration
7689 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7690 TREE_PURPOSE is the default value, if any. */
7692 static tree
7693 cp_parser_template_parameter (cp_parser* parser)
7695 cp_token *token;
7697 /* Peek at the next token. */
7698 token = cp_lexer_peek_token (parser->lexer);
7699 /* If it is `class' or `template', we have a type-parameter. */
7700 if (token->keyword == RID_TEMPLATE)
7701 return cp_parser_type_parameter (parser);
7702 /* If it is `class' or `typename' we do not know yet whether it is a
7703 type parameter or a non-type parameter. Consider:
7705 template <typename T, typename T::X X> ...
7709 template <class C, class D*> ...
7711 Here, the first parameter is a type parameter, and the second is
7712 a non-type parameter. We can tell by looking at the token after
7713 the identifier -- if it is a `,', `=', or `>' then we have a type
7714 parameter. */
7715 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7717 /* Peek at the token after `class' or `typename'. */
7718 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7719 /* If it's an identifier, skip it. */
7720 if (token->type == CPP_NAME)
7721 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7722 /* Now, see if the token looks like the end of a template
7723 parameter. */
7724 if (token->type == CPP_COMMA
7725 || token->type == CPP_EQ
7726 || token->type == CPP_GREATER)
7727 return cp_parser_type_parameter (parser);
7730 /* Otherwise, it is a non-type parameter.
7732 [temp.param]
7734 When parsing a default template-argument for a non-type
7735 template-parameter, the first non-nested `>' is taken as the end
7736 of the template parameter-list rather than a greater-than
7737 operator. */
7738 return
7739 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7742 /* Parse a type-parameter.
7744 type-parameter:
7745 class identifier [opt]
7746 class identifier [opt] = type-id
7747 typename identifier [opt]
7748 typename identifier [opt] = type-id
7749 template < template-parameter-list > class identifier [opt]
7750 template < template-parameter-list > class identifier [opt]
7751 = id-expression
7753 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7754 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7755 the declaration of the parameter. */
7757 static tree
7758 cp_parser_type_parameter (cp_parser* parser)
7760 cp_token *token;
7761 tree parameter;
7763 /* Look for a keyword to tell us what kind of parameter this is. */
7764 token = cp_parser_require (parser, CPP_KEYWORD,
7765 "`class', `typename', or `template'");
7766 if (!token)
7767 return error_mark_node;
7769 switch (token->keyword)
7771 case RID_CLASS:
7772 case RID_TYPENAME:
7774 tree identifier;
7775 tree default_argument;
7777 /* If the next token is an identifier, then it names the
7778 parameter. */
7779 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7780 identifier = cp_parser_identifier (parser);
7781 else
7782 identifier = NULL_TREE;
7784 /* Create the parameter. */
7785 parameter = finish_template_type_parm (class_type_node, identifier);
7787 /* If the next token is an `=', we have a default argument. */
7788 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7790 /* Consume the `=' token. */
7791 cp_lexer_consume_token (parser->lexer);
7792 /* Parse the default-argumen. */
7793 default_argument = cp_parser_type_id (parser);
7795 else
7796 default_argument = NULL_TREE;
7798 /* Create the combined representation of the parameter and the
7799 default argument. */
7800 parameter = build_tree_list (default_argument,
7801 parameter);
7803 break;
7805 case RID_TEMPLATE:
7807 tree parameter_list;
7808 tree identifier;
7809 tree default_argument;
7811 /* Look for the `<'. */
7812 cp_parser_require (parser, CPP_LESS, "`<'");
7813 /* Parse the template-parameter-list. */
7814 begin_template_parm_list ();
7815 parameter_list
7816 = cp_parser_template_parameter_list (parser);
7817 parameter_list = end_template_parm_list (parameter_list);
7818 /* Look for the `>'. */
7819 cp_parser_require (parser, CPP_GREATER, "`>'");
7820 /* Look for the `class' keyword. */
7821 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7822 /* If the next token is an `=', then there is a
7823 default-argument. If the next token is a `>', we are at
7824 the end of the parameter-list. If the next token is a `,',
7825 then we are at the end of this parameter. */
7826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7827 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7828 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7829 identifier = cp_parser_identifier (parser);
7830 else
7831 identifier = NULL_TREE;
7832 /* Create the template parameter. */
7833 parameter = finish_template_template_parm (class_type_node,
7834 identifier);
7836 /* If the next token is an `=', then there is a
7837 default-argument. */
7838 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7840 /* Consume the `='. */
7841 cp_lexer_consume_token (parser->lexer);
7842 /* Parse the id-expression. */
7843 default_argument
7844 = cp_parser_id_expression (parser,
7845 /*template_keyword_p=*/false,
7846 /*check_dependency_p=*/true,
7847 /*template_p=*/NULL);
7848 /* Look up the name. */
7849 default_argument
7850 = cp_parser_lookup_name_simple (parser, default_argument);
7851 /* See if the default argument is valid. */
7852 default_argument
7853 = check_template_template_default_arg (default_argument);
7855 else
7856 default_argument = NULL_TREE;
7858 /* Create the combined representation of the parameter and the
7859 default argument. */
7860 parameter = build_tree_list (default_argument,
7861 parameter);
7863 break;
7865 default:
7866 /* Anything else is an error. */
7867 cp_parser_error (parser,
7868 "expected `class', `typename', or `template'");
7869 parameter = error_mark_node;
7872 return parameter;
7875 /* Parse a template-id.
7877 template-id:
7878 template-name < template-argument-list [opt] >
7880 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7881 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7882 returned. Otherwise, if the template-name names a function, or set
7883 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7884 names a class, returns a TYPE_DECL for the specialization.
7886 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7887 uninstantiated templates. */
7889 static tree
7890 cp_parser_template_id (cp_parser *parser,
7891 bool template_keyword_p,
7892 bool check_dependency_p)
7894 tree template;
7895 tree arguments;
7896 tree saved_scope;
7897 tree saved_qualifying_scope;
7898 tree saved_object_scope;
7899 tree template_id;
7900 bool saved_greater_than_is_operator_p;
7901 ptrdiff_t start_of_id;
7902 tree access_check = NULL_TREE;
7903 cp_token *next_token;
7905 /* If the next token corresponds to a template-id, there is no need
7906 to reparse it. */
7907 next_token = cp_lexer_peek_token (parser->lexer);
7908 if (next_token->type == CPP_TEMPLATE_ID)
7910 tree value;
7911 tree check;
7913 /* Get the stored value. */
7914 value = cp_lexer_consume_token (parser->lexer)->value;
7915 /* Perform any access checks that were deferred. */
7916 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7917 perform_or_defer_access_check (TREE_PURPOSE (check),
7918 TREE_VALUE (check));
7919 /* Return the stored value. */
7920 return TREE_VALUE (value);
7923 /* Avoid performing name lookup if there is no possibility of
7924 finding a template-id. */
7925 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7926 || (next_token->type == CPP_NAME
7927 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7929 cp_parser_error (parser, "expected template-id");
7930 return error_mark_node;
7933 /* Remember where the template-id starts. */
7934 if (cp_parser_parsing_tentatively (parser)
7935 && !cp_parser_committed_to_tentative_parse (parser))
7937 next_token = cp_lexer_peek_token (parser->lexer);
7938 start_of_id = cp_lexer_token_difference (parser->lexer,
7939 parser->lexer->first_token,
7940 next_token);
7942 else
7943 start_of_id = -1;
7945 push_deferring_access_checks (dk_deferred);
7947 /* Parse the template-name. */
7948 template = cp_parser_template_name (parser, template_keyword_p,
7949 check_dependency_p);
7950 if (template == error_mark_node)
7952 pop_deferring_access_checks ();
7953 return error_mark_node;
7956 /* Look for the `<' that starts the template-argument-list. */
7957 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7959 pop_deferring_access_checks ();
7960 return error_mark_node;
7963 /* [temp.names]
7965 When parsing a template-id, the first non-nested `>' is taken as
7966 the end of the template-argument-list rather than a greater-than
7967 operator. */
7968 saved_greater_than_is_operator_p
7969 = parser->greater_than_is_operator_p;
7970 parser->greater_than_is_operator_p = false;
7971 /* Parsing the argument list may modify SCOPE, so we save it
7972 here. */
7973 saved_scope = parser->scope;
7974 saved_qualifying_scope = parser->qualifying_scope;
7975 saved_object_scope = parser->object_scope;
7976 /* Parse the template-argument-list itself. */
7977 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7978 arguments = NULL_TREE;
7979 else
7980 arguments = cp_parser_template_argument_list (parser);
7981 /* Look for the `>' that ends the template-argument-list. */
7982 cp_parser_require (parser, CPP_GREATER, "`>'");
7983 /* The `>' token might be a greater-than operator again now. */
7984 parser->greater_than_is_operator_p
7985 = saved_greater_than_is_operator_p;
7986 /* Restore the SAVED_SCOPE. */
7987 parser->scope = saved_scope;
7988 parser->qualifying_scope = saved_qualifying_scope;
7989 parser->object_scope = saved_object_scope;
7991 /* Build a representation of the specialization. */
7992 if (TREE_CODE (template) == IDENTIFIER_NODE)
7993 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7994 else if (DECL_CLASS_TEMPLATE_P (template)
7995 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7996 template_id
7997 = finish_template_type (template, arguments,
7998 cp_lexer_next_token_is (parser->lexer,
7999 CPP_SCOPE));
8000 else
8002 /* If it's not a class-template or a template-template, it should be
8003 a function-template. */
8004 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8005 || TREE_CODE (template) == OVERLOAD
8006 || BASELINK_P (template)),
8007 20010716);
8009 template_id = lookup_template_function (template, arguments);
8012 /* Retrieve any deferred checks. Do not pop this access checks yet
8013 so the memory will not be reclaimed during token replacing below. */
8014 access_check = get_deferred_access_checks ();
8016 /* If parsing tentatively, replace the sequence of tokens that makes
8017 up the template-id with a CPP_TEMPLATE_ID token. That way,
8018 should we re-parse the token stream, we will not have to repeat
8019 the effort required to do the parse, nor will we issue duplicate
8020 error messages about problems during instantiation of the
8021 template. */
8022 if (start_of_id >= 0)
8024 cp_token *token;
8026 /* Find the token that corresponds to the start of the
8027 template-id. */
8028 token = cp_lexer_advance_token (parser->lexer,
8029 parser->lexer->first_token,
8030 start_of_id);
8032 /* Reset the contents of the START_OF_ID token. */
8033 token->type = CPP_TEMPLATE_ID;
8034 token->value = build_tree_list (access_check, template_id);
8035 token->keyword = RID_MAX;
8036 /* Purge all subsequent tokens. */
8037 cp_lexer_purge_tokens_after (parser->lexer, token);
8040 pop_deferring_access_checks ();
8041 return template_id;
8044 /* Parse a template-name.
8046 template-name:
8047 identifier
8049 The standard should actually say:
8051 template-name:
8052 identifier
8053 operator-function-id
8054 conversion-function-id
8056 A defect report has been filed about this issue.
8058 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8059 `template' keyword, in a construction like:
8061 T::template f<3>()
8063 In that case `f' is taken to be a template-name, even though there
8064 is no way of knowing for sure.
8066 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8067 name refers to a set of overloaded functions, at least one of which
8068 is a template, or an IDENTIFIER_NODE with the name of the template,
8069 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8070 names are looked up inside uninstantiated templates. */
8072 static tree
8073 cp_parser_template_name (cp_parser* parser,
8074 bool template_keyword_p,
8075 bool check_dependency_p)
8077 tree identifier;
8078 tree decl;
8079 tree fns;
8081 /* If the next token is `operator', then we have either an
8082 operator-function-id or a conversion-function-id. */
8083 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8085 /* We don't know whether we're looking at an
8086 operator-function-id or a conversion-function-id. */
8087 cp_parser_parse_tentatively (parser);
8088 /* Try an operator-function-id. */
8089 identifier = cp_parser_operator_function_id (parser);
8090 /* If that didn't work, try a conversion-function-id. */
8091 if (!cp_parser_parse_definitely (parser))
8092 identifier = cp_parser_conversion_function_id (parser);
8094 /* Look for the identifier. */
8095 else
8096 identifier = cp_parser_identifier (parser);
8098 /* If we didn't find an identifier, we don't have a template-id. */
8099 if (identifier == error_mark_node)
8100 return error_mark_node;
8102 /* If the name immediately followed the `template' keyword, then it
8103 is a template-name. However, if the next token is not `<', then
8104 we do not treat it as a template-name, since it is not being used
8105 as part of a template-id. This enables us to handle constructs
8106 like:
8108 template <typename T> struct S { S(); };
8109 template <typename T> S<T>::S();
8111 correctly. We would treat `S' as a template -- if it were `S<T>'
8112 -- but we do not if there is no `<'. */
8113 if (template_keyword_p && processing_template_decl
8114 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8115 return identifier;
8117 /* Look up the name. */
8118 decl = cp_parser_lookup_name (parser, identifier,
8119 /*is_type=*/false,
8120 /*is_namespace=*/false,
8121 check_dependency_p);
8122 decl = maybe_get_template_decl_from_type_decl (decl);
8124 /* If DECL is a template, then the name was a template-name. */
8125 if (TREE_CODE (decl) == TEMPLATE_DECL)
8127 else
8129 /* The standard does not explicitly indicate whether a name that
8130 names a set of overloaded declarations, some of which are
8131 templates, is a template-name. However, such a name should
8132 be a template-name; otherwise, there is no way to form a
8133 template-id for the overloaded templates. */
8134 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8135 if (TREE_CODE (fns) == OVERLOAD)
8137 tree fn;
8139 for (fn = fns; fn; fn = OVL_NEXT (fn))
8140 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8141 break;
8143 else
8145 /* Otherwise, the name does not name a template. */
8146 cp_parser_error (parser, "expected template-name");
8147 return error_mark_node;
8151 /* If DECL is dependent, and refers to a function, then just return
8152 its name; we will look it up again during template instantiation. */
8153 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8155 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8156 if (TYPE_P (scope) && dependent_type_p (scope))
8157 return identifier;
8160 return decl;
8163 /* Parse a template-argument-list.
8165 template-argument-list:
8166 template-argument
8167 template-argument-list , template-argument
8169 Returns a TREE_LIST representing the arguments, in the order they
8170 appeared. The TREE_VALUE of each node is a representation of the
8171 argument. */
8173 static tree
8174 cp_parser_template_argument_list (cp_parser* parser)
8176 tree arguments = NULL_TREE;
8178 while (true)
8180 tree argument;
8182 /* Parse the template-argument. */
8183 argument = cp_parser_template_argument (parser);
8184 /* Add it to the list. */
8185 arguments = tree_cons (NULL_TREE, argument, arguments);
8186 /* If it is not a `,', then there are no more arguments. */
8187 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8188 break;
8189 /* Otherwise, consume the ','. */
8190 cp_lexer_consume_token (parser->lexer);
8193 /* We built up the arguments in reverse order. */
8194 return nreverse (arguments);
8197 /* Parse a template-argument.
8199 template-argument:
8200 assignment-expression
8201 type-id
8202 id-expression
8204 The representation is that of an assignment-expression, type-id, or
8205 id-expression -- except that the qualified id-expression is
8206 evaluated, so that the value returned is either a DECL or an
8207 OVERLOAD. */
8209 static tree
8210 cp_parser_template_argument (cp_parser* parser)
8212 tree argument;
8213 bool template_p;
8215 /* There's really no way to know what we're looking at, so we just
8216 try each alternative in order.
8218 [temp.arg]
8220 In a template-argument, an ambiguity between a type-id and an
8221 expression is resolved to a type-id, regardless of the form of
8222 the corresponding template-parameter.
8224 Therefore, we try a type-id first. */
8225 cp_parser_parse_tentatively (parser);
8226 argument = cp_parser_type_id (parser);
8227 /* If the next token isn't a `,' or a `>', then this argument wasn't
8228 really finished. */
8229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8230 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8231 cp_parser_error (parser, "expected template-argument");
8232 /* If that worked, we're done. */
8233 if (cp_parser_parse_definitely (parser))
8234 return argument;
8235 /* We're still not sure what the argument will be. */
8236 cp_parser_parse_tentatively (parser);
8237 /* Try a template. */
8238 argument = cp_parser_id_expression (parser,
8239 /*template_keyword_p=*/false,
8240 /*check_dependency_p=*/true,
8241 &template_p);
8242 /* If the next token isn't a `,' or a `>', then this argument wasn't
8243 really finished. */
8244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8245 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8246 cp_parser_error (parser, "expected template-argument");
8247 if (!cp_parser_error_occurred (parser))
8249 /* Figure out what is being referred to. */
8250 argument = cp_parser_lookup_name_simple (parser, argument);
8251 if (template_p)
8252 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8253 TREE_OPERAND (argument, 1),
8254 tf_error);
8255 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8256 cp_parser_error (parser, "expected template-name");
8258 if (cp_parser_parse_definitely (parser))
8259 return argument;
8260 /* It must be an assignment-expression. */
8261 return cp_parser_assignment_expression (parser);
8264 /* Parse an explicit-instantiation.
8266 explicit-instantiation:
8267 template declaration
8269 Although the standard says `declaration', what it really means is:
8271 explicit-instantiation:
8272 template decl-specifier-seq [opt] declarator [opt] ;
8274 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8275 supposed to be allowed. A defect report has been filed about this
8276 issue.
8278 GNU Extension:
8280 explicit-instantiation:
8281 storage-class-specifier template
8282 decl-specifier-seq [opt] declarator [opt] ;
8283 function-specifier template
8284 decl-specifier-seq [opt] declarator [opt] ; */
8286 static void
8287 cp_parser_explicit_instantiation (cp_parser* parser)
8289 bool declares_class_or_enum;
8290 tree decl_specifiers;
8291 tree attributes;
8292 tree extension_specifier = NULL_TREE;
8294 /* Look for an (optional) storage-class-specifier or
8295 function-specifier. */
8296 if (cp_parser_allow_gnu_extensions_p (parser))
8298 extension_specifier
8299 = cp_parser_storage_class_specifier_opt (parser);
8300 if (!extension_specifier)
8301 extension_specifier = cp_parser_function_specifier_opt (parser);
8304 /* Look for the `template' keyword. */
8305 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8306 /* Let the front end know that we are processing an explicit
8307 instantiation. */
8308 begin_explicit_instantiation ();
8309 /* [temp.explicit] says that we are supposed to ignore access
8310 control while processing explicit instantiation directives. */
8311 push_deferring_access_checks (dk_no_check);
8312 /* Parse a decl-specifier-seq. */
8313 decl_specifiers
8314 = cp_parser_decl_specifier_seq (parser,
8315 CP_PARSER_FLAGS_OPTIONAL,
8316 &attributes,
8317 &declares_class_or_enum);
8318 /* If there was exactly one decl-specifier, and it declared a class,
8319 and there's no declarator, then we have an explicit type
8320 instantiation. */
8321 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8323 tree type;
8325 type = check_tag_decl (decl_specifiers);
8326 /* Turn access control back on for names used during
8327 template instantiation. */
8328 pop_deferring_access_checks ();
8329 if (type)
8330 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8332 else
8334 tree declarator;
8335 tree decl;
8337 /* Parse the declarator. */
8338 declarator
8339 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8340 /*ctor_dtor_or_conv_p=*/NULL);
8341 decl = grokdeclarator (declarator, decl_specifiers,
8342 NORMAL, 0, NULL);
8343 /* Turn access control back on for names used during
8344 template instantiation. */
8345 pop_deferring_access_checks ();
8346 /* Do the explicit instantiation. */
8347 do_decl_instantiation (decl, extension_specifier);
8349 /* We're done with the instantiation. */
8350 end_explicit_instantiation ();
8352 cp_parser_consume_semicolon_at_end_of_statement (parser);
8355 /* Parse an explicit-specialization.
8357 explicit-specialization:
8358 template < > declaration
8360 Although the standard says `declaration', what it really means is:
8362 explicit-specialization:
8363 template <> decl-specifier [opt] init-declarator [opt] ;
8364 template <> function-definition
8365 template <> explicit-specialization
8366 template <> template-declaration */
8368 static void
8369 cp_parser_explicit_specialization (cp_parser* parser)
8371 /* Look for the `template' keyword. */
8372 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8373 /* Look for the `<'. */
8374 cp_parser_require (parser, CPP_LESS, "`<'");
8375 /* Look for the `>'. */
8376 cp_parser_require (parser, CPP_GREATER, "`>'");
8377 /* We have processed another parameter list. */
8378 ++parser->num_template_parameter_lists;
8379 /* Let the front end know that we are beginning a specialization. */
8380 begin_specialization ();
8382 /* If the next keyword is `template', we need to figure out whether
8383 or not we're looking a template-declaration. */
8384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8386 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8387 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8388 cp_parser_template_declaration_after_export (parser,
8389 /*member_p=*/false);
8390 else
8391 cp_parser_explicit_specialization (parser);
8393 else
8394 /* Parse the dependent declaration. */
8395 cp_parser_single_declaration (parser,
8396 /*member_p=*/false,
8397 /*friend_p=*/NULL);
8399 /* We're done with the specialization. */
8400 end_specialization ();
8401 /* We're done with this parameter list. */
8402 --parser->num_template_parameter_lists;
8405 /* Parse a type-specifier.
8407 type-specifier:
8408 simple-type-specifier
8409 class-specifier
8410 enum-specifier
8411 elaborated-type-specifier
8412 cv-qualifier
8414 GNU Extension:
8416 type-specifier:
8417 __complex__
8419 Returns a representation of the type-specifier. If the
8420 type-specifier is a keyword (like `int' or `const', or
8421 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8422 For a class-specifier, enum-specifier, or elaborated-type-specifier
8423 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8425 If IS_FRIEND is TRUE then this type-specifier is being declared a
8426 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8427 appearing in a decl-specifier-seq.
8429 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8430 class-specifier, enum-specifier, or elaborated-type-specifier, then
8431 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8432 FALSE.
8434 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8435 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8436 is set to FALSE. */
8438 static tree
8439 cp_parser_type_specifier (cp_parser* parser,
8440 cp_parser_flags flags,
8441 bool is_friend,
8442 bool is_declaration,
8443 bool* declares_class_or_enum,
8444 bool* is_cv_qualifier)
8446 tree type_spec = NULL_TREE;
8447 cp_token *token;
8448 enum rid keyword;
8450 /* Assume this type-specifier does not declare a new type. */
8451 if (declares_class_or_enum)
8452 *declares_class_or_enum = false;
8453 /* And that it does not specify a cv-qualifier. */
8454 if (is_cv_qualifier)
8455 *is_cv_qualifier = false;
8456 /* Peek at the next token. */
8457 token = cp_lexer_peek_token (parser->lexer);
8459 /* If we're looking at a keyword, we can use that to guide the
8460 production we choose. */
8461 keyword = token->keyword;
8462 switch (keyword)
8464 /* Any of these indicate either a class-specifier, or an
8465 elaborated-type-specifier. */
8466 case RID_CLASS:
8467 case RID_STRUCT:
8468 case RID_UNION:
8469 case RID_ENUM:
8470 /* Parse tentatively so that we can back up if we don't find a
8471 class-specifier or enum-specifier. */
8472 cp_parser_parse_tentatively (parser);
8473 /* Look for the class-specifier or enum-specifier. */
8474 if (keyword == RID_ENUM)
8475 type_spec = cp_parser_enum_specifier (parser);
8476 else
8477 type_spec = cp_parser_class_specifier (parser);
8479 /* If that worked, we're done. */
8480 if (cp_parser_parse_definitely (parser))
8482 if (declares_class_or_enum)
8483 *declares_class_or_enum = true;
8484 return type_spec;
8487 /* Fall through. */
8489 case RID_TYPENAME:
8490 /* Look for an elaborated-type-specifier. */
8491 type_spec = cp_parser_elaborated_type_specifier (parser,
8492 is_friend,
8493 is_declaration);
8494 /* We're declaring a class or enum -- unless we're using
8495 `typename'. */
8496 if (declares_class_or_enum && keyword != RID_TYPENAME)
8497 *declares_class_or_enum = true;
8498 return type_spec;
8500 case RID_CONST:
8501 case RID_VOLATILE:
8502 case RID_RESTRICT:
8503 type_spec = cp_parser_cv_qualifier_opt (parser);
8504 /* Even though we call a routine that looks for an optional
8505 qualifier, we know that there should be one. */
8506 my_friendly_assert (type_spec != NULL, 20000328);
8507 /* This type-specifier was a cv-qualified. */
8508 if (is_cv_qualifier)
8509 *is_cv_qualifier = true;
8511 return type_spec;
8513 case RID_COMPLEX:
8514 /* The `__complex__' keyword is a GNU extension. */
8515 return cp_lexer_consume_token (parser->lexer)->value;
8517 default:
8518 break;
8521 /* If we do not already have a type-specifier, assume we are looking
8522 at a simple-type-specifier. */
8523 type_spec = cp_parser_simple_type_specifier (parser, flags);
8525 /* If we didn't find a type-specifier, and a type-specifier was not
8526 optional in this context, issue an error message. */
8527 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8529 cp_parser_error (parser, "expected type specifier");
8530 return error_mark_node;
8533 return type_spec;
8536 /* Parse a simple-type-specifier.
8538 simple-type-specifier:
8539 :: [opt] nested-name-specifier [opt] type-name
8540 :: [opt] nested-name-specifier template template-id
8541 char
8542 wchar_t
8543 bool
8544 short
8546 long
8547 signed
8548 unsigned
8549 float
8550 double
8551 void
8553 GNU Extension:
8555 simple-type-specifier:
8556 __typeof__ unary-expression
8557 __typeof__ ( type-id )
8559 For the various keywords, the value returned is simply the
8560 TREE_IDENTIFIER representing the keyword. For the first two
8561 productions, the value returned is the indicated TYPE_DECL. */
8563 static tree
8564 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags)
8566 tree type = NULL_TREE;
8567 cp_token *token;
8569 /* Peek at the next token. */
8570 token = cp_lexer_peek_token (parser->lexer);
8572 /* If we're looking at a keyword, things are easy. */
8573 switch (token->keyword)
8575 case RID_CHAR:
8576 case RID_WCHAR:
8577 case RID_BOOL:
8578 case RID_SHORT:
8579 case RID_INT:
8580 case RID_LONG:
8581 case RID_SIGNED:
8582 case RID_UNSIGNED:
8583 case RID_FLOAT:
8584 case RID_DOUBLE:
8585 case RID_VOID:
8586 /* Consume the token. */
8587 return cp_lexer_consume_token (parser->lexer)->value;
8589 case RID_TYPEOF:
8591 tree operand;
8593 /* Consume the `typeof' token. */
8594 cp_lexer_consume_token (parser->lexer);
8595 /* Parse the operand to `typeof' */
8596 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8597 /* If it is not already a TYPE, take its type. */
8598 if (!TYPE_P (operand))
8599 operand = finish_typeof (operand);
8601 return operand;
8604 default:
8605 break;
8608 /* The type-specifier must be a user-defined type. */
8609 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8611 /* Don't gobble tokens or issue error messages if this is an
8612 optional type-specifier. */
8613 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8614 cp_parser_parse_tentatively (parser);
8616 /* Look for the optional `::' operator. */
8617 cp_parser_global_scope_opt (parser,
8618 /*current_scope_valid_p=*/false);
8619 /* Look for the nested-name specifier. */
8620 cp_parser_nested_name_specifier_opt (parser,
8621 /*typename_keyword_p=*/false,
8622 /*check_dependency_p=*/true,
8623 /*type_p=*/false);
8624 /* If we have seen a nested-name-specifier, and the next token
8625 is `template', then we are using the template-id production. */
8626 if (parser->scope
8627 && cp_parser_optional_template_keyword (parser))
8629 /* Look for the template-id. */
8630 type = cp_parser_template_id (parser,
8631 /*template_keyword_p=*/true,
8632 /*check_dependency_p=*/true);
8633 /* If the template-id did not name a type, we are out of
8634 luck. */
8635 if (TREE_CODE (type) != TYPE_DECL)
8637 cp_parser_error (parser, "expected template-id for type");
8638 type = NULL_TREE;
8641 /* Otherwise, look for a type-name. */
8642 else
8644 type = cp_parser_type_name (parser);
8645 if (type == error_mark_node)
8646 type = NULL_TREE;
8649 /* If it didn't work out, we don't have a TYPE. */
8650 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8651 && !cp_parser_parse_definitely (parser))
8652 type = NULL_TREE;
8655 /* If we didn't get a type-name, issue an error message. */
8656 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8658 cp_parser_error (parser, "expected type-name");
8659 return error_mark_node;
8662 return type;
8665 /* Parse a type-name.
8667 type-name:
8668 class-name
8669 enum-name
8670 typedef-name
8672 enum-name:
8673 identifier
8675 typedef-name:
8676 identifier
8678 Returns a TYPE_DECL for the the type. */
8680 static tree
8681 cp_parser_type_name (cp_parser* parser)
8683 tree type_decl;
8684 tree identifier;
8686 /* We can't know yet whether it is a class-name or not. */
8687 cp_parser_parse_tentatively (parser);
8688 /* Try a class-name. */
8689 type_decl = cp_parser_class_name (parser,
8690 /*typename_keyword_p=*/false,
8691 /*template_keyword_p=*/false,
8692 /*type_p=*/false,
8693 /*check_dependency_p=*/true,
8694 /*class_head_p=*/false);
8695 /* If it's not a class-name, keep looking. */
8696 if (!cp_parser_parse_definitely (parser))
8698 /* It must be a typedef-name or an enum-name. */
8699 identifier = cp_parser_identifier (parser);
8700 if (identifier == error_mark_node)
8701 return error_mark_node;
8703 /* Look up the type-name. */
8704 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8705 /* Issue an error if we did not find a type-name. */
8706 if (TREE_CODE (type_decl) != TYPE_DECL)
8708 cp_parser_error (parser, "expected type-name");
8709 type_decl = error_mark_node;
8711 /* Remember that the name was used in the definition of the
8712 current class so that we can check later to see if the
8713 meaning would have been different after the class was
8714 entirely defined. */
8715 else if (type_decl != error_mark_node
8716 && !parser->scope)
8717 maybe_note_name_used_in_class (identifier, type_decl);
8720 return type_decl;
8724 /* Parse an elaborated-type-specifier. Note that the grammar given
8725 here incorporates the resolution to DR68.
8727 elaborated-type-specifier:
8728 class-key :: [opt] nested-name-specifier [opt] identifier
8729 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8730 enum :: [opt] nested-name-specifier [opt] identifier
8731 typename :: [opt] nested-name-specifier identifier
8732 typename :: [opt] nested-name-specifier template [opt]
8733 template-id
8735 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8736 declared `friend'. If IS_DECLARATION is TRUE, then this
8737 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8738 something is being declared.
8740 Returns the TYPE specified. */
8742 static tree
8743 cp_parser_elaborated_type_specifier (cp_parser* parser,
8744 bool is_friend,
8745 bool is_declaration)
8747 enum tag_types tag_type;
8748 tree identifier;
8749 tree type = NULL_TREE;
8751 /* See if we're looking at the `enum' keyword. */
8752 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8754 /* Consume the `enum' token. */
8755 cp_lexer_consume_token (parser->lexer);
8756 /* Remember that it's an enumeration type. */
8757 tag_type = enum_type;
8759 /* Or, it might be `typename'. */
8760 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8761 RID_TYPENAME))
8763 /* Consume the `typename' token. */
8764 cp_lexer_consume_token (parser->lexer);
8765 /* Remember that it's a `typename' type. */
8766 tag_type = typename_type;
8767 /* The `typename' keyword is only allowed in templates. */
8768 if (!processing_template_decl)
8769 pedwarn ("using `typename' outside of template");
8771 /* Otherwise it must be a class-key. */
8772 else
8774 tag_type = cp_parser_class_key (parser);
8775 if (tag_type == none_type)
8776 return error_mark_node;
8779 /* Look for the `::' operator. */
8780 cp_parser_global_scope_opt (parser,
8781 /*current_scope_valid_p=*/false);
8782 /* Look for the nested-name-specifier. */
8783 if (tag_type == typename_type)
8785 if (cp_parser_nested_name_specifier (parser,
8786 /*typename_keyword_p=*/true,
8787 /*check_dependency_p=*/true,
8788 /*type_p=*/true)
8789 == error_mark_node)
8790 return error_mark_node;
8792 else
8793 /* Even though `typename' is not present, the proposed resolution
8794 to Core Issue 180 says that in `class A<T>::B', `B' should be
8795 considered a type-name, even if `A<T>' is dependent. */
8796 cp_parser_nested_name_specifier_opt (parser,
8797 /*typename_keyword_p=*/true,
8798 /*check_dependency_p=*/true,
8799 /*type_p=*/true);
8800 /* For everything but enumeration types, consider a template-id. */
8801 if (tag_type != enum_type)
8803 bool template_p = false;
8804 tree decl;
8806 /* Allow the `template' keyword. */
8807 template_p = cp_parser_optional_template_keyword (parser);
8808 /* If we didn't see `template', we don't know if there's a
8809 template-id or not. */
8810 if (!template_p)
8811 cp_parser_parse_tentatively (parser);
8812 /* Parse the template-id. */
8813 decl = cp_parser_template_id (parser, template_p,
8814 /*check_dependency_p=*/true);
8815 /* If we didn't find a template-id, look for an ordinary
8816 identifier. */
8817 if (!template_p && !cp_parser_parse_definitely (parser))
8819 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8820 in effect, then we must assume that, upon instantiation, the
8821 template will correspond to a class. */
8822 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8823 && tag_type == typename_type)
8824 type = make_typename_type (parser->scope, decl,
8825 /*complain=*/1);
8826 else
8827 type = TREE_TYPE (decl);
8830 /* For an enumeration type, consider only a plain identifier. */
8831 if (!type)
8833 identifier = cp_parser_identifier (parser);
8835 if (identifier == error_mark_node)
8836 return error_mark_node;
8838 /* For a `typename', we needn't call xref_tag. */
8839 if (tag_type == typename_type)
8840 return make_typename_type (parser->scope, identifier,
8841 /*complain=*/1);
8842 /* Look up a qualified name in the usual way. */
8843 if (parser->scope)
8845 tree decl;
8847 /* In an elaborated-type-specifier, names are assumed to name
8848 types, so we set IS_TYPE to TRUE when calling
8849 cp_parser_lookup_name. */
8850 decl = cp_parser_lookup_name (parser, identifier,
8851 /*is_type=*/true,
8852 /*is_namespace=*/false,
8853 /*check_dependency=*/true);
8855 /* If we are parsing friend declaration, DECL may be a
8856 TEMPLATE_DECL tree node here. However, we need to check
8857 whether this TEMPLATE_DECL results in valid code. Consider
8858 the following example:
8860 namespace N {
8861 template <class T> class C {};
8863 class X {
8864 template <class T> friend class N::C; // #1, valid code
8866 template <class T> class Y {
8867 friend class N::C; // #2, invalid code
8870 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8871 name lookup of `N::C'. We see that friend declaration must
8872 be template for the code to be valid. Note that
8873 processing_template_decl does not work here since it is
8874 always 1 for the above two cases. */
8876 decl = (cp_parser_maybe_treat_template_as_class
8877 (decl, /*tag_name_p=*/is_friend
8878 && parser->num_template_parameter_lists));
8880 if (TREE_CODE (decl) != TYPE_DECL)
8882 error ("expected type-name");
8883 return error_mark_node;
8885 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8886 && tag_type != enum_type)
8887 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8888 tag_type == record_type ? "struct" : "class");
8889 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8890 && tag_type == enum_type)
8891 error ("`%T' referred to as enum", TREE_TYPE (decl));
8893 type = TREE_TYPE (decl);
8895 else
8897 /* An elaborated-type-specifier sometimes introduces a new type and
8898 sometimes names an existing type. Normally, the rule is that it
8899 introduces a new type only if there is not an existing type of
8900 the same name already in scope. For example, given:
8902 struct S {};
8903 void f() { struct S s; }
8905 the `struct S' in the body of `f' is the same `struct S' as in
8906 the global scope; the existing definition is used. However, if
8907 there were no global declaration, this would introduce a new
8908 local class named `S'.
8910 An exception to this rule applies to the following code:
8912 namespace N { struct S; }
8914 Here, the elaborated-type-specifier names a new type
8915 unconditionally; even if there is already an `S' in the
8916 containing scope this declaration names a new type.
8917 This exception only applies if the elaborated-type-specifier
8918 forms the complete declaration:
8920 [class.name]
8922 A declaration consisting solely of `class-key identifier ;' is
8923 either a redeclaration of the name in the current scope or a
8924 forward declaration of the identifier as a class name. It
8925 introduces the name into the current scope.
8927 We are in this situation precisely when the next token is a `;'.
8929 An exception to the exception is that a `friend' declaration does
8930 *not* name a new type; i.e., given:
8932 struct S { friend struct T; };
8934 `T' is not a new type in the scope of `S'.
8936 Also, `new struct S' or `sizeof (struct S)' never results in the
8937 definition of a new type; a new type can only be declared in a
8938 declaration context. */
8940 type = xref_tag (tag_type, identifier,
8941 /*attributes=*/NULL_TREE,
8942 (is_friend
8943 || !is_declaration
8944 || cp_lexer_next_token_is_not (parser->lexer,
8945 CPP_SEMICOLON)));
8948 if (tag_type != enum_type)
8949 cp_parser_check_class_key (tag_type, type);
8950 return type;
8953 /* Parse an enum-specifier.
8955 enum-specifier:
8956 enum identifier [opt] { enumerator-list [opt] }
8958 Returns an ENUM_TYPE representing the enumeration. */
8960 static tree
8961 cp_parser_enum_specifier (cp_parser* parser)
8963 cp_token *token;
8964 tree identifier = NULL_TREE;
8965 tree type;
8967 /* Look for the `enum' keyword. */
8968 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8969 return error_mark_node;
8970 /* Peek at the next token. */
8971 token = cp_lexer_peek_token (parser->lexer);
8973 /* See if it is an identifier. */
8974 if (token->type == CPP_NAME)
8975 identifier = cp_parser_identifier (parser);
8977 /* Look for the `{'. */
8978 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8979 return error_mark_node;
8981 /* At this point, we're going ahead with the enum-specifier, even
8982 if some other problem occurs. */
8983 cp_parser_commit_to_tentative_parse (parser);
8985 /* Issue an error message if type-definitions are forbidden here. */
8986 cp_parser_check_type_definition (parser);
8988 /* Create the new type. */
8989 type = start_enum (identifier ? identifier : make_anon_name ());
8991 /* Peek at the next token. */
8992 token = cp_lexer_peek_token (parser->lexer);
8993 /* If it's not a `}', then there are some enumerators. */
8994 if (token->type != CPP_CLOSE_BRACE)
8995 cp_parser_enumerator_list (parser, type);
8996 /* Look for the `}'. */
8997 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8999 /* Finish up the enumeration. */
9000 finish_enum (type);
9002 return type;
9005 /* Parse an enumerator-list. The enumerators all have the indicated
9006 TYPE.
9008 enumerator-list:
9009 enumerator-definition
9010 enumerator-list , enumerator-definition */
9012 static void
9013 cp_parser_enumerator_list (cp_parser* parser, tree type)
9015 while (true)
9017 cp_token *token;
9019 /* Parse an enumerator-definition. */
9020 cp_parser_enumerator_definition (parser, type);
9021 /* Peek at the next token. */
9022 token = cp_lexer_peek_token (parser->lexer);
9023 /* If it's not a `,', then we've reached the end of the
9024 list. */
9025 if (token->type != CPP_COMMA)
9026 break;
9027 /* Otherwise, consume the `,' and keep going. */
9028 cp_lexer_consume_token (parser->lexer);
9029 /* If the next token is a `}', there is a trailing comma. */
9030 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9032 if (pedantic && !in_system_header)
9033 pedwarn ("comma at end of enumerator list");
9034 break;
9039 /* Parse an enumerator-definition. The enumerator has the indicated
9040 TYPE.
9042 enumerator-definition:
9043 enumerator
9044 enumerator = constant-expression
9046 enumerator:
9047 identifier */
9049 static void
9050 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9052 cp_token *token;
9053 tree identifier;
9054 tree value;
9056 /* Look for the identifier. */
9057 identifier = cp_parser_identifier (parser);
9058 if (identifier == error_mark_node)
9059 return;
9061 /* Peek at the next token. */
9062 token = cp_lexer_peek_token (parser->lexer);
9063 /* If it's an `=', then there's an explicit value. */
9064 if (token->type == CPP_EQ)
9066 /* Consume the `=' token. */
9067 cp_lexer_consume_token (parser->lexer);
9068 /* Parse the value. */
9069 value = cp_parser_constant_expression (parser,
9070 /*allow_non_constant=*/false,
9071 NULL);
9073 else
9074 value = NULL_TREE;
9076 /* Create the enumerator. */
9077 build_enumerator (identifier, value, type);
9080 /* Parse a namespace-name.
9082 namespace-name:
9083 original-namespace-name
9084 namespace-alias
9086 Returns the NAMESPACE_DECL for the namespace. */
9088 static tree
9089 cp_parser_namespace_name (cp_parser* parser)
9091 tree identifier;
9092 tree namespace_decl;
9094 /* Get the name of the namespace. */
9095 identifier = cp_parser_identifier (parser);
9096 if (identifier == error_mark_node)
9097 return error_mark_node;
9099 /* Look up the identifier in the currently active scope. Look only
9100 for namespaces, due to:
9102 [basic.lookup.udir]
9104 When looking up a namespace-name in a using-directive or alias
9105 definition, only namespace names are considered.
9107 And:
9109 [basic.lookup.qual]
9111 During the lookup of a name preceding the :: scope resolution
9112 operator, object, function, and enumerator names are ignored.
9114 (Note that cp_parser_class_or_namespace_name only calls this
9115 function if the token after the name is the scope resolution
9116 operator.) */
9117 namespace_decl = cp_parser_lookup_name (parser, identifier,
9118 /*is_type=*/false,
9119 /*is_namespace=*/true,
9120 /*check_dependency=*/true);
9121 /* If it's not a namespace, issue an error. */
9122 if (namespace_decl == error_mark_node
9123 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9125 cp_parser_error (parser, "expected namespace-name");
9126 namespace_decl = error_mark_node;
9129 return namespace_decl;
9132 /* Parse a namespace-definition.
9134 namespace-definition:
9135 named-namespace-definition
9136 unnamed-namespace-definition
9138 named-namespace-definition:
9139 original-namespace-definition
9140 extension-namespace-definition
9142 original-namespace-definition:
9143 namespace identifier { namespace-body }
9145 extension-namespace-definition:
9146 namespace original-namespace-name { namespace-body }
9148 unnamed-namespace-definition:
9149 namespace { namespace-body } */
9151 static void
9152 cp_parser_namespace_definition (cp_parser* parser)
9154 tree identifier;
9156 /* Look for the `namespace' keyword. */
9157 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9159 /* Get the name of the namespace. We do not attempt to distinguish
9160 between an original-namespace-definition and an
9161 extension-namespace-definition at this point. The semantic
9162 analysis routines are responsible for that. */
9163 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9164 identifier = cp_parser_identifier (parser);
9165 else
9166 identifier = NULL_TREE;
9168 /* Look for the `{' to start the namespace. */
9169 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9170 /* Start the namespace. */
9171 push_namespace (identifier);
9172 /* Parse the body of the namespace. */
9173 cp_parser_namespace_body (parser);
9174 /* Finish the namespace. */
9175 pop_namespace ();
9176 /* Look for the final `}'. */
9177 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9180 /* Parse a namespace-body.
9182 namespace-body:
9183 declaration-seq [opt] */
9185 static void
9186 cp_parser_namespace_body (cp_parser* parser)
9188 cp_parser_declaration_seq_opt (parser);
9191 /* Parse a namespace-alias-definition.
9193 namespace-alias-definition:
9194 namespace identifier = qualified-namespace-specifier ; */
9196 static void
9197 cp_parser_namespace_alias_definition (cp_parser* parser)
9199 tree identifier;
9200 tree namespace_specifier;
9202 /* Look for the `namespace' keyword. */
9203 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9204 /* Look for the identifier. */
9205 identifier = cp_parser_identifier (parser);
9206 if (identifier == error_mark_node)
9207 return;
9208 /* Look for the `=' token. */
9209 cp_parser_require (parser, CPP_EQ, "`='");
9210 /* Look for the qualified-namespace-specifier. */
9211 namespace_specifier
9212 = cp_parser_qualified_namespace_specifier (parser);
9213 /* Look for the `;' token. */
9214 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9216 /* Register the alias in the symbol table. */
9217 do_namespace_alias (identifier, namespace_specifier);
9220 /* Parse a qualified-namespace-specifier.
9222 qualified-namespace-specifier:
9223 :: [opt] nested-name-specifier [opt] namespace-name
9225 Returns a NAMESPACE_DECL corresponding to the specified
9226 namespace. */
9228 static tree
9229 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9231 /* Look for the optional `::'. */
9232 cp_parser_global_scope_opt (parser,
9233 /*current_scope_valid_p=*/false);
9235 /* Look for the optional nested-name-specifier. */
9236 cp_parser_nested_name_specifier_opt (parser,
9237 /*typename_keyword_p=*/false,
9238 /*check_dependency_p=*/true,
9239 /*type_p=*/false);
9241 return cp_parser_namespace_name (parser);
9244 /* Parse a using-declaration.
9246 using-declaration:
9247 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9248 using :: unqualified-id ; */
9250 static void
9251 cp_parser_using_declaration (cp_parser* parser)
9253 cp_token *token;
9254 bool typename_p = false;
9255 bool global_scope_p;
9256 tree decl;
9257 tree identifier;
9258 tree scope;
9260 /* Look for the `using' keyword. */
9261 cp_parser_require_keyword (parser, RID_USING, "`using'");
9263 /* Peek at the next token. */
9264 token = cp_lexer_peek_token (parser->lexer);
9265 /* See if it's `typename'. */
9266 if (token->keyword == RID_TYPENAME)
9268 /* Remember that we've seen it. */
9269 typename_p = true;
9270 /* Consume the `typename' token. */
9271 cp_lexer_consume_token (parser->lexer);
9274 /* Look for the optional global scope qualification. */
9275 global_scope_p
9276 = (cp_parser_global_scope_opt (parser,
9277 /*current_scope_valid_p=*/false)
9278 != NULL_TREE);
9280 /* If we saw `typename', or didn't see `::', then there must be a
9281 nested-name-specifier present. */
9282 if (typename_p || !global_scope_p)
9283 cp_parser_nested_name_specifier (parser, typename_p,
9284 /*check_dependency_p=*/true,
9285 /*type_p=*/false);
9286 /* Otherwise, we could be in either of the two productions. In that
9287 case, treat the nested-name-specifier as optional. */
9288 else
9289 cp_parser_nested_name_specifier_opt (parser,
9290 /*typename_keyword_p=*/false,
9291 /*check_dependency_p=*/true,
9292 /*type_p=*/false);
9294 /* Parse the unqualified-id. */
9295 identifier = cp_parser_unqualified_id (parser,
9296 /*template_keyword_p=*/false,
9297 /*check_dependency_p=*/true);
9299 /* The function we call to handle a using-declaration is different
9300 depending on what scope we are in. */
9301 scope = current_scope ();
9302 if (scope && TYPE_P (scope))
9304 /* Create the USING_DECL. */
9305 decl = do_class_using_decl (build_nt (SCOPE_REF,
9306 parser->scope,
9307 identifier));
9308 /* Add it to the list of members in this class. */
9309 finish_member_declaration (decl);
9311 else
9313 decl = cp_parser_lookup_name_simple (parser, identifier);
9314 if (decl == error_mark_node)
9316 if (parser->scope && parser->scope != global_namespace)
9317 error ("`%D::%D' has not been declared",
9318 parser->scope, identifier);
9319 else
9320 error ("`::%D' has not been declared", identifier);
9322 else if (scope)
9323 do_local_using_decl (decl);
9324 else
9325 do_toplevel_using_decl (decl);
9328 /* Look for the final `;'. */
9329 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9332 /* Parse a using-directive.
9334 using-directive:
9335 using namespace :: [opt] nested-name-specifier [opt]
9336 namespace-name ; */
9338 static void
9339 cp_parser_using_directive (cp_parser* parser)
9341 tree namespace_decl;
9343 /* Look for the `using' keyword. */
9344 cp_parser_require_keyword (parser, RID_USING, "`using'");
9345 /* And the `namespace' keyword. */
9346 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9347 /* Look for the optional `::' operator. */
9348 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9349 /* And the optional nested-name-sepcifier. */
9350 cp_parser_nested_name_specifier_opt (parser,
9351 /*typename_keyword_p=*/false,
9352 /*check_dependency_p=*/true,
9353 /*type_p=*/false);
9354 /* Get the namespace being used. */
9355 namespace_decl = cp_parser_namespace_name (parser);
9356 /* Update the symbol table. */
9357 do_using_directive (namespace_decl);
9358 /* Look for the final `;'. */
9359 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9362 /* Parse an asm-definition.
9364 asm-definition:
9365 asm ( string-literal ) ;
9367 GNU Extension:
9369 asm-definition:
9370 asm volatile [opt] ( string-literal ) ;
9371 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9372 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9373 : asm-operand-list [opt] ) ;
9374 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9375 : asm-operand-list [opt]
9376 : asm-operand-list [opt] ) ; */
9378 static void
9379 cp_parser_asm_definition (cp_parser* parser)
9381 cp_token *token;
9382 tree string;
9383 tree outputs = NULL_TREE;
9384 tree inputs = NULL_TREE;
9385 tree clobbers = NULL_TREE;
9386 tree asm_stmt;
9387 bool volatile_p = false;
9388 bool extended_p = false;
9390 /* Look for the `asm' keyword. */
9391 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9392 /* See if the next token is `volatile'. */
9393 if (cp_parser_allow_gnu_extensions_p (parser)
9394 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9396 /* Remember that we saw the `volatile' keyword. */
9397 volatile_p = true;
9398 /* Consume the token. */
9399 cp_lexer_consume_token (parser->lexer);
9401 /* Look for the opening `('. */
9402 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9403 /* Look for the string. */
9404 token = cp_parser_require (parser, CPP_STRING, "asm body");
9405 if (!token)
9406 return;
9407 string = token->value;
9408 /* If we're allowing GNU extensions, check for the extended assembly
9409 syntax. Unfortunately, the `:' tokens need not be separated by
9410 a space in C, and so, for compatibility, we tolerate that here
9411 too. Doing that means that we have to treat the `::' operator as
9412 two `:' tokens. */
9413 if (cp_parser_allow_gnu_extensions_p (parser)
9414 && at_function_scope_p ()
9415 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9416 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9418 bool inputs_p = false;
9419 bool clobbers_p = false;
9421 /* The extended syntax was used. */
9422 extended_p = true;
9424 /* Look for outputs. */
9425 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9427 /* Consume the `:'. */
9428 cp_lexer_consume_token (parser->lexer);
9429 /* Parse the output-operands. */
9430 if (cp_lexer_next_token_is_not (parser->lexer,
9431 CPP_COLON)
9432 && cp_lexer_next_token_is_not (parser->lexer,
9433 CPP_SCOPE)
9434 && cp_lexer_next_token_is_not (parser->lexer,
9435 CPP_CLOSE_PAREN))
9436 outputs = cp_parser_asm_operand_list (parser);
9438 /* If the next token is `::', there are no outputs, and the
9439 next token is the beginning of the inputs. */
9440 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9442 /* Consume the `::' token. */
9443 cp_lexer_consume_token (parser->lexer);
9444 /* The inputs are coming next. */
9445 inputs_p = true;
9448 /* Look for inputs. */
9449 if (inputs_p
9450 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9452 if (!inputs_p)
9453 /* Consume the `:'. */
9454 cp_lexer_consume_token (parser->lexer);
9455 /* Parse the output-operands. */
9456 if (cp_lexer_next_token_is_not (parser->lexer,
9457 CPP_COLON)
9458 && cp_lexer_next_token_is_not (parser->lexer,
9459 CPP_SCOPE)
9460 && cp_lexer_next_token_is_not (parser->lexer,
9461 CPP_CLOSE_PAREN))
9462 inputs = cp_parser_asm_operand_list (parser);
9464 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9465 /* The clobbers are coming next. */
9466 clobbers_p = true;
9468 /* Look for clobbers. */
9469 if (clobbers_p
9470 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9472 if (!clobbers_p)
9473 /* Consume the `:'. */
9474 cp_lexer_consume_token (parser->lexer);
9475 /* Parse the clobbers. */
9476 if (cp_lexer_next_token_is_not (parser->lexer,
9477 CPP_CLOSE_PAREN))
9478 clobbers = cp_parser_asm_clobber_list (parser);
9481 /* Look for the closing `)'. */
9482 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9483 cp_parser_skip_to_closing_parenthesis (parser);
9484 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9486 /* Create the ASM_STMT. */
9487 if (at_function_scope_p ())
9489 asm_stmt =
9490 finish_asm_stmt (volatile_p
9491 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9492 string, outputs, inputs, clobbers);
9493 /* If the extended syntax was not used, mark the ASM_STMT. */
9494 if (!extended_p)
9495 ASM_INPUT_P (asm_stmt) = 1;
9497 else
9498 assemble_asm (string);
9501 /* Declarators [gram.dcl.decl] */
9503 /* Parse an init-declarator.
9505 init-declarator:
9506 declarator initializer [opt]
9508 GNU Extension:
9510 init-declarator:
9511 declarator asm-specification [opt] attributes [opt] initializer [opt]
9513 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9514 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9515 then this declarator appears in a class scope. The new DECL created
9516 by this declarator is returned.
9518 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9519 for a function-definition here as well. If the declarator is a
9520 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9521 be TRUE upon return. By that point, the function-definition will
9522 have been completely parsed.
9524 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9525 is FALSE. */
9527 static tree
9528 cp_parser_init_declarator (cp_parser* parser,
9529 tree decl_specifiers,
9530 tree prefix_attributes,
9531 bool function_definition_allowed_p,
9532 bool member_p,
9533 bool* function_definition_p)
9535 cp_token *token;
9536 tree declarator;
9537 tree attributes;
9538 tree asm_specification;
9539 tree initializer;
9540 tree decl = NULL_TREE;
9541 tree scope;
9542 bool is_initialized;
9543 bool is_parenthesized_init;
9544 bool ctor_dtor_or_conv_p;
9545 bool friend_p;
9547 /* Assume that this is not the declarator for a function
9548 definition. */
9549 if (function_definition_p)
9550 *function_definition_p = false;
9552 /* Defer access checks while parsing the declarator; we cannot know
9553 what names are accessible until we know what is being
9554 declared. */
9555 resume_deferring_access_checks ();
9557 /* Parse the declarator. */
9558 declarator
9559 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9560 &ctor_dtor_or_conv_p);
9561 /* Gather up the deferred checks. */
9562 stop_deferring_access_checks ();
9564 /* If the DECLARATOR was erroneous, there's no need to go
9565 further. */
9566 if (declarator == error_mark_node)
9567 return error_mark_node;
9569 /* Figure out what scope the entity declared by the DECLARATOR is
9570 located in. `grokdeclarator' sometimes changes the scope, so
9571 we compute it now. */
9572 scope = get_scope_of_declarator (declarator);
9574 /* If we're allowing GNU extensions, look for an asm-specification
9575 and attributes. */
9576 if (cp_parser_allow_gnu_extensions_p (parser))
9578 /* Look for an asm-specification. */
9579 asm_specification = cp_parser_asm_specification_opt (parser);
9580 /* And attributes. */
9581 attributes = cp_parser_attributes_opt (parser);
9583 else
9585 asm_specification = NULL_TREE;
9586 attributes = NULL_TREE;
9589 /* Peek at the next token. */
9590 token = cp_lexer_peek_token (parser->lexer);
9591 /* Check to see if the token indicates the start of a
9592 function-definition. */
9593 if (cp_parser_token_starts_function_definition_p (token))
9595 if (!function_definition_allowed_p)
9597 /* If a function-definition should not appear here, issue an
9598 error message. */
9599 cp_parser_error (parser,
9600 "a function-definition is not allowed here");
9601 return error_mark_node;
9603 else
9605 /* Neither attributes nor an asm-specification are allowed
9606 on a function-definition. */
9607 if (asm_specification)
9608 error ("an asm-specification is not allowed on a function-definition");
9609 if (attributes)
9610 error ("attributes are not allowed on a function-definition");
9611 /* This is a function-definition. */
9612 *function_definition_p = true;
9614 /* Parse the function definition. */
9615 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9616 (parser, decl_specifiers, prefix_attributes, declarator));
9618 return decl;
9622 /* [dcl.dcl]
9624 Only in function declarations for constructors, destructors, and
9625 type conversions can the decl-specifier-seq be omitted.
9627 We explicitly postpone this check past the point where we handle
9628 function-definitions because we tolerate function-definitions
9629 that are missing their return types in some modes. */
9630 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9632 cp_parser_error (parser,
9633 "expected constructor, destructor, or type conversion");
9634 return error_mark_node;
9637 /* An `=' or an `(' indicates an initializer. */
9638 is_initialized = (token->type == CPP_EQ
9639 || token->type == CPP_OPEN_PAREN);
9640 /* If the init-declarator isn't initialized and isn't followed by a
9641 `,' or `;', it's not a valid init-declarator. */
9642 if (!is_initialized
9643 && token->type != CPP_COMMA
9644 && token->type != CPP_SEMICOLON)
9646 cp_parser_error (parser, "expected init-declarator");
9647 return error_mark_node;
9650 /* Because start_decl has side-effects, we should only call it if we
9651 know we're going ahead. By this point, we know that we cannot
9652 possibly be looking at any other construct. */
9653 cp_parser_commit_to_tentative_parse (parser);
9655 /* Check to see whether or not this declaration is a friend. */
9656 friend_p = cp_parser_friend_p (decl_specifiers);
9658 /* Check that the number of template-parameter-lists is OK. */
9659 if (!cp_parser_check_declarator_template_parameters (parser,
9660 declarator))
9661 return error_mark_node;
9663 /* Enter the newly declared entry in the symbol table. If we're
9664 processing a declaration in a class-specifier, we wait until
9665 after processing the initializer. */
9666 if (!member_p)
9668 if (parser->in_unbraced_linkage_specification_p)
9670 decl_specifiers = tree_cons (error_mark_node,
9671 get_identifier ("extern"),
9672 decl_specifiers);
9673 have_extern_spec = false;
9675 decl = start_decl (declarator,
9676 decl_specifiers,
9677 is_initialized,
9678 attributes,
9679 prefix_attributes);
9682 /* Enter the SCOPE. That way unqualified names appearing in the
9683 initializer will be looked up in SCOPE. */
9684 if (scope)
9685 push_scope (scope);
9687 /* Perform deferred access control checks, now that we know in which
9688 SCOPE the declared entity resides. */
9689 if (!member_p && decl)
9691 tree saved_current_function_decl = NULL_TREE;
9693 /* If the entity being declared is a function, pretend that we
9694 are in its scope. If it is a `friend', it may have access to
9695 things that would not otherwise be accessible. */
9696 if (TREE_CODE (decl) == FUNCTION_DECL)
9698 saved_current_function_decl = current_function_decl;
9699 current_function_decl = decl;
9702 /* Perform the access control checks for the declarator and the
9703 the decl-specifiers. */
9704 perform_deferred_access_checks ();
9706 /* Restore the saved value. */
9707 if (TREE_CODE (decl) == FUNCTION_DECL)
9708 current_function_decl = saved_current_function_decl;
9711 /* Parse the initializer. */
9712 if (is_initialized)
9713 initializer = cp_parser_initializer (parser, &is_parenthesized_init);
9714 else
9716 initializer = NULL_TREE;
9717 is_parenthesized_init = false;
9720 /* The old parser allows attributes to appear after a parenthesized
9721 initializer. Mark Mitchell proposed removing this functionality
9722 on the GCC mailing lists on 2002-08-13. This parser accepts the
9723 attributes -- but ignores them. */
9724 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9725 if (cp_parser_attributes_opt (parser))
9726 warning ("attributes after parenthesized initializer ignored");
9728 /* Leave the SCOPE, now that we have processed the initializer. It
9729 is important to do this before calling cp_finish_decl because it
9730 makes decisions about whether to create DECL_STMTs or not based
9731 on the current scope. */
9732 if (scope)
9733 pop_scope (scope);
9735 /* For an in-class declaration, use `grokfield' to create the
9736 declaration. */
9737 if (member_p)
9738 decl = grokfield (declarator, decl_specifiers,
9739 initializer, /*asmspec=*/NULL_TREE,
9740 /*attributes=*/NULL_TREE);
9742 /* Finish processing the declaration. But, skip friend
9743 declarations. */
9744 if (!friend_p && decl)
9745 cp_finish_decl (decl,
9746 initializer,
9747 asm_specification,
9748 /* If the initializer is in parentheses, then this is
9749 a direct-initialization, which means that an
9750 `explicit' constructor is OK. Otherwise, an
9751 `explicit' constructor cannot be used. */
9752 ((is_parenthesized_init || !is_initialized)
9753 ? 0 : LOOKUP_ONLYCONVERTING));
9755 return decl;
9758 /* Parse a declarator.
9760 declarator:
9761 direct-declarator
9762 ptr-operator declarator
9764 abstract-declarator:
9765 ptr-operator abstract-declarator [opt]
9766 direct-abstract-declarator
9768 GNU Extensions:
9770 declarator:
9771 attributes [opt] direct-declarator
9772 attributes [opt] ptr-operator declarator
9774 abstract-declarator:
9775 attributes [opt] ptr-operator abstract-declarator [opt]
9776 attributes [opt] direct-abstract-declarator
9778 Returns a representation of the declarator. If the declarator has
9779 the form `* declarator', then an INDIRECT_REF is returned, whose
9780 only operand is the sub-declarator. Analagously, `& declarator' is
9781 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9782 used. The first operand is the TYPE for `X'. The second operand
9783 is an INDIRECT_REF whose operand is the sub-declarator.
9785 Otherwise, the reprsentation is as for a direct-declarator.
9787 (It would be better to define a structure type to represent
9788 declarators, rather than abusing `tree' nodes to represent
9789 declarators. That would be much clearer and save some memory.
9790 There is no reason for declarators to be garbage-collected, for
9791 example; they are created during parser and no longer needed after
9792 `grokdeclarator' has been called.)
9794 For a ptr-operator that has the optional cv-qualifier-seq,
9795 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9796 node.
9798 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9799 true if this declarator represents a constructor, destructor, or
9800 type conversion operator. Otherwise, it is set to false.
9802 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9803 a decl-specifier-seq unless it declares a constructor, destructor,
9804 or conversion. It might seem that we could check this condition in
9805 semantic analysis, rather than parsing, but that makes it difficult
9806 to handle something like `f()'. We want to notice that there are
9807 no decl-specifiers, and therefore realize that this is an
9808 expression, not a declaration.) */
9810 static tree
9811 cp_parser_declarator (cp_parser* parser,
9812 cp_parser_declarator_kind dcl_kind,
9813 bool* ctor_dtor_or_conv_p)
9815 cp_token *token;
9816 tree declarator;
9817 enum tree_code code;
9818 tree cv_qualifier_seq;
9819 tree class_type;
9820 tree attributes = NULL_TREE;
9822 /* Assume this is not a constructor, destructor, or type-conversion
9823 operator. */
9824 if (ctor_dtor_or_conv_p)
9825 *ctor_dtor_or_conv_p = false;
9827 if (cp_parser_allow_gnu_extensions_p (parser))
9828 attributes = cp_parser_attributes_opt (parser);
9830 /* Peek at the next token. */
9831 token = cp_lexer_peek_token (parser->lexer);
9833 /* Check for the ptr-operator production. */
9834 cp_parser_parse_tentatively (parser);
9835 /* Parse the ptr-operator. */
9836 code = cp_parser_ptr_operator (parser,
9837 &class_type,
9838 &cv_qualifier_seq);
9839 /* If that worked, then we have a ptr-operator. */
9840 if (cp_parser_parse_definitely (parser))
9842 /* The dependent declarator is optional if we are parsing an
9843 abstract-declarator. */
9844 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9845 cp_parser_parse_tentatively (parser);
9847 /* Parse the dependent declarator. */
9848 declarator = cp_parser_declarator (parser, dcl_kind,
9849 /*ctor_dtor_or_conv_p=*/NULL);
9851 /* If we are parsing an abstract-declarator, we must handle the
9852 case where the dependent declarator is absent. */
9853 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9854 && !cp_parser_parse_definitely (parser))
9855 declarator = NULL_TREE;
9857 /* Build the representation of the ptr-operator. */
9858 if (code == INDIRECT_REF)
9859 declarator = make_pointer_declarator (cv_qualifier_seq,
9860 declarator);
9861 else
9862 declarator = make_reference_declarator (cv_qualifier_seq,
9863 declarator);
9864 /* Handle the pointer-to-member case. */
9865 if (class_type)
9866 declarator = build_nt (SCOPE_REF, class_type, declarator);
9868 /* Everything else is a direct-declarator. */
9869 else
9870 declarator = cp_parser_direct_declarator (parser,
9871 dcl_kind,
9872 ctor_dtor_or_conv_p);
9874 if (attributes && declarator != error_mark_node)
9875 declarator = tree_cons (attributes, declarator, NULL_TREE);
9877 return declarator;
9880 /* Parse a direct-declarator or direct-abstract-declarator.
9882 direct-declarator:
9883 declarator-id
9884 direct-declarator ( parameter-declaration-clause )
9885 cv-qualifier-seq [opt]
9886 exception-specification [opt]
9887 direct-declarator [ constant-expression [opt] ]
9888 ( declarator )
9890 direct-abstract-declarator:
9891 direct-abstract-declarator [opt]
9892 ( parameter-declaration-clause )
9893 cv-qualifier-seq [opt]
9894 exception-specification [opt]
9895 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9896 ( abstract-declarator )
9898 Returns a representation of the declarator. DCL_KIND is
9899 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9900 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9901 we are parsing a direct-declarator. It is
9902 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9903 of ambiguity we prefer an abstract declarator, as per
9904 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9905 cp_parser_declarator.
9907 For the declarator-id production, the representation is as for an
9908 id-expression, except that a qualified name is represented as a
9909 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9910 see the documentation of the FUNCTION_DECLARATOR_* macros for
9911 information about how to find the various declarator components.
9912 An array-declarator is represented as an ARRAY_REF. The
9913 direct-declarator is the first operand; the constant-expression
9914 indicating the size of the array is the second operand. */
9916 static tree
9917 cp_parser_direct_declarator (cp_parser* parser,
9918 cp_parser_declarator_kind dcl_kind,
9919 bool* ctor_dtor_or_conv_p)
9921 cp_token *token;
9922 tree declarator = NULL_TREE;
9923 tree scope = NULL_TREE;
9924 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9925 bool saved_in_declarator_p = parser->in_declarator_p;
9926 bool first = true;
9928 while (true)
9930 /* Peek at the next token. */
9931 token = cp_lexer_peek_token (parser->lexer);
9932 if (token->type == CPP_OPEN_PAREN)
9934 /* This is either a parameter-declaration-clause, or a
9935 parenthesized declarator. When we know we are parsing a
9936 named declarator, it must be a paranthesized declarator
9937 if FIRST is true. For instance, `(int)' is a
9938 parameter-declaration-clause, with an omitted
9939 direct-abstract-declarator. But `((*))', is a
9940 parenthesized abstract declarator. Finally, when T is a
9941 template parameter `(T)' is a
9942 paremeter-declaration-clause, and not a parenthesized
9943 named declarator.
9945 We first try and parse a parameter-declaration-clause,
9946 and then try a nested declarator (if FIRST is true).
9948 It is not an error for it not to be a
9949 parameter-declaration-clause, even when FIRST is
9950 false. Consider,
9952 int i (int);
9953 int i (3);
9955 The first is the declaration of a function while the
9956 second is a the definition of a variable, including its
9957 initializer.
9959 Having seen only the parenthesis, we cannot know which of
9960 these two alternatives should be selected. Even more
9961 complex are examples like:
9963 int i (int (a));
9964 int i (int (3));
9966 The former is a function-declaration; the latter is a
9967 variable initialization.
9969 Thus again, we try a parameter-declation-clause, and if
9970 that fails, we back out and return. */
9972 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9974 tree params;
9976 cp_parser_parse_tentatively (parser);
9978 /* Consume the `('. */
9979 cp_lexer_consume_token (parser->lexer);
9980 if (first)
9982 /* If this is going to be an abstract declarator, we're
9983 in a declarator and we can't have default args. */
9984 parser->default_arg_ok_p = false;
9985 parser->in_declarator_p = true;
9988 /* Parse the parameter-declaration-clause. */
9989 params = cp_parser_parameter_declaration_clause (parser);
9991 /* If all went well, parse the cv-qualifier-seq and the
9992 exception-specfication. */
9993 if (cp_parser_parse_definitely (parser))
9995 tree cv_qualifiers;
9996 tree exception_specification;
9998 first = false;
9999 /* Consume the `)'. */
10000 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10002 /* Parse the cv-qualifier-seq. */
10003 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10004 /* And the exception-specification. */
10005 exception_specification
10006 = cp_parser_exception_specification_opt (parser);
10008 /* Create the function-declarator. */
10009 declarator = make_call_declarator (declarator,
10010 params,
10011 cv_qualifiers,
10012 exception_specification);
10013 /* Any subsequent parameter lists are to do with
10014 return type, so are not those of the declared
10015 function. */
10016 parser->default_arg_ok_p = false;
10018 /* Repeat the main loop. */
10019 continue;
10023 /* If this is the first, we can try a parenthesized
10024 declarator. */
10025 if (first)
10027 parser->default_arg_ok_p = saved_default_arg_ok_p;
10028 parser->in_declarator_p = saved_in_declarator_p;
10030 /* Consume the `('. */
10031 cp_lexer_consume_token (parser->lexer);
10032 /* Parse the nested declarator. */
10033 declarator
10034 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10035 first = false;
10036 /* Expect a `)'. */
10037 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10038 declarator = error_mark_node;
10039 if (declarator == error_mark_node)
10040 break;
10042 goto handle_declarator;
10044 /* Otherwise, we must be done. */
10045 else
10046 break;
10048 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10049 && token->type == CPP_OPEN_SQUARE)
10051 /* Parse an array-declarator. */
10052 tree bounds;
10054 first = false;
10055 parser->default_arg_ok_p = false;
10056 parser->in_declarator_p = true;
10057 /* Consume the `['. */
10058 cp_lexer_consume_token (parser->lexer);
10059 /* Peek at the next token. */
10060 token = cp_lexer_peek_token (parser->lexer);
10061 /* If the next token is `]', then there is no
10062 constant-expression. */
10063 if (token->type != CPP_CLOSE_SQUARE)
10065 bool non_constant_p;
10067 bounds
10068 = cp_parser_constant_expression (parser,
10069 /*allow_non_constant=*/true,
10070 &non_constant_p);
10071 /* If we're in a template, but the constant-expression
10072 isn't value dependent, simplify it. We're supposed
10073 to treat:
10075 template <typename T> void f(T[1 + 1]);
10076 template <typename T> void f(T[2]);
10078 as two declarations of the same function, for
10079 example. */
10080 if (processing_template_decl
10081 && !non_constant_p
10082 && !value_dependent_expression_p (bounds))
10084 HOST_WIDE_INT saved_processing_template_decl;
10086 saved_processing_template_decl = processing_template_decl;
10087 processing_template_decl = 0;
10088 bounds = build_expr_from_tree (bounds);
10089 processing_template_decl = saved_processing_template_decl;
10092 else
10093 bounds = NULL_TREE;
10094 /* Look for the closing `]'. */
10095 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10097 declarator = error_mark_node;
10098 break;
10101 declarator = build_nt (ARRAY_REF, declarator, bounds);
10103 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10105 /* Parse a declarator_id */
10106 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10107 cp_parser_parse_tentatively (parser);
10108 declarator = cp_parser_declarator_id (parser);
10109 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10111 if (!cp_parser_parse_definitely (parser))
10112 declarator = error_mark_node;
10113 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10115 cp_parser_error (parser, "expected unqualified-id");
10116 declarator = error_mark_node;
10120 if (declarator == error_mark_node)
10121 break;
10123 if (TREE_CODE (declarator) == SCOPE_REF)
10125 tree scope = TREE_OPERAND (declarator, 0);
10127 /* In the declaration of a member of a template class
10128 outside of the class itself, the SCOPE will sometimes
10129 be a TYPENAME_TYPE. For example, given:
10131 template <typename T>
10132 int S<T>::R::i = 3;
10134 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10135 this context, we must resolve S<T>::R to an ordinary
10136 type, rather than a typename type.
10138 The reason we normally avoid resolving TYPENAME_TYPEs
10139 is that a specialization of `S' might render
10140 `S<T>::R' not a type. However, if `S' is
10141 specialized, then this `i' will not be used, so there
10142 is no harm in resolving the types here. */
10143 if (TREE_CODE (scope) == TYPENAME_TYPE)
10145 tree type;
10147 /* Resolve the TYPENAME_TYPE. */
10148 type = resolve_typename_type (scope,
10149 /*only_current_p=*/false);
10150 /* If that failed, the declarator is invalid. */
10151 if (type != error_mark_node)
10152 scope = type;
10153 /* Build a new DECLARATOR. */
10154 declarator = build_nt (SCOPE_REF,
10155 scope,
10156 TREE_OPERAND (declarator, 1));
10160 /* Check to see whether the declarator-id names a constructor,
10161 destructor, or conversion. */
10162 if (declarator && ctor_dtor_or_conv_p
10163 && ((TREE_CODE (declarator) == SCOPE_REF
10164 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10165 || (TREE_CODE (declarator) != SCOPE_REF
10166 && at_class_scope_p ())))
10168 tree unqualified_name;
10169 tree class_type;
10171 /* Get the unqualified part of the name. */
10172 if (TREE_CODE (declarator) == SCOPE_REF)
10174 class_type = TREE_OPERAND (declarator, 0);
10175 unqualified_name = TREE_OPERAND (declarator, 1);
10177 else
10179 class_type = current_class_type;
10180 unqualified_name = declarator;
10183 /* See if it names ctor, dtor or conv. */
10184 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10185 || IDENTIFIER_TYPENAME_P (unqualified_name)
10186 || constructor_name_p (unqualified_name, class_type))
10187 *ctor_dtor_or_conv_p = true;
10190 handle_declarator:;
10191 scope = get_scope_of_declarator (declarator);
10192 if (scope)
10193 /* Any names that appear after the declarator-id for a member
10194 are looked up in the containing scope. */
10195 push_scope (scope);
10196 parser->in_declarator_p = true;
10197 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10198 || (declarator
10199 && (TREE_CODE (declarator) == SCOPE_REF
10200 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10201 /* Default args are only allowed on function
10202 declarations. */
10203 parser->default_arg_ok_p = saved_default_arg_ok_p;
10204 else
10205 parser->default_arg_ok_p = false;
10207 first = false;
10209 /* We're done. */
10210 else
10211 break;
10214 /* For an abstract declarator, we might wind up with nothing at this
10215 point. That's an error; the declarator is not optional. */
10216 if (!declarator)
10217 cp_parser_error (parser, "expected declarator");
10219 /* If we entered a scope, we must exit it now. */
10220 if (scope)
10221 pop_scope (scope);
10223 parser->default_arg_ok_p = saved_default_arg_ok_p;
10224 parser->in_declarator_p = saved_in_declarator_p;
10226 return declarator;
10229 /* Parse a ptr-operator.
10231 ptr-operator:
10232 * cv-qualifier-seq [opt]
10234 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10236 GNU Extension:
10238 ptr-operator:
10239 & cv-qualifier-seq [opt]
10241 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10242 used. Returns ADDR_EXPR if a reference was used. In the
10243 case of a pointer-to-member, *TYPE is filled in with the
10244 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10245 with the cv-qualifier-seq, or NULL_TREE, if there are no
10246 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10248 static enum tree_code
10249 cp_parser_ptr_operator (cp_parser* parser,
10250 tree* type,
10251 tree* cv_qualifier_seq)
10253 enum tree_code code = ERROR_MARK;
10254 cp_token *token;
10256 /* Assume that it's not a pointer-to-member. */
10257 *type = NULL_TREE;
10258 /* And that there are no cv-qualifiers. */
10259 *cv_qualifier_seq = NULL_TREE;
10261 /* Peek at the next token. */
10262 token = cp_lexer_peek_token (parser->lexer);
10263 /* If it's a `*' or `&' we have a pointer or reference. */
10264 if (token->type == CPP_MULT || token->type == CPP_AND)
10266 /* Remember which ptr-operator we were processing. */
10267 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10269 /* Consume the `*' or `&'. */
10270 cp_lexer_consume_token (parser->lexer);
10272 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10273 `&', if we are allowing GNU extensions. (The only qualifier
10274 that can legally appear after `&' is `restrict', but that is
10275 enforced during semantic analysis. */
10276 if (code == INDIRECT_REF
10277 || cp_parser_allow_gnu_extensions_p (parser))
10278 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10280 else
10282 /* Try the pointer-to-member case. */
10283 cp_parser_parse_tentatively (parser);
10284 /* Look for the optional `::' operator. */
10285 cp_parser_global_scope_opt (parser,
10286 /*current_scope_valid_p=*/false);
10287 /* Look for the nested-name specifier. */
10288 cp_parser_nested_name_specifier (parser,
10289 /*typename_keyword_p=*/false,
10290 /*check_dependency_p=*/true,
10291 /*type_p=*/false);
10292 /* If we found it, and the next token is a `*', then we are
10293 indeed looking at a pointer-to-member operator. */
10294 if (!cp_parser_error_occurred (parser)
10295 && cp_parser_require (parser, CPP_MULT, "`*'"))
10297 /* The type of which the member is a member is given by the
10298 current SCOPE. */
10299 *type = parser->scope;
10300 /* The next name will not be qualified. */
10301 parser->scope = NULL_TREE;
10302 parser->qualifying_scope = NULL_TREE;
10303 parser->object_scope = NULL_TREE;
10304 /* Indicate that the `*' operator was used. */
10305 code = INDIRECT_REF;
10306 /* Look for the optional cv-qualifier-seq. */
10307 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10309 /* If that didn't work we don't have a ptr-operator. */
10310 if (!cp_parser_parse_definitely (parser))
10311 cp_parser_error (parser, "expected ptr-operator");
10314 return code;
10317 /* Parse an (optional) cv-qualifier-seq.
10319 cv-qualifier-seq:
10320 cv-qualifier cv-qualifier-seq [opt]
10322 Returns a TREE_LIST. The TREE_VALUE of each node is the
10323 representation of a cv-qualifier. */
10325 static tree
10326 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10328 tree cv_qualifiers = NULL_TREE;
10330 while (true)
10332 tree cv_qualifier;
10334 /* Look for the next cv-qualifier. */
10335 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10336 /* If we didn't find one, we're done. */
10337 if (!cv_qualifier)
10338 break;
10340 /* Add this cv-qualifier to the list. */
10341 cv_qualifiers
10342 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10345 /* We built up the list in reverse order. */
10346 return nreverse (cv_qualifiers);
10349 /* Parse an (optional) cv-qualifier.
10351 cv-qualifier:
10352 const
10353 volatile
10355 GNU Extension:
10357 cv-qualifier:
10358 __restrict__ */
10360 static tree
10361 cp_parser_cv_qualifier_opt (cp_parser* parser)
10363 cp_token *token;
10364 tree cv_qualifier = NULL_TREE;
10366 /* Peek at the next token. */
10367 token = cp_lexer_peek_token (parser->lexer);
10368 /* See if it's a cv-qualifier. */
10369 switch (token->keyword)
10371 case RID_CONST:
10372 case RID_VOLATILE:
10373 case RID_RESTRICT:
10374 /* Save the value of the token. */
10375 cv_qualifier = token->value;
10376 /* Consume the token. */
10377 cp_lexer_consume_token (parser->lexer);
10378 break;
10380 default:
10381 break;
10384 return cv_qualifier;
10387 /* Parse a declarator-id.
10389 declarator-id:
10390 id-expression
10391 :: [opt] nested-name-specifier [opt] type-name
10393 In the `id-expression' case, the value returned is as for
10394 cp_parser_id_expression if the id-expression was an unqualified-id.
10395 If the id-expression was a qualified-id, then a SCOPE_REF is
10396 returned. The first operand is the scope (either a NAMESPACE_DECL
10397 or TREE_TYPE), but the second is still just a representation of an
10398 unqualified-id. */
10400 static tree
10401 cp_parser_declarator_id (cp_parser* parser)
10403 tree id_expression;
10405 /* The expression must be an id-expression. Assume that qualified
10406 names are the names of types so that:
10408 template <class T>
10409 int S<T>::R::i = 3;
10411 will work; we must treat `S<T>::R' as the name of a type.
10412 Similarly, assume that qualified names are templates, where
10413 required, so that:
10415 template <class T>
10416 int S<T>::R<T>::i = 3;
10418 will work, too. */
10419 id_expression = cp_parser_id_expression (parser,
10420 /*template_keyword_p=*/false,
10421 /*check_dependency_p=*/false,
10422 /*template_p=*/NULL);
10423 /* If the name was qualified, create a SCOPE_REF to represent
10424 that. */
10425 if (parser->scope)
10427 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10428 parser->scope = NULL_TREE;
10431 return id_expression;
10434 /* Parse a type-id.
10436 type-id:
10437 type-specifier-seq abstract-declarator [opt]
10439 Returns the TYPE specified. */
10441 static tree
10442 cp_parser_type_id (cp_parser* parser)
10444 tree type_specifier_seq;
10445 tree abstract_declarator;
10447 /* Parse the type-specifier-seq. */
10448 type_specifier_seq
10449 = cp_parser_type_specifier_seq (parser);
10450 if (type_specifier_seq == error_mark_node)
10451 return error_mark_node;
10453 /* There might or might not be an abstract declarator. */
10454 cp_parser_parse_tentatively (parser);
10455 /* Look for the declarator. */
10456 abstract_declarator
10457 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10458 /* Check to see if there really was a declarator. */
10459 if (!cp_parser_parse_definitely (parser))
10460 abstract_declarator = NULL_TREE;
10462 return groktypename (build_tree_list (type_specifier_seq,
10463 abstract_declarator));
10466 /* Parse a type-specifier-seq.
10468 type-specifier-seq:
10469 type-specifier type-specifier-seq [opt]
10471 GNU extension:
10473 type-specifier-seq:
10474 attributes type-specifier-seq [opt]
10476 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10477 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10479 static tree
10480 cp_parser_type_specifier_seq (cp_parser* parser)
10482 bool seen_type_specifier = false;
10483 tree type_specifier_seq = NULL_TREE;
10485 /* Parse the type-specifiers and attributes. */
10486 while (true)
10488 tree type_specifier;
10490 /* Check for attributes first. */
10491 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10493 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10494 NULL_TREE,
10495 type_specifier_seq);
10496 continue;
10499 /* After the first type-specifier, others are optional. */
10500 if (seen_type_specifier)
10501 cp_parser_parse_tentatively (parser);
10502 /* Look for the type-specifier. */
10503 type_specifier = cp_parser_type_specifier (parser,
10504 CP_PARSER_FLAGS_NONE,
10505 /*is_friend=*/false,
10506 /*is_declaration=*/false,
10507 NULL,
10508 NULL);
10509 /* If the first type-specifier could not be found, this is not a
10510 type-specifier-seq at all. */
10511 if (!seen_type_specifier && type_specifier == error_mark_node)
10512 return error_mark_node;
10513 /* If subsequent type-specifiers could not be found, the
10514 type-specifier-seq is complete. */
10515 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10516 break;
10518 /* Add the new type-specifier to the list. */
10519 type_specifier_seq
10520 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10521 seen_type_specifier = true;
10524 /* We built up the list in reverse order. */
10525 return nreverse (type_specifier_seq);
10528 /* Parse a parameter-declaration-clause.
10530 parameter-declaration-clause:
10531 parameter-declaration-list [opt] ... [opt]
10532 parameter-declaration-list , ...
10534 Returns a representation for the parameter declarations. Each node
10535 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10536 representation.) If the parameter-declaration-clause ends with an
10537 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10538 list. A return value of NULL_TREE indicates a
10539 parameter-declaration-clause consisting only of an ellipsis. */
10541 static tree
10542 cp_parser_parameter_declaration_clause (cp_parser* parser)
10544 tree parameters;
10545 cp_token *token;
10546 bool ellipsis_p;
10548 /* Peek at the next token. */
10549 token = cp_lexer_peek_token (parser->lexer);
10550 /* Check for trivial parameter-declaration-clauses. */
10551 if (token->type == CPP_ELLIPSIS)
10553 /* Consume the `...' token. */
10554 cp_lexer_consume_token (parser->lexer);
10555 return NULL_TREE;
10557 else if (token->type == CPP_CLOSE_PAREN)
10558 /* There are no parameters. */
10560 #ifndef NO_IMPLICIT_EXTERN_C
10561 if (in_system_header && current_class_type == NULL
10562 && current_lang_name == lang_name_c)
10563 return NULL_TREE;
10564 else
10565 #endif
10566 return void_list_node;
10568 /* Check for `(void)', too, which is a special case. */
10569 else if (token->keyword == RID_VOID
10570 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10571 == CPP_CLOSE_PAREN))
10573 /* Consume the `void' token. */
10574 cp_lexer_consume_token (parser->lexer);
10575 /* There are no parameters. */
10576 return void_list_node;
10579 /* Parse the parameter-declaration-list. */
10580 parameters = cp_parser_parameter_declaration_list (parser);
10581 /* If a parse error occurred while parsing the
10582 parameter-declaration-list, then the entire
10583 parameter-declaration-clause is erroneous. */
10584 if (parameters == error_mark_node)
10585 return error_mark_node;
10587 /* Peek at the next token. */
10588 token = cp_lexer_peek_token (parser->lexer);
10589 /* If it's a `,', the clause should terminate with an ellipsis. */
10590 if (token->type == CPP_COMMA)
10592 /* Consume the `,'. */
10593 cp_lexer_consume_token (parser->lexer);
10594 /* Expect an ellipsis. */
10595 ellipsis_p
10596 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10598 /* It might also be `...' if the optional trailing `,' was
10599 omitted. */
10600 else if (token->type == CPP_ELLIPSIS)
10602 /* Consume the `...' token. */
10603 cp_lexer_consume_token (parser->lexer);
10604 /* And remember that we saw it. */
10605 ellipsis_p = true;
10607 else
10608 ellipsis_p = false;
10610 /* Finish the parameter list. */
10611 return finish_parmlist (parameters, ellipsis_p);
10614 /* Parse a parameter-declaration-list.
10616 parameter-declaration-list:
10617 parameter-declaration
10618 parameter-declaration-list , parameter-declaration
10620 Returns a representation of the parameter-declaration-list, as for
10621 cp_parser_parameter_declaration_clause. However, the
10622 `void_list_node' is never appended to the list. */
10624 static tree
10625 cp_parser_parameter_declaration_list (cp_parser* parser)
10627 tree parameters = NULL_TREE;
10629 /* Look for more parameters. */
10630 while (true)
10632 tree parameter;
10633 /* Parse the parameter. */
10634 parameter
10635 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10637 /* If a parse error ocurred parsing the parameter declaration,
10638 then the entire parameter-declaration-list is erroneous. */
10639 if (parameter == error_mark_node)
10641 parameters = error_mark_node;
10642 break;
10644 /* Add the new parameter to the list. */
10645 TREE_CHAIN (parameter) = parameters;
10646 parameters = parameter;
10648 /* Peek at the next token. */
10649 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10650 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10651 /* The parameter-declaration-list is complete. */
10652 break;
10653 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10655 cp_token *token;
10657 /* Peek at the next token. */
10658 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10659 /* If it's an ellipsis, then the list is complete. */
10660 if (token->type == CPP_ELLIPSIS)
10661 break;
10662 /* Otherwise, there must be more parameters. Consume the
10663 `,'. */
10664 cp_lexer_consume_token (parser->lexer);
10666 else
10668 cp_parser_error (parser, "expected `,' or `...'");
10669 break;
10673 /* We built up the list in reverse order; straighten it out now. */
10674 return nreverse (parameters);
10677 /* Parse a parameter declaration.
10679 parameter-declaration:
10680 decl-specifier-seq declarator
10681 decl-specifier-seq declarator = assignment-expression
10682 decl-specifier-seq abstract-declarator [opt]
10683 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10685 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10686 declares a template parameter. (In that case, a non-nested `>'
10687 token encountered during the parsing of the assignment-expression
10688 is not interpreted as a greater-than operator.)
10690 Returns a TREE_LIST representing the parameter-declaration. The
10691 TREE_VALUE is a representation of the decl-specifier-seq and
10692 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10693 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10694 TREE_VALUE represents the declarator. */
10696 static tree
10697 cp_parser_parameter_declaration (cp_parser *parser,
10698 bool template_parm_p)
10700 bool declares_class_or_enum;
10701 bool greater_than_is_operator_p;
10702 tree decl_specifiers;
10703 tree attributes;
10704 tree declarator;
10705 tree default_argument;
10706 tree parameter;
10707 cp_token *token;
10708 const char *saved_message;
10710 /* In a template parameter, `>' is not an operator.
10712 [temp.param]
10714 When parsing a default template-argument for a non-type
10715 template-parameter, the first non-nested `>' is taken as the end
10716 of the template parameter-list rather than a greater-than
10717 operator. */
10718 greater_than_is_operator_p = !template_parm_p;
10720 /* Type definitions may not appear in parameter types. */
10721 saved_message = parser->type_definition_forbidden_message;
10722 parser->type_definition_forbidden_message
10723 = "types may not be defined in parameter types";
10725 /* Parse the declaration-specifiers. */
10726 decl_specifiers
10727 = cp_parser_decl_specifier_seq (parser,
10728 CP_PARSER_FLAGS_NONE,
10729 &attributes,
10730 &declares_class_or_enum);
10731 /* If an error occurred, there's no reason to attempt to parse the
10732 rest of the declaration. */
10733 if (cp_parser_error_occurred (parser))
10735 parser->type_definition_forbidden_message = saved_message;
10736 return error_mark_node;
10739 /* Peek at the next token. */
10740 token = cp_lexer_peek_token (parser->lexer);
10741 /* If the next token is a `)', `,', `=', `>', or `...', then there
10742 is no declarator. */
10743 if (token->type == CPP_CLOSE_PAREN
10744 || token->type == CPP_COMMA
10745 || token->type == CPP_EQ
10746 || token->type == CPP_ELLIPSIS
10747 || token->type == CPP_GREATER)
10748 declarator = NULL_TREE;
10749 /* Otherwise, there should be a declarator. */
10750 else
10752 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10753 parser->default_arg_ok_p = false;
10755 declarator = cp_parser_declarator (parser,
10756 CP_PARSER_DECLARATOR_EITHER,
10757 /*ctor_dtor_or_conv_p=*/NULL);
10758 parser->default_arg_ok_p = saved_default_arg_ok_p;
10759 /* After the declarator, allow more attributes. */
10760 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10763 /* The restriction on defining new types applies only to the type
10764 of the parameter, not to the default argument. */
10765 parser->type_definition_forbidden_message = saved_message;
10767 /* If the next token is `=', then process a default argument. */
10768 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10770 bool saved_greater_than_is_operator_p;
10771 /* Consume the `='. */
10772 cp_lexer_consume_token (parser->lexer);
10774 /* If we are defining a class, then the tokens that make up the
10775 default argument must be saved and processed later. */
10776 if (!template_parm_p && at_class_scope_p ()
10777 && TYPE_BEING_DEFINED (current_class_type))
10779 unsigned depth = 0;
10781 /* Create a DEFAULT_ARG to represented the unparsed default
10782 argument. */
10783 default_argument = make_node (DEFAULT_ARG);
10784 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10786 /* Add tokens until we have processed the entire default
10787 argument. */
10788 while (true)
10790 bool done = false;
10791 cp_token *token;
10793 /* Peek at the next token. */
10794 token = cp_lexer_peek_token (parser->lexer);
10795 /* What we do depends on what token we have. */
10796 switch (token->type)
10798 /* In valid code, a default argument must be
10799 immediately followed by a `,' `)', or `...'. */
10800 case CPP_COMMA:
10801 case CPP_CLOSE_PAREN:
10802 case CPP_ELLIPSIS:
10803 /* If we run into a non-nested `;', `}', or `]',
10804 then the code is invalid -- but the default
10805 argument is certainly over. */
10806 case CPP_SEMICOLON:
10807 case CPP_CLOSE_BRACE:
10808 case CPP_CLOSE_SQUARE:
10809 if (depth == 0)
10810 done = true;
10811 /* Update DEPTH, if necessary. */
10812 else if (token->type == CPP_CLOSE_PAREN
10813 || token->type == CPP_CLOSE_BRACE
10814 || token->type == CPP_CLOSE_SQUARE)
10815 --depth;
10816 break;
10818 case CPP_OPEN_PAREN:
10819 case CPP_OPEN_SQUARE:
10820 case CPP_OPEN_BRACE:
10821 ++depth;
10822 break;
10824 case CPP_GREATER:
10825 /* If we see a non-nested `>', and `>' is not an
10826 operator, then it marks the end of the default
10827 argument. */
10828 if (!depth && !greater_than_is_operator_p)
10829 done = true;
10830 break;
10832 /* If we run out of tokens, issue an error message. */
10833 case CPP_EOF:
10834 error ("file ends in default argument");
10835 done = true;
10836 break;
10838 case CPP_NAME:
10839 case CPP_SCOPE:
10840 /* In these cases, we should look for template-ids.
10841 For example, if the default argument is
10842 `X<int, double>()', we need to do name lookup to
10843 figure out whether or not `X' is a template; if
10844 so, the `,' does not end the deault argument.
10846 That is not yet done. */
10847 break;
10849 default:
10850 break;
10853 /* If we've reached the end, stop. */
10854 if (done)
10855 break;
10857 /* Add the token to the token block. */
10858 token = cp_lexer_consume_token (parser->lexer);
10859 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10860 token);
10863 /* Outside of a class definition, we can just parse the
10864 assignment-expression. */
10865 else
10867 bool saved_local_variables_forbidden_p;
10869 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10870 set correctly. */
10871 saved_greater_than_is_operator_p
10872 = parser->greater_than_is_operator_p;
10873 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10874 /* Local variable names (and the `this' keyword) may not
10875 appear in a default argument. */
10876 saved_local_variables_forbidden_p
10877 = parser->local_variables_forbidden_p;
10878 parser->local_variables_forbidden_p = true;
10879 /* Parse the assignment-expression. */
10880 default_argument = cp_parser_assignment_expression (parser);
10881 /* Restore saved state. */
10882 parser->greater_than_is_operator_p
10883 = saved_greater_than_is_operator_p;
10884 parser->local_variables_forbidden_p
10885 = saved_local_variables_forbidden_p;
10887 if (!parser->default_arg_ok_p)
10889 pedwarn ("default arguments are only permitted on functions");
10890 if (flag_pedantic_errors)
10891 default_argument = NULL_TREE;
10894 else
10895 default_argument = NULL_TREE;
10897 /* Create the representation of the parameter. */
10898 if (attributes)
10899 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10900 parameter = build_tree_list (default_argument,
10901 build_tree_list (decl_specifiers,
10902 declarator));
10904 return parameter;
10907 /* Parse a function-definition.
10909 function-definition:
10910 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10911 function-body
10912 decl-specifier-seq [opt] declarator function-try-block
10914 GNU Extension:
10916 function-definition:
10917 __extension__ function-definition
10919 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10920 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10921 be a `friend'. */
10923 static tree
10924 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10926 tree decl_specifiers;
10927 tree attributes;
10928 tree declarator;
10929 tree fn;
10930 cp_token *token;
10931 bool declares_class_or_enum;
10932 bool member_p;
10933 /* The saved value of the PEDANTIC flag. */
10934 int saved_pedantic;
10936 /* Any pending qualification must be cleared by our caller. It is
10937 more robust to force the callers to clear PARSER->SCOPE than to
10938 do it here since if the qualification is in effect here, it might
10939 also end up in effect elsewhere that it is not intended. */
10940 my_friendly_assert (!parser->scope, 20010821);
10942 /* Handle `__extension__'. */
10943 if (cp_parser_extension_opt (parser, &saved_pedantic))
10945 /* Parse the function-definition. */
10946 fn = cp_parser_function_definition (parser, friend_p);
10947 /* Restore the PEDANTIC flag. */
10948 pedantic = saved_pedantic;
10950 return fn;
10953 /* Check to see if this definition appears in a class-specifier. */
10954 member_p = (at_class_scope_p ()
10955 && TYPE_BEING_DEFINED (current_class_type));
10956 /* Defer access checks in the decl-specifier-seq until we know what
10957 function is being defined. There is no need to do this for the
10958 definition of member functions; we cannot be defining a member
10959 from another class. */
10960 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10962 /* Parse the decl-specifier-seq. */
10963 decl_specifiers
10964 = cp_parser_decl_specifier_seq (parser,
10965 CP_PARSER_FLAGS_OPTIONAL,
10966 &attributes,
10967 &declares_class_or_enum);
10968 /* Figure out whether this declaration is a `friend'. */
10969 if (friend_p)
10970 *friend_p = cp_parser_friend_p (decl_specifiers);
10972 /* Parse the declarator. */
10973 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10974 /*ctor_dtor_or_conv_p=*/NULL);
10976 /* Gather up any access checks that occurred. */
10977 stop_deferring_access_checks ();
10979 /* If something has already gone wrong, we may as well stop now. */
10980 if (declarator == error_mark_node)
10982 /* Skip to the end of the function, or if this wasn't anything
10983 like a function-definition, to a `;' in the hopes of finding
10984 a sensible place from which to continue parsing. */
10985 cp_parser_skip_to_end_of_block_or_statement (parser);
10986 pop_deferring_access_checks ();
10987 return error_mark_node;
10990 /* The next character should be a `{' (for a simple function
10991 definition), a `:' (for a ctor-initializer), or `try' (for a
10992 function-try block). */
10993 token = cp_lexer_peek_token (parser->lexer);
10994 if (!cp_parser_token_starts_function_definition_p (token))
10996 /* Issue the error-message. */
10997 cp_parser_error (parser, "expected function-definition");
10998 /* Skip to the next `;'. */
10999 cp_parser_skip_to_end_of_block_or_statement (parser);
11001 pop_deferring_access_checks ();
11002 return error_mark_node;
11005 /* If we are in a class scope, then we must handle
11006 function-definitions specially. In particular, we save away the
11007 tokens that make up the function body, and parse them again
11008 later, in order to handle code like:
11010 struct S {
11011 int f () { return i; }
11012 int i;
11015 Here, we cannot parse the body of `f' until after we have seen
11016 the declaration of `i'. */
11017 if (member_p)
11019 cp_token_cache *cache;
11021 /* Create the function-declaration. */
11022 fn = start_method (decl_specifiers, declarator, attributes);
11023 /* If something went badly wrong, bail out now. */
11024 if (fn == error_mark_node)
11026 /* If there's a function-body, skip it. */
11027 if (cp_parser_token_starts_function_definition_p
11028 (cp_lexer_peek_token (parser->lexer)))
11029 cp_parser_skip_to_end_of_block_or_statement (parser);
11030 pop_deferring_access_checks ();
11031 return error_mark_node;
11034 /* Create a token cache. */
11035 cache = cp_token_cache_new ();
11036 /* Save away the tokens that make up the body of the
11037 function. */
11038 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11039 /* Handle function try blocks. */
11040 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11041 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11043 /* Save away the inline definition; we will process it when the
11044 class is complete. */
11045 DECL_PENDING_INLINE_INFO (fn) = cache;
11046 DECL_PENDING_INLINE_P (fn) = 1;
11048 /* We need to know that this was defined in the class, so that
11049 friend templates are handled correctly. */
11050 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
11052 /* We're done with the inline definition. */
11053 finish_method (fn);
11055 /* Add FN to the queue of functions to be parsed later. */
11056 TREE_VALUE (parser->unparsed_functions_queues)
11057 = tree_cons (NULL_TREE, fn,
11058 TREE_VALUE (parser->unparsed_functions_queues));
11060 pop_deferring_access_checks ();
11061 return fn;
11064 /* Check that the number of template-parameter-lists is OK. */
11065 if (!cp_parser_check_declarator_template_parameters (parser,
11066 declarator))
11068 cp_parser_skip_to_end_of_block_or_statement (parser);
11069 pop_deferring_access_checks ();
11070 return error_mark_node;
11073 fn = cp_parser_function_definition_from_specifiers_and_declarator
11074 (parser, decl_specifiers, attributes, declarator);
11075 pop_deferring_access_checks ();
11076 return fn;
11079 /* Parse a function-body.
11081 function-body:
11082 compound_statement */
11084 static void
11085 cp_parser_function_body (cp_parser *parser)
11087 cp_parser_compound_statement (parser);
11090 /* Parse a ctor-initializer-opt followed by a function-body. Return
11091 true if a ctor-initializer was present. */
11093 static bool
11094 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11096 tree body;
11097 bool ctor_initializer_p;
11099 /* Begin the function body. */
11100 body = begin_function_body ();
11101 /* Parse the optional ctor-initializer. */
11102 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11103 /* Parse the function-body. */
11104 cp_parser_function_body (parser);
11105 /* Finish the function body. */
11106 finish_function_body (body);
11108 return ctor_initializer_p;
11111 /* Parse an initializer.
11113 initializer:
11114 = initializer-clause
11115 ( expression-list )
11117 Returns a expression representing the initializer. If no
11118 initializer is present, NULL_TREE is returned.
11120 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11121 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11122 set to FALSE if there is no initializer present. */
11124 static tree
11125 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init)
11127 cp_token *token;
11128 tree init;
11130 /* Peek at the next token. */
11131 token = cp_lexer_peek_token (parser->lexer);
11133 /* Let our caller know whether or not this initializer was
11134 parenthesized. */
11135 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11137 if (token->type == CPP_EQ)
11139 /* Consume the `='. */
11140 cp_lexer_consume_token (parser->lexer);
11141 /* Parse the initializer-clause. */
11142 init = cp_parser_initializer_clause (parser);
11144 else if (token->type == CPP_OPEN_PAREN)
11146 /* Consume the `('. */
11147 cp_lexer_consume_token (parser->lexer);
11148 /* Parse the expression-list. */
11149 init = cp_parser_expression_list (parser);
11150 /* Consume the `)' token. */
11151 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11152 cp_parser_skip_to_closing_parenthesis (parser);
11154 else
11156 /* Anything else is an error. */
11157 cp_parser_error (parser, "expected initializer");
11158 init = error_mark_node;
11161 return init;
11164 /* Parse an initializer-clause.
11166 initializer-clause:
11167 assignment-expression
11168 { initializer-list , [opt] }
11171 Returns an expression representing the initializer.
11173 If the `assignment-expression' production is used the value
11174 returned is simply a reprsentation for the expression.
11176 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11177 the elements of the initializer-list (or NULL_TREE, if the last
11178 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11179 NULL_TREE. There is no way to detect whether or not the optional
11180 trailing `,' was provided. */
11182 static tree
11183 cp_parser_initializer_clause (cp_parser* parser)
11185 tree initializer;
11187 /* If it is not a `{', then we are looking at an
11188 assignment-expression. */
11189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11190 initializer = cp_parser_assignment_expression (parser);
11191 else
11193 /* Consume the `{' token. */
11194 cp_lexer_consume_token (parser->lexer);
11195 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11196 initializer = make_node (CONSTRUCTOR);
11197 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11198 necessary, but check_initializer depends upon it, for
11199 now. */
11200 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11201 /* If it's not a `}', then there is a non-trivial initializer. */
11202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11204 /* Parse the initializer list. */
11205 CONSTRUCTOR_ELTS (initializer)
11206 = cp_parser_initializer_list (parser);
11207 /* A trailing `,' token is allowed. */
11208 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11209 cp_lexer_consume_token (parser->lexer);
11212 /* Now, there should be a trailing `}'. */
11213 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11216 return initializer;
11219 /* Parse an initializer-list.
11221 initializer-list:
11222 initializer-clause
11223 initializer-list , initializer-clause
11225 GNU Extension:
11227 initializer-list:
11228 identifier : initializer-clause
11229 initializer-list, identifier : initializer-clause
11231 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11232 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11233 IDENTIFIER_NODE naming the field to initialize. */
11235 static tree
11236 cp_parser_initializer_list (cp_parser* parser)
11238 tree initializers = NULL_TREE;
11240 /* Parse the rest of the list. */
11241 while (true)
11243 cp_token *token;
11244 tree identifier;
11245 tree initializer;
11247 /* If the next token is an identifier and the following one is a
11248 colon, we are looking at the GNU designated-initializer
11249 syntax. */
11250 if (cp_parser_allow_gnu_extensions_p (parser)
11251 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11252 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11254 /* Consume the identifier. */
11255 identifier = cp_lexer_consume_token (parser->lexer)->value;
11256 /* Consume the `:'. */
11257 cp_lexer_consume_token (parser->lexer);
11259 else
11260 identifier = NULL_TREE;
11262 /* Parse the initializer. */
11263 initializer = cp_parser_initializer_clause (parser);
11265 /* Add it to the list. */
11266 initializers = tree_cons (identifier, initializer, initializers);
11268 /* If the next token is not a comma, we have reached the end of
11269 the list. */
11270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11271 break;
11273 /* Peek at the next token. */
11274 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11275 /* If the next token is a `}', then we're still done. An
11276 initializer-clause can have a trailing `,' after the
11277 initializer-list and before the closing `}'. */
11278 if (token->type == CPP_CLOSE_BRACE)
11279 break;
11281 /* Consume the `,' token. */
11282 cp_lexer_consume_token (parser->lexer);
11285 /* The initializers were built up in reverse order, so we need to
11286 reverse them now. */
11287 return nreverse (initializers);
11290 /* Classes [gram.class] */
11292 /* Parse a class-name.
11294 class-name:
11295 identifier
11296 template-id
11298 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11299 to indicate that names looked up in dependent types should be
11300 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11301 keyword has been used to indicate that the name that appears next
11302 is a template. TYPE_P is true iff the next name should be treated
11303 as class-name, even if it is declared to be some other kind of name
11304 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11305 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11306 being defined in a class-head.
11308 Returns the TYPE_DECL representing the class. */
11310 static tree
11311 cp_parser_class_name (cp_parser *parser,
11312 bool typename_keyword_p,
11313 bool template_keyword_p,
11314 bool type_p,
11315 bool check_dependency_p,
11316 bool class_head_p)
11318 tree decl;
11319 tree scope;
11320 bool typename_p;
11321 cp_token *token;
11323 /* All class-names start with an identifier. */
11324 token = cp_lexer_peek_token (parser->lexer);
11325 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11327 cp_parser_error (parser, "expected class-name");
11328 return error_mark_node;
11331 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11332 to a template-id, so we save it here. */
11333 scope = parser->scope;
11334 /* Any name names a type if we're following the `typename' keyword
11335 in a qualified name where the enclosing scope is type-dependent. */
11336 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11337 && dependent_type_p (scope));
11338 /* Handle the common case (an identifier, but not a template-id)
11339 efficiently. */
11340 if (token->type == CPP_NAME
11341 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11343 tree identifier;
11345 /* Look for the identifier. */
11346 identifier = cp_parser_identifier (parser);
11347 /* If the next token isn't an identifier, we are certainly not
11348 looking at a class-name. */
11349 if (identifier == error_mark_node)
11350 decl = error_mark_node;
11351 /* If we know this is a type-name, there's no need to look it
11352 up. */
11353 else if (typename_p)
11354 decl = identifier;
11355 else
11357 /* If the next token is a `::', then the name must be a type
11358 name.
11360 [basic.lookup.qual]
11362 During the lookup for a name preceding the :: scope
11363 resolution operator, object, function, and enumerator
11364 names are ignored. */
11365 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11366 type_p = true;
11367 /* Look up the name. */
11368 decl = cp_parser_lookup_name (parser, identifier,
11369 type_p,
11370 /*is_namespace=*/false,
11371 check_dependency_p);
11374 else
11376 /* Try a template-id. */
11377 decl = cp_parser_template_id (parser, template_keyword_p,
11378 check_dependency_p);
11379 if (decl == error_mark_node)
11380 return error_mark_node;
11383 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11385 /* If this is a typename, create a TYPENAME_TYPE. */
11386 if (typename_p && decl != error_mark_node)
11387 decl = TYPE_NAME (make_typename_type (scope, decl,
11388 /*complain=*/1));
11390 /* Check to see that it is really the name of a class. */
11391 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11392 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11393 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11394 /* Situations like this:
11396 template <typename T> struct A {
11397 typename T::template X<int>::I i;
11400 are problematic. Is `T::template X<int>' a class-name? The
11401 standard does not seem to be definitive, but there is no other
11402 valid interpretation of the following `::'. Therefore, those
11403 names are considered class-names. */
11404 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11405 else if (decl == error_mark_node
11406 || TREE_CODE (decl) != TYPE_DECL
11407 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11409 cp_parser_error (parser, "expected class-name");
11410 return error_mark_node;
11413 return decl;
11416 /* Parse a class-specifier.
11418 class-specifier:
11419 class-head { member-specification [opt] }
11421 Returns the TREE_TYPE representing the class. */
11423 static tree
11424 cp_parser_class_specifier (cp_parser* parser)
11426 cp_token *token;
11427 tree type;
11428 tree attributes = NULL_TREE;
11429 int has_trailing_semicolon;
11430 bool nested_name_specifier_p;
11431 unsigned saved_num_template_parameter_lists;
11433 push_deferring_access_checks (dk_no_deferred);
11435 /* Parse the class-head. */
11436 type = cp_parser_class_head (parser,
11437 &nested_name_specifier_p);
11438 /* If the class-head was a semantic disaster, skip the entire body
11439 of the class. */
11440 if (!type)
11442 cp_parser_skip_to_end_of_block_or_statement (parser);
11443 pop_deferring_access_checks ();
11444 return error_mark_node;
11447 /* Look for the `{'. */
11448 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11450 pop_deferring_access_checks ();
11451 return error_mark_node;
11454 /* Issue an error message if type-definitions are forbidden here. */
11455 cp_parser_check_type_definition (parser);
11456 /* Remember that we are defining one more class. */
11457 ++parser->num_classes_being_defined;
11458 /* Inside the class, surrounding template-parameter-lists do not
11459 apply. */
11460 saved_num_template_parameter_lists
11461 = parser->num_template_parameter_lists;
11462 parser->num_template_parameter_lists = 0;
11464 /* Start the class. */
11465 type = begin_class_definition (type);
11466 if (type == error_mark_node)
11467 /* If the type is erroneous, skip the entire body of the class. */
11468 cp_parser_skip_to_closing_brace (parser);
11469 else
11470 /* Parse the member-specification. */
11471 cp_parser_member_specification_opt (parser);
11472 /* Look for the trailing `}'. */
11473 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11474 /* We get better error messages by noticing a common problem: a
11475 missing trailing `;'. */
11476 token = cp_lexer_peek_token (parser->lexer);
11477 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11478 /* Look for attributes to apply to this class. */
11479 if (cp_parser_allow_gnu_extensions_p (parser))
11480 attributes = cp_parser_attributes_opt (parser);
11481 /* Finish the class definition. */
11482 type = finish_class_definition (type,
11483 attributes,
11484 has_trailing_semicolon,
11485 nested_name_specifier_p);
11486 /* If this class is not itself within the scope of another class,
11487 then we need to parse the bodies of all of the queued function
11488 definitions. Note that the queued functions defined in a class
11489 are not always processed immediately following the
11490 class-specifier for that class. Consider:
11492 struct A {
11493 struct B { void f() { sizeof (A); } };
11496 If `f' were processed before the processing of `A' were
11497 completed, there would be no way to compute the size of `A'.
11498 Note that the nesting we are interested in here is lexical --
11499 not the semantic nesting given by TYPE_CONTEXT. In particular,
11500 for:
11502 struct A { struct B; };
11503 struct A::B { void f() { } };
11505 there is no need to delay the parsing of `A::B::f'. */
11506 if (--parser->num_classes_being_defined == 0)
11508 tree last_scope = NULL_TREE;
11509 tree queue_entry;
11510 tree fn;
11512 /* Reverse the queue, so that we process it in the order the
11513 functions were declared. */
11514 TREE_VALUE (parser->unparsed_functions_queues)
11515 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11516 /* In a first pass, parse default arguments to the functions.
11517 Then, in a second pass, parse the bodies of the functions.
11518 This two-phased approach handles cases like:
11520 struct S {
11521 void f() { g(); }
11522 void g(int i = 3);
11526 for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11527 queue_entry;
11528 queue_entry = TREE_CHAIN (queue_entry))
11530 fn = TREE_VALUE (queue_entry);
11531 if (DECL_FUNCTION_TEMPLATE_P (fn))
11532 fn = DECL_TEMPLATE_RESULT (fn);
11533 /* Make sure that any template parameters are in scope. */
11534 maybe_begin_member_template_processing (fn);
11535 /* If there are default arguments that have not yet been processed,
11536 take care of them now. */
11537 cp_parser_late_parsing_default_args (parser, fn);
11538 /* Remove any template parameters from the symbol table. */
11539 maybe_end_member_template_processing ();
11541 /* Now parse the body of the functions. */
11542 while (TREE_VALUE (parser->unparsed_functions_queues))
11545 /* Figure out which function we need to process. */
11546 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11547 fn = TREE_VALUE (queue_entry);
11549 /* Parse the function. */
11550 cp_parser_late_parsing_for_member (parser, fn);
11552 TREE_VALUE (parser->unparsed_functions_queues)
11553 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11556 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11557 more time than we have popped, so me must pop here. */
11558 if (last_scope)
11559 pop_scope (last_scope);
11562 /* Put back any saved access checks. */
11563 pop_deferring_access_checks ();
11565 /* Restore the count of active template-parameter-lists. */
11566 parser->num_template_parameter_lists
11567 = saved_num_template_parameter_lists;
11569 return type;
11572 /* Parse a class-head.
11574 class-head:
11575 class-key identifier [opt] base-clause [opt]
11576 class-key nested-name-specifier identifier base-clause [opt]
11577 class-key nested-name-specifier [opt] template-id
11578 base-clause [opt]
11580 GNU Extensions:
11581 class-key attributes identifier [opt] base-clause [opt]
11582 class-key attributes nested-name-specifier identifier base-clause [opt]
11583 class-key attributes nested-name-specifier [opt] template-id
11584 base-clause [opt]
11586 Returns the TYPE of the indicated class. Sets
11587 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11588 involving a nested-name-specifier was used, and FALSE otherwise.
11590 Returns NULL_TREE if the class-head is syntactically valid, but
11591 semantically invalid in a way that means we should skip the entire
11592 body of the class. */
11594 static tree
11595 cp_parser_class_head (cp_parser* parser,
11596 bool* nested_name_specifier_p)
11598 cp_token *token;
11599 tree nested_name_specifier;
11600 enum tag_types class_key;
11601 tree id = NULL_TREE;
11602 tree type = NULL_TREE;
11603 tree attributes;
11604 bool template_id_p = false;
11605 bool qualified_p = false;
11606 bool invalid_nested_name_p = false;
11607 unsigned num_templates;
11609 /* Assume no nested-name-specifier will be present. */
11610 *nested_name_specifier_p = false;
11611 /* Assume no template parameter lists will be used in defining the
11612 type. */
11613 num_templates = 0;
11615 /* Look for the class-key. */
11616 class_key = cp_parser_class_key (parser);
11617 if (class_key == none_type)
11618 return error_mark_node;
11620 /* Parse the attributes. */
11621 attributes = cp_parser_attributes_opt (parser);
11623 /* If the next token is `::', that is invalid -- but sometimes
11624 people do try to write:
11626 struct ::S {};
11628 Handle this gracefully by accepting the extra qualifier, and then
11629 issuing an error about it later if this really is a
11630 class-head. If it turns out just to be an elaborated type
11631 specifier, remain silent. */
11632 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11633 qualified_p = true;
11635 push_deferring_access_checks (dk_no_check);
11637 /* Determine the name of the class. Begin by looking for an
11638 optional nested-name-specifier. */
11639 nested_name_specifier
11640 = cp_parser_nested_name_specifier_opt (parser,
11641 /*typename_keyword_p=*/false,
11642 /*check_dependency_p=*/false,
11643 /*type_p=*/false);
11644 /* If there was a nested-name-specifier, then there *must* be an
11645 identifier. */
11646 if (nested_name_specifier)
11648 /* Although the grammar says `identifier', it really means
11649 `class-name' or `template-name'. You are only allowed to
11650 define a class that has already been declared with this
11651 syntax.
11653 The proposed resolution for Core Issue 180 says that whever
11654 you see `class T::X' you should treat `X' as a type-name.
11656 It is OK to define an inaccessible class; for example:
11658 class A { class B; };
11659 class A::B {};
11661 We do not know if we will see a class-name, or a
11662 template-name. We look for a class-name first, in case the
11663 class-name is a template-id; if we looked for the
11664 template-name first we would stop after the template-name. */
11665 cp_parser_parse_tentatively (parser);
11666 type = cp_parser_class_name (parser,
11667 /*typename_keyword_p=*/false,
11668 /*template_keyword_p=*/false,
11669 /*type_p=*/true,
11670 /*check_dependency_p=*/false,
11671 /*class_head_p=*/true);
11672 /* If that didn't work, ignore the nested-name-specifier. */
11673 if (!cp_parser_parse_definitely (parser))
11675 invalid_nested_name_p = true;
11676 id = cp_parser_identifier (parser);
11677 if (id == error_mark_node)
11678 id = NULL_TREE;
11680 /* If we could not find a corresponding TYPE, treat this
11681 declaration like an unqualified declaration. */
11682 if (type == error_mark_node)
11683 nested_name_specifier = NULL_TREE;
11684 /* Otherwise, count the number of templates used in TYPE and its
11685 containing scopes. */
11686 else
11688 tree scope;
11690 for (scope = TREE_TYPE (type);
11691 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11692 scope = (TYPE_P (scope)
11693 ? TYPE_CONTEXT (scope)
11694 : DECL_CONTEXT (scope)))
11695 if (TYPE_P (scope)
11696 && CLASS_TYPE_P (scope)
11697 && CLASSTYPE_TEMPLATE_INFO (scope)
11698 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11699 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11700 ++num_templates;
11703 /* Otherwise, the identifier is optional. */
11704 else
11706 /* We don't know whether what comes next is a template-id,
11707 an identifier, or nothing at all. */
11708 cp_parser_parse_tentatively (parser);
11709 /* Check for a template-id. */
11710 id = cp_parser_template_id (parser,
11711 /*template_keyword_p=*/false,
11712 /*check_dependency_p=*/true);
11713 /* If that didn't work, it could still be an identifier. */
11714 if (!cp_parser_parse_definitely (parser))
11716 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11717 id = cp_parser_identifier (parser);
11718 else
11719 id = NULL_TREE;
11721 else
11723 template_id_p = true;
11724 ++num_templates;
11728 pop_deferring_access_checks ();
11730 /* If it's not a `:' or a `{' then we can't really be looking at a
11731 class-head, since a class-head only appears as part of a
11732 class-specifier. We have to detect this situation before calling
11733 xref_tag, since that has irreversible side-effects. */
11734 if (!cp_parser_next_token_starts_class_definition_p (parser))
11736 cp_parser_error (parser, "expected `{' or `:'");
11737 return error_mark_node;
11740 /* At this point, we're going ahead with the class-specifier, even
11741 if some other problem occurs. */
11742 cp_parser_commit_to_tentative_parse (parser);
11743 /* Issue the error about the overly-qualified name now. */
11744 if (qualified_p)
11745 cp_parser_error (parser,
11746 "global qualification of class name is invalid");
11747 else if (invalid_nested_name_p)
11748 cp_parser_error (parser,
11749 "qualified name does not name a class");
11750 /* Make sure that the right number of template parameters were
11751 present. */
11752 if (!cp_parser_check_template_parameters (parser, num_templates))
11753 /* If something went wrong, there is no point in even trying to
11754 process the class-definition. */
11755 return NULL_TREE;
11757 /* Look up the type. */
11758 if (template_id_p)
11760 type = TREE_TYPE (id);
11761 maybe_process_partial_specialization (type);
11763 else if (!nested_name_specifier)
11765 /* If the class was unnamed, create a dummy name. */
11766 if (!id)
11767 id = make_anon_name ();
11768 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11770 else
11772 tree class_type;
11773 tree scope;
11775 /* Given:
11777 template <typename T> struct S { struct T };
11778 template <typename T> struct S<T>::T { };
11780 we will get a TYPENAME_TYPE when processing the definition of
11781 `S::T'. We need to resolve it to the actual type before we
11782 try to define it. */
11783 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11785 class_type = resolve_typename_type (TREE_TYPE (type),
11786 /*only_current_p=*/false);
11787 if (class_type != error_mark_node)
11788 type = TYPE_NAME (class_type);
11789 else
11791 cp_parser_error (parser, "could not resolve typename type");
11792 type = error_mark_node;
11796 /* Figure out in what scope the declaration is being placed. */
11797 scope = current_scope ();
11798 if (!scope)
11799 scope = current_namespace;
11800 /* If that scope does not contain the scope in which the
11801 class was originally declared, the program is invalid. */
11802 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11804 error ("declaration of `%D' in `%D' which does not "
11805 "enclose `%D'", type, scope, nested_name_specifier);
11806 return NULL_TREE;
11809 maybe_process_partial_specialization (TREE_TYPE (type));
11810 class_type = current_class_type;
11811 type = TREE_TYPE (handle_class_head (class_key,
11812 nested_name_specifier,
11813 type,
11814 attributes));
11815 if (type != error_mark_node)
11817 if (!class_type && TYPE_CONTEXT (type))
11818 *nested_name_specifier_p = true;
11819 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11820 class_type))
11821 *nested_name_specifier_p = true;
11824 /* Indicate whether this class was declared as a `class' or as a
11825 `struct'. */
11826 if (TREE_CODE (type) == RECORD_TYPE)
11827 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11828 cp_parser_check_class_key (class_key, type);
11830 /* Enter the scope containing the class; the names of base classes
11831 should be looked up in that context. For example, given:
11833 struct A { struct B {}; struct C; };
11834 struct A::C : B {};
11836 is valid. */
11837 if (nested_name_specifier)
11838 push_scope (nested_name_specifier);
11839 /* Now, look for the base-clause. */
11840 token = cp_lexer_peek_token (parser->lexer);
11841 if (token->type == CPP_COLON)
11843 tree bases;
11845 /* Get the list of base-classes. */
11846 bases = cp_parser_base_clause (parser);
11847 /* Process them. */
11848 xref_basetypes (type, bases);
11850 /* Leave the scope given by the nested-name-specifier. We will
11851 enter the class scope itself while processing the members. */
11852 if (nested_name_specifier)
11853 pop_scope (nested_name_specifier);
11855 return type;
11858 /* Parse a class-key.
11860 class-key:
11861 class
11862 struct
11863 union
11865 Returns the kind of class-key specified, or none_type to indicate
11866 error. */
11868 static enum tag_types
11869 cp_parser_class_key (cp_parser* parser)
11871 cp_token *token;
11872 enum tag_types tag_type;
11874 /* Look for the class-key. */
11875 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11876 if (!token)
11877 return none_type;
11879 /* Check to see if the TOKEN is a class-key. */
11880 tag_type = cp_parser_token_is_class_key (token);
11881 if (!tag_type)
11882 cp_parser_error (parser, "expected class-key");
11883 return tag_type;
11886 /* Parse an (optional) member-specification.
11888 member-specification:
11889 member-declaration member-specification [opt]
11890 access-specifier : member-specification [opt] */
11892 static void
11893 cp_parser_member_specification_opt (cp_parser* parser)
11895 while (true)
11897 cp_token *token;
11898 enum rid keyword;
11900 /* Peek at the next token. */
11901 token = cp_lexer_peek_token (parser->lexer);
11902 /* If it's a `}', or EOF then we've seen all the members. */
11903 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11904 break;
11906 /* See if this token is a keyword. */
11907 keyword = token->keyword;
11908 switch (keyword)
11910 case RID_PUBLIC:
11911 case RID_PROTECTED:
11912 case RID_PRIVATE:
11913 /* Consume the access-specifier. */
11914 cp_lexer_consume_token (parser->lexer);
11915 /* Remember which access-specifier is active. */
11916 current_access_specifier = token->value;
11917 /* Look for the `:'. */
11918 cp_parser_require (parser, CPP_COLON, "`:'");
11919 break;
11921 default:
11922 /* Otherwise, the next construction must be a
11923 member-declaration. */
11924 cp_parser_member_declaration (parser);
11929 /* Parse a member-declaration.
11931 member-declaration:
11932 decl-specifier-seq [opt] member-declarator-list [opt] ;
11933 function-definition ; [opt]
11934 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11935 using-declaration
11936 template-declaration
11938 member-declarator-list:
11939 member-declarator
11940 member-declarator-list , member-declarator
11942 member-declarator:
11943 declarator pure-specifier [opt]
11944 declarator constant-initializer [opt]
11945 identifier [opt] : constant-expression
11947 GNU Extensions:
11949 member-declaration:
11950 __extension__ member-declaration
11952 member-declarator:
11953 declarator attributes [opt] pure-specifier [opt]
11954 declarator attributes [opt] constant-initializer [opt]
11955 identifier [opt] attributes [opt] : constant-expression */
11957 static void
11958 cp_parser_member_declaration (cp_parser* parser)
11960 tree decl_specifiers;
11961 tree prefix_attributes;
11962 tree decl;
11963 bool declares_class_or_enum;
11964 bool friend_p;
11965 cp_token *token;
11966 int saved_pedantic;
11968 /* Check for the `__extension__' keyword. */
11969 if (cp_parser_extension_opt (parser, &saved_pedantic))
11971 /* Recurse. */
11972 cp_parser_member_declaration (parser);
11973 /* Restore the old value of the PEDANTIC flag. */
11974 pedantic = saved_pedantic;
11976 return;
11979 /* Check for a template-declaration. */
11980 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11982 /* Parse the template-declaration. */
11983 cp_parser_template_declaration (parser, /*member_p=*/true);
11985 return;
11988 /* Check for a using-declaration. */
11989 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11991 /* Parse the using-declaration. */
11992 cp_parser_using_declaration (parser);
11994 return;
11997 /* We can't tell whether we're looking at a declaration or a
11998 function-definition. */
11999 cp_parser_parse_tentatively (parser);
12001 /* Parse the decl-specifier-seq. */
12002 decl_specifiers
12003 = cp_parser_decl_specifier_seq (parser,
12004 CP_PARSER_FLAGS_OPTIONAL,
12005 &prefix_attributes,
12006 &declares_class_or_enum);
12007 /* Check for an invalid type-name. */
12008 if (cp_parser_diagnose_invalid_type_name (parser))
12009 return;
12010 /* If there is no declarator, then the decl-specifier-seq should
12011 specify a type. */
12012 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12014 /* If there was no decl-specifier-seq, and the next token is a
12015 `;', then we have something like:
12017 struct S { ; };
12019 [class.mem]
12021 Each member-declaration shall declare at least one member
12022 name of the class. */
12023 if (!decl_specifiers)
12025 if (pedantic)
12026 pedwarn ("extra semicolon");
12028 else
12030 tree type;
12032 /* See if this declaration is a friend. */
12033 friend_p = cp_parser_friend_p (decl_specifiers);
12034 /* If there were decl-specifiers, check to see if there was
12035 a class-declaration. */
12036 type = check_tag_decl (decl_specifiers);
12037 /* Nested classes have already been added to the class, but
12038 a `friend' needs to be explicitly registered. */
12039 if (friend_p)
12041 /* If the `friend' keyword was present, the friend must
12042 be introduced with a class-key. */
12043 if (!declares_class_or_enum)
12044 error ("a class-key must be used when declaring a friend");
12045 /* In this case:
12047 template <typename T> struct A {
12048 friend struct A<T>::B;
12051 A<T>::B will be represented by a TYPENAME_TYPE, and
12052 therefore not recognized by check_tag_decl. */
12053 if (!type)
12055 tree specifier;
12057 for (specifier = decl_specifiers;
12058 specifier;
12059 specifier = TREE_CHAIN (specifier))
12061 tree s = TREE_VALUE (specifier);
12063 if (TREE_CODE (s) == IDENTIFIER_NODE
12064 && IDENTIFIER_GLOBAL_VALUE (s))
12065 type = IDENTIFIER_GLOBAL_VALUE (s);
12066 if (TREE_CODE (s) == TYPE_DECL)
12067 s = TREE_TYPE (s);
12068 if (TYPE_P (s))
12070 type = s;
12071 break;
12075 if (!type)
12076 error ("friend declaration does not name a class or "
12077 "function");
12078 else
12079 make_friend_class (current_class_type, type);
12081 /* If there is no TYPE, an error message will already have
12082 been issued. */
12083 else if (!type)
12085 /* An anonymous aggregate has to be handled specially; such
12086 a declaration really declares a data member (with a
12087 particular type), as opposed to a nested class. */
12088 else if (ANON_AGGR_TYPE_P (type))
12090 /* Remove constructors and such from TYPE, now that we
12091 know it is an anoymous aggregate. */
12092 fixup_anonymous_aggr (type);
12093 /* And make the corresponding data member. */
12094 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12095 /* Add it to the class. */
12096 finish_member_declaration (decl);
12100 else
12102 /* See if these declarations will be friends. */
12103 friend_p = cp_parser_friend_p (decl_specifiers);
12105 /* Keep going until we hit the `;' at the end of the
12106 declaration. */
12107 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12109 tree attributes = NULL_TREE;
12110 tree first_attribute;
12112 /* Peek at the next token. */
12113 token = cp_lexer_peek_token (parser->lexer);
12115 /* Check for a bitfield declaration. */
12116 if (token->type == CPP_COLON
12117 || (token->type == CPP_NAME
12118 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12119 == CPP_COLON))
12121 tree identifier;
12122 tree width;
12124 /* Get the name of the bitfield. Note that we cannot just
12125 check TOKEN here because it may have been invalidated by
12126 the call to cp_lexer_peek_nth_token above. */
12127 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12128 identifier = cp_parser_identifier (parser);
12129 else
12130 identifier = NULL_TREE;
12132 /* Consume the `:' token. */
12133 cp_lexer_consume_token (parser->lexer);
12134 /* Get the width of the bitfield. */
12135 width
12136 = cp_parser_constant_expression (parser,
12137 /*allow_non_constant=*/false,
12138 NULL);
12140 /* Look for attributes that apply to the bitfield. */
12141 attributes = cp_parser_attributes_opt (parser);
12142 /* Remember which attributes are prefix attributes and
12143 which are not. */
12144 first_attribute = attributes;
12145 /* Combine the attributes. */
12146 attributes = chainon (prefix_attributes, attributes);
12148 /* Create the bitfield declaration. */
12149 decl = grokbitfield (identifier,
12150 decl_specifiers,
12151 width);
12152 /* Apply the attributes. */
12153 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12155 else
12157 tree declarator;
12158 tree initializer;
12159 tree asm_specification;
12160 bool ctor_dtor_or_conv_p;
12162 /* Parse the declarator. */
12163 declarator
12164 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12165 &ctor_dtor_or_conv_p);
12167 /* If something went wrong parsing the declarator, make sure
12168 that we at least consume some tokens. */
12169 if (declarator == error_mark_node)
12171 /* Skip to the end of the statement. */
12172 cp_parser_skip_to_end_of_statement (parser);
12173 break;
12176 /* Look for an asm-specification. */
12177 asm_specification = cp_parser_asm_specification_opt (parser);
12178 /* Look for attributes that apply to the declaration. */
12179 attributes = cp_parser_attributes_opt (parser);
12180 /* Remember which attributes are prefix attributes and
12181 which are not. */
12182 first_attribute = attributes;
12183 /* Combine the attributes. */
12184 attributes = chainon (prefix_attributes, attributes);
12186 /* If it's an `=', then we have a constant-initializer or a
12187 pure-specifier. It is not correct to parse the
12188 initializer before registering the member declaration
12189 since the member declaration should be in scope while
12190 its initializer is processed. However, the rest of the
12191 front end does not yet provide an interface that allows
12192 us to handle this correctly. */
12193 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12195 /* In [class.mem]:
12197 A pure-specifier shall be used only in the declaration of
12198 a virtual function.
12200 A member-declarator can contain a constant-initializer
12201 only if it declares a static member of integral or
12202 enumeration type.
12204 Therefore, if the DECLARATOR is for a function, we look
12205 for a pure-specifier; otherwise, we look for a
12206 constant-initializer. When we call `grokfield', it will
12207 perform more stringent semantics checks. */
12208 if (TREE_CODE (declarator) == CALL_EXPR)
12209 initializer = cp_parser_pure_specifier (parser);
12210 else
12212 /* This declaration cannot be a function
12213 definition. */
12214 cp_parser_commit_to_tentative_parse (parser);
12215 /* Parse the initializer. */
12216 initializer = cp_parser_constant_initializer (parser);
12219 /* Otherwise, there is no initializer. */
12220 else
12221 initializer = NULL_TREE;
12223 /* See if we are probably looking at a function
12224 definition. We are certainly not looking at at a
12225 member-declarator. Calling `grokfield' has
12226 side-effects, so we must not do it unless we are sure
12227 that we are looking at a member-declarator. */
12228 if (cp_parser_token_starts_function_definition_p
12229 (cp_lexer_peek_token (parser->lexer)))
12230 decl = error_mark_node;
12231 else
12232 /* Create the declaration. */
12233 decl = grokfield (declarator,
12234 decl_specifiers,
12235 initializer,
12236 asm_specification,
12237 attributes);
12240 /* Reset PREFIX_ATTRIBUTES. */
12241 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12242 attributes = TREE_CHAIN (attributes);
12243 if (attributes)
12244 TREE_CHAIN (attributes) = NULL_TREE;
12246 /* If there is any qualification still in effect, clear it
12247 now; we will be starting fresh with the next declarator. */
12248 parser->scope = NULL_TREE;
12249 parser->qualifying_scope = NULL_TREE;
12250 parser->object_scope = NULL_TREE;
12251 /* If it's a `,', then there are more declarators. */
12252 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12253 cp_lexer_consume_token (parser->lexer);
12254 /* If the next token isn't a `;', then we have a parse error. */
12255 else if (cp_lexer_next_token_is_not (parser->lexer,
12256 CPP_SEMICOLON))
12258 cp_parser_error (parser, "expected `;'");
12259 /* Skip tokens until we find a `;' */
12260 cp_parser_skip_to_end_of_statement (parser);
12262 break;
12265 if (decl)
12267 /* Add DECL to the list of members. */
12268 if (!friend_p)
12269 finish_member_declaration (decl);
12271 /* If DECL is a function, we must return
12272 to parse it later. (Even though there is no definition,
12273 there might be default arguments that need handling.) */
12274 if (TREE_CODE (decl) == FUNCTION_DECL)
12275 TREE_VALUE (parser->unparsed_functions_queues)
12276 = tree_cons (NULL_TREE, decl,
12277 TREE_VALUE (parser->unparsed_functions_queues));
12282 /* If everything went well, look for the `;'. */
12283 if (cp_parser_parse_definitely (parser))
12285 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12286 return;
12289 /* Parse the function-definition. */
12290 decl = cp_parser_function_definition (parser, &friend_p);
12291 /* If the member was not a friend, declare it here. */
12292 if (!friend_p)
12293 finish_member_declaration (decl);
12294 /* Peek at the next token. */
12295 token = cp_lexer_peek_token (parser->lexer);
12296 /* If the next token is a semicolon, consume it. */
12297 if (token->type == CPP_SEMICOLON)
12298 cp_lexer_consume_token (parser->lexer);
12301 /* Parse a pure-specifier.
12303 pure-specifier:
12306 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12307 Otherwiser, ERROR_MARK_NODE is returned. */
12309 static tree
12310 cp_parser_pure_specifier (cp_parser* parser)
12312 cp_token *token;
12314 /* Look for the `=' token. */
12315 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12316 return error_mark_node;
12317 /* Look for the `0' token. */
12318 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12319 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12320 to get information from the lexer about how the number was
12321 spelled in order to fix this problem. */
12322 if (!token || !integer_zerop (token->value))
12323 return error_mark_node;
12325 return integer_zero_node;
12328 /* Parse a constant-initializer.
12330 constant-initializer:
12331 = constant-expression
12333 Returns a representation of the constant-expression. */
12335 static tree
12336 cp_parser_constant_initializer (cp_parser* parser)
12338 /* Look for the `=' token. */
12339 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12340 return error_mark_node;
12342 /* It is invalid to write:
12344 struct S { static const int i = { 7 }; };
12347 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12349 cp_parser_error (parser,
12350 "a brace-enclosed initializer is not allowed here");
12351 /* Consume the opening brace. */
12352 cp_lexer_consume_token (parser->lexer);
12353 /* Skip the initializer. */
12354 cp_parser_skip_to_closing_brace (parser);
12355 /* Look for the trailing `}'. */
12356 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12358 return error_mark_node;
12361 return cp_parser_constant_expression (parser,
12362 /*allow_non_constant=*/false,
12363 NULL);
12366 /* Derived classes [gram.class.derived] */
12368 /* Parse a base-clause.
12370 base-clause:
12371 : base-specifier-list
12373 base-specifier-list:
12374 base-specifier
12375 base-specifier-list , base-specifier
12377 Returns a TREE_LIST representing the base-classes, in the order in
12378 which they were declared. The representation of each node is as
12379 described by cp_parser_base_specifier.
12381 In the case that no bases are specified, this function will return
12382 NULL_TREE, not ERROR_MARK_NODE. */
12384 static tree
12385 cp_parser_base_clause (cp_parser* parser)
12387 tree bases = NULL_TREE;
12389 /* Look for the `:' that begins the list. */
12390 cp_parser_require (parser, CPP_COLON, "`:'");
12392 /* Scan the base-specifier-list. */
12393 while (true)
12395 cp_token *token;
12396 tree base;
12398 /* Look for the base-specifier. */
12399 base = cp_parser_base_specifier (parser);
12400 /* Add BASE to the front of the list. */
12401 if (base != error_mark_node)
12403 TREE_CHAIN (base) = bases;
12404 bases = base;
12406 /* Peek at the next token. */
12407 token = cp_lexer_peek_token (parser->lexer);
12408 /* If it's not a comma, then the list is complete. */
12409 if (token->type != CPP_COMMA)
12410 break;
12411 /* Consume the `,'. */
12412 cp_lexer_consume_token (parser->lexer);
12415 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12416 base class had a qualified name. However, the next name that
12417 appears is certainly not qualified. */
12418 parser->scope = NULL_TREE;
12419 parser->qualifying_scope = NULL_TREE;
12420 parser->object_scope = NULL_TREE;
12422 return nreverse (bases);
12425 /* Parse a base-specifier.
12427 base-specifier:
12428 :: [opt] nested-name-specifier [opt] class-name
12429 virtual access-specifier [opt] :: [opt] nested-name-specifier
12430 [opt] class-name
12431 access-specifier virtual [opt] :: [opt] nested-name-specifier
12432 [opt] class-name
12434 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12435 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12436 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12437 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12439 static tree
12440 cp_parser_base_specifier (cp_parser* parser)
12442 cp_token *token;
12443 bool done = false;
12444 bool virtual_p = false;
12445 bool duplicate_virtual_error_issued_p = false;
12446 bool duplicate_access_error_issued_p = false;
12447 bool class_scope_p, template_p;
12448 tree access = access_default_node;
12449 tree type;
12451 /* Process the optional `virtual' and `access-specifier'. */
12452 while (!done)
12454 /* Peek at the next token. */
12455 token = cp_lexer_peek_token (parser->lexer);
12456 /* Process `virtual'. */
12457 switch (token->keyword)
12459 case RID_VIRTUAL:
12460 /* If `virtual' appears more than once, issue an error. */
12461 if (virtual_p && !duplicate_virtual_error_issued_p)
12463 cp_parser_error (parser,
12464 "`virtual' specified more than once in base-specified");
12465 duplicate_virtual_error_issued_p = true;
12468 virtual_p = true;
12470 /* Consume the `virtual' token. */
12471 cp_lexer_consume_token (parser->lexer);
12473 break;
12475 case RID_PUBLIC:
12476 case RID_PROTECTED:
12477 case RID_PRIVATE:
12478 /* If more than one access specifier appears, issue an
12479 error. */
12480 if (access != access_default_node
12481 && !duplicate_access_error_issued_p)
12483 cp_parser_error (parser,
12484 "more than one access specifier in base-specified");
12485 duplicate_access_error_issued_p = true;
12488 access = ridpointers[(int) token->keyword];
12490 /* Consume the access-specifier. */
12491 cp_lexer_consume_token (parser->lexer);
12493 break;
12495 default:
12496 done = true;
12497 break;
12501 /* Look for the optional `::' operator. */
12502 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12503 /* Look for the nested-name-specifier. The simplest way to
12504 implement:
12506 [temp.res]
12508 The keyword `typename' is not permitted in a base-specifier or
12509 mem-initializer; in these contexts a qualified name that
12510 depends on a template-parameter is implicitly assumed to be a
12511 type name.
12513 is to pretend that we have seen the `typename' keyword at this
12514 point. */
12515 cp_parser_nested_name_specifier_opt (parser,
12516 /*typename_keyword_p=*/true,
12517 /*check_dependency_p=*/true,
12518 /*type_p=*/true);
12519 /* If the base class is given by a qualified name, assume that names
12520 we see are type names or templates, as appropriate. */
12521 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12522 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12524 /* Finally, look for the class-name. */
12525 type = cp_parser_class_name (parser,
12526 class_scope_p,
12527 template_p,
12528 /*type_p=*/true,
12529 /*check_dependency_p=*/true,
12530 /*class_head_p=*/false);
12532 if (type == error_mark_node)
12533 return error_mark_node;
12535 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12538 /* Exception handling [gram.exception] */
12540 /* Parse an (optional) exception-specification.
12542 exception-specification:
12543 throw ( type-id-list [opt] )
12545 Returns a TREE_LIST representing the exception-specification. The
12546 TREE_VALUE of each node is a type. */
12548 static tree
12549 cp_parser_exception_specification_opt (cp_parser* parser)
12551 cp_token *token;
12552 tree type_id_list;
12554 /* Peek at the next token. */
12555 token = cp_lexer_peek_token (parser->lexer);
12556 /* If it's not `throw', then there's no exception-specification. */
12557 if (!cp_parser_is_keyword (token, RID_THROW))
12558 return NULL_TREE;
12560 /* Consume the `throw'. */
12561 cp_lexer_consume_token (parser->lexer);
12563 /* Look for the `('. */
12564 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12566 /* Peek at the next token. */
12567 token = cp_lexer_peek_token (parser->lexer);
12568 /* If it's not a `)', then there is a type-id-list. */
12569 if (token->type != CPP_CLOSE_PAREN)
12571 const char *saved_message;
12573 /* Types may not be defined in an exception-specification. */
12574 saved_message = parser->type_definition_forbidden_message;
12575 parser->type_definition_forbidden_message
12576 = "types may not be defined in an exception-specification";
12577 /* Parse the type-id-list. */
12578 type_id_list = cp_parser_type_id_list (parser);
12579 /* Restore the saved message. */
12580 parser->type_definition_forbidden_message = saved_message;
12582 else
12583 type_id_list = empty_except_spec;
12585 /* Look for the `)'. */
12586 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12588 return type_id_list;
12591 /* Parse an (optional) type-id-list.
12593 type-id-list:
12594 type-id
12595 type-id-list , type-id
12597 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12598 in the order that the types were presented. */
12600 static tree
12601 cp_parser_type_id_list (cp_parser* parser)
12603 tree types = NULL_TREE;
12605 while (true)
12607 cp_token *token;
12608 tree type;
12610 /* Get the next type-id. */
12611 type = cp_parser_type_id (parser);
12612 /* Add it to the list. */
12613 types = add_exception_specifier (types, type, /*complain=*/1);
12614 /* Peek at the next token. */
12615 token = cp_lexer_peek_token (parser->lexer);
12616 /* If it is not a `,', we are done. */
12617 if (token->type != CPP_COMMA)
12618 break;
12619 /* Consume the `,'. */
12620 cp_lexer_consume_token (parser->lexer);
12623 return nreverse (types);
12626 /* Parse a try-block.
12628 try-block:
12629 try compound-statement handler-seq */
12631 static tree
12632 cp_parser_try_block (cp_parser* parser)
12634 tree try_block;
12636 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12637 try_block = begin_try_block ();
12638 cp_parser_compound_statement (parser);
12639 finish_try_block (try_block);
12640 cp_parser_handler_seq (parser);
12641 finish_handler_sequence (try_block);
12643 return try_block;
12646 /* Parse a function-try-block.
12648 function-try-block:
12649 try ctor-initializer [opt] function-body handler-seq */
12651 static bool
12652 cp_parser_function_try_block (cp_parser* parser)
12654 tree try_block;
12655 bool ctor_initializer_p;
12657 /* Look for the `try' keyword. */
12658 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12659 return false;
12660 /* Let the rest of the front-end know where we are. */
12661 try_block = begin_function_try_block ();
12662 /* Parse the function-body. */
12663 ctor_initializer_p
12664 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12665 /* We're done with the `try' part. */
12666 finish_function_try_block (try_block);
12667 /* Parse the handlers. */
12668 cp_parser_handler_seq (parser);
12669 /* We're done with the handlers. */
12670 finish_function_handler_sequence (try_block);
12672 return ctor_initializer_p;
12675 /* Parse a handler-seq.
12677 handler-seq:
12678 handler handler-seq [opt] */
12680 static void
12681 cp_parser_handler_seq (cp_parser* parser)
12683 while (true)
12685 cp_token *token;
12687 /* Parse the handler. */
12688 cp_parser_handler (parser);
12689 /* Peek at the next token. */
12690 token = cp_lexer_peek_token (parser->lexer);
12691 /* If it's not `catch' then there are no more handlers. */
12692 if (!cp_parser_is_keyword (token, RID_CATCH))
12693 break;
12697 /* Parse a handler.
12699 handler:
12700 catch ( exception-declaration ) compound-statement */
12702 static void
12703 cp_parser_handler (cp_parser* parser)
12705 tree handler;
12706 tree declaration;
12708 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12709 handler = begin_handler ();
12710 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12711 declaration = cp_parser_exception_declaration (parser);
12712 finish_handler_parms (declaration, handler);
12713 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12714 cp_parser_compound_statement (parser);
12715 finish_handler (handler);
12718 /* Parse an exception-declaration.
12720 exception-declaration:
12721 type-specifier-seq declarator
12722 type-specifier-seq abstract-declarator
12723 type-specifier-seq
12724 ...
12726 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12727 ellipsis variant is used. */
12729 static tree
12730 cp_parser_exception_declaration (cp_parser* parser)
12732 tree type_specifiers;
12733 tree declarator;
12734 const char *saved_message;
12736 /* If it's an ellipsis, it's easy to handle. */
12737 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12739 /* Consume the `...' token. */
12740 cp_lexer_consume_token (parser->lexer);
12741 return NULL_TREE;
12744 /* Types may not be defined in exception-declarations. */
12745 saved_message = parser->type_definition_forbidden_message;
12746 parser->type_definition_forbidden_message
12747 = "types may not be defined in exception-declarations";
12749 /* Parse the type-specifier-seq. */
12750 type_specifiers = cp_parser_type_specifier_seq (parser);
12751 /* If it's a `)', then there is no declarator. */
12752 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12753 declarator = NULL_TREE;
12754 else
12755 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12756 /*ctor_dtor_or_conv_p=*/NULL);
12758 /* Restore the saved message. */
12759 parser->type_definition_forbidden_message = saved_message;
12761 return start_handler_parms (type_specifiers, declarator);
12764 /* Parse a throw-expression.
12766 throw-expression:
12767 throw assignment-expresion [opt]
12769 Returns a THROW_EXPR representing the throw-expression. */
12771 static tree
12772 cp_parser_throw_expression (cp_parser* parser)
12774 tree expression;
12776 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12777 /* We can't be sure if there is an assignment-expression or not. */
12778 cp_parser_parse_tentatively (parser);
12779 /* Try it. */
12780 expression = cp_parser_assignment_expression (parser);
12781 /* If it didn't work, this is just a rethrow. */
12782 if (!cp_parser_parse_definitely (parser))
12783 expression = NULL_TREE;
12785 return build_throw (expression);
12788 /* GNU Extensions */
12790 /* Parse an (optional) asm-specification.
12792 asm-specification:
12793 asm ( string-literal )
12795 If the asm-specification is present, returns a STRING_CST
12796 corresponding to the string-literal. Otherwise, returns
12797 NULL_TREE. */
12799 static tree
12800 cp_parser_asm_specification_opt (cp_parser* parser)
12802 cp_token *token;
12803 tree asm_specification;
12805 /* Peek at the next token. */
12806 token = cp_lexer_peek_token (parser->lexer);
12807 /* If the next token isn't the `asm' keyword, then there's no
12808 asm-specification. */
12809 if (!cp_parser_is_keyword (token, RID_ASM))
12810 return NULL_TREE;
12812 /* Consume the `asm' token. */
12813 cp_lexer_consume_token (parser->lexer);
12814 /* Look for the `('. */
12815 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12817 /* Look for the string-literal. */
12818 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12819 if (token)
12820 asm_specification = token->value;
12821 else
12822 asm_specification = NULL_TREE;
12824 /* Look for the `)'. */
12825 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12827 return asm_specification;
12830 /* Parse an asm-operand-list.
12832 asm-operand-list:
12833 asm-operand
12834 asm-operand-list , asm-operand
12836 asm-operand:
12837 string-literal ( expression )
12838 [ string-literal ] string-literal ( expression )
12840 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12841 each node is the expression. The TREE_PURPOSE is itself a
12842 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12843 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12844 is a STRING_CST for the string literal before the parenthesis. */
12846 static tree
12847 cp_parser_asm_operand_list (cp_parser* parser)
12849 tree asm_operands = NULL_TREE;
12851 while (true)
12853 tree string_literal;
12854 tree expression;
12855 tree name;
12856 cp_token *token;
12858 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12860 /* Consume the `[' token. */
12861 cp_lexer_consume_token (parser->lexer);
12862 /* Read the operand name. */
12863 name = cp_parser_identifier (parser);
12864 if (name != error_mark_node)
12865 name = build_string (IDENTIFIER_LENGTH (name),
12866 IDENTIFIER_POINTER (name));
12867 /* Look for the closing `]'. */
12868 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12870 else
12871 name = NULL_TREE;
12872 /* Look for the string-literal. */
12873 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12874 string_literal = token ? token->value : error_mark_node;
12875 /* Look for the `('. */
12876 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12877 /* Parse the expression. */
12878 expression = cp_parser_expression (parser);
12879 /* Look for the `)'. */
12880 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12881 /* Add this operand to the list. */
12882 asm_operands = tree_cons (build_tree_list (name, string_literal),
12883 expression,
12884 asm_operands);
12885 /* If the next token is not a `,', there are no more
12886 operands. */
12887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12888 break;
12889 /* Consume the `,'. */
12890 cp_lexer_consume_token (parser->lexer);
12893 return nreverse (asm_operands);
12896 /* Parse an asm-clobber-list.
12898 asm-clobber-list:
12899 string-literal
12900 asm-clobber-list , string-literal
12902 Returns a TREE_LIST, indicating the clobbers in the order that they
12903 appeared. The TREE_VALUE of each node is a STRING_CST. */
12905 static tree
12906 cp_parser_asm_clobber_list (cp_parser* parser)
12908 tree clobbers = NULL_TREE;
12910 while (true)
12912 cp_token *token;
12913 tree string_literal;
12915 /* Look for the string literal. */
12916 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12917 string_literal = token ? token->value : error_mark_node;
12918 /* Add it to the list. */
12919 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12920 /* If the next token is not a `,', then the list is
12921 complete. */
12922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12923 break;
12924 /* Consume the `,' token. */
12925 cp_lexer_consume_token (parser->lexer);
12928 return clobbers;
12931 /* Parse an (optional) series of attributes.
12933 attributes:
12934 attributes attribute
12936 attribute:
12937 __attribute__ (( attribute-list [opt] ))
12939 The return value is as for cp_parser_attribute_list. */
12941 static tree
12942 cp_parser_attributes_opt (cp_parser* parser)
12944 tree attributes = NULL_TREE;
12946 while (true)
12948 cp_token *token;
12949 tree attribute_list;
12951 /* Peek at the next token. */
12952 token = cp_lexer_peek_token (parser->lexer);
12953 /* If it's not `__attribute__', then we're done. */
12954 if (token->keyword != RID_ATTRIBUTE)
12955 break;
12957 /* Consume the `__attribute__' keyword. */
12958 cp_lexer_consume_token (parser->lexer);
12959 /* Look for the two `(' tokens. */
12960 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12961 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12963 /* Peek at the next token. */
12964 token = cp_lexer_peek_token (parser->lexer);
12965 if (token->type != CPP_CLOSE_PAREN)
12966 /* Parse the attribute-list. */
12967 attribute_list = cp_parser_attribute_list (parser);
12968 else
12969 /* If the next token is a `)', then there is no attribute
12970 list. */
12971 attribute_list = NULL;
12973 /* Look for the two `)' tokens. */
12974 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12975 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12977 /* Add these new attributes to the list. */
12978 attributes = chainon (attributes, attribute_list);
12981 return attributes;
12984 /* Parse an attribute-list.
12986 attribute-list:
12987 attribute
12988 attribute-list , attribute
12990 attribute:
12991 identifier
12992 identifier ( identifier )
12993 identifier ( identifier , expression-list )
12994 identifier ( expression-list )
12996 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12997 TREE_PURPOSE of each node is the identifier indicating which
12998 attribute is in use. The TREE_VALUE represents the arguments, if
12999 any. */
13001 static tree
13002 cp_parser_attribute_list (cp_parser* parser)
13004 tree attribute_list = NULL_TREE;
13006 while (true)
13008 cp_token *token;
13009 tree identifier;
13010 tree attribute;
13012 /* Look for the identifier. We also allow keywords here; for
13013 example `__attribute__ ((const))' is legal. */
13014 token = cp_lexer_peek_token (parser->lexer);
13015 if (token->type != CPP_NAME
13016 && token->type != CPP_KEYWORD)
13017 return error_mark_node;
13018 /* Consume the token. */
13019 token = cp_lexer_consume_token (parser->lexer);
13021 /* Save away the identifier that indicates which attribute this is. */
13022 identifier = token->value;
13023 attribute = build_tree_list (identifier, NULL_TREE);
13025 /* Peek at the next token. */
13026 token = cp_lexer_peek_token (parser->lexer);
13027 /* If it's an `(', then parse the attribute arguments. */
13028 if (token->type == CPP_OPEN_PAREN)
13030 tree arguments;
13031 int arguments_allowed_p = 1;
13033 /* Consume the `('. */
13034 cp_lexer_consume_token (parser->lexer);
13035 /* Peek at the next token. */
13036 token = cp_lexer_peek_token (parser->lexer);
13037 /* Check to see if the next token is an identifier. */
13038 if (token->type == CPP_NAME)
13040 /* Save the identifier. */
13041 identifier = token->value;
13042 /* Consume the identifier. */
13043 cp_lexer_consume_token (parser->lexer);
13044 /* Peek at the next token. */
13045 token = cp_lexer_peek_token (parser->lexer);
13046 /* If the next token is a `,', then there are some other
13047 expressions as well. */
13048 if (token->type == CPP_COMMA)
13049 /* Consume the comma. */
13050 cp_lexer_consume_token (parser->lexer);
13051 else
13052 arguments_allowed_p = 0;
13054 else
13055 identifier = NULL_TREE;
13057 /* If there are arguments, parse them too. */
13058 if (arguments_allowed_p)
13059 arguments = cp_parser_expression_list (parser);
13060 else
13061 arguments = NULL_TREE;
13063 /* Combine the identifier and the arguments. */
13064 if (identifier)
13065 arguments = tree_cons (NULL_TREE, identifier, arguments);
13067 /* Save the identifier and arguments away. */
13068 TREE_VALUE (attribute) = arguments;
13070 /* Look for the closing `)'. */
13071 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13074 /* Add this attribute to the list. */
13075 TREE_CHAIN (attribute) = attribute_list;
13076 attribute_list = attribute;
13078 /* Now, look for more attributes. */
13079 token = cp_lexer_peek_token (parser->lexer);
13080 /* If the next token isn't a `,', we're done. */
13081 if (token->type != CPP_COMMA)
13082 break;
13084 /* Consume the commma and keep going. */
13085 cp_lexer_consume_token (parser->lexer);
13088 /* We built up the list in reverse order. */
13089 return nreverse (attribute_list);
13092 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13093 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13094 current value of the PEDANTIC flag, regardless of whether or not
13095 the `__extension__' keyword is present. The caller is responsible
13096 for restoring the value of the PEDANTIC flag. */
13098 static bool
13099 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13101 /* Save the old value of the PEDANTIC flag. */
13102 *saved_pedantic = pedantic;
13104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13106 /* Consume the `__extension__' token. */
13107 cp_lexer_consume_token (parser->lexer);
13108 /* We're not being pedantic while the `__extension__' keyword is
13109 in effect. */
13110 pedantic = 0;
13112 return true;
13115 return false;
13118 /* Parse a label declaration.
13120 label-declaration:
13121 __label__ label-declarator-seq ;
13123 label-declarator-seq:
13124 identifier , label-declarator-seq
13125 identifier */
13127 static void
13128 cp_parser_label_declaration (cp_parser* parser)
13130 /* Look for the `__label__' keyword. */
13131 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13133 while (true)
13135 tree identifier;
13137 /* Look for an identifier. */
13138 identifier = cp_parser_identifier (parser);
13139 /* Declare it as a lobel. */
13140 finish_label_decl (identifier);
13141 /* If the next token is a `;', stop. */
13142 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13143 break;
13144 /* Look for the `,' separating the label declarations. */
13145 cp_parser_require (parser, CPP_COMMA, "`,'");
13148 /* Look for the final `;'. */
13149 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13152 /* Support Functions */
13154 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13155 NAME should have one of the representations used for an
13156 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13157 is returned. If PARSER->SCOPE is a dependent type, then a
13158 SCOPE_REF is returned.
13160 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13161 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13162 was formed. Abstractly, such entities should not be passed to this
13163 function, because they do not need to be looked up, but it is
13164 simpler to check for this special case here, rather than at the
13165 call-sites.
13167 In cases not explicitly covered above, this function returns a
13168 DECL, OVERLOAD, or baselink representing the result of the lookup.
13169 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13170 is returned.
13172 If IS_TYPE is TRUE, bindings that do not refer to types are
13173 ignored.
13175 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13176 are ignored.
13178 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13179 types. */
13181 static tree
13182 cp_parser_lookup_name (cp_parser *parser, tree name,
13183 bool is_type, bool is_namespace, bool check_dependency)
13185 tree decl;
13186 tree object_type = parser->context->object_type;
13188 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13189 no longer valid. Note that if we are parsing tentatively, and
13190 the parse fails, OBJECT_TYPE will be automatically restored. */
13191 parser->context->object_type = NULL_TREE;
13193 if (name == error_mark_node)
13194 return error_mark_node;
13196 /* A template-id has already been resolved; there is no lookup to
13197 do. */
13198 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13199 return name;
13200 if (BASELINK_P (name))
13202 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13203 == TEMPLATE_ID_EXPR),
13204 20020909);
13205 return name;
13208 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13209 it should already have been checked to make sure that the name
13210 used matches the type being destroyed. */
13211 if (TREE_CODE (name) == BIT_NOT_EXPR)
13213 tree type;
13215 /* Figure out to which type this destructor applies. */
13216 if (parser->scope)
13217 type = parser->scope;
13218 else if (object_type)
13219 type = object_type;
13220 else
13221 type = current_class_type;
13222 /* If that's not a class type, there is no destructor. */
13223 if (!type || !CLASS_TYPE_P (type))
13224 return error_mark_node;
13225 /* If it was a class type, return the destructor. */
13226 return CLASSTYPE_DESTRUCTORS (type);
13229 /* By this point, the NAME should be an ordinary identifier. If
13230 the id-expression was a qualified name, the qualifying scope is
13231 stored in PARSER->SCOPE at this point. */
13232 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13233 20000619);
13235 /* Perform the lookup. */
13236 if (parser->scope)
13238 bool dependent_p;
13240 if (parser->scope == error_mark_node)
13241 return error_mark_node;
13243 /* If the SCOPE is dependent, the lookup must be deferred until
13244 the template is instantiated -- unless we are explicitly
13245 looking up names in uninstantiated templates. Even then, we
13246 cannot look up the name if the scope is not a class type; it
13247 might, for example, be a template type parameter. */
13248 dependent_p = (TYPE_P (parser->scope)
13249 && !(parser->in_declarator_p
13250 && currently_open_class (parser->scope))
13251 && dependent_type_p (parser->scope));
13252 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13253 && dependent_p)
13255 if (!is_type)
13256 decl = build_nt (SCOPE_REF, parser->scope, name);
13257 else
13258 /* The resolution to Core Issue 180 says that `struct A::B'
13259 should be considered a type-name, even if `A' is
13260 dependent. */
13261 decl = TYPE_NAME (make_typename_type (parser->scope,
13262 name,
13263 /*complain=*/1));
13265 else
13267 /* If PARSER->SCOPE is a dependent type, then it must be a
13268 class type, and we must not be checking dependencies;
13269 otherwise, we would have processed this lookup above. So
13270 that PARSER->SCOPE is not considered a dependent base by
13271 lookup_member, we must enter the scope here. */
13272 if (dependent_p)
13273 push_scope (parser->scope);
13274 /* If the PARSER->SCOPE is a a template specialization, it
13275 may be instantiated during name lookup. In that case,
13276 errors may be issued. Even if we rollback the current
13277 tentative parse, those errors are valid. */
13278 decl = lookup_qualified_name (parser->scope, name, is_type,
13279 /*flags=*/0);
13280 if (dependent_p)
13281 pop_scope (parser->scope);
13283 parser->qualifying_scope = parser->scope;
13284 parser->object_scope = NULL_TREE;
13286 else if (object_type)
13288 tree object_decl = NULL_TREE;
13289 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13290 OBJECT_TYPE is not a class. */
13291 if (CLASS_TYPE_P (object_type))
13292 /* If the OBJECT_TYPE is a template specialization, it may
13293 be instantiated during name lookup. In that case, errors
13294 may be issued. Even if we rollback the current tentative
13295 parse, those errors are valid. */
13296 object_decl = lookup_member (object_type,
13297 name,
13298 /*protect=*/0, is_type);
13299 /* Look it up in the enclosing context, too. */
13300 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13301 is_namespace,
13302 /*flags=*/0);
13303 parser->object_scope = object_type;
13304 parser->qualifying_scope = NULL_TREE;
13305 if (object_decl)
13306 decl = object_decl;
13308 else
13310 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13311 is_namespace,
13312 /*flags=*/0);
13313 parser->qualifying_scope = NULL_TREE;
13314 parser->object_scope = NULL_TREE;
13317 /* If the lookup failed, let our caller know. */
13318 if (!decl
13319 || decl == error_mark_node
13320 || (TREE_CODE (decl) == FUNCTION_DECL
13321 && DECL_ANTICIPATED (decl)))
13322 return error_mark_node;
13324 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13325 if (TREE_CODE (decl) == TREE_LIST)
13327 /* The error message we have to print is too complicated for
13328 cp_parser_error, so we incorporate its actions directly. */
13329 if (!cp_parser_simulate_error (parser))
13331 error ("reference to `%D' is ambiguous", name);
13332 print_candidates (decl);
13334 return error_mark_node;
13337 my_friendly_assert (DECL_P (decl)
13338 || TREE_CODE (decl) == OVERLOAD
13339 || TREE_CODE (decl) == SCOPE_REF
13340 || BASELINK_P (decl),
13341 20000619);
13343 /* If we have resolved the name of a member declaration, check to
13344 see if the declaration is accessible. When the name resolves to
13345 set of overloaded functions, accesibility is checked when
13346 overload resolution is done.
13348 During an explicit instantiation, access is not checked at all,
13349 as per [temp.explicit]. */
13350 if (DECL_P (decl))
13352 tree qualifying_type;
13354 /* Figure out the type through which DECL is being
13355 accessed. */
13356 qualifying_type
13357 = cp_parser_scope_through_which_access_occurs (decl,
13358 object_type,
13359 parser->scope);
13360 if (qualifying_type)
13361 perform_or_defer_access_check (TYPE_BINFO (qualifying_type), decl);
13364 return decl;
13367 /* Like cp_parser_lookup_name, but for use in the typical case where
13368 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13369 TRUE. */
13371 static tree
13372 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13374 return cp_parser_lookup_name (parser, name,
13375 /*is_type=*/false,
13376 /*is_namespace=*/false,
13377 /*check_dependency=*/true);
13380 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13381 the current context, return the TYPE_DECL. If TAG_NAME_P is
13382 true, the DECL indicates the class being defined in a class-head,
13383 or declared in an elaborated-type-specifier.
13385 Otherwise, return DECL. */
13387 static tree
13388 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13390 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13391 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13393 struct A {
13394 template <typename T> struct B;
13397 template <typename T> struct A::B {};
13399 Similarly, in a elaborated-type-specifier:
13401 namespace N { struct X{}; }
13403 struct A {
13404 template <typename T> friend struct N::X;
13407 However, if the DECL refers to a class type, and we are in
13408 the scope of the class, then the name lookup automatically
13409 finds the TYPE_DECL created by build_self_reference rather
13410 than a TEMPLATE_DECL. For example, in:
13412 template <class T> struct S {
13413 S s;
13416 there is no need to handle such case. */
13418 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13419 return DECL_TEMPLATE_RESULT (decl);
13421 return decl;
13424 /* If too many, or too few, template-parameter lists apply to the
13425 declarator, issue an error message. Returns TRUE if all went well,
13426 and FALSE otherwise. */
13428 static bool
13429 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13430 tree declarator)
13432 unsigned num_templates;
13434 /* We haven't seen any classes that involve template parameters yet. */
13435 num_templates = 0;
13437 switch (TREE_CODE (declarator))
13439 case CALL_EXPR:
13440 case ARRAY_REF:
13441 case INDIRECT_REF:
13442 case ADDR_EXPR:
13444 tree main_declarator = TREE_OPERAND (declarator, 0);
13445 return
13446 cp_parser_check_declarator_template_parameters (parser,
13447 main_declarator);
13450 case SCOPE_REF:
13452 tree scope;
13453 tree member;
13455 scope = TREE_OPERAND (declarator, 0);
13456 member = TREE_OPERAND (declarator, 1);
13458 /* If this is a pointer-to-member, then we are not interested
13459 in the SCOPE, because it does not qualify the thing that is
13460 being declared. */
13461 if (TREE_CODE (member) == INDIRECT_REF)
13462 return (cp_parser_check_declarator_template_parameters
13463 (parser, member));
13465 while (scope && CLASS_TYPE_P (scope))
13467 /* You're supposed to have one `template <...>'
13468 for every template class, but you don't need one
13469 for a full specialization. For example:
13471 template <class T> struct S{};
13472 template <> struct S<int> { void f(); };
13473 void S<int>::f () {}
13475 is correct; there shouldn't be a `template <>' for
13476 the definition of `S<int>::f'. */
13477 if (CLASSTYPE_TEMPLATE_INFO (scope)
13478 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13479 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13480 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13481 ++num_templates;
13483 scope = TYPE_CONTEXT (scope);
13487 /* Fall through. */
13489 default:
13490 /* If the DECLARATOR has the form `X<y>' then it uses one
13491 additional level of template parameters. */
13492 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13493 ++num_templates;
13495 return cp_parser_check_template_parameters (parser,
13496 num_templates);
13500 /* NUM_TEMPLATES were used in the current declaration. If that is
13501 invalid, return FALSE and issue an error messages. Otherwise,
13502 return TRUE. */
13504 static bool
13505 cp_parser_check_template_parameters (cp_parser* parser,
13506 unsigned num_templates)
13508 /* If there are more template classes than parameter lists, we have
13509 something like:
13511 template <class T> void S<T>::R<T>::f (); */
13512 if (parser->num_template_parameter_lists < num_templates)
13514 error ("too few template-parameter-lists");
13515 return false;
13517 /* If there are the same number of template classes and parameter
13518 lists, that's OK. */
13519 if (parser->num_template_parameter_lists == num_templates)
13520 return true;
13521 /* If there are more, but only one more, then we are referring to a
13522 member template. That's OK too. */
13523 if (parser->num_template_parameter_lists == num_templates + 1)
13524 return true;
13525 /* Otherwise, there are too many template parameter lists. We have
13526 something like:
13528 template <class T> template <class U> void S::f(); */
13529 error ("too many template-parameter-lists");
13530 return false;
13533 /* Parse a binary-expression of the general form:
13535 binary-expression:
13536 <expr>
13537 binary-expression <token> <expr>
13539 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13540 to parser the <expr>s. If the first production is used, then the
13541 value returned by FN is returned directly. Otherwise, a node with
13542 the indicated EXPR_TYPE is returned, with operands corresponding to
13543 the two sub-expressions. */
13545 static tree
13546 cp_parser_binary_expression (cp_parser* parser,
13547 const cp_parser_token_tree_map token_tree_map,
13548 cp_parser_expression_fn fn)
13550 tree lhs;
13552 /* Parse the first expression. */
13553 lhs = (*fn) (parser);
13554 /* Now, look for more expressions. */
13555 while (true)
13557 cp_token *token;
13558 const cp_parser_token_tree_map_node *map_node;
13559 tree rhs;
13561 /* Peek at the next token. */
13562 token = cp_lexer_peek_token (parser->lexer);
13563 /* If the token is `>', and that's not an operator at the
13564 moment, then we're done. */
13565 if (token->type == CPP_GREATER
13566 && !parser->greater_than_is_operator_p)
13567 break;
13568 /* If we find one of the tokens we want, build the correspoding
13569 tree representation. */
13570 for (map_node = token_tree_map;
13571 map_node->token_type != CPP_EOF;
13572 ++map_node)
13573 if (map_node->token_type == token->type)
13575 /* Consume the operator token. */
13576 cp_lexer_consume_token (parser->lexer);
13577 /* Parse the right-hand side of the expression. */
13578 rhs = (*fn) (parser);
13579 /* Build the binary tree node. */
13580 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13581 break;
13584 /* If the token wasn't one of the ones we want, we're done. */
13585 if (map_node->token_type == CPP_EOF)
13586 break;
13589 return lhs;
13592 /* Parse an optional `::' token indicating that the following name is
13593 from the global namespace. If so, PARSER->SCOPE is set to the
13594 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13595 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13596 Returns the new value of PARSER->SCOPE, if the `::' token is
13597 present, and NULL_TREE otherwise. */
13599 static tree
13600 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13602 cp_token *token;
13604 /* Peek at the next token. */
13605 token = cp_lexer_peek_token (parser->lexer);
13606 /* If we're looking at a `::' token then we're starting from the
13607 global namespace, not our current location. */
13608 if (token->type == CPP_SCOPE)
13610 /* Consume the `::' token. */
13611 cp_lexer_consume_token (parser->lexer);
13612 /* Set the SCOPE so that we know where to start the lookup. */
13613 parser->scope = global_namespace;
13614 parser->qualifying_scope = global_namespace;
13615 parser->object_scope = NULL_TREE;
13617 return parser->scope;
13619 else if (!current_scope_valid_p)
13621 parser->scope = NULL_TREE;
13622 parser->qualifying_scope = NULL_TREE;
13623 parser->object_scope = NULL_TREE;
13626 return NULL_TREE;
13629 /* Returns TRUE if the upcoming token sequence is the start of a
13630 constructor declarator. If FRIEND_P is true, the declarator is
13631 preceded by the `friend' specifier. */
13633 static bool
13634 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13636 bool constructor_p;
13637 tree type_decl = NULL_TREE;
13638 bool nested_name_p;
13639 cp_token *next_token;
13641 /* The common case is that this is not a constructor declarator, so
13642 try to avoid doing lots of work if at all possible. It's not
13643 valid declare a constructor at function scope. */
13644 if (at_function_scope_p ())
13645 return false;
13646 /* And only certain tokens can begin a constructor declarator. */
13647 next_token = cp_lexer_peek_token (parser->lexer);
13648 if (next_token->type != CPP_NAME
13649 && next_token->type != CPP_SCOPE
13650 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13651 && next_token->type != CPP_TEMPLATE_ID)
13652 return false;
13654 /* Parse tentatively; we are going to roll back all of the tokens
13655 consumed here. */
13656 cp_parser_parse_tentatively (parser);
13657 /* Assume that we are looking at a constructor declarator. */
13658 constructor_p = true;
13660 /* Look for the optional `::' operator. */
13661 cp_parser_global_scope_opt (parser,
13662 /*current_scope_valid_p=*/false);
13663 /* Look for the nested-name-specifier. */
13664 nested_name_p
13665 = (cp_parser_nested_name_specifier_opt (parser,
13666 /*typename_keyword_p=*/false,
13667 /*check_dependency_p=*/false,
13668 /*type_p=*/false)
13669 != NULL_TREE);
13670 /* Outside of a class-specifier, there must be a
13671 nested-name-specifier. */
13672 if (!nested_name_p &&
13673 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13674 || friend_p))
13675 constructor_p = false;
13676 /* If we still think that this might be a constructor-declarator,
13677 look for a class-name. */
13678 if (constructor_p)
13680 /* If we have:
13682 template <typename T> struct S { S(); };
13683 template <typename T> S<T>::S ();
13685 we must recognize that the nested `S' names a class.
13686 Similarly, for:
13688 template <typename T> S<T>::S<T> ();
13690 we must recognize that the nested `S' names a template. */
13691 type_decl = cp_parser_class_name (parser,
13692 /*typename_keyword_p=*/false,
13693 /*template_keyword_p=*/false,
13694 /*type_p=*/false,
13695 /*check_dependency_p=*/false,
13696 /*class_head_p=*/false);
13697 /* If there was no class-name, then this is not a constructor. */
13698 constructor_p = !cp_parser_error_occurred (parser);
13701 /* If we're still considering a constructor, we have to see a `(',
13702 to begin the parameter-declaration-clause, followed by either a
13703 `)', an `...', or a decl-specifier. We need to check for a
13704 type-specifier to avoid being fooled into thinking that:
13706 S::S (f) (int);
13708 is a constructor. (It is actually a function named `f' that
13709 takes one parameter (of type `int') and returns a value of type
13710 `S::S'. */
13711 if (constructor_p
13712 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13715 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13716 && !cp_parser_storage_class_specifier_opt (parser))
13718 tree type;
13720 /* Names appearing in the type-specifier should be looked up
13721 in the scope of the class. */
13722 if (current_class_type)
13723 type = NULL_TREE;
13724 else
13726 type = TREE_TYPE (type_decl);
13727 if (TREE_CODE (type) == TYPENAME_TYPE)
13729 type = resolve_typename_type (type,
13730 /*only_current_p=*/false);
13731 if (type == error_mark_node)
13733 cp_parser_abort_tentative_parse (parser);
13734 return false;
13737 push_scope (type);
13739 /* Look for the type-specifier. */
13740 cp_parser_type_specifier (parser,
13741 CP_PARSER_FLAGS_NONE,
13742 /*is_friend=*/false,
13743 /*is_declarator=*/true,
13744 /*declares_class_or_enum=*/NULL,
13745 /*is_cv_qualifier=*/NULL);
13746 /* Leave the scope of the class. */
13747 if (type)
13748 pop_scope (type);
13750 constructor_p = !cp_parser_error_occurred (parser);
13753 else
13754 constructor_p = false;
13755 /* We did not really want to consume any tokens. */
13756 cp_parser_abort_tentative_parse (parser);
13758 return constructor_p;
13761 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13762 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13763 they must be performed once we are in the scope of the function.
13765 Returns the function defined. */
13767 static tree
13768 cp_parser_function_definition_from_specifiers_and_declarator
13769 (cp_parser* parser,
13770 tree decl_specifiers,
13771 tree attributes,
13772 tree declarator)
13774 tree fn;
13775 bool success_p;
13777 /* Begin the function-definition. */
13778 success_p = begin_function_definition (decl_specifiers,
13779 attributes,
13780 declarator);
13782 /* If there were names looked up in the decl-specifier-seq that we
13783 did not check, check them now. We must wait until we are in the
13784 scope of the function to perform the checks, since the function
13785 might be a friend. */
13786 perform_deferred_access_checks ();
13788 if (!success_p)
13790 /* If begin_function_definition didn't like the definition, skip
13791 the entire function. */
13792 error ("invalid function declaration");
13793 cp_parser_skip_to_end_of_block_or_statement (parser);
13794 fn = error_mark_node;
13796 else
13797 fn = cp_parser_function_definition_after_declarator (parser,
13798 /*inline_p=*/false);
13800 return fn;
13803 /* Parse the part of a function-definition that follows the
13804 declarator. INLINE_P is TRUE iff this function is an inline
13805 function defined with a class-specifier.
13807 Returns the function defined. */
13809 static tree
13810 cp_parser_function_definition_after_declarator (cp_parser* parser,
13811 bool inline_p)
13813 tree fn;
13814 bool ctor_initializer_p = false;
13815 bool saved_in_unbraced_linkage_specification_p;
13816 unsigned saved_num_template_parameter_lists;
13818 /* If the next token is `return', then the code may be trying to
13819 make use of the "named return value" extension that G++ used to
13820 support. */
13821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13823 /* Consume the `return' keyword. */
13824 cp_lexer_consume_token (parser->lexer);
13825 /* Look for the identifier that indicates what value is to be
13826 returned. */
13827 cp_parser_identifier (parser);
13828 /* Issue an error message. */
13829 error ("named return values are no longer supported");
13830 /* Skip tokens until we reach the start of the function body. */
13831 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13832 cp_lexer_consume_token (parser->lexer);
13834 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13835 anything declared inside `f'. */
13836 saved_in_unbraced_linkage_specification_p
13837 = parser->in_unbraced_linkage_specification_p;
13838 parser->in_unbraced_linkage_specification_p = false;
13839 /* Inside the function, surrounding template-parameter-lists do not
13840 apply. */
13841 saved_num_template_parameter_lists
13842 = parser->num_template_parameter_lists;
13843 parser->num_template_parameter_lists = 0;
13844 /* If the next token is `try', then we are looking at a
13845 function-try-block. */
13846 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13847 ctor_initializer_p = cp_parser_function_try_block (parser);
13848 /* A function-try-block includes the function-body, so we only do
13849 this next part if we're not processing a function-try-block. */
13850 else
13851 ctor_initializer_p
13852 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13854 /* Finish the function. */
13855 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13856 (inline_p ? 2 : 0));
13857 /* Generate code for it, if necessary. */
13858 expand_or_defer_fn (fn);
13859 /* Restore the saved values. */
13860 parser->in_unbraced_linkage_specification_p
13861 = saved_in_unbraced_linkage_specification_p;
13862 parser->num_template_parameter_lists
13863 = saved_num_template_parameter_lists;
13865 return fn;
13868 /* Parse a template-declaration, assuming that the `export' (and
13869 `extern') keywords, if present, has already been scanned. MEMBER_P
13870 is as for cp_parser_template_declaration. */
13872 static void
13873 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13875 tree decl = NULL_TREE;
13876 tree parameter_list;
13877 bool friend_p = false;
13879 /* Look for the `template' keyword. */
13880 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13881 return;
13883 /* And the `<'. */
13884 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13885 return;
13887 /* Parse the template parameters. */
13888 begin_template_parm_list ();
13889 /* If the next token is `>', then we have an invalid
13890 specialization. Rather than complain about an invalid template
13891 parameter, issue an error message here. */
13892 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13894 cp_parser_error (parser, "invalid explicit specialization");
13895 parameter_list = NULL_TREE;
13897 else
13898 parameter_list = cp_parser_template_parameter_list (parser);
13899 parameter_list = end_template_parm_list (parameter_list);
13900 /* Look for the `>'. */
13901 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13902 /* We just processed one more parameter list. */
13903 ++parser->num_template_parameter_lists;
13904 /* If the next token is `template', there are more template
13905 parameters. */
13906 if (cp_lexer_next_token_is_keyword (parser->lexer,
13907 RID_TEMPLATE))
13908 cp_parser_template_declaration_after_export (parser, member_p);
13909 else
13911 decl = cp_parser_single_declaration (parser,
13912 member_p,
13913 &friend_p);
13915 /* If this is a member template declaration, let the front
13916 end know. */
13917 if (member_p && !friend_p && decl)
13918 decl = finish_member_template_decl (decl);
13919 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13920 make_friend_class (current_class_type, TREE_TYPE (decl));
13922 /* We are done with the current parameter list. */
13923 --parser->num_template_parameter_lists;
13925 /* Finish up. */
13926 finish_template_decl (parameter_list);
13928 /* Register member declarations. */
13929 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13930 finish_member_declaration (decl);
13932 /* If DECL is a function template, we must return to parse it later.
13933 (Even though there is no definition, there might be default
13934 arguments that need handling.) */
13935 if (member_p && decl
13936 && (TREE_CODE (decl) == FUNCTION_DECL
13937 || DECL_FUNCTION_TEMPLATE_P (decl)))
13938 TREE_VALUE (parser->unparsed_functions_queues)
13939 = tree_cons (NULL_TREE, decl,
13940 TREE_VALUE (parser->unparsed_functions_queues));
13943 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13944 `function-definition' sequence. MEMBER_P is true, this declaration
13945 appears in a class scope.
13947 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13948 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13950 static tree
13951 cp_parser_single_declaration (cp_parser* parser,
13952 bool member_p,
13953 bool* friend_p)
13955 bool declares_class_or_enum;
13956 tree decl = NULL_TREE;
13957 tree decl_specifiers;
13958 tree attributes;
13960 /* Parse the dependent declaration. We don't know yet
13961 whether it will be a function-definition. */
13962 cp_parser_parse_tentatively (parser);
13963 /* Defer access checks until we know what is being declared. */
13964 push_deferring_access_checks (dk_deferred);
13966 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13967 alternative. */
13968 decl_specifiers
13969 = cp_parser_decl_specifier_seq (parser,
13970 CP_PARSER_FLAGS_OPTIONAL,
13971 &attributes,
13972 &declares_class_or_enum);
13973 /* Gather up the access checks that occurred the
13974 decl-specifier-seq. */
13975 stop_deferring_access_checks ();
13977 /* Check for the declaration of a template class. */
13978 if (declares_class_or_enum)
13980 if (cp_parser_declares_only_class_p (parser))
13982 decl = shadow_tag (decl_specifiers);
13983 if (decl)
13984 decl = TYPE_NAME (decl);
13985 else
13986 decl = error_mark_node;
13989 else
13990 decl = NULL_TREE;
13991 /* If it's not a template class, try for a template function. If
13992 the next token is a `;', then this declaration does not declare
13993 anything. But, if there were errors in the decl-specifiers, then
13994 the error might well have come from an attempted class-specifier.
13995 In that case, there's no need to warn about a missing declarator. */
13996 if (!decl
13997 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13998 || !value_member (error_mark_node, decl_specifiers)))
13999 decl = cp_parser_init_declarator (parser,
14000 decl_specifiers,
14001 attributes,
14002 /*function_definition_allowed_p=*/false,
14003 member_p,
14004 /*function_definition_p=*/NULL);
14006 pop_deferring_access_checks ();
14008 /* Clear any current qualification; whatever comes next is the start
14009 of something new. */
14010 parser->scope = NULL_TREE;
14011 parser->qualifying_scope = NULL_TREE;
14012 parser->object_scope = NULL_TREE;
14013 /* Look for a trailing `;' after the declaration. */
14014 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
14015 && cp_parser_committed_to_tentative_parse (parser))
14016 cp_parser_skip_to_end_of_block_or_statement (parser);
14017 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14018 if (cp_parser_parse_definitely (parser))
14020 if (friend_p)
14021 *friend_p = cp_parser_friend_p (decl_specifiers);
14023 /* Otherwise, try a function-definition. */
14024 else
14025 decl = cp_parser_function_definition (parser, friend_p);
14027 return decl;
14030 /* Parse a functional cast to TYPE. Returns an expression
14031 representing the cast. */
14033 static tree
14034 cp_parser_functional_cast (cp_parser* parser, tree type)
14036 tree expression_list;
14038 /* Look for the opening `('. */
14039 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14040 return error_mark_node;
14041 /* If the next token is not an `)', there are arguments to the
14042 cast. */
14043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14044 expression_list = cp_parser_expression_list (parser);
14045 else
14046 expression_list = NULL_TREE;
14047 /* Look for the closing `)'. */
14048 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14050 return build_functional_cast (type, expression_list);
14053 /* MEMBER_FUNCTION is a member function, or a friend. If default
14054 arguments, or the body of the function have not yet been parsed,
14055 parse them now. */
14057 static void
14058 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14060 cp_lexer *saved_lexer;
14062 /* If this member is a template, get the underlying
14063 FUNCTION_DECL. */
14064 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14065 member_function = DECL_TEMPLATE_RESULT (member_function);
14067 /* There should not be any class definitions in progress at this
14068 point; the bodies of members are only parsed outside of all class
14069 definitions. */
14070 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14071 /* While we're parsing the member functions we might encounter more
14072 classes. We want to handle them right away, but we don't want
14073 them getting mixed up with functions that are currently in the
14074 queue. */
14075 parser->unparsed_functions_queues
14076 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14078 /* Make sure that any template parameters are in scope. */
14079 maybe_begin_member_template_processing (member_function);
14081 /* If the body of the function has not yet been parsed, parse it
14082 now. */
14083 if (DECL_PENDING_INLINE_P (member_function))
14085 tree function_scope;
14086 cp_token_cache *tokens;
14088 /* The function is no longer pending; we are processing it. */
14089 tokens = DECL_PENDING_INLINE_INFO (member_function);
14090 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14091 DECL_PENDING_INLINE_P (member_function) = 0;
14092 /* If this was an inline function in a local class, enter the scope
14093 of the containing function. */
14094 function_scope = decl_function_context (member_function);
14095 if (function_scope)
14096 push_function_context_to (function_scope);
14098 /* Save away the current lexer. */
14099 saved_lexer = parser->lexer;
14100 /* Make a new lexer to feed us the tokens saved for this function. */
14101 parser->lexer = cp_lexer_new_from_tokens (tokens);
14102 parser->lexer->next = saved_lexer;
14104 /* Set the current source position to be the location of the first
14105 token in the saved inline body. */
14106 cp_lexer_peek_token (parser->lexer);
14108 /* Let the front end know that we going to be defining this
14109 function. */
14110 start_function (NULL_TREE, member_function, NULL_TREE,
14111 SF_PRE_PARSED | SF_INCLASS_INLINE);
14113 /* Now, parse the body of the function. */
14114 cp_parser_function_definition_after_declarator (parser,
14115 /*inline_p=*/true);
14117 /* Leave the scope of the containing function. */
14118 if (function_scope)
14119 pop_function_context_from (function_scope);
14120 /* Restore the lexer. */
14121 parser->lexer = saved_lexer;
14124 /* Remove any template parameters from the symbol table. */
14125 maybe_end_member_template_processing ();
14127 /* Restore the queue. */
14128 parser->unparsed_functions_queues
14129 = TREE_CHAIN (parser->unparsed_functions_queues);
14132 /* FN is a FUNCTION_DECL which may contains a parameter with an
14133 unparsed DEFAULT_ARG. Parse the default args now. */
14135 static void
14136 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14138 cp_lexer *saved_lexer;
14139 cp_token_cache *tokens;
14140 bool saved_local_variables_forbidden_p;
14141 tree parameters;
14143 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14144 parameters;
14145 parameters = TREE_CHAIN (parameters))
14147 if (!TREE_PURPOSE (parameters)
14148 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14149 continue;
14151 /* Save away the current lexer. */
14152 saved_lexer = parser->lexer;
14153 /* Create a new one, using the tokens we have saved. */
14154 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14155 parser->lexer = cp_lexer_new_from_tokens (tokens);
14157 /* Set the current source position to be the location of the
14158 first token in the default argument. */
14159 cp_lexer_peek_token (parser->lexer);
14161 /* Local variable names (and the `this' keyword) may not appear
14162 in a default argument. */
14163 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14164 parser->local_variables_forbidden_p = true;
14165 /* Parse the assignment-expression. */
14166 if (DECL_CONTEXT (fn))
14167 push_nested_class (DECL_CONTEXT (fn));
14168 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14169 if (DECL_CONTEXT (fn))
14170 pop_nested_class ();
14172 /* Restore saved state. */
14173 parser->lexer = saved_lexer;
14174 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14178 /* Parse the operand of `sizeof' (or a similar operator). Returns
14179 either a TYPE or an expression, depending on the form of the
14180 input. The KEYWORD indicates which kind of expression we have
14181 encountered. */
14183 static tree
14184 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14186 static const char *format;
14187 tree expr = NULL_TREE;
14188 const char *saved_message;
14189 bool saved_constant_expression_p;
14191 /* Initialize FORMAT the first time we get here. */
14192 if (!format)
14193 format = "types may not be defined in `%s' expressions";
14195 /* Types cannot be defined in a `sizeof' expression. Save away the
14196 old message. */
14197 saved_message = parser->type_definition_forbidden_message;
14198 /* And create the new one. */
14199 parser->type_definition_forbidden_message
14200 = ((const char *)
14201 xmalloc (strlen (format)
14202 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14203 + 1 /* `\0' */));
14204 sprintf ((char *) parser->type_definition_forbidden_message,
14205 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14207 /* The restrictions on constant-expressions do not apply inside
14208 sizeof expressions. */
14209 saved_constant_expression_p = parser->constant_expression_p;
14210 parser->constant_expression_p = false;
14212 /* Do not actually evaluate the expression. */
14213 ++skip_evaluation;
14214 /* If it's a `(', then we might be looking at the type-id
14215 construction. */
14216 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14218 tree type;
14220 /* We can't be sure yet whether we're looking at a type-id or an
14221 expression. */
14222 cp_parser_parse_tentatively (parser);
14223 /* Consume the `('. */
14224 cp_lexer_consume_token (parser->lexer);
14225 /* Parse the type-id. */
14226 type = cp_parser_type_id (parser);
14227 /* Now, look for the trailing `)'. */
14228 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14229 /* If all went well, then we're done. */
14230 if (cp_parser_parse_definitely (parser))
14232 /* Build a list of decl-specifiers; right now, we have only
14233 a single type-specifier. */
14234 type = build_tree_list (NULL_TREE,
14235 type);
14237 /* Call grokdeclarator to figure out what type this is. */
14238 expr = grokdeclarator (NULL_TREE,
14239 type,
14240 TYPENAME,
14241 /*initialized=*/0,
14242 /*attrlist=*/NULL);
14246 /* If the type-id production did not work out, then we must be
14247 looking at the unary-expression production. */
14248 if (!expr)
14249 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14250 /* Go back to evaluating expressions. */
14251 --skip_evaluation;
14253 /* Free the message we created. */
14254 free ((char *) parser->type_definition_forbidden_message);
14255 /* And restore the old one. */
14256 parser->type_definition_forbidden_message = saved_message;
14257 parser->constant_expression_p = saved_constant_expression_p;
14259 return expr;
14262 /* If the current declaration has no declarator, return true. */
14264 static bool
14265 cp_parser_declares_only_class_p (cp_parser *parser)
14267 /* If the next token is a `;' or a `,' then there is no
14268 declarator. */
14269 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14270 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14273 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14274 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14276 static bool
14277 cp_parser_friend_p (tree decl_specifiers)
14279 while (decl_specifiers)
14281 /* See if this decl-specifier is `friend'. */
14282 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14283 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14284 return true;
14286 /* Go on to the next decl-specifier. */
14287 decl_specifiers = TREE_CHAIN (decl_specifiers);
14290 return false;
14293 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14294 issue an error message indicating that TOKEN_DESC was expected.
14296 Returns the token consumed, if the token had the appropriate type.
14297 Otherwise, returns NULL. */
14299 static cp_token *
14300 cp_parser_require (cp_parser* parser,
14301 enum cpp_ttype type,
14302 const char* token_desc)
14304 if (cp_lexer_next_token_is (parser->lexer, type))
14305 return cp_lexer_consume_token (parser->lexer);
14306 else
14308 /* Output the MESSAGE -- unless we're parsing tentatively. */
14309 if (!cp_parser_simulate_error (parser))
14310 error ("expected %s", token_desc);
14311 return NULL;
14315 /* Like cp_parser_require, except that tokens will be skipped until
14316 the desired token is found. An error message is still produced if
14317 the next token is not as expected. */
14319 static void
14320 cp_parser_skip_until_found (cp_parser* parser,
14321 enum cpp_ttype type,
14322 const char* token_desc)
14324 cp_token *token;
14325 unsigned nesting_depth = 0;
14327 if (cp_parser_require (parser, type, token_desc))
14328 return;
14330 /* Skip tokens until the desired token is found. */
14331 while (true)
14333 /* Peek at the next token. */
14334 token = cp_lexer_peek_token (parser->lexer);
14335 /* If we've reached the token we want, consume it and
14336 stop. */
14337 if (token->type == type && !nesting_depth)
14339 cp_lexer_consume_token (parser->lexer);
14340 return;
14342 /* If we've run out of tokens, stop. */
14343 if (token->type == CPP_EOF)
14344 return;
14345 if (token->type == CPP_OPEN_BRACE
14346 || token->type == CPP_OPEN_PAREN
14347 || token->type == CPP_OPEN_SQUARE)
14348 ++nesting_depth;
14349 else if (token->type == CPP_CLOSE_BRACE
14350 || token->type == CPP_CLOSE_PAREN
14351 || token->type == CPP_CLOSE_SQUARE)
14353 if (nesting_depth-- == 0)
14354 return;
14356 /* Consume this token. */
14357 cp_lexer_consume_token (parser->lexer);
14361 /* If the next token is the indicated keyword, consume it. Otherwise,
14362 issue an error message indicating that TOKEN_DESC was expected.
14364 Returns the token consumed, if the token had the appropriate type.
14365 Otherwise, returns NULL. */
14367 static cp_token *
14368 cp_parser_require_keyword (cp_parser* parser,
14369 enum rid keyword,
14370 const char* token_desc)
14372 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14374 if (token && token->keyword != keyword)
14376 dyn_string_t error_msg;
14378 /* Format the error message. */
14379 error_msg = dyn_string_new (0);
14380 dyn_string_append_cstr (error_msg, "expected ");
14381 dyn_string_append_cstr (error_msg, token_desc);
14382 cp_parser_error (parser, error_msg->s);
14383 dyn_string_delete (error_msg);
14384 return NULL;
14387 return token;
14390 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14391 function-definition. */
14393 static bool
14394 cp_parser_token_starts_function_definition_p (cp_token* token)
14396 return (/* An ordinary function-body begins with an `{'. */
14397 token->type == CPP_OPEN_BRACE
14398 /* A ctor-initializer begins with a `:'. */
14399 || token->type == CPP_COLON
14400 /* A function-try-block begins with `try'. */
14401 || token->keyword == RID_TRY
14402 /* The named return value extension begins with `return'. */
14403 || token->keyword == RID_RETURN);
14406 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14407 definition. */
14409 static bool
14410 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14412 cp_token *token;
14414 token = cp_lexer_peek_token (parser->lexer);
14415 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14418 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14419 or none_type otherwise. */
14421 static enum tag_types
14422 cp_parser_token_is_class_key (cp_token* token)
14424 switch (token->keyword)
14426 case RID_CLASS:
14427 return class_type;
14428 case RID_STRUCT:
14429 return record_type;
14430 case RID_UNION:
14431 return union_type;
14433 default:
14434 return none_type;
14438 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14440 static void
14441 cp_parser_check_class_key (enum tag_types class_key, tree type)
14443 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14444 pedwarn ("`%s' tag used in naming `%#T'",
14445 class_key == union_type ? "union"
14446 : class_key == record_type ? "struct" : "class",
14447 type);
14450 /* Look for the `template' keyword, as a syntactic disambiguator.
14451 Return TRUE iff it is present, in which case it will be
14452 consumed. */
14454 static bool
14455 cp_parser_optional_template_keyword (cp_parser *parser)
14457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14459 /* The `template' keyword can only be used within templates;
14460 outside templates the parser can always figure out what is a
14461 template and what is not. */
14462 if (!processing_template_decl)
14464 error ("`template' (as a disambiguator) is only allowed "
14465 "within templates");
14466 /* If this part of the token stream is rescanned, the same
14467 error message would be generated. So, we purge the token
14468 from the stream. */
14469 cp_lexer_purge_token (parser->lexer);
14470 return false;
14472 else
14474 /* Consume the `template' keyword. */
14475 cp_lexer_consume_token (parser->lexer);
14476 return true;
14480 return false;
14483 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14484 set PARSER->SCOPE, and perform other related actions. */
14486 static void
14487 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14489 tree value;
14490 tree check;
14492 /* Get the stored value. */
14493 value = cp_lexer_consume_token (parser->lexer)->value;
14494 /* Perform any access checks that were deferred. */
14495 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14496 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14497 /* Set the scope from the stored value. */
14498 parser->scope = TREE_VALUE (value);
14499 parser->qualifying_scope = TREE_TYPE (value);
14500 parser->object_scope = NULL_TREE;
14503 /* Add tokens to CACHE until an non-nested END token appears. */
14505 static void
14506 cp_parser_cache_group (cp_parser *parser,
14507 cp_token_cache *cache,
14508 enum cpp_ttype end,
14509 unsigned depth)
14511 while (true)
14513 cp_token *token;
14515 /* Abort a parenthesized expression if we encounter a brace. */
14516 if ((end == CPP_CLOSE_PAREN || depth == 0)
14517 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14518 return;
14519 /* Consume the next token. */
14520 token = cp_lexer_consume_token (parser->lexer);
14521 /* If we've reached the end of the file, stop. */
14522 if (token->type == CPP_EOF)
14523 return;
14524 /* Add this token to the tokens we are saving. */
14525 cp_token_cache_push_token (cache, token);
14526 /* See if it starts a new group. */
14527 if (token->type == CPP_OPEN_BRACE)
14529 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14530 if (depth == 0)
14531 return;
14533 else if (token->type == CPP_OPEN_PAREN)
14534 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14535 else if (token->type == end)
14536 return;
14540 /* Begin parsing tentatively. We always save tokens while parsing
14541 tentatively so that if the tentative parsing fails we can restore the
14542 tokens. */
14544 static void
14545 cp_parser_parse_tentatively (cp_parser* parser)
14547 /* Enter a new parsing context. */
14548 parser->context = cp_parser_context_new (parser->context);
14549 /* Begin saving tokens. */
14550 cp_lexer_save_tokens (parser->lexer);
14551 /* In order to avoid repetitive access control error messages,
14552 access checks are queued up until we are no longer parsing
14553 tentatively. */
14554 push_deferring_access_checks (dk_deferred);
14557 /* Commit to the currently active tentative parse. */
14559 static void
14560 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14562 cp_parser_context *context;
14563 cp_lexer *lexer;
14565 /* Mark all of the levels as committed. */
14566 lexer = parser->lexer;
14567 for (context = parser->context; context->next; context = context->next)
14569 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14570 break;
14571 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14572 while (!cp_lexer_saving_tokens (lexer))
14573 lexer = lexer->next;
14574 cp_lexer_commit_tokens (lexer);
14578 /* Abort the currently active tentative parse. All consumed tokens
14579 will be rolled back, and no diagnostics will be issued. */
14581 static void
14582 cp_parser_abort_tentative_parse (cp_parser* parser)
14584 cp_parser_simulate_error (parser);
14585 /* Now, pretend that we want to see if the construct was
14586 successfully parsed. */
14587 cp_parser_parse_definitely (parser);
14590 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14591 token stream. Otherwise, commit to the tokens we have consumed.
14592 Returns true if no error occurred; false otherwise. */
14594 static bool
14595 cp_parser_parse_definitely (cp_parser* parser)
14597 bool error_occurred;
14598 cp_parser_context *context;
14600 /* Remember whether or not an error ocurred, since we are about to
14601 destroy that information. */
14602 error_occurred = cp_parser_error_occurred (parser);
14603 /* Remove the topmost context from the stack. */
14604 context = parser->context;
14605 parser->context = context->next;
14606 /* If no parse errors occurred, commit to the tentative parse. */
14607 if (!error_occurred)
14609 /* Commit to the tokens read tentatively, unless that was
14610 already done. */
14611 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14612 cp_lexer_commit_tokens (parser->lexer);
14614 pop_to_parent_deferring_access_checks ();
14616 /* Otherwise, if errors occurred, roll back our state so that things
14617 are just as they were before we began the tentative parse. */
14618 else
14620 cp_lexer_rollback_tokens (parser->lexer);
14621 pop_deferring_access_checks ();
14623 /* Add the context to the front of the free list. */
14624 context->next = cp_parser_context_free_list;
14625 cp_parser_context_free_list = context;
14627 return !error_occurred;
14630 /* Returns true if we are parsing tentatively -- but have decided that
14631 we will stick with this tentative parse, even if errors occur. */
14633 static bool
14634 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14636 return (cp_parser_parsing_tentatively (parser)
14637 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14640 /* Returns nonzero iff an error has occurred during the most recent
14641 tentative parse. */
14643 static bool
14644 cp_parser_error_occurred (cp_parser* parser)
14646 return (cp_parser_parsing_tentatively (parser)
14647 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14650 /* Returns nonzero if GNU extensions are allowed. */
14652 static bool
14653 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14655 return parser->allow_gnu_extensions_p;
14660 /* The parser. */
14662 static GTY (()) cp_parser *the_parser;
14664 /* External interface. */
14666 /* Parse the entire translation unit. */
14669 yyparse (void)
14671 bool error_occurred;
14673 the_parser = cp_parser_new ();
14674 push_deferring_access_checks (flag_access_control
14675 ? dk_no_deferred : dk_no_check);
14676 error_occurred = cp_parser_translation_unit (the_parser);
14677 the_parser = NULL;
14679 finish_file ();
14681 return error_occurred;
14684 /* Clean up after parsing the entire translation unit. */
14686 void
14687 free_parser_stacks (void)
14689 /* Nothing to do. */
14692 /* This variable must be provided by every front end. */
14694 int yydebug;
14696 #include "gt-cp-parser.h"