]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
* g++.dg/parse/parse6.C: New test.
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
0a3b29ad 1/* C++ Parser.
4c99a080 2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
0a3b29ad 3 Written by Mark Mitchell <mark@codesourcery.com>.
4
6f0d25a6 5 This file is part of GCC.
0a3b29ad 6
6f0d25a6 7 GCC is free software; you can redistribute it and/or modify it
0a3b29ad 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
6f0d25a6 12 GCC is distributed in the hope that it will be useful, but
0a3b29ad 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
6f0d25a6 18 along with GCC; see the file COPYING. If not, write to the Free
0a3b29ad 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
2c593bd0 1199/* Class variables. */
1200
6e9029b4 1201static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
2c593bd0 1202
0a3b29ad 1203/* Constructors and destructors. */
1204
1205/* Construct a new context. The context below this one on the stack
1206 is given by NEXT. */
1207
1208static cp_parser_context *
1209cp_parser_context_new (next)
1210 cp_parser_context *next;
1211{
1212 cp_parser_context *context;
1213
1214 /* Allocate the storage. */
2c593bd0 1215 if (cp_parser_context_free_list != NULL)
1216 {
1217 /* Pull the first entry from the free list. */
1218 context = cp_parser_context_free_list;
1219 cp_parser_context_free_list = context->next;
1220 memset ((char *)context, 0, sizeof (*context));
1221 }
1222 else
1223 context = ((cp_parser_context *)
1224 ggc_alloc_cleared (sizeof (cp_parser_context)));
0a3b29ad 1225 /* No errors have occurred yet in this context. */
1226 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1227 /* If this is not the bottomost context, copy information that we
1228 need from the previous context. */
1229 if (next)
1230 {
1231 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1232 expression, then we are parsing one in this context, too. */
1233 context->object_type = next->object_type;
1234 /* We are deferring access checks here if we were in the NEXT
1235 context. */
1236 context->deferring_access_checks_p
1237 = next->deferring_access_checks_p;
1238 /* Thread the stack. */
1239 context->next = next;
1240 }
1241
1242 return context;
1243}
1244
1245/* The cp_parser structure represents the C++ parser. */
1246
1247typedef struct cp_parser GTY(())
1248{
1249 /* The lexer from which we are obtaining tokens. */
1250 cp_lexer *lexer;
1251
1252 /* The scope in which names should be looked up. If NULL_TREE, then
1253 we look up names in the scope that is currently open in the
1254 source program. If non-NULL, this is either a TYPE or
1255 NAMESPACE_DECL for the scope in which we should look.
1256
1257 This value is not cleared automatically after a name is looked
1258 up, so we must be careful to clear it before starting a new look
1259 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1260 will look up `Z' in the scope of `X', rather than the current
1261 scope.) Unfortunately, it is difficult to tell when name lookup
1262 is complete, because we sometimes peek at a token, look it up,
1263 and then decide not to consume it. */
1264 tree scope;
1265
1266 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1267 last lookup took place. OBJECT_SCOPE is used if an expression
1268 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1269 respectively. QUALIFYING_SCOPE is used for an expression of the
1270 form "X::Y"; it refers to X. */
1271 tree object_scope;
1272 tree qualifying_scope;
1273
1274 /* A stack of parsing contexts. All but the bottom entry on the
1275 stack will be tentative contexts.
1276
1277 We parse tentatively in order to determine which construct is in
1278 use in some situations. For example, in order to determine
1279 whether a statement is an expression-statement or a
1280 declaration-statement we parse it tentatively as a
1281 declaration-statement. If that fails, we then reparse the same
1282 token stream as an expression-statement. */
1283 cp_parser_context *context;
1284
1285 /* True if we are parsing GNU C++. If this flag is not set, then
1286 GNU extensions are not recognized. */
1287 bool allow_gnu_extensions_p;
1288
1289 /* TRUE if the `>' token should be interpreted as the greater-than
1290 operator. FALSE if it is the end of a template-id or
1291 template-parameter-list. */
1292 bool greater_than_is_operator_p;
1293
1294 /* TRUE if default arguments are allowed within a parameter list
1295 that starts at this point. FALSE if only a gnu extension makes
1296 them permissable. */
1297 bool default_arg_ok_p;
1298
1299 /* TRUE if we are parsing an integral constant-expression. See
1300 [expr.const] for a precise definition. */
1301 /* FIXME: Need to implement code that checks this flag. */
1302 bool constant_expression_p;
1303
1304 /* TRUE if local variable names and `this' are forbidden in the
1305 current context. */
1306 bool local_variables_forbidden_p;
1307
1308 /* TRUE if the declaration we are parsing is part of a
1309 linkage-specification of the form `extern string-literal
1310 declaration'. */
1311 bool in_unbraced_linkage_specification_p;
1312
1313 /* TRUE if we are presently parsing a declarator, after the
1314 direct-declarator. */
1315 bool in_declarator_p;
1316
1317 /* If non-NULL, then we are parsing a construct where new type
1318 definitions are not permitted. The string stored here will be
1319 issued as an error message if a type is defined. */
1320 const char *type_definition_forbidden_message;
1321
1322 /* List of FUNCTION_TYPEs which contain unprocessed DEFAULT_ARGs
1323 during class parsing, and are not FUNCTION_DECLs. G++ has an
1324 awkward extension allowing default args on pointers to functions
1325 etc. */
1326 tree default_arg_types;
1327
1328 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1329 but may not have been parsed. These functions are friends of
1330 members defined within a class-specification; they are not
1331 procssed until the class is complete. The active queue is at the
1332 front of the list.
1333
1334 Within each queue, functions appear in the reverse order that
1335 they appeared in the source. The TREE_PURPOSE of each node is
1336 the class in which the function was defined or declared; the
1337 TREE_VALUE is the FUNCTION_DECL itself. */
1338 tree unparsed_functions_queues;
1339
1340 /* The number of classes whose definitions are currently in
1341 progress. */
1342 unsigned num_classes_being_defined;
1343
1344 /* The number of template parameter lists that apply directly to the
1345 current declaration. */
1346 unsigned num_template_parameter_lists;
1347} cp_parser;
1348
1349/* The type of a function that parses some kind of expression */
1350typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1351
1352/* Prototypes. */
1353
1354/* Constructors and destructors. */
1355
1356static cp_parser *cp_parser_new
1357 PARAMS ((void));
1358
1359/* Routines to parse various constructs.
1360
1361 Those that return `tree' will return the error_mark_node (rather
1362 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1363 Sometimes, they will return an ordinary node if error-recovery was
1364 attempted, even though a parse error occurrred. So, to check
1365 whether or not a parse error occurred, you should always use
1366 cp_parser_error_occurred. If the construct is optional (indicated
1367 either by an `_opt' in the name of the function that does the
1368 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1369 the construct is not present. */
1370
1371/* Lexical conventions [gram.lex] */
1372
1373static tree cp_parser_identifier
1374 PARAMS ((cp_parser *));
1375
1376/* Basic concepts [gram.basic] */
1377
1378static bool cp_parser_translation_unit
1379 PARAMS ((cp_parser *));
1380
1381/* Expressions [gram.expr] */
1382
1383static tree cp_parser_primary_expression
1384 (cp_parser *, cp_parser_id_kind *, tree *);
1385static tree cp_parser_id_expression
1386 PARAMS ((cp_parser *, bool, bool, bool *));
1387static tree cp_parser_unqualified_id
1388 PARAMS ((cp_parser *, bool, bool));
1389static tree cp_parser_nested_name_specifier_opt
1390 (cp_parser *, bool, bool, bool);
1391static tree cp_parser_nested_name_specifier
1392 (cp_parser *, bool, bool, bool);
1393static tree cp_parser_class_or_namespace_name
1394 (cp_parser *, bool, bool, bool, bool);
1395static tree cp_parser_postfix_expression
1396 (cp_parser *, bool);
1397static tree cp_parser_expression_list
1398 PARAMS ((cp_parser *));
1399static void cp_parser_pseudo_destructor_name
1400 PARAMS ((cp_parser *, tree *, tree *));
1401static tree cp_parser_unary_expression
1402 (cp_parser *, bool);
1403static enum tree_code cp_parser_unary_operator
1404 PARAMS ((cp_token *));
1405static tree cp_parser_new_expression
1406 PARAMS ((cp_parser *));
1407static tree cp_parser_new_placement
1408 PARAMS ((cp_parser *));
1409static tree cp_parser_new_type_id
1410 PARAMS ((cp_parser *));
1411static tree cp_parser_new_declarator_opt
1412 PARAMS ((cp_parser *));
1413static tree cp_parser_direct_new_declarator
1414 PARAMS ((cp_parser *));
1415static tree cp_parser_new_initializer
1416 PARAMS ((cp_parser *));
1417static tree cp_parser_delete_expression
1418 PARAMS ((cp_parser *));
1419static tree cp_parser_cast_expression
1420 (cp_parser *, bool);
1421static tree cp_parser_pm_expression
1422 PARAMS ((cp_parser *));
1423static tree cp_parser_multiplicative_expression
1424 PARAMS ((cp_parser *));
1425static tree cp_parser_additive_expression
1426 PARAMS ((cp_parser *));
1427static tree cp_parser_shift_expression
1428 PARAMS ((cp_parser *));
1429static tree cp_parser_relational_expression
1430 PARAMS ((cp_parser *));
1431static tree cp_parser_equality_expression
1432 PARAMS ((cp_parser *));
1433static tree cp_parser_and_expression
1434 PARAMS ((cp_parser *));
1435static tree cp_parser_exclusive_or_expression
1436 PARAMS ((cp_parser *));
1437static tree cp_parser_inclusive_or_expression
1438 PARAMS ((cp_parser *));
1439static tree cp_parser_logical_and_expression
1440 PARAMS ((cp_parser *));
1441static tree cp_parser_logical_or_expression
1442 PARAMS ((cp_parser *));
1443static tree cp_parser_conditional_expression
1444 PARAMS ((cp_parser *));
1445static tree cp_parser_question_colon_clause
1446 PARAMS ((cp_parser *, tree));
1447static tree cp_parser_assignment_expression
1448 PARAMS ((cp_parser *));
1449static enum tree_code cp_parser_assignment_operator_opt
1450 PARAMS ((cp_parser *));
1451static tree cp_parser_expression
1452 PARAMS ((cp_parser *));
1453static tree cp_parser_constant_expression
1454 PARAMS ((cp_parser *));
1455
1456/* Statements [gram.stmt.stmt] */
1457
1458static void cp_parser_statement
1459 PARAMS ((cp_parser *));
1460static tree cp_parser_labeled_statement
1461 PARAMS ((cp_parser *));
1462static tree cp_parser_expression_statement
1463 PARAMS ((cp_parser *));
1464static tree cp_parser_compound_statement
1465 (cp_parser *);
1466static void cp_parser_statement_seq_opt
1467 PARAMS ((cp_parser *));
1468static tree cp_parser_selection_statement
1469 PARAMS ((cp_parser *));
1470static tree cp_parser_condition
1471 PARAMS ((cp_parser *));
1472static tree cp_parser_iteration_statement
1473 PARAMS ((cp_parser *));
1474static void cp_parser_for_init_statement
1475 PARAMS ((cp_parser *));
1476static tree cp_parser_jump_statement
1477 PARAMS ((cp_parser *));
1478static void cp_parser_declaration_statement
1479 PARAMS ((cp_parser *));
1480
1481static tree cp_parser_implicitly_scoped_statement
1482 PARAMS ((cp_parser *));
1483static void cp_parser_already_scoped_statement
1484 PARAMS ((cp_parser *));
1485
1486/* Declarations [gram.dcl.dcl] */
1487
1488static void cp_parser_declaration_seq_opt
1489 PARAMS ((cp_parser *));
1490static void cp_parser_declaration
1491 PARAMS ((cp_parser *));
1492static void cp_parser_block_declaration
1493 PARAMS ((cp_parser *, bool));
1494static void cp_parser_simple_declaration
1495 PARAMS ((cp_parser *, bool));
1496static tree cp_parser_decl_specifier_seq
1497 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1498static tree cp_parser_storage_class_specifier_opt
1499 PARAMS ((cp_parser *));
1500static tree cp_parser_function_specifier_opt
1501 PARAMS ((cp_parser *));
1502static tree cp_parser_type_specifier
1503 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1504static tree cp_parser_simple_type_specifier
1505 PARAMS ((cp_parser *, cp_parser_flags));
1506static tree cp_parser_type_name
1507 PARAMS ((cp_parser *));
1508static tree cp_parser_elaborated_type_specifier
1509 PARAMS ((cp_parser *, bool, bool));
1510static tree cp_parser_enum_specifier
1511 PARAMS ((cp_parser *));
1512static void cp_parser_enumerator_list
1513 PARAMS ((cp_parser *, tree));
1514static void cp_parser_enumerator_definition
1515 PARAMS ((cp_parser *, tree));
1516static tree cp_parser_namespace_name
1517 PARAMS ((cp_parser *));
1518static void cp_parser_namespace_definition
1519 PARAMS ((cp_parser *));
1520static void cp_parser_namespace_body
1521 PARAMS ((cp_parser *));
1522static tree cp_parser_qualified_namespace_specifier
1523 PARAMS ((cp_parser *));
1524static void cp_parser_namespace_alias_definition
1525 PARAMS ((cp_parser *));
1526static void cp_parser_using_declaration
1527 PARAMS ((cp_parser *));
1528static void cp_parser_using_directive
1529 PARAMS ((cp_parser *));
1530static void cp_parser_asm_definition
1531 PARAMS ((cp_parser *));
1532static void cp_parser_linkage_specification
1533 PARAMS ((cp_parser *));
1534
1535/* Declarators [gram.dcl.decl] */
1536
1537static tree cp_parser_init_declarator
1538 PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1539static tree cp_parser_declarator
1540 PARAMS ((cp_parser *, bool, bool *));
1541static tree cp_parser_direct_declarator
1542 PARAMS ((cp_parser *, bool, bool *));
1543static enum tree_code cp_parser_ptr_operator
1544 PARAMS ((cp_parser *, tree *, tree *));
1545static tree cp_parser_cv_qualifier_seq_opt
1546 PARAMS ((cp_parser *));
1547static tree cp_parser_cv_qualifier_opt
1548 PARAMS ((cp_parser *));
1549static tree cp_parser_declarator_id
1550 PARAMS ((cp_parser *));
1551static tree cp_parser_type_id
1552 PARAMS ((cp_parser *));
1553static tree cp_parser_type_specifier_seq
1554 PARAMS ((cp_parser *));
1555static tree cp_parser_parameter_declaration_clause
1556 PARAMS ((cp_parser *));
1557static tree cp_parser_parameter_declaration_list
1558 PARAMS ((cp_parser *));
1559static tree cp_parser_parameter_declaration
1560 PARAMS ((cp_parser *, bool));
1561static tree cp_parser_function_definition
1562 PARAMS ((cp_parser *, bool *));
1563static void cp_parser_function_body
1564 (cp_parser *);
1565static tree cp_parser_initializer
1566 PARAMS ((cp_parser *, bool *));
1567static tree cp_parser_initializer_clause
1568 PARAMS ((cp_parser *));
1569static tree cp_parser_initializer_list
1570 PARAMS ((cp_parser *));
1571
1572static bool cp_parser_ctor_initializer_opt_and_function_body
1573 (cp_parser *);
1574
1575/* Classes [gram.class] */
1576
1577static tree cp_parser_class_name
1578 (cp_parser *, bool, bool, bool, bool, bool, bool);
1579static tree cp_parser_class_specifier
1580 PARAMS ((cp_parser *));
1581static tree cp_parser_class_head
1582 PARAMS ((cp_parser *, bool *, bool *, tree *));
1583static enum tag_types cp_parser_class_key
1584 PARAMS ((cp_parser *));
1585static void cp_parser_member_specification_opt
1586 PARAMS ((cp_parser *));
1587static void cp_parser_member_declaration
1588 PARAMS ((cp_parser *));
1589static tree cp_parser_pure_specifier
1590 PARAMS ((cp_parser *));
1591static tree cp_parser_constant_initializer
1592 PARAMS ((cp_parser *));
1593
1594/* Derived classes [gram.class.derived] */
1595
1596static tree cp_parser_base_clause
1597 PARAMS ((cp_parser *));
1598static tree cp_parser_base_specifier
1599 PARAMS ((cp_parser *));
1600
1601/* Special member functions [gram.special] */
1602
1603static tree cp_parser_conversion_function_id
1604 PARAMS ((cp_parser *));
1605static tree cp_parser_conversion_type_id
1606 PARAMS ((cp_parser *));
1607static tree cp_parser_conversion_declarator_opt
1608 PARAMS ((cp_parser *));
1609static bool cp_parser_ctor_initializer_opt
1610 PARAMS ((cp_parser *));
1611static void cp_parser_mem_initializer_list
1612 PARAMS ((cp_parser *));
1613static tree cp_parser_mem_initializer
1614 PARAMS ((cp_parser *));
1615static tree cp_parser_mem_initializer_id
1616 PARAMS ((cp_parser *));
1617
1618/* Overloading [gram.over] */
1619
1620static tree cp_parser_operator_function_id
1621 PARAMS ((cp_parser *));
1622static tree cp_parser_operator
1623 PARAMS ((cp_parser *));
1624
1625/* Templates [gram.temp] */
1626
1627static void cp_parser_template_declaration
1628 PARAMS ((cp_parser *, bool));
1629static tree cp_parser_template_parameter_list
1630 PARAMS ((cp_parser *));
1631static tree cp_parser_template_parameter
1632 PARAMS ((cp_parser *));
1633static tree cp_parser_type_parameter
1634 PARAMS ((cp_parser *));
1635static tree cp_parser_template_id
1636 PARAMS ((cp_parser *, bool, bool));
1637static tree cp_parser_template_name
1638 PARAMS ((cp_parser *, bool, bool));
1639static tree cp_parser_template_argument_list
1640 PARAMS ((cp_parser *));
1641static tree cp_parser_template_argument
1642 PARAMS ((cp_parser *));
1643static void cp_parser_explicit_instantiation
1644 PARAMS ((cp_parser *));
1645static void cp_parser_explicit_specialization
1646 PARAMS ((cp_parser *));
1647
1648/* Exception handling [gram.exception] */
1649
1650static tree cp_parser_try_block
1651 PARAMS ((cp_parser *));
1652static bool cp_parser_function_try_block
1653 PARAMS ((cp_parser *));
1654static void cp_parser_handler_seq
1655 PARAMS ((cp_parser *));
1656static void cp_parser_handler
1657 PARAMS ((cp_parser *));
1658static tree cp_parser_exception_declaration
1659 PARAMS ((cp_parser *));
1660static tree cp_parser_throw_expression
1661 PARAMS ((cp_parser *));
1662static tree cp_parser_exception_specification_opt
1663 PARAMS ((cp_parser *));
1664static tree cp_parser_type_id_list
1665 PARAMS ((cp_parser *));
1666
1667/* GNU Extensions */
1668
1669static tree cp_parser_asm_specification_opt
1670 PARAMS ((cp_parser *));
1671static tree cp_parser_asm_operand_list
1672 PARAMS ((cp_parser *));
1673static tree cp_parser_asm_clobber_list
1674 PARAMS ((cp_parser *));
1675static tree cp_parser_attributes_opt
1676 PARAMS ((cp_parser *));
1677static tree cp_parser_attribute_list
1678 PARAMS ((cp_parser *));
1679static bool cp_parser_extension_opt
1680 PARAMS ((cp_parser *, int *));
1681static void cp_parser_label_declaration
1682 PARAMS ((cp_parser *));
1683
1684/* Utility Routines */
1685
1686static tree cp_parser_lookup_name
6fc758aa 1687 PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
0a3b29ad 1688static tree cp_parser_lookup_name_simple
1689 PARAMS ((cp_parser *, tree));
1690static tree cp_parser_resolve_typename_type
1691 PARAMS ((cp_parser *, tree));
1692static tree cp_parser_maybe_treat_template_as_class
1693 (tree, bool);
1694static bool cp_parser_check_declarator_template_parameters
1695 PARAMS ((cp_parser *, tree));
1696static bool cp_parser_check_template_parameters
1697 PARAMS ((cp_parser *, unsigned));
1698static tree cp_parser_binary_expression
1699 PARAMS ((cp_parser *,
1700 cp_parser_token_tree_map,
1701 cp_parser_expression_fn));
1702static tree cp_parser_global_scope_opt
1703 PARAMS ((cp_parser *, bool));
1704static bool cp_parser_constructor_declarator_p
1705 (cp_parser *, bool);
1706static tree cp_parser_function_definition_from_specifiers_and_declarator
1707 PARAMS ((cp_parser *, tree, tree, tree, tree));
1708static tree cp_parser_function_definition_after_declarator
1709 PARAMS ((cp_parser *, bool));
1710static void cp_parser_template_declaration_after_export
1711 PARAMS ((cp_parser *, bool));
1712static tree cp_parser_single_declaration
1713 PARAMS ((cp_parser *, bool, bool *));
1714static tree cp_parser_functional_cast
1715 PARAMS ((cp_parser *, tree));
1716static void cp_parser_late_parsing_for_member
1717 PARAMS ((cp_parser *, tree));
1718static void cp_parser_late_parsing_default_args
2c593bd0 1719 (cp_parser *, tree, tree);
0a3b29ad 1720static tree cp_parser_sizeof_operand
1721 PARAMS ((cp_parser *, enum rid));
1722static bool cp_parser_declares_only_class_p
1723 PARAMS ((cp_parser *));
1724static bool cp_parser_friend_p
1725 PARAMS ((tree));
1726static cp_token *cp_parser_require
1727 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1728static cp_token *cp_parser_require_keyword
1729 PARAMS ((cp_parser *, enum rid, const char *));
1730static bool cp_parser_token_starts_function_definition_p
1731 PARAMS ((cp_token *));
1732static bool cp_parser_next_token_starts_class_definition_p
1733 (cp_parser *);
1734static enum tag_types cp_parser_token_is_class_key
1735 PARAMS ((cp_token *));
1736static void cp_parser_check_class_key
1737 (enum tag_types, tree type);
1738static bool cp_parser_optional_template_keyword
1739 (cp_parser *);
1740static void cp_parser_cache_group
1741 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1742static void cp_parser_parse_tentatively
1743 PARAMS ((cp_parser *));
1744static void cp_parser_commit_to_tentative_parse
1745 PARAMS ((cp_parser *));
1746static void cp_parser_abort_tentative_parse
1747 PARAMS ((cp_parser *));
1748static bool cp_parser_parse_definitely
1749 PARAMS ((cp_parser *));
1750static bool cp_parser_parsing_tentatively
1751 PARAMS ((cp_parser *));
1752static bool cp_parser_committed_to_tentative_parse
1753 PARAMS ((cp_parser *));
1754static void cp_parser_error
1755 PARAMS ((cp_parser *, const char *));
2c593bd0 1756static bool cp_parser_simulate_error
0a3b29ad 1757 PARAMS ((cp_parser *));
1758static void cp_parser_check_type_definition
1759 PARAMS ((cp_parser *));
1760static bool cp_parser_skip_to_closing_parenthesis
1761 PARAMS ((cp_parser *));
1762static bool cp_parser_skip_to_closing_parenthesis_or_comma
1763 (cp_parser *);
1764static void cp_parser_skip_to_end_of_statement
1765 PARAMS ((cp_parser *));
1766static void cp_parser_skip_to_end_of_block_or_statement
1767 PARAMS ((cp_parser *));
1768static void cp_parser_skip_to_closing_brace
1769 (cp_parser *);
1770static void cp_parser_skip_until_found
1771 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1772static bool cp_parser_error_occurred
1773 PARAMS ((cp_parser *));
1774static bool cp_parser_allow_gnu_extensions_p
1775 PARAMS ((cp_parser *));
1776static bool cp_parser_is_string_literal
1777 PARAMS ((cp_token *));
1778static bool cp_parser_is_keyword
1779 PARAMS ((cp_token *, enum rid));
1780static bool cp_parser_dependent_type_p
1781 (tree);
1782static bool cp_parser_value_dependent_expression_p
1783 (tree);
1784static bool cp_parser_type_dependent_expression_p
1785 (tree);
1786static bool cp_parser_dependent_template_arg_p
1787 (tree);
1788static bool cp_parser_dependent_template_id_p
1789 (tree, tree);
1790static bool cp_parser_dependent_template_p
1791 (tree);
1792static void cp_parser_defer_access_check
1793 (cp_parser *, tree, tree);
1794static void cp_parser_start_deferring_access_checks
1795 (cp_parser *);
1796static tree cp_parser_stop_deferring_access_checks
1797 PARAMS ((cp_parser *));
1798static void cp_parser_perform_deferred_access_checks
1799 PARAMS ((tree));
1800static tree cp_parser_scope_through_which_access_occurs
1801 (tree, tree, tree);
1802
1803/* Returns non-zero if TOKEN is a string literal. */
1804
1805static bool
1806cp_parser_is_string_literal (token)
1807 cp_token *token;
1808{
1809 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1810}
1811
1812/* Returns non-zero if TOKEN is the indicated KEYWORD. */
1813
1814static bool
1815cp_parser_is_keyword (token, keyword)
1816 cp_token *token;
1817 enum rid keyword;
1818{
1819 return token->keyword == keyword;
1820}
1821
1822/* Returns TRUE if TYPE is dependent, in the sense of
1823 [temp.dep.type]. */
1824
1825static bool
1826cp_parser_dependent_type_p (type)
1827 tree type;
1828{
1829 tree scope;
1830
1831 if (!processing_template_decl)
1832 return false;
1833
1834 /* If the type is NULL, we have not computed a type for the entity
1835 in question; in that case, the type is dependent. */
1836 if (!type)
1837 return true;
1838
1839 /* Erroneous types can be considered non-dependent. */
1840 if (type == error_mark_node)
1841 return false;
1842
1843 /* [temp.dep.type]
1844
1845 A type is dependent if it is:
1846
1847 -- a template parameter. */
1848 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1849 return true;
1850 /* -- a qualified-id with a nested-name-specifier which contains a
1851 class-name that names a dependent type or whose unqualified-id
1852 names a dependent type. */
1853 if (TREE_CODE (type) == TYPENAME_TYPE)
1854 return true;
1855 /* -- a cv-qualified type where the cv-unqualified type is
1856 dependent. */
1857 type = TYPE_MAIN_VARIANT (type);
1858 /* -- a compound type constructed from any dependent type. */
1859 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1860 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1861 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1862 (type)));
1863 else if (TREE_CODE (type) == POINTER_TYPE
1864 || TREE_CODE (type) == REFERENCE_TYPE)
1865 return cp_parser_dependent_type_p (TREE_TYPE (type));
1866 else if (TREE_CODE (type) == FUNCTION_TYPE
1867 || TREE_CODE (type) == METHOD_TYPE)
1868 {
1869 tree arg_type;
1870
1871 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1872 return true;
1873 for (arg_type = TYPE_ARG_TYPES (type);
1874 arg_type;
1875 arg_type = TREE_CHAIN (arg_type))
1876 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1877 return true;
1878 return false;
1879 }
1880 /* -- an array type constructed from any dependent type or whose
1881 size is specified by a constant expression that is
1882 value-dependent. */
1883 if (TREE_CODE (type) == ARRAY_TYPE)
1884 {
d1a9ebc2 1885 if (TYPE_DOMAIN (type)
0a3b29ad 1886 && ((cp_parser_value_dependent_expression_p
1887 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1888 || (cp_parser_type_dependent_expression_p
1889 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1890 return true;
1891 return cp_parser_dependent_type_p (TREE_TYPE (type));
1892 }
1893 /* -- a template-id in which either the template name is a template
1894 parameter or any of the template arguments is a dependent type or
1895 an expression that is type-dependent or value-dependent.
1896
1897 This language seems somewhat confused; for example, it does not
1898 discuss template template arguments. Therefore, we use the
1899 definition for dependent template arguments in [temp.dep.temp]. */
1900 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1901 && (cp_parser_dependent_template_id_p
1902 (CLASSTYPE_TI_TEMPLATE (type),
1903 CLASSTYPE_TI_ARGS (type))))
1904 return true;
1905 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1906 return true;
1907 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1908 expression is not type-dependent, then it should already been
1909 have resolved. */
1910 if (TREE_CODE (type) == TYPEOF_TYPE)
1911 return true;
1912 /* The standard does not specifically mention types that are local
1913 to template functions or local classes, but they should be
1914 considered dependent too. For example:
1915
1916 template <int I> void f() {
1917 enum E { a = I };
1918 S<sizeof (E)> s;
1919 }
1920
1921 The size of `E' cannot be known until the value of `I' has been
1922 determined. Therefore, `E' must be considered dependent. */
1923 scope = TYPE_CONTEXT (type);
1924 if (scope && TYPE_P (scope))
1925 return cp_parser_dependent_type_p (scope);
1926 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1927 return cp_parser_type_dependent_expression_p (scope);
1928
1929 /* Other types are non-dependent. */
1930 return false;
1931}
1932
1933/* Returns TRUE if the EXPRESSION is value-dependent. */
1934
1935static bool
1936cp_parser_value_dependent_expression_p (tree expression)
1937{
1938 if (!processing_template_decl)
1939 return false;
1940
1941 /* A name declared with a dependent type. */
1942 if (DECL_P (expression)
1943 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1944 return true;
1945 /* A non-type template parameter. */
1946 if ((TREE_CODE (expression) == CONST_DECL
1947 && DECL_TEMPLATE_PARM_P (expression))
1948 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1949 return true;
1950 /* A constant with integral or enumeration type and is initialized
1951 with an expression that is value-dependent. */
1952 if (TREE_CODE (expression) == VAR_DECL
1953 && DECL_INITIAL (expression)
1954 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1955 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1956 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1957 return true;
1958 /* These expressions are value-dependent if the type to which the
1959 cast occurs is dependent. */
1960 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1961 || TREE_CODE (expression) == STATIC_CAST_EXPR
1962 || TREE_CODE (expression) == CONST_CAST_EXPR
1963 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
1964 || TREE_CODE (expression) == CAST_EXPR)
1965 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1966 return true;
1967 /* A `sizeof' expression where the sizeof operand is a type is
1968 value-dependent if the type is dependent. If the type was not
1969 dependent, we would no longer have a SIZEOF_EXPR, so any
1970 SIZEOF_EXPR is dependent. */
1971 if (TREE_CODE (expression) == SIZEOF_EXPR)
1972 return true;
1973 /* A constant expression is value-dependent if any subexpression is
1974 value-dependent. */
1975 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
1976 {
1977 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
1978 {
1979 case '1':
1980 return (cp_parser_value_dependent_expression_p
1981 (TREE_OPERAND (expression, 0)));
1982 case '<':
1983 case '2':
1984 return ((cp_parser_value_dependent_expression_p
1985 (TREE_OPERAND (expression, 0)))
1986 || (cp_parser_value_dependent_expression_p
1987 (TREE_OPERAND (expression, 1))));
1988 case 'e':
1989 {
1990 int i;
1991 for (i = 0;
1992 i < TREE_CODE_LENGTH (TREE_CODE (expression));
1993 ++i)
1994 if (cp_parser_value_dependent_expression_p
1995 (TREE_OPERAND (expression, i)))
1996 return true;
1997 return false;
1998 }
1999 }
2000 }
2001
2002 /* The expression is not value-dependent. */
2003 return false;
2004}
2005
2006/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2007 [temp.dep.expr]. */
2008
2009static bool
2010cp_parser_type_dependent_expression_p (expression)
2011 tree expression;
2012{
2013 if (!processing_template_decl)
2014 return false;
2015
2016 /* Some expression forms are never type-dependent. */
2017 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2018 || TREE_CODE (expression) == SIZEOF_EXPR
2019 || TREE_CODE (expression) == ALIGNOF_EXPR
2020 || TREE_CODE (expression) == TYPEID_EXPR
2021 || TREE_CODE (expression) == DELETE_EXPR
2022 || TREE_CODE (expression) == VEC_DELETE_EXPR
2023 || TREE_CODE (expression) == THROW_EXPR)
2024 return false;
2025
2026 /* The types of these expressions depends only on the type to which
2027 the cast occurs. */
2028 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2029 || TREE_CODE (expression) == STATIC_CAST_EXPR
2030 || TREE_CODE (expression) == CONST_CAST_EXPR
2031 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2032 || TREE_CODE (expression) == CAST_EXPR)
2033 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2034 /* The types of these expressions depends only on the type created
2035 by the expression. */
2036 else if (TREE_CODE (expression) == NEW_EXPR
2037 || TREE_CODE (expression) == VEC_NEW_EXPR)
2038 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2039
2040 if (TREE_CODE (expression) == FUNCTION_DECL
2041 && DECL_LANG_SPECIFIC (expression)
2042 && DECL_TEMPLATE_INFO (expression)
2043 && (cp_parser_dependent_template_id_p
2044 (DECL_TI_TEMPLATE (expression),
2045 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2046 return true;
2047
2048 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2049}
2050
2051/* Returns TRUE if the ARG (a template argument) is dependent. */
2052
2053static bool
2054cp_parser_dependent_template_arg_p (tree arg)
2055{
2056 if (!processing_template_decl)
2057 return false;
2058
2059 if (TREE_CODE (arg) == TEMPLATE_DECL
2060 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2061 return cp_parser_dependent_template_p (arg);
2062 else if (TYPE_P (arg))
2063 return cp_parser_dependent_type_p (arg);
2064 else
2065 return (cp_parser_type_dependent_expression_p (arg)
2066 || cp_parser_value_dependent_expression_p (arg));
2067}
2068
2069/* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2070
2071static bool
2072cp_parser_dependent_template_id_p (tree tmpl, tree args)
2073{
2074 int i;
2075
2076 if (cp_parser_dependent_template_p (tmpl))
2077 return true;
2078 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2079 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2080 return true;
2081 return false;
2082}
2083
2084/* Returns TRUE if the template TMPL is dependent. */
2085
2086static bool
2087cp_parser_dependent_template_p (tree tmpl)
2088{
2089 /* Template template parameters are dependent. */
2090 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2091 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2092 return true;
2093 /* So are member templates of dependent classes. */
2094 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2095 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2096 return false;
2097}
2098
2099/* Defer checking the accessibility of DECL, when looked up in
2100 CLASS_TYPE. */
2101
2102static void
2103cp_parser_defer_access_check (cp_parser *parser,
2104 tree class_type,
2105 tree decl)
2106{
2107 tree check;
2108
2109 /* If we are not supposed to defer access checks, just check now. */
2110 if (!parser->context->deferring_access_checks_p)
2111 {
2112 enforce_access (class_type, decl);
2113 return;
2114 }
2115
2116 /* See if we are already going to perform this check. */
2117 for (check = parser->context->deferred_access_checks;
2118 check;
2119 check = TREE_CHAIN (check))
2120 if (TREE_VALUE (check) == decl
2121 && same_type_p (TREE_PURPOSE (check), class_type))
2122 return;
2123 /* If not, record the check. */
2124 parser->context->deferred_access_checks
2125 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2126}
2127
2128/* Start deferring access control checks. */
2129
2130static void
2131cp_parser_start_deferring_access_checks (cp_parser *parser)
2132{
2133 parser->context->deferring_access_checks_p = true;
2134}
2135
2136/* Stop deferring access control checks. Returns a TREE_LIST
2137 representing the deferred checks. The TREE_PURPOSE of each node is
2138 the type through which the access occurred; the TREE_VALUE is the
2139 declaration named. */
2140
2141static tree
2142cp_parser_stop_deferring_access_checks (parser)
2143 cp_parser *parser;
2144{
2145 tree access_checks;
2146
2147 parser->context->deferring_access_checks_p = false;
2148 access_checks = parser->context->deferred_access_checks;
2149 parser->context->deferred_access_checks = NULL_TREE;
2150
2151 return access_checks;
2152}
2153
2154/* Perform the deferred ACCESS_CHECKS, whose representation is as
2155 documented with cp_parser_stop_deferrring_access_checks. */
2156
2157static void
2158cp_parser_perform_deferred_access_checks (access_checks)
2159 tree access_checks;
2160{
2161 tree deferred_check;
2162
2163 /* Look through all the deferred checks. */
2164 for (deferred_check = access_checks;
2165 deferred_check;
2166 deferred_check = TREE_CHAIN (deferred_check))
2167 /* Check access. */
2168 enforce_access (TREE_PURPOSE (deferred_check),
2169 TREE_VALUE (deferred_check));
2170}
2171
2172/* Returns the scope through which DECL is being accessed, or
2173 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2174 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2175 or `x', respectively. If the DECL was named as `A::B' then
2176 NESTED_NAME_SPECIFIER is `A'. */
2177
2178tree
2179cp_parser_scope_through_which_access_occurs (decl,
2180 object_type,
2181 nested_name_specifier)
2182 tree decl;
2183 tree object_type;
2184 tree nested_name_specifier;
2185{
2186 tree scope;
2187 tree qualifying_type = NULL_TREE;
2188
2189 /* Determine the SCOPE of DECL. */
2190 scope = context_for_name_lookup (decl);
2191 /* If the SCOPE is not a type, then DECL is not a member. */
2192 if (!TYPE_P (scope))
2193 return NULL_TREE;
2194 /* Figure out the type through which DECL is being accessed. */
2195 if (object_type && DERIVED_FROM_P (scope, object_type))
2196 /* If we are processing a `->' or `.' expression, use the type of the
2197 left-hand side. */
2198 qualifying_type = object_type;
2199 else if (nested_name_specifier)
2200 {
2201 /* If the reference is to a non-static member of the
2202 current class, treat it as if it were referenced through
2203 `this'. */
2204 if (DECL_NONSTATIC_MEMBER_P (decl)
2205 && current_class_ptr
2206 && DERIVED_FROM_P (scope, current_class_type))
2207 qualifying_type = current_class_type;
2208 /* Otherwise, use the type indicated by the
2209 nested-name-specifier. */
2210 else
2211 qualifying_type = nested_name_specifier;
2212 }
2213 else
2214 /* Otherwise, the name must be from the current class or one of
2215 its bases. */
2216 qualifying_type = currently_open_derived_class (scope);
2217
2218 return qualifying_type;
2219}
2220
2221/* Issue the indicated error MESSAGE. */
2222
2223static void
2224cp_parser_error (parser, message)
2225 cp_parser *parser;
2226 const char *message;
2227{
0a3b29ad 2228 /* Output the MESSAGE -- unless we're parsing tentatively. */
2c593bd0 2229 if (!cp_parser_simulate_error (parser))
0a3b29ad 2230 error (message);
2231}
2232
2233/* If we are parsing tentatively, remember that an error has occurred
2c593bd0 2234 during this tentative parse. Returns true if the error was
2235 simulated; false if a messgae should be issued by the caller. */
0a3b29ad 2236
2c593bd0 2237static bool
0a3b29ad 2238cp_parser_simulate_error (parser)
2239 cp_parser *parser;
2240{
2241 if (cp_parser_parsing_tentatively (parser)
2242 && !cp_parser_committed_to_tentative_parse (parser))
2c593bd0 2243 {
2244 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2245 return true;
2246 }
2247 return false;
0a3b29ad 2248}
2249
2250/* This function is called when a type is defined. If type
2251 definitions are forbidden at this point, an error message is
2252 issued. */
2253
2254static void
2255cp_parser_check_type_definition (parser)
2256 cp_parser *parser;
2257{
2258 /* If types are forbidden here, issue a message. */
2259 if (parser->type_definition_forbidden_message)
2260 /* Use `%s' to print the string in case there are any escape
2261 characters in the message. */
2262 error ("%s", parser->type_definition_forbidden_message);
2263}
2264
2265/* Consume tokens up to, and including, the next non-nested closing `)'.
2266 Returns TRUE iff we found a closing `)'. */
2267
2268static bool
2269cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2270{
2271 unsigned nesting_depth = 0;
2272
2273 while (true)
2274 {
2275 cp_token *token;
2276
2277 /* If we've run out of tokens, then there is no closing `)'. */
2278 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2279 return false;
2280 /* Consume the token. */
2281 token = cp_lexer_consume_token (parser->lexer);
2282 /* If it is an `(', we have entered another level of nesting. */
2283 if (token->type == CPP_OPEN_PAREN)
2284 ++nesting_depth;
2285 /* If it is a `)', then we might be done. */
2286 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2287 return true;
2288 }
2289}
2290
2291/* Consume tokens until the next token is a `)', or a `,'. Returns
2292 TRUE if the next token is a `,'. */
2293
2294static bool
2295cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2296{
2297 unsigned nesting_depth = 0;
2298
2299 while (true)
2300 {
2301 cp_token *token = cp_lexer_peek_token (parser->lexer);
2302
2303 /* If we've run out of tokens, then there is no closing `)'. */
2304 if (token->type == CPP_EOF)
2305 return false;
2306 /* If it is a `,' stop. */
2307 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2308 return true;
2309 /* If it is a `)', stop. */
2310 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2311 return false;
2312 /* If it is an `(', we have entered another level of nesting. */
2313 else if (token->type == CPP_OPEN_PAREN)
2314 ++nesting_depth;
2315 /* Consume the token. */
2316 token = cp_lexer_consume_token (parser->lexer);
2317 }
2318}
2319
2320/* Consume tokens until we reach the end of the current statement.
2321 Normally, that will be just before consuming a `;'. However, if a
2322 non-nested `}' comes first, then we stop before consuming that. */
2323
2324static void
2325cp_parser_skip_to_end_of_statement (parser)
2326 cp_parser *parser;
2327{
2328 unsigned nesting_depth = 0;
2329
2330 while (true)
2331 {
2332 cp_token *token;
2333
2334 /* Peek at the next token. */
2335 token = cp_lexer_peek_token (parser->lexer);
2336 /* If we've run out of tokens, stop. */
2337 if (token->type == CPP_EOF)
2338 break;
2339 /* If the next token is a `;', we have reached the end of the
2340 statement. */
2341 if (token->type == CPP_SEMICOLON && !nesting_depth)
2342 break;
2343 /* If the next token is a non-nested `}', then we have reached
2344 the end of the current block. */
2345 if (token->type == CPP_CLOSE_BRACE)
2346 {
2347 /* If this is a non-nested `}', stop before consuming it.
2348 That way, when confronted with something like:
2349
2350 { 3 + }
2351
2352 we stop before consuming the closing `}', even though we
2353 have not yet reached a `;'. */
2354 if (nesting_depth == 0)
2355 break;
2356 /* If it is the closing `}' for a block that we have
2357 scanned, stop -- but only after consuming the token.
2358 That way given:
2359
2360 void f g () { ... }
2361 typedef int I;
2362
2363 we will stop after the body of the erroneously declared
2364 function, but before consuming the following `typedef'
2365 declaration. */
2366 if (--nesting_depth == 0)
2367 {
2368 cp_lexer_consume_token (parser->lexer);
2369 break;
2370 }
2371 }
2372 /* If it the next token is a `{', then we are entering a new
2373 block. Consume the entire block. */
2374 else if (token->type == CPP_OPEN_BRACE)
2375 ++nesting_depth;
2376 /* Consume the token. */
2377 cp_lexer_consume_token (parser->lexer);
2378 }
2379}
2380
2381/* Skip tokens until we have consumed an entire block, or until we
2382 have consumed a non-nested `;'. */
2383
2384static void
2385cp_parser_skip_to_end_of_block_or_statement (parser)
2386 cp_parser *parser;
2387{
2388 unsigned nesting_depth = 0;
2389
2390 while (true)
2391 {
2392 cp_token *token;
2393
2394 /* Peek at the next token. */
2395 token = cp_lexer_peek_token (parser->lexer);
2396 /* If we've run out of tokens, stop. */
2397 if (token->type == CPP_EOF)
2398 break;
2399 /* If the next token is a `;', we have reached the end of the
2400 statement. */
2401 if (token->type == CPP_SEMICOLON && !nesting_depth)
2402 {
2403 /* Consume the `;'. */
2404 cp_lexer_consume_token (parser->lexer);
2405 break;
2406 }
2407 /* Consume the token. */
2408 token = cp_lexer_consume_token (parser->lexer);
2409 /* If the next token is a non-nested `}', then we have reached
2410 the end of the current block. */
2411 if (token->type == CPP_CLOSE_BRACE
2412 && (nesting_depth == 0 || --nesting_depth == 0))
2413 break;
2414 /* If it the next token is a `{', then we are entering a new
2415 block. Consume the entire block. */
2416 if (token->type == CPP_OPEN_BRACE)
2417 ++nesting_depth;
2418 }
2419}
2420
2421/* Skip tokens until a non-nested closing curly brace is the next
2422 token. */
2423
2424static void
2425cp_parser_skip_to_closing_brace (cp_parser *parser)
2426{
2427 unsigned nesting_depth = 0;
2428
2429 while (true)
2430 {
2431 cp_token *token;
2432
2433 /* Peek at the next token. */
2434 token = cp_lexer_peek_token (parser->lexer);
2435 /* If we've run out of tokens, stop. */
2436 if (token->type == CPP_EOF)
2437 break;
2438 /* If the next token is a non-nested `}', then we have reached
2439 the end of the current block. */
2440 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2441 break;
2442 /* If it the next token is a `{', then we are entering a new
2443 block. Consume the entire block. */
2444 else if (token->type == CPP_OPEN_BRACE)
2445 ++nesting_depth;
2446 /* Consume the token. */
2447 cp_lexer_consume_token (parser->lexer);
2448 }
2449}
2450
2451/* Create a new C++ parser. */
2452
2453static cp_parser *
2454cp_parser_new ()
2455{
2456 cp_parser *parser;
2457
2458 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2459 parser->lexer = cp_lexer_new (/*main_lexer_p=*/true);
2460 parser->context = cp_parser_context_new (NULL);
2461
2462 /* For now, we always accept GNU extensions. */
2463 parser->allow_gnu_extensions_p = 1;
2464
2465 /* The `>' token is a greater-than operator, not the end of a
2466 template-id. */
2467 parser->greater_than_is_operator_p = true;
2468
2469 parser->default_arg_ok_p = true;
2470
2471 /* We are not parsing a constant-expression. */
2472 parser->constant_expression_p = false;
2473
2474 /* Local variable names are not forbidden. */
2475 parser->local_variables_forbidden_p = false;
2476
2477 /* We are not procesing an `extern "C"' declaration. */
2478 parser->in_unbraced_linkage_specification_p = false;
2479
2480 /* We are not processing a declarator. */
2481 parser->in_declarator_p = false;
2482
2483 /* There are no default args to process. */
2484 parser->default_arg_types = NULL;
2485
2486 /* The unparsed function queue is empty. */
2487 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2488
2489 /* There are no classes being defined. */
2490 parser->num_classes_being_defined = 0;
2491
2492 /* No template parameters apply. */
2493 parser->num_template_parameter_lists = 0;
2494
2495 return parser;
2496}
2497
2498/* Lexical conventions [gram.lex] */
2499
2500/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2501 identifier. */
2502
2503static tree
2504cp_parser_identifier (parser)
2505 cp_parser *parser;
2506{
2507 cp_token *token;
2508
2509 /* Look for the identifier. */
2510 token = cp_parser_require (parser, CPP_NAME, "identifier");
2511 /* Return the value. */
2512 return token ? token->value : error_mark_node;
2513}
2514
2515/* Basic concepts [gram.basic] */
2516
2517/* Parse a translation-unit.
2518
2519 translation-unit:
2520 declaration-seq [opt]
2521
2522 Returns TRUE if all went well. */
2523
2524static bool
2525cp_parser_translation_unit (parser)
2526 cp_parser *parser;
2527{
2528 while (true)
2529 {
2530 cp_parser_declaration_seq_opt (parser);
2531
2532 /* If there are no tokens left then all went well. */
2533 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2534 break;
2535
2536 /* Otherwise, issue an error message. */
2537 cp_parser_error (parser, "expected declaration");
2538 return false;
2539 }
2540
2541 /* Consume the EOF token. */
2542 cp_parser_require (parser, CPP_EOF, "end-of-file");
2543
2544 /* Finish up. */
2545 finish_translation_unit ();
2546
2547 /* All went well. */
2548 return true;
2549}
2550
2551/* Expressions [gram.expr] */
2552
2553/* Parse a primary-expression.
2554
2555 primary-expression:
2556 literal
2557 this
2558 ( expression )
2559 id-expression
2560
2561 GNU Extensions:
2562
2563 primary-expression:
2564 ( compound-statement )
2565 __builtin_va_arg ( assignment-expression , type-id )
2566
2567 literal:
2568 __null
2569
2570 Returns a representation of the expression.
2571
2572 *IDK indicates what kind of id-expression (if any) was present.
2573
2574 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2575 used as the operand of a pointer-to-member. In that case,
2576 *QUALIFYING_CLASS gives the class that is used as the qualifying
2577 class in the pointer-to-member. */
2578
2579static tree
2580cp_parser_primary_expression (cp_parser *parser,
2581 cp_parser_id_kind *idk,
2582 tree *qualifying_class)
2583{
2584 cp_token *token;
2585
2586 /* Assume the primary expression is not an id-expression. */
2587 *idk = CP_PARSER_ID_KIND_NONE;
2588 /* And that it cannot be used as pointer-to-member. */
2589 *qualifying_class = NULL_TREE;
2590
2591 /* Peek at the next token. */
2592 token = cp_lexer_peek_token (parser->lexer);
2593 switch (token->type)
2594 {
2595 /* literal:
2596 integer-literal
2597 character-literal
2598 floating-literal
2599 string-literal
2600 boolean-literal */
2601 case CPP_CHAR:
2602 case CPP_WCHAR:
2603 case CPP_STRING:
2604 case CPP_WSTRING:
2605 case CPP_NUMBER:
2606 token = cp_lexer_consume_token (parser->lexer);
2607 return token->value;
2608
2609 case CPP_OPEN_PAREN:
2610 {
2611 tree expr;
2612 bool saved_greater_than_is_operator_p;
2613
2614 /* Consume the `('. */
2615 cp_lexer_consume_token (parser->lexer);
2616 /* Within a parenthesized expression, a `>' token is always
2617 the greater-than operator. */
2618 saved_greater_than_is_operator_p
2619 = parser->greater_than_is_operator_p;
2620 parser->greater_than_is_operator_p = true;
2621 /* If we see `( { ' then we are looking at the beginning of
2622 a GNU statement-expression. */
2623 if (cp_parser_allow_gnu_extensions_p (parser)
2624 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2625 {
2626 /* Statement-expressions are not allowed by the standard. */
2627 if (pedantic)
2628 pedwarn ("ISO C++ forbids braced-groups within expressions");
2629
2630 /* And they're not allowed outside of a function-body; you
2631 cannot, for example, write:
2632
2633 int i = ({ int j = 3; j + 1; });
2634
2635 at class or namespace scope. */
2636 if (!at_function_scope_p ())
2637 error ("statement-expressions are allowed only inside functions");
2638 /* Start the statement-expression. */
2639 expr = begin_stmt_expr ();
2640 /* Parse the compound-statement. */
2641 cp_parser_compound_statement (parser);
2642 /* Finish up. */
2643 expr = finish_stmt_expr (expr);
2644 }
2645 else
2646 {
2647 /* Parse the parenthesized expression. */
2648 expr = cp_parser_expression (parser);
2649 /* Let the front end know that this expression was
2650 enclosed in parentheses. This matters in case, for
2651 example, the expression is of the form `A::B', since
2652 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2653 not. */
2654 finish_parenthesized_expr (expr);
2655 }
2656 /* The `>' token might be the end of a template-id or
2657 template-parameter-list now. */
2658 parser->greater_than_is_operator_p
2659 = saved_greater_than_is_operator_p;
2660 /* Consume the `)'. */
2661 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2662 cp_parser_skip_to_end_of_statement (parser);
2663
2664 return expr;
2665 }
2666
2667 case CPP_KEYWORD:
2668 switch (token->keyword)
2669 {
2670 /* These two are the boolean literals. */
2671 case RID_TRUE:
2672 cp_lexer_consume_token (parser->lexer);
2673 return boolean_true_node;
2674 case RID_FALSE:
2675 cp_lexer_consume_token (parser->lexer);
2676 return boolean_false_node;
2677
2678 /* The `__null' literal. */
2679 case RID_NULL:
2680 cp_lexer_consume_token (parser->lexer);
2681 return null_node;
2682
2683 /* Recognize the `this' keyword. */
2684 case RID_THIS:
2685 cp_lexer_consume_token (parser->lexer);
2686 if (parser->local_variables_forbidden_p)
2687 {
2688 error ("`this' may not be used in this context");
2689 return error_mark_node;
2690 }
2691 return finish_this_expr ();
2692
2693 /* The `operator' keyword can be the beginning of an
2694 id-expression. */
2695 case RID_OPERATOR:
2696 goto id_expression;
2697
2698 case RID_FUNCTION_NAME:
2699 case RID_PRETTY_FUNCTION_NAME:
2700 case RID_C99_FUNCTION_NAME:
2701 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2702 __func__ are the names of variables -- but they are
2703 treated specially. Therefore, they are handled here,
2704 rather than relying on the generic id-expression logic
2705 below. Gramatically, these names are id-expressions.
2706
2707 Consume the token. */
2708 token = cp_lexer_consume_token (parser->lexer);
2709 /* Look up the name. */
2710 return finish_fname (token->value);
2711
2712 case RID_VA_ARG:
2713 {
2714 tree expression;
2715 tree type;
2716
2717 /* The `__builtin_va_arg' construct is used to handle
2718 `va_arg'. Consume the `__builtin_va_arg' token. */
2719 cp_lexer_consume_token (parser->lexer);
2720 /* Look for the opening `('. */
2721 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2722 /* Now, parse the assignment-expression. */
2723 expression = cp_parser_assignment_expression (parser);
2724 /* Look for the `,'. */
2725 cp_parser_require (parser, CPP_COMMA, "`,'");
2726 /* Parse the type-id. */
2727 type = cp_parser_type_id (parser);
2728 /* Look for the closing `)'. */
2729 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2730
2731 return build_x_va_arg (expression, type);
2732 }
2733
2734 default:
2735 cp_parser_error (parser, "expected primary-expression");
2736 return error_mark_node;
2737 }
2738 /* Fall through. */
2739
2740 /* An id-expression can start with either an identifier, a
2741 `::' as the beginning of a qualified-id, or the "operator"
2742 keyword. */
2743 case CPP_NAME:
2744 case CPP_SCOPE:
2745 case CPP_TEMPLATE_ID:
2746 case CPP_NESTED_NAME_SPECIFIER:
2747 {
2748 tree id_expression;
2749 tree decl;
2750
2751 id_expression:
2752 /* Parse the id-expression. */
2753 id_expression
2754 = cp_parser_id_expression (parser,
2755 /*template_keyword_p=*/false,
2756 /*check_dependency_p=*/true,
2757 /*template_p=*/NULL);
2758 if (id_expression == error_mark_node)
2759 return error_mark_node;
2760 /* If we have a template-id, then no further lookup is
2761 required. If the template-id was for a template-class, we
2762 will sometimes have a TYPE_DECL at this point. */
2763 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2764 || TREE_CODE (id_expression) == TYPE_DECL)
2765 decl = id_expression;
2766 /* Look up the name. */
2767 else
2768 {
2769 decl = cp_parser_lookup_name_simple (parser, id_expression);
2770 /* If name lookup gives us a SCOPE_REF, then the
2771 qualifying scope was dependent. Just propagate the
2772 name. */
2773 if (TREE_CODE (decl) == SCOPE_REF)
2774 {
2775 if (TYPE_P (TREE_OPERAND (decl, 0)))
2776 *qualifying_class = TREE_OPERAND (decl, 0);
2777 return decl;
2778 }
2779 /* Check to see if DECL is a local variable in a context
2780 where that is forbidden. */
2781 if (parser->local_variables_forbidden_p
2782 && local_variable_p (decl))
2783 {
2784 /* It might be that we only found DECL because we are
2785 trying to be generous with pre-ISO scoping rules.
2786 For example, consider:
2787
2788 int i;
2789 void g() {
2790 for (int i = 0; i < 10; ++i) {}
2791 extern void f(int j = i);
2792 }
2793
2794 Here, name look up will originally find the out
2795 of scope `i'. We need to issue a warning message,
2796 but then use the global `i'. */
2797 decl = check_for_out_of_scope_variable (decl);
2798 if (local_variable_p (decl))
2799 {
2800 error ("local variable `%D' may not appear in this context",
2801 decl);
2802 return error_mark_node;
2803 }
2804 }
2805
2806 /* If unqualified name lookup fails while processing a
2807 template, that just means that we need to do name
2808 lookup again when the template is instantiated. */
2809 if (!parser->scope
2810 && decl == error_mark_node
2811 && processing_template_decl)
2812 {
2813 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2814 return build_min_nt (LOOKUP_EXPR, id_expression);
2815 }
2816 else if (decl == error_mark_node
2817 && !processing_template_decl)
2818 {
2819 if (!parser->scope)
2820 {
2821 /* It may be resolvable as a koenig lookup function
2822 call. */
2823 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2824 return id_expression;
2825 }
2826 else if (TYPE_P (parser->scope)
2827 && !COMPLETE_TYPE_P (parser->scope))
2828 error ("incomplete type `%T' used in nested name specifier",
2829 parser->scope);
2830 else if (parser->scope != global_namespace)
2831 error ("`%D' is not a member of `%D'",
2832 id_expression, parser->scope);
2833 else
2834 error ("`::%D' has not been declared", id_expression);
2835 }
2836 /* If DECL is a variable would be out of scope under
2837 ANSI/ISO rules, but in scope in the ARM, name lookup
2838 will succeed. Issue a diagnostic here. */
2839 else
2840 decl = check_for_out_of_scope_variable (decl);
2841
2842 /* Remember that the name was used in the definition of
2843 the current class so that we can check later to see if
2844 the meaning would have been different after the class
2845 was entirely defined. */
2846 if (!parser->scope && decl != error_mark_node)
2847 maybe_note_name_used_in_class (id_expression, decl);
2848 }
2849
2850 /* If we didn't find anything, or what we found was a type,
2851 then this wasn't really an id-expression. */
2852 if (TREE_CODE (decl) == TYPE_DECL
2853 || TREE_CODE (decl) == NAMESPACE_DECL
2854 || (TREE_CODE (decl) == TEMPLATE_DECL
2855 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2856 {
2857 cp_parser_error (parser,
2858 "expected primary-expression");
2859 return error_mark_node;
2860 }
2861
2862 /* If the name resolved to a template parameter, there is no
2863 need to look it up again later. Similarly, we resolve
2864 enumeration constants to their underlying values. */
2865 if (TREE_CODE (decl) == CONST_DECL)
2866 {
2867 *idk = CP_PARSER_ID_KIND_NONE;
2868 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2869 return DECL_INITIAL (decl);
2870 return decl;
2871 }
2872 else
2873 {
2874 bool dependent_p;
2875
2876 /* If the declaration was explicitly qualified indicate
2877 that. The semantics of `A::f(3)' are different than
2878 `f(3)' if `f' is virtual. */
2879 *idk = (parser->scope
2880 ? CP_PARSER_ID_KIND_QUALIFIED
2881 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2882 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2883 : CP_PARSER_ID_KIND_UNQUALIFIED));
2884
2885
2886 /* [temp.dep.expr]
2887
2888 An id-expression is type-dependent if it contains an
2889 identifier that was declared with a dependent type.
2890
2891 As an optimization, we could choose not to create a
2892 LOOKUP_EXPR for a name that resolved to a local
2893 variable in the template function that we are currently
2894 declaring; such a name cannot ever resolve to anything
2895 else. If we did that we would not have to look up
2896 these names at instantiation time.
2897
2898 The standard is not very specific about an
2899 id-expression that names a set of overloaded functions.
2900 What if some of them have dependent types and some of
2901 them do not? Presumably, such a name should be treated
2902 as a dependent name. */
2903 /* Assume the name is not dependent. */
2904 dependent_p = false;
2905 if (!processing_template_decl)
2906 /* No names are dependent outside a template. */
2907 ;
2908 /* A template-id where the name of the template was not
2909 resolved is definitely dependent. */
2910 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2911 && (TREE_CODE (TREE_OPERAND (decl, 0))
2912 == IDENTIFIER_NODE))
2913 dependent_p = true;
2914 /* For anything except an overloaded function, just check
2915 its type. */
2916 else if (!is_overloaded_fn (decl))
2917 dependent_p
2918 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2919 /* For a set of overloaded functions, check each of the
2920 functions. */
2921 else
2922 {
2923 tree fns = decl;
2924
2925 if (BASELINK_P (fns))
2926 fns = BASELINK_FUNCTIONS (fns);
2927
2928 /* For a template-id, check to see if the template
2929 arguments are dependent. */
2930 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2931 {
2932 tree args = TREE_OPERAND (fns, 1);
2933
2934 if (args && TREE_CODE (args) == TREE_LIST)
2935 {
2936 while (args)
2937 {
2938 if (cp_parser_dependent_template_arg_p
2939 (TREE_VALUE (args)))
2940 {
2941 dependent_p = true;
2942 break;
2943 }
2944 args = TREE_CHAIN (args);
2945 }
2946 }
2947 else if (args && TREE_CODE (args) == TREE_VEC)
2948 {
2949 int i;
2950 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2951 if (cp_parser_dependent_template_arg_p
2952 (TREE_VEC_ELT (args, i)))
2953 {
2954 dependent_p = true;
2955 break;
2956 }
2957 }
2958
2959 /* The functions are those referred to by the
2960 template-id. */
2961 fns = TREE_OPERAND (fns, 0);
2962 }
2963
2964 /* If there are no dependent template arguments, go
2965 through the overlaoded functions. */
2966 while (fns && !dependent_p)
2967 {
2968 tree fn = OVL_CURRENT (fns);
2969
2970 /* Member functions of dependent classes are
2971 dependent. */
2972 if (TREE_CODE (fn) == FUNCTION_DECL
2973 && cp_parser_type_dependent_expression_p (fn))
2974 dependent_p = true;
2975 else if (TREE_CODE (fn) == TEMPLATE_DECL
2976 && cp_parser_dependent_template_p (fn))
2977 dependent_p = true;
2978
2979 fns = OVL_NEXT (fns);
2980 }
2981 }
2982
2983 /* If the name was dependent on a template parameter,
2984 we will resolve the name at instantiation time. */
2985 if (dependent_p)
2986 {
2987 /* Create a SCOPE_REF for qualified names. */
2988 if (parser->scope)
2989 {
2990 if (TYPE_P (parser->scope))
2991 *qualifying_class = parser->scope;
2992 return build_nt (SCOPE_REF,
2993 parser->scope,
2994 id_expression);
2995 }
2996 /* A TEMPLATE_ID already contains all the information
2997 we need. */
2998 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2999 return id_expression;
3000 /* Create a LOOKUP_EXPR for other unqualified names. */
3001 return build_min_nt (LOOKUP_EXPR, id_expression);
3002 }
3003
3004 if (parser->scope)
3005 {
3006 decl = (adjust_result_of_qualified_name_lookup
3007 (decl, parser->scope, current_class_type));
3008 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3009 *qualifying_class = parser->scope;
3010 }
3011 /* Resolve references to variables of anonymous unions
3012 into COMPONENT_REFs. */
3013 else if (TREE_CODE (decl) == ALIAS_DECL)
3014 decl = DECL_INITIAL (decl);
3015 else
3016 /* Transform references to non-static data members into
3017 COMPONENT_REFs. */
3018 decl = hack_identifier (decl, id_expression);
3019 }
3020
3021 if (TREE_DEPRECATED (decl))
3022 warn_deprecated_use (decl);
3023
3024 return decl;
3025 }
3026
3027 /* Anything else is an error. */
3028 default:
3029 cp_parser_error (parser, "expected primary-expression");
3030 return error_mark_node;
3031 }
3032}
3033
3034/* Parse an id-expression.
3035
3036 id-expression:
3037 unqualified-id
3038 qualified-id
3039
3040 qualified-id:
3041 :: [opt] nested-name-specifier template [opt] unqualified-id
3042 :: identifier
3043 :: operator-function-id
3044 :: template-id
3045
3046 Return a representation of the unqualified portion of the
3047 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3048 a `::' or nested-name-specifier.
3049
3050 Often, if the id-expression was a qualified-id, the caller will
3051 want to make a SCOPE_REF to represent the qualified-id. This
3052 function does not do this in order to avoid wastefully creating
3053 SCOPE_REFs when they are not required.
3054
3055 If ASSUME_TYPENAME_P is true then we assume that qualified names
3056 are typenames. This flag is set when parsing a declarator-id;
3057 for something like:
3058
3059 template <class T>
3060 int S<T>::R::i = 3;
3061
3062 we are supposed to assume that `S<T>::R' is a class.
3063
3064 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3065 `template' keyword.
3066
3067 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3068 uninstantiated templates.
3069
3070 If *TEMPLATE_KEYWORD_P is non-NULL, it is set to true iff the
3071 `template' keyword is used to explicitly indicate that the entity
3072 named is a template. */
3073
3074static tree
3075cp_parser_id_expression (cp_parser *parser,
3076 bool template_keyword_p,
3077 bool check_dependency_p,
3078 bool *template_p)
3079{
3080 bool global_scope_p;
3081 bool nested_name_specifier_p;
3082
3083 /* Assume the `template' keyword was not used. */
3084 if (template_p)
3085 *template_p = false;
3086
3087 /* Look for the optional `::' operator. */
3088 global_scope_p
3089 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3090 != NULL_TREE);
3091 /* Look for the optional nested-name-specifier. */
3092 nested_name_specifier_p
3093 = (cp_parser_nested_name_specifier_opt (parser,
3094 /*typename_keyword_p=*/false,
3095 check_dependency_p,
3096 /*type_p=*/false)
3097 != NULL_TREE);
3098 /* If there is a nested-name-specifier, then we are looking at
3099 the first qualified-id production. */
3100 if (nested_name_specifier_p)
3101 {
3102 tree saved_scope;
3103 tree saved_object_scope;
3104 tree saved_qualifying_scope;
3105 tree unqualified_id;
3106 bool is_template;
3107
3108 /* See if the next token is the `template' keyword. */
3109 if (!template_p)
3110 template_p = &is_template;
3111 *template_p = cp_parser_optional_template_keyword (parser);
3112 /* Name lookup we do during the processing of the
3113 unqualified-id might obliterate SCOPE. */
3114 saved_scope = parser->scope;
3115 saved_object_scope = parser->object_scope;
3116 saved_qualifying_scope = parser->qualifying_scope;
3117 /* Process the final unqualified-id. */
3118 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3119 check_dependency_p);
3120 /* Restore the SAVED_SCOPE for our caller. */
3121 parser->scope = saved_scope;
3122 parser->object_scope = saved_object_scope;
3123 parser->qualifying_scope = saved_qualifying_scope;
3124
3125 return unqualified_id;
3126 }
3127 /* Otherwise, if we are in global scope, then we are looking at one
3128 of the other qualified-id productions. */
3129 else if (global_scope_p)
3130 {
3131 cp_token *token;
3132 tree id;
3133
2c593bd0 3134 /* Peek at the next token. */
3135 token = cp_lexer_peek_token (parser->lexer);
3136
3137 /* If it's an identifier, and the next token is not a "<", then
3138 we can avoid the template-id case. This is an optimization
3139 for this common case. */
3140 if (token->type == CPP_NAME
3141 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3142 return cp_parser_identifier (parser);
3143
0a3b29ad 3144 cp_parser_parse_tentatively (parser);
3145 /* Try a template-id. */
3146 id = cp_parser_template_id (parser,
3147 /*template_keyword_p=*/false,
3148 /*check_dependency_p=*/true);
3149 /* If that worked, we're done. */
3150 if (cp_parser_parse_definitely (parser))
3151 return id;
3152
2c593bd0 3153 /* Peek at the next token. (Changes in the token buffer may
3154 have invalidated the pointer obtained above.) */
0a3b29ad 3155 token = cp_lexer_peek_token (parser->lexer);
3156
3157 switch (token->type)
3158 {
3159 case CPP_NAME:
3160 return cp_parser_identifier (parser);
3161
3162 case CPP_KEYWORD:
3163 if (token->keyword == RID_OPERATOR)
3164 return cp_parser_operator_function_id (parser);
3165 /* Fall through. */
3166
3167 default:
3168 cp_parser_error (parser, "expected id-expression");
3169 return error_mark_node;
3170 }
3171 }
3172 else
3173 return cp_parser_unqualified_id (parser, template_keyword_p,
3174 /*check_dependency_p=*/true);
3175}
3176
3177/* Parse an unqualified-id.
3178
3179 unqualified-id:
3180 identifier
3181 operator-function-id
3182 conversion-function-id
3183 ~ class-name
3184 template-id
3185
3186 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3187 keyword, in a construct like `A::template ...'.
3188
3189 Returns a representation of unqualified-id. For the `identifier'
3190 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3191 production a BIT_NOT_EXPR is returned; the operand of the
3192 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3193 other productions, see the documentation accompanying the
3194 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3195 names are looked up in uninstantiated templates. */
3196
3197static tree
3198cp_parser_unqualified_id (parser, template_keyword_p,
3199 check_dependency_p)
3200 cp_parser *parser;
3201 bool template_keyword_p;
3202 bool check_dependency_p;
3203{
3204 cp_token *token;
3205
3206 /* Peek at the next token. */
3207 token = cp_lexer_peek_token (parser->lexer);
3208
3209 switch (token->type)
3210 {
3211 case CPP_NAME:
3212 {
3213 tree id;
3214
3215 /* We don't know yet whether or not this will be a
3216 template-id. */
3217 cp_parser_parse_tentatively (parser);
3218 /* Try a template-id. */
3219 id = cp_parser_template_id (parser, template_keyword_p,
3220 check_dependency_p);
3221 /* If it worked, we're done. */
3222 if (cp_parser_parse_definitely (parser))
3223 return id;
3224 /* Otherwise, it's an ordinary identifier. */
3225 return cp_parser_identifier (parser);
3226 }
3227
3228 case CPP_TEMPLATE_ID:
3229 return cp_parser_template_id (parser, template_keyword_p,
3230 check_dependency_p);
3231
3232 case CPP_COMPL:
3233 {
3234 tree type_decl;
3235 tree qualifying_scope;
3236 tree object_scope;
3237 tree scope;
3238
3239 /* Consume the `~' token. */
3240 cp_lexer_consume_token (parser->lexer);
3241 /* Parse the class-name. The standard, as written, seems to
3242 say that:
3243
3244 template <typename T> struct S { ~S (); };
3245 template <typename T> S<T>::~S() {}
3246
3247 is invalid, since `~' must be followed by a class-name, but
3248 `S<T>' is dependent, and so not known to be a class.
3249 That's not right; we need to look in uninstantiated
3250 templates. A further complication arises from:
3251
3252 template <typename T> void f(T t) {
3253 t.T::~T();
3254 }
3255
3256 Here, it is not possible to look up `T' in the scope of `T'
3257 itself. We must look in both the current scope, and the
3258 scope of the containing complete expression.
3259
3260 Yet another issue is:
3261
3262 struct S {
3263 int S;
3264 ~S();
3265 };
3266
3267 S::~S() {}
3268
3269 The standard does not seem to say that the `S' in `~S'
3270 should refer to the type `S' and not the data member
3271 `S::S'. */
3272
3273 /* DR 244 says that we look up the name after the "~" in the
3274 same scope as we looked up the qualifying name. That idea
3275 isn't fully worked out; it's more complicated than that. */
3276 scope = parser->scope;
3277 object_scope = parser->object_scope;
3278 qualifying_scope = parser->qualifying_scope;
3279
3280 /* If the name is of the form "X::~X" it's OK. */
3281 if (scope && TYPE_P (scope)
3282 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3283 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3284 == CPP_OPEN_PAREN)
3285 && (cp_lexer_peek_token (parser->lexer)->value
3286 == TYPE_IDENTIFIER (scope)))
3287 {
3288 cp_lexer_consume_token (parser->lexer);
3289 return build_nt (BIT_NOT_EXPR, scope);
3290 }
3291
3292 /* If there was an explicit qualification (S::~T), first look
3293 in the scope given by the qualification (i.e., S). */
3294 if (scope)
3295 {
3296 cp_parser_parse_tentatively (parser);
3297 type_decl = cp_parser_class_name (parser,
3298 /*typename_keyword_p=*/false,
3299 /*template_keyword_p=*/false,
3300 /*type_p=*/false,
3301 /*check_access_p=*/true,
3302 /*check_dependency=*/false,
3303 /*class_head_p=*/false);
3304 if (cp_parser_parse_definitely (parser))
3305 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3306 }
3307 /* In "N::S::~S", look in "N" as well. */
3308 if (scope && qualifying_scope)
3309 {
3310 cp_parser_parse_tentatively (parser);
3311 parser->scope = qualifying_scope;
3312 parser->object_scope = NULL_TREE;
3313 parser->qualifying_scope = NULL_TREE;
3314 type_decl
3315 = cp_parser_class_name (parser,
3316 /*typename_keyword_p=*/false,
3317 /*template_keyword_p=*/false,
3318 /*type_p=*/false,
3319 /*check_access_p=*/true,
3320 /*check_dependency=*/false,
3321 /*class_head_p=*/false);
3322 if (cp_parser_parse_definitely (parser))
3323 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3324 }
3325 /* In "p->S::~T", look in the scope given by "*p" as well. */
3326 else if (object_scope)
3327 {
3328 cp_parser_parse_tentatively (parser);
3329 parser->scope = object_scope;
3330 parser->object_scope = NULL_TREE;
3331 parser->qualifying_scope = NULL_TREE;
3332 type_decl
3333 = cp_parser_class_name (parser,
3334 /*typename_keyword_p=*/false,
3335 /*template_keyword_p=*/false,
3336 /*type_p=*/false,
3337 /*check_access_p=*/true,
3338 /*check_dependency=*/false,
3339 /*class_head_p=*/false);
3340 if (cp_parser_parse_definitely (parser))
3341 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3342 }
3343 /* Look in the surrounding context. */
3344 parser->scope = NULL_TREE;
3345 parser->object_scope = NULL_TREE;
3346 parser->qualifying_scope = NULL_TREE;
3347 type_decl
3348 = cp_parser_class_name (parser,
3349 /*typename_keyword_p=*/false,
3350 /*template_keyword_p=*/false,
3351 /*type_p=*/false,
3352 /*check_access_p=*/true,
3353 /*check_dependency=*/false,
3354 /*class_head_p=*/false);
3355 /* If an error occurred, assume that the name of the
3356 destructor is the same as the name of the qualifying
3357 class. That allows us to keep parsing after running
3358 into ill-formed destructor names. */
3359 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3360 return build_nt (BIT_NOT_EXPR, scope);
3361 else if (type_decl == error_mark_node)
3362 return error_mark_node;
3363
3364 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3365 }
3366
3367 case CPP_KEYWORD:
3368 if (token->keyword == RID_OPERATOR)
3369 {
3370 tree id;
3371
3372 /* This could be a template-id, so we try that first. */
3373 cp_parser_parse_tentatively (parser);
3374 /* Try a template-id. */
3375 id = cp_parser_template_id (parser, template_keyword_p,
3376 /*check_dependency_p=*/true);
3377 /* If that worked, we're done. */
3378 if (cp_parser_parse_definitely (parser))
3379 return id;
3380 /* We still don't know whether we're looking at an
3381 operator-function-id or a conversion-function-id. */
3382 cp_parser_parse_tentatively (parser);
3383 /* Try an operator-function-id. */
3384 id = cp_parser_operator_function_id (parser);
3385 /* If that didn't work, try a conversion-function-id. */
3386 if (!cp_parser_parse_definitely (parser))
3387 id = cp_parser_conversion_function_id (parser);
3388
3389 return id;
3390 }
3391 /* Fall through. */
3392
3393 default:
3394 cp_parser_error (parser, "expected unqualified-id");
3395 return error_mark_node;
3396 }
3397}
3398
3399/* Parse an (optional) nested-name-specifier.
3400
3401 nested-name-specifier:
3402 class-or-namespace-name :: nested-name-specifier [opt]
3403 class-or-namespace-name :: template nested-name-specifier [opt]
3404
3405 PARSER->SCOPE should be set appropriately before this function is
3406 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3407 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3408 in name lookups.
3409
3410 Sets PARSER->SCOPE to the class (TYPE) or namespace
3411 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3412 it unchanged if there is no nested-name-specifier. Returns the new
3413 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3414
3415static tree
3416cp_parser_nested_name_specifier_opt (cp_parser *parser,
3417 bool typename_keyword_p,
3418 bool check_dependency_p,
3419 bool type_p)
3420{
3421 bool success = false;
3422 tree access_check = NULL_TREE;
3423 ptrdiff_t start;
3424
3425 /* If the next token corresponds to a nested name specifier, there
3426 is no need to reparse it. */
3427 if (cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3428 {
3429 tree value;
3430 tree check;
3431
3432 /* Get the stored value. */
3433 value = cp_lexer_consume_token (parser->lexer)->value;
3434 /* Perform any access checks that were deferred. */
3435 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
3436 cp_parser_defer_access_check (parser,
3437 TREE_PURPOSE (check),
3438 TREE_VALUE (check));
3439 /* Set the scope from the stored value. */
3440 parser->scope = TREE_VALUE (value);
3441 parser->qualifying_scope = TREE_TYPE (value);
3442 parser->object_scope = NULL_TREE;
3443 return parser->scope;
3444 }
3445
3446 /* Remember where the nested-name-specifier starts. */
3447 if (cp_parser_parsing_tentatively (parser)
3448 && !cp_parser_committed_to_tentative_parse (parser))
3449 {
3450 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
3451 start = cp_lexer_token_difference (parser->lexer,
3452 parser->lexer->first_token,
3453 next_token);
3454 access_check = parser->context->deferred_access_checks;
3455 }
3456 else
3457 start = -1;
3458
3459 while (true)
3460 {
3461 tree new_scope;
3462 tree old_scope;
3463 tree saved_qualifying_scope;
3464 cp_token *token;
3465 bool template_keyword_p;
3466
3467 /* Spot cases that cannot be the beginning of a
3468 nested-name-specifier. On the second and subsequent times
3469 through the loop, we look for the `template' keyword. */
3470 if (success
3471 && cp_lexer_next_token_is_keyword (parser->lexer,
3472 RID_TEMPLATE))
3473 ;
3474 /* A template-id can start a nested-name-specifier. */
3475 else if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
3476 ;
3477 else
3478 {
3479 /* If the next token is not an identifier, then it is
3480 definitely not a class-or-namespace-name. */
3481 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
3482 break;
3483 /* If the following token is neither a `<' (to begin a
3484 template-id), nor a `::', then we are not looking at a
3485 nested-name-specifier. */
3486 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3487 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3488 break;
3489 }
3490
3491 /* The nested-name-specifier is optional, so we parse
3492 tentatively. */
3493 cp_parser_parse_tentatively (parser);
3494
3495 /* Look for the optional `template' keyword, if this isn't the
3496 first time through the loop. */
3497 if (success)
3498 template_keyword_p = cp_parser_optional_template_keyword (parser);
3499 else
3500 template_keyword_p = false;
3501
3502 /* Save the old scope since the name lookup we are about to do
3503 might destroy it. */
3504 old_scope = parser->scope;
3505 saved_qualifying_scope = parser->qualifying_scope;
3506 /* Parse the qualifying entity. */
3507 new_scope
3508 = cp_parser_class_or_namespace_name (parser,
3509 typename_keyword_p,
3510 template_keyword_p,
3511 check_dependency_p,
3512 type_p);
3513 /* Look for the `::' token. */
3514 cp_parser_require (parser, CPP_SCOPE, "`::'");
3515
3516 /* If we found what we wanted, we keep going; otherwise, we're
3517 done. */
3518 if (!cp_parser_parse_definitely (parser))
3519 {
3520 bool error_p = false;
3521
3522 /* Restore the OLD_SCOPE since it was valid before the
3523 failed attempt at finding the last
3524 class-or-namespace-name. */
3525 parser->scope = old_scope;
3526 parser->qualifying_scope = saved_qualifying_scope;
3527 /* If the next token is an identifier, and the one after
3528 that is a `::', then any valid interpretation would have
3529 found a class-or-namespace-name. */
3530 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3531 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3532 == CPP_SCOPE)
3533 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3534 != CPP_COMPL))
3535 {
3536 token = cp_lexer_consume_token (parser->lexer);
3537 if (!error_p)
3538 {
3539 tree decl;
3540
3541 decl = cp_parser_lookup_name_simple (parser, token->value);
3542 if (TREE_CODE (decl) == TEMPLATE_DECL)
3543 error ("`%D' used without template parameters",
3544 decl);
3545 else if (parser->scope)
3546 {
3547 if (TYPE_P (parser->scope))
3548 error ("`%T::%D' is not a class-name or "
3549 "namespace-name",
3550 parser->scope, token->value);
3551 else
3552 error ("`%D::%D' is not a class-name or "
3553 "namespace-name",
3554 parser->scope, token->value);
3555 }
3556 else
3557 error ("`%D' is not a class-name or namespace-name",
3558 token->value);
3559 parser->scope = NULL_TREE;
3560 error_p = true;
6fc758aa 3561 /* Treat this as a successful nested-name-specifier
3562 due to:
3563
3564 [basic.lookup.qual]
3565
3566 If the name found is not a class-name (clause
3567 _class_) or namespace-name (_namespace.def_), the
3568 program is ill-formed. */
3569 success = true;
0a3b29ad 3570 }
3571 cp_lexer_consume_token (parser->lexer);
3572 }
3573 break;
3574 }
3575
3576 /* We've found one valid nested-name-specifier. */
3577 success = true;
3578 /* Make sure we look in the right scope the next time through
3579 the loop. */
3580 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3581 ? TREE_TYPE (new_scope)
3582 : new_scope);
3583 /* If it is a class scope, try to complete it; we are about to
3584 be looking up names inside the class. */
3585 if (TYPE_P (parser->scope))
3586 complete_type (parser->scope);
3587 }
3588
3589 /* If parsing tentatively, replace the sequence of tokens that makes
3590 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3591 token. That way, should we re-parse the token stream, we will
3592 not have to repeat the effort required to do the parse, nor will
3593 we issue duplicate error messages. */
3594 if (success && start >= 0)
3595 {
3596 cp_token *token;
3597 tree c;
3598
3599 /* Find the token that corresponds to the start of the
3600 template-id. */
3601 token = cp_lexer_advance_token (parser->lexer,
3602 parser->lexer->first_token,
3603 start);
3604
3605 /* Remember the access checks associated with this
3606 nested-name-specifier. */
3607 c = parser->context->deferred_access_checks;
3608 if (c == access_check)
3609 access_check = NULL_TREE;
3610 else
3611 {
3612 while (TREE_CHAIN (c) != access_check)
3613 c = TREE_CHAIN (c);
3614 access_check = parser->context->deferred_access_checks;
3615 parser->context->deferred_access_checks = TREE_CHAIN (c);
3616 TREE_CHAIN (c) = NULL_TREE;
3617 }
3618
3619 /* Reset the contents of the START token. */
3620 token->type = CPP_NESTED_NAME_SPECIFIER;
3621 token->value = build_tree_list (access_check, parser->scope);
3622 TREE_TYPE (token->value) = parser->qualifying_scope;
3623 token->keyword = RID_MAX;
3624 /* Purge all subsequent tokens. */
3625 cp_lexer_purge_tokens_after (parser->lexer, token);
3626 }
3627
3628 return success ? parser->scope : NULL_TREE;
3629}
3630
3631/* Parse a nested-name-specifier. See
3632 cp_parser_nested_name_specifier_opt for details. This function
3633 behaves identically, except that it will an issue an error if no
3634 nested-name-specifier is present, and it will return
3635 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3636 is present. */
3637
3638static tree
3639cp_parser_nested_name_specifier (cp_parser *parser,
3640 bool typename_keyword_p,
3641 bool check_dependency_p,
3642 bool type_p)
3643{
3644 tree scope;
3645
3646 /* Look for the nested-name-specifier. */
3647 scope = cp_parser_nested_name_specifier_opt (parser,
3648 typename_keyword_p,
3649 check_dependency_p,
3650 type_p);
3651 /* If it was not present, issue an error message. */
3652 if (!scope)
3653 {
3654 cp_parser_error (parser, "expected nested-name-specifier");
3655 return error_mark_node;
3656 }
3657
3658 return scope;
3659}
3660
3661/* Parse a class-or-namespace-name.
3662
3663 class-or-namespace-name:
3664 class-name
3665 namespace-name
3666
3667 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3668 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3669 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3670 TYPE_P is TRUE iff the next name should be taken as a class-name,
3671 even the same name is declared to be another entity in the same
3672 scope.
3673
3674 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6fc758aa 3675 specified by the class-or-namespace-name. If neither is found the
3676 ERROR_MARK_NODE is returned. */
0a3b29ad 3677
3678static tree
3679cp_parser_class_or_namespace_name (cp_parser *parser,
3680 bool typename_keyword_p,
3681 bool template_keyword_p,
3682 bool check_dependency_p,
3683 bool type_p)
3684{
3685 tree saved_scope;
3686 tree saved_qualifying_scope;
3687 tree saved_object_scope;
3688 tree scope;
6fc758aa 3689 bool only_class_p;
0a3b29ad 3690
3691 /* If the next token is the `template' keyword, we know that we are
3692 looking at a class-name. */
3693 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3694 return cp_parser_class_name (parser,
3695 typename_keyword_p,
3696 template_keyword_p,
3697 type_p,
3698 /*check_access_p=*/true,
3699 check_dependency_p,
3700 /*class_head_p=*/false);
3701 /* Before we try to parse the class-name, we must save away the
3702 current PARSER->SCOPE since cp_parser_class_name will destroy
3703 it. */
3704 saved_scope = parser->scope;
3705 saved_qualifying_scope = parser->qualifying_scope;
3706 saved_object_scope = parser->object_scope;
6fc758aa 3707 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3708 there is no need to look for a namespace-name. */
3709 only_class_p = saved_scope && TYPE_P (saved_scope);
3710 if (!only_class_p)
3711 cp_parser_parse_tentatively (parser);
0a3b29ad 3712 scope = cp_parser_class_name (parser,
3713 typename_keyword_p,
3714 template_keyword_p,
3715 type_p,
3716 /*check_access_p=*/true,
3717 check_dependency_p,
3718 /*class_head_p=*/false);
3719 /* If that didn't work, try for a namespace-name. */
6fc758aa 3720 if (!only_class_p && !cp_parser_parse_definitely (parser))
0a3b29ad 3721 {
3722 /* Restore the saved scope. */
3723 parser->scope = saved_scope;
3724 parser->qualifying_scope = saved_qualifying_scope;
3725 parser->object_scope = saved_object_scope;
6fc758aa 3726 /* If we are not looking at an identifier followed by the scope
3727 resolution operator, then this is not part of a
3728 nested-name-specifier. (Note that this function is only used
3729 to parse the components of a nested-name-specifier.) */
3730 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3731 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3732 return error_mark_node;
0a3b29ad 3733 scope = cp_parser_namespace_name (parser);
3734 }
3735
3736 return scope;
3737}
3738
3739/* Parse a postfix-expression.
3740
3741 postfix-expression:
3742 primary-expression
3743 postfix-expression [ expression ]
3744 postfix-expression ( expression-list [opt] )
3745 simple-type-specifier ( expression-list [opt] )
3746 typename :: [opt] nested-name-specifier identifier
3747 ( expression-list [opt] )
3748 typename :: [opt] nested-name-specifier template [opt] template-id
3749 ( expression-list [opt] )
3750 postfix-expression . template [opt] id-expression
3751 postfix-expression -> template [opt] id-expression
3752 postfix-expression . pseudo-destructor-name
3753 postfix-expression -> pseudo-destructor-name
3754 postfix-expression ++
3755 postfix-expression --
3756 dynamic_cast < type-id > ( expression )
3757 static_cast < type-id > ( expression )
3758 reinterpret_cast < type-id > ( expression )
3759 const_cast < type-id > ( expression )
3760 typeid ( expression )
3761 typeid ( type-id )
3762
3763 GNU Extension:
3764
3765 postfix-expression:
3766 ( type-id ) { initializer-list , [opt] }
3767
3768 This extension is a GNU version of the C99 compound-literal
3769 construct. (The C99 grammar uses `type-name' instead of `type-id',
3770 but they are essentially the same concept.)
3771
3772 If ADDRESS_P is true, the postfix expression is the operand of the
3773 `&' operator.
3774
3775 Returns a representation of the expression. */
3776
3777static tree
3778cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3779{
3780 cp_token *token;
3781 enum rid keyword;
3782 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3783 tree postfix_expression = NULL_TREE;
3784 /* Non-NULL only if the current postfix-expression can be used to
3785 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3786 class used to qualify the member. */
3787 tree qualifying_class = NULL_TREE;
3788 bool done;
3789
3790 /* Peek at the next token. */
3791 token = cp_lexer_peek_token (parser->lexer);
3792 /* Some of the productions are determined by keywords. */
3793 keyword = token->keyword;
3794 switch (keyword)
3795 {
3796 case RID_DYNCAST:
3797 case RID_STATCAST:
3798 case RID_REINTCAST:
3799 case RID_CONSTCAST:
3800 {
3801 tree type;
3802 tree expression;
3803 const char *saved_message;
3804
3805 /* All of these can be handled in the same way from the point
3806 of view of parsing. Begin by consuming the token
3807 identifying the cast. */
3808 cp_lexer_consume_token (parser->lexer);
3809
3810 /* New types cannot be defined in the cast. */
3811 saved_message = parser->type_definition_forbidden_message;
3812 parser->type_definition_forbidden_message
3813 = "types may not be defined in casts";
3814
3815 /* Look for the opening `<'. */
3816 cp_parser_require (parser, CPP_LESS, "`<'");
3817 /* Parse the type to which we are casting. */
3818 type = cp_parser_type_id (parser);
3819 /* Look for the closing `>'. */
3820 cp_parser_require (parser, CPP_GREATER, "`>'");
3821 /* Restore the old message. */
3822 parser->type_definition_forbidden_message = saved_message;
3823
3824 /* And the expression which is being cast. */
3825 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3826 expression = cp_parser_expression (parser);
3827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3828
3829 switch (keyword)
3830 {
3831 case RID_DYNCAST:
3832 postfix_expression
3833 = build_dynamic_cast (type, expression);
3834 break;
3835 case RID_STATCAST:
3836 postfix_expression
3837 = build_static_cast (type, expression);
3838 break;
3839 case RID_REINTCAST:
3840 postfix_expression
3841 = build_reinterpret_cast (type, expression);
3842 break;
3843 case RID_CONSTCAST:
3844 postfix_expression
3845 = build_const_cast (type, expression);
3846 break;
3847 default:
3848 abort ();
3849 }
3850 }
3851 break;
3852
3853 case RID_TYPEID:
3854 {
3855 tree type;
3856 const char *saved_message;
3857
3858 /* Consume the `typeid' token. */
3859 cp_lexer_consume_token (parser->lexer);
3860 /* Look for the `(' token. */
3861 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3862 /* Types cannot be defined in a `typeid' expression. */
3863 saved_message = parser->type_definition_forbidden_message;
3864 parser->type_definition_forbidden_message
3865 = "types may not be defined in a `typeid\' expression";
3866 /* We can't be sure yet whether we're looking at a type-id or an
3867 expression. */
3868 cp_parser_parse_tentatively (parser);
3869 /* Try a type-id first. */
3870 type = cp_parser_type_id (parser);
3871 /* Look for the `)' token. Otherwise, we can't be sure that
3872 we're not looking at an expression: consider `typeid (int
3873 (3))', for example. */
3874 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3875 /* If all went well, simply lookup the type-id. */
3876 if (cp_parser_parse_definitely (parser))
3877 postfix_expression = get_typeid (type);
3878 /* Otherwise, fall back to the expression variant. */
3879 else
3880 {
3881 tree expression;
3882
3883 /* Look for an expression. */
3884 expression = cp_parser_expression (parser);
3885 /* Compute its typeid. */
3886 postfix_expression = build_typeid (expression);
3887 /* Look for the `)' token. */
3888 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3889 }
3890
3891 /* Restore the saved message. */
3892 parser->type_definition_forbidden_message = saved_message;
3893 }
3894 break;
3895
3896 case RID_TYPENAME:
3897 {
3898 bool template_p = false;
3899 tree id;
3900 tree type;
3901
3902 /* Consume the `typename' token. */
3903 cp_lexer_consume_token (parser->lexer);
3904 /* Look for the optional `::' operator. */
3905 cp_parser_global_scope_opt (parser,
3906 /*current_scope_valid_p=*/false);
3907 /* Look for the nested-name-specifier. */
3908 cp_parser_nested_name_specifier (parser,
3909 /*typename_keyword_p=*/true,
3910 /*check_dependency_p=*/true,
3911 /*type_p=*/true);
3912 /* Look for the optional `template' keyword. */
3913 template_p = cp_parser_optional_template_keyword (parser);
3914 /* We don't know whether we're looking at a template-id or an
3915 identifier. */
3916 cp_parser_parse_tentatively (parser);
3917 /* Try a template-id. */
3918 id = cp_parser_template_id (parser, template_p,
3919 /*check_dependency_p=*/true);
3920 /* If that didn't work, try an identifier. */
3921 if (!cp_parser_parse_definitely (parser))
3922 id = cp_parser_identifier (parser);
3923 /* Create a TYPENAME_TYPE to represent the type to which the
3924 functional cast is being performed. */
3925 type = make_typename_type (parser->scope, id,
3926 /*complain=*/1);
3927
3928 postfix_expression = cp_parser_functional_cast (parser, type);
3929 }
3930 break;
3931
3932 default:
3933 {
3934 tree type;
3935
3936 /* If the next thing is a simple-type-specifier, we may be
3937 looking at a functional cast. We could also be looking at
3938 an id-expression. So, we try the functional cast, and if
3939 that doesn't work we fall back to the primary-expression. */
3940 cp_parser_parse_tentatively (parser);
3941 /* Look for the simple-type-specifier. */
3942 type = cp_parser_simple_type_specifier (parser,
3943 CP_PARSER_FLAGS_NONE);
3944 /* Parse the cast itself. */
3945 if (!cp_parser_error_occurred (parser))
3946 postfix_expression
3947 = cp_parser_functional_cast (parser, type);
3948 /* If that worked, we're done. */
3949 if (cp_parser_parse_definitely (parser))
3950 break;
3951
3952 /* If the functional-cast didn't work out, try a
3953 compound-literal. */
3954 if (cp_parser_allow_gnu_extensions_p (parser))
3955 {
3956 tree initializer_list = NULL_TREE;
3957
3958 cp_parser_parse_tentatively (parser);
3959 /* Look for the `('. */
3960 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
3961 {
3962 type = cp_parser_type_id (parser);
3963 /* Look for the `)'. */
3964 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3965 /* Look for the `{'. */
3966 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3967 /* If things aren't going well, there's no need to
3968 keep going. */
3969 if (!cp_parser_error_occurred (parser))
3970 {
3971 /* Parse the initializer-list. */
3972 initializer_list
3973 = cp_parser_initializer_list (parser);
3974 /* Allow a trailing `,'. */
3975 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3976 cp_lexer_consume_token (parser->lexer);
3977 /* Look for the final `}'. */
3978 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3979 }
3980 }
3981 /* If that worked, we're definitely looking at a
3982 compound-literal expression. */
3983 if (cp_parser_parse_definitely (parser))
3984 {
3985 /* Warn the user that a compound literal is not
3986 allowed in standard C++. */
3987 if (pedantic)
3988 pedwarn ("ISO C++ forbids compound-literals");
3989 /* Form the representation of the compound-literal. */
3990 postfix_expression
3991 = finish_compound_literal (type, initializer_list);
3992 break;
3993 }
3994 }
3995
3996 /* It must be a primary-expression. */
3997 postfix_expression = cp_parser_primary_expression (parser,
3998 &idk,
3999 &qualifying_class);
4000 }
4001 break;
4002 }
4003
4004 /* Peek at the next token. */
4005 token = cp_lexer_peek_token (parser->lexer);
4006 done = (token->type != CPP_OPEN_SQUARE
4007 && token->type != CPP_OPEN_PAREN
4008 && token->type != CPP_DOT
4009 && token->type != CPP_DEREF
4010 && token->type != CPP_PLUS_PLUS
4011 && token->type != CPP_MINUS_MINUS);
4012
4013 /* If the postfix expression is complete, finish up. */
4014 if (address_p && qualifying_class && done)
4015 {
4016 if (TREE_CODE (postfix_expression) == SCOPE_REF)
4017 postfix_expression = TREE_OPERAND (postfix_expression, 1);
4018 postfix_expression
4019 = build_offset_ref (qualifying_class, postfix_expression);
4020 return postfix_expression;
4021 }
4022
4023 /* Otherwise, if we were avoiding committing until we knew
4024 whether or not we had a pointer-to-member, we now know that
4025 the expression is an ordinary reference to a qualified name. */
4026 if (qualifying_class && !processing_template_decl)
4027 {
4028 if (TREE_CODE (postfix_expression) == FIELD_DECL)
4029 postfix_expression
4030 = finish_non_static_data_member (postfix_expression,
4031 qualifying_class);
4032 else if (BASELINK_P (postfix_expression))
4033 {
4034 tree fn;
4035 tree fns;
4036
4037 /* See if any of the functions are non-static members. */
4038 fns = BASELINK_FUNCTIONS (postfix_expression);
4039 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4040 fns = TREE_OPERAND (fns, 0);
4041 for (fn = fns; fn; fn = OVL_NEXT (fn))
4042 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4043 break;
4044 /* If so, the expression may be relative to the current
4045 class. */
4046 if (fn && current_class_type
4047 && DERIVED_FROM_P (qualifying_class, current_class_type))
4048 postfix_expression
4049 = (build_class_member_access_expr
4050 (maybe_dummy_object (qualifying_class, NULL),
4051 postfix_expression,
4052 BASELINK_ACCESS_BINFO (postfix_expression),
4053 /*preserve_reference=*/false));
4054 else if (done)
4055 return build_offset_ref (qualifying_class,
4056 postfix_expression);
4057 }
4058 }
4059
4060 /* Remember that there was a reference to this entity. */
4061 if (DECL_P (postfix_expression))
4062 mark_used (postfix_expression);
4063
4064 /* Keep looping until the postfix-expression is complete. */
4065 while (true)
4066 {
4067 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4068 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4069 {
4070 /* It is not a Koenig lookup function call. */
4071 unqualified_name_lookup_error (postfix_expression);
4072 postfix_expression = error_mark_node;
4073 }
4074
4075 /* Peek at the next token. */
4076 token = cp_lexer_peek_token (parser->lexer);
4077
4078 switch (token->type)
4079 {
4080 case CPP_OPEN_SQUARE:
4081 /* postfix-expression [ expression ] */
4082 {
4083 tree index;
4084
4085 /* Consume the `[' token. */
4086 cp_lexer_consume_token (parser->lexer);
4087 /* Parse the index expression. */
4088 index = cp_parser_expression (parser);
4089 /* Look for the closing `]'. */
4090 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4091
4092 /* Build the ARRAY_REF. */
4093 postfix_expression
4094 = grok_array_decl (postfix_expression, index);
4095 idk = CP_PARSER_ID_KIND_NONE;
4096 }
4097 break;
4098
4099 case CPP_OPEN_PAREN:
4100 /* postfix-expression ( expression-list [opt] ) */
4101 {
4102 tree args;
4103
4104 /* Consume the `(' token. */
4105 cp_lexer_consume_token (parser->lexer);
4106 /* If the next token is not a `)', then there are some
4107 arguments. */
4108 if (cp_lexer_next_token_is_not (parser->lexer,
4109 CPP_CLOSE_PAREN))
4110 args = cp_parser_expression_list (parser);
4111 else
4112 args = NULL_TREE;
4113 /* Look for the closing `)'. */
4114 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4115
4116 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4117 && (is_overloaded_fn (postfix_expression)
4118 || DECL_P (postfix_expression)
4119 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4120 && args)
4121 {
4122 tree arg;
4123 tree identifier = NULL_TREE;
4124 tree functions = NULL_TREE;
4125
4126 /* Find the name of the overloaded function. */
4127 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4128 identifier = postfix_expression;
4129 else if (is_overloaded_fn (postfix_expression))
4130 {
4131 functions = postfix_expression;
4132 identifier = DECL_NAME (get_first_fn (functions));
4133 }
4134 else if (DECL_P (postfix_expression))
4135 {
4136 functions = postfix_expression;
4137 identifier = DECL_NAME (postfix_expression);
4138 }
4139
4140 /* A call to a namespace-scope function using an
4141 unqualified name.
4142
4143 Do Koenig lookup -- unless any of the arguments are
4144 type-dependent. */
4145 for (arg = args; arg; arg = TREE_CHAIN (arg))
4146 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4147 break;
4148 if (!arg)
4149 {
4150 postfix_expression
4151 = lookup_arg_dependent(identifier, functions, args);
4152 if (!postfix_expression)
4153 {
4154 /* The unqualified name could not be resolved. */
4155 unqualified_name_lookup_error (identifier);
4156 postfix_expression = error_mark_node;
4157 }
4158 postfix_expression
4159 = build_call_from_tree (postfix_expression, args,
4160 /*diallow_virtual=*/false);
4161 break;
4162 }
4163 postfix_expression = build_min_nt (LOOKUP_EXPR,
4164 identifier);
4165 }
4166 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4167 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4168 {
4169 /* The unqualified name could not be resolved. */
4170 unqualified_name_lookup_error (postfix_expression);
4171 postfix_expression = error_mark_node;
4172 break;
4173 }
4174
4175 /* In the body of a template, no further processing is
4176 required. */
4177 if (processing_template_decl)
4178 {
4179 postfix_expression = build_nt (CALL_EXPR,
4180 postfix_expression,
4181 args);
4182 break;
4183 }
4184
4185 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4186 postfix_expression
4187 = (build_new_method_call
4188 (TREE_OPERAND (postfix_expression, 0),
4189 TREE_OPERAND (postfix_expression, 1),
4190 args, NULL_TREE,
4191 (idk == CP_PARSER_ID_KIND_QUALIFIED
4192 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4193 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4194 postfix_expression = (build_offset_ref_call_from_tree
4195 (postfix_expression, args));
4196 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
4197 {
4198 /* A call to a static class member, or a
4199 namespace-scope function. */
4200 postfix_expression
4201 = finish_call_expr (postfix_expression, args,
4202 /*disallow_virtual=*/true);
4203 }
4204 else
4205 {
4206 /* All other function calls. */
4207 postfix_expression
4208 = finish_call_expr (postfix_expression, args,
4209 /*disallow_virtual=*/false);
4210 }
4211
4212 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4213 idk = CP_PARSER_ID_KIND_NONE;
4214 }
4215 break;
4216
4217 case CPP_DOT:
4218 case CPP_DEREF:
4219 /* postfix-expression . template [opt] id-expression
4220 postfix-expression . pseudo-destructor-name
4221 postfix-expression -> template [opt] id-expression
4222 postfix-expression -> pseudo-destructor-name */
4223 {
4224 tree name;
4225 bool dependent_p;
4226 bool template_p;
4227 tree scope = NULL_TREE;
4228
4229 /* If this is a `->' operator, dereference the pointer. */
4230 if (token->type == CPP_DEREF)
4231 postfix_expression = build_x_arrow (postfix_expression);
4232 /* Check to see whether or not the expression is
4233 type-dependent. */
4234 dependent_p = (cp_parser_type_dependent_expression_p
4235 (postfix_expression));
4236 /* The identifier following the `->' or `.' is not
4237 qualified. */
4238 parser->scope = NULL_TREE;
4239 parser->qualifying_scope = NULL_TREE;
4240 parser->object_scope = NULL_TREE;
4241 /* Enter the scope corresponding to the type of the object
4242 given by the POSTFIX_EXPRESSION. */
4243 if (!dependent_p
4244 && TREE_TYPE (postfix_expression) != NULL_TREE)
4245 {
4246 scope = TREE_TYPE (postfix_expression);
4247 /* According to the standard, no expression should
4248 ever have reference type. Unfortunately, we do not
4249 currently match the standard in this respect in
4250 that our internal representation of an expression
4251 may have reference type even when the standard says
4252 it does not. Therefore, we have to manually obtain
4253 the underlying type here. */
4254 if (TREE_CODE (scope) == REFERENCE_TYPE)
4255 scope = TREE_TYPE (scope);
4256 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4257 type of the field. We get an OFFSET_TYPE for
4258 something like:
4259
4260 S::T.a ...
4261
4262 Probably, we should not get an OFFSET_TYPE here;
4263 that transformation should be made only if `&S::T'
4264 is written. */
4265 if (TREE_CODE (scope) == OFFSET_TYPE)
4266 scope = TREE_TYPE (scope);
4267 /* The type of the POSTFIX_EXPRESSION must be
4268 complete. */
4269 scope = complete_type_or_else (scope, NULL_TREE);
4270 /* Let the name lookup machinery know that we are
4271 processing a class member access expression. */
4272 parser->context->object_type = scope;
4273 /* If something went wrong, we want to be able to
4274 discern that case, as opposed to the case where
4275 there was no SCOPE due to the type of expression
4276 being dependent. */
4277 if (!scope)
4278 scope = error_mark_node;
4279 }
4280
4281 /* Consume the `.' or `->' operator. */
4282 cp_lexer_consume_token (parser->lexer);
4283 /* If the SCOPE is not a scalar type, we are looking at an
4284 ordinary class member access expression, rather than a
4285 pseudo-destructor-name. */
4286 if (!scope || !SCALAR_TYPE_P (scope))
4287 {
4288 template_p = cp_parser_optional_template_keyword (parser);
4289 /* Parse the id-expression. */
4290 name = cp_parser_id_expression (parser,
4291 template_p,
4292 /*check_dependency_p=*/true,
4293 /*template_p=*/NULL);
4294 /* In general, build a SCOPE_REF if the member name is
4295 qualified. However, if the name was not dependent
4296 and has already been resolved; there is no need to
4297 build the SCOPE_REF. For example;
4298
4299 struct X { void f(); };
4300 template <typename T> void f(T* t) { t->X::f(); }
4301
4302 Even though "t" is dependent, "X::f" is not and has
4303 except that for a BASELINK there is no need to
4304 include scope information. */
4305 if (name != error_mark_node
4306 && !BASELINK_P (name)
4307 && parser->scope)
4308 {
4309 name = build_nt (SCOPE_REF, parser->scope, name);
4310 parser->scope = NULL_TREE;
4311 parser->qualifying_scope = NULL_TREE;
4312 parser->object_scope = NULL_TREE;
4313 }
4314 postfix_expression
4315 = finish_class_member_access_expr (postfix_expression, name);
4316 }
4317 /* Otherwise, try the pseudo-destructor-name production. */
4318 else
4319 {
4320 tree s;
4321 tree type;
4322
4323 /* Parse the pseudo-destructor-name. */
4324 cp_parser_pseudo_destructor_name (parser, &s, &type);
4325 /* Form the call. */
4326 postfix_expression
4327 = finish_pseudo_destructor_expr (postfix_expression,
4328 s, TREE_TYPE (type));
4329 }
4330
4331 /* We no longer need to look up names in the scope of the
4332 object on the left-hand side of the `.' or `->'
4333 operator. */
4334 parser->context->object_type = NULL_TREE;
4335 idk = CP_PARSER_ID_KIND_NONE;
4336 }
4337 break;
4338
4339 case CPP_PLUS_PLUS:
4340 /* postfix-expression ++ */
4341 /* Consume the `++' token. */
4342 cp_lexer_consume_token (parser->lexer);
4343 /* Generate a reprsentation for the complete expression. */
4344 postfix_expression
4345 = finish_increment_expr (postfix_expression,
4346 POSTINCREMENT_EXPR);
4347 idk = CP_PARSER_ID_KIND_NONE;
4348 break;
4349
4350 case CPP_MINUS_MINUS:
4351 /* postfix-expression -- */
4352 /* Consume the `--' token. */
4353 cp_lexer_consume_token (parser->lexer);
4354 /* Generate a reprsentation for the complete expression. */
4355 postfix_expression
4356 = finish_increment_expr (postfix_expression,
4357 POSTDECREMENT_EXPR);
4358 idk = CP_PARSER_ID_KIND_NONE;
4359 break;
4360
4361 default:
4362 return postfix_expression;
4363 }
4364 }
4365
4366 /* We should never get here. */
4367 abort ();
4368 return error_mark_node;
4369}
4370
4371/* Parse an expression-list.
4372
4373 expression-list:
4374 assignment-expression
4375 expression-list, assignment-expression
4376
4377 Returns a TREE_LIST. The TREE_VALUE of each node is a
4378 representation of an assignment-expression. Note that a TREE_LIST
4379 is returned even if there is only a single expression in the list. */
4380
4381static tree
4382cp_parser_expression_list (parser)
4383 cp_parser *parser;
4384{
4385 tree expression_list = NULL_TREE;
4386
4387 /* Consume expressions until there are no more. */
4388 while (true)
4389 {
4390 tree expr;
4391
4392 /* Parse the next assignment-expression. */
4393 expr = cp_parser_assignment_expression (parser);
4394 /* Add it to the list. */
4395 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4396
4397 /* If the next token isn't a `,', then we are done. */
4398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4399 {
4400 /* All uses of expression-list in the grammar are followed
4401 by a `)'. Therefore, if the next token is not a `)' an
4402 error will be issued, unless we are parsing tentatively.
4403 Skip ahead to see if there is another `,' before the `)';
4404 if so, we can go there and recover. */
4405 if (cp_parser_parsing_tentatively (parser)
4406 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4407 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4408 break;
4409 }
4410
4411 /* Otherwise, consume the `,' and keep going. */
4412 cp_lexer_consume_token (parser->lexer);
4413 }
4414
4415 /* We built up the list in reverse order so we must reverse it now. */
4416 return nreverse (expression_list);
4417}
4418
4419/* Parse a pseudo-destructor-name.
4420
4421 pseudo-destructor-name:
4422 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4423 :: [opt] nested-name-specifier template template-id :: ~ type-name
4424 :: [opt] nested-name-specifier [opt] ~ type-name
4425
4426 If either of the first two productions is used, sets *SCOPE to the
4427 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4428 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4429 or ERROR_MARK_NODE if no type-name is present. */
4430
4431static void
4432cp_parser_pseudo_destructor_name (parser, scope, type)
4433 cp_parser *parser;
4434 tree *scope;
4435 tree *type;
4436{
4437 bool nested_name_specifier_p;
4438
4439 /* Look for the optional `::' operator. */
4440 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4441 /* Look for the optional nested-name-specifier. */
4442 nested_name_specifier_p
4443 = (cp_parser_nested_name_specifier_opt (parser,
4444 /*typename_keyword_p=*/false,
4445 /*check_dependency_p=*/true,
4446 /*type_p=*/false)
4447 != NULL_TREE);
4448 /* Now, if we saw a nested-name-specifier, we might be doing the
4449 second production. */
4450 if (nested_name_specifier_p
4451 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4452 {
4453 /* Consume the `template' keyword. */
4454 cp_lexer_consume_token (parser->lexer);
4455 /* Parse the template-id. */
4456 cp_parser_template_id (parser,
4457 /*template_keyword_p=*/true,
4458 /*check_dependency_p=*/false);
4459 /* Look for the `::' token. */
4460 cp_parser_require (parser, CPP_SCOPE, "`::'");
4461 }
4462 /* If the next token is not a `~', then there might be some
4463 additional qualification. */
4464 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4465 {
4466 /* Look for the type-name. */
4467 *scope = TREE_TYPE (cp_parser_type_name (parser));
4468 /* Look for the `::' token. */
4469 cp_parser_require (parser, CPP_SCOPE, "`::'");
4470 }
4471 else
4472 *scope = NULL_TREE;
4473
4474 /* Look for the `~'. */
4475 cp_parser_require (parser, CPP_COMPL, "`~'");
4476 /* Look for the type-name again. We are not responsible for
4477 checking that it matches the first type-name. */
4478 *type = cp_parser_type_name (parser);
4479}
4480
4481/* Parse a unary-expression.
4482
4483 unary-expression:
4484 postfix-expression
4485 ++ cast-expression
4486 -- cast-expression
4487 unary-operator cast-expression
4488 sizeof unary-expression
4489 sizeof ( type-id )
4490 new-expression
4491 delete-expression
4492
4493 GNU Extensions:
4494
4495 unary-expression:
4496 __extension__ cast-expression
4497 __alignof__ unary-expression
4498 __alignof__ ( type-id )
4499 __real__ cast-expression
4500 __imag__ cast-expression
4501 && identifier
4502
4503 ADDRESS_P is true iff the unary-expression is appearing as the
4504 operand of the `&' operator.
4505
4506 Returns a representation of the expresion. */
4507
4508static tree
4509cp_parser_unary_expression (cp_parser *parser, bool address_p)
4510{
4511 cp_token *token;
4512 enum tree_code unary_operator;
4513
4514 /* Peek at the next token. */
4515 token = cp_lexer_peek_token (parser->lexer);
4516 /* Some keywords give away the kind of expression. */
4517 if (token->type == CPP_KEYWORD)
4518 {
4519 enum rid keyword = token->keyword;
4520
4521 switch (keyword)
4522 {
4523 case RID_ALIGNOF:
4524 {
4525 /* Consume the `alignof' token. */
4526 cp_lexer_consume_token (parser->lexer);
4527 /* Parse the operand. */
4528 return finish_alignof (cp_parser_sizeof_operand
4529 (parser, keyword));
4530 }
4531
4532 case RID_SIZEOF:
4533 {
4534 tree operand;
4535
4536 /* Consume the `sizeof' token. */
4537 cp_lexer_consume_token (parser->lexer);
4538 /* Parse the operand. */
4539 operand = cp_parser_sizeof_operand (parser, keyword);
4540
4541 /* If the type of the operand cannot be determined build a
4542 SIZEOF_EXPR. */
4543 if (TYPE_P (operand)
4544 ? cp_parser_dependent_type_p (operand)
4545 : cp_parser_type_dependent_expression_p (operand))
4546 return build_min (SIZEOF_EXPR, size_type_node, operand);
4547 /* Otherwise, compute the constant value. */
4548 else
4549 return finish_sizeof (operand);
4550 }
4551
4552 case RID_NEW:
4553 return cp_parser_new_expression (parser);
4554
4555 case RID_DELETE:
4556 return cp_parser_delete_expression (parser);
4557
4558 case RID_EXTENSION:
4559 {
4560 /* The saved value of the PEDANTIC flag. */
4561 int saved_pedantic;
4562 tree expr;
4563
4564 /* Save away the PEDANTIC flag. */
4565 cp_parser_extension_opt (parser, &saved_pedantic);
4566 /* Parse the cast-expression. */
4567 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4568 /* Restore the PEDANTIC flag. */
4569 pedantic = saved_pedantic;
4570
4571 return expr;
4572 }
4573
4574 case RID_REALPART:
4575 case RID_IMAGPART:
4576 {
4577 tree expression;
4578
4579 /* Consume the `__real__' or `__imag__' token. */
4580 cp_lexer_consume_token (parser->lexer);
4581 /* Parse the cast-expression. */
4582 expression = cp_parser_cast_expression (parser,
4583 /*address_p=*/false);
4584 /* Create the complete representation. */
4585 return build_x_unary_op ((keyword == RID_REALPART
4586 ? REALPART_EXPR : IMAGPART_EXPR),
4587 expression);
4588 }
4589 break;
4590
4591 default:
4592 break;
4593 }
4594 }
4595
4596 /* Look for the `:: new' and `:: delete', which also signal the
4597 beginning of a new-expression, or delete-expression,
4598 respectively. If the next token is `::', then it might be one of
4599 these. */
4600 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4601 {
4602 enum rid keyword;
4603
4604 /* See if the token after the `::' is one of the keywords in
4605 which we're interested. */
4606 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4607 /* If it's `new', we have a new-expression. */
4608 if (keyword == RID_NEW)
4609 return cp_parser_new_expression (parser);
4610 /* Similarly, for `delete'. */
4611 else if (keyword == RID_DELETE)
4612 return cp_parser_delete_expression (parser);
4613 }
4614
4615 /* Look for a unary operator. */
4616 unary_operator = cp_parser_unary_operator (token);
4617 /* The `++' and `--' operators can be handled similarly, even though
4618 they are not technically unary-operators in the grammar. */
4619 if (unary_operator == ERROR_MARK)
4620 {
4621 if (token->type == CPP_PLUS_PLUS)
4622 unary_operator = PREINCREMENT_EXPR;
4623 else if (token->type == CPP_MINUS_MINUS)
4624 unary_operator = PREDECREMENT_EXPR;
4625 /* Handle the GNU address-of-label extension. */
4626 else if (cp_parser_allow_gnu_extensions_p (parser)
4627 && token->type == CPP_AND_AND)
4628 {
4629 tree identifier;
4630
4631 /* Consume the '&&' token. */
4632 cp_lexer_consume_token (parser->lexer);
4633 /* Look for the identifier. */
4634 identifier = cp_parser_identifier (parser);
4635 /* Create an expression representing the address. */
4636 return finish_label_address_expr (identifier);
4637 }
4638 }
4639 if (unary_operator != ERROR_MARK)
4640 {
4641 tree cast_expression;
4642
4643 /* Consume the operator token. */
4644 token = cp_lexer_consume_token (parser->lexer);
4645 /* Parse the cast-expression. */
4646 cast_expression
4647 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4648 /* Now, build an appropriate representation. */
4649 switch (unary_operator)
4650 {
4651 case INDIRECT_REF:
4652 return build_x_indirect_ref (cast_expression, "unary *");
4653
4654 case ADDR_EXPR:
4655 return build_x_unary_op (ADDR_EXPR, cast_expression);
4656
4657 case CONVERT_EXPR:
4658 case NEGATE_EXPR:
4659 case TRUTH_NOT_EXPR:
4660 case PREINCREMENT_EXPR:
4661 case PREDECREMENT_EXPR:
4662 return finish_unary_op_expr (unary_operator, cast_expression);
4663
4664 case BIT_NOT_EXPR:
4665 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4666
4667 default:
4668 abort ();
4669 return error_mark_node;
4670 }
4671 }
4672
4673 return cp_parser_postfix_expression (parser, address_p);
4674}
4675
4676/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4677 unary-operator, the corresponding tree code is returned. */
4678
4679static enum tree_code
4680cp_parser_unary_operator (token)
4681 cp_token *token;
4682{
4683 switch (token->type)
4684 {
4685 case CPP_MULT:
4686 return INDIRECT_REF;
4687
4688 case CPP_AND:
4689 return ADDR_EXPR;
4690
4691 case CPP_PLUS:
4692 return CONVERT_EXPR;
4693
4694 case CPP_MINUS:
4695 return NEGATE_EXPR;
4696
4697 case CPP_NOT:
4698 return TRUTH_NOT_EXPR;
4699
4700 case CPP_COMPL:
4701 return BIT_NOT_EXPR;
4702
4703 default:
4704 return ERROR_MARK;
4705 }
4706}
4707
4708/* Parse a new-expression.
4709
4710 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4711 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4712
4713 Returns a representation of the expression. */
4714
4715static tree
4716cp_parser_new_expression (parser)
4717 cp_parser *parser;
4718{
4719 bool global_scope_p;
4720 tree placement;
4721 tree type;
4722 tree initializer;
4723
4724 /* Look for the optional `::' operator. */
4725 global_scope_p
4726 = (cp_parser_global_scope_opt (parser,
4727 /*current_scope_valid_p=*/false)
4728 != NULL_TREE);
4729 /* Look for the `new' operator. */
4730 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4731 /* There's no easy way to tell a new-placement from the
4732 `( type-id )' construct. */
4733 cp_parser_parse_tentatively (parser);
4734 /* Look for a new-placement. */
4735 placement = cp_parser_new_placement (parser);
4736 /* If that didn't work out, there's no new-placement. */
4737 if (!cp_parser_parse_definitely (parser))
4738 placement = NULL_TREE;
4739
4740 /* If the next token is a `(', then we have a parenthesized
4741 type-id. */
4742 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4743 {
4744 /* Consume the `('. */
4745 cp_lexer_consume_token (parser->lexer);
4746 /* Parse the type-id. */
4747 type = cp_parser_type_id (parser);
4748 /* Look for the closing `)'. */
4749 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4750 }
4751 /* Otherwise, there must be a new-type-id. */
4752 else
4753 type = cp_parser_new_type_id (parser);
4754
4755 /* If the next token is a `(', then we have a new-initializer. */
4756 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4757 initializer = cp_parser_new_initializer (parser);
4758 else
4759 initializer = NULL_TREE;
4760
4761 /* Create a representation of the new-expression. */
4762 return build_new (placement, type, initializer, global_scope_p);
4763}
4764
4765/* Parse a new-placement.
4766
4767 new-placement:
4768 ( expression-list )
4769
4770 Returns the same representation as for an expression-list. */
4771
4772static tree
4773cp_parser_new_placement (parser)
4774 cp_parser *parser;
4775{
4776 tree expression_list;
4777
4778 /* Look for the opening `('. */
4779 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4780 return error_mark_node;
4781 /* Parse the expression-list. */
4782 expression_list = cp_parser_expression_list (parser);
4783 /* Look for the closing `)'. */
4784 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4785
4786 return expression_list;
4787}
4788
4789/* Parse a new-type-id.
4790
4791 new-type-id:
4792 type-specifier-seq new-declarator [opt]
4793
4794 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4795 and whose TREE_VALUE is the new-declarator. */
4796
4797static tree
4798cp_parser_new_type_id (parser)
4799 cp_parser *parser;
4800{
4801 tree type_specifier_seq;
4802 tree declarator;
4803 const char *saved_message;
4804
4805 /* The type-specifier sequence must not contain type definitions.
4806 (It cannot contain declarations of new types either, but if they
4807 are not definitions we will catch that because they are not
4808 complete.) */
4809 saved_message = parser->type_definition_forbidden_message;
4810 parser->type_definition_forbidden_message
4811 = "types may not be defined in a new-type-id";
4812 /* Parse the type-specifier-seq. */
4813 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4814 /* Restore the old message. */
4815 parser->type_definition_forbidden_message = saved_message;
4816 /* Parse the new-declarator. */
4817 declarator = cp_parser_new_declarator_opt (parser);
4818
4819 return build_tree_list (type_specifier_seq, declarator);
4820}
4821
4822/* Parse an (optional) new-declarator.
4823
4824 new-declarator:
4825 ptr-operator new-declarator [opt]
4826 direct-new-declarator
4827
4828 Returns a representation of the declarator. See
4829 cp_parser_declarator for the representations used. */
4830
4831static tree
4832cp_parser_new_declarator_opt (parser)
4833 cp_parser *parser;
4834{
4835 enum tree_code code;
4836 tree type;
4837 tree cv_qualifier_seq;
4838
4839 /* We don't know if there's a ptr-operator next, or not. */
4840 cp_parser_parse_tentatively (parser);
4841 /* Look for a ptr-operator. */
4842 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4843 /* If that worked, look for more new-declarators. */
4844 if (cp_parser_parse_definitely (parser))
4845 {
4846 tree declarator;
4847
4848 /* Parse another optional declarator. */
4849 declarator = cp_parser_new_declarator_opt (parser);
4850
4851 /* Create the representation of the declarator. */
4852 if (code == INDIRECT_REF)
4853 declarator = make_pointer_declarator (cv_qualifier_seq,
4854 declarator);
4855 else
4856 declarator = make_reference_declarator (cv_qualifier_seq,
4857 declarator);
4858
4859 /* Handle the pointer-to-member case. */
4860 if (type)
4861 declarator = build_nt (SCOPE_REF, type, declarator);
4862
4863 return declarator;
4864 }
4865
4866 /* If the next token is a `[', there is a direct-new-declarator. */
4867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4868 return cp_parser_direct_new_declarator (parser);
4869
4870 return NULL_TREE;
4871}
4872
4873/* Parse a direct-new-declarator.
4874
4875 direct-new-declarator:
4876 [ expression ]
4877 direct-new-declarator [constant-expression]
4878
4879 Returns an ARRAY_REF, following the same conventions as are
4880 documented for cp_parser_direct_declarator. */
4881
4882static tree
4883cp_parser_direct_new_declarator (parser)
4884 cp_parser *parser;
4885{
4886 tree declarator = NULL_TREE;
4887
4888 while (true)
4889 {
4890 tree expression;
4891
4892 /* Look for the opening `['. */
4893 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4894 /* The first expression is not required to be constant. */
4895 if (!declarator)
4896 {
4897 expression = cp_parser_expression (parser);
4898 /* The standard requires that the expression have integral
4899 type. DR 74 adds enumeration types. We believe that the
4900 real intent is that these expressions be handled like the
4901 expression in a `switch' condition, which also allows
4902 classes with a single conversion to integral or
4903 enumeration type. */
4904 if (!processing_template_decl)
4905 {
4906 expression
4907 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4908 expression,
35771a9a 4909 /*complain=*/true);
0a3b29ad 4910 if (!expression)
4911 {
4912 error ("expression in new-declarator must have integral or enumeration type");
4913 expression = error_mark_node;
4914 }
4915 }
4916 }
4917 /* But all the other expressions must be. */
4918 else
4919 expression = cp_parser_constant_expression (parser);
4920 /* Look for the closing `]'. */
4921 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4922
4923 /* Add this bound to the declarator. */
4924 declarator = build_nt (ARRAY_REF, declarator, expression);
4925
4926 /* If the next token is not a `[', then there are no more
4927 bounds. */
4928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4929 break;
4930 }
4931
4932 return declarator;
4933}
4934
4935/* Parse a new-initializer.
4936
4937 new-initializer:
4938 ( expression-list [opt] )
4939
4940 Returns a reprsentation of the expression-list. If there is no
4941 expression-list, VOID_ZERO_NODE is returned. */
4942
4943static tree
4944cp_parser_new_initializer (parser)
4945 cp_parser *parser;
4946{
4947 tree expression_list;
4948
4949 /* Look for the opening parenthesis. */
4950 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4951 /* If the next token is not a `)', then there is an
4952 expression-list. */
4953 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4954 expression_list = cp_parser_expression_list (parser);
4955 else
4956 expression_list = void_zero_node;
4957 /* Look for the closing parenthesis. */
4958 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4959
4960 return expression_list;
4961}
4962
4963/* Parse a delete-expression.
4964
4965 delete-expression:
4966 :: [opt] delete cast-expression
4967 :: [opt] delete [ ] cast-expression
4968
4969 Returns a representation of the expression. */
4970
4971static tree
4972cp_parser_delete_expression (parser)
4973 cp_parser *parser;
4974{
4975 bool global_scope_p;
4976 bool array_p;
4977 tree expression;
4978
4979 /* Look for the optional `::' operator. */
4980 global_scope_p
4981 = (cp_parser_global_scope_opt (parser,
4982 /*current_scope_valid_p=*/false)
4983 != NULL_TREE);
4984 /* Look for the `delete' keyword. */
4985 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4986 /* See if the array syntax is in use. */
4987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4988 {
4989 /* Consume the `[' token. */
4990 cp_lexer_consume_token (parser->lexer);
4991 /* Look for the `]' token. */
4992 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4993 /* Remember that this is the `[]' construct. */
4994 array_p = true;
4995 }
4996 else
4997 array_p = false;
4998
4999 /* Parse the cast-expression. */
5000 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5001
5002 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5003}
5004
5005/* Parse a cast-expression.
5006
5007 cast-expression:
5008 unary-expression
5009 ( type-id ) cast-expression
5010
5011 Returns a representation of the expression. */
5012
5013static tree
5014cp_parser_cast_expression (cp_parser *parser, bool address_p)
5015{
5016 /* If it's a `(', then we might be looking at a cast. */
5017 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5018 {
5019 tree type = NULL_TREE;
5020 tree expr = NULL_TREE;
5021 bool compound_literal_p;
5022 const char *saved_message;
5023
5024 /* There's no way to know yet whether or not this is a cast.
5025 For example, `(int (3))' is a unary-expression, while `(int)
5026 3' is a cast. So, we resort to parsing tentatively. */
5027 cp_parser_parse_tentatively (parser);
5028 /* Types may not be defined in a cast. */
5029 saved_message = parser->type_definition_forbidden_message;
5030 parser->type_definition_forbidden_message
5031 = "types may not be defined in casts";
5032 /* Consume the `('. */
5033 cp_lexer_consume_token (parser->lexer);
5034 /* A very tricky bit is that `(struct S) { 3 }' is a
5035 compound-literal (which we permit in C++ as an extension).
5036 But, that construct is not a cast-expression -- it is a
5037 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5038 is legal; if the compound-literal were a cast-expression,
5039 you'd need an extra set of parentheses.) But, if we parse
5040 the type-id, and it happens to be a class-specifier, then we
5041 will commit to the parse at that point, because we cannot
5042 undo the action that is done when creating a new class. So,
5043 then we cannot back up and do a postfix-expression.
5044
5045 Therefore, we scan ahead to the closing `)', and check to see
5046 if the token after the `)' is a `{'. If so, we are not
5047 looking at a cast-expression.
5048
5049 Save tokens so that we can put them back. */
5050 cp_lexer_save_tokens (parser->lexer);
5051 /* Skip tokens until the next token is a closing parenthesis.
5052 If we find the closing `)', and the next token is a `{', then
5053 we are looking at a compound-literal. */
5054 compound_literal_p
5055 = (cp_parser_skip_to_closing_parenthesis (parser)
5056 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5057 /* Roll back the tokens we skipped. */
5058 cp_lexer_rollback_tokens (parser->lexer);
5059 /* If we were looking at a compound-literal, simulate an error
5060 so that the call to cp_parser_parse_definitely below will
5061 fail. */
5062 if (compound_literal_p)
5063 cp_parser_simulate_error (parser);
5064 else
5065 {
5066 /* Look for the type-id. */
5067 type = cp_parser_type_id (parser);
5068 /* Look for the closing `)'. */
5069 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5070 }
5071
5072 /* Restore the saved message. */
5073 parser->type_definition_forbidden_message = saved_message;
5074
5075 /* If all went well, this is a cast. */
5076 if (cp_parser_parse_definitely (parser))
5077 {
5078 /* Parse the dependent expression. */
5079 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5080 /* Warn about old-style casts, if so requested. */
5081 if (warn_old_style_cast
5082 && !in_system_header
5083 && !VOID_TYPE_P (type)
5084 && current_lang_name != lang_name_c)
5085 warning ("use of old-style cast");
5086 /* Perform the cast. */
5087 expr = build_c_cast (type, expr);
5088 }
5089
5090 if (expr)
5091 return expr;
5092 }
5093
5094 /* If we get here, then it's not a cast, so it must be a
5095 unary-expression. */
5096 return cp_parser_unary_expression (parser, address_p);
5097}
5098
5099/* Parse a pm-expression.
5100
5101 pm-expression:
5102 cast-expression
5103 pm-expression .* cast-expression
5104 pm-expression ->* cast-expression
5105
5106 Returns a representation of the expression. */
5107
5108static tree
5109cp_parser_pm_expression (parser)
5110 cp_parser *parser;
5111{
5112 tree cast_expr;
5113 tree pm_expr;
5114
5115 /* Parse the cast-expresion. */
5116 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5117 pm_expr = cast_expr;
5118 /* Now look for pointer-to-member operators. */
5119 while (true)
5120 {
5121 cp_token *token;
5122 enum cpp_ttype token_type;
5123
5124 /* Peek at the next token. */
5125 token = cp_lexer_peek_token (parser->lexer);
5126 token_type = token->type;
5127 /* If it's not `.*' or `->*' there's no pointer-to-member
5128 operation. */
5129 if (token_type != CPP_DOT_STAR
5130 && token_type != CPP_DEREF_STAR)
5131 break;
5132
5133 /* Consume the token. */
5134 cp_lexer_consume_token (parser->lexer);
5135
5136 /* Parse another cast-expression. */
5137 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5138
5139 /* Build the representation of the pointer-to-member
5140 operation. */
5141 if (token_type == CPP_DEREF_STAR)
5142 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5143 else
5144 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5145 }
5146
5147 return pm_expr;
5148}
5149
5150/* Parse a multiplicative-expression.
5151
5152 mulitplicative-expression:
5153 pm-expression
5154 multiplicative-expression * pm-expression
5155 multiplicative-expression / pm-expression
5156 multiplicative-expression % pm-expression
5157
5158 Returns a representation of the expression. */
5159
5160static tree
5161cp_parser_multiplicative_expression (parser)
5162 cp_parser *parser;
5163{
5164 static cp_parser_token_tree_map map = {
5165 { CPP_MULT, MULT_EXPR },
5166 { CPP_DIV, TRUNC_DIV_EXPR },
5167 { CPP_MOD, TRUNC_MOD_EXPR },
5168 { CPP_EOF, ERROR_MARK }
5169 };
5170
5171 return cp_parser_binary_expression (parser,
5172 map,
5173 cp_parser_pm_expression);
5174}
5175
5176/* Parse an additive-expression.
5177
5178 additive-expression:
5179 multiplicative-expression
5180 additive-expression + multiplicative-expression
5181 additive-expression - multiplicative-expression
5182
5183 Returns a representation of the expression. */
5184
5185static tree
5186cp_parser_additive_expression (parser)
5187 cp_parser *parser;
5188{
5189 static cp_parser_token_tree_map map = {
5190 { CPP_PLUS, PLUS_EXPR },
5191 { CPP_MINUS, MINUS_EXPR },
5192 { CPP_EOF, ERROR_MARK }
5193 };
5194
5195 return cp_parser_binary_expression (parser,
5196 map,
5197 cp_parser_multiplicative_expression);
5198}
5199
5200/* Parse a shift-expression.
5201
5202 shift-expression:
5203 additive-expression
5204 shift-expression << additive-expression
5205 shift-expression >> additive-expression
5206
5207 Returns a representation of the expression. */
5208
5209static tree
5210cp_parser_shift_expression (parser)
5211 cp_parser *parser;
5212{
5213 static cp_parser_token_tree_map map = {
5214 { CPP_LSHIFT, LSHIFT_EXPR },
5215 { CPP_RSHIFT, RSHIFT_EXPR },
5216 { CPP_EOF, ERROR_MARK }
5217 };
5218
5219 return cp_parser_binary_expression (parser,
5220 map,
5221 cp_parser_additive_expression);
5222}
5223
5224/* Parse a relational-expression.
5225
5226 relational-expression:
5227 shift-expression
5228 relational-expression < shift-expression
5229 relational-expression > shift-expression
5230 relational-expression <= shift-expression
5231 relational-expression >= shift-expression
5232
5233 GNU Extension:
5234
5235 relational-expression:
5236 relational-expression <? shift-expression
5237 relational-expression >? shift-expression
5238
5239 Returns a representation of the expression. */
5240
5241static tree
5242cp_parser_relational_expression (parser)
5243 cp_parser *parser;
5244{
5245 static cp_parser_token_tree_map map = {
5246 { CPP_LESS, LT_EXPR },
5247 { CPP_GREATER, GT_EXPR },
5248 { CPP_LESS_EQ, LE_EXPR },
5249 { CPP_GREATER_EQ, GE_EXPR },
5250 { CPP_MIN, MIN_EXPR },
5251 { CPP_MAX, MAX_EXPR },
5252 { CPP_EOF, ERROR_MARK }
5253 };
5254
5255 return cp_parser_binary_expression (parser,
5256 map,
5257 cp_parser_shift_expression);
5258}
5259
5260/* Parse an equality-expression.
5261
5262 equality-expression:
5263 relational-expression
5264 equality-expression == relational-expression
5265 equality-expression != relational-expression
5266
5267 Returns a representation of the expression. */
5268
5269static tree
5270cp_parser_equality_expression (parser)
5271 cp_parser *parser;
5272{
5273 static cp_parser_token_tree_map map = {
5274 { CPP_EQ_EQ, EQ_EXPR },
5275 { CPP_NOT_EQ, NE_EXPR },
5276 { CPP_EOF, ERROR_MARK }
5277 };
5278
5279 return cp_parser_binary_expression (parser,
5280 map,
5281 cp_parser_relational_expression);
5282}
5283
5284/* Parse an and-expression.
5285
5286 and-expression:
5287 equality-expression
5288 and-expression & equality-expression
5289
5290 Returns a representation of the expression. */
5291
5292static tree
5293cp_parser_and_expression (parser)
5294 cp_parser *parser;
5295{
5296 static cp_parser_token_tree_map map = {
5297 { CPP_AND, BIT_AND_EXPR },
5298 { CPP_EOF, ERROR_MARK }
5299 };
5300
5301 return cp_parser_binary_expression (parser,
5302 map,
5303 cp_parser_equality_expression);
5304}
5305
5306/* Parse an exclusive-or-expression.
5307
5308 exclusive-or-expression:
5309 and-expression
5310 exclusive-or-expression ^ and-expression
5311
5312 Returns a representation of the expression. */
5313
5314static tree
5315cp_parser_exclusive_or_expression (parser)
5316 cp_parser *parser;
5317{
5318 static cp_parser_token_tree_map map = {
5319 { CPP_XOR, BIT_XOR_EXPR },
5320 { CPP_EOF, ERROR_MARK }
5321 };
5322
5323 return cp_parser_binary_expression (parser,
5324 map,
5325 cp_parser_and_expression);
5326}
5327
5328
5329/* Parse an inclusive-or-expression.
5330
5331 inclusive-or-expression:
5332 exclusive-or-expression
5333 inclusive-or-expression | exclusive-or-expression
5334
5335 Returns a representation of the expression. */
5336
5337static tree
5338cp_parser_inclusive_or_expression (parser)
5339 cp_parser *parser;
5340{
5341 static cp_parser_token_tree_map map = {
5342 { CPP_OR, BIT_IOR_EXPR },
5343 { CPP_EOF, ERROR_MARK }
5344 };
5345
5346 return cp_parser_binary_expression (parser,
5347 map,
5348 cp_parser_exclusive_or_expression);
5349}
5350
5351/* Parse a logical-and-expression.
5352
5353 logical-and-expression:
5354 inclusive-or-expression
5355 logical-and-expression && inclusive-or-expression
5356
5357 Returns a representation of the expression. */
5358
5359static tree
5360cp_parser_logical_and_expression (parser)
5361 cp_parser *parser;
5362{
5363 static cp_parser_token_tree_map map = {
5364 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5365 { CPP_EOF, ERROR_MARK }
5366 };
5367
5368 return cp_parser_binary_expression (parser,
5369 map,
5370 cp_parser_inclusive_or_expression);
5371}
5372
5373/* Parse a logical-or-expression.
5374
5375 logical-or-expression:
5376 logical-and-expresion
5377 logical-or-expression || logical-and-expression
5378
5379 Returns a representation of the expression. */
5380
5381static tree
5382cp_parser_logical_or_expression (parser)
5383 cp_parser *parser;
5384{
5385 static cp_parser_token_tree_map map = {
5386 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5387 { CPP_EOF, ERROR_MARK }
5388 };
5389
5390 return cp_parser_binary_expression (parser,
5391 map,
5392 cp_parser_logical_and_expression);
5393}
5394
5395/* Parse a conditional-expression.
5396
5397 conditional-expression:
5398 logical-or-expression
5399 logical-or-expression ? expression : assignment-expression
5400
5401 GNU Extensions:
5402
5403 conditional-expression:
5404 logical-or-expression ? : assignment-expression
5405
5406 Returns a representation of the expression. */
5407
5408static tree
5409cp_parser_conditional_expression (parser)
5410 cp_parser *parser;
5411{
5412 tree logical_or_expr;
5413
5414 /* Parse the logical-or-expression. */
5415 logical_or_expr = cp_parser_logical_or_expression (parser);
5416 /* If the next token is a `?', then we have a real conditional
5417 expression. */
5418 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5419 return cp_parser_question_colon_clause (parser, logical_or_expr);
5420 /* Otherwise, the value is simply the logical-or-expression. */
5421 else
5422 return logical_or_expr;
5423}
5424
5425/* Parse the `? expression : assignment-expression' part of a
5426 conditional-expression. The LOGICAL_OR_EXPR is the
5427 logical-or-expression that started the conditional-expression.
5428 Returns a representation of the entire conditional-expression.
5429
5430 This routine exists only so that it can be shared between
5431 cp_parser_conditional_expression and
5432 cp_parser_assignment_expression.
5433
5434 ? expression : assignment-expression
5435
5436 GNU Extensions:
5437
5438 ? : assignment-expression */
5439
5440static tree
5441cp_parser_question_colon_clause (parser, logical_or_expr)
5442 cp_parser *parser;
5443 tree logical_or_expr;
5444{
5445 tree expr;
5446 tree assignment_expr;
5447
5448 /* Consume the `?' token. */
5449 cp_lexer_consume_token (parser->lexer);
5450 if (cp_parser_allow_gnu_extensions_p (parser)
5451 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5452 /* Implicit true clause. */
5453 expr = NULL_TREE;
5454 else
5455 /* Parse the expression. */
5456 expr = cp_parser_expression (parser);
5457
5458 /* The next token should be a `:'. */
5459 cp_parser_require (parser, CPP_COLON, "`:'");
5460 /* Parse the assignment-expression. */
5461 assignment_expr = cp_parser_assignment_expression (parser);
5462
5463 /* Build the conditional-expression. */
5464 return build_x_conditional_expr (logical_or_expr,
5465 expr,
5466 assignment_expr);
5467}
5468
5469/* Parse an assignment-expression.
5470
5471 assignment-expression:
5472 conditional-expression
5473 logical-or-expression assignment-operator assignment_expression
5474 throw-expression
5475
5476 Returns a representation for the expression. */
5477
5478static tree
5479cp_parser_assignment_expression (parser)
5480 cp_parser *parser;
5481{
5482 tree expr;
5483
5484 /* If the next token is the `throw' keyword, then we're looking at
5485 a throw-expression. */
5486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5487 expr = cp_parser_throw_expression (parser);
5488 /* Otherwise, it must be that we are looking at a
5489 logical-or-expression. */
5490 else
5491 {
5492 /* Parse the logical-or-expression. */
5493 expr = cp_parser_logical_or_expression (parser);
5494 /* If the next token is a `?' then we're actually looking at a
5495 conditional-expression. */
5496 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5497 return cp_parser_question_colon_clause (parser, expr);
5498 else
5499 {
5500 enum tree_code assignment_operator;
5501
5502 /* If it's an assignment-operator, we're using the second
5503 production. */
5504 assignment_operator
5505 = cp_parser_assignment_operator_opt (parser);
5506 if (assignment_operator != ERROR_MARK)
5507 {
5508 tree rhs;
5509
5510 /* Parse the right-hand side of the assignment. */
5511 rhs = cp_parser_assignment_expression (parser);
5512 /* Build the asignment expression. */
5513 expr = build_x_modify_expr (expr,
5514 assignment_operator,
5515 rhs);
5516 }
5517 }
5518 }
5519
5520 return expr;
5521}
5522
5523/* Parse an (optional) assignment-operator.
5524
5525 assignment-operator: one of
5526 = *= /= %= += -= >>= <<= &= ^= |=
5527
5528 GNU Extension:
5529
5530 assignment-operator: one of
5531 <?= >?=
5532
5533 If the next token is an assignment operator, the corresponding tree
5534 code is returned, and the token is consumed. For example, for
5535 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5536 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5537 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5538 operator, ERROR_MARK is returned. */
5539
5540static enum tree_code
5541cp_parser_assignment_operator_opt (parser)
5542 cp_parser *parser;
5543{
5544 enum tree_code op;
5545 cp_token *token;
5546
5547 /* Peek at the next toen. */
5548 token = cp_lexer_peek_token (parser->lexer);
5549
5550 switch (token->type)
5551 {
5552 case CPP_EQ:
5553 op = NOP_EXPR;
5554 break;
5555
5556 case CPP_MULT_EQ:
5557 op = MULT_EXPR;
5558 break;
5559
5560 case CPP_DIV_EQ:
5561 op = TRUNC_DIV_EXPR;
5562 break;
5563
5564 case CPP_MOD_EQ:
5565 op = TRUNC_MOD_EXPR;
5566 break;
5567
5568 case CPP_PLUS_EQ:
5569 op = PLUS_EXPR;
5570 break;
5571
5572 case CPP_MINUS_EQ:
5573 op = MINUS_EXPR;
5574 break;
5575
5576 case CPP_RSHIFT_EQ:
5577 op = RSHIFT_EXPR;
5578 break;
5579
5580 case CPP_LSHIFT_EQ:
5581 op = LSHIFT_EXPR;
5582 break;
5583
5584 case CPP_AND_EQ:
5585 op = BIT_AND_EXPR;
5586 break;
5587
5588 case CPP_XOR_EQ:
5589 op = BIT_XOR_EXPR;
5590 break;
5591
5592 case CPP_OR_EQ:
5593 op = BIT_IOR_EXPR;
5594 break;
5595
5596 case CPP_MIN_EQ:
5597 op = MIN_EXPR;
5598 break;
5599
5600 case CPP_MAX_EQ:
5601 op = MAX_EXPR;
5602 break;
5603
5604 default:
5605 /* Nothing else is an assignment operator. */
5606 op = ERROR_MARK;
5607 }
5608
5609 /* If it was an assignment operator, consume it. */
5610 if (op != ERROR_MARK)
5611 cp_lexer_consume_token (parser->lexer);
5612
5613 return op;
5614}
5615
5616/* Parse an expression.
5617
5618 expression:
5619 assignment-expression
5620 expression , assignment-expression
5621
5622 Returns a representation of the expression. */
5623
5624static tree
5625cp_parser_expression (parser)
5626 cp_parser *parser;
5627{
5628 tree expression = NULL_TREE;
5629 bool saw_comma_p = false;
5630
5631 while (true)
5632 {
5633 tree assignment_expression;
5634
5635 /* Parse the next assignment-expression. */
5636 assignment_expression
5637 = cp_parser_assignment_expression (parser);
5638 /* If this is the first assignment-expression, we can just
5639 save it away. */
5640 if (!expression)
5641 expression = assignment_expression;
5642 /* Otherwise, chain the expressions together. It is unclear why
5643 we do not simply build COMPOUND_EXPRs as we go. */
5644 else
5645 expression = tree_cons (NULL_TREE,
5646 assignment_expression,
5647 expression);
5648 /* If the next token is not a comma, then we are done with the
5649 expression. */
5650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5651 break;
5652 /* Consume the `,'. */
5653 cp_lexer_consume_token (parser->lexer);
5654 /* The first time we see a `,', we must take special action
5655 because the representation used for a single expression is
5656 different from that used for a list containing the single
5657 expression. */
5658 if (!saw_comma_p)
5659 {
5660 /* Remember that this expression has a `,' in it. */
5661 saw_comma_p = true;
5662 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5663 additional expressions to it. */
5664 expression = build_tree_list (NULL_TREE, expression);
5665 }
5666 }
5667
5668 /* Build a COMPOUND_EXPR to represent the entire expression, if
5669 necessary. We built up the list in reverse order, so we must
5670 straighten it out here. */
5671 if (saw_comma_p)
5672 expression = build_x_compound_expr (nreverse (expression));
5673
5674 return expression;
5675}
5676
5677/* Parse a constant-expression.
5678
5679 constant-expression:
5680 conditional-expression */
5681
5682static tree
5683cp_parser_constant_expression (parser)
5684 cp_parser *parser;
5685{
5686 bool saved_constant_expression_p;
5687 tree expression;
5688
5689 /* It might seem that we could simply parse the
5690 conditional-expression, and then check to see if it were
5691 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5692 one that the compiler can figure out is constant, possibly after
5693 doing some simplifications or optimizations. The standard has a
5694 precise definition of constant-expression, and we must honor
5695 that, even though it is somewhat more restrictive.
5696
5697 For example:
5698
5699 int i[(2, 3)];
5700
5701 is not a legal declaration, because `(2, 3)' is not a
5702 constant-expression. The `,' operator is forbidden in a
5703 constant-expression. However, GCC's constant-folding machinery
5704 will fold this operation to an INTEGER_CST for `3'. */
5705
5706 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5707 saved_constant_expression_p = parser->constant_expression_p;
5708 /* We are now parsing a constant-expression. */
5709 parser->constant_expression_p = true;
5710 /* Parse the conditional-expression. */
5711 expression = cp_parser_conditional_expression (parser);
5712 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5713 parser->constant_expression_p = saved_constant_expression_p;
5714
5715 return expression;
5716}
5717
5718/* Statements [gram.stmt.stmt] */
5719
5720/* Parse a statement.
5721
5722 statement:
5723 labeled-statement
5724 expression-statement
5725 compound-statement
5726 selection-statement
5727 iteration-statement
5728 jump-statement
5729 declaration-statement
5730 try-block */
5731
5732static void
5733cp_parser_statement (parser)
5734 cp_parser *parser;
5735{
5736 tree statement;
5737 cp_token *token;
5738 int statement_line_number;
5739
5740 /* There is no statement yet. */
5741 statement = NULL_TREE;
5742 /* Peek at the next token. */
5743 token = cp_lexer_peek_token (parser->lexer);
5744 /* Remember the line number of the first token in the statement. */
5745 statement_line_number = token->line_number;
5746 /* If this is a keyword, then that will often determine what kind of
5747 statement we have. */
5748 if (token->type == CPP_KEYWORD)
5749 {
5750 enum rid keyword = token->keyword;
5751
5752 switch (keyword)
5753 {
5754 case RID_CASE:
5755 case RID_DEFAULT:
5756 statement = cp_parser_labeled_statement (parser);
5757 break;
5758
5759 case RID_IF:
5760 case RID_SWITCH:
5761 statement = cp_parser_selection_statement (parser);
5762 break;
5763
5764 case RID_WHILE:
5765 case RID_DO:
5766 case RID_FOR:
5767 statement = cp_parser_iteration_statement (parser);
5768 break;
5769
5770 case RID_BREAK:
5771 case RID_CONTINUE:
5772 case RID_RETURN:
5773 case RID_GOTO:
5774 statement = cp_parser_jump_statement (parser);
5775 break;
5776
5777 case RID_TRY:
5778 statement = cp_parser_try_block (parser);
5779 break;
5780
5781 default:
5782 /* It might be a keyword like `int' that can start a
5783 declaration-statement. */
5784 break;
5785 }
5786 }
5787 else if (token->type == CPP_NAME)
5788 {
5789 /* If the next token is a `:', then we are looking at a
5790 labeled-statement. */
5791 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5792 if (token->type == CPP_COLON)
5793 statement = cp_parser_labeled_statement (parser);
5794 }
5795 /* Anything that starts with a `{' must be a compound-statement. */
5796 else if (token->type == CPP_OPEN_BRACE)
5797 statement = cp_parser_compound_statement (parser);
5798
5799 /* Everything else must be a declaration-statement or an
5800 expression-statement. Try for the declaration-statement
5801 first, unless we are looking at a `;', in which case we know that
5802 we have an expression-statement. */
5803 if (!statement)
5804 {
5805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5806 {
5807 cp_parser_parse_tentatively (parser);
5808 /* Try to parse the declaration-statement. */
5809 cp_parser_declaration_statement (parser);
5810 /* If that worked, we're done. */
5811 if (cp_parser_parse_definitely (parser))
5812 return;
5813 }
5814 /* Look for an expression-statement instead. */
5815 statement = cp_parser_expression_statement (parser);
5816 }
5817
5818 /* Set the line number for the statement. */
5819 if (statement && statement_code_p (TREE_CODE (statement)))
5820 STMT_LINENO (statement) = statement_line_number;
5821}
5822
5823/* Parse a labeled-statement.
5824
5825 labeled-statement:
5826 identifier : statement
5827 case constant-expression : statement
5828 default : statement
5829
5830 Returns the new CASE_LABEL, for a `case' or `default' label. For
5831 an ordinary label, returns a LABEL_STMT. */
5832
5833static tree
5834cp_parser_labeled_statement (parser)
5835 cp_parser *parser;
5836{
5837 cp_token *token;
5838 tree statement = NULL_TREE;
5839
5840 /* The next token should be an identifier. */
5841 token = cp_lexer_peek_token (parser->lexer);
5842 if (token->type != CPP_NAME
5843 && token->type != CPP_KEYWORD)
5844 {
5845 cp_parser_error (parser, "expected labeled-statement");
5846 return error_mark_node;
5847 }
5848
5849 switch (token->keyword)
5850 {
5851 case RID_CASE:
5852 {
5853 tree expr;
5854
5855 /* Consume the `case' token. */
5856 cp_lexer_consume_token (parser->lexer);
5857 /* Parse the constant-expression. */
5858 expr = cp_parser_constant_expression (parser);
5859 /* Create the label. */
5860 statement = finish_case_label (expr, NULL_TREE);
5861 }
5862 break;
5863
5864 case RID_DEFAULT:
5865 /* Consume the `default' token. */
5866 cp_lexer_consume_token (parser->lexer);
5867 /* Create the label. */
5868 statement = finish_case_label (NULL_TREE, NULL_TREE);
5869 break;
5870
5871 default:
5872 /* Anything else must be an ordinary label. */
5873 statement = finish_label_stmt (cp_parser_identifier (parser));
5874 break;
5875 }
5876
5877 /* Require the `:' token. */
5878 cp_parser_require (parser, CPP_COLON, "`:'");
5879 /* Parse the labeled statement. */
5880 cp_parser_statement (parser);
5881
5882 /* Return the label, in the case of a `case' or `default' label. */
5883 return statement;
5884}
5885
5886/* Parse an expression-statement.
5887
5888 expression-statement:
5889 expression [opt] ;
5890
5891 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5892 statement consists of nothing more than an `;'. */
5893
5894static tree
5895cp_parser_expression_statement (parser)
5896 cp_parser *parser;
5897{
5898 tree statement;
5899
5900 /* If the next token is not a `;', then there is an expression to parse. */
5901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5902 statement = finish_expr_stmt (cp_parser_expression (parser));
5903 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5904 else
5905 {
5906 finish_stmt ();
5907 statement = NULL_TREE;
5908 }
5909 /* Consume the final `;'. */
5910 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5911 {
5912 /* If there is additional (erroneous) input, skip to the end of
5913 the statement. */
5914 cp_parser_skip_to_end_of_statement (parser);
5915 /* If the next token is now a `;', consume it. */
5916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5917 cp_lexer_consume_token (parser->lexer);
5918 }
5919
5920 return statement;
5921}
5922
5923/* Parse a compound-statement.
5924
5925 compound-statement:
5926 { statement-seq [opt] }
5927
5928 Returns a COMPOUND_STMT representing the statement. */
5929
5930static tree
5931cp_parser_compound_statement (cp_parser *parser)
5932{
5933 tree compound_stmt;
5934
5935 /* Consume the `{'. */
5936 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5937 return error_mark_node;
5938 /* Begin the compound-statement. */
5939 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5940 /* Parse an (optional) statement-seq. */
5941 cp_parser_statement_seq_opt (parser);
5942 /* Finish the compound-statement. */
5943 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5944 /* Consume the `}'. */
5945 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5946
5947 return compound_stmt;
5948}
5949
5950/* Parse an (optional) statement-seq.
5951
5952 statement-seq:
5953 statement
5954 statement-seq [opt] statement */
5955
5956static void
5957cp_parser_statement_seq_opt (parser)
5958 cp_parser *parser;
5959{
5960 /* Scan statements until there aren't any more. */
5961 while (true)
5962 {
5963 /* If we're looking at a `}', then we've run out of statements. */
5964 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5965 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5966 break;
5967
5968 /* Parse the statement. */
5969 cp_parser_statement (parser);
5970 }
5971}
5972
5973/* Parse a selection-statement.
5974
5975 selection-statement:
5976 if ( condition ) statement
5977 if ( condition ) statement else statement
5978 switch ( condition ) statement
5979
5980 Returns the new IF_STMT or SWITCH_STMT. */
5981
5982static tree
5983cp_parser_selection_statement (parser)
5984 cp_parser *parser;
5985{
5986 cp_token *token;
5987 enum rid keyword;
5988
5989 /* Peek at the next token. */
5990 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5991
5992 /* See what kind of keyword it is. */
5993 keyword = token->keyword;
5994 switch (keyword)
5995 {
5996 case RID_IF:
5997 case RID_SWITCH:
5998 {
5999 tree statement;
6000 tree condition;
6001
6002 /* Look for the `('. */
6003 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6004 {
6005 cp_parser_skip_to_end_of_statement (parser);
6006 return error_mark_node;
6007 }
6008
6009 /* Begin the selection-statement. */
6010 if (keyword == RID_IF)
6011 statement = begin_if_stmt ();
6012 else
6013 statement = begin_switch_stmt ();
6014
6015 /* Parse the condition. */
6016 condition = cp_parser_condition (parser);
6017 /* Look for the `)'. */
6018 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6019 cp_parser_skip_to_closing_parenthesis (parser);
6020
6021 if (keyword == RID_IF)
6022 {
6023 tree then_stmt;
6024
6025 /* Add the condition. */
6026 finish_if_stmt_cond (condition, statement);
6027
6028 /* Parse the then-clause. */
6029 then_stmt = cp_parser_implicitly_scoped_statement (parser);
6030 finish_then_clause (statement);
6031
6032 /* If the next token is `else', parse the else-clause. */
6033 if (cp_lexer_next_token_is_keyword (parser->lexer,
6034 RID_ELSE))
6035 {
6036 tree else_stmt;
6037
6038 /* Consume the `else' keyword. */
6039 cp_lexer_consume_token (parser->lexer);
6040 /* Parse the else-clause. */
6041 else_stmt
6042 = cp_parser_implicitly_scoped_statement (parser);
6043 finish_else_clause (statement);
6044 }
6045
6046 /* Now we're all done with the if-statement. */
6047 finish_if_stmt ();
6048 }
6049 else
6050 {
6051 tree body;
6052
6053 /* Add the condition. */
6054 finish_switch_cond (condition, statement);
6055
6056 /* Parse the body of the switch-statement. */
6057 body = cp_parser_implicitly_scoped_statement (parser);
6058
6059 /* Now we're all done with the switch-statement. */
6060 finish_switch_stmt (statement);
6061 }
6062
6063 return statement;
6064 }
6065 break;
6066
6067 default:
6068 cp_parser_error (parser, "expected selection-statement");
6069 return error_mark_node;
6070 }
6071}
6072
6073/* Parse a condition.
6074
6075 condition:
6076 expression
6077 type-specifier-seq declarator = assignment-expression
6078
6079 GNU Extension:
6080
6081 condition:
6082 type-specifier-seq declarator asm-specification [opt]
6083 attributes [opt] = assignment-expression
6084
6085 Returns the expression that should be tested. */
6086
6087static tree
6088cp_parser_condition (parser)
6089 cp_parser *parser;
6090{
6091 tree type_specifiers;
6092 const char *saved_message;
6093
6094 /* Try the declaration first. */
6095 cp_parser_parse_tentatively (parser);
6096 /* New types are not allowed in the type-specifier-seq for a
6097 condition. */
6098 saved_message = parser->type_definition_forbidden_message;
6099 parser->type_definition_forbidden_message
6100 = "types may not be defined in conditions";
6101 /* Parse the type-specifier-seq. */
6102 type_specifiers = cp_parser_type_specifier_seq (parser);
6103 /* Restore the saved message. */
6104 parser->type_definition_forbidden_message = saved_message;
6105 /* If all is well, we might be looking at a declaration. */
6106 if (!cp_parser_error_occurred (parser))
6107 {
6108 tree decl;
6109 tree asm_specification;
6110 tree attributes;
6111 tree declarator;
6112 tree initializer = NULL_TREE;
6113
6114 /* Parse the declarator. */
6115 declarator = cp_parser_declarator (parser,
6116 /*abstract_p=*/false,
6117 /*ctor_dtor_or_conv_p=*/NULL);
6118 /* Parse the attributes. */
6119 attributes = cp_parser_attributes_opt (parser);
6120 /* Parse the asm-specification. */
6121 asm_specification = cp_parser_asm_specification_opt (parser);
6122 /* If the next token is not an `=', then we might still be
6123 looking at an expression. For example:
6124
6125 if (A(a).x)
6126
6127 looks like a decl-specifier-seq and a declarator -- but then
6128 there is no `=', so this is an expression. */
6129 cp_parser_require (parser, CPP_EQ, "`='");
6130 /* If we did see an `=', then we are looking at a declaration
6131 for sure. */
6132 if (cp_parser_parse_definitely (parser))
6133 {
6134 /* Create the declaration. */
6135 decl = start_decl (declarator, type_specifiers,
6136 /*initialized_p=*/true,
6137 attributes, /*prefix_attributes=*/NULL_TREE);
6138 /* Parse the assignment-expression. */
6139 initializer = cp_parser_assignment_expression (parser);
6140
6141 /* Process the initializer. */
6142 cp_finish_decl (decl,
6143 initializer,
6144 asm_specification,
6145 LOOKUP_ONLYCONVERTING);
6146
6147 return convert_from_reference (decl);
6148 }
6149 }
6150 /* If we didn't even get past the declarator successfully, we are
6151 definitely not looking at a declaration. */
6152 else
6153 cp_parser_abort_tentative_parse (parser);
6154
6155 /* Otherwise, we are looking at an expression. */
6156 return cp_parser_expression (parser);
6157}
6158
6159/* Parse an iteration-statement.
6160
6161 iteration-statement:
6162 while ( condition ) statement
6163 do statement while ( expression ) ;
6164 for ( for-init-statement condition [opt] ; expression [opt] )
6165 statement
6166
6167 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6168
6169static tree
6170cp_parser_iteration_statement (parser)
6171 cp_parser *parser;
6172{
6173 cp_token *token;
6174 enum rid keyword;
6175 tree statement;
6176
6177 /* Peek at the next token. */
6178 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6179 if (!token)
6180 return error_mark_node;
6181
6182 /* See what kind of keyword it is. */
6183 keyword = token->keyword;
6184 switch (keyword)
6185 {
6186 case RID_WHILE:
6187 {
6188 tree condition;
6189
6190 /* Begin the while-statement. */
6191 statement = begin_while_stmt ();
6192 /* Look for the `('. */
6193 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6194 /* Parse the condition. */
6195 condition = cp_parser_condition (parser);
6196 finish_while_stmt_cond (condition, statement);
6197 /* Look for the `)'. */
6198 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6199 /* Parse the dependent statement. */
6200 cp_parser_already_scoped_statement (parser);
6201 /* We're done with the while-statement. */
6202 finish_while_stmt (statement);
6203 }
6204 break;
6205
6206 case RID_DO:
6207 {
6208 tree expression;
6209
6210 /* Begin the do-statement. */
6211 statement = begin_do_stmt ();
6212 /* Parse the body of the do-statement. */
6213 cp_parser_implicitly_scoped_statement (parser);
6214 finish_do_body (statement);
6215 /* Look for the `while' keyword. */
6216 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6217 /* Look for the `('. */
6218 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6219 /* Parse the expression. */
6220 expression = cp_parser_expression (parser);
6221 /* We're done with the do-statement. */
6222 finish_do_stmt (expression, statement);
6223 /* Look for the `)'. */
6224 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6225 /* Look for the `;'. */
6226 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6227 }
6228 break;
6229
6230 case RID_FOR:
6231 {
6232 tree condition = NULL_TREE;
6233 tree expression = NULL_TREE;
6234
6235 /* Begin the for-statement. */
6236 statement = begin_for_stmt ();
6237 /* Look for the `('. */
6238 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6239 /* Parse the initialization. */
6240 cp_parser_for_init_statement (parser);
6241 finish_for_init_stmt (statement);
6242
6243 /* If there's a condition, process it. */
6244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6245 condition = cp_parser_condition (parser);
6246 finish_for_cond (condition, statement);
6247 /* Look for the `;'. */
6248 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6249
6250 /* If there's an expression, process it. */
6251 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6252 expression = cp_parser_expression (parser);
6253 finish_for_expr (expression, statement);
6254 /* Look for the `)'. */
6255 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6256
6257 /* Parse the body of the for-statement. */
6258 cp_parser_already_scoped_statement (parser);
6259
6260 /* We're done with the for-statement. */
6261 finish_for_stmt (statement);
6262 }
6263 break;
6264
6265 default:
6266 cp_parser_error (parser, "expected iteration-statement");
6267 statement = error_mark_node;
6268 break;
6269 }
6270
6271 return statement;
6272}
6273
6274/* Parse a for-init-statement.
6275
6276 for-init-statement:
6277 expression-statement
6278 simple-declaration */
6279
6280static void
6281cp_parser_for_init_statement (parser)
6282 cp_parser *parser;
6283{
6284 /* If the next token is a `;', then we have an empty
6285 expression-statement. Gramatically, this is also a
6286 simple-declaration, but an invalid one, because it does not
6287 declare anything. Therefore, if we did not handle this case
6288 specially, we would issue an error message about an invalid
6289 declaration. */
6290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6291 {
6292 /* We're going to speculatively look for a declaration, falling back
6293 to an expression, if necessary. */
6294 cp_parser_parse_tentatively (parser);
6295 /* Parse the declaration. */
6296 cp_parser_simple_declaration (parser,
6297 /*function_definition_allowed_p=*/false);
6298 /* If the tentative parse failed, then we shall need to look for an
6299 expression-statement. */
6300 if (cp_parser_parse_definitely (parser))
6301 return;
6302 }
6303
6304 cp_parser_expression_statement (parser);
6305}
6306
6307/* Parse a jump-statement.
6308
6309 jump-statement:
6310 break ;
6311 continue ;
6312 return expression [opt] ;
6313 goto identifier ;
6314
6315 GNU extension:
6316
6317 jump-statement:
6318 goto * expression ;
6319
6320 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6321 GOTO_STMT. */
6322
6323static tree
6324cp_parser_jump_statement (parser)
6325 cp_parser *parser;
6326{
6327 tree statement = error_mark_node;
6328 cp_token *token;
6329 enum rid keyword;
6330
6331 /* Peek at the next token. */
6332 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6333 if (!token)
6334 return error_mark_node;
6335
6336 /* See what kind of keyword it is. */
6337 keyword = token->keyword;
6338 switch (keyword)
6339 {
6340 case RID_BREAK:
6341 statement = finish_break_stmt ();
6342 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6343 break;
6344
6345 case RID_CONTINUE:
6346 statement = finish_continue_stmt ();
6347 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6348 break;
6349
6350 case RID_RETURN:
6351 {
6352 tree expr;
6353
6354 /* If the next token is a `;', then there is no
6355 expression. */
6356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6357 expr = cp_parser_expression (parser);
6358 else
6359 expr = NULL_TREE;
6360 /* Build the return-statement. */
6361 statement = finish_return_stmt (expr);
6362 /* Look for the final `;'. */
6363 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6364 }
6365 break;
6366
6367 case RID_GOTO:
6368 /* Create the goto-statement. */
6369 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6370 {
6371 /* Issue a warning about this use of a GNU extension. */
6372 if (pedantic)
6373 pedwarn ("ISO C++ forbids computed gotos");
6374 /* Consume the '*' token. */
6375 cp_lexer_consume_token (parser->lexer);
6376 /* Parse the dependent expression. */
6377 finish_goto_stmt (cp_parser_expression (parser));
6378 }
6379 else
6380 finish_goto_stmt (cp_parser_identifier (parser));
6381 /* Look for the final `;'. */
6382 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6383 break;
6384
6385 default:
6386 cp_parser_error (parser, "expected jump-statement");
6387 break;
6388 }
6389
6390 return statement;
6391}
6392
6393/* Parse a declaration-statement.
6394
6395 declaration-statement:
6396 block-declaration */
6397
6398static void
6399cp_parser_declaration_statement (parser)
6400 cp_parser *parser;
6401{
6402 /* Parse the block-declaration. */
6403 cp_parser_block_declaration (parser, /*statement_p=*/true);
6404
6405 /* Finish off the statement. */
6406 finish_stmt ();
6407}
6408
6409/* Some dependent statements (like `if (cond) statement'), are
6410 implicitly in their own scope. In other words, if the statement is
6411 a single statement (as opposed to a compound-statement), it is
6412 none-the-less treated as if it were enclosed in braces. Any
6413 declarations appearing in the dependent statement are out of scope
6414 after control passes that point. This function parses a statement,
6415 but ensures that is in its own scope, even if it is not a
6416 compound-statement.
6417
6418 Returns the new statement. */
6419
6420static tree
6421cp_parser_implicitly_scoped_statement (parser)
6422 cp_parser *parser;
6423{
6424 tree statement;
6425
6426 /* If the token is not a `{', then we must take special action. */
6427 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6428 {
6429 /* Create a compound-statement. */
6430 statement = begin_compound_stmt (/*has_no_scope=*/0);
6431 /* Parse the dependent-statement. */
6432 cp_parser_statement (parser);
6433 /* Finish the dummy compound-statement. */
6434 finish_compound_stmt (/*has_no_scope=*/0, statement);
6435 }
6436 /* Otherwise, we simply parse the statement directly. */
6437 else
6438 statement = cp_parser_compound_statement (parser);
6439
6440 /* Return the statement. */
6441 return statement;
6442}
6443
6444/* For some dependent statements (like `while (cond) statement'), we
6445 have already created a scope. Therefore, even if the dependent
6446 statement is a compound-statement, we do not want to create another
6447 scope. */
6448
6449static void
6450cp_parser_already_scoped_statement (parser)
6451 cp_parser *parser;
6452{
6453 /* If the token is not a `{', then we must take special action. */
6454 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6455 {
6456 tree statement;
6457
6458 /* Create a compound-statement. */
6459 statement = begin_compound_stmt (/*has_no_scope=*/1);
6460 /* Parse the dependent-statement. */
6461 cp_parser_statement (parser);
6462 /* Finish the dummy compound-statement. */
6463 finish_compound_stmt (/*has_no_scope=*/1, statement);
6464 }
6465 /* Otherwise, we simply parse the statement directly. */
6466 else
6467 cp_parser_statement (parser);
6468}
6469
6470/* Declarations [gram.dcl.dcl] */
6471
6472/* Parse an optional declaration-sequence.
6473
6474 declaration-seq:
6475 declaration
6476 declaration-seq declaration */
6477
6478static void
6479cp_parser_declaration_seq_opt (parser)
6480 cp_parser *parser;
6481{
6482 while (true)
6483 {
6484 cp_token *token;
6485
6486 token = cp_lexer_peek_token (parser->lexer);
6487
6488 if (token->type == CPP_CLOSE_BRACE
6489 || token->type == CPP_EOF)
6490 break;
6491
6492 if (token->type == CPP_SEMICOLON)
6493 {
6494 /* A declaration consisting of a single semicolon is
6495 invalid. Allow it unless we're being pedantic. */
6496 if (pedantic)
6497 pedwarn ("extra `;'");
6498 cp_lexer_consume_token (parser->lexer);
6499 continue;
6500 }
6501
313a21c0 6502 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6503 parser to enter or exit implict `extern "C"' blocks. */
6504 while (pending_lang_change > 0)
6505 {
6506 push_lang_context (lang_name_c);
6507 --pending_lang_change;
6508 }
6509 while (pending_lang_change < 0)
6510 {
6511 pop_lang_context ();
6512 ++pending_lang_change;
6513 }
6514
6515 /* Parse the declaration itself. */
0a3b29ad 6516 cp_parser_declaration (parser);
6517 }
6518}
6519
6520/* Parse a declaration.
6521
6522 declaration:
6523 block-declaration
6524 function-definition
6525 template-declaration
6526 explicit-instantiation
6527 explicit-specialization
6528 linkage-specification
6529 namespace-definition */
6530
6531static void
6532cp_parser_declaration (parser)
6533 cp_parser *parser;
6534{
6535 cp_token token1;
6536 cp_token token2;
6537
6538 /* Try to figure out what kind of declaration is present. */
6539 token1 = *cp_lexer_peek_token (parser->lexer);
6540 if (token1.type != CPP_EOF)
6541 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6542
6543 /* If the next token is `extern' and the following token is a string
6544 literal, then we have a linkage specification. */
6545 if (token1.keyword == RID_EXTERN
6546 && cp_parser_is_string_literal (&token2))
6547 cp_parser_linkage_specification (parser);
6548 /* If the next token is `template', then we have either a template
6549 declaration, an explicit instantiation, or an explicit
6550 specialization. */
6551 else if (token1.keyword == RID_TEMPLATE)
6552 {
6553 /* `template <>' indicates a template specialization. */
6554 if (token2.type == CPP_LESS
6555 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6556 cp_parser_explicit_specialization (parser);
6557 /* `template <' indicates a template declaration. */
6558 else if (token2.type == CPP_LESS)
6559 cp_parser_template_declaration (parser, /*member_p=*/false);
6560 /* Anything else must be an explicit instantiation. */
6561 else
6562 cp_parser_explicit_instantiation (parser);
6563 }
6564 /* If the next token is `export', then we have a template
6565 declaration. */
6566 else if (token1.keyword == RID_EXPORT)
6567 cp_parser_template_declaration (parser, /*member_p=*/false);
6568 /* If the next token is `extern', 'static' or 'inline' and the one
6569 after that is `template', we have a GNU extended explicit
6570 instantiation directive. */
6571 else if (cp_parser_allow_gnu_extensions_p (parser)
6572 && (token1.keyword == RID_EXTERN
6573 || token1.keyword == RID_STATIC
6574 || token1.keyword == RID_INLINE)
6575 && token2.keyword == RID_TEMPLATE)
6576 cp_parser_explicit_instantiation (parser);
6577 /* If the next token is `namespace', check for a named or unnamed
6578 namespace definition. */
6579 else if (token1.keyword == RID_NAMESPACE
6580 && (/* A named namespace definition. */
6581 (token2.type == CPP_NAME
6582 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6583 == CPP_OPEN_BRACE))
6584 /* An unnamed namespace definition. */
6585 || token2.type == CPP_OPEN_BRACE))
6586 cp_parser_namespace_definition (parser);
6587 /* We must have either a block declaration or a function
6588 definition. */
6589 else
6590 /* Try to parse a block-declaration, or a function-definition. */
6591 cp_parser_block_declaration (parser, /*statement_p=*/false);
6592}
6593
6594/* Parse a block-declaration.
6595
6596 block-declaration:
6597 simple-declaration
6598 asm-definition
6599 namespace-alias-definition
6600 using-declaration
6601 using-directive
6602
6603 GNU Extension:
6604
6605 block-declaration:
6606 __extension__ block-declaration
6607 label-declaration
6608
6609 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6610 part of a declaration-statement. */
6611
6612static void
6613cp_parser_block_declaration (cp_parser *parser,
6614 bool statement_p)
6615{
6616 cp_token *token1;
6617 int saved_pedantic;
6618
6619 /* Check for the `__extension__' keyword. */
6620 if (cp_parser_extension_opt (parser, &saved_pedantic))
6621 {
6622 /* Parse the qualified declaration. */
6623 cp_parser_block_declaration (parser, statement_p);
6624 /* Restore the PEDANTIC flag. */
6625 pedantic = saved_pedantic;
6626
6627 return;
6628 }
6629
6630 /* Peek at the next token to figure out which kind of declaration is
6631 present. */
6632 token1 = cp_lexer_peek_token (parser->lexer);
6633
6634 /* If the next keyword is `asm', we have an asm-definition. */
6635 if (token1->keyword == RID_ASM)
6636 {
6637 if (statement_p)
6638 cp_parser_commit_to_tentative_parse (parser);
6639 cp_parser_asm_definition (parser);
6640 }
6641 /* If the next keyword is `namespace', we have a
6642 namespace-alias-definition. */
6643 else if (token1->keyword == RID_NAMESPACE)
6644 cp_parser_namespace_alias_definition (parser);
6645 /* If the next keyword is `using', we have either a
6646 using-declaration or a using-directive. */
6647 else if (token1->keyword == RID_USING)
6648 {
6649 cp_token *token2;
6650
6651 if (statement_p)
6652 cp_parser_commit_to_tentative_parse (parser);
6653 /* If the token after `using' is `namespace', then we have a
6654 using-directive. */
6655 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6656 if (token2->keyword == RID_NAMESPACE)
6657 cp_parser_using_directive (parser);
6658 /* Otherwise, it's a using-declaration. */
6659 else
6660 cp_parser_using_declaration (parser);
6661 }
6662 /* If the next keyword is `__label__' we have a label declaration. */
6663 else if (token1->keyword == RID_LABEL)
6664 {
6665 if (statement_p)
6666 cp_parser_commit_to_tentative_parse (parser);
6667 cp_parser_label_declaration (parser);
6668 }
6669 /* Anything else must be a simple-declaration. */
6670 else
6671 cp_parser_simple_declaration (parser, !statement_p);
6672}
6673
6674/* Parse a simple-declaration.
6675
6676 simple-declaration:
6677 decl-specifier-seq [opt] init-declarator-list [opt] ;
6678
6679 init-declarator-list:
6680 init-declarator
6681 init-declarator-list , init-declarator
6682
6683 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6684 function-definition as a simple-declaration. */
6685
6686static void
6687cp_parser_simple_declaration (parser, function_definition_allowed_p)
6688 cp_parser *parser;
6689 bool function_definition_allowed_p;
6690{
6691 tree decl_specifiers;
6692 tree attributes;
6693 tree access_checks;
6694 bool declares_class_or_enum;
6695 bool saw_declarator;
6696
6697 /* Defer access checks until we know what is being declared; the
6698 checks for names appearing in the decl-specifier-seq should be
6699 done as if we were in the scope of the thing being declared. */
6700 cp_parser_start_deferring_access_checks (parser);
6701 /* Parse the decl-specifier-seq. We have to keep track of whether
6702 or not the decl-specifier-seq declares a named class or
6703 enumeration type, since that is the only case in which the
6704 init-declarator-list is allowed to be empty.
6705
6706 [dcl.dcl]
6707
6708 In a simple-declaration, the optional init-declarator-list can be
6709 omitted only when declaring a class or enumeration, that is when
6710 the decl-specifier-seq contains either a class-specifier, an
6711 elaborated-type-specifier, or an enum-specifier. */
6712 decl_specifiers
6713 = cp_parser_decl_specifier_seq (parser,
6714 CP_PARSER_FLAGS_OPTIONAL,
6715 &attributes,
6716 &declares_class_or_enum);
6717 /* We no longer need to defer access checks. */
6718 access_checks = cp_parser_stop_deferring_access_checks (parser);
6719
6720 /* Keep going until we hit the `;' at the end of the simple
6721 declaration. */
6722 saw_declarator = false;
6723 while (cp_lexer_next_token_is_not (parser->lexer,
6724 CPP_SEMICOLON))
6725 {
6726 cp_token *token;
6727 bool function_definition_p;
6728
6729 saw_declarator = true;
6730 /* Parse the init-declarator. */
6731 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6732 access_checks,
6733 function_definition_allowed_p,
6734 /*member_p=*/false,
6735 &function_definition_p);
6736 /* Handle function definitions specially. */
6737 if (function_definition_p)
6738 {
6739 /* If the next token is a `,', then we are probably
6740 processing something like:
6741
6742 void f() {}, *p;
6743
6744 which is erroneous. */
6745 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6746 error ("mixing declarations and function-definitions is forbidden");
6747 /* Otherwise, we're done with the list of declarators. */
6748 else
6749 return;
6750 }
6751 /* The next token should be either a `,' or a `;'. */
6752 token = cp_lexer_peek_token (parser->lexer);
6753 /* If it's a `,', there are more declarators to come. */
6754 if (token->type == CPP_COMMA)
6755 cp_lexer_consume_token (parser->lexer);
6756 /* If it's a `;', we are done. */
6757 else if (token->type == CPP_SEMICOLON)
6758 break;
6759 /* Anything else is an error. */
6760 else
6761 {
6762 cp_parser_error (parser, "expected `,' or `;'");
6763 /* Skip tokens until we reach the end of the statement. */
6764 cp_parser_skip_to_end_of_statement (parser);
6765 return;
6766 }
6767 /* After the first time around, a function-definition is not
6768 allowed -- even if it was OK at first. For example:
6769
6770 int i, f() {}
6771
6772 is not valid. */
6773 function_definition_allowed_p = false;
6774 }
6775
6776 /* Issue an error message if no declarators are present, and the
6777 decl-specifier-seq does not itself declare a class or
6778 enumeration. */
6779 if (!saw_declarator)
6780 {
6781 if (cp_parser_declares_only_class_p (parser))
6782 shadow_tag (decl_specifiers);
6783 /* Perform any deferred access checks. */
6784 cp_parser_perform_deferred_access_checks (access_checks);
6785 }
6786
6787 /* Consume the `;'. */
6788 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6789
6790 /* Mark all the classes that appeared in the decl-specifier-seq as
6791 having received a `;'. */
6792 note_list_got_semicolon (decl_specifiers);
6793}
6794
6795/* Parse a decl-specifier-seq.
6796
6797 decl-specifier-seq:
6798 decl-specifier-seq [opt] decl-specifier
6799
6800 decl-specifier:
6801 storage-class-specifier
6802 type-specifier
6803 function-specifier
6804 friend
6805 typedef
6806
6807 GNU Extension:
6808
6809 decl-specifier-seq:
6810 decl-specifier-seq [opt] attributes
6811
6812 Returns a TREE_LIST, giving the decl-specifiers in the order they
6813 appear in the source code. The TREE_VALUE of each node is the
6814 decl-specifier. For a keyword (such as `auto' or `friend'), the
6815 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6816 representation of a type-specifier, see cp_parser_type_specifier.
6817
6818 If there are attributes, they will be stored in *ATTRIBUTES,
6819 represented as described above cp_parser_attributes.
6820
6821 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6822 appears, and the entity that will be a friend is not going to be a
6823 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6824 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6825 friendship is granted might not be a class. */
6826
6827static tree
6828cp_parser_decl_specifier_seq (parser, flags, attributes,
6829 declares_class_or_enum)
6830 cp_parser *parser;
6831 cp_parser_flags flags;
6832 tree *attributes;
6833 bool *declares_class_or_enum;
6834{
6835 tree decl_specs = NULL_TREE;
6836 bool friend_p = false;
6837
6838 /* Assume no class or enumeration type is declared. */
6839 *declares_class_or_enum = false;
6840
6841 /* Assume there are no attributes. */
6842 *attributes = NULL_TREE;
6843
6844 /* Keep reading specifiers until there are no more to read. */
6845 while (true)
6846 {
6847 tree decl_spec = NULL_TREE;
6848 bool constructor_p;
6849 cp_token *token;
6850
6851 /* Peek at the next token. */
6852 token = cp_lexer_peek_token (parser->lexer);
6853 /* Handle attributes. */
6854 if (token->keyword == RID_ATTRIBUTE)
6855 {
6856 /* Parse the attributes. */
6857 decl_spec = cp_parser_attributes_opt (parser);
6858 /* Add them to the list. */
6859 *attributes = chainon (*attributes, decl_spec);
6860 continue;
6861 }
6862 /* If the next token is an appropriate keyword, we can simply
6863 add it to the list. */
6864 switch (token->keyword)
6865 {
6866 case RID_FRIEND:
6867 /* decl-specifier:
6868 friend */
6869 friend_p = true;
6870 /* The representation of the specifier is simply the
6871 appropriate TREE_IDENTIFIER node. */
6872 decl_spec = token->value;
6873 /* Consume the token. */
6874 cp_lexer_consume_token (parser->lexer);
6875 break;
6876
6877 /* function-specifier:
6878 inline
6879 virtual
6880 explicit */
6881 case RID_INLINE:
6882 case RID_VIRTUAL:
6883 case RID_EXPLICIT:
6884 decl_spec = cp_parser_function_specifier_opt (parser);
6885 break;
6886
6887 /* decl-specifier:
6888 typedef */
6889 case RID_TYPEDEF:
6890 /* The representation of the specifier is simply the
6891 appropriate TREE_IDENTIFIER node. */
6892 decl_spec = token->value;
6893 /* Consume the token. */
6894 cp_lexer_consume_token (parser->lexer);
6895 break;
6896
6897 /* storage-class-specifier:
6898 auto
6899 register
6900 static
6901 extern
6902 mutable
6903
6904 GNU Extension:
6905 thread */
6906 case RID_AUTO:
6907 case RID_REGISTER:
6908 case RID_STATIC:
6909 case RID_EXTERN:
6910 case RID_MUTABLE:
6911 case RID_THREAD:
6912 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6913 break;
6914
6915 default:
6916 break;
6917 }
6918
6919 /* Constructors are a special case. The `S' in `S()' is not a
6920 decl-specifier; it is the beginning of the declarator. */
6921 constructor_p = (!decl_spec
6922 && cp_parser_constructor_declarator_p (parser,
6923 friend_p));
6924
6925 /* If we don't have a DECL_SPEC yet, then we must be looking at
6926 a type-specifier. */
6927 if (!decl_spec && !constructor_p)
6928 {
6929 bool decl_spec_declares_class_or_enum;
6930 bool is_cv_qualifier;
6931
6932 decl_spec
6933 = cp_parser_type_specifier (parser, flags,
6934 friend_p,
6935 /*is_declaration=*/true,
6936 &decl_spec_declares_class_or_enum,
6937 &is_cv_qualifier);
6938
6939 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6940
6941 /* If this type-specifier referenced a user-defined type
6942 (a typedef, class-name, etc.), then we can't allow any
6943 more such type-specifiers henceforth.
6944
6945 [dcl.spec]
6946
6947 The longest sequence of decl-specifiers that could
6948 possibly be a type name is taken as the
6949 decl-specifier-seq of a declaration. The sequence shall
6950 be self-consistent as described below.
6951
6952 [dcl.type]
6953
6954 As a general rule, at most one type-specifier is allowed
6955 in the complete decl-specifier-seq of a declaration. The
6956 only exceptions are the following:
6957
6958 -- const or volatile can be combined with any other
6959 type-specifier.
6960
6961 -- signed or unsigned can be combined with char, long,
6962 short, or int.
6963
6964 -- ..
6965
6966 Example:
6967
6968 typedef char* Pc;
6969 void g (const int Pc);
6970
6971 Here, Pc is *not* part of the decl-specifier seq; it's
6972 the declarator. Therefore, once we see a type-specifier
6973 (other than a cv-qualifier), we forbid any additional
6974 user-defined types. We *do* still allow things like `int
6975 int' to be considered a decl-specifier-seq, and issue the
6976 error message later. */
6977 if (decl_spec && !is_cv_qualifier)
6978 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6979 }
6980
6981 /* If we still do not have a DECL_SPEC, then there are no more
6982 decl-specifiers. */
6983 if (!decl_spec)
6984 {
6985 /* Issue an error message, unless the entire construct was
6986 optional. */
6987 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6988 {
6989 cp_parser_error (parser, "expected decl specifier");
6990 return error_mark_node;
6991 }
6992
6993 break;
6994 }
6995
6996 /* Add the DECL_SPEC to the list of specifiers. */
6997 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6998
6999 /* After we see one decl-specifier, further decl-specifiers are
7000 always optional. */
7001 flags |= CP_PARSER_FLAGS_OPTIONAL;
7002 }
7003
7004 /* We have built up the DECL_SPECS in reverse order. Return them in
7005 the correct order. */
7006 return nreverse (decl_specs);
7007}
7008
7009/* Parse an (optional) storage-class-specifier.
7010
7011 storage-class-specifier:
7012 auto
7013 register
7014 static
7015 extern
7016 mutable
7017
7018 GNU Extension:
7019
7020 storage-class-specifier:
7021 thread
7022
7023 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7024
7025static tree
7026cp_parser_storage_class_specifier_opt (parser)
7027 cp_parser *parser;
7028{
7029 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7030 {
7031 case RID_AUTO:
7032 case RID_REGISTER:
7033 case RID_STATIC:
7034 case RID_EXTERN:
7035 case RID_MUTABLE:
7036 case RID_THREAD:
7037 /* Consume the token. */
7038 return cp_lexer_consume_token (parser->lexer)->value;
7039
7040 default:
7041 return NULL_TREE;
7042 }
7043}
7044
7045/* Parse an (optional) function-specifier.
7046
7047 function-specifier:
7048 inline
7049 virtual
7050 explicit
7051
7052 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7053
7054static tree
7055cp_parser_function_specifier_opt (parser)
7056 cp_parser *parser;
7057{
7058 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7059 {
7060 case RID_INLINE:
7061 case RID_VIRTUAL:
7062 case RID_EXPLICIT:
7063 /* Consume the token. */
7064 return cp_lexer_consume_token (parser->lexer)->value;
7065
7066 default:
7067 return NULL_TREE;
7068 }
7069}
7070
7071/* Parse a linkage-specification.
7072
7073 linkage-specification:
7074 extern string-literal { declaration-seq [opt] }
7075 extern string-literal declaration */
7076
7077static void
7078cp_parser_linkage_specification (parser)
7079 cp_parser *parser;
7080{
7081 cp_token *token;
7082 tree linkage;
7083
7084 /* Look for the `extern' keyword. */
7085 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7086
7087 /* Peek at the next token. */
7088 token = cp_lexer_peek_token (parser->lexer);
7089 /* If it's not a string-literal, then there's a problem. */
7090 if (!cp_parser_is_string_literal (token))
7091 {
7092 cp_parser_error (parser, "expected language-name");
7093 return;
7094 }
7095 /* Consume the token. */
7096 cp_lexer_consume_token (parser->lexer);
7097
7098 /* Transform the literal into an identifier. If the literal is a
7099 wide-character string, or contains embedded NULs, then we can't
7100 handle it as the user wants. */
7101 if (token->type == CPP_WSTRING
7102 || (strlen (TREE_STRING_POINTER (token->value))
7103 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7104 {
7105 cp_parser_error (parser, "invalid linkage-specification");
7106 /* Assume C++ linkage. */
7107 linkage = get_identifier ("c++");
7108 }
7109 /* If it's a simple string constant, things are easier. */
7110 else
7111 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7112
7113 /* We're now using the new linkage. */
7114 push_lang_context (linkage);
7115
7116 /* If the next token is a `{', then we're using the first
7117 production. */
7118 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7119 {
7120 /* Consume the `{' token. */
7121 cp_lexer_consume_token (parser->lexer);
7122 /* Parse the declarations. */
7123 cp_parser_declaration_seq_opt (parser);
7124 /* Look for the closing `}'. */
7125 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7126 }
7127 /* Otherwise, there's just one declaration. */
7128 else
7129 {
7130 bool saved_in_unbraced_linkage_specification_p;
7131
7132 saved_in_unbraced_linkage_specification_p
7133 = parser->in_unbraced_linkage_specification_p;
7134 parser->in_unbraced_linkage_specification_p = true;
7135 have_extern_spec = true;
7136 cp_parser_declaration (parser);
7137 have_extern_spec = false;
7138 parser->in_unbraced_linkage_specification_p
7139 = saved_in_unbraced_linkage_specification_p;
7140 }
7141
7142 /* We're done with the linkage-specification. */
7143 pop_lang_context ();
7144}
7145
7146/* Special member functions [gram.special] */
7147
7148/* Parse a conversion-function-id.
7149
7150 conversion-function-id:
7151 operator conversion-type-id
7152
7153 Returns an IDENTIFIER_NODE representing the operator. */
7154
7155static tree
7156cp_parser_conversion_function_id (parser)
7157 cp_parser *parser;
7158{
7159 tree type;
7160 tree saved_scope;
7161 tree saved_qualifying_scope;
7162 tree saved_object_scope;
7163
7164 /* Look for the `operator' token. */
7165 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7166 return error_mark_node;
7167 /* When we parse the conversion-type-id, the current scope will be
7168 reset. However, we need that information in able to look up the
7169 conversion function later, so we save it here. */
7170 saved_scope = parser->scope;
7171 saved_qualifying_scope = parser->qualifying_scope;
7172 saved_object_scope = parser->object_scope;
7173 /* We must enter the scope of the class so that the names of
7174 entities declared within the class are available in the
7175 conversion-type-id. For example, consider:
7176
7177 struct S {
7178 typedef int I;
7179 operator I();
7180 };
7181
7182 S::operator I() { ... }
7183
7184 In order to see that `I' is a type-name in the definition, we
7185 must be in the scope of `S'. */
7186 if (saved_scope)
7187 push_scope (saved_scope);
7188 /* Parse the conversion-type-id. */
7189 type = cp_parser_conversion_type_id (parser);
7190 /* Leave the scope of the class, if any. */
7191 if (saved_scope)
7192 pop_scope (saved_scope);
7193 /* Restore the saved scope. */
7194 parser->scope = saved_scope;
7195 parser->qualifying_scope = saved_qualifying_scope;
7196 parser->object_scope = saved_object_scope;
7197 /* If the TYPE is invalid, indicate failure. */
7198 if (type == error_mark_node)
7199 return error_mark_node;
7200 return mangle_conv_op_name_for_type (type);
7201}
7202
7203/* Parse a conversion-type-id:
7204
7205 conversion-type-id:
7206 type-specifier-seq conversion-declarator [opt]
7207
7208 Returns the TYPE specified. */
7209
7210static tree
7211cp_parser_conversion_type_id (parser)
7212 cp_parser *parser;
7213{
7214 tree attributes;
7215 tree type_specifiers;
7216 tree declarator;
7217
7218 /* Parse the attributes. */
7219 attributes = cp_parser_attributes_opt (parser);
7220 /* Parse the type-specifiers. */
7221 type_specifiers = cp_parser_type_specifier_seq (parser);
7222 /* If that didn't work, stop. */
7223 if (type_specifiers == error_mark_node)
7224 return error_mark_node;
7225 /* Parse the conversion-declarator. */
7226 declarator = cp_parser_conversion_declarator_opt (parser);
7227
7228 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7229 /*initialized=*/0, &attributes);
7230}
7231
7232/* Parse an (optional) conversion-declarator.
7233
7234 conversion-declarator:
7235 ptr-operator conversion-declarator [opt]
7236
7237 Returns a representation of the declarator. See
7238 cp_parser_declarator for details. */
7239
7240static tree
7241cp_parser_conversion_declarator_opt (parser)
7242 cp_parser *parser;
7243{
7244 enum tree_code code;
7245 tree class_type;
7246 tree cv_qualifier_seq;
7247
7248 /* We don't know if there's a ptr-operator next, or not. */
7249 cp_parser_parse_tentatively (parser);
7250 /* Try the ptr-operator. */
7251 code = cp_parser_ptr_operator (parser, &class_type,
7252 &cv_qualifier_seq);
7253 /* If it worked, look for more conversion-declarators. */
7254 if (cp_parser_parse_definitely (parser))
7255 {
7256 tree declarator;
7257
7258 /* Parse another optional declarator. */
7259 declarator = cp_parser_conversion_declarator_opt (parser);
7260
7261 /* Create the representation of the declarator. */
7262 if (code == INDIRECT_REF)
7263 declarator = make_pointer_declarator (cv_qualifier_seq,
7264 declarator);
7265 else
7266 declarator = make_reference_declarator (cv_qualifier_seq,
7267 declarator);
7268
7269 /* Handle the pointer-to-member case. */
7270 if (class_type)
7271 declarator = build_nt (SCOPE_REF, class_type, declarator);
7272
7273 return declarator;
7274 }
7275
7276 return NULL_TREE;
7277}
7278
7279/* Parse an (optional) ctor-initializer.
7280
7281 ctor-initializer:
7282 : mem-initializer-list
7283
7284 Returns TRUE iff the ctor-initializer was actually present. */
7285
7286static bool
7287cp_parser_ctor_initializer_opt (parser)
7288 cp_parser *parser;
7289{
7290 /* If the next token is not a `:', then there is no
7291 ctor-initializer. */
7292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7293 {
7294 /* Do default initialization of any bases and members. */
7295 if (DECL_CONSTRUCTOR_P (current_function_decl))
7296 finish_mem_initializers (NULL_TREE);
7297
7298 return false;
7299 }
7300
7301 /* Consume the `:' token. */
7302 cp_lexer_consume_token (parser->lexer);
7303 /* And the mem-initializer-list. */
7304 cp_parser_mem_initializer_list (parser);
7305
7306 return true;
7307}
7308
7309/* Parse a mem-initializer-list.
7310
7311 mem-initializer-list:
7312 mem-initializer
7313 mem-initializer , mem-initializer-list */
7314
7315static void
7316cp_parser_mem_initializer_list (parser)
7317 cp_parser *parser;
7318{
7319 tree mem_initializer_list = NULL_TREE;
7320
7321 /* Let the semantic analysis code know that we are starting the
7322 mem-initializer-list. */
7323 begin_mem_initializers ();
7324
7325 /* Loop through the list. */
7326 while (true)
7327 {
7328 tree mem_initializer;
7329
7330 /* Parse the mem-initializer. */
7331 mem_initializer = cp_parser_mem_initializer (parser);
7332 /* Add it to the list, unless it was erroneous. */
7333 if (mem_initializer)
7334 {
7335 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7336 mem_initializer_list = mem_initializer;
7337 }
7338 /* If the next token is not a `,', we're done. */
7339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7340 break;
7341 /* Consume the `,' token. */
7342 cp_lexer_consume_token (parser->lexer);
7343 }
7344
7345 /* Perform semantic analysis. */
7346 finish_mem_initializers (mem_initializer_list);
7347}
7348
7349/* Parse a mem-initializer.
7350
7351 mem-initializer:
7352 mem-initializer-id ( expression-list [opt] )
7353
7354 GNU extension:
7355
7356 mem-initializer:
7357 ( expresion-list [opt] )
7358
7359 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7360 class) or FIELD_DECL (for a non-static data member) to initialize;
7361 the TREE_VALUE is the expression-list. */
7362
7363static tree
7364cp_parser_mem_initializer (parser)
7365 cp_parser *parser;
7366{
7367 tree mem_initializer_id;
7368 tree expression_list;
7369
7370 /* Find out what is being initialized. */
7371 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7372 {
7373 pedwarn ("anachronistic old-style base class initializer");
7374 mem_initializer_id = NULL_TREE;
7375 }
7376 else
7377 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7378 /* Look for the opening `('. */
7379 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7380 /* Parse the expression-list. */
7381 if (cp_lexer_next_token_is_not (parser->lexer,
7382 CPP_CLOSE_PAREN))
7383 expression_list = cp_parser_expression_list (parser);
7384 else
7385 expression_list = void_type_node;
7386 /* Look for the closing `)'. */
7387 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7388
7389 return expand_member_init (mem_initializer_id,
7390 expression_list);
7391}
7392
7393/* Parse a mem-initializer-id.
7394
7395 mem-initializer-id:
7396 :: [opt] nested-name-specifier [opt] class-name
7397 identifier
7398
7399 Returns a TYPE indicating the class to be initializer for the first
7400 production. Returns an IDENTIFIER_NODE indicating the data member
7401 to be initialized for the second production. */
7402
7403static tree
7404cp_parser_mem_initializer_id (parser)
7405 cp_parser *parser;
7406{
7407 bool global_scope_p;
7408 bool nested_name_specifier_p;
7409 tree id;
7410
7411 /* Look for the optional `::' operator. */
7412 global_scope_p
7413 = (cp_parser_global_scope_opt (parser,
7414 /*current_scope_valid_p=*/false)
7415 != NULL_TREE);
7416 /* Look for the optional nested-name-specifier. The simplest way to
7417 implement:
7418
7419 [temp.res]
7420
7421 The keyword `typename' is not permitted in a base-specifier or
7422 mem-initializer; in these contexts a qualified name that
7423 depends on a template-parameter is implicitly assumed to be a
7424 type name.
7425
7426 is to assume that we have seen the `typename' keyword at this
7427 point. */
7428 nested_name_specifier_p
7429 = (cp_parser_nested_name_specifier_opt (parser,
7430 /*typename_keyword_p=*/true,
7431 /*check_dependency_p=*/true,
7432 /*type_p=*/true)
7433 != NULL_TREE);
7434 /* If there is a `::' operator or a nested-name-specifier, then we
7435 are definitely looking for a class-name. */
7436 if (global_scope_p || nested_name_specifier_p)
7437 return cp_parser_class_name (parser,
7438 /*typename_keyword_p=*/true,
7439 /*template_keyword_p=*/false,
7440 /*type_p=*/false,
7441 /*check_access_p=*/true,
7442 /*check_dependency_p=*/true,
7443 /*class_head_p=*/false);
7444 /* Otherwise, we could also be looking for an ordinary identifier. */
7445 cp_parser_parse_tentatively (parser);
7446 /* Try a class-name. */
7447 id = cp_parser_class_name (parser,
7448 /*typename_keyword_p=*/true,
7449 /*template_keyword_p=*/false,
7450 /*type_p=*/false,
7451 /*check_access_p=*/true,
7452 /*check_dependency_p=*/true,
7453 /*class_head_p=*/false);
7454 /* If we found one, we're done. */
7455 if (cp_parser_parse_definitely (parser))
7456 return id;
7457 /* Otherwise, look for an ordinary identifier. */
7458 return cp_parser_identifier (parser);
7459}
7460
7461/* Overloading [gram.over] */
7462
7463/* Parse an operator-function-id.
7464
7465 operator-function-id:
7466 operator operator
7467
7468 Returns an IDENTIFIER_NODE for the operator which is a
7469 human-readable spelling of the identifier, e.g., `operator +'. */
7470
7471static tree
7472cp_parser_operator_function_id (parser)
7473 cp_parser *parser;
7474{
7475 /* Look for the `operator' keyword. */
7476 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7477 return error_mark_node;
7478 /* And then the name of the operator itself. */
7479 return cp_parser_operator (parser);
7480}
7481
7482/* Parse an operator.
7483
7484 operator:
7485 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7486 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7487 || ++ -- , ->* -> () []
7488
7489 GNU Extensions:
7490
7491 operator:
7492 <? >? <?= >?=
7493
7494 Returns an IDENTIFIER_NODE for the operator which is a
7495 human-readable spelling of the identifier, e.g., `operator +'. */
7496
7497static tree
7498cp_parser_operator (parser)
7499 cp_parser *parser;
7500{
7501 tree id = NULL_TREE;
7502 cp_token *token;
7503
7504 /* Peek at the next token. */
7505 token = cp_lexer_peek_token (parser->lexer);
7506 /* Figure out which operator we have. */
7507 switch (token->type)
7508 {
7509 case CPP_KEYWORD:
7510 {
7511 enum tree_code op;
7512
7513 /* The keyword should be either `new' or `delete'. */
7514 if (token->keyword == RID_NEW)
7515 op = NEW_EXPR;
7516 else if (token->keyword == RID_DELETE)
7517 op = DELETE_EXPR;
7518 else
7519 break;
7520
7521 /* Consume the `new' or `delete' token. */
7522 cp_lexer_consume_token (parser->lexer);
7523
7524 /* Peek at the next token. */
7525 token = cp_lexer_peek_token (parser->lexer);
7526 /* If it's a `[' token then this is the array variant of the
7527 operator. */
7528 if (token->type == CPP_OPEN_SQUARE)
7529 {
7530 /* Consume the `[' token. */
7531 cp_lexer_consume_token (parser->lexer);
7532 /* Look for the `]' token. */
7533 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7534 id = ansi_opname (op == NEW_EXPR
7535 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7536 }
7537 /* Otherwise, we have the non-array variant. */
7538 else
7539 id = ansi_opname (op);
7540
7541 return id;
7542 }
7543
7544 case CPP_PLUS:
7545 id = ansi_opname (PLUS_EXPR);
7546 break;
7547
7548 case CPP_MINUS:
7549 id = ansi_opname (MINUS_EXPR);
7550 break;
7551
7552 case CPP_MULT:
7553 id = ansi_opname (MULT_EXPR);
7554 break;
7555
7556 case CPP_DIV:
7557 id = ansi_opname (TRUNC_DIV_EXPR);
7558 break;
7559
7560 case CPP_MOD:
7561 id = ansi_opname (TRUNC_MOD_EXPR);
7562 break;
7563
7564 case CPP_XOR:
7565 id = ansi_opname (BIT_XOR_EXPR);
7566 break;
7567
7568 case CPP_AND:
7569 id = ansi_opname (BIT_AND_EXPR);
7570 break;
7571
7572 case CPP_OR:
7573 id = ansi_opname (BIT_IOR_EXPR);
7574 break;
7575
7576 case CPP_COMPL:
7577 id = ansi_opname (BIT_NOT_EXPR);
7578 break;
7579
7580 case CPP_NOT:
7581 id = ansi_opname (TRUTH_NOT_EXPR);
7582 break;
7583
7584 case CPP_EQ:
7585 id = ansi_assopname (NOP_EXPR);
7586 break;
7587
7588 case CPP_LESS:
7589 id = ansi_opname (LT_EXPR);
7590 break;
7591
7592 case CPP_GREATER:
7593 id = ansi_opname (GT_EXPR);
7594 break;
7595
7596 case CPP_PLUS_EQ:
7597 id = ansi_assopname (PLUS_EXPR);
7598 break;
7599
7600 case CPP_MINUS_EQ:
7601 id = ansi_assopname (MINUS_EXPR);
7602 break;
7603
7604 case CPP_MULT_EQ:
7605 id = ansi_assopname (MULT_EXPR);
7606 break;
7607
7608 case CPP_DIV_EQ:
7609 id = ansi_assopname (TRUNC_DIV_EXPR);
7610 break;
7611
7612 case CPP_MOD_EQ:
7613 id = ansi_assopname (TRUNC_MOD_EXPR);
7614 break;
7615
7616 case CPP_XOR_EQ:
7617 id = ansi_assopname (BIT_XOR_EXPR);
7618 break;
7619
7620 case CPP_AND_EQ:
7621 id = ansi_assopname (BIT_AND_EXPR);
7622 break;
7623
7624 case CPP_OR_EQ:
7625 id = ansi_assopname (BIT_IOR_EXPR);
7626 break;
7627
7628 case CPP_LSHIFT:
7629 id = ansi_opname (LSHIFT_EXPR);
7630 break;
7631
7632 case CPP_RSHIFT:
7633 id = ansi_opname (RSHIFT_EXPR);
7634 break;
7635
7636 case CPP_LSHIFT_EQ:
7637 id = ansi_assopname (LSHIFT_EXPR);
7638 break;
7639
7640 case CPP_RSHIFT_EQ:
7641 id = ansi_assopname (RSHIFT_EXPR);
7642 break;
7643
7644 case CPP_EQ_EQ:
7645 id = ansi_opname (EQ_EXPR);
7646 break;
7647
7648 case CPP_NOT_EQ:
7649 id = ansi_opname (NE_EXPR);
7650 break;
7651
7652 case CPP_LESS_EQ:
7653 id = ansi_opname (LE_EXPR);
7654 break;
7655
7656 case CPP_GREATER_EQ:
7657 id = ansi_opname (GE_EXPR);
7658 break;
7659
7660 case CPP_AND_AND:
7661 id = ansi_opname (TRUTH_ANDIF_EXPR);
7662 break;
7663
7664 case CPP_OR_OR:
7665 id = ansi_opname (TRUTH_ORIF_EXPR);
7666 break;
7667
7668 case CPP_PLUS_PLUS:
7669 id = ansi_opname (POSTINCREMENT_EXPR);
7670 break;
7671
7672 case CPP_MINUS_MINUS:
7673 id = ansi_opname (PREDECREMENT_EXPR);
7674 break;
7675
7676 case CPP_COMMA:
7677 id = ansi_opname (COMPOUND_EXPR);
7678 break;
7679
7680 case CPP_DEREF_STAR:
7681 id = ansi_opname (MEMBER_REF);
7682 break;
7683
7684 case CPP_DEREF:
7685 id = ansi_opname (COMPONENT_REF);
7686 break;
7687
7688 case CPP_OPEN_PAREN:
7689 /* Consume the `('. */
7690 cp_lexer_consume_token (parser->lexer);
7691 /* Look for the matching `)'. */
7692 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7693 return ansi_opname (CALL_EXPR);
7694
7695 case CPP_OPEN_SQUARE:
7696 /* Consume the `['. */
7697 cp_lexer_consume_token (parser->lexer);
7698 /* Look for the matching `]'. */
7699 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7700 return ansi_opname (ARRAY_REF);
7701
7702 /* Extensions. */
7703 case CPP_MIN:
7704 id = ansi_opname (MIN_EXPR);
7705 break;
7706
7707 case CPP_MAX:
7708 id = ansi_opname (MAX_EXPR);
7709 break;
7710
7711 case CPP_MIN_EQ:
7712 id = ansi_assopname (MIN_EXPR);
7713 break;
7714
7715 case CPP_MAX_EQ:
7716 id = ansi_assopname (MAX_EXPR);
7717 break;
7718
7719 default:
7720 /* Anything else is an error. */
7721 break;
7722 }
7723
7724 /* If we have selected an identifier, we need to consume the
7725 operator token. */
7726 if (id)
7727 cp_lexer_consume_token (parser->lexer);
7728 /* Otherwise, no valid operator name was present. */
7729 else
7730 {
7731 cp_parser_error (parser, "expected operator");
7732 id = error_mark_node;
7733 }
7734
7735 return id;
7736}
7737
7738/* Parse a template-declaration.
7739
7740 template-declaration:
7741 export [opt] template < template-parameter-list > declaration
7742
7743 If MEMBER_P is TRUE, this template-declaration occurs within a
7744 class-specifier.
7745
7746 The grammar rule given by the standard isn't correct. What
7747 is really meant is:
7748
7749 template-declaration:
7750 export [opt] template-parameter-list-seq
7751 decl-specifier-seq [opt] init-declarator [opt] ;
7752 export [opt] template-parameter-list-seq
7753 function-definition
7754
7755 template-parameter-list-seq:
7756 template-parameter-list-seq [opt]
7757 template < template-parameter-list > */
7758
7759static void
7760cp_parser_template_declaration (parser, member_p)
7761 cp_parser *parser;
7762 bool member_p;
7763{
7764 /* Check for `export'. */
7765 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7766 {
7767 /* Consume the `export' token. */
7768 cp_lexer_consume_token (parser->lexer);
7769 /* Warn that we do not support `export'. */
7770 warning ("keyword `export' not implemented, and will be ignored");
7771 }
7772
7773 cp_parser_template_declaration_after_export (parser, member_p);
7774}
7775
7776/* Parse a template-parameter-list.
7777
7778 template-parameter-list:
7779 template-parameter
7780 template-parameter-list , template-parameter
7781
7782 Returns a TREE_LIST. Each node represents a template parameter.
7783 The nodes are connected via their TREE_CHAINs. */
7784
7785static tree
7786cp_parser_template_parameter_list (parser)
7787 cp_parser *parser;
7788{
7789 tree parameter_list = NULL_TREE;
7790
7791 while (true)
7792 {
7793 tree parameter;
7794 cp_token *token;
7795
7796 /* Parse the template-parameter. */
7797 parameter = cp_parser_template_parameter (parser);
7798 /* Add it to the list. */
7799 parameter_list = process_template_parm (parameter_list,
7800 parameter);
7801
7802 /* Peek at the next token. */
7803 token = cp_lexer_peek_token (parser->lexer);
7804 /* If it's not a `,', we're done. */
7805 if (token->type != CPP_COMMA)
7806 break;
7807 /* Otherwise, consume the `,' token. */
7808 cp_lexer_consume_token (parser->lexer);
7809 }
7810
7811 return parameter_list;
7812}
7813
7814/* Parse a template-parameter.
7815
7816 template-parameter:
7817 type-parameter
7818 parameter-declaration
7819
7820 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7821 TREE_PURPOSE is the default value, if any. */
7822
7823static tree
7824cp_parser_template_parameter (parser)
7825 cp_parser *parser;
7826{
7827 cp_token *token;
7828
7829 /* Peek at the next token. */
7830 token = cp_lexer_peek_token (parser->lexer);
7831 /* If it is `class' or `template', we have a type-parameter. */
7832 if (token->keyword == RID_TEMPLATE)
7833 return cp_parser_type_parameter (parser);
7834 /* If it is `class' or `typename' we do not know yet whether it is a
7835 type parameter or a non-type parameter. Consider:
7836
7837 template <typename T, typename T::X X> ...
7838
7839 or:
7840
7841 template <class C, class D*> ...
7842
7843 Here, the first parameter is a type parameter, and the second is
7844 a non-type parameter. We can tell by looking at the token after
7845 the identifier -- if it is a `,', `=', or `>' then we have a type
7846 parameter. */
7847 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7848 {
7849 /* Peek at the token after `class' or `typename'. */
7850 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7851 /* If it's an identifier, skip it. */
7852 if (token->type == CPP_NAME)
7853 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7854 /* Now, see if the token looks like the end of a template
7855 parameter. */
7856 if (token->type == CPP_COMMA
7857 || token->type == CPP_EQ
7858 || token->type == CPP_GREATER)
7859 return cp_parser_type_parameter (parser);
7860 }
7861
7862 /* Otherwise, it is a non-type parameter.
7863
7864 [temp.param]
7865
7866 When parsing a default template-argument for a non-type
7867 template-parameter, the first non-nested `>' is taken as the end
7868 of the template parameter-list rather than a greater-than
7869 operator. */
7870 return
7871 cp_parser_parameter_declaration (parser,
7872 /*greater_than_is_operator_p=*/false);
7873}
7874
7875/* Parse a type-parameter.
7876
7877 type-parameter:
7878 class identifier [opt]
7879 class identifier [opt] = type-id
7880 typename identifier [opt]
7881 typename identifier [opt] = type-id
7882 template < template-parameter-list > class identifier [opt]
7883 template < template-parameter-list > class identifier [opt]
7884 = id-expression
7885
7886 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7887 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7888 the declaration of the parameter. */
7889
7890static tree
7891cp_parser_type_parameter (parser)
7892 cp_parser *parser;
7893{
7894 cp_token *token;
7895 tree parameter;
7896
7897 /* Look for a keyword to tell us what kind of parameter this is. */
7898 token = cp_parser_require (parser, CPP_KEYWORD,
7899 "expected `class', `typename', or `template'");
7900 if (!token)
7901 return error_mark_node;
7902
7903 switch (token->keyword)
7904 {
7905 case RID_CLASS:
7906 case RID_TYPENAME:
7907 {
7908 tree identifier;
7909 tree default_argument;
7910
7911 /* If the next token is an identifier, then it names the
7912 parameter. */
7913 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7914 identifier = cp_parser_identifier (parser);
7915 else
7916 identifier = NULL_TREE;
7917
7918 /* Create the parameter. */
7919 parameter = finish_template_type_parm (class_type_node, identifier);
7920
7921 /* If the next token is an `=', we have a default argument. */
7922 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7923 {
7924 /* Consume the `=' token. */
7925 cp_lexer_consume_token (parser->lexer);
7926 /* Parse the default-argumen. */
7927 default_argument = cp_parser_type_id (parser);
7928 }
7929 else
7930 default_argument = NULL_TREE;
7931
7932 /* Create the combined representation of the parameter and the
7933 default argument. */
7934 parameter = build_tree_list (default_argument,
7935 parameter);
7936 }
7937 break;
7938
7939 case RID_TEMPLATE:
7940 {
7941 tree parameter_list;
7942 tree identifier;
7943 tree default_argument;
7944
7945 /* Look for the `<'. */
7946 cp_parser_require (parser, CPP_LESS, "`<'");
7947 /* Parse the template-parameter-list. */
7948 begin_template_parm_list ();
7949 parameter_list
7950 = cp_parser_template_parameter_list (parser);
7951 parameter_list = end_template_parm_list (parameter_list);
7952 /* Look for the `>'. */
7953 cp_parser_require (parser, CPP_GREATER, "`>'");
7954 /* Look for the `class' keyword. */
7955 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7956 /* If the next token is an `=', then there is a
7957 default-argument. If the next token is a `>', we are at
7958 the end of the parameter-list. If the next token is a `,',
7959 then we are at the end of this parameter. */
7960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7961 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7962 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7963 identifier = cp_parser_identifier (parser);
7964 else
7965 identifier = NULL_TREE;
7966 /* Create the template parameter. */
7967 parameter = finish_template_template_parm (class_type_node,
7968 identifier);
7969
7970 /* If the next token is an `=', then there is a
7971 default-argument. */
7972 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7973 {
7974 /* Consume the `='. */
7975 cp_lexer_consume_token (parser->lexer);
7976 /* Parse the id-expression. */
7977 default_argument
7978 = cp_parser_id_expression (parser,
7979 /*template_keyword_p=*/false,
7980 /*check_dependency_p=*/true,
7981 /*template_p=*/NULL);
7982 /* Look up the name. */
7983 default_argument
7984 = cp_parser_lookup_name_simple (parser, default_argument);
7985 /* See if the default argument is valid. */
7986 default_argument
7987 = check_template_template_default_arg (default_argument);
7988 }
7989 else
7990 default_argument = NULL_TREE;
7991
7992 /* Create the combined representation of the parameter and the
7993 default argument. */
7994 parameter = build_tree_list (default_argument,
7995 parameter);
7996 }
7997 break;
7998
7999 default:
8000 /* Anything else is an error. */
8001 cp_parser_error (parser,
8002 "expected `class', `typename', or `template'");
8003 parameter = error_mark_node;
8004 }
8005
8006 return parameter;
8007}
8008
8009/* Parse a template-id.
8010
8011 template-id:
8012 template-name < template-argument-list [opt] >
8013
8014 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8015 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8016 returned. Otherwise, if the template-name names a function, or set
8017 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8018 names a class, returns a TYPE_DECL for the specialization.
8019
8020 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8021 uninstantiated templates. */
8022
8023static tree
8024cp_parser_template_id (cp_parser *parser,
8025 bool template_keyword_p,
8026 bool check_dependency_p)
8027{
8028 tree template;
8029 tree arguments;
8030 tree saved_scope;
8031 tree saved_qualifying_scope;
8032 tree saved_object_scope;
8033 tree template_id;
8034 bool saved_greater_than_is_operator_p;
8035 ptrdiff_t start_of_id;
8036 tree access_check = NULL_TREE;
8037
8038 /* If the next token corresponds to a template-id, there is no need
8039 to reparse it. */
8040 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
8041 {
8042 tree value;
8043 tree check;
8044
8045 /* Get the stored value. */
8046 value = cp_lexer_consume_token (parser->lexer)->value;
8047 /* Perform any access checks that were deferred. */
8048 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8049 cp_parser_defer_access_check (parser,
8050 TREE_PURPOSE (check),
8051 TREE_VALUE (check));
8052 /* Return the stored value. */
8053 return TREE_VALUE (value);
8054 }
8055
8056 /* Remember where the template-id starts. */
8057 if (cp_parser_parsing_tentatively (parser)
8058 && !cp_parser_committed_to_tentative_parse (parser))
8059 {
8060 cp_token *next_token = cp_lexer_peek_token (parser->lexer);
8061 start_of_id = cp_lexer_token_difference (parser->lexer,
8062 parser->lexer->first_token,
8063 next_token);
8064 access_check = parser->context->deferred_access_checks;
8065 }
8066 else
8067 start_of_id = -1;
8068
8069 /* Parse the template-name. */
8070 template = cp_parser_template_name (parser, template_keyword_p,
8071 check_dependency_p);
8072 if (template == error_mark_node)
8073 return error_mark_node;
8074
8075 /* Look for the `<' that starts the template-argument-list. */
8076 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8077 return error_mark_node;
8078
8079 /* [temp.names]
8080
8081 When parsing a template-id, the first non-nested `>' is taken as
8082 the end of the template-argument-list rather than a greater-than
8083 operator. */
8084 saved_greater_than_is_operator_p
8085 = parser->greater_than_is_operator_p;
8086 parser->greater_than_is_operator_p = false;
8087 /* Parsing the argument list may modify SCOPE, so we save it
8088 here. */
8089 saved_scope = parser->scope;
8090 saved_qualifying_scope = parser->qualifying_scope;
8091 saved_object_scope = parser->object_scope;
8092 /* Parse the template-argument-list itself. */
8093 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8094 arguments = NULL_TREE;
8095 else
8096 arguments = cp_parser_template_argument_list (parser);
8097 /* Look for the `>' that ends the template-argument-list. */
8098 cp_parser_require (parser, CPP_GREATER, "`>'");
8099 /* The `>' token might be a greater-than operator again now. */
8100 parser->greater_than_is_operator_p
8101 = saved_greater_than_is_operator_p;
8102 /* Restore the SAVED_SCOPE. */
8103 parser->scope = saved_scope;
8104 parser->qualifying_scope = saved_qualifying_scope;
8105 parser->object_scope = saved_object_scope;
8106
8107 /* Build a representation of the specialization. */
8108 if (TREE_CODE (template) == IDENTIFIER_NODE)
8109 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8110 else if (DECL_CLASS_TEMPLATE_P (template)
8111 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8112 template_id
8113 = finish_template_type (template, arguments,
8114 cp_lexer_next_token_is (parser->lexer,
8115 CPP_SCOPE));
8116 else
8117 {
8118 /* If it's not a class-template or a template-template, it should be
8119 a function-template. */
8120 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8121 || TREE_CODE (template) == OVERLOAD
8122 || BASELINK_P (template)),
8123 20010716);
8124
8125 template_id = lookup_template_function (template, arguments);
8126 }
8127
8128 /* If parsing tentatively, replace the sequence of tokens that makes
8129 up the template-id with a CPP_TEMPLATE_ID token. That way,
8130 should we re-parse the token stream, we will not have to repeat
8131 the effort required to do the parse, nor will we issue duplicate
8132 error messages about problems during instantiation of the
8133 template. */
8134 if (start_of_id >= 0)
8135 {
8136 cp_token *token;
8137 tree c;
8138
8139 /* Find the token that corresponds to the start of the
8140 template-id. */
8141 token = cp_lexer_advance_token (parser->lexer,
8142 parser->lexer->first_token,
8143 start_of_id);
8144
8145 /* Remember the access checks associated with this
8146 nested-name-specifier. */
8147 c = parser->context->deferred_access_checks;
8148 if (c == access_check)
8149 access_check = NULL_TREE;
8150 else
8151 {
8152 while (TREE_CHAIN (c) != access_check)
8153 c = TREE_CHAIN (c);
8154 access_check = parser->context->deferred_access_checks;
8155 parser->context->deferred_access_checks = TREE_CHAIN (c);
8156 TREE_CHAIN (c) = NULL_TREE;
8157 }
8158
8159 /* Reset the contents of the START_OF_ID token. */
8160 token->type = CPP_TEMPLATE_ID;
8161 token->value = build_tree_list (access_check, template_id);
8162 token->keyword = RID_MAX;
8163 /* Purge all subsequent tokens. */
8164 cp_lexer_purge_tokens_after (parser->lexer, token);
8165 }
8166
8167 return template_id;
8168}
8169
8170/* Parse a template-name.
8171
8172 template-name:
8173 identifier
8174
8175 The standard should actually say:
8176
8177 template-name:
8178 identifier
8179 operator-function-id
8180 conversion-function-id
8181
8182 A defect report has been filed about this issue.
8183
8184 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8185 `template' keyword, in a construction like:
8186
8187 T::template f<3>()
8188
8189 In that case `f' is taken to be a template-name, even though there
8190 is no way of knowing for sure.
8191
8192 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8193 name refers to a set of overloaded functions, at least one of which
8194 is a template, or an IDENTIFIER_NODE with the name of the template,
8195 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8196 names are looked up inside uninstantiated templates. */
8197
8198static tree
8199cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8200 cp_parser *parser;
8201 bool template_keyword_p;
8202 bool check_dependency_p;
8203{
8204 tree identifier;
8205 tree decl;
8206 tree fns;
8207
8208 /* If the next token is `operator', then we have either an
8209 operator-function-id or a conversion-function-id. */
8210 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8211 {
8212 /* We don't know whether we're looking at an
8213 operator-function-id or a conversion-function-id. */
8214 cp_parser_parse_tentatively (parser);
8215 /* Try an operator-function-id. */
8216 identifier = cp_parser_operator_function_id (parser);
8217 /* If that didn't work, try a conversion-function-id. */
8218 if (!cp_parser_parse_definitely (parser))
8219 identifier = cp_parser_conversion_function_id (parser);
8220 }
8221 /* Look for the identifier. */
8222 else
8223 identifier = cp_parser_identifier (parser);
8224
8225 /* If we didn't find an identifier, we don't have a template-id. */
8226 if (identifier == error_mark_node)
8227 return error_mark_node;
8228
8229 /* If the name immediately followed the `template' keyword, then it
8230 is a template-name. However, if the next token is not `<', then
8231 we do not treat it as a template-name, since it is not being used
8232 as part of a template-id. This enables us to handle constructs
8233 like:
8234
8235 template <typename T> struct S { S(); };
8236 template <typename T> S<T>::S();
8237
8238 correctly. We would treat `S' as a template -- if it were `S<T>'
8239 -- but we do not if there is no `<'. */
8240 if (template_keyword_p && processing_template_decl
8241 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8242 return identifier;
8243
8244 /* Look up the name. */
8245 decl = cp_parser_lookup_name (parser, identifier,
8246 /*check_access=*/true,
8247 /*is_type=*/false,
6fc758aa 8248 /*is_namespace=*/false,
0a3b29ad 8249 check_dependency_p);
8250 decl = maybe_get_template_decl_from_type_decl (decl);
8251
8252 /* If DECL is a template, then the name was a template-name. */
8253 if (TREE_CODE (decl) == TEMPLATE_DECL)
8254 ;
8255 else
8256 {
8257 /* The standard does not explicitly indicate whether a name that
8258 names a set of overloaded declarations, some of which are
8259 templates, is a template-name. However, such a name should
8260 be a template-name; otherwise, there is no way to form a
8261 template-id for the overloaded templates. */
8262 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8263 if (TREE_CODE (fns) == OVERLOAD)
8264 {
8265 tree fn;
8266
8267 for (fn = fns; fn; fn = OVL_NEXT (fn))
8268 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8269 break;
8270 }
8271 else
8272 {
8273 /* Otherwise, the name does not name a template. */
8274 cp_parser_error (parser, "expected template-name");
8275 return error_mark_node;
8276 }
8277 }
8278
8279 /* If DECL is dependent, and refers to a function, then just return
8280 its name; we will look it up again during template instantiation. */
8281 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8282 {
8283 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8284 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8285 return identifier;
8286 }
8287
8288 return decl;
8289}
8290
8291/* Parse a template-argument-list.
8292
8293 template-argument-list:
8294 template-argument
8295 template-argument-list , template-argument
8296
8297 Returns a TREE_LIST representing the arguments, in the order they
8298 appeared. The TREE_VALUE of each node is a representation of the
8299 argument. */
8300
8301static tree
8302cp_parser_template_argument_list (parser)
8303 cp_parser *parser;
8304{
8305 tree arguments = NULL_TREE;
8306
8307 while (true)
8308 {
8309 tree argument;
8310
8311 /* Parse the template-argument. */
8312 argument = cp_parser_template_argument (parser);
8313 /* Add it to the list. */
8314 arguments = tree_cons (NULL_TREE, argument, arguments);
8315 /* If it is not a `,', then there are no more arguments. */
8316 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8317 break;
8318 /* Otherwise, consume the ','. */
8319 cp_lexer_consume_token (parser->lexer);
8320 }
8321
8322 /* We built up the arguments in reverse order. */
8323 return nreverse (arguments);
8324}
8325
8326/* Parse a template-argument.
8327
8328 template-argument:
8329 assignment-expression
8330 type-id
8331 id-expression
8332
8333 The representation is that of an assignment-expression, type-id, or
8334 id-expression -- except that the qualified id-expression is
8335 evaluated, so that the value returned is either a DECL or an
8336 OVERLOAD. */
8337
8338static tree
8339cp_parser_template_argument (parser)
8340 cp_parser *parser;
8341{
8342 tree argument;
8343 bool template_p;
8344
8345 /* There's really no way to know what we're looking at, so we just
8346 try each alternative in order.
8347
8348 [temp.arg]
8349
8350 In a template-argument, an ambiguity between a type-id and an
8351 expression is resolved to a type-id, regardless of the form of
8352 the corresponding template-parameter.
8353
8354 Therefore, we try a type-id first. */
8355 cp_parser_parse_tentatively (parser);
0a3b29ad 8356 argument = cp_parser_type_id (parser);
8357 /* If the next token isn't a `,' or a `>', then this argument wasn't
8358 really finished. */
8359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8360 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8361 cp_parser_error (parser, "expected template-argument");
8362 /* If that worked, we're done. */
8363 if (cp_parser_parse_definitely (parser))
8364 return argument;
8365 /* We're still not sure what the argument will be. */
8366 cp_parser_parse_tentatively (parser);
8367 /* Try a template. */
8368 argument = cp_parser_id_expression (parser,
8369 /*template_keyword_p=*/false,
8370 /*check_dependency_p=*/true,
8371 &template_p);
8372 /* If the next token isn't a `,' or a `>', then this argument wasn't
8373 really finished. */
8374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8375 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8376 cp_parser_error (parser, "expected template-argument");
8377 if (!cp_parser_error_occurred (parser))
8378 {
8379 /* Figure out what is being referred to. */
8380 argument = cp_parser_lookup_name_simple (parser, argument);
8381 if (template_p)
8382 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8383 TREE_OPERAND (argument, 1),
8384 tf_error | tf_parsing);
8385 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8386 cp_parser_error (parser, "expected template-name");
8387 }
8388 if (cp_parser_parse_definitely (parser))
8389 return argument;
8390 /* It must be an assignment-expression. */
8391 return cp_parser_assignment_expression (parser);
8392}
8393
8394/* Parse an explicit-instantiation.
8395
8396 explicit-instantiation:
8397 template declaration
8398
8399 Although the standard says `declaration', what it really means is:
8400
8401 explicit-instantiation:
8402 template decl-specifier-seq [opt] declarator [opt] ;
8403
8404 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8405 supposed to be allowed. A defect report has been filed about this
8406 issue.
8407
8408 GNU Extension:
8409
8410 explicit-instantiation:
8411 storage-class-specifier template
8412 decl-specifier-seq [opt] declarator [opt] ;
8413 function-specifier template
8414 decl-specifier-seq [opt] declarator [opt] ; */
8415
8416static void
8417cp_parser_explicit_instantiation (parser)
8418 cp_parser *parser;
8419{
8420 bool declares_class_or_enum;
8421 tree decl_specifiers;
8422 tree attributes;
8423 tree extension_specifier = NULL_TREE;
8424
8425 /* Look for an (optional) storage-class-specifier or
8426 function-specifier. */
8427 if (cp_parser_allow_gnu_extensions_p (parser))
8428 {
8429 extension_specifier
8430 = cp_parser_storage_class_specifier_opt (parser);
8431 if (!extension_specifier)
8432 extension_specifier = cp_parser_function_specifier_opt (parser);
8433 }
8434
8435 /* Look for the `template' keyword. */
8436 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8437 /* Let the front end know that we are processing an explicit
8438 instantiation. */
8439 begin_explicit_instantiation ();
8440 /* [temp.explicit] says that we are supposed to ignore access
8441 control while processing explicit instantiation directives. */
8442 scope_chain->check_access = 0;
8443 /* Parse a decl-specifier-seq. */
8444 decl_specifiers
8445 = cp_parser_decl_specifier_seq (parser,
8446 CP_PARSER_FLAGS_OPTIONAL,
8447 &attributes,
8448 &declares_class_or_enum);
8449 /* If there was exactly one decl-specifier, and it declared a class,
8450 and there's no declarator, then we have an explicit type
8451 instantiation. */
8452 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8453 {
8454 tree type;
8455
8456 type = check_tag_decl (decl_specifiers);
8457 if (type)
8458 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8459 }
8460 else
8461 {
8462 tree declarator;
8463 tree decl;
8464
8465 /* Parse the declarator. */
8466 declarator
8467 = cp_parser_declarator (parser,
8468 /*abstract_p=*/false,
8469 /*ctor_dtor_or_conv_p=*/NULL);
8470 decl = grokdeclarator (declarator, decl_specifiers,
8471 NORMAL, 0, NULL);
8472 /* Do the explicit instantiation. */
8473 do_decl_instantiation (decl, extension_specifier);
8474 }
8475 /* We're done with the instantiation. */
8476 end_explicit_instantiation ();
8477 /* Trun access control back on. */
8478 scope_chain->check_access = flag_access_control;
8479
8480 /* Look for the trailing `;'. */
8481 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8482}
8483
8484/* Parse an explicit-specialization.
8485
8486 explicit-specialization:
8487 template < > declaration
8488
8489 Although the standard says `declaration', what it really means is:
8490
8491 explicit-specialization:
8492 template <> decl-specifier [opt] init-declarator [opt] ;
8493 template <> function-definition
8494 template <> explicit-specialization
8495 template <> template-declaration */
8496
8497static void
8498cp_parser_explicit_specialization (parser)
8499 cp_parser *parser;
8500{
8501 /* Look for the `template' keyword. */
8502 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8503 /* Look for the `<'. */
8504 cp_parser_require (parser, CPP_LESS, "`<'");
8505 /* Look for the `>'. */
8506 cp_parser_require (parser, CPP_GREATER, "`>'");
8507 /* We have processed another parameter list. */
8508 ++parser->num_template_parameter_lists;
8509 /* Let the front end know that we are beginning a specialization. */
8510 begin_specialization ();
8511
8512 /* If the next keyword is `template', we need to figure out whether
8513 or not we're looking a template-declaration. */
8514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8515 {
8516 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8517 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8518 cp_parser_template_declaration_after_export (parser,
8519 /*member_p=*/false);
8520 else
8521 cp_parser_explicit_specialization (parser);
8522 }
8523 else
8524 /* Parse the dependent declaration. */
8525 cp_parser_single_declaration (parser,
8526 /*member_p=*/false,
8527 /*friend_p=*/NULL);
8528
8529 /* We're done with the specialization. */
8530 end_specialization ();
8531 /* We're done with this parameter list. */
8532 --parser->num_template_parameter_lists;
8533}
8534
8535/* Parse a type-specifier.
8536
8537 type-specifier:
8538 simple-type-specifier
8539 class-specifier
8540 enum-specifier
8541 elaborated-type-specifier
8542 cv-qualifier
8543
8544 GNU Extension:
8545
8546 type-specifier:
8547 __complex__
8548
8549 Returns a representation of the type-specifier. If the
8550 type-specifier is a keyword (like `int' or `const', or
8551 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8552 For a class-specifier, enum-specifier, or elaborated-type-specifier
8553 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8554
8555 If IS_FRIEND is TRUE then this type-specifier is being declared a
8556 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8557 appearing in a decl-specifier-seq.
8558
8559 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8560 class-specifier, enum-specifier, or elaborated-type-specifier, then
8561 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8562 FALSE.
8563
8564 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8565 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8566 is set to FALSE. */
8567
8568static tree
8569cp_parser_type_specifier (parser,
8570 flags,
8571 is_friend,
8572 is_declaration,
8573 declares_class_or_enum,
8574 is_cv_qualifier)
8575 cp_parser *parser;
8576 cp_parser_flags flags;
8577 bool is_friend;
8578 bool is_declaration;
8579 bool *declares_class_or_enum;
8580 bool *is_cv_qualifier;
8581{
8582 tree type_spec = NULL_TREE;
8583 cp_token *token;
8584 enum rid keyword;
8585
8586 /* Assume this type-specifier does not declare a new type. */
8587 if (declares_class_or_enum)
8588 *declares_class_or_enum = false;
8589 /* And that it does not specify a cv-qualifier. */
8590 if (is_cv_qualifier)
8591 *is_cv_qualifier = false;
8592 /* Peek at the next token. */
8593 token = cp_lexer_peek_token (parser->lexer);
8594
8595 /* If we're looking at a keyword, we can use that to guide the
8596 production we choose. */
8597 keyword = token->keyword;
8598 switch (keyword)
8599 {
8600 /* Any of these indicate either a class-specifier, or an
8601 elaborated-type-specifier. */
8602 case RID_CLASS:
8603 case RID_STRUCT:
8604 case RID_UNION:
8605 case RID_ENUM:
8606 /* Parse tentatively so that we can back up if we don't find a
8607 class-specifier or enum-specifier. */
8608 cp_parser_parse_tentatively (parser);
8609 /* Look for the class-specifier or enum-specifier. */
8610 if (keyword == RID_ENUM)
8611 type_spec = cp_parser_enum_specifier (parser);
8612 else
8613 type_spec = cp_parser_class_specifier (parser);
8614
8615 /* If that worked, we're done. */
8616 if (cp_parser_parse_definitely (parser))
8617 {
8618 if (declares_class_or_enum)
8619 *declares_class_or_enum = true;
8620 return type_spec;
8621 }
8622
8623 /* Fall through. */
8624
8625 case RID_TYPENAME:
8626 /* Look for an elaborated-type-specifier. */
8627 type_spec = cp_parser_elaborated_type_specifier (parser,
8628 is_friend,
8629 is_declaration);
8630 /* We're declaring a class or enum -- unless we're using
8631 `typename'. */
8632 if (declares_class_or_enum && keyword != RID_TYPENAME)
8633 *declares_class_or_enum = true;
8634 return type_spec;
8635
8636 case RID_CONST:
8637 case RID_VOLATILE:
8638 case RID_RESTRICT:
8639 type_spec = cp_parser_cv_qualifier_opt (parser);
8640 /* Even though we call a routine that looks for an optional
8641 qualifier, we know that there should be one. */
8642 my_friendly_assert (type_spec != NULL, 20000328);
8643 /* This type-specifier was a cv-qualified. */
8644 if (is_cv_qualifier)
8645 *is_cv_qualifier = true;
8646
8647 return type_spec;
8648
8649 case RID_COMPLEX:
8650 /* The `__complex__' keyword is a GNU extension. */
8651 return cp_lexer_consume_token (parser->lexer)->value;
8652
8653 default:
8654 break;
8655 }
8656
8657 /* If we do not already have a type-specifier, assume we are looking
8658 at a simple-type-specifier. */
8659 type_spec = cp_parser_simple_type_specifier (parser, flags);
8660
8661 /* If we didn't find a type-specifier, and a type-specifier was not
8662 optional in this context, issue an error message. */
8663 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8664 {
8665 cp_parser_error (parser, "expected type specifier");
8666 return error_mark_node;
8667 }
8668
8669 return type_spec;
8670}
8671
8672/* Parse a simple-type-specifier.
8673
8674 simple-type-specifier:
8675 :: [opt] nested-name-specifier [opt] type-name
8676 :: [opt] nested-name-specifier template template-id
8677 char
8678 wchar_t
8679 bool
8680 short
8681 int
8682 long
8683 signed
8684 unsigned
8685 float
8686 double
8687 void
8688
8689 GNU Extension:
8690
8691 simple-type-specifier:
8692 __typeof__ unary-expression
8693 __typeof__ ( type-id )
8694
8695 For the various keywords, the value returned is simply the
8696 TREE_IDENTIFIER representing the keyword. For the first two
8697 productions, the value returned is the indicated TYPE_DECL. */
8698
8699static tree
8700cp_parser_simple_type_specifier (parser, flags)
8701 cp_parser *parser;
8702 cp_parser_flags flags;
8703{
8704 tree type = NULL_TREE;
8705 cp_token *token;
8706
8707 /* Peek at the next token. */
8708 token = cp_lexer_peek_token (parser->lexer);
8709
8710 /* If we're looking at a keyword, things are easy. */
8711 switch (token->keyword)
8712 {
8713 case RID_CHAR:
8714 case RID_WCHAR:
8715 case RID_BOOL:
8716 case RID_SHORT:
8717 case RID_INT:
8718 case RID_LONG:
8719 case RID_SIGNED:
8720 case RID_UNSIGNED:
8721 case RID_FLOAT:
8722 case RID_DOUBLE:
8723 case RID_VOID:
8724 /* Consume the token. */
8725 return cp_lexer_consume_token (parser->lexer)->value;
8726
8727 case RID_TYPEOF:
8728 {
8729 tree operand;
8730
8731 /* Consume the `typeof' token. */
8732 cp_lexer_consume_token (parser->lexer);
8733 /* Parse the operand to `typeof' */
8734 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8735 /* If it is not already a TYPE, take its type. */
8736 if (!TYPE_P (operand))
8737 operand = finish_typeof (operand);
8738
8739 return operand;
8740 }
8741
8742 default:
8743 break;
8744 }
8745
8746 /* The type-specifier must be a user-defined type. */
8747 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8748 {
8749 /* Don't gobble tokens or issue error messages if this is an
8750 optional type-specifier. */
8751 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8752 cp_parser_parse_tentatively (parser);
8753
8754 /* Look for the optional `::' operator. */
8755 cp_parser_global_scope_opt (parser,
8756 /*current_scope_valid_p=*/false);
8757 /* Look for the nested-name specifier. */
8758 cp_parser_nested_name_specifier_opt (parser,
8759 /*typename_keyword_p=*/false,
8760 /*check_dependency_p=*/true,
8761 /*type_p=*/false);
8762 /* If we have seen a nested-name-specifier, and the next token
8763 is `template', then we are using the template-id production. */
8764 if (parser->scope
8765 && cp_parser_optional_template_keyword (parser))
8766 {
8767 /* Look for the template-id. */
8768 type = cp_parser_template_id (parser,
8769 /*template_keyword_p=*/true,
8770 /*check_dependency_p=*/true);
8771 /* If the template-id did not name a type, we are out of
8772 luck. */
8773 if (TREE_CODE (type) != TYPE_DECL)
8774 {
8775 cp_parser_error (parser, "expected template-id for type");
8776 type = NULL_TREE;
8777 }
8778 }
8779 /* Otherwise, look for a type-name. */
8780 else
8781 {
8782 type = cp_parser_type_name (parser);
8783 if (type == error_mark_node)
8784 type = NULL_TREE;
8785 }
8786
8787 /* If it didn't work out, we don't have a TYPE. */
8788 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8789 && !cp_parser_parse_definitely (parser))
8790 type = NULL_TREE;
8791 }
8792
8793 /* If we didn't get a type-name, issue an error message. */
8794 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8795 {
8796 cp_parser_error (parser, "expected type-name");
8797 return error_mark_node;
8798 }
8799
8800 return type;
8801}
8802
8803/* Parse a type-name.
8804
8805 type-name:
8806 class-name
8807 enum-name
8808 typedef-name
8809
8810 enum-name:
8811 identifier
8812
8813 typedef-name:
8814 identifier
8815
8816 Returns a TYPE_DECL for the the type. */
8817
8818static tree
8819cp_parser_type_name (parser)
8820 cp_parser *parser;
8821{
8822 tree type_decl;
8823 tree identifier;
8824
8825 /* We can't know yet whether it is a class-name or not. */
8826 cp_parser_parse_tentatively (parser);
8827 /* Try a class-name. */
8828 type_decl = cp_parser_class_name (parser,
8829 /*typename_keyword_p=*/false,
8830 /*template_keyword_p=*/false,
8831 /*type_p=*/false,
8832 /*check_access_p=*/true,
8833 /*check_dependency_p=*/true,
8834 /*class_head_p=*/false);
8835 /* If it's not a class-name, keep looking. */
8836 if (!cp_parser_parse_definitely (parser))
8837 {
8838 /* It must be a typedef-name or an enum-name. */
8839 identifier = cp_parser_identifier (parser);
8840 if (identifier == error_mark_node)
8841 return error_mark_node;
8842
8843 /* Look up the type-name. */
8844 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8845 /* Issue an error if we did not find a type-name. */
8846 if (TREE_CODE (type_decl) != TYPE_DECL)
8847 {
8848 cp_parser_error (parser, "expected type-name");
8849 type_decl = error_mark_node;
8850 }
8851 /* Remember that the name was used in the definition of the
8852 current class so that we can check later to see if the
8853 meaning would have been different after the class was
8854 entirely defined. */
8855 else if (type_decl != error_mark_node
8856 && !parser->scope)
8857 maybe_note_name_used_in_class (identifier, type_decl);
8858 }
8859
8860 return type_decl;
8861}
8862
8863
8864/* Parse an elaborated-type-specifier. Note that the grammar given
8865 here incorporates the resolution to DR68.
8866
8867 elaborated-type-specifier:
8868 class-key :: [opt] nested-name-specifier [opt] identifier
8869 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8870 enum :: [opt] nested-name-specifier [opt] identifier
8871 typename :: [opt] nested-name-specifier identifier
8872 typename :: [opt] nested-name-specifier template [opt]
8873 template-id
8874
8875 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8876 declared `friend'. If IS_DECLARATION is TRUE, then this
8877 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8878 something is being declared.
8879
8880 Returns the TYPE specified. */
8881
8882static tree
8883cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8884 cp_parser *parser;
8885 bool is_friend;
8886 bool is_declaration;
8887{
8888 enum tag_types tag_type;
8889 tree identifier;
8890 tree type = NULL_TREE;
8891
8892 /* See if we're looking at the `enum' keyword. */
8893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8894 {
8895 /* Consume the `enum' token. */
8896 cp_lexer_consume_token (parser->lexer);
8897 /* Remember that it's an enumeration type. */
8898 tag_type = enum_type;
8899 }
8900 /* Or, it might be `typename'. */
8901 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8902 RID_TYPENAME))
8903 {
8904 /* Consume the `typename' token. */
8905 cp_lexer_consume_token (parser->lexer);
8906 /* Remember that it's a `typename' type. */
8907 tag_type = typename_type;
8908 /* The `typename' keyword is only allowed in templates. */
8909 if (!processing_template_decl)
8910 pedwarn ("using `typename' outside of template");
8911 }
8912 /* Otherwise it must be a class-key. */
8913 else
8914 {
8915 tag_type = cp_parser_class_key (parser);
8916 if (tag_type == none_type)
8917 return error_mark_node;
8918 }
8919
8920 /* Look for the `::' operator. */
8921 cp_parser_global_scope_opt (parser,
8922 /*current_scope_valid_p=*/false);
8923 /* Look for the nested-name-specifier. */
8924 if (tag_type == typename_type)
8925 cp_parser_nested_name_specifier (parser,
8926 /*typename_keyword_p=*/true,
8927 /*check_dependency_p=*/true,
8928 /*type_p=*/true);
8929 else
8930 /* Even though `typename' is not present, the proposed resolution
8931 to Core Issue 180 says that in `class A<T>::B', `B' should be
8932 considered a type-name, even if `A<T>' is dependent. */
8933 cp_parser_nested_name_specifier_opt (parser,
8934 /*typename_keyword_p=*/true,
8935 /*check_dependency_p=*/true,
8936 /*type_p=*/true);
8937 /* For everything but enumeration types, consider a template-id. */
8938 if (tag_type != enum_type)
8939 {
8940 bool template_p = false;
8941 tree decl;
8942
8943 /* Allow the `template' keyword. */
8944 template_p = cp_parser_optional_template_keyword (parser);
8945 /* If we didn't see `template', we don't know if there's a
8946 template-id or not. */
8947 if (!template_p)
8948 cp_parser_parse_tentatively (parser);
8949 /* Parse the template-id. */
8950 decl = cp_parser_template_id (parser, template_p,
8951 /*check_dependency_p=*/true);
8952 /* If we didn't find a template-id, look for an ordinary
8953 identifier. */
8954 if (!template_p && !cp_parser_parse_definitely (parser))
8955 ;
8956 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8957 in effect, then we must assume that, upon instantiation, the
8958 template will correspond to a class. */
8959 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8960 && tag_type == typename_type)
8961 type = make_typename_type (parser->scope, decl,
8962 /*complain=*/1);
8963 else
8964 type = TREE_TYPE (decl);
8965 }
8966
8967 /* For an enumeration type, consider only a plain identifier. */
8968 if (!type)
8969 {
8970 identifier = cp_parser_identifier (parser);
8971
8972 if (identifier == error_mark_node)
8973 return error_mark_node;
8974
8975 /* For a `typename', we needn't call xref_tag. */
8976 if (tag_type == typename_type)
8977 return make_typename_type (parser->scope, identifier,
8978 /*complain=*/1);
8979 /* Look up a qualified name in the usual way. */
8980 if (parser->scope)
8981 {
8982 tree decl;
8983
8984 /* In an elaborated-type-specifier, names are assumed to name
8985 types, so we set IS_TYPE to TRUE when calling
8986 cp_parser_lookup_name. */
8987 decl = cp_parser_lookup_name (parser, identifier,
8988 /*check_access=*/true,
8989 /*is_type=*/true,
6fc758aa 8990 /*is_namespace=*/false,
0a3b29ad 8991 /*check_dependency=*/true);
8992 decl = (cp_parser_maybe_treat_template_as_class
8993 (decl, /*tag_name_p=*/is_friend));
8994
8995 if (TREE_CODE (decl) != TYPE_DECL)
8996 {
8997 error ("expected type-name");
8998 return error_mark_node;
8999 }
9000 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9001 && tag_type != enum_type)
9002 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9003 tag_type == record_type ? "struct" : "class");
9004 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9005 && tag_type == enum_type)
9006 error ("`%T' referred to as enum", TREE_TYPE (decl));
9007
9008 type = TREE_TYPE (decl);
9009 }
9010 else
9011 {
9012 /* An elaborated-type-specifier sometimes introduces a new type and
9013 sometimes names an existing type. Normally, the rule is that it
9014 introduces a new type only if there is not an existing type of
9015 the same name already in scope. For example, given:
9016
9017 struct S {};
9018 void f() { struct S s; }
9019
9020 the `struct S' in the body of `f' is the same `struct S' as in
9021 the global scope; the existing definition is used. However, if
9022 there were no global declaration, this would introduce a new
9023 local class named `S'.
9024
9025 An exception to this rule applies to the following code:
9026
9027 namespace N { struct S; }
9028
9029 Here, the elaborated-type-specifier names a new type
9030 unconditionally; even if there is already an `S' in the
9031 containing scope this declaration names a new type.
9032 This exception only applies if the elaborated-type-specifier
9033 forms the complete declaration:
9034
9035 [class.name]
9036
9037 A declaration consisting solely of `class-key identifier ;' is
9038 either a redeclaration of the name in the current scope or a
9039 forward declaration of the identifier as a class name. It
9040 introduces the name into the current scope.
9041
9042 We are in this situation precisely when the next token is a `;'.
9043
9044 An exception to the exception is that a `friend' declaration does
9045 *not* name a new type; i.e., given:
9046
9047 struct S { friend struct T; };
9048
9049 `T' is not a new type in the scope of `S'.
9050
9051 Also, `new struct S' or `sizeof (struct S)' never results in the
9052 definition of a new type; a new type can only be declared in a
9053 declaration context. */
9054
9055 type = xref_tag (tag_type, identifier,
9056 /*attributes=*/NULL_TREE,
9057 (is_friend
9058 || !is_declaration
9059 || cp_lexer_next_token_is_not (parser->lexer,
9060 CPP_SEMICOLON)));
9061 }
9062 }
9063 if (tag_type != enum_type)
9064 cp_parser_check_class_key (tag_type, type);
9065 return type;
9066}
9067
9068/* Parse an enum-specifier.
9069
9070 enum-specifier:
9071 enum identifier [opt] { enumerator-list [opt] }
9072
9073 Returns an ENUM_TYPE representing the enumeration. */
9074
9075static tree
9076cp_parser_enum_specifier (parser)
9077 cp_parser *parser;
9078{
9079 cp_token *token;
9080 tree identifier = NULL_TREE;
9081 tree type;
9082
9083 /* Look for the `enum' keyword. */
9084 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9085 return error_mark_node;
9086 /* Peek at the next token. */
9087 token = cp_lexer_peek_token (parser->lexer);
9088
9089 /* See if it is an identifier. */
9090 if (token->type == CPP_NAME)
9091 identifier = cp_parser_identifier (parser);
9092
9093 /* Look for the `{'. */
9094 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9095 return error_mark_node;
9096
9097 /* At this point, we're going ahead with the enum-specifier, even
9098 if some other problem occurs. */
9099 cp_parser_commit_to_tentative_parse (parser);
9100
9101 /* Issue an error message if type-definitions are forbidden here. */
9102 cp_parser_check_type_definition (parser);
9103
9104 /* Create the new type. */
9105 type = start_enum (identifier ? identifier : make_anon_name ());
9106
9107 /* Peek at the next token. */
9108 token = cp_lexer_peek_token (parser->lexer);
9109 /* If it's not a `}', then there are some enumerators. */
9110 if (token->type != CPP_CLOSE_BRACE)
9111 cp_parser_enumerator_list (parser, type);
9112 /* Look for the `}'. */
9113 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9114
9115 /* Finish up the enumeration. */
9116 finish_enum (type);
9117
9118 return type;
9119}
9120
9121/* Parse an enumerator-list. The enumerators all have the indicated
9122 TYPE.
9123
9124 enumerator-list:
9125 enumerator-definition
9126 enumerator-list , enumerator-definition */
9127
9128static void
9129cp_parser_enumerator_list (parser, type)
9130 cp_parser *parser;
9131 tree type;
9132{
9133 while (true)
9134 {
9135 cp_token *token;
9136
9137 /* Parse an enumerator-definition. */
9138 cp_parser_enumerator_definition (parser, type);
9139 /* Peek at the next token. */
9140 token = cp_lexer_peek_token (parser->lexer);
9141 /* If it's not a `,', then we've reached the end of the
9142 list. */
9143 if (token->type != CPP_COMMA)
9144 break;
9145 /* Otherwise, consume the `,' and keep going. */
9146 cp_lexer_consume_token (parser->lexer);
9147 /* If the next token is a `}', there is a trailing comma. */
9148 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9149 {
9150 if (pedantic && !in_system_header)
9151 pedwarn ("comma at end of enumerator list");
9152 break;
9153 }
9154 }
9155}
9156
9157/* Parse an enumerator-definition. The enumerator has the indicated
9158 TYPE.
9159
9160 enumerator-definition:
9161 enumerator
9162 enumerator = constant-expression
9163
9164 enumerator:
9165 identifier */
9166
9167static void
9168cp_parser_enumerator_definition (parser, type)
9169 cp_parser *parser;
9170 tree type;
9171{
9172 cp_token *token;
9173 tree identifier;
9174 tree value;
9175
9176 /* Look for the identifier. */
9177 identifier = cp_parser_identifier (parser);
9178 if (identifier == error_mark_node)
9179 return;
9180
9181 /* Peek at the next token. */
9182 token = cp_lexer_peek_token (parser->lexer);
9183 /* If it's an `=', then there's an explicit value. */
9184 if (token->type == CPP_EQ)
9185 {
9186 /* Consume the `=' token. */
9187 cp_lexer_consume_token (parser->lexer);
9188 /* Parse the value. */
9189 value = cp_parser_constant_expression (parser);
9190 }
9191 else
9192 value = NULL_TREE;
9193
9194 /* Create the enumerator. */
9195 build_enumerator (identifier, value, type);
9196}
9197
9198/* Parse a namespace-name.
9199
9200 namespace-name:
9201 original-namespace-name
9202 namespace-alias
9203
9204 Returns the NAMESPACE_DECL for the namespace. */
9205
9206static tree
9207cp_parser_namespace_name (parser)
9208 cp_parser *parser;
9209{
9210 tree identifier;
9211 tree namespace_decl;
9212
9213 /* Get the name of the namespace. */
9214 identifier = cp_parser_identifier (parser);
9215 if (identifier == error_mark_node)
9216 return error_mark_node;
9217
6fc758aa 9218 /* Look up the identifier in the currently active scope. Look only
9219 for namespaces, due to:
9220
9221 [basic.lookup.udir]
9222
9223 When looking up a namespace-name in a using-directive or alias
9224 definition, only namespace names are considered.
9225
9226 And:
9227
9228 [basic.lookup.qual]
9229
9230 During the lookup of a name preceding the :: scope resolution
9231 operator, object, function, and enumerator names are ignored.
9232
9233 (Note that cp_parser_class_or_namespace_name only calls this
9234 function if the token after the name is the scope resolution
9235 operator.) */
9236 namespace_decl = cp_parser_lookup_name (parser, identifier,
9237 /*check_access=*/true,
9238 /*is_type=*/false,
9239 /*is_namespace=*/true,
9240 /*check_dependency=*/true);
0a3b29ad 9241 /* If it's not a namespace, issue an error. */
9242 if (namespace_decl == error_mark_node
9243 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9244 {
9245 cp_parser_error (parser, "expected namespace-name");
9246 namespace_decl = error_mark_node;
9247 }
9248
9249 return namespace_decl;
9250}
9251
9252/* Parse a namespace-definition.
9253
9254 namespace-definition:
9255 named-namespace-definition
9256 unnamed-namespace-definition
9257
9258 named-namespace-definition:
9259 original-namespace-definition
9260 extension-namespace-definition
9261
9262 original-namespace-definition:
9263 namespace identifier { namespace-body }
9264
9265 extension-namespace-definition:
9266 namespace original-namespace-name { namespace-body }
9267
9268 unnamed-namespace-definition:
9269 namespace { namespace-body } */
9270
9271static void
9272cp_parser_namespace_definition (parser)
9273 cp_parser *parser;
9274{
9275 tree identifier;
9276
9277 /* Look for the `namespace' keyword. */
9278 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9279
9280 /* Get the name of the namespace. We do not attempt to distinguish
9281 between an original-namespace-definition and an
9282 extension-namespace-definition at this point. The semantic
9283 analysis routines are responsible for that. */
9284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9285 identifier = cp_parser_identifier (parser);
9286 else
9287 identifier = NULL_TREE;
9288
9289 /* Look for the `{' to start the namespace. */
9290 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9291 /* Start the namespace. */
9292 push_namespace (identifier);
9293 /* Parse the body of the namespace. */
9294 cp_parser_namespace_body (parser);
9295 /* Finish the namespace. */
9296 pop_namespace ();
9297 /* Look for the final `}'. */
9298 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9299}
9300
9301/* Parse a namespace-body.
9302
9303 namespace-body:
9304 declaration-seq [opt] */
9305
9306static void
9307cp_parser_namespace_body (parser)
9308 cp_parser *parser;
9309{
9310 cp_parser_declaration_seq_opt (parser);
9311}
9312
9313/* Parse a namespace-alias-definition.
9314
9315 namespace-alias-definition:
9316 namespace identifier = qualified-namespace-specifier ; */
9317
9318static void
9319cp_parser_namespace_alias_definition (parser)
9320 cp_parser *parser;
9321{
9322 tree identifier;
9323 tree namespace_specifier;
9324
9325 /* Look for the `namespace' keyword. */
9326 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9327 /* Look for the identifier. */
9328 identifier = cp_parser_identifier (parser);
9329 if (identifier == error_mark_node)
9330 return;
9331 /* Look for the `=' token. */
9332 cp_parser_require (parser, CPP_EQ, "`='");
9333 /* Look for the qualified-namespace-specifier. */
9334 namespace_specifier
9335 = cp_parser_qualified_namespace_specifier (parser);
9336 /* Look for the `;' token. */
9337 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9338
9339 /* Register the alias in the symbol table. */
9340 do_namespace_alias (identifier, namespace_specifier);
9341}
9342
9343/* Parse a qualified-namespace-specifier.
9344
9345 qualified-namespace-specifier:
9346 :: [opt] nested-name-specifier [opt] namespace-name
9347
9348 Returns a NAMESPACE_DECL corresponding to the specified
9349 namespace. */
9350
9351static tree
9352cp_parser_qualified_namespace_specifier (parser)
9353 cp_parser *parser;
9354{
9355 /* Look for the optional `::'. */
9356 cp_parser_global_scope_opt (parser,
9357 /*current_scope_valid_p=*/false);
9358
9359 /* Look for the optional nested-name-specifier. */
9360 cp_parser_nested_name_specifier_opt (parser,
9361 /*typename_keyword_p=*/false,
9362 /*check_dependency_p=*/true,
9363 /*type_p=*/false);
9364
9365 return cp_parser_namespace_name (parser);
9366}
9367
9368/* Parse a using-declaration.
9369
9370 using-declaration:
9371 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9372 using :: unqualified-id ; */
9373
9374static void
9375cp_parser_using_declaration (parser)
9376 cp_parser *parser;
9377{
9378 cp_token *token;
9379 bool typename_p = false;
9380 bool global_scope_p;
9381 tree decl;
9382 tree identifier;
9383 tree scope;
9384
9385 /* Look for the `using' keyword. */
9386 cp_parser_require_keyword (parser, RID_USING, "`using'");
9387
9388 /* Peek at the next token. */
9389 token = cp_lexer_peek_token (parser->lexer);
9390 /* See if it's `typename'. */
9391 if (token->keyword == RID_TYPENAME)
9392 {
9393 /* Remember that we've seen it. */
9394 typename_p = true;
9395 /* Consume the `typename' token. */
9396 cp_lexer_consume_token (parser->lexer);
9397 }
9398
9399 /* Look for the optional global scope qualification. */
9400 global_scope_p
9401 = (cp_parser_global_scope_opt (parser,
9402 /*current_scope_valid_p=*/false)
9403 != NULL_TREE);
9404
9405 /* If we saw `typename', or didn't see `::', then there must be a
9406 nested-name-specifier present. */
9407 if (typename_p || !global_scope_p)
9408 cp_parser_nested_name_specifier (parser, typename_p,
9409 /*check_dependency_p=*/true,
9410 /*type_p=*/false);
9411 /* Otherwise, we could be in either of the two productions. In that
9412 case, treat the nested-name-specifier as optional. */
9413 else
9414 cp_parser_nested_name_specifier_opt (parser,
9415 /*typename_keyword_p=*/false,
9416 /*check_dependency_p=*/true,
9417 /*type_p=*/false);
9418
9419 /* Parse the unqualified-id. */
9420 identifier = cp_parser_unqualified_id (parser,
9421 /*template_keyword_p=*/false,
9422 /*check_dependency_p=*/true);
9423
9424 /* The function we call to handle a using-declaration is different
9425 depending on what scope we are in. */
9426 scope = current_scope ();
9427 if (scope && TYPE_P (scope))
9428 {
9429 /* Create the USING_DECL. */
9430 decl = do_class_using_decl (build_nt (SCOPE_REF,
9431 parser->scope,
9432 identifier));
9433 /* Add it to the list of members in this class. */
9434 finish_member_declaration (decl);
9435 }
9436 else
9437 {
9438 decl = cp_parser_lookup_name_simple (parser, identifier);
9439 if (scope)
9440 do_local_using_decl (decl);
9441 else
9442 do_toplevel_using_decl (decl);
9443 }
9444
9445 /* Look for the final `;'. */
9446 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9447}
9448
9449/* Parse a using-directive.
9450
9451 using-directive:
9452 using namespace :: [opt] nested-name-specifier [opt]
9453 namespace-name ; */
9454
9455static void
9456cp_parser_using_directive (parser)
9457 cp_parser *parser;
9458{
9459 tree namespace_decl;
9460
9461 /* Look for the `using' keyword. */
9462 cp_parser_require_keyword (parser, RID_USING, "`using'");
9463 /* And the `namespace' keyword. */
9464 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9465 /* Look for the optional `::' operator. */
9466 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9467 /* And the optional nested-name-sepcifier. */
9468 cp_parser_nested_name_specifier_opt (parser,
9469 /*typename_keyword_p=*/false,
9470 /*check_dependency_p=*/true,
9471 /*type_p=*/false);
9472 /* Get the namespace being used. */
9473 namespace_decl = cp_parser_namespace_name (parser);
9474 /* Update the symbol table. */
9475 do_using_directive (namespace_decl);
9476 /* Look for the final `;'. */
9477 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9478}
9479
9480/* Parse an asm-definition.
9481
9482 asm-definition:
9483 asm ( string-literal ) ;
9484
9485 GNU Extension:
9486
9487 asm-definition:
9488 asm volatile [opt] ( string-literal ) ;
9489 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9490 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9491 : asm-operand-list [opt] ) ;
9492 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9493 : asm-operand-list [opt]
9494 : asm-operand-list [opt] ) ; */
9495
9496static void
9497cp_parser_asm_definition (parser)
9498 cp_parser *parser;
9499{
9500 cp_token *token;
9501 tree string;
9502 tree outputs = NULL_TREE;
9503 tree inputs = NULL_TREE;
9504 tree clobbers = NULL_TREE;
9505 tree asm_stmt;
9506 bool volatile_p = false;
9507 bool extended_p = false;
9508
9509 /* Look for the `asm' keyword. */
9510 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9511 /* See if the next token is `volatile'. */
9512 if (cp_parser_allow_gnu_extensions_p (parser)
9513 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9514 {
9515 /* Remember that we saw the `volatile' keyword. */
9516 volatile_p = true;
9517 /* Consume the token. */
9518 cp_lexer_consume_token (parser->lexer);
9519 }
9520 /* Look for the opening `('. */
9521 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9522 /* Look for the string. */
9523 token = cp_parser_require (parser, CPP_STRING, "asm body");
9524 if (!token)
9525 return;
9526 string = token->value;
9527 /* If we're allowing GNU extensions, check for the extended assembly
9528 syntax. Unfortunately, the `:' tokens need not be separated by
9529 a space in C, and so, for compatibility, we tolerate that here
9530 too. Doing that means that we have to treat the `::' operator as
9531 two `:' tokens. */
9532 if (cp_parser_allow_gnu_extensions_p (parser)
9533 && at_function_scope_p ()
9534 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9535 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9536 {
9537 bool inputs_p = false;
9538 bool clobbers_p = false;
9539
9540 /* The extended syntax was used. */
9541 extended_p = true;
9542
9543 /* Look for outputs. */
9544 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9545 {
9546 /* Consume the `:'. */
9547 cp_lexer_consume_token (parser->lexer);
9548 /* Parse the output-operands. */
9549 if (cp_lexer_next_token_is_not (parser->lexer,
9550 CPP_COLON)
9551 && cp_lexer_next_token_is_not (parser->lexer,
9552 CPP_SCOPE))
9553 outputs = cp_parser_asm_operand_list (parser);
9554 }
9555 /* If the next token is `::', there are no outputs, and the
9556 next token is the beginning of the inputs. */
9557 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9558 {
9559 /* Consume the `::' token. */
9560 cp_lexer_consume_token (parser->lexer);
9561 /* The inputs are coming next. */
9562 inputs_p = true;
9563 }
9564
9565 /* Look for inputs. */
9566 if (inputs_p
9567 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9568 {
9569 if (!inputs_p)
9570 /* Consume the `:'. */
9571 cp_lexer_consume_token (parser->lexer);
9572 /* Parse the output-operands. */
9573 if (cp_lexer_next_token_is_not (parser->lexer,
9574 CPP_COLON)
9575 && cp_lexer_next_token_is_not (parser->lexer,
9576 CPP_SCOPE))
9577 inputs = cp_parser_asm_operand_list (parser);
9578 }
9579 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9580 /* The clobbers are coming next. */
9581 clobbers_p = true;
9582
9583 /* Look for clobbers. */
9584 if (clobbers_p
9585 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9586 {
9587 if (!clobbers_p)
9588 /* Consume the `:'. */
9589 cp_lexer_consume_token (parser->lexer);
9590 /* Parse the clobbers. */
9591 clobbers = cp_parser_asm_clobber_list (parser);
9592 }
9593 }
9594 /* Look for the closing `)'. */
9595 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9596 cp_parser_skip_to_closing_parenthesis (parser);
9597 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9598
9599 /* Create the ASM_STMT. */
9600 if (at_function_scope_p ())
9601 {
9602 asm_stmt =
9603 finish_asm_stmt (volatile_p
9604 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9605 string, outputs, inputs, clobbers);
9606 /* If the extended syntax was not used, mark the ASM_STMT. */
9607 if (!extended_p)
9608 ASM_INPUT_P (asm_stmt) = 1;
9609 }
9610 else
9611 assemble_asm (string);
9612}
9613
9614/* Declarators [gram.dcl.decl] */
9615
9616/* Parse an init-declarator.
9617
9618 init-declarator:
9619 declarator initializer [opt]
9620
9621 GNU Extension:
9622
9623 init-declarator:
9624 declarator asm-specification [opt] attributes [opt] initializer [opt]
9625
9626 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9627 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9628 represent deferred access checks from the decl-specifier-seq. If
9629 MEMBER_P is TRUE, then this declarator appears in a class scope.
9630 The new DECL created by this declarator is returned.
9631
9632 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9633 for a function-definition here as well. If the declarator is a
9634 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9635 be TRUE upon return. By that point, the function-definition will
9636 have been completely parsed.
9637
9638 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9639 is FALSE. */
9640
9641static tree
9642cp_parser_init_declarator (parser,
9643 decl_specifiers,
9644 prefix_attributes,
9645 access_checks,
9646 function_definition_allowed_p,
9647 member_p,
9648 function_definition_p)
9649 cp_parser *parser;
9650 tree decl_specifiers;
9651 tree prefix_attributes;
9652 tree access_checks;
9653 bool function_definition_allowed_p;
9654 bool member_p;
9655 bool *function_definition_p;
9656{
9657 cp_token *token;
9658 tree declarator;
9659 tree attributes;
9660 tree asm_specification;
9661 tree initializer;
9662 tree decl = NULL_TREE;
9663 tree scope;
9664 tree declarator_access_checks;
9665 bool is_initialized;
9666 bool is_parenthesized_init;
9667 bool ctor_dtor_or_conv_p;
9668 bool friend_p;
9669
9670 /* Assume that this is not the declarator for a function
9671 definition. */
9672 if (function_definition_p)
9673 *function_definition_p = false;
9674
9675 /* Defer access checks while parsing the declarator; we cannot know
9676 what names are accessible until we know what is being
9677 declared. */
9678 cp_parser_start_deferring_access_checks (parser);
9679 /* Parse the declarator. */
9680 declarator
9681 = cp_parser_declarator (parser,
9682 /*abstract_p=*/false,
9683 &ctor_dtor_or_conv_p);
9684 /* Gather up the deferred checks. */
9685 declarator_access_checks
9686 = cp_parser_stop_deferring_access_checks (parser);
9687
9688 /* If the DECLARATOR was erroneous, there's no need to go
9689 further. */
9690 if (declarator == error_mark_node)
9691 return error_mark_node;
9692
9693 /* Figure out what scope the entity declared by the DECLARATOR is
9694 located in. `grokdeclarator' sometimes changes the scope, so
9695 we compute it now. */
9696 scope = get_scope_of_declarator (declarator);
9697
9698 /* If we're allowing GNU extensions, look for an asm-specification
9699 and attributes. */
9700 if (cp_parser_allow_gnu_extensions_p (parser))
9701 {
9702 /* Look for an asm-specification. */
9703 asm_specification = cp_parser_asm_specification_opt (parser);
9704 /* And attributes. */
9705 attributes = cp_parser_attributes_opt (parser);
9706 }
9707 else
9708 {
9709 asm_specification = NULL_TREE;
9710 attributes = NULL_TREE;
9711 }
9712
9713 /* Peek at the next token. */
9714 token = cp_lexer_peek_token (parser->lexer);
9715 /* Check to see if the token indicates the start of a
9716 function-definition. */
9717 if (cp_parser_token_starts_function_definition_p (token))
9718 {
9719 if (!function_definition_allowed_p)
9720 {
9721 /* If a function-definition should not appear here, issue an
9722 error message. */
9723 cp_parser_error (parser,
9724 "a function-definition is not allowed here");
9725 return error_mark_node;
9726 }
9727 else
9728 {
9729 tree *ac;
9730
9731 /* Neither attributes nor an asm-specification are allowed
9732 on a function-definition. */
9733 if (asm_specification)
9734 error ("an asm-specification is not allowed on a function-definition");
9735 if (attributes)
9736 error ("attributes are not allowed on a function-definition");
9737 /* This is a function-definition. */
9738 *function_definition_p = true;
9739
9740 /* Thread the access checks together. */
9741 ac = &access_checks;
9742 while (*ac)
9743 ac = &TREE_CHAIN (*ac);
9744 *ac = declarator_access_checks;
9745
9746 /* Parse the function definition. */
9747 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9748 (parser, decl_specifiers, prefix_attributes, declarator,
9749 access_checks));
9750
9751 /* Pull the access-checks apart again. */
9752 *ac = NULL_TREE;
9753
9754 return decl;
9755 }
9756 }
9757
9758 /* [dcl.dcl]
9759
9760 Only in function declarations for constructors, destructors, and
9761 type conversions can the decl-specifier-seq be omitted.
9762
9763 We explicitly postpone this check past the point where we handle
9764 function-definitions because we tolerate function-definitions
9765 that are missing their return types in some modes. */
9766 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9767 {
9768 cp_parser_error (parser,
9769 "expected constructor, destructor, or type conversion");
9770 return error_mark_node;
9771 }
9772
9773 /* An `=' or an `(' indicates an initializer. */
9774 is_initialized = (token->type == CPP_EQ
9775 || token->type == CPP_OPEN_PAREN);
9776 /* If the init-declarator isn't initialized and isn't followed by a
9777 `,' or `;', it's not a valid init-declarator. */
9778 if (!is_initialized
9779 && token->type != CPP_COMMA
9780 && token->type != CPP_SEMICOLON)
9781 {
9782 cp_parser_error (parser, "expected init-declarator");
9783 return error_mark_node;
9784 }
9785
9786 /* Because start_decl has side-effects, we should only call it if we
9787 know we're going ahead. By this point, we know that we cannot
9788 possibly be looking at any other construct. */
9789 cp_parser_commit_to_tentative_parse (parser);
9790
9791 /* Check to see whether or not this declaration is a friend. */
9792 friend_p = cp_parser_friend_p (decl_specifiers);
9793
9794 /* Check that the number of template-parameter-lists is OK. */
9795 if (!cp_parser_check_declarator_template_parameters (parser,
9796 declarator))
9797 return error_mark_node;
9798
9799 /* Enter the newly declared entry in the symbol table. If we're
9800 processing a declaration in a class-specifier, we wait until
9801 after processing the initializer. */
9802 if (!member_p)
9803 {
9804 if (parser->in_unbraced_linkage_specification_p)
9805 {
9806 decl_specifiers = tree_cons (error_mark_node,
9807 get_identifier ("extern"),
9808 decl_specifiers);
9809 have_extern_spec = false;
9810 }
9811 decl = start_decl (declarator,
9812 decl_specifiers,
9813 is_initialized,
9814 attributes,
9815 prefix_attributes);
9816 }
9817
9818 /* Enter the SCOPE. That way unqualified names appearing in the
9819 initializer will be looked up in SCOPE. */
9820 if (scope)
9821 push_scope (scope);
9822
9823 /* Perform deferred access control checks, now that we know in which
9824 SCOPE the declared entity resides. */
9825 if (!member_p && decl)
9826 {
9827 tree saved_current_function_decl = NULL_TREE;
9828
9829 /* If the entity being declared is a function, pretend that we
9830 are in its scope. If it is a `friend', it may have access to
9831 things that would not otherwise be accessible. */
9832 if (TREE_CODE (decl) == FUNCTION_DECL)
9833 {
9834 saved_current_function_decl = current_function_decl;
9835 current_function_decl = decl;
9836 }
9837
9838 /* Perform the access control checks for the decl-specifiers. */
9839 cp_parser_perform_deferred_access_checks (access_checks);
9840 /* And for the declarator. */
9841 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9842
9843 /* Restore the saved value. */
9844 if (TREE_CODE (decl) == FUNCTION_DECL)
9845 current_function_decl = saved_current_function_decl;
9846 }
9847
9848 /* Parse the initializer. */
9849 if (is_initialized)
9850 initializer = cp_parser_initializer (parser,
9851 &is_parenthesized_init);
9852 else
9853 {
9854 initializer = NULL_TREE;
9855 is_parenthesized_init = false;
9856 }
9857
9858 /* The old parser allows attributes to appear after a parenthesized
9859 initializer. Mark Mitchell proposed removing this functionality
9860 on the GCC mailing lists on 2002-08-13. This parser accepts the
9861 attributes -- but ignores them. */
9862 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9863 if (cp_parser_attributes_opt (parser))
9864 warning ("attributes after parenthesized initializer ignored");
9865
9866 /* Leave the SCOPE, now that we have processed the initializer. It
9867 is important to do this before calling cp_finish_decl because it
9868 makes decisions about whether to create DECL_STMTs or not based
9869 on the current scope. */
9870 if (scope)
9871 pop_scope (scope);
9872
9873 /* For an in-class declaration, use `grokfield' to create the
9874 declaration. */
9875 if (member_p)
9876 decl = grokfield (declarator, decl_specifiers,
9877 initializer, /*asmspec=*/NULL_TREE,
9878 /*attributes=*/NULL_TREE);
9879
9880 /* Finish processing the declaration. But, skip friend
9881 declarations. */
9882 if (!friend_p && decl)
9883 cp_finish_decl (decl,
9884 initializer,
9885 asm_specification,
9886 /* If the initializer is in parentheses, then this is
9887 a direct-initialization, which means that an
9888 `explicit' constructor is OK. Otherwise, an
9889 `explicit' constructor cannot be used. */
9890 ((is_parenthesized_init || !is_initialized)
9891 ? 0 : LOOKUP_ONLYCONVERTING));
9892
9893 return decl;
9894}
9895
9896/* Parse a declarator.
9897
9898 declarator:
9899 direct-declarator
9900 ptr-operator declarator
9901
9902 abstract-declarator:
9903 ptr-operator abstract-declarator [opt]
9904 direct-abstract-declarator
9905
9906 GNU Extensions:
9907
9908 declarator:
9909 attributes [opt] direct-declarator
9910 attributes [opt] ptr-operator declarator
9911
9912 abstract-declarator:
9913 attributes [opt] ptr-operator abstract-declarator [opt]
9914 attributes [opt] direct-abstract-declarator
9915
9916 Returns a representation of the declarator. If the declarator has
9917 the form `* declarator', then an INDIRECT_REF is returned, whose
9918 only operand is the sub-declarator. Analagously, `& declarator' is
9919 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9920 used. The first operand is the TYPE for `X'. The second operand
9921 is an INDIRECT_REF whose operand is the sub-declarator.
9922
9923 Otherwise, the reprsentation is as for a direct-declarator.
9924
9925 (It would be better to define a structure type to represent
9926 declarators, rather than abusing `tree' nodes to represent
9927 declarators. That would be much clearer and save some memory.
9928 There is no reason for declarators to be garbage-collected, for
9929 example; they are created during parser and no longer needed after
9930 `grokdeclarator' has been called.)
9931
9932 For a ptr-operator that has the optional cv-qualifier-seq,
9933 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9934 node.
9935
9936 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9937 true if this declarator represents a constructor, destructor, or
9938 type conversion operator. Otherwise, it is set to false.
9939
9940 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9941 a decl-specifier-seq unless it declares a constructor, destructor,
9942 or conversion. It might seem that we could check this condition in
9943 semantic analysis, rather than parsing, but that makes it difficult
9944 to handle something like `f()'. We want to notice that there are
9945 no decl-specifiers, and therefore realize that this is an
9946 expression, not a declaration.) */
9947
9948static tree
9949cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
9950 cp_parser *parser;
9951 bool abstract_p;
9952 bool *ctor_dtor_or_conv_p;
9953{
9954 cp_token *token;
9955 tree declarator;
9956 enum tree_code code;
9957 tree cv_qualifier_seq;
9958 tree class_type;
9959 tree attributes = NULL_TREE;
9960
9961 /* Assume this is not a constructor, destructor, or type-conversion
9962 operator. */
9963 if (ctor_dtor_or_conv_p)
9964 *ctor_dtor_or_conv_p = false;
9965
9966 if (cp_parser_allow_gnu_extensions_p (parser))
9967 attributes = cp_parser_attributes_opt (parser);
9968
9969 /* Peek at the next token. */
9970 token = cp_lexer_peek_token (parser->lexer);
9971
9972 /* Check for the ptr-operator production. */
9973 cp_parser_parse_tentatively (parser);
9974 /* Parse the ptr-operator. */
9975 code = cp_parser_ptr_operator (parser,
9976 &class_type,
9977 &cv_qualifier_seq);
9978 /* If that worked, then we have a ptr-operator. */
9979 if (cp_parser_parse_definitely (parser))
9980 {
9981 /* The dependent declarator is optional if we are parsing an
9982 abstract-declarator. */
9983 if (abstract_p)
9984 cp_parser_parse_tentatively (parser);
9985
9986 /* Parse the dependent declarator. */
9987 declarator = cp_parser_declarator (parser, abstract_p,
9988 /*ctor_dtor_or_conv_p=*/NULL);
9989
9990 /* If we are parsing an abstract-declarator, we must handle the
9991 case where the dependent declarator is absent. */
9992 if (abstract_p && !cp_parser_parse_definitely (parser))
9993 declarator = NULL_TREE;
9994
9995 /* Build the representation of the ptr-operator. */
9996 if (code == INDIRECT_REF)
9997 declarator = make_pointer_declarator (cv_qualifier_seq,
9998 declarator);
9999 else
10000 declarator = make_reference_declarator (cv_qualifier_seq,
10001 declarator);
10002 /* Handle the pointer-to-member case. */
10003 if (class_type)
10004 declarator = build_nt (SCOPE_REF, class_type, declarator);
10005 }
10006 /* Everything else is a direct-declarator. */
10007 else
10008 declarator = cp_parser_direct_declarator (parser,
10009 abstract_p,
10010 ctor_dtor_or_conv_p);
10011
10012 if (attributes && declarator != error_mark_node)
10013 declarator = tree_cons (attributes, declarator, NULL_TREE);
10014
10015 return declarator;
10016}
10017
10018/* Parse a direct-declarator or direct-abstract-declarator.
10019
10020 direct-declarator:
10021 declarator-id
10022 direct-declarator ( parameter-declaration-clause )
10023 cv-qualifier-seq [opt]
10024 exception-specification [opt]
10025 direct-declarator [ constant-expression [opt] ]
10026 ( declarator )
10027
10028 direct-abstract-declarator:
10029 direct-abstract-declarator [opt]
10030 ( parameter-declaration-clause )
10031 cv-qualifier-seq [opt]
10032 exception-specification [opt]
10033 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10034 ( abstract-declarator )
10035
10036 Returns a representation of the declarator. ABSTRACT_P is TRUE if
10037 we are parsing a direct-abstract-declarator; FALSE if we are
10038 parsing a direct-declarator. CTOR_DTOR_OR_CONV_P is as for
10039 cp_parser_declarator.
10040
10041 For the declarator-id production, the representation is as for an
10042 id-expression, except that a qualified name is represented as a
10043 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10044 see the documentation of the FUNCTION_DECLARATOR_* macros for
10045 information about how to find the various declarator components.
10046 An array-declarator is represented as an ARRAY_REF. The
10047 direct-declarator is the first operand; the constant-expression
10048 indicating the size of the array is the second operand. */
10049
10050static tree
10051cp_parser_direct_declarator (parser, abstract_p, ctor_dtor_or_conv_p)
10052 cp_parser *parser;
10053 bool abstract_p;
10054 bool *ctor_dtor_or_conv_p;
10055{
10056 cp_token *token;
10057 tree declarator;
10058 tree scope = NULL_TREE;
10059 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10060 bool saved_in_declarator_p = parser->in_declarator_p;
10061
10062 /* Peek at the next token. */
10063 token = cp_lexer_peek_token (parser->lexer);
10064 /* Find the initial direct-declarator. It might be a parenthesized
10065 declarator. */
10066 if (token->type == CPP_OPEN_PAREN)
10067 {
efd34bd3 10068 bool error_p;
10069
0a3b29ad 10070 /* For an abstract declarator we do not know whether we are
10071 looking at the beginning of a parameter-declaration-clause,
10072 or at a parenthesized abstract declarator. For example, if
10073 we see `(int)', we are looking at a
10074 parameter-declaration-clause, and the
10075 direct-abstract-declarator has been omitted. If, on the
10076 other hand we are looking at `((*))' then we are looking at a
10077 parenthesized abstract-declarator. There is no easy way to
10078 tell which situation we are in. */
10079 if (abstract_p)
10080 cp_parser_parse_tentatively (parser);
10081
10082 /* Consume the `('. */
10083 cp_lexer_consume_token (parser->lexer);
10084 /* Parse the nested declarator. */
10085 declarator
10086 = cp_parser_declarator (parser, abstract_p, ctor_dtor_or_conv_p);
10087 /* Expect a `)'. */
efd34bd3 10088 error_p = !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
0a3b29ad 10089
10090 /* If parsing a parenthesized abstract declarator didn't work,
10091 try a parameter-declaration-clause. */
10092 if (abstract_p && !cp_parser_parse_definitely (parser))
10093 declarator = NULL_TREE;
10094 /* If we were not parsing an abstract declarator, but failed to
10095 find a satisfactory nested declarator, then an error has
10096 occurred. */
efd34bd3 10097 else if (!abstract_p
10098 && (declarator == error_mark_node || error_p))
0a3b29ad 10099 return error_mark_node;
10100 /* Default args cannot appear in an abstract decl. */
10101 parser->default_arg_ok_p = false;
10102 }
10103 /* Otherwise, for a non-abstract declarator, there should be a
10104 declarator-id. */
10105 else if (!abstract_p)
10106 {
10107 declarator = cp_parser_declarator_id (parser);
10108
10109 if (TREE_CODE (declarator) == SCOPE_REF)
10110 {
10111 scope = TREE_OPERAND (declarator, 0);
10112
10113 /* In the declaration of a member of a template class
10114 outside of the class itself, the SCOPE will sometimes be
10115 a TYPENAME_TYPE. For example, given:
10116
10117 template <typename T>
10118 int S<T>::R::i = 3;
10119
10120 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In this
10121 context, we must resolve S<T>::R to an ordinary type,
10122 rather than a typename type.
10123
10124 The reason we normally avoid resolving TYPENAME_TYPEs is
10125 that a specialization of `S' might render `S<T>::R' not a
10126 type. However, if `S' is specialized, then this `i' will
10127 not be used, so there is no harm in resolving the types
10128 here. */
10129 if (TREE_CODE (scope) == TYPENAME_TYPE)
10130 {
10131 /* Resolve the TYPENAME_TYPE. */
10132 scope = cp_parser_resolve_typename_type (parser, scope);
10133 /* If that failed, the declarator is invalid. */
10134 if (scope == error_mark_node)
10135 return error_mark_node;
10136 /* Build a new DECLARATOR. */
10137 declarator = build_nt (SCOPE_REF,
10138 scope,
10139 TREE_OPERAND (declarator, 1));
10140 }
10141 }
10142 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10143 /* Default args can only appear for a function decl. */
10144 parser->default_arg_ok_p = false;
10145
10146 /* Check to see whether the declarator-id names a constructor,
10147 destructor, or conversion. */
10148 if (ctor_dtor_or_conv_p
10149 && ((TREE_CODE (declarator) == SCOPE_REF
10150 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10151 || (TREE_CODE (declarator) != SCOPE_REF
10152 && at_class_scope_p ())))
10153 {
10154 tree unqualified_name;
10155 tree class_type;
10156
10157 /* Get the unqualified part of the name. */
10158 if (TREE_CODE (declarator) == SCOPE_REF)
10159 {
10160 class_type = TREE_OPERAND (declarator, 0);
10161 unqualified_name = TREE_OPERAND (declarator, 1);
10162 }
10163 else
10164 {
10165 class_type = current_class_type;
10166 unqualified_name = declarator;
10167 }
10168
10169 /* See if it names ctor, dtor or conv. */
10170 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10171 || IDENTIFIER_TYPENAME_P (unqualified_name)
10172 || constructor_name_p (unqualified_name, class_type))
10173 {
10174 *ctor_dtor_or_conv_p = true;
10175 /* We would have cleared the default arg flag above, but
10176 they are ok. */
10177 parser->default_arg_ok_p = saved_default_arg_ok_p;
10178 }
10179 }
10180 }
10181 /* But for an abstract declarator, the initial direct-declarator can
10182 be omitted. */
10183 else
10184 {
10185 declarator = NULL_TREE;
10186 parser->default_arg_ok_p = false;
10187 }
10188
10189 scope = get_scope_of_declarator (declarator);
10190 if (scope)
10191 /* Any names that appear after the declarator-id for a member
10192 are looked up in the containing scope. */
10193 push_scope (scope);
10194 else
10195 scope = NULL_TREE;
10196 parser->in_declarator_p = true;
10197
10198 /* Now, parse function-declarators and array-declarators until there
10199 are no more. */
10200 while (true)
10201 {
10202 /* Peek at the next token. */
10203 token = cp_lexer_peek_token (parser->lexer);
10204 /* If it's a `[', we're looking at an array-declarator. */
10205 if (token->type == CPP_OPEN_SQUARE)
10206 {
10207 tree bounds;
10208
10209 /* Consume the `['. */
10210 cp_lexer_consume_token (parser->lexer);
10211 /* Peek at the next token. */
10212 token = cp_lexer_peek_token (parser->lexer);
10213 /* If the next token is `]', then there is no
10214 constant-expression. */
10215 if (token->type != CPP_CLOSE_SQUARE)
10216 bounds = cp_parser_constant_expression (parser);
10217 else
10218 bounds = NULL_TREE;
10219 /* Look for the closing `]'. */
10220 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
10221
10222 declarator = build_nt (ARRAY_REF, declarator, bounds);
10223 }
10224 /* If it's a `(', we're looking at a function-declarator. */
10225 else if (token->type == CPP_OPEN_PAREN)
10226 {
10227 /* A function-declarator. Or maybe not. Consider, for
10228 example:
10229
10230 int i (int);
10231 int i (3);
10232
10233 The first is the declaration of a function while the
10234 second is a the definition of a variable, including its
10235 initializer.
10236
10237 Having seen only the parenthesis, we cannot know which of
10238 these two alternatives should be selected. Even more
10239 complex are examples like:
10240
10241 int i (int (a));
10242 int i (int (3));
10243
10244 The former is a function-declaration; the latter is a
10245 variable initialization.
10246
10247 First, we attempt to parse a parameter-declaration
10248 clause. If this works, then we continue; otherwise, we
10249 replace the tokens consumed in the process and continue. */
10250 tree params;
10251
10252 /* We are now parsing tentatively. */
10253 cp_parser_parse_tentatively (parser);
10254
10255 /* Consume the `('. */
10256 cp_lexer_consume_token (parser->lexer);
10257 /* Parse the parameter-declaration-clause. */
10258 params = cp_parser_parameter_declaration_clause (parser);
10259
10260 /* If all went well, parse the cv-qualifier-seq and the
10261 exception-specification. */
10262 if (cp_parser_parse_definitely (parser))
10263 {
10264 tree cv_qualifiers;
10265 tree exception_specification;
10266
10267 /* Consume the `)'. */
10268 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10269
10270 /* Parse the cv-qualifier-seq. */
10271 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10272 /* And the exception-specification. */
10273 exception_specification
10274 = cp_parser_exception_specification_opt (parser);
10275
10276 /* Create the function-declarator. */
10277 declarator = make_call_declarator (declarator,
10278 params,
10279 cv_qualifiers,
10280 exception_specification);
10281 }
10282 /* Otherwise, we must be done with the declarator. */
10283 else
10284 break;
10285 }
10286 /* Otherwise, we're done with the declarator. */
10287 else
10288 break;
10289 /* Any subsequent parameter lists are to do with return type, so
10290 are not those of the declared function. */
10291 parser->default_arg_ok_p = false;
10292 }
10293
10294 /* For an abstract declarator, we might wind up with nothing at this
10295 point. That's an error; the declarator is not optional. */
10296 if (!declarator)
10297 cp_parser_error (parser, "expected declarator");
10298
10299 /* If we entered a scope, we must exit it now. */
10300 if (scope)
10301 pop_scope (scope);
10302
10303 parser->default_arg_ok_p = saved_default_arg_ok_p;
10304 parser->in_declarator_p = saved_in_declarator_p;
10305
10306 return declarator;
10307}
10308
10309/* Parse a ptr-operator.
10310
10311 ptr-operator:
10312 * cv-qualifier-seq [opt]
10313 &
10314 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10315
10316 GNU Extension:
10317
10318 ptr-operator:
10319 & cv-qualifier-seq [opt]
10320
10321 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10322 used. Returns ADDR_EXPR if a reference was used. In the
10323 case of a pointer-to-member, *TYPE is filled in with the
10324 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10325 with the cv-qualifier-seq, or NULL_TREE, if there are no
10326 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10327
10328static enum tree_code
10329cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10330 cp_parser *parser;
10331 tree *type;
10332 tree *cv_qualifier_seq;
10333{
10334 enum tree_code code = ERROR_MARK;
10335 cp_token *token;
10336
10337 /* Assume that it's not a pointer-to-member. */
10338 *type = NULL_TREE;
10339 /* And that there are no cv-qualifiers. */
10340 *cv_qualifier_seq = NULL_TREE;
10341
10342 /* Peek at the next token. */
10343 token = cp_lexer_peek_token (parser->lexer);
10344 /* If it's a `*' or `&' we have a pointer or reference. */
10345 if (token->type == CPP_MULT || token->type == CPP_AND)
10346 {
10347 /* Remember which ptr-operator we were processing. */
10348 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10349
10350 /* Consume the `*' or `&'. */
10351 cp_lexer_consume_token (parser->lexer);
10352
10353 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10354 `&', if we are allowing GNU extensions. (The only qualifier
10355 that can legally appear after `&' is `restrict', but that is
10356 enforced during semantic analysis. */
10357 if (code == INDIRECT_REF
10358 || cp_parser_allow_gnu_extensions_p (parser))
10359 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10360 }
10361 else
10362 {
10363 /* Try the pointer-to-member case. */
10364 cp_parser_parse_tentatively (parser);
10365 /* Look for the optional `::' operator. */
10366 cp_parser_global_scope_opt (parser,
10367 /*current_scope_valid_p=*/false);
10368 /* Look for the nested-name specifier. */
10369 cp_parser_nested_name_specifier (parser,
10370 /*typename_keyword_p=*/false,
10371 /*check_dependency_p=*/true,
10372 /*type_p=*/false);
10373 /* If we found it, and the next token is a `*', then we are
10374 indeed looking at a pointer-to-member operator. */
10375 if (!cp_parser_error_occurred (parser)
10376 && cp_parser_require (parser, CPP_MULT, "`*'"))
10377 {
10378 /* The type of which the member is a member is given by the
10379 current SCOPE. */
10380 *type = parser->scope;
10381 /* The next name will not be qualified. */
10382 parser->scope = NULL_TREE;
10383 parser->qualifying_scope = NULL_TREE;
10384 parser->object_scope = NULL_TREE;
10385 /* Indicate that the `*' operator was used. */
10386 code = INDIRECT_REF;
10387 /* Look for the optional cv-qualifier-seq. */
10388 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10389 }
10390 /* If that didn't work we don't have a ptr-operator. */
10391 if (!cp_parser_parse_definitely (parser))
10392 cp_parser_error (parser, "expected ptr-operator");
10393 }
10394
10395 return code;
10396}
10397
10398/* Parse an (optional) cv-qualifier-seq.
10399
10400 cv-qualifier-seq:
10401 cv-qualifier cv-qualifier-seq [opt]
10402
10403 Returns a TREE_LIST. The TREE_VALUE of each node is the
10404 representation of a cv-qualifier. */
10405
10406static tree
10407cp_parser_cv_qualifier_seq_opt (parser)
10408 cp_parser *parser;
10409{
10410 tree cv_qualifiers = NULL_TREE;
10411
10412 while (true)
10413 {
10414 tree cv_qualifier;
10415
10416 /* Look for the next cv-qualifier. */
10417 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10418 /* If we didn't find one, we're done. */
10419 if (!cv_qualifier)
10420 break;
10421
10422 /* Add this cv-qualifier to the list. */
10423 cv_qualifiers
10424 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10425 }
10426
10427 /* We built up the list in reverse order. */
10428 return nreverse (cv_qualifiers);
10429}
10430
10431/* Parse an (optional) cv-qualifier.
10432
10433 cv-qualifier:
10434 const
10435 volatile
10436
10437 GNU Extension:
10438
10439 cv-qualifier:
10440 __restrict__ */
10441
10442static tree
10443cp_parser_cv_qualifier_opt (parser)
10444 cp_parser *parser;
10445{
10446 cp_token *token;
10447 tree cv_qualifier = NULL_TREE;
10448
10449 /* Peek at the next token. */
10450 token = cp_lexer_peek_token (parser->lexer);
10451 /* See if it's a cv-qualifier. */
10452 switch (token->keyword)
10453 {
10454 case RID_CONST:
10455 case RID_VOLATILE:
10456 case RID_RESTRICT:
10457 /* Save the value of the token. */
10458 cv_qualifier = token->value;
10459 /* Consume the token. */
10460 cp_lexer_consume_token (parser->lexer);
10461 break;
10462
10463 default:
10464 break;
10465 }
10466
10467 return cv_qualifier;
10468}
10469
10470/* Parse a declarator-id.
10471
10472 declarator-id:
10473 id-expression
10474 :: [opt] nested-name-specifier [opt] type-name
10475
10476 In the `id-expression' case, the value returned is as for
10477 cp_parser_id_expression if the id-expression was an unqualified-id.
10478 If the id-expression was a qualified-id, then a SCOPE_REF is
10479 returned. The first operand is the scope (either a NAMESPACE_DECL
10480 or TREE_TYPE), but the second is still just a representation of an
10481 unqualified-id. */
10482
10483static tree
10484cp_parser_declarator_id (parser)
10485 cp_parser *parser;
10486{
10487 tree id_expression;
10488
10489 /* The expression must be an id-expression. Assume that qualified
10490 names are the names of types so that:
10491
10492 template <class T>
10493 int S<T>::R::i = 3;
10494
10495 will work; we must treat `S<T>::R' as the name of a type.
10496 Similarly, assume that qualified names are templates, where
10497 required, so that:
10498
10499 template <class T>
10500 int S<T>::R<T>::i = 3;
10501
10502 will work, too. */
10503 id_expression = cp_parser_id_expression (parser,
10504 /*template_keyword_p=*/false,
10505 /*check_dependency_p=*/false,
10506 /*template_p=*/NULL);
10507 /* If the name was qualified, create a SCOPE_REF to represent
10508 that. */
10509 if (parser->scope)
10510 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10511
10512 return id_expression;
10513}
10514
10515/* Parse a type-id.
10516
10517 type-id:
10518 type-specifier-seq abstract-declarator [opt]
10519
10520 Returns the TYPE specified. */
10521
10522static tree
10523cp_parser_type_id (parser)
10524 cp_parser *parser;
10525{
10526 tree type_specifier_seq;
10527 tree abstract_declarator;
10528
10529 /* Parse the type-specifier-seq. */
10530 type_specifier_seq
10531 = cp_parser_type_specifier_seq (parser);
10532 if (type_specifier_seq == error_mark_node)
10533 return error_mark_node;
10534
10535 /* There might or might not be an abstract declarator. */
10536 cp_parser_parse_tentatively (parser);
10537 /* Look for the declarator. */
10538 abstract_declarator
10539 = cp_parser_declarator (parser, /*abstract_p=*/true, NULL);
10540 /* Check to see if there really was a declarator. */
10541 if (!cp_parser_parse_definitely (parser))
10542 abstract_declarator = NULL_TREE;
10543
10544 return groktypename (build_tree_list (type_specifier_seq,
10545 abstract_declarator));
10546}
10547
10548/* Parse a type-specifier-seq.
10549
10550 type-specifier-seq:
10551 type-specifier type-specifier-seq [opt]
10552
10553 GNU extension:
10554
10555 type-specifier-seq:
10556 attributes type-specifier-seq [opt]
10557
10558 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10559 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10560
10561static tree
10562cp_parser_type_specifier_seq (parser)
10563 cp_parser *parser;
10564{
10565 bool seen_type_specifier = false;
10566 tree type_specifier_seq = NULL_TREE;
10567
10568 /* Parse the type-specifiers and attributes. */
10569 while (true)
10570 {
10571 tree type_specifier;
10572
10573 /* Check for attributes first. */
10574 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10575 {
10576 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10577 NULL_TREE,
10578 type_specifier_seq);
10579 continue;
10580 }
10581
10582 /* After the first type-specifier, others are optional. */
10583 if (seen_type_specifier)
10584 cp_parser_parse_tentatively (parser);
10585 /* Look for the type-specifier. */
10586 type_specifier = cp_parser_type_specifier (parser,
10587 CP_PARSER_FLAGS_NONE,
10588 /*is_friend=*/false,
10589 /*is_declaration=*/false,
10590 NULL,
10591 NULL);
10592 /* If the first type-specifier could not be found, this is not a
10593 type-specifier-seq at all. */
10594 if (!seen_type_specifier && type_specifier == error_mark_node)
10595 return error_mark_node;
10596 /* If subsequent type-specifiers could not be found, the
10597 type-specifier-seq is complete. */
10598 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10599 break;
10600
10601 /* Add the new type-specifier to the list. */
10602 type_specifier_seq
10603 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10604 seen_type_specifier = true;
10605 }
10606
10607 /* We built up the list in reverse order. */
10608 return nreverse (type_specifier_seq);
10609}
10610
10611/* Parse a parameter-declaration-clause.
10612
10613 parameter-declaration-clause:
10614 parameter-declaration-list [opt] ... [opt]
10615 parameter-declaration-list , ...
10616
10617 Returns a representation for the parameter declarations. Each node
10618 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10619 representation.) If the parameter-declaration-clause ends with an
10620 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10621 list. A return value of NULL_TREE indicates a
10622 parameter-declaration-clause consisting only of an ellipsis. */
10623
10624static tree
10625cp_parser_parameter_declaration_clause (parser)
10626 cp_parser *parser;
10627{
10628 tree parameters;
10629 cp_token *token;
10630 bool ellipsis_p;
10631
10632 /* Peek at the next token. */
10633 token = cp_lexer_peek_token (parser->lexer);
10634 /* Check for trivial parameter-declaration-clauses. */
10635 if (token->type == CPP_ELLIPSIS)
10636 {
10637 /* Consume the `...' token. */
10638 cp_lexer_consume_token (parser->lexer);
10639 return NULL_TREE;
10640 }
10641 else if (token->type == CPP_CLOSE_PAREN)
10642 /* There are no parameters. */
2bd78947 10643 {
10644#ifndef NO_IMPLICIT_EXTERN_C
10645 if (in_system_header && current_class_type == NULL
10646 && current_lang_name == lang_name_c)
10647 return NULL_TREE;
10648 else
10649#endif
10650 return void_list_node;
10651 }
0a3b29ad 10652 /* Check for `(void)', too, which is a special case. */
10653 else if (token->keyword == RID_VOID
10654 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10655 == CPP_CLOSE_PAREN))
10656 {
10657 /* Consume the `void' token. */
10658 cp_lexer_consume_token (parser->lexer);
10659 /* There are no parameters. */
10660 return void_list_node;
10661 }
10662
10663 /* Parse the parameter-declaration-list. */
10664 parameters = cp_parser_parameter_declaration_list (parser);
10665 /* If a parse error occurred while parsing the
10666 parameter-declaration-list, then the entire
10667 parameter-declaration-clause is erroneous. */
10668 if (parameters == error_mark_node)
10669 return error_mark_node;
10670
10671 /* Peek at the next token. */
10672 token = cp_lexer_peek_token (parser->lexer);
10673 /* If it's a `,', the clause should terminate with an ellipsis. */
10674 if (token->type == CPP_COMMA)
10675 {
10676 /* Consume the `,'. */
10677 cp_lexer_consume_token (parser->lexer);
10678 /* Expect an ellipsis. */
10679 ellipsis_p
10680 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10681 }
10682 /* It might also be `...' if the optional trailing `,' was
10683 omitted. */
10684 else if (token->type == CPP_ELLIPSIS)
10685 {
10686 /* Consume the `...' token. */
10687 cp_lexer_consume_token (parser->lexer);
10688 /* And remember that we saw it. */
10689 ellipsis_p = true;
10690 }
10691 else
10692 ellipsis_p = false;
10693
10694 /* Finish the parameter list. */
10695 return finish_parmlist (parameters, ellipsis_p);
10696}
10697
10698/* Parse a parameter-declaration-list.
10699
10700 parameter-declaration-list:
10701 parameter-declaration
10702 parameter-declaration-list , parameter-declaration
10703
10704 Returns a representation of the parameter-declaration-list, as for
10705 cp_parser_parameter_declaration_clause. However, the
10706 `void_list_node' is never appended to the list. */
10707
10708static tree
10709cp_parser_parameter_declaration_list (parser)
10710 cp_parser *parser;
10711{
10712 tree parameters = NULL_TREE;
10713
10714 /* Look for more parameters. */
10715 while (true)
10716 {
10717 tree parameter;
10718 /* Parse the parameter. */
10719 parameter
10720 = cp_parser_parameter_declaration (parser,
10721 /*greater_than_is_operator_p=*/true);
10722 /* If a parse error ocurred parsing the parameter declaration,
10723 then the entire parameter-declaration-list is erroneous. */
10724 if (parameter == error_mark_node)
10725 {
10726 parameters = error_mark_node;
10727 break;
10728 }
10729 /* Add the new parameter to the list. */
10730 TREE_CHAIN (parameter) = parameters;
10731 parameters = parameter;
10732
10733 /* Peek at the next token. */
10734 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10735 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10736 /* The parameter-declaration-list is complete. */
10737 break;
10738 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10739 {
10740 cp_token *token;
10741
10742 /* Peek at the next token. */
10743 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10744 /* If it's an ellipsis, then the list is complete. */
10745 if (token->type == CPP_ELLIPSIS)
10746 break;
10747 /* Otherwise, there must be more parameters. Consume the
10748 `,'. */
10749 cp_lexer_consume_token (parser->lexer);
10750 }
10751 else
10752 {
10753 cp_parser_error (parser, "expected `,' or `...'");
10754 break;
10755 }
10756 }
10757
10758 /* We built up the list in reverse order; straighten it out now. */
10759 return nreverse (parameters);
10760}
10761
10762/* Parse a parameter declaration.
10763
10764 parameter-declaration:
10765 decl-specifier-seq declarator
10766 decl-specifier-seq declarator = assignment-expression
10767 decl-specifier-seq abstract-declarator [opt]
10768 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10769
10770 If GREATER_THAN_IS_OPERATOR_P is FALSE, then a non-nested `>' token
10771 encountered during the parsing of the assignment-expression is not
10772 interpreted as a greater-than operator.
10773
10774 Returns a TREE_LIST representing the parameter-declaration. The
10775 TREE_VALUE is a representation of the decl-specifier-seq and
10776 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10777 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10778 TREE_VALUE represents the declarator. */
10779
10780static tree
10781cp_parser_parameter_declaration (parser, greater_than_is_operator_p)
10782 cp_parser *parser;
10783 bool greater_than_is_operator_p;
10784{
10785 bool declares_class_or_enum;
10786 tree decl_specifiers;
10787 tree attributes;
10788 tree declarator;
10789 tree default_argument;
10790 tree parameter;
10791 cp_token *token;
10792 const char *saved_message;
10793
10794 /* Type definitions may not appear in parameter types. */
10795 saved_message = parser->type_definition_forbidden_message;
10796 parser->type_definition_forbidden_message
10797 = "types may not be defined in parameter types";
10798
10799 /* Parse the declaration-specifiers. */
10800 decl_specifiers
10801 = cp_parser_decl_specifier_seq (parser,
10802 CP_PARSER_FLAGS_NONE,
10803 &attributes,
10804 &declares_class_or_enum);
10805 /* If an error occurred, there's no reason to attempt to parse the
10806 rest of the declaration. */
10807 if (cp_parser_error_occurred (parser))
10808 {
10809 parser->type_definition_forbidden_message = saved_message;
10810 return error_mark_node;
10811 }
10812
10813 /* Peek at the next token. */
10814 token = cp_lexer_peek_token (parser->lexer);
10815 /* If the next token is a `)', `,', `=', `>', or `...', then there
10816 is no declarator. */
10817 if (token->type == CPP_CLOSE_PAREN
10818 || token->type == CPP_COMMA
10819 || token->type == CPP_EQ
10820 || token->type == CPP_ELLIPSIS
10821 || token->type == CPP_GREATER)
10822 declarator = NULL_TREE;
10823 /* Otherwise, there should be a declarator. */
10824 else
10825 {
10826 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10827 parser->default_arg_ok_p = false;
10828
10829 /* We don't know whether the declarator will be abstract or
10830 not. So, first we try an ordinary declarator. */
10831 cp_parser_parse_tentatively (parser);
10832 declarator = cp_parser_declarator (parser,
10833 /*abstract_p=*/false,
10834 /*ctor_dtor_or_conv_p=*/NULL);
10835 /* If that didn't work, look for an abstract declarator. */
10836 if (!cp_parser_parse_definitely (parser))
10837 declarator = cp_parser_declarator (parser,
10838 /*abstract_p=*/true,
10839 /*ctor_dtor_or_conv_p=*/NULL);
10840 parser->default_arg_ok_p = saved_default_arg_ok_p;
10841 }
10842
10843 /* The restriction on definining new types applies only to the type
10844 of the parameter, not to the default argument. */
10845 parser->type_definition_forbidden_message = saved_message;
10846
10847 /* If the next token is `=', then process a default argument. */
10848 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10849 {
10850 bool saved_greater_than_is_operator_p;
10851 /* Consume the `='. */
10852 cp_lexer_consume_token (parser->lexer);
10853
10854 /* If we are defining a class, then the tokens that make up the
10855 default argument must be saved and processed later. */
10856 if (at_class_scope_p () && TYPE_BEING_DEFINED (current_class_type))
10857 {
10858 unsigned depth = 0;
10859
10860 /* Create a DEFAULT_ARG to represented the unparsed default
10861 argument. */
10862 default_argument = make_node (DEFAULT_ARG);
10863 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10864
10865 /* Add tokens until we have processed the entire default
10866 argument. */
10867 while (true)
10868 {
10869 bool done = false;
10870 cp_token *token;
10871
10872 /* Peek at the next token. */
10873 token = cp_lexer_peek_token (parser->lexer);
10874 /* What we do depends on what token we have. */
10875 switch (token->type)
10876 {
10877 /* In valid code, a default argument must be
10878 immediately followed by a `,' `)', or `...'. */
10879 case CPP_COMMA:
10880 case CPP_CLOSE_PAREN:
10881 case CPP_ELLIPSIS:
10882 /* If we run into a non-nested `;', `}', or `]',
10883 then the code is invalid -- but the default
10884 argument is certainly over. */
10885 case CPP_SEMICOLON:
10886 case CPP_CLOSE_BRACE:
10887 case CPP_CLOSE_SQUARE:
10888 if (depth == 0)
10889 done = true;
10890 /* Update DEPTH, if necessary. */
10891 else if (token->type == CPP_CLOSE_PAREN
10892 || token->type == CPP_CLOSE_BRACE
10893 || token->type == CPP_CLOSE_SQUARE)
10894 --depth;
10895 break;
10896
10897 case CPP_OPEN_PAREN:
10898 case CPP_OPEN_SQUARE:
10899 case CPP_OPEN_BRACE:
10900 ++depth;
10901 break;
10902
10903 case CPP_GREATER:
10904 /* If we see a non-nested `>', and `>' is not an
10905 operator, then it marks the end of the default
10906 argument. */
10907 if (!depth && !greater_than_is_operator_p)
10908 done = true;
10909 break;
10910
10911 /* If we run out of tokens, issue an error message. */
10912 case CPP_EOF:
10913 error ("file ends in default argument");
10914 done = true;
10915 break;
10916
10917 case CPP_NAME:
10918 case CPP_SCOPE:
10919 /* In these cases, we should look for template-ids.
10920 For example, if the default argument is
10921 `X<int, double>()', we need to do name lookup to
10922 figure out whether or not `X' is a template; if
10923 so, the `,' does not end the deault argument.
10924
10925 That is not yet done. */
10926 break;
10927
10928 default:
10929 break;
10930 }
10931
10932 /* If we've reached the end, stop. */
10933 if (done)
10934 break;
10935
10936 /* Add the token to the token block. */
10937 token = cp_lexer_consume_token (parser->lexer);
10938 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10939 token);
10940 }
10941 }
10942 /* Outside of a class definition, we can just parse the
10943 assignment-expression. */
10944 else
10945 {
10946 bool saved_local_variables_forbidden_p;
10947
10948 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10949 set correctly. */
10950 saved_greater_than_is_operator_p
10951 = parser->greater_than_is_operator_p;
10952 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10953 /* Local variable names (and the `this' keyword) may not
10954 appear in a default argument. */
10955 saved_local_variables_forbidden_p
10956 = parser->local_variables_forbidden_p;
10957 parser->local_variables_forbidden_p = true;
10958 /* Parse the assignment-expression. */
10959 default_argument = cp_parser_assignment_expression (parser);
10960 /* Restore saved state. */
10961 parser->greater_than_is_operator_p
10962 = saved_greater_than_is_operator_p;
10963 parser->local_variables_forbidden_p
10964 = saved_local_variables_forbidden_p;
10965 }
10966 if (!parser->default_arg_ok_p)
10967 {
10968 pedwarn ("default arguments are only permitted on functions");
10969 if (flag_pedantic_errors)
10970 default_argument = NULL_TREE;
10971 }
10972 }
10973 else
10974 default_argument = NULL_TREE;
10975
10976 /* Create the representation of the parameter. */
10977 if (attributes)
10978 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10979 parameter = build_tree_list (default_argument,
10980 build_tree_list (decl_specifiers,
10981 declarator));
10982
10983 return parameter;
10984}
10985
10986/* Parse a function-definition.
10987
10988 function-definition:
10989 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10990 function-body
10991 decl-specifier-seq [opt] declarator function-try-block
10992
10993 GNU Extension:
10994
10995 function-definition:
10996 __extension__ function-definition
10997
10998 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10999 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11000 be a `friend'. */
11001
11002static tree
11003cp_parser_function_definition (parser, friend_p)
11004 cp_parser *parser;
11005 bool *friend_p;
11006{
11007 tree decl_specifiers;
11008 tree attributes;
11009 tree declarator;
11010 tree fn;
11011 tree access_checks;
11012 cp_token *token;
11013 bool declares_class_or_enum;
11014 bool member_p;
11015 /* The saved value of the PEDANTIC flag. */
11016 int saved_pedantic;
11017
11018 /* Any pending qualification must be cleared by our caller. It is
11019 more robust to force the callers to clear PARSER->SCOPE than to
11020 do it here since if the qualification is in effect here, it might
11021 also end up in effect elsewhere that it is not intended. */
11022 my_friendly_assert (!parser->scope, 20010821);
11023
11024 /* Handle `__extension__'. */
11025 if (cp_parser_extension_opt (parser, &saved_pedantic))
11026 {
11027 /* Parse the function-definition. */
11028 fn = cp_parser_function_definition (parser, friend_p);
11029 /* Restore the PEDANTIC flag. */
11030 pedantic = saved_pedantic;
11031
11032 return fn;
11033 }
11034
11035 /* Check to see if this definition appears in a class-specifier. */
11036 member_p = (at_class_scope_p ()
11037 && TYPE_BEING_DEFINED (current_class_type));
11038 /* Defer access checks in the decl-specifier-seq until we know what
11039 function is being defined. There is no need to do this for the
11040 definition of member functions; we cannot be defining a member
11041 from another class. */
11042 if (!member_p)
11043 cp_parser_start_deferring_access_checks (parser);
11044 /* Parse the decl-specifier-seq. */
11045 decl_specifiers
11046 = cp_parser_decl_specifier_seq (parser,
11047 CP_PARSER_FLAGS_OPTIONAL,
11048 &attributes,
11049 &declares_class_or_enum);
11050 /* Figure out whether this declaration is a `friend'. */
11051 if (friend_p)
11052 *friend_p = cp_parser_friend_p (decl_specifiers);
11053
11054 /* Parse the declarator. */
11055 declarator = cp_parser_declarator (parser,
11056 /*abstract_p=*/false,
11057 /*ctor_dtor_or_conv_p=*/NULL);
11058
11059 /* Gather up any access checks that occurred. */
11060 if (!member_p)
11061 access_checks = cp_parser_stop_deferring_access_checks (parser);
11062 else
11063 access_checks = NULL_TREE;
11064
11065 /* If something has already gone wrong, we may as well stop now. */
11066 if (declarator == error_mark_node)
11067 {
11068 /* Skip to the end of the function, or if this wasn't anything
11069 like a function-definition, to a `;' in the hopes of finding
11070 a sensible place from which to continue parsing. */
11071 cp_parser_skip_to_end_of_block_or_statement (parser);
11072 return error_mark_node;
11073 }
11074
11075 /* The next character should be a `{' (for a simple function
11076 definition), a `:' (for a ctor-initializer), or `try' (for a
11077 function-try block). */
11078 token = cp_lexer_peek_token (parser->lexer);
11079 if (!cp_parser_token_starts_function_definition_p (token))
11080 {
11081 /* Issue the error-message. */
11082 cp_parser_error (parser, "expected function-definition");
11083 /* Skip to the next `;'. */
11084 cp_parser_skip_to_end_of_block_or_statement (parser);
11085
11086 return error_mark_node;
11087 }
11088
11089 /* If we are in a class scope, then we must handle
11090 function-definitions specially. In particular, we save away the
11091 tokens that make up the function body, and parse them again
11092 later, in order to handle code like:
11093
11094 struct S {
11095 int f () { return i; }
11096 int i;
11097 };
11098
11099 Here, we cannot parse the body of `f' until after we have seen
11100 the declaration of `i'. */
11101 if (member_p)
11102 {
11103 cp_token_cache *cache;
11104
11105 /* Create the function-declaration. */
11106 fn = start_method (decl_specifiers, declarator, attributes);
11107 /* If something went badly wrong, bail out now. */
11108 if (fn == error_mark_node)
11109 {
11110 /* If there's a function-body, skip it. */
11111 if (cp_parser_token_starts_function_definition_p
11112 (cp_lexer_peek_token (parser->lexer)))
11113 cp_parser_skip_to_end_of_block_or_statement (parser);
11114 return error_mark_node;
11115 }
11116
11117 /* Create a token cache. */
11118 cache = cp_token_cache_new ();
11119 /* Save away the tokens that make up the body of the
11120 function. */
11121 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11122 /* Handle function try blocks. */
11123 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11124 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11125
11126 /* Save away the inline definition; we will process it when the
11127 class is complete. */
11128 DECL_PENDING_INLINE_INFO (fn) = cache;
11129 DECL_PENDING_INLINE_P (fn) = 1;
11130
11131 /* We're done with the inline definition. */
11132 finish_method (fn);
11133
11134 /* Add FN to the queue of functions to be parsed later. */
11135 TREE_VALUE (parser->unparsed_functions_queues)
11136 = tree_cons (current_class_type, fn,
11137 TREE_VALUE (parser->unparsed_functions_queues));
11138
11139 return fn;
11140 }
11141
11142 /* Check that the number of template-parameter-lists is OK. */
11143 if (!cp_parser_check_declarator_template_parameters (parser,
11144 declarator))
11145 {
11146 cp_parser_skip_to_end_of_block_or_statement (parser);
11147 return error_mark_node;
11148 }
11149
11150 return (cp_parser_function_definition_from_specifiers_and_declarator
11151 (parser, decl_specifiers, attributes, declarator, access_checks));
11152}
11153
11154/* Parse a function-body.
11155
11156 function-body:
11157 compound_statement */
11158
11159static void
11160cp_parser_function_body (cp_parser *parser)
11161{
11162 cp_parser_compound_statement (parser);
11163}
11164
11165/* Parse a ctor-initializer-opt followed by a function-body. Return
11166 true if a ctor-initializer was present. */
11167
11168static bool
11169cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11170{
11171 tree body;
11172 bool ctor_initializer_p;
11173
11174 /* Begin the function body. */
11175 body = begin_function_body ();
11176 /* Parse the optional ctor-initializer. */
11177 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11178 /* Parse the function-body. */
11179 cp_parser_function_body (parser);
11180 /* Finish the function body. */
11181 finish_function_body (body);
11182
11183 return ctor_initializer_p;
11184}
11185
11186/* Parse an initializer.
11187
11188 initializer:
11189 = initializer-clause
11190 ( expression-list )
11191
11192 Returns a expression representing the initializer. If no
11193 initializer is present, NULL_TREE is returned.
11194
11195 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11196 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11197 set to FALSE if there is no initializer present. */
11198
11199static tree
11200cp_parser_initializer (parser, is_parenthesized_init)
11201 cp_parser *parser;
11202 bool *is_parenthesized_init;
11203{
11204 cp_token *token;
11205 tree init;
11206
11207 /* Peek at the next token. */
11208 token = cp_lexer_peek_token (parser->lexer);
11209
11210 /* Let our caller know whether or not this initializer was
11211 parenthesized. */
11212 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11213
11214 if (token->type == CPP_EQ)
11215 {
11216 /* Consume the `='. */
11217 cp_lexer_consume_token (parser->lexer);
11218 /* Parse the initializer-clause. */
11219 init = cp_parser_initializer_clause (parser);
11220 }
11221 else if (token->type == CPP_OPEN_PAREN)
11222 {
11223 /* Consume the `('. */
11224 cp_lexer_consume_token (parser->lexer);
11225 /* Parse the expression-list. */
11226 init = cp_parser_expression_list (parser);
11227 /* Consume the `)' token. */
11228 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11229 cp_parser_skip_to_closing_parenthesis (parser);
11230 }
11231 else
11232 {
11233 /* Anything else is an error. */
11234 cp_parser_error (parser, "expected initializer");
11235 init = error_mark_node;
11236 }
11237
11238 return init;
11239}
11240
11241/* Parse an initializer-clause.
11242
11243 initializer-clause:
11244 assignment-expression
11245 { initializer-list , [opt] }
11246 { }
11247
11248 Returns an expression representing the initializer.
11249
11250 If the `assignment-expression' production is used the value
11251 returned is simply a reprsentation for the expression.
11252
11253 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11254 the elements of the initializer-list (or NULL_TREE, if the last
11255 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11256 NULL_TREE. There is no way to detect whether or not the optional
11257 trailing `,' was provided. */
11258
11259static tree
11260cp_parser_initializer_clause (parser)
11261 cp_parser *parser;
11262{
11263 tree initializer;
11264
11265 /* If it is not a `{', then we are looking at an
11266 assignment-expression. */
11267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11268 initializer = cp_parser_assignment_expression (parser);
11269 else
11270 {
11271 /* Consume the `{' token. */
11272 cp_lexer_consume_token (parser->lexer);
11273 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11274 initializer = make_node (CONSTRUCTOR);
11275 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11276 necessary, but check_initializer depends upon it, for
11277 now. */
11278 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11279 /* If it's not a `}', then there is a non-trivial initializer. */
11280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11281 {
11282 /* Parse the initializer list. */
11283 CONSTRUCTOR_ELTS (initializer)
11284 = cp_parser_initializer_list (parser);
11285 /* A trailing `,' token is allowed. */
11286 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11287 cp_lexer_consume_token (parser->lexer);
11288 }
11289
11290 /* Now, there should be a trailing `}'. */
11291 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11292 }
11293
11294 return initializer;
11295}
11296
11297/* Parse an initializer-list.
11298
11299 initializer-list:
11300 initializer-clause
11301 initializer-list , initializer-clause
11302
11303 GNU Extension:
11304
11305 initializer-list:
11306 identifier : initializer-clause
11307 initializer-list, identifier : initializer-clause
11308
11309 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11310 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11311 IDENTIFIER_NODE naming the field to initialize. */
11312
11313static tree
11314cp_parser_initializer_list (parser)
11315 cp_parser *parser;
11316{
11317 tree initializers = NULL_TREE;
11318
11319 /* Parse the rest of the list. */
11320 while (true)
11321 {
11322 cp_token *token;
11323 tree identifier;
11324 tree initializer;
11325
11326 /* If the next token is an identifier and the following one is a
11327 colon, we are looking at the GNU designated-initializer
11328 syntax. */
11329 if (cp_parser_allow_gnu_extensions_p (parser)
11330 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11331 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11332 {
11333 /* Consume the identifier. */
11334 identifier = cp_lexer_consume_token (parser->lexer)->value;
11335 /* Consume the `:'. */
11336 cp_lexer_consume_token (parser->lexer);
11337 }
11338 else
11339 identifier = NULL_TREE;
11340
11341 /* Parse the initializer. */
11342 initializer = cp_parser_initializer_clause (parser);
11343
11344 /* Add it to the list. */
11345 initializers = tree_cons (identifier, initializer, initializers);
11346
11347 /* If the next token is not a comma, we have reached the end of
11348 the list. */
11349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11350 break;
11351
11352 /* Peek at the next token. */
11353 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11354 /* If the next token is a `}', then we're still done. An
11355 initializer-clause can have a trailing `,' after the
11356 initializer-list and before the closing `}'. */
11357 if (token->type == CPP_CLOSE_BRACE)
11358 break;
11359
11360 /* Consume the `,' token. */
11361 cp_lexer_consume_token (parser->lexer);
11362 }
11363
11364 /* The initializers were built up in reverse order, so we need to
11365 reverse them now. */
11366 return nreverse (initializers);
11367}
11368
11369/* Classes [gram.class] */
11370
11371/* Parse a class-name.
11372
11373 class-name:
11374 identifier
11375 template-id
11376
11377 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11378 to indicate that names looked up in dependent types should be
11379 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11380 keyword has been used to indicate that the name that appears next
11381 is a template. TYPE_P is true iff the next name should be treated
11382 as class-name, even if it is declared to be some other kind of name
11383 as well. The accessibility of the class-name is checked iff
11384 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11385 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11386 is the class being defined in a class-head.
11387
11388 Returns the TYPE_DECL representing the class. */
11389
11390static tree
11391cp_parser_class_name (cp_parser *parser,
11392 bool typename_keyword_p,
11393 bool template_keyword_p,
11394 bool type_p,
11395 bool check_access_p,
11396 bool check_dependency_p,
11397 bool class_head_p)
11398{
11399 tree decl;
11400 tree scope;
11401 bool typename_p;
2c593bd0 11402 cp_token *token;
11403
11404 /* All class-names start with an identifier. */
11405 token = cp_lexer_peek_token (parser->lexer);
11406 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11407 {
11408 cp_parser_error (parser, "expected class-name");
11409 return error_mark_node;
11410 }
11411
0a3b29ad 11412 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11413 to a template-id, so we save it here. */
11414 scope = parser->scope;
11415 /* Any name names a type if we're following the `typename' keyword
11416 in a qualified name where the enclosing scope is type-dependent. */
11417 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11418 && cp_parser_dependent_type_p (scope));
2c593bd0 11419 /* Handle the common case (an identifier, but not a template-id)
11420 efficiently. */
11421 if (token->type == CPP_NAME
11422 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
0a3b29ad 11423 {
0a3b29ad 11424 tree identifier;
11425
11426 /* Look for the identifier. */
11427 identifier = cp_parser_identifier (parser);
11428 /* If the next token isn't an identifier, we are certainly not
11429 looking at a class-name. */
11430 if (identifier == error_mark_node)
11431 decl = error_mark_node;
11432 /* If we know this is a type-name, there's no need to look it
11433 up. */
11434 else if (typename_p)
11435 decl = identifier;
11436 else
11437 {
11438 /* If the next token is a `::', then the name must be a type
11439 name.
11440
11441 [basic.lookup.qual]
11442
11443 During the lookup for a name preceding the :: scope
11444 resolution operator, object, function, and enumerator
11445 names are ignored. */
11446 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11447 type_p = true;
11448 /* Look up the name. */
11449 decl = cp_parser_lookup_name (parser, identifier,
11450 check_access_p,
11451 type_p,
6fc758aa 11452 /*is_namespace=*/false,
0a3b29ad 11453 check_dependency_p);
11454 }
11455 }
2c593bd0 11456 else
11457 {
11458 /* Try a template-id. */
11459 decl = cp_parser_template_id (parser, template_keyword_p,
11460 check_dependency_p);
11461 if (decl == error_mark_node)
11462 return error_mark_node;
11463 }
0a3b29ad 11464
11465 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11466
11467 /* If this is a typename, create a TYPENAME_TYPE. */
11468 if (typename_p && decl != error_mark_node)
11469 decl = TYPE_NAME (make_typename_type (scope, decl,
11470 /*complain=*/1));
11471
11472 /* Check to see that it is really the name of a class. */
11473 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11474 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11475 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11476 /* Situations like this:
11477
11478 template <typename T> struct A {
11479 typename T::template X<int>::I i;
11480 };
11481
11482 are problematic. Is `T::template X<int>' a class-name? The
11483 standard does not seem to be definitive, but there is no other
11484 valid interpretation of the following `::'. Therefore, those
11485 names are considered class-names. */
11486 decl = TYPE_NAME (make_typename_type (scope, decl,
11487 tf_error | tf_parsing));
11488 else if (decl == error_mark_node
11489 || TREE_CODE (decl) != TYPE_DECL
11490 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11491 {
11492 cp_parser_error (parser, "expected class-name");
11493 return error_mark_node;
11494 }
11495
11496 return decl;
11497}
11498
11499/* Parse a class-specifier.
11500
11501 class-specifier:
11502 class-head { member-specification [opt] }
11503
11504 Returns the TREE_TYPE representing the class. */
11505
11506static tree
11507cp_parser_class_specifier (parser)
11508 cp_parser *parser;
11509{
11510 cp_token *token;
11511 tree type;
11512 tree attributes = NULL_TREE;
11513 int has_trailing_semicolon;
11514 bool nested_name_specifier_p;
11515 bool deferring_access_checks_p;
11516 tree saved_access_checks;
11517 unsigned saved_num_template_parameter_lists;
11518
11519 /* Parse the class-head. */
11520 type = cp_parser_class_head (parser,
11521 &nested_name_specifier_p,
11522 &deferring_access_checks_p,
11523 &saved_access_checks);
11524 /* If the class-head was a semantic disaster, skip the entire body
11525 of the class. */
11526 if (!type)
11527 {
11528 cp_parser_skip_to_end_of_block_or_statement (parser);
11529 return error_mark_node;
11530 }
11531 /* Look for the `{'. */
11532 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11533 return error_mark_node;
11534 /* Issue an error message if type-definitions are forbidden here. */
11535 cp_parser_check_type_definition (parser);
11536 /* Remember that we are defining one more class. */
11537 ++parser->num_classes_being_defined;
11538 /* Inside the class, surrounding template-parameter-lists do not
11539 apply. */
11540 saved_num_template_parameter_lists
11541 = parser->num_template_parameter_lists;
11542 parser->num_template_parameter_lists = 0;
11543 /* Start the class. */
11544 type = begin_class_definition (type);
11545 if (type == error_mark_node)
11546 /* If the type is erroneous, skip the entire body of the class. */
11547 cp_parser_skip_to_closing_brace (parser);
11548 else
11549 /* Parse the member-specification. */
11550 cp_parser_member_specification_opt (parser);
11551 /* Look for the trailing `}'. */
11552 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11553 /* We get better error messages by noticing a common problem: a
11554 missing trailing `;'. */
11555 token = cp_lexer_peek_token (parser->lexer);
11556 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11557 /* Look for attributes to apply to this class. */
11558 if (cp_parser_allow_gnu_extensions_p (parser))
11559 attributes = cp_parser_attributes_opt (parser);
11560 /* Finish the class definition. */
11561 type = finish_class_definition (type,
11562 attributes,
11563 has_trailing_semicolon,
11564 nested_name_specifier_p);
11565 /* If this class is not itself within the scope of another class,
11566 then we need to parse the bodies of all of the queued function
11567 definitions. Note that the queued functions defined in a class
11568 are not always processed immediately following the
11569 class-specifier for that class. Consider:
11570
11571 struct A {
11572 struct B { void f() { sizeof (A); } };
11573 };
11574
11575 If `f' were processed before the processing of `A' were
11576 completed, there would be no way to compute the size of `A'.
11577 Note that the nesting we are interested in here is lexical --
11578 not the semantic nesting given by TYPE_CONTEXT. In particular,
11579 for:
11580
11581 struct A { struct B; };
11582 struct A::B { void f() { } };
11583
11584 there is no need to delay the parsing of `A::B::f'. */
11585 if (--parser->num_classes_being_defined == 0)
11586 {
11587 tree last_scope = NULL_TREE;
11588
11589 /* Process non FUNCTION_DECL related DEFAULT_ARGs. */
11590 for (parser->default_arg_types = nreverse (parser->default_arg_types);
11591 parser->default_arg_types;
11592 parser->default_arg_types = TREE_CHAIN (parser->default_arg_types))
11593 cp_parser_late_parsing_default_args
2c593bd0 11594 (parser, TREE_PURPOSE (parser->default_arg_types), NULL_TREE);
0a3b29ad 11595
11596 /* Reverse the queue, so that we process it in the order the
11597 functions were declared. */
11598 TREE_VALUE (parser->unparsed_functions_queues)
11599 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11600 /* Loop through all of the functions. */
11601 while (TREE_VALUE (parser->unparsed_functions_queues))
11602
11603 {
11604 tree fn;
11605 tree fn_scope;
11606 tree queue_entry;
11607
11608 /* Figure out which function we need to process. */
11609 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11610 fn_scope = TREE_PURPOSE (queue_entry);
11611 fn = TREE_VALUE (queue_entry);
11612
11613 /* Parse the function. */
11614 cp_parser_late_parsing_for_member (parser, fn);
11615
11616 TREE_VALUE (parser->unparsed_functions_queues)
11617 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11618 }
11619
11620 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11621 more time than we have popped, so me must pop here. */
11622 if (last_scope)
11623 pop_scope (last_scope);
11624 }
11625
11626 /* Put back any saved access checks. */
11627 if (deferring_access_checks_p)
11628 {
11629 cp_parser_start_deferring_access_checks (parser);
11630 parser->context->deferred_access_checks = saved_access_checks;
11631 }
11632
11633 /* Restore the count of active template-parameter-lists. */
11634 parser->num_template_parameter_lists
11635 = saved_num_template_parameter_lists;
11636
11637 return type;
11638}
11639
11640/* Parse a class-head.
11641
11642 class-head:
11643 class-key identifier [opt] base-clause [opt]
11644 class-key nested-name-specifier identifier base-clause [opt]
11645 class-key nested-name-specifier [opt] template-id
11646 base-clause [opt]
11647
11648 GNU Extensions:
11649 class-key attributes identifier [opt] base-clause [opt]
11650 class-key attributes nested-name-specifier identifier base-clause [opt]
11651 class-key attributes nested-name-specifier [opt] template-id
11652 base-clause [opt]
11653
11654 Returns the TYPE of the indicated class. Sets
11655 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11656 involving a nested-name-specifier was used, and FALSE otherwise.
11657 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11658 access checks before this class-head. In that case,
11659 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11660 checks.
11661
11662 Returns NULL_TREE if the class-head is syntactically valid, but
11663 semantically invalid in a way that means we should skip the entire
11664 body of the class. */
11665
11666static tree
11667cp_parser_class_head (parser,
11668 nested_name_specifier_p,
11669 deferring_access_checks_p,
11670 saved_access_checks)
11671 cp_parser *parser;
11672 bool *nested_name_specifier_p;
11673 bool *deferring_access_checks_p;
11674 tree *saved_access_checks;
11675{
11676 cp_token *token;
11677 tree nested_name_specifier;
11678 enum tag_types class_key;
11679 tree id = NULL_TREE;
11680 tree type = NULL_TREE;
11681 tree attributes;
11682 bool template_id_p = false;
11683 bool qualified_p = false;
11684 bool invalid_nested_name_p = false;
11685 unsigned num_templates;
11686
11687 /* Assume no nested-name-specifier will be present. */
11688 *nested_name_specifier_p = false;
11689 /* Assume no template parameter lists will be used in defining the
11690 type. */
11691 num_templates = 0;
11692
11693 /* Look for the class-key. */
11694 class_key = cp_parser_class_key (parser);
11695 if (class_key == none_type)
11696 return error_mark_node;
11697
11698 /* Parse the attributes. */
11699 attributes = cp_parser_attributes_opt (parser);
11700
11701 /* If the next token is `::', that is invalid -- but sometimes
11702 people do try to write:
11703
11704 struct ::S {};
11705
11706 Handle this gracefully by accepting the extra qualifier, and then
11707 issuing an error about it later if this really is a
11708 class-header. If it turns out just to be an elaborated type
11709 specifier, remain silent. */
11710 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11711 qualified_p = true;
11712
11713 /* Determine the name of the class. Begin by looking for an
11714 optional nested-name-specifier. */
11715 nested_name_specifier
11716 = cp_parser_nested_name_specifier_opt (parser,
11717 /*typename_keyword_p=*/false,
11718 /*check_dependency_p=*/true,
11719 /*type_p=*/false);
11720 /* If there was a nested-name-specifier, then there *must* be an
11721 identifier. */
11722 if (nested_name_specifier)
11723 {
11724 /* Although the grammar says `identifier', it really means
11725 `class-name' or `template-name'. You are only allowed to
11726 define a class that has already been declared with this
11727 syntax.
11728
11729 The proposed resolution for Core Issue 180 says that whever
11730 you see `class T::X' you should treat `X' as a type-name.
11731
11732 It is OK to define an inaccessible class; for example:
11733
11734 class A { class B; };
11735 class A::B {};
11736
11737 So, we ask cp_parser_class_name not to check accessibility.
11738
11739 We do not know if we will see a class-name, or a
11740 template-name. We look for a class-name first, in case the
11741 class-name is a template-id; if we looked for the
11742 template-name first we would stop after the template-name. */
11743 cp_parser_parse_tentatively (parser);
11744 type = cp_parser_class_name (parser,
11745 /*typename_keyword_p=*/false,
11746 /*template_keyword_p=*/false,
11747 /*type_p=*/true,
11748 /*check_access_p=*/false,
11749 /*check_dependency_p=*/false,
11750 /*class_head_p=*/true);
11751 /* If that didn't work, ignore the nested-name-specifier. */
11752 if (!cp_parser_parse_definitely (parser))
11753 {
11754 invalid_nested_name_p = true;
11755 id = cp_parser_identifier (parser);
11756 if (id == error_mark_node)
11757 id = NULL_TREE;
11758 }
11759 /* If we could not find a corresponding TYPE, treat this
11760 declaration like an unqualified declaration. */
11761 if (type == error_mark_node)
11762 nested_name_specifier = NULL_TREE;
11763 /* Otherwise, count the number of templates used in TYPE and its
11764 containing scopes. */
11765 else
11766 {
11767 tree scope;
11768
11769 for (scope = TREE_TYPE (type);
11770 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11771 scope = (TYPE_P (scope)
11772 ? TYPE_CONTEXT (scope)
11773 : DECL_CONTEXT (scope)))
11774 if (TYPE_P (scope)
11775 && CLASS_TYPE_P (scope)
11776 && CLASSTYPE_TEMPLATE_INFO (scope)
11777 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
11778 ++num_templates;
11779 }
11780 }
11781 /* Otherwise, the identifier is optional. */
11782 else
11783 {
11784 /* We don't know whether what comes next is a template-id,
11785 an identifier, or nothing at all. */
11786 cp_parser_parse_tentatively (parser);
11787 /* Check for a template-id. */
11788 id = cp_parser_template_id (parser,
11789 /*template_keyword_p=*/false,
11790 /*check_dependency_p=*/true);
11791 /* If that didn't work, it could still be an identifier. */
11792 if (!cp_parser_parse_definitely (parser))
11793 {
11794 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11795 id = cp_parser_identifier (parser);
11796 else
11797 id = NULL_TREE;
11798 }
11799 else
11800 {
11801 template_id_p = true;
11802 ++num_templates;
11803 }
11804 }
11805
11806 /* If it's not a `:' or a `{' then we can't really be looking at a
11807 class-head, since a class-head only appears as part of a
11808 class-specifier. We have to detect this situation before calling
11809 xref_tag, since that has irreversible side-effects. */
11810 if (!cp_parser_next_token_starts_class_definition_p (parser))
11811 {
11812 cp_parser_error (parser, "expected `{' or `:'");
11813 return error_mark_node;
11814 }
11815
11816 /* At this point, we're going ahead with the class-specifier, even
11817 if some other problem occurs. */
11818 cp_parser_commit_to_tentative_parse (parser);
11819 /* Issue the error about the overly-qualified name now. */
11820 if (qualified_p)
11821 cp_parser_error (parser,
11822 "global qualification of class name is invalid");
11823 else if (invalid_nested_name_p)
11824 cp_parser_error (parser,
11825 "qualified name does not name a class");
11826 /* Make sure that the right number of template parameters were
11827 present. */
11828 if (!cp_parser_check_template_parameters (parser, num_templates))
11829 /* If something went wrong, there is no point in even trying to
11830 process the class-definition. */
11831 return NULL_TREE;
11832
11833 /* We do not need to defer access checks for entities declared
11834 within the class. But, we do need to save any access checks that
11835 are currently deferred and restore them later, in case we are in
11836 the middle of something else. */
11837 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
11838 if (*deferring_access_checks_p)
11839 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
11840
11841 /* Look up the type. */
11842 if (template_id_p)
11843 {
11844 type = TREE_TYPE (id);
11845 maybe_process_partial_specialization (type);
11846 }
11847 else if (!nested_name_specifier)
11848 {
11849 /* If the class was unnamed, create a dummy name. */
11850 if (!id)
11851 id = make_anon_name ();
11852 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11853 }
11854 else
11855 {
26ac6687 11856 bool new_type_p;
0a3b29ad 11857 tree class_type;
11858
11859 /* Given:
11860
11861 template <typename T> struct S { struct T };
11862 template <typename T> struct S::T { };
11863
11864 we will get a TYPENAME_TYPE when processing the definition of
11865 `S::T'. We need to resolve it to the actual type before we
11866 try to define it. */
11867 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11868 {
11869 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
11870 if (type != error_mark_node)
11871 type = TYPE_NAME (type);
11872 }
11873
11874 maybe_process_partial_specialization (TREE_TYPE (type));
11875 class_type = current_class_type;
11876 type = TREE_TYPE (handle_class_head (class_key,
11877 nested_name_specifier,
11878 type,
11879 attributes,
26ac6687 11880 /*defn_p=*/true,
0a3b29ad 11881 &new_type_p));
11882 if (type != error_mark_node)
11883 {
11884 if (!class_type && TYPE_CONTEXT (type))
11885 *nested_name_specifier_p = true;
11886 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11887 class_type))
11888 *nested_name_specifier_p = true;
11889 }
11890 }
11891 /* Indicate whether this class was declared as a `class' or as a
11892 `struct'. */
11893 if (TREE_CODE (type) == RECORD_TYPE)
11894 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11895 cp_parser_check_class_key (class_key, type);
11896
11897 /* Enter the scope containing the class; the names of base classes
11898 should be looked up in that context. For example, given:
11899
11900 struct A { struct B {}; struct C; };
11901 struct A::C : B {};
11902
11903 is valid. */
11904 if (nested_name_specifier)
11905 push_scope (nested_name_specifier);
11906 /* Now, look for the base-clause. */
11907 token = cp_lexer_peek_token (parser->lexer);
11908 if (token->type == CPP_COLON)
11909 {
11910 tree bases;
11911
11912 /* Get the list of base-classes. */
11913 bases = cp_parser_base_clause (parser);
11914 /* Process them. */
11915 xref_basetypes (type, bases);
11916 }
11917 /* Leave the scope given by the nested-name-specifier. We will
11918 enter the class scope itself while processing the members. */
11919 if (nested_name_specifier)
11920 pop_scope (nested_name_specifier);
11921
11922 return type;
11923}
11924
11925/* Parse a class-key.
11926
11927 class-key:
11928 class
11929 struct
11930 union
11931
11932 Returns the kind of class-key specified, or none_type to indicate
11933 error. */
11934
11935static enum tag_types
11936cp_parser_class_key (parser)
11937 cp_parser *parser;
11938{
11939 cp_token *token;
11940 enum tag_types tag_type;
11941
11942 /* Look for the class-key. */
11943 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11944 if (!token)
11945 return none_type;
11946
11947 /* Check to see if the TOKEN is a class-key. */
11948 tag_type = cp_parser_token_is_class_key (token);
11949 if (!tag_type)
11950 cp_parser_error (parser, "expected class-key");
11951 return tag_type;
11952}
11953
11954/* Parse an (optional) member-specification.
11955
11956 member-specification:
11957 member-declaration member-specification [opt]
11958 access-specifier : member-specification [opt] */
11959
11960static void
11961cp_parser_member_specification_opt (parser)
11962 cp_parser *parser;
11963{
11964 while (true)
11965 {
11966 cp_token *token;
11967 enum rid keyword;
11968
11969 /* Peek at the next token. */
11970 token = cp_lexer_peek_token (parser->lexer);
11971 /* If it's a `}', or EOF then we've seen all the members. */
11972 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11973 break;
11974
11975 /* See if this token is a keyword. */
11976 keyword = token->keyword;
11977 switch (keyword)
11978 {
11979 case RID_PUBLIC:
11980 case RID_PROTECTED:
11981 case RID_PRIVATE:
11982 /* Consume the access-specifier. */
11983 cp_lexer_consume_token (parser->lexer);
11984 /* Remember which access-specifier is active. */
11985 current_access_specifier = token->value;
11986 /* Look for the `:'. */
11987 cp_parser_require (parser, CPP_COLON, "`:'");
11988 break;
11989
11990 default:
11991 /* Otherwise, the next construction must be a
11992 member-declaration. */
11993 cp_parser_member_declaration (parser);
11994 reset_type_access_control ();
11995 }
11996 }
11997}
11998
11999/* Parse a member-declaration.
12000
12001 member-declaration:
12002 decl-specifier-seq [opt] member-declarator-list [opt] ;
12003 function-definition ; [opt]
12004 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12005 using-declaration
12006 template-declaration
12007
12008 member-declarator-list:
12009 member-declarator
12010 member-declarator-list , member-declarator
12011
12012 member-declarator:
12013 declarator pure-specifier [opt]
12014 declarator constant-initializer [opt]
12015 identifier [opt] : constant-expression
12016
12017 GNU Extensions:
12018
12019 member-declaration:
12020 __extension__ member-declaration
12021
12022 member-declarator:
12023 declarator attributes [opt] pure-specifier [opt]
12024 declarator attributes [opt] constant-initializer [opt]
12025 identifier [opt] attributes [opt] : constant-expression */
12026
12027static void
12028cp_parser_member_declaration (parser)
12029 cp_parser *parser;
12030{
12031 tree decl_specifiers;
12032 tree prefix_attributes;
12033 tree decl;
12034 bool declares_class_or_enum;
12035 bool friend_p;
12036 cp_token *token;
12037 int saved_pedantic;
12038
12039 /* Check for the `__extension__' keyword. */
12040 if (cp_parser_extension_opt (parser, &saved_pedantic))
12041 {
12042 /* Recurse. */
12043 cp_parser_member_declaration (parser);
12044 /* Restore the old value of the PEDANTIC flag. */
12045 pedantic = saved_pedantic;
12046
12047 return;
12048 }
12049
12050 /* Check for a template-declaration. */
12051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12052 {
12053 /* Parse the template-declaration. */
12054 cp_parser_template_declaration (parser, /*member_p=*/true);
12055
12056 return;
12057 }
12058
12059 /* Check for a using-declaration. */
12060 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12061 {
12062 /* Parse the using-declaration. */
12063 cp_parser_using_declaration (parser);
12064
12065 return;
12066 }
12067
12068 /* We can't tell whether we're looking at a declaration or a
12069 function-definition. */
12070 cp_parser_parse_tentatively (parser);
12071
12072 /* Parse the decl-specifier-seq. */
12073 decl_specifiers
12074 = cp_parser_decl_specifier_seq (parser,
12075 CP_PARSER_FLAGS_OPTIONAL,
12076 &prefix_attributes,
12077 &declares_class_or_enum);
12078 /* If there is no declarator, then the decl-specifier-seq should
12079 specify a type. */
12080 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12081 {
12082 /* If there was no decl-specifier-seq, and the next token is a
12083 `;', then we have something like:
12084
12085 struct S { ; };
12086
12087 [class.mem]
12088
12089 Each member-declaration shall declare at least one member
12090 name of the class. */
12091 if (!decl_specifiers)
12092 {
12093 if (pedantic)
12094 pedwarn ("extra semicolon");
12095 }
12096 else
12097 {
12098 tree type;
12099
12100 /* See if this declaration is a friend. */
12101 friend_p = cp_parser_friend_p (decl_specifiers);
12102 /* If there were decl-specifiers, check to see if there was
12103 a class-declaration. */
12104 type = check_tag_decl (decl_specifiers);
12105 /* Nested classes have already been added to the class, but
12106 a `friend' needs to be explicitly registered. */
12107 if (friend_p)
12108 {
12109 /* If the `friend' keyword was present, the friend must
12110 be introduced with a class-key. */
12111 if (!declares_class_or_enum)
12112 error ("a class-key must be used when declaring a friend");
12113 /* In this case:
12114
12115 template <typename T> struct A {
12116 friend struct A<T>::B;
12117 };
12118
12119 A<T>::B will be represented by a TYPENAME_TYPE, and
12120 therefore not recognized by check_tag_decl. */
12121 if (!type)
12122 {
12123 tree specifier;
12124
12125 for (specifier = decl_specifiers;
12126 specifier;
12127 specifier = TREE_CHAIN (specifier))
12128 {
12129 tree s = TREE_VALUE (specifier);
12130
12131 if (TREE_CODE (s) == IDENTIFIER_NODE
12132 && IDENTIFIER_GLOBAL_VALUE (s))
12133 type = IDENTIFIER_GLOBAL_VALUE (s);
12134 if (TREE_CODE (s) == TYPE_DECL)
12135 s = TREE_TYPE (s);
12136 if (TYPE_P (s))
12137 {
12138 type = s;
12139 break;
12140 }
12141 }
12142 }
12143 if (!type)
12144 error ("friend declaration does not name a class or "
12145 "function");
12146 else
12147 make_friend_class (current_class_type, type);
12148 }
12149 /* If there is no TYPE, an error message will already have
12150 been issued. */
12151 else if (!type)
12152 ;
12153 /* An anonymous aggregate has to be handled specially; such
12154 a declaration really declares a data member (with a
12155 particular type), as opposed to a nested class. */
12156 else if (ANON_AGGR_TYPE_P (type))
12157 {
12158 /* Remove constructors and such from TYPE, now that we
12159 know it is an anoymous aggregate. */
12160 fixup_anonymous_aggr (type);
12161 /* And make the corresponding data member. */
12162 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12163 /* Add it to the class. */
12164 finish_member_declaration (decl);
12165 }
12166 }
12167 }
12168 else
12169 {
12170 /* See if these declarations will be friends. */
12171 friend_p = cp_parser_friend_p (decl_specifiers);
12172
12173 /* Keep going until we hit the `;' at the end of the
12174 declaration. */
12175 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12176 {
12177 tree attributes = NULL_TREE;
12178 tree first_attribute;
12179
12180 /* Peek at the next token. */
12181 token = cp_lexer_peek_token (parser->lexer);
12182
12183 /* Check for a bitfield declaration. */
12184 if (token->type == CPP_COLON
12185 || (token->type == CPP_NAME
12186 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12187 == CPP_COLON))
12188 {
12189 tree identifier;
12190 tree width;
12191
12192 /* Get the name of the bitfield. Note that we cannot just
12193 check TOKEN here because it may have been invalidated by
12194 the call to cp_lexer_peek_nth_token above. */
12195 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12196 identifier = cp_parser_identifier (parser);
12197 else
12198 identifier = NULL_TREE;
12199
12200 /* Consume the `:' token. */
12201 cp_lexer_consume_token (parser->lexer);
12202 /* Get the width of the bitfield. */
12203 width = cp_parser_constant_expression (parser);
12204
12205 /* Look for attributes that apply to the bitfield. */
12206 attributes = cp_parser_attributes_opt (parser);
12207 /* Remember which attributes are prefix attributes and
12208 which are not. */
12209 first_attribute = attributes;
12210 /* Combine the attributes. */
12211 attributes = chainon (prefix_attributes, attributes);
12212
12213 /* Create the bitfield declaration. */
12214 decl = grokbitfield (identifier,
12215 decl_specifiers,
12216 width);
12217 /* Apply the attributes. */
12218 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12219 }
12220 else
12221 {
12222 tree declarator;
12223 tree initializer;
12224 tree asm_specification;
12225 bool ctor_dtor_or_conv_p;
12226
12227 /* Parse the declarator. */
12228 declarator
12229 = cp_parser_declarator (parser,
12230 /*abstract_p=*/false,
12231 &ctor_dtor_or_conv_p);
12232
12233 /* If something went wrong parsing the declarator, make sure
12234 that we at least consume some tokens. */
12235 if (declarator == error_mark_node)
12236 {
12237 /* Skip to the end of the statement. */
12238 cp_parser_skip_to_end_of_statement (parser);
12239 break;
12240 }
12241
12242 /* Look for an asm-specification. */
12243 asm_specification = cp_parser_asm_specification_opt (parser);
12244 /* Look for attributes that apply to the declaration. */
12245 attributes = cp_parser_attributes_opt (parser);
12246 /* Remember which attributes are prefix attributes and
12247 which are not. */
12248 first_attribute = attributes;
12249 /* Combine the attributes. */
12250 attributes = chainon (prefix_attributes, attributes);
12251
12252 /* If it's an `=', then we have a constant-initializer or a
12253 pure-specifier. It is not correct to parse the
12254 initializer before registering the member declaration
12255 since the member declaration should be in scope while
12256 its initializer is processed. However, the rest of the
12257 front end does not yet provide an interface that allows
12258 us to handle this correctly. */
12259 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12260 {
12261 /* In [class.mem]:
12262
12263 A pure-specifier shall be used only in the declaration of
12264 a virtual function.
12265
12266 A member-declarator can contain a constant-initializer
12267 only if it declares a static member of integral or
12268 enumeration type.
12269
12270 Therefore, if the DECLARATOR is for a function, we look
12271 for a pure-specifier; otherwise, we look for a
12272 constant-initializer. When we call `grokfield', it will
12273 perform more stringent semantics checks. */
12274 if (TREE_CODE (declarator) == CALL_EXPR)
12275 initializer = cp_parser_pure_specifier (parser);
12276 else
12277 {
12278 /* This declaration cannot be a function
12279 definition. */
12280 cp_parser_commit_to_tentative_parse (parser);
12281 /* Parse the initializer. */
12282 initializer = cp_parser_constant_initializer (parser);
12283 }
12284 }
12285 /* Otherwise, there is no initializer. */
12286 else
12287 initializer = NULL_TREE;
12288
12289 /* See if we are probably looking at a function
12290 definition. We are certainly not looking at at a
12291 member-declarator. Calling `grokfield' has
12292 side-effects, so we must not do it unless we are sure
12293 that we are looking at a member-declarator. */
12294 if (cp_parser_token_starts_function_definition_p
12295 (cp_lexer_peek_token (parser->lexer)))
12296 decl = error_mark_node;
12297 else
12298 /* Create the declaration. */
12299 decl = grokfield (declarator,
12300 decl_specifiers,
12301 initializer,
12302 asm_specification,
12303 attributes);
12304 }
12305
12306 /* Reset PREFIX_ATTRIBUTES. */
12307 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12308 attributes = TREE_CHAIN (attributes);
12309 if (attributes)
12310 TREE_CHAIN (attributes) = NULL_TREE;
12311
12312 /* If there is any qualification still in effect, clear it
12313 now; we will be starting fresh with the next declarator. */
12314 parser->scope = NULL_TREE;
12315 parser->qualifying_scope = NULL_TREE;
12316 parser->object_scope = NULL_TREE;
12317 /* If it's a `,', then there are more declarators. */
12318 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12319 cp_lexer_consume_token (parser->lexer);
12320 /* If the next token isn't a `;', then we have a parse error. */
12321 else if (cp_lexer_next_token_is_not (parser->lexer,
12322 CPP_SEMICOLON))
12323 {
12324 cp_parser_error (parser, "expected `;'");
12325 /* Skip tokens until we find a `;' */
12326 cp_parser_skip_to_end_of_statement (parser);
12327
12328 break;
12329 }
12330
12331 if (decl)
12332 {
12333 /* Add DECL to the list of members. */
12334 if (!friend_p)
12335 finish_member_declaration (decl);
12336
12337 /* If DECL is a function, we must return
12338 to parse it later. (Even though there is no definition,
12339 there might be default arguments that need handling.) */
12340 if (TREE_CODE (decl) == FUNCTION_DECL)
12341 TREE_VALUE (parser->unparsed_functions_queues)
12342 = tree_cons (current_class_type, decl,
12343 TREE_VALUE (parser->unparsed_functions_queues));
12344 }
12345 }
12346 }
12347
12348 /* If everything went well, look for the `;'. */
12349 if (cp_parser_parse_definitely (parser))
12350 {
12351 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12352 return;
12353 }
12354
12355 /* Parse the function-definition. */
12356 decl = cp_parser_function_definition (parser, &friend_p);
12357 /* If the member was not a friend, declare it here. */
12358 if (!friend_p)
12359 finish_member_declaration (decl);
12360 /* Peek at the next token. */
12361 token = cp_lexer_peek_token (parser->lexer);
12362 /* If the next token is a semicolon, consume it. */
12363 if (token->type == CPP_SEMICOLON)
12364 cp_lexer_consume_token (parser->lexer);
12365}
12366
12367/* Parse a pure-specifier.
12368
12369 pure-specifier:
12370 = 0
12371
12372 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12373 Otherwiser, ERROR_MARK_NODE is returned. */
12374
12375static tree
12376cp_parser_pure_specifier (parser)
12377 cp_parser *parser;
12378{
12379 cp_token *token;
12380
12381 /* Look for the `=' token. */
12382 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12383 return error_mark_node;
12384 /* Look for the `0' token. */
12385 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12386 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12387 to get information from the lexer about how the number was
12388 spelled in order to fix this problem. */
12389 if (!token || !integer_zerop (token->value))
12390 return error_mark_node;
12391
12392 return integer_zero_node;
12393}
12394
12395/* Parse a constant-initializer.
12396
12397 constant-initializer:
12398 = constant-expression
12399
12400 Returns a representation of the constant-expression. */
12401
12402static tree
12403cp_parser_constant_initializer (parser)
12404 cp_parser *parser;
12405{
12406 /* Look for the `=' token. */
12407 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12408 return error_mark_node;
12409
12410 /* It is invalid to write:
12411
12412 struct S { static const int i = { 7 }; };
12413
12414 */
12415 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12416 {
12417 cp_parser_error (parser,
12418 "a brace-enclosed initializer is not allowed here");
12419 /* Consume the opening brace. */
12420 cp_lexer_consume_token (parser->lexer);
12421 /* Skip the initializer. */
12422 cp_parser_skip_to_closing_brace (parser);
12423 /* Look for the trailing `}'. */
12424 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12425
12426 return error_mark_node;
12427 }
12428
12429 return cp_parser_constant_expression (parser);
12430}
12431
12432/* Derived classes [gram.class.derived] */
12433
12434/* Parse a base-clause.
12435
12436 base-clause:
12437 : base-specifier-list
12438
12439 base-specifier-list:
12440 base-specifier
12441 base-specifier-list , base-specifier
12442
12443 Returns a TREE_LIST representing the base-classes, in the order in
12444 which they were declared. The representation of each node is as
12445 described by cp_parser_base_specifier.
12446
12447 In the case that no bases are specified, this function will return
12448 NULL_TREE, not ERROR_MARK_NODE. */
12449
12450static tree
12451cp_parser_base_clause (parser)
12452 cp_parser *parser;
12453{
12454 tree bases = NULL_TREE;
12455
12456 /* Look for the `:' that begins the list. */
12457 cp_parser_require (parser, CPP_COLON, "`:'");
12458
12459 /* Scan the base-specifier-list. */
12460 while (true)
12461 {
12462 cp_token *token;
12463 tree base;
12464
12465 /* Look for the base-specifier. */
12466 base = cp_parser_base_specifier (parser);
12467 /* Add BASE to the front of the list. */
12468 if (base != error_mark_node)
12469 {
12470 TREE_CHAIN (base) = bases;
12471 bases = base;
12472 }
12473 /* Peek at the next token. */
12474 token = cp_lexer_peek_token (parser->lexer);
12475 /* If it's not a comma, then the list is complete. */
12476 if (token->type != CPP_COMMA)
12477 break;
12478 /* Consume the `,'. */
12479 cp_lexer_consume_token (parser->lexer);
12480 }
12481
12482 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12483 base class had a qualified name. However, the next name that
12484 appears is certainly not qualified. */
12485 parser->scope = NULL_TREE;
12486 parser->qualifying_scope = NULL_TREE;
12487 parser->object_scope = NULL_TREE;
12488
12489 return nreverse (bases);
12490}
12491
12492/* Parse a base-specifier.
12493
12494 base-specifier:
12495 :: [opt] nested-name-specifier [opt] class-name
12496 virtual access-specifier [opt] :: [opt] nested-name-specifier
12497 [opt] class-name
12498 access-specifier virtual [opt] :: [opt] nested-name-specifier
12499 [opt] class-name
12500
12501 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12502 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12503 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12504 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12505
12506static tree
12507cp_parser_base_specifier (parser)
12508 cp_parser *parser;
12509{
12510 cp_token *token;
12511 bool done = false;
12512 bool virtual_p = false;
12513 bool duplicate_virtual_error_issued_p = false;
12514 bool duplicate_access_error_issued_p = false;
12515 bool class_scope_p;
12516 access_kind access = ak_none;
12517 tree access_node;
12518 tree type;
12519
12520 /* Process the optional `virtual' and `access-specifier'. */
12521 while (!done)
12522 {
12523 /* Peek at the next token. */
12524 token = cp_lexer_peek_token (parser->lexer);
12525 /* Process `virtual'. */
12526 switch (token->keyword)
12527 {
12528 case RID_VIRTUAL:
12529 /* If `virtual' appears more than once, issue an error. */
12530 if (virtual_p && !duplicate_virtual_error_issued_p)
12531 {
12532 cp_parser_error (parser,
12533 "`virtual' specified more than once in base-specified");
12534 duplicate_virtual_error_issued_p = true;
12535 }
12536
12537 virtual_p = true;
12538
12539 /* Consume the `virtual' token. */
12540 cp_lexer_consume_token (parser->lexer);
12541
12542 break;
12543
12544 case RID_PUBLIC:
12545 case RID_PROTECTED:
12546 case RID_PRIVATE:
12547 /* If more than one access specifier appears, issue an
12548 error. */
12549 if (access != ak_none && !duplicate_access_error_issued_p)
12550 {
12551 cp_parser_error (parser,
12552 "more than one access specifier in base-specified");
12553 duplicate_access_error_issued_p = true;
12554 }
12555
12556 access = ((access_kind)
12557 tree_low_cst (ridpointers[(int) token->keyword],
12558 /*pos=*/1));
12559
12560 /* Consume the access-specifier. */
12561 cp_lexer_consume_token (parser->lexer);
12562
12563 break;
12564
12565 default:
12566 done = true;
12567 break;
12568 }
12569 }
12570
12571 /* Map `virtual_p' and `access' onto one of the access
12572 tree-nodes. */
12573 if (!virtual_p)
12574 switch (access)
12575 {
12576 case ak_none:
12577 access_node = access_default_node;
12578 break;
12579 case ak_public:
12580 access_node = access_public_node;
12581 break;
12582 case ak_protected:
12583 access_node = access_protected_node;
12584 break;
12585 case ak_private:
12586 access_node = access_private_node;
12587 break;
12588 default:
12589 abort ();
12590 }
12591 else
12592 switch (access)
12593 {
12594 case ak_none:
12595 access_node = access_default_virtual_node;
12596 break;
12597 case ak_public:
12598 access_node = access_public_virtual_node;
12599 break;
12600 case ak_protected:
12601 access_node = access_protected_virtual_node;
12602 break;
12603 case ak_private:
12604 access_node = access_private_virtual_node;
12605 break;
12606 default:
12607 abort ();
12608 }
12609
12610 /* Look for the optional `::' operator. */
12611 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12612 /* Look for the nested-name-specifier. The simplest way to
12613 implement:
12614
12615 [temp.res]
12616
12617 The keyword `typename' is not permitted in a base-specifier or
12618 mem-initializer; in these contexts a qualified name that
12619 depends on a template-parameter is implicitly assumed to be a
12620 type name.
12621
12622 is to pretend that we have seen the `typename' keyword at this
12623 point. */
12624 cp_parser_nested_name_specifier_opt (parser,
12625 /*typename_keyword_p=*/true,
12626 /*check_dependency_p=*/true,
12627 /*type_p=*/true);
12628 /* If the base class is given by a qualified name, assume that names
12629 we see are type names or templates, as appropriate. */
12630 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12631 /* Finally, look for the class-name. */
12632 type = cp_parser_class_name (parser,
12633 class_scope_p,
12634 class_scope_p,
12635 /*type_p=*/true,
12636 /*check_access=*/true,
12637 /*check_dependency_p=*/true,
12638 /*class_head_p=*/false);
12639
12640 if (type == error_mark_node)
12641 return error_mark_node;
12642
12643 return finish_base_specifier (access_node, TREE_TYPE (type));
12644}
12645
12646/* Exception handling [gram.exception] */
12647
12648/* Parse an (optional) exception-specification.
12649
12650 exception-specification:
12651 throw ( type-id-list [opt] )
12652
12653 Returns a TREE_LIST representing the exception-specification. The
12654 TREE_VALUE of each node is a type. */
12655
12656static tree
12657cp_parser_exception_specification_opt (parser)
12658 cp_parser *parser;
12659{
12660 cp_token *token;
12661 tree type_id_list;
12662
12663 /* Peek at the next token. */
12664 token = cp_lexer_peek_token (parser->lexer);
12665 /* If it's not `throw', then there's no exception-specification. */
12666 if (!cp_parser_is_keyword (token, RID_THROW))
12667 return NULL_TREE;
12668
12669 /* Consume the `throw'. */
12670 cp_lexer_consume_token (parser->lexer);
12671
12672 /* Look for the `('. */
12673 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12674
12675 /* Peek at the next token. */
12676 token = cp_lexer_peek_token (parser->lexer);
12677 /* If it's not a `)', then there is a type-id-list. */
12678 if (token->type != CPP_CLOSE_PAREN)
12679 {
12680 const char *saved_message;
12681
12682 /* Types may not be defined in an exception-specification. */
12683 saved_message = parser->type_definition_forbidden_message;
12684 parser->type_definition_forbidden_message
12685 = "types may not be defined in an exception-specification";
12686 /* Parse the type-id-list. */
12687 type_id_list = cp_parser_type_id_list (parser);
12688 /* Restore the saved message. */
12689 parser->type_definition_forbidden_message = saved_message;
12690 }
12691 else
12692 type_id_list = empty_except_spec;
12693
12694 /* Look for the `)'. */
12695 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12696
12697 return type_id_list;
12698}
12699
12700/* Parse an (optional) type-id-list.
12701
12702 type-id-list:
12703 type-id
12704 type-id-list , type-id
12705
12706 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12707 in the order that the types were presented. */
12708
12709static tree
12710cp_parser_type_id_list (parser)
12711 cp_parser *parser;
12712{
12713 tree types = NULL_TREE;
12714
12715 while (true)
12716 {
12717 cp_token *token;
12718 tree type;
12719
12720 /* Get the next type-id. */
12721 type = cp_parser_type_id (parser);
12722 /* Add it to the list. */
12723 types = add_exception_specifier (types, type, /*complain=*/1);
12724 /* Peek at the next token. */
12725 token = cp_lexer_peek_token (parser->lexer);
12726 /* If it is not a `,', we are done. */
12727 if (token->type != CPP_COMMA)
12728 break;
12729 /* Consume the `,'. */
12730 cp_lexer_consume_token (parser->lexer);
12731 }
12732
12733 return nreverse (types);
12734}
12735
12736/* Parse a try-block.
12737
12738 try-block:
12739 try compound-statement handler-seq */
12740
12741static tree
12742cp_parser_try_block (parser)
12743 cp_parser *parser;
12744{
12745 tree try_block;
12746
12747 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12748 try_block = begin_try_block ();
12749 cp_parser_compound_statement (parser);
12750 finish_try_block (try_block);
12751 cp_parser_handler_seq (parser);
12752 finish_handler_sequence (try_block);
12753
12754 return try_block;
12755}
12756
12757/* Parse a function-try-block.
12758
12759 function-try-block:
12760 try ctor-initializer [opt] function-body handler-seq */
12761
12762static bool
12763cp_parser_function_try_block (parser)
12764 cp_parser *parser;
12765{
12766 tree try_block;
12767 bool ctor_initializer_p;
12768
12769 /* Look for the `try' keyword. */
12770 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12771 return false;
12772 /* Let the rest of the front-end know where we are. */
12773 try_block = begin_function_try_block ();
12774 /* Parse the function-body. */
12775 ctor_initializer_p
12776 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12777 /* We're done with the `try' part. */
12778 finish_function_try_block (try_block);
12779 /* Parse the handlers. */
12780 cp_parser_handler_seq (parser);
12781 /* We're done with the handlers. */
12782 finish_function_handler_sequence (try_block);
12783
12784 return ctor_initializer_p;
12785}
12786
12787/* Parse a handler-seq.
12788
12789 handler-seq:
12790 handler handler-seq [opt] */
12791
12792static void
12793cp_parser_handler_seq (parser)
12794 cp_parser *parser;
12795{
12796 while (true)
12797 {
12798 cp_token *token;
12799
12800 /* Parse the handler. */
12801 cp_parser_handler (parser);
12802 /* Peek at the next token. */
12803 token = cp_lexer_peek_token (parser->lexer);
12804 /* If it's not `catch' then there are no more handlers. */
12805 if (!cp_parser_is_keyword (token, RID_CATCH))
12806 break;
12807 }
12808}
12809
12810/* Parse a handler.
12811
12812 handler:
12813 catch ( exception-declaration ) compound-statement */
12814
12815static void
12816cp_parser_handler (parser)
12817 cp_parser *parser;
12818{
12819 tree handler;
12820 tree declaration;
12821
12822 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12823 handler = begin_handler ();
12824 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12825 declaration = cp_parser_exception_declaration (parser);
12826 finish_handler_parms (declaration, handler);
12827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12828 cp_parser_compound_statement (parser);
12829 finish_handler (handler);
12830}
12831
12832/* Parse an exception-declaration.
12833
12834 exception-declaration:
12835 type-specifier-seq declarator
12836 type-specifier-seq abstract-declarator
12837 type-specifier-seq
12838 ...
12839
12840 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12841 ellipsis variant is used. */
12842
12843static tree
12844cp_parser_exception_declaration (parser)
12845 cp_parser *parser;
12846{
12847 tree type_specifiers;
12848 tree declarator;
12849 const char *saved_message;
12850
12851 /* If it's an ellipsis, it's easy to handle. */
12852 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12853 {
12854 /* Consume the `...' token. */
12855 cp_lexer_consume_token (parser->lexer);
12856 return NULL_TREE;
12857 }
12858
12859 /* Types may not be defined in exception-declarations. */
12860 saved_message = parser->type_definition_forbidden_message;
12861 parser->type_definition_forbidden_message
12862 = "types may not be defined in exception-declarations";
12863
12864 /* Parse the type-specifier-seq. */
12865 type_specifiers = cp_parser_type_specifier_seq (parser);
12866 /* If it's a `)', then there is no declarator. */
12867 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12868 declarator = NULL_TREE;
12869 else
12870 {
12871 /* Otherwise, we can't be sure whether we are looking at a
12872 direct, or an abstract, declarator. */
12873 cp_parser_parse_tentatively (parser);
12874 /* Try an ordinary declarator. */
12875 declarator = cp_parser_declarator (parser,
12876 /*abstract_p=*/false,
12877 /*ctor_dtor_or_conv_p=*/NULL);
12878 /* If that didn't work, try an abstract declarator. */
12879 if (!cp_parser_parse_definitely (parser))
12880 declarator = cp_parser_declarator (parser,
12881 /*abstract_p=*/true,
12882 /*ctor_dtor_or_conv_p=*/NULL);
12883 }
12884
12885 /* Restore the saved message. */
12886 parser->type_definition_forbidden_message = saved_message;
12887
12888 return start_handler_parms (type_specifiers, declarator);
12889}
12890
12891/* Parse a throw-expression.
12892
12893 throw-expression:
12894 throw assignment-expresion [opt]
12895
12896 Returns a THROW_EXPR representing the throw-expression. */
12897
12898static tree
12899cp_parser_throw_expression (parser)
12900 cp_parser *parser;
12901{
12902 tree expression;
12903
12904 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12905 /* We can't be sure if there is an assignment-expression or not. */
12906 cp_parser_parse_tentatively (parser);
12907 /* Try it. */
12908 expression = cp_parser_assignment_expression (parser);
12909 /* If it didn't work, this is just a rethrow. */
12910 if (!cp_parser_parse_definitely (parser))
12911 expression = NULL_TREE;
12912
12913 return build_throw (expression);
12914}
12915
12916/* GNU Extensions */
12917
12918/* Parse an (optional) asm-specification.
12919
12920 asm-specification:
12921 asm ( string-literal )
12922
12923 If the asm-specification is present, returns a STRING_CST
12924 corresponding to the string-literal. Otherwise, returns
12925 NULL_TREE. */
12926
12927static tree
12928cp_parser_asm_specification_opt (parser)
12929 cp_parser *parser;
12930{
12931 cp_token *token;
12932 tree asm_specification;
12933
12934 /* Peek at the next token. */
12935 token = cp_lexer_peek_token (parser->lexer);
12936 /* If the next token isn't the `asm' keyword, then there's no
12937 asm-specification. */
12938 if (!cp_parser_is_keyword (token, RID_ASM))
12939 return NULL_TREE;
12940
12941 /* Consume the `asm' token. */
12942 cp_lexer_consume_token (parser->lexer);
12943 /* Look for the `('. */
12944 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12945
12946 /* Look for the string-literal. */
12947 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12948 if (token)
12949 asm_specification = token->value;
12950 else
12951 asm_specification = NULL_TREE;
12952
12953 /* Look for the `)'. */
12954 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12955
12956 return asm_specification;
12957}
12958
12959/* Parse an asm-operand-list.
12960
12961 asm-operand-list:
12962 asm-operand
12963 asm-operand-list , asm-operand
12964
12965 asm-operand:
12966 string-literal ( expression )
12967 [ string-literal ] string-literal ( expression )
12968
12969 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12970 each node is the expression. The TREE_PURPOSE is itself a
12971 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12972 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12973 is a STRING_CST for the string literal before the parenthesis. */
12974
12975static tree
12976cp_parser_asm_operand_list (parser)
12977 cp_parser *parser;
12978{
12979 tree asm_operands = NULL_TREE;
12980
12981 while (true)
12982 {
12983 tree string_literal;
12984 tree expression;
12985 tree name;
12986 cp_token *token;
12987
12988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12989 {
12990 /* Consume the `[' token. */
12991 cp_lexer_consume_token (parser->lexer);
12992 /* Read the operand name. */
12993 name = cp_parser_identifier (parser);
12994 if (name != error_mark_node)
12995 name = build_string (IDENTIFIER_LENGTH (name),
12996 IDENTIFIER_POINTER (name));
12997 /* Look for the closing `]'. */
12998 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12999 }
13000 else
13001 name = NULL_TREE;
13002 /* Look for the string-literal. */
13003 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13004 string_literal = token ? token->value : error_mark_node;
13005 /* Look for the `('. */
13006 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13007 /* Parse the expression. */
13008 expression = cp_parser_expression (parser);
13009 /* Look for the `)'. */
13010 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13011 /* Add this operand to the list. */
13012 asm_operands = tree_cons (build_tree_list (name, string_literal),
13013 expression,
13014 asm_operands);
13015 /* If the next token is not a `,', there are no more
13016 operands. */
13017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13018 break;
13019 /* Consume the `,'. */
13020 cp_lexer_consume_token (parser->lexer);
13021 }
13022
13023 return nreverse (asm_operands);
13024}
13025
13026/* Parse an asm-clobber-list.
13027
13028 asm-clobber-list:
13029 string-literal
13030 asm-clobber-list , string-literal
13031
13032 Returns a TREE_LIST, indicating the clobbers in the order that they
13033 appeared. The TREE_VALUE of each node is a STRING_CST. */
13034
13035static tree
13036cp_parser_asm_clobber_list (parser)
13037 cp_parser *parser;
13038{
13039 tree clobbers = NULL_TREE;
13040
13041 while (true)
13042 {
13043 cp_token *token;
13044 tree string_literal;
13045
13046 /* Look for the string literal. */
13047 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13048 string_literal = token ? token->value : error_mark_node;
13049 /* Add it to the list. */
13050 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13051 /* If the next token is not a `,', then the list is
13052 complete. */
13053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13054 break;
13055 /* Consume the `,' token. */
13056 cp_lexer_consume_token (parser->lexer);
13057 }
13058
13059 return clobbers;
13060}
13061
13062/* Parse an (optional) series of attributes.
13063
13064 attributes:
13065 attributes attribute
13066
13067 attribute:
13068 __attribute__ (( attribute-list [opt] ))
13069
13070 The return value is as for cp_parser_attribute_list. */
13071
13072static tree
13073cp_parser_attributes_opt (parser)
13074 cp_parser *parser;
13075{
13076 tree attributes = NULL_TREE;
13077
13078 while (true)
13079 {
13080 cp_token *token;
13081 tree attribute_list;
13082
13083 /* Peek at the next token. */
13084 token = cp_lexer_peek_token (parser->lexer);
13085 /* If it's not `__attribute__', then we're done. */
13086 if (token->keyword != RID_ATTRIBUTE)
13087 break;
13088
13089 /* Consume the `__attribute__' keyword. */
13090 cp_lexer_consume_token (parser->lexer);
13091 /* Look for the two `(' tokens. */
13092 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13093 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13094
13095 /* Peek at the next token. */
13096 token = cp_lexer_peek_token (parser->lexer);
13097 if (token->type != CPP_CLOSE_PAREN)
13098 /* Parse the attribute-list. */
13099 attribute_list = cp_parser_attribute_list (parser);
13100 else
13101 /* If the next token is a `)', then there is no attribute
13102 list. */
13103 attribute_list = NULL;
13104
13105 /* Look for the two `)' tokens. */
13106 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13107 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13108
13109 /* Add these new attributes to the list. */
13110 attributes = chainon (attributes, attribute_list);
13111 }
13112
13113 return attributes;
13114}
13115
13116/* Parse an attribute-list.
13117
13118 attribute-list:
13119 attribute
13120 attribute-list , attribute
13121
13122 attribute:
13123 identifier
13124 identifier ( identifier )
13125 identifier ( identifier , expression-list )
13126 identifier ( expression-list )
13127
13128 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13129 TREE_PURPOSE of each node is the identifier indicating which
13130 attribute is in use. The TREE_VALUE represents the arguments, if
13131 any. */
13132
13133static tree
13134cp_parser_attribute_list (parser)
13135 cp_parser *parser;
13136{
13137 tree attribute_list = NULL_TREE;
13138
13139 while (true)
13140 {
13141 cp_token *token;
13142 tree identifier;
13143 tree attribute;
13144
13145 /* Look for the identifier. We also allow keywords here; for
13146 example `__attribute__ ((const))' is legal. */
13147 token = cp_lexer_peek_token (parser->lexer);
13148 if (token->type != CPP_NAME
13149 && token->type != CPP_KEYWORD)
13150 return error_mark_node;
13151 /* Consume the token. */
13152 token = cp_lexer_consume_token (parser->lexer);
13153
13154 /* Save away the identifier that indicates which attribute this is. */
13155 identifier = token->value;
13156 attribute = build_tree_list (identifier, NULL_TREE);
13157
13158 /* Peek at the next token. */
13159 token = cp_lexer_peek_token (parser->lexer);
13160 /* If it's an `(', then parse the attribute arguments. */
13161 if (token->type == CPP_OPEN_PAREN)
13162 {
13163 tree arguments;
13164 int arguments_allowed_p = 1;
13165
13166 /* Consume the `('. */
13167 cp_lexer_consume_token (parser->lexer);
13168 /* Peek at the next token. */
13169 token = cp_lexer_peek_token (parser->lexer);
13170 /* Check to see if the next token is an identifier. */
13171 if (token->type == CPP_NAME)
13172 {
13173 /* Save the identifier. */
13174 identifier = token->value;
13175 /* Consume the identifier. */
13176 cp_lexer_consume_token (parser->lexer);
13177 /* Peek at the next token. */
13178 token = cp_lexer_peek_token (parser->lexer);
13179 /* If the next token is a `,', then there are some other
13180 expressions as well. */
13181 if (token->type == CPP_COMMA)
13182 /* Consume the comma. */
13183 cp_lexer_consume_token (parser->lexer);
13184 else
13185 arguments_allowed_p = 0;
13186 }
13187 else
13188 identifier = NULL_TREE;
13189
13190 /* If there are arguments, parse them too. */
13191 if (arguments_allowed_p)
13192 arguments = cp_parser_expression_list (parser);
13193 else
13194 arguments = NULL_TREE;
13195
13196 /* Combine the identifier and the arguments. */
13197 if (identifier)
13198 arguments = tree_cons (NULL_TREE, identifier, arguments);
13199
13200 /* Save the identifier and arguments away. */
13201 TREE_VALUE (attribute) = arguments;
13202
13203 /* Look for the closing `)'. */
13204 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13205 }
13206
13207 /* Add this attribute to the list. */
13208 TREE_CHAIN (attribute) = attribute_list;
13209 attribute_list = attribute;
13210
13211 /* Now, look for more attributes. */
13212 token = cp_lexer_peek_token (parser->lexer);
13213 /* If the next token isn't a `,', we're done. */
13214 if (token->type != CPP_COMMA)
13215 break;
13216
13217 /* Consume the commma and keep going. */
13218 cp_lexer_consume_token (parser->lexer);
13219 }
13220
13221 /* We built up the list in reverse order. */
13222 return nreverse (attribute_list);
13223}
13224
13225/* Parse an optional `__extension__' keyword. Returns TRUE if it is
13226 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13227 current value of the PEDANTIC flag, regardless of whether or not
13228 the `__extension__' keyword is present. The caller is responsible
13229 for restoring the value of the PEDANTIC flag. */
13230
13231static bool
13232cp_parser_extension_opt (parser, saved_pedantic)
13233 cp_parser *parser;
13234 int *saved_pedantic;
13235{
13236 /* Save the old value of the PEDANTIC flag. */
13237 *saved_pedantic = pedantic;
13238
13239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13240 {
13241 /* Consume the `__extension__' token. */
13242 cp_lexer_consume_token (parser->lexer);
13243 /* We're not being pedantic while the `__extension__' keyword is
13244 in effect. */
13245 pedantic = 0;
13246
13247 return true;
13248 }
13249
13250 return false;
13251}
13252
13253/* Parse a label declaration.
13254
13255 label-declaration:
13256 __label__ label-declarator-seq ;
13257
13258 label-declarator-seq:
13259 identifier , label-declarator-seq
13260 identifier */
13261
13262static void
13263cp_parser_label_declaration (parser)
13264 cp_parser *parser;
13265{
13266 /* Look for the `__label__' keyword. */
13267 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13268
13269 while (true)
13270 {
13271 tree identifier;
13272
13273 /* Look for an identifier. */
13274 identifier = cp_parser_identifier (parser);
13275 /* Declare it as a lobel. */
13276 finish_label_decl (identifier);
13277 /* If the next token is a `;', stop. */
13278 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13279 break;
13280 /* Look for the `,' separating the label declarations. */
13281 cp_parser_require (parser, CPP_COMMA, "`,'");
13282 }
13283
13284 /* Look for the final `;'. */
13285 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13286}
13287
13288/* Support Functions */
13289
13290/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13291 NAME should have one of the representations used for an
13292 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13293 is returned. If PARSER->SCOPE is a dependent type, then a
13294 SCOPE_REF is returned.
13295
13296 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13297 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13298 was formed. Abstractly, such entities should not be passed to this
13299 function, because they do not need to be looked up, but it is
13300 simpler to check for this special case here, rather than at the
13301 call-sites.
13302
13303 In cases not explicitly covered above, this function returns a
13304 DECL, OVERLOAD, or baselink representing the result of the lookup.
13305 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13306 is returned.
13307
13308 If CHECK_ACCESS is TRUE, then access control is performed on the
13309 declaration to which the name resolves, and an error message is
13310 issued if the declaration is inaccessible.
13311
13312 If IS_TYPE is TRUE, bindings that do not refer to types are
13313 ignored.
13314
6fc758aa 13315 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13316 are ignored.
13317
0a3b29ad 13318 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13319 types. */
13320
13321static tree
6fc758aa 13322cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13323 bool is_type, bool is_namespace, bool check_dependency)
0a3b29ad 13324{
13325 tree decl;
13326 tree object_type = parser->context->object_type;
13327
13328 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13329 no longer valid. Note that if we are parsing tentatively, and
13330 the parse fails, OBJECT_TYPE will be automatically restored. */
13331 parser->context->object_type = NULL_TREE;
13332
13333 if (name == error_mark_node)
13334 return error_mark_node;
13335
13336 /* A template-id has already been resolved; there is no lookup to
13337 do. */
13338 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13339 return name;
13340 if (BASELINK_P (name))
13341 {
13342 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13343 == TEMPLATE_ID_EXPR),
13344 20020909);
13345 return name;
13346 }
13347
13348 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13349 it should already have been checked to make sure that the name
13350 used matches the type being destroyed. */
13351 if (TREE_CODE (name) == BIT_NOT_EXPR)
13352 {
13353 tree type;
13354
13355 /* Figure out to which type this destructor applies. */
13356 if (parser->scope)
13357 type = parser->scope;
13358 else if (object_type)
13359 type = object_type;
13360 else
13361 type = current_class_type;
13362 /* If that's not a class type, there is no destructor. */
13363 if (!type || !CLASS_TYPE_P (type))
13364 return error_mark_node;
13365 /* If it was a class type, return the destructor. */
13366 return CLASSTYPE_DESTRUCTORS (type);
13367 }
13368
13369 /* By this point, the NAME should be an ordinary identifier. If
13370 the id-expression was a qualified name, the qualifying scope is
13371 stored in PARSER->SCOPE at this point. */
13372 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13373 20000619);
13374
13375 /* Perform the lookup. */
13376 if (parser->scope)
13377 {
13378 bool dependent_type_p;
13379
13380 if (parser->scope == error_mark_node)
13381 return error_mark_node;
13382
13383 /* If the SCOPE is dependent, the lookup must be deferred until
13384 the template is instantiated -- unless we are explicitly
13385 looking up names in uninstantiated templates. Even then, we
13386 cannot look up the name if the scope is not a class type; it
13387 might, for example, be a template type parameter. */
13388 dependent_type_p = (TYPE_P (parser->scope)
13389 && !(parser->in_declarator_p
13390 && currently_open_class (parser->scope))
13391 && cp_parser_dependent_type_p (parser->scope));
13392 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13393 && dependent_type_p)
13394 {
13395 if (!is_type)
13396 decl = build_nt (SCOPE_REF, parser->scope, name);
13397 else
13398 /* The resolution to Core Issue 180 says that `struct A::B'
13399 should be considered a type-name, even if `A' is
13400 dependent. */
13401 decl = TYPE_NAME (make_typename_type (parser->scope,
13402 name,
13403 /*complain=*/1));
13404 }
13405 else
13406 {
13407 /* If PARSER->SCOPE is a dependent type, then it must be a
13408 class type, and we must not be checking dependencies;
13409 otherwise, we would have processed this lookup above. So
13410 that PARSER->SCOPE is not considered a dependent base by
13411 lookup_member, we must enter the scope here. */
13412 if (dependent_type_p)
13413 push_scope (parser->scope);
13414 /* If the PARSER->SCOPE is a a template specialization, it
13415 may be instantiated during name lookup. In that case,
13416 errors may be issued. Even if we rollback the current
13417 tentative parse, those errors are valid. */
13418 decl = lookup_qualified_name (parser->scope, name, is_type,
13419 /*flags=*/0);
13420 if (dependent_type_p)
13421 pop_scope (parser->scope);
13422 }
13423 parser->qualifying_scope = parser->scope;
13424 parser->object_scope = NULL_TREE;
13425 }
13426 else if (object_type)
13427 {
13428 tree object_decl = NULL_TREE;
13429 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13430 OBJECT_TYPE is not a class. */
13431 if (CLASS_TYPE_P (object_type))
13432 /* If the OBJECT_TYPE is a template specialization, it may
13433 be instantiated during name lookup. In that case, errors
13434 may be issued. Even if we rollback the current tentative
13435 parse, those errors are valid. */
13436 object_decl = lookup_member (object_type,
13437 name,
13438 /*protect=*/0, is_type);
13439 /* Look it up in the enclosing context, too. */
13440 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
6fc758aa 13441 is_namespace,
0a3b29ad 13442 /*flags=*/0);
13443 parser->object_scope = object_type;
13444 parser->qualifying_scope = NULL_TREE;
13445 if (object_decl)
13446 decl = object_decl;
13447 }
13448 else
13449 {
13450 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
6fc758aa 13451 is_namespace,
0a3b29ad 13452 /*flags=*/0);
13453 parser->qualifying_scope = NULL_TREE;
13454 parser->object_scope = NULL_TREE;
13455 }
13456
13457 /* If the lookup failed, let our caller know. */
13458 if (!decl
13459 || decl == error_mark_node
13460 || (TREE_CODE (decl) == FUNCTION_DECL
13461 && DECL_ANTICIPATED (decl)))
13462 return error_mark_node;
13463
13464 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13465 if (TREE_CODE (decl) == TREE_LIST)
13466 {
13467 /* The error message we have to print is too complicated for
13468 cp_parser_error, so we incorporate its actions directly. */
2c593bd0 13469 if (!cp_parser_simulate_error (parser))
0a3b29ad 13470 {
13471 error ("reference to `%D' is ambiguous", name);
13472 print_candidates (decl);
13473 }
13474 return error_mark_node;
13475 }
13476
13477 my_friendly_assert (DECL_P (decl)
13478 || TREE_CODE (decl) == OVERLOAD
13479 || TREE_CODE (decl) == SCOPE_REF
13480 || BASELINK_P (decl),
13481 20000619);
13482
13483 /* If we have resolved the name of a member declaration, check to
13484 see if the declaration is accessible. When the name resolves to
13485 set of overloaded functions, accesibility is checked when
13486 overload resolution is done.
13487
13488 During an explicit instantiation, access is not checked at all,
13489 as per [temp.explicit]. */
13490 if (check_access && scope_chain->check_access && DECL_P (decl))
13491 {
13492 tree qualifying_type;
13493
13494 /* Figure out the type through which DECL is being
13495 accessed. */
13496 qualifying_type
13497 = cp_parser_scope_through_which_access_occurs (decl,
13498 object_type,
13499 parser->scope);
13500 if (qualifying_type)
13501 {
13502 /* If we are supposed to defer access checks, just record
13503 the information for later. */
13504 if (parser->context->deferring_access_checks_p)
13505 cp_parser_defer_access_check (parser, qualifying_type, decl);
13506 /* Otherwise, check accessibility now. */
13507 else
13508 enforce_access (qualifying_type, decl);
13509 }
13510 }
13511
13512 return decl;
13513}
13514
13515/* Like cp_parser_lookup_name, but for use in the typical case where
13516 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13517 TRUE. */
13518
13519static tree
13520cp_parser_lookup_name_simple (parser, name)
13521 cp_parser *parser;
13522 tree name;
13523{
13524 return cp_parser_lookup_name (parser, name,
13525 /*check_access=*/true,
6fc758aa 13526 /*is_type=*/false,
13527 /*is_namespace=*/false,
0a3b29ad 13528 /*check_dependency=*/true);
13529}
13530
13531/* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13532 TYPENAME_TYPE corresponds. Note that this function peers inside
13533 uninstantiated templates and therefore should be used only in
13534 extremely limited situations. */
13535
13536static tree
13537cp_parser_resolve_typename_type (parser, type)
13538 cp_parser *parser;
13539 tree type;
13540{
13541 tree scope;
13542 tree name;
13543 tree decl;
13544
13545 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13546 20010702);
13547
13548 scope = TYPE_CONTEXT (type);
13549 name = DECL_NAME (TYPE_NAME (type));
13550
13551 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13552 it first before we can figure out what NAME refers to. */
13553 if (TREE_CODE (scope) == TYPENAME_TYPE)
13554 scope = cp_parser_resolve_typename_type (parser, scope);
13555 /* If we don't know what SCOPE refers to, then we cannot resolve the
13556 TYPENAME_TYPE. */
13557 if (scope == error_mark_node)
13558 return error_mark_node;
13559 /* If the SCOPE is a template type parameter, we have no way of
13560 resolving the name. */
13561 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13562 return type;
13563 /* Enter the SCOPE so that name lookup will be resolved as if we
13564 were in the class definition. In particular, SCOPE will no
13565 longer be considered a dependent type. */
13566 push_scope (scope);
13567 /* Look up the declaration. */
13568 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13569 /* If all went well, we got a TYPE_DECL for a non-typename. */
13570 if (!decl
13571 || TREE_CODE (decl) != TYPE_DECL
13572 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13573 {
13574 cp_parser_error (parser, "could not resolve typename type");
13575 type = error_mark_node;
13576 }
13577 else
13578 type = TREE_TYPE (decl);
13579 /* Leave the SCOPE. */
13580 pop_scope (scope);
13581
13582 return type;
13583}
13584
13585/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13586 the current context, return the TYPE_DECL. If TAG_NAME_P is
13587 true, the DECL indicates the class being defined in a class-head,
13588 or declared in an elaborated-type-specifier.
13589
13590 Otherwise, return DECL. */
13591
13592static tree
13593cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13594{
13595 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13596 the scope of the class, then treat the TEMPLATE_DECL as a
13597 class-name. For example, in:
13598
13599 template <class T> struct S {
13600 S s;
13601 };
13602
13603 is OK.
13604
13605 If the TEMPLATE_DECL is being declared as part of a class-head,
13606 the same translation occurs:
13607
13608 struct A {
13609 template <typename T> struct B;
13610 };
13611
13612 template <typename T> struct A::B {};
13613
13614 Similarly, in a elaborated-type-specifier:
13615
13616 namespace N { struct X{}; }
13617
13618 struct A {
13619 template <typename T> friend struct N::X;
13620 };
13621
13622 */
13623 if (DECL_CLASS_TEMPLATE_P (decl)
13624 && (tag_name_p
13625 || (current_class_type
13626 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13627 current_class_type))))
13628 return DECL_TEMPLATE_RESULT (decl);
13629
13630 return decl;
13631}
13632
13633/* If too many, or too few, template-parameter lists apply to the
13634 declarator, issue an error message. Returns TRUE if all went well,
13635 and FALSE otherwise. */
13636
13637static bool
13638cp_parser_check_declarator_template_parameters (parser, declarator)
13639 cp_parser *parser;
13640 tree declarator;
13641{
13642 unsigned num_templates;
13643
13644 /* We haven't seen any classes that involve template parameters yet. */
13645 num_templates = 0;
13646
13647 switch (TREE_CODE (declarator))
13648 {
13649 case CALL_EXPR:
13650 case ARRAY_REF:
13651 case INDIRECT_REF:
13652 case ADDR_EXPR:
13653 {
13654 tree main_declarator = TREE_OPERAND (declarator, 0);
13655 return
13656 cp_parser_check_declarator_template_parameters (parser,
13657 main_declarator);
13658 }
13659
13660 case SCOPE_REF:
13661 {
13662 tree scope;
13663 tree member;
13664
13665 scope = TREE_OPERAND (declarator, 0);
13666 member = TREE_OPERAND (declarator, 1);
13667
13668 /* If this is a pointer-to-member, then we are not interested
13669 in the SCOPE, because it does not qualify the thing that is
13670 being declared. */
13671 if (TREE_CODE (member) == INDIRECT_REF)
13672 return (cp_parser_check_declarator_template_parameters
13673 (parser, member));
13674
13675 while (scope && CLASS_TYPE_P (scope))
13676 {
13677 /* You're supposed to have one `template <...>'
13678 for every template class, but you don't need one
13679 for a full specialization. For example:
13680
13681 template <class T> struct S{};
13682 template <> struct S<int> { void f(); };
13683 void S<int>::f () {}
13684
13685 is correct; there shouldn't be a `template <>' for
13686 the definition of `S<int>::f'. */
13687 if (CLASSTYPE_TEMPLATE_INFO (scope)
13688 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13689 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13690 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13691 ++num_templates;
13692
13693 scope = TYPE_CONTEXT (scope);
13694 }
13695 }
13696
13697 /* Fall through. */
13698
13699 default:
13700 /* If the DECLARATOR has the form `X<y>' then it uses one
13701 additional level of template parameters. */
13702 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13703 ++num_templates;
13704
13705 return cp_parser_check_template_parameters (parser,
13706 num_templates);
13707 }
13708}
13709
13710/* NUM_TEMPLATES were used in the current declaration. If that is
13711 invalid, return FALSE and issue an error messages. Otherwise,
13712 return TRUE. */
13713
13714static bool
13715cp_parser_check_template_parameters (parser, num_templates)
13716 cp_parser *parser;
13717 unsigned num_templates;
13718{
13719 /* If there are more template classes than parameter lists, we have
13720 something like:
13721
13722 template <class T> void S<T>::R<T>::f (); */
13723 if (parser->num_template_parameter_lists < num_templates)
13724 {
13725 error ("too few template-parameter-lists");
13726 return false;
13727 }
13728 /* If there are the same number of template classes and parameter
13729 lists, that's OK. */
13730 if (parser->num_template_parameter_lists == num_templates)
13731 return true;
13732 /* If there are more, but only one more, then we are referring to a
13733 member template. That's OK too. */
13734 if (parser->num_template_parameter_lists == num_templates + 1)
13735 return true;
13736 /* Otherwise, there are too many template parameter lists. We have
13737 something like:
13738
13739 template <class T> template <class U> void S::f(); */
13740 error ("too many template-parameter-lists");
13741 return false;
13742}
13743
13744/* Parse a binary-expression of the general form:
13745
13746 binary-expression:
13747 <expr>
13748 binary-expression <token> <expr>
13749
13750 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13751 to parser the <expr>s. If the first production is used, then the
13752 value returned by FN is returned directly. Otherwise, a node with
13753 the indicated EXPR_TYPE is returned, with operands corresponding to
13754 the two sub-expressions. */
13755
13756static tree
13757cp_parser_binary_expression (parser, token_tree_map, fn)
13758 cp_parser *parser;
13759 cp_parser_token_tree_map token_tree_map;
13760 cp_parser_expression_fn fn;
13761{
13762 tree lhs;
13763
13764 /* Parse the first expression. */
13765 lhs = (*fn) (parser);
13766 /* Now, look for more expressions. */
13767 while (true)
13768 {
13769 cp_token *token;
13770 cp_parser_token_tree_map_node *map_node;
13771 tree rhs;
13772
13773 /* Peek at the next token. */
13774 token = cp_lexer_peek_token (parser->lexer);
13775 /* If the token is `>', and that's not an operator at the
13776 moment, then we're done. */
13777 if (token->type == CPP_GREATER
13778 && !parser->greater_than_is_operator_p)
13779 break;
13780 /* If we find one of the tokens we want, build the correspoding
13781 tree representation. */
13782 for (map_node = token_tree_map;
13783 map_node->token_type != CPP_EOF;
13784 ++map_node)
13785 if (map_node->token_type == token->type)
13786 {
13787 /* Consume the operator token. */
13788 cp_lexer_consume_token (parser->lexer);
13789 /* Parse the right-hand side of the expression. */
13790 rhs = (*fn) (parser);
13791 /* Build the binary tree node. */
13792 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13793 break;
13794 }
13795
13796 /* If the token wasn't one of the ones we want, we're done. */
13797 if (map_node->token_type == CPP_EOF)
13798 break;
13799 }
13800
13801 return lhs;
13802}
13803
13804/* Parse an optional `::' token indicating that the following name is
13805 from the global namespace. If so, PARSER->SCOPE is set to the
13806 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13807 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13808 Returns the new value of PARSER->SCOPE, if the `::' token is
13809 present, and NULL_TREE otherwise. */
13810
13811static tree
13812cp_parser_global_scope_opt (parser, current_scope_valid_p)
13813 cp_parser *parser;
13814 bool current_scope_valid_p;
13815{
13816 cp_token *token;
13817
13818 /* Peek at the next token. */
13819 token = cp_lexer_peek_token (parser->lexer);
13820 /* If we're looking at a `::' token then we're starting from the
13821 global namespace, not our current location. */
13822 if (token->type == CPP_SCOPE)
13823 {
13824 /* Consume the `::' token. */
13825 cp_lexer_consume_token (parser->lexer);
13826 /* Set the SCOPE so that we know where to start the lookup. */
13827 parser->scope = global_namespace;
13828 parser->qualifying_scope = global_namespace;
13829 parser->object_scope = NULL_TREE;
13830
13831 return parser->scope;
13832 }
13833 else if (!current_scope_valid_p)
13834 {
13835 parser->scope = NULL_TREE;
13836 parser->qualifying_scope = NULL_TREE;
13837 parser->object_scope = NULL_TREE;
13838 }
13839
13840 return NULL_TREE;
13841}
13842
13843/* Returns TRUE if the upcoming token sequence is the start of a
13844 constructor declarator. If FRIEND_P is true, the declarator is
13845 preceded by the `friend' specifier. */
13846
13847static bool
13848cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13849{
13850 bool constructor_p;
13851 tree type_decl = NULL_TREE;
13852 bool nested_name_p;
13853
13854 /* Parse tentatively; we are going to roll back all of the tokens
13855 consumed here. */
13856 cp_parser_parse_tentatively (parser);
13857 /* Assume that we are looking at a constructor declarator. */
13858 constructor_p = true;
13859 /* Look for the optional `::' operator. */
13860 cp_parser_global_scope_opt (parser,
13861 /*current_scope_valid_p=*/false);
13862 /* Look for the nested-name-specifier. */
13863 nested_name_p
13864 = (cp_parser_nested_name_specifier_opt (parser,
13865 /*typename_keyword_p=*/false,
13866 /*check_dependency_p=*/false,
13867 /*type_p=*/false)
13868 != NULL_TREE);
13869 /* Outside of a class-specifier, there must be a
13870 nested-name-specifier. */
13871 if (!nested_name_p &&
13872 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13873 || friend_p))
13874 constructor_p = false;
13875 /* If we still think that this might be a constructor-declarator,
13876 look for a class-name. */
13877 if (constructor_p)
13878 {
13879 /* If we have:
13880
13881 template <typename T> struct S { S(); }
13882 template <typename T> S<T>::S ();
13883
13884 we must recognize that the nested `S' names a class.
13885 Similarly, for:
13886
13887 template <typename T> S<T>::S<T> ();
13888
13889 we must recognize that the nested `S' names a template. */
13890 type_decl = cp_parser_class_name (parser,
13891 /*typename_keyword_p=*/false,
13892 /*template_keyword_p=*/false,
13893 /*type_p=*/false,
13894 /*check_access_p=*/false,
13895 /*check_dependency_p=*/false,
13896 /*class_head_p=*/false);
13897 /* If there was no class-name, then this is not a constructor. */
13898 constructor_p = !cp_parser_error_occurred (parser);
13899 }
13900 /* If we're still considering a constructor, we have to see a `(',
13901 to begin the parameter-declaration-clause, followed by either a
13902 `)', an `...', or a decl-specifier. We need to check for a
13903 type-specifier to avoid being fooled into thinking that:
13904
13905 S::S (f) (int);
13906
13907 is a constructor. (It is actually a function named `f' that
13908 takes one parameter (of type `int') and returns a value of type
13909 `S::S'. */
13910 if (constructor_p
13911 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13912 {
13913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13914 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13915 && !cp_parser_storage_class_specifier_opt (parser))
13916 {
13917 if (current_class_type
13918 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
13919 /* The constructor for one class cannot be declared inside
13920 another. */
13921 constructor_p = false;
13922 else
13923 {
13924 tree type;
13925
13926 /* Names appearing in the type-specifier should be looked up
13927 in the scope of the class. */
13928 if (current_class_type)
13929 type = NULL_TREE;
13930 else
13931 {
13932 type = TREE_TYPE (type_decl);
13933 if (TREE_CODE (type) == TYPENAME_TYPE)
13934 type = cp_parser_resolve_typename_type (parser, type);
13935 push_scope (type);
13936 }
13937 /* Look for the type-specifier. */
13938 cp_parser_type_specifier (parser,
13939 CP_PARSER_FLAGS_NONE,
13940 /*is_friend=*/false,
13941 /*is_declarator=*/true,
13942 /*declares_class_or_enum=*/NULL,
13943 /*is_cv_qualifier=*/NULL);
13944 /* Leave the scope of the class. */
13945 if (type)
13946 pop_scope (type);
13947
13948 constructor_p = !cp_parser_error_occurred (parser);
13949 }
13950 }
13951 }
13952 else
13953 constructor_p = false;
13954 /* We did not really want to consume any tokens. */
13955 cp_parser_abort_tentative_parse (parser);
13956
13957 return constructor_p;
13958}
13959
13960/* Parse the definition of the function given by the DECL_SPECIFIERS,
13961 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
13962 they must be performed once we are in the scope of the function.
13963
13964 Returns the function defined. */
13965
13966static tree
13967cp_parser_function_definition_from_specifiers_and_declarator
13968 (parser, decl_specifiers, attributes, declarator, access_checks)
13969 cp_parser *parser;
13970 tree decl_specifiers;
13971 tree attributes;
13972 tree declarator;
13973 tree access_checks;
13974{
13975 tree fn;
13976 bool success_p;
13977
13978 /* Begin the function-definition. */
13979 success_p = begin_function_definition (decl_specifiers,
13980 attributes,
13981 declarator);
13982
13983 /* If there were names looked up in the decl-specifier-seq that we
13984 did not check, check them now. We must wait until we are in the
13985 scope of the function to perform the checks, since the function
13986 might be a friend. */
13987 cp_parser_perform_deferred_access_checks (access_checks);
13988
13989 if (!success_p)
13990 {
13991 /* If begin_function_definition didn't like the definition, skip
13992 the entire function. */
13993 error ("invalid function declaration");
13994 cp_parser_skip_to_end_of_block_or_statement (parser);
13995 fn = error_mark_node;
13996 }
13997 else
13998 fn = cp_parser_function_definition_after_declarator (parser,
13999 /*inline_p=*/false);
14000
14001 return fn;
14002}
14003
14004/* Parse the part of a function-definition that follows the
14005 declarator. INLINE_P is TRUE iff this function is an inline
14006 function defined with a class-specifier.
14007
14008 Returns the function defined. */
14009
14010static tree
14011cp_parser_function_definition_after_declarator (parser,
14012 inline_p)
14013 cp_parser *parser;
14014 bool inline_p;
14015{
14016 tree fn;
14017 bool ctor_initializer_p = false;
14018 bool saved_in_unbraced_linkage_specification_p;
14019 unsigned saved_num_template_parameter_lists;
14020
14021 /* If the next token is `return', then the code may be trying to
14022 make use of the "named return value" extension that G++ used to
14023 support. */
14024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14025 {
14026 /* Consume the `return' keyword. */
14027 cp_lexer_consume_token (parser->lexer);
14028 /* Look for the identifier that indicates what value is to be
14029 returned. */
14030 cp_parser_identifier (parser);
14031 /* Issue an error message. */
14032 error ("named return values are no longer supported");
14033 /* Skip tokens until we reach the start of the function body. */
14034 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14035 cp_lexer_consume_token (parser->lexer);
14036 }
14037 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14038 anything declared inside `f'. */
14039 saved_in_unbraced_linkage_specification_p
14040 = parser->in_unbraced_linkage_specification_p;
14041 parser->in_unbraced_linkage_specification_p = false;
14042 /* Inside the function, surrounding template-parameter-lists do not
14043 apply. */
14044 saved_num_template_parameter_lists
14045 = parser->num_template_parameter_lists;
14046 parser->num_template_parameter_lists = 0;
14047 /* If the next token is `try', then we are looking at a
14048 function-try-block. */
14049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14050 ctor_initializer_p = cp_parser_function_try_block (parser);
14051 /* A function-try-block includes the function-body, so we only do
14052 this next part if we're not processing a function-try-block. */
14053 else
14054 ctor_initializer_p
14055 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14056
14057 /* Finish the function. */
14058 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14059 (inline_p ? 2 : 0));
14060 /* Generate code for it, if necessary. */
14061 expand_body (fn);
14062 /* Restore the saved values. */
14063 parser->in_unbraced_linkage_specification_p
14064 = saved_in_unbraced_linkage_specification_p;
14065 parser->num_template_parameter_lists
14066 = saved_num_template_parameter_lists;
14067
14068 return fn;
14069}
14070
14071/* Parse a template-declaration, assuming that the `export' (and
14072 `extern') keywords, if present, has already been scanned. MEMBER_P
14073 is as for cp_parser_template_declaration. */
14074
14075static void
14076cp_parser_template_declaration_after_export (parser, member_p)
14077 cp_parser *parser;
14078 bool member_p;
14079{
14080 tree decl = NULL_TREE;
14081 tree parameter_list;
14082 bool friend_p = false;
14083
14084 /* Look for the `template' keyword. */
14085 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14086 return;
14087
14088 /* And the `<'. */
14089 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14090 return;
14091
14092 /* Parse the template parameters. */
14093 begin_template_parm_list ();
14094 /* If the next token is `>', then we have an invalid
14095 specialization. Rather than complain about an invalid template
14096 parameter, issue an error message here. */
14097 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14098 {
14099 cp_parser_error (parser, "invalid explicit specialization");
14100 parameter_list = NULL_TREE;
14101 }
14102 else
14103 parameter_list = cp_parser_template_parameter_list (parser);
14104 parameter_list = end_template_parm_list (parameter_list);
14105 /* Look for the `>'. */
14106 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14107 /* We just processed one more parameter list. */
14108 ++parser->num_template_parameter_lists;
14109 /* If the next token is `template', there are more template
14110 parameters. */
14111 if (cp_lexer_next_token_is_keyword (parser->lexer,
14112 RID_TEMPLATE))
14113 cp_parser_template_declaration_after_export (parser, member_p);
14114 else
14115 {
14116 decl = cp_parser_single_declaration (parser,
14117 member_p,
14118 &friend_p);
14119
14120 /* If this is a member template declaration, let the front
14121 end know. */
14122 if (member_p && !friend_p && decl)
14123 decl = finish_member_template_decl (decl);
14124 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14125 make_friend_class (current_class_type, TREE_TYPE (decl));
14126 }
14127 /* We are done with the current parameter list. */
14128 --parser->num_template_parameter_lists;
14129
14130 /* Finish up. */
14131 finish_template_decl (parameter_list);
14132
14133 /* Register member declarations. */
14134 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14135 finish_member_declaration (decl);
14136
14137 /* If DECL is a function template, we must return to parse it later.
14138 (Even though there is no definition, there might be default
14139 arguments that need handling.) */
14140 if (member_p && decl
14141 && (TREE_CODE (decl) == FUNCTION_DECL
14142 || DECL_FUNCTION_TEMPLATE_P (decl)))
14143 TREE_VALUE (parser->unparsed_functions_queues)
14144 = tree_cons (current_class_type, decl,
14145 TREE_VALUE (parser->unparsed_functions_queues));
14146}
14147
14148/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14149 `function-definition' sequence. MEMBER_P is true, this declaration
14150 appears in a class scope.
14151
14152 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14153 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14154
14155static tree
14156cp_parser_single_declaration (parser,
14157 member_p,
14158 friend_p)
14159 cp_parser *parser;
14160 bool member_p;
14161 bool *friend_p;
14162{
14163 bool declares_class_or_enum;
14164 tree decl = NULL_TREE;
14165 tree decl_specifiers;
14166 tree attributes;
14167 tree access_checks;
14168
14169 /* Parse the dependent declaration. We don't know yet
14170 whether it will be a function-definition. */
14171 cp_parser_parse_tentatively (parser);
14172 /* Defer access checks until we know what is being declared. */
14173 cp_parser_start_deferring_access_checks (parser);
14174 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14175 alternative. */
14176 decl_specifiers
14177 = cp_parser_decl_specifier_seq (parser,
14178 CP_PARSER_FLAGS_OPTIONAL,
14179 &attributes,
14180 &declares_class_or_enum);
14181 /* Gather up the access checks that occurred the
14182 decl-specifier-seq. */
14183 access_checks = cp_parser_stop_deferring_access_checks (parser);
14184 /* Check for the declaration of a template class. */
14185 if (declares_class_or_enum)
14186 {
14187 if (cp_parser_declares_only_class_p (parser))
14188 {
14189 decl = shadow_tag (decl_specifiers);
14190 if (decl)
14191 decl = TYPE_NAME (decl);
14192 else
14193 decl = error_mark_node;
14194 }
14195 }
14196 else
14197 decl = NULL_TREE;
14198 /* If it's not a template class, try for a template function. If
14199 the next token is a `;', then this declaration does not declare
14200 anything. But, if there were errors in the decl-specifiers, then
14201 the error might well have come from an attempted class-specifier.
14202 In that case, there's no need to warn about a missing declarator. */
14203 if (!decl
14204 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14205 || !value_member (error_mark_node, decl_specifiers)))
14206 decl = cp_parser_init_declarator (parser,
14207 decl_specifiers,
14208 attributes,
14209 access_checks,
14210 /*function_definition_allowed_p=*/false,
14211 member_p,
14212 /*function_definition_p=*/NULL);
14213 /* Clear any current qualification; whatever comes next is the start
14214 of something new. */
14215 parser->scope = NULL_TREE;
14216 parser->qualifying_scope = NULL_TREE;
14217 parser->object_scope = NULL_TREE;
14218 /* Look for a trailing `;' after the declaration. */
14219 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14220 && cp_parser_committed_to_tentative_parse (parser))
14221 cp_parser_skip_to_end_of_block_or_statement (parser);
14222 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14223 if (cp_parser_parse_definitely (parser))
14224 {
14225 if (friend_p)
14226 *friend_p = cp_parser_friend_p (decl_specifiers);
14227 }
14228 /* Otherwise, try a function-definition. */
14229 else
14230 decl = cp_parser_function_definition (parser, friend_p);
14231
14232 return decl;
14233}
14234
14235/* Parse a functional cast to TYPE. Returns an expression
14236 representing the cast. */
14237
14238static tree
14239cp_parser_functional_cast (parser, type)
14240 cp_parser *parser;
14241 tree type;
14242{
14243 tree expression_list;
14244
14245 /* Look for the opening `('. */
14246 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14247 return error_mark_node;
14248 /* If the next token is not an `)', there are arguments to the
14249 cast. */
14250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14251 expression_list = cp_parser_expression_list (parser);
14252 else
14253 expression_list = NULL_TREE;
14254 /* Look for the closing `)'. */
14255 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14256
14257 return build_functional_cast (type, expression_list);
14258}
14259
14260/* MEMBER_FUNCTION is a member function, or a friend. If default
14261 arguments, or the body of the function have not yet been parsed,
14262 parse them now. */
14263
14264static void
14265cp_parser_late_parsing_for_member (parser, member_function)
14266 cp_parser *parser;
14267 tree member_function;
14268{
14269 cp_lexer *saved_lexer;
14270
14271 /* If this member is a template, get the underlying
14272 FUNCTION_DECL. */
14273 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14274 member_function = DECL_TEMPLATE_RESULT (member_function);
14275
14276 /* There should not be any class definitions in progress at this
14277 point; the bodies of members are only parsed outside of all class
14278 definitions. */
14279 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14280 /* While we're parsing the member functions we might encounter more
14281 classes. We want to handle them right away, but we don't want
14282 them getting mixed up with functions that are currently in the
14283 queue. */
14284 parser->unparsed_functions_queues
14285 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14286
14287 /* Make sure that any template parameters are in scope. */
14288 maybe_begin_member_template_processing (member_function);
14289
14290 /* If there are default arguments that have not yet been processed,
14291 take care of them now. */
2c593bd0 14292 cp_parser_late_parsing_default_args (parser, TREE_TYPE (member_function),
14293 DECL_FUNCTION_MEMBER_P (member_function)
14294 ? DECL_CONTEXT (member_function)
14295 : NULL_TREE);
0a3b29ad 14296
14297 /* If the body of the function has not yet been parsed, parse it
14298 now. */
14299 if (DECL_PENDING_INLINE_P (member_function))
14300 {
14301 tree function_scope;
14302 cp_token_cache *tokens;
14303
14304 /* The function is no longer pending; we are processing it. */
14305 tokens = DECL_PENDING_INLINE_INFO (member_function);
14306 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14307 DECL_PENDING_INLINE_P (member_function) = 0;
14308 /* If this was an inline function in a local class, enter the scope
14309 of the containing function. */
14310 function_scope = decl_function_context (member_function);
14311 if (function_scope)
14312 push_function_context_to (function_scope);
14313
14314 /* Save away the current lexer. */
14315 saved_lexer = parser->lexer;
14316 /* Make a new lexer to feed us the tokens saved for this function. */
14317 parser->lexer = cp_lexer_new_from_tokens (tokens);
14318 parser->lexer->next = saved_lexer;
14319
14320 /* Set the current source position to be the location of the first
14321 token in the saved inline body. */
14322 cp_lexer_set_source_position_from_token
14323 (parser->lexer,
14324 cp_lexer_peek_token (parser->lexer));
14325
14326 /* Let the front end know that we going to be defining this
14327 function. */
14328 start_function (NULL_TREE, member_function, NULL_TREE,
14329 SF_PRE_PARSED | SF_INCLASS_INLINE);
14330
14331 /* Now, parse the body of the function. */
14332 cp_parser_function_definition_after_declarator (parser,
14333 /*inline_p=*/true);
14334
14335 /* Leave the scope of the containing function. */
14336 if (function_scope)
14337 pop_function_context_from (function_scope);
14338 /* Restore the lexer. */
14339 parser->lexer = saved_lexer;
14340 }
14341
14342 /* Remove any template parameters from the symbol table. */
14343 maybe_end_member_template_processing ();
14344
14345 /* Restore the queue. */
14346 parser->unparsed_functions_queues
14347 = TREE_CHAIN (parser->unparsed_functions_queues);
14348}
14349
14350/* TYPE is a FUNCTION_TYPE or METHOD_TYPE which contains a parameter
2c593bd0 14351 with an unparsed DEFAULT_ARG. If non-NULL, SCOPE is the class in
14352 whose context name lookups in the default argument should occur.
14353 Parse the default args now. */
0a3b29ad 14354
14355static void
2c593bd0 14356cp_parser_late_parsing_default_args (cp_parser *parser, tree type, tree scope)
0a3b29ad 14357{
14358 cp_lexer *saved_lexer;
14359 cp_token_cache *tokens;
14360 bool saved_local_variables_forbidden_p;
14361 tree parameters;
14362
14363 for (parameters = TYPE_ARG_TYPES (type);
14364 parameters;
14365 parameters = TREE_CHAIN (parameters))
14366 {
14367 if (!TREE_PURPOSE (parameters)
14368 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14369 continue;
14370
14371 /* Save away the current lexer. */
14372 saved_lexer = parser->lexer;
14373 /* Create a new one, using the tokens we have saved. */
14374 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14375 parser->lexer = cp_lexer_new_from_tokens (tokens);
14376
14377 /* Set the current source position to be the location of the
14378 first token in the default argument. */
14379 cp_lexer_set_source_position_from_token
14380 (parser->lexer, cp_lexer_peek_token (parser->lexer));
14381
14382 /* Local variable names (and the `this' keyword) may not appear
14383 in a default argument. */
14384 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14385 parser->local_variables_forbidden_p = true;
14386 /* Parse the assignment-expression. */
2c593bd0 14387 if (scope)
14388 push_nested_class (scope, 1);
0a3b29ad 14389 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
2c593bd0 14390 if (scope)
14391 pop_nested_class ();
0a3b29ad 14392
14393 /* Restore saved state. */
14394 parser->lexer = saved_lexer;
14395 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14396 }
14397}
14398
14399/* Parse the operand of `sizeof' (or a similar operator). Returns
14400 either a TYPE or an expression, depending on the form of the
14401 input. The KEYWORD indicates which kind of expression we have
14402 encountered. */
14403
14404static tree
14405cp_parser_sizeof_operand (parser, keyword)
14406 cp_parser *parser;
14407 enum rid keyword;
14408{
14409 static const char *format;
14410 tree expr = NULL_TREE;
14411 const char *saved_message;
14412 bool saved_constant_expression_p;
14413
14414 /* Initialize FORMAT the first time we get here. */
14415 if (!format)
14416 format = "types may not be defined in `%s' expressions";
14417
14418 /* Types cannot be defined in a `sizeof' expression. Save away the
14419 old message. */
14420 saved_message = parser->type_definition_forbidden_message;
14421 /* And create the new one. */
14422 parser->type_definition_forbidden_message
14423 = ((const char *)
14424 xmalloc (strlen (format)
14425 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14426 + 1 /* `\0' */));
14427 sprintf ((char *) parser->type_definition_forbidden_message,
14428 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14429
14430 /* The restrictions on constant-expressions do not apply inside
14431 sizeof expressions. */
14432 saved_constant_expression_p = parser->constant_expression_p;
14433 parser->constant_expression_p = false;
14434
4c99a080 14435 /* Do not actually evaluate the expression. */
14436 ++skip_evaluation;
0a3b29ad 14437 /* If it's a `(', then we might be looking at the type-id
14438 construction. */
14439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14440 {
14441 tree type;
14442
14443 /* We can't be sure yet whether we're looking at a type-id or an
14444 expression. */
14445 cp_parser_parse_tentatively (parser);
14446 /* Consume the `('. */
14447 cp_lexer_consume_token (parser->lexer);
14448 /* Parse the type-id. */
14449 type = cp_parser_type_id (parser);
14450 /* Now, look for the trailing `)'. */
14451 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14452 /* If all went well, then we're done. */
14453 if (cp_parser_parse_definitely (parser))
14454 {
14455 /* Build a list of decl-specifiers; right now, we have only
14456 a single type-specifier. */
14457 type = build_tree_list (NULL_TREE,
14458 type);
14459
14460 /* Call grokdeclarator to figure out what type this is. */
14461 expr = grokdeclarator (NULL_TREE,
14462 type,
14463 TYPENAME,
14464 /*initialized=*/0,
14465 /*attrlist=*/NULL);
14466 }
14467 }
14468
14469 /* If the type-id production did not work out, then we must be
14470 looking at the unary-expression production. */
14471 if (!expr)
14472 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
4c99a080 14473 /* Go back to evaluating expressions. */
14474 --skip_evaluation;
0a3b29ad 14475
14476 /* Free the message we created. */
14477 free ((char *) parser->type_definition_forbidden_message);
14478 /* And restore the old one. */
14479 parser->type_definition_forbidden_message = saved_message;
14480 parser->constant_expression_p = saved_constant_expression_p;
14481
14482 return expr;
14483}
14484
14485/* If the current declaration has no declarator, return true. */
14486
14487static bool
14488cp_parser_declares_only_class_p (cp_parser *parser)
14489{
14490 /* If the next token is a `;' or a `,' then there is no
14491 declarator. */
14492 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14493 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14494}
14495
14496/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14497 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14498
14499static bool
14500cp_parser_friend_p (decl_specifiers)
14501 tree decl_specifiers;
14502{
14503 while (decl_specifiers)
14504 {
14505 /* See if this decl-specifier is `friend'. */
14506 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14507 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14508 return true;
14509
14510 /* Go on to the next decl-specifier. */
14511 decl_specifiers = TREE_CHAIN (decl_specifiers);
14512 }
14513
14514 return false;
14515}
14516
14517/* If the next token is of the indicated TYPE, consume it. Otherwise,
14518 issue an error message indicating that TOKEN_DESC was expected.
14519
14520 Returns the token consumed, if the token had the appropriate type.
14521 Otherwise, returns NULL. */
14522
14523static cp_token *
14524cp_parser_require (parser, type, token_desc)
14525 cp_parser *parser;
14526 enum cpp_ttype type;
14527 const char *token_desc;
14528{
14529 if (cp_lexer_next_token_is (parser->lexer, type))
14530 return cp_lexer_consume_token (parser->lexer);
14531 else
14532 {
2c593bd0 14533 /* Output the MESSAGE -- unless we're parsing tentatively. */
14534 if (!cp_parser_simulate_error (parser))
14535 error ("expected %s", token_desc);
0a3b29ad 14536 return NULL;
14537 }
14538}
14539
14540/* Like cp_parser_require, except that tokens will be skipped until
14541 the desired token is found. An error message is still produced if
14542 the next token is not as expected. */
14543
14544static void
14545cp_parser_skip_until_found (parser, type, token_desc)
14546 cp_parser *parser;
14547 enum cpp_ttype type;
14548 const char *token_desc;
14549{
14550 cp_token *token;
14551 unsigned nesting_depth = 0;
14552
14553 if (cp_parser_require (parser, type, token_desc))
14554 return;
14555
14556 /* Skip tokens until the desired token is found. */
14557 while (true)
14558 {
14559 /* Peek at the next token. */
14560 token = cp_lexer_peek_token (parser->lexer);
14561 /* If we've reached the token we want, consume it and
14562 stop. */
14563 if (token->type == type && !nesting_depth)
14564 {
14565 cp_lexer_consume_token (parser->lexer);
14566 return;
14567 }
14568 /* If we've run out of tokens, stop. */
14569 if (token->type == CPP_EOF)
14570 return;
14571 if (token->type == CPP_OPEN_BRACE
14572 || token->type == CPP_OPEN_PAREN
14573 || token->type == CPP_OPEN_SQUARE)
14574 ++nesting_depth;
14575 else if (token->type == CPP_CLOSE_BRACE
14576 || token->type == CPP_CLOSE_PAREN
14577 || token->type == CPP_CLOSE_SQUARE)
14578 {
14579 if (nesting_depth-- == 0)
14580 return;
14581 }
14582 /* Consume this token. */
14583 cp_lexer_consume_token (parser->lexer);
14584 }
14585}
14586
14587/* If the next token is the indicated keyword, consume it. Otherwise,
14588 issue an error message indicating that TOKEN_DESC was expected.
14589
14590 Returns the token consumed, if the token had the appropriate type.
14591 Otherwise, returns NULL. */
14592
14593static cp_token *
14594cp_parser_require_keyword (parser, keyword, token_desc)
14595 cp_parser *parser;
14596 enum rid keyword;
14597 const char *token_desc;
14598{
14599 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14600
14601 if (token && token->keyword != keyword)
14602 {
14603 dyn_string_t error_msg;
14604
14605 /* Format the error message. */
14606 error_msg = dyn_string_new (0);
14607 dyn_string_append_cstr (error_msg, "expected ");
14608 dyn_string_append_cstr (error_msg, token_desc);
14609 cp_parser_error (parser, error_msg->s);
14610 dyn_string_delete (error_msg);
14611 return NULL;
14612 }
14613
14614 return token;
14615}
14616
14617/* Returns TRUE iff TOKEN is a token that can begin the body of a
14618 function-definition. */
14619
14620static bool
14621cp_parser_token_starts_function_definition_p (token)
14622 cp_token *token;
14623{
14624 return (/* An ordinary function-body begins with an `{'. */
14625 token->type == CPP_OPEN_BRACE
14626 /* A ctor-initializer begins with a `:'. */
14627 || token->type == CPP_COLON
14628 /* A function-try-block begins with `try'. */
14629 || token->keyword == RID_TRY
14630 /* The named return value extension begins with `return'. */
14631 || token->keyword == RID_RETURN);
14632}
14633
14634/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14635 definition. */
14636
14637static bool
14638cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14639{
14640 cp_token *token;
14641
14642 token = cp_lexer_peek_token (parser->lexer);
14643 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14644}
14645
14646/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14647 or none_type otherwise. */
14648
14649static enum tag_types
14650cp_parser_token_is_class_key (token)
14651 cp_token *token;
14652{
14653 switch (token->keyword)
14654 {
14655 case RID_CLASS:
14656 return class_type;
14657 case RID_STRUCT:
14658 return record_type;
14659 case RID_UNION:
14660 return union_type;
14661
14662 default:
14663 return none_type;
14664 }
14665}
14666
14667/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14668
14669static void
14670cp_parser_check_class_key (enum tag_types class_key, tree type)
14671{
14672 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14673 pedwarn ("`%s' tag used in naming `%#T'",
14674 class_key == union_type ? "union"
14675 : class_key == record_type ? "struct" : "class",
14676 type);
14677}
14678
14679/* Look for the `template' keyword, as a syntactic disambiguator.
14680 Return TRUE iff it is present, in which case it will be
14681 consumed. */
14682
14683static bool
14684cp_parser_optional_template_keyword (cp_parser *parser)
14685{
14686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14687 {
14688 /* The `template' keyword can only be used within templates;
14689 outside templates the parser can always figure out what is a
14690 template and what is not. */
14691 if (!processing_template_decl)
14692 {
14693 error ("`template' (as a disambiguator) is only allowed "
14694 "within templates");
14695 /* If this part of the token stream is rescanned, the same
14696 error message would be generated. So, we purge the token
14697 from the stream. */
14698 cp_lexer_purge_token (parser->lexer);
14699 return false;
14700 }
14701 else
14702 {
14703 /* Consume the `template' keyword. */
14704 cp_lexer_consume_token (parser->lexer);
14705 return true;
14706 }
14707 }
14708
14709 return false;
14710}
14711
14712/* Add tokens to CACHE until an non-nested END token appears. */
14713
14714static void
14715cp_parser_cache_group (cp_parser *parser,
14716 cp_token_cache *cache,
14717 enum cpp_ttype end,
14718 unsigned depth)
14719{
14720 while (true)
14721 {
14722 cp_token *token;
14723
14724 /* Abort a parenthesized expression if we encounter a brace. */
14725 if ((end == CPP_CLOSE_PAREN || depth == 0)
14726 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14727 return;
14728 /* Consume the next token. */
14729 token = cp_lexer_consume_token (parser->lexer);
14730 /* If we've reached the end of the file, stop. */
14731 if (token->type == CPP_EOF)
14732 return;
14733 /* Add this token to the tokens we are saving. */
14734 cp_token_cache_push_token (cache, token);
14735 /* See if it starts a new group. */
14736 if (token->type == CPP_OPEN_BRACE)
14737 {
14738 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14739 if (depth == 0)
14740 return;
14741 }
14742 else if (token->type == CPP_OPEN_PAREN)
14743 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14744 else if (token->type == end)
14745 return;
14746 }
14747}
14748
14749/* Begin parsing tentatively. We always save tokens while parsing
14750 tentatively so that if the tentative parsing fails we can restore the
14751 tokens. */
14752
14753static void
14754cp_parser_parse_tentatively (parser)
14755 cp_parser *parser;
14756{
14757 /* Enter a new parsing context. */
14758 parser->context = cp_parser_context_new (parser->context);
14759 /* Begin saving tokens. */
14760 cp_lexer_save_tokens (parser->lexer);
14761 /* In order to avoid repetitive access control error messages,
14762 access checks are queued up until we are no longer parsing
14763 tentatively. */
14764 cp_parser_start_deferring_access_checks (parser);
14765}
14766
14767/* Commit to the currently active tentative parse. */
14768
14769static void
14770cp_parser_commit_to_tentative_parse (parser)
14771 cp_parser *parser;
14772{
14773 cp_parser_context *context;
14774 cp_lexer *lexer;
14775
14776 /* Mark all of the levels as committed. */
14777 lexer = parser->lexer;
14778 for (context = parser->context; context->next; context = context->next)
14779 {
14780 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14781 break;
14782 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14783 while (!cp_lexer_saving_tokens (lexer))
14784 lexer = lexer->next;
14785 cp_lexer_commit_tokens (lexer);
14786 }
14787}
14788
14789/* Abort the currently active tentative parse. All consumed tokens
14790 will be rolled back, and no diagnostics will be issued. */
14791
14792static void
14793cp_parser_abort_tentative_parse (parser)
14794 cp_parser *parser;
14795{
14796 cp_parser_simulate_error (parser);
14797 /* Now, pretend that we want to see if the construct was
14798 successfully parsed. */
14799 cp_parser_parse_definitely (parser);
14800}
14801
14802/* Stop parsing tentatively. If a parse error has ocurred, restore the
14803 token stream. Otherwise, commit to the tokens we have consumed.
14804 Returns true if no error occurred; false otherwise. */
14805
14806static bool
14807cp_parser_parse_definitely (parser)
14808 cp_parser *parser;
14809{
14810 bool error_occurred;
14811 cp_parser_context *context;
14812
14813 /* Remember whether or not an error ocurred, since we are about to
14814 destroy that information. */
14815 error_occurred = cp_parser_error_occurred (parser);
14816 /* Remove the topmost context from the stack. */
14817 context = parser->context;
14818 parser->context = context->next;
14819 /* If no parse errors occurred, commit to the tentative parse. */
14820 if (!error_occurred)
14821 {
14822 /* Commit to the tokens read tentatively, unless that was
14823 already done. */
14824 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14825 cp_lexer_commit_tokens (parser->lexer);
14826 if (!parser->context->deferring_access_checks_p)
14827 /* If in the parent context we are not deferring checks, then
14828 these perform these checks now. */
14829 (cp_parser_perform_deferred_access_checks
14830 (context->deferred_access_checks));
14831 else
14832 /* Any lookups that were deferred during the tentative parse are
14833 still deferred. */
14834 parser->context->deferred_access_checks
14835 = chainon (parser->context->deferred_access_checks,
14836 context->deferred_access_checks);
0a3b29ad 14837 }
14838 /* Otherwise, if errors occurred, roll back our state so that things
14839 are just as they were before we began the tentative parse. */
14840 else
2c593bd0 14841 cp_lexer_rollback_tokens (parser->lexer);
14842 /* Add the context to the front of the free list. */
14843 context->next = cp_parser_context_free_list;
14844 cp_parser_context_free_list = context;
14845
14846 return !error_occurred;
0a3b29ad 14847}
14848
14849/* Returns non-zero if we are parsing tentatively. */
14850
14851static bool
14852cp_parser_parsing_tentatively (parser)
14853 cp_parser *parser;
14854{
14855 return parser->context->next != NULL;
14856}
14857
14858/* Returns true if we are parsing tentatively -- but have decided that
14859 we will stick with this tentative parse, even if errors occur. */
14860
14861static bool
14862cp_parser_committed_to_tentative_parse (parser)
14863 cp_parser *parser;
14864{
14865 return (cp_parser_parsing_tentatively (parser)
14866 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14867}
14868
14869/* Returns non-zero iff an error has occurred during the most recent
14870 tentative parse. */
14871
14872static bool
14873cp_parser_error_occurred (parser)
14874 cp_parser *parser;
14875{
14876 return (cp_parser_parsing_tentatively (parser)
14877 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14878}
14879
14880/* Returns non-zero if GNU extensions are allowed. */
14881
14882static bool
14883cp_parser_allow_gnu_extensions_p (parser)
14884 cp_parser *parser;
14885{
14886 return parser->allow_gnu_extensions_p;
14887}
14888
14889\f
14890
14891/* The parser. */
14892
14893static GTY (()) cp_parser *the_parser;
14894
14895/* External interface. */
14896
14897/* Parse the entire translation unit. */
14898
14899int
14900yyparse ()
14901{
14902 bool error_occurred;
14903
14904 the_parser = cp_parser_new ();
14905 error_occurred = cp_parser_translation_unit (the_parser);
14906 the_parser = NULL;
14907
14908 return error_occurred;
14909}
14910
14911/* Clean up after parsing the entire translation unit. */
14912
14913void
14914free_parser_stacks ()
14915{
14916 /* Nothing to do. */
14917}
14918
14919/* This variable must be provided by every front end. */
14920
14921int yydebug;
14922
14923#include "gt-cp-parser.h"