]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
re PR c++/9264 ([parser] ICE on invalid octal constant)
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
a723baf1 1/* C++ Parser.
3beb3abf 2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
a723baf1
MM
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
f5adbb8d 5 This file is part of GCC.
a723baf1 6
f5adbb8d 7 GCC is free software; you can redistribute it and/or modify it
a723baf1
MM
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
f5adbb8d 12 GCC is distributed in the hope that it will be useful, but
a723baf1
MM
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
f5adbb8d 18 along with GCC; see the file COPYING. If not, write to the Free
a723baf1
MM
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "dyn-string.h"
27#include "varray.h"
28#include "cpplib.h"
29#include "tree.h"
30#include "cp-tree.h"
31#include "c-pragma.h"
32#include "decl.h"
33#include "flags.h"
34#include "diagnostic.h"
a723baf1
MM
35#include "toplev.h"
36#include "output.h"
37
38\f
39/* The lexer. */
40
41/* Overview
42 --------
43
44 A cp_lexer represents a stream of cp_tokens. It allows arbitrary
45 look-ahead.
46
47 Methodology
48 -----------
49
50 We use a circular buffer to store incoming tokens.
51
52 Some artifacts of the C++ language (such as the
53 expression/declaration ambiguity) require arbitrary look-ahead.
54 The strategy we adopt for dealing with these problems is to attempt
55 to parse one construct (e.g., the declaration) and fall back to the
56 other (e.g., the expression) if that attempt does not succeed.
57 Therefore, we must sometimes store an arbitrary number of tokens.
58
59 The parser routinely peeks at the next token, and then consumes it
60 later. That also requires a buffer in which to store the tokens.
61
62 In order to easily permit adding tokens to the end of the buffer,
63 while removing them from the beginning of the buffer, we use a
64 circular buffer. */
65
66/* A C++ token. */
67
68typedef struct cp_token GTY (())
69{
70 /* The kind of token. */
71 enum cpp_ttype type;
72 /* The value associated with this token, if any. */
73 tree value;
74 /* If this token is a keyword, this value indicates which keyword.
75 Otherwise, this value is RID_MAX. */
76 enum rid keyword;
77 /* The file in which this token was found. */
78 const char *file_name;
79 /* The line at which this token was found. */
80 int line_number;
81} cp_token;
82
83/* The number of tokens in a single token block. */
84
85#define CP_TOKEN_BLOCK_NUM_TOKENS 32
86
87/* A group of tokens. These groups are chained together to store
88 large numbers of tokens. (For example, a token block is created
89 when the body of an inline member function is first encountered;
90 the tokens are processed later after the class definition is
91 complete.)
92
93 This somewhat ungainly data structure (as opposed to, say, a
94 variable-length array), is used due to contraints imposed by the
95 current garbage-collection methodology. If it is made more
96 flexible, we could perhaps simplify the data structures involved. */
97
98typedef struct cp_token_block GTY (())
99{
100 /* The tokens. */
101 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
102 /* The number of tokens in this block. */
103 size_t num_tokens;
104 /* The next token block in the chain. */
105 struct cp_token_block *next;
106 /* The previous block in the chain. */
107 struct cp_token_block *prev;
108} cp_token_block;
109
110typedef struct cp_token_cache GTY (())
111{
112 /* The first block in the cache. NULL if there are no tokens in the
113 cache. */
114 cp_token_block *first;
115 /* The last block in the cache. NULL If there are no tokens in the
116 cache. */
117 cp_token_block *last;
118} cp_token_cache;
119
120/* Prototypes. */
121
122static cp_token_cache *cp_token_cache_new
123 (void);
124static void cp_token_cache_push_token
125 (cp_token_cache *, cp_token *);
126
127/* Create a new cp_token_cache. */
128
129static cp_token_cache *
130cp_token_cache_new ()
131{
132 return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
133}
134
135/* Add *TOKEN to *CACHE. */
136
137static void
138cp_token_cache_push_token (cp_token_cache *cache,
139 cp_token *token)
140{
141 cp_token_block *b = cache->last;
142
143 /* See if we need to allocate a new token block. */
144 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
145 {
146 b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
147 b->prev = cache->last;
148 if (cache->last)
149 {
150 cache->last->next = b;
151 cache->last = b;
152 }
153 else
154 cache->first = cache->last = b;
155 }
156 /* Add this token to the current token block. */
157 b->tokens[b->num_tokens++] = *token;
158}
159
160/* The cp_lexer structure represents the C++ lexer. It is responsible
161 for managing the token stream from the preprocessor and supplying
162 it to the parser. */
163
164typedef struct cp_lexer GTY (())
165{
166 /* The memory allocated for the buffer. Never NULL. */
167 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
168 /* A pointer just past the end of the memory allocated for the buffer. */
169 cp_token * GTY ((skip (""))) buffer_end;
170 /* The first valid token in the buffer, or NULL if none. */
171 cp_token * GTY ((skip (""))) first_token;
172 /* The next available token. If NEXT_TOKEN is NULL, then there are
173 no more available tokens. */
174 cp_token * GTY ((skip (""))) next_token;
175 /* A pointer just past the last available token. If FIRST_TOKEN is
176 NULL, however, there are no available tokens, and then this
177 location is simply the place in which the next token read will be
178 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
179 When the LAST_TOKEN == BUFFER, then the last token is at the
180 highest memory address in the BUFFER. */
181 cp_token * GTY ((skip (""))) last_token;
182
183 /* A stack indicating positions at which cp_lexer_save_tokens was
184 called. The top entry is the most recent position at which we
185 began saving tokens. The entries are differences in token
186 position between FIRST_TOKEN and the first saved token.
187
188 If the stack is non-empty, we are saving tokens. When a token is
189 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
190 pointer will not. The token stream will be preserved so that it
191 can be reexamined later.
192
193 If the stack is empty, then we are not saving tokens. Whenever a
194 token is consumed, the FIRST_TOKEN pointer will be moved, and the
195 consumed token will be gone forever. */
196 varray_type saved_tokens;
197
198 /* The STRING_CST tokens encountered while processing the current
199 string literal. */
200 varray_type string_tokens;
201
202 /* True if we should obtain more tokens from the preprocessor; false
203 if we are processing a saved token cache. */
204 bool main_lexer_p;
205
206 /* True if we should output debugging information. */
207 bool debugging_p;
208
209 /* The next lexer in a linked list of lexers. */
210 struct cp_lexer *next;
211} cp_lexer;
212
213/* Prototypes. */
214
17211ab5
GK
215static cp_lexer *cp_lexer_new_main
216 PARAMS ((void));
a723baf1
MM
217static cp_lexer *cp_lexer_new_from_tokens
218 PARAMS ((struct cp_token_cache *));
219static int cp_lexer_saving_tokens
220 PARAMS ((const cp_lexer *));
221static cp_token *cp_lexer_next_token
222 PARAMS ((cp_lexer *, cp_token *));
223static ptrdiff_t cp_lexer_token_difference
224 PARAMS ((cp_lexer *, cp_token *, cp_token *));
225static cp_token *cp_lexer_read_token
226 PARAMS ((cp_lexer *));
227static void cp_lexer_maybe_grow_buffer
228 PARAMS ((cp_lexer *));
229static void cp_lexer_get_preprocessor_token
230 PARAMS ((cp_lexer *, cp_token *));
231static cp_token *cp_lexer_peek_token
232 PARAMS ((cp_lexer *));
233static cp_token *cp_lexer_peek_nth_token
234 PARAMS ((cp_lexer *, size_t));
f7b5ecd9 235static inline bool cp_lexer_next_token_is
a723baf1
MM
236 PARAMS ((cp_lexer *, enum cpp_ttype));
237static bool cp_lexer_next_token_is_not
238 PARAMS ((cp_lexer *, enum cpp_ttype));
239static bool cp_lexer_next_token_is_keyword
240 PARAMS ((cp_lexer *, enum rid));
241static cp_token *cp_lexer_consume_token
242 PARAMS ((cp_lexer *));
243static void cp_lexer_purge_token
244 (cp_lexer *);
245static void cp_lexer_purge_tokens_after
246 (cp_lexer *, cp_token *);
247static void cp_lexer_save_tokens
248 PARAMS ((cp_lexer *));
249static void cp_lexer_commit_tokens
250 PARAMS ((cp_lexer *));
251static void cp_lexer_rollback_tokens
252 PARAMS ((cp_lexer *));
f7b5ecd9 253static inline void cp_lexer_set_source_position_from_token
a723baf1
MM
254 PARAMS ((cp_lexer *, const cp_token *));
255static void cp_lexer_print_token
256 PARAMS ((FILE *, cp_token *));
f7b5ecd9 257static inline bool cp_lexer_debugging_p
a723baf1
MM
258 PARAMS ((cp_lexer *));
259static void cp_lexer_start_debugging
260 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
261static void cp_lexer_stop_debugging
262 PARAMS ((cp_lexer *)) ATTRIBUTE_UNUSED;
263
264/* Manifest constants. */
265
266#define CP_TOKEN_BUFFER_SIZE 5
267#define CP_SAVED_TOKENS_SIZE 5
268
269/* A token type for keywords, as opposed to ordinary identifiers. */
270#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
271
272/* A token type for template-ids. If a template-id is processed while
273 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
274 the value of the CPP_TEMPLATE_ID is whatever was returned by
275 cp_parser_template_id. */
276#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
277
278/* A token type for nested-name-specifiers. If a
279 nested-name-specifier is processed while parsing tentatively, it is
280 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
281 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
282 cp_parser_nested_name_specifier_opt. */
283#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
284
285/* A token type for tokens that are not tokens at all; these are used
286 to mark the end of a token block. */
287#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
288
289/* Variables. */
290
291/* The stream to which debugging output should be written. */
292static FILE *cp_lexer_debug_stream;
293
17211ab5
GK
294/* Create a new main C++ lexer, the lexer that gets tokens from the
295 preprocessor. */
a723baf1
MM
296
297static cp_lexer *
17211ab5 298cp_lexer_new_main (void)
a723baf1
MM
299{
300 cp_lexer *lexer;
17211ab5
GK
301 cp_token first_token;
302
303 /* It's possible that lexing the first token will load a PCH file,
304 which is a GC collection point. So we have to grab the first
305 token before allocating any memory. */
306 cp_lexer_get_preprocessor_token (NULL, &first_token);
307 cpp_get_callbacks (parse_in)->valid_pch = NULL;
a723baf1
MM
308
309 /* Allocate the memory. */
310 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
311
312 /* Create the circular buffer. */
313 lexer->buffer = ((cp_token *)
17211ab5 314 ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token)));
a723baf1
MM
315 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
316
17211ab5
GK
317 /* There is one token in the buffer. */
318 lexer->last_token = lexer->buffer + 1;
319 lexer->first_token = lexer->buffer;
320 lexer->next_token = lexer->buffer;
321 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
a723baf1
MM
322
323 /* This lexer obtains more tokens by calling c_lex. */
17211ab5 324 lexer->main_lexer_p = true;
a723baf1
MM
325
326 /* Create the SAVED_TOKENS stack. */
327 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
328
329 /* Create the STRINGS array. */
330 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
331
332 /* Assume we are not debugging. */
333 lexer->debugging_p = false;
334
335 return lexer;
336}
337
338/* Create a new lexer whose token stream is primed with the TOKENS.
339 When these tokens are exhausted, no new tokens will be read. */
340
341static cp_lexer *
342cp_lexer_new_from_tokens (cp_token_cache *tokens)
343{
344 cp_lexer *lexer;
345 cp_token *token;
346 cp_token_block *block;
347 ptrdiff_t num_tokens;
348
17211ab5
GK
349 /* Allocate the memory. */
350 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
a723baf1
MM
351
352 /* Create a new buffer, appropriately sized. */
353 num_tokens = 0;
354 for (block = tokens->first; block != NULL; block = block->next)
355 num_tokens += block->num_tokens;
17211ab5 356 lexer->buffer = ((cp_token *) ggc_alloc (num_tokens * sizeof (cp_token)));
a723baf1
MM
357 lexer->buffer_end = lexer->buffer + num_tokens;
358
359 /* Install the tokens. */
360 token = lexer->buffer;
361 for (block = tokens->first; block != NULL; block = block->next)
362 {
363 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
364 token += block->num_tokens;
365 }
366
367 /* The FIRST_TOKEN is the beginning of the buffer. */
368 lexer->first_token = lexer->buffer;
369 /* The next available token is also at the beginning of the buffer. */
370 lexer->next_token = lexer->buffer;
371 /* The buffer is full. */
372 lexer->last_token = lexer->first_token;
373
17211ab5
GK
374 /* This lexer doesn't obtain more tokens. */
375 lexer->main_lexer_p = false;
376
377 /* Create the SAVED_TOKENS stack. */
378 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
379
380 /* Create the STRINGS array. */
381 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
382
383 /* Assume we are not debugging. */
384 lexer->debugging_p = false;
385
a723baf1
MM
386 return lexer;
387}
388
f7b5ecd9 389/* Returns non-zero if debugging information should be output. */
a723baf1 390
f7b5ecd9
MM
391static inline bool
392cp_lexer_debugging_p (cp_lexer *lexer)
a723baf1 393{
f7b5ecd9
MM
394 return lexer->debugging_p;
395}
396
397/* Set the current source position from the information stored in
398 TOKEN. */
399
400static inline void
401cp_lexer_set_source_position_from_token (lexer, token)
402 cp_lexer *lexer ATTRIBUTE_UNUSED;
403 const cp_token *token;
404{
405 /* Ideally, the source position information would not be a global
406 variable, but it is. */
407
408 /* Update the line number. */
409 if (token->type != CPP_EOF)
410 {
411 lineno = token->line_number;
412 input_filename = token->file_name;
413 }
a723baf1
MM
414}
415
416/* TOKEN points into the circular token buffer. Return a pointer to
417 the next token in the buffer. */
418
f7b5ecd9 419static inline cp_token *
a723baf1
MM
420cp_lexer_next_token (lexer, token)
421 cp_lexer *lexer;
422 cp_token *token;
423{
424 token++;
425 if (token == lexer->buffer_end)
426 token = lexer->buffer;
427 return token;
428}
429
f7b5ecd9
MM
430/* Non-zero if we are presently saving tokens. */
431
432static int
433cp_lexer_saving_tokens (lexer)
434 const cp_lexer *lexer;
435{
436 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
437}
438
a723baf1
MM
439/* Return a pointer to the token that is N tokens beyond TOKEN in the
440 buffer. */
441
442static cp_token *
443cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
444{
445 token += n;
446 if (token >= lexer->buffer_end)
447 token = lexer->buffer + (token - lexer->buffer_end);
448 return token;
449}
450
451/* Returns the number of times that START would have to be incremented
452 to reach FINISH. If START and FINISH are the same, returns zero. */
453
454static ptrdiff_t
455cp_lexer_token_difference (lexer, start, finish)
456 cp_lexer *lexer;
457 cp_token *start;
458 cp_token *finish;
459{
460 if (finish >= start)
461 return finish - start;
462 else
463 return ((lexer->buffer_end - lexer->buffer)
464 - (start - finish));
465}
466
467/* Obtain another token from the C preprocessor and add it to the
468 token buffer. Returns the newly read token. */
469
470static cp_token *
471cp_lexer_read_token (lexer)
472 cp_lexer *lexer;
473{
474 cp_token *token;
475
476 /* Make sure there is room in the buffer. */
477 cp_lexer_maybe_grow_buffer (lexer);
478
479 /* If there weren't any tokens, then this one will be the first. */
480 if (!lexer->first_token)
481 lexer->first_token = lexer->last_token;
482 /* Similarly, if there were no available tokens, there is one now. */
483 if (!lexer->next_token)
484 lexer->next_token = lexer->last_token;
485
486 /* Figure out where we're going to store the new token. */
487 token = lexer->last_token;
488
489 /* Get a new token from the preprocessor. */
490 cp_lexer_get_preprocessor_token (lexer, token);
491
492 /* Increment LAST_TOKEN. */
493 lexer->last_token = cp_lexer_next_token (lexer, token);
494
495 /* The preprocessor does not yet do translation phase six, i.e., the
496 combination of adjacent string literals. Therefore, we do it
497 here. */
498 if (token->type == CPP_STRING || token->type == CPP_WSTRING)
499 {
500 ptrdiff_t delta;
501 int i;
502
503 /* When we grow the buffer, we may invalidate TOKEN. So, save
504 the distance from the beginning of the BUFFER so that we can
505 recaulate it. */
506 delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
507 /* Make sure there is room in the buffer for another token. */
508 cp_lexer_maybe_grow_buffer (lexer);
509 /* Restore TOKEN. */
510 token = lexer->buffer;
511 for (i = 0; i < delta; ++i)
512 token = cp_lexer_next_token (lexer, token);
513
514 VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
515 while (true)
516 {
517 /* Read the token after TOKEN. */
518 cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
519 /* See whether it's another string constant. */
520 if (lexer->last_token->type != token->type)
521 {
522 /* If not, then it will be the next real token. */
523 lexer->last_token = cp_lexer_next_token (lexer,
524 lexer->last_token);
525 break;
526 }
527
528 /* Chain the strings together. */
529 VARRAY_PUSH_TREE (lexer->string_tokens,
530 lexer->last_token->value);
531 }
532
533 /* Create a single STRING_CST. Curiously we have to call
534 combine_strings even if there is only a single string in
535 order to get the type set correctly. */
536 token->value = combine_strings (lexer->string_tokens);
537 VARRAY_CLEAR (lexer->string_tokens);
538 token->value = fix_string_type (token->value);
539 /* Strings should have type `const char []'. Right now, we will
540 have an ARRAY_TYPE that is constant rather than an array of
541 constant elements. */
542 if (flag_const_strings)
543 {
544 tree type;
545
546 /* Get the current type. It will be an ARRAY_TYPE. */
547 type = TREE_TYPE (token->value);
548 /* Use build_cplus_array_type to rebuild the array, thereby
549 getting the right type. */
550 type = build_cplus_array_type (TREE_TYPE (type),
551 TYPE_DOMAIN (type));
552 /* Reset the type of the token. */
553 TREE_TYPE (token->value) = type;
554 }
555 }
556
557 return token;
558}
559
560/* If the circular buffer is full, make it bigger. */
561
562static void
563cp_lexer_maybe_grow_buffer (lexer)
564 cp_lexer *lexer;
565{
566 /* If the buffer is full, enlarge it. */
567 if (lexer->last_token == lexer->first_token)
568 {
569 cp_token *new_buffer;
570 cp_token *old_buffer;
571 cp_token *new_first_token;
572 ptrdiff_t buffer_length;
573 size_t num_tokens_to_copy;
574
575 /* Remember the current buffer pointer. It will become invalid,
576 but we will need to do pointer arithmetic involving this
577 value. */
578 old_buffer = lexer->buffer;
579 /* Compute the current buffer size. */
580 buffer_length = lexer->buffer_end - lexer->buffer;
581 /* Allocate a buffer twice as big. */
582 new_buffer = ((cp_token *)
583 ggc_realloc (lexer->buffer,
584 2 * buffer_length * sizeof (cp_token)));
585
586 /* Because the buffer is circular, logically consecutive tokens
587 are not necessarily placed consecutively in memory.
588 Therefore, we must keep move the tokens that were before
589 FIRST_TOKEN to the second half of the newly allocated
590 buffer. */
591 num_tokens_to_copy = (lexer->first_token - old_buffer);
592 memcpy (new_buffer + buffer_length,
593 new_buffer,
594 num_tokens_to_copy * sizeof (cp_token));
595 /* Clear the rest of the buffer. We never look at this storage,
596 but the garbage collector may. */
597 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
598 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
599
600 /* Now recompute all of the buffer pointers. */
601 new_first_token
602 = new_buffer + (lexer->first_token - old_buffer);
603 if (lexer->next_token != NULL)
604 {
605 ptrdiff_t next_token_delta;
606
607 if (lexer->next_token > lexer->first_token)
608 next_token_delta = lexer->next_token - lexer->first_token;
609 else
610 next_token_delta =
611 buffer_length - (lexer->first_token - lexer->next_token);
612 lexer->next_token = new_first_token + next_token_delta;
613 }
614 lexer->last_token = new_first_token + buffer_length;
615 lexer->buffer = new_buffer;
616 lexer->buffer_end = new_buffer + buffer_length * 2;
617 lexer->first_token = new_first_token;
618 }
619}
620
621/* Store the next token from the preprocessor in *TOKEN. */
622
623static void
624cp_lexer_get_preprocessor_token (lexer, token)
625 cp_lexer *lexer ATTRIBUTE_UNUSED;
626 cp_token *token;
627{
628 bool done;
629
630 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 631 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
632 {
633 token->type = CPP_EOF;
634 token->line_number = 0;
635 token->file_name = NULL;
636 token->value = NULL_TREE;
637 token->keyword = RID_MAX;
638
639 return;
640 }
641
642 done = false;
643 /* Keep going until we get a token we like. */
644 while (!done)
645 {
646 /* Get a new token from the preprocessor. */
647 token->type = c_lex (&token->value);
648 /* Issue messages about tokens we cannot process. */
649 switch (token->type)
650 {
651 case CPP_ATSIGN:
652 case CPP_HASH:
653 case CPP_PASTE:
654 error ("invalid token");
655 break;
656
657 case CPP_OTHER:
658 /* These tokens are already warned about by c_lex. */
659 break;
660
661 default:
662 /* This is a good token, so we exit the loop. */
663 done = true;
664 break;
665 }
666 }
667 /* Now we've got our token. */
668 token->line_number = lineno;
669 token->file_name = input_filename;
670
671 /* Check to see if this token is a keyword. */
672 if (token->type == CPP_NAME
673 && C_IS_RESERVED_WORD (token->value))
674 {
675 /* Mark this token as a keyword. */
676 token->type = CPP_KEYWORD;
677 /* Record which keyword. */
678 token->keyword = C_RID_CODE (token->value);
679 /* Update the value. Some keywords are mapped to particular
680 entities, rather than simply having the value of the
681 corresponding IDENTIFIER_NODE. For example, `__const' is
682 mapped to `const'. */
683 token->value = ridpointers[token->keyword];
684 }
685 else
686 token->keyword = RID_MAX;
687}
688
689/* Return a pointer to the next token in the token stream, but do not
690 consume it. */
691
692static cp_token *
693cp_lexer_peek_token (lexer)
694 cp_lexer *lexer;
695{
696 cp_token *token;
697
698 /* If there are no tokens, read one now. */
699 if (!lexer->next_token)
700 cp_lexer_read_token (lexer);
701
702 /* Provide debugging output. */
703 if (cp_lexer_debugging_p (lexer))
704 {
705 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
706 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
707 fprintf (cp_lexer_debug_stream, "\n");
708 }
709
710 token = lexer->next_token;
711 cp_lexer_set_source_position_from_token (lexer, token);
712 return token;
713}
714
715/* Return true if the next token has the indicated TYPE. */
716
717static bool
718cp_lexer_next_token_is (lexer, type)
719 cp_lexer *lexer;
720 enum cpp_ttype type;
721{
722 cp_token *token;
723
724 /* Peek at the next token. */
725 token = cp_lexer_peek_token (lexer);
726 /* Check to see if it has the indicated TYPE. */
727 return token->type == type;
728}
729
730/* Return true if the next token does not have the indicated TYPE. */
731
732static bool
733cp_lexer_next_token_is_not (lexer, type)
734 cp_lexer *lexer;
735 enum cpp_ttype type;
736{
737 return !cp_lexer_next_token_is (lexer, type);
738}
739
740/* Return true if the next token is the indicated KEYWORD. */
741
742static bool
743cp_lexer_next_token_is_keyword (lexer, keyword)
744 cp_lexer *lexer;
745 enum rid keyword;
746{
747 cp_token *token;
748
749 /* Peek at the next token. */
750 token = cp_lexer_peek_token (lexer);
751 /* Check to see if it is the indicated keyword. */
752 return token->keyword == keyword;
753}
754
755/* Return a pointer to the Nth token in the token stream. If N is 1,
756 then this is precisely equivalent to cp_lexer_peek_token. */
757
758static cp_token *
759cp_lexer_peek_nth_token (lexer, n)
760 cp_lexer *lexer;
761 size_t n;
762{
763 cp_token *token;
764
765 /* N is 1-based, not zero-based. */
766 my_friendly_assert (n > 0, 20000224);
767
768 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
769 token = lexer->next_token;
770 /* If there are no tokens in the buffer, get one now. */
771 if (!token)
772 {
773 cp_lexer_read_token (lexer);
774 token = lexer->next_token;
775 }
776
777 /* Now, read tokens until we have enough. */
778 while (--n > 0)
779 {
780 /* Advance to the next token. */
781 token = cp_lexer_next_token (lexer, token);
782 /* If that's all the tokens we have, read a new one. */
783 if (token == lexer->last_token)
784 token = cp_lexer_read_token (lexer);
785 }
786
787 return token;
788}
789
790/* Consume the next token. The pointer returned is valid only until
791 another token is read. Callers should preserve copy the token
792 explicitly if they will need its value for a longer period of
793 time. */
794
795static cp_token *
796cp_lexer_consume_token (lexer)
797 cp_lexer *lexer;
798{
799 cp_token *token;
800
801 /* If there are no tokens, read one now. */
802 if (!lexer->next_token)
803 cp_lexer_read_token (lexer);
804
805 /* Remember the token we'll be returning. */
806 token = lexer->next_token;
807
808 /* Increment NEXT_TOKEN. */
809 lexer->next_token = cp_lexer_next_token (lexer,
810 lexer->next_token);
811 /* Check to see if we're all out of tokens. */
812 if (lexer->next_token == lexer->last_token)
813 lexer->next_token = NULL;
814
815 /* If we're not saving tokens, then move FIRST_TOKEN too. */
816 if (!cp_lexer_saving_tokens (lexer))
817 {
818 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
819 if (!lexer->next_token)
820 lexer->first_token = NULL;
821 else
822 lexer->first_token = lexer->next_token;
823 }
824
825 /* Provide debugging output. */
826 if (cp_lexer_debugging_p (lexer))
827 {
828 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
829 cp_lexer_print_token (cp_lexer_debug_stream, token);
830 fprintf (cp_lexer_debug_stream, "\n");
831 }
832
833 return token;
834}
835
836/* Permanently remove the next token from the token stream. There
837 must be a valid next token already; this token never reads
838 additional tokens from the preprocessor. */
839
840static void
841cp_lexer_purge_token (cp_lexer *lexer)
842{
843 cp_token *token;
844 cp_token *next_token;
845
846 token = lexer->next_token;
847 while (true)
848 {
849 next_token = cp_lexer_next_token (lexer, token);
850 if (next_token == lexer->last_token)
851 break;
852 *token = *next_token;
853 token = next_token;
854 }
855
856 lexer->last_token = token;
857 /* The token purged may have been the only token remaining; if so,
858 clear NEXT_TOKEN. */
859 if (lexer->next_token == token)
860 lexer->next_token = NULL;
861}
862
863/* Permanently remove all tokens after TOKEN, up to, but not
864 including, the token that will be returned next by
865 cp_lexer_peek_token. */
866
867static void
868cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
869{
870 cp_token *peek;
871 cp_token *t1;
872 cp_token *t2;
873
874 if (lexer->next_token)
875 {
876 /* Copy the tokens that have not yet been read to the location
877 immediately following TOKEN. */
878 t1 = cp_lexer_next_token (lexer, token);
879 t2 = peek = cp_lexer_peek_token (lexer);
880 /* Move tokens into the vacant area between TOKEN and PEEK. */
881 while (t2 != lexer->last_token)
882 {
883 *t1 = *t2;
884 t1 = cp_lexer_next_token (lexer, t1);
885 t2 = cp_lexer_next_token (lexer, t2);
886 }
887 /* Now, the next available token is right after TOKEN. */
888 lexer->next_token = cp_lexer_next_token (lexer, token);
889 /* And the last token is wherever we ended up. */
890 lexer->last_token = t1;
891 }
892 else
893 {
894 /* There are no tokens in the buffer, so there is nothing to
895 copy. The last token in the buffer is TOKEN itself. */
896 lexer->last_token = cp_lexer_next_token (lexer, token);
897 }
898}
899
900/* Begin saving tokens. All tokens consumed after this point will be
901 preserved. */
902
903static void
904cp_lexer_save_tokens (lexer)
905 cp_lexer *lexer;
906{
907 /* Provide debugging output. */
908 if (cp_lexer_debugging_p (lexer))
909 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
910
911 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
912 restore the tokens if required. */
913 if (!lexer->next_token)
914 cp_lexer_read_token (lexer);
915
916 VARRAY_PUSH_INT (lexer->saved_tokens,
917 cp_lexer_token_difference (lexer,
918 lexer->first_token,
919 lexer->next_token));
920}
921
922/* Commit to the portion of the token stream most recently saved. */
923
924static void
925cp_lexer_commit_tokens (lexer)
926 cp_lexer *lexer;
927{
928 /* Provide debugging output. */
929 if (cp_lexer_debugging_p (lexer))
930 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
931
932 VARRAY_POP (lexer->saved_tokens);
933}
934
935/* Return all tokens saved since the last call to cp_lexer_save_tokens
936 to the token stream. Stop saving tokens. */
937
938static void
939cp_lexer_rollback_tokens (lexer)
940 cp_lexer *lexer;
941{
942 size_t delta;
943
944 /* Provide debugging output. */
945 if (cp_lexer_debugging_p (lexer))
946 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
947
948 /* Find the token that was the NEXT_TOKEN when we started saving
949 tokens. */
950 delta = VARRAY_TOP_INT(lexer->saved_tokens);
951 /* Make it the next token again now. */
952 lexer->next_token = cp_lexer_advance_token (lexer,
953 lexer->first_token,
954 delta);
15d2cb19 955 /* It might be the case that there were no tokens when we started
a723baf1
MM
956 saving tokens, but that there are some tokens now. */
957 if (!lexer->next_token && lexer->first_token)
958 lexer->next_token = lexer->first_token;
959
960 /* Stop saving tokens. */
961 VARRAY_POP (lexer->saved_tokens);
962}
963
a723baf1
MM
964/* Print a representation of the TOKEN on the STREAM. */
965
966static void
967cp_lexer_print_token (stream, token)
968 FILE *stream;
969 cp_token *token;
970{
971 const char *token_type = NULL;
972
973 /* Figure out what kind of token this is. */
974 switch (token->type)
975 {
976 case CPP_EQ:
977 token_type = "EQ";
978 break;
979
980 case CPP_COMMA:
981 token_type = "COMMA";
982 break;
983
984 case CPP_OPEN_PAREN:
985 token_type = "OPEN_PAREN";
986 break;
987
988 case CPP_CLOSE_PAREN:
989 token_type = "CLOSE_PAREN";
990 break;
991
992 case CPP_OPEN_BRACE:
993 token_type = "OPEN_BRACE";
994 break;
995
996 case CPP_CLOSE_BRACE:
997 token_type = "CLOSE_BRACE";
998 break;
999
1000 case CPP_SEMICOLON:
1001 token_type = "SEMICOLON";
1002 break;
1003
1004 case CPP_NAME:
1005 token_type = "NAME";
1006 break;
1007
1008 case CPP_EOF:
1009 token_type = "EOF";
1010 break;
1011
1012 case CPP_KEYWORD:
1013 token_type = "keyword";
1014 break;
1015
1016 /* This is not a token that we know how to handle yet. */
1017 default:
1018 break;
1019 }
1020
1021 /* If we have a name for the token, print it out. Otherwise, we
1022 simply give the numeric code. */
1023 if (token_type)
1024 fprintf (stream, "%s", token_type);
1025 else
1026 fprintf (stream, "%d", token->type);
1027 /* And, for an identifier, print the identifier name. */
1028 if (token->type == CPP_NAME
1029 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1030 For example, `struct' is mapped to an INTEGER_CST. */
1031 || (token->type == CPP_KEYWORD
1032 && TREE_CODE (token->value) == IDENTIFIER_NODE))
1033 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1034}
1035
a723baf1
MM
1036/* Start emitting debugging information. */
1037
1038static void
1039cp_lexer_start_debugging (lexer)
1040 cp_lexer *lexer;
1041{
1042 ++lexer->debugging_p;
1043}
1044
1045/* Stop emitting debugging information. */
1046
1047static void
1048cp_lexer_stop_debugging (lexer)
1049 cp_lexer *lexer;
1050{
1051 --lexer->debugging_p;
1052}
1053
1054\f
1055/* The parser. */
1056
1057/* Overview
1058 --------
1059
1060 A cp_parser parses the token stream as specified by the C++
1061 grammar. Its job is purely parsing, not semantic analysis. For
1062 example, the parser breaks the token stream into declarators,
1063 expressions, statements, and other similar syntactic constructs.
1064 It does not check that the types of the expressions on either side
1065 of an assignment-statement are compatible, or that a function is
1066 not declared with a parameter of type `void'.
1067
1068 The parser invokes routines elsewhere in the compiler to perform
1069 semantic analysis and to build up the abstract syntax tree for the
1070 code processed.
1071
1072 The parser (and the template instantiation code, which is, in a
1073 way, a close relative of parsing) are the only parts of the
1074 compiler that should be calling push_scope and pop_scope, or
1075 related functions. The parser (and template instantiation code)
1076 keeps track of what scope is presently active; everything else
1077 should simply honor that. (The code that generates static
1078 initializers may also need to set the scope, in order to check
1079 access control correctly when emitting the initializers.)
1080
1081 Methodology
1082 -----------
1083
1084 The parser is of the standard recursive-descent variety. Upcoming
1085 tokens in the token stream are examined in order to determine which
1086 production to use when parsing a non-terminal. Some C++ constructs
1087 require arbitrary look ahead to disambiguate. For example, it is
1088 impossible, in the general case, to tell whether a statement is an
1089 expression or declaration without scanning the entire statement.
1090 Therefore, the parser is capable of "parsing tentatively." When the
1091 parser is not sure what construct comes next, it enters this mode.
1092 Then, while we attempt to parse the construct, the parser queues up
1093 error messages, rather than issuing them immediately, and saves the
1094 tokens it consumes. If the construct is parsed successfully, the
1095 parser "commits", i.e., it issues any queued error messages and
1096 the tokens that were being preserved are permanently discarded.
1097 If, however, the construct is not parsed successfully, the parser
1098 rolls back its state completely so that it can resume parsing using
1099 a different alternative.
1100
1101 Future Improvements
1102 -------------------
1103
1104 The performance of the parser could probably be improved
1105 substantially. Some possible improvements include:
1106
1107 - The expression parser recurses through the various levels of
1108 precedence as specified in the grammar, rather than using an
1109 operator-precedence technique. Therefore, parsing a simple
1110 identifier requires multiple recursive calls.
1111
1112 - We could often eliminate the need to parse tentatively by
1113 looking ahead a little bit. In some places, this approach
1114 might not entirely eliminate the need to parse tentatively, but
1115 it might still speed up the average case. */
1116
1117/* Flags that are passed to some parsing functions. These values can
1118 be bitwise-ored together. */
1119
1120typedef enum cp_parser_flags
1121{
1122 /* No flags. */
1123 CP_PARSER_FLAGS_NONE = 0x0,
1124 /* The construct is optional. If it is not present, then no error
1125 should be issued. */
1126 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1127 /* When parsing a type-specifier, do not allow user-defined types. */
1128 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1129} cp_parser_flags;
1130
1131/* The different kinds of ids that we ecounter. */
1132
1133typedef enum cp_parser_id_kind
1134{
1135 /* Not an id at all. */
1136 CP_PARSER_ID_KIND_NONE,
1137 /* An unqualified-id that is not a template-id. */
1138 CP_PARSER_ID_KIND_UNQUALIFIED,
1139 /* An unqualified template-id. */
1140 CP_PARSER_ID_KIND_TEMPLATE_ID,
1141 /* A qualified-id. */
1142 CP_PARSER_ID_KIND_QUALIFIED
1143} cp_parser_id_kind;
1144
62b8a44e
NS
1145/* The different kinds of declarators we want to parse. */
1146
1147typedef enum cp_parser_declarator_kind
1148{
1149 /* We want an abstract declartor. */
1150 CP_PARSER_DECLARATOR_ABSTRACT,
1151 /* We want a named declarator. */
1152 CP_PARSER_DECLARATOR_NAMED,
1153 /* We don't mind. */
1154 CP_PARSER_DECLARATOR_EITHER
1155} cp_parser_declarator_kind;
1156
a723baf1
MM
1157/* A mapping from a token type to a corresponding tree node type. */
1158
1159typedef struct cp_parser_token_tree_map_node
1160{
1161 /* The token type. */
1162 enum cpp_ttype token_type;
1163 /* The corresponding tree code. */
1164 enum tree_code tree_type;
1165} cp_parser_token_tree_map_node;
1166
1167/* A complete map consists of several ordinary entries, followed by a
1168 terminator. The terminating entry has a token_type of CPP_EOF. */
1169
1170typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1171
1172/* The status of a tentative parse. */
1173
1174typedef enum cp_parser_status_kind
1175{
1176 /* No errors have occurred. */
1177 CP_PARSER_STATUS_KIND_NO_ERROR,
1178 /* An error has occurred. */
1179 CP_PARSER_STATUS_KIND_ERROR,
1180 /* We are committed to this tentative parse, whether or not an error
1181 has occurred. */
1182 CP_PARSER_STATUS_KIND_COMMITTED
1183} cp_parser_status_kind;
1184
1185/* Context that is saved and restored when parsing tentatively. */
1186
1187typedef struct cp_parser_context GTY (())
1188{
1189 /* If this is a tentative parsing context, the status of the
1190 tentative parse. */
1191 enum cp_parser_status_kind status;
1192 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1193 that are looked up in this context must be looked up both in the
1194 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1195 the context of the containing expression. */
1196 tree object_type;
1197 /* A TREE_LIST representing name-lookups for which we have deferred
1198 checking access controls. We cannot check the accessibility of
1199 names used in a decl-specifier-seq until we know what is being
1200 declared because code like:
1201
1202 class A {
1203 class B {};
1204 B* f();
1205 }
1206
1207 A::B* A::f() { return 0; }
1208
1209 is valid, even though `A::B' is not generally accessible.
1210
1211 The TREE_PURPOSE of each node is the scope used to qualify the
1212 name being looked up; the TREE_VALUE is the DECL to which the
1213 name was resolved. */
1214 tree deferred_access_checks;
1215 /* TRUE iff we are deferring access checks. */
1216 bool deferring_access_checks_p;
1217 /* The next parsing context in the stack. */
1218 struct cp_parser_context *next;
1219} cp_parser_context;
1220
1221/* Prototypes. */
1222
1223/* Constructors and destructors. */
1224
1225static cp_parser_context *cp_parser_context_new
1226 PARAMS ((cp_parser_context *));
1227
e5976695
MM
1228/* Class variables. */
1229
92bc1323 1230static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
e5976695 1231
a723baf1
MM
1232/* Constructors and destructors. */
1233
1234/* Construct a new context. The context below this one on the stack
1235 is given by NEXT. */
1236
1237static cp_parser_context *
1238cp_parser_context_new (next)
1239 cp_parser_context *next;
1240{
1241 cp_parser_context *context;
1242
1243 /* Allocate the storage. */
e5976695
MM
1244 if (cp_parser_context_free_list != NULL)
1245 {
1246 /* Pull the first entry from the free list. */
1247 context = cp_parser_context_free_list;
1248 cp_parser_context_free_list = context->next;
1249 memset ((char *)context, 0, sizeof (*context));
1250 }
1251 else
1252 context = ((cp_parser_context *)
1253 ggc_alloc_cleared (sizeof (cp_parser_context)));
a723baf1
MM
1254 /* No errors have occurred yet in this context. */
1255 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1256 /* If this is not the bottomost context, copy information that we
1257 need from the previous context. */
1258 if (next)
1259 {
1260 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1261 expression, then we are parsing one in this context, too. */
1262 context->object_type = next->object_type;
1263 /* We are deferring access checks here if we were in the NEXT
1264 context. */
1265 context->deferring_access_checks_p
1266 = next->deferring_access_checks_p;
1267 /* Thread the stack. */
1268 context->next = next;
1269 }
1270
1271 return context;
1272}
1273
1274/* The cp_parser structure represents the C++ parser. */
1275
1276typedef struct cp_parser GTY(())
1277{
1278 /* The lexer from which we are obtaining tokens. */
1279 cp_lexer *lexer;
1280
1281 /* The scope in which names should be looked up. If NULL_TREE, then
1282 we look up names in the scope that is currently open in the
1283 source program. If non-NULL, this is either a TYPE or
1284 NAMESPACE_DECL for the scope in which we should look.
1285
1286 This value is not cleared automatically after a name is looked
1287 up, so we must be careful to clear it before starting a new look
1288 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1289 will look up `Z' in the scope of `X', rather than the current
1290 scope.) Unfortunately, it is difficult to tell when name lookup
1291 is complete, because we sometimes peek at a token, look it up,
1292 and then decide not to consume it. */
1293 tree scope;
1294
1295 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1296 last lookup took place. OBJECT_SCOPE is used if an expression
1297 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1298 respectively. QUALIFYING_SCOPE is used for an expression of the
1299 form "X::Y"; it refers to X. */
1300 tree object_scope;
1301 tree qualifying_scope;
1302
1303 /* A stack of parsing contexts. All but the bottom entry on the
1304 stack will be tentative contexts.
1305
1306 We parse tentatively in order to determine which construct is in
1307 use in some situations. For example, in order to determine
1308 whether a statement is an expression-statement or a
1309 declaration-statement we parse it tentatively as a
1310 declaration-statement. If that fails, we then reparse the same
1311 token stream as an expression-statement. */
1312 cp_parser_context *context;
1313
1314 /* True if we are parsing GNU C++. If this flag is not set, then
1315 GNU extensions are not recognized. */
1316 bool allow_gnu_extensions_p;
1317
1318 /* TRUE if the `>' token should be interpreted as the greater-than
1319 operator. FALSE if it is the end of a template-id or
1320 template-parameter-list. */
1321 bool greater_than_is_operator_p;
1322
1323 /* TRUE if default arguments are allowed within a parameter list
1324 that starts at this point. FALSE if only a gnu extension makes
1325 them permissable. */
1326 bool default_arg_ok_p;
1327
1328 /* TRUE if we are parsing an integral constant-expression. See
1329 [expr.const] for a precise definition. */
1330 /* FIXME: Need to implement code that checks this flag. */
1331 bool constant_expression_p;
1332
1333 /* TRUE if local variable names and `this' are forbidden in the
1334 current context. */
1335 bool local_variables_forbidden_p;
1336
1337 /* TRUE if the declaration we are parsing is part of a
1338 linkage-specification of the form `extern string-literal
1339 declaration'. */
1340 bool in_unbraced_linkage_specification_p;
1341
1342 /* TRUE if we are presently parsing a declarator, after the
1343 direct-declarator. */
1344 bool in_declarator_p;
1345
1346 /* If non-NULL, then we are parsing a construct where new type
1347 definitions are not permitted. The string stored here will be
1348 issued as an error message if a type is defined. */
1349 const char *type_definition_forbidden_message;
1350
a723baf1
MM
1351 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1352 but may not have been parsed. These functions are friends of
1353 members defined within a class-specification; they are not
1354 procssed until the class is complete. The active queue is at the
1355 front of the list.
1356
1357 Within each queue, functions appear in the reverse order that
8218bd34
MM
1358 they appeared in the source. Each TREE_VALUE is a
1359 FUNCTION_DECL of TEMPLATE_DECL corresponding to a member
1360 function. */
a723baf1
MM
1361 tree unparsed_functions_queues;
1362
1363 /* The number of classes whose definitions are currently in
1364 progress. */
1365 unsigned num_classes_being_defined;
1366
1367 /* The number of template parameter lists that apply directly to the
1368 current declaration. */
1369 unsigned num_template_parameter_lists;
24c0ef37
GS
1370
1371 /* List of access checks lists, used to prevent GC collection while
1372 they are in use. */
1373 tree access_checks_lists;
a723baf1
MM
1374} cp_parser;
1375
1376/* The type of a function that parses some kind of expression */
1377typedef tree (*cp_parser_expression_fn) PARAMS ((cp_parser *));
1378
1379/* Prototypes. */
1380
1381/* Constructors and destructors. */
1382
1383static cp_parser *cp_parser_new
1384 PARAMS ((void));
1385
1386/* Routines to parse various constructs.
1387
1388 Those that return `tree' will return the error_mark_node (rather
1389 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1390 Sometimes, they will return an ordinary node if error-recovery was
1391 attempted, even though a parse error occurrred. So, to check
1392 whether or not a parse error occurred, you should always use
1393 cp_parser_error_occurred. If the construct is optional (indicated
1394 either by an `_opt' in the name of the function that does the
1395 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1396 the construct is not present. */
1397
1398/* Lexical conventions [gram.lex] */
1399
1400static tree cp_parser_identifier
1401 PARAMS ((cp_parser *));
1402
1403/* Basic concepts [gram.basic] */
1404
1405static bool cp_parser_translation_unit
1406 PARAMS ((cp_parser *));
1407
1408/* Expressions [gram.expr] */
1409
1410static tree cp_parser_primary_expression
1411 (cp_parser *, cp_parser_id_kind *, tree *);
1412static tree cp_parser_id_expression
1413 PARAMS ((cp_parser *, bool, bool, bool *));
1414static tree cp_parser_unqualified_id
1415 PARAMS ((cp_parser *, bool, bool));
1416static tree cp_parser_nested_name_specifier_opt
1417 (cp_parser *, bool, bool, bool);
1418static tree cp_parser_nested_name_specifier
1419 (cp_parser *, bool, bool, bool);
1420static tree cp_parser_class_or_namespace_name
1421 (cp_parser *, bool, bool, bool, bool);
1422static tree cp_parser_postfix_expression
1423 (cp_parser *, bool);
1424static tree cp_parser_expression_list
1425 PARAMS ((cp_parser *));
1426static void cp_parser_pseudo_destructor_name
1427 PARAMS ((cp_parser *, tree *, tree *));
1428static tree cp_parser_unary_expression
1429 (cp_parser *, bool);
1430static enum tree_code cp_parser_unary_operator
1431 PARAMS ((cp_token *));
1432static tree cp_parser_new_expression
1433 PARAMS ((cp_parser *));
1434static tree cp_parser_new_placement
1435 PARAMS ((cp_parser *));
1436static tree cp_parser_new_type_id
1437 PARAMS ((cp_parser *));
1438static tree cp_parser_new_declarator_opt
1439 PARAMS ((cp_parser *));
1440static tree cp_parser_direct_new_declarator
1441 PARAMS ((cp_parser *));
1442static tree cp_parser_new_initializer
1443 PARAMS ((cp_parser *));
1444static tree cp_parser_delete_expression
1445 PARAMS ((cp_parser *));
1446static tree cp_parser_cast_expression
1447 (cp_parser *, bool);
1448static tree cp_parser_pm_expression
1449 PARAMS ((cp_parser *));
1450static tree cp_parser_multiplicative_expression
1451 PARAMS ((cp_parser *));
1452static tree cp_parser_additive_expression
1453 PARAMS ((cp_parser *));
1454static tree cp_parser_shift_expression
1455 PARAMS ((cp_parser *));
1456static tree cp_parser_relational_expression
1457 PARAMS ((cp_parser *));
1458static tree cp_parser_equality_expression
1459 PARAMS ((cp_parser *));
1460static tree cp_parser_and_expression
1461 PARAMS ((cp_parser *));
1462static tree cp_parser_exclusive_or_expression
1463 PARAMS ((cp_parser *));
1464static tree cp_parser_inclusive_or_expression
1465 PARAMS ((cp_parser *));
1466static tree cp_parser_logical_and_expression
1467 PARAMS ((cp_parser *));
1468static tree cp_parser_logical_or_expression
1469 PARAMS ((cp_parser *));
1470static tree cp_parser_conditional_expression
1471 PARAMS ((cp_parser *));
1472static tree cp_parser_question_colon_clause
1473 PARAMS ((cp_parser *, tree));
1474static tree cp_parser_assignment_expression
1475 PARAMS ((cp_parser *));
1476static enum tree_code cp_parser_assignment_operator_opt
1477 PARAMS ((cp_parser *));
1478static tree cp_parser_expression
1479 PARAMS ((cp_parser *));
1480static tree cp_parser_constant_expression
1481 PARAMS ((cp_parser *));
1482
1483/* Statements [gram.stmt.stmt] */
1484
1485static void cp_parser_statement
1486 PARAMS ((cp_parser *));
1487static tree cp_parser_labeled_statement
1488 PARAMS ((cp_parser *));
1489static tree cp_parser_expression_statement
1490 PARAMS ((cp_parser *));
1491static tree cp_parser_compound_statement
1492 (cp_parser *);
1493static void cp_parser_statement_seq_opt
1494 PARAMS ((cp_parser *));
1495static tree cp_parser_selection_statement
1496 PARAMS ((cp_parser *));
1497static tree cp_parser_condition
1498 PARAMS ((cp_parser *));
1499static tree cp_parser_iteration_statement
1500 PARAMS ((cp_parser *));
1501static void cp_parser_for_init_statement
1502 PARAMS ((cp_parser *));
1503static tree cp_parser_jump_statement
1504 PARAMS ((cp_parser *));
1505static void cp_parser_declaration_statement
1506 PARAMS ((cp_parser *));
1507
1508static tree cp_parser_implicitly_scoped_statement
1509 PARAMS ((cp_parser *));
1510static void cp_parser_already_scoped_statement
1511 PARAMS ((cp_parser *));
1512
1513/* Declarations [gram.dcl.dcl] */
1514
1515static void cp_parser_declaration_seq_opt
1516 PARAMS ((cp_parser *));
1517static void cp_parser_declaration
1518 PARAMS ((cp_parser *));
1519static void cp_parser_block_declaration
1520 PARAMS ((cp_parser *, bool));
1521static void cp_parser_simple_declaration
1522 PARAMS ((cp_parser *, bool));
1523static tree cp_parser_decl_specifier_seq
1524 PARAMS ((cp_parser *, cp_parser_flags, tree *, bool *));
1525static tree cp_parser_storage_class_specifier_opt
1526 PARAMS ((cp_parser *));
1527static tree cp_parser_function_specifier_opt
1528 PARAMS ((cp_parser *));
1529static tree cp_parser_type_specifier
1530 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1531static tree cp_parser_simple_type_specifier
1532 PARAMS ((cp_parser *, cp_parser_flags));
1533static tree cp_parser_type_name
1534 PARAMS ((cp_parser *));
1535static tree cp_parser_elaborated_type_specifier
1536 PARAMS ((cp_parser *, bool, bool));
1537static tree cp_parser_enum_specifier
1538 PARAMS ((cp_parser *));
1539static void cp_parser_enumerator_list
1540 PARAMS ((cp_parser *, tree));
1541static void cp_parser_enumerator_definition
1542 PARAMS ((cp_parser *, tree));
1543static tree cp_parser_namespace_name
1544 PARAMS ((cp_parser *));
1545static void cp_parser_namespace_definition
1546 PARAMS ((cp_parser *));
1547static void cp_parser_namespace_body
1548 PARAMS ((cp_parser *));
1549static tree cp_parser_qualified_namespace_specifier
1550 PARAMS ((cp_parser *));
1551static void cp_parser_namespace_alias_definition
1552 PARAMS ((cp_parser *));
1553static void cp_parser_using_declaration
1554 PARAMS ((cp_parser *));
1555static void cp_parser_using_directive
1556 PARAMS ((cp_parser *));
1557static void cp_parser_asm_definition
1558 PARAMS ((cp_parser *));
1559static void cp_parser_linkage_specification
1560 PARAMS ((cp_parser *));
1561
1562/* Declarators [gram.dcl.decl] */
1563
1564static tree cp_parser_init_declarator
1565 PARAMS ((cp_parser *, tree, tree, tree, bool, bool, bool *));
1566static tree cp_parser_declarator
62b8a44e 1567 PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
a723baf1 1568static tree cp_parser_direct_declarator
62b8a44e 1569 PARAMS ((cp_parser *, cp_parser_declarator_kind, bool *));
a723baf1
MM
1570static enum tree_code cp_parser_ptr_operator
1571 PARAMS ((cp_parser *, tree *, tree *));
1572static tree cp_parser_cv_qualifier_seq_opt
1573 PARAMS ((cp_parser *));
1574static tree cp_parser_cv_qualifier_opt
1575 PARAMS ((cp_parser *));
1576static tree cp_parser_declarator_id
1577 PARAMS ((cp_parser *));
1578static tree cp_parser_type_id
1579 PARAMS ((cp_parser *));
1580static tree cp_parser_type_specifier_seq
1581 PARAMS ((cp_parser *));
1582static tree cp_parser_parameter_declaration_clause
1583 PARAMS ((cp_parser *));
1584static tree cp_parser_parameter_declaration_list
1585 PARAMS ((cp_parser *));
1586static tree cp_parser_parameter_declaration
1587 PARAMS ((cp_parser *, bool));
1588static tree cp_parser_function_definition
1589 PARAMS ((cp_parser *, bool *));
1590static void cp_parser_function_body
1591 (cp_parser *);
1592static tree cp_parser_initializer
1593 PARAMS ((cp_parser *, bool *));
1594static tree cp_parser_initializer_clause
1595 PARAMS ((cp_parser *));
1596static tree cp_parser_initializer_list
1597 PARAMS ((cp_parser *));
1598
1599static bool cp_parser_ctor_initializer_opt_and_function_body
1600 (cp_parser *);
1601
1602/* Classes [gram.class] */
1603
1604static tree cp_parser_class_name
1605 (cp_parser *, bool, bool, bool, bool, bool, bool);
1606static tree cp_parser_class_specifier
1607 PARAMS ((cp_parser *));
1608static tree cp_parser_class_head
1609 PARAMS ((cp_parser *, bool *, bool *, tree *));
1610static enum tag_types cp_parser_class_key
1611 PARAMS ((cp_parser *));
1612static void cp_parser_member_specification_opt
1613 PARAMS ((cp_parser *));
1614static void cp_parser_member_declaration
1615 PARAMS ((cp_parser *));
1616static tree cp_parser_pure_specifier
1617 PARAMS ((cp_parser *));
1618static tree cp_parser_constant_initializer
1619 PARAMS ((cp_parser *));
1620
1621/* Derived classes [gram.class.derived] */
1622
1623static tree cp_parser_base_clause
1624 PARAMS ((cp_parser *));
1625static tree cp_parser_base_specifier
1626 PARAMS ((cp_parser *));
1627
1628/* Special member functions [gram.special] */
1629
1630static tree cp_parser_conversion_function_id
1631 PARAMS ((cp_parser *));
1632static tree cp_parser_conversion_type_id
1633 PARAMS ((cp_parser *));
1634static tree cp_parser_conversion_declarator_opt
1635 PARAMS ((cp_parser *));
1636static bool cp_parser_ctor_initializer_opt
1637 PARAMS ((cp_parser *));
1638static void cp_parser_mem_initializer_list
1639 PARAMS ((cp_parser *));
1640static tree cp_parser_mem_initializer
1641 PARAMS ((cp_parser *));
1642static tree cp_parser_mem_initializer_id
1643 PARAMS ((cp_parser *));
1644
1645/* Overloading [gram.over] */
1646
1647static tree cp_parser_operator_function_id
1648 PARAMS ((cp_parser *));
1649static tree cp_parser_operator
1650 PARAMS ((cp_parser *));
1651
1652/* Templates [gram.temp] */
1653
1654static void cp_parser_template_declaration
1655 PARAMS ((cp_parser *, bool));
1656static tree cp_parser_template_parameter_list
1657 PARAMS ((cp_parser *));
1658static tree cp_parser_template_parameter
1659 PARAMS ((cp_parser *));
1660static tree cp_parser_type_parameter
1661 PARAMS ((cp_parser *));
1662static tree cp_parser_template_id
1663 PARAMS ((cp_parser *, bool, bool));
1664static tree cp_parser_template_name
1665 PARAMS ((cp_parser *, bool, bool));
1666static tree cp_parser_template_argument_list
1667 PARAMS ((cp_parser *));
1668static tree cp_parser_template_argument
1669 PARAMS ((cp_parser *));
1670static void cp_parser_explicit_instantiation
1671 PARAMS ((cp_parser *));
1672static void cp_parser_explicit_specialization
1673 PARAMS ((cp_parser *));
1674
1675/* Exception handling [gram.exception] */
1676
1677static tree cp_parser_try_block
1678 PARAMS ((cp_parser *));
1679static bool cp_parser_function_try_block
1680 PARAMS ((cp_parser *));
1681static void cp_parser_handler_seq
1682 PARAMS ((cp_parser *));
1683static void cp_parser_handler
1684 PARAMS ((cp_parser *));
1685static tree cp_parser_exception_declaration
1686 PARAMS ((cp_parser *));
1687static tree cp_parser_throw_expression
1688 PARAMS ((cp_parser *));
1689static tree cp_parser_exception_specification_opt
1690 PARAMS ((cp_parser *));
1691static tree cp_parser_type_id_list
1692 PARAMS ((cp_parser *));
1693
1694/* GNU Extensions */
1695
1696static tree cp_parser_asm_specification_opt
1697 PARAMS ((cp_parser *));
1698static tree cp_parser_asm_operand_list
1699 PARAMS ((cp_parser *));
1700static tree cp_parser_asm_clobber_list
1701 PARAMS ((cp_parser *));
1702static tree cp_parser_attributes_opt
1703 PARAMS ((cp_parser *));
1704static tree cp_parser_attribute_list
1705 PARAMS ((cp_parser *));
1706static bool cp_parser_extension_opt
1707 PARAMS ((cp_parser *, int *));
1708static void cp_parser_label_declaration
1709 PARAMS ((cp_parser *));
1710
1711/* Utility Routines */
1712
1713static tree cp_parser_lookup_name
eea9800f 1714 PARAMS ((cp_parser *, tree, bool, bool, bool, bool));
a723baf1
MM
1715static tree cp_parser_lookup_name_simple
1716 PARAMS ((cp_parser *, tree));
1717static tree cp_parser_resolve_typename_type
1718 PARAMS ((cp_parser *, tree));
1719static tree cp_parser_maybe_treat_template_as_class
1720 (tree, bool);
1721static bool cp_parser_check_declarator_template_parameters
1722 PARAMS ((cp_parser *, tree));
1723static bool cp_parser_check_template_parameters
1724 PARAMS ((cp_parser *, unsigned));
1725static tree cp_parser_binary_expression
1726 PARAMS ((cp_parser *,
39b1af70 1727 const cp_parser_token_tree_map,
a723baf1
MM
1728 cp_parser_expression_fn));
1729static tree cp_parser_global_scope_opt
1730 PARAMS ((cp_parser *, bool));
1731static bool cp_parser_constructor_declarator_p
1732 (cp_parser *, bool);
1733static tree cp_parser_function_definition_from_specifiers_and_declarator
1734 PARAMS ((cp_parser *, tree, tree, tree, tree));
1735static tree cp_parser_function_definition_after_declarator
1736 PARAMS ((cp_parser *, bool));
1737static void cp_parser_template_declaration_after_export
1738 PARAMS ((cp_parser *, bool));
1739static tree cp_parser_single_declaration
1740 PARAMS ((cp_parser *, bool, bool *));
1741static tree cp_parser_functional_cast
1742 PARAMS ((cp_parser *, tree));
1743static void cp_parser_late_parsing_for_member
1744 PARAMS ((cp_parser *, tree));
1745static void cp_parser_late_parsing_default_args
8218bd34 1746 (cp_parser *, tree);
a723baf1
MM
1747static tree cp_parser_sizeof_operand
1748 PARAMS ((cp_parser *, enum rid));
1749static bool cp_parser_declares_only_class_p
1750 PARAMS ((cp_parser *));
1751static bool cp_parser_friend_p
1752 PARAMS ((tree));
1753static cp_token *cp_parser_require
1754 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1755static cp_token *cp_parser_require_keyword
1756 PARAMS ((cp_parser *, enum rid, const char *));
1757static bool cp_parser_token_starts_function_definition_p
1758 PARAMS ((cp_token *));
1759static bool cp_parser_next_token_starts_class_definition_p
1760 (cp_parser *);
1761static enum tag_types cp_parser_token_is_class_key
1762 PARAMS ((cp_token *));
1763static void cp_parser_check_class_key
1764 (enum tag_types, tree type);
1765static bool cp_parser_optional_template_keyword
1766 (cp_parser *);
2050a1bb
MM
1767static void cp_parser_pre_parsed_nested_name_specifier
1768 (cp_parser *);
a723baf1
MM
1769static void cp_parser_cache_group
1770 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1771static void cp_parser_parse_tentatively
1772 PARAMS ((cp_parser *));
1773static void cp_parser_commit_to_tentative_parse
1774 PARAMS ((cp_parser *));
1775static void cp_parser_abort_tentative_parse
1776 PARAMS ((cp_parser *));
1777static bool cp_parser_parse_definitely
1778 PARAMS ((cp_parser *));
f7b5ecd9 1779static inline bool cp_parser_parsing_tentatively
a723baf1
MM
1780 PARAMS ((cp_parser *));
1781static bool cp_parser_committed_to_tentative_parse
1782 PARAMS ((cp_parser *));
1783static void cp_parser_error
1784 PARAMS ((cp_parser *, const char *));
e5976695 1785static bool cp_parser_simulate_error
a723baf1
MM
1786 PARAMS ((cp_parser *));
1787static void cp_parser_check_type_definition
1788 PARAMS ((cp_parser *));
1789static bool cp_parser_skip_to_closing_parenthesis
1790 PARAMS ((cp_parser *));
1791static bool cp_parser_skip_to_closing_parenthesis_or_comma
1792 (cp_parser *);
1793static void cp_parser_skip_to_end_of_statement
1794 PARAMS ((cp_parser *));
1795static void cp_parser_skip_to_end_of_block_or_statement
1796 PARAMS ((cp_parser *));
1797static void cp_parser_skip_to_closing_brace
1798 (cp_parser *);
1799static void cp_parser_skip_until_found
1800 PARAMS ((cp_parser *, enum cpp_ttype, const char *));
1801static bool cp_parser_error_occurred
1802 PARAMS ((cp_parser *));
1803static bool cp_parser_allow_gnu_extensions_p
1804 PARAMS ((cp_parser *));
1805static bool cp_parser_is_string_literal
1806 PARAMS ((cp_token *));
1807static bool cp_parser_is_keyword
1808 PARAMS ((cp_token *, enum rid));
1809static bool cp_parser_dependent_type_p
1810 (tree);
1811static bool cp_parser_value_dependent_expression_p
1812 (tree);
1813static bool cp_parser_type_dependent_expression_p
1814 (tree);
1815static bool cp_parser_dependent_template_arg_p
1816 (tree);
1817static bool cp_parser_dependent_template_id_p
1818 (tree, tree);
1819static bool cp_parser_dependent_template_p
1820 (tree);
1821static void cp_parser_defer_access_check
1822 (cp_parser *, tree, tree);
1823static void cp_parser_start_deferring_access_checks
1824 (cp_parser *);
1825static tree cp_parser_stop_deferring_access_checks
1826 PARAMS ((cp_parser *));
1827static void cp_parser_perform_deferred_access_checks
1828 PARAMS ((tree));
1829static tree cp_parser_scope_through_which_access_occurs
1830 (tree, tree, tree);
1831
f7b5ecd9
MM
1832/* Returns non-zero if we are parsing tentatively. */
1833
1834static inline bool
1835cp_parser_parsing_tentatively (parser)
1836 cp_parser *parser;
1837{
1838 return parser->context->next != NULL;
1839}
1840
a723baf1
MM
1841/* Returns non-zero if TOKEN is a string literal. */
1842
1843static bool
1844cp_parser_is_string_literal (token)
1845 cp_token *token;
1846{
1847 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1848}
1849
1850/* Returns non-zero if TOKEN is the indicated KEYWORD. */
1851
1852static bool
1853cp_parser_is_keyword (token, keyword)
1854 cp_token *token;
1855 enum rid keyword;
1856{
1857 return token->keyword == keyword;
1858}
1859
1860/* Returns TRUE if TYPE is dependent, in the sense of
1861 [temp.dep.type]. */
1862
1863static bool
1864cp_parser_dependent_type_p (type)
1865 tree type;
1866{
1867 tree scope;
1868
1869 if (!processing_template_decl)
1870 return false;
1871
1872 /* If the type is NULL, we have not computed a type for the entity
1873 in question; in that case, the type is dependent. */
1874 if (!type)
1875 return true;
1876
1877 /* Erroneous types can be considered non-dependent. */
1878 if (type == error_mark_node)
1879 return false;
1880
1881 /* [temp.dep.type]
1882
1883 A type is dependent if it is:
1884
1885 -- a template parameter. */
1886 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
1887 return true;
1888 /* -- a qualified-id with a nested-name-specifier which contains a
1889 class-name that names a dependent type or whose unqualified-id
1890 names a dependent type. */
1891 if (TREE_CODE (type) == TYPENAME_TYPE)
1892 return true;
1893 /* -- a cv-qualified type where the cv-unqualified type is
1894 dependent. */
1895 type = TYPE_MAIN_VARIANT (type);
1896 /* -- a compound type constructed from any dependent type. */
1897 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
1898 return (cp_parser_dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
1899 || cp_parser_dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1900 (type)));
1901 else if (TREE_CODE (type) == POINTER_TYPE
1902 || TREE_CODE (type) == REFERENCE_TYPE)
1903 return cp_parser_dependent_type_p (TREE_TYPE (type));
1904 else if (TREE_CODE (type) == FUNCTION_TYPE
1905 || TREE_CODE (type) == METHOD_TYPE)
1906 {
1907 tree arg_type;
1908
1909 if (cp_parser_dependent_type_p (TREE_TYPE (type)))
1910 return true;
1911 for (arg_type = TYPE_ARG_TYPES (type);
1912 arg_type;
1913 arg_type = TREE_CHAIN (arg_type))
1914 if (cp_parser_dependent_type_p (TREE_VALUE (arg_type)))
1915 return true;
1916 return false;
1917 }
1918 /* -- an array type constructed from any dependent type or whose
1919 size is specified by a constant expression that is
1920 value-dependent. */
1921 if (TREE_CODE (type) == ARRAY_TYPE)
1922 {
f1aba0a5 1923 if (TYPE_DOMAIN (type)
a723baf1
MM
1924 && ((cp_parser_value_dependent_expression_p
1925 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1926 || (cp_parser_type_dependent_expression_p
1927 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
1928 return true;
1929 return cp_parser_dependent_type_p (TREE_TYPE (type));
1930 }
1931 /* -- a template-id in which either the template name is a template
1932 parameter or any of the template arguments is a dependent type or
1933 an expression that is type-dependent or value-dependent.
1934
1935 This language seems somewhat confused; for example, it does not
1936 discuss template template arguments. Therefore, we use the
1937 definition for dependent template arguments in [temp.dep.temp]. */
1938 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
1939 && (cp_parser_dependent_template_id_p
1940 (CLASSTYPE_TI_TEMPLATE (type),
1941 CLASSTYPE_TI_ARGS (type))))
1942 return true;
1943 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1944 return true;
1945 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
1946 expression is not type-dependent, then it should already been
1947 have resolved. */
1948 if (TREE_CODE (type) == TYPEOF_TYPE)
1949 return true;
1950 /* The standard does not specifically mention types that are local
1951 to template functions or local classes, but they should be
1952 considered dependent too. For example:
1953
1954 template <int I> void f() {
1955 enum E { a = I };
1956 S<sizeof (E)> s;
1957 }
1958
1959 The size of `E' cannot be known until the value of `I' has been
1960 determined. Therefore, `E' must be considered dependent. */
1961 scope = TYPE_CONTEXT (type);
1962 if (scope && TYPE_P (scope))
1963 return cp_parser_dependent_type_p (scope);
1964 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
1965 return cp_parser_type_dependent_expression_p (scope);
1966
1967 /* Other types are non-dependent. */
1968 return false;
1969}
1970
1971/* Returns TRUE if the EXPRESSION is value-dependent. */
1972
1973static bool
1974cp_parser_value_dependent_expression_p (tree expression)
1975{
1976 if (!processing_template_decl)
1977 return false;
1978
1979 /* A name declared with a dependent type. */
1980 if (DECL_P (expression)
1981 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
1982 return true;
1983 /* A non-type template parameter. */
1984 if ((TREE_CODE (expression) == CONST_DECL
1985 && DECL_TEMPLATE_PARM_P (expression))
1986 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
1987 return true;
1988 /* A constant with integral or enumeration type and is initialized
1989 with an expression that is value-dependent. */
1990 if (TREE_CODE (expression) == VAR_DECL
1991 && DECL_INITIAL (expression)
1992 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
1993 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
1994 && cp_parser_value_dependent_expression_p (DECL_INITIAL (expression)))
1995 return true;
1996 /* These expressions are value-dependent if the type to which the
1997 cast occurs is dependent. */
1998 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
1999 || TREE_CODE (expression) == STATIC_CAST_EXPR
2000 || TREE_CODE (expression) == CONST_CAST_EXPR
2001 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2002 || TREE_CODE (expression) == CAST_EXPR)
2003 && cp_parser_dependent_type_p (TREE_TYPE (expression)))
2004 return true;
2005 /* A `sizeof' expression where the sizeof operand is a type is
2006 value-dependent if the type is dependent. If the type was not
2007 dependent, we would no longer have a SIZEOF_EXPR, so any
2008 SIZEOF_EXPR is dependent. */
2009 if (TREE_CODE (expression) == SIZEOF_EXPR)
2010 return true;
2011 /* A constant expression is value-dependent if any subexpression is
2012 value-dependent. */
2013 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
2014 {
2015 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
2016 {
2017 case '1':
2018 return (cp_parser_value_dependent_expression_p
2019 (TREE_OPERAND (expression, 0)));
2020 case '<':
2021 case '2':
2022 return ((cp_parser_value_dependent_expression_p
2023 (TREE_OPERAND (expression, 0)))
2024 || (cp_parser_value_dependent_expression_p
2025 (TREE_OPERAND (expression, 1))));
2026 case 'e':
2027 {
2028 int i;
2029 for (i = 0;
2030 i < TREE_CODE_LENGTH (TREE_CODE (expression));
2031 ++i)
2032 if (cp_parser_value_dependent_expression_p
2033 (TREE_OPERAND (expression, i)))
2034 return true;
2035 return false;
2036 }
2037 }
2038 }
2039
2040 /* The expression is not value-dependent. */
2041 return false;
2042}
2043
2044/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
2045 [temp.dep.expr]. */
2046
2047static bool
2048cp_parser_type_dependent_expression_p (expression)
2049 tree expression;
2050{
2051 if (!processing_template_decl)
2052 return false;
2053
2054 /* Some expression forms are never type-dependent. */
2055 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
2056 || TREE_CODE (expression) == SIZEOF_EXPR
2057 || TREE_CODE (expression) == ALIGNOF_EXPR
2058 || TREE_CODE (expression) == TYPEID_EXPR
2059 || TREE_CODE (expression) == DELETE_EXPR
2060 || TREE_CODE (expression) == VEC_DELETE_EXPR
2061 || TREE_CODE (expression) == THROW_EXPR)
2062 return false;
2063
2064 /* The types of these expressions depends only on the type to which
2065 the cast occurs. */
2066 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
2067 || TREE_CODE (expression) == STATIC_CAST_EXPR
2068 || TREE_CODE (expression) == CONST_CAST_EXPR
2069 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
2070 || TREE_CODE (expression) == CAST_EXPR)
2071 return cp_parser_dependent_type_p (TREE_TYPE (expression));
2072 /* The types of these expressions depends only on the type created
2073 by the expression. */
2074 else if (TREE_CODE (expression) == NEW_EXPR
2075 || TREE_CODE (expression) == VEC_NEW_EXPR)
2076 return cp_parser_dependent_type_p (TREE_OPERAND (expression, 1));
2077
2078 if (TREE_CODE (expression) == FUNCTION_DECL
2079 && DECL_LANG_SPECIFIC (expression)
2080 && DECL_TEMPLATE_INFO (expression)
2081 && (cp_parser_dependent_template_id_p
2082 (DECL_TI_TEMPLATE (expression),
2083 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
2084 return true;
2085
2086 return (cp_parser_dependent_type_p (TREE_TYPE (expression)));
2087}
2088
2089/* Returns TRUE if the ARG (a template argument) is dependent. */
2090
2091static bool
2092cp_parser_dependent_template_arg_p (tree arg)
2093{
2094 if (!processing_template_decl)
2095 return false;
2096
2097 if (TREE_CODE (arg) == TEMPLATE_DECL
2098 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2099 return cp_parser_dependent_template_p (arg);
2100 else if (TYPE_P (arg))
2101 return cp_parser_dependent_type_p (arg);
2102 else
2103 return (cp_parser_type_dependent_expression_p (arg)
2104 || cp_parser_value_dependent_expression_p (arg));
2105}
2106
2107/* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
2108
2109static bool
2110cp_parser_dependent_template_id_p (tree tmpl, tree args)
2111{
2112 int i;
2113
2114 if (cp_parser_dependent_template_p (tmpl))
2115 return true;
2116 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2117 if (cp_parser_dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2118 return true;
2119 return false;
2120}
2121
2122/* Returns TRUE if the template TMPL is dependent. */
2123
2124static bool
2125cp_parser_dependent_template_p (tree tmpl)
2126{
2127 /* Template template parameters are dependent. */
2128 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
2129 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
2130 return true;
2131 /* So are member templates of dependent classes. */
2132 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
2133 return cp_parser_dependent_type_p (DECL_CONTEXT (tmpl));
2134 return false;
2135}
2136
2137/* Defer checking the accessibility of DECL, when looked up in
2138 CLASS_TYPE. */
2139
2140static void
2141cp_parser_defer_access_check (cp_parser *parser,
2142 tree class_type,
2143 tree decl)
2144{
2145 tree check;
2146
2147 /* If we are not supposed to defer access checks, just check now. */
2148 if (!parser->context->deferring_access_checks_p)
2149 {
2150 enforce_access (class_type, decl);
2151 return;
2152 }
2153
2154 /* See if we are already going to perform this check. */
2155 for (check = parser->context->deferred_access_checks;
2156 check;
2157 check = TREE_CHAIN (check))
2158 if (TREE_VALUE (check) == decl
2159 && same_type_p (TREE_PURPOSE (check), class_type))
2160 return;
2161 /* If not, record the check. */
2162 parser->context->deferred_access_checks
2163 = tree_cons (class_type, decl, parser->context->deferred_access_checks);
2164}
2165
2166/* Start deferring access control checks. */
2167
2168static void
2169cp_parser_start_deferring_access_checks (cp_parser *parser)
2170{
2171 parser->context->deferring_access_checks_p = true;
2172}
2173
2174/* Stop deferring access control checks. Returns a TREE_LIST
2175 representing the deferred checks. The TREE_PURPOSE of each node is
2176 the type through which the access occurred; the TREE_VALUE is the
2177 declaration named. */
2178
2179static tree
2180cp_parser_stop_deferring_access_checks (parser)
2181 cp_parser *parser;
2182{
2183 tree access_checks;
2184
2185 parser->context->deferring_access_checks_p = false;
2186 access_checks = parser->context->deferred_access_checks;
2187 parser->context->deferred_access_checks = NULL_TREE;
2188
2189 return access_checks;
2190}
2191
2192/* Perform the deferred ACCESS_CHECKS, whose representation is as
2193 documented with cp_parser_stop_deferrring_access_checks. */
2194
2195static void
2196cp_parser_perform_deferred_access_checks (access_checks)
2197 tree access_checks;
2198{
2199 tree deferred_check;
2200
2201 /* Look through all the deferred checks. */
2202 for (deferred_check = access_checks;
2203 deferred_check;
2204 deferred_check = TREE_CHAIN (deferred_check))
2205 /* Check access. */
2206 enforce_access (TREE_PURPOSE (deferred_check),
2207 TREE_VALUE (deferred_check));
2208}
2209
2210/* Returns the scope through which DECL is being accessed, or
2211 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
2212 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
2213 or `x', respectively. If the DECL was named as `A::B' then
2214 NESTED_NAME_SPECIFIER is `A'. */
2215
2216tree
2217cp_parser_scope_through_which_access_occurs (decl,
2218 object_type,
2219 nested_name_specifier)
2220 tree decl;
2221 tree object_type;
2222 tree nested_name_specifier;
2223{
2224 tree scope;
2225 tree qualifying_type = NULL_TREE;
2226
2227 /* Determine the SCOPE of DECL. */
2228 scope = context_for_name_lookup (decl);
2229 /* If the SCOPE is not a type, then DECL is not a member. */
2230 if (!TYPE_P (scope))
2231 return NULL_TREE;
2232 /* Figure out the type through which DECL is being accessed. */
a6f6052a
MM
2233 if (object_type
2234 /* OBJECT_TYPE might not be a class type; consider:
2235
2236 class A { typedef int I; };
2237 I *p;
2238 p->A::I::~I();
2239
2240 In this case, we will have "A::I" as the DECL, but "I" as the
2241 OBJECT_TYPE. */
2242 && CLASS_TYPE_P (object_type)
2243 && DERIVED_FROM_P (scope, object_type))
a723baf1
MM
2244 /* If we are processing a `->' or `.' expression, use the type of the
2245 left-hand side. */
2246 qualifying_type = object_type;
2247 else if (nested_name_specifier)
2248 {
2249 /* If the reference is to a non-static member of the
2250 current class, treat it as if it were referenced through
2251 `this'. */
2252 if (DECL_NONSTATIC_MEMBER_P (decl)
2253 && current_class_ptr
2254 && DERIVED_FROM_P (scope, current_class_type))
2255 qualifying_type = current_class_type;
2256 /* Otherwise, use the type indicated by the
2257 nested-name-specifier. */
2258 else
2259 qualifying_type = nested_name_specifier;
2260 }
2261 else
2262 /* Otherwise, the name must be from the current class or one of
2263 its bases. */
2264 qualifying_type = currently_open_derived_class (scope);
2265
2266 return qualifying_type;
2267}
2268
2269/* Issue the indicated error MESSAGE. */
2270
2271static void
2272cp_parser_error (parser, message)
2273 cp_parser *parser;
2274 const char *message;
2275{
a723baf1 2276 /* Output the MESSAGE -- unless we're parsing tentatively. */
e5976695 2277 if (!cp_parser_simulate_error (parser))
a723baf1
MM
2278 error (message);
2279}
2280
2281/* If we are parsing tentatively, remember that an error has occurred
e5976695
MM
2282 during this tentative parse. Returns true if the error was
2283 simulated; false if a messgae should be issued by the caller. */
a723baf1 2284
e5976695 2285static bool
a723baf1
MM
2286cp_parser_simulate_error (parser)
2287 cp_parser *parser;
2288{
2289 if (cp_parser_parsing_tentatively (parser)
2290 && !cp_parser_committed_to_tentative_parse (parser))
e5976695
MM
2291 {
2292 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2293 return true;
2294 }
2295 return false;
a723baf1
MM
2296}
2297
2298/* This function is called when a type is defined. If type
2299 definitions are forbidden at this point, an error message is
2300 issued. */
2301
2302static void
2303cp_parser_check_type_definition (parser)
2304 cp_parser *parser;
2305{
2306 /* If types are forbidden here, issue a message. */
2307 if (parser->type_definition_forbidden_message)
2308 /* Use `%s' to print the string in case there are any escape
2309 characters in the message. */
2310 error ("%s", parser->type_definition_forbidden_message);
2311}
2312
2313/* Consume tokens up to, and including, the next non-nested closing `)'.
2314 Returns TRUE iff we found a closing `)'. */
2315
2316static bool
2317cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
2318{
2319 unsigned nesting_depth = 0;
2320
2321 while (true)
2322 {
2323 cp_token *token;
2324
2325 /* If we've run out of tokens, then there is no closing `)'. */
2326 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2327 return false;
2328 /* Consume the token. */
2329 token = cp_lexer_consume_token (parser->lexer);
2330 /* If it is an `(', we have entered another level of nesting. */
2331 if (token->type == CPP_OPEN_PAREN)
2332 ++nesting_depth;
2333 /* If it is a `)', then we might be done. */
2334 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2335 return true;
2336 }
2337}
2338
2339/* Consume tokens until the next token is a `)', or a `,'. Returns
2340 TRUE if the next token is a `,'. */
2341
2342static bool
2343cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2344{
2345 unsigned nesting_depth = 0;
2346
2347 while (true)
2348 {
2349 cp_token *token = cp_lexer_peek_token (parser->lexer);
2350
2351 /* If we've run out of tokens, then there is no closing `)'. */
2352 if (token->type == CPP_EOF)
2353 return false;
2354 /* If it is a `,' stop. */
2355 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2356 return true;
2357 /* If it is a `)', stop. */
2358 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2359 return false;
2360 /* If it is an `(', we have entered another level of nesting. */
2361 else if (token->type == CPP_OPEN_PAREN)
2362 ++nesting_depth;
2363 /* Consume the token. */
2364 token = cp_lexer_consume_token (parser->lexer);
2365 }
2366}
2367
2368/* Consume tokens until we reach the end of the current statement.
2369 Normally, that will be just before consuming a `;'. However, if a
2370 non-nested `}' comes first, then we stop before consuming that. */
2371
2372static void
2373cp_parser_skip_to_end_of_statement (parser)
2374 cp_parser *parser;
2375{
2376 unsigned nesting_depth = 0;
2377
2378 while (true)
2379 {
2380 cp_token *token;
2381
2382 /* Peek at the next token. */
2383 token = cp_lexer_peek_token (parser->lexer);
2384 /* If we've run out of tokens, stop. */
2385 if (token->type == CPP_EOF)
2386 break;
2387 /* If the next token is a `;', we have reached the end of the
2388 statement. */
2389 if (token->type == CPP_SEMICOLON && !nesting_depth)
2390 break;
2391 /* If the next token is a non-nested `}', then we have reached
2392 the end of the current block. */
2393 if (token->type == CPP_CLOSE_BRACE)
2394 {
2395 /* If this is a non-nested `}', stop before consuming it.
2396 That way, when confronted with something like:
2397
2398 { 3 + }
2399
2400 we stop before consuming the closing `}', even though we
2401 have not yet reached a `;'. */
2402 if (nesting_depth == 0)
2403 break;
2404 /* If it is the closing `}' for a block that we have
2405 scanned, stop -- but only after consuming the token.
2406 That way given:
2407
2408 void f g () { ... }
2409 typedef int I;
2410
2411 we will stop after the body of the erroneously declared
2412 function, but before consuming the following `typedef'
2413 declaration. */
2414 if (--nesting_depth == 0)
2415 {
2416 cp_lexer_consume_token (parser->lexer);
2417 break;
2418 }
2419 }
2420 /* If it the next token is a `{', then we are entering a new
2421 block. Consume the entire block. */
2422 else if (token->type == CPP_OPEN_BRACE)
2423 ++nesting_depth;
2424 /* Consume the token. */
2425 cp_lexer_consume_token (parser->lexer);
2426 }
2427}
2428
2429/* Skip tokens until we have consumed an entire block, or until we
2430 have consumed a non-nested `;'. */
2431
2432static void
2433cp_parser_skip_to_end_of_block_or_statement (parser)
2434 cp_parser *parser;
2435{
2436 unsigned nesting_depth = 0;
2437
2438 while (true)
2439 {
2440 cp_token *token;
2441
2442 /* Peek at the next token. */
2443 token = cp_lexer_peek_token (parser->lexer);
2444 /* If we've run out of tokens, stop. */
2445 if (token->type == CPP_EOF)
2446 break;
2447 /* If the next token is a `;', we have reached the end of the
2448 statement. */
2449 if (token->type == CPP_SEMICOLON && !nesting_depth)
2450 {
2451 /* Consume the `;'. */
2452 cp_lexer_consume_token (parser->lexer);
2453 break;
2454 }
2455 /* Consume the token. */
2456 token = cp_lexer_consume_token (parser->lexer);
2457 /* If the next token is a non-nested `}', then we have reached
2458 the end of the current block. */
2459 if (token->type == CPP_CLOSE_BRACE
2460 && (nesting_depth == 0 || --nesting_depth == 0))
2461 break;
2462 /* If it the next token is a `{', then we are entering a new
2463 block. Consume the entire block. */
2464 if (token->type == CPP_OPEN_BRACE)
2465 ++nesting_depth;
2466 }
2467}
2468
2469/* Skip tokens until a non-nested closing curly brace is the next
2470 token. */
2471
2472static void
2473cp_parser_skip_to_closing_brace (cp_parser *parser)
2474{
2475 unsigned nesting_depth = 0;
2476
2477 while (true)
2478 {
2479 cp_token *token;
2480
2481 /* Peek at the next token. */
2482 token = cp_lexer_peek_token (parser->lexer);
2483 /* If we've run out of tokens, stop. */
2484 if (token->type == CPP_EOF)
2485 break;
2486 /* If the next token is a non-nested `}', then we have reached
2487 the end of the current block. */
2488 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2489 break;
2490 /* If it the next token is a `{', then we are entering a new
2491 block. Consume the entire block. */
2492 else if (token->type == CPP_OPEN_BRACE)
2493 ++nesting_depth;
2494 /* Consume the token. */
2495 cp_lexer_consume_token (parser->lexer);
2496 }
2497}
2498
2499/* Create a new C++ parser. */
2500
2501static cp_parser *
2502cp_parser_new ()
2503{
2504 cp_parser *parser;
17211ab5
GK
2505 cp_lexer *lexer;
2506
2507 /* cp_lexer_new_main is called before calling ggc_alloc because
2508 cp_lexer_new_main might load a PCH file. */
2509 lexer = cp_lexer_new_main ();
a723baf1
MM
2510
2511 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2512 parser->lexer = lexer;
a723baf1
MM
2513 parser->context = cp_parser_context_new (NULL);
2514
2515 /* For now, we always accept GNU extensions. */
2516 parser->allow_gnu_extensions_p = 1;
2517
2518 /* The `>' token is a greater-than operator, not the end of a
2519 template-id. */
2520 parser->greater_than_is_operator_p = true;
2521
2522 parser->default_arg_ok_p = true;
2523
2524 /* We are not parsing a constant-expression. */
2525 parser->constant_expression_p = false;
2526
2527 /* Local variable names are not forbidden. */
2528 parser->local_variables_forbidden_p = false;
2529
2530 /* We are not procesing an `extern "C"' declaration. */
2531 parser->in_unbraced_linkage_specification_p = false;
2532
2533 /* We are not processing a declarator. */
2534 parser->in_declarator_p = false;
2535
a723baf1
MM
2536 /* The unparsed function queue is empty. */
2537 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2538
2539 /* There are no classes being defined. */
2540 parser->num_classes_being_defined = 0;
2541
2542 /* No template parameters apply. */
2543 parser->num_template_parameter_lists = 0;
2544
2545 return parser;
2546}
2547
2548/* Lexical conventions [gram.lex] */
2549
2550/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2551 identifier. */
2552
2553static tree
2554cp_parser_identifier (parser)
2555 cp_parser *parser;
2556{
2557 cp_token *token;
2558
2559 /* Look for the identifier. */
2560 token = cp_parser_require (parser, CPP_NAME, "identifier");
2561 /* Return the value. */
2562 return token ? token->value : error_mark_node;
2563}
2564
2565/* Basic concepts [gram.basic] */
2566
2567/* Parse a translation-unit.
2568
2569 translation-unit:
2570 declaration-seq [opt]
2571
2572 Returns TRUE if all went well. */
2573
2574static bool
2575cp_parser_translation_unit (parser)
2576 cp_parser *parser;
2577{
2578 while (true)
2579 {
2580 cp_parser_declaration_seq_opt (parser);
2581
2582 /* If there are no tokens left then all went well. */
2583 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2584 break;
2585
2586 /* Otherwise, issue an error message. */
2587 cp_parser_error (parser, "expected declaration");
2588 return false;
2589 }
2590
2591 /* Consume the EOF token. */
2592 cp_parser_require (parser, CPP_EOF, "end-of-file");
2593
2594 /* Finish up. */
2595 finish_translation_unit ();
2596
2597 /* All went well. */
2598 return true;
2599}
2600
2601/* Expressions [gram.expr] */
2602
2603/* Parse a primary-expression.
2604
2605 primary-expression:
2606 literal
2607 this
2608 ( expression )
2609 id-expression
2610
2611 GNU Extensions:
2612
2613 primary-expression:
2614 ( compound-statement )
2615 __builtin_va_arg ( assignment-expression , type-id )
2616
2617 literal:
2618 __null
2619
2620 Returns a representation of the expression.
2621
2622 *IDK indicates what kind of id-expression (if any) was present.
2623
2624 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2625 used as the operand of a pointer-to-member. In that case,
2626 *QUALIFYING_CLASS gives the class that is used as the qualifying
2627 class in the pointer-to-member. */
2628
2629static tree
2630cp_parser_primary_expression (cp_parser *parser,
2631 cp_parser_id_kind *idk,
2632 tree *qualifying_class)
2633{
2634 cp_token *token;
2635
2636 /* Assume the primary expression is not an id-expression. */
2637 *idk = CP_PARSER_ID_KIND_NONE;
2638 /* And that it cannot be used as pointer-to-member. */
2639 *qualifying_class = NULL_TREE;
2640
2641 /* Peek at the next token. */
2642 token = cp_lexer_peek_token (parser->lexer);
2643 switch (token->type)
2644 {
2645 /* literal:
2646 integer-literal
2647 character-literal
2648 floating-literal
2649 string-literal
2650 boolean-literal */
2651 case CPP_CHAR:
2652 case CPP_WCHAR:
2653 case CPP_STRING:
2654 case CPP_WSTRING:
2655 case CPP_NUMBER:
2656 token = cp_lexer_consume_token (parser->lexer);
2657 return token->value;
2658
2659 case CPP_OPEN_PAREN:
2660 {
2661 tree expr;
2662 bool saved_greater_than_is_operator_p;
2663
2664 /* Consume the `('. */
2665 cp_lexer_consume_token (parser->lexer);
2666 /* Within a parenthesized expression, a `>' token is always
2667 the greater-than operator. */
2668 saved_greater_than_is_operator_p
2669 = parser->greater_than_is_operator_p;
2670 parser->greater_than_is_operator_p = true;
2671 /* If we see `( { ' then we are looking at the beginning of
2672 a GNU statement-expression. */
2673 if (cp_parser_allow_gnu_extensions_p (parser)
2674 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2675 {
2676 /* Statement-expressions are not allowed by the standard. */
2677 if (pedantic)
2678 pedwarn ("ISO C++ forbids braced-groups within expressions");
2679
2680 /* And they're not allowed outside of a function-body; you
2681 cannot, for example, write:
2682
2683 int i = ({ int j = 3; j + 1; });
2684
2685 at class or namespace scope. */
2686 if (!at_function_scope_p ())
2687 error ("statement-expressions are allowed only inside functions");
2688 /* Start the statement-expression. */
2689 expr = begin_stmt_expr ();
2690 /* Parse the compound-statement. */
2691 cp_parser_compound_statement (parser);
2692 /* Finish up. */
2693 expr = finish_stmt_expr (expr);
2694 }
2695 else
2696 {
2697 /* Parse the parenthesized expression. */
2698 expr = cp_parser_expression (parser);
2699 /* Let the front end know that this expression was
2700 enclosed in parentheses. This matters in case, for
2701 example, the expression is of the form `A::B', since
2702 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2703 not. */
2704 finish_parenthesized_expr (expr);
2705 }
2706 /* The `>' token might be the end of a template-id or
2707 template-parameter-list now. */
2708 parser->greater_than_is_operator_p
2709 = saved_greater_than_is_operator_p;
2710 /* Consume the `)'. */
2711 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2712 cp_parser_skip_to_end_of_statement (parser);
2713
2714 return expr;
2715 }
2716
2717 case CPP_KEYWORD:
2718 switch (token->keyword)
2719 {
2720 /* These two are the boolean literals. */
2721 case RID_TRUE:
2722 cp_lexer_consume_token (parser->lexer);
2723 return boolean_true_node;
2724 case RID_FALSE:
2725 cp_lexer_consume_token (parser->lexer);
2726 return boolean_false_node;
2727
2728 /* The `__null' literal. */
2729 case RID_NULL:
2730 cp_lexer_consume_token (parser->lexer);
2731 return null_node;
2732
2733 /* Recognize the `this' keyword. */
2734 case RID_THIS:
2735 cp_lexer_consume_token (parser->lexer);
2736 if (parser->local_variables_forbidden_p)
2737 {
2738 error ("`this' may not be used in this context");
2739 return error_mark_node;
2740 }
2741 return finish_this_expr ();
2742
2743 /* The `operator' keyword can be the beginning of an
2744 id-expression. */
2745 case RID_OPERATOR:
2746 goto id_expression;
2747
2748 case RID_FUNCTION_NAME:
2749 case RID_PRETTY_FUNCTION_NAME:
2750 case RID_C99_FUNCTION_NAME:
2751 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2752 __func__ are the names of variables -- but they are
2753 treated specially. Therefore, they are handled here,
2754 rather than relying on the generic id-expression logic
2755 below. Gramatically, these names are id-expressions.
2756
2757 Consume the token. */
2758 token = cp_lexer_consume_token (parser->lexer);
2759 /* Look up the name. */
2760 return finish_fname (token->value);
2761
2762 case RID_VA_ARG:
2763 {
2764 tree expression;
2765 tree type;
2766
2767 /* The `__builtin_va_arg' construct is used to handle
2768 `va_arg'. Consume the `__builtin_va_arg' token. */
2769 cp_lexer_consume_token (parser->lexer);
2770 /* Look for the opening `('. */
2771 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2772 /* Now, parse the assignment-expression. */
2773 expression = cp_parser_assignment_expression (parser);
2774 /* Look for the `,'. */
2775 cp_parser_require (parser, CPP_COMMA, "`,'");
2776 /* Parse the type-id. */
2777 type = cp_parser_type_id (parser);
2778 /* Look for the closing `)'. */
2779 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2780
2781 return build_x_va_arg (expression, type);
2782 }
2783
2784 default:
2785 cp_parser_error (parser, "expected primary-expression");
2786 return error_mark_node;
2787 }
2788 /* Fall through. */
2789
2790 /* An id-expression can start with either an identifier, a
2791 `::' as the beginning of a qualified-id, or the "operator"
2792 keyword. */
2793 case CPP_NAME:
2794 case CPP_SCOPE:
2795 case CPP_TEMPLATE_ID:
2796 case CPP_NESTED_NAME_SPECIFIER:
2797 {
2798 tree id_expression;
2799 tree decl;
2800
2801 id_expression:
2802 /* Parse the id-expression. */
2803 id_expression
2804 = cp_parser_id_expression (parser,
2805 /*template_keyword_p=*/false,
2806 /*check_dependency_p=*/true,
2807 /*template_p=*/NULL);
2808 if (id_expression == error_mark_node)
2809 return error_mark_node;
2810 /* If we have a template-id, then no further lookup is
2811 required. If the template-id was for a template-class, we
2812 will sometimes have a TYPE_DECL at this point. */
2813 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2814 || TREE_CODE (id_expression) == TYPE_DECL)
2815 decl = id_expression;
2816 /* Look up the name. */
2817 else
2818 {
2819 decl = cp_parser_lookup_name_simple (parser, id_expression);
2820 /* If name lookup gives us a SCOPE_REF, then the
2821 qualifying scope was dependent. Just propagate the
2822 name. */
2823 if (TREE_CODE (decl) == SCOPE_REF)
2824 {
2825 if (TYPE_P (TREE_OPERAND (decl, 0)))
2826 *qualifying_class = TREE_OPERAND (decl, 0);
2827 return decl;
2828 }
2829 /* Check to see if DECL is a local variable in a context
2830 where that is forbidden. */
2831 if (parser->local_variables_forbidden_p
2832 && local_variable_p (decl))
2833 {
2834 /* It might be that we only found DECL because we are
2835 trying to be generous with pre-ISO scoping rules.
2836 For example, consider:
2837
2838 int i;
2839 void g() {
2840 for (int i = 0; i < 10; ++i) {}
2841 extern void f(int j = i);
2842 }
2843
2844 Here, name look up will originally find the out
2845 of scope `i'. We need to issue a warning message,
2846 but then use the global `i'. */
2847 decl = check_for_out_of_scope_variable (decl);
2848 if (local_variable_p (decl))
2849 {
2850 error ("local variable `%D' may not appear in this context",
2851 decl);
2852 return error_mark_node;
2853 }
2854 }
2855
2856 /* If unqualified name lookup fails while processing a
2857 template, that just means that we need to do name
2858 lookup again when the template is instantiated. */
2859 if (!parser->scope
2860 && decl == error_mark_node
2861 && processing_template_decl)
2862 {
2863 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2864 return build_min_nt (LOOKUP_EXPR, id_expression);
2865 }
2866 else if (decl == error_mark_node
2867 && !processing_template_decl)
2868 {
2869 if (!parser->scope)
2870 {
2871 /* It may be resolvable as a koenig lookup function
2872 call. */
2873 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2874 return id_expression;
2875 }
2876 else if (TYPE_P (parser->scope)
2877 && !COMPLETE_TYPE_P (parser->scope))
2878 error ("incomplete type `%T' used in nested name specifier",
2879 parser->scope);
2880 else if (parser->scope != global_namespace)
2881 error ("`%D' is not a member of `%D'",
2882 id_expression, parser->scope);
2883 else
2884 error ("`::%D' has not been declared", id_expression);
2885 }
2886 /* If DECL is a variable would be out of scope under
2887 ANSI/ISO rules, but in scope in the ARM, name lookup
2888 will succeed. Issue a diagnostic here. */
2889 else
2890 decl = check_for_out_of_scope_variable (decl);
2891
2892 /* Remember that the name was used in the definition of
2893 the current class so that we can check later to see if
2894 the meaning would have been different after the class
2895 was entirely defined. */
2896 if (!parser->scope && decl != error_mark_node)
2897 maybe_note_name_used_in_class (id_expression, decl);
2898 }
2899
2900 /* If we didn't find anything, or what we found was a type,
2901 then this wasn't really an id-expression. */
2902 if (TREE_CODE (decl) == TYPE_DECL
2903 || TREE_CODE (decl) == NAMESPACE_DECL
2904 || (TREE_CODE (decl) == TEMPLATE_DECL
2905 && !DECL_FUNCTION_TEMPLATE_P (decl)))
2906 {
2907 cp_parser_error (parser,
2908 "expected primary-expression");
2909 return error_mark_node;
2910 }
2911
2912 /* If the name resolved to a template parameter, there is no
2913 need to look it up again later. Similarly, we resolve
2914 enumeration constants to their underlying values. */
2915 if (TREE_CODE (decl) == CONST_DECL)
2916 {
2917 *idk = CP_PARSER_ID_KIND_NONE;
2918 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2919 return DECL_INITIAL (decl);
2920 return decl;
2921 }
2922 else
2923 {
2924 bool dependent_p;
2925
2926 /* If the declaration was explicitly qualified indicate
2927 that. The semantics of `A::f(3)' are different than
2928 `f(3)' if `f' is virtual. */
2929 *idk = (parser->scope
2930 ? CP_PARSER_ID_KIND_QUALIFIED
2931 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2932 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2933 : CP_PARSER_ID_KIND_UNQUALIFIED));
2934
2935
2936 /* [temp.dep.expr]
2937
2938 An id-expression is type-dependent if it contains an
2939 identifier that was declared with a dependent type.
2940
2941 As an optimization, we could choose not to create a
2942 LOOKUP_EXPR for a name that resolved to a local
2943 variable in the template function that we are currently
2944 declaring; such a name cannot ever resolve to anything
2945 else. If we did that we would not have to look up
2946 these names at instantiation time.
2947
2948 The standard is not very specific about an
2949 id-expression that names a set of overloaded functions.
2950 What if some of them have dependent types and some of
2951 them do not? Presumably, such a name should be treated
2952 as a dependent name. */
2953 /* Assume the name is not dependent. */
2954 dependent_p = false;
2955 if (!processing_template_decl)
2956 /* No names are dependent outside a template. */
2957 ;
2958 /* A template-id where the name of the template was not
2959 resolved is definitely dependent. */
2960 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2961 && (TREE_CODE (TREE_OPERAND (decl, 0))
2962 == IDENTIFIER_NODE))
2963 dependent_p = true;
2964 /* For anything except an overloaded function, just check
2965 its type. */
2966 else if (!is_overloaded_fn (decl))
2967 dependent_p
2968 = cp_parser_dependent_type_p (TREE_TYPE (decl));
2969 /* For a set of overloaded functions, check each of the
2970 functions. */
2971 else
2972 {
2973 tree fns = decl;
2974
2975 if (BASELINK_P (fns))
2976 fns = BASELINK_FUNCTIONS (fns);
2977
2978 /* For a template-id, check to see if the template
2979 arguments are dependent. */
2980 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2981 {
2982 tree args = TREE_OPERAND (fns, 1);
2983
2984 if (args && TREE_CODE (args) == TREE_LIST)
2985 {
2986 while (args)
2987 {
2988 if (cp_parser_dependent_template_arg_p
2989 (TREE_VALUE (args)))
2990 {
2991 dependent_p = true;
2992 break;
2993 }
2994 args = TREE_CHAIN (args);
2995 }
2996 }
2997 else if (args && TREE_CODE (args) == TREE_VEC)
2998 {
2999 int i;
3000 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
3001 if (cp_parser_dependent_template_arg_p
3002 (TREE_VEC_ELT (args, i)))
3003 {
3004 dependent_p = true;
3005 break;
3006 }
3007 }
3008
3009 /* The functions are those referred to by the
3010 template-id. */
3011 fns = TREE_OPERAND (fns, 0);
3012 }
3013
3014 /* If there are no dependent template arguments, go
3015 through the overlaoded functions. */
3016 while (fns && !dependent_p)
3017 {
3018 tree fn = OVL_CURRENT (fns);
3019
3020 /* Member functions of dependent classes are
3021 dependent. */
3022 if (TREE_CODE (fn) == FUNCTION_DECL
3023 && cp_parser_type_dependent_expression_p (fn))
3024 dependent_p = true;
3025 else if (TREE_CODE (fn) == TEMPLATE_DECL
3026 && cp_parser_dependent_template_p (fn))
3027 dependent_p = true;
3028
3029 fns = OVL_NEXT (fns);
3030 }
3031 }
3032
3033 /* If the name was dependent on a template parameter,
3034 we will resolve the name at instantiation time. */
3035 if (dependent_p)
3036 {
3037 /* Create a SCOPE_REF for qualified names. */
3038 if (parser->scope)
3039 {
3040 if (TYPE_P (parser->scope))
3041 *qualifying_class = parser->scope;
3042 return build_nt (SCOPE_REF,
3043 parser->scope,
3044 id_expression);
3045 }
3046 /* A TEMPLATE_ID already contains all the information
3047 we need. */
3048 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
3049 return id_expression;
3050 /* Create a LOOKUP_EXPR for other unqualified names. */
3051 return build_min_nt (LOOKUP_EXPR, id_expression);
3052 }
3053
3054 if (parser->scope)
3055 {
3056 decl = (adjust_result_of_qualified_name_lookup
3057 (decl, parser->scope, current_class_type));
3058 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
3059 *qualifying_class = parser->scope;
3060 }
a723baf1
MM
3061 else
3062 /* Transform references to non-static data members into
3063 COMPONENT_REFs. */
3064 decl = hack_identifier (decl, id_expression);
f74dbcec
JM
3065
3066 /* Resolve references to variables of anonymous unions
3067 into COMPONENT_REFs. */
3068 if (TREE_CODE (decl) == ALIAS_DECL)
3069 decl = DECL_INITIAL (decl);
a723baf1
MM
3070 }
3071
3072 if (TREE_DEPRECATED (decl))
3073 warn_deprecated_use (decl);
3074
3075 return decl;
3076 }
3077
3078 /* Anything else is an error. */
3079 default:
3080 cp_parser_error (parser, "expected primary-expression");
3081 return error_mark_node;
3082 }
3083}
3084
3085/* Parse an id-expression.
3086
3087 id-expression:
3088 unqualified-id
3089 qualified-id
3090
3091 qualified-id:
3092 :: [opt] nested-name-specifier template [opt] unqualified-id
3093 :: identifier
3094 :: operator-function-id
3095 :: template-id
3096
3097 Return a representation of the unqualified portion of the
3098 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3099 a `::' or nested-name-specifier.
3100
3101 Often, if the id-expression was a qualified-id, the caller will
3102 want to make a SCOPE_REF to represent the qualified-id. This
3103 function does not do this in order to avoid wastefully creating
3104 SCOPE_REFs when they are not required.
3105
a723baf1
MM
3106 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3107 `template' keyword.
3108
3109 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3110 uninstantiated templates.
3111
15d2cb19 3112 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1
MM
3113 `template' keyword is used to explicitly indicate that the entity
3114 named is a template. */
3115
3116static tree
3117cp_parser_id_expression (cp_parser *parser,
3118 bool template_keyword_p,
3119 bool check_dependency_p,
3120 bool *template_p)
3121{
3122 bool global_scope_p;
3123 bool nested_name_specifier_p;
3124
3125 /* Assume the `template' keyword was not used. */
3126 if (template_p)
3127 *template_p = false;
3128
3129 /* Look for the optional `::' operator. */
3130 global_scope_p
3131 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3132 != NULL_TREE);
3133 /* Look for the optional nested-name-specifier. */
3134 nested_name_specifier_p
3135 = (cp_parser_nested_name_specifier_opt (parser,
3136 /*typename_keyword_p=*/false,
3137 check_dependency_p,
3138 /*type_p=*/false)
3139 != NULL_TREE);
3140 /* If there is a nested-name-specifier, then we are looking at
3141 the first qualified-id production. */
3142 if (nested_name_specifier_p)
3143 {
3144 tree saved_scope;
3145 tree saved_object_scope;
3146 tree saved_qualifying_scope;
3147 tree unqualified_id;
3148 bool is_template;
3149
3150 /* See if the next token is the `template' keyword. */
3151 if (!template_p)
3152 template_p = &is_template;
3153 *template_p = cp_parser_optional_template_keyword (parser);
3154 /* Name lookup we do during the processing of the
3155 unqualified-id might obliterate SCOPE. */
3156 saved_scope = parser->scope;
3157 saved_object_scope = parser->object_scope;
3158 saved_qualifying_scope = parser->qualifying_scope;
3159 /* Process the final unqualified-id. */
3160 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3161 check_dependency_p);
3162 /* Restore the SAVED_SCOPE for our caller. */
3163 parser->scope = saved_scope;
3164 parser->object_scope = saved_object_scope;
3165 parser->qualifying_scope = saved_qualifying_scope;
3166
3167 return unqualified_id;
3168 }
3169 /* Otherwise, if we are in global scope, then we are looking at one
3170 of the other qualified-id productions. */
3171 else if (global_scope_p)
3172 {
3173 cp_token *token;
3174 tree id;
3175
e5976695
MM
3176 /* Peek at the next token. */
3177 token = cp_lexer_peek_token (parser->lexer);
3178
3179 /* If it's an identifier, and the next token is not a "<", then
3180 we can avoid the template-id case. This is an optimization
3181 for this common case. */
3182 if (token->type == CPP_NAME
3183 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
3184 return cp_parser_identifier (parser);
3185
a723baf1
MM
3186 cp_parser_parse_tentatively (parser);
3187 /* Try a template-id. */
3188 id = cp_parser_template_id (parser,
3189 /*template_keyword_p=*/false,
3190 /*check_dependency_p=*/true);
3191 /* If that worked, we're done. */
3192 if (cp_parser_parse_definitely (parser))
3193 return id;
3194
e5976695
MM
3195 /* Peek at the next token. (Changes in the token buffer may
3196 have invalidated the pointer obtained above.) */
a723baf1
MM
3197 token = cp_lexer_peek_token (parser->lexer);
3198
3199 switch (token->type)
3200 {
3201 case CPP_NAME:
3202 return cp_parser_identifier (parser);
3203
3204 case CPP_KEYWORD:
3205 if (token->keyword == RID_OPERATOR)
3206 return cp_parser_operator_function_id (parser);
3207 /* Fall through. */
3208
3209 default:
3210 cp_parser_error (parser, "expected id-expression");
3211 return error_mark_node;
3212 }
3213 }
3214 else
3215 return cp_parser_unqualified_id (parser, template_keyword_p,
3216 /*check_dependency_p=*/true);
3217}
3218
3219/* Parse an unqualified-id.
3220
3221 unqualified-id:
3222 identifier
3223 operator-function-id
3224 conversion-function-id
3225 ~ class-name
3226 template-id
3227
3228 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3229 keyword, in a construct like `A::template ...'.
3230
3231 Returns a representation of unqualified-id. For the `identifier'
3232 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3233 production a BIT_NOT_EXPR is returned; the operand of the
3234 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3235 other productions, see the documentation accompanying the
3236 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3237 names are looked up in uninstantiated templates. */
3238
3239static tree
3240cp_parser_unqualified_id (parser, template_keyword_p,
3241 check_dependency_p)
3242 cp_parser *parser;
3243 bool template_keyword_p;
3244 bool check_dependency_p;
3245{
3246 cp_token *token;
3247
3248 /* Peek at the next token. */
3249 token = cp_lexer_peek_token (parser->lexer);
3250
3251 switch (token->type)
3252 {
3253 case CPP_NAME:
3254 {
3255 tree id;
3256
3257 /* We don't know yet whether or not this will be a
3258 template-id. */
3259 cp_parser_parse_tentatively (parser);
3260 /* Try a template-id. */
3261 id = cp_parser_template_id (parser, template_keyword_p,
3262 check_dependency_p);
3263 /* If it worked, we're done. */
3264 if (cp_parser_parse_definitely (parser))
3265 return id;
3266 /* Otherwise, it's an ordinary identifier. */
3267 return cp_parser_identifier (parser);
3268 }
3269
3270 case CPP_TEMPLATE_ID:
3271 return cp_parser_template_id (parser, template_keyword_p,
3272 check_dependency_p);
3273
3274 case CPP_COMPL:
3275 {
3276 tree type_decl;
3277 tree qualifying_scope;
3278 tree object_scope;
3279 tree scope;
3280
3281 /* Consume the `~' token. */
3282 cp_lexer_consume_token (parser->lexer);
3283 /* Parse the class-name. The standard, as written, seems to
3284 say that:
3285
3286 template <typename T> struct S { ~S (); };
3287 template <typename T> S<T>::~S() {}
3288
3289 is invalid, since `~' must be followed by a class-name, but
3290 `S<T>' is dependent, and so not known to be a class.
3291 That's not right; we need to look in uninstantiated
3292 templates. A further complication arises from:
3293
3294 template <typename T> void f(T t) {
3295 t.T::~T();
3296 }
3297
3298 Here, it is not possible to look up `T' in the scope of `T'
3299 itself. We must look in both the current scope, and the
3300 scope of the containing complete expression.
3301
3302 Yet another issue is:
3303
3304 struct S {
3305 int S;
3306 ~S();
3307 };
3308
3309 S::~S() {}
3310
3311 The standard does not seem to say that the `S' in `~S'
3312 should refer to the type `S' and not the data member
3313 `S::S'. */
3314
3315 /* DR 244 says that we look up the name after the "~" in the
3316 same scope as we looked up the qualifying name. That idea
3317 isn't fully worked out; it's more complicated than that. */
3318 scope = parser->scope;
3319 object_scope = parser->object_scope;
3320 qualifying_scope = parser->qualifying_scope;
3321
3322 /* If the name is of the form "X::~X" it's OK. */
3323 if (scope && TYPE_P (scope)
3324 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3325 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3326 == CPP_OPEN_PAREN)
3327 && (cp_lexer_peek_token (parser->lexer)->value
3328 == TYPE_IDENTIFIER (scope)))
3329 {
3330 cp_lexer_consume_token (parser->lexer);
3331 return build_nt (BIT_NOT_EXPR, scope);
3332 }
3333
3334 /* If there was an explicit qualification (S::~T), first look
3335 in the scope given by the qualification (i.e., S). */
3336 if (scope)
3337 {
3338 cp_parser_parse_tentatively (parser);
3339 type_decl = cp_parser_class_name (parser,
3340 /*typename_keyword_p=*/false,
3341 /*template_keyword_p=*/false,
3342 /*type_p=*/false,
3343 /*check_access_p=*/true,
3344 /*check_dependency=*/false,
3345 /*class_head_p=*/false);
3346 if (cp_parser_parse_definitely (parser))
3347 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3348 }
3349 /* In "N::S::~S", look in "N" as well. */
3350 if (scope && qualifying_scope)
3351 {
3352 cp_parser_parse_tentatively (parser);
3353 parser->scope = qualifying_scope;
3354 parser->object_scope = NULL_TREE;
3355 parser->qualifying_scope = NULL_TREE;
3356 type_decl
3357 = cp_parser_class_name (parser,
3358 /*typename_keyword_p=*/false,
3359 /*template_keyword_p=*/false,
3360 /*type_p=*/false,
3361 /*check_access_p=*/true,
3362 /*check_dependency=*/false,
3363 /*class_head_p=*/false);
3364 if (cp_parser_parse_definitely (parser))
3365 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3366 }
3367 /* In "p->S::~T", look in the scope given by "*p" as well. */
3368 else if (object_scope)
3369 {
3370 cp_parser_parse_tentatively (parser);
3371 parser->scope = object_scope;
3372 parser->object_scope = NULL_TREE;
3373 parser->qualifying_scope = NULL_TREE;
3374 type_decl
3375 = cp_parser_class_name (parser,
3376 /*typename_keyword_p=*/false,
3377 /*template_keyword_p=*/false,
3378 /*type_p=*/false,
3379 /*check_access_p=*/true,
3380 /*check_dependency=*/false,
3381 /*class_head_p=*/false);
3382 if (cp_parser_parse_definitely (parser))
3383 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3384 }
3385 /* Look in the surrounding context. */
3386 parser->scope = NULL_TREE;
3387 parser->object_scope = NULL_TREE;
3388 parser->qualifying_scope = NULL_TREE;
3389 type_decl
3390 = cp_parser_class_name (parser,
3391 /*typename_keyword_p=*/false,
3392 /*template_keyword_p=*/false,
3393 /*type_p=*/false,
3394 /*check_access_p=*/true,
3395 /*check_dependency=*/false,
3396 /*class_head_p=*/false);
3397 /* If an error occurred, assume that the name of the
3398 destructor is the same as the name of the qualifying
3399 class. That allows us to keep parsing after running
3400 into ill-formed destructor names. */
3401 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3402 return build_nt (BIT_NOT_EXPR, scope);
3403 else if (type_decl == error_mark_node)
3404 return error_mark_node;
3405
3406 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3407 }
3408
3409 case CPP_KEYWORD:
3410 if (token->keyword == RID_OPERATOR)
3411 {
3412 tree id;
3413
3414 /* This could be a template-id, so we try that first. */
3415 cp_parser_parse_tentatively (parser);
3416 /* Try a template-id. */
3417 id = cp_parser_template_id (parser, template_keyword_p,
3418 /*check_dependency_p=*/true);
3419 /* If that worked, we're done. */
3420 if (cp_parser_parse_definitely (parser))
3421 return id;
3422 /* We still don't know whether we're looking at an
3423 operator-function-id or a conversion-function-id. */
3424 cp_parser_parse_tentatively (parser);
3425 /* Try an operator-function-id. */
3426 id = cp_parser_operator_function_id (parser);
3427 /* If that didn't work, try a conversion-function-id. */
3428 if (!cp_parser_parse_definitely (parser))
3429 id = cp_parser_conversion_function_id (parser);
3430
3431 return id;
3432 }
3433 /* Fall through. */
3434
3435 default:
3436 cp_parser_error (parser, "expected unqualified-id");
3437 return error_mark_node;
3438 }
3439}
3440
3441/* Parse an (optional) nested-name-specifier.
3442
3443 nested-name-specifier:
3444 class-or-namespace-name :: nested-name-specifier [opt]
3445 class-or-namespace-name :: template nested-name-specifier [opt]
3446
3447 PARSER->SCOPE should be set appropriately before this function is
3448 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3449 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3450 in name lookups.
3451
3452 Sets PARSER->SCOPE to the class (TYPE) or namespace
3453 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3454 it unchanged if there is no nested-name-specifier. Returns the new
3455 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3456
3457static tree
3458cp_parser_nested_name_specifier_opt (cp_parser *parser,
3459 bool typename_keyword_p,
3460 bool check_dependency_p,
3461 bool type_p)
3462{
3463 bool success = false;
3464 tree access_check = NULL_TREE;
3465 ptrdiff_t start;
2050a1bb 3466 cp_token* token;
a723baf1
MM
3467
3468 /* If the next token corresponds to a nested name specifier, there
2050a1bb
MM
3469 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3470 false, it may have been true before, in which case something
3471 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3472 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3473 CHECK_DEPENDENCY_P is false, we have to fall through into the
3474 main loop. */
3475 if (check_dependency_p
3476 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3477 {
3478 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3479 return parser->scope;
3480 }
3481
3482 /* Remember where the nested-name-specifier starts. */
3483 if (cp_parser_parsing_tentatively (parser)
3484 && !cp_parser_committed_to_tentative_parse (parser))
3485 {
2050a1bb 3486 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
3487 start = cp_lexer_token_difference (parser->lexer,
3488 parser->lexer->first_token,
2050a1bb 3489 token);
a723baf1
MM
3490 access_check = parser->context->deferred_access_checks;
3491 }
3492 else
3493 start = -1;
3494
3495 while (true)
3496 {
3497 tree new_scope;
3498 tree old_scope;
3499 tree saved_qualifying_scope;
a723baf1
MM
3500 bool template_keyword_p;
3501
2050a1bb
MM
3502 /* Spot cases that cannot be the beginning of a
3503 nested-name-specifier. */
3504 token = cp_lexer_peek_token (parser->lexer);
3505
3506 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3507 the already parsed nested-name-specifier. */
3508 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3509 {
3510 /* Grab the nested-name-specifier and continue the loop. */
3511 cp_parser_pre_parsed_nested_name_specifier (parser);
3512 success = true;
3513 continue;
3514 }
3515
a723baf1
MM
3516 /* Spot cases that cannot be the beginning of a
3517 nested-name-specifier. On the second and subsequent times
3518 through the loop, we look for the `template' keyword. */
f7b5ecd9 3519 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3520 ;
3521 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3522 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3523 ;
3524 else
3525 {
3526 /* If the next token is not an identifier, then it is
3527 definitely not a class-or-namespace-name. */
f7b5ecd9 3528 if (token->type != CPP_NAME)
a723baf1
MM
3529 break;
3530 /* If the following token is neither a `<' (to begin a
3531 template-id), nor a `::', then we are not looking at a
3532 nested-name-specifier. */
3533 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3534 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3535 break;
3536 }
3537
3538 /* The nested-name-specifier is optional, so we parse
3539 tentatively. */
3540 cp_parser_parse_tentatively (parser);
3541
3542 /* Look for the optional `template' keyword, if this isn't the
3543 first time through the loop. */
3544 if (success)
3545 template_keyword_p = cp_parser_optional_template_keyword (parser);
3546 else
3547 template_keyword_p = false;
3548
3549 /* Save the old scope since the name lookup we are about to do
3550 might destroy it. */
3551 old_scope = parser->scope;
3552 saved_qualifying_scope = parser->qualifying_scope;
3553 /* Parse the qualifying entity. */
3554 new_scope
3555 = cp_parser_class_or_namespace_name (parser,
3556 typename_keyword_p,
3557 template_keyword_p,
3558 check_dependency_p,
3559 type_p);
3560 /* Look for the `::' token. */
3561 cp_parser_require (parser, CPP_SCOPE, "`::'");
3562
3563 /* If we found what we wanted, we keep going; otherwise, we're
3564 done. */
3565 if (!cp_parser_parse_definitely (parser))
3566 {
3567 bool error_p = false;
3568
3569 /* Restore the OLD_SCOPE since it was valid before the
3570 failed attempt at finding the last
3571 class-or-namespace-name. */
3572 parser->scope = old_scope;
3573 parser->qualifying_scope = saved_qualifying_scope;
3574 /* If the next token is an identifier, and the one after
3575 that is a `::', then any valid interpretation would have
3576 found a class-or-namespace-name. */
3577 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3578 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3579 == CPP_SCOPE)
3580 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3581 != CPP_COMPL))
3582 {
3583 token = cp_lexer_consume_token (parser->lexer);
3584 if (!error_p)
3585 {
3586 tree decl;
3587
3588 decl = cp_parser_lookup_name_simple (parser, token->value);
3589 if (TREE_CODE (decl) == TEMPLATE_DECL)
3590 error ("`%D' used without template parameters",
3591 decl);
3592 else if (parser->scope)
3593 {
3594 if (TYPE_P (parser->scope))
3595 error ("`%T::%D' is not a class-name or "
3596 "namespace-name",
3597 parser->scope, token->value);
3598 else
3599 error ("`%D::%D' is not a class-name or "
3600 "namespace-name",
3601 parser->scope, token->value);
3602 }
3603 else
3604 error ("`%D' is not a class-name or namespace-name",
3605 token->value);
3606 parser->scope = NULL_TREE;
3607 error_p = true;
eea9800f
MM
3608 /* Treat this as a successful nested-name-specifier
3609 due to:
3610
3611 [basic.lookup.qual]
3612
3613 If the name found is not a class-name (clause
3614 _class_) or namespace-name (_namespace.def_), the
3615 program is ill-formed. */
3616 success = true;
a723baf1
MM
3617 }
3618 cp_lexer_consume_token (parser->lexer);
3619 }
3620 break;
3621 }
3622
3623 /* We've found one valid nested-name-specifier. */
3624 success = true;
3625 /* Make sure we look in the right scope the next time through
3626 the loop. */
3627 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3628 ? TREE_TYPE (new_scope)
3629 : new_scope);
3630 /* If it is a class scope, try to complete it; we are about to
3631 be looking up names inside the class. */
3632 if (TYPE_P (parser->scope))
3633 complete_type (parser->scope);
3634 }
3635
3636 /* If parsing tentatively, replace the sequence of tokens that makes
3637 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3638 token. That way, should we re-parse the token stream, we will
3639 not have to repeat the effort required to do the parse, nor will
3640 we issue duplicate error messages. */
3641 if (success && start >= 0)
3642 {
a723baf1
MM
3643 tree c;
3644
3645 /* Find the token that corresponds to the start of the
3646 template-id. */
3647 token = cp_lexer_advance_token (parser->lexer,
3648 parser->lexer->first_token,
3649 start);
3650
3651 /* Remember the access checks associated with this
3652 nested-name-specifier. */
3653 c = parser->context->deferred_access_checks;
3654 if (c == access_check)
3655 access_check = NULL_TREE;
3656 else
3657 {
3658 while (TREE_CHAIN (c) != access_check)
3659 c = TREE_CHAIN (c);
3660 access_check = parser->context->deferred_access_checks;
3661 parser->context->deferred_access_checks = TREE_CHAIN (c);
3662 TREE_CHAIN (c) = NULL_TREE;
3663 }
3664
3665 /* Reset the contents of the START token. */
3666 token->type = CPP_NESTED_NAME_SPECIFIER;
3667 token->value = build_tree_list (access_check, parser->scope);
3668 TREE_TYPE (token->value) = parser->qualifying_scope;
3669 token->keyword = RID_MAX;
3670 /* Purge all subsequent tokens. */
3671 cp_lexer_purge_tokens_after (parser->lexer, token);
3672 }
3673
3674 return success ? parser->scope : NULL_TREE;
3675}
3676
3677/* Parse a nested-name-specifier. See
3678 cp_parser_nested_name_specifier_opt for details. This function
3679 behaves identically, except that it will an issue an error if no
3680 nested-name-specifier is present, and it will return
3681 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3682 is present. */
3683
3684static tree
3685cp_parser_nested_name_specifier (cp_parser *parser,
3686 bool typename_keyword_p,
3687 bool check_dependency_p,
3688 bool type_p)
3689{
3690 tree scope;
3691
3692 /* Look for the nested-name-specifier. */
3693 scope = cp_parser_nested_name_specifier_opt (parser,
3694 typename_keyword_p,
3695 check_dependency_p,
3696 type_p);
3697 /* If it was not present, issue an error message. */
3698 if (!scope)
3699 {
3700 cp_parser_error (parser, "expected nested-name-specifier");
3701 return error_mark_node;
3702 }
3703
3704 return scope;
3705}
3706
3707/* Parse a class-or-namespace-name.
3708
3709 class-or-namespace-name:
3710 class-name
3711 namespace-name
3712
3713 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3714 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3715 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3716 TYPE_P is TRUE iff the next name should be taken as a class-name,
3717 even the same name is declared to be another entity in the same
3718 scope.
3719
3720 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3721 specified by the class-or-namespace-name. If neither is found the
3722 ERROR_MARK_NODE is returned. */
a723baf1
MM
3723
3724static tree
3725cp_parser_class_or_namespace_name (cp_parser *parser,
3726 bool typename_keyword_p,
3727 bool template_keyword_p,
3728 bool check_dependency_p,
3729 bool type_p)
3730{
3731 tree saved_scope;
3732 tree saved_qualifying_scope;
3733 tree saved_object_scope;
3734 tree scope;
eea9800f 3735 bool only_class_p;
a723baf1
MM
3736
3737 /* If the next token is the `template' keyword, we know that we are
3738 looking at a class-name. */
3739 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
3740 return cp_parser_class_name (parser,
3741 typename_keyword_p,
3742 template_keyword_p,
3743 type_p,
3744 /*check_access_p=*/true,
3745 check_dependency_p,
3746 /*class_head_p=*/false);
3747 /* Before we try to parse the class-name, we must save away the
3748 current PARSER->SCOPE since cp_parser_class_name will destroy
3749 it. */
3750 saved_scope = parser->scope;
3751 saved_qualifying_scope = parser->qualifying_scope;
3752 saved_object_scope = parser->object_scope;
eea9800f
MM
3753 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3754 there is no need to look for a namespace-name. */
3755 only_class_p = saved_scope && TYPE_P (saved_scope);
3756 if (!only_class_p)
3757 cp_parser_parse_tentatively (parser);
a723baf1
MM
3758 scope = cp_parser_class_name (parser,
3759 typename_keyword_p,
3760 template_keyword_p,
3761 type_p,
3762 /*check_access_p=*/true,
3763 check_dependency_p,
3764 /*class_head_p=*/false);
3765 /* If that didn't work, try for a namespace-name. */
eea9800f 3766 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3767 {
3768 /* Restore the saved scope. */
3769 parser->scope = saved_scope;
3770 parser->qualifying_scope = saved_qualifying_scope;
3771 parser->object_scope = saved_object_scope;
eea9800f
MM
3772 /* If we are not looking at an identifier followed by the scope
3773 resolution operator, then this is not part of a
3774 nested-name-specifier. (Note that this function is only used
3775 to parse the components of a nested-name-specifier.) */
3776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3777 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3778 return error_mark_node;
a723baf1
MM
3779 scope = cp_parser_namespace_name (parser);
3780 }
3781
3782 return scope;
3783}
3784
3785/* Parse a postfix-expression.
3786
3787 postfix-expression:
3788 primary-expression
3789 postfix-expression [ expression ]
3790 postfix-expression ( expression-list [opt] )
3791 simple-type-specifier ( expression-list [opt] )
3792 typename :: [opt] nested-name-specifier identifier
3793 ( expression-list [opt] )
3794 typename :: [opt] nested-name-specifier template [opt] template-id
3795 ( expression-list [opt] )
3796 postfix-expression . template [opt] id-expression
3797 postfix-expression -> template [opt] id-expression
3798 postfix-expression . pseudo-destructor-name
3799 postfix-expression -> pseudo-destructor-name
3800 postfix-expression ++
3801 postfix-expression --
3802 dynamic_cast < type-id > ( expression )
3803 static_cast < type-id > ( expression )
3804 reinterpret_cast < type-id > ( expression )
3805 const_cast < type-id > ( expression )
3806 typeid ( expression )
3807 typeid ( type-id )
3808
3809 GNU Extension:
3810
3811 postfix-expression:
3812 ( type-id ) { initializer-list , [opt] }
3813
3814 This extension is a GNU version of the C99 compound-literal
3815 construct. (The C99 grammar uses `type-name' instead of `type-id',
3816 but they are essentially the same concept.)
3817
3818 If ADDRESS_P is true, the postfix expression is the operand of the
3819 `&' operator.
3820
3821 Returns a representation of the expression. */
3822
3823static tree
3824cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3825{
3826 cp_token *token;
3827 enum rid keyword;
3828 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3829 tree postfix_expression = NULL_TREE;
3830 /* Non-NULL only if the current postfix-expression can be used to
3831 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3832 class used to qualify the member. */
3833 tree qualifying_class = NULL_TREE;
3834 bool done;
3835
3836 /* Peek at the next token. */
3837 token = cp_lexer_peek_token (parser->lexer);
3838 /* Some of the productions are determined by keywords. */
3839 keyword = token->keyword;
3840 switch (keyword)
3841 {
3842 case RID_DYNCAST:
3843 case RID_STATCAST:
3844 case RID_REINTCAST:
3845 case RID_CONSTCAST:
3846 {
3847 tree type;
3848 tree expression;
3849 const char *saved_message;
3850
3851 /* All of these can be handled in the same way from the point
3852 of view of parsing. Begin by consuming the token
3853 identifying the cast. */
3854 cp_lexer_consume_token (parser->lexer);
3855
3856 /* New types cannot be defined in the cast. */
3857 saved_message = parser->type_definition_forbidden_message;
3858 parser->type_definition_forbidden_message
3859 = "types may not be defined in casts";
3860
3861 /* Look for the opening `<'. */
3862 cp_parser_require (parser, CPP_LESS, "`<'");
3863 /* Parse the type to which we are casting. */
3864 type = cp_parser_type_id (parser);
3865 /* Look for the closing `>'. */
3866 cp_parser_require (parser, CPP_GREATER, "`>'");
3867 /* Restore the old message. */
3868 parser->type_definition_forbidden_message = saved_message;
3869
3870 /* And the expression which is being cast. */
3871 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3872 expression = cp_parser_expression (parser);
3873 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3874
3875 switch (keyword)
3876 {
3877 case RID_DYNCAST:
3878 postfix_expression
3879 = build_dynamic_cast (type, expression);
3880 break;
3881 case RID_STATCAST:
3882 postfix_expression
3883 = build_static_cast (type, expression);
3884 break;
3885 case RID_REINTCAST:
3886 postfix_expression
3887 = build_reinterpret_cast (type, expression);
3888 break;
3889 case RID_CONSTCAST:
3890 postfix_expression
3891 = build_const_cast (type, expression);
3892 break;
3893 default:
3894 abort ();
3895 }
3896 }
3897 break;
3898
3899 case RID_TYPEID:
3900 {
3901 tree type;
3902 const char *saved_message;
3903
3904 /* Consume the `typeid' token. */
3905 cp_lexer_consume_token (parser->lexer);
3906 /* Look for the `(' token. */
3907 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3908 /* Types cannot be defined in a `typeid' expression. */
3909 saved_message = parser->type_definition_forbidden_message;
3910 parser->type_definition_forbidden_message
3911 = "types may not be defined in a `typeid\' expression";
3912 /* We can't be sure yet whether we're looking at a type-id or an
3913 expression. */
3914 cp_parser_parse_tentatively (parser);
3915 /* Try a type-id first. */
3916 type = cp_parser_type_id (parser);
3917 /* Look for the `)' token. Otherwise, we can't be sure that
3918 we're not looking at an expression: consider `typeid (int
3919 (3))', for example. */
3920 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3921 /* If all went well, simply lookup the type-id. */
3922 if (cp_parser_parse_definitely (parser))
3923 postfix_expression = get_typeid (type);
3924 /* Otherwise, fall back to the expression variant. */
3925 else
3926 {
3927 tree expression;
3928
3929 /* Look for an expression. */
3930 expression = cp_parser_expression (parser);
3931 /* Compute its typeid. */
3932 postfix_expression = build_typeid (expression);
3933 /* Look for the `)' token. */
3934 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3935 }
3936
3937 /* Restore the saved message. */
3938 parser->type_definition_forbidden_message = saved_message;
3939 }
3940 break;
3941
3942 case RID_TYPENAME:
3943 {
3944 bool template_p = false;
3945 tree id;
3946 tree type;
3947
3948 /* Consume the `typename' token. */
3949 cp_lexer_consume_token (parser->lexer);
3950 /* Look for the optional `::' operator. */
3951 cp_parser_global_scope_opt (parser,
3952 /*current_scope_valid_p=*/false);
3953 /* Look for the nested-name-specifier. */
3954 cp_parser_nested_name_specifier (parser,
3955 /*typename_keyword_p=*/true,
3956 /*check_dependency_p=*/true,
3957 /*type_p=*/true);
3958 /* Look for the optional `template' keyword. */
3959 template_p = cp_parser_optional_template_keyword (parser);
3960 /* We don't know whether we're looking at a template-id or an
3961 identifier. */
3962 cp_parser_parse_tentatively (parser);
3963 /* Try a template-id. */
3964 id = cp_parser_template_id (parser, template_p,
3965 /*check_dependency_p=*/true);
3966 /* If that didn't work, try an identifier. */
3967 if (!cp_parser_parse_definitely (parser))
3968 id = cp_parser_identifier (parser);
3969 /* Create a TYPENAME_TYPE to represent the type to which the
3970 functional cast is being performed. */
3971 type = make_typename_type (parser->scope, id,
3972 /*complain=*/1);
3973
3974 postfix_expression = cp_parser_functional_cast (parser, type);
3975 }
3976 break;
3977
3978 default:
3979 {
3980 tree type;
3981
3982 /* If the next thing is a simple-type-specifier, we may be
3983 looking at a functional cast. We could also be looking at
3984 an id-expression. So, we try the functional cast, and if
3985 that doesn't work we fall back to the primary-expression. */
3986 cp_parser_parse_tentatively (parser);
3987 /* Look for the simple-type-specifier. */
3988 type = cp_parser_simple_type_specifier (parser,
3989 CP_PARSER_FLAGS_NONE);
3990 /* Parse the cast itself. */
3991 if (!cp_parser_error_occurred (parser))
3992 postfix_expression
3993 = cp_parser_functional_cast (parser, type);
3994 /* If that worked, we're done. */
3995 if (cp_parser_parse_definitely (parser))
3996 break;
3997
3998 /* If the functional-cast didn't work out, try a
3999 compound-literal. */
4000 if (cp_parser_allow_gnu_extensions_p (parser))
4001 {
4002 tree initializer_list = NULL_TREE;
4003
4004 cp_parser_parse_tentatively (parser);
4005 /* Look for the `('. */
4006 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4007 {
4008 type = cp_parser_type_id (parser);
4009 /* Look for the `)'. */
4010 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4011 /* Look for the `{'. */
4012 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4013 /* If things aren't going well, there's no need to
4014 keep going. */
4015 if (!cp_parser_error_occurred (parser))
4016 {
4017 /* Parse the initializer-list. */
4018 initializer_list
4019 = cp_parser_initializer_list (parser);
4020 /* Allow a trailing `,'. */
4021 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4022 cp_lexer_consume_token (parser->lexer);
4023 /* Look for the final `}'. */
4024 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4025 }
4026 }
4027 /* If that worked, we're definitely looking at a
4028 compound-literal expression. */
4029 if (cp_parser_parse_definitely (parser))
4030 {
4031 /* Warn the user that a compound literal is not
4032 allowed in standard C++. */
4033 if (pedantic)
4034 pedwarn ("ISO C++ forbids compound-literals");
4035 /* Form the representation of the compound-literal. */
4036 postfix_expression
4037 = finish_compound_literal (type, initializer_list);
4038 break;
4039 }
4040 }
4041
4042 /* It must be a primary-expression. */
4043 postfix_expression = cp_parser_primary_expression (parser,
4044 &idk,
4045 &qualifying_class);
4046 }
4047 break;
4048 }
4049
4050 /* Peek at the next token. */
4051 token = cp_lexer_peek_token (parser->lexer);
4052 done = (token->type != CPP_OPEN_SQUARE
4053 && token->type != CPP_OPEN_PAREN
4054 && token->type != CPP_DOT
4055 && token->type != CPP_DEREF
4056 && token->type != CPP_PLUS_PLUS
4057 && token->type != CPP_MINUS_MINUS);
4058
4059 /* If the postfix expression is complete, finish up. */
4060 if (address_p && qualifying_class && done)
4061 {
4062 if (TREE_CODE (postfix_expression) == SCOPE_REF)
4063 postfix_expression = TREE_OPERAND (postfix_expression, 1);
4064 postfix_expression
4065 = build_offset_ref (qualifying_class, postfix_expression);
4066 return postfix_expression;
4067 }
4068
4069 /* Otherwise, if we were avoiding committing until we knew
4070 whether or not we had a pointer-to-member, we now know that
4071 the expression is an ordinary reference to a qualified name. */
4072 if (qualifying_class && !processing_template_decl)
4073 {
4074 if (TREE_CODE (postfix_expression) == FIELD_DECL)
4075 postfix_expression
4076 = finish_non_static_data_member (postfix_expression,
4077 qualifying_class);
4078 else if (BASELINK_P (postfix_expression))
4079 {
4080 tree fn;
4081 tree fns;
4082
4083 /* See if any of the functions are non-static members. */
4084 fns = BASELINK_FUNCTIONS (postfix_expression);
4085 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
4086 fns = TREE_OPERAND (fns, 0);
4087 for (fn = fns; fn; fn = OVL_NEXT (fn))
4088 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
4089 break;
4090 /* If so, the expression may be relative to the current
4091 class. */
4092 if (fn && current_class_type
4093 && DERIVED_FROM_P (qualifying_class, current_class_type))
4094 postfix_expression
4095 = (build_class_member_access_expr
4096 (maybe_dummy_object (qualifying_class, NULL),
4097 postfix_expression,
4098 BASELINK_ACCESS_BINFO (postfix_expression),
4099 /*preserve_reference=*/false));
4100 else if (done)
4101 return build_offset_ref (qualifying_class,
4102 postfix_expression);
4103 }
4104 }
4105
4106 /* Remember that there was a reference to this entity. */
4107 if (DECL_P (postfix_expression))
4108 mark_used (postfix_expression);
4109
4110 /* Keep looping until the postfix-expression is complete. */
4111 while (true)
4112 {
4113 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4114 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4115 {
4116 /* It is not a Koenig lookup function call. */
4117 unqualified_name_lookup_error (postfix_expression);
4118 postfix_expression = error_mark_node;
4119 }
4120
4121 /* Peek at the next token. */
4122 token = cp_lexer_peek_token (parser->lexer);
4123
4124 switch (token->type)
4125 {
4126 case CPP_OPEN_SQUARE:
4127 /* postfix-expression [ expression ] */
4128 {
4129 tree index;
4130
4131 /* Consume the `[' token. */
4132 cp_lexer_consume_token (parser->lexer);
4133 /* Parse the index expression. */
4134 index = cp_parser_expression (parser);
4135 /* Look for the closing `]'. */
4136 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4137
4138 /* Build the ARRAY_REF. */
4139 postfix_expression
4140 = grok_array_decl (postfix_expression, index);
4141 idk = CP_PARSER_ID_KIND_NONE;
4142 }
4143 break;
4144
4145 case CPP_OPEN_PAREN:
4146 /* postfix-expression ( expression-list [opt] ) */
4147 {
4148 tree args;
4149
4150 /* Consume the `(' token. */
4151 cp_lexer_consume_token (parser->lexer);
4152 /* If the next token is not a `)', then there are some
4153 arguments. */
4154 if (cp_lexer_next_token_is_not (parser->lexer,
4155 CPP_CLOSE_PAREN))
4156 args = cp_parser_expression_list (parser);
4157 else
4158 args = NULL_TREE;
4159 /* Look for the closing `)'. */
4160 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4161
4162 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4163 && (is_overloaded_fn (postfix_expression)
4164 || DECL_P (postfix_expression)
4165 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4166 && args)
4167 {
4168 tree arg;
4169 tree identifier = NULL_TREE;
4170 tree functions = NULL_TREE;
4171
4172 /* Find the name of the overloaded function. */
4173 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4174 identifier = postfix_expression;
4175 else if (is_overloaded_fn (postfix_expression))
4176 {
4177 functions = postfix_expression;
4178 identifier = DECL_NAME (get_first_fn (functions));
4179 }
4180 else if (DECL_P (postfix_expression))
4181 {
4182 functions = postfix_expression;
4183 identifier = DECL_NAME (postfix_expression);
4184 }
4185
4186 /* A call to a namespace-scope function using an
4187 unqualified name.
4188
4189 Do Koenig lookup -- unless any of the arguments are
4190 type-dependent. */
4191 for (arg = args; arg; arg = TREE_CHAIN (arg))
4192 if (cp_parser_type_dependent_expression_p (TREE_VALUE (arg)))
4193 break;
4194 if (!arg)
4195 {
4196 postfix_expression
4197 = lookup_arg_dependent(identifier, functions, args);
4198 if (!postfix_expression)
4199 {
4200 /* The unqualified name could not be resolved. */
4201 unqualified_name_lookup_error (identifier);
4202 postfix_expression = error_mark_node;
4203 }
4204 postfix_expression
4205 = build_call_from_tree (postfix_expression, args,
4206 /*diallow_virtual=*/false);
4207 break;
4208 }
4209 postfix_expression = build_min_nt (LOOKUP_EXPR,
4210 identifier);
4211 }
4212 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
4213 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4214 {
4215 /* The unqualified name could not be resolved. */
4216 unqualified_name_lookup_error (postfix_expression);
4217 postfix_expression = error_mark_node;
4218 break;
4219 }
4220
4221 /* In the body of a template, no further processing is
4222 required. */
4223 if (processing_template_decl)
4224 {
4225 postfix_expression = build_nt (CALL_EXPR,
4226 postfix_expression,
4227 args);
4228 break;
4229 }
4230
4231 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4232 postfix_expression
4233 = (build_new_method_call
4234 (TREE_OPERAND (postfix_expression, 0),
4235 TREE_OPERAND (postfix_expression, 1),
4236 args, NULL_TREE,
4237 (idk == CP_PARSER_ID_KIND_QUALIFIED
4238 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4239 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
4240 postfix_expression = (build_offset_ref_call_from_tree
4241 (postfix_expression, args));
4242 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
2050a1bb
MM
4243 /* A call to a static class member, or a namespace-scope
4244 function. */
4245 postfix_expression
4246 = finish_call_expr (postfix_expression, args,
4247 /*disallow_virtual=*/true);
a723baf1 4248 else
2050a1bb
MM
4249 /* All other function calls. */
4250 postfix_expression
4251 = finish_call_expr (postfix_expression, args,
4252 /*disallow_virtual=*/false);
a723baf1
MM
4253
4254 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4255 idk = CP_PARSER_ID_KIND_NONE;
4256 }
4257 break;
4258
4259 case CPP_DOT:
4260 case CPP_DEREF:
4261 /* postfix-expression . template [opt] id-expression
4262 postfix-expression . pseudo-destructor-name
4263 postfix-expression -> template [opt] id-expression
4264 postfix-expression -> pseudo-destructor-name */
4265 {
4266 tree name;
4267 bool dependent_p;
4268 bool template_p;
4269 tree scope = NULL_TREE;
4270
4271 /* If this is a `->' operator, dereference the pointer. */
4272 if (token->type == CPP_DEREF)
4273 postfix_expression = build_x_arrow (postfix_expression);
4274 /* Check to see whether or not the expression is
4275 type-dependent. */
4276 dependent_p = (cp_parser_type_dependent_expression_p
4277 (postfix_expression));
4278 /* The identifier following the `->' or `.' is not
4279 qualified. */
4280 parser->scope = NULL_TREE;
4281 parser->qualifying_scope = NULL_TREE;
4282 parser->object_scope = NULL_TREE;
4283 /* Enter the scope corresponding to the type of the object
4284 given by the POSTFIX_EXPRESSION. */
4285 if (!dependent_p
4286 && TREE_TYPE (postfix_expression) != NULL_TREE)
4287 {
4288 scope = TREE_TYPE (postfix_expression);
4289 /* According to the standard, no expression should
4290 ever have reference type. Unfortunately, we do not
4291 currently match the standard in this respect in
4292 that our internal representation of an expression
4293 may have reference type even when the standard says
4294 it does not. Therefore, we have to manually obtain
4295 the underlying type here. */
4296 if (TREE_CODE (scope) == REFERENCE_TYPE)
4297 scope = TREE_TYPE (scope);
4298 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4299 type of the field. We get an OFFSET_TYPE for
4300 something like:
4301
4302 S::T.a ...
4303
4304 Probably, we should not get an OFFSET_TYPE here;
4305 that transformation should be made only if `&S::T'
4306 is written. */
4307 if (TREE_CODE (scope) == OFFSET_TYPE)
4308 scope = TREE_TYPE (scope);
4309 /* The type of the POSTFIX_EXPRESSION must be
4310 complete. */
4311 scope = complete_type_or_else (scope, NULL_TREE);
4312 /* Let the name lookup machinery know that we are
4313 processing a class member access expression. */
4314 parser->context->object_type = scope;
4315 /* If something went wrong, we want to be able to
4316 discern that case, as opposed to the case where
4317 there was no SCOPE due to the type of expression
4318 being dependent. */
4319 if (!scope)
4320 scope = error_mark_node;
4321 }
4322
4323 /* Consume the `.' or `->' operator. */
4324 cp_lexer_consume_token (parser->lexer);
4325 /* If the SCOPE is not a scalar type, we are looking at an
4326 ordinary class member access expression, rather than a
4327 pseudo-destructor-name. */
4328 if (!scope || !SCALAR_TYPE_P (scope))
4329 {
4330 template_p = cp_parser_optional_template_keyword (parser);
4331 /* Parse the id-expression. */
4332 name = cp_parser_id_expression (parser,
4333 template_p,
4334 /*check_dependency_p=*/true,
4335 /*template_p=*/NULL);
4336 /* In general, build a SCOPE_REF if the member name is
4337 qualified. However, if the name was not dependent
4338 and has already been resolved; there is no need to
4339 build the SCOPE_REF. For example;
4340
4341 struct X { void f(); };
4342 template <typename T> void f(T* t) { t->X::f(); }
4343
4344 Even though "t" is dependent, "X::f" is not and has
4345 except that for a BASELINK there is no need to
4346 include scope information. */
4347 if (name != error_mark_node
4348 && !BASELINK_P (name)
4349 && parser->scope)
4350 {
4351 name = build_nt (SCOPE_REF, parser->scope, name);
4352 parser->scope = NULL_TREE;
4353 parser->qualifying_scope = NULL_TREE;
4354 parser->object_scope = NULL_TREE;
4355 }
4356 postfix_expression
4357 = finish_class_member_access_expr (postfix_expression, name);
4358 }
4359 /* Otherwise, try the pseudo-destructor-name production. */
4360 else
4361 {
4362 tree s;
4363 tree type;
4364
4365 /* Parse the pseudo-destructor-name. */
4366 cp_parser_pseudo_destructor_name (parser, &s, &type);
4367 /* Form the call. */
4368 postfix_expression
4369 = finish_pseudo_destructor_expr (postfix_expression,
4370 s, TREE_TYPE (type));
4371 }
4372
4373 /* We no longer need to look up names in the scope of the
4374 object on the left-hand side of the `.' or `->'
4375 operator. */
4376 parser->context->object_type = NULL_TREE;
4377 idk = CP_PARSER_ID_KIND_NONE;
4378 }
4379 break;
4380
4381 case CPP_PLUS_PLUS:
4382 /* postfix-expression ++ */
4383 /* Consume the `++' token. */
4384 cp_lexer_consume_token (parser->lexer);
4385 /* Generate a reprsentation for the complete expression. */
4386 postfix_expression
4387 = finish_increment_expr (postfix_expression,
4388 POSTINCREMENT_EXPR);
4389 idk = CP_PARSER_ID_KIND_NONE;
4390 break;
4391
4392 case CPP_MINUS_MINUS:
4393 /* postfix-expression -- */
4394 /* Consume the `--' token. */
4395 cp_lexer_consume_token (parser->lexer);
4396 /* Generate a reprsentation for the complete expression. */
4397 postfix_expression
4398 = finish_increment_expr (postfix_expression,
4399 POSTDECREMENT_EXPR);
4400 idk = CP_PARSER_ID_KIND_NONE;
4401 break;
4402
4403 default:
4404 return postfix_expression;
4405 }
4406 }
4407
4408 /* We should never get here. */
4409 abort ();
4410 return error_mark_node;
4411}
4412
4413/* Parse an expression-list.
4414
4415 expression-list:
4416 assignment-expression
4417 expression-list, assignment-expression
4418
4419 Returns a TREE_LIST. The TREE_VALUE of each node is a
4420 representation of an assignment-expression. Note that a TREE_LIST
4421 is returned even if there is only a single expression in the list. */
4422
4423static tree
4424cp_parser_expression_list (parser)
4425 cp_parser *parser;
4426{
4427 tree expression_list = NULL_TREE;
4428
4429 /* Consume expressions until there are no more. */
4430 while (true)
4431 {
4432 tree expr;
4433
4434 /* Parse the next assignment-expression. */
4435 expr = cp_parser_assignment_expression (parser);
4436 /* Add it to the list. */
4437 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4438
4439 /* If the next token isn't a `,', then we are done. */
4440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4441 {
4442 /* All uses of expression-list in the grammar are followed
4443 by a `)'. Therefore, if the next token is not a `)' an
4444 error will be issued, unless we are parsing tentatively.
4445 Skip ahead to see if there is another `,' before the `)';
4446 if so, we can go there and recover. */
4447 if (cp_parser_parsing_tentatively (parser)
4448 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4449 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4450 break;
4451 }
4452
4453 /* Otherwise, consume the `,' and keep going. */
4454 cp_lexer_consume_token (parser->lexer);
4455 }
4456
4457 /* We built up the list in reverse order so we must reverse it now. */
4458 return nreverse (expression_list);
4459}
4460
4461/* Parse a pseudo-destructor-name.
4462
4463 pseudo-destructor-name:
4464 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4465 :: [opt] nested-name-specifier template template-id :: ~ type-name
4466 :: [opt] nested-name-specifier [opt] ~ type-name
4467
4468 If either of the first two productions is used, sets *SCOPE to the
4469 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4470 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4471 or ERROR_MARK_NODE if no type-name is present. */
4472
4473static void
4474cp_parser_pseudo_destructor_name (parser, scope, type)
4475 cp_parser *parser;
4476 tree *scope;
4477 tree *type;
4478{
4479 bool nested_name_specifier_p;
4480
4481 /* Look for the optional `::' operator. */
4482 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4483 /* Look for the optional nested-name-specifier. */
4484 nested_name_specifier_p
4485 = (cp_parser_nested_name_specifier_opt (parser,
4486 /*typename_keyword_p=*/false,
4487 /*check_dependency_p=*/true,
4488 /*type_p=*/false)
4489 != NULL_TREE);
4490 /* Now, if we saw a nested-name-specifier, we might be doing the
4491 second production. */
4492 if (nested_name_specifier_p
4493 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4494 {
4495 /* Consume the `template' keyword. */
4496 cp_lexer_consume_token (parser->lexer);
4497 /* Parse the template-id. */
4498 cp_parser_template_id (parser,
4499 /*template_keyword_p=*/true,
4500 /*check_dependency_p=*/false);
4501 /* Look for the `::' token. */
4502 cp_parser_require (parser, CPP_SCOPE, "`::'");
4503 }
4504 /* If the next token is not a `~', then there might be some
4505 additional qualification. */
4506 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4507 {
4508 /* Look for the type-name. */
4509 *scope = TREE_TYPE (cp_parser_type_name (parser));
4510 /* Look for the `::' token. */
4511 cp_parser_require (parser, CPP_SCOPE, "`::'");
4512 }
4513 else
4514 *scope = NULL_TREE;
4515
4516 /* Look for the `~'. */
4517 cp_parser_require (parser, CPP_COMPL, "`~'");
4518 /* Look for the type-name again. We are not responsible for
4519 checking that it matches the first type-name. */
4520 *type = cp_parser_type_name (parser);
4521}
4522
4523/* Parse a unary-expression.
4524
4525 unary-expression:
4526 postfix-expression
4527 ++ cast-expression
4528 -- cast-expression
4529 unary-operator cast-expression
4530 sizeof unary-expression
4531 sizeof ( type-id )
4532 new-expression
4533 delete-expression
4534
4535 GNU Extensions:
4536
4537 unary-expression:
4538 __extension__ cast-expression
4539 __alignof__ unary-expression
4540 __alignof__ ( type-id )
4541 __real__ cast-expression
4542 __imag__ cast-expression
4543 && identifier
4544
4545 ADDRESS_P is true iff the unary-expression is appearing as the
4546 operand of the `&' operator.
4547
4548 Returns a representation of the expresion. */
4549
4550static tree
4551cp_parser_unary_expression (cp_parser *parser, bool address_p)
4552{
4553 cp_token *token;
4554 enum tree_code unary_operator;
4555
4556 /* Peek at the next token. */
4557 token = cp_lexer_peek_token (parser->lexer);
4558 /* Some keywords give away the kind of expression. */
4559 if (token->type == CPP_KEYWORD)
4560 {
4561 enum rid keyword = token->keyword;
4562
4563 switch (keyword)
4564 {
4565 case RID_ALIGNOF:
4566 {
4567 /* Consume the `alignof' token. */
4568 cp_lexer_consume_token (parser->lexer);
4569 /* Parse the operand. */
4570 return finish_alignof (cp_parser_sizeof_operand
4571 (parser, keyword));
4572 }
4573
4574 case RID_SIZEOF:
4575 {
4576 tree operand;
4577
4578 /* Consume the `sizeof' token. */
4579 cp_lexer_consume_token (parser->lexer);
4580 /* Parse the operand. */
4581 operand = cp_parser_sizeof_operand (parser, keyword);
4582
4583 /* If the type of the operand cannot be determined build a
4584 SIZEOF_EXPR. */
4585 if (TYPE_P (operand)
4586 ? cp_parser_dependent_type_p (operand)
4587 : cp_parser_type_dependent_expression_p (operand))
4588 return build_min (SIZEOF_EXPR, size_type_node, operand);
4589 /* Otherwise, compute the constant value. */
4590 else
4591 return finish_sizeof (operand);
4592 }
4593
4594 case RID_NEW:
4595 return cp_parser_new_expression (parser);
4596
4597 case RID_DELETE:
4598 return cp_parser_delete_expression (parser);
4599
4600 case RID_EXTENSION:
4601 {
4602 /* The saved value of the PEDANTIC flag. */
4603 int saved_pedantic;
4604 tree expr;
4605
4606 /* Save away the PEDANTIC flag. */
4607 cp_parser_extension_opt (parser, &saved_pedantic);
4608 /* Parse the cast-expression. */
4609 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4610 /* Restore the PEDANTIC flag. */
4611 pedantic = saved_pedantic;
4612
4613 return expr;
4614 }
4615
4616 case RID_REALPART:
4617 case RID_IMAGPART:
4618 {
4619 tree expression;
4620
4621 /* Consume the `__real__' or `__imag__' token. */
4622 cp_lexer_consume_token (parser->lexer);
4623 /* Parse the cast-expression. */
4624 expression = cp_parser_cast_expression (parser,
4625 /*address_p=*/false);
4626 /* Create the complete representation. */
4627 return build_x_unary_op ((keyword == RID_REALPART
4628 ? REALPART_EXPR : IMAGPART_EXPR),
4629 expression);
4630 }
4631 break;
4632
4633 default:
4634 break;
4635 }
4636 }
4637
4638 /* Look for the `:: new' and `:: delete', which also signal the
4639 beginning of a new-expression, or delete-expression,
4640 respectively. If the next token is `::', then it might be one of
4641 these. */
4642 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4643 {
4644 enum rid keyword;
4645
4646 /* See if the token after the `::' is one of the keywords in
4647 which we're interested. */
4648 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4649 /* If it's `new', we have a new-expression. */
4650 if (keyword == RID_NEW)
4651 return cp_parser_new_expression (parser);
4652 /* Similarly, for `delete'. */
4653 else if (keyword == RID_DELETE)
4654 return cp_parser_delete_expression (parser);
4655 }
4656
4657 /* Look for a unary operator. */
4658 unary_operator = cp_parser_unary_operator (token);
4659 /* The `++' and `--' operators can be handled similarly, even though
4660 they are not technically unary-operators in the grammar. */
4661 if (unary_operator == ERROR_MARK)
4662 {
4663 if (token->type == CPP_PLUS_PLUS)
4664 unary_operator = PREINCREMENT_EXPR;
4665 else if (token->type == CPP_MINUS_MINUS)
4666 unary_operator = PREDECREMENT_EXPR;
4667 /* Handle the GNU address-of-label extension. */
4668 else if (cp_parser_allow_gnu_extensions_p (parser)
4669 && token->type == CPP_AND_AND)
4670 {
4671 tree identifier;
4672
4673 /* Consume the '&&' token. */
4674 cp_lexer_consume_token (parser->lexer);
4675 /* Look for the identifier. */
4676 identifier = cp_parser_identifier (parser);
4677 /* Create an expression representing the address. */
4678 return finish_label_address_expr (identifier);
4679 }
4680 }
4681 if (unary_operator != ERROR_MARK)
4682 {
4683 tree cast_expression;
4684
4685 /* Consume the operator token. */
4686 token = cp_lexer_consume_token (parser->lexer);
4687 /* Parse the cast-expression. */
4688 cast_expression
4689 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4690 /* Now, build an appropriate representation. */
4691 switch (unary_operator)
4692 {
4693 case INDIRECT_REF:
4694 return build_x_indirect_ref (cast_expression, "unary *");
4695
4696 case ADDR_EXPR:
4697 return build_x_unary_op (ADDR_EXPR, cast_expression);
4698
4699 case CONVERT_EXPR:
4700 case NEGATE_EXPR:
4701 case TRUTH_NOT_EXPR:
4702 case PREINCREMENT_EXPR:
4703 case PREDECREMENT_EXPR:
4704 return finish_unary_op_expr (unary_operator, cast_expression);
4705
4706 case BIT_NOT_EXPR:
4707 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4708
4709 default:
4710 abort ();
4711 return error_mark_node;
4712 }
4713 }
4714
4715 return cp_parser_postfix_expression (parser, address_p);
4716}
4717
4718/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4719 unary-operator, the corresponding tree code is returned. */
4720
4721static enum tree_code
4722cp_parser_unary_operator (token)
4723 cp_token *token;
4724{
4725 switch (token->type)
4726 {
4727 case CPP_MULT:
4728 return INDIRECT_REF;
4729
4730 case CPP_AND:
4731 return ADDR_EXPR;
4732
4733 case CPP_PLUS:
4734 return CONVERT_EXPR;
4735
4736 case CPP_MINUS:
4737 return NEGATE_EXPR;
4738
4739 case CPP_NOT:
4740 return TRUTH_NOT_EXPR;
4741
4742 case CPP_COMPL:
4743 return BIT_NOT_EXPR;
4744
4745 default:
4746 return ERROR_MARK;
4747 }
4748}
4749
4750/* Parse a new-expression.
4751
4752 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4753 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4754
4755 Returns a representation of the expression. */
4756
4757static tree
4758cp_parser_new_expression (parser)
4759 cp_parser *parser;
4760{
4761 bool global_scope_p;
4762 tree placement;
4763 tree type;
4764 tree initializer;
4765
4766 /* Look for the optional `::' operator. */
4767 global_scope_p
4768 = (cp_parser_global_scope_opt (parser,
4769 /*current_scope_valid_p=*/false)
4770 != NULL_TREE);
4771 /* Look for the `new' operator. */
4772 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4773 /* There's no easy way to tell a new-placement from the
4774 `( type-id )' construct. */
4775 cp_parser_parse_tentatively (parser);
4776 /* Look for a new-placement. */
4777 placement = cp_parser_new_placement (parser);
4778 /* If that didn't work out, there's no new-placement. */
4779 if (!cp_parser_parse_definitely (parser))
4780 placement = NULL_TREE;
4781
4782 /* If the next token is a `(', then we have a parenthesized
4783 type-id. */
4784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4785 {
4786 /* Consume the `('. */
4787 cp_lexer_consume_token (parser->lexer);
4788 /* Parse the type-id. */
4789 type = cp_parser_type_id (parser);
4790 /* Look for the closing `)'. */
4791 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4792 }
4793 /* Otherwise, there must be a new-type-id. */
4794 else
4795 type = cp_parser_new_type_id (parser);
4796
4797 /* If the next token is a `(', then we have a new-initializer. */
4798 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4799 initializer = cp_parser_new_initializer (parser);
4800 else
4801 initializer = NULL_TREE;
4802
4803 /* Create a representation of the new-expression. */
4804 return build_new (placement, type, initializer, global_scope_p);
4805}
4806
4807/* Parse a new-placement.
4808
4809 new-placement:
4810 ( expression-list )
4811
4812 Returns the same representation as for an expression-list. */
4813
4814static tree
4815cp_parser_new_placement (parser)
4816 cp_parser *parser;
4817{
4818 tree expression_list;
4819
4820 /* Look for the opening `('. */
4821 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4822 return error_mark_node;
4823 /* Parse the expression-list. */
4824 expression_list = cp_parser_expression_list (parser);
4825 /* Look for the closing `)'. */
4826 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4827
4828 return expression_list;
4829}
4830
4831/* Parse a new-type-id.
4832
4833 new-type-id:
4834 type-specifier-seq new-declarator [opt]
4835
4836 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4837 and whose TREE_VALUE is the new-declarator. */
4838
4839static tree
4840cp_parser_new_type_id (parser)
4841 cp_parser *parser;
4842{
4843 tree type_specifier_seq;
4844 tree declarator;
4845 const char *saved_message;
4846
4847 /* The type-specifier sequence must not contain type definitions.
4848 (It cannot contain declarations of new types either, but if they
4849 are not definitions we will catch that because they are not
4850 complete.) */
4851 saved_message = parser->type_definition_forbidden_message;
4852 parser->type_definition_forbidden_message
4853 = "types may not be defined in a new-type-id";
4854 /* Parse the type-specifier-seq. */
4855 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4856 /* Restore the old message. */
4857 parser->type_definition_forbidden_message = saved_message;
4858 /* Parse the new-declarator. */
4859 declarator = cp_parser_new_declarator_opt (parser);
4860
4861 return build_tree_list (type_specifier_seq, declarator);
4862}
4863
4864/* Parse an (optional) new-declarator.
4865
4866 new-declarator:
4867 ptr-operator new-declarator [opt]
4868 direct-new-declarator
4869
4870 Returns a representation of the declarator. See
4871 cp_parser_declarator for the representations used. */
4872
4873static tree
4874cp_parser_new_declarator_opt (parser)
4875 cp_parser *parser;
4876{
4877 enum tree_code code;
4878 tree type;
4879 tree cv_qualifier_seq;
4880
4881 /* We don't know if there's a ptr-operator next, or not. */
4882 cp_parser_parse_tentatively (parser);
4883 /* Look for a ptr-operator. */
4884 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4885 /* If that worked, look for more new-declarators. */
4886 if (cp_parser_parse_definitely (parser))
4887 {
4888 tree declarator;
4889
4890 /* Parse another optional declarator. */
4891 declarator = cp_parser_new_declarator_opt (parser);
4892
4893 /* Create the representation of the declarator. */
4894 if (code == INDIRECT_REF)
4895 declarator = make_pointer_declarator (cv_qualifier_seq,
4896 declarator);
4897 else
4898 declarator = make_reference_declarator (cv_qualifier_seq,
4899 declarator);
4900
4901 /* Handle the pointer-to-member case. */
4902 if (type)
4903 declarator = build_nt (SCOPE_REF, type, declarator);
4904
4905 return declarator;
4906 }
4907
4908 /* If the next token is a `[', there is a direct-new-declarator. */
4909 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4910 return cp_parser_direct_new_declarator (parser);
4911
4912 return NULL_TREE;
4913}
4914
4915/* Parse a direct-new-declarator.
4916
4917 direct-new-declarator:
4918 [ expression ]
4919 direct-new-declarator [constant-expression]
4920
4921 Returns an ARRAY_REF, following the same conventions as are
4922 documented for cp_parser_direct_declarator. */
4923
4924static tree
4925cp_parser_direct_new_declarator (parser)
4926 cp_parser *parser;
4927{
4928 tree declarator = NULL_TREE;
4929
4930 while (true)
4931 {
4932 tree expression;
4933
4934 /* Look for the opening `['. */
4935 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4936 /* The first expression is not required to be constant. */
4937 if (!declarator)
4938 {
4939 expression = cp_parser_expression (parser);
4940 /* The standard requires that the expression have integral
4941 type. DR 74 adds enumeration types. We believe that the
4942 real intent is that these expressions be handled like the
4943 expression in a `switch' condition, which also allows
4944 classes with a single conversion to integral or
4945 enumeration type. */
4946 if (!processing_template_decl)
4947 {
4948 expression
4949 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4950 expression,
b746c5dc 4951 /*complain=*/true);
a723baf1
MM
4952 if (!expression)
4953 {
4954 error ("expression in new-declarator must have integral or enumeration type");
4955 expression = error_mark_node;
4956 }
4957 }
4958 }
4959 /* But all the other expressions must be. */
4960 else
4961 expression = cp_parser_constant_expression (parser);
4962 /* Look for the closing `]'. */
4963 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4964
4965 /* Add this bound to the declarator. */
4966 declarator = build_nt (ARRAY_REF, declarator, expression);
4967
4968 /* If the next token is not a `[', then there are no more
4969 bounds. */
4970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4971 break;
4972 }
4973
4974 return declarator;
4975}
4976
4977/* Parse a new-initializer.
4978
4979 new-initializer:
4980 ( expression-list [opt] )
4981
4982 Returns a reprsentation of the expression-list. If there is no
4983 expression-list, VOID_ZERO_NODE is returned. */
4984
4985static tree
4986cp_parser_new_initializer (parser)
4987 cp_parser *parser;
4988{
4989 tree expression_list;
4990
4991 /* Look for the opening parenthesis. */
4992 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4993 /* If the next token is not a `)', then there is an
4994 expression-list. */
4995 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4996 expression_list = cp_parser_expression_list (parser);
4997 else
4998 expression_list = void_zero_node;
4999 /* Look for the closing parenthesis. */
5000 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5001
5002 return expression_list;
5003}
5004
5005/* Parse a delete-expression.
5006
5007 delete-expression:
5008 :: [opt] delete cast-expression
5009 :: [opt] delete [ ] cast-expression
5010
5011 Returns a representation of the expression. */
5012
5013static tree
5014cp_parser_delete_expression (parser)
5015 cp_parser *parser;
5016{
5017 bool global_scope_p;
5018 bool array_p;
5019 tree expression;
5020
5021 /* Look for the optional `::' operator. */
5022 global_scope_p
5023 = (cp_parser_global_scope_opt (parser,
5024 /*current_scope_valid_p=*/false)
5025 != NULL_TREE);
5026 /* Look for the `delete' keyword. */
5027 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5028 /* See if the array syntax is in use. */
5029 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5030 {
5031 /* Consume the `[' token. */
5032 cp_lexer_consume_token (parser->lexer);
5033 /* Look for the `]' token. */
5034 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5035 /* Remember that this is the `[]' construct. */
5036 array_p = true;
5037 }
5038 else
5039 array_p = false;
5040
5041 /* Parse the cast-expression. */
5042 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
5043
5044 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5045}
5046
5047/* Parse a cast-expression.
5048
5049 cast-expression:
5050 unary-expression
5051 ( type-id ) cast-expression
5052
5053 Returns a representation of the expression. */
5054
5055static tree
5056cp_parser_cast_expression (cp_parser *parser, bool address_p)
5057{
5058 /* If it's a `(', then we might be looking at a cast. */
5059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5060 {
5061 tree type = NULL_TREE;
5062 tree expr = NULL_TREE;
5063 bool compound_literal_p;
5064 const char *saved_message;
5065
5066 /* There's no way to know yet whether or not this is a cast.
5067 For example, `(int (3))' is a unary-expression, while `(int)
5068 3' is a cast. So, we resort to parsing tentatively. */
5069 cp_parser_parse_tentatively (parser);
5070 /* Types may not be defined in a cast. */
5071 saved_message = parser->type_definition_forbidden_message;
5072 parser->type_definition_forbidden_message
5073 = "types may not be defined in casts";
5074 /* Consume the `('. */
5075 cp_lexer_consume_token (parser->lexer);
5076 /* A very tricky bit is that `(struct S) { 3 }' is a
5077 compound-literal (which we permit in C++ as an extension).
5078 But, that construct is not a cast-expression -- it is a
5079 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5080 is legal; if the compound-literal were a cast-expression,
5081 you'd need an extra set of parentheses.) But, if we parse
5082 the type-id, and it happens to be a class-specifier, then we
5083 will commit to the parse at that point, because we cannot
5084 undo the action that is done when creating a new class. So,
5085 then we cannot back up and do a postfix-expression.
5086
5087 Therefore, we scan ahead to the closing `)', and check to see
5088 if the token after the `)' is a `{'. If so, we are not
5089 looking at a cast-expression.
5090
5091 Save tokens so that we can put them back. */
5092 cp_lexer_save_tokens (parser->lexer);
5093 /* Skip tokens until the next token is a closing parenthesis.
5094 If we find the closing `)', and the next token is a `{', then
5095 we are looking at a compound-literal. */
5096 compound_literal_p
5097 = (cp_parser_skip_to_closing_parenthesis (parser)
5098 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5099 /* Roll back the tokens we skipped. */
5100 cp_lexer_rollback_tokens (parser->lexer);
5101 /* If we were looking at a compound-literal, simulate an error
5102 so that the call to cp_parser_parse_definitely below will
5103 fail. */
5104 if (compound_literal_p)
5105 cp_parser_simulate_error (parser);
5106 else
5107 {
5108 /* Look for the type-id. */
5109 type = cp_parser_type_id (parser);
5110 /* Look for the closing `)'. */
5111 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5112 }
5113
5114 /* Restore the saved message. */
5115 parser->type_definition_forbidden_message = saved_message;
5116
5117 /* If all went well, this is a cast. */
5118 if (cp_parser_parse_definitely (parser))
5119 {
5120 /* Parse the dependent expression. */
5121 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5122 /* Warn about old-style casts, if so requested. */
5123 if (warn_old_style_cast
5124 && !in_system_header
5125 && !VOID_TYPE_P (type)
5126 && current_lang_name != lang_name_c)
5127 warning ("use of old-style cast");
5128 /* Perform the cast. */
5129 expr = build_c_cast (type, expr);
5130 }
5131
5132 if (expr)
5133 return expr;
5134 }
5135
5136 /* If we get here, then it's not a cast, so it must be a
5137 unary-expression. */
5138 return cp_parser_unary_expression (parser, address_p);
5139}
5140
5141/* Parse a pm-expression.
5142
5143 pm-expression:
5144 cast-expression
5145 pm-expression .* cast-expression
5146 pm-expression ->* cast-expression
5147
5148 Returns a representation of the expression. */
5149
5150static tree
5151cp_parser_pm_expression (parser)
5152 cp_parser *parser;
5153{
5154 tree cast_expr;
5155 tree pm_expr;
5156
5157 /* Parse the cast-expresion. */
5158 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5159 pm_expr = cast_expr;
5160 /* Now look for pointer-to-member operators. */
5161 while (true)
5162 {
5163 cp_token *token;
5164 enum cpp_ttype token_type;
5165
5166 /* Peek at the next token. */
5167 token = cp_lexer_peek_token (parser->lexer);
5168 token_type = token->type;
5169 /* If it's not `.*' or `->*' there's no pointer-to-member
5170 operation. */
5171 if (token_type != CPP_DOT_STAR
5172 && token_type != CPP_DEREF_STAR)
5173 break;
5174
5175 /* Consume the token. */
5176 cp_lexer_consume_token (parser->lexer);
5177
5178 /* Parse another cast-expression. */
5179 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
5180
5181 /* Build the representation of the pointer-to-member
5182 operation. */
5183 if (token_type == CPP_DEREF_STAR)
5184 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
5185 else
5186 pm_expr = build_m_component_ref (pm_expr, cast_expr);
5187 }
5188
5189 return pm_expr;
5190}
5191
5192/* Parse a multiplicative-expression.
5193
5194 mulitplicative-expression:
5195 pm-expression
5196 multiplicative-expression * pm-expression
5197 multiplicative-expression / pm-expression
5198 multiplicative-expression % pm-expression
5199
5200 Returns a representation of the expression. */
5201
5202static tree
5203cp_parser_multiplicative_expression (parser)
5204 cp_parser *parser;
5205{
39b1af70 5206 static const cp_parser_token_tree_map map = {
a723baf1
MM
5207 { CPP_MULT, MULT_EXPR },
5208 { CPP_DIV, TRUNC_DIV_EXPR },
5209 { CPP_MOD, TRUNC_MOD_EXPR },
5210 { CPP_EOF, ERROR_MARK }
5211 };
5212
5213 return cp_parser_binary_expression (parser,
5214 map,
5215 cp_parser_pm_expression);
5216}
5217
5218/* Parse an additive-expression.
5219
5220 additive-expression:
5221 multiplicative-expression
5222 additive-expression + multiplicative-expression
5223 additive-expression - multiplicative-expression
5224
5225 Returns a representation of the expression. */
5226
5227static tree
5228cp_parser_additive_expression (parser)
5229 cp_parser *parser;
5230{
39b1af70 5231 static const cp_parser_token_tree_map map = {
a723baf1
MM
5232 { CPP_PLUS, PLUS_EXPR },
5233 { CPP_MINUS, MINUS_EXPR },
5234 { CPP_EOF, ERROR_MARK }
5235 };
5236
5237 return cp_parser_binary_expression (parser,
5238 map,
5239 cp_parser_multiplicative_expression);
5240}
5241
5242/* Parse a shift-expression.
5243
5244 shift-expression:
5245 additive-expression
5246 shift-expression << additive-expression
5247 shift-expression >> additive-expression
5248
5249 Returns a representation of the expression. */
5250
5251static tree
5252cp_parser_shift_expression (parser)
5253 cp_parser *parser;
5254{
39b1af70 5255 static const cp_parser_token_tree_map map = {
a723baf1
MM
5256 { CPP_LSHIFT, LSHIFT_EXPR },
5257 { CPP_RSHIFT, RSHIFT_EXPR },
5258 { CPP_EOF, ERROR_MARK }
5259 };
5260
5261 return cp_parser_binary_expression (parser,
5262 map,
5263 cp_parser_additive_expression);
5264}
5265
5266/* Parse a relational-expression.
5267
5268 relational-expression:
5269 shift-expression
5270 relational-expression < shift-expression
5271 relational-expression > shift-expression
5272 relational-expression <= shift-expression
5273 relational-expression >= shift-expression
5274
5275 GNU Extension:
5276
5277 relational-expression:
5278 relational-expression <? shift-expression
5279 relational-expression >? shift-expression
5280
5281 Returns a representation of the expression. */
5282
5283static tree
5284cp_parser_relational_expression (parser)
5285 cp_parser *parser;
5286{
39b1af70 5287 static const cp_parser_token_tree_map map = {
a723baf1
MM
5288 { CPP_LESS, LT_EXPR },
5289 { CPP_GREATER, GT_EXPR },
5290 { CPP_LESS_EQ, LE_EXPR },
5291 { CPP_GREATER_EQ, GE_EXPR },
5292 { CPP_MIN, MIN_EXPR },
5293 { CPP_MAX, MAX_EXPR },
5294 { CPP_EOF, ERROR_MARK }
5295 };
5296
5297 return cp_parser_binary_expression (parser,
5298 map,
5299 cp_parser_shift_expression);
5300}
5301
5302/* Parse an equality-expression.
5303
5304 equality-expression:
5305 relational-expression
5306 equality-expression == relational-expression
5307 equality-expression != relational-expression
5308
5309 Returns a representation of the expression. */
5310
5311static tree
5312cp_parser_equality_expression (parser)
5313 cp_parser *parser;
5314{
39b1af70 5315 static const cp_parser_token_tree_map map = {
a723baf1
MM
5316 { CPP_EQ_EQ, EQ_EXPR },
5317 { CPP_NOT_EQ, NE_EXPR },
5318 { CPP_EOF, ERROR_MARK }
5319 };
5320
5321 return cp_parser_binary_expression (parser,
5322 map,
5323 cp_parser_relational_expression);
5324}
5325
5326/* Parse an and-expression.
5327
5328 and-expression:
5329 equality-expression
5330 and-expression & equality-expression
5331
5332 Returns a representation of the expression. */
5333
5334static tree
5335cp_parser_and_expression (parser)
5336 cp_parser *parser;
5337{
39b1af70 5338 static const cp_parser_token_tree_map map = {
a723baf1
MM
5339 { CPP_AND, BIT_AND_EXPR },
5340 { CPP_EOF, ERROR_MARK }
5341 };
5342
5343 return cp_parser_binary_expression (parser,
5344 map,
5345 cp_parser_equality_expression);
5346}
5347
5348/* Parse an exclusive-or-expression.
5349
5350 exclusive-or-expression:
5351 and-expression
5352 exclusive-or-expression ^ and-expression
5353
5354 Returns a representation of the expression. */
5355
5356static tree
5357cp_parser_exclusive_or_expression (parser)
5358 cp_parser *parser;
5359{
39b1af70 5360 static const cp_parser_token_tree_map map = {
a723baf1
MM
5361 { CPP_XOR, BIT_XOR_EXPR },
5362 { CPP_EOF, ERROR_MARK }
5363 };
5364
5365 return cp_parser_binary_expression (parser,
5366 map,
5367 cp_parser_and_expression);
5368}
5369
5370
5371/* Parse an inclusive-or-expression.
5372
5373 inclusive-or-expression:
5374 exclusive-or-expression
5375 inclusive-or-expression | exclusive-or-expression
5376
5377 Returns a representation of the expression. */
5378
5379static tree
5380cp_parser_inclusive_or_expression (parser)
5381 cp_parser *parser;
5382{
39b1af70 5383 static const cp_parser_token_tree_map map = {
a723baf1
MM
5384 { CPP_OR, BIT_IOR_EXPR },
5385 { CPP_EOF, ERROR_MARK }
5386 };
5387
5388 return cp_parser_binary_expression (parser,
5389 map,
5390 cp_parser_exclusive_or_expression);
5391}
5392
5393/* Parse a logical-and-expression.
5394
5395 logical-and-expression:
5396 inclusive-or-expression
5397 logical-and-expression && inclusive-or-expression
5398
5399 Returns a representation of the expression. */
5400
5401static tree
5402cp_parser_logical_and_expression (parser)
5403 cp_parser *parser;
5404{
39b1af70 5405 static const cp_parser_token_tree_map map = {
a723baf1
MM
5406 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5407 { CPP_EOF, ERROR_MARK }
5408 };
5409
5410 return cp_parser_binary_expression (parser,
5411 map,
5412 cp_parser_inclusive_or_expression);
5413}
5414
5415/* Parse a logical-or-expression.
5416
5417 logical-or-expression:
5418 logical-and-expresion
5419 logical-or-expression || logical-and-expression
5420
5421 Returns a representation of the expression. */
5422
5423static tree
5424cp_parser_logical_or_expression (parser)
5425 cp_parser *parser;
5426{
39b1af70 5427 static const cp_parser_token_tree_map map = {
a723baf1
MM
5428 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5429 { CPP_EOF, ERROR_MARK }
5430 };
5431
5432 return cp_parser_binary_expression (parser,
5433 map,
5434 cp_parser_logical_and_expression);
5435}
5436
5437/* Parse a conditional-expression.
5438
5439 conditional-expression:
5440 logical-or-expression
5441 logical-or-expression ? expression : assignment-expression
5442
5443 GNU Extensions:
5444
5445 conditional-expression:
5446 logical-or-expression ? : assignment-expression
5447
5448 Returns a representation of the expression. */
5449
5450static tree
5451cp_parser_conditional_expression (parser)
5452 cp_parser *parser;
5453{
5454 tree logical_or_expr;
5455
5456 /* Parse the logical-or-expression. */
5457 logical_or_expr = cp_parser_logical_or_expression (parser);
5458 /* If the next token is a `?', then we have a real conditional
5459 expression. */
5460 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5461 return cp_parser_question_colon_clause (parser, logical_or_expr);
5462 /* Otherwise, the value is simply the logical-or-expression. */
5463 else
5464 return logical_or_expr;
5465}
5466
5467/* Parse the `? expression : assignment-expression' part of a
5468 conditional-expression. The LOGICAL_OR_EXPR is the
5469 logical-or-expression that started the conditional-expression.
5470 Returns a representation of the entire conditional-expression.
5471
5472 This routine exists only so that it can be shared between
5473 cp_parser_conditional_expression and
5474 cp_parser_assignment_expression.
5475
5476 ? expression : assignment-expression
5477
5478 GNU Extensions:
5479
5480 ? : assignment-expression */
5481
5482static tree
5483cp_parser_question_colon_clause (parser, logical_or_expr)
5484 cp_parser *parser;
5485 tree logical_or_expr;
5486{
5487 tree expr;
5488 tree assignment_expr;
5489
5490 /* Consume the `?' token. */
5491 cp_lexer_consume_token (parser->lexer);
5492 if (cp_parser_allow_gnu_extensions_p (parser)
5493 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5494 /* Implicit true clause. */
5495 expr = NULL_TREE;
5496 else
5497 /* Parse the expression. */
5498 expr = cp_parser_expression (parser);
5499
5500 /* The next token should be a `:'. */
5501 cp_parser_require (parser, CPP_COLON, "`:'");
5502 /* Parse the assignment-expression. */
5503 assignment_expr = cp_parser_assignment_expression (parser);
5504
5505 /* Build the conditional-expression. */
5506 return build_x_conditional_expr (logical_or_expr,
5507 expr,
5508 assignment_expr);
5509}
5510
5511/* Parse an assignment-expression.
5512
5513 assignment-expression:
5514 conditional-expression
5515 logical-or-expression assignment-operator assignment_expression
5516 throw-expression
5517
5518 Returns a representation for the expression. */
5519
5520static tree
5521cp_parser_assignment_expression (parser)
5522 cp_parser *parser;
5523{
5524 tree expr;
5525
5526 /* If the next token is the `throw' keyword, then we're looking at
5527 a throw-expression. */
5528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5529 expr = cp_parser_throw_expression (parser);
5530 /* Otherwise, it must be that we are looking at a
5531 logical-or-expression. */
5532 else
5533 {
5534 /* Parse the logical-or-expression. */
5535 expr = cp_parser_logical_or_expression (parser);
5536 /* If the next token is a `?' then we're actually looking at a
5537 conditional-expression. */
5538 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5539 return cp_parser_question_colon_clause (parser, expr);
5540 else
5541 {
5542 enum tree_code assignment_operator;
5543
5544 /* If it's an assignment-operator, we're using the second
5545 production. */
5546 assignment_operator
5547 = cp_parser_assignment_operator_opt (parser);
5548 if (assignment_operator != ERROR_MARK)
5549 {
5550 tree rhs;
5551
5552 /* Parse the right-hand side of the assignment. */
5553 rhs = cp_parser_assignment_expression (parser);
5554 /* Build the asignment expression. */
5555 expr = build_x_modify_expr (expr,
5556 assignment_operator,
5557 rhs);
5558 }
5559 }
5560 }
5561
5562 return expr;
5563}
5564
5565/* Parse an (optional) assignment-operator.
5566
5567 assignment-operator: one of
5568 = *= /= %= += -= >>= <<= &= ^= |=
5569
5570 GNU Extension:
5571
5572 assignment-operator: one of
5573 <?= >?=
5574
5575 If the next token is an assignment operator, the corresponding tree
5576 code is returned, and the token is consumed. For example, for
5577 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5578 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5579 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5580 operator, ERROR_MARK is returned. */
5581
5582static enum tree_code
5583cp_parser_assignment_operator_opt (parser)
5584 cp_parser *parser;
5585{
5586 enum tree_code op;
5587 cp_token *token;
5588
5589 /* Peek at the next toen. */
5590 token = cp_lexer_peek_token (parser->lexer);
5591
5592 switch (token->type)
5593 {
5594 case CPP_EQ:
5595 op = NOP_EXPR;
5596 break;
5597
5598 case CPP_MULT_EQ:
5599 op = MULT_EXPR;
5600 break;
5601
5602 case CPP_DIV_EQ:
5603 op = TRUNC_DIV_EXPR;
5604 break;
5605
5606 case CPP_MOD_EQ:
5607 op = TRUNC_MOD_EXPR;
5608 break;
5609
5610 case CPP_PLUS_EQ:
5611 op = PLUS_EXPR;
5612 break;
5613
5614 case CPP_MINUS_EQ:
5615 op = MINUS_EXPR;
5616 break;
5617
5618 case CPP_RSHIFT_EQ:
5619 op = RSHIFT_EXPR;
5620 break;
5621
5622 case CPP_LSHIFT_EQ:
5623 op = LSHIFT_EXPR;
5624 break;
5625
5626 case CPP_AND_EQ:
5627 op = BIT_AND_EXPR;
5628 break;
5629
5630 case CPP_XOR_EQ:
5631 op = BIT_XOR_EXPR;
5632 break;
5633
5634 case CPP_OR_EQ:
5635 op = BIT_IOR_EXPR;
5636 break;
5637
5638 case CPP_MIN_EQ:
5639 op = MIN_EXPR;
5640 break;
5641
5642 case CPP_MAX_EQ:
5643 op = MAX_EXPR;
5644 break;
5645
5646 default:
5647 /* Nothing else is an assignment operator. */
5648 op = ERROR_MARK;
5649 }
5650
5651 /* If it was an assignment operator, consume it. */
5652 if (op != ERROR_MARK)
5653 cp_lexer_consume_token (parser->lexer);
5654
5655 return op;
5656}
5657
5658/* Parse an expression.
5659
5660 expression:
5661 assignment-expression
5662 expression , assignment-expression
5663
5664 Returns a representation of the expression. */
5665
5666static tree
5667cp_parser_expression (parser)
5668 cp_parser *parser;
5669{
5670 tree expression = NULL_TREE;
5671 bool saw_comma_p = false;
5672
5673 while (true)
5674 {
5675 tree assignment_expression;
5676
5677 /* Parse the next assignment-expression. */
5678 assignment_expression
5679 = cp_parser_assignment_expression (parser);
5680 /* If this is the first assignment-expression, we can just
5681 save it away. */
5682 if (!expression)
5683 expression = assignment_expression;
5684 /* Otherwise, chain the expressions together. It is unclear why
5685 we do not simply build COMPOUND_EXPRs as we go. */
5686 else
5687 expression = tree_cons (NULL_TREE,
5688 assignment_expression,
5689 expression);
5690 /* If the next token is not a comma, then we are done with the
5691 expression. */
5692 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5693 break;
5694 /* Consume the `,'. */
5695 cp_lexer_consume_token (parser->lexer);
5696 /* The first time we see a `,', we must take special action
5697 because the representation used for a single expression is
5698 different from that used for a list containing the single
5699 expression. */
5700 if (!saw_comma_p)
5701 {
5702 /* Remember that this expression has a `,' in it. */
5703 saw_comma_p = true;
5704 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5705 additional expressions to it. */
5706 expression = build_tree_list (NULL_TREE, expression);
5707 }
5708 }
5709
5710 /* Build a COMPOUND_EXPR to represent the entire expression, if
5711 necessary. We built up the list in reverse order, so we must
5712 straighten it out here. */
5713 if (saw_comma_p)
5714 expression = build_x_compound_expr (nreverse (expression));
5715
5716 return expression;
5717}
5718
5719/* Parse a constant-expression.
5720
5721 constant-expression:
5722 conditional-expression */
5723
5724static tree
5725cp_parser_constant_expression (parser)
5726 cp_parser *parser;
5727{
5728 bool saved_constant_expression_p;
5729 tree expression;
5730
5731 /* It might seem that we could simply parse the
5732 conditional-expression, and then check to see if it were
5733 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5734 one that the compiler can figure out is constant, possibly after
5735 doing some simplifications or optimizations. The standard has a
5736 precise definition of constant-expression, and we must honor
5737 that, even though it is somewhat more restrictive.
5738
5739 For example:
5740
5741 int i[(2, 3)];
5742
5743 is not a legal declaration, because `(2, 3)' is not a
5744 constant-expression. The `,' operator is forbidden in a
5745 constant-expression. However, GCC's constant-folding machinery
5746 will fold this operation to an INTEGER_CST for `3'. */
5747
5748 /* Save the old setting of CONSTANT_EXPRESSION_P. */
5749 saved_constant_expression_p = parser->constant_expression_p;
5750 /* We are now parsing a constant-expression. */
5751 parser->constant_expression_p = true;
5752 /* Parse the conditional-expression. */
5753 expression = cp_parser_conditional_expression (parser);
5754 /* Restore the old setting of CONSTANT_EXPRESSION_P. */
5755 parser->constant_expression_p = saved_constant_expression_p;
5756
5757 return expression;
5758}
5759
5760/* Statements [gram.stmt.stmt] */
5761
5762/* Parse a statement.
5763
5764 statement:
5765 labeled-statement
5766 expression-statement
5767 compound-statement
5768 selection-statement
5769 iteration-statement
5770 jump-statement
5771 declaration-statement
5772 try-block */
5773
5774static void
5775cp_parser_statement (parser)
5776 cp_parser *parser;
5777{
5778 tree statement;
5779 cp_token *token;
5780 int statement_line_number;
5781
5782 /* There is no statement yet. */
5783 statement = NULL_TREE;
5784 /* Peek at the next token. */
5785 token = cp_lexer_peek_token (parser->lexer);
5786 /* Remember the line number of the first token in the statement. */
5787 statement_line_number = token->line_number;
5788 /* If this is a keyword, then that will often determine what kind of
5789 statement we have. */
5790 if (token->type == CPP_KEYWORD)
5791 {
5792 enum rid keyword = token->keyword;
5793
5794 switch (keyword)
5795 {
5796 case RID_CASE:
5797 case RID_DEFAULT:
5798 statement = cp_parser_labeled_statement (parser);
5799 break;
5800
5801 case RID_IF:
5802 case RID_SWITCH:
5803 statement = cp_parser_selection_statement (parser);
5804 break;
5805
5806 case RID_WHILE:
5807 case RID_DO:
5808 case RID_FOR:
5809 statement = cp_parser_iteration_statement (parser);
5810 break;
5811
5812 case RID_BREAK:
5813 case RID_CONTINUE:
5814 case RID_RETURN:
5815 case RID_GOTO:
5816 statement = cp_parser_jump_statement (parser);
5817 break;
5818
5819 case RID_TRY:
5820 statement = cp_parser_try_block (parser);
5821 break;
5822
5823 default:
5824 /* It might be a keyword like `int' that can start a
5825 declaration-statement. */
5826 break;
5827 }
5828 }
5829 else if (token->type == CPP_NAME)
5830 {
5831 /* If the next token is a `:', then we are looking at a
5832 labeled-statement. */
5833 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5834 if (token->type == CPP_COLON)
5835 statement = cp_parser_labeled_statement (parser);
5836 }
5837 /* Anything that starts with a `{' must be a compound-statement. */
5838 else if (token->type == CPP_OPEN_BRACE)
5839 statement = cp_parser_compound_statement (parser);
5840
5841 /* Everything else must be a declaration-statement or an
5842 expression-statement. Try for the declaration-statement
5843 first, unless we are looking at a `;', in which case we know that
5844 we have an expression-statement. */
5845 if (!statement)
5846 {
5847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5848 {
5849 cp_parser_parse_tentatively (parser);
5850 /* Try to parse the declaration-statement. */
5851 cp_parser_declaration_statement (parser);
5852 /* If that worked, we're done. */
5853 if (cp_parser_parse_definitely (parser))
5854 return;
5855 }
5856 /* Look for an expression-statement instead. */
5857 statement = cp_parser_expression_statement (parser);
5858 }
5859
5860 /* Set the line number for the statement. */
5861 if (statement && statement_code_p (TREE_CODE (statement)))
5862 STMT_LINENO (statement) = statement_line_number;
5863}
5864
5865/* Parse a labeled-statement.
5866
5867 labeled-statement:
5868 identifier : statement
5869 case constant-expression : statement
5870 default : statement
5871
5872 Returns the new CASE_LABEL, for a `case' or `default' label. For
5873 an ordinary label, returns a LABEL_STMT. */
5874
5875static tree
5876cp_parser_labeled_statement (parser)
5877 cp_parser *parser;
5878{
5879 cp_token *token;
5880 tree statement = NULL_TREE;
5881
5882 /* The next token should be an identifier. */
5883 token = cp_lexer_peek_token (parser->lexer);
5884 if (token->type != CPP_NAME
5885 && token->type != CPP_KEYWORD)
5886 {
5887 cp_parser_error (parser, "expected labeled-statement");
5888 return error_mark_node;
5889 }
5890
5891 switch (token->keyword)
5892 {
5893 case RID_CASE:
5894 {
5895 tree expr;
5896
5897 /* Consume the `case' token. */
5898 cp_lexer_consume_token (parser->lexer);
5899 /* Parse the constant-expression. */
5900 expr = cp_parser_constant_expression (parser);
5901 /* Create the label. */
5902 statement = finish_case_label (expr, NULL_TREE);
5903 }
5904 break;
5905
5906 case RID_DEFAULT:
5907 /* Consume the `default' token. */
5908 cp_lexer_consume_token (parser->lexer);
5909 /* Create the label. */
5910 statement = finish_case_label (NULL_TREE, NULL_TREE);
5911 break;
5912
5913 default:
5914 /* Anything else must be an ordinary label. */
5915 statement = finish_label_stmt (cp_parser_identifier (parser));
5916 break;
5917 }
5918
5919 /* Require the `:' token. */
5920 cp_parser_require (parser, CPP_COLON, "`:'");
5921 /* Parse the labeled statement. */
5922 cp_parser_statement (parser);
5923
5924 /* Return the label, in the case of a `case' or `default' label. */
5925 return statement;
5926}
5927
5928/* Parse an expression-statement.
5929
5930 expression-statement:
5931 expression [opt] ;
5932
5933 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5934 statement consists of nothing more than an `;'. */
5935
5936static tree
5937cp_parser_expression_statement (parser)
5938 cp_parser *parser;
5939{
5940 tree statement;
5941
5942 /* If the next token is not a `;', then there is an expression to parse. */
5943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5944 statement = finish_expr_stmt (cp_parser_expression (parser));
5945 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5946 else
5947 {
5948 finish_stmt ();
5949 statement = NULL_TREE;
5950 }
5951 /* Consume the final `;'. */
5952 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
5953 {
5954 /* If there is additional (erroneous) input, skip to the end of
5955 the statement. */
5956 cp_parser_skip_to_end_of_statement (parser);
5957 /* If the next token is now a `;', consume it. */
5958 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5959 cp_lexer_consume_token (parser->lexer);
5960 }
5961
5962 return statement;
5963}
5964
5965/* Parse a compound-statement.
5966
5967 compound-statement:
5968 { statement-seq [opt] }
5969
5970 Returns a COMPOUND_STMT representing the statement. */
5971
5972static tree
5973cp_parser_compound_statement (cp_parser *parser)
5974{
5975 tree compound_stmt;
5976
5977 /* Consume the `{'. */
5978 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5979 return error_mark_node;
5980 /* Begin the compound-statement. */
5981 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5982 /* Parse an (optional) statement-seq. */
5983 cp_parser_statement_seq_opt (parser);
5984 /* Finish the compound-statement. */
5985 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5986 /* Consume the `}'. */
5987 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5988
5989 return compound_stmt;
5990}
5991
5992/* Parse an (optional) statement-seq.
5993
5994 statement-seq:
5995 statement
5996 statement-seq [opt] statement */
5997
5998static void
5999cp_parser_statement_seq_opt (parser)
6000 cp_parser *parser;
6001{
6002 /* Scan statements until there aren't any more. */
6003 while (true)
6004 {
6005 /* If we're looking at a `}', then we've run out of statements. */
6006 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6007 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6008 break;
6009
6010 /* Parse the statement. */
6011 cp_parser_statement (parser);
6012 }
6013}
6014
6015/* Parse a selection-statement.
6016
6017 selection-statement:
6018 if ( condition ) statement
6019 if ( condition ) statement else statement
6020 switch ( condition ) statement
6021
6022 Returns the new IF_STMT or SWITCH_STMT. */
6023
6024static tree
6025cp_parser_selection_statement (parser)
6026 cp_parser *parser;
6027{
6028 cp_token *token;
6029 enum rid keyword;
6030
6031 /* Peek at the next token. */
6032 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6033
6034 /* See what kind of keyword it is. */
6035 keyword = token->keyword;
6036 switch (keyword)
6037 {
6038 case RID_IF:
6039 case RID_SWITCH:
6040 {
6041 tree statement;
6042 tree condition;
6043
6044 /* Look for the `('. */
6045 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6046 {
6047 cp_parser_skip_to_end_of_statement (parser);
6048 return error_mark_node;
6049 }
6050
6051 /* Begin the selection-statement. */
6052 if (keyword == RID_IF)
6053 statement = begin_if_stmt ();
6054 else
6055 statement = begin_switch_stmt ();
6056
6057 /* Parse the condition. */
6058 condition = cp_parser_condition (parser);
6059 /* Look for the `)'. */
6060 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6061 cp_parser_skip_to_closing_parenthesis (parser);
6062
6063 if (keyword == RID_IF)
6064 {
6065 tree then_stmt;
6066
6067 /* Add the condition. */
6068 finish_if_stmt_cond (condition, statement);
6069
6070 /* Parse the then-clause. */
6071 then_stmt = cp_parser_implicitly_scoped_statement (parser);
6072 finish_then_clause (statement);
6073
6074 /* If the next token is `else', parse the else-clause. */
6075 if (cp_lexer_next_token_is_keyword (parser->lexer,
6076 RID_ELSE))
6077 {
6078 tree else_stmt;
6079
6080 /* Consume the `else' keyword. */
6081 cp_lexer_consume_token (parser->lexer);
6082 /* Parse the else-clause. */
6083 else_stmt
6084 = cp_parser_implicitly_scoped_statement (parser);
6085 finish_else_clause (statement);
6086 }
6087
6088 /* Now we're all done with the if-statement. */
6089 finish_if_stmt ();
6090 }
6091 else
6092 {
6093 tree body;
6094
6095 /* Add the condition. */
6096 finish_switch_cond (condition, statement);
6097
6098 /* Parse the body of the switch-statement. */
6099 body = cp_parser_implicitly_scoped_statement (parser);
6100
6101 /* Now we're all done with the switch-statement. */
6102 finish_switch_stmt (statement);
6103 }
6104
6105 return statement;
6106 }
6107 break;
6108
6109 default:
6110 cp_parser_error (parser, "expected selection-statement");
6111 return error_mark_node;
6112 }
6113}
6114
6115/* Parse a condition.
6116
6117 condition:
6118 expression
6119 type-specifier-seq declarator = assignment-expression
6120
6121 GNU Extension:
6122
6123 condition:
6124 type-specifier-seq declarator asm-specification [opt]
6125 attributes [opt] = assignment-expression
6126
6127 Returns the expression that should be tested. */
6128
6129static tree
6130cp_parser_condition (parser)
6131 cp_parser *parser;
6132{
6133 tree type_specifiers;
6134 const char *saved_message;
6135
6136 /* Try the declaration first. */
6137 cp_parser_parse_tentatively (parser);
6138 /* New types are not allowed in the type-specifier-seq for a
6139 condition. */
6140 saved_message = parser->type_definition_forbidden_message;
6141 parser->type_definition_forbidden_message
6142 = "types may not be defined in conditions";
6143 /* Parse the type-specifier-seq. */
6144 type_specifiers = cp_parser_type_specifier_seq (parser);
6145 /* Restore the saved message. */
6146 parser->type_definition_forbidden_message = saved_message;
6147 /* If all is well, we might be looking at a declaration. */
6148 if (!cp_parser_error_occurred (parser))
6149 {
6150 tree decl;
6151 tree asm_specification;
6152 tree attributes;
6153 tree declarator;
6154 tree initializer = NULL_TREE;
6155
6156 /* Parse the declarator. */
62b8a44e 6157 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
6158 /*ctor_dtor_or_conv_p=*/NULL);
6159 /* Parse the attributes. */
6160 attributes = cp_parser_attributes_opt (parser);
6161 /* Parse the asm-specification. */
6162 asm_specification = cp_parser_asm_specification_opt (parser);
6163 /* If the next token is not an `=', then we might still be
6164 looking at an expression. For example:
6165
6166 if (A(a).x)
6167
6168 looks like a decl-specifier-seq and a declarator -- but then
6169 there is no `=', so this is an expression. */
6170 cp_parser_require (parser, CPP_EQ, "`='");
6171 /* If we did see an `=', then we are looking at a declaration
6172 for sure. */
6173 if (cp_parser_parse_definitely (parser))
6174 {
6175 /* Create the declaration. */
6176 decl = start_decl (declarator, type_specifiers,
6177 /*initialized_p=*/true,
6178 attributes, /*prefix_attributes=*/NULL_TREE);
6179 /* Parse the assignment-expression. */
6180 initializer = cp_parser_assignment_expression (parser);
6181
6182 /* Process the initializer. */
6183 cp_finish_decl (decl,
6184 initializer,
6185 asm_specification,
6186 LOOKUP_ONLYCONVERTING);
6187
6188 return convert_from_reference (decl);
6189 }
6190 }
6191 /* If we didn't even get past the declarator successfully, we are
6192 definitely not looking at a declaration. */
6193 else
6194 cp_parser_abort_tentative_parse (parser);
6195
6196 /* Otherwise, we are looking at an expression. */
6197 return cp_parser_expression (parser);
6198}
6199
6200/* Parse an iteration-statement.
6201
6202 iteration-statement:
6203 while ( condition ) statement
6204 do statement while ( expression ) ;
6205 for ( for-init-statement condition [opt] ; expression [opt] )
6206 statement
6207
6208 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6209
6210static tree
6211cp_parser_iteration_statement (parser)
6212 cp_parser *parser;
6213{
6214 cp_token *token;
6215 enum rid keyword;
6216 tree statement;
6217
6218 /* Peek at the next token. */
6219 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6220 if (!token)
6221 return error_mark_node;
6222
6223 /* See what kind of keyword it is. */
6224 keyword = token->keyword;
6225 switch (keyword)
6226 {
6227 case RID_WHILE:
6228 {
6229 tree condition;
6230
6231 /* Begin the while-statement. */
6232 statement = begin_while_stmt ();
6233 /* Look for the `('. */
6234 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6235 /* Parse the condition. */
6236 condition = cp_parser_condition (parser);
6237 finish_while_stmt_cond (condition, statement);
6238 /* Look for the `)'. */
6239 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6240 /* Parse the dependent statement. */
6241 cp_parser_already_scoped_statement (parser);
6242 /* We're done with the while-statement. */
6243 finish_while_stmt (statement);
6244 }
6245 break;
6246
6247 case RID_DO:
6248 {
6249 tree expression;
6250
6251 /* Begin the do-statement. */
6252 statement = begin_do_stmt ();
6253 /* Parse the body of the do-statement. */
6254 cp_parser_implicitly_scoped_statement (parser);
6255 finish_do_body (statement);
6256 /* Look for the `while' keyword. */
6257 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6258 /* Look for the `('. */
6259 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6260 /* Parse the expression. */
6261 expression = cp_parser_expression (parser);
6262 /* We're done with the do-statement. */
6263 finish_do_stmt (expression, statement);
6264 /* Look for the `)'. */
6265 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6266 /* Look for the `;'. */
6267 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6268 }
6269 break;
6270
6271 case RID_FOR:
6272 {
6273 tree condition = NULL_TREE;
6274 tree expression = NULL_TREE;
6275
6276 /* Begin the for-statement. */
6277 statement = begin_for_stmt ();
6278 /* Look for the `('. */
6279 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6280 /* Parse the initialization. */
6281 cp_parser_for_init_statement (parser);
6282 finish_for_init_stmt (statement);
6283
6284 /* If there's a condition, process it. */
6285 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6286 condition = cp_parser_condition (parser);
6287 finish_for_cond (condition, statement);
6288 /* Look for the `;'. */
6289 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6290
6291 /* If there's an expression, process it. */
6292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6293 expression = cp_parser_expression (parser);
6294 finish_for_expr (expression, statement);
6295 /* Look for the `)'. */
6296 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6297
6298 /* Parse the body of the for-statement. */
6299 cp_parser_already_scoped_statement (parser);
6300
6301 /* We're done with the for-statement. */
6302 finish_for_stmt (statement);
6303 }
6304 break;
6305
6306 default:
6307 cp_parser_error (parser, "expected iteration-statement");
6308 statement = error_mark_node;
6309 break;
6310 }
6311
6312 return statement;
6313}
6314
6315/* Parse a for-init-statement.
6316
6317 for-init-statement:
6318 expression-statement
6319 simple-declaration */
6320
6321static void
6322cp_parser_for_init_statement (parser)
6323 cp_parser *parser;
6324{
6325 /* If the next token is a `;', then we have an empty
6326 expression-statement. Gramatically, this is also a
6327 simple-declaration, but an invalid one, because it does not
6328 declare anything. Therefore, if we did not handle this case
6329 specially, we would issue an error message about an invalid
6330 declaration. */
6331 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6332 {
6333 /* We're going to speculatively look for a declaration, falling back
6334 to an expression, if necessary. */
6335 cp_parser_parse_tentatively (parser);
6336 /* Parse the declaration. */
6337 cp_parser_simple_declaration (parser,
6338 /*function_definition_allowed_p=*/false);
6339 /* If the tentative parse failed, then we shall need to look for an
6340 expression-statement. */
6341 if (cp_parser_parse_definitely (parser))
6342 return;
6343 }
6344
6345 cp_parser_expression_statement (parser);
6346}
6347
6348/* Parse a jump-statement.
6349
6350 jump-statement:
6351 break ;
6352 continue ;
6353 return expression [opt] ;
6354 goto identifier ;
6355
6356 GNU extension:
6357
6358 jump-statement:
6359 goto * expression ;
6360
6361 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6362 GOTO_STMT. */
6363
6364static tree
6365cp_parser_jump_statement (parser)
6366 cp_parser *parser;
6367{
6368 tree statement = error_mark_node;
6369 cp_token *token;
6370 enum rid keyword;
6371
6372 /* Peek at the next token. */
6373 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6374 if (!token)
6375 return error_mark_node;
6376
6377 /* See what kind of keyword it is. */
6378 keyword = token->keyword;
6379 switch (keyword)
6380 {
6381 case RID_BREAK:
6382 statement = finish_break_stmt ();
6383 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6384 break;
6385
6386 case RID_CONTINUE:
6387 statement = finish_continue_stmt ();
6388 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6389 break;
6390
6391 case RID_RETURN:
6392 {
6393 tree expr;
6394
6395 /* If the next token is a `;', then there is no
6396 expression. */
6397 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6398 expr = cp_parser_expression (parser);
6399 else
6400 expr = NULL_TREE;
6401 /* Build the return-statement. */
6402 statement = finish_return_stmt (expr);
6403 /* Look for the final `;'. */
6404 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6405 }
6406 break;
6407
6408 case RID_GOTO:
6409 /* Create the goto-statement. */
6410 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6411 {
6412 /* Issue a warning about this use of a GNU extension. */
6413 if (pedantic)
6414 pedwarn ("ISO C++ forbids computed gotos");
6415 /* Consume the '*' token. */
6416 cp_lexer_consume_token (parser->lexer);
6417 /* Parse the dependent expression. */
6418 finish_goto_stmt (cp_parser_expression (parser));
6419 }
6420 else
6421 finish_goto_stmt (cp_parser_identifier (parser));
6422 /* Look for the final `;'. */
6423 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6424 break;
6425
6426 default:
6427 cp_parser_error (parser, "expected jump-statement");
6428 break;
6429 }
6430
6431 return statement;
6432}
6433
6434/* Parse a declaration-statement.
6435
6436 declaration-statement:
6437 block-declaration */
6438
6439static void
6440cp_parser_declaration_statement (parser)
6441 cp_parser *parser;
6442{
6443 /* Parse the block-declaration. */
6444 cp_parser_block_declaration (parser, /*statement_p=*/true);
6445
6446 /* Finish off the statement. */
6447 finish_stmt ();
6448}
6449
6450/* Some dependent statements (like `if (cond) statement'), are
6451 implicitly in their own scope. In other words, if the statement is
6452 a single statement (as opposed to a compound-statement), it is
6453 none-the-less treated as if it were enclosed in braces. Any
6454 declarations appearing in the dependent statement are out of scope
6455 after control passes that point. This function parses a statement,
6456 but ensures that is in its own scope, even if it is not a
6457 compound-statement.
6458
6459 Returns the new statement. */
6460
6461static tree
6462cp_parser_implicitly_scoped_statement (parser)
6463 cp_parser *parser;
6464{
6465 tree statement;
6466
6467 /* If the token is not a `{', then we must take special action. */
6468 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6469 {
6470 /* Create a compound-statement. */
6471 statement = begin_compound_stmt (/*has_no_scope=*/0);
6472 /* Parse the dependent-statement. */
6473 cp_parser_statement (parser);
6474 /* Finish the dummy compound-statement. */
6475 finish_compound_stmt (/*has_no_scope=*/0, statement);
6476 }
6477 /* Otherwise, we simply parse the statement directly. */
6478 else
6479 statement = cp_parser_compound_statement (parser);
6480
6481 /* Return the statement. */
6482 return statement;
6483}
6484
6485/* For some dependent statements (like `while (cond) statement'), we
6486 have already created a scope. Therefore, even if the dependent
6487 statement is a compound-statement, we do not want to create another
6488 scope. */
6489
6490static void
6491cp_parser_already_scoped_statement (parser)
6492 cp_parser *parser;
6493{
6494 /* If the token is not a `{', then we must take special action. */
6495 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6496 {
6497 tree statement;
6498
6499 /* Create a compound-statement. */
6500 statement = begin_compound_stmt (/*has_no_scope=*/1);
6501 /* Parse the dependent-statement. */
6502 cp_parser_statement (parser);
6503 /* Finish the dummy compound-statement. */
6504 finish_compound_stmt (/*has_no_scope=*/1, statement);
6505 }
6506 /* Otherwise, we simply parse the statement directly. */
6507 else
6508 cp_parser_statement (parser);
6509}
6510
6511/* Declarations [gram.dcl.dcl] */
6512
6513/* Parse an optional declaration-sequence.
6514
6515 declaration-seq:
6516 declaration
6517 declaration-seq declaration */
6518
6519static void
6520cp_parser_declaration_seq_opt (parser)
6521 cp_parser *parser;
6522{
6523 while (true)
6524 {
6525 cp_token *token;
6526
6527 token = cp_lexer_peek_token (parser->lexer);
6528
6529 if (token->type == CPP_CLOSE_BRACE
6530 || token->type == CPP_EOF)
6531 break;
6532
6533 if (token->type == CPP_SEMICOLON)
6534 {
6535 /* A declaration consisting of a single semicolon is
6536 invalid. Allow it unless we're being pedantic. */
6537 if (pedantic)
6538 pedwarn ("extra `;'");
6539 cp_lexer_consume_token (parser->lexer);
6540 continue;
6541 }
6542
c838d82f
MM
6543 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6544 parser to enter or exit implict `extern "C"' blocks. */
6545 while (pending_lang_change > 0)
6546 {
6547 push_lang_context (lang_name_c);
6548 --pending_lang_change;
6549 }
6550 while (pending_lang_change < 0)
6551 {
6552 pop_lang_context ();
6553 ++pending_lang_change;
6554 }
6555
6556 /* Parse the declaration itself. */
a723baf1
MM
6557 cp_parser_declaration (parser);
6558 }
6559}
6560
6561/* Parse a declaration.
6562
6563 declaration:
6564 block-declaration
6565 function-definition
6566 template-declaration
6567 explicit-instantiation
6568 explicit-specialization
6569 linkage-specification
1092805d
MM
6570 namespace-definition
6571
6572 GNU extension:
6573
6574 declaration:
6575 __extension__ declaration */
a723baf1
MM
6576
6577static void
6578cp_parser_declaration (parser)
6579 cp_parser *parser;
6580{
6581 cp_token token1;
6582 cp_token token2;
1092805d
MM
6583 int saved_pedantic;
6584
6585 /* Check for the `__extension__' keyword. */
6586 if (cp_parser_extension_opt (parser, &saved_pedantic))
6587 {
6588 /* Parse the qualified declaration. */
6589 cp_parser_declaration (parser);
6590 /* Restore the PEDANTIC flag. */
6591 pedantic = saved_pedantic;
6592
6593 return;
6594 }
a723baf1
MM
6595
6596 /* Try to figure out what kind of declaration is present. */
6597 token1 = *cp_lexer_peek_token (parser->lexer);
6598 if (token1.type != CPP_EOF)
6599 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6600
6601 /* If the next token is `extern' and the following token is a string
6602 literal, then we have a linkage specification. */
6603 if (token1.keyword == RID_EXTERN
6604 && cp_parser_is_string_literal (&token2))
6605 cp_parser_linkage_specification (parser);
6606 /* If the next token is `template', then we have either a template
6607 declaration, an explicit instantiation, or an explicit
6608 specialization. */
6609 else if (token1.keyword == RID_TEMPLATE)
6610 {
6611 /* `template <>' indicates a template specialization. */
6612 if (token2.type == CPP_LESS
6613 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6614 cp_parser_explicit_specialization (parser);
6615 /* `template <' indicates a template declaration. */
6616 else if (token2.type == CPP_LESS)
6617 cp_parser_template_declaration (parser, /*member_p=*/false);
6618 /* Anything else must be an explicit instantiation. */
6619 else
6620 cp_parser_explicit_instantiation (parser);
6621 }
6622 /* If the next token is `export', then we have a template
6623 declaration. */
6624 else if (token1.keyword == RID_EXPORT)
6625 cp_parser_template_declaration (parser, /*member_p=*/false);
6626 /* If the next token is `extern', 'static' or 'inline' and the one
6627 after that is `template', we have a GNU extended explicit
6628 instantiation directive. */
6629 else if (cp_parser_allow_gnu_extensions_p (parser)
6630 && (token1.keyword == RID_EXTERN
6631 || token1.keyword == RID_STATIC
6632 || token1.keyword == RID_INLINE)
6633 && token2.keyword == RID_TEMPLATE)
6634 cp_parser_explicit_instantiation (parser);
6635 /* If the next token is `namespace', check for a named or unnamed
6636 namespace definition. */
6637 else if (token1.keyword == RID_NAMESPACE
6638 && (/* A named namespace definition. */
6639 (token2.type == CPP_NAME
6640 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6641 == CPP_OPEN_BRACE))
6642 /* An unnamed namespace definition. */
6643 || token2.type == CPP_OPEN_BRACE))
6644 cp_parser_namespace_definition (parser);
6645 /* We must have either a block declaration or a function
6646 definition. */
6647 else
6648 /* Try to parse a block-declaration, or a function-definition. */
6649 cp_parser_block_declaration (parser, /*statement_p=*/false);
6650}
6651
6652/* Parse a block-declaration.
6653
6654 block-declaration:
6655 simple-declaration
6656 asm-definition
6657 namespace-alias-definition
6658 using-declaration
6659 using-directive
6660
6661 GNU Extension:
6662
6663 block-declaration:
6664 __extension__ block-declaration
6665 label-declaration
6666
6667 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6668 part of a declaration-statement. */
6669
6670static void
6671cp_parser_block_declaration (cp_parser *parser,
6672 bool statement_p)
6673{
6674 cp_token *token1;
6675 int saved_pedantic;
6676
6677 /* Check for the `__extension__' keyword. */
6678 if (cp_parser_extension_opt (parser, &saved_pedantic))
6679 {
6680 /* Parse the qualified declaration. */
6681 cp_parser_block_declaration (parser, statement_p);
6682 /* Restore the PEDANTIC flag. */
6683 pedantic = saved_pedantic;
6684
6685 return;
6686 }
6687
6688 /* Peek at the next token to figure out which kind of declaration is
6689 present. */
6690 token1 = cp_lexer_peek_token (parser->lexer);
6691
6692 /* If the next keyword is `asm', we have an asm-definition. */
6693 if (token1->keyword == RID_ASM)
6694 {
6695 if (statement_p)
6696 cp_parser_commit_to_tentative_parse (parser);
6697 cp_parser_asm_definition (parser);
6698 }
6699 /* If the next keyword is `namespace', we have a
6700 namespace-alias-definition. */
6701 else if (token1->keyword == RID_NAMESPACE)
6702 cp_parser_namespace_alias_definition (parser);
6703 /* If the next keyword is `using', we have either a
6704 using-declaration or a using-directive. */
6705 else if (token1->keyword == RID_USING)
6706 {
6707 cp_token *token2;
6708
6709 if (statement_p)
6710 cp_parser_commit_to_tentative_parse (parser);
6711 /* If the token after `using' is `namespace', then we have a
6712 using-directive. */
6713 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6714 if (token2->keyword == RID_NAMESPACE)
6715 cp_parser_using_directive (parser);
6716 /* Otherwise, it's a using-declaration. */
6717 else
6718 cp_parser_using_declaration (parser);
6719 }
6720 /* If the next keyword is `__label__' we have a label declaration. */
6721 else if (token1->keyword == RID_LABEL)
6722 {
6723 if (statement_p)
6724 cp_parser_commit_to_tentative_parse (parser);
6725 cp_parser_label_declaration (parser);
6726 }
6727 /* Anything else must be a simple-declaration. */
6728 else
6729 cp_parser_simple_declaration (parser, !statement_p);
6730}
6731
6732/* Parse a simple-declaration.
6733
6734 simple-declaration:
6735 decl-specifier-seq [opt] init-declarator-list [opt] ;
6736
6737 init-declarator-list:
6738 init-declarator
6739 init-declarator-list , init-declarator
6740
6741 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6742 function-definition as a simple-declaration. */
6743
6744static void
6745cp_parser_simple_declaration (parser, function_definition_allowed_p)
6746 cp_parser *parser;
6747 bool function_definition_allowed_p;
6748{
6749 tree decl_specifiers;
6750 tree attributes;
6751 tree access_checks;
6752 bool declares_class_or_enum;
6753 bool saw_declarator;
6754
6755 /* Defer access checks until we know what is being declared; the
6756 checks for names appearing in the decl-specifier-seq should be
6757 done as if we were in the scope of the thing being declared. */
6758 cp_parser_start_deferring_access_checks (parser);
6759 /* Parse the decl-specifier-seq. We have to keep track of whether
6760 or not the decl-specifier-seq declares a named class or
6761 enumeration type, since that is the only case in which the
6762 init-declarator-list is allowed to be empty.
6763
6764 [dcl.dcl]
6765
6766 In a simple-declaration, the optional init-declarator-list can be
6767 omitted only when declaring a class or enumeration, that is when
6768 the decl-specifier-seq contains either a class-specifier, an
6769 elaborated-type-specifier, or an enum-specifier. */
6770 decl_specifiers
6771 = cp_parser_decl_specifier_seq (parser,
6772 CP_PARSER_FLAGS_OPTIONAL,
6773 &attributes,
6774 &declares_class_or_enum);
6775 /* We no longer need to defer access checks. */
6776 access_checks = cp_parser_stop_deferring_access_checks (parser);
6777
24c0ef37
GS
6778 /* Prevent access checks from being reclaimed by GC. */
6779 parser->access_checks_lists = tree_cons (NULL_TREE, access_checks,
6780 parser->access_checks_lists);
6781
a723baf1
MM
6782 /* Keep going until we hit the `;' at the end of the simple
6783 declaration. */
6784 saw_declarator = false;
6785 while (cp_lexer_next_token_is_not (parser->lexer,
6786 CPP_SEMICOLON))
6787 {
6788 cp_token *token;
6789 bool function_definition_p;
6790
6791 saw_declarator = true;
6792 /* Parse the init-declarator. */
6793 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6794 access_checks,
6795 function_definition_allowed_p,
6796 /*member_p=*/false,
6797 &function_definition_p);
6798 /* Handle function definitions specially. */
6799 if (function_definition_p)
6800 {
6801 /* If the next token is a `,', then we are probably
6802 processing something like:
6803
6804 void f() {}, *p;
6805
6806 which is erroneous. */
6807 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6808 error ("mixing declarations and function-definitions is forbidden");
6809 /* Otherwise, we're done with the list of declarators. */
6810 else
24c0ef37
GS
6811 {
6812 /* Discard access checks no longer in use. */
6813 parser->access_checks_lists
6814 = TREE_CHAIN (parser->access_checks_lists);
6815 return;
6816 }
a723baf1
MM
6817 }
6818 /* The next token should be either a `,' or a `;'. */
6819 token = cp_lexer_peek_token (parser->lexer);
6820 /* If it's a `,', there are more declarators to come. */
6821 if (token->type == CPP_COMMA)
6822 cp_lexer_consume_token (parser->lexer);
6823 /* If it's a `;', we are done. */
6824 else if (token->type == CPP_SEMICOLON)
6825 break;
6826 /* Anything else is an error. */
6827 else
6828 {
6829 cp_parser_error (parser, "expected `,' or `;'");
6830 /* Skip tokens until we reach the end of the statement. */
6831 cp_parser_skip_to_end_of_statement (parser);
24c0ef37
GS
6832 /* Discard access checks no longer in use. */
6833 parser->access_checks_lists
6834 = TREE_CHAIN (parser->access_checks_lists);
a723baf1
MM
6835 return;
6836 }
6837 /* After the first time around, a function-definition is not
6838 allowed -- even if it was OK at first. For example:
6839
6840 int i, f() {}
6841
6842 is not valid. */
6843 function_definition_allowed_p = false;
6844 }
6845
6846 /* Issue an error message if no declarators are present, and the
6847 decl-specifier-seq does not itself declare a class or
6848 enumeration. */
6849 if (!saw_declarator)
6850 {
6851 if (cp_parser_declares_only_class_p (parser))
6852 shadow_tag (decl_specifiers);
6853 /* Perform any deferred access checks. */
6854 cp_parser_perform_deferred_access_checks (access_checks);
6855 }
6856
6857 /* Consume the `;'. */
6858 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6859
6860 /* Mark all the classes that appeared in the decl-specifier-seq as
6861 having received a `;'. */
6862 note_list_got_semicolon (decl_specifiers);
24c0ef37
GS
6863
6864 /* Discard access checks no longer in use. */
6865 parser->access_checks_lists = TREE_CHAIN (parser->access_checks_lists);
a723baf1
MM
6866}
6867
6868/* Parse a decl-specifier-seq.
6869
6870 decl-specifier-seq:
6871 decl-specifier-seq [opt] decl-specifier
6872
6873 decl-specifier:
6874 storage-class-specifier
6875 type-specifier
6876 function-specifier
6877 friend
6878 typedef
6879
6880 GNU Extension:
6881
6882 decl-specifier-seq:
6883 decl-specifier-seq [opt] attributes
6884
6885 Returns a TREE_LIST, giving the decl-specifiers in the order they
6886 appear in the source code. The TREE_VALUE of each node is the
6887 decl-specifier. For a keyword (such as `auto' or `friend'), the
6888 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6889 representation of a type-specifier, see cp_parser_type_specifier.
6890
6891 If there are attributes, they will be stored in *ATTRIBUTES,
6892 represented as described above cp_parser_attributes.
6893
6894 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6895 appears, and the entity that will be a friend is not going to be a
6896 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6897 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6898 friendship is granted might not be a class. */
6899
6900static tree
6901cp_parser_decl_specifier_seq (parser, flags, attributes,
6902 declares_class_or_enum)
6903 cp_parser *parser;
6904 cp_parser_flags flags;
6905 tree *attributes;
6906 bool *declares_class_or_enum;
6907{
6908 tree decl_specs = NULL_TREE;
6909 bool friend_p = false;
2050a1bb 6910 bool constructor_possible_p = true;
a723baf1
MM
6911
6912 /* Assume no class or enumeration type is declared. */
6913 *declares_class_or_enum = false;
6914
6915 /* Assume there are no attributes. */
6916 *attributes = NULL_TREE;
6917
6918 /* Keep reading specifiers until there are no more to read. */
6919 while (true)
6920 {
6921 tree decl_spec = NULL_TREE;
6922 bool constructor_p;
6923 cp_token *token;
6924
6925 /* Peek at the next token. */
6926 token = cp_lexer_peek_token (parser->lexer);
6927 /* Handle attributes. */
6928 if (token->keyword == RID_ATTRIBUTE)
6929 {
6930 /* Parse the attributes. */
6931 decl_spec = cp_parser_attributes_opt (parser);
6932 /* Add them to the list. */
6933 *attributes = chainon (*attributes, decl_spec);
6934 continue;
6935 }
6936 /* If the next token is an appropriate keyword, we can simply
6937 add it to the list. */
6938 switch (token->keyword)
6939 {
6940 case RID_FRIEND:
6941 /* decl-specifier:
6942 friend */
6943 friend_p = true;
6944 /* The representation of the specifier is simply the
6945 appropriate TREE_IDENTIFIER node. */
6946 decl_spec = token->value;
6947 /* Consume the token. */
6948 cp_lexer_consume_token (parser->lexer);
6949 break;
6950
6951 /* function-specifier:
6952 inline
6953 virtual
6954 explicit */
6955 case RID_INLINE:
6956 case RID_VIRTUAL:
6957 case RID_EXPLICIT:
6958 decl_spec = cp_parser_function_specifier_opt (parser);
6959 break;
6960
6961 /* decl-specifier:
6962 typedef */
6963 case RID_TYPEDEF:
6964 /* The representation of the specifier is simply the
6965 appropriate TREE_IDENTIFIER node. */
6966 decl_spec = token->value;
6967 /* Consume the token. */
6968 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6969 /* A constructor declarator cannot appear in a typedef. */
6970 constructor_possible_p = false;
a723baf1
MM
6971 break;
6972
6973 /* storage-class-specifier:
6974 auto
6975 register
6976 static
6977 extern
6978 mutable
6979
6980 GNU Extension:
6981 thread */
6982 case RID_AUTO:
6983 case RID_REGISTER:
6984 case RID_STATIC:
6985 case RID_EXTERN:
6986 case RID_MUTABLE:
6987 case RID_THREAD:
6988 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6989 break;
6990
6991 default:
6992 break;
6993 }
6994
6995 /* Constructors are a special case. The `S' in `S()' is not a
6996 decl-specifier; it is the beginning of the declarator. */
6997 constructor_p = (!decl_spec
2050a1bb 6998 && constructor_possible_p
a723baf1
MM
6999 && cp_parser_constructor_declarator_p (parser,
7000 friend_p));
7001
7002 /* If we don't have a DECL_SPEC yet, then we must be looking at
7003 a type-specifier. */
7004 if (!decl_spec && !constructor_p)
7005 {
7006 bool decl_spec_declares_class_or_enum;
7007 bool is_cv_qualifier;
7008
7009 decl_spec
7010 = cp_parser_type_specifier (parser, flags,
7011 friend_p,
7012 /*is_declaration=*/true,
7013 &decl_spec_declares_class_or_enum,
7014 &is_cv_qualifier);
7015
7016 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7017
7018 /* If this type-specifier referenced a user-defined type
7019 (a typedef, class-name, etc.), then we can't allow any
7020 more such type-specifiers henceforth.
7021
7022 [dcl.spec]
7023
7024 The longest sequence of decl-specifiers that could
7025 possibly be a type name is taken as the
7026 decl-specifier-seq of a declaration. The sequence shall
7027 be self-consistent as described below.
7028
7029 [dcl.type]
7030
7031 As a general rule, at most one type-specifier is allowed
7032 in the complete decl-specifier-seq of a declaration. The
7033 only exceptions are the following:
7034
7035 -- const or volatile can be combined with any other
7036 type-specifier.
7037
7038 -- signed or unsigned can be combined with char, long,
7039 short, or int.
7040
7041 -- ..
7042
7043 Example:
7044
7045 typedef char* Pc;
7046 void g (const int Pc);
7047
7048 Here, Pc is *not* part of the decl-specifier seq; it's
7049 the declarator. Therefore, once we see a type-specifier
7050 (other than a cv-qualifier), we forbid any additional
7051 user-defined types. We *do* still allow things like `int
7052 int' to be considered a decl-specifier-seq, and issue the
7053 error message later. */
7054 if (decl_spec && !is_cv_qualifier)
7055 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
7056 /* A constructor declarator cannot follow a type-specifier. */
7057 if (decl_spec)
7058 constructor_possible_p = false;
a723baf1
MM
7059 }
7060
7061 /* If we still do not have a DECL_SPEC, then there are no more
7062 decl-specifiers. */
7063 if (!decl_spec)
7064 {
7065 /* Issue an error message, unless the entire construct was
7066 optional. */
7067 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
7068 {
7069 cp_parser_error (parser, "expected decl specifier");
7070 return error_mark_node;
7071 }
7072
7073 break;
7074 }
7075
7076 /* Add the DECL_SPEC to the list of specifiers. */
7077 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
7078
7079 /* After we see one decl-specifier, further decl-specifiers are
7080 always optional. */
7081 flags |= CP_PARSER_FLAGS_OPTIONAL;
7082 }
7083
7084 /* We have built up the DECL_SPECS in reverse order. Return them in
7085 the correct order. */
7086 return nreverse (decl_specs);
7087}
7088
7089/* Parse an (optional) storage-class-specifier.
7090
7091 storage-class-specifier:
7092 auto
7093 register
7094 static
7095 extern
7096 mutable
7097
7098 GNU Extension:
7099
7100 storage-class-specifier:
7101 thread
7102
7103 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7104
7105static tree
7106cp_parser_storage_class_specifier_opt (parser)
7107 cp_parser *parser;
7108{
7109 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7110 {
7111 case RID_AUTO:
7112 case RID_REGISTER:
7113 case RID_STATIC:
7114 case RID_EXTERN:
7115 case RID_MUTABLE:
7116 case RID_THREAD:
7117 /* Consume the token. */
7118 return cp_lexer_consume_token (parser->lexer)->value;
7119
7120 default:
7121 return NULL_TREE;
7122 }
7123}
7124
7125/* Parse an (optional) function-specifier.
7126
7127 function-specifier:
7128 inline
7129 virtual
7130 explicit
7131
7132 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7133
7134static tree
7135cp_parser_function_specifier_opt (parser)
7136 cp_parser *parser;
7137{
7138 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7139 {
7140 case RID_INLINE:
7141 case RID_VIRTUAL:
7142 case RID_EXPLICIT:
7143 /* Consume the token. */
7144 return cp_lexer_consume_token (parser->lexer)->value;
7145
7146 default:
7147 return NULL_TREE;
7148 }
7149}
7150
7151/* Parse a linkage-specification.
7152
7153 linkage-specification:
7154 extern string-literal { declaration-seq [opt] }
7155 extern string-literal declaration */
7156
7157static void
7158cp_parser_linkage_specification (parser)
7159 cp_parser *parser;
7160{
7161 cp_token *token;
7162 tree linkage;
7163
7164 /* Look for the `extern' keyword. */
7165 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7166
7167 /* Peek at the next token. */
7168 token = cp_lexer_peek_token (parser->lexer);
7169 /* If it's not a string-literal, then there's a problem. */
7170 if (!cp_parser_is_string_literal (token))
7171 {
7172 cp_parser_error (parser, "expected language-name");
7173 return;
7174 }
7175 /* Consume the token. */
7176 cp_lexer_consume_token (parser->lexer);
7177
7178 /* Transform the literal into an identifier. If the literal is a
7179 wide-character string, or contains embedded NULs, then we can't
7180 handle it as the user wants. */
7181 if (token->type == CPP_WSTRING
7182 || (strlen (TREE_STRING_POINTER (token->value))
7183 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
7184 {
7185 cp_parser_error (parser, "invalid linkage-specification");
7186 /* Assume C++ linkage. */
7187 linkage = get_identifier ("c++");
7188 }
7189 /* If it's a simple string constant, things are easier. */
7190 else
7191 linkage = get_identifier (TREE_STRING_POINTER (token->value));
7192
7193 /* We're now using the new linkage. */
7194 push_lang_context (linkage);
7195
7196 /* If the next token is a `{', then we're using the first
7197 production. */
7198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7199 {
7200 /* Consume the `{' token. */
7201 cp_lexer_consume_token (parser->lexer);
7202 /* Parse the declarations. */
7203 cp_parser_declaration_seq_opt (parser);
7204 /* Look for the closing `}'. */
7205 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7206 }
7207 /* Otherwise, there's just one declaration. */
7208 else
7209 {
7210 bool saved_in_unbraced_linkage_specification_p;
7211
7212 saved_in_unbraced_linkage_specification_p
7213 = parser->in_unbraced_linkage_specification_p;
7214 parser->in_unbraced_linkage_specification_p = true;
7215 have_extern_spec = true;
7216 cp_parser_declaration (parser);
7217 have_extern_spec = false;
7218 parser->in_unbraced_linkage_specification_p
7219 = saved_in_unbraced_linkage_specification_p;
7220 }
7221
7222 /* We're done with the linkage-specification. */
7223 pop_lang_context ();
7224}
7225
7226/* Special member functions [gram.special] */
7227
7228/* Parse a conversion-function-id.
7229
7230 conversion-function-id:
7231 operator conversion-type-id
7232
7233 Returns an IDENTIFIER_NODE representing the operator. */
7234
7235static tree
7236cp_parser_conversion_function_id (parser)
7237 cp_parser *parser;
7238{
7239 tree type;
7240 tree saved_scope;
7241 tree saved_qualifying_scope;
7242 tree saved_object_scope;
7243
7244 /* Look for the `operator' token. */
7245 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7246 return error_mark_node;
7247 /* When we parse the conversion-type-id, the current scope will be
7248 reset. However, we need that information in able to look up the
7249 conversion function later, so we save it here. */
7250 saved_scope = parser->scope;
7251 saved_qualifying_scope = parser->qualifying_scope;
7252 saved_object_scope = parser->object_scope;
7253 /* We must enter the scope of the class so that the names of
7254 entities declared within the class are available in the
7255 conversion-type-id. For example, consider:
7256
7257 struct S {
7258 typedef int I;
7259 operator I();
7260 };
7261
7262 S::operator I() { ... }
7263
7264 In order to see that `I' is a type-name in the definition, we
7265 must be in the scope of `S'. */
7266 if (saved_scope)
7267 push_scope (saved_scope);
7268 /* Parse the conversion-type-id. */
7269 type = cp_parser_conversion_type_id (parser);
7270 /* Leave the scope of the class, if any. */
7271 if (saved_scope)
7272 pop_scope (saved_scope);
7273 /* Restore the saved scope. */
7274 parser->scope = saved_scope;
7275 parser->qualifying_scope = saved_qualifying_scope;
7276 parser->object_scope = saved_object_scope;
7277 /* If the TYPE is invalid, indicate failure. */
7278 if (type == error_mark_node)
7279 return error_mark_node;
7280 return mangle_conv_op_name_for_type (type);
7281}
7282
7283/* Parse a conversion-type-id:
7284
7285 conversion-type-id:
7286 type-specifier-seq conversion-declarator [opt]
7287
7288 Returns the TYPE specified. */
7289
7290static tree
7291cp_parser_conversion_type_id (parser)
7292 cp_parser *parser;
7293{
7294 tree attributes;
7295 tree type_specifiers;
7296 tree declarator;
7297
7298 /* Parse the attributes. */
7299 attributes = cp_parser_attributes_opt (parser);
7300 /* Parse the type-specifiers. */
7301 type_specifiers = cp_parser_type_specifier_seq (parser);
7302 /* If that didn't work, stop. */
7303 if (type_specifiers == error_mark_node)
7304 return error_mark_node;
7305 /* Parse the conversion-declarator. */
7306 declarator = cp_parser_conversion_declarator_opt (parser);
7307
7308 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7309 /*initialized=*/0, &attributes);
7310}
7311
7312/* Parse an (optional) conversion-declarator.
7313
7314 conversion-declarator:
7315 ptr-operator conversion-declarator [opt]
7316
7317 Returns a representation of the declarator. See
7318 cp_parser_declarator for details. */
7319
7320static tree
7321cp_parser_conversion_declarator_opt (parser)
7322 cp_parser *parser;
7323{
7324 enum tree_code code;
7325 tree class_type;
7326 tree cv_qualifier_seq;
7327
7328 /* We don't know if there's a ptr-operator next, or not. */
7329 cp_parser_parse_tentatively (parser);
7330 /* Try the ptr-operator. */
7331 code = cp_parser_ptr_operator (parser, &class_type,
7332 &cv_qualifier_seq);
7333 /* If it worked, look for more conversion-declarators. */
7334 if (cp_parser_parse_definitely (parser))
7335 {
7336 tree declarator;
7337
7338 /* Parse another optional declarator. */
7339 declarator = cp_parser_conversion_declarator_opt (parser);
7340
7341 /* Create the representation of the declarator. */
7342 if (code == INDIRECT_REF)
7343 declarator = make_pointer_declarator (cv_qualifier_seq,
7344 declarator);
7345 else
7346 declarator = make_reference_declarator (cv_qualifier_seq,
7347 declarator);
7348
7349 /* Handle the pointer-to-member case. */
7350 if (class_type)
7351 declarator = build_nt (SCOPE_REF, class_type, declarator);
7352
7353 return declarator;
7354 }
7355
7356 return NULL_TREE;
7357}
7358
7359/* Parse an (optional) ctor-initializer.
7360
7361 ctor-initializer:
7362 : mem-initializer-list
7363
7364 Returns TRUE iff the ctor-initializer was actually present. */
7365
7366static bool
7367cp_parser_ctor_initializer_opt (parser)
7368 cp_parser *parser;
7369{
7370 /* If the next token is not a `:', then there is no
7371 ctor-initializer. */
7372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7373 {
7374 /* Do default initialization of any bases and members. */
7375 if (DECL_CONSTRUCTOR_P (current_function_decl))
7376 finish_mem_initializers (NULL_TREE);
7377
7378 return false;
7379 }
7380
7381 /* Consume the `:' token. */
7382 cp_lexer_consume_token (parser->lexer);
7383 /* And the mem-initializer-list. */
7384 cp_parser_mem_initializer_list (parser);
7385
7386 return true;
7387}
7388
7389/* Parse a mem-initializer-list.
7390
7391 mem-initializer-list:
7392 mem-initializer
7393 mem-initializer , mem-initializer-list */
7394
7395static void
7396cp_parser_mem_initializer_list (parser)
7397 cp_parser *parser;
7398{
7399 tree mem_initializer_list = NULL_TREE;
7400
7401 /* Let the semantic analysis code know that we are starting the
7402 mem-initializer-list. */
7403 begin_mem_initializers ();
7404
7405 /* Loop through the list. */
7406 while (true)
7407 {
7408 tree mem_initializer;
7409
7410 /* Parse the mem-initializer. */
7411 mem_initializer = cp_parser_mem_initializer (parser);
7412 /* Add it to the list, unless it was erroneous. */
7413 if (mem_initializer)
7414 {
7415 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7416 mem_initializer_list = mem_initializer;
7417 }
7418 /* If the next token is not a `,', we're done. */
7419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7420 break;
7421 /* Consume the `,' token. */
7422 cp_lexer_consume_token (parser->lexer);
7423 }
7424
7425 /* Perform semantic analysis. */
7426 finish_mem_initializers (mem_initializer_list);
7427}
7428
7429/* Parse a mem-initializer.
7430
7431 mem-initializer:
7432 mem-initializer-id ( expression-list [opt] )
7433
7434 GNU extension:
7435
7436 mem-initializer:
7437 ( expresion-list [opt] )
7438
7439 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7440 class) or FIELD_DECL (for a non-static data member) to initialize;
7441 the TREE_VALUE is the expression-list. */
7442
7443static tree
7444cp_parser_mem_initializer (parser)
7445 cp_parser *parser;
7446{
7447 tree mem_initializer_id;
7448 tree expression_list;
7449
7450 /* Find out what is being initialized. */
7451 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7452 {
7453 pedwarn ("anachronistic old-style base class initializer");
7454 mem_initializer_id = NULL_TREE;
7455 }
7456 else
7457 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7458 /* Look for the opening `('. */
7459 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7460 /* Parse the expression-list. */
7461 if (cp_lexer_next_token_is_not (parser->lexer,
7462 CPP_CLOSE_PAREN))
7463 expression_list = cp_parser_expression_list (parser);
7464 else
7465 expression_list = void_type_node;
7466 /* Look for the closing `)'. */
7467 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7468
7469 return expand_member_init (mem_initializer_id,
7470 expression_list);
7471}
7472
7473/* Parse a mem-initializer-id.
7474
7475 mem-initializer-id:
7476 :: [opt] nested-name-specifier [opt] class-name
7477 identifier
7478
7479 Returns a TYPE indicating the class to be initializer for the first
7480 production. Returns an IDENTIFIER_NODE indicating the data member
7481 to be initialized for the second production. */
7482
7483static tree
7484cp_parser_mem_initializer_id (parser)
7485 cp_parser *parser;
7486{
7487 bool global_scope_p;
7488 bool nested_name_specifier_p;
7489 tree id;
7490
7491 /* Look for the optional `::' operator. */
7492 global_scope_p
7493 = (cp_parser_global_scope_opt (parser,
7494 /*current_scope_valid_p=*/false)
7495 != NULL_TREE);
7496 /* Look for the optional nested-name-specifier. The simplest way to
7497 implement:
7498
7499 [temp.res]
7500
7501 The keyword `typename' is not permitted in a base-specifier or
7502 mem-initializer; in these contexts a qualified name that
7503 depends on a template-parameter is implicitly assumed to be a
7504 type name.
7505
7506 is to assume that we have seen the `typename' keyword at this
7507 point. */
7508 nested_name_specifier_p
7509 = (cp_parser_nested_name_specifier_opt (parser,
7510 /*typename_keyword_p=*/true,
7511 /*check_dependency_p=*/true,
7512 /*type_p=*/true)
7513 != NULL_TREE);
7514 /* If there is a `::' operator or a nested-name-specifier, then we
7515 are definitely looking for a class-name. */
7516 if (global_scope_p || nested_name_specifier_p)
7517 return cp_parser_class_name (parser,
7518 /*typename_keyword_p=*/true,
7519 /*template_keyword_p=*/false,
7520 /*type_p=*/false,
7521 /*check_access_p=*/true,
7522 /*check_dependency_p=*/true,
7523 /*class_head_p=*/false);
7524 /* Otherwise, we could also be looking for an ordinary identifier. */
7525 cp_parser_parse_tentatively (parser);
7526 /* Try a class-name. */
7527 id = cp_parser_class_name (parser,
7528 /*typename_keyword_p=*/true,
7529 /*template_keyword_p=*/false,
7530 /*type_p=*/false,
7531 /*check_access_p=*/true,
7532 /*check_dependency_p=*/true,
7533 /*class_head_p=*/false);
7534 /* If we found one, we're done. */
7535 if (cp_parser_parse_definitely (parser))
7536 return id;
7537 /* Otherwise, look for an ordinary identifier. */
7538 return cp_parser_identifier (parser);
7539}
7540
7541/* Overloading [gram.over] */
7542
7543/* Parse an operator-function-id.
7544
7545 operator-function-id:
7546 operator operator
7547
7548 Returns an IDENTIFIER_NODE for the operator which is a
7549 human-readable spelling of the identifier, e.g., `operator +'. */
7550
7551static tree
7552cp_parser_operator_function_id (parser)
7553 cp_parser *parser;
7554{
7555 /* Look for the `operator' keyword. */
7556 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7557 return error_mark_node;
7558 /* And then the name of the operator itself. */
7559 return cp_parser_operator (parser);
7560}
7561
7562/* Parse an operator.
7563
7564 operator:
7565 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7566 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7567 || ++ -- , ->* -> () []
7568
7569 GNU Extensions:
7570
7571 operator:
7572 <? >? <?= >?=
7573
7574 Returns an IDENTIFIER_NODE for the operator which is a
7575 human-readable spelling of the identifier, e.g., `operator +'. */
7576
7577static tree
7578cp_parser_operator (parser)
7579 cp_parser *parser;
7580{
7581 tree id = NULL_TREE;
7582 cp_token *token;
7583
7584 /* Peek at the next token. */
7585 token = cp_lexer_peek_token (parser->lexer);
7586 /* Figure out which operator we have. */
7587 switch (token->type)
7588 {
7589 case CPP_KEYWORD:
7590 {
7591 enum tree_code op;
7592
7593 /* The keyword should be either `new' or `delete'. */
7594 if (token->keyword == RID_NEW)
7595 op = NEW_EXPR;
7596 else if (token->keyword == RID_DELETE)
7597 op = DELETE_EXPR;
7598 else
7599 break;
7600
7601 /* Consume the `new' or `delete' token. */
7602 cp_lexer_consume_token (parser->lexer);
7603
7604 /* Peek at the next token. */
7605 token = cp_lexer_peek_token (parser->lexer);
7606 /* If it's a `[' token then this is the array variant of the
7607 operator. */
7608 if (token->type == CPP_OPEN_SQUARE)
7609 {
7610 /* Consume the `[' token. */
7611 cp_lexer_consume_token (parser->lexer);
7612 /* Look for the `]' token. */
7613 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7614 id = ansi_opname (op == NEW_EXPR
7615 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7616 }
7617 /* Otherwise, we have the non-array variant. */
7618 else
7619 id = ansi_opname (op);
7620
7621 return id;
7622 }
7623
7624 case CPP_PLUS:
7625 id = ansi_opname (PLUS_EXPR);
7626 break;
7627
7628 case CPP_MINUS:
7629 id = ansi_opname (MINUS_EXPR);
7630 break;
7631
7632 case CPP_MULT:
7633 id = ansi_opname (MULT_EXPR);
7634 break;
7635
7636 case CPP_DIV:
7637 id = ansi_opname (TRUNC_DIV_EXPR);
7638 break;
7639
7640 case CPP_MOD:
7641 id = ansi_opname (TRUNC_MOD_EXPR);
7642 break;
7643
7644 case CPP_XOR:
7645 id = ansi_opname (BIT_XOR_EXPR);
7646 break;
7647
7648 case CPP_AND:
7649 id = ansi_opname (BIT_AND_EXPR);
7650 break;
7651
7652 case CPP_OR:
7653 id = ansi_opname (BIT_IOR_EXPR);
7654 break;
7655
7656 case CPP_COMPL:
7657 id = ansi_opname (BIT_NOT_EXPR);
7658 break;
7659
7660 case CPP_NOT:
7661 id = ansi_opname (TRUTH_NOT_EXPR);
7662 break;
7663
7664 case CPP_EQ:
7665 id = ansi_assopname (NOP_EXPR);
7666 break;
7667
7668 case CPP_LESS:
7669 id = ansi_opname (LT_EXPR);
7670 break;
7671
7672 case CPP_GREATER:
7673 id = ansi_opname (GT_EXPR);
7674 break;
7675
7676 case CPP_PLUS_EQ:
7677 id = ansi_assopname (PLUS_EXPR);
7678 break;
7679
7680 case CPP_MINUS_EQ:
7681 id = ansi_assopname (MINUS_EXPR);
7682 break;
7683
7684 case CPP_MULT_EQ:
7685 id = ansi_assopname (MULT_EXPR);
7686 break;
7687
7688 case CPP_DIV_EQ:
7689 id = ansi_assopname (TRUNC_DIV_EXPR);
7690 break;
7691
7692 case CPP_MOD_EQ:
7693 id = ansi_assopname (TRUNC_MOD_EXPR);
7694 break;
7695
7696 case CPP_XOR_EQ:
7697 id = ansi_assopname (BIT_XOR_EXPR);
7698 break;
7699
7700 case CPP_AND_EQ:
7701 id = ansi_assopname (BIT_AND_EXPR);
7702 break;
7703
7704 case CPP_OR_EQ:
7705 id = ansi_assopname (BIT_IOR_EXPR);
7706 break;
7707
7708 case CPP_LSHIFT:
7709 id = ansi_opname (LSHIFT_EXPR);
7710 break;
7711
7712 case CPP_RSHIFT:
7713 id = ansi_opname (RSHIFT_EXPR);
7714 break;
7715
7716 case CPP_LSHIFT_EQ:
7717 id = ansi_assopname (LSHIFT_EXPR);
7718 break;
7719
7720 case CPP_RSHIFT_EQ:
7721 id = ansi_assopname (RSHIFT_EXPR);
7722 break;
7723
7724 case CPP_EQ_EQ:
7725 id = ansi_opname (EQ_EXPR);
7726 break;
7727
7728 case CPP_NOT_EQ:
7729 id = ansi_opname (NE_EXPR);
7730 break;
7731
7732 case CPP_LESS_EQ:
7733 id = ansi_opname (LE_EXPR);
7734 break;
7735
7736 case CPP_GREATER_EQ:
7737 id = ansi_opname (GE_EXPR);
7738 break;
7739
7740 case CPP_AND_AND:
7741 id = ansi_opname (TRUTH_ANDIF_EXPR);
7742 break;
7743
7744 case CPP_OR_OR:
7745 id = ansi_opname (TRUTH_ORIF_EXPR);
7746 break;
7747
7748 case CPP_PLUS_PLUS:
7749 id = ansi_opname (POSTINCREMENT_EXPR);
7750 break;
7751
7752 case CPP_MINUS_MINUS:
7753 id = ansi_opname (PREDECREMENT_EXPR);
7754 break;
7755
7756 case CPP_COMMA:
7757 id = ansi_opname (COMPOUND_EXPR);
7758 break;
7759
7760 case CPP_DEREF_STAR:
7761 id = ansi_opname (MEMBER_REF);
7762 break;
7763
7764 case CPP_DEREF:
7765 id = ansi_opname (COMPONENT_REF);
7766 break;
7767
7768 case CPP_OPEN_PAREN:
7769 /* Consume the `('. */
7770 cp_lexer_consume_token (parser->lexer);
7771 /* Look for the matching `)'. */
7772 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7773 return ansi_opname (CALL_EXPR);
7774
7775 case CPP_OPEN_SQUARE:
7776 /* Consume the `['. */
7777 cp_lexer_consume_token (parser->lexer);
7778 /* Look for the matching `]'. */
7779 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7780 return ansi_opname (ARRAY_REF);
7781
7782 /* Extensions. */
7783 case CPP_MIN:
7784 id = ansi_opname (MIN_EXPR);
7785 break;
7786
7787 case CPP_MAX:
7788 id = ansi_opname (MAX_EXPR);
7789 break;
7790
7791 case CPP_MIN_EQ:
7792 id = ansi_assopname (MIN_EXPR);
7793 break;
7794
7795 case CPP_MAX_EQ:
7796 id = ansi_assopname (MAX_EXPR);
7797 break;
7798
7799 default:
7800 /* Anything else is an error. */
7801 break;
7802 }
7803
7804 /* If we have selected an identifier, we need to consume the
7805 operator token. */
7806 if (id)
7807 cp_lexer_consume_token (parser->lexer);
7808 /* Otherwise, no valid operator name was present. */
7809 else
7810 {
7811 cp_parser_error (parser, "expected operator");
7812 id = error_mark_node;
7813 }
7814
7815 return id;
7816}
7817
7818/* Parse a template-declaration.
7819
7820 template-declaration:
7821 export [opt] template < template-parameter-list > declaration
7822
7823 If MEMBER_P is TRUE, this template-declaration occurs within a
7824 class-specifier.
7825
7826 The grammar rule given by the standard isn't correct. What
7827 is really meant is:
7828
7829 template-declaration:
7830 export [opt] template-parameter-list-seq
7831 decl-specifier-seq [opt] init-declarator [opt] ;
7832 export [opt] template-parameter-list-seq
7833 function-definition
7834
7835 template-parameter-list-seq:
7836 template-parameter-list-seq [opt]
7837 template < template-parameter-list > */
7838
7839static void
7840cp_parser_template_declaration (parser, member_p)
7841 cp_parser *parser;
7842 bool member_p;
7843{
7844 /* Check for `export'. */
7845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7846 {
7847 /* Consume the `export' token. */
7848 cp_lexer_consume_token (parser->lexer);
7849 /* Warn that we do not support `export'. */
7850 warning ("keyword `export' not implemented, and will be ignored");
7851 }
7852
7853 cp_parser_template_declaration_after_export (parser, member_p);
7854}
7855
7856/* Parse a template-parameter-list.
7857
7858 template-parameter-list:
7859 template-parameter
7860 template-parameter-list , template-parameter
7861
7862 Returns a TREE_LIST. Each node represents a template parameter.
7863 The nodes are connected via their TREE_CHAINs. */
7864
7865static tree
7866cp_parser_template_parameter_list (parser)
7867 cp_parser *parser;
7868{
7869 tree parameter_list = NULL_TREE;
7870
7871 while (true)
7872 {
7873 tree parameter;
7874 cp_token *token;
7875
7876 /* Parse the template-parameter. */
7877 parameter = cp_parser_template_parameter (parser);
7878 /* Add it to the list. */
7879 parameter_list = process_template_parm (parameter_list,
7880 parameter);
7881
7882 /* Peek at the next token. */
7883 token = cp_lexer_peek_token (parser->lexer);
7884 /* If it's not a `,', we're done. */
7885 if (token->type != CPP_COMMA)
7886 break;
7887 /* Otherwise, consume the `,' token. */
7888 cp_lexer_consume_token (parser->lexer);
7889 }
7890
7891 return parameter_list;
7892}
7893
7894/* Parse a template-parameter.
7895
7896 template-parameter:
7897 type-parameter
7898 parameter-declaration
7899
7900 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7901 TREE_PURPOSE is the default value, if any. */
7902
7903static tree
7904cp_parser_template_parameter (parser)
7905 cp_parser *parser;
7906{
7907 cp_token *token;
7908
7909 /* Peek at the next token. */
7910 token = cp_lexer_peek_token (parser->lexer);
7911 /* If it is `class' or `template', we have a type-parameter. */
7912 if (token->keyword == RID_TEMPLATE)
7913 return cp_parser_type_parameter (parser);
7914 /* If it is `class' or `typename' we do not know yet whether it is a
7915 type parameter or a non-type parameter. Consider:
7916
7917 template <typename T, typename T::X X> ...
7918
7919 or:
7920
7921 template <class C, class D*> ...
7922
7923 Here, the first parameter is a type parameter, and the second is
7924 a non-type parameter. We can tell by looking at the token after
7925 the identifier -- if it is a `,', `=', or `>' then we have a type
7926 parameter. */
7927 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7928 {
7929 /* Peek at the token after `class' or `typename'. */
7930 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7931 /* If it's an identifier, skip it. */
7932 if (token->type == CPP_NAME)
7933 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7934 /* Now, see if the token looks like the end of a template
7935 parameter. */
7936 if (token->type == CPP_COMMA
7937 || token->type == CPP_EQ
7938 || token->type == CPP_GREATER)
7939 return cp_parser_type_parameter (parser);
7940 }
7941
7942 /* Otherwise, it is a non-type parameter.
7943
7944 [temp.param]
7945
7946 When parsing a default template-argument for a non-type
7947 template-parameter, the first non-nested `>' is taken as the end
7948 of the template parameter-list rather than a greater-than
7949 operator. */
7950 return
ec194454 7951 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
a723baf1
MM
7952}
7953
7954/* Parse a type-parameter.
7955
7956 type-parameter:
7957 class identifier [opt]
7958 class identifier [opt] = type-id
7959 typename identifier [opt]
7960 typename identifier [opt] = type-id
7961 template < template-parameter-list > class identifier [opt]
7962 template < template-parameter-list > class identifier [opt]
7963 = id-expression
7964
7965 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7966 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7967 the declaration of the parameter. */
7968
7969static tree
7970cp_parser_type_parameter (parser)
7971 cp_parser *parser;
7972{
7973 cp_token *token;
7974 tree parameter;
7975
7976 /* Look for a keyword to tell us what kind of parameter this is. */
7977 token = cp_parser_require (parser, CPP_KEYWORD,
7978 "expected `class', `typename', or `template'");
7979 if (!token)
7980 return error_mark_node;
7981
7982 switch (token->keyword)
7983 {
7984 case RID_CLASS:
7985 case RID_TYPENAME:
7986 {
7987 tree identifier;
7988 tree default_argument;
7989
7990 /* If the next token is an identifier, then it names the
7991 parameter. */
7992 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7993 identifier = cp_parser_identifier (parser);
7994 else
7995 identifier = NULL_TREE;
7996
7997 /* Create the parameter. */
7998 parameter = finish_template_type_parm (class_type_node, identifier);
7999
8000 /* If the next token is an `=', we have a default argument. */
8001 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8002 {
8003 /* Consume the `=' token. */
8004 cp_lexer_consume_token (parser->lexer);
8005 /* Parse the default-argumen. */
8006 default_argument = cp_parser_type_id (parser);
8007 }
8008 else
8009 default_argument = NULL_TREE;
8010
8011 /* Create the combined representation of the parameter and the
8012 default argument. */
8013 parameter = build_tree_list (default_argument,
8014 parameter);
8015 }
8016 break;
8017
8018 case RID_TEMPLATE:
8019 {
8020 tree parameter_list;
8021 tree identifier;
8022 tree default_argument;
8023
8024 /* Look for the `<'. */
8025 cp_parser_require (parser, CPP_LESS, "`<'");
8026 /* Parse the template-parameter-list. */
8027 begin_template_parm_list ();
8028 parameter_list
8029 = cp_parser_template_parameter_list (parser);
8030 parameter_list = end_template_parm_list (parameter_list);
8031 /* Look for the `>'. */
8032 cp_parser_require (parser, CPP_GREATER, "`>'");
8033 /* Look for the `class' keyword. */
8034 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8035 /* If the next token is an `=', then there is a
8036 default-argument. If the next token is a `>', we are at
8037 the end of the parameter-list. If the next token is a `,',
8038 then we are at the end of this parameter. */
8039 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8040 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8041 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8042 identifier = cp_parser_identifier (parser);
8043 else
8044 identifier = NULL_TREE;
8045 /* Create the template parameter. */
8046 parameter = finish_template_template_parm (class_type_node,
8047 identifier);
8048
8049 /* If the next token is an `=', then there is a
8050 default-argument. */
8051 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8052 {
8053 /* Consume the `='. */
8054 cp_lexer_consume_token (parser->lexer);
8055 /* Parse the id-expression. */
8056 default_argument
8057 = cp_parser_id_expression (parser,
8058 /*template_keyword_p=*/false,
8059 /*check_dependency_p=*/true,
8060 /*template_p=*/NULL);
8061 /* Look up the name. */
8062 default_argument
8063 = cp_parser_lookup_name_simple (parser, default_argument);
8064 /* See if the default argument is valid. */
8065 default_argument
8066 = check_template_template_default_arg (default_argument);
8067 }
8068 else
8069 default_argument = NULL_TREE;
8070
8071 /* Create the combined representation of the parameter and the
8072 default argument. */
8073 parameter = build_tree_list (default_argument,
8074 parameter);
8075 }
8076 break;
8077
8078 default:
8079 /* Anything else is an error. */
8080 cp_parser_error (parser,
8081 "expected `class', `typename', or `template'");
8082 parameter = error_mark_node;
8083 }
8084
8085 return parameter;
8086}
8087
8088/* Parse a template-id.
8089
8090 template-id:
8091 template-name < template-argument-list [opt] >
8092
8093 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8094 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
8095 returned. Otherwise, if the template-name names a function, or set
8096 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
8097 names a class, returns a TYPE_DECL for the specialization.
8098
8099 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8100 uninstantiated templates. */
8101
8102static tree
8103cp_parser_template_id (cp_parser *parser,
8104 bool template_keyword_p,
8105 bool check_dependency_p)
8106{
8107 tree template;
8108 tree arguments;
8109 tree saved_scope;
8110 tree saved_qualifying_scope;
8111 tree saved_object_scope;
8112 tree template_id;
8113 bool saved_greater_than_is_operator_p;
8114 ptrdiff_t start_of_id;
8115 tree access_check = NULL_TREE;
2050a1bb 8116 cp_token *next_token;
a723baf1
MM
8117
8118 /* If the next token corresponds to a template-id, there is no need
8119 to reparse it. */
2050a1bb
MM
8120 next_token = cp_lexer_peek_token (parser->lexer);
8121 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
8122 {
8123 tree value;
8124 tree check;
8125
8126 /* Get the stored value. */
8127 value = cp_lexer_consume_token (parser->lexer)->value;
8128 /* Perform any access checks that were deferred. */
8129 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8130 cp_parser_defer_access_check (parser,
8131 TREE_PURPOSE (check),
8132 TREE_VALUE (check));
8133 /* Return the stored value. */
8134 return TREE_VALUE (value);
8135 }
8136
2050a1bb
MM
8137 /* Avoid performing name lookup if there is no possibility of
8138 finding a template-id. */
8139 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8140 || (next_token->type == CPP_NAME
8141 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
8142 {
8143 cp_parser_error (parser, "expected template-id");
8144 return error_mark_node;
8145 }
8146
a723baf1
MM
8147 /* Remember where the template-id starts. */
8148 if (cp_parser_parsing_tentatively (parser)
8149 && !cp_parser_committed_to_tentative_parse (parser))
8150 {
2050a1bb 8151 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
8152 start_of_id = cp_lexer_token_difference (parser->lexer,
8153 parser->lexer->first_token,
8154 next_token);
8155 access_check = parser->context->deferred_access_checks;
8156 }
8157 else
8158 start_of_id = -1;
8159
8160 /* Parse the template-name. */
8161 template = cp_parser_template_name (parser, template_keyword_p,
8162 check_dependency_p);
8163 if (template == error_mark_node)
8164 return error_mark_node;
8165
8166 /* Look for the `<' that starts the template-argument-list. */
8167 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8168 return error_mark_node;
8169
8170 /* [temp.names]
8171
8172 When parsing a template-id, the first non-nested `>' is taken as
8173 the end of the template-argument-list rather than a greater-than
8174 operator. */
8175 saved_greater_than_is_operator_p
8176 = parser->greater_than_is_operator_p;
8177 parser->greater_than_is_operator_p = false;
8178 /* Parsing the argument list may modify SCOPE, so we save it
8179 here. */
8180 saved_scope = parser->scope;
8181 saved_qualifying_scope = parser->qualifying_scope;
8182 saved_object_scope = parser->object_scope;
8183 /* Parse the template-argument-list itself. */
8184 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
8185 arguments = NULL_TREE;
8186 else
8187 arguments = cp_parser_template_argument_list (parser);
8188 /* Look for the `>' that ends the template-argument-list. */
8189 cp_parser_require (parser, CPP_GREATER, "`>'");
8190 /* The `>' token might be a greater-than operator again now. */
8191 parser->greater_than_is_operator_p
8192 = saved_greater_than_is_operator_p;
8193 /* Restore the SAVED_SCOPE. */
8194 parser->scope = saved_scope;
8195 parser->qualifying_scope = saved_qualifying_scope;
8196 parser->object_scope = saved_object_scope;
8197
8198 /* Build a representation of the specialization. */
8199 if (TREE_CODE (template) == IDENTIFIER_NODE)
8200 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8201 else if (DECL_CLASS_TEMPLATE_P (template)
8202 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8203 template_id
8204 = finish_template_type (template, arguments,
8205 cp_lexer_next_token_is (parser->lexer,
8206 CPP_SCOPE));
8207 else
8208 {
8209 /* If it's not a class-template or a template-template, it should be
8210 a function-template. */
8211 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8212 || TREE_CODE (template) == OVERLOAD
8213 || BASELINK_P (template)),
8214 20010716);
8215
8216 template_id = lookup_template_function (template, arguments);
8217 }
8218
8219 /* If parsing tentatively, replace the sequence of tokens that makes
8220 up the template-id with a CPP_TEMPLATE_ID token. That way,
8221 should we re-parse the token stream, we will not have to repeat
8222 the effort required to do the parse, nor will we issue duplicate
8223 error messages about problems during instantiation of the
8224 template. */
8225 if (start_of_id >= 0)
8226 {
8227 cp_token *token;
8228 tree c;
8229
8230 /* Find the token that corresponds to the start of the
8231 template-id. */
8232 token = cp_lexer_advance_token (parser->lexer,
8233 parser->lexer->first_token,
8234 start_of_id);
8235
8236 /* Remember the access checks associated with this
8237 nested-name-specifier. */
8238 c = parser->context->deferred_access_checks;
8239 if (c == access_check)
8240 access_check = NULL_TREE;
8241 else
8242 {
8243 while (TREE_CHAIN (c) != access_check)
8244 c = TREE_CHAIN (c);
8245 access_check = parser->context->deferred_access_checks;
8246 parser->context->deferred_access_checks = TREE_CHAIN (c);
8247 TREE_CHAIN (c) = NULL_TREE;
8248 }
8249
8250 /* Reset the contents of the START_OF_ID token. */
8251 token->type = CPP_TEMPLATE_ID;
8252 token->value = build_tree_list (access_check, template_id);
8253 token->keyword = RID_MAX;
8254 /* Purge all subsequent tokens. */
8255 cp_lexer_purge_tokens_after (parser->lexer, token);
8256 }
8257
8258 return template_id;
8259}
8260
8261/* Parse a template-name.
8262
8263 template-name:
8264 identifier
8265
8266 The standard should actually say:
8267
8268 template-name:
8269 identifier
8270 operator-function-id
8271 conversion-function-id
8272
8273 A defect report has been filed about this issue.
8274
8275 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8276 `template' keyword, in a construction like:
8277
8278 T::template f<3>()
8279
8280 In that case `f' is taken to be a template-name, even though there
8281 is no way of knowing for sure.
8282
8283 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8284 name refers to a set of overloaded functions, at least one of which
8285 is a template, or an IDENTIFIER_NODE with the name of the template,
8286 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8287 names are looked up inside uninstantiated templates. */
8288
8289static tree
8290cp_parser_template_name (parser, template_keyword_p, check_dependency_p)
8291 cp_parser *parser;
8292 bool template_keyword_p;
8293 bool check_dependency_p;
8294{
8295 tree identifier;
8296 tree decl;
8297 tree fns;
8298
8299 /* If the next token is `operator', then we have either an
8300 operator-function-id or a conversion-function-id. */
8301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8302 {
8303 /* We don't know whether we're looking at an
8304 operator-function-id or a conversion-function-id. */
8305 cp_parser_parse_tentatively (parser);
8306 /* Try an operator-function-id. */
8307 identifier = cp_parser_operator_function_id (parser);
8308 /* If that didn't work, try a conversion-function-id. */
8309 if (!cp_parser_parse_definitely (parser))
8310 identifier = cp_parser_conversion_function_id (parser);
8311 }
8312 /* Look for the identifier. */
8313 else
8314 identifier = cp_parser_identifier (parser);
8315
8316 /* If we didn't find an identifier, we don't have a template-id. */
8317 if (identifier == error_mark_node)
8318 return error_mark_node;
8319
8320 /* If the name immediately followed the `template' keyword, then it
8321 is a template-name. However, if the next token is not `<', then
8322 we do not treat it as a template-name, since it is not being used
8323 as part of a template-id. This enables us to handle constructs
8324 like:
8325
8326 template <typename T> struct S { S(); };
8327 template <typename T> S<T>::S();
8328
8329 correctly. We would treat `S' as a template -- if it were `S<T>'
8330 -- but we do not if there is no `<'. */
8331 if (template_keyword_p && processing_template_decl
8332 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8333 return identifier;
8334
8335 /* Look up the name. */
8336 decl = cp_parser_lookup_name (parser, identifier,
8337 /*check_access=*/true,
8338 /*is_type=*/false,
eea9800f 8339 /*is_namespace=*/false,
a723baf1
MM
8340 check_dependency_p);
8341 decl = maybe_get_template_decl_from_type_decl (decl);
8342
8343 /* If DECL is a template, then the name was a template-name. */
8344 if (TREE_CODE (decl) == TEMPLATE_DECL)
8345 ;
8346 else
8347 {
8348 /* The standard does not explicitly indicate whether a name that
8349 names a set of overloaded declarations, some of which are
8350 templates, is a template-name. However, such a name should
8351 be a template-name; otherwise, there is no way to form a
8352 template-id for the overloaded templates. */
8353 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8354 if (TREE_CODE (fns) == OVERLOAD)
8355 {
8356 tree fn;
8357
8358 for (fn = fns; fn; fn = OVL_NEXT (fn))
8359 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8360 break;
8361 }
8362 else
8363 {
8364 /* Otherwise, the name does not name a template. */
8365 cp_parser_error (parser, "expected template-name");
8366 return error_mark_node;
8367 }
8368 }
8369
8370 /* If DECL is dependent, and refers to a function, then just return
8371 its name; we will look it up again during template instantiation. */
8372 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8373 {
8374 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8375 if (TYPE_P (scope) && cp_parser_dependent_type_p (scope))
8376 return identifier;
8377 }
8378
8379 return decl;
8380}
8381
8382/* Parse a template-argument-list.
8383
8384 template-argument-list:
8385 template-argument
8386 template-argument-list , template-argument
8387
8388 Returns a TREE_LIST representing the arguments, in the order they
8389 appeared. The TREE_VALUE of each node is a representation of the
8390 argument. */
8391
8392static tree
8393cp_parser_template_argument_list (parser)
8394 cp_parser *parser;
8395{
8396 tree arguments = NULL_TREE;
8397
8398 while (true)
8399 {
8400 tree argument;
8401
8402 /* Parse the template-argument. */
8403 argument = cp_parser_template_argument (parser);
8404 /* Add it to the list. */
8405 arguments = tree_cons (NULL_TREE, argument, arguments);
8406 /* If it is not a `,', then there are no more arguments. */
8407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8408 break;
8409 /* Otherwise, consume the ','. */
8410 cp_lexer_consume_token (parser->lexer);
8411 }
8412
8413 /* We built up the arguments in reverse order. */
8414 return nreverse (arguments);
8415}
8416
8417/* Parse a template-argument.
8418
8419 template-argument:
8420 assignment-expression
8421 type-id
8422 id-expression
8423
8424 The representation is that of an assignment-expression, type-id, or
8425 id-expression -- except that the qualified id-expression is
8426 evaluated, so that the value returned is either a DECL or an
8427 OVERLOAD. */
8428
8429static tree
8430cp_parser_template_argument (parser)
8431 cp_parser *parser;
8432{
8433 tree argument;
8434 bool template_p;
8435
8436 /* There's really no way to know what we're looking at, so we just
8437 try each alternative in order.
8438
8439 [temp.arg]
8440
8441 In a template-argument, an ambiguity between a type-id and an
8442 expression is resolved to a type-id, regardless of the form of
8443 the corresponding template-parameter.
8444
8445 Therefore, we try a type-id first. */
8446 cp_parser_parse_tentatively (parser);
a723baf1
MM
8447 argument = cp_parser_type_id (parser);
8448 /* If the next token isn't a `,' or a `>', then this argument wasn't
8449 really finished. */
8450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8451 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8452 cp_parser_error (parser, "expected template-argument");
8453 /* If that worked, we're done. */
8454 if (cp_parser_parse_definitely (parser))
8455 return argument;
8456 /* We're still not sure what the argument will be. */
8457 cp_parser_parse_tentatively (parser);
8458 /* Try a template. */
8459 argument = cp_parser_id_expression (parser,
8460 /*template_keyword_p=*/false,
8461 /*check_dependency_p=*/true,
8462 &template_p);
8463 /* If the next token isn't a `,' or a `>', then this argument wasn't
8464 really finished. */
8465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8466 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8467 cp_parser_error (parser, "expected template-argument");
8468 if (!cp_parser_error_occurred (parser))
8469 {
8470 /* Figure out what is being referred to. */
8471 argument = cp_parser_lookup_name_simple (parser, argument);
8472 if (template_p)
8473 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8474 TREE_OPERAND (argument, 1),
8475 tf_error | tf_parsing);
8476 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8477 cp_parser_error (parser, "expected template-name");
8478 }
8479 if (cp_parser_parse_definitely (parser))
8480 return argument;
8481 /* It must be an assignment-expression. */
8482 return cp_parser_assignment_expression (parser);
8483}
8484
8485/* Parse an explicit-instantiation.
8486
8487 explicit-instantiation:
8488 template declaration
8489
8490 Although the standard says `declaration', what it really means is:
8491
8492 explicit-instantiation:
8493 template decl-specifier-seq [opt] declarator [opt] ;
8494
8495 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8496 supposed to be allowed. A defect report has been filed about this
8497 issue.
8498
8499 GNU Extension:
8500
8501 explicit-instantiation:
8502 storage-class-specifier template
8503 decl-specifier-seq [opt] declarator [opt] ;
8504 function-specifier template
8505 decl-specifier-seq [opt] declarator [opt] ; */
8506
8507static void
8508cp_parser_explicit_instantiation (parser)
8509 cp_parser *parser;
8510{
8511 bool declares_class_or_enum;
8512 tree decl_specifiers;
8513 tree attributes;
8514 tree extension_specifier = NULL_TREE;
8515
8516 /* Look for an (optional) storage-class-specifier or
8517 function-specifier. */
8518 if (cp_parser_allow_gnu_extensions_p (parser))
8519 {
8520 extension_specifier
8521 = cp_parser_storage_class_specifier_opt (parser);
8522 if (!extension_specifier)
8523 extension_specifier = cp_parser_function_specifier_opt (parser);
8524 }
8525
8526 /* Look for the `template' keyword. */
8527 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8528 /* Let the front end know that we are processing an explicit
8529 instantiation. */
8530 begin_explicit_instantiation ();
8531 /* [temp.explicit] says that we are supposed to ignore access
8532 control while processing explicit instantiation directives. */
8533 scope_chain->check_access = 0;
8534 /* Parse a decl-specifier-seq. */
8535 decl_specifiers
8536 = cp_parser_decl_specifier_seq (parser,
8537 CP_PARSER_FLAGS_OPTIONAL,
8538 &attributes,
8539 &declares_class_or_enum);
8540 /* If there was exactly one decl-specifier, and it declared a class,
8541 and there's no declarator, then we have an explicit type
8542 instantiation. */
8543 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8544 {
8545 tree type;
8546
8547 type = check_tag_decl (decl_specifiers);
8548 if (type)
8549 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8550 }
8551 else
8552 {
8553 tree declarator;
8554 tree decl;
8555
8556 /* Parse the declarator. */
8557 declarator
62b8a44e 8558 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
8559 /*ctor_dtor_or_conv_p=*/NULL);
8560 decl = grokdeclarator (declarator, decl_specifiers,
8561 NORMAL, 0, NULL);
8562 /* Do the explicit instantiation. */
8563 do_decl_instantiation (decl, extension_specifier);
8564 }
8565 /* We're done with the instantiation. */
8566 end_explicit_instantiation ();
8567 /* Trun access control back on. */
8568 scope_chain->check_access = flag_access_control;
8569
8570 /* Look for the trailing `;'. */
8571 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8572}
8573
8574/* Parse an explicit-specialization.
8575
8576 explicit-specialization:
8577 template < > declaration
8578
8579 Although the standard says `declaration', what it really means is:
8580
8581 explicit-specialization:
8582 template <> decl-specifier [opt] init-declarator [opt] ;
8583 template <> function-definition
8584 template <> explicit-specialization
8585 template <> template-declaration */
8586
8587static void
8588cp_parser_explicit_specialization (parser)
8589 cp_parser *parser;
8590{
8591 /* Look for the `template' keyword. */
8592 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8593 /* Look for the `<'. */
8594 cp_parser_require (parser, CPP_LESS, "`<'");
8595 /* Look for the `>'. */
8596 cp_parser_require (parser, CPP_GREATER, "`>'");
8597 /* We have processed another parameter list. */
8598 ++parser->num_template_parameter_lists;
8599 /* Let the front end know that we are beginning a specialization. */
8600 begin_specialization ();
8601
8602 /* If the next keyword is `template', we need to figure out whether
8603 or not we're looking a template-declaration. */
8604 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8605 {
8606 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8607 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8608 cp_parser_template_declaration_after_export (parser,
8609 /*member_p=*/false);
8610 else
8611 cp_parser_explicit_specialization (parser);
8612 }
8613 else
8614 /* Parse the dependent declaration. */
8615 cp_parser_single_declaration (parser,
8616 /*member_p=*/false,
8617 /*friend_p=*/NULL);
8618
8619 /* We're done with the specialization. */
8620 end_specialization ();
8621 /* We're done with this parameter list. */
8622 --parser->num_template_parameter_lists;
8623}
8624
8625/* Parse a type-specifier.
8626
8627 type-specifier:
8628 simple-type-specifier
8629 class-specifier
8630 enum-specifier
8631 elaborated-type-specifier
8632 cv-qualifier
8633
8634 GNU Extension:
8635
8636 type-specifier:
8637 __complex__
8638
8639 Returns a representation of the type-specifier. If the
8640 type-specifier is a keyword (like `int' or `const', or
8641 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8642 For a class-specifier, enum-specifier, or elaborated-type-specifier
8643 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8644
8645 If IS_FRIEND is TRUE then this type-specifier is being declared a
8646 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8647 appearing in a decl-specifier-seq.
8648
8649 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8650 class-specifier, enum-specifier, or elaborated-type-specifier, then
8651 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8652 FALSE.
8653
8654 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8655 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8656 is set to FALSE. */
8657
8658static tree
8659cp_parser_type_specifier (parser,
8660 flags,
8661 is_friend,
8662 is_declaration,
8663 declares_class_or_enum,
8664 is_cv_qualifier)
8665 cp_parser *parser;
8666 cp_parser_flags flags;
8667 bool is_friend;
8668 bool is_declaration;
8669 bool *declares_class_or_enum;
8670 bool *is_cv_qualifier;
8671{
8672 tree type_spec = NULL_TREE;
8673 cp_token *token;
8674 enum rid keyword;
8675
8676 /* Assume this type-specifier does not declare a new type. */
8677 if (declares_class_or_enum)
8678 *declares_class_or_enum = false;
8679 /* And that it does not specify a cv-qualifier. */
8680 if (is_cv_qualifier)
8681 *is_cv_qualifier = false;
8682 /* Peek at the next token. */
8683 token = cp_lexer_peek_token (parser->lexer);
8684
8685 /* If we're looking at a keyword, we can use that to guide the
8686 production we choose. */
8687 keyword = token->keyword;
8688 switch (keyword)
8689 {
8690 /* Any of these indicate either a class-specifier, or an
8691 elaborated-type-specifier. */
8692 case RID_CLASS:
8693 case RID_STRUCT:
8694 case RID_UNION:
8695 case RID_ENUM:
8696 /* Parse tentatively so that we can back up if we don't find a
8697 class-specifier or enum-specifier. */
8698 cp_parser_parse_tentatively (parser);
8699 /* Look for the class-specifier or enum-specifier. */
8700 if (keyword == RID_ENUM)
8701 type_spec = cp_parser_enum_specifier (parser);
8702 else
8703 type_spec = cp_parser_class_specifier (parser);
8704
8705 /* If that worked, we're done. */
8706 if (cp_parser_parse_definitely (parser))
8707 {
8708 if (declares_class_or_enum)
8709 *declares_class_or_enum = true;
8710 return type_spec;
8711 }
8712
8713 /* Fall through. */
8714
8715 case RID_TYPENAME:
8716 /* Look for an elaborated-type-specifier. */
8717 type_spec = cp_parser_elaborated_type_specifier (parser,
8718 is_friend,
8719 is_declaration);
8720 /* We're declaring a class or enum -- unless we're using
8721 `typename'. */
8722 if (declares_class_or_enum && keyword != RID_TYPENAME)
8723 *declares_class_or_enum = true;
8724 return type_spec;
8725
8726 case RID_CONST:
8727 case RID_VOLATILE:
8728 case RID_RESTRICT:
8729 type_spec = cp_parser_cv_qualifier_opt (parser);
8730 /* Even though we call a routine that looks for an optional
8731 qualifier, we know that there should be one. */
8732 my_friendly_assert (type_spec != NULL, 20000328);
8733 /* This type-specifier was a cv-qualified. */
8734 if (is_cv_qualifier)
8735 *is_cv_qualifier = true;
8736
8737 return type_spec;
8738
8739 case RID_COMPLEX:
8740 /* The `__complex__' keyword is a GNU extension. */
8741 return cp_lexer_consume_token (parser->lexer)->value;
8742
8743 default:
8744 break;
8745 }
8746
8747 /* If we do not already have a type-specifier, assume we are looking
8748 at a simple-type-specifier. */
8749 type_spec = cp_parser_simple_type_specifier (parser, flags);
8750
8751 /* If we didn't find a type-specifier, and a type-specifier was not
8752 optional in this context, issue an error message. */
8753 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8754 {
8755 cp_parser_error (parser, "expected type specifier");
8756 return error_mark_node;
8757 }
8758
8759 return type_spec;
8760}
8761
8762/* Parse a simple-type-specifier.
8763
8764 simple-type-specifier:
8765 :: [opt] nested-name-specifier [opt] type-name
8766 :: [opt] nested-name-specifier template template-id
8767 char
8768 wchar_t
8769 bool
8770 short
8771 int
8772 long
8773 signed
8774 unsigned
8775 float
8776 double
8777 void
8778
8779 GNU Extension:
8780
8781 simple-type-specifier:
8782 __typeof__ unary-expression
8783 __typeof__ ( type-id )
8784
8785 For the various keywords, the value returned is simply the
8786 TREE_IDENTIFIER representing the keyword. For the first two
8787 productions, the value returned is the indicated TYPE_DECL. */
8788
8789static tree
8790cp_parser_simple_type_specifier (parser, flags)
8791 cp_parser *parser;
8792 cp_parser_flags flags;
8793{
8794 tree type = NULL_TREE;
8795 cp_token *token;
8796
8797 /* Peek at the next token. */
8798 token = cp_lexer_peek_token (parser->lexer);
8799
8800 /* If we're looking at a keyword, things are easy. */
8801 switch (token->keyword)
8802 {
8803 case RID_CHAR:
8804 case RID_WCHAR:
8805 case RID_BOOL:
8806 case RID_SHORT:
8807 case RID_INT:
8808 case RID_LONG:
8809 case RID_SIGNED:
8810 case RID_UNSIGNED:
8811 case RID_FLOAT:
8812 case RID_DOUBLE:
8813 case RID_VOID:
8814 /* Consume the token. */
8815 return cp_lexer_consume_token (parser->lexer)->value;
8816
8817 case RID_TYPEOF:
8818 {
8819 tree operand;
8820
8821 /* Consume the `typeof' token. */
8822 cp_lexer_consume_token (parser->lexer);
8823 /* Parse the operand to `typeof' */
8824 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8825 /* If it is not already a TYPE, take its type. */
8826 if (!TYPE_P (operand))
8827 operand = finish_typeof (operand);
8828
8829 return operand;
8830 }
8831
8832 default:
8833 break;
8834 }
8835
8836 /* The type-specifier must be a user-defined type. */
8837 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8838 {
8839 /* Don't gobble tokens or issue error messages if this is an
8840 optional type-specifier. */
8841 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8842 cp_parser_parse_tentatively (parser);
8843
8844 /* Look for the optional `::' operator. */
8845 cp_parser_global_scope_opt (parser,
8846 /*current_scope_valid_p=*/false);
8847 /* Look for the nested-name specifier. */
8848 cp_parser_nested_name_specifier_opt (parser,
8849 /*typename_keyword_p=*/false,
8850 /*check_dependency_p=*/true,
8851 /*type_p=*/false);
8852 /* If we have seen a nested-name-specifier, and the next token
8853 is `template', then we are using the template-id production. */
8854 if (parser->scope
8855 && cp_parser_optional_template_keyword (parser))
8856 {
8857 /* Look for the template-id. */
8858 type = cp_parser_template_id (parser,
8859 /*template_keyword_p=*/true,
8860 /*check_dependency_p=*/true);
8861 /* If the template-id did not name a type, we are out of
8862 luck. */
8863 if (TREE_CODE (type) != TYPE_DECL)
8864 {
8865 cp_parser_error (parser, "expected template-id for type");
8866 type = NULL_TREE;
8867 }
8868 }
8869 /* Otherwise, look for a type-name. */
8870 else
8871 {
8872 type = cp_parser_type_name (parser);
8873 if (type == error_mark_node)
8874 type = NULL_TREE;
8875 }
8876
8877 /* If it didn't work out, we don't have a TYPE. */
8878 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8879 && !cp_parser_parse_definitely (parser))
8880 type = NULL_TREE;
8881 }
8882
8883 /* If we didn't get a type-name, issue an error message. */
8884 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8885 {
8886 cp_parser_error (parser, "expected type-name");
8887 return error_mark_node;
8888 }
8889
8890 return type;
8891}
8892
8893/* Parse a type-name.
8894
8895 type-name:
8896 class-name
8897 enum-name
8898 typedef-name
8899
8900 enum-name:
8901 identifier
8902
8903 typedef-name:
8904 identifier
8905
8906 Returns a TYPE_DECL for the the type. */
8907
8908static tree
8909cp_parser_type_name (parser)
8910 cp_parser *parser;
8911{
8912 tree type_decl;
8913 tree identifier;
8914
8915 /* We can't know yet whether it is a class-name or not. */
8916 cp_parser_parse_tentatively (parser);
8917 /* Try a class-name. */
8918 type_decl = cp_parser_class_name (parser,
8919 /*typename_keyword_p=*/false,
8920 /*template_keyword_p=*/false,
8921 /*type_p=*/false,
8922 /*check_access_p=*/true,
8923 /*check_dependency_p=*/true,
8924 /*class_head_p=*/false);
8925 /* If it's not a class-name, keep looking. */
8926 if (!cp_parser_parse_definitely (parser))
8927 {
8928 /* It must be a typedef-name or an enum-name. */
8929 identifier = cp_parser_identifier (parser);
8930 if (identifier == error_mark_node)
8931 return error_mark_node;
8932
8933 /* Look up the type-name. */
8934 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8935 /* Issue an error if we did not find a type-name. */
8936 if (TREE_CODE (type_decl) != TYPE_DECL)
8937 {
8938 cp_parser_error (parser, "expected type-name");
8939 type_decl = error_mark_node;
8940 }
8941 /* Remember that the name was used in the definition of the
8942 current class so that we can check later to see if the
8943 meaning would have been different after the class was
8944 entirely defined. */
8945 else if (type_decl != error_mark_node
8946 && !parser->scope)
8947 maybe_note_name_used_in_class (identifier, type_decl);
8948 }
8949
8950 return type_decl;
8951}
8952
8953
8954/* Parse an elaborated-type-specifier. Note that the grammar given
8955 here incorporates the resolution to DR68.
8956
8957 elaborated-type-specifier:
8958 class-key :: [opt] nested-name-specifier [opt] identifier
8959 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8960 enum :: [opt] nested-name-specifier [opt] identifier
8961 typename :: [opt] nested-name-specifier identifier
8962 typename :: [opt] nested-name-specifier template [opt]
8963 template-id
8964
8965 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8966 declared `friend'. If IS_DECLARATION is TRUE, then this
8967 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8968 something is being declared.
8969
8970 Returns the TYPE specified. */
8971
8972static tree
8973cp_parser_elaborated_type_specifier (parser, is_friend, is_declaration)
8974 cp_parser *parser;
8975 bool is_friend;
8976 bool is_declaration;
8977{
8978 enum tag_types tag_type;
8979 tree identifier;
8980 tree type = NULL_TREE;
8981
8982 /* See if we're looking at the `enum' keyword. */
8983 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8984 {
8985 /* Consume the `enum' token. */
8986 cp_lexer_consume_token (parser->lexer);
8987 /* Remember that it's an enumeration type. */
8988 tag_type = enum_type;
8989 }
8990 /* Or, it might be `typename'. */
8991 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8992 RID_TYPENAME))
8993 {
8994 /* Consume the `typename' token. */
8995 cp_lexer_consume_token (parser->lexer);
8996 /* Remember that it's a `typename' type. */
8997 tag_type = typename_type;
8998 /* The `typename' keyword is only allowed in templates. */
8999 if (!processing_template_decl)
9000 pedwarn ("using `typename' outside of template");
9001 }
9002 /* Otherwise it must be a class-key. */
9003 else
9004 {
9005 tag_type = cp_parser_class_key (parser);
9006 if (tag_type == none_type)
9007 return error_mark_node;
9008 }
9009
9010 /* Look for the `::' operator. */
9011 cp_parser_global_scope_opt (parser,
9012 /*current_scope_valid_p=*/false);
9013 /* Look for the nested-name-specifier. */
9014 if (tag_type == typename_type)
9015 cp_parser_nested_name_specifier (parser,
9016 /*typename_keyword_p=*/true,
9017 /*check_dependency_p=*/true,
9018 /*type_p=*/true);
9019 else
9020 /* Even though `typename' is not present, the proposed resolution
9021 to Core Issue 180 says that in `class A<T>::B', `B' should be
9022 considered a type-name, even if `A<T>' is dependent. */
9023 cp_parser_nested_name_specifier_opt (parser,
9024 /*typename_keyword_p=*/true,
9025 /*check_dependency_p=*/true,
9026 /*type_p=*/true);
9027 /* For everything but enumeration types, consider a template-id. */
9028 if (tag_type != enum_type)
9029 {
9030 bool template_p = false;
9031 tree decl;
9032
9033 /* Allow the `template' keyword. */
9034 template_p = cp_parser_optional_template_keyword (parser);
9035 /* If we didn't see `template', we don't know if there's a
9036 template-id or not. */
9037 if (!template_p)
9038 cp_parser_parse_tentatively (parser);
9039 /* Parse the template-id. */
9040 decl = cp_parser_template_id (parser, template_p,
9041 /*check_dependency_p=*/true);
9042 /* If we didn't find a template-id, look for an ordinary
9043 identifier. */
9044 if (!template_p && !cp_parser_parse_definitely (parser))
9045 ;
9046 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9047 in effect, then we must assume that, upon instantiation, the
9048 template will correspond to a class. */
9049 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9050 && tag_type == typename_type)
9051 type = make_typename_type (parser->scope, decl,
9052 /*complain=*/1);
9053 else
9054 type = TREE_TYPE (decl);
9055 }
9056
9057 /* For an enumeration type, consider only a plain identifier. */
9058 if (!type)
9059 {
9060 identifier = cp_parser_identifier (parser);
9061
9062 if (identifier == error_mark_node)
9063 return error_mark_node;
9064
9065 /* For a `typename', we needn't call xref_tag. */
9066 if (tag_type == typename_type)
9067 return make_typename_type (parser->scope, identifier,
9068 /*complain=*/1);
9069 /* Look up a qualified name in the usual way. */
9070 if (parser->scope)
9071 {
9072 tree decl;
9073
9074 /* In an elaborated-type-specifier, names are assumed to name
9075 types, so we set IS_TYPE to TRUE when calling
9076 cp_parser_lookup_name. */
9077 decl = cp_parser_lookup_name (parser, identifier,
9078 /*check_access=*/true,
9079 /*is_type=*/true,
eea9800f 9080 /*is_namespace=*/false,
a723baf1
MM
9081 /*check_dependency=*/true);
9082 decl = (cp_parser_maybe_treat_template_as_class
9083 (decl, /*tag_name_p=*/is_friend));
9084
9085 if (TREE_CODE (decl) != TYPE_DECL)
9086 {
9087 error ("expected type-name");
9088 return error_mark_node;
9089 }
9090 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
9091 && tag_type != enum_type)
9092 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
9093 tag_type == record_type ? "struct" : "class");
9094 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
9095 && tag_type == enum_type)
9096 error ("`%T' referred to as enum", TREE_TYPE (decl));
9097
9098 type = TREE_TYPE (decl);
9099 }
9100 else
9101 {
9102 /* An elaborated-type-specifier sometimes introduces a new type and
9103 sometimes names an existing type. Normally, the rule is that it
9104 introduces a new type only if there is not an existing type of
9105 the same name already in scope. For example, given:
9106
9107 struct S {};
9108 void f() { struct S s; }
9109
9110 the `struct S' in the body of `f' is the same `struct S' as in
9111 the global scope; the existing definition is used. However, if
9112 there were no global declaration, this would introduce a new
9113 local class named `S'.
9114
9115 An exception to this rule applies to the following code:
9116
9117 namespace N { struct S; }
9118
9119 Here, the elaborated-type-specifier names a new type
9120 unconditionally; even if there is already an `S' in the
9121 containing scope this declaration names a new type.
9122 This exception only applies if the elaborated-type-specifier
9123 forms the complete declaration:
9124
9125 [class.name]
9126
9127 A declaration consisting solely of `class-key identifier ;' is
9128 either a redeclaration of the name in the current scope or a
9129 forward declaration of the identifier as a class name. It
9130 introduces the name into the current scope.
9131
9132 We are in this situation precisely when the next token is a `;'.
9133
9134 An exception to the exception is that a `friend' declaration does
9135 *not* name a new type; i.e., given:
9136
9137 struct S { friend struct T; };
9138
9139 `T' is not a new type in the scope of `S'.
9140
9141 Also, `new struct S' or `sizeof (struct S)' never results in the
9142 definition of a new type; a new type can only be declared in a
9143 declaration context. */
9144
9145 type = xref_tag (tag_type, identifier,
9146 /*attributes=*/NULL_TREE,
9147 (is_friend
9148 || !is_declaration
9149 || cp_lexer_next_token_is_not (parser->lexer,
9150 CPP_SEMICOLON)));
9151 }
9152 }
9153 if (tag_type != enum_type)
9154 cp_parser_check_class_key (tag_type, type);
9155 return type;
9156}
9157
9158/* Parse an enum-specifier.
9159
9160 enum-specifier:
9161 enum identifier [opt] { enumerator-list [opt] }
9162
9163 Returns an ENUM_TYPE representing the enumeration. */
9164
9165static tree
9166cp_parser_enum_specifier (parser)
9167 cp_parser *parser;
9168{
9169 cp_token *token;
9170 tree identifier = NULL_TREE;
9171 tree type;
9172
9173 /* Look for the `enum' keyword. */
9174 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9175 return error_mark_node;
9176 /* Peek at the next token. */
9177 token = cp_lexer_peek_token (parser->lexer);
9178
9179 /* See if it is an identifier. */
9180 if (token->type == CPP_NAME)
9181 identifier = cp_parser_identifier (parser);
9182
9183 /* Look for the `{'. */
9184 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9185 return error_mark_node;
9186
9187 /* At this point, we're going ahead with the enum-specifier, even
9188 if some other problem occurs. */
9189 cp_parser_commit_to_tentative_parse (parser);
9190
9191 /* Issue an error message if type-definitions are forbidden here. */
9192 cp_parser_check_type_definition (parser);
9193
9194 /* Create the new type. */
9195 type = start_enum (identifier ? identifier : make_anon_name ());
9196
9197 /* Peek at the next token. */
9198 token = cp_lexer_peek_token (parser->lexer);
9199 /* If it's not a `}', then there are some enumerators. */
9200 if (token->type != CPP_CLOSE_BRACE)
9201 cp_parser_enumerator_list (parser, type);
9202 /* Look for the `}'. */
9203 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9204
9205 /* Finish up the enumeration. */
9206 finish_enum (type);
9207
9208 return type;
9209}
9210
9211/* Parse an enumerator-list. The enumerators all have the indicated
9212 TYPE.
9213
9214 enumerator-list:
9215 enumerator-definition
9216 enumerator-list , enumerator-definition */
9217
9218static void
9219cp_parser_enumerator_list (parser, type)
9220 cp_parser *parser;
9221 tree type;
9222{
9223 while (true)
9224 {
9225 cp_token *token;
9226
9227 /* Parse an enumerator-definition. */
9228 cp_parser_enumerator_definition (parser, type);
9229 /* Peek at the next token. */
9230 token = cp_lexer_peek_token (parser->lexer);
9231 /* If it's not a `,', then we've reached the end of the
9232 list. */
9233 if (token->type != CPP_COMMA)
9234 break;
9235 /* Otherwise, consume the `,' and keep going. */
9236 cp_lexer_consume_token (parser->lexer);
9237 /* If the next token is a `}', there is a trailing comma. */
9238 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9239 {
9240 if (pedantic && !in_system_header)
9241 pedwarn ("comma at end of enumerator list");
9242 break;
9243 }
9244 }
9245}
9246
9247/* Parse an enumerator-definition. The enumerator has the indicated
9248 TYPE.
9249
9250 enumerator-definition:
9251 enumerator
9252 enumerator = constant-expression
9253
9254 enumerator:
9255 identifier */
9256
9257static void
9258cp_parser_enumerator_definition (parser, type)
9259 cp_parser *parser;
9260 tree type;
9261{
9262 cp_token *token;
9263 tree identifier;
9264 tree value;
9265
9266 /* Look for the identifier. */
9267 identifier = cp_parser_identifier (parser);
9268 if (identifier == error_mark_node)
9269 return;
9270
9271 /* Peek at the next token. */
9272 token = cp_lexer_peek_token (parser->lexer);
9273 /* If it's an `=', then there's an explicit value. */
9274 if (token->type == CPP_EQ)
9275 {
9276 /* Consume the `=' token. */
9277 cp_lexer_consume_token (parser->lexer);
9278 /* Parse the value. */
9279 value = cp_parser_constant_expression (parser);
9280 }
9281 else
9282 value = NULL_TREE;
9283
9284 /* Create the enumerator. */
9285 build_enumerator (identifier, value, type);
9286}
9287
9288/* Parse a namespace-name.
9289
9290 namespace-name:
9291 original-namespace-name
9292 namespace-alias
9293
9294 Returns the NAMESPACE_DECL for the namespace. */
9295
9296static tree
9297cp_parser_namespace_name (parser)
9298 cp_parser *parser;
9299{
9300 tree identifier;
9301 tree namespace_decl;
9302
9303 /* Get the name of the namespace. */
9304 identifier = cp_parser_identifier (parser);
9305 if (identifier == error_mark_node)
9306 return error_mark_node;
9307
eea9800f
MM
9308 /* Look up the identifier in the currently active scope. Look only
9309 for namespaces, due to:
9310
9311 [basic.lookup.udir]
9312
9313 When looking up a namespace-name in a using-directive or alias
9314 definition, only namespace names are considered.
9315
9316 And:
9317
9318 [basic.lookup.qual]
9319
9320 During the lookup of a name preceding the :: scope resolution
9321 operator, object, function, and enumerator names are ignored.
9322
9323 (Note that cp_parser_class_or_namespace_name only calls this
9324 function if the token after the name is the scope resolution
9325 operator.) */
9326 namespace_decl = cp_parser_lookup_name (parser, identifier,
9327 /*check_access=*/true,
9328 /*is_type=*/false,
9329 /*is_namespace=*/true,
9330 /*check_dependency=*/true);
a723baf1
MM
9331 /* If it's not a namespace, issue an error. */
9332 if (namespace_decl == error_mark_node
9333 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9334 {
9335 cp_parser_error (parser, "expected namespace-name");
9336 namespace_decl = error_mark_node;
9337 }
9338
9339 return namespace_decl;
9340}
9341
9342/* Parse a namespace-definition.
9343
9344 namespace-definition:
9345 named-namespace-definition
9346 unnamed-namespace-definition
9347
9348 named-namespace-definition:
9349 original-namespace-definition
9350 extension-namespace-definition
9351
9352 original-namespace-definition:
9353 namespace identifier { namespace-body }
9354
9355 extension-namespace-definition:
9356 namespace original-namespace-name { namespace-body }
9357
9358 unnamed-namespace-definition:
9359 namespace { namespace-body } */
9360
9361static void
9362cp_parser_namespace_definition (parser)
9363 cp_parser *parser;
9364{
9365 tree identifier;
9366
9367 /* Look for the `namespace' keyword. */
9368 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9369
9370 /* Get the name of the namespace. We do not attempt to distinguish
9371 between an original-namespace-definition and an
9372 extension-namespace-definition at this point. The semantic
9373 analysis routines are responsible for that. */
9374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9375 identifier = cp_parser_identifier (parser);
9376 else
9377 identifier = NULL_TREE;
9378
9379 /* Look for the `{' to start the namespace. */
9380 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9381 /* Start the namespace. */
9382 push_namespace (identifier);
9383 /* Parse the body of the namespace. */
9384 cp_parser_namespace_body (parser);
9385 /* Finish the namespace. */
9386 pop_namespace ();
9387 /* Look for the final `}'. */
9388 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9389}
9390
9391/* Parse a namespace-body.
9392
9393 namespace-body:
9394 declaration-seq [opt] */
9395
9396static void
9397cp_parser_namespace_body (parser)
9398 cp_parser *parser;
9399{
9400 cp_parser_declaration_seq_opt (parser);
9401}
9402
9403/* Parse a namespace-alias-definition.
9404
9405 namespace-alias-definition:
9406 namespace identifier = qualified-namespace-specifier ; */
9407
9408static void
9409cp_parser_namespace_alias_definition (parser)
9410 cp_parser *parser;
9411{
9412 tree identifier;
9413 tree namespace_specifier;
9414
9415 /* Look for the `namespace' keyword. */
9416 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9417 /* Look for the identifier. */
9418 identifier = cp_parser_identifier (parser);
9419 if (identifier == error_mark_node)
9420 return;
9421 /* Look for the `=' token. */
9422 cp_parser_require (parser, CPP_EQ, "`='");
9423 /* Look for the qualified-namespace-specifier. */
9424 namespace_specifier
9425 = cp_parser_qualified_namespace_specifier (parser);
9426 /* Look for the `;' token. */
9427 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9428
9429 /* Register the alias in the symbol table. */
9430 do_namespace_alias (identifier, namespace_specifier);
9431}
9432
9433/* Parse a qualified-namespace-specifier.
9434
9435 qualified-namespace-specifier:
9436 :: [opt] nested-name-specifier [opt] namespace-name
9437
9438 Returns a NAMESPACE_DECL corresponding to the specified
9439 namespace. */
9440
9441static tree
9442cp_parser_qualified_namespace_specifier (parser)
9443 cp_parser *parser;
9444{
9445 /* Look for the optional `::'. */
9446 cp_parser_global_scope_opt (parser,
9447 /*current_scope_valid_p=*/false);
9448
9449 /* Look for the optional nested-name-specifier. */
9450 cp_parser_nested_name_specifier_opt (parser,
9451 /*typename_keyword_p=*/false,
9452 /*check_dependency_p=*/true,
9453 /*type_p=*/false);
9454
9455 return cp_parser_namespace_name (parser);
9456}
9457
9458/* Parse a using-declaration.
9459
9460 using-declaration:
9461 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9462 using :: unqualified-id ; */
9463
9464static void
9465cp_parser_using_declaration (parser)
9466 cp_parser *parser;
9467{
9468 cp_token *token;
9469 bool typename_p = false;
9470 bool global_scope_p;
9471 tree decl;
9472 tree identifier;
9473 tree scope;
9474
9475 /* Look for the `using' keyword. */
9476 cp_parser_require_keyword (parser, RID_USING, "`using'");
9477
9478 /* Peek at the next token. */
9479 token = cp_lexer_peek_token (parser->lexer);
9480 /* See if it's `typename'. */
9481 if (token->keyword == RID_TYPENAME)
9482 {
9483 /* Remember that we've seen it. */
9484 typename_p = true;
9485 /* Consume the `typename' token. */
9486 cp_lexer_consume_token (parser->lexer);
9487 }
9488
9489 /* Look for the optional global scope qualification. */
9490 global_scope_p
9491 = (cp_parser_global_scope_opt (parser,
9492 /*current_scope_valid_p=*/false)
9493 != NULL_TREE);
9494
9495 /* If we saw `typename', or didn't see `::', then there must be a
9496 nested-name-specifier present. */
9497 if (typename_p || !global_scope_p)
9498 cp_parser_nested_name_specifier (parser, typename_p,
9499 /*check_dependency_p=*/true,
9500 /*type_p=*/false);
9501 /* Otherwise, we could be in either of the two productions. In that
9502 case, treat the nested-name-specifier as optional. */
9503 else
9504 cp_parser_nested_name_specifier_opt (parser,
9505 /*typename_keyword_p=*/false,
9506 /*check_dependency_p=*/true,
9507 /*type_p=*/false);
9508
9509 /* Parse the unqualified-id. */
9510 identifier = cp_parser_unqualified_id (parser,
9511 /*template_keyword_p=*/false,
9512 /*check_dependency_p=*/true);
9513
9514 /* The function we call to handle a using-declaration is different
9515 depending on what scope we are in. */
9516 scope = current_scope ();
9517 if (scope && TYPE_P (scope))
9518 {
9519 /* Create the USING_DECL. */
9520 decl = do_class_using_decl (build_nt (SCOPE_REF,
9521 parser->scope,
9522 identifier));
9523 /* Add it to the list of members in this class. */
9524 finish_member_declaration (decl);
9525 }
9526 else
9527 {
9528 decl = cp_parser_lookup_name_simple (parser, identifier);
9529 if (scope)
9530 do_local_using_decl (decl);
9531 else
9532 do_toplevel_using_decl (decl);
9533 }
9534
9535 /* Look for the final `;'. */
9536 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9537}
9538
9539/* Parse a using-directive.
9540
9541 using-directive:
9542 using namespace :: [opt] nested-name-specifier [opt]
9543 namespace-name ; */
9544
9545static void
9546cp_parser_using_directive (parser)
9547 cp_parser *parser;
9548{
9549 tree namespace_decl;
9550
9551 /* Look for the `using' keyword. */
9552 cp_parser_require_keyword (parser, RID_USING, "`using'");
9553 /* And the `namespace' keyword. */
9554 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9555 /* Look for the optional `::' operator. */
9556 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9557 /* And the optional nested-name-sepcifier. */
9558 cp_parser_nested_name_specifier_opt (parser,
9559 /*typename_keyword_p=*/false,
9560 /*check_dependency_p=*/true,
9561 /*type_p=*/false);
9562 /* Get the namespace being used. */
9563 namespace_decl = cp_parser_namespace_name (parser);
9564 /* Update the symbol table. */
9565 do_using_directive (namespace_decl);
9566 /* Look for the final `;'. */
9567 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9568}
9569
9570/* Parse an asm-definition.
9571
9572 asm-definition:
9573 asm ( string-literal ) ;
9574
9575 GNU Extension:
9576
9577 asm-definition:
9578 asm volatile [opt] ( string-literal ) ;
9579 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9580 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9581 : asm-operand-list [opt] ) ;
9582 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9583 : asm-operand-list [opt]
9584 : asm-operand-list [opt] ) ; */
9585
9586static void
9587cp_parser_asm_definition (parser)
9588 cp_parser *parser;
9589{
9590 cp_token *token;
9591 tree string;
9592 tree outputs = NULL_TREE;
9593 tree inputs = NULL_TREE;
9594 tree clobbers = NULL_TREE;
9595 tree asm_stmt;
9596 bool volatile_p = false;
9597 bool extended_p = false;
9598
9599 /* Look for the `asm' keyword. */
9600 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9601 /* See if the next token is `volatile'. */
9602 if (cp_parser_allow_gnu_extensions_p (parser)
9603 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9604 {
9605 /* Remember that we saw the `volatile' keyword. */
9606 volatile_p = true;
9607 /* Consume the token. */
9608 cp_lexer_consume_token (parser->lexer);
9609 }
9610 /* Look for the opening `('. */
9611 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9612 /* Look for the string. */
9613 token = cp_parser_require (parser, CPP_STRING, "asm body");
9614 if (!token)
9615 return;
9616 string = token->value;
9617 /* If we're allowing GNU extensions, check for the extended assembly
9618 syntax. Unfortunately, the `:' tokens need not be separated by
9619 a space in C, and so, for compatibility, we tolerate that here
9620 too. Doing that means that we have to treat the `::' operator as
9621 two `:' tokens. */
9622 if (cp_parser_allow_gnu_extensions_p (parser)
9623 && at_function_scope_p ()
9624 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9625 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9626 {
9627 bool inputs_p = false;
9628 bool clobbers_p = false;
9629
9630 /* The extended syntax was used. */
9631 extended_p = true;
9632
9633 /* Look for outputs. */
9634 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9635 {
9636 /* Consume the `:'. */
9637 cp_lexer_consume_token (parser->lexer);
9638 /* Parse the output-operands. */
9639 if (cp_lexer_next_token_is_not (parser->lexer,
9640 CPP_COLON)
9641 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9642 CPP_SCOPE)
9643 && cp_lexer_next_token_is_not (parser->lexer,
9644 CPP_CLOSE_PAREN))
a723baf1
MM
9645 outputs = cp_parser_asm_operand_list (parser);
9646 }
9647 /* If the next token is `::', there are no outputs, and the
9648 next token is the beginning of the inputs. */
9649 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9650 {
9651 /* Consume the `::' token. */
9652 cp_lexer_consume_token (parser->lexer);
9653 /* The inputs are coming next. */
9654 inputs_p = true;
9655 }
9656
9657 /* Look for inputs. */
9658 if (inputs_p
9659 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9660 {
9661 if (!inputs_p)
9662 /* Consume the `:'. */
9663 cp_lexer_consume_token (parser->lexer);
9664 /* Parse the output-operands. */
9665 if (cp_lexer_next_token_is_not (parser->lexer,
9666 CPP_COLON)
9667 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9668 CPP_SCOPE)
9669 && cp_lexer_next_token_is_not (parser->lexer,
9670 CPP_CLOSE_PAREN))
a723baf1
MM
9671 inputs = cp_parser_asm_operand_list (parser);
9672 }
9673 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9674 /* The clobbers are coming next. */
9675 clobbers_p = true;
9676
9677 /* Look for clobbers. */
9678 if (clobbers_p
9679 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9680 {
9681 if (!clobbers_p)
9682 /* Consume the `:'. */
9683 cp_lexer_consume_token (parser->lexer);
9684 /* Parse the clobbers. */
8caf4c38
MM
9685 if (cp_lexer_next_token_is_not (parser->lexer,
9686 CPP_CLOSE_PAREN))
9687 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9688 }
9689 }
9690 /* Look for the closing `)'. */
9691 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9692 cp_parser_skip_to_closing_parenthesis (parser);
9693 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9694
9695 /* Create the ASM_STMT. */
9696 if (at_function_scope_p ())
9697 {
9698 asm_stmt =
9699 finish_asm_stmt (volatile_p
9700 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9701 string, outputs, inputs, clobbers);
9702 /* If the extended syntax was not used, mark the ASM_STMT. */
9703 if (!extended_p)
9704 ASM_INPUT_P (asm_stmt) = 1;
9705 }
9706 else
9707 assemble_asm (string);
9708}
9709
9710/* Declarators [gram.dcl.decl] */
9711
9712/* Parse an init-declarator.
9713
9714 init-declarator:
9715 declarator initializer [opt]
9716
9717 GNU Extension:
9718
9719 init-declarator:
9720 declarator asm-specification [opt] attributes [opt] initializer [opt]
9721
9722 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9723 Returns a reprsentation of the entity declared. The ACCESS_CHECKS
9724 represent deferred access checks from the decl-specifier-seq. If
9725 MEMBER_P is TRUE, then this declarator appears in a class scope.
9726 The new DECL created by this declarator is returned.
9727
9728 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9729 for a function-definition here as well. If the declarator is a
9730 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9731 be TRUE upon return. By that point, the function-definition will
9732 have been completely parsed.
9733
9734 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9735 is FALSE. */
9736
9737static tree
9738cp_parser_init_declarator (parser,
9739 decl_specifiers,
9740 prefix_attributes,
9741 access_checks,
9742 function_definition_allowed_p,
9743 member_p,
9744 function_definition_p)
9745 cp_parser *parser;
9746 tree decl_specifiers;
9747 tree prefix_attributes;
9748 tree access_checks;
9749 bool function_definition_allowed_p;
9750 bool member_p;
9751 bool *function_definition_p;
9752{
9753 cp_token *token;
9754 tree declarator;
9755 tree attributes;
9756 tree asm_specification;
9757 tree initializer;
9758 tree decl = NULL_TREE;
9759 tree scope;
9760 tree declarator_access_checks;
9761 bool is_initialized;
9762 bool is_parenthesized_init;
9763 bool ctor_dtor_or_conv_p;
9764 bool friend_p;
9765
9766 /* Assume that this is not the declarator for a function
9767 definition. */
9768 if (function_definition_p)
9769 *function_definition_p = false;
9770
9771 /* Defer access checks while parsing the declarator; we cannot know
9772 what names are accessible until we know what is being
9773 declared. */
9774 cp_parser_start_deferring_access_checks (parser);
9775 /* Parse the declarator. */
9776 declarator
62b8a44e 9777 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
9778 &ctor_dtor_or_conv_p);
9779 /* Gather up the deferred checks. */
9780 declarator_access_checks
9781 = cp_parser_stop_deferring_access_checks (parser);
9782
24c0ef37
GS
9783 /* Prevent the access checks from being reclaimed by GC. */
9784 parser->access_checks_lists
9785 = tree_cons (NULL_TREE, declarator_access_checks,
9786 parser->access_checks_lists);
9787
a723baf1
MM
9788 /* If the DECLARATOR was erroneous, there's no need to go
9789 further. */
9790 if (declarator == error_mark_node)
24c0ef37
GS
9791 {
9792 /* Discard access checks no longer in use. */
9793 parser->access_checks_lists
9794 = TREE_CHAIN (parser->access_checks_lists);
9795 return error_mark_node;
9796 }
a723baf1
MM
9797
9798 /* Figure out what scope the entity declared by the DECLARATOR is
9799 located in. `grokdeclarator' sometimes changes the scope, so
9800 we compute it now. */
9801 scope = get_scope_of_declarator (declarator);
9802
9803 /* If we're allowing GNU extensions, look for an asm-specification
9804 and attributes. */
9805 if (cp_parser_allow_gnu_extensions_p (parser))
9806 {
9807 /* Look for an asm-specification. */
9808 asm_specification = cp_parser_asm_specification_opt (parser);
9809 /* And attributes. */
9810 attributes = cp_parser_attributes_opt (parser);
9811 }
9812 else
9813 {
9814 asm_specification = NULL_TREE;
9815 attributes = NULL_TREE;
9816 }
9817
9818 /* Peek at the next token. */
9819 token = cp_lexer_peek_token (parser->lexer);
9820 /* Check to see if the token indicates the start of a
9821 function-definition. */
9822 if (cp_parser_token_starts_function_definition_p (token))
9823 {
9824 if (!function_definition_allowed_p)
9825 {
9826 /* If a function-definition should not appear here, issue an
9827 error message. */
9828 cp_parser_error (parser,
9829 "a function-definition is not allowed here");
24c0ef37
GS
9830 /* Discard access checks no longer in use. */
9831 parser->access_checks_lists
9832 = TREE_CHAIN (parser->access_checks_lists);
a723baf1
MM
9833 return error_mark_node;
9834 }
9835 else
9836 {
9837 tree *ac;
9838
9839 /* Neither attributes nor an asm-specification are allowed
9840 on a function-definition. */
9841 if (asm_specification)
9842 error ("an asm-specification is not allowed on a function-definition");
9843 if (attributes)
9844 error ("attributes are not allowed on a function-definition");
9845 /* This is a function-definition. */
9846 *function_definition_p = true;
9847
9848 /* Thread the access checks together. */
9849 ac = &access_checks;
9850 while (*ac)
9851 ac = &TREE_CHAIN (*ac);
9852 *ac = declarator_access_checks;
9853
9854 /* Parse the function definition. */
9855 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9856 (parser, decl_specifiers, prefix_attributes, declarator,
9857 access_checks));
9858
9859 /* Pull the access-checks apart again. */
9860 *ac = NULL_TREE;
9861
24c0ef37
GS
9862 /* Discard access checks no longer in use. */
9863 parser->access_checks_lists
9864 = TREE_CHAIN (parser->access_checks_lists);
9865
a723baf1
MM
9866 return decl;
9867 }
9868 }
9869
9870 /* [dcl.dcl]
9871
9872 Only in function declarations for constructors, destructors, and
9873 type conversions can the decl-specifier-seq be omitted.
9874
9875 We explicitly postpone this check past the point where we handle
9876 function-definitions because we tolerate function-definitions
9877 that are missing their return types in some modes. */
9878 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9879 {
9880 cp_parser_error (parser,
9881 "expected constructor, destructor, or type conversion");
24c0ef37
GS
9882 /* Discard access checks no longer in use. */
9883 parser->access_checks_lists
9884 = TREE_CHAIN (parser->access_checks_lists);
a723baf1
MM
9885 return error_mark_node;
9886 }
9887
9888 /* An `=' or an `(' indicates an initializer. */
9889 is_initialized = (token->type == CPP_EQ
9890 || token->type == CPP_OPEN_PAREN);
9891 /* If the init-declarator isn't initialized and isn't followed by a
9892 `,' or `;', it's not a valid init-declarator. */
9893 if (!is_initialized
9894 && token->type != CPP_COMMA
9895 && token->type != CPP_SEMICOLON)
9896 {
9897 cp_parser_error (parser, "expected init-declarator");
24c0ef37
GS
9898 /* Discard access checks no longer in use. */
9899 parser->access_checks_lists
9900 = TREE_CHAIN (parser->access_checks_lists);
a723baf1
MM
9901 return error_mark_node;
9902 }
9903
9904 /* Because start_decl has side-effects, we should only call it if we
9905 know we're going ahead. By this point, we know that we cannot
9906 possibly be looking at any other construct. */
9907 cp_parser_commit_to_tentative_parse (parser);
9908
9909 /* Check to see whether or not this declaration is a friend. */
9910 friend_p = cp_parser_friend_p (decl_specifiers);
9911
9912 /* Check that the number of template-parameter-lists is OK. */
9913 if (!cp_parser_check_declarator_template_parameters (parser,
9914 declarator))
24c0ef37
GS
9915 {
9916 /* Discard access checks no longer in use. */
9917 parser->access_checks_lists
9918 = TREE_CHAIN (parser->access_checks_lists);
9919 return error_mark_node;
9920 }
a723baf1
MM
9921
9922 /* Enter the newly declared entry in the symbol table. If we're
9923 processing a declaration in a class-specifier, we wait until
9924 after processing the initializer. */
9925 if (!member_p)
9926 {
9927 if (parser->in_unbraced_linkage_specification_p)
9928 {
9929 decl_specifiers = tree_cons (error_mark_node,
9930 get_identifier ("extern"),
9931 decl_specifiers);
9932 have_extern_spec = false;
9933 }
9934 decl = start_decl (declarator,
9935 decl_specifiers,
9936 is_initialized,
9937 attributes,
9938 prefix_attributes);
9939 }
9940
9941 /* Enter the SCOPE. That way unqualified names appearing in the
9942 initializer will be looked up in SCOPE. */
9943 if (scope)
9944 push_scope (scope);
9945
9946 /* Perform deferred access control checks, now that we know in which
9947 SCOPE the declared entity resides. */
9948 if (!member_p && decl)
9949 {
9950 tree saved_current_function_decl = NULL_TREE;
9951
9952 /* If the entity being declared is a function, pretend that we
9953 are in its scope. If it is a `friend', it may have access to
9954 things that would not otherwise be accessible. */
9955 if (TREE_CODE (decl) == FUNCTION_DECL)
9956 {
9957 saved_current_function_decl = current_function_decl;
9958 current_function_decl = decl;
9959 }
9960
9961 /* Perform the access control checks for the decl-specifiers. */
9962 cp_parser_perform_deferred_access_checks (access_checks);
9963 /* And for the declarator. */
9964 cp_parser_perform_deferred_access_checks (declarator_access_checks);
9965
9966 /* Restore the saved value. */
9967 if (TREE_CODE (decl) == FUNCTION_DECL)
9968 current_function_decl = saved_current_function_decl;
9969 }
9970
9971 /* Parse the initializer. */
9972 if (is_initialized)
9973 initializer = cp_parser_initializer (parser,
9974 &is_parenthesized_init);
9975 else
9976 {
9977 initializer = NULL_TREE;
9978 is_parenthesized_init = false;
9979 }
9980
9981 /* The old parser allows attributes to appear after a parenthesized
9982 initializer. Mark Mitchell proposed removing this functionality
9983 on the GCC mailing lists on 2002-08-13. This parser accepts the
9984 attributes -- but ignores them. */
9985 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9986 if (cp_parser_attributes_opt (parser))
9987 warning ("attributes after parenthesized initializer ignored");
9988
9989 /* Leave the SCOPE, now that we have processed the initializer. It
9990 is important to do this before calling cp_finish_decl because it
9991 makes decisions about whether to create DECL_STMTs or not based
9992 on the current scope. */
9993 if (scope)
9994 pop_scope (scope);
9995
9996 /* For an in-class declaration, use `grokfield' to create the
9997 declaration. */
9998 if (member_p)
9999 decl = grokfield (declarator, decl_specifiers,
10000 initializer, /*asmspec=*/NULL_TREE,
10001 /*attributes=*/NULL_TREE);
10002
10003 /* Finish processing the declaration. But, skip friend
10004 declarations. */
10005 if (!friend_p && decl)
10006 cp_finish_decl (decl,
10007 initializer,
10008 asm_specification,
10009 /* If the initializer is in parentheses, then this is
10010 a direct-initialization, which means that an
10011 `explicit' constructor is OK. Otherwise, an
10012 `explicit' constructor cannot be used. */
10013 ((is_parenthesized_init || !is_initialized)
10014 ? 0 : LOOKUP_ONLYCONVERTING));
10015
24c0ef37
GS
10016 /* Discard access checks no longer in use. */
10017 parser->access_checks_lists
10018 = TREE_CHAIN (parser->access_checks_lists);
10019
a723baf1
MM
10020 return decl;
10021}
10022
10023/* Parse a declarator.
10024
10025 declarator:
10026 direct-declarator
10027 ptr-operator declarator
10028
10029 abstract-declarator:
10030 ptr-operator abstract-declarator [opt]
10031 direct-abstract-declarator
10032
10033 GNU Extensions:
10034
10035 declarator:
10036 attributes [opt] direct-declarator
10037 attributes [opt] ptr-operator declarator
10038
10039 abstract-declarator:
10040 attributes [opt] ptr-operator abstract-declarator [opt]
10041 attributes [opt] direct-abstract-declarator
10042
10043 Returns a representation of the declarator. If the declarator has
10044 the form `* declarator', then an INDIRECT_REF is returned, whose
10045 only operand is the sub-declarator. Analagously, `& declarator' is
10046 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
10047 used. The first operand is the TYPE for `X'. The second operand
10048 is an INDIRECT_REF whose operand is the sub-declarator.
10049
10050 Otherwise, the reprsentation is as for a direct-declarator.
10051
10052 (It would be better to define a structure type to represent
10053 declarators, rather than abusing `tree' nodes to represent
10054 declarators. That would be much clearer and save some memory.
10055 There is no reason for declarators to be garbage-collected, for
10056 example; they are created during parser and no longer needed after
10057 `grokdeclarator' has been called.)
10058
10059 For a ptr-operator that has the optional cv-qualifier-seq,
10060 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10061 node.
10062
10063 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
10064 true if this declarator represents a constructor, destructor, or
10065 type conversion operator. Otherwise, it is set to false.
10066
10067 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10068 a decl-specifier-seq unless it declares a constructor, destructor,
10069 or conversion. It might seem that we could check this condition in
10070 semantic analysis, rather than parsing, but that makes it difficult
10071 to handle something like `f()'. We want to notice that there are
10072 no decl-specifiers, and therefore realize that this is an
10073 expression, not a declaration.) */
10074
10075static tree
62b8a44e 10076cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
a723baf1 10077 cp_parser *parser;
62b8a44e 10078 cp_parser_declarator_kind dcl_kind;
a723baf1
MM
10079 bool *ctor_dtor_or_conv_p;
10080{
10081 cp_token *token;
10082 tree declarator;
10083 enum tree_code code;
10084 tree cv_qualifier_seq;
10085 tree class_type;
10086 tree attributes = NULL_TREE;
10087
10088 /* Assume this is not a constructor, destructor, or type-conversion
10089 operator. */
10090 if (ctor_dtor_or_conv_p)
10091 *ctor_dtor_or_conv_p = false;
10092
10093 if (cp_parser_allow_gnu_extensions_p (parser))
10094 attributes = cp_parser_attributes_opt (parser);
10095
10096 /* Peek at the next token. */
10097 token = cp_lexer_peek_token (parser->lexer);
10098
10099 /* Check for the ptr-operator production. */
10100 cp_parser_parse_tentatively (parser);
10101 /* Parse the ptr-operator. */
10102 code = cp_parser_ptr_operator (parser,
10103 &class_type,
10104 &cv_qualifier_seq);
10105 /* If that worked, then we have a ptr-operator. */
10106 if (cp_parser_parse_definitely (parser))
10107 {
10108 /* The dependent declarator is optional if we are parsing an
10109 abstract-declarator. */
62b8a44e 10110 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
10111 cp_parser_parse_tentatively (parser);
10112
10113 /* Parse the dependent declarator. */
62b8a44e 10114 declarator = cp_parser_declarator (parser, dcl_kind,
a723baf1
MM
10115 /*ctor_dtor_or_conv_p=*/NULL);
10116
10117 /* If we are parsing an abstract-declarator, we must handle the
10118 case where the dependent declarator is absent. */
62b8a44e
NS
10119 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10120 && !cp_parser_parse_definitely (parser))
a723baf1
MM
10121 declarator = NULL_TREE;
10122
10123 /* Build the representation of the ptr-operator. */
10124 if (code == INDIRECT_REF)
10125 declarator = make_pointer_declarator (cv_qualifier_seq,
10126 declarator);
10127 else
10128 declarator = make_reference_declarator (cv_qualifier_seq,
10129 declarator);
10130 /* Handle the pointer-to-member case. */
10131 if (class_type)
10132 declarator = build_nt (SCOPE_REF, class_type, declarator);
10133 }
10134 /* Everything else is a direct-declarator. */
10135 else
10136 declarator = cp_parser_direct_declarator (parser,
62b8a44e 10137 dcl_kind,
a723baf1
MM
10138 ctor_dtor_or_conv_p);
10139
10140 if (attributes && declarator != error_mark_node)
10141 declarator = tree_cons (attributes, declarator, NULL_TREE);
10142
10143 return declarator;
10144}
10145
10146/* Parse a direct-declarator or direct-abstract-declarator.
10147
10148 direct-declarator:
10149 declarator-id
10150 direct-declarator ( parameter-declaration-clause )
10151 cv-qualifier-seq [opt]
10152 exception-specification [opt]
10153 direct-declarator [ constant-expression [opt] ]
10154 ( declarator )
10155
10156 direct-abstract-declarator:
10157 direct-abstract-declarator [opt]
10158 ( parameter-declaration-clause )
10159 cv-qualifier-seq [opt]
10160 exception-specification [opt]
10161 direct-abstract-declarator [opt] [ constant-expression [opt] ]
10162 ( abstract-declarator )
10163
62b8a44e
NS
10164 Returns a representation of the declarator. DCL_KIND is
10165 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10166 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
10167 we are parsing a direct-declarator. It is
10168 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10169 of ambiguity we prefer an abstract declarator, as per
10170 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
10171 cp_parser_declarator.
10172
10173 For the declarator-id production, the representation is as for an
10174 id-expression, except that a qualified name is represented as a
10175 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
10176 see the documentation of the FUNCTION_DECLARATOR_* macros for
10177 information about how to find the various declarator components.
10178 An array-declarator is represented as an ARRAY_REF. The
10179 direct-declarator is the first operand; the constant-expression
10180 indicating the size of the array is the second operand. */
10181
10182static tree
62b8a44e 10183cp_parser_direct_declarator (parser, dcl_kind, ctor_dtor_or_conv_p)
a723baf1 10184 cp_parser *parser;
62b8a44e 10185 cp_parser_declarator_kind dcl_kind;
a723baf1
MM
10186 bool *ctor_dtor_or_conv_p;
10187{
10188 cp_token *token;
62b8a44e 10189 tree declarator = NULL_TREE;
a723baf1
MM
10190 tree scope = NULL_TREE;
10191 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10192 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e
NS
10193 bool first = true;
10194
10195 while (true)
a723baf1 10196 {
62b8a44e
NS
10197 /* Peek at the next token. */
10198 token = cp_lexer_peek_token (parser->lexer);
10199 if (token->type == CPP_OPEN_PAREN)
a723baf1 10200 {
62b8a44e
NS
10201 /* This is either a parameter-declaration-clause, or a
10202 parenthesized declarator. When we know we are parsing a
2050a1bb 10203 named declarator, it must be a paranthesized declarator
62b8a44e
NS
10204 if FIRST is true. For instance, `(int)' is a
10205 parameter-declaration-clause, with an omitted
10206 direct-abstract-declarator. But `((*))', is a
10207 parenthesized abstract declarator. Finally, when T is a
10208 template parameter `(T)' is a
10209 paremeter-declaration-clause, and not a parenthesized
10210 named declarator.
a723baf1 10211
62b8a44e
NS
10212 We first try and parse a parameter-declaration-clause,
10213 and then try a nested declarator (if FIRST is true).
a723baf1 10214
62b8a44e
NS
10215 It is not an error for it not to be a
10216 parameter-declaration-clause, even when FIRST is
10217 false. Consider,
10218
10219 int i (int);
10220 int i (3);
10221
10222 The first is the declaration of a function while the
10223 second is a the definition of a variable, including its
10224 initializer.
10225
10226 Having seen only the parenthesis, we cannot know which of
10227 these two alternatives should be selected. Even more
10228 complex are examples like:
10229
10230 int i (int (a));
10231 int i (int (3));
10232
10233 The former is a function-declaration; the latter is a
10234 variable initialization.
10235
10236 Thus again, we try a parameter-declation-clause, and if
10237 that fails, we back out and return. */
10238
10239 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 10240 {
62b8a44e
NS
10241 tree params;
10242
10243 cp_parser_parse_tentatively (parser);
a723baf1 10244
62b8a44e
NS
10245 /* Consume the `('. */
10246 cp_lexer_consume_token (parser->lexer);
10247 if (first)
10248 {
10249 /* If this is going to be an abstract declarator, we're
10250 in a declarator and we can't have default args. */
10251 parser->default_arg_ok_p = false;
10252 parser->in_declarator_p = true;
10253 }
10254
10255 /* Parse the parameter-declaration-clause. */
10256 params = cp_parser_parameter_declaration_clause (parser);
10257
10258 /* If all went well, parse the cv-qualifier-seq and the
10259 exception-specfication. */
10260 if (cp_parser_parse_definitely (parser))
10261 {
10262 tree cv_qualifiers;
10263 tree exception_specification;
10264
10265 first = false;
10266 /* Consume the `)'. */
10267 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10268
10269 /* Parse the cv-qualifier-seq. */
10270 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10271 /* And the exception-specification. */
10272 exception_specification
10273 = cp_parser_exception_specification_opt (parser);
10274
10275 /* Create the function-declarator. */
10276 declarator = make_call_declarator (declarator,
10277 params,
10278 cv_qualifiers,
10279 exception_specification);
10280 /* Any subsequent parameter lists are to do with
10281 return type, so are not those of the declared
10282 function. */
10283 parser->default_arg_ok_p = false;
10284
10285 /* Repeat the main loop. */
10286 continue;
10287 }
10288 }
10289
10290 /* If this is the first, we can try a parenthesized
10291 declarator. */
10292 if (first)
a723baf1 10293 {
a723baf1 10294 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e
NS
10295 parser->in_declarator_p = saved_in_declarator_p;
10296
10297 /* Consume the `('. */
10298 cp_lexer_consume_token (parser->lexer);
10299 /* Parse the nested declarator. */
10300 declarator
10301 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10302 first = false;
10303 /* Expect a `)'. */
10304 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10305 declarator = error_mark_node;
10306 if (declarator == error_mark_node)
10307 break;
10308
10309 goto handle_declarator;
a723baf1 10310 }
62b8a44e
NS
10311 /* Otherwise, we must be done. */
10312 else
10313 break;
a723baf1 10314 }
62b8a44e
NS
10315 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10316 && token->type == CPP_OPEN_SQUARE)
a723baf1 10317 {
62b8a44e 10318 /* Parse an array-declarator. */
a723baf1
MM
10319 tree bounds;
10320
62b8a44e
NS
10321 first = false;
10322 parser->default_arg_ok_p = false;
10323 parser->in_declarator_p = true;
a723baf1
MM
10324 /* Consume the `['. */
10325 cp_lexer_consume_token (parser->lexer);
10326 /* Peek at the next token. */
10327 token = cp_lexer_peek_token (parser->lexer);
10328 /* If the next token is `]', then there is no
10329 constant-expression. */
10330 if (token->type != CPP_CLOSE_SQUARE)
10331 bounds = cp_parser_constant_expression (parser);
10332 else
10333 bounds = NULL_TREE;
10334 /* Look for the closing `]'. */
62b8a44e
NS
10335 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10336 {
10337 declarator = error_mark_node;
10338 break;
10339 }
a723baf1
MM
10340
10341 declarator = build_nt (ARRAY_REF, declarator, bounds);
10342 }
62b8a44e 10343 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 10344 {
62b8a44e
NS
10345 /* Parse a declarator_id */
10346 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10347 cp_parser_parse_tentatively (parser);
10348 declarator = cp_parser_declarator_id (parser);
10349 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER
10350 && !cp_parser_parse_definitely (parser))
10351 declarator = error_mark_node;
10352 if (declarator == error_mark_node)
10353 break;
a723baf1 10354
62b8a44e
NS
10355 if (TREE_CODE (declarator) == SCOPE_REF)
10356 {
10357 tree scope = TREE_OPERAND (declarator, 0);
a723baf1 10358
62b8a44e
NS
10359 /* In the declaration of a member of a template class
10360 outside of the class itself, the SCOPE will sometimes
10361 be a TYPENAME_TYPE. For example, given:
10362
10363 template <typename T>
10364 int S<T>::R::i = 3;
10365
10366 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10367 this context, we must resolve S<T>::R to an ordinary
10368 type, rather than a typename type.
10369
10370 The reason we normally avoid resolving TYPENAME_TYPEs
10371 is that a specialization of `S' might render
10372 `S<T>::R' not a type. However, if `S' is
10373 specialized, then this `i' will not be used, so there
10374 is no harm in resolving the types here. */
10375 if (TREE_CODE (scope) == TYPENAME_TYPE)
10376 {
10377 /* Resolve the TYPENAME_TYPE. */
10378 scope = cp_parser_resolve_typename_type (parser, scope);
10379 /* If that failed, the declarator is invalid. */
10380 if (scope == error_mark_node)
10381 return error_mark_node;
10382 /* Build a new DECLARATOR. */
10383 declarator = build_nt (SCOPE_REF,
10384 scope,
10385 TREE_OPERAND (declarator, 1));
10386 }
10387 }
10388
10389 /* Check to see whether the declarator-id names a constructor,
10390 destructor, or conversion. */
10391 if (declarator && ctor_dtor_or_conv_p
10392 && ((TREE_CODE (declarator) == SCOPE_REF
10393 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10394 || (TREE_CODE (declarator) != SCOPE_REF
10395 && at_class_scope_p ())))
a723baf1 10396 {
62b8a44e
NS
10397 tree unqualified_name;
10398 tree class_type;
10399
10400 /* Get the unqualified part of the name. */
10401 if (TREE_CODE (declarator) == SCOPE_REF)
10402 {
10403 class_type = TREE_OPERAND (declarator, 0);
10404 unqualified_name = TREE_OPERAND (declarator, 1);
10405 }
10406 else
10407 {
10408 class_type = current_class_type;
10409 unqualified_name = declarator;
10410 }
10411
10412 /* See if it names ctor, dtor or conv. */
10413 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10414 || IDENTIFIER_TYPENAME_P (unqualified_name)
10415 || constructor_name_p (unqualified_name, class_type))
10416 *ctor_dtor_or_conv_p = true;
a723baf1 10417 }
62b8a44e
NS
10418
10419 handle_declarator:;
10420 scope = get_scope_of_declarator (declarator);
10421 if (scope)
10422 /* Any names that appear after the declarator-id for a member
10423 are looked up in the containing scope. */
10424 push_scope (scope);
10425 parser->in_declarator_p = true;
10426 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10427 || (declarator
10428 && (TREE_CODE (declarator) == SCOPE_REF
10429 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10430 /* Default args are only allowed on function
10431 declarations. */
10432 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 10433 else
62b8a44e
NS
10434 parser->default_arg_ok_p = false;
10435
10436 first = false;
a723baf1 10437 }
62b8a44e 10438 /* We're done. */
a723baf1
MM
10439 else
10440 break;
a723baf1
MM
10441 }
10442
10443 /* For an abstract declarator, we might wind up with nothing at this
10444 point. That's an error; the declarator is not optional. */
10445 if (!declarator)
10446 cp_parser_error (parser, "expected declarator");
10447
10448 /* If we entered a scope, we must exit it now. */
10449 if (scope)
10450 pop_scope (scope);
10451
10452 parser->default_arg_ok_p = saved_default_arg_ok_p;
10453 parser->in_declarator_p = saved_in_declarator_p;
10454
10455 return declarator;
10456}
10457
10458/* Parse a ptr-operator.
10459
10460 ptr-operator:
10461 * cv-qualifier-seq [opt]
10462 &
10463 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10464
10465 GNU Extension:
10466
10467 ptr-operator:
10468 & cv-qualifier-seq [opt]
10469
10470 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10471 used. Returns ADDR_EXPR if a reference was used. In the
10472 case of a pointer-to-member, *TYPE is filled in with the
10473 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10474 with the cv-qualifier-seq, or NULL_TREE, if there are no
10475 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10476
10477static enum tree_code
10478cp_parser_ptr_operator (parser, type, cv_qualifier_seq)
10479 cp_parser *parser;
10480 tree *type;
10481 tree *cv_qualifier_seq;
10482{
10483 enum tree_code code = ERROR_MARK;
10484 cp_token *token;
10485
10486 /* Assume that it's not a pointer-to-member. */
10487 *type = NULL_TREE;
10488 /* And that there are no cv-qualifiers. */
10489 *cv_qualifier_seq = NULL_TREE;
10490
10491 /* Peek at the next token. */
10492 token = cp_lexer_peek_token (parser->lexer);
10493 /* If it's a `*' or `&' we have a pointer or reference. */
10494 if (token->type == CPP_MULT || token->type == CPP_AND)
10495 {
10496 /* Remember which ptr-operator we were processing. */
10497 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10498
10499 /* Consume the `*' or `&'. */
10500 cp_lexer_consume_token (parser->lexer);
10501
10502 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10503 `&', if we are allowing GNU extensions. (The only qualifier
10504 that can legally appear after `&' is `restrict', but that is
10505 enforced during semantic analysis. */
10506 if (code == INDIRECT_REF
10507 || cp_parser_allow_gnu_extensions_p (parser))
10508 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10509 }
10510 else
10511 {
10512 /* Try the pointer-to-member case. */
10513 cp_parser_parse_tentatively (parser);
10514 /* Look for the optional `::' operator. */
10515 cp_parser_global_scope_opt (parser,
10516 /*current_scope_valid_p=*/false);
10517 /* Look for the nested-name specifier. */
10518 cp_parser_nested_name_specifier (parser,
10519 /*typename_keyword_p=*/false,
10520 /*check_dependency_p=*/true,
10521 /*type_p=*/false);
10522 /* If we found it, and the next token is a `*', then we are
10523 indeed looking at a pointer-to-member operator. */
10524 if (!cp_parser_error_occurred (parser)
10525 && cp_parser_require (parser, CPP_MULT, "`*'"))
10526 {
10527 /* The type of which the member is a member is given by the
10528 current SCOPE. */
10529 *type = parser->scope;
10530 /* The next name will not be qualified. */
10531 parser->scope = NULL_TREE;
10532 parser->qualifying_scope = NULL_TREE;
10533 parser->object_scope = NULL_TREE;
10534 /* Indicate that the `*' operator was used. */
10535 code = INDIRECT_REF;
10536 /* Look for the optional cv-qualifier-seq. */
10537 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10538 }
10539 /* If that didn't work we don't have a ptr-operator. */
10540 if (!cp_parser_parse_definitely (parser))
10541 cp_parser_error (parser, "expected ptr-operator");
10542 }
10543
10544 return code;
10545}
10546
10547/* Parse an (optional) cv-qualifier-seq.
10548
10549 cv-qualifier-seq:
10550 cv-qualifier cv-qualifier-seq [opt]
10551
10552 Returns a TREE_LIST. The TREE_VALUE of each node is the
10553 representation of a cv-qualifier. */
10554
10555static tree
10556cp_parser_cv_qualifier_seq_opt (parser)
10557 cp_parser *parser;
10558{
10559 tree cv_qualifiers = NULL_TREE;
10560
10561 while (true)
10562 {
10563 tree cv_qualifier;
10564
10565 /* Look for the next cv-qualifier. */
10566 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10567 /* If we didn't find one, we're done. */
10568 if (!cv_qualifier)
10569 break;
10570
10571 /* Add this cv-qualifier to the list. */
10572 cv_qualifiers
10573 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10574 }
10575
10576 /* We built up the list in reverse order. */
10577 return nreverse (cv_qualifiers);
10578}
10579
10580/* Parse an (optional) cv-qualifier.
10581
10582 cv-qualifier:
10583 const
10584 volatile
10585
10586 GNU Extension:
10587
10588 cv-qualifier:
10589 __restrict__ */
10590
10591static tree
10592cp_parser_cv_qualifier_opt (parser)
10593 cp_parser *parser;
10594{
10595 cp_token *token;
10596 tree cv_qualifier = NULL_TREE;
10597
10598 /* Peek at the next token. */
10599 token = cp_lexer_peek_token (parser->lexer);
10600 /* See if it's a cv-qualifier. */
10601 switch (token->keyword)
10602 {
10603 case RID_CONST:
10604 case RID_VOLATILE:
10605 case RID_RESTRICT:
10606 /* Save the value of the token. */
10607 cv_qualifier = token->value;
10608 /* Consume the token. */
10609 cp_lexer_consume_token (parser->lexer);
10610 break;
10611
10612 default:
10613 break;
10614 }
10615
10616 return cv_qualifier;
10617}
10618
10619/* Parse a declarator-id.
10620
10621 declarator-id:
10622 id-expression
10623 :: [opt] nested-name-specifier [opt] type-name
10624
10625 In the `id-expression' case, the value returned is as for
10626 cp_parser_id_expression if the id-expression was an unqualified-id.
10627 If the id-expression was a qualified-id, then a SCOPE_REF is
10628 returned. The first operand is the scope (either a NAMESPACE_DECL
10629 or TREE_TYPE), but the second is still just a representation of an
10630 unqualified-id. */
10631
10632static tree
10633cp_parser_declarator_id (parser)
10634 cp_parser *parser;
10635{
10636 tree id_expression;
10637
10638 /* The expression must be an id-expression. Assume that qualified
10639 names are the names of types so that:
10640
10641 template <class T>
10642 int S<T>::R::i = 3;
10643
10644 will work; we must treat `S<T>::R' as the name of a type.
10645 Similarly, assume that qualified names are templates, where
10646 required, so that:
10647
10648 template <class T>
10649 int S<T>::R<T>::i = 3;
10650
10651 will work, too. */
10652 id_expression = cp_parser_id_expression (parser,
10653 /*template_keyword_p=*/false,
10654 /*check_dependency_p=*/false,
10655 /*template_p=*/NULL);
10656 /* If the name was qualified, create a SCOPE_REF to represent
10657 that. */
10658 if (parser->scope)
10659 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10660
10661 return id_expression;
10662}
10663
10664/* Parse a type-id.
10665
10666 type-id:
10667 type-specifier-seq abstract-declarator [opt]
10668
10669 Returns the TYPE specified. */
10670
10671static tree
10672cp_parser_type_id (parser)
10673 cp_parser *parser;
10674{
10675 tree type_specifier_seq;
10676 tree abstract_declarator;
10677
10678 /* Parse the type-specifier-seq. */
10679 type_specifier_seq
10680 = cp_parser_type_specifier_seq (parser);
10681 if (type_specifier_seq == error_mark_node)
10682 return error_mark_node;
10683
10684 /* There might or might not be an abstract declarator. */
10685 cp_parser_parse_tentatively (parser);
10686 /* Look for the declarator. */
10687 abstract_declarator
62b8a44e 10688 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
a723baf1
MM
10689 /* Check to see if there really was a declarator. */
10690 if (!cp_parser_parse_definitely (parser))
10691 abstract_declarator = NULL_TREE;
10692
10693 return groktypename (build_tree_list (type_specifier_seq,
10694 abstract_declarator));
10695}
10696
10697/* Parse a type-specifier-seq.
10698
10699 type-specifier-seq:
10700 type-specifier type-specifier-seq [opt]
10701
10702 GNU extension:
10703
10704 type-specifier-seq:
10705 attributes type-specifier-seq [opt]
10706
10707 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10708 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10709
10710static tree
10711cp_parser_type_specifier_seq (parser)
10712 cp_parser *parser;
10713{
10714 bool seen_type_specifier = false;
10715 tree type_specifier_seq = NULL_TREE;
10716
10717 /* Parse the type-specifiers and attributes. */
10718 while (true)
10719 {
10720 tree type_specifier;
10721
10722 /* Check for attributes first. */
10723 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10724 {
10725 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10726 NULL_TREE,
10727 type_specifier_seq);
10728 continue;
10729 }
10730
10731 /* After the first type-specifier, others are optional. */
10732 if (seen_type_specifier)
10733 cp_parser_parse_tentatively (parser);
10734 /* Look for the type-specifier. */
10735 type_specifier = cp_parser_type_specifier (parser,
10736 CP_PARSER_FLAGS_NONE,
10737 /*is_friend=*/false,
10738 /*is_declaration=*/false,
10739 NULL,
10740 NULL);
10741 /* If the first type-specifier could not be found, this is not a
10742 type-specifier-seq at all. */
10743 if (!seen_type_specifier && type_specifier == error_mark_node)
10744 return error_mark_node;
10745 /* If subsequent type-specifiers could not be found, the
10746 type-specifier-seq is complete. */
10747 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10748 break;
10749
10750 /* Add the new type-specifier to the list. */
10751 type_specifier_seq
10752 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10753 seen_type_specifier = true;
10754 }
10755
10756 /* We built up the list in reverse order. */
10757 return nreverse (type_specifier_seq);
10758}
10759
10760/* Parse a parameter-declaration-clause.
10761
10762 parameter-declaration-clause:
10763 parameter-declaration-list [opt] ... [opt]
10764 parameter-declaration-list , ...
10765
10766 Returns a representation for the parameter declarations. Each node
10767 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10768 representation.) If the parameter-declaration-clause ends with an
10769 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10770 list. A return value of NULL_TREE indicates a
10771 parameter-declaration-clause consisting only of an ellipsis. */
10772
10773static tree
10774cp_parser_parameter_declaration_clause (parser)
10775 cp_parser *parser;
10776{
10777 tree parameters;
10778 cp_token *token;
10779 bool ellipsis_p;
10780
10781 /* Peek at the next token. */
10782 token = cp_lexer_peek_token (parser->lexer);
10783 /* Check for trivial parameter-declaration-clauses. */
10784 if (token->type == CPP_ELLIPSIS)
10785 {
10786 /* Consume the `...' token. */
10787 cp_lexer_consume_token (parser->lexer);
10788 return NULL_TREE;
10789 }
10790 else if (token->type == CPP_CLOSE_PAREN)
10791 /* There are no parameters. */
c73aecdf
DE
10792 {
10793#ifndef NO_IMPLICIT_EXTERN_C
10794 if (in_system_header && current_class_type == NULL
10795 && current_lang_name == lang_name_c)
10796 return NULL_TREE;
10797 else
10798#endif
10799 return void_list_node;
10800 }
a723baf1
MM
10801 /* Check for `(void)', too, which is a special case. */
10802 else if (token->keyword == RID_VOID
10803 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10804 == CPP_CLOSE_PAREN))
10805 {
10806 /* Consume the `void' token. */
10807 cp_lexer_consume_token (parser->lexer);
10808 /* There are no parameters. */
10809 return void_list_node;
10810 }
10811
10812 /* Parse the parameter-declaration-list. */
10813 parameters = cp_parser_parameter_declaration_list (parser);
10814 /* If a parse error occurred while parsing the
10815 parameter-declaration-list, then the entire
10816 parameter-declaration-clause is erroneous. */
10817 if (parameters == error_mark_node)
10818 return error_mark_node;
10819
10820 /* Peek at the next token. */
10821 token = cp_lexer_peek_token (parser->lexer);
10822 /* If it's a `,', the clause should terminate with an ellipsis. */
10823 if (token->type == CPP_COMMA)
10824 {
10825 /* Consume the `,'. */
10826 cp_lexer_consume_token (parser->lexer);
10827 /* Expect an ellipsis. */
10828 ellipsis_p
10829 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10830 }
10831 /* It might also be `...' if the optional trailing `,' was
10832 omitted. */
10833 else if (token->type == CPP_ELLIPSIS)
10834 {
10835 /* Consume the `...' token. */
10836 cp_lexer_consume_token (parser->lexer);
10837 /* And remember that we saw it. */
10838 ellipsis_p = true;
10839 }
10840 else
10841 ellipsis_p = false;
10842
10843 /* Finish the parameter list. */
10844 return finish_parmlist (parameters, ellipsis_p);
10845}
10846
10847/* Parse a parameter-declaration-list.
10848
10849 parameter-declaration-list:
10850 parameter-declaration
10851 parameter-declaration-list , parameter-declaration
10852
10853 Returns a representation of the parameter-declaration-list, as for
10854 cp_parser_parameter_declaration_clause. However, the
10855 `void_list_node' is never appended to the list. */
10856
10857static tree
10858cp_parser_parameter_declaration_list (parser)
10859 cp_parser *parser;
10860{
10861 tree parameters = NULL_TREE;
10862
10863 /* Look for more parameters. */
10864 while (true)
10865 {
10866 tree parameter;
10867 /* Parse the parameter. */
10868 parameter
ec194454
MM
10869 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10870
a723baf1
MM
10871 /* If a parse error ocurred parsing the parameter declaration,
10872 then the entire parameter-declaration-list is erroneous. */
10873 if (parameter == error_mark_node)
10874 {
10875 parameters = error_mark_node;
10876 break;
10877 }
10878 /* Add the new parameter to the list. */
10879 TREE_CHAIN (parameter) = parameters;
10880 parameters = parameter;
10881
10882 /* Peek at the next token. */
10883 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10884 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10885 /* The parameter-declaration-list is complete. */
10886 break;
10887 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10888 {
10889 cp_token *token;
10890
10891 /* Peek at the next token. */
10892 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10893 /* If it's an ellipsis, then the list is complete. */
10894 if (token->type == CPP_ELLIPSIS)
10895 break;
10896 /* Otherwise, there must be more parameters. Consume the
10897 `,'. */
10898 cp_lexer_consume_token (parser->lexer);
10899 }
10900 else
10901 {
10902 cp_parser_error (parser, "expected `,' or `...'");
10903 break;
10904 }
10905 }
10906
10907 /* We built up the list in reverse order; straighten it out now. */
10908 return nreverse (parameters);
10909}
10910
10911/* Parse a parameter declaration.
10912
10913 parameter-declaration:
10914 decl-specifier-seq declarator
10915 decl-specifier-seq declarator = assignment-expression
10916 decl-specifier-seq abstract-declarator [opt]
10917 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10918
ec194454
MM
10919 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10920 declares a template parameter. (In that case, a non-nested `>'
10921 token encountered during the parsing of the assignment-expression
10922 is not interpreted as a greater-than operator.)
a723baf1
MM
10923
10924 Returns a TREE_LIST representing the parameter-declaration. The
10925 TREE_VALUE is a representation of the decl-specifier-seq and
10926 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10927 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10928 TREE_VALUE represents the declarator. */
10929
10930static tree
ec194454
MM
10931cp_parser_parameter_declaration (cp_parser *parser,
10932 bool template_parm_p)
a723baf1
MM
10933{
10934 bool declares_class_or_enum;
ec194454 10935 bool greater_than_is_operator_p;
a723baf1
MM
10936 tree decl_specifiers;
10937 tree attributes;
10938 tree declarator;
10939 tree default_argument;
10940 tree parameter;
10941 cp_token *token;
10942 const char *saved_message;
10943
ec194454
MM
10944 /* In a template parameter, `>' is not an operator.
10945
10946 [temp.param]
10947
10948 When parsing a default template-argument for a non-type
10949 template-parameter, the first non-nested `>' is taken as the end
10950 of the template parameter-list rather than a greater-than
10951 operator. */
10952 greater_than_is_operator_p = !template_parm_p;
10953
a723baf1
MM
10954 /* Type definitions may not appear in parameter types. */
10955 saved_message = parser->type_definition_forbidden_message;
10956 parser->type_definition_forbidden_message
10957 = "types may not be defined in parameter types";
10958
10959 /* Parse the declaration-specifiers. */
10960 decl_specifiers
10961 = cp_parser_decl_specifier_seq (parser,
10962 CP_PARSER_FLAGS_NONE,
10963 &attributes,
10964 &declares_class_or_enum);
10965 /* If an error occurred, there's no reason to attempt to parse the
10966 rest of the declaration. */
10967 if (cp_parser_error_occurred (parser))
10968 {
10969 parser->type_definition_forbidden_message = saved_message;
10970 return error_mark_node;
10971 }
10972
10973 /* Peek at the next token. */
10974 token = cp_lexer_peek_token (parser->lexer);
10975 /* If the next token is a `)', `,', `=', `>', or `...', then there
10976 is no declarator. */
10977 if (token->type == CPP_CLOSE_PAREN
10978 || token->type == CPP_COMMA
10979 || token->type == CPP_EQ
10980 || token->type == CPP_ELLIPSIS
10981 || token->type == CPP_GREATER)
10982 declarator = NULL_TREE;
10983 /* Otherwise, there should be a declarator. */
10984 else
10985 {
10986 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10987 parser->default_arg_ok_p = false;
10988
a723baf1 10989 declarator = cp_parser_declarator (parser,
62b8a44e 10990 CP_PARSER_DECLARATOR_EITHER,
a723baf1 10991 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1 10992 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
10993 /* After the declarator, allow more attributes. */
10994 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
10995 }
10996
62b8a44e 10997 /* The restriction on defining new types applies only to the type
a723baf1
MM
10998 of the parameter, not to the default argument. */
10999 parser->type_definition_forbidden_message = saved_message;
11000
11001 /* If the next token is `=', then process a default argument. */
11002 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11003 {
11004 bool saved_greater_than_is_operator_p;
11005 /* Consume the `='. */
11006 cp_lexer_consume_token (parser->lexer);
11007
11008 /* If we are defining a class, then the tokens that make up the
11009 default argument must be saved and processed later. */
ec194454
MM
11010 if (!template_parm_p && at_class_scope_p ()
11011 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
11012 {
11013 unsigned depth = 0;
11014
11015 /* Create a DEFAULT_ARG to represented the unparsed default
11016 argument. */
11017 default_argument = make_node (DEFAULT_ARG);
11018 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11019
11020 /* Add tokens until we have processed the entire default
11021 argument. */
11022 while (true)
11023 {
11024 bool done = false;
11025 cp_token *token;
11026
11027 /* Peek at the next token. */
11028 token = cp_lexer_peek_token (parser->lexer);
11029 /* What we do depends on what token we have. */
11030 switch (token->type)
11031 {
11032 /* In valid code, a default argument must be
11033 immediately followed by a `,' `)', or `...'. */
11034 case CPP_COMMA:
11035 case CPP_CLOSE_PAREN:
11036 case CPP_ELLIPSIS:
11037 /* If we run into a non-nested `;', `}', or `]',
11038 then the code is invalid -- but the default
11039 argument is certainly over. */
11040 case CPP_SEMICOLON:
11041 case CPP_CLOSE_BRACE:
11042 case CPP_CLOSE_SQUARE:
11043 if (depth == 0)
11044 done = true;
11045 /* Update DEPTH, if necessary. */
11046 else if (token->type == CPP_CLOSE_PAREN
11047 || token->type == CPP_CLOSE_BRACE
11048 || token->type == CPP_CLOSE_SQUARE)
11049 --depth;
11050 break;
11051
11052 case CPP_OPEN_PAREN:
11053 case CPP_OPEN_SQUARE:
11054 case CPP_OPEN_BRACE:
11055 ++depth;
11056 break;
11057
11058 case CPP_GREATER:
11059 /* If we see a non-nested `>', and `>' is not an
11060 operator, then it marks the end of the default
11061 argument. */
11062 if (!depth && !greater_than_is_operator_p)
11063 done = true;
11064 break;
11065
11066 /* If we run out of tokens, issue an error message. */
11067 case CPP_EOF:
11068 error ("file ends in default argument");
11069 done = true;
11070 break;
11071
11072 case CPP_NAME:
11073 case CPP_SCOPE:
11074 /* In these cases, we should look for template-ids.
11075 For example, if the default argument is
11076 `X<int, double>()', we need to do name lookup to
11077 figure out whether or not `X' is a template; if
11078 so, the `,' does not end the deault argument.
11079
11080 That is not yet done. */
11081 break;
11082
11083 default:
11084 break;
11085 }
11086
11087 /* If we've reached the end, stop. */
11088 if (done)
11089 break;
11090
11091 /* Add the token to the token block. */
11092 token = cp_lexer_consume_token (parser->lexer);
11093 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11094 token);
11095 }
11096 }
11097 /* Outside of a class definition, we can just parse the
11098 assignment-expression. */
11099 else
11100 {
11101 bool saved_local_variables_forbidden_p;
11102
11103 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11104 set correctly. */
11105 saved_greater_than_is_operator_p
11106 = parser->greater_than_is_operator_p;
11107 parser->greater_than_is_operator_p = greater_than_is_operator_p;
11108 /* Local variable names (and the `this' keyword) may not
11109 appear in a default argument. */
11110 saved_local_variables_forbidden_p
11111 = parser->local_variables_forbidden_p;
11112 parser->local_variables_forbidden_p = true;
11113 /* Parse the assignment-expression. */
11114 default_argument = cp_parser_assignment_expression (parser);
11115 /* Restore saved state. */
11116 parser->greater_than_is_operator_p
11117 = saved_greater_than_is_operator_p;
11118 parser->local_variables_forbidden_p
11119 = saved_local_variables_forbidden_p;
11120 }
11121 if (!parser->default_arg_ok_p)
11122 {
11123 pedwarn ("default arguments are only permitted on functions");
11124 if (flag_pedantic_errors)
11125 default_argument = NULL_TREE;
11126 }
11127 }
11128 else
11129 default_argument = NULL_TREE;
11130
11131 /* Create the representation of the parameter. */
11132 if (attributes)
11133 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11134 parameter = build_tree_list (default_argument,
11135 build_tree_list (decl_specifiers,
11136 declarator));
11137
11138 return parameter;
11139}
11140
11141/* Parse a function-definition.
11142
11143 function-definition:
11144 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11145 function-body
11146 decl-specifier-seq [opt] declarator function-try-block
11147
11148 GNU Extension:
11149
11150 function-definition:
11151 __extension__ function-definition
11152
11153 Returns the FUNCTION_DECL for the function. If FRIEND_P is
11154 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
11155 be a `friend'. */
11156
11157static tree
11158cp_parser_function_definition (parser, friend_p)
11159 cp_parser *parser;
11160 bool *friend_p;
11161{
11162 tree decl_specifiers;
11163 tree attributes;
11164 tree declarator;
11165 tree fn;
11166 tree access_checks;
11167 cp_token *token;
11168 bool declares_class_or_enum;
11169 bool member_p;
11170 /* The saved value of the PEDANTIC flag. */
11171 int saved_pedantic;
11172
11173 /* Any pending qualification must be cleared by our caller. It is
11174 more robust to force the callers to clear PARSER->SCOPE than to
11175 do it here since if the qualification is in effect here, it might
11176 also end up in effect elsewhere that it is not intended. */
11177 my_friendly_assert (!parser->scope, 20010821);
11178
11179 /* Handle `__extension__'. */
11180 if (cp_parser_extension_opt (parser, &saved_pedantic))
11181 {
11182 /* Parse the function-definition. */
11183 fn = cp_parser_function_definition (parser, friend_p);
11184 /* Restore the PEDANTIC flag. */
11185 pedantic = saved_pedantic;
11186
11187 return fn;
11188 }
11189
11190 /* Check to see if this definition appears in a class-specifier. */
11191 member_p = (at_class_scope_p ()
11192 && TYPE_BEING_DEFINED (current_class_type));
11193 /* Defer access checks in the decl-specifier-seq until we know what
11194 function is being defined. There is no need to do this for the
11195 definition of member functions; we cannot be defining a member
11196 from another class. */
11197 if (!member_p)
11198 cp_parser_start_deferring_access_checks (parser);
11199 /* Parse the decl-specifier-seq. */
11200 decl_specifiers
11201 = cp_parser_decl_specifier_seq (parser,
11202 CP_PARSER_FLAGS_OPTIONAL,
11203 &attributes,
11204 &declares_class_or_enum);
11205 /* Figure out whether this declaration is a `friend'. */
11206 if (friend_p)
11207 *friend_p = cp_parser_friend_p (decl_specifiers);
11208
11209 /* Parse the declarator. */
62b8a44e 11210 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
11211 /*ctor_dtor_or_conv_p=*/NULL);
11212
11213 /* Gather up any access checks that occurred. */
11214 if (!member_p)
11215 access_checks = cp_parser_stop_deferring_access_checks (parser);
11216 else
11217 access_checks = NULL_TREE;
11218
11219 /* If something has already gone wrong, we may as well stop now. */
11220 if (declarator == error_mark_node)
11221 {
11222 /* Skip to the end of the function, or if this wasn't anything
11223 like a function-definition, to a `;' in the hopes of finding
11224 a sensible place from which to continue parsing. */
11225 cp_parser_skip_to_end_of_block_or_statement (parser);
11226 return error_mark_node;
11227 }
11228
11229 /* The next character should be a `{' (for a simple function
11230 definition), a `:' (for a ctor-initializer), or `try' (for a
11231 function-try block). */
11232 token = cp_lexer_peek_token (parser->lexer);
11233 if (!cp_parser_token_starts_function_definition_p (token))
11234 {
11235 /* Issue the error-message. */
11236 cp_parser_error (parser, "expected function-definition");
11237 /* Skip to the next `;'. */
11238 cp_parser_skip_to_end_of_block_or_statement (parser);
11239
11240 return error_mark_node;
11241 }
11242
11243 /* If we are in a class scope, then we must handle
11244 function-definitions specially. In particular, we save away the
11245 tokens that make up the function body, and parse them again
11246 later, in order to handle code like:
11247
11248 struct S {
11249 int f () { return i; }
11250 int i;
11251 };
11252
11253 Here, we cannot parse the body of `f' until after we have seen
11254 the declaration of `i'. */
11255 if (member_p)
11256 {
11257 cp_token_cache *cache;
11258
11259 /* Create the function-declaration. */
11260 fn = start_method (decl_specifiers, declarator, attributes);
11261 /* If something went badly wrong, bail out now. */
11262 if (fn == error_mark_node)
11263 {
11264 /* If there's a function-body, skip it. */
11265 if (cp_parser_token_starts_function_definition_p
11266 (cp_lexer_peek_token (parser->lexer)))
11267 cp_parser_skip_to_end_of_block_or_statement (parser);
11268 return error_mark_node;
11269 }
11270
11271 /* Create a token cache. */
11272 cache = cp_token_cache_new ();
11273 /* Save away the tokens that make up the body of the
11274 function. */
11275 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11276 /* Handle function try blocks. */
11277 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11278 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11279
11280 /* Save away the inline definition; we will process it when the
11281 class is complete. */
11282 DECL_PENDING_INLINE_INFO (fn) = cache;
11283 DECL_PENDING_INLINE_P (fn) = 1;
11284
11285 /* We're done with the inline definition. */
11286 finish_method (fn);
11287
11288 /* Add FN to the queue of functions to be parsed later. */
11289 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 11290 = tree_cons (NULL_TREE, fn,
a723baf1
MM
11291 TREE_VALUE (parser->unparsed_functions_queues));
11292
11293 return fn;
11294 }
11295
11296 /* Check that the number of template-parameter-lists is OK. */
11297 if (!cp_parser_check_declarator_template_parameters (parser,
11298 declarator))
11299 {
11300 cp_parser_skip_to_end_of_block_or_statement (parser);
11301 return error_mark_node;
11302 }
11303
11304 return (cp_parser_function_definition_from_specifiers_and_declarator
11305 (parser, decl_specifiers, attributes, declarator, access_checks));
11306}
11307
11308/* Parse a function-body.
11309
11310 function-body:
11311 compound_statement */
11312
11313static void
11314cp_parser_function_body (cp_parser *parser)
11315{
11316 cp_parser_compound_statement (parser);
11317}
11318
11319/* Parse a ctor-initializer-opt followed by a function-body. Return
11320 true if a ctor-initializer was present. */
11321
11322static bool
11323cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11324{
11325 tree body;
11326 bool ctor_initializer_p;
11327
11328 /* Begin the function body. */
11329 body = begin_function_body ();
11330 /* Parse the optional ctor-initializer. */
11331 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11332 /* Parse the function-body. */
11333 cp_parser_function_body (parser);
11334 /* Finish the function body. */
11335 finish_function_body (body);
11336
11337 return ctor_initializer_p;
11338}
11339
11340/* Parse an initializer.
11341
11342 initializer:
11343 = initializer-clause
11344 ( expression-list )
11345
11346 Returns a expression representing the initializer. If no
11347 initializer is present, NULL_TREE is returned.
11348
11349 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11350 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11351 set to FALSE if there is no initializer present. */
11352
11353static tree
11354cp_parser_initializer (parser, is_parenthesized_init)
11355 cp_parser *parser;
11356 bool *is_parenthesized_init;
11357{
11358 cp_token *token;
11359 tree init;
11360
11361 /* Peek at the next token. */
11362 token = cp_lexer_peek_token (parser->lexer);
11363
11364 /* Let our caller know whether or not this initializer was
11365 parenthesized. */
11366 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11367
11368 if (token->type == CPP_EQ)
11369 {
11370 /* Consume the `='. */
11371 cp_lexer_consume_token (parser->lexer);
11372 /* Parse the initializer-clause. */
11373 init = cp_parser_initializer_clause (parser);
11374 }
11375 else if (token->type == CPP_OPEN_PAREN)
11376 {
11377 /* Consume the `('. */
11378 cp_lexer_consume_token (parser->lexer);
11379 /* Parse the expression-list. */
11380 init = cp_parser_expression_list (parser);
11381 /* Consume the `)' token. */
11382 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11383 cp_parser_skip_to_closing_parenthesis (parser);
11384 }
11385 else
11386 {
11387 /* Anything else is an error. */
11388 cp_parser_error (parser, "expected initializer");
11389 init = error_mark_node;
11390 }
11391
11392 return init;
11393}
11394
11395/* Parse an initializer-clause.
11396
11397 initializer-clause:
11398 assignment-expression
11399 { initializer-list , [opt] }
11400 { }
11401
11402 Returns an expression representing the initializer.
11403
11404 If the `assignment-expression' production is used the value
11405 returned is simply a reprsentation for the expression.
11406
11407 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11408 the elements of the initializer-list (or NULL_TREE, if the last
11409 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11410 NULL_TREE. There is no way to detect whether or not the optional
11411 trailing `,' was provided. */
11412
11413static tree
11414cp_parser_initializer_clause (parser)
11415 cp_parser *parser;
11416{
11417 tree initializer;
11418
11419 /* If it is not a `{', then we are looking at an
11420 assignment-expression. */
11421 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11422 initializer = cp_parser_assignment_expression (parser);
11423 else
11424 {
11425 /* Consume the `{' token. */
11426 cp_lexer_consume_token (parser->lexer);
11427 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11428 initializer = make_node (CONSTRUCTOR);
11429 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11430 necessary, but check_initializer depends upon it, for
11431 now. */
11432 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11433 /* If it's not a `}', then there is a non-trivial initializer. */
11434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11435 {
11436 /* Parse the initializer list. */
11437 CONSTRUCTOR_ELTS (initializer)
11438 = cp_parser_initializer_list (parser);
11439 /* A trailing `,' token is allowed. */
11440 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11441 cp_lexer_consume_token (parser->lexer);
11442 }
11443
11444 /* Now, there should be a trailing `}'. */
11445 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11446 }
11447
11448 return initializer;
11449}
11450
11451/* Parse an initializer-list.
11452
11453 initializer-list:
11454 initializer-clause
11455 initializer-list , initializer-clause
11456
11457 GNU Extension:
11458
11459 initializer-list:
11460 identifier : initializer-clause
11461 initializer-list, identifier : initializer-clause
11462
11463 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11464 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11465 IDENTIFIER_NODE naming the field to initialize. */
11466
11467static tree
11468cp_parser_initializer_list (parser)
11469 cp_parser *parser;
11470{
11471 tree initializers = NULL_TREE;
11472
11473 /* Parse the rest of the list. */
11474 while (true)
11475 {
11476 cp_token *token;
11477 tree identifier;
11478 tree initializer;
11479
11480 /* If the next token is an identifier and the following one is a
11481 colon, we are looking at the GNU designated-initializer
11482 syntax. */
11483 if (cp_parser_allow_gnu_extensions_p (parser)
11484 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11485 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11486 {
11487 /* Consume the identifier. */
11488 identifier = cp_lexer_consume_token (parser->lexer)->value;
11489 /* Consume the `:'. */
11490 cp_lexer_consume_token (parser->lexer);
11491 }
11492 else
11493 identifier = NULL_TREE;
11494
11495 /* Parse the initializer. */
11496 initializer = cp_parser_initializer_clause (parser);
11497
11498 /* Add it to the list. */
11499 initializers = tree_cons (identifier, initializer, initializers);
11500
11501 /* If the next token is not a comma, we have reached the end of
11502 the list. */
11503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11504 break;
11505
11506 /* Peek at the next token. */
11507 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11508 /* If the next token is a `}', then we're still done. An
11509 initializer-clause can have a trailing `,' after the
11510 initializer-list and before the closing `}'. */
11511 if (token->type == CPP_CLOSE_BRACE)
11512 break;
11513
11514 /* Consume the `,' token. */
11515 cp_lexer_consume_token (parser->lexer);
11516 }
11517
11518 /* The initializers were built up in reverse order, so we need to
11519 reverse them now. */
11520 return nreverse (initializers);
11521}
11522
11523/* Classes [gram.class] */
11524
11525/* Parse a class-name.
11526
11527 class-name:
11528 identifier
11529 template-id
11530
11531 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11532 to indicate that names looked up in dependent types should be
11533 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11534 keyword has been used to indicate that the name that appears next
11535 is a template. TYPE_P is true iff the next name should be treated
11536 as class-name, even if it is declared to be some other kind of name
11537 as well. The accessibility of the class-name is checked iff
11538 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11539 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11540 is the class being defined in a class-head.
11541
11542 Returns the TYPE_DECL representing the class. */
11543
11544static tree
11545cp_parser_class_name (cp_parser *parser,
11546 bool typename_keyword_p,
11547 bool template_keyword_p,
11548 bool type_p,
11549 bool check_access_p,
11550 bool check_dependency_p,
11551 bool class_head_p)
11552{
11553 tree decl;
11554 tree scope;
11555 bool typename_p;
e5976695
MM
11556 cp_token *token;
11557
11558 /* All class-names start with an identifier. */
11559 token = cp_lexer_peek_token (parser->lexer);
11560 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11561 {
11562 cp_parser_error (parser, "expected class-name");
11563 return error_mark_node;
11564 }
11565
a723baf1
MM
11566 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11567 to a template-id, so we save it here. */
11568 scope = parser->scope;
11569 /* Any name names a type if we're following the `typename' keyword
11570 in a qualified name where the enclosing scope is type-dependent. */
11571 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11572 && cp_parser_dependent_type_p (scope));
e5976695
MM
11573 /* Handle the common case (an identifier, but not a template-id)
11574 efficiently. */
11575 if (token->type == CPP_NAME
11576 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
a723baf1 11577 {
a723baf1
MM
11578 tree identifier;
11579
11580 /* Look for the identifier. */
11581 identifier = cp_parser_identifier (parser);
11582 /* If the next token isn't an identifier, we are certainly not
11583 looking at a class-name. */
11584 if (identifier == error_mark_node)
11585 decl = error_mark_node;
11586 /* If we know this is a type-name, there's no need to look it
11587 up. */
11588 else if (typename_p)
11589 decl = identifier;
11590 else
11591 {
11592 /* If the next token is a `::', then the name must be a type
11593 name.
11594
11595 [basic.lookup.qual]
11596
11597 During the lookup for a name preceding the :: scope
11598 resolution operator, object, function, and enumerator
11599 names are ignored. */
11600 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11601 type_p = true;
11602 /* Look up the name. */
11603 decl = cp_parser_lookup_name (parser, identifier,
11604 check_access_p,
11605 type_p,
eea9800f 11606 /*is_namespace=*/false,
a723baf1
MM
11607 check_dependency_p);
11608 }
11609 }
e5976695
MM
11610 else
11611 {
11612 /* Try a template-id. */
11613 decl = cp_parser_template_id (parser, template_keyword_p,
11614 check_dependency_p);
11615 if (decl == error_mark_node)
11616 return error_mark_node;
11617 }
a723baf1
MM
11618
11619 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11620
11621 /* If this is a typename, create a TYPENAME_TYPE. */
11622 if (typename_p && decl != error_mark_node)
11623 decl = TYPE_NAME (make_typename_type (scope, decl,
11624 /*complain=*/1));
11625
11626 /* Check to see that it is really the name of a class. */
11627 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11628 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11629 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11630 /* Situations like this:
11631
11632 template <typename T> struct A {
11633 typename T::template X<int>::I i;
11634 };
11635
11636 are problematic. Is `T::template X<int>' a class-name? The
11637 standard does not seem to be definitive, but there is no other
11638 valid interpretation of the following `::'. Therefore, those
11639 names are considered class-names. */
11640 decl = TYPE_NAME (make_typename_type (scope, decl,
11641 tf_error | tf_parsing));
11642 else if (decl == error_mark_node
11643 || TREE_CODE (decl) != TYPE_DECL
11644 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11645 {
11646 cp_parser_error (parser, "expected class-name");
11647 return error_mark_node;
11648 }
11649
11650 return decl;
11651}
11652
11653/* Parse a class-specifier.
11654
11655 class-specifier:
11656 class-head { member-specification [opt] }
11657
11658 Returns the TREE_TYPE representing the class. */
11659
11660static tree
11661cp_parser_class_specifier (parser)
11662 cp_parser *parser;
11663{
11664 cp_token *token;
11665 tree type;
11666 tree attributes = NULL_TREE;
11667 int has_trailing_semicolon;
11668 bool nested_name_specifier_p;
11669 bool deferring_access_checks_p;
11670 tree saved_access_checks;
11671 unsigned saved_num_template_parameter_lists;
11672
11673 /* Parse the class-head. */
11674 type = cp_parser_class_head (parser,
11675 &nested_name_specifier_p,
11676 &deferring_access_checks_p,
11677 &saved_access_checks);
11678 /* If the class-head was a semantic disaster, skip the entire body
11679 of the class. */
11680 if (!type)
11681 {
11682 cp_parser_skip_to_end_of_block_or_statement (parser);
11683 return error_mark_node;
11684 }
11685 /* Look for the `{'. */
11686 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11687 return error_mark_node;
11688 /* Issue an error message if type-definitions are forbidden here. */
11689 cp_parser_check_type_definition (parser);
11690 /* Remember that we are defining one more class. */
11691 ++parser->num_classes_being_defined;
11692 /* Inside the class, surrounding template-parameter-lists do not
11693 apply. */
11694 saved_num_template_parameter_lists
11695 = parser->num_template_parameter_lists;
11696 parser->num_template_parameter_lists = 0;
11697 /* Start the class. */
11698 type = begin_class_definition (type);
11699 if (type == error_mark_node)
11700 /* If the type is erroneous, skip the entire body of the class. */
11701 cp_parser_skip_to_closing_brace (parser);
11702 else
11703 /* Parse the member-specification. */
11704 cp_parser_member_specification_opt (parser);
11705 /* Look for the trailing `}'. */
11706 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11707 /* We get better error messages by noticing a common problem: a
11708 missing trailing `;'. */
11709 token = cp_lexer_peek_token (parser->lexer);
11710 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11711 /* Look for attributes to apply to this class. */
11712 if (cp_parser_allow_gnu_extensions_p (parser))
11713 attributes = cp_parser_attributes_opt (parser);
11714 /* Finish the class definition. */
11715 type = finish_class_definition (type,
11716 attributes,
11717 has_trailing_semicolon,
11718 nested_name_specifier_p);
11719 /* If this class is not itself within the scope of another class,
11720 then we need to parse the bodies of all of the queued function
11721 definitions. Note that the queued functions defined in a class
11722 are not always processed immediately following the
11723 class-specifier for that class. Consider:
11724
11725 struct A {
11726 struct B { void f() { sizeof (A); } };
11727 };
11728
11729 If `f' were processed before the processing of `A' were
11730 completed, there would be no way to compute the size of `A'.
11731 Note that the nesting we are interested in here is lexical --
11732 not the semantic nesting given by TYPE_CONTEXT. In particular,
11733 for:
11734
11735 struct A { struct B; };
11736 struct A::B { void f() { } };
11737
11738 there is no need to delay the parsing of `A::B::f'. */
11739 if (--parser->num_classes_being_defined == 0)
11740 {
11741 tree last_scope = NULL_TREE;
8218bd34
MM
11742 tree queue_entry;
11743 tree fn;
a723baf1 11744
a723baf1
MM
11745 /* Reverse the queue, so that we process it in the order the
11746 functions were declared. */
11747 TREE_VALUE (parser->unparsed_functions_queues)
11748 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
8218bd34
MM
11749 /* In a first pass, parse default arguments to the functions.
11750 Then, in a second pass, parse the bodies of the functions.
11751 This two-phased approach handles cases like:
11752
11753 struct S {
11754 void f() { g(); }
11755 void g(int i = 3);
11756 };
11757
11758 */
11759 for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11760 queue_entry;
11761 queue_entry = TREE_CHAIN (queue_entry))
11762 {
11763 fn = TREE_VALUE (queue_entry);
11764 if (DECL_FUNCTION_TEMPLATE_P (fn))
11765 fn = DECL_TEMPLATE_RESULT (fn);
11766 /* Make sure that any template parameters are in scope. */
11767 maybe_begin_member_template_processing (fn);
11768 /* If there are default arguments that have not yet been processed,
11769 take care of them now. */
11770 cp_parser_late_parsing_default_args (parser, fn);
11771 /* Remove any template parameters from the symbol table. */
11772 maybe_end_member_template_processing ();
11773 }
11774 /* Now parse the body of the functions. */
a723baf1
MM
11775 while (TREE_VALUE (parser->unparsed_functions_queues))
11776
11777 {
a723baf1
MM
11778 /* Figure out which function we need to process. */
11779 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
a723baf1
MM
11780 fn = TREE_VALUE (queue_entry);
11781
11782 /* Parse the function. */
11783 cp_parser_late_parsing_for_member (parser, fn);
11784
11785 TREE_VALUE (parser->unparsed_functions_queues)
11786 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11787 }
11788
11789 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11790 more time than we have popped, so me must pop here. */
11791 if (last_scope)
11792 pop_scope (last_scope);
11793 }
11794
11795 /* Put back any saved access checks. */
11796 if (deferring_access_checks_p)
11797 {
11798 cp_parser_start_deferring_access_checks (parser);
11799 parser->context->deferred_access_checks = saved_access_checks;
11800 }
11801
11802 /* Restore the count of active template-parameter-lists. */
11803 parser->num_template_parameter_lists
11804 = saved_num_template_parameter_lists;
11805
11806 return type;
11807}
11808
11809/* Parse a class-head.
11810
11811 class-head:
11812 class-key identifier [opt] base-clause [opt]
11813 class-key nested-name-specifier identifier base-clause [opt]
11814 class-key nested-name-specifier [opt] template-id
11815 base-clause [opt]
11816
11817 GNU Extensions:
11818 class-key attributes identifier [opt] base-clause [opt]
11819 class-key attributes nested-name-specifier identifier base-clause [opt]
11820 class-key attributes nested-name-specifier [opt] template-id
11821 base-clause [opt]
11822
11823 Returns the TYPE of the indicated class. Sets
11824 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11825 involving a nested-name-specifier was used, and FALSE otherwise.
11826 Sets *DEFERRING_ACCESS_CHECKS_P to TRUE iff we were deferring
11827 access checks before this class-head. In that case,
11828 *SAVED_ACCESS_CHECKS is set to the current list of deferred access
11829 checks.
11830
11831 Returns NULL_TREE if the class-head is syntactically valid, but
11832 semantically invalid in a way that means we should skip the entire
11833 body of the class. */
11834
11835static tree
11836cp_parser_class_head (parser,
11837 nested_name_specifier_p,
11838 deferring_access_checks_p,
11839 saved_access_checks)
11840 cp_parser *parser;
11841 bool *nested_name_specifier_p;
11842 bool *deferring_access_checks_p;
11843 tree *saved_access_checks;
11844{
11845 cp_token *token;
11846 tree nested_name_specifier;
11847 enum tag_types class_key;
11848 tree id = NULL_TREE;
11849 tree type = NULL_TREE;
11850 tree attributes;
11851 bool template_id_p = false;
11852 bool qualified_p = false;
11853 bool invalid_nested_name_p = false;
11854 unsigned num_templates;
11855
11856 /* Assume no nested-name-specifier will be present. */
11857 *nested_name_specifier_p = false;
11858 /* Assume no template parameter lists will be used in defining the
11859 type. */
11860 num_templates = 0;
11861
11862 /* Look for the class-key. */
11863 class_key = cp_parser_class_key (parser);
11864 if (class_key == none_type)
11865 return error_mark_node;
11866
11867 /* Parse the attributes. */
11868 attributes = cp_parser_attributes_opt (parser);
11869
11870 /* If the next token is `::', that is invalid -- but sometimes
11871 people do try to write:
11872
11873 struct ::S {};
11874
11875 Handle this gracefully by accepting the extra qualifier, and then
11876 issuing an error about it later if this really is a
2050a1bb 11877 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11878 specifier, remain silent. */
11879 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11880 qualified_p = true;
11881
11882 /* Determine the name of the class. Begin by looking for an
11883 optional nested-name-specifier. */
11884 nested_name_specifier
11885 = cp_parser_nested_name_specifier_opt (parser,
11886 /*typename_keyword_p=*/false,
11887 /*check_dependency_p=*/true,
11888 /*type_p=*/false);
11889 /* If there was a nested-name-specifier, then there *must* be an
11890 identifier. */
11891 if (nested_name_specifier)
11892 {
11893 /* Although the grammar says `identifier', it really means
11894 `class-name' or `template-name'. You are only allowed to
11895 define a class that has already been declared with this
11896 syntax.
11897
11898 The proposed resolution for Core Issue 180 says that whever
11899 you see `class T::X' you should treat `X' as a type-name.
11900
11901 It is OK to define an inaccessible class; for example:
11902
11903 class A { class B; };
11904 class A::B {};
11905
11906 So, we ask cp_parser_class_name not to check accessibility.
11907
11908 We do not know if we will see a class-name, or a
11909 template-name. We look for a class-name first, in case the
11910 class-name is a template-id; if we looked for the
11911 template-name first we would stop after the template-name. */
11912 cp_parser_parse_tentatively (parser);
11913 type = cp_parser_class_name (parser,
11914 /*typename_keyword_p=*/false,
11915 /*template_keyword_p=*/false,
11916 /*type_p=*/true,
11917 /*check_access_p=*/false,
11918 /*check_dependency_p=*/false,
11919 /*class_head_p=*/true);
11920 /* If that didn't work, ignore the nested-name-specifier. */
11921 if (!cp_parser_parse_definitely (parser))
11922 {
11923 invalid_nested_name_p = true;
11924 id = cp_parser_identifier (parser);
11925 if (id == error_mark_node)
11926 id = NULL_TREE;
11927 }
11928 /* If we could not find a corresponding TYPE, treat this
11929 declaration like an unqualified declaration. */
11930 if (type == error_mark_node)
11931 nested_name_specifier = NULL_TREE;
11932 /* Otherwise, count the number of templates used in TYPE and its
11933 containing scopes. */
11934 else
11935 {
11936 tree scope;
11937
11938 for (scope = TREE_TYPE (type);
11939 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11940 scope = (TYPE_P (scope)
11941 ? TYPE_CONTEXT (scope)
11942 : DECL_CONTEXT (scope)))
11943 if (TYPE_P (scope)
11944 && CLASS_TYPE_P (scope)
11945 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
11946 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11947 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
11948 ++num_templates;
11949 }
11950 }
11951 /* Otherwise, the identifier is optional. */
11952 else
11953 {
11954 /* We don't know whether what comes next is a template-id,
11955 an identifier, or nothing at all. */
11956 cp_parser_parse_tentatively (parser);
11957 /* Check for a template-id. */
11958 id = cp_parser_template_id (parser,
11959 /*template_keyword_p=*/false,
11960 /*check_dependency_p=*/true);
11961 /* If that didn't work, it could still be an identifier. */
11962 if (!cp_parser_parse_definitely (parser))
11963 {
11964 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11965 id = cp_parser_identifier (parser);
11966 else
11967 id = NULL_TREE;
11968 }
11969 else
11970 {
11971 template_id_p = true;
11972 ++num_templates;
11973 }
11974 }
11975
11976 /* If it's not a `:' or a `{' then we can't really be looking at a
11977 class-head, since a class-head only appears as part of a
11978 class-specifier. We have to detect this situation before calling
11979 xref_tag, since that has irreversible side-effects. */
11980 if (!cp_parser_next_token_starts_class_definition_p (parser))
11981 {
11982 cp_parser_error (parser, "expected `{' or `:'");
11983 return error_mark_node;
11984 }
11985
11986 /* At this point, we're going ahead with the class-specifier, even
11987 if some other problem occurs. */
11988 cp_parser_commit_to_tentative_parse (parser);
11989 /* Issue the error about the overly-qualified name now. */
11990 if (qualified_p)
11991 cp_parser_error (parser,
11992 "global qualification of class name is invalid");
11993 else if (invalid_nested_name_p)
11994 cp_parser_error (parser,
11995 "qualified name does not name a class");
11996 /* Make sure that the right number of template parameters were
11997 present. */
11998 if (!cp_parser_check_template_parameters (parser, num_templates))
11999 /* If something went wrong, there is no point in even trying to
12000 process the class-definition. */
12001 return NULL_TREE;
12002
12003 /* We do not need to defer access checks for entities declared
12004 within the class. But, we do need to save any access checks that
12005 are currently deferred and restore them later, in case we are in
12006 the middle of something else. */
12007 *deferring_access_checks_p = parser->context->deferring_access_checks_p;
12008 if (*deferring_access_checks_p)
12009 *saved_access_checks = cp_parser_stop_deferring_access_checks (parser);
12010
12011 /* Look up the type. */
12012 if (template_id_p)
12013 {
12014 type = TREE_TYPE (id);
12015 maybe_process_partial_specialization (type);
12016 }
12017 else if (!nested_name_specifier)
12018 {
12019 /* If the class was unnamed, create a dummy name. */
12020 if (!id)
12021 id = make_anon_name ();
12022 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
12023 }
12024 else
12025 {
848eed92 12026 bool new_type_p;
a723baf1
MM
12027 tree class_type;
12028
12029 /* Given:
12030
12031 template <typename T> struct S { struct T };
12032 template <typename T> struct S::T { };
12033
12034 we will get a TYPENAME_TYPE when processing the definition of
12035 `S::T'. We need to resolve it to the actual type before we
12036 try to define it. */
12037 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12038 {
12039 type = cp_parser_resolve_typename_type (parser, TREE_TYPE (type));
12040 if (type != error_mark_node)
12041 type = TYPE_NAME (type);
12042 }
12043
12044 maybe_process_partial_specialization (TREE_TYPE (type));
12045 class_type = current_class_type;
12046 type = TREE_TYPE (handle_class_head (class_key,
12047 nested_name_specifier,
12048 type,
12049 attributes,
848eed92 12050 /*defn_p=*/true,
a723baf1
MM
12051 &new_type_p));
12052 if (type != error_mark_node)
12053 {
12054 if (!class_type && TYPE_CONTEXT (type))
12055 *nested_name_specifier_p = true;
12056 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
12057 class_type))
12058 *nested_name_specifier_p = true;
12059 }
12060 }
12061 /* Indicate whether this class was declared as a `class' or as a
12062 `struct'. */
12063 if (TREE_CODE (type) == RECORD_TYPE)
12064 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12065 cp_parser_check_class_key (class_key, type);
12066
12067 /* Enter the scope containing the class; the names of base classes
12068 should be looked up in that context. For example, given:
12069
12070 struct A { struct B {}; struct C; };
12071 struct A::C : B {};
12072
12073 is valid. */
12074 if (nested_name_specifier)
12075 push_scope (nested_name_specifier);
12076 /* Now, look for the base-clause. */
12077 token = cp_lexer_peek_token (parser->lexer);
12078 if (token->type == CPP_COLON)
12079 {
12080 tree bases;
12081
12082 /* Get the list of base-classes. */
12083 bases = cp_parser_base_clause (parser);
12084 /* Process them. */
12085 xref_basetypes (type, bases);
12086 }
12087 /* Leave the scope given by the nested-name-specifier. We will
12088 enter the class scope itself while processing the members. */
12089 if (nested_name_specifier)
12090 pop_scope (nested_name_specifier);
12091
12092 return type;
12093}
12094
12095/* Parse a class-key.
12096
12097 class-key:
12098 class
12099 struct
12100 union
12101
12102 Returns the kind of class-key specified, or none_type to indicate
12103 error. */
12104
12105static enum tag_types
12106cp_parser_class_key (parser)
12107 cp_parser *parser;
12108{
12109 cp_token *token;
12110 enum tag_types tag_type;
12111
12112 /* Look for the class-key. */
12113 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12114 if (!token)
12115 return none_type;
12116
12117 /* Check to see if the TOKEN is a class-key. */
12118 tag_type = cp_parser_token_is_class_key (token);
12119 if (!tag_type)
12120 cp_parser_error (parser, "expected class-key");
12121 return tag_type;
12122}
12123
12124/* Parse an (optional) member-specification.
12125
12126 member-specification:
12127 member-declaration member-specification [opt]
12128 access-specifier : member-specification [opt] */
12129
12130static void
12131cp_parser_member_specification_opt (parser)
12132 cp_parser *parser;
12133{
12134 while (true)
12135 {
12136 cp_token *token;
12137 enum rid keyword;
12138
12139 /* Peek at the next token. */
12140 token = cp_lexer_peek_token (parser->lexer);
12141 /* If it's a `}', or EOF then we've seen all the members. */
12142 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12143 break;
12144
12145 /* See if this token is a keyword. */
12146 keyword = token->keyword;
12147 switch (keyword)
12148 {
12149 case RID_PUBLIC:
12150 case RID_PROTECTED:
12151 case RID_PRIVATE:
12152 /* Consume the access-specifier. */
12153 cp_lexer_consume_token (parser->lexer);
12154 /* Remember which access-specifier is active. */
12155 current_access_specifier = token->value;
12156 /* Look for the `:'. */
12157 cp_parser_require (parser, CPP_COLON, "`:'");
12158 break;
12159
12160 default:
12161 /* Otherwise, the next construction must be a
12162 member-declaration. */
12163 cp_parser_member_declaration (parser);
12164 reset_type_access_control ();
12165 }
12166 }
12167}
12168
12169/* Parse a member-declaration.
12170
12171 member-declaration:
12172 decl-specifier-seq [opt] member-declarator-list [opt] ;
12173 function-definition ; [opt]
12174 :: [opt] nested-name-specifier template [opt] unqualified-id ;
12175 using-declaration
12176 template-declaration
12177
12178 member-declarator-list:
12179 member-declarator
12180 member-declarator-list , member-declarator
12181
12182 member-declarator:
12183 declarator pure-specifier [opt]
12184 declarator constant-initializer [opt]
12185 identifier [opt] : constant-expression
12186
12187 GNU Extensions:
12188
12189 member-declaration:
12190 __extension__ member-declaration
12191
12192 member-declarator:
12193 declarator attributes [opt] pure-specifier [opt]
12194 declarator attributes [opt] constant-initializer [opt]
12195 identifier [opt] attributes [opt] : constant-expression */
12196
12197static void
12198cp_parser_member_declaration (parser)
12199 cp_parser *parser;
12200{
12201 tree decl_specifiers;
12202 tree prefix_attributes;
12203 tree decl;
12204 bool declares_class_or_enum;
12205 bool friend_p;
12206 cp_token *token;
12207 int saved_pedantic;
12208
12209 /* Check for the `__extension__' keyword. */
12210 if (cp_parser_extension_opt (parser, &saved_pedantic))
12211 {
12212 /* Recurse. */
12213 cp_parser_member_declaration (parser);
12214 /* Restore the old value of the PEDANTIC flag. */
12215 pedantic = saved_pedantic;
12216
12217 return;
12218 }
12219
12220 /* Check for a template-declaration. */
12221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12222 {
12223 /* Parse the template-declaration. */
12224 cp_parser_template_declaration (parser, /*member_p=*/true);
12225
12226 return;
12227 }
12228
12229 /* Check for a using-declaration. */
12230 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12231 {
12232 /* Parse the using-declaration. */
12233 cp_parser_using_declaration (parser);
12234
12235 return;
12236 }
12237
12238 /* We can't tell whether we're looking at a declaration or a
12239 function-definition. */
12240 cp_parser_parse_tentatively (parser);
12241
12242 /* Parse the decl-specifier-seq. */
12243 decl_specifiers
12244 = cp_parser_decl_specifier_seq (parser,
12245 CP_PARSER_FLAGS_OPTIONAL,
12246 &prefix_attributes,
12247 &declares_class_or_enum);
12248 /* If there is no declarator, then the decl-specifier-seq should
12249 specify a type. */
12250 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12251 {
12252 /* If there was no decl-specifier-seq, and the next token is a
12253 `;', then we have something like:
12254
12255 struct S { ; };
12256
12257 [class.mem]
12258
12259 Each member-declaration shall declare at least one member
12260 name of the class. */
12261 if (!decl_specifiers)
12262 {
12263 if (pedantic)
12264 pedwarn ("extra semicolon");
12265 }
12266 else
12267 {
12268 tree type;
12269
12270 /* See if this declaration is a friend. */
12271 friend_p = cp_parser_friend_p (decl_specifiers);
12272 /* If there were decl-specifiers, check to see if there was
12273 a class-declaration. */
12274 type = check_tag_decl (decl_specifiers);
12275 /* Nested classes have already been added to the class, but
12276 a `friend' needs to be explicitly registered. */
12277 if (friend_p)
12278 {
12279 /* If the `friend' keyword was present, the friend must
12280 be introduced with a class-key. */
12281 if (!declares_class_or_enum)
12282 error ("a class-key must be used when declaring a friend");
12283 /* In this case:
12284
12285 template <typename T> struct A {
12286 friend struct A<T>::B;
12287 };
12288
12289 A<T>::B will be represented by a TYPENAME_TYPE, and
12290 therefore not recognized by check_tag_decl. */
12291 if (!type)
12292 {
12293 tree specifier;
12294
12295 for (specifier = decl_specifiers;
12296 specifier;
12297 specifier = TREE_CHAIN (specifier))
12298 {
12299 tree s = TREE_VALUE (specifier);
12300
12301 if (TREE_CODE (s) == IDENTIFIER_NODE
12302 && IDENTIFIER_GLOBAL_VALUE (s))
12303 type = IDENTIFIER_GLOBAL_VALUE (s);
12304 if (TREE_CODE (s) == TYPE_DECL)
12305 s = TREE_TYPE (s);
12306 if (TYPE_P (s))
12307 {
12308 type = s;
12309 break;
12310 }
12311 }
12312 }
12313 if (!type)
12314 error ("friend declaration does not name a class or "
12315 "function");
12316 else
12317 make_friend_class (current_class_type, type);
12318 }
12319 /* If there is no TYPE, an error message will already have
12320 been issued. */
12321 else if (!type)
12322 ;
12323 /* An anonymous aggregate has to be handled specially; such
12324 a declaration really declares a data member (with a
12325 particular type), as opposed to a nested class. */
12326 else if (ANON_AGGR_TYPE_P (type))
12327 {
12328 /* Remove constructors and such from TYPE, now that we
12329 know it is an anoymous aggregate. */
12330 fixup_anonymous_aggr (type);
12331 /* And make the corresponding data member. */
12332 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12333 /* Add it to the class. */
12334 finish_member_declaration (decl);
12335 }
12336 }
12337 }
12338 else
12339 {
12340 /* See if these declarations will be friends. */
12341 friend_p = cp_parser_friend_p (decl_specifiers);
12342
12343 /* Keep going until we hit the `;' at the end of the
12344 declaration. */
12345 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12346 {
12347 tree attributes = NULL_TREE;
12348 tree first_attribute;
12349
12350 /* Peek at the next token. */
12351 token = cp_lexer_peek_token (parser->lexer);
12352
12353 /* Check for a bitfield declaration. */
12354 if (token->type == CPP_COLON
12355 || (token->type == CPP_NAME
12356 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12357 == CPP_COLON))
12358 {
12359 tree identifier;
12360 tree width;
12361
12362 /* Get the name of the bitfield. Note that we cannot just
12363 check TOKEN here because it may have been invalidated by
12364 the call to cp_lexer_peek_nth_token above. */
12365 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12366 identifier = cp_parser_identifier (parser);
12367 else
12368 identifier = NULL_TREE;
12369
12370 /* Consume the `:' token. */
12371 cp_lexer_consume_token (parser->lexer);
12372 /* Get the width of the bitfield. */
12373 width = cp_parser_constant_expression (parser);
12374
12375 /* Look for attributes that apply to the bitfield. */
12376 attributes = cp_parser_attributes_opt (parser);
12377 /* Remember which attributes are prefix attributes and
12378 which are not. */
12379 first_attribute = attributes;
12380 /* Combine the attributes. */
12381 attributes = chainon (prefix_attributes, attributes);
12382
12383 /* Create the bitfield declaration. */
12384 decl = grokbitfield (identifier,
12385 decl_specifiers,
12386 width);
12387 /* Apply the attributes. */
12388 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12389 }
12390 else
12391 {
12392 tree declarator;
12393 tree initializer;
12394 tree asm_specification;
12395 bool ctor_dtor_or_conv_p;
12396
12397 /* Parse the declarator. */
12398 declarator
62b8a44e 12399 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
12400 &ctor_dtor_or_conv_p);
12401
12402 /* If something went wrong parsing the declarator, make sure
12403 that we at least consume some tokens. */
12404 if (declarator == error_mark_node)
12405 {
12406 /* Skip to the end of the statement. */
12407 cp_parser_skip_to_end_of_statement (parser);
12408 break;
12409 }
12410
12411 /* Look for an asm-specification. */
12412 asm_specification = cp_parser_asm_specification_opt (parser);
12413 /* Look for attributes that apply to the declaration. */
12414 attributes = cp_parser_attributes_opt (parser);
12415 /* Remember which attributes are prefix attributes and
12416 which are not. */
12417 first_attribute = attributes;
12418 /* Combine the attributes. */
12419 attributes = chainon (prefix_attributes, attributes);
12420
12421 /* If it's an `=', then we have a constant-initializer or a
12422 pure-specifier. It is not correct to parse the
12423 initializer before registering the member declaration
12424 since the member declaration should be in scope while
12425 its initializer is processed. However, the rest of the
12426 front end does not yet provide an interface that allows
12427 us to handle this correctly. */
12428 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12429 {
12430 /* In [class.mem]:
12431
12432 A pure-specifier shall be used only in the declaration of
12433 a virtual function.
12434
12435 A member-declarator can contain a constant-initializer
12436 only if it declares a static member of integral or
12437 enumeration type.
12438
12439 Therefore, if the DECLARATOR is for a function, we look
12440 for a pure-specifier; otherwise, we look for a
12441 constant-initializer. When we call `grokfield', it will
12442 perform more stringent semantics checks. */
12443 if (TREE_CODE (declarator) == CALL_EXPR)
12444 initializer = cp_parser_pure_specifier (parser);
12445 else
12446 {
12447 /* This declaration cannot be a function
12448 definition. */
12449 cp_parser_commit_to_tentative_parse (parser);
12450 /* Parse the initializer. */
12451 initializer = cp_parser_constant_initializer (parser);
12452 }
12453 }
12454 /* Otherwise, there is no initializer. */
12455 else
12456 initializer = NULL_TREE;
12457
12458 /* See if we are probably looking at a function
12459 definition. We are certainly not looking at at a
12460 member-declarator. Calling `grokfield' has
12461 side-effects, so we must not do it unless we are sure
12462 that we are looking at a member-declarator. */
12463 if (cp_parser_token_starts_function_definition_p
12464 (cp_lexer_peek_token (parser->lexer)))
12465 decl = error_mark_node;
12466 else
12467 /* Create the declaration. */
12468 decl = grokfield (declarator,
12469 decl_specifiers,
12470 initializer,
12471 asm_specification,
12472 attributes);
12473 }
12474
12475 /* Reset PREFIX_ATTRIBUTES. */
12476 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12477 attributes = TREE_CHAIN (attributes);
12478 if (attributes)
12479 TREE_CHAIN (attributes) = NULL_TREE;
12480
12481 /* If there is any qualification still in effect, clear it
12482 now; we will be starting fresh with the next declarator. */
12483 parser->scope = NULL_TREE;
12484 parser->qualifying_scope = NULL_TREE;
12485 parser->object_scope = NULL_TREE;
12486 /* If it's a `,', then there are more declarators. */
12487 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12488 cp_lexer_consume_token (parser->lexer);
12489 /* If the next token isn't a `;', then we have a parse error. */
12490 else if (cp_lexer_next_token_is_not (parser->lexer,
12491 CPP_SEMICOLON))
12492 {
12493 cp_parser_error (parser, "expected `;'");
12494 /* Skip tokens until we find a `;' */
12495 cp_parser_skip_to_end_of_statement (parser);
12496
12497 break;
12498 }
12499
12500 if (decl)
12501 {
12502 /* Add DECL to the list of members. */
12503 if (!friend_p)
12504 finish_member_declaration (decl);
12505
12506 /* If DECL is a function, we must return
12507 to parse it later. (Even though there is no definition,
12508 there might be default arguments that need handling.) */
12509 if (TREE_CODE (decl) == FUNCTION_DECL)
12510 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 12511 = tree_cons (NULL_TREE, decl,
a723baf1
MM
12512 TREE_VALUE (parser->unparsed_functions_queues));
12513 }
12514 }
12515 }
12516
12517 /* If everything went well, look for the `;'. */
12518 if (cp_parser_parse_definitely (parser))
12519 {
12520 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12521 return;
12522 }
12523
12524 /* Parse the function-definition. */
12525 decl = cp_parser_function_definition (parser, &friend_p);
12526 /* If the member was not a friend, declare it here. */
12527 if (!friend_p)
12528 finish_member_declaration (decl);
12529 /* Peek at the next token. */
12530 token = cp_lexer_peek_token (parser->lexer);
12531 /* If the next token is a semicolon, consume it. */
12532 if (token->type == CPP_SEMICOLON)
12533 cp_lexer_consume_token (parser->lexer);
12534}
12535
12536/* Parse a pure-specifier.
12537
12538 pure-specifier:
12539 = 0
12540
12541 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12542 Otherwiser, ERROR_MARK_NODE is returned. */
12543
12544static tree
12545cp_parser_pure_specifier (parser)
12546 cp_parser *parser;
12547{
12548 cp_token *token;
12549
12550 /* Look for the `=' token. */
12551 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12552 return error_mark_node;
12553 /* Look for the `0' token. */
12554 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12555 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12556 to get information from the lexer about how the number was
12557 spelled in order to fix this problem. */
12558 if (!token || !integer_zerop (token->value))
12559 return error_mark_node;
12560
12561 return integer_zero_node;
12562}
12563
12564/* Parse a constant-initializer.
12565
12566 constant-initializer:
12567 = constant-expression
12568
12569 Returns a representation of the constant-expression. */
12570
12571static tree
12572cp_parser_constant_initializer (parser)
12573 cp_parser *parser;
12574{
12575 /* Look for the `=' token. */
12576 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12577 return error_mark_node;
12578
12579 /* It is invalid to write:
12580
12581 struct S { static const int i = { 7 }; };
12582
12583 */
12584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12585 {
12586 cp_parser_error (parser,
12587 "a brace-enclosed initializer is not allowed here");
12588 /* Consume the opening brace. */
12589 cp_lexer_consume_token (parser->lexer);
12590 /* Skip the initializer. */
12591 cp_parser_skip_to_closing_brace (parser);
12592 /* Look for the trailing `}'. */
12593 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12594
12595 return error_mark_node;
12596 }
12597
12598 return cp_parser_constant_expression (parser);
12599}
12600
12601/* Derived classes [gram.class.derived] */
12602
12603/* Parse a base-clause.
12604
12605 base-clause:
12606 : base-specifier-list
12607
12608 base-specifier-list:
12609 base-specifier
12610 base-specifier-list , base-specifier
12611
12612 Returns a TREE_LIST representing the base-classes, in the order in
12613 which they were declared. The representation of each node is as
12614 described by cp_parser_base_specifier.
12615
12616 In the case that no bases are specified, this function will return
12617 NULL_TREE, not ERROR_MARK_NODE. */
12618
12619static tree
12620cp_parser_base_clause (parser)
12621 cp_parser *parser;
12622{
12623 tree bases = NULL_TREE;
12624
12625 /* Look for the `:' that begins the list. */
12626 cp_parser_require (parser, CPP_COLON, "`:'");
12627
12628 /* Scan the base-specifier-list. */
12629 while (true)
12630 {
12631 cp_token *token;
12632 tree base;
12633
12634 /* Look for the base-specifier. */
12635 base = cp_parser_base_specifier (parser);
12636 /* Add BASE to the front of the list. */
12637 if (base != error_mark_node)
12638 {
12639 TREE_CHAIN (base) = bases;
12640 bases = base;
12641 }
12642 /* Peek at the next token. */
12643 token = cp_lexer_peek_token (parser->lexer);
12644 /* If it's not a comma, then the list is complete. */
12645 if (token->type != CPP_COMMA)
12646 break;
12647 /* Consume the `,'. */
12648 cp_lexer_consume_token (parser->lexer);
12649 }
12650
12651 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12652 base class had a qualified name. However, the next name that
12653 appears is certainly not qualified. */
12654 parser->scope = NULL_TREE;
12655 parser->qualifying_scope = NULL_TREE;
12656 parser->object_scope = NULL_TREE;
12657
12658 return nreverse (bases);
12659}
12660
12661/* Parse a base-specifier.
12662
12663 base-specifier:
12664 :: [opt] nested-name-specifier [opt] class-name
12665 virtual access-specifier [opt] :: [opt] nested-name-specifier
12666 [opt] class-name
12667 access-specifier virtual [opt] :: [opt] nested-name-specifier
12668 [opt] class-name
12669
12670 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12671 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12672 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12673 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12674
12675static tree
12676cp_parser_base_specifier (parser)
12677 cp_parser *parser;
12678{
12679 cp_token *token;
12680 bool done = false;
12681 bool virtual_p = false;
12682 bool duplicate_virtual_error_issued_p = false;
12683 bool duplicate_access_error_issued_p = false;
12684 bool class_scope_p;
12685 access_kind access = ak_none;
12686 tree access_node;
12687 tree type;
12688
12689 /* Process the optional `virtual' and `access-specifier'. */
12690 while (!done)
12691 {
12692 /* Peek at the next token. */
12693 token = cp_lexer_peek_token (parser->lexer);
12694 /* Process `virtual'. */
12695 switch (token->keyword)
12696 {
12697 case RID_VIRTUAL:
12698 /* If `virtual' appears more than once, issue an error. */
12699 if (virtual_p && !duplicate_virtual_error_issued_p)
12700 {
12701 cp_parser_error (parser,
12702 "`virtual' specified more than once in base-specified");
12703 duplicate_virtual_error_issued_p = true;
12704 }
12705
12706 virtual_p = true;
12707
12708 /* Consume the `virtual' token. */
12709 cp_lexer_consume_token (parser->lexer);
12710
12711 break;
12712
12713 case RID_PUBLIC:
12714 case RID_PROTECTED:
12715 case RID_PRIVATE:
12716 /* If more than one access specifier appears, issue an
12717 error. */
12718 if (access != ak_none && !duplicate_access_error_issued_p)
12719 {
12720 cp_parser_error (parser,
12721 "more than one access specifier in base-specified");
12722 duplicate_access_error_issued_p = true;
12723 }
12724
12725 access = ((access_kind)
12726 tree_low_cst (ridpointers[(int) token->keyword],
12727 /*pos=*/1));
12728
12729 /* Consume the access-specifier. */
12730 cp_lexer_consume_token (parser->lexer);
12731
12732 break;
12733
12734 default:
12735 done = true;
12736 break;
12737 }
12738 }
12739
12740 /* Map `virtual_p' and `access' onto one of the access
12741 tree-nodes. */
12742 if (!virtual_p)
12743 switch (access)
12744 {
12745 case ak_none:
12746 access_node = access_default_node;
12747 break;
12748 case ak_public:
12749 access_node = access_public_node;
12750 break;
12751 case ak_protected:
12752 access_node = access_protected_node;
12753 break;
12754 case ak_private:
12755 access_node = access_private_node;
12756 break;
12757 default:
12758 abort ();
12759 }
12760 else
12761 switch (access)
12762 {
12763 case ak_none:
12764 access_node = access_default_virtual_node;
12765 break;
12766 case ak_public:
12767 access_node = access_public_virtual_node;
12768 break;
12769 case ak_protected:
12770 access_node = access_protected_virtual_node;
12771 break;
12772 case ak_private:
12773 access_node = access_private_virtual_node;
12774 break;
12775 default:
12776 abort ();
12777 }
12778
12779 /* Look for the optional `::' operator. */
12780 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12781 /* Look for the nested-name-specifier. The simplest way to
12782 implement:
12783
12784 [temp.res]
12785
12786 The keyword `typename' is not permitted in a base-specifier or
12787 mem-initializer; in these contexts a qualified name that
12788 depends on a template-parameter is implicitly assumed to be a
12789 type name.
12790
12791 is to pretend that we have seen the `typename' keyword at this
12792 point. */
12793 cp_parser_nested_name_specifier_opt (parser,
12794 /*typename_keyword_p=*/true,
12795 /*check_dependency_p=*/true,
12796 /*type_p=*/true);
12797 /* If the base class is given by a qualified name, assume that names
12798 we see are type names or templates, as appropriate. */
12799 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12800 /* Finally, look for the class-name. */
12801 type = cp_parser_class_name (parser,
12802 class_scope_p,
12803 class_scope_p,
12804 /*type_p=*/true,
12805 /*check_access=*/true,
12806 /*check_dependency_p=*/true,
12807 /*class_head_p=*/false);
12808
12809 if (type == error_mark_node)
12810 return error_mark_node;
12811
12812 return finish_base_specifier (access_node, TREE_TYPE (type));
12813}
12814
12815/* Exception handling [gram.exception] */
12816
12817/* Parse an (optional) exception-specification.
12818
12819 exception-specification:
12820 throw ( type-id-list [opt] )
12821
12822 Returns a TREE_LIST representing the exception-specification. The
12823 TREE_VALUE of each node is a type. */
12824
12825static tree
12826cp_parser_exception_specification_opt (parser)
12827 cp_parser *parser;
12828{
12829 cp_token *token;
12830 tree type_id_list;
12831
12832 /* Peek at the next token. */
12833 token = cp_lexer_peek_token (parser->lexer);
12834 /* If it's not `throw', then there's no exception-specification. */
12835 if (!cp_parser_is_keyword (token, RID_THROW))
12836 return NULL_TREE;
12837
12838 /* Consume the `throw'. */
12839 cp_lexer_consume_token (parser->lexer);
12840
12841 /* Look for the `('. */
12842 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12843
12844 /* Peek at the next token. */
12845 token = cp_lexer_peek_token (parser->lexer);
12846 /* If it's not a `)', then there is a type-id-list. */
12847 if (token->type != CPP_CLOSE_PAREN)
12848 {
12849 const char *saved_message;
12850
12851 /* Types may not be defined in an exception-specification. */
12852 saved_message = parser->type_definition_forbidden_message;
12853 parser->type_definition_forbidden_message
12854 = "types may not be defined in an exception-specification";
12855 /* Parse the type-id-list. */
12856 type_id_list = cp_parser_type_id_list (parser);
12857 /* Restore the saved message. */
12858 parser->type_definition_forbidden_message = saved_message;
12859 }
12860 else
12861 type_id_list = empty_except_spec;
12862
12863 /* Look for the `)'. */
12864 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12865
12866 return type_id_list;
12867}
12868
12869/* Parse an (optional) type-id-list.
12870
12871 type-id-list:
12872 type-id
12873 type-id-list , type-id
12874
12875 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12876 in the order that the types were presented. */
12877
12878static tree
12879cp_parser_type_id_list (parser)
12880 cp_parser *parser;
12881{
12882 tree types = NULL_TREE;
12883
12884 while (true)
12885 {
12886 cp_token *token;
12887 tree type;
12888
12889 /* Get the next type-id. */
12890 type = cp_parser_type_id (parser);
12891 /* Add it to the list. */
12892 types = add_exception_specifier (types, type, /*complain=*/1);
12893 /* Peek at the next token. */
12894 token = cp_lexer_peek_token (parser->lexer);
12895 /* If it is not a `,', we are done. */
12896 if (token->type != CPP_COMMA)
12897 break;
12898 /* Consume the `,'. */
12899 cp_lexer_consume_token (parser->lexer);
12900 }
12901
12902 return nreverse (types);
12903}
12904
12905/* Parse a try-block.
12906
12907 try-block:
12908 try compound-statement handler-seq */
12909
12910static tree
12911cp_parser_try_block (parser)
12912 cp_parser *parser;
12913{
12914 tree try_block;
12915
12916 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12917 try_block = begin_try_block ();
12918 cp_parser_compound_statement (parser);
12919 finish_try_block (try_block);
12920 cp_parser_handler_seq (parser);
12921 finish_handler_sequence (try_block);
12922
12923 return try_block;
12924}
12925
12926/* Parse a function-try-block.
12927
12928 function-try-block:
12929 try ctor-initializer [opt] function-body handler-seq */
12930
12931static bool
12932cp_parser_function_try_block (parser)
12933 cp_parser *parser;
12934{
12935 tree try_block;
12936 bool ctor_initializer_p;
12937
12938 /* Look for the `try' keyword. */
12939 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12940 return false;
12941 /* Let the rest of the front-end know where we are. */
12942 try_block = begin_function_try_block ();
12943 /* Parse the function-body. */
12944 ctor_initializer_p
12945 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12946 /* We're done with the `try' part. */
12947 finish_function_try_block (try_block);
12948 /* Parse the handlers. */
12949 cp_parser_handler_seq (parser);
12950 /* We're done with the handlers. */
12951 finish_function_handler_sequence (try_block);
12952
12953 return ctor_initializer_p;
12954}
12955
12956/* Parse a handler-seq.
12957
12958 handler-seq:
12959 handler handler-seq [opt] */
12960
12961static void
12962cp_parser_handler_seq (parser)
12963 cp_parser *parser;
12964{
12965 while (true)
12966 {
12967 cp_token *token;
12968
12969 /* Parse the handler. */
12970 cp_parser_handler (parser);
12971 /* Peek at the next token. */
12972 token = cp_lexer_peek_token (parser->lexer);
12973 /* If it's not `catch' then there are no more handlers. */
12974 if (!cp_parser_is_keyword (token, RID_CATCH))
12975 break;
12976 }
12977}
12978
12979/* Parse a handler.
12980
12981 handler:
12982 catch ( exception-declaration ) compound-statement */
12983
12984static void
12985cp_parser_handler (parser)
12986 cp_parser *parser;
12987{
12988 tree handler;
12989 tree declaration;
12990
12991 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12992 handler = begin_handler ();
12993 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12994 declaration = cp_parser_exception_declaration (parser);
12995 finish_handler_parms (declaration, handler);
12996 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12997 cp_parser_compound_statement (parser);
12998 finish_handler (handler);
12999}
13000
13001/* Parse an exception-declaration.
13002
13003 exception-declaration:
13004 type-specifier-seq declarator
13005 type-specifier-seq abstract-declarator
13006 type-specifier-seq
13007 ...
13008
13009 Returns a VAR_DECL for the declaration, or NULL_TREE if the
13010 ellipsis variant is used. */
13011
13012static tree
13013cp_parser_exception_declaration (parser)
13014 cp_parser *parser;
13015{
13016 tree type_specifiers;
13017 tree declarator;
13018 const char *saved_message;
13019
13020 /* If it's an ellipsis, it's easy to handle. */
13021 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13022 {
13023 /* Consume the `...' token. */
13024 cp_lexer_consume_token (parser->lexer);
13025 return NULL_TREE;
13026 }
13027
13028 /* Types may not be defined in exception-declarations. */
13029 saved_message = parser->type_definition_forbidden_message;
13030 parser->type_definition_forbidden_message
13031 = "types may not be defined in exception-declarations";
13032
13033 /* Parse the type-specifier-seq. */
13034 type_specifiers = cp_parser_type_specifier_seq (parser);
13035 /* If it's a `)', then there is no declarator. */
13036 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13037 declarator = NULL_TREE;
13038 else
62b8a44e
NS
13039 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13040 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1
MM
13041
13042 /* Restore the saved message. */
13043 parser->type_definition_forbidden_message = saved_message;
13044
13045 return start_handler_parms (type_specifiers, declarator);
13046}
13047
13048/* Parse a throw-expression.
13049
13050 throw-expression:
13051 throw assignment-expresion [opt]
13052
13053 Returns a THROW_EXPR representing the throw-expression. */
13054
13055static tree
13056cp_parser_throw_expression (parser)
13057 cp_parser *parser;
13058{
13059 tree expression;
13060
13061 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13062 /* We can't be sure if there is an assignment-expression or not. */
13063 cp_parser_parse_tentatively (parser);
13064 /* Try it. */
13065 expression = cp_parser_assignment_expression (parser);
13066 /* If it didn't work, this is just a rethrow. */
13067 if (!cp_parser_parse_definitely (parser))
13068 expression = NULL_TREE;
13069
13070 return build_throw (expression);
13071}
13072
13073/* GNU Extensions */
13074
13075/* Parse an (optional) asm-specification.
13076
13077 asm-specification:
13078 asm ( string-literal )
13079
13080 If the asm-specification is present, returns a STRING_CST
13081 corresponding to the string-literal. Otherwise, returns
13082 NULL_TREE. */
13083
13084static tree
13085cp_parser_asm_specification_opt (parser)
13086 cp_parser *parser;
13087{
13088 cp_token *token;
13089 tree asm_specification;
13090
13091 /* Peek at the next token. */
13092 token = cp_lexer_peek_token (parser->lexer);
13093 /* If the next token isn't the `asm' keyword, then there's no
13094 asm-specification. */
13095 if (!cp_parser_is_keyword (token, RID_ASM))
13096 return NULL_TREE;
13097
13098 /* Consume the `asm' token. */
13099 cp_lexer_consume_token (parser->lexer);
13100 /* Look for the `('. */
13101 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13102
13103 /* Look for the string-literal. */
13104 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13105 if (token)
13106 asm_specification = token->value;
13107 else
13108 asm_specification = NULL_TREE;
13109
13110 /* Look for the `)'. */
13111 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13112
13113 return asm_specification;
13114}
13115
13116/* Parse an asm-operand-list.
13117
13118 asm-operand-list:
13119 asm-operand
13120 asm-operand-list , asm-operand
13121
13122 asm-operand:
13123 string-literal ( expression )
13124 [ string-literal ] string-literal ( expression )
13125
13126 Returns a TREE_LIST representing the operands. The TREE_VALUE of
13127 each node is the expression. The TREE_PURPOSE is itself a
13128 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13129 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13130 is a STRING_CST for the string literal before the parenthesis. */
13131
13132static tree
13133cp_parser_asm_operand_list (parser)
13134 cp_parser *parser;
13135{
13136 tree asm_operands = NULL_TREE;
13137
13138 while (true)
13139 {
13140 tree string_literal;
13141 tree expression;
13142 tree name;
13143 cp_token *token;
13144
13145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13146 {
13147 /* Consume the `[' token. */
13148 cp_lexer_consume_token (parser->lexer);
13149 /* Read the operand name. */
13150 name = cp_parser_identifier (parser);
13151 if (name != error_mark_node)
13152 name = build_string (IDENTIFIER_LENGTH (name),
13153 IDENTIFIER_POINTER (name));
13154 /* Look for the closing `]'. */
13155 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13156 }
13157 else
13158 name = NULL_TREE;
13159 /* Look for the string-literal. */
13160 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13161 string_literal = token ? token->value : error_mark_node;
13162 /* Look for the `('. */
13163 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13164 /* Parse the expression. */
13165 expression = cp_parser_expression (parser);
13166 /* Look for the `)'. */
13167 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13168 /* Add this operand to the list. */
13169 asm_operands = tree_cons (build_tree_list (name, string_literal),
13170 expression,
13171 asm_operands);
13172 /* If the next token is not a `,', there are no more
13173 operands. */
13174 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13175 break;
13176 /* Consume the `,'. */
13177 cp_lexer_consume_token (parser->lexer);
13178 }
13179
13180 return nreverse (asm_operands);
13181}
13182
13183/* Parse an asm-clobber-list.
13184
13185 asm-clobber-list:
13186 string-literal
13187 asm-clobber-list , string-literal
13188
13189 Returns a TREE_LIST, indicating the clobbers in the order that they
13190 appeared. The TREE_VALUE of each node is a STRING_CST. */
13191
13192static tree
13193cp_parser_asm_clobber_list (parser)
13194 cp_parser *parser;
13195{
13196 tree clobbers = NULL_TREE;
13197
13198 while (true)
13199 {
13200 cp_token *token;
13201 tree string_literal;
13202
13203 /* Look for the string literal. */
13204 token = cp_parser_require (parser, CPP_STRING, "string-literal");
13205 string_literal = token ? token->value : error_mark_node;
13206 /* Add it to the list. */
13207 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13208 /* If the next token is not a `,', then the list is
13209 complete. */
13210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13211 break;
13212 /* Consume the `,' token. */
13213 cp_lexer_consume_token (parser->lexer);
13214 }
13215
13216 return clobbers;
13217}
13218
13219/* Parse an (optional) series of attributes.
13220
13221 attributes:
13222 attributes attribute
13223
13224 attribute:
13225 __attribute__ (( attribute-list [opt] ))
13226
13227 The return value is as for cp_parser_attribute_list. */
13228
13229static tree
13230cp_parser_attributes_opt (parser)
13231 cp_parser *parser;
13232{
13233 tree attributes = NULL_TREE;
13234
13235 while (true)
13236 {
13237 cp_token *token;
13238 tree attribute_list;
13239
13240 /* Peek at the next token. */
13241 token = cp_lexer_peek_token (parser->lexer);
13242 /* If it's not `__attribute__', then we're done. */
13243 if (token->keyword != RID_ATTRIBUTE)
13244 break;
13245
13246 /* Consume the `__attribute__' keyword. */
13247 cp_lexer_consume_token (parser->lexer);
13248 /* Look for the two `(' tokens. */
13249 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13250 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13251
13252 /* Peek at the next token. */
13253 token = cp_lexer_peek_token (parser->lexer);
13254 if (token->type != CPP_CLOSE_PAREN)
13255 /* Parse the attribute-list. */
13256 attribute_list = cp_parser_attribute_list (parser);
13257 else
13258 /* If the next token is a `)', then there is no attribute
13259 list. */
13260 attribute_list = NULL;
13261
13262 /* Look for the two `)' tokens. */
13263 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13264 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13265
13266 /* Add these new attributes to the list. */
13267 attributes = chainon (attributes, attribute_list);
13268 }
13269
13270 return attributes;
13271}
13272
13273/* Parse an attribute-list.
13274
13275 attribute-list:
13276 attribute
13277 attribute-list , attribute
13278
13279 attribute:
13280 identifier
13281 identifier ( identifier )
13282 identifier ( identifier , expression-list )
13283 identifier ( expression-list )
13284
13285 Returns a TREE_LIST. Each node corresponds to an attribute. THe
13286 TREE_PURPOSE of each node is the identifier indicating which
13287 attribute is in use. The TREE_VALUE represents the arguments, if
13288 any. */
13289
13290static tree
13291cp_parser_attribute_list (parser)
13292 cp_parser *parser;
13293{
13294 tree attribute_list = NULL_TREE;
13295
13296 while (true)
13297 {
13298 cp_token *token;
13299 tree identifier;
13300 tree attribute;
13301
13302 /* Look for the identifier. We also allow keywords here; for
13303 example `__attribute__ ((const))' is legal. */
13304 token = cp_lexer_peek_token (parser->lexer);
13305 if (token->type != CPP_NAME
13306 && token->type != CPP_KEYWORD)
13307 return error_mark_node;
13308 /* Consume the token. */
13309 token = cp_lexer_consume_token (parser->lexer);
13310
13311 /* Save away the identifier that indicates which attribute this is. */
13312 identifier = token->value;
13313 attribute = build_tree_list (identifier, NULL_TREE);
13314
13315 /* Peek at the next token. */
13316 token = cp_lexer_peek_token (parser->lexer);
13317 /* If it's an `(', then parse the attribute arguments. */
13318 if (token->type == CPP_OPEN_PAREN)
13319 {
13320 tree arguments;
13321 int arguments_allowed_p = 1;
13322
13323 /* Consume the `('. */
13324 cp_lexer_consume_token (parser->lexer);
13325 /* Peek at the next token. */
13326 token = cp_lexer_peek_token (parser->lexer);
13327 /* Check to see if the next token is an identifier. */
13328 if (token->type == CPP_NAME)
13329 {
13330 /* Save the identifier. */
13331 identifier = token->value;
13332 /* Consume the identifier. */
13333 cp_lexer_consume_token (parser->lexer);
13334 /* Peek at the next token. */
13335 token = cp_lexer_peek_token (parser->lexer);
13336 /* If the next token is a `,', then there are some other
13337 expressions as well. */
13338 if (token->type == CPP_COMMA)
13339 /* Consume the comma. */
13340 cp_lexer_consume_token (parser->lexer);
13341 else
13342 arguments_allowed_p = 0;
13343 }
13344 else
13345 identifier = NULL_TREE;
13346
13347 /* If there are arguments, parse them too. */
13348 if (arguments_allowed_p)
13349 arguments = cp_parser_expression_list (parser);
13350 else
13351 arguments = NULL_TREE;
13352
13353 /* Combine the identifier and the arguments. */
13354 if (identifier)
13355 arguments = tree_cons (NULL_TREE, identifier, arguments);
13356
13357 /* Save the identifier and arguments away. */
13358 TREE_VALUE (attribute) = arguments;
13359
13360 /* Look for the closing `)'. */
13361 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13362 }
13363
13364 /* Add this attribute to the list. */
13365 TREE_CHAIN (attribute) = attribute_list;
13366 attribute_list = attribute;
13367
13368 /* Now, look for more attributes. */
13369 token = cp_lexer_peek_token (parser->lexer);
13370 /* If the next token isn't a `,', we're done. */
13371 if (token->type != CPP_COMMA)
13372 break;
13373
13374 /* Consume the commma and keep going. */
13375 cp_lexer_consume_token (parser->lexer);
13376 }
13377
13378 /* We built up the list in reverse order. */
13379 return nreverse (attribute_list);
13380}
13381
13382/* Parse an optional `__extension__' keyword. Returns TRUE if it is
13383 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13384 current value of the PEDANTIC flag, regardless of whether or not
13385 the `__extension__' keyword is present. The caller is responsible
13386 for restoring the value of the PEDANTIC flag. */
13387
13388static bool
13389cp_parser_extension_opt (parser, saved_pedantic)
13390 cp_parser *parser;
13391 int *saved_pedantic;
13392{
13393 /* Save the old value of the PEDANTIC flag. */
13394 *saved_pedantic = pedantic;
13395
13396 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13397 {
13398 /* Consume the `__extension__' token. */
13399 cp_lexer_consume_token (parser->lexer);
13400 /* We're not being pedantic while the `__extension__' keyword is
13401 in effect. */
13402 pedantic = 0;
13403
13404 return true;
13405 }
13406
13407 return false;
13408}
13409
13410/* Parse a label declaration.
13411
13412 label-declaration:
13413 __label__ label-declarator-seq ;
13414
13415 label-declarator-seq:
13416 identifier , label-declarator-seq
13417 identifier */
13418
13419static void
13420cp_parser_label_declaration (parser)
13421 cp_parser *parser;
13422{
13423 /* Look for the `__label__' keyword. */
13424 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13425
13426 while (true)
13427 {
13428 tree identifier;
13429
13430 /* Look for an identifier. */
13431 identifier = cp_parser_identifier (parser);
13432 /* Declare it as a lobel. */
13433 finish_label_decl (identifier);
13434 /* If the next token is a `;', stop. */
13435 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13436 break;
13437 /* Look for the `,' separating the label declarations. */
13438 cp_parser_require (parser, CPP_COMMA, "`,'");
13439 }
13440
13441 /* Look for the final `;'. */
13442 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13443}
13444
13445/* Support Functions */
13446
13447/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13448 NAME should have one of the representations used for an
13449 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13450 is returned. If PARSER->SCOPE is a dependent type, then a
13451 SCOPE_REF is returned.
13452
13453 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13454 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13455 was formed. Abstractly, such entities should not be passed to this
13456 function, because they do not need to be looked up, but it is
13457 simpler to check for this special case here, rather than at the
13458 call-sites.
13459
13460 In cases not explicitly covered above, this function returns a
13461 DECL, OVERLOAD, or baselink representing the result of the lookup.
13462 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13463 is returned.
13464
13465 If CHECK_ACCESS is TRUE, then access control is performed on the
13466 declaration to which the name resolves, and an error message is
13467 issued if the declaration is inaccessible.
13468
13469 If IS_TYPE is TRUE, bindings that do not refer to types are
13470 ignored.
13471
eea9800f
MM
13472 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13473 are ignored.
13474
a723baf1
MM
13475 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13476 types. */
13477
13478static tree
eea9800f
MM
13479cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13480 bool is_type, bool is_namespace, bool check_dependency)
a723baf1
MM
13481{
13482 tree decl;
13483 tree object_type = parser->context->object_type;
13484
13485 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13486 no longer valid. Note that if we are parsing tentatively, and
13487 the parse fails, OBJECT_TYPE will be automatically restored. */
13488 parser->context->object_type = NULL_TREE;
13489
13490 if (name == error_mark_node)
13491 return error_mark_node;
13492
13493 /* A template-id has already been resolved; there is no lookup to
13494 do. */
13495 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13496 return name;
13497 if (BASELINK_P (name))
13498 {
13499 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13500 == TEMPLATE_ID_EXPR),
13501 20020909);
13502 return name;
13503 }
13504
13505 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13506 it should already have been checked to make sure that the name
13507 used matches the type being destroyed. */
13508 if (TREE_CODE (name) == BIT_NOT_EXPR)
13509 {
13510 tree type;
13511
13512 /* Figure out to which type this destructor applies. */
13513 if (parser->scope)
13514 type = parser->scope;
13515 else if (object_type)
13516 type = object_type;
13517 else
13518 type = current_class_type;
13519 /* If that's not a class type, there is no destructor. */
13520 if (!type || !CLASS_TYPE_P (type))
13521 return error_mark_node;
13522 /* If it was a class type, return the destructor. */
13523 return CLASSTYPE_DESTRUCTORS (type);
13524 }
13525
13526 /* By this point, the NAME should be an ordinary identifier. If
13527 the id-expression was a qualified name, the qualifying scope is
13528 stored in PARSER->SCOPE at this point. */
13529 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13530 20000619);
13531
13532 /* Perform the lookup. */
13533 if (parser->scope)
13534 {
13535 bool dependent_type_p;
13536
13537 if (parser->scope == error_mark_node)
13538 return error_mark_node;
13539
13540 /* If the SCOPE is dependent, the lookup must be deferred until
13541 the template is instantiated -- unless we are explicitly
13542 looking up names in uninstantiated templates. Even then, we
13543 cannot look up the name if the scope is not a class type; it
13544 might, for example, be a template type parameter. */
13545 dependent_type_p = (TYPE_P (parser->scope)
13546 && !(parser->in_declarator_p
13547 && currently_open_class (parser->scope))
13548 && cp_parser_dependent_type_p (parser->scope));
13549 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13550 && dependent_type_p)
13551 {
13552 if (!is_type)
13553 decl = build_nt (SCOPE_REF, parser->scope, name);
13554 else
13555 /* The resolution to Core Issue 180 says that `struct A::B'
13556 should be considered a type-name, even if `A' is
13557 dependent. */
13558 decl = TYPE_NAME (make_typename_type (parser->scope,
13559 name,
13560 /*complain=*/1));
13561 }
13562 else
13563 {
13564 /* If PARSER->SCOPE is a dependent type, then it must be a
13565 class type, and we must not be checking dependencies;
13566 otherwise, we would have processed this lookup above. So
13567 that PARSER->SCOPE is not considered a dependent base by
13568 lookup_member, we must enter the scope here. */
13569 if (dependent_type_p)
13570 push_scope (parser->scope);
13571 /* If the PARSER->SCOPE is a a template specialization, it
13572 may be instantiated during name lookup. In that case,
13573 errors may be issued. Even if we rollback the current
13574 tentative parse, those errors are valid. */
13575 decl = lookup_qualified_name (parser->scope, name, is_type,
13576 /*flags=*/0);
13577 if (dependent_type_p)
13578 pop_scope (parser->scope);
13579 }
13580 parser->qualifying_scope = parser->scope;
13581 parser->object_scope = NULL_TREE;
13582 }
13583 else if (object_type)
13584 {
13585 tree object_decl = NULL_TREE;
13586 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13587 OBJECT_TYPE is not a class. */
13588 if (CLASS_TYPE_P (object_type))
13589 /* If the OBJECT_TYPE is a template specialization, it may
13590 be instantiated during name lookup. In that case, errors
13591 may be issued. Even if we rollback the current tentative
13592 parse, those errors are valid. */
13593 object_decl = lookup_member (object_type,
13594 name,
13595 /*protect=*/0, is_type);
13596 /* Look it up in the enclosing context, too. */
13597 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13598 is_namespace,
a723baf1
MM
13599 /*flags=*/0);
13600 parser->object_scope = object_type;
13601 parser->qualifying_scope = NULL_TREE;
13602 if (object_decl)
13603 decl = object_decl;
13604 }
13605 else
13606 {
13607 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13608 is_namespace,
a723baf1
MM
13609 /*flags=*/0);
13610 parser->qualifying_scope = NULL_TREE;
13611 parser->object_scope = NULL_TREE;
13612 }
13613
13614 /* If the lookup failed, let our caller know. */
13615 if (!decl
13616 || decl == error_mark_node
13617 || (TREE_CODE (decl) == FUNCTION_DECL
13618 && DECL_ANTICIPATED (decl)))
13619 return error_mark_node;
13620
13621 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13622 if (TREE_CODE (decl) == TREE_LIST)
13623 {
13624 /* The error message we have to print is too complicated for
13625 cp_parser_error, so we incorporate its actions directly. */
e5976695 13626 if (!cp_parser_simulate_error (parser))
a723baf1
MM
13627 {
13628 error ("reference to `%D' is ambiguous", name);
13629 print_candidates (decl);
13630 }
13631 return error_mark_node;
13632 }
13633
13634 my_friendly_assert (DECL_P (decl)
13635 || TREE_CODE (decl) == OVERLOAD
13636 || TREE_CODE (decl) == SCOPE_REF
13637 || BASELINK_P (decl),
13638 20000619);
13639
13640 /* If we have resolved the name of a member declaration, check to
13641 see if the declaration is accessible. When the name resolves to
13642 set of overloaded functions, accesibility is checked when
13643 overload resolution is done.
13644
13645 During an explicit instantiation, access is not checked at all,
13646 as per [temp.explicit]. */
13647 if (check_access && scope_chain->check_access && DECL_P (decl))
13648 {
13649 tree qualifying_type;
13650
13651 /* Figure out the type through which DECL is being
13652 accessed. */
13653 qualifying_type
13654 = cp_parser_scope_through_which_access_occurs (decl,
13655 object_type,
13656 parser->scope);
13657 if (qualifying_type)
13658 {
13659 /* If we are supposed to defer access checks, just record
13660 the information for later. */
13661 if (parser->context->deferring_access_checks_p)
13662 cp_parser_defer_access_check (parser, qualifying_type, decl);
13663 /* Otherwise, check accessibility now. */
13664 else
13665 enforce_access (qualifying_type, decl);
13666 }
13667 }
13668
13669 return decl;
13670}
13671
13672/* Like cp_parser_lookup_name, but for use in the typical case where
13673 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13674 TRUE. */
13675
13676static tree
13677cp_parser_lookup_name_simple (parser, name)
13678 cp_parser *parser;
13679 tree name;
13680{
13681 return cp_parser_lookup_name (parser, name,
13682 /*check_access=*/true,
eea9800f
MM
13683 /*is_type=*/false,
13684 /*is_namespace=*/false,
a723baf1
MM
13685 /*check_dependency=*/true);
13686}
13687
13688/* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13689 TYPENAME_TYPE corresponds. Note that this function peers inside
13690 uninstantiated templates and therefore should be used only in
13691 extremely limited situations. */
13692
13693static tree
13694cp_parser_resolve_typename_type (parser, type)
13695 cp_parser *parser;
13696 tree type;
13697{
13698 tree scope;
13699 tree name;
13700 tree decl;
13701
13702 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
13703 20010702);
13704
13705 scope = TYPE_CONTEXT (type);
13706 name = DECL_NAME (TYPE_NAME (type));
13707
13708 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13709 it first before we can figure out what NAME refers to. */
13710 if (TREE_CODE (scope) == TYPENAME_TYPE)
13711 scope = cp_parser_resolve_typename_type (parser, scope);
13712 /* If we don't know what SCOPE refers to, then we cannot resolve the
13713 TYPENAME_TYPE. */
13714 if (scope == error_mark_node)
13715 return error_mark_node;
13716 /* If the SCOPE is a template type parameter, we have no way of
13717 resolving the name. */
13718 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13719 return type;
13720 /* Enter the SCOPE so that name lookup will be resolved as if we
13721 were in the class definition. In particular, SCOPE will no
13722 longer be considered a dependent type. */
13723 push_scope (scope);
13724 /* Look up the declaration. */
13725 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/1);
13726 /* If all went well, we got a TYPE_DECL for a non-typename. */
13727 if (!decl
13728 || TREE_CODE (decl) != TYPE_DECL
13729 || TREE_CODE (TREE_TYPE (decl)) == TYPENAME_TYPE)
13730 {
13731 cp_parser_error (parser, "could not resolve typename type");
13732 type = error_mark_node;
13733 }
13734 else
13735 type = TREE_TYPE (decl);
13736 /* Leave the SCOPE. */
13737 pop_scope (scope);
13738
13739 return type;
13740}
13741
13742/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13743 the current context, return the TYPE_DECL. If TAG_NAME_P is
13744 true, the DECL indicates the class being defined in a class-head,
13745 or declared in an elaborated-type-specifier.
13746
13747 Otherwise, return DECL. */
13748
13749static tree
13750cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13751{
13752 /* If the DECL is a TEMPLATE_DECL for a class type, and we are in
13753 the scope of the class, then treat the TEMPLATE_DECL as a
13754 class-name. For example, in:
13755
13756 template <class T> struct S {
13757 S s;
13758 };
13759
13760 is OK.
13761
13762 If the TEMPLATE_DECL is being declared as part of a class-head,
13763 the same translation occurs:
13764
13765 struct A {
13766 template <typename T> struct B;
13767 };
13768
13769 template <typename T> struct A::B {};
13770
13771 Similarly, in a elaborated-type-specifier:
13772
13773 namespace N { struct X{}; }
13774
13775 struct A {
13776 template <typename T> friend struct N::X;
13777 };
13778
13779 */
13780 if (DECL_CLASS_TEMPLATE_P (decl)
13781 && (tag_name_p
13782 || (current_class_type
13783 && same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (decl)),
13784 current_class_type))))
13785 return DECL_TEMPLATE_RESULT (decl);
13786
13787 return decl;
13788}
13789
13790/* If too many, or too few, template-parameter lists apply to the
13791 declarator, issue an error message. Returns TRUE if all went well,
13792 and FALSE otherwise. */
13793
13794static bool
13795cp_parser_check_declarator_template_parameters (parser, declarator)
13796 cp_parser *parser;
13797 tree declarator;
13798{
13799 unsigned num_templates;
13800
13801 /* We haven't seen any classes that involve template parameters yet. */
13802 num_templates = 0;
13803
13804 switch (TREE_CODE (declarator))
13805 {
13806 case CALL_EXPR:
13807 case ARRAY_REF:
13808 case INDIRECT_REF:
13809 case ADDR_EXPR:
13810 {
13811 tree main_declarator = TREE_OPERAND (declarator, 0);
13812 return
13813 cp_parser_check_declarator_template_parameters (parser,
13814 main_declarator);
13815 }
13816
13817 case SCOPE_REF:
13818 {
13819 tree scope;
13820 tree member;
13821
13822 scope = TREE_OPERAND (declarator, 0);
13823 member = TREE_OPERAND (declarator, 1);
13824
13825 /* If this is a pointer-to-member, then we are not interested
13826 in the SCOPE, because it does not qualify the thing that is
13827 being declared. */
13828 if (TREE_CODE (member) == INDIRECT_REF)
13829 return (cp_parser_check_declarator_template_parameters
13830 (parser, member));
13831
13832 while (scope && CLASS_TYPE_P (scope))
13833 {
13834 /* You're supposed to have one `template <...>'
13835 for every template class, but you don't need one
13836 for a full specialization. For example:
13837
13838 template <class T> struct S{};
13839 template <> struct S<int> { void f(); };
13840 void S<int>::f () {}
13841
13842 is correct; there shouldn't be a `template <>' for
13843 the definition of `S<int>::f'. */
13844 if (CLASSTYPE_TEMPLATE_INFO (scope)
13845 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13846 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13847 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13848 ++num_templates;
13849
13850 scope = TYPE_CONTEXT (scope);
13851 }
13852 }
13853
13854 /* Fall through. */
13855
13856 default:
13857 /* If the DECLARATOR has the form `X<y>' then it uses one
13858 additional level of template parameters. */
13859 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13860 ++num_templates;
13861
13862 return cp_parser_check_template_parameters (parser,
13863 num_templates);
13864 }
13865}
13866
13867/* NUM_TEMPLATES were used in the current declaration. If that is
13868 invalid, return FALSE and issue an error messages. Otherwise,
13869 return TRUE. */
13870
13871static bool
13872cp_parser_check_template_parameters (parser, num_templates)
13873 cp_parser *parser;
13874 unsigned num_templates;
13875{
13876 /* If there are more template classes than parameter lists, we have
13877 something like:
13878
13879 template <class T> void S<T>::R<T>::f (); */
13880 if (parser->num_template_parameter_lists < num_templates)
13881 {
13882 error ("too few template-parameter-lists");
13883 return false;
13884 }
13885 /* If there are the same number of template classes and parameter
13886 lists, that's OK. */
13887 if (parser->num_template_parameter_lists == num_templates)
13888 return true;
13889 /* If there are more, but only one more, then we are referring to a
13890 member template. That's OK too. */
13891 if (parser->num_template_parameter_lists == num_templates + 1)
13892 return true;
13893 /* Otherwise, there are too many template parameter lists. We have
13894 something like:
13895
13896 template <class T> template <class U> void S::f(); */
13897 error ("too many template-parameter-lists");
13898 return false;
13899}
13900
13901/* Parse a binary-expression of the general form:
13902
13903 binary-expression:
13904 <expr>
13905 binary-expression <token> <expr>
13906
13907 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13908 to parser the <expr>s. If the first production is used, then the
13909 value returned by FN is returned directly. Otherwise, a node with
13910 the indicated EXPR_TYPE is returned, with operands corresponding to
13911 the two sub-expressions. */
13912
13913static tree
13914cp_parser_binary_expression (parser, token_tree_map, fn)
13915 cp_parser *parser;
39b1af70 13916 const cp_parser_token_tree_map token_tree_map;
a723baf1
MM
13917 cp_parser_expression_fn fn;
13918{
13919 tree lhs;
13920
13921 /* Parse the first expression. */
13922 lhs = (*fn) (parser);
13923 /* Now, look for more expressions. */
13924 while (true)
13925 {
13926 cp_token *token;
39b1af70 13927 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13928 tree rhs;
13929
13930 /* Peek at the next token. */
13931 token = cp_lexer_peek_token (parser->lexer);
13932 /* If the token is `>', and that's not an operator at the
13933 moment, then we're done. */
13934 if (token->type == CPP_GREATER
13935 && !parser->greater_than_is_operator_p)
13936 break;
13937 /* If we find one of the tokens we want, build the correspoding
13938 tree representation. */
13939 for (map_node = token_tree_map;
13940 map_node->token_type != CPP_EOF;
13941 ++map_node)
13942 if (map_node->token_type == token->type)
13943 {
13944 /* Consume the operator token. */
13945 cp_lexer_consume_token (parser->lexer);
13946 /* Parse the right-hand side of the expression. */
13947 rhs = (*fn) (parser);
13948 /* Build the binary tree node. */
13949 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13950 break;
13951 }
13952
13953 /* If the token wasn't one of the ones we want, we're done. */
13954 if (map_node->token_type == CPP_EOF)
13955 break;
13956 }
13957
13958 return lhs;
13959}
13960
13961/* Parse an optional `::' token indicating that the following name is
13962 from the global namespace. If so, PARSER->SCOPE is set to the
13963 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13964 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13965 Returns the new value of PARSER->SCOPE, if the `::' token is
13966 present, and NULL_TREE otherwise. */
13967
13968static tree
13969cp_parser_global_scope_opt (parser, current_scope_valid_p)
13970 cp_parser *parser;
13971 bool current_scope_valid_p;
13972{
13973 cp_token *token;
13974
13975 /* Peek at the next token. */
13976 token = cp_lexer_peek_token (parser->lexer);
13977 /* If we're looking at a `::' token then we're starting from the
13978 global namespace, not our current location. */
13979 if (token->type == CPP_SCOPE)
13980 {
13981 /* Consume the `::' token. */
13982 cp_lexer_consume_token (parser->lexer);
13983 /* Set the SCOPE so that we know where to start the lookup. */
13984 parser->scope = global_namespace;
13985 parser->qualifying_scope = global_namespace;
13986 parser->object_scope = NULL_TREE;
13987
13988 return parser->scope;
13989 }
13990 else if (!current_scope_valid_p)
13991 {
13992 parser->scope = NULL_TREE;
13993 parser->qualifying_scope = NULL_TREE;
13994 parser->object_scope = NULL_TREE;
13995 }
13996
13997 return NULL_TREE;
13998}
13999
14000/* Returns TRUE if the upcoming token sequence is the start of a
14001 constructor declarator. If FRIEND_P is true, the declarator is
14002 preceded by the `friend' specifier. */
14003
14004static bool
14005cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14006{
14007 bool constructor_p;
14008 tree type_decl = NULL_TREE;
14009 bool nested_name_p;
2050a1bb
MM
14010 cp_token *next_token;
14011
14012 /* The common case is that this is not a constructor declarator, so
14013 try to avoid doing lots of work if at all possible. */
14014 next_token = cp_lexer_peek_token (parser->lexer);
14015 if (next_token->type != CPP_NAME
14016 && next_token->type != CPP_SCOPE
14017 && next_token->type != CPP_NESTED_NAME_SPECIFIER
14018 && next_token->type != CPP_TEMPLATE_ID)
14019 return false;
a723baf1
MM
14020
14021 /* Parse tentatively; we are going to roll back all of the tokens
14022 consumed here. */
14023 cp_parser_parse_tentatively (parser);
14024 /* Assume that we are looking at a constructor declarator. */
14025 constructor_p = true;
14026 /* Look for the optional `::' operator. */
14027 cp_parser_global_scope_opt (parser,
14028 /*current_scope_valid_p=*/false);
14029 /* Look for the nested-name-specifier. */
14030 nested_name_p
14031 = (cp_parser_nested_name_specifier_opt (parser,
14032 /*typename_keyword_p=*/false,
14033 /*check_dependency_p=*/false,
14034 /*type_p=*/false)
14035 != NULL_TREE);
14036 /* Outside of a class-specifier, there must be a
14037 nested-name-specifier. */
14038 if (!nested_name_p &&
14039 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14040 || friend_p))
14041 constructor_p = false;
14042 /* If we still think that this might be a constructor-declarator,
14043 look for a class-name. */
14044 if (constructor_p)
14045 {
14046 /* If we have:
14047
14048 template <typename T> struct S { S(); }
14049 template <typename T> S<T>::S ();
14050
14051 we must recognize that the nested `S' names a class.
14052 Similarly, for:
14053
14054 template <typename T> S<T>::S<T> ();
14055
14056 we must recognize that the nested `S' names a template. */
14057 type_decl = cp_parser_class_name (parser,
14058 /*typename_keyword_p=*/false,
14059 /*template_keyword_p=*/false,
14060 /*type_p=*/false,
14061 /*check_access_p=*/false,
14062 /*check_dependency_p=*/false,
14063 /*class_head_p=*/false);
14064 /* If there was no class-name, then this is not a constructor. */
14065 constructor_p = !cp_parser_error_occurred (parser);
14066 }
14067 /* If we're still considering a constructor, we have to see a `(',
14068 to begin the parameter-declaration-clause, followed by either a
14069 `)', an `...', or a decl-specifier. We need to check for a
14070 type-specifier to avoid being fooled into thinking that:
14071
14072 S::S (f) (int);
14073
14074 is a constructor. (It is actually a function named `f' that
14075 takes one parameter (of type `int') and returns a value of type
14076 `S::S'. */
14077 if (constructor_p
14078 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14079 {
14080 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14081 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14082 && !cp_parser_storage_class_specifier_opt (parser))
14083 {
14084 if (current_class_type
14085 && !same_type_p (current_class_type, TREE_TYPE (type_decl)))
14086 /* The constructor for one class cannot be declared inside
14087 another. */
14088 constructor_p = false;
14089 else
14090 {
14091 tree type;
14092
14093 /* Names appearing in the type-specifier should be looked up
14094 in the scope of the class. */
14095 if (current_class_type)
14096 type = NULL_TREE;
14097 else
14098 {
14099 type = TREE_TYPE (type_decl);
14100 if (TREE_CODE (type) == TYPENAME_TYPE)
14101 type = cp_parser_resolve_typename_type (parser, type);
14102 push_scope (type);
14103 }
14104 /* Look for the type-specifier. */
14105 cp_parser_type_specifier (parser,
14106 CP_PARSER_FLAGS_NONE,
14107 /*is_friend=*/false,
14108 /*is_declarator=*/true,
14109 /*declares_class_or_enum=*/NULL,
14110 /*is_cv_qualifier=*/NULL);
14111 /* Leave the scope of the class. */
14112 if (type)
14113 pop_scope (type);
14114
14115 constructor_p = !cp_parser_error_occurred (parser);
14116 }
14117 }
14118 }
14119 else
14120 constructor_p = false;
14121 /* We did not really want to consume any tokens. */
14122 cp_parser_abort_tentative_parse (parser);
14123
14124 return constructor_p;
14125}
14126
14127/* Parse the definition of the function given by the DECL_SPECIFIERS,
14128 ATTRIBUTES, and DECLARATOR. The ACCESS_CHECKS have been deferred;
14129 they must be performed once we are in the scope of the function.
14130
14131 Returns the function defined. */
14132
14133static tree
14134cp_parser_function_definition_from_specifiers_and_declarator
14135 (parser, decl_specifiers, attributes, declarator, access_checks)
14136 cp_parser *parser;
14137 tree decl_specifiers;
14138 tree attributes;
14139 tree declarator;
14140 tree access_checks;
14141{
14142 tree fn;
14143 bool success_p;
14144
14145 /* Begin the function-definition. */
14146 success_p = begin_function_definition (decl_specifiers,
14147 attributes,
14148 declarator);
14149
14150 /* If there were names looked up in the decl-specifier-seq that we
14151 did not check, check them now. We must wait until we are in the
14152 scope of the function to perform the checks, since the function
14153 might be a friend. */
14154 cp_parser_perform_deferred_access_checks (access_checks);
14155
14156 if (!success_p)
14157 {
14158 /* If begin_function_definition didn't like the definition, skip
14159 the entire function. */
14160 error ("invalid function declaration");
14161 cp_parser_skip_to_end_of_block_or_statement (parser);
14162 fn = error_mark_node;
14163 }
14164 else
14165 fn = cp_parser_function_definition_after_declarator (parser,
14166 /*inline_p=*/false);
14167
14168 return fn;
14169}
14170
14171/* Parse the part of a function-definition that follows the
14172 declarator. INLINE_P is TRUE iff this function is an inline
14173 function defined with a class-specifier.
14174
14175 Returns the function defined. */
14176
14177static tree
14178cp_parser_function_definition_after_declarator (parser,
14179 inline_p)
14180 cp_parser *parser;
14181 bool inline_p;
14182{
14183 tree fn;
14184 bool ctor_initializer_p = false;
14185 bool saved_in_unbraced_linkage_specification_p;
14186 unsigned saved_num_template_parameter_lists;
14187
14188 /* If the next token is `return', then the code may be trying to
14189 make use of the "named return value" extension that G++ used to
14190 support. */
14191 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14192 {
14193 /* Consume the `return' keyword. */
14194 cp_lexer_consume_token (parser->lexer);
14195 /* Look for the identifier that indicates what value is to be
14196 returned. */
14197 cp_parser_identifier (parser);
14198 /* Issue an error message. */
14199 error ("named return values are no longer supported");
14200 /* Skip tokens until we reach the start of the function body. */
14201 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14202 cp_lexer_consume_token (parser->lexer);
14203 }
14204 /* The `extern' in `extern "C" void f () { ... }' does not apply to
14205 anything declared inside `f'. */
14206 saved_in_unbraced_linkage_specification_p
14207 = parser->in_unbraced_linkage_specification_p;
14208 parser->in_unbraced_linkage_specification_p = false;
14209 /* Inside the function, surrounding template-parameter-lists do not
14210 apply. */
14211 saved_num_template_parameter_lists
14212 = parser->num_template_parameter_lists;
14213 parser->num_template_parameter_lists = 0;
14214 /* If the next token is `try', then we are looking at a
14215 function-try-block. */
14216 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14217 ctor_initializer_p = cp_parser_function_try_block (parser);
14218 /* A function-try-block includes the function-body, so we only do
14219 this next part if we're not processing a function-try-block. */
14220 else
14221 ctor_initializer_p
14222 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14223
14224 /* Finish the function. */
14225 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14226 (inline_p ? 2 : 0));
14227 /* Generate code for it, if necessary. */
14228 expand_body (fn);
14229 /* Restore the saved values. */
14230 parser->in_unbraced_linkage_specification_p
14231 = saved_in_unbraced_linkage_specification_p;
14232 parser->num_template_parameter_lists
14233 = saved_num_template_parameter_lists;
14234
14235 return fn;
14236}
14237
14238/* Parse a template-declaration, assuming that the `export' (and
14239 `extern') keywords, if present, has already been scanned. MEMBER_P
14240 is as for cp_parser_template_declaration. */
14241
14242static void
14243cp_parser_template_declaration_after_export (parser, member_p)
14244 cp_parser *parser;
14245 bool member_p;
14246{
14247 tree decl = NULL_TREE;
14248 tree parameter_list;
14249 bool friend_p = false;
14250
14251 /* Look for the `template' keyword. */
14252 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14253 return;
14254
14255 /* And the `<'. */
14256 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14257 return;
14258
14259 /* Parse the template parameters. */
14260 begin_template_parm_list ();
14261 /* If the next token is `>', then we have an invalid
14262 specialization. Rather than complain about an invalid template
14263 parameter, issue an error message here. */
14264 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14265 {
14266 cp_parser_error (parser, "invalid explicit specialization");
14267 parameter_list = NULL_TREE;
14268 }
14269 else
14270 parameter_list = cp_parser_template_parameter_list (parser);
14271 parameter_list = end_template_parm_list (parameter_list);
14272 /* Look for the `>'. */
14273 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14274 /* We just processed one more parameter list. */
14275 ++parser->num_template_parameter_lists;
14276 /* If the next token is `template', there are more template
14277 parameters. */
14278 if (cp_lexer_next_token_is_keyword (parser->lexer,
14279 RID_TEMPLATE))
14280 cp_parser_template_declaration_after_export (parser, member_p);
14281 else
14282 {
14283 decl = cp_parser_single_declaration (parser,
14284 member_p,
14285 &friend_p);
14286
14287 /* If this is a member template declaration, let the front
14288 end know. */
14289 if (member_p && !friend_p && decl)
14290 decl = finish_member_template_decl (decl);
14291 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14292 make_friend_class (current_class_type, TREE_TYPE (decl));
14293 }
14294 /* We are done with the current parameter list. */
14295 --parser->num_template_parameter_lists;
14296
14297 /* Finish up. */
14298 finish_template_decl (parameter_list);
14299
14300 /* Register member declarations. */
14301 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14302 finish_member_declaration (decl);
14303
14304 /* If DECL is a function template, we must return to parse it later.
14305 (Even though there is no definition, there might be default
14306 arguments that need handling.) */
14307 if (member_p && decl
14308 && (TREE_CODE (decl) == FUNCTION_DECL
14309 || DECL_FUNCTION_TEMPLATE_P (decl)))
14310 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 14311 = tree_cons (NULL_TREE, decl,
a723baf1
MM
14312 TREE_VALUE (parser->unparsed_functions_queues));
14313}
14314
14315/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14316 `function-definition' sequence. MEMBER_P is true, this declaration
14317 appears in a class scope.
14318
14319 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
14320 *FRIEND_P is set to TRUE iff the declaration is a friend. */
14321
14322static tree
14323cp_parser_single_declaration (parser,
14324 member_p,
14325 friend_p)
14326 cp_parser *parser;
14327 bool member_p;
14328 bool *friend_p;
14329{
14330 bool declares_class_or_enum;
14331 tree decl = NULL_TREE;
14332 tree decl_specifiers;
14333 tree attributes;
14334 tree access_checks;
14335
14336 /* Parse the dependent declaration. We don't know yet
14337 whether it will be a function-definition. */
14338 cp_parser_parse_tentatively (parser);
14339 /* Defer access checks until we know what is being declared. */
14340 cp_parser_start_deferring_access_checks (parser);
14341 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14342 alternative. */
14343 decl_specifiers
14344 = cp_parser_decl_specifier_seq (parser,
14345 CP_PARSER_FLAGS_OPTIONAL,
14346 &attributes,
14347 &declares_class_or_enum);
14348 /* Gather up the access checks that occurred the
14349 decl-specifier-seq. */
14350 access_checks = cp_parser_stop_deferring_access_checks (parser);
14351 /* Check for the declaration of a template class. */
14352 if (declares_class_or_enum)
14353 {
14354 if (cp_parser_declares_only_class_p (parser))
14355 {
14356 decl = shadow_tag (decl_specifiers);
14357 if (decl)
14358 decl = TYPE_NAME (decl);
14359 else
14360 decl = error_mark_node;
14361 }
14362 }
14363 else
14364 decl = NULL_TREE;
14365 /* If it's not a template class, try for a template function. If
14366 the next token is a `;', then this declaration does not declare
14367 anything. But, if there were errors in the decl-specifiers, then
14368 the error might well have come from an attempted class-specifier.
14369 In that case, there's no need to warn about a missing declarator. */
14370 if (!decl
14371 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14372 || !value_member (error_mark_node, decl_specifiers)))
14373 decl = cp_parser_init_declarator (parser,
14374 decl_specifiers,
14375 attributes,
14376 access_checks,
14377 /*function_definition_allowed_p=*/false,
14378 member_p,
14379 /*function_definition_p=*/NULL);
14380 /* Clear any current qualification; whatever comes next is the start
14381 of something new. */
14382 parser->scope = NULL_TREE;
14383 parser->qualifying_scope = NULL_TREE;
14384 parser->object_scope = NULL_TREE;
14385 /* Look for a trailing `;' after the declaration. */
14386 if (!cp_parser_require (parser, CPP_SEMICOLON, "expected `;'")
14387 && cp_parser_committed_to_tentative_parse (parser))
14388 cp_parser_skip_to_end_of_block_or_statement (parser);
14389 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14390 if (cp_parser_parse_definitely (parser))
14391 {
14392 if (friend_p)
14393 *friend_p = cp_parser_friend_p (decl_specifiers);
14394 }
14395 /* Otherwise, try a function-definition. */
14396 else
14397 decl = cp_parser_function_definition (parser, friend_p);
14398
14399 return decl;
14400}
14401
14402/* Parse a functional cast to TYPE. Returns an expression
14403 representing the cast. */
14404
14405static tree
14406cp_parser_functional_cast (parser, type)
14407 cp_parser *parser;
14408 tree type;
14409{
14410 tree expression_list;
14411
14412 /* Look for the opening `('. */
14413 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14414 return error_mark_node;
14415 /* If the next token is not an `)', there are arguments to the
14416 cast. */
14417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14418 expression_list = cp_parser_expression_list (parser);
14419 else
14420 expression_list = NULL_TREE;
14421 /* Look for the closing `)'. */
14422 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14423
14424 return build_functional_cast (type, expression_list);
14425}
14426
14427/* MEMBER_FUNCTION is a member function, or a friend. If default
14428 arguments, or the body of the function have not yet been parsed,
14429 parse them now. */
14430
14431static void
14432cp_parser_late_parsing_for_member (parser, member_function)
14433 cp_parser *parser;
14434 tree member_function;
14435{
14436 cp_lexer *saved_lexer;
14437
14438 /* If this member is a template, get the underlying
14439 FUNCTION_DECL. */
14440 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14441 member_function = DECL_TEMPLATE_RESULT (member_function);
14442
14443 /* There should not be any class definitions in progress at this
14444 point; the bodies of members are only parsed outside of all class
14445 definitions. */
14446 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14447 /* While we're parsing the member functions we might encounter more
14448 classes. We want to handle them right away, but we don't want
14449 them getting mixed up with functions that are currently in the
14450 queue. */
14451 parser->unparsed_functions_queues
14452 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14453
14454 /* Make sure that any template parameters are in scope. */
14455 maybe_begin_member_template_processing (member_function);
14456
a723baf1
MM
14457 /* If the body of the function has not yet been parsed, parse it
14458 now. */
14459 if (DECL_PENDING_INLINE_P (member_function))
14460 {
14461 tree function_scope;
14462 cp_token_cache *tokens;
14463
14464 /* The function is no longer pending; we are processing it. */
14465 tokens = DECL_PENDING_INLINE_INFO (member_function);
14466 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14467 DECL_PENDING_INLINE_P (member_function) = 0;
14468 /* If this was an inline function in a local class, enter the scope
14469 of the containing function. */
14470 function_scope = decl_function_context (member_function);
14471 if (function_scope)
14472 push_function_context_to (function_scope);
14473
14474 /* Save away the current lexer. */
14475 saved_lexer = parser->lexer;
14476 /* Make a new lexer to feed us the tokens saved for this function. */
14477 parser->lexer = cp_lexer_new_from_tokens (tokens);
14478 parser->lexer->next = saved_lexer;
14479
14480 /* Set the current source position to be the location of the first
14481 token in the saved inline body. */
3466b292 14482 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14483
14484 /* Let the front end know that we going to be defining this
14485 function. */
14486 start_function (NULL_TREE, member_function, NULL_TREE,
14487 SF_PRE_PARSED | SF_INCLASS_INLINE);
14488
14489 /* Now, parse the body of the function. */
14490 cp_parser_function_definition_after_declarator (parser,
14491 /*inline_p=*/true);
14492
14493 /* Leave the scope of the containing function. */
14494 if (function_scope)
14495 pop_function_context_from (function_scope);
14496 /* Restore the lexer. */
14497 parser->lexer = saved_lexer;
14498 }
14499
14500 /* Remove any template parameters from the symbol table. */
14501 maybe_end_member_template_processing ();
14502
14503 /* Restore the queue. */
14504 parser->unparsed_functions_queues
14505 = TREE_CHAIN (parser->unparsed_functions_queues);
14506}
14507
8218bd34
MM
14508/* FN is a FUNCTION_DECL which may contains a parameter with an
14509 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
14510
14511static void
8218bd34 14512cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
14513{
14514 cp_lexer *saved_lexer;
14515 cp_token_cache *tokens;
14516 bool saved_local_variables_forbidden_p;
14517 tree parameters;
8218bd34
MM
14518
14519 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
14520 parameters;
14521 parameters = TREE_CHAIN (parameters))
14522 {
14523 if (!TREE_PURPOSE (parameters)
14524 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14525 continue;
14526
14527 /* Save away the current lexer. */
14528 saved_lexer = parser->lexer;
14529 /* Create a new one, using the tokens we have saved. */
14530 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14531 parser->lexer = cp_lexer_new_from_tokens (tokens);
14532
14533 /* Set the current source position to be the location of the
14534 first token in the default argument. */
3466b292 14535 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14536
14537 /* Local variable names (and the `this' keyword) may not appear
14538 in a default argument. */
14539 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14540 parser->local_variables_forbidden_p = true;
14541 /* Parse the assignment-expression. */
8218bd34
MM
14542 if (DECL_CONTEXT (fn))
14543 push_nested_class (DECL_CONTEXT (fn), 1);
a723baf1 14544 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
8218bd34 14545 if (DECL_CONTEXT (fn))
e5976695 14546 pop_nested_class ();
a723baf1
MM
14547
14548 /* Restore saved state. */
14549 parser->lexer = saved_lexer;
14550 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14551 }
14552}
14553
14554/* Parse the operand of `sizeof' (or a similar operator). Returns
14555 either a TYPE or an expression, depending on the form of the
14556 input. The KEYWORD indicates which kind of expression we have
14557 encountered. */
14558
14559static tree
14560cp_parser_sizeof_operand (parser, keyword)
14561 cp_parser *parser;
14562 enum rid keyword;
14563{
14564 static const char *format;
14565 tree expr = NULL_TREE;
14566 const char *saved_message;
14567 bool saved_constant_expression_p;
14568
14569 /* Initialize FORMAT the first time we get here. */
14570 if (!format)
14571 format = "types may not be defined in `%s' expressions";
14572
14573 /* Types cannot be defined in a `sizeof' expression. Save away the
14574 old message. */
14575 saved_message = parser->type_definition_forbidden_message;
14576 /* And create the new one. */
14577 parser->type_definition_forbidden_message
14578 = ((const char *)
14579 xmalloc (strlen (format)
14580 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14581 + 1 /* `\0' */));
14582 sprintf ((char *) parser->type_definition_forbidden_message,
14583 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14584
14585 /* The restrictions on constant-expressions do not apply inside
14586 sizeof expressions. */
14587 saved_constant_expression_p = parser->constant_expression_p;
14588 parser->constant_expression_p = false;
14589
3beb3abf
MM
14590 /* Do not actually evaluate the expression. */
14591 ++skip_evaluation;
a723baf1
MM
14592 /* If it's a `(', then we might be looking at the type-id
14593 construction. */
14594 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14595 {
14596 tree type;
14597
14598 /* We can't be sure yet whether we're looking at a type-id or an
14599 expression. */
14600 cp_parser_parse_tentatively (parser);
14601 /* Consume the `('. */
14602 cp_lexer_consume_token (parser->lexer);
14603 /* Parse the type-id. */
14604 type = cp_parser_type_id (parser);
14605 /* Now, look for the trailing `)'. */
14606 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14607 /* If all went well, then we're done. */
14608 if (cp_parser_parse_definitely (parser))
14609 {
14610 /* Build a list of decl-specifiers; right now, we have only
14611 a single type-specifier. */
14612 type = build_tree_list (NULL_TREE,
14613 type);
14614
14615 /* Call grokdeclarator to figure out what type this is. */
14616 expr = grokdeclarator (NULL_TREE,
14617 type,
14618 TYPENAME,
14619 /*initialized=*/0,
14620 /*attrlist=*/NULL);
14621 }
14622 }
14623
14624 /* If the type-id production did not work out, then we must be
14625 looking at the unary-expression production. */
14626 if (!expr)
14627 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
14628 /* Go back to evaluating expressions. */
14629 --skip_evaluation;
a723baf1
MM
14630
14631 /* Free the message we created. */
14632 free ((char *) parser->type_definition_forbidden_message);
14633 /* And restore the old one. */
14634 parser->type_definition_forbidden_message = saved_message;
14635 parser->constant_expression_p = saved_constant_expression_p;
14636
14637 return expr;
14638}
14639
14640/* If the current declaration has no declarator, return true. */
14641
14642static bool
14643cp_parser_declares_only_class_p (cp_parser *parser)
14644{
14645 /* If the next token is a `;' or a `,' then there is no
14646 declarator. */
14647 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14648 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14649}
14650
14651/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14652 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14653
14654static bool
14655cp_parser_friend_p (decl_specifiers)
14656 tree decl_specifiers;
14657{
14658 while (decl_specifiers)
14659 {
14660 /* See if this decl-specifier is `friend'. */
14661 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14662 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14663 return true;
14664
14665 /* Go on to the next decl-specifier. */
14666 decl_specifiers = TREE_CHAIN (decl_specifiers);
14667 }
14668
14669 return false;
14670}
14671
14672/* If the next token is of the indicated TYPE, consume it. Otherwise,
14673 issue an error message indicating that TOKEN_DESC was expected.
14674
14675 Returns the token consumed, if the token had the appropriate type.
14676 Otherwise, returns NULL. */
14677
14678static cp_token *
14679cp_parser_require (parser, type, token_desc)
14680 cp_parser *parser;
14681 enum cpp_ttype type;
14682 const char *token_desc;
14683{
14684 if (cp_lexer_next_token_is (parser->lexer, type))
14685 return cp_lexer_consume_token (parser->lexer);
14686 else
14687 {
e5976695
MM
14688 /* Output the MESSAGE -- unless we're parsing tentatively. */
14689 if (!cp_parser_simulate_error (parser))
14690 error ("expected %s", token_desc);
a723baf1
MM
14691 return NULL;
14692 }
14693}
14694
14695/* Like cp_parser_require, except that tokens will be skipped until
14696 the desired token is found. An error message is still produced if
14697 the next token is not as expected. */
14698
14699static void
14700cp_parser_skip_until_found (parser, type, token_desc)
14701 cp_parser *parser;
14702 enum cpp_ttype type;
14703 const char *token_desc;
14704{
14705 cp_token *token;
14706 unsigned nesting_depth = 0;
14707
14708 if (cp_parser_require (parser, type, token_desc))
14709 return;
14710
14711 /* Skip tokens until the desired token is found. */
14712 while (true)
14713 {
14714 /* Peek at the next token. */
14715 token = cp_lexer_peek_token (parser->lexer);
14716 /* If we've reached the token we want, consume it and
14717 stop. */
14718 if (token->type == type && !nesting_depth)
14719 {
14720 cp_lexer_consume_token (parser->lexer);
14721 return;
14722 }
14723 /* If we've run out of tokens, stop. */
14724 if (token->type == CPP_EOF)
14725 return;
14726 if (token->type == CPP_OPEN_BRACE
14727 || token->type == CPP_OPEN_PAREN
14728 || token->type == CPP_OPEN_SQUARE)
14729 ++nesting_depth;
14730 else if (token->type == CPP_CLOSE_BRACE
14731 || token->type == CPP_CLOSE_PAREN
14732 || token->type == CPP_CLOSE_SQUARE)
14733 {
14734 if (nesting_depth-- == 0)
14735 return;
14736 }
14737 /* Consume this token. */
14738 cp_lexer_consume_token (parser->lexer);
14739 }
14740}
14741
14742/* If the next token is the indicated keyword, consume it. Otherwise,
14743 issue an error message indicating that TOKEN_DESC was expected.
14744
14745 Returns the token consumed, if the token had the appropriate type.
14746 Otherwise, returns NULL. */
14747
14748static cp_token *
14749cp_parser_require_keyword (parser, keyword, token_desc)
14750 cp_parser *parser;
14751 enum rid keyword;
14752 const char *token_desc;
14753{
14754 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14755
14756 if (token && token->keyword != keyword)
14757 {
14758 dyn_string_t error_msg;
14759
14760 /* Format the error message. */
14761 error_msg = dyn_string_new (0);
14762 dyn_string_append_cstr (error_msg, "expected ");
14763 dyn_string_append_cstr (error_msg, token_desc);
14764 cp_parser_error (parser, error_msg->s);
14765 dyn_string_delete (error_msg);
14766 return NULL;
14767 }
14768
14769 return token;
14770}
14771
14772/* Returns TRUE iff TOKEN is a token that can begin the body of a
14773 function-definition. */
14774
14775static bool
14776cp_parser_token_starts_function_definition_p (token)
14777 cp_token *token;
14778{
14779 return (/* An ordinary function-body begins with an `{'. */
14780 token->type == CPP_OPEN_BRACE
14781 /* A ctor-initializer begins with a `:'. */
14782 || token->type == CPP_COLON
14783 /* A function-try-block begins with `try'. */
14784 || token->keyword == RID_TRY
14785 /* The named return value extension begins with `return'. */
14786 || token->keyword == RID_RETURN);
14787}
14788
14789/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14790 definition. */
14791
14792static bool
14793cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14794{
14795 cp_token *token;
14796
14797 token = cp_lexer_peek_token (parser->lexer);
14798 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14799}
14800
14801/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14802 or none_type otherwise. */
14803
14804static enum tag_types
14805cp_parser_token_is_class_key (token)
14806 cp_token *token;
14807{
14808 switch (token->keyword)
14809 {
14810 case RID_CLASS:
14811 return class_type;
14812 case RID_STRUCT:
14813 return record_type;
14814 case RID_UNION:
14815 return union_type;
14816
14817 default:
14818 return none_type;
14819 }
14820}
14821
14822/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14823
14824static void
14825cp_parser_check_class_key (enum tag_types class_key, tree type)
14826{
14827 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14828 pedwarn ("`%s' tag used in naming `%#T'",
14829 class_key == union_type ? "union"
14830 : class_key == record_type ? "struct" : "class",
14831 type);
14832}
14833
14834/* Look for the `template' keyword, as a syntactic disambiguator.
14835 Return TRUE iff it is present, in which case it will be
14836 consumed. */
14837
14838static bool
14839cp_parser_optional_template_keyword (cp_parser *parser)
14840{
14841 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14842 {
14843 /* The `template' keyword can only be used within templates;
14844 outside templates the parser can always figure out what is a
14845 template and what is not. */
14846 if (!processing_template_decl)
14847 {
14848 error ("`template' (as a disambiguator) is only allowed "
14849 "within templates");
14850 /* If this part of the token stream is rescanned, the same
14851 error message would be generated. So, we purge the token
14852 from the stream. */
14853 cp_lexer_purge_token (parser->lexer);
14854 return false;
14855 }
14856 else
14857 {
14858 /* Consume the `template' keyword. */
14859 cp_lexer_consume_token (parser->lexer);
14860 return true;
14861 }
14862 }
14863
14864 return false;
14865}
14866
2050a1bb
MM
14867/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14868 set PARSER->SCOPE, and perform other related actions. */
14869
14870static void
14871cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14872{
14873 tree value;
14874 tree check;
14875
14876 /* Get the stored value. */
14877 value = cp_lexer_consume_token (parser->lexer)->value;
14878 /* Perform any access checks that were deferred. */
14879 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14880 cp_parser_defer_access_check (parser,
14881 TREE_PURPOSE (check),
14882 TREE_VALUE (check));
14883 /* Set the scope from the stored value. */
14884 parser->scope = TREE_VALUE (value);
14885 parser->qualifying_scope = TREE_TYPE (value);
14886 parser->object_scope = NULL_TREE;
14887}
14888
a723baf1
MM
14889/* Add tokens to CACHE until an non-nested END token appears. */
14890
14891static void
14892cp_parser_cache_group (cp_parser *parser,
14893 cp_token_cache *cache,
14894 enum cpp_ttype end,
14895 unsigned depth)
14896{
14897 while (true)
14898 {
14899 cp_token *token;
14900
14901 /* Abort a parenthesized expression if we encounter a brace. */
14902 if ((end == CPP_CLOSE_PAREN || depth == 0)
14903 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14904 return;
14905 /* Consume the next token. */
14906 token = cp_lexer_consume_token (parser->lexer);
14907 /* If we've reached the end of the file, stop. */
14908 if (token->type == CPP_EOF)
14909 return;
14910 /* Add this token to the tokens we are saving. */
14911 cp_token_cache_push_token (cache, token);
14912 /* See if it starts a new group. */
14913 if (token->type == CPP_OPEN_BRACE)
14914 {
14915 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14916 if (depth == 0)
14917 return;
14918 }
14919 else if (token->type == CPP_OPEN_PAREN)
14920 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14921 else if (token->type == end)
14922 return;
14923 }
14924}
14925
14926/* Begin parsing tentatively. We always save tokens while parsing
14927 tentatively so that if the tentative parsing fails we can restore the
14928 tokens. */
14929
14930static void
14931cp_parser_parse_tentatively (parser)
14932 cp_parser *parser;
14933{
14934 /* Enter a new parsing context. */
14935 parser->context = cp_parser_context_new (parser->context);
14936 /* Begin saving tokens. */
14937 cp_lexer_save_tokens (parser->lexer);
14938 /* In order to avoid repetitive access control error messages,
14939 access checks are queued up until we are no longer parsing
14940 tentatively. */
14941 cp_parser_start_deferring_access_checks (parser);
14942}
14943
14944/* Commit to the currently active tentative parse. */
14945
14946static void
14947cp_parser_commit_to_tentative_parse (parser)
14948 cp_parser *parser;
14949{
14950 cp_parser_context *context;
14951 cp_lexer *lexer;
14952
14953 /* Mark all of the levels as committed. */
14954 lexer = parser->lexer;
14955 for (context = parser->context; context->next; context = context->next)
14956 {
14957 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14958 break;
14959 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14960 while (!cp_lexer_saving_tokens (lexer))
14961 lexer = lexer->next;
14962 cp_lexer_commit_tokens (lexer);
14963 }
14964}
14965
14966/* Abort the currently active tentative parse. All consumed tokens
14967 will be rolled back, and no diagnostics will be issued. */
14968
14969static void
14970cp_parser_abort_tentative_parse (parser)
14971 cp_parser *parser;
14972{
14973 cp_parser_simulate_error (parser);
14974 /* Now, pretend that we want to see if the construct was
14975 successfully parsed. */
14976 cp_parser_parse_definitely (parser);
14977}
14978
14979/* Stop parsing tentatively. If a parse error has ocurred, restore the
14980 token stream. Otherwise, commit to the tokens we have consumed.
14981 Returns true if no error occurred; false otherwise. */
14982
14983static bool
14984cp_parser_parse_definitely (parser)
14985 cp_parser *parser;
14986{
14987 bool error_occurred;
14988 cp_parser_context *context;
14989
14990 /* Remember whether or not an error ocurred, since we are about to
14991 destroy that information. */
14992 error_occurred = cp_parser_error_occurred (parser);
14993 /* Remove the topmost context from the stack. */
14994 context = parser->context;
14995 parser->context = context->next;
14996 /* If no parse errors occurred, commit to the tentative parse. */
14997 if (!error_occurred)
14998 {
14999 /* Commit to the tokens read tentatively, unless that was
15000 already done. */
15001 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15002 cp_lexer_commit_tokens (parser->lexer);
15003 if (!parser->context->deferring_access_checks_p)
15004 /* If in the parent context we are not deferring checks, then
15005 these perform these checks now. */
15006 (cp_parser_perform_deferred_access_checks
15007 (context->deferred_access_checks));
15008 else
15009 /* Any lookups that were deferred during the tentative parse are
15010 still deferred. */
15011 parser->context->deferred_access_checks
15012 = chainon (parser->context->deferred_access_checks,
15013 context->deferred_access_checks);
a723baf1
MM
15014 }
15015 /* Otherwise, if errors occurred, roll back our state so that things
15016 are just as they were before we began the tentative parse. */
15017 else
e5976695
MM
15018 cp_lexer_rollback_tokens (parser->lexer);
15019 /* Add the context to the front of the free list. */
15020 context->next = cp_parser_context_free_list;
15021 cp_parser_context_free_list = context;
15022
15023 return !error_occurred;
a723baf1
MM
15024}
15025
a723baf1
MM
15026/* Returns true if we are parsing tentatively -- but have decided that
15027 we will stick with this tentative parse, even if errors occur. */
15028
15029static bool
15030cp_parser_committed_to_tentative_parse (parser)
15031 cp_parser *parser;
15032{
15033 return (cp_parser_parsing_tentatively (parser)
15034 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15035}
15036
15037/* Returns non-zero iff an error has occurred during the most recent
15038 tentative parse. */
15039
15040static bool
15041cp_parser_error_occurred (parser)
15042 cp_parser *parser;
15043{
15044 return (cp_parser_parsing_tentatively (parser)
15045 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15046}
15047
15048/* Returns non-zero if GNU extensions are allowed. */
15049
15050static bool
15051cp_parser_allow_gnu_extensions_p (parser)
15052 cp_parser *parser;
15053{
15054 return parser->allow_gnu_extensions_p;
15055}
15056
15057\f
15058
15059/* The parser. */
15060
15061static GTY (()) cp_parser *the_parser;
15062
15063/* External interface. */
15064
15065/* Parse the entire translation unit. */
15066
15067int
15068yyparse ()
15069{
15070 bool error_occurred;
15071
15072 the_parser = cp_parser_new ();
15073 error_occurred = cp_parser_translation_unit (the_parser);
15074 the_parser = NULL;
17211ab5
GK
15075
15076 finish_file ();
a723baf1
MM
15077
15078 return error_occurred;
15079}
15080
15081/* Clean up after parsing the entire translation unit. */
15082
15083void
15084free_parser_stacks ()
15085{
15086 /* Nothing to do. */
15087}
15088
15089/* This variable must be provided by every front end. */
15090
15091int yydebug;
15092
15093#include "gt-cp-parser.h"