(read_braced_string): Check for EOF. If encountered issue an error message.
[official-gcc.git] / gcc / cp / parser.c
blob4799944b99481c86dcefbe436e91ceecd2e14207
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 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. */
2787 && !(TREE_CODE (decl) == VAR_DECL
2788 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2789 && DECL_INITIAL (decl)
2790 && TREE_CONSTANT (DECL_INITIAL (decl))))
2792 if (!parser->allow_non_constant_expression_p)
2793 return cp_parser_non_constant_id_expression (decl);
2794 parser->non_constant_expression_p = true;
2797 if (parser->scope)
2799 decl = (adjust_result_of_qualified_name_lookup
2800 (decl, parser->scope, current_class_type));
2801 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2802 *qualifying_class = parser->scope;
2803 else if (!processing_template_decl)
2804 decl = convert_from_reference (decl);
2806 else
2807 /* Transform references to non-static data members into
2808 COMPONENT_REFs. */
2809 decl = hack_identifier (decl, id_expression);
2811 /* Resolve references to variables of anonymous unions
2812 into COMPONENT_REFs. */
2813 if (TREE_CODE (decl) == ALIAS_DECL)
2814 decl = DECL_INITIAL (decl);
2817 if (TREE_DEPRECATED (decl))
2818 warn_deprecated_use (decl);
2820 return decl;
2823 /* Anything else is an error. */
2824 default:
2825 cp_parser_error (parser, "expected primary-expression");
2826 return error_mark_node;
2830 /* Parse an id-expression.
2832 id-expression:
2833 unqualified-id
2834 qualified-id
2836 qualified-id:
2837 :: [opt] nested-name-specifier template [opt] unqualified-id
2838 :: identifier
2839 :: operator-function-id
2840 :: template-id
2842 Return a representation of the unqualified portion of the
2843 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2844 a `::' or nested-name-specifier.
2846 Often, if the id-expression was a qualified-id, the caller will
2847 want to make a SCOPE_REF to represent the qualified-id. This
2848 function does not do this in order to avoid wastefully creating
2849 SCOPE_REFs when they are not required.
2851 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2852 `template' keyword.
2854 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2855 uninstantiated templates.
2857 If *TEMPLATE_P is non-NULL, it is set to true iff the
2858 `template' keyword is used to explicitly indicate that the entity
2859 named is a template. */
2861 static tree
2862 cp_parser_id_expression (cp_parser *parser,
2863 bool template_keyword_p,
2864 bool check_dependency_p,
2865 bool *template_p)
2867 bool global_scope_p;
2868 bool nested_name_specifier_p;
2870 /* Assume the `template' keyword was not used. */
2871 if (template_p)
2872 *template_p = false;
2874 /* Look for the optional `::' operator. */
2875 global_scope_p
2876 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2877 != NULL_TREE);
2878 /* Look for the optional nested-name-specifier. */
2879 nested_name_specifier_p
2880 = (cp_parser_nested_name_specifier_opt (parser,
2881 /*typename_keyword_p=*/false,
2882 check_dependency_p,
2883 /*type_p=*/false)
2884 != NULL_TREE);
2885 /* If there is a nested-name-specifier, then we are looking at
2886 the first qualified-id production. */
2887 if (nested_name_specifier_p)
2889 tree saved_scope;
2890 tree saved_object_scope;
2891 tree saved_qualifying_scope;
2892 tree unqualified_id;
2893 bool is_template;
2895 /* See if the next token is the `template' keyword. */
2896 if (!template_p)
2897 template_p = &is_template;
2898 *template_p = cp_parser_optional_template_keyword (parser);
2899 /* Name lookup we do during the processing of the
2900 unqualified-id might obliterate SCOPE. */
2901 saved_scope = parser->scope;
2902 saved_object_scope = parser->object_scope;
2903 saved_qualifying_scope = parser->qualifying_scope;
2904 /* Process the final unqualified-id. */
2905 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2906 check_dependency_p);
2907 /* Restore the SAVED_SCOPE for our caller. */
2908 parser->scope = saved_scope;
2909 parser->object_scope = saved_object_scope;
2910 parser->qualifying_scope = saved_qualifying_scope;
2912 return unqualified_id;
2914 /* Otherwise, if we are in global scope, then we are looking at one
2915 of the other qualified-id productions. */
2916 else if (global_scope_p)
2918 cp_token *token;
2919 tree id;
2921 /* Peek at the next token. */
2922 token = cp_lexer_peek_token (parser->lexer);
2924 /* If it's an identifier, and the next token is not a "<", then
2925 we can avoid the template-id case. This is an optimization
2926 for this common case. */
2927 if (token->type == CPP_NAME
2928 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2929 return cp_parser_identifier (parser);
2931 cp_parser_parse_tentatively (parser);
2932 /* Try a template-id. */
2933 id = cp_parser_template_id (parser,
2934 /*template_keyword_p=*/false,
2935 /*check_dependency_p=*/true);
2936 /* If that worked, we're done. */
2937 if (cp_parser_parse_definitely (parser))
2938 return id;
2940 /* Peek at the next token. (Changes in the token buffer may
2941 have invalidated the pointer obtained above.) */
2942 token = cp_lexer_peek_token (parser->lexer);
2944 switch (token->type)
2946 case CPP_NAME:
2947 return cp_parser_identifier (parser);
2949 case CPP_KEYWORD:
2950 if (token->keyword == RID_OPERATOR)
2951 return cp_parser_operator_function_id (parser);
2952 /* Fall through. */
2954 default:
2955 cp_parser_error (parser, "expected id-expression");
2956 return error_mark_node;
2959 else
2960 return cp_parser_unqualified_id (parser, template_keyword_p,
2961 /*check_dependency_p=*/true);
2964 /* Parse an unqualified-id.
2966 unqualified-id:
2967 identifier
2968 operator-function-id
2969 conversion-function-id
2970 ~ class-name
2971 template-id
2973 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2974 keyword, in a construct like `A::template ...'.
2976 Returns a representation of unqualified-id. For the `identifier'
2977 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2978 production a BIT_NOT_EXPR is returned; the operand of the
2979 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2980 other productions, see the documentation accompanying the
2981 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2982 names are looked up in uninstantiated templates. */
2984 static tree
2985 cp_parser_unqualified_id (cp_parser* parser,
2986 bool template_keyword_p,
2987 bool check_dependency_p)
2989 cp_token *token;
2991 /* Peek at the next token. */
2992 token = cp_lexer_peek_token (parser->lexer);
2994 switch (token->type)
2996 case CPP_NAME:
2998 tree id;
3000 /* We don't know yet whether or not this will be a
3001 template-id. */
3002 cp_parser_parse_tentatively (parser);
3003 /* Try a template-id. */
3004 id = cp_parser_template_id (parser, template_keyword_p,
3005 check_dependency_p);
3006 /* If it worked, we're done. */
3007 if (cp_parser_parse_definitely (parser))
3008 return id;
3009 /* Otherwise, it's an ordinary identifier. */
3010 return cp_parser_identifier (parser);
3013 case CPP_TEMPLATE_ID:
3014 return cp_parser_template_id (parser, template_keyword_p,
3015 check_dependency_p);
3017 case CPP_COMPL:
3019 tree type_decl;
3020 tree qualifying_scope;
3021 tree object_scope;
3022 tree scope;
3024 /* Consume the `~' token. */
3025 cp_lexer_consume_token (parser->lexer);
3026 /* Parse the class-name. The standard, as written, seems to
3027 say that:
3029 template <typename T> struct S { ~S (); };
3030 template <typename T> S<T>::~S() {}
3032 is invalid, since `~' must be followed by a class-name, but
3033 `S<T>' is dependent, and so not known to be a class.
3034 That's not right; we need to look in uninstantiated
3035 templates. A further complication arises from:
3037 template <typename T> void f(T t) {
3038 t.T::~T();
3041 Here, it is not possible to look up `T' in the scope of `T'
3042 itself. We must look in both the current scope, and the
3043 scope of the containing complete expression.
3045 Yet another issue is:
3047 struct S {
3048 int S;
3049 ~S();
3052 S::~S() {}
3054 The standard does not seem to say that the `S' in `~S'
3055 should refer to the type `S' and not the data member
3056 `S::S'. */
3058 /* DR 244 says that we look up the name after the "~" in the
3059 same scope as we looked up the qualifying name. That idea
3060 isn't fully worked out; it's more complicated than that. */
3061 scope = parser->scope;
3062 object_scope = parser->object_scope;
3063 qualifying_scope = parser->qualifying_scope;
3065 /* If the name is of the form "X::~X" it's OK. */
3066 if (scope && TYPE_P (scope)
3067 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3068 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3069 == CPP_OPEN_PAREN)
3070 && (cp_lexer_peek_token (parser->lexer)->value
3071 == TYPE_IDENTIFIER (scope)))
3073 cp_lexer_consume_token (parser->lexer);
3074 return build_nt (BIT_NOT_EXPR, scope);
3077 /* If there was an explicit qualification (S::~T), first look
3078 in the scope given by the qualification (i.e., S). */
3079 if (scope)
3081 cp_parser_parse_tentatively (parser);
3082 type_decl = cp_parser_class_name (parser,
3083 /*typename_keyword_p=*/false,
3084 /*template_keyword_p=*/false,
3085 /*type_p=*/false,
3086 /*check_dependency=*/false,
3087 /*class_head_p=*/false);
3088 if (cp_parser_parse_definitely (parser))
3089 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3091 /* In "N::S::~S", look in "N" as well. */
3092 if (scope && qualifying_scope)
3094 cp_parser_parse_tentatively (parser);
3095 parser->scope = qualifying_scope;
3096 parser->object_scope = NULL_TREE;
3097 parser->qualifying_scope = NULL_TREE;
3098 type_decl
3099 = cp_parser_class_name (parser,
3100 /*typename_keyword_p=*/false,
3101 /*template_keyword_p=*/false,
3102 /*type_p=*/false,
3103 /*check_dependency=*/false,
3104 /*class_head_p=*/false);
3105 if (cp_parser_parse_definitely (parser))
3106 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3108 /* In "p->S::~T", look in the scope given by "*p" as well. */
3109 else if (object_scope)
3111 cp_parser_parse_tentatively (parser);
3112 parser->scope = object_scope;
3113 parser->object_scope = NULL_TREE;
3114 parser->qualifying_scope = NULL_TREE;
3115 type_decl
3116 = cp_parser_class_name (parser,
3117 /*typename_keyword_p=*/false,
3118 /*template_keyword_p=*/false,
3119 /*type_p=*/false,
3120 /*check_dependency=*/false,
3121 /*class_head_p=*/false);
3122 if (cp_parser_parse_definitely (parser))
3123 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3125 /* Look in the surrounding context. */
3126 parser->scope = NULL_TREE;
3127 parser->object_scope = NULL_TREE;
3128 parser->qualifying_scope = NULL_TREE;
3129 type_decl
3130 = cp_parser_class_name (parser,
3131 /*typename_keyword_p=*/false,
3132 /*template_keyword_p=*/false,
3133 /*type_p=*/false,
3134 /*check_dependency=*/false,
3135 /*class_head_p=*/false);
3136 /* If an error occurred, assume that the name of the
3137 destructor is the same as the name of the qualifying
3138 class. That allows us to keep parsing after running
3139 into ill-formed destructor names. */
3140 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3141 return build_nt (BIT_NOT_EXPR, scope);
3142 else if (type_decl == error_mark_node)
3143 return error_mark_node;
3145 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3148 case CPP_KEYWORD:
3149 if (token->keyword == RID_OPERATOR)
3151 tree id;
3153 /* This could be a template-id, so we try that first. */
3154 cp_parser_parse_tentatively (parser);
3155 /* Try a template-id. */
3156 id = cp_parser_template_id (parser, template_keyword_p,
3157 /*check_dependency_p=*/true);
3158 /* If that worked, we're done. */
3159 if (cp_parser_parse_definitely (parser))
3160 return id;
3161 /* We still don't know whether we're looking at an
3162 operator-function-id or a conversion-function-id. */
3163 cp_parser_parse_tentatively (parser);
3164 /* Try an operator-function-id. */
3165 id = cp_parser_operator_function_id (parser);
3166 /* If that didn't work, try a conversion-function-id. */
3167 if (!cp_parser_parse_definitely (parser))
3168 id = cp_parser_conversion_function_id (parser);
3170 return id;
3172 /* Fall through. */
3174 default:
3175 cp_parser_error (parser, "expected unqualified-id");
3176 return error_mark_node;
3180 /* Parse an (optional) nested-name-specifier.
3182 nested-name-specifier:
3183 class-or-namespace-name :: nested-name-specifier [opt]
3184 class-or-namespace-name :: template nested-name-specifier [opt]
3186 PARSER->SCOPE should be set appropriately before this function is
3187 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3188 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3189 in name lookups.
3191 Sets PARSER->SCOPE to the class (TYPE) or namespace
3192 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3193 it unchanged if there is no nested-name-specifier. Returns the new
3194 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3196 static tree
3197 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3198 bool typename_keyword_p,
3199 bool check_dependency_p,
3200 bool type_p)
3202 bool success = false;
3203 tree access_check = NULL_TREE;
3204 ptrdiff_t start;
3205 cp_token* token;
3207 /* If the next token corresponds to a nested name specifier, there
3208 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3209 false, it may have been true before, in which case something
3210 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3211 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3212 CHECK_DEPENDENCY_P is false, we have to fall through into the
3213 main loop. */
3214 if (check_dependency_p
3215 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3217 cp_parser_pre_parsed_nested_name_specifier (parser);
3218 return parser->scope;
3221 /* Remember where the nested-name-specifier starts. */
3222 if (cp_parser_parsing_tentatively (parser)
3223 && !cp_parser_committed_to_tentative_parse (parser))
3225 token = cp_lexer_peek_token (parser->lexer);
3226 start = cp_lexer_token_difference (parser->lexer,
3227 parser->lexer->first_token,
3228 token);
3230 else
3231 start = -1;
3233 push_deferring_access_checks (dk_deferred);
3235 while (true)
3237 tree new_scope;
3238 tree old_scope;
3239 tree saved_qualifying_scope;
3240 bool template_keyword_p;
3242 /* Spot cases that cannot be the beginning of a
3243 nested-name-specifier. */
3244 token = cp_lexer_peek_token (parser->lexer);
3246 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3247 the already parsed nested-name-specifier. */
3248 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3250 /* Grab the nested-name-specifier and continue the loop. */
3251 cp_parser_pre_parsed_nested_name_specifier (parser);
3252 success = true;
3253 continue;
3256 /* Spot cases that cannot be the beginning of a
3257 nested-name-specifier. On the second and subsequent times
3258 through the loop, we look for the `template' keyword. */
3259 if (success && token->keyword == RID_TEMPLATE)
3261 /* A template-id can start a nested-name-specifier. */
3262 else if (token->type == CPP_TEMPLATE_ID)
3264 else
3266 /* If the next token is not an identifier, then it is
3267 definitely not a class-or-namespace-name. */
3268 if (token->type != CPP_NAME)
3269 break;
3270 /* If the following token is neither a `<' (to begin a
3271 template-id), nor a `::', then we are not looking at a
3272 nested-name-specifier. */
3273 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3274 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3275 break;
3278 /* The nested-name-specifier is optional, so we parse
3279 tentatively. */
3280 cp_parser_parse_tentatively (parser);
3282 /* Look for the optional `template' keyword, if this isn't the
3283 first time through the loop. */
3284 if (success)
3285 template_keyword_p = cp_parser_optional_template_keyword (parser);
3286 else
3287 template_keyword_p = false;
3289 /* Save the old scope since the name lookup we are about to do
3290 might destroy it. */
3291 old_scope = parser->scope;
3292 saved_qualifying_scope = parser->qualifying_scope;
3293 /* Parse the qualifying entity. */
3294 new_scope
3295 = cp_parser_class_or_namespace_name (parser,
3296 typename_keyword_p,
3297 template_keyword_p,
3298 check_dependency_p,
3299 type_p);
3300 /* Look for the `::' token. */
3301 cp_parser_require (parser, CPP_SCOPE, "`::'");
3303 /* If we found what we wanted, we keep going; otherwise, we're
3304 done. */
3305 if (!cp_parser_parse_definitely (parser))
3307 bool error_p = false;
3309 /* Restore the OLD_SCOPE since it was valid before the
3310 failed attempt at finding the last
3311 class-or-namespace-name. */
3312 parser->scope = old_scope;
3313 parser->qualifying_scope = saved_qualifying_scope;
3314 /* If the next token is an identifier, and the one after
3315 that is a `::', then any valid interpretation would have
3316 found a class-or-namespace-name. */
3317 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3318 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3319 == CPP_SCOPE)
3320 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3321 != CPP_COMPL))
3323 token = cp_lexer_consume_token (parser->lexer);
3324 if (!error_p)
3326 tree decl;
3328 decl = cp_parser_lookup_name_simple (parser, token->value);
3329 if (TREE_CODE (decl) == TEMPLATE_DECL)
3330 error ("`%D' used without template parameters",
3331 decl);
3332 else if (parser->scope)
3334 if (TYPE_P (parser->scope))
3335 error ("`%T::%D' is not a class-name or "
3336 "namespace-name",
3337 parser->scope, token->value);
3338 else
3339 error ("`%D::%D' is not a class-name or "
3340 "namespace-name",
3341 parser->scope, token->value);
3343 else
3344 error ("`%D' is not a class-name or namespace-name",
3345 token->value);
3346 parser->scope = NULL_TREE;
3347 error_p = true;
3348 /* Treat this as a successful nested-name-specifier
3349 due to:
3351 [basic.lookup.qual]
3353 If the name found is not a class-name (clause
3354 _class_) or namespace-name (_namespace.def_), the
3355 program is ill-formed. */
3356 success = true;
3358 cp_lexer_consume_token (parser->lexer);
3360 break;
3363 /* We've found one valid nested-name-specifier. */
3364 success = true;
3365 /* Make sure we look in the right scope the next time through
3366 the loop. */
3367 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3368 ? TREE_TYPE (new_scope)
3369 : new_scope);
3370 /* If it is a class scope, try to complete it; we are about to
3371 be looking up names inside the class. */
3372 if (TYPE_P (parser->scope)
3373 /* Since checking types for dependency can be expensive,
3374 avoid doing it if the type is already complete. */
3375 && !COMPLETE_TYPE_P (parser->scope)
3376 /* Do not try to complete dependent types. */
3377 && !dependent_type_p (parser->scope))
3378 complete_type (parser->scope);
3381 /* Retrieve any deferred checks. Do not pop this access checks yet
3382 so the memory will not be reclaimed during token replacing below. */
3383 access_check = get_deferred_access_checks ();
3385 /* If parsing tentatively, replace the sequence of tokens that makes
3386 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3387 token. That way, should we re-parse the token stream, we will
3388 not have to repeat the effort required to do the parse, nor will
3389 we issue duplicate error messages. */
3390 if (success && start >= 0)
3392 /* Find the token that corresponds to the start of the
3393 template-id. */
3394 token = cp_lexer_advance_token (parser->lexer,
3395 parser->lexer->first_token,
3396 start);
3398 /* Reset the contents of the START token. */
3399 token->type = CPP_NESTED_NAME_SPECIFIER;
3400 token->value = build_tree_list (access_check, parser->scope);
3401 TREE_TYPE (token->value) = parser->qualifying_scope;
3402 token->keyword = RID_MAX;
3403 /* Purge all subsequent tokens. */
3404 cp_lexer_purge_tokens_after (parser->lexer, token);
3407 pop_deferring_access_checks ();
3408 return success ? parser->scope : NULL_TREE;
3411 /* Parse a nested-name-specifier. See
3412 cp_parser_nested_name_specifier_opt for details. This function
3413 behaves identically, except that it will an issue an error if no
3414 nested-name-specifier is present, and it will return
3415 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3416 is present. */
3418 static tree
3419 cp_parser_nested_name_specifier (cp_parser *parser,
3420 bool typename_keyword_p,
3421 bool check_dependency_p,
3422 bool type_p)
3424 tree scope;
3426 /* Look for the nested-name-specifier. */
3427 scope = cp_parser_nested_name_specifier_opt (parser,
3428 typename_keyword_p,
3429 check_dependency_p,
3430 type_p);
3431 /* If it was not present, issue an error message. */
3432 if (!scope)
3434 cp_parser_error (parser, "expected nested-name-specifier");
3435 return error_mark_node;
3438 return scope;
3441 /* Parse a class-or-namespace-name.
3443 class-or-namespace-name:
3444 class-name
3445 namespace-name
3447 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3448 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3449 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3450 TYPE_P is TRUE iff the next name should be taken as a class-name,
3451 even the same name is declared to be another entity in the same
3452 scope.
3454 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3455 specified by the class-or-namespace-name. If neither is found the
3456 ERROR_MARK_NODE is returned. */
3458 static tree
3459 cp_parser_class_or_namespace_name (cp_parser *parser,
3460 bool typename_keyword_p,
3461 bool template_keyword_p,
3462 bool check_dependency_p,
3463 bool type_p)
3465 tree saved_scope;
3466 tree saved_qualifying_scope;
3467 tree saved_object_scope;
3468 tree scope;
3469 bool only_class_p;
3471 /* Before we try to parse the class-name, we must save away the
3472 current PARSER->SCOPE since cp_parser_class_name will destroy
3473 it. */
3474 saved_scope = parser->scope;
3475 saved_qualifying_scope = parser->qualifying_scope;
3476 saved_object_scope = parser->object_scope;
3477 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3478 there is no need to look for a namespace-name. */
3479 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3480 if (!only_class_p)
3481 cp_parser_parse_tentatively (parser);
3482 scope = cp_parser_class_name (parser,
3483 typename_keyword_p,
3484 template_keyword_p,
3485 type_p,
3486 check_dependency_p,
3487 /*class_head_p=*/false);
3488 /* If that didn't work, try for a namespace-name. */
3489 if (!only_class_p && !cp_parser_parse_definitely (parser))
3491 /* Restore the saved scope. */
3492 parser->scope = saved_scope;
3493 parser->qualifying_scope = saved_qualifying_scope;
3494 parser->object_scope = saved_object_scope;
3495 /* If we are not looking at an identifier followed by the scope
3496 resolution operator, then this is not part of a
3497 nested-name-specifier. (Note that this function is only used
3498 to parse the components of a nested-name-specifier.) */
3499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3500 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3501 return error_mark_node;
3502 scope = cp_parser_namespace_name (parser);
3505 return scope;
3508 /* Parse a postfix-expression.
3510 postfix-expression:
3511 primary-expression
3512 postfix-expression [ expression ]
3513 postfix-expression ( expression-list [opt] )
3514 simple-type-specifier ( expression-list [opt] )
3515 typename :: [opt] nested-name-specifier identifier
3516 ( expression-list [opt] )
3517 typename :: [opt] nested-name-specifier template [opt] template-id
3518 ( expression-list [opt] )
3519 postfix-expression . template [opt] id-expression
3520 postfix-expression -> template [opt] id-expression
3521 postfix-expression . pseudo-destructor-name
3522 postfix-expression -> pseudo-destructor-name
3523 postfix-expression ++
3524 postfix-expression --
3525 dynamic_cast < type-id > ( expression )
3526 static_cast < type-id > ( expression )
3527 reinterpret_cast < type-id > ( expression )
3528 const_cast < type-id > ( expression )
3529 typeid ( expression )
3530 typeid ( type-id )
3532 GNU Extension:
3534 postfix-expression:
3535 ( type-id ) { initializer-list , [opt] }
3537 This extension is a GNU version of the C99 compound-literal
3538 construct. (The C99 grammar uses `type-name' instead of `type-id',
3539 but they are essentially the same concept.)
3541 If ADDRESS_P is true, the postfix expression is the operand of the
3542 `&' operator.
3544 Returns a representation of the expression. */
3546 static tree
3547 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3549 cp_token *token;
3550 enum rid keyword;
3551 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3552 tree postfix_expression = NULL_TREE;
3553 /* Non-NULL only if the current postfix-expression can be used to
3554 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3555 class used to qualify the member. */
3556 tree qualifying_class = NULL_TREE;
3557 bool done;
3559 /* Peek at the next token. */
3560 token = cp_lexer_peek_token (parser->lexer);
3561 /* Some of the productions are determined by keywords. */
3562 keyword = token->keyword;
3563 switch (keyword)
3565 case RID_DYNCAST:
3566 case RID_STATCAST:
3567 case RID_REINTCAST:
3568 case RID_CONSTCAST:
3570 tree type;
3571 tree expression;
3572 const char *saved_message;
3574 /* All of these can be handled in the same way from the point
3575 of view of parsing. Begin by consuming the token
3576 identifying the cast. */
3577 cp_lexer_consume_token (parser->lexer);
3579 /* New types cannot be defined in the cast. */
3580 saved_message = parser->type_definition_forbidden_message;
3581 parser->type_definition_forbidden_message
3582 = "types may not be defined in casts";
3584 /* Look for the opening `<'. */
3585 cp_parser_require (parser, CPP_LESS, "`<'");
3586 /* Parse the type to which we are casting. */
3587 type = cp_parser_type_id (parser);
3588 /* Look for the closing `>'. */
3589 cp_parser_require (parser, CPP_GREATER, "`>'");
3590 /* Restore the old message. */
3591 parser->type_definition_forbidden_message = saved_message;
3593 /* And the expression which is being cast. */
3594 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3595 expression = cp_parser_expression (parser);
3596 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3598 /* Only type conversions to integral or enumeration types
3599 can be used in constant-expressions. */
3600 if (parser->constant_expression_p
3601 && !dependent_type_p (type)
3602 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3604 if (!parser->allow_non_constant_expression_p)
3605 return (cp_parser_non_constant_expression
3606 ("a cast to a type other than an integral or "
3607 "enumeration type"));
3608 parser->non_constant_expression_p = true;
3611 switch (keyword)
3613 case RID_DYNCAST:
3614 postfix_expression
3615 = build_dynamic_cast (type, expression);
3616 break;
3617 case RID_STATCAST:
3618 postfix_expression
3619 = build_static_cast (type, expression);
3620 break;
3621 case RID_REINTCAST:
3622 postfix_expression
3623 = build_reinterpret_cast (type, expression);
3624 break;
3625 case RID_CONSTCAST:
3626 postfix_expression
3627 = build_const_cast (type, expression);
3628 break;
3629 default:
3630 abort ();
3633 break;
3635 case RID_TYPEID:
3637 tree type;
3638 const char *saved_message;
3640 /* Consume the `typeid' token. */
3641 cp_lexer_consume_token (parser->lexer);
3642 /* Look for the `(' token. */
3643 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3644 /* Types cannot be defined in a `typeid' expression. */
3645 saved_message = parser->type_definition_forbidden_message;
3646 parser->type_definition_forbidden_message
3647 = "types may not be defined in a `typeid\' expression";
3648 /* We can't be sure yet whether we're looking at a type-id or an
3649 expression. */
3650 cp_parser_parse_tentatively (parser);
3651 /* Try a type-id first. */
3652 type = cp_parser_type_id (parser);
3653 /* Look for the `)' token. Otherwise, we can't be sure that
3654 we're not looking at an expression: consider `typeid (int
3655 (3))', for example. */
3656 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3657 /* If all went well, simply lookup the type-id. */
3658 if (cp_parser_parse_definitely (parser))
3659 postfix_expression = get_typeid (type);
3660 /* Otherwise, fall back to the expression variant. */
3661 else
3663 tree expression;
3665 /* Look for an expression. */
3666 expression = cp_parser_expression (parser);
3667 /* Compute its typeid. */
3668 postfix_expression = build_typeid (expression);
3669 /* Look for the `)' token. */
3670 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3673 /* Restore the saved message. */
3674 parser->type_definition_forbidden_message = saved_message;
3676 break;
3678 case RID_TYPENAME:
3680 bool template_p = false;
3681 tree id;
3682 tree type;
3684 /* Consume the `typename' token. */
3685 cp_lexer_consume_token (parser->lexer);
3686 /* Look for the optional `::' operator. */
3687 cp_parser_global_scope_opt (parser,
3688 /*current_scope_valid_p=*/false);
3689 /* Look for the nested-name-specifier. */
3690 cp_parser_nested_name_specifier (parser,
3691 /*typename_keyword_p=*/true,
3692 /*check_dependency_p=*/true,
3693 /*type_p=*/true);
3694 /* Look for the optional `template' keyword. */
3695 template_p = cp_parser_optional_template_keyword (parser);
3696 /* We don't know whether we're looking at a template-id or an
3697 identifier. */
3698 cp_parser_parse_tentatively (parser);
3699 /* Try a template-id. */
3700 id = cp_parser_template_id (parser, template_p,
3701 /*check_dependency_p=*/true);
3702 /* If that didn't work, try an identifier. */
3703 if (!cp_parser_parse_definitely (parser))
3704 id = cp_parser_identifier (parser);
3705 /* Create a TYPENAME_TYPE to represent the type to which the
3706 functional cast is being performed. */
3707 type = make_typename_type (parser->scope, id,
3708 /*complain=*/1);
3710 postfix_expression = cp_parser_functional_cast (parser, type);
3712 break;
3714 default:
3716 tree type;
3718 /* If the next thing is a simple-type-specifier, we may be
3719 looking at a functional cast. We could also be looking at
3720 an id-expression. So, we try the functional cast, and if
3721 that doesn't work we fall back to the primary-expression. */
3722 cp_parser_parse_tentatively (parser);
3723 /* Look for the simple-type-specifier. */
3724 type = cp_parser_simple_type_specifier (parser,
3725 CP_PARSER_FLAGS_NONE);
3726 /* Parse the cast itself. */
3727 if (!cp_parser_error_occurred (parser))
3728 postfix_expression
3729 = cp_parser_functional_cast (parser, type);
3730 /* If that worked, we're done. */
3731 if (cp_parser_parse_definitely (parser))
3732 break;
3734 /* If the functional-cast didn't work out, try a
3735 compound-literal. */
3736 if (cp_parser_allow_gnu_extensions_p (parser)
3737 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3739 tree initializer_list = NULL_TREE;
3741 cp_parser_parse_tentatively (parser);
3742 /* Consume the `('. */
3743 cp_lexer_consume_token (parser->lexer);
3744 /* Parse the type. */
3745 type = cp_parser_type_id (parser);
3746 /* Look for the `)'. */
3747 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3748 /* Look for the `{'. */
3749 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3750 /* If things aren't going well, there's no need to
3751 keep going. */
3752 if (!cp_parser_error_occurred (parser))
3754 /* Parse the initializer-list. */
3755 initializer_list
3756 = cp_parser_initializer_list (parser);
3757 /* Allow a trailing `,'. */
3758 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3759 cp_lexer_consume_token (parser->lexer);
3760 /* Look for the final `}'. */
3761 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3763 /* If that worked, we're definitely looking at a
3764 compound-literal expression. */
3765 if (cp_parser_parse_definitely (parser))
3767 /* Warn the user that a compound literal is not
3768 allowed in standard C++. */
3769 if (pedantic)
3770 pedwarn ("ISO C++ forbids compound-literals");
3771 /* Form the representation of the compound-literal. */
3772 postfix_expression
3773 = finish_compound_literal (type, initializer_list);
3774 break;
3778 /* It must be a primary-expression. */
3779 postfix_expression = cp_parser_primary_expression (parser,
3780 &idk,
3781 &qualifying_class);
3783 break;
3786 /* Peek at the next token. */
3787 token = cp_lexer_peek_token (parser->lexer);
3788 done = (token->type != CPP_OPEN_SQUARE
3789 && token->type != CPP_OPEN_PAREN
3790 && token->type != CPP_DOT
3791 && token->type != CPP_DEREF
3792 && token->type != CPP_PLUS_PLUS
3793 && token->type != CPP_MINUS_MINUS);
3795 /* If the postfix expression is complete, finish up. */
3796 if (address_p && qualifying_class && done)
3798 if (TREE_CODE (postfix_expression) == SCOPE_REF)
3799 postfix_expression = TREE_OPERAND (postfix_expression, 1);
3800 postfix_expression
3801 = build_offset_ref (qualifying_class, postfix_expression);
3802 return postfix_expression;
3805 /* Otherwise, if we were avoiding committing until we knew
3806 whether or not we had a pointer-to-member, we now know that
3807 the expression is an ordinary reference to a qualified name. */
3808 if (qualifying_class)
3810 if (TREE_CODE (postfix_expression) == FIELD_DECL)
3811 postfix_expression
3812 = finish_non_static_data_member (postfix_expression,
3813 qualifying_class);
3814 else if (BASELINK_P (postfix_expression)
3815 && !processing_template_decl)
3817 tree fn;
3818 tree fns;
3820 /* See if any of the functions are non-static members. */
3821 fns = BASELINK_FUNCTIONS (postfix_expression);
3822 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3823 fns = TREE_OPERAND (fns, 0);
3824 for (fn = fns; fn; fn = OVL_NEXT (fn))
3825 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3826 break;
3827 /* If so, the expression may be relative to the current
3828 class. */
3829 if (fn && current_class_type
3830 && DERIVED_FROM_P (qualifying_class, current_class_type))
3831 postfix_expression
3832 = (build_class_member_access_expr
3833 (maybe_dummy_object (qualifying_class, NULL),
3834 postfix_expression,
3835 BASELINK_ACCESS_BINFO (postfix_expression),
3836 /*preserve_reference=*/false));
3837 else if (done)
3838 return build_offset_ref (qualifying_class,
3839 postfix_expression);
3843 /* Remember that there was a reference to this entity. */
3844 if (DECL_P (postfix_expression))
3845 mark_used (postfix_expression);
3847 /* Keep looping until the postfix-expression is complete. */
3848 while (true)
3850 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3851 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3853 /* It is not a Koenig lookup function call. */
3854 unqualified_name_lookup_error (postfix_expression);
3855 postfix_expression = error_mark_node;
3858 /* Peek at the next token. */
3859 token = cp_lexer_peek_token (parser->lexer);
3861 switch (token->type)
3863 case CPP_OPEN_SQUARE:
3864 /* postfix-expression [ expression ] */
3866 tree index;
3868 /* Consume the `[' token. */
3869 cp_lexer_consume_token (parser->lexer);
3870 /* Parse the index expression. */
3871 index = cp_parser_expression (parser);
3872 /* Look for the closing `]'. */
3873 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3875 /* Build the ARRAY_REF. */
3876 postfix_expression
3877 = grok_array_decl (postfix_expression, index);
3878 idk = CP_PARSER_ID_KIND_NONE;
3880 break;
3882 case CPP_OPEN_PAREN:
3883 /* postfix-expression ( expression-list [opt] ) */
3885 tree args;
3887 /* Consume the `(' token. */
3888 cp_lexer_consume_token (parser->lexer);
3889 /* If the next token is not a `)', then there are some
3890 arguments. */
3891 if (cp_lexer_next_token_is_not (parser->lexer,
3892 CPP_CLOSE_PAREN))
3893 args = cp_parser_expression_list (parser);
3894 else
3895 args = NULL_TREE;
3896 /* Look for the closing `)'. */
3897 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3898 /* Function calls are not permitted in
3899 constant-expressions. */
3900 if (parser->constant_expression_p)
3902 if (!parser->allow_non_constant_expression_p)
3903 return cp_parser_non_constant_expression ("a function call");
3904 parser->non_constant_expression_p = true;
3907 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
3908 && (is_overloaded_fn (postfix_expression)
3909 || DECL_P (postfix_expression)
3910 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3911 && args)
3913 tree arg;
3914 tree identifier = NULL_TREE;
3915 tree functions = NULL_TREE;
3917 /* Find the name of the overloaded function. */
3918 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3919 identifier = postfix_expression;
3920 else if (is_overloaded_fn (postfix_expression))
3922 functions = postfix_expression;
3923 identifier = DECL_NAME (get_first_fn (functions));
3925 else if (DECL_P (postfix_expression))
3927 functions = postfix_expression;
3928 identifier = DECL_NAME (postfix_expression);
3931 /* A call to a namespace-scope function using an
3932 unqualified name.
3934 Do Koenig lookup -- unless any of the arguments are
3935 type-dependent. */
3936 for (arg = args; arg; arg = TREE_CHAIN (arg))
3937 if (type_dependent_expression_p (TREE_VALUE (arg)))
3938 break;
3939 if (!arg)
3941 postfix_expression
3942 = lookup_arg_dependent(identifier, functions, args);
3943 if (!postfix_expression)
3945 /* The unqualified name could not be resolved. */
3946 unqualified_name_lookup_error (identifier);
3947 postfix_expression = error_mark_node;
3949 postfix_expression
3950 = build_call_from_tree (postfix_expression, args,
3951 /*diallow_virtual=*/false);
3952 break;
3954 postfix_expression = build_min_nt (LOOKUP_EXPR,
3955 identifier);
3957 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
3958 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3960 /* The unqualified name could not be resolved. */
3961 unqualified_name_lookup_error (postfix_expression);
3962 postfix_expression = error_mark_node;
3963 break;
3966 /* In the body of a template, no further processing is
3967 required. */
3968 if (processing_template_decl)
3970 postfix_expression = build_nt (CALL_EXPR,
3971 postfix_expression,
3972 args);
3973 break;
3976 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3977 postfix_expression
3978 = (build_new_method_call
3979 (TREE_OPERAND (postfix_expression, 0),
3980 TREE_OPERAND (postfix_expression, 1),
3981 args, NULL_TREE,
3982 (idk == CP_PARSER_ID_KIND_QUALIFIED
3983 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3984 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
3985 postfix_expression = (build_offset_ref_call_from_tree
3986 (postfix_expression, args));
3987 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
3988 /* A call to a static class member, or a namespace-scope
3989 function. */
3990 postfix_expression
3991 = finish_call_expr (postfix_expression, args,
3992 /*disallow_virtual=*/true);
3993 else
3994 /* All other function calls. */
3995 postfix_expression
3996 = finish_call_expr (postfix_expression, args,
3997 /*disallow_virtual=*/false);
3999 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4000 idk = CP_PARSER_ID_KIND_NONE;
4002 break;
4004 case CPP_DOT:
4005 case CPP_DEREF:
4006 /* postfix-expression . template [opt] id-expression
4007 postfix-expression . pseudo-destructor-name
4008 postfix-expression -> template [opt] id-expression
4009 postfix-expression -> pseudo-destructor-name */
4011 tree name;
4012 bool dependent_p;
4013 bool template_p;
4014 tree scope = NULL_TREE;
4016 /* If this is a `->' operator, dereference the pointer. */
4017 if (token->type == CPP_DEREF)
4018 postfix_expression = build_x_arrow (postfix_expression);
4019 /* Check to see whether or not the expression is
4020 type-dependent. */
4021 dependent_p = type_dependent_expression_p (postfix_expression);
4022 /* The identifier following the `->' or `.' is not
4023 qualified. */
4024 parser->scope = NULL_TREE;
4025 parser->qualifying_scope = NULL_TREE;
4026 parser->object_scope = NULL_TREE;
4027 idk = CP_PARSER_ID_KIND_NONE;
4028 /* Enter the scope corresponding to the type of the object
4029 given by the POSTFIX_EXPRESSION. */
4030 if (!dependent_p
4031 && TREE_TYPE (postfix_expression) != NULL_TREE)
4033 scope = TREE_TYPE (postfix_expression);
4034 /* According to the standard, no expression should
4035 ever have reference type. Unfortunately, we do not
4036 currently match the standard in this respect in
4037 that our internal representation of an expression
4038 may have reference type even when the standard says
4039 it does not. Therefore, we have to manually obtain
4040 the underlying type here. */
4041 if (TREE_CODE (scope) == REFERENCE_TYPE)
4042 scope = TREE_TYPE (scope);
4043 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4044 type of the field. We get an OFFSET_TYPE for
4045 something like:
4047 S::T.a ...
4049 Probably, we should not get an OFFSET_TYPE here;
4050 that transformation should be made only if `&S::T'
4051 is written. */
4052 if (TREE_CODE (scope) == OFFSET_TYPE)
4053 scope = TREE_TYPE (scope);
4054 /* The type of the POSTFIX_EXPRESSION must be
4055 complete. */
4056 scope = complete_type_or_else (scope, NULL_TREE);
4057 /* Let the name lookup machinery know that we are
4058 processing a class member access expression. */
4059 parser->context->object_type = scope;
4060 /* If something went wrong, we want to be able to
4061 discern that case, as opposed to the case where
4062 there was no SCOPE due to the type of expression
4063 being dependent. */
4064 if (!scope)
4065 scope = error_mark_node;
4068 /* Consume the `.' or `->' operator. */
4069 cp_lexer_consume_token (parser->lexer);
4070 /* If the SCOPE is not a scalar type, we are looking at an
4071 ordinary class member access expression, rather than a
4072 pseudo-destructor-name. */
4073 if (!scope || !SCALAR_TYPE_P (scope))
4075 template_p = cp_parser_optional_template_keyword (parser);
4076 /* Parse the id-expression. */
4077 name = cp_parser_id_expression (parser,
4078 template_p,
4079 /*check_dependency_p=*/true,
4080 /*template_p=*/NULL);
4081 /* In general, build a SCOPE_REF if the member name is
4082 qualified. However, if the name was not dependent
4083 and has already been resolved; there is no need to
4084 build the SCOPE_REF. For example;
4086 struct X { void f(); };
4087 template <typename T> void f(T* t) { t->X::f(); }
4089 Even though "t" is dependent, "X::f" is not and has
4090 except that for a BASELINK there is no need to
4091 include scope information. */
4093 /* But we do need to remember that there was an explicit
4094 scope for virtual function calls. */
4095 if (parser->scope)
4096 idk = CP_PARSER_ID_KIND_QUALIFIED;
4098 if (name != error_mark_node
4099 && !BASELINK_P (name)
4100 && parser->scope)
4102 name = build_nt (SCOPE_REF, parser->scope, name);
4103 parser->scope = NULL_TREE;
4104 parser->qualifying_scope = NULL_TREE;
4105 parser->object_scope = NULL_TREE;
4107 postfix_expression
4108 = finish_class_member_access_expr (postfix_expression, name);
4110 /* Otherwise, try the pseudo-destructor-name production. */
4111 else
4113 tree s;
4114 tree type;
4116 /* Parse the pseudo-destructor-name. */
4117 cp_parser_pseudo_destructor_name (parser, &s, &type);
4118 /* Form the call. */
4119 postfix_expression
4120 = finish_pseudo_destructor_expr (postfix_expression,
4121 s, TREE_TYPE (type));
4124 /* We no longer need to look up names in the scope of the
4125 object on the left-hand side of the `.' or `->'
4126 operator. */
4127 parser->context->object_type = NULL_TREE;
4129 break;
4131 case CPP_PLUS_PLUS:
4132 /* postfix-expression ++ */
4133 /* Consume the `++' token. */
4134 cp_lexer_consume_token (parser->lexer);
4135 /* Increments may not appear in constant-expressions. */
4136 if (parser->constant_expression_p)
4138 if (!parser->allow_non_constant_expression_p)
4139 return cp_parser_non_constant_expression ("an increment");
4140 parser->non_constant_expression_p = true;
4142 /* Generate a reprsentation for the complete expression. */
4143 postfix_expression
4144 = finish_increment_expr (postfix_expression,
4145 POSTINCREMENT_EXPR);
4146 idk = CP_PARSER_ID_KIND_NONE;
4147 break;
4149 case CPP_MINUS_MINUS:
4150 /* postfix-expression -- */
4151 /* Consume the `--' token. */
4152 cp_lexer_consume_token (parser->lexer);
4153 /* Decrements may not appear in constant-expressions. */
4154 if (parser->constant_expression_p)
4156 if (!parser->allow_non_constant_expression_p)
4157 return cp_parser_non_constant_expression ("a decrement");
4158 parser->non_constant_expression_p = true;
4160 /* Generate a reprsentation for the complete expression. */
4161 postfix_expression
4162 = finish_increment_expr (postfix_expression,
4163 POSTDECREMENT_EXPR);
4164 idk = CP_PARSER_ID_KIND_NONE;
4165 break;
4167 default:
4168 return postfix_expression;
4172 /* We should never get here. */
4173 abort ();
4174 return error_mark_node;
4177 /* Parse an expression-list.
4179 expression-list:
4180 assignment-expression
4181 expression-list, assignment-expression
4183 Returns a TREE_LIST. The TREE_VALUE of each node is a
4184 representation of an assignment-expression. Note that a TREE_LIST
4185 is returned even if there is only a single expression in the list. */
4187 static tree
4188 cp_parser_expression_list (cp_parser* parser)
4190 tree expression_list = NULL_TREE;
4192 /* Consume expressions until there are no more. */
4193 while (true)
4195 tree expr;
4197 /* Parse the next assignment-expression. */
4198 expr = cp_parser_assignment_expression (parser);
4199 /* Add it to the list. */
4200 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4202 /* If the next token isn't a `,', then we are done. */
4203 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4205 /* All uses of expression-list in the grammar are followed
4206 by a `)'. Therefore, if the next token is not a `)' an
4207 error will be issued, unless we are parsing tentatively.
4208 Skip ahead to see if there is another `,' before the `)';
4209 if so, we can go there and recover. */
4210 if (cp_parser_parsing_tentatively (parser)
4211 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4212 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4213 break;
4216 /* Otherwise, consume the `,' and keep going. */
4217 cp_lexer_consume_token (parser->lexer);
4220 /* We built up the list in reverse order so we must reverse it now. */
4221 return nreverse (expression_list);
4224 /* Parse a pseudo-destructor-name.
4226 pseudo-destructor-name:
4227 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4228 :: [opt] nested-name-specifier template template-id :: ~ type-name
4229 :: [opt] nested-name-specifier [opt] ~ type-name
4231 If either of the first two productions is used, sets *SCOPE to the
4232 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4233 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4234 or ERROR_MARK_NODE if no type-name is present. */
4236 static void
4237 cp_parser_pseudo_destructor_name (cp_parser* parser,
4238 tree* scope,
4239 tree* type)
4241 bool nested_name_specifier_p;
4243 /* Look for the optional `::' operator. */
4244 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4245 /* Look for the optional nested-name-specifier. */
4246 nested_name_specifier_p
4247 = (cp_parser_nested_name_specifier_opt (parser,
4248 /*typename_keyword_p=*/false,
4249 /*check_dependency_p=*/true,
4250 /*type_p=*/false)
4251 != NULL_TREE);
4252 /* Now, if we saw a nested-name-specifier, we might be doing the
4253 second production. */
4254 if (nested_name_specifier_p
4255 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4257 /* Consume the `template' keyword. */
4258 cp_lexer_consume_token (parser->lexer);
4259 /* Parse the template-id. */
4260 cp_parser_template_id (parser,
4261 /*template_keyword_p=*/true,
4262 /*check_dependency_p=*/false);
4263 /* Look for the `::' token. */
4264 cp_parser_require (parser, CPP_SCOPE, "`::'");
4266 /* If the next token is not a `~', then there might be some
4267 additional qualification. */
4268 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4270 /* Look for the type-name. */
4271 *scope = TREE_TYPE (cp_parser_type_name (parser));
4272 /* Look for the `::' token. */
4273 cp_parser_require (parser, CPP_SCOPE, "`::'");
4275 else
4276 *scope = NULL_TREE;
4278 /* Look for the `~'. */
4279 cp_parser_require (parser, CPP_COMPL, "`~'");
4280 /* Look for the type-name again. We are not responsible for
4281 checking that it matches the first type-name. */
4282 *type = cp_parser_type_name (parser);
4285 /* Parse a unary-expression.
4287 unary-expression:
4288 postfix-expression
4289 ++ cast-expression
4290 -- cast-expression
4291 unary-operator cast-expression
4292 sizeof unary-expression
4293 sizeof ( type-id )
4294 new-expression
4295 delete-expression
4297 GNU Extensions:
4299 unary-expression:
4300 __extension__ cast-expression
4301 __alignof__ unary-expression
4302 __alignof__ ( type-id )
4303 __real__ cast-expression
4304 __imag__ cast-expression
4305 && identifier
4307 ADDRESS_P is true iff the unary-expression is appearing as the
4308 operand of the `&' operator.
4310 Returns a representation of the expresion. */
4312 static tree
4313 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4315 cp_token *token;
4316 enum tree_code unary_operator;
4318 /* Peek at the next token. */
4319 token = cp_lexer_peek_token (parser->lexer);
4320 /* Some keywords give away the kind of expression. */
4321 if (token->type == CPP_KEYWORD)
4323 enum rid keyword = token->keyword;
4325 switch (keyword)
4327 case RID_ALIGNOF:
4329 /* Consume the `alignof' token. */
4330 cp_lexer_consume_token (parser->lexer);
4331 /* Parse the operand. */
4332 return finish_alignof (cp_parser_sizeof_operand
4333 (parser, keyword));
4336 case RID_SIZEOF:
4338 tree operand;
4340 /* Consume the `sizeof' token. */
4341 cp_lexer_consume_token (parser->lexer);
4342 /* Parse the operand. */
4343 operand = cp_parser_sizeof_operand (parser, keyword);
4345 /* If the type of the operand cannot be determined build a
4346 SIZEOF_EXPR. */
4347 if (TYPE_P (operand)
4348 ? dependent_type_p (operand)
4349 : type_dependent_expression_p (operand))
4350 return build_min (SIZEOF_EXPR, size_type_node, operand);
4351 /* Otherwise, compute the constant value. */
4352 else
4353 return finish_sizeof (operand);
4356 case RID_NEW:
4357 return cp_parser_new_expression (parser);
4359 case RID_DELETE:
4360 return cp_parser_delete_expression (parser);
4362 case RID_EXTENSION:
4364 /* The saved value of the PEDANTIC flag. */
4365 int saved_pedantic;
4366 tree expr;
4368 /* Save away the PEDANTIC flag. */
4369 cp_parser_extension_opt (parser, &saved_pedantic);
4370 /* Parse the cast-expression. */
4371 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4372 /* Restore the PEDANTIC flag. */
4373 pedantic = saved_pedantic;
4375 return expr;
4378 case RID_REALPART:
4379 case RID_IMAGPART:
4381 tree expression;
4383 /* Consume the `__real__' or `__imag__' token. */
4384 cp_lexer_consume_token (parser->lexer);
4385 /* Parse the cast-expression. */
4386 expression = cp_parser_cast_expression (parser,
4387 /*address_p=*/false);
4388 /* Create the complete representation. */
4389 return build_x_unary_op ((keyword == RID_REALPART
4390 ? REALPART_EXPR : IMAGPART_EXPR),
4391 expression);
4393 break;
4395 default:
4396 break;
4400 /* Look for the `:: new' and `:: delete', which also signal the
4401 beginning of a new-expression, or delete-expression,
4402 respectively. If the next token is `::', then it might be one of
4403 these. */
4404 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4406 enum rid keyword;
4408 /* See if the token after the `::' is one of the keywords in
4409 which we're interested. */
4410 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4411 /* If it's `new', we have a new-expression. */
4412 if (keyword == RID_NEW)
4413 return cp_parser_new_expression (parser);
4414 /* Similarly, for `delete'. */
4415 else if (keyword == RID_DELETE)
4416 return cp_parser_delete_expression (parser);
4419 /* Look for a unary operator. */
4420 unary_operator = cp_parser_unary_operator (token);
4421 /* The `++' and `--' operators can be handled similarly, even though
4422 they are not technically unary-operators in the grammar. */
4423 if (unary_operator == ERROR_MARK)
4425 if (token->type == CPP_PLUS_PLUS)
4426 unary_operator = PREINCREMENT_EXPR;
4427 else if (token->type == CPP_MINUS_MINUS)
4428 unary_operator = PREDECREMENT_EXPR;
4429 /* Handle the GNU address-of-label extension. */
4430 else if (cp_parser_allow_gnu_extensions_p (parser)
4431 && token->type == CPP_AND_AND)
4433 tree identifier;
4435 /* Consume the '&&' token. */
4436 cp_lexer_consume_token (parser->lexer);
4437 /* Look for the identifier. */
4438 identifier = cp_parser_identifier (parser);
4439 /* Create an expression representing the address. */
4440 return finish_label_address_expr (identifier);
4443 if (unary_operator != ERROR_MARK)
4445 tree cast_expression;
4447 /* Consume the operator token. */
4448 token = cp_lexer_consume_token (parser->lexer);
4449 /* Parse the cast-expression. */
4450 cast_expression
4451 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4452 /* Now, build an appropriate representation. */
4453 switch (unary_operator)
4455 case INDIRECT_REF:
4456 return build_x_indirect_ref (cast_expression, "unary *");
4458 case ADDR_EXPR:
4459 return build_x_unary_op (ADDR_EXPR, cast_expression);
4461 case PREINCREMENT_EXPR:
4462 case PREDECREMENT_EXPR:
4463 if (parser->constant_expression_p)
4465 if (!parser->allow_non_constant_expression_p)
4466 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4467 ? "an increment"
4468 : "a decrement");
4469 parser->non_constant_expression_p = true;
4471 /* Fall through. */
4472 case CONVERT_EXPR:
4473 case NEGATE_EXPR:
4474 case TRUTH_NOT_EXPR:
4475 return finish_unary_op_expr (unary_operator, cast_expression);
4477 case BIT_NOT_EXPR:
4478 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4480 default:
4481 abort ();
4482 return error_mark_node;
4486 return cp_parser_postfix_expression (parser, address_p);
4489 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4490 unary-operator, the corresponding tree code is returned. */
4492 static enum tree_code
4493 cp_parser_unary_operator (cp_token* token)
4495 switch (token->type)
4497 case CPP_MULT:
4498 return INDIRECT_REF;
4500 case CPP_AND:
4501 return ADDR_EXPR;
4503 case CPP_PLUS:
4504 return CONVERT_EXPR;
4506 case CPP_MINUS:
4507 return NEGATE_EXPR;
4509 case CPP_NOT:
4510 return TRUTH_NOT_EXPR;
4512 case CPP_COMPL:
4513 return BIT_NOT_EXPR;
4515 default:
4516 return ERROR_MARK;
4520 /* Parse a new-expression.
4522 new-expression:
4523 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4524 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4526 Returns a representation of the expression. */
4528 static tree
4529 cp_parser_new_expression (cp_parser* parser)
4531 bool global_scope_p;
4532 tree placement;
4533 tree type;
4534 tree initializer;
4536 /* Look for the optional `::' operator. */
4537 global_scope_p
4538 = (cp_parser_global_scope_opt (parser,
4539 /*current_scope_valid_p=*/false)
4540 != NULL_TREE);
4541 /* Look for the `new' operator. */
4542 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4543 /* There's no easy way to tell a new-placement from the
4544 `( type-id )' construct. */
4545 cp_parser_parse_tentatively (parser);
4546 /* Look for a new-placement. */
4547 placement = cp_parser_new_placement (parser);
4548 /* If that didn't work out, there's no new-placement. */
4549 if (!cp_parser_parse_definitely (parser))
4550 placement = NULL_TREE;
4552 /* If the next token is a `(', then we have a parenthesized
4553 type-id. */
4554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4556 /* Consume the `('. */
4557 cp_lexer_consume_token (parser->lexer);
4558 /* Parse the type-id. */
4559 type = cp_parser_type_id (parser);
4560 /* Look for the closing `)'. */
4561 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4563 /* Otherwise, there must be a new-type-id. */
4564 else
4565 type = cp_parser_new_type_id (parser);
4567 /* If the next token is a `(', then we have a new-initializer. */
4568 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4569 initializer = cp_parser_new_initializer (parser);
4570 else
4571 initializer = NULL_TREE;
4573 /* Create a representation of the new-expression. */
4574 return build_new (placement, type, initializer, global_scope_p);
4577 /* Parse a new-placement.
4579 new-placement:
4580 ( expression-list )
4582 Returns the same representation as for an expression-list. */
4584 static tree
4585 cp_parser_new_placement (cp_parser* parser)
4587 tree expression_list;
4589 /* Look for the opening `('. */
4590 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4591 return error_mark_node;
4592 /* Parse the expression-list. */
4593 expression_list = cp_parser_expression_list (parser);
4594 /* Look for the closing `)'. */
4595 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4597 return expression_list;
4600 /* Parse a new-type-id.
4602 new-type-id:
4603 type-specifier-seq new-declarator [opt]
4605 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4606 and whose TREE_VALUE is the new-declarator. */
4608 static tree
4609 cp_parser_new_type_id (cp_parser* parser)
4611 tree type_specifier_seq;
4612 tree declarator;
4613 const char *saved_message;
4615 /* The type-specifier sequence must not contain type definitions.
4616 (It cannot contain declarations of new types either, but if they
4617 are not definitions we will catch that because they are not
4618 complete.) */
4619 saved_message = parser->type_definition_forbidden_message;
4620 parser->type_definition_forbidden_message
4621 = "types may not be defined in a new-type-id";
4622 /* Parse the type-specifier-seq. */
4623 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4624 /* Restore the old message. */
4625 parser->type_definition_forbidden_message = saved_message;
4626 /* Parse the new-declarator. */
4627 declarator = cp_parser_new_declarator_opt (parser);
4629 return build_tree_list (type_specifier_seq, declarator);
4632 /* Parse an (optional) new-declarator.
4634 new-declarator:
4635 ptr-operator new-declarator [opt]
4636 direct-new-declarator
4638 Returns a representation of the declarator. See
4639 cp_parser_declarator for the representations used. */
4641 static tree
4642 cp_parser_new_declarator_opt (cp_parser* parser)
4644 enum tree_code code;
4645 tree type;
4646 tree cv_qualifier_seq;
4648 /* We don't know if there's a ptr-operator next, or not. */
4649 cp_parser_parse_tentatively (parser);
4650 /* Look for a ptr-operator. */
4651 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4652 /* If that worked, look for more new-declarators. */
4653 if (cp_parser_parse_definitely (parser))
4655 tree declarator;
4657 /* Parse another optional declarator. */
4658 declarator = cp_parser_new_declarator_opt (parser);
4660 /* Create the representation of the declarator. */
4661 if (code == INDIRECT_REF)
4662 declarator = make_pointer_declarator (cv_qualifier_seq,
4663 declarator);
4664 else
4665 declarator = make_reference_declarator (cv_qualifier_seq,
4666 declarator);
4668 /* Handle the pointer-to-member case. */
4669 if (type)
4670 declarator = build_nt (SCOPE_REF, type, declarator);
4672 return declarator;
4675 /* If the next token is a `[', there is a direct-new-declarator. */
4676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4677 return cp_parser_direct_new_declarator (parser);
4679 return NULL_TREE;
4682 /* Parse a direct-new-declarator.
4684 direct-new-declarator:
4685 [ expression ]
4686 direct-new-declarator [constant-expression]
4688 Returns an ARRAY_REF, following the same conventions as are
4689 documented for cp_parser_direct_declarator. */
4691 static tree
4692 cp_parser_direct_new_declarator (cp_parser* parser)
4694 tree declarator = NULL_TREE;
4696 while (true)
4698 tree expression;
4700 /* Look for the opening `['. */
4701 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4702 /* The first expression is not required to be constant. */
4703 if (!declarator)
4705 expression = cp_parser_expression (parser);
4706 /* The standard requires that the expression have integral
4707 type. DR 74 adds enumeration types. We believe that the
4708 real intent is that these expressions be handled like the
4709 expression in a `switch' condition, which also allows
4710 classes with a single conversion to integral or
4711 enumeration type. */
4712 if (!processing_template_decl)
4714 expression
4715 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4716 expression,
4717 /*complain=*/true);
4718 if (!expression)
4720 error ("expression in new-declarator must have integral or enumeration type");
4721 expression = error_mark_node;
4725 /* But all the other expressions must be. */
4726 else
4727 expression
4728 = cp_parser_constant_expression (parser,
4729 /*allow_non_constant=*/false,
4730 NULL);
4731 /* Look for the closing `]'. */
4732 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4734 /* Add this bound to the declarator. */
4735 declarator = build_nt (ARRAY_REF, declarator, expression);
4737 /* If the next token is not a `[', then there are no more
4738 bounds. */
4739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4740 break;
4743 return declarator;
4746 /* Parse a new-initializer.
4748 new-initializer:
4749 ( expression-list [opt] )
4751 Returns a reprsentation of the expression-list. If there is no
4752 expression-list, VOID_ZERO_NODE is returned. */
4754 static tree
4755 cp_parser_new_initializer (cp_parser* parser)
4757 tree expression_list;
4759 /* Look for the opening parenthesis. */
4760 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4761 /* If the next token is not a `)', then there is an
4762 expression-list. */
4763 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4764 expression_list = cp_parser_expression_list (parser);
4765 else
4766 expression_list = void_zero_node;
4767 /* Look for the closing parenthesis. */
4768 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4770 return expression_list;
4773 /* Parse a delete-expression.
4775 delete-expression:
4776 :: [opt] delete cast-expression
4777 :: [opt] delete [ ] cast-expression
4779 Returns a representation of the expression. */
4781 static tree
4782 cp_parser_delete_expression (cp_parser* parser)
4784 bool global_scope_p;
4785 bool array_p;
4786 tree expression;
4788 /* Look for the optional `::' operator. */
4789 global_scope_p
4790 = (cp_parser_global_scope_opt (parser,
4791 /*current_scope_valid_p=*/false)
4792 != NULL_TREE);
4793 /* Look for the `delete' keyword. */
4794 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4795 /* See if the array syntax is in use. */
4796 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4798 /* Consume the `[' token. */
4799 cp_lexer_consume_token (parser->lexer);
4800 /* Look for the `]' token. */
4801 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4802 /* Remember that this is the `[]' construct. */
4803 array_p = true;
4805 else
4806 array_p = false;
4808 /* Parse the cast-expression. */
4809 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
4811 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4814 /* Parse a cast-expression.
4816 cast-expression:
4817 unary-expression
4818 ( type-id ) cast-expression
4820 Returns a representation of the expression. */
4822 static tree
4823 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4825 /* If it's a `(', then we might be looking at a cast. */
4826 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4828 tree type = NULL_TREE;
4829 tree expr = NULL_TREE;
4830 bool compound_literal_p;
4831 const char *saved_message;
4833 /* There's no way to know yet whether or not this is a cast.
4834 For example, `(int (3))' is a unary-expression, while `(int)
4835 3' is a cast. So, we resort to parsing tentatively. */
4836 cp_parser_parse_tentatively (parser);
4837 /* Types may not be defined in a cast. */
4838 saved_message = parser->type_definition_forbidden_message;
4839 parser->type_definition_forbidden_message
4840 = "types may not be defined in casts";
4841 /* Consume the `('. */
4842 cp_lexer_consume_token (parser->lexer);
4843 /* A very tricky bit is that `(struct S) { 3 }' is a
4844 compound-literal (which we permit in C++ as an extension).
4845 But, that construct is not a cast-expression -- it is a
4846 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4847 is legal; if the compound-literal were a cast-expression,
4848 you'd need an extra set of parentheses.) But, if we parse
4849 the type-id, and it happens to be a class-specifier, then we
4850 will commit to the parse at that point, because we cannot
4851 undo the action that is done when creating a new class. So,
4852 then we cannot back up and do a postfix-expression.
4854 Therefore, we scan ahead to the closing `)', and check to see
4855 if the token after the `)' is a `{'. If so, we are not
4856 looking at a cast-expression.
4858 Save tokens so that we can put them back. */
4859 cp_lexer_save_tokens (parser->lexer);
4860 /* Skip tokens until the next token is a closing parenthesis.
4861 If we find the closing `)', and the next token is a `{', then
4862 we are looking at a compound-literal. */
4863 compound_literal_p
4864 = (cp_parser_skip_to_closing_parenthesis (parser)
4865 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4866 /* Roll back the tokens we skipped. */
4867 cp_lexer_rollback_tokens (parser->lexer);
4868 /* If we were looking at a compound-literal, simulate an error
4869 so that the call to cp_parser_parse_definitely below will
4870 fail. */
4871 if (compound_literal_p)
4872 cp_parser_simulate_error (parser);
4873 else
4875 /* Look for the type-id. */
4876 type = cp_parser_type_id (parser);
4877 /* Look for the closing `)'. */
4878 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4881 /* Restore the saved message. */
4882 parser->type_definition_forbidden_message = saved_message;
4884 /* If ok so far, parse the dependent expression. We cannot be
4885 sure it is a cast. Consider `(T ())'. It is a parenthesized
4886 ctor of T, but looks like a cast to function returning T
4887 without a dependent expression. */
4888 if (!cp_parser_error_occurred (parser))
4889 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4891 if (cp_parser_parse_definitely (parser))
4893 /* Warn about old-style casts, if so requested. */
4894 if (warn_old_style_cast
4895 && !in_system_header
4896 && !VOID_TYPE_P (type)
4897 && current_lang_name != lang_name_c)
4898 warning ("use of old-style cast");
4900 /* Only type conversions to integral or enumeration types
4901 can be used in constant-expressions. */
4902 if (parser->constant_expression_p
4903 && !dependent_type_p (type)
4904 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4906 if (!parser->allow_non_constant_expression_p)
4907 return (cp_parser_non_constant_expression
4908 ("a casts to a type other than an integral or "
4909 "enumeration type"));
4910 parser->non_constant_expression_p = true;
4912 /* Perform the cast. */
4913 expr = build_c_cast (type, expr);
4914 return expr;
4918 /* If we get here, then it's not a cast, so it must be a
4919 unary-expression. */
4920 return cp_parser_unary_expression (parser, address_p);
4923 /* Parse a pm-expression.
4925 pm-expression:
4926 cast-expression
4927 pm-expression .* cast-expression
4928 pm-expression ->* cast-expression
4930 Returns a representation of the expression. */
4932 static tree
4933 cp_parser_pm_expression (cp_parser* parser)
4935 tree cast_expr;
4936 tree pm_expr;
4938 /* Parse the cast-expresion. */
4939 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4940 pm_expr = cast_expr;
4941 /* Now look for pointer-to-member operators. */
4942 while (true)
4944 cp_token *token;
4945 enum cpp_ttype token_type;
4947 /* Peek at the next token. */
4948 token = cp_lexer_peek_token (parser->lexer);
4949 token_type = token->type;
4950 /* If it's not `.*' or `->*' there's no pointer-to-member
4951 operation. */
4952 if (token_type != CPP_DOT_STAR
4953 && token_type != CPP_DEREF_STAR)
4954 break;
4956 /* Consume the token. */
4957 cp_lexer_consume_token (parser->lexer);
4959 /* Parse another cast-expression. */
4960 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4962 /* Build the representation of the pointer-to-member
4963 operation. */
4964 if (token_type == CPP_DEREF_STAR)
4965 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
4966 else
4967 pm_expr = build_m_component_ref (pm_expr, cast_expr);
4970 return pm_expr;
4973 /* Parse a multiplicative-expression.
4975 mulitplicative-expression:
4976 pm-expression
4977 multiplicative-expression * pm-expression
4978 multiplicative-expression / pm-expression
4979 multiplicative-expression % pm-expression
4981 Returns a representation of the expression. */
4983 static tree
4984 cp_parser_multiplicative_expression (cp_parser* parser)
4986 static const cp_parser_token_tree_map map = {
4987 { CPP_MULT, MULT_EXPR },
4988 { CPP_DIV, TRUNC_DIV_EXPR },
4989 { CPP_MOD, TRUNC_MOD_EXPR },
4990 { CPP_EOF, ERROR_MARK }
4993 return cp_parser_binary_expression (parser,
4994 map,
4995 cp_parser_pm_expression);
4998 /* Parse an additive-expression.
5000 additive-expression:
5001 multiplicative-expression
5002 additive-expression + multiplicative-expression
5003 additive-expression - multiplicative-expression
5005 Returns a representation of the expression. */
5007 static tree
5008 cp_parser_additive_expression (cp_parser* parser)
5010 static const cp_parser_token_tree_map map = {
5011 { CPP_PLUS, PLUS_EXPR },
5012 { CPP_MINUS, MINUS_EXPR },
5013 { CPP_EOF, ERROR_MARK }
5016 return cp_parser_binary_expression (parser,
5017 map,
5018 cp_parser_multiplicative_expression);
5021 /* Parse a shift-expression.
5023 shift-expression:
5024 additive-expression
5025 shift-expression << additive-expression
5026 shift-expression >> additive-expression
5028 Returns a representation of the expression. */
5030 static tree
5031 cp_parser_shift_expression (cp_parser* parser)
5033 static const cp_parser_token_tree_map map = {
5034 { CPP_LSHIFT, LSHIFT_EXPR },
5035 { CPP_RSHIFT, RSHIFT_EXPR },
5036 { CPP_EOF, ERROR_MARK }
5039 return cp_parser_binary_expression (parser,
5040 map,
5041 cp_parser_additive_expression);
5044 /* Parse a relational-expression.
5046 relational-expression:
5047 shift-expression
5048 relational-expression < shift-expression
5049 relational-expression > shift-expression
5050 relational-expression <= shift-expression
5051 relational-expression >= shift-expression
5053 GNU Extension:
5055 relational-expression:
5056 relational-expression <? shift-expression
5057 relational-expression >? shift-expression
5059 Returns a representation of the expression. */
5061 static tree
5062 cp_parser_relational_expression (cp_parser* parser)
5064 static const cp_parser_token_tree_map map = {
5065 { CPP_LESS, LT_EXPR },
5066 { CPP_GREATER, GT_EXPR },
5067 { CPP_LESS_EQ, LE_EXPR },
5068 { CPP_GREATER_EQ, GE_EXPR },
5069 { CPP_MIN, MIN_EXPR },
5070 { CPP_MAX, MAX_EXPR },
5071 { CPP_EOF, ERROR_MARK }
5074 return cp_parser_binary_expression (parser,
5075 map,
5076 cp_parser_shift_expression);
5079 /* Parse an equality-expression.
5081 equality-expression:
5082 relational-expression
5083 equality-expression == relational-expression
5084 equality-expression != relational-expression
5086 Returns a representation of the expression. */
5088 static tree
5089 cp_parser_equality_expression (cp_parser* parser)
5091 static const cp_parser_token_tree_map map = {
5092 { CPP_EQ_EQ, EQ_EXPR },
5093 { CPP_NOT_EQ, NE_EXPR },
5094 { CPP_EOF, ERROR_MARK }
5097 return cp_parser_binary_expression (parser,
5098 map,
5099 cp_parser_relational_expression);
5102 /* Parse an and-expression.
5104 and-expression:
5105 equality-expression
5106 and-expression & equality-expression
5108 Returns a representation of the expression. */
5110 static tree
5111 cp_parser_and_expression (cp_parser* parser)
5113 static const cp_parser_token_tree_map map = {
5114 { CPP_AND, BIT_AND_EXPR },
5115 { CPP_EOF, ERROR_MARK }
5118 return cp_parser_binary_expression (parser,
5119 map,
5120 cp_parser_equality_expression);
5123 /* Parse an exclusive-or-expression.
5125 exclusive-or-expression:
5126 and-expression
5127 exclusive-or-expression ^ and-expression
5129 Returns a representation of the expression. */
5131 static tree
5132 cp_parser_exclusive_or_expression (cp_parser* parser)
5134 static const cp_parser_token_tree_map map = {
5135 { CPP_XOR, BIT_XOR_EXPR },
5136 { CPP_EOF, ERROR_MARK }
5139 return cp_parser_binary_expression (parser,
5140 map,
5141 cp_parser_and_expression);
5145 /* Parse an inclusive-or-expression.
5147 inclusive-or-expression:
5148 exclusive-or-expression
5149 inclusive-or-expression | exclusive-or-expression
5151 Returns a representation of the expression. */
5153 static tree
5154 cp_parser_inclusive_or_expression (cp_parser* parser)
5156 static const cp_parser_token_tree_map map = {
5157 { CPP_OR, BIT_IOR_EXPR },
5158 { CPP_EOF, ERROR_MARK }
5161 return cp_parser_binary_expression (parser,
5162 map,
5163 cp_parser_exclusive_or_expression);
5166 /* Parse a logical-and-expression.
5168 logical-and-expression:
5169 inclusive-or-expression
5170 logical-and-expression && inclusive-or-expression
5172 Returns a representation of the expression. */
5174 static tree
5175 cp_parser_logical_and_expression (cp_parser* parser)
5177 static const cp_parser_token_tree_map map = {
5178 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5179 { CPP_EOF, ERROR_MARK }
5182 return cp_parser_binary_expression (parser,
5183 map,
5184 cp_parser_inclusive_or_expression);
5187 /* Parse a logical-or-expression.
5189 logical-or-expression:
5190 logical-and-expresion
5191 logical-or-expression || logical-and-expression
5193 Returns a representation of the expression. */
5195 static tree
5196 cp_parser_logical_or_expression (cp_parser* parser)
5198 static const cp_parser_token_tree_map map = {
5199 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5200 { CPP_EOF, ERROR_MARK }
5203 return cp_parser_binary_expression (parser,
5204 map,
5205 cp_parser_logical_and_expression);
5208 /* Parse a conditional-expression.
5210 conditional-expression:
5211 logical-or-expression
5212 logical-or-expression ? expression : assignment-expression
5214 GNU Extensions:
5216 conditional-expression:
5217 logical-or-expression ? : assignment-expression
5219 Returns a representation of the expression. */
5221 static tree
5222 cp_parser_conditional_expression (cp_parser* parser)
5224 tree logical_or_expr;
5226 /* Parse the logical-or-expression. */
5227 logical_or_expr = cp_parser_logical_or_expression (parser);
5228 /* If the next token is a `?', then we have a real conditional
5229 expression. */
5230 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5231 return cp_parser_question_colon_clause (parser, logical_or_expr);
5232 /* Otherwise, the value is simply the logical-or-expression. */
5233 else
5234 return logical_or_expr;
5237 /* Parse the `? expression : assignment-expression' part of a
5238 conditional-expression. The LOGICAL_OR_EXPR is the
5239 logical-or-expression that started the conditional-expression.
5240 Returns a representation of the entire conditional-expression.
5242 This routine exists only so that it can be shared between
5243 cp_parser_conditional_expression and
5244 cp_parser_assignment_expression.
5246 ? expression : assignment-expression
5248 GNU Extensions:
5250 ? : assignment-expression */
5252 static tree
5253 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5255 tree expr;
5256 tree assignment_expr;
5258 /* Consume the `?' token. */
5259 cp_lexer_consume_token (parser->lexer);
5260 if (cp_parser_allow_gnu_extensions_p (parser)
5261 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5262 /* Implicit true clause. */
5263 expr = NULL_TREE;
5264 else
5265 /* Parse the expression. */
5266 expr = cp_parser_expression (parser);
5268 /* The next token should be a `:'. */
5269 cp_parser_require (parser, CPP_COLON, "`:'");
5270 /* Parse the assignment-expression. */
5271 assignment_expr = cp_parser_assignment_expression (parser);
5273 /* Build the conditional-expression. */
5274 return build_x_conditional_expr (logical_or_expr,
5275 expr,
5276 assignment_expr);
5279 /* Parse an assignment-expression.
5281 assignment-expression:
5282 conditional-expression
5283 logical-or-expression assignment-operator assignment_expression
5284 throw-expression
5286 Returns a representation for the expression. */
5288 static tree
5289 cp_parser_assignment_expression (cp_parser* parser)
5291 tree expr;
5293 /* If the next token is the `throw' keyword, then we're looking at
5294 a throw-expression. */
5295 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5296 expr = cp_parser_throw_expression (parser);
5297 /* Otherwise, it must be that we are looking at a
5298 logical-or-expression. */
5299 else
5301 /* Parse the logical-or-expression. */
5302 expr = cp_parser_logical_or_expression (parser);
5303 /* If the next token is a `?' then we're actually looking at a
5304 conditional-expression. */
5305 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5306 return cp_parser_question_colon_clause (parser, expr);
5307 else
5309 enum tree_code assignment_operator;
5311 /* If it's an assignment-operator, we're using the second
5312 production. */
5313 assignment_operator
5314 = cp_parser_assignment_operator_opt (parser);
5315 if (assignment_operator != ERROR_MARK)
5317 tree rhs;
5319 /* Parse the right-hand side of the assignment. */
5320 rhs = cp_parser_assignment_expression (parser);
5321 /* An assignment may not appear in a
5322 constant-expression. */
5323 if (parser->constant_expression_p)
5325 if (!parser->allow_non_constant_expression_p)
5326 return cp_parser_non_constant_expression ("an assignment");
5327 parser->non_constant_expression_p = true;
5329 /* Build the asignment expression. */
5330 expr = build_x_modify_expr (expr,
5331 assignment_operator,
5332 rhs);
5337 return expr;
5340 /* Parse an (optional) assignment-operator.
5342 assignment-operator: one of
5343 = *= /= %= += -= >>= <<= &= ^= |=
5345 GNU Extension:
5347 assignment-operator: one of
5348 <?= >?=
5350 If the next token is an assignment operator, the corresponding tree
5351 code is returned, and the token is consumed. For example, for
5352 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5353 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5354 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5355 operator, ERROR_MARK is returned. */
5357 static enum tree_code
5358 cp_parser_assignment_operator_opt (cp_parser* parser)
5360 enum tree_code op;
5361 cp_token *token;
5363 /* Peek at the next toen. */
5364 token = cp_lexer_peek_token (parser->lexer);
5366 switch (token->type)
5368 case CPP_EQ:
5369 op = NOP_EXPR;
5370 break;
5372 case CPP_MULT_EQ:
5373 op = MULT_EXPR;
5374 break;
5376 case CPP_DIV_EQ:
5377 op = TRUNC_DIV_EXPR;
5378 break;
5380 case CPP_MOD_EQ:
5381 op = TRUNC_MOD_EXPR;
5382 break;
5384 case CPP_PLUS_EQ:
5385 op = PLUS_EXPR;
5386 break;
5388 case CPP_MINUS_EQ:
5389 op = MINUS_EXPR;
5390 break;
5392 case CPP_RSHIFT_EQ:
5393 op = RSHIFT_EXPR;
5394 break;
5396 case CPP_LSHIFT_EQ:
5397 op = LSHIFT_EXPR;
5398 break;
5400 case CPP_AND_EQ:
5401 op = BIT_AND_EXPR;
5402 break;
5404 case CPP_XOR_EQ:
5405 op = BIT_XOR_EXPR;
5406 break;
5408 case CPP_OR_EQ:
5409 op = BIT_IOR_EXPR;
5410 break;
5412 case CPP_MIN_EQ:
5413 op = MIN_EXPR;
5414 break;
5416 case CPP_MAX_EQ:
5417 op = MAX_EXPR;
5418 break;
5420 default:
5421 /* Nothing else is an assignment operator. */
5422 op = ERROR_MARK;
5425 /* If it was an assignment operator, consume it. */
5426 if (op != ERROR_MARK)
5427 cp_lexer_consume_token (parser->lexer);
5429 return op;
5432 /* Parse an expression.
5434 expression:
5435 assignment-expression
5436 expression , assignment-expression
5438 Returns a representation of the expression. */
5440 static tree
5441 cp_parser_expression (cp_parser* parser)
5443 tree expression = NULL_TREE;
5444 bool saw_comma_p = false;
5446 while (true)
5448 tree assignment_expression;
5450 /* Parse the next assignment-expression. */
5451 assignment_expression
5452 = cp_parser_assignment_expression (parser);
5453 /* If this is the first assignment-expression, we can just
5454 save it away. */
5455 if (!expression)
5456 expression = assignment_expression;
5457 /* Otherwise, chain the expressions together. It is unclear why
5458 we do not simply build COMPOUND_EXPRs as we go. */
5459 else
5460 expression = tree_cons (NULL_TREE,
5461 assignment_expression,
5462 expression);
5463 /* If the next token is not a comma, then we are done with the
5464 expression. */
5465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5466 break;
5467 /* Consume the `,'. */
5468 cp_lexer_consume_token (parser->lexer);
5469 /* The first time we see a `,', we must take special action
5470 because the representation used for a single expression is
5471 different from that used for a list containing the single
5472 expression. */
5473 if (!saw_comma_p)
5475 /* Remember that this expression has a `,' in it. */
5476 saw_comma_p = true;
5477 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5478 additional expressions to it. */
5479 expression = build_tree_list (NULL_TREE, expression);
5483 /* Build a COMPOUND_EXPR to represent the entire expression, if
5484 necessary. We built up the list in reverse order, so we must
5485 straighten it out here. */
5486 if (saw_comma_p)
5488 /* A comma operator cannot appear in a constant-expression. */
5489 if (parser->constant_expression_p)
5491 if (!parser->allow_non_constant_expression_p)
5492 return cp_parser_non_constant_expression ("a comma operator");
5493 parser->non_constant_expression_p = true;
5495 expression = build_x_compound_expr (nreverse (expression));
5498 return expression;
5501 /* Parse a constant-expression.
5503 constant-expression:
5504 conditional-expression
5506 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5507 accepted. In that case *NON_CONSTANT_P is set to TRUE. If
5508 ALLOW_NON_CONSTANT_P is false, NON_CONSTANT_P should be NULL. */
5510 static tree
5511 cp_parser_constant_expression (cp_parser* parser,
5512 bool allow_non_constant_p,
5513 bool *non_constant_p)
5515 bool saved_constant_expression_p;
5516 bool saved_allow_non_constant_expression_p;
5517 bool saved_non_constant_expression_p;
5518 tree expression;
5520 /* It might seem that we could simply parse the
5521 conditional-expression, and then check to see if it were
5522 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5523 one that the compiler can figure out is constant, possibly after
5524 doing some simplifications or optimizations. The standard has a
5525 precise definition of constant-expression, and we must honor
5526 that, even though it is somewhat more restrictive.
5528 For example:
5530 int i[(2, 3)];
5532 is not a legal declaration, because `(2, 3)' is not a
5533 constant-expression. The `,' operator is forbidden in a
5534 constant-expression. However, GCC's constant-folding machinery
5535 will fold this operation to an INTEGER_CST for `3'. */
5537 /* Save the old settings. */
5538 saved_constant_expression_p = parser->constant_expression_p;
5539 saved_allow_non_constant_expression_p
5540 = parser->allow_non_constant_expression_p;
5541 saved_non_constant_expression_p = parser->non_constant_expression_p;
5542 /* We are now parsing a constant-expression. */
5543 parser->constant_expression_p = true;
5544 parser->allow_non_constant_expression_p = allow_non_constant_p;
5545 parser->non_constant_expression_p = false;
5546 /* Parse the conditional-expression. */
5547 expression = cp_parser_conditional_expression (parser);
5548 /* Restore the old settings. */
5549 parser->constant_expression_p = saved_constant_expression_p;
5550 parser->allow_non_constant_expression_p
5551 = saved_allow_non_constant_expression_p;
5552 if (allow_non_constant_p)
5553 *non_constant_p = parser->non_constant_expression_p;
5554 parser->non_constant_expression_p = saved_non_constant_expression_p;
5556 return expression;
5559 /* Statements [gram.stmt.stmt] */
5561 /* Parse a statement.
5563 statement:
5564 labeled-statement
5565 expression-statement
5566 compound-statement
5567 selection-statement
5568 iteration-statement
5569 jump-statement
5570 declaration-statement
5571 try-block */
5573 static void
5574 cp_parser_statement (cp_parser* parser)
5576 tree statement;
5577 cp_token *token;
5578 int statement_line_number;
5580 /* There is no statement yet. */
5581 statement = NULL_TREE;
5582 /* Peek at the next token. */
5583 token = cp_lexer_peek_token (parser->lexer);
5584 /* Remember the line number of the first token in the statement. */
5585 statement_line_number = token->location.line;
5586 /* If this is a keyword, then that will often determine what kind of
5587 statement we have. */
5588 if (token->type == CPP_KEYWORD)
5590 enum rid keyword = token->keyword;
5592 switch (keyword)
5594 case RID_CASE:
5595 case RID_DEFAULT:
5596 statement = cp_parser_labeled_statement (parser);
5597 break;
5599 case RID_IF:
5600 case RID_SWITCH:
5601 statement = cp_parser_selection_statement (parser);
5602 break;
5604 case RID_WHILE:
5605 case RID_DO:
5606 case RID_FOR:
5607 statement = cp_parser_iteration_statement (parser);
5608 break;
5610 case RID_BREAK:
5611 case RID_CONTINUE:
5612 case RID_RETURN:
5613 case RID_GOTO:
5614 statement = cp_parser_jump_statement (parser);
5615 break;
5617 case RID_TRY:
5618 statement = cp_parser_try_block (parser);
5619 break;
5621 default:
5622 /* It might be a keyword like `int' that can start a
5623 declaration-statement. */
5624 break;
5627 else if (token->type == CPP_NAME)
5629 /* If the next token is a `:', then we are looking at a
5630 labeled-statement. */
5631 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5632 if (token->type == CPP_COLON)
5633 statement = cp_parser_labeled_statement (parser);
5635 /* Anything that starts with a `{' must be a compound-statement. */
5636 else if (token->type == CPP_OPEN_BRACE)
5637 statement = cp_parser_compound_statement (parser);
5639 /* Everything else must be a declaration-statement or an
5640 expression-statement. Try for the declaration-statement
5641 first, unless we are looking at a `;', in which case we know that
5642 we have an expression-statement. */
5643 if (!statement)
5645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5647 cp_parser_parse_tentatively (parser);
5648 /* Try to parse the declaration-statement. */
5649 cp_parser_declaration_statement (parser);
5650 /* If that worked, we're done. */
5651 if (cp_parser_parse_definitely (parser))
5652 return;
5654 /* Look for an expression-statement instead. */
5655 statement = cp_parser_expression_statement (parser);
5658 /* Set the line number for the statement. */
5659 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5660 STMT_LINENO (statement) = statement_line_number;
5663 /* Parse a labeled-statement.
5665 labeled-statement:
5666 identifier : statement
5667 case constant-expression : statement
5668 default : statement
5670 Returns the new CASE_LABEL, for a `case' or `default' label. For
5671 an ordinary label, returns a LABEL_STMT. */
5673 static tree
5674 cp_parser_labeled_statement (cp_parser* parser)
5676 cp_token *token;
5677 tree statement = NULL_TREE;
5679 /* The next token should be an identifier. */
5680 token = cp_lexer_peek_token (parser->lexer);
5681 if (token->type != CPP_NAME
5682 && token->type != CPP_KEYWORD)
5684 cp_parser_error (parser, "expected labeled-statement");
5685 return error_mark_node;
5688 switch (token->keyword)
5690 case RID_CASE:
5692 tree expr;
5694 /* Consume the `case' token. */
5695 cp_lexer_consume_token (parser->lexer);
5696 /* Parse the constant-expression. */
5697 expr = cp_parser_constant_expression (parser,
5698 /*allow_non_constant=*/false,
5699 NULL);
5700 /* Create the label. */
5701 statement = finish_case_label (expr, NULL_TREE);
5703 break;
5705 case RID_DEFAULT:
5706 /* Consume the `default' token. */
5707 cp_lexer_consume_token (parser->lexer);
5708 /* Create the label. */
5709 statement = finish_case_label (NULL_TREE, NULL_TREE);
5710 break;
5712 default:
5713 /* Anything else must be an ordinary label. */
5714 statement = finish_label_stmt (cp_parser_identifier (parser));
5715 break;
5718 /* Require the `:' token. */
5719 cp_parser_require (parser, CPP_COLON, "`:'");
5720 /* Parse the labeled statement. */
5721 cp_parser_statement (parser);
5723 /* Return the label, in the case of a `case' or `default' label. */
5724 return statement;
5727 /* Parse an expression-statement.
5729 expression-statement:
5730 expression [opt] ;
5732 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5733 statement consists of nothing more than an `;'. */
5735 static tree
5736 cp_parser_expression_statement (cp_parser* parser)
5738 tree statement;
5740 /* If the next token is not a `;', then there is an expression to parse. */
5741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5742 statement = finish_expr_stmt (cp_parser_expression (parser));
5743 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5744 else
5746 finish_stmt ();
5747 statement = NULL_TREE;
5749 /* Consume the final `;'. */
5750 cp_parser_consume_semicolon_at_end_of_statement (parser);
5752 return statement;
5755 /* Parse a compound-statement.
5757 compound-statement:
5758 { statement-seq [opt] }
5760 Returns a COMPOUND_STMT representing the statement. */
5762 static tree
5763 cp_parser_compound_statement (cp_parser *parser)
5765 tree compound_stmt;
5767 /* Consume the `{'. */
5768 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5769 return error_mark_node;
5770 /* Begin the compound-statement. */
5771 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5772 /* Parse an (optional) statement-seq. */
5773 cp_parser_statement_seq_opt (parser);
5774 /* Finish the compound-statement. */
5775 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5776 /* Consume the `}'. */
5777 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5779 return compound_stmt;
5782 /* Parse an (optional) statement-seq.
5784 statement-seq:
5785 statement
5786 statement-seq [opt] statement */
5788 static void
5789 cp_parser_statement_seq_opt (cp_parser* parser)
5791 /* Scan statements until there aren't any more. */
5792 while (true)
5794 /* If we're looking at a `}', then we've run out of statements. */
5795 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5796 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5797 break;
5799 /* Parse the statement. */
5800 cp_parser_statement (parser);
5804 /* Parse a selection-statement.
5806 selection-statement:
5807 if ( condition ) statement
5808 if ( condition ) statement else statement
5809 switch ( condition ) statement
5811 Returns the new IF_STMT or SWITCH_STMT. */
5813 static tree
5814 cp_parser_selection_statement (cp_parser* parser)
5816 cp_token *token;
5817 enum rid keyword;
5819 /* Peek at the next token. */
5820 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5822 /* See what kind of keyword it is. */
5823 keyword = token->keyword;
5824 switch (keyword)
5826 case RID_IF:
5827 case RID_SWITCH:
5829 tree statement;
5830 tree condition;
5832 /* Look for the `('. */
5833 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5835 cp_parser_skip_to_end_of_statement (parser);
5836 return error_mark_node;
5839 /* Begin the selection-statement. */
5840 if (keyword == RID_IF)
5841 statement = begin_if_stmt ();
5842 else
5843 statement = begin_switch_stmt ();
5845 /* Parse the condition. */
5846 condition = cp_parser_condition (parser);
5847 /* Look for the `)'. */
5848 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5849 cp_parser_skip_to_closing_parenthesis (parser);
5851 if (keyword == RID_IF)
5853 tree then_stmt;
5855 /* Add the condition. */
5856 finish_if_stmt_cond (condition, statement);
5858 /* Parse the then-clause. */
5859 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5860 finish_then_clause (statement);
5862 /* If the next token is `else', parse the else-clause. */
5863 if (cp_lexer_next_token_is_keyword (parser->lexer,
5864 RID_ELSE))
5866 tree else_stmt;
5868 /* Consume the `else' keyword. */
5869 cp_lexer_consume_token (parser->lexer);
5870 /* Parse the else-clause. */
5871 else_stmt
5872 = cp_parser_implicitly_scoped_statement (parser);
5873 finish_else_clause (statement);
5876 /* Now we're all done with the if-statement. */
5877 finish_if_stmt ();
5879 else
5881 tree body;
5883 /* Add the condition. */
5884 finish_switch_cond (condition, statement);
5886 /* Parse the body of the switch-statement. */
5887 body = cp_parser_implicitly_scoped_statement (parser);
5889 /* Now we're all done with the switch-statement. */
5890 finish_switch_stmt (statement);
5893 return statement;
5895 break;
5897 default:
5898 cp_parser_error (parser, "expected selection-statement");
5899 return error_mark_node;
5903 /* Parse a condition.
5905 condition:
5906 expression
5907 type-specifier-seq declarator = assignment-expression
5909 GNU Extension:
5911 condition:
5912 type-specifier-seq declarator asm-specification [opt]
5913 attributes [opt] = assignment-expression
5915 Returns the expression that should be tested. */
5917 static tree
5918 cp_parser_condition (cp_parser* parser)
5920 tree type_specifiers;
5921 const char *saved_message;
5923 /* Try the declaration first. */
5924 cp_parser_parse_tentatively (parser);
5925 /* New types are not allowed in the type-specifier-seq for a
5926 condition. */
5927 saved_message = parser->type_definition_forbidden_message;
5928 parser->type_definition_forbidden_message
5929 = "types may not be defined in conditions";
5930 /* Parse the type-specifier-seq. */
5931 type_specifiers = cp_parser_type_specifier_seq (parser);
5932 /* Restore the saved message. */
5933 parser->type_definition_forbidden_message = saved_message;
5934 /* If all is well, we might be looking at a declaration. */
5935 if (!cp_parser_error_occurred (parser))
5937 tree decl;
5938 tree asm_specification;
5939 tree attributes;
5940 tree declarator;
5941 tree initializer = NULL_TREE;
5943 /* Parse the declarator. */
5944 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5945 /*ctor_dtor_or_conv_p=*/NULL);
5946 /* Parse the attributes. */
5947 attributes = cp_parser_attributes_opt (parser);
5948 /* Parse the asm-specification. */
5949 asm_specification = cp_parser_asm_specification_opt (parser);
5950 /* If the next token is not an `=', then we might still be
5951 looking at an expression. For example:
5953 if (A(a).x)
5955 looks like a decl-specifier-seq and a declarator -- but then
5956 there is no `=', so this is an expression. */
5957 cp_parser_require (parser, CPP_EQ, "`='");
5958 /* If we did see an `=', then we are looking at a declaration
5959 for sure. */
5960 if (cp_parser_parse_definitely (parser))
5962 /* Create the declaration. */
5963 decl = start_decl (declarator, type_specifiers,
5964 /*initialized_p=*/true,
5965 attributes, /*prefix_attributes=*/NULL_TREE);
5966 /* Parse the assignment-expression. */
5967 initializer = cp_parser_assignment_expression (parser);
5969 /* Process the initializer. */
5970 cp_finish_decl (decl,
5971 initializer,
5972 asm_specification,
5973 LOOKUP_ONLYCONVERTING);
5975 return convert_from_reference (decl);
5978 /* If we didn't even get past the declarator successfully, we are
5979 definitely not looking at a declaration. */
5980 else
5981 cp_parser_abort_tentative_parse (parser);
5983 /* Otherwise, we are looking at an expression. */
5984 return cp_parser_expression (parser);
5987 /* Parse an iteration-statement.
5989 iteration-statement:
5990 while ( condition ) statement
5991 do statement while ( expression ) ;
5992 for ( for-init-statement condition [opt] ; expression [opt] )
5993 statement
5995 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5997 static tree
5998 cp_parser_iteration_statement (cp_parser* parser)
6000 cp_token *token;
6001 enum rid keyword;
6002 tree statement;
6004 /* Peek at the next token. */
6005 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6006 if (!token)
6007 return error_mark_node;
6009 /* See what kind of keyword it is. */
6010 keyword = token->keyword;
6011 switch (keyword)
6013 case RID_WHILE:
6015 tree condition;
6017 /* Begin the while-statement. */
6018 statement = begin_while_stmt ();
6019 /* Look for the `('. */
6020 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6021 /* Parse the condition. */
6022 condition = cp_parser_condition (parser);
6023 finish_while_stmt_cond (condition, statement);
6024 /* Look for the `)'. */
6025 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6026 /* Parse the dependent statement. */
6027 cp_parser_already_scoped_statement (parser);
6028 /* We're done with the while-statement. */
6029 finish_while_stmt (statement);
6031 break;
6033 case RID_DO:
6035 tree expression;
6037 /* Begin the do-statement. */
6038 statement = begin_do_stmt ();
6039 /* Parse the body of the do-statement. */
6040 cp_parser_implicitly_scoped_statement (parser);
6041 finish_do_body (statement);
6042 /* Look for the `while' keyword. */
6043 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6044 /* Look for the `('. */
6045 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6046 /* Parse the expression. */
6047 expression = cp_parser_expression (parser);
6048 /* We're done with the do-statement. */
6049 finish_do_stmt (expression, statement);
6050 /* Look for the `)'. */
6051 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6052 /* Look for the `;'. */
6053 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6055 break;
6057 case RID_FOR:
6059 tree condition = NULL_TREE;
6060 tree expression = NULL_TREE;
6062 /* Begin the for-statement. */
6063 statement = begin_for_stmt ();
6064 /* Look for the `('. */
6065 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6066 /* Parse the initialization. */
6067 cp_parser_for_init_statement (parser);
6068 finish_for_init_stmt (statement);
6070 /* If there's a condition, process it. */
6071 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6072 condition = cp_parser_condition (parser);
6073 finish_for_cond (condition, statement);
6074 /* Look for the `;'. */
6075 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6077 /* If there's an expression, process it. */
6078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6079 expression = cp_parser_expression (parser);
6080 finish_for_expr (expression, statement);
6081 /* Look for the `)'. */
6082 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6084 /* Parse the body of the for-statement. */
6085 cp_parser_already_scoped_statement (parser);
6087 /* We're done with the for-statement. */
6088 finish_for_stmt (statement);
6090 break;
6092 default:
6093 cp_parser_error (parser, "expected iteration-statement");
6094 statement = error_mark_node;
6095 break;
6098 return statement;
6101 /* Parse a for-init-statement.
6103 for-init-statement:
6104 expression-statement
6105 simple-declaration */
6107 static void
6108 cp_parser_for_init_statement (cp_parser* parser)
6110 /* If the next token is a `;', then we have an empty
6111 expression-statement. Gramatically, this is also a
6112 simple-declaration, but an invalid one, because it does not
6113 declare anything. Therefore, if we did not handle this case
6114 specially, we would issue an error message about an invalid
6115 declaration. */
6116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6118 /* We're going to speculatively look for a declaration, falling back
6119 to an expression, if necessary. */
6120 cp_parser_parse_tentatively (parser);
6121 /* Parse the declaration. */
6122 cp_parser_simple_declaration (parser,
6123 /*function_definition_allowed_p=*/false);
6124 /* If the tentative parse failed, then we shall need to look for an
6125 expression-statement. */
6126 if (cp_parser_parse_definitely (parser))
6127 return;
6130 cp_parser_expression_statement (parser);
6133 /* Parse a jump-statement.
6135 jump-statement:
6136 break ;
6137 continue ;
6138 return expression [opt] ;
6139 goto identifier ;
6141 GNU extension:
6143 jump-statement:
6144 goto * expression ;
6146 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6147 GOTO_STMT. */
6149 static tree
6150 cp_parser_jump_statement (cp_parser* parser)
6152 tree statement = error_mark_node;
6153 cp_token *token;
6154 enum rid keyword;
6156 /* Peek at the next token. */
6157 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6158 if (!token)
6159 return error_mark_node;
6161 /* See what kind of keyword it is. */
6162 keyword = token->keyword;
6163 switch (keyword)
6165 case RID_BREAK:
6166 statement = finish_break_stmt ();
6167 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6168 break;
6170 case RID_CONTINUE:
6171 statement = finish_continue_stmt ();
6172 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6173 break;
6175 case RID_RETURN:
6177 tree expr;
6179 /* If the next token is a `;', then there is no
6180 expression. */
6181 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6182 expr = cp_parser_expression (parser);
6183 else
6184 expr = NULL_TREE;
6185 /* Build the return-statement. */
6186 statement = finish_return_stmt (expr);
6187 /* Look for the final `;'. */
6188 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6190 break;
6192 case RID_GOTO:
6193 /* Create the goto-statement. */
6194 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6196 /* Issue a warning about this use of a GNU extension. */
6197 if (pedantic)
6198 pedwarn ("ISO C++ forbids computed gotos");
6199 /* Consume the '*' token. */
6200 cp_lexer_consume_token (parser->lexer);
6201 /* Parse the dependent expression. */
6202 finish_goto_stmt (cp_parser_expression (parser));
6204 else
6205 finish_goto_stmt (cp_parser_identifier (parser));
6206 /* Look for the final `;'. */
6207 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6208 break;
6210 default:
6211 cp_parser_error (parser, "expected jump-statement");
6212 break;
6215 return statement;
6218 /* Parse a declaration-statement.
6220 declaration-statement:
6221 block-declaration */
6223 static void
6224 cp_parser_declaration_statement (cp_parser* parser)
6226 /* Parse the block-declaration. */
6227 cp_parser_block_declaration (parser, /*statement_p=*/true);
6229 /* Finish off the statement. */
6230 finish_stmt ();
6233 /* Some dependent statements (like `if (cond) statement'), are
6234 implicitly in their own scope. In other words, if the statement is
6235 a single statement (as opposed to a compound-statement), it is
6236 none-the-less treated as if it were enclosed in braces. Any
6237 declarations appearing in the dependent statement are out of scope
6238 after control passes that point. This function parses a statement,
6239 but ensures that is in its own scope, even if it is not a
6240 compound-statement.
6242 Returns the new statement. */
6244 static tree
6245 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6247 tree statement;
6249 /* If the token is not a `{', then we must take special action. */
6250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6252 /* Create a compound-statement. */
6253 statement = begin_compound_stmt (/*has_no_scope=*/0);
6254 /* Parse the dependent-statement. */
6255 cp_parser_statement (parser);
6256 /* Finish the dummy compound-statement. */
6257 finish_compound_stmt (/*has_no_scope=*/0, statement);
6259 /* Otherwise, we simply parse the statement directly. */
6260 else
6261 statement = cp_parser_compound_statement (parser);
6263 /* Return the statement. */
6264 return statement;
6267 /* For some dependent statements (like `while (cond) statement'), we
6268 have already created a scope. Therefore, even if the dependent
6269 statement is a compound-statement, we do not want to create another
6270 scope. */
6272 static void
6273 cp_parser_already_scoped_statement (cp_parser* parser)
6275 /* If the token is not a `{', then we must take special action. */
6276 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6278 tree statement;
6280 /* Create a compound-statement. */
6281 statement = begin_compound_stmt (/*has_no_scope=*/1);
6282 /* Parse the dependent-statement. */
6283 cp_parser_statement (parser);
6284 /* Finish the dummy compound-statement. */
6285 finish_compound_stmt (/*has_no_scope=*/1, statement);
6287 /* Otherwise, we simply parse the statement directly. */
6288 else
6289 cp_parser_statement (parser);
6292 /* Declarations [gram.dcl.dcl] */
6294 /* Parse an optional declaration-sequence.
6296 declaration-seq:
6297 declaration
6298 declaration-seq declaration */
6300 static void
6301 cp_parser_declaration_seq_opt (cp_parser* parser)
6303 while (true)
6305 cp_token *token;
6307 token = cp_lexer_peek_token (parser->lexer);
6309 if (token->type == CPP_CLOSE_BRACE
6310 || token->type == CPP_EOF)
6311 break;
6313 if (token->type == CPP_SEMICOLON)
6315 /* A declaration consisting of a single semicolon is
6316 invalid. Allow it unless we're being pedantic. */
6317 if (pedantic)
6318 pedwarn ("extra `;'");
6319 cp_lexer_consume_token (parser->lexer);
6320 continue;
6323 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6324 parser to enter or exit implict `extern "C"' blocks. */
6325 while (pending_lang_change > 0)
6327 push_lang_context (lang_name_c);
6328 --pending_lang_change;
6330 while (pending_lang_change < 0)
6332 pop_lang_context ();
6333 ++pending_lang_change;
6336 /* Parse the declaration itself. */
6337 cp_parser_declaration (parser);
6341 /* Parse a declaration.
6343 declaration:
6344 block-declaration
6345 function-definition
6346 template-declaration
6347 explicit-instantiation
6348 explicit-specialization
6349 linkage-specification
6350 namespace-definition
6352 GNU extension:
6354 declaration:
6355 __extension__ declaration */
6357 static void
6358 cp_parser_declaration (cp_parser* parser)
6360 cp_token token1;
6361 cp_token token2;
6362 int saved_pedantic;
6364 /* Check for the `__extension__' keyword. */
6365 if (cp_parser_extension_opt (parser, &saved_pedantic))
6367 /* Parse the qualified declaration. */
6368 cp_parser_declaration (parser);
6369 /* Restore the PEDANTIC flag. */
6370 pedantic = saved_pedantic;
6372 return;
6375 /* Try to figure out what kind of declaration is present. */
6376 token1 = *cp_lexer_peek_token (parser->lexer);
6377 if (token1.type != CPP_EOF)
6378 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6380 /* If the next token is `extern' and the following token is a string
6381 literal, then we have a linkage specification. */
6382 if (token1.keyword == RID_EXTERN
6383 && cp_parser_is_string_literal (&token2))
6384 cp_parser_linkage_specification (parser);
6385 /* If the next token is `template', then we have either a template
6386 declaration, an explicit instantiation, or an explicit
6387 specialization. */
6388 else if (token1.keyword == RID_TEMPLATE)
6390 /* `template <>' indicates a template specialization. */
6391 if (token2.type == CPP_LESS
6392 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6393 cp_parser_explicit_specialization (parser);
6394 /* `template <' indicates a template declaration. */
6395 else if (token2.type == CPP_LESS)
6396 cp_parser_template_declaration (parser, /*member_p=*/false);
6397 /* Anything else must be an explicit instantiation. */
6398 else
6399 cp_parser_explicit_instantiation (parser);
6401 /* If the next token is `export', then we have a template
6402 declaration. */
6403 else if (token1.keyword == RID_EXPORT)
6404 cp_parser_template_declaration (parser, /*member_p=*/false);
6405 /* If the next token is `extern', 'static' or 'inline' and the one
6406 after that is `template', we have a GNU extended explicit
6407 instantiation directive. */
6408 else if (cp_parser_allow_gnu_extensions_p (parser)
6409 && (token1.keyword == RID_EXTERN
6410 || token1.keyword == RID_STATIC
6411 || token1.keyword == RID_INLINE)
6412 && token2.keyword == RID_TEMPLATE)
6413 cp_parser_explicit_instantiation (parser);
6414 /* If the next token is `namespace', check for a named or unnamed
6415 namespace definition. */
6416 else if (token1.keyword == RID_NAMESPACE
6417 && (/* A named namespace definition. */
6418 (token2.type == CPP_NAME
6419 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6420 == CPP_OPEN_BRACE))
6421 /* An unnamed namespace definition. */
6422 || token2.type == CPP_OPEN_BRACE))
6423 cp_parser_namespace_definition (parser);
6424 /* We must have either a block declaration or a function
6425 definition. */
6426 else
6427 /* Try to parse a block-declaration, or a function-definition. */
6428 cp_parser_block_declaration (parser, /*statement_p=*/false);
6431 /* Parse a block-declaration.
6433 block-declaration:
6434 simple-declaration
6435 asm-definition
6436 namespace-alias-definition
6437 using-declaration
6438 using-directive
6440 GNU Extension:
6442 block-declaration:
6443 __extension__ block-declaration
6444 label-declaration
6446 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6447 part of a declaration-statement. */
6449 static void
6450 cp_parser_block_declaration (cp_parser *parser,
6451 bool statement_p)
6453 cp_token *token1;
6454 int saved_pedantic;
6456 /* Check for the `__extension__' keyword. */
6457 if (cp_parser_extension_opt (parser, &saved_pedantic))
6459 /* Parse the qualified declaration. */
6460 cp_parser_block_declaration (parser, statement_p);
6461 /* Restore the PEDANTIC flag. */
6462 pedantic = saved_pedantic;
6464 return;
6467 /* Peek at the next token to figure out which kind of declaration is
6468 present. */
6469 token1 = cp_lexer_peek_token (parser->lexer);
6471 /* If the next keyword is `asm', we have an asm-definition. */
6472 if (token1->keyword == RID_ASM)
6474 if (statement_p)
6475 cp_parser_commit_to_tentative_parse (parser);
6476 cp_parser_asm_definition (parser);
6478 /* If the next keyword is `namespace', we have a
6479 namespace-alias-definition. */
6480 else if (token1->keyword == RID_NAMESPACE)
6481 cp_parser_namespace_alias_definition (parser);
6482 /* If the next keyword is `using', we have either a
6483 using-declaration or a using-directive. */
6484 else if (token1->keyword == RID_USING)
6486 cp_token *token2;
6488 if (statement_p)
6489 cp_parser_commit_to_tentative_parse (parser);
6490 /* If the token after `using' is `namespace', then we have a
6491 using-directive. */
6492 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6493 if (token2->keyword == RID_NAMESPACE)
6494 cp_parser_using_directive (parser);
6495 /* Otherwise, it's a using-declaration. */
6496 else
6497 cp_parser_using_declaration (parser);
6499 /* If the next keyword is `__label__' we have a label declaration. */
6500 else if (token1->keyword == RID_LABEL)
6502 if (statement_p)
6503 cp_parser_commit_to_tentative_parse (parser);
6504 cp_parser_label_declaration (parser);
6506 /* Anything else must be a simple-declaration. */
6507 else
6508 cp_parser_simple_declaration (parser, !statement_p);
6511 /* Parse a simple-declaration.
6513 simple-declaration:
6514 decl-specifier-seq [opt] init-declarator-list [opt] ;
6516 init-declarator-list:
6517 init-declarator
6518 init-declarator-list , init-declarator
6520 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6521 function-definition as a simple-declaration. */
6523 static void
6524 cp_parser_simple_declaration (cp_parser* parser,
6525 bool function_definition_allowed_p)
6527 tree decl_specifiers;
6528 tree attributes;
6529 bool declares_class_or_enum;
6530 bool saw_declarator;
6532 /* Defer access checks until we know what is being declared; the
6533 checks for names appearing in the decl-specifier-seq should be
6534 done as if we were in the scope of the thing being declared. */
6535 push_deferring_access_checks (dk_deferred);
6537 /* Parse the decl-specifier-seq. We have to keep track of whether
6538 or not the decl-specifier-seq declares a named class or
6539 enumeration type, since that is the only case in which the
6540 init-declarator-list is allowed to be empty.
6542 [dcl.dcl]
6544 In a simple-declaration, the optional init-declarator-list can be
6545 omitted only when declaring a class or enumeration, that is when
6546 the decl-specifier-seq contains either a class-specifier, an
6547 elaborated-type-specifier, or an enum-specifier. */
6548 decl_specifiers
6549 = cp_parser_decl_specifier_seq (parser,
6550 CP_PARSER_FLAGS_OPTIONAL,
6551 &attributes,
6552 &declares_class_or_enum);
6553 /* We no longer need to defer access checks. */
6554 stop_deferring_access_checks ();
6556 /* If the next two tokens are both identifiers, the code is
6557 erroneous. The usual cause of this situation is code like:
6559 T t;
6561 where "T" should name a type -- but does not. */
6562 if (cp_parser_diagnose_invalid_type_name (parser))
6564 /* If parsing tentatively, we should commit; we really are
6565 looking at a declaration. */
6566 cp_parser_commit_to_tentative_parse (parser);
6567 /* Give up. */
6568 return;
6571 /* Keep going until we hit the `;' at the end of the simple
6572 declaration. */
6573 saw_declarator = false;
6574 while (cp_lexer_next_token_is_not (parser->lexer,
6575 CPP_SEMICOLON))
6577 cp_token *token;
6578 bool function_definition_p;
6580 saw_declarator = true;
6581 /* Parse the init-declarator. */
6582 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6583 function_definition_allowed_p,
6584 /*member_p=*/false,
6585 &function_definition_p);
6586 /* If an error occurred while parsing tentatively, exit quickly.
6587 (That usually happens when in the body of a function; each
6588 statement is treated as a declaration-statement until proven
6589 otherwise.) */
6590 if (cp_parser_error_occurred (parser))
6592 pop_deferring_access_checks ();
6593 return;
6595 /* Handle function definitions specially. */
6596 if (function_definition_p)
6598 /* If the next token is a `,', then we are probably
6599 processing something like:
6601 void f() {}, *p;
6603 which is erroneous. */
6604 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6605 error ("mixing declarations and function-definitions is forbidden");
6606 /* Otherwise, we're done with the list of declarators. */
6607 else
6609 pop_deferring_access_checks ();
6610 return;
6613 /* The next token should be either a `,' or a `;'. */
6614 token = cp_lexer_peek_token (parser->lexer);
6615 /* If it's a `,', there are more declarators to come. */
6616 if (token->type == CPP_COMMA)
6617 cp_lexer_consume_token (parser->lexer);
6618 /* If it's a `;', we are done. */
6619 else if (token->type == CPP_SEMICOLON)
6620 break;
6621 /* Anything else is an error. */
6622 else
6624 cp_parser_error (parser, "expected `,' or `;'");
6625 /* Skip tokens until we reach the end of the statement. */
6626 cp_parser_skip_to_end_of_statement (parser);
6627 pop_deferring_access_checks ();
6628 return;
6630 /* After the first time around, a function-definition is not
6631 allowed -- even if it was OK at first. For example:
6633 int i, f() {}
6635 is not valid. */
6636 function_definition_allowed_p = false;
6639 /* Issue an error message if no declarators are present, and the
6640 decl-specifier-seq does not itself declare a class or
6641 enumeration. */
6642 if (!saw_declarator)
6644 if (cp_parser_declares_only_class_p (parser))
6645 shadow_tag (decl_specifiers);
6646 /* Perform any deferred access checks. */
6647 perform_deferred_access_checks ();
6650 pop_deferring_access_checks ();
6652 /* Consume the `;'. */
6653 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6655 /* Mark all the classes that appeared in the decl-specifier-seq as
6656 having received a `;'. */
6657 note_list_got_semicolon (decl_specifiers);
6660 /* Parse a decl-specifier-seq.
6662 decl-specifier-seq:
6663 decl-specifier-seq [opt] decl-specifier
6665 decl-specifier:
6666 storage-class-specifier
6667 type-specifier
6668 function-specifier
6669 friend
6670 typedef
6672 GNU Extension:
6674 decl-specifier-seq:
6675 decl-specifier-seq [opt] attributes
6677 Returns a TREE_LIST, giving the decl-specifiers in the order they
6678 appear in the source code. The TREE_VALUE of each node is the
6679 decl-specifier. For a keyword (such as `auto' or `friend'), the
6680 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6681 representation of a type-specifier, see cp_parser_type_specifier.
6683 If there are attributes, they will be stored in *ATTRIBUTES,
6684 represented as described above cp_parser_attributes.
6686 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6687 appears, and the entity that will be a friend is not going to be a
6688 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6689 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6690 friendship is granted might not be a class. */
6692 static tree
6693 cp_parser_decl_specifier_seq (cp_parser* parser,
6694 cp_parser_flags flags,
6695 tree* attributes,
6696 bool* declares_class_or_enum)
6698 tree decl_specs = NULL_TREE;
6699 bool friend_p = false;
6700 bool constructor_possible_p = !parser->in_declarator_p;
6702 /* Assume no class or enumeration type is declared. */
6703 *declares_class_or_enum = false;
6705 /* Assume there are no attributes. */
6706 *attributes = NULL_TREE;
6708 /* Keep reading specifiers until there are no more to read. */
6709 while (true)
6711 tree decl_spec = NULL_TREE;
6712 bool constructor_p;
6713 cp_token *token;
6715 /* Peek at the next token. */
6716 token = cp_lexer_peek_token (parser->lexer);
6717 /* Handle attributes. */
6718 if (token->keyword == RID_ATTRIBUTE)
6720 /* Parse the attributes. */
6721 decl_spec = cp_parser_attributes_opt (parser);
6722 /* Add them to the list. */
6723 *attributes = chainon (*attributes, decl_spec);
6724 continue;
6726 /* If the next token is an appropriate keyword, we can simply
6727 add it to the list. */
6728 switch (token->keyword)
6730 case RID_FRIEND:
6731 /* decl-specifier:
6732 friend */
6733 friend_p = true;
6734 /* The representation of the specifier is simply the
6735 appropriate TREE_IDENTIFIER node. */
6736 decl_spec = token->value;
6737 /* Consume the token. */
6738 cp_lexer_consume_token (parser->lexer);
6739 break;
6741 /* function-specifier:
6742 inline
6743 virtual
6744 explicit */
6745 case RID_INLINE:
6746 case RID_VIRTUAL:
6747 case RID_EXPLICIT:
6748 decl_spec = cp_parser_function_specifier_opt (parser);
6749 break;
6751 /* decl-specifier:
6752 typedef */
6753 case RID_TYPEDEF:
6754 /* The representation of the specifier is simply the
6755 appropriate TREE_IDENTIFIER node. */
6756 decl_spec = token->value;
6757 /* Consume the token. */
6758 cp_lexer_consume_token (parser->lexer);
6759 /* A constructor declarator cannot appear in a typedef. */
6760 constructor_possible_p = false;
6761 /* The "typedef" keyword can only occur in a declaration; we
6762 may as well commit at this point. */
6763 cp_parser_commit_to_tentative_parse (parser);
6764 break;
6766 /* storage-class-specifier:
6767 auto
6768 register
6769 static
6770 extern
6771 mutable
6773 GNU Extension:
6774 thread */
6775 case RID_AUTO:
6776 case RID_REGISTER:
6777 case RID_STATIC:
6778 case RID_EXTERN:
6779 case RID_MUTABLE:
6780 case RID_THREAD:
6781 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6782 break;
6784 default:
6785 break;
6788 /* Constructors are a special case. The `S' in `S()' is not a
6789 decl-specifier; it is the beginning of the declarator. */
6790 constructor_p = (!decl_spec
6791 && constructor_possible_p
6792 && cp_parser_constructor_declarator_p (parser,
6793 friend_p));
6795 /* If we don't have a DECL_SPEC yet, then we must be looking at
6796 a type-specifier. */
6797 if (!decl_spec && !constructor_p)
6799 bool decl_spec_declares_class_or_enum;
6800 bool is_cv_qualifier;
6802 decl_spec
6803 = cp_parser_type_specifier (parser, flags,
6804 friend_p,
6805 /*is_declaration=*/true,
6806 &decl_spec_declares_class_or_enum,
6807 &is_cv_qualifier);
6809 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6811 /* If this type-specifier referenced a user-defined type
6812 (a typedef, class-name, etc.), then we can't allow any
6813 more such type-specifiers henceforth.
6815 [dcl.spec]
6817 The longest sequence of decl-specifiers that could
6818 possibly be a type name is taken as the
6819 decl-specifier-seq of a declaration. The sequence shall
6820 be self-consistent as described below.
6822 [dcl.type]
6824 As a general rule, at most one type-specifier is allowed
6825 in the complete decl-specifier-seq of a declaration. The
6826 only exceptions are the following:
6828 -- const or volatile can be combined with any other
6829 type-specifier.
6831 -- signed or unsigned can be combined with char, long,
6832 short, or int.
6834 -- ..
6836 Example:
6838 typedef char* Pc;
6839 void g (const int Pc);
6841 Here, Pc is *not* part of the decl-specifier seq; it's
6842 the declarator. Therefore, once we see a type-specifier
6843 (other than a cv-qualifier), we forbid any additional
6844 user-defined types. We *do* still allow things like `int
6845 int' to be considered a decl-specifier-seq, and issue the
6846 error message later. */
6847 if (decl_spec && !is_cv_qualifier)
6848 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6849 /* A constructor declarator cannot follow a type-specifier. */
6850 if (decl_spec)
6851 constructor_possible_p = false;
6854 /* If we still do not have a DECL_SPEC, then there are no more
6855 decl-specifiers. */
6856 if (!decl_spec)
6858 /* Issue an error message, unless the entire construct was
6859 optional. */
6860 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6862 cp_parser_error (parser, "expected decl specifier");
6863 return error_mark_node;
6866 break;
6869 /* Add the DECL_SPEC to the list of specifiers. */
6870 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6872 /* After we see one decl-specifier, further decl-specifiers are
6873 always optional. */
6874 flags |= CP_PARSER_FLAGS_OPTIONAL;
6877 /* We have built up the DECL_SPECS in reverse order. Return them in
6878 the correct order. */
6879 return nreverse (decl_specs);
6882 /* Parse an (optional) storage-class-specifier.
6884 storage-class-specifier:
6885 auto
6886 register
6887 static
6888 extern
6889 mutable
6891 GNU Extension:
6893 storage-class-specifier:
6894 thread
6896 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6898 static tree
6899 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6901 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6903 case RID_AUTO:
6904 case RID_REGISTER:
6905 case RID_STATIC:
6906 case RID_EXTERN:
6907 case RID_MUTABLE:
6908 case RID_THREAD:
6909 /* Consume the token. */
6910 return cp_lexer_consume_token (parser->lexer)->value;
6912 default:
6913 return NULL_TREE;
6917 /* Parse an (optional) function-specifier.
6919 function-specifier:
6920 inline
6921 virtual
6922 explicit
6924 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6926 static tree
6927 cp_parser_function_specifier_opt (cp_parser* parser)
6929 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6931 case RID_INLINE:
6932 case RID_VIRTUAL:
6933 case RID_EXPLICIT:
6934 /* Consume the token. */
6935 return cp_lexer_consume_token (parser->lexer)->value;
6937 default:
6938 return NULL_TREE;
6942 /* Parse a linkage-specification.
6944 linkage-specification:
6945 extern string-literal { declaration-seq [opt] }
6946 extern string-literal declaration */
6948 static void
6949 cp_parser_linkage_specification (cp_parser* parser)
6951 cp_token *token;
6952 tree linkage;
6954 /* Look for the `extern' keyword. */
6955 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6957 /* Peek at the next token. */
6958 token = cp_lexer_peek_token (parser->lexer);
6959 /* If it's not a string-literal, then there's a problem. */
6960 if (!cp_parser_is_string_literal (token))
6962 cp_parser_error (parser, "expected language-name");
6963 return;
6965 /* Consume the token. */
6966 cp_lexer_consume_token (parser->lexer);
6968 /* Transform the literal into an identifier. If the literal is a
6969 wide-character string, or contains embedded NULs, then we can't
6970 handle it as the user wants. */
6971 if (token->type == CPP_WSTRING
6972 || (strlen (TREE_STRING_POINTER (token->value))
6973 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6975 cp_parser_error (parser, "invalid linkage-specification");
6976 /* Assume C++ linkage. */
6977 linkage = get_identifier ("c++");
6979 /* If it's a simple string constant, things are easier. */
6980 else
6981 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6983 /* We're now using the new linkage. */
6984 push_lang_context (linkage);
6986 /* If the next token is a `{', then we're using the first
6987 production. */
6988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6990 /* Consume the `{' token. */
6991 cp_lexer_consume_token (parser->lexer);
6992 /* Parse the declarations. */
6993 cp_parser_declaration_seq_opt (parser);
6994 /* Look for the closing `}'. */
6995 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6997 /* Otherwise, there's just one declaration. */
6998 else
7000 bool saved_in_unbraced_linkage_specification_p;
7002 saved_in_unbraced_linkage_specification_p
7003 = parser->in_unbraced_linkage_specification_p;
7004 parser->in_unbraced_linkage_specification_p = true;
7005 have_extern_spec = true;
7006 cp_parser_declaration (parser);
7007 have_extern_spec = false;
7008 parser->in_unbraced_linkage_specification_p
7009 = saved_in_unbraced_linkage_specification_p;
7012 /* We're done with the linkage-specification. */
7013 pop_lang_context ();
7016 /* Special member functions [gram.special] */
7018 /* Parse a conversion-function-id.
7020 conversion-function-id:
7021 operator conversion-type-id
7023 Returns an IDENTIFIER_NODE representing the operator. */
7025 static tree
7026 cp_parser_conversion_function_id (cp_parser* parser)
7028 tree type;
7029 tree saved_scope;
7030 tree saved_qualifying_scope;
7031 tree saved_object_scope;
7033 /* Look for the `operator' token. */
7034 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7035 return error_mark_node;
7036 /* When we parse the conversion-type-id, the current scope will be
7037 reset. However, we need that information in able to look up the
7038 conversion function later, so we save it here. */
7039 saved_scope = parser->scope;
7040 saved_qualifying_scope = parser->qualifying_scope;
7041 saved_object_scope = parser->object_scope;
7042 /* We must enter the scope of the class so that the names of
7043 entities declared within the class are available in the
7044 conversion-type-id. For example, consider:
7046 struct S {
7047 typedef int I;
7048 operator I();
7051 S::operator I() { ... }
7053 In order to see that `I' is a type-name in the definition, we
7054 must be in the scope of `S'. */
7055 if (saved_scope)
7056 push_scope (saved_scope);
7057 /* Parse the conversion-type-id. */
7058 type = cp_parser_conversion_type_id (parser);
7059 /* Leave the scope of the class, if any. */
7060 if (saved_scope)
7061 pop_scope (saved_scope);
7062 /* Restore the saved scope. */
7063 parser->scope = saved_scope;
7064 parser->qualifying_scope = saved_qualifying_scope;
7065 parser->object_scope = saved_object_scope;
7066 /* If the TYPE is invalid, indicate failure. */
7067 if (type == error_mark_node)
7068 return error_mark_node;
7069 return mangle_conv_op_name_for_type (type);
7072 /* Parse a conversion-type-id:
7074 conversion-type-id:
7075 type-specifier-seq conversion-declarator [opt]
7077 Returns the TYPE specified. */
7079 static tree
7080 cp_parser_conversion_type_id (cp_parser* parser)
7082 tree attributes;
7083 tree type_specifiers;
7084 tree declarator;
7086 /* Parse the attributes. */
7087 attributes = cp_parser_attributes_opt (parser);
7088 /* Parse the type-specifiers. */
7089 type_specifiers = cp_parser_type_specifier_seq (parser);
7090 /* If that didn't work, stop. */
7091 if (type_specifiers == error_mark_node)
7092 return error_mark_node;
7093 /* Parse the conversion-declarator. */
7094 declarator = cp_parser_conversion_declarator_opt (parser);
7096 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7097 /*initialized=*/0, &attributes);
7100 /* Parse an (optional) conversion-declarator.
7102 conversion-declarator:
7103 ptr-operator conversion-declarator [opt]
7105 Returns a representation of the declarator. See
7106 cp_parser_declarator for details. */
7108 static tree
7109 cp_parser_conversion_declarator_opt (cp_parser* parser)
7111 enum tree_code code;
7112 tree class_type;
7113 tree cv_qualifier_seq;
7115 /* We don't know if there's a ptr-operator next, or not. */
7116 cp_parser_parse_tentatively (parser);
7117 /* Try the ptr-operator. */
7118 code = cp_parser_ptr_operator (parser, &class_type,
7119 &cv_qualifier_seq);
7120 /* If it worked, look for more conversion-declarators. */
7121 if (cp_parser_parse_definitely (parser))
7123 tree declarator;
7125 /* Parse another optional declarator. */
7126 declarator = cp_parser_conversion_declarator_opt (parser);
7128 /* Create the representation of the declarator. */
7129 if (code == INDIRECT_REF)
7130 declarator = make_pointer_declarator (cv_qualifier_seq,
7131 declarator);
7132 else
7133 declarator = make_reference_declarator (cv_qualifier_seq,
7134 declarator);
7136 /* Handle the pointer-to-member case. */
7137 if (class_type)
7138 declarator = build_nt (SCOPE_REF, class_type, declarator);
7140 return declarator;
7143 return NULL_TREE;
7146 /* Parse an (optional) ctor-initializer.
7148 ctor-initializer:
7149 : mem-initializer-list
7151 Returns TRUE iff the ctor-initializer was actually present. */
7153 static bool
7154 cp_parser_ctor_initializer_opt (cp_parser* parser)
7156 /* If the next token is not a `:', then there is no
7157 ctor-initializer. */
7158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7160 /* Do default initialization of any bases and members. */
7161 if (DECL_CONSTRUCTOR_P (current_function_decl))
7162 finish_mem_initializers (NULL_TREE);
7164 return false;
7167 /* Consume the `:' token. */
7168 cp_lexer_consume_token (parser->lexer);
7169 /* And the mem-initializer-list. */
7170 cp_parser_mem_initializer_list (parser);
7172 return true;
7175 /* Parse a mem-initializer-list.
7177 mem-initializer-list:
7178 mem-initializer
7179 mem-initializer , mem-initializer-list */
7181 static void
7182 cp_parser_mem_initializer_list (cp_parser* parser)
7184 tree mem_initializer_list = NULL_TREE;
7186 /* Let the semantic analysis code know that we are starting the
7187 mem-initializer-list. */
7188 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7189 error ("only constructors take base initializers");
7191 /* Loop through the list. */
7192 while (true)
7194 tree mem_initializer;
7196 /* Parse the mem-initializer. */
7197 mem_initializer = cp_parser_mem_initializer (parser);
7198 /* Add it to the list, unless it was erroneous. */
7199 if (mem_initializer)
7201 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7202 mem_initializer_list = mem_initializer;
7204 /* If the next token is not a `,', we're done. */
7205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7206 break;
7207 /* Consume the `,' token. */
7208 cp_lexer_consume_token (parser->lexer);
7211 /* Perform semantic analysis. */
7212 if (DECL_CONSTRUCTOR_P (current_function_decl))
7213 finish_mem_initializers (mem_initializer_list);
7216 /* Parse a mem-initializer.
7218 mem-initializer:
7219 mem-initializer-id ( expression-list [opt] )
7221 GNU extension:
7223 mem-initializer:
7224 ( expresion-list [opt] )
7226 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7227 class) or FIELD_DECL (for a non-static data member) to initialize;
7228 the TREE_VALUE is the expression-list. */
7230 static tree
7231 cp_parser_mem_initializer (cp_parser* parser)
7233 tree mem_initializer_id;
7234 tree expression_list;
7235 tree member;
7237 /* Find out what is being initialized. */
7238 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7240 pedwarn ("anachronistic old-style base class initializer");
7241 mem_initializer_id = NULL_TREE;
7243 else
7244 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7245 member = expand_member_init (mem_initializer_id);
7246 if (member && !DECL_P (member))
7247 in_base_initializer = 1;
7249 /* Look for the opening `('. */
7250 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7251 /* Parse the expression-list. */
7252 if (cp_lexer_next_token_is_not (parser->lexer,
7253 CPP_CLOSE_PAREN))
7254 expression_list = cp_parser_expression_list (parser);
7255 else
7256 expression_list = void_type_node;
7257 /* Look for the closing `)'. */
7258 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7260 in_base_initializer = 0;
7262 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7265 /* Parse a mem-initializer-id.
7267 mem-initializer-id:
7268 :: [opt] nested-name-specifier [opt] class-name
7269 identifier
7271 Returns a TYPE indicating the class to be initializer for the first
7272 production. Returns an IDENTIFIER_NODE indicating the data member
7273 to be initialized for the second production. */
7275 static tree
7276 cp_parser_mem_initializer_id (cp_parser* parser)
7278 bool global_scope_p;
7279 bool nested_name_specifier_p;
7280 tree id;
7282 /* Look for the optional `::' operator. */
7283 global_scope_p
7284 = (cp_parser_global_scope_opt (parser,
7285 /*current_scope_valid_p=*/false)
7286 != NULL_TREE);
7287 /* Look for the optional nested-name-specifier. The simplest way to
7288 implement:
7290 [temp.res]
7292 The keyword `typename' is not permitted in a base-specifier or
7293 mem-initializer; in these contexts a qualified name that
7294 depends on a template-parameter is implicitly assumed to be a
7295 type name.
7297 is to assume that we have seen the `typename' keyword at this
7298 point. */
7299 nested_name_specifier_p
7300 = (cp_parser_nested_name_specifier_opt (parser,
7301 /*typename_keyword_p=*/true,
7302 /*check_dependency_p=*/true,
7303 /*type_p=*/true)
7304 != NULL_TREE);
7305 /* If there is a `::' operator or a nested-name-specifier, then we
7306 are definitely looking for a class-name. */
7307 if (global_scope_p || nested_name_specifier_p)
7308 return cp_parser_class_name (parser,
7309 /*typename_keyword_p=*/true,
7310 /*template_keyword_p=*/false,
7311 /*type_p=*/false,
7312 /*check_dependency_p=*/true,
7313 /*class_head_p=*/false);
7314 /* Otherwise, we could also be looking for an ordinary identifier. */
7315 cp_parser_parse_tentatively (parser);
7316 /* Try a class-name. */
7317 id = cp_parser_class_name (parser,
7318 /*typename_keyword_p=*/true,
7319 /*template_keyword_p=*/false,
7320 /*type_p=*/false,
7321 /*check_dependency_p=*/true,
7322 /*class_head_p=*/false);
7323 /* If we found one, we're done. */
7324 if (cp_parser_parse_definitely (parser))
7325 return id;
7326 /* Otherwise, look for an ordinary identifier. */
7327 return cp_parser_identifier (parser);
7330 /* Overloading [gram.over] */
7332 /* Parse an operator-function-id.
7334 operator-function-id:
7335 operator operator
7337 Returns an IDENTIFIER_NODE for the operator which is a
7338 human-readable spelling of the identifier, e.g., `operator +'. */
7340 static tree
7341 cp_parser_operator_function_id (cp_parser* parser)
7343 /* Look for the `operator' keyword. */
7344 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7345 return error_mark_node;
7346 /* And then the name of the operator itself. */
7347 return cp_parser_operator (parser);
7350 /* Parse an operator.
7352 operator:
7353 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7354 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7355 || ++ -- , ->* -> () []
7357 GNU Extensions:
7359 operator:
7360 <? >? <?= >?=
7362 Returns an IDENTIFIER_NODE for the operator which is a
7363 human-readable spelling of the identifier, e.g., `operator +'. */
7365 static tree
7366 cp_parser_operator (cp_parser* parser)
7368 tree id = NULL_TREE;
7369 cp_token *token;
7371 /* Peek at the next token. */
7372 token = cp_lexer_peek_token (parser->lexer);
7373 /* Figure out which operator we have. */
7374 switch (token->type)
7376 case CPP_KEYWORD:
7378 enum tree_code op;
7380 /* The keyword should be either `new' or `delete'. */
7381 if (token->keyword == RID_NEW)
7382 op = NEW_EXPR;
7383 else if (token->keyword == RID_DELETE)
7384 op = DELETE_EXPR;
7385 else
7386 break;
7388 /* Consume the `new' or `delete' token. */
7389 cp_lexer_consume_token (parser->lexer);
7391 /* Peek at the next token. */
7392 token = cp_lexer_peek_token (parser->lexer);
7393 /* If it's a `[' token then this is the array variant of the
7394 operator. */
7395 if (token->type == CPP_OPEN_SQUARE)
7397 /* Consume the `[' token. */
7398 cp_lexer_consume_token (parser->lexer);
7399 /* Look for the `]' token. */
7400 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7401 id = ansi_opname (op == NEW_EXPR
7402 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7404 /* Otherwise, we have the non-array variant. */
7405 else
7406 id = ansi_opname (op);
7408 return id;
7411 case CPP_PLUS:
7412 id = ansi_opname (PLUS_EXPR);
7413 break;
7415 case CPP_MINUS:
7416 id = ansi_opname (MINUS_EXPR);
7417 break;
7419 case CPP_MULT:
7420 id = ansi_opname (MULT_EXPR);
7421 break;
7423 case CPP_DIV:
7424 id = ansi_opname (TRUNC_DIV_EXPR);
7425 break;
7427 case CPP_MOD:
7428 id = ansi_opname (TRUNC_MOD_EXPR);
7429 break;
7431 case CPP_XOR:
7432 id = ansi_opname (BIT_XOR_EXPR);
7433 break;
7435 case CPP_AND:
7436 id = ansi_opname (BIT_AND_EXPR);
7437 break;
7439 case CPP_OR:
7440 id = ansi_opname (BIT_IOR_EXPR);
7441 break;
7443 case CPP_COMPL:
7444 id = ansi_opname (BIT_NOT_EXPR);
7445 break;
7447 case CPP_NOT:
7448 id = ansi_opname (TRUTH_NOT_EXPR);
7449 break;
7451 case CPP_EQ:
7452 id = ansi_assopname (NOP_EXPR);
7453 break;
7455 case CPP_LESS:
7456 id = ansi_opname (LT_EXPR);
7457 break;
7459 case CPP_GREATER:
7460 id = ansi_opname (GT_EXPR);
7461 break;
7463 case CPP_PLUS_EQ:
7464 id = ansi_assopname (PLUS_EXPR);
7465 break;
7467 case CPP_MINUS_EQ:
7468 id = ansi_assopname (MINUS_EXPR);
7469 break;
7471 case CPP_MULT_EQ:
7472 id = ansi_assopname (MULT_EXPR);
7473 break;
7475 case CPP_DIV_EQ:
7476 id = ansi_assopname (TRUNC_DIV_EXPR);
7477 break;
7479 case CPP_MOD_EQ:
7480 id = ansi_assopname (TRUNC_MOD_EXPR);
7481 break;
7483 case CPP_XOR_EQ:
7484 id = ansi_assopname (BIT_XOR_EXPR);
7485 break;
7487 case CPP_AND_EQ:
7488 id = ansi_assopname (BIT_AND_EXPR);
7489 break;
7491 case CPP_OR_EQ:
7492 id = ansi_assopname (BIT_IOR_EXPR);
7493 break;
7495 case CPP_LSHIFT:
7496 id = ansi_opname (LSHIFT_EXPR);
7497 break;
7499 case CPP_RSHIFT:
7500 id = ansi_opname (RSHIFT_EXPR);
7501 break;
7503 case CPP_LSHIFT_EQ:
7504 id = ansi_assopname (LSHIFT_EXPR);
7505 break;
7507 case CPP_RSHIFT_EQ:
7508 id = ansi_assopname (RSHIFT_EXPR);
7509 break;
7511 case CPP_EQ_EQ:
7512 id = ansi_opname (EQ_EXPR);
7513 break;
7515 case CPP_NOT_EQ:
7516 id = ansi_opname (NE_EXPR);
7517 break;
7519 case CPP_LESS_EQ:
7520 id = ansi_opname (LE_EXPR);
7521 break;
7523 case CPP_GREATER_EQ:
7524 id = ansi_opname (GE_EXPR);
7525 break;
7527 case CPP_AND_AND:
7528 id = ansi_opname (TRUTH_ANDIF_EXPR);
7529 break;
7531 case CPP_OR_OR:
7532 id = ansi_opname (TRUTH_ORIF_EXPR);
7533 break;
7535 case CPP_PLUS_PLUS:
7536 id = ansi_opname (POSTINCREMENT_EXPR);
7537 break;
7539 case CPP_MINUS_MINUS:
7540 id = ansi_opname (PREDECREMENT_EXPR);
7541 break;
7543 case CPP_COMMA:
7544 id = ansi_opname (COMPOUND_EXPR);
7545 break;
7547 case CPP_DEREF_STAR:
7548 id = ansi_opname (MEMBER_REF);
7549 break;
7551 case CPP_DEREF:
7552 id = ansi_opname (COMPONENT_REF);
7553 break;
7555 case CPP_OPEN_PAREN:
7556 /* Consume the `('. */
7557 cp_lexer_consume_token (parser->lexer);
7558 /* Look for the matching `)'. */
7559 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7560 return ansi_opname (CALL_EXPR);
7562 case CPP_OPEN_SQUARE:
7563 /* Consume the `['. */
7564 cp_lexer_consume_token (parser->lexer);
7565 /* Look for the matching `]'. */
7566 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7567 return ansi_opname (ARRAY_REF);
7569 /* Extensions. */
7570 case CPP_MIN:
7571 id = ansi_opname (MIN_EXPR);
7572 break;
7574 case CPP_MAX:
7575 id = ansi_opname (MAX_EXPR);
7576 break;
7578 case CPP_MIN_EQ:
7579 id = ansi_assopname (MIN_EXPR);
7580 break;
7582 case CPP_MAX_EQ:
7583 id = ansi_assopname (MAX_EXPR);
7584 break;
7586 default:
7587 /* Anything else is an error. */
7588 break;
7591 /* If we have selected an identifier, we need to consume the
7592 operator token. */
7593 if (id)
7594 cp_lexer_consume_token (parser->lexer);
7595 /* Otherwise, no valid operator name was present. */
7596 else
7598 cp_parser_error (parser, "expected operator");
7599 id = error_mark_node;
7602 return id;
7605 /* Parse a template-declaration.
7607 template-declaration:
7608 export [opt] template < template-parameter-list > declaration
7610 If MEMBER_P is TRUE, this template-declaration occurs within a
7611 class-specifier.
7613 The grammar rule given by the standard isn't correct. What
7614 is really meant is:
7616 template-declaration:
7617 export [opt] template-parameter-list-seq
7618 decl-specifier-seq [opt] init-declarator [opt] ;
7619 export [opt] template-parameter-list-seq
7620 function-definition
7622 template-parameter-list-seq:
7623 template-parameter-list-seq [opt]
7624 template < template-parameter-list > */
7626 static void
7627 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7629 /* Check for `export'. */
7630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7632 /* Consume the `export' token. */
7633 cp_lexer_consume_token (parser->lexer);
7634 /* Warn that we do not support `export'. */
7635 warning ("keyword `export' not implemented, and will be ignored");
7638 cp_parser_template_declaration_after_export (parser, member_p);
7641 /* Parse a template-parameter-list.
7643 template-parameter-list:
7644 template-parameter
7645 template-parameter-list , template-parameter
7647 Returns a TREE_LIST. Each node represents a template parameter.
7648 The nodes are connected via their TREE_CHAINs. */
7650 static tree
7651 cp_parser_template_parameter_list (cp_parser* parser)
7653 tree parameter_list = NULL_TREE;
7655 while (true)
7657 tree parameter;
7658 cp_token *token;
7660 /* Parse the template-parameter. */
7661 parameter = cp_parser_template_parameter (parser);
7662 /* Add it to the list. */
7663 parameter_list = process_template_parm (parameter_list,
7664 parameter);
7666 /* Peek at the next token. */
7667 token = cp_lexer_peek_token (parser->lexer);
7668 /* If it's not a `,', we're done. */
7669 if (token->type != CPP_COMMA)
7670 break;
7671 /* Otherwise, consume the `,' token. */
7672 cp_lexer_consume_token (parser->lexer);
7675 return parameter_list;
7678 /* Parse a template-parameter.
7680 template-parameter:
7681 type-parameter
7682 parameter-declaration
7684 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7685 TREE_PURPOSE is the default value, if any. */
7687 static tree
7688 cp_parser_template_parameter (cp_parser* parser)
7690 cp_token *token;
7692 /* Peek at the next token. */
7693 token = cp_lexer_peek_token (parser->lexer);
7694 /* If it is `class' or `template', we have a type-parameter. */
7695 if (token->keyword == RID_TEMPLATE)
7696 return cp_parser_type_parameter (parser);
7697 /* If it is `class' or `typename' we do not know yet whether it is a
7698 type parameter or a non-type parameter. Consider:
7700 template <typename T, typename T::X X> ...
7704 template <class C, class D*> ...
7706 Here, the first parameter is a type parameter, and the second is
7707 a non-type parameter. We can tell by looking at the token after
7708 the identifier -- if it is a `,', `=', or `>' then we have a type
7709 parameter. */
7710 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7712 /* Peek at the token after `class' or `typename'. */
7713 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7714 /* If it's an identifier, skip it. */
7715 if (token->type == CPP_NAME)
7716 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7717 /* Now, see if the token looks like the end of a template
7718 parameter. */
7719 if (token->type == CPP_COMMA
7720 || token->type == CPP_EQ
7721 || token->type == CPP_GREATER)
7722 return cp_parser_type_parameter (parser);
7725 /* Otherwise, it is a non-type parameter.
7727 [temp.param]
7729 When parsing a default template-argument for a non-type
7730 template-parameter, the first non-nested `>' is taken as the end
7731 of the template parameter-list rather than a greater-than
7732 operator. */
7733 return
7734 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7737 /* Parse a type-parameter.
7739 type-parameter:
7740 class identifier [opt]
7741 class identifier [opt] = type-id
7742 typename identifier [opt]
7743 typename identifier [opt] = type-id
7744 template < template-parameter-list > class identifier [opt]
7745 template < template-parameter-list > class identifier [opt]
7746 = id-expression
7748 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7749 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7750 the declaration of the parameter. */
7752 static tree
7753 cp_parser_type_parameter (cp_parser* parser)
7755 cp_token *token;
7756 tree parameter;
7758 /* Look for a keyword to tell us what kind of parameter this is. */
7759 token = cp_parser_require (parser, CPP_KEYWORD,
7760 "`class', `typename', or `template'");
7761 if (!token)
7762 return error_mark_node;
7764 switch (token->keyword)
7766 case RID_CLASS:
7767 case RID_TYPENAME:
7769 tree identifier;
7770 tree default_argument;
7772 /* If the next token is an identifier, then it names the
7773 parameter. */
7774 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7775 identifier = cp_parser_identifier (parser);
7776 else
7777 identifier = NULL_TREE;
7779 /* Create the parameter. */
7780 parameter = finish_template_type_parm (class_type_node, identifier);
7782 /* If the next token is an `=', we have a default argument. */
7783 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7785 /* Consume the `=' token. */
7786 cp_lexer_consume_token (parser->lexer);
7787 /* Parse the default-argumen. */
7788 default_argument = cp_parser_type_id (parser);
7790 else
7791 default_argument = NULL_TREE;
7793 /* Create the combined representation of the parameter and the
7794 default argument. */
7795 parameter = build_tree_list (default_argument,
7796 parameter);
7798 break;
7800 case RID_TEMPLATE:
7802 tree parameter_list;
7803 tree identifier;
7804 tree default_argument;
7806 /* Look for the `<'. */
7807 cp_parser_require (parser, CPP_LESS, "`<'");
7808 /* Parse the template-parameter-list. */
7809 begin_template_parm_list ();
7810 parameter_list
7811 = cp_parser_template_parameter_list (parser);
7812 parameter_list = end_template_parm_list (parameter_list);
7813 /* Look for the `>'. */
7814 cp_parser_require (parser, CPP_GREATER, "`>'");
7815 /* Look for the `class' keyword. */
7816 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7817 /* If the next token is an `=', then there is a
7818 default-argument. If the next token is a `>', we are at
7819 the end of the parameter-list. If the next token is a `,',
7820 then we are at the end of this parameter. */
7821 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7822 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7823 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7824 identifier = cp_parser_identifier (parser);
7825 else
7826 identifier = NULL_TREE;
7827 /* Create the template parameter. */
7828 parameter = finish_template_template_parm (class_type_node,
7829 identifier);
7831 /* If the next token is an `=', then there is a
7832 default-argument. */
7833 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7835 /* Consume the `='. */
7836 cp_lexer_consume_token (parser->lexer);
7837 /* Parse the id-expression. */
7838 default_argument
7839 = cp_parser_id_expression (parser,
7840 /*template_keyword_p=*/false,
7841 /*check_dependency_p=*/true,
7842 /*template_p=*/NULL);
7843 /* Look up the name. */
7844 default_argument
7845 = cp_parser_lookup_name_simple (parser, default_argument);
7846 /* See if the default argument is valid. */
7847 default_argument
7848 = check_template_template_default_arg (default_argument);
7850 else
7851 default_argument = NULL_TREE;
7853 /* Create the combined representation of the parameter and the
7854 default argument. */
7855 parameter = build_tree_list (default_argument,
7856 parameter);
7858 break;
7860 default:
7861 /* Anything else is an error. */
7862 cp_parser_error (parser,
7863 "expected `class', `typename', or `template'");
7864 parameter = error_mark_node;
7867 return parameter;
7870 /* Parse a template-id.
7872 template-id:
7873 template-name < template-argument-list [opt] >
7875 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7876 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7877 returned. Otherwise, if the template-name names a function, or set
7878 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7879 names a class, returns a TYPE_DECL for the specialization.
7881 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7882 uninstantiated templates. */
7884 static tree
7885 cp_parser_template_id (cp_parser *parser,
7886 bool template_keyword_p,
7887 bool check_dependency_p)
7889 tree template;
7890 tree arguments;
7891 tree saved_scope;
7892 tree saved_qualifying_scope;
7893 tree saved_object_scope;
7894 tree template_id;
7895 bool saved_greater_than_is_operator_p;
7896 ptrdiff_t start_of_id;
7897 tree access_check = NULL_TREE;
7898 cp_token *next_token;
7900 /* If the next token corresponds to a template-id, there is no need
7901 to reparse it. */
7902 next_token = cp_lexer_peek_token (parser->lexer);
7903 if (next_token->type == CPP_TEMPLATE_ID)
7905 tree value;
7906 tree check;
7908 /* Get the stored value. */
7909 value = cp_lexer_consume_token (parser->lexer)->value;
7910 /* Perform any access checks that were deferred. */
7911 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7912 perform_or_defer_access_check (TREE_PURPOSE (check),
7913 TREE_VALUE (check));
7914 /* Return the stored value. */
7915 return TREE_VALUE (value);
7918 /* Avoid performing name lookup if there is no possibility of
7919 finding a template-id. */
7920 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7921 || (next_token->type == CPP_NAME
7922 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7924 cp_parser_error (parser, "expected template-id");
7925 return error_mark_node;
7928 /* Remember where the template-id starts. */
7929 if (cp_parser_parsing_tentatively (parser)
7930 && !cp_parser_committed_to_tentative_parse (parser))
7932 next_token = cp_lexer_peek_token (parser->lexer);
7933 start_of_id = cp_lexer_token_difference (parser->lexer,
7934 parser->lexer->first_token,
7935 next_token);
7937 else
7938 start_of_id = -1;
7940 push_deferring_access_checks (dk_deferred);
7942 /* Parse the template-name. */
7943 template = cp_parser_template_name (parser, template_keyword_p,
7944 check_dependency_p);
7945 if (template == error_mark_node)
7947 pop_deferring_access_checks ();
7948 return error_mark_node;
7951 /* Look for the `<' that starts the template-argument-list. */
7952 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7954 pop_deferring_access_checks ();
7955 return error_mark_node;
7958 /* [temp.names]
7960 When parsing a template-id, the first non-nested `>' is taken as
7961 the end of the template-argument-list rather than a greater-than
7962 operator. */
7963 saved_greater_than_is_operator_p
7964 = parser->greater_than_is_operator_p;
7965 parser->greater_than_is_operator_p = false;
7966 /* Parsing the argument list may modify SCOPE, so we save it
7967 here. */
7968 saved_scope = parser->scope;
7969 saved_qualifying_scope = parser->qualifying_scope;
7970 saved_object_scope = parser->object_scope;
7971 /* Parse the template-argument-list itself. */
7972 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7973 arguments = NULL_TREE;
7974 else
7975 arguments = cp_parser_template_argument_list (parser);
7976 /* Look for the `>' that ends the template-argument-list. */
7977 cp_parser_require (parser, CPP_GREATER, "`>'");
7978 /* The `>' token might be a greater-than operator again now. */
7979 parser->greater_than_is_operator_p
7980 = saved_greater_than_is_operator_p;
7981 /* Restore the SAVED_SCOPE. */
7982 parser->scope = saved_scope;
7983 parser->qualifying_scope = saved_qualifying_scope;
7984 parser->object_scope = saved_object_scope;
7986 /* Build a representation of the specialization. */
7987 if (TREE_CODE (template) == IDENTIFIER_NODE)
7988 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7989 else if (DECL_CLASS_TEMPLATE_P (template)
7990 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7991 template_id
7992 = finish_template_type (template, arguments,
7993 cp_lexer_next_token_is (parser->lexer,
7994 CPP_SCOPE));
7995 else
7997 /* If it's not a class-template or a template-template, it should be
7998 a function-template. */
7999 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8000 || TREE_CODE (template) == OVERLOAD
8001 || BASELINK_P (template)),
8002 20010716);
8004 template_id = lookup_template_function (template, arguments);
8007 /* Retrieve any deferred checks. Do not pop this access checks yet
8008 so the memory will not be reclaimed during token replacing below. */
8009 access_check = get_deferred_access_checks ();
8011 /* If parsing tentatively, replace the sequence of tokens that makes
8012 up the template-id with a CPP_TEMPLATE_ID token. That way,
8013 should we re-parse the token stream, we will not have to repeat
8014 the effort required to do the parse, nor will we issue duplicate
8015 error messages about problems during instantiation of the
8016 template. */
8017 if (start_of_id >= 0)
8019 cp_token *token;
8021 /* Find the token that corresponds to the start of the
8022 template-id. */
8023 token = cp_lexer_advance_token (parser->lexer,
8024 parser->lexer->first_token,
8025 start_of_id);
8027 /* Reset the contents of the START_OF_ID token. */
8028 token->type = CPP_TEMPLATE_ID;
8029 token->value = build_tree_list (access_check, template_id);
8030 token->keyword = RID_MAX;
8031 /* Purge all subsequent tokens. */
8032 cp_lexer_purge_tokens_after (parser->lexer, token);
8035 pop_deferring_access_checks ();
8036 return template_id;
8039 /* Parse a template-name.
8041 template-name:
8042 identifier
8044 The standard should actually say:
8046 template-name:
8047 identifier
8048 operator-function-id
8049 conversion-function-id
8051 A defect report has been filed about this issue.
8053 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8054 `template' keyword, in a construction like:
8056 T::template f<3>()
8058 In that case `f' is taken to be a template-name, even though there
8059 is no way of knowing for sure.
8061 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8062 name refers to a set of overloaded functions, at least one of which
8063 is a template, or an IDENTIFIER_NODE with the name of the template,
8064 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8065 names are looked up inside uninstantiated templates. */
8067 static tree
8068 cp_parser_template_name (cp_parser* parser,
8069 bool template_keyword_p,
8070 bool check_dependency_p)
8072 tree identifier;
8073 tree decl;
8074 tree fns;
8076 /* If the next token is `operator', then we have either an
8077 operator-function-id or a conversion-function-id. */
8078 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8080 /* We don't know whether we're looking at an
8081 operator-function-id or a conversion-function-id. */
8082 cp_parser_parse_tentatively (parser);
8083 /* Try an operator-function-id. */
8084 identifier = cp_parser_operator_function_id (parser);
8085 /* If that didn't work, try a conversion-function-id. */
8086 if (!cp_parser_parse_definitely (parser))
8087 identifier = cp_parser_conversion_function_id (parser);
8089 /* Look for the identifier. */
8090 else
8091 identifier = cp_parser_identifier (parser);
8093 /* If we didn't find an identifier, we don't have a template-id. */
8094 if (identifier == error_mark_node)
8095 return error_mark_node;
8097 /* If the name immediately followed the `template' keyword, then it
8098 is a template-name. However, if the next token is not `<', then
8099 we do not treat it as a template-name, since it is not being used
8100 as part of a template-id. This enables us to handle constructs
8101 like:
8103 template <typename T> struct S { S(); };
8104 template <typename T> S<T>::S();
8106 correctly. We would treat `S' as a template -- if it were `S<T>'
8107 -- but we do not if there is no `<'. */
8108 if (template_keyword_p && processing_template_decl
8109 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8110 return identifier;
8112 /* Look up the name. */
8113 decl = cp_parser_lookup_name (parser, identifier,
8114 /*is_type=*/false,
8115 /*is_namespace=*/false,
8116 check_dependency_p);
8117 decl = maybe_get_template_decl_from_type_decl (decl);
8119 /* If DECL is a template, then the name was a template-name. */
8120 if (TREE_CODE (decl) == TEMPLATE_DECL)
8122 else
8124 /* The standard does not explicitly indicate whether a name that
8125 names a set of overloaded declarations, some of which are
8126 templates, is a template-name. However, such a name should
8127 be a template-name; otherwise, there is no way to form a
8128 template-id for the overloaded templates. */
8129 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8130 if (TREE_CODE (fns) == OVERLOAD)
8132 tree fn;
8134 for (fn = fns; fn; fn = OVL_NEXT (fn))
8135 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8136 break;
8138 else
8140 /* Otherwise, the name does not name a template. */
8141 cp_parser_error (parser, "expected template-name");
8142 return error_mark_node;
8146 /* If DECL is dependent, and refers to a function, then just return
8147 its name; we will look it up again during template instantiation. */
8148 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8150 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8151 if (TYPE_P (scope) && dependent_type_p (scope))
8152 return identifier;
8155 return decl;
8158 /* Parse a template-argument-list.
8160 template-argument-list:
8161 template-argument
8162 template-argument-list , template-argument
8164 Returns a TREE_LIST representing the arguments, in the order they
8165 appeared. The TREE_VALUE of each node is a representation of the
8166 argument. */
8168 static tree
8169 cp_parser_template_argument_list (cp_parser* parser)
8171 tree arguments = NULL_TREE;
8173 while (true)
8175 tree argument;
8177 /* Parse the template-argument. */
8178 argument = cp_parser_template_argument (parser);
8179 /* Add it to the list. */
8180 arguments = tree_cons (NULL_TREE, argument, arguments);
8181 /* If it is not a `,', then there are no more arguments. */
8182 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8183 break;
8184 /* Otherwise, consume the ','. */
8185 cp_lexer_consume_token (parser->lexer);
8188 /* We built up the arguments in reverse order. */
8189 return nreverse (arguments);
8192 /* Parse a template-argument.
8194 template-argument:
8195 assignment-expression
8196 type-id
8197 id-expression
8199 The representation is that of an assignment-expression, type-id, or
8200 id-expression -- except that the qualified id-expression is
8201 evaluated, so that the value returned is either a DECL or an
8202 OVERLOAD. */
8204 static tree
8205 cp_parser_template_argument (cp_parser* parser)
8207 tree argument;
8208 bool template_p;
8210 /* There's really no way to know what we're looking at, so we just
8211 try each alternative in order.
8213 [temp.arg]
8215 In a template-argument, an ambiguity between a type-id and an
8216 expression is resolved to a type-id, regardless of the form of
8217 the corresponding template-parameter.
8219 Therefore, we try a type-id first. */
8220 cp_parser_parse_tentatively (parser);
8221 argument = cp_parser_type_id (parser);
8222 /* If the next token isn't a `,' or a `>', then this argument wasn't
8223 really finished. */
8224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8225 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8226 cp_parser_error (parser, "expected template-argument");
8227 /* If that worked, we're done. */
8228 if (cp_parser_parse_definitely (parser))
8229 return argument;
8230 /* We're still not sure what the argument will be. */
8231 cp_parser_parse_tentatively (parser);
8232 /* Try a template. */
8233 argument = cp_parser_id_expression (parser,
8234 /*template_keyword_p=*/false,
8235 /*check_dependency_p=*/true,
8236 &template_p);
8237 /* If the next token isn't a `,' or a `>', then this argument wasn't
8238 really finished. */
8239 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8240 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8241 cp_parser_error (parser, "expected template-argument");
8242 if (!cp_parser_error_occurred (parser))
8244 /* Figure out what is being referred to. */
8245 argument = cp_parser_lookup_name_simple (parser, argument);
8246 if (template_p)
8247 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8248 TREE_OPERAND (argument, 1),
8249 tf_error);
8250 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8251 cp_parser_error (parser, "expected template-name");
8253 if (cp_parser_parse_definitely (parser))
8254 return argument;
8255 /* It must be an assignment-expression. */
8256 return cp_parser_assignment_expression (parser);
8259 /* Parse an explicit-instantiation.
8261 explicit-instantiation:
8262 template declaration
8264 Although the standard says `declaration', what it really means is:
8266 explicit-instantiation:
8267 template decl-specifier-seq [opt] declarator [opt] ;
8269 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8270 supposed to be allowed. A defect report has been filed about this
8271 issue.
8273 GNU Extension:
8275 explicit-instantiation:
8276 storage-class-specifier template
8277 decl-specifier-seq [opt] declarator [opt] ;
8278 function-specifier template
8279 decl-specifier-seq [opt] declarator [opt] ; */
8281 static void
8282 cp_parser_explicit_instantiation (cp_parser* parser)
8284 bool declares_class_or_enum;
8285 tree decl_specifiers;
8286 tree attributes;
8287 tree extension_specifier = NULL_TREE;
8289 /* Look for an (optional) storage-class-specifier or
8290 function-specifier. */
8291 if (cp_parser_allow_gnu_extensions_p (parser))
8293 extension_specifier
8294 = cp_parser_storage_class_specifier_opt (parser);
8295 if (!extension_specifier)
8296 extension_specifier = cp_parser_function_specifier_opt (parser);
8299 /* Look for the `template' keyword. */
8300 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8301 /* Let the front end know that we are processing an explicit
8302 instantiation. */
8303 begin_explicit_instantiation ();
8304 /* [temp.explicit] says that we are supposed to ignore access
8305 control while processing explicit instantiation directives. */
8306 push_deferring_access_checks (dk_no_check);
8307 /* Parse a decl-specifier-seq. */
8308 decl_specifiers
8309 = cp_parser_decl_specifier_seq (parser,
8310 CP_PARSER_FLAGS_OPTIONAL,
8311 &attributes,
8312 &declares_class_or_enum);
8313 /* If there was exactly one decl-specifier, and it declared a class,
8314 and there's no declarator, then we have an explicit type
8315 instantiation. */
8316 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8318 tree type;
8320 type = check_tag_decl (decl_specifiers);
8321 /* Turn access control back on for names used during
8322 template instantiation. */
8323 pop_deferring_access_checks ();
8324 if (type)
8325 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8327 else
8329 tree declarator;
8330 tree decl;
8332 /* Parse the declarator. */
8333 declarator
8334 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8335 /*ctor_dtor_or_conv_p=*/NULL);
8336 decl = grokdeclarator (declarator, decl_specifiers,
8337 NORMAL, 0, NULL);
8338 /* Turn access control back on for names used during
8339 template instantiation. */
8340 pop_deferring_access_checks ();
8341 /* Do the explicit instantiation. */
8342 do_decl_instantiation (decl, extension_specifier);
8344 /* We're done with the instantiation. */
8345 end_explicit_instantiation ();
8347 cp_parser_consume_semicolon_at_end_of_statement (parser);
8350 /* Parse an explicit-specialization.
8352 explicit-specialization:
8353 template < > declaration
8355 Although the standard says `declaration', what it really means is:
8357 explicit-specialization:
8358 template <> decl-specifier [opt] init-declarator [opt] ;
8359 template <> function-definition
8360 template <> explicit-specialization
8361 template <> template-declaration */
8363 static void
8364 cp_parser_explicit_specialization (cp_parser* parser)
8366 /* Look for the `template' keyword. */
8367 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8368 /* Look for the `<'. */
8369 cp_parser_require (parser, CPP_LESS, "`<'");
8370 /* Look for the `>'. */
8371 cp_parser_require (parser, CPP_GREATER, "`>'");
8372 /* We have processed another parameter list. */
8373 ++parser->num_template_parameter_lists;
8374 /* Let the front end know that we are beginning a specialization. */
8375 begin_specialization ();
8377 /* If the next keyword is `template', we need to figure out whether
8378 or not we're looking a template-declaration. */
8379 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8381 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8382 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8383 cp_parser_template_declaration_after_export (parser,
8384 /*member_p=*/false);
8385 else
8386 cp_parser_explicit_specialization (parser);
8388 else
8389 /* Parse the dependent declaration. */
8390 cp_parser_single_declaration (parser,
8391 /*member_p=*/false,
8392 /*friend_p=*/NULL);
8394 /* We're done with the specialization. */
8395 end_specialization ();
8396 /* We're done with this parameter list. */
8397 --parser->num_template_parameter_lists;
8400 /* Parse a type-specifier.
8402 type-specifier:
8403 simple-type-specifier
8404 class-specifier
8405 enum-specifier
8406 elaborated-type-specifier
8407 cv-qualifier
8409 GNU Extension:
8411 type-specifier:
8412 __complex__
8414 Returns a representation of the type-specifier. If the
8415 type-specifier is a keyword (like `int' or `const', or
8416 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8417 For a class-specifier, enum-specifier, or elaborated-type-specifier
8418 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8420 If IS_FRIEND is TRUE then this type-specifier is being declared a
8421 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8422 appearing in a decl-specifier-seq.
8424 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8425 class-specifier, enum-specifier, or elaborated-type-specifier, then
8426 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8427 FALSE.
8429 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8430 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8431 is set to FALSE. */
8433 static tree
8434 cp_parser_type_specifier (cp_parser* parser,
8435 cp_parser_flags flags,
8436 bool is_friend,
8437 bool is_declaration,
8438 bool* declares_class_or_enum,
8439 bool* is_cv_qualifier)
8441 tree type_spec = NULL_TREE;
8442 cp_token *token;
8443 enum rid keyword;
8445 /* Assume this type-specifier does not declare a new type. */
8446 if (declares_class_or_enum)
8447 *declares_class_or_enum = false;
8448 /* And that it does not specify a cv-qualifier. */
8449 if (is_cv_qualifier)
8450 *is_cv_qualifier = false;
8451 /* Peek at the next token. */
8452 token = cp_lexer_peek_token (parser->lexer);
8454 /* If we're looking at a keyword, we can use that to guide the
8455 production we choose. */
8456 keyword = token->keyword;
8457 switch (keyword)
8459 /* Any of these indicate either a class-specifier, or an
8460 elaborated-type-specifier. */
8461 case RID_CLASS:
8462 case RID_STRUCT:
8463 case RID_UNION:
8464 case RID_ENUM:
8465 /* Parse tentatively so that we can back up if we don't find a
8466 class-specifier or enum-specifier. */
8467 cp_parser_parse_tentatively (parser);
8468 /* Look for the class-specifier or enum-specifier. */
8469 if (keyword == RID_ENUM)
8470 type_spec = cp_parser_enum_specifier (parser);
8471 else
8472 type_spec = cp_parser_class_specifier (parser);
8474 /* If that worked, we're done. */
8475 if (cp_parser_parse_definitely (parser))
8477 if (declares_class_or_enum)
8478 *declares_class_or_enum = true;
8479 return type_spec;
8482 /* Fall through. */
8484 case RID_TYPENAME:
8485 /* Look for an elaborated-type-specifier. */
8486 type_spec = cp_parser_elaborated_type_specifier (parser,
8487 is_friend,
8488 is_declaration);
8489 /* We're declaring a class or enum -- unless we're using
8490 `typename'. */
8491 if (declares_class_or_enum && keyword != RID_TYPENAME)
8492 *declares_class_or_enum = true;
8493 return type_spec;
8495 case RID_CONST:
8496 case RID_VOLATILE:
8497 case RID_RESTRICT:
8498 type_spec = cp_parser_cv_qualifier_opt (parser);
8499 /* Even though we call a routine that looks for an optional
8500 qualifier, we know that there should be one. */
8501 my_friendly_assert (type_spec != NULL, 20000328);
8502 /* This type-specifier was a cv-qualified. */
8503 if (is_cv_qualifier)
8504 *is_cv_qualifier = true;
8506 return type_spec;
8508 case RID_COMPLEX:
8509 /* The `__complex__' keyword is a GNU extension. */
8510 return cp_lexer_consume_token (parser->lexer)->value;
8512 default:
8513 break;
8516 /* If we do not already have a type-specifier, assume we are looking
8517 at a simple-type-specifier. */
8518 type_spec = cp_parser_simple_type_specifier (parser, flags);
8520 /* If we didn't find a type-specifier, and a type-specifier was not
8521 optional in this context, issue an error message. */
8522 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8524 cp_parser_error (parser, "expected type specifier");
8525 return error_mark_node;
8528 return type_spec;
8531 /* Parse a simple-type-specifier.
8533 simple-type-specifier:
8534 :: [opt] nested-name-specifier [opt] type-name
8535 :: [opt] nested-name-specifier template template-id
8536 char
8537 wchar_t
8538 bool
8539 short
8541 long
8542 signed
8543 unsigned
8544 float
8545 double
8546 void
8548 GNU Extension:
8550 simple-type-specifier:
8551 __typeof__ unary-expression
8552 __typeof__ ( type-id )
8554 For the various keywords, the value returned is simply the
8555 TREE_IDENTIFIER representing the keyword. For the first two
8556 productions, the value returned is the indicated TYPE_DECL. */
8558 static tree
8559 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags)
8561 tree type = NULL_TREE;
8562 cp_token *token;
8564 /* Peek at the next token. */
8565 token = cp_lexer_peek_token (parser->lexer);
8567 /* If we're looking at a keyword, things are easy. */
8568 switch (token->keyword)
8570 case RID_CHAR:
8571 case RID_WCHAR:
8572 case RID_BOOL:
8573 case RID_SHORT:
8574 case RID_INT:
8575 case RID_LONG:
8576 case RID_SIGNED:
8577 case RID_UNSIGNED:
8578 case RID_FLOAT:
8579 case RID_DOUBLE:
8580 case RID_VOID:
8581 /* Consume the token. */
8582 return cp_lexer_consume_token (parser->lexer)->value;
8584 case RID_TYPEOF:
8586 tree operand;
8588 /* Consume the `typeof' token. */
8589 cp_lexer_consume_token (parser->lexer);
8590 /* Parse the operand to `typeof' */
8591 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8592 /* If it is not already a TYPE, take its type. */
8593 if (!TYPE_P (operand))
8594 operand = finish_typeof (operand);
8596 return operand;
8599 default:
8600 break;
8603 /* The type-specifier must be a user-defined type. */
8604 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8606 /* Don't gobble tokens or issue error messages if this is an
8607 optional type-specifier. */
8608 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8609 cp_parser_parse_tentatively (parser);
8611 /* Look for the optional `::' operator. */
8612 cp_parser_global_scope_opt (parser,
8613 /*current_scope_valid_p=*/false);
8614 /* Look for the nested-name specifier. */
8615 cp_parser_nested_name_specifier_opt (parser,
8616 /*typename_keyword_p=*/false,
8617 /*check_dependency_p=*/true,
8618 /*type_p=*/false);
8619 /* If we have seen a nested-name-specifier, and the next token
8620 is `template', then we are using the template-id production. */
8621 if (parser->scope
8622 && cp_parser_optional_template_keyword (parser))
8624 /* Look for the template-id. */
8625 type = cp_parser_template_id (parser,
8626 /*template_keyword_p=*/true,
8627 /*check_dependency_p=*/true);
8628 /* If the template-id did not name a type, we are out of
8629 luck. */
8630 if (TREE_CODE (type) != TYPE_DECL)
8632 cp_parser_error (parser, "expected template-id for type");
8633 type = NULL_TREE;
8636 /* Otherwise, look for a type-name. */
8637 else
8639 type = cp_parser_type_name (parser);
8640 if (type == error_mark_node)
8641 type = NULL_TREE;
8644 /* If it didn't work out, we don't have a TYPE. */
8645 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8646 && !cp_parser_parse_definitely (parser))
8647 type = NULL_TREE;
8650 /* If we didn't get a type-name, issue an error message. */
8651 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8653 cp_parser_error (parser, "expected type-name");
8654 return error_mark_node;
8657 return type;
8660 /* Parse a type-name.
8662 type-name:
8663 class-name
8664 enum-name
8665 typedef-name
8667 enum-name:
8668 identifier
8670 typedef-name:
8671 identifier
8673 Returns a TYPE_DECL for the the type. */
8675 static tree
8676 cp_parser_type_name (cp_parser* parser)
8678 tree type_decl;
8679 tree identifier;
8681 /* We can't know yet whether it is a class-name or not. */
8682 cp_parser_parse_tentatively (parser);
8683 /* Try a class-name. */
8684 type_decl = cp_parser_class_name (parser,
8685 /*typename_keyword_p=*/false,
8686 /*template_keyword_p=*/false,
8687 /*type_p=*/false,
8688 /*check_dependency_p=*/true,
8689 /*class_head_p=*/false);
8690 /* If it's not a class-name, keep looking. */
8691 if (!cp_parser_parse_definitely (parser))
8693 /* It must be a typedef-name or an enum-name. */
8694 identifier = cp_parser_identifier (parser);
8695 if (identifier == error_mark_node)
8696 return error_mark_node;
8698 /* Look up the type-name. */
8699 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8700 /* Issue an error if we did not find a type-name. */
8701 if (TREE_CODE (type_decl) != TYPE_DECL)
8703 cp_parser_error (parser, "expected type-name");
8704 type_decl = error_mark_node;
8706 /* Remember that the name was used in the definition of the
8707 current class so that we can check later to see if the
8708 meaning would have been different after the class was
8709 entirely defined. */
8710 else if (type_decl != error_mark_node
8711 && !parser->scope)
8712 maybe_note_name_used_in_class (identifier, type_decl);
8715 return type_decl;
8719 /* Parse an elaborated-type-specifier. Note that the grammar given
8720 here incorporates the resolution to DR68.
8722 elaborated-type-specifier:
8723 class-key :: [opt] nested-name-specifier [opt] identifier
8724 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8725 enum :: [opt] nested-name-specifier [opt] identifier
8726 typename :: [opt] nested-name-specifier identifier
8727 typename :: [opt] nested-name-specifier template [opt]
8728 template-id
8730 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8731 declared `friend'. If IS_DECLARATION is TRUE, then this
8732 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8733 something is being declared.
8735 Returns the TYPE specified. */
8737 static tree
8738 cp_parser_elaborated_type_specifier (cp_parser* parser,
8739 bool is_friend,
8740 bool is_declaration)
8742 enum tag_types tag_type;
8743 tree identifier;
8744 tree type = NULL_TREE;
8746 /* See if we're looking at the `enum' keyword. */
8747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8749 /* Consume the `enum' token. */
8750 cp_lexer_consume_token (parser->lexer);
8751 /* Remember that it's an enumeration type. */
8752 tag_type = enum_type;
8754 /* Or, it might be `typename'. */
8755 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8756 RID_TYPENAME))
8758 /* Consume the `typename' token. */
8759 cp_lexer_consume_token (parser->lexer);
8760 /* Remember that it's a `typename' type. */
8761 tag_type = typename_type;
8762 /* The `typename' keyword is only allowed in templates. */
8763 if (!processing_template_decl)
8764 pedwarn ("using `typename' outside of template");
8766 /* Otherwise it must be a class-key. */
8767 else
8769 tag_type = cp_parser_class_key (parser);
8770 if (tag_type == none_type)
8771 return error_mark_node;
8774 /* Look for the `::' operator. */
8775 cp_parser_global_scope_opt (parser,
8776 /*current_scope_valid_p=*/false);
8777 /* Look for the nested-name-specifier. */
8778 if (tag_type == typename_type)
8780 if (cp_parser_nested_name_specifier (parser,
8781 /*typename_keyword_p=*/true,
8782 /*check_dependency_p=*/true,
8783 /*type_p=*/true)
8784 == error_mark_node)
8785 return error_mark_node;
8787 else
8788 /* Even though `typename' is not present, the proposed resolution
8789 to Core Issue 180 says that in `class A<T>::B', `B' should be
8790 considered a type-name, even if `A<T>' is dependent. */
8791 cp_parser_nested_name_specifier_opt (parser,
8792 /*typename_keyword_p=*/true,
8793 /*check_dependency_p=*/true,
8794 /*type_p=*/true);
8795 /* For everything but enumeration types, consider a template-id. */
8796 if (tag_type != enum_type)
8798 bool template_p = false;
8799 tree decl;
8801 /* Allow the `template' keyword. */
8802 template_p = cp_parser_optional_template_keyword (parser);
8803 /* If we didn't see `template', we don't know if there's a
8804 template-id or not. */
8805 if (!template_p)
8806 cp_parser_parse_tentatively (parser);
8807 /* Parse the template-id. */
8808 decl = cp_parser_template_id (parser, template_p,
8809 /*check_dependency_p=*/true);
8810 /* If we didn't find a template-id, look for an ordinary
8811 identifier. */
8812 if (!template_p && !cp_parser_parse_definitely (parser))
8814 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8815 in effect, then we must assume that, upon instantiation, the
8816 template will correspond to a class. */
8817 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8818 && tag_type == typename_type)
8819 type = make_typename_type (parser->scope, decl,
8820 /*complain=*/1);
8821 else
8822 type = TREE_TYPE (decl);
8825 /* For an enumeration type, consider only a plain identifier. */
8826 if (!type)
8828 identifier = cp_parser_identifier (parser);
8830 if (identifier == error_mark_node)
8831 return error_mark_node;
8833 /* For a `typename', we needn't call xref_tag. */
8834 if (tag_type == typename_type)
8835 return make_typename_type (parser->scope, identifier,
8836 /*complain=*/1);
8837 /* Look up a qualified name in the usual way. */
8838 if (parser->scope)
8840 tree decl;
8842 /* In an elaborated-type-specifier, names are assumed to name
8843 types, so we set IS_TYPE to TRUE when calling
8844 cp_parser_lookup_name. */
8845 decl = cp_parser_lookup_name (parser, identifier,
8846 /*is_type=*/true,
8847 /*is_namespace=*/false,
8848 /*check_dependency=*/true);
8850 /* If we are parsing friend declaration, DECL may be a
8851 TEMPLATE_DECL tree node here. However, we need to check
8852 whether this TEMPLATE_DECL results in valid code. Consider
8853 the following example:
8855 namespace N {
8856 template <class T> class C {};
8858 class X {
8859 template <class T> friend class N::C; // #1, valid code
8861 template <class T> class Y {
8862 friend class N::C; // #2, invalid code
8865 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8866 name lookup of `N::C'. We see that friend declaration must
8867 be template for the code to be valid. Note that
8868 processing_template_decl does not work here since it is
8869 always 1 for the above two cases. */
8871 decl = (cp_parser_maybe_treat_template_as_class
8872 (decl, /*tag_name_p=*/is_friend
8873 && parser->num_template_parameter_lists));
8875 if (TREE_CODE (decl) != TYPE_DECL)
8877 error ("expected type-name");
8878 return error_mark_node;
8880 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8881 && tag_type != enum_type)
8882 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8883 tag_type == record_type ? "struct" : "class");
8884 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8885 && tag_type == enum_type)
8886 error ("`%T' referred to as enum", TREE_TYPE (decl));
8888 type = TREE_TYPE (decl);
8890 else
8892 /* An elaborated-type-specifier sometimes introduces a new type and
8893 sometimes names an existing type. Normally, the rule is that it
8894 introduces a new type only if there is not an existing type of
8895 the same name already in scope. For example, given:
8897 struct S {};
8898 void f() { struct S s; }
8900 the `struct S' in the body of `f' is the same `struct S' as in
8901 the global scope; the existing definition is used. However, if
8902 there were no global declaration, this would introduce a new
8903 local class named `S'.
8905 An exception to this rule applies to the following code:
8907 namespace N { struct S; }
8909 Here, the elaborated-type-specifier names a new type
8910 unconditionally; even if there is already an `S' in the
8911 containing scope this declaration names a new type.
8912 This exception only applies if the elaborated-type-specifier
8913 forms the complete declaration:
8915 [class.name]
8917 A declaration consisting solely of `class-key identifier ;' is
8918 either a redeclaration of the name in the current scope or a
8919 forward declaration of the identifier as a class name. It
8920 introduces the name into the current scope.
8922 We are in this situation precisely when the next token is a `;'.
8924 An exception to the exception is that a `friend' declaration does
8925 *not* name a new type; i.e., given:
8927 struct S { friend struct T; };
8929 `T' is not a new type in the scope of `S'.
8931 Also, `new struct S' or `sizeof (struct S)' never results in the
8932 definition of a new type; a new type can only be declared in a
8933 declaration context. */
8935 type = xref_tag (tag_type, identifier,
8936 /*attributes=*/NULL_TREE,
8937 (is_friend
8938 || !is_declaration
8939 || cp_lexer_next_token_is_not (parser->lexer,
8940 CPP_SEMICOLON)));
8943 if (tag_type != enum_type)
8944 cp_parser_check_class_key (tag_type, type);
8945 return type;
8948 /* Parse an enum-specifier.
8950 enum-specifier:
8951 enum identifier [opt] { enumerator-list [opt] }
8953 Returns an ENUM_TYPE representing the enumeration. */
8955 static tree
8956 cp_parser_enum_specifier (cp_parser* parser)
8958 cp_token *token;
8959 tree identifier = NULL_TREE;
8960 tree type;
8962 /* Look for the `enum' keyword. */
8963 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8964 return error_mark_node;
8965 /* Peek at the next token. */
8966 token = cp_lexer_peek_token (parser->lexer);
8968 /* See if it is an identifier. */
8969 if (token->type == CPP_NAME)
8970 identifier = cp_parser_identifier (parser);
8972 /* Look for the `{'. */
8973 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8974 return error_mark_node;
8976 /* At this point, we're going ahead with the enum-specifier, even
8977 if some other problem occurs. */
8978 cp_parser_commit_to_tentative_parse (parser);
8980 /* Issue an error message if type-definitions are forbidden here. */
8981 cp_parser_check_type_definition (parser);
8983 /* Create the new type. */
8984 type = start_enum (identifier ? identifier : make_anon_name ());
8986 /* Peek at the next token. */
8987 token = cp_lexer_peek_token (parser->lexer);
8988 /* If it's not a `}', then there are some enumerators. */
8989 if (token->type != CPP_CLOSE_BRACE)
8990 cp_parser_enumerator_list (parser, type);
8991 /* Look for the `}'. */
8992 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8994 /* Finish up the enumeration. */
8995 finish_enum (type);
8997 return type;
9000 /* Parse an enumerator-list. The enumerators all have the indicated
9001 TYPE.
9003 enumerator-list:
9004 enumerator-definition
9005 enumerator-list , enumerator-definition */
9007 static void
9008 cp_parser_enumerator_list (cp_parser* parser, tree type)
9010 while (true)
9012 cp_token *token;
9014 /* Parse an enumerator-definition. */
9015 cp_parser_enumerator_definition (parser, type);
9016 /* Peek at the next token. */
9017 token = cp_lexer_peek_token (parser->lexer);
9018 /* If it's not a `,', then we've reached the end of the
9019 list. */
9020 if (token->type != CPP_COMMA)
9021 break;
9022 /* Otherwise, consume the `,' and keep going. */
9023 cp_lexer_consume_token (parser->lexer);
9024 /* If the next token is a `}', there is a trailing comma. */
9025 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9027 if (pedantic && !in_system_header)
9028 pedwarn ("comma at end of enumerator list");
9029 break;
9034 /* Parse an enumerator-definition. The enumerator has the indicated
9035 TYPE.
9037 enumerator-definition:
9038 enumerator
9039 enumerator = constant-expression
9041 enumerator:
9042 identifier */
9044 static void
9045 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9047 cp_token *token;
9048 tree identifier;
9049 tree value;
9051 /* Look for the identifier. */
9052 identifier = cp_parser_identifier (parser);
9053 if (identifier == error_mark_node)
9054 return;
9056 /* Peek at the next token. */
9057 token = cp_lexer_peek_token (parser->lexer);
9058 /* If it's an `=', then there's an explicit value. */
9059 if (token->type == CPP_EQ)
9061 /* Consume the `=' token. */
9062 cp_lexer_consume_token (parser->lexer);
9063 /* Parse the value. */
9064 value = cp_parser_constant_expression (parser,
9065 /*allow_non_constant=*/false,
9066 NULL);
9068 else
9069 value = NULL_TREE;
9071 /* Create the enumerator. */
9072 build_enumerator (identifier, value, type);
9075 /* Parse a namespace-name.
9077 namespace-name:
9078 original-namespace-name
9079 namespace-alias
9081 Returns the NAMESPACE_DECL for the namespace. */
9083 static tree
9084 cp_parser_namespace_name (cp_parser* parser)
9086 tree identifier;
9087 tree namespace_decl;
9089 /* Get the name of the namespace. */
9090 identifier = cp_parser_identifier (parser);
9091 if (identifier == error_mark_node)
9092 return error_mark_node;
9094 /* Look up the identifier in the currently active scope. Look only
9095 for namespaces, due to:
9097 [basic.lookup.udir]
9099 When looking up a namespace-name in a using-directive or alias
9100 definition, only namespace names are considered.
9102 And:
9104 [basic.lookup.qual]
9106 During the lookup of a name preceding the :: scope resolution
9107 operator, object, function, and enumerator names are ignored.
9109 (Note that cp_parser_class_or_namespace_name only calls this
9110 function if the token after the name is the scope resolution
9111 operator.) */
9112 namespace_decl = cp_parser_lookup_name (parser, identifier,
9113 /*is_type=*/false,
9114 /*is_namespace=*/true,
9115 /*check_dependency=*/true);
9116 /* If it's not a namespace, issue an error. */
9117 if (namespace_decl == error_mark_node
9118 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9120 cp_parser_error (parser, "expected namespace-name");
9121 namespace_decl = error_mark_node;
9124 return namespace_decl;
9127 /* Parse a namespace-definition.
9129 namespace-definition:
9130 named-namespace-definition
9131 unnamed-namespace-definition
9133 named-namespace-definition:
9134 original-namespace-definition
9135 extension-namespace-definition
9137 original-namespace-definition:
9138 namespace identifier { namespace-body }
9140 extension-namespace-definition:
9141 namespace original-namespace-name { namespace-body }
9143 unnamed-namespace-definition:
9144 namespace { namespace-body } */
9146 static void
9147 cp_parser_namespace_definition (cp_parser* parser)
9149 tree identifier;
9151 /* Look for the `namespace' keyword. */
9152 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9154 /* Get the name of the namespace. We do not attempt to distinguish
9155 between an original-namespace-definition and an
9156 extension-namespace-definition at this point. The semantic
9157 analysis routines are responsible for that. */
9158 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9159 identifier = cp_parser_identifier (parser);
9160 else
9161 identifier = NULL_TREE;
9163 /* Look for the `{' to start the namespace. */
9164 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9165 /* Start the namespace. */
9166 push_namespace (identifier);
9167 /* Parse the body of the namespace. */
9168 cp_parser_namespace_body (parser);
9169 /* Finish the namespace. */
9170 pop_namespace ();
9171 /* Look for the final `}'. */
9172 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9175 /* Parse a namespace-body.
9177 namespace-body:
9178 declaration-seq [opt] */
9180 static void
9181 cp_parser_namespace_body (cp_parser* parser)
9183 cp_parser_declaration_seq_opt (parser);
9186 /* Parse a namespace-alias-definition.
9188 namespace-alias-definition:
9189 namespace identifier = qualified-namespace-specifier ; */
9191 static void
9192 cp_parser_namespace_alias_definition (cp_parser* parser)
9194 tree identifier;
9195 tree namespace_specifier;
9197 /* Look for the `namespace' keyword. */
9198 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9199 /* Look for the identifier. */
9200 identifier = cp_parser_identifier (parser);
9201 if (identifier == error_mark_node)
9202 return;
9203 /* Look for the `=' token. */
9204 cp_parser_require (parser, CPP_EQ, "`='");
9205 /* Look for the qualified-namespace-specifier. */
9206 namespace_specifier
9207 = cp_parser_qualified_namespace_specifier (parser);
9208 /* Look for the `;' token. */
9209 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9211 /* Register the alias in the symbol table. */
9212 do_namespace_alias (identifier, namespace_specifier);
9215 /* Parse a qualified-namespace-specifier.
9217 qualified-namespace-specifier:
9218 :: [opt] nested-name-specifier [opt] namespace-name
9220 Returns a NAMESPACE_DECL corresponding to the specified
9221 namespace. */
9223 static tree
9224 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9226 /* Look for the optional `::'. */
9227 cp_parser_global_scope_opt (parser,
9228 /*current_scope_valid_p=*/false);
9230 /* Look for the optional nested-name-specifier. */
9231 cp_parser_nested_name_specifier_opt (parser,
9232 /*typename_keyword_p=*/false,
9233 /*check_dependency_p=*/true,
9234 /*type_p=*/false);
9236 return cp_parser_namespace_name (parser);
9239 /* Parse a using-declaration.
9241 using-declaration:
9242 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9243 using :: unqualified-id ; */
9245 static void
9246 cp_parser_using_declaration (cp_parser* parser)
9248 cp_token *token;
9249 bool typename_p = false;
9250 bool global_scope_p;
9251 tree decl;
9252 tree identifier;
9253 tree scope;
9255 /* Look for the `using' keyword. */
9256 cp_parser_require_keyword (parser, RID_USING, "`using'");
9258 /* Peek at the next token. */
9259 token = cp_lexer_peek_token (parser->lexer);
9260 /* See if it's `typename'. */
9261 if (token->keyword == RID_TYPENAME)
9263 /* Remember that we've seen it. */
9264 typename_p = true;
9265 /* Consume the `typename' token. */
9266 cp_lexer_consume_token (parser->lexer);
9269 /* Look for the optional global scope qualification. */
9270 global_scope_p
9271 = (cp_parser_global_scope_opt (parser,
9272 /*current_scope_valid_p=*/false)
9273 != NULL_TREE);
9275 /* If we saw `typename', or didn't see `::', then there must be a
9276 nested-name-specifier present. */
9277 if (typename_p || !global_scope_p)
9278 cp_parser_nested_name_specifier (parser, typename_p,
9279 /*check_dependency_p=*/true,
9280 /*type_p=*/false);
9281 /* Otherwise, we could be in either of the two productions. In that
9282 case, treat the nested-name-specifier as optional. */
9283 else
9284 cp_parser_nested_name_specifier_opt (parser,
9285 /*typename_keyword_p=*/false,
9286 /*check_dependency_p=*/true,
9287 /*type_p=*/false);
9289 /* Parse the unqualified-id. */
9290 identifier = cp_parser_unqualified_id (parser,
9291 /*template_keyword_p=*/false,
9292 /*check_dependency_p=*/true);
9294 /* The function we call to handle a using-declaration is different
9295 depending on what scope we are in. */
9296 scope = current_scope ();
9297 if (scope && TYPE_P (scope))
9299 /* Create the USING_DECL. */
9300 decl = do_class_using_decl (build_nt (SCOPE_REF,
9301 parser->scope,
9302 identifier));
9303 /* Add it to the list of members in this class. */
9304 finish_member_declaration (decl);
9306 else
9308 decl = cp_parser_lookup_name_simple (parser, identifier);
9309 if (decl == error_mark_node)
9311 if (parser->scope && parser->scope != global_namespace)
9312 error ("`%D::%D' has not been declared",
9313 parser->scope, identifier);
9314 else
9315 error ("`::%D' has not been declared", identifier);
9317 else if (scope)
9318 do_local_using_decl (decl);
9319 else
9320 do_toplevel_using_decl (decl);
9323 /* Look for the final `;'. */
9324 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9327 /* Parse a using-directive.
9329 using-directive:
9330 using namespace :: [opt] nested-name-specifier [opt]
9331 namespace-name ; */
9333 static void
9334 cp_parser_using_directive (cp_parser* parser)
9336 tree namespace_decl;
9338 /* Look for the `using' keyword. */
9339 cp_parser_require_keyword (parser, RID_USING, "`using'");
9340 /* And the `namespace' keyword. */
9341 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9342 /* Look for the optional `::' operator. */
9343 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9344 /* And the optional nested-name-sepcifier. */
9345 cp_parser_nested_name_specifier_opt (parser,
9346 /*typename_keyword_p=*/false,
9347 /*check_dependency_p=*/true,
9348 /*type_p=*/false);
9349 /* Get the namespace being used. */
9350 namespace_decl = cp_parser_namespace_name (parser);
9351 /* Update the symbol table. */
9352 do_using_directive (namespace_decl);
9353 /* Look for the final `;'. */
9354 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9357 /* Parse an asm-definition.
9359 asm-definition:
9360 asm ( string-literal ) ;
9362 GNU Extension:
9364 asm-definition:
9365 asm volatile [opt] ( string-literal ) ;
9366 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9367 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9368 : asm-operand-list [opt] ) ;
9369 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9370 : asm-operand-list [opt]
9371 : asm-operand-list [opt] ) ; */
9373 static void
9374 cp_parser_asm_definition (cp_parser* parser)
9376 cp_token *token;
9377 tree string;
9378 tree outputs = NULL_TREE;
9379 tree inputs = NULL_TREE;
9380 tree clobbers = NULL_TREE;
9381 tree asm_stmt;
9382 bool volatile_p = false;
9383 bool extended_p = false;
9385 /* Look for the `asm' keyword. */
9386 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9387 /* See if the next token is `volatile'. */
9388 if (cp_parser_allow_gnu_extensions_p (parser)
9389 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9391 /* Remember that we saw the `volatile' keyword. */
9392 volatile_p = true;
9393 /* Consume the token. */
9394 cp_lexer_consume_token (parser->lexer);
9396 /* Look for the opening `('. */
9397 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9398 /* Look for the string. */
9399 token = cp_parser_require (parser, CPP_STRING, "asm body");
9400 if (!token)
9401 return;
9402 string = token->value;
9403 /* If we're allowing GNU extensions, check for the extended assembly
9404 syntax. Unfortunately, the `:' tokens need not be separated by
9405 a space in C, and so, for compatibility, we tolerate that here
9406 too. Doing that means that we have to treat the `::' operator as
9407 two `:' tokens. */
9408 if (cp_parser_allow_gnu_extensions_p (parser)
9409 && at_function_scope_p ()
9410 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9411 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9413 bool inputs_p = false;
9414 bool clobbers_p = false;
9416 /* The extended syntax was used. */
9417 extended_p = true;
9419 /* Look for outputs. */
9420 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9422 /* Consume the `:'. */
9423 cp_lexer_consume_token (parser->lexer);
9424 /* Parse the output-operands. */
9425 if (cp_lexer_next_token_is_not (parser->lexer,
9426 CPP_COLON)
9427 && cp_lexer_next_token_is_not (parser->lexer,
9428 CPP_SCOPE)
9429 && cp_lexer_next_token_is_not (parser->lexer,
9430 CPP_CLOSE_PAREN))
9431 outputs = cp_parser_asm_operand_list (parser);
9433 /* If the next token is `::', there are no outputs, and the
9434 next token is the beginning of the inputs. */
9435 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9437 /* Consume the `::' token. */
9438 cp_lexer_consume_token (parser->lexer);
9439 /* The inputs are coming next. */
9440 inputs_p = true;
9443 /* Look for inputs. */
9444 if (inputs_p
9445 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9447 if (!inputs_p)
9448 /* Consume the `:'. */
9449 cp_lexer_consume_token (parser->lexer);
9450 /* Parse the output-operands. */
9451 if (cp_lexer_next_token_is_not (parser->lexer,
9452 CPP_COLON)
9453 && cp_lexer_next_token_is_not (parser->lexer,
9454 CPP_SCOPE)
9455 && cp_lexer_next_token_is_not (parser->lexer,
9456 CPP_CLOSE_PAREN))
9457 inputs = cp_parser_asm_operand_list (parser);
9459 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9460 /* The clobbers are coming next. */
9461 clobbers_p = true;
9463 /* Look for clobbers. */
9464 if (clobbers_p
9465 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9467 if (!clobbers_p)
9468 /* Consume the `:'. */
9469 cp_lexer_consume_token (parser->lexer);
9470 /* Parse the clobbers. */
9471 if (cp_lexer_next_token_is_not (parser->lexer,
9472 CPP_CLOSE_PAREN))
9473 clobbers = cp_parser_asm_clobber_list (parser);
9476 /* Look for the closing `)'. */
9477 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9478 cp_parser_skip_to_closing_parenthesis (parser);
9479 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9481 /* Create the ASM_STMT. */
9482 if (at_function_scope_p ())
9484 asm_stmt =
9485 finish_asm_stmt (volatile_p
9486 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9487 string, outputs, inputs, clobbers);
9488 /* If the extended syntax was not used, mark the ASM_STMT. */
9489 if (!extended_p)
9490 ASM_INPUT_P (asm_stmt) = 1;
9492 else
9493 assemble_asm (string);
9496 /* Declarators [gram.dcl.decl] */
9498 /* Parse an init-declarator.
9500 init-declarator:
9501 declarator initializer [opt]
9503 GNU Extension:
9505 init-declarator:
9506 declarator asm-specification [opt] attributes [opt] initializer [opt]
9508 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9509 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9510 then this declarator appears in a class scope. The new DECL created
9511 by this declarator is returned.
9513 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9514 for a function-definition here as well. If the declarator is a
9515 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9516 be TRUE upon return. By that point, the function-definition will
9517 have been completely parsed.
9519 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9520 is FALSE. */
9522 static tree
9523 cp_parser_init_declarator (cp_parser* parser,
9524 tree decl_specifiers,
9525 tree prefix_attributes,
9526 bool function_definition_allowed_p,
9527 bool member_p,
9528 bool* function_definition_p)
9530 cp_token *token;
9531 tree declarator;
9532 tree attributes;
9533 tree asm_specification;
9534 tree initializer;
9535 tree decl = NULL_TREE;
9536 tree scope;
9537 bool is_initialized;
9538 bool is_parenthesized_init;
9539 bool ctor_dtor_or_conv_p;
9540 bool friend_p;
9542 /* Assume that this is not the declarator for a function
9543 definition. */
9544 if (function_definition_p)
9545 *function_definition_p = false;
9547 /* Defer access checks while parsing the declarator; we cannot know
9548 what names are accessible until we know what is being
9549 declared. */
9550 resume_deferring_access_checks ();
9552 /* Parse the declarator. */
9553 declarator
9554 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9555 &ctor_dtor_or_conv_p);
9556 /* Gather up the deferred checks. */
9557 stop_deferring_access_checks ();
9559 /* If the DECLARATOR was erroneous, there's no need to go
9560 further. */
9561 if (declarator == error_mark_node)
9562 return error_mark_node;
9564 /* Figure out what scope the entity declared by the DECLARATOR is
9565 located in. `grokdeclarator' sometimes changes the scope, so
9566 we compute it now. */
9567 scope = get_scope_of_declarator (declarator);
9569 /* If we're allowing GNU extensions, look for an asm-specification
9570 and attributes. */
9571 if (cp_parser_allow_gnu_extensions_p (parser))
9573 /* Look for an asm-specification. */
9574 asm_specification = cp_parser_asm_specification_opt (parser);
9575 /* And attributes. */
9576 attributes = cp_parser_attributes_opt (parser);
9578 else
9580 asm_specification = NULL_TREE;
9581 attributes = NULL_TREE;
9584 /* Peek at the next token. */
9585 token = cp_lexer_peek_token (parser->lexer);
9586 /* Check to see if the token indicates the start of a
9587 function-definition. */
9588 if (cp_parser_token_starts_function_definition_p (token))
9590 if (!function_definition_allowed_p)
9592 /* If a function-definition should not appear here, issue an
9593 error message. */
9594 cp_parser_error (parser,
9595 "a function-definition is not allowed here");
9596 return error_mark_node;
9598 else
9600 /* Neither attributes nor an asm-specification are allowed
9601 on a function-definition. */
9602 if (asm_specification)
9603 error ("an asm-specification is not allowed on a function-definition");
9604 if (attributes)
9605 error ("attributes are not allowed on a function-definition");
9606 /* This is a function-definition. */
9607 *function_definition_p = true;
9609 /* Parse the function definition. */
9610 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9611 (parser, decl_specifiers, prefix_attributes, declarator));
9613 return decl;
9617 /* [dcl.dcl]
9619 Only in function declarations for constructors, destructors, and
9620 type conversions can the decl-specifier-seq be omitted.
9622 We explicitly postpone this check past the point where we handle
9623 function-definitions because we tolerate function-definitions
9624 that are missing their return types in some modes. */
9625 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9627 cp_parser_error (parser,
9628 "expected constructor, destructor, or type conversion");
9629 return error_mark_node;
9632 /* An `=' or an `(' indicates an initializer. */
9633 is_initialized = (token->type == CPP_EQ
9634 || token->type == CPP_OPEN_PAREN);
9635 /* If the init-declarator isn't initialized and isn't followed by a
9636 `,' or `;', it's not a valid init-declarator. */
9637 if (!is_initialized
9638 && token->type != CPP_COMMA
9639 && token->type != CPP_SEMICOLON)
9641 cp_parser_error (parser, "expected init-declarator");
9642 return error_mark_node;
9645 /* Because start_decl has side-effects, we should only call it if we
9646 know we're going ahead. By this point, we know that we cannot
9647 possibly be looking at any other construct. */
9648 cp_parser_commit_to_tentative_parse (parser);
9650 /* Check to see whether or not this declaration is a friend. */
9651 friend_p = cp_parser_friend_p (decl_specifiers);
9653 /* Check that the number of template-parameter-lists is OK. */
9654 if (!cp_parser_check_declarator_template_parameters (parser,
9655 declarator))
9656 return error_mark_node;
9658 /* Enter the newly declared entry in the symbol table. If we're
9659 processing a declaration in a class-specifier, we wait until
9660 after processing the initializer. */
9661 if (!member_p)
9663 if (parser->in_unbraced_linkage_specification_p)
9665 decl_specifiers = tree_cons (error_mark_node,
9666 get_identifier ("extern"),
9667 decl_specifiers);
9668 have_extern_spec = false;
9670 decl = start_decl (declarator,
9671 decl_specifiers,
9672 is_initialized,
9673 attributes,
9674 prefix_attributes);
9677 /* Enter the SCOPE. That way unqualified names appearing in the
9678 initializer will be looked up in SCOPE. */
9679 if (scope)
9680 push_scope (scope);
9682 /* Perform deferred access control checks, now that we know in which
9683 SCOPE the declared entity resides. */
9684 if (!member_p && decl)
9686 tree saved_current_function_decl = NULL_TREE;
9688 /* If the entity being declared is a function, pretend that we
9689 are in its scope. If it is a `friend', it may have access to
9690 things that would not otherwise be accessible. */
9691 if (TREE_CODE (decl) == FUNCTION_DECL)
9693 saved_current_function_decl = current_function_decl;
9694 current_function_decl = decl;
9697 /* Perform the access control checks for the declarator and the
9698 the decl-specifiers. */
9699 perform_deferred_access_checks ();
9701 /* Restore the saved value. */
9702 if (TREE_CODE (decl) == FUNCTION_DECL)
9703 current_function_decl = saved_current_function_decl;
9706 /* Parse the initializer. */
9707 if (is_initialized)
9708 initializer = cp_parser_initializer (parser, &is_parenthesized_init);
9709 else
9711 initializer = NULL_TREE;
9712 is_parenthesized_init = false;
9715 /* The old parser allows attributes to appear after a parenthesized
9716 initializer. Mark Mitchell proposed removing this functionality
9717 on the GCC mailing lists on 2002-08-13. This parser accepts the
9718 attributes -- but ignores them. */
9719 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9720 if (cp_parser_attributes_opt (parser))
9721 warning ("attributes after parenthesized initializer ignored");
9723 /* Leave the SCOPE, now that we have processed the initializer. It
9724 is important to do this before calling cp_finish_decl because it
9725 makes decisions about whether to create DECL_STMTs or not based
9726 on the current scope. */
9727 if (scope)
9728 pop_scope (scope);
9730 /* For an in-class declaration, use `grokfield' to create the
9731 declaration. */
9732 if (member_p)
9733 decl = grokfield (declarator, decl_specifiers,
9734 initializer, /*asmspec=*/NULL_TREE,
9735 /*attributes=*/NULL_TREE);
9737 /* Finish processing the declaration. But, skip friend
9738 declarations. */
9739 if (!friend_p && decl)
9740 cp_finish_decl (decl,
9741 initializer,
9742 asm_specification,
9743 /* If the initializer is in parentheses, then this is
9744 a direct-initialization, which means that an
9745 `explicit' constructor is OK. Otherwise, an
9746 `explicit' constructor cannot be used. */
9747 ((is_parenthesized_init || !is_initialized)
9748 ? 0 : LOOKUP_ONLYCONVERTING));
9750 return decl;
9753 /* Parse a declarator.
9755 declarator:
9756 direct-declarator
9757 ptr-operator declarator
9759 abstract-declarator:
9760 ptr-operator abstract-declarator [opt]
9761 direct-abstract-declarator
9763 GNU Extensions:
9765 declarator:
9766 attributes [opt] direct-declarator
9767 attributes [opt] ptr-operator declarator
9769 abstract-declarator:
9770 attributes [opt] ptr-operator abstract-declarator [opt]
9771 attributes [opt] direct-abstract-declarator
9773 Returns a representation of the declarator. If the declarator has
9774 the form `* declarator', then an INDIRECT_REF is returned, whose
9775 only operand is the sub-declarator. Analagously, `& declarator' is
9776 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9777 used. The first operand is the TYPE for `X'. The second operand
9778 is an INDIRECT_REF whose operand is the sub-declarator.
9780 Otherwise, the reprsentation is as for a direct-declarator.
9782 (It would be better to define a structure type to represent
9783 declarators, rather than abusing `tree' nodes to represent
9784 declarators. That would be much clearer and save some memory.
9785 There is no reason for declarators to be garbage-collected, for
9786 example; they are created during parser and no longer needed after
9787 `grokdeclarator' has been called.)
9789 For a ptr-operator that has the optional cv-qualifier-seq,
9790 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9791 node.
9793 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9794 true if this declarator represents a constructor, destructor, or
9795 type conversion operator. Otherwise, it is set to false.
9797 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9798 a decl-specifier-seq unless it declares a constructor, destructor,
9799 or conversion. It might seem that we could check this condition in
9800 semantic analysis, rather than parsing, but that makes it difficult
9801 to handle something like `f()'. We want to notice that there are
9802 no decl-specifiers, and therefore realize that this is an
9803 expression, not a declaration.) */
9805 static tree
9806 cp_parser_declarator (cp_parser* parser,
9807 cp_parser_declarator_kind dcl_kind,
9808 bool* ctor_dtor_or_conv_p)
9810 cp_token *token;
9811 tree declarator;
9812 enum tree_code code;
9813 tree cv_qualifier_seq;
9814 tree class_type;
9815 tree attributes = NULL_TREE;
9817 /* Assume this is not a constructor, destructor, or type-conversion
9818 operator. */
9819 if (ctor_dtor_or_conv_p)
9820 *ctor_dtor_or_conv_p = false;
9822 if (cp_parser_allow_gnu_extensions_p (parser))
9823 attributes = cp_parser_attributes_opt (parser);
9825 /* Peek at the next token. */
9826 token = cp_lexer_peek_token (parser->lexer);
9828 /* Check for the ptr-operator production. */
9829 cp_parser_parse_tentatively (parser);
9830 /* Parse the ptr-operator. */
9831 code = cp_parser_ptr_operator (parser,
9832 &class_type,
9833 &cv_qualifier_seq);
9834 /* If that worked, then we have a ptr-operator. */
9835 if (cp_parser_parse_definitely (parser))
9837 /* The dependent declarator is optional if we are parsing an
9838 abstract-declarator. */
9839 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9840 cp_parser_parse_tentatively (parser);
9842 /* Parse the dependent declarator. */
9843 declarator = cp_parser_declarator (parser, dcl_kind,
9844 /*ctor_dtor_or_conv_p=*/NULL);
9846 /* If we are parsing an abstract-declarator, we must handle the
9847 case where the dependent declarator is absent. */
9848 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9849 && !cp_parser_parse_definitely (parser))
9850 declarator = NULL_TREE;
9852 /* Build the representation of the ptr-operator. */
9853 if (code == INDIRECT_REF)
9854 declarator = make_pointer_declarator (cv_qualifier_seq,
9855 declarator);
9856 else
9857 declarator = make_reference_declarator (cv_qualifier_seq,
9858 declarator);
9859 /* Handle the pointer-to-member case. */
9860 if (class_type)
9861 declarator = build_nt (SCOPE_REF, class_type, declarator);
9863 /* Everything else is a direct-declarator. */
9864 else
9865 declarator = cp_parser_direct_declarator (parser,
9866 dcl_kind,
9867 ctor_dtor_or_conv_p);
9869 if (attributes && declarator != error_mark_node)
9870 declarator = tree_cons (attributes, declarator, NULL_TREE);
9872 return declarator;
9875 /* Parse a direct-declarator or direct-abstract-declarator.
9877 direct-declarator:
9878 declarator-id
9879 direct-declarator ( parameter-declaration-clause )
9880 cv-qualifier-seq [opt]
9881 exception-specification [opt]
9882 direct-declarator [ constant-expression [opt] ]
9883 ( declarator )
9885 direct-abstract-declarator:
9886 direct-abstract-declarator [opt]
9887 ( parameter-declaration-clause )
9888 cv-qualifier-seq [opt]
9889 exception-specification [opt]
9890 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9891 ( abstract-declarator )
9893 Returns a representation of the declarator. DCL_KIND is
9894 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9895 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9896 we are parsing a direct-declarator. It is
9897 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9898 of ambiguity we prefer an abstract declarator, as per
9899 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9900 cp_parser_declarator.
9902 For the declarator-id production, the representation is as for an
9903 id-expression, except that a qualified name is represented as a
9904 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9905 see the documentation of the FUNCTION_DECLARATOR_* macros for
9906 information about how to find the various declarator components.
9907 An array-declarator is represented as an ARRAY_REF. The
9908 direct-declarator is the first operand; the constant-expression
9909 indicating the size of the array is the second operand. */
9911 static tree
9912 cp_parser_direct_declarator (cp_parser* parser,
9913 cp_parser_declarator_kind dcl_kind,
9914 bool* ctor_dtor_or_conv_p)
9916 cp_token *token;
9917 tree declarator = NULL_TREE;
9918 tree scope = NULL_TREE;
9919 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9920 bool saved_in_declarator_p = parser->in_declarator_p;
9921 bool first = true;
9923 while (true)
9925 /* Peek at the next token. */
9926 token = cp_lexer_peek_token (parser->lexer);
9927 if (token->type == CPP_OPEN_PAREN)
9929 /* This is either a parameter-declaration-clause, or a
9930 parenthesized declarator. When we know we are parsing a
9931 named declarator, it must be a paranthesized declarator
9932 if FIRST is true. For instance, `(int)' is a
9933 parameter-declaration-clause, with an omitted
9934 direct-abstract-declarator. But `((*))', is a
9935 parenthesized abstract declarator. Finally, when T is a
9936 template parameter `(T)' is a
9937 paremeter-declaration-clause, and not a parenthesized
9938 named declarator.
9940 We first try and parse a parameter-declaration-clause,
9941 and then try a nested declarator (if FIRST is true).
9943 It is not an error for it not to be a
9944 parameter-declaration-clause, even when FIRST is
9945 false. Consider,
9947 int i (int);
9948 int i (3);
9950 The first is the declaration of a function while the
9951 second is a the definition of a variable, including its
9952 initializer.
9954 Having seen only the parenthesis, we cannot know which of
9955 these two alternatives should be selected. Even more
9956 complex are examples like:
9958 int i (int (a));
9959 int i (int (3));
9961 The former is a function-declaration; the latter is a
9962 variable initialization.
9964 Thus again, we try a parameter-declation-clause, and if
9965 that fails, we back out and return. */
9967 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9969 tree params;
9971 cp_parser_parse_tentatively (parser);
9973 /* Consume the `('. */
9974 cp_lexer_consume_token (parser->lexer);
9975 if (first)
9977 /* If this is going to be an abstract declarator, we're
9978 in a declarator and we can't have default args. */
9979 parser->default_arg_ok_p = false;
9980 parser->in_declarator_p = true;
9983 /* Parse the parameter-declaration-clause. */
9984 params = cp_parser_parameter_declaration_clause (parser);
9986 /* If all went well, parse the cv-qualifier-seq and the
9987 exception-specfication. */
9988 if (cp_parser_parse_definitely (parser))
9990 tree cv_qualifiers;
9991 tree exception_specification;
9993 first = false;
9994 /* Consume the `)'. */
9995 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9997 /* Parse the cv-qualifier-seq. */
9998 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9999 /* And the exception-specification. */
10000 exception_specification
10001 = cp_parser_exception_specification_opt (parser);
10003 /* Create the function-declarator. */
10004 declarator = make_call_declarator (declarator,
10005 params,
10006 cv_qualifiers,
10007 exception_specification);
10008 /* Any subsequent parameter lists are to do with
10009 return type, so are not those of the declared
10010 function. */
10011 parser->default_arg_ok_p = false;
10013 /* Repeat the main loop. */
10014 continue;
10018 /* If this is the first, we can try a parenthesized
10019 declarator. */
10020 if (first)
10022 parser->default_arg_ok_p = saved_default_arg_ok_p;
10023 parser->in_declarator_p = saved_in_declarator_p;
10025 /* Consume the `('. */
10026 cp_lexer_consume_token (parser->lexer);
10027 /* Parse the nested declarator. */
10028 declarator
10029 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10030 first = false;
10031 /* Expect a `)'. */
10032 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10033 declarator = error_mark_node;
10034 if (declarator == error_mark_node)
10035 break;
10037 goto handle_declarator;
10039 /* Otherwise, we must be done. */
10040 else
10041 break;
10043 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10044 && token->type == CPP_OPEN_SQUARE)
10046 /* Parse an array-declarator. */
10047 tree bounds;
10049 first = false;
10050 parser->default_arg_ok_p = false;
10051 parser->in_declarator_p = true;
10052 /* Consume the `['. */
10053 cp_lexer_consume_token (parser->lexer);
10054 /* Peek at the next token. */
10055 token = cp_lexer_peek_token (parser->lexer);
10056 /* If the next token is `]', then there is no
10057 constant-expression. */
10058 if (token->type != CPP_CLOSE_SQUARE)
10060 bool non_constant_p;
10062 bounds
10063 = cp_parser_constant_expression (parser,
10064 /*allow_non_constant=*/true,
10065 &non_constant_p);
10066 /* If we're in a template, but the constant-expression
10067 isn't value dependent, simplify it. We're supposed
10068 to treat:
10070 template <typename T> void f(T[1 + 1]);
10071 template <typename T> void f(T[2]);
10073 as two declarations of the same function, for
10074 example. */
10075 if (processing_template_decl
10076 && !non_constant_p
10077 && !value_dependent_expression_p (bounds))
10079 HOST_WIDE_INT saved_processing_template_decl;
10081 saved_processing_template_decl = processing_template_decl;
10082 processing_template_decl = 0;
10083 bounds = build_expr_from_tree (bounds);
10084 processing_template_decl = saved_processing_template_decl;
10087 else
10088 bounds = NULL_TREE;
10089 /* Look for the closing `]'. */
10090 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10092 declarator = error_mark_node;
10093 break;
10096 declarator = build_nt (ARRAY_REF, declarator, bounds);
10098 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10100 /* Parse a declarator_id */
10101 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10102 cp_parser_parse_tentatively (parser);
10103 declarator = cp_parser_declarator_id (parser);
10104 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10106 if (!cp_parser_parse_definitely (parser))
10107 declarator = error_mark_node;
10108 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10110 cp_parser_error (parser, "expected unqualified-id");
10111 declarator = error_mark_node;
10115 if (declarator == error_mark_node)
10116 break;
10118 if (TREE_CODE (declarator) == SCOPE_REF)
10120 tree scope = TREE_OPERAND (declarator, 0);
10122 /* In the declaration of a member of a template class
10123 outside of the class itself, the SCOPE will sometimes
10124 be a TYPENAME_TYPE. For example, given:
10126 template <typename T>
10127 int S<T>::R::i = 3;
10129 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10130 this context, we must resolve S<T>::R to an ordinary
10131 type, rather than a typename type.
10133 The reason we normally avoid resolving TYPENAME_TYPEs
10134 is that a specialization of `S' might render
10135 `S<T>::R' not a type. However, if `S' is
10136 specialized, then this `i' will not be used, so there
10137 is no harm in resolving the types here. */
10138 if (TREE_CODE (scope) == TYPENAME_TYPE)
10140 tree type;
10142 /* Resolve the TYPENAME_TYPE. */
10143 type = resolve_typename_type (scope,
10144 /*only_current_p=*/false);
10145 /* If that failed, the declarator is invalid. */
10146 if (type != error_mark_node)
10147 scope = type;
10148 /* Build a new DECLARATOR. */
10149 declarator = build_nt (SCOPE_REF,
10150 scope,
10151 TREE_OPERAND (declarator, 1));
10155 /* Check to see whether the declarator-id names a constructor,
10156 destructor, or conversion. */
10157 if (declarator && ctor_dtor_or_conv_p
10158 && ((TREE_CODE (declarator) == SCOPE_REF
10159 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10160 || (TREE_CODE (declarator) != SCOPE_REF
10161 && at_class_scope_p ())))
10163 tree unqualified_name;
10164 tree class_type;
10166 /* Get the unqualified part of the name. */
10167 if (TREE_CODE (declarator) == SCOPE_REF)
10169 class_type = TREE_OPERAND (declarator, 0);
10170 unqualified_name = TREE_OPERAND (declarator, 1);
10172 else
10174 class_type = current_class_type;
10175 unqualified_name = declarator;
10178 /* See if it names ctor, dtor or conv. */
10179 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10180 || IDENTIFIER_TYPENAME_P (unqualified_name)
10181 || constructor_name_p (unqualified_name, class_type))
10182 *ctor_dtor_or_conv_p = true;
10185 handle_declarator:;
10186 scope = get_scope_of_declarator (declarator);
10187 if (scope)
10188 /* Any names that appear after the declarator-id for a member
10189 are looked up in the containing scope. */
10190 push_scope (scope);
10191 parser->in_declarator_p = true;
10192 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10193 || (declarator
10194 && (TREE_CODE (declarator) == SCOPE_REF
10195 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10196 /* Default args are only allowed on function
10197 declarations. */
10198 parser->default_arg_ok_p = saved_default_arg_ok_p;
10199 else
10200 parser->default_arg_ok_p = false;
10202 first = false;
10204 /* We're done. */
10205 else
10206 break;
10209 /* For an abstract declarator, we might wind up with nothing at this
10210 point. That's an error; the declarator is not optional. */
10211 if (!declarator)
10212 cp_parser_error (parser, "expected declarator");
10214 /* If we entered a scope, we must exit it now. */
10215 if (scope)
10216 pop_scope (scope);
10218 parser->default_arg_ok_p = saved_default_arg_ok_p;
10219 parser->in_declarator_p = saved_in_declarator_p;
10221 return declarator;
10224 /* Parse a ptr-operator.
10226 ptr-operator:
10227 * cv-qualifier-seq [opt]
10229 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10231 GNU Extension:
10233 ptr-operator:
10234 & cv-qualifier-seq [opt]
10236 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10237 used. Returns ADDR_EXPR if a reference was used. In the
10238 case of a pointer-to-member, *TYPE is filled in with the
10239 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10240 with the cv-qualifier-seq, or NULL_TREE, if there are no
10241 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10243 static enum tree_code
10244 cp_parser_ptr_operator (cp_parser* parser,
10245 tree* type,
10246 tree* cv_qualifier_seq)
10248 enum tree_code code = ERROR_MARK;
10249 cp_token *token;
10251 /* Assume that it's not a pointer-to-member. */
10252 *type = NULL_TREE;
10253 /* And that there are no cv-qualifiers. */
10254 *cv_qualifier_seq = NULL_TREE;
10256 /* Peek at the next token. */
10257 token = cp_lexer_peek_token (parser->lexer);
10258 /* If it's a `*' or `&' we have a pointer or reference. */
10259 if (token->type == CPP_MULT || token->type == CPP_AND)
10261 /* Remember which ptr-operator we were processing. */
10262 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10264 /* Consume the `*' or `&'. */
10265 cp_lexer_consume_token (parser->lexer);
10267 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10268 `&', if we are allowing GNU extensions. (The only qualifier
10269 that can legally appear after `&' is `restrict', but that is
10270 enforced during semantic analysis. */
10271 if (code == INDIRECT_REF
10272 || cp_parser_allow_gnu_extensions_p (parser))
10273 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10275 else
10277 /* Try the pointer-to-member case. */
10278 cp_parser_parse_tentatively (parser);
10279 /* Look for the optional `::' operator. */
10280 cp_parser_global_scope_opt (parser,
10281 /*current_scope_valid_p=*/false);
10282 /* Look for the nested-name specifier. */
10283 cp_parser_nested_name_specifier (parser,
10284 /*typename_keyword_p=*/false,
10285 /*check_dependency_p=*/true,
10286 /*type_p=*/false);
10287 /* If we found it, and the next token is a `*', then we are
10288 indeed looking at a pointer-to-member operator. */
10289 if (!cp_parser_error_occurred (parser)
10290 && cp_parser_require (parser, CPP_MULT, "`*'"))
10292 /* The type of which the member is a member is given by the
10293 current SCOPE. */
10294 *type = parser->scope;
10295 /* The next name will not be qualified. */
10296 parser->scope = NULL_TREE;
10297 parser->qualifying_scope = NULL_TREE;
10298 parser->object_scope = NULL_TREE;
10299 /* Indicate that the `*' operator was used. */
10300 code = INDIRECT_REF;
10301 /* Look for the optional cv-qualifier-seq. */
10302 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10304 /* If that didn't work we don't have a ptr-operator. */
10305 if (!cp_parser_parse_definitely (parser))
10306 cp_parser_error (parser, "expected ptr-operator");
10309 return code;
10312 /* Parse an (optional) cv-qualifier-seq.
10314 cv-qualifier-seq:
10315 cv-qualifier cv-qualifier-seq [opt]
10317 Returns a TREE_LIST. The TREE_VALUE of each node is the
10318 representation of a cv-qualifier. */
10320 static tree
10321 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10323 tree cv_qualifiers = NULL_TREE;
10325 while (true)
10327 tree cv_qualifier;
10329 /* Look for the next cv-qualifier. */
10330 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10331 /* If we didn't find one, we're done. */
10332 if (!cv_qualifier)
10333 break;
10335 /* Add this cv-qualifier to the list. */
10336 cv_qualifiers
10337 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10340 /* We built up the list in reverse order. */
10341 return nreverse (cv_qualifiers);
10344 /* Parse an (optional) cv-qualifier.
10346 cv-qualifier:
10347 const
10348 volatile
10350 GNU Extension:
10352 cv-qualifier:
10353 __restrict__ */
10355 static tree
10356 cp_parser_cv_qualifier_opt (cp_parser* parser)
10358 cp_token *token;
10359 tree cv_qualifier = NULL_TREE;
10361 /* Peek at the next token. */
10362 token = cp_lexer_peek_token (parser->lexer);
10363 /* See if it's a cv-qualifier. */
10364 switch (token->keyword)
10366 case RID_CONST:
10367 case RID_VOLATILE:
10368 case RID_RESTRICT:
10369 /* Save the value of the token. */
10370 cv_qualifier = token->value;
10371 /* Consume the token. */
10372 cp_lexer_consume_token (parser->lexer);
10373 break;
10375 default:
10376 break;
10379 return cv_qualifier;
10382 /* Parse a declarator-id.
10384 declarator-id:
10385 id-expression
10386 :: [opt] nested-name-specifier [opt] type-name
10388 In the `id-expression' case, the value returned is as for
10389 cp_parser_id_expression if the id-expression was an unqualified-id.
10390 If the id-expression was a qualified-id, then a SCOPE_REF is
10391 returned. The first operand is the scope (either a NAMESPACE_DECL
10392 or TREE_TYPE), but the second is still just a representation of an
10393 unqualified-id. */
10395 static tree
10396 cp_parser_declarator_id (cp_parser* parser)
10398 tree id_expression;
10400 /* The expression must be an id-expression. Assume that qualified
10401 names are the names of types so that:
10403 template <class T>
10404 int S<T>::R::i = 3;
10406 will work; we must treat `S<T>::R' as the name of a type.
10407 Similarly, assume that qualified names are templates, where
10408 required, so that:
10410 template <class T>
10411 int S<T>::R<T>::i = 3;
10413 will work, too. */
10414 id_expression = cp_parser_id_expression (parser,
10415 /*template_keyword_p=*/false,
10416 /*check_dependency_p=*/false,
10417 /*template_p=*/NULL);
10418 /* If the name was qualified, create a SCOPE_REF to represent
10419 that. */
10420 if (parser->scope)
10422 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10423 parser->scope = NULL_TREE;
10426 return id_expression;
10429 /* Parse a type-id.
10431 type-id:
10432 type-specifier-seq abstract-declarator [opt]
10434 Returns the TYPE specified. */
10436 static tree
10437 cp_parser_type_id (cp_parser* parser)
10439 tree type_specifier_seq;
10440 tree abstract_declarator;
10442 /* Parse the type-specifier-seq. */
10443 type_specifier_seq
10444 = cp_parser_type_specifier_seq (parser);
10445 if (type_specifier_seq == error_mark_node)
10446 return error_mark_node;
10448 /* There might or might not be an abstract declarator. */
10449 cp_parser_parse_tentatively (parser);
10450 /* Look for the declarator. */
10451 abstract_declarator
10452 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10453 /* Check to see if there really was a declarator. */
10454 if (!cp_parser_parse_definitely (parser))
10455 abstract_declarator = NULL_TREE;
10457 return groktypename (build_tree_list (type_specifier_seq,
10458 abstract_declarator));
10461 /* Parse a type-specifier-seq.
10463 type-specifier-seq:
10464 type-specifier type-specifier-seq [opt]
10466 GNU extension:
10468 type-specifier-seq:
10469 attributes type-specifier-seq [opt]
10471 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10472 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10474 static tree
10475 cp_parser_type_specifier_seq (cp_parser* parser)
10477 bool seen_type_specifier = false;
10478 tree type_specifier_seq = NULL_TREE;
10480 /* Parse the type-specifiers and attributes. */
10481 while (true)
10483 tree type_specifier;
10485 /* Check for attributes first. */
10486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10488 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10489 NULL_TREE,
10490 type_specifier_seq);
10491 continue;
10494 /* After the first type-specifier, others are optional. */
10495 if (seen_type_specifier)
10496 cp_parser_parse_tentatively (parser);
10497 /* Look for the type-specifier. */
10498 type_specifier = cp_parser_type_specifier (parser,
10499 CP_PARSER_FLAGS_NONE,
10500 /*is_friend=*/false,
10501 /*is_declaration=*/false,
10502 NULL,
10503 NULL);
10504 /* If the first type-specifier could not be found, this is not a
10505 type-specifier-seq at all. */
10506 if (!seen_type_specifier && type_specifier == error_mark_node)
10507 return error_mark_node;
10508 /* If subsequent type-specifiers could not be found, the
10509 type-specifier-seq is complete. */
10510 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10511 break;
10513 /* Add the new type-specifier to the list. */
10514 type_specifier_seq
10515 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10516 seen_type_specifier = true;
10519 /* We built up the list in reverse order. */
10520 return nreverse (type_specifier_seq);
10523 /* Parse a parameter-declaration-clause.
10525 parameter-declaration-clause:
10526 parameter-declaration-list [opt] ... [opt]
10527 parameter-declaration-list , ...
10529 Returns a representation for the parameter declarations. Each node
10530 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10531 representation.) If the parameter-declaration-clause ends with an
10532 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10533 list. A return value of NULL_TREE indicates a
10534 parameter-declaration-clause consisting only of an ellipsis. */
10536 static tree
10537 cp_parser_parameter_declaration_clause (cp_parser* parser)
10539 tree parameters;
10540 cp_token *token;
10541 bool ellipsis_p;
10543 /* Peek at the next token. */
10544 token = cp_lexer_peek_token (parser->lexer);
10545 /* Check for trivial parameter-declaration-clauses. */
10546 if (token->type == CPP_ELLIPSIS)
10548 /* Consume the `...' token. */
10549 cp_lexer_consume_token (parser->lexer);
10550 return NULL_TREE;
10552 else if (token->type == CPP_CLOSE_PAREN)
10553 /* There are no parameters. */
10555 #ifndef NO_IMPLICIT_EXTERN_C
10556 if (in_system_header && current_class_type == NULL
10557 && current_lang_name == lang_name_c)
10558 return NULL_TREE;
10559 else
10560 #endif
10561 return void_list_node;
10563 /* Check for `(void)', too, which is a special case. */
10564 else if (token->keyword == RID_VOID
10565 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10566 == CPP_CLOSE_PAREN))
10568 /* Consume the `void' token. */
10569 cp_lexer_consume_token (parser->lexer);
10570 /* There are no parameters. */
10571 return void_list_node;
10574 /* Parse the parameter-declaration-list. */
10575 parameters = cp_parser_parameter_declaration_list (parser);
10576 /* If a parse error occurred while parsing the
10577 parameter-declaration-list, then the entire
10578 parameter-declaration-clause is erroneous. */
10579 if (parameters == error_mark_node)
10580 return error_mark_node;
10582 /* Peek at the next token. */
10583 token = cp_lexer_peek_token (parser->lexer);
10584 /* If it's a `,', the clause should terminate with an ellipsis. */
10585 if (token->type == CPP_COMMA)
10587 /* Consume the `,'. */
10588 cp_lexer_consume_token (parser->lexer);
10589 /* Expect an ellipsis. */
10590 ellipsis_p
10591 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10593 /* It might also be `...' if the optional trailing `,' was
10594 omitted. */
10595 else if (token->type == CPP_ELLIPSIS)
10597 /* Consume the `...' token. */
10598 cp_lexer_consume_token (parser->lexer);
10599 /* And remember that we saw it. */
10600 ellipsis_p = true;
10602 else
10603 ellipsis_p = false;
10605 /* Finish the parameter list. */
10606 return finish_parmlist (parameters, ellipsis_p);
10609 /* Parse a parameter-declaration-list.
10611 parameter-declaration-list:
10612 parameter-declaration
10613 parameter-declaration-list , parameter-declaration
10615 Returns a representation of the parameter-declaration-list, as for
10616 cp_parser_parameter_declaration_clause. However, the
10617 `void_list_node' is never appended to the list. */
10619 static tree
10620 cp_parser_parameter_declaration_list (cp_parser* parser)
10622 tree parameters = NULL_TREE;
10624 /* Look for more parameters. */
10625 while (true)
10627 tree parameter;
10628 /* Parse the parameter. */
10629 parameter
10630 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10632 /* If a parse error ocurred parsing the parameter declaration,
10633 then the entire parameter-declaration-list is erroneous. */
10634 if (parameter == error_mark_node)
10636 parameters = error_mark_node;
10637 break;
10639 /* Add the new parameter to the list. */
10640 TREE_CHAIN (parameter) = parameters;
10641 parameters = parameter;
10643 /* Peek at the next token. */
10644 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10645 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10646 /* The parameter-declaration-list is complete. */
10647 break;
10648 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10650 cp_token *token;
10652 /* Peek at the next token. */
10653 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10654 /* If it's an ellipsis, then the list is complete. */
10655 if (token->type == CPP_ELLIPSIS)
10656 break;
10657 /* Otherwise, there must be more parameters. Consume the
10658 `,'. */
10659 cp_lexer_consume_token (parser->lexer);
10661 else
10663 cp_parser_error (parser, "expected `,' or `...'");
10664 break;
10668 /* We built up the list in reverse order; straighten it out now. */
10669 return nreverse (parameters);
10672 /* Parse a parameter declaration.
10674 parameter-declaration:
10675 decl-specifier-seq declarator
10676 decl-specifier-seq declarator = assignment-expression
10677 decl-specifier-seq abstract-declarator [opt]
10678 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10680 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10681 declares a template parameter. (In that case, a non-nested `>'
10682 token encountered during the parsing of the assignment-expression
10683 is not interpreted as a greater-than operator.)
10685 Returns a TREE_LIST representing the parameter-declaration. The
10686 TREE_VALUE is a representation of the decl-specifier-seq and
10687 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10688 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10689 TREE_VALUE represents the declarator. */
10691 static tree
10692 cp_parser_parameter_declaration (cp_parser *parser,
10693 bool template_parm_p)
10695 bool declares_class_or_enum;
10696 bool greater_than_is_operator_p;
10697 tree decl_specifiers;
10698 tree attributes;
10699 tree declarator;
10700 tree default_argument;
10701 tree parameter;
10702 cp_token *token;
10703 const char *saved_message;
10705 /* In a template parameter, `>' is not an operator.
10707 [temp.param]
10709 When parsing a default template-argument for a non-type
10710 template-parameter, the first non-nested `>' is taken as the end
10711 of the template parameter-list rather than a greater-than
10712 operator. */
10713 greater_than_is_operator_p = !template_parm_p;
10715 /* Type definitions may not appear in parameter types. */
10716 saved_message = parser->type_definition_forbidden_message;
10717 parser->type_definition_forbidden_message
10718 = "types may not be defined in parameter types";
10720 /* Parse the declaration-specifiers. */
10721 decl_specifiers
10722 = cp_parser_decl_specifier_seq (parser,
10723 CP_PARSER_FLAGS_NONE,
10724 &attributes,
10725 &declares_class_or_enum);
10726 /* If an error occurred, there's no reason to attempt to parse the
10727 rest of the declaration. */
10728 if (cp_parser_error_occurred (parser))
10730 parser->type_definition_forbidden_message = saved_message;
10731 return error_mark_node;
10734 /* Peek at the next token. */
10735 token = cp_lexer_peek_token (parser->lexer);
10736 /* If the next token is a `)', `,', `=', `>', or `...', then there
10737 is no declarator. */
10738 if (token->type == CPP_CLOSE_PAREN
10739 || token->type == CPP_COMMA
10740 || token->type == CPP_EQ
10741 || token->type == CPP_ELLIPSIS
10742 || token->type == CPP_GREATER)
10743 declarator = NULL_TREE;
10744 /* Otherwise, there should be a declarator. */
10745 else
10747 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10748 parser->default_arg_ok_p = false;
10750 declarator = cp_parser_declarator (parser,
10751 CP_PARSER_DECLARATOR_EITHER,
10752 /*ctor_dtor_or_conv_p=*/NULL);
10753 parser->default_arg_ok_p = saved_default_arg_ok_p;
10754 /* After the declarator, allow more attributes. */
10755 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10758 /* The restriction on defining new types applies only to the type
10759 of the parameter, not to the default argument. */
10760 parser->type_definition_forbidden_message = saved_message;
10762 /* If the next token is `=', then process a default argument. */
10763 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10765 bool saved_greater_than_is_operator_p;
10766 /* Consume the `='. */
10767 cp_lexer_consume_token (parser->lexer);
10769 /* If we are defining a class, then the tokens that make up the
10770 default argument must be saved and processed later. */
10771 if (!template_parm_p && at_class_scope_p ()
10772 && TYPE_BEING_DEFINED (current_class_type))
10774 unsigned depth = 0;
10776 /* Create a DEFAULT_ARG to represented the unparsed default
10777 argument. */
10778 default_argument = make_node (DEFAULT_ARG);
10779 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10781 /* Add tokens until we have processed the entire default
10782 argument. */
10783 while (true)
10785 bool done = false;
10786 cp_token *token;
10788 /* Peek at the next token. */
10789 token = cp_lexer_peek_token (parser->lexer);
10790 /* What we do depends on what token we have. */
10791 switch (token->type)
10793 /* In valid code, a default argument must be
10794 immediately followed by a `,' `)', or `...'. */
10795 case CPP_COMMA:
10796 case CPP_CLOSE_PAREN:
10797 case CPP_ELLIPSIS:
10798 /* If we run into a non-nested `;', `}', or `]',
10799 then the code is invalid -- but the default
10800 argument is certainly over. */
10801 case CPP_SEMICOLON:
10802 case CPP_CLOSE_BRACE:
10803 case CPP_CLOSE_SQUARE:
10804 if (depth == 0)
10805 done = true;
10806 /* Update DEPTH, if necessary. */
10807 else if (token->type == CPP_CLOSE_PAREN
10808 || token->type == CPP_CLOSE_BRACE
10809 || token->type == CPP_CLOSE_SQUARE)
10810 --depth;
10811 break;
10813 case CPP_OPEN_PAREN:
10814 case CPP_OPEN_SQUARE:
10815 case CPP_OPEN_BRACE:
10816 ++depth;
10817 break;
10819 case CPP_GREATER:
10820 /* If we see a non-nested `>', and `>' is not an
10821 operator, then it marks the end of the default
10822 argument. */
10823 if (!depth && !greater_than_is_operator_p)
10824 done = true;
10825 break;
10827 /* If we run out of tokens, issue an error message. */
10828 case CPP_EOF:
10829 error ("file ends in default argument");
10830 done = true;
10831 break;
10833 case CPP_NAME:
10834 case CPP_SCOPE:
10835 /* In these cases, we should look for template-ids.
10836 For example, if the default argument is
10837 `X<int, double>()', we need to do name lookup to
10838 figure out whether or not `X' is a template; if
10839 so, the `,' does not end the deault argument.
10841 That is not yet done. */
10842 break;
10844 default:
10845 break;
10848 /* If we've reached the end, stop. */
10849 if (done)
10850 break;
10852 /* Add the token to the token block. */
10853 token = cp_lexer_consume_token (parser->lexer);
10854 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10855 token);
10858 /* Outside of a class definition, we can just parse the
10859 assignment-expression. */
10860 else
10862 bool saved_local_variables_forbidden_p;
10864 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10865 set correctly. */
10866 saved_greater_than_is_operator_p
10867 = parser->greater_than_is_operator_p;
10868 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10869 /* Local variable names (and the `this' keyword) may not
10870 appear in a default argument. */
10871 saved_local_variables_forbidden_p
10872 = parser->local_variables_forbidden_p;
10873 parser->local_variables_forbidden_p = true;
10874 /* Parse the assignment-expression. */
10875 default_argument = cp_parser_assignment_expression (parser);
10876 /* Restore saved state. */
10877 parser->greater_than_is_operator_p
10878 = saved_greater_than_is_operator_p;
10879 parser->local_variables_forbidden_p
10880 = saved_local_variables_forbidden_p;
10882 if (!parser->default_arg_ok_p)
10884 pedwarn ("default arguments are only permitted on functions");
10885 if (flag_pedantic_errors)
10886 default_argument = NULL_TREE;
10889 else
10890 default_argument = NULL_TREE;
10892 /* Create the representation of the parameter. */
10893 if (attributes)
10894 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10895 parameter = build_tree_list (default_argument,
10896 build_tree_list (decl_specifiers,
10897 declarator));
10899 return parameter;
10902 /* Parse a function-definition.
10904 function-definition:
10905 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10906 function-body
10907 decl-specifier-seq [opt] declarator function-try-block
10909 GNU Extension:
10911 function-definition:
10912 __extension__ function-definition
10914 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10915 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10916 be a `friend'. */
10918 static tree
10919 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10921 tree decl_specifiers;
10922 tree attributes;
10923 tree declarator;
10924 tree fn;
10925 cp_token *token;
10926 bool declares_class_or_enum;
10927 bool member_p;
10928 /* The saved value of the PEDANTIC flag. */
10929 int saved_pedantic;
10931 /* Any pending qualification must be cleared by our caller. It is
10932 more robust to force the callers to clear PARSER->SCOPE than to
10933 do it here since if the qualification is in effect here, it might
10934 also end up in effect elsewhere that it is not intended. */
10935 my_friendly_assert (!parser->scope, 20010821);
10937 /* Handle `__extension__'. */
10938 if (cp_parser_extension_opt (parser, &saved_pedantic))
10940 /* Parse the function-definition. */
10941 fn = cp_parser_function_definition (parser, friend_p);
10942 /* Restore the PEDANTIC flag. */
10943 pedantic = saved_pedantic;
10945 return fn;
10948 /* Check to see if this definition appears in a class-specifier. */
10949 member_p = (at_class_scope_p ()
10950 && TYPE_BEING_DEFINED (current_class_type));
10951 /* Defer access checks in the decl-specifier-seq until we know what
10952 function is being defined. There is no need to do this for the
10953 definition of member functions; we cannot be defining a member
10954 from another class. */
10955 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10957 /* Parse the decl-specifier-seq. */
10958 decl_specifiers
10959 = cp_parser_decl_specifier_seq (parser,
10960 CP_PARSER_FLAGS_OPTIONAL,
10961 &attributes,
10962 &declares_class_or_enum);
10963 /* Figure out whether this declaration is a `friend'. */
10964 if (friend_p)
10965 *friend_p = cp_parser_friend_p (decl_specifiers);
10967 /* Parse the declarator. */
10968 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10969 /*ctor_dtor_or_conv_p=*/NULL);
10971 /* Gather up any access checks that occurred. */
10972 stop_deferring_access_checks ();
10974 /* If something has already gone wrong, we may as well stop now. */
10975 if (declarator == error_mark_node)
10977 /* Skip to the end of the function, or if this wasn't anything
10978 like a function-definition, to a `;' in the hopes of finding
10979 a sensible place from which to continue parsing. */
10980 cp_parser_skip_to_end_of_block_or_statement (parser);
10981 pop_deferring_access_checks ();
10982 return error_mark_node;
10985 /* The next character should be a `{' (for a simple function
10986 definition), a `:' (for a ctor-initializer), or `try' (for a
10987 function-try block). */
10988 token = cp_lexer_peek_token (parser->lexer);
10989 if (!cp_parser_token_starts_function_definition_p (token))
10991 /* Issue the error-message. */
10992 cp_parser_error (parser, "expected function-definition");
10993 /* Skip to the next `;'. */
10994 cp_parser_skip_to_end_of_block_or_statement (parser);
10996 pop_deferring_access_checks ();
10997 return error_mark_node;
11000 /* If we are in a class scope, then we must handle
11001 function-definitions specially. In particular, we save away the
11002 tokens that make up the function body, and parse them again
11003 later, in order to handle code like:
11005 struct S {
11006 int f () { return i; }
11007 int i;
11010 Here, we cannot parse the body of `f' until after we have seen
11011 the declaration of `i'. */
11012 if (member_p)
11014 cp_token_cache *cache;
11016 /* Create the function-declaration. */
11017 fn = start_method (decl_specifiers, declarator, attributes);
11018 /* If something went badly wrong, bail out now. */
11019 if (fn == error_mark_node)
11021 /* If there's a function-body, skip it. */
11022 if (cp_parser_token_starts_function_definition_p
11023 (cp_lexer_peek_token (parser->lexer)))
11024 cp_parser_skip_to_end_of_block_or_statement (parser);
11025 pop_deferring_access_checks ();
11026 return error_mark_node;
11029 /* Create a token cache. */
11030 cache = cp_token_cache_new ();
11031 /* Save away the tokens that make up the body of the
11032 function. */
11033 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11034 /* Handle function try blocks. */
11035 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11036 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11038 /* Save away the inline definition; we will process it when the
11039 class is complete. */
11040 DECL_PENDING_INLINE_INFO (fn) = cache;
11041 DECL_PENDING_INLINE_P (fn) = 1;
11043 /* We need to know that this was defined in the class, so that
11044 friend templates are handled correctly. */
11045 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
11047 /* We're done with the inline definition. */
11048 finish_method (fn);
11050 /* Add FN to the queue of functions to be parsed later. */
11051 TREE_VALUE (parser->unparsed_functions_queues)
11052 = tree_cons (NULL_TREE, fn,
11053 TREE_VALUE (parser->unparsed_functions_queues));
11055 pop_deferring_access_checks ();
11056 return fn;
11059 /* Check that the number of template-parameter-lists is OK. */
11060 if (!cp_parser_check_declarator_template_parameters (parser,
11061 declarator))
11063 cp_parser_skip_to_end_of_block_or_statement (parser);
11064 pop_deferring_access_checks ();
11065 return error_mark_node;
11068 fn = cp_parser_function_definition_from_specifiers_and_declarator
11069 (parser, decl_specifiers, attributes, declarator);
11070 pop_deferring_access_checks ();
11071 return fn;
11074 /* Parse a function-body.
11076 function-body:
11077 compound_statement */
11079 static void
11080 cp_parser_function_body (cp_parser *parser)
11082 cp_parser_compound_statement (parser);
11085 /* Parse a ctor-initializer-opt followed by a function-body. Return
11086 true if a ctor-initializer was present. */
11088 static bool
11089 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11091 tree body;
11092 bool ctor_initializer_p;
11094 /* Begin the function body. */
11095 body = begin_function_body ();
11096 /* Parse the optional ctor-initializer. */
11097 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11098 /* Parse the function-body. */
11099 cp_parser_function_body (parser);
11100 /* Finish the function body. */
11101 finish_function_body (body);
11103 return ctor_initializer_p;
11106 /* Parse an initializer.
11108 initializer:
11109 = initializer-clause
11110 ( expression-list )
11112 Returns a expression representing the initializer. If no
11113 initializer is present, NULL_TREE is returned.
11115 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11116 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11117 set to FALSE if there is no initializer present. */
11119 static tree
11120 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init)
11122 cp_token *token;
11123 tree init;
11125 /* Peek at the next token. */
11126 token = cp_lexer_peek_token (parser->lexer);
11128 /* Let our caller know whether or not this initializer was
11129 parenthesized. */
11130 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11132 if (token->type == CPP_EQ)
11134 /* Consume the `='. */
11135 cp_lexer_consume_token (parser->lexer);
11136 /* Parse the initializer-clause. */
11137 init = cp_parser_initializer_clause (parser);
11139 else if (token->type == CPP_OPEN_PAREN)
11141 /* Consume the `('. */
11142 cp_lexer_consume_token (parser->lexer);
11143 /* Parse the expression-list. */
11144 init = cp_parser_expression_list (parser);
11145 /* Consume the `)' token. */
11146 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11147 cp_parser_skip_to_closing_parenthesis (parser);
11149 else
11151 /* Anything else is an error. */
11152 cp_parser_error (parser, "expected initializer");
11153 init = error_mark_node;
11156 return init;
11159 /* Parse an initializer-clause.
11161 initializer-clause:
11162 assignment-expression
11163 { initializer-list , [opt] }
11166 Returns an expression representing the initializer.
11168 If the `assignment-expression' production is used the value
11169 returned is simply a reprsentation for the expression.
11171 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11172 the elements of the initializer-list (or NULL_TREE, if the last
11173 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11174 NULL_TREE. There is no way to detect whether or not the optional
11175 trailing `,' was provided. */
11177 static tree
11178 cp_parser_initializer_clause (cp_parser* parser)
11180 tree initializer;
11182 /* If it is not a `{', then we are looking at an
11183 assignment-expression. */
11184 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11185 initializer = cp_parser_assignment_expression (parser);
11186 else
11188 /* Consume the `{' token. */
11189 cp_lexer_consume_token (parser->lexer);
11190 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11191 initializer = make_node (CONSTRUCTOR);
11192 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11193 necessary, but check_initializer depends upon it, for
11194 now. */
11195 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11196 /* If it's not a `}', then there is a non-trivial initializer. */
11197 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11199 /* Parse the initializer list. */
11200 CONSTRUCTOR_ELTS (initializer)
11201 = cp_parser_initializer_list (parser);
11202 /* A trailing `,' token is allowed. */
11203 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11204 cp_lexer_consume_token (parser->lexer);
11207 /* Now, there should be a trailing `}'. */
11208 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11211 return initializer;
11214 /* Parse an initializer-list.
11216 initializer-list:
11217 initializer-clause
11218 initializer-list , initializer-clause
11220 GNU Extension:
11222 initializer-list:
11223 identifier : initializer-clause
11224 initializer-list, identifier : initializer-clause
11226 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11227 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11228 IDENTIFIER_NODE naming the field to initialize. */
11230 static tree
11231 cp_parser_initializer_list (cp_parser* parser)
11233 tree initializers = NULL_TREE;
11235 /* Parse the rest of the list. */
11236 while (true)
11238 cp_token *token;
11239 tree identifier;
11240 tree initializer;
11242 /* If the next token is an identifier and the following one is a
11243 colon, we are looking at the GNU designated-initializer
11244 syntax. */
11245 if (cp_parser_allow_gnu_extensions_p (parser)
11246 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11247 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11249 /* Consume the identifier. */
11250 identifier = cp_lexer_consume_token (parser->lexer)->value;
11251 /* Consume the `:'. */
11252 cp_lexer_consume_token (parser->lexer);
11254 else
11255 identifier = NULL_TREE;
11257 /* Parse the initializer. */
11258 initializer = cp_parser_initializer_clause (parser);
11260 /* Add it to the list. */
11261 initializers = tree_cons (identifier, initializer, initializers);
11263 /* If the next token is not a comma, we have reached the end of
11264 the list. */
11265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11266 break;
11268 /* Peek at the next token. */
11269 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11270 /* If the next token is a `}', then we're still done. An
11271 initializer-clause can have a trailing `,' after the
11272 initializer-list and before the closing `}'. */
11273 if (token->type == CPP_CLOSE_BRACE)
11274 break;
11276 /* Consume the `,' token. */
11277 cp_lexer_consume_token (parser->lexer);
11280 /* The initializers were built up in reverse order, so we need to
11281 reverse them now. */
11282 return nreverse (initializers);
11285 /* Classes [gram.class] */
11287 /* Parse a class-name.
11289 class-name:
11290 identifier
11291 template-id
11293 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11294 to indicate that names looked up in dependent types should be
11295 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11296 keyword has been used to indicate that the name that appears next
11297 is a template. TYPE_P is true iff the next name should be treated
11298 as class-name, even if it is declared to be some other kind of name
11299 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11300 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11301 being defined in a class-head.
11303 Returns the TYPE_DECL representing the class. */
11305 static tree
11306 cp_parser_class_name (cp_parser *parser,
11307 bool typename_keyword_p,
11308 bool template_keyword_p,
11309 bool type_p,
11310 bool check_dependency_p,
11311 bool class_head_p)
11313 tree decl;
11314 tree scope;
11315 bool typename_p;
11316 cp_token *token;
11318 /* All class-names start with an identifier. */
11319 token = cp_lexer_peek_token (parser->lexer);
11320 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11322 cp_parser_error (parser, "expected class-name");
11323 return error_mark_node;
11326 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11327 to a template-id, so we save it here. */
11328 scope = parser->scope;
11329 /* Any name names a type if we're following the `typename' keyword
11330 in a qualified name where the enclosing scope is type-dependent. */
11331 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11332 && dependent_type_p (scope));
11333 /* Handle the common case (an identifier, but not a template-id)
11334 efficiently. */
11335 if (token->type == CPP_NAME
11336 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11338 tree identifier;
11340 /* Look for the identifier. */
11341 identifier = cp_parser_identifier (parser);
11342 /* If the next token isn't an identifier, we are certainly not
11343 looking at a class-name. */
11344 if (identifier == error_mark_node)
11345 decl = error_mark_node;
11346 /* If we know this is a type-name, there's no need to look it
11347 up. */
11348 else if (typename_p)
11349 decl = identifier;
11350 else
11352 /* If the next token is a `::', then the name must be a type
11353 name.
11355 [basic.lookup.qual]
11357 During the lookup for a name preceding the :: scope
11358 resolution operator, object, function, and enumerator
11359 names are ignored. */
11360 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11361 type_p = true;
11362 /* Look up the name. */
11363 decl = cp_parser_lookup_name (parser, identifier,
11364 type_p,
11365 /*is_namespace=*/false,
11366 check_dependency_p);
11369 else
11371 /* Try a template-id. */
11372 decl = cp_parser_template_id (parser, template_keyword_p,
11373 check_dependency_p);
11374 if (decl == error_mark_node)
11375 return error_mark_node;
11378 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11380 /* If this is a typename, create a TYPENAME_TYPE. */
11381 if (typename_p && decl != error_mark_node)
11382 decl = TYPE_NAME (make_typename_type (scope, decl,
11383 /*complain=*/1));
11385 /* Check to see that it is really the name of a class. */
11386 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11387 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11388 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11389 /* Situations like this:
11391 template <typename T> struct A {
11392 typename T::template X<int>::I i;
11395 are problematic. Is `T::template X<int>' a class-name? The
11396 standard does not seem to be definitive, but there is no other
11397 valid interpretation of the following `::'. Therefore, those
11398 names are considered class-names. */
11399 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11400 else if (decl == error_mark_node
11401 || TREE_CODE (decl) != TYPE_DECL
11402 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11404 cp_parser_error (parser, "expected class-name");
11405 return error_mark_node;
11408 return decl;
11411 /* Parse a class-specifier.
11413 class-specifier:
11414 class-head { member-specification [opt] }
11416 Returns the TREE_TYPE representing the class. */
11418 static tree
11419 cp_parser_class_specifier (cp_parser* parser)
11421 cp_token *token;
11422 tree type;
11423 tree attributes = NULL_TREE;
11424 int has_trailing_semicolon;
11425 bool nested_name_specifier_p;
11426 unsigned saved_num_template_parameter_lists;
11428 push_deferring_access_checks (dk_no_deferred);
11430 /* Parse the class-head. */
11431 type = cp_parser_class_head (parser,
11432 &nested_name_specifier_p);
11433 /* If the class-head was a semantic disaster, skip the entire body
11434 of the class. */
11435 if (!type)
11437 cp_parser_skip_to_end_of_block_or_statement (parser);
11438 pop_deferring_access_checks ();
11439 return error_mark_node;
11442 /* Look for the `{'. */
11443 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11445 pop_deferring_access_checks ();
11446 return error_mark_node;
11449 /* Issue an error message if type-definitions are forbidden here. */
11450 cp_parser_check_type_definition (parser);
11451 /* Remember that we are defining one more class. */
11452 ++parser->num_classes_being_defined;
11453 /* Inside the class, surrounding template-parameter-lists do not
11454 apply. */
11455 saved_num_template_parameter_lists
11456 = parser->num_template_parameter_lists;
11457 parser->num_template_parameter_lists = 0;
11459 /* Start the class. */
11460 type = begin_class_definition (type);
11461 if (type == error_mark_node)
11462 /* If the type is erroneous, skip the entire body of the class. */
11463 cp_parser_skip_to_closing_brace (parser);
11464 else
11465 /* Parse the member-specification. */
11466 cp_parser_member_specification_opt (parser);
11467 /* Look for the trailing `}'. */
11468 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11469 /* We get better error messages by noticing a common problem: a
11470 missing trailing `;'. */
11471 token = cp_lexer_peek_token (parser->lexer);
11472 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11473 /* Look for attributes to apply to this class. */
11474 if (cp_parser_allow_gnu_extensions_p (parser))
11475 attributes = cp_parser_attributes_opt (parser);
11476 /* Finish the class definition. */
11477 type = finish_class_definition (type,
11478 attributes,
11479 has_trailing_semicolon,
11480 nested_name_specifier_p);
11481 /* If this class is not itself within the scope of another class,
11482 then we need to parse the bodies of all of the queued function
11483 definitions. Note that the queued functions defined in a class
11484 are not always processed immediately following the
11485 class-specifier for that class. Consider:
11487 struct A {
11488 struct B { void f() { sizeof (A); } };
11491 If `f' were processed before the processing of `A' were
11492 completed, there would be no way to compute the size of `A'.
11493 Note that the nesting we are interested in here is lexical --
11494 not the semantic nesting given by TYPE_CONTEXT. In particular,
11495 for:
11497 struct A { struct B; };
11498 struct A::B { void f() { } };
11500 there is no need to delay the parsing of `A::B::f'. */
11501 if (--parser->num_classes_being_defined == 0)
11503 tree last_scope = NULL_TREE;
11504 tree queue_entry;
11505 tree fn;
11507 /* Reverse the queue, so that we process it in the order the
11508 functions were declared. */
11509 TREE_VALUE (parser->unparsed_functions_queues)
11510 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11511 /* In a first pass, parse default arguments to the functions.
11512 Then, in a second pass, parse the bodies of the functions.
11513 This two-phased approach handles cases like:
11515 struct S {
11516 void f() { g(); }
11517 void g(int i = 3);
11521 for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11522 queue_entry;
11523 queue_entry = TREE_CHAIN (queue_entry))
11525 fn = TREE_VALUE (queue_entry);
11526 if (DECL_FUNCTION_TEMPLATE_P (fn))
11527 fn = DECL_TEMPLATE_RESULT (fn);
11528 /* Make sure that any template parameters are in scope. */
11529 maybe_begin_member_template_processing (fn);
11530 /* If there are default arguments that have not yet been processed,
11531 take care of them now. */
11532 cp_parser_late_parsing_default_args (parser, fn);
11533 /* Remove any template parameters from the symbol table. */
11534 maybe_end_member_template_processing ();
11536 /* Now parse the body of the functions. */
11537 while (TREE_VALUE (parser->unparsed_functions_queues))
11540 /* Figure out which function we need to process. */
11541 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11542 fn = TREE_VALUE (queue_entry);
11544 /* Parse the function. */
11545 cp_parser_late_parsing_for_member (parser, fn);
11547 TREE_VALUE (parser->unparsed_functions_queues)
11548 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11551 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11552 more time than we have popped, so me must pop here. */
11553 if (last_scope)
11554 pop_scope (last_scope);
11557 /* Put back any saved access checks. */
11558 pop_deferring_access_checks ();
11560 /* Restore the count of active template-parameter-lists. */
11561 parser->num_template_parameter_lists
11562 = saved_num_template_parameter_lists;
11564 return type;
11567 /* Parse a class-head.
11569 class-head:
11570 class-key identifier [opt] base-clause [opt]
11571 class-key nested-name-specifier identifier base-clause [opt]
11572 class-key nested-name-specifier [opt] template-id
11573 base-clause [opt]
11575 GNU Extensions:
11576 class-key attributes identifier [opt] base-clause [opt]
11577 class-key attributes nested-name-specifier identifier base-clause [opt]
11578 class-key attributes nested-name-specifier [opt] template-id
11579 base-clause [opt]
11581 Returns the TYPE of the indicated class. Sets
11582 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11583 involving a nested-name-specifier was used, and FALSE otherwise.
11585 Returns NULL_TREE if the class-head is syntactically valid, but
11586 semantically invalid in a way that means we should skip the entire
11587 body of the class. */
11589 static tree
11590 cp_parser_class_head (cp_parser* parser,
11591 bool* nested_name_specifier_p)
11593 cp_token *token;
11594 tree nested_name_specifier;
11595 enum tag_types class_key;
11596 tree id = NULL_TREE;
11597 tree type = NULL_TREE;
11598 tree attributes;
11599 bool template_id_p = false;
11600 bool qualified_p = false;
11601 bool invalid_nested_name_p = false;
11602 unsigned num_templates;
11604 /* Assume no nested-name-specifier will be present. */
11605 *nested_name_specifier_p = false;
11606 /* Assume no template parameter lists will be used in defining the
11607 type. */
11608 num_templates = 0;
11610 /* Look for the class-key. */
11611 class_key = cp_parser_class_key (parser);
11612 if (class_key == none_type)
11613 return error_mark_node;
11615 /* Parse the attributes. */
11616 attributes = cp_parser_attributes_opt (parser);
11618 /* If the next token is `::', that is invalid -- but sometimes
11619 people do try to write:
11621 struct ::S {};
11623 Handle this gracefully by accepting the extra qualifier, and then
11624 issuing an error about it later if this really is a
11625 class-head. If it turns out just to be an elaborated type
11626 specifier, remain silent. */
11627 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11628 qualified_p = true;
11630 push_deferring_access_checks (dk_no_check);
11632 /* Determine the name of the class. Begin by looking for an
11633 optional nested-name-specifier. */
11634 nested_name_specifier
11635 = cp_parser_nested_name_specifier_opt (parser,
11636 /*typename_keyword_p=*/false,
11637 /*check_dependency_p=*/false,
11638 /*type_p=*/false);
11639 /* If there was a nested-name-specifier, then there *must* be an
11640 identifier. */
11641 if (nested_name_specifier)
11643 /* Although the grammar says `identifier', it really means
11644 `class-name' or `template-name'. You are only allowed to
11645 define a class that has already been declared with this
11646 syntax.
11648 The proposed resolution for Core Issue 180 says that whever
11649 you see `class T::X' you should treat `X' as a type-name.
11651 It is OK to define an inaccessible class; for example:
11653 class A { class B; };
11654 class A::B {};
11656 We do not know if we will see a class-name, or a
11657 template-name. We look for a class-name first, in case the
11658 class-name is a template-id; if we looked for the
11659 template-name first we would stop after the template-name. */
11660 cp_parser_parse_tentatively (parser);
11661 type = cp_parser_class_name (parser,
11662 /*typename_keyword_p=*/false,
11663 /*template_keyword_p=*/false,
11664 /*type_p=*/true,
11665 /*check_dependency_p=*/false,
11666 /*class_head_p=*/true);
11667 /* If that didn't work, ignore the nested-name-specifier. */
11668 if (!cp_parser_parse_definitely (parser))
11670 invalid_nested_name_p = true;
11671 id = cp_parser_identifier (parser);
11672 if (id == error_mark_node)
11673 id = NULL_TREE;
11675 /* If we could not find a corresponding TYPE, treat this
11676 declaration like an unqualified declaration. */
11677 if (type == error_mark_node)
11678 nested_name_specifier = NULL_TREE;
11679 /* Otherwise, count the number of templates used in TYPE and its
11680 containing scopes. */
11681 else
11683 tree scope;
11685 for (scope = TREE_TYPE (type);
11686 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11687 scope = (TYPE_P (scope)
11688 ? TYPE_CONTEXT (scope)
11689 : DECL_CONTEXT (scope)))
11690 if (TYPE_P (scope)
11691 && CLASS_TYPE_P (scope)
11692 && CLASSTYPE_TEMPLATE_INFO (scope)
11693 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11694 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11695 ++num_templates;
11698 /* Otherwise, the identifier is optional. */
11699 else
11701 /* We don't know whether what comes next is a template-id,
11702 an identifier, or nothing at all. */
11703 cp_parser_parse_tentatively (parser);
11704 /* Check for a template-id. */
11705 id = cp_parser_template_id (parser,
11706 /*template_keyword_p=*/false,
11707 /*check_dependency_p=*/true);
11708 /* If that didn't work, it could still be an identifier. */
11709 if (!cp_parser_parse_definitely (parser))
11711 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11712 id = cp_parser_identifier (parser);
11713 else
11714 id = NULL_TREE;
11716 else
11718 template_id_p = true;
11719 ++num_templates;
11723 pop_deferring_access_checks ();
11725 /* If it's not a `:' or a `{' then we can't really be looking at a
11726 class-head, since a class-head only appears as part of a
11727 class-specifier. We have to detect this situation before calling
11728 xref_tag, since that has irreversible side-effects. */
11729 if (!cp_parser_next_token_starts_class_definition_p (parser))
11731 cp_parser_error (parser, "expected `{' or `:'");
11732 return error_mark_node;
11735 /* At this point, we're going ahead with the class-specifier, even
11736 if some other problem occurs. */
11737 cp_parser_commit_to_tentative_parse (parser);
11738 /* Issue the error about the overly-qualified name now. */
11739 if (qualified_p)
11740 cp_parser_error (parser,
11741 "global qualification of class name is invalid");
11742 else if (invalid_nested_name_p)
11743 cp_parser_error (parser,
11744 "qualified name does not name a class");
11745 /* Make sure that the right number of template parameters were
11746 present. */
11747 if (!cp_parser_check_template_parameters (parser, num_templates))
11748 /* If something went wrong, there is no point in even trying to
11749 process the class-definition. */
11750 return NULL_TREE;
11752 /* Look up the type. */
11753 if (template_id_p)
11755 type = TREE_TYPE (id);
11756 maybe_process_partial_specialization (type);
11758 else if (!nested_name_specifier)
11760 /* If the class was unnamed, create a dummy name. */
11761 if (!id)
11762 id = make_anon_name ();
11763 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11765 else
11767 tree class_type;
11768 tree scope;
11770 /* Given:
11772 template <typename T> struct S { struct T };
11773 template <typename T> struct S<T>::T { };
11775 we will get a TYPENAME_TYPE when processing the definition of
11776 `S::T'. We need to resolve it to the actual type before we
11777 try to define it. */
11778 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11780 class_type = resolve_typename_type (TREE_TYPE (type),
11781 /*only_current_p=*/false);
11782 if (class_type != error_mark_node)
11783 type = TYPE_NAME (class_type);
11784 else
11786 cp_parser_error (parser, "could not resolve typename type");
11787 type = error_mark_node;
11791 /* Figure out in what scope the declaration is being placed. */
11792 scope = current_scope ();
11793 if (!scope)
11794 scope = current_namespace;
11795 /* If that scope does not contain the scope in which the
11796 class was originally declared, the program is invalid. */
11797 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11799 error ("declaration of `%D' in `%D' which does not "
11800 "enclose `%D'", type, scope, nested_name_specifier);
11801 return NULL_TREE;
11804 maybe_process_partial_specialization (TREE_TYPE (type));
11805 class_type = current_class_type;
11806 type = TREE_TYPE (handle_class_head (class_key,
11807 nested_name_specifier,
11808 type,
11809 attributes));
11810 if (type != error_mark_node)
11812 if (!class_type && TYPE_CONTEXT (type))
11813 *nested_name_specifier_p = true;
11814 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11815 class_type))
11816 *nested_name_specifier_p = true;
11819 /* Indicate whether this class was declared as a `class' or as a
11820 `struct'. */
11821 if (TREE_CODE (type) == RECORD_TYPE)
11822 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11823 cp_parser_check_class_key (class_key, type);
11825 /* Enter the scope containing the class; the names of base classes
11826 should be looked up in that context. For example, given:
11828 struct A { struct B {}; struct C; };
11829 struct A::C : B {};
11831 is valid. */
11832 if (nested_name_specifier)
11833 push_scope (nested_name_specifier);
11834 /* Now, look for the base-clause. */
11835 token = cp_lexer_peek_token (parser->lexer);
11836 if (token->type == CPP_COLON)
11838 tree bases;
11840 /* Get the list of base-classes. */
11841 bases = cp_parser_base_clause (parser);
11842 /* Process them. */
11843 xref_basetypes (type, bases);
11845 /* Leave the scope given by the nested-name-specifier. We will
11846 enter the class scope itself while processing the members. */
11847 if (nested_name_specifier)
11848 pop_scope (nested_name_specifier);
11850 return type;
11853 /* Parse a class-key.
11855 class-key:
11856 class
11857 struct
11858 union
11860 Returns the kind of class-key specified, or none_type to indicate
11861 error. */
11863 static enum tag_types
11864 cp_parser_class_key (cp_parser* parser)
11866 cp_token *token;
11867 enum tag_types tag_type;
11869 /* Look for the class-key. */
11870 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11871 if (!token)
11872 return none_type;
11874 /* Check to see if the TOKEN is a class-key. */
11875 tag_type = cp_parser_token_is_class_key (token);
11876 if (!tag_type)
11877 cp_parser_error (parser, "expected class-key");
11878 return tag_type;
11881 /* Parse an (optional) member-specification.
11883 member-specification:
11884 member-declaration member-specification [opt]
11885 access-specifier : member-specification [opt] */
11887 static void
11888 cp_parser_member_specification_opt (cp_parser* parser)
11890 while (true)
11892 cp_token *token;
11893 enum rid keyword;
11895 /* Peek at the next token. */
11896 token = cp_lexer_peek_token (parser->lexer);
11897 /* If it's a `}', or EOF then we've seen all the members. */
11898 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11899 break;
11901 /* See if this token is a keyword. */
11902 keyword = token->keyword;
11903 switch (keyword)
11905 case RID_PUBLIC:
11906 case RID_PROTECTED:
11907 case RID_PRIVATE:
11908 /* Consume the access-specifier. */
11909 cp_lexer_consume_token (parser->lexer);
11910 /* Remember which access-specifier is active. */
11911 current_access_specifier = token->value;
11912 /* Look for the `:'. */
11913 cp_parser_require (parser, CPP_COLON, "`:'");
11914 break;
11916 default:
11917 /* Otherwise, the next construction must be a
11918 member-declaration. */
11919 cp_parser_member_declaration (parser);
11924 /* Parse a member-declaration.
11926 member-declaration:
11927 decl-specifier-seq [opt] member-declarator-list [opt] ;
11928 function-definition ; [opt]
11929 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11930 using-declaration
11931 template-declaration
11933 member-declarator-list:
11934 member-declarator
11935 member-declarator-list , member-declarator
11937 member-declarator:
11938 declarator pure-specifier [opt]
11939 declarator constant-initializer [opt]
11940 identifier [opt] : constant-expression
11942 GNU Extensions:
11944 member-declaration:
11945 __extension__ member-declaration
11947 member-declarator:
11948 declarator attributes [opt] pure-specifier [opt]
11949 declarator attributes [opt] constant-initializer [opt]
11950 identifier [opt] attributes [opt] : constant-expression */
11952 static void
11953 cp_parser_member_declaration (cp_parser* parser)
11955 tree decl_specifiers;
11956 tree prefix_attributes;
11957 tree decl;
11958 bool declares_class_or_enum;
11959 bool friend_p;
11960 cp_token *token;
11961 int saved_pedantic;
11963 /* Check for the `__extension__' keyword. */
11964 if (cp_parser_extension_opt (parser, &saved_pedantic))
11966 /* Recurse. */
11967 cp_parser_member_declaration (parser);
11968 /* Restore the old value of the PEDANTIC flag. */
11969 pedantic = saved_pedantic;
11971 return;
11974 /* Check for a template-declaration. */
11975 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11977 /* Parse the template-declaration. */
11978 cp_parser_template_declaration (parser, /*member_p=*/true);
11980 return;
11983 /* Check for a using-declaration. */
11984 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11986 /* Parse the using-declaration. */
11987 cp_parser_using_declaration (parser);
11989 return;
11992 /* We can't tell whether we're looking at a declaration or a
11993 function-definition. */
11994 cp_parser_parse_tentatively (parser);
11996 /* Parse the decl-specifier-seq. */
11997 decl_specifiers
11998 = cp_parser_decl_specifier_seq (parser,
11999 CP_PARSER_FLAGS_OPTIONAL,
12000 &prefix_attributes,
12001 &declares_class_or_enum);
12002 /* Check for an invalid type-name. */
12003 if (cp_parser_diagnose_invalid_type_name (parser))
12004 return;
12005 /* If there is no declarator, then the decl-specifier-seq should
12006 specify a type. */
12007 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12009 /* If there was no decl-specifier-seq, and the next token is a
12010 `;', then we have something like:
12012 struct S { ; };
12014 [class.mem]
12016 Each member-declaration shall declare at least one member
12017 name of the class. */
12018 if (!decl_specifiers)
12020 if (pedantic)
12021 pedwarn ("extra semicolon");
12023 else
12025 tree type;
12027 /* See if this declaration is a friend. */
12028 friend_p = cp_parser_friend_p (decl_specifiers);
12029 /* If there were decl-specifiers, check to see if there was
12030 a class-declaration. */
12031 type = check_tag_decl (decl_specifiers);
12032 /* Nested classes have already been added to the class, but
12033 a `friend' needs to be explicitly registered. */
12034 if (friend_p)
12036 /* If the `friend' keyword was present, the friend must
12037 be introduced with a class-key. */
12038 if (!declares_class_or_enum)
12039 error ("a class-key must be used when declaring a friend");
12040 /* In this case:
12042 template <typename T> struct A {
12043 friend struct A<T>::B;
12046 A<T>::B will be represented by a TYPENAME_TYPE, and
12047 therefore not recognized by check_tag_decl. */
12048 if (!type)
12050 tree specifier;
12052 for (specifier = decl_specifiers;
12053 specifier;
12054 specifier = TREE_CHAIN (specifier))
12056 tree s = TREE_VALUE (specifier);
12058 if (TREE_CODE (s) == IDENTIFIER_NODE
12059 && IDENTIFIER_GLOBAL_VALUE (s))
12060 type = IDENTIFIER_GLOBAL_VALUE (s);
12061 if (TREE_CODE (s) == TYPE_DECL)
12062 s = TREE_TYPE (s);
12063 if (TYPE_P (s))
12065 type = s;
12066 break;
12070 if (!type)
12071 error ("friend declaration does not name a class or "
12072 "function");
12073 else
12074 make_friend_class (current_class_type, type);
12076 /* If there is no TYPE, an error message will already have
12077 been issued. */
12078 else if (!type)
12080 /* An anonymous aggregate has to be handled specially; such
12081 a declaration really declares a data member (with a
12082 particular type), as opposed to a nested class. */
12083 else if (ANON_AGGR_TYPE_P (type))
12085 /* Remove constructors and such from TYPE, now that we
12086 know it is an anoymous aggregate. */
12087 fixup_anonymous_aggr (type);
12088 /* And make the corresponding data member. */
12089 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12090 /* Add it to the class. */
12091 finish_member_declaration (decl);
12095 else
12097 /* See if these declarations will be friends. */
12098 friend_p = cp_parser_friend_p (decl_specifiers);
12100 /* Keep going until we hit the `;' at the end of the
12101 declaration. */
12102 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12104 tree attributes = NULL_TREE;
12105 tree first_attribute;
12107 /* Peek at the next token. */
12108 token = cp_lexer_peek_token (parser->lexer);
12110 /* Check for a bitfield declaration. */
12111 if (token->type == CPP_COLON
12112 || (token->type == CPP_NAME
12113 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12114 == CPP_COLON))
12116 tree identifier;
12117 tree width;
12119 /* Get the name of the bitfield. Note that we cannot just
12120 check TOKEN here because it may have been invalidated by
12121 the call to cp_lexer_peek_nth_token above. */
12122 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12123 identifier = cp_parser_identifier (parser);
12124 else
12125 identifier = NULL_TREE;
12127 /* Consume the `:' token. */
12128 cp_lexer_consume_token (parser->lexer);
12129 /* Get the width of the bitfield. */
12130 width
12131 = cp_parser_constant_expression (parser,
12132 /*allow_non_constant=*/false,
12133 NULL);
12135 /* Look for attributes that apply to the bitfield. */
12136 attributes = cp_parser_attributes_opt (parser);
12137 /* Remember which attributes are prefix attributes and
12138 which are not. */
12139 first_attribute = attributes;
12140 /* Combine the attributes. */
12141 attributes = chainon (prefix_attributes, attributes);
12143 /* Create the bitfield declaration. */
12144 decl = grokbitfield (identifier,
12145 decl_specifiers,
12146 width);
12147 /* Apply the attributes. */
12148 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12150 else
12152 tree declarator;
12153 tree initializer;
12154 tree asm_specification;
12155 bool ctor_dtor_or_conv_p;
12157 /* Parse the declarator. */
12158 declarator
12159 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12160 &ctor_dtor_or_conv_p);
12162 /* If something went wrong parsing the declarator, make sure
12163 that we at least consume some tokens. */
12164 if (declarator == error_mark_node)
12166 /* Skip to the end of the statement. */
12167 cp_parser_skip_to_end_of_statement (parser);
12168 break;
12171 /* Look for an asm-specification. */
12172 asm_specification = cp_parser_asm_specification_opt (parser);
12173 /* Look for attributes that apply to the declaration. */
12174 attributes = cp_parser_attributes_opt (parser);
12175 /* Remember which attributes are prefix attributes and
12176 which are not. */
12177 first_attribute = attributes;
12178 /* Combine the attributes. */
12179 attributes = chainon (prefix_attributes, attributes);
12181 /* If it's an `=', then we have a constant-initializer or a
12182 pure-specifier. It is not correct to parse the
12183 initializer before registering the member declaration
12184 since the member declaration should be in scope while
12185 its initializer is processed. However, the rest of the
12186 front end does not yet provide an interface that allows
12187 us to handle this correctly. */
12188 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12190 /* In [class.mem]:
12192 A pure-specifier shall be used only in the declaration of
12193 a virtual function.
12195 A member-declarator can contain a constant-initializer
12196 only if it declares a static member of integral or
12197 enumeration type.
12199 Therefore, if the DECLARATOR is for a function, we look
12200 for a pure-specifier; otherwise, we look for a
12201 constant-initializer. When we call `grokfield', it will
12202 perform more stringent semantics checks. */
12203 if (TREE_CODE (declarator) == CALL_EXPR)
12204 initializer = cp_parser_pure_specifier (parser);
12205 else
12207 /* This declaration cannot be a function
12208 definition. */
12209 cp_parser_commit_to_tentative_parse (parser);
12210 /* Parse the initializer. */
12211 initializer = cp_parser_constant_initializer (parser);
12214 /* Otherwise, there is no initializer. */
12215 else
12216 initializer = NULL_TREE;
12218 /* See if we are probably looking at a function
12219 definition. We are certainly not looking at at a
12220 member-declarator. Calling `grokfield' has
12221 side-effects, so we must not do it unless we are sure
12222 that we are looking at a member-declarator. */
12223 if (cp_parser_token_starts_function_definition_p
12224 (cp_lexer_peek_token (parser->lexer)))
12225 decl = error_mark_node;
12226 else
12227 /* Create the declaration. */
12228 decl = grokfield (declarator,
12229 decl_specifiers,
12230 initializer,
12231 asm_specification,
12232 attributes);
12235 /* Reset PREFIX_ATTRIBUTES. */
12236 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12237 attributes = TREE_CHAIN (attributes);
12238 if (attributes)
12239 TREE_CHAIN (attributes) = NULL_TREE;
12241 /* If there is any qualification still in effect, clear it
12242 now; we will be starting fresh with the next declarator. */
12243 parser->scope = NULL_TREE;
12244 parser->qualifying_scope = NULL_TREE;
12245 parser->object_scope = NULL_TREE;
12246 /* If it's a `,', then there are more declarators. */
12247 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12248 cp_lexer_consume_token (parser->lexer);
12249 /* If the next token isn't a `;', then we have a parse error. */
12250 else if (cp_lexer_next_token_is_not (parser->lexer,
12251 CPP_SEMICOLON))
12253 cp_parser_error (parser, "expected `;'");
12254 /* Skip tokens until we find a `;' */
12255 cp_parser_skip_to_end_of_statement (parser);
12257 break;
12260 if (decl)
12262 /* Add DECL to the list of members. */
12263 if (!friend_p)
12264 finish_member_declaration (decl);
12266 /* If DECL is a function, we must return
12267 to parse it later. (Even though there is no definition,
12268 there might be default arguments that need handling.) */
12269 if (TREE_CODE (decl) == FUNCTION_DECL)
12270 TREE_VALUE (parser->unparsed_functions_queues)
12271 = tree_cons (NULL_TREE, decl,
12272 TREE_VALUE (parser->unparsed_functions_queues));
12277 /* If everything went well, look for the `;'. */
12278 if (cp_parser_parse_definitely (parser))
12280 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12281 return;
12284 /* Parse the function-definition. */
12285 decl = cp_parser_function_definition (parser, &friend_p);
12286 /* If the member was not a friend, declare it here. */
12287 if (!friend_p)
12288 finish_member_declaration (decl);
12289 /* Peek at the next token. */
12290 token = cp_lexer_peek_token (parser->lexer);
12291 /* If the next token is a semicolon, consume it. */
12292 if (token->type == CPP_SEMICOLON)
12293 cp_lexer_consume_token (parser->lexer);
12296 /* Parse a pure-specifier.
12298 pure-specifier:
12301 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12302 Otherwiser, ERROR_MARK_NODE is returned. */
12304 static tree
12305 cp_parser_pure_specifier (cp_parser* parser)
12307 cp_token *token;
12309 /* Look for the `=' token. */
12310 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12311 return error_mark_node;
12312 /* Look for the `0' token. */
12313 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12314 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12315 to get information from the lexer about how the number was
12316 spelled in order to fix this problem. */
12317 if (!token || !integer_zerop (token->value))
12318 return error_mark_node;
12320 return integer_zero_node;
12323 /* Parse a constant-initializer.
12325 constant-initializer:
12326 = constant-expression
12328 Returns a representation of the constant-expression. */
12330 static tree
12331 cp_parser_constant_initializer (cp_parser* parser)
12333 /* Look for the `=' token. */
12334 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12335 return error_mark_node;
12337 /* It is invalid to write:
12339 struct S { static const int i = { 7 }; };
12342 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12344 cp_parser_error (parser,
12345 "a brace-enclosed initializer is not allowed here");
12346 /* Consume the opening brace. */
12347 cp_lexer_consume_token (parser->lexer);
12348 /* Skip the initializer. */
12349 cp_parser_skip_to_closing_brace (parser);
12350 /* Look for the trailing `}'. */
12351 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12353 return error_mark_node;
12356 return cp_parser_constant_expression (parser,
12357 /*allow_non_constant=*/false,
12358 NULL);
12361 /* Derived classes [gram.class.derived] */
12363 /* Parse a base-clause.
12365 base-clause:
12366 : base-specifier-list
12368 base-specifier-list:
12369 base-specifier
12370 base-specifier-list , base-specifier
12372 Returns a TREE_LIST representing the base-classes, in the order in
12373 which they were declared. The representation of each node is as
12374 described by cp_parser_base_specifier.
12376 In the case that no bases are specified, this function will return
12377 NULL_TREE, not ERROR_MARK_NODE. */
12379 static tree
12380 cp_parser_base_clause (cp_parser* parser)
12382 tree bases = NULL_TREE;
12384 /* Look for the `:' that begins the list. */
12385 cp_parser_require (parser, CPP_COLON, "`:'");
12387 /* Scan the base-specifier-list. */
12388 while (true)
12390 cp_token *token;
12391 tree base;
12393 /* Look for the base-specifier. */
12394 base = cp_parser_base_specifier (parser);
12395 /* Add BASE to the front of the list. */
12396 if (base != error_mark_node)
12398 TREE_CHAIN (base) = bases;
12399 bases = base;
12401 /* Peek at the next token. */
12402 token = cp_lexer_peek_token (parser->lexer);
12403 /* If it's not a comma, then the list is complete. */
12404 if (token->type != CPP_COMMA)
12405 break;
12406 /* Consume the `,'. */
12407 cp_lexer_consume_token (parser->lexer);
12410 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12411 base class had a qualified name. However, the next name that
12412 appears is certainly not qualified. */
12413 parser->scope = NULL_TREE;
12414 parser->qualifying_scope = NULL_TREE;
12415 parser->object_scope = NULL_TREE;
12417 return nreverse (bases);
12420 /* Parse a base-specifier.
12422 base-specifier:
12423 :: [opt] nested-name-specifier [opt] class-name
12424 virtual access-specifier [opt] :: [opt] nested-name-specifier
12425 [opt] class-name
12426 access-specifier virtual [opt] :: [opt] nested-name-specifier
12427 [opt] class-name
12429 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12430 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12431 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12432 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12434 static tree
12435 cp_parser_base_specifier (cp_parser* parser)
12437 cp_token *token;
12438 bool done = false;
12439 bool virtual_p = false;
12440 bool duplicate_virtual_error_issued_p = false;
12441 bool duplicate_access_error_issued_p = false;
12442 bool class_scope_p, template_p;
12443 tree access = access_default_node;
12444 tree type;
12446 /* Process the optional `virtual' and `access-specifier'. */
12447 while (!done)
12449 /* Peek at the next token. */
12450 token = cp_lexer_peek_token (parser->lexer);
12451 /* Process `virtual'. */
12452 switch (token->keyword)
12454 case RID_VIRTUAL:
12455 /* If `virtual' appears more than once, issue an error. */
12456 if (virtual_p && !duplicate_virtual_error_issued_p)
12458 cp_parser_error (parser,
12459 "`virtual' specified more than once in base-specified");
12460 duplicate_virtual_error_issued_p = true;
12463 virtual_p = true;
12465 /* Consume the `virtual' token. */
12466 cp_lexer_consume_token (parser->lexer);
12468 break;
12470 case RID_PUBLIC:
12471 case RID_PROTECTED:
12472 case RID_PRIVATE:
12473 /* If more than one access specifier appears, issue an
12474 error. */
12475 if (access != access_default_node
12476 && !duplicate_access_error_issued_p)
12478 cp_parser_error (parser,
12479 "more than one access specifier in base-specified");
12480 duplicate_access_error_issued_p = true;
12483 access = ridpointers[(int) token->keyword];
12485 /* Consume the access-specifier. */
12486 cp_lexer_consume_token (parser->lexer);
12488 break;
12490 default:
12491 done = true;
12492 break;
12496 /* Look for the optional `::' operator. */
12497 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12498 /* Look for the nested-name-specifier. The simplest way to
12499 implement:
12501 [temp.res]
12503 The keyword `typename' is not permitted in a base-specifier or
12504 mem-initializer; in these contexts a qualified name that
12505 depends on a template-parameter is implicitly assumed to be a
12506 type name.
12508 is to pretend that we have seen the `typename' keyword at this
12509 point. */
12510 cp_parser_nested_name_specifier_opt (parser,
12511 /*typename_keyword_p=*/true,
12512 /*check_dependency_p=*/true,
12513 /*type_p=*/true);
12514 /* If the base class is given by a qualified name, assume that names
12515 we see are type names or templates, as appropriate. */
12516 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12517 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12519 /* Finally, look for the class-name. */
12520 type = cp_parser_class_name (parser,
12521 class_scope_p,
12522 template_p,
12523 /*type_p=*/true,
12524 /*check_dependency_p=*/true,
12525 /*class_head_p=*/false);
12527 if (type == error_mark_node)
12528 return error_mark_node;
12530 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12533 /* Exception handling [gram.exception] */
12535 /* Parse an (optional) exception-specification.
12537 exception-specification:
12538 throw ( type-id-list [opt] )
12540 Returns a TREE_LIST representing the exception-specification. The
12541 TREE_VALUE of each node is a type. */
12543 static tree
12544 cp_parser_exception_specification_opt (cp_parser* parser)
12546 cp_token *token;
12547 tree type_id_list;
12549 /* Peek at the next token. */
12550 token = cp_lexer_peek_token (parser->lexer);
12551 /* If it's not `throw', then there's no exception-specification. */
12552 if (!cp_parser_is_keyword (token, RID_THROW))
12553 return NULL_TREE;
12555 /* Consume the `throw'. */
12556 cp_lexer_consume_token (parser->lexer);
12558 /* Look for the `('. */
12559 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12561 /* Peek at the next token. */
12562 token = cp_lexer_peek_token (parser->lexer);
12563 /* If it's not a `)', then there is a type-id-list. */
12564 if (token->type != CPP_CLOSE_PAREN)
12566 const char *saved_message;
12568 /* Types may not be defined in an exception-specification. */
12569 saved_message = parser->type_definition_forbidden_message;
12570 parser->type_definition_forbidden_message
12571 = "types may not be defined in an exception-specification";
12572 /* Parse the type-id-list. */
12573 type_id_list = cp_parser_type_id_list (parser);
12574 /* Restore the saved message. */
12575 parser->type_definition_forbidden_message = saved_message;
12577 else
12578 type_id_list = empty_except_spec;
12580 /* Look for the `)'. */
12581 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12583 return type_id_list;
12586 /* Parse an (optional) type-id-list.
12588 type-id-list:
12589 type-id
12590 type-id-list , type-id
12592 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12593 in the order that the types were presented. */
12595 static tree
12596 cp_parser_type_id_list (cp_parser* parser)
12598 tree types = NULL_TREE;
12600 while (true)
12602 cp_token *token;
12603 tree type;
12605 /* Get the next type-id. */
12606 type = cp_parser_type_id (parser);
12607 /* Add it to the list. */
12608 types = add_exception_specifier (types, type, /*complain=*/1);
12609 /* Peek at the next token. */
12610 token = cp_lexer_peek_token (parser->lexer);
12611 /* If it is not a `,', we are done. */
12612 if (token->type != CPP_COMMA)
12613 break;
12614 /* Consume the `,'. */
12615 cp_lexer_consume_token (parser->lexer);
12618 return nreverse (types);
12621 /* Parse a try-block.
12623 try-block:
12624 try compound-statement handler-seq */
12626 static tree
12627 cp_parser_try_block (cp_parser* parser)
12629 tree try_block;
12631 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12632 try_block = begin_try_block ();
12633 cp_parser_compound_statement (parser);
12634 finish_try_block (try_block);
12635 cp_parser_handler_seq (parser);
12636 finish_handler_sequence (try_block);
12638 return try_block;
12641 /* Parse a function-try-block.
12643 function-try-block:
12644 try ctor-initializer [opt] function-body handler-seq */
12646 static bool
12647 cp_parser_function_try_block (cp_parser* parser)
12649 tree try_block;
12650 bool ctor_initializer_p;
12652 /* Look for the `try' keyword. */
12653 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12654 return false;
12655 /* Let the rest of the front-end know where we are. */
12656 try_block = begin_function_try_block ();
12657 /* Parse the function-body. */
12658 ctor_initializer_p
12659 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12660 /* We're done with the `try' part. */
12661 finish_function_try_block (try_block);
12662 /* Parse the handlers. */
12663 cp_parser_handler_seq (parser);
12664 /* We're done with the handlers. */
12665 finish_function_handler_sequence (try_block);
12667 return ctor_initializer_p;
12670 /* Parse a handler-seq.
12672 handler-seq:
12673 handler handler-seq [opt] */
12675 static void
12676 cp_parser_handler_seq (cp_parser* parser)
12678 while (true)
12680 cp_token *token;
12682 /* Parse the handler. */
12683 cp_parser_handler (parser);
12684 /* Peek at the next token. */
12685 token = cp_lexer_peek_token (parser->lexer);
12686 /* If it's not `catch' then there are no more handlers. */
12687 if (!cp_parser_is_keyword (token, RID_CATCH))
12688 break;
12692 /* Parse a handler.
12694 handler:
12695 catch ( exception-declaration ) compound-statement */
12697 static void
12698 cp_parser_handler (cp_parser* parser)
12700 tree handler;
12701 tree declaration;
12703 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12704 handler = begin_handler ();
12705 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12706 declaration = cp_parser_exception_declaration (parser);
12707 finish_handler_parms (declaration, handler);
12708 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12709 cp_parser_compound_statement (parser);
12710 finish_handler (handler);
12713 /* Parse an exception-declaration.
12715 exception-declaration:
12716 type-specifier-seq declarator
12717 type-specifier-seq abstract-declarator
12718 type-specifier-seq
12719 ...
12721 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12722 ellipsis variant is used. */
12724 static tree
12725 cp_parser_exception_declaration (cp_parser* parser)
12727 tree type_specifiers;
12728 tree declarator;
12729 const char *saved_message;
12731 /* If it's an ellipsis, it's easy to handle. */
12732 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12734 /* Consume the `...' token. */
12735 cp_lexer_consume_token (parser->lexer);
12736 return NULL_TREE;
12739 /* Types may not be defined in exception-declarations. */
12740 saved_message = parser->type_definition_forbidden_message;
12741 parser->type_definition_forbidden_message
12742 = "types may not be defined in exception-declarations";
12744 /* Parse the type-specifier-seq. */
12745 type_specifiers = cp_parser_type_specifier_seq (parser);
12746 /* If it's a `)', then there is no declarator. */
12747 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12748 declarator = NULL_TREE;
12749 else
12750 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12751 /*ctor_dtor_or_conv_p=*/NULL);
12753 /* Restore the saved message. */
12754 parser->type_definition_forbidden_message = saved_message;
12756 return start_handler_parms (type_specifiers, declarator);
12759 /* Parse a throw-expression.
12761 throw-expression:
12762 throw assignment-expresion [opt]
12764 Returns a THROW_EXPR representing the throw-expression. */
12766 static tree
12767 cp_parser_throw_expression (cp_parser* parser)
12769 tree expression;
12771 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12772 /* We can't be sure if there is an assignment-expression or not. */
12773 cp_parser_parse_tentatively (parser);
12774 /* Try it. */
12775 expression = cp_parser_assignment_expression (parser);
12776 /* If it didn't work, this is just a rethrow. */
12777 if (!cp_parser_parse_definitely (parser))
12778 expression = NULL_TREE;
12780 return build_throw (expression);
12783 /* GNU Extensions */
12785 /* Parse an (optional) asm-specification.
12787 asm-specification:
12788 asm ( string-literal )
12790 If the asm-specification is present, returns a STRING_CST
12791 corresponding to the string-literal. Otherwise, returns
12792 NULL_TREE. */
12794 static tree
12795 cp_parser_asm_specification_opt (cp_parser* parser)
12797 cp_token *token;
12798 tree asm_specification;
12800 /* Peek at the next token. */
12801 token = cp_lexer_peek_token (parser->lexer);
12802 /* If the next token isn't the `asm' keyword, then there's no
12803 asm-specification. */
12804 if (!cp_parser_is_keyword (token, RID_ASM))
12805 return NULL_TREE;
12807 /* Consume the `asm' token. */
12808 cp_lexer_consume_token (parser->lexer);
12809 /* Look for the `('. */
12810 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12812 /* Look for the string-literal. */
12813 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12814 if (token)
12815 asm_specification = token->value;
12816 else
12817 asm_specification = NULL_TREE;
12819 /* Look for the `)'. */
12820 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12822 return asm_specification;
12825 /* Parse an asm-operand-list.
12827 asm-operand-list:
12828 asm-operand
12829 asm-operand-list , asm-operand
12831 asm-operand:
12832 string-literal ( expression )
12833 [ string-literal ] string-literal ( expression )
12835 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12836 each node is the expression. The TREE_PURPOSE is itself a
12837 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12838 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12839 is a STRING_CST for the string literal before the parenthesis. */
12841 static tree
12842 cp_parser_asm_operand_list (cp_parser* parser)
12844 tree asm_operands = NULL_TREE;
12846 while (true)
12848 tree string_literal;
12849 tree expression;
12850 tree name;
12851 cp_token *token;
12853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12855 /* Consume the `[' token. */
12856 cp_lexer_consume_token (parser->lexer);
12857 /* Read the operand name. */
12858 name = cp_parser_identifier (parser);
12859 if (name != error_mark_node)
12860 name = build_string (IDENTIFIER_LENGTH (name),
12861 IDENTIFIER_POINTER (name));
12862 /* Look for the closing `]'. */
12863 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12865 else
12866 name = NULL_TREE;
12867 /* Look for the string-literal. */
12868 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12869 string_literal = token ? token->value : error_mark_node;
12870 /* Look for the `('. */
12871 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12872 /* Parse the expression. */
12873 expression = cp_parser_expression (parser);
12874 /* Look for the `)'. */
12875 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12876 /* Add this operand to the list. */
12877 asm_operands = tree_cons (build_tree_list (name, string_literal),
12878 expression,
12879 asm_operands);
12880 /* If the next token is not a `,', there are no more
12881 operands. */
12882 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12883 break;
12884 /* Consume the `,'. */
12885 cp_lexer_consume_token (parser->lexer);
12888 return nreverse (asm_operands);
12891 /* Parse an asm-clobber-list.
12893 asm-clobber-list:
12894 string-literal
12895 asm-clobber-list , string-literal
12897 Returns a TREE_LIST, indicating the clobbers in the order that they
12898 appeared. The TREE_VALUE of each node is a STRING_CST. */
12900 static tree
12901 cp_parser_asm_clobber_list (cp_parser* parser)
12903 tree clobbers = NULL_TREE;
12905 while (true)
12907 cp_token *token;
12908 tree string_literal;
12910 /* Look for the string literal. */
12911 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12912 string_literal = token ? token->value : error_mark_node;
12913 /* Add it to the list. */
12914 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12915 /* If the next token is not a `,', then the list is
12916 complete. */
12917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12918 break;
12919 /* Consume the `,' token. */
12920 cp_lexer_consume_token (parser->lexer);
12923 return clobbers;
12926 /* Parse an (optional) series of attributes.
12928 attributes:
12929 attributes attribute
12931 attribute:
12932 __attribute__ (( attribute-list [opt] ))
12934 The return value is as for cp_parser_attribute_list. */
12936 static tree
12937 cp_parser_attributes_opt (cp_parser* parser)
12939 tree attributes = NULL_TREE;
12941 while (true)
12943 cp_token *token;
12944 tree attribute_list;
12946 /* Peek at the next token. */
12947 token = cp_lexer_peek_token (parser->lexer);
12948 /* If it's not `__attribute__', then we're done. */
12949 if (token->keyword != RID_ATTRIBUTE)
12950 break;
12952 /* Consume the `__attribute__' keyword. */
12953 cp_lexer_consume_token (parser->lexer);
12954 /* Look for the two `(' tokens. */
12955 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12956 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12958 /* Peek at the next token. */
12959 token = cp_lexer_peek_token (parser->lexer);
12960 if (token->type != CPP_CLOSE_PAREN)
12961 /* Parse the attribute-list. */
12962 attribute_list = cp_parser_attribute_list (parser);
12963 else
12964 /* If the next token is a `)', then there is no attribute
12965 list. */
12966 attribute_list = NULL;
12968 /* Look for the two `)' tokens. */
12969 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12970 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12972 /* Add these new attributes to the list. */
12973 attributes = chainon (attributes, attribute_list);
12976 return attributes;
12979 /* Parse an attribute-list.
12981 attribute-list:
12982 attribute
12983 attribute-list , attribute
12985 attribute:
12986 identifier
12987 identifier ( identifier )
12988 identifier ( identifier , expression-list )
12989 identifier ( expression-list )
12991 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12992 TREE_PURPOSE of each node is the identifier indicating which
12993 attribute is in use. The TREE_VALUE represents the arguments, if
12994 any. */
12996 static tree
12997 cp_parser_attribute_list (cp_parser* parser)
12999 tree attribute_list = NULL_TREE;
13001 while (true)
13003 cp_token *token;
13004 tree identifier;
13005 tree attribute;
13007 /* Look for the identifier. We also allow keywords here; for
13008 example `__attribute__ ((const))' is legal. */
13009 token = cp_lexer_peek_token (parser->lexer);
13010 if (token->type != CPP_NAME
13011 && token->type != CPP_KEYWORD)
13012 return error_mark_node;
13013 /* Consume the token. */
13014 token = cp_lexer_consume_token (parser->lexer);
13016 /* Save away the identifier that indicates which attribute this is. */
13017 identifier = token->value;
13018 attribute = build_tree_list (identifier, NULL_TREE);
13020 /* Peek at the next token. */
13021 token = cp_lexer_peek_token (parser->lexer);
13022 /* If it's an `(', then parse the attribute arguments. */
13023 if (token->type == CPP_OPEN_PAREN)
13025 tree arguments;
13026 int arguments_allowed_p = 1;
13028 /* Consume the `('. */
13029 cp_lexer_consume_token (parser->lexer);
13030 /* Peek at the next token. */
13031 token = cp_lexer_peek_token (parser->lexer);
13032 /* Check to see if the next token is an identifier. */
13033 if (token->type == CPP_NAME)
13035 /* Save the identifier. */
13036 identifier = token->value;
13037 /* Consume the identifier. */
13038 cp_lexer_consume_token (parser->lexer);
13039 /* Peek at the next token. */
13040 token = cp_lexer_peek_token (parser->lexer);
13041 /* If the next token is a `,', then there are some other
13042 expressions as well. */
13043 if (token->type == CPP_COMMA)
13044 /* Consume the comma. */
13045 cp_lexer_consume_token (parser->lexer);
13046 else
13047 arguments_allowed_p = 0;
13049 else
13050 identifier = NULL_TREE;
13052 /* If there are arguments, parse them too. */
13053 if (arguments_allowed_p)
13054 arguments = cp_parser_expression_list (parser);
13055 else
13056 arguments = NULL_TREE;
13058 /* Combine the identifier and the arguments. */
13059 if (identifier)
13060 arguments = tree_cons (NULL_TREE, identifier, arguments);
13062 /* Save the identifier and arguments away. */
13063 TREE_VALUE (attribute) = arguments;
13065 /* Look for the closing `)'. */
13066 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13069 /* Add this attribute to the list. */
13070 TREE_CHAIN (attribute) = attribute_list;
13071 attribute_list = attribute;
13073 /* Now, look for more attributes. */
13074 token = cp_lexer_peek_token (parser->lexer);
13075 /* If the next token isn't a `,', we're done. */
13076 if (token->type != CPP_COMMA)
13077 break;
13079 /* Consume the commma and keep going. */
13080 cp_lexer_consume_token (parser->lexer);
13083 /* We built up the list in reverse order. */
13084 return nreverse (attribute_list);
13087 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
13088 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13089 current value of the PEDANTIC flag, regardless of whether or not
13090 the `__extension__' keyword is present. The caller is responsible
13091 for restoring the value of the PEDANTIC flag. */
13093 static bool
13094 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13096 /* Save the old value of the PEDANTIC flag. */
13097 *saved_pedantic = pedantic;
13099 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13101 /* Consume the `__extension__' token. */
13102 cp_lexer_consume_token (parser->lexer);
13103 /* We're not being pedantic while the `__extension__' keyword is
13104 in effect. */
13105 pedantic = 0;
13107 return true;
13110 return false;
13113 /* Parse a label declaration.
13115 label-declaration:
13116 __label__ label-declarator-seq ;
13118 label-declarator-seq:
13119 identifier , label-declarator-seq
13120 identifier */
13122 static void
13123 cp_parser_label_declaration (cp_parser* parser)
13125 /* Look for the `__label__' keyword. */
13126 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13128 while (true)
13130 tree identifier;
13132 /* Look for an identifier. */
13133 identifier = cp_parser_identifier (parser);
13134 /* Declare it as a lobel. */
13135 finish_label_decl (identifier);
13136 /* If the next token is a `;', stop. */
13137 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13138 break;
13139 /* Look for the `,' separating the label declarations. */
13140 cp_parser_require (parser, CPP_COMMA, "`,'");
13143 /* Look for the final `;'. */
13144 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13147 /* Support Functions */
13149 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13150 NAME should have one of the representations used for an
13151 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13152 is returned. If PARSER->SCOPE is a dependent type, then a
13153 SCOPE_REF is returned.
13155 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13156 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13157 was formed. Abstractly, such entities should not be passed to this
13158 function, because they do not need to be looked up, but it is
13159 simpler to check for this special case here, rather than at the
13160 call-sites.
13162 In cases not explicitly covered above, this function returns a
13163 DECL, OVERLOAD, or baselink representing the result of the lookup.
13164 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13165 is returned.
13167 If IS_TYPE is TRUE, bindings that do not refer to types are
13168 ignored.
13170 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13171 are ignored.
13173 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13174 types. */
13176 static tree
13177 cp_parser_lookup_name (cp_parser *parser, tree name,
13178 bool is_type, bool is_namespace, bool check_dependency)
13180 tree decl;
13181 tree object_type = parser->context->object_type;
13183 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13184 no longer valid. Note that if we are parsing tentatively, and
13185 the parse fails, OBJECT_TYPE will be automatically restored. */
13186 parser->context->object_type = NULL_TREE;
13188 if (name == error_mark_node)
13189 return error_mark_node;
13191 /* A template-id has already been resolved; there is no lookup to
13192 do. */
13193 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13194 return name;
13195 if (BASELINK_P (name))
13197 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13198 == TEMPLATE_ID_EXPR),
13199 20020909);
13200 return name;
13203 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13204 it should already have been checked to make sure that the name
13205 used matches the type being destroyed. */
13206 if (TREE_CODE (name) == BIT_NOT_EXPR)
13208 tree type;
13210 /* Figure out to which type this destructor applies. */
13211 if (parser->scope)
13212 type = parser->scope;
13213 else if (object_type)
13214 type = object_type;
13215 else
13216 type = current_class_type;
13217 /* If that's not a class type, there is no destructor. */
13218 if (!type || !CLASS_TYPE_P (type))
13219 return error_mark_node;
13220 /* If it was a class type, return the destructor. */
13221 return CLASSTYPE_DESTRUCTORS (type);
13224 /* By this point, the NAME should be an ordinary identifier. If
13225 the id-expression was a qualified name, the qualifying scope is
13226 stored in PARSER->SCOPE at this point. */
13227 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13228 20000619);
13230 /* Perform the lookup. */
13231 if (parser->scope)
13233 bool dependent_p;
13235 if (parser->scope == error_mark_node)
13236 return error_mark_node;
13238 /* If the SCOPE is dependent, the lookup must be deferred until
13239 the template is instantiated -- unless we are explicitly
13240 looking up names in uninstantiated templates. Even then, we
13241 cannot look up the name if the scope is not a class type; it
13242 might, for example, be a template type parameter. */
13243 dependent_p = (TYPE_P (parser->scope)
13244 && !(parser->in_declarator_p
13245 && currently_open_class (parser->scope))
13246 && dependent_type_p (parser->scope));
13247 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13248 && dependent_p)
13250 if (!is_type)
13251 decl = build_nt (SCOPE_REF, parser->scope, name);
13252 else
13253 /* The resolution to Core Issue 180 says that `struct A::B'
13254 should be considered a type-name, even if `A' is
13255 dependent. */
13256 decl = TYPE_NAME (make_typename_type (parser->scope,
13257 name,
13258 /*complain=*/1));
13260 else
13262 /* If PARSER->SCOPE is a dependent type, then it must be a
13263 class type, and we must not be checking dependencies;
13264 otherwise, we would have processed this lookup above. So
13265 that PARSER->SCOPE is not considered a dependent base by
13266 lookup_member, we must enter the scope here. */
13267 if (dependent_p)
13268 push_scope (parser->scope);
13269 /* If the PARSER->SCOPE is a a template specialization, it
13270 may be instantiated during name lookup. In that case,
13271 errors may be issued. Even if we rollback the current
13272 tentative parse, those errors are valid. */
13273 decl = lookup_qualified_name (parser->scope, name, is_type,
13274 /*flags=*/0);
13275 if (dependent_p)
13276 pop_scope (parser->scope);
13278 parser->qualifying_scope = parser->scope;
13279 parser->object_scope = NULL_TREE;
13281 else if (object_type)
13283 tree object_decl = NULL_TREE;
13284 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13285 OBJECT_TYPE is not a class. */
13286 if (CLASS_TYPE_P (object_type))
13287 /* If the OBJECT_TYPE is a template specialization, it may
13288 be instantiated during name lookup. In that case, errors
13289 may be issued. Even if we rollback the current tentative
13290 parse, those errors are valid. */
13291 object_decl = lookup_member (object_type,
13292 name,
13293 /*protect=*/0, is_type);
13294 /* Look it up in the enclosing context, too. */
13295 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13296 is_namespace,
13297 /*flags=*/0);
13298 parser->object_scope = object_type;
13299 parser->qualifying_scope = NULL_TREE;
13300 if (object_decl)
13301 decl = object_decl;
13303 else
13305 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13306 is_namespace,
13307 /*flags=*/0);
13308 parser->qualifying_scope = NULL_TREE;
13309 parser->object_scope = NULL_TREE;
13312 /* If the lookup failed, let our caller know. */
13313 if (!decl
13314 || decl == error_mark_node
13315 || (TREE_CODE (decl) == FUNCTION_DECL
13316 && DECL_ANTICIPATED (decl)))
13317 return error_mark_node;
13319 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13320 if (TREE_CODE (decl) == TREE_LIST)
13322 /* The error message we have to print is too complicated for
13323 cp_parser_error, so we incorporate its actions directly. */
13324 if (!cp_parser_simulate_error (parser))
13326 error ("reference to `%D' is ambiguous", name);
13327 print_candidates (decl);
13329 return error_mark_node;
13332 my_friendly_assert (DECL_P (decl)
13333 || TREE_CODE (decl) == OVERLOAD
13334 || TREE_CODE (decl) == SCOPE_REF
13335 || BASELINK_P (decl),
13336 20000619);
13338 /* If we have resolved the name of a member declaration, check to
13339 see if the declaration is accessible. When the name resolves to
13340 set of overloaded functions, accesibility is checked when
13341 overload resolution is done.
13343 During an explicit instantiation, access is not checked at all,
13344 as per [temp.explicit]. */
13345 if (DECL_P (decl))
13347 tree qualifying_type;
13349 /* Figure out the type through which DECL is being
13350 accessed. */
13351 qualifying_type
13352 = cp_parser_scope_through_which_access_occurs (decl,
13353 object_type,
13354 parser->scope);
13355 if (qualifying_type)
13356 perform_or_defer_access_check (qualifying_type, decl);
13359 return decl;
13362 /* Like cp_parser_lookup_name, but for use in the typical case where
13363 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13364 TRUE. */
13366 static tree
13367 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13369 return cp_parser_lookup_name (parser, name,
13370 /*is_type=*/false,
13371 /*is_namespace=*/false,
13372 /*check_dependency=*/true);
13375 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13376 the current context, return the TYPE_DECL. If TAG_NAME_P is
13377 true, the DECL indicates the class being defined in a class-head,
13378 or declared in an elaborated-type-specifier.
13380 Otherwise, return DECL. */
13382 static tree
13383 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13385 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13386 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13388 struct A {
13389 template <typename T> struct B;
13392 template <typename T> struct A::B {};
13394 Similarly, in a elaborated-type-specifier:
13396 namespace N { struct X{}; }
13398 struct A {
13399 template <typename T> friend struct N::X;
13402 However, if the DECL refers to a class type, and we are in
13403 the scope of the class, then the name lookup automatically
13404 finds the TYPE_DECL created by build_self_reference rather
13405 than a TEMPLATE_DECL. For example, in:
13407 template <class T> struct S {
13408 S s;
13411 there is no need to handle such case. */
13413 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13414 return DECL_TEMPLATE_RESULT (decl);
13416 return decl;
13419 /* If too many, or too few, template-parameter lists apply to the
13420 declarator, issue an error message. Returns TRUE if all went well,
13421 and FALSE otherwise. */
13423 static bool
13424 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13425 tree declarator)
13427 unsigned num_templates;
13429 /* We haven't seen any classes that involve template parameters yet. */
13430 num_templates = 0;
13432 switch (TREE_CODE (declarator))
13434 case CALL_EXPR:
13435 case ARRAY_REF:
13436 case INDIRECT_REF:
13437 case ADDR_EXPR:
13439 tree main_declarator = TREE_OPERAND (declarator, 0);
13440 return
13441 cp_parser_check_declarator_template_parameters (parser,
13442 main_declarator);
13445 case SCOPE_REF:
13447 tree scope;
13448 tree member;
13450 scope = TREE_OPERAND (declarator, 0);
13451 member = TREE_OPERAND (declarator, 1);
13453 /* If this is a pointer-to-member, then we are not interested
13454 in the SCOPE, because it does not qualify the thing that is
13455 being declared. */
13456 if (TREE_CODE (member) == INDIRECT_REF)
13457 return (cp_parser_check_declarator_template_parameters
13458 (parser, member));
13460 while (scope && CLASS_TYPE_P (scope))
13462 /* You're supposed to have one `template <...>'
13463 for every template class, but you don't need one
13464 for a full specialization. For example:
13466 template <class T> struct S{};
13467 template <> struct S<int> { void f(); };
13468 void S<int>::f () {}
13470 is correct; there shouldn't be a `template <>' for
13471 the definition of `S<int>::f'. */
13472 if (CLASSTYPE_TEMPLATE_INFO (scope)
13473 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13474 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13475 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13476 ++num_templates;
13478 scope = TYPE_CONTEXT (scope);
13482 /* Fall through. */
13484 default:
13485 /* If the DECLARATOR has the form `X<y>' then it uses one
13486 additional level of template parameters. */
13487 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13488 ++num_templates;
13490 return cp_parser_check_template_parameters (parser,
13491 num_templates);
13495 /* NUM_TEMPLATES were used in the current declaration. If that is
13496 invalid, return FALSE and issue an error messages. Otherwise,
13497 return TRUE. */
13499 static bool
13500 cp_parser_check_template_parameters (cp_parser* parser,
13501 unsigned num_templates)
13503 /* If there are more template classes than parameter lists, we have
13504 something like:
13506 template <class T> void S<T>::R<T>::f (); */
13507 if (parser->num_template_parameter_lists < num_templates)
13509 error ("too few template-parameter-lists");
13510 return false;
13512 /* If there are the same number of template classes and parameter
13513 lists, that's OK. */
13514 if (parser->num_template_parameter_lists == num_templates)
13515 return true;
13516 /* If there are more, but only one more, then we are referring to a
13517 member template. That's OK too. */
13518 if (parser->num_template_parameter_lists == num_templates + 1)
13519 return true;
13520 /* Otherwise, there are too many template parameter lists. We have
13521 something like:
13523 template <class T> template <class U> void S::f(); */
13524 error ("too many template-parameter-lists");
13525 return false;
13528 /* Parse a binary-expression of the general form:
13530 binary-expression:
13531 <expr>
13532 binary-expression <token> <expr>
13534 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13535 to parser the <expr>s. If the first production is used, then the
13536 value returned by FN is returned directly. Otherwise, a node with
13537 the indicated EXPR_TYPE is returned, with operands corresponding to
13538 the two sub-expressions. */
13540 static tree
13541 cp_parser_binary_expression (cp_parser* parser,
13542 const cp_parser_token_tree_map token_tree_map,
13543 cp_parser_expression_fn fn)
13545 tree lhs;
13547 /* Parse the first expression. */
13548 lhs = (*fn) (parser);
13549 /* Now, look for more expressions. */
13550 while (true)
13552 cp_token *token;
13553 const cp_parser_token_tree_map_node *map_node;
13554 tree rhs;
13556 /* Peek at the next token. */
13557 token = cp_lexer_peek_token (parser->lexer);
13558 /* If the token is `>', and that's not an operator at the
13559 moment, then we're done. */
13560 if (token->type == CPP_GREATER
13561 && !parser->greater_than_is_operator_p)
13562 break;
13563 /* If we find one of the tokens we want, build the correspoding
13564 tree representation. */
13565 for (map_node = token_tree_map;
13566 map_node->token_type != CPP_EOF;
13567 ++map_node)
13568 if (map_node->token_type == token->type)
13570 /* Consume the operator token. */
13571 cp_lexer_consume_token (parser->lexer);
13572 /* Parse the right-hand side of the expression. */
13573 rhs = (*fn) (parser);
13574 /* Build the binary tree node. */
13575 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13576 break;
13579 /* If the token wasn't one of the ones we want, we're done. */
13580 if (map_node->token_type == CPP_EOF)
13581 break;
13584 return lhs;
13587 /* Parse an optional `::' token indicating that the following name is
13588 from the global namespace. If so, PARSER->SCOPE is set to the
13589 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13590 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13591 Returns the new value of PARSER->SCOPE, if the `::' token is
13592 present, and NULL_TREE otherwise. */
13594 static tree
13595 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13597 cp_token *token;
13599 /* Peek at the next token. */
13600 token = cp_lexer_peek_token (parser->lexer);
13601 /* If we're looking at a `::' token then we're starting from the
13602 global namespace, not our current location. */
13603 if (token->type == CPP_SCOPE)
13605 /* Consume the `::' token. */
13606 cp_lexer_consume_token (parser->lexer);
13607 /* Set the SCOPE so that we know where to start the lookup. */
13608 parser->scope = global_namespace;
13609 parser->qualifying_scope = global_namespace;
13610 parser->object_scope = NULL_TREE;
13612 return parser->scope;
13614 else if (!current_scope_valid_p)
13616 parser->scope = NULL_TREE;
13617 parser->qualifying_scope = NULL_TREE;
13618 parser->object_scope = NULL_TREE;
13621 return NULL_TREE;
13624 /* Returns TRUE if the upcoming token sequence is the start of a
13625 constructor declarator. If FRIEND_P is true, the declarator is
13626 preceded by the `friend' specifier. */
13628 static bool
13629 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13631 bool constructor_p;
13632 tree type_decl = NULL_TREE;
13633 bool nested_name_p;
13634 cp_token *next_token;
13636 /* The common case is that this is not a constructor declarator, so
13637 try to avoid doing lots of work if at all possible. It's not
13638 valid declare a constructor at function scope. */
13639 if (at_function_scope_p ())
13640 return false;
13641 /* And only certain tokens can begin a constructor declarator. */
13642 next_token = cp_lexer_peek_token (parser->lexer);
13643 if (next_token->type != CPP_NAME
13644 && next_token->type != CPP_SCOPE
13645 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13646 && next_token->type != CPP_TEMPLATE_ID)
13647 return false;
13649 /* Parse tentatively; we are going to roll back all of the tokens
13650 consumed here. */
13651 cp_parser_parse_tentatively (parser);
13652 /* Assume that we are looking at a constructor declarator. */
13653 constructor_p = true;
13655 /* Look for the optional `::' operator. */
13656 cp_parser_global_scope_opt (parser,
13657 /*current_scope_valid_p=*/false);
13658 /* Look for the nested-name-specifier. */
13659 nested_name_p
13660 = (cp_parser_nested_name_specifier_opt (parser,
13661 /*typename_keyword_p=*/false,
13662 /*check_dependency_p=*/false,
13663 /*type_p=*/false)
13664 != NULL_TREE);
13665 /* Outside of a class-specifier, there must be a
13666 nested-name-specifier. */
13667 if (!nested_name_p &&
13668 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13669 || friend_p))
13670 constructor_p = false;
13671 /* If we still think that this might be a constructor-declarator,
13672 look for a class-name. */
13673 if (constructor_p)
13675 /* If we have:
13677 template <typename T> struct S { S(); };
13678 template <typename T> S<T>::S ();
13680 we must recognize that the nested `S' names a class.
13681 Similarly, for:
13683 template <typename T> S<T>::S<T> ();
13685 we must recognize that the nested `S' names a template. */
13686 type_decl = cp_parser_class_name (parser,
13687 /*typename_keyword_p=*/false,
13688 /*template_keyword_p=*/false,
13689 /*type_p=*/false,
13690 /*check_dependency_p=*/false,
13691 /*class_head_p=*/false);
13692 /* If there was no class-name, then this is not a constructor. */
13693 constructor_p = !cp_parser_error_occurred (parser);
13696 /* If we're still considering a constructor, we have to see a `(',
13697 to begin the parameter-declaration-clause, followed by either a
13698 `)', an `...', or a decl-specifier. We need to check for a
13699 type-specifier to avoid being fooled into thinking that:
13701 S::S (f) (int);
13703 is a constructor. (It is actually a function named `f' that
13704 takes one parameter (of type `int') and returns a value of type
13705 `S::S'. */
13706 if (constructor_p
13707 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13710 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13711 && !cp_parser_storage_class_specifier_opt (parser))
13713 tree type;
13715 /* Names appearing in the type-specifier should be looked up
13716 in the scope of the class. */
13717 if (current_class_type)
13718 type = NULL_TREE;
13719 else
13721 type = TREE_TYPE (type_decl);
13722 if (TREE_CODE (type) == TYPENAME_TYPE)
13724 type = resolve_typename_type (type,
13725 /*only_current_p=*/false);
13726 if (type == error_mark_node)
13728 cp_parser_abort_tentative_parse (parser);
13729 return false;
13732 push_scope (type);
13734 /* Look for the type-specifier. */
13735 cp_parser_type_specifier (parser,
13736 CP_PARSER_FLAGS_NONE,
13737 /*is_friend=*/false,
13738 /*is_declarator=*/true,
13739 /*declares_class_or_enum=*/NULL,
13740 /*is_cv_qualifier=*/NULL);
13741 /* Leave the scope of the class. */
13742 if (type)
13743 pop_scope (type);
13745 constructor_p = !cp_parser_error_occurred (parser);
13748 else
13749 constructor_p = false;
13750 /* We did not really want to consume any tokens. */
13751 cp_parser_abort_tentative_parse (parser);
13753 return constructor_p;
13756 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13757 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13758 they must be performed once we are in the scope of the function.
13760 Returns the function defined. */
13762 static tree
13763 cp_parser_function_definition_from_specifiers_and_declarator
13764 (cp_parser* parser,
13765 tree decl_specifiers,
13766 tree attributes,
13767 tree declarator)
13769 tree fn;
13770 bool success_p;
13772 /* Begin the function-definition. */
13773 success_p = begin_function_definition (decl_specifiers,
13774 attributes,
13775 declarator);
13777 /* If there were names looked up in the decl-specifier-seq that we
13778 did not check, check them now. We must wait until we are in the
13779 scope of the function to perform the checks, since the function
13780 might be a friend. */
13781 perform_deferred_access_checks ();
13783 if (!success_p)
13785 /* If begin_function_definition didn't like the definition, skip
13786 the entire function. */
13787 error ("invalid function declaration");
13788 cp_parser_skip_to_end_of_block_or_statement (parser);
13789 fn = error_mark_node;
13791 else
13792 fn = cp_parser_function_definition_after_declarator (parser,
13793 /*inline_p=*/false);
13795 return fn;
13798 /* Parse the part of a function-definition that follows the
13799 declarator. INLINE_P is TRUE iff this function is an inline
13800 function defined with a class-specifier.
13802 Returns the function defined. */
13804 static tree
13805 cp_parser_function_definition_after_declarator (cp_parser* parser,
13806 bool inline_p)
13808 tree fn;
13809 bool ctor_initializer_p = false;
13810 bool saved_in_unbraced_linkage_specification_p;
13811 unsigned saved_num_template_parameter_lists;
13813 /* If the next token is `return', then the code may be trying to
13814 make use of the "named return value" extension that G++ used to
13815 support. */
13816 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13818 /* Consume the `return' keyword. */
13819 cp_lexer_consume_token (parser->lexer);
13820 /* Look for the identifier that indicates what value is to be
13821 returned. */
13822 cp_parser_identifier (parser);
13823 /* Issue an error message. */
13824 error ("named return values are no longer supported");
13825 /* Skip tokens until we reach the start of the function body. */
13826 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13827 cp_lexer_consume_token (parser->lexer);
13829 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13830 anything declared inside `f'. */
13831 saved_in_unbraced_linkage_specification_p
13832 = parser->in_unbraced_linkage_specification_p;
13833 parser->in_unbraced_linkage_specification_p = false;
13834 /* Inside the function, surrounding template-parameter-lists do not
13835 apply. */
13836 saved_num_template_parameter_lists
13837 = parser->num_template_parameter_lists;
13838 parser->num_template_parameter_lists = 0;
13839 /* If the next token is `try', then we are looking at a
13840 function-try-block. */
13841 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13842 ctor_initializer_p = cp_parser_function_try_block (parser);
13843 /* A function-try-block includes the function-body, so we only do
13844 this next part if we're not processing a function-try-block. */
13845 else
13846 ctor_initializer_p
13847 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13849 /* Finish the function. */
13850 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13851 (inline_p ? 2 : 0));
13852 /* Generate code for it, if necessary. */
13853 expand_body (fn);
13854 /* Restore the saved values. */
13855 parser->in_unbraced_linkage_specification_p
13856 = saved_in_unbraced_linkage_specification_p;
13857 parser->num_template_parameter_lists
13858 = saved_num_template_parameter_lists;
13860 return fn;
13863 /* Parse a template-declaration, assuming that the `export' (and
13864 `extern') keywords, if present, has already been scanned. MEMBER_P
13865 is as for cp_parser_template_declaration. */
13867 static void
13868 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13870 tree decl = NULL_TREE;
13871 tree parameter_list;
13872 bool friend_p = false;
13874 /* Look for the `template' keyword. */
13875 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13876 return;
13878 /* And the `<'. */
13879 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13880 return;
13882 /* Parse the template parameters. */
13883 begin_template_parm_list ();
13884 /* If the next token is `>', then we have an invalid
13885 specialization. Rather than complain about an invalid template
13886 parameter, issue an error message here. */
13887 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13889 cp_parser_error (parser, "invalid explicit specialization");
13890 parameter_list = NULL_TREE;
13892 else
13893 parameter_list = cp_parser_template_parameter_list (parser);
13894 parameter_list = end_template_parm_list (parameter_list);
13895 /* Look for the `>'. */
13896 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13897 /* We just processed one more parameter list. */
13898 ++parser->num_template_parameter_lists;
13899 /* If the next token is `template', there are more template
13900 parameters. */
13901 if (cp_lexer_next_token_is_keyword (parser->lexer,
13902 RID_TEMPLATE))
13903 cp_parser_template_declaration_after_export (parser, member_p);
13904 else
13906 decl = cp_parser_single_declaration (parser,
13907 member_p,
13908 &friend_p);
13910 /* If this is a member template declaration, let the front
13911 end know. */
13912 if (member_p && !friend_p && decl)
13913 decl = finish_member_template_decl (decl);
13914 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13915 make_friend_class (current_class_type, TREE_TYPE (decl));
13917 /* We are done with the current parameter list. */
13918 --parser->num_template_parameter_lists;
13920 /* Finish up. */
13921 finish_template_decl (parameter_list);
13923 /* Register member declarations. */
13924 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13925 finish_member_declaration (decl);
13927 /* If DECL is a function template, we must return to parse it later.
13928 (Even though there is no definition, there might be default
13929 arguments that need handling.) */
13930 if (member_p && decl
13931 && (TREE_CODE (decl) == FUNCTION_DECL
13932 || DECL_FUNCTION_TEMPLATE_P (decl)))
13933 TREE_VALUE (parser->unparsed_functions_queues)
13934 = tree_cons (NULL_TREE, decl,
13935 TREE_VALUE (parser->unparsed_functions_queues));
13938 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13939 `function-definition' sequence. MEMBER_P is true, this declaration
13940 appears in a class scope.
13942 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13943 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13945 static tree
13946 cp_parser_single_declaration (cp_parser* parser,
13947 bool member_p,
13948 bool* friend_p)
13950 bool declares_class_or_enum;
13951 tree decl = NULL_TREE;
13952 tree decl_specifiers;
13953 tree attributes;
13955 /* Parse the dependent declaration. We don't know yet
13956 whether it will be a function-definition. */
13957 cp_parser_parse_tentatively (parser);
13958 /* Defer access checks until we know what is being declared. */
13959 push_deferring_access_checks (dk_deferred);
13961 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13962 alternative. */
13963 decl_specifiers
13964 = cp_parser_decl_specifier_seq (parser,
13965 CP_PARSER_FLAGS_OPTIONAL,
13966 &attributes,
13967 &declares_class_or_enum);
13968 /* Gather up the access checks that occurred the
13969 decl-specifier-seq. */
13970 stop_deferring_access_checks ();
13972 /* Check for the declaration of a template class. */
13973 if (declares_class_or_enum)
13975 if (cp_parser_declares_only_class_p (parser))
13977 decl = shadow_tag (decl_specifiers);
13978 if (decl)
13979 decl = TYPE_NAME (decl);
13980 else
13981 decl = error_mark_node;
13984 else
13985 decl = NULL_TREE;
13986 /* If it's not a template class, try for a template function. If
13987 the next token is a `;', then this declaration does not declare
13988 anything. But, if there were errors in the decl-specifiers, then
13989 the error might well have come from an attempted class-specifier.
13990 In that case, there's no need to warn about a missing declarator. */
13991 if (!decl
13992 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13993 || !value_member (error_mark_node, decl_specifiers)))
13994 decl = cp_parser_init_declarator (parser,
13995 decl_specifiers,
13996 attributes,
13997 /*function_definition_allowed_p=*/false,
13998 member_p,
13999 /*function_definition_p=*/NULL);
14001 pop_deferring_access_checks ();
14003 /* Clear any current qualification; whatever comes next is the start
14004 of something new. */
14005 parser->scope = NULL_TREE;
14006 parser->qualifying_scope = NULL_TREE;
14007 parser->object_scope = NULL_TREE;
14008 /* Look for a trailing `;' after the declaration. */
14009 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
14010 && cp_parser_committed_to_tentative_parse (parser))
14011 cp_parser_skip_to_end_of_block_or_statement (parser);
14012 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14013 if (cp_parser_parse_definitely (parser))
14015 if (friend_p)
14016 *friend_p = cp_parser_friend_p (decl_specifiers);
14018 /* Otherwise, try a function-definition. */
14019 else
14020 decl = cp_parser_function_definition (parser, friend_p);
14022 return decl;
14025 /* Parse a functional cast to TYPE. Returns an expression
14026 representing the cast. */
14028 static tree
14029 cp_parser_functional_cast (cp_parser* parser, tree type)
14031 tree expression_list;
14033 /* Look for the opening `('. */
14034 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14035 return error_mark_node;
14036 /* If the next token is not an `)', there are arguments to the
14037 cast. */
14038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14039 expression_list = cp_parser_expression_list (parser);
14040 else
14041 expression_list = NULL_TREE;
14042 /* Look for the closing `)'. */
14043 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14045 return build_functional_cast (type, expression_list);
14048 /* MEMBER_FUNCTION is a member function, or a friend. If default
14049 arguments, or the body of the function have not yet been parsed,
14050 parse them now. */
14052 static void
14053 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14055 cp_lexer *saved_lexer;
14057 /* If this member is a template, get the underlying
14058 FUNCTION_DECL. */
14059 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14060 member_function = DECL_TEMPLATE_RESULT (member_function);
14062 /* There should not be any class definitions in progress at this
14063 point; the bodies of members are only parsed outside of all class
14064 definitions. */
14065 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14066 /* While we're parsing the member functions we might encounter more
14067 classes. We want to handle them right away, but we don't want
14068 them getting mixed up with functions that are currently in the
14069 queue. */
14070 parser->unparsed_functions_queues
14071 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14073 /* Make sure that any template parameters are in scope. */
14074 maybe_begin_member_template_processing (member_function);
14076 /* If the body of the function has not yet been parsed, parse it
14077 now. */
14078 if (DECL_PENDING_INLINE_P (member_function))
14080 tree function_scope;
14081 cp_token_cache *tokens;
14083 /* The function is no longer pending; we are processing it. */
14084 tokens = DECL_PENDING_INLINE_INFO (member_function);
14085 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14086 DECL_PENDING_INLINE_P (member_function) = 0;
14087 /* If this was an inline function in a local class, enter the scope
14088 of the containing function. */
14089 function_scope = decl_function_context (member_function);
14090 if (function_scope)
14091 push_function_context_to (function_scope);
14093 /* Save away the current lexer. */
14094 saved_lexer = parser->lexer;
14095 /* Make a new lexer to feed us the tokens saved for this function. */
14096 parser->lexer = cp_lexer_new_from_tokens (tokens);
14097 parser->lexer->next = saved_lexer;
14099 /* Set the current source position to be the location of the first
14100 token in the saved inline body. */
14101 cp_lexer_peek_token (parser->lexer);
14103 /* Let the front end know that we going to be defining this
14104 function. */
14105 start_function (NULL_TREE, member_function, NULL_TREE,
14106 SF_PRE_PARSED | SF_INCLASS_INLINE);
14108 /* Now, parse the body of the function. */
14109 cp_parser_function_definition_after_declarator (parser,
14110 /*inline_p=*/true);
14112 /* Leave the scope of the containing function. */
14113 if (function_scope)
14114 pop_function_context_from (function_scope);
14115 /* Restore the lexer. */
14116 parser->lexer = saved_lexer;
14119 /* Remove any template parameters from the symbol table. */
14120 maybe_end_member_template_processing ();
14122 /* Restore the queue. */
14123 parser->unparsed_functions_queues
14124 = TREE_CHAIN (parser->unparsed_functions_queues);
14127 /* FN is a FUNCTION_DECL which may contains a parameter with an
14128 unparsed DEFAULT_ARG. Parse the default args now. */
14130 static void
14131 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14133 cp_lexer *saved_lexer;
14134 cp_token_cache *tokens;
14135 bool saved_local_variables_forbidden_p;
14136 tree parameters;
14138 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14139 parameters;
14140 parameters = TREE_CHAIN (parameters))
14142 if (!TREE_PURPOSE (parameters)
14143 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14144 continue;
14146 /* Save away the current lexer. */
14147 saved_lexer = parser->lexer;
14148 /* Create a new one, using the tokens we have saved. */
14149 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14150 parser->lexer = cp_lexer_new_from_tokens (tokens);
14152 /* Set the current source position to be the location of the
14153 first token in the default argument. */
14154 cp_lexer_peek_token (parser->lexer);
14156 /* Local variable names (and the `this' keyword) may not appear
14157 in a default argument. */
14158 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14159 parser->local_variables_forbidden_p = true;
14160 /* Parse the assignment-expression. */
14161 if (DECL_CONTEXT (fn))
14162 push_nested_class (DECL_CONTEXT (fn));
14163 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14164 if (DECL_CONTEXT (fn))
14165 pop_nested_class ();
14167 /* Restore saved state. */
14168 parser->lexer = saved_lexer;
14169 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14173 /* Parse the operand of `sizeof' (or a similar operator). Returns
14174 either a TYPE or an expression, depending on the form of the
14175 input. The KEYWORD indicates which kind of expression we have
14176 encountered. */
14178 static tree
14179 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14181 static const char *format;
14182 tree expr = NULL_TREE;
14183 const char *saved_message;
14184 bool saved_constant_expression_p;
14186 /* Initialize FORMAT the first time we get here. */
14187 if (!format)
14188 format = "types may not be defined in `%s' expressions";
14190 /* Types cannot be defined in a `sizeof' expression. Save away the
14191 old message. */
14192 saved_message = parser->type_definition_forbidden_message;
14193 /* And create the new one. */
14194 parser->type_definition_forbidden_message
14195 = ((const char *)
14196 xmalloc (strlen (format)
14197 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14198 + 1 /* `\0' */));
14199 sprintf ((char *) parser->type_definition_forbidden_message,
14200 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14202 /* The restrictions on constant-expressions do not apply inside
14203 sizeof expressions. */
14204 saved_constant_expression_p = parser->constant_expression_p;
14205 parser->constant_expression_p = false;
14207 /* Do not actually evaluate the expression. */
14208 ++skip_evaluation;
14209 /* If it's a `(', then we might be looking at the type-id
14210 construction. */
14211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14213 tree type;
14215 /* We can't be sure yet whether we're looking at a type-id or an
14216 expression. */
14217 cp_parser_parse_tentatively (parser);
14218 /* Consume the `('. */
14219 cp_lexer_consume_token (parser->lexer);
14220 /* Parse the type-id. */
14221 type = cp_parser_type_id (parser);
14222 /* Now, look for the trailing `)'. */
14223 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14224 /* If all went well, then we're done. */
14225 if (cp_parser_parse_definitely (parser))
14227 /* Build a list of decl-specifiers; right now, we have only
14228 a single type-specifier. */
14229 type = build_tree_list (NULL_TREE,
14230 type);
14232 /* Call grokdeclarator to figure out what type this is. */
14233 expr = grokdeclarator (NULL_TREE,
14234 type,
14235 TYPENAME,
14236 /*initialized=*/0,
14237 /*attrlist=*/NULL);
14241 /* If the type-id production did not work out, then we must be
14242 looking at the unary-expression production. */
14243 if (!expr)
14244 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14245 /* Go back to evaluating expressions. */
14246 --skip_evaluation;
14248 /* Free the message we created. */
14249 free ((char *) parser->type_definition_forbidden_message);
14250 /* And restore the old one. */
14251 parser->type_definition_forbidden_message = saved_message;
14252 parser->constant_expression_p = saved_constant_expression_p;
14254 return expr;
14257 /* If the current declaration has no declarator, return true. */
14259 static bool
14260 cp_parser_declares_only_class_p (cp_parser *parser)
14262 /* If the next token is a `;' or a `,' then there is no
14263 declarator. */
14264 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14265 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14268 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14269 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14271 static bool
14272 cp_parser_friend_p (tree decl_specifiers)
14274 while (decl_specifiers)
14276 /* See if this decl-specifier is `friend'. */
14277 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14278 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14279 return true;
14281 /* Go on to the next decl-specifier. */
14282 decl_specifiers = TREE_CHAIN (decl_specifiers);
14285 return false;
14288 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14289 issue an error message indicating that TOKEN_DESC was expected.
14291 Returns the token consumed, if the token had the appropriate type.
14292 Otherwise, returns NULL. */
14294 static cp_token *
14295 cp_parser_require (cp_parser* parser,
14296 enum cpp_ttype type,
14297 const char* token_desc)
14299 if (cp_lexer_next_token_is (parser->lexer, type))
14300 return cp_lexer_consume_token (parser->lexer);
14301 else
14303 /* Output the MESSAGE -- unless we're parsing tentatively. */
14304 if (!cp_parser_simulate_error (parser))
14305 error ("expected %s", token_desc);
14306 return NULL;
14310 /* Like cp_parser_require, except that tokens will be skipped until
14311 the desired token is found. An error message is still produced if
14312 the next token is not as expected. */
14314 static void
14315 cp_parser_skip_until_found (cp_parser* parser,
14316 enum cpp_ttype type,
14317 const char* token_desc)
14319 cp_token *token;
14320 unsigned nesting_depth = 0;
14322 if (cp_parser_require (parser, type, token_desc))
14323 return;
14325 /* Skip tokens until the desired token is found. */
14326 while (true)
14328 /* Peek at the next token. */
14329 token = cp_lexer_peek_token (parser->lexer);
14330 /* If we've reached the token we want, consume it and
14331 stop. */
14332 if (token->type == type && !nesting_depth)
14334 cp_lexer_consume_token (parser->lexer);
14335 return;
14337 /* If we've run out of tokens, stop. */
14338 if (token->type == CPP_EOF)
14339 return;
14340 if (token->type == CPP_OPEN_BRACE
14341 || token->type == CPP_OPEN_PAREN
14342 || token->type == CPP_OPEN_SQUARE)
14343 ++nesting_depth;
14344 else if (token->type == CPP_CLOSE_BRACE
14345 || token->type == CPP_CLOSE_PAREN
14346 || token->type == CPP_CLOSE_SQUARE)
14348 if (nesting_depth-- == 0)
14349 return;
14351 /* Consume this token. */
14352 cp_lexer_consume_token (parser->lexer);
14356 /* If the next token is the indicated keyword, consume it. Otherwise,
14357 issue an error message indicating that TOKEN_DESC was expected.
14359 Returns the token consumed, if the token had the appropriate type.
14360 Otherwise, returns NULL. */
14362 static cp_token *
14363 cp_parser_require_keyword (cp_parser* parser,
14364 enum rid keyword,
14365 const char* token_desc)
14367 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14369 if (token && token->keyword != keyword)
14371 dyn_string_t error_msg;
14373 /* Format the error message. */
14374 error_msg = dyn_string_new (0);
14375 dyn_string_append_cstr (error_msg, "expected ");
14376 dyn_string_append_cstr (error_msg, token_desc);
14377 cp_parser_error (parser, error_msg->s);
14378 dyn_string_delete (error_msg);
14379 return NULL;
14382 return token;
14385 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14386 function-definition. */
14388 static bool
14389 cp_parser_token_starts_function_definition_p (cp_token* token)
14391 return (/* An ordinary function-body begins with an `{'. */
14392 token->type == CPP_OPEN_BRACE
14393 /* A ctor-initializer begins with a `:'. */
14394 || token->type == CPP_COLON
14395 /* A function-try-block begins with `try'. */
14396 || token->keyword == RID_TRY
14397 /* The named return value extension begins with `return'. */
14398 || token->keyword == RID_RETURN);
14401 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14402 definition. */
14404 static bool
14405 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14407 cp_token *token;
14409 token = cp_lexer_peek_token (parser->lexer);
14410 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14413 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14414 or none_type otherwise. */
14416 static enum tag_types
14417 cp_parser_token_is_class_key (cp_token* token)
14419 switch (token->keyword)
14421 case RID_CLASS:
14422 return class_type;
14423 case RID_STRUCT:
14424 return record_type;
14425 case RID_UNION:
14426 return union_type;
14428 default:
14429 return none_type;
14433 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14435 static void
14436 cp_parser_check_class_key (enum tag_types class_key, tree type)
14438 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14439 pedwarn ("`%s' tag used in naming `%#T'",
14440 class_key == union_type ? "union"
14441 : class_key == record_type ? "struct" : "class",
14442 type);
14445 /* Look for the `template' keyword, as a syntactic disambiguator.
14446 Return TRUE iff it is present, in which case it will be
14447 consumed. */
14449 static bool
14450 cp_parser_optional_template_keyword (cp_parser *parser)
14452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14454 /* The `template' keyword can only be used within templates;
14455 outside templates the parser can always figure out what is a
14456 template and what is not. */
14457 if (!processing_template_decl)
14459 error ("`template' (as a disambiguator) is only allowed "
14460 "within templates");
14461 /* If this part of the token stream is rescanned, the same
14462 error message would be generated. So, we purge the token
14463 from the stream. */
14464 cp_lexer_purge_token (parser->lexer);
14465 return false;
14467 else
14469 /* Consume the `template' keyword. */
14470 cp_lexer_consume_token (parser->lexer);
14471 return true;
14475 return false;
14478 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14479 set PARSER->SCOPE, and perform other related actions. */
14481 static void
14482 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14484 tree value;
14485 tree check;
14487 /* Get the stored value. */
14488 value = cp_lexer_consume_token (parser->lexer)->value;
14489 /* Perform any access checks that were deferred. */
14490 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14491 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14492 /* Set the scope from the stored value. */
14493 parser->scope = TREE_VALUE (value);
14494 parser->qualifying_scope = TREE_TYPE (value);
14495 parser->object_scope = NULL_TREE;
14498 /* Add tokens to CACHE until an non-nested END token appears. */
14500 static void
14501 cp_parser_cache_group (cp_parser *parser,
14502 cp_token_cache *cache,
14503 enum cpp_ttype end,
14504 unsigned depth)
14506 while (true)
14508 cp_token *token;
14510 /* Abort a parenthesized expression if we encounter a brace. */
14511 if ((end == CPP_CLOSE_PAREN || depth == 0)
14512 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14513 return;
14514 /* Consume the next token. */
14515 token = cp_lexer_consume_token (parser->lexer);
14516 /* If we've reached the end of the file, stop. */
14517 if (token->type == CPP_EOF)
14518 return;
14519 /* Add this token to the tokens we are saving. */
14520 cp_token_cache_push_token (cache, token);
14521 /* See if it starts a new group. */
14522 if (token->type == CPP_OPEN_BRACE)
14524 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14525 if (depth == 0)
14526 return;
14528 else if (token->type == CPP_OPEN_PAREN)
14529 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14530 else if (token->type == end)
14531 return;
14535 /* Begin parsing tentatively. We always save tokens while parsing
14536 tentatively so that if the tentative parsing fails we can restore the
14537 tokens. */
14539 static void
14540 cp_parser_parse_tentatively (cp_parser* parser)
14542 /* Enter a new parsing context. */
14543 parser->context = cp_parser_context_new (parser->context);
14544 /* Begin saving tokens. */
14545 cp_lexer_save_tokens (parser->lexer);
14546 /* In order to avoid repetitive access control error messages,
14547 access checks are queued up until we are no longer parsing
14548 tentatively. */
14549 push_deferring_access_checks (dk_deferred);
14552 /* Commit to the currently active tentative parse. */
14554 static void
14555 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14557 cp_parser_context *context;
14558 cp_lexer *lexer;
14560 /* Mark all of the levels as committed. */
14561 lexer = parser->lexer;
14562 for (context = parser->context; context->next; context = context->next)
14564 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14565 break;
14566 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14567 while (!cp_lexer_saving_tokens (lexer))
14568 lexer = lexer->next;
14569 cp_lexer_commit_tokens (lexer);
14573 /* Abort the currently active tentative parse. All consumed tokens
14574 will be rolled back, and no diagnostics will be issued. */
14576 static void
14577 cp_parser_abort_tentative_parse (cp_parser* parser)
14579 cp_parser_simulate_error (parser);
14580 /* Now, pretend that we want to see if the construct was
14581 successfully parsed. */
14582 cp_parser_parse_definitely (parser);
14585 /* Stop parsing tentatively. If a parse error has ocurred, restore the
14586 token stream. Otherwise, commit to the tokens we have consumed.
14587 Returns true if no error occurred; false otherwise. */
14589 static bool
14590 cp_parser_parse_definitely (cp_parser* parser)
14592 bool error_occurred;
14593 cp_parser_context *context;
14595 /* Remember whether or not an error ocurred, since we are about to
14596 destroy that information. */
14597 error_occurred = cp_parser_error_occurred (parser);
14598 /* Remove the topmost context from the stack. */
14599 context = parser->context;
14600 parser->context = context->next;
14601 /* If no parse errors occurred, commit to the tentative parse. */
14602 if (!error_occurred)
14604 /* Commit to the tokens read tentatively, unless that was
14605 already done. */
14606 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14607 cp_lexer_commit_tokens (parser->lexer);
14609 pop_to_parent_deferring_access_checks ();
14611 /* Otherwise, if errors occurred, roll back our state so that things
14612 are just as they were before we began the tentative parse. */
14613 else
14615 cp_lexer_rollback_tokens (parser->lexer);
14616 pop_deferring_access_checks ();
14618 /* Add the context to the front of the free list. */
14619 context->next = cp_parser_context_free_list;
14620 cp_parser_context_free_list = context;
14622 return !error_occurred;
14625 /* Returns true if we are parsing tentatively -- but have decided that
14626 we will stick with this tentative parse, even if errors occur. */
14628 static bool
14629 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14631 return (cp_parser_parsing_tentatively (parser)
14632 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14635 /* Returns nonzero iff an error has occurred during the most recent
14636 tentative parse. */
14638 static bool
14639 cp_parser_error_occurred (cp_parser* parser)
14641 return (cp_parser_parsing_tentatively (parser)
14642 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14645 /* Returns nonzero if GNU extensions are allowed. */
14647 static bool
14648 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14650 return parser->allow_gnu_extensions_p;
14655 /* The parser. */
14657 static GTY (()) cp_parser *the_parser;
14659 /* External interface. */
14661 /* Parse the entire translation unit. */
14664 yyparse (void)
14666 bool error_occurred;
14668 the_parser = cp_parser_new ();
14669 push_deferring_access_checks (flag_access_control
14670 ? dk_no_deferred : dk_no_check);
14671 error_occurred = cp_parser_translation_unit (the_parser);
14672 the_parser = NULL;
14674 finish_file ();
14676 return error_occurred;
14679 /* Clean up after parsing the entire translation unit. */
14681 void
14682 free_parser_stacks (void)
14684 /* Nothing to do. */
14687 /* This variable must be provided by every front end. */
14689 int yydebug;
14691 #include "gt-cp-parser.h"