]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
parse.y: Remove.
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
a723baf1
MM
1/* C++ Parser.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to the Free
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"
35#include "ggc.h"
36#include "toplev.h"
37#include "output.h"
38
39\f
40/* The lexer. */
41
42/* Overview
43 --------
44
45 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
46 look-ahead.
47
48 Methodology
49 -----------
50
51 We use a circular buffer to store incoming tokens.
52
53 Some artifacts of the C++ language (such as the
54 expression/declaration ambiguity) require arbitrary look-ahead.
55 The strategy we adopt for dealing with these problems is to attempt
56 to parse one construct (e.g., the declaration) and fall back to the
57 other (e.g., the expression) if that attempt does not succeed.
58 Therefore, we must sometimes store an arbitrary number of tokens.
59
60 The parser routinely peeks at the next token, and then consumes it
61 later. That also requires a buffer in which to store the tokens.
62
63 In order to easily permit adding tokens to the end of the buffer,
64 while removing them from the beginning of the buffer, we use a
65 circular buffer. */
66
67/* A C++ token. */
68
69typedef struct cp_token GTY (())
70{
71 /* The kind of token. */
72 enum cpp_ttype type;
73 /* The value associated with this token, if any. */
74 tree value;
75 /* If this token is a keyword, this value indicates which keyword.
76 Otherwise, this value is RID_MAX. */
77 enum rid keyword;
78 /* The file in which this token was found. */
79 const char *file_name;
80 /* The line at which this token was found. */
81 int line_number;
82} cp_token;
83
84/* The number of tokens in a single token block. */
85
86#define CP_TOKEN_BLOCK_NUM_TOKENS 32
87
88/* A group of tokens. These groups are chained together to store
89 large numbers of tokens. (For example, a token block is created
90 when the body of an inline member function is first encountered;
91 the tokens are processed later after the class definition is
92 complete.)
93
94 This somewhat ungainly data structure (as opposed to, say, a
95 variable-length array), is used due to contraints imposed by the
96 current garbage-collection methodology. If it is made more
97 flexible, we could perhaps simplify the data structures involved. */
98
99typedef struct cp_token_block GTY (())
100{
101 /* The tokens. */
102 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103 /* The number of tokens in this block. */
104 size_t num_tokens;
105 /* The next token block in the chain. */
106 struct cp_token_block *next;
107 /* The previous block in the chain. */
108 struct cp_token_block *prev;
109} cp_token_block;
110
111typedef struct cp_token_cache GTY (())
112{
113 /* The first block in the cache. NULL if there are no tokens in the
114 cache. */
115 cp_token_block *first;
116 /* The last block in the cache. NULL If there are no tokens in the
117 cache. */
118 cp_token_block *last;
119} cp_token_cache;
120
121/* Prototypes. */
122
123static cp_token_cache *cp_token_cache_new
124 (void);
125static void cp_token_cache_push_token
126 (cp_token_cache *, cp_token *);
127
128/* Create a new cp_token_cache. */
129
130static cp_token_cache *
131cp_token_cache_new ()
132{
133 return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
134}
135
136/* Add *TOKEN to *CACHE. */
137
138static void
139cp_token_cache_push_token (cp_token_cache *cache,
140 cp_token *token)
141{
142 cp_token_block *b = cache->last;
143
144 /* See if we need to allocate a new token block. */
145 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146 {
147 b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
148 b->prev = cache->last;
149 if (cache->last)
150 {
151 cache->last->next = b;
152 cache->last = b;
153 }
154 else
155 cache->first = cache->last = b;
156 }
157 /* Add this token to the current token block. */
158 b->tokens[b->num_tokens++] = *token;
159}
160
161/* The cp_lexer structure represents the C++ lexer. It is responsible
162 for managing the token stream from the preprocessor and supplying
163 it to the parser. */
164
165typedef struct cp_lexer GTY (())
166{
167 /* The memory allocated for the buffer. Never NULL. */
168 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169 /* A pointer just past the end of the memory allocated for the buffer. */
170 cp_token * GTY ((skip (""))) buffer_end;
171 /* The first valid token in the buffer, or NULL if none. */
172 cp_token * GTY ((skip (""))) first_token;
173 /* The next available token. If NEXT_TOKEN is NULL, then there are
174 no more available tokens. */
175 cp_token * GTY ((skip (""))) next_token;
176 /* A pointer just past the last available token. If FIRST_TOKEN is
177 NULL, however, there are no available tokens, and then this
178 location is simply the place in which the next token read will be
179 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180 When the LAST_TOKEN == BUFFER, then the last token is at the
181 highest memory address in the BUFFER. */
182 cp_token * GTY ((skip (""))) last_token;
183
184 /* A stack indicating positions at which cp_lexer_save_tokens was
185 called. The top entry is the most recent position at which we
186 began saving tokens. The entries are differences in token
187 position between FIRST_TOKEN and the first saved token.
188
189 If the stack is non-empty, we are saving tokens. When a token is
190 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191 pointer will not. The token stream will be preserved so that it
192 can be reexamined later.
193
194 If the stack is empty, then we are not saving tokens. Whenever a
195 token is consumed, the FIRST_TOKEN pointer will be moved, and the
196 consumed token will be gone forever. */
197 varray_type saved_tokens;
198
199 /* The STRING_CST tokens encountered while processing the current
200 string literal. */
201 varray_type string_tokens;
202
203 /* True if we should obtain more tokens from the preprocessor; false
204 if we are processing a saved token cache. */
205 bool main_lexer_p;
206
207 /* True if we should output debugging information. */
208 bool debugging_p;
209
210 /* The next lexer in a linked list of lexers. */
211 struct cp_lexer *next;
212} cp_lexer;
213
214/* Prototypes. */
215
216static cp_lexer *cp_lexer_new
217 PARAMS ((bool));
218static cp_lexer *cp_lexer_new_from_tokens
219 PARAMS ((struct cp_token_cache *));
220static int cp_lexer_saving_tokens
221 PARAMS ((const cp_lexer *));
222static cp_token *cp_lexer_next_token
223 PARAMS ((cp_lexer *, cp_token *));
224static ptrdiff_t cp_lexer_token_difference
225 PARAMS ((cp_lexer *, cp_token *, cp_token *));
226static cp_token *cp_lexer_read_token
227 PARAMS ((cp_lexer *));
228static void cp_lexer_maybe_grow_buffer
229 PARAMS ((cp_lexer *));
230static void cp_lexer_get_preprocessor_token
231 PARAMS ((cp_lexer *, cp_token *));
232static cp_token *cp_lexer_peek_token
233 PARAMS ((cp_lexer *));
234static cp_token *cp_lexer_peek_nth_token
235 PARAMS ((cp_lexer *, size_t));
236static bool cp_lexer_next_token_is
237 PARAMS ((cp_lexer *, enum cpp_ttype));
238static bool cp_lexer_next_token_is_not
239 PARAMS ((cp_lexer *, enum cpp_ttype));
240static bool cp_lexer_next_token_is_keyword
241 PARAMS ((cp_lexer *, enum rid));
242static cp_token *cp_lexer_consume_token
243 PARAMS ((cp_lexer *));
244static void cp_lexer_purge_token
245 (cp_lexer *);
246static void cp_lexer_purge_tokens_after
247 (cp_lexer *, cp_token *);
248static void cp_lexer_save_tokens
249 PARAMS ((cp_lexer *));
250static void cp_lexer_commit_tokens
251 PARAMS ((cp_lexer *));
252static void cp_lexer_rollback_tokens
253 PARAMS ((cp_lexer *));
254static void cp_lexer_set_source_position_from_token
255 PARAMS ((cp_lexer *, const cp_token *));
256static void cp_lexer_print_token
257 PARAMS ((FILE *, cp_token *));
258static bool cp_lexer_debugging_p
259 PARAMS ((cp_lexer *));
260static void cp_lexer_start_debugging
261 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
262static void cp_lexer_stop_debugging
263 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
264
265/* Manifest constants. */
266
267#define CP_TOKEN_BUFFER_SIZE 5
268#define CP_SAVED_TOKENS_SIZE 5
269
270/* A token type for keywords, as opposed to ordinary identifiers. */
271#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
272
273/* A token type for template-ids. If a template-id is processed while
274 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
275 the value of the CPP_TEMPLATE_ID is whatever was returned by
276 cp_parser_template_id. */
277#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
278
279/* A token type for nested-name-specifiers. If a
280 nested-name-specifier is processed while parsing tentatively, it is
281 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
282 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
283 cp_parser_nested_name_specifier_opt. */
284#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
285
286/* A token type for tokens that are not tokens at all; these are used
287 to mark the end of a token block. */
288#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
289
290/* Variables. */
291
292/* The stream to which debugging output should be written. */
293static FILE *cp_lexer_debug_stream;
294
295/* Create a new C++ lexer. If MAIN_LEXER_P is true the new lexer is
296 the main lexer -- i.e, the lexer that gets tokens from the
297 preprocessor. Otherwise, it is a lexer that uses a cache of stored
298 tokens. */
299
300static cp_lexer *
301cp_lexer_new (bool main_lexer_p)
302{
303 cp_lexer *lexer;
304
305 /* Allocate the memory. */
306 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
307
308 /* Create the circular buffer. */
309 lexer->buffer = ((cp_token *)
310 ggc_alloc (CP_TOKEN_BUFFER_SIZE * sizeof (cp_token)));
311 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
312
313 /* There are no tokens in the buffer. */
314 lexer->last_token = lexer->buffer;
315
316 /* This lexer obtains more tokens by calling c_lex. */
317 lexer->main_lexer_p = main_lexer_p;
318
319 /* Create the SAVED_TOKENS stack. */
320 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
321
322 /* Create the STRINGS array. */
323 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
324
325 /* Assume we are not debugging. */
326 lexer->debugging_p = false;
327
328 return lexer;
329}
330
331/* Create a new lexer whose token stream is primed with the TOKENS.
332 When these tokens are exhausted, no new tokens will be read. */
333
334static cp_lexer *
335cp_lexer_new_from_tokens (cp_token_cache *tokens)
336{
337 cp_lexer *lexer;
338 cp_token *token;
339 cp_token_block *block;
340 ptrdiff_t num_tokens;
341
342 /* Create the lexer. */
343 lexer = cp_lexer_new (/*main_lexer_p=*/false);
344
345 /* Create a new buffer, appropriately sized. */
346 num_tokens = 0;
347 for (block = tokens->first; block != NULL; block = block->next)
348 num_tokens += block->num_tokens;
349 lexer->buffer = ((cp_token *)
350 ggc_alloc (num_tokens * sizeof (cp_token)));
351 lexer->buffer_end = lexer->buffer + num_tokens;
352
353 /* Install the tokens. */
354 token = lexer->buffer;
355 for (block = tokens->first; block != NULL; block = block->next)
356 {
357 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
358 token += block->num_tokens;
359 }
360
361 /* The FIRST_TOKEN is the beginning of the buffer. */
362 lexer->first_token = lexer->buffer;
363 /* The next available token is also at the beginning of the buffer. */
364 lexer->next_token = lexer->buffer;
365 /* The buffer is full. */
366 lexer->last_token = lexer->first_token;
367
368 return lexer;
369}
370
371/* Non-zero if we are presently saving tokens. */
372
373static int
374cp_lexer_saving_tokens (lexer)
375 const cp_lexer *lexer;
376{
377 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
378}
379
380/* TOKEN points into the circular token buffer. Return a pointer to
381 the next token in the buffer. */
382
383static cp_token *
384cp_lexer_next_token (lexer, token)
385 cp_lexer *lexer;
386 cp_token *token;
387{
388 token++;
389 if (token == lexer->buffer_end)
390 token = lexer->buffer;
391 return token;
392}
393
394/* Return a pointer to the token that is N tokens beyond TOKEN in the
395 buffer. */
396
397static cp_token *
398cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
399{
400 token += n;
401 if (token >= lexer->buffer_end)
402 token = lexer->buffer + (token - lexer->buffer_end);
403 return token;
404}
405
406/* Returns the number of times that START would have to be incremented
407 to reach FINISH. If START and FINISH are the same, returns zero. */
408
409static ptrdiff_t
410cp_lexer_token_difference (lexer, start, finish)
411 cp_lexer *lexer;
412 cp_token *start;
413 cp_token *finish;
414{
415 if (finish >= start)
416 return finish - start;
417 else
418 return ((lexer->buffer_end - lexer->buffer)
419 - (start - finish));
420}
421
422/* Obtain another token from the C preprocessor and add it to the
423 token buffer. Returns the newly read token. */
424
425static cp_token *
426cp_lexer_read_token (lexer)
427 cp_lexer *lexer;
428{
429 cp_token *token;
430
431 /* Make sure there is room in the buffer. */
432 cp_lexer_maybe_grow_buffer (lexer);
433
434 /* If there weren't any tokens, then this one will be the first. */
435 if (!lexer->first_token)
436 lexer->first_token = lexer->last_token;
437 /* Similarly, if there were no available tokens, there is one now. */
438 if (!lexer->next_token)
439 lexer->next_token = lexer->last_token;
440
441 /* Figure out where we're going to store the new token. */
442 token = lexer->last_token;
443
444 /* Get a new token from the preprocessor. */
445 cp_lexer_get_preprocessor_token (lexer, token);
446
447 /* Increment LAST_TOKEN. */
448 lexer->last_token = cp_lexer_next_token (lexer, token);
449
450 /* The preprocessor does not yet do translation phase six, i.e., the
451 combination of adjacent string literals. Therefore, we do it
452 here. */
453 if (token->type == CPP_STRING || token->type == CPP_WSTRING)
454 {
455 ptrdiff_t delta;
456 int i;
457
458 /* When we grow the buffer, we may invalidate TOKEN. So, save
459 the distance from the beginning of the BUFFER so that we can
460 recaulate it. */
461 delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
462 /* Make sure there is room in the buffer for another token. */
463 cp_lexer_maybe_grow_buffer (lexer);
464 /* Restore TOKEN. */
465 token = lexer->buffer;
466 for (i = 0; i < delta; ++i)
467 token = cp_lexer_next_token (lexer, token);
468
469 VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
470 while (true)
471 {
472 /* Read the token after TOKEN. */
473 cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
474 /* See whether it's another string constant. */
475 if (lexer->last_token->type != token->type)
476 {
477 /* If not, then it will be the next real token. */
478 lexer->last_token = cp_lexer_next_token (lexer,
479 lexer->last_token);
480 break;
481 }
482
483 /* Chain the strings together. */
484 VARRAY_PUSH_TREE (lexer->string_tokens,
485 lexer->last_token->value);
486 }
487
488 /* Create a single STRING_CST. Curiously we have to call
489 combine_strings even if there is only a single string in
490 order to get the type set correctly. */
491 token->value = combine_strings (lexer->string_tokens);
492 VARRAY_CLEAR (lexer->string_tokens);
493 token->value = fix_string_type (token->value);
494 /* Strings should have type `const char []'. Right now, we will
495 have an ARRAY_TYPE that is constant rather than an array of
496 constant elements. */
497 if (flag_const_strings)
498 {
499 tree type;
500
501 /* Get the current type. It will be an ARRAY_TYPE. */
502 type = TREE_TYPE (token->value);
503 /* Use build_cplus_array_type to rebuild the array, thereby
504 getting the right type. */
505 type = build_cplus_array_type (TREE_TYPE (type),
506 TYPE_DOMAIN (type));
507 /* Reset the type of the token. */
508 TREE_TYPE (token->value) = type;
509 }
510 }
511
512 return token;
513}
514
515/* If the circular buffer is full, make it bigger. */
516
517static void
518cp_lexer_maybe_grow_buffer (lexer)
519 cp_lexer *lexer;
520{
521 /* If the buffer is full, enlarge it. */
522 if (lexer->last_token == lexer->first_token)
523 {
524 cp_token *new_buffer;
525 cp_token *old_buffer;
526 cp_token *new_first_token;
527 ptrdiff_t buffer_length;
528 size_t num_tokens_to_copy;
529
530 /* Remember the current buffer pointer. It will become invalid,
531 but we will need to do pointer arithmetic involving this
532 value. */
533 old_buffer = lexer->buffer;
534 /* Compute the current buffer size. */
535 buffer_length = lexer->buffer_end - lexer->buffer;
536 /* Allocate a buffer twice as big. */
537 new_buffer = ((cp_token *)
538 ggc_realloc (lexer->buffer,
539 2 * buffer_length * sizeof (cp_token)));
540
541 /* Because the buffer is circular, logically consecutive tokens
542 are not necessarily placed consecutively in memory.
543 Therefore, we must keep move the tokens that were before
544 FIRST_TOKEN to the second half of the newly allocated
545 buffer. */
546 num_tokens_to_copy = (lexer->first_token - old_buffer);
547 memcpy (new_buffer + buffer_length,
548 new_buffer,
549 num_tokens_to_copy * sizeof (cp_token));
550 /* Clear the rest of the buffer. We never look at this storage,
551 but the garbage collector may. */
552 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
553 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
554
555 /* Now recompute all of the buffer pointers. */
556 new_first_token
557 = new_buffer + (lexer->first_token - old_buffer);
558 if (lexer->next_token != NULL)
559 {
560 ptrdiff_t next_token_delta;
561
562 if (lexer->next_token > lexer->first_token)
563 next_token_delta = lexer->next_token - lexer->first_token;
564 else
565 next_token_delta =
566 buffer_length - (lexer->first_token - lexer->next_token);
567 lexer->next_token = new_first_token + next_token_delta;
568 }
569 lexer->last_token = new_first_token + buffer_length;
570 lexer->buffer = new_buffer;
571 lexer->buffer_end = new_buffer + buffer_length * 2;
572 lexer->first_token = new_first_token;
573 }
574}
575
576/* Store the next token from the preprocessor in *TOKEN. */
577
578static void
579cp_lexer_get_preprocessor_token (lexer, token)
580 cp_lexer *lexer ATTRIBUTE_UNUSED;
581 cp_token *token;
582{
583 bool done;
584
585 /* If this not the main lexer, return a terminating CPP_EOF token. */
586 if (!lexer->main_lexer_p)
587 {
588 token->type = CPP_EOF;
589 token->line_number = 0;
590 token->file_name = NULL;
591 token->value = NULL_TREE;
592 token->keyword = RID_MAX;
593
594 return;
595 }
596
597 done = false;
598 /* Keep going until we get a token we like. */
599 while (!done)
600 {
601 /* Get a new token from the preprocessor. */
602 token->type = c_lex (&token->value);
603 /* Issue messages about tokens we cannot process. */
604 switch (token->type)
605 {
606 case CPP_ATSIGN:
607 case CPP_HASH:
608 case CPP_PASTE:
609 error ("invalid token");
610 break;
611
612 case CPP_OTHER:
613 /* These tokens are already warned about by c_lex. */
614 break;
615
616 default:
617 /* This is a good token, so we exit the loop. */
618 done = true;
619 break;
620 }
621 }
622 /* Now we've got our token. */
623 token->line_number = lineno;
624 token->file_name = input_filename;
625
626 /* Check to see if this token is a keyword. */
627 if (token->type == CPP_NAME
628 && C_IS_RESERVED_WORD (token->value))
629 {
630 /* Mark this token as a keyword. */
631 token->type = CPP_KEYWORD;
632 /* Record which keyword. */
633 token->keyword = C_RID_CODE (token->value);
634 /* Update the value. Some keywords are mapped to particular
635 entities, rather than simply having the value of the
636 corresponding IDENTIFIER_NODE. For example, `__const' is
637 mapped to `const'. */
638 token->value = ridpointers[token->keyword];
639 }
640 else
641 token->keyword = RID_MAX;
642}
643
644/* Return a pointer to the next token in the token stream, but do not
645 consume it. */
646
647static cp_token *
648cp_lexer_peek_token (lexer)
649 cp_lexer *lexer;
650{
651 cp_token *token;
652
653 /* If there are no tokens, read one now. */
654 if (!lexer->next_token)
655 cp_lexer_read_token (lexer);
656
657 /* Provide debugging output. */
658 if (cp_lexer_debugging_p (lexer))
659 {
660 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
661 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
662 fprintf (cp_lexer_debug_stream, "\n");
663 }
664
665 token = lexer->next_token;
666 cp_lexer_set_source_position_from_token (lexer, token);
667 return token;
668}
669
670/* Return true if the next token has the indicated TYPE. */
671
672static bool
673cp_lexer_next_token_is (lexer, type)
674 cp_lexer *lexer;
675 enum cpp_ttype type;
676{
677 cp_token *token;
678
679 /* Peek at the next token. */
680 token = cp_lexer_peek_token (lexer);
681 /* Check to see if it has the indicated TYPE. */
682 return token->type == type;
683}
684
685/* Return true if the next token does not have the indicated TYPE. */
686
687static bool
688cp_lexer_next_token_is_not (lexer, type)
689 cp_lexer *lexer;
690 enum cpp_ttype type;
691{
692 return !cp_lexer_next_token_is (lexer, type);
693}
694
695/* Return true if the next token is the indicated KEYWORD. */
696
697static bool
698cp_lexer_next_token_is_keyword (lexer, keyword)
699 cp_lexer *lexer;
700 enum rid keyword;
701{
702 cp_token *token;
703
704 /* Peek at the next token. */
705 token = cp_lexer_peek_token (lexer);
706 /* Check to see if it is the indicated keyword. */
707 return token->keyword == keyword;
708}
709
710/* Return a pointer to the Nth token in the token stream. If N is 1,
711 then this is precisely equivalent to cp_lexer_peek_token. */
712
713static cp_token *
714cp_lexer_peek_nth_token (lexer, n)
715 cp_lexer *lexer;
716 size_t n;
717{
718 cp_token *token;
719
720 /* N is 1-based, not zero-based. */
721 my_friendly_assert (n > 0, 20000224);
722
723 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
724 token = lexer->next_token;
725 /* If there are no tokens in the buffer, get one now. */
726 if (!token)
727 {
728 cp_lexer_read_token (lexer);
729 token = lexer->next_token;
730 }
731
732 /* Now, read tokens until we have enough. */
733 while (--n > 0)
734 {
735 /* Advance to the next token. */
736 token = cp_lexer_next_token (lexer, token);
737 /* If that's all the tokens we have, read a new one. */
738 if (token == lexer->last_token)
739 token = cp_lexer_read_token (lexer);
740 }
741
742 return token;
743}
744
745/* Consume the next token. The pointer returned is valid only until
746 another token is read. Callers should preserve copy the token
747 explicitly if they will need its value for a longer period of
748 time. */
749
750static cp_token *
751cp_lexer_consume_token (lexer)
752 cp_lexer *lexer;
753{
754 cp_token *token;
755
756 /* If there are no tokens, read one now. */
757 if (!lexer->next_token)
758 cp_lexer_read_token (lexer);
759
760 /* Remember the token we'll be returning. */
761 token = lexer->next_token;
762
763 /* Increment NEXT_TOKEN. */
764 lexer->next_token = cp_lexer_next_token (lexer,
765 lexer->next_token);
766 /* Check to see if we're all out of tokens. */
767 if (lexer->next_token == lexer->last_token)
768 lexer->next_token = NULL;
769
770 /* If we're not saving tokens, then move FIRST_TOKEN too. */
771 if (!cp_lexer_saving_tokens (lexer))
772 {
773 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
774 if (!lexer->next_token)
775 lexer->first_token = NULL;
776 else
777 lexer->first_token = lexer->next_token;
778 }
779
780 /* Provide debugging output. */
781 if (cp_lexer_debugging_p (lexer))
782 {
783 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
784 cp_lexer_print_token (cp_lexer_debug_stream, token);
785 fprintf (cp_lexer_debug_stream, "\n");
786 }
787
788 return token;
789}
790
791/* Permanently remove the next token from the token stream. There
792 must be a valid next token already; this token never reads
793 additional tokens from the preprocessor. */
794
795static void
796cp_lexer_purge_token (cp_lexer *lexer)
797{
798 cp_token *token;
799 cp_token *next_token;
800
801 token = lexer->next_token;
802 while (true)
803 {
804 next_token = cp_lexer_next_token (lexer, token);
805 if (next_token == lexer->last_token)
806 break;
807 *token = *next_token;
808 token = next_token;
809 }
810
811 lexer->last_token = token;
812 /* The token purged may have been the only token remaining; if so,
813 clear NEXT_TOKEN. */
814 if (lexer->next_token == token)
815 lexer->next_token = NULL;
816}
817
818/* Permanently remove all tokens after TOKEN, up to, but not
819 including, the token that will be returned next by
820 cp_lexer_peek_token. */
821
822static void
823cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
824{
825 cp_token *peek;
826 cp_token *t1;
827 cp_token *t2;
828
829 if (lexer->next_token)
830 {
831 /* Copy the tokens that have not yet been read to the location
832 immediately following TOKEN. */
833 t1 = cp_lexer_next_token (lexer, token);
834 t2 = peek = cp_lexer_peek_token (lexer);
835 /* Move tokens into the vacant area between TOKEN and PEEK. */
836 while (t2 != lexer->last_token)
837 {
838 *t1 = *t2;
839 t1 = cp_lexer_next_token (lexer, t1);
840 t2 = cp_lexer_next_token (lexer, t2);
841 }
842 /* Now, the next available token is right after TOKEN. */
843 lexer->next_token = cp_lexer_next_token (lexer, token);
844 /* And the last token is wherever we ended up. */
845 lexer->last_token = t1;
846 }
847 else
848 {
849 /* There are no tokens in the buffer, so there is nothing to
850 copy. The last token in the buffer is TOKEN itself. */
851 lexer->last_token = cp_lexer_next_token (lexer, token);
852 }
853}
854
855/* Begin saving tokens. All tokens consumed after this point will be
856 preserved. */
857
858static void
859cp_lexer_save_tokens (lexer)
860 cp_lexer *lexer;
861{
862 /* Provide debugging output. */
863 if (cp_lexer_debugging_p (lexer))
864 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
865
866 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
867 restore the tokens if required. */
868 if (!lexer->next_token)
869 cp_lexer_read_token (lexer);
870
871 VARRAY_PUSH_INT (lexer->saved_tokens,
872 cp_lexer_token_difference (lexer,
873 lexer->first_token,
874 lexer->next_token));
875}
876
877/* Commit to the portion of the token stream most recently saved. */
878
879static void
880cp_lexer_commit_tokens (lexer)
881 cp_lexer *lexer;
882{
883 /* Provide debugging output. */
884 if (cp_lexer_debugging_p (lexer))
885 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
886
887 VARRAY_POP (lexer->saved_tokens);
888}
889
890/* Return all tokens saved since the last call to cp_lexer_save_tokens
891 to the token stream. Stop saving tokens. */
892
893static void
894cp_lexer_rollback_tokens (lexer)
895 cp_lexer *lexer;
896{
897 size_t delta;
898
899 /* Provide debugging output. */
900 if (cp_lexer_debugging_p (lexer))
901 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
902
903 /* Find the token that was the NEXT_TOKEN when we started saving
904 tokens. */
905 delta = VARRAY_TOP_INT(lexer->saved_tokens);
906 /* Make it the next token again now. */
907 lexer->next_token = cp_lexer_advance_token (lexer,
908 lexer->first_token,
909 delta);
910 /* It might be the case that there wer no tokens when we started
911 saving tokens, but that there are some tokens now. */
912 if (!lexer->next_token && lexer->first_token)
913 lexer->next_token = lexer->first_token;
914
915 /* Stop saving tokens. */
916 VARRAY_POP (lexer->saved_tokens);
917}
918
919/* Set the current source position from the information stored in
920 TOKEN. */
921
922static void
923cp_lexer_set_source_position_from_token (lexer, token)
924 cp_lexer *lexer ATTRIBUTE_UNUSED;
925 const cp_token *token;
926{
927 /* Ideally, the source position information would not be a global
928 variable, but it is. */
929
930 /* Update the line number. */
931 if (token->type != CPP_EOF)
932 {
933 lineno = token->line_number;
934 input_filename = token->file_name;
935 }
936}
937
938/* Print a representation of the TOKEN on the STREAM. */
939
940static void
941cp_lexer_print_token (stream, token)
942 FILE *stream;
943 cp_token *token;
944{
945 const char *token_type = NULL;
946
947 /* Figure out what kind of token this is. */
948 switch (token->type)
949 {
950 case CPP_EQ:
951 token_type = "EQ";
952 break;
953
954 case CPP_COMMA:
955 token_type = "COMMA";
956 break;
957
958 case CPP_OPEN_PAREN:
959 token_type = "OPEN_PAREN";
960 break;
961
962 case CPP_CLOSE_PAREN:
963 token_type = "CLOSE_PAREN";
964 break;
965
966 case CPP_OPEN_BRACE:
967 token_type = "OPEN_BRACE";
968 break;
969
970 case CPP_CLOSE_BRACE:
971 token_type = "CLOSE_BRACE";
972 break;
973
974 case CPP_SEMICOLON:
975 token_type = "SEMICOLON";
976 break;
977
978 case CPP_NAME:
979 token_type = "NAME";
980 break;
981
982 case CPP_EOF:
983 token_type = "EOF";
984 break;
985
986 case CPP_KEYWORD:
987 token_type = "keyword";
988 break;
989
990 /* This is not a token that we know how to handle yet. */
991 default:
992 break;
993 }
994
995 /* If we have a name for the token, print it out. Otherwise, we
996 simply give the numeric code. */
997 if (token_type)
998 fprintf (stream, "%s", token_type);
999 else
1000 fprintf (stream, "%d", token->type);
1001 /* And, for an identifier, print the identifier name. */
1002 if (token->type == CPP_NAME
1003 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1004 For example, `struct' is mapped to an INTEGER_CST. */
1005 || (token->type == CPP_KEYWORD
1006 && TREE_CODE (token->value) == IDENTIFIER_NODE))
1007 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1008}
1009
1010/* Returns non-zero if debugging information should be output. */
1011
1012static bool
1013cp_lexer_debugging_p (lexer)
1014 cp_lexer *lexer;
1015{
1016 return lexer->debugging_p;
1017}
1018
1019/* Start emitting debugging information. */
1020
1021static void
1022cp_lexer_start_debugging (lexer)
1023 cp_lexer *lexer;
1024{
1025 ++lexer->debugging_p;
1026}
1027
1028/* Stop emitting debugging information. */
1029
1030static void
1031cp_lexer_stop_debugging (lexer)
1032 cp_lexer *lexer;
1033{
1034 --lexer->debugging_p;
1035}
1036
1037\f
1038/* The parser. */
1039
1040/* Overview
1041 --------
1042
1043 A cp_parser parses the token stream as specified by the C++
1044 grammar. Its job is purely parsing, not semantic analysis. For
1045 example, the parser breaks the token stream into declarators,
1046 expressions, statements, and other similar syntactic constructs.
1047 It does not check that the types of the expressions on either side
1048 of an assignment-statement are compatible, or that a function is
1049 not declared with a parameter of type `void'.
1050
1051 The parser invokes routines elsewhere in the compiler to perform
1052 semantic analysis and to build up the abstract syntax tree for the
1053 code processed.
1054
1055 The parser (and the template instantiation code, which is, in a
1056 way, a close relative of parsing) are the only parts of the
1057 compiler that should be calling push_scope and pop_scope, or
1058 related functions. The parser (and template instantiation code)
1059 keeps track of what scope is presently active; everything else
1060 should simply honor that. (The code that generates static
1061 initializers may also need to set the scope, in order to check
1062 access control correctly when emitting the initializers.)
1063
1064 Methodology
1065 -----------
1066
1067 The parser is of the standard recursive-descent variety. Upcoming
1068 tokens in the token stream are examined in order to determine which
1069 production to use when parsing a non-terminal. Some C++ constructs
1070 require arbitrary look ahead to disambiguate. For example, it is
1071 impossible, in the general case, to tell whether a statement is an
1072 expression or declaration without scanning the entire statement.
1073 Therefore, the parser is capable of "parsing tentatively." When the
1074 parser is not sure what construct comes next, it enters this mode.
1075 Then, while we attempt to parse the construct, the parser queues up
1076 error messages, rather than issuing them immediately, and saves the
1077 tokens it consumes. If the construct is parsed successfully, the
1078 parser "commits", i.e., it issues any queued error messages and
1079 the tokens that were being preserved are permanently discarded.
1080 If, however, the construct is not parsed successfully, the parser
1081 rolls back its state completely so that it can resume parsing using
1082 a different alternative.
1083
1084 Future Improvements
1085 -------------------
1086
1087 The performance of the parser could probably be improved
1088 substantially. Some possible improvements include:
1089
1090 - The expression parser recurses through the various levels of
1091 precedence as specified in the grammar, rather than using an
1092 operator-precedence technique. Therefore, parsing a simple
1093 identifier requires multiple recursive calls.
1094
1095 - We could often eliminate the need to parse tentatively by
1096 looking ahead a little bit. In some places, this approach
1097 might not entirely eliminate the need to parse tentatively, but
1098 it might still speed up the average case. */
1099
1100/* Flags that are passed to some parsing functions. These values can
1101 be bitwise-ored together. */
1102
1103typedef enum cp_parser_flags
1104{
1105 /* No flags. */
1106 CP_PARSER_FLAGS_NONE = 0x0,
1107 /* The construct is optional. If it is not present, then no error
1108 should be issued. */
1109 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1110 /* When parsing a type-specifier, do not allow user-defined types. */
1111 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1112} cp_parser_flags;
1113
1114/* The different kinds of ids that we ecounter. */
1115
1116typedef enum cp_parser_id_kind
1117{
1118 /* Not an id at all. */
1119 CP_PARSER_ID_KIND_NONE,
1120 /* An unqualified-id that is not a template-id. */
1121 CP_PARSER_ID_KIND_UNQUALIFIED,
1122 /* An unqualified template-id. */
1123 CP_PARSER_ID_KIND_TEMPLATE_ID,
1124 /* A qualified-id. */
1125 CP_PARSER_ID_KIND_QUALIFIED
1126} cp_parser_id_kind;
1127
1128/* A mapping from a token type to a corresponding tree node type. */
1129
1130typedef struct cp_parser_token_tree_map_node
1131{
1132 /* The token type. */
1133 enum cpp_ttype token_type;
1134 /* The corresponding tree code. */
1135 enum tree_code tree_type;
1136} cp_parser_token_tree_map_node;
1137
1138/* A complete map consists of several ordinary entries, followed by a
1139 terminator. The terminating entry has a token_type of CPP_EOF. */
1140
1141typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1142
1143/* The status of a tentative parse. */
1144
1145typedef enum cp_parser_status_kind
1146{
1147 /* No errors have occurred. */
1148 CP_PARSER_STATUS_KIND_NO_ERROR,
1149 /* An error has occurred. */
1150 CP_PARSER_STATUS_KIND_ERROR,
1151 /* We are committed to this tentative parse, whether or not an error
1152 has occurred. */
1153 CP_PARSER_STATUS_KIND_COMMITTED
1154} cp_parser_status_kind;
1155
1156/* Context that is saved and restored when parsing tentatively. */
1157
1158typedef struct cp_parser_context GTY (())
1159{
1160 /* If this is a tentative parsing context, the status of the
1161 tentative parse. */
1162 enum cp_parser_status_kind status;
1163 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1164 that are looked up in this context must be looked up both in the
1165 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1166 the context of the containing expression. */
1167 tree object_type;
1168 /* A TREE_LIST representing name-lookups for which we have deferred
1169 checking access controls. We cannot check the accessibility of
1170 names used in a decl-specifier-seq until we know what is being
1171 declared because code like:
1172
1173 class A {
1174 class B {};
1175 B* f();
1176 }
1177
1178 A::B* A::f() { return 0; }
1179
1180 is valid, even though `A::B' is not generally accessible.
1181
1182 The TREE_PURPOSE of each node is the scope used to qualify the
1183 name being looked up; the TREE_VALUE is the DECL to which the
1184 name was resolved. */
1185 tree deferred_access_checks;
1186 /* TRUE iff we are deferring access checks. */
1187 bool deferring_access_checks_p;
1188 /* The next parsing context in the stack. */
1189 struct cp_parser_context *next;
1190} cp_parser_context;
1191
1192/* Prototypes. */
1193
1194/* Constructors and destructors. */
1195
1196static cp_parser_context *cp_parser_context_new
1197 PARAMS ((cp_parser_context *));
1198
1199/* Constructors and destructors. */
1200
1201/* Construct a new context. The context below this one on the stack
1202 is given by NEXT. */
1203
1204static cp_parser_context *
1205cp_parser_context_new (next)
1206 cp_parser_context *next;
1207{
1208 cp_parser_context *context;
1209
1210 /* Allocate the storage. */
1211 context = ((cp_parser_context *)
1212 ggc_alloc_cleared (sizeof (cp_parser_context)));
1213 /* No errors have occurred yet in this context. */
1214 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1215 /* If this is not the bottomost context, copy information that we
1216 need from the previous context. */
1217 if (next)
1218 {
1219 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1220 expression, then we are parsing one in this context, too. */
1221 context->object_type = next->object_type;
1222 /* We are deferring access checks here if we were in the NEXT
1223 context. */
1224 context->deferring_access_checks_p
1225 = next->deferring_access_checks_p;
1226 /* Thread the stack. */
1227 context->next = next;
1228 }
1229
1230 return context;
1231}
1232
1233/* The cp_parser structure represents the C++ parser. */
1234
1235typedef struct cp_parser GTY(())
1236{
1237 /* The lexer from which we are obtaining tokens. */
1238 cp_lexer *lexer;
1239
1240 /* The scope in which names should be looked up. If NULL_TREE, then
1241 we look up names in the scope that is currently open in the
1242 source program. If non-NULL, this is either a TYPE or
1243 NAMESPACE_DECL for the scope in which we should look.
1244
1245 This value is not cleared automatically after a name is looked
1246 up, so we must be careful to clear it before starting a new look
1247 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1248 will look up `Z' in the scope of `X', rather than the current
1249 scope.) Unfortunately, it is difficult to tell when name lookup
1250 is complete, because we sometimes peek at a token, look it up,
1251 and then decide not to consume it. */
1252 tree scope;
1253
1254 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1255 last lookup took place. OBJECT_SCOPE is used if an expression
1256 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1257 respectively. QUALIFYING_SCOPE is used for an expression of the
1258 form "X::Y"; it refers to X. */
1259 tree object_scope;
1260 tree qualifying_scope;
1261
1262 /* A stack of parsing contexts. All but the bottom entry on the
1263 stack will be tentative contexts.
1264
1265 We parse tentatively in order to determine which construct is in
1266 use in some situations. For example, in order to determine
1267 whether a statement is an expression-statement or a
1268 declaration-statement we parse it tentatively as a
1269 declaration-statement. If that fails, we then reparse the same
1270 token stream as an expression-statement. */
1271 cp_parser_context *context;
1272
1273 /* True if we are parsing GNU C++. If this flag is not set, then
1274 GNU extensions are not recognized. */
1275 bool allow_gnu_extensions_p;
1276
1277 /* TRUE if the `>' token should be interpreted as the greater-than
1278 operator. FALSE if it is the end of a template-id or
1279 template-parameter-list. */
1280 bool greater_than_is_operator_p;
1281
1282 /* TRUE if default arguments are allowed within a parameter list
1283 that starts at this point. FALSE if only a gnu extension makes
1284 them permissable. */
1285 bool default_arg_ok_p;
1286
1287 /* TRUE if we are parsing an integral constant-expression. See
1288 [expr.const] for a precise definition. */
1289 /* FIXME: Need to implement code that checks this flag. */
1290 bool constant_expression_p;
1291
1292 /* TRUE if local variable names and `this' are forbidden in the
1293 current context. */
1294 bool local_variables_forbidden_p;
1295
1296 /* TRUE if the declaration we are parsing is part of a
1297 linkage-specification of the form `extern string-literal
1298 declaration'. */
1299 bool in_unbraced_linkage_specification_p;
1300
1301 /* TRUE if we are presently parsing a declarator, after the
1302 direct-declarator. */
1303 bool in_declarator_p;
1304
1305 /* If non-NULL, then we are parsing a construct where new type
1306 definitions are not permitted. The string stored here will be
1307 issued as an error message if a type is defined. */
1308 const char *type_definition_forbidden_message;
1309
1310 /* List of FUNCTION_TYPEs which contain unprocessed DEFAULT_ARGs
1311 during class parsing, and are not FUNCTION_DECLs. G++ has an
1312 awkward extension allowing default args on pointers to functions
1313 etc. */
1314 tree default_arg_types;
1315
1316 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1317 but may not have been parsed. These functions are friends of
1318 members defined within a class-specification; they are not
1319 procssed until the class is complete. The active queue is at the
1320 front of the list.
1321
1322 Within each queue, functions appear in the reverse order that
1323 they appeared in the source. The TREE_PURPOSE of each node is
1324 the class in which the function was defined or declared; the
1325 TREE_VALUE is the FUNCTION_DECL itself. */
1326 tree unparsed_functions_queues;
1327
1328 /* The number of classes whose definitions are currently in
1329 progress. */
1330 unsigned num_classes_being_defined;
1331
1332 /* The number of template parameter lists that apply directly to the
1333 current declaration. */
1334 unsigned num_template_parameter_lists;
1335} cp_parser;
1336
1337/* The type of a function that parses some kind of expression */
1338typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1339
1340/* Prototypes. */
1341
1342/* Constructors and destructors. */
1343
1344static cp_parser *cp_parser_new
1345 PARAMS ((void));
1346
1347/* Routines to parse various constructs.
1348
1349 Those that return `tree' will return the error_mark_node (rather
1350 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1351 Sometimes, they will return an ordinary node if error-recovery was
1352 attempted, even though a parse error occurrred. So, to check
1353 whether or not a parse error occurred, you should always use
1354 cp_parser_error_occurred. If the construct is optional (indicated
1355 either by an `_opt' in the name of the function that does the
1356 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1357 the construct is not present. */
1358
1359/* Lexical conventions [gram.lex] */
1360
1361static tree cp_parser_identifier
1362 PARAMS ((cp_parser *));
1363
1364/* Basic concepts [gram.basic] */
1365
1366static bool cp_parser_translation_unit
1367 PARAMS ((cp_parser *));
1368
1369/* Expressions [gram.expr] */
1370
1371static tree cp_parser_primary_expression
1372 (cp_parser *, cp_parser_id_kind *, tree *);
1373static tree cp_parser_id_expression
1374 PARAMS ((cp_parser *, bool, bool, bool *));
1375static tree cp_parser_unqualified_id
1376 PARAMS ((cp_parser *, bool, bool));
1377static tree cp_parser_nested_name_specifier_opt
1378 (cp_parser *, bool, bool, bool);
1379static tree cp_parser_nested_name_specifier
1380 (cp_parser *, bool, bool, bool);
1381static tree cp_parser_class_or_namespace_name
1382 (cp_parser *, bool, bool, bool, bool);
1383static tree cp_parser_postfix_expression
1384 (cp_parser *, bool);
1385static tree cp_parser_expression_list
1386 PARAMS ((cp_parser *));
1387static void cp_parser_pseudo_destructor_name
1388 PARAMS ((cp_parser *, tree *, tree *));
1389static tree cp_parser_unary_expression
1390 (cp_parser *, bool);
1391static enum tree_code cp_parser_unary_operator
1392 PARAMS ((cp_token *));
1393static tree cp_parser_new_expression
1394 PARAMS ((cp_parser *));
1395static tree cp_parser_new_placement
1396 PARAMS ((cp_parser *));
1397static tree cp_parser_new_type_id
1398 PARAMS ((cp_parser *));
1399static tree cp_parser_new_declarator_opt
1400 PARAMS ((cp_parser *));
1401static tree cp_parser_direct_new_declarator
1402 PARAMS ((cp_parser *));
1403static tree cp_parser_new_initializer
1404 PARAMS ((cp_parser *));
1405static tree cp_parser_delete_expression
1406 PARAMS ((cp_parser *));
1407static tree cp_parser_cast_expression
1408 (cp_parser *, bool);
1409static tree cp_parser_pm_expression
1410 PARAMS ((cp_parser *));
1411static tree cp_parser_multiplicative_expression
1412 PARAMS ((cp_parser *));
1413static tree cp_parser_additive_expression
1414 PARAMS ((cp_parser *));
1415static tree cp_parser_shift_expression
1416 PARAMS ((cp_parser *));
1417static tree cp_parser_relational_expression
1418 PARAMS ((cp_parser *));
1419static tree cp_parser_equality_expression
1420 PARAMS ((cp_parser *));
1421static tree cp_parser_and_expression
1422 PARAMS ((cp_parser *));
1423static tree cp_parser_exclusive_or_expression
1424 PARAMS ((cp_parser *));
1425static tree cp_parser_inclusive_or_expression
1426 PARAMS ((cp_parser *));
1427static tree cp_parser_logical_and_expression
1428 PARAMS ((cp_parser *));
1429static tree cp_parser_logical_or_expression
1430 PARAMS ((cp_parser *));
1431static tree cp_parser_conditional_expression
1432 PARAMS ((cp_parser *));
1433static tree cp_parser_question_colon_clause
1434 PARAMS ((cp_parser *, tree));
1435static tree cp_parser_assignment_expression
1436 PARAMS ((cp_parser *));
1437static enum tree_code cp_parser_assignment_operator_opt
1438 PARAMS ((cp_parser *));
1439static tree cp_parser_expression
1440 PARAMS ((cp_parser *));
1441static tree cp_parser_constant_expression
1442 PARAMS ((cp_parser *));
1443
1444/* Statements [gram.stmt.stmt] */
1445
1446static void cp_parser_statement
1447 PARAMS ((cp_parser *));
1448static tree cp_parser_labeled_statement
1449 PARAMS ((cp_parser *));
1450static tree cp_parser_expression_statement
1451 PARAMS ((cp_parser *));
1452static tree cp_parser_compound_statement
1453 (cp_parser *);
1454static void cp_parser_statement_seq_opt
1455 PARAMS ((cp_parser *));
1456static tree cp_parser_selection_statement
1457 PARAMS ((cp_parser *));
1458static tree cp_parser_condition
1459 PARAMS ((cp_parser *));
1460static tree cp_parser_iteration_statement
1461 PARAMS ((cp_parser *));
1462static void cp_parser_for_init_statement
1463 PARAMS ((cp_parser *));
1464static tree cp_parser_jump_statement
1465 PARAMS ((cp_parser *));
1466static void cp_parser_declaration_statement
1467 PARAMS ((cp_parser *));
1468
1469static tree cp_parser_implicitly_scoped_statement
1470 PARAMS ((cp_parser *));
1471static void cp_parser_already_scoped_statement
1472 PARAMS ((cp_parser *));
1473
1474/* Declarations [gram.dcl.dcl] */
1475
1476static void cp_parser_declaration_seq_opt
1477 PARAMS ((cp_parser *));
1478static void cp_parser_declaration
1479 PARAMS ((cp_parser *));
1480static void cp_parser_block_declaration
1481 PARAMS ((cp_parser *, bool));
1482static void cp_parser_simple_declaration
1483 PARAMS ((cp_parser *, bool));
1484static tree cp_parser_decl_specifier_seq
1485 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1486static tree cp_parser_storage_class_specifier_opt
1487 PARAMS ((cp_parser *));
1488static tree cp_parser_function_specifier_opt
1489 PARAMS ((cp_parser *));
1490static tree cp_parser_type_specifier
1491 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1492static tree cp_parser_simple_type_specifier
1493 PARAMS ((cp_parser *, cp_parser_flags));
1494static tree cp_parser_type_name
1495 PARAMS ((cp_parser *));
1496static tree cp_parser_elaborated_type_specifier
1497 PARAMS ((cp_parser *, bool, bool));
1498static tree cp_parser_enum_specifier
1499 PARAMS ((cp_parser *));
1500static void cp_parser_enumerator_list
1501 PARAMS ((cp_parser *, tree));
1502static void cp_parser_enumerator_definition
1503 PARAMS ((cp_parser *, tree));
1504static tree cp_parser_namespace_name
1505 PARAMS ((cp_parser *));
1506static void cp_parser_namespace_definition
1507 PARAMS ((cp_parser *));
1508static void cp_parser_namespace_body
1509 PARAMS ((cp_parser *));
1510static tree cp_parser_qualified_namespace_specifier
1511 PARAMS ((cp_parser *));
1512static void cp_parser_namespace_alias_definition
1513 PARAMS ((cp_parser *));
1514static void cp_parser_using_declaration
1515 PARAMS ((cp_parser *));
1516static void cp_parser_using_directive
1517 PARAMS ((cp_parser *));
1518static void cp_parser_asm_definition
1519 PARAMS ((cp_parser *));
1520static void cp_parser_linkage_specification
1521 PARAMS ((cp_parser *));
1522
1523/* Declarators [gram.dcl.decl] */
1524
1525static tree cp_parser_init_declarator
1526 PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1527static tree cp_parser_declarator
1528 PARAMS ((cp_parser *, bool, bool *));
1529static tree cp_parser_direct_declarator
1530 PARAMS ((cp_parser *, bool, bool *));
1531static enum tree_code cp_parser_ptr_operator
1532 PARAMS ((cp_parser *, tree *, tree *));
1533static tree cp_parser_cv_qualifier_seq_opt
1534 PARAMS ((cp_parser *));
1535static tree cp_parser_cv_qualifier_opt
1536 PARAMS ((cp_parser *));
1537static tree cp_parser_declarator_id
1538 PARAMS ((cp_parser *));
1539static tree cp_parser_type_id
1540 PARAMS ((cp_parser *));
1541static tree cp_parser_type_specifier_seq
1542 PARAMS ((cp_parser *));
1543static tree cp_parser_parameter_declaration_clause
1544 PARAMS ((cp_parser *));
1545static tree cp_parser_parameter_declaration_list
1546 PARAMS ((cp_parser *));
1547static tree cp_parser_parameter_declaration
1548 PARAMS ((cp_parser *, bool));
1549static tree cp_parser_function_definition
1550 PARAMS ((cp_parser *, bool *));
1551static void cp_parser_function_body
1552 (cp_parser *);
1553static tree cp_parser_initializer
1554 PARAMS ((cp_parser *, bool *));
1555static tree cp_parser_initializer_clause
1556 PARAMS ((cp_parser *));
1557static tree cp_parser_initializer_list
1558 PARAMS ((cp_parser *));
1559
1560static bool cp_parser_ctor_initializer_opt_and_function_body
1561 (cp_parser *);
1562
1563/* Classes [gram.class] */
1564
1565static tree cp_parser_class_name
1566 (cp_parser *, bool, bool, bool, bool, bool, bool);
1567static tree cp_parser_class_specifier
1568 PARAMS ((cp_parser *));
1569static tree cp_parser_class_head
1570 PARAMS ((cp_parser *, bool *, bool *, tree *));
1571static enum tag_types cp_parser_class_key
1572 PARAMS ((cp_parser *));
1573static void cp_parser_member_specification_opt
1574 PARAMS ((cp_parser *));
1575static void cp_parser_member_declaration
1576 PARAMS ((cp_parser *));
1577static tree cp_parser_pure_specifier
1578 PARAMS ((cp_parser *));
1579static tree cp_parser_constant_initializer
1580 PARAMS ((cp_parser *));
1581
1582/* Derived classes [gram.class.derived] */
1583
1584static tree cp_parser_base_clause
1585 PARAMS ((cp_parser *));
1586static tree cp_parser_base_specifier
1587 PARAMS ((cp_parser *));
1588
1589/* Special member functions [gram.special] */
1590
1591static tree cp_parser_conversion_function_id
1592 PARAMS ((cp_parser *));
1593static tree cp_parser_conversion_type_id
1594 PARAMS ((cp_parser *));
1595static tree cp_parser_conversion_declarator_opt
1596 PARAMS ((cp_parser *));
1597static bool cp_parser_ctor_initializer_opt
1598 PARAMS ((cp_parser *));
1599static void cp_parser_mem_initializer_list
1600 PARAMS ((cp_parser *));
1601static tree cp_parser_mem_initializer
1602 PARAMS ((cp_parser *));
1603static tree cp_parser_mem_initializer_id
1604 PARAMS ((cp_parser *));
1605
1606/* Overloading [gram.over] */
1607
1608static tree cp_parser_operator_function_id
1609 PARAMS ((cp_parser *));
1610static tree cp_parser_operator
1611 PARAMS ((cp_parser *));
1612
1613/* Templates [gram.temp] */
1614
1615static void cp_parser_template_declaration
1616 PARAMS ((cp_parser *, bool));
1617static tree cp_parser_template_parameter_list
1618 PARAMS ((cp_parser *));
1619static tree cp_parser_template_parameter
1620 PARAMS ((cp_parser *));
1621static tree cp_parser_type_parameter
1622 PARAMS ((cp_parser *));
1623static tree cp_parser_template_id
1624 PARAMS ((cp_parser *, bool, bool));
1625static tree cp_parser_template_name
1626 PARAMS ((cp_parser *, bool, bool));
1627static tree cp_parser_template_argument_list
1628 PARAMS ((cp_parser *));
1629static tree cp_parser_template_argument
1630 PARAMS ((cp_parser *));
1631static void cp_parser_explicit_instantiation
1632 PARAMS ((cp_parser *));
1633static void cp_parser_explicit_specialization
1634 PARAMS ((cp_parser *));
1635
1636/* Exception handling [gram.exception] */
1637
1638static tree cp_parser_try_block
1639 PARAMS ((cp_parser *));
1640static bool cp_parser_function_try_block
1641 PARAMS ((cp_parser *));
1642static void cp_parser_handler_seq
1643 PARAMS ((cp_parser *));
1644static void cp_parser_handler
1645 PARAMS ((cp_parser *));
1646static tree cp_parser_exception_declaration
1647 PARAMS ((cp_parser *));
1648static tree cp_parser_throw_expression
1649 PARAMS ((cp_parser *));
1650static tree cp_parser_exception_specification_opt
1651 PARAMS ((cp_parser *));
1652static tree cp_parser_type_id_list
1653 PARAMS ((cp_parser *));
1654
1655/* GNU Extensions */
1656
1657static tree cp_parser_asm_specification_opt
1658 PARAMS ((cp_parser *));
1659static tree cp_parser_asm_operand_list
1660 PARAMS ((cp_parser *));
1661static tree cp_parser_asm_clobber_list
1662 PARAMS ((cp_parser *));
1663static tree cp_parser_attributes_opt
1664 PARAMS ((cp_parser *));
1665static tree cp_parser_attribute_list
1666 PARAMS ((cp_parser *));
1667static bool cp_parser_extension_opt
1668 PARAMS ((cp_parser *, int *));
1669static void cp_parser_label_declaration
1670 PARAMS ((cp_parser *));
1671
1672/* Utility Routines */
1673
1674static tree cp_parser_lookup_name
1675 PARAMS ((cp_parser *, tree, bool, bool, bool));
1676static tree cp_parser_lookup_name_simple
1677 PARAMS ((cp_parser *, tree));
1678static tree cp_parser_resolve_typename_type
1679 PARAMS ((cp_parser *, tree));
1680static tree cp_parser_maybe_treat_template_as_class
1681 (tree, bool);
1682static bool cp_parser_check_declarator_template_parameters
1683 PARAMS ((cp_parser *, tree));
1684static bool cp_parser_check_template_parameters
1685 PARAMS ((cp_parser *, unsigned));
1686static tree cp_parser_binary_expression
1687 PARAMS ((cp_parser *,
1688 cp_parser_token_tree_map,
1689 cp_parser_expression_fn));
1690static tree cp_parser_global_scope_opt
1691 PARAMS ((cp_parser *, bool));
1692static bool cp_parser_constructor_declarator_p
1693 (cp_parser *, bool);
1694static tree cp_parser_function_definition_from_specifiers_and_declarator
1695 PARAMS ((cp_parser *, tree, tree, tree, tree));
1696static tree cp_parser_function_definition_after_declarator
1697 PARAMS ((cp_parser *, bool));
1698static void cp_parser_template_declaration_after_export
1699 PARAMS ((cp_parser *, bool));
1700static tree cp_parser_single_declaration
1701 PARAMS ((cp_parser *, bool, bool *));
1702static tree cp_parser_functional_cast
1703 PARAMS ((cp_parser *, tree));
1704static void cp_parser_late_parsing_for_member
1705 PARAMS ((cp_parser *, tree));
1706static void cp_parser_late_parsing_default_args
1707 PARAMS ((cp_parser *, tree));
1708static tree cp_parser_sizeof_operand
1709 PARAMS ((cp_parser *, enum rid));
1710static bool cp_parser_declares_only_class_p
1711 PARAMS ((cp_parser *));
1712static bool cp_parser_friend_p
1713 PARAMS ((tree));
1714static cp_token *cp_parser_require
1715 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1716static cp_token *cp_parser_require_keyword
1717 PARAMS ((cp_parser *, enum rid, const char *));
1718static bool cp_parser_token_starts_function_definition_p
1719 PARAMS ((cp_token *));
1720static bool cp_parser_next_token_starts_class_definition_p
1721 (cp_parser *);
1722static enum tag_types cp_parser_token_is_class_key
1723 PARAMS ((cp_token *));
1724static void cp_parser_check_class_key
1725 (enum tag_types, tree type);
1726static bool cp_parser_optional_template_keyword
1727 (cp_parser *);
1728static void cp_parser_cache_group
1729 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1730static void cp_parser_parse_tentatively
1731 PARAMS ((cp_parser *));
1732static void cp_parser_commit_to_tentative_parse
1733 PARAMS ((cp_parser *));
1734static void cp_parser_abort_tentative_parse
1735 PARAMS ((cp_parser *));
1736static bool cp_parser_parse_definitely
1737 PARAMS ((cp_parser *));
1738static bool cp_parser_parsing_tentatively
1739 PARAMS ((cp_parser *));
1740static bool cp_parser_committed_to_tentative_parse
1741 PARAMS ((cp_parser *));
1742static void cp_parser_error
1743 PARAMS ((cp_parser *, const char *));
1744static void cp_parser_simulate_error
1745 PARAMS ((cp_parser *));
1746static void cp_parser_check_type_definition
1747 PARAMS ((cp_parser *));
1748static bool cp_parser_skip_to_closing_parenthesis
1749 PARAMS ((cp_parser *));
1750static bool cp_parser_skip_to_closing_parenthesis_or_comma
1751 (cp_parser *);
1752static void cp_parser_skip_to_end_of_statement
1753 PARAMS ((cp_parser *));
1754static void cp_parser_skip_to_end_of_block_or_statement
1755 PARAMS ((cp_parser *));
1756static void cp_parser_skip_to_closing_brace
1757 (cp_parser *);
1758static void cp_parser_skip_until_found
1759 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1760static bool cp_parser_error_occurred
1761 PARAMS ((cp_parser *));
1762static bool cp_parser_allow_gnu_extensions_p
1763 PARAMS ((cp_parser *));
1764static bool cp_parser_is_string_literal
1765 PARAMS ((cp_token *));
1766static bool cp_parser_is_keyword
1767 PARAMS ((cp_token *, enum rid));
1768static bool cp_parser_dependent_type_p
1769 (tree);
1770static bool cp_parser_value_dependent_expression_p
1771 (tree);
1772static bool cp_parser_type_dependent_expression_p
1773 (tree);
1774static bool cp_parser_dependent_template_arg_p
1775 (tree);
1776static bool cp_parser_dependent_template_id_p
1777 (tree, tree);
1778static bool cp_parser_dependent_template_p
1779 (tree);
1780static void cp_parser_defer_access_check
1781 (cp_parser *, tree, tree);
1782static void cp_parser_start_deferring_access_checks
1783 (cp_parser *);
1784static tree cp_parser_stop_deferring_access_checks
1785 PARAMS ((cp_parser *));
1786static void cp_parser_perform_deferred_access_checks
1787 PARAMS ((tree));
1788static tree cp_parser_scope_through_which_access_occurs
1789 (tree, tree, tree);
1790
1791/* Returns non-zero if TOKEN is a string literal. */
1792
1793static bool
1794cp_parser_is_string_literal (token)
1795 cp_token *token;
1796{
1797 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1798}
1799
1800/* Returns non-zero if TOKEN is the indicated KEYWORD. */
1801
1802static bool
1803cp_parser_is_keyword (token, keyword)
1804 cp_token *token;
1805 enum rid keyword;
1806{
1807 return token->keyword == keyword;
1808}
1809
1810/* Returns TRUE if TYPE is dependent, in the sense of
1811 [temp.dep.type]. */
1812
1813static bool
1814cp_parser_dependent_type_p (type)
1815 tree type;
1816{
1817 tree scope;
1818
1819 if (!processing_template_decl)
1820 return false;
1821
1822 /* If the type is NULL, we have not computed a type for the entity
1823 in question; in that case, the type is dependent. */
1824 if (!type)
1825 return true;
1826
1827 /* Erroneous types can be considered non-dependent. */
1828 if (type == error_mark_node)
1829 return false;
1830
1831 /* [temp.dep.type]
1832
1833 A type is dependent if it is:
1834
1835 -- a template parameter. */
1836 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1837 return true;
1838 /* -- a qualified-id with a nested-name-specifier which contains a
1839 class-name that names a dependent type or whose unqualified-id
1840 names a dependent type. */
1841 if (TREE_CODE (type) == TYPENAME_TYPE)
1842 return true;
1843 /* -- a cv-qualified type where the cv-unqualified type is
1844 dependent. */
1845 type = TYPE_MAIN_VARIANT (type);
1846 /* -- a compound type constructed from any dependent type. */
1847 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1848 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1849 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1850 (type)));
1851 else if (TREE_CODE (type) == POINTER_TYPE
1852 || TREE_CODE (type) == REFERENCE_TYPE)
1853 return cp_parser_dependent_type_p (TREE_TYPE (type));
1854 else if (TREE_CODE (type) == FUNCTION_TYPE
1855 || TREE_CODE (type) == METHOD_TYPE)
1856 {
1857 tree arg_type;
1858
1859 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1860 return true;
1861 for (arg_type = TYPE_ARG_TYPES (type);
1862 arg_type;
1863 arg_type = TREE_CHAIN (arg_type))
1864 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1865 return true;
1866 return false;
1867 }
1868 /* -- an array type constructed from any dependent type or whose
1869 size is specified by a constant expression that is
1870 value-dependent. */
1871 if (TREE_CODE (type) == ARRAY_TYPE)
1872 {
1873 if (TYPE_DOMAIN (TREE_TYPE (type))
1874 && ((cp_parser_value_dependent_expression_p
1875 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1876 || (cp_parser_type_dependent_expression_p
1877 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1878 return true;
1879 return cp_parser_dependent_type_p (TREE_TYPE (type));
1880 }
1881 /* -- a template-id in which either the template name is a template
1882 parameter or any of the template arguments is a dependent type or
1883 an expression that is type-dependent or value-dependent.
1884
1885 This language seems somewhat confused; for example, it does not
1886 discuss template template arguments. Therefore, we use the
1887 definition for dependent template arguments in [temp.dep.temp]. */
1888 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1889 && (cp_parser_dependent_template_id_p
1890 (CLASSTYPE_TI_TEMPLATE (type),
1891 CLASSTYPE_TI_ARGS (type))))
1892 return true;
1893 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1894 return true;
1895 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1896 expression is not type-dependent, then it should already been
1897 have resolved. */
1898 if (TREE_CODE (type) == TYPEOF_TYPE)
1899 return true;
1900 /* The standard does not specifically mention types that are local
1901 to template functions or local classes, but they should be
1902 considered dependent too. For example:
1903
1904 template <int I> void f() {
1905 enum E { a = I };
1906 S<sizeof (E)> s;
1907 }
1908
1909 The size of `E' cannot be known until the value of `I' has been
1910 determined. Therefore, `E' must be considered dependent. */
1911 scope = TYPE_CONTEXT (type);
1912 if (scope && TYPE_P (scope))
1913 return cp_parser_dependent_type_p (scope);
1914 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1915 return cp_parser_type_dependent_expression_p (scope);
1916
1917 /* Other types are non-dependent. */
1918 return false;
1919}
1920
1921/* Returns TRUE if the EXPRESSION is value-dependent. */
1922
1923static bool
1924cp_parser_value_dependent_expression_p (tree expression)
1925{
1926 if (!processing_template_decl)
1927 return false;
1928
1929 /* A name declared with a dependent type. */
1930 if (DECL_P (expression)
1931 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1932 return true;
1933 /* A non-type template parameter. */
1934 if ((TREE_CODE (expression) == CONST_DECL
1935 && DECL_TEMPLATE_PARM_P (expression))
1936 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1937 return true;
1938 /* A constant with integral or enumeration type and is initialized
1939 with an expression that is value-dependent. */
1940 if (TREE_CODE (expression) == VAR_DECL
1941 && DECL_INITIAL (expression)
1942 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1943 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1944 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1945 return true;
1946 /* These expressions are value-dependent if the type to which the
1947 cast occurs is dependent. */
1948 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1949 || TREE_CODE (expression) == STATIC_CAST_EXPR
1950 || TREE_CODE (expression) == CONST_CAST_EXPR
1951 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1952 || TREE_CODE (expression) == CAST_EXPR)
1953 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1954 return true;
1955 /* A `sizeof' expression where the sizeof operand is a type is
1956 value-dependent if the type is dependent. If the type was not
1957 dependent, we would no longer have a SIZEOF_EXPR, so any
1958 SIZEOF_EXPR is dependent. */
1959 if (TREE_CODE (expression) == SIZEOF_EXPR)
1960 return true;
1961 /* A constant expression is value-dependent if any subexpression is
1962 value-dependent. */
1963 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1964 {
1965 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1966 {
1967 case '1':
1968 return (cp_parser_value_dependent_expression_p
1969 (TREE_OPERAND (expression, 0)));
1970 case '<':
1971 case '2':
1972 return ((cp_parser_value_dependent_expression_p
1973 (TREE_OPERAND (expression, 0)))
1974 || (cp_parser_value_dependent_expression_p
1975 (TREE_OPERAND (expression, 1))));
1976 case 'e':
1977 {
1978 int i;
1979 for (i = 0;
1980 i < TREE_CODE_LENGTH (TREE_CODE (expression));
1981 ++i)
1982 if (cp_parser_value_dependent_expression_p
1983 (TREE_OPERAND (expression, i)))
1984 return true;
1985 return false;
1986 }
1987 }
1988 }
1989
1990 /* The expression is not value-dependent. */
1991 return false;
1992}
1993
1994/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
1995 [temp.dep.expr]. */
1996
1997static bool
1998cp_parser_type_dependent_expression_p (expression)
1999 tree expression;
2000{
2001 if (!processing_template_decl)
2002 return false;
2003
2004 /* Some expression forms are never type-dependent. */
2005 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2006 || TREE_CODE (expression) == SIZEOF_EXPR
2007 || TREE_CODE (expression) == ALIGNOF_EXPR
2008 || TREE_CODE (expression) == TYPEID_EXPR
2009 || TREE_CODE (expression) == DELETE_EXPR
2010 || TREE_CODE (expression) == VEC_DELETE_EXPR
2011 || TREE_CODE (expression) == THROW_EXPR)
2012 return false;
2013
2014 /* The types of these expressions depends only on the type to which
2015 the cast occurs. */
2016 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2017 || TREE_CODE (expression) == STATIC_CAST_EXPR
2018 || TREE_CODE (expression) == CONST_CAST_EXPR
2019 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2020 || TREE_CODE (expression) == CAST_EXPR)
2021 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2022 /* The types of these expressions depends only on the type created
2023 by the expression. */
2024 else if (TREE_CODE (expression) == NEW_EXPR
2025 || TREE_CODE (expression) == VEC_NEW_EXPR)
2026 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2027
2028 if (TREE_CODE (expression) == FUNCTION_DECL
2029 && DECL_LANG_SPECIFIC (expression)
2030 && DECL_TEMPLATE_INFO (expression)
2031 && (cp_parser_dependent_template_id_p
2032 (DECL_TI_TEMPLATE (expression),
2033 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2034 return true;
2035
2036 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2037}
2038
2039/* Returns TRUE if the ARG (a template argument) is dependent. */
2040
2041static bool
2042cp_parser_dependent_template_arg_p (tree arg)
2043{
2044 if (!processing_template_decl)
2045 return false;
2046
2047 if (TREE_CODE (arg) == TEMPLATE_DECL
2048 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2049 return cp_parser_dependent_template_p (arg);
2050 else if (TYPE_P (arg))
2051 return cp_parser_dependent_type_p (arg);
2052 else
2053 return (cp_parser_type_dependent_expression_p (arg)
2054 || cp_parser_value_dependent_expression_p (arg));
2055}
2056
2057/* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2058
2059static bool
2060cp_parser_dependent_template_id_p (tree tmpl, tree args)
2061{
2062 int i;
2063
2064 if (cp_parser_dependent_template_p (tmpl))
2065 return true;
2066 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2067 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2068 return true;
2069 return false;
2070}
2071
2072/* Returns TRUE if the template TMPL is dependent. */
2073
2074static bool
2075cp_parser_dependent_template_p (tree tmpl)
2076{
2077 /* Template template parameters are dependent. */
2078 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2079 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2080 return true;
2081 /* So are member templates of dependent classes. */
2082 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2083 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2084 return false;
2085}
2086
2087/* Defer checking the accessibility of DECL, when looked up in
2088 CLASS_TYPE. */
2089
2090static void
2091cp_parser_defer_access_check (cp_parser *parser,
2092 tree class_type,
2093 tree decl)
2094{
2095 tree check;
2096
2097 /* If we are not supposed to defer access checks, just check now. */
2098 if (!parser->context->deferring_access_checks_p)
2099 {
2100 enforce_access (class_type, decl);
2101 return;
2102 }
2103
2104 /* See if we are already going to perform this check. */
2105 for (check = parser->context->deferred_access_checks;
2106 check;
2107 check = TREE_CHAIN (check))
2108 if (TREE_VALUE (check) == decl
2109 && same_type_p (TREE_PURPOSE (check), class_type))
2110 return;
2111 /* If not, record the check. */
2112 parser->context->deferred_access_checks
2113 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2114}
2115
2116/* Start deferring access control checks. */
2117
2118static void
2119cp_parser_start_deferring_access_checks (cp_parser *parser)
2120{
2121 parser->context->deferring_access_checks_p = true;
2122}
2123
2124/* Stop deferring access control checks. Returns a TREE_LIST
2125 representing the deferred checks. The TREE_PURPOSE of each node is
2126 the type through which the access occurred; the TREE_VALUE is the
2127 declaration named. */
2128
2129static tree
2130cp_parser_stop_deferring_access_checks (parser)
2131 cp_parser *parser;
2132{
2133 tree access_checks;
2134
2135 parser->context->deferring_access_checks_p = false;
2136 access_checks = parser->context->deferred_access_checks;
2137 parser->context->deferred_access_checks = NULL_TREE;
2138
2139 return access_checks;
2140}
2141
2142/* Perform the deferred ACCESS_CHECKS, whose representation is as
2143 documented with cp_parser_stop_deferrring_access_checks. */
2144
2145static void
2146cp_parser_perform_deferred_access_checks (access_checks)
2147 tree access_checks;
2148{
2149 tree deferred_check;
2150
2151 /* Look through all the deferred checks. */
2152 for (deferred_check = access_checks;
2153 deferred_check;
2154 deferred_check = TREE_CHAIN (deferred_check))
2155 /* Check access. */
2156 enforce_access (TREE_PURPOSE (deferred_check),
2157 TREE_VALUE (deferred_check));
2158}
2159
2160/* Returns the scope through which DECL is being accessed, or
2161 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2162 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2163 or `x', respectively. If the DECL was named as `A::B' then
2164 NESTED_NAME_SPECIFIER is `A'. */
2165
2166tree
2167cp_parser_scope_through_which_access_occurs (decl,
2168 object_type,
2169 nested_name_specifier)
2170 tree decl;
2171 tree object_type;
2172 tree nested_name_specifier;
2173{
2174 tree scope;
2175 tree qualifying_type = NULL_TREE;
2176
2177 /* Determine the SCOPE of DECL. */
2178 scope = context_for_name_lookup (decl);
2179 /* If the SCOPE is not a type, then DECL is not a member. */
2180 if (!TYPE_P (scope))
2181 return NULL_TREE;
2182 /* Figure out the type through which DECL is being accessed. */
2183 if (object_type && DERIVED_FROM_P (scope, object_type))
2184 /* If we are processing a `->' or `.' expression, use the type of the
2185 left-hand side. */
2186 qualifying_type = object_type;
2187 else if (nested_name_specifier)
2188 {
2189 /* If the reference is to a non-static member of the
2190 current class, treat it as if it were referenced through
2191 `this'. */
2192 if (DECL_NONSTATIC_MEMBER_P (decl)
2193 && current_class_ptr
2194 && DERIVED_FROM_P (scope, current_class_type))
2195 qualifying_type = current_class_type;
2196 /* Otherwise, use the type indicated by the
2197 nested-name-specifier. */
2198 else
2199 qualifying_type = nested_name_specifier;
2200 }
2201 else
2202 /* Otherwise, the name must be from the current class or one of
2203 its bases. */
2204 qualifying_type = currently_open_derived_class (scope);
2205
2206 return qualifying_type;
2207}
2208
2209/* Issue the indicated error MESSAGE. */
2210
2211static void
2212cp_parser_error (parser, message)
2213 cp_parser *parser;
2214 const char *message;
2215{
2216 /* Remember that we have issued an error. */
2217 cp_parser_simulate_error (parser);
2218 /* Output the MESSAGE -- unless we're parsing tentatively. */
2219 if (!cp_parser_parsing_tentatively (parser)
2220 || cp_parser_committed_to_tentative_parse (parser))
2221 error (message);
2222}
2223
2224/* If we are parsing tentatively, remember that an error has occurred
2225 during this tentative parse. */
2226
2227static void
2228cp_parser_simulate_error (parser)
2229 cp_parser *parser;
2230{
2231 if (cp_parser_parsing_tentatively (parser)
2232 && !cp_parser_committed_to_tentative_parse (parser))
2233 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2234}
2235
2236/* This function is called when a type is defined. If type
2237 definitions are forbidden at this point, an error message is
2238 issued. */
2239
2240static void
2241cp_parser_check_type_definition (parser)
2242 cp_parser *parser;
2243{
2244 /* If types are forbidden here, issue a message. */
2245 if (parser->type_definition_forbidden_message)
2246 /* Use `%s' to print the string in case there are any escape
2247 characters in the message. */
2248 error ("%s", parser->type_definition_forbidden_message);
2249}
2250
2251/* Consume tokens up to, and including, the next non-nested closing `)'.
2252 Returns TRUE iff we found a closing `)'. */
2253
2254static bool
2255cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2256{
2257 unsigned nesting_depth = 0;
2258
2259 while (true)
2260 {
2261 cp_token *token;
2262
2263 /* If we've run out of tokens, then there is no closing `)'. */
2264 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2265 return false;
2266 /* Consume the token. */
2267 token = cp_lexer_consume_token (parser->lexer);
2268 /* If it is an `(', we have entered another level of nesting. */
2269 if (token->type == CPP_OPEN_PAREN)
2270 ++nesting_depth;
2271 /* If it is a `)', then we might be done. */
2272 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2273 return true;
2274 }
2275}
2276
2277/* Consume tokens until the next token is a `)', or a `,'. Returns
2278 TRUE if the next token is a `,'. */
2279
2280static bool
2281cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2282{
2283 unsigned nesting_depth = 0;
2284
2285 while (true)
2286 {
2287 cp_token *token = cp_lexer_peek_token (parser->lexer);
2288
2289 /* If we've run out of tokens, then there is no closing `)'. */
2290 if (token->type == CPP_EOF)
2291 return false;
2292 /* If it is a `,' stop. */
2293 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2294 return true;
2295 /* If it is a `)', stop. */
2296 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2297 return false;
2298 /* If it is an `(', we have entered another level of nesting. */
2299 else if (token->type == CPP_OPEN_PAREN)
2300 ++nesting_depth;
2301 /* Consume the token. */
2302 token = cp_lexer_consume_token (parser->lexer);
2303 }
2304}
2305
2306/* Consume tokens until we reach the end of the current statement.
2307 Normally, that will be just before consuming a `;'. However, if a
2308 non-nested `}' comes first, then we stop before consuming that. */
2309
2310static void
2311cp_parser_skip_to_end_of_statement (parser)
2312 cp_parser *parser;
2313{
2314 unsigned nesting_depth = 0;
2315
2316 while (true)
2317 {
2318 cp_token *token;
2319
2320 /* Peek at the next token. */
2321 token = cp_lexer_peek_token (parser->lexer);
2322 /* If we've run out of tokens, stop. */
2323 if (token->type == CPP_EOF)
2324 break;
2325 /* If the next token is a `;', we have reached the end of the
2326 statement. */
2327 if (token->type == CPP_SEMICOLON && !nesting_depth)
2328 break;
2329 /* If the next token is a non-nested `}', then we have reached
2330 the end of the current block. */
2331 if (token->type == CPP_CLOSE_BRACE)
2332 {
2333 /* If this is a non-nested `}', stop before consuming it.
2334 That way, when confronted with something like:
2335
2336 { 3 + }
2337
2338 we stop before consuming the closing `}', even though we
2339 have not yet reached a `;'. */
2340 if (nesting_depth == 0)
2341 break;
2342 /* If it is the closing `}' for a block that we have
2343 scanned, stop -- but only after consuming the token.
2344 That way given:
2345
2346 void f g () { ... }
2347 typedef int I;
2348
2349 we will stop after the body of the erroneously declared
2350 function, but before consuming the following `typedef'
2351 declaration. */
2352 if (--nesting_depth == 0)
2353 {
2354 cp_lexer_consume_token (parser->lexer);
2355 break;
2356 }
2357 }
2358 /* If it the next token is a `{', then we are entering a new
2359 block. Consume the entire block. */
2360 else if (token->type == CPP_OPEN_BRACE)
2361 ++nesting_depth;
2362 /* Consume the token. */
2363 cp_lexer_consume_token (parser->lexer);
2364 }
2365}
2366
2367/* Skip tokens until we have consumed an entire block, or until we
2368 have consumed a non-nested `;'. */
2369
2370static void
2371cp_parser_skip_to_end_of_block_or_statement (parser)
2372 cp_parser *parser;
2373{
2374 unsigned nesting_depth = 0;
2375
2376 while (true)
2377 {
2378 cp_token *token;
2379
2380 /* Peek at the next token. */
2381 token = cp_lexer_peek_token (parser->lexer);
2382 /* If we've run out of tokens, stop. */
2383 if (token->type == CPP_EOF)
2384 break;
2385 /* If the next token is a `;', we have reached the end of the
2386 statement. */
2387 if (token->type == CPP_SEMICOLON && !nesting_depth)
2388 {
2389 /* Consume the `;'. */
2390 cp_lexer_consume_token (parser->lexer);
2391 break;
2392 }
2393 /* Consume the token. */
2394 token = cp_lexer_consume_token (parser->lexer);
2395 /* If the next token is a non-nested `}', then we have reached
2396 the end of the current block. */
2397 if (token->type == CPP_CLOSE_BRACE
2398 && (nesting_depth == 0 || --nesting_depth == 0))
2399 break;
2400 /* If it the next token is a `{', then we are entering a new
2401 block. Consume the entire block. */
2402 if (token->type == CPP_OPEN_BRACE)
2403 ++nesting_depth;
2404 }
2405}
2406
2407/* Skip tokens until a non-nested closing curly brace is the next
2408 token. */
2409
2410static void
2411cp_parser_skip_to_closing_brace (cp_parser *parser)
2412{
2413 unsigned nesting_depth = 0;
2414
2415 while (true)
2416 {
2417 cp_token *token;
2418
2419 /* Peek at the next token. */
2420 token = cp_lexer_peek_token (parser->lexer);
2421 /* If we've run out of tokens, stop. */
2422 if (token->type == CPP_EOF)
2423 break;
2424 /* If the next token is a non-nested `}', then we have reached
2425 the end of the current block. */
2426 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2427 break;
2428 /* If it the next token is a `{', then we are entering a new
2429 block. Consume the entire block. */
2430 else if (token->type == CPP_OPEN_BRACE)
2431 ++nesting_depth;
2432 /* Consume the token. */
2433 cp_lexer_consume_token (parser->lexer);
2434 }
2435}
2436
2437/* Create a new C++ parser. */
2438
2439static cp_parser *
2440cp_parser_new ()
2441{
2442 cp_parser *parser;
2443
2444 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2445 parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2446 parser->context = cp_parser_context_new (NULL);
2447
2448 /* For now, we always accept GNU extensions. */
2449 parser->allow_gnu_extensions_p = 1;
2450
2451 /* The `>' token is a greater-than operator, not the end of a
2452 template-id. */
2453 parser->greater_than_is_operator_p = true;
2454
2455 parser->default_arg_ok_p = true;
2456
2457 /* We are not parsing a constant-expression. */
2458 parser->constant_expression_p = false;
2459
2460 /* Local variable names are not forbidden. */
2461 parser->local_variables_forbidden_p = false;
2462
2463 /* We are not procesing an `extern "C"' declaration. */
2464 parser->in_unbraced_linkage_specification_p = false;
2465
2466 /* We are not processing a declarator. */
2467 parser->in_declarator_p = false;
2468
2469 /* There are no default args to process. */
2470 parser->default_arg_types = NULL;
2471
2472 /* The unparsed function queue is empty. */
2473 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2474
2475 /* There are no classes being defined. */
2476 parser->num_classes_being_defined = 0;
2477
2478 /* No template parameters apply. */
2479 parser->num_template_parameter_lists = 0;
2480
2481 return parser;
2482}
2483
2484/* Lexical conventions [gram.lex] */
2485
2486/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2487 identifier. */
2488
2489static tree
2490cp_parser_identifier (parser)
2491 cp_parser *parser;
2492{
2493 cp_token *token;
2494
2495 /* Look for the identifier. */
2496 token = cp_parser_require (parser, CPP_NAME, "identifier");
2497 /* Return the value. */
2498 return token ? token->value : error_mark_node;
2499}
2500
2501/* Basic concepts [gram.basic] */
2502
2503/* Parse a translation-unit.
2504
2505 translation-unit:
2506 declaration-seq [opt]
2507
2508 Returns TRUE if all went well. */
2509
2510static bool
2511cp_parser_translation_unit (parser)
2512 cp_parser *parser;
2513{
2514 while (true)
2515 {
2516 cp_parser_declaration_seq_opt (parser);
2517
2518 /* If there are no tokens left then all went well. */
2519 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2520 break;
2521
2522 /* Otherwise, issue an error message. */
2523 cp_parser_error (parser, "expected declaration");
2524 return false;
2525 }
2526
2527 /* Consume the EOF token. */
2528 cp_parser_require (parser, CPP_EOF, "end-of-file");
2529
2530 /* Finish up. */
2531 finish_translation_unit ();
2532
2533 /* All went well. */
2534 return true;
2535}
2536
2537/* Expressions [gram.expr] */
2538
2539/* Parse a primary-expression.
2540
2541 primary-expression:
2542 literal
2543 this
2544 ( expression )
2545 id-expression
2546
2547 GNU Extensions:
2548
2549 primary-expression:
2550 ( compound-statement )
2551 __builtin_va_arg ( assignment-expression , type-id )
2552
2553 literal:
2554 __null
2555
2556 Returns a representation of the expression.
2557
2558 *IDK indicates what kind of id-expression (if any) was present.
2559
2560 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2561 used as the operand of a pointer-to-member. In that case,
2562 *QUALIFYING_CLASS gives the class that is used as the qualifying
2563 class in the pointer-to-member. */
2564
2565static tree
2566cp_parser_primary_expression (cp_parser *parser,
2567 cp_parser_id_kind *idk,
2568 tree *qualifying_class)
2569{
2570 cp_token *token;
2571
2572 /* Assume the primary expression is not an id-expression. */
2573 *idk = CP_PARSER_ID_KIND_NONE;
2574 /* And that it cannot be used as pointer-to-member. */
2575 *qualifying_class = NULL_TREE;
2576
2577 /* Peek at the next token. */
2578 token = cp_lexer_peek_token (parser->lexer);
2579 switch (token->type)
2580 {
2581 /* literal:
2582 integer-literal
2583 character-literal
2584 floating-literal
2585 string-literal
2586 boolean-literal */
2587 case CPP_CHAR:
2588 case CPP_WCHAR:
2589 case CPP_STRING:
2590 case CPP_WSTRING:
2591 case CPP_NUMBER:
2592 token = cp_lexer_consume_token (parser->lexer);
2593 return token->value;
2594
2595 case CPP_OPEN_PAREN:
2596 {
2597 tree expr;
2598 bool saved_greater_than_is_operator_p;
2599
2600 /* Consume the `('. */
2601 cp_lexer_consume_token (parser->lexer);
2602 /* Within a parenthesized expression, a `>' token is always
2603 the greater-than operator. */
2604 saved_greater_than_is_operator_p
2605 = parser->greater_than_is_operator_p;
2606 parser->greater_than_is_operator_p = true;
2607 /* If we see `( { ' then we are looking at the beginning of
2608 a GNU statement-expression. */
2609 if (cp_parser_allow_gnu_extensions_p (parser)
2610 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2611 {
2612 /* Statement-expressions are not allowed by the standard. */
2613 if (pedantic)
2614 pedwarn ("ISO C++ forbids braced-groups within expressions");
2615
2616 /* And they're not allowed outside of a function-body; you
2617 cannot, for example, write:
2618
2619 int i = ({ int j = 3; j + 1; });
2620
2621 at class or namespace scope. */
2622 if (!at_function_scope_p ())
2623 error ("statement-expressions are allowed only inside functions");
2624 /* Start the statement-expression. */
2625 expr = begin_stmt_expr ();
2626 /* Parse the compound-statement. */
2627 cp_parser_compound_statement (parser);
2628 /* Finish up. */
2629 expr = finish_stmt_expr (expr);
2630 }
2631 else
2632 {
2633 /* Parse the parenthesized expression. */
2634 expr = cp_parser_expression (parser);
2635 /* Let the front end know that this expression was
2636 enclosed in parentheses. This matters in case, for
2637 example, the expression is of the form `A::B', since
2638 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2639 not. */
2640 finish_parenthesized_expr (expr);
2641 }
2642 /* The `>' token might be the end of a template-id or
2643 template-parameter-list now. */
2644 parser->greater_than_is_operator_p
2645 = saved_greater_than_is_operator_p;
2646 /* Consume the `)'. */
2647 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2648 cp_parser_skip_to_end_of_statement (parser);
2649
2650 return expr;
2651 }
2652
2653 case CPP_KEYWORD:
2654 switch (token->keyword)
2655 {
2656 /* These two are the boolean literals. */
2657 case RID_TRUE:
2658 cp_lexer_consume_token (parser->lexer);
2659 return boolean_true_node;
2660 case RID_FALSE:
2661 cp_lexer_consume_token (parser->lexer);
2662 return boolean_false_node;
2663
2664 /* The `__null' literal. */
2665 case RID_NULL:
2666 cp_lexer_consume_token (parser->lexer);
2667 return null_node;
2668
2669 /* Recognize the `this' keyword. */
2670 case RID_THIS:
2671 cp_lexer_consume_token (parser->lexer);
2672 if (parser->local_variables_forbidden_p)
2673 {
2674 error ("`this' may not be used in this context");
2675 return error_mark_node;
2676 }
2677 return finish_this_expr ();
2678
2679 /* The `operator' keyword can be the beginning of an
2680 id-expression. */
2681 case RID_OPERATOR:
2682 goto id_expression;
2683
2684 case RID_FUNCTION_NAME:
2685 case RID_PRETTY_FUNCTION_NAME:
2686 case RID_C99_FUNCTION_NAME:
2687 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2688 __func__ are the names of variables -- but they are
2689 treated specially. Therefore, they are handled here,
2690 rather than relying on the generic id-expression logic
2691 below. Gramatically, these names are id-expressions.
2692
2693 Consume the token. */
2694 token = cp_lexer_consume_token (parser->lexer);
2695 /* Look up the name. */
2696 return finish_fname (token->value);
2697
2698 case RID_VA_ARG:
2699 {
2700 tree expression;
2701 tree type;
2702
2703 /* The `__builtin_va_arg' construct is used to handle
2704 `va_arg'. Consume the `__builtin_va_arg' token. */
2705 cp_lexer_consume_token (parser->lexer);
2706 /* Look for the opening `('. */
2707 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2708 /* Now, parse the assignment-expression. */
2709 expression = cp_parser_assignment_expression (parser);
2710 /* Look for the `,'. */
2711 cp_parser_require (parser, CPP_COMMA, "`,'");
2712 /* Parse the type-id. */
2713 type = cp_parser_type_id (parser);
2714 /* Look for the closing `)'. */
2715 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2716
2717 return build_x_va_arg (expression, type);
2718 }
2719
2720 default:
2721 cp_parser_error (parser, "expected primary-expression");
2722 return error_mark_node;
2723 }
2724 /* Fall through. */
2725
2726 /* An id-expression can start with either an identifier, a
2727 `::' as the beginning of a qualified-id, or the "operator"
2728 keyword. */
2729 case CPP_NAME:
2730 case CPP_SCOPE:
2731 case CPP_TEMPLATE_ID:
2732 case CPP_NESTED_NAME_SPECIFIER:
2733 {
2734 tree id_expression;
2735 tree decl;
2736
2737 id_expression:
2738 /* Parse the id-expression. */
2739 id_expression
2740 = cp_parser_id_expression (parser,
2741 /*template_keyword_p=*/false,
2742 /*check_dependency_p=*/true,
2743 /*template_p=*/NULL);
2744 if (id_expression == error_mark_node)
2745 return error_mark_node;
2746 /* If we have a template-id, then no further lookup is
2747 required. If the template-id was for a template-class, we
2748 will sometimes have a TYPE_DECL at this point. */
2749 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2750 || TREE_CODE (id_expression) == TYPE_DECL)
2751 decl = id_expression;
2752 /* Look up the name. */
2753 else
2754 {
2755 decl = cp_parser_lookup_name_simple (parser, id_expression);
2756 /* If name lookup gives us a SCOPE_REF, then the
2757 qualifying scope was dependent. Just propagate the
2758 name. */
2759 if (TREE_CODE (decl) == SCOPE_REF)
2760 {
2761 if (TYPE_P (TREE_OPERAND (decl, 0)))
2762 *qualifying_class = TREE_OPERAND (decl, 0);
2763 return decl;
2764 }
2765 /* Check to see if DECL is a local variable in a context
2766 where that is forbidden. */
2767 if (parser->local_variables_forbidden_p
2768 && local_variable_p (decl))
2769 {
2770 /* It might be that we only found DECL because we are
2771 trying to be generous with pre-ISO scoping rules.
2772 For example, consider:
2773
2774 int i;
2775 void g() {
2776 for (int i = 0; i < 10; ++i) {}
2777 extern void f(int j = i);
2778 }
2779
2780 Here, name look up will originally find the out
2781 of scope `i'. We need to issue a warning message,
2782 but then use the global `i'. */
2783 decl = check_for_out_of_scope_variable (decl);
2784 if (local_variable_p (decl))
2785 {
2786 error ("local variable `%D' may not appear in this context",
2787 decl);
2788 return error_mark_node;
2789 }
2790 }
2791
2792 /* If unqualified name lookup fails while processing a
2793 template, that just means that we need to do name
2794 lookup again when the template is instantiated. */
2795 if (!parser->scope
2796 && decl == error_mark_node
2797 && processing_template_decl)
2798 {
2799 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2800 return build_min_nt (LOOKUP_EXPR, id_expression);
2801 }
2802 else if (decl == error_mark_node
2803 && !processing_template_decl)
2804 {
2805 if (!parser->scope)
2806 {
2807 /* It may be resolvable as a koenig lookup function
2808 call. */
2809 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2810 return id_expression;
2811 }
2812 else if (TYPE_P (parser->scope)
2813 && !COMPLETE_TYPE_P (parser->scope))
2814 error ("incomplete type `%T' used in nested name specifier",
2815 parser->scope);
2816 else if (parser->scope != global_namespace)
2817 error ("`%D' is not a member of `%D'",
2818 id_expression, parser->scope);
2819 else
2820 error ("`::%D' has not been declared", id_expression);
2821 }
2822 /* If DECL is a variable would be out of scope under
2823 ANSI/ISO rules, but in scope in the ARM, name lookup
2824 will succeed. Issue a diagnostic here. */
2825 else
2826 decl = check_for_out_of_scope_variable (decl);
2827
2828 /* Remember that the name was used in the definition of
2829 the current class so that we can check later to see if
2830 the meaning would have been different after the class
2831 was entirely defined. */
2832 if (!parser->scope && decl != error_mark_node)
2833 maybe_note_name_used_in_class (id_expression, decl);
2834 }
2835
2836 /* If we didn't find anything, or what we found was a type,
2837 then this wasn't really an id-expression. */
2838 if (TREE_CODE (decl) == TYPE_DECL
2839 || TREE_CODE (decl) == NAMESPACE_DECL
2840 || (TREE_CODE (decl) == TEMPLATE_DECL
2841 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2842 {
2843 cp_parser_error (parser,
2844 "expected primary-expression");
2845 return error_mark_node;
2846 }
2847
2848 /* If the name resolved to a template parameter, there is no
2849 need to look it up again later. Similarly, we resolve
2850 enumeration constants to their underlying values. */
2851 if (TREE_CODE (decl) == CONST_DECL)
2852 {
2853 *idk = CP_PARSER_ID_KIND_NONE;
2854 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2855 return DECL_INITIAL (decl);
2856 return decl;
2857 }
2858 else
2859 {
2860 bool dependent_p;
2861
2862 /* If the declaration was explicitly qualified indicate
2863 that. The semantics of `A::f(3)' are different than
2864 `f(3)' if `f' is virtual. */
2865 *idk = (parser->scope
2866 ? CP_PARSER_ID_KIND_QUALIFIED
2867 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2868 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2869 : CP_PARSER_ID_KIND_UNQUALIFIED));
2870
2871
2872 /* [temp.dep.expr]
2873
2874 An id-expression is type-dependent if it contains an
2875 identifier that was declared with a dependent type.
2876
2877 As an optimization, we could choose not to create a
2878 LOOKUP_EXPR for a name that resolved to a local
2879 variable in the template function that we are currently
2880 declaring; such a name cannot ever resolve to anything
2881 else. If we did that we would not have to look up
2882 these names at instantiation time.
2883
2884 The standard is not very specific about an
2885 id-expression that names a set of overloaded functions.
2886 What if some of them have dependent types and some of
2887 them do not? Presumably, such a name should be treated
2888 as a dependent name. */
2889 /* Assume the name is not dependent. */
2890 dependent_p = false;
2891 if (!processing_template_decl)
2892 /* No names are dependent outside a template. */
2893 ;
2894 /* A template-id where the name of the template was not
2895 resolved is definitely dependent. */
2896 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2897 && (TREE_CODE (TREE_OPERAND (decl, 0))
2898 == IDENTIFIER_NODE))
2899 dependent_p = true;
2900 /* For anything except an overloaded function, just check
2901 its type. */
2902 else if (!is_overloaded_fn (decl))
2903 dependent_p
2904 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2905 /* For a set of overloaded functions, check each of the
2906 functions. */
2907 else
2908 {
2909 tree fns = decl;
2910
2911 if (BASELINK_P (fns))
2912 fns = BASELINK_FUNCTIONS (fns);
2913
2914 /* For a template-id, check to see if the template
2915 arguments are dependent. */
2916 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2917 {
2918 tree args = TREE_OPERAND (fns, 1);
2919
2920 if (args && TREE_CODE (args) == TREE_LIST)
2921 {
2922 while (args)
2923 {
2924 if (cp_parser_dependent_template_arg_p
2925 (TREE_VALUE (args)))
2926 {
2927 dependent_p = true;
2928 break;
2929 }
2930 args = TREE_CHAIN (args);
2931 }
2932 }
2933 else if (args && TREE_CODE (args) == TREE_VEC)
2934 {
2935 int i;
2936 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2937 if (cp_parser_dependent_template_arg_p
2938 (TREE_VEC_ELT (args, i)))
2939 {
2940 dependent_p = true;
2941 break;
2942 }
2943 }
2944
2945 /* The functions are those referred to by the
2946 template-id. */
2947 fns = TREE_OPERAND (fns, 0);
2948 }
2949
2950 /* If there are no dependent template arguments, go
2951 through the overlaoded functions. */
2952 while (fns && !dependent_p)
2953 {
2954 tree fn = OVL_CURRENT (fns);
2955
2956 /* Member functions of dependent classes are
2957 dependent. */
2958 if (TREE_CODE (fn) == FUNCTION_DECL
2959 && cp_parser_type_dependent_expression_p (fn))
2960 dependent_p = true;
2961 else if (TREE_CODE (fn) == TEMPLATE_DECL
2962 && cp_parser_dependent_template_p (fn))
2963 dependent_p = true;
2964
2965 fns = OVL_NEXT (fns);
2966 }
2967 }
2968
2969 /* If the name was dependent on a template parameter,
2970 we will resolve the name at instantiation time. */
2971 if (dependent_p)
2972 {
2973 /* Create a SCOPE_REF for qualified names. */
2974 if (parser->scope)
2975 {
2976 if (TYPE_P (parser->scope))
2977 *qualifying_class = parser->scope;
2978 return build_nt (SCOPE_REF,
2979 parser->scope,
2980 id_expression);
2981 }
2982 /* A TEMPLATE_ID already contains all the information
2983 we need. */
2984 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2985 return id_expression;
2986 /* Create a LOOKUP_EXPR for other unqualified names. */
2987 return build_min_nt (LOOKUP_EXPR, id_expression);
2988 }
2989
2990 if (parser->scope)
2991 {
2992 decl = (adjust_result_of_qualified_name_lookup
2993 (decl, parser->scope, current_class_type));
2994 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2995 *qualifying_class = parser->scope;
2996 }
2997 /* Resolve references to variables of anonymous unions
2998 into COMPONENT_REFs. */
2999 else if (TREE_CODE (decl) == ALIAS_DECL)
3000 decl = DECL_INITIAL (decl);
3001 else
3002 /* Transform references to non-static data members into
3003 COMPONENT_REFs. */
3004 decl = hack_identifier (decl, id_expression);
3005 }
3006
3007 if (TREE_DEPRECATED (decl))
3008 warn_deprecated_use (decl);
3009
3010 return decl;
3011 }
3012
3013 /* Anything else is an error. */
3014 default:
3015 cp_parser_error (parser, "expected primary-expression");
3016 return error_mark_node;
3017 }
3018}
3019
3020/* Parse an id-expression.
3021
3022 id-expression:
3023 unqualified-id
3024 qualified-id
3025
3026 qualified-id:
3027 :: [opt] nested-name-specifier template [opt] unqualified-id
3028 :: identifier
3029 :: operator-function-id
3030 :: template-id
3031
3032 Return a representation of the unqualified portion of the
3033 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3034 a `::' or nested-name-specifier.
3035
3036 Often, if the id-expression was a qualified-id, the caller will
3037 want to make a SCOPE_REF to represent the qualified-id. This
3038 function does not do this in order to avoid wastefully creating
3039 SCOPE_REFs when they are not required.
3040
3041 If ASSUME_TYPENAME_P is true then we assume that qualified names
3042 are typenames. This flag is set when parsing a declarator-id;
3043 for something like:
3044
3045 template <class T>
3046 int S<T>::R::i = 3;
3047
3048 we are supposed to assume that `S<T>::R' is a class.
3049
3050 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3051 `template' keyword.
3052
3053 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3054 uninstantiated templates.
3055
3056 If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3057 `template' keyword is used to explicitly indicate that the entity
3058 named is a template. */
3059
3060static tree
3061cp_parser_id_expression (cp_parser *parser,
3062 bool template_keyword_p,
3063 bool check_dependency_p,
3064 bool *template_p)
3065{
3066 bool global_scope_p;
3067 bool nested_name_specifier_p;
3068
3069 /* Assume the `template' keyword was not used. */
3070 if (template_p)
3071 *template_p = false;
3072
3073 /* Look for the optional `::' operator. */
3074 global_scope_p
3075 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3076 != NULL_TREE);
3077 /* Look for the optional nested-name-specifier. */
3078 nested_name_specifier_p
3079 = (cp_parser_nested_name_specifier_opt (parser,
3080 /*typename_keyword_p=*/false,
3081 check_dependency_p,
3082 /*type_p=*/false)
3083 != NULL_TREE);
3084 /* If there is a nested-name-specifier, then we are looking at
3085 the first qualified-id production. */
3086 if (nested_name_specifier_p)
3087 {
3088 tree saved_scope;
3089 tree saved_object_scope;
3090 tree saved_qualifying_scope;
3091 tree unqualified_id;
3092 bool is_template;
3093
3094 /* See if the next token is the `template' keyword. */
3095 if (!template_p)
3096 template_p = &is_template;
3097 *template_p = cp_parser_optional_template_keyword (parser);
3098 /* Name lookup we do during the processing of the
3099 unqualified-id might obliterate SCOPE. */
3100 saved_scope = parser->scope;
3101 saved_object_scope = parser->object_scope;
3102 saved_qualifying_scope = parser->qualifying_scope;
3103 /* Process the final unqualified-id. */
3104 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3105 check_dependency_p);
3106 /* Restore the SAVED_SCOPE for our caller. */
3107 parser->scope = saved_scope;
3108 parser->object_scope = saved_object_scope;
3109 parser->qualifying_scope = saved_qualifying_scope;
3110
3111 return unqualified_id;
3112 }
3113 /* Otherwise, if we are in global scope, then we are looking at one
3114 of the other qualified-id productions. */
3115 else if (global_scope_p)
3116 {
3117 cp_token *token;
3118 tree id;
3119
3120 /* We don't know yet whether or not this will be a
3121 template-id. */
3122 cp_parser_parse_tentatively (parser);
3123 /* Try a template-id. */
3124 id = cp_parser_template_id (parser,
3125 /*template_keyword_p=*/false,
3126 /*check_dependency_p=*/true);
3127 /* If that worked, we're done. */
3128 if (cp_parser_parse_definitely (parser))
3129 return id;
3130
3131 /* Peek at the next token. */
3132 token = cp_lexer_peek_token (parser->lexer);
3133
3134 switch (token->type)
3135 {
3136 case CPP_NAME:
3137 return cp_parser_identifier (parser);
3138
3139 case CPP_KEYWORD:
3140 if (token->keyword == RID_OPERATOR)
3141 return cp_parser_operator_function_id (parser);
3142 /* Fall through. */
3143
3144 default:
3145 cp_parser_error (parser, "expected id-expression");
3146 return error_mark_node;
3147 }
3148 }
3149 else
3150 return cp_parser_unqualified_id (parser, template_keyword_p,
3151 /*check_dependency_p=*/true);
3152}
3153
3154/* Parse an unqualified-id.
3155
3156 unqualified-id:
3157 identifier
3158 operator-function-id
3159 conversion-function-id
3160 ~ class-name
3161 template-id
3162
3163 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3164 keyword, in a construct like `A::template ...'.
3165
3166 Returns a representation of unqualified-id. For the `identifier'
3167 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3168 production a BIT_NOT_EXPR is returned; the operand of the
3169 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3170 other productions, see the documentation accompanying the
3171 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3172 names are looked up in uninstantiated templates. */
3173
3174static tree
3175cp_parser_unqualified_id (parser, template_keyword_p,
3176 check_dependency_p)
3177 cp_parser *parser;
3178 bool template_keyword_p;
3179 bool check_dependency_p;
3180{
3181 cp_token *token;
3182
3183 /* Peek at the next token. */
3184 token = cp_lexer_peek_token (parser->lexer);
3185
3186 switch (token->type)
3187 {
3188 case CPP_NAME:
3189 {
3190 tree id;
3191
3192 /* We don't know yet whether or not this will be a
3193 template-id. */
3194 cp_parser_parse_tentatively (parser);
3195 /* Try a template-id. */
3196 id = cp_parser_template_id (parser, template_keyword_p,
3197 check_dependency_p);
3198 /* If it worked, we're done. */
3199 if (cp_parser_parse_definitely (parser))
3200 return id;
3201 /* Otherwise, it's an ordinary identifier. */
3202 return cp_parser_identifier (parser);
3203 }
3204
3205 case CPP_TEMPLATE_ID:
3206 return cp_parser_template_id (parser, template_keyword_p,
3207 check_dependency_p);
3208
3209 case CPP_COMPL:
3210 {
3211 tree type_decl;
3212 tree qualifying_scope;
3213 tree object_scope;
3214 tree scope;
3215
3216 /* Consume the `~' token. */
3217 cp_lexer_consume_token (parser->lexer);
3218 /* Parse the class-name. The standard, as written, seems to
3219 say that:
3220
3221 template <typename T> struct S { ~S (); };
3222 template <typename T> S<T>::~S() {}
3223
3224 is invalid, since `~' must be followed by a class-name, but
3225 `S<T>' is dependent, and so not known to be a class.
3226 That's not right; we need to look in uninstantiated
3227 templates. A further complication arises from:
3228
3229 template <typename T> void f(T t) {
3230 t.T::~T();
3231 }
3232
3233 Here, it is not possible to look up `T' in the scope of `T'
3234 itself. We must look in both the current scope, and the
3235 scope of the containing complete expression.
3236
3237 Yet another issue is:
3238
3239 struct S {
3240 int S;
3241 ~S();
3242 };
3243
3244 S::~S() {}
3245
3246 The standard does not seem to say that the `S' in `~S'
3247 should refer to the type `S' and not the data member
3248 `S::S'. */
3249
3250 /* DR 244 says that we look up the name after the "~" in the
3251 same scope as we looked up the qualifying name. That idea
3252 isn't fully worked out; it's more complicated than that. */
3253 scope = parser->scope;
3254 object_scope = parser->object_scope;
3255 qualifying_scope = parser->qualifying_scope;
3256
3257 /* If the name is of the form "X::~X" it's OK. */
3258 if (scope && TYPE_P (scope)
3259 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3260 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3261 == CPP_OPEN_PAREN)
3262 && (cp_lexer_peek_token (parser->lexer)->value
3263 == TYPE_IDENTIFIER (scope)))
3264 {
3265 cp_lexer_consume_token (parser->lexer);
3266 return build_nt (BIT_NOT_EXPR, scope);
3267 }
3268
3269 /* If there was an explicit qualification (S::~T), first look
3270 in the scope given by the qualification (i.e., S). */
3271 if (scope)
3272 {
3273 cp_parser_parse_tentatively (parser);
3274 type_decl = cp_parser_class_name (parser,
3275 /*typename_keyword_p=*/false,
3276 /*template_keyword_p=*/false,
3277 /*type_p=*/false,
3278 /*check_access_p=*/true,
3279 /*check_dependency=*/false,
3280 /*class_head_p=*/false);
3281 if (cp_parser_parse_definitely (parser))
3282 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3283 }
3284 /* In "N::S::~S", look in "N" as well. */
3285 if (scope && qualifying_scope)
3286 {
3287 cp_parser_parse_tentatively (parser);
3288 parser->scope = qualifying_scope;
3289 parser->object_scope = NULL_TREE;
3290 parser->qualifying_scope = NULL_TREE;
3291 type_decl
3292 = cp_parser_class_name (parser,
3293 /*typename_keyword_p=*/false,
3294 /*template_keyword_p=*/false,
3295 /*type_p=*/false,
3296 /*check_access_p=*/true,
3297 /*check_dependency=*/false,
3298 /*class_head_p=*/false);
3299 if (cp_parser_parse_definitely (parser))
3300 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3301 }
3302 /* In "p->S::~T", look in the scope given by "*p" as well. */
3303 else if (object_scope)
3304 {
3305 cp_parser_parse_tentatively (parser);
3306 parser->scope = object_scope;
3307 parser->object_scope = NULL_TREE;
3308 parser->qualifying_scope = NULL_TREE;
3309 type_decl
3310 = cp_parser_class_name (parser,
3311 /*typename_keyword_p=*/false,
3312 /*template_keyword_p=*/false,
3313 /*type_p=*/false,
3314 /*check_access_p=*/true,
3315 /*check_dependency=*/false,
3316 /*class_head_p=*/false);
3317 if (cp_parser_parse_definitely (parser))
3318 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3319 }
3320 /* Look in the surrounding context. */
3321 parser->scope = NULL_TREE;
3322 parser->object_scope = NULL_TREE;
3323 parser->qualifying_scope = NULL_TREE;
3324 type_decl
3325 = cp_parser_class_name (parser,
3326 /*typename_keyword_p=*/false,
3327 /*template_keyword_p=*/false,
3328 /*type_p=*/false,
3329 /*check_access_p=*/true,
3330 /*check_dependency=*/false,
3331 /*class_head_p=*/false);
3332 /* If an error occurred, assume that the name of the
3333 destructor is the same as the name of the qualifying
3334 class. That allows us to keep parsing after running
3335 into ill-formed destructor names. */
3336 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3337 return build_nt (BIT_NOT_EXPR, scope);
3338 else if (type_decl == error_mark_node)
3339 return error_mark_node;
3340
3341 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3342 }
3343
3344 case CPP_KEYWORD:
3345 if (token->keyword == RID_OPERATOR)
3346 {
3347 tree id;
3348
3349 /* This could be a template-id, so we try that first. */
3350 cp_parser_parse_tentatively (parser);
3351 /* Try a template-id. */
3352 id = cp_parser_template_id (parser, template_keyword_p,
3353 /*check_dependency_p=*/true);
3354 /* If that worked, we're done. */
3355 if (cp_parser_parse_definitely (parser))
3356 return id;
3357 /* We still don't know whether we're looking at an
3358 operator-function-id or a conversion-function-id. */
3359 cp_parser_parse_tentatively (parser);
3360 /* Try an operator-function-id. */
3361 id = cp_parser_operator_function_id (parser);
3362 /* If that didn't work, try a conversion-function-id. */
3363 if (!cp_parser_parse_definitely (parser))
3364 id = cp_parser_conversion_function_id (parser);
3365
3366 return id;
3367 }
3368 /* Fall through. */
3369
3370 default:
3371 cp_parser_error (parser, "expected unqualified-id");
3372 return error_mark_node;
3373 }
3374}
3375
3376/* Parse an (optional) nested-name-specifier.
3377
3378 nested-name-specifier:
3379 class-or-namespace-name :: nested-name-specifier [opt]
3380 class-or-namespace-name :: template nested-name-specifier [opt]
3381
3382 PARSER->SCOPE should be set appropriately before this function is
3383 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3384 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3385 in name lookups.
3386
3387 Sets PARSER->SCOPE to the class (TYPE) or namespace
3388 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3389 it unchanged if there is no nested-name-specifier. Returns the new
3390 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3391
3392static tree
3393cp_parser_nested_name_specifier_opt (cp_parser *parser,
3394 bool typename_keyword_p,
3395 bool check_dependency_p,
3396 bool type_p)
3397{
3398 bool success = false;
3399 tree access_check = NULL_TREE;
3400 ptrdiff_t start;
3401
3402 /* If the next token corresponds to a nested name specifier, there
3403 is no need to reparse it. */
3404 if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3405 {
3406 tree value;
3407 tree check;
3408
3409 /* Get the stored value. */
3410 value = cp_lexer_consume_token (parser->lexer)->value;
3411 /* Perform any access checks that were deferred. */
3412 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3413 cp_parser_defer_access_check (parser,
3414 TREE_PURPOSE (check),
3415 TREE_VALUE (check));
3416 /* Set the scope from the stored value. */
3417 parser->scope = TREE_VALUE (value);
3418 parser->qualifying_scope = TREE_TYPE (value);
3419 parser->object_scope = NULL_TREE;
3420 return parser->scope;
3421 }
3422
3423 /* Remember where the nested-name-specifier starts. */
3424 if (cp_parser_parsing_tentatively (parser)
3425 && !cp_parser_committed_to_tentative_parse (parser))
3426 {
3427 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3428 start = cp_lexer_token_difference (parser->lexer,
3429 parser->lexer->first_token,
3430 next_token);
3431 access_check = parser->context->deferred_access_checks;
3432 }
3433 else
3434 start = -1;
3435
3436 while (true)
3437 {
3438 tree new_scope;
3439 tree old_scope;
3440 tree saved_qualifying_scope;
3441 cp_token *token;
3442 bool template_keyword_p;
3443
3444 /* Spot cases that cannot be the beginning of a
3445 nested-name-specifier. On the second and subsequent times
3446 through the loop, we look for the `template' keyword. */
3447 if (success
3448 && cp_lexer_next_token_is_keyword (parser->lexer,
3449 RID_TEMPLATE))
3450 ;
3451 /* A template-id can start a nested-name-specifier. */
3452 else if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
3453 ;
3454 else
3455 {
3456 /* If the next token is not an identifier, then it is
3457 definitely not a class-or-namespace-name. */
3458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
3459 break;
3460 /* If the following token is neither a `<' (to begin a
3461 template-id), nor a `::', then we are not looking at a
3462 nested-name-specifier. */
3463 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3464 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3465 break;
3466 }
3467
3468 /* The nested-name-specifier is optional, so we parse
3469 tentatively. */
3470 cp_parser_parse_tentatively (parser);
3471
3472 /* Look for the optional `template' keyword, if this isn't the
3473 first time through the loop. */
3474 if (success)
3475 template_keyword_p = cp_parser_optional_template_keyword (parser);
3476 else
3477 template_keyword_p = false;
3478
3479 /* Save the old scope since the name lookup we are about to do
3480 might destroy it. */
3481 old_scope = parser->scope;
3482 saved_qualifying_scope = parser->qualifying_scope;
3483 /* Parse the qualifying entity. */
3484 new_scope
3485 = cp_parser_class_or_namespace_name (parser,
3486 typename_keyword_p,
3487 template_keyword_p,
3488 check_dependency_p,
3489 type_p);
3490 /* Look for the `::' token. */
3491 cp_parser_require (parser, CPP_SCOPE, "`::'");
3492
3493 /* If we found what we wanted, we keep going; otherwise, we're
3494 done. */
3495 if (!cp_parser_parse_definitely (parser))
3496 {
3497 bool error_p = false;
3498
3499 /* Restore the OLD_SCOPE since it was valid before the
3500 failed attempt at finding the last
3501 class-or-namespace-name. */
3502 parser->scope = old_scope;
3503 parser->qualifying_scope = saved_qualifying_scope;
3504 /* If the next token is an identifier, and the one after
3505 that is a `::', then any valid interpretation would have
3506 found a class-or-namespace-name. */
3507 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3508 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3509 == CPP_SCOPE)
3510 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3511 != CPP_COMPL))
3512 {
3513 token = cp_lexer_consume_token (parser->lexer);
3514 if (!error_p)
3515 {
3516 tree decl;
3517
3518 decl = cp_parser_lookup_name_simple (parser, token->value);
3519 if (TREE_CODE (decl) == TEMPLATE_DECL)
3520 error ("`%D' used without template parameters",
3521 decl);
3522 else if (parser->scope)
3523 {
3524 if (TYPE_P (parser->scope))
3525 error ("`%T::%D' is not a class-name or "
3526 "namespace-name",
3527 parser->scope, token->value);
3528 else
3529 error ("`%D::%D' is not a class-name or "
3530 "namespace-name",
3531 parser->scope, token->value);
3532 }
3533 else
3534 error ("`%D' is not a class-name or namespace-name",
3535 token->value);
3536 parser->scope = NULL_TREE;
3537 error_p = true;
3538 }
3539 cp_lexer_consume_token (parser->lexer);
3540 }
3541 break;
3542 }
3543
3544 /* We've found one valid nested-name-specifier. */
3545 success = true;
3546 /* Make sure we look in the right scope the next time through
3547 the loop. */
3548 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3549 ? TREE_TYPE (new_scope)
3550 : new_scope);
3551 /* If it is a class scope, try to complete it; we are about to
3552 be looking up names inside the class. */
3553 if (TYPE_P (parser->scope))
3554 complete_type (parser->scope);
3555 }
3556
3557 /* If parsing tentatively, replace the sequence of tokens that makes
3558 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3559 token. That way, should we re-parse the token stream, we will
3560 not have to repeat the effort required to do the parse, nor will
3561 we issue duplicate error messages. */
3562 if (success && start >= 0)
3563 {
3564 cp_token *token;
3565 tree c;
3566
3567 /* Find the token that corresponds to the start of the
3568 template-id. */
3569 token = cp_lexer_advance_token (parser->lexer,
3570 parser->lexer->first_token,
3571 start);
3572
3573 /* Remember the access checks associated with this
3574 nested-name-specifier. */
3575 c = parser->context->deferred_access_checks;
3576 if (c == access_check)
3577 access_check = NULL_TREE;
3578 else
3579 {
3580 while (TREE_CHAIN (c) != access_check)
3581 c = TREE_CHAIN (c);
3582 access_check = parser->context->deferred_access_checks;
3583 parser->context->deferred_access_checks = TREE_CHAIN (c);
3584 TREE_CHAIN (c) = NULL_TREE;
3585 }
3586
3587 /* Reset the contents of the START token. */
3588 token->type = CPP_NESTED_NAME_SPECIFIER;
3589 token->value = build_tree_list (access_check, parser->scope);
3590 TREE_TYPE (token->value) = parser->qualifying_scope;
3591 token->keyword = RID_MAX;
3592 /* Purge all subsequent tokens. */
3593 cp_lexer_purge_tokens_after (parser->lexer, token);
3594 }
3595
3596 return success ? parser->scope : NULL_TREE;
3597}
3598
3599/* Parse a nested-name-specifier. See
3600 cp_parser_nested_name_specifier_opt for details. This function
3601 behaves identically, except that it will an issue an error if no
3602 nested-name-specifier is present, and it will return
3603 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3604 is present. */
3605
3606static tree
3607cp_parser_nested_name_specifier (cp_parser *parser,
3608 bool typename_keyword_p,
3609 bool check_dependency_p,
3610 bool type_p)
3611{
3612 tree scope;
3613
3614 /* Look for the nested-name-specifier. */
3615 scope = cp_parser_nested_name_specifier_opt (parser,
3616 typename_keyword_p,
3617 check_dependency_p,
3618 type_p);
3619 /* If it was not present, issue an error message. */
3620 if (!scope)
3621 {
3622 cp_parser_error (parser, "expected nested-name-specifier");
3623 return error_mark_node;
3624 }
3625
3626 return scope;
3627}
3628
3629/* Parse a class-or-namespace-name.
3630
3631 class-or-namespace-name:
3632 class-name
3633 namespace-name
3634
3635 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3636 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3637 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3638 TYPE_P is TRUE iff the next name should be taken as a class-name,
3639 even the same name is declared to be another entity in the same
3640 scope.
3641
3642 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3643 specified by the class-or-namespace-name. */
3644
3645static tree
3646cp_parser_class_or_namespace_name (cp_parser *parser,
3647 bool typename_keyword_p,
3648 bool template_keyword_p,
3649 bool check_dependency_p,
3650 bool type_p)
3651{
3652 tree saved_scope;
3653 tree saved_qualifying_scope;
3654 tree saved_object_scope;
3655 tree scope;
3656
3657 /* If the next token is the `template' keyword, we know that we are
3658 looking at a class-name. */
3659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3660 return cp_parser_class_name (parser,
3661 typename_keyword_p,
3662 template_keyword_p,
3663 type_p,
3664 /*check_access_p=*/true,
3665 check_dependency_p,
3666 /*class_head_p=*/false);
3667 /* Before we try to parse the class-name, we must save away the
3668 current PARSER->SCOPE since cp_parser_class_name will destroy
3669 it. */
3670 saved_scope = parser->scope;
3671 saved_qualifying_scope = parser->qualifying_scope;
3672 saved_object_scope = parser->object_scope;
3673 /* Try for a class-name first. */
3674 cp_parser_parse_tentatively (parser);
3675 scope = cp_parser_class_name (parser,
3676 typename_keyword_p,
3677 template_keyword_p,
3678 type_p,
3679 /*check_access_p=*/true,
3680 check_dependency_p,
3681 /*class_head_p=*/false);
3682 /* If that didn't work, try for a namespace-name. */
3683 if (!cp_parser_parse_definitely (parser))
3684 {
3685 /* Restore the saved scope. */
3686 parser->scope = saved_scope;
3687 parser->qualifying_scope = saved_qualifying_scope;
3688 parser->object_scope = saved_object_scope;
3689 /* Now look for a namespace-name. */
3690 scope = cp_parser_namespace_name (parser);
3691 }
3692
3693 return scope;
3694}
3695
3696/* Parse a postfix-expression.
3697
3698 postfix-expression:
3699 primary-expression
3700 postfix-expression [ expression ]
3701 postfix-expression ( expression-list [opt] )
3702 simple-type-specifier ( expression-list [opt] )
3703 typename :: [opt] nested-name-specifier identifier
3704 ( expression-list [opt] )
3705 typename :: [opt] nested-name-specifier template [opt] template-id
3706 ( expression-list [opt] )
3707 postfix-expression . template [opt] id-expression
3708 postfix-expression -> template [opt] id-expression
3709 postfix-expression . pseudo-destructor-name
3710 postfix-expression -> pseudo-destructor-name
3711 postfix-expression ++
3712 postfix-expression --
3713 dynamic_cast < type-id > ( expression )
3714 static_cast < type-id > ( expression )
3715 reinterpret_cast < type-id > ( expression )
3716 const_cast < type-id > ( expression )
3717 typeid ( expression )
3718 typeid ( type-id )
3719
3720 GNU Extension:
3721
3722 postfix-expression:
3723 ( type-id ) { initializer-list , [opt] }
3724
3725 This extension is a GNU version of the C99 compound-literal
3726 construct. (The C99 grammar uses `type-name' instead of `type-id',
3727 but they are essentially the same concept.)
3728
3729 If ADDRESS_P is true, the postfix expression is the operand of the
3730 `&' operator.
3731
3732 Returns a representation of the expression. */
3733
3734static tree
3735cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3736{
3737 cp_token *token;
3738 enum rid keyword;
3739 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3740 tree postfix_expression = NULL_TREE;
3741 /* Non-NULL only if the current postfix-expression can be used to
3742 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3743 class used to qualify the member. */
3744 tree qualifying_class = NULL_TREE;
3745 bool done;
3746
3747 /* Peek at the next token. */
3748 token = cp_lexer_peek_token (parser->lexer);
3749 /* Some of the productions are determined by keywords. */
3750 keyword = token->keyword;
3751 switch (keyword)
3752 {
3753 case RID_DYNCAST:
3754 case RID_STATCAST:
3755 case RID_REINTCAST:
3756 case RID_CONSTCAST:
3757 {
3758 tree type;
3759 tree expression;
3760 const char *saved_message;
3761
3762 /* All of these can be handled in the same way from the point
3763 of view of parsing. Begin by consuming the token
3764 identifying the cast. */
3765 cp_lexer_consume_token (parser->lexer);
3766
3767 /* New types cannot be defined in the cast. */
3768 saved_message = parser->type_definition_forbidden_message;
3769 parser->type_definition_forbidden_message
3770 = "types may not be defined in casts";
3771
3772 /* Look for the opening `<'. */
3773 cp_parser_require (parser, CPP_LESS, "`<'");
3774 /* Parse the type to which we are casting. */
3775 type = cp_parser_type_id (parser);
3776 /* Look for the closing `>'. */
3777 cp_parser_require (parser, CPP_GREATER, "`>'");
3778 /* Restore the old message. */
3779 parser->type_definition_forbidden_message = saved_message;
3780
3781 /* And the expression which is being cast. */
3782 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3783 expression = cp_parser_expression (parser);
3784 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3785
3786 switch (keyword)
3787 {
3788 case RID_DYNCAST:
3789 postfix_expression
3790 = build_dynamic_cast (type, expression);
3791 break;
3792 case RID_STATCAST:
3793 postfix_expression
3794 = build_static_cast (type, expression);
3795 break;
3796 case RID_REINTCAST:
3797 postfix_expression
3798 = build_reinterpret_cast (type, expression);
3799 break;
3800 case RID_CONSTCAST:
3801 postfix_expression
3802 = build_const_cast (type, expression);
3803 break;
3804 default:
3805 abort ();
3806 }
3807 }
3808 break;
3809
3810 case RID_TYPEID:
3811 {
3812 tree type;
3813 const char *saved_message;
3814
3815 /* Consume the `typeid' token. */
3816 cp_lexer_consume_token (parser->lexer);
3817 /* Look for the `(' token. */
3818 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3819 /* Types cannot be defined in a `typeid' expression. */
3820 saved_message = parser->type_definition_forbidden_message;
3821 parser->type_definition_forbidden_message
3822 = "types may not be defined in a `typeid\' expression";
3823 /* We can't be sure yet whether we're looking at a type-id or an
3824 expression. */
3825 cp_parser_parse_tentatively (parser);
3826 /* Try a type-id first. */
3827 type = cp_parser_type_id (parser);
3828 /* Look for the `)' token. Otherwise, we can't be sure that
3829 we're not looking at an expression: consider `typeid (int
3830 (3))', for example. */
3831 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3832 /* If all went well, simply lookup the type-id. */
3833 if (cp_parser_parse_definitely (parser))
3834 postfix_expression = get_typeid (type);
3835 /* Otherwise, fall back to the expression variant. */
3836 else
3837 {
3838 tree expression;
3839
3840 /* Look for an expression. */
3841 expression = cp_parser_expression (parser);
3842 /* Compute its typeid. */
3843 postfix_expression = build_typeid (expression);
3844 /* Look for the `)' token. */
3845 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3846 }
3847
3848 /* Restore the saved message. */
3849 parser->type_definition_forbidden_message = saved_message;
3850 }
3851 break;
3852
3853 case RID_TYPENAME:
3854 {
3855 bool template_p = false;
3856 tree id;
3857 tree type;
3858
3859 /* Consume the `typename' token. */
3860 cp_lexer_consume_token (parser->lexer);
3861 /* Look for the optional `::' operator. */
3862 cp_parser_global_scope_opt (parser,
3863 /*current_scope_valid_p=*/false);
3864 /* Look for the nested-name-specifier. */
3865 cp_parser_nested_name_specifier (parser,
3866 /*typename_keyword_p=*/true,
3867 /*check_dependency_p=*/true,
3868 /*type_p=*/true);
3869 /* Look for the optional `template' keyword. */
3870 template_p = cp_parser_optional_template_keyword (parser);
3871 /* We don't know whether we're looking at a template-id or an
3872 identifier. */
3873 cp_parser_parse_tentatively (parser);
3874 /* Try a template-id. */
3875 id = cp_parser_template_id (parser, template_p,
3876 /*check_dependency_p=*/true);
3877 /* If that didn't work, try an identifier. */
3878 if (!cp_parser_parse_definitely (parser))
3879 id = cp_parser_identifier (parser);
3880 /* Create a TYPENAME_TYPE to represent the type to which the
3881 functional cast is being performed. */
3882 type = make_typename_type (parser->scope, id,
3883 /*complain=*/1);
3884
3885 postfix_expression = cp_parser_functional_cast (parser, type);
3886 }
3887 break;
3888
3889 default:
3890 {
3891 tree type;
3892
3893 /* If the next thing is a simple-type-specifier, we may be
3894 looking at a functional cast. We could also be looking at
3895 an id-expression. So, we try the functional cast, and if
3896 that doesn't work we fall back to the primary-expression. */
3897 cp_parser_parse_tentatively (parser);
3898 /* Look for the simple-type-specifier. */
3899 type = cp_parser_simple_type_specifier (parser,
3900 CP_PARSER_FLAGS_NONE);
3901 /* Parse the cast itself. */
3902 if (!cp_parser_error_occurred (parser))
3903 postfix_expression
3904 = cp_parser_functional_cast (parser, type);
3905 /* If that worked, we're done. */
3906 if (cp_parser_parse_definitely (parser))
3907 break;
3908
3909 /* If the functional-cast didn't work out, try a
3910 compound-literal. */
3911 if (cp_parser_allow_gnu_extensions_p (parser))
3912 {
3913 tree initializer_list = NULL_TREE;
3914
3915 cp_parser_parse_tentatively (parser);
3916 /* Look for the `('. */
3917 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3918 {
3919 type = cp_parser_type_id (parser);
3920 /* Look for the `)'. */
3921 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3922 /* Look for the `{'. */
3923 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3924 /* If things aren't going well, there's no need to
3925 keep going. */
3926 if (!cp_parser_error_occurred (parser))
3927 {
3928 /* Parse the initializer-list. */
3929 initializer_list
3930 = cp_parser_initializer_list (parser);
3931 /* Allow a trailing `,'. */
3932 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3933 cp_lexer_consume_token (parser->lexer);
3934 /* Look for the final `}'. */
3935 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3936 }
3937 }
3938 /* If that worked, we're definitely looking at a
3939 compound-literal expression. */
3940 if (cp_parser_parse_definitely (parser))
3941 {
3942 /* Warn the user that a compound literal is not
3943 allowed in standard C++. */
3944 if (pedantic)
3945 pedwarn ("ISO C++ forbids compound-literals");
3946 /* Form the representation of the compound-literal. */
3947 postfix_expression
3948 = finish_compound_literal (type, initializer_list);
3949 break;
3950 }
3951 }
3952
3953 /* It must be a primary-expression. */
3954 postfix_expression = cp_parser_primary_expression (parser,
3955 &idk,
3956 &qualifying_class);
3957 }
3958 break;
3959 }
3960
3961 /* Peek at the next token. */
3962 token = cp_lexer_peek_token (parser->lexer);
3963 done = (token->type != CPP_OPEN_SQUARE
3964 && token->type != CPP_OPEN_PAREN
3965 && token->type != CPP_DOT
3966 && token->type != CPP_DEREF
3967 && token->type != CPP_PLUS_PLUS
3968 && token->type != CPP_MINUS_MINUS);
3969
3970 /* If the postfix expression is complete, finish up. */
3971 if (address_p && qualifying_class && done)
3972 {
3973 if (TREE_CODE (postfix_expression) == SCOPE_REF)
3974 postfix_expression = TREE_OPERAND (postfix_expression, 1);
3975 postfix_expression
3976 = build_offset_ref (qualifying_class, postfix_expression);
3977 return postfix_expression;
3978 }
3979
3980 /* Otherwise, if we were avoiding committing until we knew
3981 whether or not we had a pointer-to-member, we now know that
3982 the expression is an ordinary reference to a qualified name. */
3983 if (qualifying_class && !processing_template_decl)
3984 {
3985 if (TREE_CODE (postfix_expression) == FIELD_DECL)
3986 postfix_expression
3987 = finish_non_static_data_member (postfix_expression,
3988 qualifying_class);
3989 else if (BASELINK_P (postfix_expression))
3990 {
3991 tree fn;
3992 tree fns;
3993
3994 /* See if any of the functions are non-static members. */
3995 fns = BASELINK_FUNCTIONS (postfix_expression);
3996 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3997 fns = TREE_OPERAND (fns, 0);
3998 for (fn = fns; fn; fn = OVL_NEXT (fn))
3999 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4000 break;
4001 /* If so, the expression may be relative to the current
4002 class. */
4003 if (fn && current_class_type
4004 && DERIVED_FROM_P (qualifying_class, current_class_type))
4005 postfix_expression
4006 = (build_class_member_access_expr
4007 (maybe_dummy_object (qualifying_class, NULL),
4008 postfix_expression,
4009 BASELINK_ACCESS_BINFO (postfix_expression),
4010 /*preserve_reference=*/false));
4011 else if (done)
4012 return build_offset_ref (qualifying_class,
4013 postfix_expression);
4014 }
4015 }
4016
4017 /* Remember that there was a reference to this entity. */
4018 if (DECL_P (postfix_expression))
4019 mark_used (postfix_expression);
4020
4021 /* Keep looping until the postfix-expression is complete. */
4022 while (true)
4023 {
4024 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4025 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4026 {
4027 /* It is not a Koenig lookup function call. */
4028 unqualified_name_lookup_error (postfix_expression);
4029 postfix_expression = error_mark_node;
4030 }
4031
4032 /* Peek at the next token. */
4033 token = cp_lexer_peek_token (parser->lexer);
4034
4035 switch (token->type)
4036 {
4037 case CPP_OPEN_SQUARE:
4038 /* postfix-expression [ expression ] */
4039 {
4040 tree index;
4041
4042 /* Consume the `[' token. */
4043 cp_lexer_consume_token (parser->lexer);
4044 /* Parse the index expression. */
4045 index = cp_parser_expression (parser);
4046 /* Look for the closing `]'. */
4047 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4048
4049 /* Build the ARRAY_REF. */
4050 postfix_expression
4051 = grok_array_decl (postfix_expression, index);
4052 idk = CP_PARSER_ID_KIND_NONE;
4053 }
4054 break;
4055
4056 case CPP_OPEN_PAREN:
4057 /* postfix-expression ( expression-list [opt] ) */
4058 {
4059 tree args;
4060
4061 /* Consume the `(' token. */
4062 cp_lexer_consume_token (parser->lexer);
4063 /* If the next token is not a `)', then there are some
4064 arguments. */
4065 if (cp_lexer_next_token_is_not (parser->lexer,
4066 CPP_CLOSE_PAREN))
4067 args = cp_parser_expression_list (parser);
4068 else
4069 args = NULL_TREE;
4070 /* Look for the closing `)'. */
4071 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4072
4073 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4074 && (is_overloaded_fn (postfix_expression)
4075 || DECL_P (postfix_expression)
4076 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4077 && args)
4078 {
4079 tree arg;
4080 tree identifier = NULL_TREE;
4081 tree functions = NULL_TREE;
4082
4083 /* Find the name of the overloaded function. */
4084 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4085 identifier = postfix_expression;
4086 else if (is_overloaded_fn (postfix_expression))
4087 {
4088 functions = postfix_expression;
4089 identifier = DECL_NAME (get_first_fn (functions));
4090 }
4091 else if (DECL_P (postfix_expression))
4092 {
4093 functions = postfix_expression;
4094 identifier = DECL_NAME (postfix_expression);
4095 }
4096
4097 /* A call to a namespace-scope function using an
4098 unqualified name.
4099
4100 Do Koenig lookup -- unless any of the arguments are
4101 type-dependent. */
4102 for (arg = args; arg; arg = TREE_CHAIN (arg))
4103 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4104 break;
4105 if (!arg)
4106 {
4107 postfix_expression
4108 = lookup_arg_dependent(identifier, functions, args);
4109 if (!postfix_expression)
4110 {
4111 /* The unqualified name could not be resolved. */
4112 unqualified_name_lookup_error (identifier);
4113 postfix_expression = error_mark_node;
4114 }
4115 postfix_expression
4116 = build_call_from_tree (postfix_expression, args,
4117 /*diallow_virtual=*/false);
4118 break;
4119 }
4120 postfix_expression = build_min_nt (LOOKUP_EXPR,
4121 identifier);
4122 }
4123 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4124 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4125 {
4126 /* The unqualified name could not be resolved. */
4127 unqualified_name_lookup_error (postfix_expression);
4128 postfix_expression = error_mark_node;
4129 break;
4130 }
4131
4132 /* In the body of a template, no further processing is
4133 required. */
4134 if (processing_template_decl)
4135 {
4136 postfix_expression = build_nt (CALL_EXPR,
4137 postfix_expression,
4138 args);
4139 break;
4140 }
4141
4142 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4143 postfix_expression
4144 = (build_new_method_call
4145 (TREE_OPERAND (postfix_expression, 0),
4146 TREE_OPERAND (postfix_expression, 1),
4147 args, NULL_TREE,
4148 (idk == CP_PARSER_ID_KIND_QUALIFIED
4149 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4150 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4151 postfix_expression = (build_offset_ref_call_from_tree
4152 (postfix_expression, args));
4153 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4154 {
4155 /* A call to a static class member, or a
4156 namespace-scope function. */
4157 postfix_expression
4158 = finish_call_expr (postfix_expression, args,
4159 /*disallow_virtual=*/true);
4160 }
4161 else
4162 {
4163 /* All other function calls. */
4164 postfix_expression
4165 = finish_call_expr (postfix_expression, args,
4166 /*disallow_virtual=*/false);
4167 }
4168
4169 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4170 idk = CP_PARSER_ID_KIND_NONE;
4171 }
4172 break;
4173
4174 case CPP_DOT:
4175 case CPP_DEREF:
4176 /* postfix-expression . template [opt] id-expression
4177 postfix-expression . pseudo-destructor-name
4178 postfix-expression -> template [opt] id-expression
4179 postfix-expression -> pseudo-destructor-name */
4180 {
4181 tree name;
4182 bool dependent_p;
4183 bool template_p;
4184 tree scope = NULL_TREE;
4185
4186 /* If this is a `->' operator, dereference the pointer. */
4187 if (token->type == CPP_DEREF)
4188 postfix_expression = build_x_arrow (postfix_expression);
4189 /* Check to see whether or not the expression is
4190 type-dependent. */
4191 dependent_p = (cp_parser_type_dependent_expression_p
4192 (postfix_expression));
4193 /* The identifier following the `->' or `.' is not
4194 qualified. */
4195 parser->scope = NULL_TREE;
4196 parser->qualifying_scope = NULL_TREE;
4197 parser->object_scope = NULL_TREE;
4198 /* Enter the scope corresponding to the type of the object
4199 given by the POSTFIX_EXPRESSION. */
4200 if (!dependent_p
4201 && TREE_TYPE (postfix_expression) != NULL_TREE)
4202 {
4203 scope = TREE_TYPE (postfix_expression);
4204 /* According to the standard, no expression should
4205 ever have reference type. Unfortunately, we do not
4206 currently match the standard in this respect in
4207 that our internal representation of an expression
4208 may have reference type even when the standard says
4209 it does not. Therefore, we have to manually obtain
4210 the underlying type here. */
4211 if (TREE_CODE (scope) == REFERENCE_TYPE)
4212 scope = TREE_TYPE (scope);
4213 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4214 type of the field. We get an OFFSET_TYPE for
4215 something like:
4216
4217 S::T.a ...
4218
4219 Probably, we should not get an OFFSET_TYPE here;
4220 that transformation should be made only if `&S::T'
4221 is written. */
4222 if (TREE_CODE (scope) == OFFSET_TYPE)
4223 scope = TREE_TYPE (scope);
4224 /* The type of the POSTFIX_EXPRESSION must be
4225 complete. */
4226 scope = complete_type_or_else (scope, NULL_TREE);
4227 /* Let the name lookup machinery know that we are
4228 processing a class member access expression. */
4229 parser->context->object_type = scope;
4230 /* If something went wrong, we want to be able to
4231 discern that case, as opposed to the case where
4232 there was no SCOPE due to the type of expression
4233 being dependent. */
4234 if (!scope)
4235 scope = error_mark_node;
4236 }
4237
4238 /* Consume the `.' or `->' operator. */
4239 cp_lexer_consume_token (parser->lexer);
4240 /* If the SCOPE is not a scalar type, we are looking at an
4241 ordinary class member access expression, rather than a
4242 pseudo-destructor-name. */
4243 if (!scope || !SCALAR_TYPE_P (scope))
4244 {
4245 template_p = cp_parser_optional_template_keyword (parser);
4246 /* Parse the id-expression. */
4247 name = cp_parser_id_expression (parser,
4248 template_p,
4249 /*check_dependency_p=*/true,
4250 /*template_p=*/NULL);
4251 /* In general, build a SCOPE_REF if the member name is
4252 qualified. However, if the name was not dependent
4253 and has already been resolved; there is no need to
4254 build the SCOPE_REF. For example;
4255
4256 struct X { void f(); };
4257 template <typename T> void f(T* t) { t->X::f(); }
4258
4259 Even though "t" is dependent, "X::f" is not and has
4260 except that for a BASELINK there is no need to
4261 include scope information. */
4262 if (name != error_mark_node
4263 && !BASELINK_P (name)
4264 && parser->scope)
4265 {
4266 name = build_nt (SCOPE_REF, parser->scope, name);
4267 parser->scope = NULL_TREE;
4268 parser->qualifying_scope = NULL_TREE;
4269 parser->object_scope = NULL_TREE;
4270 }
4271 postfix_expression
4272 = finish_class_member_access_expr (postfix_expression, name);
4273 }
4274 /* Otherwise, try the pseudo-destructor-name production. */
4275 else
4276 {
4277 tree s;
4278 tree type;
4279
4280 /* Parse the pseudo-destructor-name. */
4281 cp_parser_pseudo_destructor_name (parser, &s, &type);
4282 /* Form the call. */
4283 postfix_expression
4284 = finish_pseudo_destructor_expr (postfix_expression,
4285 s, TREE_TYPE (type));
4286 }
4287
4288 /* We no longer need to look up names in the scope of the
4289 object on the left-hand side of the `.' or `->'
4290 operator. */
4291 parser->context->object_type = NULL_TREE;
4292 idk = CP_PARSER_ID_KIND_NONE;
4293 }
4294 break;
4295
4296 case CPP_PLUS_PLUS:
4297 /* postfix-expression ++ */
4298 /* Consume the `++' token. */
4299 cp_lexer_consume_token (parser->lexer);
4300 /* Generate a reprsentation for the complete expression. */
4301 postfix_expression
4302 = finish_increment_expr (postfix_expression,
4303 POSTINCREMENT_EXPR);
4304 idk = CP_PARSER_ID_KIND_NONE;
4305 break;
4306
4307 case CPP_MINUS_MINUS:
4308 /* postfix-expression -- */
4309 /* Consume the `--' token. */
4310 cp_lexer_consume_token (parser->lexer);
4311 /* Generate a reprsentation for the complete expression. */
4312 postfix_expression
4313 = finish_increment_expr (postfix_expression,
4314 POSTDECREMENT_EXPR);
4315 idk = CP_PARSER_ID_KIND_NONE;
4316 break;
4317
4318 default:
4319 return postfix_expression;
4320 }
4321 }
4322
4323 /* We should never get here. */
4324 abort ();
4325 return error_mark_node;
4326}
4327
4328/* Parse an expression-list.
4329
4330 expression-list:
4331 assignment-expression
4332 expression-list, assignment-expression
4333
4334 Returns a TREE_LIST. The TREE_VALUE of each node is a
4335 representation of an assignment-expression. Note that a TREE_LIST
4336 is returned even if there is only a single expression in the list. */
4337
4338static tree
4339cp_parser_expression_list (parser)
4340 cp_parser *parser;
4341{
4342 tree expression_list = NULL_TREE;
4343
4344 /* Consume expressions until there are no more. */
4345 while (true)
4346 {
4347 tree expr;
4348
4349 /* Parse the next assignment-expression. */
4350 expr = cp_parser_assignment_expression (parser);
4351 /* Add it to the list. */
4352 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4353
4354 /* If the next token isn't a `,', then we are done. */
4355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4356 {
4357 /* All uses of expression-list in the grammar are followed
4358 by a `)'. Therefore, if the next token is not a `)' an
4359 error will be issued, unless we are parsing tentatively.
4360 Skip ahead to see if there is another `,' before the `)';
4361 if so, we can go there and recover. */
4362 if (cp_parser_parsing_tentatively (parser)
4363 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4364 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4365 break;
4366 }
4367
4368 /* Otherwise, consume the `,' and keep going. */
4369 cp_lexer_consume_token (parser->lexer);
4370 }
4371
4372 /* We built up the list in reverse order so we must reverse it now. */
4373 return nreverse (expression_list);
4374}
4375
4376/* Parse a pseudo-destructor-name.
4377
4378 pseudo-destructor-name:
4379 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4380 :: [opt] nested-name-specifier template template-id :: ~ type-name
4381 :: [opt] nested-name-specifier [opt] ~ type-name
4382
4383 If either of the first two productions is used, sets *SCOPE to the
4384 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4385 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4386 or ERROR_MARK_NODE if no type-name is present. */
4387
4388static void
4389cp_parser_pseudo_destructor_name (parser, scope, type)
4390 cp_parser *parser;
4391 tree *scope;
4392 tree *type;
4393{
4394 bool nested_name_specifier_p;
4395
4396 /* Look for the optional `::' operator. */
4397 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4398 /* Look for the optional nested-name-specifier. */
4399 nested_name_specifier_p
4400 = (cp_parser_nested_name_specifier_opt (parser,
4401 /*typename_keyword_p=*/false,
4402 /*check_dependency_p=*/true,
4403 /*type_p=*/false)
4404 != NULL_TREE);
4405 /* Now, if we saw a nested-name-specifier, we might be doing the
4406 second production. */
4407 if (nested_name_specifier_p
4408 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4409 {
4410 /* Consume the `template' keyword. */
4411 cp_lexer_consume_token (parser->lexer);
4412 /* Parse the template-id. */
4413 cp_parser_template_id (parser,
4414 /*template_keyword_p=*/true,
4415 /*check_dependency_p=*/false);
4416 /* Look for the `::' token. */
4417 cp_parser_require (parser, CPP_SCOPE, "`::'");
4418 }
4419 /* If the next token is not a `~', then there might be some
4420 additional qualification. */
4421 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4422 {
4423 /* Look for the type-name. */
4424 *scope = TREE_TYPE (cp_parser_type_name (parser));
4425 /* Look for the `::' token. */
4426 cp_parser_require (parser, CPP_SCOPE, "`::'");
4427 }
4428 else
4429 *scope = NULL_TREE;
4430
4431 /* Look for the `~'. */
4432 cp_parser_require (parser, CPP_COMPL, "`~'");
4433 /* Look for the type-name again. We are not responsible for
4434 checking that it matches the first type-name. */
4435 *type = cp_parser_type_name (parser);
4436}
4437
4438/* Parse a unary-expression.
4439
4440 unary-expression:
4441 postfix-expression
4442 ++ cast-expression
4443 -- cast-expression
4444 unary-operator cast-expression
4445 sizeof unary-expression
4446 sizeof ( type-id )
4447 new-expression
4448 delete-expression
4449
4450 GNU Extensions:
4451
4452 unary-expression:
4453 __extension__ cast-expression
4454 __alignof__ unary-expression
4455 __alignof__ ( type-id )
4456 __real__ cast-expression
4457 __imag__ cast-expression
4458 && identifier
4459
4460 ADDRESS_P is true iff the unary-expression is appearing as the
4461 operand of the `&' operator.
4462
4463 Returns a representation of the expresion. */
4464
4465static tree
4466cp_parser_unary_expression (cp_parser *parser, bool address_p)
4467{
4468 cp_token *token;
4469 enum tree_code unary_operator;
4470
4471 /* Peek at the next token. */
4472 token = cp_lexer_peek_token (parser->lexer);
4473 /* Some keywords give away the kind of expression. */
4474 if (token->type == CPP_KEYWORD)
4475 {
4476 enum rid keyword = token->keyword;
4477
4478 switch (keyword)
4479 {
4480 case RID_ALIGNOF:
4481 {
4482 /* Consume the `alignof' token. */
4483 cp_lexer_consume_token (parser->lexer);
4484 /* Parse the operand. */
4485 return finish_alignof (cp_parser_sizeof_operand
4486 (parser, keyword));
4487 }
4488
4489 case RID_SIZEOF:
4490 {
4491 tree operand;
4492
4493 /* Consume the `sizeof' token. */
4494 cp_lexer_consume_token (parser->lexer);
4495 /* Parse the operand. */
4496 operand = cp_parser_sizeof_operand (parser, keyword);
4497
4498 /* If the type of the operand cannot be determined build a
4499 SIZEOF_EXPR. */
4500 if (TYPE_P (operand)
4501 ? cp_parser_dependent_type_p (operand)
4502 : cp_parser_type_dependent_expression_p (operand))
4503 return build_min (SIZEOF_EXPR, size_type_node, operand);
4504 /* Otherwise, compute the constant value. */
4505 else
4506 return finish_sizeof (operand);
4507 }
4508
4509 case RID_NEW:
4510 return cp_parser_new_expression (parser);
4511
4512 case RID_DELETE:
4513 return cp_parser_delete_expression (parser);
4514
4515 case RID_EXTENSION:
4516 {
4517 /* The saved value of the PEDANTIC flag. */
4518 int saved_pedantic;
4519 tree expr;
4520
4521 /* Save away the PEDANTIC flag. */
4522 cp_parser_extension_opt (parser, &saved_pedantic);
4523 /* Parse the cast-expression. */
4524 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4525 /* Restore the PEDANTIC flag. */
4526 pedantic = saved_pedantic;
4527
4528 return expr;
4529 }
4530
4531 case RID_REALPART:
4532 case RID_IMAGPART:
4533 {
4534 tree expression;
4535
4536 /* Consume the `__real__' or `__imag__' token. */
4537 cp_lexer_consume_token (parser->lexer);
4538 /* Parse the cast-expression. */
4539 expression = cp_parser_cast_expression (parser,
4540 /*address_p=*/false);
4541 /* Create the complete representation. */
4542 return build_x_unary_op ((keyword == RID_REALPART
4543 ? REALPART_EXPR : IMAGPART_EXPR),
4544 expression);
4545 }
4546 break;
4547
4548 default:
4549 break;
4550 }
4551 }
4552
4553 /* Look for the `:: new' and `:: delete', which also signal the
4554 beginning of a new-expression, or delete-expression,
4555 respectively. If the next token is `::', then it might be one of
4556 these. */
4557 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4558 {
4559 enum rid keyword;
4560
4561 /* See if the token after the `::' is one of the keywords in
4562 which we're interested. */
4563 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4564 /* If it's `new', we have a new-expression. */
4565 if (keyword == RID_NEW)
4566 return cp_parser_new_expression (parser);
4567 /* Similarly, for `delete'. */
4568 else if (keyword == RID_DELETE)
4569 return cp_parser_delete_expression (parser);
4570 }
4571
4572 /* Look for a unary operator. */
4573 unary_operator = cp_parser_unary_operator (token);
4574 /* The `++' and `--' operators can be handled similarly, even though
4575 they are not technically unary-operators in the grammar. */
4576 if (unary_operator == ERROR_MARK)
4577 {
4578 if (token->type == CPP_PLUS_PLUS)
4579 unary_operator = PREINCREMENT_EXPR;
4580 else if (token->type == CPP_MINUS_MINUS)
4581 unary_operator = PREDECREMENT_EXPR;
4582 /* Handle the GNU address-of-label extension. */
4583 else if (cp_parser_allow_gnu_extensions_p (parser)
4584 && token->type == CPP_AND_AND)
4585 {
4586 tree identifier;
4587
4588 /* Consume the '&&' token. */
4589 cp_lexer_consume_token (parser->lexer);
4590 /* Look for the identifier. */
4591 identifier = cp_parser_identifier (parser);
4592 /* Create an expression representing the address. */
4593 return finish_label_address_expr (identifier);
4594 }
4595 }
4596 if (unary_operator != ERROR_MARK)
4597 {
4598 tree cast_expression;
4599
4600 /* Consume the operator token. */
4601 token = cp_lexer_consume_token (parser->lexer);
4602 /* Parse the cast-expression. */
4603 cast_expression
4604 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4605 /* Now, build an appropriate representation. */
4606 switch (unary_operator)
4607 {
4608 case INDIRECT_REF:
4609 return build_x_indirect_ref (cast_expression, "unary *");
4610
4611 case ADDR_EXPR:
4612 return build_x_unary_op (ADDR_EXPR, cast_expression);
4613
4614 case CONVERT_EXPR:
4615 case NEGATE_EXPR:
4616 case TRUTH_NOT_EXPR:
4617 case PREINCREMENT_EXPR:
4618 case PREDECREMENT_EXPR:
4619 return finish_unary_op_expr (unary_operator, cast_expression);
4620
4621 case BIT_NOT_EXPR:
4622 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4623
4624 default:
4625 abort ();
4626 return error_mark_node;
4627 }
4628 }
4629
4630 return cp_parser_postfix_expression (parser, address_p);
4631}
4632
4633/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4634 unary-operator, the corresponding tree code is returned. */
4635
4636static enum tree_code
4637cp_parser_unary_operator (token)
4638 cp_token *token;
4639{
4640 switch (token->type)
4641 {
4642 case CPP_MULT:
4643 return INDIRECT_REF;
4644
4645 case CPP_AND:
4646 return ADDR_EXPR;
4647
4648 case CPP_PLUS:
4649 return CONVERT_EXPR;
4650
4651 case CPP_MINUS:
4652 return NEGATE_EXPR;
4653
4654 case CPP_NOT:
4655 return TRUTH_NOT_EXPR;
4656
4657 case CPP_COMPL:
4658 return BIT_NOT_EXPR;
4659
4660 default:
4661 return ERROR_MARK;
4662 }
4663}
4664
4665/* Parse a new-expression.
4666
4667 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4668 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4669
4670 Returns a representation of the expression. */
4671
4672static tree
4673cp_parser_new_expression (parser)
4674 cp_parser *parser;
4675{
4676 bool global_scope_p;
4677 tree placement;
4678 tree type;
4679 tree initializer;
4680
4681 /* Look for the optional `::' operator. */
4682 global_scope_p
4683 = (cp_parser_global_scope_opt (parser,
4684 /*current_scope_valid_p=*/false)
4685 != NULL_TREE);
4686 /* Look for the `new' operator. */
4687 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4688 /* There's no easy way to tell a new-placement from the
4689 `( type-id )' construct. */
4690 cp_parser_parse_tentatively (parser);
4691 /* Look for a new-placement. */
4692 placement = cp_parser_new_placement (parser);
4693 /* If that didn't work out, there's no new-placement. */
4694 if (!cp_parser_parse_definitely (parser))
4695 placement = NULL_TREE;
4696
4697 /* If the next token is a `(', then we have a parenthesized
4698 type-id. */
4699 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4700 {
4701 /* Consume the `('. */
4702 cp_lexer_consume_token (parser->lexer);
4703 /* Parse the type-id. */
4704 type = cp_parser_type_id (parser);
4705 /* Look for the closing `)'. */
4706 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4707 }
4708 /* Otherwise, there must be a new-type-id. */
4709 else
4710 type = cp_parser_new_type_id (parser);
4711
4712 /* If the next token is a `(', then we have a new-initializer. */
4713 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4714 initializer = cp_parser_new_initializer (parser);
4715 else
4716 initializer = NULL_TREE;
4717
4718 /* Create a representation of the new-expression. */
4719 return build_new (placement, type, initializer, global_scope_p);
4720}
4721
4722/* Parse a new-placement.
4723
4724 new-placement:
4725 ( expression-list )
4726
4727 Returns the same representation as for an expression-list. */
4728
4729static tree
4730cp_parser_new_placement (parser)
4731 cp_parser *parser;
4732{
4733 tree expression_list;
4734
4735 /* Look for the opening `('. */
4736 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4737 return error_mark_node;
4738 /* Parse the expression-list. */
4739 expression_list = cp_parser_expression_list (parser);
4740 /* Look for the closing `)'. */
4741 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4742
4743 return expression_list;
4744}
4745
4746/* Parse a new-type-id.
4747
4748 new-type-id:
4749 type-specifier-seq new-declarator [opt]
4750
4751 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4752 and whose TREE_VALUE is the new-declarator. */
4753
4754static tree
4755cp_parser_new_type_id (parser)
4756 cp_parser *parser;
4757{
4758 tree type_specifier_seq;
4759 tree declarator;
4760 const char *saved_message;
4761
4762 /* The type-specifier sequence must not contain type definitions.
4763 (It cannot contain declarations of new types either, but if they
4764 are not definitions we will catch that because they are not
4765 complete.) */
4766 saved_message = parser->type_definition_forbidden_message;
4767 parser->type_definition_forbidden_message
4768 = "types may not be defined in a new-type-id";
4769 /* Parse the type-specifier-seq. */
4770 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4771 /* Restore the old message. */
4772 parser->type_definition_forbidden_message = saved_message;
4773 /* Parse the new-declarator. */
4774 declarator = cp_parser_new_declarator_opt (parser);
4775
4776 return build_tree_list (type_specifier_seq, declarator);
4777}
4778
4779/* Parse an (optional) new-declarator.
4780
4781 new-declarator:
4782 ptr-operator new-declarator [opt]
4783 direct-new-declarator
4784
4785 Returns a representation of the declarator. See
4786 cp_parser_declarator for the representations used. */
4787
4788static tree
4789cp_parser_new_declarator_opt (parser)
4790 cp_parser *parser;
4791{
4792 enum tree_code code;
4793 tree type;
4794 tree cv_qualifier_seq;
4795
4796 /* We don't know if there's a ptr-operator next, or not. */
4797 cp_parser_parse_tentatively (parser);
4798 /* Look for a ptr-operator. */
4799 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4800 /* If that worked, look for more new-declarators. */
4801 if (cp_parser_parse_definitely (parser))
4802 {
4803 tree declarator;
4804
4805 /* Parse another optional declarator. */
4806 declarator = cp_parser_new_declarator_opt (parser);
4807
4808 /* Create the representation of the declarator. */
4809 if (code == INDIRECT_REF)
4810 declarator = make_pointer_declarator (cv_qualifier_seq,
4811 declarator);
4812 else
4813 declarator = make_reference_declarator (cv_qualifier_seq,
4814 declarator);
4815
4816 /* Handle the pointer-to-member case. */
4817 if (type)
4818 declarator = build_nt (SCOPE_REF, type, declarator);
4819
4820 return declarator;
4821 }
4822
4823 /* If the next token is a `[', there is a direct-new-declarator. */
4824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4825 return cp_parser_direct_new_declarator (parser);
4826
4827 return NULL_TREE;
4828}
4829
4830/* Parse a direct-new-declarator.
4831
4832 direct-new-declarator:
4833 [ expression ]
4834 direct-new-declarator [constant-expression]
4835
4836 Returns an ARRAY_REF, following the same conventions as are
4837 documented for cp_parser_direct_declarator. */
4838
4839static tree
4840cp_parser_direct_new_declarator (parser)
4841 cp_parser *parser;
4842{
4843 tree declarator = NULL_TREE;
4844
4845 while (true)
4846 {
4847 tree expression;
4848
4849 /* Look for the opening `['. */
4850 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4851 /* The first expression is not required to be constant. */
4852 if (!declarator)
4853 {
4854 expression = cp_parser_expression (parser);
4855 /* The standard requires that the expression have integral
4856 type. DR 74 adds enumeration types. We believe that the
4857 real intent is that these expressions be handled like the
4858 expression in a `switch' condition, which also allows
4859 classes with a single conversion to integral or
4860 enumeration type. */
4861 if (!processing_template_decl)
4862 {
4863 expression
4864 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4865 expression,
b746c5dc 4866 /*complain=*/true);
a723baf1
MM
4867 if (!expression)
4868 {
4869 error ("expression in new-declarator must have integral or enumeration type");
4870 expression = error_mark_node;
4871 }
4872 }
4873 }
4874 /* But all the other expressions must be. */
4875 else
4876 expression = cp_parser_constant_expression (parser);
4877 /* Look for the closing `]'. */
4878 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4879
4880 /* Add this bound to the declarator. */
4881 declarator = build_nt (ARRAY_REF, declarator, expression);
4882
4883 /* If the next token is not a `[', then there are no more
4884 bounds. */
4885 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4886 break;
4887 }
4888
4889 return declarator;
4890}
4891
4892/* Parse a new-initializer.
4893
4894 new-initializer:
4895 ( expression-list [opt] )
4896
4897 Returns a reprsentation of the expression-list. If there is no
4898 expression-list, VOID_ZERO_NODE is returned. */
4899
4900static tree
4901cp_parser_new_initializer (parser)
4902 cp_parser *parser;
4903{
4904 tree expression_list;
4905
4906 /* Look for the opening parenthesis. */
4907 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4908 /* If the next token is not a `)', then there is an
4909 expression-list. */
4910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4911 expression_list = cp_parser_expression_list (parser);
4912 else
4913 expression_list = void_zero_node;
4914 /* Look for the closing parenthesis. */
4915 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4916
4917 return expression_list;
4918}
4919
4920/* Parse a delete-expression.
4921
4922 delete-expression:
4923 :: [opt] delete cast-expression
4924 :: [opt] delete [ ] cast-expression
4925
4926 Returns a representation of the expression. */
4927
4928static tree
4929cp_parser_delete_expression (parser)
4930 cp_parser *parser;
4931{
4932 bool global_scope_p;
4933 bool array_p;
4934 tree expression;
4935
4936 /* Look for the optional `::' operator. */
4937 global_scope_p
4938 = (cp_parser_global_scope_opt (parser,
4939 /*current_scope_valid_p=*/false)
4940 != NULL_TREE);
4941 /* Look for the `delete' keyword. */
4942 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4943 /* See if the array syntax is in use. */
4944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4945 {
4946 /* Consume the `[' token. */
4947 cp_lexer_consume_token (parser->lexer);
4948 /* Look for the `]' token. */
4949 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4950 /* Remember that this is the `[]' construct. */
4951 array_p = true;
4952 }
4953 else
4954 array_p = false;
4955
4956 /* Parse the cast-expression. */
4957 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
4958
4959 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4960}
4961
4962/* Parse a cast-expression.
4963
4964 cast-expression:
4965 unary-expression
4966 ( type-id ) cast-expression
4967
4968 Returns a representation of the expression. */
4969
4970static tree
4971cp_parser_cast_expression (cp_parser *parser, bool address_p)
4972{
4973 /* If it's a `(', then we might be looking at a cast. */
4974 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4975 {
4976 tree type = NULL_TREE;
4977 tree expr = NULL_TREE;
4978 bool compound_literal_p;
4979 const char *saved_message;
4980
4981 /* There's no way to know yet whether or not this is a cast.
4982 For example, `(int (3))' is a unary-expression, while `(int)
4983 3' is a cast. So, we resort to parsing tentatively. */
4984 cp_parser_parse_tentatively (parser);
4985 /* Types may not be defined in a cast. */
4986 saved_message = parser->type_definition_forbidden_message;
4987 parser->type_definition_forbidden_message
4988 = "types may not be defined in casts";
4989 /* Consume the `('. */
4990 cp_lexer_consume_token (parser->lexer);
4991 /* A very tricky bit is that `(struct S) { 3 }' is a
4992 compound-literal (which we permit in C++ as an extension).
4993 But, that construct is not a cast-expression -- it is a
4994 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4995 is legal; if the compound-literal were a cast-expression,
4996 you'd need an extra set of parentheses.) But, if we parse
4997 the type-id, and it happens to be a class-specifier, then we
4998 will commit to the parse at that point, because we cannot
4999 undo the action that is done when creating a new class. So,
5000 then we cannot back up and do a postfix-expression.
5001
5002 Therefore, we scan ahead to the closing `)', and check to see
5003 if the token after the `)' is a `{'. If so, we are not
5004 looking at a cast-expression.
5005
5006 Save tokens so that we can put them back. */
5007 cp_lexer_save_tokens (parser->lexer);
5008 /* Skip tokens until the next token is a closing parenthesis.
5009 If we find the closing `)', and the next token is a `{', then
5010 we are looking at a compound-literal. */
5011 compound_literal_p
5012 = (cp_parser_skip_to_closing_parenthesis (parser)
5013 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5014 /* Roll back the tokens we skipped. */
5015 cp_lexer_rollback_tokens (parser->lexer);
5016 /* If we were looking at a compound-literal, simulate an error
5017 so that the call to cp_parser_parse_definitely below will
5018 fail. */
5019 if (compound_literal_p)
5020 cp_parser_simulate_error (parser);
5021 else
5022 {
5023 /* Look for the type-id. */
5024 type = cp_parser_type_id (parser);
5025 /* Look for the closing `)'. */
5026 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5027 }
5028
5029 /* Restore the saved message. */
5030 parser->type_definition_forbidden_message = saved_message;
5031
5032 /* If all went well, this is a cast. */
5033 if (cp_parser_parse_definitely (parser))
5034 {
5035 /* Parse the dependent expression. */
5036 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5037 /* Warn about old-style casts, if so requested. */
5038 if (warn_old_style_cast
5039 && !in_system_header
5040 && !VOID_TYPE_P (type)
5041 && current_lang_name != lang_name_c)
5042 warning ("use of old-style cast");
5043 /* Perform the cast. */
5044 expr = build_c_cast (type, expr);
5045 }
5046
5047 if (expr)
5048 return expr;
5049 }
5050
5051 /* If we get here, then it's not a cast, so it must be a
5052 unary-expression. */
5053 return cp_parser_unary_expression (parser, address_p);
5054}
5055
5056/* Parse a pm-expression.
5057
5058 pm-expression:
5059 cast-expression
5060 pm-expression .* cast-expression
5061 pm-expression ->* cast-expression
5062
5063 Returns a representation of the expression. */
5064
5065static tree
5066cp_parser_pm_expression (parser)
5067 cp_parser *parser;
5068{
5069 tree cast_expr;
5070 tree pm_expr;
5071
5072 /* Parse the cast-expresion. */
5073 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5074 pm_expr = cast_expr;
5075 /* Now look for pointer-to-member operators. */
5076 while (true)
5077 {
5078 cp_token *token;
5079 enum cpp_ttype token_type;
5080
5081 /* Peek at the next token. */
5082 token = cp_lexer_peek_token (parser->lexer);
5083 token_type = token->type;
5084 /* If it's not `.*' or `->*' there's no pointer-to-member
5085 operation. */
5086 if (token_type != CPP_DOT_STAR
5087 && token_type != CPP_DEREF_STAR)
5088 break;
5089
5090 /* Consume the token. */
5091 cp_lexer_consume_token (parser->lexer);
5092
5093 /* Parse another cast-expression. */
5094 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5095
5096 /* Build the representation of the pointer-to-member
5097 operation. */
5098 if (token_type == CPP_DEREF_STAR)
5099 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5100 else
5101 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5102 }
5103
5104 return pm_expr;
5105}
5106
5107/* Parse a multiplicative-expression.
5108
5109 mulitplicative-expression:
5110 pm-expression
5111 multiplicative-expression * pm-expression
5112 multiplicative-expression / pm-expression
5113 multiplicative-expression % pm-expression
5114
5115 Returns a representation of the expression. */
5116
5117static tree
5118cp_parser_multiplicative_expression (parser)
5119 cp_parser *parser;
5120{
5121 static cp_parser_token_tree_map map = {
5122 { CPP_MULT, MULT_EXPR },
5123 { CPP_DIV, TRUNC_DIV_EXPR },
5124 { CPP_MOD, TRUNC_MOD_EXPR },
5125 { CPP_EOF, ERROR_MARK }
5126 };
5127
5128 return cp_parser_binary_expression (parser,
5129 map,
5130 cp_parser_pm_expression);
5131}
5132
5133/* Parse an additive-expression.
5134
5135 additive-expression:
5136 multiplicative-expression
5137 additive-expression + multiplicative-expression
5138 additive-expression - multiplicative-expression
5139
5140 Returns a representation of the expression. */
5141
5142static tree
5143cp_parser_additive_expression (parser)
5144 cp_parser *parser;
5145{
5146 static cp_parser_token_tree_map map = {
5147 { CPP_PLUS, PLUS_EXPR },
5148 { CPP_MINUS, MINUS_EXPR },
5149 { CPP_EOF, ERROR_MARK }
5150 };
5151
5152 return cp_parser_binary_expression (parser,
5153 map,
5154 cp_parser_multiplicative_expression);
5155}
5156
5157/* Parse a shift-expression.
5158
5159 shift-expression:
5160 additive-expression
5161 shift-expression << additive-expression
5162 shift-expression >> additive-expression
5163
5164 Returns a representation of the expression. */
5165
5166static tree
5167cp_parser_shift_expression (parser)
5168 cp_parser *parser;
5169{
5170 static cp_parser_token_tree_map map = {
5171 { CPP_LSHIFT, LSHIFT_EXPR },
5172 { CPP_RSHIFT, RSHIFT_EXPR },
5173 { CPP_EOF, ERROR_MARK }
5174 };
5175
5176 return cp_parser_binary_expression (parser,
5177 map,
5178 cp_parser_additive_expression);
5179}
5180
5181/* Parse a relational-expression.
5182
5183 relational-expression:
5184 shift-expression
5185 relational-expression < shift-expression
5186 relational-expression > shift-expression
5187 relational-expression <= shift-expression
5188 relational-expression >= shift-expression
5189
5190 GNU Extension:
5191
5192 relational-expression:
5193 relational-expression <? shift-expression
5194 relational-expression >? shift-expression
5195
5196 Returns a representation of the expression. */
5197
5198static tree
5199cp_parser_relational_expression (parser)
5200 cp_parser *parser;
5201{
5202 static cp_parser_token_tree_map map = {
5203 { CPP_LESS, LT_EXPR },
5204 { CPP_GREATER, GT_EXPR },
5205 { CPP_LESS_EQ, LE_EXPR },
5206 { CPP_GREATER_EQ, GE_EXPR },
5207 { CPP_MIN, MIN_EXPR },
5208 { CPP_MAX, MAX_EXPR },
5209 { CPP_EOF, ERROR_MARK }
5210 };
5211
5212 return cp_parser_binary_expression (parser,
5213 map,
5214 cp_parser_shift_expression);
5215}
5216
5217/* Parse an equality-expression.
5218
5219 equality-expression:
5220 relational-expression
5221 equality-expression == relational-expression
5222 equality-expression != relational-expression
5223
5224 Returns a representation of the expression. */
5225
5226static tree
5227cp_parser_equality_expression (parser)
5228 cp_parser *parser;
5229{
5230 static cp_parser_token_tree_map map = {
5231 { CPP_EQ_EQ, EQ_EXPR },
5232 { CPP_NOT_EQ, NE_EXPR },
5233 { CPP_EOF, ERROR_MARK }
5234 };
5235
5236 return cp_parser_binary_expression (parser,
5237 map,
5238 cp_parser_relational_expression);
5239}
5240
5241/* Parse an and-expression.
5242
5243 and-expression:
5244 equality-expression
5245 and-expression & equality-expression
5246
5247 Returns a representation of the expression. */
5248
5249static tree
5250cp_parser_and_expression (parser)
5251 cp_parser *parser;
5252{
5253 static cp_parser_token_tree_map map = {
5254 { CPP_AND, BIT_AND_EXPR },
5255 { CPP_EOF, ERROR_MARK }
5256 };
5257
5258 return cp_parser_binary_expression (parser,
5259 map,
5260 cp_parser_equality_expression);
5261}
5262
5263/* Parse an exclusive-or-expression.
5264
5265 exclusive-or-expression:
5266 and-expression
5267 exclusive-or-expression ^ and-expression
5268
5269 Returns a representation of the expression. */
5270
5271static tree
5272cp_parser_exclusive_or_expression (parser)
5273 cp_parser *parser;
5274{
5275 static cp_parser_token_tree_map map = {
5276 { CPP_XOR, BIT_XOR_EXPR },
5277 { CPP_EOF, ERROR_MARK }
5278 };
5279
5280 return cp_parser_binary_expression (parser,
5281 map,
5282 cp_parser_and_expression);
5283}
5284
5285
5286/* Parse an inclusive-or-expression.
5287
5288 inclusive-or-expression:
5289 exclusive-or-expression
5290 inclusive-or-expression | exclusive-or-expression
5291
5292 Returns a representation of the expression. */
5293
5294static tree
5295cp_parser_inclusive_or_expression (parser)
5296 cp_parser *parser;
5297{
5298 static cp_parser_token_tree_map map = {
5299 { CPP_OR, BIT_IOR_EXPR },
5300 { CPP_EOF, ERROR_MARK }
5301 };
5302
5303 return cp_parser_binary_expression (parser,
5304 map,
5305 cp_parser_exclusive_or_expression);
5306}
5307
5308/* Parse a logical-and-expression.
5309
5310 logical-and-expression:
5311 inclusive-or-expression
5312 logical-and-expression && inclusive-or-expression
5313
5314 Returns a representation of the expression. */
5315
5316static tree
5317cp_parser_logical_and_expression (parser)
5318 cp_parser *parser;
5319{
5320 static cp_parser_token_tree_map map = {
5321 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5322 { CPP_EOF, ERROR_MARK }
5323 };
5324
5325 return cp_parser_binary_expression (parser,
5326 map,
5327 cp_parser_inclusive_or_expression);
5328}
5329
5330/* Parse a logical-or-expression.
5331
5332 logical-or-expression:
5333 logical-and-expresion
5334 logical-or-expression || logical-and-expression
5335
5336 Returns a representation of the expression. */
5337
5338static tree
5339cp_parser_logical_or_expression (parser)
5340 cp_parser *parser;
5341{
5342 static cp_parser_token_tree_map map = {
5343 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5344 { CPP_EOF, ERROR_MARK }
5345 };
5346
5347 return cp_parser_binary_expression (parser,
5348 map,
5349 cp_parser_logical_and_expression);
5350}
5351
5352/* Parse a conditional-expression.
5353
5354 conditional-expression:
5355 logical-or-expression
5356 logical-or-expression ? expression : assignment-expression
5357
5358 GNU Extensions:
5359
5360 conditional-expression:
5361 logical-or-expression ? : assignment-expression
5362
5363 Returns a representation of the expression. */
5364
5365static tree
5366cp_parser_conditional_expression (parser)
5367 cp_parser *parser;
5368{
5369 tree logical_or_expr;
5370
5371 /* Parse the logical-or-expression. */
5372 logical_or_expr = cp_parser_logical_or_expression (parser);
5373 /* If the next token is a `?', then we have a real conditional
5374 expression. */
5375 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5376 return cp_parser_question_colon_clause (parser, logical_or_expr);
5377 /* Otherwise, the value is simply the logical-or-expression. */
5378 else
5379 return logical_or_expr;
5380}
5381
5382/* Parse the `? expression : assignment-expression' part of a
5383 conditional-expression. The LOGICAL_OR_EXPR is the
5384 logical-or-expression that started the conditional-expression.
5385 Returns a representation of the entire conditional-expression.
5386
5387 This routine exists only so that it can be shared between
5388 cp_parser_conditional_expression and
5389 cp_parser_assignment_expression.
5390
5391 ? expression : assignment-expression
5392
5393 GNU Extensions:
5394
5395 ? : assignment-expression */
5396
5397static tree
5398cp_parser_question_colon_clause (parser, logical_or_expr)
5399 cp_parser *parser;
5400 tree logical_or_expr;
5401{
5402 tree expr;
5403 tree assignment_expr;
5404
5405 /* Consume the `?' token. */
5406 cp_lexer_consume_token (parser->lexer);
5407 if (cp_parser_allow_gnu_extensions_p (parser)
5408 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5409 /* Implicit true clause. */
5410 expr = NULL_TREE;
5411 else
5412 /* Parse the expression. */
5413 expr = cp_parser_expression (parser);
5414
5415 /* The next token should be a `:'. */
5416 cp_parser_require (parser, CPP_COLON, "`:'");
5417 /* Parse the assignment-expression. */
5418 assignment_expr = cp_parser_assignment_expression (parser);
5419
5420 /* Build the conditional-expression. */
5421 return build_x_conditional_expr (logical_or_expr,
5422 expr,
5423 assignment_expr);
5424}
5425
5426/* Parse an assignment-expression.
5427
5428 assignment-expression:
5429 conditional-expression
5430 logical-or-expression assignment-operator assignment_expression
5431 throw-expression
5432
5433 Returns a representation for the expression. */
5434
5435static tree
5436cp_parser_assignment_expression (parser)
5437 cp_parser *parser;
5438{
5439 tree expr;
5440
5441 /* If the next token is the `throw' keyword, then we're looking at
5442 a throw-expression. */
5443 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5444 expr = cp_parser_throw_expression (parser);
5445 /* Otherwise, it must be that we are looking at a
5446 logical-or-expression. */
5447 else
5448 {
5449 /* Parse the logical-or-expression. */
5450 expr = cp_parser_logical_or_expression (parser);
5451 /* If the next token is a `?' then we're actually looking at a
5452 conditional-expression. */
5453 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5454 return cp_parser_question_colon_clause (parser, expr);
5455 else
5456 {
5457 enum tree_code assignment_operator;
5458
5459 /* If it's an assignment-operator, we're using the second
5460 production. */
5461 assignment_operator
5462 = cp_parser_assignment_operator_opt (parser);
5463 if (assignment_operator != ERROR_MARK)
5464 {
5465 tree rhs;
5466
5467 /* Parse the right-hand side of the assignment. */
5468 rhs = cp_parser_assignment_expression (parser);
5469 /* Build the asignment expression. */
5470 expr = build_x_modify_expr (expr,
5471 assignment_operator,
5472 rhs);
5473 }
5474 }
5475 }
5476
5477 return expr;
5478}
5479
5480/* Parse an (optional) assignment-operator.
5481
5482 assignment-operator: one of
5483 = *= /= %= += -= >>= <<= &= ^= |=
5484
5485 GNU Extension:
5486
5487 assignment-operator: one of
5488 <?= >?=
5489
5490 If the next token is an assignment operator, the corresponding tree
5491 code is returned, and the token is consumed. For example, for
5492 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5493 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5494 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5495 operator, ERROR_MARK is returned. */
5496
5497static enum tree_code
5498cp_parser_assignment_operator_opt (parser)
5499 cp_parser *parser;
5500{
5501 enum tree_code op;
5502 cp_token *token;
5503
5504 /* Peek at the next toen. */
5505 token = cp_lexer_peek_token (parser->lexer);
5506
5507 switch (token->type)
5508 {
5509 case CPP_EQ:
5510 op = NOP_EXPR;
5511 break;
5512
5513 case CPP_MULT_EQ:
5514 op = MULT_EXPR;
5515 break;
5516
5517 case CPP_DIV_EQ:
5518 op = TRUNC_DIV_EXPR;
5519 break;
5520
5521 case CPP_MOD_EQ:
5522 op = TRUNC_MOD_EXPR;
5523 break;
5524
5525 case CPP_PLUS_EQ:
5526 op = PLUS_EXPR;
5527 break;
5528
5529 case CPP_MINUS_EQ:
5530 op = MINUS_EXPR;
5531 break;
5532
5533 case CPP_RSHIFT_EQ:
5534 op = RSHIFT_EXPR;
5535 break;
5536
5537 case CPP_LSHIFT_EQ:
5538 op = LSHIFT_EXPR;
5539 break;
5540
5541 case CPP_AND_EQ:
5542 op = BIT_AND_EXPR;
5543 break;
5544
5545 case CPP_XOR_EQ:
5546 op = BIT_XOR_EXPR;
5547 break;
5548
5549 case CPP_OR_EQ:
5550 op = BIT_IOR_EXPR;
5551 break;
5552
5553 case CPP_MIN_EQ:
5554 op = MIN_EXPR;
5555 break;
5556
5557 case CPP_MAX_EQ:
5558 op = MAX_EXPR;
5559 break;
5560
5561 default:
5562 /* Nothing else is an assignment operator. */
5563 op = ERROR_MARK;
5564 }
5565
5566 /* If it was an assignment operator, consume it. */
5567 if (op != ERROR_MARK)
5568 cp_lexer_consume_token (parser->lexer);
5569
5570 return op;
5571}
5572
5573/* Parse an expression.
5574
5575 expression:
5576 assignment-expression
5577 expression , assignment-expression
5578
5579 Returns a representation of the expression. */
5580
5581static tree
5582cp_parser_expression (parser)
5583 cp_parser *parser;
5584{
5585 tree expression = NULL_TREE;
5586 bool saw_comma_p = false;
5587
5588 while (true)
5589 {
5590 tree assignment_expression;
5591
5592 /* Parse the next assignment-expression. */
5593 assignment_expression
5594 = cp_parser_assignment_expression (parser);
5595 /* If this is the first assignment-expression, we can just
5596 save it away. */
5597 if (!expression)
5598 expression = assignment_expression;
5599 /* Otherwise, chain the expressions together. It is unclear why
5600 we do not simply build COMPOUND_EXPRs as we go. */
5601 else
5602 expression = tree_cons (NULL_TREE,
5603 assignment_expression,
5604 expression);
5605 /* If the next token is not a comma, then we are done with the
5606 expression. */
5607 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5608 break;
5609 /* Consume the `,'. */
5610 cp_lexer_consume_token (parser->lexer);
5611 /* The first time we see a `,', we must take special action
5612 because the representation used for a single expression is
5613 different from that used for a list containing the single
5614 expression. */
5615 if (!saw_comma_p)
5616 {
5617 /* Remember that this expression has a `,' in it. */
5618 saw_comma_p = true;
5619 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5620 additional expressions to it. */
5621 expression = build_tree_list (NULL_TREE, expression);
5622 }
5623 }
5624
5625 /* Build a COMPOUND_EXPR to represent the entire expression, if
5626 necessary. We built up the list in reverse order, so we must
5627 straighten it out here. */
5628 if (saw_comma_p)
5629 expression = build_x_compound_expr (nreverse (expression));
5630
5631 return expression;
5632}
5633
5634/* Parse a constant-expression.
5635
5636 constant-expression:
5637 conditional-expression */
5638
5639static tree
5640cp_parser_constant_expression (parser)
5641 cp_parser *parser;
5642{
5643 bool saved_constant_expression_p;
5644 tree expression;
5645
5646 /* It might seem that we could simply parse the
5647 conditional-expression, and then check to see if it were
5648 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5649 one that the compiler can figure out is constant, possibly after
5650 doing some simplifications or optimizations. The standard has a
5651 precise definition of constant-expression, and we must honor
5652 that, even though it is somewhat more restrictive.
5653
5654 For example:
5655
5656 int i[(2, 3)];
5657
5658 is not a legal declaration, because `(2, 3)' is not a
5659 constant-expression. The `,' operator is forbidden in a
5660 constant-expression. However, GCC's constant-folding machinery
5661 will fold this operation to an INTEGER_CST for `3'. */
5662
5663 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5664 saved_constant_expression_p = parser->constant_expression_p;
5665 /* We are now parsing a constant-expression. */
5666 parser->constant_expression_p = true;
5667 /* Parse the conditional-expression. */
5668 expression = cp_parser_conditional_expression (parser);
5669 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5670 parser->constant_expression_p = saved_constant_expression_p;
5671
5672 return expression;
5673}
5674
5675/* Statements [gram.stmt.stmt] */
5676
5677/* Parse a statement.
5678
5679 statement:
5680 labeled-statement
5681 expression-statement
5682 compound-statement
5683 selection-statement
5684 iteration-statement
5685 jump-statement
5686 declaration-statement
5687 try-block */
5688
5689static void
5690cp_parser_statement (parser)
5691 cp_parser *parser;
5692{
5693 tree statement;
5694 cp_token *token;
5695 int statement_line_number;
5696
5697 /* There is no statement yet. */
5698 statement = NULL_TREE;
5699 /* Peek at the next token. */
5700 token = cp_lexer_peek_token (parser->lexer);
5701 /* Remember the line number of the first token in the statement. */
5702 statement_line_number = token->line_number;
5703 /* If this is a keyword, then that will often determine what kind of
5704 statement we have. */
5705 if (token->type == CPP_KEYWORD)
5706 {
5707 enum rid keyword = token->keyword;
5708
5709 switch (keyword)
5710 {
5711 case RID_CASE:
5712 case RID_DEFAULT:
5713 statement = cp_parser_labeled_statement (parser);
5714 break;
5715
5716 case RID_IF:
5717 case RID_SWITCH:
5718 statement = cp_parser_selection_statement (parser);
5719 break;
5720
5721 case RID_WHILE:
5722 case RID_DO:
5723 case RID_FOR:
5724 statement = cp_parser_iteration_statement (parser);
5725 break;
5726
5727 case RID_BREAK:
5728 case RID_CONTINUE:
5729 case RID_RETURN:
5730 case RID_GOTO:
5731 statement = cp_parser_jump_statement (parser);
5732 break;
5733
5734 case RID_TRY:
5735 statement = cp_parser_try_block (parser);
5736 break;
5737
5738 default:
5739 /* It might be a keyword like `int' that can start a
5740 declaration-statement. */
5741 break;
5742 }
5743 }
5744 else if (token->type == CPP_NAME)
5745 {
5746 /* If the next token is a `:', then we are looking at a
5747 labeled-statement. */
5748 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5749 if (token->type == CPP_COLON)
5750 statement = cp_parser_labeled_statement (parser);
5751 }
5752 /* Anything that starts with a `{' must be a compound-statement. */
5753 else if (token->type == CPP_OPEN_BRACE)
5754 statement = cp_parser_compound_statement (parser);
5755
5756 /* Everything else must be a declaration-statement or an
5757 expression-statement. Try for the declaration-statement
5758 first, unless we are looking at a `;', in which case we know that
5759 we have an expression-statement. */
5760 if (!statement)
5761 {
5762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5763 {
5764 cp_parser_parse_tentatively (parser);
5765 /* Try to parse the declaration-statement. */
5766 cp_parser_declaration_statement (parser);
5767 /* If that worked, we're done. */
5768 if (cp_parser_parse_definitely (parser))
5769 return;
5770 }
5771 /* Look for an expression-statement instead. */
5772 statement = cp_parser_expression_statement (parser);
5773 }
5774
5775 /* Set the line number for the statement. */
5776 if (statement && statement_code_p (TREE_CODE (statement)))
5777 STMT_LINENO (statement) = statement_line_number;
5778}
5779
5780/* Parse a labeled-statement.
5781
5782 labeled-statement:
5783 identifier : statement
5784 case constant-expression : statement
5785 default : statement
5786
5787 Returns the new CASE_LABEL, for a `case' or `default' label. For
5788 an ordinary label, returns a LABEL_STMT. */
5789
5790static tree
5791cp_parser_labeled_statement (parser)
5792 cp_parser *parser;
5793{
5794 cp_token *token;
5795 tree statement = NULL_TREE;
5796
5797 /* The next token should be an identifier. */
5798 token = cp_lexer_peek_token (parser->lexer);
5799 if (token->type != CPP_NAME
5800 && token->type != CPP_KEYWORD)
5801 {
5802 cp_parser_error (parser, "expected labeled-statement");
5803 return error_mark_node;
5804 }
5805
5806 switch (token->keyword)
5807 {
5808 case RID_CASE:
5809 {
5810 tree expr;
5811
5812 /* Consume the `case' token. */
5813 cp_lexer_consume_token (parser->lexer);
5814 /* Parse the constant-expression. */
5815 expr = cp_parser_constant_expression (parser);
5816 /* Create the label. */
5817 statement = finish_case_label (expr, NULL_TREE);
5818 }
5819 break;
5820
5821 case RID_DEFAULT:
5822 /* Consume the `default' token. */
5823 cp_lexer_consume_token (parser->lexer);
5824 /* Create the label. */
5825 statement = finish_case_label (NULL_TREE, NULL_TREE);
5826 break;
5827
5828 default:
5829 /* Anything else must be an ordinary label. */
5830 statement = finish_label_stmt (cp_parser_identifier (parser));
5831 break;
5832 }
5833
5834 /* Require the `:' token. */
5835 cp_parser_require (parser, CPP_COLON, "`:'");
5836 /* Parse the labeled statement. */
5837 cp_parser_statement (parser);
5838
5839 /* Return the label, in the case of a `case' or `default' label. */
5840 return statement;
5841}
5842
5843/* Parse an expression-statement.
5844
5845 expression-statement:
5846 expression [opt] ;
5847
5848 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5849 statement consists of nothing more than an `;'. */
5850
5851static tree
5852cp_parser_expression_statement (parser)
5853 cp_parser *parser;
5854{
5855 tree statement;
5856
5857 /* If the next token is not a `;', then there is an expression to parse. */
5858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5859 statement = finish_expr_stmt (cp_parser_expression (parser));
5860 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5861 else
5862 {
5863 finish_stmt ();
5864 statement = NULL_TREE;
5865 }
5866 /* Consume the final `;'. */
5867 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5868 {
5869 /* If there is additional (erroneous) input, skip to the end of
5870 the statement. */
5871 cp_parser_skip_to_end_of_statement (parser);
5872 /* If the next token is now a `;', consume it. */
5873 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5874 cp_lexer_consume_token (parser->lexer);
5875 }
5876
5877 return statement;
5878}
5879
5880/* Parse a compound-statement.
5881
5882 compound-statement:
5883 { statement-seq [opt] }
5884
5885 Returns a COMPOUND_STMT representing the statement. */
5886
5887static tree
5888cp_parser_compound_statement (cp_parser *parser)
5889{
5890 tree compound_stmt;
5891
5892 /* Consume the `{'. */
5893 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5894 return error_mark_node;
5895 /* Begin the compound-statement. */
5896 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5897 /* Parse an (optional) statement-seq. */
5898 cp_parser_statement_seq_opt (parser);
5899 /* Finish the compound-statement. */
5900 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5901 /* Consume the `}'. */
5902 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5903
5904 return compound_stmt;
5905}
5906
5907/* Parse an (optional) statement-seq.
5908
5909 statement-seq:
5910 statement
5911 statement-seq [opt] statement */
5912
5913static void
5914cp_parser_statement_seq_opt (parser)
5915 cp_parser *parser;
5916{
5917 /* Scan statements until there aren't any more. */
5918 while (true)
5919 {
5920 /* If we're looking at a `}', then we've run out of statements. */
5921 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5922 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5923 break;
5924
5925 /* Parse the statement. */
5926 cp_parser_statement (parser);
5927 }
5928}
5929
5930/* Parse a selection-statement.
5931
5932 selection-statement:
5933 if ( condition ) statement
5934 if ( condition ) statement else statement
5935 switch ( condition ) statement
5936
5937 Returns the new IF_STMT or SWITCH_STMT. */
5938
5939static tree
5940cp_parser_selection_statement (parser)
5941 cp_parser *parser;
5942{
5943 cp_token *token;
5944 enum rid keyword;
5945
5946 /* Peek at the next token. */
5947 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5948
5949 /* See what kind of keyword it is. */
5950 keyword = token->keyword;
5951 switch (keyword)
5952 {
5953 case RID_IF:
5954 case RID_SWITCH:
5955 {
5956 tree statement;
5957 tree condition;
5958
5959 /* Look for the `('. */
5960 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5961 {
5962 cp_parser_skip_to_end_of_statement (parser);
5963 return error_mark_node;
5964 }
5965
5966 /* Begin the selection-statement. */
5967 if (keyword == RID_IF)
5968 statement = begin_if_stmt ();
5969 else
5970 statement = begin_switch_stmt ();
5971
5972 /* Parse the condition. */
5973 condition = cp_parser_condition (parser);
5974 /* Look for the `)'. */
5975 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5976 cp_parser_skip_to_closing_parenthesis (parser);
5977
5978 if (keyword == RID_IF)
5979 {
5980 tree then_stmt;
5981
5982 /* Add the condition. */
5983 finish_if_stmt_cond (condition, statement);
5984
5985 /* Parse the then-clause. */
5986 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5987 finish_then_clause (statement);
5988
5989 /* If the next token is `else', parse the else-clause. */
5990 if (cp_lexer_next_token_is_keyword (parser->lexer,
5991 RID_ELSE))
5992 {
5993 tree else_stmt;
5994
5995 /* Consume the `else' keyword. */
5996 cp_lexer_consume_token (parser->lexer);
5997 /* Parse the else-clause. */
5998 else_stmt
5999 = cp_parser_implicitly_scoped_statement (parser);
6000 finish_else_clause (statement);
6001 }
6002
6003 /* Now we're all done with the if-statement. */
6004 finish_if_stmt ();
6005 }
6006 else
6007 {
6008 tree body;
6009
6010 /* Add the condition. */
6011 finish_switch_cond (condition, statement);
6012
6013 /* Parse the body of the switch-statement. */
6014 body = cp_parser_implicitly_scoped_statement (parser);
6015
6016 /* Now we're all done with the switch-statement. */
6017 finish_switch_stmt (statement);
6018 }
6019
6020 return statement;
6021 }
6022 break;
6023
6024 default:
6025 cp_parser_error (parser, "expected selection-statement");
6026 return error_mark_node;
6027 }
6028}
6029
6030/* Parse a condition.
6031
6032 condition:
6033 expression
6034 type-specifier-seq declarator = assignment-expression
6035
6036 GNU Extension:
6037
6038 condition:
6039 type-specifier-seq declarator asm-specification [opt]
6040 attributes [opt] = assignment-expression
6041
6042 Returns the expression that should be tested. */
6043
6044static tree
6045cp_parser_condition (parser)
6046 cp_parser *parser;
6047{
6048 tree type_specifiers;
6049 const char *saved_message;
6050
6051 /* Try the declaration first. */
6052 cp_parser_parse_tentatively (parser);
6053 /* New types are not allowed in the type-specifier-seq for a
6054 condition. */
6055 saved_message = parser->type_definition_forbidden_message;
6056 parser->type_definition_forbidden_message
6057 = "types may not be defined in conditions";
6058 /* Parse the type-specifier-seq. */
6059 type_specifiers = cp_parser_type_specifier_seq (parser);
6060 /* Restore the saved message. */
6061 parser->type_definition_forbidden_message = saved_message;
6062 /* If all is well, we might be looking at a declaration. */
6063 if (!cp_parser_error_occurred (parser))
6064 {
6065 tree decl;
6066 tree asm_specification;
6067 tree attributes;
6068 tree declarator;
6069 tree initializer = NULL_TREE;
6070
6071 /* Parse the declarator. */
6072 declarator = cp_parser_declarator (parser,
6073 /*abstract_p=*/false,
6074 /*ctor_dtor_or_conv_p=*/NULL);
6075 /* Parse the attributes. */
6076 attributes = cp_parser_attributes_opt (parser);
6077 /* Parse the asm-specification. */
6078 asm_specification = cp_parser_asm_specification_opt (parser);
6079 /* If the next token is not an `=', then we might still be
6080 looking at an expression. For example:
6081
6082 if (A(a).x)
6083
6084 looks like a decl-specifier-seq and a declarator -- but then
6085 there is no `=', so this is an expression. */
6086 cp_parser_require (parser, CPP_EQ, "`='");
6087 /* If we did see an `=', then we are looking at a declaration
6088 for sure. */
6089 if (cp_parser_parse_definitely (parser))
6090 {
6091 /* Create the declaration. */
6092 decl = start_decl (declarator, type_specifiers,
6093 /*initialized_p=*/true,
6094 attributes, /*prefix_attributes=*/NULL_TREE);
6095 /* Parse the assignment-expression. */
6096 initializer = cp_parser_assignment_expression (parser);
6097
6098 /* Process the initializer. */
6099 cp_finish_decl (decl,
6100 initializer,
6101 asm_specification,
6102 LOOKUP_ONLYCONVERTING);
6103
6104 return convert_from_reference (decl);
6105 }
6106 }
6107 /* If we didn't even get past the declarator successfully, we are
6108 definitely not looking at a declaration. */
6109 else
6110 cp_parser_abort_tentative_parse (parser);
6111
6112 /* Otherwise, we are looking at an expression. */
6113 return cp_parser_expression (parser);
6114}
6115
6116/* Parse an iteration-statement.
6117
6118 iteration-statement:
6119 while ( condition ) statement
6120 do statement while ( expression ) ;
6121 for ( for-init-statement condition [opt] ; expression [opt] )
6122 statement
6123
6124 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6125
6126static tree
6127cp_parser_iteration_statement (parser)
6128 cp_parser *parser;
6129{
6130 cp_token *token;
6131 enum rid keyword;
6132 tree statement;
6133
6134 /* Peek at the next token. */
6135 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6136 if (!token)
6137 return error_mark_node;
6138
6139 /* See what kind of keyword it is. */
6140 keyword = token->keyword;
6141 switch (keyword)
6142 {
6143 case RID_WHILE:
6144 {
6145 tree condition;
6146
6147 /* Begin the while-statement. */
6148 statement = begin_while_stmt ();
6149 /* Look for the `('. */
6150 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6151 /* Parse the condition. */
6152 condition = cp_parser_condition (parser);
6153 finish_while_stmt_cond (condition, statement);
6154 /* Look for the `)'. */
6155 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6156 /* Parse the dependent statement. */
6157 cp_parser_already_scoped_statement (parser);
6158 /* We're done with the while-statement. */
6159 finish_while_stmt (statement);
6160 }
6161 break;
6162
6163 case RID_DO:
6164 {
6165 tree expression;
6166
6167 /* Begin the do-statement. */
6168 statement = begin_do_stmt ();
6169 /* Parse the body of the do-statement. */
6170 cp_parser_implicitly_scoped_statement (parser);
6171 finish_do_body (statement);
6172 /* Look for the `while' keyword. */
6173 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6174 /* Look for the `('. */
6175 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6176 /* Parse the expression. */
6177 expression = cp_parser_expression (parser);
6178 /* We're done with the do-statement. */
6179 finish_do_stmt (expression, statement);
6180 /* Look for the `)'. */
6181 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6182 /* Look for the `;'. */
6183 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6184 }
6185 break;
6186
6187 case RID_FOR:
6188 {
6189 tree condition = NULL_TREE;
6190 tree expression = NULL_TREE;
6191
6192 /* Begin the for-statement. */
6193 statement = begin_for_stmt ();
6194 /* Look for the `('. */
6195 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6196 /* Parse the initialization. */
6197 cp_parser_for_init_statement (parser);
6198 finish_for_init_stmt (statement);
6199
6200 /* If there's a condition, process it. */
6201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6202 condition = cp_parser_condition (parser);
6203 finish_for_cond (condition, statement);
6204 /* Look for the `;'. */
6205 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6206
6207 /* If there's an expression, process it. */
6208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6209 expression = cp_parser_expression (parser);
6210 finish_for_expr (expression, statement);
6211 /* Look for the `)'. */
6212 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6213
6214 /* Parse the body of the for-statement. */
6215 cp_parser_already_scoped_statement (parser);
6216
6217 /* We're done with the for-statement. */
6218 finish_for_stmt (statement);
6219 }
6220 break;
6221
6222 default:
6223 cp_parser_error (parser, "expected iteration-statement");
6224 statement = error_mark_node;
6225 break;
6226 }
6227
6228 return statement;
6229}
6230
6231/* Parse a for-init-statement.
6232
6233 for-init-statement:
6234 expression-statement
6235 simple-declaration */
6236
6237static void
6238cp_parser_for_init_statement (parser)
6239 cp_parser *parser;
6240{
6241 /* If the next token is a `;', then we have an empty
6242 expression-statement. Gramatically, this is also a
6243 simple-declaration, but an invalid one, because it does not
6244 declare anything. Therefore, if we did not handle this case
6245 specially, we would issue an error message about an invalid
6246 declaration. */
6247 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6248 {
6249 /* We're going to speculatively look for a declaration, falling back
6250 to an expression, if necessary. */
6251 cp_parser_parse_tentatively (parser);
6252 /* Parse the declaration. */
6253 cp_parser_simple_declaration (parser,
6254 /*function_definition_allowed_p=*/false);
6255 /* If the tentative parse failed, then we shall need to look for an
6256 expression-statement. */
6257 if (cp_parser_parse_definitely (parser))
6258 return;
6259 }
6260
6261 cp_parser_expression_statement (parser);
6262}
6263
6264/* Parse a jump-statement.
6265
6266 jump-statement:
6267 break ;
6268 continue ;
6269 return expression [opt] ;
6270 goto identifier ;
6271
6272 GNU extension:
6273
6274 jump-statement:
6275 goto * expression ;
6276
6277 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6278 GOTO_STMT. */
6279
6280static tree
6281cp_parser_jump_statement (parser)
6282 cp_parser *parser;
6283{
6284 tree statement = error_mark_node;
6285 cp_token *token;
6286 enum rid keyword;
6287
6288 /* Peek at the next token. */
6289 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6290 if (!token)
6291 return error_mark_node;
6292
6293 /* See what kind of keyword it is. */
6294 keyword = token->keyword;
6295 switch (keyword)
6296 {
6297 case RID_BREAK:
6298 statement = finish_break_stmt ();
6299 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6300 break;
6301
6302 case RID_CONTINUE:
6303 statement = finish_continue_stmt ();
6304 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6305 break;
6306
6307 case RID_RETURN:
6308 {
6309 tree expr;
6310
6311 /* If the next token is a `;', then there is no
6312 expression. */
6313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6314 expr = cp_parser_expression (parser);
6315 else
6316 expr = NULL_TREE;
6317 /* Build the return-statement. */
6318 statement = finish_return_stmt (expr);
6319 /* Look for the final `;'. */
6320 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6321 }
6322 break;
6323
6324 case RID_GOTO:
6325 /* Create the goto-statement. */
6326 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6327 {
6328 /* Issue a warning about this use of a GNU extension. */
6329 if (pedantic)
6330 pedwarn ("ISO C++ forbids computed gotos");
6331 /* Consume the '*' token. */
6332 cp_lexer_consume_token (parser->lexer);
6333 /* Parse the dependent expression. */
6334 finish_goto_stmt (cp_parser_expression (parser));
6335 }
6336 else
6337 finish_goto_stmt (cp_parser_identifier (parser));
6338 /* Look for the final `;'. */
6339 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6340 break;
6341
6342 default:
6343 cp_parser_error (parser, "expected jump-statement");
6344 break;
6345 }
6346
6347 return statement;
6348}
6349
6350/* Parse a declaration-statement.
6351
6352 declaration-statement:
6353 block-declaration */
6354
6355static void
6356cp_parser_declaration_statement (parser)
6357 cp_parser *parser;
6358{
6359 /* Parse the block-declaration. */
6360 cp_parser_block_declaration (parser, /*statement_p=*/true);
6361
6362 /* Finish off the statement. */
6363 finish_stmt ();
6364}
6365
6366/* Some dependent statements (like `if (cond) statement'), are
6367 implicitly in their own scope. In other words, if the statement is
6368 a single statement (as opposed to a compound-statement), it is
6369 none-the-less treated as if it were enclosed in braces. Any
6370 declarations appearing in the dependent statement are out of scope
6371 after control passes that point. This function parses a statement,
6372 but ensures that is in its own scope, even if it is not a
6373 compound-statement.
6374
6375 Returns the new statement. */
6376
6377static tree
6378cp_parser_implicitly_scoped_statement (parser)
6379 cp_parser *parser;
6380{
6381 tree statement;
6382
6383 /* If the token is not a `{', then we must take special action. */
6384 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6385 {
6386 /* Create a compound-statement. */
6387 statement = begin_compound_stmt (/*has_no_scope=*/0);
6388 /* Parse the dependent-statement. */
6389 cp_parser_statement (parser);
6390 /* Finish the dummy compound-statement. */
6391 finish_compound_stmt (/*has_no_scope=*/0, statement);
6392 }
6393 /* Otherwise, we simply parse the statement directly. */
6394 else
6395 statement = cp_parser_compound_statement (parser);
6396
6397 /* Return the statement. */
6398 return statement;
6399}
6400
6401/* For some dependent statements (like `while (cond) statement'), we
6402 have already created a scope. Therefore, even if the dependent
6403 statement is a compound-statement, we do not want to create another
6404 scope. */
6405
6406static void
6407cp_parser_already_scoped_statement (parser)
6408 cp_parser *parser;
6409{
6410 /* If the token is not a `{', then we must take special action. */
6411 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6412 {
6413 tree statement;
6414
6415 /* Create a compound-statement. */
6416 statement = begin_compound_stmt (/*has_no_scope=*/1);
6417 /* Parse the dependent-statement. */
6418 cp_parser_statement (parser);
6419 /* Finish the dummy compound-statement. */
6420 finish_compound_stmt (/*has_no_scope=*/1, statement);
6421 }
6422 /* Otherwise, we simply parse the statement directly. */
6423 else
6424 cp_parser_statement (parser);
6425}
6426
6427/* Declarations [gram.dcl.dcl] */
6428
6429/* Parse an optional declaration-sequence.
6430
6431 declaration-seq:
6432 declaration
6433 declaration-seq declaration */
6434
6435static void
6436cp_parser_declaration_seq_opt (parser)
6437 cp_parser *parser;
6438{
6439 while (true)
6440 {
6441 cp_token *token;
6442
6443 token = cp_lexer_peek_token (parser->lexer);
6444
6445 if (token->type == CPP_CLOSE_BRACE
6446 || token->type == CPP_EOF)
6447 break;
6448
6449 if (token->type == CPP_SEMICOLON)
6450 {
6451 /* A declaration consisting of a single semicolon is
6452 invalid. Allow it unless we're being pedantic. */
6453 if (pedantic)
6454 pedwarn ("extra `;'");
6455 cp_lexer_consume_token (parser->lexer);
6456 continue;
6457 }
6458
6459 cp_parser_declaration (parser);
6460 }
6461}
6462
6463/* Parse a declaration.
6464
6465 declaration:
6466 block-declaration
6467 function-definition
6468 template-declaration
6469 explicit-instantiation
6470 explicit-specialization
6471 linkage-specification
6472 namespace-definition */
6473
6474static void
6475cp_parser_declaration (parser)
6476 cp_parser *parser;
6477{
6478 cp_token token1;
6479 cp_token token2;
6480
6481 /* Try to figure out what kind of declaration is present. */
6482 token1 = *cp_lexer_peek_token (parser->lexer);
6483 if (token1.type != CPP_EOF)
6484 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6485
6486 /* If the next token is `extern' and the following token is a string
6487 literal, then we have a linkage specification. */
6488 if (token1.keyword == RID_EXTERN
6489 && cp_parser_is_string_literal (&token2))
6490 cp_parser_linkage_specification (parser);
6491 /* If the next token is `template', then we have either a template
6492 declaration, an explicit instantiation, or an explicit
6493 specialization. */
6494 else if (token1.keyword == RID_TEMPLATE)
6495 {
6496 /* `template <>' indicates a template specialization. */
6497 if (token2.type == CPP_LESS
6498 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6499 cp_parser_explicit_specialization (parser);
6500 /* `template <' indicates a template declaration. */
6501 else if (token2.type == CPP_LESS)
6502 cp_parser_template_declaration (parser, /*member_p=*/false);
6503 /* Anything else must be an explicit instantiation. */
6504 else
6505 cp_parser_explicit_instantiation (parser);
6506 }
6507 /* If the next token is `export', then we have a template
6508 declaration. */
6509 else if (token1.keyword == RID_EXPORT)
6510 cp_parser_template_declaration (parser, /*member_p=*/false);
6511 /* If the next token is `extern', 'static' or 'inline' and the one
6512 after that is `template', we have a GNU extended explicit
6513 instantiation directive. */
6514 else if (cp_parser_allow_gnu_extensions_p (parser)
6515 && (token1.keyword == RID_EXTERN
6516 || token1.keyword == RID_STATIC
6517 || token1.keyword == RID_INLINE)
6518 && token2.keyword == RID_TEMPLATE)
6519 cp_parser_explicit_instantiation (parser);
6520 /* If the next token is `namespace', check for a named or unnamed
6521 namespace definition. */
6522 else if (token1.keyword == RID_NAMESPACE
6523 && (/* A named namespace definition. */
6524 (token2.type == CPP_NAME
6525 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6526 == CPP_OPEN_BRACE))
6527 /* An unnamed namespace definition. */
6528 || token2.type == CPP_OPEN_BRACE))
6529 cp_parser_namespace_definition (parser);
6530 /* We must have either a block declaration or a function
6531 definition. */
6532 else
6533 /* Try to parse a block-declaration, or a function-definition. */
6534 cp_parser_block_declaration (parser, /*statement_p=*/false);
6535}
6536
6537/* Parse a block-declaration.
6538
6539 block-declaration:
6540 simple-declaration
6541 asm-definition
6542 namespace-alias-definition
6543 using-declaration
6544 using-directive
6545
6546 GNU Extension:
6547
6548 block-declaration:
6549 __extension__ block-declaration
6550 label-declaration
6551
6552 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6553 part of a declaration-statement. */
6554
6555static void
6556cp_parser_block_declaration (cp_parser *parser,
6557 bool statement_p)
6558{
6559 cp_token *token1;
6560 int saved_pedantic;
6561
6562 /* Check for the `__extension__' keyword. */
6563 if (cp_parser_extension_opt (parser, &saved_pedantic))
6564 {
6565 /* Parse the qualified declaration. */
6566 cp_parser_block_declaration (parser, statement_p);
6567 /* Restore the PEDANTIC flag. */
6568 pedantic = saved_pedantic;
6569
6570 return;
6571 }
6572
6573 /* Peek at the next token to figure out which kind of declaration is
6574 present. */
6575 token1 = cp_lexer_peek_token (parser->lexer);
6576
6577 /* If the next keyword is `asm', we have an asm-definition. */
6578 if (token1->keyword == RID_ASM)
6579 {
6580 if (statement_p)
6581 cp_parser_commit_to_tentative_parse (parser);
6582 cp_parser_asm_definition (parser);
6583 }
6584 /* If the next keyword is `namespace', we have a
6585 namespace-alias-definition. */
6586 else if (token1->keyword == RID_NAMESPACE)
6587 cp_parser_namespace_alias_definition (parser);
6588 /* If the next keyword is `using', we have either a
6589 using-declaration or a using-directive. */
6590 else if (token1->keyword == RID_USING)
6591 {
6592 cp_token *token2;
6593
6594 if (statement_p)
6595 cp_parser_commit_to_tentative_parse (parser);
6596 /* If the token after `using' is `namespace', then we have a
6597 using-directive. */
6598 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6599 if (token2->keyword == RID_NAMESPACE)
6600 cp_parser_using_directive (parser);
6601 /* Otherwise, it's a using-declaration. */
6602 else
6603 cp_parser_using_declaration (parser);
6604 }
6605 /* If the next keyword is `__label__' we have a label declaration. */
6606 else if (token1->keyword == RID_LABEL)
6607 {
6608 if (statement_p)
6609 cp_parser_commit_to_tentative_parse (parser);
6610 cp_parser_label_declaration (parser);
6611 }
6612 /* Anything else must be a simple-declaration. */
6613 else
6614 cp_parser_simple_declaration (parser, !statement_p);
6615}
6616
6617/* Parse a simple-declaration.
6618
6619 simple-declaration:
6620 decl-specifier-seq [opt] init-declarator-list [opt] ;
6621
6622 init-declarator-list:
6623 init-declarator
6624 init-declarator-list , init-declarator
6625
6626 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6627 function-definition as a simple-declaration. */
6628
6629static void
6630cp_parser_simple_declaration (parser, function_definition_allowed_p)
6631 cp_parser *parser;
6632 bool function_definition_allowed_p;
6633{
6634 tree decl_specifiers;
6635 tree attributes;
6636 tree access_checks;
6637 bool declares_class_or_enum;
6638 bool saw_declarator;
6639
6640 /* Defer access checks until we know what is being declared; the
6641 checks for names appearing in the decl-specifier-seq should be
6642 done as if we were in the scope of the thing being declared. */
6643 cp_parser_start_deferring_access_checks (parser);
6644 /* Parse the decl-specifier-seq. We have to keep track of whether
6645 or not the decl-specifier-seq declares a named class or
6646 enumeration type, since that is the only case in which the
6647 init-declarator-list is allowed to be empty.
6648
6649 [dcl.dcl]
6650
6651 In a simple-declaration, the optional init-declarator-list can be
6652 omitted only when declaring a class or enumeration, that is when
6653 the decl-specifier-seq contains either a class-specifier, an
6654 elaborated-type-specifier, or an enum-specifier. */
6655 decl_specifiers
6656 = cp_parser_decl_specifier_seq (parser,
6657 CP_PARSER_FLAGS_OPTIONAL,
6658 &attributes,
6659 &declares_class_or_enum);
6660 /* We no longer need to defer access checks. */
6661 access_checks = cp_parser_stop_deferring_access_checks (parser);
6662
6663 /* Keep going until we hit the `;' at the end of the simple
6664 declaration. */
6665 saw_declarator = false;
6666 while (cp_lexer_next_token_is_not (parser->lexer,
6667 CPP_SEMICOLON))
6668 {
6669 cp_token *token;
6670 bool function_definition_p;
6671
6672 saw_declarator = true;
6673 /* Parse the init-declarator. */
6674 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6675 access_checks,
6676 function_definition_allowed_p,
6677 /*member_p=*/false,
6678 &function_definition_p);
6679 /* Handle function definitions specially. */
6680 if (function_definition_p)
6681 {
6682 /* If the next token is a `,', then we are probably
6683 processing something like:
6684
6685 void f() {}, *p;
6686
6687 which is erroneous. */
6688 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6689 error ("mixing declarations and function-definitions is forbidden");
6690 /* Otherwise, we're done with the list of declarators. */
6691 else
6692 return;
6693 }
6694 /* The next token should be either a `,' or a `;'. */
6695 token = cp_lexer_peek_token (parser->lexer);
6696 /* If it's a `,', there are more declarators to come. */
6697 if (token->type == CPP_COMMA)
6698 cp_lexer_consume_token (parser->lexer);
6699 /* If it's a `;', we are done. */
6700 else if (token->type == CPP_SEMICOLON)
6701 break;
6702 /* Anything else is an error. */
6703 else
6704 {
6705 cp_parser_error (parser, "expected `,' or `;'");
6706 /* Skip tokens until we reach the end of the statement. */
6707 cp_parser_skip_to_end_of_statement (parser);
6708 return;
6709 }
6710 /* After the first time around, a function-definition is not
6711 allowed -- even if it was OK at first. For example:
6712
6713 int i, f() {}
6714
6715 is not valid. */
6716 function_definition_allowed_p = false;
6717 }
6718
6719 /* Issue an error message if no declarators are present, and the
6720 decl-specifier-seq does not itself declare a class or
6721 enumeration. */
6722 if (!saw_declarator)
6723 {
6724 if (cp_parser_declares_only_class_p (parser))
6725 shadow_tag (decl_specifiers);
6726 /* Perform any deferred access checks. */
6727 cp_parser_perform_deferred_access_checks (access_checks);
6728 }
6729
6730 /* Consume the `;'. */
6731 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6732
6733 /* Mark all the classes that appeared in the decl-specifier-seq as
6734 having received a `;'. */
6735 note_list_got_semicolon (decl_specifiers);
6736}
6737
6738/* Parse a decl-specifier-seq.
6739
6740 decl-specifier-seq:
6741 decl-specifier-seq [opt] decl-specifier
6742
6743 decl-specifier:
6744 storage-class-specifier
6745 type-specifier
6746 function-specifier
6747 friend
6748 typedef
6749
6750 GNU Extension:
6751
6752 decl-specifier-seq:
6753 decl-specifier-seq [opt] attributes
6754
6755 Returns a TREE_LIST, giving the decl-specifiers in the order they
6756 appear in the source code. The TREE_VALUE of each node is the
6757 decl-specifier. For a keyword (such as `auto' or `friend'), the
6758 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6759 representation of a type-specifier, see cp_parser_type_specifier.
6760
6761 If there are attributes, they will be stored in *ATTRIBUTES,
6762 represented as described above cp_parser_attributes.
6763
6764 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6765 appears, and the entity that will be a friend is not going to be a
6766 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6767 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6768 friendship is granted might not be a class. */
6769
6770static tree
6771cp_parser_decl_specifier_seq (parser, flags, attributes,
6772 declares_class_or_enum)
6773 cp_parser *parser;
6774 cp_parser_flags flags;
6775 tree *attributes;
6776 bool *declares_class_or_enum;
6777{
6778 tree decl_specs = NULL_TREE;
6779 bool friend_p = false;
6780
6781 /* Assume no class or enumeration type is declared. */
6782 *declares_class_or_enum = false;
6783
6784 /* Assume there are no attributes. */
6785 *attributes = NULL_TREE;
6786
6787 /* Keep reading specifiers until there are no more to read. */
6788 while (true)
6789 {
6790 tree decl_spec = NULL_TREE;
6791 bool constructor_p;
6792 cp_token *token;
6793
6794 /* Peek at the next token. */
6795 token = cp_lexer_peek_token (parser->lexer);
6796 /* Handle attributes. */
6797 if (token->keyword == RID_ATTRIBUTE)
6798 {
6799 /* Parse the attributes. */
6800 decl_spec = cp_parser_attributes_opt (parser);
6801 /* Add them to the list. */
6802 *attributes = chainon (*attributes, decl_spec);
6803 continue;
6804 }
6805 /* If the next token is an appropriate keyword, we can simply
6806 add it to the list. */
6807 switch (token->keyword)
6808 {
6809 case RID_FRIEND:
6810 /* decl-specifier:
6811 friend */
6812 friend_p = true;
6813 /* The representation of the specifier is simply the
6814 appropriate TREE_IDENTIFIER node. */
6815 decl_spec = token->value;
6816 /* Consume the token. */
6817 cp_lexer_consume_token (parser->lexer);
6818 break;
6819
6820 /* function-specifier:
6821 inline
6822 virtual
6823 explicit */
6824 case RID_INLINE:
6825 case RID_VIRTUAL:
6826 case RID_EXPLICIT:
6827 decl_spec = cp_parser_function_specifier_opt (parser);
6828 break;
6829
6830 /* decl-specifier:
6831 typedef */
6832 case RID_TYPEDEF:
6833 /* The representation of the specifier is simply the
6834 appropriate TREE_IDENTIFIER node. */
6835 decl_spec = token->value;
6836 /* Consume the token. */
6837 cp_lexer_consume_token (parser->lexer);
6838 break;
6839
6840 /* storage-class-specifier:
6841 auto
6842 register
6843 static
6844 extern
6845 mutable
6846
6847 GNU Extension:
6848 thread */
6849 case RID_AUTO:
6850 case RID_REGISTER:
6851 case RID_STATIC:
6852 case RID_EXTERN:
6853 case RID_MUTABLE:
6854 case RID_THREAD:
6855 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6856 break;
6857
6858 default:
6859 break;
6860 }
6861
6862 /* Constructors are a special case. The `S' in `S()' is not a
6863 decl-specifier; it is the beginning of the declarator. */
6864 constructor_p = (!decl_spec
6865 && cp_parser_constructor_declarator_p (parser,
6866 friend_p));
6867
6868 /* If we don't have a DECL_SPEC yet, then we must be looking at
6869 a type-specifier. */
6870 if (!decl_spec && !constructor_p)
6871 {
6872 bool decl_spec_declares_class_or_enum;
6873 bool is_cv_qualifier;
6874
6875 decl_spec
6876 = cp_parser_type_specifier (parser, flags,
6877 friend_p,
6878 /*is_declaration=*/true,
6879 &decl_spec_declares_class_or_enum,
6880 &is_cv_qualifier);
6881
6882 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6883
6884 /* If this type-specifier referenced a user-defined type
6885 (a typedef, class-name, etc.), then we can't allow any
6886 more such type-specifiers henceforth.
6887
6888 [dcl.spec]
6889
6890 The longest sequence of decl-specifiers that could
6891 possibly be a type name is taken as the
6892 decl-specifier-seq of a declaration. The sequence shall
6893 be self-consistent as described below.
6894
6895 [dcl.type]
6896
6897 As a general rule, at most one type-specifier is allowed
6898 in the complete decl-specifier-seq of a declaration. The
6899 only exceptions are the following:
6900
6901 -- const or volatile can be combined with any other
6902 type-specifier.
6903
6904 -- signed or unsigned can be combined with char, long,
6905 short, or int.
6906
6907 -- ..
6908
6909 Example:
6910
6911 typedef char* Pc;
6912 void g (const int Pc);
6913
6914 Here, Pc is *not* part of the decl-specifier seq; it's
6915 the declarator. Therefore, once we see a type-specifier
6916 (other than a cv-qualifier), we forbid any additional
6917 user-defined types. We *do* still allow things like `int
6918 int' to be considered a decl-specifier-seq, and issue the
6919 error message later. */
6920 if (decl_spec && !is_cv_qualifier)
6921 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6922 }
6923
6924 /* If we still do not have a DECL_SPEC, then there are no more
6925 decl-specifiers. */
6926 if (!decl_spec)
6927 {
6928 /* Issue an error message, unless the entire construct was
6929 optional. */
6930 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6931 {
6932 cp_parser_error (parser, "expected decl specifier");
6933 return error_mark_node;
6934 }
6935
6936 break;
6937 }
6938
6939 /* Add the DECL_SPEC to the list of specifiers. */
6940 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6941
6942 /* After we see one decl-specifier, further decl-specifiers are
6943 always optional. */
6944 flags |= CP_PARSER_FLAGS_OPTIONAL;
6945 }
6946
6947 /* We have built up the DECL_SPECS in reverse order. Return them in
6948 the correct order. */
6949 return nreverse (decl_specs);
6950}
6951
6952/* Parse an (optional) storage-class-specifier.
6953
6954 storage-class-specifier:
6955 auto
6956 register
6957 static
6958 extern
6959 mutable
6960
6961 GNU Extension:
6962
6963 storage-class-specifier:
6964 thread
6965
6966 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6967
6968static tree
6969cp_parser_storage_class_specifier_opt (parser)
6970 cp_parser *parser;
6971{
6972 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6973 {
6974 case RID_AUTO:
6975 case RID_REGISTER:
6976 case RID_STATIC:
6977 case RID_EXTERN:
6978 case RID_MUTABLE:
6979 case RID_THREAD:
6980 /* Consume the token. */
6981 return cp_lexer_consume_token (parser->lexer)->value;
6982
6983 default:
6984 return NULL_TREE;
6985 }
6986}
6987
6988/* Parse an (optional) function-specifier.
6989
6990 function-specifier:
6991 inline
6992 virtual
6993 explicit
6994
6995 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6996
6997static tree
6998cp_parser_function_specifier_opt (parser)
6999 cp_parser *parser;
7000{
7001 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7002 {
7003 case RID_INLINE:
7004 case RID_VIRTUAL:
7005 case RID_EXPLICIT:
7006 /* Consume the token. */
7007 return cp_lexer_consume_token (parser->lexer)->value;
7008
7009 default:
7010 return NULL_TREE;
7011 }
7012}
7013
7014/* Parse a linkage-specification.
7015
7016 linkage-specification:
7017 extern string-literal { declaration-seq [opt] }
7018 extern string-literal declaration */
7019
7020static void
7021cp_parser_linkage_specification (parser)
7022 cp_parser *parser;
7023{
7024 cp_token *token;
7025 tree linkage;
7026
7027 /* Look for the `extern' keyword. */
7028 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7029
7030 /* Peek at the next token. */
7031 token = cp_lexer_peek_token (parser->lexer);
7032 /* If it's not a string-literal, then there's a problem. */
7033 if (!cp_parser_is_string_literal (token))
7034 {
7035 cp_parser_error (parser, "expected language-name");
7036 return;
7037 }
7038 /* Consume the token. */
7039 cp_lexer_consume_token (parser->lexer);
7040
7041 /* Transform the literal into an identifier. If the literal is a
7042 wide-character string, or contains embedded NULs, then we can't
7043 handle it as the user wants. */
7044 if (token->type == CPP_WSTRING
7045 || (strlen (TREE_STRING_POINTER (token->value))
7046 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7047 {
7048 cp_parser_error (parser, "invalid linkage-specification");
7049 /* Assume C++ linkage. */
7050 linkage = get_identifier ("c++");
7051 }
7052 /* If it's a simple string constant, things are easier. */
7053 else
7054 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7055
7056 /* We're now using the new linkage. */
7057 push_lang_context (linkage);
7058
7059 /* If the next token is a `{', then we're using the first
7060 production. */
7061 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7062 {
7063 /* Consume the `{' token. */
7064 cp_lexer_consume_token (parser->lexer);
7065 /* Parse the declarations. */
7066 cp_parser_declaration_seq_opt (parser);
7067 /* Look for the closing `}'. */
7068 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7069 }
7070 /* Otherwise, there's just one declaration. */
7071 else
7072 {
7073 bool saved_in_unbraced_linkage_specification_p;
7074
7075 saved_in_unbraced_linkage_specification_p
7076 = parser->in_unbraced_linkage_specification_p;
7077 parser->in_unbraced_linkage_specification_p = true;
7078 have_extern_spec = true;
7079 cp_parser_declaration (parser);
7080 have_extern_spec = false;
7081 parser->in_unbraced_linkage_specification_p
7082 = saved_in_unbraced_linkage_specification_p;
7083 }
7084
7085 /* We're done with the linkage-specification. */
7086 pop_lang_context ();
7087}
7088
7089/* Special member functions [gram.special] */
7090
7091/* Parse a conversion-function-id.
7092
7093 conversion-function-id:
7094 operator conversion-type-id
7095
7096 Returns an IDENTIFIER_NODE representing the operator. */
7097
7098static tree
7099cp_parser_conversion_function_id (parser)
7100 cp_parser *parser;
7101{
7102 tree type;
7103 tree saved_scope;
7104 tree saved_qualifying_scope;
7105 tree saved_object_scope;
7106
7107 /* Look for the `operator' token. */
7108 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7109 return error_mark_node;
7110 /* When we parse the conversion-type-id, the current scope will be
7111 reset. However, we need that information in able to look up the
7112 conversion function later, so we save it here. */
7113 saved_scope = parser->scope;
7114 saved_qualifying_scope = parser->qualifying_scope;
7115 saved_object_scope = parser->object_scope;
7116 /* We must enter the scope of the class so that the names of
7117 entities declared within the class are available in the
7118 conversion-type-id. For example, consider:
7119
7120 struct S {
7121 typedef int I;
7122 operator I();
7123 };
7124
7125 S::operator I() { ... }
7126
7127 In order to see that `I' is a type-name in the definition, we
7128 must be in the scope of `S'. */
7129 if (saved_scope)
7130 push_scope (saved_scope);
7131 /* Parse the conversion-type-id. */
7132 type = cp_parser_conversion_type_id (parser);
7133 /* Leave the scope of the class, if any. */
7134 if (saved_scope)
7135 pop_scope (saved_scope);
7136 /* Restore the saved scope. */
7137 parser->scope = saved_scope;
7138 parser->qualifying_scope = saved_qualifying_scope;
7139 parser->object_scope = saved_object_scope;
7140 /* If the TYPE is invalid, indicate failure. */
7141 if (type == error_mark_node)
7142 return error_mark_node;
7143 return mangle_conv_op_name_for_type (type);
7144}
7145
7146/* Parse a conversion-type-id:
7147
7148 conversion-type-id:
7149 type-specifier-seq conversion-declarator [opt]
7150
7151 Returns the TYPE specified. */
7152
7153static tree
7154cp_parser_conversion_type_id (parser)
7155 cp_parser *parser;
7156{
7157 tree attributes;
7158 tree type_specifiers;
7159 tree declarator;
7160
7161 /* Parse the attributes. */
7162 attributes = cp_parser_attributes_opt (parser);
7163 /* Parse the type-specifiers. */
7164 type_specifiers = cp_parser_type_specifier_seq (parser);
7165 /* If that didn't work, stop. */
7166 if (type_specifiers == error_mark_node)
7167 return error_mark_node;
7168 /* Parse the conversion-declarator. */
7169 declarator = cp_parser_conversion_declarator_opt (parser);
7170
7171 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7172 /*initialized=*/0, &attributes);
7173}
7174
7175/* Parse an (optional) conversion-declarator.
7176
7177 conversion-declarator:
7178 ptr-operator conversion-declarator [opt]
7179
7180 Returns a representation of the declarator. See
7181 cp_parser_declarator for details. */
7182
7183static tree
7184cp_parser_conversion_declarator_opt (parser)
7185 cp_parser *parser;
7186{
7187 enum tree_code code;
7188 tree class_type;
7189 tree cv_qualifier_seq;
7190
7191 /* We don't know if there's a ptr-operator next, or not. */
7192 cp_parser_parse_tentatively (parser);
7193 /* Try the ptr-operator. */
7194 code = cp_parser_ptr_operator (parser, &class_type,
7195 &cv_qualifier_seq);
7196 /* If it worked, look for more conversion-declarators. */
7197 if (cp_parser_parse_definitely (parser))
7198 {
7199 tree declarator;
7200
7201 /* Parse another optional declarator. */
7202 declarator = cp_parser_conversion_declarator_opt (parser);
7203
7204 /* Create the representation of the declarator. */
7205 if (code == INDIRECT_REF)
7206 declarator = make_pointer_declarator (cv_qualifier_seq,
7207 declarator);
7208 else
7209 declarator = make_reference_declarator (cv_qualifier_seq,
7210 declarator);
7211
7212 /* Handle the pointer-to-member case. */
7213 if (class_type)
7214 declarator = build_nt (SCOPE_REF, class_type, declarator);
7215
7216 return declarator;
7217 }
7218
7219 return NULL_TREE;
7220}
7221
7222/* Parse an (optional) ctor-initializer.
7223
7224 ctor-initializer:
7225 : mem-initializer-list
7226
7227 Returns TRUE iff the ctor-initializer was actually present. */
7228
7229static bool
7230cp_parser_ctor_initializer_opt (parser)
7231 cp_parser *parser;
7232{
7233 /* If the next token is not a `:', then there is no
7234 ctor-initializer. */
7235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7236 {
7237 /* Do default initialization of any bases and members. */
7238 if (DECL_CONSTRUCTOR_P (current_function_decl))
7239 finish_mem_initializers (NULL_TREE);
7240
7241 return false;
7242 }
7243
7244 /* Consume the `:' token. */
7245 cp_lexer_consume_token (parser->lexer);
7246 /* And the mem-initializer-list. */
7247 cp_parser_mem_initializer_list (parser);
7248
7249 return true;
7250}
7251
7252/* Parse a mem-initializer-list.
7253
7254 mem-initializer-list:
7255 mem-initializer
7256 mem-initializer , mem-initializer-list */
7257
7258static void
7259cp_parser_mem_initializer_list (parser)
7260 cp_parser *parser;
7261{
7262 tree mem_initializer_list = NULL_TREE;
7263
7264 /* Let the semantic analysis code know that we are starting the
7265 mem-initializer-list. */
7266 begin_mem_initializers ();
7267
7268 /* Loop through the list. */
7269 while (true)
7270 {
7271 tree mem_initializer;
7272
7273 /* Parse the mem-initializer. */
7274 mem_initializer = cp_parser_mem_initializer (parser);
7275 /* Add it to the list, unless it was erroneous. */
7276 if (mem_initializer)
7277 {
7278 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7279 mem_initializer_list = mem_initializer;
7280 }
7281 /* If the next token is not a `,', we're done. */
7282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7283 break;
7284 /* Consume the `,' token. */
7285 cp_lexer_consume_token (parser->lexer);
7286 }
7287
7288 /* Perform semantic analysis. */
7289 finish_mem_initializers (mem_initializer_list);
7290}
7291
7292/* Parse a mem-initializer.
7293
7294 mem-initializer:
7295 mem-initializer-id ( expression-list [opt] )
7296
7297 GNU extension:
7298
7299 mem-initializer:
7300 ( expresion-list [opt] )
7301
7302 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7303 class) or FIELD_DECL (for a non-static data member) to initialize;
7304 the TREE_VALUE is the expression-list. */
7305
7306static tree
7307cp_parser_mem_initializer (parser)
7308 cp_parser *parser;
7309{
7310 tree mem_initializer_id;
7311 tree expression_list;
7312
7313 /* Find out what is being initialized. */
7314 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7315 {
7316 pedwarn ("anachronistic old-style base class initializer");
7317 mem_initializer_id = NULL_TREE;
7318 }
7319 else
7320 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7321 /* Look for the opening `('. */
7322 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7323 /* Parse the expression-list. */
7324 if (cp_lexer_next_token_is_not (parser->lexer,
7325 CPP_CLOSE_PAREN))
7326 expression_list = cp_parser_expression_list (parser);
7327 else
7328 expression_list = void_type_node;
7329 /* Look for the closing `)'. */
7330 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7331
7332 return expand_member_init (mem_initializer_id,
7333 expression_list);
7334}
7335
7336/* Parse a mem-initializer-id.
7337
7338 mem-initializer-id:
7339 :: [opt] nested-name-specifier [opt] class-name
7340 identifier
7341
7342 Returns a TYPE indicating the class to be initializer for the first
7343 production. Returns an IDENTIFIER_NODE indicating the data member
7344 to be initialized for the second production. */
7345
7346static tree
7347cp_parser_mem_initializer_id (parser)
7348 cp_parser *parser;
7349{
7350 bool global_scope_p;
7351 bool nested_name_specifier_p;
7352 tree id;
7353
7354 /* Look for the optional `::' operator. */
7355 global_scope_p
7356 = (cp_parser_global_scope_opt (parser,
7357 /*current_scope_valid_p=*/false)
7358 != NULL_TREE);
7359 /* Look for the optional nested-name-specifier. The simplest way to
7360 implement:
7361
7362 [temp.res]
7363
7364 The keyword `typename' is not permitted in a base-specifier or
7365 mem-initializer; in these contexts a qualified name that
7366 depends on a template-parameter is implicitly assumed to be a
7367 type name.
7368
7369 is to assume that we have seen the `typename' keyword at this
7370 point. */
7371 nested_name_specifier_p
7372 = (cp_parser_nested_name_specifier_opt (parser,
7373 /*typename_keyword_p=*/true,
7374 /*check_dependency_p=*/true,
7375 /*type_p=*/true)
7376 != NULL_TREE);
7377 /* If there is a `::' operator or a nested-name-specifier, then we
7378 are definitely looking for a class-name. */
7379 if (global_scope_p || nested_name_specifier_p)
7380 return cp_parser_class_name (parser,
7381 /*typename_keyword_p=*/true,
7382 /*template_keyword_p=*/false,
7383 /*type_p=*/false,
7384 /*check_access_p=*/true,
7385 /*check_dependency_p=*/true,
7386 /*class_head_p=*/false);
7387 /* Otherwise, we could also be looking for an ordinary identifier. */
7388 cp_parser_parse_tentatively (parser);
7389 /* Try a class-name. */
7390 id = cp_parser_class_name (parser,
7391 /*typename_keyword_p=*/true,
7392 /*template_keyword_p=*/false,
7393 /*type_p=*/false,
7394 /*check_access_p=*/true,
7395 /*check_dependency_p=*/true,
7396 /*class_head_p=*/false);
7397 /* If we found one, we're done. */
7398 if (cp_parser_parse_definitely (parser))
7399 return id;
7400 /* Otherwise, look for an ordinary identifier. */
7401 return cp_parser_identifier (parser);
7402}
7403
7404/* Overloading [gram.over] */
7405
7406/* Parse an operator-function-id.
7407
7408 operator-function-id:
7409 operator operator
7410
7411 Returns an IDENTIFIER_NODE for the operator which is a
7412 human-readable spelling of the identifier, e.g., `operator +'. */
7413
7414static tree
7415cp_parser_operator_function_id (parser)
7416 cp_parser *parser;
7417{
7418 /* Look for the `operator' keyword. */
7419 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7420 return error_mark_node;
7421 /* And then the name of the operator itself. */
7422 return cp_parser_operator (parser);
7423}
7424
7425/* Parse an operator.
7426
7427 operator:
7428 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7429 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7430 || ++ -- , ->* -> () []
7431
7432 GNU Extensions:
7433
7434 operator:
7435 <? >? <?= >?=
7436
7437 Returns an IDENTIFIER_NODE for the operator which is a
7438 human-readable spelling of the identifier, e.g., `operator +'. */
7439
7440static tree
7441cp_parser_operator (parser)
7442 cp_parser *parser;
7443{
7444 tree id = NULL_TREE;
7445 cp_token *token;
7446
7447 /* Peek at the next token. */
7448 token = cp_lexer_peek_token (parser->lexer);
7449 /* Figure out which operator we have. */
7450 switch (token->type)
7451 {
7452 case CPP_KEYWORD:
7453 {
7454 enum tree_code op;
7455
7456 /* The keyword should be either `new' or `delete'. */
7457 if (token->keyword == RID_NEW)
7458 op = NEW_EXPR;
7459 else if (token->keyword == RID_DELETE)
7460 op = DELETE_EXPR;
7461 else
7462 break;
7463
7464 /* Consume the `new' or `delete' token. */
7465 cp_lexer_consume_token (parser->lexer);
7466
7467 /* Peek at the next token. */
7468 token = cp_lexer_peek_token (parser->lexer);
7469 /* If it's a `[' token then this is the array variant of the
7470 operator. */
7471 if (token->type == CPP_OPEN_SQUARE)
7472 {
7473 /* Consume the `[' token. */
7474 cp_lexer_consume_token (parser->lexer);
7475 /* Look for the `]' token. */
7476 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7477 id = ansi_opname (op == NEW_EXPR
7478 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7479 }
7480 /* Otherwise, we have the non-array variant. */
7481 else
7482 id = ansi_opname (op);
7483
7484 return id;
7485 }
7486
7487 case CPP_PLUS:
7488 id = ansi_opname (PLUS_EXPR);
7489 break;
7490
7491 case CPP_MINUS:
7492 id = ansi_opname (MINUS_EXPR);
7493 break;
7494
7495 case CPP_MULT:
7496 id = ansi_opname (MULT_EXPR);
7497 break;
7498
7499 case CPP_DIV:
7500 id = ansi_opname (TRUNC_DIV_EXPR);
7501 break;
7502
7503 case CPP_MOD:
7504 id = ansi_opname (TRUNC_MOD_EXPR);
7505 break;
7506
7507 case CPP_XOR:
7508 id = ansi_opname (BIT_XOR_EXPR);
7509 break;
7510
7511 case CPP_AND:
7512 id = ansi_opname (BIT_AND_EXPR);
7513 break;
7514
7515 case CPP_OR:
7516 id = ansi_opname (BIT_IOR_EXPR);
7517 break;
7518
7519 case CPP_COMPL:
7520 id = ansi_opname (BIT_NOT_EXPR);
7521 break;
7522
7523 case CPP_NOT:
7524 id = ansi_opname (TRUTH_NOT_EXPR);
7525 break;
7526
7527 case CPP_EQ:
7528 id = ansi_assopname (NOP_EXPR);
7529 break;
7530
7531 case CPP_LESS:
7532 id = ansi_opname (LT_EXPR);
7533 break;
7534
7535 case CPP_GREATER:
7536 id = ansi_opname (GT_EXPR);
7537 break;
7538
7539 case CPP_PLUS_EQ:
7540 id = ansi_assopname (PLUS_EXPR);
7541 break;
7542
7543 case CPP_MINUS_EQ:
7544 id = ansi_assopname (MINUS_EXPR);
7545 break;
7546
7547 case CPP_MULT_EQ:
7548 id = ansi_assopname (MULT_EXPR);
7549 break;
7550
7551 case CPP_DIV_EQ:
7552 id = ansi_assopname (TRUNC_DIV_EXPR);
7553 break;
7554
7555 case CPP_MOD_EQ:
7556 id = ansi_assopname (TRUNC_MOD_EXPR);
7557 break;
7558
7559 case CPP_XOR_EQ:
7560 id = ansi_assopname (BIT_XOR_EXPR);
7561 break;
7562
7563 case CPP_AND_EQ:
7564 id = ansi_assopname (BIT_AND_EXPR);
7565 break;
7566
7567 case CPP_OR_EQ:
7568 id = ansi_assopname (BIT_IOR_EXPR);
7569 break;
7570
7571 case CPP_LSHIFT:
7572 id = ansi_opname (LSHIFT_EXPR);
7573 break;
7574
7575 case CPP_RSHIFT:
7576 id = ansi_opname (RSHIFT_EXPR);
7577 break;
7578
7579 case CPP_LSHIFT_EQ:
7580 id = ansi_assopname (LSHIFT_EXPR);
7581 break;
7582
7583 case CPP_RSHIFT_EQ:
7584 id = ansi_assopname (RSHIFT_EXPR);
7585 break;
7586
7587 case CPP_EQ_EQ:
7588 id = ansi_opname (EQ_EXPR);
7589 break;
7590
7591 case CPP_NOT_EQ:
7592 id = ansi_opname (NE_EXPR);
7593 break;
7594
7595 case CPP_LESS_EQ:
7596 id = ansi_opname (LE_EXPR);
7597 break;
7598
7599 case CPP_GREATER_EQ:
7600 id = ansi_opname (GE_EXPR);
7601 break;
7602
7603 case CPP_AND_AND:
7604 id = ansi_opname (TRUTH_ANDIF_EXPR);
7605 break;
7606
7607 case CPP_OR_OR:
7608 id = ansi_opname (TRUTH_ORIF_EXPR);
7609 break;
7610
7611 case CPP_PLUS_PLUS:
7612 id = ansi_opname (POSTINCREMENT_EXPR);
7613 break;
7614
7615 case CPP_MINUS_MINUS:
7616 id = ansi_opname (PREDECREMENT_EXPR);
7617 break;
7618
7619 case CPP_COMMA:
7620 id = ansi_opname (COMPOUND_EXPR);
7621 break;
7622
7623 case CPP_DEREF_STAR:
7624 id = ansi_opname (MEMBER_REF);
7625 break;
7626
7627 case CPP_DEREF:
7628 id = ansi_opname (COMPONENT_REF);
7629 break;
7630
7631 case CPP_OPEN_PAREN:
7632 /* Consume the `('. */
7633 cp_lexer_consume_token (parser->lexer);
7634 /* Look for the matching `)'. */
7635 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7636 return ansi_opname (CALL_EXPR);
7637
7638 case CPP_OPEN_SQUARE:
7639 /* Consume the `['. */
7640 cp_lexer_consume_token (parser->lexer);
7641 /* Look for the matching `]'. */
7642 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7643 return ansi_opname (ARRAY_REF);
7644
7645 /* Extensions. */
7646 case CPP_MIN:
7647 id = ansi_opname (MIN_EXPR);
7648 break;
7649
7650 case CPP_MAX:
7651 id = ansi_opname (MAX_EXPR);
7652 break;
7653
7654 case CPP_MIN_EQ:
7655 id = ansi_assopname (MIN_EXPR);
7656 break;
7657
7658 case CPP_MAX_EQ:
7659 id = ansi_assopname (MAX_EXPR);
7660 break;
7661
7662 default:
7663 /* Anything else is an error. */
7664 break;
7665 }
7666
7667 /* If we have selected an identifier, we need to consume the
7668 operator token. */
7669 if (id)
7670 cp_lexer_consume_token (parser->lexer);
7671 /* Otherwise, no valid operator name was present. */
7672 else
7673 {
7674 cp_parser_error (parser, "expected operator");
7675 id = error_mark_node;
7676 }
7677
7678 return id;
7679}
7680
7681/* Parse a template-declaration.
7682
7683 template-declaration:
7684 export [opt] template < template-parameter-list > declaration
7685
7686 If MEMBER_P is TRUE, this template-declaration occurs within a
7687 class-specifier.
7688
7689 The grammar rule given by the standard isn't correct. What
7690 is really meant is:
7691
7692 template-declaration:
7693 export [opt] template-parameter-list-seq
7694 decl-specifier-seq [opt] init-declarator [opt] ;
7695 export [opt] template-parameter-list-seq
7696 function-definition
7697
7698 template-parameter-list-seq:
7699 template-parameter-list-seq [opt]
7700 template < template-parameter-list > */
7701
7702static void
7703cp_parser_template_declaration (parser, member_p)
7704 cp_parser *parser;
7705 bool member_p;
7706{
7707 /* Check for `export'. */
7708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7709 {
7710 /* Consume the `export' token. */
7711 cp_lexer_consume_token (parser->lexer);
7712 /* Warn that we do not support `export'. */
7713 warning ("keyword `export' not implemented, and will be ignored");
7714 }
7715
7716 cp_parser_template_declaration_after_export (parser, member_p);
7717}
7718
7719/* Parse a template-parameter-list.
7720
7721 template-parameter-list:
7722 template-parameter
7723 template-parameter-list , template-parameter
7724
7725 Returns a TREE_LIST. Each node represents a template parameter.
7726 The nodes are connected via their TREE_CHAINs. */
7727
7728static tree
7729cp_parser_template_parameter_list (parser)
7730 cp_parser *parser;
7731{
7732 tree parameter_list = NULL_TREE;
7733
7734 while (true)
7735 {
7736 tree parameter;
7737 cp_token *token;
7738
7739 /* Parse the template-parameter. */
7740 parameter = cp_parser_template_parameter (parser);
7741 /* Add it to the list. */
7742 parameter_list = process_template_parm (parameter_list,
7743 parameter);
7744
7745 /* Peek at the next token. */
7746 token = cp_lexer_peek_token (parser->lexer);
7747 /* If it's not a `,', we're done. */
7748 if (token->type != CPP_COMMA)
7749 break;
7750 /* Otherwise, consume the `,' token. */
7751 cp_lexer_consume_token (parser->lexer);
7752 }
7753
7754 return parameter_list;
7755}
7756
7757/* Parse a template-parameter.
7758
7759 template-parameter:
7760 type-parameter
7761 parameter-declaration
7762
7763 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7764 TREE_PURPOSE is the default value, if any. */
7765
7766static tree
7767cp_parser_template_parameter (parser)
7768 cp_parser *parser;
7769{
7770 cp_token *token;
7771
7772 /* Peek at the next token. */
7773 token = cp_lexer_peek_token (parser->lexer);
7774 /* If it is `class' or `template', we have a type-parameter. */
7775 if (token->keyword == RID_TEMPLATE)
7776 return cp_parser_type_parameter (parser);
7777 /* If it is `class' or `typename' we do not know yet whether it is a
7778 type parameter or a non-type parameter. Consider:
7779
7780 template <typename T, typename T::X X> ...
7781
7782 or:
7783
7784 template <class C, class D*> ...
7785
7786 Here, the first parameter is a type parameter, and the second is
7787 a non-type parameter. We can tell by looking at the token after
7788 the identifier -- if it is a `,', `=', or `>' then we have a type
7789 parameter. */
7790 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7791 {
7792 /* Peek at the token after `class' or `typename'. */
7793 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7794 /* If it's an identifier, skip it. */
7795 if (token->type == CPP_NAME)
7796 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7797 /* Now, see if the token looks like the end of a template
7798 parameter. */
7799 if (token->type == CPP_COMMA
7800 || token->type == CPP_EQ
7801 || token->type == CPP_GREATER)
7802 return cp_parser_type_parameter (parser);
7803 }
7804
7805 /* Otherwise, it is a non-type parameter.
7806
7807 [temp.param]
7808
7809 When parsing a default template-argument for a non-type
7810 template-parameter, the first non-nested `>' is taken as the end
7811 of the template parameter-list rather than a greater-than
7812 operator. */
7813 return
7814 cp_parser_parameter_declaration (parser,
7815 /*greater_than_is_operator_p=*/false);
7816}
7817
7818/* Parse a type-parameter.
7819
7820 type-parameter:
7821 class identifier [opt]
7822 class identifier [opt] = type-id
7823 typename identifier [opt]
7824 typename identifier [opt] = type-id
7825 template < template-parameter-list > class identifier [opt]
7826 template < template-parameter-list > class identifier [opt]
7827 = id-expression
7828
7829 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7830 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7831 the declaration of the parameter. */
7832
7833static tree
7834cp_parser_type_parameter (parser)
7835 cp_parser *parser;
7836{
7837 cp_token *token;
7838 tree parameter;
7839
7840 /* Look for a keyword to tell us what kind of parameter this is. */
7841 token = cp_parser_require (parser, CPP_KEYWORD,
7842 "expected `class', `typename', or `template'");
7843 if (!token)
7844 return error_mark_node;
7845
7846 switch (token->keyword)
7847 {
7848 case RID_CLASS:
7849 case RID_TYPENAME:
7850 {
7851 tree identifier;
7852 tree default_argument;
7853
7854 /* If the next token is an identifier, then it names the
7855 parameter. */
7856 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7857 identifier = cp_parser_identifier (parser);
7858 else
7859 identifier = NULL_TREE;
7860
7861 /* Create the parameter. */
7862 parameter = finish_template_type_parm (class_type_node, identifier);
7863
7864 /* If the next token is an `=', we have a default argument. */
7865 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7866 {
7867 /* Consume the `=' token. */
7868 cp_lexer_consume_token (parser->lexer);
7869 /* Parse the default-argumen. */
7870 default_argument = cp_parser_type_id (parser);
7871 }
7872 else
7873 default_argument = NULL_TREE;
7874
7875 /* Create the combined representation of the parameter and the
7876 default argument. */
7877 parameter = build_tree_list (default_argument,
7878 parameter);
7879 }
7880 break;
7881
7882 case RID_TEMPLATE:
7883 {
7884 tree parameter_list;
7885 tree identifier;
7886 tree default_argument;
7887
7888 /* Look for the `<'. */
7889 cp_parser_require (parser, CPP_LESS, "`<'");
7890 /* Parse the template-parameter-list. */
7891 begin_template_parm_list ();
7892 parameter_list
7893 = cp_parser_template_parameter_list (parser);
7894 parameter_list = end_template_parm_list (parameter_list);
7895 /* Look for the `>'. */
7896 cp_parser_require (parser, CPP_GREATER, "`>'");
7897 /* Look for the `class' keyword. */
7898 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7899 /* If the next token is an `=', then there is a
7900 default-argument. If the next token is a `>', we are at
7901 the end of the parameter-list. If the next token is a `,',
7902 then we are at the end of this parameter. */
7903 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7904 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7905 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7906 identifier = cp_parser_identifier (parser);
7907 else
7908 identifier = NULL_TREE;
7909 /* Create the template parameter. */
7910 parameter = finish_template_template_parm (class_type_node,
7911 identifier);
7912
7913 /* If the next token is an `=', then there is a
7914 default-argument. */
7915 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7916 {
7917 /* Consume the `='. */
7918 cp_lexer_consume_token (parser->lexer);
7919 /* Parse the id-expression. */
7920 default_argument
7921 = cp_parser_id_expression (parser,
7922 /*template_keyword_p=*/false,
7923 /*check_dependency_p=*/true,
7924 /*template_p=*/NULL);
7925 /* Look up the name. */
7926 default_argument
7927 = cp_parser_lookup_name_simple (parser, default_argument);
7928 /* See if the default argument is valid. */
7929 default_argument
7930 = check_template_template_default_arg (default_argument);
7931 }
7932 else
7933 default_argument = NULL_TREE;
7934
7935 /* Create the combined representation of the parameter and the
7936 default argument. */
7937 parameter = build_tree_list (default_argument,
7938 parameter);
7939 }
7940 break;
7941
7942 default:
7943 /* Anything else is an error. */
7944 cp_parser_error (parser,
7945 "expected `class', `typename', or `template'");
7946 parameter = error_mark_node;
7947 }
7948
7949 return parameter;
7950}
7951
7952/* Parse a template-id.
7953
7954 template-id:
7955 template-name < template-argument-list [opt] >
7956
7957 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7958 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7959 returned. Otherwise, if the template-name names a function, or set
7960 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7961 names a class, returns a TYPE_DECL for the specialization.
7962
7963 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7964 uninstantiated templates. */
7965
7966static tree
7967cp_parser_template_id (cp_parser *parser,
7968 bool template_keyword_p,
7969 bool check_dependency_p)
7970{
7971 tree template;
7972 tree arguments;
7973 tree saved_scope;
7974 tree saved_qualifying_scope;
7975 tree saved_object_scope;
7976 tree template_id;
7977 bool saved_greater_than_is_operator_p;
7978 ptrdiff_t start_of_id;
7979 tree access_check = NULL_TREE;
7980
7981 /* If the next token corresponds to a template-id, there is no need
7982 to reparse it. */
7983 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
7984 {
7985 tree value;
7986 tree check;
7987
7988 /* Get the stored value. */
7989 value = cp_lexer_consume_token (parser->lexer)->value;
7990 /* Perform any access checks that were deferred. */
7991 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7992 cp_parser_defer_access_check (parser,
7993 TREE_PURPOSE (check),
7994 TREE_VALUE (check));
7995 /* Return the stored value. */
7996 return TREE_VALUE (value);
7997 }
7998
7999 /* Remember where the template-id starts. */
8000 if (cp_parser_parsing_tentatively (parser)
8001 && !cp_parser_committed_to_tentative_parse (parser))
8002 {
8003 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8004 start_of_id = cp_lexer_token_difference (parser->lexer,
8005 parser->lexer->first_token,
8006 next_token);
8007 access_check = parser->context->deferred_access_checks;
8008 }
8009 else
8010 start_of_id = -1;
8011
8012 /* Parse the template-name. */
8013 template = cp_parser_template_name (parser, template_keyword_p,
8014 check_dependency_p);
8015 if (template == error_mark_node)
8016 return error_mark_node;
8017
8018 /* Look for the `<' that starts the template-argument-list. */
8019 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8020 return error_mark_node;
8021
8022 /* [temp.names]
8023
8024 When parsing a template-id, the first non-nested `>' is taken as
8025 the end of the template-argument-list rather than a greater-than
8026 operator. */
8027 saved_greater_than_is_operator_p
8028 = parser->greater_than_is_operator_p;
8029 parser->greater_than_is_operator_p = false;
8030 /* Parsing the argument list may modify SCOPE, so we save it
8031 here. */
8032 saved_scope = parser->scope;
8033 saved_qualifying_scope = parser->qualifying_scope;
8034 saved_object_scope = parser->object_scope;
8035 /* Parse the template-argument-list itself. */
8036 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8037 arguments = NULL_TREE;
8038 else
8039 arguments = cp_parser_template_argument_list (parser);
8040 /* Look for the `>' that ends the template-argument-list. */
8041 cp_parser_require (parser, CPP_GREATER, "`>'");
8042 /* The `>' token might be a greater-than operator again now. */
8043 parser->greater_than_is_operator_p
8044 = saved_greater_than_is_operator_p;
8045 /* Restore the SAVED_SCOPE. */
8046 parser->scope = saved_scope;
8047 parser->qualifying_scope = saved_qualifying_scope;
8048 parser->object_scope = saved_object_scope;
8049
8050 /* Build a representation of the specialization. */
8051 if (TREE_CODE (template) == IDENTIFIER_NODE)
8052 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8053 else if (DECL_CLASS_TEMPLATE_P (template)
8054 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8055 template_id
8056 = finish_template_type (template, arguments,
8057 cp_lexer_next_token_is (parser->lexer,
8058 CPP_SCOPE));
8059 else
8060 {
8061 /* If it's not a class-template or a template-template, it should be
8062 a function-template. */
8063 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8064 || TREE_CODE (template) == OVERLOAD
8065 || BASELINK_P (template)),
8066 20010716);
8067
8068 template_id = lookup_template_function (template, arguments);
8069 }
8070
8071 /* If parsing tentatively, replace the sequence of tokens that makes
8072 up the template-id with a CPP_TEMPLATE_ID token. That way,
8073 should we re-parse the token stream, we will not have to repeat
8074 the effort required to do the parse, nor will we issue duplicate
8075 error messages about problems during instantiation of the
8076 template. */
8077 if (start_of_id >= 0)
8078 {
8079 cp_token *token;
8080 tree c;
8081
8082 /* Find the token that corresponds to the start of the
8083 template-id. */
8084 token = cp_lexer_advance_token (parser->lexer,
8085 parser->lexer->first_token,
8086 start_of_id);
8087
8088 /* Remember the access checks associated with this
8089 nested-name-specifier. */
8090 c = parser->context->deferred_access_checks;
8091 if (c == access_check)
8092 access_check = NULL_TREE;
8093 else
8094 {
8095 while (TREE_CHAIN (c) != access_check)
8096 c = TREE_CHAIN (c);
8097 access_check = parser->context->deferred_access_checks;
8098 parser->context->deferred_access_checks = TREE_CHAIN (c);
8099 TREE_CHAIN (c) = NULL_TREE;
8100 }
8101
8102 /* Reset the contents of the START_OF_ID token. */
8103 token->type = CPP_TEMPLATE_ID;
8104 token->value = build_tree_list (access_check, template_id);
8105 token->keyword = RID_MAX;
8106 /* Purge all subsequent tokens. */
8107 cp_lexer_purge_tokens_after (parser->lexer, token);
8108 }
8109
8110 return template_id;
8111}
8112
8113/* Parse a template-name.
8114
8115 template-name:
8116 identifier
8117
8118 The standard should actually say:
8119
8120 template-name:
8121 identifier
8122 operator-function-id
8123 conversion-function-id
8124
8125 A defect report has been filed about this issue.
8126
8127 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8128 `template' keyword, in a construction like:
8129
8130 T::template f<3>()
8131
8132 In that case `f' is taken to be a template-name, even though there
8133 is no way of knowing for sure.
8134
8135 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8136 name refers to a set of overloaded functions, at least one of which
8137 is a template, or an IDENTIFIER_NODE with the name of the template,
8138 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8139 names are looked up inside uninstantiated templates. */
8140
8141static tree
8142cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8143 cp_parser *parser;
8144 bool template_keyword_p;
8145 bool check_dependency_p;
8146{
8147 tree identifier;
8148 tree decl;
8149 tree fns;
8150
8151 /* If the next token is `operator', then we have either an
8152 operator-function-id or a conversion-function-id. */
8153 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8154 {
8155 /* We don't know whether we're looking at an
8156 operator-function-id or a conversion-function-id. */
8157 cp_parser_parse_tentatively (parser);
8158 /* Try an operator-function-id. */
8159 identifier = cp_parser_operator_function_id (parser);
8160 /* If that didn't work, try a conversion-function-id. */
8161 if (!cp_parser_parse_definitely (parser))
8162 identifier = cp_parser_conversion_function_id (parser);
8163 }
8164 /* Look for the identifier. */
8165 else
8166 identifier = cp_parser_identifier (parser);
8167
8168 /* If we didn't find an identifier, we don't have a template-id. */
8169 if (identifier == error_mark_node)
8170 return error_mark_node;
8171
8172 /* If the name immediately followed the `template' keyword, then it
8173 is a template-name. However, if the next token is not `<', then
8174 we do not treat it as a template-name, since it is not being used
8175 as part of a template-id. This enables us to handle constructs
8176 like:
8177
8178 template <typename T> struct S { S(); };
8179 template <typename T> S<T>::S();
8180
8181 correctly. We would treat `S' as a template -- if it were `S<T>'
8182 -- but we do not if there is no `<'. */
8183 if (template_keyword_p && processing_template_decl
8184 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8185 return identifier;
8186
8187 /* Look up the name. */
8188 decl = cp_parser_lookup_name (parser, identifier,
8189 /*check_access=*/true,
8190 /*is_type=*/false,
8191 check_dependency_p);
8192 decl = maybe_get_template_decl_from_type_decl (decl);
8193
8194 /* If DECL is a template, then the name was a template-name. */
8195 if (TREE_CODE (decl) == TEMPLATE_DECL)
8196 ;
8197 else
8198 {
8199 /* The standard does not explicitly indicate whether a name that
8200 names a set of overloaded declarations, some of which are
8201 templates, is a template-name. However, such a name should
8202 be a template-name; otherwise, there is no way to form a
8203 template-id for the overloaded templates. */
8204 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8205 if (TREE_CODE (fns) == OVERLOAD)
8206 {
8207 tree fn;
8208
8209 for (fn = fns; fn; fn = OVL_NEXT (fn))
8210 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8211 break;
8212 }
8213 else
8214 {
8215 /* Otherwise, the name does not name a template. */
8216 cp_parser_error (parser, "expected template-name");
8217 return error_mark_node;
8218 }
8219 }
8220
8221 /* If DECL is dependent, and refers to a function, then just return
8222 its name; we will look it up again during template instantiation. */
8223 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8224 {
8225 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8226 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8227 return identifier;
8228 }
8229
8230 return decl;
8231}
8232
8233/* Parse a template-argument-list.
8234
8235 template-argument-list:
8236 template-argument
8237 template-argument-list , template-argument
8238
8239 Returns a TREE_LIST representing the arguments, in the order they
8240 appeared. The TREE_VALUE of each node is a representation of the
8241 argument. */
8242
8243static tree
8244cp_parser_template_argument_list (parser)
8245 cp_parser *parser;
8246{
8247 tree arguments = NULL_TREE;
8248
8249 while (true)
8250 {
8251 tree argument;
8252
8253 /* Parse the template-argument. */
8254 argument = cp_parser_template_argument (parser);
8255 /* Add it to the list. */
8256 arguments = tree_cons (NULL_TREE, argument, arguments);
8257 /* If it is not a `,', then there are no more arguments. */
8258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8259 break;
8260 /* Otherwise, consume the ','. */
8261 cp_lexer_consume_token (parser->lexer);
8262 }
8263
8264 /* We built up the arguments in reverse order. */
8265 return nreverse (arguments);
8266}
8267
8268/* Parse a template-argument.
8269
8270 template-argument:
8271 assignment-expression
8272 type-id
8273 id-expression
8274
8275 The representation is that of an assignment-expression, type-id, or
8276 id-expression -- except that the qualified id-expression is
8277 evaluated, so that the value returned is either a DECL or an
8278 OVERLOAD. */
8279
8280static tree
8281cp_parser_template_argument (parser)
8282 cp_parser *parser;
8283{
8284 tree argument;
8285 bool template_p;
8286
8287 /* There's really no way to know what we're looking at, so we just
8288 try each alternative in order.
8289
8290 [temp.arg]
8291
8292 In a template-argument, an ambiguity between a type-id and an
8293 expression is resolved to a type-id, regardless of the form of
8294 the corresponding template-parameter.
8295
8296 Therefore, we try a type-id first. */
8297 cp_parser_parse_tentatively (parser);
8298 /* Otherwise, try a type-id. */
8299 argument = cp_parser_type_id (parser);
8300 /* If the next token isn't a `,' or a `>', then this argument wasn't
8301 really finished. */
8302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8303 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8304 cp_parser_error (parser, "expected template-argument");
8305 /* If that worked, we're done. */
8306 if (cp_parser_parse_definitely (parser))
8307 return argument;
8308 /* We're still not sure what the argument will be. */
8309 cp_parser_parse_tentatively (parser);
8310 /* Try a template. */
8311 argument = cp_parser_id_expression (parser,
8312 /*template_keyword_p=*/false,
8313 /*check_dependency_p=*/true,
8314 &template_p);
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 (!cp_parser_error_occurred (parser))
8321 {
8322 /* Figure out what is being referred to. */
8323 argument = cp_parser_lookup_name_simple (parser, argument);
8324 if (template_p)
8325 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8326 TREE_OPERAND (argument, 1),
8327 tf_error | tf_parsing);
8328 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8329 cp_parser_error (parser, "expected template-name");
8330 }
8331 if (cp_parser_parse_definitely (parser))
8332 return argument;
8333 /* It must be an assignment-expression. */
8334 return cp_parser_assignment_expression (parser);
8335}
8336
8337/* Parse an explicit-instantiation.
8338
8339 explicit-instantiation:
8340 template declaration
8341
8342 Although the standard says `declaration', what it really means is:
8343
8344 explicit-instantiation:
8345 template decl-specifier-seq [opt] declarator [opt] ;
8346
8347 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8348 supposed to be allowed. A defect report has been filed about this
8349 issue.
8350
8351 GNU Extension:
8352
8353 explicit-instantiation:
8354 storage-class-specifier template
8355 decl-specifier-seq [opt] declarator [opt] ;
8356 function-specifier template
8357 decl-specifier-seq [opt] declarator [opt] ; */
8358
8359static void
8360cp_parser_explicit_instantiation (parser)
8361 cp_parser *parser;
8362{
8363 bool declares_class_or_enum;
8364 tree decl_specifiers;
8365 tree attributes;
8366 tree extension_specifier = NULL_TREE;
8367
8368 /* Look for an (optional) storage-class-specifier or
8369 function-specifier. */
8370 if (cp_parser_allow_gnu_extensions_p (parser))
8371 {
8372 extension_specifier
8373 = cp_parser_storage_class_specifier_opt (parser);
8374 if (!extension_specifier)
8375 extension_specifier = cp_parser_function_specifier_opt (parser);
8376 }
8377
8378 /* Look for the `template' keyword. */
8379 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8380 /* Let the front end know that we are processing an explicit
8381 instantiation. */
8382 begin_explicit_instantiation ();
8383 /* [temp.explicit] says that we are supposed to ignore access
8384 control while processing explicit instantiation directives. */
8385 scope_chain->check_access = 0;
8386 /* Parse a decl-specifier-seq. */
8387 decl_specifiers
8388 = cp_parser_decl_specifier_seq (parser,
8389 CP_PARSER_FLAGS_OPTIONAL,
8390 &attributes,
8391 &declares_class_or_enum);
8392 /* If there was exactly one decl-specifier, and it declared a class,
8393 and there's no declarator, then we have an explicit type
8394 instantiation. */
8395 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8396 {
8397 tree type;
8398
8399 type = check_tag_decl (decl_specifiers);
8400 if (type)
8401 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8402 }
8403 else
8404 {
8405 tree declarator;
8406 tree decl;
8407
8408 /* Parse the declarator. */
8409 declarator
8410 = cp_parser_declarator (parser,
8411 /*abstract_p=*/false,
8412 /*ctor_dtor_or_conv_p=*/NULL);
8413 decl = grokdeclarator (declarator, decl_specifiers,
8414 NORMAL, 0, NULL);
8415 /* Do the explicit instantiation. */
8416 do_decl_instantiation (decl, extension_specifier);
8417 }
8418 /* We're done with the instantiation. */
8419 end_explicit_instantiation ();
8420 /* Trun access control back on. */
8421 scope_chain->check_access = flag_access_control;
8422
8423 /* Look for the trailing `;'. */
8424 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8425}
8426
8427/* Parse an explicit-specialization.
8428
8429 explicit-specialization:
8430 template < > declaration
8431
8432 Although the standard says `declaration', what it really means is:
8433
8434 explicit-specialization:
8435 template <> decl-specifier [opt] init-declarator [opt] ;
8436 template <> function-definition
8437 template <> explicit-specialization
8438 template <> template-declaration */
8439
8440static void
8441cp_parser_explicit_specialization (parser)
8442 cp_parser *parser;
8443{
8444 /* Look for the `template' keyword. */
8445 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8446 /* Look for the `<'. */
8447 cp_parser_require (parser, CPP_LESS, "`<'");
8448 /* Look for the `>'. */
8449 cp_parser_require (parser, CPP_GREATER, "`>'");
8450 /* We have processed another parameter list. */
8451 ++parser->num_template_parameter_lists;
8452 /* Let the front end know that we are beginning a specialization. */
8453 begin_specialization ();
8454
8455 /* If the next keyword is `template', we need to figure out whether
8456 or not we're looking a template-declaration. */
8457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8458 {
8459 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8460 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8461 cp_parser_template_declaration_after_export (parser,
8462 /*member_p=*/false);
8463 else
8464 cp_parser_explicit_specialization (parser);
8465 }
8466 else
8467 /* Parse the dependent declaration. */
8468 cp_parser_single_declaration (parser,
8469 /*member_p=*/false,
8470 /*friend_p=*/NULL);
8471
8472 /* We're done with the specialization. */
8473 end_specialization ();
8474 /* We're done with this parameter list. */
8475 --parser->num_template_parameter_lists;
8476}
8477
8478/* Parse a type-specifier.
8479
8480 type-specifier:
8481 simple-type-specifier
8482 class-specifier
8483 enum-specifier
8484 elaborated-type-specifier
8485 cv-qualifier
8486
8487 GNU Extension:
8488
8489 type-specifier:
8490 __complex__
8491
8492 Returns a representation of the type-specifier. If the
8493 type-specifier is a keyword (like `int' or `const', or
8494 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8495 For a class-specifier, enum-specifier, or elaborated-type-specifier
8496 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8497
8498 If IS_FRIEND is TRUE then this type-specifier is being declared a
8499 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8500 appearing in a decl-specifier-seq.
8501
8502 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8503 class-specifier, enum-specifier, or elaborated-type-specifier, then
8504 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8505 FALSE.
8506
8507 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8508 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8509 is set to FALSE. */
8510
8511static tree
8512cp_parser_type_specifier (parser,
8513 flags,
8514 is_friend,
8515 is_declaration,
8516 declares_class_or_enum,
8517 is_cv_qualifier)
8518 cp_parser *parser;
8519 cp_parser_flags flags;
8520 bool is_friend;
8521 bool is_declaration;
8522 bool *declares_class_or_enum;
8523 bool *is_cv_qualifier;
8524{
8525 tree type_spec = NULL_TREE;
8526 cp_token *token;
8527 enum rid keyword;
8528
8529 /* Assume this type-specifier does not declare a new type. */
8530 if (declares_class_or_enum)
8531 *declares_class_or_enum = false;
8532 /* And that it does not specify a cv-qualifier. */
8533 if (is_cv_qualifier)
8534 *is_cv_qualifier = false;
8535 /* Peek at the next token. */
8536 token = cp_lexer_peek_token (parser->lexer);
8537
8538 /* If we're looking at a keyword, we can use that to guide the
8539 production we choose. */
8540 keyword = token->keyword;
8541 switch (keyword)
8542 {
8543 /* Any of these indicate either a class-specifier, or an
8544 elaborated-type-specifier. */
8545 case RID_CLASS:
8546 case RID_STRUCT:
8547 case RID_UNION:
8548 case RID_ENUM:
8549 /* Parse tentatively so that we can back up if we don't find a
8550 class-specifier or enum-specifier. */
8551 cp_parser_parse_tentatively (parser);
8552 /* Look for the class-specifier or enum-specifier. */
8553 if (keyword == RID_ENUM)
8554 type_spec = cp_parser_enum_specifier (parser);
8555 else
8556 type_spec = cp_parser_class_specifier (parser);
8557
8558 /* If that worked, we're done. */
8559 if (cp_parser_parse_definitely (parser))
8560 {
8561 if (declares_class_or_enum)
8562 *declares_class_or_enum = true;
8563 return type_spec;
8564 }
8565
8566 /* Fall through. */
8567
8568 case RID_TYPENAME:
8569 /* Look for an elaborated-type-specifier. */
8570 type_spec = cp_parser_elaborated_type_specifier (parser,
8571 is_friend,
8572 is_declaration);
8573 /* We're declaring a class or enum -- unless we're using
8574 `typename'. */
8575 if (declares_class_or_enum && keyword != RID_TYPENAME)
8576 *declares_class_or_enum = true;
8577 return type_spec;
8578
8579 case RID_CONST:
8580 case RID_VOLATILE:
8581 case RID_RESTRICT:
8582 type_spec = cp_parser_cv_qualifier_opt (parser);
8583 /* Even though we call a routine that looks for an optional
8584 qualifier, we know that there should be one. */
8585 my_friendly_assert (type_spec != NULL, 20000328);
8586 /* This type-specifier was a cv-qualified. */
8587 if (is_cv_qualifier)
8588 *is_cv_qualifier = true;
8589
8590 return type_spec;
8591
8592 case RID_COMPLEX:
8593 /* The `__complex__' keyword is a GNU extension. */
8594 return cp_lexer_consume_token (parser->lexer)->value;
8595
8596 default:
8597 break;
8598 }
8599
8600 /* If we do not already have a type-specifier, assume we are looking
8601 at a simple-type-specifier. */
8602 type_spec = cp_parser_simple_type_specifier (parser, flags);
8603
8604 /* If we didn't find a type-specifier, and a type-specifier was not
8605 optional in this context, issue an error message. */
8606 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8607 {
8608 cp_parser_error (parser, "expected type specifier");
8609 return error_mark_node;
8610 }
8611
8612 return type_spec;
8613}
8614
8615/* Parse a simple-type-specifier.
8616
8617 simple-type-specifier:
8618 :: [opt] nested-name-specifier [opt] type-name
8619 :: [opt] nested-name-specifier template template-id
8620 char
8621 wchar_t
8622 bool
8623 short
8624 int
8625 long
8626 signed
8627 unsigned
8628 float
8629 double
8630 void
8631
8632 GNU Extension:
8633
8634 simple-type-specifier:
8635 __typeof__ unary-expression
8636 __typeof__ ( type-id )
8637
8638 For the various keywords, the value returned is simply the
8639 TREE_IDENTIFIER representing the keyword. For the first two
8640 productions, the value returned is the indicated TYPE_DECL. */
8641
8642static tree
8643cp_parser_simple_type_specifier (parser, flags)
8644 cp_parser *parser;
8645 cp_parser_flags flags;
8646{
8647 tree type = NULL_TREE;
8648 cp_token *token;
8649
8650 /* Peek at the next token. */
8651 token = cp_lexer_peek_token (parser->lexer);
8652
8653 /* If we're looking at a keyword, things are easy. */
8654 switch (token->keyword)
8655 {
8656 case RID_CHAR:
8657 case RID_WCHAR:
8658 case RID_BOOL:
8659 case RID_SHORT:
8660 case RID_INT:
8661 case RID_LONG:
8662 case RID_SIGNED:
8663 case RID_UNSIGNED:
8664 case RID_FLOAT:
8665 case RID_DOUBLE:
8666 case RID_VOID:
8667 /* Consume the token. */
8668 return cp_lexer_consume_token (parser->lexer)->value;
8669
8670 case RID_TYPEOF:
8671 {
8672 tree operand;
8673
8674 /* Consume the `typeof' token. */
8675 cp_lexer_consume_token (parser->lexer);
8676 /* Parse the operand to `typeof' */
8677 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8678 /* If it is not already a TYPE, take its type. */
8679 if (!TYPE_P (operand))
8680 operand = finish_typeof (operand);
8681
8682 return operand;
8683 }
8684
8685 default:
8686 break;
8687 }
8688
8689 /* The type-specifier must be a user-defined type. */
8690 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8691 {
8692 /* Don't gobble tokens or issue error messages if this is an
8693 optional type-specifier. */
8694 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8695 cp_parser_parse_tentatively (parser);
8696
8697 /* Look for the optional `::' operator. */
8698 cp_parser_global_scope_opt (parser,
8699 /*current_scope_valid_p=*/false);
8700 /* Look for the nested-name specifier. */
8701 cp_parser_nested_name_specifier_opt (parser,
8702 /*typename_keyword_p=*/false,
8703 /*check_dependency_p=*/true,
8704 /*type_p=*/false);
8705 /* If we have seen a nested-name-specifier, and the next token
8706 is `template', then we are using the template-id production. */
8707 if (parser->scope
8708 && cp_parser_optional_template_keyword (parser))
8709 {
8710 /* Look for the template-id. */
8711 type = cp_parser_template_id (parser,
8712 /*template_keyword_p=*/true,
8713 /*check_dependency_p=*/true);
8714 /* If the template-id did not name a type, we are out of
8715 luck. */
8716 if (TREE_CODE (type) != TYPE_DECL)
8717 {
8718 cp_parser_error (parser, "expected template-id for type");
8719 type = NULL_TREE;
8720 }
8721 }
8722 /* Otherwise, look for a type-name. */
8723 else
8724 {
8725 type = cp_parser_type_name (parser);
8726 if (type == error_mark_node)
8727 type = NULL_TREE;
8728 }
8729
8730 /* If it didn't work out, we don't have a TYPE. */
8731 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8732 && !cp_parser_parse_definitely (parser))
8733 type = NULL_TREE;
8734 }
8735
8736 /* If we didn't get a type-name, issue an error message. */
8737 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8738 {
8739 cp_parser_error (parser, "expected type-name");
8740 return error_mark_node;
8741 }
8742
8743 return type;
8744}
8745
8746/* Parse a type-name.
8747
8748 type-name:
8749 class-name
8750 enum-name
8751 typedef-name
8752
8753 enum-name:
8754 identifier
8755
8756 typedef-name:
8757 identifier
8758
8759 Returns a TYPE_DECL for the the type. */
8760
8761static tree
8762cp_parser_type_name (parser)
8763 cp_parser *parser;
8764{
8765 tree type_decl;
8766 tree identifier;
8767
8768 /* We can't know yet whether it is a class-name or not. */
8769 cp_parser_parse_tentatively (parser);
8770 /* Try a class-name. */
8771 type_decl = cp_parser_class_name (parser,
8772 /*typename_keyword_p=*/false,
8773 /*template_keyword_p=*/false,
8774 /*type_p=*/false,
8775 /*check_access_p=*/true,
8776 /*check_dependency_p=*/true,
8777 /*class_head_p=*/false);
8778 /* If it's not a class-name, keep looking. */
8779 if (!cp_parser_parse_definitely (parser))
8780 {
8781 /* It must be a typedef-name or an enum-name. */
8782 identifier = cp_parser_identifier (parser);
8783 if (identifier == error_mark_node)
8784 return error_mark_node;
8785
8786 /* Look up the type-name. */
8787 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8788 /* Issue an error if we did not find a type-name. */
8789 if (TREE_CODE (type_decl) != TYPE_DECL)
8790 {
8791 cp_parser_error (parser, "expected type-name");
8792 type_decl = error_mark_node;
8793 }
8794 /* Remember that the name was used in the definition of the
8795 current class so that we can check later to see if the
8796 meaning would have been different after the class was
8797 entirely defined. */
8798 else if (type_decl != error_mark_node
8799 && !parser->scope)
8800 maybe_note_name_used_in_class (identifier, type_decl);
8801 }
8802
8803 return type_decl;
8804}
8805
8806
8807/* Parse an elaborated-type-specifier. Note that the grammar given
8808 here incorporates the resolution to DR68.
8809
8810 elaborated-type-specifier:
8811 class-key :: [opt] nested-name-specifier [opt] identifier
8812 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8813 enum :: [opt] nested-name-specifier [opt] identifier
8814 typename :: [opt] nested-name-specifier identifier
8815 typename :: [opt] nested-name-specifier template [opt]
8816 template-id
8817
8818 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8819 declared `friend'. If IS_DECLARATION is TRUE, then this
8820 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8821 something is being declared.
8822
8823 Returns the TYPE specified. */
8824
8825static tree
8826cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8827 cp_parser *parser;
8828 bool is_friend;
8829 bool is_declaration;
8830{
8831 enum tag_types tag_type;
8832 tree identifier;
8833 tree type = NULL_TREE;
8834
8835 /* See if we're looking at the `enum' keyword. */
8836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8837 {
8838 /* Consume the `enum' token. */
8839 cp_lexer_consume_token (parser->lexer);
8840 /* Remember that it's an enumeration type. */
8841 tag_type = enum_type;
8842 }
8843 /* Or, it might be `typename'. */
8844 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8845 RID_TYPENAME))
8846 {
8847 /* Consume the `typename' token. */
8848 cp_lexer_consume_token (parser->lexer);
8849 /* Remember that it's a `typename' type. */
8850 tag_type = typename_type;
8851 /* The `typename' keyword is only allowed in templates. */
8852 if (!processing_template_decl)
8853 pedwarn ("using `typename' outside of template");
8854 }
8855 /* Otherwise it must be a class-key. */
8856 else
8857 {
8858 tag_type = cp_parser_class_key (parser);
8859 if (tag_type == none_type)
8860 return error_mark_node;
8861 }
8862
8863 /* Look for the `::' operator. */
8864 cp_parser_global_scope_opt (parser,
8865 /*current_scope_valid_p=*/false);
8866 /* Look for the nested-name-specifier. */
8867 if (tag_type == typename_type)
8868 cp_parser_nested_name_specifier (parser,
8869 /*typename_keyword_p=*/true,
8870 /*check_dependency_p=*/true,
8871 /*type_p=*/true);
8872 else
8873 /* Even though `typename' is not present, the proposed resolution
8874 to Core Issue 180 says that in `class A<T>::B', `B' should be
8875 considered a type-name, even if `A<T>' is dependent. */
8876 cp_parser_nested_name_specifier_opt (parser,
8877 /*typename_keyword_p=*/true,
8878 /*check_dependency_p=*/true,
8879 /*type_p=*/true);
8880 /* For everything but enumeration types, consider a template-id. */
8881 if (tag_type != enum_type)
8882 {
8883 bool template_p = false;
8884 tree decl;
8885
8886 /* Allow the `template' keyword. */
8887 template_p = cp_parser_optional_template_keyword (parser);
8888 /* If we didn't see `template', we don't know if there's a
8889 template-id or not. */
8890 if (!template_p)
8891 cp_parser_parse_tentatively (parser);
8892 /* Parse the template-id. */
8893 decl = cp_parser_template_id (parser, template_p,
8894 /*check_dependency_p=*/true);
8895 /* If we didn't find a template-id, look for an ordinary
8896 identifier. */
8897 if (!template_p && !cp_parser_parse_definitely (parser))
8898 ;
8899 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8900 in effect, then we must assume that, upon instantiation, the
8901 template will correspond to a class. */
8902 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8903 && tag_type == typename_type)
8904 type = make_typename_type (parser->scope, decl,
8905 /*complain=*/1);
8906 else
8907 type = TREE_TYPE (decl);
8908 }
8909
8910 /* For an enumeration type, consider only a plain identifier. */
8911 if (!type)
8912 {
8913 identifier = cp_parser_identifier (parser);
8914
8915 if (identifier == error_mark_node)
8916 return error_mark_node;
8917
8918 /* For a `typename', we needn't call xref_tag. */
8919 if (tag_type == typename_type)
8920 return make_typename_type (parser->scope, identifier,
8921 /*complain=*/1);
8922 /* Look up a qualified name in the usual way. */
8923 if (parser->scope)
8924 {
8925 tree decl;
8926
8927 /* In an elaborated-type-specifier, names are assumed to name
8928 types, so we set IS_TYPE to TRUE when calling
8929 cp_parser_lookup_name. */
8930 decl = cp_parser_lookup_name (parser, identifier,
8931 /*check_access=*/true,
8932 /*is_type=*/true,
8933 /*check_dependency=*/true);
8934 decl = (cp_parser_maybe_treat_template_as_class
8935 (decl, /*tag_name_p=*/is_friend));
8936
8937 if (TREE_CODE (decl) != TYPE_DECL)
8938 {
8939 error ("expected type-name");
8940 return error_mark_node;
8941 }
8942 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8943 && tag_type != enum_type)
8944 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8945 tag_type == record_type ? "struct" : "class");
8946 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8947 && tag_type == enum_type)
8948 error ("`%T' referred to as enum", TREE_TYPE (decl));
8949
8950 type = TREE_TYPE (decl);
8951 }
8952 else
8953 {
8954 /* An elaborated-type-specifier sometimes introduces a new type and
8955 sometimes names an existing type. Normally, the rule is that it
8956 introduces a new type only if there is not an existing type of
8957 the same name already in scope. For example, given:
8958
8959 struct S {};
8960 void f() { struct S s; }
8961
8962 the `struct S' in the body of `f' is the same `struct S' as in
8963 the global scope; the existing definition is used. However, if
8964 there were no global declaration, this would introduce a new
8965 local class named `S'.
8966
8967 An exception to this rule applies to the following code:
8968
8969 namespace N { struct S; }
8970
8971 Here, the elaborated-type-specifier names a new type
8972 unconditionally; even if there is already an `S' in the
8973 containing scope this declaration names a new type.
8974 This exception only applies if the elaborated-type-specifier
8975 forms the complete declaration:
8976
8977 [class.name]
8978
8979 A declaration consisting solely of `class-key identifier ;' is
8980 either a redeclaration of the name in the current scope or a
8981 forward declaration of the identifier as a class name. It
8982 introduces the name into the current scope.
8983
8984 We are in this situation precisely when the next token is a `;'.
8985
8986 An exception to the exception is that a `friend' declaration does
8987 *not* name a new type; i.e., given:
8988
8989 struct S { friend struct T; };
8990
8991 `T' is not a new type in the scope of `S'.
8992
8993 Also, `new struct S' or `sizeof (struct S)' never results in the
8994 definition of a new type; a new type can only be declared in a
8995 declaration context. */
8996
8997 type = xref_tag (tag_type, identifier,
8998 /*attributes=*/NULL_TREE,
8999 (is_friend
9000 || !is_declaration
9001 || cp_lexer_next_token_is_not (parser->lexer,
9002 CPP_SEMICOLON)));
9003 }
9004 }
9005 if (tag_type != enum_type)
9006 cp_parser_check_class_key (tag_type, type);
9007 return type;
9008}
9009
9010/* Parse an enum-specifier.
9011
9012 enum-specifier:
9013 enum identifier [opt] { enumerator-list [opt] }
9014
9015 Returns an ENUM_TYPE representing the enumeration. */
9016
9017static tree
9018cp_parser_enum_specifier (parser)
9019 cp_parser *parser;
9020{
9021 cp_token *token;
9022 tree identifier = NULL_TREE;
9023 tree type;
9024
9025 /* Look for the `enum' keyword. */
9026 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9027 return error_mark_node;
9028 /* Peek at the next token. */
9029 token = cp_lexer_peek_token (parser->lexer);
9030
9031 /* See if it is an identifier. */
9032 if (token->type == CPP_NAME)
9033 identifier = cp_parser_identifier (parser);
9034
9035 /* Look for the `{'. */
9036 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9037 return error_mark_node;
9038
9039 /* At this point, we're going ahead with the enum-specifier, even
9040 if some other problem occurs. */
9041 cp_parser_commit_to_tentative_parse (parser);
9042
9043 /* Issue an error message if type-definitions are forbidden here. */
9044 cp_parser_check_type_definition (parser);
9045
9046 /* Create the new type. */
9047 type = start_enum (identifier ? identifier : make_anon_name ());
9048
9049 /* Peek at the next token. */
9050 token = cp_lexer_peek_token (parser->lexer);
9051 /* If it's not a `}', then there are some enumerators. */
9052 if (token->type != CPP_CLOSE_BRACE)
9053 cp_parser_enumerator_list (parser, type);
9054 /* Look for the `}'. */
9055 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9056
9057 /* Finish up the enumeration. */
9058 finish_enum (type);
9059
9060 return type;
9061}
9062
9063/* Parse an enumerator-list. The enumerators all have the indicated
9064 TYPE.
9065
9066 enumerator-list:
9067 enumerator-definition
9068 enumerator-list , enumerator-definition */
9069
9070static void
9071cp_parser_enumerator_list (parser, type)
9072 cp_parser *parser;
9073 tree type;
9074{
9075 while (true)
9076 {
9077 cp_token *token;
9078
9079 /* Parse an enumerator-definition. */
9080 cp_parser_enumerator_definition (parser, type);
9081 /* Peek at the next token. */
9082 token = cp_lexer_peek_token (parser->lexer);
9083 /* If it's not a `,', then we've reached the end of the
9084 list. */
9085 if (token->type != CPP_COMMA)
9086 break;
9087 /* Otherwise, consume the `,' and keep going. */
9088 cp_lexer_consume_token (parser->lexer);
9089 /* If the next token is a `}', there is a trailing comma. */
9090 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9091 {
9092 if (pedantic && !in_system_header)
9093 pedwarn ("comma at end of enumerator list");
9094 break;
9095 }
9096 }
9097}
9098
9099/* Parse an enumerator-definition. The enumerator has the indicated
9100 TYPE.
9101
9102 enumerator-definition:
9103 enumerator
9104 enumerator = constant-expression
9105
9106 enumerator:
9107 identifier */
9108
9109static void
9110cp_parser_enumerator_definition (parser, type)
9111 cp_parser *parser;
9112 tree type;
9113{
9114 cp_token *token;
9115 tree identifier;
9116 tree value;
9117
9118 /* Look for the identifier. */
9119 identifier = cp_parser_identifier (parser);
9120 if (identifier == error_mark_node)
9121 return;
9122
9123 /* Peek at the next token. */
9124 token = cp_lexer_peek_token (parser->lexer);
9125 /* If it's an `=', then there's an explicit value. */
9126 if (token->type == CPP_EQ)
9127 {
9128 /* Consume the `=' token. */
9129 cp_lexer_consume_token (parser->lexer);
9130 /* Parse the value. */
9131 value = cp_parser_constant_expression (parser);
9132 }
9133 else
9134 value = NULL_TREE;
9135
9136 /* Create the enumerator. */
9137 build_enumerator (identifier, value, type);
9138}
9139
9140/* Parse a namespace-name.
9141
9142 namespace-name:
9143 original-namespace-name
9144 namespace-alias
9145
9146 Returns the NAMESPACE_DECL for the namespace. */
9147
9148static tree
9149cp_parser_namespace_name (parser)
9150 cp_parser *parser;
9151{
9152 tree identifier;
9153 tree namespace_decl;
9154
9155 /* Get the name of the namespace. */
9156 identifier = cp_parser_identifier (parser);
9157 if (identifier == error_mark_node)
9158 return error_mark_node;
9159
9160 /* Look up the identifier in the currently active scope. */
9161 namespace_decl = cp_parser_lookup_name_simple (parser, identifier);
9162 /* If it's not a namespace, issue an error. */
9163 if (namespace_decl == error_mark_node
9164 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9165 {
9166 cp_parser_error (parser, "expected namespace-name");
9167 namespace_decl = error_mark_node;
9168 }
9169
9170 return namespace_decl;
9171}
9172
9173/* Parse a namespace-definition.
9174
9175 namespace-definition:
9176 named-namespace-definition
9177 unnamed-namespace-definition
9178
9179 named-namespace-definition:
9180 original-namespace-definition
9181 extension-namespace-definition
9182
9183 original-namespace-definition:
9184 namespace identifier { namespace-body }
9185
9186 extension-namespace-definition:
9187 namespace original-namespace-name { namespace-body }
9188
9189 unnamed-namespace-definition:
9190 namespace { namespace-body } */
9191
9192static void
9193cp_parser_namespace_definition (parser)
9194 cp_parser *parser;
9195{
9196 tree identifier;
9197
9198 /* Look for the `namespace' keyword. */
9199 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9200
9201 /* Get the name of the namespace. We do not attempt to distinguish
9202 between an original-namespace-definition and an
9203 extension-namespace-definition at this point. The semantic
9204 analysis routines are responsible for that. */
9205 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9206 identifier = cp_parser_identifier (parser);
9207 else
9208 identifier = NULL_TREE;
9209
9210 /* Look for the `{' to start the namespace. */
9211 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9212 /* Start the namespace. */
9213 push_namespace (identifier);
9214 /* Parse the body of the namespace. */
9215 cp_parser_namespace_body (parser);
9216 /* Finish the namespace. */
9217 pop_namespace ();
9218 /* Look for the final `}'. */
9219 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9220}
9221
9222/* Parse a namespace-body.
9223
9224 namespace-body:
9225 declaration-seq [opt] */
9226
9227static void
9228cp_parser_namespace_body (parser)
9229 cp_parser *parser;
9230{
9231 cp_parser_declaration_seq_opt (parser);
9232}
9233
9234/* Parse a namespace-alias-definition.
9235
9236 namespace-alias-definition:
9237 namespace identifier = qualified-namespace-specifier ; */
9238
9239static void
9240cp_parser_namespace_alias_definition (parser)
9241 cp_parser *parser;
9242{
9243 tree identifier;
9244 tree namespace_specifier;
9245
9246 /* Look for the `namespace' keyword. */
9247 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9248 /* Look for the identifier. */
9249 identifier = cp_parser_identifier (parser);
9250 if (identifier == error_mark_node)
9251 return;
9252 /* Look for the `=' token. */
9253 cp_parser_require (parser, CPP_EQ, "`='");
9254 /* Look for the qualified-namespace-specifier. */
9255 namespace_specifier
9256 = cp_parser_qualified_namespace_specifier (parser);
9257 /* Look for the `;' token. */
9258 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9259
9260 /* Register the alias in the symbol table. */
9261 do_namespace_alias (identifier, namespace_specifier);
9262}
9263
9264/* Parse a qualified-namespace-specifier.
9265
9266 qualified-namespace-specifier:
9267 :: [opt] nested-name-specifier [opt] namespace-name
9268
9269 Returns a NAMESPACE_DECL corresponding to the specified
9270 namespace. */
9271
9272static tree
9273cp_parser_qualified_namespace_specifier (parser)
9274 cp_parser *parser;
9275{
9276 /* Look for the optional `::'. */
9277 cp_parser_global_scope_opt (parser,
9278 /*current_scope_valid_p=*/false);
9279
9280 /* Look for the optional nested-name-specifier. */
9281 cp_parser_nested_name_specifier_opt (parser,
9282 /*typename_keyword_p=*/false,
9283 /*check_dependency_p=*/true,
9284 /*type_p=*/false);
9285
9286 return cp_parser_namespace_name (parser);
9287}
9288
9289/* Parse a using-declaration.
9290
9291 using-declaration:
9292 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9293 using :: unqualified-id ; */
9294
9295static void
9296cp_parser_using_declaration (parser)
9297 cp_parser *parser;
9298{
9299 cp_token *token;
9300 bool typename_p = false;
9301 bool global_scope_p;
9302 tree decl;
9303 tree identifier;
9304 tree scope;
9305
9306 /* Look for the `using' keyword. */
9307 cp_parser_require_keyword (parser, RID_USING, "`using'");
9308
9309 /* Peek at the next token. */
9310 token = cp_lexer_peek_token (parser->lexer);
9311 /* See if it's `typename'. */
9312 if (token->keyword == RID_TYPENAME)
9313 {
9314 /* Remember that we've seen it. */
9315 typename_p = true;
9316 /* Consume the `typename' token. */
9317 cp_lexer_consume_token (parser->lexer);
9318 }
9319
9320 /* Look for the optional global scope qualification. */
9321 global_scope_p
9322 = (cp_parser_global_scope_opt (parser,
9323 /*current_scope_valid_p=*/false)
9324 != NULL_TREE);
9325
9326 /* If we saw `typename', or didn't see `::', then there must be a
9327 nested-name-specifier present. */
9328 if (typename_p || !global_scope_p)
9329 cp_parser_nested_name_specifier (parser, typename_p,
9330 /*check_dependency_p=*/true,
9331 /*type_p=*/false);
9332 /* Otherwise, we could be in either of the two productions. In that
9333 case, treat the nested-name-specifier as optional. */
9334 else
9335 cp_parser_nested_name_specifier_opt (parser,
9336 /*typename_keyword_p=*/false,
9337 /*check_dependency_p=*/true,
9338 /*type_p=*/false);
9339
9340 /* Parse the unqualified-id. */
9341 identifier = cp_parser_unqualified_id (parser,
9342 /*template_keyword_p=*/false,
9343 /*check_dependency_p=*/true);
9344
9345 /* The function we call to handle a using-declaration is different
9346 depending on what scope we are in. */
9347 scope = current_scope ();
9348 if (scope && TYPE_P (scope))
9349 {
9350 /* Create the USING_DECL. */
9351 decl = do_class_using_decl (build_nt (SCOPE_REF,
9352 parser->scope,
9353 identifier));
9354 /* Add it to the list of members in this class. */
9355 finish_member_declaration (decl);
9356 }
9357 else
9358 {
9359 decl = cp_parser_lookup_name_simple (parser, identifier);
9360 if (scope)
9361 do_local_using_decl (decl);
9362 else
9363 do_toplevel_using_decl (decl);
9364 }
9365
9366 /* Look for the final `;'. */
9367 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9368}
9369
9370/* Parse a using-directive.
9371
9372 using-directive:
9373 using namespace :: [opt] nested-name-specifier [opt]
9374 namespace-name ; */
9375
9376static void
9377cp_parser_using_directive (parser)
9378 cp_parser *parser;
9379{
9380 tree namespace_decl;
9381
9382 /* Look for the `using' keyword. */
9383 cp_parser_require_keyword (parser, RID_USING, "`using'");
9384 /* And the `namespace' keyword. */
9385 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9386 /* Look for the optional `::' operator. */
9387 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9388 /* And the optional nested-name-sepcifier. */
9389 cp_parser_nested_name_specifier_opt (parser,
9390 /*typename_keyword_p=*/false,
9391 /*check_dependency_p=*/true,
9392 /*type_p=*/false);
9393 /* Get the namespace being used. */
9394 namespace_decl = cp_parser_namespace_name (parser);
9395 /* Update the symbol table. */
9396 do_using_directive (namespace_decl);
9397 /* Look for the final `;'. */
9398 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9399}
9400
9401/* Parse an asm-definition.
9402
9403 asm-definition:
9404 asm ( string-literal ) ;
9405
9406 GNU Extension:
9407
9408 asm-definition:
9409 asm volatile [opt] ( string-literal ) ;
9410 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9411 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9412 : asm-operand-list [opt] ) ;
9413 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9414 : asm-operand-list [opt]
9415 : asm-operand-list [opt] ) ; */
9416
9417static void
9418cp_parser_asm_definition (parser)
9419 cp_parser *parser;
9420{
9421 cp_token *token;
9422 tree string;
9423 tree outputs = NULL_TREE;
9424 tree inputs = NULL_TREE;
9425 tree clobbers = NULL_TREE;
9426 tree asm_stmt;
9427 bool volatile_p = false;
9428 bool extended_p = false;
9429
9430 /* Look for the `asm' keyword. */
9431 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9432 /* See if the next token is `volatile'. */
9433 if (cp_parser_allow_gnu_extensions_p (parser)
9434 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9435 {
9436 /* Remember that we saw the `volatile' keyword. */
9437 volatile_p = true;
9438 /* Consume the token. */
9439 cp_lexer_consume_token (parser->lexer);
9440 }
9441 /* Look for the opening `('. */
9442 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9443 /* Look for the string. */
9444 token = cp_parser_require (parser, CPP_STRING, "asm body");
9445 if (!token)
9446 return;
9447 string = token->value;
9448 /* If we're allowing GNU extensions, check for the extended assembly
9449 syntax. Unfortunately, the `:' tokens need not be separated by
9450 a space in C, and so, for compatibility, we tolerate that here
9451 too. Doing that means that we have to treat the `::' operator as
9452 two `:' tokens. */
9453 if (cp_parser_allow_gnu_extensions_p (parser)
9454 && at_function_scope_p ()
9455 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9456 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9457 {
9458 bool inputs_p = false;
9459 bool clobbers_p = false;
9460
9461 /* The extended syntax was used. */
9462 extended_p = true;
9463
9464 /* Look for outputs. */
9465 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9466 {
9467 /* Consume the `:'. */
9468 cp_lexer_consume_token (parser->lexer);
9469 /* Parse the output-operands. */
9470 if (cp_lexer_next_token_is_not (parser->lexer,
9471 CPP_COLON)
9472 && cp_lexer_next_token_is_not (parser->lexer,
9473 CPP_SCOPE))
9474 outputs = cp_parser_asm_operand_list (parser);
9475 }
9476 /* If the next token is `::', there are no outputs, and the
9477 next token is the beginning of the inputs. */
9478 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9479 {
9480 /* Consume the `::' token. */
9481 cp_lexer_consume_token (parser->lexer);
9482 /* The inputs are coming next. */
9483 inputs_p = true;
9484 }
9485
9486 /* Look for inputs. */
9487 if (inputs_p
9488 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9489 {
9490 if (!inputs_p)
9491 /* Consume the `:'. */
9492 cp_lexer_consume_token (parser->lexer);
9493 /* Parse the output-operands. */
9494 if (cp_lexer_next_token_is_not (parser->lexer,
9495 CPP_COLON)
9496 && cp_lexer_next_token_is_not (parser->lexer,
9497 CPP_SCOPE))
9498 inputs = cp_parser_asm_operand_list (parser);
9499 }
9500 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9501 /* The clobbers are coming next. */
9502 clobbers_p = true;
9503
9504 /* Look for clobbers. */
9505 if (clobbers_p
9506 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9507 {
9508 if (!clobbers_p)
9509 /* Consume the `:'. */
9510 cp_lexer_consume_token (parser->lexer);
9511 /* Parse the clobbers. */
9512 clobbers = cp_parser_asm_clobber_list (parser);
9513 }
9514 }
9515 /* Look for the closing `)'. */
9516 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9517 cp_parser_skip_to_closing_parenthesis (parser);
9518 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9519
9520 /* Create the ASM_STMT. */
9521 if (at_function_scope_p ())
9522 {
9523 asm_stmt =
9524 finish_asm_stmt (volatile_p
9525 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9526 string, outputs, inputs, clobbers);
9527 /* If the extended syntax was not used, mark the ASM_STMT. */
9528 if (!extended_p)
9529 ASM_INPUT_P (asm_stmt) = 1;
9530 }
9531 else
9532 assemble_asm (string);
9533}
9534
9535/* Declarators [gram.dcl.decl] */
9536
9537/* Parse an init-declarator.
9538
9539 init-declarator:
9540 declarator initializer [opt]
9541
9542 GNU Extension:
9543
9544 init-declarator:
9545 declarator asm-specification [opt] attributes [opt] initializer [opt]
9546
9547 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9548 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9549 represent deferred access checks from the decl-specifier-seq. If
9550 MEMBER_P is TRUE, then this declarator appears in a class scope.
9551 The new DECL created by this declarator is returned.
9552
9553 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9554 for a function-definition here as well. If the declarator is a
9555 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9556 be TRUE upon return. By that point, the function-definition will
9557 have been completely parsed.
9558
9559 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9560 is FALSE. */
9561
9562static tree
9563cp_parser_init_declarator (parser,
9564 decl_specifiers,
9565 prefix_attributes,
9566 access_checks,
9567 function_definition_allowed_p,
9568 member_p,
9569 function_definition_p)
9570 cp_parser *parser;
9571 tree decl_specifiers;
9572 tree prefix_attributes;
9573 tree access_checks;
9574 bool function_definition_allowed_p;
9575 bool member_p;
9576 bool *function_definition_p;
9577{
9578 cp_token *token;
9579 tree declarator;
9580 tree attributes;
9581 tree asm_specification;
9582 tree initializer;
9583 tree decl = NULL_TREE;
9584 tree scope;
9585 tree declarator_access_checks;
9586 bool is_initialized;
9587 bool is_parenthesized_init;
9588 bool ctor_dtor_or_conv_p;
9589 bool friend_p;
9590
9591 /* Assume that this is not the declarator for a function
9592 definition. */
9593 if (function_definition_p)
9594 *function_definition_p = false;
9595
9596 /* Defer access checks while parsing the declarator; we cannot know
9597 what names are accessible until we know what is being
9598 declared. */
9599 cp_parser_start_deferring_access_checks (parser);
9600 /* Parse the declarator. */
9601 declarator
9602 = cp_parser_declarator (parser,
9603 /*abstract_p=*/false,
9604 &ctor_dtor_or_conv_p);
9605 /* Gather up the deferred checks. */
9606 declarator_access_checks
9607 = cp_parser_stop_deferring_access_checks (parser);
9608
9609 /* If the DECLARATOR was erroneous, there's no need to go
9610 further. */
9611 if (declarator == error_mark_node)
9612 return error_mark_node;
9613
9614 /* Figure out what scope the entity declared by the DECLARATOR is
9615 located in. `grokdeclarator' sometimes changes the scope, so
9616 we compute it now. */
9617 scope = get_scope_of_declarator (declarator);
9618
9619 /* If we're allowing GNU extensions, look for an asm-specification
9620 and attributes. */
9621 if (cp_parser_allow_gnu_extensions_p (parser))
9622 {
9623 /* Look for an asm-specification. */
9624 asm_specification = cp_parser_asm_specification_opt (parser);
9625 /* And attributes. */
9626 attributes = cp_parser_attributes_opt (parser);
9627 }
9628 else
9629 {
9630 asm_specification = NULL_TREE;
9631 attributes = NULL_TREE;
9632 }
9633
9634 /* Peek at the next token. */
9635 token = cp_lexer_peek_token (parser->lexer);
9636 /* Check to see if the token indicates the start of a
9637 function-definition. */
9638 if (cp_parser_token_starts_function_definition_p (token))
9639 {
9640 if (!function_definition_allowed_p)
9641 {
9642 /* If a function-definition should not appear here, issue an
9643 error message. */
9644 cp_parser_error (parser,
9645 "a function-definition is not allowed here");
9646 return error_mark_node;
9647 }
9648 else
9649 {
9650 tree *ac;
9651
9652 /* Neither attributes nor an asm-specification are allowed
9653 on a function-definition. */
9654 if (asm_specification)
9655 error ("an asm-specification is not allowed on a function-definition");
9656 if (attributes)
9657 error ("attributes are not allowed on a function-definition");
9658 /* This is a function-definition. */
9659 *function_definition_p = true;
9660
9661 /* Thread the access checks together. */
9662 ac = &access_checks;
9663 while (*ac)
9664 ac = &TREE_CHAIN (*ac);
9665 *ac = declarator_access_checks;
9666
9667 /* Parse the function definition. */
9668 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9669 (parser, decl_specifiers, prefix_attributes, declarator,
9670 access_checks));
9671
9672 /* Pull the access-checks apart again. */
9673 *ac = NULL_TREE;
9674
9675 return decl;
9676 }
9677 }
9678
9679 /* [dcl.dcl]
9680
9681 Only in function declarations for constructors, destructors, and
9682 type conversions can the decl-specifier-seq be omitted.
9683
9684 We explicitly postpone this check past the point where we handle
9685 function-definitions because we tolerate function-definitions
9686 that are missing their return types in some modes. */
9687 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9688 {
9689 cp_parser_error (parser,
9690 "expected constructor, destructor, or type conversion");
9691 return error_mark_node;
9692 }
9693
9694 /* An `=' or an `(' indicates an initializer. */
9695 is_initialized = (token->type == CPP_EQ
9696 || token->type == CPP_OPEN_PAREN);
9697 /* If the init-declarator isn't initialized and isn't followed by a
9698 `,' or `;', it's not a valid init-declarator. */
9699 if (!is_initialized
9700 && token->type != CPP_COMMA
9701 && token->type != CPP_SEMICOLON)
9702 {
9703 cp_parser_error (parser, "expected init-declarator");
9704 return error_mark_node;
9705 }
9706
9707 /* Because start_decl has side-effects, we should only call it if we
9708 know we're going ahead. By this point, we know that we cannot
9709 possibly be looking at any other construct. */
9710 cp_parser_commit_to_tentative_parse (parser);
9711
9712 /* Check to see whether or not this declaration is a friend. */
9713 friend_p = cp_parser_friend_p (decl_specifiers);
9714
9715 /* Check that the number of template-parameter-lists is OK. */
9716 if (!cp_parser_check_declarator_template_parameters (parser,
9717 declarator))
9718 return error_mark_node;
9719
9720 /* Enter the newly declared entry in the symbol table. If we're
9721 processing a declaration in a class-specifier, we wait until
9722 after processing the initializer. */
9723 if (!member_p)
9724 {
9725 if (parser->in_unbraced_linkage_specification_p)
9726 {
9727 decl_specifiers = tree_cons (error_mark_node,
9728 get_identifier ("extern"),
9729 decl_specifiers);
9730 have_extern_spec = false;
9731 }
9732 decl = start_decl (declarator,
9733 decl_specifiers,
9734 is_initialized,
9735 attributes,
9736 prefix_attributes);
9737 }
9738
9739 /* Enter the SCOPE. That way unqualified names appearing in the
9740 initializer will be looked up in SCOPE. */
9741 if (scope)
9742 push_scope (scope);
9743
9744 /* Perform deferred access control checks, now that we know in which
9745 SCOPE the declared entity resides. */
9746 if (!member_p && decl)
9747 {
9748 tree saved_current_function_decl = NULL_TREE;
9749
9750 /* If the entity being declared is a function, pretend that we
9751 are in its scope. If it is a `friend', it may have access to
9752 things that would not otherwise be accessible. */
9753 if (TREE_CODE (decl) == FUNCTION_DECL)
9754 {
9755 saved_current_function_decl = current_function_decl;
9756 current_function_decl = decl;
9757 }
9758
9759 /* Perform the access control checks for the decl-specifiers. */
9760 cp_parser_perform_deferred_access_checks (access_checks);
9761 /* And for the declarator. */
9762 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9763
9764 /* Restore the saved value. */
9765 if (TREE_CODE (decl) == FUNCTION_DECL)
9766 current_function_decl = saved_current_function_decl;
9767 }
9768
9769 /* Parse the initializer. */
9770 if (is_initialized)
9771 initializer = cp_parser_initializer (parser,
9772 &is_parenthesized_init);
9773 else
9774 {
9775 initializer = NULL_TREE;
9776 is_parenthesized_init = false;
9777 }
9778
9779 /* The old parser allows attributes to appear after a parenthesized
9780 initializer. Mark Mitchell proposed removing this functionality
9781 on the GCC mailing lists on 2002-08-13. This parser accepts the
9782 attributes -- but ignores them. */
9783 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9784 if (cp_parser_attributes_opt (parser))
9785 warning ("attributes after parenthesized initializer ignored");
9786
9787 /* Leave the SCOPE, now that we have processed the initializer. It
9788 is important to do this before calling cp_finish_decl because it
9789 makes decisions about whether to create DECL_STMTs or not based
9790 on the current scope. */
9791 if (scope)
9792 pop_scope (scope);
9793
9794 /* For an in-class declaration, use `grokfield' to create the
9795 declaration. */
9796 if (member_p)
9797 decl = grokfield (declarator, decl_specifiers,
9798 initializer, /*asmspec=*/NULL_TREE,
9799 /*attributes=*/NULL_TREE);
9800
9801 /* Finish processing the declaration. But, skip friend
9802 declarations. */
9803 if (!friend_p && decl)
9804 cp_finish_decl (decl,
9805 initializer,
9806 asm_specification,
9807 /* If the initializer is in parentheses, then this is
9808 a direct-initialization, which means that an
9809 `explicit' constructor is OK. Otherwise, an
9810 `explicit' constructor cannot be used. */
9811 ((is_parenthesized_init || !is_initialized)
9812 ? 0 : LOOKUP_ONLYCONVERTING));
9813
9814 return decl;
9815}
9816
9817/* Parse a declarator.
9818
9819 declarator:
9820 direct-declarator
9821 ptr-operator declarator
9822
9823 abstract-declarator:
9824 ptr-operator abstract-declarator [opt]
9825 direct-abstract-declarator
9826
9827 GNU Extensions:
9828
9829 declarator:
9830 attributes [opt] direct-declarator
9831 attributes [opt] ptr-operator declarator
9832
9833 abstract-declarator:
9834 attributes [opt] ptr-operator abstract-declarator [opt]
9835 attributes [opt] direct-abstract-declarator
9836
9837 Returns a representation of the declarator. If the declarator has
9838 the form `* declarator', then an INDIRECT_REF is returned, whose
9839 only operand is the sub-declarator. Analagously, `& declarator' is
9840 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9841 used. The first operand is the TYPE for `X'. The second operand
9842 is an INDIRECT_REF whose operand is the sub-declarator.
9843
9844 Otherwise, the reprsentation is as for a direct-declarator.
9845
9846 (It would be better to define a structure type to represent
9847 declarators, rather than abusing `tree' nodes to represent
9848 declarators. That would be much clearer and save some memory.
9849 There is no reason for declarators to be garbage-collected, for
9850 example; they are created during parser and no longer needed after
9851 `grokdeclarator' has been called.)
9852
9853 For a ptr-operator that has the optional cv-qualifier-seq,
9854 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9855 node.
9856
9857 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9858 true if this declarator represents a constructor, destructor, or
9859 type conversion operator. Otherwise, it is set to false.
9860
9861 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9862 a decl-specifier-seq unless it declares a constructor, destructor,
9863 or conversion. It might seem that we could check this condition in
9864 semantic analysis, rather than parsing, but that makes it difficult
9865 to handle something like `f()'. We want to notice that there are
9866 no decl-specifiers, and therefore realize that this is an
9867 expression, not a declaration.) */
9868
9869static tree
9870cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9871 cp_parser *parser;
9872 bool abstract_p;
9873 bool *ctor_dtor_or_conv_p;
9874{
9875 cp_token *token;
9876 tree declarator;
9877 enum tree_code code;
9878 tree cv_qualifier_seq;
9879 tree class_type;
9880 tree attributes = NULL_TREE;
9881
9882 /* Assume this is not a constructor, destructor, or type-conversion
9883 operator. */
9884 if (ctor_dtor_or_conv_p)
9885 *ctor_dtor_or_conv_p = false;
9886
9887 if (cp_parser_allow_gnu_extensions_p (parser))
9888 attributes = cp_parser_attributes_opt (parser);
9889
9890 /* Peek at the next token. */
9891 token = cp_lexer_peek_token (parser->lexer);
9892
9893 /* Check for the ptr-operator production. */
9894 cp_parser_parse_tentatively (parser);
9895 /* Parse the ptr-operator. */
9896 code = cp_parser_ptr_operator (parser,
9897 &class_type,
9898 &cv_qualifier_seq);
9899 /* If that worked, then we have a ptr-operator. */
9900 if (cp_parser_parse_definitely (parser))
9901 {
9902 /* The dependent declarator is optional if we are parsing an
9903 abstract-declarator. */
9904 if (abstract_p)
9905 cp_parser_parse_tentatively (parser);
9906
9907 /* Parse the dependent declarator. */
9908 declarator = cp_parser_declarator (parser, abstract_p,
9909 /*ctor_dtor_or_conv_p=*/NULL);
9910
9911 /* If we are parsing an abstract-declarator, we must handle the
9912 case where the dependent declarator is absent. */
9913 if (abstract_p && !cp_parser_parse_definitely (parser))
9914 declarator = NULL_TREE;
9915
9916 /* Build the representation of the ptr-operator. */
9917 if (code == INDIRECT_REF)
9918 declarator = make_pointer_declarator (cv_qualifier_seq,
9919 declarator);
9920 else
9921 declarator = make_reference_declarator (cv_qualifier_seq,
9922 declarator);
9923 /* Handle the pointer-to-member case. */
9924 if (class_type)
9925 declarator = build_nt (SCOPE_REF, class_type, declarator);
9926 }
9927 /* Everything else is a direct-declarator. */
9928 else
9929 declarator = cp_parser_direct_declarator (parser,
9930 abstract_p,
9931 ctor_dtor_or_conv_p);
9932
9933 if (attributes && declarator != error_mark_node)
9934 declarator = tree_cons (attributes, declarator, NULL_TREE);
9935
9936 return declarator;
9937}
9938
9939/* Parse a direct-declarator or direct-abstract-declarator.
9940
9941 direct-declarator:
9942 declarator-id
9943 direct-declarator ( parameter-declaration-clause )
9944 cv-qualifier-seq [opt]
9945 exception-specification [opt]
9946 direct-declarator [ constant-expression [opt] ]
9947 ( declarator )
9948
9949 direct-abstract-declarator:
9950 direct-abstract-declarator [opt]
9951 ( parameter-declaration-clause )
9952 cv-qualifier-seq [opt]
9953 exception-specification [opt]
9954 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9955 ( abstract-declarator )
9956
9957 Returns a representation of the declarator. ABSTRACT_P is TRUE if
9958 we are parsing a direct-abstract-declarator; FALSE if we are
9959 parsing a direct-declarator. CTOR_DTOR_OR_CONV_P is as for
9960 cp_parser_declarator.
9961
9962 For the declarator-id production, the representation is as for an
9963 id-expression, except that a qualified name is represented as a
9964 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9965 see the documentation of the FUNCTION_DECLARATOR_* macros for
9966 information about how to find the various declarator components.
9967 An array-declarator is represented as an ARRAY_REF. The
9968 direct-declarator is the first operand; the constant-expression
9969 indicating the size of the array is the second operand. */
9970
9971static tree
9972cp_parser_direct_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9973 cp_parser *parser;
9974 bool abstract_p;
9975 bool *ctor_dtor_or_conv_p;
9976{
9977 cp_token *token;
9978 tree declarator;
9979 tree scope = NULL_TREE;
9980 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9981 bool saved_in_declarator_p = parser->in_declarator_p;
9982
9983 /* Peek at the next token. */
9984 token = cp_lexer_peek_token (parser->lexer);
9985 /* Find the initial direct-declarator. It might be a parenthesized
9986 declarator. */
9987 if (token->type == CPP_OPEN_PAREN)
9988 {
9989 /* For an abstract declarator we do not know whether we are
9990 looking at the beginning of a parameter-declaration-clause,
9991 or at a parenthesized abstract declarator. For example, if
9992 we see `(int)', we are looking at a
9993 parameter-declaration-clause, and the
9994 direct-abstract-declarator has been omitted. If, on the
9995 other hand we are looking at `((*))' then we are looking at a
9996 parenthesized abstract-declarator. There is no easy way to
9997 tell which situation we are in. */
9998 if (abstract_p)
9999 cp_parser_parse_tentatively (parser);
10000
10001 /* Consume the `('. */
10002 cp_lexer_consume_token (parser->lexer);
10003 /* Parse the nested declarator. */
10004 declarator
10005 = cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p);
10006 /* Expect a `)'. */
10007 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10008
10009 /* If parsing a parenthesized abstract declarator didn't work,
10010 try a parameter-declaration-clause. */
10011 if (abstract_p && !cp_parser_parse_definitely (parser))
10012 declarator = NULL_TREE;
10013 /* If we were not parsing an abstract declarator, but failed to
10014 find a satisfactory nested declarator, then an error has
10015 occurred. */
10016 else if (!abstract_p && declarator == error_mark_node)
10017 return error_mark_node;
10018 /* Default args cannot appear in an abstract decl. */
10019 parser->default_arg_ok_p = false;
10020 }
10021 /* Otherwise, for a non-abstract declarator, there should be a
10022 declarator-id. */
10023 else if (!abstract_p)
10024 {
10025 declarator = cp_parser_declarator_id (parser);
10026
10027 if (TREE_CODE (declarator) == SCOPE_REF)
10028 {
10029 scope = TREE_OPERAND (declarator, 0);
10030
10031 /* In the declaration of a member of a template class
10032 outside of the class itself, the SCOPE will sometimes be
10033 a TYPENAME_TYPE. For example, given:
10034
10035 template <typename T>
10036 int S<T>::R::i = 3;
10037
10038 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In this
10039 context, we must resolve S<T>::R to an ordinary type,
10040 rather than a typename type.
10041
10042 The reason we normally avoid resolving TYPENAME_TYPEs is
10043 that a specialization of `S' might render `S<T>::R' not a
10044 type. However, if `S' is specialized, then this `i' will
10045 not be used, so there is no harm in resolving the types
10046 here. */
10047 if (TREE_CODE (scope) == TYPENAME_TYPE)
10048 {
10049 /* Resolve the TYPENAME_TYPE. */
10050 scope = cp_parser_resolve_typename_type (parser, scope);
10051 /* If that failed, the declarator is invalid. */
10052 if (scope == error_mark_node)
10053 return error_mark_node;
10054 /* Build a new DECLARATOR. */
10055 declarator = build_nt (SCOPE_REF,
10056 scope,
10057 TREE_OPERAND (declarator, 1));
10058 }
10059 }
10060 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10061 /* Default args can only appear for a function decl. */
10062 parser->default_arg_ok_p = false;
10063
10064 /* Check to see whether the declarator-id names a constructor,
10065 destructor, or conversion. */
10066 if (ctor_dtor_or_conv_p
10067 && ((TREE_CODE (declarator) == SCOPE_REF
10068 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10069 || (TREE_CODE (declarator) != SCOPE_REF
10070 && at_class_scope_p ())))
10071 {
10072 tree unqualified_name;
10073 tree class_type;
10074
10075 /* Get the unqualified part of the name. */
10076 if (TREE_CODE (declarator) == SCOPE_REF)
10077 {
10078 class_type = TREE_OPERAND (declarator, 0);
10079 unqualified_name = TREE_OPERAND (declarator, 1);
10080 }
10081 else
10082 {
10083 class_type = current_class_type;
10084 unqualified_name = declarator;
10085 }
10086
10087 /* See if it names ctor, dtor or conv. */
10088 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10089 || IDENTIFIER_TYPENAME_P (unqualified_name)
10090 || constructor_name_p (unqualified_name, class_type))
10091 {
10092 *ctor_dtor_or_conv_p = true;
10093 /* We would have cleared the default arg flag above, but
10094 they are ok. */
10095 parser->default_arg_ok_p = saved_default_arg_ok_p;
10096 }
10097 }
10098 }
10099 /* But for an abstract declarator, the initial direct-declarator can
10100 be omitted. */
10101 else
10102 {
10103 declarator = NULL_TREE;
10104 parser->default_arg_ok_p = false;
10105 }
10106
10107 scope = get_scope_of_declarator (declarator);
10108 if (scope)
10109 /* Any names that appear after the declarator-id for a member
10110 are looked up in the containing scope. */
10111 push_scope (scope);
10112 else
10113 scope = NULL_TREE;
10114 parser->in_declarator_p = true;
10115
10116 /* Now, parse function-declarators and array-declarators until there
10117 are no more. */
10118 while (true)
10119 {
10120 /* Peek at the next token. */
10121 token = cp_lexer_peek_token (parser->lexer);
10122 /* If it's a `[', we're looking at an array-declarator. */
10123 if (token->type == CPP_OPEN_SQUARE)
10124 {
10125 tree bounds;
10126
10127 /* Consume the `['. */
10128 cp_lexer_consume_token (parser->lexer);
10129 /* Peek at the next token. */
10130 token = cp_lexer_peek_token (parser->lexer);
10131 /* If the next token is `]', then there is no
10132 constant-expression. */
10133 if (token->type != CPP_CLOSE_SQUARE)
10134 bounds = cp_parser_constant_expression (parser);
10135 else
10136 bounds = NULL_TREE;
10137 /* Look for the closing `]'. */
10138 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
10139
10140 declarator = build_nt (ARRAY_REF, declarator, bounds);
10141 }
10142 /* If it's a `(', we're looking at a function-declarator. */
10143 else if (token->type == CPP_OPEN_PAREN)
10144 {
10145 /* A function-declarator. Or maybe not. Consider, for
10146 example:
10147
10148 int i (int);
10149 int i (3);
10150
10151 The first is the declaration of a function while the
10152 second is a the definition of a variable, including its
10153 initializer.
10154
10155 Having seen only the parenthesis, we cannot know which of
10156 these two alternatives should be selected. Even more
10157 complex are examples like:
10158
10159 int i (int (a));
10160 int i (int (3));
10161
10162 The former is a function-declaration; the latter is a
10163 variable initialization.
10164
10165 First, we attempt to parse a parameter-declaration
10166 clause. If this works, then we continue; otherwise, we
10167 replace the tokens consumed in the process and continue. */
10168 tree params;
10169
10170 /* We are now parsing tentatively. */
10171 cp_parser_parse_tentatively (parser);
10172
10173 /* Consume the `('. */
10174 cp_lexer_consume_token (parser->lexer);
10175 /* Parse the parameter-declaration-clause. */
10176 params = cp_parser_parameter_declaration_clause (parser);
10177
10178 /* If all went well, parse the cv-qualifier-seq and the
10179 exception-specification. */
10180 if (cp_parser_parse_definitely (parser))
10181 {
10182 tree cv_qualifiers;
10183 tree exception_specification;
10184
10185 /* Consume the `)'. */
10186 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10187
10188 /* Parse the cv-qualifier-seq. */
10189 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10190 /* And the exception-specification. */
10191 exception_specification
10192 = cp_parser_exception_specification_opt (parser);
10193
10194 /* Create the function-declarator. */
10195 declarator = make_call_declarator (declarator,
10196 params,
10197 cv_qualifiers,
10198 exception_specification);
10199 }
10200 /* Otherwise, we must be done with the declarator. */
10201 else
10202 break;
10203 }
10204 /* Otherwise, we're done with the declarator. */
10205 else
10206 break;
10207 /* Any subsequent parameter lists are to do with return type, so
10208 are not those of the declared function. */
10209 parser->default_arg_ok_p = false;
10210 }
10211
10212 /* For an abstract declarator, we might wind up with nothing at this
10213 point. That's an error; the declarator is not optional. */
10214 if (!declarator)
10215 cp_parser_error (parser, "expected declarator");
10216
10217 /* If we entered a scope, we must exit it now. */
10218 if (scope)
10219 pop_scope (scope);
10220
10221 parser->default_arg_ok_p = saved_default_arg_ok_p;
10222 parser->in_declarator_p = saved_in_declarator_p;
10223
10224 return declarator;
10225}
10226
10227/* Parse a ptr-operator.
10228
10229 ptr-operator:
10230 * cv-qualifier-seq [opt]
10231 &
10232 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10233
10234 GNU Extension:
10235
10236 ptr-operator:
10237 & cv-qualifier-seq [opt]
10238
10239 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10240 used. Returns ADDR_EXPR if a reference was used. In the
10241 case of a pointer-to-member, *TYPE is filled in with the
10242 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10243 with the cv-qualifier-seq, or NULL_TREE, if there are no
10244 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10245
10246static enum tree_code
10247cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10248 cp_parser *parser;
10249 tree *type;
10250 tree *cv_qualifier_seq;
10251{
10252 enum tree_code code = ERROR_MARK;
10253 cp_token *token;
10254
10255 /* Assume that it's not a pointer-to-member. */
10256 *type = NULL_TREE;
10257 /* And that there are no cv-qualifiers. */
10258 *cv_qualifier_seq = NULL_TREE;
10259
10260 /* Peek at the next token. */
10261 token = cp_lexer_peek_token (parser->lexer);
10262 /* If it's a `*' or `&' we have a pointer or reference. */
10263 if (token->type == CPP_MULT || token->type == CPP_AND)
10264 {
10265 /* Remember which ptr-operator we were processing. */
10266 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10267
10268 /* Consume the `*' or `&'. */
10269 cp_lexer_consume_token (parser->lexer);
10270
10271 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10272 `&', if we are allowing GNU extensions. (The only qualifier
10273 that can legally appear after `&' is `restrict', but that is
10274 enforced during semantic analysis. */
10275 if (code == INDIRECT_REF
10276 || cp_parser_allow_gnu_extensions_p (parser))
10277 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10278 }
10279 else
10280 {
10281 /* Try the pointer-to-member case. */
10282 cp_parser_parse_tentatively (parser);
10283 /* Look for the optional `::' operator. */
10284 cp_parser_global_scope_opt (parser,
10285 /*current_scope_valid_p=*/false);
10286 /* Look for the nested-name specifier. */
10287 cp_parser_nested_name_specifier (parser,
10288 /*typename_keyword_p=*/false,
10289 /*check_dependency_p=*/true,
10290 /*type_p=*/false);
10291 /* If we found it, and the next token is a `*', then we are
10292 indeed looking at a pointer-to-member operator. */
10293 if (!cp_parser_error_occurred (parser)
10294 && cp_parser_require (parser, CPP_MULT, "`*'"))
10295 {
10296 /* The type of which the member is a member is given by the
10297 current SCOPE. */
10298 *type = parser->scope;
10299 /* The next name will not be qualified. */
10300 parser->scope = NULL_TREE;
10301 parser->qualifying_scope = NULL_TREE;
10302 parser->object_scope = NULL_TREE;
10303 /* Indicate that the `*' operator was used. */
10304 code = INDIRECT_REF;
10305 /* Look for the optional cv-qualifier-seq. */
10306 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10307 }
10308 /* If that didn't work we don't have a ptr-operator. */
10309 if (!cp_parser_parse_definitely (parser))
10310 cp_parser_error (parser, "expected ptr-operator");
10311 }
10312
10313 return code;
10314}
10315
10316/* Parse an (optional) cv-qualifier-seq.
10317
10318 cv-qualifier-seq:
10319 cv-qualifier cv-qualifier-seq [opt]
10320
10321 Returns a TREE_LIST. The TREE_VALUE of each node is the
10322 representation of a cv-qualifier. */
10323
10324static tree
10325cp_parser_cv_qualifier_seq_opt (parser)
10326 cp_parser *parser;
10327{
10328 tree cv_qualifiers = NULL_TREE;
10329
10330 while (true)
10331 {
10332 tree cv_qualifier;
10333
10334 /* Look for the next cv-qualifier. */
10335 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10336 /* If we didn't find one, we're done. */
10337 if (!cv_qualifier)
10338 break;
10339
10340 /* Add this cv-qualifier to the list. */
10341 cv_qualifiers
10342 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10343 }
10344
10345 /* We built up the list in reverse order. */
10346 return nreverse (cv_qualifiers);
10347}
10348
10349/* Parse an (optional) cv-qualifier.
10350
10351 cv-qualifier:
10352 const
10353 volatile
10354
10355 GNU Extension:
10356
10357 cv-qualifier:
10358 __restrict__ */
10359
10360static tree
10361cp_parser_cv_qualifier_opt (parser)
10362 cp_parser *parser;
10363{
10364 cp_token *token;
10365 tree cv_qualifier = NULL_TREE;
10366
10367 /* Peek at the next token. */
10368 token = cp_lexer_peek_token (parser->lexer);
10369 /* See if it's a cv-qualifier. */
10370 switch (token->keyword)
10371 {
10372 case RID_CONST:
10373 case RID_VOLATILE:
10374 case RID_RESTRICT:
10375 /* Save the value of the token. */
10376 cv_qualifier = token->value;
10377 /* Consume the token. */
10378 cp_lexer_consume_token (parser->lexer);
10379 break;
10380
10381 default:
10382 break;
10383 }
10384
10385 return cv_qualifier;
10386}
10387
10388/* Parse a declarator-id.
10389
10390 declarator-id:
10391 id-expression
10392 :: [opt] nested-name-specifier [opt] type-name
10393
10394 In the `id-expression' case, the value returned is as for
10395 cp_parser_id_expression if the id-expression was an unqualified-id.
10396 If the id-expression was a qualified-id, then a SCOPE_REF is
10397 returned. The first operand is the scope (either a NAMESPACE_DECL
10398 or TREE_TYPE), but the second is still just a representation of an
10399 unqualified-id. */
10400
10401static tree
10402cp_parser_declarator_id (parser)
10403 cp_parser *parser;
10404{
10405 tree id_expression;
10406
10407 /* The expression must be an id-expression. Assume that qualified
10408 names are the names of types so that:
10409
10410 template <class T>
10411 int S<T>::R::i = 3;
10412
10413 will work; we must treat `S<T>::R' as the name of a type.
10414 Similarly, assume that qualified names are templates, where
10415 required, so that:
10416
10417 template <class T>
10418 int S<T>::R<T>::i = 3;
10419
10420 will work, too. */
10421 id_expression = cp_parser_id_expression (parser,
10422 /*template_keyword_p=*/false,
10423 /*check_dependency_p=*/false,
10424 /*template_p=*/NULL);
10425 /* If the name was qualified, create a SCOPE_REF to represent
10426 that. */
10427 if (parser->scope)
10428 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10429
10430 return id_expression;
10431}
10432
10433/* Parse a type-id.
10434
10435 type-id:
10436 type-specifier-seq abstract-declarator [opt]
10437
10438 Returns the TYPE specified. */
10439
10440static tree
10441cp_parser_type_id (parser)
10442 cp_parser *parser;
10443{
10444 tree type_specifier_seq;
10445 tree abstract_declarator;
10446
10447 /* Parse the type-specifier-seq. */
10448 type_specifier_seq
10449 = cp_parser_type_specifier_seq (parser);
10450 if (type_specifier_seq == error_mark_node)
10451 return error_mark_node;
10452
10453 /* There might or might not be an abstract declarator. */
10454 cp_parser_parse_tentatively (parser);
10455 /* Look for the declarator. */
10456 abstract_declarator
10457 = cp_parser_declarator (parser, /*abstract_p=*/true, NULL);
10458 /* Check to see if there really was a declarator. */
10459 if (!cp_parser_parse_definitely (parser))
10460 abstract_declarator = NULL_TREE;
10461
10462 return groktypename (build_tree_list (type_specifier_seq,
10463 abstract_declarator));
10464}
10465
10466/* Parse a type-specifier-seq.
10467
10468 type-specifier-seq:
10469 type-specifier type-specifier-seq [opt]
10470
10471 GNU extension:
10472
10473 type-specifier-seq:
10474 attributes type-specifier-seq [opt]
10475
10476 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10477 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10478
10479static tree
10480cp_parser_type_specifier_seq (parser)
10481 cp_parser *parser;
10482{
10483 bool seen_type_specifier = false;
10484 tree type_specifier_seq = NULL_TREE;
10485
10486 /* Parse the type-specifiers and attributes. */
10487 while (true)
10488 {
10489 tree type_specifier;
10490
10491 /* Check for attributes first. */
10492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10493 {
10494 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10495 NULL_TREE,
10496 type_specifier_seq);
10497 continue;
10498 }
10499
10500 /* After the first type-specifier, others are optional. */
10501 if (seen_type_specifier)
10502 cp_parser_parse_tentatively (parser);
10503 /* Look for the type-specifier. */
10504 type_specifier = cp_parser_type_specifier (parser,
10505 CP_PARSER_FLAGS_NONE,
10506 /*is_friend=*/false,
10507 /*is_declaration=*/false,
10508 NULL,
10509 NULL);
10510 /* If the first type-specifier could not be found, this is not a
10511 type-specifier-seq at all. */
10512 if (!seen_type_specifier && type_specifier == error_mark_node)
10513 return error_mark_node;
10514 /* If subsequent type-specifiers could not be found, the
10515 type-specifier-seq is complete. */
10516 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10517 break;
10518
10519 /* Add the new type-specifier to the list. */
10520 type_specifier_seq
10521 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10522 seen_type_specifier = true;
10523 }
10524
10525 /* We built up the list in reverse order. */
10526 return nreverse (type_specifier_seq);
10527}
10528
10529/* Parse a parameter-declaration-clause.
10530
10531 parameter-declaration-clause:
10532 parameter-declaration-list [opt] ... [opt]
10533 parameter-declaration-list , ...
10534
10535 Returns a representation for the parameter declarations. Each node
10536 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10537 representation.) If the parameter-declaration-clause ends with an
10538 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10539 list. A return value of NULL_TREE indicates a
10540 parameter-declaration-clause consisting only of an ellipsis. */
10541
10542static tree
10543cp_parser_parameter_declaration_clause (parser)
10544 cp_parser *parser;
10545{
10546 tree parameters;
10547 cp_token *token;
10548 bool ellipsis_p;
10549
10550 /* Peek at the next token. */
10551 token = cp_lexer_peek_token (parser->lexer);
10552 /* Check for trivial parameter-declaration-clauses. */
10553 if (token->type == CPP_ELLIPSIS)
10554 {
10555 /* Consume the `...' token. */
10556 cp_lexer_consume_token (parser->lexer);
10557 return NULL_TREE;
10558 }
10559 else if (token->type == CPP_CLOSE_PAREN)
10560 /* There are no parameters. */
c73aecdf
DE
10561 {
10562#ifndef NO_IMPLICIT_EXTERN_C
10563 if (in_system_header && current_class_type == NULL
10564 && current_lang_name == lang_name_c)
10565 return NULL_TREE;
10566 else
10567#endif
10568 return void_list_node;
10569 }
a723baf1
MM
10570 /* Check for `(void)', too, which is a special case. */
10571 else if (token->keyword == RID_VOID
10572 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10573 == CPP_CLOSE_PAREN))
10574 {
10575 /* Consume the `void' token. */
10576 cp_lexer_consume_token (parser->lexer);
10577 /* There are no parameters. */
10578 return void_list_node;
10579 }
10580
10581 /* Parse the parameter-declaration-list. */
10582 parameters = cp_parser_parameter_declaration_list (parser);
10583 /* If a parse error occurred while parsing the
10584 parameter-declaration-list, then the entire
10585 parameter-declaration-clause is erroneous. */
10586 if (parameters == error_mark_node)
10587 return error_mark_node;
10588
10589 /* Peek at the next token. */
10590 token = cp_lexer_peek_token (parser->lexer);
10591 /* If it's a `,', the clause should terminate with an ellipsis. */
10592 if (token->type == CPP_COMMA)
10593 {
10594 /* Consume the `,'. */
10595 cp_lexer_consume_token (parser->lexer);
10596 /* Expect an ellipsis. */
10597 ellipsis_p
10598 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10599 }
10600 /* It might also be `...' if the optional trailing `,' was
10601 omitted. */
10602 else if (token->type == CPP_ELLIPSIS)
10603 {
10604 /* Consume the `...' token. */
10605 cp_lexer_consume_token (parser->lexer);
10606 /* And remember that we saw it. */
10607 ellipsis_p = true;
10608 }
10609 else
10610 ellipsis_p = false;
10611
10612 /* Finish the parameter list. */
10613 return finish_parmlist (parameters, ellipsis_p);
10614}
10615
10616/* Parse a parameter-declaration-list.
10617
10618 parameter-declaration-list:
10619 parameter-declaration
10620 parameter-declaration-list , parameter-declaration
10621
10622 Returns a representation of the parameter-declaration-list, as for
10623 cp_parser_parameter_declaration_clause. However, the
10624 `void_list_node' is never appended to the list. */
10625
10626static tree
10627cp_parser_parameter_declaration_list (parser)
10628 cp_parser *parser;
10629{
10630 tree parameters = NULL_TREE;
10631
10632 /* Look for more parameters. */
10633 while (true)
10634 {
10635 tree parameter;
10636 /* Parse the parameter. */
10637 parameter
10638 = cp_parser_parameter_declaration (parser,
10639 /*greater_than_is_operator_p=*/true);
10640 /* If a parse error ocurred parsing the parameter declaration,
10641 then the entire parameter-declaration-list is erroneous. */
10642 if (parameter == error_mark_node)
10643 {
10644 parameters = error_mark_node;
10645 break;
10646 }
10647 /* Add the new parameter to the list. */
10648 TREE_CHAIN (parameter) = parameters;
10649 parameters = parameter;
10650
10651 /* Peek at the next token. */
10652 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10653 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10654 /* The parameter-declaration-list is complete. */
10655 break;
10656 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10657 {
10658 cp_token *token;
10659
10660 /* Peek at the next token. */
10661 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10662 /* If it's an ellipsis, then the list is complete. */
10663 if (token->type == CPP_ELLIPSIS)
10664 break;
10665 /* Otherwise, there must be more parameters. Consume the
10666 `,'. */
10667 cp_lexer_consume_token (parser->lexer);
10668 }
10669 else
10670 {
10671 cp_parser_error (parser, "expected `,' or `...'");
10672 break;
10673 }
10674 }
10675
10676 /* We built up the list in reverse order; straighten it out now. */
10677 return nreverse (parameters);
10678}
10679
10680/* Parse a parameter declaration.
10681
10682 parameter-declaration:
10683 decl-specifier-seq declarator
10684 decl-specifier-seq declarator = assignment-expression
10685 decl-specifier-seq abstract-declarator [opt]
10686 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10687
10688 If GREATER_THAN_IS_OPERATOR_P is FALSE, then a non-nested `>' token
10689 encountered during the parsing of the assignment-expression is not
10690 interpreted as a greater-than operator.
10691
10692 Returns a TREE_LIST representing the parameter-declaration. The
10693 TREE_VALUE is a representation of the decl-specifier-seq and
10694 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10695 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10696 TREE_VALUE represents the declarator. */
10697
10698static tree
10699cp_parser_parameter_declaration (parser, greater_than_is_operator_p)
10700 cp_parser *parser;
10701 bool greater_than_is_operator_p;
10702{
10703 bool declares_class_or_enum;
10704 tree decl_specifiers;
10705 tree attributes;
10706 tree declarator;
10707 tree default_argument;
10708 tree parameter;
10709 cp_token *token;
10710 const char *saved_message;
10711
10712 /* Type definitions may not appear in parameter types. */
10713 saved_message = parser->type_definition_forbidden_message;
10714 parser->type_definition_forbidden_message
10715 = "types may not be defined in parameter types";
10716
10717 /* Parse the declaration-specifiers. */
10718 decl_specifiers
10719 = cp_parser_decl_specifier_seq (parser,
10720 CP_PARSER_FLAGS_NONE,
10721 &attributes,
10722 &declares_class_or_enum);
10723 /* If an error occurred, there's no reason to attempt to parse the
10724 rest of the declaration. */
10725 if (cp_parser_error_occurred (parser))
10726 {
10727 parser->type_definition_forbidden_message = saved_message;
10728 return error_mark_node;
10729 }
10730
10731 /* Peek at the next token. */
10732 token = cp_lexer_peek_token (parser->lexer);
10733 /* If the next token is a `)', `,', `=', `>', or `...', then there
10734 is no declarator. */
10735 if (token->type == CPP_CLOSE_PAREN
10736 || token->type == CPP_COMMA
10737 || token->type == CPP_EQ
10738 || token->type == CPP_ELLIPSIS
10739 || token->type == CPP_GREATER)
10740 declarator = NULL_TREE;
10741 /* Otherwise, there should be a declarator. */
10742 else
10743 {
10744 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10745 parser->default_arg_ok_p = false;
10746
10747 /* We don't know whether the declarator will be abstract or
10748 not. So, first we try an ordinary declarator. */
10749 cp_parser_parse_tentatively (parser);
10750 declarator = cp_parser_declarator (parser,
10751 /*abstract_p=*/false,
10752 /*ctor_dtor_or_conv_p=*/NULL);
10753 /* If that didn't work, look for an abstract declarator. */
10754 if (!cp_parser_parse_definitely (parser))
10755 declarator = cp_parser_declarator (parser,
10756 /*abstract_p=*/true,
10757 /*ctor_dtor_or_conv_p=*/NULL);
10758 parser->default_arg_ok_p = saved_default_arg_ok_p;
10759 }
10760
10761 /* The restriction on definining new types applies only to the type
10762 of the parameter, not to the default argument. */
10763 parser->type_definition_forbidden_message = saved_message;
10764
10765 /* If the next token is `=', then process a default argument. */
10766 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10767 {
10768 bool saved_greater_than_is_operator_p;
10769 /* Consume the `='. */
10770 cp_lexer_consume_token (parser->lexer);
10771
10772 /* If we are defining a class, then the tokens that make up the
10773 default argument must be saved and processed later. */
10774 if (at_class_scope_p () && TYPE_BEING_DEFINED (current_class_type))
10775 {
10776 unsigned depth = 0;
10777
10778 /* Create a DEFAULT_ARG to represented the unparsed default
10779 argument. */
10780 default_argument = make_node (DEFAULT_ARG);
10781 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10782
10783 /* Add tokens until we have processed the entire default
10784 argument. */
10785 while (true)
10786 {
10787 bool done = false;
10788 cp_token *token;
10789
10790 /* Peek at the next token. */
10791 token = cp_lexer_peek_token (parser->lexer);
10792 /* What we do depends on what token we have. */
10793 switch (token->type)
10794 {
10795 /* In valid code, a default argument must be
10796 immediately followed by a `,' `)', or `...'. */
10797 case CPP_COMMA:
10798 case CPP_CLOSE_PAREN:
10799 case CPP_ELLIPSIS:
10800 /* If we run into a non-nested `;', `}', or `]',
10801 then the code is invalid -- but the default
10802 argument is certainly over. */
10803 case CPP_SEMICOLON:
10804 case CPP_CLOSE_BRACE:
10805 case CPP_CLOSE_SQUARE:
10806 if (depth == 0)
10807 done = true;
10808 /* Update DEPTH, if necessary. */
10809 else if (token->type == CPP_CLOSE_PAREN
10810 || token->type == CPP_CLOSE_BRACE
10811 || token->type == CPP_CLOSE_SQUARE)
10812 --depth;
10813 break;
10814
10815 case CPP_OPEN_PAREN:
10816 case CPP_OPEN_SQUARE:
10817 case CPP_OPEN_BRACE:
10818 ++depth;
10819 break;
10820
10821 case CPP_GREATER:
10822 /* If we see a non-nested `>', and `>' is not an
10823 operator, then it marks the end of the default
10824 argument. */
10825 if (!depth && !greater_than_is_operator_p)
10826 done = true;
10827 break;
10828
10829 /* If we run out of tokens, issue an error message. */
10830 case CPP_EOF:
10831 error ("file ends in default argument");
10832 done = true;
10833 break;
10834
10835 case CPP_NAME:
10836 case CPP_SCOPE:
10837 /* In these cases, we should look for template-ids.
10838 For example, if the default argument is
10839 `X<int, double>()', we need to do name lookup to
10840 figure out whether or not `X' is a template; if
10841 so, the `,' does not end the deault argument.
10842
10843 That is not yet done. */
10844 break;
10845
10846 default:
10847 break;
10848 }
10849
10850 /* If we've reached the end, stop. */
10851 if (done)
10852 break;
10853
10854 /* Add the token to the token block. */
10855 token = cp_lexer_consume_token (parser->lexer);
10856 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10857 token);
10858 }
10859 }
10860 /* Outside of a class definition, we can just parse the
10861 assignment-expression. */
10862 else
10863 {
10864 bool saved_local_variables_forbidden_p;
10865
10866 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10867 set correctly. */
10868 saved_greater_than_is_operator_p
10869 = parser->greater_than_is_operator_p;
10870 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10871 /* Local variable names (and the `this' keyword) may not
10872 appear in a default argument. */
10873 saved_local_variables_forbidden_p
10874 = parser->local_variables_forbidden_p;
10875 parser->local_variables_forbidden_p = true;
10876 /* Parse the assignment-expression. */
10877 default_argument = cp_parser_assignment_expression (parser);
10878 /* Restore saved state. */
10879 parser->greater_than_is_operator_p
10880 = saved_greater_than_is_operator_p;
10881 parser->local_variables_forbidden_p
10882 = saved_local_variables_forbidden_p;
10883 }
10884 if (!parser->default_arg_ok_p)
10885 {
10886 pedwarn ("default arguments are only permitted on functions");
10887 if (flag_pedantic_errors)
10888 default_argument = NULL_TREE;
10889 }
10890 }
10891 else
10892 default_argument = NULL_TREE;
10893
10894 /* Create the representation of the parameter. */
10895 if (attributes)
10896 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10897 parameter = build_tree_list (default_argument,
10898 build_tree_list (decl_specifiers,
10899 declarator));
10900
10901 return parameter;
10902}
10903
10904/* Parse a function-definition.
10905
10906 function-definition:
10907 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10908 function-body
10909 decl-specifier-seq [opt] declarator function-try-block
10910
10911 GNU Extension:
10912
10913 function-definition:
10914 __extension__ function-definition
10915
10916 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10917 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10918 be a `friend'. */
10919
10920static tree
10921cp_parser_function_definition (parser, friend_p)
10922 cp_parser *parser;
10923 bool *friend_p;
10924{
10925 tree decl_specifiers;
10926 tree attributes;
10927 tree declarator;
10928 tree fn;
10929 tree access_checks;
10930 cp_token *token;
10931 bool declares_class_or_enum;
10932 bool member_p;
10933 /* The saved value of the PEDANTIC flag. */
10934 int saved_pedantic;
10935
10936 /* Any pending qualification must be cleared by our caller. It is
10937 more robust to force the callers to clear PARSER->SCOPE than to
10938 do it here since if the qualification is in effect here, it might
10939 also end up in effect elsewhere that it is not intended. */
10940 my_friendly_assert (!parser->scope, 20010821);
10941
10942 /* Handle `__extension__'. */
10943 if (cp_parser_extension_opt (parser, &saved_pedantic))
10944 {
10945 /* Parse the function-definition. */
10946 fn = cp_parser_function_definition (parser, friend_p);
10947 /* Restore the PEDANTIC flag. */
10948 pedantic = saved_pedantic;
10949
10950 return fn;
10951 }
10952
10953 /* Check to see if this definition appears in a class-specifier. */
10954 member_p = (at_class_scope_p ()
10955 && TYPE_BEING_DEFINED (current_class_type));
10956 /* Defer access checks in the decl-specifier-seq until we know what
10957 function is being defined. There is no need to do this for the
10958 definition of member functions; we cannot be defining a member
10959 from another class. */
10960 if (!member_p)
10961 cp_parser_start_deferring_access_checks (parser);
10962 /* Parse the decl-specifier-seq. */
10963 decl_specifiers
10964 = cp_parser_decl_specifier_seq (parser,
10965 CP_PARSER_FLAGS_OPTIONAL,
10966 &attributes,
10967 &declares_class_or_enum);
10968 /* Figure out whether this declaration is a `friend'. */
10969 if (friend_p)
10970 *friend_p = cp_parser_friend_p (decl_specifiers);
10971
10972 /* Parse the declarator. */
10973 declarator = cp_parser_declarator (parser,
10974 /*abstract_p=*/false,
10975 /*ctor_dtor_or_conv_p=*/NULL);
10976
10977 /* Gather up any access checks that occurred. */
10978 if (!member_p)
10979 access_checks = cp_parser_stop_deferring_access_checks (parser);
10980 else
10981 access_checks = NULL_TREE;
10982
10983 /* If something has already gone wrong, we may as well stop now. */
10984 if (declarator == error_mark_node)
10985 {
10986 /* Skip to the end of the function, or if this wasn't anything
10987 like a function-definition, to a `;' in the hopes of finding
10988 a sensible place from which to continue parsing. */
10989 cp_parser_skip_to_end_of_block_or_statement (parser);
10990 return error_mark_node;
10991 }
10992
10993 /* The next character should be a `{' (for a simple function
10994 definition), a `:' (for a ctor-initializer), or `try' (for a
10995 function-try block). */
10996 token = cp_lexer_peek_token (parser->lexer);
10997 if (!cp_parser_token_starts_function_definition_p (token))
10998 {
10999 /* Issue the error-message. */
11000 cp_parser_error (parser, "expected function-definition");
11001 /* Skip to the next `;'. */
11002 cp_parser_skip_to_end_of_block_or_statement (parser);
11003
11004 return error_mark_node;
11005 }
11006
11007 /* If we are in a class scope, then we must handle
11008 function-definitions specially. In particular, we save away the
11009 tokens that make up the function body, and parse them again
11010 later, in order to handle code like:
11011
11012 struct S {
11013 int f () { return i; }
11014 int i;
11015 };
11016
11017 Here, we cannot parse the body of `f' until after we have seen
11018 the declaration of `i'. */
11019 if (member_p)
11020 {
11021 cp_token_cache *cache;
11022
11023 /* Create the function-declaration. */
11024 fn = start_method (decl_specifiers, declarator, attributes);
11025 /* If something went badly wrong, bail out now. */
11026 if (fn == error_mark_node)
11027 {
11028 /* If there's a function-body, skip it. */
11029 if (cp_parser_token_starts_function_definition_p
11030 (cp_lexer_peek_token (parser->lexer)))
11031 cp_parser_skip_to_end_of_block_or_statement (parser);
11032 return error_mark_node;
11033 }
11034
11035 /* Create a token cache. */
11036 cache = cp_token_cache_new ();
11037 /* Save away the tokens that make up the body of the
11038 function. */
11039 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11040 /* Handle function try blocks. */
11041 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11042 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11043
11044 /* Save away the inline definition; we will process it when the
11045 class is complete. */
11046 DECL_PENDING_INLINE_INFO (fn) = cache;
11047 DECL_PENDING_INLINE_P (fn) = 1;
11048
11049 /* We're done with the inline definition. */
11050 finish_method (fn);
11051
11052 /* Add FN to the queue of functions to be parsed later. */
11053 TREE_VALUE (parser->unparsed_functions_queues)
11054 = tree_cons (current_class_type, fn,
11055 TREE_VALUE (parser->unparsed_functions_queues));
11056
11057 return fn;
11058 }
11059
11060 /* Check that the number of template-parameter-lists is OK. */
11061 if (!cp_parser_check_declarator_template_parameters (parser,
11062 declarator))
11063 {
11064 cp_parser_skip_to_end_of_block_or_statement (parser);
11065 return error_mark_node;
11066 }
11067
11068 return (cp_parser_function_definition_from_specifiers_and_declarator
11069 (parser, decl_specifiers, attributes, declarator, access_checks));
11070}
11071
11072/* Parse a function-body.
11073
11074 function-body:
11075 compound_statement */
11076
11077static void
11078cp_parser_function_body (cp_parser *parser)
11079{
11080 cp_parser_compound_statement (parser);
11081}
11082
11083/* Parse a ctor-initializer-opt followed by a function-body. Return
11084 true if a ctor-initializer was present. */
11085
11086static bool
11087cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11088{
11089 tree body;
11090 bool ctor_initializer_p;
11091
11092 /* Begin the function body. */
11093 body = begin_function_body ();
11094 /* Parse the optional ctor-initializer. */
11095 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11096 /* Parse the function-body. */
11097 cp_parser_function_body (parser);
11098 /* Finish the function body. */
11099 finish_function_body (body);
11100
11101 return ctor_initializer_p;
11102}
11103
11104/* Parse an initializer.
11105
11106 initializer:
11107 = initializer-clause
11108 ( expression-list )
11109
11110 Returns a expression representing the initializer. If no
11111 initializer is present, NULL_TREE is returned.
11112
11113 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11114 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11115 set to FALSE if there is no initializer present. */
11116
11117static tree
11118cp_parser_initializer (parser, is_parenthesized_init)
11119 cp_parser *parser;
11120 bool *is_parenthesized_init;
11121{
11122 cp_token *token;
11123 tree init;
11124
11125 /* Peek at the next token. */
11126 token = cp_lexer_peek_token (parser->lexer);
11127
11128 /* Let our caller know whether or not this initializer was
11129 parenthesized. */
11130 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11131
11132 if (token->type == CPP_EQ)
11133 {
11134 /* Consume the `='. */
11135 cp_lexer_consume_token (parser->lexer);
11136 /* Parse the initializer-clause. */
11137 init = cp_parser_initializer_clause (parser);
11138 }
11139 else if (token->type == CPP_OPEN_PAREN)
11140 {
11141 /* Consume the `('. */
11142 cp_lexer_consume_token (parser->lexer);
11143 /* Parse the expression-list. */
11144 init = cp_parser_expression_list (parser);
11145 /* Consume the `)' token. */
11146 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11147 cp_parser_skip_to_closing_parenthesis (parser);
11148 }
11149 else
11150 {
11151 /* Anything else is an error. */
11152 cp_parser_error (parser, "expected initializer");
11153 init = error_mark_node;
11154 }
11155
11156 return init;
11157}
11158
11159/* Parse an initializer-clause.
11160
11161 initializer-clause:
11162 assignment-expression
11163 { initializer-list , [opt] }
11164 { }
11165
11166 Returns an expression representing the initializer.
11167
11168 If the `assignment-expression' production is used the value
11169 returned is simply a reprsentation for the expression.
11170
11171 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11172 the elements of the initializer-list (or NULL_TREE, if the last
11173 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11174 NULL_TREE. There is no way to detect whether or not the optional
11175 trailing `,' was provided. */
11176
11177static tree
11178cp_parser_initializer_clause (parser)
11179 cp_parser *parser;
11180{
11181 tree initializer;
11182
11183 /* If it is not a `{', then we are looking at an
11184 assignment-expression. */
11185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11186 initializer = cp_parser_assignment_expression (parser);
11187 else
11188 {
11189 /* Consume the `{' token. */
11190 cp_lexer_consume_token (parser->lexer);
11191 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11192 initializer = make_node (CONSTRUCTOR);
11193 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11194 necessary, but check_initializer depends upon it, for
11195 now. */
11196 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11197 /* If it's not a `}', then there is a non-trivial initializer. */
11198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11199 {
11200 /* Parse the initializer list. */
11201 CONSTRUCTOR_ELTS (initializer)
11202 = cp_parser_initializer_list (parser);
11203 /* A trailing `,' token is allowed. */
11204 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11205 cp_lexer_consume_token (parser->lexer);
11206 }
11207
11208 /* Now, there should be a trailing `}'. */
11209 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11210 }
11211
11212 return initializer;
11213}
11214
11215/* Parse an initializer-list.
11216
11217 initializer-list:
11218 initializer-clause
11219 initializer-list , initializer-clause
11220
11221 GNU Extension:
11222
11223 initializer-list:
11224 identifier : initializer-clause
11225 initializer-list, identifier : initializer-clause
11226
11227 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11228 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11229 IDENTIFIER_NODE naming the field to initialize. */
11230
11231static tree
11232cp_parser_initializer_list (parser)
11233 cp_parser *parser;
11234{
11235 tree initializers = NULL_TREE;
11236
11237 /* Parse the rest of the list. */
11238 while (true)
11239 {
11240 cp_token *token;
11241 tree identifier;
11242 tree initializer;
11243
11244 /* If the next token is an identifier and the following one is a
11245 colon, we are looking at the GNU designated-initializer
11246 syntax. */
11247 if (cp_parser_allow_gnu_extensions_p (parser)
11248 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11249 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11250 {
11251 /* Consume the identifier. */
11252 identifier = cp_lexer_consume_token (parser->lexer)->value;
11253 /* Consume the `:'. */
11254 cp_lexer_consume_token (parser->lexer);
11255 }
11256 else
11257 identifier = NULL_TREE;
11258
11259 /* Parse the initializer. */
11260 initializer = cp_parser_initializer_clause (parser);
11261
11262 /* Add it to the list. */
11263 initializers = tree_cons (identifier, initializer, initializers);
11264
11265 /* If the next token is not a comma, we have reached the end of
11266 the list. */
11267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11268 break;
11269
11270 /* Peek at the next token. */
11271 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11272 /* If the next token is a `}', then we're still done. An
11273 initializer-clause can have a trailing `,' after the
11274 initializer-list and before the closing `}'. */
11275 if (token->type == CPP_CLOSE_BRACE)
11276 break;
11277
11278 /* Consume the `,' token. */
11279 cp_lexer_consume_token (parser->lexer);
11280 }
11281
11282 /* The initializers were built up in reverse order, so we need to
11283 reverse them now. */
11284 return nreverse (initializers);
11285}
11286
11287/* Classes [gram.class] */
11288
11289/* Parse a class-name.
11290
11291 class-name:
11292 identifier
11293 template-id
11294
11295 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11296 to indicate that names looked up in dependent types should be
11297 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11298 keyword has been used to indicate that the name that appears next
11299 is a template. TYPE_P is true iff the next name should be treated
11300 as class-name, even if it is declared to be some other kind of name
11301 as well. The accessibility of the class-name is checked iff
11302 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11303 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11304 is the class being defined in a class-head.
11305
11306 Returns the TYPE_DECL representing the class. */
11307
11308static tree
11309cp_parser_class_name (cp_parser *parser,
11310 bool typename_keyword_p,
11311 bool template_keyword_p,
11312 bool type_p,
11313 bool check_access_p,
11314 bool check_dependency_p,
11315 bool class_head_p)
11316{
11317 tree decl;
11318 tree scope;
11319 bool typename_p;
11320
11321 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11322 to a template-id, so we save it here. */
11323 scope = parser->scope;
11324 /* Any name names a type if we're following the `typename' keyword
11325 in a qualified name where the enclosing scope is type-dependent. */
11326 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11327 && cp_parser_dependent_type_p (scope));
11328
11329 /* We don't know whether what comes next is a template-id or
11330 not. */
11331 cp_parser_parse_tentatively (parser);
11332 /* Try a template-id. */
11333 decl = cp_parser_template_id (parser, template_keyword_p,
11334 check_dependency_p);
11335 if (cp_parser_parse_definitely (parser))
11336 {
11337 if (decl == error_mark_node)
11338 return error_mark_node;
11339 }
11340 else
11341 {
11342 /* If it wasn't a template-id, try a simple identifier. */
11343 tree identifier;
11344
11345 /* Look for the identifier. */
11346 identifier = cp_parser_identifier (parser);
11347 /* If the next token isn't an identifier, we are certainly not
11348 looking at a class-name. */
11349 if (identifier == error_mark_node)
11350 decl = error_mark_node;
11351 /* If we know this is a type-name, there's no need to look it
11352 up. */
11353 else if (typename_p)
11354 decl = identifier;
11355 else
11356 {
11357 /* If the next token is a `::', then the name must be a type
11358 name.
11359
11360 [basic.lookup.qual]
11361
11362 During the lookup for a name preceding the :: scope
11363 resolution operator, object, function, and enumerator
11364 names are ignored. */
11365 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11366 type_p = true;
11367 /* Look up the name. */
11368 decl = cp_parser_lookup_name (parser, identifier,
11369 check_access_p,
11370 type_p,
11371 check_dependency_p);
11372 }
11373 }
11374
11375 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11376
11377 /* If this is a typename, create a TYPENAME_TYPE. */
11378 if (typename_p && decl != error_mark_node)
11379 decl = TYPE_NAME (make_typename_type (scope, decl,
11380 /*complain=*/1));
11381
11382 /* Check to see that it is really the name of a class. */
11383 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11384 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11385 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11386 /* Situations like this:
11387
11388 template <typename T> struct A {
11389 typename T::template X<int>::I i;
11390 };
11391
11392 are problematic. Is `T::template X<int>' a class-name? The
11393 standard does not seem to be definitive, but there is no other
11394 valid interpretation of the following `::'. Therefore, those
11395 names are considered class-names. */
11396 decl = TYPE_NAME (make_typename_type (scope, decl,
11397 tf_error | tf_parsing));
11398 else if (decl == error_mark_node
11399 || TREE_CODE (decl) != TYPE_DECL
11400 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11401 {
11402 cp_parser_error (parser, "expected class-name");
11403 return error_mark_node;
11404 }
11405
11406 return decl;
11407}
11408
11409/* Parse a class-specifier.
11410
11411 class-specifier:
11412 class-head { member-specification [opt] }
11413
11414 Returns the TREE_TYPE representing the class. */
11415
11416static tree
11417cp_parser_class_specifier (parser)
11418 cp_parser *parser;
11419{
11420 cp_token *token;
11421 tree type;
11422 tree attributes = NULL_TREE;
11423 int has_trailing_semicolon;
11424 bool nested_name_specifier_p;
11425 bool deferring_access_checks_p;
11426 tree saved_access_checks;
11427 unsigned saved_num_template_parameter_lists;
11428
11429 /* Parse the class-head. */
11430 type = cp_parser_class_head (parser,
11431 &nested_name_specifier_p,
11432 &deferring_access_checks_p,
11433 &saved_access_checks);
11434 /* If the class-head was a semantic disaster, skip the entire body
11435 of the class. */
11436 if (!type)
11437 {
11438 cp_parser_skip_to_end_of_block_or_statement (parser);
11439 return error_mark_node;
11440 }
11441 /* Look for the `{'. */
11442 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11443 return error_mark_node;
11444 /* Issue an error message if type-definitions are forbidden here. */
11445 cp_parser_check_type_definition (parser);
11446 /* Remember that we are defining one more class. */
11447 ++parser->num_classes_being_defined;
11448 /* Inside the class, surrounding template-parameter-lists do not
11449 apply. */
11450 saved_num_template_parameter_lists
11451 = parser->num_template_parameter_lists;
11452 parser->num_template_parameter_lists = 0;
11453 /* Start the class. */
11454 type = begin_class_definition (type);
11455 if (type == error_mark_node)
11456 /* If the type is erroneous, skip the entire body of the class. */
11457 cp_parser_skip_to_closing_brace (parser);
11458 else
11459 /* Parse the member-specification. */
11460 cp_parser_member_specification_opt (parser);
11461 /* Look for the trailing `}'. */
11462 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11463 /* We get better error messages by noticing a common problem: a
11464 missing trailing `;'. */
11465 token = cp_lexer_peek_token (parser->lexer);
11466 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11467 /* Look for attributes to apply to this class. */
11468 if (cp_parser_allow_gnu_extensions_p (parser))
11469 attributes = cp_parser_attributes_opt (parser);
11470 /* Finish the class definition. */
11471 type = finish_class_definition (type,
11472 attributes,
11473 has_trailing_semicolon,
11474 nested_name_specifier_p);
11475 /* If this class is not itself within the scope of another class,
11476 then we need to parse the bodies of all of the queued function
11477 definitions. Note that the queued functions defined in a class
11478 are not always processed immediately following the
11479 class-specifier for that class. Consider:
11480
11481 struct A {
11482 struct B { void f() { sizeof (A); } };
11483 };
11484
11485 If `f' were processed before the processing of `A' were
11486 completed, there would be no way to compute the size of `A'.
11487 Note that the nesting we are interested in here is lexical --
11488 not the semantic nesting given by TYPE_CONTEXT. In particular,
11489 for:
11490
11491 struct A { struct B; };
11492 struct A::B { void f() { } };
11493
11494 there is no need to delay the parsing of `A::B::f'. */
11495 if (--parser->num_classes_being_defined == 0)
11496 {
11497 tree last_scope = NULL_TREE;
11498
11499 /* Process non FUNCTION_DECL related DEFAULT_ARGs. */
11500 for (parser->default_arg_types = nreverse (parser->default_arg_types);
11501 parser->default_arg_types;
11502 parser->default_arg_types = TREE_CHAIN (parser->default_arg_types))
11503 cp_parser_late_parsing_default_args
11504 (parser, TREE_PURPOSE (parser->default_arg_types));
11505
11506 /* Reverse the queue, so that we process it in the order the
11507 functions were declared. */
11508 TREE_VALUE (parser->unparsed_functions_queues)
11509 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11510 /* Loop through all of the functions. */
11511 while (TREE_VALUE (parser->unparsed_functions_queues))
11512
11513 {
11514 tree fn;
11515 tree fn_scope;
11516 tree queue_entry;
11517
11518 /* Figure out which function we need to process. */
11519 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11520 fn_scope = TREE_PURPOSE (queue_entry);
11521 fn = TREE_VALUE (queue_entry);
11522
11523 /* Parse the function. */
11524 cp_parser_late_parsing_for_member (parser, fn);
11525
11526 TREE_VALUE (parser->unparsed_functions_queues)
11527 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11528 }
11529
11530 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11531 more time than we have popped, so me must pop here. */
11532 if (last_scope)
11533 pop_scope (last_scope);
11534 }
11535
11536 /* Put back any saved access checks. */
11537 if (deferring_access_checks_p)
11538 {
11539 cp_parser_start_deferring_access_checks (parser);
11540 parser->context->deferred_access_checks = saved_access_checks;
11541 }
11542
11543 /* Restore the count of active template-parameter-lists. */
11544 parser->num_template_parameter_lists
11545 = saved_num_template_parameter_lists;
11546
11547 return type;
11548}
11549
11550/* Parse a class-head.
11551
11552 class-head:
11553 class-key identifier [opt] base-clause [opt]
11554 class-key nested-name-specifier identifier base-clause [opt]
11555 class-key nested-name-specifier [opt] template-id
11556 base-clause [opt]
11557
11558 GNU Extensions:
11559 class-key attributes identifier [opt] base-clause [opt]
11560 class-key attributes nested-name-specifier identifier base-clause [opt]
11561 class-key attributes nested-name-specifier [opt] template-id
11562 base-clause [opt]
11563
11564 Returns the TYPE of the indicated class. Sets
11565 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11566 involving a nested-name-specifier was used, and FALSE otherwise.
11567 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11568 access checks before this class-head. In that case,
11569 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11570 checks.
11571
11572 Returns NULL_TREE if the class-head is syntactically valid, but
11573 semantically invalid in a way that means we should skip the entire
11574 body of the class. */
11575
11576static tree
11577cp_parser_class_head (parser,
11578 nested_name_specifier_p,
11579 deferring_access_checks_p,
11580 saved_access_checks)
11581 cp_parser *parser;
11582 bool *nested_name_specifier_p;
11583 bool *deferring_access_checks_p;
11584 tree *saved_access_checks;
11585{
11586 cp_token *token;
11587 tree nested_name_specifier;
11588 enum tag_types class_key;
11589 tree id = NULL_TREE;
11590 tree type = NULL_TREE;
11591 tree attributes;
11592 bool template_id_p = false;
11593 bool qualified_p = false;
11594 bool invalid_nested_name_p = false;
11595 unsigned num_templates;
11596
11597 /* Assume no nested-name-specifier will be present. */
11598 *nested_name_specifier_p = false;
11599 /* Assume no template parameter lists will be used in defining the
11600 type. */
11601 num_templates = 0;
11602
11603 /* Look for the class-key. */
11604 class_key = cp_parser_class_key (parser);
11605 if (class_key == none_type)
11606 return error_mark_node;
11607
11608 /* Parse the attributes. */
11609 attributes = cp_parser_attributes_opt (parser);
11610
11611 /* If the next token is `::', that is invalid -- but sometimes
11612 people do try to write:
11613
11614 struct ::S {};
11615
11616 Handle this gracefully by accepting the extra qualifier, and then
11617 issuing an error about it later if this really is a
11618 class-header. If it turns out just to be an elaborated type
11619 specifier, remain silent. */
11620 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11621 qualified_p = true;
11622
11623 /* Determine the name of the class. Begin by looking for an
11624 optional nested-name-specifier. */
11625 nested_name_specifier
11626 = cp_parser_nested_name_specifier_opt (parser,
11627 /*typename_keyword_p=*/false,
11628 /*check_dependency_p=*/true,
11629 /*type_p=*/false);
11630 /* If there was a nested-name-specifier, then there *must* be an
11631 identifier. */
11632 if (nested_name_specifier)
11633 {
11634 /* Although the grammar says `identifier', it really means
11635 `class-name' or `template-name'. You are only allowed to
11636 define a class that has already been declared with this
11637 syntax.
11638
11639 The proposed resolution for Core Issue 180 says that whever
11640 you see `class T::X' you should treat `X' as a type-name.
11641
11642 It is OK to define an inaccessible class; for example:
11643
11644 class A { class B; };
11645 class A::B {};
11646
11647 So, we ask cp_parser_class_name not to check accessibility.
11648
11649 We do not know if we will see a class-name, or a
11650 template-name. We look for a class-name first, in case the
11651 class-name is a template-id; if we looked for the
11652 template-name first we would stop after the template-name. */
11653 cp_parser_parse_tentatively (parser);
11654 type = cp_parser_class_name (parser,
11655 /*typename_keyword_p=*/false,
11656 /*template_keyword_p=*/false,
11657 /*type_p=*/true,
11658 /*check_access_p=*/false,
11659 /*check_dependency_p=*/false,
11660 /*class_head_p=*/true);
11661 /* If that didn't work, ignore the nested-name-specifier. */
11662 if (!cp_parser_parse_definitely (parser))
11663 {
11664 invalid_nested_name_p = true;
11665 id = cp_parser_identifier (parser);
11666 if (id == error_mark_node)
11667 id = NULL_TREE;
11668 }
11669 /* If we could not find a corresponding TYPE, treat this
11670 declaration like an unqualified declaration. */
11671 if (type == error_mark_node)
11672 nested_name_specifier = NULL_TREE;
11673 /* Otherwise, count the number of templates used in TYPE and its
11674 containing scopes. */
11675 else
11676 {
11677 tree scope;
11678
11679 for (scope = TREE_TYPE (type);
11680 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11681 scope = (TYPE_P (scope)
11682 ? TYPE_CONTEXT (scope)
11683 : DECL_CONTEXT (scope)))
11684 if (TYPE_P (scope)
11685 && CLASS_TYPE_P (scope)
11686 && CLASSTYPE_TEMPLATE_INFO (scope)
11687 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11688 ++num_templates;
11689 }
11690 }
11691 /* Otherwise, the identifier is optional. */
11692 else
11693 {
11694 /* We don't know whether what comes next is a template-id,
11695 an identifier, or nothing at all. */
11696 cp_parser_parse_tentatively (parser);
11697 /* Check for a template-id. */
11698 id = cp_parser_template_id (parser,
11699 /*template_keyword_p=*/false,
11700 /*check_dependency_p=*/true);
11701 /* If that didn't work, it could still be an identifier. */
11702 if (!cp_parser_parse_definitely (parser))
11703 {
11704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11705 id = cp_parser_identifier (parser);
11706 else
11707 id = NULL_TREE;
11708 }
11709 else
11710 {
11711 template_id_p = true;
11712 ++num_templates;
11713 }
11714 }
11715
11716 /* If it's not a `:' or a `{' then we can't really be looking at a
11717 class-head, since a class-head only appears as part of a
11718 class-specifier. We have to detect this situation before calling
11719 xref_tag, since that has irreversible side-effects. */
11720 if (!cp_parser_next_token_starts_class_definition_p (parser))
11721 {
11722 cp_parser_error (parser, "expected `{' or `:'");
11723 return error_mark_node;
11724 }
11725
11726 /* At this point, we're going ahead with the class-specifier, even
11727 if some other problem occurs. */
11728 cp_parser_commit_to_tentative_parse (parser);
11729 /* Issue the error about the overly-qualified name now. */
11730 if (qualified_p)
11731 cp_parser_error (parser,
11732 "global qualification of class name is invalid");
11733 else if (invalid_nested_name_p)
11734 cp_parser_error (parser,
11735 "qualified name does not name a class");
11736 /* Make sure that the right number of template parameters were
11737 present. */
11738 if (!cp_parser_check_template_parameters (parser, num_templates))
11739 /* If something went wrong, there is no point in even trying to
11740 process the class-definition. */
11741 return NULL_TREE;
11742
11743 /* We do not need to defer access checks for entities declared
11744 within the class. But, we do need to save any access checks that
11745 are currently deferred and restore them later, in case we are in
11746 the middle of something else. */
11747 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11748 if (*deferring_access_checks_p)
11749 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11750
11751 /* Look up the type. */
11752 if (template_id_p)
11753 {
11754 type = TREE_TYPE (id);
11755 maybe_process_partial_specialization (type);
11756 }
11757 else if (!nested_name_specifier)
11758 {
11759 /* If the class was unnamed, create a dummy name. */
11760 if (!id)
11761 id = make_anon_name ();
11762 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11763 }
11764 else
11765 {
848eed92 11766 bool new_type_p;
a723baf1
MM
11767 tree class_type;
11768
11769 /* Given:
11770
11771 template <typename T> struct S { struct T };
11772 template <typename T> struct S::T { };
11773
11774 we will get a TYPENAME_TYPE when processing the definition of
11775 `S::T'. We need to resolve it to the actual type before we
11776 try to define it. */
11777 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11778 {
11779 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11780 if (type != error_mark_node)
11781 type = TYPE_NAME (type);
11782 }
11783
11784 maybe_process_partial_specialization (TREE_TYPE (type));
11785 class_type = current_class_type;
11786 type = TREE_TYPE (handle_class_head (class_key,
11787 nested_name_specifier,
11788 type,
11789 attributes,
848eed92 11790 /*defn_p=*/true,
a723baf1
MM
11791 &new_type_p));
11792 if (type != error_mark_node)
11793 {
11794 if (!class_type && TYPE_CONTEXT (type))
11795 *nested_name_specifier_p = true;
11796 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11797 class_type))
11798 *nested_name_specifier_p = true;
11799 }
11800 }
11801 /* Indicate whether this class was declared as a `class' or as a
11802 `struct'. */
11803 if (TREE_CODE (type) == RECORD_TYPE)
11804 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11805 cp_parser_check_class_key (class_key, type);
11806
11807 /* Enter the scope containing the class; the names of base classes
11808 should be looked up in that context. For example, given:
11809
11810 struct A { struct B {}; struct C; };
11811 struct A::C : B {};
11812
11813 is valid. */
11814 if (nested_name_specifier)
11815 push_scope (nested_name_specifier);
11816 /* Now, look for the base-clause. */
11817 token = cp_lexer_peek_token (parser->lexer);
11818 if (token->type == CPP_COLON)
11819 {
11820 tree bases;
11821
11822 /* Get the list of base-classes. */
11823 bases = cp_parser_base_clause (parser);
11824 /* Process them. */
11825 xref_basetypes (type, bases);
11826 }
11827 /* Leave the scope given by the nested-name-specifier. We will
11828 enter the class scope itself while processing the members. */
11829 if (nested_name_specifier)
11830 pop_scope (nested_name_specifier);
11831
11832 return type;
11833}
11834
11835/* Parse a class-key.
11836
11837 class-key:
11838 class
11839 struct
11840 union
11841
11842 Returns the kind of class-key specified, or none_type to indicate
11843 error. */
11844
11845static enum tag_types
11846cp_parser_class_key (parser)
11847 cp_parser *parser;
11848{
11849 cp_token *token;
11850 enum tag_types tag_type;
11851
11852 /* Look for the class-key. */
11853 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11854 if (!token)
11855 return none_type;
11856
11857 /* Check to see if the TOKEN is a class-key. */
11858 tag_type = cp_parser_token_is_class_key (token);
11859 if (!tag_type)
11860 cp_parser_error (parser, "expected class-key");
11861 return tag_type;
11862}
11863
11864/* Parse an (optional) member-specification.
11865
11866 member-specification:
11867 member-declaration member-specification [opt]
11868 access-specifier : member-specification [opt] */
11869
11870static void
11871cp_parser_member_specification_opt (parser)
11872 cp_parser *parser;
11873{
11874 while (true)
11875 {
11876 cp_token *token;
11877 enum rid keyword;
11878
11879 /* Peek at the next token. */
11880 token = cp_lexer_peek_token (parser->lexer);
11881 /* If it's a `}', or EOF then we've seen all the members. */
11882 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11883 break;
11884
11885 /* See if this token is a keyword. */
11886 keyword = token->keyword;
11887 switch (keyword)
11888 {
11889 case RID_PUBLIC:
11890 case RID_PROTECTED:
11891 case RID_PRIVATE:
11892 /* Consume the access-specifier. */
11893 cp_lexer_consume_token (parser->lexer);
11894 /* Remember which access-specifier is active. */
11895 current_access_specifier = token->value;
11896 /* Look for the `:'. */
11897 cp_parser_require (parser, CPP_COLON, "`:'");
11898 break;
11899
11900 default:
11901 /* Otherwise, the next construction must be a
11902 member-declaration. */
11903 cp_parser_member_declaration (parser);
11904 reset_type_access_control ();
11905 }
11906 }
11907}
11908
11909/* Parse a member-declaration.
11910
11911 member-declaration:
11912 decl-specifier-seq [opt] member-declarator-list [opt] ;
11913 function-definition ; [opt]
11914 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11915 using-declaration
11916 template-declaration
11917
11918 member-declarator-list:
11919 member-declarator
11920 member-declarator-list , member-declarator
11921
11922 member-declarator:
11923 declarator pure-specifier [opt]
11924 declarator constant-initializer [opt]
11925 identifier [opt] : constant-expression
11926
11927 GNU Extensions:
11928
11929 member-declaration:
11930 __extension__ member-declaration
11931
11932 member-declarator:
11933 declarator attributes [opt] pure-specifier [opt]
11934 declarator attributes [opt] constant-initializer [opt]
11935 identifier [opt] attributes [opt] : constant-expression */
11936
11937static void
11938cp_parser_member_declaration (parser)
11939 cp_parser *parser;
11940{
11941 tree decl_specifiers;
11942 tree prefix_attributes;
11943 tree decl;
11944 bool declares_class_or_enum;
11945 bool friend_p;
11946 cp_token *token;
11947 int saved_pedantic;
11948
11949 /* Check for the `__extension__' keyword. */
11950 if (cp_parser_extension_opt (parser, &saved_pedantic))
11951 {
11952 /* Recurse. */
11953 cp_parser_member_declaration (parser);
11954 /* Restore the old value of the PEDANTIC flag. */
11955 pedantic = saved_pedantic;
11956
11957 return;
11958 }
11959
11960 /* Check for a template-declaration. */
11961 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11962 {
11963 /* Parse the template-declaration. */
11964 cp_parser_template_declaration (parser, /*member_p=*/true);
11965
11966 return;
11967 }
11968
11969 /* Check for a using-declaration. */
11970 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11971 {
11972 /* Parse the using-declaration. */
11973 cp_parser_using_declaration (parser);
11974
11975 return;
11976 }
11977
11978 /* We can't tell whether we're looking at a declaration or a
11979 function-definition. */
11980 cp_parser_parse_tentatively (parser);
11981
11982 /* Parse the decl-specifier-seq. */
11983 decl_specifiers
11984 = cp_parser_decl_specifier_seq (parser,
11985 CP_PARSER_FLAGS_OPTIONAL,
11986 &prefix_attributes,
11987 &declares_class_or_enum);
11988 /* If there is no declarator, then the decl-specifier-seq should
11989 specify a type. */
11990 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11991 {
11992 /* If there was no decl-specifier-seq, and the next token is a
11993 `;', then we have something like:
11994
11995 struct S { ; };
11996
11997 [class.mem]
11998
11999 Each member-declaration shall declare at least one member
12000 name of the class. */
12001 if (!decl_specifiers)
12002 {
12003 if (pedantic)
12004 pedwarn ("extra semicolon");
12005 }
12006 else
12007 {
12008 tree type;
12009
12010 /* See if this declaration is a friend. */
12011 friend_p = cp_parser_friend_p (decl_specifiers);
12012 /* If there were decl-specifiers, check to see if there was
12013 a class-declaration. */
12014 type = check_tag_decl (decl_specifiers);
12015 /* Nested classes have already been added to the class, but
12016 a `friend' needs to be explicitly registered. */
12017 if (friend_p)
12018 {
12019 /* If the `friend' keyword was present, the friend must
12020 be introduced with a class-key. */
12021 if (!declares_class_or_enum)
12022 error ("a class-key must be used when declaring a friend");
12023 /* In this case:
12024
12025 template <typename T> struct A {
12026 friend struct A<T>::B;
12027 };
12028
12029 A<T>::B will be represented by a TYPENAME_TYPE, and
12030 therefore not recognized by check_tag_decl. */
12031 if (!type)
12032 {
12033 tree specifier;
12034
12035 for (specifier = decl_specifiers;
12036 specifier;
12037 specifier = TREE_CHAIN (specifier))
12038 {
12039 tree s = TREE_VALUE (specifier);
12040
12041 if (TREE_CODE (s) == IDENTIFIER_NODE
12042 && IDENTIFIER_GLOBAL_VALUE (s))
12043 type = IDENTIFIER_GLOBAL_VALUE (s);
12044 if (TREE_CODE (s) == TYPE_DECL)
12045 s = TREE_TYPE (s);
12046 if (TYPE_P (s))
12047 {
12048 type = s;
12049 break;
12050 }
12051 }
12052 }
12053 if (!type)
12054 error ("friend declaration does not name a class or "
12055 "function");
12056 else
12057 make_friend_class (current_class_type, type);
12058 }
12059 /* If there is no TYPE, an error message will already have
12060 been issued. */
12061 else if (!type)
12062 ;
12063 /* An anonymous aggregate has to be handled specially; such
12064 a declaration really declares a data member (with a
12065 particular type), as opposed to a nested class. */
12066 else if (ANON_AGGR_TYPE_P (type))
12067 {
12068 /* Remove constructors and such from TYPE, now that we
12069 know it is an anoymous aggregate. */
12070 fixup_anonymous_aggr (type);
12071 /* And make the corresponding data member. */
12072 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12073 /* Add it to the class. */
12074 finish_member_declaration (decl);
12075 }
12076 }
12077 }
12078 else
12079 {
12080 /* See if these declarations will be friends. */
12081 friend_p = cp_parser_friend_p (decl_specifiers);
12082
12083 /* Keep going until we hit the `;' at the end of the
12084 declaration. */
12085 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12086 {
12087 tree attributes = NULL_TREE;
12088 tree first_attribute;
12089
12090 /* Peek at the next token. */
12091 token = cp_lexer_peek_token (parser->lexer);
12092
12093 /* Check for a bitfield declaration. */
12094 if (token->type == CPP_COLON
12095 || (token->type == CPP_NAME
12096 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12097 == CPP_COLON))
12098 {
12099 tree identifier;
12100 tree width;
12101
12102 /* Get the name of the bitfield. Note that we cannot just
12103 check TOKEN here because it may have been invalidated by
12104 the call to cp_lexer_peek_nth_token above. */
12105 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12106 identifier = cp_parser_identifier (parser);
12107 else
12108 identifier = NULL_TREE;
12109
12110 /* Consume the `:' token. */
12111 cp_lexer_consume_token (parser->lexer);
12112 /* Get the width of the bitfield. */
12113 width = cp_parser_constant_expression (parser);
12114
12115 /* Look for attributes that apply to the bitfield. */
12116 attributes = cp_parser_attributes_opt (parser);
12117 /* Remember which attributes are prefix attributes and
12118 which are not. */
12119 first_attribute = attributes;
12120 /* Combine the attributes. */
12121 attributes = chainon (prefix_attributes, attributes);
12122
12123 /* Create the bitfield declaration. */
12124 decl = grokbitfield (identifier,
12125 decl_specifiers,
12126 width);
12127 /* Apply the attributes. */
12128 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12129 }
12130 else
12131 {
12132 tree declarator;
12133 tree initializer;
12134 tree asm_specification;
12135 bool ctor_dtor_or_conv_p;
12136
12137 /* Parse the declarator. */
12138 declarator
12139 = cp_parser_declarator (parser,
12140 /*abstract_p=*/false,
12141 &ctor_dtor_or_conv_p);
12142
12143 /* If something went wrong parsing the declarator, make sure
12144 that we at least consume some tokens. */
12145 if (declarator == error_mark_node)
12146 {
12147 /* Skip to the end of the statement. */
12148 cp_parser_skip_to_end_of_statement (parser);
12149 break;
12150 }
12151
12152 /* Look for an asm-specification. */
12153 asm_specification = cp_parser_asm_specification_opt (parser);
12154 /* Look for attributes that apply to the declaration. */
12155 attributes = cp_parser_attributes_opt (parser);
12156 /* Remember which attributes are prefix attributes and
12157 which are not. */
12158 first_attribute = attributes;
12159 /* Combine the attributes. */
12160 attributes = chainon (prefix_attributes, attributes);
12161
12162 /* If it's an `=', then we have a constant-initializer or a
12163 pure-specifier. It is not correct to parse the
12164 initializer before registering the member declaration
12165 since the member declaration should be in scope while
12166 its initializer is processed. However, the rest of the
12167 front end does not yet provide an interface that allows
12168 us to handle this correctly. */
12169 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12170 {
12171 /* In [class.mem]:
12172
12173 A pure-specifier shall be used only in the declaration of
12174 a virtual function.
12175
12176 A member-declarator can contain a constant-initializer
12177 only if it declares a static member of integral or
12178 enumeration type.
12179
12180 Therefore, if the DECLARATOR is for a function, we look
12181 for a pure-specifier; otherwise, we look for a
12182 constant-initializer. When we call `grokfield', it will
12183 perform more stringent semantics checks. */
12184 if (TREE_CODE (declarator) == CALL_EXPR)
12185 initializer = cp_parser_pure_specifier (parser);
12186 else
12187 {
12188 /* This declaration cannot be a function
12189 definition. */
12190 cp_parser_commit_to_tentative_parse (parser);
12191 /* Parse the initializer. */
12192 initializer = cp_parser_constant_initializer (parser);
12193 }
12194 }
12195 /* Otherwise, there is no initializer. */
12196 else
12197 initializer = NULL_TREE;
12198
12199 /* See if we are probably looking at a function
12200 definition. We are certainly not looking at at a
12201 member-declarator. Calling `grokfield' has
12202 side-effects, so we must not do it unless we are sure
12203 that we are looking at a member-declarator. */
12204 if (cp_parser_token_starts_function_definition_p
12205 (cp_lexer_peek_token (parser->lexer)))
12206 decl = error_mark_node;
12207 else
12208 /* Create the declaration. */
12209 decl = grokfield (declarator,
12210 decl_specifiers,
12211 initializer,
12212 asm_specification,
12213 attributes);
12214 }
12215
12216 /* Reset PREFIX_ATTRIBUTES. */
12217 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12218 attributes = TREE_CHAIN (attributes);
12219 if (attributes)
12220 TREE_CHAIN (attributes) = NULL_TREE;
12221
12222 /* If there is any qualification still in effect, clear it
12223 now; we will be starting fresh with the next declarator. */
12224 parser->scope = NULL_TREE;
12225 parser->qualifying_scope = NULL_TREE;
12226 parser->object_scope = NULL_TREE;
12227 /* If it's a `,', then there are more declarators. */
12228 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12229 cp_lexer_consume_token (parser->lexer);
12230 /* If the next token isn't a `;', then we have a parse error. */
12231 else if (cp_lexer_next_token_is_not (parser->lexer,
12232 CPP_SEMICOLON))
12233 {
12234 cp_parser_error (parser, "expected `;'");
12235 /* Skip tokens until we find a `;' */
12236 cp_parser_skip_to_end_of_statement (parser);
12237
12238 break;
12239 }
12240
12241 if (decl)
12242 {
12243 /* Add DECL to the list of members. */
12244 if (!friend_p)
12245 finish_member_declaration (decl);
12246
12247 /* If DECL is a function, we must return
12248 to parse it later. (Even though there is no definition,
12249 there might be default arguments that need handling.) */
12250 if (TREE_CODE (decl) == FUNCTION_DECL)
12251 TREE_VALUE (parser->unparsed_functions_queues)
12252 = tree_cons (current_class_type, decl,
12253 TREE_VALUE (parser->unparsed_functions_queues));
12254 }
12255 }
12256 }
12257
12258 /* If everything went well, look for the `;'. */
12259 if (cp_parser_parse_definitely (parser))
12260 {
12261 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12262 return;
12263 }
12264
12265 /* Parse the function-definition. */
12266 decl = cp_parser_function_definition (parser, &friend_p);
12267 /* If the member was not a friend, declare it here. */
12268 if (!friend_p)
12269 finish_member_declaration (decl);
12270 /* Peek at the next token. */
12271 token = cp_lexer_peek_token (parser->lexer);
12272 /* If the next token is a semicolon, consume it. */
12273 if (token->type == CPP_SEMICOLON)
12274 cp_lexer_consume_token (parser->lexer);
12275}
12276
12277/* Parse a pure-specifier.
12278
12279 pure-specifier:
12280 = 0
12281
12282 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12283 Otherwiser, ERROR_MARK_NODE is returned. */
12284
12285static tree
12286cp_parser_pure_specifier (parser)
12287 cp_parser *parser;
12288{
12289 cp_token *token;
12290
12291 /* Look for the `=' token. */
12292 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12293 return error_mark_node;
12294 /* Look for the `0' token. */
12295 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12296 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12297 to get information from the lexer about how the number was
12298 spelled in order to fix this problem. */
12299 if (!token || !integer_zerop (token->value))
12300 return error_mark_node;
12301
12302 return integer_zero_node;
12303}
12304
12305/* Parse a constant-initializer.
12306
12307 constant-initializer:
12308 = constant-expression
12309
12310 Returns a representation of the constant-expression. */
12311
12312static tree
12313cp_parser_constant_initializer (parser)
12314 cp_parser *parser;
12315{
12316 /* Look for the `=' token. */
12317 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12318 return error_mark_node;
12319
12320 /* It is invalid to write:
12321
12322 struct S { static const int i = { 7 }; };
12323
12324 */
12325 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12326 {
12327 cp_parser_error (parser,
12328 "a brace-enclosed initializer is not allowed here");
12329 /* Consume the opening brace. */
12330 cp_lexer_consume_token (parser->lexer);
12331 /* Skip the initializer. */
12332 cp_parser_skip_to_closing_brace (parser);
12333 /* Look for the trailing `}'. */
12334 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12335
12336 return error_mark_node;
12337 }
12338
12339 return cp_parser_constant_expression (parser);
12340}
12341
12342/* Derived classes [gram.class.derived] */
12343
12344/* Parse a base-clause.
12345
12346 base-clause:
12347 : base-specifier-list
12348
12349 base-specifier-list:
12350 base-specifier
12351 base-specifier-list , base-specifier
12352
12353 Returns a TREE_LIST representing the base-classes, in the order in
12354 which they were declared. The representation of each node is as
12355 described by cp_parser_base_specifier.
12356
12357 In the case that no bases are specified, this function will return
12358 NULL_TREE, not ERROR_MARK_NODE. */
12359
12360static tree
12361cp_parser_base_clause (parser)
12362 cp_parser *parser;
12363{
12364 tree bases = NULL_TREE;
12365
12366 /* Look for the `:' that begins the list. */
12367 cp_parser_require (parser, CPP_COLON, "`:'");
12368
12369 /* Scan the base-specifier-list. */
12370 while (true)
12371 {
12372 cp_token *token;
12373 tree base;
12374
12375 /* Look for the base-specifier. */
12376 base = cp_parser_base_specifier (parser);
12377 /* Add BASE to the front of the list. */
12378 if (base != error_mark_node)
12379 {
12380 TREE_CHAIN (base) = bases;
12381 bases = base;
12382 }
12383 /* Peek at the next token. */
12384 token = cp_lexer_peek_token (parser->lexer);
12385 /* If it's not a comma, then the list is complete. */
12386 if (token->type != CPP_COMMA)
12387 break;
12388 /* Consume the `,'. */
12389 cp_lexer_consume_token (parser->lexer);
12390 }
12391
12392 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12393 base class had a qualified name. However, the next name that
12394 appears is certainly not qualified. */
12395 parser->scope = NULL_TREE;
12396 parser->qualifying_scope = NULL_TREE;
12397 parser->object_scope = NULL_TREE;
12398
12399 return nreverse (bases);
12400}
12401
12402/* Parse a base-specifier.
12403
12404 base-specifier:
12405 :: [opt] nested-name-specifier [opt] class-name
12406 virtual access-specifier [opt] :: [opt] nested-name-specifier
12407 [opt] class-name
12408 access-specifier virtual [opt] :: [opt] nested-name-specifier
12409 [opt] class-name
12410
12411 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12412 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12413 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12414 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12415
12416static tree
12417cp_parser_base_specifier (parser)
12418 cp_parser *parser;
12419{
12420 cp_token *token;
12421 bool done = false;
12422 bool virtual_p = false;
12423 bool duplicate_virtual_error_issued_p = false;
12424 bool duplicate_access_error_issued_p = false;
12425 bool class_scope_p;
12426 access_kind access = ak_none;
12427 tree access_node;
12428 tree type;
12429
12430 /* Process the optional `virtual' and `access-specifier'. */
12431 while (!done)
12432 {
12433 /* Peek at the next token. */
12434 token = cp_lexer_peek_token (parser->lexer);
12435 /* Process `virtual'. */
12436 switch (token->keyword)
12437 {
12438 case RID_VIRTUAL:
12439 /* If `virtual' appears more than once, issue an error. */
12440 if (virtual_p && !duplicate_virtual_error_issued_p)
12441 {
12442 cp_parser_error (parser,
12443 "`virtual' specified more than once in base-specified");
12444 duplicate_virtual_error_issued_p = true;
12445 }
12446
12447 virtual_p = true;
12448
12449 /* Consume the `virtual' token. */
12450 cp_lexer_consume_token (parser->lexer);
12451
12452 break;
12453
12454 case RID_PUBLIC:
12455 case RID_PROTECTED:
12456 case RID_PRIVATE:
12457 /* If more than one access specifier appears, issue an
12458 error. */
12459 if (access != ak_none && !duplicate_access_error_issued_p)
12460 {
12461 cp_parser_error (parser,
12462 "more than one access specifier in base-specified");
12463 duplicate_access_error_issued_p = true;
12464 }
12465
12466 access = ((access_kind)
12467 tree_low_cst (ridpointers[(int) token->keyword],
12468 /*pos=*/1));
12469
12470 /* Consume the access-specifier. */
12471 cp_lexer_consume_token (parser->lexer);
12472
12473 break;
12474
12475 default:
12476 done = true;
12477 break;
12478 }
12479 }
12480
12481 /* Map `virtual_p' and `access' onto one of the access
12482 tree-nodes. */
12483 if (!virtual_p)
12484 switch (access)
12485 {
12486 case ak_none:
12487 access_node = access_default_node;
12488 break;
12489 case ak_public:
12490 access_node = access_public_node;
12491 break;
12492 case ak_protected:
12493 access_node = access_protected_node;
12494 break;
12495 case ak_private:
12496 access_node = access_private_node;
12497 break;
12498 default:
12499 abort ();
12500 }
12501 else
12502 switch (access)
12503 {
12504 case ak_none:
12505 access_node = access_default_virtual_node;
12506 break;
12507 case ak_public:
12508 access_node = access_public_virtual_node;
12509 break;
12510 case ak_protected:
12511 access_node = access_protected_virtual_node;
12512 break;
12513 case ak_private:
12514 access_node = access_private_virtual_node;
12515 break;
12516 default:
12517 abort ();
12518 }
12519
12520 /* Look for the optional `::' operator. */
12521 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12522 /* Look for the nested-name-specifier. The simplest way to
12523 implement:
12524
12525 [temp.res]
12526
12527 The keyword `typename' is not permitted in a base-specifier or
12528 mem-initializer; in these contexts a qualified name that
12529 depends on a template-parameter is implicitly assumed to be a
12530 type name.
12531
12532 is to pretend that we have seen the `typename' keyword at this
12533 point. */
12534 cp_parser_nested_name_specifier_opt (parser,
12535 /*typename_keyword_p=*/true,
12536 /*check_dependency_p=*/true,
12537 /*type_p=*/true);
12538 /* If the base class is given by a qualified name, assume that names
12539 we see are type names or templates, as appropriate. */
12540 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12541 /* Finally, look for the class-name. */
12542 type = cp_parser_class_name (parser,
12543 class_scope_p,
12544 class_scope_p,
12545 /*type_p=*/true,
12546 /*check_access=*/true,
12547 /*check_dependency_p=*/true,
12548 /*class_head_p=*/false);
12549
12550 if (type == error_mark_node)
12551 return error_mark_node;
12552
12553 return finish_base_specifier (access_node, TREE_TYPE (type));
12554}
12555
12556/* Exception handling [gram.exception] */
12557
12558/* Parse an (optional) exception-specification.
12559
12560 exception-specification:
12561 throw ( type-id-list [opt] )
12562
12563 Returns a TREE_LIST representing the exception-specification. The
12564 TREE_VALUE of each node is a type. */
12565
12566static tree
12567cp_parser_exception_specification_opt (parser)
12568 cp_parser *parser;
12569{
12570 cp_token *token;
12571 tree type_id_list;
12572
12573 /* Peek at the next token. */
12574 token = cp_lexer_peek_token (parser->lexer);
12575 /* If it's not `throw', then there's no exception-specification. */
12576 if (!cp_parser_is_keyword (token, RID_THROW))
12577 return NULL_TREE;
12578
12579 /* Consume the `throw'. */
12580 cp_lexer_consume_token (parser->lexer);
12581
12582 /* Look for the `('. */
12583 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12584
12585 /* Peek at the next token. */
12586 token = cp_lexer_peek_token (parser->lexer);
12587 /* If it's not a `)', then there is a type-id-list. */
12588 if (token->type != CPP_CLOSE_PAREN)
12589 {
12590 const char *saved_message;
12591
12592 /* Types may not be defined in an exception-specification. */
12593 saved_message = parser->type_definition_forbidden_message;
12594 parser->type_definition_forbidden_message
12595 = "types may not be defined in an exception-specification";
12596 /* Parse the type-id-list. */
12597 type_id_list = cp_parser_type_id_list (parser);
12598 /* Restore the saved message. */
12599 parser->type_definition_forbidden_message = saved_message;
12600 }
12601 else
12602 type_id_list = empty_except_spec;
12603
12604 /* Look for the `)'. */
12605 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12606
12607 return type_id_list;
12608}
12609
12610/* Parse an (optional) type-id-list.
12611
12612 type-id-list:
12613 type-id
12614 type-id-list , type-id
12615
12616 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12617 in the order that the types were presented. */
12618
12619static tree
12620cp_parser_type_id_list (parser)
12621 cp_parser *parser;
12622{
12623 tree types = NULL_TREE;
12624
12625 while (true)
12626 {
12627 cp_token *token;
12628 tree type;
12629
12630 /* Get the next type-id. */
12631 type = cp_parser_type_id (parser);
12632 /* Add it to the list. */
12633 types = add_exception_specifier (types, type, /*complain=*/1);
12634 /* Peek at the next token. */
12635 token = cp_lexer_peek_token (parser->lexer);
12636 /* If it is not a `,', we are done. */
12637 if (token->type != CPP_COMMA)
12638 break;
12639 /* Consume the `,'. */
12640 cp_lexer_consume_token (parser->lexer);
12641 }
12642
12643 return nreverse (types);
12644}
12645
12646/* Parse a try-block.
12647
12648 try-block:
12649 try compound-statement handler-seq */
12650
12651static tree
12652cp_parser_try_block (parser)
12653 cp_parser *parser;
12654{
12655 tree try_block;
12656
12657 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12658 try_block = begin_try_block ();
12659 cp_parser_compound_statement (parser);
12660 finish_try_block (try_block);
12661 cp_parser_handler_seq (parser);
12662 finish_handler_sequence (try_block);
12663
12664 return try_block;
12665}
12666
12667/* Parse a function-try-block.
12668
12669 function-try-block:
12670 try ctor-initializer [opt] function-body handler-seq */
12671
12672static bool
12673cp_parser_function_try_block (parser)
12674 cp_parser *parser;
12675{
12676 tree try_block;
12677 bool ctor_initializer_p;
12678
12679 /* Look for the `try' keyword. */
12680 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12681 return false;
12682 /* Let the rest of the front-end know where we are. */
12683 try_block = begin_function_try_block ();
12684 /* Parse the function-body. */
12685 ctor_initializer_p
12686 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12687 /* We're done with the `try' part. */
12688 finish_function_try_block (try_block);
12689 /* Parse the handlers. */
12690 cp_parser_handler_seq (parser);
12691 /* We're done with the handlers. */
12692 finish_function_handler_sequence (try_block);
12693
12694 return ctor_initializer_p;
12695}
12696
12697/* Parse a handler-seq.
12698
12699 handler-seq:
12700 handler handler-seq [opt] */
12701
12702static void
12703cp_parser_handler_seq (parser)
12704 cp_parser *parser;
12705{
12706 while (true)
12707 {
12708 cp_token *token;
12709
12710 /* Parse the handler. */
12711 cp_parser_handler (parser);
12712 /* Peek at the next token. */
12713 token = cp_lexer_peek_token (parser->lexer);
12714 /* If it's not `catch' then there are no more handlers. */
12715 if (!cp_parser_is_keyword (token, RID_CATCH))
12716 break;
12717 }
12718}
12719
12720/* Parse a handler.
12721
12722 handler:
12723 catch ( exception-declaration ) compound-statement */
12724
12725static void
12726cp_parser_handler (parser)
12727 cp_parser *parser;
12728{
12729 tree handler;
12730 tree declaration;
12731
12732 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12733 handler = begin_handler ();
12734 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12735 declaration = cp_parser_exception_declaration (parser);
12736 finish_handler_parms (declaration, handler);
12737 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12738 cp_parser_compound_statement (parser);
12739 finish_handler (handler);
12740}
12741
12742/* Parse an exception-declaration.
12743
12744 exception-declaration:
12745 type-specifier-seq declarator
12746 type-specifier-seq abstract-declarator
12747 type-specifier-seq
12748 ...
12749
12750 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12751 ellipsis variant is used. */
12752
12753static tree
12754cp_parser_exception_declaration (parser)
12755 cp_parser *parser;
12756{
12757 tree type_specifiers;
12758 tree declarator;
12759 const char *saved_message;
12760
12761 /* If it's an ellipsis, it's easy to handle. */
12762 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12763 {
12764 /* Consume the `...' token. */
12765 cp_lexer_consume_token (parser->lexer);
12766 return NULL_TREE;
12767 }
12768
12769 /* Types may not be defined in exception-declarations. */
12770 saved_message = parser->type_definition_forbidden_message;
12771 parser->type_definition_forbidden_message
12772 = "types may not be defined in exception-declarations";
12773
12774 /* Parse the type-specifier-seq. */
12775 type_specifiers = cp_parser_type_specifier_seq (parser);
12776 /* If it's a `)', then there is no declarator. */
12777 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12778 declarator = NULL_TREE;
12779 else
12780 {
12781 /* Otherwise, we can't be sure whether we are looking at a
12782 direct, or an abstract, declarator. */
12783 cp_parser_parse_tentatively (parser);
12784 /* Try an ordinary declarator. */
12785 declarator = cp_parser_declarator (parser,
12786 /*abstract_p=*/false,
12787 /*ctor_dtor_or_conv_p=*/NULL);
12788 /* If that didn't work, try an abstract declarator. */
12789 if (!cp_parser_parse_definitely (parser))
12790 declarator = cp_parser_declarator (parser,
12791 /*abstract_p=*/true,
12792 /*ctor_dtor_or_conv_p=*/NULL);
12793 }
12794
12795 /* Restore the saved message. */
12796 parser->type_definition_forbidden_message = saved_message;
12797
12798 return start_handler_parms (type_specifiers, declarator);
12799}
12800
12801/* Parse a throw-expression.
12802
12803 throw-expression:
12804 throw assignment-expresion [opt]
12805
12806 Returns a THROW_EXPR representing the throw-expression. */
12807
12808static tree
12809cp_parser_throw_expression (parser)
12810 cp_parser *parser;
12811{
12812 tree expression;
12813
12814 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12815 /* We can't be sure if there is an assignment-expression or not. */
12816 cp_parser_parse_tentatively (parser);
12817 /* Try it. */
12818 expression = cp_parser_assignment_expression (parser);
12819 /* If it didn't work, this is just a rethrow. */
12820 if (!cp_parser_parse_definitely (parser))
12821 expression = NULL_TREE;
12822
12823 return build_throw (expression);
12824}
12825
12826/* GNU Extensions */
12827
12828/* Parse an (optional) asm-specification.
12829
12830 asm-specification:
12831 asm ( string-literal )
12832
12833 If the asm-specification is present, returns a STRING_CST
12834 corresponding to the string-literal. Otherwise, returns
12835 NULL_TREE. */
12836
12837static tree
12838cp_parser_asm_specification_opt (parser)
12839 cp_parser *parser;
12840{
12841 cp_token *token;
12842 tree asm_specification;
12843
12844 /* Peek at the next token. */
12845 token = cp_lexer_peek_token (parser->lexer);
12846 /* If the next token isn't the `asm' keyword, then there's no
12847 asm-specification. */
12848 if (!cp_parser_is_keyword (token, RID_ASM))
12849 return NULL_TREE;
12850
12851 /* Consume the `asm' token. */
12852 cp_lexer_consume_token (parser->lexer);
12853 /* Look for the `('. */
12854 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12855
12856 /* Look for the string-literal. */
12857 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12858 if (token)
12859 asm_specification = token->value;
12860 else
12861 asm_specification = NULL_TREE;
12862
12863 /* Look for the `)'. */
12864 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12865
12866 return asm_specification;
12867}
12868
12869/* Parse an asm-operand-list.
12870
12871 asm-operand-list:
12872 asm-operand
12873 asm-operand-list , asm-operand
12874
12875 asm-operand:
12876 string-literal ( expression )
12877 [ string-literal ] string-literal ( expression )
12878
12879 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12880 each node is the expression. The TREE_PURPOSE is itself a
12881 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12882 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12883 is a STRING_CST for the string literal before the parenthesis. */
12884
12885static tree
12886cp_parser_asm_operand_list (parser)
12887 cp_parser *parser;
12888{
12889 tree asm_operands = NULL_TREE;
12890
12891 while (true)
12892 {
12893 tree string_literal;
12894 tree expression;
12895 tree name;
12896 cp_token *token;
12897
12898 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12899 {
12900 /* Consume the `[' token. */
12901 cp_lexer_consume_token (parser->lexer);
12902 /* Read the operand name. */
12903 name = cp_parser_identifier (parser);
12904 if (name != error_mark_node)
12905 name = build_string (IDENTIFIER_LENGTH (name),
12906 IDENTIFIER_POINTER (name));
12907 /* Look for the closing `]'. */
12908 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12909 }
12910 else
12911 name = NULL_TREE;
12912 /* Look for the string-literal. */
12913 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12914 string_literal = token ? token->value : error_mark_node;
12915 /* Look for the `('. */
12916 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12917 /* Parse the expression. */
12918 expression = cp_parser_expression (parser);
12919 /* Look for the `)'. */
12920 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12921 /* Add this operand to the list. */
12922 asm_operands = tree_cons (build_tree_list (name, string_literal),
12923 expression,
12924 asm_operands);
12925 /* If the next token is not a `,', there are no more
12926 operands. */
12927 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12928 break;
12929 /* Consume the `,'. */
12930 cp_lexer_consume_token (parser->lexer);
12931 }
12932
12933 return nreverse (asm_operands);
12934}
12935
12936/* Parse an asm-clobber-list.
12937
12938 asm-clobber-list:
12939 string-literal
12940 asm-clobber-list , string-literal
12941
12942 Returns a TREE_LIST, indicating the clobbers in the order that they
12943 appeared. The TREE_VALUE of each node is a STRING_CST. */
12944
12945static tree
12946cp_parser_asm_clobber_list (parser)
12947 cp_parser *parser;
12948{
12949 tree clobbers = NULL_TREE;
12950
12951 while (true)
12952 {
12953 cp_token *token;
12954 tree string_literal;
12955
12956 /* Look for the string literal. */
12957 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12958 string_literal = token ? token->value : error_mark_node;
12959 /* Add it to the list. */
12960 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12961 /* If the next token is not a `,', then the list is
12962 complete. */
12963 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12964 break;
12965 /* Consume the `,' token. */
12966 cp_lexer_consume_token (parser->lexer);
12967 }
12968
12969 return clobbers;
12970}
12971
12972/* Parse an (optional) series of attributes.
12973
12974 attributes:
12975 attributes attribute
12976
12977 attribute:
12978 __attribute__ (( attribute-list [opt] ))
12979
12980 The return value is as for cp_parser_attribute_list. */
12981
12982static tree
12983cp_parser_attributes_opt (parser)
12984 cp_parser *parser;
12985{
12986 tree attributes = NULL_TREE;
12987
12988 while (true)
12989 {
12990 cp_token *token;
12991 tree attribute_list;
12992
12993 /* Peek at the next token. */
12994 token = cp_lexer_peek_token (parser->lexer);
12995 /* If it's not `__attribute__', then we're done. */
12996 if (token->keyword != RID_ATTRIBUTE)
12997 break;
12998
12999 /* Consume the `__attribute__' keyword. */
13000 cp_lexer_consume_token (parser->lexer);
13001 /* Look for the two `(' tokens. */
13002 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13003 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13004
13005 /* Peek at the next token. */
13006 token = cp_lexer_peek_token (parser->lexer);
13007 if (token->type != CPP_CLOSE_PAREN)
13008 /* Parse the attribute-list. */
13009 attribute_list = cp_parser_attribute_list (parser);
13010 else
13011 /* If the next token is a `)', then there is no attribute
13012 list. */
13013 attribute_list = NULL;
13014
13015 /* Look for the two `)' tokens. */
13016 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13017 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13018
13019 /* Add these new attributes to the list. */
13020 attributes = chainon (attributes, attribute_list);
13021 }
13022
13023 return attributes;
13024}
13025
13026/* Parse an attribute-list.
13027
13028 attribute-list:
13029 attribute
13030 attribute-list , attribute
13031
13032 attribute:
13033 identifier
13034 identifier ( identifier )
13035 identifier ( identifier , expression-list )
13036 identifier ( expression-list )
13037
13038 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13039 TREE_PURPOSE of each node is the identifier indicating which
13040 attribute is in use. The TREE_VALUE represents the arguments, if
13041 any. */
13042
13043static tree
13044cp_parser_attribute_list (parser)
13045 cp_parser *parser;
13046{
13047 tree attribute_list = NULL_TREE;
13048
13049 while (true)
13050 {
13051 cp_token *token;
13052 tree identifier;
13053 tree attribute;
13054
13055 /* Look for the identifier. We also allow keywords here; for
13056 example `__attribute__ ((const))' is legal. */
13057 token = cp_lexer_peek_token (parser->lexer);
13058 if (token->type != CPP_NAME
13059 && token->type != CPP_KEYWORD)
13060 return error_mark_node;
13061 /* Consume the token. */
13062 token = cp_lexer_consume_token (parser->lexer);
13063
13064 /* Save away the identifier that indicates which attribute this is. */
13065 identifier = token->value;
13066 attribute = build_tree_list (identifier, NULL_TREE);
13067
13068 /* Peek at the next token. */
13069 token = cp_lexer_peek_token (parser->lexer);
13070 /* If it's an `(', then parse the attribute arguments. */
13071 if (token->type == CPP_OPEN_PAREN)
13072 {
13073 tree arguments;
13074 int arguments_allowed_p = 1;
13075
13076 /* Consume the `('. */
13077 cp_lexer_consume_token (parser->lexer);
13078 /* Peek at the next token. */
13079 token = cp_lexer_peek_token (parser->lexer);
13080 /* Check to see if the next token is an identifier. */
13081 if (token->type == CPP_NAME)
13082 {
13083 /* Save the identifier. */
13084 identifier = token->value;
13085 /* Consume the identifier. */
13086 cp_lexer_consume_token (parser->lexer);
13087 /* Peek at the next token. */
13088 token = cp_lexer_peek_token (parser->lexer);
13089 /* If the next token is a `,', then there are some other
13090 expressions as well. */
13091 if (token->type == CPP_COMMA)
13092 /* Consume the comma. */
13093 cp_lexer_consume_token (parser->lexer);
13094 else
13095 arguments_allowed_p = 0;
13096 }
13097 else
13098 identifier = NULL_TREE;
13099
13100 /* If there are arguments, parse them too. */
13101 if (arguments_allowed_p)
13102 arguments = cp_parser_expression_list (parser);
13103 else
13104 arguments = NULL_TREE;
13105
13106 /* Combine the identifier and the arguments. */
13107 if (identifier)
13108 arguments = tree_cons (NULL_TREE, identifier, arguments);
13109
13110 /* Save the identifier and arguments away. */
13111 TREE_VALUE (attribute) = arguments;
13112
13113 /* Look for the closing `)'. */
13114 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13115 }
13116
13117 /* Add this attribute to the list. */
13118 TREE_CHAIN (attribute) = attribute_list;
13119 attribute_list = attribute;
13120
13121 /* Now, look for more attributes. */
13122 token = cp_lexer_peek_token (parser->lexer);
13123 /* If the next token isn't a `,', we're done. */
13124 if (token->type != CPP_COMMA)
13125 break;
13126
13127 /* Consume the commma and keep going. */
13128 cp_lexer_consume_token (parser->lexer);
13129 }
13130
13131 /* We built up the list in reverse order. */
13132 return nreverse (attribute_list);
13133}
13134
13135/* Parse an optional `__extension__' keyword. Returns TRUE if it is
13136 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13137 current value of the PEDANTIC flag, regardless of whether or not
13138 the `__extension__' keyword is present. The caller is responsible
13139 for restoring the value of the PEDANTIC flag. */
13140
13141static bool
13142cp_parser_extension_opt (parser, saved_pedantic)
13143 cp_parser *parser;
13144 int *saved_pedantic;
13145{
13146 /* Save the old value of the PEDANTIC flag. */
13147 *saved_pedantic = pedantic;
13148
13149 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13150 {
13151 /* Consume the `__extension__' token. */
13152 cp_lexer_consume_token (parser->lexer);
13153 /* We're not being pedantic while the `__extension__' keyword is
13154 in effect. */
13155 pedantic = 0;
13156
13157 return true;
13158 }
13159
13160 return false;
13161}
13162
13163/* Parse a label declaration.
13164
13165 label-declaration:
13166 __label__ label-declarator-seq ;
13167
13168 label-declarator-seq:
13169 identifier , label-declarator-seq
13170 identifier */
13171
13172static void
13173cp_parser_label_declaration (parser)
13174 cp_parser *parser;
13175{
13176 /* Look for the `__label__' keyword. */
13177 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13178
13179 while (true)
13180 {
13181 tree identifier;
13182
13183 /* Look for an identifier. */
13184 identifier = cp_parser_identifier (parser);
13185 /* Declare it as a lobel. */
13186 finish_label_decl (identifier);
13187 /* If the next token is a `;', stop. */
13188 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13189 break;
13190 /* Look for the `,' separating the label declarations. */
13191 cp_parser_require (parser, CPP_COMMA, "`,'");
13192 }
13193
13194 /* Look for the final `;'. */
13195 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13196}
13197
13198/* Support Functions */
13199
13200/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13201 NAME should have one of the representations used for an
13202 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13203 is returned. If PARSER->SCOPE is a dependent type, then a
13204 SCOPE_REF is returned.
13205
13206 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13207 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13208 was formed. Abstractly, such entities should not be passed to this
13209 function, because they do not need to be looked up, but it is
13210 simpler to check for this special case here, rather than at the
13211 call-sites.
13212
13213 In cases not explicitly covered above, this function returns a
13214 DECL, OVERLOAD, or baselink representing the result of the lookup.
13215 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13216 is returned.
13217
13218 If CHECK_ACCESS is TRUE, then access control is performed on the
13219 declaration to which the name resolves, and an error message is
13220 issued if the declaration is inaccessible.
13221
13222 If IS_TYPE is TRUE, bindings that do not refer to types are
13223 ignored.
13224
13225 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13226 types. */
13227
13228static tree
13229cp_parser_lookup_name (parser, name, check_access, is_type,
13230 check_dependency)
13231 cp_parser *parser;
13232 tree name;
13233 bool check_access;
13234 bool is_type;
13235 bool check_dependency;
13236{
13237 tree decl;
13238 tree object_type = parser->context->object_type;
13239
13240 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13241 no longer valid. Note that if we are parsing tentatively, and
13242 the parse fails, OBJECT_TYPE will be automatically restored. */
13243 parser->context->object_type = NULL_TREE;
13244
13245 if (name == error_mark_node)
13246 return error_mark_node;
13247
13248 /* A template-id has already been resolved; there is no lookup to
13249 do. */
13250 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13251 return name;
13252 if (BASELINK_P (name))
13253 {
13254 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13255 == TEMPLATE_ID_EXPR),
13256 20020909);
13257 return name;
13258 }
13259
13260 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13261 it should already have been checked to make sure that the name
13262 used matches the type being destroyed. */
13263 if (TREE_CODE (name) == BIT_NOT_EXPR)
13264 {
13265 tree type;
13266
13267 /* Figure out to which type this destructor applies. */
13268 if (parser->scope)
13269 type = parser->scope;
13270 else if (object_type)
13271 type = object_type;
13272 else
13273 type = current_class_type;
13274 /* If that's not a class type, there is no destructor. */
13275 if (!type || !CLASS_TYPE_P (type))
13276 return error_mark_node;
13277 /* If it was a class type, return the destructor. */
13278 return CLASSTYPE_DESTRUCTORS (type);
13279 }
13280
13281 /* By this point, the NAME should be an ordinary identifier. If
13282 the id-expression was a qualified name, the qualifying scope is
13283 stored in PARSER->SCOPE at this point. */
13284 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13285 20000619);
13286
13287 /* Perform the lookup. */
13288 if (parser->scope)
13289 {
13290 bool dependent_type_p;
13291
13292 if (parser->scope == error_mark_node)
13293 return error_mark_node;
13294
13295 /* If the SCOPE is dependent, the lookup must be deferred until
13296 the template is instantiated -- unless we are explicitly
13297 looking up names in uninstantiated templates. Even then, we
13298 cannot look up the name if the scope is not a class type; it
13299 might, for example, be a template type parameter. */
13300 dependent_type_p = (TYPE_P (parser->scope)
13301 && !(parser->in_declarator_p
13302 && currently_open_class (parser->scope))
13303 && cp_parser_dependent_type_p (parser->scope));
13304 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13305 && dependent_type_p)
13306 {
13307 if (!is_type)
13308 decl = build_nt (SCOPE_REF, parser->scope, name);
13309 else
13310 /* The resolution to Core Issue 180 says that `struct A::B'
13311 should be considered a type-name, even if `A' is
13312 dependent. */
13313 decl = TYPE_NAME (make_typename_type (parser->scope,
13314 name,
13315 /*complain=*/1));
13316 }
13317 else
13318 {
13319 /* If PARSER->SCOPE is a dependent type, then it must be a
13320 class type, and we must not be checking dependencies;
13321 otherwise, we would have processed this lookup above. So
13322 that PARSER->SCOPE is not considered a dependent base by
13323 lookup_member, we must enter the scope here. */
13324 if (dependent_type_p)
13325 push_scope (parser->scope);
13326 /* If the PARSER->SCOPE is a a template specialization, it
13327 may be instantiated during name lookup. In that case,
13328 errors may be issued. Even if we rollback the current
13329 tentative parse, those errors are valid. */
13330 decl = lookup_qualified_name (parser->scope, name, is_type,
13331 /*flags=*/0);
13332 if (dependent_type_p)
13333 pop_scope (parser->scope);
13334 }
13335 parser->qualifying_scope = parser->scope;
13336 parser->object_scope = NULL_TREE;
13337 }
13338 else if (object_type)
13339 {
13340 tree object_decl = NULL_TREE;
13341 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13342 OBJECT_TYPE is not a class. */
13343 if (CLASS_TYPE_P (object_type))
13344 /* If the OBJECT_TYPE is a template specialization, it may
13345 be instantiated during name lookup. In that case, errors
13346 may be issued. Even if we rollback the current tentative
13347 parse, those errors are valid. */
13348 object_decl = lookup_member (object_type,
13349 name,
13350 /*protect=*/0, is_type);
13351 /* Look it up in the enclosing context, too. */
13352 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13353 /*namespaces_only=*/0,
13354 /*flags=*/0);
13355 parser->object_scope = object_type;
13356 parser->qualifying_scope = NULL_TREE;
13357 if (object_decl)
13358 decl = object_decl;
13359 }
13360 else
13361 {
13362 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13363 /*namespaces_only=*/0,
13364 /*flags=*/0);
13365 parser->qualifying_scope = NULL_TREE;
13366 parser->object_scope = NULL_TREE;
13367 }
13368
13369 /* If the lookup failed, let our caller know. */
13370 if (!decl
13371 || decl == error_mark_node
13372 || (TREE_CODE (decl) == FUNCTION_DECL
13373 && DECL_ANTICIPATED (decl)))
13374 return error_mark_node;
13375
13376 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13377 if (TREE_CODE (decl) == TREE_LIST)
13378 {
13379 /* The error message we have to print is too complicated for
13380 cp_parser_error, so we incorporate its actions directly. */
13381 cp_parser_simulate_error (parser);
13382 if (!cp_parser_parsing_tentatively (parser)
13383 || cp_parser_committed_to_tentative_parse (parser))
13384 {
13385 error ("reference to `%D' is ambiguous", name);
13386 print_candidates (decl);
13387 }
13388 return error_mark_node;
13389 }
13390
13391 my_friendly_assert (DECL_P (decl)
13392 || TREE_CODE (decl) == OVERLOAD
13393 || TREE_CODE (decl) == SCOPE_REF
13394 || BASELINK_P (decl),
13395 20000619);
13396
13397 /* If we have resolved the name of a member declaration, check to
13398 see if the declaration is accessible. When the name resolves to
13399 set of overloaded functions, accesibility is checked when
13400 overload resolution is done.
13401
13402 During an explicit instantiation, access is not checked at all,
13403 as per [temp.explicit]. */
13404 if (check_access && scope_chain->check_access && DECL_P (decl))
13405 {
13406 tree qualifying_type;
13407
13408 /* Figure out the type through which DECL is being
13409 accessed. */
13410 qualifying_type
13411 = cp_parser_scope_through_which_access_occurs (decl,
13412 object_type,
13413 parser->scope);
13414 if (qualifying_type)
13415 {
13416 /* If we are supposed to defer access checks, just record
13417 the information for later. */
13418 if (parser->context->deferring_access_checks_p)
13419 cp_parser_defer_access_check (parser, qualifying_type, decl);
13420 /* Otherwise, check accessibility now. */
13421 else
13422 enforce_access (qualifying_type, decl);
13423 }
13424 }
13425
13426 return decl;
13427}
13428
13429/* Like cp_parser_lookup_name, but for use in the typical case where
13430 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13431 TRUE. */
13432
13433static tree
13434cp_parser_lookup_name_simple (parser, name)
13435 cp_parser *parser;
13436 tree name;
13437{
13438 return cp_parser_lookup_name (parser, name,
13439 /*check_access=*/true,
13440 /*is_type=*/false,
13441 /*check_dependency=*/true);
13442}
13443
13444/* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13445 TYPENAME_TYPE corresponds. Note that this function peers inside
13446 uninstantiated templates and therefore should be used only in
13447 extremely limited situations. */
13448
13449static tree
13450cp_parser_resolve_typename_type (parser, type)
13451 cp_parser *parser;
13452 tree type;
13453{
13454 tree scope;
13455 tree name;
13456 tree decl;
13457
13458 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13459 20010702);
13460
13461 scope = TYPE_CONTEXT (type);
13462 name = DECL_NAME (TYPE_NAME (type));
13463
13464 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13465 it first before we can figure out what NAME refers to. */
13466 if (TREE_CODE (scope) == TYPENAME_TYPE)
13467 scope = cp_parser_resolve_typename_type (parser, scope);
13468 /* If we don't know what SCOPE refers to, then we cannot resolve the
13469 TYPENAME_TYPE. */
13470 if (scope == error_mark_node)
13471 return error_mark_node;
13472 /* If the SCOPE is a template type parameter, we have no way of
13473 resolving the name. */
13474 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13475 return type;
13476 /* Enter the SCOPE so that name lookup will be resolved as if we
13477 were in the class definition. In particular, SCOPE will no
13478 longer be considered a dependent type. */
13479 push_scope (scope);
13480 /* Look up the declaration. */
13481 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13482 /* If all went well, we got a TYPE_DECL for a non-typename. */
13483 if (!decl
13484 || TREE_CODE (decl) != TYPE_DECL
13485 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13486 {
13487 cp_parser_error (parser, "could not resolve typename type");
13488 type = error_mark_node;
13489 }
13490 else
13491 type = TREE_TYPE (decl);
13492 /* Leave the SCOPE. */
13493 pop_scope (scope);
13494
13495 return type;
13496}
13497
13498/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13499 the current context, return the TYPE_DECL. If TAG_NAME_P is
13500 true, the DECL indicates the class being defined in a class-head,
13501 or declared in an elaborated-type-specifier.
13502
13503 Otherwise, return DECL. */
13504
13505static tree
13506cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13507{
13508 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13509 the scope of the class, then treat the TEMPLATE_DECL as a
13510 class-name. For example, in:
13511
13512 template <class T> struct S {
13513 S s;
13514 };
13515
13516 is OK.
13517
13518 If the TEMPLATE_DECL is being declared as part of a class-head,
13519 the same translation occurs:
13520
13521 struct A {
13522 template <typename T> struct B;
13523 };
13524
13525 template <typename T> struct A::B {};
13526
13527 Similarly, in a elaborated-type-specifier:
13528
13529 namespace N { struct X{}; }
13530
13531 struct A {
13532 template <typename T> friend struct N::X;
13533 };
13534
13535 */
13536 if (DECL_CLASS_TEMPLATE_P (decl)
13537 && (tag_name_p
13538 || (current_class_type
13539 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13540 current_class_type))))
13541 return DECL_TEMPLATE_RESULT (decl);
13542
13543 return decl;
13544}
13545
13546/* If too many, or too few, template-parameter lists apply to the
13547 declarator, issue an error message. Returns TRUE if all went well,
13548 and FALSE otherwise. */
13549
13550static bool
13551cp_parser_check_declarator_template_parameters (parser, declarator)
13552 cp_parser *parser;
13553 tree declarator;
13554{
13555 unsigned num_templates;
13556
13557 /* We haven't seen any classes that involve template parameters yet. */
13558 num_templates = 0;
13559
13560 switch (TREE_CODE (declarator))
13561 {
13562 case CALL_EXPR:
13563 case ARRAY_REF:
13564 case INDIRECT_REF:
13565 case ADDR_EXPR:
13566 {
13567 tree main_declarator = TREE_OPERAND (declarator, 0);
13568 return
13569 cp_parser_check_declarator_template_parameters (parser,
13570 main_declarator);
13571 }
13572
13573 case SCOPE_REF:
13574 {
13575 tree scope;
13576 tree member;
13577
13578 scope = TREE_OPERAND (declarator, 0);
13579 member = TREE_OPERAND (declarator, 1);
13580
13581 /* If this is a pointer-to-member, then we are not interested
13582 in the SCOPE, because it does not qualify the thing that is
13583 being declared. */
13584 if (TREE_CODE (member) == INDIRECT_REF)
13585 return (cp_parser_check_declarator_template_parameters
13586 (parser, member));
13587
13588 while (scope && CLASS_TYPE_P (scope))
13589 {
13590 /* You're supposed to have one `template <...>'
13591 for every template class, but you don't need one
13592 for a full specialization. For example:
13593
13594 template <class T> struct S{};
13595 template <> struct S<int> { void f(); };
13596 void S<int>::f () {}
13597
13598 is correct; there shouldn't be a `template <>' for
13599 the definition of `S<int>::f'. */
13600 if (CLASSTYPE_TEMPLATE_INFO (scope)
13601 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13602 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13603 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13604 ++num_templates;
13605
13606 scope = TYPE_CONTEXT (scope);
13607 }
13608 }
13609
13610 /* Fall through. */
13611
13612 default:
13613 /* If the DECLARATOR has the form `X<y>' then it uses one
13614 additional level of template parameters. */
13615 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13616 ++num_templates;
13617
13618 return cp_parser_check_template_parameters (parser,
13619 num_templates);
13620 }
13621}
13622
13623/* NUM_TEMPLATES were used in the current declaration. If that is
13624 invalid, return FALSE and issue an error messages. Otherwise,
13625 return TRUE. */
13626
13627static bool
13628cp_parser_check_template_parameters (parser, num_templates)
13629 cp_parser *parser;
13630 unsigned num_templates;
13631{
13632 /* If there are more template classes than parameter lists, we have
13633 something like:
13634
13635 template <class T> void S<T>::R<T>::f (); */
13636 if (parser->num_template_parameter_lists < num_templates)
13637 {
13638 error ("too few template-parameter-lists");
13639 return false;
13640 }
13641 /* If there are the same number of template classes and parameter
13642 lists, that's OK. */
13643 if (parser->num_template_parameter_lists == num_templates)
13644 return true;
13645 /* If there are more, but only one more, then we are referring to a
13646 member template. That's OK too. */
13647 if (parser->num_template_parameter_lists == num_templates + 1)
13648 return true;
13649 /* Otherwise, there are too many template parameter lists. We have
13650 something like:
13651
13652 template <class T> template <class U> void S::f(); */
13653 error ("too many template-parameter-lists");
13654 return false;
13655}
13656
13657/* Parse a binary-expression of the general form:
13658
13659 binary-expression:
13660 <expr>
13661 binary-expression <token> <expr>
13662
13663 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13664 to parser the <expr>s. If the first production is used, then the
13665 value returned by FN is returned directly. Otherwise, a node with
13666 the indicated EXPR_TYPE is returned, with operands corresponding to
13667 the two sub-expressions. */
13668
13669static tree
13670cp_parser_binary_expression (parser, token_tree_map, fn)
13671 cp_parser *parser;
13672 cp_parser_token_tree_map token_tree_map;
13673 cp_parser_expression_fn fn;
13674{
13675 tree lhs;
13676
13677 /* Parse the first expression. */
13678 lhs = (*fn) (parser);
13679 /* Now, look for more expressions. */
13680 while (true)
13681 {
13682 cp_token *token;
13683 cp_parser_token_tree_map_node *map_node;
13684 tree rhs;
13685
13686 /* Peek at the next token. */
13687 token = cp_lexer_peek_token (parser->lexer);
13688 /* If the token is `>', and that's not an operator at the
13689 moment, then we're done. */
13690 if (token->type == CPP_GREATER
13691 && !parser->greater_than_is_operator_p)
13692 break;
13693 /* If we find one of the tokens we want, build the correspoding
13694 tree representation. */
13695 for (map_node = token_tree_map;
13696 map_node->token_type != CPP_EOF;
13697 ++map_node)
13698 if (map_node->token_type == token->type)
13699 {
13700 /* Consume the operator token. */
13701 cp_lexer_consume_token (parser->lexer);
13702 /* Parse the right-hand side of the expression. */
13703 rhs = (*fn) (parser);
13704 /* Build the binary tree node. */
13705 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13706 break;
13707 }
13708
13709 /* If the token wasn't one of the ones we want, we're done. */
13710 if (map_node->token_type == CPP_EOF)
13711 break;
13712 }
13713
13714 return lhs;
13715}
13716
13717/* Parse an optional `::' token indicating that the following name is
13718 from the global namespace. If so, PARSER->SCOPE is set to the
13719 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13720 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13721 Returns the new value of PARSER->SCOPE, if the `::' token is
13722 present, and NULL_TREE otherwise. */
13723
13724static tree
13725cp_parser_global_scope_opt (parser, current_scope_valid_p)
13726 cp_parser *parser;
13727 bool current_scope_valid_p;
13728{
13729 cp_token *token;
13730
13731 /* Peek at the next token. */
13732 token = cp_lexer_peek_token (parser->lexer);
13733 /* If we're looking at a `::' token then we're starting from the
13734 global namespace, not our current location. */
13735 if (token->type == CPP_SCOPE)
13736 {
13737 /* Consume the `::' token. */
13738 cp_lexer_consume_token (parser->lexer);
13739 /* Set the SCOPE so that we know where to start the lookup. */
13740 parser->scope = global_namespace;
13741 parser->qualifying_scope = global_namespace;
13742 parser->object_scope = NULL_TREE;
13743
13744 return parser->scope;
13745 }
13746 else if (!current_scope_valid_p)
13747 {
13748 parser->scope = NULL_TREE;
13749 parser->qualifying_scope = NULL_TREE;
13750 parser->object_scope = NULL_TREE;
13751 }
13752
13753 return NULL_TREE;
13754}
13755
13756/* Returns TRUE if the upcoming token sequence is the start of a
13757 constructor declarator. If FRIEND_P is true, the declarator is
13758 preceded by the `friend' specifier. */
13759
13760static bool
13761cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13762{
13763 bool constructor_p;
13764 tree type_decl = NULL_TREE;
13765 bool nested_name_p;
13766
13767 /* Parse tentatively; we are going to roll back all of the tokens
13768 consumed here. */
13769 cp_parser_parse_tentatively (parser);
13770 /* Assume that we are looking at a constructor declarator. */
13771 constructor_p = true;
13772 /* Look for the optional `::' operator. */
13773 cp_parser_global_scope_opt (parser,
13774 /*current_scope_valid_p=*/false);
13775 /* Look for the nested-name-specifier. */
13776 nested_name_p
13777 = (cp_parser_nested_name_specifier_opt (parser,
13778 /*typename_keyword_p=*/false,
13779 /*check_dependency_p=*/false,
13780 /*type_p=*/false)
13781 != NULL_TREE);
13782 /* Outside of a class-specifier, there must be a
13783 nested-name-specifier. */
13784 if (!nested_name_p &&
13785 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13786 || friend_p))
13787 constructor_p = false;
13788 /* If we still think that this might be a constructor-declarator,
13789 look for a class-name. */
13790 if (constructor_p)
13791 {
13792 /* If we have:
13793
13794 template <typename T> struct S { S(); }
13795 template <typename T> S<T>::S ();
13796
13797 we must recognize that the nested `S' names a class.
13798 Similarly, for:
13799
13800 template <typename T> S<T>::S<T> ();
13801
13802 we must recognize that the nested `S' names a template. */
13803 type_decl = cp_parser_class_name (parser,
13804 /*typename_keyword_p=*/false,
13805 /*template_keyword_p=*/false,
13806 /*type_p=*/false,
13807 /*check_access_p=*/false,
13808 /*check_dependency_p=*/false,
13809 /*class_head_p=*/false);
13810 /* If there was no class-name, then this is not a constructor. */
13811 constructor_p = !cp_parser_error_occurred (parser);
13812 }
13813 /* If we're still considering a constructor, we have to see a `(',
13814 to begin the parameter-declaration-clause, followed by either a
13815 `)', an `...', or a decl-specifier. We need to check for a
13816 type-specifier to avoid being fooled into thinking that:
13817
13818 S::S (f) (int);
13819
13820 is a constructor. (It is actually a function named `f' that
13821 takes one parameter (of type `int') and returns a value of type
13822 `S::S'. */
13823 if (constructor_p
13824 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13825 {
13826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13827 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13828 && !cp_parser_storage_class_specifier_opt (parser))
13829 {
13830 if (current_class_type
13831 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13832 /* The constructor for one class cannot be declared inside
13833 another. */
13834 constructor_p = false;
13835 else
13836 {
13837 tree type;
13838
13839 /* Names appearing in the type-specifier should be looked up
13840 in the scope of the class. */
13841 if (current_class_type)
13842 type = NULL_TREE;
13843 else
13844 {
13845 type = TREE_TYPE (type_decl);
13846 if (TREE_CODE (type) == TYPENAME_TYPE)
13847 type = cp_parser_resolve_typename_type (parser, type);
13848 push_scope (type);
13849 }
13850 /* Look for the type-specifier. */
13851 cp_parser_type_specifier (parser,
13852 CP_PARSER_FLAGS_NONE,
13853 /*is_friend=*/false,
13854 /*is_declarator=*/true,
13855 /*declares_class_or_enum=*/NULL,
13856 /*is_cv_qualifier=*/NULL);
13857 /* Leave the scope of the class. */
13858 if (type)
13859 pop_scope (type);
13860
13861 constructor_p = !cp_parser_error_occurred (parser);
13862 }
13863 }
13864 }
13865 else
13866 constructor_p = false;
13867 /* We did not really want to consume any tokens. */
13868 cp_parser_abort_tentative_parse (parser);
13869
13870 return constructor_p;
13871}
13872
13873/* Parse the definition of the function given by the DECL_SPECIFIERS,
13874 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
13875 they must be performed once we are in the scope of the function.
13876
13877 Returns the function defined. */
13878
13879static tree
13880cp_parser_function_definition_from_specifiers_and_declarator
13881 (parser, decl_specifiers, attributes, declarator, access_checks)
13882 cp_parser *parser;
13883 tree decl_specifiers;
13884 tree attributes;
13885 tree declarator;
13886 tree access_checks;
13887{
13888 tree fn;
13889 bool success_p;
13890
13891 /* Begin the function-definition. */
13892 success_p = begin_function_definition (decl_specifiers,
13893 attributes,
13894 declarator);
13895
13896 /* If there were names looked up in the decl-specifier-seq that we
13897 did not check, check them now. We must wait until we are in the
13898 scope of the function to perform the checks, since the function
13899 might be a friend. */
13900 cp_parser_perform_deferred_access_checks (access_checks);
13901
13902 if (!success_p)
13903 {
13904 /* If begin_function_definition didn't like the definition, skip
13905 the entire function. */
13906 error ("invalid function declaration");
13907 cp_parser_skip_to_end_of_block_or_statement (parser);
13908 fn = error_mark_node;
13909 }
13910 else
13911 fn = cp_parser_function_definition_after_declarator (parser,
13912 /*inline_p=*/false);
13913
13914 return fn;
13915}
13916
13917/* Parse the part of a function-definition that follows the
13918 declarator. INLINE_P is TRUE iff this function is an inline
13919 function defined with a class-specifier.
13920
13921 Returns the function defined. */
13922
13923static tree
13924cp_parser_function_definition_after_declarator (parser,
13925 inline_p)
13926 cp_parser *parser;
13927 bool inline_p;
13928{
13929 tree fn;
13930 bool ctor_initializer_p = false;
13931 bool saved_in_unbraced_linkage_specification_p;
13932 unsigned saved_num_template_parameter_lists;
13933
13934 /* If the next token is `return', then the code may be trying to
13935 make use of the "named return value" extension that G++ used to
13936 support. */
13937 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13938 {
13939 /* Consume the `return' keyword. */
13940 cp_lexer_consume_token (parser->lexer);
13941 /* Look for the identifier that indicates what value is to be
13942 returned. */
13943 cp_parser_identifier (parser);
13944 /* Issue an error message. */
13945 error ("named return values are no longer supported");
13946 /* Skip tokens until we reach the start of the function body. */
13947 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13948 cp_lexer_consume_token (parser->lexer);
13949 }
13950 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13951 anything declared inside `f'. */
13952 saved_in_unbraced_linkage_specification_p
13953 = parser->in_unbraced_linkage_specification_p;
13954 parser->in_unbraced_linkage_specification_p = false;
13955 /* Inside the function, surrounding template-parameter-lists do not
13956 apply. */
13957 saved_num_template_parameter_lists
13958 = parser->num_template_parameter_lists;
13959 parser->num_template_parameter_lists = 0;
13960 /* If the next token is `try', then we are looking at a
13961 function-try-block. */
13962 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13963 ctor_initializer_p = cp_parser_function_try_block (parser);
13964 /* A function-try-block includes the function-body, so we only do
13965 this next part if we're not processing a function-try-block. */
13966 else
13967 ctor_initializer_p
13968 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13969
13970 /* Finish the function. */
13971 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13972 (inline_p ? 2 : 0));
13973 /* Generate code for it, if necessary. */
13974 expand_body (fn);
13975 /* Restore the saved values. */
13976 parser->in_unbraced_linkage_specification_p
13977 = saved_in_unbraced_linkage_specification_p;
13978 parser->num_template_parameter_lists
13979 = saved_num_template_parameter_lists;
13980
13981 return fn;
13982}
13983
13984/* Parse a template-declaration, assuming that the `export' (and
13985 `extern') keywords, if present, has already been scanned. MEMBER_P
13986 is as for cp_parser_template_declaration. */
13987
13988static void
13989cp_parser_template_declaration_after_export (parser, member_p)
13990 cp_parser *parser;
13991 bool member_p;
13992{
13993 tree decl = NULL_TREE;
13994 tree parameter_list;
13995 bool friend_p = false;
13996
13997 /* Look for the `template' keyword. */
13998 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13999 return;
14000
14001 /* And the `<'. */
14002 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14003 return;
14004
14005 /* Parse the template parameters. */
14006 begin_template_parm_list ();
14007 /* If the next token is `>', then we have an invalid
14008 specialization. Rather than complain about an invalid template
14009 parameter, issue an error message here. */
14010 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14011 {
14012 cp_parser_error (parser, "invalid explicit specialization");
14013 parameter_list = NULL_TREE;
14014 }
14015 else
14016 parameter_list = cp_parser_template_parameter_list (parser);
14017 parameter_list = end_template_parm_list (parameter_list);
14018 /* Look for the `>'. */
14019 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14020 /* We just processed one more parameter list. */
14021 ++parser->num_template_parameter_lists;
14022 /* If the next token is `template', there are more template
14023 parameters. */
14024 if (cp_lexer_next_token_is_keyword (parser->lexer,
14025 RID_TEMPLATE))
14026 cp_parser_template_declaration_after_export (parser, member_p);
14027 else
14028 {
14029 decl = cp_parser_single_declaration (parser,
14030 member_p,
14031 &friend_p);
14032
14033 /* If this is a member template declaration, let the front
14034 end know. */
14035 if (member_p && !friend_p && decl)
14036 decl = finish_member_template_decl (decl);
14037 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14038 make_friend_class (current_class_type, TREE_TYPE (decl));
14039 }
14040 /* We are done with the current parameter list. */
14041 --parser->num_template_parameter_lists;
14042
14043 /* Finish up. */
14044 finish_template_decl (parameter_list);
14045
14046 /* Register member declarations. */
14047 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14048 finish_member_declaration (decl);
14049
14050 /* If DECL is a function template, we must return to parse it later.
14051 (Even though there is no definition, there might be default
14052 arguments that need handling.) */
14053 if (member_p && decl
14054 && (TREE_CODE (decl) == FUNCTION_DECL
14055 || DECL_FUNCTION_TEMPLATE_P (decl)))
14056 TREE_VALUE (parser->unparsed_functions_queues)
14057 = tree_cons (current_class_type, decl,
14058 TREE_VALUE (parser->unparsed_functions_queues));
14059}
14060
14061/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14062 `function-definition' sequence. MEMBER_P is true, this declaration
14063 appears in a class scope.
14064
14065 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14066 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14067
14068static tree
14069cp_parser_single_declaration (parser,
14070 member_p,
14071 friend_p)
14072 cp_parser *parser;
14073 bool member_p;
14074 bool *friend_p;
14075{
14076 bool declares_class_or_enum;
14077 tree decl = NULL_TREE;
14078 tree decl_specifiers;
14079 tree attributes;
14080 tree access_checks;
14081
14082 /* Parse the dependent declaration. We don't know yet
14083 whether it will be a function-definition. */
14084 cp_parser_parse_tentatively (parser);
14085 /* Defer access checks until we know what is being declared. */
14086 cp_parser_start_deferring_access_checks (parser);
14087 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14088 alternative. */
14089 decl_specifiers
14090 = cp_parser_decl_specifier_seq (parser,
14091 CP_PARSER_FLAGS_OPTIONAL,
14092 &attributes,
14093 &declares_class_or_enum);
14094 /* Gather up the access checks that occurred the
14095 decl-specifier-seq. */
14096 access_checks = cp_parser_stop_deferring_access_checks (parser);
14097 /* Check for the declaration of a template class. */
14098 if (declares_class_or_enum)
14099 {
14100 if (cp_parser_declares_only_class_p (parser))
14101 {
14102 decl = shadow_tag (decl_specifiers);
14103 if (decl)
14104 decl = TYPE_NAME (decl);
14105 else
14106 decl = error_mark_node;
14107 }
14108 }
14109 else
14110 decl = NULL_TREE;
14111 /* If it's not a template class, try for a template function. If
14112 the next token is a `;', then this declaration does not declare
14113 anything. But, if there were errors in the decl-specifiers, then
14114 the error might well have come from an attempted class-specifier.
14115 In that case, there's no need to warn about a missing declarator. */
14116 if (!decl
14117 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14118 || !value_member (error_mark_node, decl_specifiers)))
14119 decl = cp_parser_init_declarator (parser,
14120 decl_specifiers,
14121 attributes,
14122 access_checks,
14123 /*function_definition_allowed_p=*/false,
14124 member_p,
14125 /*function_definition_p=*/NULL);
14126 /* Clear any current qualification; whatever comes next is the start
14127 of something new. */
14128 parser->scope = NULL_TREE;
14129 parser->qualifying_scope = NULL_TREE;
14130 parser->object_scope = NULL_TREE;
14131 /* Look for a trailing `;' after the declaration. */
14132 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14133 && cp_parser_committed_to_tentative_parse (parser))
14134 cp_parser_skip_to_end_of_block_or_statement (parser);
14135 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14136 if (cp_parser_parse_definitely (parser))
14137 {
14138 if (friend_p)
14139 *friend_p = cp_parser_friend_p (decl_specifiers);
14140 }
14141 /* Otherwise, try a function-definition. */
14142 else
14143 decl = cp_parser_function_definition (parser, friend_p);
14144
14145 return decl;
14146}
14147
14148/* Parse a functional cast to TYPE. Returns an expression
14149 representing the cast. */
14150
14151static tree
14152cp_parser_functional_cast (parser, type)
14153 cp_parser *parser;
14154 tree type;
14155{
14156 tree expression_list;
14157
14158 /* Look for the opening `('. */
14159 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14160 return error_mark_node;
14161 /* If the next token is not an `)', there are arguments to the
14162 cast. */
14163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14164 expression_list = cp_parser_expression_list (parser);
14165 else
14166 expression_list = NULL_TREE;
14167 /* Look for the closing `)'. */
14168 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14169
14170 return build_functional_cast (type, expression_list);
14171}
14172
14173/* MEMBER_FUNCTION is a member function, or a friend. If default
14174 arguments, or the body of the function have not yet been parsed,
14175 parse them now. */
14176
14177static void
14178cp_parser_late_parsing_for_member (parser, member_function)
14179 cp_parser *parser;
14180 tree member_function;
14181{
14182 cp_lexer *saved_lexer;
14183
14184 /* If this member is a template, get the underlying
14185 FUNCTION_DECL. */
14186 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14187 member_function = DECL_TEMPLATE_RESULT (member_function);
14188
14189 /* There should not be any class definitions in progress at this
14190 point; the bodies of members are only parsed outside of all class
14191 definitions. */
14192 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14193 /* While we're parsing the member functions we might encounter more
14194 classes. We want to handle them right away, but we don't want
14195 them getting mixed up with functions that are currently in the
14196 queue. */
14197 parser->unparsed_functions_queues
14198 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14199
14200 /* Make sure that any template parameters are in scope. */
14201 maybe_begin_member_template_processing (member_function);
14202
14203 /* If there are default arguments that have not yet been processed,
14204 take care of them now. */
14205 if (DECL_FUNCTION_MEMBER_P (member_function))
14206 push_nested_class (DECL_CONTEXT (member_function), 1);
14207 cp_parser_late_parsing_default_args (parser, TREE_TYPE (member_function));
14208 if (DECL_FUNCTION_MEMBER_P (member_function))
14209 pop_nested_class ();
14210
14211 /* If the body of the function has not yet been parsed, parse it
14212 now. */
14213 if (DECL_PENDING_INLINE_P (member_function))
14214 {
14215 tree function_scope;
14216 cp_token_cache *tokens;
14217
14218 /* The function is no longer pending; we are processing it. */
14219 tokens = DECL_PENDING_INLINE_INFO (member_function);
14220 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14221 DECL_PENDING_INLINE_P (member_function) = 0;
14222 /* If this was an inline function in a local class, enter the scope
14223 of the containing function. */
14224 function_scope = decl_function_context (member_function);
14225 if (function_scope)
14226 push_function_context_to (function_scope);
14227
14228 /* Save away the current lexer. */
14229 saved_lexer = parser->lexer;
14230 /* Make a new lexer to feed us the tokens saved for this function. */
14231 parser->lexer = cp_lexer_new_from_tokens (tokens);
14232 parser->lexer->next = saved_lexer;
14233
14234 /* Set the current source position to be the location of the first
14235 token in the saved inline body. */
14236 cp_lexer_set_source_position_from_token
14237 (parser->lexer,
14238 cp_lexer_peek_token (parser->lexer));
14239
14240 /* Let the front end know that we going to be defining this
14241 function. */
14242 start_function (NULL_TREE, member_function, NULL_TREE,
14243 SF_PRE_PARSED | SF_INCLASS_INLINE);
14244
14245 /* Now, parse the body of the function. */
14246 cp_parser_function_definition_after_declarator (parser,
14247 /*inline_p=*/true);
14248
14249 /* Leave the scope of the containing function. */
14250 if (function_scope)
14251 pop_function_context_from (function_scope);
14252 /* Restore the lexer. */
14253 parser->lexer = saved_lexer;
14254 }
14255
14256 /* Remove any template parameters from the symbol table. */
14257 maybe_end_member_template_processing ();
14258
14259 /* Restore the queue. */
14260 parser->unparsed_functions_queues
14261 = TREE_CHAIN (parser->unparsed_functions_queues);
14262}
14263
14264/* TYPE is a FUNCTION_TYPE or METHOD_TYPE which contains a parameter
14265 with an unparsed DEFAULT_ARG. Parse those default args now. */
14266
14267static void
14268cp_parser_late_parsing_default_args (parser, type)
14269 cp_parser *parser;
14270 tree type;
14271{
14272 cp_lexer *saved_lexer;
14273 cp_token_cache *tokens;
14274 bool saved_local_variables_forbidden_p;
14275 tree parameters;
14276
14277 for (parameters = TYPE_ARG_TYPES (type);
14278 parameters;
14279 parameters = TREE_CHAIN (parameters))
14280 {
14281 if (!TREE_PURPOSE (parameters)
14282 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14283 continue;
14284
14285 /* Save away the current lexer. */
14286 saved_lexer = parser->lexer;
14287 /* Create a new one, using the tokens we have saved. */
14288 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14289 parser->lexer = cp_lexer_new_from_tokens (tokens);
14290
14291 /* Set the current source position to be the location of the
14292 first token in the default argument. */
14293 cp_lexer_set_source_position_from_token
14294 (parser->lexer, cp_lexer_peek_token (parser->lexer));
14295
14296 /* Local variable names (and the `this' keyword) may not appear
14297 in a default argument. */
14298 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14299 parser->local_variables_forbidden_p = true;
14300 /* Parse the assignment-expression. */
14301 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14302
14303 /* Restore saved state. */
14304 parser->lexer = saved_lexer;
14305 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14306 }
14307}
14308
14309/* Parse the operand of `sizeof' (or a similar operator). Returns
14310 either a TYPE or an expression, depending on the form of the
14311 input. The KEYWORD indicates which kind of expression we have
14312 encountered. */
14313
14314static tree
14315cp_parser_sizeof_operand (parser, keyword)
14316 cp_parser *parser;
14317 enum rid keyword;
14318{
14319 static const char *format;
14320 tree expr = NULL_TREE;
14321 const char *saved_message;
14322 bool saved_constant_expression_p;
14323
14324 /* Initialize FORMAT the first time we get here. */
14325 if (!format)
14326 format = "types may not be defined in `%s' expressions";
14327
14328 /* Types cannot be defined in a `sizeof' expression. Save away the
14329 old message. */
14330 saved_message = parser->type_definition_forbidden_message;
14331 /* And create the new one. */
14332 parser->type_definition_forbidden_message
14333 = ((const char *)
14334 xmalloc (strlen (format)
14335 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14336 + 1 /* `\0' */));
14337 sprintf ((char *) parser->type_definition_forbidden_message,
14338 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14339
14340 /* The restrictions on constant-expressions do not apply inside
14341 sizeof expressions. */
14342 saved_constant_expression_p = parser->constant_expression_p;
14343 parser->constant_expression_p = false;
14344
14345 /* If it's a `(', then we might be looking at the type-id
14346 construction. */
14347 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14348 {
14349 tree type;
14350
14351 /* We can't be sure yet whether we're looking at a type-id or an
14352 expression. */
14353 cp_parser_parse_tentatively (parser);
14354 /* Consume the `('. */
14355 cp_lexer_consume_token (parser->lexer);
14356 /* Parse the type-id. */
14357 type = cp_parser_type_id (parser);
14358 /* Now, look for the trailing `)'. */
14359 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14360 /* If all went well, then we're done. */
14361 if (cp_parser_parse_definitely (parser))
14362 {
14363 /* Build a list of decl-specifiers; right now, we have only
14364 a single type-specifier. */
14365 type = build_tree_list (NULL_TREE,
14366 type);
14367
14368 /* Call grokdeclarator to figure out what type this is. */
14369 expr = grokdeclarator (NULL_TREE,
14370 type,
14371 TYPENAME,
14372 /*initialized=*/0,
14373 /*attrlist=*/NULL);
14374 }
14375 }
14376
14377 /* If the type-id production did not work out, then we must be
14378 looking at the unary-expression production. */
14379 if (!expr)
14380 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14381
14382 /* Free the message we created. */
14383 free ((char *) parser->type_definition_forbidden_message);
14384 /* And restore the old one. */
14385 parser->type_definition_forbidden_message = saved_message;
14386 parser->constant_expression_p = saved_constant_expression_p;
14387
14388 return expr;
14389}
14390
14391/* If the current declaration has no declarator, return true. */
14392
14393static bool
14394cp_parser_declares_only_class_p (cp_parser *parser)
14395{
14396 /* If the next token is a `;' or a `,' then there is no
14397 declarator. */
14398 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14399 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14400}
14401
14402/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14403 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14404
14405static bool
14406cp_parser_friend_p (decl_specifiers)
14407 tree decl_specifiers;
14408{
14409 while (decl_specifiers)
14410 {
14411 /* See if this decl-specifier is `friend'. */
14412 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14413 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14414 return true;
14415
14416 /* Go on to the next decl-specifier. */
14417 decl_specifiers = TREE_CHAIN (decl_specifiers);
14418 }
14419
14420 return false;
14421}
14422
14423/* If the next token is of the indicated TYPE, consume it. Otherwise,
14424 issue an error message indicating that TOKEN_DESC was expected.
14425
14426 Returns the token consumed, if the token had the appropriate type.
14427 Otherwise, returns NULL. */
14428
14429static cp_token *
14430cp_parser_require (parser, type, token_desc)
14431 cp_parser *parser;
14432 enum cpp_ttype type;
14433 const char *token_desc;
14434{
14435 if (cp_lexer_next_token_is (parser->lexer, type))
14436 return cp_lexer_consume_token (parser->lexer);
14437 else
14438 {
14439 dyn_string_t error_msg;
14440
14441 /* Format the error message. */
14442 error_msg = dyn_string_new (0);
14443 dyn_string_append_cstr (error_msg, "expected ");
14444 dyn_string_append_cstr (error_msg, token_desc);
14445 cp_parser_error (parser, error_msg->s);
14446 dyn_string_delete (error_msg);
14447 return NULL;
14448 }
14449}
14450
14451/* Like cp_parser_require, except that tokens will be skipped until
14452 the desired token is found. An error message is still produced if
14453 the next token is not as expected. */
14454
14455static void
14456cp_parser_skip_until_found (parser, type, token_desc)
14457 cp_parser *parser;
14458 enum cpp_ttype type;
14459 const char *token_desc;
14460{
14461 cp_token *token;
14462 unsigned nesting_depth = 0;
14463
14464 if (cp_parser_require (parser, type, token_desc))
14465 return;
14466
14467 /* Skip tokens until the desired token is found. */
14468 while (true)
14469 {
14470 /* Peek at the next token. */
14471 token = cp_lexer_peek_token (parser->lexer);
14472 /* If we've reached the token we want, consume it and
14473 stop. */
14474 if (token->type == type && !nesting_depth)
14475 {
14476 cp_lexer_consume_token (parser->lexer);
14477 return;
14478 }
14479 /* If we've run out of tokens, stop. */
14480 if (token->type == CPP_EOF)
14481 return;
14482 if (token->type == CPP_OPEN_BRACE
14483 || token->type == CPP_OPEN_PAREN
14484 || token->type == CPP_OPEN_SQUARE)
14485 ++nesting_depth;
14486 else if (token->type == CPP_CLOSE_BRACE
14487 || token->type == CPP_CLOSE_PAREN
14488 || token->type == CPP_CLOSE_SQUARE)
14489 {
14490 if (nesting_depth-- == 0)
14491 return;
14492 }
14493 /* Consume this token. */
14494 cp_lexer_consume_token (parser->lexer);
14495 }
14496}
14497
14498/* If the next token is the indicated keyword, consume it. Otherwise,
14499 issue an error message indicating that TOKEN_DESC was expected.
14500
14501 Returns the token consumed, if the token had the appropriate type.
14502 Otherwise, returns NULL. */
14503
14504static cp_token *
14505cp_parser_require_keyword (parser, keyword, token_desc)
14506 cp_parser *parser;
14507 enum rid keyword;
14508 const char *token_desc;
14509{
14510 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14511
14512 if (token && token->keyword != keyword)
14513 {
14514 dyn_string_t error_msg;
14515
14516 /* Format the error message. */
14517 error_msg = dyn_string_new (0);
14518 dyn_string_append_cstr (error_msg, "expected ");
14519 dyn_string_append_cstr (error_msg, token_desc);
14520 cp_parser_error (parser, error_msg->s);
14521 dyn_string_delete (error_msg);
14522 return NULL;
14523 }
14524
14525 return token;
14526}
14527
14528/* Returns TRUE iff TOKEN is a token that can begin the body of a
14529 function-definition. */
14530
14531static bool
14532cp_parser_token_starts_function_definition_p (token)
14533 cp_token *token;
14534{
14535 return (/* An ordinary function-body begins with an `{'. */
14536 token->type == CPP_OPEN_BRACE
14537 /* A ctor-initializer begins with a `:'. */
14538 || token->type == CPP_COLON
14539 /* A function-try-block begins with `try'. */
14540 || token->keyword == RID_TRY
14541 /* The named return value extension begins with `return'. */
14542 || token->keyword == RID_RETURN);
14543}
14544
14545/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14546 definition. */
14547
14548static bool
14549cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14550{
14551 cp_token *token;
14552
14553 token = cp_lexer_peek_token (parser->lexer);
14554 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14555}
14556
14557/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14558 or none_type otherwise. */
14559
14560static enum tag_types
14561cp_parser_token_is_class_key (token)
14562 cp_token *token;
14563{
14564 switch (token->keyword)
14565 {
14566 case RID_CLASS:
14567 return class_type;
14568 case RID_STRUCT:
14569 return record_type;
14570 case RID_UNION:
14571 return union_type;
14572
14573 default:
14574 return none_type;
14575 }
14576}
14577
14578/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14579
14580static void
14581cp_parser_check_class_key (enum tag_types class_key, tree type)
14582{
14583 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14584 pedwarn ("`%s' tag used in naming `%#T'",
14585 class_key == union_type ? "union"
14586 : class_key == record_type ? "struct" : "class",
14587 type);
14588}
14589
14590/* Look for the `template' keyword, as a syntactic disambiguator.
14591 Return TRUE iff it is present, in which case it will be
14592 consumed. */
14593
14594static bool
14595cp_parser_optional_template_keyword (cp_parser *parser)
14596{
14597 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14598 {
14599 /* The `template' keyword can only be used within templates;
14600 outside templates the parser can always figure out what is a
14601 template and what is not. */
14602 if (!processing_template_decl)
14603 {
14604 error ("`template' (as a disambiguator) is only allowed "
14605 "within templates");
14606 /* If this part of the token stream is rescanned, the same
14607 error message would be generated. So, we purge the token
14608 from the stream. */
14609 cp_lexer_purge_token (parser->lexer);
14610 return false;
14611 }
14612 else
14613 {
14614 /* Consume the `template' keyword. */
14615 cp_lexer_consume_token (parser->lexer);
14616 return true;
14617 }
14618 }
14619
14620 return false;
14621}
14622
14623/* Add tokens to CACHE until an non-nested END token appears. */
14624
14625static void
14626cp_parser_cache_group (cp_parser *parser,
14627 cp_token_cache *cache,
14628 enum cpp_ttype end,
14629 unsigned depth)
14630{
14631 while (true)
14632 {
14633 cp_token *token;
14634
14635 /* Abort a parenthesized expression if we encounter a brace. */
14636 if ((end == CPP_CLOSE_PAREN || depth == 0)
14637 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14638 return;
14639 /* Consume the next token. */
14640 token = cp_lexer_consume_token (parser->lexer);
14641 /* If we've reached the end of the file, stop. */
14642 if (token->type == CPP_EOF)
14643 return;
14644 /* Add this token to the tokens we are saving. */
14645 cp_token_cache_push_token (cache, token);
14646 /* See if it starts a new group. */
14647 if (token->type == CPP_OPEN_BRACE)
14648 {
14649 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14650 if (depth == 0)
14651 return;
14652 }
14653 else if (token->type == CPP_OPEN_PAREN)
14654 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14655 else if (token->type == end)
14656 return;
14657 }
14658}
14659
14660/* Begin parsing tentatively. We always save tokens while parsing
14661 tentatively so that if the tentative parsing fails we can restore the
14662 tokens. */
14663
14664static void
14665cp_parser_parse_tentatively (parser)
14666 cp_parser *parser;
14667{
14668 /* Enter a new parsing context. */
14669 parser->context = cp_parser_context_new (parser->context);
14670 /* Begin saving tokens. */
14671 cp_lexer_save_tokens (parser->lexer);
14672 /* In order to avoid repetitive access control error messages,
14673 access checks are queued up until we are no longer parsing
14674 tentatively. */
14675 cp_parser_start_deferring_access_checks (parser);
14676}
14677
14678/* Commit to the currently active tentative parse. */
14679
14680static void
14681cp_parser_commit_to_tentative_parse (parser)
14682 cp_parser *parser;
14683{
14684 cp_parser_context *context;
14685 cp_lexer *lexer;
14686
14687 /* Mark all of the levels as committed. */
14688 lexer = parser->lexer;
14689 for (context = parser->context; context->next; context = context->next)
14690 {
14691 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14692 break;
14693 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14694 while (!cp_lexer_saving_tokens (lexer))
14695 lexer = lexer->next;
14696 cp_lexer_commit_tokens (lexer);
14697 }
14698}
14699
14700/* Abort the currently active tentative parse. All consumed tokens
14701 will be rolled back, and no diagnostics will be issued. */
14702
14703static void
14704cp_parser_abort_tentative_parse (parser)
14705 cp_parser *parser;
14706{
14707 cp_parser_simulate_error (parser);
14708 /* Now, pretend that we want to see if the construct was
14709 successfully parsed. */
14710 cp_parser_parse_definitely (parser);
14711}
14712
14713/* Stop parsing tentatively. If a parse error has ocurred, restore the
14714 token stream. Otherwise, commit to the tokens we have consumed.
14715 Returns true if no error occurred; false otherwise. */
14716
14717static bool
14718cp_parser_parse_definitely (parser)
14719 cp_parser *parser;
14720{
14721 bool error_occurred;
14722 cp_parser_context *context;
14723
14724 /* Remember whether or not an error ocurred, since we are about to
14725 destroy that information. */
14726 error_occurred = cp_parser_error_occurred (parser);
14727 /* Remove the topmost context from the stack. */
14728 context = parser->context;
14729 parser->context = context->next;
14730 /* If no parse errors occurred, commit to the tentative parse. */
14731 if (!error_occurred)
14732 {
14733 /* Commit to the tokens read tentatively, unless that was
14734 already done. */
14735 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14736 cp_lexer_commit_tokens (parser->lexer);
14737 if (!parser->context->deferring_access_checks_p)
14738 /* If in the parent context we are not deferring checks, then
14739 these perform these checks now. */
14740 (cp_parser_perform_deferred_access_checks
14741 (context->deferred_access_checks));
14742 else
14743 /* Any lookups that were deferred during the tentative parse are
14744 still deferred. */
14745 parser->context->deferred_access_checks
14746 = chainon (parser->context->deferred_access_checks,
14747 context->deferred_access_checks);
14748 return true;
14749 }
14750 /* Otherwise, if errors occurred, roll back our state so that things
14751 are just as they were before we began the tentative parse. */
14752 else
14753 {
14754 cp_lexer_rollback_tokens (parser->lexer);
14755 return false;
14756 }
14757}
14758
14759/* Returns non-zero if we are parsing tentatively. */
14760
14761static bool
14762cp_parser_parsing_tentatively (parser)
14763 cp_parser *parser;
14764{
14765 return parser->context->next != NULL;
14766}
14767
14768/* Returns true if we are parsing tentatively -- but have decided that
14769 we will stick with this tentative parse, even if errors occur. */
14770
14771static bool
14772cp_parser_committed_to_tentative_parse (parser)
14773 cp_parser *parser;
14774{
14775 return (cp_parser_parsing_tentatively (parser)
14776 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14777}
14778
14779/* Returns non-zero iff an error has occurred during the most recent
14780 tentative parse. */
14781
14782static bool
14783cp_parser_error_occurred (parser)
14784 cp_parser *parser;
14785{
14786 return (cp_parser_parsing_tentatively (parser)
14787 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14788}
14789
14790/* Returns non-zero if GNU extensions are allowed. */
14791
14792static bool
14793cp_parser_allow_gnu_extensions_p (parser)
14794 cp_parser *parser;
14795{
14796 return parser->allow_gnu_extensions_p;
14797}
14798
14799\f
14800
14801/* The parser. */
14802
14803static GTY (()) cp_parser *the_parser;
14804
14805/* External interface. */
14806
14807/* Parse the entire translation unit. */
14808
14809int
14810yyparse ()
14811{
14812 bool error_occurred;
14813
14814 the_parser = cp_parser_new ();
14815 error_occurred = cp_parser_translation_unit (the_parser);
14816 the_parser = NULL;
14817
14818 return error_occurred;
14819}
14820
14821/* Clean up after parsing the entire translation unit. */
14822
14823void
14824free_parser_stacks ()
14825{
14826 /* Nothing to do. */
14827}
14828
14829/* This variable must be provided by every front end. */
14830
14831int yydebug;
14832
14833#include "gt-cp-parser.h"