]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
Makefile.in (cgraph.o): Depend on gt-cgraph.h and varray.h.
[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 215static cp_lexer *cp_lexer_new_main
94edc4ab 216 (void);
a723baf1 217static cp_lexer *cp_lexer_new_from_tokens
94edc4ab 218 (struct cp_token_cache *);
a723baf1 219static int cp_lexer_saving_tokens
94edc4ab 220 (const cp_lexer *);
a723baf1 221static cp_token *cp_lexer_next_token
94edc4ab
NN
222 (cp_lexer *, cp_token *);
223static ptrdiff_t cp_lexer_token_difference
224 (cp_lexer *, cp_token *, cp_token *);
a723baf1 225static cp_token *cp_lexer_read_token
94edc4ab 226 (cp_lexer *);
a723baf1 227static void cp_lexer_maybe_grow_buffer
94edc4ab 228 (cp_lexer *);
a723baf1 229static void cp_lexer_get_preprocessor_token
94edc4ab 230 (cp_lexer *, cp_token *);
a723baf1 231static cp_token *cp_lexer_peek_token
94edc4ab 232 (cp_lexer *);
a723baf1 233static cp_token *cp_lexer_peek_nth_token
94edc4ab 234 (cp_lexer *, size_t);
f7b5ecd9 235static inline bool cp_lexer_next_token_is
94edc4ab 236 (cp_lexer *, enum cpp_ttype);
a723baf1 237static bool cp_lexer_next_token_is_not
94edc4ab 238 (cp_lexer *, enum cpp_ttype);
a723baf1 239static bool cp_lexer_next_token_is_keyword
94edc4ab
NN
240 (cp_lexer *, enum rid);
241static cp_token *cp_lexer_consume_token
242 (cp_lexer *);
a723baf1
MM
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
94edc4ab 248 (cp_lexer *);
a723baf1 249static void cp_lexer_commit_tokens
94edc4ab 250 (cp_lexer *);
a723baf1 251static void cp_lexer_rollback_tokens
94edc4ab 252 (cp_lexer *);
f7b5ecd9 253static inline void cp_lexer_set_source_position_from_token
94edc4ab 254 (cp_lexer *, const cp_token *);
a723baf1 255static void cp_lexer_print_token
94edc4ab 256 (FILE *, cp_token *);
f7b5ecd9 257static inline bool cp_lexer_debugging_p
94edc4ab 258 (cp_lexer *);
a723baf1 259static void cp_lexer_start_debugging
94edc4ab 260 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1 261static void cp_lexer_stop_debugging
94edc4ab 262 (cp_lexer *) ATTRIBUTE_UNUSED;
a723baf1
MM
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
94edc4ab
NN
401cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
402 const cp_token *token)
f7b5ecd9
MM
403{
404 /* Ideally, the source position information would not be a global
405 variable, but it is. */
406
407 /* Update the line number. */
408 if (token->type != CPP_EOF)
409 {
410 lineno = token->line_number;
411 input_filename = token->file_name;
412 }
a723baf1
MM
413}
414
415/* TOKEN points into the circular token buffer. Return a pointer to
416 the next token in the buffer. */
417
f7b5ecd9 418static inline cp_token *
94edc4ab 419cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
a723baf1
MM
420{
421 token++;
422 if (token == lexer->buffer_end)
423 token = lexer->buffer;
424 return token;
425}
426
f7b5ecd9
MM
427/* Non-zero if we are presently saving tokens. */
428
429static int
94edc4ab 430cp_lexer_saving_tokens (const cp_lexer* lexer)
f7b5ecd9
MM
431{
432 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
433}
434
a723baf1
MM
435/* Return a pointer to the token that is N tokens beyond TOKEN in the
436 buffer. */
437
438static cp_token *
439cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
440{
441 token += n;
442 if (token >= lexer->buffer_end)
443 token = lexer->buffer + (token - lexer->buffer_end);
444 return token;
445}
446
447/* Returns the number of times that START would have to be incremented
448 to reach FINISH. If START and FINISH are the same, returns zero. */
449
450static ptrdiff_t
94edc4ab 451cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
a723baf1
MM
452{
453 if (finish >= start)
454 return finish - start;
455 else
456 return ((lexer->buffer_end - lexer->buffer)
457 - (start - finish));
458}
459
460/* Obtain another token from the C preprocessor and add it to the
461 token buffer. Returns the newly read token. */
462
463static cp_token *
94edc4ab 464cp_lexer_read_token (cp_lexer* lexer)
a723baf1
MM
465{
466 cp_token *token;
467
468 /* Make sure there is room in the buffer. */
469 cp_lexer_maybe_grow_buffer (lexer);
470
471 /* If there weren't any tokens, then this one will be the first. */
472 if (!lexer->first_token)
473 lexer->first_token = lexer->last_token;
474 /* Similarly, if there were no available tokens, there is one now. */
475 if (!lexer->next_token)
476 lexer->next_token = lexer->last_token;
477
478 /* Figure out where we're going to store the new token. */
479 token = lexer->last_token;
480
481 /* Get a new token from the preprocessor. */
482 cp_lexer_get_preprocessor_token (lexer, token);
483
484 /* Increment LAST_TOKEN. */
485 lexer->last_token = cp_lexer_next_token (lexer, token);
486
487 /* The preprocessor does not yet do translation phase six, i.e., the
488 combination of adjacent string literals. Therefore, we do it
489 here. */
490 if (token->type == CPP_STRING || token->type == CPP_WSTRING)
491 {
492 ptrdiff_t delta;
493 int i;
494
495 /* When we grow the buffer, we may invalidate TOKEN. So, save
496 the distance from the beginning of the BUFFER so that we can
497 recaulate it. */
498 delta = cp_lexer_token_difference (lexer, lexer->buffer, token);
499 /* Make sure there is room in the buffer for another token. */
500 cp_lexer_maybe_grow_buffer (lexer);
501 /* Restore TOKEN. */
502 token = lexer->buffer;
503 for (i = 0; i < delta; ++i)
504 token = cp_lexer_next_token (lexer, token);
505
506 VARRAY_PUSH_TREE (lexer->string_tokens, token->value);
507 while (true)
508 {
509 /* Read the token after TOKEN. */
510 cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
511 /* See whether it's another string constant. */
512 if (lexer->last_token->type != token->type)
513 {
514 /* If not, then it will be the next real token. */
515 lexer->last_token = cp_lexer_next_token (lexer,
516 lexer->last_token);
517 break;
518 }
519
520 /* Chain the strings together. */
521 VARRAY_PUSH_TREE (lexer->string_tokens,
522 lexer->last_token->value);
523 }
524
525 /* Create a single STRING_CST. Curiously we have to call
526 combine_strings even if there is only a single string in
527 order to get the type set correctly. */
528 token->value = combine_strings (lexer->string_tokens);
529 VARRAY_CLEAR (lexer->string_tokens);
530 token->value = fix_string_type (token->value);
531 /* Strings should have type `const char []'. Right now, we will
532 have an ARRAY_TYPE that is constant rather than an array of
533 constant elements. */
534 if (flag_const_strings)
535 {
536 tree type;
537
538 /* Get the current type. It will be an ARRAY_TYPE. */
539 type = TREE_TYPE (token->value);
540 /* Use build_cplus_array_type to rebuild the array, thereby
541 getting the right type. */
542 type = build_cplus_array_type (TREE_TYPE (type),
543 TYPE_DOMAIN (type));
544 /* Reset the type of the token. */
545 TREE_TYPE (token->value) = type;
546 }
547 }
548
549 return token;
550}
551
552/* If the circular buffer is full, make it bigger. */
553
554static void
94edc4ab 555cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
a723baf1
MM
556{
557 /* If the buffer is full, enlarge it. */
558 if (lexer->last_token == lexer->first_token)
559 {
560 cp_token *new_buffer;
561 cp_token *old_buffer;
562 cp_token *new_first_token;
563 ptrdiff_t buffer_length;
564 size_t num_tokens_to_copy;
565
566 /* Remember the current buffer pointer. It will become invalid,
567 but we will need to do pointer arithmetic involving this
568 value. */
569 old_buffer = lexer->buffer;
570 /* Compute the current buffer size. */
571 buffer_length = lexer->buffer_end - lexer->buffer;
572 /* Allocate a buffer twice as big. */
573 new_buffer = ((cp_token *)
574 ggc_realloc (lexer->buffer,
575 2 * buffer_length * sizeof (cp_token)));
576
577 /* Because the buffer is circular, logically consecutive tokens
578 are not necessarily placed consecutively in memory.
579 Therefore, we must keep move the tokens that were before
580 FIRST_TOKEN to the second half of the newly allocated
581 buffer. */
582 num_tokens_to_copy = (lexer->first_token - old_buffer);
583 memcpy (new_buffer + buffer_length,
584 new_buffer,
585 num_tokens_to_copy * sizeof (cp_token));
586 /* Clear the rest of the buffer. We never look at this storage,
587 but the garbage collector may. */
588 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
589 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
590
591 /* Now recompute all of the buffer pointers. */
592 new_first_token
593 = new_buffer + (lexer->first_token - old_buffer);
594 if (lexer->next_token != NULL)
595 {
596 ptrdiff_t next_token_delta;
597
598 if (lexer->next_token > lexer->first_token)
599 next_token_delta = lexer->next_token - lexer->first_token;
600 else
601 next_token_delta =
602 buffer_length - (lexer->first_token - lexer->next_token);
603 lexer->next_token = new_first_token + next_token_delta;
604 }
605 lexer->last_token = new_first_token + buffer_length;
606 lexer->buffer = new_buffer;
607 lexer->buffer_end = new_buffer + buffer_length * 2;
608 lexer->first_token = new_first_token;
609 }
610}
611
612/* Store the next token from the preprocessor in *TOKEN. */
613
614static void
94edc4ab
NN
615cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
616 cp_token *token)
a723baf1
MM
617{
618 bool done;
619
620 /* If this not the main lexer, return a terminating CPP_EOF token. */
17211ab5 621 if (lexer != NULL && !lexer->main_lexer_p)
a723baf1
MM
622 {
623 token->type = CPP_EOF;
624 token->line_number = 0;
625 token->file_name = NULL;
626 token->value = NULL_TREE;
627 token->keyword = RID_MAX;
628
629 return;
630 }
631
632 done = false;
633 /* Keep going until we get a token we like. */
634 while (!done)
635 {
636 /* Get a new token from the preprocessor. */
637 token->type = c_lex (&token->value);
638 /* Issue messages about tokens we cannot process. */
639 switch (token->type)
640 {
641 case CPP_ATSIGN:
642 case CPP_HASH:
643 case CPP_PASTE:
644 error ("invalid token");
645 break;
646
647 case CPP_OTHER:
648 /* These tokens are already warned about by c_lex. */
649 break;
650
651 default:
652 /* This is a good token, so we exit the loop. */
653 done = true;
654 break;
655 }
656 }
657 /* Now we've got our token. */
658 token->line_number = lineno;
659 token->file_name = input_filename;
660
661 /* Check to see if this token is a keyword. */
662 if (token->type == CPP_NAME
663 && C_IS_RESERVED_WORD (token->value))
664 {
665 /* Mark this token as a keyword. */
666 token->type = CPP_KEYWORD;
667 /* Record which keyword. */
668 token->keyword = C_RID_CODE (token->value);
669 /* Update the value. Some keywords are mapped to particular
670 entities, rather than simply having the value of the
671 corresponding IDENTIFIER_NODE. For example, `__const' is
672 mapped to `const'. */
673 token->value = ridpointers[token->keyword];
674 }
675 else
676 token->keyword = RID_MAX;
677}
678
679/* Return a pointer to the next token in the token stream, but do not
680 consume it. */
681
682static cp_token *
94edc4ab 683cp_lexer_peek_token (cp_lexer* lexer)
a723baf1
MM
684{
685 cp_token *token;
686
687 /* If there are no tokens, read one now. */
688 if (!lexer->next_token)
689 cp_lexer_read_token (lexer);
690
691 /* Provide debugging output. */
692 if (cp_lexer_debugging_p (lexer))
693 {
694 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
695 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
696 fprintf (cp_lexer_debug_stream, "\n");
697 }
698
699 token = lexer->next_token;
700 cp_lexer_set_source_position_from_token (lexer, token);
701 return token;
702}
703
704/* Return true if the next token has the indicated TYPE. */
705
706static bool
94edc4ab 707cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
708{
709 cp_token *token;
710
711 /* Peek at the next token. */
712 token = cp_lexer_peek_token (lexer);
713 /* Check to see if it has the indicated TYPE. */
714 return token->type == type;
715}
716
717/* Return true if the next token does not have the indicated TYPE. */
718
719static bool
94edc4ab 720cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
a723baf1
MM
721{
722 return !cp_lexer_next_token_is (lexer, type);
723}
724
725/* Return true if the next token is the indicated KEYWORD. */
726
727static bool
94edc4ab 728cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
a723baf1
MM
729{
730 cp_token *token;
731
732 /* Peek at the next token. */
733 token = cp_lexer_peek_token (lexer);
734 /* Check to see if it is the indicated keyword. */
735 return token->keyword == keyword;
736}
737
738/* Return a pointer to the Nth token in the token stream. If N is 1,
739 then this is precisely equivalent to cp_lexer_peek_token. */
740
741static cp_token *
94edc4ab 742cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
a723baf1
MM
743{
744 cp_token *token;
745
746 /* N is 1-based, not zero-based. */
747 my_friendly_assert (n > 0, 20000224);
748
749 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
750 token = lexer->next_token;
751 /* If there are no tokens in the buffer, get one now. */
752 if (!token)
753 {
754 cp_lexer_read_token (lexer);
755 token = lexer->next_token;
756 }
757
758 /* Now, read tokens until we have enough. */
759 while (--n > 0)
760 {
761 /* Advance to the next token. */
762 token = cp_lexer_next_token (lexer, token);
763 /* If that's all the tokens we have, read a new one. */
764 if (token == lexer->last_token)
765 token = cp_lexer_read_token (lexer);
766 }
767
768 return token;
769}
770
771/* Consume the next token. The pointer returned is valid only until
772 another token is read. Callers should preserve copy the token
773 explicitly if they will need its value for a longer period of
774 time. */
775
776static cp_token *
94edc4ab 777cp_lexer_consume_token (cp_lexer* lexer)
a723baf1
MM
778{
779 cp_token *token;
780
781 /* If there are no tokens, read one now. */
782 if (!lexer->next_token)
783 cp_lexer_read_token (lexer);
784
785 /* Remember the token we'll be returning. */
786 token = lexer->next_token;
787
788 /* Increment NEXT_TOKEN. */
789 lexer->next_token = cp_lexer_next_token (lexer,
790 lexer->next_token);
791 /* Check to see if we're all out of tokens. */
792 if (lexer->next_token == lexer->last_token)
793 lexer->next_token = NULL;
794
795 /* If we're not saving tokens, then move FIRST_TOKEN too. */
796 if (!cp_lexer_saving_tokens (lexer))
797 {
798 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
799 if (!lexer->next_token)
800 lexer->first_token = NULL;
801 else
802 lexer->first_token = lexer->next_token;
803 }
804
805 /* Provide debugging output. */
806 if (cp_lexer_debugging_p (lexer))
807 {
808 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
809 cp_lexer_print_token (cp_lexer_debug_stream, token);
810 fprintf (cp_lexer_debug_stream, "\n");
811 }
812
813 return token;
814}
815
816/* Permanently remove the next token from the token stream. There
817 must be a valid next token already; this token never reads
818 additional tokens from the preprocessor. */
819
820static void
821cp_lexer_purge_token (cp_lexer *lexer)
822{
823 cp_token *token;
824 cp_token *next_token;
825
826 token = lexer->next_token;
827 while (true)
828 {
829 next_token = cp_lexer_next_token (lexer, token);
830 if (next_token == lexer->last_token)
831 break;
832 *token = *next_token;
833 token = next_token;
834 }
835
836 lexer->last_token = token;
837 /* The token purged may have been the only token remaining; if so,
838 clear NEXT_TOKEN. */
839 if (lexer->next_token == token)
840 lexer->next_token = NULL;
841}
842
843/* Permanently remove all tokens after TOKEN, up to, but not
844 including, the token that will be returned next by
845 cp_lexer_peek_token. */
846
847static void
848cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
849{
850 cp_token *peek;
851 cp_token *t1;
852 cp_token *t2;
853
854 if (lexer->next_token)
855 {
856 /* Copy the tokens that have not yet been read to the location
857 immediately following TOKEN. */
858 t1 = cp_lexer_next_token (lexer, token);
859 t2 = peek = cp_lexer_peek_token (lexer);
860 /* Move tokens into the vacant area between TOKEN and PEEK. */
861 while (t2 != lexer->last_token)
862 {
863 *t1 = *t2;
864 t1 = cp_lexer_next_token (lexer, t1);
865 t2 = cp_lexer_next_token (lexer, t2);
866 }
867 /* Now, the next available token is right after TOKEN. */
868 lexer->next_token = cp_lexer_next_token (lexer, token);
869 /* And the last token is wherever we ended up. */
870 lexer->last_token = t1;
871 }
872 else
873 {
874 /* There are no tokens in the buffer, so there is nothing to
875 copy. The last token in the buffer is TOKEN itself. */
876 lexer->last_token = cp_lexer_next_token (lexer, token);
877 }
878}
879
880/* Begin saving tokens. All tokens consumed after this point will be
881 preserved. */
882
883static void
94edc4ab 884cp_lexer_save_tokens (cp_lexer* lexer)
a723baf1
MM
885{
886 /* Provide debugging output. */
887 if (cp_lexer_debugging_p (lexer))
888 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
889
890 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
891 restore the tokens if required. */
892 if (!lexer->next_token)
893 cp_lexer_read_token (lexer);
894
895 VARRAY_PUSH_INT (lexer->saved_tokens,
896 cp_lexer_token_difference (lexer,
897 lexer->first_token,
898 lexer->next_token));
899}
900
901/* Commit to the portion of the token stream most recently saved. */
902
903static void
94edc4ab 904cp_lexer_commit_tokens (cp_lexer* lexer)
a723baf1
MM
905{
906 /* Provide debugging output. */
907 if (cp_lexer_debugging_p (lexer))
908 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
909
910 VARRAY_POP (lexer->saved_tokens);
911}
912
913/* Return all tokens saved since the last call to cp_lexer_save_tokens
914 to the token stream. Stop saving tokens. */
915
916static void
94edc4ab 917cp_lexer_rollback_tokens (cp_lexer* lexer)
a723baf1
MM
918{
919 size_t delta;
920
921 /* Provide debugging output. */
922 if (cp_lexer_debugging_p (lexer))
923 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
924
925 /* Find the token that was the NEXT_TOKEN when we started saving
926 tokens. */
927 delta = VARRAY_TOP_INT(lexer->saved_tokens);
928 /* Make it the next token again now. */
929 lexer->next_token = cp_lexer_advance_token (lexer,
930 lexer->first_token,
931 delta);
15d2cb19 932 /* It might be the case that there were no tokens when we started
a723baf1
MM
933 saving tokens, but that there are some tokens now. */
934 if (!lexer->next_token && lexer->first_token)
935 lexer->next_token = lexer->first_token;
936
937 /* Stop saving tokens. */
938 VARRAY_POP (lexer->saved_tokens);
939}
940
a723baf1
MM
941/* Print a representation of the TOKEN on the STREAM. */
942
943static void
94edc4ab 944cp_lexer_print_token (FILE * stream, cp_token* token)
a723baf1
MM
945{
946 const char *token_type = NULL;
947
948 /* Figure out what kind of token this is. */
949 switch (token->type)
950 {
951 case CPP_EQ:
952 token_type = "EQ";
953 break;
954
955 case CPP_COMMA:
956 token_type = "COMMA";
957 break;
958
959 case CPP_OPEN_PAREN:
960 token_type = "OPEN_PAREN";
961 break;
962
963 case CPP_CLOSE_PAREN:
964 token_type = "CLOSE_PAREN";
965 break;
966
967 case CPP_OPEN_BRACE:
968 token_type = "OPEN_BRACE";
969 break;
970
971 case CPP_CLOSE_BRACE:
972 token_type = "CLOSE_BRACE";
973 break;
974
975 case CPP_SEMICOLON:
976 token_type = "SEMICOLON";
977 break;
978
979 case CPP_NAME:
980 token_type = "NAME";
981 break;
982
983 case CPP_EOF:
984 token_type = "EOF";
985 break;
986
987 case CPP_KEYWORD:
988 token_type = "keyword";
989 break;
990
991 /* This is not a token that we know how to handle yet. */
992 default:
993 break;
994 }
995
996 /* If we have a name for the token, print it out. Otherwise, we
997 simply give the numeric code. */
998 if (token_type)
999 fprintf (stream, "%s", token_type);
1000 else
1001 fprintf (stream, "%d", token->type);
1002 /* And, for an identifier, print the identifier name. */
1003 if (token->type == CPP_NAME
1004 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1005 For example, `struct' is mapped to an INTEGER_CST. */
1006 || (token->type == CPP_KEYWORD
1007 && TREE_CODE (token->value) == IDENTIFIER_NODE))
1008 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
1009}
1010
a723baf1
MM
1011/* Start emitting debugging information. */
1012
1013static void
94edc4ab 1014cp_lexer_start_debugging (cp_lexer* lexer)
a723baf1
MM
1015{
1016 ++lexer->debugging_p;
1017}
1018
1019/* Stop emitting debugging information. */
1020
1021static void
94edc4ab 1022cp_lexer_stop_debugging (cp_lexer* lexer)
a723baf1
MM
1023{
1024 --lexer->debugging_p;
1025}
1026
1027\f
1028/* The parser. */
1029
1030/* Overview
1031 --------
1032
1033 A cp_parser parses the token stream as specified by the C++
1034 grammar. Its job is purely parsing, not semantic analysis. For
1035 example, the parser breaks the token stream into declarators,
1036 expressions, statements, and other similar syntactic constructs.
1037 It does not check that the types of the expressions on either side
1038 of an assignment-statement are compatible, or that a function is
1039 not declared with a parameter of type `void'.
1040
1041 The parser invokes routines elsewhere in the compiler to perform
1042 semantic analysis and to build up the abstract syntax tree for the
1043 code processed.
1044
1045 The parser (and the template instantiation code, which is, in a
1046 way, a close relative of parsing) are the only parts of the
1047 compiler that should be calling push_scope and pop_scope, or
1048 related functions. The parser (and template instantiation code)
1049 keeps track of what scope is presently active; everything else
1050 should simply honor that. (The code that generates static
1051 initializers may also need to set the scope, in order to check
1052 access control correctly when emitting the initializers.)
1053
1054 Methodology
1055 -----------
1056
1057 The parser is of the standard recursive-descent variety. Upcoming
1058 tokens in the token stream are examined in order to determine which
1059 production to use when parsing a non-terminal. Some C++ constructs
1060 require arbitrary look ahead to disambiguate. For example, it is
1061 impossible, in the general case, to tell whether a statement is an
1062 expression or declaration without scanning the entire statement.
1063 Therefore, the parser is capable of "parsing tentatively." When the
1064 parser is not sure what construct comes next, it enters this mode.
1065 Then, while we attempt to parse the construct, the parser queues up
1066 error messages, rather than issuing them immediately, and saves the
1067 tokens it consumes. If the construct is parsed successfully, the
1068 parser "commits", i.e., it issues any queued error messages and
1069 the tokens that were being preserved are permanently discarded.
1070 If, however, the construct is not parsed successfully, the parser
1071 rolls back its state completely so that it can resume parsing using
1072 a different alternative.
1073
1074 Future Improvements
1075 -------------------
1076
1077 The performance of the parser could probably be improved
1078 substantially. Some possible improvements include:
1079
1080 - The expression parser recurses through the various levels of
1081 precedence as specified in the grammar, rather than using an
1082 operator-precedence technique. Therefore, parsing a simple
1083 identifier requires multiple recursive calls.
1084
1085 - We could often eliminate the need to parse tentatively by
1086 looking ahead a little bit. In some places, this approach
1087 might not entirely eliminate the need to parse tentatively, but
1088 it might still speed up the average case. */
1089
1090/* Flags that are passed to some parsing functions. These values can
1091 be bitwise-ored together. */
1092
1093typedef enum cp_parser_flags
1094{
1095 /* No flags. */
1096 CP_PARSER_FLAGS_NONE = 0x0,
1097 /* The construct is optional. If it is not present, then no error
1098 should be issued. */
1099 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1100 /* When parsing a type-specifier, do not allow user-defined types. */
1101 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1102} cp_parser_flags;
1103
1104/* The different kinds of ids that we ecounter. */
1105
1106typedef enum cp_parser_id_kind
1107{
1108 /* Not an id at all. */
1109 CP_PARSER_ID_KIND_NONE,
1110 /* An unqualified-id that is not a template-id. */
1111 CP_PARSER_ID_KIND_UNQUALIFIED,
1112 /* An unqualified template-id. */
1113 CP_PARSER_ID_KIND_TEMPLATE_ID,
1114 /* A qualified-id. */
1115 CP_PARSER_ID_KIND_QUALIFIED
1116} cp_parser_id_kind;
1117
62b8a44e
NS
1118/* The different kinds of declarators we want to parse. */
1119
1120typedef enum cp_parser_declarator_kind
1121{
1122 /* We want an abstract declartor. */
1123 CP_PARSER_DECLARATOR_ABSTRACT,
1124 /* We want a named declarator. */
1125 CP_PARSER_DECLARATOR_NAMED,
712becab 1126 /* We don't mind, but the name must be an unqualified-id */
62b8a44e
NS
1127 CP_PARSER_DECLARATOR_EITHER
1128} cp_parser_declarator_kind;
1129
a723baf1
MM
1130/* A mapping from a token type to a corresponding tree node type. */
1131
1132typedef struct cp_parser_token_tree_map_node
1133{
1134 /* The token type. */
1135 enum cpp_ttype token_type;
1136 /* The corresponding tree code. */
1137 enum tree_code tree_type;
1138} cp_parser_token_tree_map_node;
1139
1140/* A complete map consists of several ordinary entries, followed by a
1141 terminator. The terminating entry has a token_type of CPP_EOF. */
1142
1143typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1144
1145/* The status of a tentative parse. */
1146
1147typedef enum cp_parser_status_kind
1148{
1149 /* No errors have occurred. */
1150 CP_PARSER_STATUS_KIND_NO_ERROR,
1151 /* An error has occurred. */
1152 CP_PARSER_STATUS_KIND_ERROR,
1153 /* We are committed to this tentative parse, whether or not an error
1154 has occurred. */
1155 CP_PARSER_STATUS_KIND_COMMITTED
1156} cp_parser_status_kind;
1157
1158/* Context that is saved and restored when parsing tentatively. */
1159
1160typedef struct cp_parser_context GTY (())
1161{
1162 /* If this is a tentative parsing context, the status of the
1163 tentative parse. */
1164 enum cp_parser_status_kind status;
1165 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1166 that are looked up in this context must be looked up both in the
1167 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1168 the context of the containing expression. */
1169 tree object_type;
a723baf1
MM
1170 /* The next parsing context in the stack. */
1171 struct cp_parser_context *next;
1172} cp_parser_context;
1173
1174/* Prototypes. */
1175
1176/* Constructors and destructors. */
1177
1178static cp_parser_context *cp_parser_context_new
94edc4ab 1179 (cp_parser_context *);
a723baf1 1180
e5976695
MM
1181/* Class variables. */
1182
92bc1323 1183static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
e5976695 1184
a723baf1
MM
1185/* Constructors and destructors. */
1186
1187/* Construct a new context. The context below this one on the stack
1188 is given by NEXT. */
1189
1190static cp_parser_context *
94edc4ab 1191cp_parser_context_new (cp_parser_context* next)
a723baf1
MM
1192{
1193 cp_parser_context *context;
1194
1195 /* Allocate the storage. */
e5976695
MM
1196 if (cp_parser_context_free_list != NULL)
1197 {
1198 /* Pull the first entry from the free list. */
1199 context = cp_parser_context_free_list;
1200 cp_parser_context_free_list = context->next;
1201 memset ((char *)context, 0, sizeof (*context));
1202 }
1203 else
1204 context = ((cp_parser_context *)
1205 ggc_alloc_cleared (sizeof (cp_parser_context)));
a723baf1
MM
1206 /* No errors have occurred yet in this context. */
1207 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1208 /* If this is not the bottomost context, copy information that we
1209 need from the previous context. */
1210 if (next)
1211 {
1212 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1213 expression, then we are parsing one in this context, too. */
1214 context->object_type = next->object_type;
a723baf1
MM
1215 /* Thread the stack. */
1216 context->next = next;
1217 }
1218
1219 return context;
1220}
1221
1222/* The cp_parser structure represents the C++ parser. */
1223
1224typedef struct cp_parser GTY(())
1225{
1226 /* The lexer from which we are obtaining tokens. */
1227 cp_lexer *lexer;
1228
1229 /* The scope in which names should be looked up. If NULL_TREE, then
1230 we look up names in the scope that is currently open in the
1231 source program. If non-NULL, this is either a TYPE or
1232 NAMESPACE_DECL for the scope in which we should look.
1233
1234 This value is not cleared automatically after a name is looked
1235 up, so we must be careful to clear it before starting a new look
1236 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1237 will look up `Z' in the scope of `X', rather than the current
1238 scope.) Unfortunately, it is difficult to tell when name lookup
1239 is complete, because we sometimes peek at a token, look it up,
1240 and then decide not to consume it. */
1241 tree scope;
1242
1243 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1244 last lookup took place. OBJECT_SCOPE is used if an expression
1245 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1246 respectively. QUALIFYING_SCOPE is used for an expression of the
1247 form "X::Y"; it refers to X. */
1248 tree object_scope;
1249 tree qualifying_scope;
1250
1251 /* A stack of parsing contexts. All but the bottom entry on the
1252 stack will be tentative contexts.
1253
1254 We parse tentatively in order to determine which construct is in
1255 use in some situations. For example, in order to determine
1256 whether a statement is an expression-statement or a
1257 declaration-statement we parse it tentatively as a
1258 declaration-statement. If that fails, we then reparse the same
1259 token stream as an expression-statement. */
1260 cp_parser_context *context;
1261
1262 /* True if we are parsing GNU C++. If this flag is not set, then
1263 GNU extensions are not recognized. */
1264 bool allow_gnu_extensions_p;
1265
1266 /* TRUE if the `>' token should be interpreted as the greater-than
1267 operator. FALSE if it is the end of a template-id or
1268 template-parameter-list. */
1269 bool greater_than_is_operator_p;
1270
1271 /* TRUE if default arguments are allowed within a parameter list
1272 that starts at this point. FALSE if only a gnu extension makes
1273 them permissable. */
1274 bool default_arg_ok_p;
1275
1276 /* TRUE if we are parsing an integral constant-expression. See
1277 [expr.const] for a precise definition. */
a723baf1
MM
1278 bool constant_expression_p;
1279
14d22dd6
MM
1280 /* TRUE if we are parsing an integral constant-expression -- but a
1281 non-constant expression should be permitted as well. This flag
1282 is used when parsing an array bound so that GNU variable-length
1283 arrays are tolerated. */
1284 bool allow_non_constant_expression_p;
1285
1286 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1287 been seen that makes the expression non-constant. */
1288 bool non_constant_expression_p;
1289
a723baf1
MM
1290 /* TRUE if local variable names and `this' are forbidden in the
1291 current context. */
1292 bool local_variables_forbidden_p;
1293
1294 /* TRUE if the declaration we are parsing is part of a
1295 linkage-specification of the form `extern string-literal
1296 declaration'. */
1297 bool in_unbraced_linkage_specification_p;
1298
1299 /* TRUE if we are presently parsing a declarator, after the
1300 direct-declarator. */
1301 bool in_declarator_p;
1302
1303 /* If non-NULL, then we are parsing a construct where new type
1304 definitions are not permitted. The string stored here will be
1305 issued as an error message if a type is defined. */
1306 const char *type_definition_forbidden_message;
1307
a723baf1
MM
1308 /* A TREE_LIST of queues of functions whose bodies have been lexed,
1309 but may not have been parsed. These functions are friends of
1310 members defined within a class-specification; they are not
1311 procssed until the class is complete. The active queue is at the
1312 front of the list.
1313
1314 Within each queue, functions appear in the reverse order that
8218bd34
MM
1315 they appeared in the source. Each TREE_VALUE is a
1316 FUNCTION_DECL of TEMPLATE_DECL corresponding to a member
1317 function. */
a723baf1
MM
1318 tree unparsed_functions_queues;
1319
1320 /* The number of classes whose definitions are currently in
1321 progress. */
1322 unsigned num_classes_being_defined;
1323
1324 /* The number of template parameter lists that apply directly to the
1325 current declaration. */
1326 unsigned num_template_parameter_lists;
1327} cp_parser;
1328
1329/* The type of a function that parses some kind of expression */
94edc4ab 1330typedef tree (*cp_parser_expression_fn) (cp_parser *);
a723baf1
MM
1331
1332/* Prototypes. */
1333
1334/* Constructors and destructors. */
1335
1336static cp_parser *cp_parser_new
94edc4ab 1337 (void);
a723baf1
MM
1338
1339/* Routines to parse various constructs.
1340
1341 Those that return `tree' will return the error_mark_node (rather
1342 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1343 Sometimes, they will return an ordinary node if error-recovery was
1344 attempted, even though a parse error occurrred. So, to check
1345 whether or not a parse error occurred, you should always use
1346 cp_parser_error_occurred. If the construct is optional (indicated
1347 either by an `_opt' in the name of the function that does the
1348 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1349 the construct is not present. */
1350
1351/* Lexical conventions [gram.lex] */
1352
1353static tree cp_parser_identifier
94edc4ab 1354 (cp_parser *);
a723baf1
MM
1355
1356/* Basic concepts [gram.basic] */
1357
1358static bool cp_parser_translation_unit
94edc4ab 1359 (cp_parser *);
a723baf1
MM
1360
1361/* Expressions [gram.expr] */
1362
1363static tree cp_parser_primary_expression
1364 (cp_parser *, cp_parser_id_kind *, tree *);
1365static tree cp_parser_id_expression
94edc4ab 1366 (cp_parser *, bool, bool, bool *);
a723baf1 1367static tree cp_parser_unqualified_id
94edc4ab 1368 (cp_parser *, bool, bool);
a723baf1
MM
1369static tree cp_parser_nested_name_specifier_opt
1370 (cp_parser *, bool, bool, bool);
1371static tree cp_parser_nested_name_specifier
1372 (cp_parser *, bool, bool, bool);
1373static tree cp_parser_class_or_namespace_name
1374 (cp_parser *, bool, bool, bool, bool);
1375static tree cp_parser_postfix_expression
1376 (cp_parser *, bool);
1377static tree cp_parser_expression_list
94edc4ab 1378 (cp_parser *);
a723baf1 1379static void cp_parser_pseudo_destructor_name
94edc4ab 1380 (cp_parser *, tree *, tree *);
a723baf1
MM
1381static tree cp_parser_unary_expression
1382 (cp_parser *, bool);
1383static enum tree_code cp_parser_unary_operator
94edc4ab 1384 (cp_token *);
a723baf1 1385static tree cp_parser_new_expression
94edc4ab 1386 (cp_parser *);
a723baf1 1387static tree cp_parser_new_placement
94edc4ab 1388 (cp_parser *);
a723baf1 1389static tree cp_parser_new_type_id
94edc4ab 1390 (cp_parser *);
a723baf1 1391static tree cp_parser_new_declarator_opt
94edc4ab 1392 (cp_parser *);
a723baf1 1393static tree cp_parser_direct_new_declarator
94edc4ab 1394 (cp_parser *);
a723baf1 1395static tree cp_parser_new_initializer
94edc4ab 1396 (cp_parser *);
a723baf1 1397static tree cp_parser_delete_expression
94edc4ab 1398 (cp_parser *);
a723baf1
MM
1399static tree cp_parser_cast_expression
1400 (cp_parser *, bool);
1401static tree cp_parser_pm_expression
94edc4ab 1402 (cp_parser *);
a723baf1 1403static tree cp_parser_multiplicative_expression
94edc4ab 1404 (cp_parser *);
a723baf1 1405static tree cp_parser_additive_expression
94edc4ab 1406 (cp_parser *);
a723baf1 1407static tree cp_parser_shift_expression
94edc4ab 1408 (cp_parser *);
a723baf1 1409static tree cp_parser_relational_expression
94edc4ab 1410 (cp_parser *);
a723baf1 1411static tree cp_parser_equality_expression
94edc4ab 1412 (cp_parser *);
a723baf1 1413static tree cp_parser_and_expression
94edc4ab 1414 (cp_parser *);
a723baf1 1415static tree cp_parser_exclusive_or_expression
94edc4ab 1416 (cp_parser *);
a723baf1 1417static tree cp_parser_inclusive_or_expression
94edc4ab 1418 (cp_parser *);
a723baf1 1419static tree cp_parser_logical_and_expression
94edc4ab 1420 (cp_parser *);
a723baf1 1421static tree cp_parser_logical_or_expression
94edc4ab 1422 (cp_parser *);
a723baf1 1423static tree cp_parser_conditional_expression
94edc4ab 1424 (cp_parser *);
a723baf1 1425static tree cp_parser_question_colon_clause
94edc4ab 1426 (cp_parser *, tree);
a723baf1 1427static tree cp_parser_assignment_expression
94edc4ab 1428 (cp_parser *);
a723baf1 1429static enum tree_code cp_parser_assignment_operator_opt
94edc4ab 1430 (cp_parser *);
a723baf1 1431static tree cp_parser_expression
94edc4ab 1432 (cp_parser *);
a723baf1 1433static tree cp_parser_constant_expression
14d22dd6 1434 (cp_parser *, bool, bool *);
a723baf1
MM
1435
1436/* Statements [gram.stmt.stmt] */
1437
1438static void cp_parser_statement
94edc4ab 1439 (cp_parser *);
a723baf1 1440static tree cp_parser_labeled_statement
94edc4ab 1441 (cp_parser *);
a723baf1 1442static tree cp_parser_expression_statement
94edc4ab 1443 (cp_parser *);
a723baf1
MM
1444static tree cp_parser_compound_statement
1445 (cp_parser *);
1446static void cp_parser_statement_seq_opt
94edc4ab 1447 (cp_parser *);
a723baf1 1448static tree cp_parser_selection_statement
94edc4ab 1449 (cp_parser *);
a723baf1 1450static tree cp_parser_condition
94edc4ab 1451 (cp_parser *);
a723baf1 1452static tree cp_parser_iteration_statement
94edc4ab 1453 (cp_parser *);
a723baf1 1454static void cp_parser_for_init_statement
94edc4ab 1455 (cp_parser *);
a723baf1 1456static tree cp_parser_jump_statement
94edc4ab 1457 (cp_parser *);
a723baf1 1458static void cp_parser_declaration_statement
94edc4ab 1459 (cp_parser *);
a723baf1
MM
1460
1461static tree cp_parser_implicitly_scoped_statement
94edc4ab 1462 (cp_parser *);
a723baf1 1463static void cp_parser_already_scoped_statement
94edc4ab 1464 (cp_parser *);
a723baf1
MM
1465
1466/* Declarations [gram.dcl.dcl] */
1467
1468static void cp_parser_declaration_seq_opt
94edc4ab 1469 (cp_parser *);
a723baf1 1470static void cp_parser_declaration
94edc4ab 1471 (cp_parser *);
a723baf1 1472static void cp_parser_block_declaration
94edc4ab 1473 (cp_parser *, bool);
a723baf1 1474static void cp_parser_simple_declaration
94edc4ab 1475 (cp_parser *, bool);
a723baf1 1476static tree cp_parser_decl_specifier_seq
94edc4ab 1477 (cp_parser *, cp_parser_flags, tree *, bool *);
a723baf1 1478static tree cp_parser_storage_class_specifier_opt
94edc4ab 1479 (cp_parser *);
a723baf1 1480static tree cp_parser_function_specifier_opt
94edc4ab 1481 (cp_parser *);
a723baf1 1482static tree cp_parser_type_specifier
94edc4ab 1483 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
a723baf1 1484static tree cp_parser_simple_type_specifier
94edc4ab 1485 (cp_parser *, cp_parser_flags);
a723baf1 1486static tree cp_parser_type_name
94edc4ab 1487 (cp_parser *);
a723baf1 1488static tree cp_parser_elaborated_type_specifier
94edc4ab 1489 (cp_parser *, bool, bool);
a723baf1 1490static tree cp_parser_enum_specifier
94edc4ab 1491 (cp_parser *);
a723baf1 1492static void cp_parser_enumerator_list
94edc4ab 1493 (cp_parser *, tree);
a723baf1 1494static void cp_parser_enumerator_definition
94edc4ab 1495 (cp_parser *, tree);
a723baf1 1496static tree cp_parser_namespace_name
94edc4ab 1497 (cp_parser *);
a723baf1 1498static void cp_parser_namespace_definition
94edc4ab 1499 (cp_parser *);
a723baf1 1500static void cp_parser_namespace_body
94edc4ab 1501 (cp_parser *);
a723baf1 1502static tree cp_parser_qualified_namespace_specifier
94edc4ab 1503 (cp_parser *);
a723baf1 1504static void cp_parser_namespace_alias_definition
94edc4ab 1505 (cp_parser *);
a723baf1 1506static void cp_parser_using_declaration
94edc4ab 1507 (cp_parser *);
a723baf1 1508static void cp_parser_using_directive
94edc4ab 1509 (cp_parser *);
a723baf1 1510static void cp_parser_asm_definition
94edc4ab 1511 (cp_parser *);
a723baf1 1512static void cp_parser_linkage_specification
94edc4ab 1513 (cp_parser *);
a723baf1
MM
1514
1515/* Declarators [gram.dcl.decl] */
1516
1517static tree cp_parser_init_declarator
94edc4ab 1518 (cp_parser *, tree, tree, bool, bool, bool *);
a723baf1 1519static tree cp_parser_declarator
94edc4ab 1520 (cp_parser *, cp_parser_declarator_kind, bool *);
a723baf1 1521static tree cp_parser_direct_declarator
94edc4ab 1522 (cp_parser *, cp_parser_declarator_kind, bool *);
a723baf1 1523static enum tree_code cp_parser_ptr_operator
94edc4ab 1524 (cp_parser *, tree *, tree *);
a723baf1 1525static tree cp_parser_cv_qualifier_seq_opt
94edc4ab 1526 (cp_parser *);
a723baf1 1527static tree cp_parser_cv_qualifier_opt
94edc4ab 1528 (cp_parser *);
a723baf1 1529static tree cp_parser_declarator_id
94edc4ab 1530 (cp_parser *);
a723baf1 1531static tree cp_parser_type_id
94edc4ab 1532 (cp_parser *);
a723baf1 1533static tree cp_parser_type_specifier_seq
94edc4ab 1534 (cp_parser *);
a723baf1 1535static tree cp_parser_parameter_declaration_clause
94edc4ab 1536 (cp_parser *);
a723baf1 1537static tree cp_parser_parameter_declaration_list
94edc4ab 1538 (cp_parser *);
a723baf1 1539static tree cp_parser_parameter_declaration
94edc4ab 1540 (cp_parser *, bool);
a723baf1 1541static tree cp_parser_function_definition
94edc4ab 1542 (cp_parser *, bool *);
a723baf1
MM
1543static void cp_parser_function_body
1544 (cp_parser *);
1545static tree cp_parser_initializer
94edc4ab 1546 (cp_parser *, bool *);
a723baf1 1547static tree cp_parser_initializer_clause
94edc4ab 1548 (cp_parser *);
a723baf1 1549static tree cp_parser_initializer_list
94edc4ab 1550 (cp_parser *);
a723baf1
MM
1551
1552static bool cp_parser_ctor_initializer_opt_and_function_body
1553 (cp_parser *);
1554
1555/* Classes [gram.class] */
1556
1557static tree cp_parser_class_name
1558 (cp_parser *, bool, bool, bool, bool, bool, bool);
1559static tree cp_parser_class_specifier
94edc4ab 1560 (cp_parser *);
a723baf1 1561static tree cp_parser_class_head
94edc4ab 1562 (cp_parser *, bool *);
a723baf1 1563static enum tag_types cp_parser_class_key
94edc4ab 1564 (cp_parser *);
a723baf1 1565static void cp_parser_member_specification_opt
94edc4ab 1566 (cp_parser *);
a723baf1 1567static void cp_parser_member_declaration
94edc4ab 1568 (cp_parser *);
a723baf1 1569static tree cp_parser_pure_specifier
94edc4ab 1570 (cp_parser *);
a723baf1 1571static tree cp_parser_constant_initializer
94edc4ab 1572 (cp_parser *);
a723baf1
MM
1573
1574/* Derived classes [gram.class.derived] */
1575
1576static tree cp_parser_base_clause
94edc4ab 1577 (cp_parser *);
a723baf1 1578static tree cp_parser_base_specifier
94edc4ab 1579 (cp_parser *);
a723baf1
MM
1580
1581/* Special member functions [gram.special] */
1582
1583static tree cp_parser_conversion_function_id
94edc4ab 1584 (cp_parser *);
a723baf1 1585static tree cp_parser_conversion_type_id
94edc4ab 1586 (cp_parser *);
a723baf1 1587static tree cp_parser_conversion_declarator_opt
94edc4ab 1588 (cp_parser *);
a723baf1 1589static bool cp_parser_ctor_initializer_opt
94edc4ab 1590 (cp_parser *);
a723baf1 1591static void cp_parser_mem_initializer_list
94edc4ab 1592 (cp_parser *);
a723baf1 1593static tree cp_parser_mem_initializer
94edc4ab 1594 (cp_parser *);
a723baf1 1595static tree cp_parser_mem_initializer_id
94edc4ab 1596 (cp_parser *);
a723baf1
MM
1597
1598/* Overloading [gram.over] */
1599
1600static tree cp_parser_operator_function_id
94edc4ab 1601 (cp_parser *);
a723baf1 1602static tree cp_parser_operator
94edc4ab 1603 (cp_parser *);
a723baf1
MM
1604
1605/* Templates [gram.temp] */
1606
1607static void cp_parser_template_declaration
94edc4ab 1608 (cp_parser *, bool);
a723baf1 1609static tree cp_parser_template_parameter_list
94edc4ab 1610 (cp_parser *);
a723baf1 1611static tree cp_parser_template_parameter
94edc4ab 1612 (cp_parser *);
a723baf1 1613static tree cp_parser_type_parameter
94edc4ab 1614 (cp_parser *);
a723baf1 1615static tree cp_parser_template_id
94edc4ab 1616 (cp_parser *, bool, bool);
a723baf1 1617static tree cp_parser_template_name
94edc4ab 1618 (cp_parser *, bool, bool);
a723baf1 1619static tree cp_parser_template_argument_list
94edc4ab 1620 (cp_parser *);
a723baf1 1621static tree cp_parser_template_argument
94edc4ab 1622 (cp_parser *);
a723baf1 1623static void cp_parser_explicit_instantiation
94edc4ab 1624 (cp_parser *);
a723baf1 1625static void cp_parser_explicit_specialization
94edc4ab 1626 (cp_parser *);
a723baf1
MM
1627
1628/* Exception handling [gram.exception] */
1629
1630static tree cp_parser_try_block
94edc4ab 1631 (cp_parser *);
a723baf1 1632static bool cp_parser_function_try_block
94edc4ab 1633 (cp_parser *);
a723baf1 1634static void cp_parser_handler_seq
94edc4ab 1635 (cp_parser *);
a723baf1 1636static void cp_parser_handler
94edc4ab 1637 (cp_parser *);
a723baf1 1638static tree cp_parser_exception_declaration
94edc4ab 1639 (cp_parser *);
a723baf1 1640static tree cp_parser_throw_expression
94edc4ab 1641 (cp_parser *);
a723baf1 1642static tree cp_parser_exception_specification_opt
94edc4ab 1643 (cp_parser *);
a723baf1 1644static tree cp_parser_type_id_list
94edc4ab 1645 (cp_parser *);
a723baf1
MM
1646
1647/* GNU Extensions */
1648
1649static tree cp_parser_asm_specification_opt
94edc4ab 1650 (cp_parser *);
a723baf1 1651static tree cp_parser_asm_operand_list
94edc4ab 1652 (cp_parser *);
a723baf1 1653static tree cp_parser_asm_clobber_list
94edc4ab 1654 (cp_parser *);
a723baf1 1655static tree cp_parser_attributes_opt
94edc4ab 1656 (cp_parser *);
a723baf1 1657static tree cp_parser_attribute_list
94edc4ab 1658 (cp_parser *);
a723baf1 1659static bool cp_parser_extension_opt
94edc4ab 1660 (cp_parser *, int *);
a723baf1 1661static void cp_parser_label_declaration
94edc4ab 1662 (cp_parser *);
a723baf1
MM
1663
1664/* Utility Routines */
1665
1666static tree cp_parser_lookup_name
94edc4ab 1667 (cp_parser *, tree, bool, bool, bool, bool);
a723baf1 1668static tree cp_parser_lookup_name_simple
94edc4ab 1669 (cp_parser *, tree);
a723baf1
MM
1670static tree cp_parser_maybe_treat_template_as_class
1671 (tree, bool);
1672static bool cp_parser_check_declarator_template_parameters
94edc4ab 1673 (cp_parser *, tree);
a723baf1 1674static bool cp_parser_check_template_parameters
94edc4ab 1675 (cp_parser *, unsigned);
a723baf1 1676static tree cp_parser_binary_expression
94edc4ab 1677 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
a723baf1 1678static tree cp_parser_global_scope_opt
94edc4ab 1679 (cp_parser *, bool);
a723baf1
MM
1680static bool cp_parser_constructor_declarator_p
1681 (cp_parser *, bool);
1682static tree cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab 1683 (cp_parser *, tree, tree, tree);
a723baf1 1684static tree cp_parser_function_definition_after_declarator
94edc4ab 1685 (cp_parser *, bool);
a723baf1 1686static void cp_parser_template_declaration_after_export
94edc4ab 1687 (cp_parser *, bool);
a723baf1 1688static tree cp_parser_single_declaration
94edc4ab 1689 (cp_parser *, bool, bool *);
a723baf1 1690static tree cp_parser_functional_cast
94edc4ab 1691 (cp_parser *, tree);
a723baf1 1692static void cp_parser_late_parsing_for_member
94edc4ab 1693 (cp_parser *, tree);
a723baf1 1694static void cp_parser_late_parsing_default_args
8218bd34 1695 (cp_parser *, tree);
a723baf1 1696static tree cp_parser_sizeof_operand
94edc4ab 1697 (cp_parser *, enum rid);
a723baf1 1698static bool cp_parser_declares_only_class_p
94edc4ab 1699 (cp_parser *);
a723baf1 1700static bool cp_parser_friend_p
94edc4ab 1701 (tree);
a723baf1 1702static cp_token *cp_parser_require
94edc4ab 1703 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1704static cp_token *cp_parser_require_keyword
94edc4ab 1705 (cp_parser *, enum rid, const char *);
a723baf1 1706static bool cp_parser_token_starts_function_definition_p
94edc4ab 1707 (cp_token *);
a723baf1
MM
1708static bool cp_parser_next_token_starts_class_definition_p
1709 (cp_parser *);
1710static enum tag_types cp_parser_token_is_class_key
94edc4ab 1711 (cp_token *);
a723baf1
MM
1712static void cp_parser_check_class_key
1713 (enum tag_types, tree type);
1714static bool cp_parser_optional_template_keyword
1715 (cp_parser *);
2050a1bb
MM
1716static void cp_parser_pre_parsed_nested_name_specifier
1717 (cp_parser *);
a723baf1
MM
1718static void cp_parser_cache_group
1719 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1720static void cp_parser_parse_tentatively
94edc4ab 1721 (cp_parser *);
a723baf1 1722static void cp_parser_commit_to_tentative_parse
94edc4ab 1723 (cp_parser *);
a723baf1 1724static void cp_parser_abort_tentative_parse
94edc4ab 1725 (cp_parser *);
a723baf1 1726static bool cp_parser_parse_definitely
94edc4ab 1727 (cp_parser *);
f7b5ecd9 1728static inline bool cp_parser_parsing_tentatively
94edc4ab 1729 (cp_parser *);
a723baf1 1730static bool cp_parser_committed_to_tentative_parse
94edc4ab 1731 (cp_parser *);
a723baf1 1732static void cp_parser_error
94edc4ab 1733 (cp_parser *, const char *);
e5976695 1734static bool cp_parser_simulate_error
94edc4ab 1735 (cp_parser *);
a723baf1 1736static void cp_parser_check_type_definition
94edc4ab 1737 (cp_parser *);
14d22dd6
MM
1738static tree cp_parser_non_constant_expression
1739 (const char *);
1740static tree cp_parser_non_constant_id_expression
1741 (tree);
8fbc5ae7
MM
1742static bool cp_parser_diagnose_invalid_type_name
1743 (cp_parser *);
a723baf1 1744static bool cp_parser_skip_to_closing_parenthesis
94edc4ab 1745 (cp_parser *);
a723baf1
MM
1746static bool cp_parser_skip_to_closing_parenthesis_or_comma
1747 (cp_parser *);
1748static void cp_parser_skip_to_end_of_statement
94edc4ab 1749 (cp_parser *);
e0860732
MM
1750static void cp_parser_consume_semicolon_at_end_of_statement
1751 (cp_parser *);
a723baf1 1752static void cp_parser_skip_to_end_of_block_or_statement
94edc4ab 1753 (cp_parser *);
a723baf1
MM
1754static void cp_parser_skip_to_closing_brace
1755 (cp_parser *);
1756static void cp_parser_skip_until_found
94edc4ab 1757 (cp_parser *, enum cpp_ttype, const char *);
a723baf1 1758static bool cp_parser_error_occurred
94edc4ab 1759 (cp_parser *);
a723baf1 1760static bool cp_parser_allow_gnu_extensions_p
94edc4ab 1761 (cp_parser *);
a723baf1 1762static bool cp_parser_is_string_literal
94edc4ab 1763 (cp_token *);
a723baf1 1764static bool cp_parser_is_keyword
94edc4ab 1765 (cp_token *, enum rid);
a723baf1
MM
1766static tree cp_parser_scope_through_which_access_occurs
1767 (tree, tree, tree);
1768
f7b5ecd9
MM
1769/* Returns non-zero if we are parsing tentatively. */
1770
1771static inline bool
94edc4ab 1772cp_parser_parsing_tentatively (cp_parser* parser)
f7b5ecd9
MM
1773{
1774 return parser->context->next != NULL;
1775}
1776
a723baf1
MM
1777/* Returns non-zero if TOKEN is a string literal. */
1778
1779static bool
94edc4ab 1780cp_parser_is_string_literal (cp_token* token)
a723baf1
MM
1781{
1782 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1783}
1784
1785/* Returns non-zero if TOKEN is the indicated KEYWORD. */
1786
1787static bool
94edc4ab 1788cp_parser_is_keyword (cp_token* token, enum rid keyword)
a723baf1
MM
1789{
1790 return token->keyword == keyword;
1791}
1792
a723baf1
MM
1793/* Returns the scope through which DECL is being accessed, or
1794 NULL_TREE if DECL is not a member. If OBJECT_TYPE is non-NULL, we
1795 have just seen `x->' or `x.' and OBJECT_TYPE is the type of `*x',
1796 or `x', respectively. If the DECL was named as `A::B' then
1797 NESTED_NAME_SPECIFIER is `A'. */
1798
1799tree
94edc4ab
NN
1800cp_parser_scope_through_which_access_occurs (tree decl,
1801 tree object_type,
1802 tree nested_name_specifier)
a723baf1
MM
1803{
1804 tree scope;
1805 tree qualifying_type = NULL_TREE;
1806
1807 /* Determine the SCOPE of DECL. */
1808 scope = context_for_name_lookup (decl);
1809 /* If the SCOPE is not a type, then DECL is not a member. */
1810 if (!TYPE_P (scope))
1811 return NULL_TREE;
1812 /* Figure out the type through which DECL is being accessed. */
a6f6052a
MM
1813 if (object_type
1814 /* OBJECT_TYPE might not be a class type; consider:
1815
1816 class A { typedef int I; };
1817 I *p;
1818 p->A::I::~I();
1819
1820 In this case, we will have "A::I" as the DECL, but "I" as the
1821 OBJECT_TYPE. */
1822 && CLASS_TYPE_P (object_type)
1823 && DERIVED_FROM_P (scope, object_type))
a723baf1
MM
1824 /* If we are processing a `->' or `.' expression, use the type of the
1825 left-hand side. */
1826 qualifying_type = object_type;
1827 else if (nested_name_specifier)
1828 {
1829 /* If the reference is to a non-static member of the
1830 current class, treat it as if it were referenced through
1831 `this'. */
1832 if (DECL_NONSTATIC_MEMBER_P (decl)
1833 && current_class_ptr
1834 && DERIVED_FROM_P (scope, current_class_type))
1835 qualifying_type = current_class_type;
1836 /* Otherwise, use the type indicated by the
1837 nested-name-specifier. */
1838 else
1839 qualifying_type = nested_name_specifier;
1840 }
1841 else
1842 /* Otherwise, the name must be from the current class or one of
1843 its bases. */
1844 qualifying_type = currently_open_derived_class (scope);
1845
1846 return qualifying_type;
1847}
1848
1849/* Issue the indicated error MESSAGE. */
1850
1851static void
94edc4ab 1852cp_parser_error (cp_parser* parser, const char* message)
a723baf1 1853{
a723baf1 1854 /* Output the MESSAGE -- unless we're parsing tentatively. */
e5976695 1855 if (!cp_parser_simulate_error (parser))
a723baf1
MM
1856 error (message);
1857}
1858
1859/* If we are parsing tentatively, remember that an error has occurred
e5976695
MM
1860 during this tentative parse. Returns true if the error was
1861 simulated; false if a messgae should be issued by the caller. */
a723baf1 1862
e5976695 1863static bool
94edc4ab 1864cp_parser_simulate_error (cp_parser* parser)
a723baf1
MM
1865{
1866 if (cp_parser_parsing_tentatively (parser)
1867 && !cp_parser_committed_to_tentative_parse (parser))
e5976695
MM
1868 {
1869 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1870 return true;
1871 }
1872 return false;
a723baf1
MM
1873}
1874
1875/* This function is called when a type is defined. If type
1876 definitions are forbidden at this point, an error message is
1877 issued. */
1878
1879static void
94edc4ab 1880cp_parser_check_type_definition (cp_parser* parser)
a723baf1
MM
1881{
1882 /* If types are forbidden here, issue a message. */
1883 if (parser->type_definition_forbidden_message)
1884 /* Use `%s' to print the string in case there are any escape
1885 characters in the message. */
1886 error ("%s", parser->type_definition_forbidden_message);
1887}
1888
14d22dd6
MM
1889/* Issue an eror message about the fact that THING appeared in a
1890 constant-expression. Returns ERROR_MARK_NODE. */
1891
1892static tree
1893cp_parser_non_constant_expression (const char *thing)
1894{
1895 error ("%s cannot appear in a constant-expression", thing);
1896 return error_mark_node;
1897}
1898
1899/* Issue an eror message about the fact that DECL appeared in a
1900 constant-expression. Returns ERROR_MARK_NODE. */
1901
1902static tree
1903cp_parser_non_constant_id_expression (tree decl)
1904{
1905 error ("`%D' cannot appear in a constant-expression", decl);
1906 return error_mark_node;
1907}
1908
8fbc5ae7
MM
1909/* Check for a common situation where a type-name should be present,
1910 but is not, and issue a sensible error message. Returns true if an
1911 invalid type-name was detected. */
1912
1913static bool
1914cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1915{
1916 /* If the next two tokens are both identifiers, the code is
1917 erroneous. The usual cause of this situation is code like:
1918
1919 T t;
1920
1921 where "T" should name a type -- but does not. */
1922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1923 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1924 {
1925 tree name;
1926
1927 /* If parsing tenatively, we should commit; we really are
1928 looking at a declaration. */
1929 /* Consume the first identifier. */
1930 name = cp_lexer_consume_token (parser->lexer)->value;
1931 /* Issue an error message. */
1932 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1933 /* If we're in a template class, it's possible that the user was
1934 referring to a type from a base class. For example:
1935
1936 template <typename T> struct A { typedef T X; };
1937 template <typename T> struct B : public A<T> { X x; };
1938
1939 The user should have said "typename A<T>::X". */
1940 if (processing_template_decl && current_class_type)
1941 {
1942 tree b;
1943
1944 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1945 b;
1946 b = TREE_CHAIN (b))
1947 {
1948 tree base_type = BINFO_TYPE (b);
1949 if (CLASS_TYPE_P (base_type)
1fb3244a 1950 && dependent_type_p (base_type))
8fbc5ae7
MM
1951 {
1952 tree field;
1953 /* Go from a particular instantiation of the
1954 template (which will have an empty TYPE_FIELDs),
1955 to the main version. */
353b4fc0 1956 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
8fbc5ae7
MM
1957 for (field = TYPE_FIELDS (base_type);
1958 field;
1959 field = TREE_CHAIN (field))
1960 if (TREE_CODE (field) == TYPE_DECL
1961 && DECL_NAME (field) == name)
1962 {
1963 error ("(perhaps `typename %T::%s' was intended)",
1964 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1965 break;
1966 }
1967 if (field)
1968 break;
1969 }
1970 }
1971 }
1972 /* Skip to the end of the declaration; there's no point in
1973 trying to process it. */
1974 cp_parser_skip_to_end_of_statement (parser);
1975
1976 return true;
1977 }
1978
1979 return false;
1980}
1981
a723baf1
MM
1982/* Consume tokens up to, and including, the next non-nested closing `)'.
1983 Returns TRUE iff we found a closing `)'. */
1984
1985static bool
1986cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
1987{
1988 unsigned nesting_depth = 0;
1989
1990 while (true)
1991 {
1992 cp_token *token;
1993
1994 /* If we've run out of tokens, then there is no closing `)'. */
1995 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1996 return false;
1997 /* Consume the token. */
1998 token = cp_lexer_consume_token (parser->lexer);
1999 /* If it is an `(', we have entered another level of nesting. */
2000 if (token->type == CPP_OPEN_PAREN)
2001 ++nesting_depth;
2002 /* If it is a `)', then we might be done. */
2003 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2004 return true;
2005 }
2006}
2007
2008/* Consume tokens until the next token is a `)', or a `,'. Returns
2009 TRUE if the next token is a `,'. */
2010
2011static bool
2012cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
2013{
2014 unsigned nesting_depth = 0;
2015
2016 while (true)
2017 {
2018 cp_token *token = cp_lexer_peek_token (parser->lexer);
2019
2020 /* If we've run out of tokens, then there is no closing `)'. */
2021 if (token->type == CPP_EOF)
2022 return false;
2023 /* If it is a `,' stop. */
2024 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
2025 return true;
2026 /* If it is a `)', stop. */
2027 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
2028 return false;
2029 /* If it is an `(', we have entered another level of nesting. */
2030 else if (token->type == CPP_OPEN_PAREN)
2031 ++nesting_depth;
2032 /* Consume the token. */
2033 token = cp_lexer_consume_token (parser->lexer);
2034 }
2035}
2036
2037/* Consume tokens until we reach the end of the current statement.
2038 Normally, that will be just before consuming a `;'. However, if a
2039 non-nested `}' comes first, then we stop before consuming that. */
2040
2041static void
94edc4ab 2042cp_parser_skip_to_end_of_statement (cp_parser* parser)
a723baf1
MM
2043{
2044 unsigned nesting_depth = 0;
2045
2046 while (true)
2047 {
2048 cp_token *token;
2049
2050 /* Peek at the next token. */
2051 token = cp_lexer_peek_token (parser->lexer);
2052 /* If we've run out of tokens, stop. */
2053 if (token->type == CPP_EOF)
2054 break;
2055 /* If the next token is a `;', we have reached the end of the
2056 statement. */
2057 if (token->type == CPP_SEMICOLON && !nesting_depth)
2058 break;
2059 /* If the next token is a non-nested `}', then we have reached
2060 the end of the current block. */
2061 if (token->type == CPP_CLOSE_BRACE)
2062 {
2063 /* If this is a non-nested `}', stop before consuming it.
2064 That way, when confronted with something like:
2065
2066 { 3 + }
2067
2068 we stop before consuming the closing `}', even though we
2069 have not yet reached a `;'. */
2070 if (nesting_depth == 0)
2071 break;
2072 /* If it is the closing `}' for a block that we have
2073 scanned, stop -- but only after consuming the token.
2074 That way given:
2075
2076 void f g () { ... }
2077 typedef int I;
2078
2079 we will stop after the body of the erroneously declared
2080 function, but before consuming the following `typedef'
2081 declaration. */
2082 if (--nesting_depth == 0)
2083 {
2084 cp_lexer_consume_token (parser->lexer);
2085 break;
2086 }
2087 }
2088 /* If it the next token is a `{', then we are entering a new
2089 block. Consume the entire block. */
2090 else if (token->type == CPP_OPEN_BRACE)
2091 ++nesting_depth;
2092 /* Consume the token. */
2093 cp_lexer_consume_token (parser->lexer);
2094 }
2095}
2096
e0860732
MM
2097/* This function is called at the end of a statement or declaration.
2098 If the next token is a semicolon, it is consumed; otherwise, error
2099 recovery is attempted. */
2100
2101static void
2102cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2103{
2104 /* Look for the trailing `;'. */
2105 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2106 {
2107 /* If there is additional (erroneous) input, skip to the end of
2108 the statement. */
2109 cp_parser_skip_to_end_of_statement (parser);
2110 /* If the next token is now a `;', consume it. */
2111 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2112 cp_lexer_consume_token (parser->lexer);
2113 }
2114}
2115
a723baf1
MM
2116/* Skip tokens until we have consumed an entire block, or until we
2117 have consumed a non-nested `;'. */
2118
2119static void
94edc4ab 2120cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
a723baf1
MM
2121{
2122 unsigned nesting_depth = 0;
2123
2124 while (true)
2125 {
2126 cp_token *token;
2127
2128 /* Peek at the next token. */
2129 token = cp_lexer_peek_token (parser->lexer);
2130 /* If we've run out of tokens, stop. */
2131 if (token->type == CPP_EOF)
2132 break;
2133 /* If the next token is a `;', we have reached the end of the
2134 statement. */
2135 if (token->type == CPP_SEMICOLON && !nesting_depth)
2136 {
2137 /* Consume the `;'. */
2138 cp_lexer_consume_token (parser->lexer);
2139 break;
2140 }
2141 /* Consume the token. */
2142 token = cp_lexer_consume_token (parser->lexer);
2143 /* If the next token is a non-nested `}', then we have reached
2144 the end of the current block. */
2145 if (token->type == CPP_CLOSE_BRACE
2146 && (nesting_depth == 0 || --nesting_depth == 0))
2147 break;
2148 /* If it the next token is a `{', then we are entering a new
2149 block. Consume the entire block. */
2150 if (token->type == CPP_OPEN_BRACE)
2151 ++nesting_depth;
2152 }
2153}
2154
2155/* Skip tokens until a non-nested closing curly brace is the next
2156 token. */
2157
2158static void
2159cp_parser_skip_to_closing_brace (cp_parser *parser)
2160{
2161 unsigned nesting_depth = 0;
2162
2163 while (true)
2164 {
2165 cp_token *token;
2166
2167 /* Peek at the next token. */
2168 token = cp_lexer_peek_token (parser->lexer);
2169 /* If we've run out of tokens, stop. */
2170 if (token->type == CPP_EOF)
2171 break;
2172 /* If the next token is a non-nested `}', then we have reached
2173 the end of the current block. */
2174 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2175 break;
2176 /* If it the next token is a `{', then we are entering a new
2177 block. Consume the entire block. */
2178 else if (token->type == CPP_OPEN_BRACE)
2179 ++nesting_depth;
2180 /* Consume the token. */
2181 cp_lexer_consume_token (parser->lexer);
2182 }
2183}
2184
2185/* Create a new C++ parser. */
2186
2187static cp_parser *
94edc4ab 2188cp_parser_new (void)
a723baf1
MM
2189{
2190 cp_parser *parser;
17211ab5
GK
2191 cp_lexer *lexer;
2192
2193 /* cp_lexer_new_main is called before calling ggc_alloc because
2194 cp_lexer_new_main might load a PCH file. */
2195 lexer = cp_lexer_new_main ();
a723baf1
MM
2196
2197 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
17211ab5 2198 parser->lexer = lexer;
a723baf1
MM
2199 parser->context = cp_parser_context_new (NULL);
2200
2201 /* For now, we always accept GNU extensions. */
2202 parser->allow_gnu_extensions_p = 1;
2203
2204 /* The `>' token is a greater-than operator, not the end of a
2205 template-id. */
2206 parser->greater_than_is_operator_p = true;
2207
2208 parser->default_arg_ok_p = true;
2209
2210 /* We are not parsing a constant-expression. */
2211 parser->constant_expression_p = false;
14d22dd6
MM
2212 parser->allow_non_constant_expression_p = false;
2213 parser->non_constant_expression_p = false;
a723baf1
MM
2214
2215 /* Local variable names are not forbidden. */
2216 parser->local_variables_forbidden_p = false;
2217
2218 /* We are not procesing an `extern "C"' declaration. */
2219 parser->in_unbraced_linkage_specification_p = false;
2220
2221 /* We are not processing a declarator. */
2222 parser->in_declarator_p = false;
2223
a723baf1
MM
2224 /* The unparsed function queue is empty. */
2225 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2226
2227 /* There are no classes being defined. */
2228 parser->num_classes_being_defined = 0;
2229
2230 /* No template parameters apply. */
2231 parser->num_template_parameter_lists = 0;
2232
2233 return parser;
2234}
2235
2236/* Lexical conventions [gram.lex] */
2237
2238/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2239 identifier. */
2240
2241static tree
94edc4ab 2242cp_parser_identifier (cp_parser* parser)
a723baf1
MM
2243{
2244 cp_token *token;
2245
2246 /* Look for the identifier. */
2247 token = cp_parser_require (parser, CPP_NAME, "identifier");
2248 /* Return the value. */
2249 return token ? token->value : error_mark_node;
2250}
2251
2252/* Basic concepts [gram.basic] */
2253
2254/* Parse a translation-unit.
2255
2256 translation-unit:
2257 declaration-seq [opt]
2258
2259 Returns TRUE if all went well. */
2260
2261static bool
94edc4ab 2262cp_parser_translation_unit (cp_parser* parser)
a723baf1
MM
2263{
2264 while (true)
2265 {
2266 cp_parser_declaration_seq_opt (parser);
2267
2268 /* If there are no tokens left then all went well. */
2269 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2270 break;
2271
2272 /* Otherwise, issue an error message. */
2273 cp_parser_error (parser, "expected declaration");
2274 return false;
2275 }
2276
2277 /* Consume the EOF token. */
2278 cp_parser_require (parser, CPP_EOF, "end-of-file");
2279
2280 /* Finish up. */
2281 finish_translation_unit ();
2282
2283 /* All went well. */
2284 return true;
2285}
2286
2287/* Expressions [gram.expr] */
2288
2289/* Parse a primary-expression.
2290
2291 primary-expression:
2292 literal
2293 this
2294 ( expression )
2295 id-expression
2296
2297 GNU Extensions:
2298
2299 primary-expression:
2300 ( compound-statement )
2301 __builtin_va_arg ( assignment-expression , type-id )
2302
2303 literal:
2304 __null
2305
2306 Returns a representation of the expression.
2307
2308 *IDK indicates what kind of id-expression (if any) was present.
2309
2310 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2311 used as the operand of a pointer-to-member. In that case,
2312 *QUALIFYING_CLASS gives the class that is used as the qualifying
2313 class in the pointer-to-member. */
2314
2315static tree
2316cp_parser_primary_expression (cp_parser *parser,
2317 cp_parser_id_kind *idk,
2318 tree *qualifying_class)
2319{
2320 cp_token *token;
2321
2322 /* Assume the primary expression is not an id-expression. */
2323 *idk = CP_PARSER_ID_KIND_NONE;
2324 /* And that it cannot be used as pointer-to-member. */
2325 *qualifying_class = NULL_TREE;
2326
2327 /* Peek at the next token. */
2328 token = cp_lexer_peek_token (parser->lexer);
2329 switch (token->type)
2330 {
2331 /* literal:
2332 integer-literal
2333 character-literal
2334 floating-literal
2335 string-literal
2336 boolean-literal */
2337 case CPP_CHAR:
2338 case CPP_WCHAR:
2339 case CPP_STRING:
2340 case CPP_WSTRING:
2341 case CPP_NUMBER:
2342 token = cp_lexer_consume_token (parser->lexer);
2343 return token->value;
2344
2345 case CPP_OPEN_PAREN:
2346 {
2347 tree expr;
2348 bool saved_greater_than_is_operator_p;
2349
2350 /* Consume the `('. */
2351 cp_lexer_consume_token (parser->lexer);
2352 /* Within a parenthesized expression, a `>' token is always
2353 the greater-than operator. */
2354 saved_greater_than_is_operator_p
2355 = parser->greater_than_is_operator_p;
2356 parser->greater_than_is_operator_p = true;
2357 /* If we see `( { ' then we are looking at the beginning of
2358 a GNU statement-expression. */
2359 if (cp_parser_allow_gnu_extensions_p (parser)
2360 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2361 {
2362 /* Statement-expressions are not allowed by the standard. */
2363 if (pedantic)
2364 pedwarn ("ISO C++ forbids braced-groups within expressions");
2365
2366 /* And they're not allowed outside of a function-body; you
2367 cannot, for example, write:
2368
2369 int i = ({ int j = 3; j + 1; });
2370
2371 at class or namespace scope. */
2372 if (!at_function_scope_p ())
2373 error ("statement-expressions are allowed only inside functions");
2374 /* Start the statement-expression. */
2375 expr = begin_stmt_expr ();
2376 /* Parse the compound-statement. */
2377 cp_parser_compound_statement (parser);
2378 /* Finish up. */
2379 expr = finish_stmt_expr (expr);
2380 }
2381 else
2382 {
2383 /* Parse the parenthesized expression. */
2384 expr = cp_parser_expression (parser);
2385 /* Let the front end know that this expression was
2386 enclosed in parentheses. This matters in case, for
2387 example, the expression is of the form `A::B', since
2388 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2389 not. */
2390 finish_parenthesized_expr (expr);
2391 }
2392 /* The `>' token might be the end of a template-id or
2393 template-parameter-list now. */
2394 parser->greater_than_is_operator_p
2395 = saved_greater_than_is_operator_p;
2396 /* Consume the `)'. */
2397 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2398 cp_parser_skip_to_end_of_statement (parser);
2399
2400 return expr;
2401 }
2402
2403 case CPP_KEYWORD:
2404 switch (token->keyword)
2405 {
2406 /* These two are the boolean literals. */
2407 case RID_TRUE:
2408 cp_lexer_consume_token (parser->lexer);
2409 return boolean_true_node;
2410 case RID_FALSE:
2411 cp_lexer_consume_token (parser->lexer);
2412 return boolean_false_node;
2413
2414 /* The `__null' literal. */
2415 case RID_NULL:
2416 cp_lexer_consume_token (parser->lexer);
2417 return null_node;
2418
2419 /* Recognize the `this' keyword. */
2420 case RID_THIS:
2421 cp_lexer_consume_token (parser->lexer);
2422 if (parser->local_variables_forbidden_p)
2423 {
2424 error ("`this' may not be used in this context");
2425 return error_mark_node;
2426 }
14d22dd6
MM
2427 /* Pointers cannot appear in constant-expressions. */
2428 if (parser->constant_expression_p)
2429 {
2430 if (!parser->allow_non_constant_expression_p)
2431 return cp_parser_non_constant_expression ("`this'");
2432 parser->non_constant_expression_p = true;
2433 }
a723baf1
MM
2434 return finish_this_expr ();
2435
2436 /* The `operator' keyword can be the beginning of an
2437 id-expression. */
2438 case RID_OPERATOR:
2439 goto id_expression;
2440
2441 case RID_FUNCTION_NAME:
2442 case RID_PRETTY_FUNCTION_NAME:
2443 case RID_C99_FUNCTION_NAME:
2444 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2445 __func__ are the names of variables -- but they are
2446 treated specially. Therefore, they are handled here,
2447 rather than relying on the generic id-expression logic
2448 below. Gramatically, these names are id-expressions.
2449
2450 Consume the token. */
2451 token = cp_lexer_consume_token (parser->lexer);
2452 /* Look up the name. */
2453 return finish_fname (token->value);
2454
2455 case RID_VA_ARG:
2456 {
2457 tree expression;
2458 tree type;
2459
2460 /* The `__builtin_va_arg' construct is used to handle
2461 `va_arg'. Consume the `__builtin_va_arg' token. */
2462 cp_lexer_consume_token (parser->lexer);
2463 /* Look for the opening `('. */
2464 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2465 /* Now, parse the assignment-expression. */
2466 expression = cp_parser_assignment_expression (parser);
2467 /* Look for the `,'. */
2468 cp_parser_require (parser, CPP_COMMA, "`,'");
2469 /* Parse the type-id. */
2470 type = cp_parser_type_id (parser);
2471 /* Look for the closing `)'. */
2472 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
2473 /* Using `va_arg' in a constant-expression is not
2474 allowed. */
2475 if (parser->constant_expression_p)
2476 {
2477 if (!parser->allow_non_constant_expression_p)
2478 return cp_parser_non_constant_expression ("`va_arg'");
2479 parser->non_constant_expression_p = true;
2480 }
a723baf1
MM
2481 return build_x_va_arg (expression, type);
2482 }
2483
2484 default:
2485 cp_parser_error (parser, "expected primary-expression");
2486 return error_mark_node;
2487 }
2488 /* Fall through. */
2489
2490 /* An id-expression can start with either an identifier, a
2491 `::' as the beginning of a qualified-id, or the "operator"
2492 keyword. */
2493 case CPP_NAME:
2494 case CPP_SCOPE:
2495 case CPP_TEMPLATE_ID:
2496 case CPP_NESTED_NAME_SPECIFIER:
2497 {
2498 tree id_expression;
2499 tree decl;
2500
2501 id_expression:
2502 /* Parse the id-expression. */
2503 id_expression
2504 = cp_parser_id_expression (parser,
2505 /*template_keyword_p=*/false,
2506 /*check_dependency_p=*/true,
2507 /*template_p=*/NULL);
2508 if (id_expression == error_mark_node)
2509 return error_mark_node;
2510 /* If we have a template-id, then no further lookup is
2511 required. If the template-id was for a template-class, we
2512 will sometimes have a TYPE_DECL at this point. */
2513 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2514 || TREE_CODE (id_expression) == TYPE_DECL)
2515 decl = id_expression;
2516 /* Look up the name. */
2517 else
2518 {
2519 decl = cp_parser_lookup_name_simple (parser, id_expression);
2520 /* If name lookup gives us a SCOPE_REF, then the
2521 qualifying scope was dependent. Just propagate the
2522 name. */
2523 if (TREE_CODE (decl) == SCOPE_REF)
2524 {
2525 if (TYPE_P (TREE_OPERAND (decl, 0)))
2526 *qualifying_class = TREE_OPERAND (decl, 0);
14d22dd6
MM
2527 /* Since this name was dependent, the expression isn't
2528 constant -- yet. No error is issued because it
2529 might be constant when things are instantiated. */
2530 if (parser->constant_expression_p)
2531 parser->non_constant_expression_p = true;
a723baf1
MM
2532 return decl;
2533 }
2534 /* Check to see if DECL is a local variable in a context
2535 where that is forbidden. */
2536 if (parser->local_variables_forbidden_p
2537 && local_variable_p (decl))
2538 {
2539 /* It might be that we only found DECL because we are
2540 trying to be generous with pre-ISO scoping rules.
2541 For example, consider:
2542
2543 int i;
2544 void g() {
2545 for (int i = 0; i < 10; ++i) {}
2546 extern void f(int j = i);
2547 }
2548
2549 Here, name look up will originally find the out
2550 of scope `i'. We need to issue a warning message,
2551 but then use the global `i'. */
2552 decl = check_for_out_of_scope_variable (decl);
2553 if (local_variable_p (decl))
2554 {
2555 error ("local variable `%D' may not appear in this context",
2556 decl);
2557 return error_mark_node;
2558 }
2559 }
2560
a723baf1
MM
2561 if (!parser->scope
2562 && decl == error_mark_node
2563 && processing_template_decl)
2564 {
8fbc5ae7
MM
2565 /* Unqualified name lookup failed while processing a
2566 template. */
a723baf1 2567 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
8fbc5ae7
MM
2568 /* If the next token is a parenthesis, assume that
2569 Koenig lookup will succeed when instantiating the
2570 template. */
2571 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
2572 return build_min_nt (LOOKUP_EXPR, id_expression);
2573 /* If we're not doing Koenig lookup, issue an error. */
2574 error ("`%D' has not been declared", id_expression);
2575 return error_mark_node;
a723baf1
MM
2576 }
2577 else if (decl == error_mark_node
2578 && !processing_template_decl)
2579 {
2580 if (!parser->scope)
2581 {
2582 /* It may be resolvable as a koenig lookup function
2583 call. */
2584 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2585 return id_expression;
2586 }
2587 else if (TYPE_P (parser->scope)
2588 && !COMPLETE_TYPE_P (parser->scope))
2589 error ("incomplete type `%T' used in nested name specifier",
2590 parser->scope);
2591 else if (parser->scope != global_namespace)
2592 error ("`%D' is not a member of `%D'",
2593 id_expression, parser->scope);
2594 else
2595 error ("`::%D' has not been declared", id_expression);
2596 }
2597 /* If DECL is a variable would be out of scope under
2598 ANSI/ISO rules, but in scope in the ARM, name lookup
2599 will succeed. Issue a diagnostic here. */
2600 else
2601 decl = check_for_out_of_scope_variable (decl);
2602
2603 /* Remember that the name was used in the definition of
2604 the current class so that we can check later to see if
2605 the meaning would have been different after the class
2606 was entirely defined. */
2607 if (!parser->scope && decl != error_mark_node)
2608 maybe_note_name_used_in_class (id_expression, decl);
2609 }
2610
2611 /* If we didn't find anything, or what we found was a type,
2612 then this wasn't really an id-expression. */
c006d942
MM
2613 if (TREE_CODE (decl) == TEMPLATE_DECL
2614 && !DECL_FUNCTION_TEMPLATE_P (decl))
2615 {
2616 cp_parser_error (parser, "missing template arguments");
2617 return error_mark_node;
2618 }
2619 else if (TREE_CODE (decl) == TYPE_DECL
2620 || TREE_CODE (decl) == NAMESPACE_DECL)
a723baf1
MM
2621 {
2622 cp_parser_error (parser,
2623 "expected primary-expression");
2624 return error_mark_node;
2625 }
2626
2627 /* If the name resolved to a template parameter, there is no
2628 need to look it up again later. Similarly, we resolve
2629 enumeration constants to their underlying values. */
2630 if (TREE_CODE (decl) == CONST_DECL)
2631 {
2632 *idk = CP_PARSER_ID_KIND_NONE;
2633 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2634 return DECL_INITIAL (decl);
2635 return decl;
2636 }
2637 else
2638 {
2639 bool dependent_p;
2640
2641 /* If the declaration was explicitly qualified indicate
2642 that. The semantics of `A::f(3)' are different than
2643 `f(3)' if `f' is virtual. */
2644 *idk = (parser->scope
2645 ? CP_PARSER_ID_KIND_QUALIFIED
2646 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2647 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2648 : CP_PARSER_ID_KIND_UNQUALIFIED));
2649
2650
2651 /* [temp.dep.expr]
2652
2653 An id-expression is type-dependent if it contains an
2654 identifier that was declared with a dependent type.
2655
2656 As an optimization, we could choose not to create a
2657 LOOKUP_EXPR for a name that resolved to a local
2658 variable in the template function that we are currently
2659 declaring; such a name cannot ever resolve to anything
2660 else. If we did that we would not have to look up
2661 these names at instantiation time.
2662
2663 The standard is not very specific about an
2664 id-expression that names a set of overloaded functions.
2665 What if some of them have dependent types and some of
2666 them do not? Presumably, such a name should be treated
2667 as a dependent name. */
2668 /* Assume the name is not dependent. */
2669 dependent_p = false;
2670 if (!processing_template_decl)
2671 /* No names are dependent outside a template. */
2672 ;
2673 /* A template-id where the name of the template was not
2674 resolved is definitely dependent. */
2675 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2676 && (TREE_CODE (TREE_OPERAND (decl, 0))
2677 == IDENTIFIER_NODE))
2678 dependent_p = true;
2679 /* For anything except an overloaded function, just check
2680 its type. */
2681 else if (!is_overloaded_fn (decl))
2682 dependent_p
1fb3244a 2683 = dependent_type_p (TREE_TYPE (decl));
a723baf1
MM
2684 /* For a set of overloaded functions, check each of the
2685 functions. */
2686 else
2687 {
2688 tree fns = decl;
2689
2690 if (BASELINK_P (fns))
2691 fns = BASELINK_FUNCTIONS (fns);
2692
2693 /* For a template-id, check to see if the template
2694 arguments are dependent. */
2695 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2696 {
2697 tree args = TREE_OPERAND (fns, 1);
2698
2699 if (args && TREE_CODE (args) == TREE_LIST)
2700 {
2701 while (args)
2702 {
1fb3244a 2703 if (dependent_template_arg_p (TREE_VALUE (args)))
a723baf1
MM
2704 {
2705 dependent_p = true;
2706 break;
2707 }
2708 args = TREE_CHAIN (args);
2709 }
2710 }
2711 else if (args && TREE_CODE (args) == TREE_VEC)
2712 {
2713 int i;
2714 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
1fb3244a 2715 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
a723baf1
MM
2716 {
2717 dependent_p = true;
2718 break;
2719 }
2720 }
2721
2722 /* The functions are those referred to by the
2723 template-id. */
2724 fns = TREE_OPERAND (fns, 0);
2725 }
2726
2727 /* If there are no dependent template arguments, go
2728 through the overlaoded functions. */
2729 while (fns && !dependent_p)
2730 {
2731 tree fn = OVL_CURRENT (fns);
2732
2733 /* Member functions of dependent classes are
2734 dependent. */
2735 if (TREE_CODE (fn) == FUNCTION_DECL
1fb3244a 2736 && type_dependent_expression_p (fn))
a723baf1
MM
2737 dependent_p = true;
2738 else if (TREE_CODE (fn) == TEMPLATE_DECL
1fb3244a 2739 && dependent_template_p (fn))
a723baf1
MM
2740 dependent_p = true;
2741
2742 fns = OVL_NEXT (fns);
2743 }
2744 }
2745
2746 /* If the name was dependent on a template parameter,
2747 we will resolve the name at instantiation time. */
2748 if (dependent_p)
2749 {
2750 /* Create a SCOPE_REF for qualified names. */
2751 if (parser->scope)
2752 {
2753 if (TYPE_P (parser->scope))
2754 *qualifying_class = parser->scope;
14d22dd6
MM
2755 /* Since this name was dependent, the expression isn't
2756 constant -- yet. No error is issued because it
2757 might be constant when things are instantiated. */
2758 if (parser->constant_expression_p)
2759 parser->non_constant_expression_p = true;
a723baf1
MM
2760 return build_nt (SCOPE_REF,
2761 parser->scope,
2762 id_expression);
2763 }
2764 /* A TEMPLATE_ID already contains all the information
2765 we need. */
2766 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2767 return id_expression;
14d22dd6
MM
2768 /* Since this name was dependent, the expression isn't
2769 constant -- yet. No error is issued because it
2770 might be constant when things are instantiated. */
2771 if (parser->constant_expression_p)
2772 parser->non_constant_expression_p = true;
a723baf1
MM
2773 /* Create a LOOKUP_EXPR for other unqualified names. */
2774 return build_min_nt (LOOKUP_EXPR, id_expression);
2775 }
2776
14d22dd6
MM
2777 /* Only certain kinds of names are allowed in constant
2778 expression. Enumerators have already been handled
2779 above. */
2780 if (parser->constant_expression_p
2781 /* Non-type template parameters of integral or
2782 enumeration type. */
2783 && !(TREE_CODE (decl) == TEMPLATE_PARM_INDEX
2784 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl)))
2785 /* Const variables or static data members of integral
2786 or enumeration types initialized with constant
2787 expressions. */
2788 && !(TREE_CODE (decl) == VAR_DECL
2789 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2790 && DECL_INITIAL (decl)
2791 && TREE_CONSTANT (DECL_INITIAL (decl))))
2792 {
2793 if (!parser->allow_non_constant_expression_p)
2794 return cp_parser_non_constant_id_expression (decl);
2795 parser->non_constant_expression_p = true;
2796 }
2797
a723baf1
MM
2798 if (parser->scope)
2799 {
2800 decl = (adjust_result_of_qualified_name_lookup
2801 (decl, parser->scope, current_class_type));
2802 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2803 *qualifying_class = parser->scope;
2804 }
a723baf1
MM
2805 else
2806 /* Transform references to non-static data members into
2807 COMPONENT_REFs. */
2808 decl = hack_identifier (decl, id_expression);
f74dbcec
JM
2809
2810 /* Resolve references to variables of anonymous unions
2811 into COMPONENT_REFs. */
2812 if (TREE_CODE (decl) == ALIAS_DECL)
2813 decl = DECL_INITIAL (decl);
a723baf1
MM
2814 }
2815
2816 if (TREE_DEPRECATED (decl))
2817 warn_deprecated_use (decl);
2818
2819 return decl;
2820 }
2821
2822 /* Anything else is an error. */
2823 default:
2824 cp_parser_error (parser, "expected primary-expression");
2825 return error_mark_node;
2826 }
2827}
2828
2829/* Parse an id-expression.
2830
2831 id-expression:
2832 unqualified-id
2833 qualified-id
2834
2835 qualified-id:
2836 :: [opt] nested-name-specifier template [opt] unqualified-id
2837 :: identifier
2838 :: operator-function-id
2839 :: template-id
2840
2841 Return a representation of the unqualified portion of the
2842 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2843 a `::' or nested-name-specifier.
2844
2845 Often, if the id-expression was a qualified-id, the caller will
2846 want to make a SCOPE_REF to represent the qualified-id. This
2847 function does not do this in order to avoid wastefully creating
2848 SCOPE_REFs when they are not required.
2849
a723baf1
MM
2850 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2851 `template' keyword.
2852
2853 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2854 uninstantiated templates.
2855
15d2cb19 2856 If *TEMPLATE_P is non-NULL, it is set to true iff the
a723baf1
MM
2857 `template' keyword is used to explicitly indicate that the entity
2858 named is a template. */
2859
2860static tree
2861cp_parser_id_expression (cp_parser *parser,
2862 bool template_keyword_p,
2863 bool check_dependency_p,
2864 bool *template_p)
2865{
2866 bool global_scope_p;
2867 bool nested_name_specifier_p;
2868
2869 /* Assume the `template' keyword was not used. */
2870 if (template_p)
2871 *template_p = false;
2872
2873 /* Look for the optional `::' operator. */
2874 global_scope_p
2875 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2876 != NULL_TREE);
2877 /* Look for the optional nested-name-specifier. */
2878 nested_name_specifier_p
2879 = (cp_parser_nested_name_specifier_opt (parser,
2880 /*typename_keyword_p=*/false,
2881 check_dependency_p,
2882 /*type_p=*/false)
2883 != NULL_TREE);
2884 /* If there is a nested-name-specifier, then we are looking at
2885 the first qualified-id production. */
2886 if (nested_name_specifier_p)
2887 {
2888 tree saved_scope;
2889 tree saved_object_scope;
2890 tree saved_qualifying_scope;
2891 tree unqualified_id;
2892 bool is_template;
2893
2894 /* See if the next token is the `template' keyword. */
2895 if (!template_p)
2896 template_p = &is_template;
2897 *template_p = cp_parser_optional_template_keyword (parser);
2898 /* Name lookup we do during the processing of the
2899 unqualified-id might obliterate SCOPE. */
2900 saved_scope = parser->scope;
2901 saved_object_scope = parser->object_scope;
2902 saved_qualifying_scope = parser->qualifying_scope;
2903 /* Process the final unqualified-id. */
2904 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2905 check_dependency_p);
2906 /* Restore the SAVED_SCOPE for our caller. */
2907 parser->scope = saved_scope;
2908 parser->object_scope = saved_object_scope;
2909 parser->qualifying_scope = saved_qualifying_scope;
2910
2911 return unqualified_id;
2912 }
2913 /* Otherwise, if we are in global scope, then we are looking at one
2914 of the other qualified-id productions. */
2915 else if (global_scope_p)
2916 {
2917 cp_token *token;
2918 tree id;
2919
e5976695
MM
2920 /* Peek at the next token. */
2921 token = cp_lexer_peek_token (parser->lexer);
2922
2923 /* If it's an identifier, and the next token is not a "<", then
2924 we can avoid the template-id case. This is an optimization
2925 for this common case. */
2926 if (token->type == CPP_NAME
2927 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2928 return cp_parser_identifier (parser);
2929
a723baf1
MM
2930 cp_parser_parse_tentatively (parser);
2931 /* Try a template-id. */
2932 id = cp_parser_template_id (parser,
2933 /*template_keyword_p=*/false,
2934 /*check_dependency_p=*/true);
2935 /* If that worked, we're done. */
2936 if (cp_parser_parse_definitely (parser))
2937 return id;
2938
e5976695
MM
2939 /* Peek at the next token. (Changes in the token buffer may
2940 have invalidated the pointer obtained above.) */
a723baf1
MM
2941 token = cp_lexer_peek_token (parser->lexer);
2942
2943 switch (token->type)
2944 {
2945 case CPP_NAME:
2946 return cp_parser_identifier (parser);
2947
2948 case CPP_KEYWORD:
2949 if (token->keyword == RID_OPERATOR)
2950 return cp_parser_operator_function_id (parser);
2951 /* Fall through. */
2952
2953 default:
2954 cp_parser_error (parser, "expected id-expression");
2955 return error_mark_node;
2956 }
2957 }
2958 else
2959 return cp_parser_unqualified_id (parser, template_keyword_p,
2960 /*check_dependency_p=*/true);
2961}
2962
2963/* Parse an unqualified-id.
2964
2965 unqualified-id:
2966 identifier
2967 operator-function-id
2968 conversion-function-id
2969 ~ class-name
2970 template-id
2971
2972 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2973 keyword, in a construct like `A::template ...'.
2974
2975 Returns a representation of unqualified-id. For the `identifier'
2976 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2977 production a BIT_NOT_EXPR is returned; the operand of the
2978 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2979 other productions, see the documentation accompanying the
2980 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2981 names are looked up in uninstantiated templates. */
2982
2983static tree
94edc4ab
NN
2984cp_parser_unqualified_id (cp_parser* parser,
2985 bool template_keyword_p,
2986 bool check_dependency_p)
a723baf1
MM
2987{
2988 cp_token *token;
2989
2990 /* Peek at the next token. */
2991 token = cp_lexer_peek_token (parser->lexer);
2992
2993 switch (token->type)
2994 {
2995 case CPP_NAME:
2996 {
2997 tree id;
2998
2999 /* We don't know yet whether or not this will be a
3000 template-id. */
3001 cp_parser_parse_tentatively (parser);
3002 /* Try a template-id. */
3003 id = cp_parser_template_id (parser, template_keyword_p,
3004 check_dependency_p);
3005 /* If it worked, we're done. */
3006 if (cp_parser_parse_definitely (parser))
3007 return id;
3008 /* Otherwise, it's an ordinary identifier. */
3009 return cp_parser_identifier (parser);
3010 }
3011
3012 case CPP_TEMPLATE_ID:
3013 return cp_parser_template_id (parser, template_keyword_p,
3014 check_dependency_p);
3015
3016 case CPP_COMPL:
3017 {
3018 tree type_decl;
3019 tree qualifying_scope;
3020 tree object_scope;
3021 tree scope;
3022
3023 /* Consume the `~' token. */
3024 cp_lexer_consume_token (parser->lexer);
3025 /* Parse the class-name. The standard, as written, seems to
3026 say that:
3027
3028 template <typename T> struct S { ~S (); };
3029 template <typename T> S<T>::~S() {}
3030
3031 is invalid, since `~' must be followed by a class-name, but
3032 `S<T>' is dependent, and so not known to be a class.
3033 That's not right; we need to look in uninstantiated
3034 templates. A further complication arises from:
3035
3036 template <typename T> void f(T t) {
3037 t.T::~T();
3038 }
3039
3040 Here, it is not possible to look up `T' in the scope of `T'
3041 itself. We must look in both the current scope, and the
3042 scope of the containing complete expression.
3043
3044 Yet another issue is:
3045
3046 struct S {
3047 int S;
3048 ~S();
3049 };
3050
3051 S::~S() {}
3052
3053 The standard does not seem to say that the `S' in `~S'
3054 should refer to the type `S' and not the data member
3055 `S::S'. */
3056
3057 /* DR 244 says that we look up the name after the "~" in the
3058 same scope as we looked up the qualifying name. That idea
3059 isn't fully worked out; it's more complicated than that. */
3060 scope = parser->scope;
3061 object_scope = parser->object_scope;
3062 qualifying_scope = parser->qualifying_scope;
3063
3064 /* If the name is of the form "X::~X" it's OK. */
3065 if (scope && TYPE_P (scope)
3066 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3067 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3068 == CPP_OPEN_PAREN)
3069 && (cp_lexer_peek_token (parser->lexer)->value
3070 == TYPE_IDENTIFIER (scope)))
3071 {
3072 cp_lexer_consume_token (parser->lexer);
3073 return build_nt (BIT_NOT_EXPR, scope);
3074 }
3075
3076 /* If there was an explicit qualification (S::~T), first look
3077 in the scope given by the qualification (i.e., S). */
3078 if (scope)
3079 {
3080 cp_parser_parse_tentatively (parser);
3081 type_decl = cp_parser_class_name (parser,
3082 /*typename_keyword_p=*/false,
3083 /*template_keyword_p=*/false,
3084 /*type_p=*/false,
3085 /*check_access_p=*/true,
3086 /*check_dependency=*/false,
3087 /*class_head_p=*/false);
3088 if (cp_parser_parse_definitely (parser))
3089 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3090 }
3091 /* In "N::S::~S", look in "N" as well. */
3092 if (scope && qualifying_scope)
3093 {
3094 cp_parser_parse_tentatively (parser);
3095 parser->scope = qualifying_scope;
3096 parser->object_scope = NULL_TREE;
3097 parser->qualifying_scope = NULL_TREE;
3098 type_decl
3099 = cp_parser_class_name (parser,
3100 /*typename_keyword_p=*/false,
3101 /*template_keyword_p=*/false,
3102 /*type_p=*/false,
3103 /*check_access_p=*/true,
3104 /*check_dependency=*/false,
3105 /*class_head_p=*/false);
3106 if (cp_parser_parse_definitely (parser))
3107 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3108 }
3109 /* In "p->S::~T", look in the scope given by "*p" as well. */
3110 else if (object_scope)
3111 {
3112 cp_parser_parse_tentatively (parser);
3113 parser->scope = object_scope;
3114 parser->object_scope = NULL_TREE;
3115 parser->qualifying_scope = NULL_TREE;
3116 type_decl
3117 = cp_parser_class_name (parser,
3118 /*typename_keyword_p=*/false,
3119 /*template_keyword_p=*/false,
3120 /*type_p=*/false,
3121 /*check_access_p=*/true,
3122 /*check_dependency=*/false,
3123 /*class_head_p=*/false);
3124 if (cp_parser_parse_definitely (parser))
3125 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3126 }
3127 /* Look in the surrounding context. */
3128 parser->scope = NULL_TREE;
3129 parser->object_scope = NULL_TREE;
3130 parser->qualifying_scope = NULL_TREE;
3131 type_decl
3132 = cp_parser_class_name (parser,
3133 /*typename_keyword_p=*/false,
3134 /*template_keyword_p=*/false,
3135 /*type_p=*/false,
3136 /*check_access_p=*/true,
3137 /*check_dependency=*/false,
3138 /*class_head_p=*/false);
3139 /* If an error occurred, assume that the name of the
3140 destructor is the same as the name of the qualifying
3141 class. That allows us to keep parsing after running
3142 into ill-formed destructor names. */
3143 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3144 return build_nt (BIT_NOT_EXPR, scope);
3145 else if (type_decl == error_mark_node)
3146 return error_mark_node;
3147
3148 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3149 }
3150
3151 case CPP_KEYWORD:
3152 if (token->keyword == RID_OPERATOR)
3153 {
3154 tree id;
3155
3156 /* This could be a template-id, so we try that first. */
3157 cp_parser_parse_tentatively (parser);
3158 /* Try a template-id. */
3159 id = cp_parser_template_id (parser, template_keyword_p,
3160 /*check_dependency_p=*/true);
3161 /* If that worked, we're done. */
3162 if (cp_parser_parse_definitely (parser))
3163 return id;
3164 /* We still don't know whether we're looking at an
3165 operator-function-id or a conversion-function-id. */
3166 cp_parser_parse_tentatively (parser);
3167 /* Try an operator-function-id. */
3168 id = cp_parser_operator_function_id (parser);
3169 /* If that didn't work, try a conversion-function-id. */
3170 if (!cp_parser_parse_definitely (parser))
3171 id = cp_parser_conversion_function_id (parser);
3172
3173 return id;
3174 }
3175 /* Fall through. */
3176
3177 default:
3178 cp_parser_error (parser, "expected unqualified-id");
3179 return error_mark_node;
3180 }
3181}
3182
3183/* Parse an (optional) nested-name-specifier.
3184
3185 nested-name-specifier:
3186 class-or-namespace-name :: nested-name-specifier [opt]
3187 class-or-namespace-name :: template nested-name-specifier [opt]
3188
3189 PARSER->SCOPE should be set appropriately before this function is
3190 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3191 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3192 in name lookups.
3193
3194 Sets PARSER->SCOPE to the class (TYPE) or namespace
3195 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3196 it unchanged if there is no nested-name-specifier. Returns the new
3197 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3198
3199static tree
3200cp_parser_nested_name_specifier_opt (cp_parser *parser,
3201 bool typename_keyword_p,
3202 bool check_dependency_p,
3203 bool type_p)
3204{
3205 bool success = false;
3206 tree access_check = NULL_TREE;
3207 ptrdiff_t start;
2050a1bb 3208 cp_token* token;
a723baf1
MM
3209
3210 /* If the next token corresponds to a nested name specifier, there
2050a1bb
MM
3211 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3212 false, it may have been true before, in which case something
3213 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3214 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3215 CHECK_DEPENDENCY_P is false, we have to fall through into the
3216 main loop. */
3217 if (check_dependency_p
3218 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3219 {
3220 cp_parser_pre_parsed_nested_name_specifier (parser);
a723baf1
MM
3221 return parser->scope;
3222 }
3223
3224 /* Remember where the nested-name-specifier starts. */
3225 if (cp_parser_parsing_tentatively (parser)
3226 && !cp_parser_committed_to_tentative_parse (parser))
3227 {
2050a1bb 3228 token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
3229 start = cp_lexer_token_difference (parser->lexer,
3230 parser->lexer->first_token,
2050a1bb 3231 token);
a723baf1
MM
3232 }
3233 else
3234 start = -1;
3235
cf22909c
KL
3236 push_deferring_access_checks (true);
3237
a723baf1
MM
3238 while (true)
3239 {
3240 tree new_scope;
3241 tree old_scope;
3242 tree saved_qualifying_scope;
a723baf1
MM
3243 bool template_keyword_p;
3244
2050a1bb
MM
3245 /* Spot cases that cannot be the beginning of a
3246 nested-name-specifier. */
3247 token = cp_lexer_peek_token (parser->lexer);
3248
3249 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3250 the already parsed nested-name-specifier. */
3251 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3252 {
3253 /* Grab the nested-name-specifier and continue the loop. */
3254 cp_parser_pre_parsed_nested_name_specifier (parser);
3255 success = true;
3256 continue;
3257 }
3258
a723baf1
MM
3259 /* Spot cases that cannot be the beginning of a
3260 nested-name-specifier. On the second and subsequent times
3261 through the loop, we look for the `template' keyword. */
f7b5ecd9 3262 if (success && token->keyword == RID_TEMPLATE)
a723baf1
MM
3263 ;
3264 /* A template-id can start a nested-name-specifier. */
f7b5ecd9 3265 else if (token->type == CPP_TEMPLATE_ID)
a723baf1
MM
3266 ;
3267 else
3268 {
3269 /* If the next token is not an identifier, then it is
3270 definitely not a class-or-namespace-name. */
f7b5ecd9 3271 if (token->type != CPP_NAME)
a723baf1
MM
3272 break;
3273 /* If the following token is neither a `<' (to begin a
3274 template-id), nor a `::', then we are not looking at a
3275 nested-name-specifier. */
3276 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3277 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3278 break;
3279 }
3280
3281 /* The nested-name-specifier is optional, so we parse
3282 tentatively. */
3283 cp_parser_parse_tentatively (parser);
3284
3285 /* Look for the optional `template' keyword, if this isn't the
3286 first time through the loop. */
3287 if (success)
3288 template_keyword_p = cp_parser_optional_template_keyword (parser);
3289 else
3290 template_keyword_p = false;
3291
3292 /* Save the old scope since the name lookup we are about to do
3293 might destroy it. */
3294 old_scope = parser->scope;
3295 saved_qualifying_scope = parser->qualifying_scope;
3296 /* Parse the qualifying entity. */
3297 new_scope
3298 = cp_parser_class_or_namespace_name (parser,
3299 typename_keyword_p,
3300 template_keyword_p,
3301 check_dependency_p,
3302 type_p);
3303 /* Look for the `::' token. */
3304 cp_parser_require (parser, CPP_SCOPE, "`::'");
3305
3306 /* If we found what we wanted, we keep going; otherwise, we're
3307 done. */
3308 if (!cp_parser_parse_definitely (parser))
3309 {
3310 bool error_p = false;
3311
3312 /* Restore the OLD_SCOPE since it was valid before the
3313 failed attempt at finding the last
3314 class-or-namespace-name. */
3315 parser->scope = old_scope;
3316 parser->qualifying_scope = saved_qualifying_scope;
3317 /* If the next token is an identifier, and the one after
3318 that is a `::', then any valid interpretation would have
3319 found a class-or-namespace-name. */
3320 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3321 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3322 == CPP_SCOPE)
3323 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3324 != CPP_COMPL))
3325 {
3326 token = cp_lexer_consume_token (parser->lexer);
3327 if (!error_p)
3328 {
3329 tree decl;
3330
3331 decl = cp_parser_lookup_name_simple (parser, token->value);
3332 if (TREE_CODE (decl) == TEMPLATE_DECL)
3333 error ("`%D' used without template parameters",
3334 decl);
3335 else if (parser->scope)
3336 {
3337 if (TYPE_P (parser->scope))
3338 error ("`%T::%D' is not a class-name or "
3339 "namespace-name",
3340 parser->scope, token->value);
3341 else
3342 error ("`%D::%D' is not a class-name or "
3343 "namespace-name",
3344 parser->scope, token->value);
3345 }
3346 else
3347 error ("`%D' is not a class-name or namespace-name",
3348 token->value);
3349 parser->scope = NULL_TREE;
3350 error_p = true;
eea9800f
MM
3351 /* Treat this as a successful nested-name-specifier
3352 due to:
3353
3354 [basic.lookup.qual]
3355
3356 If the name found is not a class-name (clause
3357 _class_) or namespace-name (_namespace.def_), the
3358 program is ill-formed. */
3359 success = true;
a723baf1
MM
3360 }
3361 cp_lexer_consume_token (parser->lexer);
3362 }
3363 break;
3364 }
3365
3366 /* We've found one valid nested-name-specifier. */
3367 success = true;
3368 /* Make sure we look in the right scope the next time through
3369 the loop. */
3370 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3371 ? TREE_TYPE (new_scope)
3372 : new_scope);
3373 /* If it is a class scope, try to complete it; we are about to
3374 be looking up names inside the class. */
8fbc5ae7
MM
3375 if (TYPE_P (parser->scope)
3376 /* Since checking types for dependency can be expensive,
3377 avoid doing it if the type is already complete. */
3378 && !COMPLETE_TYPE_P (parser->scope)
3379 /* Do not try to complete dependent types. */
1fb3244a 3380 && !dependent_type_p (parser->scope))
a723baf1
MM
3381 complete_type (parser->scope);
3382 }
3383
cf22909c
KL
3384 /* Retrieve any deferred checks. Do not pop this access checks yet
3385 so the memory will not be reclaimed during token replacing below. */
3386 access_check = get_deferred_access_checks ();
3387
a723baf1
MM
3388 /* If parsing tentatively, replace the sequence of tokens that makes
3389 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3390 token. That way, should we re-parse the token stream, we will
3391 not have to repeat the effort required to do the parse, nor will
3392 we issue duplicate error messages. */
3393 if (success && start >= 0)
3394 {
a723baf1
MM
3395 /* Find the token that corresponds to the start of the
3396 template-id. */
3397 token = cp_lexer_advance_token (parser->lexer,
3398 parser->lexer->first_token,
3399 start);
3400
a723baf1
MM
3401 /* Reset the contents of the START token. */
3402 token->type = CPP_NESTED_NAME_SPECIFIER;
3403 token->value = build_tree_list (access_check, parser->scope);
3404 TREE_TYPE (token->value) = parser->qualifying_scope;
3405 token->keyword = RID_MAX;
3406 /* Purge all subsequent tokens. */
3407 cp_lexer_purge_tokens_after (parser->lexer, token);
3408 }
3409
cf22909c 3410 pop_deferring_access_checks ();
a723baf1
MM
3411 return success ? parser->scope : NULL_TREE;
3412}
3413
3414/* Parse a nested-name-specifier. See
3415 cp_parser_nested_name_specifier_opt for details. This function
3416 behaves identically, except that it will an issue an error if no
3417 nested-name-specifier is present, and it will return
3418 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3419 is present. */
3420
3421static tree
3422cp_parser_nested_name_specifier (cp_parser *parser,
3423 bool typename_keyword_p,
3424 bool check_dependency_p,
3425 bool type_p)
3426{
3427 tree scope;
3428
3429 /* Look for the nested-name-specifier. */
3430 scope = cp_parser_nested_name_specifier_opt (parser,
3431 typename_keyword_p,
3432 check_dependency_p,
3433 type_p);
3434 /* If it was not present, issue an error message. */
3435 if (!scope)
3436 {
3437 cp_parser_error (parser, "expected nested-name-specifier");
3438 return error_mark_node;
3439 }
3440
3441 return scope;
3442}
3443
3444/* Parse a class-or-namespace-name.
3445
3446 class-or-namespace-name:
3447 class-name
3448 namespace-name
3449
3450 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3451 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3452 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3453 TYPE_P is TRUE iff the next name should be taken as a class-name,
3454 even the same name is declared to be another entity in the same
3455 scope.
3456
3457 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
eea9800f
MM
3458 specified by the class-or-namespace-name. If neither is found the
3459 ERROR_MARK_NODE is returned. */
a723baf1
MM
3460
3461static tree
3462cp_parser_class_or_namespace_name (cp_parser *parser,
3463 bool typename_keyword_p,
3464 bool template_keyword_p,
3465 bool check_dependency_p,
3466 bool type_p)
3467{
3468 tree saved_scope;
3469 tree saved_qualifying_scope;
3470 tree saved_object_scope;
3471 tree scope;
eea9800f 3472 bool only_class_p;
a723baf1 3473
a723baf1
MM
3474 /* Before we try to parse the class-name, we must save away the
3475 current PARSER->SCOPE since cp_parser_class_name will destroy
3476 it. */
3477 saved_scope = parser->scope;
3478 saved_qualifying_scope = parser->qualifying_scope;
3479 saved_object_scope = parser->object_scope;
eea9800f
MM
3480 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3481 there is no need to look for a namespace-name. */
bbaab916 3482 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
eea9800f
MM
3483 if (!only_class_p)
3484 cp_parser_parse_tentatively (parser);
a723baf1
MM
3485 scope = cp_parser_class_name (parser,
3486 typename_keyword_p,
3487 template_keyword_p,
3488 type_p,
3489 /*check_access_p=*/true,
3490 check_dependency_p,
3491 /*class_head_p=*/false);
3492 /* If that didn't work, try for a namespace-name. */
eea9800f 3493 if (!only_class_p && !cp_parser_parse_definitely (parser))
a723baf1
MM
3494 {
3495 /* Restore the saved scope. */
3496 parser->scope = saved_scope;
3497 parser->qualifying_scope = saved_qualifying_scope;
3498 parser->object_scope = saved_object_scope;
eea9800f
MM
3499 /* If we are not looking at an identifier followed by the scope
3500 resolution operator, then this is not part of a
3501 nested-name-specifier. (Note that this function is only used
3502 to parse the components of a nested-name-specifier.) */
3503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3504 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3505 return error_mark_node;
a723baf1
MM
3506 scope = cp_parser_namespace_name (parser);
3507 }
3508
3509 return scope;
3510}
3511
3512/* Parse a postfix-expression.
3513
3514 postfix-expression:
3515 primary-expression
3516 postfix-expression [ expression ]
3517 postfix-expression ( expression-list [opt] )
3518 simple-type-specifier ( expression-list [opt] )
3519 typename :: [opt] nested-name-specifier identifier
3520 ( expression-list [opt] )
3521 typename :: [opt] nested-name-specifier template [opt] template-id
3522 ( expression-list [opt] )
3523 postfix-expression . template [opt] id-expression
3524 postfix-expression -> template [opt] id-expression
3525 postfix-expression . pseudo-destructor-name
3526 postfix-expression -> pseudo-destructor-name
3527 postfix-expression ++
3528 postfix-expression --
3529 dynamic_cast < type-id > ( expression )
3530 static_cast < type-id > ( expression )
3531 reinterpret_cast < type-id > ( expression )
3532 const_cast < type-id > ( expression )
3533 typeid ( expression )
3534 typeid ( type-id )
3535
3536 GNU Extension:
3537
3538 postfix-expression:
3539 ( type-id ) { initializer-list , [opt] }
3540
3541 This extension is a GNU version of the C99 compound-literal
3542 construct. (The C99 grammar uses `type-name' instead of `type-id',
3543 but they are essentially the same concept.)
3544
3545 If ADDRESS_P is true, the postfix expression is the operand of the
3546 `&' operator.
3547
3548 Returns a representation of the expression. */
3549
3550static tree
3551cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3552{
3553 cp_token *token;
3554 enum rid keyword;
3555 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3556 tree postfix_expression = NULL_TREE;
3557 /* Non-NULL only if the current postfix-expression can be used to
3558 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3559 class used to qualify the member. */
3560 tree qualifying_class = NULL_TREE;
3561 bool done;
3562
3563 /* Peek at the next token. */
3564 token = cp_lexer_peek_token (parser->lexer);
3565 /* Some of the productions are determined by keywords. */
3566 keyword = token->keyword;
3567 switch (keyword)
3568 {
3569 case RID_DYNCAST:
3570 case RID_STATCAST:
3571 case RID_REINTCAST:
3572 case RID_CONSTCAST:
3573 {
3574 tree type;
3575 tree expression;
3576 const char *saved_message;
3577
3578 /* All of these can be handled in the same way from the point
3579 of view of parsing. Begin by consuming the token
3580 identifying the cast. */
3581 cp_lexer_consume_token (parser->lexer);
3582
3583 /* New types cannot be defined in the cast. */
3584 saved_message = parser->type_definition_forbidden_message;
3585 parser->type_definition_forbidden_message
3586 = "types may not be defined in casts";
3587
3588 /* Look for the opening `<'. */
3589 cp_parser_require (parser, CPP_LESS, "`<'");
3590 /* Parse the type to which we are casting. */
3591 type = cp_parser_type_id (parser);
3592 /* Look for the closing `>'. */
3593 cp_parser_require (parser, CPP_GREATER, "`>'");
3594 /* Restore the old message. */
3595 parser->type_definition_forbidden_message = saved_message;
3596
3597 /* And the expression which is being cast. */
3598 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3599 expression = cp_parser_expression (parser);
3600 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3601
14d22dd6
MM
3602 /* Only type conversions to integral or enumeration types
3603 can be used in constant-expressions. */
3604 if (parser->constant_expression_p
3605 && !dependent_type_p (type)
3606 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3607 {
3608 if (!parser->allow_non_constant_expression_p)
3609 return (cp_parser_non_constant_expression
3610 ("a cast to a type other than an integral or "
3611 "enumeration type"));
3612 parser->non_constant_expression_p = true;
3613 }
3614
a723baf1
MM
3615 switch (keyword)
3616 {
3617 case RID_DYNCAST:
3618 postfix_expression
3619 = build_dynamic_cast (type, expression);
3620 break;
3621 case RID_STATCAST:
3622 postfix_expression
3623 = build_static_cast (type, expression);
3624 break;
3625 case RID_REINTCAST:
3626 postfix_expression
3627 = build_reinterpret_cast (type, expression);
3628 break;
3629 case RID_CONSTCAST:
3630 postfix_expression
3631 = build_const_cast (type, expression);
3632 break;
3633 default:
3634 abort ();
3635 }
3636 }
3637 break;
3638
3639 case RID_TYPEID:
3640 {
3641 tree type;
3642 const char *saved_message;
3643
3644 /* Consume the `typeid' token. */
3645 cp_lexer_consume_token (parser->lexer);
3646 /* Look for the `(' token. */
3647 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3648 /* Types cannot be defined in a `typeid' expression. */
3649 saved_message = parser->type_definition_forbidden_message;
3650 parser->type_definition_forbidden_message
3651 = "types may not be defined in a `typeid\' expression";
3652 /* We can't be sure yet whether we're looking at a type-id or an
3653 expression. */
3654 cp_parser_parse_tentatively (parser);
3655 /* Try a type-id first. */
3656 type = cp_parser_type_id (parser);
3657 /* Look for the `)' token. Otherwise, we can't be sure that
3658 we're not looking at an expression: consider `typeid (int
3659 (3))', for example. */
3660 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3661 /* If all went well, simply lookup the type-id. */
3662 if (cp_parser_parse_definitely (parser))
3663 postfix_expression = get_typeid (type);
3664 /* Otherwise, fall back to the expression variant. */
3665 else
3666 {
3667 tree expression;
3668
3669 /* Look for an expression. */
3670 expression = cp_parser_expression (parser);
3671 /* Compute its typeid. */
3672 postfix_expression = build_typeid (expression);
3673 /* Look for the `)' token. */
3674 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3675 }
3676
3677 /* Restore the saved message. */
3678 parser->type_definition_forbidden_message = saved_message;
3679 }
3680 break;
3681
3682 case RID_TYPENAME:
3683 {
3684 bool template_p = false;
3685 tree id;
3686 tree type;
3687
3688 /* Consume the `typename' token. */
3689 cp_lexer_consume_token (parser->lexer);
3690 /* Look for the optional `::' operator. */
3691 cp_parser_global_scope_opt (parser,
3692 /*current_scope_valid_p=*/false);
3693 /* Look for the nested-name-specifier. */
3694 cp_parser_nested_name_specifier (parser,
3695 /*typename_keyword_p=*/true,
3696 /*check_dependency_p=*/true,
3697 /*type_p=*/true);
3698 /* Look for the optional `template' keyword. */
3699 template_p = cp_parser_optional_template_keyword (parser);
3700 /* We don't know whether we're looking at a template-id or an
3701 identifier. */
3702 cp_parser_parse_tentatively (parser);
3703 /* Try a template-id. */
3704 id = cp_parser_template_id (parser, template_p,
3705 /*check_dependency_p=*/true);
3706 /* If that didn't work, try an identifier. */
3707 if (!cp_parser_parse_definitely (parser))
3708 id = cp_parser_identifier (parser);
3709 /* Create a TYPENAME_TYPE to represent the type to which the
3710 functional cast is being performed. */
3711 type = make_typename_type (parser->scope, id,
3712 /*complain=*/1);
3713
3714 postfix_expression = cp_parser_functional_cast (parser, type);
3715 }
3716 break;
3717
3718 default:
3719 {
3720 tree type;
3721
3722 /* If the next thing is a simple-type-specifier, we may be
3723 looking at a functional cast. We could also be looking at
3724 an id-expression. So, we try the functional cast, and if
3725 that doesn't work we fall back to the primary-expression. */
3726 cp_parser_parse_tentatively (parser);
3727 /* Look for the simple-type-specifier. */
3728 type = cp_parser_simple_type_specifier (parser,
3729 CP_PARSER_FLAGS_NONE);
3730 /* Parse the cast itself. */
3731 if (!cp_parser_error_occurred (parser))
3732 postfix_expression
3733 = cp_parser_functional_cast (parser, type);
3734 /* If that worked, we're done. */
3735 if (cp_parser_parse_definitely (parser))
3736 break;
3737
3738 /* If the functional-cast didn't work out, try a
3739 compound-literal. */
14d22dd6
MM
3740 if (cp_parser_allow_gnu_extensions_p (parser)
3741 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a723baf1
MM
3742 {
3743 tree initializer_list = NULL_TREE;
3744
3745 cp_parser_parse_tentatively (parser);
14d22dd6
MM
3746 /* Consume the `('. */
3747 cp_lexer_consume_token (parser->lexer);
3748 /* Parse the type. */
3749 type = cp_parser_type_id (parser);
3750 /* Look for the `)'. */
3751 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3752 /* Look for the `{'. */
3753 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3754 /* If things aren't going well, there's no need to
3755 keep going. */
3756 if (!cp_parser_error_occurred (parser))
a723baf1 3757 {
14d22dd6
MM
3758 /* Parse the initializer-list. */
3759 initializer_list
3760 = cp_parser_initializer_list (parser);
3761 /* Allow a trailing `,'. */
3762 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3763 cp_lexer_consume_token (parser->lexer);
3764 /* Look for the final `}'. */
3765 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
a723baf1
MM
3766 }
3767 /* If that worked, we're definitely looking at a
3768 compound-literal expression. */
3769 if (cp_parser_parse_definitely (parser))
3770 {
3771 /* Warn the user that a compound literal is not
3772 allowed in standard C++. */
3773 if (pedantic)
3774 pedwarn ("ISO C++ forbids compound-literals");
3775 /* Form the representation of the compound-literal. */
3776 postfix_expression
3777 = finish_compound_literal (type, initializer_list);
3778 break;
3779 }
3780 }
3781
3782 /* It must be a primary-expression. */
3783 postfix_expression = cp_parser_primary_expression (parser,
3784 &idk,
3785 &qualifying_class);
3786 }
3787 break;
3788 }
3789
3790 /* Peek at the next token. */
3791 token = cp_lexer_peek_token (parser->lexer);
3792 done = (token->type != CPP_OPEN_SQUARE
3793 && token->type != CPP_OPEN_PAREN
3794 && token->type != CPP_DOT
3795 && token->type != CPP_DEREF
3796 && token->type != CPP_PLUS_PLUS
3797 && token->type != CPP_MINUS_MINUS);
3798
3799 /* If the postfix expression is complete, finish up. */
3800 if (address_p && qualifying_class && done)
3801 {
3802 if (TREE_CODE (postfix_expression) == SCOPE_REF)
3803 postfix_expression = TREE_OPERAND (postfix_expression, 1);
3804 postfix_expression
3805 = build_offset_ref (qualifying_class, postfix_expression);
3806 return postfix_expression;
3807 }
3808
3809 /* Otherwise, if we were avoiding committing until we knew
3810 whether or not we had a pointer-to-member, we now know that
3811 the expression is an ordinary reference to a qualified name. */
089d6ea7 3812 if (qualifying_class)
a723baf1
MM
3813 {
3814 if (TREE_CODE (postfix_expression) == FIELD_DECL)
3815 postfix_expression
3816 = finish_non_static_data_member (postfix_expression,
3817 qualifying_class);
089d6ea7
MM
3818 else if (BASELINK_P (postfix_expression)
3819 && !processing_template_decl)
a723baf1
MM
3820 {
3821 tree fn;
3822 tree fns;
3823
3824 /* See if any of the functions are non-static members. */
3825 fns = BASELINK_FUNCTIONS (postfix_expression);
3826 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
3827 fns = TREE_OPERAND (fns, 0);
3828 for (fn = fns; fn; fn = OVL_NEXT (fn))
3829 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3830 break;
3831 /* If so, the expression may be relative to the current
3832 class. */
3833 if (fn && current_class_type
3834 && DERIVED_FROM_P (qualifying_class, current_class_type))
3835 postfix_expression
3836 = (build_class_member_access_expr
3837 (maybe_dummy_object (qualifying_class, NULL),
3838 postfix_expression,
3839 BASELINK_ACCESS_BINFO (postfix_expression),
3840 /*preserve_reference=*/false));
3841 else if (done)
3842 return build_offset_ref (qualifying_class,
3843 postfix_expression);
3844 }
3845 }
3846
3847 /* Remember that there was a reference to this entity. */
3848 if (DECL_P (postfix_expression))
3849 mark_used (postfix_expression);
3850
3851 /* Keep looping until the postfix-expression is complete. */
3852 while (true)
3853 {
3854 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3855 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3856 {
3857 /* It is not a Koenig lookup function call. */
3858 unqualified_name_lookup_error (postfix_expression);
3859 postfix_expression = error_mark_node;
3860 }
3861
3862 /* Peek at the next token. */
3863 token = cp_lexer_peek_token (parser->lexer);
3864
3865 switch (token->type)
3866 {
3867 case CPP_OPEN_SQUARE:
3868 /* postfix-expression [ expression ] */
3869 {
3870 tree index;
3871
3872 /* Consume the `[' token. */
3873 cp_lexer_consume_token (parser->lexer);
3874 /* Parse the index expression. */
3875 index = cp_parser_expression (parser);
3876 /* Look for the closing `]'. */
3877 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3878
3879 /* Build the ARRAY_REF. */
3880 postfix_expression
3881 = grok_array_decl (postfix_expression, index);
3882 idk = CP_PARSER_ID_KIND_NONE;
3883 }
3884 break;
3885
3886 case CPP_OPEN_PAREN:
3887 /* postfix-expression ( expression-list [opt] ) */
3888 {
3889 tree args;
3890
3891 /* Consume the `(' token. */
3892 cp_lexer_consume_token (parser->lexer);
3893 /* If the next token is not a `)', then there are some
3894 arguments. */
3895 if (cp_lexer_next_token_is_not (parser->lexer,
3896 CPP_CLOSE_PAREN))
3897 args = cp_parser_expression_list (parser);
3898 else
3899 args = NULL_TREE;
3900 /* Look for the closing `)'. */
3901 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14d22dd6
MM
3902 /* Function calls are not permitted in
3903 constant-expressions. */
3904 if (parser->constant_expression_p)
3905 {
3906 if (!parser->allow_non_constant_expression_p)
3907 return cp_parser_non_constant_expression ("a function call");
3908 parser->non_constant_expression_p = true;
3909 }
a723baf1
MM
3910
3911 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
3912 && (is_overloaded_fn (postfix_expression)
3913 || DECL_P (postfix_expression)
3914 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3915 && args)
3916 {
3917 tree arg;
3918 tree identifier = NULL_TREE;
3919 tree functions = NULL_TREE;
3920
3921 /* Find the name of the overloaded function. */
3922 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3923 identifier = postfix_expression;
3924 else if (is_overloaded_fn (postfix_expression))
3925 {
3926 functions = postfix_expression;
3927 identifier = DECL_NAME (get_first_fn (functions));
3928 }
3929 else if (DECL_P (postfix_expression))
3930 {
3931 functions = postfix_expression;
3932 identifier = DECL_NAME (postfix_expression);
3933 }
3934
3935 /* A call to a namespace-scope function using an
3936 unqualified name.
3937
3938 Do Koenig lookup -- unless any of the arguments are
3939 type-dependent. */
3940 for (arg = args; arg; arg = TREE_CHAIN (arg))
1fb3244a 3941 if (type_dependent_expression_p (TREE_VALUE (arg)))
a723baf1
MM
3942 break;
3943 if (!arg)
3944 {
3945 postfix_expression
3946 = lookup_arg_dependent(identifier, functions, args);
3947 if (!postfix_expression)
3948 {
3949 /* The unqualified name could not be resolved. */
3950 unqualified_name_lookup_error (identifier);
3951 postfix_expression = error_mark_node;
3952 }
3953 postfix_expression
3954 = build_call_from_tree (postfix_expression, args,
3955 /*diallow_virtual=*/false);
3956 break;
3957 }
3958 postfix_expression = build_min_nt (LOOKUP_EXPR,
3959 identifier);
3960 }
3961 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
3962 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3963 {
3964 /* The unqualified name could not be resolved. */
3965 unqualified_name_lookup_error (postfix_expression);
3966 postfix_expression = error_mark_node;
3967 break;
3968 }
3969
3970 /* In the body of a template, no further processing is
3971 required. */
3972 if (processing_template_decl)
3973 {
3974 postfix_expression = build_nt (CALL_EXPR,
3975 postfix_expression,
3976 args);
3977 break;
3978 }
3979
3980 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3981 postfix_expression
3982 = (build_new_method_call
3983 (TREE_OPERAND (postfix_expression, 0),
3984 TREE_OPERAND (postfix_expression, 1),
3985 args, NULL_TREE,
3986 (idk == CP_PARSER_ID_KIND_QUALIFIED
3987 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3988 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
3989 postfix_expression = (build_offset_ref_call_from_tree
3990 (postfix_expression, args));
3991 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
2050a1bb
MM
3992 /* A call to a static class member, or a namespace-scope
3993 function. */
3994 postfix_expression
3995 = finish_call_expr (postfix_expression, args,
3996 /*disallow_virtual=*/true);
a723baf1 3997 else
2050a1bb
MM
3998 /* All other function calls. */
3999 postfix_expression
4000 = finish_call_expr (postfix_expression, args,
4001 /*disallow_virtual=*/false);
a723baf1
MM
4002
4003 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4004 idk = CP_PARSER_ID_KIND_NONE;
4005 }
4006 break;
4007
4008 case CPP_DOT:
4009 case CPP_DEREF:
4010 /* postfix-expression . template [opt] id-expression
4011 postfix-expression . pseudo-destructor-name
4012 postfix-expression -> template [opt] id-expression
4013 postfix-expression -> pseudo-destructor-name */
4014 {
4015 tree name;
4016 bool dependent_p;
4017 bool template_p;
4018 tree scope = NULL_TREE;
4019
4020 /* If this is a `->' operator, dereference the pointer. */
4021 if (token->type == CPP_DEREF)
4022 postfix_expression = build_x_arrow (postfix_expression);
4023 /* Check to see whether or not the expression is
4024 type-dependent. */
bbaab916 4025 dependent_p = type_dependent_expression_p (postfix_expression);
a723baf1
MM
4026 /* The identifier following the `->' or `.' is not
4027 qualified. */
4028 parser->scope = NULL_TREE;
4029 parser->qualifying_scope = NULL_TREE;
4030 parser->object_scope = NULL_TREE;
a6bd211d 4031 idk = CP_PARSER_ID_KIND_NONE;
a723baf1
MM
4032 /* Enter the scope corresponding to the type of the object
4033 given by the POSTFIX_EXPRESSION. */
4034 if (!dependent_p
4035 && TREE_TYPE (postfix_expression) != NULL_TREE)
4036 {
4037 scope = TREE_TYPE (postfix_expression);
4038 /* According to the standard, no expression should
4039 ever have reference type. Unfortunately, we do not
4040 currently match the standard in this respect in
4041 that our internal representation of an expression
4042 may have reference type even when the standard says
4043 it does not. Therefore, we have to manually obtain
4044 the underlying type here. */
4045 if (TREE_CODE (scope) == REFERENCE_TYPE)
4046 scope = TREE_TYPE (scope);
4047 /* If the SCOPE is an OFFSET_TYPE, then we grab the
4048 type of the field. We get an OFFSET_TYPE for
4049 something like:
4050
4051 S::T.a ...
4052
4053 Probably, we should not get an OFFSET_TYPE here;
4054 that transformation should be made only if `&S::T'
4055 is written. */
4056 if (TREE_CODE (scope) == OFFSET_TYPE)
4057 scope = TREE_TYPE (scope);
4058 /* The type of the POSTFIX_EXPRESSION must be
4059 complete. */
4060 scope = complete_type_or_else (scope, NULL_TREE);
4061 /* Let the name lookup machinery know that we are
4062 processing a class member access expression. */
4063 parser->context->object_type = scope;
4064 /* If something went wrong, we want to be able to
4065 discern that case, as opposed to the case where
4066 there was no SCOPE due to the type of expression
4067 being dependent. */
4068 if (!scope)
4069 scope = error_mark_node;
4070 }
4071
4072 /* Consume the `.' or `->' operator. */
4073 cp_lexer_consume_token (parser->lexer);
4074 /* If the SCOPE is not a scalar type, we are looking at an
4075 ordinary class member access expression, rather than a
4076 pseudo-destructor-name. */
4077 if (!scope || !SCALAR_TYPE_P (scope))
4078 {
4079 template_p = cp_parser_optional_template_keyword (parser);
4080 /* Parse the id-expression. */
4081 name = cp_parser_id_expression (parser,
4082 template_p,
4083 /*check_dependency_p=*/true,
4084 /*template_p=*/NULL);
4085 /* In general, build a SCOPE_REF if the member name is
4086 qualified. However, if the name was not dependent
4087 and has already been resolved; there is no need to
4088 build the SCOPE_REF. For example;
4089
4090 struct X { void f(); };
4091 template <typename T> void f(T* t) { t->X::f(); }
4092
4093 Even though "t" is dependent, "X::f" is not and has
4094 except that for a BASELINK there is no need to
4095 include scope information. */
a6bd211d
JM
4096
4097 /* But we do need to remember that there was an explicit
4098 scope for virtual function calls. */
4099 if (parser->scope)
4100 idk = CP_PARSER_ID_KIND_QUALIFIED;
4101
a723baf1
MM
4102 if (name != error_mark_node
4103 && !BASELINK_P (name)
4104 && parser->scope)
4105 {
4106 name = build_nt (SCOPE_REF, parser->scope, name);
4107 parser->scope = NULL_TREE;
4108 parser->qualifying_scope = NULL_TREE;
4109 parser->object_scope = NULL_TREE;
4110 }
4111 postfix_expression
4112 = finish_class_member_access_expr (postfix_expression, name);
4113 }
4114 /* Otherwise, try the pseudo-destructor-name production. */
4115 else
4116 {
4117 tree s;
4118 tree type;
4119
4120 /* Parse the pseudo-destructor-name. */
4121 cp_parser_pseudo_destructor_name (parser, &s, &type);
4122 /* Form the call. */
4123 postfix_expression
4124 = finish_pseudo_destructor_expr (postfix_expression,
4125 s, TREE_TYPE (type));
4126 }
4127
4128 /* We no longer need to look up names in the scope of the
4129 object on the left-hand side of the `.' or `->'
4130 operator. */
4131 parser->context->object_type = NULL_TREE;
a723baf1
MM
4132 }
4133 break;
4134
4135 case CPP_PLUS_PLUS:
4136 /* postfix-expression ++ */
4137 /* Consume the `++' token. */
4138 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
4139 /* Increments may not appear in constant-expressions. */
4140 if (parser->constant_expression_p)
4141 {
4142 if (!parser->allow_non_constant_expression_p)
4143 return cp_parser_non_constant_expression ("an increment");
4144 parser->non_constant_expression_p = true;
4145 }
a723baf1
MM
4146 /* Generate a reprsentation for the complete expression. */
4147 postfix_expression
4148 = finish_increment_expr (postfix_expression,
4149 POSTINCREMENT_EXPR);
4150 idk = CP_PARSER_ID_KIND_NONE;
4151 break;
4152
4153 case CPP_MINUS_MINUS:
4154 /* postfix-expression -- */
4155 /* Consume the `--' token. */
4156 cp_lexer_consume_token (parser->lexer);
14d22dd6
MM
4157 /* Decrements may not appear in constant-expressions. */
4158 if (parser->constant_expression_p)
4159 {
4160 if (!parser->allow_non_constant_expression_p)
4161 return cp_parser_non_constant_expression ("a decrement");
4162 parser->non_constant_expression_p = true;
4163 }
a723baf1
MM
4164 /* Generate a reprsentation for the complete expression. */
4165 postfix_expression
4166 = finish_increment_expr (postfix_expression,
4167 POSTDECREMENT_EXPR);
4168 idk = CP_PARSER_ID_KIND_NONE;
4169 break;
4170
4171 default:
4172 return postfix_expression;
4173 }
4174 }
4175
4176 /* We should never get here. */
4177 abort ();
4178 return error_mark_node;
4179}
4180
4181/* Parse an expression-list.
4182
4183 expression-list:
4184 assignment-expression
4185 expression-list, assignment-expression
4186
4187 Returns a TREE_LIST. The TREE_VALUE of each node is a
4188 representation of an assignment-expression. Note that a TREE_LIST
4189 is returned even if there is only a single expression in the list. */
4190
4191static tree
94edc4ab 4192cp_parser_expression_list (cp_parser* parser)
a723baf1
MM
4193{
4194 tree expression_list = NULL_TREE;
4195
4196 /* Consume expressions until there are no more. */
4197 while (true)
4198 {
4199 tree expr;
4200
4201 /* Parse the next assignment-expression. */
4202 expr = cp_parser_assignment_expression (parser);
4203 /* Add it to the list. */
4204 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4205
4206 /* If the next token isn't a `,', then we are done. */
4207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4208 {
4209 /* All uses of expression-list in the grammar are followed
4210 by a `)'. Therefore, if the next token is not a `)' an
4211 error will be issued, unless we are parsing tentatively.
4212 Skip ahead to see if there is another `,' before the `)';
4213 if so, we can go there and recover. */
4214 if (cp_parser_parsing_tentatively (parser)
4215 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4216 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4217 break;
4218 }
4219
4220 /* Otherwise, consume the `,' and keep going. */
4221 cp_lexer_consume_token (parser->lexer);
4222 }
4223
4224 /* We built up the list in reverse order so we must reverse it now. */
4225 return nreverse (expression_list);
4226}
4227
4228/* Parse a pseudo-destructor-name.
4229
4230 pseudo-destructor-name:
4231 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4232 :: [opt] nested-name-specifier template template-id :: ~ type-name
4233 :: [opt] nested-name-specifier [opt] ~ type-name
4234
4235 If either of the first two productions is used, sets *SCOPE to the
4236 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4237 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4238 or ERROR_MARK_NODE if no type-name is present. */
4239
4240static void
94edc4ab
NN
4241cp_parser_pseudo_destructor_name (cp_parser* parser,
4242 tree* scope,
4243 tree* type)
a723baf1
MM
4244{
4245 bool nested_name_specifier_p;
4246
4247 /* Look for the optional `::' operator. */
4248 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4249 /* Look for the optional nested-name-specifier. */
4250 nested_name_specifier_p
4251 = (cp_parser_nested_name_specifier_opt (parser,
4252 /*typename_keyword_p=*/false,
4253 /*check_dependency_p=*/true,
4254 /*type_p=*/false)
4255 != NULL_TREE);
4256 /* Now, if we saw a nested-name-specifier, we might be doing the
4257 second production. */
4258 if (nested_name_specifier_p
4259 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4260 {
4261 /* Consume the `template' keyword. */
4262 cp_lexer_consume_token (parser->lexer);
4263 /* Parse the template-id. */
4264 cp_parser_template_id (parser,
4265 /*template_keyword_p=*/true,
4266 /*check_dependency_p=*/false);
4267 /* Look for the `::' token. */
4268 cp_parser_require (parser, CPP_SCOPE, "`::'");
4269 }
4270 /* If the next token is not a `~', then there might be some
4271 additional qualification. */
4272 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4273 {
4274 /* Look for the type-name. */
4275 *scope = TREE_TYPE (cp_parser_type_name (parser));
4276 /* Look for the `::' token. */
4277 cp_parser_require (parser, CPP_SCOPE, "`::'");
4278 }
4279 else
4280 *scope = NULL_TREE;
4281
4282 /* Look for the `~'. */
4283 cp_parser_require (parser, CPP_COMPL, "`~'");
4284 /* Look for the type-name again. We are not responsible for
4285 checking that it matches the first type-name. */
4286 *type = cp_parser_type_name (parser);
4287}
4288
4289/* Parse a unary-expression.
4290
4291 unary-expression:
4292 postfix-expression
4293 ++ cast-expression
4294 -- cast-expression
4295 unary-operator cast-expression
4296 sizeof unary-expression
4297 sizeof ( type-id )
4298 new-expression
4299 delete-expression
4300
4301 GNU Extensions:
4302
4303 unary-expression:
4304 __extension__ cast-expression
4305 __alignof__ unary-expression
4306 __alignof__ ( type-id )
4307 __real__ cast-expression
4308 __imag__ cast-expression
4309 && identifier
4310
4311 ADDRESS_P is true iff the unary-expression is appearing as the
4312 operand of the `&' operator.
4313
4314 Returns a representation of the expresion. */
4315
4316static tree
4317cp_parser_unary_expression (cp_parser *parser, bool address_p)
4318{
4319 cp_token *token;
4320 enum tree_code unary_operator;
4321
4322 /* Peek at the next token. */
4323 token = cp_lexer_peek_token (parser->lexer);
4324 /* Some keywords give away the kind of expression. */
4325 if (token->type == CPP_KEYWORD)
4326 {
4327 enum rid keyword = token->keyword;
4328
4329 switch (keyword)
4330 {
4331 case RID_ALIGNOF:
4332 {
4333 /* Consume the `alignof' token. */
4334 cp_lexer_consume_token (parser->lexer);
4335 /* Parse the operand. */
4336 return finish_alignof (cp_parser_sizeof_operand
4337 (parser, keyword));
4338 }
4339
4340 case RID_SIZEOF:
4341 {
4342 tree operand;
4343
4344 /* Consume the `sizeof' token. */
4345 cp_lexer_consume_token (parser->lexer);
4346 /* Parse the operand. */
4347 operand = cp_parser_sizeof_operand (parser, keyword);
4348
4349 /* If the type of the operand cannot be determined build a
4350 SIZEOF_EXPR. */
4351 if (TYPE_P (operand)
1fb3244a
MM
4352 ? dependent_type_p (operand)
4353 : type_dependent_expression_p (operand))
a723baf1
MM
4354 return build_min (SIZEOF_EXPR, size_type_node, operand);
4355 /* Otherwise, compute the constant value. */
4356 else
4357 return finish_sizeof (operand);
4358 }
4359
4360 case RID_NEW:
4361 return cp_parser_new_expression (parser);
4362
4363 case RID_DELETE:
4364 return cp_parser_delete_expression (parser);
4365
4366 case RID_EXTENSION:
4367 {
4368 /* The saved value of the PEDANTIC flag. */
4369 int saved_pedantic;
4370 tree expr;
4371
4372 /* Save away the PEDANTIC flag. */
4373 cp_parser_extension_opt (parser, &saved_pedantic);
4374 /* Parse the cast-expression. */
4375 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4376 /* Restore the PEDANTIC flag. */
4377 pedantic = saved_pedantic;
4378
4379 return expr;
4380 }
4381
4382 case RID_REALPART:
4383 case RID_IMAGPART:
4384 {
4385 tree expression;
4386
4387 /* Consume the `__real__' or `__imag__' token. */
4388 cp_lexer_consume_token (parser->lexer);
4389 /* Parse the cast-expression. */
4390 expression = cp_parser_cast_expression (parser,
4391 /*address_p=*/false);
4392 /* Create the complete representation. */
4393 return build_x_unary_op ((keyword == RID_REALPART
4394 ? REALPART_EXPR : IMAGPART_EXPR),
4395 expression);
4396 }
4397 break;
4398
4399 default:
4400 break;
4401 }
4402 }
4403
4404 /* Look for the `:: new' and `:: delete', which also signal the
4405 beginning of a new-expression, or delete-expression,
4406 respectively. If the next token is `::', then it might be one of
4407 these. */
4408 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4409 {
4410 enum rid keyword;
4411
4412 /* See if the token after the `::' is one of the keywords in
4413 which we're interested. */
4414 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4415 /* If it's `new', we have a new-expression. */
4416 if (keyword == RID_NEW)
4417 return cp_parser_new_expression (parser);
4418 /* Similarly, for `delete'. */
4419 else if (keyword == RID_DELETE)
4420 return cp_parser_delete_expression (parser);
4421 }
4422
4423 /* Look for a unary operator. */
4424 unary_operator = cp_parser_unary_operator (token);
4425 /* The `++' and `--' operators can be handled similarly, even though
4426 they are not technically unary-operators in the grammar. */
4427 if (unary_operator == ERROR_MARK)
4428 {
4429 if (token->type == CPP_PLUS_PLUS)
4430 unary_operator = PREINCREMENT_EXPR;
4431 else if (token->type == CPP_MINUS_MINUS)
4432 unary_operator = PREDECREMENT_EXPR;
4433 /* Handle the GNU address-of-label extension. */
4434 else if (cp_parser_allow_gnu_extensions_p (parser)
4435 && token->type == CPP_AND_AND)
4436 {
4437 tree identifier;
4438
4439 /* Consume the '&&' token. */
4440 cp_lexer_consume_token (parser->lexer);
4441 /* Look for the identifier. */
4442 identifier = cp_parser_identifier (parser);
4443 /* Create an expression representing the address. */
4444 return finish_label_address_expr (identifier);
4445 }
4446 }
4447 if (unary_operator != ERROR_MARK)
4448 {
4449 tree cast_expression;
4450
4451 /* Consume the operator token. */
4452 token = cp_lexer_consume_token (parser->lexer);
4453 /* Parse the cast-expression. */
4454 cast_expression
4455 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4456 /* Now, build an appropriate representation. */
4457 switch (unary_operator)
4458 {
4459 case INDIRECT_REF:
4460 return build_x_indirect_ref (cast_expression, "unary *");
4461
4462 case ADDR_EXPR:
4463 return build_x_unary_op (ADDR_EXPR, cast_expression);
4464
14d22dd6
MM
4465 case PREINCREMENT_EXPR:
4466 case PREDECREMENT_EXPR:
4467 if (parser->constant_expression_p)
4468 {
4469 if (!parser->allow_non_constant_expression_p)
4470 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4471 ? "an increment"
4472 : "a decrement");
4473 parser->non_constant_expression_p = true;
4474 }
4475 /* Fall through. */
a723baf1
MM
4476 case CONVERT_EXPR:
4477 case NEGATE_EXPR:
4478 case TRUTH_NOT_EXPR:
a723baf1
MM
4479 return finish_unary_op_expr (unary_operator, cast_expression);
4480
4481 case BIT_NOT_EXPR:
4482 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4483
4484 default:
4485 abort ();
4486 return error_mark_node;
4487 }
4488 }
4489
4490 return cp_parser_postfix_expression (parser, address_p);
4491}
4492
4493/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4494 unary-operator, the corresponding tree code is returned. */
4495
4496static enum tree_code
94edc4ab 4497cp_parser_unary_operator (cp_token* token)
a723baf1
MM
4498{
4499 switch (token->type)
4500 {
4501 case CPP_MULT:
4502 return INDIRECT_REF;
4503
4504 case CPP_AND:
4505 return ADDR_EXPR;
4506
4507 case CPP_PLUS:
4508 return CONVERT_EXPR;
4509
4510 case CPP_MINUS:
4511 return NEGATE_EXPR;
4512
4513 case CPP_NOT:
4514 return TRUTH_NOT_EXPR;
4515
4516 case CPP_COMPL:
4517 return BIT_NOT_EXPR;
4518
4519 default:
4520 return ERROR_MARK;
4521 }
4522}
4523
4524/* Parse a new-expression.
4525
ca099ac8 4526 new-expression:
a723baf1
MM
4527 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4528 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4529
4530 Returns a representation of the expression. */
4531
4532static tree
94edc4ab 4533cp_parser_new_expression (cp_parser* parser)
a723baf1
MM
4534{
4535 bool global_scope_p;
4536 tree placement;
4537 tree type;
4538 tree initializer;
4539
4540 /* Look for the optional `::' operator. */
4541 global_scope_p
4542 = (cp_parser_global_scope_opt (parser,
4543 /*current_scope_valid_p=*/false)
4544 != NULL_TREE);
4545 /* Look for the `new' operator. */
4546 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4547 /* There's no easy way to tell a new-placement from the
4548 `( type-id )' construct. */
4549 cp_parser_parse_tentatively (parser);
4550 /* Look for a new-placement. */
4551 placement = cp_parser_new_placement (parser);
4552 /* If that didn't work out, there's no new-placement. */
4553 if (!cp_parser_parse_definitely (parser))
4554 placement = NULL_TREE;
4555
4556 /* If the next token is a `(', then we have a parenthesized
4557 type-id. */
4558 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4559 {
4560 /* Consume the `('. */
4561 cp_lexer_consume_token (parser->lexer);
4562 /* Parse the type-id. */
4563 type = cp_parser_type_id (parser);
4564 /* Look for the closing `)'. */
4565 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4566 }
4567 /* Otherwise, there must be a new-type-id. */
4568 else
4569 type = cp_parser_new_type_id (parser);
4570
4571 /* If the next token is a `(', then we have a new-initializer. */
4572 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4573 initializer = cp_parser_new_initializer (parser);
4574 else
4575 initializer = NULL_TREE;
4576
4577 /* Create a representation of the new-expression. */
4578 return build_new (placement, type, initializer, global_scope_p);
4579}
4580
4581/* Parse a new-placement.
4582
4583 new-placement:
4584 ( expression-list )
4585
4586 Returns the same representation as for an expression-list. */
4587
4588static tree
94edc4ab 4589cp_parser_new_placement (cp_parser* parser)
a723baf1
MM
4590{
4591 tree expression_list;
4592
4593 /* Look for the opening `('. */
4594 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4595 return error_mark_node;
4596 /* Parse the expression-list. */
4597 expression_list = cp_parser_expression_list (parser);
4598 /* Look for the closing `)'. */
4599 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4600
4601 return expression_list;
4602}
4603
4604/* Parse a new-type-id.
4605
4606 new-type-id:
4607 type-specifier-seq new-declarator [opt]
4608
4609 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4610 and whose TREE_VALUE is the new-declarator. */
4611
4612static tree
94edc4ab 4613cp_parser_new_type_id (cp_parser* parser)
a723baf1
MM
4614{
4615 tree type_specifier_seq;
4616 tree declarator;
4617 const char *saved_message;
4618
4619 /* The type-specifier sequence must not contain type definitions.
4620 (It cannot contain declarations of new types either, but if they
4621 are not definitions we will catch that because they are not
4622 complete.) */
4623 saved_message = parser->type_definition_forbidden_message;
4624 parser->type_definition_forbidden_message
4625 = "types may not be defined in a new-type-id";
4626 /* Parse the type-specifier-seq. */
4627 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4628 /* Restore the old message. */
4629 parser->type_definition_forbidden_message = saved_message;
4630 /* Parse the new-declarator. */
4631 declarator = cp_parser_new_declarator_opt (parser);
4632
4633 return build_tree_list (type_specifier_seq, declarator);
4634}
4635
4636/* Parse an (optional) new-declarator.
4637
4638 new-declarator:
4639 ptr-operator new-declarator [opt]
4640 direct-new-declarator
4641
4642 Returns a representation of the declarator. See
4643 cp_parser_declarator for the representations used. */
4644
4645static tree
94edc4ab 4646cp_parser_new_declarator_opt (cp_parser* parser)
a723baf1
MM
4647{
4648 enum tree_code code;
4649 tree type;
4650 tree cv_qualifier_seq;
4651
4652 /* We don't know if there's a ptr-operator next, or not. */
4653 cp_parser_parse_tentatively (parser);
4654 /* Look for a ptr-operator. */
4655 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4656 /* If that worked, look for more new-declarators. */
4657 if (cp_parser_parse_definitely (parser))
4658 {
4659 tree declarator;
4660
4661 /* Parse another optional declarator. */
4662 declarator = cp_parser_new_declarator_opt (parser);
4663
4664 /* Create the representation of the declarator. */
4665 if (code == INDIRECT_REF)
4666 declarator = make_pointer_declarator (cv_qualifier_seq,
4667 declarator);
4668 else
4669 declarator = make_reference_declarator (cv_qualifier_seq,
4670 declarator);
4671
4672 /* Handle the pointer-to-member case. */
4673 if (type)
4674 declarator = build_nt (SCOPE_REF, type, declarator);
4675
4676 return declarator;
4677 }
4678
4679 /* If the next token is a `[', there is a direct-new-declarator. */
4680 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4681 return cp_parser_direct_new_declarator (parser);
4682
4683 return NULL_TREE;
4684}
4685
4686/* Parse a direct-new-declarator.
4687
4688 direct-new-declarator:
4689 [ expression ]
4690 direct-new-declarator [constant-expression]
4691
4692 Returns an ARRAY_REF, following the same conventions as are
4693 documented for cp_parser_direct_declarator. */
4694
4695static tree
94edc4ab 4696cp_parser_direct_new_declarator (cp_parser* parser)
a723baf1
MM
4697{
4698 tree declarator = NULL_TREE;
4699
4700 while (true)
4701 {
4702 tree expression;
4703
4704 /* Look for the opening `['. */
4705 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4706 /* The first expression is not required to be constant. */
4707 if (!declarator)
4708 {
4709 expression = cp_parser_expression (parser);
4710 /* The standard requires that the expression have integral
4711 type. DR 74 adds enumeration types. We believe that the
4712 real intent is that these expressions be handled like the
4713 expression in a `switch' condition, which also allows
4714 classes with a single conversion to integral or
4715 enumeration type. */
4716 if (!processing_template_decl)
4717 {
4718 expression
4719 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4720 expression,
b746c5dc 4721 /*complain=*/true);
a723baf1
MM
4722 if (!expression)
4723 {
4724 error ("expression in new-declarator must have integral or enumeration type");
4725 expression = error_mark_node;
4726 }
4727 }
4728 }
4729 /* But all the other expressions must be. */
4730 else
14d22dd6
MM
4731 expression
4732 = cp_parser_constant_expression (parser,
4733 /*allow_non_constant=*/false,
4734 NULL);
a723baf1
MM
4735 /* Look for the closing `]'. */
4736 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4737
4738 /* Add this bound to the declarator. */
4739 declarator = build_nt (ARRAY_REF, declarator, expression);
4740
4741 /* If the next token is not a `[', then there are no more
4742 bounds. */
4743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4744 break;
4745 }
4746
4747 return declarator;
4748}
4749
4750/* Parse a new-initializer.
4751
4752 new-initializer:
4753 ( expression-list [opt] )
4754
4755 Returns a reprsentation of the expression-list. If there is no
4756 expression-list, VOID_ZERO_NODE is returned. */
4757
4758static tree
94edc4ab 4759cp_parser_new_initializer (cp_parser* parser)
a723baf1
MM
4760{
4761 tree expression_list;
4762
4763 /* Look for the opening parenthesis. */
4764 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4765 /* If the next token is not a `)', then there is an
4766 expression-list. */
4767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4768 expression_list = cp_parser_expression_list (parser);
4769 else
4770 expression_list = void_zero_node;
4771 /* Look for the closing parenthesis. */
4772 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4773
4774 return expression_list;
4775}
4776
4777/* Parse a delete-expression.
4778
4779 delete-expression:
4780 :: [opt] delete cast-expression
4781 :: [opt] delete [ ] cast-expression
4782
4783 Returns a representation of the expression. */
4784
4785static tree
94edc4ab 4786cp_parser_delete_expression (cp_parser* parser)
a723baf1
MM
4787{
4788 bool global_scope_p;
4789 bool array_p;
4790 tree expression;
4791
4792 /* Look for the optional `::' operator. */
4793 global_scope_p
4794 = (cp_parser_global_scope_opt (parser,
4795 /*current_scope_valid_p=*/false)
4796 != NULL_TREE);
4797 /* Look for the `delete' keyword. */
4798 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4799 /* See if the array syntax is in use. */
4800 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4801 {
4802 /* Consume the `[' token. */
4803 cp_lexer_consume_token (parser->lexer);
4804 /* Look for the `]' token. */
4805 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4806 /* Remember that this is the `[]' construct. */
4807 array_p = true;
4808 }
4809 else
4810 array_p = false;
4811
4812 /* Parse the cast-expression. */
4813 expression = cp_parser_cast_expression (parser, /*address_p=*/false);
4814
4815 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4816}
4817
4818/* Parse a cast-expression.
4819
4820 cast-expression:
4821 unary-expression
4822 ( type-id ) cast-expression
4823
4824 Returns a representation of the expression. */
4825
4826static tree
4827cp_parser_cast_expression (cp_parser *parser, bool address_p)
4828{
4829 /* If it's a `(', then we might be looking at a cast. */
4830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4831 {
4832 tree type = NULL_TREE;
4833 tree expr = NULL_TREE;
4834 bool compound_literal_p;
4835 const char *saved_message;
4836
4837 /* There's no way to know yet whether or not this is a cast.
4838 For example, `(int (3))' is a unary-expression, while `(int)
4839 3' is a cast. So, we resort to parsing tentatively. */
4840 cp_parser_parse_tentatively (parser);
4841 /* Types may not be defined in a cast. */
4842 saved_message = parser->type_definition_forbidden_message;
4843 parser->type_definition_forbidden_message
4844 = "types may not be defined in casts";
4845 /* Consume the `('. */
4846 cp_lexer_consume_token (parser->lexer);
4847 /* A very tricky bit is that `(struct S) { 3 }' is a
4848 compound-literal (which we permit in C++ as an extension).
4849 But, that construct is not a cast-expression -- it is a
4850 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4851 is legal; if the compound-literal were a cast-expression,
4852 you'd need an extra set of parentheses.) But, if we parse
4853 the type-id, and it happens to be a class-specifier, then we
4854 will commit to the parse at that point, because we cannot
4855 undo the action that is done when creating a new class. So,
4856 then we cannot back up and do a postfix-expression.
4857
4858 Therefore, we scan ahead to the closing `)', and check to see
4859 if the token after the `)' is a `{'. If so, we are not
4860 looking at a cast-expression.
4861
4862 Save tokens so that we can put them back. */
4863 cp_lexer_save_tokens (parser->lexer);
4864 /* Skip tokens until the next token is a closing parenthesis.
4865 If we find the closing `)', and the next token is a `{', then
4866 we are looking at a compound-literal. */
4867 compound_literal_p
4868 = (cp_parser_skip_to_closing_parenthesis (parser)
4869 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4870 /* Roll back the tokens we skipped. */
4871 cp_lexer_rollback_tokens (parser->lexer);
4872 /* If we were looking at a compound-literal, simulate an error
4873 so that the call to cp_parser_parse_definitely below will
4874 fail. */
4875 if (compound_literal_p)
4876 cp_parser_simulate_error (parser);
4877 else
4878 {
4879 /* Look for the type-id. */
4880 type = cp_parser_type_id (parser);
4881 /* Look for the closing `)'. */
4882 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4883 }
4884
4885 /* Restore the saved message. */
4886 parser->type_definition_forbidden_message = saved_message;
4887
bbaab916
NS
4888 /* If ok so far, parse the dependent expression. We cannot be
4889 sure it is a cast. Consider `(T ())'. It is a parenthesized
4890 ctor of T, but looks like a cast to function returning T
4891 without a dependent expression. */
4892 if (!cp_parser_error_occurred (parser))
4893 expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4894
a723baf1
MM
4895 if (cp_parser_parse_definitely (parser))
4896 {
a723baf1
MM
4897 /* Warn about old-style casts, if so requested. */
4898 if (warn_old_style_cast
4899 && !in_system_header
4900 && !VOID_TYPE_P (type)
4901 && current_lang_name != lang_name_c)
4902 warning ("use of old-style cast");
14d22dd6
MM
4903
4904 /* Only type conversions to integral or enumeration types
4905 can be used in constant-expressions. */
4906 if (parser->constant_expression_p
4907 && !dependent_type_p (type)
4908 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4909 {
4910 if (!parser->allow_non_constant_expression_p)
4911 return (cp_parser_non_constant_expression
4912 ("a casts to a type other than an integral or "
4913 "enumeration type"));
4914 parser->non_constant_expression_p = true;
4915 }
a723baf1
MM
4916 /* Perform the cast. */
4917 expr = build_c_cast (type, expr);
bbaab916 4918 return expr;
a723baf1 4919 }
a723baf1
MM
4920 }
4921
4922 /* If we get here, then it's not a cast, so it must be a
4923 unary-expression. */
4924 return cp_parser_unary_expression (parser, address_p);
4925}
4926
4927/* Parse a pm-expression.
4928
4929 pm-expression:
4930 cast-expression
4931 pm-expression .* cast-expression
4932 pm-expression ->* cast-expression
4933
4934 Returns a representation of the expression. */
4935
4936static tree
94edc4ab 4937cp_parser_pm_expression (cp_parser* parser)
a723baf1
MM
4938{
4939 tree cast_expr;
4940 tree pm_expr;
4941
4942 /* Parse the cast-expresion. */
4943 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4944 pm_expr = cast_expr;
4945 /* Now look for pointer-to-member operators. */
4946 while (true)
4947 {
4948 cp_token *token;
4949 enum cpp_ttype token_type;
4950
4951 /* Peek at the next token. */
4952 token = cp_lexer_peek_token (parser->lexer);
4953 token_type = token->type;
4954 /* If it's not `.*' or `->*' there's no pointer-to-member
4955 operation. */
4956 if (token_type != CPP_DOT_STAR
4957 && token_type != CPP_DEREF_STAR)
4958 break;
4959
4960 /* Consume the token. */
4961 cp_lexer_consume_token (parser->lexer);
4962
4963 /* Parse another cast-expression. */
4964 cast_expr = cp_parser_cast_expression (parser, /*address_p=*/false);
4965
4966 /* Build the representation of the pointer-to-member
4967 operation. */
4968 if (token_type == CPP_DEREF_STAR)
4969 pm_expr = build_x_binary_op (MEMBER_REF, pm_expr, cast_expr);
4970 else
4971 pm_expr = build_m_component_ref (pm_expr, cast_expr);
4972 }
4973
4974 return pm_expr;
4975}
4976
4977/* Parse a multiplicative-expression.
4978
4979 mulitplicative-expression:
4980 pm-expression
4981 multiplicative-expression * pm-expression
4982 multiplicative-expression / pm-expression
4983 multiplicative-expression % pm-expression
4984
4985 Returns a representation of the expression. */
4986
4987static tree
94edc4ab 4988cp_parser_multiplicative_expression (cp_parser* parser)
a723baf1 4989{
39b1af70 4990 static const cp_parser_token_tree_map map = {
a723baf1
MM
4991 { CPP_MULT, MULT_EXPR },
4992 { CPP_DIV, TRUNC_DIV_EXPR },
4993 { CPP_MOD, TRUNC_MOD_EXPR },
4994 { CPP_EOF, ERROR_MARK }
4995 };
4996
4997 return cp_parser_binary_expression (parser,
4998 map,
4999 cp_parser_pm_expression);
5000}
5001
5002/* Parse an additive-expression.
5003
5004 additive-expression:
5005 multiplicative-expression
5006 additive-expression + multiplicative-expression
5007 additive-expression - multiplicative-expression
5008
5009 Returns a representation of the expression. */
5010
5011static tree
94edc4ab 5012cp_parser_additive_expression (cp_parser* parser)
a723baf1 5013{
39b1af70 5014 static const cp_parser_token_tree_map map = {
a723baf1
MM
5015 { CPP_PLUS, PLUS_EXPR },
5016 { CPP_MINUS, MINUS_EXPR },
5017 { CPP_EOF, ERROR_MARK }
5018 };
5019
5020 return cp_parser_binary_expression (parser,
5021 map,
5022 cp_parser_multiplicative_expression);
5023}
5024
5025/* Parse a shift-expression.
5026
5027 shift-expression:
5028 additive-expression
5029 shift-expression << additive-expression
5030 shift-expression >> additive-expression
5031
5032 Returns a representation of the expression. */
5033
5034static tree
94edc4ab 5035cp_parser_shift_expression (cp_parser* parser)
a723baf1 5036{
39b1af70 5037 static const cp_parser_token_tree_map map = {
a723baf1
MM
5038 { CPP_LSHIFT, LSHIFT_EXPR },
5039 { CPP_RSHIFT, RSHIFT_EXPR },
5040 { CPP_EOF, ERROR_MARK }
5041 };
5042
5043 return cp_parser_binary_expression (parser,
5044 map,
5045 cp_parser_additive_expression);
5046}
5047
5048/* Parse a relational-expression.
5049
5050 relational-expression:
5051 shift-expression
5052 relational-expression < shift-expression
5053 relational-expression > shift-expression
5054 relational-expression <= shift-expression
5055 relational-expression >= shift-expression
5056
5057 GNU Extension:
5058
5059 relational-expression:
5060 relational-expression <? shift-expression
5061 relational-expression >? shift-expression
5062
5063 Returns a representation of the expression. */
5064
5065static tree
94edc4ab 5066cp_parser_relational_expression (cp_parser* parser)
a723baf1 5067{
39b1af70 5068 static const cp_parser_token_tree_map map = {
a723baf1
MM
5069 { CPP_LESS, LT_EXPR },
5070 { CPP_GREATER, GT_EXPR },
5071 { CPP_LESS_EQ, LE_EXPR },
5072 { CPP_GREATER_EQ, GE_EXPR },
5073 { CPP_MIN, MIN_EXPR },
5074 { CPP_MAX, MAX_EXPR },
5075 { CPP_EOF, ERROR_MARK }
5076 };
5077
5078 return cp_parser_binary_expression (parser,
5079 map,
5080 cp_parser_shift_expression);
5081}
5082
5083/* Parse an equality-expression.
5084
5085 equality-expression:
5086 relational-expression
5087 equality-expression == relational-expression
5088 equality-expression != relational-expression
5089
5090 Returns a representation of the expression. */
5091
5092static tree
94edc4ab 5093cp_parser_equality_expression (cp_parser* parser)
a723baf1 5094{
39b1af70 5095 static const cp_parser_token_tree_map map = {
a723baf1
MM
5096 { CPP_EQ_EQ, EQ_EXPR },
5097 { CPP_NOT_EQ, NE_EXPR },
5098 { CPP_EOF, ERROR_MARK }
5099 };
5100
5101 return cp_parser_binary_expression (parser,
5102 map,
5103 cp_parser_relational_expression);
5104}
5105
5106/* Parse an and-expression.
5107
5108 and-expression:
5109 equality-expression
5110 and-expression & equality-expression
5111
5112 Returns a representation of the expression. */
5113
5114static tree
94edc4ab 5115cp_parser_and_expression (cp_parser* parser)
a723baf1 5116{
39b1af70 5117 static const cp_parser_token_tree_map map = {
a723baf1
MM
5118 { CPP_AND, BIT_AND_EXPR },
5119 { CPP_EOF, ERROR_MARK }
5120 };
5121
5122 return cp_parser_binary_expression (parser,
5123 map,
5124 cp_parser_equality_expression);
5125}
5126
5127/* Parse an exclusive-or-expression.
5128
5129 exclusive-or-expression:
5130 and-expression
5131 exclusive-or-expression ^ and-expression
5132
5133 Returns a representation of the expression. */
5134
5135static tree
94edc4ab 5136cp_parser_exclusive_or_expression (cp_parser* parser)
a723baf1 5137{
39b1af70 5138 static const cp_parser_token_tree_map map = {
a723baf1
MM
5139 { CPP_XOR, BIT_XOR_EXPR },
5140 { CPP_EOF, ERROR_MARK }
5141 };
5142
5143 return cp_parser_binary_expression (parser,
5144 map,
5145 cp_parser_and_expression);
5146}
5147
5148
5149/* Parse an inclusive-or-expression.
5150
5151 inclusive-or-expression:
5152 exclusive-or-expression
5153 inclusive-or-expression | exclusive-or-expression
5154
5155 Returns a representation of the expression. */
5156
5157static tree
94edc4ab 5158cp_parser_inclusive_or_expression (cp_parser* parser)
a723baf1 5159{
39b1af70 5160 static const cp_parser_token_tree_map map = {
a723baf1
MM
5161 { CPP_OR, BIT_IOR_EXPR },
5162 { CPP_EOF, ERROR_MARK }
5163 };
5164
5165 return cp_parser_binary_expression (parser,
5166 map,
5167 cp_parser_exclusive_or_expression);
5168}
5169
5170/* Parse a logical-and-expression.
5171
5172 logical-and-expression:
5173 inclusive-or-expression
5174 logical-and-expression && inclusive-or-expression
5175
5176 Returns a representation of the expression. */
5177
5178static tree
94edc4ab 5179cp_parser_logical_and_expression (cp_parser* parser)
a723baf1 5180{
39b1af70 5181 static const cp_parser_token_tree_map map = {
a723baf1
MM
5182 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5183 { CPP_EOF, ERROR_MARK }
5184 };
5185
5186 return cp_parser_binary_expression (parser,
5187 map,
5188 cp_parser_inclusive_or_expression);
5189}
5190
5191/* Parse a logical-or-expression.
5192
5193 logical-or-expression:
5194 logical-and-expresion
5195 logical-or-expression || logical-and-expression
5196
5197 Returns a representation of the expression. */
5198
5199static tree
94edc4ab 5200cp_parser_logical_or_expression (cp_parser* parser)
a723baf1 5201{
39b1af70 5202 static const cp_parser_token_tree_map map = {
a723baf1
MM
5203 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5204 { CPP_EOF, ERROR_MARK }
5205 };
5206
5207 return cp_parser_binary_expression (parser,
5208 map,
5209 cp_parser_logical_and_expression);
5210}
5211
5212/* Parse a conditional-expression.
5213
5214 conditional-expression:
5215 logical-or-expression
5216 logical-or-expression ? expression : assignment-expression
5217
5218 GNU Extensions:
5219
5220 conditional-expression:
5221 logical-or-expression ? : assignment-expression
5222
5223 Returns a representation of the expression. */
5224
5225static tree
94edc4ab 5226cp_parser_conditional_expression (cp_parser* parser)
a723baf1
MM
5227{
5228 tree logical_or_expr;
5229
5230 /* Parse the logical-or-expression. */
5231 logical_or_expr = cp_parser_logical_or_expression (parser);
5232 /* If the next token is a `?', then we have a real conditional
5233 expression. */
5234 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5235 return cp_parser_question_colon_clause (parser, logical_or_expr);
5236 /* Otherwise, the value is simply the logical-or-expression. */
5237 else
5238 return logical_or_expr;
5239}
5240
5241/* Parse the `? expression : assignment-expression' part of a
5242 conditional-expression. The LOGICAL_OR_EXPR is the
5243 logical-or-expression that started the conditional-expression.
5244 Returns a representation of the entire conditional-expression.
5245
5246 This routine exists only so that it can be shared between
5247 cp_parser_conditional_expression and
5248 cp_parser_assignment_expression.
5249
5250 ? expression : assignment-expression
5251
5252 GNU Extensions:
5253
5254 ? : assignment-expression */
5255
5256static tree
94edc4ab 5257cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
a723baf1
MM
5258{
5259 tree expr;
5260 tree assignment_expr;
5261
5262 /* Consume the `?' token. */
5263 cp_lexer_consume_token (parser->lexer);
5264 if (cp_parser_allow_gnu_extensions_p (parser)
5265 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5266 /* Implicit true clause. */
5267 expr = NULL_TREE;
5268 else
5269 /* Parse the expression. */
5270 expr = cp_parser_expression (parser);
5271
5272 /* The next token should be a `:'. */
5273 cp_parser_require (parser, CPP_COLON, "`:'");
5274 /* Parse the assignment-expression. */
5275 assignment_expr = cp_parser_assignment_expression (parser);
5276
5277 /* Build the conditional-expression. */
5278 return build_x_conditional_expr (logical_or_expr,
5279 expr,
5280 assignment_expr);
5281}
5282
5283/* Parse an assignment-expression.
5284
5285 assignment-expression:
5286 conditional-expression
5287 logical-or-expression assignment-operator assignment_expression
5288 throw-expression
5289
5290 Returns a representation for the expression. */
5291
5292static tree
94edc4ab 5293cp_parser_assignment_expression (cp_parser* parser)
a723baf1
MM
5294{
5295 tree expr;
5296
5297 /* If the next token is the `throw' keyword, then we're looking at
5298 a throw-expression. */
5299 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5300 expr = cp_parser_throw_expression (parser);
5301 /* Otherwise, it must be that we are looking at a
5302 logical-or-expression. */
5303 else
5304 {
5305 /* Parse the logical-or-expression. */
5306 expr = cp_parser_logical_or_expression (parser);
5307 /* If the next token is a `?' then we're actually looking at a
5308 conditional-expression. */
5309 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5310 return cp_parser_question_colon_clause (parser, expr);
5311 else
5312 {
5313 enum tree_code assignment_operator;
5314
5315 /* If it's an assignment-operator, we're using the second
5316 production. */
5317 assignment_operator
5318 = cp_parser_assignment_operator_opt (parser);
5319 if (assignment_operator != ERROR_MARK)
5320 {
5321 tree rhs;
5322
5323 /* Parse the right-hand side of the assignment. */
5324 rhs = cp_parser_assignment_expression (parser);
14d22dd6
MM
5325 /* An assignment may not appear in a
5326 constant-expression. */
5327 if (parser->constant_expression_p)
5328 {
5329 if (!parser->allow_non_constant_expression_p)
5330 return cp_parser_non_constant_expression ("an assignment");
5331 parser->non_constant_expression_p = true;
5332 }
a723baf1
MM
5333 /* Build the asignment expression. */
5334 expr = build_x_modify_expr (expr,
5335 assignment_operator,
5336 rhs);
5337 }
5338 }
5339 }
5340
5341 return expr;
5342}
5343
5344/* Parse an (optional) assignment-operator.
5345
5346 assignment-operator: one of
5347 = *= /= %= += -= >>= <<= &= ^= |=
5348
5349 GNU Extension:
5350
5351 assignment-operator: one of
5352 <?= >?=
5353
5354 If the next token is an assignment operator, the corresponding tree
5355 code is returned, and the token is consumed. For example, for
5356 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5357 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5358 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5359 operator, ERROR_MARK is returned. */
5360
5361static enum tree_code
94edc4ab 5362cp_parser_assignment_operator_opt (cp_parser* parser)
a723baf1
MM
5363{
5364 enum tree_code op;
5365 cp_token *token;
5366
5367 /* Peek at the next toen. */
5368 token = cp_lexer_peek_token (parser->lexer);
5369
5370 switch (token->type)
5371 {
5372 case CPP_EQ:
5373 op = NOP_EXPR;
5374 break;
5375
5376 case CPP_MULT_EQ:
5377 op = MULT_EXPR;
5378 break;
5379
5380 case CPP_DIV_EQ:
5381 op = TRUNC_DIV_EXPR;
5382 break;
5383
5384 case CPP_MOD_EQ:
5385 op = TRUNC_MOD_EXPR;
5386 break;
5387
5388 case CPP_PLUS_EQ:
5389 op = PLUS_EXPR;
5390 break;
5391
5392 case CPP_MINUS_EQ:
5393 op = MINUS_EXPR;
5394 break;
5395
5396 case CPP_RSHIFT_EQ:
5397 op = RSHIFT_EXPR;
5398 break;
5399
5400 case CPP_LSHIFT_EQ:
5401 op = LSHIFT_EXPR;
5402 break;
5403
5404 case CPP_AND_EQ:
5405 op = BIT_AND_EXPR;
5406 break;
5407
5408 case CPP_XOR_EQ:
5409 op = BIT_XOR_EXPR;
5410 break;
5411
5412 case CPP_OR_EQ:
5413 op = BIT_IOR_EXPR;
5414 break;
5415
5416 case CPP_MIN_EQ:
5417 op = MIN_EXPR;
5418 break;
5419
5420 case CPP_MAX_EQ:
5421 op = MAX_EXPR;
5422 break;
5423
5424 default:
5425 /* Nothing else is an assignment operator. */
5426 op = ERROR_MARK;
5427 }
5428
5429 /* If it was an assignment operator, consume it. */
5430 if (op != ERROR_MARK)
5431 cp_lexer_consume_token (parser->lexer);
5432
5433 return op;
5434}
5435
5436/* Parse an expression.
5437
5438 expression:
5439 assignment-expression
5440 expression , assignment-expression
5441
5442 Returns a representation of the expression. */
5443
5444static tree
94edc4ab 5445cp_parser_expression (cp_parser* parser)
a723baf1
MM
5446{
5447 tree expression = NULL_TREE;
5448 bool saw_comma_p = false;
5449
5450 while (true)
5451 {
5452 tree assignment_expression;
5453
5454 /* Parse the next assignment-expression. */
5455 assignment_expression
5456 = cp_parser_assignment_expression (parser);
5457 /* If this is the first assignment-expression, we can just
5458 save it away. */
5459 if (!expression)
5460 expression = assignment_expression;
5461 /* Otherwise, chain the expressions together. It is unclear why
5462 we do not simply build COMPOUND_EXPRs as we go. */
5463 else
5464 expression = tree_cons (NULL_TREE,
5465 assignment_expression,
5466 expression);
5467 /* If the next token is not a comma, then we are done with the
5468 expression. */
5469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5470 break;
5471 /* Consume the `,'. */
5472 cp_lexer_consume_token (parser->lexer);
5473 /* The first time we see a `,', we must take special action
5474 because the representation used for a single expression is
5475 different from that used for a list containing the single
5476 expression. */
5477 if (!saw_comma_p)
5478 {
5479 /* Remember that this expression has a `,' in it. */
5480 saw_comma_p = true;
5481 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5482 additional expressions to it. */
5483 expression = build_tree_list (NULL_TREE, expression);
5484 }
5485 }
5486
5487 /* Build a COMPOUND_EXPR to represent the entire expression, if
5488 necessary. We built up the list in reverse order, so we must
5489 straighten it out here. */
5490 if (saw_comma_p)
14d22dd6
MM
5491 {
5492 /* A comma operator cannot appear in a constant-expression. */
5493 if (parser->constant_expression_p)
5494 {
5495 if (!parser->allow_non_constant_expression_p)
5496 return cp_parser_non_constant_expression ("a comma operator");
5497 parser->non_constant_expression_p = true;
5498 }
5499 expression = build_x_compound_expr (nreverse (expression));
5500 }
a723baf1
MM
5501
5502 return expression;
5503}
5504
5505/* Parse a constant-expression.
5506
5507 constant-expression:
14d22dd6
MM
5508 conditional-expression
5509
5510 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5511 accepted. In that case *NON_CONSTANT_P is set to TRUE. If
5512 ALLOW_NON_CONSTANT_P is false, NON_CONSTANT_P should be NULL. */
a723baf1
MM
5513
5514static tree
14d22dd6
MM
5515cp_parser_constant_expression (cp_parser* parser,
5516 bool allow_non_constant_p,
5517 bool *non_constant_p)
a723baf1
MM
5518{
5519 bool saved_constant_expression_p;
14d22dd6
MM
5520 bool saved_allow_non_constant_expression_p;
5521 bool saved_non_constant_expression_p;
a723baf1
MM
5522 tree expression;
5523
5524 /* It might seem that we could simply parse the
5525 conditional-expression, and then check to see if it were
5526 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5527 one that the compiler can figure out is constant, possibly after
5528 doing some simplifications or optimizations. The standard has a
5529 precise definition of constant-expression, and we must honor
5530 that, even though it is somewhat more restrictive.
5531
5532 For example:
5533
5534 int i[(2, 3)];
5535
5536 is not a legal declaration, because `(2, 3)' is not a
5537 constant-expression. The `,' operator is forbidden in a
5538 constant-expression. However, GCC's constant-folding machinery
5539 will fold this operation to an INTEGER_CST for `3'. */
5540
14d22dd6 5541 /* Save the old settings. */
a723baf1 5542 saved_constant_expression_p = parser->constant_expression_p;
14d22dd6
MM
5543 saved_allow_non_constant_expression_p
5544 = parser->allow_non_constant_expression_p;
5545 saved_non_constant_expression_p = parser->non_constant_expression_p;
a723baf1
MM
5546 /* We are now parsing a constant-expression. */
5547 parser->constant_expression_p = true;
14d22dd6
MM
5548 parser->allow_non_constant_expression_p = allow_non_constant_p;
5549 parser->non_constant_expression_p = false;
a723baf1
MM
5550 /* Parse the conditional-expression. */
5551 expression = cp_parser_conditional_expression (parser);
14d22dd6 5552 /* Restore the old settings. */
a723baf1 5553 parser->constant_expression_p = saved_constant_expression_p;
14d22dd6
MM
5554 parser->allow_non_constant_expression_p
5555 = saved_allow_non_constant_expression_p;
5556 if (allow_non_constant_p)
5557 *non_constant_p = parser->non_constant_expression_p;
5558 parser->non_constant_expression_p = saved_non_constant_expression_p;
a723baf1
MM
5559
5560 return expression;
5561}
5562
5563/* Statements [gram.stmt.stmt] */
5564
5565/* Parse a statement.
5566
5567 statement:
5568 labeled-statement
5569 expression-statement
5570 compound-statement
5571 selection-statement
5572 iteration-statement
5573 jump-statement
5574 declaration-statement
5575 try-block */
5576
5577static void
94edc4ab 5578cp_parser_statement (cp_parser* parser)
a723baf1
MM
5579{
5580 tree statement;
5581 cp_token *token;
5582 int statement_line_number;
5583
5584 /* There is no statement yet. */
5585 statement = NULL_TREE;
5586 /* Peek at the next token. */
5587 token = cp_lexer_peek_token (parser->lexer);
5588 /* Remember the line number of the first token in the statement. */
5589 statement_line_number = token->line_number;
5590 /* If this is a keyword, then that will often determine what kind of
5591 statement we have. */
5592 if (token->type == CPP_KEYWORD)
5593 {
5594 enum rid keyword = token->keyword;
5595
5596 switch (keyword)
5597 {
5598 case RID_CASE:
5599 case RID_DEFAULT:
5600 statement = cp_parser_labeled_statement (parser);
5601 break;
5602
5603 case RID_IF:
5604 case RID_SWITCH:
5605 statement = cp_parser_selection_statement (parser);
5606 break;
5607
5608 case RID_WHILE:
5609 case RID_DO:
5610 case RID_FOR:
5611 statement = cp_parser_iteration_statement (parser);
5612 break;
5613
5614 case RID_BREAK:
5615 case RID_CONTINUE:
5616 case RID_RETURN:
5617 case RID_GOTO:
5618 statement = cp_parser_jump_statement (parser);
5619 break;
5620
5621 case RID_TRY:
5622 statement = cp_parser_try_block (parser);
5623 break;
5624
5625 default:
5626 /* It might be a keyword like `int' that can start a
5627 declaration-statement. */
5628 break;
5629 }
5630 }
5631 else if (token->type == CPP_NAME)
5632 {
5633 /* If the next token is a `:', then we are looking at a
5634 labeled-statement. */
5635 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5636 if (token->type == CPP_COLON)
5637 statement = cp_parser_labeled_statement (parser);
5638 }
5639 /* Anything that starts with a `{' must be a compound-statement. */
5640 else if (token->type == CPP_OPEN_BRACE)
5641 statement = cp_parser_compound_statement (parser);
5642
5643 /* Everything else must be a declaration-statement or an
5644 expression-statement. Try for the declaration-statement
5645 first, unless we are looking at a `;', in which case we know that
5646 we have an expression-statement. */
5647 if (!statement)
5648 {
5649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5650 {
5651 cp_parser_parse_tentatively (parser);
5652 /* Try to parse the declaration-statement. */
5653 cp_parser_declaration_statement (parser);
5654 /* If that worked, we're done. */
5655 if (cp_parser_parse_definitely (parser))
5656 return;
5657 }
5658 /* Look for an expression-statement instead. */
5659 statement = cp_parser_expression_statement (parser);
5660 }
5661
5662 /* Set the line number for the statement. */
5663 if (statement && statement_code_p (TREE_CODE (statement)))
5664 STMT_LINENO (statement) = statement_line_number;
5665}
5666
5667/* Parse a labeled-statement.
5668
5669 labeled-statement:
5670 identifier : statement
5671 case constant-expression : statement
5672 default : statement
5673
5674 Returns the new CASE_LABEL, for a `case' or `default' label. For
5675 an ordinary label, returns a LABEL_STMT. */
5676
5677static tree
94edc4ab 5678cp_parser_labeled_statement (cp_parser* parser)
a723baf1
MM
5679{
5680 cp_token *token;
5681 tree statement = NULL_TREE;
5682
5683 /* The next token should be an identifier. */
5684 token = cp_lexer_peek_token (parser->lexer);
5685 if (token->type != CPP_NAME
5686 && token->type != CPP_KEYWORD)
5687 {
5688 cp_parser_error (parser, "expected labeled-statement");
5689 return error_mark_node;
5690 }
5691
5692 switch (token->keyword)
5693 {
5694 case RID_CASE:
5695 {
5696 tree expr;
5697
5698 /* Consume the `case' token. */
5699 cp_lexer_consume_token (parser->lexer);
5700 /* Parse the constant-expression. */
14d22dd6
MM
5701 expr = cp_parser_constant_expression (parser,
5702 /*allow_non_constant=*/false,
5703 NULL);
a723baf1
MM
5704 /* Create the label. */
5705 statement = finish_case_label (expr, NULL_TREE);
5706 }
5707 break;
5708
5709 case RID_DEFAULT:
5710 /* Consume the `default' token. */
5711 cp_lexer_consume_token (parser->lexer);
5712 /* Create the label. */
5713 statement = finish_case_label (NULL_TREE, NULL_TREE);
5714 break;
5715
5716 default:
5717 /* Anything else must be an ordinary label. */
5718 statement = finish_label_stmt (cp_parser_identifier (parser));
5719 break;
5720 }
5721
5722 /* Require the `:' token. */
5723 cp_parser_require (parser, CPP_COLON, "`:'");
5724 /* Parse the labeled statement. */
5725 cp_parser_statement (parser);
5726
5727 /* Return the label, in the case of a `case' or `default' label. */
5728 return statement;
5729}
5730
5731/* Parse an expression-statement.
5732
5733 expression-statement:
5734 expression [opt] ;
5735
5736 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5737 statement consists of nothing more than an `;'. */
5738
5739static tree
94edc4ab 5740cp_parser_expression_statement (cp_parser* parser)
a723baf1
MM
5741{
5742 tree statement;
5743
5744 /* If the next token is not a `;', then there is an expression to parse. */
5745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5746 statement = finish_expr_stmt (cp_parser_expression (parser));
5747 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5748 else
5749 {
5750 finish_stmt ();
5751 statement = NULL_TREE;
5752 }
5753 /* Consume the final `;'. */
e0860732 5754 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
5755
5756 return statement;
5757}
5758
5759/* Parse a compound-statement.
5760
5761 compound-statement:
5762 { statement-seq [opt] }
5763
5764 Returns a COMPOUND_STMT representing the statement. */
5765
5766static tree
5767cp_parser_compound_statement (cp_parser *parser)
5768{
5769 tree compound_stmt;
5770
5771 /* Consume the `{'. */
5772 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5773 return error_mark_node;
5774 /* Begin the compound-statement. */
5775 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5776 /* Parse an (optional) statement-seq. */
5777 cp_parser_statement_seq_opt (parser);
5778 /* Finish the compound-statement. */
5779 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5780 /* Consume the `}'. */
5781 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5782
5783 return compound_stmt;
5784}
5785
5786/* Parse an (optional) statement-seq.
5787
5788 statement-seq:
5789 statement
5790 statement-seq [opt] statement */
5791
5792static void
94edc4ab 5793cp_parser_statement_seq_opt (cp_parser* parser)
a723baf1
MM
5794{
5795 /* Scan statements until there aren't any more. */
5796 while (true)
5797 {
5798 /* If we're looking at a `}', then we've run out of statements. */
5799 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5800 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5801 break;
5802
5803 /* Parse the statement. */
5804 cp_parser_statement (parser);
5805 }
5806}
5807
5808/* Parse a selection-statement.
5809
5810 selection-statement:
5811 if ( condition ) statement
5812 if ( condition ) statement else statement
5813 switch ( condition ) statement
5814
5815 Returns the new IF_STMT or SWITCH_STMT. */
5816
5817static tree
94edc4ab 5818cp_parser_selection_statement (cp_parser* parser)
a723baf1
MM
5819{
5820 cp_token *token;
5821 enum rid keyword;
5822
5823 /* Peek at the next token. */
5824 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5825
5826 /* See what kind of keyword it is. */
5827 keyword = token->keyword;
5828 switch (keyword)
5829 {
5830 case RID_IF:
5831 case RID_SWITCH:
5832 {
5833 tree statement;
5834 tree condition;
5835
5836 /* Look for the `('. */
5837 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5838 {
5839 cp_parser_skip_to_end_of_statement (parser);
5840 return error_mark_node;
5841 }
5842
5843 /* Begin the selection-statement. */
5844 if (keyword == RID_IF)
5845 statement = begin_if_stmt ();
5846 else
5847 statement = begin_switch_stmt ();
5848
5849 /* Parse the condition. */
5850 condition = cp_parser_condition (parser);
5851 /* Look for the `)'. */
5852 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5853 cp_parser_skip_to_closing_parenthesis (parser);
5854
5855 if (keyword == RID_IF)
5856 {
5857 tree then_stmt;
5858
5859 /* Add the condition. */
5860 finish_if_stmt_cond (condition, statement);
5861
5862 /* Parse the then-clause. */
5863 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5864 finish_then_clause (statement);
5865
5866 /* If the next token is `else', parse the else-clause. */
5867 if (cp_lexer_next_token_is_keyword (parser->lexer,
5868 RID_ELSE))
5869 {
5870 tree else_stmt;
5871
5872 /* Consume the `else' keyword. */
5873 cp_lexer_consume_token (parser->lexer);
5874 /* Parse the else-clause. */
5875 else_stmt
5876 = cp_parser_implicitly_scoped_statement (parser);
5877 finish_else_clause (statement);
5878 }
5879
5880 /* Now we're all done with the if-statement. */
5881 finish_if_stmt ();
5882 }
5883 else
5884 {
5885 tree body;
5886
5887 /* Add the condition. */
5888 finish_switch_cond (condition, statement);
5889
5890 /* Parse the body of the switch-statement. */
5891 body = cp_parser_implicitly_scoped_statement (parser);
5892
5893 /* Now we're all done with the switch-statement. */
5894 finish_switch_stmt (statement);
5895 }
5896
5897 return statement;
5898 }
5899 break;
5900
5901 default:
5902 cp_parser_error (parser, "expected selection-statement");
5903 return error_mark_node;
5904 }
5905}
5906
5907/* Parse a condition.
5908
5909 condition:
5910 expression
5911 type-specifier-seq declarator = assignment-expression
5912
5913 GNU Extension:
5914
5915 condition:
5916 type-specifier-seq declarator asm-specification [opt]
5917 attributes [opt] = assignment-expression
5918
5919 Returns the expression that should be tested. */
5920
5921static tree
94edc4ab 5922cp_parser_condition (cp_parser* parser)
a723baf1
MM
5923{
5924 tree type_specifiers;
5925 const char *saved_message;
5926
5927 /* Try the declaration first. */
5928 cp_parser_parse_tentatively (parser);
5929 /* New types are not allowed in the type-specifier-seq for a
5930 condition. */
5931 saved_message = parser->type_definition_forbidden_message;
5932 parser->type_definition_forbidden_message
5933 = "types may not be defined in conditions";
5934 /* Parse the type-specifier-seq. */
5935 type_specifiers = cp_parser_type_specifier_seq (parser);
5936 /* Restore the saved message. */
5937 parser->type_definition_forbidden_message = saved_message;
5938 /* If all is well, we might be looking at a declaration. */
5939 if (!cp_parser_error_occurred (parser))
5940 {
5941 tree decl;
5942 tree asm_specification;
5943 tree attributes;
5944 tree declarator;
5945 tree initializer = NULL_TREE;
5946
5947 /* Parse the declarator. */
62b8a44e 5948 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
5949 /*ctor_dtor_or_conv_p=*/NULL);
5950 /* Parse the attributes. */
5951 attributes = cp_parser_attributes_opt (parser);
5952 /* Parse the asm-specification. */
5953 asm_specification = cp_parser_asm_specification_opt (parser);
5954 /* If the next token is not an `=', then we might still be
5955 looking at an expression. For example:
5956
5957 if (A(a).x)
5958
5959 looks like a decl-specifier-seq and a declarator -- but then
5960 there is no `=', so this is an expression. */
5961 cp_parser_require (parser, CPP_EQ, "`='");
5962 /* If we did see an `=', then we are looking at a declaration
5963 for sure. */
5964 if (cp_parser_parse_definitely (parser))
5965 {
5966 /* Create the declaration. */
5967 decl = start_decl (declarator, type_specifiers,
5968 /*initialized_p=*/true,
5969 attributes, /*prefix_attributes=*/NULL_TREE);
5970 /* Parse the assignment-expression. */
5971 initializer = cp_parser_assignment_expression (parser);
5972
5973 /* Process the initializer. */
5974 cp_finish_decl (decl,
5975 initializer,
5976 asm_specification,
5977 LOOKUP_ONLYCONVERTING);
5978
5979 return convert_from_reference (decl);
5980 }
5981 }
5982 /* If we didn't even get past the declarator successfully, we are
5983 definitely not looking at a declaration. */
5984 else
5985 cp_parser_abort_tentative_parse (parser);
5986
5987 /* Otherwise, we are looking at an expression. */
5988 return cp_parser_expression (parser);
5989}
5990
5991/* Parse an iteration-statement.
5992
5993 iteration-statement:
5994 while ( condition ) statement
5995 do statement while ( expression ) ;
5996 for ( for-init-statement condition [opt] ; expression [opt] )
5997 statement
5998
5999 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6000
6001static tree
94edc4ab 6002cp_parser_iteration_statement (cp_parser* parser)
a723baf1
MM
6003{
6004 cp_token *token;
6005 enum rid keyword;
6006 tree statement;
6007
6008 /* Peek at the next token. */
6009 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6010 if (!token)
6011 return error_mark_node;
6012
6013 /* See what kind of keyword it is. */
6014 keyword = token->keyword;
6015 switch (keyword)
6016 {
6017 case RID_WHILE:
6018 {
6019 tree condition;
6020
6021 /* Begin the while-statement. */
6022 statement = begin_while_stmt ();
6023 /* Look for the `('. */
6024 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6025 /* Parse the condition. */
6026 condition = cp_parser_condition (parser);
6027 finish_while_stmt_cond (condition, statement);
6028 /* Look for the `)'. */
6029 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6030 /* Parse the dependent statement. */
6031 cp_parser_already_scoped_statement (parser);
6032 /* We're done with the while-statement. */
6033 finish_while_stmt (statement);
6034 }
6035 break;
6036
6037 case RID_DO:
6038 {
6039 tree expression;
6040
6041 /* Begin the do-statement. */
6042 statement = begin_do_stmt ();
6043 /* Parse the body of the do-statement. */
6044 cp_parser_implicitly_scoped_statement (parser);
6045 finish_do_body (statement);
6046 /* Look for the `while' keyword. */
6047 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6048 /* Look for the `('. */
6049 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6050 /* Parse the expression. */
6051 expression = cp_parser_expression (parser);
6052 /* We're done with the do-statement. */
6053 finish_do_stmt (expression, statement);
6054 /* Look for the `)'. */
6055 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6056 /* Look for the `;'. */
6057 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6058 }
6059 break;
6060
6061 case RID_FOR:
6062 {
6063 tree condition = NULL_TREE;
6064 tree expression = NULL_TREE;
6065
6066 /* Begin the for-statement. */
6067 statement = begin_for_stmt ();
6068 /* Look for the `('. */
6069 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6070 /* Parse the initialization. */
6071 cp_parser_for_init_statement (parser);
6072 finish_for_init_stmt (statement);
6073
6074 /* If there's a condition, process it. */
6075 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6076 condition = cp_parser_condition (parser);
6077 finish_for_cond (condition, statement);
6078 /* Look for the `;'. */
6079 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6080
6081 /* If there's an expression, process it. */
6082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6083 expression = cp_parser_expression (parser);
6084 finish_for_expr (expression, statement);
6085 /* Look for the `)'. */
6086 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6087
6088 /* Parse the body of the for-statement. */
6089 cp_parser_already_scoped_statement (parser);
6090
6091 /* We're done with the for-statement. */
6092 finish_for_stmt (statement);
6093 }
6094 break;
6095
6096 default:
6097 cp_parser_error (parser, "expected iteration-statement");
6098 statement = error_mark_node;
6099 break;
6100 }
6101
6102 return statement;
6103}
6104
6105/* Parse a for-init-statement.
6106
6107 for-init-statement:
6108 expression-statement
6109 simple-declaration */
6110
6111static void
94edc4ab 6112cp_parser_for_init_statement (cp_parser* parser)
a723baf1
MM
6113{
6114 /* If the next token is a `;', then we have an empty
6115 expression-statement. Gramatically, this is also a
6116 simple-declaration, but an invalid one, because it does not
6117 declare anything. Therefore, if we did not handle this case
6118 specially, we would issue an error message about an invalid
6119 declaration. */
6120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6121 {
6122 /* We're going to speculatively look for a declaration, falling back
6123 to an expression, if necessary. */
6124 cp_parser_parse_tentatively (parser);
6125 /* Parse the declaration. */
6126 cp_parser_simple_declaration (parser,
6127 /*function_definition_allowed_p=*/false);
6128 /* If the tentative parse failed, then we shall need to look for an
6129 expression-statement. */
6130 if (cp_parser_parse_definitely (parser))
6131 return;
6132 }
6133
6134 cp_parser_expression_statement (parser);
6135}
6136
6137/* Parse a jump-statement.
6138
6139 jump-statement:
6140 break ;
6141 continue ;
6142 return expression [opt] ;
6143 goto identifier ;
6144
6145 GNU extension:
6146
6147 jump-statement:
6148 goto * expression ;
6149
6150 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6151 GOTO_STMT. */
6152
6153static tree
94edc4ab 6154cp_parser_jump_statement (cp_parser* parser)
a723baf1
MM
6155{
6156 tree statement = error_mark_node;
6157 cp_token *token;
6158 enum rid keyword;
6159
6160 /* Peek at the next token. */
6161 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6162 if (!token)
6163 return error_mark_node;
6164
6165 /* See what kind of keyword it is. */
6166 keyword = token->keyword;
6167 switch (keyword)
6168 {
6169 case RID_BREAK:
6170 statement = finish_break_stmt ();
6171 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6172 break;
6173
6174 case RID_CONTINUE:
6175 statement = finish_continue_stmt ();
6176 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6177 break;
6178
6179 case RID_RETURN:
6180 {
6181 tree expr;
6182
6183 /* If the next token is a `;', then there is no
6184 expression. */
6185 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6186 expr = cp_parser_expression (parser);
6187 else
6188 expr = NULL_TREE;
6189 /* Build the return-statement. */
6190 statement = finish_return_stmt (expr);
6191 /* Look for the final `;'. */
6192 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6193 }
6194 break;
6195
6196 case RID_GOTO:
6197 /* Create the goto-statement. */
6198 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6199 {
6200 /* Issue a warning about this use of a GNU extension. */
6201 if (pedantic)
6202 pedwarn ("ISO C++ forbids computed gotos");
6203 /* Consume the '*' token. */
6204 cp_lexer_consume_token (parser->lexer);
6205 /* Parse the dependent expression. */
6206 finish_goto_stmt (cp_parser_expression (parser));
6207 }
6208 else
6209 finish_goto_stmt (cp_parser_identifier (parser));
6210 /* Look for the final `;'. */
6211 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6212 break;
6213
6214 default:
6215 cp_parser_error (parser, "expected jump-statement");
6216 break;
6217 }
6218
6219 return statement;
6220}
6221
6222/* Parse a declaration-statement.
6223
6224 declaration-statement:
6225 block-declaration */
6226
6227static void
94edc4ab 6228cp_parser_declaration_statement (cp_parser* parser)
a723baf1
MM
6229{
6230 /* Parse the block-declaration. */
6231 cp_parser_block_declaration (parser, /*statement_p=*/true);
6232
6233 /* Finish off the statement. */
6234 finish_stmt ();
6235}
6236
6237/* Some dependent statements (like `if (cond) statement'), are
6238 implicitly in their own scope. In other words, if the statement is
6239 a single statement (as opposed to a compound-statement), it is
6240 none-the-less treated as if it were enclosed in braces. Any
6241 declarations appearing in the dependent statement are out of scope
6242 after control passes that point. This function parses a statement,
6243 but ensures that is in its own scope, even if it is not a
6244 compound-statement.
6245
6246 Returns the new statement. */
6247
6248static tree
94edc4ab 6249cp_parser_implicitly_scoped_statement (cp_parser* parser)
a723baf1
MM
6250{
6251 tree statement;
6252
6253 /* If the token is not a `{', then we must take special action. */
6254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6255 {
6256 /* Create a compound-statement. */
6257 statement = begin_compound_stmt (/*has_no_scope=*/0);
6258 /* Parse the dependent-statement. */
6259 cp_parser_statement (parser);
6260 /* Finish the dummy compound-statement. */
6261 finish_compound_stmt (/*has_no_scope=*/0, statement);
6262 }
6263 /* Otherwise, we simply parse the statement directly. */
6264 else
6265 statement = cp_parser_compound_statement (parser);
6266
6267 /* Return the statement. */
6268 return statement;
6269}
6270
6271/* For some dependent statements (like `while (cond) statement'), we
6272 have already created a scope. Therefore, even if the dependent
6273 statement is a compound-statement, we do not want to create another
6274 scope. */
6275
6276static void
94edc4ab 6277cp_parser_already_scoped_statement (cp_parser* parser)
a723baf1
MM
6278{
6279 /* If the token is not a `{', then we must take special action. */
6280 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6281 {
6282 tree statement;
6283
6284 /* Create a compound-statement. */
6285 statement = begin_compound_stmt (/*has_no_scope=*/1);
6286 /* Parse the dependent-statement. */
6287 cp_parser_statement (parser);
6288 /* Finish the dummy compound-statement. */
6289 finish_compound_stmt (/*has_no_scope=*/1, statement);
6290 }
6291 /* Otherwise, we simply parse the statement directly. */
6292 else
6293 cp_parser_statement (parser);
6294}
6295
6296/* Declarations [gram.dcl.dcl] */
6297
6298/* Parse an optional declaration-sequence.
6299
6300 declaration-seq:
6301 declaration
6302 declaration-seq declaration */
6303
6304static void
94edc4ab 6305cp_parser_declaration_seq_opt (cp_parser* parser)
a723baf1
MM
6306{
6307 while (true)
6308 {
6309 cp_token *token;
6310
6311 token = cp_lexer_peek_token (parser->lexer);
6312
6313 if (token->type == CPP_CLOSE_BRACE
6314 || token->type == CPP_EOF)
6315 break;
6316
6317 if (token->type == CPP_SEMICOLON)
6318 {
6319 /* A declaration consisting of a single semicolon is
6320 invalid. Allow it unless we're being pedantic. */
6321 if (pedantic)
6322 pedwarn ("extra `;'");
6323 cp_lexer_consume_token (parser->lexer);
6324 continue;
6325 }
6326
c838d82f
MM
6327 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6328 parser to enter or exit implict `extern "C"' blocks. */
6329 while (pending_lang_change > 0)
6330 {
6331 push_lang_context (lang_name_c);
6332 --pending_lang_change;
6333 }
6334 while (pending_lang_change < 0)
6335 {
6336 pop_lang_context ();
6337 ++pending_lang_change;
6338 }
6339
6340 /* Parse the declaration itself. */
a723baf1
MM
6341 cp_parser_declaration (parser);
6342 }
6343}
6344
6345/* Parse a declaration.
6346
6347 declaration:
6348 block-declaration
6349 function-definition
6350 template-declaration
6351 explicit-instantiation
6352 explicit-specialization
6353 linkage-specification
1092805d
MM
6354 namespace-definition
6355
6356 GNU extension:
6357
6358 declaration:
6359 __extension__ declaration */
a723baf1
MM
6360
6361static void
94edc4ab 6362cp_parser_declaration (cp_parser* parser)
a723baf1
MM
6363{
6364 cp_token token1;
6365 cp_token token2;
1092805d
MM
6366 int saved_pedantic;
6367
6368 /* Check for the `__extension__' keyword. */
6369 if (cp_parser_extension_opt (parser, &saved_pedantic))
6370 {
6371 /* Parse the qualified declaration. */
6372 cp_parser_declaration (parser);
6373 /* Restore the PEDANTIC flag. */
6374 pedantic = saved_pedantic;
6375
6376 return;
6377 }
a723baf1
MM
6378
6379 /* Try to figure out what kind of declaration is present. */
6380 token1 = *cp_lexer_peek_token (parser->lexer);
6381 if (token1.type != CPP_EOF)
6382 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6383
6384 /* If the next token is `extern' and the following token is a string
6385 literal, then we have a linkage specification. */
6386 if (token1.keyword == RID_EXTERN
6387 && cp_parser_is_string_literal (&token2))
6388 cp_parser_linkage_specification (parser);
6389 /* If the next token is `template', then we have either a template
6390 declaration, an explicit instantiation, or an explicit
6391 specialization. */
6392 else if (token1.keyword == RID_TEMPLATE)
6393 {
6394 /* `template <>' indicates a template specialization. */
6395 if (token2.type == CPP_LESS
6396 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6397 cp_parser_explicit_specialization (parser);
6398 /* `template <' indicates a template declaration. */
6399 else if (token2.type == CPP_LESS)
6400 cp_parser_template_declaration (parser, /*member_p=*/false);
6401 /* Anything else must be an explicit instantiation. */
6402 else
6403 cp_parser_explicit_instantiation (parser);
6404 }
6405 /* If the next token is `export', then we have a template
6406 declaration. */
6407 else if (token1.keyword == RID_EXPORT)
6408 cp_parser_template_declaration (parser, /*member_p=*/false);
6409 /* If the next token is `extern', 'static' or 'inline' and the one
6410 after that is `template', we have a GNU extended explicit
6411 instantiation directive. */
6412 else if (cp_parser_allow_gnu_extensions_p (parser)
6413 && (token1.keyword == RID_EXTERN
6414 || token1.keyword == RID_STATIC
6415 || token1.keyword == RID_INLINE)
6416 && token2.keyword == RID_TEMPLATE)
6417 cp_parser_explicit_instantiation (parser);
6418 /* If the next token is `namespace', check for a named or unnamed
6419 namespace definition. */
6420 else if (token1.keyword == RID_NAMESPACE
6421 && (/* A named namespace definition. */
6422 (token2.type == CPP_NAME
6423 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6424 == CPP_OPEN_BRACE))
6425 /* An unnamed namespace definition. */
6426 || token2.type == CPP_OPEN_BRACE))
6427 cp_parser_namespace_definition (parser);
6428 /* We must have either a block declaration or a function
6429 definition. */
6430 else
6431 /* Try to parse a block-declaration, or a function-definition. */
6432 cp_parser_block_declaration (parser, /*statement_p=*/false);
6433}
6434
6435/* Parse a block-declaration.
6436
6437 block-declaration:
6438 simple-declaration
6439 asm-definition
6440 namespace-alias-definition
6441 using-declaration
6442 using-directive
6443
6444 GNU Extension:
6445
6446 block-declaration:
6447 __extension__ block-declaration
6448 label-declaration
6449
6450 If STATEMENT_P is TRUE, then this block-declaration is ocurring as
6451 part of a declaration-statement. */
6452
6453static void
6454cp_parser_block_declaration (cp_parser *parser,
6455 bool statement_p)
6456{
6457 cp_token *token1;
6458 int saved_pedantic;
6459
6460 /* Check for the `__extension__' keyword. */
6461 if (cp_parser_extension_opt (parser, &saved_pedantic))
6462 {
6463 /* Parse the qualified declaration. */
6464 cp_parser_block_declaration (parser, statement_p);
6465 /* Restore the PEDANTIC flag. */
6466 pedantic = saved_pedantic;
6467
6468 return;
6469 }
6470
6471 /* Peek at the next token to figure out which kind of declaration is
6472 present. */
6473 token1 = cp_lexer_peek_token (parser->lexer);
6474
6475 /* If the next keyword is `asm', we have an asm-definition. */
6476 if (token1->keyword == RID_ASM)
6477 {
6478 if (statement_p)
6479 cp_parser_commit_to_tentative_parse (parser);
6480 cp_parser_asm_definition (parser);
6481 }
6482 /* If the next keyword is `namespace', we have a
6483 namespace-alias-definition. */
6484 else if (token1->keyword == RID_NAMESPACE)
6485 cp_parser_namespace_alias_definition (parser);
6486 /* If the next keyword is `using', we have either a
6487 using-declaration or a using-directive. */
6488 else if (token1->keyword == RID_USING)
6489 {
6490 cp_token *token2;
6491
6492 if (statement_p)
6493 cp_parser_commit_to_tentative_parse (parser);
6494 /* If the token after `using' is `namespace', then we have a
6495 using-directive. */
6496 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6497 if (token2->keyword == RID_NAMESPACE)
6498 cp_parser_using_directive (parser);
6499 /* Otherwise, it's a using-declaration. */
6500 else
6501 cp_parser_using_declaration (parser);
6502 }
6503 /* If the next keyword is `__label__' we have a label declaration. */
6504 else if (token1->keyword == RID_LABEL)
6505 {
6506 if (statement_p)
6507 cp_parser_commit_to_tentative_parse (parser);
6508 cp_parser_label_declaration (parser);
6509 }
6510 /* Anything else must be a simple-declaration. */
6511 else
6512 cp_parser_simple_declaration (parser, !statement_p);
6513}
6514
6515/* Parse a simple-declaration.
6516
6517 simple-declaration:
6518 decl-specifier-seq [opt] init-declarator-list [opt] ;
6519
6520 init-declarator-list:
6521 init-declarator
6522 init-declarator-list , init-declarator
6523
6524 If FUNCTION_DEFINTION_ALLOWED_P is TRUE, then we also recognize a
6525 function-definition as a simple-declaration. */
6526
6527static void
94edc4ab
NN
6528cp_parser_simple_declaration (cp_parser* parser,
6529 bool function_definition_allowed_p)
a723baf1
MM
6530{
6531 tree decl_specifiers;
6532 tree attributes;
a723baf1
MM
6533 bool declares_class_or_enum;
6534 bool saw_declarator;
6535
6536 /* Defer access checks until we know what is being declared; the
6537 checks for names appearing in the decl-specifier-seq should be
6538 done as if we were in the scope of the thing being declared. */
cf22909c
KL
6539 push_deferring_access_checks (true);
6540
a723baf1
MM
6541 /* Parse the decl-specifier-seq. We have to keep track of whether
6542 or not the decl-specifier-seq declares a named class or
6543 enumeration type, since that is the only case in which the
6544 init-declarator-list is allowed to be empty.
6545
6546 [dcl.dcl]
6547
6548 In a simple-declaration, the optional init-declarator-list can be
6549 omitted only when declaring a class or enumeration, that is when
6550 the decl-specifier-seq contains either a class-specifier, an
6551 elaborated-type-specifier, or an enum-specifier. */
6552 decl_specifiers
6553 = cp_parser_decl_specifier_seq (parser,
6554 CP_PARSER_FLAGS_OPTIONAL,
6555 &attributes,
6556 &declares_class_or_enum);
6557 /* We no longer need to defer access checks. */
cf22909c 6558 stop_deferring_access_checks ();
24c0ef37 6559
8fbc5ae7
MM
6560 /* If the next two tokens are both identifiers, the code is
6561 erroneous. The usual cause of this situation is code like:
6562
6563 T t;
6564
6565 where "T" should name a type -- but does not. */
6566 if (cp_parser_diagnose_invalid_type_name (parser))
6567 {
6568 /* If parsing tenatively, we should commit; we really are
6569 looking at a declaration. */
6570 cp_parser_commit_to_tentative_parse (parser);
6571 /* Give up. */
6572 return;
6573 }
6574
a723baf1
MM
6575 /* Keep going until we hit the `;' at the end of the simple
6576 declaration. */
6577 saw_declarator = false;
6578 while (cp_lexer_next_token_is_not (parser->lexer,
6579 CPP_SEMICOLON))
6580 {
6581 cp_token *token;
6582 bool function_definition_p;
6583
6584 saw_declarator = true;
6585 /* Parse the init-declarator. */
6586 cp_parser_init_declarator (parser, decl_specifiers, attributes,
a723baf1
MM
6587 function_definition_allowed_p,
6588 /*member_p=*/false,
6589 &function_definition_p);
1fb3244a
MM
6590 /* If an error occurred while parsing tentatively, exit quickly.
6591 (That usually happens when in the body of a function; each
6592 statement is treated as a declaration-statement until proven
6593 otherwise.) */
6594 if (cp_parser_error_occurred (parser))
6595 {
6596 pop_deferring_access_checks ();
6597 return;
6598 }
a723baf1
MM
6599 /* Handle function definitions specially. */
6600 if (function_definition_p)
6601 {
6602 /* If the next token is a `,', then we are probably
6603 processing something like:
6604
6605 void f() {}, *p;
6606
6607 which is erroneous. */
6608 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6609 error ("mixing declarations and function-definitions is forbidden");
6610 /* Otherwise, we're done with the list of declarators. */
6611 else
24c0ef37 6612 {
cf22909c 6613 pop_deferring_access_checks ();
24c0ef37
GS
6614 return;
6615 }
a723baf1
MM
6616 }
6617 /* The next token should be either a `,' or a `;'. */
6618 token = cp_lexer_peek_token (parser->lexer);
6619 /* If it's a `,', there are more declarators to come. */
6620 if (token->type == CPP_COMMA)
6621 cp_lexer_consume_token (parser->lexer);
6622 /* If it's a `;', we are done. */
6623 else if (token->type == CPP_SEMICOLON)
6624 break;
6625 /* Anything else is an error. */
6626 else
6627 {
6628 cp_parser_error (parser, "expected `,' or `;'");
6629 /* Skip tokens until we reach the end of the statement. */
6630 cp_parser_skip_to_end_of_statement (parser);
cf22909c 6631 pop_deferring_access_checks ();
a723baf1
MM
6632 return;
6633 }
6634 /* After the first time around, a function-definition is not
6635 allowed -- even if it was OK at first. For example:
6636
6637 int i, f() {}
6638
6639 is not valid. */
6640 function_definition_allowed_p = false;
6641 }
6642
6643 /* Issue an error message if no declarators are present, and the
6644 decl-specifier-seq does not itself declare a class or
6645 enumeration. */
6646 if (!saw_declarator)
6647 {
6648 if (cp_parser_declares_only_class_p (parser))
6649 shadow_tag (decl_specifiers);
6650 /* Perform any deferred access checks. */
cf22909c 6651 perform_deferred_access_checks ();
a723baf1
MM
6652 }
6653
cf22909c
KL
6654 pop_deferring_access_checks ();
6655
a723baf1
MM
6656 /* Consume the `;'. */
6657 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6658
6659 /* Mark all the classes that appeared in the decl-specifier-seq as
6660 having received a `;'. */
6661 note_list_got_semicolon (decl_specifiers);
6662}
6663
6664/* Parse a decl-specifier-seq.
6665
6666 decl-specifier-seq:
6667 decl-specifier-seq [opt] decl-specifier
6668
6669 decl-specifier:
6670 storage-class-specifier
6671 type-specifier
6672 function-specifier
6673 friend
6674 typedef
6675
6676 GNU Extension:
6677
6678 decl-specifier-seq:
6679 decl-specifier-seq [opt] attributes
6680
6681 Returns a TREE_LIST, giving the decl-specifiers in the order they
6682 appear in the source code. The TREE_VALUE of each node is the
6683 decl-specifier. For a keyword (such as `auto' or `friend'), the
6684 TREE_VALUE is simply the correspoding TREE_IDENTIFIER. For the
6685 representation of a type-specifier, see cp_parser_type_specifier.
6686
6687 If there are attributes, they will be stored in *ATTRIBUTES,
6688 represented as described above cp_parser_attributes.
6689
6690 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6691 appears, and the entity that will be a friend is not going to be a
6692 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6693 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6694 friendship is granted might not be a class. */
6695
6696static tree
94edc4ab
NN
6697cp_parser_decl_specifier_seq (cp_parser* parser,
6698 cp_parser_flags flags,
6699 tree* attributes,
6700 bool* declares_class_or_enum)
a723baf1
MM
6701{
6702 tree decl_specs = NULL_TREE;
6703 bool friend_p = false;
f2ce60b8
NS
6704 bool constructor_possible_p = !parser->in_declarator_p;
6705
a723baf1
MM
6706 /* Assume no class or enumeration type is declared. */
6707 *declares_class_or_enum = false;
6708
6709 /* Assume there are no attributes. */
6710 *attributes = NULL_TREE;
6711
6712 /* Keep reading specifiers until there are no more to read. */
6713 while (true)
6714 {
6715 tree decl_spec = NULL_TREE;
6716 bool constructor_p;
6717 cp_token *token;
6718
6719 /* Peek at the next token. */
6720 token = cp_lexer_peek_token (parser->lexer);
6721 /* Handle attributes. */
6722 if (token->keyword == RID_ATTRIBUTE)
6723 {
6724 /* Parse the attributes. */
6725 decl_spec = cp_parser_attributes_opt (parser);
6726 /* Add them to the list. */
6727 *attributes = chainon (*attributes, decl_spec);
6728 continue;
6729 }
6730 /* If the next token is an appropriate keyword, we can simply
6731 add it to the list. */
6732 switch (token->keyword)
6733 {
6734 case RID_FRIEND:
6735 /* decl-specifier:
6736 friend */
6737 friend_p = true;
6738 /* The representation of the specifier is simply the
6739 appropriate TREE_IDENTIFIER node. */
6740 decl_spec = token->value;
6741 /* Consume the token. */
6742 cp_lexer_consume_token (parser->lexer);
6743 break;
6744
6745 /* function-specifier:
6746 inline
6747 virtual
6748 explicit */
6749 case RID_INLINE:
6750 case RID_VIRTUAL:
6751 case RID_EXPLICIT:
6752 decl_spec = cp_parser_function_specifier_opt (parser);
6753 break;
6754
6755 /* decl-specifier:
6756 typedef */
6757 case RID_TYPEDEF:
6758 /* The representation of the specifier is simply the
6759 appropriate TREE_IDENTIFIER node. */
6760 decl_spec = token->value;
6761 /* Consume the token. */
6762 cp_lexer_consume_token (parser->lexer);
2050a1bb
MM
6763 /* A constructor declarator cannot appear in a typedef. */
6764 constructor_possible_p = false;
c006d942
MM
6765 /* The "typedef" keyword can only occur in a declaration; we
6766 may as well commit at this point. */
6767 cp_parser_commit_to_tentative_parse (parser);
a723baf1
MM
6768 break;
6769
6770 /* storage-class-specifier:
6771 auto
6772 register
6773 static
6774 extern
6775 mutable
6776
6777 GNU Extension:
6778 thread */
6779 case RID_AUTO:
6780 case RID_REGISTER:
6781 case RID_STATIC:
6782 case RID_EXTERN:
6783 case RID_MUTABLE:
6784 case RID_THREAD:
6785 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6786 break;
6787
6788 default:
6789 break;
6790 }
6791
6792 /* Constructors are a special case. The `S' in `S()' is not a
6793 decl-specifier; it is the beginning of the declarator. */
6794 constructor_p = (!decl_spec
2050a1bb 6795 && constructor_possible_p
a723baf1
MM
6796 && cp_parser_constructor_declarator_p (parser,
6797 friend_p));
6798
6799 /* If we don't have a DECL_SPEC yet, then we must be looking at
6800 a type-specifier. */
6801 if (!decl_spec && !constructor_p)
6802 {
6803 bool decl_spec_declares_class_or_enum;
6804 bool is_cv_qualifier;
6805
6806 decl_spec
6807 = cp_parser_type_specifier (parser, flags,
6808 friend_p,
6809 /*is_declaration=*/true,
6810 &decl_spec_declares_class_or_enum,
6811 &is_cv_qualifier);
6812
6813 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6814
6815 /* If this type-specifier referenced a user-defined type
6816 (a typedef, class-name, etc.), then we can't allow any
6817 more such type-specifiers henceforth.
6818
6819 [dcl.spec]
6820
6821 The longest sequence of decl-specifiers that could
6822 possibly be a type name is taken as the
6823 decl-specifier-seq of a declaration. The sequence shall
6824 be self-consistent as described below.
6825
6826 [dcl.type]
6827
6828 As a general rule, at most one type-specifier is allowed
6829 in the complete decl-specifier-seq of a declaration. The
6830 only exceptions are the following:
6831
6832 -- const or volatile can be combined with any other
6833 type-specifier.
6834
6835 -- signed or unsigned can be combined with char, long,
6836 short, or int.
6837
6838 -- ..
6839
6840 Example:
6841
6842 typedef char* Pc;
6843 void g (const int Pc);
6844
6845 Here, Pc is *not* part of the decl-specifier seq; it's
6846 the declarator. Therefore, once we see a type-specifier
6847 (other than a cv-qualifier), we forbid any additional
6848 user-defined types. We *do* still allow things like `int
6849 int' to be considered a decl-specifier-seq, and issue the
6850 error message later. */
6851 if (decl_spec && !is_cv_qualifier)
6852 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
2050a1bb
MM
6853 /* A constructor declarator cannot follow a type-specifier. */
6854 if (decl_spec)
6855 constructor_possible_p = false;
a723baf1
MM
6856 }
6857
6858 /* If we still do not have a DECL_SPEC, then there are no more
6859 decl-specifiers. */
6860 if (!decl_spec)
6861 {
6862 /* Issue an error message, unless the entire construct was
6863 optional. */
6864 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6865 {
6866 cp_parser_error (parser, "expected decl specifier");
6867 return error_mark_node;
6868 }
6869
6870 break;
6871 }
6872
6873 /* Add the DECL_SPEC to the list of specifiers. */
6874 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6875
6876 /* After we see one decl-specifier, further decl-specifiers are
6877 always optional. */
6878 flags |= CP_PARSER_FLAGS_OPTIONAL;
6879 }
6880
6881 /* We have built up the DECL_SPECS in reverse order. Return them in
6882 the correct order. */
6883 return nreverse (decl_specs);
6884}
6885
6886/* Parse an (optional) storage-class-specifier.
6887
6888 storage-class-specifier:
6889 auto
6890 register
6891 static
6892 extern
6893 mutable
6894
6895 GNU Extension:
6896
6897 storage-class-specifier:
6898 thread
6899
6900 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6901
6902static tree
94edc4ab 6903cp_parser_storage_class_specifier_opt (cp_parser* parser)
a723baf1
MM
6904{
6905 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6906 {
6907 case RID_AUTO:
6908 case RID_REGISTER:
6909 case RID_STATIC:
6910 case RID_EXTERN:
6911 case RID_MUTABLE:
6912 case RID_THREAD:
6913 /* Consume the token. */
6914 return cp_lexer_consume_token (parser->lexer)->value;
6915
6916 default:
6917 return NULL_TREE;
6918 }
6919}
6920
6921/* Parse an (optional) function-specifier.
6922
6923 function-specifier:
6924 inline
6925 virtual
6926 explicit
6927
6928 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6929
6930static tree
94edc4ab 6931cp_parser_function_specifier_opt (cp_parser* parser)
a723baf1
MM
6932{
6933 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6934 {
6935 case RID_INLINE:
6936 case RID_VIRTUAL:
6937 case RID_EXPLICIT:
6938 /* Consume the token. */
6939 return cp_lexer_consume_token (parser->lexer)->value;
6940
6941 default:
6942 return NULL_TREE;
6943 }
6944}
6945
6946/* Parse a linkage-specification.
6947
6948 linkage-specification:
6949 extern string-literal { declaration-seq [opt] }
6950 extern string-literal declaration */
6951
6952static void
94edc4ab 6953cp_parser_linkage_specification (cp_parser* parser)
a723baf1
MM
6954{
6955 cp_token *token;
6956 tree linkage;
6957
6958 /* Look for the `extern' keyword. */
6959 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6960
6961 /* Peek at the next token. */
6962 token = cp_lexer_peek_token (parser->lexer);
6963 /* If it's not a string-literal, then there's a problem. */
6964 if (!cp_parser_is_string_literal (token))
6965 {
6966 cp_parser_error (parser, "expected language-name");
6967 return;
6968 }
6969 /* Consume the token. */
6970 cp_lexer_consume_token (parser->lexer);
6971
6972 /* Transform the literal into an identifier. If the literal is a
6973 wide-character string, or contains embedded NULs, then we can't
6974 handle it as the user wants. */
6975 if (token->type == CPP_WSTRING
6976 || (strlen (TREE_STRING_POINTER (token->value))
6977 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6978 {
6979 cp_parser_error (parser, "invalid linkage-specification");
6980 /* Assume C++ linkage. */
6981 linkage = get_identifier ("c++");
6982 }
6983 /* If it's a simple string constant, things are easier. */
6984 else
6985 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6986
6987 /* We're now using the new linkage. */
6988 push_lang_context (linkage);
6989
6990 /* If the next token is a `{', then we're using the first
6991 production. */
6992 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6993 {
6994 /* Consume the `{' token. */
6995 cp_lexer_consume_token (parser->lexer);
6996 /* Parse the declarations. */
6997 cp_parser_declaration_seq_opt (parser);
6998 /* Look for the closing `}'. */
6999 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7000 }
7001 /* Otherwise, there's just one declaration. */
7002 else
7003 {
7004 bool saved_in_unbraced_linkage_specification_p;
7005
7006 saved_in_unbraced_linkage_specification_p
7007 = parser->in_unbraced_linkage_specification_p;
7008 parser->in_unbraced_linkage_specification_p = true;
7009 have_extern_spec = true;
7010 cp_parser_declaration (parser);
7011 have_extern_spec = false;
7012 parser->in_unbraced_linkage_specification_p
7013 = saved_in_unbraced_linkage_specification_p;
7014 }
7015
7016 /* We're done with the linkage-specification. */
7017 pop_lang_context ();
7018}
7019
7020/* Special member functions [gram.special] */
7021
7022/* Parse a conversion-function-id.
7023
7024 conversion-function-id:
7025 operator conversion-type-id
7026
7027 Returns an IDENTIFIER_NODE representing the operator. */
7028
7029static tree
94edc4ab 7030cp_parser_conversion_function_id (cp_parser* parser)
a723baf1
MM
7031{
7032 tree type;
7033 tree saved_scope;
7034 tree saved_qualifying_scope;
7035 tree saved_object_scope;
7036
7037 /* Look for the `operator' token. */
7038 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7039 return error_mark_node;
7040 /* When we parse the conversion-type-id, the current scope will be
7041 reset. However, we need that information in able to look up the
7042 conversion function later, so we save it here. */
7043 saved_scope = parser->scope;
7044 saved_qualifying_scope = parser->qualifying_scope;
7045 saved_object_scope = parser->object_scope;
7046 /* We must enter the scope of the class so that the names of
7047 entities declared within the class are available in the
7048 conversion-type-id. For example, consider:
7049
7050 struct S {
7051 typedef int I;
7052 operator I();
7053 };
7054
7055 S::operator I() { ... }
7056
7057 In order to see that `I' is a type-name in the definition, we
7058 must be in the scope of `S'. */
7059 if (saved_scope)
7060 push_scope (saved_scope);
7061 /* Parse the conversion-type-id. */
7062 type = cp_parser_conversion_type_id (parser);
7063 /* Leave the scope of the class, if any. */
7064 if (saved_scope)
7065 pop_scope (saved_scope);
7066 /* Restore the saved scope. */
7067 parser->scope = saved_scope;
7068 parser->qualifying_scope = saved_qualifying_scope;
7069 parser->object_scope = saved_object_scope;
7070 /* If the TYPE is invalid, indicate failure. */
7071 if (type == error_mark_node)
7072 return error_mark_node;
7073 return mangle_conv_op_name_for_type (type);
7074}
7075
7076/* Parse a conversion-type-id:
7077
7078 conversion-type-id:
7079 type-specifier-seq conversion-declarator [opt]
7080
7081 Returns the TYPE specified. */
7082
7083static tree
94edc4ab 7084cp_parser_conversion_type_id (cp_parser* parser)
a723baf1
MM
7085{
7086 tree attributes;
7087 tree type_specifiers;
7088 tree declarator;
7089
7090 /* Parse the attributes. */
7091 attributes = cp_parser_attributes_opt (parser);
7092 /* Parse the type-specifiers. */
7093 type_specifiers = cp_parser_type_specifier_seq (parser);
7094 /* If that didn't work, stop. */
7095 if (type_specifiers == error_mark_node)
7096 return error_mark_node;
7097 /* Parse the conversion-declarator. */
7098 declarator = cp_parser_conversion_declarator_opt (parser);
7099
7100 return grokdeclarator (declarator, type_specifiers, TYPENAME,
7101 /*initialized=*/0, &attributes);
7102}
7103
7104/* Parse an (optional) conversion-declarator.
7105
7106 conversion-declarator:
7107 ptr-operator conversion-declarator [opt]
7108
7109 Returns a representation of the declarator. See
7110 cp_parser_declarator for details. */
7111
7112static tree
94edc4ab 7113cp_parser_conversion_declarator_opt (cp_parser* parser)
a723baf1
MM
7114{
7115 enum tree_code code;
7116 tree class_type;
7117 tree cv_qualifier_seq;
7118
7119 /* We don't know if there's a ptr-operator next, or not. */
7120 cp_parser_parse_tentatively (parser);
7121 /* Try the ptr-operator. */
7122 code = cp_parser_ptr_operator (parser, &class_type,
7123 &cv_qualifier_seq);
7124 /* If it worked, look for more conversion-declarators. */
7125 if (cp_parser_parse_definitely (parser))
7126 {
7127 tree declarator;
7128
7129 /* Parse another optional declarator. */
7130 declarator = cp_parser_conversion_declarator_opt (parser);
7131
7132 /* Create the representation of the declarator. */
7133 if (code == INDIRECT_REF)
7134 declarator = make_pointer_declarator (cv_qualifier_seq,
7135 declarator);
7136 else
7137 declarator = make_reference_declarator (cv_qualifier_seq,
7138 declarator);
7139
7140 /* Handle the pointer-to-member case. */
7141 if (class_type)
7142 declarator = build_nt (SCOPE_REF, class_type, declarator);
7143
7144 return declarator;
7145 }
7146
7147 return NULL_TREE;
7148}
7149
7150/* Parse an (optional) ctor-initializer.
7151
7152 ctor-initializer:
7153 : mem-initializer-list
7154
7155 Returns TRUE iff the ctor-initializer was actually present. */
7156
7157static bool
94edc4ab 7158cp_parser_ctor_initializer_opt (cp_parser* parser)
a723baf1
MM
7159{
7160 /* If the next token is not a `:', then there is no
7161 ctor-initializer. */
7162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7163 {
7164 /* Do default initialization of any bases and members. */
7165 if (DECL_CONSTRUCTOR_P (current_function_decl))
7166 finish_mem_initializers (NULL_TREE);
7167
7168 return false;
7169 }
7170
7171 /* Consume the `:' token. */
7172 cp_lexer_consume_token (parser->lexer);
7173 /* And the mem-initializer-list. */
7174 cp_parser_mem_initializer_list (parser);
7175
7176 return true;
7177}
7178
7179/* Parse a mem-initializer-list.
7180
7181 mem-initializer-list:
7182 mem-initializer
7183 mem-initializer , mem-initializer-list */
7184
7185static void
94edc4ab 7186cp_parser_mem_initializer_list (cp_parser* parser)
a723baf1
MM
7187{
7188 tree mem_initializer_list = NULL_TREE;
7189
7190 /* Let the semantic analysis code know that we are starting the
7191 mem-initializer-list. */
7192 begin_mem_initializers ();
7193
7194 /* Loop through the list. */
7195 while (true)
7196 {
7197 tree mem_initializer;
7198
7199 /* Parse the mem-initializer. */
7200 mem_initializer = cp_parser_mem_initializer (parser);
7201 /* Add it to the list, unless it was erroneous. */
7202 if (mem_initializer)
7203 {
7204 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7205 mem_initializer_list = mem_initializer;
7206 }
7207 /* If the next token is not a `,', we're done. */
7208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7209 break;
7210 /* Consume the `,' token. */
7211 cp_lexer_consume_token (parser->lexer);
7212 }
7213
7214 /* Perform semantic analysis. */
7215 finish_mem_initializers (mem_initializer_list);
7216}
7217
7218/* Parse a mem-initializer.
7219
7220 mem-initializer:
7221 mem-initializer-id ( expression-list [opt] )
7222
7223 GNU extension:
7224
7225 mem-initializer:
7226 ( expresion-list [opt] )
7227
7228 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7229 class) or FIELD_DECL (for a non-static data member) to initialize;
7230 the TREE_VALUE is the expression-list. */
7231
7232static tree
94edc4ab 7233cp_parser_mem_initializer (cp_parser* parser)
a723baf1
MM
7234{
7235 tree mem_initializer_id;
7236 tree expression_list;
7237
7238 /* Find out what is being initialized. */
7239 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7240 {
7241 pedwarn ("anachronistic old-style base class initializer");
7242 mem_initializer_id = NULL_TREE;
7243 }
7244 else
7245 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7246 /* Look for the opening `('. */
7247 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7248 /* Parse the expression-list. */
7249 if (cp_lexer_next_token_is_not (parser->lexer,
7250 CPP_CLOSE_PAREN))
7251 expression_list = cp_parser_expression_list (parser);
7252 else
7253 expression_list = void_type_node;
7254 /* Look for the closing `)'. */
7255 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7256
7257 return expand_member_init (mem_initializer_id,
7258 expression_list);
7259}
7260
7261/* Parse a mem-initializer-id.
7262
7263 mem-initializer-id:
7264 :: [opt] nested-name-specifier [opt] class-name
7265 identifier
7266
7267 Returns a TYPE indicating the class to be initializer for the first
7268 production. Returns an IDENTIFIER_NODE indicating the data member
7269 to be initialized for the second production. */
7270
7271static tree
94edc4ab 7272cp_parser_mem_initializer_id (cp_parser* parser)
a723baf1
MM
7273{
7274 bool global_scope_p;
7275 bool nested_name_specifier_p;
7276 tree id;
7277
7278 /* Look for the optional `::' operator. */
7279 global_scope_p
7280 = (cp_parser_global_scope_opt (parser,
7281 /*current_scope_valid_p=*/false)
7282 != NULL_TREE);
7283 /* Look for the optional nested-name-specifier. The simplest way to
7284 implement:
7285
7286 [temp.res]
7287
7288 The keyword `typename' is not permitted in a base-specifier or
7289 mem-initializer; in these contexts a qualified name that
7290 depends on a template-parameter is implicitly assumed to be a
7291 type name.
7292
7293 is to assume that we have seen the `typename' keyword at this
7294 point. */
7295 nested_name_specifier_p
7296 = (cp_parser_nested_name_specifier_opt (parser,
7297 /*typename_keyword_p=*/true,
7298 /*check_dependency_p=*/true,
7299 /*type_p=*/true)
7300 != NULL_TREE);
7301 /* If there is a `::' operator or a nested-name-specifier, then we
7302 are definitely looking for a class-name. */
7303 if (global_scope_p || nested_name_specifier_p)
7304 return cp_parser_class_name (parser,
7305 /*typename_keyword_p=*/true,
7306 /*template_keyword_p=*/false,
7307 /*type_p=*/false,
7308 /*check_access_p=*/true,
7309 /*check_dependency_p=*/true,
7310 /*class_head_p=*/false);
7311 /* Otherwise, we could also be looking for an ordinary identifier. */
7312 cp_parser_parse_tentatively (parser);
7313 /* Try a class-name. */
7314 id = cp_parser_class_name (parser,
7315 /*typename_keyword_p=*/true,
7316 /*template_keyword_p=*/false,
7317 /*type_p=*/false,
7318 /*check_access_p=*/true,
7319 /*check_dependency_p=*/true,
7320 /*class_head_p=*/false);
7321 /* If we found one, we're done. */
7322 if (cp_parser_parse_definitely (parser))
7323 return id;
7324 /* Otherwise, look for an ordinary identifier. */
7325 return cp_parser_identifier (parser);
7326}
7327
7328/* Overloading [gram.over] */
7329
7330/* Parse an operator-function-id.
7331
7332 operator-function-id:
7333 operator operator
7334
7335 Returns an IDENTIFIER_NODE for the operator which is a
7336 human-readable spelling of the identifier, e.g., `operator +'. */
7337
7338static tree
94edc4ab 7339cp_parser_operator_function_id (cp_parser* parser)
a723baf1
MM
7340{
7341 /* Look for the `operator' keyword. */
7342 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7343 return error_mark_node;
7344 /* And then the name of the operator itself. */
7345 return cp_parser_operator (parser);
7346}
7347
7348/* Parse an operator.
7349
7350 operator:
7351 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7352 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7353 || ++ -- , ->* -> () []
7354
7355 GNU Extensions:
7356
7357 operator:
7358 <? >? <?= >?=
7359
7360 Returns an IDENTIFIER_NODE for the operator which is a
7361 human-readable spelling of the identifier, e.g., `operator +'. */
7362
7363static tree
94edc4ab 7364cp_parser_operator (cp_parser* parser)
a723baf1
MM
7365{
7366 tree id = NULL_TREE;
7367 cp_token *token;
7368
7369 /* Peek at the next token. */
7370 token = cp_lexer_peek_token (parser->lexer);
7371 /* Figure out which operator we have. */
7372 switch (token->type)
7373 {
7374 case CPP_KEYWORD:
7375 {
7376 enum tree_code op;
7377
7378 /* The keyword should be either `new' or `delete'. */
7379 if (token->keyword == RID_NEW)
7380 op = NEW_EXPR;
7381 else if (token->keyword == RID_DELETE)
7382 op = DELETE_EXPR;
7383 else
7384 break;
7385
7386 /* Consume the `new' or `delete' token. */
7387 cp_lexer_consume_token (parser->lexer);
7388
7389 /* Peek at the next token. */
7390 token = cp_lexer_peek_token (parser->lexer);
7391 /* If it's a `[' token then this is the array variant of the
7392 operator. */
7393 if (token->type == CPP_OPEN_SQUARE)
7394 {
7395 /* Consume the `[' token. */
7396 cp_lexer_consume_token (parser->lexer);
7397 /* Look for the `]' token. */
7398 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7399 id = ansi_opname (op == NEW_EXPR
7400 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7401 }
7402 /* Otherwise, we have the non-array variant. */
7403 else
7404 id = ansi_opname (op);
7405
7406 return id;
7407 }
7408
7409 case CPP_PLUS:
7410 id = ansi_opname (PLUS_EXPR);
7411 break;
7412
7413 case CPP_MINUS:
7414 id = ansi_opname (MINUS_EXPR);
7415 break;
7416
7417 case CPP_MULT:
7418 id = ansi_opname (MULT_EXPR);
7419 break;
7420
7421 case CPP_DIV:
7422 id = ansi_opname (TRUNC_DIV_EXPR);
7423 break;
7424
7425 case CPP_MOD:
7426 id = ansi_opname (TRUNC_MOD_EXPR);
7427 break;
7428
7429 case CPP_XOR:
7430 id = ansi_opname (BIT_XOR_EXPR);
7431 break;
7432
7433 case CPP_AND:
7434 id = ansi_opname (BIT_AND_EXPR);
7435 break;
7436
7437 case CPP_OR:
7438 id = ansi_opname (BIT_IOR_EXPR);
7439 break;
7440
7441 case CPP_COMPL:
7442 id = ansi_opname (BIT_NOT_EXPR);
7443 break;
7444
7445 case CPP_NOT:
7446 id = ansi_opname (TRUTH_NOT_EXPR);
7447 break;
7448
7449 case CPP_EQ:
7450 id = ansi_assopname (NOP_EXPR);
7451 break;
7452
7453 case CPP_LESS:
7454 id = ansi_opname (LT_EXPR);
7455 break;
7456
7457 case CPP_GREATER:
7458 id = ansi_opname (GT_EXPR);
7459 break;
7460
7461 case CPP_PLUS_EQ:
7462 id = ansi_assopname (PLUS_EXPR);
7463 break;
7464
7465 case CPP_MINUS_EQ:
7466 id = ansi_assopname (MINUS_EXPR);
7467 break;
7468
7469 case CPP_MULT_EQ:
7470 id = ansi_assopname (MULT_EXPR);
7471 break;
7472
7473 case CPP_DIV_EQ:
7474 id = ansi_assopname (TRUNC_DIV_EXPR);
7475 break;
7476
7477 case CPP_MOD_EQ:
7478 id = ansi_assopname (TRUNC_MOD_EXPR);
7479 break;
7480
7481 case CPP_XOR_EQ:
7482 id = ansi_assopname (BIT_XOR_EXPR);
7483 break;
7484
7485 case CPP_AND_EQ:
7486 id = ansi_assopname (BIT_AND_EXPR);
7487 break;
7488
7489 case CPP_OR_EQ:
7490 id = ansi_assopname (BIT_IOR_EXPR);
7491 break;
7492
7493 case CPP_LSHIFT:
7494 id = ansi_opname (LSHIFT_EXPR);
7495 break;
7496
7497 case CPP_RSHIFT:
7498 id = ansi_opname (RSHIFT_EXPR);
7499 break;
7500
7501 case CPP_LSHIFT_EQ:
7502 id = ansi_assopname (LSHIFT_EXPR);
7503 break;
7504
7505 case CPP_RSHIFT_EQ:
7506 id = ansi_assopname (RSHIFT_EXPR);
7507 break;
7508
7509 case CPP_EQ_EQ:
7510 id = ansi_opname (EQ_EXPR);
7511 break;
7512
7513 case CPP_NOT_EQ:
7514 id = ansi_opname (NE_EXPR);
7515 break;
7516
7517 case CPP_LESS_EQ:
7518 id = ansi_opname (LE_EXPR);
7519 break;
7520
7521 case CPP_GREATER_EQ:
7522 id = ansi_opname (GE_EXPR);
7523 break;
7524
7525 case CPP_AND_AND:
7526 id = ansi_opname (TRUTH_ANDIF_EXPR);
7527 break;
7528
7529 case CPP_OR_OR:
7530 id = ansi_opname (TRUTH_ORIF_EXPR);
7531 break;
7532
7533 case CPP_PLUS_PLUS:
7534 id = ansi_opname (POSTINCREMENT_EXPR);
7535 break;
7536
7537 case CPP_MINUS_MINUS:
7538 id = ansi_opname (PREDECREMENT_EXPR);
7539 break;
7540
7541 case CPP_COMMA:
7542 id = ansi_opname (COMPOUND_EXPR);
7543 break;
7544
7545 case CPP_DEREF_STAR:
7546 id = ansi_opname (MEMBER_REF);
7547 break;
7548
7549 case CPP_DEREF:
7550 id = ansi_opname (COMPONENT_REF);
7551 break;
7552
7553 case CPP_OPEN_PAREN:
7554 /* Consume the `('. */
7555 cp_lexer_consume_token (parser->lexer);
7556 /* Look for the matching `)'. */
7557 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7558 return ansi_opname (CALL_EXPR);
7559
7560 case CPP_OPEN_SQUARE:
7561 /* Consume the `['. */
7562 cp_lexer_consume_token (parser->lexer);
7563 /* Look for the matching `]'. */
7564 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7565 return ansi_opname (ARRAY_REF);
7566
7567 /* Extensions. */
7568 case CPP_MIN:
7569 id = ansi_opname (MIN_EXPR);
7570 break;
7571
7572 case CPP_MAX:
7573 id = ansi_opname (MAX_EXPR);
7574 break;
7575
7576 case CPP_MIN_EQ:
7577 id = ansi_assopname (MIN_EXPR);
7578 break;
7579
7580 case CPP_MAX_EQ:
7581 id = ansi_assopname (MAX_EXPR);
7582 break;
7583
7584 default:
7585 /* Anything else is an error. */
7586 break;
7587 }
7588
7589 /* If we have selected an identifier, we need to consume the
7590 operator token. */
7591 if (id)
7592 cp_lexer_consume_token (parser->lexer);
7593 /* Otherwise, no valid operator name was present. */
7594 else
7595 {
7596 cp_parser_error (parser, "expected operator");
7597 id = error_mark_node;
7598 }
7599
7600 return id;
7601}
7602
7603/* Parse a template-declaration.
7604
7605 template-declaration:
7606 export [opt] template < template-parameter-list > declaration
7607
7608 If MEMBER_P is TRUE, this template-declaration occurs within a
7609 class-specifier.
7610
7611 The grammar rule given by the standard isn't correct. What
7612 is really meant is:
7613
7614 template-declaration:
7615 export [opt] template-parameter-list-seq
7616 decl-specifier-seq [opt] init-declarator [opt] ;
7617 export [opt] template-parameter-list-seq
7618 function-definition
7619
7620 template-parameter-list-seq:
7621 template-parameter-list-seq [opt]
7622 template < template-parameter-list > */
7623
7624static void
94edc4ab 7625cp_parser_template_declaration (cp_parser* parser, bool member_p)
a723baf1
MM
7626{
7627 /* Check for `export'. */
7628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7629 {
7630 /* Consume the `export' token. */
7631 cp_lexer_consume_token (parser->lexer);
7632 /* Warn that we do not support `export'. */
7633 warning ("keyword `export' not implemented, and will be ignored");
7634 }
7635
7636 cp_parser_template_declaration_after_export (parser, member_p);
7637}
7638
7639/* Parse a template-parameter-list.
7640
7641 template-parameter-list:
7642 template-parameter
7643 template-parameter-list , template-parameter
7644
7645 Returns a TREE_LIST. Each node represents a template parameter.
7646 The nodes are connected via their TREE_CHAINs. */
7647
7648static tree
94edc4ab 7649cp_parser_template_parameter_list (cp_parser* parser)
a723baf1
MM
7650{
7651 tree parameter_list = NULL_TREE;
7652
7653 while (true)
7654 {
7655 tree parameter;
7656 cp_token *token;
7657
7658 /* Parse the template-parameter. */
7659 parameter = cp_parser_template_parameter (parser);
7660 /* Add it to the list. */
7661 parameter_list = process_template_parm (parameter_list,
7662 parameter);
7663
7664 /* Peek at the next token. */
7665 token = cp_lexer_peek_token (parser->lexer);
7666 /* If it's not a `,', we're done. */
7667 if (token->type != CPP_COMMA)
7668 break;
7669 /* Otherwise, consume the `,' token. */
7670 cp_lexer_consume_token (parser->lexer);
7671 }
7672
7673 return parameter_list;
7674}
7675
7676/* Parse a template-parameter.
7677
7678 template-parameter:
7679 type-parameter
7680 parameter-declaration
7681
7682 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7683 TREE_PURPOSE is the default value, if any. */
7684
7685static tree
94edc4ab 7686cp_parser_template_parameter (cp_parser* parser)
a723baf1
MM
7687{
7688 cp_token *token;
7689
7690 /* Peek at the next token. */
7691 token = cp_lexer_peek_token (parser->lexer);
7692 /* If it is `class' or `template', we have a type-parameter. */
7693 if (token->keyword == RID_TEMPLATE)
7694 return cp_parser_type_parameter (parser);
7695 /* If it is `class' or `typename' we do not know yet whether it is a
7696 type parameter or a non-type parameter. Consider:
7697
7698 template <typename T, typename T::X X> ...
7699
7700 or:
7701
7702 template <class C, class D*> ...
7703
7704 Here, the first parameter is a type parameter, and the second is
7705 a non-type parameter. We can tell by looking at the token after
7706 the identifier -- if it is a `,', `=', or `>' then we have a type
7707 parameter. */
7708 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7709 {
7710 /* Peek at the token after `class' or `typename'. */
7711 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7712 /* If it's an identifier, skip it. */
7713 if (token->type == CPP_NAME)
7714 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7715 /* Now, see if the token looks like the end of a template
7716 parameter. */
7717 if (token->type == CPP_COMMA
7718 || token->type == CPP_EQ
7719 || token->type == CPP_GREATER)
7720 return cp_parser_type_parameter (parser);
7721 }
7722
7723 /* Otherwise, it is a non-type parameter.
7724
7725 [temp.param]
7726
7727 When parsing a default template-argument for a non-type
7728 template-parameter, the first non-nested `>' is taken as the end
7729 of the template parameter-list rather than a greater-than
7730 operator. */
7731 return
ec194454 7732 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
a723baf1
MM
7733}
7734
7735/* Parse a type-parameter.
7736
7737 type-parameter:
7738 class identifier [opt]
7739 class identifier [opt] = type-id
7740 typename identifier [opt]
7741 typename identifier [opt] = type-id
7742 template < template-parameter-list > class identifier [opt]
7743 template < template-parameter-list > class identifier [opt]
7744 = id-expression
7745
7746 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7747 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7748 the declaration of the parameter. */
7749
7750static tree
94edc4ab 7751cp_parser_type_parameter (cp_parser* parser)
a723baf1
MM
7752{
7753 cp_token *token;
7754 tree parameter;
7755
7756 /* Look for a keyword to tell us what kind of parameter this is. */
7757 token = cp_parser_require (parser, CPP_KEYWORD,
8a6393df 7758 "`class', `typename', or `template'");
a723baf1
MM
7759 if (!token)
7760 return error_mark_node;
7761
7762 switch (token->keyword)
7763 {
7764 case RID_CLASS:
7765 case RID_TYPENAME:
7766 {
7767 tree identifier;
7768 tree default_argument;
7769
7770 /* If the next token is an identifier, then it names the
7771 parameter. */
7772 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7773 identifier = cp_parser_identifier (parser);
7774 else
7775 identifier = NULL_TREE;
7776
7777 /* Create the parameter. */
7778 parameter = finish_template_type_parm (class_type_node, identifier);
7779
7780 /* If the next token is an `=', we have a default argument. */
7781 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7782 {
7783 /* Consume the `=' token. */
7784 cp_lexer_consume_token (parser->lexer);
7785 /* Parse the default-argumen. */
7786 default_argument = cp_parser_type_id (parser);
7787 }
7788 else
7789 default_argument = NULL_TREE;
7790
7791 /* Create the combined representation of the parameter and the
7792 default argument. */
7793 parameter = build_tree_list (default_argument,
7794 parameter);
7795 }
7796 break;
7797
7798 case RID_TEMPLATE:
7799 {
7800 tree parameter_list;
7801 tree identifier;
7802 tree default_argument;
7803
7804 /* Look for the `<'. */
7805 cp_parser_require (parser, CPP_LESS, "`<'");
7806 /* Parse the template-parameter-list. */
7807 begin_template_parm_list ();
7808 parameter_list
7809 = cp_parser_template_parameter_list (parser);
7810 parameter_list = end_template_parm_list (parameter_list);
7811 /* Look for the `>'. */
7812 cp_parser_require (parser, CPP_GREATER, "`>'");
7813 /* Look for the `class' keyword. */
7814 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7815 /* If the next token is an `=', then there is a
7816 default-argument. If the next token is a `>', we are at
7817 the end of the parameter-list. If the next token is a `,',
7818 then we are at the end of this parameter. */
7819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7820 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7821 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7822 identifier = cp_parser_identifier (parser);
7823 else
7824 identifier = NULL_TREE;
7825 /* Create the template parameter. */
7826 parameter = finish_template_template_parm (class_type_node,
7827 identifier);
7828
7829 /* If the next token is an `=', then there is a
7830 default-argument. */
7831 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7832 {
7833 /* Consume the `='. */
7834 cp_lexer_consume_token (parser->lexer);
7835 /* Parse the id-expression. */
7836 default_argument
7837 = cp_parser_id_expression (parser,
7838 /*template_keyword_p=*/false,
7839 /*check_dependency_p=*/true,
7840 /*template_p=*/NULL);
7841 /* Look up the name. */
7842 default_argument
7843 = cp_parser_lookup_name_simple (parser, default_argument);
7844 /* See if the default argument is valid. */
7845 default_argument
7846 = check_template_template_default_arg (default_argument);
7847 }
7848 else
7849 default_argument = NULL_TREE;
7850
7851 /* Create the combined representation of the parameter and the
7852 default argument. */
7853 parameter = build_tree_list (default_argument,
7854 parameter);
7855 }
7856 break;
7857
7858 default:
7859 /* Anything else is an error. */
7860 cp_parser_error (parser,
7861 "expected `class', `typename', or `template'");
7862 parameter = error_mark_node;
7863 }
7864
7865 return parameter;
7866}
7867
7868/* Parse a template-id.
7869
7870 template-id:
7871 template-name < template-argument-list [opt] >
7872
7873 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7874 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7875 returned. Otherwise, if the template-name names a function, or set
7876 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7877 names a class, returns a TYPE_DECL for the specialization.
7878
7879 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7880 uninstantiated templates. */
7881
7882static tree
7883cp_parser_template_id (cp_parser *parser,
7884 bool template_keyword_p,
7885 bool check_dependency_p)
7886{
7887 tree template;
7888 tree arguments;
7889 tree saved_scope;
7890 tree saved_qualifying_scope;
7891 tree saved_object_scope;
7892 tree template_id;
7893 bool saved_greater_than_is_operator_p;
7894 ptrdiff_t start_of_id;
7895 tree access_check = NULL_TREE;
2050a1bb 7896 cp_token *next_token;
a723baf1
MM
7897
7898 /* If the next token corresponds to a template-id, there is no need
7899 to reparse it. */
2050a1bb
MM
7900 next_token = cp_lexer_peek_token (parser->lexer);
7901 if (next_token->type == CPP_TEMPLATE_ID)
a723baf1
MM
7902 {
7903 tree value;
7904 tree check;
7905
7906 /* Get the stored value. */
7907 value = cp_lexer_consume_token (parser->lexer)->value;
7908 /* Perform any access checks that were deferred. */
7909 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c
KL
7910 perform_or_defer_access_check (TREE_PURPOSE (check),
7911 TREE_VALUE (check));
a723baf1
MM
7912 /* Return the stored value. */
7913 return TREE_VALUE (value);
7914 }
7915
2050a1bb
MM
7916 /* Avoid performing name lookup if there is no possibility of
7917 finding a template-id. */
7918 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7919 || (next_token->type == CPP_NAME
7920 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7921 {
7922 cp_parser_error (parser, "expected template-id");
7923 return error_mark_node;
7924 }
7925
a723baf1
MM
7926 /* Remember where the template-id starts. */
7927 if (cp_parser_parsing_tentatively (parser)
7928 && !cp_parser_committed_to_tentative_parse (parser))
7929 {
2050a1bb 7930 next_token = cp_lexer_peek_token (parser->lexer);
a723baf1
MM
7931 start_of_id = cp_lexer_token_difference (parser->lexer,
7932 parser->lexer->first_token,
7933 next_token);
a723baf1
MM
7934 }
7935 else
7936 start_of_id = -1;
7937
cf22909c
KL
7938 push_deferring_access_checks (true);
7939
a723baf1
MM
7940 /* Parse the template-name. */
7941 template = cp_parser_template_name (parser, template_keyword_p,
7942 check_dependency_p);
7943 if (template == error_mark_node)
cf22909c
KL
7944 {
7945 pop_deferring_access_checks ();
7946 return error_mark_node;
7947 }
a723baf1
MM
7948
7949 /* Look for the `<' that starts the template-argument-list. */
7950 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
cf22909c
KL
7951 {
7952 pop_deferring_access_checks ();
7953 return error_mark_node;
7954 }
a723baf1
MM
7955
7956 /* [temp.names]
7957
7958 When parsing a template-id, the first non-nested `>' is taken as
7959 the end of the template-argument-list rather than a greater-than
7960 operator. */
7961 saved_greater_than_is_operator_p
7962 = parser->greater_than_is_operator_p;
7963 parser->greater_than_is_operator_p = false;
7964 /* Parsing the argument list may modify SCOPE, so we save it
7965 here. */
7966 saved_scope = parser->scope;
7967 saved_qualifying_scope = parser->qualifying_scope;
7968 saved_object_scope = parser->object_scope;
7969 /* Parse the template-argument-list itself. */
7970 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7971 arguments = NULL_TREE;
7972 else
7973 arguments = cp_parser_template_argument_list (parser);
7974 /* Look for the `>' that ends the template-argument-list. */
7975 cp_parser_require (parser, CPP_GREATER, "`>'");
7976 /* The `>' token might be a greater-than operator again now. */
7977 parser->greater_than_is_operator_p
7978 = saved_greater_than_is_operator_p;
7979 /* Restore the SAVED_SCOPE. */
7980 parser->scope = saved_scope;
7981 parser->qualifying_scope = saved_qualifying_scope;
7982 parser->object_scope = saved_object_scope;
7983
7984 /* Build a representation of the specialization. */
7985 if (TREE_CODE (template) == IDENTIFIER_NODE)
7986 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7987 else if (DECL_CLASS_TEMPLATE_P (template)
7988 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7989 template_id
7990 = finish_template_type (template, arguments,
7991 cp_lexer_next_token_is (parser->lexer,
7992 CPP_SCOPE));
7993 else
7994 {
7995 /* If it's not a class-template or a template-template, it should be
7996 a function-template. */
7997 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7998 || TREE_CODE (template) == OVERLOAD
7999 || BASELINK_P (template)),
8000 20010716);
8001
8002 template_id = lookup_template_function (template, arguments);
8003 }
8004
cf22909c
KL
8005 /* Retrieve any deferred checks. Do not pop this access checks yet
8006 so the memory will not be reclaimed during token replacing below. */
8007 access_check = get_deferred_access_checks ();
8008
a723baf1
MM
8009 /* If parsing tentatively, replace the sequence of tokens that makes
8010 up the template-id with a CPP_TEMPLATE_ID token. That way,
8011 should we re-parse the token stream, we will not have to repeat
8012 the effort required to do the parse, nor will we issue duplicate
8013 error messages about problems during instantiation of the
8014 template. */
8015 if (start_of_id >= 0)
8016 {
8017 cp_token *token;
a723baf1
MM
8018
8019 /* Find the token that corresponds to the start of the
8020 template-id. */
8021 token = cp_lexer_advance_token (parser->lexer,
8022 parser->lexer->first_token,
8023 start_of_id);
8024
a723baf1
MM
8025 /* Reset the contents of the START_OF_ID token. */
8026 token->type = CPP_TEMPLATE_ID;
8027 token->value = build_tree_list (access_check, template_id);
8028 token->keyword = RID_MAX;
8029 /* Purge all subsequent tokens. */
8030 cp_lexer_purge_tokens_after (parser->lexer, token);
8031 }
8032
cf22909c 8033 pop_deferring_access_checks ();
a723baf1
MM
8034 return template_id;
8035}
8036
8037/* Parse a template-name.
8038
8039 template-name:
8040 identifier
8041
8042 The standard should actually say:
8043
8044 template-name:
8045 identifier
8046 operator-function-id
8047 conversion-function-id
8048
8049 A defect report has been filed about this issue.
8050
8051 If TEMPLATE_KEYWORD_P is true, then we have just seen the
8052 `template' keyword, in a construction like:
8053
8054 T::template f<3>()
8055
8056 In that case `f' is taken to be a template-name, even though there
8057 is no way of knowing for sure.
8058
8059 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8060 name refers to a set of overloaded functions, at least one of which
8061 is a template, or an IDENTIFIER_NODE with the name of the template,
8062 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
8063 names are looked up inside uninstantiated templates. */
8064
8065static tree
94edc4ab
NN
8066cp_parser_template_name (cp_parser* parser,
8067 bool template_keyword_p,
8068 bool check_dependency_p)
a723baf1
MM
8069{
8070 tree identifier;
8071 tree decl;
8072 tree fns;
8073
8074 /* If the next token is `operator', then we have either an
8075 operator-function-id or a conversion-function-id. */
8076 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8077 {
8078 /* We don't know whether we're looking at an
8079 operator-function-id or a conversion-function-id. */
8080 cp_parser_parse_tentatively (parser);
8081 /* Try an operator-function-id. */
8082 identifier = cp_parser_operator_function_id (parser);
8083 /* If that didn't work, try a conversion-function-id. */
8084 if (!cp_parser_parse_definitely (parser))
8085 identifier = cp_parser_conversion_function_id (parser);
8086 }
8087 /* Look for the identifier. */
8088 else
8089 identifier = cp_parser_identifier (parser);
8090
8091 /* If we didn't find an identifier, we don't have a template-id. */
8092 if (identifier == error_mark_node)
8093 return error_mark_node;
8094
8095 /* If the name immediately followed the `template' keyword, then it
8096 is a template-name. However, if the next token is not `<', then
8097 we do not treat it as a template-name, since it is not being used
8098 as part of a template-id. This enables us to handle constructs
8099 like:
8100
8101 template <typename T> struct S { S(); };
8102 template <typename T> S<T>::S();
8103
8104 correctly. We would treat `S' as a template -- if it were `S<T>'
8105 -- but we do not if there is no `<'. */
8106 if (template_keyword_p && processing_template_decl
8107 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
8108 return identifier;
8109
8110 /* Look up the name. */
8111 decl = cp_parser_lookup_name (parser, identifier,
8112 /*check_access=*/true,
8113 /*is_type=*/false,
eea9800f 8114 /*is_namespace=*/false,
a723baf1
MM
8115 check_dependency_p);
8116 decl = maybe_get_template_decl_from_type_decl (decl);
8117
8118 /* If DECL is a template, then the name was a template-name. */
8119 if (TREE_CODE (decl) == TEMPLATE_DECL)
8120 ;
8121 else
8122 {
8123 /* The standard does not explicitly indicate whether a name that
8124 names a set of overloaded declarations, some of which are
8125 templates, is a template-name. However, such a name should
8126 be a template-name; otherwise, there is no way to form a
8127 template-id for the overloaded templates. */
8128 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8129 if (TREE_CODE (fns) == OVERLOAD)
8130 {
8131 tree fn;
8132
8133 for (fn = fns; fn; fn = OVL_NEXT (fn))
8134 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8135 break;
8136 }
8137 else
8138 {
8139 /* Otherwise, the name does not name a template. */
8140 cp_parser_error (parser, "expected template-name");
8141 return error_mark_node;
8142 }
8143 }
8144
8145 /* If DECL is dependent, and refers to a function, then just return
8146 its name; we will look it up again during template instantiation. */
8147 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8148 {
8149 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
1fb3244a 8150 if (TYPE_P (scope) && dependent_type_p (scope))
a723baf1
MM
8151 return identifier;
8152 }
8153
8154 return decl;
8155}
8156
8157/* Parse a template-argument-list.
8158
8159 template-argument-list:
8160 template-argument
8161 template-argument-list , template-argument
8162
8163 Returns a TREE_LIST representing the arguments, in the order they
8164 appeared. The TREE_VALUE of each node is a representation of the
8165 argument. */
8166
8167static tree
94edc4ab 8168cp_parser_template_argument_list (cp_parser* parser)
a723baf1
MM
8169{
8170 tree arguments = NULL_TREE;
8171
8172 while (true)
8173 {
8174 tree argument;
8175
8176 /* Parse the template-argument. */
8177 argument = cp_parser_template_argument (parser);
8178 /* Add it to the list. */
8179 arguments = tree_cons (NULL_TREE, argument, arguments);
8180 /* If it is not a `,', then there are no more arguments. */
8181 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8182 break;
8183 /* Otherwise, consume the ','. */
8184 cp_lexer_consume_token (parser->lexer);
8185 }
8186
8187 /* We built up the arguments in reverse order. */
8188 return nreverse (arguments);
8189}
8190
8191/* Parse a template-argument.
8192
8193 template-argument:
8194 assignment-expression
8195 type-id
8196 id-expression
8197
8198 The representation is that of an assignment-expression, type-id, or
8199 id-expression -- except that the qualified id-expression is
8200 evaluated, so that the value returned is either a DECL or an
8201 OVERLOAD. */
8202
8203static tree
94edc4ab 8204cp_parser_template_argument (cp_parser* parser)
a723baf1
MM
8205{
8206 tree argument;
8207 bool template_p;
8208
8209 /* There's really no way to know what we're looking at, so we just
8210 try each alternative in order.
8211
8212 [temp.arg]
8213
8214 In a template-argument, an ambiguity between a type-id and an
8215 expression is resolved to a type-id, regardless of the form of
8216 the corresponding template-parameter.
8217
8218 Therefore, we try a type-id first. */
8219 cp_parser_parse_tentatively (parser);
a723baf1
MM
8220 argument = cp_parser_type_id (parser);
8221 /* If the next token isn't a `,' or a `>', then this argument wasn't
8222 really finished. */
8223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8224 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8225 cp_parser_error (parser, "expected template-argument");
8226 /* If that worked, we're done. */
8227 if (cp_parser_parse_definitely (parser))
8228 return argument;
8229 /* We're still not sure what the argument will be. */
8230 cp_parser_parse_tentatively (parser);
8231 /* Try a template. */
8232 argument = cp_parser_id_expression (parser,
8233 /*template_keyword_p=*/false,
8234 /*check_dependency_p=*/true,
8235 &template_p);
8236 /* If the next token isn't a `,' or a `>', then this argument wasn't
8237 really finished. */
8238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8239 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8240 cp_parser_error (parser, "expected template-argument");
8241 if (!cp_parser_error_occurred (parser))
8242 {
8243 /* Figure out what is being referred to. */
8244 argument = cp_parser_lookup_name_simple (parser, argument);
8245 if (template_p)
8246 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8247 TREE_OPERAND (argument, 1),
8248 tf_error | tf_parsing);
8249 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8250 cp_parser_error (parser, "expected template-name");
8251 }
8252 if (cp_parser_parse_definitely (parser))
8253 return argument;
8254 /* It must be an assignment-expression. */
8255 return cp_parser_assignment_expression (parser);
8256}
8257
8258/* Parse an explicit-instantiation.
8259
8260 explicit-instantiation:
8261 template declaration
8262
8263 Although the standard says `declaration', what it really means is:
8264
8265 explicit-instantiation:
8266 template decl-specifier-seq [opt] declarator [opt] ;
8267
8268 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8269 supposed to be allowed. A defect report has been filed about this
8270 issue.
8271
8272 GNU Extension:
8273
8274 explicit-instantiation:
8275 storage-class-specifier template
8276 decl-specifier-seq [opt] declarator [opt] ;
8277 function-specifier template
8278 decl-specifier-seq [opt] declarator [opt] ; */
8279
8280static void
94edc4ab 8281cp_parser_explicit_instantiation (cp_parser* parser)
a723baf1
MM
8282{
8283 bool declares_class_or_enum;
8284 tree decl_specifiers;
8285 tree attributes;
8286 tree extension_specifier = NULL_TREE;
8287
8288 /* Look for an (optional) storage-class-specifier or
8289 function-specifier. */
8290 if (cp_parser_allow_gnu_extensions_p (parser))
8291 {
8292 extension_specifier
8293 = cp_parser_storage_class_specifier_opt (parser);
8294 if (!extension_specifier)
8295 extension_specifier = cp_parser_function_specifier_opt (parser);
8296 }
8297
8298 /* Look for the `template' keyword. */
8299 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8300 /* Let the front end know that we are processing an explicit
8301 instantiation. */
8302 begin_explicit_instantiation ();
8303 /* [temp.explicit] says that we are supposed to ignore access
8304 control while processing explicit instantiation directives. */
8305 scope_chain->check_access = 0;
8306 /* Parse a decl-specifier-seq. */
8307 decl_specifiers
8308 = cp_parser_decl_specifier_seq (parser,
8309 CP_PARSER_FLAGS_OPTIONAL,
8310 &attributes,
8311 &declares_class_or_enum);
8312 /* If there was exactly one decl-specifier, and it declared a class,
8313 and there's no declarator, then we have an explicit type
8314 instantiation. */
8315 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8316 {
8317 tree type;
8318
8319 type = check_tag_decl (decl_specifiers);
8320 if (type)
8321 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8322 }
8323 else
8324 {
8325 tree declarator;
8326 tree decl;
8327
8328 /* Parse the declarator. */
8329 declarator
62b8a44e 8330 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
8331 /*ctor_dtor_or_conv_p=*/NULL);
8332 decl = grokdeclarator (declarator, decl_specifiers,
8333 NORMAL, 0, NULL);
8334 /* Do the explicit instantiation. */
8335 do_decl_instantiation (decl, extension_specifier);
8336 }
8337 /* We're done with the instantiation. */
8338 end_explicit_instantiation ();
8339 /* Trun access control back on. */
8340 scope_chain->check_access = flag_access_control;
8341
e0860732 8342 cp_parser_consume_semicolon_at_end_of_statement (parser);
a723baf1
MM
8343}
8344
8345/* Parse an explicit-specialization.
8346
8347 explicit-specialization:
8348 template < > declaration
8349
8350 Although the standard says `declaration', what it really means is:
8351
8352 explicit-specialization:
8353 template <> decl-specifier [opt] init-declarator [opt] ;
8354 template <> function-definition
8355 template <> explicit-specialization
8356 template <> template-declaration */
8357
8358static void
94edc4ab 8359cp_parser_explicit_specialization (cp_parser* parser)
a723baf1
MM
8360{
8361 /* Look for the `template' keyword. */
8362 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8363 /* Look for the `<'. */
8364 cp_parser_require (parser, CPP_LESS, "`<'");
8365 /* Look for the `>'. */
8366 cp_parser_require (parser, CPP_GREATER, "`>'");
8367 /* We have processed another parameter list. */
8368 ++parser->num_template_parameter_lists;
8369 /* Let the front end know that we are beginning a specialization. */
8370 begin_specialization ();
8371
8372 /* If the next keyword is `template', we need to figure out whether
8373 or not we're looking a template-declaration. */
8374 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8375 {
8376 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8377 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8378 cp_parser_template_declaration_after_export (parser,
8379 /*member_p=*/false);
8380 else
8381 cp_parser_explicit_specialization (parser);
8382 }
8383 else
8384 /* Parse the dependent declaration. */
8385 cp_parser_single_declaration (parser,
8386 /*member_p=*/false,
8387 /*friend_p=*/NULL);
8388
8389 /* We're done with the specialization. */
8390 end_specialization ();
8391 /* We're done with this parameter list. */
8392 --parser->num_template_parameter_lists;
8393}
8394
8395/* Parse a type-specifier.
8396
8397 type-specifier:
8398 simple-type-specifier
8399 class-specifier
8400 enum-specifier
8401 elaborated-type-specifier
8402 cv-qualifier
8403
8404 GNU Extension:
8405
8406 type-specifier:
8407 __complex__
8408
8409 Returns a representation of the type-specifier. If the
8410 type-specifier is a keyword (like `int' or `const', or
8411 `__complex__') then the correspoding IDENTIFIER_NODE is returned.
8412 For a class-specifier, enum-specifier, or elaborated-type-specifier
8413 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8414
8415 If IS_FRIEND is TRUE then this type-specifier is being declared a
8416 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8417 appearing in a decl-specifier-seq.
8418
8419 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8420 class-specifier, enum-specifier, or elaborated-type-specifier, then
8421 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8422 FALSE.
8423
8424 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8425 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8426 is set to FALSE. */
8427
8428static tree
94edc4ab
NN
8429cp_parser_type_specifier (cp_parser* parser,
8430 cp_parser_flags flags,
8431 bool is_friend,
8432 bool is_declaration,
8433 bool* declares_class_or_enum,
8434 bool* is_cv_qualifier)
a723baf1
MM
8435{
8436 tree type_spec = NULL_TREE;
8437 cp_token *token;
8438 enum rid keyword;
8439
8440 /* Assume this type-specifier does not declare a new type. */
8441 if (declares_class_or_enum)
8442 *declares_class_or_enum = false;
8443 /* And that it does not specify a cv-qualifier. */
8444 if (is_cv_qualifier)
8445 *is_cv_qualifier = false;
8446 /* Peek at the next token. */
8447 token = cp_lexer_peek_token (parser->lexer);
8448
8449 /* If we're looking at a keyword, we can use that to guide the
8450 production we choose. */
8451 keyword = token->keyword;
8452 switch (keyword)
8453 {
8454 /* Any of these indicate either a class-specifier, or an
8455 elaborated-type-specifier. */
8456 case RID_CLASS:
8457 case RID_STRUCT:
8458 case RID_UNION:
8459 case RID_ENUM:
8460 /* Parse tentatively so that we can back up if we don't find a
8461 class-specifier or enum-specifier. */
8462 cp_parser_parse_tentatively (parser);
8463 /* Look for the class-specifier or enum-specifier. */
8464 if (keyword == RID_ENUM)
8465 type_spec = cp_parser_enum_specifier (parser);
8466 else
8467 type_spec = cp_parser_class_specifier (parser);
8468
8469 /* If that worked, we're done. */
8470 if (cp_parser_parse_definitely (parser))
8471 {
8472 if (declares_class_or_enum)
8473 *declares_class_or_enum = true;
8474 return type_spec;
8475 }
8476
8477 /* Fall through. */
8478
8479 case RID_TYPENAME:
8480 /* Look for an elaborated-type-specifier. */
8481 type_spec = cp_parser_elaborated_type_specifier (parser,
8482 is_friend,
8483 is_declaration);
8484 /* We're declaring a class or enum -- unless we're using
8485 `typename'. */
8486 if (declares_class_or_enum && keyword != RID_TYPENAME)
8487 *declares_class_or_enum = true;
8488 return type_spec;
8489
8490 case RID_CONST:
8491 case RID_VOLATILE:
8492 case RID_RESTRICT:
8493 type_spec = cp_parser_cv_qualifier_opt (parser);
8494 /* Even though we call a routine that looks for an optional
8495 qualifier, we know that there should be one. */
8496 my_friendly_assert (type_spec != NULL, 20000328);
8497 /* This type-specifier was a cv-qualified. */
8498 if (is_cv_qualifier)
8499 *is_cv_qualifier = true;
8500
8501 return type_spec;
8502
8503 case RID_COMPLEX:
8504 /* The `__complex__' keyword is a GNU extension. */
8505 return cp_lexer_consume_token (parser->lexer)->value;
8506
8507 default:
8508 break;
8509 }
8510
8511 /* If we do not already have a type-specifier, assume we are looking
8512 at a simple-type-specifier. */
8513 type_spec = cp_parser_simple_type_specifier (parser, flags);
8514
8515 /* If we didn't find a type-specifier, and a type-specifier was not
8516 optional in this context, issue an error message. */
8517 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8518 {
8519 cp_parser_error (parser, "expected type specifier");
8520 return error_mark_node;
8521 }
8522
8523 return type_spec;
8524}
8525
8526/* Parse a simple-type-specifier.
8527
8528 simple-type-specifier:
8529 :: [opt] nested-name-specifier [opt] type-name
8530 :: [opt] nested-name-specifier template template-id
8531 char
8532 wchar_t
8533 bool
8534 short
8535 int
8536 long
8537 signed
8538 unsigned
8539 float
8540 double
8541 void
8542
8543 GNU Extension:
8544
8545 simple-type-specifier:
8546 __typeof__ unary-expression
8547 __typeof__ ( type-id )
8548
8549 For the various keywords, the value returned is simply the
8550 TREE_IDENTIFIER representing the keyword. For the first two
8551 productions, the value returned is the indicated TYPE_DECL. */
8552
8553static tree
94edc4ab 8554cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags)
a723baf1
MM
8555{
8556 tree type = NULL_TREE;
8557 cp_token *token;
8558
8559 /* Peek at the next token. */
8560 token = cp_lexer_peek_token (parser->lexer);
8561
8562 /* If we're looking at a keyword, things are easy. */
8563 switch (token->keyword)
8564 {
8565 case RID_CHAR:
8566 case RID_WCHAR:
8567 case RID_BOOL:
8568 case RID_SHORT:
8569 case RID_INT:
8570 case RID_LONG:
8571 case RID_SIGNED:
8572 case RID_UNSIGNED:
8573 case RID_FLOAT:
8574 case RID_DOUBLE:
8575 case RID_VOID:
8576 /* Consume the token. */
8577 return cp_lexer_consume_token (parser->lexer)->value;
8578
8579 case RID_TYPEOF:
8580 {
8581 tree operand;
8582
8583 /* Consume the `typeof' token. */
8584 cp_lexer_consume_token (parser->lexer);
8585 /* Parse the operand to `typeof' */
8586 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8587 /* If it is not already a TYPE, take its type. */
8588 if (!TYPE_P (operand))
8589 operand = finish_typeof (operand);
8590
8591 return operand;
8592 }
8593
8594 default:
8595 break;
8596 }
8597
8598 /* The type-specifier must be a user-defined type. */
8599 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8600 {
8601 /* Don't gobble tokens or issue error messages if this is an
8602 optional type-specifier. */
8603 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8604 cp_parser_parse_tentatively (parser);
8605
8606 /* Look for the optional `::' operator. */
8607 cp_parser_global_scope_opt (parser,
8608 /*current_scope_valid_p=*/false);
8609 /* Look for the nested-name specifier. */
8610 cp_parser_nested_name_specifier_opt (parser,
8611 /*typename_keyword_p=*/false,
8612 /*check_dependency_p=*/true,
8613 /*type_p=*/false);
8614 /* If we have seen a nested-name-specifier, and the next token
8615 is `template', then we are using the template-id production. */
8616 if (parser->scope
8617 && cp_parser_optional_template_keyword (parser))
8618 {
8619 /* Look for the template-id. */
8620 type = cp_parser_template_id (parser,
8621 /*template_keyword_p=*/true,
8622 /*check_dependency_p=*/true);
8623 /* If the template-id did not name a type, we are out of
8624 luck. */
8625 if (TREE_CODE (type) != TYPE_DECL)
8626 {
8627 cp_parser_error (parser, "expected template-id for type");
8628 type = NULL_TREE;
8629 }
8630 }
8631 /* Otherwise, look for a type-name. */
8632 else
8633 {
8634 type = cp_parser_type_name (parser);
8635 if (type == error_mark_node)
8636 type = NULL_TREE;
8637 }
8638
8639 /* If it didn't work out, we don't have a TYPE. */
8640 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8641 && !cp_parser_parse_definitely (parser))
8642 type = NULL_TREE;
8643 }
8644
8645 /* If we didn't get a type-name, issue an error message. */
8646 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8647 {
8648 cp_parser_error (parser, "expected type-name");
8649 return error_mark_node;
8650 }
8651
8652 return type;
8653}
8654
8655/* Parse a type-name.
8656
8657 type-name:
8658 class-name
8659 enum-name
8660 typedef-name
8661
8662 enum-name:
8663 identifier
8664
8665 typedef-name:
8666 identifier
8667
8668 Returns a TYPE_DECL for the the type. */
8669
8670static tree
94edc4ab 8671cp_parser_type_name (cp_parser* parser)
a723baf1
MM
8672{
8673 tree type_decl;
8674 tree identifier;
8675
8676 /* We can't know yet whether it is a class-name or not. */
8677 cp_parser_parse_tentatively (parser);
8678 /* Try a class-name. */
8679 type_decl = cp_parser_class_name (parser,
8680 /*typename_keyword_p=*/false,
8681 /*template_keyword_p=*/false,
8682 /*type_p=*/false,
8683 /*check_access_p=*/true,
8684 /*check_dependency_p=*/true,
8685 /*class_head_p=*/false);
8686 /* If it's not a class-name, keep looking. */
8687 if (!cp_parser_parse_definitely (parser))
8688 {
8689 /* It must be a typedef-name or an enum-name. */
8690 identifier = cp_parser_identifier (parser);
8691 if (identifier == error_mark_node)
8692 return error_mark_node;
8693
8694 /* Look up the type-name. */
8695 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8696 /* Issue an error if we did not find a type-name. */
8697 if (TREE_CODE (type_decl) != TYPE_DECL)
8698 {
8699 cp_parser_error (parser, "expected type-name");
8700 type_decl = error_mark_node;
8701 }
8702 /* Remember that the name was used in the definition of the
8703 current class so that we can check later to see if the
8704 meaning would have been different after the class was
8705 entirely defined. */
8706 else if (type_decl != error_mark_node
8707 && !parser->scope)
8708 maybe_note_name_used_in_class (identifier, type_decl);
8709 }
8710
8711 return type_decl;
8712}
8713
8714
8715/* Parse an elaborated-type-specifier. Note that the grammar given
8716 here incorporates the resolution to DR68.
8717
8718 elaborated-type-specifier:
8719 class-key :: [opt] nested-name-specifier [opt] identifier
8720 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8721 enum :: [opt] nested-name-specifier [opt] identifier
8722 typename :: [opt] nested-name-specifier identifier
8723 typename :: [opt] nested-name-specifier template [opt]
8724 template-id
8725
8726 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8727 declared `friend'. If IS_DECLARATION is TRUE, then this
8728 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8729 something is being declared.
8730
8731 Returns the TYPE specified. */
8732
8733static tree
94edc4ab
NN
8734cp_parser_elaborated_type_specifier (cp_parser* parser,
8735 bool is_friend,
8736 bool is_declaration)
a723baf1
MM
8737{
8738 enum tag_types tag_type;
8739 tree identifier;
8740 tree type = NULL_TREE;
8741
8742 /* See if we're looking at the `enum' keyword. */
8743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8744 {
8745 /* Consume the `enum' token. */
8746 cp_lexer_consume_token (parser->lexer);
8747 /* Remember that it's an enumeration type. */
8748 tag_type = enum_type;
8749 }
8750 /* Or, it might be `typename'. */
8751 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8752 RID_TYPENAME))
8753 {
8754 /* Consume the `typename' token. */
8755 cp_lexer_consume_token (parser->lexer);
8756 /* Remember that it's a `typename' type. */
8757 tag_type = typename_type;
8758 /* The `typename' keyword is only allowed in templates. */
8759 if (!processing_template_decl)
8760 pedwarn ("using `typename' outside of template");
8761 }
8762 /* Otherwise it must be a class-key. */
8763 else
8764 {
8765 tag_type = cp_parser_class_key (parser);
8766 if (tag_type == none_type)
8767 return error_mark_node;
8768 }
8769
8770 /* Look for the `::' operator. */
8771 cp_parser_global_scope_opt (parser,
8772 /*current_scope_valid_p=*/false);
8773 /* Look for the nested-name-specifier. */
8774 if (tag_type == typename_type)
8fa1ad0e
MM
8775 {
8776 if (cp_parser_nested_name_specifier (parser,
8777 /*typename_keyword_p=*/true,
8778 /*check_dependency_p=*/true,
8779 /*type_p=*/true)
8780 == error_mark_node)
8781 return error_mark_node;
8782 }
a723baf1
MM
8783 else
8784 /* Even though `typename' is not present, the proposed resolution
8785 to Core Issue 180 says that in `class A<T>::B', `B' should be
8786 considered a type-name, even if `A<T>' is dependent. */
8787 cp_parser_nested_name_specifier_opt (parser,
8788 /*typename_keyword_p=*/true,
8789 /*check_dependency_p=*/true,
8790 /*type_p=*/true);
8791 /* For everything but enumeration types, consider a template-id. */
8792 if (tag_type != enum_type)
8793 {
8794 bool template_p = false;
8795 tree decl;
8796
8797 /* Allow the `template' keyword. */
8798 template_p = cp_parser_optional_template_keyword (parser);
8799 /* If we didn't see `template', we don't know if there's a
8800 template-id or not. */
8801 if (!template_p)
8802 cp_parser_parse_tentatively (parser);
8803 /* Parse the template-id. */
8804 decl = cp_parser_template_id (parser, template_p,
8805 /*check_dependency_p=*/true);
8806 /* If we didn't find a template-id, look for an ordinary
8807 identifier. */
8808 if (!template_p && !cp_parser_parse_definitely (parser))
8809 ;
8810 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8811 in effect, then we must assume that, upon instantiation, the
8812 template will correspond to a class. */
8813 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8814 && tag_type == typename_type)
8815 type = make_typename_type (parser->scope, decl,
8816 /*complain=*/1);
8817 else
8818 type = TREE_TYPE (decl);
8819 }
8820
8821 /* For an enumeration type, consider only a plain identifier. */
8822 if (!type)
8823 {
8824 identifier = cp_parser_identifier (parser);
8825
8826 if (identifier == error_mark_node)
8827 return error_mark_node;
8828
8829 /* For a `typename', we needn't call xref_tag. */
8830 if (tag_type == typename_type)
8831 return make_typename_type (parser->scope, identifier,
8832 /*complain=*/1);
8833 /* Look up a qualified name in the usual way. */
8834 if (parser->scope)
8835 {
8836 tree decl;
8837
8838 /* In an elaborated-type-specifier, names are assumed to name
8839 types, so we set IS_TYPE to TRUE when calling
8840 cp_parser_lookup_name. */
8841 decl = cp_parser_lookup_name (parser, identifier,
8842 /*check_access=*/true,
8843 /*is_type=*/true,
eea9800f 8844 /*is_namespace=*/false,
a723baf1 8845 /*check_dependency=*/true);
710b73e6
KL
8846
8847 /* If we are parsing friend declaration, DECL may be a
8848 TEMPLATE_DECL tree node here. However, we need to check
8849 whether this TEMPLATE_DECL results in valid code. Consider
8850 the following example:
8851
8852 namespace N {
8853 template <class T> class C {};
8854 }
8855 class X {
8856 template <class T> friend class N::C; // #1, valid code
8857 };
8858 template <class T> class Y {
8859 friend class N::C; // #2, invalid code
8860 };
8861
8862 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8863 name lookup of `N::C'. We see that friend declaration must
8864 be template for the code to be valid. Note that
8865 processing_template_decl does not work here since it is
8866 always 1 for the above two cases. */
8867
a723baf1 8868 decl = (cp_parser_maybe_treat_template_as_class
710b73e6
KL
8869 (decl, /*tag_name_p=*/is_friend
8870 && parser->num_template_parameter_lists));
a723baf1
MM
8871
8872 if (TREE_CODE (decl) != TYPE_DECL)
8873 {
8874 error ("expected type-name");
8875 return error_mark_node;
8876 }
8877 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8878 && tag_type != enum_type)
8879 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8880 tag_type == record_type ? "struct" : "class");
8881 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8882 && tag_type == enum_type)
8883 error ("`%T' referred to as enum", TREE_TYPE (decl));
8884
8885 type = TREE_TYPE (decl);
8886 }
8887 else
8888 {
8889 /* An elaborated-type-specifier sometimes introduces a new type and
8890 sometimes names an existing type. Normally, the rule is that it
8891 introduces a new type only if there is not an existing type of
8892 the same name already in scope. For example, given:
8893
8894 struct S {};
8895 void f() { struct S s; }
8896
8897 the `struct S' in the body of `f' is the same `struct S' as in
8898 the global scope; the existing definition is used. However, if
8899 there were no global declaration, this would introduce a new
8900 local class named `S'.
8901
8902 An exception to this rule applies to the following code:
8903
8904 namespace N { struct S; }
8905
8906 Here, the elaborated-type-specifier names a new type
8907 unconditionally; even if there is already an `S' in the
8908 containing scope this declaration names a new type.
8909 This exception only applies if the elaborated-type-specifier
8910 forms the complete declaration:
8911
8912 [class.name]
8913
8914 A declaration consisting solely of `class-key identifier ;' is
8915 either a redeclaration of the name in the current scope or a
8916 forward declaration of the identifier as a class name. It
8917 introduces the name into the current scope.
8918
8919 We are in this situation precisely when the next token is a `;'.
8920
8921 An exception to the exception is that a `friend' declaration does
8922 *not* name a new type; i.e., given:
8923
8924 struct S { friend struct T; };
8925
8926 `T' is not a new type in the scope of `S'.
8927
8928 Also, `new struct S' or `sizeof (struct S)' never results in the
8929 definition of a new type; a new type can only be declared in a
8930 declaration context. */
8931
8932 type = xref_tag (tag_type, identifier,
8933 /*attributes=*/NULL_TREE,
8934 (is_friend
8935 || !is_declaration
8936 || cp_lexer_next_token_is_not (parser->lexer,
8937 CPP_SEMICOLON)));
8938 }
8939 }
8940 if (tag_type != enum_type)
8941 cp_parser_check_class_key (tag_type, type);
8942 return type;
8943}
8944
8945/* Parse an enum-specifier.
8946
8947 enum-specifier:
8948 enum identifier [opt] { enumerator-list [opt] }
8949
8950 Returns an ENUM_TYPE representing the enumeration. */
8951
8952static tree
94edc4ab 8953cp_parser_enum_specifier (cp_parser* parser)
a723baf1
MM
8954{
8955 cp_token *token;
8956 tree identifier = NULL_TREE;
8957 tree type;
8958
8959 /* Look for the `enum' keyword. */
8960 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8961 return error_mark_node;
8962 /* Peek at the next token. */
8963 token = cp_lexer_peek_token (parser->lexer);
8964
8965 /* See if it is an identifier. */
8966 if (token->type == CPP_NAME)
8967 identifier = cp_parser_identifier (parser);
8968
8969 /* Look for the `{'. */
8970 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8971 return error_mark_node;
8972
8973 /* At this point, we're going ahead with the enum-specifier, even
8974 if some other problem occurs. */
8975 cp_parser_commit_to_tentative_parse (parser);
8976
8977 /* Issue an error message if type-definitions are forbidden here. */
8978 cp_parser_check_type_definition (parser);
8979
8980 /* Create the new type. */
8981 type = start_enum (identifier ? identifier : make_anon_name ());
8982
8983 /* Peek at the next token. */
8984 token = cp_lexer_peek_token (parser->lexer);
8985 /* If it's not a `}', then there are some enumerators. */
8986 if (token->type != CPP_CLOSE_BRACE)
8987 cp_parser_enumerator_list (parser, type);
8988 /* Look for the `}'. */
8989 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8990
8991 /* Finish up the enumeration. */
8992 finish_enum (type);
8993
8994 return type;
8995}
8996
8997/* Parse an enumerator-list. The enumerators all have the indicated
8998 TYPE.
8999
9000 enumerator-list:
9001 enumerator-definition
9002 enumerator-list , enumerator-definition */
9003
9004static void
94edc4ab 9005cp_parser_enumerator_list (cp_parser* parser, tree type)
a723baf1
MM
9006{
9007 while (true)
9008 {
9009 cp_token *token;
9010
9011 /* Parse an enumerator-definition. */
9012 cp_parser_enumerator_definition (parser, type);
9013 /* Peek at the next token. */
9014 token = cp_lexer_peek_token (parser->lexer);
9015 /* If it's not a `,', then we've reached the end of the
9016 list. */
9017 if (token->type != CPP_COMMA)
9018 break;
9019 /* Otherwise, consume the `,' and keep going. */
9020 cp_lexer_consume_token (parser->lexer);
9021 /* If the next token is a `}', there is a trailing comma. */
9022 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9023 {
9024 if (pedantic && !in_system_header)
9025 pedwarn ("comma at end of enumerator list");
9026 break;
9027 }
9028 }
9029}
9030
9031/* Parse an enumerator-definition. The enumerator has the indicated
9032 TYPE.
9033
9034 enumerator-definition:
9035 enumerator
9036 enumerator = constant-expression
9037
9038 enumerator:
9039 identifier */
9040
9041static void
94edc4ab 9042cp_parser_enumerator_definition (cp_parser* parser, tree type)
a723baf1
MM
9043{
9044 cp_token *token;
9045 tree identifier;
9046 tree value;
9047
9048 /* Look for the identifier. */
9049 identifier = cp_parser_identifier (parser);
9050 if (identifier == error_mark_node)
9051 return;
9052
9053 /* Peek at the next token. */
9054 token = cp_lexer_peek_token (parser->lexer);
9055 /* If it's an `=', then there's an explicit value. */
9056 if (token->type == CPP_EQ)
9057 {
9058 /* Consume the `=' token. */
9059 cp_lexer_consume_token (parser->lexer);
9060 /* Parse the value. */
14d22dd6
MM
9061 value = cp_parser_constant_expression (parser,
9062 /*allow_non_constant=*/false,
9063 NULL);
a723baf1
MM
9064 }
9065 else
9066 value = NULL_TREE;
9067
9068 /* Create the enumerator. */
9069 build_enumerator (identifier, value, type);
9070}
9071
9072/* Parse a namespace-name.
9073
9074 namespace-name:
9075 original-namespace-name
9076 namespace-alias
9077
9078 Returns the NAMESPACE_DECL for the namespace. */
9079
9080static tree
94edc4ab 9081cp_parser_namespace_name (cp_parser* parser)
a723baf1
MM
9082{
9083 tree identifier;
9084 tree namespace_decl;
9085
9086 /* Get the name of the namespace. */
9087 identifier = cp_parser_identifier (parser);
9088 if (identifier == error_mark_node)
9089 return error_mark_node;
9090
eea9800f
MM
9091 /* Look up the identifier in the currently active scope. Look only
9092 for namespaces, due to:
9093
9094 [basic.lookup.udir]
9095
9096 When looking up a namespace-name in a using-directive or alias
9097 definition, only namespace names are considered.
9098
9099 And:
9100
9101 [basic.lookup.qual]
9102
9103 During the lookup of a name preceding the :: scope resolution
9104 operator, object, function, and enumerator names are ignored.
9105
9106 (Note that cp_parser_class_or_namespace_name only calls this
9107 function if the token after the name is the scope resolution
9108 operator.) */
9109 namespace_decl = cp_parser_lookup_name (parser, identifier,
9110 /*check_access=*/true,
9111 /*is_type=*/false,
9112 /*is_namespace=*/true,
9113 /*check_dependency=*/true);
a723baf1
MM
9114 /* If it's not a namespace, issue an error. */
9115 if (namespace_decl == error_mark_node
9116 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9117 {
9118 cp_parser_error (parser, "expected namespace-name");
9119 namespace_decl = error_mark_node;
9120 }
9121
9122 return namespace_decl;
9123}
9124
9125/* Parse a namespace-definition.
9126
9127 namespace-definition:
9128 named-namespace-definition
9129 unnamed-namespace-definition
9130
9131 named-namespace-definition:
9132 original-namespace-definition
9133 extension-namespace-definition
9134
9135 original-namespace-definition:
9136 namespace identifier { namespace-body }
9137
9138 extension-namespace-definition:
9139 namespace original-namespace-name { namespace-body }
9140
9141 unnamed-namespace-definition:
9142 namespace { namespace-body } */
9143
9144static void
94edc4ab 9145cp_parser_namespace_definition (cp_parser* parser)
a723baf1
MM
9146{
9147 tree identifier;
9148
9149 /* Look for the `namespace' keyword. */
9150 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9151
9152 /* Get the name of the namespace. We do not attempt to distinguish
9153 between an original-namespace-definition and an
9154 extension-namespace-definition at this point. The semantic
9155 analysis routines are responsible for that. */
9156 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9157 identifier = cp_parser_identifier (parser);
9158 else
9159 identifier = NULL_TREE;
9160
9161 /* Look for the `{' to start the namespace. */
9162 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9163 /* Start the namespace. */
9164 push_namespace (identifier);
9165 /* Parse the body of the namespace. */
9166 cp_parser_namespace_body (parser);
9167 /* Finish the namespace. */
9168 pop_namespace ();
9169 /* Look for the final `}'. */
9170 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9171}
9172
9173/* Parse a namespace-body.
9174
9175 namespace-body:
9176 declaration-seq [opt] */
9177
9178static void
94edc4ab 9179cp_parser_namespace_body (cp_parser* parser)
a723baf1
MM
9180{
9181 cp_parser_declaration_seq_opt (parser);
9182}
9183
9184/* Parse a namespace-alias-definition.
9185
9186 namespace-alias-definition:
9187 namespace identifier = qualified-namespace-specifier ; */
9188
9189static void
94edc4ab 9190cp_parser_namespace_alias_definition (cp_parser* parser)
a723baf1
MM
9191{
9192 tree identifier;
9193 tree namespace_specifier;
9194
9195 /* Look for the `namespace' keyword. */
9196 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9197 /* Look for the identifier. */
9198 identifier = cp_parser_identifier (parser);
9199 if (identifier == error_mark_node)
9200 return;
9201 /* Look for the `=' token. */
9202 cp_parser_require (parser, CPP_EQ, "`='");
9203 /* Look for the qualified-namespace-specifier. */
9204 namespace_specifier
9205 = cp_parser_qualified_namespace_specifier (parser);
9206 /* Look for the `;' token. */
9207 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9208
9209 /* Register the alias in the symbol table. */
9210 do_namespace_alias (identifier, namespace_specifier);
9211}
9212
9213/* Parse a qualified-namespace-specifier.
9214
9215 qualified-namespace-specifier:
9216 :: [opt] nested-name-specifier [opt] namespace-name
9217
9218 Returns a NAMESPACE_DECL corresponding to the specified
9219 namespace. */
9220
9221static tree
94edc4ab 9222cp_parser_qualified_namespace_specifier (cp_parser* parser)
a723baf1
MM
9223{
9224 /* Look for the optional `::'. */
9225 cp_parser_global_scope_opt (parser,
9226 /*current_scope_valid_p=*/false);
9227
9228 /* Look for the optional nested-name-specifier. */
9229 cp_parser_nested_name_specifier_opt (parser,
9230 /*typename_keyword_p=*/false,
9231 /*check_dependency_p=*/true,
9232 /*type_p=*/false);
9233
9234 return cp_parser_namespace_name (parser);
9235}
9236
9237/* Parse a using-declaration.
9238
9239 using-declaration:
9240 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9241 using :: unqualified-id ; */
9242
9243static void
94edc4ab 9244cp_parser_using_declaration (cp_parser* parser)
a723baf1
MM
9245{
9246 cp_token *token;
9247 bool typename_p = false;
9248 bool global_scope_p;
9249 tree decl;
9250 tree identifier;
9251 tree scope;
9252
9253 /* Look for the `using' keyword. */
9254 cp_parser_require_keyword (parser, RID_USING, "`using'");
9255
9256 /* Peek at the next token. */
9257 token = cp_lexer_peek_token (parser->lexer);
9258 /* See if it's `typename'. */
9259 if (token->keyword == RID_TYPENAME)
9260 {
9261 /* Remember that we've seen it. */
9262 typename_p = true;
9263 /* Consume the `typename' token. */
9264 cp_lexer_consume_token (parser->lexer);
9265 }
9266
9267 /* Look for the optional global scope qualification. */
9268 global_scope_p
9269 = (cp_parser_global_scope_opt (parser,
9270 /*current_scope_valid_p=*/false)
9271 != NULL_TREE);
9272
9273 /* If we saw `typename', or didn't see `::', then there must be a
9274 nested-name-specifier present. */
9275 if (typename_p || !global_scope_p)
9276 cp_parser_nested_name_specifier (parser, typename_p,
9277 /*check_dependency_p=*/true,
9278 /*type_p=*/false);
9279 /* Otherwise, we could be in either of the two productions. In that
9280 case, treat the nested-name-specifier as optional. */
9281 else
9282 cp_parser_nested_name_specifier_opt (parser,
9283 /*typename_keyword_p=*/false,
9284 /*check_dependency_p=*/true,
9285 /*type_p=*/false);
9286
9287 /* Parse the unqualified-id. */
9288 identifier = cp_parser_unqualified_id (parser,
9289 /*template_keyword_p=*/false,
9290 /*check_dependency_p=*/true);
9291
9292 /* The function we call to handle a using-declaration is different
9293 depending on what scope we are in. */
9294 scope = current_scope ();
9295 if (scope && TYPE_P (scope))
9296 {
9297 /* Create the USING_DECL. */
9298 decl = do_class_using_decl (build_nt (SCOPE_REF,
9299 parser->scope,
9300 identifier));
9301 /* Add it to the list of members in this class. */
9302 finish_member_declaration (decl);
9303 }
9304 else
9305 {
9306 decl = cp_parser_lookup_name_simple (parser, identifier);
4eb6d609
MM
9307 if (decl == error_mark_node)
9308 {
9309 if (parser->scope && parser->scope != global_namespace)
9310 error ("`%D::%D' has not been declared",
9311 parser->scope, identifier);
9312 else
9313 error ("`::%D' has not been declared", identifier);
9314 }
9315 else if (scope)
a723baf1
MM
9316 do_local_using_decl (decl);
9317 else
9318 do_toplevel_using_decl (decl);
9319 }
9320
9321 /* Look for the final `;'. */
9322 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9323}
9324
9325/* Parse a using-directive.
9326
9327 using-directive:
9328 using namespace :: [opt] nested-name-specifier [opt]
9329 namespace-name ; */
9330
9331static void
94edc4ab 9332cp_parser_using_directive (cp_parser* parser)
a723baf1
MM
9333{
9334 tree namespace_decl;
9335
9336 /* Look for the `using' keyword. */
9337 cp_parser_require_keyword (parser, RID_USING, "`using'");
9338 /* And the `namespace' keyword. */
9339 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9340 /* Look for the optional `::' operator. */
9341 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9342 /* And the optional nested-name-sepcifier. */
9343 cp_parser_nested_name_specifier_opt (parser,
9344 /*typename_keyword_p=*/false,
9345 /*check_dependency_p=*/true,
9346 /*type_p=*/false);
9347 /* Get the namespace being used. */
9348 namespace_decl = cp_parser_namespace_name (parser);
9349 /* Update the symbol table. */
9350 do_using_directive (namespace_decl);
9351 /* Look for the final `;'. */
9352 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9353}
9354
9355/* Parse an asm-definition.
9356
9357 asm-definition:
9358 asm ( string-literal ) ;
9359
9360 GNU Extension:
9361
9362 asm-definition:
9363 asm volatile [opt] ( string-literal ) ;
9364 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9365 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9366 : asm-operand-list [opt] ) ;
9367 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9368 : asm-operand-list [opt]
9369 : asm-operand-list [opt] ) ; */
9370
9371static void
94edc4ab 9372cp_parser_asm_definition (cp_parser* parser)
a723baf1
MM
9373{
9374 cp_token *token;
9375 tree string;
9376 tree outputs = NULL_TREE;
9377 tree inputs = NULL_TREE;
9378 tree clobbers = NULL_TREE;
9379 tree asm_stmt;
9380 bool volatile_p = false;
9381 bool extended_p = false;
9382
9383 /* Look for the `asm' keyword. */
9384 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9385 /* See if the next token is `volatile'. */
9386 if (cp_parser_allow_gnu_extensions_p (parser)
9387 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9388 {
9389 /* Remember that we saw the `volatile' keyword. */
9390 volatile_p = true;
9391 /* Consume the token. */
9392 cp_lexer_consume_token (parser->lexer);
9393 }
9394 /* Look for the opening `('. */
9395 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9396 /* Look for the string. */
9397 token = cp_parser_require (parser, CPP_STRING, "asm body");
9398 if (!token)
9399 return;
9400 string = token->value;
9401 /* If we're allowing GNU extensions, check for the extended assembly
9402 syntax. Unfortunately, the `:' tokens need not be separated by
9403 a space in C, and so, for compatibility, we tolerate that here
9404 too. Doing that means that we have to treat the `::' operator as
9405 two `:' tokens. */
9406 if (cp_parser_allow_gnu_extensions_p (parser)
9407 && at_function_scope_p ()
9408 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9409 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9410 {
9411 bool inputs_p = false;
9412 bool clobbers_p = false;
9413
9414 /* The extended syntax was used. */
9415 extended_p = true;
9416
9417 /* Look for outputs. */
9418 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9419 {
9420 /* Consume the `:'. */
9421 cp_lexer_consume_token (parser->lexer);
9422 /* Parse the output-operands. */
9423 if (cp_lexer_next_token_is_not (parser->lexer,
9424 CPP_COLON)
9425 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9426 CPP_SCOPE)
9427 && cp_lexer_next_token_is_not (parser->lexer,
9428 CPP_CLOSE_PAREN))
a723baf1
MM
9429 outputs = cp_parser_asm_operand_list (parser);
9430 }
9431 /* If the next token is `::', there are no outputs, and the
9432 next token is the beginning of the inputs. */
9433 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9434 {
9435 /* Consume the `::' token. */
9436 cp_lexer_consume_token (parser->lexer);
9437 /* The inputs are coming next. */
9438 inputs_p = true;
9439 }
9440
9441 /* Look for inputs. */
9442 if (inputs_p
9443 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9444 {
9445 if (!inputs_p)
9446 /* Consume the `:'. */
9447 cp_lexer_consume_token (parser->lexer);
9448 /* Parse the output-operands. */
9449 if (cp_lexer_next_token_is_not (parser->lexer,
9450 CPP_COLON)
9451 && cp_lexer_next_token_is_not (parser->lexer,
8caf4c38
MM
9452 CPP_SCOPE)
9453 && cp_lexer_next_token_is_not (parser->lexer,
9454 CPP_CLOSE_PAREN))
a723baf1
MM
9455 inputs = cp_parser_asm_operand_list (parser);
9456 }
9457 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9458 /* The clobbers are coming next. */
9459 clobbers_p = true;
9460
9461 /* Look for clobbers. */
9462 if (clobbers_p
9463 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9464 {
9465 if (!clobbers_p)
9466 /* Consume the `:'. */
9467 cp_lexer_consume_token (parser->lexer);
9468 /* Parse the clobbers. */
8caf4c38
MM
9469 if (cp_lexer_next_token_is_not (parser->lexer,
9470 CPP_CLOSE_PAREN))
9471 clobbers = cp_parser_asm_clobber_list (parser);
a723baf1
MM
9472 }
9473 }
9474 /* Look for the closing `)'. */
9475 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9476 cp_parser_skip_to_closing_parenthesis (parser);
9477 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9478
9479 /* Create the ASM_STMT. */
9480 if (at_function_scope_p ())
9481 {
9482 asm_stmt =
9483 finish_asm_stmt (volatile_p
9484 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9485 string, outputs, inputs, clobbers);
9486 /* If the extended syntax was not used, mark the ASM_STMT. */
9487 if (!extended_p)
9488 ASM_INPUT_P (asm_stmt) = 1;
9489 }
9490 else
9491 assemble_asm (string);
9492}
9493
9494/* Declarators [gram.dcl.decl] */
9495
9496/* Parse an init-declarator.
9497
9498 init-declarator:
9499 declarator initializer [opt]
9500
9501 GNU Extension:
9502
9503 init-declarator:
9504 declarator asm-specification [opt] attributes [opt] initializer [opt]
9505
9506 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
c8e4f0e9 9507 Returns a representation of the entity declared. If MEMBER_P is TRUE,
cf22909c
KL
9508 then this declarator appears in a class scope. The new DECL created
9509 by this declarator is returned.
a723baf1
MM
9510
9511 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9512 for a function-definition here as well. If the declarator is a
9513 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9514 be TRUE upon return. By that point, the function-definition will
9515 have been completely parsed.
9516
9517 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9518 is FALSE. */
9519
9520static tree
94edc4ab
NN
9521cp_parser_init_declarator (cp_parser* parser,
9522 tree decl_specifiers,
9523 tree prefix_attributes,
9524 bool function_definition_allowed_p,
9525 bool member_p,
9526 bool* function_definition_p)
a723baf1
MM
9527{
9528 cp_token *token;
9529 tree declarator;
9530 tree attributes;
9531 tree asm_specification;
9532 tree initializer;
9533 tree decl = NULL_TREE;
9534 tree scope;
a723baf1
MM
9535 bool is_initialized;
9536 bool is_parenthesized_init;
9537 bool ctor_dtor_or_conv_p;
9538 bool friend_p;
9539
9540 /* Assume that this is not the declarator for a function
9541 definition. */
9542 if (function_definition_p)
9543 *function_definition_p = false;
9544
9545 /* Defer access checks while parsing the declarator; we cannot know
9546 what names are accessible until we know what is being
9547 declared. */
cf22909c
KL
9548 resume_deferring_access_checks ();
9549
a723baf1
MM
9550 /* Parse the declarator. */
9551 declarator
62b8a44e 9552 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
9553 &ctor_dtor_or_conv_p);
9554 /* Gather up the deferred checks. */
cf22909c 9555 stop_deferring_access_checks ();
24c0ef37 9556
a723baf1
MM
9557 /* If the DECLARATOR was erroneous, there's no need to go
9558 further. */
9559 if (declarator == error_mark_node)
cf22909c 9560 return error_mark_node;
a723baf1
MM
9561
9562 /* Figure out what scope the entity declared by the DECLARATOR is
9563 located in. `grokdeclarator' sometimes changes the scope, so
9564 we compute it now. */
9565 scope = get_scope_of_declarator (declarator);
9566
9567 /* If we're allowing GNU extensions, look for an asm-specification
9568 and attributes. */
9569 if (cp_parser_allow_gnu_extensions_p (parser))
9570 {
9571 /* Look for an asm-specification. */
9572 asm_specification = cp_parser_asm_specification_opt (parser);
9573 /* And attributes. */
9574 attributes = cp_parser_attributes_opt (parser);
9575 }
9576 else
9577 {
9578 asm_specification = NULL_TREE;
9579 attributes = NULL_TREE;
9580 }
9581
9582 /* Peek at the next token. */
9583 token = cp_lexer_peek_token (parser->lexer);
9584 /* Check to see if the token indicates the start of a
9585 function-definition. */
9586 if (cp_parser_token_starts_function_definition_p (token))
9587 {
9588 if (!function_definition_allowed_p)
9589 {
9590 /* If a function-definition should not appear here, issue an
9591 error message. */
9592 cp_parser_error (parser,
9593 "a function-definition is not allowed here");
9594 return error_mark_node;
9595 }
9596 else
9597 {
a723baf1
MM
9598 /* Neither attributes nor an asm-specification are allowed
9599 on a function-definition. */
9600 if (asm_specification)
9601 error ("an asm-specification is not allowed on a function-definition");
9602 if (attributes)
9603 error ("attributes are not allowed on a function-definition");
9604 /* This is a function-definition. */
9605 *function_definition_p = true;
9606
a723baf1
MM
9607 /* Parse the function definition. */
9608 decl = (cp_parser_function_definition_from_specifiers_and_declarator
cf22909c 9609 (parser, decl_specifiers, prefix_attributes, declarator));
24c0ef37 9610
a723baf1
MM
9611 return decl;
9612 }
9613 }
9614
9615 /* [dcl.dcl]
9616
9617 Only in function declarations for constructors, destructors, and
9618 type conversions can the decl-specifier-seq be omitted.
9619
9620 We explicitly postpone this check past the point where we handle
9621 function-definitions because we tolerate function-definitions
9622 that are missing their return types in some modes. */
9623 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9624 {
9625 cp_parser_error (parser,
9626 "expected constructor, destructor, or type conversion");
9627 return error_mark_node;
9628 }
9629
9630 /* An `=' or an `(' indicates an initializer. */
9631 is_initialized = (token->type == CPP_EQ
9632 || token->type == CPP_OPEN_PAREN);
9633 /* If the init-declarator isn't initialized and isn't followed by a
9634 `,' or `;', it's not a valid init-declarator. */
9635 if (!is_initialized
9636 && token->type != CPP_COMMA
9637 && token->type != CPP_SEMICOLON)
9638 {
9639 cp_parser_error (parser, "expected init-declarator");
9640 return error_mark_node;
9641 }
9642
9643 /* Because start_decl has side-effects, we should only call it if we
9644 know we're going ahead. By this point, we know that we cannot
9645 possibly be looking at any other construct. */
9646 cp_parser_commit_to_tentative_parse (parser);
9647
9648 /* Check to see whether or not this declaration is a friend. */
9649 friend_p = cp_parser_friend_p (decl_specifiers);
9650
9651 /* Check that the number of template-parameter-lists is OK. */
9652 if (!cp_parser_check_declarator_template_parameters (parser,
9653 declarator))
cf22909c 9654 return error_mark_node;
a723baf1
MM
9655
9656 /* Enter the newly declared entry in the symbol table. If we're
9657 processing a declaration in a class-specifier, we wait until
9658 after processing the initializer. */
9659 if (!member_p)
9660 {
9661 if (parser->in_unbraced_linkage_specification_p)
9662 {
9663 decl_specifiers = tree_cons (error_mark_node,
9664 get_identifier ("extern"),
9665 decl_specifiers);
9666 have_extern_spec = false;
9667 }
9668 decl = start_decl (declarator,
9669 decl_specifiers,
9670 is_initialized,
9671 attributes,
9672 prefix_attributes);
9673 }
9674
9675 /* Enter the SCOPE. That way unqualified names appearing in the
9676 initializer will be looked up in SCOPE. */
9677 if (scope)
9678 push_scope (scope);
9679
9680 /* Perform deferred access control checks, now that we know in which
9681 SCOPE the declared entity resides. */
9682 if (!member_p && decl)
9683 {
9684 tree saved_current_function_decl = NULL_TREE;
9685
9686 /* If the entity being declared is a function, pretend that we
9687 are in its scope. If it is a `friend', it may have access to
9688 things that would not otherwise be accessible. */
9689 if (TREE_CODE (decl) == FUNCTION_DECL)
9690 {
9691 saved_current_function_decl = current_function_decl;
9692 current_function_decl = decl;
9693 }
9694
cf22909c
KL
9695 /* Perform the access control checks for the declarator and the
9696 the decl-specifiers. */
9697 perform_deferred_access_checks ();
a723baf1
MM
9698
9699 /* Restore the saved value. */
9700 if (TREE_CODE (decl) == FUNCTION_DECL)
9701 current_function_decl = saved_current_function_decl;
9702 }
9703
9704 /* Parse the initializer. */
9705 if (is_initialized)
704a0bbd 9706 initializer = cp_parser_initializer (parser, &is_parenthesized_init);
a723baf1
MM
9707 else
9708 {
9709 initializer = NULL_TREE;
9710 is_parenthesized_init = false;
9711 }
9712
9713 /* The old parser allows attributes to appear after a parenthesized
9714 initializer. Mark Mitchell proposed removing this functionality
9715 on the GCC mailing lists on 2002-08-13. This parser accepts the
9716 attributes -- but ignores them. */
9717 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9718 if (cp_parser_attributes_opt (parser))
9719 warning ("attributes after parenthesized initializer ignored");
9720
9721 /* Leave the SCOPE, now that we have processed the initializer. It
9722 is important to do this before calling cp_finish_decl because it
9723 makes decisions about whether to create DECL_STMTs or not based
9724 on the current scope. */
9725 if (scope)
9726 pop_scope (scope);
9727
9728 /* For an in-class declaration, use `grokfield' to create the
9729 declaration. */
9730 if (member_p)
9731 decl = grokfield (declarator, decl_specifiers,
9732 initializer, /*asmspec=*/NULL_TREE,
9733 /*attributes=*/NULL_TREE);
9734
9735 /* Finish processing the declaration. But, skip friend
9736 declarations. */
9737 if (!friend_p && decl)
9738 cp_finish_decl (decl,
9739 initializer,
9740 asm_specification,
9741 /* If the initializer is in parentheses, then this is
9742 a direct-initialization, which means that an
9743 `explicit' constructor is OK. Otherwise, an
9744 `explicit' constructor cannot be used. */
9745 ((is_parenthesized_init || !is_initialized)
9746 ? 0 : LOOKUP_ONLYCONVERTING));
9747
9748 return decl;
9749}
9750
9751/* Parse a declarator.
9752
9753 declarator:
9754 direct-declarator
9755 ptr-operator declarator
9756
9757 abstract-declarator:
9758 ptr-operator abstract-declarator [opt]
9759 direct-abstract-declarator
9760
9761 GNU Extensions:
9762
9763 declarator:
9764 attributes [opt] direct-declarator
9765 attributes [opt] ptr-operator declarator
9766
9767 abstract-declarator:
9768 attributes [opt] ptr-operator abstract-declarator [opt]
9769 attributes [opt] direct-abstract-declarator
9770
9771 Returns a representation of the declarator. If the declarator has
9772 the form `* declarator', then an INDIRECT_REF is returned, whose
9773 only operand is the sub-declarator. Analagously, `& declarator' is
9774 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9775 used. The first operand is the TYPE for `X'. The second operand
9776 is an INDIRECT_REF whose operand is the sub-declarator.
9777
9778 Otherwise, the reprsentation is as for a direct-declarator.
9779
9780 (It would be better to define a structure type to represent
9781 declarators, rather than abusing `tree' nodes to represent
9782 declarators. That would be much clearer and save some memory.
9783 There is no reason for declarators to be garbage-collected, for
9784 example; they are created during parser and no longer needed after
9785 `grokdeclarator' has been called.)
9786
9787 For a ptr-operator that has the optional cv-qualifier-seq,
9788 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9789 node.
9790
9791 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9792 true if this declarator represents a constructor, destructor, or
9793 type conversion operator. Otherwise, it is set to false.
9794
9795 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9796 a decl-specifier-seq unless it declares a constructor, destructor,
9797 or conversion. It might seem that we could check this condition in
9798 semantic analysis, rather than parsing, but that makes it difficult
9799 to handle something like `f()'. We want to notice that there are
9800 no decl-specifiers, and therefore realize that this is an
9801 expression, not a declaration.) */
9802
9803static tree
94edc4ab
NN
9804cp_parser_declarator (cp_parser* parser,
9805 cp_parser_declarator_kind dcl_kind,
9806 bool* ctor_dtor_or_conv_p)
a723baf1
MM
9807{
9808 cp_token *token;
9809 tree declarator;
9810 enum tree_code code;
9811 tree cv_qualifier_seq;
9812 tree class_type;
9813 tree attributes = NULL_TREE;
9814
9815 /* Assume this is not a constructor, destructor, or type-conversion
9816 operator. */
9817 if (ctor_dtor_or_conv_p)
9818 *ctor_dtor_or_conv_p = false;
9819
9820 if (cp_parser_allow_gnu_extensions_p (parser))
9821 attributes = cp_parser_attributes_opt (parser);
9822
9823 /* Peek at the next token. */
9824 token = cp_lexer_peek_token (parser->lexer);
9825
9826 /* Check for the ptr-operator production. */
9827 cp_parser_parse_tentatively (parser);
9828 /* Parse the ptr-operator. */
9829 code = cp_parser_ptr_operator (parser,
9830 &class_type,
9831 &cv_qualifier_seq);
9832 /* If that worked, then we have a ptr-operator. */
9833 if (cp_parser_parse_definitely (parser))
9834 {
9835 /* The dependent declarator is optional if we are parsing an
9836 abstract-declarator. */
62b8a44e 9837 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1
MM
9838 cp_parser_parse_tentatively (parser);
9839
9840 /* Parse the dependent declarator. */
62b8a44e 9841 declarator = cp_parser_declarator (parser, dcl_kind,
a723baf1
MM
9842 /*ctor_dtor_or_conv_p=*/NULL);
9843
9844 /* If we are parsing an abstract-declarator, we must handle the
9845 case where the dependent declarator is absent. */
62b8a44e
NS
9846 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9847 && !cp_parser_parse_definitely (parser))
a723baf1
MM
9848 declarator = NULL_TREE;
9849
9850 /* Build the representation of the ptr-operator. */
9851 if (code == INDIRECT_REF)
9852 declarator = make_pointer_declarator (cv_qualifier_seq,
9853 declarator);
9854 else
9855 declarator = make_reference_declarator (cv_qualifier_seq,
9856 declarator);
9857 /* Handle the pointer-to-member case. */
9858 if (class_type)
9859 declarator = build_nt (SCOPE_REF, class_type, declarator);
9860 }
9861 /* Everything else is a direct-declarator. */
9862 else
9863 declarator = cp_parser_direct_declarator (parser,
62b8a44e 9864 dcl_kind,
a723baf1
MM
9865 ctor_dtor_or_conv_p);
9866
9867 if (attributes && declarator != error_mark_node)
9868 declarator = tree_cons (attributes, declarator, NULL_TREE);
9869
9870 return declarator;
9871}
9872
9873/* Parse a direct-declarator or direct-abstract-declarator.
9874
9875 direct-declarator:
9876 declarator-id
9877 direct-declarator ( parameter-declaration-clause )
9878 cv-qualifier-seq [opt]
9879 exception-specification [opt]
9880 direct-declarator [ constant-expression [opt] ]
9881 ( declarator )
9882
9883 direct-abstract-declarator:
9884 direct-abstract-declarator [opt]
9885 ( parameter-declaration-clause )
9886 cv-qualifier-seq [opt]
9887 exception-specification [opt]
9888 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9889 ( abstract-declarator )
9890
62b8a44e
NS
9891 Returns a representation of the declarator. DCL_KIND is
9892 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9893 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9894 we are parsing a direct-declarator. It is
9895 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9896 of ambiguity we prefer an abstract declarator, as per
9897 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
a723baf1
MM
9898 cp_parser_declarator.
9899
9900 For the declarator-id production, the representation is as for an
9901 id-expression, except that a qualified name is represented as a
9902 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9903 see the documentation of the FUNCTION_DECLARATOR_* macros for
9904 information about how to find the various declarator components.
9905 An array-declarator is represented as an ARRAY_REF. The
9906 direct-declarator is the first operand; the constant-expression
9907 indicating the size of the array is the second operand. */
9908
9909static tree
94edc4ab
NN
9910cp_parser_direct_declarator (cp_parser* parser,
9911 cp_parser_declarator_kind dcl_kind,
9912 bool* ctor_dtor_or_conv_p)
a723baf1
MM
9913{
9914 cp_token *token;
62b8a44e 9915 tree declarator = NULL_TREE;
a723baf1
MM
9916 tree scope = NULL_TREE;
9917 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9918 bool saved_in_declarator_p = parser->in_declarator_p;
62b8a44e
NS
9919 bool first = true;
9920
9921 while (true)
a723baf1 9922 {
62b8a44e
NS
9923 /* Peek at the next token. */
9924 token = cp_lexer_peek_token (parser->lexer);
9925 if (token->type == CPP_OPEN_PAREN)
a723baf1 9926 {
62b8a44e
NS
9927 /* This is either a parameter-declaration-clause, or a
9928 parenthesized declarator. When we know we are parsing a
2050a1bb 9929 named declarator, it must be a paranthesized declarator
62b8a44e
NS
9930 if FIRST is true. For instance, `(int)' is a
9931 parameter-declaration-clause, with an omitted
9932 direct-abstract-declarator. But `((*))', is a
9933 parenthesized abstract declarator. Finally, when T is a
9934 template parameter `(T)' is a
9935 paremeter-declaration-clause, and not a parenthesized
9936 named declarator.
a723baf1 9937
62b8a44e
NS
9938 We first try and parse a parameter-declaration-clause,
9939 and then try a nested declarator (if FIRST is true).
a723baf1 9940
62b8a44e
NS
9941 It is not an error for it not to be a
9942 parameter-declaration-clause, even when FIRST is
9943 false. Consider,
9944
9945 int i (int);
9946 int i (3);
9947
9948 The first is the declaration of a function while the
9949 second is a the definition of a variable, including its
9950 initializer.
9951
9952 Having seen only the parenthesis, we cannot know which of
9953 these two alternatives should be selected. Even more
9954 complex are examples like:
9955
9956 int i (int (a));
9957 int i (int (3));
9958
9959 The former is a function-declaration; the latter is a
9960 variable initialization.
9961
9962 Thus again, we try a parameter-declation-clause, and if
9963 that fails, we back out and return. */
9964
9965 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
a723baf1 9966 {
62b8a44e
NS
9967 tree params;
9968
9969 cp_parser_parse_tentatively (parser);
a723baf1 9970
62b8a44e
NS
9971 /* Consume the `('. */
9972 cp_lexer_consume_token (parser->lexer);
9973 if (first)
9974 {
9975 /* If this is going to be an abstract declarator, we're
9976 in a declarator and we can't have default args. */
9977 parser->default_arg_ok_p = false;
9978 parser->in_declarator_p = true;
9979 }
9980
9981 /* Parse the parameter-declaration-clause. */
9982 params = cp_parser_parameter_declaration_clause (parser);
9983
9984 /* If all went well, parse the cv-qualifier-seq and the
9985 exception-specfication. */
9986 if (cp_parser_parse_definitely (parser))
9987 {
9988 tree cv_qualifiers;
9989 tree exception_specification;
9990
9991 first = false;
9992 /* Consume the `)'. */
9993 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9994
9995 /* Parse the cv-qualifier-seq. */
9996 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9997 /* And the exception-specification. */
9998 exception_specification
9999 = cp_parser_exception_specification_opt (parser);
10000
10001 /* Create the function-declarator. */
10002 declarator = make_call_declarator (declarator,
10003 params,
10004 cv_qualifiers,
10005 exception_specification);
10006 /* Any subsequent parameter lists are to do with
10007 return type, so are not those of the declared
10008 function. */
10009 parser->default_arg_ok_p = false;
10010
10011 /* Repeat the main loop. */
10012 continue;
10013 }
10014 }
10015
10016 /* If this is the first, we can try a parenthesized
10017 declarator. */
10018 if (first)
a723baf1 10019 {
a723baf1 10020 parser->default_arg_ok_p = saved_default_arg_ok_p;
62b8a44e
NS
10021 parser->in_declarator_p = saved_in_declarator_p;
10022
10023 /* Consume the `('. */
10024 cp_lexer_consume_token (parser->lexer);
10025 /* Parse the nested declarator. */
10026 declarator
10027 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
10028 first = false;
10029 /* Expect a `)'. */
10030 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10031 declarator = error_mark_node;
10032 if (declarator == error_mark_node)
10033 break;
10034
10035 goto handle_declarator;
a723baf1 10036 }
62b8a44e
NS
10037 /* Otherwise, we must be done. */
10038 else
10039 break;
a723baf1 10040 }
62b8a44e
NS
10041 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10042 && token->type == CPP_OPEN_SQUARE)
a723baf1 10043 {
62b8a44e 10044 /* Parse an array-declarator. */
a723baf1
MM
10045 tree bounds;
10046
62b8a44e
NS
10047 first = false;
10048 parser->default_arg_ok_p = false;
10049 parser->in_declarator_p = true;
a723baf1
MM
10050 /* Consume the `['. */
10051 cp_lexer_consume_token (parser->lexer);
10052 /* Peek at the next token. */
10053 token = cp_lexer_peek_token (parser->lexer);
10054 /* If the next token is `]', then there is no
10055 constant-expression. */
10056 if (token->type != CPP_CLOSE_SQUARE)
14d22dd6
MM
10057 {
10058 bool non_constant_p;
10059
10060 bounds
10061 = cp_parser_constant_expression (parser,
10062 /*allow_non_constant=*/true,
10063 &non_constant_p);
10064 /* If we're in a template, but the constant-expression
10065 isn't value dependent, simplify it. We're supposed
10066 to treat:
10067
10068 template <typename T> void f(T[1 + 1]);
10069 template <typename T> void f(T[2]);
10070
10071 as two declarations of the same function, for
10072 example. */
10073 if (processing_template_decl
10074 && !non_constant_p
10075 && !value_dependent_expression_p (bounds))
10076 {
10077 HOST_WIDE_INT saved_processing_template_decl;
10078
10079 saved_processing_template_decl = processing_template_decl;
10080 processing_template_decl = 0;
10081 bounds = build_expr_from_tree (bounds);
10082 processing_template_decl = saved_processing_template_decl;
10083 }
10084 }
a723baf1
MM
10085 else
10086 bounds = NULL_TREE;
10087 /* Look for the closing `]'. */
62b8a44e
NS
10088 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10089 {
10090 declarator = error_mark_node;
10091 break;
10092 }
a723baf1
MM
10093
10094 declarator = build_nt (ARRAY_REF, declarator, bounds);
10095 }
62b8a44e 10096 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
a723baf1 10097 {
62b8a44e
NS
10098 /* Parse a declarator_id */
10099 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10100 cp_parser_parse_tentatively (parser);
10101 declarator = cp_parser_declarator_id (parser);
712becab
NS
10102 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10103 {
10104 if (!cp_parser_parse_definitely (parser))
10105 declarator = error_mark_node;
10106 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10107 {
10108 cp_parser_error (parser, "expected unqualified-id");
10109 declarator = error_mark_node;
10110 }
10111 }
10112
62b8a44e
NS
10113 if (declarator == error_mark_node)
10114 break;
a723baf1 10115
62b8a44e
NS
10116 if (TREE_CODE (declarator) == SCOPE_REF)
10117 {
10118 tree scope = TREE_OPERAND (declarator, 0);
712becab 10119
62b8a44e
NS
10120 /* In the declaration of a member of a template class
10121 outside of the class itself, the SCOPE will sometimes
10122 be a TYPENAME_TYPE. For example, given:
10123
10124 template <typename T>
10125 int S<T>::R::i = 3;
10126
10127 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
10128 this context, we must resolve S<T>::R to an ordinary
10129 type, rather than a typename type.
10130
10131 The reason we normally avoid resolving TYPENAME_TYPEs
10132 is that a specialization of `S' might render
10133 `S<T>::R' not a type. However, if `S' is
10134 specialized, then this `i' will not be used, so there
10135 is no harm in resolving the types here. */
10136 if (TREE_CODE (scope) == TYPENAME_TYPE)
10137 {
14d22dd6
MM
10138 tree type;
10139
62b8a44e 10140 /* Resolve the TYPENAME_TYPE. */
14d22dd6
MM
10141 type = resolve_typename_type (scope,
10142 /*only_current_p=*/false);
62b8a44e 10143 /* If that failed, the declarator is invalid. */
14d22dd6
MM
10144 if (type != error_mark_node)
10145 scope = type;
62b8a44e
NS
10146 /* Build a new DECLARATOR. */
10147 declarator = build_nt (SCOPE_REF,
10148 scope,
10149 TREE_OPERAND (declarator, 1));
10150 }
10151 }
10152
10153 /* Check to see whether the declarator-id names a constructor,
10154 destructor, or conversion. */
10155 if (declarator && ctor_dtor_or_conv_p
10156 && ((TREE_CODE (declarator) == SCOPE_REF
10157 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10158 || (TREE_CODE (declarator) != SCOPE_REF
10159 && at_class_scope_p ())))
a723baf1 10160 {
62b8a44e
NS
10161 tree unqualified_name;
10162 tree class_type;
10163
10164 /* Get the unqualified part of the name. */
10165 if (TREE_CODE (declarator) == SCOPE_REF)
10166 {
10167 class_type = TREE_OPERAND (declarator, 0);
10168 unqualified_name = TREE_OPERAND (declarator, 1);
10169 }
10170 else
10171 {
10172 class_type = current_class_type;
10173 unqualified_name = declarator;
10174 }
10175
10176 /* See if it names ctor, dtor or conv. */
10177 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10178 || IDENTIFIER_TYPENAME_P (unqualified_name)
10179 || constructor_name_p (unqualified_name, class_type))
10180 *ctor_dtor_or_conv_p = true;
a723baf1 10181 }
62b8a44e
NS
10182
10183 handle_declarator:;
10184 scope = get_scope_of_declarator (declarator);
10185 if (scope)
10186 /* Any names that appear after the declarator-id for a member
10187 are looked up in the containing scope. */
10188 push_scope (scope);
10189 parser->in_declarator_p = true;
10190 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10191 || (declarator
10192 && (TREE_CODE (declarator) == SCOPE_REF
10193 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10194 /* Default args are only allowed on function
10195 declarations. */
10196 parser->default_arg_ok_p = saved_default_arg_ok_p;
a723baf1 10197 else
62b8a44e
NS
10198 parser->default_arg_ok_p = false;
10199
10200 first = false;
a723baf1 10201 }
62b8a44e 10202 /* We're done. */
a723baf1
MM
10203 else
10204 break;
a723baf1
MM
10205 }
10206
10207 /* For an abstract declarator, we might wind up with nothing at this
10208 point. That's an error; the declarator is not optional. */
10209 if (!declarator)
10210 cp_parser_error (parser, "expected declarator");
10211
10212 /* If we entered a scope, we must exit it now. */
10213 if (scope)
10214 pop_scope (scope);
10215
10216 parser->default_arg_ok_p = saved_default_arg_ok_p;
10217 parser->in_declarator_p = saved_in_declarator_p;
10218
10219 return declarator;
10220}
10221
10222/* Parse a ptr-operator.
10223
10224 ptr-operator:
10225 * cv-qualifier-seq [opt]
10226 &
10227 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10228
10229 GNU Extension:
10230
10231 ptr-operator:
10232 & cv-qualifier-seq [opt]
10233
10234 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10235 used. Returns ADDR_EXPR if a reference was used. In the
10236 case of a pointer-to-member, *TYPE is filled in with the
10237 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10238 with the cv-qualifier-seq, or NULL_TREE, if there are no
10239 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10240
10241static enum tree_code
94edc4ab
NN
10242cp_parser_ptr_operator (cp_parser* parser,
10243 tree* type,
10244 tree* cv_qualifier_seq)
a723baf1
MM
10245{
10246 enum tree_code code = ERROR_MARK;
10247 cp_token *token;
10248
10249 /* Assume that it's not a pointer-to-member. */
10250 *type = NULL_TREE;
10251 /* And that there are no cv-qualifiers. */
10252 *cv_qualifier_seq = NULL_TREE;
10253
10254 /* Peek at the next token. */
10255 token = cp_lexer_peek_token (parser->lexer);
10256 /* If it's a `*' or `&' we have a pointer or reference. */
10257 if (token->type == CPP_MULT || token->type == CPP_AND)
10258 {
10259 /* Remember which ptr-operator we were processing. */
10260 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10261
10262 /* Consume the `*' or `&'. */
10263 cp_lexer_consume_token (parser->lexer);
10264
10265 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10266 `&', if we are allowing GNU extensions. (The only qualifier
10267 that can legally appear after `&' is `restrict', but that is
10268 enforced during semantic analysis. */
10269 if (code == INDIRECT_REF
10270 || cp_parser_allow_gnu_extensions_p (parser))
10271 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10272 }
10273 else
10274 {
10275 /* Try the pointer-to-member case. */
10276 cp_parser_parse_tentatively (parser);
10277 /* Look for the optional `::' operator. */
10278 cp_parser_global_scope_opt (parser,
10279 /*current_scope_valid_p=*/false);
10280 /* Look for the nested-name specifier. */
10281 cp_parser_nested_name_specifier (parser,
10282 /*typename_keyword_p=*/false,
10283 /*check_dependency_p=*/true,
10284 /*type_p=*/false);
10285 /* If we found it, and the next token is a `*', then we are
10286 indeed looking at a pointer-to-member operator. */
10287 if (!cp_parser_error_occurred (parser)
10288 && cp_parser_require (parser, CPP_MULT, "`*'"))
10289 {
10290 /* The type of which the member is a member is given by the
10291 current SCOPE. */
10292 *type = parser->scope;
10293 /* The next name will not be qualified. */
10294 parser->scope = NULL_TREE;
10295 parser->qualifying_scope = NULL_TREE;
10296 parser->object_scope = NULL_TREE;
10297 /* Indicate that the `*' operator was used. */
10298 code = INDIRECT_REF;
10299 /* Look for the optional cv-qualifier-seq. */
10300 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10301 }
10302 /* If that didn't work we don't have a ptr-operator. */
10303 if (!cp_parser_parse_definitely (parser))
10304 cp_parser_error (parser, "expected ptr-operator");
10305 }
10306
10307 return code;
10308}
10309
10310/* Parse an (optional) cv-qualifier-seq.
10311
10312 cv-qualifier-seq:
10313 cv-qualifier cv-qualifier-seq [opt]
10314
10315 Returns a TREE_LIST. The TREE_VALUE of each node is the
10316 representation of a cv-qualifier. */
10317
10318static tree
94edc4ab 10319cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
a723baf1
MM
10320{
10321 tree cv_qualifiers = NULL_TREE;
10322
10323 while (true)
10324 {
10325 tree cv_qualifier;
10326
10327 /* Look for the next cv-qualifier. */
10328 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10329 /* If we didn't find one, we're done. */
10330 if (!cv_qualifier)
10331 break;
10332
10333 /* Add this cv-qualifier to the list. */
10334 cv_qualifiers
10335 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10336 }
10337
10338 /* We built up the list in reverse order. */
10339 return nreverse (cv_qualifiers);
10340}
10341
10342/* Parse an (optional) cv-qualifier.
10343
10344 cv-qualifier:
10345 const
10346 volatile
10347
10348 GNU Extension:
10349
10350 cv-qualifier:
10351 __restrict__ */
10352
10353static tree
94edc4ab 10354cp_parser_cv_qualifier_opt (cp_parser* parser)
a723baf1
MM
10355{
10356 cp_token *token;
10357 tree cv_qualifier = NULL_TREE;
10358
10359 /* Peek at the next token. */
10360 token = cp_lexer_peek_token (parser->lexer);
10361 /* See if it's a cv-qualifier. */
10362 switch (token->keyword)
10363 {
10364 case RID_CONST:
10365 case RID_VOLATILE:
10366 case RID_RESTRICT:
10367 /* Save the value of the token. */
10368 cv_qualifier = token->value;
10369 /* Consume the token. */
10370 cp_lexer_consume_token (parser->lexer);
10371 break;
10372
10373 default:
10374 break;
10375 }
10376
10377 return cv_qualifier;
10378}
10379
10380/* Parse a declarator-id.
10381
10382 declarator-id:
10383 id-expression
10384 :: [opt] nested-name-specifier [opt] type-name
10385
10386 In the `id-expression' case, the value returned is as for
10387 cp_parser_id_expression if the id-expression was an unqualified-id.
10388 If the id-expression was a qualified-id, then a SCOPE_REF is
10389 returned. The first operand is the scope (either a NAMESPACE_DECL
10390 or TREE_TYPE), but the second is still just a representation of an
10391 unqualified-id. */
10392
10393static tree
94edc4ab 10394cp_parser_declarator_id (cp_parser* parser)
a723baf1
MM
10395{
10396 tree id_expression;
10397
10398 /* The expression must be an id-expression. Assume that qualified
10399 names are the names of types so that:
10400
10401 template <class T>
10402 int S<T>::R::i = 3;
10403
10404 will work; we must treat `S<T>::R' as the name of a type.
10405 Similarly, assume that qualified names are templates, where
10406 required, so that:
10407
10408 template <class T>
10409 int S<T>::R<T>::i = 3;
10410
10411 will work, too. */
10412 id_expression = cp_parser_id_expression (parser,
10413 /*template_keyword_p=*/false,
10414 /*check_dependency_p=*/false,
10415 /*template_p=*/NULL);
10416 /* If the name was qualified, create a SCOPE_REF to represent
10417 that. */
10418 if (parser->scope)
10419 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10420
10421 return id_expression;
10422}
10423
10424/* Parse a type-id.
10425
10426 type-id:
10427 type-specifier-seq abstract-declarator [opt]
10428
10429 Returns the TYPE specified. */
10430
10431static tree
94edc4ab 10432cp_parser_type_id (cp_parser* parser)
a723baf1
MM
10433{
10434 tree type_specifier_seq;
10435 tree abstract_declarator;
10436
10437 /* Parse the type-specifier-seq. */
10438 type_specifier_seq
10439 = cp_parser_type_specifier_seq (parser);
10440 if (type_specifier_seq == error_mark_node)
10441 return error_mark_node;
10442
10443 /* There might or might not be an abstract declarator. */
10444 cp_parser_parse_tentatively (parser);
10445 /* Look for the declarator. */
10446 abstract_declarator
62b8a44e 10447 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
a723baf1
MM
10448 /* Check to see if there really was a declarator. */
10449 if (!cp_parser_parse_definitely (parser))
10450 abstract_declarator = NULL_TREE;
10451
10452 return groktypename (build_tree_list (type_specifier_seq,
10453 abstract_declarator));
10454}
10455
10456/* Parse a type-specifier-seq.
10457
10458 type-specifier-seq:
10459 type-specifier type-specifier-seq [opt]
10460
10461 GNU extension:
10462
10463 type-specifier-seq:
10464 attributes type-specifier-seq [opt]
10465
10466 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10467 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10468
10469static tree
94edc4ab 10470cp_parser_type_specifier_seq (cp_parser* parser)
a723baf1
MM
10471{
10472 bool seen_type_specifier = false;
10473 tree type_specifier_seq = NULL_TREE;
10474
10475 /* Parse the type-specifiers and attributes. */
10476 while (true)
10477 {
10478 tree type_specifier;
10479
10480 /* Check for attributes first. */
10481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10482 {
10483 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10484 NULL_TREE,
10485 type_specifier_seq);
10486 continue;
10487 }
10488
10489 /* After the first type-specifier, others are optional. */
10490 if (seen_type_specifier)
10491 cp_parser_parse_tentatively (parser);
10492 /* Look for the type-specifier. */
10493 type_specifier = cp_parser_type_specifier (parser,
10494 CP_PARSER_FLAGS_NONE,
10495 /*is_friend=*/false,
10496 /*is_declaration=*/false,
10497 NULL,
10498 NULL);
10499 /* If the first type-specifier could not be found, this is not a
10500 type-specifier-seq at all. */
10501 if (!seen_type_specifier && type_specifier == error_mark_node)
10502 return error_mark_node;
10503 /* If subsequent type-specifiers could not be found, the
10504 type-specifier-seq is complete. */
10505 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10506 break;
10507
10508 /* Add the new type-specifier to the list. */
10509 type_specifier_seq
10510 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10511 seen_type_specifier = true;
10512 }
10513
10514 /* We built up the list in reverse order. */
10515 return nreverse (type_specifier_seq);
10516}
10517
10518/* Parse a parameter-declaration-clause.
10519
10520 parameter-declaration-clause:
10521 parameter-declaration-list [opt] ... [opt]
10522 parameter-declaration-list , ...
10523
10524 Returns a representation for the parameter declarations. Each node
10525 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10526 representation.) If the parameter-declaration-clause ends with an
10527 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10528 list. A return value of NULL_TREE indicates a
10529 parameter-declaration-clause consisting only of an ellipsis. */
10530
10531static tree
94edc4ab 10532cp_parser_parameter_declaration_clause (cp_parser* parser)
a723baf1
MM
10533{
10534 tree parameters;
10535 cp_token *token;
10536 bool ellipsis_p;
10537
10538 /* Peek at the next token. */
10539 token = cp_lexer_peek_token (parser->lexer);
10540 /* Check for trivial parameter-declaration-clauses. */
10541 if (token->type == CPP_ELLIPSIS)
10542 {
10543 /* Consume the `...' token. */
10544 cp_lexer_consume_token (parser->lexer);
10545 return NULL_TREE;
10546 }
10547 else if (token->type == CPP_CLOSE_PAREN)
10548 /* There are no parameters. */
c73aecdf
DE
10549 {
10550#ifndef NO_IMPLICIT_EXTERN_C
10551 if (in_system_header && current_class_type == NULL
10552 && current_lang_name == lang_name_c)
10553 return NULL_TREE;
10554 else
10555#endif
10556 return void_list_node;
10557 }
a723baf1
MM
10558 /* Check for `(void)', too, which is a special case. */
10559 else if (token->keyword == RID_VOID
10560 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10561 == CPP_CLOSE_PAREN))
10562 {
10563 /* Consume the `void' token. */
10564 cp_lexer_consume_token (parser->lexer);
10565 /* There are no parameters. */
10566 return void_list_node;
10567 }
10568
10569 /* Parse the parameter-declaration-list. */
10570 parameters = cp_parser_parameter_declaration_list (parser);
10571 /* If a parse error occurred while parsing the
10572 parameter-declaration-list, then the entire
10573 parameter-declaration-clause is erroneous. */
10574 if (parameters == error_mark_node)
10575 return error_mark_node;
10576
10577 /* Peek at the next token. */
10578 token = cp_lexer_peek_token (parser->lexer);
10579 /* If it's a `,', the clause should terminate with an ellipsis. */
10580 if (token->type == CPP_COMMA)
10581 {
10582 /* Consume the `,'. */
10583 cp_lexer_consume_token (parser->lexer);
10584 /* Expect an ellipsis. */
10585 ellipsis_p
10586 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10587 }
10588 /* It might also be `...' if the optional trailing `,' was
10589 omitted. */
10590 else if (token->type == CPP_ELLIPSIS)
10591 {
10592 /* Consume the `...' token. */
10593 cp_lexer_consume_token (parser->lexer);
10594 /* And remember that we saw it. */
10595 ellipsis_p = true;
10596 }
10597 else
10598 ellipsis_p = false;
10599
10600 /* Finish the parameter list. */
10601 return finish_parmlist (parameters, ellipsis_p);
10602}
10603
10604/* Parse a parameter-declaration-list.
10605
10606 parameter-declaration-list:
10607 parameter-declaration
10608 parameter-declaration-list , parameter-declaration
10609
10610 Returns a representation of the parameter-declaration-list, as for
10611 cp_parser_parameter_declaration_clause. However, the
10612 `void_list_node' is never appended to the list. */
10613
10614static tree
94edc4ab 10615cp_parser_parameter_declaration_list (cp_parser* parser)
a723baf1
MM
10616{
10617 tree parameters = NULL_TREE;
10618
10619 /* Look for more parameters. */
10620 while (true)
10621 {
10622 tree parameter;
10623 /* Parse the parameter. */
10624 parameter
ec194454
MM
10625 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10626
a723baf1
MM
10627 /* If a parse error ocurred parsing the parameter declaration,
10628 then the entire parameter-declaration-list is erroneous. */
10629 if (parameter == error_mark_node)
10630 {
10631 parameters = error_mark_node;
10632 break;
10633 }
10634 /* Add the new parameter to the list. */
10635 TREE_CHAIN (parameter) = parameters;
10636 parameters = parameter;
10637
10638 /* Peek at the next token. */
10639 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10640 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10641 /* The parameter-declaration-list is complete. */
10642 break;
10643 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10644 {
10645 cp_token *token;
10646
10647 /* Peek at the next token. */
10648 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10649 /* If it's an ellipsis, then the list is complete. */
10650 if (token->type == CPP_ELLIPSIS)
10651 break;
10652 /* Otherwise, there must be more parameters. Consume the
10653 `,'. */
10654 cp_lexer_consume_token (parser->lexer);
10655 }
10656 else
10657 {
10658 cp_parser_error (parser, "expected `,' or `...'");
10659 break;
10660 }
10661 }
10662
10663 /* We built up the list in reverse order; straighten it out now. */
10664 return nreverse (parameters);
10665}
10666
10667/* Parse a parameter declaration.
10668
10669 parameter-declaration:
10670 decl-specifier-seq declarator
10671 decl-specifier-seq declarator = assignment-expression
10672 decl-specifier-seq abstract-declarator [opt]
10673 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10674
ec194454
MM
10675 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10676 declares a template parameter. (In that case, a non-nested `>'
10677 token encountered during the parsing of the assignment-expression
10678 is not interpreted as a greater-than operator.)
a723baf1
MM
10679
10680 Returns a TREE_LIST representing the parameter-declaration. The
10681 TREE_VALUE is a representation of the decl-specifier-seq and
10682 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10683 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10684 TREE_VALUE represents the declarator. */
10685
10686static tree
ec194454
MM
10687cp_parser_parameter_declaration (cp_parser *parser,
10688 bool template_parm_p)
a723baf1
MM
10689{
10690 bool declares_class_or_enum;
ec194454 10691 bool greater_than_is_operator_p;
a723baf1
MM
10692 tree decl_specifiers;
10693 tree attributes;
10694 tree declarator;
10695 tree default_argument;
10696 tree parameter;
10697 cp_token *token;
10698 const char *saved_message;
10699
ec194454
MM
10700 /* In a template parameter, `>' is not an operator.
10701
10702 [temp.param]
10703
10704 When parsing a default template-argument for a non-type
10705 template-parameter, the first non-nested `>' is taken as the end
10706 of the template parameter-list rather than a greater-than
10707 operator. */
10708 greater_than_is_operator_p = !template_parm_p;
10709
a723baf1
MM
10710 /* Type definitions may not appear in parameter types. */
10711 saved_message = parser->type_definition_forbidden_message;
10712 parser->type_definition_forbidden_message
10713 = "types may not be defined in parameter types";
10714
10715 /* Parse the declaration-specifiers. */
10716 decl_specifiers
10717 = cp_parser_decl_specifier_seq (parser,
10718 CP_PARSER_FLAGS_NONE,
10719 &attributes,
10720 &declares_class_or_enum);
10721 /* If an error occurred, there's no reason to attempt to parse the
10722 rest of the declaration. */
10723 if (cp_parser_error_occurred (parser))
10724 {
10725 parser->type_definition_forbidden_message = saved_message;
10726 return error_mark_node;
10727 }
10728
10729 /* Peek at the next token. */
10730 token = cp_lexer_peek_token (parser->lexer);
10731 /* If the next token is a `)', `,', `=', `>', or `...', then there
10732 is no declarator. */
10733 if (token->type == CPP_CLOSE_PAREN
10734 || token->type == CPP_COMMA
10735 || token->type == CPP_EQ
10736 || token->type == CPP_ELLIPSIS
10737 || token->type == CPP_GREATER)
10738 declarator = NULL_TREE;
10739 /* Otherwise, there should be a declarator. */
10740 else
10741 {
10742 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10743 parser->default_arg_ok_p = false;
10744
a723baf1 10745 declarator = cp_parser_declarator (parser,
62b8a44e 10746 CP_PARSER_DECLARATOR_EITHER,
a723baf1 10747 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1 10748 parser->default_arg_ok_p = saved_default_arg_ok_p;
4971227d
MM
10749 /* After the declarator, allow more attributes. */
10750 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
a723baf1
MM
10751 }
10752
62b8a44e 10753 /* The restriction on defining new types applies only to the type
a723baf1
MM
10754 of the parameter, not to the default argument. */
10755 parser->type_definition_forbidden_message = saved_message;
10756
10757 /* If the next token is `=', then process a default argument. */
10758 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10759 {
10760 bool saved_greater_than_is_operator_p;
10761 /* Consume the `='. */
10762 cp_lexer_consume_token (parser->lexer);
10763
10764 /* If we are defining a class, then the tokens that make up the
10765 default argument must be saved and processed later. */
ec194454
MM
10766 if (!template_parm_p && at_class_scope_p ()
10767 && TYPE_BEING_DEFINED (current_class_type))
a723baf1
MM
10768 {
10769 unsigned depth = 0;
10770
10771 /* Create a DEFAULT_ARG to represented the unparsed default
10772 argument. */
10773 default_argument = make_node (DEFAULT_ARG);
10774 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10775
10776 /* Add tokens until we have processed the entire default
10777 argument. */
10778 while (true)
10779 {
10780 bool done = false;
10781 cp_token *token;
10782
10783 /* Peek at the next token. */
10784 token = cp_lexer_peek_token (parser->lexer);
10785 /* What we do depends on what token we have. */
10786 switch (token->type)
10787 {
10788 /* In valid code, a default argument must be
10789 immediately followed by a `,' `)', or `...'. */
10790 case CPP_COMMA:
10791 case CPP_CLOSE_PAREN:
10792 case CPP_ELLIPSIS:
10793 /* If we run into a non-nested `;', `}', or `]',
10794 then the code is invalid -- but the default
10795 argument is certainly over. */
10796 case CPP_SEMICOLON:
10797 case CPP_CLOSE_BRACE:
10798 case CPP_CLOSE_SQUARE:
10799 if (depth == 0)
10800 done = true;
10801 /* Update DEPTH, if necessary. */
10802 else if (token->type == CPP_CLOSE_PAREN
10803 || token->type == CPP_CLOSE_BRACE
10804 || token->type == CPP_CLOSE_SQUARE)
10805 --depth;
10806 break;
10807
10808 case CPP_OPEN_PAREN:
10809 case CPP_OPEN_SQUARE:
10810 case CPP_OPEN_BRACE:
10811 ++depth;
10812 break;
10813
10814 case CPP_GREATER:
10815 /* If we see a non-nested `>', and `>' is not an
10816 operator, then it marks the end of the default
10817 argument. */
10818 if (!depth && !greater_than_is_operator_p)
10819 done = true;
10820 break;
10821
10822 /* If we run out of tokens, issue an error message. */
10823 case CPP_EOF:
10824 error ("file ends in default argument");
10825 done = true;
10826 break;
10827
10828 case CPP_NAME:
10829 case CPP_SCOPE:
10830 /* In these cases, we should look for template-ids.
10831 For example, if the default argument is
10832 `X<int, double>()', we need to do name lookup to
10833 figure out whether or not `X' is a template; if
10834 so, the `,' does not end the deault argument.
10835
10836 That is not yet done. */
10837 break;
10838
10839 default:
10840 break;
10841 }
10842
10843 /* If we've reached the end, stop. */
10844 if (done)
10845 break;
10846
10847 /* Add the token to the token block. */
10848 token = cp_lexer_consume_token (parser->lexer);
10849 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10850 token);
10851 }
10852 }
10853 /* Outside of a class definition, we can just parse the
10854 assignment-expression. */
10855 else
10856 {
10857 bool saved_local_variables_forbidden_p;
10858
10859 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10860 set correctly. */
10861 saved_greater_than_is_operator_p
10862 = parser->greater_than_is_operator_p;
10863 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10864 /* Local variable names (and the `this' keyword) may not
10865 appear in a default argument. */
10866 saved_local_variables_forbidden_p
10867 = parser->local_variables_forbidden_p;
10868 parser->local_variables_forbidden_p = true;
10869 /* Parse the assignment-expression. */
10870 default_argument = cp_parser_assignment_expression (parser);
10871 /* Restore saved state. */
10872 parser->greater_than_is_operator_p
10873 = saved_greater_than_is_operator_p;
10874 parser->local_variables_forbidden_p
10875 = saved_local_variables_forbidden_p;
10876 }
10877 if (!parser->default_arg_ok_p)
10878 {
10879 pedwarn ("default arguments are only permitted on functions");
10880 if (flag_pedantic_errors)
10881 default_argument = NULL_TREE;
10882 }
10883 }
10884 else
10885 default_argument = NULL_TREE;
10886
10887 /* Create the representation of the parameter. */
10888 if (attributes)
10889 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10890 parameter = build_tree_list (default_argument,
10891 build_tree_list (decl_specifiers,
10892 declarator));
10893
10894 return parameter;
10895}
10896
10897/* Parse a function-definition.
10898
10899 function-definition:
10900 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10901 function-body
10902 decl-specifier-seq [opt] declarator function-try-block
10903
10904 GNU Extension:
10905
10906 function-definition:
10907 __extension__ function-definition
10908
10909 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10910 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10911 be a `friend'. */
10912
10913static tree
94edc4ab 10914cp_parser_function_definition (cp_parser* parser, bool* friend_p)
a723baf1
MM
10915{
10916 tree decl_specifiers;
10917 tree attributes;
10918 tree declarator;
10919 tree fn;
a723baf1
MM
10920 cp_token *token;
10921 bool declares_class_or_enum;
10922 bool member_p;
10923 /* The saved value of the PEDANTIC flag. */
10924 int saved_pedantic;
10925
10926 /* Any pending qualification must be cleared by our caller. It is
10927 more robust to force the callers to clear PARSER->SCOPE than to
10928 do it here since if the qualification is in effect here, it might
10929 also end up in effect elsewhere that it is not intended. */
10930 my_friendly_assert (!parser->scope, 20010821);
10931
10932 /* Handle `__extension__'. */
10933 if (cp_parser_extension_opt (parser, &saved_pedantic))
10934 {
10935 /* Parse the function-definition. */
10936 fn = cp_parser_function_definition (parser, friend_p);
10937 /* Restore the PEDANTIC flag. */
10938 pedantic = saved_pedantic;
10939
10940 return fn;
10941 }
10942
10943 /* Check to see if this definition appears in a class-specifier. */
10944 member_p = (at_class_scope_p ()
10945 && TYPE_BEING_DEFINED (current_class_type));
10946 /* Defer access checks in the decl-specifier-seq until we know what
10947 function is being defined. There is no need to do this for the
10948 definition of member functions; we cannot be defining a member
10949 from another class. */
cf22909c
KL
10950 push_deferring_access_checks (!member_p);
10951
a723baf1
MM
10952 /* Parse the decl-specifier-seq. */
10953 decl_specifiers
10954 = cp_parser_decl_specifier_seq (parser,
10955 CP_PARSER_FLAGS_OPTIONAL,
10956 &attributes,
10957 &declares_class_or_enum);
10958 /* Figure out whether this declaration is a `friend'. */
10959 if (friend_p)
10960 *friend_p = cp_parser_friend_p (decl_specifiers);
10961
10962 /* Parse the declarator. */
62b8a44e 10963 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
10964 /*ctor_dtor_or_conv_p=*/NULL);
10965
10966 /* Gather up any access checks that occurred. */
cf22909c 10967 stop_deferring_access_checks ();
a723baf1
MM
10968
10969 /* If something has already gone wrong, we may as well stop now. */
10970 if (declarator == error_mark_node)
10971 {
10972 /* Skip to the end of the function, or if this wasn't anything
10973 like a function-definition, to a `;' in the hopes of finding
10974 a sensible place from which to continue parsing. */
10975 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 10976 pop_deferring_access_checks ();
a723baf1
MM
10977 return error_mark_node;
10978 }
10979
10980 /* The next character should be a `{' (for a simple function
10981 definition), a `:' (for a ctor-initializer), or `try' (for a
10982 function-try block). */
10983 token = cp_lexer_peek_token (parser->lexer);
10984 if (!cp_parser_token_starts_function_definition_p (token))
10985 {
10986 /* Issue the error-message. */
10987 cp_parser_error (parser, "expected function-definition");
10988 /* Skip to the next `;'. */
10989 cp_parser_skip_to_end_of_block_or_statement (parser);
10990
cf22909c 10991 pop_deferring_access_checks ();
a723baf1
MM
10992 return error_mark_node;
10993 }
10994
10995 /* If we are in a class scope, then we must handle
10996 function-definitions specially. In particular, we save away the
10997 tokens that make up the function body, and parse them again
10998 later, in order to handle code like:
10999
11000 struct S {
11001 int f () { return i; }
11002 int i;
11003 };
11004
11005 Here, we cannot parse the body of `f' until after we have seen
11006 the declaration of `i'. */
11007 if (member_p)
11008 {
11009 cp_token_cache *cache;
11010
11011 /* Create the function-declaration. */
11012 fn = start_method (decl_specifiers, declarator, attributes);
11013 /* If something went badly wrong, bail out now. */
11014 if (fn == error_mark_node)
11015 {
11016 /* If there's a function-body, skip it. */
11017 if (cp_parser_token_starts_function_definition_p
11018 (cp_lexer_peek_token (parser->lexer)))
11019 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11020 pop_deferring_access_checks ();
a723baf1
MM
11021 return error_mark_node;
11022 }
11023
11024 /* Create a token cache. */
11025 cache = cp_token_cache_new ();
11026 /* Save away the tokens that make up the body of the
11027 function. */
11028 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11029 /* Handle function try blocks. */
11030 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
11031 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
11032
11033 /* Save away the inline definition; we will process it when the
11034 class is complete. */
11035 DECL_PENDING_INLINE_INFO (fn) = cache;
11036 DECL_PENDING_INLINE_P (fn) = 1;
11037
11038 /* We're done with the inline definition. */
11039 finish_method (fn);
11040
11041 /* Add FN to the queue of functions to be parsed later. */
11042 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 11043 = tree_cons (NULL_TREE, fn,
a723baf1
MM
11044 TREE_VALUE (parser->unparsed_functions_queues));
11045
cf22909c 11046 pop_deferring_access_checks ();
a723baf1
MM
11047 return fn;
11048 }
11049
11050 /* Check that the number of template-parameter-lists is OK. */
11051 if (!cp_parser_check_declarator_template_parameters (parser,
11052 declarator))
11053 {
11054 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11055 pop_deferring_access_checks ();
a723baf1
MM
11056 return error_mark_node;
11057 }
11058
cf22909c
KL
11059 fn = cp_parser_function_definition_from_specifiers_and_declarator
11060 (parser, decl_specifiers, attributes, declarator);
11061 pop_deferring_access_checks ();
11062 return fn;
a723baf1
MM
11063}
11064
11065/* Parse a function-body.
11066
11067 function-body:
11068 compound_statement */
11069
11070static void
11071cp_parser_function_body (cp_parser *parser)
11072{
11073 cp_parser_compound_statement (parser);
11074}
11075
11076/* Parse a ctor-initializer-opt followed by a function-body. Return
11077 true if a ctor-initializer was present. */
11078
11079static bool
11080cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11081{
11082 tree body;
11083 bool ctor_initializer_p;
11084
11085 /* Begin the function body. */
11086 body = begin_function_body ();
11087 /* Parse the optional ctor-initializer. */
11088 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11089 /* Parse the function-body. */
11090 cp_parser_function_body (parser);
11091 /* Finish the function body. */
11092 finish_function_body (body);
11093
11094 return ctor_initializer_p;
11095}
11096
11097/* Parse an initializer.
11098
11099 initializer:
11100 = initializer-clause
11101 ( expression-list )
11102
11103 Returns a expression representing the initializer. If no
11104 initializer is present, NULL_TREE is returned.
11105
11106 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11107 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
11108 set to FALSE if there is no initializer present. */
11109
11110static tree
94edc4ab 11111cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init)
a723baf1
MM
11112{
11113 cp_token *token;
11114 tree init;
11115
11116 /* Peek at the next token. */
11117 token = cp_lexer_peek_token (parser->lexer);
11118
11119 /* Let our caller know whether or not this initializer was
11120 parenthesized. */
11121 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11122
11123 if (token->type == CPP_EQ)
11124 {
11125 /* Consume the `='. */
11126 cp_lexer_consume_token (parser->lexer);
11127 /* Parse the initializer-clause. */
11128 init = cp_parser_initializer_clause (parser);
11129 }
11130 else if (token->type == CPP_OPEN_PAREN)
11131 {
11132 /* Consume the `('. */
11133 cp_lexer_consume_token (parser->lexer);
11134 /* Parse the expression-list. */
11135 init = cp_parser_expression_list (parser);
11136 /* Consume the `)' token. */
11137 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11138 cp_parser_skip_to_closing_parenthesis (parser);
11139 }
11140 else
11141 {
11142 /* Anything else is an error. */
11143 cp_parser_error (parser, "expected initializer");
11144 init = error_mark_node;
11145 }
11146
11147 return init;
11148}
11149
11150/* Parse an initializer-clause.
11151
11152 initializer-clause:
11153 assignment-expression
11154 { initializer-list , [opt] }
11155 { }
11156
11157 Returns an expression representing the initializer.
11158
11159 If the `assignment-expression' production is used the value
11160 returned is simply a reprsentation for the expression.
11161
11162 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11163 the elements of the initializer-list (or NULL_TREE, if the last
11164 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11165 NULL_TREE. There is no way to detect whether or not the optional
11166 trailing `,' was provided. */
11167
11168static tree
94edc4ab 11169cp_parser_initializer_clause (cp_parser* parser)
a723baf1
MM
11170{
11171 tree initializer;
11172
11173 /* If it is not a `{', then we are looking at an
11174 assignment-expression. */
11175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11176 initializer = cp_parser_assignment_expression (parser);
11177 else
11178 {
11179 /* Consume the `{' token. */
11180 cp_lexer_consume_token (parser->lexer);
11181 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11182 initializer = make_node (CONSTRUCTOR);
11183 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11184 necessary, but check_initializer depends upon it, for
11185 now. */
11186 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11187 /* If it's not a `}', then there is a non-trivial initializer. */
11188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11189 {
11190 /* Parse the initializer list. */
11191 CONSTRUCTOR_ELTS (initializer)
11192 = cp_parser_initializer_list (parser);
11193 /* A trailing `,' token is allowed. */
11194 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11195 cp_lexer_consume_token (parser->lexer);
11196 }
11197
11198 /* Now, there should be a trailing `}'. */
11199 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11200 }
11201
11202 return initializer;
11203}
11204
11205/* Parse an initializer-list.
11206
11207 initializer-list:
11208 initializer-clause
11209 initializer-list , initializer-clause
11210
11211 GNU Extension:
11212
11213 initializer-list:
11214 identifier : initializer-clause
11215 initializer-list, identifier : initializer-clause
11216
11217 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11218 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11219 IDENTIFIER_NODE naming the field to initialize. */
11220
11221static tree
94edc4ab 11222cp_parser_initializer_list (cp_parser* parser)
a723baf1
MM
11223{
11224 tree initializers = NULL_TREE;
11225
11226 /* Parse the rest of the list. */
11227 while (true)
11228 {
11229 cp_token *token;
11230 tree identifier;
11231 tree initializer;
11232
11233 /* If the next token is an identifier and the following one is a
11234 colon, we are looking at the GNU designated-initializer
11235 syntax. */
11236 if (cp_parser_allow_gnu_extensions_p (parser)
11237 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11238 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11239 {
11240 /* Consume the identifier. */
11241 identifier = cp_lexer_consume_token (parser->lexer)->value;
11242 /* Consume the `:'. */
11243 cp_lexer_consume_token (parser->lexer);
11244 }
11245 else
11246 identifier = NULL_TREE;
11247
11248 /* Parse the initializer. */
11249 initializer = cp_parser_initializer_clause (parser);
11250
11251 /* Add it to the list. */
11252 initializers = tree_cons (identifier, initializer, initializers);
11253
11254 /* If the next token is not a comma, we have reached the end of
11255 the list. */
11256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11257 break;
11258
11259 /* Peek at the next token. */
11260 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11261 /* If the next token is a `}', then we're still done. An
11262 initializer-clause can have a trailing `,' after the
11263 initializer-list and before the closing `}'. */
11264 if (token->type == CPP_CLOSE_BRACE)
11265 break;
11266
11267 /* Consume the `,' token. */
11268 cp_lexer_consume_token (parser->lexer);
11269 }
11270
11271 /* The initializers were built up in reverse order, so we need to
11272 reverse them now. */
11273 return nreverse (initializers);
11274}
11275
11276/* Classes [gram.class] */
11277
11278/* Parse a class-name.
11279
11280 class-name:
11281 identifier
11282 template-id
11283
11284 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11285 to indicate that names looked up in dependent types should be
11286 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11287 keyword has been used to indicate that the name that appears next
11288 is a template. TYPE_P is true iff the next name should be treated
11289 as class-name, even if it is declared to be some other kind of name
11290 as well. The accessibility of the class-name is checked iff
11291 CHECK_ACCESS_P is true. If CHECK_DEPENDENCY_P is FALSE, names are
11292 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
11293 is the class being defined in a class-head.
11294
11295 Returns the TYPE_DECL representing the class. */
11296
11297static tree
11298cp_parser_class_name (cp_parser *parser,
11299 bool typename_keyword_p,
11300 bool template_keyword_p,
11301 bool type_p,
11302 bool check_access_p,
11303 bool check_dependency_p,
11304 bool class_head_p)
11305{
11306 tree decl;
11307 tree scope;
11308 bool typename_p;
e5976695
MM
11309 cp_token *token;
11310
11311 /* All class-names start with an identifier. */
11312 token = cp_lexer_peek_token (parser->lexer);
11313 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11314 {
11315 cp_parser_error (parser, "expected class-name");
11316 return error_mark_node;
11317 }
11318
a723baf1
MM
11319 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11320 to a template-id, so we save it here. */
11321 scope = parser->scope;
11322 /* Any name names a type if we're following the `typename' keyword
11323 in a qualified name where the enclosing scope is type-dependent. */
11324 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
1fb3244a 11325 && dependent_type_p (scope));
e5976695
MM
11326 /* Handle the common case (an identifier, but not a template-id)
11327 efficiently. */
11328 if (token->type == CPP_NAME
11329 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
a723baf1 11330 {
a723baf1
MM
11331 tree identifier;
11332
11333 /* Look for the identifier. */
11334 identifier = cp_parser_identifier (parser);
11335 /* If the next token isn't an identifier, we are certainly not
11336 looking at a class-name. */
11337 if (identifier == error_mark_node)
11338 decl = error_mark_node;
11339 /* If we know this is a type-name, there's no need to look it
11340 up. */
11341 else if (typename_p)
11342 decl = identifier;
11343 else
11344 {
11345 /* If the next token is a `::', then the name must be a type
11346 name.
11347
11348 [basic.lookup.qual]
11349
11350 During the lookup for a name preceding the :: scope
11351 resolution operator, object, function, and enumerator
11352 names are ignored. */
11353 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11354 type_p = true;
11355 /* Look up the name. */
11356 decl = cp_parser_lookup_name (parser, identifier,
11357 check_access_p,
11358 type_p,
eea9800f 11359 /*is_namespace=*/false,
a723baf1
MM
11360 check_dependency_p);
11361 }
11362 }
e5976695
MM
11363 else
11364 {
11365 /* Try a template-id. */
11366 decl = cp_parser_template_id (parser, template_keyword_p,
11367 check_dependency_p);
11368 if (decl == error_mark_node)
11369 return error_mark_node;
11370 }
a723baf1
MM
11371
11372 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11373
11374 /* If this is a typename, create a TYPENAME_TYPE. */
11375 if (typename_p && decl != error_mark_node)
11376 decl = TYPE_NAME (make_typename_type (scope, decl,
11377 /*complain=*/1));
11378
11379 /* Check to see that it is really the name of a class. */
11380 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11381 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11382 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11383 /* Situations like this:
11384
11385 template <typename T> struct A {
11386 typename T::template X<int>::I i;
11387 };
11388
11389 are problematic. Is `T::template X<int>' a class-name? The
11390 standard does not seem to be definitive, but there is no other
11391 valid interpretation of the following `::'. Therefore, those
11392 names are considered class-names. */
11393 decl = TYPE_NAME (make_typename_type (scope, decl,
11394 tf_error | tf_parsing));
11395 else if (decl == error_mark_node
11396 || TREE_CODE (decl) != TYPE_DECL
11397 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11398 {
11399 cp_parser_error (parser, "expected class-name");
11400 return error_mark_node;
11401 }
11402
11403 return decl;
11404}
11405
11406/* Parse a class-specifier.
11407
11408 class-specifier:
11409 class-head { member-specification [opt] }
11410
11411 Returns the TREE_TYPE representing the class. */
11412
11413static tree
94edc4ab 11414cp_parser_class_specifier (cp_parser* parser)
a723baf1
MM
11415{
11416 cp_token *token;
11417 tree type;
11418 tree attributes = NULL_TREE;
11419 int has_trailing_semicolon;
11420 bool nested_name_specifier_p;
a723baf1
MM
11421 unsigned saved_num_template_parameter_lists;
11422
cf22909c
KL
11423 push_deferring_access_checks (false);
11424
a723baf1
MM
11425 /* Parse the class-head. */
11426 type = cp_parser_class_head (parser,
cf22909c 11427 &nested_name_specifier_p);
a723baf1
MM
11428 /* If the class-head was a semantic disaster, skip the entire body
11429 of the class. */
11430 if (!type)
11431 {
11432 cp_parser_skip_to_end_of_block_or_statement (parser);
cf22909c 11433 pop_deferring_access_checks ();
a723baf1
MM
11434 return error_mark_node;
11435 }
cf22909c 11436
a723baf1
MM
11437 /* Look for the `{'. */
11438 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
cf22909c
KL
11439 {
11440 pop_deferring_access_checks ();
11441 return error_mark_node;
11442 }
11443
a723baf1
MM
11444 /* Issue an error message if type-definitions are forbidden here. */
11445 cp_parser_check_type_definition (parser);
11446 /* Remember that we are defining one more class. */
11447 ++parser->num_classes_being_defined;
11448 /* Inside the class, surrounding template-parameter-lists do not
11449 apply. */
11450 saved_num_template_parameter_lists
11451 = parser->num_template_parameter_lists;
11452 parser->num_template_parameter_lists = 0;
11453 /* Start the class. */
11454 type = begin_class_definition (type);
11455 if (type == error_mark_node)
11456 /* If the type is erroneous, skip the entire body of the class. */
11457 cp_parser_skip_to_closing_brace (parser);
11458 else
11459 /* Parse the member-specification. */
11460 cp_parser_member_specification_opt (parser);
11461 /* Look for the trailing `}'. */
11462 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11463 /* We get better error messages by noticing a common problem: a
11464 missing trailing `;'. */
11465 token = cp_lexer_peek_token (parser->lexer);
11466 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11467 /* Look for attributes to apply to this class. */
11468 if (cp_parser_allow_gnu_extensions_p (parser))
11469 attributes = cp_parser_attributes_opt (parser);
11470 /* Finish the class definition. */
11471 type = finish_class_definition (type,
11472 attributes,
11473 has_trailing_semicolon,
11474 nested_name_specifier_p);
11475 /* If this class is not itself within the scope of another class,
11476 then we need to parse the bodies of all of the queued function
11477 definitions. Note that the queued functions defined in a class
11478 are not always processed immediately following the
11479 class-specifier for that class. Consider:
11480
11481 struct A {
11482 struct B { void f() { sizeof (A); } };
11483 };
11484
11485 If `f' were processed before the processing of `A' were
11486 completed, there would be no way to compute the size of `A'.
11487 Note that the nesting we are interested in here is lexical --
11488 not the semantic nesting given by TYPE_CONTEXT. In particular,
11489 for:
11490
11491 struct A { struct B; };
11492 struct A::B { void f() { } };
11493
11494 there is no need to delay the parsing of `A::B::f'. */
11495 if (--parser->num_classes_being_defined == 0)
11496 {
11497 tree last_scope = NULL_TREE;
8218bd34
MM
11498 tree queue_entry;
11499 tree fn;
a723baf1 11500
a723baf1
MM
11501 /* Reverse the queue, so that we process it in the order the
11502 functions were declared. */
11503 TREE_VALUE (parser->unparsed_functions_queues)
11504 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
8218bd34
MM
11505 /* In a first pass, parse default arguments to the functions.
11506 Then, in a second pass, parse the bodies of the functions.
11507 This two-phased approach handles cases like:
11508
11509 struct S {
11510 void f() { g(); }
11511 void g(int i = 3);
11512 };
11513
11514 */
11515 for (queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
11516 queue_entry;
11517 queue_entry = TREE_CHAIN (queue_entry))
11518 {
11519 fn = TREE_VALUE (queue_entry);
11520 if (DECL_FUNCTION_TEMPLATE_P (fn))
11521 fn = DECL_TEMPLATE_RESULT (fn);
11522 /* Make sure that any template parameters are in scope. */
11523 maybe_begin_member_template_processing (fn);
11524 /* If there are default arguments that have not yet been processed,
11525 take care of them now. */
11526 cp_parser_late_parsing_default_args (parser, fn);
11527 /* Remove any template parameters from the symbol table. */
11528 maybe_end_member_template_processing ();
11529 }
11530 /* Now parse the body of the functions. */
a723baf1
MM
11531 while (TREE_VALUE (parser->unparsed_functions_queues))
11532
11533 {
a723baf1
MM
11534 /* Figure out which function we need to process. */
11535 queue_entry = TREE_VALUE (parser->unparsed_functions_queues);
a723baf1
MM
11536 fn = TREE_VALUE (queue_entry);
11537
11538 /* Parse the function. */
11539 cp_parser_late_parsing_for_member (parser, fn);
11540
11541 TREE_VALUE (parser->unparsed_functions_queues)
11542 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues));
11543 }
11544
11545 /* If LAST_SCOPE is non-NULL, then we have pushed scopes one
11546 more time than we have popped, so me must pop here. */
11547 if (last_scope)
11548 pop_scope (last_scope);
11549 }
11550
11551 /* Put back any saved access checks. */
cf22909c 11552 pop_deferring_access_checks ();
a723baf1
MM
11553
11554 /* Restore the count of active template-parameter-lists. */
11555 parser->num_template_parameter_lists
11556 = saved_num_template_parameter_lists;
11557
11558 return type;
11559}
11560
11561/* Parse a class-head.
11562
11563 class-head:
11564 class-key identifier [opt] base-clause [opt]
11565 class-key nested-name-specifier identifier base-clause [opt]
11566 class-key nested-name-specifier [opt] template-id
11567 base-clause [opt]
11568
11569 GNU Extensions:
11570 class-key attributes identifier [opt] base-clause [opt]
11571 class-key attributes nested-name-specifier identifier base-clause [opt]
11572 class-key attributes nested-name-specifier [opt] template-id
11573 base-clause [opt]
11574
11575 Returns the TYPE of the indicated class. Sets
11576 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11577 involving a nested-name-specifier was used, and FALSE otherwise.
a723baf1
MM
11578
11579 Returns NULL_TREE if the class-head is syntactically valid, but
11580 semantically invalid in a way that means we should skip the entire
11581 body of the class. */
11582
11583static tree
94edc4ab
NN
11584cp_parser_class_head (cp_parser* parser,
11585 bool* nested_name_specifier_p)
a723baf1
MM
11586{
11587 cp_token *token;
11588 tree nested_name_specifier;
11589 enum tag_types class_key;
11590 tree id = NULL_TREE;
11591 tree type = NULL_TREE;
11592 tree attributes;
11593 bool template_id_p = false;
11594 bool qualified_p = false;
11595 bool invalid_nested_name_p = false;
11596 unsigned num_templates;
11597
11598 /* Assume no nested-name-specifier will be present. */
11599 *nested_name_specifier_p = false;
11600 /* Assume no template parameter lists will be used in defining the
11601 type. */
11602 num_templates = 0;
11603
11604 /* Look for the class-key. */
11605 class_key = cp_parser_class_key (parser);
11606 if (class_key == none_type)
11607 return error_mark_node;
11608
11609 /* Parse the attributes. */
11610 attributes = cp_parser_attributes_opt (parser);
11611
11612 /* If the next token is `::', that is invalid -- but sometimes
11613 people do try to write:
11614
11615 struct ::S {};
11616
11617 Handle this gracefully by accepting the extra qualifier, and then
11618 issuing an error about it later if this really is a
2050a1bb 11619 class-head. If it turns out just to be an elaborated type
a723baf1
MM
11620 specifier, remain silent. */
11621 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11622 qualified_p = true;
11623
11624 /* Determine the name of the class. Begin by looking for an
11625 optional nested-name-specifier. */
11626 nested_name_specifier
11627 = cp_parser_nested_name_specifier_opt (parser,
11628 /*typename_keyword_p=*/false,
11629 /*check_dependency_p=*/true,
11630 /*type_p=*/false);
11631 /* If there was a nested-name-specifier, then there *must* be an
11632 identifier. */
11633 if (nested_name_specifier)
11634 {
11635 /* Although the grammar says `identifier', it really means
11636 `class-name' or `template-name'. You are only allowed to
11637 define a class that has already been declared with this
11638 syntax.
11639
11640 The proposed resolution for Core Issue 180 says that whever
11641 you see `class T::X' you should treat `X' as a type-name.
11642
11643 It is OK to define an inaccessible class; for example:
11644
11645 class A { class B; };
11646 class A::B {};
11647
11648 So, we ask cp_parser_class_name not to check accessibility.
11649
11650 We do not know if we will see a class-name, or a
11651 template-name. We look for a class-name first, in case the
11652 class-name is a template-id; if we looked for the
11653 template-name first we would stop after the template-name. */
11654 cp_parser_parse_tentatively (parser);
11655 type = cp_parser_class_name (parser,
11656 /*typename_keyword_p=*/false,
11657 /*template_keyword_p=*/false,
11658 /*type_p=*/true,
11659 /*check_access_p=*/false,
11660 /*check_dependency_p=*/false,
11661 /*class_head_p=*/true);
11662 /* If that didn't work, ignore the nested-name-specifier. */
11663 if (!cp_parser_parse_definitely (parser))
11664 {
11665 invalid_nested_name_p = true;
11666 id = cp_parser_identifier (parser);
11667 if (id == error_mark_node)
11668 id = NULL_TREE;
11669 }
11670 /* If we could not find a corresponding TYPE, treat this
11671 declaration like an unqualified declaration. */
11672 if (type == error_mark_node)
11673 nested_name_specifier = NULL_TREE;
11674 /* Otherwise, count the number of templates used in TYPE and its
11675 containing scopes. */
11676 else
11677 {
11678 tree scope;
11679
11680 for (scope = TREE_TYPE (type);
11681 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11682 scope = (TYPE_P (scope)
11683 ? TYPE_CONTEXT (scope)
11684 : DECL_CONTEXT (scope)))
11685 if (TYPE_P (scope)
11686 && CLASS_TYPE_P (scope)
11687 && CLASSTYPE_TEMPLATE_INFO (scope)
2050a1bb
MM
11688 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11689 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
a723baf1
MM
11690 ++num_templates;
11691 }
11692 }
11693 /* Otherwise, the identifier is optional. */
11694 else
11695 {
11696 /* We don't know whether what comes next is a template-id,
11697 an identifier, or nothing at all. */
11698 cp_parser_parse_tentatively (parser);
11699 /* Check for a template-id. */
11700 id = cp_parser_template_id (parser,
11701 /*template_keyword_p=*/false,
11702 /*check_dependency_p=*/true);
11703 /* If that didn't work, it could still be an identifier. */
11704 if (!cp_parser_parse_definitely (parser))
11705 {
11706 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11707 id = cp_parser_identifier (parser);
11708 else
11709 id = NULL_TREE;
11710 }
11711 else
11712 {
11713 template_id_p = true;
11714 ++num_templates;
11715 }
11716 }
11717
11718 /* If it's not a `:' or a `{' then we can't really be looking at a
11719 class-head, since a class-head only appears as part of a
11720 class-specifier. We have to detect this situation before calling
11721 xref_tag, since that has irreversible side-effects. */
11722 if (!cp_parser_next_token_starts_class_definition_p (parser))
11723 {
11724 cp_parser_error (parser, "expected `{' or `:'");
11725 return error_mark_node;
11726 }
11727
11728 /* At this point, we're going ahead with the class-specifier, even
11729 if some other problem occurs. */
11730 cp_parser_commit_to_tentative_parse (parser);
11731 /* Issue the error about the overly-qualified name now. */
11732 if (qualified_p)
11733 cp_parser_error (parser,
11734 "global qualification of class name is invalid");
11735 else if (invalid_nested_name_p)
11736 cp_parser_error (parser,
11737 "qualified name does not name a class");
11738 /* Make sure that the right number of template parameters were
11739 present. */
11740 if (!cp_parser_check_template_parameters (parser, num_templates))
11741 /* If something went wrong, there is no point in even trying to
11742 process the class-definition. */
11743 return NULL_TREE;
11744
a723baf1
MM
11745 /* Look up the type. */
11746 if (template_id_p)
11747 {
11748 type = TREE_TYPE (id);
11749 maybe_process_partial_specialization (type);
11750 }
11751 else if (!nested_name_specifier)
11752 {
11753 /* If the class was unnamed, create a dummy name. */
11754 if (!id)
11755 id = make_anon_name ();
11756 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11757 }
11758 else
11759 {
a723baf1 11760 tree class_type;
089d6ea7 11761 tree scope;
a723baf1
MM
11762
11763 /* Given:
11764
11765 template <typename T> struct S { struct T };
14d22dd6 11766 template <typename T> struct S<T>::T { };
a723baf1
MM
11767
11768 we will get a TYPENAME_TYPE when processing the definition of
11769 `S::T'. We need to resolve it to the actual type before we
11770 try to define it. */
11771 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11772 {
14d22dd6
MM
11773 class_type = resolve_typename_type (TREE_TYPE (type),
11774 /*only_current_p=*/false);
11775 if (class_type != error_mark_node)
11776 type = TYPE_NAME (class_type);
11777 else
11778 {
11779 cp_parser_error (parser, "could not resolve typename type");
11780 type = error_mark_node;
11781 }
a723baf1
MM
11782 }
11783
089d6ea7
MM
11784 /* Figure out in what scope the declaration is being placed. */
11785 scope = current_scope ();
11786 if (!scope)
11787 scope = current_namespace;
11788 /* If that scope does not contain the scope in which the
11789 class was originally declared, the program is invalid. */
11790 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11791 {
11792 error ("declaration of `%D' in `%D' which does not "
11793 "enclose `%D'", type, scope, nested_name_specifier);
11794 return NULL_TREE;
11795 }
11796
a723baf1
MM
11797 maybe_process_partial_specialization (TREE_TYPE (type));
11798 class_type = current_class_type;
11799 type = TREE_TYPE (handle_class_head (class_key,
11800 nested_name_specifier,
11801 type,
089d6ea7 11802 attributes));
a723baf1
MM
11803 if (type != error_mark_node)
11804 {
11805 if (!class_type && TYPE_CONTEXT (type))
11806 *nested_name_specifier_p = true;
11807 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11808 class_type))
11809 *nested_name_specifier_p = true;
11810 }
11811 }
11812 /* Indicate whether this class was declared as a `class' or as a
11813 `struct'. */
11814 if (TREE_CODE (type) == RECORD_TYPE)
11815 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11816 cp_parser_check_class_key (class_key, type);
11817
11818 /* Enter the scope containing the class; the names of base classes
11819 should be looked up in that context. For example, given:
11820
11821 struct A { struct B {}; struct C; };
11822 struct A::C : B {};
11823
11824 is valid. */
11825 if (nested_name_specifier)
11826 push_scope (nested_name_specifier);
11827 /* Now, look for the base-clause. */
11828 token = cp_lexer_peek_token (parser->lexer);
11829 if (token->type == CPP_COLON)
11830 {
11831 tree bases;
11832
11833 /* Get the list of base-classes. */
11834 bases = cp_parser_base_clause (parser);
11835 /* Process them. */
11836 xref_basetypes (type, bases);
11837 }
11838 /* Leave the scope given by the nested-name-specifier. We will
11839 enter the class scope itself while processing the members. */
11840 if (nested_name_specifier)
11841 pop_scope (nested_name_specifier);
11842
11843 return type;
11844}
11845
11846/* Parse a class-key.
11847
11848 class-key:
11849 class
11850 struct
11851 union
11852
11853 Returns the kind of class-key specified, or none_type to indicate
11854 error. */
11855
11856static enum tag_types
94edc4ab 11857cp_parser_class_key (cp_parser* parser)
a723baf1
MM
11858{
11859 cp_token *token;
11860 enum tag_types tag_type;
11861
11862 /* Look for the class-key. */
11863 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11864 if (!token)
11865 return none_type;
11866
11867 /* Check to see if the TOKEN is a class-key. */
11868 tag_type = cp_parser_token_is_class_key (token);
11869 if (!tag_type)
11870 cp_parser_error (parser, "expected class-key");
11871 return tag_type;
11872}
11873
11874/* Parse an (optional) member-specification.
11875
11876 member-specification:
11877 member-declaration member-specification [opt]
11878 access-specifier : member-specification [opt] */
11879
11880static void
94edc4ab 11881cp_parser_member_specification_opt (cp_parser* parser)
a723baf1
MM
11882{
11883 while (true)
11884 {
11885 cp_token *token;
11886 enum rid keyword;
11887
11888 /* Peek at the next token. */
11889 token = cp_lexer_peek_token (parser->lexer);
11890 /* If it's a `}', or EOF then we've seen all the members. */
11891 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11892 break;
11893
11894 /* See if this token is a keyword. */
11895 keyword = token->keyword;
11896 switch (keyword)
11897 {
11898 case RID_PUBLIC:
11899 case RID_PROTECTED:
11900 case RID_PRIVATE:
11901 /* Consume the access-specifier. */
11902 cp_lexer_consume_token (parser->lexer);
11903 /* Remember which access-specifier is active. */
11904 current_access_specifier = token->value;
11905 /* Look for the `:'. */
11906 cp_parser_require (parser, CPP_COLON, "`:'");
11907 break;
11908
11909 default:
11910 /* Otherwise, the next construction must be a
11911 member-declaration. */
11912 cp_parser_member_declaration (parser);
a723baf1
MM
11913 }
11914 }
11915}
11916
11917/* Parse a member-declaration.
11918
11919 member-declaration:
11920 decl-specifier-seq [opt] member-declarator-list [opt] ;
11921 function-definition ; [opt]
11922 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11923 using-declaration
11924 template-declaration
11925
11926 member-declarator-list:
11927 member-declarator
11928 member-declarator-list , member-declarator
11929
11930 member-declarator:
11931 declarator pure-specifier [opt]
11932 declarator constant-initializer [opt]
11933 identifier [opt] : constant-expression
11934
11935 GNU Extensions:
11936
11937 member-declaration:
11938 __extension__ member-declaration
11939
11940 member-declarator:
11941 declarator attributes [opt] pure-specifier [opt]
11942 declarator attributes [opt] constant-initializer [opt]
11943 identifier [opt] attributes [opt] : constant-expression */
11944
11945static void
94edc4ab 11946cp_parser_member_declaration (cp_parser* parser)
a723baf1
MM
11947{
11948 tree decl_specifiers;
11949 tree prefix_attributes;
11950 tree decl;
11951 bool declares_class_or_enum;
11952 bool friend_p;
11953 cp_token *token;
11954 int saved_pedantic;
11955
11956 /* Check for the `__extension__' keyword. */
11957 if (cp_parser_extension_opt (parser, &saved_pedantic))
11958 {
11959 /* Recurse. */
11960 cp_parser_member_declaration (parser);
11961 /* Restore the old value of the PEDANTIC flag. */
11962 pedantic = saved_pedantic;
11963
11964 return;
11965 }
11966
11967 /* Check for a template-declaration. */
11968 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11969 {
11970 /* Parse the template-declaration. */
11971 cp_parser_template_declaration (parser, /*member_p=*/true);
11972
11973 return;
11974 }
11975
11976 /* Check for a using-declaration. */
11977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11978 {
11979 /* Parse the using-declaration. */
11980 cp_parser_using_declaration (parser);
11981
11982 return;
11983 }
11984
11985 /* We can't tell whether we're looking at a declaration or a
11986 function-definition. */
11987 cp_parser_parse_tentatively (parser);
11988
11989 /* Parse the decl-specifier-seq. */
11990 decl_specifiers
11991 = cp_parser_decl_specifier_seq (parser,
11992 CP_PARSER_FLAGS_OPTIONAL,
11993 &prefix_attributes,
11994 &declares_class_or_enum);
8fbc5ae7
MM
11995 /* Check for an invalid type-name. */
11996 if (cp_parser_diagnose_invalid_type_name (parser))
11997 return;
a723baf1
MM
11998 /* If there is no declarator, then the decl-specifier-seq should
11999 specify a type. */
12000 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12001 {
12002 /* If there was no decl-specifier-seq, and the next token is a
12003 `;', then we have something like:
12004
12005 struct S { ; };
12006
12007 [class.mem]
12008
12009 Each member-declaration shall declare at least one member
12010 name of the class. */
12011 if (!decl_specifiers)
12012 {
12013 if (pedantic)
12014 pedwarn ("extra semicolon");
12015 }
12016 else
12017 {
12018 tree type;
12019
12020 /* See if this declaration is a friend. */
12021 friend_p = cp_parser_friend_p (decl_specifiers);
12022 /* If there were decl-specifiers, check to see if there was
12023 a class-declaration. */
12024 type = check_tag_decl (decl_specifiers);
12025 /* Nested classes have already been added to the class, but
12026 a `friend' needs to be explicitly registered. */
12027 if (friend_p)
12028 {
12029 /* If the `friend' keyword was present, the friend must
12030 be introduced with a class-key. */
12031 if (!declares_class_or_enum)
12032 error ("a class-key must be used when declaring a friend");
12033 /* In this case:
12034
12035 template <typename T> struct A {
12036 friend struct A<T>::B;
12037 };
12038
12039 A<T>::B will be represented by a TYPENAME_TYPE, and
12040 therefore not recognized by check_tag_decl. */
12041 if (!type)
12042 {
12043 tree specifier;
12044
12045 for (specifier = decl_specifiers;
12046 specifier;
12047 specifier = TREE_CHAIN (specifier))
12048 {
12049 tree s = TREE_VALUE (specifier);
12050
12051 if (TREE_CODE (s) == IDENTIFIER_NODE
12052 && IDENTIFIER_GLOBAL_VALUE (s))
12053 type = IDENTIFIER_GLOBAL_VALUE (s);
12054 if (TREE_CODE (s) == TYPE_DECL)
12055 s = TREE_TYPE (s);
12056 if (TYPE_P (s))
12057 {
12058 type = s;
12059 break;
12060 }
12061 }
12062 }
12063 if (!type)
12064 error ("friend declaration does not name a class or "
12065 "function");
12066 else
12067 make_friend_class (current_class_type, type);
12068 }
12069 /* If there is no TYPE, an error message will already have
12070 been issued. */
12071 else if (!type)
12072 ;
12073 /* An anonymous aggregate has to be handled specially; such
12074 a declaration really declares a data member (with a
12075 particular type), as opposed to a nested class. */
12076 else if (ANON_AGGR_TYPE_P (type))
12077 {
12078 /* Remove constructors and such from TYPE, now that we
12079 know it is an anoymous aggregate. */
12080 fixup_anonymous_aggr (type);
12081 /* And make the corresponding data member. */
12082 decl = build_decl (FIELD_DECL, NULL_TREE, type);
12083 /* Add it to the class. */
12084 finish_member_declaration (decl);
12085 }
12086 }
12087 }
12088 else
12089 {
12090 /* See if these declarations will be friends. */
12091 friend_p = cp_parser_friend_p (decl_specifiers);
12092
12093 /* Keep going until we hit the `;' at the end of the
12094 declaration. */
12095 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12096 {
12097 tree attributes = NULL_TREE;
12098 tree first_attribute;
12099
12100 /* Peek at the next token. */
12101 token = cp_lexer_peek_token (parser->lexer);
12102
12103 /* Check for a bitfield declaration. */
12104 if (token->type == CPP_COLON
12105 || (token->type == CPP_NAME
12106 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12107 == CPP_COLON))
12108 {
12109 tree identifier;
12110 tree width;
12111
12112 /* Get the name of the bitfield. Note that we cannot just
12113 check TOKEN here because it may have been invalidated by
12114 the call to cp_lexer_peek_nth_token above. */
12115 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12116 identifier = cp_parser_identifier (parser);
12117 else
12118 identifier = NULL_TREE;
12119
12120 /* Consume the `:' token. */
12121 cp_lexer_consume_token (parser->lexer);
12122 /* Get the width of the bitfield. */
14d22dd6
MM
12123 width
12124 = cp_parser_constant_expression (parser,
12125 /*allow_non_constant=*/false,
12126 NULL);
a723baf1
MM
12127
12128 /* Look for attributes that apply to the bitfield. */
12129 attributes = cp_parser_attributes_opt (parser);
12130 /* Remember which attributes are prefix attributes and
12131 which are not. */
12132 first_attribute = attributes;
12133 /* Combine the attributes. */
12134 attributes = chainon (prefix_attributes, attributes);
12135
12136 /* Create the bitfield declaration. */
12137 decl = grokbitfield (identifier,
12138 decl_specifiers,
12139 width);
12140 /* Apply the attributes. */
12141 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12142 }
12143 else
12144 {
12145 tree declarator;
12146 tree initializer;
12147 tree asm_specification;
12148 bool ctor_dtor_or_conv_p;
12149
12150 /* Parse the declarator. */
12151 declarator
62b8a44e 12152 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
a723baf1
MM
12153 &ctor_dtor_or_conv_p);
12154
12155 /* If something went wrong parsing the declarator, make sure
12156 that we at least consume some tokens. */
12157 if (declarator == error_mark_node)
12158 {
12159 /* Skip to the end of the statement. */
12160 cp_parser_skip_to_end_of_statement (parser);
12161 break;
12162 }
12163
12164 /* Look for an asm-specification. */
12165 asm_specification = cp_parser_asm_specification_opt (parser);
12166 /* Look for attributes that apply to the declaration. */
12167 attributes = cp_parser_attributes_opt (parser);
12168 /* Remember which attributes are prefix attributes and
12169 which are not. */
12170 first_attribute = attributes;
12171 /* Combine the attributes. */
12172 attributes = chainon (prefix_attributes, attributes);
12173
12174 /* If it's an `=', then we have a constant-initializer or a
12175 pure-specifier. It is not correct to parse the
12176 initializer before registering the member declaration
12177 since the member declaration should be in scope while
12178 its initializer is processed. However, the rest of the
12179 front end does not yet provide an interface that allows
12180 us to handle this correctly. */
12181 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12182 {
12183 /* In [class.mem]:
12184
12185 A pure-specifier shall be used only in the declaration of
12186 a virtual function.
12187
12188 A member-declarator can contain a constant-initializer
12189 only if it declares a static member of integral or
12190 enumeration type.
12191
12192 Therefore, if the DECLARATOR is for a function, we look
12193 for a pure-specifier; otherwise, we look for a
12194 constant-initializer. When we call `grokfield', it will
12195 perform more stringent semantics checks. */
12196 if (TREE_CODE (declarator) == CALL_EXPR)
12197 initializer = cp_parser_pure_specifier (parser);
12198 else
12199 {
12200 /* This declaration cannot be a function
12201 definition. */
12202 cp_parser_commit_to_tentative_parse (parser);
12203 /* Parse the initializer. */
12204 initializer = cp_parser_constant_initializer (parser);
12205 }
12206 }
12207 /* Otherwise, there is no initializer. */
12208 else
12209 initializer = NULL_TREE;
12210
12211 /* See if we are probably looking at a function
12212 definition. We are certainly not looking at at a
12213 member-declarator. Calling `grokfield' has
12214 side-effects, so we must not do it unless we are sure
12215 that we are looking at a member-declarator. */
12216 if (cp_parser_token_starts_function_definition_p
12217 (cp_lexer_peek_token (parser->lexer)))
12218 decl = error_mark_node;
12219 else
12220 /* Create the declaration. */
12221 decl = grokfield (declarator,
12222 decl_specifiers,
12223 initializer,
12224 asm_specification,
12225 attributes);
12226 }
12227
12228 /* Reset PREFIX_ATTRIBUTES. */
12229 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12230 attributes = TREE_CHAIN (attributes);
12231 if (attributes)
12232 TREE_CHAIN (attributes) = NULL_TREE;
12233
12234 /* If there is any qualification still in effect, clear it
12235 now; we will be starting fresh with the next declarator. */
12236 parser->scope = NULL_TREE;
12237 parser->qualifying_scope = NULL_TREE;
12238 parser->object_scope = NULL_TREE;
12239 /* If it's a `,', then there are more declarators. */
12240 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12241 cp_lexer_consume_token (parser->lexer);
12242 /* If the next token isn't a `;', then we have a parse error. */
12243 else if (cp_lexer_next_token_is_not (parser->lexer,
12244 CPP_SEMICOLON))
12245 {
12246 cp_parser_error (parser, "expected `;'");
12247 /* Skip tokens until we find a `;' */
12248 cp_parser_skip_to_end_of_statement (parser);
12249
12250 break;
12251 }
12252
12253 if (decl)
12254 {
12255 /* Add DECL to the list of members. */
12256 if (!friend_p)
12257 finish_member_declaration (decl);
12258
12259 /* If DECL is a function, we must return
12260 to parse it later. (Even though there is no definition,
12261 there might be default arguments that need handling.) */
12262 if (TREE_CODE (decl) == FUNCTION_DECL)
12263 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 12264 = tree_cons (NULL_TREE, decl,
a723baf1
MM
12265 TREE_VALUE (parser->unparsed_functions_queues));
12266 }
12267 }
12268 }
12269
12270 /* If everything went well, look for the `;'. */
12271 if (cp_parser_parse_definitely (parser))
12272 {
12273 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12274 return;
12275 }
12276
12277 /* Parse the function-definition. */
12278 decl = cp_parser_function_definition (parser, &friend_p);
12279 /* If the member was not a friend, declare it here. */
12280 if (!friend_p)
12281 finish_member_declaration (decl);
12282 /* Peek at the next token. */
12283 token = cp_lexer_peek_token (parser->lexer);
12284 /* If the next token is a semicolon, consume it. */
12285 if (token->type == CPP_SEMICOLON)
12286 cp_lexer_consume_token (parser->lexer);
12287}
12288
12289/* Parse a pure-specifier.
12290
12291 pure-specifier:
12292 = 0
12293
12294 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12295 Otherwiser, ERROR_MARK_NODE is returned. */
12296
12297static tree
94edc4ab 12298cp_parser_pure_specifier (cp_parser* parser)
a723baf1
MM
12299{
12300 cp_token *token;
12301
12302 /* Look for the `=' token. */
12303 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12304 return error_mark_node;
12305 /* Look for the `0' token. */
12306 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12307 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12308 to get information from the lexer about how the number was
12309 spelled in order to fix this problem. */
12310 if (!token || !integer_zerop (token->value))
12311 return error_mark_node;
12312
12313 return integer_zero_node;
12314}
12315
12316/* Parse a constant-initializer.
12317
12318 constant-initializer:
12319 = constant-expression
12320
12321 Returns a representation of the constant-expression. */
12322
12323static tree
94edc4ab 12324cp_parser_constant_initializer (cp_parser* parser)
a723baf1
MM
12325{
12326 /* Look for the `=' token. */
12327 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12328 return error_mark_node;
12329
12330 /* It is invalid to write:
12331
12332 struct S { static const int i = { 7 }; };
12333
12334 */
12335 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12336 {
12337 cp_parser_error (parser,
12338 "a brace-enclosed initializer is not allowed here");
12339 /* Consume the opening brace. */
12340 cp_lexer_consume_token (parser->lexer);
12341 /* Skip the initializer. */
12342 cp_parser_skip_to_closing_brace (parser);
12343 /* Look for the trailing `}'. */
12344 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12345
12346 return error_mark_node;
12347 }
12348
14d22dd6
MM
12349 return cp_parser_constant_expression (parser,
12350 /*allow_non_constant=*/false,
12351 NULL);
a723baf1
MM
12352}
12353
12354/* Derived classes [gram.class.derived] */
12355
12356/* Parse a base-clause.
12357
12358 base-clause:
12359 : base-specifier-list
12360
12361 base-specifier-list:
12362 base-specifier
12363 base-specifier-list , base-specifier
12364
12365 Returns a TREE_LIST representing the base-classes, in the order in
12366 which they were declared. The representation of each node is as
12367 described by cp_parser_base_specifier.
12368
12369 In the case that no bases are specified, this function will return
12370 NULL_TREE, not ERROR_MARK_NODE. */
12371
12372static tree
94edc4ab 12373cp_parser_base_clause (cp_parser* parser)
a723baf1
MM
12374{
12375 tree bases = NULL_TREE;
12376
12377 /* Look for the `:' that begins the list. */
12378 cp_parser_require (parser, CPP_COLON, "`:'");
12379
12380 /* Scan the base-specifier-list. */
12381 while (true)
12382 {
12383 cp_token *token;
12384 tree base;
12385
12386 /* Look for the base-specifier. */
12387 base = cp_parser_base_specifier (parser);
12388 /* Add BASE to the front of the list. */
12389 if (base != error_mark_node)
12390 {
12391 TREE_CHAIN (base) = bases;
12392 bases = base;
12393 }
12394 /* Peek at the next token. */
12395 token = cp_lexer_peek_token (parser->lexer);
12396 /* If it's not a comma, then the list is complete. */
12397 if (token->type != CPP_COMMA)
12398 break;
12399 /* Consume the `,'. */
12400 cp_lexer_consume_token (parser->lexer);
12401 }
12402
12403 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12404 base class had a qualified name. However, the next name that
12405 appears is certainly not qualified. */
12406 parser->scope = NULL_TREE;
12407 parser->qualifying_scope = NULL_TREE;
12408 parser->object_scope = NULL_TREE;
12409
12410 return nreverse (bases);
12411}
12412
12413/* Parse a base-specifier.
12414
12415 base-specifier:
12416 :: [opt] nested-name-specifier [opt] class-name
12417 virtual access-specifier [opt] :: [opt] nested-name-specifier
12418 [opt] class-name
12419 access-specifier virtual [opt] :: [opt] nested-name-specifier
12420 [opt] class-name
12421
12422 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12423 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12424 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12425 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12426
12427static tree
94edc4ab 12428cp_parser_base_specifier (cp_parser* parser)
a723baf1
MM
12429{
12430 cp_token *token;
12431 bool done = false;
12432 bool virtual_p = false;
12433 bool duplicate_virtual_error_issued_p = false;
12434 bool duplicate_access_error_issued_p = false;
bbaab916 12435 bool class_scope_p, template_p;
dbbf88d1 12436 tree access = access_default_node;
a723baf1
MM
12437 tree type;
12438
12439 /* Process the optional `virtual' and `access-specifier'. */
12440 while (!done)
12441 {
12442 /* Peek at the next token. */
12443 token = cp_lexer_peek_token (parser->lexer);
12444 /* Process `virtual'. */
12445 switch (token->keyword)
12446 {
12447 case RID_VIRTUAL:
12448 /* If `virtual' appears more than once, issue an error. */
12449 if (virtual_p && !duplicate_virtual_error_issued_p)
12450 {
12451 cp_parser_error (parser,
12452 "`virtual' specified more than once in base-specified");
12453 duplicate_virtual_error_issued_p = true;
12454 }
12455
12456 virtual_p = true;
12457
12458 /* Consume the `virtual' token. */
12459 cp_lexer_consume_token (parser->lexer);
12460
12461 break;
12462
12463 case RID_PUBLIC:
12464 case RID_PROTECTED:
12465 case RID_PRIVATE:
12466 /* If more than one access specifier appears, issue an
12467 error. */
dbbf88d1
NS
12468 if (access != access_default_node
12469 && !duplicate_access_error_issued_p)
a723baf1
MM
12470 {
12471 cp_parser_error (parser,
12472 "more than one access specifier in base-specified");
12473 duplicate_access_error_issued_p = true;
12474 }
12475
dbbf88d1 12476 access = ridpointers[(int) token->keyword];
a723baf1
MM
12477
12478 /* Consume the access-specifier. */
12479 cp_lexer_consume_token (parser->lexer);
12480
12481 break;
12482
12483 default:
12484 done = true;
12485 break;
12486 }
12487 }
12488
a723baf1
MM
12489 /* Look for the optional `::' operator. */
12490 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12491 /* Look for the nested-name-specifier. The simplest way to
12492 implement:
12493
12494 [temp.res]
12495
12496 The keyword `typename' is not permitted in a base-specifier or
12497 mem-initializer; in these contexts a qualified name that
12498 depends on a template-parameter is implicitly assumed to be a
12499 type name.
12500
12501 is to pretend that we have seen the `typename' keyword at this
12502 point. */
12503 cp_parser_nested_name_specifier_opt (parser,
12504 /*typename_keyword_p=*/true,
12505 /*check_dependency_p=*/true,
12506 /*type_p=*/true);
12507 /* If the base class is given by a qualified name, assume that names
12508 we see are type names or templates, as appropriate. */
12509 class_scope_p = (parser->scope && TYPE_P (parser->scope));
bbaab916
NS
12510 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12511
a723baf1
MM
12512 /* Finally, look for the class-name. */
12513 type = cp_parser_class_name (parser,
12514 class_scope_p,
bbaab916 12515 template_p,
a723baf1
MM
12516 /*type_p=*/true,
12517 /*check_access=*/true,
12518 /*check_dependency_p=*/true,
12519 /*class_head_p=*/false);
12520
12521 if (type == error_mark_node)
12522 return error_mark_node;
12523
dbbf88d1 12524 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
a723baf1
MM
12525}
12526
12527/* Exception handling [gram.exception] */
12528
12529/* Parse an (optional) exception-specification.
12530
12531 exception-specification:
12532 throw ( type-id-list [opt] )
12533
12534 Returns a TREE_LIST representing the exception-specification. The
12535 TREE_VALUE of each node is a type. */
12536
12537static tree
94edc4ab 12538cp_parser_exception_specification_opt (cp_parser* parser)
a723baf1
MM
12539{
12540 cp_token *token;
12541 tree type_id_list;
12542
12543 /* Peek at the next token. */
12544 token = cp_lexer_peek_token (parser->lexer);
12545 /* If it's not `throw', then there's no exception-specification. */
12546 if (!cp_parser_is_keyword (token, RID_THROW))
12547 return NULL_TREE;
12548
12549 /* Consume the `throw'. */
12550 cp_lexer_consume_token (parser->lexer);
12551
12552 /* Look for the `('. */
12553 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12554
12555 /* Peek at the next token. */
12556 token = cp_lexer_peek_token (parser->lexer);
12557 /* If it's not a `)', then there is a type-id-list. */
12558 if (token->type != CPP_CLOSE_PAREN)
12559 {
12560 const char *saved_message;
12561
12562 /* Types may not be defined in an exception-specification. */
12563 saved_message = parser->type_definition_forbidden_message;
12564 parser->type_definition_forbidden_message
12565 = "types may not be defined in an exception-specification";
12566 /* Parse the type-id-list. */
12567 type_id_list = cp_parser_type_id_list (parser);
12568 /* Restore the saved message. */
12569 parser->type_definition_forbidden_message = saved_message;
12570 }
12571 else
12572 type_id_list = empty_except_spec;
12573
12574 /* Look for the `)'. */
12575 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12576
12577 return type_id_list;
12578}
12579
12580/* Parse an (optional) type-id-list.
12581
12582 type-id-list:
12583 type-id
12584 type-id-list , type-id
12585
12586 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12587 in the order that the types were presented. */
12588
12589static tree
94edc4ab 12590cp_parser_type_id_list (cp_parser* parser)
a723baf1
MM
12591{
12592 tree types = NULL_TREE;
12593
12594 while (true)
12595 {
12596 cp_token *token;
12597 tree type;
12598
12599 /* Get the next type-id. */
12600 type = cp_parser_type_id (parser);
12601 /* Add it to the list. */
12602 types = add_exception_specifier (types, type, /*complain=*/1);
12603 /* Peek at the next token. */
12604 token = cp_lexer_peek_token (parser->lexer);
12605 /* If it is not a `,', we are done. */
12606 if (token->type != CPP_COMMA)
12607 break;
12608 /* Consume the `,'. */
12609 cp_lexer_consume_token (parser->lexer);
12610 }
12611
12612 return nreverse (types);
12613}
12614
12615/* Parse a try-block.
12616
12617 try-block:
12618 try compound-statement handler-seq */
12619
12620static tree
94edc4ab 12621cp_parser_try_block (cp_parser* parser)
a723baf1
MM
12622{
12623 tree try_block;
12624
12625 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12626 try_block = begin_try_block ();
12627 cp_parser_compound_statement (parser);
12628 finish_try_block (try_block);
12629 cp_parser_handler_seq (parser);
12630 finish_handler_sequence (try_block);
12631
12632 return try_block;
12633}
12634
12635/* Parse a function-try-block.
12636
12637 function-try-block:
12638 try ctor-initializer [opt] function-body handler-seq */
12639
12640static bool
94edc4ab 12641cp_parser_function_try_block (cp_parser* parser)
a723baf1
MM
12642{
12643 tree try_block;
12644 bool ctor_initializer_p;
12645
12646 /* Look for the `try' keyword. */
12647 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12648 return false;
12649 /* Let the rest of the front-end know where we are. */
12650 try_block = begin_function_try_block ();
12651 /* Parse the function-body. */
12652 ctor_initializer_p
12653 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12654 /* We're done with the `try' part. */
12655 finish_function_try_block (try_block);
12656 /* Parse the handlers. */
12657 cp_parser_handler_seq (parser);
12658 /* We're done with the handlers. */
12659 finish_function_handler_sequence (try_block);
12660
12661 return ctor_initializer_p;
12662}
12663
12664/* Parse a handler-seq.
12665
12666 handler-seq:
12667 handler handler-seq [opt] */
12668
12669static void
94edc4ab 12670cp_parser_handler_seq (cp_parser* parser)
a723baf1
MM
12671{
12672 while (true)
12673 {
12674 cp_token *token;
12675
12676 /* Parse the handler. */
12677 cp_parser_handler (parser);
12678 /* Peek at the next token. */
12679 token = cp_lexer_peek_token (parser->lexer);
12680 /* If it's not `catch' then there are no more handlers. */
12681 if (!cp_parser_is_keyword (token, RID_CATCH))
12682 break;
12683 }
12684}
12685
12686/* Parse a handler.
12687
12688 handler:
12689 catch ( exception-declaration ) compound-statement */
12690
12691static void
94edc4ab 12692cp_parser_handler (cp_parser* parser)
a723baf1
MM
12693{
12694 tree handler;
12695 tree declaration;
12696
12697 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12698 handler = begin_handler ();
12699 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12700 declaration = cp_parser_exception_declaration (parser);
12701 finish_handler_parms (declaration, handler);
12702 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12703 cp_parser_compound_statement (parser);
12704 finish_handler (handler);
12705}
12706
12707/* Parse an exception-declaration.
12708
12709 exception-declaration:
12710 type-specifier-seq declarator
12711 type-specifier-seq abstract-declarator
12712 type-specifier-seq
12713 ...
12714
12715 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12716 ellipsis variant is used. */
12717
12718static tree
94edc4ab 12719cp_parser_exception_declaration (cp_parser* parser)
a723baf1
MM
12720{
12721 tree type_specifiers;
12722 tree declarator;
12723 const char *saved_message;
12724
12725 /* If it's an ellipsis, it's easy to handle. */
12726 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12727 {
12728 /* Consume the `...' token. */
12729 cp_lexer_consume_token (parser->lexer);
12730 return NULL_TREE;
12731 }
12732
12733 /* Types may not be defined in exception-declarations. */
12734 saved_message = parser->type_definition_forbidden_message;
12735 parser->type_definition_forbidden_message
12736 = "types may not be defined in exception-declarations";
12737
12738 /* Parse the type-specifier-seq. */
12739 type_specifiers = cp_parser_type_specifier_seq (parser);
12740 /* If it's a `)', then there is no declarator. */
12741 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12742 declarator = NULL_TREE;
12743 else
62b8a44e
NS
12744 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12745 /*ctor_dtor_or_conv_p=*/NULL);
a723baf1
MM
12746
12747 /* Restore the saved message. */
12748 parser->type_definition_forbidden_message = saved_message;
12749
12750 return start_handler_parms (type_specifiers, declarator);
12751}
12752
12753/* Parse a throw-expression.
12754
12755 throw-expression:
12756 throw assignment-expresion [opt]
12757
12758 Returns a THROW_EXPR representing the throw-expression. */
12759
12760static tree
94edc4ab 12761cp_parser_throw_expression (cp_parser* parser)
a723baf1
MM
12762{
12763 tree expression;
12764
12765 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12766 /* We can't be sure if there is an assignment-expression or not. */
12767 cp_parser_parse_tentatively (parser);
12768 /* Try it. */
12769 expression = cp_parser_assignment_expression (parser);
12770 /* If it didn't work, this is just a rethrow. */
12771 if (!cp_parser_parse_definitely (parser))
12772 expression = NULL_TREE;
12773
12774 return build_throw (expression);
12775}
12776
12777/* GNU Extensions */
12778
12779/* Parse an (optional) asm-specification.
12780
12781 asm-specification:
12782 asm ( string-literal )
12783
12784 If the asm-specification is present, returns a STRING_CST
12785 corresponding to the string-literal. Otherwise, returns
12786 NULL_TREE. */
12787
12788static tree
94edc4ab 12789cp_parser_asm_specification_opt (cp_parser* parser)
a723baf1
MM
12790{
12791 cp_token *token;
12792 tree asm_specification;
12793
12794 /* Peek at the next token. */
12795 token = cp_lexer_peek_token (parser->lexer);
12796 /* If the next token isn't the `asm' keyword, then there's no
12797 asm-specification. */
12798 if (!cp_parser_is_keyword (token, RID_ASM))
12799 return NULL_TREE;
12800
12801 /* Consume the `asm' token. */
12802 cp_lexer_consume_token (parser->lexer);
12803 /* Look for the `('. */
12804 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12805
12806 /* Look for the string-literal. */
12807 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12808 if (token)
12809 asm_specification = token->value;
12810 else
12811 asm_specification = NULL_TREE;
12812
12813 /* Look for the `)'. */
12814 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12815
12816 return asm_specification;
12817}
12818
12819/* Parse an asm-operand-list.
12820
12821 asm-operand-list:
12822 asm-operand
12823 asm-operand-list , asm-operand
12824
12825 asm-operand:
12826 string-literal ( expression )
12827 [ string-literal ] string-literal ( expression )
12828
12829 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12830 each node is the expression. The TREE_PURPOSE is itself a
12831 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12832 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12833 is a STRING_CST for the string literal before the parenthesis. */
12834
12835static tree
94edc4ab 12836cp_parser_asm_operand_list (cp_parser* parser)
a723baf1
MM
12837{
12838 tree asm_operands = NULL_TREE;
12839
12840 while (true)
12841 {
12842 tree string_literal;
12843 tree expression;
12844 tree name;
12845 cp_token *token;
12846
12847 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12848 {
12849 /* Consume the `[' token. */
12850 cp_lexer_consume_token (parser->lexer);
12851 /* Read the operand name. */
12852 name = cp_parser_identifier (parser);
12853 if (name != error_mark_node)
12854 name = build_string (IDENTIFIER_LENGTH (name),
12855 IDENTIFIER_POINTER (name));
12856 /* Look for the closing `]'. */
12857 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12858 }
12859 else
12860 name = NULL_TREE;
12861 /* Look for the string-literal. */
12862 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12863 string_literal = token ? token->value : error_mark_node;
12864 /* Look for the `('. */
12865 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12866 /* Parse the expression. */
12867 expression = cp_parser_expression (parser);
12868 /* Look for the `)'. */
12869 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12870 /* Add this operand to the list. */
12871 asm_operands = tree_cons (build_tree_list (name, string_literal),
12872 expression,
12873 asm_operands);
12874 /* If the next token is not a `,', there are no more
12875 operands. */
12876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12877 break;
12878 /* Consume the `,'. */
12879 cp_lexer_consume_token (parser->lexer);
12880 }
12881
12882 return nreverse (asm_operands);
12883}
12884
12885/* Parse an asm-clobber-list.
12886
12887 asm-clobber-list:
12888 string-literal
12889 asm-clobber-list , string-literal
12890
12891 Returns a TREE_LIST, indicating the clobbers in the order that they
12892 appeared. The TREE_VALUE of each node is a STRING_CST. */
12893
12894static tree
94edc4ab 12895cp_parser_asm_clobber_list (cp_parser* parser)
a723baf1
MM
12896{
12897 tree clobbers = NULL_TREE;
12898
12899 while (true)
12900 {
12901 cp_token *token;
12902 tree string_literal;
12903
12904 /* Look for the string literal. */
12905 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12906 string_literal = token ? token->value : error_mark_node;
12907 /* Add it to the list. */
12908 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12909 /* If the next token is not a `,', then the list is
12910 complete. */
12911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12912 break;
12913 /* Consume the `,' token. */
12914 cp_lexer_consume_token (parser->lexer);
12915 }
12916
12917 return clobbers;
12918}
12919
12920/* Parse an (optional) series of attributes.
12921
12922 attributes:
12923 attributes attribute
12924
12925 attribute:
12926 __attribute__ (( attribute-list [opt] ))
12927
12928 The return value is as for cp_parser_attribute_list. */
12929
12930static tree
94edc4ab 12931cp_parser_attributes_opt (cp_parser* parser)
a723baf1
MM
12932{
12933 tree attributes = NULL_TREE;
12934
12935 while (true)
12936 {
12937 cp_token *token;
12938 tree attribute_list;
12939
12940 /* Peek at the next token. */
12941 token = cp_lexer_peek_token (parser->lexer);
12942 /* If it's not `__attribute__', then we're done. */
12943 if (token->keyword != RID_ATTRIBUTE)
12944 break;
12945
12946 /* Consume the `__attribute__' keyword. */
12947 cp_lexer_consume_token (parser->lexer);
12948 /* Look for the two `(' tokens. */
12949 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12950 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12951
12952 /* Peek at the next token. */
12953 token = cp_lexer_peek_token (parser->lexer);
12954 if (token->type != CPP_CLOSE_PAREN)
12955 /* Parse the attribute-list. */
12956 attribute_list = cp_parser_attribute_list (parser);
12957 else
12958 /* If the next token is a `)', then there is no attribute
12959 list. */
12960 attribute_list = NULL;
12961
12962 /* Look for the two `)' tokens. */
12963 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12964 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12965
12966 /* Add these new attributes to the list. */
12967 attributes = chainon (attributes, attribute_list);
12968 }
12969
12970 return attributes;
12971}
12972
12973/* Parse an attribute-list.
12974
12975 attribute-list:
12976 attribute
12977 attribute-list , attribute
12978
12979 attribute:
12980 identifier
12981 identifier ( identifier )
12982 identifier ( identifier , expression-list )
12983 identifier ( expression-list )
12984
12985 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12986 TREE_PURPOSE of each node is the identifier indicating which
12987 attribute is in use. The TREE_VALUE represents the arguments, if
12988 any. */
12989
12990static tree
94edc4ab 12991cp_parser_attribute_list (cp_parser* parser)
a723baf1
MM
12992{
12993 tree attribute_list = NULL_TREE;
12994
12995 while (true)
12996 {
12997 cp_token *token;
12998 tree identifier;
12999 tree attribute;
13000
13001 /* Look for the identifier. We also allow keywords here; for
13002 example `__attribute__ ((const))' is legal. */
13003 token = cp_lexer_peek_token (parser->lexer);
13004 if (token->type != CPP_NAME
13005 && token->type != CPP_KEYWORD)
13006 return error_mark_node;
13007 /* Consume the token. */
13008 token = cp_lexer_consume_token (parser->lexer);
13009
13010 /* Save away the identifier that indicates which attribute this is. */
13011 identifier = token->value;
13012 attribute = build_tree_list (identifier, NULL_TREE);
13013
13014 /* Peek at the next token. */
13015 token = cp_lexer_peek_token (parser->lexer);
13016 /* If it's an `(', then parse the attribute arguments. */
13017 if (token->type == CPP_OPEN_PAREN)
13018 {
13019 tree arguments;
13020 int arguments_allowed_p = 1;
13021
13022 /* Consume the `('. */
13023 cp_lexer_consume_token (parser->lexer);
13024 /* Peek at the next token. */
13025 token = cp_lexer_peek_token (parser->lexer);
13026 /* Check to see if the next token is an identifier. */
13027 if (token->type == CPP_NAME)
13028 {
13029 /* Save the identifier. */
13030 identifier = token->value;
13031 /* Consume the identifier. */
13032 cp_lexer_consume_token (parser->lexer);
13033 /* Peek at the next token. */
13034 token = cp_lexer_peek_token (parser->lexer);
13035 /* If the next token is a `,', then there are some other
13036 expressions as well. */
13037 if (token->type == CPP_COMMA)
13038 /* Consume the comma. */
13039 cp_lexer_consume_token (parser->lexer);
13040 else
13041 arguments_allowed_p = 0;
13042 }
13043 else
13044 identifier = NULL_TREE;
13045
13046 /* If there are arguments, parse them too. */
13047 if (arguments_allowed_p)
13048 arguments = cp_parser_expression_list (parser);
13049 else
13050 arguments = NULL_TREE;
13051
13052 /* Combine the identifier and the arguments. */
13053 if (identifier)
13054 arguments = tree_cons (NULL_TREE, identifier, arguments);
13055
13056 /* Save the identifier and arguments away. */
13057 TREE_VALUE (attribute) = arguments;
13058
13059 /* Look for the closing `)'. */
13060 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13061 }
13062
13063 /* Add this attribute to the list. */
13064 TREE_CHAIN (attribute) = attribute_list;
13065 attribute_list = attribute;
13066
13067 /* Now, look for more attributes. */
13068 token = cp_lexer_peek_token (parser->lexer);
13069 /* If the next token isn't a `,', we're done. */
13070 if (token->type != CPP_COMMA)
13071 break;
13072
13073 /* Consume the commma and keep going. */
13074 cp_lexer_consume_token (parser->lexer);
13075 }
13076
13077 /* We built up the list in reverse order. */
13078 return nreverse (attribute_list);
13079}
13080
13081/* Parse an optional `__extension__' keyword. Returns TRUE if it is
13082 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
13083 current value of the PEDANTIC flag, regardless of whether or not
13084 the `__extension__' keyword is present. The caller is responsible
13085 for restoring the value of the PEDANTIC flag. */
13086
13087static bool
94edc4ab 13088cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
a723baf1
MM
13089{
13090 /* Save the old value of the PEDANTIC flag. */
13091 *saved_pedantic = pedantic;
13092
13093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13094 {
13095 /* Consume the `__extension__' token. */
13096 cp_lexer_consume_token (parser->lexer);
13097 /* We're not being pedantic while the `__extension__' keyword is
13098 in effect. */
13099 pedantic = 0;
13100
13101 return true;
13102 }
13103
13104 return false;
13105}
13106
13107/* Parse a label declaration.
13108
13109 label-declaration:
13110 __label__ label-declarator-seq ;
13111
13112 label-declarator-seq:
13113 identifier , label-declarator-seq
13114 identifier */
13115
13116static void
94edc4ab 13117cp_parser_label_declaration (cp_parser* parser)
a723baf1
MM
13118{
13119 /* Look for the `__label__' keyword. */
13120 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13121
13122 while (true)
13123 {
13124 tree identifier;
13125
13126 /* Look for an identifier. */
13127 identifier = cp_parser_identifier (parser);
13128 /* Declare it as a lobel. */
13129 finish_label_decl (identifier);
13130 /* If the next token is a `;', stop. */
13131 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13132 break;
13133 /* Look for the `,' separating the label declarations. */
13134 cp_parser_require (parser, CPP_COMMA, "`,'");
13135 }
13136
13137 /* Look for the final `;'. */
13138 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13139}
13140
13141/* Support Functions */
13142
13143/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13144 NAME should have one of the representations used for an
13145 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13146 is returned. If PARSER->SCOPE is a dependent type, then a
13147 SCOPE_REF is returned.
13148
13149 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13150 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13151 was formed. Abstractly, such entities should not be passed to this
13152 function, because they do not need to be looked up, but it is
13153 simpler to check for this special case here, rather than at the
13154 call-sites.
13155
13156 In cases not explicitly covered above, this function returns a
13157 DECL, OVERLOAD, or baselink representing the result of the lookup.
13158 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13159 is returned.
13160
13161 If CHECK_ACCESS is TRUE, then access control is performed on the
13162 declaration to which the name resolves, and an error message is
13163 issued if the declaration is inaccessible.
13164
13165 If IS_TYPE is TRUE, bindings that do not refer to types are
13166 ignored.
13167
eea9800f
MM
13168 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13169 are ignored.
13170
a723baf1
MM
13171 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13172 types. */
13173
13174static tree
eea9800f
MM
13175cp_parser_lookup_name (cp_parser *parser, tree name, bool check_access,
13176 bool is_type, bool is_namespace, bool check_dependency)
a723baf1
MM
13177{
13178 tree decl;
13179 tree object_type = parser->context->object_type;
13180
13181 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13182 no longer valid. Note that if we are parsing tentatively, and
13183 the parse fails, OBJECT_TYPE will be automatically restored. */
13184 parser->context->object_type = NULL_TREE;
13185
13186 if (name == error_mark_node)
13187 return error_mark_node;
13188
13189 /* A template-id has already been resolved; there is no lookup to
13190 do. */
13191 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13192 return name;
13193 if (BASELINK_P (name))
13194 {
13195 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13196 == TEMPLATE_ID_EXPR),
13197 20020909);
13198 return name;
13199 }
13200
13201 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13202 it should already have been checked to make sure that the name
13203 used matches the type being destroyed. */
13204 if (TREE_CODE (name) == BIT_NOT_EXPR)
13205 {
13206 tree type;
13207
13208 /* Figure out to which type this destructor applies. */
13209 if (parser->scope)
13210 type = parser->scope;
13211 else if (object_type)
13212 type = object_type;
13213 else
13214 type = current_class_type;
13215 /* If that's not a class type, there is no destructor. */
13216 if (!type || !CLASS_TYPE_P (type))
13217 return error_mark_node;
13218 /* If it was a class type, return the destructor. */
13219 return CLASSTYPE_DESTRUCTORS (type);
13220 }
13221
13222 /* By this point, the NAME should be an ordinary identifier. If
13223 the id-expression was a qualified name, the qualifying scope is
13224 stored in PARSER->SCOPE at this point. */
13225 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13226 20000619);
13227
13228 /* Perform the lookup. */
13229 if (parser->scope)
13230 {
1fb3244a 13231 bool dependent_p;
a723baf1
MM
13232
13233 if (parser->scope == error_mark_node)
13234 return error_mark_node;
13235
13236 /* If the SCOPE is dependent, the lookup must be deferred until
13237 the template is instantiated -- unless we are explicitly
13238 looking up names in uninstantiated templates. Even then, we
13239 cannot look up the name if the scope is not a class type; it
13240 might, for example, be a template type parameter. */
1fb3244a
MM
13241 dependent_p = (TYPE_P (parser->scope)
13242 && !(parser->in_declarator_p
13243 && currently_open_class (parser->scope))
13244 && dependent_type_p (parser->scope));
a723baf1 13245 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
1fb3244a 13246 && dependent_p)
a723baf1
MM
13247 {
13248 if (!is_type)
13249 decl = build_nt (SCOPE_REF, parser->scope, name);
13250 else
13251 /* The resolution to Core Issue 180 says that `struct A::B'
13252 should be considered a type-name, even if `A' is
13253 dependent. */
13254 decl = TYPE_NAME (make_typename_type (parser->scope,
13255 name,
13256 /*complain=*/1));
13257 }
13258 else
13259 {
13260 /* If PARSER->SCOPE is a dependent type, then it must be a
13261 class type, and we must not be checking dependencies;
13262 otherwise, we would have processed this lookup above. So
13263 that PARSER->SCOPE is not considered a dependent base by
13264 lookup_member, we must enter the scope here. */
1fb3244a 13265 if (dependent_p)
a723baf1
MM
13266 push_scope (parser->scope);
13267 /* If the PARSER->SCOPE is a a template specialization, it
13268 may be instantiated during name lookup. In that case,
13269 errors may be issued. Even if we rollback the current
13270 tentative parse, those errors are valid. */
13271 decl = lookup_qualified_name (parser->scope, name, is_type,
13272 /*flags=*/0);
1fb3244a 13273 if (dependent_p)
a723baf1
MM
13274 pop_scope (parser->scope);
13275 }
13276 parser->qualifying_scope = parser->scope;
13277 parser->object_scope = NULL_TREE;
13278 }
13279 else if (object_type)
13280 {
13281 tree object_decl = NULL_TREE;
13282 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13283 OBJECT_TYPE is not a class. */
13284 if (CLASS_TYPE_P (object_type))
13285 /* If the OBJECT_TYPE is a template specialization, it may
13286 be instantiated during name lookup. In that case, errors
13287 may be issued. Even if we rollback the current tentative
13288 parse, those errors are valid. */
13289 object_decl = lookup_member (object_type,
13290 name,
13291 /*protect=*/0, is_type);
13292 /* Look it up in the enclosing context, too. */
13293 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13294 is_namespace,
a723baf1
MM
13295 /*flags=*/0);
13296 parser->object_scope = object_type;
13297 parser->qualifying_scope = NULL_TREE;
13298 if (object_decl)
13299 decl = object_decl;
13300 }
13301 else
13302 {
13303 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
eea9800f 13304 is_namespace,
a723baf1
MM
13305 /*flags=*/0);
13306 parser->qualifying_scope = NULL_TREE;
13307 parser->object_scope = NULL_TREE;
13308 }
13309
13310 /* If the lookup failed, let our caller know. */
13311 if (!decl
13312 || decl == error_mark_node
13313 || (TREE_CODE (decl) == FUNCTION_DECL
13314 && DECL_ANTICIPATED (decl)))
13315 return error_mark_node;
13316
13317 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13318 if (TREE_CODE (decl) == TREE_LIST)
13319 {
13320 /* The error message we have to print is too complicated for
13321 cp_parser_error, so we incorporate its actions directly. */
e5976695 13322 if (!cp_parser_simulate_error (parser))
a723baf1
MM
13323 {
13324 error ("reference to `%D' is ambiguous", name);
13325 print_candidates (decl);
13326 }
13327 return error_mark_node;
13328 }
13329
13330 my_friendly_assert (DECL_P (decl)
13331 || TREE_CODE (decl) == OVERLOAD
13332 || TREE_CODE (decl) == SCOPE_REF
13333 || BASELINK_P (decl),
13334 20000619);
13335
13336 /* If we have resolved the name of a member declaration, check to
13337 see if the declaration is accessible. When the name resolves to
13338 set of overloaded functions, accesibility is checked when
13339 overload resolution is done.
13340
13341 During an explicit instantiation, access is not checked at all,
13342 as per [temp.explicit]. */
13343 if (check_access && scope_chain->check_access && DECL_P (decl))
13344 {
13345 tree qualifying_type;
13346
13347 /* Figure out the type through which DECL is being
13348 accessed. */
13349 qualifying_type
13350 = cp_parser_scope_through_which_access_occurs (decl,
13351 object_type,
13352 parser->scope);
13353 if (qualifying_type)
cf22909c 13354 perform_or_defer_access_check (qualifying_type, decl);
a723baf1
MM
13355 }
13356
13357 return decl;
13358}
13359
13360/* Like cp_parser_lookup_name, but for use in the typical case where
13361 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13362 TRUE. */
13363
13364static tree
94edc4ab 13365cp_parser_lookup_name_simple (cp_parser* parser, tree name)
a723baf1
MM
13366{
13367 return cp_parser_lookup_name (parser, name,
13368 /*check_access=*/true,
eea9800f
MM
13369 /*is_type=*/false,
13370 /*is_namespace=*/false,
a723baf1
MM
13371 /*check_dependency=*/true);
13372}
13373
a723baf1
MM
13374/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13375 the current context, return the TYPE_DECL. If TAG_NAME_P is
13376 true, the DECL indicates the class being defined in a class-head,
13377 or declared in an elaborated-type-specifier.
13378
13379 Otherwise, return DECL. */
13380
13381static tree
13382cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13383{
710b73e6
KL
13384 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13385 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
a723baf1
MM
13386
13387 struct A {
13388 template <typename T> struct B;
13389 };
13390
13391 template <typename T> struct A::B {};
13392
13393 Similarly, in a elaborated-type-specifier:
13394
13395 namespace N { struct X{}; }
13396
13397 struct A {
13398 template <typename T> friend struct N::X;
13399 };
13400
710b73e6
KL
13401 However, if the DECL refers to a class type, and we are in
13402 the scope of the class, then the name lookup automatically
13403 finds the TYPE_DECL created by build_self_reference rather
13404 than a TEMPLATE_DECL. For example, in:
13405
13406 template <class T> struct S {
13407 S s;
13408 };
13409
13410 there is no need to handle such case. */
13411
13412 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
a723baf1
MM
13413 return DECL_TEMPLATE_RESULT (decl);
13414
13415 return decl;
13416}
13417
13418/* If too many, or too few, template-parameter lists apply to the
13419 declarator, issue an error message. Returns TRUE if all went well,
13420 and FALSE otherwise. */
13421
13422static bool
94edc4ab
NN
13423cp_parser_check_declarator_template_parameters (cp_parser* parser,
13424 tree declarator)
a723baf1
MM
13425{
13426 unsigned num_templates;
13427
13428 /* We haven't seen any classes that involve template parameters yet. */
13429 num_templates = 0;
13430
13431 switch (TREE_CODE (declarator))
13432 {
13433 case CALL_EXPR:
13434 case ARRAY_REF:
13435 case INDIRECT_REF:
13436 case ADDR_EXPR:
13437 {
13438 tree main_declarator = TREE_OPERAND (declarator, 0);
13439 return
13440 cp_parser_check_declarator_template_parameters (parser,
13441 main_declarator);
13442 }
13443
13444 case SCOPE_REF:
13445 {
13446 tree scope;
13447 tree member;
13448
13449 scope = TREE_OPERAND (declarator, 0);
13450 member = TREE_OPERAND (declarator, 1);
13451
13452 /* If this is a pointer-to-member, then we are not interested
13453 in the SCOPE, because it does not qualify the thing that is
13454 being declared. */
13455 if (TREE_CODE (member) == INDIRECT_REF)
13456 return (cp_parser_check_declarator_template_parameters
13457 (parser, member));
13458
13459 while (scope && CLASS_TYPE_P (scope))
13460 {
13461 /* You're supposed to have one `template <...>'
13462 for every template class, but you don't need one
13463 for a full specialization. For example:
13464
13465 template <class T> struct S{};
13466 template <> struct S<int> { void f(); };
13467 void S<int>::f () {}
13468
13469 is correct; there shouldn't be a `template <>' for
13470 the definition of `S<int>::f'. */
13471 if (CLASSTYPE_TEMPLATE_INFO (scope)
13472 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13473 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13474 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13475 ++num_templates;
13476
13477 scope = TYPE_CONTEXT (scope);
13478 }
13479 }
13480
13481 /* Fall through. */
13482
13483 default:
13484 /* If the DECLARATOR has the form `X<y>' then it uses one
13485 additional level of template parameters. */
13486 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13487 ++num_templates;
13488
13489 return cp_parser_check_template_parameters (parser,
13490 num_templates);
13491 }
13492}
13493
13494/* NUM_TEMPLATES were used in the current declaration. If that is
13495 invalid, return FALSE and issue an error messages. Otherwise,
13496 return TRUE. */
13497
13498static bool
94edc4ab
NN
13499cp_parser_check_template_parameters (cp_parser* parser,
13500 unsigned num_templates)
a723baf1
MM
13501{
13502 /* If there are more template classes than parameter lists, we have
13503 something like:
13504
13505 template <class T> void S<T>::R<T>::f (); */
13506 if (parser->num_template_parameter_lists < num_templates)
13507 {
13508 error ("too few template-parameter-lists");
13509 return false;
13510 }
13511 /* If there are the same number of template classes and parameter
13512 lists, that's OK. */
13513 if (parser->num_template_parameter_lists == num_templates)
13514 return true;
13515 /* If there are more, but only one more, then we are referring to a
13516 member template. That's OK too. */
13517 if (parser->num_template_parameter_lists == num_templates + 1)
13518 return true;
13519 /* Otherwise, there are too many template parameter lists. We have
13520 something like:
13521
13522 template <class T> template <class U> void S::f(); */
13523 error ("too many template-parameter-lists");
13524 return false;
13525}
13526
13527/* Parse a binary-expression of the general form:
13528
13529 binary-expression:
13530 <expr>
13531 binary-expression <token> <expr>
13532
13533 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13534 to parser the <expr>s. If the first production is used, then the
13535 value returned by FN is returned directly. Otherwise, a node with
13536 the indicated EXPR_TYPE is returned, with operands corresponding to
13537 the two sub-expressions. */
13538
13539static tree
94edc4ab
NN
13540cp_parser_binary_expression (cp_parser* parser,
13541 const cp_parser_token_tree_map token_tree_map,
13542 cp_parser_expression_fn fn)
a723baf1
MM
13543{
13544 tree lhs;
13545
13546 /* Parse the first expression. */
13547 lhs = (*fn) (parser);
13548 /* Now, look for more expressions. */
13549 while (true)
13550 {
13551 cp_token *token;
39b1af70 13552 const cp_parser_token_tree_map_node *map_node;
a723baf1
MM
13553 tree rhs;
13554
13555 /* Peek at the next token. */
13556 token = cp_lexer_peek_token (parser->lexer);
13557 /* If the token is `>', and that's not an operator at the
13558 moment, then we're done. */
13559 if (token->type == CPP_GREATER
13560 && !parser->greater_than_is_operator_p)
13561 break;
13562 /* If we find one of the tokens we want, build the correspoding
13563 tree representation. */
13564 for (map_node = token_tree_map;
13565 map_node->token_type != CPP_EOF;
13566 ++map_node)
13567 if (map_node->token_type == token->type)
13568 {
13569 /* Consume the operator token. */
13570 cp_lexer_consume_token (parser->lexer);
13571 /* Parse the right-hand side of the expression. */
13572 rhs = (*fn) (parser);
13573 /* Build the binary tree node. */
13574 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13575 break;
13576 }
13577
13578 /* If the token wasn't one of the ones we want, we're done. */
13579 if (map_node->token_type == CPP_EOF)
13580 break;
13581 }
13582
13583 return lhs;
13584}
13585
13586/* Parse an optional `::' token indicating that the following name is
13587 from the global namespace. If so, PARSER->SCOPE is set to the
13588 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13589 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13590 Returns the new value of PARSER->SCOPE, if the `::' token is
13591 present, and NULL_TREE otherwise. */
13592
13593static tree
94edc4ab 13594cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
a723baf1
MM
13595{
13596 cp_token *token;
13597
13598 /* Peek at the next token. */
13599 token = cp_lexer_peek_token (parser->lexer);
13600 /* If we're looking at a `::' token then we're starting from the
13601 global namespace, not our current location. */
13602 if (token->type == CPP_SCOPE)
13603 {
13604 /* Consume the `::' token. */
13605 cp_lexer_consume_token (parser->lexer);
13606 /* Set the SCOPE so that we know where to start the lookup. */
13607 parser->scope = global_namespace;
13608 parser->qualifying_scope = global_namespace;
13609 parser->object_scope = NULL_TREE;
13610
13611 return parser->scope;
13612 }
13613 else if (!current_scope_valid_p)
13614 {
13615 parser->scope = NULL_TREE;
13616 parser->qualifying_scope = NULL_TREE;
13617 parser->object_scope = NULL_TREE;
13618 }
13619
13620 return NULL_TREE;
13621}
13622
13623/* Returns TRUE if the upcoming token sequence is the start of a
13624 constructor declarator. If FRIEND_P is true, the declarator is
13625 preceded by the `friend' specifier. */
13626
13627static bool
13628cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13629{
13630 bool constructor_p;
13631 tree type_decl = NULL_TREE;
13632 bool nested_name_p;
2050a1bb
MM
13633 cp_token *next_token;
13634
13635 /* The common case is that this is not a constructor declarator, so
8fbc5ae7
MM
13636 try to avoid doing lots of work if at all possible. It's not
13637 valid declare a constructor at function scope. */
13638 if (at_function_scope_p ())
13639 return false;
13640 /* And only certain tokens can begin a constructor declarator. */
2050a1bb
MM
13641 next_token = cp_lexer_peek_token (parser->lexer);
13642 if (next_token->type != CPP_NAME
13643 && next_token->type != CPP_SCOPE
13644 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13645 && next_token->type != CPP_TEMPLATE_ID)
13646 return false;
a723baf1
MM
13647
13648 /* Parse tentatively; we are going to roll back all of the tokens
13649 consumed here. */
13650 cp_parser_parse_tentatively (parser);
13651 /* Assume that we are looking at a constructor declarator. */
13652 constructor_p = true;
13653 /* Look for the optional `::' operator. */
13654 cp_parser_global_scope_opt (parser,
13655 /*current_scope_valid_p=*/false);
13656 /* Look for the nested-name-specifier. */
13657 nested_name_p
13658 = (cp_parser_nested_name_specifier_opt (parser,
13659 /*typename_keyword_p=*/false,
13660 /*check_dependency_p=*/false,
13661 /*type_p=*/false)
13662 != NULL_TREE);
13663 /* Outside of a class-specifier, there must be a
13664 nested-name-specifier. */
13665 if (!nested_name_p &&
13666 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13667 || friend_p))
13668 constructor_p = false;
13669 /* If we still think that this might be a constructor-declarator,
13670 look for a class-name. */
13671 if (constructor_p)
13672 {
13673 /* If we have:
13674
8fbc5ae7 13675 template <typename T> struct S { S(); };
a723baf1
MM
13676 template <typename T> S<T>::S ();
13677
13678 we must recognize that the nested `S' names a class.
13679 Similarly, for:
13680
13681 template <typename T> S<T>::S<T> ();
13682
13683 we must recognize that the nested `S' names a template. */
13684 type_decl = cp_parser_class_name (parser,
13685 /*typename_keyword_p=*/false,
13686 /*template_keyword_p=*/false,
13687 /*type_p=*/false,
13688 /*check_access_p=*/false,
13689 /*check_dependency_p=*/false,
13690 /*class_head_p=*/false);
13691 /* If there was no class-name, then this is not a constructor. */
13692 constructor_p = !cp_parser_error_occurred (parser);
13693 }
13694 /* If we're still considering a constructor, we have to see a `(',
13695 to begin the parameter-declaration-clause, followed by either a
13696 `)', an `...', or a decl-specifier. We need to check for a
13697 type-specifier to avoid being fooled into thinking that:
13698
13699 S::S (f) (int);
13700
13701 is a constructor. (It is actually a function named `f' that
13702 takes one parameter (of type `int') and returns a value of type
13703 `S::S'. */
13704 if (constructor_p
13705 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13706 {
13707 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13708 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13709 && !cp_parser_storage_class_specifier_opt (parser))
13710 {
5dae1114
MM
13711 tree type;
13712
13713 /* Names appearing in the type-specifier should be looked up
13714 in the scope of the class. */
13715 if (current_class_type)
13716 type = NULL_TREE;
a723baf1
MM
13717 else
13718 {
5dae1114
MM
13719 type = TREE_TYPE (type_decl);
13720 if (TREE_CODE (type) == TYPENAME_TYPE)
14d22dd6
MM
13721 {
13722 type = resolve_typename_type (type,
13723 /*only_current_p=*/false);
13724 if (type == error_mark_node)
13725 {
13726 cp_parser_abort_tentative_parse (parser);
13727 return false;
13728 }
13729 }
5dae1114 13730 push_scope (type);
a723baf1 13731 }
5dae1114
MM
13732 /* Look for the type-specifier. */
13733 cp_parser_type_specifier (parser,
13734 CP_PARSER_FLAGS_NONE,
13735 /*is_friend=*/false,
13736 /*is_declarator=*/true,
13737 /*declares_class_or_enum=*/NULL,
13738 /*is_cv_qualifier=*/NULL);
13739 /* Leave the scope of the class. */
13740 if (type)
13741 pop_scope (type);
13742
13743 constructor_p = !cp_parser_error_occurred (parser);
a723baf1
MM
13744 }
13745 }
13746 else
13747 constructor_p = false;
13748 /* We did not really want to consume any tokens. */
13749 cp_parser_abort_tentative_parse (parser);
13750
13751 return constructor_p;
13752}
13753
13754/* Parse the definition of the function given by the DECL_SPECIFIERS,
cf22909c 13755 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
a723baf1
MM
13756 they must be performed once we are in the scope of the function.
13757
13758 Returns the function defined. */
13759
13760static tree
13761cp_parser_function_definition_from_specifiers_and_declarator
94edc4ab
NN
13762 (cp_parser* parser,
13763 tree decl_specifiers,
13764 tree attributes,
13765 tree declarator)
a723baf1
MM
13766{
13767 tree fn;
13768 bool success_p;
13769
13770 /* Begin the function-definition. */
13771 success_p = begin_function_definition (decl_specifiers,
13772 attributes,
13773 declarator);
13774
13775 /* If there were names looked up in the decl-specifier-seq that we
13776 did not check, check them now. We must wait until we are in the
13777 scope of the function to perform the checks, since the function
13778 might be a friend. */
cf22909c 13779 perform_deferred_access_checks ();
a723baf1
MM
13780
13781 if (!success_p)
13782 {
13783 /* If begin_function_definition didn't like the definition, skip
13784 the entire function. */
13785 error ("invalid function declaration");
13786 cp_parser_skip_to_end_of_block_or_statement (parser);
13787 fn = error_mark_node;
13788 }
13789 else
13790 fn = cp_parser_function_definition_after_declarator (parser,
13791 /*inline_p=*/false);
13792
13793 return fn;
13794}
13795
13796/* Parse the part of a function-definition that follows the
13797 declarator. INLINE_P is TRUE iff this function is an inline
13798 function defined with a class-specifier.
13799
13800 Returns the function defined. */
13801
13802static tree
94edc4ab
NN
13803cp_parser_function_definition_after_declarator (cp_parser* parser,
13804 bool inline_p)
a723baf1
MM
13805{
13806 tree fn;
13807 bool ctor_initializer_p = false;
13808 bool saved_in_unbraced_linkage_specification_p;
13809 unsigned saved_num_template_parameter_lists;
13810
13811 /* If the next token is `return', then the code may be trying to
13812 make use of the "named return value" extension that G++ used to
13813 support. */
13814 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13815 {
13816 /* Consume the `return' keyword. */
13817 cp_lexer_consume_token (parser->lexer);
13818 /* Look for the identifier that indicates what value is to be
13819 returned. */
13820 cp_parser_identifier (parser);
13821 /* Issue an error message. */
13822 error ("named return values are no longer supported");
13823 /* Skip tokens until we reach the start of the function body. */
13824 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13825 cp_lexer_consume_token (parser->lexer);
13826 }
13827 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13828 anything declared inside `f'. */
13829 saved_in_unbraced_linkage_specification_p
13830 = parser->in_unbraced_linkage_specification_p;
13831 parser->in_unbraced_linkage_specification_p = false;
13832 /* Inside the function, surrounding template-parameter-lists do not
13833 apply. */
13834 saved_num_template_parameter_lists
13835 = parser->num_template_parameter_lists;
13836 parser->num_template_parameter_lists = 0;
13837 /* If the next token is `try', then we are looking at a
13838 function-try-block. */
13839 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13840 ctor_initializer_p = cp_parser_function_try_block (parser);
13841 /* A function-try-block includes the function-body, so we only do
13842 this next part if we're not processing a function-try-block. */
13843 else
13844 ctor_initializer_p
13845 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13846
13847 /* Finish the function. */
13848 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13849 (inline_p ? 2 : 0));
13850 /* Generate code for it, if necessary. */
13851 expand_body (fn);
13852 /* Restore the saved values. */
13853 parser->in_unbraced_linkage_specification_p
13854 = saved_in_unbraced_linkage_specification_p;
13855 parser->num_template_parameter_lists
13856 = saved_num_template_parameter_lists;
13857
13858 return fn;
13859}
13860
13861/* Parse a template-declaration, assuming that the `export' (and
13862 `extern') keywords, if present, has already been scanned. MEMBER_P
13863 is as for cp_parser_template_declaration. */
13864
13865static void
94edc4ab 13866cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
a723baf1
MM
13867{
13868 tree decl = NULL_TREE;
13869 tree parameter_list;
13870 bool friend_p = false;
13871
13872 /* Look for the `template' keyword. */
13873 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13874 return;
13875
13876 /* And the `<'. */
13877 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13878 return;
13879
13880 /* Parse the template parameters. */
13881 begin_template_parm_list ();
13882 /* If the next token is `>', then we have an invalid
13883 specialization. Rather than complain about an invalid template
13884 parameter, issue an error message here. */
13885 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13886 {
13887 cp_parser_error (parser, "invalid explicit specialization");
13888 parameter_list = NULL_TREE;
13889 }
13890 else
13891 parameter_list = cp_parser_template_parameter_list (parser);
13892 parameter_list = end_template_parm_list (parameter_list);
13893 /* Look for the `>'. */
13894 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13895 /* We just processed one more parameter list. */
13896 ++parser->num_template_parameter_lists;
13897 /* If the next token is `template', there are more template
13898 parameters. */
13899 if (cp_lexer_next_token_is_keyword (parser->lexer,
13900 RID_TEMPLATE))
13901 cp_parser_template_declaration_after_export (parser, member_p);
13902 else
13903 {
13904 decl = cp_parser_single_declaration (parser,
13905 member_p,
13906 &friend_p);
13907
13908 /* If this is a member template declaration, let the front
13909 end know. */
13910 if (member_p && !friend_p && decl)
13911 decl = finish_member_template_decl (decl);
13912 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13913 make_friend_class (current_class_type, TREE_TYPE (decl));
13914 }
13915 /* We are done with the current parameter list. */
13916 --parser->num_template_parameter_lists;
13917
13918 /* Finish up. */
13919 finish_template_decl (parameter_list);
13920
13921 /* Register member declarations. */
13922 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13923 finish_member_declaration (decl);
13924
13925 /* If DECL is a function template, we must return to parse it later.
13926 (Even though there is no definition, there might be default
13927 arguments that need handling.) */
13928 if (member_p && decl
13929 && (TREE_CODE (decl) == FUNCTION_DECL
13930 || DECL_FUNCTION_TEMPLATE_P (decl)))
13931 TREE_VALUE (parser->unparsed_functions_queues)
8218bd34 13932 = tree_cons (NULL_TREE, decl,
a723baf1
MM
13933 TREE_VALUE (parser->unparsed_functions_queues));
13934}
13935
13936/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13937 `function-definition' sequence. MEMBER_P is true, this declaration
13938 appears in a class scope.
13939
13940 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13941 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13942
13943static tree
94edc4ab
NN
13944cp_parser_single_declaration (cp_parser* parser,
13945 bool member_p,
13946 bool* friend_p)
a723baf1
MM
13947{
13948 bool declares_class_or_enum;
13949 tree decl = NULL_TREE;
13950 tree decl_specifiers;
13951 tree attributes;
a723baf1
MM
13952
13953 /* Parse the dependent declaration. We don't know yet
13954 whether it will be a function-definition. */
13955 cp_parser_parse_tentatively (parser);
13956 /* Defer access checks until we know what is being declared. */
cf22909c
KL
13957 push_deferring_access_checks (true);
13958
a723baf1
MM
13959 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13960 alternative. */
13961 decl_specifiers
13962 = cp_parser_decl_specifier_seq (parser,
13963 CP_PARSER_FLAGS_OPTIONAL,
13964 &attributes,
13965 &declares_class_or_enum);
13966 /* Gather up the access checks that occurred the
13967 decl-specifier-seq. */
cf22909c
KL
13968 stop_deferring_access_checks ();
13969
a723baf1
MM
13970 /* Check for the declaration of a template class. */
13971 if (declares_class_or_enum)
13972 {
13973 if (cp_parser_declares_only_class_p (parser))
13974 {
13975 decl = shadow_tag (decl_specifiers);
13976 if (decl)
13977 decl = TYPE_NAME (decl);
13978 else
13979 decl = error_mark_node;
13980 }
13981 }
13982 else
13983 decl = NULL_TREE;
13984 /* If it's not a template class, try for a template function. If
13985 the next token is a `;', then this declaration does not declare
13986 anything. But, if there were errors in the decl-specifiers, then
13987 the error might well have come from an attempted class-specifier.
13988 In that case, there's no need to warn about a missing declarator. */
13989 if (!decl
13990 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13991 || !value_member (error_mark_node, decl_specifiers)))
13992 decl = cp_parser_init_declarator (parser,
13993 decl_specifiers,
13994 attributes,
a723baf1
MM
13995 /*function_definition_allowed_p=*/false,
13996 member_p,
13997 /*function_definition_p=*/NULL);
cf22909c
KL
13998
13999 pop_deferring_access_checks ();
14000
a723baf1
MM
14001 /* Clear any current qualification; whatever comes next is the start
14002 of something new. */
14003 parser->scope = NULL_TREE;
14004 parser->qualifying_scope = NULL_TREE;
14005 parser->object_scope = NULL_TREE;
14006 /* Look for a trailing `;' after the declaration. */
8a6393df 14007 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
a723baf1
MM
14008 && cp_parser_committed_to_tentative_parse (parser))
14009 cp_parser_skip_to_end_of_block_or_statement (parser);
14010 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
14011 if (cp_parser_parse_definitely (parser))
14012 {
14013 if (friend_p)
14014 *friend_p = cp_parser_friend_p (decl_specifiers);
14015 }
14016 /* Otherwise, try a function-definition. */
14017 else
14018 decl = cp_parser_function_definition (parser, friend_p);
14019
14020 return decl;
14021}
14022
14023/* Parse a functional cast to TYPE. Returns an expression
14024 representing the cast. */
14025
14026static tree
94edc4ab 14027cp_parser_functional_cast (cp_parser* parser, tree type)
a723baf1
MM
14028{
14029 tree expression_list;
14030
14031 /* Look for the opening `('. */
14032 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14033 return error_mark_node;
14034 /* If the next token is not an `)', there are arguments to the
14035 cast. */
14036 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
14037 expression_list = cp_parser_expression_list (parser);
14038 else
14039 expression_list = NULL_TREE;
14040 /* Look for the closing `)'. */
14041 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14042
14043 return build_functional_cast (type, expression_list);
14044}
14045
14046/* MEMBER_FUNCTION is a member function, or a friend. If default
14047 arguments, or the body of the function have not yet been parsed,
14048 parse them now. */
14049
14050static void
94edc4ab 14051cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
a723baf1
MM
14052{
14053 cp_lexer *saved_lexer;
14054
14055 /* If this member is a template, get the underlying
14056 FUNCTION_DECL. */
14057 if (DECL_FUNCTION_TEMPLATE_P (member_function))
14058 member_function = DECL_TEMPLATE_RESULT (member_function);
14059
14060 /* There should not be any class definitions in progress at this
14061 point; the bodies of members are only parsed outside of all class
14062 definitions. */
14063 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14064 /* While we're parsing the member functions we might encounter more
14065 classes. We want to handle them right away, but we don't want
14066 them getting mixed up with functions that are currently in the
14067 queue. */
14068 parser->unparsed_functions_queues
14069 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14070
14071 /* Make sure that any template parameters are in scope. */
14072 maybe_begin_member_template_processing (member_function);
14073
a723baf1
MM
14074 /* If the body of the function has not yet been parsed, parse it
14075 now. */
14076 if (DECL_PENDING_INLINE_P (member_function))
14077 {
14078 tree function_scope;
14079 cp_token_cache *tokens;
14080
14081 /* The function is no longer pending; we are processing it. */
14082 tokens = DECL_PENDING_INLINE_INFO (member_function);
14083 DECL_PENDING_INLINE_INFO (member_function) = NULL;
14084 DECL_PENDING_INLINE_P (member_function) = 0;
14085 /* If this was an inline function in a local class, enter the scope
14086 of the containing function. */
14087 function_scope = decl_function_context (member_function);
14088 if (function_scope)
14089 push_function_context_to (function_scope);
14090
14091 /* Save away the current lexer. */
14092 saved_lexer = parser->lexer;
14093 /* Make a new lexer to feed us the tokens saved for this function. */
14094 parser->lexer = cp_lexer_new_from_tokens (tokens);
14095 parser->lexer->next = saved_lexer;
14096
14097 /* Set the current source position to be the location of the first
14098 token in the saved inline body. */
3466b292 14099 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14100
14101 /* Let the front end know that we going to be defining this
14102 function. */
14103 start_function (NULL_TREE, member_function, NULL_TREE,
14104 SF_PRE_PARSED | SF_INCLASS_INLINE);
14105
14106 /* Now, parse the body of the function. */
14107 cp_parser_function_definition_after_declarator (parser,
14108 /*inline_p=*/true);
14109
14110 /* Leave the scope of the containing function. */
14111 if (function_scope)
14112 pop_function_context_from (function_scope);
14113 /* Restore the lexer. */
14114 parser->lexer = saved_lexer;
14115 }
14116
14117 /* Remove any template parameters from the symbol table. */
14118 maybe_end_member_template_processing ();
14119
14120 /* Restore the queue. */
14121 parser->unparsed_functions_queues
14122 = TREE_CHAIN (parser->unparsed_functions_queues);
14123}
14124
8218bd34
MM
14125/* FN is a FUNCTION_DECL which may contains a parameter with an
14126 unparsed DEFAULT_ARG. Parse the default args now. */
a723baf1
MM
14127
14128static void
8218bd34 14129cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
a723baf1
MM
14130{
14131 cp_lexer *saved_lexer;
14132 cp_token_cache *tokens;
14133 bool saved_local_variables_forbidden_p;
14134 tree parameters;
8218bd34
MM
14135
14136 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
a723baf1
MM
14137 parameters;
14138 parameters = TREE_CHAIN (parameters))
14139 {
14140 if (!TREE_PURPOSE (parameters)
14141 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14142 continue;
14143
14144 /* Save away the current lexer. */
14145 saved_lexer = parser->lexer;
14146 /* Create a new one, using the tokens we have saved. */
14147 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14148 parser->lexer = cp_lexer_new_from_tokens (tokens);
14149
14150 /* Set the current source position to be the location of the
14151 first token in the default argument. */
3466b292 14152 cp_lexer_peek_token (parser->lexer);
a723baf1
MM
14153
14154 /* Local variable names (and the `this' keyword) may not appear
14155 in a default argument. */
14156 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14157 parser->local_variables_forbidden_p = true;
14158 /* Parse the assignment-expression. */
8218bd34 14159 if (DECL_CONTEXT (fn))
14d22dd6 14160 push_nested_class (DECL_CONTEXT (fn));
a723baf1 14161 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
8218bd34 14162 if (DECL_CONTEXT (fn))
e5976695 14163 pop_nested_class ();
a723baf1
MM
14164
14165 /* Restore saved state. */
14166 parser->lexer = saved_lexer;
14167 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14168 }
14169}
14170
14171/* Parse the operand of `sizeof' (or a similar operator). Returns
14172 either a TYPE or an expression, depending on the form of the
14173 input. The KEYWORD indicates which kind of expression we have
14174 encountered. */
14175
14176static tree
94edc4ab 14177cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
a723baf1
MM
14178{
14179 static const char *format;
14180 tree expr = NULL_TREE;
14181 const char *saved_message;
14182 bool saved_constant_expression_p;
14183
14184 /* Initialize FORMAT the first time we get here. */
14185 if (!format)
14186 format = "types may not be defined in `%s' expressions";
14187
14188 /* Types cannot be defined in a `sizeof' expression. Save away the
14189 old message. */
14190 saved_message = parser->type_definition_forbidden_message;
14191 /* And create the new one. */
14192 parser->type_definition_forbidden_message
14193 = ((const char *)
14194 xmalloc (strlen (format)
14195 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14196 + 1 /* `\0' */));
14197 sprintf ((char *) parser->type_definition_forbidden_message,
14198 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14199
14200 /* The restrictions on constant-expressions do not apply inside
14201 sizeof expressions. */
14202 saved_constant_expression_p = parser->constant_expression_p;
14203 parser->constant_expression_p = false;
14204
3beb3abf
MM
14205 /* Do not actually evaluate the expression. */
14206 ++skip_evaluation;
a723baf1
MM
14207 /* If it's a `(', then we might be looking at the type-id
14208 construction. */
14209 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14210 {
14211 tree type;
14212
14213 /* We can't be sure yet whether we're looking at a type-id or an
14214 expression. */
14215 cp_parser_parse_tentatively (parser);
14216 /* Consume the `('. */
14217 cp_lexer_consume_token (parser->lexer);
14218 /* Parse the type-id. */
14219 type = cp_parser_type_id (parser);
14220 /* Now, look for the trailing `)'. */
14221 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14222 /* If all went well, then we're done. */
14223 if (cp_parser_parse_definitely (parser))
14224 {
14225 /* Build a list of decl-specifiers; right now, we have only
14226 a single type-specifier. */
14227 type = build_tree_list (NULL_TREE,
14228 type);
14229
14230 /* Call grokdeclarator to figure out what type this is. */
14231 expr = grokdeclarator (NULL_TREE,
14232 type,
14233 TYPENAME,
14234 /*initialized=*/0,
14235 /*attrlist=*/NULL);
14236 }
14237 }
14238
14239 /* If the type-id production did not work out, then we must be
14240 looking at the unary-expression production. */
14241 if (!expr)
14242 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
3beb3abf
MM
14243 /* Go back to evaluating expressions. */
14244 --skip_evaluation;
a723baf1
MM
14245
14246 /* Free the message we created. */
14247 free ((char *) parser->type_definition_forbidden_message);
14248 /* And restore the old one. */
14249 parser->type_definition_forbidden_message = saved_message;
14250 parser->constant_expression_p = saved_constant_expression_p;
14251
14252 return expr;
14253}
14254
14255/* If the current declaration has no declarator, return true. */
14256
14257static bool
14258cp_parser_declares_only_class_p (cp_parser *parser)
14259{
14260 /* If the next token is a `;' or a `,' then there is no
14261 declarator. */
14262 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14263 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14264}
14265
14266/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14267 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14268
14269static bool
94edc4ab 14270cp_parser_friend_p (tree decl_specifiers)
a723baf1
MM
14271{
14272 while (decl_specifiers)
14273 {
14274 /* See if this decl-specifier is `friend'. */
14275 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14276 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14277 return true;
14278
14279 /* Go on to the next decl-specifier. */
14280 decl_specifiers = TREE_CHAIN (decl_specifiers);
14281 }
14282
14283 return false;
14284}
14285
14286/* If the next token is of the indicated TYPE, consume it. Otherwise,
14287 issue an error message indicating that TOKEN_DESC was expected.
14288
14289 Returns the token consumed, if the token had the appropriate type.
14290 Otherwise, returns NULL. */
14291
14292static cp_token *
94edc4ab
NN
14293cp_parser_require (cp_parser* parser,
14294 enum cpp_ttype type,
14295 const char* token_desc)
a723baf1
MM
14296{
14297 if (cp_lexer_next_token_is (parser->lexer, type))
14298 return cp_lexer_consume_token (parser->lexer);
14299 else
14300 {
e5976695
MM
14301 /* Output the MESSAGE -- unless we're parsing tentatively. */
14302 if (!cp_parser_simulate_error (parser))
14303 error ("expected %s", token_desc);
a723baf1
MM
14304 return NULL;
14305 }
14306}
14307
14308/* Like cp_parser_require, except that tokens will be skipped until
14309 the desired token is found. An error message is still produced if
14310 the next token is not as expected. */
14311
14312static void
94edc4ab
NN
14313cp_parser_skip_until_found (cp_parser* parser,
14314 enum cpp_ttype type,
14315 const char* token_desc)
a723baf1
MM
14316{
14317 cp_token *token;
14318 unsigned nesting_depth = 0;
14319
14320 if (cp_parser_require (parser, type, token_desc))
14321 return;
14322
14323 /* Skip tokens until the desired token is found. */
14324 while (true)
14325 {
14326 /* Peek at the next token. */
14327 token = cp_lexer_peek_token (parser->lexer);
14328 /* If we've reached the token we want, consume it and
14329 stop. */
14330 if (token->type == type && !nesting_depth)
14331 {
14332 cp_lexer_consume_token (parser->lexer);
14333 return;
14334 }
14335 /* If we've run out of tokens, stop. */
14336 if (token->type == CPP_EOF)
14337 return;
14338 if (token->type == CPP_OPEN_BRACE
14339 || token->type == CPP_OPEN_PAREN
14340 || token->type == CPP_OPEN_SQUARE)
14341 ++nesting_depth;
14342 else if (token->type == CPP_CLOSE_BRACE
14343 || token->type == CPP_CLOSE_PAREN
14344 || token->type == CPP_CLOSE_SQUARE)
14345 {
14346 if (nesting_depth-- == 0)
14347 return;
14348 }
14349 /* Consume this token. */
14350 cp_lexer_consume_token (parser->lexer);
14351 }
14352}
14353
14354/* If the next token is the indicated keyword, consume it. Otherwise,
14355 issue an error message indicating that TOKEN_DESC was expected.
14356
14357 Returns the token consumed, if the token had the appropriate type.
14358 Otherwise, returns NULL. */
14359
14360static cp_token *
94edc4ab
NN
14361cp_parser_require_keyword (cp_parser* parser,
14362 enum rid keyword,
14363 const char* token_desc)
a723baf1
MM
14364{
14365 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14366
14367 if (token && token->keyword != keyword)
14368 {
14369 dyn_string_t error_msg;
14370
14371 /* Format the error message. */
14372 error_msg = dyn_string_new (0);
14373 dyn_string_append_cstr (error_msg, "expected ");
14374 dyn_string_append_cstr (error_msg, token_desc);
14375 cp_parser_error (parser, error_msg->s);
14376 dyn_string_delete (error_msg);
14377 return NULL;
14378 }
14379
14380 return token;
14381}
14382
14383/* Returns TRUE iff TOKEN is a token that can begin the body of a
14384 function-definition. */
14385
14386static bool
94edc4ab 14387cp_parser_token_starts_function_definition_p (cp_token* token)
a723baf1
MM
14388{
14389 return (/* An ordinary function-body begins with an `{'. */
14390 token->type == CPP_OPEN_BRACE
14391 /* A ctor-initializer begins with a `:'. */
14392 || token->type == CPP_COLON
14393 /* A function-try-block begins with `try'. */
14394 || token->keyword == RID_TRY
14395 /* The named return value extension begins with `return'. */
14396 || token->keyword == RID_RETURN);
14397}
14398
14399/* Returns TRUE iff the next token is the ":" or "{" beginning a class
14400 definition. */
14401
14402static bool
14403cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14404{
14405 cp_token *token;
14406
14407 token = cp_lexer_peek_token (parser->lexer);
14408 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14409}
14410
14411/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14412 or none_type otherwise. */
14413
14414static enum tag_types
94edc4ab 14415cp_parser_token_is_class_key (cp_token* token)
a723baf1
MM
14416{
14417 switch (token->keyword)
14418 {
14419 case RID_CLASS:
14420 return class_type;
14421 case RID_STRUCT:
14422 return record_type;
14423 case RID_UNION:
14424 return union_type;
14425
14426 default:
14427 return none_type;
14428 }
14429}
14430
14431/* Issue an error message if the CLASS_KEY does not match the TYPE. */
14432
14433static void
14434cp_parser_check_class_key (enum tag_types class_key, tree type)
14435{
14436 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14437 pedwarn ("`%s' tag used in naming `%#T'",
14438 class_key == union_type ? "union"
14439 : class_key == record_type ? "struct" : "class",
14440 type);
14441}
14442
14443/* Look for the `template' keyword, as a syntactic disambiguator.
14444 Return TRUE iff it is present, in which case it will be
14445 consumed. */
14446
14447static bool
14448cp_parser_optional_template_keyword (cp_parser *parser)
14449{
14450 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14451 {
14452 /* The `template' keyword can only be used within templates;
14453 outside templates the parser can always figure out what is a
14454 template and what is not. */
14455 if (!processing_template_decl)
14456 {
14457 error ("`template' (as a disambiguator) is only allowed "
14458 "within templates");
14459 /* If this part of the token stream is rescanned, the same
14460 error message would be generated. So, we purge the token
14461 from the stream. */
14462 cp_lexer_purge_token (parser->lexer);
14463 return false;
14464 }
14465 else
14466 {
14467 /* Consume the `template' keyword. */
14468 cp_lexer_consume_token (parser->lexer);
14469 return true;
14470 }
14471 }
14472
14473 return false;
14474}
14475
2050a1bb
MM
14476/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14477 set PARSER->SCOPE, and perform other related actions. */
14478
14479static void
14480cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14481{
14482 tree value;
14483 tree check;
14484
14485 /* Get the stored value. */
14486 value = cp_lexer_consume_token (parser->lexer)->value;
14487 /* Perform any access checks that were deferred. */
14488 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
cf22909c 14489 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
2050a1bb
MM
14490 /* Set the scope from the stored value. */
14491 parser->scope = TREE_VALUE (value);
14492 parser->qualifying_scope = TREE_TYPE (value);
14493 parser->object_scope = NULL_TREE;
14494}
14495
a723baf1
MM
14496/* Add tokens to CACHE until an non-nested END token appears. */
14497
14498static void
14499cp_parser_cache_group (cp_parser *parser,
14500 cp_token_cache *cache,
14501 enum cpp_ttype end,
14502 unsigned depth)
14503{
14504 while (true)
14505 {
14506 cp_token *token;
14507
14508 /* Abort a parenthesized expression if we encounter a brace. */
14509 if ((end == CPP_CLOSE_PAREN || depth == 0)
14510 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14511 return;
14512 /* Consume the next token. */
14513 token = cp_lexer_consume_token (parser->lexer);
14514 /* If we've reached the end of the file, stop. */
14515 if (token->type == CPP_EOF)
14516 return;
14517 /* Add this token to the tokens we are saving. */
14518 cp_token_cache_push_token (cache, token);
14519 /* See if it starts a new group. */
14520 if (token->type == CPP_OPEN_BRACE)
14521 {
14522 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14523 if (depth == 0)
14524 return;
14525 }
14526 else if (token->type == CPP_OPEN_PAREN)
14527 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14528 else if (token->type == end)
14529 return;
14530 }
14531}
14532
14533/* Begin parsing tentatively. We always save tokens while parsing
14534 tentatively so that if the tentative parsing fails we can restore the
14535 tokens. */
14536
14537static void
94edc4ab 14538cp_parser_parse_tentatively (cp_parser* parser)
a723baf1
MM
14539{
14540 /* Enter a new parsing context. */
14541 parser->context = cp_parser_context_new (parser->context);
14542 /* Begin saving tokens. */
14543 cp_lexer_save_tokens (parser->lexer);
14544 /* In order to avoid repetitive access control error messages,
14545 access checks are queued up until we are no longer parsing
14546 tentatively. */
cf22909c 14547 push_deferring_access_checks (true);
a723baf1
MM
14548}
14549
14550/* Commit to the currently active tentative parse. */
14551
14552static void
94edc4ab 14553cp_parser_commit_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14554{
14555 cp_parser_context *context;
14556 cp_lexer *lexer;
14557
14558 /* Mark all of the levels as committed. */
14559 lexer = parser->lexer;
14560 for (context = parser->context; context->next; context = context->next)
14561 {
14562 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14563 break;
14564 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14565 while (!cp_lexer_saving_tokens (lexer))
14566 lexer = lexer->next;
14567 cp_lexer_commit_tokens (lexer);
14568 }
14569}
14570
14571/* Abort the currently active tentative parse. All consumed tokens
14572 will be rolled back, and no diagnostics will be issued. */
14573
14574static void
94edc4ab 14575cp_parser_abort_tentative_parse (cp_parser* parser)
a723baf1
MM
14576{
14577 cp_parser_simulate_error (parser);
14578 /* Now, pretend that we want to see if the construct was
14579 successfully parsed. */
14580 cp_parser_parse_definitely (parser);
14581}
14582
14583/* Stop parsing tentatively. If a parse error has ocurred, restore the
14584 token stream. Otherwise, commit to the tokens we have consumed.
14585 Returns true if no error occurred; false otherwise. */
14586
14587static bool
94edc4ab 14588cp_parser_parse_definitely (cp_parser* parser)
a723baf1
MM
14589{
14590 bool error_occurred;
14591 cp_parser_context *context;
14592
14593 /* Remember whether or not an error ocurred, since we are about to
14594 destroy that information. */
14595 error_occurred = cp_parser_error_occurred (parser);
14596 /* Remove the topmost context from the stack. */
14597 context = parser->context;
14598 parser->context = context->next;
14599 /* If no parse errors occurred, commit to the tentative parse. */
14600 if (!error_occurred)
14601 {
14602 /* Commit to the tokens read tentatively, unless that was
14603 already done. */
14604 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14605 cp_lexer_commit_tokens (parser->lexer);
cf22909c
KL
14606
14607 pop_to_parent_deferring_access_checks ();
a723baf1
MM
14608 }
14609 /* Otherwise, if errors occurred, roll back our state so that things
14610 are just as they were before we began the tentative parse. */
14611 else
cf22909c
KL
14612 {
14613 cp_lexer_rollback_tokens (parser->lexer);
14614 pop_deferring_access_checks ();
14615 }
e5976695
MM
14616 /* Add the context to the front of the free list. */
14617 context->next = cp_parser_context_free_list;
14618 cp_parser_context_free_list = context;
14619
14620 return !error_occurred;
a723baf1
MM
14621}
14622
a723baf1
MM
14623/* Returns true if we are parsing tentatively -- but have decided that
14624 we will stick with this tentative parse, even if errors occur. */
14625
14626static bool
94edc4ab 14627cp_parser_committed_to_tentative_parse (cp_parser* parser)
a723baf1
MM
14628{
14629 return (cp_parser_parsing_tentatively (parser)
14630 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14631}
14632
14633/* Returns non-zero iff an error has occurred during the most recent
14634 tentative parse. */
14635
14636static bool
94edc4ab 14637cp_parser_error_occurred (cp_parser* parser)
a723baf1
MM
14638{
14639 return (cp_parser_parsing_tentatively (parser)
14640 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14641}
14642
14643/* Returns non-zero if GNU extensions are allowed. */
14644
14645static bool
94edc4ab 14646cp_parser_allow_gnu_extensions_p (cp_parser* parser)
a723baf1
MM
14647{
14648 return parser->allow_gnu_extensions_p;
14649}
14650
14651\f
14652
14653/* The parser. */
14654
14655static GTY (()) cp_parser *the_parser;
14656
14657/* External interface. */
14658
14659/* Parse the entire translation unit. */
14660
14661int
94edc4ab 14662yyparse (void)
a723baf1
MM
14663{
14664 bool error_occurred;
14665
14666 the_parser = cp_parser_new ();
cf22909c 14667 push_deferring_access_checks (false);
a723baf1
MM
14668 error_occurred = cp_parser_translation_unit (the_parser);
14669 the_parser = NULL;
17211ab5
GK
14670
14671 finish_file ();
a723baf1
MM
14672
14673 return error_occurred;
14674}
14675
14676/* Clean up after parsing the entire translation unit. */
14677
14678void
94edc4ab 14679free_parser_stacks (void)
a723baf1
MM
14680{
14681 /* Nothing to do. */
14682}
14683
14684/* This variable must be provided by every front end. */
14685
14686int yydebug;
14687
14688#include "gt-cp-parser.h"