]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
config-lang.in: Add semantics.c to gtfiles.
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
a723baf1 1/* C++ Parser.
3beb3abf 2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
a723baf1
MM
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
f5adbb8d 5 This file is part of GCC.
a723baf1 6
f5adbb8d 7 GCC is free software; you can redistribute it and/or modify it
a723baf1
MM
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.
11
f5adbb8d 12 GCC is distributed in the hope that it will be useful, but
a723baf1
MM
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.
16
17 You should have received a copy of the GNU General Public License
f5adbb8d 18 along with GCC; see the file COPYING. If not, write to the Free
a723baf1
MM
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
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"
a723baf1
MM
35#include "toplev.h"
36#include "output.h"
37
38\f
39/* The lexer. */
40
41/* Overview
42 --------
43
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
45 look-ahead.
46
47 Methodology
48 -----------
49
50 We use a circular buffer to store incoming tokens.
51
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.
58
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.
61
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. */
65
66/* A C++ token. */
67
68typedef struct cp_token GTY (())
69{
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 file in which this token was found. */
78 const char *file_name;
79 /* The line at which this token was found. */
80 int line_number;
81} cp_token;
82
83/* The number of tokens in a single token block. */
84
85#define CP_TOKEN_BLOCK_NUM_TOKENS 32
86
87/* A group of tokens. These groups are chained together to store
88 large numbers of tokens. (For example, a token block is created
89 when the body of an inline member function is first encountered;
90 the tokens are processed later after the class definition is
91 complete.)
92
93 This somewhat ungainly data structure (as opposed to, say, a
94 variable-length array), is used due to contraints imposed by the
95 current garbage-collection methodology. If it is made more
96 flexible, we could perhaps simplify the data structures involved. */
97
98typedef struct cp_token_block GTY (())
99{
100 /* The tokens. */
101 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
102 /* The number of tokens in this block. */
103 size_t num_tokens;
104 /* The next token block in the chain. */
105 struct cp_token_block *next;
106 /* The previous block in the chain. */
107 struct cp_token_block *prev;
108} cp_token_block;
109
110typedef struct cp_token_cache GTY (())
111{
112 /* The first block in the cache. NULL if there are no tokens in the
113 cache. */
114 cp_token_block *first;
115 /* The last block in the cache. NULL If there are no tokens in the
116 cache. */
117 cp_token_block *last;
118} cp_token_cache;
119
120/* Prototypes. */
121
122static cp_token_cache *cp_token_cache_new
123 (void);
124static void cp_token_cache_push_token
125 (cp_token_cache *, cp_token *);
126
127/* Create a new cp_token_cache. */
128
129static cp_token_cache *
130cp_token_cache_new ()
131{
132 return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
133}
134
135/* Add *TOKEN to *CACHE. */
136
137static void
138cp_token_cache_push_token (cp_token_cache *cache,
139 cp_token *token)
140{
141 cp_token_block *b = cache->last;
142
143 /* See if we need to allocate a new token block. */
144 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
145 {
146 b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
147 b->prev = cache->last;
148 if (cache->last)
149 {
150 cache->last->next = b;
151 cache->last = b;
152 }
153 else
154 cache->first = cache->last = b;
155 }
156 /* Add this token to the current token block. */
157 b->tokens[b->num_tokens++] = *token;
158}
159
160/* The cp_lexer structure represents the C++ lexer. It is responsible
161 for managing the token stream from the preprocessor and supplying
162 it to the parser. */
163
164typedef struct cp_lexer GTY (())
165{
166 /* The memory allocated for the buffer. Never NULL. */
167 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
168 /* A pointer just past the end of the memory allocated for the buffer. */
169 cp_token * GTY ((skip (""))) buffer_end;
170 /* The first valid token in the buffer, or NULL if none. */
171 cp_token * GTY ((skip (""))) first_token;
172 /* The next available token. If NEXT_TOKEN is NULL, then there are
173 no more available tokens. */
174 cp_token * GTY ((skip (""))) next_token;
175 /* A pointer just past the last available token. If FIRST_TOKEN is
176 NULL, however, there are no available tokens, and then this
177 location is simply the place in which the next token read will be
178 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
179 When the LAST_TOKEN == BUFFER, then the last token is at the
180 highest memory address in the BUFFER. */
181 cp_token * GTY ((skip (""))) last_token;
182
183 /* A stack indicating positions at which cp_lexer_save_tokens was
184 called. The top entry is the most recent position at which we
185 began saving tokens. The entries are differences in token
186 position between FIRST_TOKEN and the first saved token.
187
188 If the stack is non-empty, we are saving tokens. When a token is
189 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
190 pointer will not. The token stream will be preserved so that it
191 can be reexamined later.
192
193 If the stack is empty, then we are not saving tokens. Whenever a
194 token is consumed, the FIRST_TOKEN pointer will be moved, and the
195 consumed token will be gone forever. */
196 varray_type saved_tokens;
197
198 /* The STRING_CST tokens encountered while processing the current
199 string literal. */
200 varray_type string_tokens;
201
202 /* True if we should obtain more tokens from the preprocessor; false
203 if we are processing a saved token cache. */
204 bool main_lexer_p;
205
206 /* True if we should output debugging information. */
207 bool debugging_p;
208
209 /* The next lexer in a linked list of lexers. */
210 struct cp_lexer *next;
211} cp_lexer;
212
213/* Prototypes. */
214
17211ab5
GK
215static cp_lexer *cp_lexer_new_main
216 PARAMS ((void));
a723baf1
MM
217static cp_lexer *cp_lexer_new_from_tokens
218 PARAMS ((struct cp_token_cache *));
219static int cp_lexer_saving_tokens
220 PARAMS ((const cp_lexer *));
221static cp_token *cp_lexer_next_token
222 PARAMS ((cp_lexer *, cp_token *));
223static ptrdiff_t cp_lexer_token_difference
224 PARAMS ((cp_lexer *, cp_token *, cp_token *));
225static cp_token *cp_lexer_read_token
226 PARAMS ((cp_lexer *));
227static void cp_lexer_maybe_grow_buffer
228 PARAMS ((cp_lexer *));
229static void cp_lexer_get_preprocessor_token
230 PARAMS ((cp_lexer *, cp_token *));
231static cp_token *cp_lexer_peek_token
232 PARAMS ((cp_lexer *));
233static cp_token *cp_lexer_peek_nth_token
234 PARAMS ((cp_lexer *, size_t));
f7b5ecd9 235static inline bool cp_lexer_next_token_is
a723baf1
MM
236 PARAMS ((cp_lexer *, enum cpp_ttype));
237static bool cp_lexer_next_token_is_not
238 PARAMS ((cp_lexer *, enum cpp_ttype));
239static bool cp_lexer_next_token_is_keyword
240 PARAMS ((cp_lexer *, enum rid));
241static cp_token *cp_lexer_consume_token
242 PARAMS ((cp_lexer *));
243static void cp_lexer_purge_token
244 (cp_lexer *);
245static void cp_lexer_purge_tokens_after
246 (cp_lexer *, cp_token *);
247static void cp_lexer_save_tokens
248 PARAMS ((cp_lexer *));
249static void cp_lexer_commit_tokens
250 PARAMS ((cp_lexer *));
251static void cp_lexer_rollback_tokens
252 PARAMS ((cp_lexer *));
f7b5ecd9 253static inline void cp_lexer_set_source_position_from_token
a723baf1
MM
254 PARAMS ((cp_lexer *, const cp_token *));
255static void cp_lexer_print_token
256 PARAMS ((FILE *, cp_token *));
f7b5ecd9 257static inline bool cp_lexer_debugging_p
a723baf1
MM
258 PARAMS ((cp_lexer *));
259static void cp_lexer_start_debugging
260 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
261static void cp_lexer_stop_debugging
262 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
263
264/* Manifest constants. */
265
266#define CP_TOKEN_BUFFER_SIZE 5
267#define CP_SAVED_TOKENS_SIZE 5
268
269/* A token type for keywords, as opposed to ordinary identifiers. */
270#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
271
272/* A token type for template-ids. If a template-id is processed while
273 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
274 the value of the CPP_TEMPLATE_ID is whatever was returned by
275 cp_parser_template_id. */
276#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
277
278/* A token type for nested-name-specifiers. If a
279 nested-name-specifier is processed while parsing tentatively, it is
280 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
281 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
282 cp_parser_nested_name_specifier_opt. */
283#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
284
285/* A token type for tokens that are not tokens at all; these are used
286 to mark the end of a token block. */
287#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
288
289/* Variables. */
290
291/* The stream to which debugging output should be written. */
292static FILE *cp_lexer_debug_stream;
293
17211ab5
GK
294/* Create a new main C++ lexer, the lexer that gets tokens from the
295 preprocessor. */
a723baf1
MM
296
297static cp_lexer *
17211ab5 298cp_lexer_new_main (void)
a723baf1
MM
299{
300 cp_lexer *lexer;
17211ab5
GK
301 cp_token first_token;
302
303 /* It's possible that lexing the first token will load a PCH file,
304 which is a GC collection point. So we have to grab the first
305 token before allocating any memory. */
306 cp_lexer_get_preprocessor_token (NULL, &first_token);
307 cpp_get_callbacks (parse_in)->valid_pch = NULL;
a723baf1
MM
308
309 /* Allocate the memory. */
310 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
311
312 /* Create the circular buffer. */
313 lexer->buffer = ((cp_token *)
17211ab5 314 ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token)));
a723baf1
MM
315 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
316
17211ab5
GK
317 /* There is one token in the buffer. */
318 lexer->last_token = lexer->buffer + 1;
319 lexer->first_token = lexer->buffer;
320 lexer->next_token = lexer->buffer;
321 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
a723baf1
MM
322
323 /* This lexer obtains more tokens by calling c_lex. */
17211ab5 324 lexer->main_lexer_p = true;
a723baf1
MM
325
326 /* Create the SAVED_TOKENS stack. */
327 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
328
329 /* Create the STRINGS array. */
330 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
331
332 /* Assume we are not debugging. */
333 lexer->debugging_p = false;
334
335 return lexer;
336}
337
338/* Create a new lexer whose token stream is primed with the TOKENS.
339 When these tokens are exhausted, no new tokens will be read. */
340
341static cp_lexer *
342cp_lexer_new_from_tokens (cp_token_cache *tokens)
343{
344 cp_lexer *lexer;
345 cp_token *token;
346 cp_token_block *block;
347 ptrdiff_t num_tokens;
348
17211ab5
GK
349 /* Allocate the memory. */
350 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
351
352 /* Create a new buffer, appropriately sized. */
353 num_tokens = 0;
354 for (block = tokens->first; block != NULL; block = block->next)
355 num_tokens += block->num_tokens;
17211ab5 356 lexer->buffer = ((cp_token *) ggc_alloc (num_tokens * sizeof (cp_token)));
a723baf1
MM
357 lexer->buffer_end = lexer->buffer + num_tokens;
358
359 /* Install the tokens. */
360 token = lexer->buffer;
361 for (block = tokens->first; block != NULL; block = block->next)
362 {
363 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
364 token += block->num_tokens;
365 }
366
367 /* The FIRST_TOKEN is the beginning of the buffer. */
368 lexer->first_token = lexer->buffer;
369 /* The next available token is also at the beginning of the buffer. */
370 lexer->next_token = lexer->buffer;
371 /* The buffer is full. */
372 lexer->last_token = lexer->first_token;
373
17211ab5
GK
374 /* This lexer doesn't obtain more tokens. */
375 lexer->main_lexer_p = false;
376
377 /* Create the SAVED_TOKENS stack. */
378 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
379
380 /* Create the STRINGS array. */
381 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
382
383 /* Assume we are not debugging. */
384 lexer->debugging_p = false;
385
a723baf1
MM
386 return lexer;
387}
388
f7b5ecd9 389/* Returns non-zero if debugging information should be output. */
a723baf1 390
f7b5ecd9
MM
391static inline bool
392cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 393{
f7b5ecd9
MM
394 return lexer->debugging_p;
395}
396
397/* Set the current source position from the information stored in
398 TOKEN. */
399
400static inline void
401cp_lexer_set_source_position_from_token (lexer, token)
402 cp_lexer *lexer ATTRIBUTE_UNUSED;
403 const cp_token *token;
404{
405 /* Ideally, the source position information would not be a global
406 variable, but it is. */
407
408 /* Update the line number. */
409 if (token->type != CPP_EOF)
410 {
411 lineno = token->line_number;
412 input_filename = token->file_name;
413 }
a723baf1
MM
414}
415
416/* TOKEN points into the circular token buffer. Return a pointer to
417 the next token in the buffer. */
418
f7b5ecd9 419static inline cp_token *
a723baf1
MM
420cp_lexer_next_token (lexer, token)
421 cp_lexer *lexer;
422 cp_token *token;
423{
424 token++;
425 if (token == lexer->buffer_end)
426 token = lexer->buffer;
427 return token;
428}
429
f7b5ecd9
MM
430/* Non-zero if we are presently saving tokens. */
431
432static int
433cp_lexer_saving_tokens (lexer)
434 const cp_lexer *lexer;
435{
436 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
437}
438
a723baf1
MM
439/* Return a pointer to the token that is N tokens beyond TOKEN in the
440 buffer. */
441
442static cp_token *
443cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
444{
445 token += n;
446 if (token >= lexer->buffer_end)
447 token = lexer->buffer + (token - lexer->buffer_end);
448 return token;
449}
450
451/* Returns the number of times that START would have to be incremented
452 to reach FINISH. If START and FINISH are the same, returns zero. */
453
454static ptrdiff_t
455cp_lexer_token_difference (lexer, start, finish)
456 cp_lexer *lexer;
457 cp_token *start;
458 cp_token *finish;
459{
460 if (finish >= start)
461 return finish - start;
462 else
463 return ((lexer->buffer_end - lexer->buffer)
464 - (start - finish));
465}
466
467/* Obtain another token from the C preprocessor and add it to the
468 token buffer. Returns the newly read token. */
469
470static cp_token *
471cp_lexer_read_token (lexer)
472 cp_lexer *lexer;
473{
474 cp_token *token;
475
476 /* Make sure there is room in the buffer. */
477 cp_lexer_maybe_grow_buffer (lexer);
478
479 /* If there weren't any tokens, then this one will be the first. */
480 if (!lexer->first_token)
481 lexer->first_token = lexer->last_token;
482 /* Similarly, if there were no available tokens, there is one now. */
483 if (!lexer->next_token)
484 lexer->next_token = lexer->last_token;
485
486 /* Figure out where we're going to store the new token. */
487 token = lexer->last_token;
488
489 /* Get a new token from the preprocessor. */
490 cp_lexer_get_preprocessor_token (lexer, token);
491
492 /* Increment LAST_TOKEN. */
493 lexer->last_token = cp_lexer_next_token (lexer, token);
494
495 /* The preprocessor does not yet do translation phase six, i.e., the
496 combination of adjacent string literals. Therefore, we do it
497 here. */
498 if (token->type == CPP_STRING || token->type == CPP_WSTRING)
499 {
500 ptrdiff_t delta;
501 int i;
502
503 /* When we grow the buffer, we may invalidate TOKEN. So, save
504 the distance from the beginning of the BUFFER so that we can
505 recaulate it. */
506 delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
507 /* Make sure there is room in the buffer for another token. */
508 cp_lexer_maybe_grow_buffer (lexer);
509 /* Restore TOKEN. */
510 token = lexer->buffer;
511 for (i = 0; i < delta; ++i)
512 token = cp_lexer_next_token (lexer, token);
513
514 VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
515 while (true)
516 {
517 /* Read the token after TOKEN. */
518 cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
519 /* See whether it's another string constant. */
520 if (lexer->last_token->type != token->type)
521 {
522 /* If not, then it will be the next real token. */
523 lexer->last_token = cp_lexer_next_token (lexer,
524 lexer->last_token);
525 break;
526 }
527
528 /* Chain the strings together. */
529 VARRAY_PUSH_TREE (lexer->string_tokens,
530 lexer->last_token->value);
531 }
532
533 /* Create a single STRING_CST. Curiously we have to call
534 combine_strings even if there is only a single string in
535 order to get the type set correctly. */
536 token->value = combine_strings (lexer->string_tokens);
537 VARRAY_CLEAR (lexer->string_tokens);
538 token->value = fix_string_type (token->value);
539 /* Strings should have type `const char []'. Right now, we will
540 have an ARRAY_TYPE that is constant rather than an array of
541 constant elements. */
542 if (flag_const_strings)
543 {
544 tree type;
545
546 /* Get the current type. It will be an ARRAY_TYPE. */
547 type = TREE_TYPE (token->value);
548 /* Use build_cplus_array_type to rebuild the array, thereby
549 getting the right type. */
550 type = build_cplus_array_type (TREE_TYPE (type),
551 TYPE_DOMAIN (type));
552 /* Reset the type of the token. */
553 TREE_TYPE (token->value) = type;
554 }
555 }
556
557 return token;
558}
559
560/* If the circular buffer is full, make it bigger. */
561
562static void
563cp_lexer_maybe_grow_buffer (lexer)
564 cp_lexer *lexer;
565{
566 /* If the buffer is full, enlarge it. */
567 if (lexer->last_token == lexer->first_token)
568 {
569 cp_token *new_buffer;
570 cp_token *old_buffer;
571 cp_token *new_first_token;
572 ptrdiff_t buffer_length;
573 size_t num_tokens_to_copy;
574
575 /* Remember the current buffer pointer. It will become invalid,
576 but we will need to do pointer arithmetic involving this
577 value. */
578 old_buffer = lexer->buffer;
579 /* Compute the current buffer size. */
580 buffer_length = lexer->buffer_end - lexer->buffer;
581 /* Allocate a buffer twice as big. */
582 new_buffer = ((cp_token *)
583 ggc_realloc (lexer->buffer,
584 2 * buffer_length * sizeof (cp_token)));
585
586 /* Because the buffer is circular, logically consecutive tokens
587 are not necessarily placed consecutively in memory.
588 Therefore, we must keep move the tokens that were before
589 FIRST_TOKEN to the second half of the newly allocated
590 buffer. */
591 num_tokens_to_copy = (lexer->first_token - old_buffer);
592 memcpy (new_buffer + buffer_length,
593 new_buffer,
594 num_tokens_to_copy * sizeof (cp_token));
595 /* Clear the rest of the buffer. We never look at this storage,
596 but the garbage collector may. */
597 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
598 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
599
600 /* Now recompute all of the buffer pointers. */
601 new_first_token
602 = new_buffer + (lexer->first_token - old_buffer);
603 if (lexer->next_token != NULL)
604 {
605 ptrdiff_t next_token_delta;
606
607 if (lexer->next_token > lexer->first_token)
608 next_token_delta = lexer->next_token - lexer->first_token;
609 else
610 next_token_delta =
611 buffer_length - (lexer->first_token - lexer->next_token);
612 lexer->next_token = new_first_token + next_token_delta;
613 }
614 lexer->last_token = new_first_token + buffer_length;
615 lexer->buffer = new_buffer;
616 lexer->buffer_end = new_buffer + buffer_length * 2;
617 lexer->first_token = new_first_token;
618 }
619}
620
621/* Store the next token from the preprocessor in *TOKEN. */
622
623static void
624cp_lexer_get_preprocessor_token (lexer, token)
625 cp_lexer *lexer ATTRIBUTE_UNUSED;
626 cp_token *token;
627{
628 bool done;
629
630 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 631 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
632 {
633 token->type = CPP_EOF;
634 token->line_number = 0;
635 token->file_name = NULL;
636 token->value = NULL_TREE;
637 token->keyword = RID_MAX;
638
639 return;
640 }
641
642 done = false;
643 /* Keep going until we get a token we like. */
644 while (!done)
645 {
646 /* Get a new token from the preprocessor. */
647 token->type = c_lex (&token->value);
648 /* Issue messages about tokens we cannot process. */
649 switch (token->type)
650 {
651 case CPP_ATSIGN:
652 case CPP_HASH:
653 case CPP_PASTE:
654 error ("invalid token");
655 break;
656
657 case CPP_OTHER:
658 /* These tokens are already warned about by c_lex. */
659 break;
660
661 default:
662 /* This is a good token, so we exit the loop. */
663 done = true;
664 break;
665 }
666 }
667 /* Now we've got our token. */
668 token->line_number = lineno;
669 token->file_name = input_filename;
670
671 /* Check to see if this token is a keyword. */
672 if (token->type == CPP_NAME
673 && C_IS_RESERVED_WORD (token->value))
674 {
675 /* Mark this token as a keyword. */
676 token->type = CPP_KEYWORD;
677 /* Record which keyword. */
678 token->keyword = C_RID_CODE (token->value);
679 /* Update the value. Some keywords are mapped to particular
680 entities, rather than simply having the value of the
681 corresponding IDENTIFIER_NODE. For example, `__const' is
682 mapped to `const'. */
683 token->value = ridpointers[token->keyword];
684 }
685 else
686 token->keyword = RID_MAX;
687}
688
689/* Return a pointer to the next token in the token stream, but do not
690 consume it. */
691
692static cp_token *
693cp_lexer_peek_token (lexer)
694 cp_lexer *lexer;
695{
696 cp_token *token;
697
698 /* If there are no tokens, read one now. */
699 if (!lexer->next_token)
700 cp_lexer_read_token (lexer);
701
702 /* Provide debugging output. */
703 if (cp_lexer_debugging_p (lexer))
704 {
705 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
706 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
707 fprintf (cp_lexer_debug_stream, "\n");
708 }
709
710 token = lexer->next_token;
711 cp_lexer_set_source_position_from_token (lexer, token);
712 return token;
713}
714
715/* Return true if the next token has the indicated TYPE. */
716
717static bool
718cp_lexer_next_token_is (lexer, type)
719 cp_lexer *lexer;
720 enum cpp_ttype type;
721{
722 cp_token *token;
723
724 /* Peek at the next token. */
725 token = cp_lexer_peek_token (lexer);
726 /* Check to see if it has the indicated TYPE. */
727 return token->type == type;
728}
729
730/* Return true if the next token does not have the indicated TYPE. */
731
732static bool
733cp_lexer_next_token_is_not (lexer, type)
734 cp_lexer *lexer;
735 enum cpp_ttype type;
736{
737 return !cp_lexer_next_token_is (lexer, type);
738}
739
740/* Return true if the next token is the indicated KEYWORD. */
741
742static bool
743cp_lexer_next_token_is_keyword (lexer, keyword)
744 cp_lexer *lexer;
745 enum rid keyword;
746{
747 cp_token *token;
748
749 /* Peek at the next token. */
750 token = cp_lexer_peek_token (lexer);
751 /* Check to see if it is the indicated keyword. */
752 return token->keyword == keyword;
753}
754
755/* Return a pointer to the Nth token in the token stream. If N is 1,
756 then this is precisely equivalent to cp_lexer_peek_token. */
757
758static cp_token *
759cp_lexer_peek_nth_token (lexer, n)
760 cp_lexer *lexer;
761 size_t n;
762{
763 cp_token *token;
764
765 /* N is 1-based, not zero-based. */
766 my_friendly_assert (n > 0, 20000224);
767
768 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
769 token = lexer->next_token;
770 /* If there are no tokens in the buffer, get one now. */
771 if (!token)
772 {
773 cp_lexer_read_token (lexer);
774 token = lexer->next_token;
775 }
776
777 /* Now, read tokens until we have enough. */
778 while (--n > 0)
779 {
780 /* Advance to the next token. */
781 token = cp_lexer_next_token (lexer, token);
782 /* If that's all the tokens we have, read a new one. */
783 if (token == lexer->last_token)
784 token = cp_lexer_read_token (lexer);
785 }
786
787 return token;
788}
789
790/* Consume the next token. The pointer returned is valid only until
791 another token is read. Callers should preserve copy the token
792 explicitly if they will need its value for a longer period of
793 time. */
794
795static cp_token *
796cp_lexer_consume_token (lexer)
797 cp_lexer *lexer;
798{
799 cp_token *token;
800
801 /* If there are no tokens, read one now. */
802 if (!lexer->next_token)
803 cp_lexer_read_token (lexer);
804
805 /* Remember the token we'll be returning. */
806 token = lexer->next_token;
807
808 /* Increment NEXT_TOKEN. */
809 lexer->next_token = cp_lexer_next_token (lexer,
810 lexer->next_token);
811 /* Check to see if we're all out of tokens. */
812 if (lexer->next_token == lexer->last_token)
813 lexer->next_token = NULL;
814
815 /* If we're not saving tokens, then move FIRST_TOKEN too. */
816 if (!cp_lexer_saving_tokens (lexer))
817 {
818 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
819 if (!lexer->next_token)
820 lexer->first_token = NULL;
821 else
822 lexer->first_token = lexer->next_token;
823 }
824
825 /* Provide debugging output. */
826 if (cp_lexer_debugging_p (lexer))
827 {
828 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
829 cp_lexer_print_token (cp_lexer_debug_stream, token);
830 fprintf (cp_lexer_debug_stream, "\n");
831 }
832
833 return token;
834}
835
836/* Permanently remove the next token from the token stream. There
837 must be a valid next token already; this token never reads
838 additional tokens from the preprocessor. */
839
840static void
841cp_lexer_purge_token (cp_lexer *lexer)
842{
843 cp_token *token;
844 cp_token *next_token;
845
846 token = lexer->next_token;
847 while (true)
848 {
849 next_token = cp_lexer_next_token (lexer, token);
850 if (next_token == lexer->last_token)
851 break;
852 *token = *next_token;
853 token = next_token;
854 }
855
856 lexer->last_token = token;
857 /* The token purged may have been the only token remaining; if so,
858 clear NEXT_TOKEN. */
859 if (lexer->next_token == token)
860 lexer->next_token = NULL;
861}
862
863/* Permanently remove all tokens after TOKEN, up to, but not
864 including, the token that will be returned next by
865 cp_lexer_peek_token. */
866
867static void
868cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
869{
870 cp_token *peek;
871 cp_token *t1;
872 cp_token *t2;
873
874 if (lexer->next_token)
875 {
876 /* Copy the tokens that have not yet been read to the location
877 immediately following TOKEN. */
878 t1 = cp_lexer_next_token (lexer, token);
879 t2 = peek = cp_lexer_peek_token (lexer);
880 /* Move tokens into the vacant area between TOKEN and PEEK. */
881 while (t2 != lexer->last_token)
882 {
883 *t1 = *t2;
884 t1 = cp_lexer_next_token (lexer, t1);
885 t2 = cp_lexer_next_token (lexer, t2);
886 }
887 /* Now, the next available token is right after TOKEN. */
888 lexer->next_token = cp_lexer_next_token (lexer, token);
889 /* And the last token is wherever we ended up. */
890 lexer->last_token = t1;
891 }
892 else
893 {
894 /* There are no tokens in the buffer, so there is nothing to
895 copy. The last token in the buffer is TOKEN itself. */
896 lexer->last_token = cp_lexer_next_token (lexer, token);
897 }
898}
899
900/* Begin saving tokens. All tokens consumed after this point will be
901 preserved. */
902
903static void
904cp_lexer_save_tokens (lexer)
905 cp_lexer *lexer;
906{
907 /* Provide debugging output. */
908 if (cp_lexer_debugging_p (lexer))
909 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
910
911 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
912 restore the tokens if required. */
913 if (!lexer->next_token)
914 cp_lexer_read_token (lexer);
915
916 VARRAY_PUSH_INT (lexer->saved_tokens,
917 cp_lexer_token_difference (lexer,
918 lexer->first_token,
919 lexer->next_token));
920}
921
922/* Commit to the portion of the token stream most recently saved. */
923
924static void
925cp_lexer_commit_tokens (lexer)
926 cp_lexer *lexer;
927{
928 /* Provide debugging output. */
929 if (cp_lexer_debugging_p (lexer))
930 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
931
932 VARRAY_POP (lexer->saved_tokens);
933}
934
935/* Return all tokens saved since the last call to cp_lexer_save_tokens
936 to the token stream. Stop saving tokens. */
937
938static void
939cp_lexer_rollback_tokens (lexer)
940 cp_lexer *lexer;
941{
942 size_t delta;
943
944 /* Provide debugging output. */
945 if (cp_lexer_debugging_p (lexer))
946 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
947
948 /* Find the token that was the NEXT_TOKEN when we started saving
949 tokens. */
950 delta = VARRAY_TOP_INT(lexer->saved_tokens);
951 /* Make it the next token again now. */
952 lexer->next_token = cp_lexer_advance_token (lexer,
953 lexer->first_token,
954 delta);
15d2cb19 955 /* It might be the case that there were no tokens when we started
a723baf1
MM
956 saving tokens, but that there are some tokens now. */
957 if (!lexer->next_token && lexer->first_token)
958 lexer->next_token = lexer->first_token;
959
960 /* Stop saving tokens. */
961 VARRAY_POP (lexer->saved_tokens);
962}
963
a723baf1
MM
964/* Print a representation of the TOKEN on the STREAM. */
965
966static void
967cp_lexer_print_token (stream, token)
968 FILE *stream;
969 cp_token *token;
970{
971 const char *token_type = NULL;
972
973 /* Figure out what kind of token this is. */
974 switch (token->type)
975 {
976 case CPP_EQ:
977 token_type = "EQ";
978 break;
979
980 case CPP_COMMA:
981 token_type = "COMMA";
982 break;
983
984 case CPP_OPEN_PAREN:
985 token_type = "OPEN_PAREN";
986 break;
987
988 case CPP_CLOSE_PAREN:
989 token_type = "CLOSE_PAREN";
990 break;
991
992 case CPP_OPEN_BRACE:
993 token_type = "OPEN_BRACE";
994 break;
995
996 case CPP_CLOSE_BRACE:
997 token_type = "CLOSE_BRACE";
998 break;
999
1000 case CPP_SEMICOLON:
1001 token_type = "SEMICOLON";
1002 break;
1003
1004 case CPP_NAME:
1005 token_type = "NAME";
1006 break;
1007
1008 case CPP_EOF:
1009 token_type = "EOF";
1010 break;
1011
1012 case CPP_KEYWORD:
1013 token_type = "keyword";
1014 break;
1015
1016 /* This is not a token that we know how to handle yet. */
1017 default:
1018 break;
1019 }
1020
1021 /* If we have a name for the token, print it out. Otherwise, we
1022 simply give the numeric code. */
1023 if (token_type)
1024 fprintf (stream, "%s", token_type);
1025 else
1026 fprintf (stream, "%d", token->type);
1027 /* And, for an identifier, print the identifier name. */
1028 if (token->type == CPP_NAME
1029 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1030 For example, `struct' is mapped to an INTEGER_CST. */
1031 || (token->type == CPP_KEYWORD
1032 && TREE_CODE (token->value) == IDENTIFIER_NODE))
1033 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1034}
1035
a723baf1
MM
1036/* Start emitting debugging information. */
1037
1038static void
1039cp_lexer_start_debugging (lexer)
1040 cp_lexer *lexer;
1041{
1042 ++lexer->debugging_p;
1043}
1044
1045/* Stop emitting debugging information. */
1046
1047static void
1048cp_lexer_stop_debugging (lexer)
1049 cp_lexer *lexer;
1050{
1051 --lexer->debugging_p;
1052}
1053
1054\f
1055/* The parser. */
1056
1057/* Overview
1058 --------
1059
1060 A cp_parser parses the token stream as specified by the C++
1061 grammar. Its job is purely parsing, not semantic analysis. For
1062 example, the parser breaks the token stream into declarators,
1063 expressions, statements, and other similar syntactic constructs.
1064 It does not check that the types of the expressions on either side
1065 of an assignment-statement are compatible, or that a function is
1066 not declared with a parameter of type `void'.
1067
1068 The parser invokes routines elsewhere in the compiler to perform
1069 semantic analysis and to build up the abstract syntax tree for the
1070 code processed.
1071
1072 The parser (and the template instantiation code, which is, in a
1073 way, a close relative of parsing) are the only parts of the
1074 compiler that should be calling push_scope and pop_scope, or
1075 related functions. The parser (and template instantiation code)
1076 keeps track of what scope is presently active; everything else
1077 should simply honor that. (The code that generates static
1078 initializers may also need to set the scope, in order to check
1079 access control correctly when emitting the initializers.)
1080
1081 Methodology
1082 -----------
1083
1084 The parser is of the standard recursive-descent variety. Upcoming
1085 tokens in the token stream are examined in order to determine which
1086 production to use when parsing a non-terminal. Some C++ constructs
1087 require arbitrary look ahead to disambiguate. For example, it is
1088 impossible, in the general case, to tell whether a statement is an
1089 expression or declaration without scanning the entire statement.
1090 Therefore, the parser is capable of "parsing tentatively." When the
1091 parser is not sure what construct comes next, it enters this mode.
1092 Then, while we attempt to parse the construct, the parser queues up
1093 error messages, rather than issuing them immediately, and saves the
1094 tokens it consumes. If the construct is parsed successfully, the
1095 parser "commits", i.e., it issues any queued error messages and
1096 the tokens that were being preserved are permanently discarded.
1097 If, however, the construct is not parsed successfully, the parser
1098 rolls back its state completely so that it can resume parsing using
1099 a different alternative.
1100
1101 Future Improvements
1102 -------------------
1103
1104 The performance of the parser could probably be improved
1105 substantially. Some possible improvements include:
1106
1107 - The expression parser recurses through the various levels of
1108 precedence as specified in the grammar, rather than using an
1109 operator-precedence technique. Therefore, parsing a simple
1110 identifier requires multiple recursive calls.
1111
1112 - We could often eliminate the need to parse tentatively by
1113 looking ahead a little bit. In some places, this approach
1114 might not entirely eliminate the need to parse tentatively, but
1115 it might still speed up the average case. */
1116
1117/* Flags that are passed to some parsing functions. These values can
1118 be bitwise-ored together. */
1119
1120typedef enum cp_parser_flags
1121{
1122 /* No flags. */
1123 CP_PARSER_FLAGS_NONE = 0x0,
1124 /* The construct is optional. If it is not present, then no error
1125 should be issued. */
1126 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1127 /* When parsing a type-specifier, do not allow user-defined types. */
1128 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1129} cp_parser_flags;
1130
1131/* The different kinds of ids that we ecounter. */
1132
1133typedef enum cp_parser_id_kind
1134{
1135 /* Not an id at all. */
1136 CP_PARSER_ID_KIND_NONE,
1137 /* An unqualified-id that is not a template-id. */
1138 CP_PARSER_ID_KIND_UNQUALIFIED,
1139 /* An unqualified template-id. */
1140 CP_PARSER_ID_KIND_TEMPLATE_ID,
1141 /* A qualified-id. */
1142 CP_PARSER_ID_KIND_QUALIFIED
1143} cp_parser_id_kind;
1144
62b8a44e
NS
1145/* The different kinds of declarators we want to parse. */
1146
1147typedef enum cp_parser_declarator_kind
1148{
1149 /* We want an abstract declartor. */
1150 CP_PARSER_DECLARATOR_ABSTRACT,
1151 /* We want a named declarator. */
1152 CP_PARSER_DECLARATOR_NAMED,
1153 /* We don't mind. */
1154 CP_PARSER_DECLARATOR_EITHER
1155} cp_parser_declarator_kind;
1156
a723baf1
MM
1157/* A mapping from a token type to a corresponding tree node type. */
1158
1159typedef struct cp_parser_token_tree_map_node
1160{
1161 /* The token type. */
1162 enum cpp_ttype token_type;
1163 /* The corresponding tree code. */
1164 enum tree_code tree_type;
1165} cp_parser_token_tree_map_node;
1166
1167/* A complete map consists of several ordinary entries, followed by a
1168 terminator. The terminating entry has a token_type of CPP_EOF. */
1169
1170typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1171
1172/* The status of a tentative parse. */
1173
1174typedef enum cp_parser_status_kind
1175{
1176 /* No errors have occurred. */
1177 CP_PARSER_STATUS_KIND_NO_ERROR,
1178 /* An error has occurred. */
1179 CP_PARSER_STATUS_KIND_ERROR,
1180 /* We are committed to this tentative parse, whether or not an error
1181 has occurred. */
1182 CP_PARSER_STATUS_KIND_COMMITTED
1183} cp_parser_status_kind;
1184
1185/* Context that is saved and restored when parsing tentatively. */
1186
1187typedef struct cp_parser_context GTY (())
1188{
1189 /* If this is a tentative parsing context, the status of the
1190 tentative parse. */
1191 enum cp_parser_status_kind status;
1192 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1193 that are looked up in this context must be looked up both in the
1194 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1195 the context of the containing expression. */
1196 tree object_type;
a723baf1
MM
1197 /* The next parsing context in the stack. */
1198 struct cp_parser_context *next;
1199} cp_parser_context;
1200
1201/* Prototypes. */
1202
1203/* Constructors and destructors. */
1204
1205static cp_parser_context *cp_parser_context_new
1206 PARAMS ((cp_parser_context *));
1207
e5976695
MM
1208/* Class variables. */
1209
92bc1323 1210static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
e5976695 1211
a723baf1
MM
1212/* Constructors and destructors. */
1213
1214/* Construct a new context. The context below this one on the stack
1215 is given by NEXT. */
1216
1217static cp_parser_context *
1218cp_parser_context_new (next)
1219 cp_parser_context *next;
1220{
1221 cp_parser_context *context;
1222
1223 /* Allocate the storage. */
e5976695
MM
1224 if (cp_parser_context_free_list != NULL)
1225 {
1226 /* Pull the first entry from the free list. */
1227 context = cp_parser_context_free_list;
1228 cp_parser_context_free_list = context->next;
1229 memset ((char *)context, 0, sizeof (*context));
1230 }
1231 else
1232 context = ((cp_parser_context *)
1233 ggc_alloc_cleared (sizeof (cp_parser_context)));
a723baf1
MM
1234 /* No errors have occurred yet in this context. */
1235 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1236 /* If this is not the bottomost context, copy information that we
1237 need from the previous context. */
1238 if (next)
1239 {
1240 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1241 expression, then we are parsing one in this context, too. */
1242 context->object_type = next->object_type;
a723baf1
MM
1243 /* Thread the stack. */
1244 context->next = next;
1245 }
1246
1247 return context;
1248}
1249
1250/* The cp_parser structure represents the C++ parser. */
1251
1252typedef struct cp_parser GTY(())
1253{
1254 /* The lexer from which we are obtaining tokens. */
1255 cp_lexer *lexer;
1256
1257 /* The scope in which names should be looked up. If NULL_TREE, then
1258 we look up names in the scope that is currently open in the
1259 source program. If non-NULL, this is either a TYPE or
1260 NAMESPACE_DECL for the scope in which we should look.
1261
1262 This value is not cleared automatically after a name is looked
1263 up, so we must be careful to clear it before starting a new look
1264 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1265 will look up `Z' in the scope of `X', rather than the current
1266 scope.) Unfortunately, it is difficult to tell when name lookup
1267 is complete, because we sometimes peek at a token, look it up,
1268 and then decide not to consume it. */
1269 tree scope;
1270
1271 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1272 last lookup took place. OBJECT_SCOPE is used if an expression
1273 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1274 respectively. QUALIFYING_SCOPE is used for an expression of the
1275 form "X::Y"; it refers to X. */
1276 tree object_scope;
1277 tree qualifying_scope;
1278
1279 /* A stack of parsing contexts. All but the bottom entry on the
1280 stack will be tentative contexts.
1281
1282 We parse tentatively in order to determine which construct is in
1283 use in some situations. For example, in order to determine
1284 whether a statement is an expression-statement or a
1285 declaration-statement we parse it tentatively as a
1286 declaration-statement. If that fails, we then reparse the same
1287 token stream as an expression-statement. */
1288 cp_parser_context *context;
1289
1290 /* True if we are parsing GNU C++. If this flag is not set, then
1291 GNU extensions are not recognized. */
1292 bool allow_gnu_extensions_p;
1293
1294 /* TRUE if the `>' token should be interpreted as the greater-than
1295 operator. FALSE if it is the end of a template-id or
1296 template-parameter-list. */
1297 bool greater_than_is_operator_p;
1298
1299 /* TRUE if default arguments are allowed within a parameter list
1300 that starts at this point. FALSE if only a gnu extension makes
1301 them permissable. */
1302 bool default_arg_ok_p;
1303
1304 /* TRUE if we are parsing an integral constant-expression. See
1305 [expr.const] for a precise definition. */
1306 /* FIXME: Need to implement code that checks this flag. */
1307 bool constant_expression_p;
1308
1309 /* TRUE if local variable names and `this' are forbidden in the
1310 current context. */
1311 bool local_variables_forbidden_p;
1312
1313 /* TRUE if the declaration we are parsing is part of a
1314 linkage-specification of the form `extern string-literal
1315 declaration'. */
1316 bool in_unbraced_linkage_specification_p;
1317
1318 /* TRUE if we are presently parsing a declarator, after the
1319 direct-declarator. */
1320 bool in_declarator_p;
1321
1322 /* If non-NULL, then we are parsing a construct where new type
1323 definitions are not permitted. The string stored here will be
1324 issued as an error message if a type is defined. */
1325 const char *type_definition_forbidden_message;
1326
a723baf1
MM
1327 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1328 but may not have been parsed. These functions are friends of
1329 members defined within a class-specification; they are not
1330 procssed until the class is complete. The active queue is at the
1331 front of the list.
1332
1333 Within each queue, functions appear in the reverse order that
8218bd34
MM
1334 they appeared in the source. Each TREE_VALUE is a
1335 FUNCTION_DECL of TEMPLATE_DECL corresponding to a member
1336 function. */
a723baf1
MM
1337 tree unparsed_functions_queues;
1338
1339 /* The number of classes whose definitions are currently in
1340 progress. */
1341 unsigned num_classes_being_defined;
1342
1343 /* The number of template parameter lists that apply directly to the
1344 current declaration. */
1345 unsigned num_template_parameter_lists;
1346} cp_parser;
1347
1348/* The type of a function that parses some kind of expression */
1349typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1350
1351/* Prototypes. */
1352
1353/* Constructors and destructors. */
1354
1355static cp_parser *cp_parser_new
1356 PARAMS ((void));
1357
1358/* Routines to parse various constructs.
1359
1360 Those that return `tree' will return the error_mark_node (rather
1361 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1362 Sometimes, they will return an ordinary node if error-recovery was
1363 attempted, even though a parse error occurrred. So, to check
1364 whether or not a parse error occurred, you should always use
1365 cp_parser_error_occurred. If the construct is optional (indicated
1366 either by an `_opt' in the name of the function that does the
1367 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1368 the construct is not present. */
1369
1370/* Lexical conventions [gram.lex] */
1371
1372static tree cp_parser_identifier
1373 PARAMS ((cp_parser *));
1374
1375/* Basic concepts [gram.basic] */
1376
1377static bool cp_parser_translation_unit
1378 PARAMS ((cp_parser *));
1379
1380/* Expressions [gram.expr] */
1381
1382static tree cp_parser_primary_expression
1383 (cp_parser *, cp_parser_id_kind *, tree *);
1384static tree cp_parser_id_expression
1385 PARAMS ((cp_parser *, bool, bool, bool *));
1386static tree cp_parser_unqualified_id
1387 PARAMS ((cp_parser *, bool, bool));
1388static tree cp_parser_nested_name_specifier_opt
1389 (cp_parser *, bool, bool, bool);
1390static tree cp_parser_nested_name_specifier
1391 (cp_parser *, bool, bool, bool);
1392static tree cp_parser_class_or_namespace_name
1393 (cp_parser *, bool, bool, bool, bool);
1394static tree cp_parser_postfix_expression
1395 (cp_parser *, bool);
1396static tree cp_parser_expression_list
1397 PARAMS ((cp_parser *));
1398static void cp_parser_pseudo_destructor_name
1399 PARAMS ((cp_parser *, tree *, tree *));
1400static tree cp_parser_unary_expression
1401 (cp_parser *, bool);
1402static enum tree_code cp_parser_unary_operator
1403 PARAMS ((cp_token *));
1404static tree cp_parser_new_expression
1405 PARAMS ((cp_parser *));
1406static tree cp_parser_new_placement
1407 PARAMS ((cp_parser *));
1408static tree cp_parser_new_type_id
1409 PARAMS ((cp_parser *));
1410static tree cp_parser_new_declarator_opt
1411 PARAMS ((cp_parser *));
1412static tree cp_parser_direct_new_declarator
1413 PARAMS ((cp_parser *));
1414static tree cp_parser_new_initializer
1415 PARAMS ((cp_parser *));
1416static tree cp_parser_delete_expression
1417 PARAMS ((cp_parser *));
1418static tree cp_parser_cast_expression
1419 (cp_parser *, bool);
1420static tree cp_parser_pm_expression
1421 PARAMS ((cp_parser *));
1422static tree cp_parser_multiplicative_expression
1423 PARAMS ((cp_parser *));
1424static tree cp_parser_additive_expression
1425 PARAMS ((cp_parser *));
1426static tree cp_parser_shift_expression
1427 PARAMS ((cp_parser *));
1428static tree cp_parser_relational_expression
1429 PARAMS ((cp_parser *));
1430static tree cp_parser_equality_expression
1431 PARAMS ((cp_parser *));
1432static tree cp_parser_and_expression
1433 PARAMS ((cp_parser *));
1434static tree cp_parser_exclusive_or_expression
1435 PARAMS ((cp_parser *));
1436static tree cp_parser_inclusive_or_expression
1437 PARAMS ((cp_parser *));
1438static tree cp_parser_logical_and_expression
1439 PARAMS ((cp_parser *));
1440static tree cp_parser_logical_or_expression
1441 PARAMS ((cp_parser *));
1442static tree cp_parser_conditional_expression
1443 PARAMS ((cp_parser *));
1444static tree cp_parser_question_colon_clause
1445 PARAMS ((cp_parser *, tree));
1446static tree cp_parser_assignment_expression
1447 PARAMS ((cp_parser *));
1448static enum tree_code cp_parser_assignment_operator_opt
1449 PARAMS ((cp_parser *));
1450static tree cp_parser_expression
1451 PARAMS ((cp_parser *));
1452static tree cp_parser_constant_expression
1453 PARAMS ((cp_parser *));
1454
1455/* Statements [gram.stmt.stmt] */
1456
1457static void cp_parser_statement
1458 PARAMS ((cp_parser *));
1459static tree cp_parser_labeled_statement
1460 PARAMS ((cp_parser *));
1461static tree cp_parser_expression_statement
1462 PARAMS ((cp_parser *));
1463static tree cp_parser_compound_statement
1464 (cp_parser *);
1465static void cp_parser_statement_seq_opt
1466 PARAMS ((cp_parser *));
1467static tree cp_parser_selection_statement
1468 PARAMS ((cp_parser *));
1469static tree cp_parser_condition
1470 PARAMS ((cp_parser *));
1471static tree cp_parser_iteration_statement
1472 PARAMS ((cp_parser *));
1473static void cp_parser_for_init_statement
1474 PARAMS ((cp_parser *));
1475static tree cp_parser_jump_statement
1476 PARAMS ((cp_parser *));
1477static void cp_parser_declaration_statement
1478 PARAMS ((cp_parser *));
1479
1480static tree cp_parser_implicitly_scoped_statement
1481 PARAMS ((cp_parser *));
1482static void cp_parser_already_scoped_statement
1483 PARAMS ((cp_parser *));
1484
1485/* Declarations [gram.dcl.dcl] */
1486
1487static void cp_parser_declaration_seq_opt
1488 PARAMS ((cp_parser *));
1489static void cp_parser_declaration
1490 PARAMS ((cp_parser *));
1491static void cp_parser_block_declaration
1492 PARAMS ((cp_parser *, bool));
1493static void cp_parser_simple_declaration
1494 PARAMS ((cp_parser *, bool));
1495static tree cp_parser_decl_specifier_seq
1496 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1497static tree cp_parser_storage_class_specifier_opt
1498 PARAMS ((cp_parser *));
1499static tree cp_parser_function_specifier_opt
1500 PARAMS ((cp_parser *));
1501static tree cp_parser_type_specifier
1502 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1503static tree cp_parser_simple_type_specifier
1504 PARAMS ((cp_parser *, cp_parser_flags));
1505static tree cp_parser_type_name
1506 PARAMS ((cp_parser *));
1507static tree cp_parser_elaborated_type_specifier
1508 PARAMS ((cp_parser *, bool, bool));
1509static tree cp_parser_enum_specifier
1510 PARAMS ((cp_parser *));
1511static void cp_parser_enumerator_list
1512 PARAMS ((cp_parser *, tree));
1513static void cp_parser_enumerator_definition
1514 PARAMS ((cp_parser *, tree));
1515static tree cp_parser_namespace_name
1516 PARAMS ((cp_parser *));
1517static void cp_parser_namespace_definition
1518 PARAMS ((cp_parser *));
1519static void cp_parser_namespace_body
1520 PARAMS ((cp_parser *));
1521static tree cp_parser_qualified_namespace_specifier
1522 PARAMS ((cp_parser *));
1523static void cp_parser_namespace_alias_definition
1524 PARAMS ((cp_parser *));
1525static void cp_parser_using_declaration
1526 PARAMS ((cp_parser *));
1527static void cp_parser_using_directive
1528 PARAMS ((cp_parser *));
1529static void cp_parser_asm_definition
1530 PARAMS ((cp_parser *));
1531static void cp_parser_linkage_specification
1532 PARAMS ((cp_parser *));
1533
1534/* Declarators [gram.dcl.decl] */
1535
1536static tree cp_parser_init_declarator
cf22909c 1537 PARAMS ((cp_parser *, tree, tree, bool, bool, bool *));
a723baf1 1538static tree cp_parser_declarator
62b8a44e 1539 PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
a723baf1 1540static tree cp_parser_direct_declarator
62b8a44e 1541 PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
a723baf1
MM
1542static enum tree_code cp_parser_ptr_operator
1543 PARAMS ((cp_parser *, tree *, tree *));
1544static tree cp_parser_cv_qualifier_seq_opt
1545 PARAMS ((cp_parser *));
1546static tree cp_parser_cv_qualifier_opt
1547 PARAMS ((cp_parser *));
1548static tree cp_parser_declarator_id
1549 PARAMS ((cp_parser *));
1550static tree cp_parser_type_id
1551 PARAMS ((cp_parser *));
1552static tree cp_parser_type_specifier_seq
1553 PARAMS ((cp_parser *));
1554static tree cp_parser_parameter_declaration_clause
1555 PARAMS ((cp_parser *));
1556static tree cp_parser_parameter_declaration_list
1557 PARAMS ((cp_parser *));
1558static tree cp_parser_parameter_declaration
1559 PARAMS ((cp_parser *, bool));
1560static tree cp_parser_function_definition
1561 PARAMS ((cp_parser *, bool *));
1562static void cp_parser_function_body
1563 (cp_parser *);
1564static tree cp_parser_initializer
1565 PARAMS ((cp_parser *, bool *));
1566static tree cp_parser_initializer_clause
1567 PARAMS ((cp_parser *));
1568static tree cp_parser_initializer_list
1569 PARAMS ((cp_parser *));
1570
1571static bool cp_parser_ctor_initializer_opt_and_function_body
1572 (cp_parser *);
1573
1574/* Classes [gram.class] */
1575
1576static tree cp_parser_class_name
1577 (cp_parser *, bool, bool, bool, bool, bool, bool);
1578static tree cp_parser_class_specifier
1579 PARAMS ((cp_parser *));
1580static tree cp_parser_class_head
cf22909c 1581 PARAMS ((cp_parser *, bool *));
a723baf1
MM
1582static enum tag_types cp_parser_class_key
1583 PARAMS ((cp_parser *));
1584static void cp_parser_member_specification_opt
1585 PARAMS ((cp_parser *));
1586static void cp_parser_member_declaration
1587 PARAMS ((cp_parser *));
1588static tree cp_parser_pure_specifier
1589 PARAMS ((cp_parser *));
1590static tree cp_parser_constant_initializer
1591 PARAMS ((cp_parser *));
1592
1593/* Derived classes [gram.class.derived] */
1594
1595static tree cp_parser_base_clause
1596 PARAMS ((cp_parser *));
1597static tree cp_parser_base_specifier
1598 PARAMS ((cp_parser *));
1599
1600/* Special member functions [gram.special] */
1601
1602static tree cp_parser_conversion_function_id
1603 PARAMS ((cp_parser *));
1604static tree cp_parser_conversion_type_id
1605 PARAMS ((cp_parser *));
1606static tree cp_parser_conversion_declarator_opt
1607 PARAMS ((cp_parser *));
1608static bool cp_parser_ctor_initializer_opt
1609 PARAMS ((cp_parser *));
1610static void cp_parser_mem_initializer_list
1611 PARAMS ((cp_parser *));
1612static tree cp_parser_mem_initializer
1613 PARAMS ((cp_parser *));
1614static tree cp_parser_mem_initializer_id
1615 PARAMS ((cp_parser *));
1616
1617/* Overloading [gram.over] */
1618
1619static tree cp_parser_operator_function_id
1620 PARAMS ((cp_parser *));
1621static tree cp_parser_operator
1622 PARAMS ((cp_parser *));
1623
1624/* Templates [gram.temp] */
1625
1626static void cp_parser_template_declaration
1627 PARAMS ((cp_parser *, bool));
1628static tree cp_parser_template_parameter_list
1629 PARAMS ((cp_parser *));
1630static tree cp_parser_template_parameter
1631 PARAMS ((cp_parser *));
1632static tree cp_parser_type_parameter
1633 PARAMS ((cp_parser *));
1634static tree cp_parser_template_id
1635 PARAMS ((cp_parser *, bool, bool));
1636static tree cp_parser_template_name
1637 PARAMS ((cp_parser *, bool, bool));
1638static tree cp_parser_template_argument_list
1639 PARAMS ((cp_parser *));
1640static tree cp_parser_template_argument
1641 PARAMS ((cp_parser *));
1642static void cp_parser_explicit_instantiation
1643 PARAMS ((cp_parser *));
1644static void cp_parser_explicit_specialization
1645 PARAMS ((cp_parser *));
1646
1647/* Exception handling [gram.exception] */
1648
1649static tree cp_parser_try_block
1650 PARAMS ((cp_parser *));
1651static bool cp_parser_function_try_block
1652 PARAMS ((cp_parser *));
1653static void cp_parser_handler_seq
1654 PARAMS ((cp_parser *));
1655static void cp_parser_handler
1656 PARAMS ((cp_parser *));
1657static tree cp_parser_exception_declaration
1658 PARAMS ((cp_parser *));
1659static tree cp_parser_throw_expression
1660 PARAMS ((cp_parser *));
1661static tree cp_parser_exception_specification_opt
1662 PARAMS ((cp_parser *));
1663static tree cp_parser_type_id_list
1664 PARAMS ((cp_parser *));
1665
1666/* GNU Extensions */
1667
1668static tree cp_parser_asm_specification_opt
1669 PARAMS ((cp_parser *));
1670static tree cp_parser_asm_operand_list
1671 PARAMS ((cp_parser *));
1672static tree cp_parser_asm_clobber_list
1673 PARAMS ((cp_parser *));
1674static tree cp_parser_attributes_opt
1675 PARAMS ((cp_parser *));
1676static tree cp_parser_attribute_list
1677 PARAMS ((cp_parser *));
1678static bool cp_parser_extension_opt
1679 PARAMS ((cp_parser *, int *));
1680static void cp_parser_label_declaration
1681 PARAMS ((cp_parser *));
1682
1683/* Utility Routines */
1684
1685static tree cp_parser_lookup_name
eea9800f 1686 PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
a723baf1
MM
1687static tree cp_parser_lookup_name_simple
1688 PARAMS ((cp_parser *, tree));
1689static tree cp_parser_resolve_typename_type
1690 PARAMS ((cp_parser *, tree));
1691static tree cp_parser_maybe_treat_template_as_class
1692 (tree, bool);
1693static bool cp_parser_check_declarator_template_parameters
1694 PARAMS ((cp_parser *, tree));
1695static bool cp_parser_check_template_parameters
1696 PARAMS ((cp_parser *, unsigned));
1697static tree cp_parser_binary_expression
1698 PARAMS ((cp_parser *,
39b1af70 1699 const cp_parser_token_tree_map,
a723baf1
MM
1700 cp_parser_expression_fn));
1701static tree cp_parser_global_scope_opt
1702 PARAMS ((cp_parser *, bool));
1703static bool cp_parser_constructor_declarator_p
1704 (cp_parser *, bool);
1705static tree cp_parser_function_definition_from_specifiers_and_declarator
cf22909c 1706 PARAMS ((cp_parser *, tree, tree, tree));
a723baf1
MM
1707static tree cp_parser_function_definition_after_declarator
1708 PARAMS ((cp_parser *, bool));
1709static void cp_parser_template_declaration_after_export
1710 PARAMS ((cp_parser *, bool));
1711static tree cp_parser_single_declaration
1712 PARAMS ((cp_parser *, bool, bool *));
1713static tree cp_parser_functional_cast
1714 PARAMS ((cp_parser *, tree));
1715static void cp_parser_late_parsing_for_member
1716 PARAMS ((cp_parser *, tree));
1717static void cp_parser_late_parsing_default_args
8218bd34 1718 (cp_parser *, tree);
a723baf1
MM
1719static tree cp_parser_sizeof_operand
1720 PARAMS ((cp_parser *, enum rid));
1721static bool cp_parser_declares_only_class_p
1722 PARAMS ((cp_parser *));
1723static bool cp_parser_friend_p
1724 PARAMS ((tree));
1725static cp_token *cp_parser_require
1726 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1727static cp_token *cp_parser_require_keyword
1728 PARAMS ((cp_parser *, enum rid, const char *));
1729static bool cp_parser_token_starts_function_definition_p
1730 PARAMS ((cp_token *));
1731static bool cp_parser_next_token_starts_class_definition_p
1732 (cp_parser *);
1733static enum tag_types cp_parser_token_is_class_key
1734 PARAMS ((cp_token *));
1735static void cp_parser_check_class_key
1736 (enum tag_types, tree type);
1737static bool cp_parser_optional_template_keyword
1738 (cp_parser *);
2050a1bb
MM
1739static void cp_parser_pre_parsed_nested_name_specifier
1740 (cp_parser *);
a723baf1
MM
1741static void cp_parser_cache_group
1742 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1743static void cp_parser_parse_tentatively
1744 PARAMS ((cp_parser *));
1745static void cp_parser_commit_to_tentative_parse
1746 PARAMS ((cp_parser *));
1747static void cp_parser_abort_tentative_parse
1748 PARAMS ((cp_parser *));
1749static bool cp_parser_parse_definitely
1750 PARAMS ((cp_parser *));
f7b5ecd9 1751static inline bool cp_parser_parsing_tentatively
a723baf1
MM
1752 PARAMS ((cp_parser *));
1753static bool cp_parser_committed_to_tentative_parse
1754 PARAMS ((cp_parser *));
1755static void cp_parser_error
1756 PARAMS ((cp_parser *, const char *));
e5976695 1757static bool cp_parser_simulate_error
a723baf1
MM
1758 PARAMS ((cp_parser *));
1759static void cp_parser_check_type_definition
1760 PARAMS ((cp_parser *));
1761static bool cp_parser_skip_to_closing_parenthesis
1762 PARAMS ((cp_parser *));
1763static bool cp_parser_skip_to_closing_parenthesis_or_comma
1764 (cp_parser *);
1765static void cp_parser_skip_to_end_of_statement
1766 PARAMS ((cp_parser *));
1767static void cp_parser_skip_to_end_of_block_or_statement
1768 PARAMS ((cp_parser *));
1769static void cp_parser_skip_to_closing_brace
1770 (cp_parser *);
1771static void cp_parser_skip_until_found
1772 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1773static bool cp_parser_error_occurred
1774 PARAMS ((cp_parser *));
1775static bool cp_parser_allow_gnu_extensions_p
1776 PARAMS ((cp_parser *));
1777static bool cp_parser_is_string_literal
1778 PARAMS ((cp_token *));
1779static bool cp_parser_is_keyword
1780 PARAMS ((cp_token *, enum rid));
1781static bool cp_parser_dependent_type_p
1782 (tree);
1783static bool cp_parser_value_dependent_expression_p
1784 (tree);
1785static bool cp_parser_type_dependent_expression_p
1786 (tree);
1787static bool cp_parser_dependent_template_arg_p
1788 (tree);
1789static bool cp_parser_dependent_template_id_p
1790 (tree, tree);
1791static bool cp_parser_dependent_template_p
1792 (tree);
a723baf1
MM
1793static tree cp_parser_scope_through_which_access_occurs
1794 (tree, tree, tree);
1795
f7b5ecd9
MM
1796/* Returns non-zero if we are parsing tentatively. */
1797
1798static inline bool
1799cp_parser_parsing_tentatively (parser)
1800 cp_parser *parser;
1801{
1802 return parser->context->next != NULL;
1803}
1804
a723baf1
MM
1805/* Returns non-zero if TOKEN is a string literal. */
1806
1807static bool
1808cp_parser_is_string_literal (token)
1809 cp_token *token;
1810{
1811 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1812}
1813
1814/* Returns non-zero if TOKEN is the indicated KEYWORD. */
1815
1816static bool
1817cp_parser_is_keyword (token, keyword)
1818 cp_token *token;
1819 enum rid keyword;
1820{
1821 return token->keyword == keyword;
1822}
1823
1824/* Returns TRUE if TYPE is dependent, in the sense of
1825 [temp.dep.type]. */
1826
1827static bool
1828cp_parser_dependent_type_p (type)
1829 tree type;
1830{
1831 tree scope;
1832
1833 if (!processing_template_decl)
1834 return false;
1835
1836 /* If the type is NULL, we have not computed a type for the entity
1837 in question; in that case, the type is dependent. */
1838 if (!type)
1839 return true;
1840
1841 /* Erroneous types can be considered non-dependent. */
1842 if (type == error_mark_node)
1843 return false;
1844
1845 /* [temp.dep.type]
1846
1847 A type is dependent if it is:
1848
1849 -- a template parameter. */
1850 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1851 return true;
1852 /* -- a qualified-id with a nested-name-specifier which contains a
1853 class-name that names a dependent type or whose unqualified-id
1854 names a dependent type. */
1855 if (TREE_CODE (type) == TYPENAME_TYPE)
1856 return true;
1857 /* -- a cv-qualified type where the cv-unqualified type is
1858 dependent. */
1859 type = TYPE_MAIN_VARIANT (type);
1860 /* -- a compound type constructed from any dependent type. */
1861 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1862 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1863 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1864 (type)));
1865 else if (TREE_CODE (type) == POINTER_TYPE
1866 || TREE_CODE (type) == REFERENCE_TYPE)
1867 return cp_parser_dependent_type_p (TREE_TYPE (type));
1868 else if (TREE_CODE (type) == FUNCTION_TYPE
1869 || TREE_CODE (type) == METHOD_TYPE)
1870 {
1871 tree arg_type;
1872
1873 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1874 return true;
1875 for (arg_type = TYPE_ARG_TYPES (type);
1876 arg_type;
1877 arg_type = TREE_CHAIN (arg_type))
1878 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1879 return true;
1880 return false;
1881 }
1882 /* -- an array type constructed from any dependent type or whose
1883 size is specified by a constant expression that is
1884 value-dependent. */
1885 if (TREE_CODE (type) == ARRAY_TYPE)
1886 {
f1aba0a5 1887 if (TYPE_DOMAIN (type)
a723baf1
MM
1888 && ((cp_parser_value_dependent_expression_p
1889 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1890 || (cp_parser_type_dependent_expression_p
1891 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1892 return true;
1893 return cp_parser_dependent_type_p (TREE_TYPE (type));
1894 }
1895 /* -- a template-id in which either the template name is a template
1896 parameter or any of the template arguments is a dependent type or
1897 an expression that is type-dependent or value-dependent.
1898
1899 This language seems somewhat confused; for example, it does not
1900 discuss template template arguments. Therefore, we use the
1901 definition for dependent template arguments in [temp.dep.temp]. */
1902 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1903 && (cp_parser_dependent_template_id_p
1904 (CLASSTYPE_TI_TEMPLATE (type),
1905 CLASSTYPE_TI_ARGS (type))))
1906 return true;
1907 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1908 return true;
1909 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1910 expression is not type-dependent, then it should already been
1911 have resolved. */
1912 if (TREE_CODE (type) == TYPEOF_TYPE)
1913 return true;
1914 /* The standard does not specifically mention types that are local
1915 to template functions or local classes, but they should be
1916 considered dependent too. For example:
1917
1918 template <int I> void f() {
1919 enum E { a = I };
1920 S<sizeof (E)> s;
1921 }
1922
1923 The size of `E' cannot be known until the value of `I' has been
1924 determined. Therefore, `E' must be considered dependent. */
1925 scope = TYPE_CONTEXT (type);
1926 if (scope && TYPE_P (scope))
1927 return cp_parser_dependent_type_p (scope);
1928 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1929 return cp_parser_type_dependent_expression_p (scope);
1930
1931 /* Other types are non-dependent. */
1932 return false;
1933}
1934
1935/* Returns TRUE if the EXPRESSION is value-dependent. */
1936
1937static bool
1938cp_parser_value_dependent_expression_p (tree expression)
1939{
1940 if (!processing_template_decl)
1941 return false;
1942
1943 /* A name declared with a dependent type. */
1944 if (DECL_P (expression)
1945 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1946 return true;
1947 /* A non-type template parameter. */
1948 if ((TREE_CODE (expression) == CONST_DECL
1949 && DECL_TEMPLATE_PARM_P (expression))
1950 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1951 return true;
1952 /* A constant with integral or enumeration type and is initialized
1953 with an expression that is value-dependent. */
1954 if (TREE_CODE (expression) == VAR_DECL
1955 && DECL_INITIAL (expression)
1956 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1957 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1958 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1959 return true;
1960 /* These expressions are value-dependent if the type to which the
1961 cast occurs is dependent. */
1962 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1963 || TREE_CODE (expression) == STATIC_CAST_EXPR
1964 || TREE_CODE (expression) == CONST_CAST_EXPR
1965 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1966 || TREE_CODE (expression) == CAST_EXPR)
1967 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1968 return true;
1969 /* A `sizeof' expression where the sizeof operand is a type is
1970 value-dependent if the type is dependent. If the type was not
1971 dependent, we would no longer have a SIZEOF_EXPR, so any
1972 SIZEOF_EXPR is dependent. */
1973 if (TREE_CODE (expression) == SIZEOF_EXPR)
1974 return true;
1975 /* A constant expression is value-dependent if any subexpression is
1976 value-dependent. */
1977 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1978 {
1979 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1980 {
1981 case '1':
1982 return (cp_parser_value_dependent_expression_p
1983 (TREE_OPERAND (expression, 0)));
1984 case '<':
1985 case '2':
1986 return ((cp_parser_value_dependent_expression_p
1987 (TREE_OPERAND (expression, 0)))
1988 || (cp_parser_value_dependent_expression_p
1989 (TREE_OPERAND (expression, 1))));
1990 case 'e':
1991 {
1992 int i;
1993 for (i = 0;
1994 i < TREE_CODE_LENGTH (TREE_CODE (expression));
1995 ++i)
1996 if (cp_parser_value_dependent_expression_p
1997 (TREE_OPERAND (expression, i)))
1998 return true;
1999 return false;
2000 }
2001 }
2002 }
2003
2004 /* The expression is not value-dependent. */
2005 return false;
2006}
2007
2008/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2009 [temp.dep.expr]. */
2010
2011static bool
2012cp_parser_type_dependent_expression_p (expression)
2013 tree expression;
2014{
2015 if (!processing_template_decl)
2016 return false;
2017
2018 /* Some expression forms are never type-dependent. */
2019 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2020 || TREE_CODE (expression) == SIZEOF_EXPR
2021 || TREE_CODE (expression) == ALIGNOF_EXPR
2022 || TREE_CODE (expression) == TYPEID_EXPR
2023 || TREE_CODE (expression) == DELETE_EXPR
2024 || TREE_CODE (expression) == VEC_DELETE_EXPR
2025 || TREE_CODE (expression) == THROW_EXPR)
2026 return false;
2027
2028 /* The types of these expressions depends only on the type to which
2029 the cast occurs. */
2030 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2031 || TREE_CODE (expression) == STATIC_CAST_EXPR
2032 || TREE_CODE (expression) == CONST_CAST_EXPR
2033 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2034 || TREE_CODE (expression) == CAST_EXPR)
2035 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2036 /* The types of these expressions depends only on the type created
2037 by the expression. */
2038 else if (TREE_CODE (expression) == NEW_EXPR
2039 || TREE_CODE (expression) == VEC_NEW_EXPR)
2040 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2041
2042 if (TREE_CODE (expression) == FUNCTION_DECL
2043 && DECL_LANG_SPECIFIC (expression)
2044 && DECL_TEMPLATE_INFO (expression)
2045 && (cp_parser_dependent_template_id_p
2046 (DECL_TI_TEMPLATE (expression),
2047 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2048 return true;
2049
2050 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2051}
2052
2053/* Returns TRUE if the ARG (a template argument) is dependent. */
2054
2055static bool
2056cp_parser_dependent_template_arg_p (tree arg)
2057{
2058 if (!processing_template_decl)
2059 return false;
2060
2061 if (TREE_CODE (arg) == TEMPLATE_DECL
2062 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2063 return cp_parser_dependent_template_p (arg);
2064 else if (TYPE_P (arg))
2065 return cp_parser_dependent_type_p (arg);
2066 else
2067 return (cp_parser_type_dependent_expression_p (arg)
2068 || cp_parser_value_dependent_expression_p (arg));
2069}
2070
2071/* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2072
2073static bool
2074cp_parser_dependent_template_id_p (tree tmpl, tree args)
2075{
2076 int i;
2077
2078 if (cp_parser_dependent_template_p (tmpl))
2079 return true;
2080 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2081 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2082 return true;
2083 return false;
2084}
2085
2086/* Returns TRUE if the template TMPL is dependent. */
2087
2088static bool
2089cp_parser_dependent_template_p (tree tmpl)
2090{
2091 /* Template template parameters are dependent. */
2092 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2093 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2094 return true;
2095 /* So are member templates of dependent classes. */
2096 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2097 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2098 return false;
2099}
2100
a723baf1
MM
2101/* Returns the scope through which DECL is being accessed, or
2102 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2103 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2104 or `x', respectively. If the DECL was named as `A::B' then
2105 NESTED_NAME_SPECIFIER is `A'. */
2106
2107tree
2108cp_parser_scope_through_which_access_occurs (decl,
2109 object_type,
2110 nested_name_specifier)
2111 tree decl;
2112 tree object_type;
2113 tree nested_name_specifier;
2114{
2115 tree scope;
2116 tree qualifying_type = NULL_TREE;
2117
2118 /* Determine the SCOPE of DECL. */
2119 scope = context_for_name_lookup (decl);
2120 /* If the SCOPE is not a type, then DECL is not a member. */
2121 if (!TYPE_P (scope))
2122 return NULL_TREE;
2123 /* Figure out the type through which DECL is being accessed. */
a6f6052a
MM
2124 if (object_type
2125 /* OBJECT_TYPE might not be a class type; consider:
2126
2127 class A { typedef int I; };
2128 I *p;
2129 p->A::I::~I();
2130
2131 In this case, we will have "A::I" as the DECL, but "I" as the
2132 OBJECT_TYPE. */
2133 && CLASS_TYPE_P (object_type)
2134 && DERIVED_FROM_P (scope, object_type))
a723baf1
MM
2135 /* If we are processing a `->' or `.' expression, use the type of the
2136 left-hand side. */
2137 qualifying_type = object_type;
2138 else if (nested_name_specifier)
2139 {
2140 /* If the reference is to a non-static member of the
2141 current class, treat it as if it were referenced through
2142 `this'. */
2143 if (DECL_NONSTATIC_MEMBER_P (decl)
2144 && current_class_ptr
2145 && DERIVED_FROM_P (scope, current_class_type))
2146 qualifying_type = current_class_type;
2147 /* Otherwise, use the type indicated by the
2148 nested-name-specifier. */
2149 else
2150 qualifying_type = nested_name_specifier;
2151 }
2152 else
2153 /* Otherwise, the name must be from the current class or one of
2154 its bases. */
2155 qualifying_type = currently_open_derived_class (scope);
2156
2157 return qualifying_type;
2158}
2159
2160/* Issue the indicated error MESSAGE. */
2161
2162static void
2163cp_parser_error (parser, message)
2164 cp_parser *parser;
2165 const char *message;
2166{
a723baf1 2167 /* Output the MESSAGE -- unless we're parsing tentatively. */
e5976695 2168 if (!cp_parser_simulate_error (parser))
a723baf1
MM
2169 error (message);
2170}
2171
2172/* If we are parsing tentatively, remember that an error has occurred
e5976695
MM
2173 during this tentative parse. Returns true if the error was
2174 simulated; false if a messgae should be issued by the caller. */
a723baf1 2175
e5976695 2176static bool
a723baf1
MM
2177cp_parser_simulate_error (parser)
2178 cp_parser *parser;
2179{
2180 if (cp_parser_parsing_tentatively (parser)
2181 && !cp_parser_committed_to_tentative_parse (parser))
e5976695
MM
2182 {
2183 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2184 return true;
2185 }
2186 return false;
a723baf1
MM
2187}
2188
2189/* This function is called when a type is defined. If type
2190 definitions are forbidden at this point, an error message is
2191 issued. */
2192
2193static void
2194cp_parser_check_type_definition (parser)
2195 cp_parser *parser;
2196{
2197 /* If types are forbidden here, issue a message. */
2198 if (parser->type_definition_forbidden_message)
2199 /* Use `%s' to print the string in case there are any escape
2200 characters in the message. */
2201 error ("%s", parser->type_definition_forbidden_message);
2202}
2203
2204/* Consume tokens up to, and including, the next non-nested closing `)'.
2205 Returns TRUE iff we found a closing `)'. */
2206
2207static bool
2208cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2209{
2210 unsigned nesting_depth = 0;
2211
2212 while (true)
2213 {
2214 cp_token *token;
2215
2216 /* If we've run out of tokens, then there is no closing `)'. */
2217 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2218 return false;
2219 /* Consume the token. */
2220 token = cp_lexer_consume_token (parser->lexer);
2221 /* If it is an `(', we have entered another level of nesting. */
2222 if (token->type == CPP_OPEN_PAREN)
2223 ++nesting_depth;
2224 /* If it is a `)', then we might be done. */
2225 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2226 return true;
2227 }
2228}
2229
2230/* Consume tokens until the next token is a `)', or a `,'. Returns
2231 TRUE if the next token is a `,'. */
2232
2233static bool
2234cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2235{
2236 unsigned nesting_depth = 0;
2237
2238 while (true)
2239 {
2240 cp_token *token = cp_lexer_peek_token (parser->lexer);
2241
2242 /* If we've run out of tokens, then there is no closing `)'. */
2243 if (token->type == CPP_EOF)
2244 return false;
2245 /* If it is a `,' stop. */
2246 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2247 return true;
2248 /* If it is a `)', stop. */
2249 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2250 return false;
2251 /* If it is an `(', we have entered another level of nesting. */
2252 else if (token->type == CPP_OPEN_PAREN)
2253 ++nesting_depth;
2254 /* Consume the token. */
2255 token = cp_lexer_consume_token (parser->lexer);
2256 }
2257}
2258
2259/* Consume tokens until we reach the end of the current statement.
2260 Normally, that will be just before consuming a `;'. However, if a
2261 non-nested `}' comes first, then we stop before consuming that. */
2262
2263static void
2264cp_parser_skip_to_end_of_statement (parser)
2265 cp_parser *parser;
2266{
2267 unsigned nesting_depth = 0;
2268
2269 while (true)
2270 {
2271 cp_token *token;
2272
2273 /* Peek at the next token. */
2274 token = cp_lexer_peek_token (parser->lexer);
2275 /* If we've run out of tokens, stop. */
2276 if (token->type == CPP_EOF)
2277 break;
2278 /* If the next token is a `;', we have reached the end of the
2279 statement. */
2280 if (token->type == CPP_SEMICOLON && !nesting_depth)
2281 break;
2282 /* If the next token is a non-nested `}', then we have reached
2283 the end of the current block. */
2284 if (token->type == CPP_CLOSE_BRACE)
2285 {
2286 /* If this is a non-nested `}', stop before consuming it.
2287 That way, when confronted with something like:
2288
2289 { 3 + }
2290
2291 we stop before consuming the closing `}', even though we
2292 have not yet reached a `;'. */
2293 if (nesting_depth == 0)
2294 break;
2295 /* If it is the closing `}' for a block that we have
2296 scanned, stop -- but only after consuming the token.
2297 That way given:
2298
2299 void f g () { ... }
2300 typedef int I;
2301
2302 we will stop after the body of the erroneously declared
2303 function, but before consuming the following `typedef'
2304 declaration. */
2305 if (--nesting_depth == 0)
2306 {
2307 cp_lexer_consume_token (parser->lexer);
2308 break;
2309 }
2310 }
2311 /* If it the next token is a `{', then we are entering a new
2312 block. Consume the entire block. */
2313 else if (token->type == CPP_OPEN_BRACE)
2314 ++nesting_depth;
2315 /* Consume the token. */
2316 cp_lexer_consume_token (parser->lexer);
2317 }
2318}
2319
2320/* Skip tokens until we have consumed an entire block, or until we
2321 have consumed a non-nested `;'. */
2322
2323static void
2324cp_parser_skip_to_end_of_block_or_statement (parser)
2325 cp_parser *parser;
2326{
2327 unsigned nesting_depth = 0;
2328
2329 while (true)
2330 {
2331 cp_token *token;
2332
2333 /* Peek at the next token. */
2334 token = cp_lexer_peek_token (parser->lexer);
2335 /* If we've run out of tokens, stop. */
2336 if (token->type == CPP_EOF)
2337 break;
2338 /* If the next token is a `;', we have reached the end of the
2339 statement. */
2340 if (token->type == CPP_SEMICOLON && !nesting_depth)
2341 {
2342 /* Consume the `;'. */
2343 cp_lexer_consume_token (parser->lexer);
2344 break;
2345 }
2346 /* Consume the token. */
2347 token = cp_lexer_consume_token (parser->lexer);
2348 /* If the next token is a non-nested `}', then we have reached
2349 the end of the current block. */
2350 if (token->type == CPP_CLOSE_BRACE
2351 && (nesting_depth == 0 || --nesting_depth == 0))
2352 break;
2353 /* If it the next token is a `{', then we are entering a new
2354 block. Consume the entire block. */
2355 if (token->type == CPP_OPEN_BRACE)
2356 ++nesting_depth;
2357 }
2358}
2359
2360/* Skip tokens until a non-nested closing curly brace is the next
2361 token. */
2362
2363static void
2364cp_parser_skip_to_closing_brace (cp_parser *parser)
2365{
2366 unsigned nesting_depth = 0;
2367
2368 while (true)
2369 {
2370 cp_token *token;
2371
2372 /* Peek at the next token. */
2373 token = cp_lexer_peek_token (parser->lexer);
2374 /* If we've run out of tokens, stop. */
2375 if (token->type == CPP_EOF)
2376 break;
2377 /* If the next token is a non-nested `}', then we have reached
2378 the end of the current block. */
2379 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2380 break;
2381 /* If it the next token is a `{', then we are entering a new
2382 block. Consume the entire block. */
2383 else if (token->type == CPP_OPEN_BRACE)
2384 ++nesting_depth;
2385 /* Consume the token. */
2386 cp_lexer_consume_token (parser->lexer);
2387 }
2388}
2389
2390/* Create a new C++ parser. */
2391
2392static cp_parser *
2393cp_parser_new ()
2394{
2395 cp_parser *parser;
17211ab5
GK
2396 cp_lexer *lexer;
2397
2398 /* cp_lexer_new_main is called before calling ggc_alloc because
2399 cp_lexer_new_main might load a PCH file. */
2400 lexer = cp_lexer_new_main ();
a723baf1
MM
2401
2402 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2403 parser->lexer = lexer;
a723baf1
MM
2404 parser->context = cp_parser_context_new (NULL);
2405
2406 /* For now, we always accept GNU extensions. */
2407 parser->allow_gnu_extensions_p = 1;
2408
2409 /* The `>' token is a greater-than operator, not the end of a
2410 template-id. */
2411 parser->greater_than_is_operator_p = true;
2412
2413 parser->default_arg_ok_p = true;
2414
2415 /* We are not parsing a constant-expression. */
2416 parser->constant_expression_p = false;
2417
2418 /* Local variable names are not forbidden. */
2419 parser->local_variables_forbidden_p = false;
2420
2421 /* We are not procesing an `extern "C"' declaration. */
2422 parser->in_unbraced_linkage_specification_p = false;
2423
2424 /* We are not processing a declarator. */
2425 parser->in_declarator_p = false;
2426
a723baf1
MM
2427 /* The unparsed function queue is empty. */
2428 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2429
2430 /* There are no classes being defined. */
2431 parser->num_classes_being_defined = 0;
2432
2433 /* No template parameters apply. */
2434 parser->num_template_parameter_lists = 0;
2435
2436 return parser;
2437}
2438
2439/* Lexical conventions [gram.lex] */
2440
2441/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2442 identifier. */
2443
2444static tree
2445cp_parser_identifier (parser)
2446 cp_parser *parser;
2447{
2448 cp_token *token;
2449
2450 /* Look for the identifier. */
2451 token = cp_parser_require (parser, CPP_NAME, "identifier");
2452 /* Return the value. */
2453 return token ? token->value : error_mark_node;
2454}
2455
2456/* Basic concepts [gram.basic] */
2457
2458/* Parse a translation-unit.
2459
2460 translation-unit:
2461 declaration-seq [opt]
2462
2463 Returns TRUE if all went well. */
2464
2465static bool
2466cp_parser_translation_unit (parser)
2467 cp_parser *parser;
2468{
2469 while (true)
2470 {
2471 cp_parser_declaration_seq_opt (parser);
2472
2473 /* If there are no tokens left then all went well. */
2474 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2475 break;
2476
2477 /* Otherwise, issue an error message. */
2478 cp_parser_error (parser, "expected declaration");
2479 return false;
2480 }
2481
2482 /* Consume the EOF token. */
2483 cp_parser_require (parser, CPP_EOF, "end-of-file");
2484
2485 /* Finish up. */
2486 finish_translation_unit ();
2487
2488 /* All went well. */
2489 return true;
2490}
2491
2492/* Expressions [gram.expr] */
2493
2494/* Parse a primary-expression.
2495
2496 primary-expression:
2497 literal
2498 this
2499 ( expression )
2500 id-expression
2501
2502 GNU Extensions:
2503
2504 primary-expression:
2505 ( compound-statement )
2506 __builtin_va_arg ( assignment-expression , type-id )
2507
2508 literal:
2509 __null
2510
2511 Returns a representation of the expression.
2512
2513 *IDK indicates what kind of id-expression (if any) was present.
2514
2515 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2516 used as the operand of a pointer-to-member. In that case,
2517 *QUALIFYING_CLASS gives the class that is used as the qualifying
2518 class in the pointer-to-member. */
2519
2520static tree
2521cp_parser_primary_expression (cp_parser *parser,
2522 cp_parser_id_kind *idk,
2523 tree *qualifying_class)
2524{
2525 cp_token *token;
2526
2527 /* Assume the primary expression is not an id-expression. */
2528 *idk = CP_PARSER_ID_KIND_NONE;
2529 /* And that it cannot be used as pointer-to-member. */
2530 *qualifying_class = NULL_TREE;
2531
2532 /* Peek at the next token. */
2533 token = cp_lexer_peek_token (parser->lexer);
2534 switch (token->type)
2535 {
2536 /* literal:
2537 integer-literal
2538 character-literal
2539 floating-literal
2540 string-literal
2541 boolean-literal */
2542 case CPP_CHAR:
2543 case CPP_WCHAR:
2544 case CPP_STRING:
2545 case CPP_WSTRING:
2546 case CPP_NUMBER:
2547 token = cp_lexer_consume_token (parser->lexer);
2548 return token->value;
2549
2550 case CPP_OPEN_PAREN:
2551 {
2552 tree expr;
2553 bool saved_greater_than_is_operator_p;
2554
2555 /* Consume the `('. */
2556 cp_lexer_consume_token (parser->lexer);
2557 /* Within a parenthesized expression, a `>' token is always
2558 the greater-than operator. */
2559 saved_greater_than_is_operator_p
2560 = parser->greater_than_is_operator_p;
2561 parser->greater_than_is_operator_p = true;
2562 /* If we see `( { ' then we are looking at the beginning of
2563 a GNU statement-expression. */
2564 if (cp_parser_allow_gnu_extensions_p (parser)
2565 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2566 {
2567 /* Statement-expressions are not allowed by the standard. */
2568 if (pedantic)
2569 pedwarn ("ISO C++ forbids braced-groups within expressions");
2570
2571 /* And they're not allowed outside of a function-body; you
2572 cannot, for example, write:
2573
2574 int i = ({ int j = 3; j + 1; });
2575
2576 at class or namespace scope. */
2577 if (!at_function_scope_p ())
2578 error ("statement-expressions are allowed only inside functions");
2579 /* Start the statement-expression. */
2580 expr = begin_stmt_expr ();
2581 /* Parse the compound-statement. */
2582 cp_parser_compound_statement (parser);
2583 /* Finish up. */
2584 expr = finish_stmt_expr (expr);
2585 }
2586 else
2587 {
2588 /* Parse the parenthesized expression. */
2589 expr = cp_parser_expression (parser);
2590 /* Let the front end know that this expression was
2591 enclosed in parentheses. This matters in case, for
2592 example, the expression is of the form `A::B', since
2593 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2594 not. */
2595 finish_parenthesized_expr (expr);
2596 }
2597 /* The `>' token might be the end of a template-id or
2598 template-parameter-list now. */
2599 parser->greater_than_is_operator_p
2600 = saved_greater_than_is_operator_p;
2601 /* Consume the `)'. */
2602 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2603 cp_parser_skip_to_end_of_statement (parser);
2604
2605 return expr;
2606 }
2607
2608 case CPP_KEYWORD:
2609 switch (token->keyword)
2610 {
2611 /* These two are the boolean literals. */
2612 case RID_TRUE:
2613 cp_lexer_consume_token (parser->lexer);
2614 return boolean_true_node;
2615 case RID_FALSE:
2616 cp_lexer_consume_token (parser->lexer);
2617 return boolean_false_node;
2618
2619 /* The `__null' literal. */
2620 case RID_NULL:
2621 cp_lexer_consume_token (parser->lexer);
2622 return null_node;
2623
2624 /* Recognize the `this' keyword. */
2625 case RID_THIS:
2626 cp_lexer_consume_token (parser->lexer);
2627 if (parser->local_variables_forbidden_p)
2628 {
2629 error ("`this' may not be used in this context");
2630 return error_mark_node;
2631 }
2632 return finish_this_expr ();
2633
2634 /* The `operator' keyword can be the beginning of an
2635 id-expression. */
2636 case RID_OPERATOR:
2637 goto id_expression;
2638
2639 case RID_FUNCTION_NAME:
2640 case RID_PRETTY_FUNCTION_NAME:
2641 case RID_C99_FUNCTION_NAME:
2642 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2643 __func__ are the names of variables -- but they are
2644 treated specially. Therefore, they are handled here,
2645 rather than relying on the generic id-expression logic
2646 below. Gramatically, these names are id-expressions.
2647
2648 Consume the token. */
2649 token = cp_lexer_consume_token (parser->lexer);
2650 /* Look up the name. */
2651 return finish_fname (token->value);
2652
2653 case RID_VA_ARG:
2654 {
2655 tree expression;
2656 tree type;
2657
2658 /* The `__builtin_va_arg' construct is used to handle
2659 `va_arg'. Consume the `__builtin_va_arg' token. */
2660 cp_lexer_consume_token (parser->lexer);
2661 /* Look for the opening `('. */
2662 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2663 /* Now, parse the assignment-expression. */
2664 expression = cp_parser_assignment_expression (parser);
2665 /* Look for the `,'. */
2666 cp_parser_require (parser, CPP_COMMA, "`,'");
2667 /* Parse the type-id. */
2668 type = cp_parser_type_id (parser);
2669 /* Look for the closing `)'. */
2670 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2671
2672 return build_x_va_arg (expression, type);
2673 }
2674
2675 default:
2676 cp_parser_error (parser, "expected primary-expression");
2677 return error_mark_node;
2678 }
2679 /* Fall through. */
2680
2681 /* An id-expression can start with either an identifier, a
2682 `::' as the beginning of a qualified-id, or the "operator"
2683 keyword. */
2684 case CPP_NAME:
2685 case CPP_SCOPE:
2686 case CPP_TEMPLATE_ID:
2687 case CPP_NESTED_NAME_SPECIFIER:
2688 {
2689 tree id_expression;
2690 tree decl;
2691
2692 id_expression:
2693 /* Parse the id-expression. */
2694 id_expression
2695 = cp_parser_id_expression (parser,
2696 /*template_keyword_p=*/false,
2697 /*check_dependency_p=*/true,
2698 /*template_p=*/NULL);
2699 if (id_expression == error_mark_node)
2700 return error_mark_node;
2701 /* If we have a template-id, then no further lookup is
2702 required. If the template-id was for a template-class, we
2703 will sometimes have a TYPE_DECL at this point. */
2704 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2705 || TREE_CODE (id_expression) == TYPE_DECL)
2706 decl = id_expression;
2707 /* Look up the name. */
2708 else
2709 {
2710 decl = cp_parser_lookup_name_simple (parser, id_expression);
2711 /* If name lookup gives us a SCOPE_REF, then the
2712 qualifying scope was dependent. Just propagate the
2713 name. */
2714 if (TREE_CODE (decl) == SCOPE_REF)
2715 {
2716 if (TYPE_P (TREE_OPERAND (decl, 0)))
2717 *qualifying_class = TREE_OPERAND (decl, 0);
2718 return decl;
2719 }
2720 /* Check to see if DECL is a local variable in a context
2721 where that is forbidden. */
2722 if (parser->local_variables_forbidden_p
2723 && local_variable_p (decl))
2724 {
2725 /* It might be that we only found DECL because we are
2726 trying to be generous with pre-ISO scoping rules.
2727 For example, consider:
2728
2729 int i;
2730 void g() {
2731 for (int i = 0; i < 10; ++i) {}
2732 extern void f(int j = i);
2733 }
2734
2735 Here, name look up will originally find the out
2736 of scope `i'. We need to issue a warning message,
2737 but then use the global `i'. */
2738 decl = check_for_out_of_scope_variable (decl);
2739 if (local_variable_p (decl))
2740 {
2741 error ("local variable `%D' may not appear in this context",
2742 decl);
2743 return error_mark_node;
2744 }
2745 }
2746
2747 /* If unqualified name lookup fails while processing a
2748 template, that just means that we need to do name
2749 lookup again when the template is instantiated. */
2750 if (!parser->scope
2751 && decl == error_mark_node
2752 && processing_template_decl)
2753 {
2754 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2755 return build_min_nt (LOOKUP_EXPR, id_expression);
2756 }
2757 else if (decl == error_mark_node
2758 && !processing_template_decl)
2759 {
2760 if (!parser->scope)
2761 {
2762 /* It may be resolvable as a koenig lookup function
2763 call. */
2764 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2765 return id_expression;
2766 }
2767 else if (TYPE_P (parser->scope)
2768 && !COMPLETE_TYPE_P (parser->scope))
2769 error ("incomplete type `%T' used in nested name specifier",
2770 parser->scope);
2771 else if (parser->scope != global_namespace)
2772 error ("`%D' is not a member of `%D'",
2773 id_expression, parser->scope);
2774 else
2775 error ("`::%D' has not been declared", id_expression);
2776 }
2777 /* If DECL is a variable would be out of scope under
2778 ANSI/ISO rules, but in scope in the ARM, name lookup
2779 will succeed. Issue a diagnostic here. */
2780 else
2781 decl = check_for_out_of_scope_variable (decl);
2782
2783 /* Remember that the name was used in the definition of
2784 the current class so that we can check later to see if
2785 the meaning would have been different after the class
2786 was entirely defined. */
2787 if (!parser->scope && decl != error_mark_node)
2788 maybe_note_name_used_in_class (id_expression, decl);
2789 }
2790
2791 /* If we didn't find anything, or what we found was a type,
2792 then this wasn't really an id-expression. */
2793 if (TREE_CODE (decl) == TYPE_DECL
2794 || TREE_CODE (decl) == NAMESPACE_DECL
2795 || (TREE_CODE (decl) == TEMPLATE_DECL
2796 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2797 {
2798 cp_parser_error (parser,
2799 "expected primary-expression");
2800 return error_mark_node;
2801 }
2802
2803 /* If the name resolved to a template parameter, there is no
2804 need to look it up again later. Similarly, we resolve
2805 enumeration constants to their underlying values. */
2806 if (TREE_CODE (decl) == CONST_DECL)
2807 {
2808 *idk = CP_PARSER_ID_KIND_NONE;
2809 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2810 return DECL_INITIAL (decl);
2811 return decl;
2812 }
2813 else
2814 {
2815 bool dependent_p;
2816
2817 /* If the declaration was explicitly qualified indicate
2818 that. The semantics of `A::f(3)' are different than
2819 `f(3)' if `f' is virtual. */
2820 *idk = (parser->scope
2821 ? CP_PARSER_ID_KIND_QUALIFIED
2822 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2823 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2824 : CP_PARSER_ID_KIND_UNQUALIFIED));
2825
2826
2827 /* [temp.dep.expr]
2828
2829 An id-expression is type-dependent if it contains an
2830 identifier that was declared with a dependent type.
2831
2832 As an optimization, we could choose not to create a
2833 LOOKUP_EXPR for a name that resolved to a local
2834 variable in the template function that we are currently
2835 declaring; such a name cannot ever resolve to anything
2836 else. If we did that we would not have to look up
2837 these names at instantiation time.
2838
2839 The standard is not very specific about an
2840 id-expression that names a set of overloaded functions.
2841 What if some of them have dependent types and some of
2842 them do not? Presumably, such a name should be treated
2843 as a dependent name. */
2844 /* Assume the name is not dependent. */
2845 dependent_p = false;
2846 if (!processing_template_decl)
2847 /* No names are dependent outside a template. */
2848 ;
2849 /* A template-id where the name of the template was not
2850 resolved is definitely dependent. */
2851 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2852 && (TREE_CODE (TREE_OPERAND (decl, 0))
2853 == IDENTIFIER_NODE))
2854 dependent_p = true;
2855 /* For anything except an overloaded function, just check
2856 its type. */
2857 else if (!is_overloaded_fn (decl))
2858 dependent_p
2859 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2860 /* For a set of overloaded functions, check each of the
2861 functions. */
2862 else
2863 {
2864 tree fns = decl;
2865
2866 if (BASELINK_P (fns))
2867 fns = BASELINK_FUNCTIONS (fns);
2868
2869 /* For a template-id, check to see if the template
2870 arguments are dependent. */
2871 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2872 {
2873 tree args = TREE_OPERAND (fns, 1);
2874
2875 if (args && TREE_CODE (args) == TREE_LIST)
2876 {
2877 while (args)
2878 {
2879 if (cp_parser_dependent_template_arg_p
2880 (TREE_VALUE (args)))
2881 {
2882 dependent_p = true;
2883 break;
2884 }
2885 args = TREE_CHAIN (args);
2886 }
2887 }
2888 else if (args && TREE_CODE (args) == TREE_VEC)
2889 {
2890 int i;
2891 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2892 if (cp_parser_dependent_template_arg_p
2893 (TREE_VEC_ELT (args, i)))
2894 {
2895 dependent_p = true;
2896 break;
2897 }
2898 }
2899
2900 /* The functions are those referred to by the
2901 template-id. */
2902 fns = TREE_OPERAND (fns, 0);
2903 }
2904
2905 /* If there are no dependent template arguments, go
2906 through the overlaoded functions. */
2907 while (fns && !dependent_p)
2908 {
2909 tree fn = OVL_CURRENT (fns);
2910
2911 /* Member functions of dependent classes are
2912 dependent. */
2913 if (TREE_CODE (fn) == FUNCTION_DECL
2914 && cp_parser_type_dependent_expression_p (fn))
2915 dependent_p = true;
2916 else if (TREE_CODE (fn) == TEMPLATE_DECL
2917 && cp_parser_dependent_template_p (fn))
2918 dependent_p = true;
2919
2920 fns = OVL_NEXT (fns);
2921 }
2922 }
2923
2924 /* If the name was dependent on a template parameter,
2925 we will resolve the name at instantiation time. */
2926 if (dependent_p)
2927 {
2928 /* Create a SCOPE_REF for qualified names. */
2929 if (parser->scope)
2930 {
2931 if (TYPE_P (parser->scope))
2932 *qualifying_class = parser->scope;
2933 return build_nt (SCOPE_REF,
2934 parser->scope,
2935 id_expression);
2936 }
2937 /* A TEMPLATE_ID already contains all the information
2938 we need. */
2939 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2940 return id_expression;
2941 /* Create a LOOKUP_EXPR for other unqualified names. */
2942 return build_min_nt (LOOKUP_EXPR, id_expression);
2943 }
2944
2945 if (parser->scope)
2946 {
2947 decl = (adjust_result_of_qualified_name_lookup
2948 (decl, parser->scope, current_class_type));
2949 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2950 *qualifying_class = parser->scope;
2951 }
a723baf1
MM
2952 else
2953 /* Transform references to non-static data members into
2954 COMPONENT_REFs. */
2955 decl = hack_identifier (decl, id_expression);
f74dbcec
JM
2956
2957 /* Resolve references to variables of anonymous unions
2958 into COMPONENT_REFs. */
2959 if (TREE_CODE (decl) == ALIAS_DECL)
2960 decl = DECL_INITIAL (decl);
a723baf1
MM
2961 }
2962
2963 if (TREE_DEPRECATED (decl))
2964 warn_deprecated_use (decl);
2965
2966 return decl;
2967 }
2968
2969 /* Anything else is an error. */
2970 default:
2971 cp_parser_error (parser, "expected primary-expression");
2972 return error_mark_node;
2973 }
2974}
2975
2976/* Parse an id-expression.
2977
2978 id-expression:
2979 unqualified-id
2980 qualified-id
2981
2982 qualified-id:
2983 :: [opt] nested-name-specifier template [opt] unqualified-id
2984 :: identifier
2985 :: operator-function-id
2986 :: template-id
2987
2988 Return a representation of the unqualified portion of the
2989 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2990 a `::' or nested-name-specifier.
2991
2992 Often, if the id-expression was a qualified-id, the caller will
2993 want to make a SCOPE_REF to represent the qualified-id. This
2994 function does not do this in order to avoid wastefully creating
2995 SCOPE_REFs when they are not required.
2996
a723baf1
MM
2997 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2998 `template' keyword.
2999
3000 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3001 uninstantiated templates.
3002
15d2cb19 3003 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1
MM
3004 `template' keyword is used to explicitly indicate that the entity
3005 named is a template. */
3006
3007static tree
3008cp_parser_id_expression (cp_parser *parser,
3009 bool template_keyword_p,
3010 bool check_dependency_p,
3011 bool *template_p)
3012{
3013 bool global_scope_p;
3014 bool nested_name_specifier_p;
3015
3016 /* Assume the `template' keyword was not used. */
3017 if (template_p)
3018 *template_p = false;
3019
3020 /* Look for the optional `::' operator. */
3021 global_scope_p
3022 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3023 != NULL_TREE);
3024 /* Look for the optional nested-name-specifier. */
3025 nested_name_specifier_p
3026 = (cp_parser_nested_name_specifier_opt (parser,
3027 /*typename_keyword_p=*/false,
3028 check_dependency_p,
3029 /*type_p=*/false)
3030 != NULL_TREE);
3031 /* If there is a nested-name-specifier, then we are looking at
3032 the first qualified-id production. */
3033 if (nested_name_specifier_p)
3034 {
3035 tree saved_scope;
3036 tree saved_object_scope;
3037 tree saved_qualifying_scope;
3038 tree unqualified_id;
3039 bool is_template;
3040
3041 /* See if the next token is the `template' keyword. */
3042 if (!template_p)
3043 template_p = &is_template;
3044 *template_p = cp_parser_optional_template_keyword (parser);
3045 /* Name lookup we do during the processing of the
3046 unqualified-id might obliterate SCOPE. */
3047 saved_scope = parser->scope;
3048 saved_object_scope = parser->object_scope;
3049 saved_qualifying_scope = parser->qualifying_scope;
3050 /* Process the final unqualified-id. */
3051 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3052 check_dependency_p);
3053 /* Restore the SAVED_SCOPE for our caller. */
3054 parser->scope = saved_scope;
3055 parser->object_scope = saved_object_scope;
3056 parser->qualifying_scope = saved_qualifying_scope;
3057
3058 return unqualified_id;
3059 }
3060 /* Otherwise, if we are in global scope, then we are looking at one
3061 of the other qualified-id productions. */
3062 else if (global_scope_p)
3063 {
3064 cp_token *token;
3065 tree id;
3066
e5976695
MM
3067 /* Peek at the next token. */
3068 token = cp_lexer_peek_token (parser->lexer);
3069
3070 /* If it's an identifier, and the next token is not a "<", then
3071 we can avoid the template-id case. This is an optimization
3072 for this common case. */
3073 if (token->type == CPP_NAME
3074 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3075 return cp_parser_identifier (parser);
3076
a723baf1
MM
3077 cp_parser_parse_tentatively (parser);
3078 /* Try a template-id. */
3079 id = cp_parser_template_id (parser,
3080 /*template_keyword_p=*/false,
3081 /*check_dependency_p=*/true);
3082 /* If that worked, we're done. */
3083 if (cp_parser_parse_definitely (parser))
3084 return id;
3085
e5976695
MM
3086 /* Peek at the next token. (Changes in the token buffer may
3087 have invalidated the pointer obtained above.) */
a723baf1
MM
3088 token = cp_lexer_peek_token (parser->lexer);
3089
3090 switch (token->type)
3091 {
3092 case CPP_NAME:
3093 return cp_parser_identifier (parser);
3094
3095 case CPP_KEYWORD:
3096 if (token->keyword == RID_OPERATOR)
3097 return cp_parser_operator_function_id (parser);
3098 /* Fall through. */
3099
3100 default:
3101 cp_parser_error (parser, "expected id-expression");
3102 return error_mark_node;
3103 }
3104 }
3105 else
3106 return cp_parser_unqualified_id (parser, template_keyword_p,
3107 /*check_dependency_p=*/true);
3108}
3109
3110/* Parse an unqualified-id.
3111
3112 unqualified-id:
3113 identifier
3114 operator-function-id
3115 conversion-function-id
3116 ~ class-name
3117 template-id
3118
3119 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3120 keyword, in a construct like `A::template ...'.
3121
3122 Returns a representation of unqualified-id. For the `identifier'
3123 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3124 production a BIT_NOT_EXPR is returned; the operand of the
3125 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3126 other productions, see the documentation accompanying the
3127 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3128 names are looked up in uninstantiated templates. */
3129
3130static tree
3131cp_parser_unqualified_id (parser, template_keyword_p,
3132 check_dependency_p)
3133 cp_parser *parser;
3134 bool template_keyword_p;
3135 bool check_dependency_p;
3136{
3137 cp_token *token;
3138
3139 /* Peek at the next token. */
3140 token = cp_lexer_peek_token (parser->lexer);
3141
3142 switch (token->type)
3143 {
3144 case CPP_NAME:
3145 {
3146 tree id;
3147
3148 /* We don't know yet whether or not this will be a
3149 template-id. */
3150 cp_parser_parse_tentatively (parser);
3151 /* Try a template-id. */
3152 id = cp_parser_template_id (parser, template_keyword_p,
3153 check_dependency_p);
3154 /* If it worked, we're done. */
3155 if (cp_parser_parse_definitely (parser))
3156 return id;
3157 /* Otherwise, it's an ordinary identifier. */
3158 return cp_parser_identifier (parser);
3159 }
3160
3161 case CPP_TEMPLATE_ID:
3162 return cp_parser_template_id (parser, template_keyword_p,
3163 check_dependency_p);
3164
3165 case CPP_COMPL:
3166 {
3167 tree type_decl;
3168 tree qualifying_scope;
3169 tree object_scope;
3170 tree scope;
3171
3172 /* Consume the `~' token. */
3173 cp_lexer_consume_token (parser->lexer);
3174 /* Parse the class-name. The standard, as written, seems to
3175 say that:
3176
3177 template <typename T> struct S { ~S (); };
3178 template <typename T> S<T>::~S() {}
3179
3180 is invalid, since `~' must be followed by a class-name, but
3181 `S<T>' is dependent, and so not known to be a class.
3182 That's not right; we need to look in uninstantiated
3183 templates. A further complication arises from:
3184
3185 template <typename T> void f(T t) {
3186 t.T::~T();
3187 }
3188
3189 Here, it is not possible to look up `T' in the scope of `T'
3190 itself. We must look in both the current scope, and the
3191 scope of the containing complete expression.
3192
3193 Yet another issue is:
3194
3195 struct S {
3196 int S;
3197 ~S();
3198 };
3199
3200 S::~S() {}
3201
3202 The standard does not seem to say that the `S' in `~S'
3203 should refer to the type `S' and not the data member
3204 `S::S'. */
3205
3206 /* DR 244 says that we look up the name after the "~" in the
3207 same scope as we looked up the qualifying name. That idea
3208 isn't fully worked out; it's more complicated than that. */
3209 scope = parser->scope;
3210 object_scope = parser->object_scope;
3211 qualifying_scope = parser->qualifying_scope;
3212
3213 /* If the name is of the form "X::~X" it's OK. */
3214 if (scope && TYPE_P (scope)
3215 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3216 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3217 == CPP_OPEN_PAREN)
3218 && (cp_lexer_peek_token (parser->lexer)->value
3219 == TYPE_IDENTIFIER (scope)))
3220 {
3221 cp_lexer_consume_token (parser->lexer);
3222 return build_nt (BIT_NOT_EXPR, scope);
3223 }
3224
3225 /* If there was an explicit qualification (S::~T), first look
3226 in the scope given by the qualification (i.e., S). */
3227 if (scope)
3228 {
3229 cp_parser_parse_tentatively (parser);
3230 type_decl = cp_parser_class_name (parser,
3231 /*typename_keyword_p=*/false,
3232 /*template_keyword_p=*/false,
3233 /*type_p=*/false,
3234 /*check_access_p=*/true,
3235 /*check_dependency=*/false,
3236 /*class_head_p=*/false);
3237 if (cp_parser_parse_definitely (parser))
3238 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3239 }
3240 /* In "N::S::~S", look in "N" as well. */
3241 if (scope && qualifying_scope)
3242 {
3243 cp_parser_parse_tentatively (parser);
3244 parser->scope = qualifying_scope;
3245 parser->object_scope = NULL_TREE;
3246 parser->qualifying_scope = NULL_TREE;
3247 type_decl
3248 = cp_parser_class_name (parser,
3249 /*typename_keyword_p=*/false,
3250 /*template_keyword_p=*/false,
3251 /*type_p=*/false,
3252 /*check_access_p=*/true,
3253 /*check_dependency=*/false,
3254 /*class_head_p=*/false);
3255 if (cp_parser_parse_definitely (parser))
3256 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3257 }
3258 /* In "p->S::~T", look in the scope given by "*p" as well. */
3259 else if (object_scope)
3260 {
3261 cp_parser_parse_tentatively (parser);
3262 parser->scope = object_scope;
3263 parser->object_scope = NULL_TREE;
3264 parser->qualifying_scope = NULL_TREE;
3265 type_decl
3266 = cp_parser_class_name (parser,
3267 /*typename_keyword_p=*/false,
3268 /*template_keyword_p=*/false,
3269 /*type_p=*/false,
3270 /*check_access_p=*/true,
3271 /*check_dependency=*/false,
3272 /*class_head_p=*/false);
3273 if (cp_parser_parse_definitely (parser))
3274 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3275 }
3276 /* Look in the surrounding context. */
3277 parser->scope = NULL_TREE;
3278 parser->object_scope = NULL_TREE;
3279 parser->qualifying_scope = NULL_TREE;
3280 type_decl
3281 = cp_parser_class_name (parser,
3282 /*typename_keyword_p=*/false,
3283 /*template_keyword_p=*/false,
3284 /*type_p=*/false,
3285 /*check_access_p=*/true,
3286 /*check_dependency=*/false,
3287 /*class_head_p=*/false);
3288 /* If an error occurred, assume that the name of the
3289 destructor is the same as the name of the qualifying
3290 class. That allows us to keep parsing after running
3291 into ill-formed destructor names. */
3292 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3293 return build_nt (BIT_NOT_EXPR, scope);
3294 else if (type_decl == error_mark_node)
3295 return error_mark_node;
3296
3297 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3298 }
3299
3300 case CPP_KEYWORD:
3301 if (token->keyword == RID_OPERATOR)
3302 {
3303 tree id;
3304
3305 /* This could be a template-id, so we try that first. */
3306 cp_parser_parse_tentatively (parser);
3307 /* Try a template-id. */
3308 id = cp_parser_template_id (parser, template_keyword_p,
3309 /*check_dependency_p=*/true);
3310 /* If that worked, we're done. */
3311 if (cp_parser_parse_definitely (parser))
3312 return id;
3313 /* We still don't know whether we're looking at an
3314 operator-function-id or a conversion-function-id. */
3315 cp_parser_parse_tentatively (parser);
3316 /* Try an operator-function-id. */
3317 id = cp_parser_operator_function_id (parser);
3318 /* If that didn't work, try a conversion-function-id. */
3319 if (!cp_parser_parse_definitely (parser))
3320 id = cp_parser_conversion_function_id (parser);
3321
3322 return id;
3323 }
3324 /* Fall through. */
3325
3326 default:
3327 cp_parser_error (parser, "expected unqualified-id");
3328 return error_mark_node;
3329 }
3330}
3331
3332/* Parse an (optional) nested-name-specifier.
3333
3334 nested-name-specifier:
3335 class-or-namespace-name :: nested-name-specifier [opt]
3336 class-or-namespace-name :: template nested-name-specifier [opt]
3337
3338 PARSER->SCOPE should be set appropriately before this function is
3339 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3340 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3341 in name lookups.
3342
3343 Sets PARSER->SCOPE to the class (TYPE) or namespace
3344 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3345 it unchanged if there is no nested-name-specifier. Returns the new
3346 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3347
3348static tree
3349cp_parser_nested_name_specifier_opt (cp_parser *parser,
3350 bool typename_keyword_p,
3351 bool check_dependency_p,
3352 bool type_p)
3353{
3354 bool success = false;
3355 tree access_check = NULL_TREE;
3356 ptrdiff_t start;
2050a1bb 3357 cp_token* token;
a723baf1
MM
3358
3359 /* If the next token corresponds to a nested name specifier, there
2050a1bb
MM
3360 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3361 false, it may have been true before, in which case something
3362 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3363 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3364 CHECK_DEPENDENCY_P is false, we have to fall through into the
3365 main loop. */
3366 if (check_dependency_p
3367 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3368 {
3369 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3370 return parser->scope;
3371 }
3372
3373 /* Remember where the nested-name-specifier starts. */
3374 if (cp_parser_parsing_tentatively (parser)
3375 && !cp_parser_committed_to_tentative_parse (parser))
3376 {
2050a1bb 3377 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
3378 start = cp_lexer_token_difference (parser->lexer,
3379 parser->lexer->first_token,
2050a1bb 3380 token);
a723baf1
MM
3381 }
3382 else
3383 start = -1;
3384
cf22909c
KL
3385 push_deferring_access_checks (true);
3386
a723baf1
MM
3387 while (true)
3388 {
3389 tree new_scope;
3390 tree old_scope;
3391 tree saved_qualifying_scope;
a723baf1
MM
3392 bool template_keyword_p;
3393
2050a1bb
MM
3394 /* Spot cases that cannot be the beginning of a
3395 nested-name-specifier. */
3396 token = cp_lexer_peek_token (parser->lexer);
3397
3398 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3399 the already parsed nested-name-specifier. */
3400 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3401 {
3402 /* Grab the nested-name-specifier and continue the loop. */
3403 cp_parser_pre_parsed_nested_name_specifier (parser);
3404 success = true;
3405 continue;
3406 }
3407
a723baf1
MM
3408 /* Spot cases that cannot be the beginning of a
3409 nested-name-specifier. On the second and subsequent times
3410 through the loop, we look for the `template' keyword. */
f7b5ecd9 3411 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3412 ;
3413 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3414 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3415 ;
3416 else
3417 {
3418 /* If the next token is not an identifier, then it is
3419 definitely not a class-or-namespace-name. */
f7b5ecd9 3420 if (token->type != CPP_NAME)
a723baf1
MM
3421 break;
3422 /* If the following token is neither a `<' (to begin a
3423 template-id), nor a `::', then we are not looking at a
3424 nested-name-specifier. */
3425 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3426 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3427 break;
3428 }
3429
3430 /* The nested-name-specifier is optional, so we parse
3431 tentatively. */
3432 cp_parser_parse_tentatively (parser);
3433
3434 /* Look for the optional `template' keyword, if this isn't the
3435 first time through the loop. */
3436 if (success)
3437 template_keyword_p = cp_parser_optional_template_keyword (parser);
3438 else
3439 template_keyword_p = false;
3440
3441 /* Save the old scope since the name lookup we are about to do
3442 might destroy it. */
3443 old_scope = parser->scope;
3444 saved_qualifying_scope = parser->qualifying_scope;
3445 /* Parse the qualifying entity. */
3446 new_scope
3447 = cp_parser_class_or_namespace_name (parser,
3448 typename_keyword_p,
3449 template_keyword_p,
3450 check_dependency_p,
3451 type_p);
3452 /* Look for the `::' token. */
3453 cp_parser_require (parser, CPP_SCOPE, "`::'");
3454
3455 /* If we found what we wanted, we keep going; otherwise, we're
3456 done. */
3457 if (!cp_parser_parse_definitely (parser))
3458 {
3459 bool error_p = false;
3460
3461 /* Restore the OLD_SCOPE since it was valid before the
3462 failed attempt at finding the last
3463 class-or-namespace-name. */
3464 parser->scope = old_scope;
3465 parser->qualifying_scope = saved_qualifying_scope;
3466 /* If the next token is an identifier, and the one after
3467 that is a `::', then any valid interpretation would have
3468 found a class-or-namespace-name. */
3469 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3470 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3471 == CPP_SCOPE)
3472 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3473 != CPP_COMPL))
3474 {
3475 token = cp_lexer_consume_token (parser->lexer);
3476 if (!error_p)
3477 {
3478 tree decl;
3479
3480 decl = cp_parser_lookup_name_simple (parser, token->value);
3481 if (TREE_CODE (decl) == TEMPLATE_DECL)
3482 error ("`%D' used without template parameters",
3483 decl);
3484 else if (parser->scope)
3485 {
3486 if (TYPE_P (parser->scope))
3487 error ("`%T::%D' is not a class-name or "
3488 "namespace-name",
3489 parser->scope, token->value);
3490 else
3491 error ("`%D::%D' is not a class-name or "
3492 "namespace-name",
3493 parser->scope, token->value);
3494 }
3495 else
3496 error ("`%D' is not a class-name or namespace-name",
3497 token->value);
3498 parser->scope = NULL_TREE;
3499 error_p = true;
eea9800f
MM
3500 /* Treat this as a successful nested-name-specifier
3501 due to:
3502
3503 [basic.lookup.qual]
3504
3505 If the name found is not a class-name (clause
3506 _class_) or namespace-name (_namespace.def_), the
3507 program is ill-formed. */
3508 success = true;
a723baf1
MM
3509 }
3510 cp_lexer_consume_token (parser->lexer);
3511 }
3512 break;
3513 }
3514
3515 /* We've found one valid nested-name-specifier. */
3516 success = true;
3517 /* Make sure we look in the right scope the next time through
3518 the loop. */
3519 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3520 ? TREE_TYPE (new_scope)
3521 : new_scope);
3522 /* If it is a class scope, try to complete it; we are about to
3523 be looking up names inside the class. */
3524 if (TYPE_P (parser->scope))
3525 complete_type (parser->scope);
3526 }
3527
cf22909c
KL
3528 /* Retrieve any deferred checks. Do not pop this access checks yet
3529 so the memory will not be reclaimed during token replacing below. */
3530 access_check = get_deferred_access_checks ();
3531
a723baf1
MM
3532 /* If parsing tentatively, replace the sequence of tokens that makes
3533 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3534 token. That way, should we re-parse the token stream, we will
3535 not have to repeat the effort required to do the parse, nor will
3536 we issue duplicate error messages. */
3537 if (success && start >= 0)
3538 {
a723baf1
MM
3539 /* Find the token that corresponds to the start of the
3540 template-id. */
3541 token = cp_lexer_advance_token (parser->lexer,
3542 parser->lexer->first_token,
3543 start);
3544
a723baf1
MM
3545 /* Reset the contents of the START token. */
3546 token->type = CPP_NESTED_NAME_SPECIFIER;
3547 token->value = build_tree_list (access_check, parser->scope);
3548 TREE_TYPE (token->value) = parser->qualifying_scope;
3549 token->keyword = RID_MAX;
3550 /* Purge all subsequent tokens. */
3551 cp_lexer_purge_tokens_after (parser->lexer, token);
3552 }
3553
cf22909c 3554 pop_deferring_access_checks ();
a723baf1
MM
3555 return success ? parser->scope : NULL_TREE;
3556}
3557
3558/* Parse a nested-name-specifier. See
3559 cp_parser_nested_name_specifier_opt for details. This function
3560 behaves identically, except that it will an issue an error if no
3561 nested-name-specifier is present, and it will return
3562 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3563 is present. */
3564
3565static tree
3566cp_parser_nested_name_specifier (cp_parser *parser,
3567 bool typename_keyword_p,
3568 bool check_dependency_p,
3569 bool type_p)
3570{
3571 tree scope;
3572
3573 /* Look for the nested-name-specifier. */
3574 scope = cp_parser_nested_name_specifier_opt (parser,
3575 typename_keyword_p,
3576 check_dependency_p,
3577 type_p);
3578 /* If it was not present, issue an error message. */
3579 if (!scope)
3580 {
3581 cp_parser_error (parser, "expected nested-name-specifier");
3582 return error_mark_node;
3583 }
3584
3585 return scope;
3586}
3587
3588/* Parse a class-or-namespace-name.
3589
3590 class-or-namespace-name:
3591 class-name
3592 namespace-name
3593
3594 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3595 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3596 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3597 TYPE_P is TRUE iff the next name should be taken as a class-name,
3598 even the same name is declared to be another entity in the same
3599 scope.
3600
3601 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3602 specified by the class-or-namespace-name. If neither is found the
3603 ERROR_MARK_NODE is returned. */
a723baf1
MM
3604
3605static tree
3606cp_parser_class_or_namespace_name (cp_parser *parser,
3607 bool typename_keyword_p,
3608 bool template_keyword_p,
3609 bool check_dependency_p,
3610 bool type_p)
3611{
3612 tree saved_scope;
3613 tree saved_qualifying_scope;
3614 tree saved_object_scope;
3615 tree scope;
eea9800f 3616 bool only_class_p;
a723baf1
MM
3617
3618 /* If the next token is the `template' keyword, we know that we are
3619 looking at a class-name. */
3620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3621 return cp_parser_class_name (parser,
3622 typename_keyword_p,
3623 template_keyword_p,
3624 type_p,
3625 /*check_access_p=*/true,
3626 check_dependency_p,
3627 /*class_head_p=*/false);
3628 /* Before we try to parse the class-name, we must save away the
3629 current PARSER->SCOPE since cp_parser_class_name will destroy
3630 it. */
3631 saved_scope = parser->scope;
3632 saved_qualifying_scope = parser->qualifying_scope;
3633 saved_object_scope = parser->object_scope;
eea9800f
MM
3634 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3635 there is no need to look for a namespace-name. */
3636 only_class_p = saved_scope && TYPE_P (saved_scope);
3637 if (!only_class_p)
3638 cp_parser_parse_tentatively (parser);
a723baf1
MM
3639 scope = cp_parser_class_name (parser,
3640 typename_keyword_p,
3641 template_keyword_p,
3642 type_p,
3643 /*check_access_p=*/true,
3644 check_dependency_p,
3645 /*class_head_p=*/false);
3646 /* If that didn't work, try for a namespace-name. */
eea9800f 3647 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3648 {
3649 /* Restore the saved scope. */
3650 parser->scope = saved_scope;
3651 parser->qualifying_scope = saved_qualifying_scope;
3652 parser->object_scope = saved_object_scope;
eea9800f
MM
3653 /* If we are not looking at an identifier followed by the scope
3654 resolution operator, then this is not part of a
3655 nested-name-specifier. (Note that this function is only used
3656 to parse the components of a nested-name-specifier.) */
3657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3658 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3659 return error_mark_node;
a723baf1
MM
3660 scope = cp_parser_namespace_name (parser);
3661 }
3662
3663 return scope;
3664}
3665
3666/* Parse a postfix-expression.
3667
3668 postfix-expression:
3669 primary-expression
3670 postfix-expression [ expression ]
3671 postfix-expression ( expression-list [opt] )
3672 simple-type-specifier ( expression-list [opt] )
3673 typename :: [opt] nested-name-specifier identifier
3674 ( expression-list [opt] )
3675 typename :: [opt] nested-name-specifier template [opt] template-id
3676 ( expression-list [opt] )
3677 postfix-expression . template [opt] id-expression
3678 postfix-expression -> template [opt] id-expression
3679 postfix-expression . pseudo-destructor-name
3680 postfix-expression -> pseudo-destructor-name
3681 postfix-expression ++
3682 postfix-expression --
3683 dynamic_cast < type-id > ( expression )
3684 static_cast < type-id > ( expression )
3685 reinterpret_cast < type-id > ( expression )
3686 const_cast < type-id > ( expression )
3687 typeid ( expression )
3688 typeid ( type-id )
3689
3690 GNU Extension:
3691
3692 postfix-expression:
3693 ( type-id ) { initializer-list , [opt] }
3694
3695 This extension is a GNU version of the C99 compound-literal
3696 construct. (The C99 grammar uses `type-name' instead of `type-id',
3697 but they are essentially the same concept.)
3698
3699 If ADDRESS_P is true, the postfix expression is the operand of the
3700 `&' operator.
3701
3702 Returns a representation of the expression. */
3703
3704static tree
3705cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3706{
3707 cp_token *token;
3708 enum rid keyword;
3709 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3710 tree postfix_expression = NULL_TREE;
3711 /* Non-NULL only if the current postfix-expression can be used to
3712 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3713 class used to qualify the member. */
3714 tree qualifying_class = NULL_TREE;
3715 bool done;
3716
3717 /* Peek at the next token. */
3718 token = cp_lexer_peek_token (parser->lexer);
3719 /* Some of the productions are determined by keywords. */
3720 keyword = token->keyword;
3721 switch (keyword)
3722 {
3723 case RID_DYNCAST:
3724 case RID_STATCAST:
3725 case RID_REINTCAST:
3726 case RID_CONSTCAST:
3727 {
3728 tree type;
3729 tree expression;
3730 const char *saved_message;
3731
3732 /* All of these can be handled in the same way from the point
3733 of view of parsing. Begin by consuming the token
3734 identifying the cast. */
3735 cp_lexer_consume_token (parser->lexer);
3736
3737 /* New types cannot be defined in the cast. */
3738 saved_message = parser->type_definition_forbidden_message;
3739 parser->type_definition_forbidden_message
3740 = "types may not be defined in casts";
3741
3742 /* Look for the opening `<'. */
3743 cp_parser_require (parser, CPP_LESS, "`<'");
3744 /* Parse the type to which we are casting. */
3745 type = cp_parser_type_id (parser);
3746 /* Look for the closing `>'. */
3747 cp_parser_require (parser, CPP_GREATER, "`>'");
3748 /* Restore the old message. */
3749 parser->type_definition_forbidden_message = saved_message;
3750
3751 /* And the expression which is being cast. */
3752 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3753 expression = cp_parser_expression (parser);
3754 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3755
3756 switch (keyword)
3757 {
3758 case RID_DYNCAST:
3759 postfix_expression
3760 = build_dynamic_cast (type, expression);
3761 break;
3762 case RID_STATCAST:
3763 postfix_expression
3764 = build_static_cast (type, expression);
3765 break;
3766 case RID_REINTCAST:
3767 postfix_expression
3768 = build_reinterpret_cast (type, expression);
3769 break;
3770 case RID_CONSTCAST:
3771 postfix_expression
3772 = build_const_cast (type, expression);
3773 break;
3774 default:
3775 abort ();
3776 }
3777 }
3778 break;
3779
3780 case RID_TYPEID:
3781 {
3782 tree type;
3783 const char *saved_message;
3784
3785 /* Consume the `typeid' token. */
3786 cp_lexer_consume_token (parser->lexer);
3787 /* Look for the `(' token. */
3788 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3789 /* Types cannot be defined in a `typeid' expression. */
3790 saved_message = parser->type_definition_forbidden_message;
3791 parser->type_definition_forbidden_message
3792 = "types may not be defined in a `typeid\' expression";
3793 /* We can't be sure yet whether we're looking at a type-id or an
3794 expression. */
3795 cp_parser_parse_tentatively (parser);
3796 /* Try a type-id first. */
3797 type = cp_parser_type_id (parser);
3798 /* Look for the `)' token. Otherwise, we can't be sure that
3799 we're not looking at an expression: consider `typeid (int
3800 (3))', for example. */
3801 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3802 /* If all went well, simply lookup the type-id. */
3803 if (cp_parser_parse_definitely (parser))
3804 postfix_expression = get_typeid (type);
3805 /* Otherwise, fall back to the expression variant. */
3806 else
3807 {
3808 tree expression;
3809
3810 /* Look for an expression. */
3811 expression = cp_parser_expression (parser);
3812 /* Compute its typeid. */
3813 postfix_expression = build_typeid (expression);
3814 /* Look for the `)' token. */
3815 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3816 }
3817
3818 /* Restore the saved message. */
3819 parser->type_definition_forbidden_message = saved_message;
3820 }
3821 break;
3822
3823 case RID_TYPENAME:
3824 {
3825 bool template_p = false;
3826 tree id;
3827 tree type;
3828
3829 /* Consume the `typename' token. */
3830 cp_lexer_consume_token (parser->lexer);
3831 /* Look for the optional `::' operator. */
3832 cp_parser_global_scope_opt (parser,
3833 /*current_scope_valid_p=*/false);
3834 /* Look for the nested-name-specifier. */
3835 cp_parser_nested_name_specifier (parser,
3836 /*typename_keyword_p=*/true,
3837 /*check_dependency_p=*/true,
3838 /*type_p=*/true);
3839 /* Look for the optional `template' keyword. */
3840 template_p = cp_parser_optional_template_keyword (parser);
3841 /* We don't know whether we're looking at a template-id or an
3842 identifier. */
3843 cp_parser_parse_tentatively (parser);
3844 /* Try a template-id. */
3845 id = cp_parser_template_id (parser, template_p,
3846 /*check_dependency_p=*/true);
3847 /* If that didn't work, try an identifier. */
3848 if (!cp_parser_parse_definitely (parser))
3849 id = cp_parser_identifier (parser);
3850 /* Create a TYPENAME_TYPE to represent the type to which the
3851 functional cast is being performed. */
3852 type = make_typename_type (parser->scope, id,
3853 /*complain=*/1);
3854
3855 postfix_expression = cp_parser_functional_cast (parser, type);
3856 }
3857 break;
3858
3859 default:
3860 {
3861 tree type;
3862
3863 /* If the next thing is a simple-type-specifier, we may be
3864 looking at a functional cast. We could also be looking at
3865 an id-expression. So, we try the functional cast, and if
3866 that doesn't work we fall back to the primary-expression. */
3867 cp_parser_parse_tentatively (parser);
3868 /* Look for the simple-type-specifier. */
3869 type = cp_parser_simple_type_specifier (parser,
3870 CP_PARSER_FLAGS_NONE);
3871 /* Parse the cast itself. */
3872 if (!cp_parser_error_occurred (parser))
3873 postfix_expression
3874 = cp_parser_functional_cast (parser, type);
3875 /* If that worked, we're done. */
3876 if (cp_parser_parse_definitely (parser))
3877 break;
3878
3879 /* If the functional-cast didn't work out, try a
3880 compound-literal. */
3881 if (cp_parser_allow_gnu_extensions_p (parser))
3882 {
3883 tree initializer_list = NULL_TREE;
3884
3885 cp_parser_parse_tentatively (parser);
3886 /* Look for the `('. */
3887 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3888 {
3889 type = cp_parser_type_id (parser);
3890 /* Look for the `)'. */
3891 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3892 /* Look for the `{'. */
3893 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3894 /* If things aren't going well, there's no need to
3895 keep going. */
3896 if (!cp_parser_error_occurred (parser))
3897 {
3898 /* Parse the initializer-list. */
3899 initializer_list
3900 = cp_parser_initializer_list (parser);
3901 /* Allow a trailing `,'. */
3902 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3903 cp_lexer_consume_token (parser->lexer);
3904 /* Look for the final `}'. */
3905 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3906 }
3907 }
3908 /* If that worked, we're definitely looking at a
3909 compound-literal expression. */
3910 if (cp_parser_parse_definitely (parser))
3911 {
3912 /* Warn the user that a compound literal is not
3913 allowed in standard C++. */
3914 if (pedantic)
3915 pedwarn ("ISO C++ forbids compound-literals");
3916 /* Form the representation of the compound-literal. */
3917 postfix_expression
3918 = finish_compound_literal (type, initializer_list);
3919 break;
3920 }
3921 }
3922
3923 /* It must be a primary-expression. */
3924 postfix_expression = cp_parser_primary_expression (parser,
3925 &idk,
3926 &qualifying_class);
3927 }
3928 break;
3929 }
3930
3931 /* Peek at the next token. */
3932 token = cp_lexer_peek_token (parser->lexer);
3933 done = (token->type != CPP_OPEN_SQUARE
3934 && token->type != CPP_OPEN_PAREN
3935 && token->type != CPP_DOT
3936 && token->type != CPP_DEREF
3937 && token->type != CPP_PLUS_PLUS
3938 && token->type != CPP_MINUS_MINUS);
3939
3940 /* If the postfix expression is complete, finish up. */
3941 if (address_p && qualifying_class && done)
3942 {
3943 if (TREE_CODE (postfix_expression) == SCOPE_REF)
3944 postfix_expression = TREE_OPERAND (postfix_expression, 1);
3945 postfix_expression
3946 = build_offset_ref (qualifying_class, postfix_expression);
3947 return postfix_expression;
3948 }
3949
3950 /* Otherwise, if we were avoiding committing until we knew
3951 whether or not we had a pointer-to-member, we now know that
3952 the expression is an ordinary reference to a qualified name. */
3953 if (qualifying_class && !processing_template_decl)
3954 {
3955 if (TREE_CODE (postfix_expression) == FIELD_DECL)
3956 postfix_expression
3957 = finish_non_static_data_member (postfix_expression,
3958 qualifying_class);
3959 else if (BASELINK_P (postfix_expression))
3960 {
3961 tree fn;
3962 tree fns;
3963
3964 /* See if any of the functions are non-static members. */
3965 fns = BASELINK_FUNCTIONS (postfix_expression);
3966 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3967 fns = TREE_OPERAND (fns, 0);
3968 for (fn = fns; fn; fn = OVL_NEXT (fn))
3969 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3970 break;
3971 /* If so, the expression may be relative to the current
3972 class. */
3973 if (fn && current_class_type
3974 && DERIVED_FROM_P (qualifying_class, current_class_type))
3975 postfix_expression
3976 = (build_class_member_access_expr
3977 (maybe_dummy_object (qualifying_class, NULL),
3978 postfix_expression,
3979 BASELINK_ACCESS_BINFO (postfix_expression),
3980 /*preserve_reference=*/false));
3981 else if (done)
3982 return build_offset_ref (qualifying_class,
3983 postfix_expression);
3984 }
3985 }
3986
3987 /* Remember that there was a reference to this entity. */
3988 if (DECL_P (postfix_expression))
3989 mark_used (postfix_expression);
3990
3991 /* Keep looping until the postfix-expression is complete. */
3992 while (true)
3993 {
3994 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3995 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3996 {
3997 /* It is not a Koenig lookup function call. */
3998 unqualified_name_lookup_error (postfix_expression);
3999 postfix_expression = error_mark_node;
4000 }
4001
4002 /* Peek at the next token. */
4003 token = cp_lexer_peek_token (parser->lexer);
4004
4005 switch (token->type)
4006 {
4007 case CPP_OPEN_SQUARE:
4008 /* postfix-expression [ expression ] */
4009 {
4010 tree index;
4011
4012 /* Consume the `[' token. */
4013 cp_lexer_consume_token (parser->lexer);
4014 /* Parse the index expression. */
4015 index = cp_parser_expression (parser);
4016 /* Look for the closing `]'. */
4017 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4018
4019 /* Build the ARRAY_REF. */
4020 postfix_expression
4021 = grok_array_decl (postfix_expression, index);
4022 idk = CP_PARSER_ID_KIND_NONE;
4023 }
4024 break;
4025
4026 case CPP_OPEN_PAREN:
4027 /* postfix-expression ( expression-list [opt] ) */
4028 {
4029 tree args;
4030
4031 /* Consume the `(' token. */
4032 cp_lexer_consume_token (parser->lexer);
4033 /* If the next token is not a `)', then there are some
4034 arguments. */
4035 if (cp_lexer_next_token_is_not (parser->lexer,
4036 CPP_CLOSE_PAREN))
4037 args = cp_parser_expression_list (parser);
4038 else
4039 args = NULL_TREE;
4040 /* Look for the closing `)'. */
4041 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4042
4043 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4044 && (is_overloaded_fn (postfix_expression)
4045 || DECL_P (postfix_expression)
4046 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4047 && args)
4048 {
4049 tree arg;
4050 tree identifier = NULL_TREE;
4051 tree functions = NULL_TREE;
4052
4053 /* Find the name of the overloaded function. */
4054 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4055 identifier = postfix_expression;
4056 else if (is_overloaded_fn (postfix_expression))
4057 {
4058 functions = postfix_expression;
4059 identifier = DECL_NAME (get_first_fn (functions));
4060 }
4061 else if (DECL_P (postfix_expression))
4062 {
4063 functions = postfix_expression;
4064 identifier = DECL_NAME (postfix_expression);
4065 }
4066
4067 /* A call to a namespace-scope function using an
4068 unqualified name.
4069
4070 Do Koenig lookup -- unless any of the arguments are
4071 type-dependent. */
4072 for (arg = args; arg; arg = TREE_CHAIN (arg))
4073 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4074 break;
4075 if (!arg)
4076 {
4077 postfix_expression
4078 = lookup_arg_dependent(identifier, functions, args);
4079 if (!postfix_expression)
4080 {
4081 /* The unqualified name could not be resolved. */
4082 unqualified_name_lookup_error (identifier);
4083 postfix_expression = error_mark_node;
4084 }
4085 postfix_expression
4086 = build_call_from_tree (postfix_expression, args,
4087 /*diallow_virtual=*/false);
4088 break;
4089 }
4090 postfix_expression = build_min_nt (LOOKUP_EXPR,
4091 identifier);
4092 }
4093 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4094 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4095 {
4096 /* The unqualified name could not be resolved. */
4097 unqualified_name_lookup_error (postfix_expression);
4098 postfix_expression = error_mark_node;
4099 break;
4100 }
4101
4102 /* In the body of a template, no further processing is
4103 required. */
4104 if (processing_template_decl)
4105 {
4106 postfix_expression = build_nt (CALL_EXPR,
4107 postfix_expression,
4108 args);
4109 break;
4110 }
4111
4112 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4113 postfix_expression
4114 = (build_new_method_call
4115 (TREE_OPERAND (postfix_expression, 0),
4116 TREE_OPERAND (postfix_expression, 1),
4117 args, NULL_TREE,
4118 (idk == CP_PARSER_ID_KIND_QUALIFIED
4119 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4120 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4121 postfix_expression = (build_offset_ref_call_from_tree
4122 (postfix_expression, args));
4123 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
2050a1bb
MM
4124 /* A call to a static class member, or a namespace-scope
4125 function. */
4126 postfix_expression
4127 = finish_call_expr (postfix_expression, args,
4128 /*disallow_virtual=*/true);
a723baf1 4129 else
2050a1bb
MM
4130 /* All other function calls. */
4131 postfix_expression
4132 = finish_call_expr (postfix_expression, args,
4133 /*disallow_virtual=*/false);
a723baf1
MM
4134
4135 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4136 idk = CP_PARSER_ID_KIND_NONE;
4137 }
4138 break;
4139
4140 case CPP_DOT:
4141 case CPP_DEREF:
4142 /* postfix-expression . template [opt] id-expression
4143 postfix-expression . pseudo-destructor-name
4144 postfix-expression -> template [opt] id-expression
4145 postfix-expression -> pseudo-destructor-name */
4146 {
4147 tree name;
4148 bool dependent_p;
4149 bool template_p;
4150 tree scope = NULL_TREE;
4151
4152 /* If this is a `->' operator, dereference the pointer. */
4153 if (token->type == CPP_DEREF)
4154 postfix_expression = build_x_arrow (postfix_expression);
4155 /* Check to see whether or not the expression is
4156 type-dependent. */
4157 dependent_p = (cp_parser_type_dependent_expression_p
4158 (postfix_expression));
4159 /* The identifier following the `->' or `.' is not
4160 qualified. */
4161 parser->scope = NULL_TREE;
4162 parser->qualifying_scope = NULL_TREE;
4163 parser->object_scope = NULL_TREE;
4164 /* Enter the scope corresponding to the type of the object
4165 given by the POSTFIX_EXPRESSION. */
4166 if (!dependent_p
4167 && TREE_TYPE (postfix_expression) != NULL_TREE)
4168 {
4169 scope = TREE_TYPE (postfix_expression);
4170 /* According to the standard, no expression should
4171 ever have reference type. Unfortunately, we do not
4172 currently match the standard in this respect in
4173 that our internal representation of an expression
4174 may have reference type even when the standard says
4175 it does not. Therefore, we have to manually obtain
4176 the underlying type here. */
4177 if (TREE_CODE (scope) == REFERENCE_TYPE)
4178 scope = TREE_TYPE (scope);
4179 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4180 type of the field. We get an OFFSET_TYPE for
4181 something like:
4182
4183 S::T.a ...
4184
4185 Probably, we should not get an OFFSET_TYPE here;
4186 that transformation should be made only if `&S::T'
4187 is written. */
4188 if (TREE_CODE (scope) == OFFSET_TYPE)
4189 scope = TREE_TYPE (scope);
4190 /* The type of the POSTFIX_EXPRESSION must be
4191 complete. */
4192 scope = complete_type_or_else (scope, NULL_TREE);
4193 /* Let the name lookup machinery know that we are
4194 processing a class member access expression. */
4195 parser->context->object_type = scope;
4196 /* If something went wrong, we want to be able to
4197 discern that case, as opposed to the case where
4198 there was no SCOPE due to the type of expression
4199 being dependent. */
4200 if (!scope)
4201 scope = error_mark_node;
4202 }
4203
4204 /* Consume the `.' or `->' operator. */
4205 cp_lexer_consume_token (parser->lexer);
4206 /* If the SCOPE is not a scalar type, we are looking at an
4207 ordinary class member access expression, rather than a
4208 pseudo-destructor-name. */
4209 if (!scope || !SCALAR_TYPE_P (scope))
4210 {
4211 template_p = cp_parser_optional_template_keyword (parser);
4212 /* Parse the id-expression. */
4213 name = cp_parser_id_expression (parser,
4214 template_p,
4215 /*check_dependency_p=*/true,
4216 /*template_p=*/NULL);
4217 /* In general, build a SCOPE_REF if the member name is
4218 qualified. However, if the name was not dependent
4219 and has already been resolved; there is no need to
4220 build the SCOPE_REF. For example;
4221
4222 struct X { void f(); };
4223 template <typename T> void f(T* t) { t->X::f(); }
4224
4225 Even though "t" is dependent, "X::f" is not and has
4226 except that for a BASELINK there is no need to
4227 include scope information. */
4228 if (name != error_mark_node
4229 && !BASELINK_P (name)
4230 && parser->scope)
4231 {
4232 name = build_nt (SCOPE_REF, parser->scope, name);
4233 parser->scope = NULL_TREE;
4234 parser->qualifying_scope = NULL_TREE;
4235 parser->object_scope = NULL_TREE;
4236 }
4237 postfix_expression
4238 = finish_class_member_access_expr (postfix_expression, name);
4239 }
4240 /* Otherwise, try the pseudo-destructor-name production. */
4241 else
4242 {
4243 tree s;
4244 tree type;
4245
4246 /* Parse the pseudo-destructor-name. */
4247 cp_parser_pseudo_destructor_name (parser, &s, &type);
4248 /* Form the call. */
4249 postfix_expression
4250 = finish_pseudo_destructor_expr (postfix_expression,
4251 s, TREE_TYPE (type));
4252 }
4253
4254 /* We no longer need to look up names in the scope of the
4255 object on the left-hand side of the `.' or `->'
4256 operator. */
4257 parser->context->object_type = NULL_TREE;
4258 idk = CP_PARSER_ID_KIND_NONE;
4259 }
4260 break;
4261
4262 case CPP_PLUS_PLUS:
4263 /* postfix-expression ++ */
4264 /* Consume the `++' token. */
4265 cp_lexer_consume_token (parser->lexer);
4266 /* Generate a reprsentation for the complete expression. */
4267 postfix_expression
4268 = finish_increment_expr (postfix_expression,
4269 POSTINCREMENT_EXPR);
4270 idk = CP_PARSER_ID_KIND_NONE;
4271 break;
4272
4273 case CPP_MINUS_MINUS:
4274 /* postfix-expression -- */
4275 /* Consume the `--' token. */
4276 cp_lexer_consume_token (parser->lexer);
4277 /* Generate a reprsentation for the complete expression. */
4278 postfix_expression
4279 = finish_increment_expr (postfix_expression,
4280 POSTDECREMENT_EXPR);
4281 idk = CP_PARSER_ID_KIND_NONE;
4282 break;
4283
4284 default:
4285 return postfix_expression;
4286 }
4287 }
4288
4289 /* We should never get here. */
4290 abort ();
4291 return error_mark_node;
4292}
4293
4294/* Parse an expression-list.
4295
4296 expression-list:
4297 assignment-expression
4298 expression-list, assignment-expression
4299
4300 Returns a TREE_LIST. The TREE_VALUE of each node is a
4301 representation of an assignment-expression. Note that a TREE_LIST
4302 is returned even if there is only a single expression in the list. */
4303
4304static tree
4305cp_parser_expression_list (parser)
4306 cp_parser *parser;
4307{
4308 tree expression_list = NULL_TREE;
4309
4310 /* Consume expressions until there are no more. */
4311 while (true)
4312 {
4313 tree expr;
4314
4315 /* Parse the next assignment-expression. */
4316 expr = cp_parser_assignment_expression (parser);
4317 /* Add it to the list. */
4318 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4319
4320 /* If the next token isn't a `,', then we are done. */
4321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4322 {
4323 /* All uses of expression-list in the grammar are followed
4324 by a `)'. Therefore, if the next token is not a `)' an
4325 error will be issued, unless we are parsing tentatively.
4326 Skip ahead to see if there is another `,' before the `)';
4327 if so, we can go there and recover. */
4328 if (cp_parser_parsing_tentatively (parser)
4329 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4330 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4331 break;
4332 }
4333
4334 /* Otherwise, consume the `,' and keep going. */
4335 cp_lexer_consume_token (parser->lexer);
4336 }
4337
4338 /* We built up the list in reverse order so we must reverse it now. */
4339 return nreverse (expression_list);
4340}
4341
4342/* Parse a pseudo-destructor-name.
4343
4344 pseudo-destructor-name:
4345 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4346 :: [opt] nested-name-specifier template template-id :: ~ type-name
4347 :: [opt] nested-name-specifier [opt] ~ type-name
4348
4349 If either of the first two productions is used, sets *SCOPE to the
4350 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4351 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4352 or ERROR_MARK_NODE if no type-name is present. */
4353
4354static void
4355cp_parser_pseudo_destructor_name (parser, scope, type)
4356 cp_parser *parser;
4357 tree *scope;
4358 tree *type;
4359{
4360 bool nested_name_specifier_p;
4361
4362 /* Look for the optional `::' operator. */
4363 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4364 /* Look for the optional nested-name-specifier. */
4365 nested_name_specifier_p
4366 = (cp_parser_nested_name_specifier_opt (parser,
4367 /*typename_keyword_p=*/false,
4368 /*check_dependency_p=*/true,
4369 /*type_p=*/false)
4370 != NULL_TREE);
4371 /* Now, if we saw a nested-name-specifier, we might be doing the
4372 second production. */
4373 if (nested_name_specifier_p
4374 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4375 {
4376 /* Consume the `template' keyword. */
4377 cp_lexer_consume_token (parser->lexer);
4378 /* Parse the template-id. */
4379 cp_parser_template_id (parser,
4380 /*template_keyword_p=*/true,
4381 /*check_dependency_p=*/false);
4382 /* Look for the `::' token. */
4383 cp_parser_require (parser, CPP_SCOPE, "`::'");
4384 }
4385 /* If the next token is not a `~', then there might be some
4386 additional qualification. */
4387 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4388 {
4389 /* Look for the type-name. */
4390 *scope = TREE_TYPE (cp_parser_type_name (parser));
4391 /* Look for the `::' token. */
4392 cp_parser_require (parser, CPP_SCOPE, "`::'");
4393 }
4394 else
4395 *scope = NULL_TREE;
4396
4397 /* Look for the `~'. */
4398 cp_parser_require (parser, CPP_COMPL, "`~'");
4399 /* Look for the type-name again. We are not responsible for
4400 checking that it matches the first type-name. */
4401 *type = cp_parser_type_name (parser);
4402}
4403
4404/* Parse a unary-expression.
4405
4406 unary-expression:
4407 postfix-expression
4408 ++ cast-expression
4409 -- cast-expression
4410 unary-operator cast-expression
4411 sizeof unary-expression
4412 sizeof ( type-id )
4413 new-expression
4414 delete-expression
4415
4416 GNU Extensions:
4417
4418 unary-expression:
4419 __extension__ cast-expression
4420 __alignof__ unary-expression
4421 __alignof__ ( type-id )
4422 __real__ cast-expression
4423 __imag__ cast-expression
4424 && identifier
4425
4426 ADDRESS_P is true iff the unary-expression is appearing as the
4427 operand of the `&' operator.
4428
4429 Returns a representation of the expresion. */
4430
4431static tree
4432cp_parser_unary_expression (cp_parser *parser, bool address_p)
4433{
4434 cp_token *token;
4435 enum tree_code unary_operator;
4436
4437 /* Peek at the next token. */
4438 token = cp_lexer_peek_token (parser->lexer);
4439 /* Some keywords give away the kind of expression. */
4440 if (token->type == CPP_KEYWORD)
4441 {
4442 enum rid keyword = token->keyword;
4443
4444 switch (keyword)
4445 {
4446 case RID_ALIGNOF:
4447 {
4448 /* Consume the `alignof' token. */
4449 cp_lexer_consume_token (parser->lexer);
4450 /* Parse the operand. */
4451 return finish_alignof (cp_parser_sizeof_operand
4452 (parser, keyword));
4453 }
4454
4455 case RID_SIZEOF:
4456 {
4457 tree operand;
4458
4459 /* Consume the `sizeof' token. */
4460 cp_lexer_consume_token (parser->lexer);
4461 /* Parse the operand. */
4462 operand = cp_parser_sizeof_operand (parser, keyword);
4463
4464 /* If the type of the operand cannot be determined build a
4465 SIZEOF_EXPR. */
4466 if (TYPE_P (operand)
4467 ? cp_parser_dependent_type_p (operand)
4468 : cp_parser_type_dependent_expression_p (operand))
4469 return build_min (SIZEOF_EXPR, size_type_node, operand);
4470 /* Otherwise, compute the constant value. */
4471 else
4472 return finish_sizeof (operand);
4473 }
4474
4475 case RID_NEW:
4476 return cp_parser_new_expression (parser);
4477
4478 case RID_DELETE:
4479 return cp_parser_delete_expression (parser);
4480
4481 case RID_EXTENSION:
4482 {
4483 /* The saved value of the PEDANTIC flag. */
4484 int saved_pedantic;
4485 tree expr;
4486
4487 /* Save away the PEDANTIC flag. */
4488 cp_parser_extension_opt (parser, &saved_pedantic);
4489 /* Parse the cast-expression. */
4490 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4491 /* Restore the PEDANTIC flag. */
4492 pedantic = saved_pedantic;
4493
4494 return expr;
4495 }
4496
4497 case RID_REALPART:
4498 case RID_IMAGPART:
4499 {
4500 tree expression;
4501
4502 /* Consume the `__real__' or `__imag__' token. */
4503 cp_lexer_consume_token (parser->lexer);
4504 /* Parse the cast-expression. */
4505 expression = cp_parser_cast_expression (parser,
4506 /*address_p=*/false);
4507 /* Create the complete representation. */
4508 return build_x_unary_op ((keyword == RID_REALPART
4509 ? REALPART_EXPR : IMAGPART_EXPR),
4510 expression);
4511 }
4512 break;
4513
4514 default:
4515 break;
4516 }
4517 }
4518
4519 /* Look for the `:: new' and `:: delete', which also signal the
4520 beginning of a new-expression, or delete-expression,
4521 respectively. If the next token is `::', then it might be one of
4522 these. */
4523 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4524 {
4525 enum rid keyword;
4526
4527 /* See if the token after the `::' is one of the keywords in
4528 which we're interested. */
4529 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4530 /* If it's `new', we have a new-expression. */
4531 if (keyword == RID_NEW)
4532 return cp_parser_new_expression (parser);
4533 /* Similarly, for `delete'. */
4534 else if (keyword == RID_DELETE)
4535 return cp_parser_delete_expression (parser);
4536 }
4537
4538 /* Look for a unary operator. */
4539 unary_operator = cp_parser_unary_operator (token);
4540 /* The `++' and `--' operators can be handled similarly, even though
4541 they are not technically unary-operators in the grammar. */
4542 if (unary_operator == ERROR_MARK)
4543 {
4544 if (token->type == CPP_PLUS_PLUS)
4545 unary_operator = PREINCREMENT_EXPR;
4546 else if (token->type == CPP_MINUS_MINUS)
4547 unary_operator = PREDECREMENT_EXPR;
4548 /* Handle the GNU address-of-label extension. */
4549 else if (cp_parser_allow_gnu_extensions_p (parser)
4550 && token->type == CPP_AND_AND)
4551 {
4552 tree identifier;
4553
4554 /* Consume the '&&' token. */
4555 cp_lexer_consume_token (parser->lexer);
4556 /* Look for the identifier. */
4557 identifier = cp_parser_identifier (parser);
4558 /* Create an expression representing the address. */
4559 return finish_label_address_expr (identifier);
4560 }
4561 }
4562 if (unary_operator != ERROR_MARK)
4563 {
4564 tree cast_expression;
4565
4566 /* Consume the operator token. */
4567 token = cp_lexer_consume_token (parser->lexer);
4568 /* Parse the cast-expression. */
4569 cast_expression
4570 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4571 /* Now, build an appropriate representation. */
4572 switch (unary_operator)
4573 {
4574 case INDIRECT_REF:
4575 return build_x_indirect_ref (cast_expression, "unary *");
4576
4577 case ADDR_EXPR:
4578 return build_x_unary_op (ADDR_EXPR, cast_expression);
4579
4580 case CONVERT_EXPR:
4581 case NEGATE_EXPR:
4582 case TRUTH_NOT_EXPR:
4583 case PREINCREMENT_EXPR:
4584 case PREDECREMENT_EXPR:
4585 return finish_unary_op_expr (unary_operator, cast_expression);
4586
4587 case BIT_NOT_EXPR:
4588 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4589
4590 default:
4591 abort ();
4592 return error_mark_node;
4593 }
4594 }
4595
4596 return cp_parser_postfix_expression (parser, address_p);
4597}
4598
4599/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4600 unary-operator, the corresponding tree code is returned. */
4601
4602static enum tree_code
4603cp_parser_unary_operator (token)
4604 cp_token *token;
4605{
4606 switch (token->type)
4607 {
4608 case CPP_MULT:
4609 return INDIRECT_REF;
4610
4611 case CPP_AND:
4612 return ADDR_EXPR;
4613
4614 case CPP_PLUS:
4615 return CONVERT_EXPR;
4616
4617 case CPP_MINUS:
4618 return NEGATE_EXPR;
4619
4620 case CPP_NOT:
4621 return TRUTH_NOT_EXPR;
4622
4623 case CPP_COMPL:
4624 return BIT_NOT_EXPR;
4625
4626 default:
4627 return ERROR_MARK;
4628 }
4629}
4630
4631/* Parse a new-expression.
4632
4633 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4634 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4635
4636 Returns a representation of the expression. */
4637
4638static tree
4639cp_parser_new_expression (parser)
4640 cp_parser *parser;
4641{
4642 bool global_scope_p;
4643 tree placement;
4644 tree type;
4645 tree initializer;
4646
4647 /* Look for the optional `::' operator. */
4648 global_scope_p
4649 = (cp_parser_global_scope_opt (parser,
4650 /*current_scope_valid_p=*/false)
4651 != NULL_TREE);
4652 /* Look for the `new' operator. */
4653 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4654 /* There's no easy way to tell a new-placement from the
4655 `( type-id )' construct. */
4656 cp_parser_parse_tentatively (parser);
4657 /* Look for a new-placement. */
4658 placement = cp_parser_new_placement (parser);
4659 /* If that didn't work out, there's no new-placement. */
4660 if (!cp_parser_parse_definitely (parser))
4661 placement = NULL_TREE;
4662
4663 /* If the next token is a `(', then we have a parenthesized
4664 type-id. */
4665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4666 {
4667 /* Consume the `('. */
4668 cp_lexer_consume_token (parser->lexer);
4669 /* Parse the type-id. */
4670 type = cp_parser_type_id (parser);
4671 /* Look for the closing `)'. */
4672 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4673 }
4674 /* Otherwise, there must be a new-type-id. */
4675 else
4676 type = cp_parser_new_type_id (parser);
4677
4678 /* If the next token is a `(', then we have a new-initializer. */
4679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4680 initializer = cp_parser_new_initializer (parser);
4681 else
4682 initializer = NULL_TREE;
4683
4684 /* Create a representation of the new-expression. */
4685 return build_new (placement, type, initializer, global_scope_p);
4686}
4687
4688/* Parse a new-placement.
4689
4690 new-placement:
4691 ( expression-list )
4692
4693 Returns the same representation as for an expression-list. */
4694
4695static tree
4696cp_parser_new_placement (parser)
4697 cp_parser *parser;
4698{
4699 tree expression_list;
4700
4701 /* Look for the opening `('. */
4702 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4703 return error_mark_node;
4704 /* Parse the expression-list. */
4705 expression_list = cp_parser_expression_list (parser);
4706 /* Look for the closing `)'. */
4707 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4708
4709 return expression_list;
4710}
4711
4712/* Parse a new-type-id.
4713
4714 new-type-id:
4715 type-specifier-seq new-declarator [opt]
4716
4717 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4718 and whose TREE_VALUE is the new-declarator. */
4719
4720static tree
4721cp_parser_new_type_id (parser)
4722 cp_parser *parser;
4723{
4724 tree type_specifier_seq;
4725 tree declarator;
4726 const char *saved_message;
4727
4728 /* The type-specifier sequence must not contain type definitions.
4729 (It cannot contain declarations of new types either, but if they
4730 are not definitions we will catch that because they are not
4731 complete.) */
4732 saved_message = parser->type_definition_forbidden_message;
4733 parser->type_definition_forbidden_message
4734 = "types may not be defined in a new-type-id";
4735 /* Parse the type-specifier-seq. */
4736 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4737 /* Restore the old message. */
4738 parser->type_definition_forbidden_message = saved_message;
4739 /* Parse the new-declarator. */
4740 declarator = cp_parser_new_declarator_opt (parser);
4741
4742 return build_tree_list (type_specifier_seq, declarator);
4743}
4744
4745/* Parse an (optional) new-declarator.
4746
4747 new-declarator:
4748 ptr-operator new-declarator [opt]
4749 direct-new-declarator
4750
4751 Returns a representation of the declarator. See
4752 cp_parser_declarator for the representations used. */
4753
4754static tree
4755cp_parser_new_declarator_opt (parser)
4756 cp_parser *parser;
4757{
4758 enum tree_code code;
4759 tree type;
4760 tree cv_qualifier_seq;
4761
4762 /* We don't know if there's a ptr-operator next, or not. */
4763 cp_parser_parse_tentatively (parser);
4764 /* Look for a ptr-operator. */
4765 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4766 /* If that worked, look for more new-declarators. */
4767 if (cp_parser_parse_definitely (parser))
4768 {
4769 tree declarator;
4770
4771 /* Parse another optional declarator. */
4772 declarator = cp_parser_new_declarator_opt (parser);
4773
4774 /* Create the representation of the declarator. */
4775 if (code == INDIRECT_REF)
4776 declarator = make_pointer_declarator (cv_qualifier_seq,
4777 declarator);
4778 else
4779 declarator = make_reference_declarator (cv_qualifier_seq,
4780 declarator);
4781
4782 /* Handle the pointer-to-member case. */
4783 if (type)
4784 declarator = build_nt (SCOPE_REF, type, declarator);
4785
4786 return declarator;
4787 }
4788
4789 /* If the next token is a `[', there is a direct-new-declarator. */
4790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4791 return cp_parser_direct_new_declarator (parser);
4792
4793 return NULL_TREE;
4794}
4795
4796/* Parse a direct-new-declarator.
4797
4798 direct-new-declarator:
4799 [ expression ]
4800 direct-new-declarator [constant-expression]
4801
4802 Returns an ARRAY_REF, following the same conventions as are
4803 documented for cp_parser_direct_declarator. */
4804
4805static tree
4806cp_parser_direct_new_declarator (parser)
4807 cp_parser *parser;
4808{
4809 tree declarator = NULL_TREE;
4810
4811 while (true)
4812 {
4813 tree expression;
4814
4815 /* Look for the opening `['. */
4816 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4817 /* The first expression is not required to be constant. */
4818 if (!declarator)
4819 {
4820 expression = cp_parser_expression (parser);
4821 /* The standard requires that the expression have integral
4822 type. DR 74 adds enumeration types. We believe that the
4823 real intent is that these expressions be handled like the
4824 expression in a `switch' condition, which also allows
4825 classes with a single conversion to integral or
4826 enumeration type. */
4827 if (!processing_template_decl)
4828 {
4829 expression
4830 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4831 expression,
b746c5dc 4832 /*complain=*/true);
a723baf1
MM
4833 if (!expression)
4834 {
4835 error ("expression in new-declarator must have integral or enumeration type");
4836 expression = error_mark_node;
4837 }
4838 }
4839 }
4840 /* But all the other expressions must be. */
4841 else
4842 expression = cp_parser_constant_expression (parser);
4843 /* Look for the closing `]'. */
4844 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4845
4846 /* Add this bound to the declarator. */
4847 declarator = build_nt (ARRAY_REF, declarator, expression);
4848
4849 /* If the next token is not a `[', then there are no more
4850 bounds. */
4851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4852 break;
4853 }
4854
4855 return declarator;
4856}
4857
4858/* Parse a new-initializer.
4859
4860 new-initializer:
4861 ( expression-list [opt] )
4862
4863 Returns a reprsentation of the expression-list. If there is no
4864 expression-list, VOID_ZERO_NODE is returned. */
4865
4866static tree
4867cp_parser_new_initializer (parser)
4868 cp_parser *parser;
4869{
4870 tree expression_list;
4871
4872 /* Look for the opening parenthesis. */
4873 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4874 /* If the next token is not a `)', then there is an
4875 expression-list. */
4876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4877 expression_list = cp_parser_expression_list (parser);
4878 else
4879 expression_list = void_zero_node;
4880 /* Look for the closing parenthesis. */
4881 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4882
4883 return expression_list;
4884}
4885
4886/* Parse a delete-expression.
4887
4888 delete-expression:
4889 :: [opt] delete cast-expression
4890 :: [opt] delete [ ] cast-expression
4891
4892 Returns a representation of the expression. */
4893
4894static tree
4895cp_parser_delete_expression (parser)
4896 cp_parser *parser;
4897{
4898 bool global_scope_p;
4899 bool array_p;
4900 tree expression;
4901
4902 /* Look for the optional `::' operator. */
4903 global_scope_p
4904 = (cp_parser_global_scope_opt (parser,
4905 /*current_scope_valid_p=*/false)
4906 != NULL_TREE);
4907 /* Look for the `delete' keyword. */
4908 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4909 /* See if the array syntax is in use. */
4910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4911 {
4912 /* Consume the `[' token. */
4913 cp_lexer_consume_token (parser->lexer);
4914 /* Look for the `]' token. */
4915 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4916 /* Remember that this is the `[]' construct. */
4917 array_p = true;
4918 }
4919 else
4920 array_p = false;
4921
4922 /* Parse the cast-expression. */
4923 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
4924
4925 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4926}
4927
4928/* Parse a cast-expression.
4929
4930 cast-expression:
4931 unary-expression
4932 ( type-id ) cast-expression
4933
4934 Returns a representation of the expression. */
4935
4936static tree
4937cp_parser_cast_expression (cp_parser *parser, bool address_p)
4938{
4939 /* If it's a `(', then we might be looking at a cast. */
4940 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4941 {
4942 tree type = NULL_TREE;
4943 tree expr = NULL_TREE;
4944 bool compound_literal_p;
4945 const char *saved_message;
4946
4947 /* There's no way to know yet whether or not this is a cast.
4948 For example, `(int (3))' is a unary-expression, while `(int)
4949 3' is a cast. So, we resort to parsing tentatively. */
4950 cp_parser_parse_tentatively (parser);
4951 /* Types may not be defined in a cast. */
4952 saved_message = parser->type_definition_forbidden_message;
4953 parser->type_definition_forbidden_message
4954 = "types may not be defined in casts";
4955 /* Consume the `('. */
4956 cp_lexer_consume_token (parser->lexer);
4957 /* A very tricky bit is that `(struct S) { 3 }' is a
4958 compound-literal (which we permit in C++ as an extension).
4959 But, that construct is not a cast-expression -- it is a
4960 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4961 is legal; if the compound-literal were a cast-expression,
4962 you'd need an extra set of parentheses.) But, if we parse
4963 the type-id, and it happens to be a class-specifier, then we
4964 will commit to the parse at that point, because we cannot
4965 undo the action that is done when creating a new class. So,
4966 then we cannot back up and do a postfix-expression.
4967
4968 Therefore, we scan ahead to the closing `)', and check to see
4969 if the token after the `)' is a `{'. If so, we are not
4970 looking at a cast-expression.
4971
4972 Save tokens so that we can put them back. */
4973 cp_lexer_save_tokens (parser->lexer);
4974 /* Skip tokens until the next token is a closing parenthesis.
4975 If we find the closing `)', and the next token is a `{', then
4976 we are looking at a compound-literal. */
4977 compound_literal_p
4978 = (cp_parser_skip_to_closing_parenthesis (parser)
4979 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4980 /* Roll back the tokens we skipped. */
4981 cp_lexer_rollback_tokens (parser->lexer);
4982 /* If we were looking at a compound-literal, simulate an error
4983 so that the call to cp_parser_parse_definitely below will
4984 fail. */
4985 if (compound_literal_p)
4986 cp_parser_simulate_error (parser);
4987 else
4988 {
4989 /* Look for the type-id. */
4990 type = cp_parser_type_id (parser);
4991 /* Look for the closing `)'. */
4992 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4993 }
4994
4995 /* Restore the saved message. */
4996 parser->type_definition_forbidden_message = saved_message;
4997
4998 /* If all went well, this is a cast. */
4999 if (cp_parser_parse_definitely (parser))
5000 {
5001 /* Parse the dependent expression. */
5002 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5003 /* Warn about old-style casts, if so requested. */
5004 if (warn_old_style_cast
5005 && !in_system_header
5006 && !VOID_TYPE_P (type)
5007 && current_lang_name != lang_name_c)
5008 warning ("use of old-style cast");
5009 /* Perform the cast. */
5010 expr = build_c_cast (type, expr);
5011 }
5012
5013 if (expr)
5014 return expr;
5015 }
5016
5017 /* If we get here, then it's not a cast, so it must be a
5018 unary-expression. */
5019 return cp_parser_unary_expression (parser, address_p);
5020}
5021
5022/* Parse a pm-expression.
5023
5024 pm-expression:
5025 cast-expression
5026 pm-expression .* cast-expression
5027 pm-expression ->* cast-expression
5028
5029 Returns a representation of the expression. */
5030
5031static tree
5032cp_parser_pm_expression (parser)
5033 cp_parser *parser;
5034{
5035 tree cast_expr;
5036 tree pm_expr;
5037
5038 /* Parse the cast-expresion. */
5039 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5040 pm_expr = cast_expr;
5041 /* Now look for pointer-to-member operators. */
5042 while (true)
5043 {
5044 cp_token *token;
5045 enum cpp_ttype token_type;
5046
5047 /* Peek at the next token. */
5048 token = cp_lexer_peek_token (parser->lexer);
5049 token_type = token->type;
5050 /* If it's not `.*' or `->*' there's no pointer-to-member
5051 operation. */
5052 if (token_type != CPP_DOT_STAR
5053 && token_type != CPP_DEREF_STAR)
5054 break;
5055
5056 /* Consume the token. */
5057 cp_lexer_consume_token (parser->lexer);
5058
5059 /* Parse another cast-expression. */
5060 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5061
5062 /* Build the representation of the pointer-to-member
5063 operation. */
5064 if (token_type == CPP_DEREF_STAR)
5065 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5066 else
5067 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5068 }
5069
5070 return pm_expr;
5071}
5072
5073/* Parse a multiplicative-expression.
5074
5075 mulitplicative-expression:
5076 pm-expression
5077 multiplicative-expression * pm-expression
5078 multiplicative-expression / pm-expression
5079 multiplicative-expression % pm-expression
5080
5081 Returns a representation of the expression. */
5082
5083static tree
5084cp_parser_multiplicative_expression (parser)
5085 cp_parser *parser;
5086{
39b1af70 5087 static const cp_parser_token_tree_map map = {
a723baf1
MM
5088 { CPP_MULT, MULT_EXPR },
5089 { CPP_DIV, TRUNC_DIV_EXPR },
5090 { CPP_MOD, TRUNC_MOD_EXPR },
5091 { CPP_EOF, ERROR_MARK }
5092 };
5093
5094 return cp_parser_binary_expression (parser,
5095 map,
5096 cp_parser_pm_expression);
5097}
5098
5099/* Parse an additive-expression.
5100
5101 additive-expression:
5102 multiplicative-expression
5103 additive-expression + multiplicative-expression
5104 additive-expression - multiplicative-expression
5105
5106 Returns a representation of the expression. */
5107
5108static tree
5109cp_parser_additive_expression (parser)
5110 cp_parser *parser;
5111{
39b1af70 5112 static const cp_parser_token_tree_map map = {
a723baf1
MM
5113 { CPP_PLUS, PLUS_EXPR },
5114 { CPP_MINUS, MINUS_EXPR },
5115 { CPP_EOF, ERROR_MARK }
5116 };
5117
5118 return cp_parser_binary_expression (parser,
5119 map,
5120 cp_parser_multiplicative_expression);
5121}
5122
5123/* Parse a shift-expression.
5124
5125 shift-expression:
5126 additive-expression
5127 shift-expression << additive-expression
5128 shift-expression >> additive-expression
5129
5130 Returns a representation of the expression. */
5131
5132static tree
5133cp_parser_shift_expression (parser)
5134 cp_parser *parser;
5135{
39b1af70 5136 static const cp_parser_token_tree_map map = {
a723baf1
MM
5137 { CPP_LSHIFT, LSHIFT_EXPR },
5138 { CPP_RSHIFT, RSHIFT_EXPR },
5139 { CPP_EOF, ERROR_MARK }
5140 };
5141
5142 return cp_parser_binary_expression (parser,
5143 map,
5144 cp_parser_additive_expression);
5145}
5146
5147/* Parse a relational-expression.
5148
5149 relational-expression:
5150 shift-expression
5151 relational-expression < shift-expression
5152 relational-expression > shift-expression
5153 relational-expression <= shift-expression
5154 relational-expression >= shift-expression
5155
5156 GNU Extension:
5157
5158 relational-expression:
5159 relational-expression <? shift-expression
5160 relational-expression >? shift-expression
5161
5162 Returns a representation of the expression. */
5163
5164static tree
5165cp_parser_relational_expression (parser)
5166 cp_parser *parser;
5167{
39b1af70 5168 static const cp_parser_token_tree_map map = {
a723baf1
MM
5169 { CPP_LESS, LT_EXPR },
5170 { CPP_GREATER, GT_EXPR },
5171 { CPP_LESS_EQ, LE_EXPR },
5172 { CPP_GREATER_EQ, GE_EXPR },
5173 { CPP_MIN, MIN_EXPR },
5174 { CPP_MAX, MAX_EXPR },
5175 { CPP_EOF, ERROR_MARK }
5176 };
5177
5178 return cp_parser_binary_expression (parser,
5179 map,
5180 cp_parser_shift_expression);
5181}
5182
5183/* Parse an equality-expression.
5184
5185 equality-expression:
5186 relational-expression
5187 equality-expression == relational-expression
5188 equality-expression != relational-expression
5189
5190 Returns a representation of the expression. */
5191
5192static tree
5193cp_parser_equality_expression (parser)
5194 cp_parser *parser;
5195{
39b1af70 5196 static const cp_parser_token_tree_map map = {
a723baf1
MM
5197 { CPP_EQ_EQ, EQ_EXPR },
5198 { CPP_NOT_EQ, NE_EXPR },
5199 { CPP_EOF, ERROR_MARK }
5200 };
5201
5202 return cp_parser_binary_expression (parser,
5203 map,
5204 cp_parser_relational_expression);
5205}
5206
5207/* Parse an and-expression.
5208
5209 and-expression:
5210 equality-expression
5211 and-expression & equality-expression
5212
5213 Returns a representation of the expression. */
5214
5215static tree
5216cp_parser_and_expression (parser)
5217 cp_parser *parser;
5218{
39b1af70 5219 static const cp_parser_token_tree_map map = {
a723baf1
MM
5220 { CPP_AND, BIT_AND_EXPR },
5221 { CPP_EOF, ERROR_MARK }
5222 };
5223
5224 return cp_parser_binary_expression (parser,
5225 map,
5226 cp_parser_equality_expression);
5227}
5228
5229/* Parse an exclusive-or-expression.
5230
5231 exclusive-or-expression:
5232 and-expression
5233 exclusive-or-expression ^ and-expression
5234
5235 Returns a representation of the expression. */
5236
5237static tree
5238cp_parser_exclusive_or_expression (parser)
5239 cp_parser *parser;
5240{
39b1af70 5241 static const cp_parser_token_tree_map map = {
a723baf1
MM
5242 { CPP_XOR, BIT_XOR_EXPR },
5243 { CPP_EOF, ERROR_MARK }
5244 };
5245
5246 return cp_parser_binary_expression (parser,
5247 map,
5248 cp_parser_and_expression);
5249}
5250
5251
5252/* Parse an inclusive-or-expression.
5253
5254 inclusive-or-expression:
5255 exclusive-or-expression
5256 inclusive-or-expression | exclusive-or-expression
5257
5258 Returns a representation of the expression. */
5259
5260static tree
5261cp_parser_inclusive_or_expression (parser)
5262 cp_parser *parser;
5263{
39b1af70 5264 static const cp_parser_token_tree_map map = {
a723baf1
MM
5265 { CPP_OR, BIT_IOR_EXPR },
5266 { CPP_EOF, ERROR_MARK }
5267 };
5268
5269 return cp_parser_binary_expression (parser,
5270 map,
5271 cp_parser_exclusive_or_expression);
5272}
5273
5274/* Parse a logical-and-expression.
5275
5276 logical-and-expression:
5277 inclusive-or-expression
5278 logical-and-expression && inclusive-or-expression
5279
5280 Returns a representation of the expression. */
5281
5282static tree
5283cp_parser_logical_and_expression (parser)
5284 cp_parser *parser;
5285{
39b1af70 5286 static const cp_parser_token_tree_map map = {
a723baf1
MM
5287 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5288 { CPP_EOF, ERROR_MARK }
5289 };
5290
5291 return cp_parser_binary_expression (parser,
5292 map,
5293 cp_parser_inclusive_or_expression);
5294}
5295
5296/* Parse a logical-or-expression.
5297
5298 logical-or-expression:
5299 logical-and-expresion
5300 logical-or-expression || logical-and-expression
5301
5302 Returns a representation of the expression. */
5303
5304static tree
5305cp_parser_logical_or_expression (parser)
5306 cp_parser *parser;
5307{
39b1af70 5308 static const cp_parser_token_tree_map map = {
a723baf1
MM
5309 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5310 { CPP_EOF, ERROR_MARK }
5311 };
5312
5313 return cp_parser_binary_expression (parser,
5314 map,
5315 cp_parser_logical_and_expression);
5316}
5317
5318/* Parse a conditional-expression.
5319
5320 conditional-expression:
5321 logical-or-expression
5322 logical-or-expression ? expression : assignment-expression
5323
5324 GNU Extensions:
5325
5326 conditional-expression:
5327 logical-or-expression ? : assignment-expression
5328
5329 Returns a representation of the expression. */
5330
5331static tree
5332cp_parser_conditional_expression (parser)
5333 cp_parser *parser;
5334{
5335 tree logical_or_expr;
5336
5337 /* Parse the logical-or-expression. */
5338 logical_or_expr = cp_parser_logical_or_expression (parser);
5339 /* If the next token is a `?', then we have a real conditional
5340 expression. */
5341 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5342 return cp_parser_question_colon_clause (parser, logical_or_expr);
5343 /* Otherwise, the value is simply the logical-or-expression. */
5344 else
5345 return logical_or_expr;
5346}
5347
5348/* Parse the `? expression : assignment-expression' part of a
5349 conditional-expression. The LOGICAL_OR_EXPR is the
5350 logical-or-expression that started the conditional-expression.
5351 Returns a representation of the entire conditional-expression.
5352
5353 This routine exists only so that it can be shared between
5354 cp_parser_conditional_expression and
5355 cp_parser_assignment_expression.
5356
5357 ? expression : assignment-expression
5358
5359 GNU Extensions:
5360
5361 ? : assignment-expression */
5362
5363static tree
5364cp_parser_question_colon_clause (parser, logical_or_expr)
5365 cp_parser *parser;
5366 tree logical_or_expr;
5367{
5368 tree expr;
5369 tree assignment_expr;
5370
5371 /* Consume the `?' token. */
5372 cp_lexer_consume_token (parser->lexer);
5373 if (cp_parser_allow_gnu_extensions_p (parser)
5374 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5375 /* Implicit true clause. */
5376 expr = NULL_TREE;
5377 else
5378 /* Parse the expression. */
5379 expr = cp_parser_expression (parser);
5380
5381 /* The next token should be a `:'. */
5382 cp_parser_require (parser, CPP_COLON, "`:'");
5383 /* Parse the assignment-expression. */
5384 assignment_expr = cp_parser_assignment_expression (parser);
5385
5386 /* Build the conditional-expression. */
5387 return build_x_conditional_expr (logical_or_expr,
5388 expr,
5389 assignment_expr);
5390}
5391
5392/* Parse an assignment-expression.
5393
5394 assignment-expression:
5395 conditional-expression
5396 logical-or-expression assignment-operator assignment_expression
5397 throw-expression
5398
5399 Returns a representation for the expression. */
5400
5401static tree
5402cp_parser_assignment_expression (parser)
5403 cp_parser *parser;
5404{
5405 tree expr;
5406
5407 /* If the next token is the `throw' keyword, then we're looking at
5408 a throw-expression. */
5409 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5410 expr = cp_parser_throw_expression (parser);
5411 /* Otherwise, it must be that we are looking at a
5412 logical-or-expression. */
5413 else
5414 {
5415 /* Parse the logical-or-expression. */
5416 expr = cp_parser_logical_or_expression (parser);
5417 /* If the next token is a `?' then we're actually looking at a
5418 conditional-expression. */
5419 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5420 return cp_parser_question_colon_clause (parser, expr);
5421 else
5422 {
5423 enum tree_code assignment_operator;
5424
5425 /* If it's an assignment-operator, we're using the second
5426 production. */
5427 assignment_operator
5428 = cp_parser_assignment_operator_opt (parser);
5429 if (assignment_operator != ERROR_MARK)
5430 {
5431 tree rhs;
5432
5433 /* Parse the right-hand side of the assignment. */
5434 rhs = cp_parser_assignment_expression (parser);
5435 /* Build the asignment expression. */
5436 expr = build_x_modify_expr (expr,
5437 assignment_operator,
5438 rhs);
5439 }
5440 }
5441 }
5442
5443 return expr;
5444}
5445
5446/* Parse an (optional) assignment-operator.
5447
5448 assignment-operator: one of
5449 = *= /= %= += -= >>= <<= &= ^= |=
5450
5451 GNU Extension:
5452
5453 assignment-operator: one of
5454 <?= >?=
5455
5456 If the next token is an assignment operator, the corresponding tree
5457 code is returned, and the token is consumed. For example, for
5458 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5459 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5460 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5461 operator, ERROR_MARK is returned. */
5462
5463static enum tree_code
5464cp_parser_assignment_operator_opt (parser)
5465 cp_parser *parser;
5466{
5467 enum tree_code op;
5468 cp_token *token;
5469
5470 /* Peek at the next toen. */
5471 token = cp_lexer_peek_token (parser->lexer);
5472
5473 switch (token->type)
5474 {
5475 case CPP_EQ:
5476 op = NOP_EXPR;
5477 break;
5478
5479 case CPP_MULT_EQ:
5480 op = MULT_EXPR;
5481 break;
5482
5483 case CPP_DIV_EQ:
5484 op = TRUNC_DIV_EXPR;
5485 break;
5486
5487 case CPP_MOD_EQ:
5488 op = TRUNC_MOD_EXPR;
5489 break;
5490
5491 case CPP_PLUS_EQ:
5492 op = PLUS_EXPR;
5493 break;
5494
5495 case CPP_MINUS_EQ:
5496 op = MINUS_EXPR;
5497 break;
5498
5499 case CPP_RSHIFT_EQ:
5500 op = RSHIFT_EXPR;
5501 break;
5502
5503 case CPP_LSHIFT_EQ:
5504 op = LSHIFT_EXPR;
5505 break;
5506
5507 case CPP_AND_EQ:
5508 op = BIT_AND_EXPR;
5509 break;
5510
5511 case CPP_XOR_EQ:
5512 op = BIT_XOR_EXPR;
5513 break;
5514
5515 case CPP_OR_EQ:
5516 op = BIT_IOR_EXPR;
5517 break;
5518
5519 case CPP_MIN_EQ:
5520 op = MIN_EXPR;
5521 break;
5522
5523 case CPP_MAX_EQ:
5524 op = MAX_EXPR;
5525 break;
5526
5527 default:
5528 /* Nothing else is an assignment operator. */
5529 op = ERROR_MARK;
5530 }
5531
5532 /* If it was an assignment operator, consume it. */
5533 if (op != ERROR_MARK)
5534 cp_lexer_consume_token (parser->lexer);
5535
5536 return op;
5537}
5538
5539/* Parse an expression.
5540
5541 expression:
5542 assignment-expression
5543 expression , assignment-expression
5544
5545 Returns a representation of the expression. */
5546
5547static tree
5548cp_parser_expression (parser)
5549 cp_parser *parser;
5550{
5551 tree expression = NULL_TREE;
5552 bool saw_comma_p = false;
5553
5554 while (true)
5555 {
5556 tree assignment_expression;
5557
5558 /* Parse the next assignment-expression. */
5559 assignment_expression
5560 = cp_parser_assignment_expression (parser);
5561 /* If this is the first assignment-expression, we can just
5562 save it away. */
5563 if (!expression)
5564 expression = assignment_expression;
5565 /* Otherwise, chain the expressions together. It is unclear why
5566 we do not simply build COMPOUND_EXPRs as we go. */
5567 else
5568 expression = tree_cons (NULL_TREE,
5569 assignment_expression,
5570 expression);
5571 /* If the next token is not a comma, then we are done with the
5572 expression. */
5573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5574 break;
5575 /* Consume the `,'. */
5576 cp_lexer_consume_token (parser->lexer);
5577 /* The first time we see a `,', we must take special action
5578 because the representation used for a single expression is
5579 different from that used for a list containing the single
5580 expression. */
5581 if (!saw_comma_p)
5582 {
5583 /* Remember that this expression has a `,' in it. */
5584 saw_comma_p = true;
5585 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5586 additional expressions to it. */
5587 expression = build_tree_list (NULL_TREE, expression);
5588 }
5589 }
5590
5591 /* Build a COMPOUND_EXPR to represent the entire expression, if
5592 necessary. We built up the list in reverse order, so we must
5593 straighten it out here. */
5594 if (saw_comma_p)
5595 expression = build_x_compound_expr (nreverse (expression));
5596
5597 return expression;
5598}
5599
5600/* Parse a constant-expression.
5601
5602 constant-expression:
5603 conditional-expression */
5604
5605static tree
5606cp_parser_constant_expression (parser)
5607 cp_parser *parser;
5608{
5609 bool saved_constant_expression_p;
5610 tree expression;
5611
5612 /* It might seem that we could simply parse the
5613 conditional-expression, and then check to see if it were
5614 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5615 one that the compiler can figure out is constant, possibly after
5616 doing some simplifications or optimizations. The standard has a
5617 precise definition of constant-expression, and we must honor
5618 that, even though it is somewhat more restrictive.
5619
5620 For example:
5621
5622 int i[(2, 3)];
5623
5624 is not a legal declaration, because `(2, 3)' is not a
5625 constant-expression. The `,' operator is forbidden in a
5626 constant-expression. However, GCC's constant-folding machinery
5627 will fold this operation to an INTEGER_CST for `3'. */
5628
5629 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5630 saved_constant_expression_p = parser->constant_expression_p;
5631 /* We are now parsing a constant-expression. */
5632 parser->constant_expression_p = true;
5633 /* Parse the conditional-expression. */
5634 expression = cp_parser_conditional_expression (parser);
5635 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5636 parser->constant_expression_p = saved_constant_expression_p;
5637
5638 return expression;
5639}
5640
5641/* Statements [gram.stmt.stmt] */
5642
5643/* Parse a statement.
5644
5645 statement:
5646 labeled-statement
5647 expression-statement
5648 compound-statement
5649 selection-statement
5650 iteration-statement
5651 jump-statement
5652 declaration-statement
5653 try-block */
5654
5655static void
5656cp_parser_statement (parser)
5657 cp_parser *parser;
5658{
5659 tree statement;
5660 cp_token *token;
5661 int statement_line_number;
5662
5663 /* There is no statement yet. */
5664 statement = NULL_TREE;
5665 /* Peek at the next token. */
5666 token = cp_lexer_peek_token (parser->lexer);
5667 /* Remember the line number of the first token in the statement. */
5668 statement_line_number = token->line_number;
5669 /* If this is a keyword, then that will often determine what kind of
5670 statement we have. */
5671 if (token->type == CPP_KEYWORD)
5672 {
5673 enum rid keyword = token->keyword;
5674
5675 switch (keyword)
5676 {
5677 case RID_CASE:
5678 case RID_DEFAULT:
5679 statement = cp_parser_labeled_statement (parser);
5680 break;
5681
5682 case RID_IF:
5683 case RID_SWITCH:
5684 statement = cp_parser_selection_statement (parser);
5685 break;
5686
5687 case RID_WHILE:
5688 case RID_DO:
5689 case RID_FOR:
5690 statement = cp_parser_iteration_statement (parser);
5691 break;
5692
5693 case RID_BREAK:
5694 case RID_CONTINUE:
5695 case RID_RETURN:
5696 case RID_GOTO:
5697 statement = cp_parser_jump_statement (parser);
5698 break;
5699
5700 case RID_TRY:
5701 statement = cp_parser_try_block (parser);
5702 break;
5703
5704 default:
5705 /* It might be a keyword like `int' that can start a
5706 declaration-statement. */
5707 break;
5708 }
5709 }
5710 else if (token->type == CPP_NAME)
5711 {
5712 /* If the next token is a `:', then we are looking at a
5713 labeled-statement. */
5714 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5715 if (token->type == CPP_COLON)
5716 statement = cp_parser_labeled_statement (parser);
5717 }
5718 /* Anything that starts with a `{' must be a compound-statement. */
5719 else if (token->type == CPP_OPEN_BRACE)
5720 statement = cp_parser_compound_statement (parser);
5721
5722 /* Everything else must be a declaration-statement or an
5723 expression-statement. Try for the declaration-statement
5724 first, unless we are looking at a `;', in which case we know that
5725 we have an expression-statement. */
5726 if (!statement)
5727 {
5728 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5729 {
5730 cp_parser_parse_tentatively (parser);
5731 /* Try to parse the declaration-statement. */
5732 cp_parser_declaration_statement (parser);
5733 /* If that worked, we're done. */
5734 if (cp_parser_parse_definitely (parser))
5735 return;
5736 }
5737 /* Look for an expression-statement instead. */
5738 statement = cp_parser_expression_statement (parser);
5739 }
5740
5741 /* Set the line number for the statement. */
5742 if (statement && statement_code_p (TREE_CODE (statement)))
5743 STMT_LINENO (statement) = statement_line_number;
5744}
5745
5746/* Parse a labeled-statement.
5747
5748 labeled-statement:
5749 identifier : statement
5750 case constant-expression : statement
5751 default : statement
5752
5753 Returns the new CASE_LABEL, for a `case' or `default' label. For
5754 an ordinary label, returns a LABEL_STMT. */
5755
5756static tree
5757cp_parser_labeled_statement (parser)
5758 cp_parser *parser;
5759{
5760 cp_token *token;
5761 tree statement = NULL_TREE;
5762
5763 /* The next token should be an identifier. */
5764 token = cp_lexer_peek_token (parser->lexer);
5765 if (token->type != CPP_NAME
5766 && token->type != CPP_KEYWORD)
5767 {
5768 cp_parser_error (parser, "expected labeled-statement");
5769 return error_mark_node;
5770 }
5771
5772 switch (token->keyword)
5773 {
5774 case RID_CASE:
5775 {
5776 tree expr;
5777
5778 /* Consume the `case' token. */
5779 cp_lexer_consume_token (parser->lexer);
5780 /* Parse the constant-expression. */
5781 expr = cp_parser_constant_expression (parser);
5782 /* Create the label. */
5783 statement = finish_case_label (expr, NULL_TREE);
5784 }
5785 break;
5786
5787 case RID_DEFAULT:
5788 /* Consume the `default' token. */
5789 cp_lexer_consume_token (parser->lexer);
5790 /* Create the label. */
5791 statement = finish_case_label (NULL_TREE, NULL_TREE);
5792 break;
5793
5794 default:
5795 /* Anything else must be an ordinary label. */
5796 statement = finish_label_stmt (cp_parser_identifier (parser));
5797 break;
5798 }
5799
5800 /* Require the `:' token. */
5801 cp_parser_require (parser, CPP_COLON, "`:'");
5802 /* Parse the labeled statement. */
5803 cp_parser_statement (parser);
5804
5805 /* Return the label, in the case of a `case' or `default' label. */
5806 return statement;
5807}
5808
5809/* Parse an expression-statement.
5810
5811 expression-statement:
5812 expression [opt] ;
5813
5814 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5815 statement consists of nothing more than an `;'. */
5816
5817static tree
5818cp_parser_expression_statement (parser)
5819 cp_parser *parser;
5820{
5821 tree statement;
5822
5823 /* If the next token is not a `;', then there is an expression to parse. */
5824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5825 statement = finish_expr_stmt (cp_parser_expression (parser));
5826 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5827 else
5828 {
5829 finish_stmt ();
5830 statement = NULL_TREE;
5831 }
5832 /* Consume the final `;'. */
5833 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5834 {
5835 /* If there is additional (erroneous) input, skip to the end of
5836 the statement. */
5837 cp_parser_skip_to_end_of_statement (parser);
5838 /* If the next token is now a `;', consume it. */
5839 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5840 cp_lexer_consume_token (parser->lexer);
5841 }
5842
5843 return statement;
5844}
5845
5846/* Parse a compound-statement.
5847
5848 compound-statement:
5849 { statement-seq [opt] }
5850
5851 Returns a COMPOUND_STMT representing the statement. */
5852
5853static tree
5854cp_parser_compound_statement (cp_parser *parser)
5855{
5856 tree compound_stmt;
5857
5858 /* Consume the `{'. */
5859 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5860 return error_mark_node;
5861 /* Begin the compound-statement. */
5862 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5863 /* Parse an (optional) statement-seq. */
5864 cp_parser_statement_seq_opt (parser);
5865 /* Finish the compound-statement. */
5866 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5867 /* Consume the `}'. */
5868 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5869
5870 return compound_stmt;
5871}
5872
5873/* Parse an (optional) statement-seq.
5874
5875 statement-seq:
5876 statement
5877 statement-seq [opt] statement */
5878
5879static void
5880cp_parser_statement_seq_opt (parser)
5881 cp_parser *parser;
5882{
5883 /* Scan statements until there aren't any more. */
5884 while (true)
5885 {
5886 /* If we're looking at a `}', then we've run out of statements. */
5887 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5888 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5889 break;
5890
5891 /* Parse the statement. */
5892 cp_parser_statement (parser);
5893 }
5894}
5895
5896/* Parse a selection-statement.
5897
5898 selection-statement:
5899 if ( condition ) statement
5900 if ( condition ) statement else statement
5901 switch ( condition ) statement
5902
5903 Returns the new IF_STMT or SWITCH_STMT. */
5904
5905static tree
5906cp_parser_selection_statement (parser)
5907 cp_parser *parser;
5908{
5909 cp_token *token;
5910 enum rid keyword;
5911
5912 /* Peek at the next token. */
5913 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5914
5915 /* See what kind of keyword it is. */
5916 keyword = token->keyword;
5917 switch (keyword)
5918 {
5919 case RID_IF:
5920 case RID_SWITCH:
5921 {
5922 tree statement;
5923 tree condition;
5924
5925 /* Look for the `('. */
5926 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5927 {
5928 cp_parser_skip_to_end_of_statement (parser);
5929 return error_mark_node;
5930 }
5931
5932 /* Begin the selection-statement. */
5933 if (keyword == RID_IF)
5934 statement = begin_if_stmt ();
5935 else
5936 statement = begin_switch_stmt ();
5937
5938 /* Parse the condition. */
5939 condition = cp_parser_condition (parser);
5940 /* Look for the `)'. */
5941 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5942 cp_parser_skip_to_closing_parenthesis (parser);
5943
5944 if (keyword == RID_IF)
5945 {
5946 tree then_stmt;
5947
5948 /* Add the condition. */
5949 finish_if_stmt_cond (condition, statement);
5950
5951 /* Parse the then-clause. */
5952 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5953 finish_then_clause (statement);
5954
5955 /* If the next token is `else', parse the else-clause. */
5956 if (cp_lexer_next_token_is_keyword (parser->lexer,
5957 RID_ELSE))
5958 {
5959 tree else_stmt;
5960
5961 /* Consume the `else' keyword. */
5962 cp_lexer_consume_token (parser->lexer);
5963 /* Parse the else-clause. */
5964 else_stmt
5965 = cp_parser_implicitly_scoped_statement (parser);
5966 finish_else_clause (statement);
5967 }
5968
5969 /* Now we're all done with the if-statement. */
5970 finish_if_stmt ();
5971 }
5972 else
5973 {
5974 tree body;
5975
5976 /* Add the condition. */
5977 finish_switch_cond (condition, statement);
5978
5979 /* Parse the body of the switch-statement. */
5980 body = cp_parser_implicitly_scoped_statement (parser);
5981
5982 /* Now we're all done with the switch-statement. */
5983 finish_switch_stmt (statement);
5984 }
5985
5986 return statement;
5987 }
5988 break;
5989
5990 default:
5991 cp_parser_error (parser, "expected selection-statement");
5992 return error_mark_node;
5993 }
5994}
5995
5996/* Parse a condition.
5997
5998 condition:
5999 expression
6000 type-specifier-seq declarator = assignment-expression
6001
6002 GNU Extension:
6003
6004 condition:
6005 type-specifier-seq declarator asm-specification [opt]
6006 attributes [opt] = assignment-expression
6007
6008 Returns the expression that should be tested. */
6009
6010static tree
6011cp_parser_condition (parser)
6012 cp_parser *parser;
6013{
6014 tree type_specifiers;
6015 const char *saved_message;
6016
6017 /* Try the declaration first. */
6018 cp_parser_parse_tentatively (parser);
6019 /* New types are not allowed in the type-specifier-seq for a
6020 condition. */
6021 saved_message = parser->type_definition_forbidden_message;
6022 parser->type_definition_forbidden_message
6023 = "types may not be defined in conditions";
6024 /* Parse the type-specifier-seq. */
6025 type_specifiers = cp_parser_type_specifier_seq (parser);
6026 /* Restore the saved message. */
6027 parser->type_definition_forbidden_message = saved_message;
6028 /* If all is well, we might be looking at a declaration. */
6029 if (!cp_parser_error_occurred (parser))
6030 {
6031 tree decl;
6032 tree asm_specification;
6033 tree attributes;
6034 tree declarator;
6035 tree initializer = NULL_TREE;
6036
6037 /* Parse the declarator. */
62b8a44e 6038 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
6039 /*ctor_dtor_or_conv_p=*/NULL);
6040 /* Parse the attributes. */
6041 attributes = cp_parser_attributes_opt (parser);
6042 /* Parse the asm-specification. */
6043 asm_specification = cp_parser_asm_specification_opt (parser);
6044 /* If the next token is not an `=', then we might still be
6045 looking at an expression. For example:
6046
6047 if (A(a).x)
6048
6049 looks like a decl-specifier-seq and a declarator -- but then
6050 there is no `=', so this is an expression. */
6051 cp_parser_require (parser, CPP_EQ, "`='");
6052 /* If we did see an `=', then we are looking at a declaration
6053 for sure. */
6054 if (cp_parser_parse_definitely (parser))
6055 {
6056 /* Create the declaration. */
6057 decl = start_decl (declarator, type_specifiers,
6058 /*initialized_p=*/true,
6059 attributes, /*prefix_attributes=*/NULL_TREE);
6060 /* Parse the assignment-expression. */
6061 initializer = cp_parser_assignment_expression (parser);
6062
6063 /* Process the initializer. */
6064 cp_finish_decl (decl,
6065 initializer,
6066 asm_specification,
6067 LOOKUP_ONLYCONVERTING);
6068
6069 return convert_from_reference (decl);
6070 }
6071 }
6072 /* If we didn't even get past the declarator successfully, we are
6073 definitely not looking at a declaration. */
6074 else
6075 cp_parser_abort_tentative_parse (parser);
6076
6077 /* Otherwise, we are looking at an expression. */
6078 return cp_parser_expression (parser);
6079}
6080
6081/* Parse an iteration-statement.
6082
6083 iteration-statement:
6084 while ( condition ) statement
6085 do statement while ( expression ) ;
6086 for ( for-init-statement condition [opt] ; expression [opt] )
6087 statement
6088
6089 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6090
6091static tree
6092cp_parser_iteration_statement (parser)
6093 cp_parser *parser;
6094{
6095 cp_token *token;
6096 enum rid keyword;
6097 tree statement;
6098
6099 /* Peek at the next token. */
6100 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6101 if (!token)
6102 return error_mark_node;
6103
6104 /* See what kind of keyword it is. */
6105 keyword = token->keyword;
6106 switch (keyword)
6107 {
6108 case RID_WHILE:
6109 {
6110 tree condition;
6111
6112 /* Begin the while-statement. */
6113 statement = begin_while_stmt ();
6114 /* Look for the `('. */
6115 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6116 /* Parse the condition. */
6117 condition = cp_parser_condition (parser);
6118 finish_while_stmt_cond (condition, statement);
6119 /* Look for the `)'. */
6120 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6121 /* Parse the dependent statement. */
6122 cp_parser_already_scoped_statement (parser);
6123 /* We're done with the while-statement. */
6124 finish_while_stmt (statement);
6125 }
6126 break;
6127
6128 case RID_DO:
6129 {
6130 tree expression;
6131
6132 /* Begin the do-statement. */
6133 statement = begin_do_stmt ();
6134 /* Parse the body of the do-statement. */
6135 cp_parser_implicitly_scoped_statement (parser);
6136 finish_do_body (statement);
6137 /* Look for the `while' keyword. */
6138 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6139 /* Look for the `('. */
6140 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6141 /* Parse the expression. */
6142 expression = cp_parser_expression (parser);
6143 /* We're done with the do-statement. */
6144 finish_do_stmt (expression, statement);
6145 /* Look for the `)'. */
6146 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6147 /* Look for the `;'. */
6148 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6149 }
6150 break;
6151
6152 case RID_FOR:
6153 {
6154 tree condition = NULL_TREE;
6155 tree expression = NULL_TREE;
6156
6157 /* Begin the for-statement. */
6158 statement = begin_for_stmt ();
6159 /* Look for the `('. */
6160 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6161 /* Parse the initialization. */
6162 cp_parser_for_init_statement (parser);
6163 finish_for_init_stmt (statement);
6164
6165 /* If there's a condition, process it. */
6166 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6167 condition = cp_parser_condition (parser);
6168 finish_for_cond (condition, statement);
6169 /* Look for the `;'. */
6170 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6171
6172 /* If there's an expression, process it. */
6173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6174 expression = cp_parser_expression (parser);
6175 finish_for_expr (expression, statement);
6176 /* Look for the `)'. */
6177 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6178
6179 /* Parse the body of the for-statement. */
6180 cp_parser_already_scoped_statement (parser);
6181
6182 /* We're done with the for-statement. */
6183 finish_for_stmt (statement);
6184 }
6185 break;
6186
6187 default:
6188 cp_parser_error (parser, "expected iteration-statement");
6189 statement = error_mark_node;
6190 break;
6191 }
6192
6193 return statement;
6194}
6195
6196/* Parse a for-init-statement.
6197
6198 for-init-statement:
6199 expression-statement
6200 simple-declaration */
6201
6202static void
6203cp_parser_for_init_statement (parser)
6204 cp_parser *parser;
6205{
6206 /* If the next token is a `;', then we have an empty
6207 expression-statement. Gramatically, this is also a
6208 simple-declaration, but an invalid one, because it does not
6209 declare anything. Therefore, if we did not handle this case
6210 specially, we would issue an error message about an invalid
6211 declaration. */
6212 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6213 {
6214 /* We're going to speculatively look for a declaration, falling back
6215 to an expression, if necessary. */
6216 cp_parser_parse_tentatively (parser);
6217 /* Parse the declaration. */
6218 cp_parser_simple_declaration (parser,
6219 /*function_definition_allowed_p=*/false);
6220 /* If the tentative parse failed, then we shall need to look for an
6221 expression-statement. */
6222 if (cp_parser_parse_definitely (parser))
6223 return;
6224 }
6225
6226 cp_parser_expression_statement (parser);
6227}
6228
6229/* Parse a jump-statement.
6230
6231 jump-statement:
6232 break ;
6233 continue ;
6234 return expression [opt] ;
6235 goto identifier ;
6236
6237 GNU extension:
6238
6239 jump-statement:
6240 goto * expression ;
6241
6242 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6243 GOTO_STMT. */
6244
6245static tree
6246cp_parser_jump_statement (parser)
6247 cp_parser *parser;
6248{
6249 tree statement = error_mark_node;
6250 cp_token *token;
6251 enum rid keyword;
6252
6253 /* Peek at the next token. */
6254 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6255 if (!token)
6256 return error_mark_node;
6257
6258 /* See what kind of keyword it is. */
6259 keyword = token->keyword;
6260 switch (keyword)
6261 {
6262 case RID_BREAK:
6263 statement = finish_break_stmt ();
6264 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6265 break;
6266
6267 case RID_CONTINUE:
6268 statement = finish_continue_stmt ();
6269 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6270 break;
6271
6272 case RID_RETURN:
6273 {
6274 tree expr;
6275
6276 /* If the next token is a `;', then there is no
6277 expression. */
6278 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6279 expr = cp_parser_expression (parser);
6280 else
6281 expr = NULL_TREE;
6282 /* Build the return-statement. */
6283 statement = finish_return_stmt (expr);
6284 /* Look for the final `;'. */
6285 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6286 }
6287 break;
6288
6289 case RID_GOTO:
6290 /* Create the goto-statement. */
6291 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6292 {
6293 /* Issue a warning about this use of a GNU extension. */
6294 if (pedantic)
6295 pedwarn ("ISO C++ forbids computed gotos");
6296 /* Consume the '*' token. */
6297 cp_lexer_consume_token (parser->lexer);
6298 /* Parse the dependent expression. */
6299 finish_goto_stmt (cp_parser_expression (parser));
6300 }
6301 else
6302 finish_goto_stmt (cp_parser_identifier (parser));
6303 /* Look for the final `;'. */
6304 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6305 break;
6306
6307 default:
6308 cp_parser_error (parser, "expected jump-statement");
6309 break;
6310 }
6311
6312 return statement;
6313}
6314
6315/* Parse a declaration-statement.
6316
6317 declaration-statement:
6318 block-declaration */
6319
6320static void
6321cp_parser_declaration_statement (parser)
6322 cp_parser *parser;
6323{
6324 /* Parse the block-declaration. */
6325 cp_parser_block_declaration (parser, /*statement_p=*/true);
6326
6327 /* Finish off the statement. */
6328 finish_stmt ();
6329}
6330
6331/* Some dependent statements (like `if (cond) statement'), are
6332 implicitly in their own scope. In other words, if the statement is
6333 a single statement (as opposed to a compound-statement), it is
6334 none-the-less treated as if it were enclosed in braces. Any
6335 declarations appearing in the dependent statement are out of scope
6336 after control passes that point. This function parses a statement,
6337 but ensures that is in its own scope, even if it is not a
6338 compound-statement.
6339
6340 Returns the new statement. */
6341
6342static tree
6343cp_parser_implicitly_scoped_statement (parser)
6344 cp_parser *parser;
6345{
6346 tree statement;
6347
6348 /* If the token is not a `{', then we must take special action. */
6349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6350 {
6351 /* Create a compound-statement. */
6352 statement = begin_compound_stmt (/*has_no_scope=*/0);
6353 /* Parse the dependent-statement. */
6354 cp_parser_statement (parser);
6355 /* Finish the dummy compound-statement. */
6356 finish_compound_stmt (/*has_no_scope=*/0, statement);
6357 }
6358 /* Otherwise, we simply parse the statement directly. */
6359 else
6360 statement = cp_parser_compound_statement (parser);
6361
6362 /* Return the statement. */
6363 return statement;
6364}
6365
6366/* For some dependent statements (like `while (cond) statement'), we
6367 have already created a scope. Therefore, even if the dependent
6368 statement is a compound-statement, we do not want to create another
6369 scope. */
6370
6371static void
6372cp_parser_already_scoped_statement (parser)
6373 cp_parser *parser;
6374{
6375 /* If the token is not a `{', then we must take special action. */
6376 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6377 {
6378 tree statement;
6379
6380 /* Create a compound-statement. */
6381 statement = begin_compound_stmt (/*has_no_scope=*/1);
6382 /* Parse the dependent-statement. */
6383 cp_parser_statement (parser);
6384 /* Finish the dummy compound-statement. */
6385 finish_compound_stmt (/*has_no_scope=*/1, statement);
6386 }
6387 /* Otherwise, we simply parse the statement directly. */
6388 else
6389 cp_parser_statement (parser);
6390}
6391
6392/* Declarations [gram.dcl.dcl] */
6393
6394/* Parse an optional declaration-sequence.
6395
6396 declaration-seq:
6397 declaration
6398 declaration-seq declaration */
6399
6400static void
6401cp_parser_declaration_seq_opt (parser)
6402 cp_parser *parser;
6403{
6404 while (true)
6405 {
6406 cp_token *token;
6407
6408 token = cp_lexer_peek_token (parser->lexer);
6409
6410 if (token->type == CPP_CLOSE_BRACE
6411 || token->type == CPP_EOF)
6412 break;
6413
6414 if (token->type == CPP_SEMICOLON)
6415 {
6416 /* A declaration consisting of a single semicolon is
6417 invalid. Allow it unless we're being pedantic. */
6418 if (pedantic)
6419 pedwarn ("extra `;'");
6420 cp_lexer_consume_token (parser->lexer);
6421 continue;
6422 }
6423
c838d82f
MM
6424 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6425 parser to enter or exit implict `extern "C"' blocks. */
6426 while (pending_lang_change > 0)
6427 {
6428 push_lang_context (lang_name_c);
6429 --pending_lang_change;
6430 }
6431 while (pending_lang_change < 0)
6432 {
6433 pop_lang_context ();
6434 ++pending_lang_change;
6435 }
6436
6437 /* Parse the declaration itself. */
a723baf1
MM
6438 cp_parser_declaration (parser);
6439 }
6440}
6441
6442/* Parse a declaration.
6443
6444 declaration:
6445 block-declaration
6446 function-definition
6447 template-declaration
6448 explicit-instantiation
6449 explicit-specialization
6450 linkage-specification
1092805d
MM
6451 namespace-definition
6452
6453 GNU extension:
6454
6455 declaration:
6456 __extension__ declaration */
a723baf1
MM
6457
6458static void
6459cp_parser_declaration (parser)
6460 cp_parser *parser;
6461{
6462 cp_token token1;
6463 cp_token token2;
1092805d
MM
6464 int saved_pedantic;
6465
6466 /* Check for the `__extension__' keyword. */
6467 if (cp_parser_extension_opt (parser, &saved_pedantic))
6468 {
6469 /* Parse the qualified declaration. */
6470 cp_parser_declaration (parser);
6471 /* Restore the PEDANTIC flag. */
6472 pedantic = saved_pedantic;
6473
6474 return;
6475 }
a723baf1
MM
6476
6477 /* Try to figure out what kind of declaration is present. */
6478 token1 = *cp_lexer_peek_token (parser->lexer);
6479 if (token1.type != CPP_EOF)
6480 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6481
6482 /* If the next token is `extern' and the following token is a string
6483 literal, then we have a linkage specification. */
6484 if (token1.keyword == RID_EXTERN
6485 && cp_parser_is_string_literal (&token2))
6486 cp_parser_linkage_specification (parser);
6487 /* If the next token is `template', then we have either a template
6488 declaration, an explicit instantiation, or an explicit
6489 specialization. */
6490 else if (token1.keyword == RID_TEMPLATE)
6491 {
6492 /* `template <>' indicates a template specialization. */
6493 if (token2.type == CPP_LESS
6494 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6495 cp_parser_explicit_specialization (parser);
6496 /* `template <' indicates a template declaration. */
6497 else if (token2.type == CPP_LESS)
6498 cp_parser_template_declaration (parser, /*member_p=*/false);
6499 /* Anything else must be an explicit instantiation. */
6500 else
6501 cp_parser_explicit_instantiation (parser);
6502 }
6503 /* If the next token is `export', then we have a template
6504 declaration. */
6505 else if (token1.keyword == RID_EXPORT)
6506 cp_parser_template_declaration (parser, /*member_p=*/false);
6507 /* If the next token is `extern', 'static' or 'inline' and the one
6508 after that is `template', we have a GNU extended explicit
6509 instantiation directive. */
6510 else if (cp_parser_allow_gnu_extensions_p (parser)
6511 && (token1.keyword == RID_EXTERN
6512 || token1.keyword == RID_STATIC
6513 || token1.keyword == RID_INLINE)
6514 && token2.keyword == RID_TEMPLATE)
6515 cp_parser_explicit_instantiation (parser);
6516 /* If the next token is `namespace', check for a named or unnamed
6517 namespace definition. */
6518 else if (token1.keyword == RID_NAMESPACE
6519 && (/* A named namespace definition. */
6520 (token2.type == CPP_NAME
6521 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6522 == CPP_OPEN_BRACE))
6523 /* An unnamed namespace definition. */
6524 || token2.type == CPP_OPEN_BRACE))
6525 cp_parser_namespace_definition (parser);
6526 /* We must have either a block declaration or a function
6527 definition. */
6528 else
6529 /* Try to parse a block-declaration, or a function-definition. */
6530 cp_parser_block_declaration (parser, /*statement_p=*/false);
6531}
6532
6533/* Parse a block-declaration.
6534
6535 block-declaration:
6536 simple-declaration
6537 asm-definition
6538 namespace-alias-definition
6539 using-declaration
6540 using-directive
6541
6542 GNU Extension:
6543
6544 block-declaration:
6545 __extension__ block-declaration
6546 label-declaration
6547
6548 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6549 part of a declaration-statement. */
6550
6551static void
6552cp_parser_block_declaration (cp_parser *parser,
6553 bool statement_p)
6554{
6555 cp_token *token1;
6556 int saved_pedantic;
6557
6558 /* Check for the `__extension__' keyword. */
6559 if (cp_parser_extension_opt (parser, &saved_pedantic))
6560 {
6561 /* Parse the qualified declaration. */
6562 cp_parser_block_declaration (parser, statement_p);
6563 /* Restore the PEDANTIC flag. */
6564 pedantic = saved_pedantic;
6565
6566 return;
6567 }
6568
6569 /* Peek at the next token to figure out which kind of declaration is
6570 present. */
6571 token1 = cp_lexer_peek_token (parser->lexer);
6572
6573 /* If the next keyword is `asm', we have an asm-definition. */
6574 if (token1->keyword == RID_ASM)
6575 {
6576 if (statement_p)
6577 cp_parser_commit_to_tentative_parse (parser);
6578 cp_parser_asm_definition (parser);
6579 }
6580 /* If the next keyword is `namespace', we have a
6581 namespace-alias-definition. */
6582 else if (token1->keyword == RID_NAMESPACE)
6583 cp_parser_namespace_alias_definition (parser);
6584 /* If the next keyword is `using', we have either a
6585 using-declaration or a using-directive. */
6586 else if (token1->keyword == RID_USING)
6587 {
6588 cp_token *token2;
6589
6590 if (statement_p)
6591 cp_parser_commit_to_tentative_parse (parser);
6592 /* If the token after `using' is `namespace', then we have a
6593 using-directive. */
6594 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6595 if (token2->keyword == RID_NAMESPACE)
6596 cp_parser_using_directive (parser);
6597 /* Otherwise, it's a using-declaration. */
6598 else
6599 cp_parser_using_declaration (parser);
6600 }
6601 /* If the next keyword is `__label__' we have a label declaration. */
6602 else if (token1->keyword == RID_LABEL)
6603 {
6604 if (statement_p)
6605 cp_parser_commit_to_tentative_parse (parser);
6606 cp_parser_label_declaration (parser);
6607 }
6608 /* Anything else must be a simple-declaration. */
6609 else
6610 cp_parser_simple_declaration (parser, !statement_p);
6611}
6612
6613/* Parse a simple-declaration.
6614
6615 simple-declaration:
6616 decl-specifier-seq [opt] init-declarator-list [opt] ;
6617
6618 init-declarator-list:
6619 init-declarator
6620 init-declarator-list , init-declarator
6621
6622 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6623 function-definition as a simple-declaration. */
6624
6625static void
6626cp_parser_simple_declaration (parser, function_definition_allowed_p)
6627 cp_parser *parser;
6628 bool function_definition_allowed_p;
6629{
6630 tree decl_specifiers;
6631 tree attributes;
a723baf1
MM
6632 bool declares_class_or_enum;
6633 bool saw_declarator;
6634
6635 /* Defer access checks until we know what is being declared; the
6636 checks for names appearing in the decl-specifier-seq should be
6637 done as if we were in the scope of the thing being declared. */
cf22909c
KL
6638 push_deferring_access_checks (true);
6639
a723baf1
MM
6640 /* Parse the decl-specifier-seq. We have to keep track of whether
6641 or not the decl-specifier-seq declares a named class or
6642 enumeration type, since that is the only case in which the
6643 init-declarator-list is allowed to be empty.
6644
6645 [dcl.dcl]
6646
6647 In a simple-declaration, the optional init-declarator-list can be
6648 omitted only when declaring a class or enumeration, that is when
6649 the decl-specifier-seq contains either a class-specifier, an
6650 elaborated-type-specifier, or an enum-specifier. */
6651 decl_specifiers
6652 = cp_parser_decl_specifier_seq (parser,
6653 CP_PARSER_FLAGS_OPTIONAL,
6654 &attributes,
6655 &declares_class_or_enum);
6656 /* We no longer need to defer access checks. */
cf22909c 6657 stop_deferring_access_checks ();
24c0ef37 6658
a723baf1
MM
6659 /* Keep going until we hit the `;' at the end of the simple
6660 declaration. */
6661 saw_declarator = false;
6662 while (cp_lexer_next_token_is_not (parser->lexer,
6663 CPP_SEMICOLON))
6664 {
6665 cp_token *token;
6666 bool function_definition_p;
6667
6668 saw_declarator = true;
6669 /* Parse the init-declarator. */
6670 cp_parser_init_declarator (parser, decl_specifiers, attributes,
a723baf1
MM
6671 function_definition_allowed_p,
6672 /*member_p=*/false,
6673 &function_definition_p);
6674 /* Handle function definitions specially. */
6675 if (function_definition_p)
6676 {
6677 /* If the next token is a `,', then we are probably
6678 processing something like:
6679
6680 void f() {}, *p;
6681
6682 which is erroneous. */
6683 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6684 error ("mixing declarations and function-definitions is forbidden");
6685 /* Otherwise, we're done with the list of declarators. */
6686 else
24c0ef37 6687 {
cf22909c 6688 pop_deferring_access_checks ();
24c0ef37
GS
6689 return;
6690 }
a723baf1
MM
6691 }
6692 /* The next token should be either a `,' or a `;'. */
6693 token = cp_lexer_peek_token (parser->lexer);
6694 /* If it's a `,', there are more declarators to come. */
6695 if (token->type == CPP_COMMA)
6696 cp_lexer_consume_token (parser->lexer);
6697 /* If it's a `;', we are done. */
6698 else if (token->type == CPP_SEMICOLON)
6699 break;
6700 /* Anything else is an error. */
6701 else
6702 {
6703 cp_parser_error (parser, "expected `,' or `;'");
6704 /* Skip tokens until we reach the end of the statement. */
6705 cp_parser_skip_to_end_of_statement (parser);
cf22909c 6706 pop_deferring_access_checks ();
a723baf1
MM
6707 return;
6708 }
6709 /* After the first time around, a function-definition is not
6710 allowed -- even if it was OK at first. For example:
6711
6712 int i, f() {}
6713
6714 is not valid. */
6715 function_definition_allowed_p = false;
6716 }
6717
6718 /* Issue an error message if no declarators are present, and the
6719 decl-specifier-seq does not itself declare a class or
6720 enumeration. */
6721 if (!saw_declarator)
6722 {
6723 if (cp_parser_declares_only_class_p (parser))
6724 shadow_tag (decl_specifiers);
6725 /* Perform any deferred access checks. */
cf22909c 6726 perform_deferred_access_checks ();
a723baf1
MM
6727 }
6728
cf22909c
KL
6729 pop_deferring_access_checks ();
6730
a723baf1
MM
6731 /* Consume the `;'. */
6732 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6733
6734 /* Mark all the classes that appeared in the decl-specifier-seq as
6735 having received a `;'. */
6736 note_list_got_semicolon (decl_specifiers);
6737}
6738
6739/* Parse a decl-specifier-seq.
6740
6741 decl-specifier-seq:
6742 decl-specifier-seq [opt] decl-specifier
6743
6744 decl-specifier:
6745 storage-class-specifier
6746 type-specifier
6747 function-specifier
6748 friend
6749 typedef
6750
6751 GNU Extension:
6752
6753 decl-specifier-seq:
6754 decl-specifier-seq [opt] attributes
6755
6756 Returns a TREE_LIST, giving the decl-specifiers in the order they
6757 appear in the source code. The TREE_VALUE of each node is the
6758 decl-specifier. For a keyword (such as `auto' or `friend'), the
6759 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6760 representation of a type-specifier, see cp_parser_type_specifier.
6761
6762 If there are attributes, they will be stored in *ATTRIBUTES,
6763 represented as described above cp_parser_attributes.
6764
6765 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6766 appears, and the entity that will be a friend is not going to be a
6767 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6768 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6769 friendship is granted might not be a class. */
6770
6771static tree
6772cp_parser_decl_specifier_seq (parser, flags, attributes,
6773 declares_class_or_enum)
6774 cp_parser *parser;
6775 cp_parser_flags flags;
6776 tree *attributes;
6777 bool *declares_class_or_enum;
6778{
6779 tree decl_specs = NULL_TREE;
6780 bool friend_p = false;
2050a1bb 6781 bool constructor_possible_p = true;
a723baf1
MM
6782
6783 /* Assume no class or enumeration type is declared. */
6784 *declares_class_or_enum = false;
6785
6786 /* Assume there are no attributes. */
6787 *attributes = NULL_TREE;
6788
6789 /* Keep reading specifiers until there are no more to read. */
6790 while (true)
6791 {
6792 tree decl_spec = NULL_TREE;
6793 bool constructor_p;
6794 cp_token *token;
6795
6796 /* Peek at the next token. */
6797 token = cp_lexer_peek_token (parser->lexer);
6798 /* Handle attributes. */
6799 if (token->keyword == RID_ATTRIBUTE)
6800 {
6801 /* Parse the attributes. */
6802 decl_spec = cp_parser_attributes_opt (parser);
6803 /* Add them to the list. */
6804 *attributes = chainon (*attributes, decl_spec);
6805 continue;
6806 }
6807 /* If the next token is an appropriate keyword, we can simply
6808 add it to the list. */
6809 switch (token->keyword)
6810 {
6811 case RID_FRIEND:
6812 /* decl-specifier:
6813 friend */
6814 friend_p = true;
6815 /* The representation of the specifier is simply the
6816 appropriate TREE_IDENTIFIER node. */
6817 decl_spec = token->value;
6818 /* Consume the token. */
6819 cp_lexer_consume_token (parser->lexer);
6820 break;
6821
6822 /* function-specifier:
6823 inline
6824 virtual
6825 explicit */
6826 case RID_INLINE:
6827 case RID_VIRTUAL:
6828 case RID_EXPLICIT:
6829 decl_spec = cp_parser_function_specifier_opt (parser);
6830 break;
6831
6832 /* decl-specifier:
6833 typedef */
6834 case RID_TYPEDEF:
6835 /* The representation of the specifier is simply the
6836 appropriate TREE_IDENTIFIER node. */
6837 decl_spec = token->value;
6838 /* Consume the token. */
6839 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6840 /* A constructor declarator cannot appear in a typedef. */
6841 constructor_possible_p = false;
a723baf1
MM
6842 break;
6843
6844 /* storage-class-specifier:
6845 auto
6846 register
6847 static
6848 extern
6849 mutable
6850
6851 GNU Extension:
6852 thread */
6853 case RID_AUTO:
6854 case RID_REGISTER:
6855 case RID_STATIC:
6856 case RID_EXTERN:
6857 case RID_MUTABLE:
6858 case RID_THREAD:
6859 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6860 break;
6861
6862 default:
6863 break;
6864 }
6865
6866 /* Constructors are a special case. The `S' in `S()' is not a
6867 decl-specifier; it is the beginning of the declarator. */
6868 constructor_p = (!decl_spec
2050a1bb 6869 && constructor_possible_p
a723baf1
MM
6870 && cp_parser_constructor_declarator_p (parser,
6871 friend_p));
6872
6873 /* If we don't have a DECL_SPEC yet, then we must be looking at
6874 a type-specifier. */
6875 if (!decl_spec && !constructor_p)
6876 {
6877 bool decl_spec_declares_class_or_enum;
6878 bool is_cv_qualifier;
6879
6880 decl_spec
6881 = cp_parser_type_specifier (parser, flags,
6882 friend_p,
6883 /*is_declaration=*/true,
6884 &decl_spec_declares_class_or_enum,
6885 &is_cv_qualifier);
6886
6887 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6888
6889 /* If this type-specifier referenced a user-defined type
6890 (a typedef, class-name, etc.), then we can't allow any
6891 more such type-specifiers henceforth.
6892
6893 [dcl.spec]
6894
6895 The longest sequence of decl-specifiers that could
6896 possibly be a type name is taken as the
6897 decl-specifier-seq of a declaration. The sequence shall
6898 be self-consistent as described below.
6899
6900 [dcl.type]
6901
6902 As a general rule, at most one type-specifier is allowed
6903 in the complete decl-specifier-seq of a declaration. The
6904 only exceptions are the following:
6905
6906 -- const or volatile can be combined with any other
6907 type-specifier.
6908
6909 -- signed or unsigned can be combined with char, long,
6910 short, or int.
6911
6912 -- ..
6913
6914 Example:
6915
6916 typedef char* Pc;
6917 void g (const int Pc);
6918
6919 Here, Pc is *not* part of the decl-specifier seq; it's
6920 the declarator. Therefore, once we see a type-specifier
6921 (other than a cv-qualifier), we forbid any additional
6922 user-defined types. We *do* still allow things like `int
6923 int' to be considered a decl-specifier-seq, and issue the
6924 error message later. */
6925 if (decl_spec && !is_cv_qualifier)
6926 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
6927 /* A constructor declarator cannot follow a type-specifier. */
6928 if (decl_spec)
6929 constructor_possible_p = false;
a723baf1
MM
6930 }
6931
6932 /* If we still do not have a DECL_SPEC, then there are no more
6933 decl-specifiers. */
6934 if (!decl_spec)
6935 {
6936 /* Issue an error message, unless the entire construct was
6937 optional. */
6938 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6939 {
6940 cp_parser_error (parser, "expected decl specifier");
6941 return error_mark_node;
6942 }
6943
6944 break;
6945 }
6946
6947 /* Add the DECL_SPEC to the list of specifiers. */
6948 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6949
6950 /* After we see one decl-specifier, further decl-specifiers are
6951 always optional. */
6952 flags |= CP_PARSER_FLAGS_OPTIONAL;
6953 }
6954
6955 /* We have built up the DECL_SPECS in reverse order. Return them in
6956 the correct order. */
6957 return nreverse (decl_specs);
6958}
6959
6960/* Parse an (optional) storage-class-specifier.
6961
6962 storage-class-specifier:
6963 auto
6964 register
6965 static
6966 extern
6967 mutable
6968
6969 GNU Extension:
6970
6971 storage-class-specifier:
6972 thread
6973
6974 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6975
6976static tree
6977cp_parser_storage_class_specifier_opt (parser)
6978 cp_parser *parser;
6979{
6980 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6981 {
6982 case RID_AUTO:
6983 case RID_REGISTER:
6984 case RID_STATIC:
6985 case RID_EXTERN:
6986 case RID_MUTABLE:
6987 case RID_THREAD:
6988 /* Consume the token. */
6989 return cp_lexer_consume_token (parser->lexer)->value;
6990
6991 default:
6992 return NULL_TREE;
6993 }
6994}
6995
6996/* Parse an (optional) function-specifier.
6997
6998 function-specifier:
6999 inline
7000 virtual
7001 explicit
7002
7003 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7004
7005static tree
7006cp_parser_function_specifier_opt (parser)
7007 cp_parser *parser;
7008{
7009 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7010 {
7011 case RID_INLINE:
7012 case RID_VIRTUAL:
7013 case RID_EXPLICIT:
7014 /* Consume the token. */
7015 return cp_lexer_consume_token (parser->lexer)->value;
7016
7017 default:
7018 return NULL_TREE;
7019 }
7020}
7021
7022/* Parse a linkage-specification.
7023
7024 linkage-specification:
7025 extern string-literal { declaration-seq [opt] }
7026 extern string-literal declaration */
7027
7028static void
7029cp_parser_linkage_specification (parser)
7030 cp_parser *parser;
7031{
7032 cp_token *token;
7033 tree linkage;
7034
7035 /* Look for the `extern' keyword. */
7036 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7037
7038 /* Peek at the next token. */
7039 token = cp_lexer_peek_token (parser->lexer);
7040 /* If it's not a string-literal, then there's a problem. */
7041 if (!cp_parser_is_string_literal (token))
7042 {
7043 cp_parser_error (parser, "expected language-name");
7044 return;
7045 }
7046 /* Consume the token. */
7047 cp_lexer_consume_token (parser->lexer);
7048
7049 /* Transform the literal into an identifier. If the literal is a
7050 wide-character string, or contains embedded NULs, then we can't
7051 handle it as the user wants. */
7052 if (token->type == CPP_WSTRING
7053 || (strlen (TREE_STRING_POINTER (token->value))
7054 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7055 {
7056 cp_parser_error (parser, "invalid linkage-specification");
7057 /* Assume C++ linkage. */
7058 linkage = get_identifier ("c++");
7059 }
7060 /* If it's a simple string constant, things are easier. */
7061 else
7062 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7063
7064 /* We're now using the new linkage. */
7065 push_lang_context (linkage);
7066
7067 /* If the next token is a `{', then we're using the first
7068 production. */
7069 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7070 {
7071 /* Consume the `{' token. */
7072 cp_lexer_consume_token (parser->lexer);
7073 /* Parse the declarations. */
7074 cp_parser_declaration_seq_opt (parser);
7075 /* Look for the closing `}'. */
7076 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7077 }
7078 /* Otherwise, there's just one declaration. */
7079 else
7080 {
7081 bool saved_in_unbraced_linkage_specification_p;
7082
7083 saved_in_unbraced_linkage_specification_p
7084 = parser->in_unbraced_linkage_specification_p;
7085 parser->in_unbraced_linkage_specification_p = true;
7086 have_extern_spec = true;
7087 cp_parser_declaration (parser);
7088 have_extern_spec = false;
7089 parser->in_unbraced_linkage_specification_p
7090 = saved_in_unbraced_linkage_specification_p;
7091 }
7092
7093 /* We're done with the linkage-specification. */
7094 pop_lang_context ();
7095}
7096
7097/* Special member functions [gram.special] */
7098
7099/* Parse a conversion-function-id.
7100
7101 conversion-function-id:
7102 operator conversion-type-id
7103
7104 Returns an IDENTIFIER_NODE representing the operator. */
7105
7106static tree
7107cp_parser_conversion_function_id (parser)
7108 cp_parser *parser;
7109{
7110 tree type;
7111 tree saved_scope;
7112 tree saved_qualifying_scope;
7113 tree saved_object_scope;
7114
7115 /* Look for the `operator' token. */
7116 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7117 return error_mark_node;
7118 /* When we parse the conversion-type-id, the current scope will be
7119 reset. However, we need that information in able to look up the
7120 conversion function later, so we save it here. */
7121 saved_scope = parser->scope;
7122 saved_qualifying_scope = parser->qualifying_scope;
7123 saved_object_scope = parser->object_scope;
7124 /* We must enter the scope of the class so that the names of
7125 entities declared within the class are available in the
7126 conversion-type-id. For example, consider:
7127
7128 struct S {
7129 typedef int I;
7130 operator I();
7131 };
7132
7133 S::operator I() { ... }
7134
7135 In order to see that `I' is a type-name in the definition, we
7136 must be in the scope of `S'. */
7137 if (saved_scope)
7138 push_scope (saved_scope);
7139 /* Parse the conversion-type-id. */
7140 type = cp_parser_conversion_type_id (parser);
7141 /* Leave the scope of the class, if any. */
7142 if (saved_scope)
7143 pop_scope (saved_scope);
7144 /* Restore the saved scope. */
7145 parser->scope = saved_scope;
7146 parser->qualifying_scope = saved_qualifying_scope;
7147 parser->object_scope = saved_object_scope;
7148 /* If the TYPE is invalid, indicate failure. */
7149 if (type == error_mark_node)
7150 return error_mark_node;
7151 return mangle_conv_op_name_for_type (type);
7152}
7153
7154/* Parse a conversion-type-id:
7155
7156 conversion-type-id:
7157 type-specifier-seq conversion-declarator [opt]
7158
7159 Returns the TYPE specified. */
7160
7161static tree
7162cp_parser_conversion_type_id (parser)
7163 cp_parser *parser;
7164{
7165 tree attributes;
7166 tree type_specifiers;
7167 tree declarator;
7168
7169 /* Parse the attributes. */
7170 attributes = cp_parser_attributes_opt (parser);
7171 /* Parse the type-specifiers. */
7172 type_specifiers = cp_parser_type_specifier_seq (parser);
7173 /* If that didn't work, stop. */
7174 if (type_specifiers == error_mark_node)
7175 return error_mark_node;
7176 /* Parse the conversion-declarator. */
7177 declarator = cp_parser_conversion_declarator_opt (parser);
7178
7179 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7180 /*initialized=*/0, &attributes);
7181}
7182
7183/* Parse an (optional) conversion-declarator.
7184
7185 conversion-declarator:
7186 ptr-operator conversion-declarator [opt]
7187
7188 Returns a representation of the declarator. See
7189 cp_parser_declarator for details. */
7190
7191static tree
7192cp_parser_conversion_declarator_opt (parser)
7193 cp_parser *parser;
7194{
7195 enum tree_code code;
7196 tree class_type;
7197 tree cv_qualifier_seq;
7198
7199 /* We don't know if there's a ptr-operator next, or not. */
7200 cp_parser_parse_tentatively (parser);
7201 /* Try the ptr-operator. */
7202 code = cp_parser_ptr_operator (parser, &class_type,
7203 &cv_qualifier_seq);
7204 /* If it worked, look for more conversion-declarators. */
7205 if (cp_parser_parse_definitely (parser))
7206 {
7207 tree declarator;
7208
7209 /* Parse another optional declarator. */
7210 declarator = cp_parser_conversion_declarator_opt (parser);
7211
7212 /* Create the representation of the declarator. */
7213 if (code == INDIRECT_REF)
7214 declarator = make_pointer_declarator (cv_qualifier_seq,
7215 declarator);
7216 else
7217 declarator = make_reference_declarator (cv_qualifier_seq,
7218 declarator);
7219
7220 /* Handle the pointer-to-member case. */
7221 if (class_type)
7222 declarator = build_nt (SCOPE_REF, class_type, declarator);
7223
7224 return declarator;
7225 }
7226
7227 return NULL_TREE;
7228}
7229
7230/* Parse an (optional) ctor-initializer.
7231
7232 ctor-initializer:
7233 : mem-initializer-list
7234
7235 Returns TRUE iff the ctor-initializer was actually present. */
7236
7237static bool
7238cp_parser_ctor_initializer_opt (parser)
7239 cp_parser *parser;
7240{
7241 /* If the next token is not a `:', then there is no
7242 ctor-initializer. */
7243 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7244 {
7245 /* Do default initialization of any bases and members. */
7246 if (DECL_CONSTRUCTOR_P (current_function_decl))
7247 finish_mem_initializers (NULL_TREE);
7248
7249 return false;
7250 }
7251
7252 /* Consume the `:' token. */
7253 cp_lexer_consume_token (parser->lexer);
7254 /* And the mem-initializer-list. */
7255 cp_parser_mem_initializer_list (parser);
7256
7257 return true;
7258}
7259
7260/* Parse a mem-initializer-list.
7261
7262 mem-initializer-list:
7263 mem-initializer
7264 mem-initializer , mem-initializer-list */
7265
7266static void
7267cp_parser_mem_initializer_list (parser)
7268 cp_parser *parser;
7269{
7270 tree mem_initializer_list = NULL_TREE;
7271
7272 /* Let the semantic analysis code know that we are starting the
7273 mem-initializer-list. */
7274 begin_mem_initializers ();
7275
7276 /* Loop through the list. */
7277 while (true)
7278 {
7279 tree mem_initializer;
7280
7281 /* Parse the mem-initializer. */
7282 mem_initializer = cp_parser_mem_initializer (parser);
7283 /* Add it to the list, unless it was erroneous. */
7284 if (mem_initializer)
7285 {
7286 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7287 mem_initializer_list = mem_initializer;
7288 }
7289 /* If the next token is not a `,', we're done. */
7290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7291 break;
7292 /* Consume the `,' token. */
7293 cp_lexer_consume_token (parser->lexer);
7294 }
7295
7296 /* Perform semantic analysis. */
7297 finish_mem_initializers (mem_initializer_list);
7298}
7299
7300/* Parse a mem-initializer.
7301
7302 mem-initializer:
7303 mem-initializer-id ( expression-list [opt] )
7304
7305 GNU extension:
7306
7307 mem-initializer:
7308 ( expresion-list [opt] )
7309
7310 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7311 class) or FIELD_DECL (for a non-static data member) to initialize;
7312 the TREE_VALUE is the expression-list. */
7313
7314static tree
7315cp_parser_mem_initializer (parser)
7316 cp_parser *parser;
7317{
7318 tree mem_initializer_id;
7319 tree expression_list;
7320
7321 /* Find out what is being initialized. */
7322 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7323 {
7324 pedwarn ("anachronistic old-style base class initializer");
7325 mem_initializer_id = NULL_TREE;
7326 }
7327 else
7328 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7329 /* Look for the opening `('. */
7330 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7331 /* Parse the expression-list. */
7332 if (cp_lexer_next_token_is_not (parser->lexer,
7333 CPP_CLOSE_PAREN))
7334 expression_list = cp_parser_expression_list (parser);
7335 else
7336 expression_list = void_type_node;
7337 /* Look for the closing `)'. */
7338 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7339
7340 return expand_member_init (mem_initializer_id,
7341 expression_list);
7342}
7343
7344/* Parse a mem-initializer-id.
7345
7346 mem-initializer-id:
7347 :: [opt] nested-name-specifier [opt] class-name
7348 identifier
7349
7350 Returns a TYPE indicating the class to be initializer for the first
7351 production. Returns an IDENTIFIER_NODE indicating the data member
7352 to be initialized for the second production. */
7353
7354static tree
7355cp_parser_mem_initializer_id (parser)
7356 cp_parser *parser;
7357{
7358 bool global_scope_p;
7359 bool nested_name_specifier_p;
7360 tree id;
7361
7362 /* Look for the optional `::' operator. */
7363 global_scope_p
7364 = (cp_parser_global_scope_opt (parser,
7365 /*current_scope_valid_p=*/false)
7366 != NULL_TREE);
7367 /* Look for the optional nested-name-specifier. The simplest way to
7368 implement:
7369
7370 [temp.res]
7371
7372 The keyword `typename' is not permitted in a base-specifier or
7373 mem-initializer; in these contexts a qualified name that
7374 depends on a template-parameter is implicitly assumed to be a
7375 type name.
7376
7377 is to assume that we have seen the `typename' keyword at this
7378 point. */
7379 nested_name_specifier_p
7380 = (cp_parser_nested_name_specifier_opt (parser,
7381 /*typename_keyword_p=*/true,
7382 /*check_dependency_p=*/true,
7383 /*type_p=*/true)
7384 != NULL_TREE);
7385 /* If there is a `::' operator or a nested-name-specifier, then we
7386 are definitely looking for a class-name. */
7387 if (global_scope_p || nested_name_specifier_p)
7388 return cp_parser_class_name (parser,
7389 /*typename_keyword_p=*/true,
7390 /*template_keyword_p=*/false,
7391 /*type_p=*/false,
7392 /*check_access_p=*/true,
7393 /*check_dependency_p=*/true,
7394 /*class_head_p=*/false);
7395 /* Otherwise, we could also be looking for an ordinary identifier. */
7396 cp_parser_parse_tentatively (parser);
7397 /* Try a class-name. */
7398 id = cp_parser_class_name (parser,
7399 /*typename_keyword_p=*/true,
7400 /*template_keyword_p=*/false,
7401 /*type_p=*/false,
7402 /*check_access_p=*/true,
7403 /*check_dependency_p=*/true,
7404 /*class_head_p=*/false);
7405 /* If we found one, we're done. */
7406 if (cp_parser_parse_definitely (parser))
7407 return id;
7408 /* Otherwise, look for an ordinary identifier. */
7409 return cp_parser_identifier (parser);
7410}
7411
7412/* Overloading [gram.over] */
7413
7414/* Parse an operator-function-id.
7415
7416 operator-function-id:
7417 operator operator
7418
7419 Returns an IDENTIFIER_NODE for the operator which is a
7420 human-readable spelling of the identifier, e.g., `operator +'. */
7421
7422static tree
7423cp_parser_operator_function_id (parser)
7424 cp_parser *parser;
7425{
7426 /* Look for the `operator' keyword. */
7427 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7428 return error_mark_node;
7429 /* And then the name of the operator itself. */
7430 return cp_parser_operator (parser);
7431}
7432
7433/* Parse an operator.
7434
7435 operator:
7436 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7437 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7438 || ++ -- , ->* -> () []
7439
7440 GNU Extensions:
7441
7442 operator:
7443 <? >? <?= >?=
7444
7445 Returns an IDENTIFIER_NODE for the operator which is a
7446 human-readable spelling of the identifier, e.g., `operator +'. */
7447
7448static tree
7449cp_parser_operator (parser)
7450 cp_parser *parser;
7451{
7452 tree id = NULL_TREE;
7453 cp_token *token;
7454
7455 /* Peek at the next token. */
7456 token = cp_lexer_peek_token (parser->lexer);
7457 /* Figure out which operator we have. */
7458 switch (token->type)
7459 {
7460 case CPP_KEYWORD:
7461 {
7462 enum tree_code op;
7463
7464 /* The keyword should be either `new' or `delete'. */
7465 if (token->keyword == RID_NEW)
7466 op = NEW_EXPR;
7467 else if (token->keyword == RID_DELETE)
7468 op = DELETE_EXPR;
7469 else
7470 break;
7471
7472 /* Consume the `new' or `delete' token. */
7473 cp_lexer_consume_token (parser->lexer);
7474
7475 /* Peek at the next token. */
7476 token = cp_lexer_peek_token (parser->lexer);
7477 /* If it's a `[' token then this is the array variant of the
7478 operator. */
7479 if (token->type == CPP_OPEN_SQUARE)
7480 {
7481 /* Consume the `[' token. */
7482 cp_lexer_consume_token (parser->lexer);
7483 /* Look for the `]' token. */
7484 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7485 id = ansi_opname (op == NEW_EXPR
7486 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7487 }
7488 /* Otherwise, we have the non-array variant. */
7489 else
7490 id = ansi_opname (op);
7491
7492 return id;
7493 }
7494
7495 case CPP_PLUS:
7496 id = ansi_opname (PLUS_EXPR);
7497 break;
7498
7499 case CPP_MINUS:
7500 id = ansi_opname (MINUS_EXPR);
7501 break;
7502
7503 case CPP_MULT:
7504 id = ansi_opname (MULT_EXPR);
7505 break;
7506
7507 case CPP_DIV:
7508 id = ansi_opname (TRUNC_DIV_EXPR);
7509 break;
7510
7511 case CPP_MOD:
7512 id = ansi_opname (TRUNC_MOD_EXPR);
7513 break;
7514
7515 case CPP_XOR:
7516 id = ansi_opname (BIT_XOR_EXPR);
7517 break;
7518
7519 case CPP_AND:
7520 id = ansi_opname (BIT_AND_EXPR);
7521 break;
7522
7523 case CPP_OR:
7524 id = ansi_opname (BIT_IOR_EXPR);
7525 break;
7526
7527 case CPP_COMPL:
7528 id = ansi_opname (BIT_NOT_EXPR);
7529 break;
7530
7531 case CPP_NOT:
7532 id = ansi_opname (TRUTH_NOT_EXPR);
7533 break;
7534
7535 case CPP_EQ:
7536 id = ansi_assopname (NOP_EXPR);
7537 break;
7538
7539 case CPP_LESS:
7540 id = ansi_opname (LT_EXPR);
7541 break;
7542
7543 case CPP_GREATER:
7544 id = ansi_opname (GT_EXPR);
7545 break;
7546
7547 case CPP_PLUS_EQ:
7548 id = ansi_assopname (PLUS_EXPR);
7549 break;
7550
7551 case CPP_MINUS_EQ:
7552 id = ansi_assopname (MINUS_EXPR);
7553 break;
7554
7555 case CPP_MULT_EQ:
7556 id = ansi_assopname (MULT_EXPR);
7557 break;
7558
7559 case CPP_DIV_EQ:
7560 id = ansi_assopname (TRUNC_DIV_EXPR);
7561 break;
7562
7563 case CPP_MOD_EQ:
7564 id = ansi_assopname (TRUNC_MOD_EXPR);
7565 break;
7566
7567 case CPP_XOR_EQ:
7568 id = ansi_assopname (BIT_XOR_EXPR);
7569 break;
7570
7571 case CPP_AND_EQ:
7572 id = ansi_assopname (BIT_AND_EXPR);
7573 break;
7574
7575 case CPP_OR_EQ:
7576 id = ansi_assopname (BIT_IOR_EXPR);
7577 break;
7578
7579 case CPP_LSHIFT:
7580 id = ansi_opname (LSHIFT_EXPR);
7581 break;
7582
7583 case CPP_RSHIFT:
7584 id = ansi_opname (RSHIFT_EXPR);
7585 break;
7586
7587 case CPP_LSHIFT_EQ:
7588 id = ansi_assopname (LSHIFT_EXPR);
7589 break;
7590
7591 case CPP_RSHIFT_EQ:
7592 id = ansi_assopname (RSHIFT_EXPR);
7593 break;
7594
7595 case CPP_EQ_EQ:
7596 id = ansi_opname (EQ_EXPR);
7597 break;
7598
7599 case CPP_NOT_EQ:
7600 id = ansi_opname (NE_EXPR);
7601 break;
7602
7603 case CPP_LESS_EQ:
7604 id = ansi_opname (LE_EXPR);
7605 break;
7606
7607 case CPP_GREATER_EQ:
7608 id = ansi_opname (GE_EXPR);
7609 break;
7610
7611 case CPP_AND_AND:
7612 id = ansi_opname (TRUTH_ANDIF_EXPR);
7613 break;
7614
7615 case CPP_OR_OR:
7616 id = ansi_opname (TRUTH_ORIF_EXPR);
7617 break;
7618
7619 case CPP_PLUS_PLUS:
7620 id = ansi_opname (POSTINCREMENT_EXPR);
7621 break;
7622
7623 case CPP_MINUS_MINUS:
7624 id = ansi_opname (PREDECREMENT_EXPR);
7625 break;
7626
7627 case CPP_COMMA:
7628 id = ansi_opname (COMPOUND_EXPR);
7629 break;
7630
7631 case CPP_DEREF_STAR:
7632 id = ansi_opname (MEMBER_REF);
7633 break;
7634
7635 case CPP_DEREF:
7636 id = ansi_opname (COMPONENT_REF);
7637 break;
7638
7639 case CPP_OPEN_PAREN:
7640 /* Consume the `('. */
7641 cp_lexer_consume_token (parser->lexer);
7642 /* Look for the matching `)'. */
7643 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7644 return ansi_opname (CALL_EXPR);
7645
7646 case CPP_OPEN_SQUARE:
7647 /* Consume the `['. */
7648 cp_lexer_consume_token (parser->lexer);
7649 /* Look for the matching `]'. */
7650 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7651 return ansi_opname (ARRAY_REF);
7652
7653 /* Extensions. */
7654 case CPP_MIN:
7655 id = ansi_opname (MIN_EXPR);
7656 break;
7657
7658 case CPP_MAX:
7659 id = ansi_opname (MAX_EXPR);
7660 break;
7661
7662 case CPP_MIN_EQ:
7663 id = ansi_assopname (MIN_EXPR);
7664 break;
7665
7666 case CPP_MAX_EQ:
7667 id = ansi_assopname (MAX_EXPR);
7668 break;
7669
7670 default:
7671 /* Anything else is an error. */
7672 break;
7673 }
7674
7675 /* If we have selected an identifier, we need to consume the
7676 operator token. */
7677 if (id)
7678 cp_lexer_consume_token (parser->lexer);
7679 /* Otherwise, no valid operator name was present. */
7680 else
7681 {
7682 cp_parser_error (parser, "expected operator");
7683 id = error_mark_node;
7684 }
7685
7686 return id;
7687}
7688
7689/* Parse a template-declaration.
7690
7691 template-declaration:
7692 export [opt] template < template-parameter-list > declaration
7693
7694 If MEMBER_P is TRUE, this template-declaration occurs within a
7695 class-specifier.
7696
7697 The grammar rule given by the standard isn't correct. What
7698 is really meant is:
7699
7700 template-declaration:
7701 export [opt] template-parameter-list-seq
7702 decl-specifier-seq [opt] init-declarator [opt] ;
7703 export [opt] template-parameter-list-seq
7704 function-definition
7705
7706 template-parameter-list-seq:
7707 template-parameter-list-seq [opt]
7708 template < template-parameter-list > */
7709
7710static void
7711cp_parser_template_declaration (parser, member_p)
7712 cp_parser *parser;
7713 bool member_p;
7714{
7715 /* Check for `export'. */
7716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7717 {
7718 /* Consume the `export' token. */
7719 cp_lexer_consume_token (parser->lexer);
7720 /* Warn that we do not support `export'. */
7721 warning ("keyword `export' not implemented, and will be ignored");
7722 }
7723
7724 cp_parser_template_declaration_after_export (parser, member_p);
7725}
7726
7727/* Parse a template-parameter-list.
7728
7729 template-parameter-list:
7730 template-parameter
7731 template-parameter-list , template-parameter
7732
7733 Returns a TREE_LIST. Each node represents a template parameter.
7734 The nodes are connected via their TREE_CHAINs. */
7735
7736static tree
7737cp_parser_template_parameter_list (parser)
7738 cp_parser *parser;
7739{
7740 tree parameter_list = NULL_TREE;
7741
7742 while (true)
7743 {
7744 tree parameter;
7745 cp_token *token;
7746
7747 /* Parse the template-parameter. */
7748 parameter = cp_parser_template_parameter (parser);
7749 /* Add it to the list. */
7750 parameter_list = process_template_parm (parameter_list,
7751 parameter);
7752
7753 /* Peek at the next token. */
7754 token = cp_lexer_peek_token (parser->lexer);
7755 /* If it's not a `,', we're done. */
7756 if (token->type != CPP_COMMA)
7757 break;
7758 /* Otherwise, consume the `,' token. */
7759 cp_lexer_consume_token (parser->lexer);
7760 }
7761
7762 return parameter_list;
7763}
7764
7765/* Parse a template-parameter.
7766
7767 template-parameter:
7768 type-parameter
7769 parameter-declaration
7770
7771 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7772 TREE_PURPOSE is the default value, if any. */
7773
7774static tree
7775cp_parser_template_parameter (parser)
7776 cp_parser *parser;
7777{
7778 cp_token *token;
7779
7780 /* Peek at the next token. */
7781 token = cp_lexer_peek_token (parser->lexer);
7782 /* If it is `class' or `template', we have a type-parameter. */
7783 if (token->keyword == RID_TEMPLATE)
7784 return cp_parser_type_parameter (parser);
7785 /* If it is `class' or `typename' we do not know yet whether it is a
7786 type parameter or a non-type parameter. Consider:
7787
7788 template <typename T, typename T::X X> ...
7789
7790 or:
7791
7792 template <class C, class D*> ...
7793
7794 Here, the first parameter is a type parameter, and the second is
7795 a non-type parameter. We can tell by looking at the token after
7796 the identifier -- if it is a `,', `=', or `>' then we have a type
7797 parameter. */
7798 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7799 {
7800 /* Peek at the token after `class' or `typename'. */
7801 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7802 /* If it's an identifier, skip it. */
7803 if (token->type == CPP_NAME)
7804 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7805 /* Now, see if the token looks like the end of a template
7806 parameter. */
7807 if (token->type == CPP_COMMA
7808 || token->type == CPP_EQ
7809 || token->type == CPP_GREATER)
7810 return cp_parser_type_parameter (parser);
7811 }
7812
7813 /* Otherwise, it is a non-type parameter.
7814
7815 [temp.param]
7816
7817 When parsing a default template-argument for a non-type
7818 template-parameter, the first non-nested `>' is taken as the end
7819 of the template parameter-list rather than a greater-than
7820 operator. */
7821 return
ec194454 7822 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
a723baf1
MM
7823}
7824
7825/* Parse a type-parameter.
7826
7827 type-parameter:
7828 class identifier [opt]
7829 class identifier [opt] = type-id
7830 typename identifier [opt]
7831 typename identifier [opt] = type-id
7832 template < template-parameter-list > class identifier [opt]
7833 template < template-parameter-list > class identifier [opt]
7834 = id-expression
7835
7836 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7837 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7838 the declaration of the parameter. */
7839
7840static tree
7841cp_parser_type_parameter (parser)
7842 cp_parser *parser;
7843{
7844 cp_token *token;
7845 tree parameter;
7846
7847 /* Look for a keyword to tell us what kind of parameter this is. */
7848 token = cp_parser_require (parser, CPP_KEYWORD,
7849 "expected `class', `typename', or `template'");
7850 if (!token)
7851 return error_mark_node;
7852
7853 switch (token->keyword)
7854 {
7855 case RID_CLASS:
7856 case RID_TYPENAME:
7857 {
7858 tree identifier;
7859 tree default_argument;
7860
7861 /* If the next token is an identifier, then it names the
7862 parameter. */
7863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7864 identifier = cp_parser_identifier (parser);
7865 else
7866 identifier = NULL_TREE;
7867
7868 /* Create the parameter. */
7869 parameter = finish_template_type_parm (class_type_node, identifier);
7870
7871 /* If the next token is an `=', we have a default argument. */
7872 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7873 {
7874 /* Consume the `=' token. */
7875 cp_lexer_consume_token (parser->lexer);
7876 /* Parse the default-argumen. */
7877 default_argument = cp_parser_type_id (parser);
7878 }
7879 else
7880 default_argument = NULL_TREE;
7881
7882 /* Create the combined representation of the parameter and the
7883 default argument. */
7884 parameter = build_tree_list (default_argument,
7885 parameter);
7886 }
7887 break;
7888
7889 case RID_TEMPLATE:
7890 {
7891 tree parameter_list;
7892 tree identifier;
7893 tree default_argument;
7894
7895 /* Look for the `<'. */
7896 cp_parser_require (parser, CPP_LESS, "`<'");
7897 /* Parse the template-parameter-list. */
7898 begin_template_parm_list ();
7899 parameter_list
7900 = cp_parser_template_parameter_list (parser);
7901 parameter_list = end_template_parm_list (parameter_list);
7902 /* Look for the `>'. */
7903 cp_parser_require (parser, CPP_GREATER, "`>'");
7904 /* Look for the `class' keyword. */
7905 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7906 /* If the next token is an `=', then there is a
7907 default-argument. If the next token is a `>', we are at
7908 the end of the parameter-list. If the next token is a `,',
7909 then we are at the end of this parameter. */
7910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7911 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7912 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7913 identifier = cp_parser_identifier (parser);
7914 else
7915 identifier = NULL_TREE;
7916 /* Create the template parameter. */
7917 parameter = finish_template_template_parm (class_type_node,
7918 identifier);
7919
7920 /* If the next token is an `=', then there is a
7921 default-argument. */
7922 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7923 {
7924 /* Consume the `='. */
7925 cp_lexer_consume_token (parser->lexer);
7926 /* Parse the id-expression. */
7927 default_argument
7928 = cp_parser_id_expression (parser,
7929 /*template_keyword_p=*/false,
7930 /*check_dependency_p=*/true,
7931 /*template_p=*/NULL);
7932 /* Look up the name. */
7933 default_argument
7934 = cp_parser_lookup_name_simple (parser, default_argument);
7935 /* See if the default argument is valid. */
7936 default_argument
7937 = check_template_template_default_arg (default_argument);
7938 }
7939 else
7940 default_argument = NULL_TREE;
7941
7942 /* Create the combined representation of the parameter and the
7943 default argument. */
7944 parameter = build_tree_list (default_argument,
7945 parameter);
7946 }
7947 break;
7948
7949 default:
7950 /* Anything else is an error. */
7951 cp_parser_error (parser,
7952 "expected `class', `typename', or `template'");
7953 parameter = error_mark_node;
7954 }
7955
7956 return parameter;
7957}
7958
7959/* Parse a template-id.
7960
7961 template-id:
7962 template-name < template-argument-list [opt] >
7963
7964 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7965 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7966 returned. Otherwise, if the template-name names a function, or set
7967 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7968 names a class, returns a TYPE_DECL for the specialization.
7969
7970 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7971 uninstantiated templates. */
7972
7973static tree
7974cp_parser_template_id (cp_parser *parser,
7975 bool template_keyword_p,
7976 bool check_dependency_p)
7977{
7978 tree template;
7979 tree arguments;
7980 tree saved_scope;
7981 tree saved_qualifying_scope;
7982 tree saved_object_scope;
7983 tree template_id;
7984 bool saved_greater_than_is_operator_p;
7985 ptrdiff_t start_of_id;
7986 tree access_check = NULL_TREE;
2050a1bb 7987 cp_token *next_token;
a723baf1
MM
7988
7989 /* If the next token corresponds to a template-id, there is no need
7990 to reparse it. */
2050a1bb
MM
7991 next_token = cp_lexer_peek_token (parser->lexer);
7992 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
7993 {
7994 tree value;
7995 tree check;
7996
7997 /* Get the stored value. */
7998 value = cp_lexer_consume_token (parser->lexer)->value;
7999 /* Perform any access checks that were deferred. */
8000 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
8001 perform_or_defer_access_check (TREE_PURPOSE (check),
8002 TREE_VALUE (check));
a723baf1
MM
8003 /* Return the stored value. */
8004 return TREE_VALUE (value);
8005 }
8006
2050a1bb
MM
8007 /* Avoid performing name lookup if there is no possibility of
8008 finding a template-id. */
8009 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8010 || (next_token->type == CPP_NAME
8011 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
8012 {
8013 cp_parser_error (parser, "expected template-id");
8014 return error_mark_node;
8015 }
8016
a723baf1
MM
8017 /* Remember where the template-id starts. */
8018 if (cp_parser_parsing_tentatively (parser)
8019 && !cp_parser_committed_to_tentative_parse (parser))
8020 {
2050a1bb 8021 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
8022 start_of_id = cp_lexer_token_difference (parser->lexer,
8023 parser->lexer->first_token,
8024 next_token);
a723baf1
MM
8025 }
8026 else
8027 start_of_id = -1;
8028
cf22909c
KL
8029 push_deferring_access_checks (true);
8030
a723baf1
MM
8031 /* Parse the template-name. */
8032 template = cp_parser_template_name (parser, template_keyword_p,
8033 check_dependency_p);
8034 if (template == error_mark_node)
cf22909c
KL
8035 {
8036 pop_deferring_access_checks ();
8037 return error_mark_node;
8038 }
a723baf1
MM
8039
8040 /* Look for the `<' that starts the template-argument-list. */
8041 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
cf22909c
KL
8042 {
8043 pop_deferring_access_checks ();
8044 return error_mark_node;
8045 }
a723baf1
MM
8046
8047 /* [temp.names]
8048
8049 When parsing a template-id, the first non-nested `>' is taken as
8050 the end of the template-argument-list rather than a greater-than
8051 operator. */
8052 saved_greater_than_is_operator_p
8053 = parser->greater_than_is_operator_p;
8054 parser->greater_than_is_operator_p = false;
8055 /* Parsing the argument list may modify SCOPE, so we save it
8056 here. */
8057 saved_scope = parser->scope;
8058 saved_qualifying_scope = parser->qualifying_scope;
8059 saved_object_scope = parser->object_scope;
8060 /* Parse the template-argument-list itself. */
8061 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8062 arguments = NULL_TREE;
8063 else
8064 arguments = cp_parser_template_argument_list (parser);
8065 /* Look for the `>' that ends the template-argument-list. */
8066 cp_parser_require (parser, CPP_GREATER, "`>'");
8067 /* The `>' token might be a greater-than operator again now. */
8068 parser->greater_than_is_operator_p
8069 = saved_greater_than_is_operator_p;
8070 /* Restore the SAVED_SCOPE. */
8071 parser->scope = saved_scope;
8072 parser->qualifying_scope = saved_qualifying_scope;
8073 parser->object_scope = saved_object_scope;
8074
8075 /* Build a representation of the specialization. */
8076 if (TREE_CODE (template) == IDENTIFIER_NODE)
8077 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8078 else if (DECL_CLASS_TEMPLATE_P (template)
8079 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8080 template_id
8081 = finish_template_type (template, arguments,
8082 cp_lexer_next_token_is (parser->lexer,
8083 CPP_SCOPE));
8084 else
8085 {
8086 /* If it's not a class-template or a template-template, it should be
8087 a function-template. */
8088 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8089 || TREE_CODE (template) == OVERLOAD
8090 || BASELINK_P (template)),
8091 20010716);
8092
8093 template_id = lookup_template_function (template, arguments);
8094 }
8095
cf22909c
KL
8096 /* Retrieve any deferred checks. Do not pop this access checks yet
8097 so the memory will not be reclaimed during token replacing below. */
8098 access_check = get_deferred_access_checks ();
8099
a723baf1
MM
8100 /* If parsing tentatively, replace the sequence of tokens that makes
8101 up the template-id with a CPP_TEMPLATE_ID token. That way,
8102 should we re-parse the token stream, we will not have to repeat
8103 the effort required to do the parse, nor will we issue duplicate
8104 error messages about problems during instantiation of the
8105 template. */
8106 if (start_of_id >= 0)
8107 {
8108 cp_token *token;
a723baf1
MM
8109
8110 /* Find the token that corresponds to the start of the
8111 template-id. */
8112 token = cp_lexer_advance_token (parser->lexer,
8113 parser->lexer->first_token,
8114 start_of_id);
8115
a723baf1
MM
8116 /* Reset the contents of the START_OF_ID token. */
8117 token->type = CPP_TEMPLATE_ID;
8118 token->value = build_tree_list (access_check, template_id);
8119 token->keyword = RID_MAX;
8120 /* Purge all subsequent tokens. */
8121 cp_lexer_purge_tokens_after (parser->lexer, token);
8122 }
8123
cf22909c 8124 pop_deferring_access_checks ();
a723baf1
MM
8125 return template_id;
8126}
8127
8128/* Parse a template-name.
8129
8130 template-name:
8131 identifier
8132
8133 The standard should actually say:
8134
8135 template-name:
8136 identifier
8137 operator-function-id
8138 conversion-function-id
8139
8140 A defect report has been filed about this issue.
8141
8142 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8143 `template' keyword, in a construction like:
8144
8145 T::template f<3>()
8146
8147 In that case `f' is taken to be a template-name, even though there
8148 is no way of knowing for sure.
8149
8150 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8151 name refers to a set of overloaded functions, at least one of which
8152 is a template, or an IDENTIFIER_NODE with the name of the template,
8153 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8154 names are looked up inside uninstantiated templates. */
8155
8156static tree
8157cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8158 cp_parser *parser;
8159 bool template_keyword_p;
8160 bool check_dependency_p;
8161{
8162 tree identifier;
8163 tree decl;
8164 tree fns;
8165
8166 /* If the next token is `operator', then we have either an
8167 operator-function-id or a conversion-function-id. */
8168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8169 {
8170 /* We don't know whether we're looking at an
8171 operator-function-id or a conversion-function-id. */
8172 cp_parser_parse_tentatively (parser);
8173 /* Try an operator-function-id. */
8174 identifier = cp_parser_operator_function_id (parser);
8175 /* If that didn't work, try a conversion-function-id. */
8176 if (!cp_parser_parse_definitely (parser))
8177 identifier = cp_parser_conversion_function_id (parser);
8178 }
8179 /* Look for the identifier. */
8180 else
8181 identifier = cp_parser_identifier (parser);
8182
8183 /* If we didn't find an identifier, we don't have a template-id. */
8184 if (identifier == error_mark_node)
8185 return error_mark_node;
8186
8187 /* If the name immediately followed the `template' keyword, then it
8188 is a template-name. However, if the next token is not `<', then
8189 we do not treat it as a template-name, since it is not being used
8190 as part of a template-id. This enables us to handle constructs
8191 like:
8192
8193 template <typename T> struct S { S(); };
8194 template <typename T> S<T>::S();
8195
8196 correctly. We would treat `S' as a template -- if it were `S<T>'
8197 -- but we do not if there is no `<'. */
8198 if (template_keyword_p && processing_template_decl
8199 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8200 return identifier;
8201
8202 /* Look up the name. */
8203 decl = cp_parser_lookup_name (parser, identifier,
8204 /*check_access=*/true,
8205 /*is_type=*/false,
eea9800f 8206 /*is_namespace=*/false,
a723baf1
MM
8207 check_dependency_p);
8208 decl = maybe_get_template_decl_from_type_decl (decl);
8209
8210 /* If DECL is a template, then the name was a template-name. */
8211 if (TREE_CODE (decl) == TEMPLATE_DECL)
8212 ;
8213 else
8214 {
8215 /* The standard does not explicitly indicate whether a name that
8216 names a set of overloaded declarations, some of which are
8217 templates, is a template-name. However, such a name should
8218 be a template-name; otherwise, there is no way to form a
8219 template-id for the overloaded templates. */
8220 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8221 if (TREE_CODE (fns) == OVERLOAD)
8222 {
8223 tree fn;
8224
8225 for (fn = fns; fn; fn = OVL_NEXT (fn))
8226 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8227 break;
8228 }
8229 else
8230 {
8231 /* Otherwise, the name does not name a template. */
8232 cp_parser_error (parser, "expected template-name");
8233 return error_mark_node;
8234 }
8235 }
8236
8237 /* If DECL is dependent, and refers to a function, then just return
8238 its name; we will look it up again during template instantiation. */
8239 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8240 {
8241 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8242 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8243 return identifier;
8244 }
8245
8246 return decl;
8247}
8248
8249/* Parse a template-argument-list.
8250
8251 template-argument-list:
8252 template-argument
8253 template-argument-list , template-argument
8254
8255 Returns a TREE_LIST representing the arguments, in the order they
8256 appeared. The TREE_VALUE of each node is a representation of the
8257 argument. */
8258
8259static tree
8260cp_parser_template_argument_list (parser)
8261 cp_parser *parser;
8262{
8263 tree arguments = NULL_TREE;
8264
8265 while (true)
8266 {
8267 tree argument;
8268
8269 /* Parse the template-argument. */
8270 argument = cp_parser_template_argument (parser);
8271 /* Add it to the list. */
8272 arguments = tree_cons (NULL_TREE, argument, arguments);
8273 /* If it is not a `,', then there are no more arguments. */
8274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8275 break;
8276 /* Otherwise, consume the ','. */
8277 cp_lexer_consume_token (parser->lexer);
8278 }
8279
8280 /* We built up the arguments in reverse order. */
8281 return nreverse (arguments);
8282}
8283
8284/* Parse a template-argument.
8285
8286 template-argument:
8287 assignment-expression
8288 type-id
8289 id-expression
8290
8291 The representation is that of an assignment-expression, type-id, or
8292 id-expression -- except that the qualified id-expression is
8293 evaluated, so that the value returned is either a DECL or an
8294 OVERLOAD. */
8295
8296static tree
8297cp_parser_template_argument (parser)
8298 cp_parser *parser;
8299{
8300 tree argument;
8301 bool template_p;
8302
8303 /* There's really no way to know what we're looking at, so we just
8304 try each alternative in order.
8305
8306 [temp.arg]
8307
8308 In a template-argument, an ambiguity between a type-id and an
8309 expression is resolved to a type-id, regardless of the form of
8310 the corresponding template-parameter.
8311
8312 Therefore, we try a type-id first. */
8313 cp_parser_parse_tentatively (parser);
a723baf1
MM
8314 argument = cp_parser_type_id (parser);
8315 /* If the next token isn't a `,' or a `>', then this argument wasn't
8316 really finished. */
8317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8318 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8319 cp_parser_error (parser, "expected template-argument");
8320 /* If that worked, we're done. */
8321 if (cp_parser_parse_definitely (parser))
8322 return argument;
8323 /* We're still not sure what the argument will be. */
8324 cp_parser_parse_tentatively (parser);
8325 /* Try a template. */
8326 argument = cp_parser_id_expression (parser,
8327 /*template_keyword_p=*/false,
8328 /*check_dependency_p=*/true,
8329 &template_p);
8330 /* If the next token isn't a `,' or a `>', then this argument wasn't
8331 really finished. */
8332 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8333 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8334 cp_parser_error (parser, "expected template-argument");
8335 if (!cp_parser_error_occurred (parser))
8336 {
8337 /* Figure out what is being referred to. */
8338 argument = cp_parser_lookup_name_simple (parser, argument);
8339 if (template_p)
8340 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8341 TREE_OPERAND (argument, 1),
8342 tf_error | tf_parsing);
8343 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8344 cp_parser_error (parser, "expected template-name");
8345 }
8346 if (cp_parser_parse_definitely (parser))
8347 return argument;
8348 /* It must be an assignment-expression. */
8349 return cp_parser_assignment_expression (parser);
8350}
8351
8352/* Parse an explicit-instantiation.
8353
8354 explicit-instantiation:
8355 template declaration
8356
8357 Although the standard says `declaration', what it really means is:
8358
8359 explicit-instantiation:
8360 template decl-specifier-seq [opt] declarator [opt] ;
8361
8362 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8363 supposed to be allowed. A defect report has been filed about this
8364 issue.
8365
8366 GNU Extension:
8367
8368 explicit-instantiation:
8369 storage-class-specifier template
8370 decl-specifier-seq [opt] declarator [opt] ;
8371 function-specifier template
8372 decl-specifier-seq [opt] declarator [opt] ; */
8373
8374static void
8375cp_parser_explicit_instantiation (parser)
8376 cp_parser *parser;
8377{
8378 bool declares_class_or_enum;
8379 tree decl_specifiers;
8380 tree attributes;
8381 tree extension_specifier = NULL_TREE;
8382
8383 /* Look for an (optional) storage-class-specifier or
8384 function-specifier. */
8385 if (cp_parser_allow_gnu_extensions_p (parser))
8386 {
8387 extension_specifier
8388 = cp_parser_storage_class_specifier_opt (parser);
8389 if (!extension_specifier)
8390 extension_specifier = cp_parser_function_specifier_opt (parser);
8391 }
8392
8393 /* Look for the `template' keyword. */
8394 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8395 /* Let the front end know that we are processing an explicit
8396 instantiation. */
8397 begin_explicit_instantiation ();
8398 /* [temp.explicit] says that we are supposed to ignore access
8399 control while processing explicit instantiation directives. */
8400 scope_chain->check_access = 0;
8401 /* Parse a decl-specifier-seq. */
8402 decl_specifiers
8403 = cp_parser_decl_specifier_seq (parser,
8404 CP_PARSER_FLAGS_OPTIONAL,
8405 &attributes,
8406 &declares_class_or_enum);
8407 /* If there was exactly one decl-specifier, and it declared a class,
8408 and there's no declarator, then we have an explicit type
8409 instantiation. */
8410 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8411 {
8412 tree type;
8413
8414 type = check_tag_decl (decl_specifiers);
8415 if (type)
8416 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8417 }
8418 else
8419 {
8420 tree declarator;
8421 tree decl;
8422
8423 /* Parse the declarator. */
8424 declarator
62b8a44e 8425 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
8426 /*ctor_dtor_or_conv_p=*/NULL);
8427 decl = grokdeclarator (declarator, decl_specifiers,
8428 NORMAL, 0, NULL);
8429 /* Do the explicit instantiation. */
8430 do_decl_instantiation (decl, extension_specifier);
8431 }
8432 /* We're done with the instantiation. */
8433 end_explicit_instantiation ();
8434 /* Trun access control back on. */
8435 scope_chain->check_access = flag_access_control;
8436
8437 /* Look for the trailing `;'. */
8438 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8439}
8440
8441/* Parse an explicit-specialization.
8442
8443 explicit-specialization:
8444 template < > declaration
8445
8446 Although the standard says `declaration', what it really means is:
8447
8448 explicit-specialization:
8449 template <> decl-specifier [opt] init-declarator [opt] ;
8450 template <> function-definition
8451 template <> explicit-specialization
8452 template <> template-declaration */
8453
8454static void
8455cp_parser_explicit_specialization (parser)
8456 cp_parser *parser;
8457{
8458 /* Look for the `template' keyword. */
8459 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8460 /* Look for the `<'. */
8461 cp_parser_require (parser, CPP_LESS, "`<'");
8462 /* Look for the `>'. */
8463 cp_parser_require (parser, CPP_GREATER, "`>'");
8464 /* We have processed another parameter list. */
8465 ++parser->num_template_parameter_lists;
8466 /* Let the front end know that we are beginning a specialization. */
8467 begin_specialization ();
8468
8469 /* If the next keyword is `template', we need to figure out whether
8470 or not we're looking a template-declaration. */
8471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8472 {
8473 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8474 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8475 cp_parser_template_declaration_after_export (parser,
8476 /*member_p=*/false);
8477 else
8478 cp_parser_explicit_specialization (parser);
8479 }
8480 else
8481 /* Parse the dependent declaration. */
8482 cp_parser_single_declaration (parser,
8483 /*member_p=*/false,
8484 /*friend_p=*/NULL);
8485
8486 /* We're done with the specialization. */
8487 end_specialization ();
8488 /* We're done with this parameter list. */
8489 --parser->num_template_parameter_lists;
8490}
8491
8492/* Parse a type-specifier.
8493
8494 type-specifier:
8495 simple-type-specifier
8496 class-specifier
8497 enum-specifier
8498 elaborated-type-specifier
8499 cv-qualifier
8500
8501 GNU Extension:
8502
8503 type-specifier:
8504 __complex__
8505
8506 Returns a representation of the type-specifier. If the
8507 type-specifier is a keyword (like `int' or `const', or
8508 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8509 For a class-specifier, enum-specifier, or elaborated-type-specifier
8510 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8511
8512 If IS_FRIEND is TRUE then this type-specifier is being declared a
8513 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8514 appearing in a decl-specifier-seq.
8515
8516 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8517 class-specifier, enum-specifier, or elaborated-type-specifier, then
8518 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8519 FALSE.
8520
8521 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8522 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8523 is set to FALSE. */
8524
8525static tree
8526cp_parser_type_specifier (parser,
8527 flags,
8528 is_friend,
8529 is_declaration,
8530 declares_class_or_enum,
8531 is_cv_qualifier)
8532 cp_parser *parser;
8533 cp_parser_flags flags;
8534 bool is_friend;
8535 bool is_declaration;
8536 bool *declares_class_or_enum;
8537 bool *is_cv_qualifier;
8538{
8539 tree type_spec = NULL_TREE;
8540 cp_token *token;
8541 enum rid keyword;
8542
8543 /* Assume this type-specifier does not declare a new type. */
8544 if (declares_class_or_enum)
8545 *declares_class_or_enum = false;
8546 /* And that it does not specify a cv-qualifier. */
8547 if (is_cv_qualifier)
8548 *is_cv_qualifier = false;
8549 /* Peek at the next token. */
8550 token = cp_lexer_peek_token (parser->lexer);
8551
8552 /* If we're looking at a keyword, we can use that to guide the
8553 production we choose. */
8554 keyword = token->keyword;
8555 switch (keyword)
8556 {
8557 /* Any of these indicate either a class-specifier, or an
8558 elaborated-type-specifier. */
8559 case RID_CLASS:
8560 case RID_STRUCT:
8561 case RID_UNION:
8562 case RID_ENUM:
8563 /* Parse tentatively so that we can back up if we don't find a
8564 class-specifier or enum-specifier. */
8565 cp_parser_parse_tentatively (parser);
8566 /* Look for the class-specifier or enum-specifier. */
8567 if (keyword == RID_ENUM)
8568 type_spec = cp_parser_enum_specifier (parser);
8569 else
8570 type_spec = cp_parser_class_specifier (parser);
8571
8572 /* If that worked, we're done. */
8573 if (cp_parser_parse_definitely (parser))
8574 {
8575 if (declares_class_or_enum)
8576 *declares_class_or_enum = true;
8577 return type_spec;
8578 }
8579
8580 /* Fall through. */
8581
8582 case RID_TYPENAME:
8583 /* Look for an elaborated-type-specifier. */
8584 type_spec = cp_parser_elaborated_type_specifier (parser,
8585 is_friend,
8586 is_declaration);
8587 /* We're declaring a class or enum -- unless we're using
8588 `typename'. */
8589 if (declares_class_or_enum && keyword != RID_TYPENAME)
8590 *declares_class_or_enum = true;
8591 return type_spec;
8592
8593 case RID_CONST:
8594 case RID_VOLATILE:
8595 case RID_RESTRICT:
8596 type_spec = cp_parser_cv_qualifier_opt (parser);
8597 /* Even though we call a routine that looks for an optional
8598 qualifier, we know that there should be one. */
8599 my_friendly_assert (type_spec != NULL, 20000328);
8600 /* This type-specifier was a cv-qualified. */
8601 if (is_cv_qualifier)
8602 *is_cv_qualifier = true;
8603
8604 return type_spec;
8605
8606 case RID_COMPLEX:
8607 /* The `__complex__' keyword is a GNU extension. */
8608 return cp_lexer_consume_token (parser->lexer)->value;
8609
8610 default:
8611 break;
8612 }
8613
8614 /* If we do not already have a type-specifier, assume we are looking
8615 at a simple-type-specifier. */
8616 type_spec = cp_parser_simple_type_specifier (parser, flags);
8617
8618 /* If we didn't find a type-specifier, and a type-specifier was not
8619 optional in this context, issue an error message. */
8620 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8621 {
8622 cp_parser_error (parser, "expected type specifier");
8623 return error_mark_node;
8624 }
8625
8626 return type_spec;
8627}
8628
8629/* Parse a simple-type-specifier.
8630
8631 simple-type-specifier:
8632 :: [opt] nested-name-specifier [opt] type-name
8633 :: [opt] nested-name-specifier template template-id
8634 char
8635 wchar_t
8636 bool
8637 short
8638 int
8639 long
8640 signed
8641 unsigned
8642 float
8643 double
8644 void
8645
8646 GNU Extension:
8647
8648 simple-type-specifier:
8649 __typeof__ unary-expression
8650 __typeof__ ( type-id )
8651
8652 For the various keywords, the value returned is simply the
8653 TREE_IDENTIFIER representing the keyword. For the first two
8654 productions, the value returned is the indicated TYPE_DECL. */
8655
8656static tree
8657cp_parser_simple_type_specifier (parser, flags)
8658 cp_parser *parser;
8659 cp_parser_flags flags;
8660{
8661 tree type = NULL_TREE;
8662 cp_token *token;
8663
8664 /* Peek at the next token. */
8665 token = cp_lexer_peek_token (parser->lexer);
8666
8667 /* If we're looking at a keyword, things are easy. */
8668 switch (token->keyword)
8669 {
8670 case RID_CHAR:
8671 case RID_WCHAR:
8672 case RID_BOOL:
8673 case RID_SHORT:
8674 case RID_INT:
8675 case RID_LONG:
8676 case RID_SIGNED:
8677 case RID_UNSIGNED:
8678 case RID_FLOAT:
8679 case RID_DOUBLE:
8680 case RID_VOID:
8681 /* Consume the token. */
8682 return cp_lexer_consume_token (parser->lexer)->value;
8683
8684 case RID_TYPEOF:
8685 {
8686 tree operand;
8687
8688 /* Consume the `typeof' token. */
8689 cp_lexer_consume_token (parser->lexer);
8690 /* Parse the operand to `typeof' */
8691 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8692 /* If it is not already a TYPE, take its type. */
8693 if (!TYPE_P (operand))
8694 operand = finish_typeof (operand);
8695
8696 return operand;
8697 }
8698
8699 default:
8700 break;
8701 }
8702
8703 /* The type-specifier must be a user-defined type. */
8704 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8705 {
8706 /* Don't gobble tokens or issue error messages if this is an
8707 optional type-specifier. */
8708 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8709 cp_parser_parse_tentatively (parser);
8710
8711 /* Look for the optional `::' operator. */
8712 cp_parser_global_scope_opt (parser,
8713 /*current_scope_valid_p=*/false);
8714 /* Look for the nested-name specifier. */
8715 cp_parser_nested_name_specifier_opt (parser,
8716 /*typename_keyword_p=*/false,
8717 /*check_dependency_p=*/true,
8718 /*type_p=*/false);
8719 /* If we have seen a nested-name-specifier, and the next token
8720 is `template', then we are using the template-id production. */
8721 if (parser->scope
8722 && cp_parser_optional_template_keyword (parser))
8723 {
8724 /* Look for the template-id. */
8725 type = cp_parser_template_id (parser,
8726 /*template_keyword_p=*/true,
8727 /*check_dependency_p=*/true);
8728 /* If the template-id did not name a type, we are out of
8729 luck. */
8730 if (TREE_CODE (type) != TYPE_DECL)
8731 {
8732 cp_parser_error (parser, "expected template-id for type");
8733 type = NULL_TREE;
8734 }
8735 }
8736 /* Otherwise, look for a type-name. */
8737 else
8738 {
8739 type = cp_parser_type_name (parser);
8740 if (type == error_mark_node)
8741 type = NULL_TREE;
8742 }
8743
8744 /* If it didn't work out, we don't have a TYPE. */
8745 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8746 && !cp_parser_parse_definitely (parser))
8747 type = NULL_TREE;
8748 }
8749
8750 /* If we didn't get a type-name, issue an error message. */
8751 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8752 {
8753 cp_parser_error (parser, "expected type-name");
8754 return error_mark_node;
8755 }
8756
8757 return type;
8758}
8759
8760/* Parse a type-name.
8761
8762 type-name:
8763 class-name
8764 enum-name
8765 typedef-name
8766
8767 enum-name:
8768 identifier
8769
8770 typedef-name:
8771 identifier
8772
8773 Returns a TYPE_DECL for the the type. */
8774
8775static tree
8776cp_parser_type_name (parser)
8777 cp_parser *parser;
8778{
8779 tree type_decl;
8780 tree identifier;
8781
8782 /* We can't know yet whether it is a class-name or not. */
8783 cp_parser_parse_tentatively (parser);
8784 /* Try a class-name. */
8785 type_decl = cp_parser_class_name (parser,
8786 /*typename_keyword_p=*/false,
8787 /*template_keyword_p=*/false,
8788 /*type_p=*/false,
8789 /*check_access_p=*/true,
8790 /*check_dependency_p=*/true,
8791 /*class_head_p=*/false);
8792 /* If it's not a class-name, keep looking. */
8793 if (!cp_parser_parse_definitely (parser))
8794 {
8795 /* It must be a typedef-name or an enum-name. */
8796 identifier = cp_parser_identifier (parser);
8797 if (identifier == error_mark_node)
8798 return error_mark_node;
8799
8800 /* Look up the type-name. */
8801 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8802 /* Issue an error if we did not find a type-name. */
8803 if (TREE_CODE (type_decl) != TYPE_DECL)
8804 {
8805 cp_parser_error (parser, "expected type-name");
8806 type_decl = error_mark_node;
8807 }
8808 /* Remember that the name was used in the definition of the
8809 current class so that we can check later to see if the
8810 meaning would have been different after the class was
8811 entirely defined. */
8812 else if (type_decl != error_mark_node
8813 && !parser->scope)
8814 maybe_note_name_used_in_class (identifier, type_decl);
8815 }
8816
8817 return type_decl;
8818}
8819
8820
8821/* Parse an elaborated-type-specifier. Note that the grammar given
8822 here incorporates the resolution to DR68.
8823
8824 elaborated-type-specifier:
8825 class-key :: [opt] nested-name-specifier [opt] identifier
8826 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8827 enum :: [opt] nested-name-specifier [opt] identifier
8828 typename :: [opt] nested-name-specifier identifier
8829 typename :: [opt] nested-name-specifier template [opt]
8830 template-id
8831
8832 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8833 declared `friend'. If IS_DECLARATION is TRUE, then this
8834 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8835 something is being declared.
8836
8837 Returns the TYPE specified. */
8838
8839static tree
8840cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8841 cp_parser *parser;
8842 bool is_friend;
8843 bool is_declaration;
8844{
8845 enum tag_types tag_type;
8846 tree identifier;
8847 tree type = NULL_TREE;
8848
8849 /* See if we're looking at the `enum' keyword. */
8850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8851 {
8852 /* Consume the `enum' token. */
8853 cp_lexer_consume_token (parser->lexer);
8854 /* Remember that it's an enumeration type. */
8855 tag_type = enum_type;
8856 }
8857 /* Or, it might be `typename'. */
8858 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8859 RID_TYPENAME))
8860 {
8861 /* Consume the `typename' token. */
8862 cp_lexer_consume_token (parser->lexer);
8863 /* Remember that it's a `typename' type. */
8864 tag_type = typename_type;
8865 /* The `typename' keyword is only allowed in templates. */
8866 if (!processing_template_decl)
8867 pedwarn ("using `typename' outside of template");
8868 }
8869 /* Otherwise it must be a class-key. */
8870 else
8871 {
8872 tag_type = cp_parser_class_key (parser);
8873 if (tag_type == none_type)
8874 return error_mark_node;
8875 }
8876
8877 /* Look for the `::' operator. */
8878 cp_parser_global_scope_opt (parser,
8879 /*current_scope_valid_p=*/false);
8880 /* Look for the nested-name-specifier. */
8881 if (tag_type == typename_type)
8fa1ad0e
MM
8882 {
8883 if (cp_parser_nested_name_specifier (parser,
8884 /*typename_keyword_p=*/true,
8885 /*check_dependency_p=*/true,
8886 /*type_p=*/true)
8887 == error_mark_node)
8888 return error_mark_node;
8889 }
a723baf1
MM
8890 else
8891 /* Even though `typename' is not present, the proposed resolution
8892 to Core Issue 180 says that in `class A<T>::B', `B' should be
8893 considered a type-name, even if `A<T>' is dependent. */
8894 cp_parser_nested_name_specifier_opt (parser,
8895 /*typename_keyword_p=*/true,
8896 /*check_dependency_p=*/true,
8897 /*type_p=*/true);
8898 /* For everything but enumeration types, consider a template-id. */
8899 if (tag_type != enum_type)
8900 {
8901 bool template_p = false;
8902 tree decl;
8903
8904 /* Allow the `template' keyword. */
8905 template_p = cp_parser_optional_template_keyword (parser);
8906 /* If we didn't see `template', we don't know if there's a
8907 template-id or not. */
8908 if (!template_p)
8909 cp_parser_parse_tentatively (parser);
8910 /* Parse the template-id. */
8911 decl = cp_parser_template_id (parser, template_p,
8912 /*check_dependency_p=*/true);
8913 /* If we didn't find a template-id, look for an ordinary
8914 identifier. */
8915 if (!template_p && !cp_parser_parse_definitely (parser))
8916 ;
8917 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8918 in effect, then we must assume that, upon instantiation, the
8919 template will correspond to a class. */
8920 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8921 && tag_type == typename_type)
8922 type = make_typename_type (parser->scope, decl,
8923 /*complain=*/1);
8924 else
8925 type = TREE_TYPE (decl);
8926 }
8927
8928 /* For an enumeration type, consider only a plain identifier. */
8929 if (!type)
8930 {
8931 identifier = cp_parser_identifier (parser);
8932
8933 if (identifier == error_mark_node)
8934 return error_mark_node;
8935
8936 /* For a `typename', we needn't call xref_tag. */
8937 if (tag_type == typename_type)
8938 return make_typename_type (parser->scope, identifier,
8939 /*complain=*/1);
8940 /* Look up a qualified name in the usual way. */
8941 if (parser->scope)
8942 {
8943 tree decl;
8944
8945 /* In an elaborated-type-specifier, names are assumed to name
8946 types, so we set IS_TYPE to TRUE when calling
8947 cp_parser_lookup_name. */
8948 decl = cp_parser_lookup_name (parser, identifier,
8949 /*check_access=*/true,
8950 /*is_type=*/true,
eea9800f 8951 /*is_namespace=*/false,
a723baf1
MM
8952 /*check_dependency=*/true);
8953 decl = (cp_parser_maybe_treat_template_as_class
8954 (decl, /*tag_name_p=*/is_friend));
8955
8956 if (TREE_CODE (decl) != TYPE_DECL)
8957 {
8958 error ("expected type-name");
8959 return error_mark_node;
8960 }
8961 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8962 && tag_type != enum_type)
8963 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8964 tag_type == record_type ? "struct" : "class");
8965 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8966 && tag_type == enum_type)
8967 error ("`%T' referred to as enum", TREE_TYPE (decl));
8968
8969 type = TREE_TYPE (decl);
8970 }
8971 else
8972 {
8973 /* An elaborated-type-specifier sometimes introduces a new type and
8974 sometimes names an existing type. Normally, the rule is that it
8975 introduces a new type only if there is not an existing type of
8976 the same name already in scope. For example, given:
8977
8978 struct S {};
8979 void f() { struct S s; }
8980
8981 the `struct S' in the body of `f' is the same `struct S' as in
8982 the global scope; the existing definition is used. However, if
8983 there were no global declaration, this would introduce a new
8984 local class named `S'.
8985
8986 An exception to this rule applies to the following code:
8987
8988 namespace N { struct S; }
8989
8990 Here, the elaborated-type-specifier names a new type
8991 unconditionally; even if there is already an `S' in the
8992 containing scope this declaration names a new type.
8993 This exception only applies if the elaborated-type-specifier
8994 forms the complete declaration:
8995
8996 [class.name]
8997
8998 A declaration consisting solely of `class-key identifier ;' is
8999 either a redeclaration of the name in the current scope or a
9000 forward declaration of the identifier as a class name. It
9001 introduces the name into the current scope.
9002
9003 We are in this situation precisely when the next token is a `;'.
9004
9005 An exception to the exception is that a `friend' declaration does
9006 *not* name a new type; i.e., given:
9007
9008 struct S { friend struct T; };
9009
9010 `T' is not a new type in the scope of `S'.
9011
9012 Also, `new struct S' or `sizeof (struct S)' never results in the
9013 definition of a new type; a new type can only be declared in a
9014 declaration context. */
9015
9016 type = xref_tag (tag_type, identifier,
9017 /*attributes=*/NULL_TREE,
9018 (is_friend
9019 || !is_declaration
9020 || cp_lexer_next_token_is_not (parser->lexer,
9021 CPP_SEMICOLON)));
9022 }
9023 }
9024 if (tag_type != enum_type)
9025 cp_parser_check_class_key (tag_type, type);
9026 return type;
9027}
9028
9029/* Parse an enum-specifier.
9030
9031 enum-specifier:
9032 enum identifier [opt] { enumerator-list [opt] }
9033
9034 Returns an ENUM_TYPE representing the enumeration. */
9035
9036static tree
9037cp_parser_enum_specifier (parser)
9038 cp_parser *parser;
9039{
9040 cp_token *token;
9041 tree identifier = NULL_TREE;
9042 tree type;
9043
9044 /* Look for the `enum' keyword. */
9045 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9046 return error_mark_node;
9047 /* Peek at the next token. */
9048 token = cp_lexer_peek_token (parser->lexer);
9049
9050 /* See if it is an identifier. */
9051 if (token->type == CPP_NAME)
9052 identifier = cp_parser_identifier (parser);
9053
9054 /* Look for the `{'. */
9055 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9056 return error_mark_node;
9057
9058 /* At this point, we're going ahead with the enum-specifier, even
9059 if some other problem occurs. */
9060 cp_parser_commit_to_tentative_parse (parser);
9061
9062 /* Issue an error message if type-definitions are forbidden here. */
9063 cp_parser_check_type_definition (parser);
9064
9065 /* Create the new type. */
9066 type = start_enum (identifier ? identifier : make_anon_name ());
9067
9068 /* Peek at the next token. */
9069 token = cp_lexer_peek_token (parser->lexer);
9070 /* If it's not a `}', then there are some enumerators. */
9071 if (token->type != CPP_CLOSE_BRACE)
9072 cp_parser_enumerator_list (parser, type);
9073 /* Look for the `}'. */
9074 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9075
9076 /* Finish up the enumeration. */
9077 finish_enum (type);
9078
9079 return type;
9080}
9081
9082/* Parse an enumerator-list. The enumerators all have the indicated
9083 TYPE.
9084
9085 enumerator-list:
9086 enumerator-definition
9087 enumerator-list , enumerator-definition */
9088
9089static void
9090cp_parser_enumerator_list (parser, type)
9091 cp_parser *parser;
9092 tree type;
9093{
9094 while (true)
9095 {
9096 cp_token *token;
9097
9098 /* Parse an enumerator-definition. */
9099 cp_parser_enumerator_definition (parser, type);
9100 /* Peek at the next token. */
9101 token = cp_lexer_peek_token (parser->lexer);
9102 /* If it's not a `,', then we've reached the end of the
9103 list. */
9104 if (token->type != CPP_COMMA)
9105 break;
9106 /* Otherwise, consume the `,' and keep going. */
9107 cp_lexer_consume_token (parser->lexer);
9108 /* If the next token is a `}', there is a trailing comma. */
9109 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9110 {
9111 if (pedantic && !in_system_header)
9112 pedwarn ("comma at end of enumerator list");
9113 break;
9114 }
9115 }
9116}
9117
9118/* Parse an enumerator-definition. The enumerator has the indicated
9119 TYPE.
9120
9121 enumerator-definition:
9122 enumerator
9123 enumerator = constant-expression
9124
9125 enumerator:
9126 identifier */
9127
9128static void
9129cp_parser_enumerator_definition (parser, type)
9130 cp_parser *parser;
9131 tree type;
9132{
9133 cp_token *token;
9134 tree identifier;
9135 tree value;
9136
9137 /* Look for the identifier. */
9138 identifier = cp_parser_identifier (parser);
9139 if (identifier == error_mark_node)
9140 return;
9141
9142 /* Peek at the next token. */
9143 token = cp_lexer_peek_token (parser->lexer);
9144 /* If it's an `=', then there's an explicit value. */
9145 if (token->type == CPP_EQ)
9146 {
9147 /* Consume the `=' token. */
9148 cp_lexer_consume_token (parser->lexer);
9149 /* Parse the value. */
9150 value = cp_parser_constant_expression (parser);
9151 }
9152 else
9153 value = NULL_TREE;
9154
9155 /* Create the enumerator. */
9156 build_enumerator (identifier, value, type);
9157}
9158
9159/* Parse a namespace-name.
9160
9161 namespace-name:
9162 original-namespace-name
9163 namespace-alias
9164
9165 Returns the NAMESPACE_DECL for the namespace. */
9166
9167static tree
9168cp_parser_namespace_name (parser)
9169 cp_parser *parser;
9170{
9171 tree identifier;
9172 tree namespace_decl;
9173
9174 /* Get the name of the namespace. */
9175 identifier = cp_parser_identifier (parser);
9176 if (identifier == error_mark_node)
9177 return error_mark_node;
9178
eea9800f
MM
9179 /* Look up the identifier in the currently active scope. Look only
9180 for namespaces, due to:
9181
9182 [basic.lookup.udir]
9183
9184 When looking up a namespace-name in a using-directive or alias
9185 definition, only namespace names are considered.
9186
9187 And:
9188
9189 [basic.lookup.qual]
9190
9191 During the lookup of a name preceding the :: scope resolution
9192 operator, object, function, and enumerator names are ignored.
9193
9194 (Note that cp_parser_class_or_namespace_name only calls this
9195 function if the token after the name is the scope resolution
9196 operator.) */
9197 namespace_decl = cp_parser_lookup_name (parser, identifier,
9198 /*check_access=*/true,
9199 /*is_type=*/false,
9200 /*is_namespace=*/true,
9201 /*check_dependency=*/true);
a723baf1
MM
9202 /* If it's not a namespace, issue an error. */
9203 if (namespace_decl == error_mark_node
9204 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9205 {
9206 cp_parser_error (parser, "expected namespace-name");
9207 namespace_decl = error_mark_node;
9208 }
9209
9210 return namespace_decl;
9211}
9212
9213/* Parse a namespace-definition.
9214
9215 namespace-definition:
9216 named-namespace-definition
9217 unnamed-namespace-definition
9218
9219 named-namespace-definition:
9220 original-namespace-definition
9221 extension-namespace-definition
9222
9223 original-namespace-definition:
9224 namespace identifier { namespace-body }
9225
9226 extension-namespace-definition:
9227 namespace original-namespace-name { namespace-body }
9228
9229 unnamed-namespace-definition:
9230 namespace { namespace-body } */
9231
9232static void
9233cp_parser_namespace_definition (parser)
9234 cp_parser *parser;
9235{
9236 tree identifier;
9237
9238 /* Look for the `namespace' keyword. */
9239 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9240
9241 /* Get the name of the namespace. We do not attempt to distinguish
9242 between an original-namespace-definition and an
9243 extension-namespace-definition at this point. The semantic
9244 analysis routines are responsible for that. */
9245 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9246 identifier = cp_parser_identifier (parser);
9247 else
9248 identifier = NULL_TREE;
9249
9250 /* Look for the `{' to start the namespace. */
9251 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9252 /* Start the namespace. */
9253 push_namespace (identifier);
9254 /* Parse the body of the namespace. */
9255 cp_parser_namespace_body (parser);
9256 /* Finish the namespace. */
9257 pop_namespace ();
9258 /* Look for the final `}'. */
9259 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9260}
9261
9262/* Parse a namespace-body.
9263
9264 namespace-body:
9265 declaration-seq [opt] */
9266
9267static void
9268cp_parser_namespace_body (parser)
9269 cp_parser *parser;
9270{
9271 cp_parser_declaration_seq_opt (parser);
9272}
9273
9274/* Parse a namespace-alias-definition.
9275
9276 namespace-alias-definition:
9277 namespace identifier = qualified-namespace-specifier ; */
9278
9279static void
9280cp_parser_namespace_alias_definition (parser)
9281 cp_parser *parser;
9282{
9283 tree identifier;
9284 tree namespace_specifier;
9285
9286 /* Look for the `namespace' keyword. */
9287 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9288 /* Look for the identifier. */
9289 identifier = cp_parser_identifier (parser);
9290 if (identifier == error_mark_node)
9291 return;
9292 /* Look for the `=' token. */
9293 cp_parser_require (parser, CPP_EQ, "`='");
9294 /* Look for the qualified-namespace-specifier. */
9295 namespace_specifier
9296 = cp_parser_qualified_namespace_specifier (parser);
9297 /* Look for the `;' token. */
9298 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9299
9300 /* Register the alias in the symbol table. */
9301 do_namespace_alias (identifier, namespace_specifier);
9302}
9303
9304/* Parse a qualified-namespace-specifier.
9305
9306 qualified-namespace-specifier:
9307 :: [opt] nested-name-specifier [opt] namespace-name
9308
9309 Returns a NAMESPACE_DECL corresponding to the specified
9310 namespace. */
9311
9312static tree
9313cp_parser_qualified_namespace_specifier (parser)
9314 cp_parser *parser;
9315{
9316 /* Look for the optional `::'. */
9317 cp_parser_global_scope_opt (parser,
9318 /*current_scope_valid_p=*/false);
9319
9320 /* Look for the optional nested-name-specifier. */
9321 cp_parser_nested_name_specifier_opt (parser,
9322 /*typename_keyword_p=*/false,
9323 /*check_dependency_p=*/true,
9324 /*type_p=*/false);
9325
9326 return cp_parser_namespace_name (parser);
9327}
9328
9329/* Parse a using-declaration.
9330
9331 using-declaration:
9332 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9333 using :: unqualified-id ; */
9334
9335static void
9336cp_parser_using_declaration (parser)
9337 cp_parser *parser;
9338{
9339 cp_token *token;
9340 bool typename_p = false;
9341 bool global_scope_p;
9342 tree decl;
9343 tree identifier;
9344 tree scope;
9345
9346 /* Look for the `using' keyword. */
9347 cp_parser_require_keyword (parser, RID_USING, "`using'");
9348
9349 /* Peek at the next token. */
9350 token = cp_lexer_peek_token (parser->lexer);
9351 /* See if it's `typename'. */
9352 if (token->keyword == RID_TYPENAME)
9353 {
9354 /* Remember that we've seen it. */
9355 typename_p = true;
9356 /* Consume the `typename' token. */
9357 cp_lexer_consume_token (parser->lexer);
9358 }
9359
9360 /* Look for the optional global scope qualification. */
9361 global_scope_p
9362 = (cp_parser_global_scope_opt (parser,
9363 /*current_scope_valid_p=*/false)
9364 != NULL_TREE);
9365
9366 /* If we saw `typename', or didn't see `::', then there must be a
9367 nested-name-specifier present. */
9368 if (typename_p || !global_scope_p)
9369 cp_parser_nested_name_specifier (parser, typename_p,
9370 /*check_dependency_p=*/true,
9371 /*type_p=*/false);
9372 /* Otherwise, we could be in either of the two productions. In that
9373 case, treat the nested-name-specifier as optional. */
9374 else
9375 cp_parser_nested_name_specifier_opt (parser,
9376 /*typename_keyword_p=*/false,
9377 /*check_dependency_p=*/true,
9378 /*type_p=*/false);
9379
9380 /* Parse the unqualified-id. */
9381 identifier = cp_parser_unqualified_id (parser,
9382 /*template_keyword_p=*/false,
9383 /*check_dependency_p=*/true);
9384
9385 /* The function we call to handle a using-declaration is different
9386 depending on what scope we are in. */
9387 scope = current_scope ();
9388 if (scope && TYPE_P (scope))
9389 {
9390 /* Create the USING_DECL. */
9391 decl = do_class_using_decl (build_nt (SCOPE_REF,
9392 parser->scope,
9393 identifier));
9394 /* Add it to the list of members in this class. */
9395 finish_member_declaration (decl);
9396 }
9397 else
9398 {
9399 decl = cp_parser_lookup_name_simple (parser, identifier);
9400 if (scope)
9401 do_local_using_decl (decl);
9402 else
9403 do_toplevel_using_decl (decl);
9404 }
9405
9406 /* Look for the final `;'. */
9407 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9408}
9409
9410/* Parse a using-directive.
9411
9412 using-directive:
9413 using namespace :: [opt] nested-name-specifier [opt]
9414 namespace-name ; */
9415
9416static void
9417cp_parser_using_directive (parser)
9418 cp_parser *parser;
9419{
9420 tree namespace_decl;
9421
9422 /* Look for the `using' keyword. */
9423 cp_parser_require_keyword (parser, RID_USING, "`using'");
9424 /* And the `namespace' keyword. */
9425 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9426 /* Look for the optional `::' operator. */
9427 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9428 /* And the optional nested-name-sepcifier. */
9429 cp_parser_nested_name_specifier_opt (parser,
9430 /*typename_keyword_p=*/false,
9431 /*check_dependency_p=*/true,
9432 /*type_p=*/false);
9433 /* Get the namespace being used. */
9434 namespace_decl = cp_parser_namespace_name (parser);
9435 /* Update the symbol table. */
9436 do_using_directive (namespace_decl);
9437 /* Look for the final `;'. */
9438 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9439}
9440
9441/* Parse an asm-definition.
9442
9443 asm-definition:
9444 asm ( string-literal ) ;
9445
9446 GNU Extension:
9447
9448 asm-definition:
9449 asm volatile [opt] ( string-literal ) ;
9450 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9451 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9452 : asm-operand-list [opt] ) ;
9453 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9454 : asm-operand-list [opt]
9455 : asm-operand-list [opt] ) ; */
9456
9457static void
9458cp_parser_asm_definition (parser)
9459 cp_parser *parser;
9460{
9461 cp_token *token;
9462 tree string;
9463 tree outputs = NULL_TREE;
9464 tree inputs = NULL_TREE;
9465 tree clobbers = NULL_TREE;
9466 tree asm_stmt;
9467 bool volatile_p = false;
9468 bool extended_p = false;
9469
9470 /* Look for the `asm' keyword. */
9471 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9472 /* See if the next token is `volatile'. */
9473 if (cp_parser_allow_gnu_extensions_p (parser)
9474 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9475 {
9476 /* Remember that we saw the `volatile' keyword. */
9477 volatile_p = true;
9478 /* Consume the token. */
9479 cp_lexer_consume_token (parser->lexer);
9480 }
9481 /* Look for the opening `('. */
9482 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9483 /* Look for the string. */
9484 token = cp_parser_require (parser, CPP_STRING, "asm body");
9485 if (!token)
9486 return;
9487 string = token->value;
9488 /* If we're allowing GNU extensions, check for the extended assembly
9489 syntax. Unfortunately, the `:' tokens need not be separated by
9490 a space in C, and so, for compatibility, we tolerate that here
9491 too. Doing that means that we have to treat the `::' operator as
9492 two `:' tokens. */
9493 if (cp_parser_allow_gnu_extensions_p (parser)
9494 && at_function_scope_p ()
9495 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9496 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9497 {
9498 bool inputs_p = false;
9499 bool clobbers_p = false;
9500
9501 /* The extended syntax was used. */
9502 extended_p = true;
9503
9504 /* Look for outputs. */
9505 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9506 {
9507 /* Consume the `:'. */
9508 cp_lexer_consume_token (parser->lexer);
9509 /* Parse the output-operands. */
9510 if (cp_lexer_next_token_is_not (parser->lexer,
9511 CPP_COLON)
9512 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9513 CPP_SCOPE)
9514 && cp_lexer_next_token_is_not (parser->lexer,
9515 CPP_CLOSE_PAREN))
a723baf1
MM
9516 outputs = cp_parser_asm_operand_list (parser);
9517 }
9518 /* If the next token is `::', there are no outputs, and the
9519 next token is the beginning of the inputs. */
9520 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9521 {
9522 /* Consume the `::' token. */
9523 cp_lexer_consume_token (parser->lexer);
9524 /* The inputs are coming next. */
9525 inputs_p = true;
9526 }
9527
9528 /* Look for inputs. */
9529 if (inputs_p
9530 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9531 {
9532 if (!inputs_p)
9533 /* Consume the `:'. */
9534 cp_lexer_consume_token (parser->lexer);
9535 /* Parse the output-operands. */
9536 if (cp_lexer_next_token_is_not (parser->lexer,
9537 CPP_COLON)
9538 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9539 CPP_SCOPE)
9540 && cp_lexer_next_token_is_not (parser->lexer,
9541 CPP_CLOSE_PAREN))
a723baf1
MM
9542 inputs = cp_parser_asm_operand_list (parser);
9543 }
9544 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9545 /* The clobbers are coming next. */
9546 clobbers_p = true;
9547
9548 /* Look for clobbers. */
9549 if (clobbers_p
9550 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9551 {
9552 if (!clobbers_p)
9553 /* Consume the `:'. */
9554 cp_lexer_consume_token (parser->lexer);
9555 /* Parse the clobbers. */
8caf4c38
MM
9556 if (cp_lexer_next_token_is_not (parser->lexer,
9557 CPP_CLOSE_PAREN))
9558 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9559 }
9560 }
9561 /* Look for the closing `)'. */
9562 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9563 cp_parser_skip_to_closing_parenthesis (parser);
9564 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9565
9566 /* Create the ASM_STMT. */
9567 if (at_function_scope_p ())
9568 {
9569 asm_stmt =
9570 finish_asm_stmt (volatile_p
9571 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9572 string, outputs, inputs, clobbers);
9573 /* If the extended syntax was not used, mark the ASM_STMT. */
9574 if (!extended_p)
9575 ASM_INPUT_P (asm_stmt) = 1;
9576 }
9577 else
9578 assemble_asm (string);
9579}
9580
9581/* Declarators [gram.dcl.decl] */
9582
9583/* Parse an init-declarator.
9584
9585 init-declarator:
9586 declarator initializer [opt]
9587
9588 GNU Extension:
9589
9590 init-declarator:
9591 declarator asm-specification [opt] attributes [opt] initializer [opt]
9592
9593 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
cf22909c
KL
9594 Returns a reprsentation of the entity declared. If MEMBER_P is TRUE,
9595 then this declarator appears in a class scope. The new DECL created
9596 by this declarator is returned.
a723baf1
MM
9597
9598 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9599 for a function-definition here as well. If the declarator is a
9600 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9601 be TRUE upon return. By that point, the function-definition will
9602 have been completely parsed.
9603
9604 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9605 is FALSE. */
9606
9607static tree
9608cp_parser_init_declarator (parser,
9609 decl_specifiers,
9610 prefix_attributes,
a723baf1
MM
9611 function_definition_allowed_p,
9612 member_p,
9613 function_definition_p)
9614 cp_parser *parser;
9615 tree decl_specifiers;
9616 tree prefix_attributes;
a723baf1
MM
9617 bool function_definition_allowed_p;
9618 bool member_p;
9619 bool *function_definition_p;
9620{
9621 cp_token *token;
9622 tree declarator;
9623 tree attributes;
9624 tree asm_specification;
9625 tree initializer;
9626 tree decl = NULL_TREE;
9627 tree scope;
a723baf1
MM
9628 bool is_initialized;
9629 bool is_parenthesized_init;
9630 bool ctor_dtor_or_conv_p;
9631 bool friend_p;
9632
9633 /* Assume that this is not the declarator for a function
9634 definition. */
9635 if (function_definition_p)
9636 *function_definition_p = false;
9637
9638 /* Defer access checks while parsing the declarator; we cannot know
9639 what names are accessible until we know what is being
9640 declared. */
cf22909c
KL
9641 resume_deferring_access_checks ();
9642
a723baf1
MM
9643 /* Parse the declarator. */
9644 declarator
62b8a44e 9645 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
9646 &ctor_dtor_or_conv_p);
9647 /* Gather up the deferred checks. */
cf22909c 9648 stop_deferring_access_checks ();
24c0ef37 9649
a723baf1
MM
9650 /* If the DECLARATOR was erroneous, there's no need to go
9651 further. */
9652 if (declarator == error_mark_node)
cf22909c 9653 return error_mark_node;
a723baf1
MM
9654
9655 /* Figure out what scope the entity declared by the DECLARATOR is
9656 located in. `grokdeclarator' sometimes changes the scope, so
9657 we compute it now. */
9658 scope = get_scope_of_declarator (declarator);
9659
9660 /* If we're allowing GNU extensions, look for an asm-specification
9661 and attributes. */
9662 if (cp_parser_allow_gnu_extensions_p (parser))
9663 {
9664 /* Look for an asm-specification. */
9665 asm_specification = cp_parser_asm_specification_opt (parser);
9666 /* And attributes. */
9667 attributes = cp_parser_attributes_opt (parser);
9668 }
9669 else
9670 {
9671 asm_specification = NULL_TREE;
9672 attributes = NULL_TREE;
9673 }
9674
9675 /* Peek at the next token. */
9676 token = cp_lexer_peek_token (parser->lexer);
9677 /* Check to see if the token indicates the start of a
9678 function-definition. */
9679 if (cp_parser_token_starts_function_definition_p (token))
9680 {
9681 if (!function_definition_allowed_p)
9682 {
9683 /* If a function-definition should not appear here, issue an
9684 error message. */
9685 cp_parser_error (parser,
9686 "a function-definition is not allowed here");
9687 return error_mark_node;
9688 }
9689 else
9690 {
a723baf1
MM
9691 /* Neither attributes nor an asm-specification are allowed
9692 on a function-definition. */
9693 if (asm_specification)
9694 error ("an asm-specification is not allowed on a function-definition");
9695 if (attributes)
9696 error ("attributes are not allowed on a function-definition");
9697 /* This is a function-definition. */
9698 *function_definition_p = true;
9699
a723baf1
MM
9700 /* Parse the function definition. */
9701 decl = (cp_parser_function_definition_from_specifiers_and_declarator
cf22909c 9702 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 9703
a723baf1
MM
9704 return decl;
9705 }
9706 }
9707
9708 /* [dcl.dcl]
9709
9710 Only in function declarations for constructors, destructors, and
9711 type conversions can the decl-specifier-seq be omitted.
9712
9713 We explicitly postpone this check past the point where we handle
9714 function-definitions because we tolerate function-definitions
9715 that are missing their return types in some modes. */
9716 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9717 {
9718 cp_parser_error (parser,
9719 "expected constructor, destructor, or type conversion");
9720 return error_mark_node;
9721 }
9722
9723 /* An `=' or an `(' indicates an initializer. */
9724 is_initialized = (token->type == CPP_EQ
9725 || token->type == CPP_OPEN_PAREN);
9726 /* If the init-declarator isn't initialized and isn't followed by a
9727 `,' or `;', it's not a valid init-declarator. */
9728 if (!is_initialized
9729 && token->type != CPP_COMMA
9730 && token->type != CPP_SEMICOLON)
9731 {
9732 cp_parser_error (parser, "expected init-declarator");
9733 return error_mark_node;
9734 }
9735
9736 /* Because start_decl has side-effects, we should only call it if we
9737 know we're going ahead. By this point, we know that we cannot
9738 possibly be looking at any other construct. */
9739 cp_parser_commit_to_tentative_parse (parser);
9740
9741 /* Check to see whether or not this declaration is a friend. */
9742 friend_p = cp_parser_friend_p (decl_specifiers);
9743
9744 /* Check that the number of template-parameter-lists is OK. */
9745 if (!cp_parser_check_declarator_template_parameters (parser,
9746 declarator))
cf22909c 9747 return error_mark_node;
a723baf1
MM
9748
9749 /* Enter the newly declared entry in the symbol table. If we're
9750 processing a declaration in a class-specifier, we wait until
9751 after processing the initializer. */
9752 if (!member_p)
9753 {
9754 if (parser->in_unbraced_linkage_specification_p)
9755 {
9756 decl_specifiers = tree_cons (error_mark_node,
9757 get_identifier ("extern"),
9758 decl_specifiers);
9759 have_extern_spec = false;
9760 }
9761 decl = start_decl (declarator,
9762 decl_specifiers,
9763 is_initialized,
9764 attributes,
9765 prefix_attributes);
9766 }
9767
9768 /* Enter the SCOPE. That way unqualified names appearing in the
9769 initializer will be looked up in SCOPE. */
9770 if (scope)
9771 push_scope (scope);
9772
9773 /* Perform deferred access control checks, now that we know in which
9774 SCOPE the declared entity resides. */
9775 if (!member_p && decl)
9776 {
9777 tree saved_current_function_decl = NULL_TREE;
9778
9779 /* If the entity being declared is a function, pretend that we
9780 are in its scope. If it is a `friend', it may have access to
9781 things that would not otherwise be accessible. */
9782 if (TREE_CODE (decl) == FUNCTION_DECL)
9783 {
9784 saved_current_function_decl = current_function_decl;
9785 current_function_decl = decl;
9786 }
9787
cf22909c
KL
9788 /* Perform the access control checks for the declarator and the
9789 the decl-specifiers. */
9790 perform_deferred_access_checks ();
a723baf1
MM
9791
9792 /* Restore the saved value. */
9793 if (TREE_CODE (decl) == FUNCTION_DECL)
9794 current_function_decl = saved_current_function_decl;
9795 }
9796
9797 /* Parse the initializer. */
9798 if (is_initialized)
9799 initializer = cp_parser_initializer (parser,
9800 &is_parenthesized_init);
9801 else
9802 {
9803 initializer = NULL_TREE;
9804 is_parenthesized_init = false;
9805 }
9806
9807 /* The old parser allows attributes to appear after a parenthesized
9808 initializer. Mark Mitchell proposed removing this functionality
9809 on the GCC mailing lists on 2002-08-13. This parser accepts the
9810 attributes -- but ignores them. */
9811 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9812 if (cp_parser_attributes_opt (parser))
9813 warning ("attributes after parenthesized initializer ignored");
9814
9815 /* Leave the SCOPE, now that we have processed the initializer. It
9816 is important to do this before calling cp_finish_decl because it
9817 makes decisions about whether to create DECL_STMTs or not based
9818 on the current scope. */
9819 if (scope)
9820 pop_scope (scope);
9821
9822 /* For an in-class declaration, use `grokfield' to create the
9823 declaration. */
9824 if (member_p)
9825 decl = grokfield (declarator, decl_specifiers,
9826 initializer, /*asmspec=*/NULL_TREE,
9827 /*attributes=*/NULL_TREE);
9828
9829 /* Finish processing the declaration. But, skip friend
9830 declarations. */
9831 if (!friend_p && decl)
9832 cp_finish_decl (decl,
9833 initializer,
9834 asm_specification,
9835 /* If the initializer is in parentheses, then this is
9836 a direct-initialization, which means that an
9837 `explicit' constructor is OK. Otherwise, an
9838 `explicit' constructor cannot be used. */
9839 ((is_parenthesized_init || !is_initialized)
9840 ? 0 : LOOKUP_ONLYCONVERTING));
9841
9842 return decl;
9843}
9844
9845/* Parse a declarator.
9846
9847 declarator:
9848 direct-declarator
9849 ptr-operator declarator
9850
9851 abstract-declarator:
9852 ptr-operator abstract-declarator [opt]
9853 direct-abstract-declarator
9854
9855 GNU Extensions:
9856
9857 declarator:
9858 attributes [opt] direct-declarator
9859 attributes [opt] ptr-operator declarator
9860
9861 abstract-declarator:
9862 attributes [opt] ptr-operator abstract-declarator [opt]
9863 attributes [opt] direct-abstract-declarator
9864
9865 Returns a representation of the declarator. If the declarator has
9866 the form `* declarator', then an INDIRECT_REF is returned, whose
9867 only operand is the sub-declarator. Analagously, `& declarator' is
9868 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9869 used. The first operand is the TYPE for `X'. The second operand
9870 is an INDIRECT_REF whose operand is the sub-declarator.
9871
9872 Otherwise, the reprsentation is as for a direct-declarator.
9873
9874 (It would be better to define a structure type to represent
9875 declarators, rather than abusing `tree' nodes to represent
9876 declarators. That would be much clearer and save some memory.
9877 There is no reason for declarators to be garbage-collected, for
9878 example; they are created during parser and no longer needed after
9879 `grokdeclarator' has been called.)
9880
9881 For a ptr-operator that has the optional cv-qualifier-seq,
9882 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9883 node.
9884
9885 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9886 true if this declarator represents a constructor, destructor, or
9887 type conversion operator. Otherwise, it is set to false.
9888
9889 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9890 a decl-specifier-seq unless it declares a constructor, destructor,
9891 or conversion. It might seem that we could check this condition in
9892 semantic analysis, rather than parsing, but that makes it difficult
9893 to handle something like `f()'. We want to notice that there are
9894 no decl-specifiers, and therefore realize that this is an
9895 expression, not a declaration.) */
9896
9897static tree
62b8a44e 9898cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
a723baf1 9899 cp_parser *parser;
62b8a44e 9900 cp_parser_declarator_kind dcl_kind;
a723baf1
MM
9901 bool *ctor_dtor_or_conv_p;
9902{
9903 cp_token *token;
9904 tree declarator;
9905 enum tree_code code;
9906 tree cv_qualifier_seq;
9907 tree class_type;
9908 tree attributes = NULL_TREE;
9909
9910 /* Assume this is not a constructor, destructor, or type-conversion
9911 operator. */
9912 if (ctor_dtor_or_conv_p)
9913 *ctor_dtor_or_conv_p = false;
9914
9915 if (cp_parser_allow_gnu_extensions_p (parser))
9916 attributes = cp_parser_attributes_opt (parser);
9917
9918 /* Peek at the next token. */
9919 token = cp_lexer_peek_token (parser->lexer);
9920
9921 /* Check for the ptr-operator production. */
9922 cp_parser_parse_tentatively (parser);
9923 /* Parse the ptr-operator. */
9924 code = cp_parser_ptr_operator (parser,
9925 &class_type,
9926 &cv_qualifier_seq);
9927 /* If that worked, then we have a ptr-operator. */
9928 if (cp_parser_parse_definitely (parser))
9929 {
9930 /* The dependent declarator is optional if we are parsing an
9931 abstract-declarator. */
62b8a44e 9932 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
9933 cp_parser_parse_tentatively (parser);
9934
9935 /* Parse the dependent declarator. */
62b8a44e 9936 declarator = cp_parser_declarator (parser, dcl_kind,
a723baf1
MM
9937 /*ctor_dtor_or_conv_p=*/NULL);
9938
9939 /* If we are parsing an abstract-declarator, we must handle the
9940 case where the dependent declarator is absent. */
62b8a44e
NS
9941 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9942 && !cp_parser_parse_definitely (parser))
a723baf1
MM
9943 declarator = NULL_TREE;
9944
9945 /* Build the representation of the ptr-operator. */
9946 if (code == INDIRECT_REF)
9947 declarator = make_pointer_declarator (cv_qualifier_seq,
9948 declarator);
9949 else
9950 declarator = make_reference_declarator (cv_qualifier_seq,
9951 declarator);
9952 /* Handle the pointer-to-member case. */
9953 if (class_type)
9954 declarator = build_nt (SCOPE_REF, class_type, declarator);
9955 }
9956 /* Everything else is a direct-declarator. */
9957 else
9958 declarator = cp_parser_direct_declarator (parser,
62b8a44e 9959 dcl_kind,
a723baf1
MM
9960 ctor_dtor_or_conv_p);
9961
9962 if (attributes && declarator != error_mark_node)
9963 declarator = tree_cons (attributes, declarator, NULL_TREE);
9964
9965 return declarator;
9966}
9967
9968/* Parse a direct-declarator or direct-abstract-declarator.
9969
9970 direct-declarator:
9971 declarator-id
9972 direct-declarator ( parameter-declaration-clause )
9973 cv-qualifier-seq [opt]
9974 exception-specification [opt]
9975 direct-declarator [ constant-expression [opt] ]
9976 ( declarator )
9977
9978 direct-abstract-declarator:
9979 direct-abstract-declarator [opt]
9980 ( parameter-declaration-clause )
9981 cv-qualifier-seq [opt]
9982 exception-specification [opt]
9983 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9984 ( abstract-declarator )
9985
62b8a44e
NS
9986 Returns a representation of the declarator. DCL_KIND is
9987 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9988 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9989 we are parsing a direct-declarator. It is
9990 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9991 of ambiguity we prefer an abstract declarator, as per
9992 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
9993 cp_parser_declarator.
9994
9995 For the declarator-id production, the representation is as for an
9996 id-expression, except that a qualified name is represented as a
9997 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9998 see the documentation of the FUNCTION_DECLARATOR_* macros for
9999 information about how to find the various declarator components.
10000 An array-declarator is represented as an ARRAY_REF. The
10001 direct-declarator is the first operand; the constant-expression
10002 indicating the size of the array is the second operand. */
10003
10004static tree
62b8a44e 10005cp_parser_direct_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
a723baf1 10006 cp_parser *parser;
62b8a44e 10007 cp_parser_declarator_kind dcl_kind;
a723baf1
MM
10008 bool *ctor_dtor_or_conv_p;
10009{
10010 cp_token *token;
62b8a44e 10011 tree declarator = NULL_TREE;
a723baf1
MM
10012 tree scope = NULL_TREE;
10013 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10014 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e
NS
10015 bool first = true;
10016
10017 while (true)
a723baf1 10018 {
62b8a44e
NS
10019 /* Peek at the next token. */
10020 token = cp_lexer_peek_token (parser->lexer);
10021 if (token->type == CPP_OPEN_PAREN)
a723baf1 10022 {
62b8a44e
NS
10023 /* This is either a parameter-declaration-clause, or a
10024 parenthesized declarator. When we know we are parsing a
2050a1bb 10025 named declarator, it must be a paranthesized declarator
62b8a44e
NS
10026 if FIRST is true. For instance, `(int)' is a
10027 parameter-declaration-clause, with an omitted
10028 direct-abstract-declarator. But `((*))', is a
10029 parenthesized abstract declarator. Finally, when T is a
10030 template parameter `(T)' is a
10031 paremeter-declaration-clause, and not a parenthesized
10032 named declarator.
a723baf1 10033
62b8a44e
NS
10034 We first try and parse a parameter-declaration-clause,
10035 and then try a nested declarator (if FIRST is true).
a723baf1 10036
62b8a44e
NS
10037 It is not an error for it not to be a
10038 parameter-declaration-clause, even when FIRST is
10039 false. Consider,
10040
10041 int i (int);
10042 int i (3);
10043
10044 The first is the declaration of a function while the
10045 second is a the definition of a variable, including its
10046 initializer.
10047
10048 Having seen only the parenthesis, we cannot know which of
10049 these two alternatives should be selected. Even more
10050 complex are examples like:
10051
10052 int i (int (a));
10053 int i (int (3));
10054
10055 The former is a function-declaration; the latter is a
10056 variable initialization.
10057
10058 Thus again, we try a parameter-declation-clause, and if
10059 that fails, we back out and return. */
10060
10061 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10062 {
62b8a44e
NS
10063 tree params;
10064
10065 cp_parser_parse_tentatively (parser);
a723baf1 10066
62b8a44e
NS
10067 /* Consume the `('. */
10068 cp_lexer_consume_token (parser->lexer);
10069 if (first)
10070 {
10071 /* If this is going to be an abstract declarator, we're
10072 in a declarator and we can't have default args. */
10073 parser->default_arg_ok_p = false;
10074 parser->in_declarator_p = true;
10075 }
10076
10077 /* Parse the parameter-declaration-clause. */
10078 params = cp_parser_parameter_declaration_clause (parser);
10079
10080 /* If all went well, parse the cv-qualifier-seq and the
10081 exception-specfication. */
10082 if (cp_parser_parse_definitely (parser))
10083 {
10084 tree cv_qualifiers;
10085 tree exception_specification;
10086
10087 first = false;
10088 /* Consume the `)'. */
10089 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10090
10091 /* Parse the cv-qualifier-seq. */
10092 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10093 /* And the exception-specification. */
10094 exception_specification
10095 = cp_parser_exception_specification_opt (parser);
10096
10097 /* Create the function-declarator. */
10098 declarator = make_call_declarator (declarator,
10099 params,
10100 cv_qualifiers,
10101 exception_specification);
10102 /* Any subsequent parameter lists are to do with
10103 return type, so are not those of the declared
10104 function. */
10105 parser->default_arg_ok_p = false;
10106
10107 /* Repeat the main loop. */
10108 continue;
10109 }
10110 }
10111
10112 /* If this is the first, we can try a parenthesized
10113 declarator. */
10114 if (first)
a723baf1 10115 {
a723baf1 10116 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e
NS
10117 parser->in_declarator_p = saved_in_declarator_p;
10118
10119 /* Consume the `('. */
10120 cp_lexer_consume_token (parser->lexer);
10121 /* Parse the nested declarator. */
10122 declarator
10123 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10124 first = false;
10125 /* Expect a `)'. */
10126 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10127 declarator = error_mark_node;
10128 if (declarator == error_mark_node)
10129 break;
10130
10131 goto handle_declarator;
a723baf1 10132 }
62b8a44e
NS
10133 /* Otherwise, we must be done. */
10134 else
10135 break;
a723baf1 10136 }
62b8a44e
NS
10137 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10138 && token->type == CPP_OPEN_SQUARE)
a723baf1 10139 {
62b8a44e 10140 /* Parse an array-declarator. */
a723baf1
MM
10141 tree bounds;
10142
62b8a44e
NS
10143 first = false;
10144 parser->default_arg_ok_p = false;
10145 parser->in_declarator_p = true;
a723baf1
MM
10146 /* Consume the `['. */
10147 cp_lexer_consume_token (parser->lexer);
10148 /* Peek at the next token. */
10149 token = cp_lexer_peek_token (parser->lexer);
10150 /* If the next token is `]', then there is no
10151 constant-expression. */
10152 if (token->type != CPP_CLOSE_SQUARE)
10153 bounds = cp_parser_constant_expression (parser);
10154 else
10155 bounds = NULL_TREE;
10156 /* Look for the closing `]'. */
62b8a44e
NS
10157 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10158 {
10159 declarator = error_mark_node;
10160 break;
10161 }
a723baf1
MM
10162
10163 declarator = build_nt (ARRAY_REF, declarator, bounds);
10164 }
62b8a44e 10165 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 10166 {
62b8a44e
NS
10167 /* Parse a declarator_id */
10168 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10169 cp_parser_parse_tentatively (parser);
10170 declarator = cp_parser_declarator_id (parser);
10171 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER
10172 && !cp_parser_parse_definitely (parser))
10173 declarator = error_mark_node;
10174 if (declarator == error_mark_node)
10175 break;
a723baf1 10176
62b8a44e
NS
10177 if (TREE_CODE (declarator) == SCOPE_REF)
10178 {
10179 tree scope = TREE_OPERAND (declarator, 0);
a723baf1 10180
62b8a44e
NS
10181 /* In the declaration of a member of a template class
10182 outside of the class itself, the SCOPE will sometimes
10183 be a TYPENAME_TYPE. For example, given:
10184
10185 template <typename T>
10186 int S<T>::R::i = 3;
10187
10188 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10189 this context, we must resolve S<T>::R to an ordinary
10190 type, rather than a typename type.
10191
10192 The reason we normally avoid resolving TYPENAME_TYPEs
10193 is that a specialization of `S' might render
10194 `S<T>::R' not a type. However, if `S' is
10195 specialized, then this `i' will not be used, so there
10196 is no harm in resolving the types here. */
10197 if (TREE_CODE (scope) == TYPENAME_TYPE)
10198 {
10199 /* Resolve the TYPENAME_TYPE. */
10200 scope = cp_parser_resolve_typename_type (parser, scope);
10201 /* If that failed, the declarator is invalid. */
10202 if (scope == error_mark_node)
10203 return error_mark_node;
10204 /* Build a new DECLARATOR. */
10205 declarator = build_nt (SCOPE_REF,
10206 scope,
10207 TREE_OPERAND (declarator, 1));
10208 }
10209 }
10210
10211 /* Check to see whether the declarator-id names a constructor,
10212 destructor, or conversion. */
10213 if (declarator && ctor_dtor_or_conv_p
10214 && ((TREE_CODE (declarator) == SCOPE_REF
10215 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10216 || (TREE_CODE (declarator) != SCOPE_REF
10217 && at_class_scope_p ())))
a723baf1 10218 {
62b8a44e
NS
10219 tree unqualified_name;
10220 tree class_type;
10221
10222 /* Get the unqualified part of the name. */
10223 if (TREE_CODE (declarator) == SCOPE_REF)
10224 {
10225 class_type = TREE_OPERAND (declarator, 0);
10226 unqualified_name = TREE_OPERAND (declarator, 1);
10227 }
10228 else
10229 {
10230 class_type = current_class_type;
10231 unqualified_name = declarator;
10232 }
10233
10234 /* See if it names ctor, dtor or conv. */
10235 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10236 || IDENTIFIER_TYPENAME_P (unqualified_name)
10237 || constructor_name_p (unqualified_name, class_type))
10238 *ctor_dtor_or_conv_p = true;
a723baf1 10239 }
62b8a44e
NS
10240
10241 handle_declarator:;
10242 scope = get_scope_of_declarator (declarator);
10243 if (scope)
10244 /* Any names that appear after the declarator-id for a member
10245 are looked up in the containing scope. */
10246 push_scope (scope);
10247 parser->in_declarator_p = true;
10248 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10249 || (declarator
10250 && (TREE_CODE (declarator) == SCOPE_REF
10251 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10252 /* Default args are only allowed on function
10253 declarations. */
10254 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 10255 else
62b8a44e
NS
10256 parser->default_arg_ok_p = false;
10257
10258 first = false;
a723baf1 10259 }
62b8a44e 10260 /* We're done. */
a723baf1
MM
10261 else
10262 break;
a723baf1
MM
10263 }
10264
10265 /* For an abstract declarator, we might wind up with nothing at this
10266 point. That's an error; the declarator is not optional. */
10267 if (!declarator)
10268 cp_parser_error (parser, "expected declarator");
10269
10270 /* If we entered a scope, we must exit it now. */
10271 if (scope)
10272 pop_scope (scope);
10273
10274 parser->default_arg_ok_p = saved_default_arg_ok_p;
10275 parser->in_declarator_p = saved_in_declarator_p;
10276
10277 return declarator;
10278}
10279
10280/* Parse a ptr-operator.
10281
10282 ptr-operator:
10283 * cv-qualifier-seq [opt]
10284 &
10285 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10286
10287 GNU Extension:
10288
10289 ptr-operator:
10290 & cv-qualifier-seq [opt]
10291
10292 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10293 used. Returns ADDR_EXPR if a reference was used. In the
10294 case of a pointer-to-member, *TYPE is filled in with the
10295 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10296 with the cv-qualifier-seq, or NULL_TREE, if there are no
10297 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10298
10299static enum tree_code
10300cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10301 cp_parser *parser;
10302 tree *type;
10303 tree *cv_qualifier_seq;
10304{
10305 enum tree_code code = ERROR_MARK;
10306 cp_token *token;
10307
10308 /* Assume that it's not a pointer-to-member. */
10309 *type = NULL_TREE;
10310 /* And that there are no cv-qualifiers. */
10311 *cv_qualifier_seq = NULL_TREE;
10312
10313 /* Peek at the next token. */
10314 token = cp_lexer_peek_token (parser->lexer);
10315 /* If it's a `*' or `&' we have a pointer or reference. */
10316 if (token->type == CPP_MULT || token->type == CPP_AND)
10317 {
10318 /* Remember which ptr-operator we were processing. */
10319 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10320
10321 /* Consume the `*' or `&'. */
10322 cp_lexer_consume_token (parser->lexer);
10323
10324 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10325 `&', if we are allowing GNU extensions. (The only qualifier
10326 that can legally appear after `&' is `restrict', but that is
10327 enforced during semantic analysis. */
10328 if (code == INDIRECT_REF
10329 || cp_parser_allow_gnu_extensions_p (parser))
10330 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10331 }
10332 else
10333 {
10334 /* Try the pointer-to-member case. */
10335 cp_parser_parse_tentatively (parser);
10336 /* Look for the optional `::' operator. */
10337 cp_parser_global_scope_opt (parser,
10338 /*current_scope_valid_p=*/false);
10339 /* Look for the nested-name specifier. */
10340 cp_parser_nested_name_specifier (parser,
10341 /*typename_keyword_p=*/false,
10342 /*check_dependency_p=*/true,
10343 /*type_p=*/false);
10344 /* If we found it, and the next token is a `*', then we are
10345 indeed looking at a pointer-to-member operator. */
10346 if (!cp_parser_error_occurred (parser)
10347 && cp_parser_require (parser, CPP_MULT, "`*'"))
10348 {
10349 /* The type of which the member is a member is given by the
10350 current SCOPE. */
10351 *type = parser->scope;
10352 /* The next name will not be qualified. */
10353 parser->scope = NULL_TREE;
10354 parser->qualifying_scope = NULL_TREE;
10355 parser->object_scope = NULL_TREE;
10356 /* Indicate that the `*' operator was used. */
10357 code = INDIRECT_REF;
10358 /* Look for the optional cv-qualifier-seq. */
10359 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10360 }
10361 /* If that didn't work we don't have a ptr-operator. */
10362 if (!cp_parser_parse_definitely (parser))
10363 cp_parser_error (parser, "expected ptr-operator");
10364 }
10365
10366 return code;
10367}
10368
10369/* Parse an (optional) cv-qualifier-seq.
10370
10371 cv-qualifier-seq:
10372 cv-qualifier cv-qualifier-seq [opt]
10373
10374 Returns a TREE_LIST. The TREE_VALUE of each node is the
10375 representation of a cv-qualifier. */
10376
10377static tree
10378cp_parser_cv_qualifier_seq_opt (parser)
10379 cp_parser *parser;
10380{
10381 tree cv_qualifiers = NULL_TREE;
10382
10383 while (true)
10384 {
10385 tree cv_qualifier;
10386
10387 /* Look for the next cv-qualifier. */
10388 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10389 /* If we didn't find one, we're done. */
10390 if (!cv_qualifier)
10391 break;
10392
10393 /* Add this cv-qualifier to the list. */
10394 cv_qualifiers
10395 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10396 }
10397
10398 /* We built up the list in reverse order. */
10399 return nreverse (cv_qualifiers);
10400}
10401
10402/* Parse an (optional) cv-qualifier.
10403
10404 cv-qualifier:
10405 const
10406 volatile
10407
10408 GNU Extension:
10409
10410 cv-qualifier:
10411 __restrict__ */
10412
10413static tree
10414cp_parser_cv_qualifier_opt (parser)
10415 cp_parser *parser;
10416{
10417 cp_token *token;
10418 tree cv_qualifier = NULL_TREE;
10419
10420 /* Peek at the next token. */
10421 token = cp_lexer_peek_token (parser->lexer);
10422 /* See if it's a cv-qualifier. */
10423 switch (token->keyword)
10424 {
10425 case RID_CONST:
10426 case RID_VOLATILE:
10427 case RID_RESTRICT:
10428 /* Save the value of the token. */
10429 cv_qualifier = token->value;
10430 /* Consume the token. */
10431 cp_lexer_consume_token (parser->lexer);
10432 break;
10433
10434 default:
10435 break;
10436 }
10437
10438 return cv_qualifier;
10439}
10440
10441/* Parse a declarator-id.
10442
10443 declarator-id:
10444 id-expression
10445 :: [opt] nested-name-specifier [opt] type-name
10446
10447 In the `id-expression' case, the value returned is as for
10448 cp_parser_id_expression if the id-expression was an unqualified-id.
10449 If the id-expression was a qualified-id, then a SCOPE_REF is
10450 returned. The first operand is the scope (either a NAMESPACE_DECL
10451 or TREE_TYPE), but the second is still just a representation of an
10452 unqualified-id. */
10453
10454static tree
10455cp_parser_declarator_id (parser)
10456 cp_parser *parser;
10457{
10458 tree id_expression;
10459
10460 /* The expression must be an id-expression. Assume that qualified
10461 names are the names of types so that:
10462
10463 template <class T>
10464 int S<T>::R::i = 3;
10465
10466 will work; we must treat `S<T>::R' as the name of a type.
10467 Similarly, assume that qualified names are templates, where
10468 required, so that:
10469
10470 template <class T>
10471 int S<T>::R<T>::i = 3;
10472
10473 will work, too. */
10474 id_expression = cp_parser_id_expression (parser,
10475 /*template_keyword_p=*/false,
10476 /*check_dependency_p=*/false,
10477 /*template_p=*/NULL);
10478 /* If the name was qualified, create a SCOPE_REF to represent
10479 that. */
10480 if (parser->scope)
10481 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10482
10483 return id_expression;
10484}
10485
10486/* Parse a type-id.
10487
10488 type-id:
10489 type-specifier-seq abstract-declarator [opt]
10490
10491 Returns the TYPE specified. */
10492
10493static tree
10494cp_parser_type_id (parser)
10495 cp_parser *parser;
10496{
10497 tree type_specifier_seq;
10498 tree abstract_declarator;
10499
10500 /* Parse the type-specifier-seq. */
10501 type_specifier_seq
10502 = cp_parser_type_specifier_seq (parser);
10503 if (type_specifier_seq == error_mark_node)
10504 return error_mark_node;
10505
10506 /* There might or might not be an abstract declarator. */
10507 cp_parser_parse_tentatively (parser);
10508 /* Look for the declarator. */
10509 abstract_declarator
62b8a44e 10510 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
a723baf1
MM
10511 /* Check to see if there really was a declarator. */
10512 if (!cp_parser_parse_definitely (parser))
10513 abstract_declarator = NULL_TREE;
10514
10515 return groktypename (build_tree_list (type_specifier_seq,
10516 abstract_declarator));
10517}
10518
10519/* Parse a type-specifier-seq.
10520
10521 type-specifier-seq:
10522 type-specifier type-specifier-seq [opt]
10523
10524 GNU extension:
10525
10526 type-specifier-seq:
10527 attributes type-specifier-seq [opt]
10528
10529 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10530 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10531
10532static tree
10533cp_parser_type_specifier_seq (parser)
10534 cp_parser *parser;
10535{
10536 bool seen_type_specifier = false;
10537 tree type_specifier_seq = NULL_TREE;
10538
10539 /* Parse the type-specifiers and attributes. */
10540 while (true)
10541 {
10542 tree type_specifier;
10543
10544 /* Check for attributes first. */
10545 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10546 {
10547 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10548 NULL_TREE,
10549 type_specifier_seq);
10550 continue;
10551 }
10552
10553 /* After the first type-specifier, others are optional. */
10554 if (seen_type_specifier)
10555 cp_parser_parse_tentatively (parser);
10556 /* Look for the type-specifier. */
10557 type_specifier = cp_parser_type_specifier (parser,
10558 CP_PARSER_FLAGS_NONE,
10559 /*is_friend=*/false,
10560 /*is_declaration=*/false,
10561 NULL,
10562 NULL);
10563 /* If the first type-specifier could not be found, this is not a
10564 type-specifier-seq at all. */
10565 if (!seen_type_specifier && type_specifier == error_mark_node)
10566 return error_mark_node;
10567 /* If subsequent type-specifiers could not be found, the
10568 type-specifier-seq is complete. */
10569 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10570 break;
10571
10572 /* Add the new type-specifier to the list. */
10573 type_specifier_seq
10574 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10575 seen_type_specifier = true;
10576 }
10577
10578 /* We built up the list in reverse order. */
10579 return nreverse (type_specifier_seq);
10580}
10581
10582/* Parse a parameter-declaration-clause.
10583
10584 parameter-declaration-clause:
10585 parameter-declaration-list [opt] ... [opt]
10586 parameter-declaration-list , ...
10587
10588 Returns a representation for the parameter declarations. Each node
10589 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10590 representation.) If the parameter-declaration-clause ends with an
10591 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10592 list. A return value of NULL_TREE indicates a
10593 parameter-declaration-clause consisting only of an ellipsis. */
10594
10595static tree
10596cp_parser_parameter_declaration_clause (parser)
10597 cp_parser *parser;
10598{
10599 tree parameters;
10600 cp_token *token;
10601 bool ellipsis_p;
10602
10603 /* Peek at the next token. */
10604 token = cp_lexer_peek_token (parser->lexer);
10605 /* Check for trivial parameter-declaration-clauses. */
10606 if (token->type == CPP_ELLIPSIS)
10607 {
10608 /* Consume the `...' token. */
10609 cp_lexer_consume_token (parser->lexer);
10610 return NULL_TREE;
10611 }
10612 else if (token->type == CPP_CLOSE_PAREN)
10613 /* There are no parameters. */
c73aecdf
DE
10614 {
10615#ifndef NO_IMPLICIT_EXTERN_C
10616 if (in_system_header && current_class_type == NULL
10617 && current_lang_name == lang_name_c)
10618 return NULL_TREE;
10619 else
10620#endif
10621 return void_list_node;
10622 }
a723baf1
MM
10623 /* Check for `(void)', too, which is a special case. */
10624 else if (token->keyword == RID_VOID
10625 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10626 == CPP_CLOSE_PAREN))
10627 {
10628 /* Consume the `void' token. */
10629 cp_lexer_consume_token (parser->lexer);
10630 /* There are no parameters. */
10631 return void_list_node;
10632 }
10633
10634 /* Parse the parameter-declaration-list. */
10635 parameters = cp_parser_parameter_declaration_list (parser);
10636 /* If a parse error occurred while parsing the
10637 parameter-declaration-list, then the entire
10638 parameter-declaration-clause is erroneous. */
10639 if (parameters == error_mark_node)
10640 return error_mark_node;
10641
10642 /* Peek at the next token. */
10643 token = cp_lexer_peek_token (parser->lexer);
10644 /* If it's a `,', the clause should terminate with an ellipsis. */
10645 if (token->type == CPP_COMMA)
10646 {
10647 /* Consume the `,'. */
10648 cp_lexer_consume_token (parser->lexer);
10649 /* Expect an ellipsis. */
10650 ellipsis_p
10651 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10652 }
10653 /* It might also be `...' if the optional trailing `,' was
10654 omitted. */
10655 else if (token->type == CPP_ELLIPSIS)
10656 {
10657 /* Consume the `...' token. */
10658 cp_lexer_consume_token (parser->lexer);
10659 /* And remember that we saw it. */
10660 ellipsis_p = true;
10661 }
10662 else
10663 ellipsis_p = false;
10664
10665 /* Finish the parameter list. */
10666 return finish_parmlist (parameters, ellipsis_p);
10667}
10668
10669/* Parse a parameter-declaration-list.
10670
10671 parameter-declaration-list:
10672 parameter-declaration
10673 parameter-declaration-list , parameter-declaration
10674
10675 Returns a representation of the parameter-declaration-list, as for
10676 cp_parser_parameter_declaration_clause. However, the
10677 `void_list_node' is never appended to the list. */
10678
10679static tree
10680cp_parser_parameter_declaration_list (parser)
10681 cp_parser *parser;
10682{
10683 tree parameters = NULL_TREE;
10684
10685 /* Look for more parameters. */
10686 while (true)
10687 {
10688 tree parameter;
10689 /* Parse the parameter. */
10690 parameter
ec194454
MM
10691 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10692
a723baf1
MM
10693 /* If a parse error ocurred parsing the parameter declaration,
10694 then the entire parameter-declaration-list is erroneous. */
10695 if (parameter == error_mark_node)
10696 {
10697 parameters = error_mark_node;
10698 break;
10699 }
10700 /* Add the new parameter to the list. */
10701 TREE_CHAIN (parameter) = parameters;
10702 parameters = parameter;
10703
10704 /* Peek at the next token. */
10705 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10706 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10707 /* The parameter-declaration-list is complete. */
10708 break;
10709 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10710 {
10711 cp_token *token;
10712
10713 /* Peek at the next token. */
10714 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10715 /* If it's an ellipsis, then the list is complete. */
10716 if (token->type == CPP_ELLIPSIS)
10717 break;
10718 /* Otherwise, there must be more parameters. Consume the
10719 `,'. */
10720 cp_lexer_consume_token (parser->lexer);
10721 }
10722 else
10723 {
10724 cp_parser_error (parser, "expected `,' or `...'");
10725 break;
10726 }
10727 }
10728
10729 /* We built up the list in reverse order; straighten it out now. */
10730 return nreverse (parameters);
10731}
10732
10733/* Parse a parameter declaration.
10734
10735 parameter-declaration:
10736 decl-specifier-seq declarator
10737 decl-specifier-seq declarator = assignment-expression
10738 decl-specifier-seq abstract-declarator [opt]
10739 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10740
ec194454
MM
10741 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10742 declares a template parameter. (In that case, a non-nested `>'
10743 token encountered during the parsing of the assignment-expression
10744 is not interpreted as a greater-than operator.)
a723baf1
MM
10745
10746 Returns a TREE_LIST representing the parameter-declaration. The
10747 TREE_VALUE is a representation of the decl-specifier-seq and
10748 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10749 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10750 TREE_VALUE represents the declarator. */
10751
10752static tree
ec194454
MM
10753cp_parser_parameter_declaration (cp_parser *parser,
10754 bool template_parm_p)
a723baf1
MM
10755{
10756 bool declares_class_or_enum;
ec194454 10757 bool greater_than_is_operator_p;
a723baf1
MM
10758 tree decl_specifiers;
10759 tree attributes;
10760 tree declarator;
10761 tree default_argument;
10762 tree parameter;
10763 cp_token *token;
10764 const char *saved_message;
10765
ec194454
MM
10766 /* In a template parameter, `>' is not an operator.
10767
10768 [temp.param]
10769
10770 When parsing a default template-argument for a non-type
10771 template-parameter, the first non-nested `>' is taken as the end
10772 of the template parameter-list rather than a greater-than
10773 operator. */
10774 greater_than_is_operator_p = !template_parm_p;
10775
a723baf1
MM
10776 /* Type definitions may not appear in parameter types. */
10777 saved_message = parser->type_definition_forbidden_message;
10778 parser->type_definition_forbidden_message
10779 = "types may not be defined in parameter types";
10780
10781 /* Parse the declaration-specifiers. */
10782 decl_specifiers
10783 = cp_parser_decl_specifier_seq (parser,
10784 CP_PARSER_FLAGS_NONE,
10785 &attributes,
10786 &declares_class_or_enum);
10787 /* If an error occurred, there's no reason to attempt to parse the
10788 rest of the declaration. */
10789 if (cp_parser_error_occurred (parser))
10790 {
10791 parser->type_definition_forbidden_message = saved_message;
10792 return error_mark_node;
10793 }
10794
10795 /* Peek at the next token. */
10796 token = cp_lexer_peek_token (parser->lexer);
10797 /* If the next token is a `)', `,', `=', `>', or `...', then there
10798 is no declarator. */
10799 if (token->type == CPP_CLOSE_PAREN
10800 || token->type == CPP_COMMA
10801 || token->type == CPP_EQ
10802 || token->type == CPP_ELLIPSIS
10803 || token->type == CPP_GREATER)
10804 declarator = NULL_TREE;
10805 /* Otherwise, there should be a declarator. */
10806 else
10807 {
10808 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10809 parser->default_arg_ok_p = false;
10810
a723baf1 10811 declarator = cp_parser_declarator (parser,
62b8a44e 10812 CP_PARSER_DECLARATOR_EITHER,
a723baf1 10813 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1 10814 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
10815 /* After the declarator, allow more attributes. */
10816 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
10817 }
10818
62b8a44e 10819 /* The restriction on defining new types applies only to the type
a723baf1
MM
10820 of the parameter, not to the default argument. */
10821 parser->type_definition_forbidden_message = saved_message;
10822
10823 /* If the next token is `=', then process a default argument. */
10824 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10825 {
10826 bool saved_greater_than_is_operator_p;
10827 /* Consume the `='. */
10828 cp_lexer_consume_token (parser->lexer);
10829
10830 /* If we are defining a class, then the tokens that make up the
10831 default argument must be saved and processed later. */
ec194454
MM
10832 if (!template_parm_p && at_class_scope_p ()
10833 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
10834 {
10835 unsigned depth = 0;
10836
10837 /* Create a DEFAULT_ARG to represented the unparsed default
10838 argument. */
10839 default_argument = make_node (DEFAULT_ARG);
10840 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10841
10842 /* Add tokens until we have processed the entire default
10843 argument. */
10844 while (true)
10845 {
10846 bool done = false;
10847 cp_token *token;
10848
10849 /* Peek at the next token. */
10850 token = cp_lexer_peek_token (parser->lexer);
10851 /* What we do depends on what token we have. */
10852 switch (token->type)
10853 {
10854 /* In valid code, a default argument must be
10855 immediately followed by a `,' `)', or `...'. */
10856 case CPP_COMMA:
10857 case CPP_CLOSE_PAREN:
10858 case CPP_ELLIPSIS:
10859 /* If we run into a non-nested `;', `}', or `]',
10860 then the code is invalid -- but the default
10861 argument is certainly over. */
10862 case CPP_SEMICOLON:
10863 case CPP_CLOSE_BRACE:
10864 case CPP_CLOSE_SQUARE:
10865 if (depth == 0)
10866 done = true;
10867 /* Update DEPTH, if necessary. */
10868 else if (token->type == CPP_CLOSE_PAREN
10869 || token->type == CPP_CLOSE_BRACE
10870 || token->type == CPP_CLOSE_SQUARE)
10871 --depth;
10872 break;
10873
10874 case CPP_OPEN_PAREN:
10875 case CPP_OPEN_SQUARE:
10876 case CPP_OPEN_BRACE:
10877 ++depth;
10878 break;
10879
10880 case CPP_GREATER:
10881 /* If we see a non-nested `>', and `>' is not an
10882 operator, then it marks the end of the default
10883 argument. */
10884 if (!depth && !greater_than_is_operator_p)
10885 done = true;
10886 break;
10887
10888 /* If we run out of tokens, issue an error message. */
10889 case CPP_EOF:
10890 error ("file ends in default argument");
10891 done = true;
10892 break;
10893
10894 case CPP_NAME:
10895 case CPP_SCOPE:
10896 /* In these cases, we should look for template-ids.
10897 For example, if the default argument is
10898 `X<int, double>()', we need to do name lookup to
10899 figure out whether or not `X' is a template; if
10900 so, the `,' does not end the deault argument.
10901
10902 That is not yet done. */
10903 break;
10904
10905 default:
10906 break;
10907 }
10908
10909 /* If we've reached the end, stop. */
10910 if (done)
10911 break;
10912
10913 /* Add the token to the token block. */
10914 token = cp_lexer_consume_token (parser->lexer);
10915 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10916 token);
10917 }
10918 }
10919 /* Outside of a class definition, we can just parse the
10920 assignment-expression. */
10921 else
10922 {
10923 bool saved_local_variables_forbidden_p;
10924
10925 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10926 set correctly. */
10927 saved_greater_than_is_operator_p
10928 = parser->greater_than_is_operator_p;
10929 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10930 /* Local variable names (and the `this' keyword) may not
10931 appear in a default argument. */
10932 saved_local_variables_forbidden_p
10933 = parser->local_variables_forbidden_p;
10934 parser->local_variables_forbidden_p = true;
10935 /* Parse the assignment-expression. */
10936 default_argument = cp_parser_assignment_expression (parser);
10937 /* Restore saved state. */
10938 parser->greater_than_is_operator_p
10939 = saved_greater_than_is_operator_p;
10940 parser->local_variables_forbidden_p
10941 = saved_local_variables_forbidden_p;
10942 }
10943 if (!parser->default_arg_ok_p)
10944 {
10945 pedwarn ("default arguments are only permitted on functions");
10946 if (flag_pedantic_errors)
10947 default_argument = NULL_TREE;
10948 }
10949 }
10950 else
10951 default_argument = NULL_TREE;
10952
10953 /* Create the representation of the parameter. */
10954 if (attributes)
10955 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10956 parameter = build_tree_list (default_argument,
10957 build_tree_list (decl_specifiers,
10958 declarator));
10959
10960 return parameter;
10961}
10962
10963/* Parse a function-definition.
10964
10965 function-definition:
10966 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10967 function-body
10968 decl-specifier-seq [opt] declarator function-try-block
10969
10970 GNU Extension:
10971
10972 function-definition:
10973 __extension__ function-definition
10974
10975 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10976 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10977 be a `friend'. */
10978
10979static tree
10980cp_parser_function_definition (parser, friend_p)
10981 cp_parser *parser;
10982 bool *friend_p;
10983{
10984 tree decl_specifiers;
10985 tree attributes;
10986 tree declarator;
10987 tree fn;
a723baf1
MM
10988 cp_token *token;
10989 bool declares_class_or_enum;
10990 bool member_p;
10991 /* The saved value of the PEDANTIC flag. */
10992 int saved_pedantic;
10993
10994 /* Any pending qualification must be cleared by our caller. It is
10995 more robust to force the callers to clear PARSER->SCOPE than to
10996 do it here since if the qualification is in effect here, it might
10997 also end up in effect elsewhere that it is not intended. */
10998 my_friendly_assert (!parser->scope, 20010821);
10999
11000 /* Handle `__extension__'. */
11001 if (cp_parser_extension_opt (parser, &saved_pedantic))
11002 {
11003 /* Parse the function-definition. */
11004 fn = cp_parser_function_definition (parser, friend_p);
11005 /* Restore the PEDANTIC flag. */
11006 pedantic = saved_pedantic;
11007
11008 return fn;
11009 }
11010
11011 /* Check to see if this definition appears in a class-specifier. */
11012 member_p = (at_class_scope_p ()
11013 && TYPE_BEING_DEFINED (current_class_type));
11014 /* Defer access checks in the decl-specifier-seq until we know what
11015 function is being defined. There is no need to do this for the
11016 definition of member functions; we cannot be defining a member
11017 from another class. */
cf22909c
KL
11018 push_deferring_access_checks (!member_p);
11019
a723baf1
MM
11020 /* Parse the decl-specifier-seq. */
11021 decl_specifiers
11022 = cp_parser_decl_specifier_seq (parser,
11023 CP_PARSER_FLAGS_OPTIONAL,
11024 &attributes,
11025 &declares_class_or_enum);
11026 /* Figure out whether this declaration is a `friend'. */
11027 if (friend_p)
11028 *friend_p = cp_parser_friend_p (decl_specifiers);
11029
11030 /* Parse the declarator. */
62b8a44e 11031 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
11032 /*ctor_dtor_or_conv_p=*/NULL);
11033
11034 /* Gather up any access checks that occurred. */
cf22909c 11035 stop_deferring_access_checks ();
a723baf1
MM
11036
11037 /* If something has already gone wrong, we may as well stop now. */
11038 if (declarator == error_mark_node)
11039 {
11040 /* Skip to the end of the function, or if this wasn't anything
11041 like a function-definition, to a `;' in the hopes of finding
11042 a sensible place from which to continue parsing. */
11043 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11044 pop_deferring_access_checks ();
a723baf1
MM
11045 return error_mark_node;
11046 }
11047
11048 /* The next character should be a `{' (for a simple function
11049 definition), a `:' (for a ctor-initializer), or `try' (for a
11050 function-try block). */
11051 token = cp_lexer_peek_token (parser->lexer);
11052 if (!cp_parser_token_starts_function_definition_p (token))
11053 {
11054 /* Issue the error-message. */
11055 cp_parser_error (parser, "expected function-definition");
11056 /* Skip to the next `;'. */
11057 cp_parser_skip_to_end_of_block_or_statement (parser);
11058
cf22909c 11059 pop_deferring_access_checks ();
a723baf1
MM
11060 return error_mark_node;
11061 }
11062
11063 /* If we are in a class scope, then we must handle
11064 function-definitions specially. In particular, we save away the
11065 tokens that make up the function body, and parse them again
11066 later, in order to handle code like:
11067
11068 struct S {
11069 int f () { return i; }
11070 int i;
11071 };
11072
11073 Here, we cannot parse the body of `f' until after we have seen
11074 the declaration of `i'. */
11075 if (member_p)
11076 {
11077 cp_token_cache *cache;
11078
11079 /* Create the function-declaration. */
11080 fn = start_method (decl_specifiers, declarator, attributes);
11081 /* If something went badly wrong, bail out now. */
11082 if (fn == error_mark_node)
11083 {
11084 /* If there's a function-body, skip it. */
11085 if (cp_parser_token_starts_function_definition_p
11086 (cp_lexer_peek_token (parser->lexer)))
11087 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11088 pop_deferring_access_checks ();
a723baf1
MM
11089 return error_mark_node;
11090 }
11091
11092 /* Create a token cache. */
11093 cache = cp_token_cache_new ();
11094 /* Save away the tokens that make up the body of the
11095 function. */
11096 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11097 /* Handle function try blocks. */
11098 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11099 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11100
11101 /* Save away the inline definition; we will process it when the
11102 class is complete. */
11103 DECL_PENDING_INLINE_INFO (fn) = cache;
11104 DECL_PENDING_INLINE_P (fn) = 1;
11105
11106 /* We're done with the inline definition. */
11107 finish_method (fn);
11108
11109 /* Add FN to the queue of functions to be parsed later. */
11110 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 11111 = tree_cons (NULL_TREE, fn,
a723baf1
MM
11112 TREE_VALUE (parser->unparsed_functions_queues));
11113
cf22909c 11114 pop_deferring_access_checks ();
a723baf1
MM
11115 return fn;
11116 }
11117
11118 /* Check that the number of template-parameter-lists is OK. */
11119 if (!cp_parser_check_declarator_template_parameters (parser,
11120 declarator))
11121 {
11122 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11123 pop_deferring_access_checks ();
a723baf1
MM
11124 return error_mark_node;
11125 }
11126
cf22909c
KL
11127 fn = cp_parser_function_definition_from_specifiers_and_declarator
11128 (parser, decl_specifiers, attributes, declarator);
11129 pop_deferring_access_checks ();
11130 return fn;
a723baf1
MM
11131}
11132
11133/* Parse a function-body.
11134
11135 function-body:
11136 compound_statement */
11137
11138static void
11139cp_parser_function_body (cp_parser *parser)
11140{
11141 cp_parser_compound_statement (parser);
11142}
11143
11144/* Parse a ctor-initializer-opt followed by a function-body. Return
11145 true if a ctor-initializer was present. */
11146
11147static bool
11148cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11149{
11150 tree body;
11151 bool ctor_initializer_p;
11152
11153 /* Begin the function body. */
11154 body = begin_function_body ();
11155 /* Parse the optional ctor-initializer. */
11156 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11157 /* Parse the function-body. */
11158 cp_parser_function_body (parser);
11159 /* Finish the function body. */
11160 finish_function_body (body);
11161
11162 return ctor_initializer_p;
11163}
11164
11165/* Parse an initializer.
11166
11167 initializer:
11168 = initializer-clause
11169 ( expression-list )
11170
11171 Returns a expression representing the initializer. If no
11172 initializer is present, NULL_TREE is returned.
11173
11174 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11175 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11176 set to FALSE if there is no initializer present. */
11177
11178static tree
11179cp_parser_initializer (parser, is_parenthesized_init)
11180 cp_parser *parser;
11181 bool *is_parenthesized_init;
11182{
11183 cp_token *token;
11184 tree init;
11185
11186 /* Peek at the next token. */
11187 token = cp_lexer_peek_token (parser->lexer);
11188
11189 /* Let our caller know whether or not this initializer was
11190 parenthesized. */
11191 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11192
11193 if (token->type == CPP_EQ)
11194 {
11195 /* Consume the `='. */
11196 cp_lexer_consume_token (parser->lexer);
11197 /* Parse the initializer-clause. */
11198 init = cp_parser_initializer_clause (parser);
11199 }
11200 else if (token->type == CPP_OPEN_PAREN)
11201 {
11202 /* Consume the `('. */
11203 cp_lexer_consume_token (parser->lexer);
11204 /* Parse the expression-list. */
11205 init = cp_parser_expression_list (parser);
11206 /* Consume the `)' token. */
11207 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11208 cp_parser_skip_to_closing_parenthesis (parser);
11209 }
11210 else
11211 {
11212 /* Anything else is an error. */
11213 cp_parser_error (parser, "expected initializer");
11214 init = error_mark_node;
11215 }
11216
11217 return init;
11218}
11219
11220/* Parse an initializer-clause.
11221
11222 initializer-clause:
11223 assignment-expression
11224 { initializer-list , [opt] }
11225 { }
11226
11227 Returns an expression representing the initializer.
11228
11229 If the `assignment-expression' production is used the value
11230 returned is simply a reprsentation for the expression.
11231
11232 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11233 the elements of the initializer-list (or NULL_TREE, if the last
11234 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11235 NULL_TREE. There is no way to detect whether or not the optional
11236 trailing `,' was provided. */
11237
11238static tree
11239cp_parser_initializer_clause (parser)
11240 cp_parser *parser;
11241{
11242 tree initializer;
11243
11244 /* If it is not a `{', then we are looking at an
11245 assignment-expression. */
11246 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11247 initializer = cp_parser_assignment_expression (parser);
11248 else
11249 {
11250 /* Consume the `{' token. */
11251 cp_lexer_consume_token (parser->lexer);
11252 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11253 initializer = make_node (CONSTRUCTOR);
11254 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11255 necessary, but check_initializer depends upon it, for
11256 now. */
11257 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11258 /* If it's not a `}', then there is a non-trivial initializer. */
11259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11260 {
11261 /* Parse the initializer list. */
11262 CONSTRUCTOR_ELTS (initializer)
11263 = cp_parser_initializer_list (parser);
11264 /* A trailing `,' token is allowed. */
11265 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11266 cp_lexer_consume_token (parser->lexer);
11267 }
11268
11269 /* Now, there should be a trailing `}'. */
11270 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11271 }
11272
11273 return initializer;
11274}
11275
11276/* Parse an initializer-list.
11277
11278 initializer-list:
11279 initializer-clause
11280 initializer-list , initializer-clause
11281
11282 GNU Extension:
11283
11284 initializer-list:
11285 identifier : initializer-clause
11286 initializer-list, identifier : initializer-clause
11287
11288 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11289 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11290 IDENTIFIER_NODE naming the field to initialize. */
11291
11292static tree
11293cp_parser_initializer_list (parser)
11294 cp_parser *parser;
11295{
11296 tree initializers = NULL_TREE;
11297
11298 /* Parse the rest of the list. */
11299 while (true)
11300 {
11301 cp_token *token;
11302 tree identifier;
11303 tree initializer;
11304
11305 /* If the next token is an identifier and the following one is a
11306 colon, we are looking at the GNU designated-initializer
11307 syntax. */
11308 if (cp_parser_allow_gnu_extensions_p (parser)
11309 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11310 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11311 {
11312 /* Consume the identifier. */
11313 identifier = cp_lexer_consume_token (parser->lexer)->value;
11314 /* Consume the `:'. */
11315 cp_lexer_consume_token (parser->lexer);
11316 }
11317 else
11318 identifier = NULL_TREE;
11319
11320 /* Parse the initializer. */
11321 initializer = cp_parser_initializer_clause (parser);
11322
11323 /* Add it to the list. */
11324 initializers = tree_cons (identifier, initializer, initializers);
11325
11326 /* If the next token is not a comma, we have reached the end of
11327 the list. */
11328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11329 break;
11330
11331 /* Peek at the next token. */
11332 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11333 /* If the next token is a `}', then we're still done. An
11334 initializer-clause can have a trailing `,' after the
11335 initializer-list and before the closing `}'. */
11336 if (token->type == CPP_CLOSE_BRACE)
11337 break;
11338
11339 /* Consume the `,' token. */
11340 cp_lexer_consume_token (parser->lexer);
11341 }
11342
11343 /* The initializers were built up in reverse order, so we need to
11344 reverse them now. */
11345 return nreverse (initializers);
11346}
11347
11348/* Classes [gram.class] */
11349
11350/* Parse a class-name.
11351
11352 class-name:
11353 identifier
11354 template-id
11355
11356 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11357 to indicate that names looked up in dependent types should be
11358 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11359 keyword has been used to indicate that the name that appears next
11360 is a template. TYPE_P is true iff the next name should be treated
11361 as class-name, even if it is declared to be some other kind of name
11362 as well. The accessibility of the class-name is checked iff
11363 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11364 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11365 is the class being defined in a class-head.
11366
11367 Returns the TYPE_DECL representing the class. */
11368
11369static tree
11370cp_parser_class_name (cp_parser *parser,
11371 bool typename_keyword_p,
11372 bool template_keyword_p,
11373 bool type_p,
11374 bool check_access_p,
11375 bool check_dependency_p,
11376 bool class_head_p)
11377{
11378 tree decl;
11379 tree scope;
11380 bool typename_p;
e5976695
MM
11381 cp_token *token;
11382
11383 /* All class-names start with an identifier. */
11384 token = cp_lexer_peek_token (parser->lexer);
11385 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11386 {
11387 cp_parser_error (parser, "expected class-name");
11388 return error_mark_node;
11389 }
11390
a723baf1
MM
11391 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11392 to a template-id, so we save it here. */
11393 scope = parser->scope;
11394 /* Any name names a type if we're following the `typename' keyword
11395 in a qualified name where the enclosing scope is type-dependent. */
11396 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11397 && cp_parser_dependent_type_p (scope));
e5976695
MM
11398 /* Handle the common case (an identifier, but not a template-id)
11399 efficiently. */
11400 if (token->type == CPP_NAME
11401 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
a723baf1 11402 {
a723baf1
MM
11403 tree identifier;
11404
11405 /* Look for the identifier. */
11406 identifier = cp_parser_identifier (parser);
11407 /* If the next token isn't an identifier, we are certainly not
11408 looking at a class-name. */
11409 if (identifier == error_mark_node)
11410 decl = error_mark_node;
11411 /* If we know this is a type-name, there's no need to look it
11412 up. */
11413 else if (typename_p)
11414 decl = identifier;
11415 else
11416 {
11417 /* If the next token is a `::', then the name must be a type
11418 name.
11419
11420 [basic.lookup.qual]
11421
11422 During the lookup for a name preceding the :: scope
11423 resolution operator, object, function, and enumerator
11424 names are ignored. */
11425 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11426 type_p = true;
11427 /* Look up the name. */
11428 decl = cp_parser_lookup_name (parser, identifier,
11429 check_access_p,
11430 type_p,
eea9800f 11431 /*is_namespace=*/false,
a723baf1
MM
11432 check_dependency_p);
11433 }
11434 }
e5976695
MM
11435 else
11436 {
11437 /* Try a template-id. */
11438 decl = cp_parser_template_id (parser, template_keyword_p,
11439 check_dependency_p);
11440 if (decl == error_mark_node)
11441 return error_mark_node;
11442 }
a723baf1
MM
11443
11444 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11445
11446 /* If this is a typename, create a TYPENAME_TYPE. */
11447 if (typename_p && decl != error_mark_node)
11448 decl = TYPE_NAME (make_typename_type (scope, decl,
11449 /*complain=*/1));
11450
11451 /* Check to see that it is really the name of a class. */
11452 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11453 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11454 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11455 /* Situations like this:
11456
11457 template <typename T> struct A {
11458 typename T::template X<int>::I i;
11459 };
11460
11461 are problematic. Is `T::template X<int>' a class-name? The
11462 standard does not seem to be definitive, but there is no other
11463 valid interpretation of the following `::'. Therefore, those
11464 names are considered class-names. */
11465 decl = TYPE_NAME (make_typename_type (scope, decl,
11466 tf_error | tf_parsing));
11467 else if (decl == error_mark_node
11468 || TREE_CODE (decl) != TYPE_DECL
11469 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11470 {
11471 cp_parser_error (parser, "expected class-name");
11472 return error_mark_node;
11473 }
11474
11475 return decl;
11476}
11477
11478/* Parse a class-specifier.
11479
11480 class-specifier:
11481 class-head { member-specification [opt] }
11482
11483 Returns the TREE_TYPE representing the class. */
11484
11485static tree
11486cp_parser_class_specifier (parser)
11487 cp_parser *parser;
11488{
11489 cp_token *token;
11490 tree type;
11491 tree attributes = NULL_TREE;
11492 int has_trailing_semicolon;
11493 bool nested_name_specifier_p;
a723baf1
MM
11494 unsigned saved_num_template_parameter_lists;
11495
cf22909c
KL
11496 push_deferring_access_checks (false);
11497
a723baf1
MM
11498 /* Parse the class-head. */
11499 type = cp_parser_class_head (parser,
cf22909c 11500 &nested_name_specifier_p);
a723baf1
MM
11501 /* If the class-head was a semantic disaster, skip the entire body
11502 of the class. */
11503 if (!type)
11504 {
11505 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11506 pop_deferring_access_checks ();
a723baf1
MM
11507 return error_mark_node;
11508 }
cf22909c 11509
a723baf1
MM
11510 /* Look for the `{'. */
11511 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
11512 {
11513 pop_deferring_access_checks ();
11514 return error_mark_node;
11515 }
11516
a723baf1
MM
11517 /* Issue an error message if type-definitions are forbidden here. */
11518 cp_parser_check_type_definition (parser);
11519 /* Remember that we are defining one more class. */
11520 ++parser->num_classes_being_defined;
11521 /* Inside the class, surrounding template-parameter-lists do not
11522 apply. */
11523 saved_num_template_parameter_lists
11524 = parser->num_template_parameter_lists;
11525 parser->num_template_parameter_lists = 0;
11526 /* Start the class. */
11527 type = begin_class_definition (type);
11528 if (type == error_mark_node)
11529 /* If the type is erroneous, skip the entire body of the class. */
11530 cp_parser_skip_to_closing_brace (parser);
11531 else
11532 /* Parse the member-specification. */
11533 cp_parser_member_specification_opt (parser);
11534 /* Look for the trailing `}'. */
11535 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11536 /* We get better error messages by noticing a common problem: a
11537 missing trailing `;'. */
11538 token = cp_lexer_peek_token (parser->lexer);
11539 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11540 /* Look for attributes to apply to this class. */
11541 if (cp_parser_allow_gnu_extensions_p (parser))
11542 attributes = cp_parser_attributes_opt (parser);
11543 /* Finish the class definition. */
11544 type = finish_class_definition (type,
11545 attributes,
11546 has_trailing_semicolon,
11547 nested_name_specifier_p);
11548 /* If this class is not itself within the scope of another class,
11549 then we need to parse the bodies of all of the queued function
11550 definitions. Note that the queued functions defined in a class
11551 are not always processed immediately following the
11552 class-specifier for that class. Consider:
11553
11554 struct A {
11555 struct B { void f() { sizeof (A); } };
11556 };
11557
11558 If `f' were processed before the processing of `A' were
11559 completed, there would be no way to compute the size of `A'.
11560 Note that the nesting we are interested in here is lexical --
11561 not the semantic nesting given by TYPE_CONTEXT. In particular,
11562 for:
11563
11564 struct A { struct B; };
11565 struct A::B { void f() { } };
11566
11567 there is no need to delay the parsing of `A::B::f'. */
11568 if (--parser->num_classes_being_defined == 0)
11569 {
11570 tree last_scope = NULL_TREE;
8218bd34
MM
11571 tree queue_entry;
11572 tree fn;
a723baf1 11573
a723baf1
MM
11574 /* Reverse the queue, so that we process it in the order the
11575 functions were declared. */
11576 TREE_VALUE (parser->unparsed_functions_queues)
11577 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
8218bd34
MM
11578 /* In a first pass, parse default arguments to the functions.
11579 Then, in a second pass, parse the bodies of the functions.
11580 This two-phased approach handles cases like:
11581
11582 struct S {
11583 void f() { g(); }
11584 void g(int i = 3);
11585 };
11586
11587 */
11588 for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11589 queue_entry;
11590 queue_entry = TREE_CHAIN (queue_entry))
11591 {
11592 fn = TREE_VALUE (queue_entry);
11593 if (DECL_FUNCTION_TEMPLATE_P (fn))
11594 fn = DECL_TEMPLATE_RESULT (fn);
11595 /* Make sure that any template parameters are in scope. */
11596 maybe_begin_member_template_processing (fn);
11597 /* If there are default arguments that have not yet been processed,
11598 take care of them now. */
11599 cp_parser_late_parsing_default_args (parser, fn);
11600 /* Remove any template parameters from the symbol table. */
11601 maybe_end_member_template_processing ();
11602 }
11603 /* Now parse the body of the functions. */
a723baf1
MM
11604 while (TREE_VALUE (parser->unparsed_functions_queues))
11605
11606 {
a723baf1
MM
11607 /* Figure out which function we need to process. */
11608 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
a723baf1
MM
11609 fn = TREE_VALUE (queue_entry);
11610
11611 /* Parse the function. */
11612 cp_parser_late_parsing_for_member (parser, fn);
11613
11614 TREE_VALUE (parser->unparsed_functions_queues)
11615 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11616 }
11617
11618 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11619 more time than we have popped, so me must pop here. */
11620 if (last_scope)
11621 pop_scope (last_scope);
11622 }
11623
11624 /* Put back any saved access checks. */
cf22909c 11625 pop_deferring_access_checks ();
a723baf1
MM
11626
11627 /* Restore the count of active template-parameter-lists. */
11628 parser->num_template_parameter_lists
11629 = saved_num_template_parameter_lists;
11630
11631 return type;
11632}
11633
11634/* Parse a class-head.
11635
11636 class-head:
11637 class-key identifier [opt] base-clause [opt]
11638 class-key nested-name-specifier identifier base-clause [opt]
11639 class-key nested-name-specifier [opt] template-id
11640 base-clause [opt]
11641
11642 GNU Extensions:
11643 class-key attributes identifier [opt] base-clause [opt]
11644 class-key attributes nested-name-specifier identifier base-clause [opt]
11645 class-key attributes nested-name-specifier [opt] template-id
11646 base-clause [opt]
11647
11648 Returns the TYPE of the indicated class. Sets
11649 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11650 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
11651
11652 Returns NULL_TREE if the class-head is syntactically valid, but
11653 semantically invalid in a way that means we should skip the entire
11654 body of the class. */
11655
11656static tree
11657cp_parser_class_head (parser,
cf22909c 11658 nested_name_specifier_p)
a723baf1
MM
11659 cp_parser *parser;
11660 bool *nested_name_specifier_p;
a723baf1
MM
11661{
11662 cp_token *token;
11663 tree nested_name_specifier;
11664 enum tag_types class_key;
11665 tree id = NULL_TREE;
11666 tree type = NULL_TREE;
11667 tree attributes;
11668 bool template_id_p = false;
11669 bool qualified_p = false;
11670 bool invalid_nested_name_p = false;
11671 unsigned num_templates;
11672
11673 /* Assume no nested-name-specifier will be present. */
11674 *nested_name_specifier_p = false;
11675 /* Assume no template parameter lists will be used in defining the
11676 type. */
11677 num_templates = 0;
11678
11679 /* Look for the class-key. */
11680 class_key = cp_parser_class_key (parser);
11681 if (class_key == none_type)
11682 return error_mark_node;
11683
11684 /* Parse the attributes. */
11685 attributes = cp_parser_attributes_opt (parser);
11686
11687 /* If the next token is `::', that is invalid -- but sometimes
11688 people do try to write:
11689
11690 struct ::S {};
11691
11692 Handle this gracefully by accepting the extra qualifier, and then
11693 issuing an error about it later if this really is a
2050a1bb 11694 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11695 specifier, remain silent. */
11696 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11697 qualified_p = true;
11698
11699 /* Determine the name of the class. Begin by looking for an
11700 optional nested-name-specifier. */
11701 nested_name_specifier
11702 = cp_parser_nested_name_specifier_opt (parser,
11703 /*typename_keyword_p=*/false,
11704 /*check_dependency_p=*/true,
11705 /*type_p=*/false);
11706 /* If there was a nested-name-specifier, then there *must* be an
11707 identifier. */
11708 if (nested_name_specifier)
11709 {
11710 /* Although the grammar says `identifier', it really means
11711 `class-name' or `template-name'. You are only allowed to
11712 define a class that has already been declared with this
11713 syntax.
11714
11715 The proposed resolution for Core Issue 180 says that whever
11716 you see `class T::X' you should treat `X' as a type-name.
11717
11718 It is OK to define an inaccessible class; for example:
11719
11720 class A { class B; };
11721 class A::B {};
11722
11723 So, we ask cp_parser_class_name not to check accessibility.
11724
11725 We do not know if we will see a class-name, or a
11726 template-name. We look for a class-name first, in case the
11727 class-name is a template-id; if we looked for the
11728 template-name first we would stop after the template-name. */
11729 cp_parser_parse_tentatively (parser);
11730 type = cp_parser_class_name (parser,
11731 /*typename_keyword_p=*/false,
11732 /*template_keyword_p=*/false,
11733 /*type_p=*/true,
11734 /*check_access_p=*/false,
11735 /*check_dependency_p=*/false,
11736 /*class_head_p=*/true);
11737 /* If that didn't work, ignore the nested-name-specifier. */
11738 if (!cp_parser_parse_definitely (parser))
11739 {
11740 invalid_nested_name_p = true;
11741 id = cp_parser_identifier (parser);
11742 if (id == error_mark_node)
11743 id = NULL_TREE;
11744 }
11745 /* If we could not find a corresponding TYPE, treat this
11746 declaration like an unqualified declaration. */
11747 if (type == error_mark_node)
11748 nested_name_specifier = NULL_TREE;
11749 /* Otherwise, count the number of templates used in TYPE and its
11750 containing scopes. */
11751 else
11752 {
11753 tree scope;
11754
11755 for (scope = TREE_TYPE (type);
11756 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11757 scope = (TYPE_P (scope)
11758 ? TYPE_CONTEXT (scope)
11759 : DECL_CONTEXT (scope)))
11760 if (TYPE_P (scope)
11761 && CLASS_TYPE_P (scope)
11762 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
11763 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11764 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
11765 ++num_templates;
11766 }
11767 }
11768 /* Otherwise, the identifier is optional. */
11769 else
11770 {
11771 /* We don't know whether what comes next is a template-id,
11772 an identifier, or nothing at all. */
11773 cp_parser_parse_tentatively (parser);
11774 /* Check for a template-id. */
11775 id = cp_parser_template_id (parser,
11776 /*template_keyword_p=*/false,
11777 /*check_dependency_p=*/true);
11778 /* If that didn't work, it could still be an identifier. */
11779 if (!cp_parser_parse_definitely (parser))
11780 {
11781 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11782 id = cp_parser_identifier (parser);
11783 else
11784 id = NULL_TREE;
11785 }
11786 else
11787 {
11788 template_id_p = true;
11789 ++num_templates;
11790 }
11791 }
11792
11793 /* If it's not a `:' or a `{' then we can't really be looking at a
11794 class-head, since a class-head only appears as part of a
11795 class-specifier. We have to detect this situation before calling
11796 xref_tag, since that has irreversible side-effects. */
11797 if (!cp_parser_next_token_starts_class_definition_p (parser))
11798 {
11799 cp_parser_error (parser, "expected `{' or `:'");
11800 return error_mark_node;
11801 }
11802
11803 /* At this point, we're going ahead with the class-specifier, even
11804 if some other problem occurs. */
11805 cp_parser_commit_to_tentative_parse (parser);
11806 /* Issue the error about the overly-qualified name now. */
11807 if (qualified_p)
11808 cp_parser_error (parser,
11809 "global qualification of class name is invalid");
11810 else if (invalid_nested_name_p)
11811 cp_parser_error (parser,
11812 "qualified name does not name a class");
11813 /* Make sure that the right number of template parameters were
11814 present. */
11815 if (!cp_parser_check_template_parameters (parser, num_templates))
11816 /* If something went wrong, there is no point in even trying to
11817 process the class-definition. */
11818 return NULL_TREE;
11819
a723baf1
MM
11820 /* Look up the type. */
11821 if (template_id_p)
11822 {
11823 type = TREE_TYPE (id);
11824 maybe_process_partial_specialization (type);
11825 }
11826 else if (!nested_name_specifier)
11827 {
11828 /* If the class was unnamed, create a dummy name. */
11829 if (!id)
11830 id = make_anon_name ();
11831 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11832 }
11833 else
11834 {
848eed92 11835 bool new_type_p;
a723baf1
MM
11836 tree class_type;
11837
11838 /* Given:
11839
11840 template <typename T> struct S { struct T };
11841 template <typename T> struct S::T { };
11842
11843 we will get a TYPENAME_TYPE when processing the definition of
11844 `S::T'. We need to resolve it to the actual type before we
11845 try to define it. */
11846 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11847 {
11848 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11849 if (type != error_mark_node)
11850 type = TYPE_NAME (type);
11851 }
11852
11853 maybe_process_partial_specialization (TREE_TYPE (type));
11854 class_type = current_class_type;
11855 type = TREE_TYPE (handle_class_head (class_key,
11856 nested_name_specifier,
11857 type,
11858 attributes,
848eed92 11859 /*defn_p=*/true,
a723baf1
MM
11860 &new_type_p));
11861 if (type != error_mark_node)
11862 {
11863 if (!class_type && TYPE_CONTEXT (type))
11864 *nested_name_specifier_p = true;
11865 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11866 class_type))
11867 *nested_name_specifier_p = true;
11868 }
11869 }
11870 /* Indicate whether this class was declared as a `class' or as a
11871 `struct'. */
11872 if (TREE_CODE (type) == RECORD_TYPE)
11873 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11874 cp_parser_check_class_key (class_key, type);
11875
11876 /* Enter the scope containing the class; the names of base classes
11877 should be looked up in that context. For example, given:
11878
11879 struct A { struct B {}; struct C; };
11880 struct A::C : B {};
11881
11882 is valid. */
11883 if (nested_name_specifier)
11884 push_scope (nested_name_specifier);
11885 /* Now, look for the base-clause. */
11886 token = cp_lexer_peek_token (parser->lexer);
11887 if (token->type == CPP_COLON)
11888 {
11889 tree bases;
11890
11891 /* Get the list of base-classes. */
11892 bases = cp_parser_base_clause (parser);
11893 /* Process them. */
11894 xref_basetypes (type, bases);
11895 }
11896 /* Leave the scope given by the nested-name-specifier. We will
11897 enter the class scope itself while processing the members. */
11898 if (nested_name_specifier)
11899 pop_scope (nested_name_specifier);
11900
11901 return type;
11902}
11903
11904/* Parse a class-key.
11905
11906 class-key:
11907 class
11908 struct
11909 union
11910
11911 Returns the kind of class-key specified, or none_type to indicate
11912 error. */
11913
11914static enum tag_types
11915cp_parser_class_key (parser)
11916 cp_parser *parser;
11917{
11918 cp_token *token;
11919 enum tag_types tag_type;
11920
11921 /* Look for the class-key. */
11922 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11923 if (!token)
11924 return none_type;
11925
11926 /* Check to see if the TOKEN is a class-key. */
11927 tag_type = cp_parser_token_is_class_key (token);
11928 if (!tag_type)
11929 cp_parser_error (parser, "expected class-key");
11930 return tag_type;
11931}
11932
11933/* Parse an (optional) member-specification.
11934
11935 member-specification:
11936 member-declaration member-specification [opt]
11937 access-specifier : member-specification [opt] */
11938
11939static void
11940cp_parser_member_specification_opt (parser)
11941 cp_parser *parser;
11942{
11943 while (true)
11944 {
11945 cp_token *token;
11946 enum rid keyword;
11947
11948 /* Peek at the next token. */
11949 token = cp_lexer_peek_token (parser->lexer);
11950 /* If it's a `}', or EOF then we've seen all the members. */
11951 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11952 break;
11953
11954 /* See if this token is a keyword. */
11955 keyword = token->keyword;
11956 switch (keyword)
11957 {
11958 case RID_PUBLIC:
11959 case RID_PROTECTED:
11960 case RID_PRIVATE:
11961 /* Consume the access-specifier. */
11962 cp_lexer_consume_token (parser->lexer);
11963 /* Remember which access-specifier is active. */
11964 current_access_specifier = token->value;
11965 /* Look for the `:'. */
11966 cp_parser_require (parser, CPP_COLON, "`:'");
11967 break;
11968
11969 default:
11970 /* Otherwise, the next construction must be a
11971 member-declaration. */
11972 cp_parser_member_declaration (parser);
a723baf1
MM
11973 }
11974 }
11975}
11976
11977/* Parse a member-declaration.
11978
11979 member-declaration:
11980 decl-specifier-seq [opt] member-declarator-list [opt] ;
11981 function-definition ; [opt]
11982 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11983 using-declaration
11984 template-declaration
11985
11986 member-declarator-list:
11987 member-declarator
11988 member-declarator-list , member-declarator
11989
11990 member-declarator:
11991 declarator pure-specifier [opt]
11992 declarator constant-initializer [opt]
11993 identifier [opt] : constant-expression
11994
11995 GNU Extensions:
11996
11997 member-declaration:
11998 __extension__ member-declaration
11999
12000 member-declarator:
12001 declarator attributes [opt] pure-specifier [opt]
12002 declarator attributes [opt] constant-initializer [opt]
12003 identifier [opt] attributes [opt] : constant-expression */
12004
12005static void
12006cp_parser_member_declaration (parser)
12007 cp_parser *parser;
12008{
12009 tree decl_specifiers;
12010 tree prefix_attributes;
12011 tree decl;
12012 bool declares_class_or_enum;
12013 bool friend_p;
12014 cp_token *token;
12015 int saved_pedantic;
12016
12017 /* Check for the `__extension__' keyword. */
12018 if (cp_parser_extension_opt (parser, &saved_pedantic))
12019 {
12020 /* Recurse. */
12021 cp_parser_member_declaration (parser);
12022 /* Restore the old value of the PEDANTIC flag. */
12023 pedantic = saved_pedantic;
12024
12025 return;
12026 }
12027
12028 /* Check for a template-declaration. */
12029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12030 {
12031 /* Parse the template-declaration. */
12032 cp_parser_template_declaration (parser, /*member_p=*/true);
12033
12034 return;
12035 }
12036
12037 /* Check for a using-declaration. */
12038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12039 {
12040 /* Parse the using-declaration. */
12041 cp_parser_using_declaration (parser);
12042
12043 return;
12044 }
12045
12046 /* We can't tell whether we're looking at a declaration or a
12047 function-definition. */
12048 cp_parser_parse_tentatively (parser);
12049
12050 /* Parse the decl-specifier-seq. */
12051 decl_specifiers
12052 = cp_parser_decl_specifier_seq (parser,
12053 CP_PARSER_FLAGS_OPTIONAL,
12054 &prefix_attributes,
12055 &declares_class_or_enum);
12056 /* If there is no declarator, then the decl-specifier-seq should
12057 specify a type. */
12058 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12059 {
12060 /* If there was no decl-specifier-seq, and the next token is a
12061 `;', then we have something like:
12062
12063 struct S { ; };
12064
12065 [class.mem]
12066
12067 Each member-declaration shall declare at least one member
12068 name of the class. */
12069 if (!decl_specifiers)
12070 {
12071 if (pedantic)
12072 pedwarn ("extra semicolon");
12073 }
12074 else
12075 {
12076 tree type;
12077
12078 /* See if this declaration is a friend. */
12079 friend_p = cp_parser_friend_p (decl_specifiers);
12080 /* If there were decl-specifiers, check to see if there was
12081 a class-declaration. */
12082 type = check_tag_decl (decl_specifiers);
12083 /* Nested classes have already been added to the class, but
12084 a `friend' needs to be explicitly registered. */
12085 if (friend_p)
12086 {
12087 /* If the `friend' keyword was present, the friend must
12088 be introduced with a class-key. */
12089 if (!declares_class_or_enum)
12090 error ("a class-key must be used when declaring a friend");
12091 /* In this case:
12092
12093 template <typename T> struct A {
12094 friend struct A<T>::B;
12095 };
12096
12097 A<T>::B will be represented by a TYPENAME_TYPE, and
12098 therefore not recognized by check_tag_decl. */
12099 if (!type)
12100 {
12101 tree specifier;
12102
12103 for (specifier = decl_specifiers;
12104 specifier;
12105 specifier = TREE_CHAIN (specifier))
12106 {
12107 tree s = TREE_VALUE (specifier);
12108
12109 if (TREE_CODE (s) == IDENTIFIER_NODE
12110 && IDENTIFIER_GLOBAL_VALUE (s))
12111 type = IDENTIFIER_GLOBAL_VALUE (s);
12112 if (TREE_CODE (s) == TYPE_DECL)
12113 s = TREE_TYPE (s);
12114 if (TYPE_P (s))
12115 {
12116 type = s;
12117 break;
12118 }
12119 }
12120 }
12121 if (!type)
12122 error ("friend declaration does not name a class or "
12123 "function");
12124 else
12125 make_friend_class (current_class_type, type);
12126 }
12127 /* If there is no TYPE, an error message will already have
12128 been issued. */
12129 else if (!type)
12130 ;
12131 /* An anonymous aggregate has to be handled specially; such
12132 a declaration really declares a data member (with a
12133 particular type), as opposed to a nested class. */
12134 else if (ANON_AGGR_TYPE_P (type))
12135 {
12136 /* Remove constructors and such from TYPE, now that we
12137 know it is an anoymous aggregate. */
12138 fixup_anonymous_aggr (type);
12139 /* And make the corresponding data member. */
12140 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12141 /* Add it to the class. */
12142 finish_member_declaration (decl);
12143 }
12144 }
12145 }
12146 else
12147 {
12148 /* See if these declarations will be friends. */
12149 friend_p = cp_parser_friend_p (decl_specifiers);
12150
12151 /* Keep going until we hit the `;' at the end of the
12152 declaration. */
12153 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12154 {
12155 tree attributes = NULL_TREE;
12156 tree first_attribute;
12157
12158 /* Peek at the next token. */
12159 token = cp_lexer_peek_token (parser->lexer);
12160
12161 /* Check for a bitfield declaration. */
12162 if (token->type == CPP_COLON
12163 || (token->type == CPP_NAME
12164 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12165 == CPP_COLON))
12166 {
12167 tree identifier;
12168 tree width;
12169
12170 /* Get the name of the bitfield. Note that we cannot just
12171 check TOKEN here because it may have been invalidated by
12172 the call to cp_lexer_peek_nth_token above. */
12173 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12174 identifier = cp_parser_identifier (parser);
12175 else
12176 identifier = NULL_TREE;
12177
12178 /* Consume the `:' token. */
12179 cp_lexer_consume_token (parser->lexer);
12180 /* Get the width of the bitfield. */
12181 width = cp_parser_constant_expression (parser);
12182
12183 /* Look for attributes that apply to the bitfield. */
12184 attributes = cp_parser_attributes_opt (parser);
12185 /* Remember which attributes are prefix attributes and
12186 which are not. */
12187 first_attribute = attributes;
12188 /* Combine the attributes. */
12189 attributes = chainon (prefix_attributes, attributes);
12190
12191 /* Create the bitfield declaration. */
12192 decl = grokbitfield (identifier,
12193 decl_specifiers,
12194 width);
12195 /* Apply the attributes. */
12196 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12197 }
12198 else
12199 {
12200 tree declarator;
12201 tree initializer;
12202 tree asm_specification;
12203 bool ctor_dtor_or_conv_p;
12204
12205 /* Parse the declarator. */
12206 declarator
62b8a44e 12207 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
12208 &ctor_dtor_or_conv_p);
12209
12210 /* If something went wrong parsing the declarator, make sure
12211 that we at least consume some tokens. */
12212 if (declarator == error_mark_node)
12213 {
12214 /* Skip to the end of the statement. */
12215 cp_parser_skip_to_end_of_statement (parser);
12216 break;
12217 }
12218
12219 /* Look for an asm-specification. */
12220 asm_specification = cp_parser_asm_specification_opt (parser);
12221 /* Look for attributes that apply to the declaration. */
12222 attributes = cp_parser_attributes_opt (parser);
12223 /* Remember which attributes are prefix attributes and
12224 which are not. */
12225 first_attribute = attributes;
12226 /* Combine the attributes. */
12227 attributes = chainon (prefix_attributes, attributes);
12228
12229 /* If it's an `=', then we have a constant-initializer or a
12230 pure-specifier. It is not correct to parse the
12231 initializer before registering the member declaration
12232 since the member declaration should be in scope while
12233 its initializer is processed. However, the rest of the
12234 front end does not yet provide an interface that allows
12235 us to handle this correctly. */
12236 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12237 {
12238 /* In [class.mem]:
12239
12240 A pure-specifier shall be used only in the declaration of
12241 a virtual function.
12242
12243 A member-declarator can contain a constant-initializer
12244 only if it declares a static member of integral or
12245 enumeration type.
12246
12247 Therefore, if the DECLARATOR is for a function, we look
12248 for a pure-specifier; otherwise, we look for a
12249 constant-initializer. When we call `grokfield', it will
12250 perform more stringent semantics checks. */
12251 if (TREE_CODE (declarator) == CALL_EXPR)
12252 initializer = cp_parser_pure_specifier (parser);
12253 else
12254 {
12255 /* This declaration cannot be a function
12256 definition. */
12257 cp_parser_commit_to_tentative_parse (parser);
12258 /* Parse the initializer. */
12259 initializer = cp_parser_constant_initializer (parser);
12260 }
12261 }
12262 /* Otherwise, there is no initializer. */
12263 else
12264 initializer = NULL_TREE;
12265
12266 /* See if we are probably looking at a function
12267 definition. We are certainly not looking at at a
12268 member-declarator. Calling `grokfield' has
12269 side-effects, so we must not do it unless we are sure
12270 that we are looking at a member-declarator. */
12271 if (cp_parser_token_starts_function_definition_p
12272 (cp_lexer_peek_token (parser->lexer)))
12273 decl = error_mark_node;
12274 else
12275 /* Create the declaration. */
12276 decl = grokfield (declarator,
12277 decl_specifiers,
12278 initializer,
12279 asm_specification,
12280 attributes);
12281 }
12282
12283 /* Reset PREFIX_ATTRIBUTES. */
12284 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12285 attributes = TREE_CHAIN (attributes);
12286 if (attributes)
12287 TREE_CHAIN (attributes) = NULL_TREE;
12288
12289 /* If there is any qualification still in effect, clear it
12290 now; we will be starting fresh with the next declarator. */
12291 parser->scope = NULL_TREE;
12292 parser->qualifying_scope = NULL_TREE;
12293 parser->object_scope = NULL_TREE;
12294 /* If it's a `,', then there are more declarators. */
12295 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12296 cp_lexer_consume_token (parser->lexer);
12297 /* If the next token isn't a `;', then we have a parse error. */
12298 else if (cp_lexer_next_token_is_not (parser->lexer,
12299 CPP_SEMICOLON))
12300 {
12301 cp_parser_error (parser, "expected `;'");
12302 /* Skip tokens until we find a `;' */
12303 cp_parser_skip_to_end_of_statement (parser);
12304
12305 break;
12306 }
12307
12308 if (decl)
12309 {
12310 /* Add DECL to the list of members. */
12311 if (!friend_p)
12312 finish_member_declaration (decl);
12313
12314 /* If DECL is a function, we must return
12315 to parse it later. (Even though there is no definition,
12316 there might be default arguments that need handling.) */
12317 if (TREE_CODE (decl) == FUNCTION_DECL)
12318 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 12319 = tree_cons (NULL_TREE, decl,
a723baf1
MM
12320 TREE_VALUE (parser->unparsed_functions_queues));
12321 }
12322 }
12323 }
12324
12325 /* If everything went well, look for the `;'. */
12326 if (cp_parser_parse_definitely (parser))
12327 {
12328 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12329 return;
12330 }
12331
12332 /* Parse the function-definition. */
12333 decl = cp_parser_function_definition (parser, &friend_p);
12334 /* If the member was not a friend, declare it here. */
12335 if (!friend_p)
12336 finish_member_declaration (decl);
12337 /* Peek at the next token. */
12338 token = cp_lexer_peek_token (parser->lexer);
12339 /* If the next token is a semicolon, consume it. */
12340 if (token->type == CPP_SEMICOLON)
12341 cp_lexer_consume_token (parser->lexer);
12342}
12343
12344/* Parse a pure-specifier.
12345
12346 pure-specifier:
12347 = 0
12348
12349 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12350 Otherwiser, ERROR_MARK_NODE is returned. */
12351
12352static tree
12353cp_parser_pure_specifier (parser)
12354 cp_parser *parser;
12355{
12356 cp_token *token;
12357
12358 /* Look for the `=' token. */
12359 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12360 return error_mark_node;
12361 /* Look for the `0' token. */
12362 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12363 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12364 to get information from the lexer about how the number was
12365 spelled in order to fix this problem. */
12366 if (!token || !integer_zerop (token->value))
12367 return error_mark_node;
12368
12369 return integer_zero_node;
12370}
12371
12372/* Parse a constant-initializer.
12373
12374 constant-initializer:
12375 = constant-expression
12376
12377 Returns a representation of the constant-expression. */
12378
12379static tree
12380cp_parser_constant_initializer (parser)
12381 cp_parser *parser;
12382{
12383 /* Look for the `=' token. */
12384 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12385 return error_mark_node;
12386
12387 /* It is invalid to write:
12388
12389 struct S { static const int i = { 7 }; };
12390
12391 */
12392 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12393 {
12394 cp_parser_error (parser,
12395 "a brace-enclosed initializer is not allowed here");
12396 /* Consume the opening brace. */
12397 cp_lexer_consume_token (parser->lexer);
12398 /* Skip the initializer. */
12399 cp_parser_skip_to_closing_brace (parser);
12400 /* Look for the trailing `}'. */
12401 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12402
12403 return error_mark_node;
12404 }
12405
12406 return cp_parser_constant_expression (parser);
12407}
12408
12409/* Derived classes [gram.class.derived] */
12410
12411/* Parse a base-clause.
12412
12413 base-clause:
12414 : base-specifier-list
12415
12416 base-specifier-list:
12417 base-specifier
12418 base-specifier-list , base-specifier
12419
12420 Returns a TREE_LIST representing the base-classes, in the order in
12421 which they were declared. The representation of each node is as
12422 described by cp_parser_base_specifier.
12423
12424 In the case that no bases are specified, this function will return
12425 NULL_TREE, not ERROR_MARK_NODE. */
12426
12427static tree
12428cp_parser_base_clause (parser)
12429 cp_parser *parser;
12430{
12431 tree bases = NULL_TREE;
12432
12433 /* Look for the `:' that begins the list. */
12434 cp_parser_require (parser, CPP_COLON, "`:'");
12435
12436 /* Scan the base-specifier-list. */
12437 while (true)
12438 {
12439 cp_token *token;
12440 tree base;
12441
12442 /* Look for the base-specifier. */
12443 base = cp_parser_base_specifier (parser);
12444 /* Add BASE to the front of the list. */
12445 if (base != error_mark_node)
12446 {
12447 TREE_CHAIN (base) = bases;
12448 bases = base;
12449 }
12450 /* Peek at the next token. */
12451 token = cp_lexer_peek_token (parser->lexer);
12452 /* If it's not a comma, then the list is complete. */
12453 if (token->type != CPP_COMMA)
12454 break;
12455 /* Consume the `,'. */
12456 cp_lexer_consume_token (parser->lexer);
12457 }
12458
12459 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12460 base class had a qualified name. However, the next name that
12461 appears is certainly not qualified. */
12462 parser->scope = NULL_TREE;
12463 parser->qualifying_scope = NULL_TREE;
12464 parser->object_scope = NULL_TREE;
12465
12466 return nreverse (bases);
12467}
12468
12469/* Parse a base-specifier.
12470
12471 base-specifier:
12472 :: [opt] nested-name-specifier [opt] class-name
12473 virtual access-specifier [opt] :: [opt] nested-name-specifier
12474 [opt] class-name
12475 access-specifier virtual [opt] :: [opt] nested-name-specifier
12476 [opt] class-name
12477
12478 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12479 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12480 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12481 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12482
12483static tree
12484cp_parser_base_specifier (parser)
12485 cp_parser *parser;
12486{
12487 cp_token *token;
12488 bool done = false;
12489 bool virtual_p = false;
12490 bool duplicate_virtual_error_issued_p = false;
12491 bool duplicate_access_error_issued_p = false;
12492 bool class_scope_p;
12493 access_kind access = ak_none;
12494 tree access_node;
12495 tree type;
12496
12497 /* Process the optional `virtual' and `access-specifier'. */
12498 while (!done)
12499 {
12500 /* Peek at the next token. */
12501 token = cp_lexer_peek_token (parser->lexer);
12502 /* Process `virtual'. */
12503 switch (token->keyword)
12504 {
12505 case RID_VIRTUAL:
12506 /* If `virtual' appears more than once, issue an error. */
12507 if (virtual_p && !duplicate_virtual_error_issued_p)
12508 {
12509 cp_parser_error (parser,
12510 "`virtual' specified more than once in base-specified");
12511 duplicate_virtual_error_issued_p = true;
12512 }
12513
12514 virtual_p = true;
12515
12516 /* Consume the `virtual' token. */
12517 cp_lexer_consume_token (parser->lexer);
12518
12519 break;
12520
12521 case RID_PUBLIC:
12522 case RID_PROTECTED:
12523 case RID_PRIVATE:
12524 /* If more than one access specifier appears, issue an
12525 error. */
12526 if (access != ak_none && !duplicate_access_error_issued_p)
12527 {
12528 cp_parser_error (parser,
12529 "more than one access specifier in base-specified");
12530 duplicate_access_error_issued_p = true;
12531 }
12532
12533 access = ((access_kind)
12534 tree_low_cst (ridpointers[(int) token->keyword],
12535 /*pos=*/1));
12536
12537 /* Consume the access-specifier. */
12538 cp_lexer_consume_token (parser->lexer);
12539
12540 break;
12541
12542 default:
12543 done = true;
12544 break;
12545 }
12546 }
12547
12548 /* Map `virtual_p' and `access' onto one of the access
12549 tree-nodes. */
12550 if (!virtual_p)
12551 switch (access)
12552 {
12553 case ak_none:
12554 access_node = access_default_node;
12555 break;
12556 case ak_public:
12557 access_node = access_public_node;
12558 break;
12559 case ak_protected:
12560 access_node = access_protected_node;
12561 break;
12562 case ak_private:
12563 access_node = access_private_node;
12564 break;
12565 default:
12566 abort ();
12567 }
12568 else
12569 switch (access)
12570 {
12571 case ak_none:
12572 access_node = access_default_virtual_node;
12573 break;
12574 case ak_public:
12575 access_node = access_public_virtual_node;
12576 break;
12577 case ak_protected:
12578 access_node = access_protected_virtual_node;
12579 break;
12580 case ak_private:
12581 access_node = access_private_virtual_node;
12582 break;
12583 default:
12584 abort ();
12585 }
12586
12587 /* Look for the optional `::' operator. */
12588 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12589 /* Look for the nested-name-specifier. The simplest way to
12590 implement:
12591
12592 [temp.res]
12593
12594 The keyword `typename' is not permitted in a base-specifier or
12595 mem-initializer; in these contexts a qualified name that
12596 depends on a template-parameter is implicitly assumed to be a
12597 type name.
12598
12599 is to pretend that we have seen the `typename' keyword at this
12600 point. */
12601 cp_parser_nested_name_specifier_opt (parser,
12602 /*typename_keyword_p=*/true,
12603 /*check_dependency_p=*/true,
12604 /*type_p=*/true);
12605 /* If the base class is given by a qualified name, assume that names
12606 we see are type names or templates, as appropriate. */
12607 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12608 /* Finally, look for the class-name. */
12609 type = cp_parser_class_name (parser,
12610 class_scope_p,
12611 class_scope_p,
12612 /*type_p=*/true,
12613 /*check_access=*/true,
12614 /*check_dependency_p=*/true,
12615 /*class_head_p=*/false);
12616
12617 if (type == error_mark_node)
12618 return error_mark_node;
12619
12620 return finish_base_specifier (access_node, TREE_TYPE (type));
12621}
12622
12623/* Exception handling [gram.exception] */
12624
12625/* Parse an (optional) exception-specification.
12626
12627 exception-specification:
12628 throw ( type-id-list [opt] )
12629
12630 Returns a TREE_LIST representing the exception-specification. The
12631 TREE_VALUE of each node is a type. */
12632
12633static tree
12634cp_parser_exception_specification_opt (parser)
12635 cp_parser *parser;
12636{
12637 cp_token *token;
12638 tree type_id_list;
12639
12640 /* Peek at the next token. */
12641 token = cp_lexer_peek_token (parser->lexer);
12642 /* If it's not `throw', then there's no exception-specification. */
12643 if (!cp_parser_is_keyword (token, RID_THROW))
12644 return NULL_TREE;
12645
12646 /* Consume the `throw'. */
12647 cp_lexer_consume_token (parser->lexer);
12648
12649 /* Look for the `('. */
12650 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12651
12652 /* Peek at the next token. */
12653 token = cp_lexer_peek_token (parser->lexer);
12654 /* If it's not a `)', then there is a type-id-list. */
12655 if (token->type != CPP_CLOSE_PAREN)
12656 {
12657 const char *saved_message;
12658
12659 /* Types may not be defined in an exception-specification. */
12660 saved_message = parser->type_definition_forbidden_message;
12661 parser->type_definition_forbidden_message
12662 = "types may not be defined in an exception-specification";
12663 /* Parse the type-id-list. */
12664 type_id_list = cp_parser_type_id_list (parser);
12665 /* Restore the saved message. */
12666 parser->type_definition_forbidden_message = saved_message;
12667 }
12668 else
12669 type_id_list = empty_except_spec;
12670
12671 /* Look for the `)'. */
12672 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12673
12674 return type_id_list;
12675}
12676
12677/* Parse an (optional) type-id-list.
12678
12679 type-id-list:
12680 type-id
12681 type-id-list , type-id
12682
12683 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12684 in the order that the types were presented. */
12685
12686static tree
12687cp_parser_type_id_list (parser)
12688 cp_parser *parser;
12689{
12690 tree types = NULL_TREE;
12691
12692 while (true)
12693 {
12694 cp_token *token;
12695 tree type;
12696
12697 /* Get the next type-id. */
12698 type = cp_parser_type_id (parser);
12699 /* Add it to the list. */
12700 types = add_exception_specifier (types, type, /*complain=*/1);
12701 /* Peek at the next token. */
12702 token = cp_lexer_peek_token (parser->lexer);
12703 /* If it is not a `,', we are done. */
12704 if (token->type != CPP_COMMA)
12705 break;
12706 /* Consume the `,'. */
12707 cp_lexer_consume_token (parser->lexer);
12708 }
12709
12710 return nreverse (types);
12711}
12712
12713/* Parse a try-block.
12714
12715 try-block:
12716 try compound-statement handler-seq */
12717
12718static tree
12719cp_parser_try_block (parser)
12720 cp_parser *parser;
12721{
12722 tree try_block;
12723
12724 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12725 try_block = begin_try_block ();
12726 cp_parser_compound_statement (parser);
12727 finish_try_block (try_block);
12728 cp_parser_handler_seq (parser);
12729 finish_handler_sequence (try_block);
12730
12731 return try_block;
12732}
12733
12734/* Parse a function-try-block.
12735
12736 function-try-block:
12737 try ctor-initializer [opt] function-body handler-seq */
12738
12739static bool
12740cp_parser_function_try_block (parser)
12741 cp_parser *parser;
12742{
12743 tree try_block;
12744 bool ctor_initializer_p;
12745
12746 /* Look for the `try' keyword. */
12747 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12748 return false;
12749 /* Let the rest of the front-end know where we are. */
12750 try_block = begin_function_try_block ();
12751 /* Parse the function-body. */
12752 ctor_initializer_p
12753 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12754 /* We're done with the `try' part. */
12755 finish_function_try_block (try_block);
12756 /* Parse the handlers. */
12757 cp_parser_handler_seq (parser);
12758 /* We're done with the handlers. */
12759 finish_function_handler_sequence (try_block);
12760
12761 return ctor_initializer_p;
12762}
12763
12764/* Parse a handler-seq.
12765
12766 handler-seq:
12767 handler handler-seq [opt] */
12768
12769static void
12770cp_parser_handler_seq (parser)
12771 cp_parser *parser;
12772{
12773 while (true)
12774 {
12775 cp_token *token;
12776
12777 /* Parse the handler. */
12778 cp_parser_handler (parser);
12779 /* Peek at the next token. */
12780 token = cp_lexer_peek_token (parser->lexer);
12781 /* If it's not `catch' then there are no more handlers. */
12782 if (!cp_parser_is_keyword (token, RID_CATCH))
12783 break;
12784 }
12785}
12786
12787/* Parse a handler.
12788
12789 handler:
12790 catch ( exception-declaration ) compound-statement */
12791
12792static void
12793cp_parser_handler (parser)
12794 cp_parser *parser;
12795{
12796 tree handler;
12797 tree declaration;
12798
12799 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12800 handler = begin_handler ();
12801 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12802 declaration = cp_parser_exception_declaration (parser);
12803 finish_handler_parms (declaration, handler);
12804 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12805 cp_parser_compound_statement (parser);
12806 finish_handler (handler);
12807}
12808
12809/* Parse an exception-declaration.
12810
12811 exception-declaration:
12812 type-specifier-seq declarator
12813 type-specifier-seq abstract-declarator
12814 type-specifier-seq
12815 ...
12816
12817 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12818 ellipsis variant is used. */
12819
12820static tree
12821cp_parser_exception_declaration (parser)
12822 cp_parser *parser;
12823{
12824 tree type_specifiers;
12825 tree declarator;
12826 const char *saved_message;
12827
12828 /* If it's an ellipsis, it's easy to handle. */
12829 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12830 {
12831 /* Consume the `...' token. */
12832 cp_lexer_consume_token (parser->lexer);
12833 return NULL_TREE;
12834 }
12835
12836 /* Types may not be defined in exception-declarations. */
12837 saved_message = parser->type_definition_forbidden_message;
12838 parser->type_definition_forbidden_message
12839 = "types may not be defined in exception-declarations";
12840
12841 /* Parse the type-specifier-seq. */
12842 type_specifiers = cp_parser_type_specifier_seq (parser);
12843 /* If it's a `)', then there is no declarator. */
12844 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12845 declarator = NULL_TREE;
12846 else
62b8a44e
NS
12847 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12848 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1
MM
12849
12850 /* Restore the saved message. */
12851 parser->type_definition_forbidden_message = saved_message;
12852
12853 return start_handler_parms (type_specifiers, declarator);
12854}
12855
12856/* Parse a throw-expression.
12857
12858 throw-expression:
12859 throw assignment-expresion [opt]
12860
12861 Returns a THROW_EXPR representing the throw-expression. */
12862
12863static tree
12864cp_parser_throw_expression (parser)
12865 cp_parser *parser;
12866{
12867 tree expression;
12868
12869 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12870 /* We can't be sure if there is an assignment-expression or not. */
12871 cp_parser_parse_tentatively (parser);
12872 /* Try it. */
12873 expression = cp_parser_assignment_expression (parser);
12874 /* If it didn't work, this is just a rethrow. */
12875 if (!cp_parser_parse_definitely (parser))
12876 expression = NULL_TREE;
12877
12878 return build_throw (expression);
12879}
12880
12881/* GNU Extensions */
12882
12883/* Parse an (optional) asm-specification.
12884
12885 asm-specification:
12886 asm ( string-literal )
12887
12888 If the asm-specification is present, returns a STRING_CST
12889 corresponding to the string-literal. Otherwise, returns
12890 NULL_TREE. */
12891
12892static tree
12893cp_parser_asm_specification_opt (parser)
12894 cp_parser *parser;
12895{
12896 cp_token *token;
12897 tree asm_specification;
12898
12899 /* Peek at the next token. */
12900 token = cp_lexer_peek_token (parser->lexer);
12901 /* If the next token isn't the `asm' keyword, then there's no
12902 asm-specification. */
12903 if (!cp_parser_is_keyword (token, RID_ASM))
12904 return NULL_TREE;
12905
12906 /* Consume the `asm' token. */
12907 cp_lexer_consume_token (parser->lexer);
12908 /* Look for the `('. */
12909 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12910
12911 /* Look for the string-literal. */
12912 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12913 if (token)
12914 asm_specification = token->value;
12915 else
12916 asm_specification = NULL_TREE;
12917
12918 /* Look for the `)'. */
12919 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12920
12921 return asm_specification;
12922}
12923
12924/* Parse an asm-operand-list.
12925
12926 asm-operand-list:
12927 asm-operand
12928 asm-operand-list , asm-operand
12929
12930 asm-operand:
12931 string-literal ( expression )
12932 [ string-literal ] string-literal ( expression )
12933
12934 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12935 each node is the expression. The TREE_PURPOSE is itself a
12936 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12937 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12938 is a STRING_CST for the string literal before the parenthesis. */
12939
12940static tree
12941cp_parser_asm_operand_list (parser)
12942 cp_parser *parser;
12943{
12944 tree asm_operands = NULL_TREE;
12945
12946 while (true)
12947 {
12948 tree string_literal;
12949 tree expression;
12950 tree name;
12951 cp_token *token;
12952
12953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12954 {
12955 /* Consume the `[' token. */
12956 cp_lexer_consume_token (parser->lexer);
12957 /* Read the operand name. */
12958 name = cp_parser_identifier (parser);
12959 if (name != error_mark_node)
12960 name = build_string (IDENTIFIER_LENGTH (name),
12961 IDENTIFIER_POINTER (name));
12962 /* Look for the closing `]'. */
12963 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12964 }
12965 else
12966 name = NULL_TREE;
12967 /* Look for the string-literal. */
12968 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12969 string_literal = token ? token->value : error_mark_node;
12970 /* Look for the `('. */
12971 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12972 /* Parse the expression. */
12973 expression = cp_parser_expression (parser);
12974 /* Look for the `)'. */
12975 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12976 /* Add this operand to the list. */
12977 asm_operands = tree_cons (build_tree_list (name, string_literal),
12978 expression,
12979 asm_operands);
12980 /* If the next token is not a `,', there are no more
12981 operands. */
12982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12983 break;
12984 /* Consume the `,'. */
12985 cp_lexer_consume_token (parser->lexer);
12986 }
12987
12988 return nreverse (asm_operands);
12989}
12990
12991/* Parse an asm-clobber-list.
12992
12993 asm-clobber-list:
12994 string-literal
12995 asm-clobber-list , string-literal
12996
12997 Returns a TREE_LIST, indicating the clobbers in the order that they
12998 appeared. The TREE_VALUE of each node is a STRING_CST. */
12999
13000static tree
13001cp_parser_asm_clobber_list (parser)
13002 cp_parser *parser;
13003{
13004 tree clobbers = NULL_TREE;
13005
13006 while (true)
13007 {
13008 cp_token *token;
13009 tree string_literal;
13010
13011 /* Look for the string literal. */
13012 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13013 string_literal = token ? token->value : error_mark_node;
13014 /* Add it to the list. */
13015 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13016 /* If the next token is not a `,', then the list is
13017 complete. */
13018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13019 break;
13020 /* Consume the `,' token. */
13021 cp_lexer_consume_token (parser->lexer);
13022 }
13023
13024 return clobbers;
13025}
13026
13027/* Parse an (optional) series of attributes.
13028
13029 attributes:
13030 attributes attribute
13031
13032 attribute:
13033 __attribute__ (( attribute-list [opt] ))
13034
13035 The return value is as for cp_parser_attribute_list. */
13036
13037static tree
13038cp_parser_attributes_opt (parser)
13039 cp_parser *parser;
13040{
13041 tree attributes = NULL_TREE;
13042
13043 while (true)
13044 {
13045 cp_token *token;
13046 tree attribute_list;
13047
13048 /* Peek at the next token. */
13049 token = cp_lexer_peek_token (parser->lexer);
13050 /* If it's not `__attribute__', then we're done. */
13051 if (token->keyword != RID_ATTRIBUTE)
13052 break;
13053
13054 /* Consume the `__attribute__' keyword. */
13055 cp_lexer_consume_token (parser->lexer);
13056 /* Look for the two `(' tokens. */
13057 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13058 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13059
13060 /* Peek at the next token. */
13061 token = cp_lexer_peek_token (parser->lexer);
13062 if (token->type != CPP_CLOSE_PAREN)
13063 /* Parse the attribute-list. */
13064 attribute_list = cp_parser_attribute_list (parser);
13065 else
13066 /* If the next token is a `)', then there is no attribute
13067 list. */
13068 attribute_list = NULL;
13069
13070 /* Look for the two `)' tokens. */
13071 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13072 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13073
13074 /* Add these new attributes to the list. */
13075 attributes = chainon (attributes, attribute_list);
13076 }
13077
13078 return attributes;
13079}
13080
13081/* Parse an attribute-list.
13082
13083 attribute-list:
13084 attribute
13085 attribute-list , attribute
13086
13087 attribute:
13088 identifier
13089 identifier ( identifier )
13090 identifier ( identifier , expression-list )
13091 identifier ( expression-list )
13092
13093 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13094 TREE_PURPOSE of each node is the identifier indicating which
13095 attribute is in use. The TREE_VALUE represents the arguments, if
13096 any. */
13097
13098static tree
13099cp_parser_attribute_list (parser)
13100 cp_parser *parser;
13101{
13102 tree attribute_list = NULL_TREE;
13103
13104 while (true)
13105 {
13106 cp_token *token;
13107 tree identifier;
13108 tree attribute;
13109
13110 /* Look for the identifier. We also allow keywords here; for
13111 example `__attribute__ ((const))' is legal. */
13112 token = cp_lexer_peek_token (parser->lexer);
13113 if (token->type != CPP_NAME
13114 && token->type != CPP_KEYWORD)
13115 return error_mark_node;
13116 /* Consume the token. */
13117 token = cp_lexer_consume_token (parser->lexer);
13118
13119 /* Save away the identifier that indicates which attribute this is. */
13120 identifier = token->value;
13121 attribute = build_tree_list (identifier, NULL_TREE);
13122
13123 /* Peek at the next token. */
13124 token = cp_lexer_peek_token (parser->lexer);
13125 /* If it's an `(', then parse the attribute arguments. */
13126 if (token->type == CPP_OPEN_PAREN)
13127 {
13128 tree arguments;
13129 int arguments_allowed_p = 1;
13130
13131 /* Consume the `('. */
13132 cp_lexer_consume_token (parser->lexer);
13133 /* Peek at the next token. */
13134 token = cp_lexer_peek_token (parser->lexer);
13135 /* Check to see if the next token is an identifier. */
13136 if (token->type == CPP_NAME)
13137 {
13138 /* Save the identifier. */
13139 identifier = token->value;
13140 /* Consume the identifier. */
13141 cp_lexer_consume_token (parser->lexer);
13142 /* Peek at the next token. */
13143 token = cp_lexer_peek_token (parser->lexer);
13144 /* If the next token is a `,', then there are some other
13145 expressions as well. */
13146 if (token->type == CPP_COMMA)
13147 /* Consume the comma. */
13148 cp_lexer_consume_token (parser->lexer);
13149 else
13150 arguments_allowed_p = 0;
13151 }
13152 else
13153 identifier = NULL_TREE;
13154
13155 /* If there are arguments, parse them too. */
13156 if (arguments_allowed_p)
13157 arguments = cp_parser_expression_list (parser);
13158 else
13159 arguments = NULL_TREE;
13160
13161 /* Combine the identifier and the arguments. */
13162 if (identifier)
13163 arguments = tree_cons (NULL_TREE, identifier, arguments);
13164
13165 /* Save the identifier and arguments away. */
13166 TREE_VALUE (attribute) = arguments;
13167
13168 /* Look for the closing `)'. */
13169 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13170 }
13171
13172 /* Add this attribute to the list. */
13173 TREE_CHAIN (attribute) = attribute_list;
13174 attribute_list = attribute;
13175
13176 /* Now, look for more attributes. */
13177 token = cp_lexer_peek_token (parser->lexer);
13178 /* If the next token isn't a `,', we're done. */
13179 if (token->type != CPP_COMMA)
13180 break;
13181
13182 /* Consume the commma and keep going. */
13183 cp_lexer_consume_token (parser->lexer);
13184 }
13185
13186 /* We built up the list in reverse order. */
13187 return nreverse (attribute_list);
13188}
13189
13190/* Parse an optional `__extension__' keyword. Returns TRUE if it is
13191 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13192 current value of the PEDANTIC flag, regardless of whether or not
13193 the `__extension__' keyword is present. The caller is responsible
13194 for restoring the value of the PEDANTIC flag. */
13195
13196static bool
13197cp_parser_extension_opt (parser, saved_pedantic)
13198 cp_parser *parser;
13199 int *saved_pedantic;
13200{
13201 /* Save the old value of the PEDANTIC flag. */
13202 *saved_pedantic = pedantic;
13203
13204 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13205 {
13206 /* Consume the `__extension__' token. */
13207 cp_lexer_consume_token (parser->lexer);
13208 /* We're not being pedantic while the `__extension__' keyword is
13209 in effect. */
13210 pedantic = 0;
13211
13212 return true;
13213 }
13214
13215 return false;
13216}
13217
13218/* Parse a label declaration.
13219
13220 label-declaration:
13221 __label__ label-declarator-seq ;
13222
13223 label-declarator-seq:
13224 identifier , label-declarator-seq
13225 identifier */
13226
13227static void
13228cp_parser_label_declaration (parser)
13229 cp_parser *parser;
13230{
13231 /* Look for the `__label__' keyword. */
13232 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13233
13234 while (true)
13235 {
13236 tree identifier;
13237
13238 /* Look for an identifier. */
13239 identifier = cp_parser_identifier (parser);
13240 /* Declare it as a lobel. */
13241 finish_label_decl (identifier);
13242 /* If the next token is a `;', stop. */
13243 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13244 break;
13245 /* Look for the `,' separating the label declarations. */
13246 cp_parser_require (parser, CPP_COMMA, "`,'");
13247 }
13248
13249 /* Look for the final `;'. */
13250 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13251}
13252
13253/* Support Functions */
13254
13255/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13256 NAME should have one of the representations used for an
13257 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13258 is returned. If PARSER->SCOPE is a dependent type, then a
13259 SCOPE_REF is returned.
13260
13261 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13262 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13263 was formed. Abstractly, such entities should not be passed to this
13264 function, because they do not need to be looked up, but it is
13265 simpler to check for this special case here, rather than at the
13266 call-sites.
13267
13268 In cases not explicitly covered above, this function returns a
13269 DECL, OVERLOAD, or baselink representing the result of the lookup.
13270 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13271 is returned.
13272
13273 If CHECK_ACCESS is TRUE, then access control is performed on the
13274 declaration to which the name resolves, and an error message is
13275 issued if the declaration is inaccessible.
13276
13277 If IS_TYPE is TRUE, bindings that do not refer to types are
13278 ignored.
13279
eea9800f
MM
13280 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13281 are ignored.
13282
a723baf1
MM
13283 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13284 types. */
13285
13286static tree
eea9800f
MM
13287cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13288 bool is_type, bool is_namespace, bool check_dependency)
a723baf1
MM
13289{
13290 tree decl;
13291 tree object_type = parser->context->object_type;
13292
13293 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13294 no longer valid. Note that if we are parsing tentatively, and
13295 the parse fails, OBJECT_TYPE will be automatically restored. */
13296 parser->context->object_type = NULL_TREE;
13297
13298 if (name == error_mark_node)
13299 return error_mark_node;
13300
13301 /* A template-id has already been resolved; there is no lookup to
13302 do. */
13303 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13304 return name;
13305 if (BASELINK_P (name))
13306 {
13307 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13308 == TEMPLATE_ID_EXPR),
13309 20020909);
13310 return name;
13311 }
13312
13313 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13314 it should already have been checked to make sure that the name
13315 used matches the type being destroyed. */
13316 if (TREE_CODE (name) == BIT_NOT_EXPR)
13317 {
13318 tree type;
13319
13320 /* Figure out to which type this destructor applies. */
13321 if (parser->scope)
13322 type = parser->scope;
13323 else if (object_type)
13324 type = object_type;
13325 else
13326 type = current_class_type;
13327 /* If that's not a class type, there is no destructor. */
13328 if (!type || !CLASS_TYPE_P (type))
13329 return error_mark_node;
13330 /* If it was a class type, return the destructor. */
13331 return CLASSTYPE_DESTRUCTORS (type);
13332 }
13333
13334 /* By this point, the NAME should be an ordinary identifier. If
13335 the id-expression was a qualified name, the qualifying scope is
13336 stored in PARSER->SCOPE at this point. */
13337 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13338 20000619);
13339
13340 /* Perform the lookup. */
13341 if (parser->scope)
13342 {
13343 bool dependent_type_p;
13344
13345 if (parser->scope == error_mark_node)
13346 return error_mark_node;
13347
13348 /* If the SCOPE is dependent, the lookup must be deferred until
13349 the template is instantiated -- unless we are explicitly
13350 looking up names in uninstantiated templates. Even then, we
13351 cannot look up the name if the scope is not a class type; it
13352 might, for example, be a template type parameter. */
13353 dependent_type_p = (TYPE_P (parser->scope)
13354 && !(parser->in_declarator_p
13355 && currently_open_class (parser->scope))
13356 && cp_parser_dependent_type_p (parser->scope));
13357 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13358 && dependent_type_p)
13359 {
13360 if (!is_type)
13361 decl = build_nt (SCOPE_REF, parser->scope, name);
13362 else
13363 /* The resolution to Core Issue 180 says that `struct A::B'
13364 should be considered a type-name, even if `A' is
13365 dependent. */
13366 decl = TYPE_NAME (make_typename_type (parser->scope,
13367 name,
13368 /*complain=*/1));
13369 }
13370 else
13371 {
13372 /* If PARSER->SCOPE is a dependent type, then it must be a
13373 class type, and we must not be checking dependencies;
13374 otherwise, we would have processed this lookup above. So
13375 that PARSER->SCOPE is not considered a dependent base by
13376 lookup_member, we must enter the scope here. */
13377 if (dependent_type_p)
13378 push_scope (parser->scope);
13379 /* If the PARSER->SCOPE is a a template specialization, it
13380 may be instantiated during name lookup. In that case,
13381 errors may be issued. Even if we rollback the current
13382 tentative parse, those errors are valid. */
13383 decl = lookup_qualified_name (parser->scope, name, is_type,
13384 /*flags=*/0);
13385 if (dependent_type_p)
13386 pop_scope (parser->scope);
13387 }
13388 parser->qualifying_scope = parser->scope;
13389 parser->object_scope = NULL_TREE;
13390 }
13391 else if (object_type)
13392 {
13393 tree object_decl = NULL_TREE;
13394 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13395 OBJECT_TYPE is not a class. */
13396 if (CLASS_TYPE_P (object_type))
13397 /* If the OBJECT_TYPE is a template specialization, it may
13398 be instantiated during name lookup. In that case, errors
13399 may be issued. Even if we rollback the current tentative
13400 parse, those errors are valid. */
13401 object_decl = lookup_member (object_type,
13402 name,
13403 /*protect=*/0, is_type);
13404 /* Look it up in the enclosing context, too. */
13405 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13406 is_namespace,
a723baf1
MM
13407 /*flags=*/0);
13408 parser->object_scope = object_type;
13409 parser->qualifying_scope = NULL_TREE;
13410 if (object_decl)
13411 decl = object_decl;
13412 }
13413 else
13414 {
13415 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13416 is_namespace,
a723baf1
MM
13417 /*flags=*/0);
13418 parser->qualifying_scope = NULL_TREE;
13419 parser->object_scope = NULL_TREE;
13420 }
13421
13422 /* If the lookup failed, let our caller know. */
13423 if (!decl
13424 || decl == error_mark_node
13425 || (TREE_CODE (decl) == FUNCTION_DECL
13426 && DECL_ANTICIPATED (decl)))
13427 return error_mark_node;
13428
13429 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13430 if (TREE_CODE (decl) == TREE_LIST)
13431 {
13432 /* The error message we have to print is too complicated for
13433 cp_parser_error, so we incorporate its actions directly. */
e5976695 13434 if (!cp_parser_simulate_error (parser))
a723baf1
MM
13435 {
13436 error ("reference to `%D' is ambiguous", name);
13437 print_candidates (decl);
13438 }
13439 return error_mark_node;
13440 }
13441
13442 my_friendly_assert (DECL_P (decl)
13443 || TREE_CODE (decl) == OVERLOAD
13444 || TREE_CODE (decl) == SCOPE_REF
13445 || BASELINK_P (decl),
13446 20000619);
13447
13448 /* If we have resolved the name of a member declaration, check to
13449 see if the declaration is accessible. When the name resolves to
13450 set of overloaded functions, accesibility is checked when
13451 overload resolution is done.
13452
13453 During an explicit instantiation, access is not checked at all,
13454 as per [temp.explicit]. */
13455 if (check_access && scope_chain->check_access && DECL_P (decl))
13456 {
13457 tree qualifying_type;
13458
13459 /* Figure out the type through which DECL is being
13460 accessed. */
13461 qualifying_type
13462 = cp_parser_scope_through_which_access_occurs (decl,
13463 object_type,
13464 parser->scope);
13465 if (qualifying_type)
cf22909c 13466 perform_or_defer_access_check (qualifying_type, decl);
a723baf1
MM
13467 }
13468
13469 return decl;
13470}
13471
13472/* Like cp_parser_lookup_name, but for use in the typical case where
13473 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13474 TRUE. */
13475
13476static tree
13477cp_parser_lookup_name_simple (parser, name)
13478 cp_parser *parser;
13479 tree name;
13480{
13481 return cp_parser_lookup_name (parser, name,
13482 /*check_access=*/true,
eea9800f
MM
13483 /*is_type=*/false,
13484 /*is_namespace=*/false,
a723baf1
MM
13485 /*check_dependency=*/true);
13486}
13487
13488/* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13489 TYPENAME_TYPE corresponds. Note that this function peers inside
13490 uninstantiated templates and therefore should be used only in
13491 extremely limited situations. */
13492
13493static tree
13494cp_parser_resolve_typename_type (parser, type)
13495 cp_parser *parser;
13496 tree type;
13497{
13498 tree scope;
13499 tree name;
13500 tree decl;
13501
13502 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13503 20010702);
13504
13505 scope = TYPE_CONTEXT (type);
13506 name = DECL_NAME (TYPE_NAME (type));
13507
13508 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13509 it first before we can figure out what NAME refers to. */
13510 if (TREE_CODE (scope) == TYPENAME_TYPE)
13511 scope = cp_parser_resolve_typename_type (parser, scope);
13512 /* If we don't know what SCOPE refers to, then we cannot resolve the
13513 TYPENAME_TYPE. */
13514 if (scope == error_mark_node)
13515 return error_mark_node;
13516 /* If the SCOPE is a template type parameter, we have no way of
13517 resolving the name. */
13518 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13519 return type;
13520 /* Enter the SCOPE so that name lookup will be resolved as if we
13521 were in the class definition. In particular, SCOPE will no
13522 longer be considered a dependent type. */
13523 push_scope (scope);
13524 /* Look up the declaration. */
13525 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13526 /* If all went well, we got a TYPE_DECL for a non-typename. */
13527 if (!decl
13528 || TREE_CODE (decl) != TYPE_DECL
13529 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13530 {
13531 cp_parser_error (parser, "could not resolve typename type");
13532 type = error_mark_node;
13533 }
13534 else
13535 type = TREE_TYPE (decl);
13536 /* Leave the SCOPE. */
13537 pop_scope (scope);
13538
13539 return type;
13540}
13541
13542/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13543 the current context, return the TYPE_DECL. If TAG_NAME_P is
13544 true, the DECL indicates the class being defined in a class-head,
13545 or declared in an elaborated-type-specifier.
13546
13547 Otherwise, return DECL. */
13548
13549static tree
13550cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13551{
13552 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13553 the scope of the class, then treat the TEMPLATE_DECL as a
13554 class-name. For example, in:
13555
13556 template <class T> struct S {
13557 S s;
13558 };
13559
13560 is OK.
13561
13562 If the TEMPLATE_DECL is being declared as part of a class-head,
13563 the same translation occurs:
13564
13565 struct A {
13566 template <typename T> struct B;
13567 };
13568
13569 template <typename T> struct A::B {};
13570
13571 Similarly, in a elaborated-type-specifier:
13572
13573 namespace N { struct X{}; }
13574
13575 struct A {
13576 template <typename T> friend struct N::X;
13577 };
13578
13579 */
13580 if (DECL_CLASS_TEMPLATE_P (decl)
13581 && (tag_name_p
13582 || (current_class_type
13583 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13584 current_class_type))))
13585 return DECL_TEMPLATE_RESULT (decl);
13586
13587 return decl;
13588}
13589
13590/* If too many, or too few, template-parameter lists apply to the
13591 declarator, issue an error message. Returns TRUE if all went well,
13592 and FALSE otherwise. */
13593
13594static bool
13595cp_parser_check_declarator_template_parameters (parser, declarator)
13596 cp_parser *parser;
13597 tree declarator;
13598{
13599 unsigned num_templates;
13600
13601 /* We haven't seen any classes that involve template parameters yet. */
13602 num_templates = 0;
13603
13604 switch (TREE_CODE (declarator))
13605 {
13606 case CALL_EXPR:
13607 case ARRAY_REF:
13608 case INDIRECT_REF:
13609 case ADDR_EXPR:
13610 {
13611 tree main_declarator = TREE_OPERAND (declarator, 0);
13612 return
13613 cp_parser_check_declarator_template_parameters (parser,
13614 main_declarator);
13615 }
13616
13617 case SCOPE_REF:
13618 {
13619 tree scope;
13620 tree member;
13621
13622 scope = TREE_OPERAND (declarator, 0);
13623 member = TREE_OPERAND (declarator, 1);
13624
13625 /* If this is a pointer-to-member, then we are not interested
13626 in the SCOPE, because it does not qualify the thing that is
13627 being declared. */
13628 if (TREE_CODE (member) == INDIRECT_REF)
13629 return (cp_parser_check_declarator_template_parameters
13630 (parser, member));
13631
13632 while (scope && CLASS_TYPE_P (scope))
13633 {
13634 /* You're supposed to have one `template <...>'
13635 for every template class, but you don't need one
13636 for a full specialization. For example:
13637
13638 template <class T> struct S{};
13639 template <> struct S<int> { void f(); };
13640 void S<int>::f () {}
13641
13642 is correct; there shouldn't be a `template <>' for
13643 the definition of `S<int>::f'. */
13644 if (CLASSTYPE_TEMPLATE_INFO (scope)
13645 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13646 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13647 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13648 ++num_templates;
13649
13650 scope = TYPE_CONTEXT (scope);
13651 }
13652 }
13653
13654 /* Fall through. */
13655
13656 default:
13657 /* If the DECLARATOR has the form `X<y>' then it uses one
13658 additional level of template parameters. */
13659 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13660 ++num_templates;
13661
13662 return cp_parser_check_template_parameters (parser,
13663 num_templates);
13664 }
13665}
13666
13667/* NUM_TEMPLATES were used in the current declaration. If that is
13668 invalid, return FALSE and issue an error messages. Otherwise,
13669 return TRUE. */
13670
13671static bool
13672cp_parser_check_template_parameters (parser, num_templates)
13673 cp_parser *parser;
13674 unsigned num_templates;
13675{
13676 /* If there are more template classes than parameter lists, we have
13677 something like:
13678
13679 template <class T> void S<T>::R<T>::f (); */
13680 if (parser->num_template_parameter_lists < num_templates)
13681 {
13682 error ("too few template-parameter-lists");
13683 return false;
13684 }
13685 /* If there are the same number of template classes and parameter
13686 lists, that's OK. */
13687 if (parser->num_template_parameter_lists == num_templates)
13688 return true;
13689 /* If there are more, but only one more, then we are referring to a
13690 member template. That's OK too. */
13691 if (parser->num_template_parameter_lists == num_templates + 1)
13692 return true;
13693 /* Otherwise, there are too many template parameter lists. We have
13694 something like:
13695
13696 template <class T> template <class U> void S::f(); */
13697 error ("too many template-parameter-lists");
13698 return false;
13699}
13700
13701/* Parse a binary-expression of the general form:
13702
13703 binary-expression:
13704 <expr>
13705 binary-expression <token> <expr>
13706
13707 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13708 to parser the <expr>s. If the first production is used, then the
13709 value returned by FN is returned directly. Otherwise, a node with
13710 the indicated EXPR_TYPE is returned, with operands corresponding to
13711 the two sub-expressions. */
13712
13713static tree
13714cp_parser_binary_expression (parser, token_tree_map, fn)
13715 cp_parser *parser;
39b1af70 13716 const cp_parser_token_tree_map token_tree_map;
a723baf1
MM
13717 cp_parser_expression_fn fn;
13718{
13719 tree lhs;
13720
13721 /* Parse the first expression. */
13722 lhs = (*fn) (parser);
13723 /* Now, look for more expressions. */
13724 while (true)
13725 {
13726 cp_token *token;
39b1af70 13727 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13728 tree rhs;
13729
13730 /* Peek at the next token. */
13731 token = cp_lexer_peek_token (parser->lexer);
13732 /* If the token is `>', and that's not an operator at the
13733 moment, then we're done. */
13734 if (token->type == CPP_GREATER
13735 && !parser->greater_than_is_operator_p)
13736 break;
13737 /* If we find one of the tokens we want, build the correspoding
13738 tree representation. */
13739 for (map_node = token_tree_map;
13740 map_node->token_type != CPP_EOF;
13741 ++map_node)
13742 if (map_node->token_type == token->type)
13743 {
13744 /* Consume the operator token. */
13745 cp_lexer_consume_token (parser->lexer);
13746 /* Parse the right-hand side of the expression. */
13747 rhs = (*fn) (parser);
13748 /* Build the binary tree node. */
13749 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13750 break;
13751 }
13752
13753 /* If the token wasn't one of the ones we want, we're done. */
13754 if (map_node->token_type == CPP_EOF)
13755 break;
13756 }
13757
13758 return lhs;
13759}
13760
13761/* Parse an optional `::' token indicating that the following name is
13762 from the global namespace. If so, PARSER->SCOPE is set to the
13763 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13764 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13765 Returns the new value of PARSER->SCOPE, if the `::' token is
13766 present, and NULL_TREE otherwise. */
13767
13768static tree
13769cp_parser_global_scope_opt (parser, current_scope_valid_p)
13770 cp_parser *parser;
13771 bool current_scope_valid_p;
13772{
13773 cp_token *token;
13774
13775 /* Peek at the next token. */
13776 token = cp_lexer_peek_token (parser->lexer);
13777 /* If we're looking at a `::' token then we're starting from the
13778 global namespace, not our current location. */
13779 if (token->type == CPP_SCOPE)
13780 {
13781 /* Consume the `::' token. */
13782 cp_lexer_consume_token (parser->lexer);
13783 /* Set the SCOPE so that we know where to start the lookup. */
13784 parser->scope = global_namespace;
13785 parser->qualifying_scope = global_namespace;
13786 parser->object_scope = NULL_TREE;
13787
13788 return parser->scope;
13789 }
13790 else if (!current_scope_valid_p)
13791 {
13792 parser->scope = NULL_TREE;
13793 parser->qualifying_scope = NULL_TREE;
13794 parser->object_scope = NULL_TREE;
13795 }
13796
13797 return NULL_TREE;
13798}
13799
13800/* Returns TRUE if the upcoming token sequence is the start of a
13801 constructor declarator. If FRIEND_P is true, the declarator is
13802 preceded by the `friend' specifier. */
13803
13804static bool
13805cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13806{
13807 bool constructor_p;
13808 tree type_decl = NULL_TREE;
13809 bool nested_name_p;
2050a1bb
MM
13810 cp_token *next_token;
13811
13812 /* The common case is that this is not a constructor declarator, so
13813 try to avoid doing lots of work if at all possible. */
13814 next_token = cp_lexer_peek_token (parser->lexer);
13815 if (next_token->type != CPP_NAME
13816 && next_token->type != CPP_SCOPE
13817 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13818 && next_token->type != CPP_TEMPLATE_ID)
13819 return false;
a723baf1
MM
13820
13821 /* Parse tentatively; we are going to roll back all of the tokens
13822 consumed here. */
13823 cp_parser_parse_tentatively (parser);
13824 /* Assume that we are looking at a constructor declarator. */
13825 constructor_p = true;
13826 /* Look for the optional `::' operator. */
13827 cp_parser_global_scope_opt (parser,
13828 /*current_scope_valid_p=*/false);
13829 /* Look for the nested-name-specifier. */
13830 nested_name_p
13831 = (cp_parser_nested_name_specifier_opt (parser,
13832 /*typename_keyword_p=*/false,
13833 /*check_dependency_p=*/false,
13834 /*type_p=*/false)
13835 != NULL_TREE);
13836 /* Outside of a class-specifier, there must be a
13837 nested-name-specifier. */
13838 if (!nested_name_p &&
13839 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13840 || friend_p))
13841 constructor_p = false;
13842 /* If we still think that this might be a constructor-declarator,
13843 look for a class-name. */
13844 if (constructor_p)
13845 {
13846 /* If we have:
13847
13848 template <typename T> struct S { S(); }
13849 template <typename T> S<T>::S ();
13850
13851 we must recognize that the nested `S' names a class.
13852 Similarly, for:
13853
13854 template <typename T> S<T>::S<T> ();
13855
13856 we must recognize that the nested `S' names a template. */
13857 type_decl = cp_parser_class_name (parser,
13858 /*typename_keyword_p=*/false,
13859 /*template_keyword_p=*/false,
13860 /*type_p=*/false,
13861 /*check_access_p=*/false,
13862 /*check_dependency_p=*/false,
13863 /*class_head_p=*/false);
13864 /* If there was no class-name, then this is not a constructor. */
13865 constructor_p = !cp_parser_error_occurred (parser);
13866 }
13867 /* If we're still considering a constructor, we have to see a `(',
13868 to begin the parameter-declaration-clause, followed by either a
13869 `)', an `...', or a decl-specifier. We need to check for a
13870 type-specifier to avoid being fooled into thinking that:
13871
13872 S::S (f) (int);
13873
13874 is a constructor. (It is actually a function named `f' that
13875 takes one parameter (of type `int') and returns a value of type
13876 `S::S'. */
13877 if (constructor_p
13878 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13879 {
13880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13881 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13882 && !cp_parser_storage_class_specifier_opt (parser))
13883 {
13884 if (current_class_type
13885 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13886 /* The constructor for one class cannot be declared inside
13887 another. */
13888 constructor_p = false;
13889 else
13890 {
13891 tree type;
13892
13893 /* Names appearing in the type-specifier should be looked up
13894 in the scope of the class. */
13895 if (current_class_type)
13896 type = NULL_TREE;
13897 else
13898 {
13899 type = TREE_TYPE (type_decl);
13900 if (TREE_CODE (type) == TYPENAME_TYPE)
13901 type = cp_parser_resolve_typename_type (parser, type);
13902 push_scope (type);
13903 }
13904 /* Look for the type-specifier. */
13905 cp_parser_type_specifier (parser,
13906 CP_PARSER_FLAGS_NONE,
13907 /*is_friend=*/false,
13908 /*is_declarator=*/true,
13909 /*declares_class_or_enum=*/NULL,
13910 /*is_cv_qualifier=*/NULL);
13911 /* Leave the scope of the class. */
13912 if (type)
13913 pop_scope (type);
13914
13915 constructor_p = !cp_parser_error_occurred (parser);
13916 }
13917 }
13918 }
13919 else
13920 constructor_p = false;
13921 /* We did not really want to consume any tokens. */
13922 cp_parser_abort_tentative_parse (parser);
13923
13924 return constructor_p;
13925}
13926
13927/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 13928 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
13929 they must be performed once we are in the scope of the function.
13930
13931 Returns the function defined. */
13932
13933static tree
13934cp_parser_function_definition_from_specifiers_and_declarator
cf22909c 13935 (parser, decl_specifiers, attributes, declarator)
a723baf1
MM
13936 cp_parser *parser;
13937 tree decl_specifiers;
13938 tree attributes;
13939 tree declarator;
a723baf1
MM
13940{
13941 tree fn;
13942 bool success_p;
13943
13944 /* Begin the function-definition. */
13945 success_p = begin_function_definition (decl_specifiers,
13946 attributes,
13947 declarator);
13948
13949 /* If there were names looked up in the decl-specifier-seq that we
13950 did not check, check them now. We must wait until we are in the
13951 scope of the function to perform the checks, since the function
13952 might be a friend. */
cf22909c 13953 perform_deferred_access_checks ();
a723baf1
MM
13954
13955 if (!success_p)
13956 {
13957 /* If begin_function_definition didn't like the definition, skip
13958 the entire function. */
13959 error ("invalid function declaration");
13960 cp_parser_skip_to_end_of_block_or_statement (parser);
13961 fn = error_mark_node;
13962 }
13963 else
13964 fn = cp_parser_function_definition_after_declarator (parser,
13965 /*inline_p=*/false);
13966
13967 return fn;
13968}
13969
13970/* Parse the part of a function-definition that follows the
13971 declarator. INLINE_P is TRUE iff this function is an inline
13972 function defined with a class-specifier.
13973
13974 Returns the function defined. */
13975
13976static tree
13977cp_parser_function_definition_after_declarator (parser,
13978 inline_p)
13979 cp_parser *parser;
13980 bool inline_p;
13981{
13982 tree fn;
13983 bool ctor_initializer_p = false;
13984 bool saved_in_unbraced_linkage_specification_p;
13985 unsigned saved_num_template_parameter_lists;
13986
13987 /* If the next token is `return', then the code may be trying to
13988 make use of the "named return value" extension that G++ used to
13989 support. */
13990 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13991 {
13992 /* Consume the `return' keyword. */
13993 cp_lexer_consume_token (parser->lexer);
13994 /* Look for the identifier that indicates what value is to be
13995 returned. */
13996 cp_parser_identifier (parser);
13997 /* Issue an error message. */
13998 error ("named return values are no longer supported");
13999 /* Skip tokens until we reach the start of the function body. */
14000 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14001 cp_lexer_consume_token (parser->lexer);
14002 }
14003 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14004 anything declared inside `f'. */
14005 saved_in_unbraced_linkage_specification_p
14006 = parser->in_unbraced_linkage_specification_p;
14007 parser->in_unbraced_linkage_specification_p = false;
14008 /* Inside the function, surrounding template-parameter-lists do not
14009 apply. */
14010 saved_num_template_parameter_lists
14011 = parser->num_template_parameter_lists;
14012 parser->num_template_parameter_lists = 0;
14013 /* If the next token is `try', then we are looking at a
14014 function-try-block. */
14015 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14016 ctor_initializer_p = cp_parser_function_try_block (parser);
14017 /* A function-try-block includes the function-body, so we only do
14018 this next part if we're not processing a function-try-block. */
14019 else
14020 ctor_initializer_p
14021 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14022
14023 /* Finish the function. */
14024 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14025 (inline_p ? 2 : 0));
14026 /* Generate code for it, if necessary. */
14027 expand_body (fn);
14028 /* Restore the saved values. */
14029 parser->in_unbraced_linkage_specification_p
14030 = saved_in_unbraced_linkage_specification_p;
14031 parser->num_template_parameter_lists
14032 = saved_num_template_parameter_lists;
14033
14034 return fn;
14035}
14036
14037/* Parse a template-declaration, assuming that the `export' (and
14038 `extern') keywords, if present, has already been scanned. MEMBER_P
14039 is as for cp_parser_template_declaration. */
14040
14041static void
14042cp_parser_template_declaration_after_export (parser, member_p)
14043 cp_parser *parser;
14044 bool member_p;
14045{
14046 tree decl = NULL_TREE;
14047 tree parameter_list;
14048 bool friend_p = false;
14049
14050 /* Look for the `template' keyword. */
14051 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14052 return;
14053
14054 /* And the `<'. */
14055 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14056 return;
14057
14058 /* Parse the template parameters. */
14059 begin_template_parm_list ();
14060 /* If the next token is `>', then we have an invalid
14061 specialization. Rather than complain about an invalid template
14062 parameter, issue an error message here. */
14063 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14064 {
14065 cp_parser_error (parser, "invalid explicit specialization");
14066 parameter_list = NULL_TREE;
14067 }
14068 else
14069 parameter_list = cp_parser_template_parameter_list (parser);
14070 parameter_list = end_template_parm_list (parameter_list);
14071 /* Look for the `>'. */
14072 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14073 /* We just processed one more parameter list. */
14074 ++parser->num_template_parameter_lists;
14075 /* If the next token is `template', there are more template
14076 parameters. */
14077 if (cp_lexer_next_token_is_keyword (parser->lexer,
14078 RID_TEMPLATE))
14079 cp_parser_template_declaration_after_export (parser, member_p);
14080 else
14081 {
14082 decl = cp_parser_single_declaration (parser,
14083 member_p,
14084 &friend_p);
14085
14086 /* If this is a member template declaration, let the front
14087 end know. */
14088 if (member_p && !friend_p && decl)
14089 decl = finish_member_template_decl (decl);
14090 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14091 make_friend_class (current_class_type, TREE_TYPE (decl));
14092 }
14093 /* We are done with the current parameter list. */
14094 --parser->num_template_parameter_lists;
14095
14096 /* Finish up. */
14097 finish_template_decl (parameter_list);
14098
14099 /* Register member declarations. */
14100 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14101 finish_member_declaration (decl);
14102
14103 /* If DECL is a function template, we must return to parse it later.
14104 (Even though there is no definition, there might be default
14105 arguments that need handling.) */
14106 if (member_p && decl
14107 && (TREE_CODE (decl) == FUNCTION_DECL
14108 || DECL_FUNCTION_TEMPLATE_P (decl)))
14109 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 14110 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14111 TREE_VALUE (parser->unparsed_functions_queues));
14112}
14113
14114/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14115 `function-definition' sequence. MEMBER_P is true, this declaration
14116 appears in a class scope.
14117
14118 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14119 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14120
14121static tree
14122cp_parser_single_declaration (parser,
14123 member_p,
14124 friend_p)
14125 cp_parser *parser;
14126 bool member_p;
14127 bool *friend_p;
14128{
14129 bool declares_class_or_enum;
14130 tree decl = NULL_TREE;
14131 tree decl_specifiers;
14132 tree attributes;
a723baf1
MM
14133
14134 /* Parse the dependent declaration. We don't know yet
14135 whether it will be a function-definition. */
14136 cp_parser_parse_tentatively (parser);
14137 /* Defer access checks until we know what is being declared. */
cf22909c
KL
14138 push_deferring_access_checks (true);
14139
a723baf1
MM
14140 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14141 alternative. */
14142 decl_specifiers
14143 = cp_parser_decl_specifier_seq (parser,
14144 CP_PARSER_FLAGS_OPTIONAL,
14145 &attributes,
14146 &declares_class_or_enum);
14147 /* Gather up the access checks that occurred the
14148 decl-specifier-seq. */
cf22909c
KL
14149 stop_deferring_access_checks ();
14150
a723baf1
MM
14151 /* Check for the declaration of a template class. */
14152 if (declares_class_or_enum)
14153 {
14154 if (cp_parser_declares_only_class_p (parser))
14155 {
14156 decl = shadow_tag (decl_specifiers);
14157 if (decl)
14158 decl = TYPE_NAME (decl);
14159 else
14160 decl = error_mark_node;
14161 }
14162 }
14163 else
14164 decl = NULL_TREE;
14165 /* If it's not a template class, try for a template function. If
14166 the next token is a `;', then this declaration does not declare
14167 anything. But, if there were errors in the decl-specifiers, then
14168 the error might well have come from an attempted class-specifier.
14169 In that case, there's no need to warn about a missing declarator. */
14170 if (!decl
14171 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14172 || !value_member (error_mark_node, decl_specifiers)))
14173 decl = cp_parser_init_declarator (parser,
14174 decl_specifiers,
14175 attributes,
a723baf1
MM
14176 /*function_definition_allowed_p=*/false,
14177 member_p,
14178 /*function_definition_p=*/NULL);
cf22909c
KL
14179
14180 pop_deferring_access_checks ();
14181
a723baf1
MM
14182 /* Clear any current qualification; whatever comes next is the start
14183 of something new. */
14184 parser->scope = NULL_TREE;
14185 parser->qualifying_scope = NULL_TREE;
14186 parser->object_scope = NULL_TREE;
14187 /* Look for a trailing `;' after the declaration. */
14188 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14189 && cp_parser_committed_to_tentative_parse (parser))
14190 cp_parser_skip_to_end_of_block_or_statement (parser);
14191 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14192 if (cp_parser_parse_definitely (parser))
14193 {
14194 if (friend_p)
14195 *friend_p = cp_parser_friend_p (decl_specifiers);
14196 }
14197 /* Otherwise, try a function-definition. */
14198 else
14199 decl = cp_parser_function_definition (parser, friend_p);
14200
14201 return decl;
14202}
14203
14204/* Parse a functional cast to TYPE. Returns an expression
14205 representing the cast. */
14206
14207static tree
14208cp_parser_functional_cast (parser, type)
14209 cp_parser *parser;
14210 tree type;
14211{
14212 tree expression_list;
14213
14214 /* Look for the opening `('. */
14215 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14216 return error_mark_node;
14217 /* If the next token is not an `)', there are arguments to the
14218 cast. */
14219 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14220 expression_list = cp_parser_expression_list (parser);
14221 else
14222 expression_list = NULL_TREE;
14223 /* Look for the closing `)'. */
14224 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14225
14226 return build_functional_cast (type, expression_list);
14227}
14228
14229/* MEMBER_FUNCTION is a member function, or a friend. If default
14230 arguments, or the body of the function have not yet been parsed,
14231 parse them now. */
14232
14233static void
14234cp_parser_late_parsing_for_member (parser, member_function)
14235 cp_parser *parser;
14236 tree member_function;
14237{
14238 cp_lexer *saved_lexer;
14239
14240 /* If this member is a template, get the underlying
14241 FUNCTION_DECL. */
14242 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14243 member_function = DECL_TEMPLATE_RESULT (member_function);
14244
14245 /* There should not be any class definitions in progress at this
14246 point; the bodies of members are only parsed outside of all class
14247 definitions. */
14248 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14249 /* While we're parsing the member functions we might encounter more
14250 classes. We want to handle them right away, but we don't want
14251 them getting mixed up with functions that are currently in the
14252 queue. */
14253 parser->unparsed_functions_queues
14254 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14255
14256 /* Make sure that any template parameters are in scope. */
14257 maybe_begin_member_template_processing (member_function);
14258
a723baf1
MM
14259 /* If the body of the function has not yet been parsed, parse it
14260 now. */
14261 if (DECL_PENDING_INLINE_P (member_function))
14262 {
14263 tree function_scope;
14264 cp_token_cache *tokens;
14265
14266 /* The function is no longer pending; we are processing it. */
14267 tokens = DECL_PENDING_INLINE_INFO (member_function);
14268 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14269 DECL_PENDING_INLINE_P (member_function) = 0;
14270 /* If this was an inline function in a local class, enter the scope
14271 of the containing function. */
14272 function_scope = decl_function_context (member_function);
14273 if (function_scope)
14274 push_function_context_to (function_scope);
14275
14276 /* Save away the current lexer. */
14277 saved_lexer = parser->lexer;
14278 /* Make a new lexer to feed us the tokens saved for this function. */
14279 parser->lexer = cp_lexer_new_from_tokens (tokens);
14280 parser->lexer->next = saved_lexer;
14281
14282 /* Set the current source position to be the location of the first
14283 token in the saved inline body. */
3466b292 14284 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14285
14286 /* Let the front end know that we going to be defining this
14287 function. */
14288 start_function (NULL_TREE, member_function, NULL_TREE,
14289 SF_PRE_PARSED | SF_INCLASS_INLINE);
14290
14291 /* Now, parse the body of the function. */
14292 cp_parser_function_definition_after_declarator (parser,
14293 /*inline_p=*/true);
14294
14295 /* Leave the scope of the containing function. */
14296 if (function_scope)
14297 pop_function_context_from (function_scope);
14298 /* Restore the lexer. */
14299 parser->lexer = saved_lexer;
14300 }
14301
14302 /* Remove any template parameters from the symbol table. */
14303 maybe_end_member_template_processing ();
14304
14305 /* Restore the queue. */
14306 parser->unparsed_functions_queues
14307 = TREE_CHAIN (parser->unparsed_functions_queues);
14308}
14309
8218bd34
MM
14310/* FN is a FUNCTION_DECL which may contains a parameter with an
14311 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
14312
14313static void
8218bd34 14314cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
14315{
14316 cp_lexer *saved_lexer;
14317 cp_token_cache *tokens;
14318 bool saved_local_variables_forbidden_p;
14319 tree parameters;
8218bd34
MM
14320
14321 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
14322 parameters;
14323 parameters = TREE_CHAIN (parameters))
14324 {
14325 if (!TREE_PURPOSE (parameters)
14326 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14327 continue;
14328
14329 /* Save away the current lexer. */
14330 saved_lexer = parser->lexer;
14331 /* Create a new one, using the tokens we have saved. */
14332 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14333 parser->lexer = cp_lexer_new_from_tokens (tokens);
14334
14335 /* Set the current source position to be the location of the
14336 first token in the default argument. */
3466b292 14337 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14338
14339 /* Local variable names (and the `this' keyword) may not appear
14340 in a default argument. */
14341 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14342 parser->local_variables_forbidden_p = true;
14343 /* Parse the assignment-expression. */
8218bd34
MM
14344 if (DECL_CONTEXT (fn))
14345 push_nested_class (DECL_CONTEXT (fn), 1);
a723baf1 14346 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
8218bd34 14347 if (DECL_CONTEXT (fn))
e5976695 14348 pop_nested_class ();
a723baf1
MM
14349
14350 /* Restore saved state. */
14351 parser->lexer = saved_lexer;
14352 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14353 }
14354}
14355
14356/* Parse the operand of `sizeof' (or a similar operator). Returns
14357 either a TYPE or an expression, depending on the form of the
14358 input. The KEYWORD indicates which kind of expression we have
14359 encountered. */
14360
14361static tree
14362cp_parser_sizeof_operand (parser, keyword)
14363 cp_parser *parser;
14364 enum rid keyword;
14365{
14366 static const char *format;
14367 tree expr = NULL_TREE;
14368 const char *saved_message;
14369 bool saved_constant_expression_p;
14370
14371 /* Initialize FORMAT the first time we get here. */
14372 if (!format)
14373 format = "types may not be defined in `%s' expressions";
14374
14375 /* Types cannot be defined in a `sizeof' expression. Save away the
14376 old message. */
14377 saved_message = parser->type_definition_forbidden_message;
14378 /* And create the new one. */
14379 parser->type_definition_forbidden_message
14380 = ((const char *)
14381 xmalloc (strlen (format)
14382 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14383 + 1 /* `\0' */));
14384 sprintf ((char *) parser->type_definition_forbidden_message,
14385 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14386
14387 /* The restrictions on constant-expressions do not apply inside
14388 sizeof expressions. */
14389 saved_constant_expression_p = parser->constant_expression_p;
14390 parser->constant_expression_p = false;
14391
3beb3abf
MM
14392 /* Do not actually evaluate the expression. */
14393 ++skip_evaluation;
a723baf1
MM
14394 /* If it's a `(', then we might be looking at the type-id
14395 construction. */
14396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14397 {
14398 tree type;
14399
14400 /* We can't be sure yet whether we're looking at a type-id or an
14401 expression. */
14402 cp_parser_parse_tentatively (parser);
14403 /* Consume the `('. */
14404 cp_lexer_consume_token (parser->lexer);
14405 /* Parse the type-id. */
14406 type = cp_parser_type_id (parser);
14407 /* Now, look for the trailing `)'. */
14408 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14409 /* If all went well, then we're done. */
14410 if (cp_parser_parse_definitely (parser))
14411 {
14412 /* Build a list of decl-specifiers; right now, we have only
14413 a single type-specifier. */
14414 type = build_tree_list (NULL_TREE,
14415 type);
14416
14417 /* Call grokdeclarator to figure out what type this is. */
14418 expr = grokdeclarator (NULL_TREE,
14419 type,
14420 TYPENAME,
14421 /*initialized=*/0,
14422 /*attrlist=*/NULL);
14423 }
14424 }
14425
14426 /* If the type-id production did not work out, then we must be
14427 looking at the unary-expression production. */
14428 if (!expr)
14429 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
14430 /* Go back to evaluating expressions. */
14431 --skip_evaluation;
a723baf1
MM
14432
14433 /* Free the message we created. */
14434 free ((char *) parser->type_definition_forbidden_message);
14435 /* And restore the old one. */
14436 parser->type_definition_forbidden_message = saved_message;
14437 parser->constant_expression_p = saved_constant_expression_p;
14438
14439 return expr;
14440}
14441
14442/* If the current declaration has no declarator, return true. */
14443
14444static bool
14445cp_parser_declares_only_class_p (cp_parser *parser)
14446{
14447 /* If the next token is a `;' or a `,' then there is no
14448 declarator. */
14449 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14450 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14451}
14452
14453/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14454 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14455
14456static bool
14457cp_parser_friend_p (decl_specifiers)
14458 tree decl_specifiers;
14459{
14460 while (decl_specifiers)
14461 {
14462 /* See if this decl-specifier is `friend'. */
14463 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14464 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14465 return true;
14466
14467 /* Go on to the next decl-specifier. */
14468 decl_specifiers = TREE_CHAIN (decl_specifiers);
14469 }
14470
14471 return false;
14472}
14473
14474/* If the next token is of the indicated TYPE, consume it. Otherwise,
14475 issue an error message indicating that TOKEN_DESC was expected.
14476
14477 Returns the token consumed, if the token had the appropriate type.
14478 Otherwise, returns NULL. */
14479
14480static cp_token *
14481cp_parser_require (parser, type, token_desc)
14482 cp_parser *parser;
14483 enum cpp_ttype type;
14484 const char *token_desc;
14485{
14486 if (cp_lexer_next_token_is (parser->lexer, type))
14487 return cp_lexer_consume_token (parser->lexer);
14488 else
14489 {
e5976695
MM
14490 /* Output the MESSAGE -- unless we're parsing tentatively. */
14491 if (!cp_parser_simulate_error (parser))
14492 error ("expected %s", token_desc);
a723baf1
MM
14493 return NULL;
14494 }
14495}
14496
14497/* Like cp_parser_require, except that tokens will be skipped until
14498 the desired token is found. An error message is still produced if
14499 the next token is not as expected. */
14500
14501static void
14502cp_parser_skip_until_found (parser, type, token_desc)
14503 cp_parser *parser;
14504 enum cpp_ttype type;
14505 const char *token_desc;
14506{
14507 cp_token *token;
14508 unsigned nesting_depth = 0;
14509
14510 if (cp_parser_require (parser, type, token_desc))
14511 return;
14512
14513 /* Skip tokens until the desired token is found. */
14514 while (true)
14515 {
14516 /* Peek at the next token. */
14517 token = cp_lexer_peek_token (parser->lexer);
14518 /* If we've reached the token we want, consume it and
14519 stop. */
14520 if (token->type == type && !nesting_depth)
14521 {
14522 cp_lexer_consume_token (parser->lexer);
14523 return;
14524 }
14525 /* If we've run out of tokens, stop. */
14526 if (token->type == CPP_EOF)
14527 return;
14528 if (token->type == CPP_OPEN_BRACE
14529 || token->type == CPP_OPEN_PAREN
14530 || token->type == CPP_OPEN_SQUARE)
14531 ++nesting_depth;
14532 else if (token->type == CPP_CLOSE_BRACE
14533 || token->type == CPP_CLOSE_PAREN
14534 || token->type == CPP_CLOSE_SQUARE)
14535 {
14536 if (nesting_depth-- == 0)
14537 return;
14538 }
14539 /* Consume this token. */
14540 cp_lexer_consume_token (parser->lexer);
14541 }
14542}
14543
14544/* If the next token is the indicated keyword, consume it. Otherwise,
14545 issue an error message indicating that TOKEN_DESC was expected.
14546
14547 Returns the token consumed, if the token had the appropriate type.
14548 Otherwise, returns NULL. */
14549
14550static cp_token *
14551cp_parser_require_keyword (parser, keyword, token_desc)
14552 cp_parser *parser;
14553 enum rid keyword;
14554 const char *token_desc;
14555{
14556 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14557
14558 if (token && token->keyword != keyword)
14559 {
14560 dyn_string_t error_msg;
14561
14562 /* Format the error message. */
14563 error_msg = dyn_string_new (0);
14564 dyn_string_append_cstr (error_msg, "expected ");
14565 dyn_string_append_cstr (error_msg, token_desc);
14566 cp_parser_error (parser, error_msg->s);
14567 dyn_string_delete (error_msg);
14568 return NULL;
14569 }
14570
14571 return token;
14572}
14573
14574/* Returns TRUE iff TOKEN is a token that can begin the body of a
14575 function-definition. */
14576
14577static bool
14578cp_parser_token_starts_function_definition_p (token)
14579 cp_token *token;
14580{
14581 return (/* An ordinary function-body begins with an `{'. */
14582 token->type == CPP_OPEN_BRACE
14583 /* A ctor-initializer begins with a `:'. */
14584 || token->type == CPP_COLON
14585 /* A function-try-block begins with `try'. */
14586 || token->keyword == RID_TRY
14587 /* The named return value extension begins with `return'. */
14588 || token->keyword == RID_RETURN);
14589}
14590
14591/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14592 definition. */
14593
14594static bool
14595cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14596{
14597 cp_token *token;
14598
14599 token = cp_lexer_peek_token (parser->lexer);
14600 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14601}
14602
14603/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14604 or none_type otherwise. */
14605
14606static enum tag_types
14607cp_parser_token_is_class_key (token)
14608 cp_token *token;
14609{
14610 switch (token->keyword)
14611 {
14612 case RID_CLASS:
14613 return class_type;
14614 case RID_STRUCT:
14615 return record_type;
14616 case RID_UNION:
14617 return union_type;
14618
14619 default:
14620 return none_type;
14621 }
14622}
14623
14624/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14625
14626static void
14627cp_parser_check_class_key (enum tag_types class_key, tree type)
14628{
14629 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14630 pedwarn ("`%s' tag used in naming `%#T'",
14631 class_key == union_type ? "union"
14632 : class_key == record_type ? "struct" : "class",
14633 type);
14634}
14635
14636/* Look for the `template' keyword, as a syntactic disambiguator.
14637 Return TRUE iff it is present, in which case it will be
14638 consumed. */
14639
14640static bool
14641cp_parser_optional_template_keyword (cp_parser *parser)
14642{
14643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14644 {
14645 /* The `template' keyword can only be used within templates;
14646 outside templates the parser can always figure out what is a
14647 template and what is not. */
14648 if (!processing_template_decl)
14649 {
14650 error ("`template' (as a disambiguator) is only allowed "
14651 "within templates");
14652 /* If this part of the token stream is rescanned, the same
14653 error message would be generated. So, we purge the token
14654 from the stream. */
14655 cp_lexer_purge_token (parser->lexer);
14656 return false;
14657 }
14658 else
14659 {
14660 /* Consume the `template' keyword. */
14661 cp_lexer_consume_token (parser->lexer);
14662 return true;
14663 }
14664 }
14665
14666 return false;
14667}
14668
2050a1bb
MM
14669/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14670 set PARSER->SCOPE, and perform other related actions. */
14671
14672static void
14673cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14674{
14675 tree value;
14676 tree check;
14677
14678 /* Get the stored value. */
14679 value = cp_lexer_consume_token (parser->lexer)->value;
14680 /* Perform any access checks that were deferred. */
14681 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 14682 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
14683 /* Set the scope from the stored value. */
14684 parser->scope = TREE_VALUE (value);
14685 parser->qualifying_scope = TREE_TYPE (value);
14686 parser->object_scope = NULL_TREE;
14687}
14688
a723baf1
MM
14689/* Add tokens to CACHE until an non-nested END token appears. */
14690
14691static void
14692cp_parser_cache_group (cp_parser *parser,
14693 cp_token_cache *cache,
14694 enum cpp_ttype end,
14695 unsigned depth)
14696{
14697 while (true)
14698 {
14699 cp_token *token;
14700
14701 /* Abort a parenthesized expression if we encounter a brace. */
14702 if ((end == CPP_CLOSE_PAREN || depth == 0)
14703 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14704 return;
14705 /* Consume the next token. */
14706 token = cp_lexer_consume_token (parser->lexer);
14707 /* If we've reached the end of the file, stop. */
14708 if (token->type == CPP_EOF)
14709 return;
14710 /* Add this token to the tokens we are saving. */
14711 cp_token_cache_push_token (cache, token);
14712 /* See if it starts a new group. */
14713 if (token->type == CPP_OPEN_BRACE)
14714 {
14715 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14716 if (depth == 0)
14717 return;
14718 }
14719 else if (token->type == CPP_OPEN_PAREN)
14720 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14721 else if (token->type == end)
14722 return;
14723 }
14724}
14725
14726/* Begin parsing tentatively. We always save tokens while parsing
14727 tentatively so that if the tentative parsing fails we can restore the
14728 tokens. */
14729
14730static void
14731cp_parser_parse_tentatively (parser)
14732 cp_parser *parser;
14733{
14734 /* Enter a new parsing context. */
14735 parser->context = cp_parser_context_new (parser->context);
14736 /* Begin saving tokens. */
14737 cp_lexer_save_tokens (parser->lexer);
14738 /* In order to avoid repetitive access control error messages,
14739 access checks are queued up until we are no longer parsing
14740 tentatively. */
cf22909c 14741 push_deferring_access_checks (true);
a723baf1
MM
14742}
14743
14744/* Commit to the currently active tentative parse. */
14745
14746static void
14747cp_parser_commit_to_tentative_parse (parser)
14748 cp_parser *parser;
14749{
14750 cp_parser_context *context;
14751 cp_lexer *lexer;
14752
14753 /* Mark all of the levels as committed. */
14754 lexer = parser->lexer;
14755 for (context = parser->context; context->next; context = context->next)
14756 {
14757 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14758 break;
14759 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14760 while (!cp_lexer_saving_tokens (lexer))
14761 lexer = lexer->next;
14762 cp_lexer_commit_tokens (lexer);
14763 }
14764}
14765
14766/* Abort the currently active tentative parse. All consumed tokens
14767 will be rolled back, and no diagnostics will be issued. */
14768
14769static void
14770cp_parser_abort_tentative_parse (parser)
14771 cp_parser *parser;
14772{
14773 cp_parser_simulate_error (parser);
14774 /* Now, pretend that we want to see if the construct was
14775 successfully parsed. */
14776 cp_parser_parse_definitely (parser);
14777}
14778
14779/* Stop parsing tentatively. If a parse error has ocurred, restore the
14780 token stream. Otherwise, commit to the tokens we have consumed.
14781 Returns true if no error occurred; false otherwise. */
14782
14783static bool
14784cp_parser_parse_definitely (parser)
14785 cp_parser *parser;
14786{
14787 bool error_occurred;
14788 cp_parser_context *context;
14789
14790 /* Remember whether or not an error ocurred, since we are about to
14791 destroy that information. */
14792 error_occurred = cp_parser_error_occurred (parser);
14793 /* Remove the topmost context from the stack. */
14794 context = parser->context;
14795 parser->context = context->next;
14796 /* If no parse errors occurred, commit to the tentative parse. */
14797 if (!error_occurred)
14798 {
14799 /* Commit to the tokens read tentatively, unless that was
14800 already done. */
14801 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14802 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
14803
14804 pop_to_parent_deferring_access_checks ();
a723baf1
MM
14805 }
14806 /* Otherwise, if errors occurred, roll back our state so that things
14807 are just as they were before we began the tentative parse. */
14808 else
cf22909c
KL
14809 {
14810 cp_lexer_rollback_tokens (parser->lexer);
14811 pop_deferring_access_checks ();
14812 }
e5976695
MM
14813 /* Add the context to the front of the free list. */
14814 context->next = cp_parser_context_free_list;
14815 cp_parser_context_free_list = context;
14816
14817 return !error_occurred;
a723baf1
MM
14818}
14819
a723baf1
MM
14820/* Returns true if we are parsing tentatively -- but have decided that
14821 we will stick with this tentative parse, even if errors occur. */
14822
14823static bool
14824cp_parser_committed_to_tentative_parse (parser)
14825 cp_parser *parser;
14826{
14827 return (cp_parser_parsing_tentatively (parser)
14828 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14829}
14830
14831/* Returns non-zero iff an error has occurred during the most recent
14832 tentative parse. */
14833
14834static bool
14835cp_parser_error_occurred (parser)
14836 cp_parser *parser;
14837{
14838 return (cp_parser_parsing_tentatively (parser)
14839 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14840}
14841
14842/* Returns non-zero if GNU extensions are allowed. */
14843
14844static bool
14845cp_parser_allow_gnu_extensions_p (parser)
14846 cp_parser *parser;
14847{
14848 return parser->allow_gnu_extensions_p;
14849}
14850
14851\f
14852
14853/* The parser. */
14854
14855static GTY (()) cp_parser *the_parser;
14856
14857/* External interface. */
14858
14859/* Parse the entire translation unit. */
14860
14861int
14862yyparse ()
14863{
14864 bool error_occurred;
14865
14866 the_parser = cp_parser_new ();
cf22909c 14867 push_deferring_access_checks (false);
a723baf1
MM
14868 error_occurred = cp_parser_translation_unit (the_parser);
14869 the_parser = NULL;
17211ab5
GK
14870
14871 finish_file ();
a723baf1
MM
14872
14873 return error_occurred;
14874}
14875
14876/* Clean up after parsing the entire translation unit. */
14877
14878void
14879free_parser_stacks ()
14880{
14881 /* Nothing to do. */
14882}
14883
14884/* This variable must be provided by every front end. */
14885
14886int yydebug;
14887
14888#include "gt-cp-parser.h"