parser.c revision 146895
1/* C++ Parser.
2   Copyright (C) 2000, 2001, 2002, 2003, 2004,
3   2005 Free Software Foundation, Inc.
4   Written by Mark Mitchell <mark@codesourcery.com>.
5
6   This file is part of GCC.
7
8   GCC is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   GCC is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with GCC; see the file COPYING.  If not, write to the Free
20   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21   02111-1307, USA.  */
22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
26#include "tm.h"
27#include "dyn-string.h"
28#include "varray.h"
29#include "cpplib.h"
30#include "tree.h"
31#include "cp-tree.h"
32#include "c-pragma.h"
33#include "decl.h"
34#include "flags.h"
35#include "diagnostic.h"
36#include "toplev.h"
37#include "output.h"
38
39
40/* The lexer.  */
41
42/* Overview
43   --------
44
45   A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
46   look-ahead.
47
48   Methodology
49   -----------
50
51   We use a circular buffer to store incoming tokens.
52
53   Some artifacts of the C++ language (such as the
54   expression/declaration ambiguity) require arbitrary look-ahead.
55   The strategy we adopt for dealing with these problems is to attempt
56   to parse one construct (e.g., the declaration) and fall back to the
57   other (e.g., the expression) if that attempt does not succeed.
58   Therefore, we must sometimes store an arbitrary number of tokens.
59
60   The parser routinely peeks at the next token, and then consumes it
61   later.  That also requires a buffer in which to store the tokens.
62
63   In order to easily permit adding tokens to the end of the buffer,
64   while removing them from the beginning of the buffer, we use a
65   circular buffer.  */
66
67/* A C++ token.  */
68
69typedef struct cp_token GTY (())
70{
71  /* The kind of token.  */
72  ENUM_BITFIELD (cpp_ttype) type : 8;
73  /* If this token is a keyword, this value indicates which keyword.
74     Otherwise, this value is RID_MAX.  */
75  ENUM_BITFIELD (rid) keyword : 8;
76  /* Token flags.  */
77  unsigned char flags;
78  /* The value associated with this token, if any.  */
79  tree value;
80  /* The location at which this token was found.  */
81  location_t location;
82} cp_token;
83
84/* The number of tokens in a single token block.
85   Computed so that cp_token_block fits in a 512B allocation unit.  */
86
87#define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
88
89/* A group of tokens.  These groups are chained together to store
90   large numbers of tokens.  (For example, a token block is created
91   when the body of an inline member function is first encountered;
92   the tokens are processed later after the class definition is
93   complete.)
94
95   This somewhat ungainly data structure (as opposed to, say, a
96   variable-length array), is used due to constraints imposed by the
97   current garbage-collection methodology.  If it is made more
98   flexible, we could perhaps simplify the data structures involved.  */
99
100typedef struct cp_token_block GTY (())
101{
102  /* The tokens.  */
103  cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
104  /* The number of tokens in this block.  */
105  size_t num_tokens;
106  /* The next token block in the chain.  */
107  struct cp_token_block *next;
108  /* The previous block in the chain.  */
109  struct cp_token_block *prev;
110} cp_token_block;
111
112typedef struct cp_token_cache GTY (())
113{
114  /* The first block in the cache.  NULL if there are no tokens in the
115     cache.  */
116  cp_token_block *first;
117  /* The last block in the cache.  NULL If there are no tokens in the
118     cache.  */
119  cp_token_block *last;
120} cp_token_cache;
121
122/* Prototypes.  */
123
124static cp_token_cache *cp_token_cache_new
125  (void);
126static void cp_token_cache_push_token
127  (cp_token_cache *, cp_token *);
128
129/* Create a new cp_token_cache.  */
130
131static cp_token_cache *
132cp_token_cache_new (void)
133{
134  return ggc_alloc_cleared (sizeof (cp_token_cache));
135}
136
137/* Add *TOKEN to *CACHE.  */
138
139static void
140cp_token_cache_push_token (cp_token_cache *cache,
141			   cp_token *token)
142{
143  cp_token_block *b = cache->last;
144
145  /* See if we need to allocate a new token block.  */
146  if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
147    {
148      b = ggc_alloc_cleared (sizeof (cp_token_block));
149      b->prev = cache->last;
150      if (cache->last)
151	{
152	  cache->last->next = b;
153	  cache->last = b;
154	}
155      else
156	cache->first = cache->last = b;
157    }
158  /* Add this token to the current token block.  */
159  b->tokens[b->num_tokens++] = *token;
160}
161
162/* The cp_lexer structure represents the C++ lexer.  It is responsible
163   for managing the token stream from the preprocessor and supplying
164   it to the parser.  */
165
166typedef struct cp_lexer GTY (())
167{
168  /* The memory allocated for the buffer.  Never NULL.  */
169  cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
170  /* A pointer just past the end of the memory allocated for the buffer.  */
171  cp_token * GTY ((skip (""))) buffer_end;
172  /* The first valid token in the buffer, or NULL if none.  */
173  cp_token * GTY ((skip (""))) first_token;
174  /* The next available token.  If NEXT_TOKEN is NULL, then there are
175     no more available tokens.  */
176  cp_token * GTY ((skip (""))) next_token;
177  /* A pointer just past the last available token.  If FIRST_TOKEN is
178     NULL, however, there are no available tokens, and then this
179     location is simply the place in which the next token read will be
180     placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
181     When the LAST_TOKEN == BUFFER, then the last token is at the
182     highest memory address in the BUFFER.  */
183  cp_token * GTY ((skip (""))) last_token;
184
185  /* A stack indicating positions at which cp_lexer_save_tokens was
186     called.  The top entry is the most recent position at which we
187     began saving tokens.  The entries are differences in token
188     position between FIRST_TOKEN and the first saved token.
189
190     If the stack is non-empty, we are saving tokens.  When a token is
191     consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
192     pointer will not.  The token stream will be preserved so that it
193     can be reexamined later.
194
195     If the stack is empty, then we are not saving tokens.  Whenever a
196     token is consumed, the FIRST_TOKEN pointer will be moved, and the
197     consumed token will be gone forever.  */
198  varray_type saved_tokens;
199
200  /* The STRING_CST tokens encountered while processing the current
201     string literal.  */
202  varray_type string_tokens;
203
204  /* True if we should obtain more tokens from the preprocessor; false
205     if we are processing a saved token cache.  */
206  bool main_lexer_p;
207
208  /* True if we should output debugging information.  */
209  bool debugging_p;
210
211  /* The next lexer in a linked list of lexers.  */
212  struct cp_lexer *next;
213} cp_lexer;
214
215/* Prototypes.  */
216
217static cp_lexer *cp_lexer_new_main
218  (void);
219static cp_lexer *cp_lexer_new_from_tokens
220  (struct cp_token_cache *);
221static int cp_lexer_saving_tokens
222  (const cp_lexer *);
223static cp_token *cp_lexer_next_token
224  (cp_lexer *, cp_token *);
225static cp_token *cp_lexer_prev_token
226  (cp_lexer *, cp_token *);
227static ptrdiff_t cp_lexer_token_difference
228  (cp_lexer *, cp_token *, cp_token *);
229static cp_token *cp_lexer_read_token
230  (cp_lexer *);
231static void cp_lexer_maybe_grow_buffer
232  (cp_lexer *);
233static void cp_lexer_get_preprocessor_token
234  (cp_lexer *, cp_token *);
235static cp_token *cp_lexer_peek_token
236  (cp_lexer *);
237static cp_token *cp_lexer_peek_nth_token
238  (cp_lexer *, size_t);
239static inline bool cp_lexer_next_token_is
240  (cp_lexer *, enum cpp_ttype);
241static bool cp_lexer_next_token_is_not
242  (cp_lexer *, enum cpp_ttype);
243static bool cp_lexer_next_token_is_keyword
244  (cp_lexer *, enum rid);
245static cp_token *cp_lexer_consume_token
246  (cp_lexer *);
247static void cp_lexer_purge_token
248  (cp_lexer *);
249static void cp_lexer_purge_tokens_after
250  (cp_lexer *, cp_token *);
251static void cp_lexer_save_tokens
252  (cp_lexer *);
253static void cp_lexer_commit_tokens
254  (cp_lexer *);
255static void cp_lexer_rollback_tokens
256  (cp_lexer *);
257static inline void cp_lexer_set_source_position_from_token
258  (cp_lexer *, const cp_token *);
259static void cp_lexer_print_token
260  (FILE *, cp_token *);
261static inline bool cp_lexer_debugging_p
262  (cp_lexer *);
263static void cp_lexer_start_debugging
264  (cp_lexer *) ATTRIBUTE_UNUSED;
265static void cp_lexer_stop_debugging
266  (cp_lexer *) ATTRIBUTE_UNUSED;
267
268/* Manifest constants.  */
269
270#define CP_TOKEN_BUFFER_SIZE 5
271#define CP_SAVED_TOKENS_SIZE 5
272
273/* A token type for keywords, as opposed to ordinary identifiers.  */
274#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
275
276/* A token type for template-ids.  If a template-id is processed while
277   parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
278   the value of the CPP_TEMPLATE_ID is whatever was returned by
279   cp_parser_template_id.  */
280#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
281
282/* A token type for nested-name-specifiers.  If a
283   nested-name-specifier is processed while parsing tentatively, it is
284   replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
285   CPP_NESTED_NAME_SPECIFIER is whatever was returned by
286   cp_parser_nested_name_specifier_opt.  */
287#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
288
289/* A token type for tokens that are not tokens at all; these are used
290   to mark the end of a token block.  */
291#define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
292
293/* Variables.  */
294
295/* The stream to which debugging output should be written.  */
296static FILE *cp_lexer_debug_stream;
297
298/* Create a new main C++ lexer, the lexer that gets tokens from the
299   preprocessor.  */
300
301static cp_lexer *
302cp_lexer_new_main (void)
303{
304  cp_lexer *lexer;
305  cp_token first_token;
306
307  /* It's possible that lexing the first token will load a PCH file,
308     which is a GC collection point.  So we have to grab the first
309     token before allocating any memory.  */
310  cp_lexer_get_preprocessor_token (NULL, &first_token);
311  c_common_no_more_pch ();
312
313  /* Allocate the memory.  */
314  lexer = ggc_alloc_cleared (sizeof (cp_lexer));
315
316  /* Create the circular buffer.  */
317  lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
318  lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
319
320  /* There is one token in the buffer.  */
321  lexer->last_token = lexer->buffer + 1;
322  lexer->first_token = lexer->buffer;
323  lexer->next_token = lexer->buffer;
324  memcpy (lexer->buffer, &first_token, sizeof (cp_token));
325
326  /* This lexer obtains more tokens by calling c_lex.  */
327  lexer->main_lexer_p = true;
328
329  /* Create the SAVED_TOKENS stack.  */
330  VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
331
332  /* Create the STRINGS array.  */
333  VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
334
335  /* Assume we are not debugging.  */
336  lexer->debugging_p = false;
337
338  return lexer;
339}
340
341/* Create a new lexer whose token stream is primed with the TOKENS.
342   When these tokens are exhausted, no new tokens will be read.  */
343
344static cp_lexer *
345cp_lexer_new_from_tokens (cp_token_cache *tokens)
346{
347  cp_lexer *lexer;
348  cp_token *token;
349  cp_token_block *block;
350  ptrdiff_t num_tokens;
351
352  /* Allocate the memory.  */
353  lexer = ggc_alloc_cleared (sizeof (cp_lexer));
354
355  /* Create a new buffer, appropriately sized.  */
356  num_tokens = 0;
357  for (block = tokens->first; block != NULL; block = block->next)
358    num_tokens += block->num_tokens;
359  lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
360  lexer->buffer_end = lexer->buffer + num_tokens;
361
362  /* Install the tokens.  */
363  token = lexer->buffer;
364  for (block = tokens->first; block != NULL; block = block->next)
365    {
366      memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
367      token += block->num_tokens;
368    }
369
370  /* The FIRST_TOKEN is the beginning of the buffer.  */
371  lexer->first_token = lexer->buffer;
372  /* The next available token is also at the beginning of the buffer.  */
373  lexer->next_token = lexer->buffer;
374  /* The buffer is full.  */
375  lexer->last_token = lexer->first_token;
376
377  /* This lexer doesn't obtain more tokens.  */
378  lexer->main_lexer_p = false;
379
380  /* Create the SAVED_TOKENS stack.  */
381  VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
382
383  /* Create the STRINGS array.  */
384  VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
385
386  /* Assume we are not debugging.  */
387  lexer->debugging_p = false;
388
389  return lexer;
390}
391
392/* Returns nonzero if debugging information should be output.  */
393
394static inline bool
395cp_lexer_debugging_p (cp_lexer *lexer)
396{
397  return lexer->debugging_p;
398}
399
400/* Set the current source position from the information stored in
401   TOKEN.  */
402
403static inline void
404cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
405                                         const cp_token *token)
406{
407  /* Ideally, the source position information would not be a global
408     variable, but it is.  */
409
410  /* Update the line number.  */
411  if (token->type != CPP_EOF)
412    input_location = token->location;
413}
414
415/* TOKEN points into the circular token buffer.  Return a pointer to
416   the next token in the buffer.  */
417
418static inline cp_token *
419cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
420{
421  token++;
422  if (token == lexer->buffer_end)
423    token = lexer->buffer;
424  return token;
425}
426
427/* TOKEN points into the circular token buffer.  Return a pointer to
428   the previous token in the buffer.  */
429
430static inline cp_token *
431cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
432{
433  if (token == lexer->buffer)
434    token = lexer->buffer_end;
435  return token - 1;
436}
437
438/* nonzero if we are presently saving tokens.  */
439
440static int
441cp_lexer_saving_tokens (const cp_lexer* lexer)
442{
443  return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
444}
445
446/* Return a pointer to the token that is N tokens beyond TOKEN in the
447   buffer.  */
448
449static cp_token *
450cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
451{
452  token += n;
453  if (token >= lexer->buffer_end)
454    token = lexer->buffer + (token - lexer->buffer_end);
455  return token;
456}
457
458/* Returns the number of times that START would have to be incremented
459   to reach FINISH.  If START and FINISH are the same, returns zero.  */
460
461static ptrdiff_t
462cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
463{
464  if (finish >= start)
465    return finish - start;
466  else
467    return ((lexer->buffer_end - lexer->buffer)
468	    - (start - finish));
469}
470
471/* Obtain another token from the C preprocessor and add it to the
472   token buffer.  Returns the newly read token.  */
473
474static cp_token *
475cp_lexer_read_token (cp_lexer* lexer)
476{
477  cp_token *token;
478
479  /* Make sure there is room in the buffer.  */
480  cp_lexer_maybe_grow_buffer (lexer);
481
482  /* If there weren't any tokens, then this one will be the first.  */
483  if (!lexer->first_token)
484    lexer->first_token = lexer->last_token;
485  /* Similarly, if there were no available tokens, there is one now.  */
486  if (!lexer->next_token)
487    lexer->next_token = lexer->last_token;
488
489  /* Figure out where we're going to store the new token.  */
490  token = lexer->last_token;
491
492  /* Get a new token from the preprocessor.  */
493  cp_lexer_get_preprocessor_token (lexer, token);
494
495  /* Increment LAST_TOKEN.  */
496  lexer->last_token = cp_lexer_next_token (lexer, token);
497
498  /* Strings should have type `const char []'.  Right now, we will
499     have an ARRAY_TYPE that is constant rather than an array of
500     constant elements.
501     FIXME: Make fix_string_type get this right in the first place.  */
502  if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
503      && flag_const_strings)
504    {
505      tree type;
506
507      /* Get the current type.  It will be an ARRAY_TYPE.  */
508      type = TREE_TYPE (token->value);
509      /* Use build_cplus_array_type to rebuild the array, thereby
510	 getting the right type.  */
511      type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
512      /* Reset the type of the token.  */
513      TREE_TYPE (token->value) = type;
514    }
515
516  return token;
517}
518
519/* If the circular buffer is full, make it bigger.  */
520
521static void
522cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
523{
524  /* If the buffer is full, enlarge it.  */
525  if (lexer->last_token == lexer->first_token)
526    {
527      cp_token *new_buffer;
528      cp_token *old_buffer;
529      cp_token *new_first_token;
530      ptrdiff_t buffer_length;
531      size_t num_tokens_to_copy;
532
533      /* Remember the current buffer pointer.  It will become invalid,
534	 but we will need to do pointer arithmetic involving this
535	 value.  */
536      old_buffer = lexer->buffer;
537      /* Compute the current buffer size.  */
538      buffer_length = lexer->buffer_end - lexer->buffer;
539      /* Allocate a buffer twice as big.  */
540      new_buffer = ggc_realloc (lexer->buffer,
541				2 * buffer_length * sizeof (cp_token));
542
543      /* Because the buffer is circular, logically consecutive tokens
544	 are not necessarily placed consecutively in memory.
545	 Therefore, we must keep move the tokens that were before
546	 FIRST_TOKEN to the second half of the newly allocated
547	 buffer.  */
548      num_tokens_to_copy = (lexer->first_token - old_buffer);
549      memcpy (new_buffer + buffer_length,
550	      new_buffer,
551	      num_tokens_to_copy * sizeof (cp_token));
552      /* Clear the rest of the buffer.  We never look at this storage,
553	 but the garbage collector may.  */
554      memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
555	      (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
556
557      /* Now recompute all of the buffer pointers.  */
558      new_first_token
559	= new_buffer + (lexer->first_token - old_buffer);
560      if (lexer->next_token != NULL)
561	{
562	  ptrdiff_t next_token_delta;
563
564	  if (lexer->next_token > lexer->first_token)
565	    next_token_delta = lexer->next_token - lexer->first_token;
566	  else
567	    next_token_delta =
568	      buffer_length - (lexer->first_token - lexer->next_token);
569	  lexer->next_token = new_first_token + next_token_delta;
570	}
571      lexer->last_token = new_first_token + buffer_length;
572      lexer->buffer = new_buffer;
573      lexer->buffer_end = new_buffer + buffer_length * 2;
574      lexer->first_token = new_first_token;
575    }
576}
577
578/* Store the next token from the preprocessor in *TOKEN.  */
579
580static void
581cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
582                                 cp_token *token)
583{
584  bool done;
585
586  /* If this not the main lexer, return a terminating CPP_EOF token.  */
587  if (lexer != NULL && !lexer->main_lexer_p)
588    {
589      token->type = CPP_EOF;
590      token->location.line = 0;
591      token->location.file = NULL;
592      token->value = NULL_TREE;
593      token->keyword = RID_MAX;
594
595      return;
596    }
597
598  done = false;
599  /* Keep going until we get a token we like.  */
600  while (!done)
601    {
602      /* Get a new token from the preprocessor.  */
603      token->type = c_lex_with_flags (&token->value, &token->flags);
604      /* Issue messages about tokens we cannot process.  */
605      switch (token->type)
606	{
607	case CPP_ATSIGN:
608	case CPP_HASH:
609	case CPP_PASTE:
610	  error ("invalid token");
611	  break;
612
613	default:
614	  /* This is a good token, so we exit the loop.  */
615	  done = true;
616	  break;
617	}
618    }
619  /* Now we've got our token.  */
620  token->location = input_location;
621
622  /* Check to see if this token is a keyword.  */
623  if (token->type == CPP_NAME
624      && C_IS_RESERVED_WORD (token->value))
625    {
626      /* Mark this token as a keyword.  */
627      token->type = CPP_KEYWORD;
628      /* Record which keyword.  */
629      token->keyword = C_RID_CODE (token->value);
630      /* Update the value.  Some keywords are mapped to particular
631	 entities, rather than simply having the value of the
632	 corresponding IDENTIFIER_NODE.  For example, `__const' is
633	 mapped to `const'.  */
634      token->value = ridpointers[token->keyword];
635    }
636  else
637    token->keyword = RID_MAX;
638}
639
640/* Return a pointer to the next token in the token stream, but do not
641   consume it.  */
642
643static cp_token *
644cp_lexer_peek_token (cp_lexer* lexer)
645{
646  cp_token *token;
647
648  /* If there are no tokens, read one now.  */
649  if (!lexer->next_token)
650    cp_lexer_read_token (lexer);
651
652  /* Provide debugging output.  */
653  if (cp_lexer_debugging_p (lexer))
654    {
655      fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
656      cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
657      fprintf (cp_lexer_debug_stream, "\n");
658    }
659
660  token = lexer->next_token;
661  cp_lexer_set_source_position_from_token (lexer, token);
662  return token;
663}
664
665/* Return true if the next token has the indicated TYPE.  */
666
667static bool
668cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
669{
670  cp_token *token;
671
672  /* Peek at the next token.  */
673  token = cp_lexer_peek_token (lexer);
674  /* Check to see if it has the indicated TYPE.  */
675  return token->type == type;
676}
677
678/* Return true if the next token does not have the indicated TYPE.  */
679
680static bool
681cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
682{
683  return !cp_lexer_next_token_is (lexer, type);
684}
685
686/* Return true if the next token is the indicated KEYWORD.  */
687
688static bool
689cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
690{
691  cp_token *token;
692
693  /* Peek at the next token.  */
694  token = cp_lexer_peek_token (lexer);
695  /* Check to see if it is the indicated keyword.  */
696  return token->keyword == keyword;
697}
698
699/* Return a pointer to the Nth token in the token stream.  If N is 1,
700   then this is precisely equivalent to cp_lexer_peek_token.  */
701
702static cp_token *
703cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
704{
705  cp_token *token;
706
707  /* N is 1-based, not zero-based.  */
708  my_friendly_assert (n > 0, 20000224);
709
710  /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
711  token = lexer->next_token;
712  /* If there are no tokens in the buffer, get one now.  */
713  if (!token)
714    {
715      cp_lexer_read_token (lexer);
716      token = lexer->next_token;
717    }
718
719  /* Now, read tokens until we have enough.  */
720  while (--n > 0)
721    {
722      /* Advance to the next token.  */
723      token = cp_lexer_next_token (lexer, token);
724      /* If that's all the tokens we have, read a new one.  */
725      if (token == lexer->last_token)
726	token = cp_lexer_read_token (lexer);
727    }
728
729  return token;
730}
731
732/* Consume the next token.  The pointer returned is valid only until
733   another token is read.  Callers should preserve copy the token
734   explicitly if they will need its value for a longer period of
735   time.  */
736
737static cp_token *
738cp_lexer_consume_token (cp_lexer* lexer)
739{
740  cp_token *token;
741
742  /* If there are no tokens, read one now.  */
743  if (!lexer->next_token)
744    cp_lexer_read_token (lexer);
745
746  /* Remember the token we'll be returning.  */
747  token = lexer->next_token;
748
749  /* Increment NEXT_TOKEN.  */
750  lexer->next_token = cp_lexer_next_token (lexer,
751					   lexer->next_token);
752  /* Check to see if we're all out of tokens.  */
753  if (lexer->next_token == lexer->last_token)
754    lexer->next_token = NULL;
755
756  /* If we're not saving tokens, then move FIRST_TOKEN too.  */
757  if (!cp_lexer_saving_tokens (lexer))
758    {
759      /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
760      if (!lexer->next_token)
761	lexer->first_token = NULL;
762      else
763	lexer->first_token = lexer->next_token;
764    }
765
766  /* Provide debugging output.  */
767  if (cp_lexer_debugging_p (lexer))
768    {
769      fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
770      cp_lexer_print_token (cp_lexer_debug_stream, token);
771      fprintf (cp_lexer_debug_stream, "\n");
772    }
773
774  return token;
775}
776
777/* Permanently remove the next token from the token stream.  There
778   must be a valid next token already; this token never reads
779   additional tokens from the preprocessor.  */
780
781static void
782cp_lexer_purge_token (cp_lexer *lexer)
783{
784  cp_token *token;
785  cp_token *next_token;
786
787  token = lexer->next_token;
788  while (true)
789    {
790      next_token = cp_lexer_next_token (lexer, token);
791      if (next_token == lexer->last_token)
792	break;
793      *token = *next_token;
794      token = next_token;
795    }
796
797  lexer->last_token = token;
798  /* The token purged may have been the only token remaining; if so,
799     clear NEXT_TOKEN.  */
800  if (lexer->next_token == token)
801    lexer->next_token = NULL;
802}
803
804/* Permanently remove all tokens after TOKEN, up to, but not
805   including, the token that will be returned next by
806   cp_lexer_peek_token.  */
807
808static void
809cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
810{
811  cp_token *peek;
812  cp_token *t1;
813  cp_token *t2;
814
815  if (lexer->next_token)
816    {
817      /* Copy the tokens that have not yet been read to the location
818	 immediately following TOKEN.  */
819      t1 = cp_lexer_next_token (lexer, token);
820      t2 = peek = cp_lexer_peek_token (lexer);
821      /* Move tokens into the vacant area between TOKEN and PEEK.  */
822      while (t2 != lexer->last_token)
823	{
824	  *t1 = *t2;
825	  t1 = cp_lexer_next_token (lexer, t1);
826	  t2 = cp_lexer_next_token (lexer, t2);
827	}
828      /* Now, the next available token is right after TOKEN.  */
829      lexer->next_token = cp_lexer_next_token (lexer, token);
830      /* And the last token is wherever we ended up.  */
831      lexer->last_token = t1;
832    }
833  else
834    {
835      /* There are no tokens in the buffer, so there is nothing to
836	 copy.  The last token in the buffer is TOKEN itself.  */
837      lexer->last_token = cp_lexer_next_token (lexer, token);
838    }
839}
840
841/* Begin saving tokens.  All tokens consumed after this point will be
842   preserved.  */
843
844static void
845cp_lexer_save_tokens (cp_lexer* lexer)
846{
847  /* Provide debugging output.  */
848  if (cp_lexer_debugging_p (lexer))
849    fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
850
851  /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
852     restore the tokens if required.  */
853  if (!lexer->next_token)
854    cp_lexer_read_token (lexer);
855
856  VARRAY_PUSH_INT (lexer->saved_tokens,
857		   cp_lexer_token_difference (lexer,
858					      lexer->first_token,
859					      lexer->next_token));
860}
861
862/* Commit to the portion of the token stream most recently saved.  */
863
864static void
865cp_lexer_commit_tokens (cp_lexer* lexer)
866{
867  /* Provide debugging output.  */
868  if (cp_lexer_debugging_p (lexer))
869    fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
870
871  VARRAY_POP (lexer->saved_tokens);
872}
873
874/* Return all tokens saved since the last call to cp_lexer_save_tokens
875   to the token stream.  Stop saving tokens.  */
876
877static void
878cp_lexer_rollback_tokens (cp_lexer* lexer)
879{
880  size_t delta;
881
882  /* Provide debugging output.  */
883  if (cp_lexer_debugging_p (lexer))
884    fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
885
886  /* Find the token that was the NEXT_TOKEN when we started saving
887     tokens.  */
888  delta = VARRAY_TOP_INT(lexer->saved_tokens);
889  /* Make it the next token again now.  */
890  lexer->next_token = cp_lexer_advance_token (lexer,
891					      lexer->first_token,
892					      delta);
893  /* It might be the case that there were no tokens when we started
894     saving tokens, but that there are some tokens now.  */
895  if (!lexer->next_token && lexer->first_token)
896    lexer->next_token = lexer->first_token;
897
898  /* Stop saving tokens.  */
899  VARRAY_POP (lexer->saved_tokens);
900}
901
902/* Print a representation of the TOKEN on the STREAM.  */
903
904static void
905cp_lexer_print_token (FILE * stream, cp_token* token)
906{
907  const char *token_type = NULL;
908
909  /* Figure out what kind of token this is.  */
910  switch (token->type)
911    {
912    case CPP_EQ:
913      token_type = "EQ";
914      break;
915
916    case CPP_COMMA:
917      token_type = "COMMA";
918      break;
919
920    case CPP_OPEN_PAREN:
921      token_type = "OPEN_PAREN";
922      break;
923
924    case CPP_CLOSE_PAREN:
925      token_type = "CLOSE_PAREN";
926      break;
927
928    case CPP_OPEN_BRACE:
929      token_type = "OPEN_BRACE";
930      break;
931
932    case CPP_CLOSE_BRACE:
933      token_type = "CLOSE_BRACE";
934      break;
935
936    case CPP_SEMICOLON:
937      token_type = "SEMICOLON";
938      break;
939
940    case CPP_NAME:
941      token_type = "NAME";
942      break;
943
944    case CPP_EOF:
945      token_type = "EOF";
946      break;
947
948    case CPP_KEYWORD:
949      token_type = "keyword";
950      break;
951
952      /* This is not a token that we know how to handle yet.  */
953    default:
954      break;
955    }
956
957  /* If we have a name for the token, print it out.  Otherwise, we
958     simply give the numeric code.  */
959  if (token_type)
960    fprintf (stream, "%s", token_type);
961  else
962    fprintf (stream, "%d", token->type);
963  /* And, for an identifier, print the identifier name.  */
964  if (token->type == CPP_NAME
965      /* Some keywords have a value that is not an IDENTIFIER_NODE.
966	 For example, `struct' is mapped to an INTEGER_CST.  */
967      || (token->type == CPP_KEYWORD
968	  && TREE_CODE (token->value) == IDENTIFIER_NODE))
969    fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
970}
971
972/* Start emitting debugging information.  */
973
974static void
975cp_lexer_start_debugging (cp_lexer* lexer)
976{
977  ++lexer->debugging_p;
978}
979
980/* Stop emitting debugging information.  */
981
982static void
983cp_lexer_stop_debugging (cp_lexer* lexer)
984{
985  --lexer->debugging_p;
986}
987
988
989/* The parser.  */
990
991/* Overview
992   --------
993
994   A cp_parser parses the token stream as specified by the C++
995   grammar.  Its job is purely parsing, not semantic analysis.  For
996   example, the parser breaks the token stream into declarators,
997   expressions, statements, and other similar syntactic constructs.
998   It does not check that the types of the expressions on either side
999   of an assignment-statement are compatible, or that a function is
1000   not declared with a parameter of type `void'.
1001
1002   The parser invokes routines elsewhere in the compiler to perform
1003   semantic analysis and to build up the abstract syntax tree for the
1004   code processed.
1005
1006   The parser (and the template instantiation code, which is, in a
1007   way, a close relative of parsing) are the only parts of the
1008   compiler that should be calling push_scope and pop_scope, or
1009   related functions.  The parser (and template instantiation code)
1010   keeps track of what scope is presently active; everything else
1011   should simply honor that.  (The code that generates static
1012   initializers may also need to set the scope, in order to check
1013   access control correctly when emitting the initializers.)
1014
1015   Methodology
1016   -----------
1017
1018   The parser is of the standard recursive-descent variety.  Upcoming
1019   tokens in the token stream are examined in order to determine which
1020   production to use when parsing a non-terminal.  Some C++ constructs
1021   require arbitrary look ahead to disambiguate.  For example, it is
1022   impossible, in the general case, to tell whether a statement is an
1023   expression or declaration without scanning the entire statement.
1024   Therefore, the parser is capable of "parsing tentatively."  When the
1025   parser is not sure what construct comes next, it enters this mode.
1026   Then, while we attempt to parse the construct, the parser queues up
1027   error messages, rather than issuing them immediately, and saves the
1028   tokens it consumes.  If the construct is parsed successfully, the
1029   parser "commits", i.e., it issues any queued error messages and
1030   the tokens that were being preserved are permanently discarded.
1031   If, however, the construct is not parsed successfully, the parser
1032   rolls back its state completely so that it can resume parsing using
1033   a different alternative.
1034
1035   Future Improvements
1036   -------------------
1037
1038   The performance of the parser could probably be improved
1039   substantially.  Some possible improvements include:
1040
1041     - The expression parser recurses through the various levels of
1042       precedence as specified in the grammar, rather than using an
1043       operator-precedence technique.  Therefore, parsing a simple
1044       identifier requires multiple recursive calls.
1045
1046     - We could often eliminate the need to parse tentatively by
1047       looking ahead a little bit.  In some places, this approach
1048       might not entirely eliminate the need to parse tentatively, but
1049       it might still speed up the average case.  */
1050
1051/* Flags that are passed to some parsing functions.  These values can
1052   be bitwise-ored together.  */
1053
1054typedef enum cp_parser_flags
1055{
1056  /* No flags.  */
1057  CP_PARSER_FLAGS_NONE = 0x0,
1058  /* The construct is optional.  If it is not present, then no error
1059     should be issued.  */
1060  CP_PARSER_FLAGS_OPTIONAL = 0x1,
1061  /* When parsing a type-specifier, do not allow user-defined types.  */
1062  CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1063} cp_parser_flags;
1064
1065/* The different kinds of declarators we want to parse.  */
1066
1067typedef enum cp_parser_declarator_kind
1068{
1069  /* We want an abstract declartor.  */
1070  CP_PARSER_DECLARATOR_ABSTRACT,
1071  /* We want a named declarator.  */
1072  CP_PARSER_DECLARATOR_NAMED,
1073  /* We don't mind, but the name must be an unqualified-id.  */
1074  CP_PARSER_DECLARATOR_EITHER
1075} cp_parser_declarator_kind;
1076
1077/* A mapping from a token type to a corresponding tree node type.  */
1078
1079typedef struct cp_parser_token_tree_map_node
1080{
1081  /* The token type.  */
1082  ENUM_BITFIELD (cpp_ttype) token_type : 8;
1083  /* The corresponding tree code.  */
1084  ENUM_BITFIELD (tree_code) tree_type : 8;
1085} cp_parser_token_tree_map_node;
1086
1087/* A complete map consists of several ordinary entries, followed by a
1088   terminator.  The terminating entry has a token_type of CPP_EOF.  */
1089
1090typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1091
1092/* The status of a tentative parse.  */
1093
1094typedef enum cp_parser_status_kind
1095{
1096  /* No errors have occurred.  */
1097  CP_PARSER_STATUS_KIND_NO_ERROR,
1098  /* An error has occurred.  */
1099  CP_PARSER_STATUS_KIND_ERROR,
1100  /* We are committed to this tentative parse, whether or not an error
1101     has occurred.  */
1102  CP_PARSER_STATUS_KIND_COMMITTED
1103} cp_parser_status_kind;
1104
1105/* Context that is saved and restored when parsing tentatively.  */
1106
1107typedef struct cp_parser_context GTY (())
1108{
1109  /* If this is a tentative parsing context, the status of the
1110     tentative parse.  */
1111  enum cp_parser_status_kind status;
1112  /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1113     that are looked up in this context must be looked up both in the
1114     scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1115     the context of the containing expression.  */
1116  tree object_type;
1117  /* The next parsing context in the stack.  */
1118  struct cp_parser_context *next;
1119} cp_parser_context;
1120
1121/* Prototypes.  */
1122
1123/* Constructors and destructors.  */
1124
1125static cp_parser_context *cp_parser_context_new
1126  (cp_parser_context *);
1127
1128/* Class variables.  */
1129
1130static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1131
1132/* Constructors and destructors.  */
1133
1134/* Construct a new context.  The context below this one on the stack
1135   is given by NEXT.  */
1136
1137static cp_parser_context *
1138cp_parser_context_new (cp_parser_context* next)
1139{
1140  cp_parser_context *context;
1141
1142  /* Allocate the storage.  */
1143  if (cp_parser_context_free_list != NULL)
1144    {
1145      /* Pull the first entry from the free list.  */
1146      context = cp_parser_context_free_list;
1147      cp_parser_context_free_list = context->next;
1148      memset (context, 0, sizeof (*context));
1149    }
1150  else
1151    context = ggc_alloc_cleared (sizeof (cp_parser_context));
1152  /* No errors have occurred yet in this context.  */
1153  context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1154  /* If this is not the bottomost context, copy information that we
1155     need from the previous context.  */
1156  if (next)
1157    {
1158      /* If, in the NEXT context, we are parsing an `x->' or `x.'
1159	 expression, then we are parsing one in this context, too.  */
1160      context->object_type = next->object_type;
1161      /* Thread the stack.  */
1162      context->next = next;
1163    }
1164
1165  return context;
1166}
1167
1168/* The cp_parser structure represents the C++ parser.  */
1169
1170typedef struct cp_parser GTY(())
1171{
1172  /* The lexer from which we are obtaining tokens.  */
1173  cp_lexer *lexer;
1174
1175  /* The scope in which names should be looked up.  If NULL_TREE, then
1176     we look up names in the scope that is currently open in the
1177     source program.  If non-NULL, this is either a TYPE or
1178     NAMESPACE_DECL for the scope in which we should look.
1179
1180     This value is not cleared automatically after a name is looked
1181     up, so we must be careful to clear it before starting a new look
1182     up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1183     will look up `Z' in the scope of `X', rather than the current
1184     scope.)  Unfortunately, it is difficult to tell when name lookup
1185     is complete, because we sometimes peek at a token, look it up,
1186     and then decide not to consume it.  */
1187  tree scope;
1188
1189  /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1190     last lookup took place.  OBJECT_SCOPE is used if an expression
1191     like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1192     respectively.  QUALIFYING_SCOPE is used for an expression of the
1193     form "X::Y"; it refers to X.  */
1194  tree object_scope;
1195  tree qualifying_scope;
1196
1197  /* A stack of parsing contexts.  All but the bottom entry on the
1198     stack will be tentative contexts.
1199
1200     We parse tentatively in order to determine which construct is in
1201     use in some situations.  For example, in order to determine
1202     whether a statement is an expression-statement or a
1203     declaration-statement we parse it tentatively as a
1204     declaration-statement.  If that fails, we then reparse the same
1205     token stream as an expression-statement.  */
1206  cp_parser_context *context;
1207
1208  /* True if we are parsing GNU C++.  If this flag is not set, then
1209     GNU extensions are not recognized.  */
1210  bool allow_gnu_extensions_p;
1211
1212  /* TRUE if the `>' token should be interpreted as the greater-than
1213     operator.  FALSE if it is the end of a template-id or
1214     template-parameter-list.  */
1215  bool greater_than_is_operator_p;
1216
1217  /* TRUE if default arguments are allowed within a parameter list
1218     that starts at this point. FALSE if only a gnu extension makes
1219     them permissible.  */
1220  bool default_arg_ok_p;
1221
1222  /* TRUE if we are parsing an integral constant-expression.  See
1223     [expr.const] for a precise definition.  */
1224  bool integral_constant_expression_p;
1225
1226  /* TRUE if we are parsing an integral constant-expression -- but a
1227     non-constant expression should be permitted as well.  This flag
1228     is used when parsing an array bound so that GNU variable-length
1229     arrays are tolerated.  */
1230  bool allow_non_integral_constant_expression_p;
1231
1232  /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1233     been seen that makes the expression non-constant.  */
1234  bool non_integral_constant_expression_p;
1235
1236  /* TRUE if we are parsing the argument to "__offsetof__".  */
1237  bool in_offsetof_p;
1238
1239  /* TRUE if local variable names and `this' are forbidden in the
1240     current context.  */
1241  bool local_variables_forbidden_p;
1242
1243  /* TRUE if the declaration we are parsing is part of a
1244     linkage-specification of the form `extern string-literal
1245     declaration'.  */
1246  bool in_unbraced_linkage_specification_p;
1247
1248  /* TRUE if we are presently parsing a declarator, after the
1249     direct-declarator.  */
1250  bool in_declarator_p;
1251
1252  /* TRUE if we are presently parsing a template-argument-list.  */
1253  bool in_template_argument_list_p;
1254
1255  /* TRUE if we are presently parsing the body of an
1256     iteration-statement.  */
1257  bool in_iteration_statement_p;
1258
1259  /* TRUE if we are presently parsing the body of a switch
1260     statement.  */
1261  bool in_switch_statement_p;
1262
1263  /* TRUE if we are parsing a type-id in an expression context.  In
1264     such a situation, both "type (expr)" and "type (type)" are valid
1265     alternatives.  */
1266  bool in_type_id_in_expr_p;
1267
1268  /* If non-NULL, then we are parsing a construct where new type
1269     definitions are not permitted.  The string stored here will be
1270     issued as an error message if a type is defined.  */
1271  const char *type_definition_forbidden_message;
1272
1273  /* A list of lists. The outer list is a stack, used for member
1274     functions of local classes. At each level there are two sub-list,
1275     one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1276     sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1277     TREE_VALUE's. The functions are chained in reverse declaration
1278     order.
1279
1280     The TREE_PURPOSE sublist contains those functions with default
1281     arguments that need post processing, and the TREE_VALUE sublist
1282     contains those functions with definitions that need post
1283     processing.
1284
1285     These lists can only be processed once the outermost class being
1286     defined is complete.  */
1287  tree unparsed_functions_queues;
1288
1289  /* The number of classes whose definitions are currently in
1290     progress.  */
1291  unsigned num_classes_being_defined;
1292
1293  /* The number of template parameter lists that apply directly to the
1294     current declaration.  */
1295  unsigned num_template_parameter_lists;
1296} cp_parser;
1297
1298/* The type of a function that parses some kind of expression.  */
1299typedef tree (*cp_parser_expression_fn) (cp_parser *);
1300
1301/* Prototypes.  */
1302
1303/* Constructors and destructors.  */
1304
1305static cp_parser *cp_parser_new
1306  (void);
1307
1308/* Routines to parse various constructs.
1309
1310   Those that return `tree' will return the error_mark_node (rather
1311   than NULL_TREE) if a parse error occurs, unless otherwise noted.
1312   Sometimes, they will return an ordinary node if error-recovery was
1313   attempted, even though a parse error occurred.  So, to check
1314   whether or not a parse error occurred, you should always use
1315   cp_parser_error_occurred.  If the construct is optional (indicated
1316   either by an `_opt' in the name of the function that does the
1317   parsing or via a FLAGS parameter), then NULL_TREE is returned if
1318   the construct is not present.  */
1319
1320/* Lexical conventions [gram.lex]  */
1321
1322static tree cp_parser_identifier
1323  (cp_parser *);
1324
1325/* Basic concepts [gram.basic]  */
1326
1327static bool cp_parser_translation_unit
1328  (cp_parser *);
1329
1330/* Expressions [gram.expr]  */
1331
1332static tree cp_parser_primary_expression
1333  (cp_parser *, cp_id_kind *, tree *);
1334static tree cp_parser_id_expression
1335  (cp_parser *, bool, bool, bool *, bool);
1336static tree cp_parser_unqualified_id
1337  (cp_parser *, bool, bool, bool);
1338static tree cp_parser_nested_name_specifier_opt
1339  (cp_parser *, bool, bool, bool, bool);
1340static tree cp_parser_nested_name_specifier
1341  (cp_parser *, bool, bool, bool, bool);
1342static tree cp_parser_class_or_namespace_name
1343  (cp_parser *, bool, bool, bool, bool, bool);
1344static tree cp_parser_postfix_expression
1345  (cp_parser *, bool);
1346static tree cp_parser_parenthesized_expression_list
1347  (cp_parser *, bool, bool *);
1348static void cp_parser_pseudo_destructor_name
1349  (cp_parser *, tree *, tree *);
1350static tree cp_parser_unary_expression
1351  (cp_parser *, bool);
1352static enum tree_code cp_parser_unary_operator
1353  (cp_token *);
1354static tree cp_parser_new_expression
1355  (cp_parser *);
1356static tree cp_parser_new_placement
1357  (cp_parser *);
1358static tree cp_parser_new_type_id
1359  (cp_parser *);
1360static tree cp_parser_new_declarator_opt
1361  (cp_parser *);
1362static tree cp_parser_direct_new_declarator
1363  (cp_parser *);
1364static tree cp_parser_new_initializer
1365  (cp_parser *);
1366static tree cp_parser_delete_expression
1367  (cp_parser *);
1368static tree cp_parser_cast_expression
1369  (cp_parser *, bool);
1370static tree cp_parser_pm_expression
1371  (cp_parser *);
1372static tree cp_parser_multiplicative_expression
1373  (cp_parser *);
1374static tree cp_parser_additive_expression
1375  (cp_parser *);
1376static tree cp_parser_shift_expression
1377  (cp_parser *);
1378static tree cp_parser_relational_expression
1379  (cp_parser *);
1380static tree cp_parser_equality_expression
1381  (cp_parser *);
1382static tree cp_parser_and_expression
1383  (cp_parser *);
1384static tree cp_parser_exclusive_or_expression
1385  (cp_parser *);
1386static tree cp_parser_inclusive_or_expression
1387  (cp_parser *);
1388static tree cp_parser_logical_and_expression
1389  (cp_parser *);
1390static tree cp_parser_logical_or_expression
1391  (cp_parser *);
1392static tree cp_parser_question_colon_clause
1393  (cp_parser *, tree);
1394static tree cp_parser_assignment_expression
1395  (cp_parser *);
1396static enum tree_code cp_parser_assignment_operator_opt
1397  (cp_parser *);
1398static tree cp_parser_expression
1399  (cp_parser *);
1400static tree cp_parser_constant_expression
1401  (cp_parser *, bool, bool *);
1402
1403/* Statements [gram.stmt.stmt]  */
1404
1405static void cp_parser_statement
1406  (cp_parser *, bool);
1407static tree cp_parser_labeled_statement
1408  (cp_parser *, bool);
1409static tree cp_parser_expression_statement
1410  (cp_parser *, bool);
1411static tree cp_parser_compound_statement
1412  (cp_parser *, bool);
1413static void cp_parser_statement_seq_opt
1414  (cp_parser *, bool);
1415static tree cp_parser_selection_statement
1416  (cp_parser *);
1417static tree cp_parser_condition
1418  (cp_parser *);
1419static tree cp_parser_iteration_statement
1420  (cp_parser *);
1421static void cp_parser_for_init_statement
1422  (cp_parser *);
1423static tree cp_parser_jump_statement
1424  (cp_parser *);
1425static void cp_parser_declaration_statement
1426  (cp_parser *);
1427
1428static tree cp_parser_implicitly_scoped_statement
1429  (cp_parser *);
1430static void cp_parser_already_scoped_statement
1431  (cp_parser *);
1432
1433/* Declarations [gram.dcl.dcl] */
1434
1435static void cp_parser_declaration_seq_opt
1436  (cp_parser *);
1437static void cp_parser_declaration
1438  (cp_parser *);
1439static void cp_parser_block_declaration
1440  (cp_parser *, bool);
1441static void cp_parser_simple_declaration
1442  (cp_parser *, bool);
1443static tree cp_parser_decl_specifier_seq
1444  (cp_parser *, cp_parser_flags, tree *, int *);
1445static tree cp_parser_storage_class_specifier_opt
1446  (cp_parser *);
1447static tree cp_parser_function_specifier_opt
1448  (cp_parser *);
1449static tree cp_parser_type_specifier
1450  (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1451static tree cp_parser_simple_type_specifier
1452  (cp_parser *, cp_parser_flags, bool);
1453static tree cp_parser_type_name
1454  (cp_parser *);
1455static tree cp_parser_elaborated_type_specifier
1456  (cp_parser *, bool, bool);
1457static tree cp_parser_enum_specifier
1458  (cp_parser *);
1459static void cp_parser_enumerator_list
1460  (cp_parser *, tree);
1461static void cp_parser_enumerator_definition
1462  (cp_parser *, tree);
1463static tree cp_parser_namespace_name
1464  (cp_parser *);
1465static void cp_parser_namespace_definition
1466  (cp_parser *);
1467static void cp_parser_namespace_body
1468  (cp_parser *);
1469static tree cp_parser_qualified_namespace_specifier
1470  (cp_parser *);
1471static void cp_parser_namespace_alias_definition
1472  (cp_parser *);
1473static void cp_parser_using_declaration
1474  (cp_parser *);
1475static void cp_parser_using_directive
1476  (cp_parser *);
1477static void cp_parser_asm_definition
1478  (cp_parser *);
1479static void cp_parser_linkage_specification
1480  (cp_parser *);
1481
1482/* Declarators [gram.dcl.decl] */
1483
1484static tree cp_parser_init_declarator
1485  (cp_parser *, tree, tree, bool, bool, int, bool *);
1486static tree cp_parser_declarator
1487  (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1488static tree cp_parser_direct_declarator
1489  (cp_parser *, cp_parser_declarator_kind, int *, bool);
1490static enum tree_code cp_parser_ptr_operator
1491  (cp_parser *, tree *, tree *);
1492static tree cp_parser_cv_qualifier_seq_opt
1493  (cp_parser *);
1494static tree cp_parser_cv_qualifier_opt
1495  (cp_parser *);
1496static tree cp_parser_declarator_id
1497  (cp_parser *);
1498static tree cp_parser_type_id
1499  (cp_parser *);
1500static tree cp_parser_type_specifier_seq
1501  (cp_parser *);
1502static tree cp_parser_parameter_declaration_clause
1503  (cp_parser *);
1504static tree cp_parser_parameter_declaration_list
1505  (cp_parser *);
1506static tree cp_parser_parameter_declaration
1507  (cp_parser *, bool, bool *);
1508static void cp_parser_function_body
1509  (cp_parser *);
1510static tree cp_parser_initializer
1511  (cp_parser *, bool *, bool *);
1512static tree cp_parser_initializer_clause
1513  (cp_parser *, bool *);
1514static tree cp_parser_initializer_list
1515  (cp_parser *, bool *);
1516
1517static bool cp_parser_ctor_initializer_opt_and_function_body
1518  (cp_parser *);
1519
1520/* Classes [gram.class] */
1521
1522static tree cp_parser_class_name
1523  (cp_parser *, bool, bool, bool, bool, bool, bool);
1524static tree cp_parser_class_specifier
1525  (cp_parser *);
1526static tree cp_parser_class_head
1527  (cp_parser *, bool *, tree *);
1528static enum tag_types cp_parser_class_key
1529  (cp_parser *);
1530static void cp_parser_member_specification_opt
1531  (cp_parser *);
1532static void cp_parser_member_declaration
1533  (cp_parser *);
1534static tree cp_parser_pure_specifier
1535  (cp_parser *);
1536static tree cp_parser_constant_initializer
1537  (cp_parser *);
1538
1539/* Derived classes [gram.class.derived] */
1540
1541static tree cp_parser_base_clause
1542  (cp_parser *);
1543static tree cp_parser_base_specifier
1544  (cp_parser *);
1545
1546/* Special member functions [gram.special] */
1547
1548static tree cp_parser_conversion_function_id
1549  (cp_parser *);
1550static tree cp_parser_conversion_type_id
1551  (cp_parser *);
1552static tree cp_parser_conversion_declarator_opt
1553  (cp_parser *);
1554static bool cp_parser_ctor_initializer_opt
1555  (cp_parser *);
1556static void cp_parser_mem_initializer_list
1557  (cp_parser *);
1558static tree cp_parser_mem_initializer
1559  (cp_parser *);
1560static tree cp_parser_mem_initializer_id
1561  (cp_parser *);
1562
1563/* Overloading [gram.over] */
1564
1565static tree cp_parser_operator_function_id
1566  (cp_parser *);
1567static tree cp_parser_operator
1568  (cp_parser *);
1569
1570/* Templates [gram.temp] */
1571
1572static void cp_parser_template_declaration
1573  (cp_parser *, bool);
1574static tree cp_parser_template_parameter_list
1575  (cp_parser *);
1576static tree cp_parser_template_parameter
1577  (cp_parser *);
1578static tree cp_parser_type_parameter
1579  (cp_parser *);
1580static tree cp_parser_template_id
1581  (cp_parser *, bool, bool, bool);
1582static tree cp_parser_template_name
1583  (cp_parser *, bool, bool, bool, bool *);
1584static tree cp_parser_template_argument_list
1585  (cp_parser *);
1586static tree cp_parser_template_argument
1587  (cp_parser *);
1588static void cp_parser_explicit_instantiation
1589  (cp_parser *);
1590static void cp_parser_explicit_specialization
1591  (cp_parser *);
1592
1593/* Exception handling [gram.exception] */
1594
1595static tree cp_parser_try_block
1596  (cp_parser *);
1597static bool cp_parser_function_try_block
1598  (cp_parser *);
1599static void cp_parser_handler_seq
1600  (cp_parser *);
1601static void cp_parser_handler
1602  (cp_parser *);
1603static tree cp_parser_exception_declaration
1604  (cp_parser *);
1605static tree cp_parser_throw_expression
1606  (cp_parser *);
1607static tree cp_parser_exception_specification_opt
1608  (cp_parser *);
1609static tree cp_parser_type_id_list
1610  (cp_parser *);
1611
1612/* GNU Extensions */
1613
1614static tree cp_parser_asm_specification_opt
1615  (cp_parser *);
1616static tree cp_parser_asm_operand_list
1617  (cp_parser *);
1618static tree cp_parser_asm_clobber_list
1619  (cp_parser *);
1620static tree cp_parser_attributes_opt
1621  (cp_parser *);
1622static tree cp_parser_attribute_list
1623  (cp_parser *);
1624static bool cp_parser_extension_opt
1625  (cp_parser *, int *);
1626static void cp_parser_label_declaration
1627  (cp_parser *);
1628
1629/* Utility Routines */
1630
1631static tree cp_parser_lookup_name
1632  (cp_parser *, tree, bool, bool, bool, bool);
1633static tree cp_parser_lookup_name_simple
1634  (cp_parser *, tree);
1635static tree cp_parser_maybe_treat_template_as_class
1636  (tree, bool);
1637static bool cp_parser_check_declarator_template_parameters
1638  (cp_parser *, tree);
1639static bool cp_parser_check_template_parameters
1640  (cp_parser *, unsigned);
1641static tree cp_parser_simple_cast_expression
1642  (cp_parser *);
1643static tree cp_parser_binary_expression
1644  (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1645static tree cp_parser_global_scope_opt
1646  (cp_parser *, bool);
1647static bool cp_parser_constructor_declarator_p
1648  (cp_parser *, bool);
1649static tree cp_parser_function_definition_from_specifiers_and_declarator
1650  (cp_parser *, tree, tree, tree);
1651static tree cp_parser_function_definition_after_declarator
1652  (cp_parser *, bool);
1653static void cp_parser_template_declaration_after_export
1654  (cp_parser *, bool);
1655static tree cp_parser_single_declaration
1656  (cp_parser *, bool, bool *);
1657static tree cp_parser_functional_cast
1658  (cp_parser *, tree);
1659static tree cp_parser_save_member_function_body
1660  (cp_parser *, tree, tree, tree);
1661static tree cp_parser_enclosed_template_argument_list
1662  (cp_parser *);
1663static void cp_parser_save_default_args
1664  (cp_parser *, tree);
1665static void cp_parser_late_parsing_for_member
1666  (cp_parser *, tree);
1667static void cp_parser_late_parsing_default_args
1668  (cp_parser *, tree);
1669static tree cp_parser_sizeof_operand
1670  (cp_parser *, enum rid);
1671static bool cp_parser_declares_only_class_p
1672  (cp_parser *);
1673static bool cp_parser_friend_p
1674  (tree);
1675static cp_token *cp_parser_require
1676  (cp_parser *, enum cpp_ttype, const char *);
1677static cp_token *cp_parser_require_keyword
1678  (cp_parser *, enum rid, const char *);
1679static bool cp_parser_token_starts_function_definition_p
1680  (cp_token *);
1681static bool cp_parser_next_token_starts_class_definition_p
1682  (cp_parser *);
1683static bool cp_parser_next_token_ends_template_argument_p
1684  (cp_parser *);
1685static bool cp_parser_nth_token_starts_template_argument_list_p
1686  (cp_parser *, size_t);
1687static enum tag_types cp_parser_token_is_class_key
1688  (cp_token *);
1689static void cp_parser_check_class_key
1690  (enum tag_types, tree type);
1691static void cp_parser_check_access_in_redeclaration
1692  (tree type);
1693static bool cp_parser_optional_template_keyword
1694  (cp_parser *);
1695static void cp_parser_pre_parsed_nested_name_specifier
1696  (cp_parser *);
1697static void cp_parser_cache_group
1698  (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1699static void cp_parser_parse_tentatively
1700  (cp_parser *);
1701static void cp_parser_commit_to_tentative_parse
1702  (cp_parser *);
1703static void cp_parser_abort_tentative_parse
1704  (cp_parser *);
1705static bool cp_parser_parse_definitely
1706  (cp_parser *);
1707static inline bool cp_parser_parsing_tentatively
1708  (cp_parser *);
1709static bool cp_parser_committed_to_tentative_parse
1710  (cp_parser *);
1711static void cp_parser_error
1712  (cp_parser *, const char *);
1713static void cp_parser_name_lookup_error
1714  (cp_parser *, tree, tree, const char *);
1715static bool cp_parser_simulate_error
1716  (cp_parser *);
1717static void cp_parser_check_type_definition
1718  (cp_parser *);
1719static void cp_parser_check_for_definition_in_return_type
1720  (tree, int);
1721static void cp_parser_check_for_invalid_template_id
1722  (cp_parser *, tree);
1723static bool cp_parser_non_integral_constant_expression
1724  (cp_parser *, const char *);
1725static bool cp_parser_diagnose_invalid_type_name
1726  (cp_parser *);
1727static int cp_parser_skip_to_closing_parenthesis
1728  (cp_parser *, bool, bool, bool);
1729static void cp_parser_skip_to_end_of_statement
1730  (cp_parser *);
1731static void cp_parser_consume_semicolon_at_end_of_statement
1732  (cp_parser *);
1733static void cp_parser_skip_to_end_of_block_or_statement
1734  (cp_parser *);
1735static void cp_parser_skip_to_closing_brace
1736  (cp_parser *);
1737static void cp_parser_skip_until_found
1738  (cp_parser *, enum cpp_ttype, const char *);
1739static bool cp_parser_error_occurred
1740  (cp_parser *);
1741static bool cp_parser_allow_gnu_extensions_p
1742  (cp_parser *);
1743static bool cp_parser_is_string_literal
1744  (cp_token *);
1745static bool cp_parser_is_keyword
1746  (cp_token *, enum rid);
1747
1748/* Returns nonzero if we are parsing tentatively.  */
1749
1750static inline bool
1751cp_parser_parsing_tentatively (cp_parser* parser)
1752{
1753  return parser->context->next != NULL;
1754}
1755
1756/* Returns nonzero if TOKEN is a string literal.  */
1757
1758static bool
1759cp_parser_is_string_literal (cp_token* token)
1760{
1761  return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1762}
1763
1764/* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1765
1766static bool
1767cp_parser_is_keyword (cp_token* token, enum rid keyword)
1768{
1769  return token->keyword == keyword;
1770}
1771
1772/* Issue the indicated error MESSAGE.  */
1773
1774static void
1775cp_parser_error (cp_parser* parser, const char* message)
1776{
1777  /* Output the MESSAGE -- unless we're parsing tentatively.  */
1778  if (!cp_parser_simulate_error (parser))
1779    {
1780      cp_token *token;
1781      token = cp_lexer_peek_token (parser->lexer);
1782      c_parse_error (message,
1783		     /* Because c_parser_error does not understand
1784			CPP_KEYWORD, keywords are treated like
1785			identifiers.  */
1786		     (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1787		     token->value);
1788    }
1789}
1790
1791/* Issue an error about name-lookup failing.  NAME is the
1792   IDENTIFIER_NODE DECL is the result of
1793   the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1794   the thing that we hoped to find.  */
1795
1796static void
1797cp_parser_name_lookup_error (cp_parser* parser,
1798			     tree name,
1799			     tree decl,
1800			     const char* desired)
1801{
1802  /* If name lookup completely failed, tell the user that NAME was not
1803     declared.  */
1804  if (decl == error_mark_node)
1805    {
1806      if (parser->scope && parser->scope != global_namespace)
1807	error ("`%D::%D' has not been declared",
1808	       parser->scope, name);
1809      else if (parser->scope == global_namespace)
1810	error ("`::%D' has not been declared", name);
1811      else
1812	error ("`%D' has not been declared", name);
1813    }
1814  else if (parser->scope && parser->scope != global_namespace)
1815    error ("`%D::%D' %s", parser->scope, name, desired);
1816  else if (parser->scope == global_namespace)
1817    error ("`::%D' %s", name, desired);
1818  else
1819    error ("`%D' %s", name, desired);
1820}
1821
1822/* If we are parsing tentatively, remember that an error has occurred
1823   during this tentative parse.  Returns true if the error was
1824   simulated; false if a messgae should be issued by the caller.  */
1825
1826static bool
1827cp_parser_simulate_error (cp_parser* parser)
1828{
1829  if (cp_parser_parsing_tentatively (parser)
1830      && !cp_parser_committed_to_tentative_parse (parser))
1831    {
1832      parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1833      return true;
1834    }
1835  return false;
1836}
1837
1838/* This function is called when a type is defined.  If type
1839   definitions are forbidden at this point, an error message is
1840   issued.  */
1841
1842static void
1843cp_parser_check_type_definition (cp_parser* parser)
1844{
1845  /* If types are forbidden here, issue a message.  */
1846  if (parser->type_definition_forbidden_message)
1847    /* Use `%s' to print the string in case there are any escape
1848       characters in the message.  */
1849    error ("%s", parser->type_definition_forbidden_message);
1850}
1851
1852/* This function is called when a declaration is parsed.  If
1853   DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1854   indicates that a type was defined in the decl-specifiers for DECL,
1855   then an error is issued.  */
1856
1857static void
1858cp_parser_check_for_definition_in_return_type (tree declarator,
1859					       int declares_class_or_enum)
1860{
1861  /* [dcl.fct] forbids type definitions in return types.
1862     Unfortunately, it's not easy to know whether or not we are
1863     processing a return type until after the fact.  */
1864  while (declarator
1865	 && (TREE_CODE (declarator) == INDIRECT_REF
1866	     || TREE_CODE (declarator) == ADDR_EXPR))
1867    declarator = TREE_OPERAND (declarator, 0);
1868  if (declarator
1869      && TREE_CODE (declarator) == CALL_EXPR
1870      && declares_class_or_enum & 2)
1871    error ("new types may not be defined in a return type");
1872}
1873
1874/* A type-specifier (TYPE) has been parsed which cannot be followed by
1875   "<" in any valid C++ program.  If the next token is indeed "<",
1876   issue a message warning the user about what appears to be an
1877   invalid attempt to form a template-id.  */
1878
1879static void
1880cp_parser_check_for_invalid_template_id (cp_parser* parser,
1881					 tree type)
1882{
1883  ptrdiff_t start;
1884  cp_token *token;
1885
1886  if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1887    {
1888      if (TYPE_P (type))
1889	error ("`%T' is not a template", type);
1890      else if (TREE_CODE (type) == IDENTIFIER_NODE)
1891	error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1892      else
1893	error ("invalid template-id");
1894      /* Remember the location of the invalid "<".  */
1895      if (cp_parser_parsing_tentatively (parser)
1896	  && !cp_parser_committed_to_tentative_parse (parser))
1897	{
1898	  token = cp_lexer_peek_token (parser->lexer);
1899	  token = cp_lexer_prev_token (parser->lexer, token);
1900	  start = cp_lexer_token_difference (parser->lexer,
1901					     parser->lexer->first_token,
1902					     token);
1903	}
1904      else
1905	start = -1;
1906      /* Consume the "<".  */
1907      cp_lexer_consume_token (parser->lexer);
1908      /* Parse the template arguments.  */
1909      cp_parser_enclosed_template_argument_list (parser);
1910      /* Permanently remove the invalid template arguments so that
1911	 this error message is not issued again.  */
1912      if (start >= 0)
1913	{
1914	  token = cp_lexer_advance_token (parser->lexer,
1915					  parser->lexer->first_token,
1916					  start);
1917	  cp_lexer_purge_tokens_after (parser->lexer, token);
1918	}
1919    }
1920}
1921
1922/* If parsing an integral constant-expression, issue an error message
1923   about the fact that THING appeared and return true.  Otherwise,
1924   return false, marking the current expression as non-constant.  */
1925
1926static bool
1927cp_parser_non_integral_constant_expression (cp_parser  *parser,
1928					    const char *thing)
1929{
1930  if (parser->integral_constant_expression_p)
1931    {
1932      if (!parser->allow_non_integral_constant_expression_p)
1933	{
1934	  error ("%s cannot appear in a constant-expression", thing);
1935	  return true;
1936	}
1937      parser->non_integral_constant_expression_p = true;
1938    }
1939  return false;
1940}
1941
1942/* Check for a common situation where a type-name should be present,
1943   but is not, and issue a sensible error message.  Returns true if an
1944   invalid type-name was detected.  */
1945
1946static bool
1947cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1948{
1949  /* If the next two tokens are both identifiers, the code is
1950     erroneous. The usual cause of this situation is code like:
1951
1952       T t;
1953
1954     where "T" should name a type -- but does not.  */
1955  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1956      && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1957    {
1958      tree name;
1959
1960      /* If parsing tentatively, we should commit; we really are
1961	 looking at a declaration.  */
1962      /* Consume the first identifier.  */
1963      name = cp_lexer_consume_token (parser->lexer)->value;
1964      /* Issue an error message.  */
1965      error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1966      /* If we're in a template class, it's possible that the user was
1967	 referring to a type from a base class.  For example:
1968
1969	   template <typename T> struct A { typedef T X; };
1970	   template <typename T> struct B : public A<T> { X x; };
1971
1972	 The user should have said "typename A<T>::X".  */
1973      if (processing_template_decl && current_class_type)
1974	{
1975	  tree b;
1976
1977	  for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1978	       b;
1979	       b = TREE_CHAIN (b))
1980	    {
1981	      tree base_type = BINFO_TYPE (b);
1982	      if (CLASS_TYPE_P (base_type)
1983		  && dependent_type_p (base_type))
1984		{
1985		  tree field;
1986		  /* Go from a particular instantiation of the
1987		     template (which will have an empty TYPE_FIELDs),
1988		     to the main version.  */
1989		  base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1990		  for (field = TYPE_FIELDS (base_type);
1991		       field;
1992		       field = TREE_CHAIN (field))
1993		    if (TREE_CODE (field) == TYPE_DECL
1994			&& DECL_NAME (field) == name)
1995		      {
1996			error ("(perhaps `typename %T::%s' was intended)",
1997			       BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1998			break;
1999		      }
2000		  if (field)
2001		    break;
2002		}
2003	    }
2004	}
2005      /* Skip to the end of the declaration; there's no point in
2006	 trying to process it.  */
2007      cp_parser_skip_to_end_of_statement (parser);
2008
2009      return true;
2010    }
2011
2012  return false;
2013}
2014
2015/* Consume tokens up to, and including, the next non-nested closing `)'.
2016   Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2017   are doing error recovery. Returns -1 if OR_COMMA is true and we
2018   found an unnested comma.  */
2019
2020static int
2021cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2022				       bool recovering,
2023				       bool or_comma,
2024				       bool consume_paren)
2025{
2026  unsigned paren_depth = 0;
2027  unsigned brace_depth = 0;
2028
2029  if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2030      && !cp_parser_committed_to_tentative_parse (parser))
2031    return 0;
2032
2033  while (true)
2034    {
2035      cp_token *token;
2036
2037      /* If we've run out of tokens, then there is no closing `)'.  */
2038      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2039	return 0;
2040
2041      token = cp_lexer_peek_token (parser->lexer);
2042
2043      /* This matches the processing in skip_to_end_of_statement.  */
2044      if (token->type == CPP_SEMICOLON && !brace_depth)
2045	return 0;
2046      if (token->type == CPP_OPEN_BRACE)
2047	++brace_depth;
2048      if (token->type == CPP_CLOSE_BRACE)
2049	{
2050	  if (!brace_depth--)
2051	    return 0;
2052	}
2053      if (recovering && or_comma && token->type == CPP_COMMA
2054	  && !brace_depth && !paren_depth)
2055	return -1;
2056
2057      if (!brace_depth)
2058	{
2059	  /* If it is an `(', we have entered another level of nesting.  */
2060	  if (token->type == CPP_OPEN_PAREN)
2061	    ++paren_depth;
2062	  /* If it is a `)', then we might be done.  */
2063	  else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2064	    {
2065	      if (consume_paren)
2066		cp_lexer_consume_token (parser->lexer);
2067	      return 1;
2068	    }
2069	}
2070
2071      /* Consume the token.  */
2072      cp_lexer_consume_token (parser->lexer);
2073    }
2074}
2075
2076/* Consume tokens until we reach the end of the current statement.
2077   Normally, that will be just before consuming a `;'.  However, if a
2078   non-nested `}' comes first, then we stop before consuming that.  */
2079
2080static void
2081cp_parser_skip_to_end_of_statement (cp_parser* parser)
2082{
2083  unsigned nesting_depth = 0;
2084
2085  while (true)
2086    {
2087      cp_token *token;
2088
2089      /* Peek at the next token.  */
2090      token = cp_lexer_peek_token (parser->lexer);
2091      /* If we've run out of tokens, stop.  */
2092      if (token->type == CPP_EOF)
2093	break;
2094      /* If the next token is a `;', we have reached the end of the
2095	 statement.  */
2096      if (token->type == CPP_SEMICOLON && !nesting_depth)
2097	break;
2098      /* If the next token is a non-nested `}', then we have reached
2099	 the end of the current block.  */
2100      if (token->type == CPP_CLOSE_BRACE)
2101	{
2102	  /* If this is a non-nested `}', stop before consuming it.
2103	     That way, when confronted with something like:
2104
2105	       { 3 + }
2106
2107	     we stop before consuming the closing `}', even though we
2108	     have not yet reached a `;'.  */
2109	  if (nesting_depth == 0)
2110	    break;
2111	  /* If it is the closing `}' for a block that we have
2112	     scanned, stop -- but only after consuming the token.
2113	     That way given:
2114
2115	        void f g () { ... }
2116		typedef int I;
2117
2118	     we will stop after the body of the erroneously declared
2119	     function, but before consuming the following `typedef'
2120	     declaration.  */
2121	  if (--nesting_depth == 0)
2122	    {
2123	      cp_lexer_consume_token (parser->lexer);
2124	      break;
2125	    }
2126	}
2127      /* If it the next token is a `{', then we are entering a new
2128	 block.  Consume the entire block.  */
2129      else if (token->type == CPP_OPEN_BRACE)
2130	++nesting_depth;
2131      /* Consume the token.  */
2132      cp_lexer_consume_token (parser->lexer);
2133    }
2134}
2135
2136/* This function is called at the end of a statement or declaration.
2137   If the next token is a semicolon, it is consumed; otherwise, error
2138   recovery is attempted.  */
2139
2140static void
2141cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2142{
2143  /* Look for the trailing `;'.  */
2144  if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2145    {
2146      /* If there is additional (erroneous) input, skip to the end of
2147	 the statement.  */
2148      cp_parser_skip_to_end_of_statement (parser);
2149      /* If the next token is now a `;', consume it.  */
2150      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2151	cp_lexer_consume_token (parser->lexer);
2152    }
2153}
2154
2155/* Skip tokens until we have consumed an entire block, or until we
2156   have consumed a non-nested `;'.  */
2157
2158static void
2159cp_parser_skip_to_end_of_block_or_statement (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 `;', we have reached the end of the
2173	 statement.  */
2174      if (token->type == CPP_SEMICOLON && !nesting_depth)
2175	{
2176	  /* Consume the `;'.  */
2177	  cp_lexer_consume_token (parser->lexer);
2178	  break;
2179	}
2180      /* Consume the token.  */
2181      token = cp_lexer_consume_token (parser->lexer);
2182      /* If the next token is a non-nested `}', then we have reached
2183	 the end of the current block.  */
2184      if (token->type == CPP_CLOSE_BRACE
2185	  && (nesting_depth == 0 || --nesting_depth == 0))
2186	break;
2187      /* If it the next token is a `{', then we are entering a new
2188	 block.  Consume the entire block.  */
2189      if (token->type == CPP_OPEN_BRACE)
2190	++nesting_depth;
2191    }
2192}
2193
2194/* Skip tokens until a non-nested closing curly brace is the next
2195   token.  */
2196
2197static void
2198cp_parser_skip_to_closing_brace (cp_parser *parser)
2199{
2200  unsigned nesting_depth = 0;
2201
2202  while (true)
2203    {
2204      cp_token *token;
2205
2206      /* Peek at the next token.  */
2207      token = cp_lexer_peek_token (parser->lexer);
2208      /* If we've run out of tokens, stop.  */
2209      if (token->type == CPP_EOF)
2210	break;
2211      /* If the next token is a non-nested `}', then we have reached
2212	 the end of the current block.  */
2213      if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2214	break;
2215      /* If it the next token is a `{', then we are entering a new
2216	 block.  Consume the entire block.  */
2217      else if (token->type == CPP_OPEN_BRACE)
2218	++nesting_depth;
2219      /* Consume the token.  */
2220      cp_lexer_consume_token (parser->lexer);
2221    }
2222}
2223
2224/* Create a new C++ parser.  */
2225
2226static cp_parser *
2227cp_parser_new (void)
2228{
2229  cp_parser *parser;
2230  cp_lexer *lexer;
2231
2232  /* cp_lexer_new_main is called before calling ggc_alloc because
2233     cp_lexer_new_main might load a PCH file.  */
2234  lexer = cp_lexer_new_main ();
2235
2236  parser = ggc_alloc_cleared (sizeof (cp_parser));
2237  parser->lexer = lexer;
2238  parser->context = cp_parser_context_new (NULL);
2239
2240  /* For now, we always accept GNU extensions.  */
2241  parser->allow_gnu_extensions_p = 1;
2242
2243  /* The `>' token is a greater-than operator, not the end of a
2244     template-id.  */
2245  parser->greater_than_is_operator_p = true;
2246
2247  parser->default_arg_ok_p = true;
2248
2249  /* We are not parsing a constant-expression.  */
2250  parser->integral_constant_expression_p = false;
2251  parser->allow_non_integral_constant_expression_p = false;
2252  parser->non_integral_constant_expression_p = false;
2253
2254  /* We are not parsing offsetof.  */
2255  parser->in_offsetof_p = false;
2256
2257  /* Local variable names are not forbidden.  */
2258  parser->local_variables_forbidden_p = false;
2259
2260  /* We are not processing an `extern "C"' declaration.  */
2261  parser->in_unbraced_linkage_specification_p = false;
2262
2263  /* We are not processing a declarator.  */
2264  parser->in_declarator_p = false;
2265
2266  /* We are not processing a template-argument-list.  */
2267  parser->in_template_argument_list_p = false;
2268
2269  /* We are not in an iteration statement.  */
2270  parser->in_iteration_statement_p = false;
2271
2272  /* We are not in a switch statement.  */
2273  parser->in_switch_statement_p = false;
2274
2275  /* We are not parsing a type-id inside an expression.  */
2276  parser->in_type_id_in_expr_p = false;
2277
2278  /* The unparsed function queue is empty.  */
2279  parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2280
2281  /* There are no classes being defined.  */
2282  parser->num_classes_being_defined = 0;
2283
2284  /* No template parameters apply.  */
2285  parser->num_template_parameter_lists = 0;
2286
2287  return parser;
2288}
2289
2290/* Lexical conventions [gram.lex]  */
2291
2292/* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2293   identifier.  */
2294
2295static tree
2296cp_parser_identifier (cp_parser* parser)
2297{
2298  cp_token *token;
2299
2300  /* Look for the identifier.  */
2301  token = cp_parser_require (parser, CPP_NAME, "identifier");
2302  /* Return the value.  */
2303  return token ? token->value : error_mark_node;
2304}
2305
2306/* Basic concepts [gram.basic]  */
2307
2308/* Parse a translation-unit.
2309
2310   translation-unit:
2311     declaration-seq [opt]
2312
2313   Returns TRUE if all went well.  */
2314
2315static bool
2316cp_parser_translation_unit (cp_parser* parser)
2317{
2318  while (true)
2319    {
2320      cp_parser_declaration_seq_opt (parser);
2321
2322      /* If there are no tokens left then all went well.  */
2323      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2324	break;
2325
2326      /* Otherwise, issue an error message.  */
2327      cp_parser_error (parser, "expected declaration");
2328      return false;
2329    }
2330
2331  /* Consume the EOF token.  */
2332  cp_parser_require (parser, CPP_EOF, "end-of-file");
2333
2334  /* Finish up.  */
2335  finish_translation_unit ();
2336
2337  /* All went well.  */
2338  return true;
2339}
2340
2341/* Expressions [gram.expr] */
2342
2343/* Parse a primary-expression.
2344
2345   primary-expression:
2346     literal
2347     this
2348     ( expression )
2349     id-expression
2350
2351   GNU Extensions:
2352
2353   primary-expression:
2354     ( compound-statement )
2355     __builtin_va_arg ( assignment-expression , type-id )
2356
2357   literal:
2358     __null
2359
2360   Returns a representation of the expression.
2361
2362   *IDK indicates what kind of id-expression (if any) was present.
2363
2364   *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2365   used as the operand of a pointer-to-member.  In that case,
2366   *QUALIFYING_CLASS gives the class that is used as the qualifying
2367   class in the pointer-to-member.  */
2368
2369static tree
2370cp_parser_primary_expression (cp_parser *parser,
2371			      cp_id_kind *idk,
2372			      tree *qualifying_class)
2373{
2374  cp_token *token;
2375
2376  /* Assume the primary expression is not an id-expression.  */
2377  *idk = CP_ID_KIND_NONE;
2378  /* And that it cannot be used as pointer-to-member.  */
2379  *qualifying_class = NULL_TREE;
2380
2381  /* Peek at the next token.  */
2382  token = cp_lexer_peek_token (parser->lexer);
2383  switch (token->type)
2384    {
2385      /* literal:
2386	   integer-literal
2387	   character-literal
2388	   floating-literal
2389	   string-literal
2390	   boolean-literal  */
2391    case CPP_CHAR:
2392    case CPP_WCHAR:
2393    case CPP_STRING:
2394    case CPP_WSTRING:
2395    case CPP_NUMBER:
2396      token = cp_lexer_consume_token (parser->lexer);
2397      return token->value;
2398
2399    case CPP_OPEN_PAREN:
2400      {
2401	tree expr;
2402	bool saved_greater_than_is_operator_p;
2403
2404	/* Consume the `('.  */
2405	cp_lexer_consume_token (parser->lexer);
2406	/* Within a parenthesized expression, a `>' token is always
2407	   the greater-than operator.  */
2408	saved_greater_than_is_operator_p
2409	  = parser->greater_than_is_operator_p;
2410	parser->greater_than_is_operator_p = true;
2411	/* If we see `( { ' then we are looking at the beginning of
2412	   a GNU statement-expression.  */
2413	if (cp_parser_allow_gnu_extensions_p (parser)
2414	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2415	  {
2416	    /* Statement-expressions are not allowed by the standard.  */
2417	    if (pedantic)
2418	      pedwarn ("ISO C++ forbids braced-groups within expressions");
2419
2420	    /* And they're not allowed outside of a function-body; you
2421	       cannot, for example, write:
2422
2423	         int i = ({ int j = 3; j + 1; });
2424
2425	       at class or namespace scope.  */
2426	    if (!at_function_scope_p ())
2427	      error ("statement-expressions are allowed only inside functions");
2428	    /* Start the statement-expression.  */
2429	    expr = begin_stmt_expr ();
2430	    /* Parse the compound-statement.  */
2431	    cp_parser_compound_statement (parser, true);
2432	    /* Finish up.  */
2433	    expr = finish_stmt_expr (expr, false);
2434	  }
2435	else
2436	  {
2437	    /* Parse the parenthesized expression.  */
2438	    expr = cp_parser_expression (parser);
2439	    /* Let the front end know that this expression was
2440	       enclosed in parentheses. This matters in case, for
2441	       example, the expression is of the form `A::B', since
2442	       `&A::B' might be a pointer-to-member, but `&(A::B)' is
2443	       not.  */
2444	    finish_parenthesized_expr (expr);
2445	  }
2446	/* The `>' token might be the end of a template-id or
2447	   template-parameter-list now.  */
2448	parser->greater_than_is_operator_p
2449	  = saved_greater_than_is_operator_p;
2450	/* Consume the `)'.  */
2451	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2452	  cp_parser_skip_to_end_of_statement (parser);
2453
2454	return expr;
2455      }
2456
2457    case CPP_KEYWORD:
2458      switch (token->keyword)
2459	{
2460	  /* These two are the boolean literals.  */
2461	case RID_TRUE:
2462	  cp_lexer_consume_token (parser->lexer);
2463	  return boolean_true_node;
2464	case RID_FALSE:
2465	  cp_lexer_consume_token (parser->lexer);
2466	  return boolean_false_node;
2467
2468	  /* The `__null' literal.  */
2469	case RID_NULL:
2470	  cp_lexer_consume_token (parser->lexer);
2471	  return null_node;
2472
2473	  /* Recognize the `this' keyword.  */
2474	case RID_THIS:
2475	  cp_lexer_consume_token (parser->lexer);
2476	  if (parser->local_variables_forbidden_p)
2477	    {
2478	      error ("`this' may not be used in this context");
2479	      return error_mark_node;
2480	    }
2481	  /* Pointers cannot appear in constant-expressions.  */
2482	  if (cp_parser_non_integral_constant_expression (parser,
2483							  "`this'"))
2484	    return error_mark_node;
2485	  return finish_this_expr ();
2486
2487	  /* The `operator' keyword can be the beginning of an
2488	     id-expression.  */
2489	case RID_OPERATOR:
2490	  goto id_expression;
2491
2492	case RID_FUNCTION_NAME:
2493	case RID_PRETTY_FUNCTION_NAME:
2494	case RID_C99_FUNCTION_NAME:
2495	  /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2496	     __func__ are the names of variables -- but they are
2497	     treated specially.  Therefore, they are handled here,
2498	     rather than relying on the generic id-expression logic
2499	     below.  Grammatically, these names are id-expressions.
2500
2501	     Consume the token.  */
2502	  token = cp_lexer_consume_token (parser->lexer);
2503	  /* Look up the name.  */
2504	  return finish_fname (token->value);
2505
2506	case RID_VA_ARG:
2507	  {
2508	    tree expression;
2509	    tree type;
2510
2511	    /* The `__builtin_va_arg' construct is used to handle
2512	       `va_arg'.  Consume the `__builtin_va_arg' token.  */
2513	    cp_lexer_consume_token (parser->lexer);
2514	    /* Look for the opening `('.  */
2515	    cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2516	    /* Now, parse the assignment-expression.  */
2517	    expression = cp_parser_assignment_expression (parser);
2518	    /* Look for the `,'.  */
2519	    cp_parser_require (parser, CPP_COMMA, "`,'");
2520	    /* Parse the type-id.  */
2521	    type = cp_parser_type_id (parser);
2522	    /* Look for the closing `)'.  */
2523	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2524	    /* Using `va_arg' in a constant-expression is not
2525	       allowed.  */
2526	    if (cp_parser_non_integral_constant_expression (parser,
2527							    "`va_arg'"))
2528	      return error_mark_node;
2529	    return build_x_va_arg (expression, type);
2530	  }
2531
2532	case RID_OFFSETOF:
2533	  {
2534	    tree expression;
2535	    bool saved_in_offsetof_p;
2536
2537	    /* Consume the "__offsetof__" token.  */
2538	    cp_lexer_consume_token (parser->lexer);
2539	    /* Consume the opening `('.  */
2540	    cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2541	    /* Parse the parenthesized (almost) constant-expression.  */
2542	    saved_in_offsetof_p = parser->in_offsetof_p;
2543	    parser->in_offsetof_p = true;
2544	    expression
2545	      = cp_parser_constant_expression (parser,
2546					       /*allow_non_constant_p=*/false,
2547					       /*non_constant_p=*/NULL);
2548	    parser->in_offsetof_p = saved_in_offsetof_p;
2549	    /* Consume the closing ')'.  */
2550	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2551
2552	    return expression;
2553	  }
2554
2555	default:
2556	  cp_parser_error (parser, "expected primary-expression");
2557	  return error_mark_node;
2558	}
2559
2560      /* An id-expression can start with either an identifier, a
2561	 `::' as the beginning of a qualified-id, or the "operator"
2562	 keyword.  */
2563    case CPP_NAME:
2564    case CPP_SCOPE:
2565    case CPP_TEMPLATE_ID:
2566    case CPP_NESTED_NAME_SPECIFIER:
2567      {
2568	tree id_expression;
2569	tree decl;
2570	const char *error_msg;
2571
2572      id_expression:
2573	/* Parse the id-expression.  */
2574	id_expression
2575	  = cp_parser_id_expression (parser,
2576				     /*template_keyword_p=*/false,
2577				     /*check_dependency_p=*/true,
2578				     /*template_p=*/NULL,
2579				     /*declarator_p=*/false);
2580	if (id_expression == error_mark_node)
2581	  return error_mark_node;
2582	/* If we have a template-id, then no further lookup is
2583	   required.  If the template-id was for a template-class, we
2584	   will sometimes have a TYPE_DECL at this point.  */
2585	else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2586	    || TREE_CODE (id_expression) == TYPE_DECL)
2587	  decl = id_expression;
2588	/* Look up the name.  */
2589	else
2590	  {
2591	    decl = cp_parser_lookup_name_simple (parser, id_expression);
2592	    /* If name lookup gives us a SCOPE_REF, then the
2593	       qualifying scope was dependent.  Just propagate the
2594	       name.  */
2595	    if (TREE_CODE (decl) == SCOPE_REF)
2596	      {
2597		if (TYPE_P (TREE_OPERAND (decl, 0)))
2598		  *qualifying_class = TREE_OPERAND (decl, 0);
2599		return decl;
2600	      }
2601	    /* Check to see if DECL is a local variable in a context
2602	       where that is forbidden.  */
2603	    if (parser->local_variables_forbidden_p
2604		&& local_variable_p (decl))
2605	      {
2606		/* It might be that we only found DECL because we are
2607		   trying to be generous with pre-ISO scoping rules.
2608		   For example, consider:
2609
2610		     int i;
2611		     void g() {
2612		       for (int i = 0; i < 10; ++i) {}
2613		       extern void f(int j = i);
2614		     }
2615
2616		   Here, name look up will originally find the out
2617		   of scope `i'.  We need to issue a warning message,
2618		   but then use the global `i'.  */
2619		decl = check_for_out_of_scope_variable (decl);
2620		if (local_variable_p (decl))
2621		  {
2622		    error ("local variable `%D' may not appear in this context",
2623			   decl);
2624		    return error_mark_node;
2625		  }
2626	      }
2627	  }
2628
2629	decl = finish_id_expression (id_expression, decl, parser->scope,
2630				     idk, qualifying_class,
2631				     parser->integral_constant_expression_p,
2632				     parser->allow_non_integral_constant_expression_p,
2633				     &parser->non_integral_constant_expression_p,
2634				     &error_msg);
2635	if (error_msg)
2636	  cp_parser_error (parser, error_msg);
2637	return decl;
2638      }
2639
2640      /* Anything else is an error.  */
2641    default:
2642      cp_parser_error (parser, "expected primary-expression");
2643      return error_mark_node;
2644    }
2645}
2646
2647/* Parse an id-expression.
2648
2649   id-expression:
2650     unqualified-id
2651     qualified-id
2652
2653   qualified-id:
2654     :: [opt] nested-name-specifier template [opt] unqualified-id
2655     :: identifier
2656     :: operator-function-id
2657     :: template-id
2658
2659   Return a representation of the unqualified portion of the
2660   identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2661   a `::' or nested-name-specifier.
2662
2663   Often, if the id-expression was a qualified-id, the caller will
2664   want to make a SCOPE_REF to represent the qualified-id.  This
2665   function does not do this in order to avoid wastefully creating
2666   SCOPE_REFs when they are not required.
2667
2668   If TEMPLATE_KEYWORD_P is true, then we have just seen the
2669   `template' keyword.
2670
2671   If CHECK_DEPENDENCY_P is false, then names are looked up inside
2672   uninstantiated templates.
2673
2674   If *TEMPLATE_P is non-NULL, it is set to true iff the
2675   `template' keyword is used to explicitly indicate that the entity
2676   named is a template.
2677
2678   If DECLARATOR_P is true, the id-expression is appearing as part of
2679   a declarator, rather than as part of an expression.  */
2680
2681static tree
2682cp_parser_id_expression (cp_parser *parser,
2683			 bool template_keyword_p,
2684			 bool check_dependency_p,
2685			 bool *template_p,
2686			 bool declarator_p)
2687{
2688  bool global_scope_p;
2689  bool nested_name_specifier_p;
2690
2691  /* Assume the `template' keyword was not used.  */
2692  if (template_p)
2693    *template_p = false;
2694
2695  /* Look for the optional `::' operator.  */
2696  global_scope_p
2697    = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2698       != NULL_TREE);
2699  /* Look for the optional nested-name-specifier.  */
2700  nested_name_specifier_p
2701    = (cp_parser_nested_name_specifier_opt (parser,
2702					    /*typename_keyword_p=*/false,
2703					    check_dependency_p,
2704					    /*type_p=*/false,
2705					    declarator_p)
2706       != NULL_TREE);
2707  /* If there is a nested-name-specifier, then we are looking at
2708     the first qualified-id production.  */
2709  if (nested_name_specifier_p)
2710    {
2711      tree saved_scope;
2712      tree saved_object_scope;
2713      tree saved_qualifying_scope;
2714      tree unqualified_id;
2715      bool is_template;
2716
2717      /* See if the next token is the `template' keyword.  */
2718      if (!template_p)
2719	template_p = &is_template;
2720      *template_p = cp_parser_optional_template_keyword (parser);
2721      /* Name lookup we do during the processing of the
2722	 unqualified-id might obliterate SCOPE.  */
2723      saved_scope = parser->scope;
2724      saved_object_scope = parser->object_scope;
2725      saved_qualifying_scope = parser->qualifying_scope;
2726      /* Process the final unqualified-id.  */
2727      unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2728						 check_dependency_p,
2729						 declarator_p);
2730      /* Restore the SAVED_SCOPE for our caller.  */
2731      parser->scope = saved_scope;
2732      parser->object_scope = saved_object_scope;
2733      parser->qualifying_scope = saved_qualifying_scope;
2734
2735      return unqualified_id;
2736    }
2737  /* Otherwise, if we are in global scope, then we are looking at one
2738     of the other qualified-id productions.  */
2739  else if (global_scope_p)
2740    {
2741      cp_token *token;
2742      tree id;
2743
2744      /* Peek at the next token.  */
2745      token = cp_lexer_peek_token (parser->lexer);
2746
2747      /* If it's an identifier, and the next token is not a "<", then
2748	 we can avoid the template-id case.  This is an optimization
2749	 for this common case.  */
2750      if (token->type == CPP_NAME
2751	  && !cp_parser_nth_token_starts_template_argument_list_p
2752	       (parser, 2))
2753	return cp_parser_identifier (parser);
2754
2755      cp_parser_parse_tentatively (parser);
2756      /* Try a template-id.  */
2757      id = cp_parser_template_id (parser,
2758				  /*template_keyword_p=*/false,
2759				  /*check_dependency_p=*/true,
2760				  declarator_p);
2761      /* If that worked, we're done.  */
2762      if (cp_parser_parse_definitely (parser))
2763	return id;
2764
2765      /* Peek at the next token.  (Changes in the token buffer may
2766	 have invalidated the pointer obtained above.)  */
2767      token = cp_lexer_peek_token (parser->lexer);
2768
2769      switch (token->type)
2770	{
2771	case CPP_NAME:
2772	  return cp_parser_identifier (parser);
2773
2774	case CPP_KEYWORD:
2775	  if (token->keyword == RID_OPERATOR)
2776	    return cp_parser_operator_function_id (parser);
2777	  /* Fall through.  */
2778
2779	default:
2780	  cp_parser_error (parser, "expected id-expression");
2781	  return error_mark_node;
2782	}
2783    }
2784  else
2785    return cp_parser_unqualified_id (parser, template_keyword_p,
2786				     /*check_dependency_p=*/true,
2787				     declarator_p);
2788}
2789
2790/* Parse an unqualified-id.
2791
2792   unqualified-id:
2793     identifier
2794     operator-function-id
2795     conversion-function-id
2796     ~ class-name
2797     template-id
2798
2799   If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2800   keyword, in a construct like `A::template ...'.
2801
2802   Returns a representation of unqualified-id.  For the `identifier'
2803   production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
2804   production a BIT_NOT_EXPR is returned; the operand of the
2805   BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
2806   other productions, see the documentation accompanying the
2807   corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
2808   names are looked up in uninstantiated templates.  If DECLARATOR_P
2809   is true, the unqualified-id is appearing as part of a declarator,
2810   rather than as part of an expression.  */
2811
2812static tree
2813cp_parser_unqualified_id (cp_parser* parser,
2814                          bool template_keyword_p,
2815			  bool check_dependency_p,
2816			  bool declarator_p)
2817{
2818  cp_token *token;
2819
2820  /* Peek at the next token.  */
2821  token = cp_lexer_peek_token (parser->lexer);
2822
2823  switch (token->type)
2824    {
2825    case CPP_NAME:
2826      {
2827	tree id;
2828
2829	/* We don't know yet whether or not this will be a
2830	   template-id.  */
2831	cp_parser_parse_tentatively (parser);
2832	/* Try a template-id.  */
2833	id = cp_parser_template_id (parser, template_keyword_p,
2834				    check_dependency_p,
2835				    declarator_p);
2836	/* If it worked, we're done.  */
2837	if (cp_parser_parse_definitely (parser))
2838	  return id;
2839	/* Otherwise, it's an ordinary identifier.  */
2840	return cp_parser_identifier (parser);
2841      }
2842
2843    case CPP_TEMPLATE_ID:
2844      return cp_parser_template_id (parser, template_keyword_p,
2845				    check_dependency_p,
2846				    declarator_p);
2847
2848    case CPP_COMPL:
2849      {
2850	tree type_decl;
2851	tree qualifying_scope;
2852	tree object_scope;
2853	tree scope;
2854	bool done;
2855
2856	/* Consume the `~' token.  */
2857	cp_lexer_consume_token (parser->lexer);
2858	/* Parse the class-name.  The standard, as written, seems to
2859	   say that:
2860
2861	     template <typename T> struct S { ~S (); };
2862	     template <typename T> S<T>::~S() {}
2863
2864           is invalid, since `~' must be followed by a class-name, but
2865	   `S<T>' is dependent, and so not known to be a class.
2866	   That's not right; we need to look in uninstantiated
2867	   templates.  A further complication arises from:
2868
2869	     template <typename T> void f(T t) {
2870	       t.T::~T();
2871	     }
2872
2873	   Here, it is not possible to look up `T' in the scope of `T'
2874	   itself.  We must look in both the current scope, and the
2875	   scope of the containing complete expression.
2876
2877	   Yet another issue is:
2878
2879             struct S {
2880               int S;
2881               ~S();
2882             };
2883
2884             S::~S() {}
2885
2886           The standard does not seem to say that the `S' in `~S'
2887	   should refer to the type `S' and not the data member
2888	   `S::S'.  */
2889
2890	/* DR 244 says that we look up the name after the "~" in the
2891	   same scope as we looked up the qualifying name.  That idea
2892	   isn't fully worked out; it's more complicated than that.  */
2893	scope = parser->scope;
2894	object_scope = parser->object_scope;
2895	qualifying_scope = parser->qualifying_scope;
2896
2897	/* If the name is of the form "X::~X" it's OK.  */
2898	if (scope && TYPE_P (scope)
2899	    && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2900	    && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2901		== CPP_OPEN_PAREN)
2902	    && (cp_lexer_peek_token (parser->lexer)->value
2903		== TYPE_IDENTIFIER (scope)))
2904	  {
2905	    cp_lexer_consume_token (parser->lexer);
2906	    return build_nt (BIT_NOT_EXPR, scope);
2907	  }
2908
2909	/* If there was an explicit qualification (S::~T), first look
2910	   in the scope given by the qualification (i.e., S).  */
2911	done = false;
2912	type_decl = NULL_TREE;
2913	if (scope)
2914	  {
2915	    cp_parser_parse_tentatively (parser);
2916	    type_decl = cp_parser_class_name (parser,
2917					      /*typename_keyword_p=*/false,
2918					      /*template_keyword_p=*/false,
2919					      /*type_p=*/false,
2920					      /*check_dependency=*/false,
2921					      /*class_head_p=*/false,
2922					      declarator_p);
2923	    if (cp_parser_parse_definitely (parser))
2924	      done = true;
2925	  }
2926	/* In "N::S::~S", look in "N" as well.  */
2927	if (!done && scope && qualifying_scope)
2928	  {
2929	    cp_parser_parse_tentatively (parser);
2930	    parser->scope = qualifying_scope;
2931	    parser->object_scope = NULL_TREE;
2932	    parser->qualifying_scope = NULL_TREE;
2933	    type_decl
2934	      = cp_parser_class_name (parser,
2935				      /*typename_keyword_p=*/false,
2936				      /*template_keyword_p=*/false,
2937				      /*type_p=*/false,
2938				      /*check_dependency=*/false,
2939				      /*class_head_p=*/false,
2940				      declarator_p);
2941	    if (cp_parser_parse_definitely (parser))
2942	      done = true;
2943	  }
2944	/* In "p->S::~T", look in the scope given by "*p" as well.  */
2945	else if (!done && object_scope)
2946	  {
2947	    cp_parser_parse_tentatively (parser);
2948	    parser->scope = object_scope;
2949	    parser->object_scope = NULL_TREE;
2950	    parser->qualifying_scope = NULL_TREE;
2951	    type_decl
2952	      = cp_parser_class_name (parser,
2953				      /*typename_keyword_p=*/false,
2954				      /*template_keyword_p=*/false,
2955				      /*type_p=*/false,
2956				      /*check_dependency=*/false,
2957				      /*class_head_p=*/false,
2958				      declarator_p);
2959	    if (cp_parser_parse_definitely (parser))
2960	      done = true;
2961	  }
2962	/* Look in the surrounding context.  */
2963	if (!done)
2964	  {
2965	    parser->scope = NULL_TREE;
2966	    parser->object_scope = NULL_TREE;
2967	    parser->qualifying_scope = NULL_TREE;
2968	    type_decl
2969	      = cp_parser_class_name (parser,
2970				      /*typename_keyword_p=*/false,
2971				      /*template_keyword_p=*/false,
2972				      /*type_p=*/false,
2973				      /*check_dependency=*/false,
2974				      /*class_head_p=*/false,
2975				      declarator_p);
2976	  }
2977	/* If an error occurred, assume that the name of the
2978	   destructor is the same as the name of the qualifying
2979	   class.  That allows us to keep parsing after running
2980	   into ill-formed destructor names.  */
2981	if (type_decl == error_mark_node && scope && TYPE_P (scope))
2982	  return build_nt (BIT_NOT_EXPR, scope);
2983	else if (type_decl == error_mark_node)
2984	  return error_mark_node;
2985
2986	/* [class.dtor]
2987
2988	   A typedef-name that names a class shall not be used as the
2989	   identifier in the declarator for a destructor declaration.  */
2990	if (declarator_p
2991	    && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
2992	    && !DECL_SELF_REFERENCE_P (type_decl))
2993	  error ("typedef-name `%D' used as destructor declarator",
2994		 type_decl);
2995
2996	return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2997      }
2998
2999    case CPP_KEYWORD:
3000      if (token->keyword == RID_OPERATOR)
3001	{
3002	  tree id;
3003
3004	  /* This could be a template-id, so we try that first.  */
3005	  cp_parser_parse_tentatively (parser);
3006	  /* Try a template-id.  */
3007	  id = cp_parser_template_id (parser, template_keyword_p,
3008				      /*check_dependency_p=*/true,
3009				      declarator_p);
3010	  /* If that worked, we're done.  */
3011	  if (cp_parser_parse_definitely (parser))
3012	    return id;
3013	  /* We still don't know whether we're looking at an
3014	     operator-function-id or a conversion-function-id.  */
3015	  cp_parser_parse_tentatively (parser);
3016	  /* Try an operator-function-id.  */
3017	  id = cp_parser_operator_function_id (parser);
3018	  /* If that didn't work, try a conversion-function-id.  */
3019	  if (!cp_parser_parse_definitely (parser))
3020	    id = cp_parser_conversion_function_id (parser);
3021
3022	  return id;
3023	}
3024      /* Fall through.  */
3025
3026    default:
3027      cp_parser_error (parser, "expected unqualified-id");
3028      return error_mark_node;
3029    }
3030}
3031
3032/* Parse an (optional) nested-name-specifier.
3033
3034   nested-name-specifier:
3035     class-or-namespace-name :: nested-name-specifier [opt]
3036     class-or-namespace-name :: template nested-name-specifier [opt]
3037
3038   PARSER->SCOPE should be set appropriately before this function is
3039   called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3040   effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3041   in name lookups.
3042
3043   Sets PARSER->SCOPE to the class (TYPE) or namespace
3044   (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3045   it unchanged if there is no nested-name-specifier.  Returns the new
3046   scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3047
3048   If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3049   part of a declaration and/or decl-specifier.  */
3050
3051static tree
3052cp_parser_nested_name_specifier_opt (cp_parser *parser,
3053				     bool typename_keyword_p,
3054				     bool check_dependency_p,
3055				     bool type_p,
3056				     bool is_declaration)
3057{
3058  bool success = false;
3059  tree access_check = NULL_TREE;
3060  ptrdiff_t start;
3061  cp_token* token;
3062
3063  /* If the next token corresponds to a nested name specifier, there
3064     is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3065     false, it may have been true before, in which case something
3066     like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3067     of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3068     CHECK_DEPENDENCY_P is false, we have to fall through into the
3069     main loop.  */
3070  if (check_dependency_p
3071      && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3072    {
3073      cp_parser_pre_parsed_nested_name_specifier (parser);
3074      return parser->scope;
3075    }
3076
3077  /* Remember where the nested-name-specifier starts.  */
3078  if (cp_parser_parsing_tentatively (parser)
3079      && !cp_parser_committed_to_tentative_parse (parser))
3080    {
3081      token = cp_lexer_peek_token (parser->lexer);
3082      start = cp_lexer_token_difference (parser->lexer,
3083					 parser->lexer->first_token,
3084					 token);
3085    }
3086  else
3087    start = -1;
3088
3089  push_deferring_access_checks (dk_deferred);
3090
3091  while (true)
3092    {
3093      tree new_scope;
3094      tree old_scope;
3095      tree saved_qualifying_scope;
3096      bool template_keyword_p;
3097
3098      /* Spot cases that cannot be the beginning of a
3099	 nested-name-specifier.  */
3100      token = cp_lexer_peek_token (parser->lexer);
3101
3102      /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3103	 the already parsed nested-name-specifier.  */
3104      if (token->type == CPP_NESTED_NAME_SPECIFIER)
3105	{
3106	  /* Grab the nested-name-specifier and continue the loop.  */
3107	  cp_parser_pre_parsed_nested_name_specifier (parser);
3108	  success = true;
3109	  continue;
3110	}
3111
3112      /* Spot cases that cannot be the beginning of a
3113	 nested-name-specifier.  On the second and subsequent times
3114	 through the loop, we look for the `template' keyword.  */
3115      if (success && token->keyword == RID_TEMPLATE)
3116	;
3117      /* A template-id can start a nested-name-specifier.  */
3118      else if (token->type == CPP_TEMPLATE_ID)
3119	;
3120      else
3121	{
3122	  /* If the next token is not an identifier, then it is
3123	     definitely not a class-or-namespace-name.  */
3124	  if (token->type != CPP_NAME)
3125	    break;
3126	  /* If the following token is neither a `<' (to begin a
3127	     template-id), nor a `::', then we are not looking at a
3128	     nested-name-specifier.  */
3129	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
3130	  if (token->type != CPP_SCOPE
3131	      && !cp_parser_nth_token_starts_template_argument_list_p
3132		  (parser, 2))
3133	    break;
3134	}
3135
3136      /* The nested-name-specifier is optional, so we parse
3137	 tentatively.  */
3138      cp_parser_parse_tentatively (parser);
3139
3140      /* Look for the optional `template' keyword, if this isn't the
3141	 first time through the loop.  */
3142      if (success)
3143	template_keyword_p = cp_parser_optional_template_keyword (parser);
3144      else
3145	template_keyword_p = false;
3146
3147      /* Save the old scope since the name lookup we are about to do
3148	 might destroy it.  */
3149      old_scope = parser->scope;
3150      saved_qualifying_scope = parser->qualifying_scope;
3151      /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3152	 look up names in "X<T>::I" in order to determine that "Y" is
3153	 a template.  So, if we have a typename at this point, we make
3154	 an effort to look through it.  */
3155      if (is_declaration
3156	  && !typename_keyword_p
3157	  && parser->scope
3158	  && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3159	parser->scope = resolve_typename_type (parser->scope,
3160					       /*only_current_p=*/false);
3161      /* Parse the qualifying entity.  */
3162      new_scope
3163	= cp_parser_class_or_namespace_name (parser,
3164					     typename_keyword_p,
3165					     template_keyword_p,
3166					     check_dependency_p,
3167					     type_p,
3168					     is_declaration);
3169      /* Look for the `::' token.  */
3170      cp_parser_require (parser, CPP_SCOPE, "`::'");
3171
3172      /* If we found what we wanted, we keep going; otherwise, we're
3173	 done.  */
3174      if (!cp_parser_parse_definitely (parser))
3175	{
3176	  bool error_p = false;
3177
3178	  /* Restore the OLD_SCOPE since it was valid before the
3179	     failed attempt at finding the last
3180	     class-or-namespace-name.  */
3181	  parser->scope = old_scope;
3182	  parser->qualifying_scope = saved_qualifying_scope;
3183	  /* If the next token is an identifier, and the one after
3184	     that is a `::', then any valid interpretation would have
3185	     found a class-or-namespace-name.  */
3186	  while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3187		 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3188		     == CPP_SCOPE)
3189		 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3190		     != CPP_COMPL))
3191	    {
3192	      token = cp_lexer_consume_token (parser->lexer);
3193	      if (!error_p)
3194		{
3195		  tree decl;
3196
3197		  decl = cp_parser_lookup_name_simple (parser, token->value);
3198		  if (TREE_CODE (decl) == TEMPLATE_DECL)
3199		    error ("`%D' used without template parameters",
3200			   decl);
3201		  else
3202		    cp_parser_name_lookup_error
3203		      (parser, token->value, decl,
3204		       "is not a class or namespace");
3205		  parser->scope = NULL_TREE;
3206		  error_p = true;
3207		  /* Treat this as a successful nested-name-specifier
3208		     due to:
3209
3210		     [basic.lookup.qual]
3211
3212		     If the name found is not a class-name (clause
3213		     _class_) or namespace-name (_namespace.def_), the
3214		     program is ill-formed.  */
3215		  success = true;
3216		}
3217	      cp_lexer_consume_token (parser->lexer);
3218	    }
3219	  break;
3220	}
3221
3222      /* We've found one valid nested-name-specifier.  */
3223      success = true;
3224      /* Make sure we look in the right scope the next time through
3225	 the loop.  */
3226      parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3227		       ? TREE_TYPE (new_scope)
3228		       : new_scope);
3229      /* If it is a class scope, try to complete it; we are about to
3230	 be looking up names inside the class.  */
3231      if (TYPE_P (parser->scope)
3232	  /* Since checking types for dependency can be expensive,
3233	     avoid doing it if the type is already complete.  */
3234	  && !COMPLETE_TYPE_P (parser->scope)
3235	  /* Do not try to complete dependent types.  */
3236	  && !dependent_type_p (parser->scope))
3237	complete_type (parser->scope);
3238    }
3239
3240  /* Retrieve any deferred checks.  Do not pop this access checks yet
3241     so the memory will not be reclaimed during token replacing below.  */
3242  access_check = get_deferred_access_checks ();
3243
3244  /* If parsing tentatively, replace the sequence of tokens that makes
3245     up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3246     token.  That way, should we re-parse the token stream, we will
3247     not have to repeat the effort required to do the parse, nor will
3248     we issue duplicate error messages.  */
3249  if (success && start >= 0)
3250    {
3251      /* Find the token that corresponds to the start of the
3252	 template-id.  */
3253      token = cp_lexer_advance_token (parser->lexer,
3254				      parser->lexer->first_token,
3255				      start);
3256
3257      /* Reset the contents of the START token.  */
3258      token->type = CPP_NESTED_NAME_SPECIFIER;
3259      token->value = build_tree_list (access_check, parser->scope);
3260      TREE_TYPE (token->value) = parser->qualifying_scope;
3261      token->keyword = RID_MAX;
3262      /* Purge all subsequent tokens.  */
3263      cp_lexer_purge_tokens_after (parser->lexer, token);
3264    }
3265
3266  pop_deferring_access_checks ();
3267  return success ? parser->scope : NULL_TREE;
3268}
3269
3270/* Parse a nested-name-specifier.  See
3271   cp_parser_nested_name_specifier_opt for details.  This function
3272   behaves identically, except that it will an issue an error if no
3273   nested-name-specifier is present, and it will return
3274   ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3275   is present.  */
3276
3277static tree
3278cp_parser_nested_name_specifier (cp_parser *parser,
3279				 bool typename_keyword_p,
3280				 bool check_dependency_p,
3281				 bool type_p,
3282				 bool is_declaration)
3283{
3284  tree scope;
3285
3286  /* Look for the nested-name-specifier.  */
3287  scope = cp_parser_nested_name_specifier_opt (parser,
3288					       typename_keyword_p,
3289					       check_dependency_p,
3290					       type_p,
3291					       is_declaration);
3292  /* If it was not present, issue an error message.  */
3293  if (!scope)
3294    {
3295      cp_parser_error (parser, "expected nested-name-specifier");
3296      parser->scope = NULL_TREE;
3297      return error_mark_node;
3298    }
3299
3300  return scope;
3301}
3302
3303/* Parse a class-or-namespace-name.
3304
3305   class-or-namespace-name:
3306     class-name
3307     namespace-name
3308
3309   TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3310   TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3311   CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3312   TYPE_P is TRUE iff the next name should be taken as a class-name,
3313   even the same name is declared to be another entity in the same
3314   scope.
3315
3316   Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3317   specified by the class-or-namespace-name.  If neither is found the
3318   ERROR_MARK_NODE is returned.  */
3319
3320static tree
3321cp_parser_class_or_namespace_name (cp_parser *parser,
3322				   bool typename_keyword_p,
3323				   bool template_keyword_p,
3324				   bool check_dependency_p,
3325				   bool type_p,
3326				   bool is_declaration)
3327{
3328  tree saved_scope;
3329  tree saved_qualifying_scope;
3330  tree saved_object_scope;
3331  tree scope;
3332  bool only_class_p;
3333
3334  /* Before we try to parse the class-name, we must save away the
3335     current PARSER->SCOPE since cp_parser_class_name will destroy
3336     it.  */
3337  saved_scope = parser->scope;
3338  saved_qualifying_scope = parser->qualifying_scope;
3339  saved_object_scope = parser->object_scope;
3340  /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3341     there is no need to look for a namespace-name.  */
3342  only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3343  if (!only_class_p)
3344    cp_parser_parse_tentatively (parser);
3345  scope = cp_parser_class_name (parser,
3346				typename_keyword_p,
3347				template_keyword_p,
3348				type_p,
3349				check_dependency_p,
3350				/*class_head_p=*/false,
3351				is_declaration);
3352  /* If that didn't work, try for a namespace-name.  */
3353  if (!only_class_p && !cp_parser_parse_definitely (parser))
3354    {
3355      /* Restore the saved scope.  */
3356      parser->scope = saved_scope;
3357      parser->qualifying_scope = saved_qualifying_scope;
3358      parser->object_scope = saved_object_scope;
3359      /* If we are not looking at an identifier followed by the scope
3360	 resolution operator, then this is not part of a
3361	 nested-name-specifier.  (Note that this function is only used
3362	 to parse the components of a nested-name-specifier.)  */
3363      if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3364	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3365	return error_mark_node;
3366      scope = cp_parser_namespace_name (parser);
3367    }
3368
3369  return scope;
3370}
3371
3372/* Parse a postfix-expression.
3373
3374   postfix-expression:
3375     primary-expression
3376     postfix-expression [ expression ]
3377     postfix-expression ( expression-list [opt] )
3378     simple-type-specifier ( expression-list [opt] )
3379     typename :: [opt] nested-name-specifier identifier
3380       ( expression-list [opt] )
3381     typename :: [opt] nested-name-specifier template [opt] template-id
3382       ( expression-list [opt] )
3383     postfix-expression . template [opt] id-expression
3384     postfix-expression -> template [opt] id-expression
3385     postfix-expression . pseudo-destructor-name
3386     postfix-expression -> pseudo-destructor-name
3387     postfix-expression ++
3388     postfix-expression --
3389     dynamic_cast < type-id > ( expression )
3390     static_cast < type-id > ( expression )
3391     reinterpret_cast < type-id > ( expression )
3392     const_cast < type-id > ( expression )
3393     typeid ( expression )
3394     typeid ( type-id )
3395
3396   GNU Extension:
3397
3398   postfix-expression:
3399     ( type-id ) { initializer-list , [opt] }
3400
3401   This extension is a GNU version of the C99 compound-literal
3402   construct.  (The C99 grammar uses `type-name' instead of `type-id',
3403   but they are essentially the same concept.)
3404
3405   If ADDRESS_P is true, the postfix expression is the operand of the
3406   `&' operator.
3407
3408   Returns a representation of the expression.  */
3409
3410static tree
3411cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3412{
3413  cp_token *token;
3414  enum rid keyword;
3415  cp_id_kind idk = CP_ID_KIND_NONE;
3416  tree postfix_expression = NULL_TREE;
3417  /* Non-NULL only if the current postfix-expression can be used to
3418     form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3419     class used to qualify the member.  */
3420  tree qualifying_class = NULL_TREE;
3421
3422  /* Peek at the next token.  */
3423  token = cp_lexer_peek_token (parser->lexer);
3424  /* Some of the productions are determined by keywords.  */
3425  keyword = token->keyword;
3426  switch (keyword)
3427    {
3428    case RID_DYNCAST:
3429    case RID_STATCAST:
3430    case RID_REINTCAST:
3431    case RID_CONSTCAST:
3432      {
3433	tree type;
3434	tree expression;
3435	const char *saved_message;
3436
3437	/* All of these can be handled in the same way from the point
3438	   of view of parsing.  Begin by consuming the token
3439	   identifying the cast.  */
3440	cp_lexer_consume_token (parser->lexer);
3441
3442	/* New types cannot be defined in the cast.  */
3443	saved_message = parser->type_definition_forbidden_message;
3444	parser->type_definition_forbidden_message
3445	  = "types may not be defined in casts";
3446
3447	/* Look for the opening `<'.  */
3448	cp_parser_require (parser, CPP_LESS, "`<'");
3449	/* Parse the type to which we are casting.  */
3450	type = cp_parser_type_id (parser);
3451	/* Look for the closing `>'.  */
3452	cp_parser_require (parser, CPP_GREATER, "`>'");
3453	/* Restore the old message.  */
3454	parser->type_definition_forbidden_message = saved_message;
3455
3456	/* And the expression which is being cast.  */
3457	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3458	expression = cp_parser_expression (parser);
3459	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3460
3461	/* Only type conversions to integral or enumeration types
3462	   can be used in constant-expressions.  */
3463	if (parser->integral_constant_expression_p
3464	    && !dependent_type_p (type)
3465	    && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3466	    /* A cast to pointer or reference type is allowed in the
3467	       implementation of "offsetof".  */
3468	    && !(parser->in_offsetof_p && POINTER_TYPE_P (type))
3469	    && (cp_parser_non_integral_constant_expression
3470		(parser,
3471		 "a cast to a type other than an integral or "
3472		 "enumeration type")))
3473	  return error_mark_node;
3474
3475	switch (keyword)
3476	  {
3477	  case RID_DYNCAST:
3478	    postfix_expression
3479	      = build_dynamic_cast (type, expression);
3480	    break;
3481	  case RID_STATCAST:
3482	    postfix_expression
3483	      = build_static_cast (type, expression);
3484	    break;
3485	  case RID_REINTCAST:
3486	    postfix_expression
3487	      = build_reinterpret_cast (type, expression);
3488	    break;
3489	  case RID_CONSTCAST:
3490	    postfix_expression
3491	      = build_const_cast (type, expression);
3492	    break;
3493	  default:
3494	    abort ();
3495	  }
3496      }
3497      break;
3498
3499    case RID_TYPEID:
3500      {
3501	tree type;
3502	const char *saved_message;
3503	bool saved_in_type_id_in_expr_p;
3504
3505	/* Consume the `typeid' token.  */
3506	cp_lexer_consume_token (parser->lexer);
3507	/* Look for the `(' token.  */
3508	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3509	/* Types cannot be defined in a `typeid' expression.  */
3510	saved_message = parser->type_definition_forbidden_message;
3511	parser->type_definition_forbidden_message
3512	  = "types may not be defined in a `typeid\' expression";
3513	/* We can't be sure yet whether we're looking at a type-id or an
3514	   expression.  */
3515	cp_parser_parse_tentatively (parser);
3516	/* Try a type-id first.  */
3517	saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3518	parser->in_type_id_in_expr_p = true;
3519	type = cp_parser_type_id (parser);
3520	parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3521	/* Look for the `)' token.  Otherwise, we can't be sure that
3522	   we're not looking at an expression: consider `typeid (int
3523	   (3))', for example.  */
3524	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3525	/* If all went well, simply lookup the type-id.  */
3526	if (cp_parser_parse_definitely (parser))
3527	  postfix_expression = get_typeid (type);
3528	/* Otherwise, fall back to the expression variant.  */
3529	else
3530	  {
3531	    tree expression;
3532
3533	    /* Look for an expression.  */
3534	    expression = cp_parser_expression (parser);
3535	    /* Compute its typeid.  */
3536	    postfix_expression = build_typeid (expression);
3537	    /* Look for the `)' token.  */
3538	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3539	  }
3540	/* `typeid' may not appear in an integral constant expression.  */
3541	if (cp_parser_non_integral_constant_expression(parser,
3542						       "`typeid' operator"))
3543	  return error_mark_node;
3544	/* Restore the saved message.  */
3545	parser->type_definition_forbidden_message = saved_message;
3546      }
3547      break;
3548
3549    case RID_TYPENAME:
3550      {
3551	bool template_p = false;
3552	tree id;
3553	tree type;
3554	tree scope;
3555
3556	/* Consume the `typename' token.  */
3557	cp_lexer_consume_token (parser->lexer);
3558	/* Look for the optional `::' operator.  */
3559	cp_parser_global_scope_opt (parser,
3560				    /*current_scope_valid_p=*/false);
3561	/* Look for the nested-name-specifier.  In case of error here,
3562	   consume the trailing id to avoid subsequent error messages
3563	   for usual cases.  */
3564	scope = cp_parser_nested_name_specifier (parser,
3565						 /*typename_keyword_p=*/true,
3566						 /*check_dependency_p=*/true,
3567						 /*type_p=*/true,
3568						 /*is_declaration=*/true);
3569
3570	/* Look for the optional `template' keyword.  */
3571	template_p = cp_parser_optional_template_keyword (parser);
3572	/* We don't know whether we're looking at a template-id or an
3573	   identifier.  */
3574	cp_parser_parse_tentatively (parser);
3575	/* Try a template-id.  */
3576	id = cp_parser_template_id (parser, template_p,
3577				    /*check_dependency_p=*/true,
3578				    /*is_declaration=*/true);
3579	/* If that didn't work, try an identifier.  */
3580	if (!cp_parser_parse_definitely (parser))
3581	  id = cp_parser_identifier (parser);
3582
3583	/* Don't process id if nested name specifier is invalid.  */
3584	if (scope == error_mark_node)
3585	  return error_mark_node;
3586	/* If we look up a template-id in a non-dependent qualifying
3587	   scope, there's no need to create a dependent type.  */
3588	else if (TREE_CODE (id) == TYPE_DECL
3589	    && !dependent_type_p (parser->scope))
3590	  type = TREE_TYPE (id);
3591	/* Create a TYPENAME_TYPE to represent the type to which the
3592	   functional cast is being performed.  */
3593	else
3594	  type = make_typename_type (parser->scope, id,
3595				     /*complain=*/1);
3596
3597	postfix_expression = cp_parser_functional_cast (parser, type);
3598      }
3599      break;
3600
3601    default:
3602      {
3603	tree type;
3604
3605	/* If the next thing is a simple-type-specifier, we may be
3606	   looking at a functional cast.  We could also be looking at
3607	   an id-expression.  So, we try the functional cast, and if
3608	   that doesn't work we fall back to the primary-expression.  */
3609	cp_parser_parse_tentatively (parser);
3610	/* Look for the simple-type-specifier.  */
3611	type = cp_parser_simple_type_specifier (parser,
3612						CP_PARSER_FLAGS_NONE,
3613						/*identifier_p=*/false);
3614	/* Parse the cast itself.  */
3615	if (!cp_parser_error_occurred (parser))
3616	  postfix_expression
3617	    = cp_parser_functional_cast (parser, type);
3618	/* If that worked, we're done.  */
3619	if (cp_parser_parse_definitely (parser))
3620	  break;
3621
3622	/* If the functional-cast didn't work out, try a
3623	   compound-literal.  */
3624	if (cp_parser_allow_gnu_extensions_p (parser)
3625	    && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3626	  {
3627	    tree initializer_list = NULL_TREE;
3628	    bool saved_in_type_id_in_expr_p;
3629
3630	    cp_parser_parse_tentatively (parser);
3631	    /* Consume the `('.  */
3632	    cp_lexer_consume_token (parser->lexer);
3633	    /* Parse the type.  */
3634	    saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3635	    parser->in_type_id_in_expr_p = true;
3636	    type = cp_parser_type_id (parser);
3637	    parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3638	    /* Look for the `)'.  */
3639	    cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3640	    /* Look for the `{'.  */
3641	    cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3642	    /* If things aren't going well, there's no need to
3643	       keep going.  */
3644	    if (!cp_parser_error_occurred (parser))
3645	      {
3646		bool non_constant_p;
3647		/* Parse the initializer-list.  */
3648		initializer_list
3649		  = cp_parser_initializer_list (parser, &non_constant_p);
3650		/* Allow a trailing `,'.  */
3651		if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3652		  cp_lexer_consume_token (parser->lexer);
3653		/* Look for the final `}'.  */
3654		cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3655	      }
3656	    /* If that worked, we're definitely looking at a
3657	       compound-literal expression.  */
3658	    if (cp_parser_parse_definitely (parser))
3659	      {
3660		/* Warn the user that a compound literal is not
3661		   allowed in standard C++.  */
3662		if (pedantic)
3663		  pedwarn ("ISO C++ forbids compound-literals");
3664		/* Form the representation of the compound-literal.  */
3665		postfix_expression
3666		  = finish_compound_literal (type, initializer_list);
3667		break;
3668	      }
3669	  }
3670
3671	/* It must be a primary-expression.  */
3672	postfix_expression = cp_parser_primary_expression (parser,
3673							   &idk,
3674							   &qualifying_class);
3675      }
3676      break;
3677    }
3678
3679  /* If we were avoiding committing to the processing of a
3680     qualified-id until we knew whether or not we had a
3681     pointer-to-member, we now know.  */
3682  if (qualifying_class)
3683    {
3684      bool done;
3685
3686      /* Peek at the next token.  */
3687      token = cp_lexer_peek_token (parser->lexer);
3688      done = (token->type != CPP_OPEN_SQUARE
3689	      && token->type != CPP_OPEN_PAREN
3690	      && token->type != CPP_DOT
3691	      && token->type != CPP_DEREF
3692	      && token->type != CPP_PLUS_PLUS
3693	      && token->type != CPP_MINUS_MINUS);
3694
3695      postfix_expression = finish_qualified_id_expr (qualifying_class,
3696						     postfix_expression,
3697						     done,
3698						     address_p);
3699      if (done)
3700	return postfix_expression;
3701    }
3702
3703  /* Keep looping until the postfix-expression is complete.  */
3704  while (true)
3705    {
3706      if (idk == CP_ID_KIND_UNQUALIFIED
3707	  && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3708	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3709	/* It is not a Koenig lookup function call.  */
3710	postfix_expression
3711	  = unqualified_name_lookup_error (postfix_expression);
3712
3713      /* Peek at the next token.  */
3714      token = cp_lexer_peek_token (parser->lexer);
3715
3716      switch (token->type)
3717	{
3718	case CPP_OPEN_SQUARE:
3719	  /* postfix-expression [ expression ] */
3720	  {
3721	    tree index;
3722
3723	    /* Consume the `[' token.  */
3724	    cp_lexer_consume_token (parser->lexer);
3725	    /* Parse the index expression.  */
3726	    index = cp_parser_expression (parser);
3727	    /* Look for the closing `]'.  */
3728	    cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3729
3730	    /* Build the ARRAY_REF.  */
3731	    postfix_expression
3732	      = grok_array_decl (postfix_expression, index);
3733	    idk = CP_ID_KIND_NONE;
3734	    /* Array references are not permitted in
3735	       constant-expressions (but they are allowed
3736	       in offsetof).  */
3737	    if (!parser->in_offsetof_p
3738		&& cp_parser_non_integral_constant_expression
3739		    (parser, "an array reference"))
3740	      postfix_expression = error_mark_node;
3741	  }
3742	  break;
3743
3744	case CPP_OPEN_PAREN:
3745	  /* postfix-expression ( expression-list [opt] ) */
3746	  {
3747	    bool koenig_p;
3748	    tree args = (cp_parser_parenthesized_expression_list
3749			 (parser, false, /*non_constant_p=*/NULL));
3750
3751	    if (args == error_mark_node)
3752	      {
3753		postfix_expression = error_mark_node;
3754		break;
3755	      }
3756
3757	    /* Function calls are not permitted in
3758	       constant-expressions.  */
3759	    if (cp_parser_non_integral_constant_expression (parser,
3760							    "a function call"))
3761	      {
3762		postfix_expression = error_mark_node;
3763		break;
3764	      }
3765
3766	    koenig_p = false;
3767	    if (idk == CP_ID_KIND_UNQUALIFIED)
3768	      {
3769		if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3770		  {
3771		    if (args)
3772		      {
3773			koenig_p = true;
3774			postfix_expression
3775			  = perform_koenig_lookup (postfix_expression, args);
3776		      }
3777		    else
3778		      postfix_expression
3779			= unqualified_fn_lookup_error (postfix_expression);
3780 		  }
3781		/* We do not perform argument-dependent lookup if
3782		   normal lookup finds a non-function, in accordance
3783		   with the expected resolution of DR 218.  */
3784		else if (args && is_overloaded_fn (postfix_expression))
3785		  {
3786		    tree fn = get_first_fn (postfix_expression);
3787		    if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3788		      fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
3789		    /* Only do argument dependent lookup if regular
3790		       lookup does not find a set of member functions.
3791		       [basic.lookup.koenig]/2a  */
3792 		    if (!DECL_FUNCTION_MEMBER_P (fn))
3793 		      {
3794 			koenig_p = true;
3795 			postfix_expression
3796 			  = perform_koenig_lookup (postfix_expression, args);
3797 		      }
3798		  }
3799	      }
3800
3801	    if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3802	      {
3803		tree instance = TREE_OPERAND (postfix_expression, 0);
3804		tree fn = TREE_OPERAND (postfix_expression, 1);
3805
3806		if (processing_template_decl
3807		    && (type_dependent_expression_p (instance)
3808			|| (!BASELINK_P (fn)
3809			    && TREE_CODE (fn) != FIELD_DECL)
3810			|| type_dependent_expression_p (fn)
3811			|| any_type_dependent_arguments_p (args)))
3812		  {
3813		    postfix_expression
3814		      = build_min_nt (CALL_EXPR, postfix_expression, args);
3815		    break;
3816		  }
3817
3818		if (BASELINK_P (fn))
3819		  postfix_expression
3820		    = (build_new_method_call
3821		       (instance, fn, args, NULL_TREE,
3822			(idk == CP_ID_KIND_QUALIFIED
3823			 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3824		else
3825		  postfix_expression
3826		    = finish_call_expr (postfix_expression, args,
3827					/*disallow_virtual=*/false,
3828					/*koenig_p=*/false);
3829	      }
3830	    else if (TREE_CODE (postfix_expression) == OFFSET_REF
3831		     || TREE_CODE (postfix_expression) == MEMBER_REF
3832		     || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3833	      postfix_expression = (build_offset_ref_call_from_tree
3834				    (postfix_expression, args));
3835	    else if (idk == CP_ID_KIND_QUALIFIED)
3836	      /* A call to a static class member, or a namespace-scope
3837		 function.  */
3838	      postfix_expression
3839		= finish_call_expr (postfix_expression, args,
3840				    /*disallow_virtual=*/true,
3841				    koenig_p);
3842	    else
3843	      /* All other function calls.  */
3844	      postfix_expression
3845		= finish_call_expr (postfix_expression, args,
3846				    /*disallow_virtual=*/false,
3847				    koenig_p);
3848
3849	    /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
3850	    idk = CP_ID_KIND_NONE;
3851	  }
3852	  break;
3853
3854	case CPP_DOT:
3855	case CPP_DEREF:
3856	  /* postfix-expression . template [opt] id-expression
3857	     postfix-expression . pseudo-destructor-name
3858	     postfix-expression -> template [opt] id-expression
3859	     postfix-expression -> pseudo-destructor-name */
3860	  {
3861	    tree name;
3862	    bool dependent_p;
3863	    bool template_p;
3864	    bool pseudo_destructor_p;
3865	    tree scope = NULL_TREE;
3866	    enum cpp_ttype token_type = token->type;
3867
3868	    /* If this is a `->' operator, dereference the pointer.  */
3869	    if (token->type == CPP_DEREF)
3870	      postfix_expression = build_x_arrow (postfix_expression);
3871	    /* Check to see whether or not the expression is
3872	       type-dependent.  */
3873	    dependent_p = type_dependent_expression_p (postfix_expression);
3874	    /* The identifier following the `->' or `.' is not
3875	       qualified.  */
3876	    parser->scope = NULL_TREE;
3877	    parser->qualifying_scope = NULL_TREE;
3878	    parser->object_scope = NULL_TREE;
3879	    idk = CP_ID_KIND_NONE;
3880	    /* Enter the scope corresponding to the type of the object
3881	       given by the POSTFIX_EXPRESSION.  */
3882	    if (!dependent_p
3883		&& TREE_TYPE (postfix_expression) != NULL_TREE)
3884	      {
3885		scope = TREE_TYPE (postfix_expression);
3886		/* According to the standard, no expression should
3887		   ever have reference type.  Unfortunately, we do not
3888		   currently match the standard in this respect in
3889		   that our internal representation of an expression
3890		   may have reference type even when the standard says
3891		   it does not.  Therefore, we have to manually obtain
3892		   the underlying type here.  */
3893		scope = non_reference (scope);
3894		/* The type of the POSTFIX_EXPRESSION must be
3895		   complete.  */
3896		scope = complete_type_or_else (scope, NULL_TREE);
3897		/* Let the name lookup machinery know that we are
3898		   processing a class member access expression.  */
3899		parser->context->object_type = scope;
3900		/* If something went wrong, we want to be able to
3901		   discern that case, as opposed to the case where
3902		   there was no SCOPE due to the type of expression
3903		   being dependent.  */
3904		if (!scope)
3905		  scope = error_mark_node;
3906		/* If the SCOPE was erroneous, make the various
3907		   semantic analysis functions exit quickly -- and
3908		   without issuing additional error messages.  */
3909		if (scope == error_mark_node)
3910		  postfix_expression = error_mark_node;
3911	      }
3912
3913	    /* Consume the `.' or `->' operator.  */
3914	    cp_lexer_consume_token (parser->lexer);
3915
3916	    /* Assume this expression is not a pseudo-destructor access.  */
3917	    pseudo_destructor_p = false;
3918
3919	    /* If the SCOPE is a scalar type, then, if this is a valid program,
3920	       we must be looking at a pseudo-destructor-name.  */
3921	    if (scope && SCALAR_TYPE_P (scope))
3922	      {
3923		tree s = NULL_TREE;
3924		tree type;
3925
3926		cp_parser_parse_tentatively (parser);
3927		/* Parse the pseudo-destructor-name.  */
3928		cp_parser_pseudo_destructor_name (parser, &s, &type);
3929		if (cp_parser_parse_definitely (parser))
3930		  {
3931		    pseudo_destructor_p = true;
3932		    postfix_expression
3933		      = finish_pseudo_destructor_expr (postfix_expression,
3934						       s, TREE_TYPE (type));
3935		  }
3936	      }
3937
3938	    if (!pseudo_destructor_p)
3939	      {
3940		/* If the SCOPE is not a scalar type, we are looking
3941		   at an ordinary class member access expression,
3942		   rather than a pseudo-destructor-name.  */
3943		template_p = cp_parser_optional_template_keyword (parser);
3944		/* Parse the id-expression.  */
3945		name = cp_parser_id_expression (parser,
3946						template_p,
3947						/*check_dependency_p=*/true,
3948						/*template_p=*/NULL,
3949						/*declarator_p=*/false);
3950		/* In general, build a SCOPE_REF if the member name is
3951		   qualified.  However, if the name was not dependent
3952		   and has already been resolved; there is no need to
3953		   build the SCOPE_REF.  For example;
3954
3955                     struct X { void f(); };
3956                     template <typename T> void f(T* t) { t->X::f(); }
3957
3958                   Even though "t" is dependent, "X::f" is not and has
3959		   been resolved to a BASELINK; there is no need to
3960		   include scope information.  */
3961
3962		/* But we do need to remember that there was an explicit
3963		   scope for virtual function calls.  */
3964		if (parser->scope)
3965		  idk = CP_ID_KIND_QUALIFIED;
3966
3967		if (name != error_mark_node
3968		    && !BASELINK_P (name)
3969		    && parser->scope)
3970		  {
3971		    name = build_nt (SCOPE_REF, parser->scope, name);
3972		    parser->scope = NULL_TREE;
3973		    parser->qualifying_scope = NULL_TREE;
3974		    parser->object_scope = NULL_TREE;
3975		  }
3976		if (scope && name && BASELINK_P (name))
3977		  adjust_result_of_qualified_name_lookup
3978		    (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
3979		postfix_expression
3980		  = finish_class_member_access_expr (postfix_expression, name);
3981	      }
3982
3983	    /* We no longer need to look up names in the scope of the
3984	       object on the left-hand side of the `.' or `->'
3985	       operator.  */
3986	    parser->context->object_type = NULL_TREE;
3987	    /* These operators may not appear in constant-expressions.  */
3988	    if (/* The "->" operator is allowed in the implementation
3989		   of "offsetof".  The "." operator may appear in the
3990		   name of the member.  */
3991		!parser->in_offsetof_p
3992		&& (cp_parser_non_integral_constant_expression
3993		    (parser,
3994		     token_type == CPP_DEREF ? "'->'" : "`.'")))
3995	      postfix_expression = error_mark_node;
3996	  }
3997	  break;
3998
3999	case CPP_PLUS_PLUS:
4000	  /* postfix-expression ++  */
4001	  /* Consume the `++' token.  */
4002	  cp_lexer_consume_token (parser->lexer);
4003	  /* Generate a representation for the complete expression.  */
4004	  postfix_expression
4005	    = finish_increment_expr (postfix_expression,
4006				     POSTINCREMENT_EXPR);
4007	  /* Increments may not appear in constant-expressions.  */
4008	  if (cp_parser_non_integral_constant_expression (parser,
4009							  "an increment"))
4010	    postfix_expression = error_mark_node;
4011	  idk = CP_ID_KIND_NONE;
4012	  break;
4013
4014	case CPP_MINUS_MINUS:
4015	  /* postfix-expression -- */
4016	  /* Consume the `--' token.  */
4017	  cp_lexer_consume_token (parser->lexer);
4018	  /* Generate a representation for the complete expression.  */
4019	  postfix_expression
4020	    = finish_increment_expr (postfix_expression,
4021				     POSTDECREMENT_EXPR);
4022	  /* Decrements may not appear in constant-expressions.  */
4023	  if (cp_parser_non_integral_constant_expression (parser,
4024							  "a decrement"))
4025	    postfix_expression = error_mark_node;
4026	  idk = CP_ID_KIND_NONE;
4027	  break;
4028
4029	default:
4030	  return postfix_expression;
4031	}
4032    }
4033
4034  /* We should never get here.  */
4035  abort ();
4036  return error_mark_node;
4037}
4038
4039/* Parse a parenthesized expression-list.
4040
4041   expression-list:
4042     assignment-expression
4043     expression-list, assignment-expression
4044
4045   attribute-list:
4046     expression-list
4047     identifier
4048     identifier, expression-list
4049
4050   Returns a TREE_LIST.  The TREE_VALUE of each node is a
4051   representation of an assignment-expression.  Note that a TREE_LIST
4052   is returned even if there is only a single expression in the list.
4053   error_mark_node is returned if the ( and or ) are
4054   missing. NULL_TREE is returned on no expressions. The parentheses
4055   are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4056   list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4057   indicates whether or not all of the expressions in the list were
4058   constant.  */
4059
4060static tree
4061cp_parser_parenthesized_expression_list (cp_parser* parser,
4062					 bool is_attribute_list,
4063					 bool *non_constant_p)
4064{
4065  tree expression_list = NULL_TREE;
4066  tree identifier = NULL_TREE;
4067
4068  /* Assume all the expressions will be constant.  */
4069  if (non_constant_p)
4070    *non_constant_p = false;
4071
4072  if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4073    return error_mark_node;
4074
4075  /* Consume expressions until there are no more.  */
4076  if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4077    while (true)
4078      {
4079	tree expr;
4080
4081	/* At the beginning of attribute lists, check to see if the
4082	   next token is an identifier.  */
4083	if (is_attribute_list
4084	    && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4085	  {
4086	    cp_token *token;
4087
4088	    /* Consume the identifier.  */
4089	    token = cp_lexer_consume_token (parser->lexer);
4090	    /* Save the identifier.  */
4091	    identifier = token->value;
4092	  }
4093	else
4094	  {
4095	    /* Parse the next assignment-expression.  */
4096	    if (non_constant_p)
4097	      {
4098		bool expr_non_constant_p;
4099		expr = (cp_parser_constant_expression
4100			(parser, /*allow_non_constant_p=*/true,
4101			 &expr_non_constant_p));
4102		if (expr_non_constant_p)
4103		  *non_constant_p = true;
4104	      }
4105	    else
4106	      expr = cp_parser_assignment_expression (parser);
4107
4108	     /* Add it to the list.  We add error_mark_node
4109		expressions to the list, so that we can still tell if
4110		the correct form for a parenthesized expression-list
4111		is found. That gives better errors.  */
4112	    expression_list = tree_cons (NULL_TREE, expr, expression_list);
4113
4114	    if (expr == error_mark_node)
4115	      goto skip_comma;
4116	  }
4117
4118	/* After the first item, attribute lists look the same as
4119	   expression lists.  */
4120	is_attribute_list = false;
4121
4122      get_comma:;
4123	/* If the next token isn't a `,', then we are done.  */
4124	if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4125	  break;
4126
4127	/* Otherwise, consume the `,' and keep going.  */
4128	cp_lexer_consume_token (parser->lexer);
4129      }
4130
4131  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4132    {
4133      int ending;
4134
4135    skip_comma:;
4136      /* We try and resync to an unnested comma, as that will give the
4137	 user better diagnostics.  */
4138      ending = cp_parser_skip_to_closing_parenthesis (parser,
4139						      /*recovering=*/true,
4140						      /*or_comma=*/true,
4141						      /*consume_paren=*/true);
4142      if (ending < 0)
4143	goto get_comma;
4144      if (!ending)
4145	return error_mark_node;
4146    }
4147
4148  /* We built up the list in reverse order so we must reverse it now.  */
4149  expression_list = nreverse (expression_list);
4150  if (identifier)
4151    expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4152
4153  return expression_list;
4154}
4155
4156/* Parse a pseudo-destructor-name.
4157
4158   pseudo-destructor-name:
4159     :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4160     :: [opt] nested-name-specifier template template-id :: ~ type-name
4161     :: [opt] nested-name-specifier [opt] ~ type-name
4162
4163   If either of the first two productions is used, sets *SCOPE to the
4164   TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4165   NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4166   or ERROR_MARK_NODE if the parse fails.  */
4167
4168static void
4169cp_parser_pseudo_destructor_name (cp_parser* parser,
4170                                  tree* scope,
4171                                  tree* type)
4172{
4173  bool nested_name_specifier_p;
4174
4175  /* Look for the optional `::' operator.  */
4176  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4177  /* Look for the optional nested-name-specifier.  */
4178  nested_name_specifier_p
4179    = (cp_parser_nested_name_specifier_opt (parser,
4180					    /*typename_keyword_p=*/false,
4181					    /*check_dependency_p=*/true,
4182					    /*type_p=*/false,
4183					    /*is_declaration=*/true)
4184       != NULL_TREE);
4185  /* Now, if we saw a nested-name-specifier, we might be doing the
4186     second production.  */
4187  if (nested_name_specifier_p
4188      && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4189    {
4190      /* Consume the `template' keyword.  */
4191      cp_lexer_consume_token (parser->lexer);
4192      /* Parse the template-id.  */
4193      cp_parser_template_id (parser,
4194			     /*template_keyword_p=*/true,
4195			     /*check_dependency_p=*/false,
4196			     /*is_declaration=*/true);
4197      /* Look for the `::' token.  */
4198      cp_parser_require (parser, CPP_SCOPE, "`::'");
4199    }
4200  /* If the next token is not a `~', then there might be some
4201     additional qualification.  */
4202  else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4203    {
4204      /* Look for the type-name.  */
4205      *scope = TREE_TYPE (cp_parser_type_name (parser));
4206
4207      /* If we didn't get an aggregate type, or we don't have ::~,
4208	 then something has gone wrong.  Since the only caller of this
4209	 function is looking for something after `.' or `->' after a
4210	 scalar type, most likely the program is trying to get a
4211	 member of a non-aggregate type.  */
4212      if (*scope == error_mark_node
4213	  || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4214	  || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4215	{
4216	  cp_parser_error (parser, "request for member of non-aggregate type");
4217	  *type = error_mark_node;
4218	  return;
4219	}
4220
4221      /* Look for the `::' token.  */
4222      cp_parser_require (parser, CPP_SCOPE, "`::'");
4223    }
4224  else
4225    *scope = NULL_TREE;
4226
4227  /* Look for the `~'.  */
4228  cp_parser_require (parser, CPP_COMPL, "`~'");
4229  /* Look for the type-name again.  We are not responsible for
4230     checking that it matches the first type-name.  */
4231  *type = cp_parser_type_name (parser);
4232}
4233
4234/* Parse a unary-expression.
4235
4236   unary-expression:
4237     postfix-expression
4238     ++ cast-expression
4239     -- cast-expression
4240     unary-operator cast-expression
4241     sizeof unary-expression
4242     sizeof ( type-id )
4243     new-expression
4244     delete-expression
4245
4246   GNU Extensions:
4247
4248   unary-expression:
4249     __extension__ cast-expression
4250     __alignof__ unary-expression
4251     __alignof__ ( type-id )
4252     __real__ cast-expression
4253     __imag__ cast-expression
4254     && identifier
4255
4256   ADDRESS_P is true iff the unary-expression is appearing as the
4257   operand of the `&' operator.
4258
4259   Returns a representation of the expression.  */
4260
4261static tree
4262cp_parser_unary_expression (cp_parser *parser, bool address_p)
4263{
4264  cp_token *token;
4265  enum tree_code unary_operator;
4266
4267  /* Peek at the next token.  */
4268  token = cp_lexer_peek_token (parser->lexer);
4269  /* Some keywords give away the kind of expression.  */
4270  if (token->type == CPP_KEYWORD)
4271    {
4272      enum rid keyword = token->keyword;
4273
4274      switch (keyword)
4275	{
4276	case RID_ALIGNOF:
4277	case RID_SIZEOF:
4278	  {
4279	    tree operand;
4280	    enum tree_code op;
4281
4282	    op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4283	    /* Consume the token.  */
4284	    cp_lexer_consume_token (parser->lexer);
4285	    /* Parse the operand.  */
4286	    operand = cp_parser_sizeof_operand (parser, keyword);
4287
4288	    if (TYPE_P (operand))
4289	      return cxx_sizeof_or_alignof_type (operand, op, true);
4290	    else
4291	      return cxx_sizeof_or_alignof_expr (operand, op);
4292	  }
4293
4294	case RID_NEW:
4295	  return cp_parser_new_expression (parser);
4296
4297	case RID_DELETE:
4298	  return cp_parser_delete_expression (parser);
4299
4300	case RID_EXTENSION:
4301	  {
4302	    /* The saved value of the PEDANTIC flag.  */
4303	    int saved_pedantic;
4304	    tree expr;
4305
4306	    /* Save away the PEDANTIC flag.  */
4307	    cp_parser_extension_opt (parser, &saved_pedantic);
4308	    /* Parse the cast-expression.  */
4309	    expr = cp_parser_simple_cast_expression (parser);
4310	    /* Restore the PEDANTIC flag.  */
4311	    pedantic = saved_pedantic;
4312
4313	    return expr;
4314	  }
4315
4316	case RID_REALPART:
4317	case RID_IMAGPART:
4318	  {
4319	    tree expression;
4320
4321	    /* Consume the `__real__' or `__imag__' token.  */
4322	    cp_lexer_consume_token (parser->lexer);
4323	    /* Parse the cast-expression.  */
4324	    expression = cp_parser_simple_cast_expression (parser);
4325	    /* Create the complete representation.  */
4326	    return build_x_unary_op ((keyword == RID_REALPART
4327				      ? REALPART_EXPR : IMAGPART_EXPR),
4328				     expression);
4329	  }
4330	  break;
4331
4332	default:
4333	  break;
4334	}
4335    }
4336
4337  /* Look for the `:: new' and `:: delete', which also signal the
4338     beginning of a new-expression, or delete-expression,
4339     respectively.  If the next token is `::', then it might be one of
4340     these.  */
4341  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4342    {
4343      enum rid keyword;
4344
4345      /* See if the token after the `::' is one of the keywords in
4346	 which we're interested.  */
4347      keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4348      /* If it's `new', we have a new-expression.  */
4349      if (keyword == RID_NEW)
4350	return cp_parser_new_expression (parser);
4351      /* Similarly, for `delete'.  */
4352      else if (keyword == RID_DELETE)
4353	return cp_parser_delete_expression (parser);
4354    }
4355
4356  /* Look for a unary operator.  */
4357  unary_operator = cp_parser_unary_operator (token);
4358  /* The `++' and `--' operators can be handled similarly, even though
4359     they are not technically unary-operators in the grammar.  */
4360  if (unary_operator == ERROR_MARK)
4361    {
4362      if (token->type == CPP_PLUS_PLUS)
4363	unary_operator = PREINCREMENT_EXPR;
4364      else if (token->type == CPP_MINUS_MINUS)
4365	unary_operator = PREDECREMENT_EXPR;
4366      /* Handle the GNU address-of-label extension.  */
4367      else if (cp_parser_allow_gnu_extensions_p (parser)
4368	       && token->type == CPP_AND_AND)
4369	{
4370	  tree identifier;
4371
4372	  /* Consume the '&&' token.  */
4373	  cp_lexer_consume_token (parser->lexer);
4374	  /* Look for the identifier.  */
4375	  identifier = cp_parser_identifier (parser);
4376	  /* Create an expression representing the address.  */
4377	  return finish_label_address_expr (identifier);
4378	}
4379    }
4380  if (unary_operator != ERROR_MARK)
4381    {
4382      tree cast_expression;
4383      tree expression = error_mark_node;
4384      const char *non_constant_p = NULL;
4385
4386      /* Consume the operator token.  */
4387      token = cp_lexer_consume_token (parser->lexer);
4388      /* Parse the cast-expression.  */
4389      cast_expression
4390	= cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4391      /* Now, build an appropriate representation.  */
4392      switch (unary_operator)
4393	{
4394	case INDIRECT_REF:
4395	  non_constant_p = "`*'";
4396	  expression = build_x_indirect_ref (cast_expression, "unary *");
4397	  break;
4398
4399	case ADDR_EXPR:
4400	  /* The "&" operator is allowed in the implementation of
4401	     "offsetof".  */
4402	  if (!parser->in_offsetof_p)
4403	    non_constant_p = "`&'";
4404	  /* Fall through.  */
4405	case BIT_NOT_EXPR:
4406	  expression = build_x_unary_op (unary_operator, cast_expression);
4407	  break;
4408
4409	case PREINCREMENT_EXPR:
4410	case PREDECREMENT_EXPR:
4411	  non_constant_p = (unary_operator == PREINCREMENT_EXPR
4412			    ? "`++'" : "`--'");
4413	  /* Fall through.  */
4414	case CONVERT_EXPR:
4415	case NEGATE_EXPR:
4416	case TRUTH_NOT_EXPR:
4417	  expression = finish_unary_op_expr (unary_operator, cast_expression);
4418	  break;
4419
4420	default:
4421	  abort ();
4422	}
4423
4424      if (non_constant_p
4425	  && cp_parser_non_integral_constant_expression (parser,
4426							 non_constant_p))
4427	expression = error_mark_node;
4428
4429      return expression;
4430    }
4431
4432  return cp_parser_postfix_expression (parser, address_p);
4433}
4434
4435/* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4436   unary-operator, the corresponding tree code is returned.  */
4437
4438static enum tree_code
4439cp_parser_unary_operator (cp_token* token)
4440{
4441  switch (token->type)
4442    {
4443    case CPP_MULT:
4444      return INDIRECT_REF;
4445
4446    case CPP_AND:
4447      return ADDR_EXPR;
4448
4449    case CPP_PLUS:
4450      return CONVERT_EXPR;
4451
4452    case CPP_MINUS:
4453      return NEGATE_EXPR;
4454
4455    case CPP_NOT:
4456      return TRUTH_NOT_EXPR;
4457
4458    case CPP_COMPL:
4459      return BIT_NOT_EXPR;
4460
4461    default:
4462      return ERROR_MARK;
4463    }
4464}
4465
4466/* Parse a new-expression.
4467
4468   new-expression:
4469     :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4470     :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4471
4472   Returns a representation of the expression.  */
4473
4474static tree
4475cp_parser_new_expression (cp_parser* parser)
4476{
4477  bool global_scope_p;
4478  tree placement;
4479  tree type;
4480  tree initializer;
4481
4482  /* Look for the optional `::' operator.  */
4483  global_scope_p
4484    = (cp_parser_global_scope_opt (parser,
4485				   /*current_scope_valid_p=*/false)
4486       != NULL_TREE);
4487  /* Look for the `new' operator.  */
4488  cp_parser_require_keyword (parser, RID_NEW, "`new'");
4489  /* There's no easy way to tell a new-placement from the
4490     `( type-id )' construct.  */
4491  cp_parser_parse_tentatively (parser);
4492  /* Look for a new-placement.  */
4493  placement = cp_parser_new_placement (parser);
4494  /* If that didn't work out, there's no new-placement.  */
4495  if (!cp_parser_parse_definitely (parser))
4496    placement = NULL_TREE;
4497
4498  /* If the next token is a `(', then we have a parenthesized
4499     type-id.  */
4500  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4501    {
4502      /* Consume the `('.  */
4503      cp_lexer_consume_token (parser->lexer);
4504      /* Parse the type-id.  */
4505      type = cp_parser_type_id (parser);
4506      /* Look for the closing `)'.  */
4507      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4508      /* There should not be a direct-new-declarator in this production,
4509         but GCC used to allowed this, so we check and emit a sensible error
4510	 message for this case.  */
4511      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4512	{
4513	  error ("array bound forbidden after parenthesized type-id");
4514	  inform ("try removing the parentheses around the type-id");
4515	  cp_parser_direct_new_declarator (parser);
4516	}
4517    }
4518  /* Otherwise, there must be a new-type-id.  */
4519  else
4520    type = cp_parser_new_type_id (parser);
4521
4522  /* If the next token is a `(', then we have a new-initializer.  */
4523  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4524    initializer = cp_parser_new_initializer (parser);
4525  else
4526    initializer = NULL_TREE;
4527
4528  /* A new-expression may not appear in an integral constant
4529     expression.  */
4530  if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4531    return error_mark_node;
4532
4533  /* Create a representation of the new-expression.  */
4534  return build_new (placement, type, initializer, global_scope_p);
4535}
4536
4537/* Parse a new-placement.
4538
4539   new-placement:
4540     ( expression-list )
4541
4542   Returns the same representation as for an expression-list.  */
4543
4544static tree
4545cp_parser_new_placement (cp_parser* parser)
4546{
4547  tree expression_list;
4548
4549  /* Parse the expression-list.  */
4550  expression_list = (cp_parser_parenthesized_expression_list
4551		     (parser, false, /*non_constant_p=*/NULL));
4552
4553  return expression_list;
4554}
4555
4556/* Parse a new-type-id.
4557
4558   new-type-id:
4559     type-specifier-seq new-declarator [opt]
4560
4561   Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4562   and whose TREE_VALUE is the new-declarator.  */
4563
4564static tree
4565cp_parser_new_type_id (cp_parser* parser)
4566{
4567  tree type_specifier_seq;
4568  tree declarator;
4569  const char *saved_message;
4570
4571  /* The type-specifier sequence must not contain type definitions.
4572     (It cannot contain declarations of new types either, but if they
4573     are not definitions we will catch that because they are not
4574     complete.)  */
4575  saved_message = parser->type_definition_forbidden_message;
4576  parser->type_definition_forbidden_message
4577    = "types may not be defined in a new-type-id";
4578  /* Parse the type-specifier-seq.  */
4579  type_specifier_seq = cp_parser_type_specifier_seq (parser);
4580  /* Restore the old message.  */
4581  parser->type_definition_forbidden_message = saved_message;
4582  /* Parse the new-declarator.  */
4583  declarator = cp_parser_new_declarator_opt (parser);
4584
4585  return build_tree_list (type_specifier_seq, declarator);
4586}
4587
4588/* Parse an (optional) new-declarator.
4589
4590   new-declarator:
4591     ptr-operator new-declarator [opt]
4592     direct-new-declarator
4593
4594   Returns a representation of the declarator.  See
4595   cp_parser_declarator for the representations used.  */
4596
4597static tree
4598cp_parser_new_declarator_opt (cp_parser* parser)
4599{
4600  enum tree_code code;
4601  tree type;
4602  tree cv_qualifier_seq;
4603
4604  /* We don't know if there's a ptr-operator next, or not.  */
4605  cp_parser_parse_tentatively (parser);
4606  /* Look for a ptr-operator.  */
4607  code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4608  /* If that worked, look for more new-declarators.  */
4609  if (cp_parser_parse_definitely (parser))
4610    {
4611      tree declarator;
4612
4613      /* Parse another optional declarator.  */
4614      declarator = cp_parser_new_declarator_opt (parser);
4615
4616      /* Create the representation of the declarator.  */
4617      if (code == INDIRECT_REF)
4618	declarator = make_pointer_declarator (cv_qualifier_seq,
4619					      declarator);
4620      else
4621	declarator = make_reference_declarator (cv_qualifier_seq,
4622						declarator);
4623
4624     /* Handle the pointer-to-member case.  */
4625     if (type)
4626       declarator = build_nt (SCOPE_REF, type, declarator);
4627
4628      return declarator;
4629    }
4630
4631  /* If the next token is a `[', there is a direct-new-declarator.  */
4632  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4633    return cp_parser_direct_new_declarator (parser);
4634
4635  return NULL_TREE;
4636}
4637
4638/* Parse a direct-new-declarator.
4639
4640   direct-new-declarator:
4641     [ expression ]
4642     direct-new-declarator [constant-expression]
4643
4644   Returns an ARRAY_REF, following the same conventions as are
4645   documented for cp_parser_direct_declarator.  */
4646
4647static tree
4648cp_parser_direct_new_declarator (cp_parser* parser)
4649{
4650  tree declarator = NULL_TREE;
4651
4652  while (true)
4653    {
4654      tree expression;
4655
4656      /* Look for the opening `['.  */
4657      cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4658      /* The first expression is not required to be constant.  */
4659      if (!declarator)
4660	{
4661	  expression = cp_parser_expression (parser);
4662	  /* The standard requires that the expression have integral
4663	     type.  DR 74 adds enumeration types.  We believe that the
4664	     real intent is that these expressions be handled like the
4665	     expression in a `switch' condition, which also allows
4666	     classes with a single conversion to integral or
4667	     enumeration type.  */
4668	  if (!processing_template_decl)
4669	    {
4670	      expression
4671		= build_expr_type_conversion (WANT_INT | WANT_ENUM,
4672					      expression,
4673					      /*complain=*/true);
4674	      if (!expression)
4675		{
4676		  error ("expression in new-declarator must have integral or enumeration type");
4677		  expression = error_mark_node;
4678		}
4679	    }
4680	}
4681      /* But all the other expressions must be.  */
4682      else
4683	expression
4684	  = cp_parser_constant_expression (parser,
4685					   /*allow_non_constant=*/false,
4686					   NULL);
4687      /* Look for the closing `]'.  */
4688      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4689
4690      /* Add this bound to the declarator.  */
4691      declarator = build_nt (ARRAY_REF, declarator, expression);
4692
4693      /* If the next token is not a `[', then there are no more
4694	 bounds.  */
4695      if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4696	break;
4697    }
4698
4699  return declarator;
4700}
4701
4702/* Parse a new-initializer.
4703
4704   new-initializer:
4705     ( expression-list [opt] )
4706
4707   Returns a representation of the expression-list.  If there is no
4708   expression-list, VOID_ZERO_NODE is returned.  */
4709
4710static tree
4711cp_parser_new_initializer (cp_parser* parser)
4712{
4713  tree expression_list;
4714
4715  expression_list = (cp_parser_parenthesized_expression_list
4716		     (parser, false, /*non_constant_p=*/NULL));
4717  if (!expression_list)
4718    expression_list = void_zero_node;
4719
4720  return expression_list;
4721}
4722
4723/* Parse a delete-expression.
4724
4725   delete-expression:
4726     :: [opt] delete cast-expression
4727     :: [opt] delete [ ] cast-expression
4728
4729   Returns a representation of the expression.  */
4730
4731static tree
4732cp_parser_delete_expression (cp_parser* parser)
4733{
4734  bool global_scope_p;
4735  bool array_p;
4736  tree expression;
4737
4738  /* Look for the optional `::' operator.  */
4739  global_scope_p
4740    = (cp_parser_global_scope_opt (parser,
4741				   /*current_scope_valid_p=*/false)
4742       != NULL_TREE);
4743  /* Look for the `delete' keyword.  */
4744  cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4745  /* See if the array syntax is in use.  */
4746  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4747    {
4748      /* Consume the `[' token.  */
4749      cp_lexer_consume_token (parser->lexer);
4750      /* Look for the `]' token.  */
4751      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4752      /* Remember that this is the `[]' construct.  */
4753      array_p = true;
4754    }
4755  else
4756    array_p = false;
4757
4758  /* Parse the cast-expression.  */
4759  expression = cp_parser_simple_cast_expression (parser);
4760
4761  /* A delete-expression may not appear in an integral constant
4762     expression.  */
4763  if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
4764    return error_mark_node;
4765
4766  return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4767}
4768
4769/* Parse a cast-expression.
4770
4771   cast-expression:
4772     unary-expression
4773     ( type-id ) cast-expression
4774
4775   Returns a representation of the expression.  */
4776
4777static tree
4778cp_parser_cast_expression (cp_parser *parser, bool address_p)
4779{
4780  /* If it's a `(', then we might be looking at a cast.  */
4781  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4782    {
4783      tree type = NULL_TREE;
4784      tree expr = NULL_TREE;
4785      bool compound_literal_p;
4786      const char *saved_message;
4787
4788      /* There's no way to know yet whether or not this is a cast.
4789	 For example, `(int (3))' is a unary-expression, while `(int)
4790	 3' is a cast.  So, we resort to parsing tentatively.  */
4791      cp_parser_parse_tentatively (parser);
4792      /* Types may not be defined in a cast.  */
4793      saved_message = parser->type_definition_forbidden_message;
4794      parser->type_definition_forbidden_message
4795	= "types may not be defined in casts";
4796      /* Consume the `('.  */
4797      cp_lexer_consume_token (parser->lexer);
4798      /* A very tricky bit is that `(struct S) { 3 }' is a
4799	 compound-literal (which we permit in C++ as an extension).
4800	 But, that construct is not a cast-expression -- it is a
4801	 postfix-expression.  (The reason is that `(struct S) { 3 }.i'
4802	 is legal; if the compound-literal were a cast-expression,
4803	 you'd need an extra set of parentheses.)  But, if we parse
4804	 the type-id, and it happens to be a class-specifier, then we
4805	 will commit to the parse at that point, because we cannot
4806	 undo the action that is done when creating a new class.  So,
4807	 then we cannot back up and do a postfix-expression.
4808
4809	 Therefore, we scan ahead to the closing `)', and check to see
4810	 if the token after the `)' is a `{'.  If so, we are not
4811	 looking at a cast-expression.
4812
4813	 Save tokens so that we can put them back.  */
4814      cp_lexer_save_tokens (parser->lexer);
4815      /* Skip tokens until the next token is a closing parenthesis.
4816	 If we find the closing `)', and the next token is a `{', then
4817	 we are looking at a compound-literal.  */
4818      compound_literal_p
4819	= (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4820						  /*consume_paren=*/true)
4821	   && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4822      /* Roll back the tokens we skipped.  */
4823      cp_lexer_rollback_tokens (parser->lexer);
4824      /* If we were looking at a compound-literal, simulate an error
4825	 so that the call to cp_parser_parse_definitely below will
4826	 fail.  */
4827      if (compound_literal_p)
4828	cp_parser_simulate_error (parser);
4829      else
4830	{
4831	  bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4832	  parser->in_type_id_in_expr_p = true;
4833	  /* Look for the type-id.  */
4834	  type = cp_parser_type_id (parser);
4835	  /* Look for the closing `)'.  */
4836	  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4837	  parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4838	}
4839
4840      /* Restore the saved message.  */
4841      parser->type_definition_forbidden_message = saved_message;
4842
4843      /* If ok so far, parse the dependent expression. We cannot be
4844         sure it is a cast. Consider `(T ())'.  It is a parenthesized
4845         ctor of T, but looks like a cast to function returning T
4846         without a dependent expression.  */
4847      if (!cp_parser_error_occurred (parser))
4848	expr = cp_parser_simple_cast_expression (parser);
4849
4850      if (cp_parser_parse_definitely (parser))
4851	{
4852	  /* Warn about old-style casts, if so requested.  */
4853	  if (warn_old_style_cast
4854	      && !in_system_header
4855	      && !VOID_TYPE_P (type)
4856	      && current_lang_name != lang_name_c)
4857	    warning ("use of old-style cast");
4858
4859	  /* Only type conversions to integral or enumeration types
4860	     can be used in constant-expressions.  */
4861	  if (parser->integral_constant_expression_p
4862	      && !dependent_type_p (type)
4863	      && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4864	      && (cp_parser_non_integral_constant_expression
4865		  (parser,
4866		   "a casts to a type other than an integral or "
4867		   "enumeration type")))
4868	    return error_mark_node;
4869
4870	  /* Perform the cast.  */
4871	  expr = build_c_cast (type, expr);
4872	  return expr;
4873	}
4874    }
4875
4876  /* If we get here, then it's not a cast, so it must be a
4877     unary-expression.  */
4878  return cp_parser_unary_expression (parser, address_p);
4879}
4880
4881/* Parse a pm-expression.
4882
4883   pm-expression:
4884     cast-expression
4885     pm-expression .* cast-expression
4886     pm-expression ->* cast-expression
4887
4888     Returns a representation of the expression.  */
4889
4890static tree
4891cp_parser_pm_expression (cp_parser* parser)
4892{
4893  static const cp_parser_token_tree_map map = {
4894    { CPP_DEREF_STAR, MEMBER_REF },
4895    { CPP_DOT_STAR, DOTSTAR_EXPR },
4896    { CPP_EOF, ERROR_MARK }
4897  };
4898
4899  return cp_parser_binary_expression (parser, map,
4900				      cp_parser_simple_cast_expression);
4901}
4902
4903/* Parse a multiplicative-expression.
4904
4905   mulitplicative-expression:
4906     pm-expression
4907     multiplicative-expression * pm-expression
4908     multiplicative-expression / pm-expression
4909     multiplicative-expression % pm-expression
4910
4911   Returns a representation of the expression.  */
4912
4913static tree
4914cp_parser_multiplicative_expression (cp_parser* parser)
4915{
4916  static const cp_parser_token_tree_map map = {
4917    { CPP_MULT, MULT_EXPR },
4918    { CPP_DIV, TRUNC_DIV_EXPR },
4919    { CPP_MOD, TRUNC_MOD_EXPR },
4920    { CPP_EOF, ERROR_MARK }
4921  };
4922
4923  return cp_parser_binary_expression (parser,
4924				      map,
4925				      cp_parser_pm_expression);
4926}
4927
4928/* Parse an additive-expression.
4929
4930   additive-expression:
4931     multiplicative-expression
4932     additive-expression + multiplicative-expression
4933     additive-expression - multiplicative-expression
4934
4935   Returns a representation of the expression.  */
4936
4937static tree
4938cp_parser_additive_expression (cp_parser* parser)
4939{
4940  static const cp_parser_token_tree_map map = {
4941    { CPP_PLUS, PLUS_EXPR },
4942    { CPP_MINUS, MINUS_EXPR },
4943    { CPP_EOF, ERROR_MARK }
4944  };
4945
4946  return cp_parser_binary_expression (parser,
4947				      map,
4948				      cp_parser_multiplicative_expression);
4949}
4950
4951/* Parse a shift-expression.
4952
4953   shift-expression:
4954     additive-expression
4955     shift-expression << additive-expression
4956     shift-expression >> additive-expression
4957
4958   Returns a representation of the expression.  */
4959
4960static tree
4961cp_parser_shift_expression (cp_parser* parser)
4962{
4963  static const cp_parser_token_tree_map map = {
4964    { CPP_LSHIFT, LSHIFT_EXPR },
4965    { CPP_RSHIFT, RSHIFT_EXPR },
4966    { CPP_EOF, ERROR_MARK }
4967  };
4968
4969  return cp_parser_binary_expression (parser,
4970				      map,
4971				      cp_parser_additive_expression);
4972}
4973
4974/* Parse a relational-expression.
4975
4976   relational-expression:
4977     shift-expression
4978     relational-expression < shift-expression
4979     relational-expression > shift-expression
4980     relational-expression <= shift-expression
4981     relational-expression >= shift-expression
4982
4983   GNU Extension:
4984
4985   relational-expression:
4986     relational-expression <? shift-expression
4987     relational-expression >? shift-expression
4988
4989   Returns a representation of the expression.  */
4990
4991static tree
4992cp_parser_relational_expression (cp_parser* parser)
4993{
4994  static const cp_parser_token_tree_map map = {
4995    { CPP_LESS, LT_EXPR },
4996    { CPP_GREATER, GT_EXPR },
4997    { CPP_LESS_EQ, LE_EXPR },
4998    { CPP_GREATER_EQ, GE_EXPR },
4999    { CPP_MIN, MIN_EXPR },
5000    { CPP_MAX, MAX_EXPR },
5001    { CPP_EOF, ERROR_MARK }
5002  };
5003
5004  return cp_parser_binary_expression (parser,
5005				      map,
5006				      cp_parser_shift_expression);
5007}
5008
5009/* Parse an equality-expression.
5010
5011   equality-expression:
5012     relational-expression
5013     equality-expression == relational-expression
5014     equality-expression != relational-expression
5015
5016   Returns a representation of the expression.  */
5017
5018static tree
5019cp_parser_equality_expression (cp_parser* parser)
5020{
5021  static const cp_parser_token_tree_map map = {
5022    { CPP_EQ_EQ, EQ_EXPR },
5023    { CPP_NOT_EQ, NE_EXPR },
5024    { CPP_EOF, ERROR_MARK }
5025  };
5026
5027  return cp_parser_binary_expression (parser,
5028				      map,
5029				      cp_parser_relational_expression);
5030}
5031
5032/* Parse an and-expression.
5033
5034   and-expression:
5035     equality-expression
5036     and-expression & equality-expression
5037
5038   Returns a representation of the expression.  */
5039
5040static tree
5041cp_parser_and_expression (cp_parser* parser)
5042{
5043  static const cp_parser_token_tree_map map = {
5044    { CPP_AND, BIT_AND_EXPR },
5045    { CPP_EOF, ERROR_MARK }
5046  };
5047
5048  return cp_parser_binary_expression (parser,
5049				      map,
5050				      cp_parser_equality_expression);
5051}
5052
5053/* Parse an exclusive-or-expression.
5054
5055   exclusive-or-expression:
5056     and-expression
5057     exclusive-or-expression ^ and-expression
5058
5059   Returns a representation of the expression.  */
5060
5061static tree
5062cp_parser_exclusive_or_expression (cp_parser* parser)
5063{
5064  static const cp_parser_token_tree_map map = {
5065    { CPP_XOR, BIT_XOR_EXPR },
5066    { CPP_EOF, ERROR_MARK }
5067  };
5068
5069  return cp_parser_binary_expression (parser,
5070				      map,
5071				      cp_parser_and_expression);
5072}
5073
5074
5075/* Parse an inclusive-or-expression.
5076
5077   inclusive-or-expression:
5078     exclusive-or-expression
5079     inclusive-or-expression | exclusive-or-expression
5080
5081   Returns a representation of the expression.  */
5082
5083static tree
5084cp_parser_inclusive_or_expression (cp_parser* parser)
5085{
5086  static const cp_parser_token_tree_map map = {
5087    { CPP_OR, BIT_IOR_EXPR },
5088    { CPP_EOF, ERROR_MARK }
5089  };
5090
5091  return cp_parser_binary_expression (parser,
5092				      map,
5093				      cp_parser_exclusive_or_expression);
5094}
5095
5096/* Parse a logical-and-expression.
5097
5098   logical-and-expression:
5099     inclusive-or-expression
5100     logical-and-expression && inclusive-or-expression
5101
5102   Returns a representation of the expression.  */
5103
5104static tree
5105cp_parser_logical_and_expression (cp_parser* parser)
5106{
5107  static const cp_parser_token_tree_map map = {
5108    { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5109    { CPP_EOF, ERROR_MARK }
5110  };
5111
5112  return cp_parser_binary_expression (parser,
5113				      map,
5114				      cp_parser_inclusive_or_expression);
5115}
5116
5117/* Parse a logical-or-expression.
5118
5119   logical-or-expression:
5120     logical-and-expression
5121     logical-or-expression || logical-and-expression
5122
5123   Returns a representation of the expression.  */
5124
5125static tree
5126cp_parser_logical_or_expression (cp_parser* parser)
5127{
5128  static const cp_parser_token_tree_map map = {
5129    { CPP_OR_OR, TRUTH_ORIF_EXPR },
5130    { CPP_EOF, ERROR_MARK }
5131  };
5132
5133  return cp_parser_binary_expression (parser,
5134				      map,
5135				      cp_parser_logical_and_expression);
5136}
5137
5138/* Parse the `? expression : assignment-expression' part of a
5139   conditional-expression.  The LOGICAL_OR_EXPR is the
5140   logical-or-expression that started the conditional-expression.
5141   Returns a representation of the entire conditional-expression.
5142
5143   This routine is used by cp_parser_assignment_expression.
5144
5145     ? expression : assignment-expression
5146
5147   GNU Extensions:
5148
5149     ? : assignment-expression */
5150
5151static tree
5152cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5153{
5154  tree expr;
5155  tree assignment_expr;
5156
5157  /* Consume the `?' token.  */
5158  cp_lexer_consume_token (parser->lexer);
5159  if (cp_parser_allow_gnu_extensions_p (parser)
5160      && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5161    /* Implicit true clause.  */
5162    expr = NULL_TREE;
5163  else
5164    /* Parse the expression.  */
5165    expr = cp_parser_expression (parser);
5166
5167  /* The next token should be a `:'.  */
5168  cp_parser_require (parser, CPP_COLON, "`:'");
5169  /* Parse the assignment-expression.  */
5170  assignment_expr = cp_parser_assignment_expression (parser);
5171
5172  /* Build the conditional-expression.  */
5173  return build_x_conditional_expr (logical_or_expr,
5174				   expr,
5175				   assignment_expr);
5176}
5177
5178/* Parse an assignment-expression.
5179
5180   assignment-expression:
5181     conditional-expression
5182     logical-or-expression assignment-operator assignment_expression
5183     throw-expression
5184
5185   Returns a representation for the expression.  */
5186
5187static tree
5188cp_parser_assignment_expression (cp_parser* parser)
5189{
5190  tree expr;
5191
5192  /* If the next token is the `throw' keyword, then we're looking at
5193     a throw-expression.  */
5194  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5195    expr = cp_parser_throw_expression (parser);
5196  /* Otherwise, it must be that we are looking at a
5197     logical-or-expression.  */
5198  else
5199    {
5200      /* Parse the logical-or-expression.  */
5201      expr = cp_parser_logical_or_expression (parser);
5202      /* If the next token is a `?' then we're actually looking at a
5203	 conditional-expression.  */
5204      if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5205	return cp_parser_question_colon_clause (parser, expr);
5206      else
5207	{
5208	  enum tree_code assignment_operator;
5209
5210	  /* If it's an assignment-operator, we're using the second
5211	     production.  */
5212	  assignment_operator
5213	    = cp_parser_assignment_operator_opt (parser);
5214	  if (assignment_operator != ERROR_MARK)
5215	    {
5216	      tree rhs;
5217
5218	      /* Parse the right-hand side of the assignment.  */
5219	      rhs = cp_parser_assignment_expression (parser);
5220	      /* An assignment may not appear in a
5221		 constant-expression.  */
5222	      if (cp_parser_non_integral_constant_expression (parser,
5223							      "an assignment"))
5224		return error_mark_node;
5225	      /* Build the assignment expression.  */
5226	      expr = build_x_modify_expr (expr,
5227					  assignment_operator,
5228					  rhs);
5229	    }
5230	}
5231    }
5232
5233  return expr;
5234}
5235
5236/* Parse an (optional) assignment-operator.
5237
5238   assignment-operator: one of
5239     = *= /= %= += -= >>= <<= &= ^= |=
5240
5241   GNU Extension:
5242
5243   assignment-operator: one of
5244     <?= >?=
5245
5246   If the next token is an assignment operator, the corresponding tree
5247   code is returned, and the token is consumed.  For example, for
5248   `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5249   NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5250   TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5251   operator, ERROR_MARK is returned.  */
5252
5253static enum tree_code
5254cp_parser_assignment_operator_opt (cp_parser* parser)
5255{
5256  enum tree_code op;
5257  cp_token *token;
5258
5259  /* Peek at the next toen.  */
5260  token = cp_lexer_peek_token (parser->lexer);
5261
5262  switch (token->type)
5263    {
5264    case CPP_EQ:
5265      op = NOP_EXPR;
5266      break;
5267
5268    case CPP_MULT_EQ:
5269      op = MULT_EXPR;
5270      break;
5271
5272    case CPP_DIV_EQ:
5273      op = TRUNC_DIV_EXPR;
5274      break;
5275
5276    case CPP_MOD_EQ:
5277      op = TRUNC_MOD_EXPR;
5278      break;
5279
5280    case CPP_PLUS_EQ:
5281      op = PLUS_EXPR;
5282      break;
5283
5284    case CPP_MINUS_EQ:
5285      op = MINUS_EXPR;
5286      break;
5287
5288    case CPP_RSHIFT_EQ:
5289      op = RSHIFT_EXPR;
5290      break;
5291
5292    case CPP_LSHIFT_EQ:
5293      op = LSHIFT_EXPR;
5294      break;
5295
5296    case CPP_AND_EQ:
5297      op = BIT_AND_EXPR;
5298      break;
5299
5300    case CPP_XOR_EQ:
5301      op = BIT_XOR_EXPR;
5302      break;
5303
5304    case CPP_OR_EQ:
5305      op = BIT_IOR_EXPR;
5306      break;
5307
5308    case CPP_MIN_EQ:
5309      op = MIN_EXPR;
5310      break;
5311
5312    case CPP_MAX_EQ:
5313      op = MAX_EXPR;
5314      break;
5315
5316    default:
5317      /* Nothing else is an assignment operator.  */
5318      op = ERROR_MARK;
5319    }
5320
5321  /* If it was an assignment operator, consume it.  */
5322  if (op != ERROR_MARK)
5323    cp_lexer_consume_token (parser->lexer);
5324
5325  return op;
5326}
5327
5328/* Parse an expression.
5329
5330   expression:
5331     assignment-expression
5332     expression , assignment-expression
5333
5334   Returns a representation of the expression.  */
5335
5336static tree
5337cp_parser_expression (cp_parser* parser)
5338{
5339  tree expression = NULL_TREE;
5340
5341  while (true)
5342    {
5343      tree assignment_expression;
5344
5345      /* Parse the next assignment-expression.  */
5346      assignment_expression
5347	= cp_parser_assignment_expression (parser);
5348      /* If this is the first assignment-expression, we can just
5349	 save it away.  */
5350      if (!expression)
5351	expression = assignment_expression;
5352      else
5353	expression = build_x_compound_expr (expression,
5354					    assignment_expression);
5355      /* If the next token is not a comma, then we are done with the
5356	 expression.  */
5357      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5358	break;
5359      /* Consume the `,'.  */
5360      cp_lexer_consume_token (parser->lexer);
5361      /* A comma operator cannot appear in a constant-expression.  */
5362      if (cp_parser_non_integral_constant_expression (parser,
5363						      "a comma operator"))
5364	expression = error_mark_node;
5365    }
5366
5367  return expression;
5368}
5369
5370/* Parse a constant-expression.
5371
5372   constant-expression:
5373     conditional-expression
5374
5375  If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5376  accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5377  constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5378  is false, NON_CONSTANT_P should be NULL.  */
5379
5380static tree
5381cp_parser_constant_expression (cp_parser* parser,
5382			       bool allow_non_constant_p,
5383			       bool *non_constant_p)
5384{
5385  bool saved_integral_constant_expression_p;
5386  bool saved_allow_non_integral_constant_expression_p;
5387  bool saved_non_integral_constant_expression_p;
5388  tree expression;
5389
5390  /* It might seem that we could simply parse the
5391     conditional-expression, and then check to see if it were
5392     TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5393     one that the compiler can figure out is constant, possibly after
5394     doing some simplifications or optimizations.  The standard has a
5395     precise definition of constant-expression, and we must honor
5396     that, even though it is somewhat more restrictive.
5397
5398     For example:
5399
5400       int i[(2, 3)];
5401
5402     is not a legal declaration, because `(2, 3)' is not a
5403     constant-expression.  The `,' operator is forbidden in a
5404     constant-expression.  However, GCC's constant-folding machinery
5405     will fold this operation to an INTEGER_CST for `3'.  */
5406
5407  /* Save the old settings.  */
5408  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5409  saved_allow_non_integral_constant_expression_p
5410    = parser->allow_non_integral_constant_expression_p;
5411  saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5412  /* We are now parsing a constant-expression.  */
5413  parser->integral_constant_expression_p = true;
5414  parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5415  parser->non_integral_constant_expression_p = false;
5416  /* Although the grammar says "conditional-expression", we parse an
5417     "assignment-expression", which also permits "throw-expression"
5418     and the use of assignment operators.  In the case that
5419     ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5420     otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5421     actually essential that we look for an assignment-expression.
5422     For example, cp_parser_initializer_clauses uses this function to
5423     determine whether a particular assignment-expression is in fact
5424     constant.  */
5425  expression = cp_parser_assignment_expression (parser);
5426  /* Restore the old settings.  */
5427  parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5428  parser->allow_non_integral_constant_expression_p
5429    = saved_allow_non_integral_constant_expression_p;
5430  if (allow_non_constant_p)
5431    *non_constant_p = parser->non_integral_constant_expression_p;
5432  parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5433
5434  return expression;
5435}
5436
5437/* Statements [gram.stmt.stmt]  */
5438
5439/* Parse a statement.
5440
5441   statement:
5442     labeled-statement
5443     expression-statement
5444     compound-statement
5445     selection-statement
5446     iteration-statement
5447     jump-statement
5448     declaration-statement
5449     try-block  */
5450
5451static void
5452cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5453{
5454  tree statement;
5455  cp_token *token;
5456  int statement_line_number;
5457
5458  /* There is no statement yet.  */
5459  statement = NULL_TREE;
5460  /* Peek at the next token.  */
5461  token = cp_lexer_peek_token (parser->lexer);
5462  /* Remember the line number of the first token in the statement.  */
5463  statement_line_number = token->location.line;
5464  /* If this is a keyword, then that will often determine what kind of
5465     statement we have.  */
5466  if (token->type == CPP_KEYWORD)
5467    {
5468      enum rid keyword = token->keyword;
5469
5470      switch (keyword)
5471	{
5472	case RID_CASE:
5473	case RID_DEFAULT:
5474	  statement = cp_parser_labeled_statement (parser,
5475						   in_statement_expr_p);
5476	  break;
5477
5478	case RID_IF:
5479	case RID_SWITCH:
5480	  statement = cp_parser_selection_statement (parser);
5481	  break;
5482
5483	case RID_WHILE:
5484	case RID_DO:
5485	case RID_FOR:
5486	  statement = cp_parser_iteration_statement (parser);
5487	  break;
5488
5489	case RID_BREAK:
5490	case RID_CONTINUE:
5491	case RID_RETURN:
5492	case RID_GOTO:
5493	  statement = cp_parser_jump_statement (parser);
5494	  break;
5495
5496	case RID_TRY:
5497	  statement = cp_parser_try_block (parser);
5498	  break;
5499
5500	default:
5501	  /* It might be a keyword like `int' that can start a
5502	     declaration-statement.  */
5503	  break;
5504	}
5505    }
5506  else if (token->type == CPP_NAME)
5507    {
5508      /* If the next token is a `:', then we are looking at a
5509	 labeled-statement.  */
5510      token = cp_lexer_peek_nth_token (parser->lexer, 2);
5511      if (token->type == CPP_COLON)
5512	statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5513    }
5514  /* Anything that starts with a `{' must be a compound-statement.  */
5515  else if (token->type == CPP_OPEN_BRACE)
5516    statement = cp_parser_compound_statement (parser, false);
5517
5518  /* Everything else must be a declaration-statement or an
5519     expression-statement.  Try for the declaration-statement
5520     first, unless we are looking at a `;', in which case we know that
5521     we have an expression-statement.  */
5522  if (!statement)
5523    {
5524      if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5525	{
5526	  cp_parser_parse_tentatively (parser);
5527	  /* Try to parse the declaration-statement.  */
5528	  cp_parser_declaration_statement (parser);
5529	  /* If that worked, we're done.  */
5530	  if (cp_parser_parse_definitely (parser))
5531	    return;
5532	}
5533      /* Look for an expression-statement instead.  */
5534      statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5535    }
5536
5537  /* Set the line number for the statement.  */
5538  if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5539    STMT_LINENO (statement) = statement_line_number;
5540}
5541
5542/* Parse a labeled-statement.
5543
5544   labeled-statement:
5545     identifier : statement
5546     case constant-expression : statement
5547     default : statement
5548
5549   GNU Extension:
5550
5551   labeled-statement:
5552     case constant-expression ... constant-expression : statement
5553
5554   Returns the new CASE_LABEL, for a `case' or `default' label.  For
5555   an ordinary label, returns a LABEL_STMT.  */
5556
5557static tree
5558cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5559{
5560  cp_token *token;
5561  tree statement = error_mark_node;
5562
5563  /* The next token should be an identifier.  */
5564  token = cp_lexer_peek_token (parser->lexer);
5565  if (token->type != CPP_NAME
5566      && token->type != CPP_KEYWORD)
5567    {
5568      cp_parser_error (parser, "expected labeled-statement");
5569      return error_mark_node;
5570    }
5571
5572  switch (token->keyword)
5573    {
5574    case RID_CASE:
5575      {
5576	tree expr, expr_hi;
5577	cp_token *ellipsis;
5578
5579	/* Consume the `case' token.  */
5580	cp_lexer_consume_token (parser->lexer);
5581	/* Parse the constant-expression.  */
5582	expr = cp_parser_constant_expression (parser,
5583					      /*allow_non_constant_p=*/false,
5584					      NULL);
5585
5586	ellipsis = cp_lexer_peek_token (parser->lexer);
5587	if (ellipsis->type == CPP_ELLIPSIS)
5588	  {
5589            /* Consume the `...' token.  */
5590	    cp_lexer_consume_token (parser->lexer);
5591	    expr_hi =
5592	      cp_parser_constant_expression (parser,
5593	    				     /*allow_non_constant_p=*/false,
5594					     NULL);
5595	    /* We don't need to emit warnings here, as the common code
5596	       will do this for us.  */
5597	  }
5598	else
5599	  expr_hi = NULL_TREE;
5600
5601	if (!parser->in_switch_statement_p)
5602	  error ("case label `%E' not within a switch statement", expr);
5603	else
5604	  statement = finish_case_label (expr, expr_hi);
5605      }
5606      break;
5607
5608    case RID_DEFAULT:
5609      /* Consume the `default' token.  */
5610      cp_lexer_consume_token (parser->lexer);
5611      if (!parser->in_switch_statement_p)
5612	error ("case label not within a switch statement");
5613      else
5614	statement = finish_case_label (NULL_TREE, NULL_TREE);
5615      break;
5616
5617    default:
5618      /* Anything else must be an ordinary label.  */
5619      statement = finish_label_stmt (cp_parser_identifier (parser));
5620      break;
5621    }
5622
5623  /* Require the `:' token.  */
5624  cp_parser_require (parser, CPP_COLON, "`:'");
5625  /* Parse the labeled statement.  */
5626  cp_parser_statement (parser, in_statement_expr_p);
5627
5628  /* Return the label, in the case of a `case' or `default' label.  */
5629  return statement;
5630}
5631
5632/* Parse an expression-statement.
5633
5634   expression-statement:
5635     expression [opt] ;
5636
5637   Returns the new EXPR_STMT -- or NULL_TREE if the expression
5638   statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5639   indicates whether this expression-statement is part of an
5640   expression statement.  */
5641
5642static tree
5643cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5644{
5645  tree statement = NULL_TREE;
5646
5647  /* If the next token is a ';', then there is no expression
5648     statement.  */
5649  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5650    statement = cp_parser_expression (parser);
5651
5652  /* Consume the final `;'.  */
5653  cp_parser_consume_semicolon_at_end_of_statement (parser);
5654
5655  if (in_statement_expr_p
5656      && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5657    {
5658      /* This is the final expression statement of a statement
5659	 expression.  */
5660      statement = finish_stmt_expr_expr (statement);
5661    }
5662  else if (statement)
5663    statement = finish_expr_stmt (statement);
5664  else
5665    finish_stmt ();
5666
5667  return statement;
5668}
5669
5670/* Parse a compound-statement.
5671
5672   compound-statement:
5673     { statement-seq [opt] }
5674
5675   Returns a COMPOUND_STMT representing the statement.  */
5676
5677static tree
5678cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5679{
5680  tree compound_stmt;
5681
5682  /* Consume the `{'.  */
5683  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5684    return error_mark_node;
5685  /* Begin the compound-statement.  */
5686  compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5687  /* Parse an (optional) statement-seq.  */
5688  cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5689  /* Finish the compound-statement.  */
5690  finish_compound_stmt (compound_stmt);
5691  /* Consume the `}'.  */
5692  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5693
5694  return compound_stmt;
5695}
5696
5697/* Parse an (optional) statement-seq.
5698
5699   statement-seq:
5700     statement
5701     statement-seq [opt] statement  */
5702
5703static void
5704cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5705{
5706  /* Scan statements until there aren't any more.  */
5707  while (true)
5708    {
5709      /* If we're looking at a `}', then we've run out of statements.  */
5710      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5711	  || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5712	break;
5713
5714      /* Parse the statement.  */
5715      cp_parser_statement (parser, in_statement_expr_p);
5716    }
5717}
5718
5719/* Parse a selection-statement.
5720
5721   selection-statement:
5722     if ( condition ) statement
5723     if ( condition ) statement else statement
5724     switch ( condition ) statement
5725
5726   Returns the new IF_STMT or SWITCH_STMT.  */
5727
5728static tree
5729cp_parser_selection_statement (cp_parser* parser)
5730{
5731  cp_token *token;
5732  enum rid keyword;
5733
5734  /* Peek at the next token.  */
5735  token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5736
5737  /* See what kind of keyword it is.  */
5738  keyword = token->keyword;
5739  switch (keyword)
5740    {
5741    case RID_IF:
5742    case RID_SWITCH:
5743      {
5744	tree statement;
5745	tree condition;
5746
5747	/* Look for the `('.  */
5748	if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5749	  {
5750	    cp_parser_skip_to_end_of_statement (parser);
5751	    return error_mark_node;
5752	  }
5753
5754	/* Begin the selection-statement.  */
5755	if (keyword == RID_IF)
5756	  statement = begin_if_stmt ();
5757	else
5758	  statement = begin_switch_stmt ();
5759
5760	/* Parse the condition.  */
5761	condition = cp_parser_condition (parser);
5762	/* Look for the `)'.  */
5763	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5764	  cp_parser_skip_to_closing_parenthesis (parser, true, false,
5765						 /*consume_paren=*/true);
5766
5767	if (keyword == RID_IF)
5768	  {
5769	    tree then_stmt;
5770
5771	    /* Add the condition.  */
5772	    finish_if_stmt_cond (condition, statement);
5773
5774	    /* Parse the then-clause.  */
5775	    then_stmt = cp_parser_implicitly_scoped_statement (parser);
5776	    finish_then_clause (statement);
5777
5778	    /* If the next token is `else', parse the else-clause.  */
5779	    if (cp_lexer_next_token_is_keyword (parser->lexer,
5780						RID_ELSE))
5781	      {
5782		tree else_stmt;
5783
5784		/* Consume the `else' keyword.  */
5785		cp_lexer_consume_token (parser->lexer);
5786		/* Parse the else-clause.  */
5787		else_stmt
5788		  = cp_parser_implicitly_scoped_statement (parser);
5789		finish_else_clause (statement);
5790	      }
5791
5792	    /* Now we're all done with the if-statement.  */
5793	    finish_if_stmt ();
5794	  }
5795	else
5796	  {
5797	    tree body;
5798	    bool in_switch_statement_p;
5799
5800	    /* Add the condition.  */
5801	    finish_switch_cond (condition, statement);
5802
5803	    /* Parse the body of the switch-statement.  */
5804	    in_switch_statement_p = parser->in_switch_statement_p;
5805	    parser->in_switch_statement_p = true;
5806	    body = cp_parser_implicitly_scoped_statement (parser);
5807	    parser->in_switch_statement_p = in_switch_statement_p;
5808
5809	    /* Now we're all done with the switch-statement.  */
5810	    finish_switch_stmt (statement);
5811	  }
5812
5813	return statement;
5814      }
5815      break;
5816
5817    default:
5818      cp_parser_error (parser, "expected selection-statement");
5819      return error_mark_node;
5820    }
5821}
5822
5823/* Parse a condition.
5824
5825   condition:
5826     expression
5827     type-specifier-seq declarator = assignment-expression
5828
5829   GNU Extension:
5830
5831   condition:
5832     type-specifier-seq declarator asm-specification [opt]
5833       attributes [opt] = assignment-expression
5834
5835   Returns the expression that should be tested.  */
5836
5837static tree
5838cp_parser_condition (cp_parser* parser)
5839{
5840  tree type_specifiers;
5841  const char *saved_message;
5842
5843  /* Try the declaration first.  */
5844  cp_parser_parse_tentatively (parser);
5845  /* New types are not allowed in the type-specifier-seq for a
5846     condition.  */
5847  saved_message = parser->type_definition_forbidden_message;
5848  parser->type_definition_forbidden_message
5849    = "types may not be defined in conditions";
5850  /* Parse the type-specifier-seq.  */
5851  type_specifiers = cp_parser_type_specifier_seq (parser);
5852  /* Restore the saved message.  */
5853  parser->type_definition_forbidden_message = saved_message;
5854  /* If all is well, we might be looking at a declaration.  */
5855  if (!cp_parser_error_occurred (parser))
5856    {
5857      tree decl;
5858      tree asm_specification;
5859      tree attributes;
5860      tree declarator;
5861      tree initializer = NULL_TREE;
5862
5863      /* Parse the declarator.  */
5864      declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5865					 /*ctor_dtor_or_conv_p=*/NULL,
5866					 /*parenthesized_p=*/NULL,
5867					 /*member_p=*/false);
5868      /* Parse the attributes.  */
5869      attributes = cp_parser_attributes_opt (parser);
5870      /* Parse the asm-specification.  */
5871      asm_specification = cp_parser_asm_specification_opt (parser);
5872      /* If the next token is not an `=', then we might still be
5873	 looking at an expression.  For example:
5874
5875	   if (A(a).x)
5876
5877	 looks like a decl-specifier-seq and a declarator -- but then
5878	 there is no `=', so this is an expression.  */
5879      cp_parser_require (parser, CPP_EQ, "`='");
5880      /* If we did see an `=', then we are looking at a declaration
5881	 for sure.  */
5882      if (cp_parser_parse_definitely (parser))
5883	{
5884	  /* Create the declaration.  */
5885	  decl = start_decl (declarator, type_specifiers,
5886			     /*initialized_p=*/true,
5887			     attributes, /*prefix_attributes=*/NULL_TREE);
5888	  /* Parse the assignment-expression.  */
5889	  initializer = cp_parser_assignment_expression (parser);
5890
5891	  /* Process the initializer.  */
5892	  cp_finish_decl (decl,
5893			  initializer,
5894			  asm_specification,
5895			  LOOKUP_ONLYCONVERTING);
5896
5897	  return convert_from_reference (decl);
5898	}
5899    }
5900  /* If we didn't even get past the declarator successfully, we are
5901     definitely not looking at a declaration.  */
5902  else
5903    cp_parser_abort_tentative_parse (parser);
5904
5905  /* Otherwise, we are looking at an expression.  */
5906  return cp_parser_expression (parser);
5907}
5908
5909/* Parse an iteration-statement.
5910
5911   iteration-statement:
5912     while ( condition ) statement
5913     do statement while ( expression ) ;
5914     for ( for-init-statement condition [opt] ; expression [opt] )
5915       statement
5916
5917   Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
5918
5919static tree
5920cp_parser_iteration_statement (cp_parser* parser)
5921{
5922  cp_token *token;
5923  enum rid keyword;
5924  tree statement;
5925  bool in_iteration_statement_p;
5926
5927
5928  /* Peek at the next token.  */
5929  token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5930  if (!token)
5931    return error_mark_node;
5932
5933  /* Remember whether or not we are already within an iteration
5934     statement.  */
5935  in_iteration_statement_p = parser->in_iteration_statement_p;
5936
5937  /* See what kind of keyword it is.  */
5938  keyword = token->keyword;
5939  switch (keyword)
5940    {
5941    case RID_WHILE:
5942      {
5943	tree condition;
5944
5945	/* Begin the while-statement.  */
5946	statement = begin_while_stmt ();
5947	/* Look for the `('.  */
5948	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5949	/* Parse the condition.  */
5950	condition = cp_parser_condition (parser);
5951	finish_while_stmt_cond (condition, statement);
5952	/* Look for the `)'.  */
5953	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5954	/* Parse the dependent statement.  */
5955	parser->in_iteration_statement_p = true;
5956	cp_parser_already_scoped_statement (parser);
5957	parser->in_iteration_statement_p = in_iteration_statement_p;
5958	/* We're done with the while-statement.  */
5959	finish_while_stmt (statement);
5960      }
5961      break;
5962
5963    case RID_DO:
5964      {
5965	tree expression;
5966
5967	/* Begin the do-statement.  */
5968	statement = begin_do_stmt ();
5969	/* Parse the body of the do-statement.  */
5970	parser->in_iteration_statement_p = true;
5971	cp_parser_implicitly_scoped_statement (parser);
5972	parser->in_iteration_statement_p = in_iteration_statement_p;
5973	finish_do_body (statement);
5974	/* Look for the `while' keyword.  */
5975	cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5976	/* Look for the `('.  */
5977	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5978	/* Parse the expression.  */
5979	expression = cp_parser_expression (parser);
5980	/* We're done with the do-statement.  */
5981	finish_do_stmt (expression, statement);
5982	/* Look for the `)'.  */
5983	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5984	/* Look for the `;'.  */
5985	cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5986      }
5987      break;
5988
5989    case RID_FOR:
5990      {
5991	tree condition = NULL_TREE;
5992	tree expression = NULL_TREE;
5993
5994	/* Begin the for-statement.  */
5995	statement = begin_for_stmt ();
5996	/* Look for the `('.  */
5997	cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5998	/* Parse the initialization.  */
5999	cp_parser_for_init_statement (parser);
6000	finish_for_init_stmt (statement);
6001
6002	/* If there's a condition, process it.  */
6003	if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6004	  condition = cp_parser_condition (parser);
6005	finish_for_cond (condition, statement);
6006	/* Look for the `;'.  */
6007	cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6008
6009	/* If there's an expression, process it.  */
6010	if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6011	  expression = cp_parser_expression (parser);
6012	finish_for_expr (expression, statement);
6013	/* Look for the `)'.  */
6014	cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6015
6016	/* Parse the body of the for-statement.  */
6017	parser->in_iteration_statement_p = true;
6018	cp_parser_already_scoped_statement (parser);
6019	parser->in_iteration_statement_p = in_iteration_statement_p;
6020
6021	/* We're done with the for-statement.  */
6022	finish_for_stmt (statement);
6023      }
6024      break;
6025
6026    default:
6027      cp_parser_error (parser, "expected iteration-statement");
6028      statement = error_mark_node;
6029      break;
6030    }
6031
6032  return statement;
6033}
6034
6035/* Parse a for-init-statement.
6036
6037   for-init-statement:
6038     expression-statement
6039     simple-declaration  */
6040
6041static void
6042cp_parser_for_init_statement (cp_parser* parser)
6043{
6044  /* If the next token is a `;', then we have an empty
6045     expression-statement.  Grammatically, this is also a
6046     simple-declaration, but an invalid one, because it does not
6047     declare anything.  Therefore, if we did not handle this case
6048     specially, we would issue an error message about an invalid
6049     declaration.  */
6050  if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6051    {
6052      /* We're going to speculatively look for a declaration, falling back
6053	 to an expression, if necessary.  */
6054      cp_parser_parse_tentatively (parser);
6055      /* Parse the declaration.  */
6056      cp_parser_simple_declaration (parser,
6057				    /*function_definition_allowed_p=*/false);
6058      /* If the tentative parse failed, then we shall need to look for an
6059	 expression-statement.  */
6060      if (cp_parser_parse_definitely (parser))
6061	return;
6062    }
6063
6064  cp_parser_expression_statement (parser, false);
6065}
6066
6067/* Parse a jump-statement.
6068
6069   jump-statement:
6070     break ;
6071     continue ;
6072     return expression [opt] ;
6073     goto identifier ;
6074
6075   GNU extension:
6076
6077   jump-statement:
6078     goto * expression ;
6079
6080   Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6081   GOTO_STMT.  */
6082
6083static tree
6084cp_parser_jump_statement (cp_parser* parser)
6085{
6086  tree statement = error_mark_node;
6087  cp_token *token;
6088  enum rid keyword;
6089
6090  /* Peek at the next token.  */
6091  token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6092  if (!token)
6093    return error_mark_node;
6094
6095  /* See what kind of keyword it is.  */
6096  keyword = token->keyword;
6097  switch (keyword)
6098    {
6099    case RID_BREAK:
6100      if (!parser->in_switch_statement_p
6101	  && !parser->in_iteration_statement_p)
6102	{
6103	  error ("break statement not within loop or switch");
6104	  statement = error_mark_node;
6105	}
6106      else
6107	statement = finish_break_stmt ();
6108      cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6109      break;
6110
6111    case RID_CONTINUE:
6112      if (!parser->in_iteration_statement_p)
6113	{
6114	  error ("continue statement not within a loop");
6115	  statement = error_mark_node;
6116	}
6117      else
6118	statement = finish_continue_stmt ();
6119      cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6120      break;
6121
6122    case RID_RETURN:
6123      {
6124	tree expr;
6125
6126	/* If the next token is a `;', then there is no
6127	   expression.  */
6128	if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6129	  expr = cp_parser_expression (parser);
6130	else
6131	  expr = NULL_TREE;
6132	/* Build the return-statement.  */
6133	statement = finish_return_stmt (expr);
6134	/* Look for the final `;'.  */
6135	cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6136      }
6137      break;
6138
6139    case RID_GOTO:
6140      /* Create the goto-statement.  */
6141      if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6142	{
6143	  /* Issue a warning about this use of a GNU extension.  */
6144	  if (pedantic)
6145	    pedwarn ("ISO C++ forbids computed gotos");
6146	  /* Consume the '*' token.  */
6147	  cp_lexer_consume_token (parser->lexer);
6148	  /* Parse the dependent expression.  */
6149	  finish_goto_stmt (cp_parser_expression (parser));
6150	}
6151      else
6152	finish_goto_stmt (cp_parser_identifier (parser));
6153      /* Look for the final `;'.  */
6154      cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6155      break;
6156
6157    default:
6158      cp_parser_error (parser, "expected jump-statement");
6159      break;
6160    }
6161
6162  return statement;
6163}
6164
6165/* Parse a declaration-statement.
6166
6167   declaration-statement:
6168     block-declaration  */
6169
6170static void
6171cp_parser_declaration_statement (cp_parser* parser)
6172{
6173  /* Parse the block-declaration.  */
6174  cp_parser_block_declaration (parser, /*statement_p=*/true);
6175
6176  /* Finish off the statement.  */
6177  finish_stmt ();
6178}
6179
6180/* Some dependent statements (like `if (cond) statement'), are
6181   implicitly in their own scope.  In other words, if the statement is
6182   a single statement (as opposed to a compound-statement), it is
6183   none-the-less treated as if it were enclosed in braces.  Any
6184   declarations appearing in the dependent statement are out of scope
6185   after control passes that point.  This function parses a statement,
6186   but ensures that is in its own scope, even if it is not a
6187   compound-statement.
6188
6189   Returns the new statement.  */
6190
6191static tree
6192cp_parser_implicitly_scoped_statement (cp_parser* parser)
6193{
6194  tree statement;
6195
6196  /* If the token is not a `{', then we must take special action.  */
6197  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6198    {
6199      /* Create a compound-statement.  */
6200      statement = begin_compound_stmt (/*has_no_scope=*/false);
6201      /* Parse the dependent-statement.  */
6202      cp_parser_statement (parser, false);
6203      /* Finish the dummy compound-statement.  */
6204      finish_compound_stmt (statement);
6205    }
6206  /* Otherwise, we simply parse the statement directly.  */
6207  else
6208    statement = cp_parser_compound_statement (parser, false);
6209
6210  /* Return the statement.  */
6211  return statement;
6212}
6213
6214/* For some dependent statements (like `while (cond) statement'), we
6215   have already created a scope.  Therefore, even if the dependent
6216   statement is a compound-statement, we do not want to create another
6217   scope.  */
6218
6219static void
6220cp_parser_already_scoped_statement (cp_parser* parser)
6221{
6222  /* If the token is not a `{', then we must take special action.  */
6223  if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6224    {
6225      tree statement;
6226
6227      /* Create a compound-statement.  */
6228      statement = begin_compound_stmt (/*has_no_scope=*/true);
6229      /* Parse the dependent-statement.  */
6230      cp_parser_statement (parser, false);
6231      /* Finish the dummy compound-statement.  */
6232      finish_compound_stmt (statement);
6233    }
6234  /* Otherwise, we simply parse the statement directly.  */
6235  else
6236    cp_parser_statement (parser, false);
6237}
6238
6239/* Declarations [gram.dcl.dcl] */
6240
6241/* Parse an optional declaration-sequence.
6242
6243   declaration-seq:
6244     declaration
6245     declaration-seq declaration  */
6246
6247static void
6248cp_parser_declaration_seq_opt (cp_parser* parser)
6249{
6250  while (true)
6251    {
6252      cp_token *token;
6253
6254      token = cp_lexer_peek_token (parser->lexer);
6255
6256      if (token->type == CPP_CLOSE_BRACE
6257	  || token->type == CPP_EOF)
6258	break;
6259
6260      if (token->type == CPP_SEMICOLON)
6261	{
6262	  /* A declaration consisting of a single semicolon is
6263	     invalid.  Allow it unless we're being pedantic.  */
6264	  if (pedantic && !in_system_header)
6265	    pedwarn ("extra `;'");
6266	  cp_lexer_consume_token (parser->lexer);
6267	  continue;
6268	}
6269
6270      /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6271	 parser to enter or exit implicit `extern "C"' blocks.  */
6272      while (pending_lang_change > 0)
6273	{
6274	  push_lang_context (lang_name_c);
6275	  --pending_lang_change;
6276	}
6277      while (pending_lang_change < 0)
6278	{
6279	  pop_lang_context ();
6280	  ++pending_lang_change;
6281	}
6282
6283      /* Parse the declaration itself.  */
6284      cp_parser_declaration (parser);
6285    }
6286}
6287
6288/* Parse a declaration.
6289
6290   declaration:
6291     block-declaration
6292     function-definition
6293     template-declaration
6294     explicit-instantiation
6295     explicit-specialization
6296     linkage-specification
6297     namespace-definition
6298
6299   GNU extension:
6300
6301   declaration:
6302      __extension__ declaration */
6303
6304static void
6305cp_parser_declaration (cp_parser* parser)
6306{
6307  cp_token token1;
6308  cp_token token2;
6309  int saved_pedantic;
6310
6311  /* Check for the `__extension__' keyword.  */
6312  if (cp_parser_extension_opt (parser, &saved_pedantic))
6313    {
6314      /* Parse the qualified declaration.  */
6315      cp_parser_declaration (parser);
6316      /* Restore the PEDANTIC flag.  */
6317      pedantic = saved_pedantic;
6318
6319      return;
6320    }
6321
6322  /* Try to figure out what kind of declaration is present.  */
6323  token1 = *cp_lexer_peek_token (parser->lexer);
6324  if (token1.type != CPP_EOF)
6325    token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6326
6327  /* If the next token is `extern' and the following token is a string
6328     literal, then we have a linkage specification.  */
6329  if (token1.keyword == RID_EXTERN
6330      && cp_parser_is_string_literal (&token2))
6331    cp_parser_linkage_specification (parser);
6332  /* If the next token is `template', then we have either a template
6333     declaration, an explicit instantiation, or an explicit
6334     specialization.  */
6335  else if (token1.keyword == RID_TEMPLATE)
6336    {
6337      /* `template <>' indicates a template specialization.  */
6338      if (token2.type == CPP_LESS
6339	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6340	cp_parser_explicit_specialization (parser);
6341      /* `template <' indicates a template declaration.  */
6342      else if (token2.type == CPP_LESS)
6343	cp_parser_template_declaration (parser, /*member_p=*/false);
6344      /* Anything else must be an explicit instantiation.  */
6345      else
6346	cp_parser_explicit_instantiation (parser);
6347    }
6348  /* If the next token is `export', then we have a template
6349     declaration.  */
6350  else if (token1.keyword == RID_EXPORT)
6351    cp_parser_template_declaration (parser, /*member_p=*/false);
6352  /* If the next token is `extern', 'static' or 'inline' and the one
6353     after that is `template', we have a GNU extended explicit
6354     instantiation directive.  */
6355  else if (cp_parser_allow_gnu_extensions_p (parser)
6356	   && (token1.keyword == RID_EXTERN
6357	       || token1.keyword == RID_STATIC
6358	       || token1.keyword == RID_INLINE)
6359	   && token2.keyword == RID_TEMPLATE)
6360    cp_parser_explicit_instantiation (parser);
6361  /* If the next token is `namespace', check for a named or unnamed
6362     namespace definition.  */
6363  else if (token1.keyword == RID_NAMESPACE
6364	   && (/* A named namespace definition.  */
6365	       (token2.type == CPP_NAME
6366		&& (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6367		    == CPP_OPEN_BRACE))
6368	       /* An unnamed namespace definition.  */
6369	       || token2.type == CPP_OPEN_BRACE))
6370    cp_parser_namespace_definition (parser);
6371  /* We must have either a block declaration or a function
6372     definition.  */
6373  else
6374    /* Try to parse a block-declaration, or a function-definition.  */
6375    cp_parser_block_declaration (parser, /*statement_p=*/false);
6376}
6377
6378/* Parse a block-declaration.
6379
6380   block-declaration:
6381     simple-declaration
6382     asm-definition
6383     namespace-alias-definition
6384     using-declaration
6385     using-directive
6386
6387   GNU Extension:
6388
6389   block-declaration:
6390     __extension__ block-declaration
6391     label-declaration
6392
6393   If STATEMENT_P is TRUE, then this block-declaration is occurring as
6394   part of a declaration-statement.  */
6395
6396static void
6397cp_parser_block_declaration (cp_parser *parser,
6398			     bool      statement_p)
6399{
6400  cp_token *token1;
6401  int saved_pedantic;
6402
6403  /* Check for the `__extension__' keyword.  */
6404  if (cp_parser_extension_opt (parser, &saved_pedantic))
6405    {
6406      /* Parse the qualified declaration.  */
6407      cp_parser_block_declaration (parser, statement_p);
6408      /* Restore the PEDANTIC flag.  */
6409      pedantic = saved_pedantic;
6410
6411      return;
6412    }
6413
6414  /* Peek at the next token to figure out which kind of declaration is
6415     present.  */
6416  token1 = cp_lexer_peek_token (parser->lexer);
6417
6418  /* If the next keyword is `asm', we have an asm-definition.  */
6419  if (token1->keyword == RID_ASM)
6420    {
6421      if (statement_p)
6422	cp_parser_commit_to_tentative_parse (parser);
6423      cp_parser_asm_definition (parser);
6424    }
6425  /* If the next keyword is `namespace', we have a
6426     namespace-alias-definition.  */
6427  else if (token1->keyword == RID_NAMESPACE)
6428    cp_parser_namespace_alias_definition (parser);
6429  /* If the next keyword is `using', we have either a
6430     using-declaration or a using-directive.  */
6431  else if (token1->keyword == RID_USING)
6432    {
6433      cp_token *token2;
6434
6435      if (statement_p)
6436	cp_parser_commit_to_tentative_parse (parser);
6437      /* If the token after `using' is `namespace', then we have a
6438	 using-directive.  */
6439      token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6440      if (token2->keyword == RID_NAMESPACE)
6441	cp_parser_using_directive (parser);
6442      /* Otherwise, it's a using-declaration.  */
6443      else
6444	cp_parser_using_declaration (parser);
6445    }
6446  /* If the next keyword is `__label__' we have a label declaration.  */
6447  else if (token1->keyword == RID_LABEL)
6448    {
6449      if (statement_p)
6450	cp_parser_commit_to_tentative_parse (parser);
6451      cp_parser_label_declaration (parser);
6452    }
6453  /* Anything else must be a simple-declaration.  */
6454  else
6455    cp_parser_simple_declaration (parser, !statement_p);
6456}
6457
6458/* Parse a simple-declaration.
6459
6460   simple-declaration:
6461     decl-specifier-seq [opt] init-declarator-list [opt] ;
6462
6463   init-declarator-list:
6464     init-declarator
6465     init-declarator-list , init-declarator
6466
6467   If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6468   function-definition as a simple-declaration.  */
6469
6470static void
6471cp_parser_simple_declaration (cp_parser* parser,
6472                              bool function_definition_allowed_p)
6473{
6474  tree decl_specifiers;
6475  tree attributes;
6476  int declares_class_or_enum;
6477  bool saw_declarator;
6478
6479  /* Defer access checks until we know what is being declared; the
6480     checks for names appearing in the decl-specifier-seq should be
6481     done as if we were in the scope of the thing being declared.  */
6482  push_deferring_access_checks (dk_deferred);
6483
6484  /* Parse the decl-specifier-seq.  We have to keep track of whether
6485     or not the decl-specifier-seq declares a named class or
6486     enumeration type, since that is the only case in which the
6487     init-declarator-list is allowed to be empty.
6488
6489     [dcl.dcl]
6490
6491     In a simple-declaration, the optional init-declarator-list can be
6492     omitted only when declaring a class or enumeration, that is when
6493     the decl-specifier-seq contains either a class-specifier, an
6494     elaborated-type-specifier, or an enum-specifier.  */
6495  decl_specifiers
6496    = cp_parser_decl_specifier_seq (parser,
6497				    CP_PARSER_FLAGS_OPTIONAL,
6498				    &attributes,
6499				    &declares_class_or_enum);
6500  /* We no longer need to defer access checks.  */
6501  stop_deferring_access_checks ();
6502
6503  /* In a block scope, a valid declaration must always have a
6504     decl-specifier-seq.  By not trying to parse declarators, we can
6505     resolve the declaration/expression ambiguity more quickly.  */
6506  if (!function_definition_allowed_p && !decl_specifiers)
6507    {
6508      cp_parser_error (parser, "expected declaration");
6509      goto done;
6510    }
6511
6512  /* If the next two tokens are both identifiers, the code is
6513     erroneous. The usual cause of this situation is code like:
6514
6515       T t;
6516
6517     where "T" should name a type -- but does not.  */
6518  if (cp_parser_diagnose_invalid_type_name (parser))
6519    {
6520      /* If parsing tentatively, we should commit; we really are
6521	 looking at a declaration.  */
6522      cp_parser_commit_to_tentative_parse (parser);
6523      /* Give up.  */
6524      goto done;
6525    }
6526
6527  /* Keep going until we hit the `;' at the end of the simple
6528     declaration.  */
6529  saw_declarator = false;
6530  while (cp_lexer_next_token_is_not (parser->lexer,
6531				     CPP_SEMICOLON))
6532    {
6533      cp_token *token;
6534      bool function_definition_p;
6535      tree decl;
6536
6537      saw_declarator = true;
6538      /* Parse the init-declarator.  */
6539      decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6540					function_definition_allowed_p,
6541					/*member_p=*/false,
6542					declares_class_or_enum,
6543					&function_definition_p);
6544      /* If an error occurred while parsing tentatively, exit quickly.
6545	 (That usually happens when in the body of a function; each
6546	 statement is treated as a declaration-statement until proven
6547	 otherwise.)  */
6548      if (cp_parser_error_occurred (parser))
6549	goto done;
6550      /* Handle function definitions specially.  */
6551      if (function_definition_p)
6552	{
6553	  /* If the next token is a `,', then we are probably
6554	     processing something like:
6555
6556	       void f() {}, *p;
6557
6558	     which is erroneous.  */
6559	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6560	    error ("mixing declarations and function-definitions is forbidden");
6561	  /* Otherwise, we're done with the list of declarators.  */
6562	  else
6563	    {
6564	      pop_deferring_access_checks ();
6565	      return;
6566	    }
6567	}
6568      /* The next token should be either a `,' or a `;'.  */
6569      token = cp_lexer_peek_token (parser->lexer);
6570      /* If it's a `,', there are more declarators to come.  */
6571      if (token->type == CPP_COMMA)
6572	cp_lexer_consume_token (parser->lexer);
6573      /* If it's a `;', we are done.  */
6574      else if (token->type == CPP_SEMICOLON)
6575	break;
6576      /* Anything else is an error.  */
6577      else
6578	{
6579	  cp_parser_error (parser, "expected `,' or `;'");
6580	  /* Skip tokens until we reach the end of the statement.  */
6581	  cp_parser_skip_to_end_of_statement (parser);
6582	  /* If the next token is now a `;', consume it.  */
6583	  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6584	    cp_lexer_consume_token (parser->lexer);
6585	  goto done;
6586	}
6587      /* After the first time around, a function-definition is not
6588	 allowed -- even if it was OK at first.  For example:
6589
6590           int i, f() {}
6591
6592         is not valid.  */
6593      function_definition_allowed_p = false;
6594    }
6595
6596  /* Issue an error message if no declarators are present, and the
6597     decl-specifier-seq does not itself declare a class or
6598     enumeration.  */
6599  if (!saw_declarator)
6600    {
6601      if (cp_parser_declares_only_class_p (parser))
6602	shadow_tag (decl_specifiers);
6603      /* Perform any deferred access checks.  */
6604      perform_deferred_access_checks ();
6605    }
6606
6607  /* Consume the `;'.  */
6608  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6609
6610 done:
6611  pop_deferring_access_checks ();
6612}
6613
6614/* Parse a decl-specifier-seq.
6615
6616   decl-specifier-seq:
6617     decl-specifier-seq [opt] decl-specifier
6618
6619   decl-specifier:
6620     storage-class-specifier
6621     type-specifier
6622     function-specifier
6623     friend
6624     typedef
6625
6626   GNU Extension:
6627
6628   decl-specifier:
6629     attributes
6630
6631   Returns a TREE_LIST, giving the decl-specifiers in the order they
6632   appear in the source code.  The TREE_VALUE of each node is the
6633   decl-specifier.  For a keyword (such as `auto' or `friend'), the
6634   TREE_VALUE is simply the corresponding TREE_IDENTIFIER.  For the
6635   representation of a type-specifier, see cp_parser_type_specifier.
6636
6637   If there are attributes, they will be stored in *ATTRIBUTES,
6638   represented as described above cp_parser_attributes.
6639
6640   If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6641   appears, and the entity that will be a friend is not going to be a
6642   class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6643   even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6644   friendship is granted might not be a class.
6645
6646   *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6647   flags:
6648
6649     1: one of the decl-specifiers is an elaborated-type-specifier
6650        (i.e., a type declaration)
6651     2: one of the decl-specifiers is an enum-specifier or a
6652        class-specifier (i.e., a type definition)
6653
6654   */
6655
6656static tree
6657cp_parser_decl_specifier_seq (cp_parser* parser,
6658                              cp_parser_flags flags,
6659                              tree* attributes,
6660			      int* declares_class_or_enum)
6661{
6662  tree decl_specs = NULL_TREE;
6663  bool friend_p = false;
6664  bool constructor_possible_p = !parser->in_declarator_p;
6665
6666  /* Assume no class or enumeration type is declared.  */
6667  *declares_class_or_enum = 0;
6668
6669  /* Assume there are no attributes.  */
6670  *attributes = NULL_TREE;
6671
6672  /* Keep reading specifiers until there are no more to read.  */
6673  while (true)
6674    {
6675      tree decl_spec = NULL_TREE;
6676      bool constructor_p;
6677      cp_token *token;
6678
6679      /* Peek at the next token.  */
6680      token = cp_lexer_peek_token (parser->lexer);
6681      /* Handle attributes.  */
6682      if (token->keyword == RID_ATTRIBUTE)
6683	{
6684	  /* Parse the attributes.  */
6685	  decl_spec = cp_parser_attributes_opt (parser);
6686	  /* Add them to the list.  */
6687	  *attributes = chainon (*attributes, decl_spec);
6688	  continue;
6689	}
6690      /* If the next token is an appropriate keyword, we can simply
6691	 add it to the list.  */
6692      switch (token->keyword)
6693	{
6694	case RID_FRIEND:
6695	  /* decl-specifier:
6696	       friend  */
6697	  if (friend_p)
6698	    error ("duplicate `friend'");
6699	  else
6700	    friend_p = true;
6701	  /* The representation of the specifier is simply the
6702	     appropriate TREE_IDENTIFIER node.  */
6703	  decl_spec = token->value;
6704	  /* Consume the token.  */
6705	  cp_lexer_consume_token (parser->lexer);
6706	  break;
6707
6708	  /* function-specifier:
6709	       inline
6710	       virtual
6711	       explicit  */
6712	case RID_INLINE:
6713	case RID_VIRTUAL:
6714	case RID_EXPLICIT:
6715	  decl_spec = cp_parser_function_specifier_opt (parser);
6716	  break;
6717
6718	  /* decl-specifier:
6719	       typedef  */
6720	case RID_TYPEDEF:
6721	  /* The representation of the specifier is simply the
6722	     appropriate TREE_IDENTIFIER node.  */
6723	  decl_spec = token->value;
6724	  /* Consume the token.  */
6725	  cp_lexer_consume_token (parser->lexer);
6726	  /* A constructor declarator cannot appear in a typedef.  */
6727	  constructor_possible_p = false;
6728	  /* The "typedef" keyword can only occur in a declaration; we
6729	     may as well commit at this point.  */
6730	  cp_parser_commit_to_tentative_parse (parser);
6731	  break;
6732
6733	  /* storage-class-specifier:
6734	       auto
6735	       register
6736	       static
6737	       extern
6738	       mutable
6739
6740             GNU Extension:
6741	       thread  */
6742	case RID_AUTO:
6743	case RID_REGISTER:
6744	case RID_STATIC:
6745	case RID_EXTERN:
6746	case RID_MUTABLE:
6747	case RID_THREAD:
6748	  decl_spec = cp_parser_storage_class_specifier_opt (parser);
6749	  break;
6750
6751	default:
6752	  break;
6753	}
6754
6755      /* Constructors are a special case.  The `S' in `S()' is not a
6756	 decl-specifier; it is the beginning of the declarator.  */
6757      constructor_p = (!decl_spec
6758		       && constructor_possible_p
6759		       && cp_parser_constructor_declarator_p (parser,
6760							      friend_p));
6761
6762      /* If we don't have a DECL_SPEC yet, then we must be looking at
6763	 a type-specifier.  */
6764      if (!decl_spec && !constructor_p)
6765	{
6766	  int decl_spec_declares_class_or_enum;
6767	  bool is_cv_qualifier;
6768
6769	  decl_spec
6770	    = cp_parser_type_specifier (parser, flags,
6771					friend_p,
6772					/*is_declaration=*/true,
6773					&decl_spec_declares_class_or_enum,
6774					&is_cv_qualifier);
6775
6776	  *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6777
6778	  /* If this type-specifier referenced a user-defined type
6779	     (a typedef, class-name, etc.), then we can't allow any
6780	     more such type-specifiers henceforth.
6781
6782	     [dcl.spec]
6783
6784	     The longest sequence of decl-specifiers that could
6785	     possibly be a type name is taken as the
6786	     decl-specifier-seq of a declaration.  The sequence shall
6787	     be self-consistent as described below.
6788
6789	     [dcl.type]
6790
6791	     As a general rule, at most one type-specifier is allowed
6792	     in the complete decl-specifier-seq of a declaration.  The
6793	     only exceptions are the following:
6794
6795	     -- const or volatile can be combined with any other
6796		type-specifier.
6797
6798	     -- signed or unsigned can be combined with char, long,
6799		short, or int.
6800
6801	     -- ..
6802
6803	     Example:
6804
6805	       typedef char* Pc;
6806	       void g (const int Pc);
6807
6808	     Here, Pc is *not* part of the decl-specifier seq; it's
6809	     the declarator.  Therefore, once we see a type-specifier
6810	     (other than a cv-qualifier), we forbid any additional
6811	     user-defined types.  We *do* still allow things like `int
6812	     int' to be considered a decl-specifier-seq, and issue the
6813	     error message later.  */
6814	  if (decl_spec && !is_cv_qualifier)
6815	    flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6816	  /* A constructor declarator cannot follow a type-specifier.  */
6817	  if (decl_spec)
6818	    constructor_possible_p = false;
6819	}
6820
6821      /* If we still do not have a DECL_SPEC, then there are no more
6822	 decl-specifiers.  */
6823      if (!decl_spec)
6824	{
6825	  /* Issue an error message, unless the entire construct was
6826             optional.  */
6827	  if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6828	    {
6829	      cp_parser_error (parser, "expected decl specifier");
6830	      return error_mark_node;
6831	    }
6832
6833	  break;
6834	}
6835
6836      /* Add the DECL_SPEC to the list of specifiers.  */
6837      if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6838	decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6839
6840      /* After we see one decl-specifier, further decl-specifiers are
6841	 always optional.  */
6842      flags |= CP_PARSER_FLAGS_OPTIONAL;
6843    }
6844
6845  /* Don't allow a friend specifier with a class definition.  */
6846  if (friend_p && (*declares_class_or_enum & 2))
6847    error ("class definition may not be declared a friend");
6848
6849  /* We have built up the DECL_SPECS in reverse order.  Return them in
6850     the correct order.  */
6851  return nreverse (decl_specs);
6852}
6853
6854/* Parse an (optional) storage-class-specifier.
6855
6856   storage-class-specifier:
6857     auto
6858     register
6859     static
6860     extern
6861     mutable
6862
6863   GNU Extension:
6864
6865   storage-class-specifier:
6866     thread
6867
6868   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6869
6870static tree
6871cp_parser_storage_class_specifier_opt (cp_parser* parser)
6872{
6873  switch (cp_lexer_peek_token (parser->lexer)->keyword)
6874    {
6875    case RID_AUTO:
6876    case RID_REGISTER:
6877    case RID_STATIC:
6878    case RID_EXTERN:
6879    case RID_MUTABLE:
6880    case RID_THREAD:
6881      /* Consume the token.  */
6882      return cp_lexer_consume_token (parser->lexer)->value;
6883
6884    default:
6885      return NULL_TREE;
6886    }
6887}
6888
6889/* Parse an (optional) function-specifier.
6890
6891   function-specifier:
6892     inline
6893     virtual
6894     explicit
6895
6896   Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6897
6898static tree
6899cp_parser_function_specifier_opt (cp_parser* parser)
6900{
6901  switch (cp_lexer_peek_token (parser->lexer)->keyword)
6902    {
6903    case RID_INLINE:
6904    case RID_VIRTUAL:
6905    case RID_EXPLICIT:
6906      /* Consume the token.  */
6907      return cp_lexer_consume_token (parser->lexer)->value;
6908
6909    default:
6910      return NULL_TREE;
6911    }
6912}
6913
6914/* Parse a linkage-specification.
6915
6916   linkage-specification:
6917     extern string-literal { declaration-seq [opt] }
6918     extern string-literal declaration  */
6919
6920static void
6921cp_parser_linkage_specification (cp_parser* parser)
6922{
6923  cp_token *token;
6924  tree linkage;
6925
6926  /* Look for the `extern' keyword.  */
6927  cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6928
6929  /* Peek at the next token.  */
6930  token = cp_lexer_peek_token (parser->lexer);
6931  /* If it's not a string-literal, then there's a problem.  */
6932  if (!cp_parser_is_string_literal (token))
6933    {
6934      cp_parser_error (parser, "expected language-name");
6935      return;
6936    }
6937  /* Consume the token.  */
6938  cp_lexer_consume_token (parser->lexer);
6939
6940  /* Transform the literal into an identifier.  If the literal is a
6941     wide-character string, or contains embedded NULs, then we can't
6942     handle it as the user wants.  */
6943  if (token->type == CPP_WSTRING
6944      || (strlen (TREE_STRING_POINTER (token->value))
6945	  != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6946    {
6947      cp_parser_error (parser, "invalid linkage-specification");
6948      /* Assume C++ linkage.  */
6949      linkage = get_identifier ("c++");
6950    }
6951  /* If it's a simple string constant, things are easier.  */
6952  else
6953    linkage = get_identifier (TREE_STRING_POINTER (token->value));
6954
6955  /* We're now using the new linkage.  */
6956  push_lang_context (linkage);
6957
6958  /* If the next token is a `{', then we're using the first
6959     production.  */
6960  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6961    {
6962      /* Consume the `{' token.  */
6963      cp_lexer_consume_token (parser->lexer);
6964      /* Parse the declarations.  */
6965      cp_parser_declaration_seq_opt (parser);
6966      /* Look for the closing `}'.  */
6967      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6968    }
6969  /* Otherwise, there's just one declaration.  */
6970  else
6971    {
6972      bool saved_in_unbraced_linkage_specification_p;
6973
6974      saved_in_unbraced_linkage_specification_p
6975	= parser->in_unbraced_linkage_specification_p;
6976      parser->in_unbraced_linkage_specification_p = true;
6977      have_extern_spec = true;
6978      cp_parser_declaration (parser);
6979      have_extern_spec = false;
6980      parser->in_unbraced_linkage_specification_p
6981	= saved_in_unbraced_linkage_specification_p;
6982    }
6983
6984  /* We're done with the linkage-specification.  */
6985  pop_lang_context ();
6986}
6987
6988/* Special member functions [gram.special] */
6989
6990/* Parse a conversion-function-id.
6991
6992   conversion-function-id:
6993     operator conversion-type-id
6994
6995   Returns an IDENTIFIER_NODE representing the operator.  */
6996
6997static tree
6998cp_parser_conversion_function_id (cp_parser* parser)
6999{
7000  tree type;
7001  tree saved_scope;
7002  tree saved_qualifying_scope;
7003  tree saved_object_scope;
7004  bool pop_p = false;
7005
7006  /* Look for the `operator' token.  */
7007  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7008    return error_mark_node;
7009  /* When we parse the conversion-type-id, the current scope will be
7010     reset.  However, we need that information in able to look up the
7011     conversion function later, so we save it here.  */
7012  saved_scope = parser->scope;
7013  saved_qualifying_scope = parser->qualifying_scope;
7014  saved_object_scope = parser->object_scope;
7015  /* We must enter the scope of the class so that the names of
7016     entities declared within the class are available in the
7017     conversion-type-id.  For example, consider:
7018
7019       struct S {
7020         typedef int I;
7021	 operator I();
7022       };
7023
7024       S::operator I() { ... }
7025
7026     In order to see that `I' is a type-name in the definition, we
7027     must be in the scope of `S'.  */
7028  if (saved_scope)
7029    pop_p = push_scope (saved_scope);
7030  /* Parse the conversion-type-id.  */
7031  type = cp_parser_conversion_type_id (parser);
7032  /* Leave the scope of the class, if any.  */
7033  if (pop_p)
7034    pop_scope (saved_scope);
7035  /* Restore the saved scope.  */
7036  parser->scope = saved_scope;
7037  parser->qualifying_scope = saved_qualifying_scope;
7038  parser->object_scope = saved_object_scope;
7039  /* If the TYPE is invalid, indicate failure.  */
7040  if (type == error_mark_node)
7041    return error_mark_node;
7042  return mangle_conv_op_name_for_type (type);
7043}
7044
7045/* Parse a conversion-type-id:
7046
7047   conversion-type-id:
7048     type-specifier-seq conversion-declarator [opt]
7049
7050   Returns the TYPE specified.  */
7051
7052static tree
7053cp_parser_conversion_type_id (cp_parser* parser)
7054{
7055  tree attributes;
7056  tree type_specifiers;
7057  tree declarator;
7058
7059  /* Parse the attributes.  */
7060  attributes = cp_parser_attributes_opt (parser);
7061  /* Parse the type-specifiers.  */
7062  type_specifiers = cp_parser_type_specifier_seq (parser);
7063  /* If that didn't work, stop.  */
7064  if (type_specifiers == error_mark_node)
7065    return error_mark_node;
7066  /* Parse the conversion-declarator.  */
7067  declarator = cp_parser_conversion_declarator_opt (parser);
7068
7069  return grokdeclarator (declarator, type_specifiers, TYPENAME,
7070			 /*initialized=*/0, &attributes);
7071}
7072
7073/* Parse an (optional) conversion-declarator.
7074
7075   conversion-declarator:
7076     ptr-operator conversion-declarator [opt]
7077
7078   Returns a representation of the declarator.  See
7079   cp_parser_declarator for details.  */
7080
7081static tree
7082cp_parser_conversion_declarator_opt (cp_parser* parser)
7083{
7084  enum tree_code code;
7085  tree class_type;
7086  tree cv_qualifier_seq;
7087
7088  /* We don't know if there's a ptr-operator next, or not.  */
7089  cp_parser_parse_tentatively (parser);
7090  /* Try the ptr-operator.  */
7091  code = cp_parser_ptr_operator (parser, &class_type,
7092				 &cv_qualifier_seq);
7093  /* If it worked, look for more conversion-declarators.  */
7094  if (cp_parser_parse_definitely (parser))
7095    {
7096     tree declarator;
7097
7098     /* Parse another optional declarator.  */
7099     declarator = cp_parser_conversion_declarator_opt (parser);
7100
7101     /* Create the representation of the declarator.  */
7102     if (code == INDIRECT_REF)
7103       declarator = make_pointer_declarator (cv_qualifier_seq,
7104					     declarator);
7105     else
7106       declarator =  make_reference_declarator (cv_qualifier_seq,
7107						declarator);
7108
7109     /* Handle the pointer-to-member case.  */
7110     if (class_type)
7111       declarator = build_nt (SCOPE_REF, class_type, declarator);
7112
7113     return declarator;
7114   }
7115
7116  return NULL_TREE;
7117}
7118
7119/* Parse an (optional) ctor-initializer.
7120
7121   ctor-initializer:
7122     : mem-initializer-list
7123
7124   Returns TRUE iff the ctor-initializer was actually present.  */
7125
7126static bool
7127cp_parser_ctor_initializer_opt (cp_parser* parser)
7128{
7129  /* If the next token is not a `:', then there is no
7130     ctor-initializer.  */
7131  if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7132    {
7133      /* Do default initialization of any bases and members.  */
7134      if (DECL_CONSTRUCTOR_P (current_function_decl))
7135	finish_mem_initializers (NULL_TREE);
7136
7137      return false;
7138    }
7139
7140  /* Consume the `:' token.  */
7141  cp_lexer_consume_token (parser->lexer);
7142  /* And the mem-initializer-list.  */
7143  cp_parser_mem_initializer_list (parser);
7144
7145  return true;
7146}
7147
7148/* Parse a mem-initializer-list.
7149
7150   mem-initializer-list:
7151     mem-initializer
7152     mem-initializer , mem-initializer-list  */
7153
7154static void
7155cp_parser_mem_initializer_list (cp_parser* parser)
7156{
7157  tree mem_initializer_list = NULL_TREE;
7158
7159  /* Let the semantic analysis code know that we are starting the
7160     mem-initializer-list.  */
7161  if (!DECL_CONSTRUCTOR_P (current_function_decl))
7162    error ("only constructors take base initializers");
7163
7164  /* Loop through the list.  */
7165  while (true)
7166    {
7167      tree mem_initializer;
7168
7169      /* Parse the mem-initializer.  */
7170      mem_initializer = cp_parser_mem_initializer (parser);
7171      /* Add it to the list, unless it was erroneous.  */
7172      if (mem_initializer)
7173	{
7174	  TREE_CHAIN (mem_initializer) = mem_initializer_list;
7175	  mem_initializer_list = mem_initializer;
7176	}
7177      /* If the next token is not a `,', we're done.  */
7178      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7179	break;
7180      /* Consume the `,' token.  */
7181      cp_lexer_consume_token (parser->lexer);
7182    }
7183
7184  /* Perform semantic analysis.  */
7185  if (DECL_CONSTRUCTOR_P (current_function_decl))
7186    finish_mem_initializers (mem_initializer_list);
7187}
7188
7189/* Parse a mem-initializer.
7190
7191   mem-initializer:
7192     mem-initializer-id ( expression-list [opt] )
7193
7194   GNU extension:
7195
7196   mem-initializer:
7197     ( expression-list [opt] )
7198
7199   Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7200   class) or FIELD_DECL (for a non-static data member) to initialize;
7201   the TREE_VALUE is the expression-list.  */
7202
7203static tree
7204cp_parser_mem_initializer (cp_parser* parser)
7205{
7206  tree mem_initializer_id;
7207  tree expression_list;
7208  tree member;
7209
7210  /* Find out what is being initialized.  */
7211  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7212    {
7213      pedwarn ("anachronistic old-style base class initializer");
7214      mem_initializer_id = NULL_TREE;
7215    }
7216  else
7217    mem_initializer_id = cp_parser_mem_initializer_id (parser);
7218  member = expand_member_init (mem_initializer_id);
7219  if (member && !DECL_P (member))
7220    in_base_initializer = 1;
7221
7222  expression_list
7223    = cp_parser_parenthesized_expression_list (parser, false,
7224					       /*non_constant_p=*/NULL);
7225  if (!expression_list)
7226    expression_list = void_type_node;
7227
7228  in_base_initializer = 0;
7229
7230  return member ? build_tree_list (member, expression_list) : NULL_TREE;
7231}
7232
7233/* Parse a mem-initializer-id.
7234
7235   mem-initializer-id:
7236     :: [opt] nested-name-specifier [opt] class-name
7237     identifier
7238
7239   Returns a TYPE indicating the class to be initializer for the first
7240   production.  Returns an IDENTIFIER_NODE indicating the data member
7241   to be initialized for the second production.  */
7242
7243static tree
7244cp_parser_mem_initializer_id (cp_parser* parser)
7245{
7246  bool global_scope_p;
7247  bool nested_name_specifier_p;
7248  bool template_p = false;
7249  tree id;
7250
7251  /* `typename' is not allowed in this context ([temp.res]).  */
7252  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7253    {
7254      error ("keyword `typename' not allowed in this context (a qualified "
7255	     "member initializer is implicitly a type)");
7256      cp_lexer_consume_token (parser->lexer);
7257    }
7258  /* Look for the optional `::' operator.  */
7259  global_scope_p
7260    = (cp_parser_global_scope_opt (parser,
7261				   /*current_scope_valid_p=*/false)
7262       != NULL_TREE);
7263  /* Look for the optional nested-name-specifier.  The simplest way to
7264     implement:
7265
7266       [temp.res]
7267
7268       The keyword `typename' is not permitted in a base-specifier or
7269       mem-initializer; in these contexts a qualified name that
7270       depends on a template-parameter is implicitly assumed to be a
7271       type name.
7272
7273     is to assume that we have seen the `typename' keyword at this
7274     point.  */
7275  nested_name_specifier_p
7276    = (cp_parser_nested_name_specifier_opt (parser,
7277					    /*typename_keyword_p=*/true,
7278					    /*check_dependency_p=*/true,
7279					    /*type_p=*/true,
7280					    /*is_declaration=*/true)
7281       != NULL_TREE);
7282  if (nested_name_specifier_p)
7283    template_p = cp_parser_optional_template_keyword (parser);
7284  /* If there is a `::' operator or a nested-name-specifier, then we
7285     are definitely looking for a class-name.  */
7286  if (global_scope_p || nested_name_specifier_p)
7287    return cp_parser_class_name (parser,
7288				 /*typename_keyword_p=*/true,
7289				 /*template_keyword_p=*/template_p,
7290				 /*type_p=*/false,
7291				 /*check_dependency_p=*/true,
7292				 /*class_head_p=*/false,
7293				 /*is_declaration=*/true);
7294  /* Otherwise, we could also be looking for an ordinary identifier.  */
7295  cp_parser_parse_tentatively (parser);
7296  /* Try a class-name.  */
7297  id = cp_parser_class_name (parser,
7298			     /*typename_keyword_p=*/true,
7299			     /*template_keyword_p=*/false,
7300			     /*type_p=*/false,
7301			     /*check_dependency_p=*/true,
7302			     /*class_head_p=*/false,
7303			     /*is_declaration=*/true);
7304  /* If we found one, we're done.  */
7305  if (cp_parser_parse_definitely (parser))
7306    return id;
7307  /* Otherwise, look for an ordinary identifier.  */
7308  return cp_parser_identifier (parser);
7309}
7310
7311/* Overloading [gram.over] */
7312
7313/* Parse an operator-function-id.
7314
7315   operator-function-id:
7316     operator operator
7317
7318   Returns an IDENTIFIER_NODE for the operator which is a
7319   human-readable spelling of the identifier, e.g., `operator +'.  */
7320
7321static tree
7322cp_parser_operator_function_id (cp_parser* parser)
7323{
7324  /* Look for the `operator' keyword.  */
7325  if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7326    return error_mark_node;
7327  /* And then the name of the operator itself.  */
7328  return cp_parser_operator (parser);
7329}
7330
7331/* Parse an operator.
7332
7333   operator:
7334     new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7335     += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7336     || ++ -- , ->* -> () []
7337
7338   GNU Extensions:
7339
7340   operator:
7341     <? >? <?= >?=
7342
7343   Returns an IDENTIFIER_NODE for the operator which is a
7344   human-readable spelling of the identifier, e.g., `operator +'.  */
7345
7346static tree
7347cp_parser_operator (cp_parser* parser)
7348{
7349  tree id = NULL_TREE;
7350  cp_token *token;
7351
7352  /* Peek at the next token.  */
7353  token = cp_lexer_peek_token (parser->lexer);
7354  /* Figure out which operator we have.  */
7355  switch (token->type)
7356    {
7357    case CPP_KEYWORD:
7358      {
7359	enum tree_code op;
7360
7361	/* The keyword should be either `new' or `delete'.  */
7362	if (token->keyword == RID_NEW)
7363	  op = NEW_EXPR;
7364	else if (token->keyword == RID_DELETE)
7365	  op = DELETE_EXPR;
7366	else
7367	  break;
7368
7369	/* Consume the `new' or `delete' token.  */
7370	cp_lexer_consume_token (parser->lexer);
7371
7372	/* Peek at the next token.  */
7373	token = cp_lexer_peek_token (parser->lexer);
7374	/* If it's a `[' token then this is the array variant of the
7375	   operator.  */
7376	if (token->type == CPP_OPEN_SQUARE)
7377	  {
7378	    /* Consume the `[' token.  */
7379	    cp_lexer_consume_token (parser->lexer);
7380	    /* Look for the `]' token.  */
7381	    cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7382	    id = ansi_opname (op == NEW_EXPR
7383			      ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7384	  }
7385	/* Otherwise, we have the non-array variant.  */
7386	else
7387	  id = ansi_opname (op);
7388
7389	return id;
7390      }
7391
7392    case CPP_PLUS:
7393      id = ansi_opname (PLUS_EXPR);
7394      break;
7395
7396    case CPP_MINUS:
7397      id = ansi_opname (MINUS_EXPR);
7398      break;
7399
7400    case CPP_MULT:
7401      id = ansi_opname (MULT_EXPR);
7402      break;
7403
7404    case CPP_DIV:
7405      id = ansi_opname (TRUNC_DIV_EXPR);
7406      break;
7407
7408    case CPP_MOD:
7409      id = ansi_opname (TRUNC_MOD_EXPR);
7410      break;
7411
7412    case CPP_XOR:
7413      id = ansi_opname (BIT_XOR_EXPR);
7414      break;
7415
7416    case CPP_AND:
7417      id = ansi_opname (BIT_AND_EXPR);
7418      break;
7419
7420    case CPP_OR:
7421      id = ansi_opname (BIT_IOR_EXPR);
7422      break;
7423
7424    case CPP_COMPL:
7425      id = ansi_opname (BIT_NOT_EXPR);
7426      break;
7427
7428    case CPP_NOT:
7429      id = ansi_opname (TRUTH_NOT_EXPR);
7430      break;
7431
7432    case CPP_EQ:
7433      id = ansi_assopname (NOP_EXPR);
7434      break;
7435
7436    case CPP_LESS:
7437      id = ansi_opname (LT_EXPR);
7438      break;
7439
7440    case CPP_GREATER:
7441      id = ansi_opname (GT_EXPR);
7442      break;
7443
7444    case CPP_PLUS_EQ:
7445      id = ansi_assopname (PLUS_EXPR);
7446      break;
7447
7448    case CPP_MINUS_EQ:
7449      id = ansi_assopname (MINUS_EXPR);
7450      break;
7451
7452    case CPP_MULT_EQ:
7453      id = ansi_assopname (MULT_EXPR);
7454      break;
7455
7456    case CPP_DIV_EQ:
7457      id = ansi_assopname (TRUNC_DIV_EXPR);
7458      break;
7459
7460    case CPP_MOD_EQ:
7461      id = ansi_assopname (TRUNC_MOD_EXPR);
7462      break;
7463
7464    case CPP_XOR_EQ:
7465      id = ansi_assopname (BIT_XOR_EXPR);
7466      break;
7467
7468    case CPP_AND_EQ:
7469      id = ansi_assopname (BIT_AND_EXPR);
7470      break;
7471
7472    case CPP_OR_EQ:
7473      id = ansi_assopname (BIT_IOR_EXPR);
7474      break;
7475
7476    case CPP_LSHIFT:
7477      id = ansi_opname (LSHIFT_EXPR);
7478      break;
7479
7480    case CPP_RSHIFT:
7481      id = ansi_opname (RSHIFT_EXPR);
7482      break;
7483
7484    case CPP_LSHIFT_EQ:
7485      id = ansi_assopname (LSHIFT_EXPR);
7486      break;
7487
7488    case CPP_RSHIFT_EQ:
7489      id = ansi_assopname (RSHIFT_EXPR);
7490      break;
7491
7492    case CPP_EQ_EQ:
7493      id = ansi_opname (EQ_EXPR);
7494      break;
7495
7496    case CPP_NOT_EQ:
7497      id = ansi_opname (NE_EXPR);
7498      break;
7499
7500    case CPP_LESS_EQ:
7501      id = ansi_opname (LE_EXPR);
7502      break;
7503
7504    case CPP_GREATER_EQ:
7505      id = ansi_opname (GE_EXPR);
7506      break;
7507
7508    case CPP_AND_AND:
7509      id = ansi_opname (TRUTH_ANDIF_EXPR);
7510      break;
7511
7512    case CPP_OR_OR:
7513      id = ansi_opname (TRUTH_ORIF_EXPR);
7514      break;
7515
7516    case CPP_PLUS_PLUS:
7517      id = ansi_opname (POSTINCREMENT_EXPR);
7518      break;
7519
7520    case CPP_MINUS_MINUS:
7521      id = ansi_opname (PREDECREMENT_EXPR);
7522      break;
7523
7524    case CPP_COMMA:
7525      id = ansi_opname (COMPOUND_EXPR);
7526      break;
7527
7528    case CPP_DEREF_STAR:
7529      id = ansi_opname (MEMBER_REF);
7530      break;
7531
7532    case CPP_DEREF:
7533      id = ansi_opname (COMPONENT_REF);
7534      break;
7535
7536    case CPP_OPEN_PAREN:
7537      /* Consume the `('.  */
7538      cp_lexer_consume_token (parser->lexer);
7539      /* Look for the matching `)'.  */
7540      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7541      return ansi_opname (CALL_EXPR);
7542
7543    case CPP_OPEN_SQUARE:
7544      /* Consume the `['.  */
7545      cp_lexer_consume_token (parser->lexer);
7546      /* Look for the matching `]'.  */
7547      cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7548      return ansi_opname (ARRAY_REF);
7549
7550      /* Extensions.  */
7551    case CPP_MIN:
7552      id = ansi_opname (MIN_EXPR);
7553      break;
7554
7555    case CPP_MAX:
7556      id = ansi_opname (MAX_EXPR);
7557      break;
7558
7559    case CPP_MIN_EQ:
7560      id = ansi_assopname (MIN_EXPR);
7561      break;
7562
7563    case CPP_MAX_EQ:
7564      id = ansi_assopname (MAX_EXPR);
7565      break;
7566
7567    default:
7568      /* Anything else is an error.  */
7569      break;
7570    }
7571
7572  /* If we have selected an identifier, we need to consume the
7573     operator token.  */
7574  if (id)
7575    cp_lexer_consume_token (parser->lexer);
7576  /* Otherwise, no valid operator name was present.  */
7577  else
7578    {
7579      cp_parser_error (parser, "expected operator");
7580      id = error_mark_node;
7581    }
7582
7583  return id;
7584}
7585
7586/* Parse a template-declaration.
7587
7588   template-declaration:
7589     export [opt] template < template-parameter-list > declaration
7590
7591   If MEMBER_P is TRUE, this template-declaration occurs within a
7592   class-specifier.
7593
7594   The grammar rule given by the standard isn't correct.  What
7595   is really meant is:
7596
7597   template-declaration:
7598     export [opt] template-parameter-list-seq
7599       decl-specifier-seq [opt] init-declarator [opt] ;
7600     export [opt] template-parameter-list-seq
7601       function-definition
7602
7603   template-parameter-list-seq:
7604     template-parameter-list-seq [opt]
7605     template < template-parameter-list >  */
7606
7607static void
7608cp_parser_template_declaration (cp_parser* parser, bool member_p)
7609{
7610  /* Check for `export'.  */
7611  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7612    {
7613      /* Consume the `export' token.  */
7614      cp_lexer_consume_token (parser->lexer);
7615      /* Warn that we do not support `export'.  */
7616      warning ("keyword `export' not implemented, and will be ignored");
7617    }
7618
7619  cp_parser_template_declaration_after_export (parser, member_p);
7620}
7621
7622/* Parse a template-parameter-list.
7623
7624   template-parameter-list:
7625     template-parameter
7626     template-parameter-list , template-parameter
7627
7628   Returns a TREE_LIST.  Each node represents a template parameter.
7629   The nodes are connected via their TREE_CHAINs.  */
7630
7631static tree
7632cp_parser_template_parameter_list (cp_parser* parser)
7633{
7634  tree parameter_list = NULL_TREE;
7635
7636  while (true)
7637    {
7638      tree parameter;
7639      cp_token *token;
7640
7641      /* Parse the template-parameter.  */
7642      parameter = cp_parser_template_parameter (parser);
7643      /* Add it to the list.  */
7644      parameter_list = process_template_parm (parameter_list,
7645					      parameter);
7646
7647      /* Peek at the next token.  */
7648      token = cp_lexer_peek_token (parser->lexer);
7649      /* If it's not a `,', we're done.  */
7650      if (token->type != CPP_COMMA)
7651	break;
7652      /* Otherwise, consume the `,' token.  */
7653      cp_lexer_consume_token (parser->lexer);
7654    }
7655
7656  return parameter_list;
7657}
7658
7659/* Parse a template-parameter.
7660
7661   template-parameter:
7662     type-parameter
7663     parameter-declaration
7664
7665   Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7666   TREE_PURPOSE is the default value, if any.  */
7667
7668static tree
7669cp_parser_template_parameter (cp_parser* parser)
7670{
7671  cp_token *token;
7672
7673  /* Peek at the next token.  */
7674  token = cp_lexer_peek_token (parser->lexer);
7675  /* If it is `class' or `template', we have a type-parameter.  */
7676  if (token->keyword == RID_TEMPLATE)
7677    return cp_parser_type_parameter (parser);
7678  /* If it is `class' or `typename' we do not know yet whether it is a
7679     type parameter or a non-type parameter.  Consider:
7680
7681       template <typename T, typename T::X X> ...
7682
7683     or:
7684
7685       template <class C, class D*> ...
7686
7687     Here, the first parameter is a type parameter, and the second is
7688     a non-type parameter.  We can tell by looking at the token after
7689     the identifier -- if it is a `,', `=', or `>' then we have a type
7690     parameter.  */
7691  if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7692    {
7693      /* Peek at the token after `class' or `typename'.  */
7694      token = cp_lexer_peek_nth_token (parser->lexer, 2);
7695      /* If it's an identifier, skip it.  */
7696      if (token->type == CPP_NAME)
7697	token = cp_lexer_peek_nth_token (parser->lexer, 3);
7698      /* Now, see if the token looks like the end of a template
7699	 parameter.  */
7700      if (token->type == CPP_COMMA
7701	  || token->type == CPP_EQ
7702	  || token->type == CPP_GREATER)
7703	return cp_parser_type_parameter (parser);
7704    }
7705
7706  /* Otherwise, it is a non-type parameter.
7707
7708     [temp.param]
7709
7710     When parsing a default template-argument for a non-type
7711     template-parameter, the first non-nested `>' is taken as the end
7712     of the template parameter-list rather than a greater-than
7713     operator.  */
7714  return
7715    cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7716				     /*parenthesized_p=*/NULL);
7717}
7718
7719/* Parse a type-parameter.
7720
7721   type-parameter:
7722     class identifier [opt]
7723     class identifier [opt] = type-id
7724     typename identifier [opt]
7725     typename identifier [opt] = type-id
7726     template < template-parameter-list > class identifier [opt]
7727     template < template-parameter-list > class identifier [opt]
7728       = id-expression
7729
7730   Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7731   TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7732   the declaration of the parameter.  */
7733
7734static tree
7735cp_parser_type_parameter (cp_parser* parser)
7736{
7737  cp_token *token;
7738  tree parameter;
7739
7740  /* Look for a keyword to tell us what kind of parameter this is.  */
7741  token = cp_parser_require (parser, CPP_KEYWORD,
7742			     "`class', `typename', or `template'");
7743  if (!token)
7744    return error_mark_node;
7745
7746  switch (token->keyword)
7747    {
7748    case RID_CLASS:
7749    case RID_TYPENAME:
7750      {
7751	tree identifier;
7752	tree default_argument;
7753
7754	/* If the next token is an identifier, then it names the
7755           parameter.  */
7756	if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7757	  identifier = cp_parser_identifier (parser);
7758	else
7759	  identifier = NULL_TREE;
7760
7761	/* Create the parameter.  */
7762	parameter = finish_template_type_parm (class_type_node, identifier);
7763
7764	/* If the next token is an `=', we have a default argument.  */
7765	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7766	  {
7767	    /* Consume the `=' token.  */
7768	    cp_lexer_consume_token (parser->lexer);
7769	    /* Parse the default-argument.  */
7770	    default_argument = cp_parser_type_id (parser);
7771	  }
7772	else
7773	  default_argument = NULL_TREE;
7774
7775	/* Create the combined representation of the parameter and the
7776	   default argument.  */
7777	parameter = build_tree_list (default_argument, parameter);
7778      }
7779      break;
7780
7781    case RID_TEMPLATE:
7782      {
7783	tree parameter_list;
7784	tree identifier;
7785	tree default_argument;
7786
7787	/* Look for the `<'.  */
7788	cp_parser_require (parser, CPP_LESS, "`<'");
7789	/* Parse the template-parameter-list.  */
7790	begin_template_parm_list ();
7791	parameter_list
7792	  = cp_parser_template_parameter_list (parser);
7793	parameter_list = end_template_parm_list (parameter_list);
7794	/* Look for the `>'.  */
7795	cp_parser_require (parser, CPP_GREATER, "`>'");
7796	/* Look for the `class' keyword.  */
7797	cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7798	/* If the next token is an `=', then there is a
7799	   default-argument.  If the next token is a `>', we are at
7800	   the end of the parameter-list.  If the next token is a `,',
7801	   then we are at the end of this parameter.  */
7802	if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7803	    && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7804	    && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7805	  identifier = cp_parser_identifier (parser);
7806	else
7807	  identifier = NULL_TREE;
7808	/* Create the template parameter.  */
7809	parameter = finish_template_template_parm (class_type_node,
7810						   identifier);
7811
7812	/* If the next token is an `=', then there is a
7813	   default-argument.  */
7814	if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7815	  {
7816	    bool is_template;
7817
7818	    /* Consume the `='.  */
7819	    cp_lexer_consume_token (parser->lexer);
7820	    /* Parse the id-expression.  */
7821	    default_argument
7822	      = cp_parser_id_expression (parser,
7823					 /*template_keyword_p=*/false,
7824					 /*check_dependency_p=*/true,
7825					 /*template_p=*/&is_template,
7826					 /*declarator_p=*/false);
7827	    if (TREE_CODE (default_argument) == TYPE_DECL)
7828	      /* If the id-expression was a template-id that refers to
7829		 a template-class, we already have the declaration here,
7830		 so no further lookup is needed.  */
7831		 ;
7832	    else
7833	      /* Look up the name.  */
7834	      default_argument
7835		= cp_parser_lookup_name (parser, default_argument,
7836					/*is_type=*/false,
7837					/*is_template=*/is_template,
7838					/*is_namespace=*/false,
7839					/*check_dependency=*/true);
7840	    /* See if the default argument is valid.  */
7841	    default_argument
7842	      = check_template_template_default_arg (default_argument);
7843	  }
7844	else
7845	  default_argument = NULL_TREE;
7846
7847	/* Create the combined representation of the parameter and the
7848	   default argument.  */
7849	parameter =  build_tree_list (default_argument, parameter);
7850      }
7851      break;
7852
7853    default:
7854      /* Anything else is an error.  */
7855      cp_parser_error (parser,
7856		       "expected `class', `typename', or `template'");
7857      parameter = error_mark_node;
7858    }
7859
7860  return parameter;
7861}
7862
7863/* Parse a template-id.
7864
7865   template-id:
7866     template-name < template-argument-list [opt] >
7867
7868   If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7869   `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
7870   returned.  Otherwise, if the template-name names a function, or set
7871   of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
7872   names a class, returns a TYPE_DECL for the specialization.
7873
7874   If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7875   uninstantiated templates.  */
7876
7877static tree
7878cp_parser_template_id (cp_parser *parser,
7879		       bool template_keyword_p,
7880		       bool check_dependency_p,
7881		       bool is_declaration)
7882{
7883  tree template;
7884  tree arguments;
7885  tree template_id;
7886  ptrdiff_t start_of_id;
7887  tree access_check = NULL_TREE;
7888  cp_token *next_token, *next_token_2;
7889  bool is_identifier;
7890
7891  /* If the next token corresponds to a template-id, there is no need
7892     to reparse it.  */
7893  next_token = cp_lexer_peek_token (parser->lexer);
7894  if (next_token->type == CPP_TEMPLATE_ID)
7895    {
7896      tree value;
7897      tree check;
7898
7899      /* Get the stored value.  */
7900      value = cp_lexer_consume_token (parser->lexer)->value;
7901      /* Perform any access checks that were deferred.  */
7902      for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7903	perform_or_defer_access_check (TREE_PURPOSE (check),
7904				       TREE_VALUE (check));
7905      /* Return the stored value.  */
7906      return TREE_VALUE (value);
7907    }
7908
7909  /* Avoid performing name lookup if there is no possibility of
7910     finding a template-id.  */
7911  if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7912      || (next_token->type == CPP_NAME
7913	  && !cp_parser_nth_token_starts_template_argument_list_p
7914	       (parser, 2)))
7915    {
7916      cp_parser_error (parser, "expected template-id");
7917      return error_mark_node;
7918    }
7919
7920  /* Remember where the template-id starts.  */
7921  if (cp_parser_parsing_tentatively (parser)
7922      && !cp_parser_committed_to_tentative_parse (parser))
7923    {
7924      next_token = cp_lexer_peek_token (parser->lexer);
7925      start_of_id = cp_lexer_token_difference (parser->lexer,
7926					       parser->lexer->first_token,
7927					       next_token);
7928    }
7929  else
7930    start_of_id = -1;
7931
7932  push_deferring_access_checks (dk_deferred);
7933
7934  /* Parse the template-name.  */
7935  is_identifier = false;
7936  template = cp_parser_template_name (parser, template_keyword_p,
7937				      check_dependency_p,
7938				      is_declaration,
7939				      &is_identifier);
7940  if (template == error_mark_node || is_identifier)
7941    {
7942      pop_deferring_access_checks ();
7943      return template;
7944    }
7945
7946  /* If we find the sequence `[:' after a template-name, it's probably
7947     a digraph-typo for `< ::'. Substitute the tokens and check if we can
7948     parse correctly the argument list.  */
7949  next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7950  next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7951  if (next_token->type == CPP_OPEN_SQUARE
7952      && next_token->flags & DIGRAPH
7953      && next_token_2->type == CPP_COLON
7954      && !(next_token_2->flags & PREV_WHITE))
7955    {
7956      cp_parser_parse_tentatively (parser);
7957      /* Change `:' into `::'.  */
7958      next_token_2->type = CPP_SCOPE;
7959      /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7960         CPP_LESS.  */
7961      cp_lexer_consume_token (parser->lexer);
7962      /* Parse the arguments.  */
7963      arguments = cp_parser_enclosed_template_argument_list (parser);
7964      if (!cp_parser_parse_definitely (parser))
7965	{
7966	  /* If we couldn't parse an argument list, then we revert our changes
7967	     and return simply an error. Maybe this is not a template-id
7968	     after all.  */
7969	  next_token_2->type = CPP_COLON;
7970	  cp_parser_error (parser, "expected `<'");
7971	  pop_deferring_access_checks ();
7972	  return error_mark_node;
7973	}
7974      /* Otherwise, emit an error about the invalid digraph, but continue
7975         parsing because we got our argument list.  */
7976      pedwarn ("`<::' cannot begin a template-argument list");
7977      inform ("`<:' is an alternate spelling for `['. Insert whitespace "
7978	      "between `<' and `::'");
7979      if (!flag_permissive)
7980	{
7981	  static bool hint;
7982	  if (!hint)
7983	    {
7984	      inform ("(if you use `-fpermissive' G++ will accept your code)");
7985	      hint = true;
7986	    }
7987	}
7988    }
7989  else
7990    {
7991      /* Look for the `<' that starts the template-argument-list.  */
7992      if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7993	{
7994	  pop_deferring_access_checks ();
7995	  return error_mark_node;
7996	}
7997      /* Parse the arguments.  */
7998      arguments = cp_parser_enclosed_template_argument_list (parser);
7999    }
8000
8001  /* Build a representation of the specialization.  */
8002  if (TREE_CODE (template) == IDENTIFIER_NODE)
8003    template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8004  else if (DECL_CLASS_TEMPLATE_P (template)
8005	   || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8006    template_id
8007      = finish_template_type (template, arguments,
8008			      cp_lexer_next_token_is (parser->lexer,
8009						      CPP_SCOPE));
8010  else
8011    {
8012      /* If it's not a class-template or a template-template, it should be
8013	 a function-template.  */
8014      my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8015			   || TREE_CODE (template) == OVERLOAD
8016			   || BASELINK_P (template)),
8017			  20010716);
8018
8019      template_id = lookup_template_function (template, arguments);
8020    }
8021
8022  /* Retrieve any deferred checks.  Do not pop this access checks yet
8023     so the memory will not be reclaimed during token replacing below.  */
8024  access_check = get_deferred_access_checks ();
8025
8026  /* If parsing tentatively, replace the sequence of tokens that makes
8027     up the template-id with a CPP_TEMPLATE_ID token.  That way,
8028     should we re-parse the token stream, we will not have to repeat
8029     the effort required to do the parse, nor will we issue duplicate
8030     error messages about problems during instantiation of the
8031     template.  */
8032  if (start_of_id >= 0)
8033    {
8034      cp_token *token;
8035
8036      /* Find the token that corresponds to the start of the
8037	 template-id.  */
8038      token = cp_lexer_advance_token (parser->lexer,
8039				      parser->lexer->first_token,
8040				      start_of_id);
8041
8042      /* Reset the contents of the START_OF_ID token.  */
8043      token->type = CPP_TEMPLATE_ID;
8044      token->value = build_tree_list (access_check, template_id);
8045      token->keyword = RID_MAX;
8046      /* Purge all subsequent tokens.  */
8047      cp_lexer_purge_tokens_after (parser->lexer, token);
8048
8049      /* ??? Can we actually assume that, if template_id ==
8050	 error_mark_node, we will have issued a diagnostic to the
8051	 user, as opposed to simply marking the tentative parse as
8052	 failed?  */
8053      if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8054	error ("parse error in template argument list");
8055    }
8056
8057  pop_deferring_access_checks ();
8058  return template_id;
8059}
8060
8061/* Parse a template-name.
8062
8063   template-name:
8064     identifier
8065
8066   The standard should actually say:
8067
8068   template-name:
8069     identifier
8070     operator-function-id
8071
8072   A defect report has been filed about this issue.
8073
8074   A conversion-function-id cannot be a template name because they cannot
8075   be part of a template-id. In fact, looking at this code:
8076
8077   a.operator K<int>()
8078
8079   the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8080   It is impossible to call a templated conversion-function-id with an
8081   explicit argument list, since the only allowed template parameter is
8082   the type to which it is converting.
8083
8084   If TEMPLATE_KEYWORD_P is true, then we have just seen the
8085   `template' keyword, in a construction like:
8086
8087     T::template f<3>()
8088
8089   In that case `f' is taken to be a template-name, even though there
8090   is no way of knowing for sure.
8091
8092   Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8093   name refers to a set of overloaded functions, at least one of which
8094   is a template, or an IDENTIFIER_NODE with the name of the template,
8095   if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8096   names are looked up inside uninstantiated templates.  */
8097
8098static tree
8099cp_parser_template_name (cp_parser* parser,
8100                         bool template_keyword_p,
8101                         bool check_dependency_p,
8102			 bool is_declaration,
8103			 bool *is_identifier)
8104{
8105  tree identifier;
8106  tree decl;
8107  tree fns;
8108
8109  /* If the next token is `operator', then we have either an
8110     operator-function-id or a conversion-function-id.  */
8111  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8112    {
8113      /* We don't know whether we're looking at an
8114	 operator-function-id or a conversion-function-id.  */
8115      cp_parser_parse_tentatively (parser);
8116      /* Try an operator-function-id.  */
8117      identifier = cp_parser_operator_function_id (parser);
8118      /* If that didn't work, try a conversion-function-id.  */
8119      if (!cp_parser_parse_definitely (parser))
8120        {
8121	  cp_parser_error (parser, "expected template-name");
8122	  return error_mark_node;
8123        }
8124    }
8125  /* Look for the identifier.  */
8126  else
8127    identifier = cp_parser_identifier (parser);
8128
8129  /* If we didn't find an identifier, we don't have a template-id.  */
8130  if (identifier == error_mark_node)
8131    return error_mark_node;
8132
8133  /* If the name immediately followed the `template' keyword, then it
8134     is a template-name.  However, if the next token is not `<', then
8135     we do not treat it as a template-name, since it is not being used
8136     as part of a template-id.  This enables us to handle constructs
8137     like:
8138
8139       template <typename T> struct S { S(); };
8140       template <typename T> S<T>::S();
8141
8142     correctly.  We would treat `S' as a template -- if it were `S<T>'
8143     -- but we do not if there is no `<'.  */
8144
8145  if (processing_template_decl
8146      && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8147    {
8148      /* In a declaration, in a dependent context, we pretend that the
8149	 "template" keyword was present in order to improve error
8150	 recovery.  For example, given:
8151
8152	   template <typename T> void f(T::X<int>);
8153
8154	 we want to treat "X<int>" as a template-id.  */
8155      if (is_declaration
8156	  && !template_keyword_p
8157	  && parser->scope && TYPE_P (parser->scope)
8158	  && check_dependency_p
8159	  && dependent_type_p (parser->scope)
8160	  /* Do not do this for dtors (or ctors), since they never
8161	     need the template keyword before their name.  */
8162	  && !constructor_name_p (identifier, parser->scope))
8163	{
8164	  ptrdiff_t start;
8165	  cp_token* token;
8166	  /* Explain what went wrong.  */
8167	  error ("non-template `%D' used as template", identifier);
8168	  inform ("use `%T::template %D' to indicate that it is a template",
8169		  parser->scope, identifier);
8170	  /* If parsing tentatively, find the location of the "<"
8171	     token.  */
8172	  if (cp_parser_parsing_tentatively (parser)
8173	      && !cp_parser_committed_to_tentative_parse (parser))
8174	    {
8175	      cp_parser_simulate_error (parser);
8176	      token = cp_lexer_peek_token (parser->lexer);
8177	      token = cp_lexer_prev_token (parser->lexer, token);
8178	      start = cp_lexer_token_difference (parser->lexer,
8179						 parser->lexer->first_token,
8180						 token);
8181	    }
8182	  else
8183	    start = -1;
8184	  /* Parse the template arguments so that we can issue error
8185	     messages about them.  */
8186	  cp_lexer_consume_token (parser->lexer);
8187	  cp_parser_enclosed_template_argument_list (parser);
8188	  /* Skip tokens until we find a good place from which to
8189	     continue parsing.  */
8190	  cp_parser_skip_to_closing_parenthesis (parser,
8191						 /*recovering=*/true,
8192						 /*or_comma=*/true,
8193						 /*consume_paren=*/false);
8194	  /* If parsing tentatively, permanently remove the
8195	     template argument list.  That will prevent duplicate
8196	     error messages from being issued about the missing
8197	     "template" keyword.  */
8198	  if (start >= 0)
8199	    {
8200	      token = cp_lexer_advance_token (parser->lexer,
8201					      parser->lexer->first_token,
8202					      start);
8203	      cp_lexer_purge_tokens_after (parser->lexer, token);
8204	    }
8205	  if (is_identifier)
8206	    *is_identifier = true;
8207	  return identifier;
8208	}
8209
8210      /* If the "template" keyword is present, then there is generally
8211	 no point in doing name-lookup, so we just return IDENTIFIER.
8212	 But, if the qualifying scope is non-dependent then we can
8213	 (and must) do name-lookup normally.  */
8214      if (template_keyword_p
8215	  && (!parser->scope
8216	      || (TYPE_P (parser->scope)
8217		  && dependent_type_p (parser->scope))))
8218	return identifier;
8219    }
8220
8221  /* Look up the name.  */
8222  decl = cp_parser_lookup_name (parser, identifier,
8223				/*is_type=*/false,
8224				/*is_template=*/false,
8225				/*is_namespace=*/false,
8226				check_dependency_p);
8227  decl = maybe_get_template_decl_from_type_decl (decl);
8228
8229  /* If DECL is a template, then the name was a template-name.  */
8230  if (TREE_CODE (decl) == TEMPLATE_DECL)
8231    ;
8232  else
8233    {
8234      tree fn = NULL_TREE;
8235
8236      /* The standard does not explicitly indicate whether a name that
8237	 names a set of overloaded declarations, some of which are
8238	 templates, is a template-name.  However, such a name should
8239	 be a template-name; otherwise, there is no way to form a
8240	 template-id for the overloaded templates.  */
8241      fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8242      if (TREE_CODE (fns) == OVERLOAD)
8243	for (fn = fns; fn; fn = OVL_NEXT (fn))
8244	  if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8245	    break;
8246
8247      if (!fn)
8248	{
8249	  /* Otherwise, the name does not name a template.  */
8250	  cp_parser_error (parser, "expected template-name");
8251	  return error_mark_node;
8252	}
8253    }
8254
8255  /* If DECL is dependent, and refers to a function, then just return
8256     its name; we will look it up again during template instantiation.  */
8257  if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8258    {
8259      tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8260      if (TYPE_P (scope) && dependent_type_p (scope))
8261	return identifier;
8262    }
8263
8264  return decl;
8265}
8266
8267/* Parse a template-argument-list.
8268
8269   template-argument-list:
8270     template-argument
8271     template-argument-list , template-argument
8272
8273   Returns a TREE_VEC containing the arguments.  */
8274
8275static tree
8276cp_parser_template_argument_list (cp_parser* parser)
8277{
8278  tree fixed_args[10];
8279  unsigned n_args = 0;
8280  unsigned alloced = 10;
8281  tree *arg_ary = fixed_args;
8282  tree vec;
8283  bool saved_in_template_argument_list_p;
8284
8285  saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8286  parser->in_template_argument_list_p = true;
8287  do
8288    {
8289      tree argument;
8290
8291      if (n_args)
8292	/* Consume the comma.  */
8293	cp_lexer_consume_token (parser->lexer);
8294
8295      /* Parse the template-argument.  */
8296      argument = cp_parser_template_argument (parser);
8297      if (n_args == alloced)
8298	{
8299	  alloced *= 2;
8300
8301	  if (arg_ary == fixed_args)
8302	    {
8303	      arg_ary = xmalloc (sizeof (tree) * alloced);
8304	      memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8305	    }
8306	  else
8307	    arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8308	}
8309      arg_ary[n_args++] = argument;
8310    }
8311  while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8312
8313  vec = make_tree_vec (n_args);
8314
8315  while (n_args--)
8316    TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8317
8318  if (arg_ary != fixed_args)
8319    free (arg_ary);
8320  parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8321  return vec;
8322}
8323
8324/* Parse a template-argument.
8325
8326   template-argument:
8327     assignment-expression
8328     type-id
8329     id-expression
8330
8331   The representation is that of an assignment-expression, type-id, or
8332   id-expression -- except that the qualified id-expression is
8333   evaluated, so that the value returned is either a DECL or an
8334   OVERLOAD.
8335
8336   Although the standard says "assignment-expression", it forbids
8337   throw-expressions or assignments in the template argument.
8338   Therefore, we use "conditional-expression" instead.  */
8339
8340static tree
8341cp_parser_template_argument (cp_parser* parser)
8342{
8343  tree argument;
8344  bool template_p;
8345  bool address_p;
8346  bool maybe_type_id = false;
8347  cp_token *token;
8348  cp_id_kind idk;
8349  tree qualifying_class;
8350
8351  /* There's really no way to know what we're looking at, so we just
8352     try each alternative in order.
8353
8354       [temp.arg]
8355
8356       In a template-argument, an ambiguity between a type-id and an
8357       expression is resolved to a type-id, regardless of the form of
8358       the corresponding template-parameter.
8359
8360     Therefore, we try a type-id first.  */
8361  cp_parser_parse_tentatively (parser);
8362  argument = cp_parser_type_id (parser);
8363  /* If there was no error parsing the type-id but the next token is a '>>',
8364     we probably found a typo for '> >'. But there are type-id which are
8365     also valid expressions. For instance:
8366
8367     struct X { int operator >> (int); };
8368     template <int V> struct Foo {};
8369     Foo<X () >> 5> r;
8370
8371     Here 'X()' is a valid type-id of a function type, but the user just
8372     wanted to write the expression "X() >> 5". Thus, we remember that we
8373     found a valid type-id, but we still try to parse the argument as an
8374     expression to see what happens.  */
8375  if (!cp_parser_error_occurred (parser)
8376      && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8377    {
8378      maybe_type_id = true;
8379      cp_parser_abort_tentative_parse (parser);
8380    }
8381  else
8382    {
8383      /* If the next token isn't a `,' or a `>', then this argument wasn't
8384      really finished. This means that the argument is not a valid
8385      type-id.  */
8386      if (!cp_parser_next_token_ends_template_argument_p (parser))
8387	cp_parser_error (parser, "expected template-argument");
8388      /* If that worked, we're done.  */
8389      if (cp_parser_parse_definitely (parser))
8390	return argument;
8391    }
8392  /* We're still not sure what the argument will be.  */
8393  cp_parser_parse_tentatively (parser);
8394  /* Try a template.  */
8395  argument = cp_parser_id_expression (parser,
8396				      /*template_keyword_p=*/false,
8397				      /*check_dependency_p=*/true,
8398				      &template_p,
8399				      /*declarator_p=*/false);
8400  /* If the next token isn't a `,' or a `>', then this argument wasn't
8401     really finished.  */
8402  if (!cp_parser_next_token_ends_template_argument_p (parser))
8403    cp_parser_error (parser, "expected template-argument");
8404  if (!cp_parser_error_occurred (parser))
8405    {
8406      /* Figure out what is being referred to.  If the id-expression
8407	 was for a class template specialization, then we will have a
8408	 TYPE_DECL at this point.  There is no need to do name lookup
8409	 at this point in that case.  */
8410      if (TREE_CODE (argument) != TYPE_DECL)
8411	argument = cp_parser_lookup_name (parser, argument,
8412					  /*is_type=*/false,
8413					  /*is_template=*/template_p,
8414					  /*is_namespace=*/false,
8415					  /*check_dependency=*/true);
8416      if (TREE_CODE (argument) != TEMPLATE_DECL
8417	  && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8418	cp_parser_error (parser, "expected template-name");
8419    }
8420  if (cp_parser_parse_definitely (parser))
8421    return argument;
8422  /* It must be a non-type argument.  There permitted cases are given
8423     in [temp.arg.nontype]:
8424
8425     -- an integral constant-expression of integral or enumeration
8426        type; or
8427
8428     -- the name of a non-type template-parameter; or
8429
8430     -- the name of an object or function with external linkage...
8431
8432     -- the address of an object or function with external linkage...
8433
8434     -- a pointer to member...  */
8435  /* Look for a non-type template parameter.  */
8436  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8437    {
8438      cp_parser_parse_tentatively (parser);
8439      argument = cp_parser_primary_expression (parser,
8440					       &idk,
8441					       &qualifying_class);
8442      if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8443	  || !cp_parser_next_token_ends_template_argument_p (parser))
8444	cp_parser_simulate_error (parser);
8445      if (cp_parser_parse_definitely (parser))
8446	return argument;
8447    }
8448  /* If the next token is "&", the argument must be the address of an
8449     object or function with external linkage.  */
8450  address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8451  if (address_p)
8452    cp_lexer_consume_token (parser->lexer);
8453  /* See if we might have an id-expression.  */
8454  token = cp_lexer_peek_token (parser->lexer);
8455  if (token->type == CPP_NAME
8456      || token->keyword == RID_OPERATOR
8457      || token->type == CPP_SCOPE
8458      || token->type == CPP_TEMPLATE_ID
8459      || token->type == CPP_NESTED_NAME_SPECIFIER)
8460    {
8461      cp_parser_parse_tentatively (parser);
8462      argument = cp_parser_primary_expression (parser,
8463					       &idk,
8464					       &qualifying_class);
8465      if (cp_parser_error_occurred (parser)
8466	  || !cp_parser_next_token_ends_template_argument_p (parser))
8467	cp_parser_abort_tentative_parse (parser);
8468      else
8469	{
8470	  if (qualifying_class)
8471	    argument = finish_qualified_id_expr (qualifying_class,
8472						 argument,
8473						 /*done=*/true,
8474						 address_p);
8475	  if (TREE_CODE (argument) == VAR_DECL)
8476	    {
8477	      /* A variable without external linkage might still be a
8478		 valid constant-expression, so no error is issued here
8479		 if the external-linkage check fails.  */
8480	      if (!DECL_EXTERNAL_LINKAGE_P (argument))
8481		cp_parser_simulate_error (parser);
8482	    }
8483	  else if (is_overloaded_fn (argument))
8484	    /* All overloaded functions are allowed; if the external
8485	       linkage test does not pass, an error will be issued
8486	       later.  */
8487	    ;
8488	  else if (address_p
8489		   && (TREE_CODE (argument) == OFFSET_REF
8490		       || TREE_CODE (argument) == SCOPE_REF))
8491	    /* A pointer-to-member.  */
8492	    ;
8493	  else
8494	    cp_parser_simulate_error (parser);
8495
8496	  if (cp_parser_parse_definitely (parser))
8497	    {
8498	      if (address_p)
8499		argument = build_x_unary_op (ADDR_EXPR, argument);
8500	      return argument;
8501	    }
8502	}
8503    }
8504  /* If the argument started with "&", there are no other valid
8505     alternatives at this point.  */
8506  if (address_p)
8507    {
8508      cp_parser_error (parser, "invalid non-type template argument");
8509      return error_mark_node;
8510    }
8511  /* If the argument wasn't successfully parsed as a type-id followed
8512     by '>>', the argument can only be a constant expression now.
8513     Otherwise, we try parsing the constant-expression tentatively,
8514     because the argument could really be a type-id.  */
8515  if (maybe_type_id)
8516    cp_parser_parse_tentatively (parser);
8517  argument = cp_parser_constant_expression (parser,
8518					    /*allow_non_constant_p=*/false,
8519					    /*non_constant_p=*/NULL);
8520  argument = fold_non_dependent_expr (argument);
8521  if (!maybe_type_id)
8522    return argument;
8523  if (!cp_parser_next_token_ends_template_argument_p (parser))
8524    cp_parser_error (parser, "expected template-argument");
8525  if (cp_parser_parse_definitely (parser))
8526    return argument;
8527  /* We did our best to parse the argument as a non type-id, but that
8528     was the only alternative that matched (albeit with a '>' after
8529     it). We can assume it's just a typo from the user, and a
8530     diagnostic will then be issued.  */
8531  return cp_parser_type_id (parser);
8532}
8533
8534/* Parse an explicit-instantiation.
8535
8536   explicit-instantiation:
8537     template declaration
8538
8539   Although the standard says `declaration', what it really means is:
8540
8541   explicit-instantiation:
8542     template decl-specifier-seq [opt] declarator [opt] ;
8543
8544   Things like `template int S<int>::i = 5, int S<double>::j;' are not
8545   supposed to be allowed.  A defect report has been filed about this
8546   issue.
8547
8548   GNU Extension:
8549
8550   explicit-instantiation:
8551     storage-class-specifier template
8552       decl-specifier-seq [opt] declarator [opt] ;
8553     function-specifier template
8554       decl-specifier-seq [opt] declarator [opt] ;  */
8555
8556static void
8557cp_parser_explicit_instantiation (cp_parser* parser)
8558{
8559  int declares_class_or_enum;
8560  tree decl_specifiers;
8561  tree attributes;
8562  tree extension_specifier = NULL_TREE;
8563
8564  /* Look for an (optional) storage-class-specifier or
8565     function-specifier.  */
8566  if (cp_parser_allow_gnu_extensions_p (parser))
8567    {
8568      extension_specifier
8569	= cp_parser_storage_class_specifier_opt (parser);
8570      if (!extension_specifier)
8571	extension_specifier = cp_parser_function_specifier_opt (parser);
8572    }
8573
8574  /* Look for the `template' keyword.  */
8575  cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8576  /* Let the front end know that we are processing an explicit
8577     instantiation.  */
8578  begin_explicit_instantiation ();
8579  /* [temp.explicit] says that we are supposed to ignore access
8580     control while processing explicit instantiation directives.  */
8581  push_deferring_access_checks (dk_no_check);
8582  /* Parse a decl-specifier-seq.  */
8583  decl_specifiers
8584    = cp_parser_decl_specifier_seq (parser,
8585				    CP_PARSER_FLAGS_OPTIONAL,
8586				    &attributes,
8587				    &declares_class_or_enum);
8588  /* If there was exactly one decl-specifier, and it declared a class,
8589     and there's no declarator, then we have an explicit type
8590     instantiation.  */
8591  if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8592    {
8593      tree type;
8594
8595      type = check_tag_decl (decl_specifiers);
8596      /* Turn access control back on for names used during
8597	 template instantiation.  */
8598      pop_deferring_access_checks ();
8599      if (type)
8600	do_type_instantiation (type, extension_specifier, /*complain=*/1);
8601    }
8602  else
8603    {
8604      tree declarator;
8605      tree decl;
8606
8607      /* Parse the declarator.  */
8608      declarator
8609	= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8610				/*ctor_dtor_or_conv_p=*/NULL,
8611				/*parenthesized_p=*/NULL,
8612				/*member_p=*/false);
8613      cp_parser_check_for_definition_in_return_type (declarator,
8614						     declares_class_or_enum);
8615      if (declarator != error_mark_node)
8616	{
8617	  decl = grokdeclarator (declarator, decl_specifiers,
8618				 NORMAL, 0, NULL);
8619	  /* Turn access control back on for names used during
8620	     template instantiation.  */
8621	  pop_deferring_access_checks ();
8622	  /* Do the explicit instantiation.  */
8623	  do_decl_instantiation (decl, extension_specifier);
8624	}
8625      else
8626	{
8627	  pop_deferring_access_checks ();
8628	  /* Skip the body of the explicit instantiation.  */
8629	  cp_parser_skip_to_end_of_statement (parser);
8630	}
8631    }
8632  /* We're done with the instantiation.  */
8633  end_explicit_instantiation ();
8634
8635  cp_parser_consume_semicolon_at_end_of_statement (parser);
8636}
8637
8638/* Parse an explicit-specialization.
8639
8640   explicit-specialization:
8641     template < > declaration
8642
8643   Although the standard says `declaration', what it really means is:
8644
8645   explicit-specialization:
8646     template <> decl-specifier [opt] init-declarator [opt] ;
8647     template <> function-definition
8648     template <> explicit-specialization
8649     template <> template-declaration  */
8650
8651static void
8652cp_parser_explicit_specialization (cp_parser* parser)
8653{
8654  /* Look for the `template' keyword.  */
8655  cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8656  /* Look for the `<'.  */
8657  cp_parser_require (parser, CPP_LESS, "`<'");
8658  /* Look for the `>'.  */
8659  cp_parser_require (parser, CPP_GREATER, "`>'");
8660  /* We have processed another parameter list.  */
8661  ++parser->num_template_parameter_lists;
8662  /* Let the front end know that we are beginning a specialization.  */
8663  begin_specialization ();
8664
8665  /* If the next keyword is `template', we need to figure out whether
8666     or not we're looking a template-declaration.  */
8667  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8668    {
8669      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8670	  && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8671	cp_parser_template_declaration_after_export (parser,
8672						     /*member_p=*/false);
8673      else
8674	cp_parser_explicit_specialization (parser);
8675    }
8676  else
8677    /* Parse the dependent declaration.  */
8678    cp_parser_single_declaration (parser,
8679				  /*member_p=*/false,
8680				  /*friend_p=*/NULL);
8681
8682  /* We're done with the specialization.  */
8683  end_specialization ();
8684  /* We're done with this parameter list.  */
8685  --parser->num_template_parameter_lists;
8686}
8687
8688/* Parse a type-specifier.
8689
8690   type-specifier:
8691     simple-type-specifier
8692     class-specifier
8693     enum-specifier
8694     elaborated-type-specifier
8695     cv-qualifier
8696
8697   GNU Extension:
8698
8699   type-specifier:
8700     __complex__
8701
8702   Returns a representation of the type-specifier.  If the
8703   type-specifier is a keyword (like `int' or `const', or
8704   `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8705   For a class-specifier, enum-specifier, or elaborated-type-specifier
8706   a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8707
8708   If IS_FRIEND is TRUE then this type-specifier is being declared a
8709   `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8710   appearing in a decl-specifier-seq.
8711
8712   If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8713   class-specifier, enum-specifier, or elaborated-type-specifier, then
8714   *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
8715   if a type is declared; 2 if it is defined.  Otherwise, it is set to
8716   zero.
8717
8718   If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8719   cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8720   is set to FALSE.  */
8721
8722static tree
8723cp_parser_type_specifier (cp_parser* parser,
8724			  cp_parser_flags flags,
8725			  bool is_friend,
8726			  bool is_declaration,
8727			  int* declares_class_or_enum,
8728			  bool* is_cv_qualifier)
8729{
8730  tree type_spec = NULL_TREE;
8731  cp_token *token;
8732  enum rid keyword;
8733
8734  /* Assume this type-specifier does not declare a new type.  */
8735  if (declares_class_or_enum)
8736    *declares_class_or_enum = 0;
8737  /* And that it does not specify a cv-qualifier.  */
8738  if (is_cv_qualifier)
8739    *is_cv_qualifier = false;
8740  /* Peek at the next token.  */
8741  token = cp_lexer_peek_token (parser->lexer);
8742
8743  /* If we're looking at a keyword, we can use that to guide the
8744     production we choose.  */
8745  keyword = token->keyword;
8746  switch (keyword)
8747    {
8748    case RID_ENUM:
8749      /* 'enum' [identifier] '{' introduces an enum-specifier;
8750	 'enum' <anything else> introduces an elaborated-type-specifier.  */
8751      if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
8752	  || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
8753	      && cp_lexer_peek_nth_token (parser->lexer, 3)->type
8754	         == CPP_OPEN_BRACE))
8755	{
8756	  if (parser->num_template_parameter_lists)
8757	    {
8758	      error ("template declaration of `enum'");
8759	      cp_parser_skip_to_end_of_block_or_statement (parser);
8760	      type_spec = error_mark_node;
8761	    }
8762	  else
8763	    type_spec = cp_parser_enum_specifier (parser);
8764
8765	  if (declares_class_or_enum)
8766	    *declares_class_or_enum = 2;
8767	  return type_spec;
8768	}
8769      else
8770	goto elaborated_type_specifier;
8771
8772      /* Any of these indicate either a class-specifier, or an
8773	 elaborated-type-specifier.  */
8774    case RID_CLASS:
8775    case RID_STRUCT:
8776    case RID_UNION:
8777      /* Parse tentatively so that we can back up if we don't find a
8778	 class-specifier or enum-specifier.  */
8779      cp_parser_parse_tentatively (parser);
8780      /* Look for the class-specifier.  */
8781      type_spec = cp_parser_class_specifier (parser);
8782      /* If that worked, we're done.  */
8783      if (cp_parser_parse_definitely (parser))
8784	{
8785	  if (declares_class_or_enum)
8786	    *declares_class_or_enum = 2;
8787	  return type_spec;
8788	}
8789
8790      /* Fall through.  */
8791
8792    case RID_TYPENAME:
8793    elaborated_type_specifier:
8794      /* Look for an elaborated-type-specifier.  */
8795      type_spec = cp_parser_elaborated_type_specifier (parser,
8796						       is_friend,
8797						       is_declaration);
8798      /* We're declaring a class or enum -- unless we're using
8799	 `typename'.  */
8800      if (declares_class_or_enum && keyword != RID_TYPENAME)
8801	*declares_class_or_enum = 1;
8802      return type_spec;
8803
8804    case RID_CONST:
8805    case RID_VOLATILE:
8806    case RID_RESTRICT:
8807      type_spec = cp_parser_cv_qualifier_opt (parser);
8808      /* Even though we call a routine that looks for an optional
8809	 qualifier, we know that there should be one.  */
8810      my_friendly_assert (type_spec != NULL, 20000328);
8811      /* This type-specifier was a cv-qualified.  */
8812      if (is_cv_qualifier)
8813	*is_cv_qualifier = true;
8814
8815      return type_spec;
8816
8817    case RID_COMPLEX:
8818      /* The `__complex__' keyword is a GNU extension.  */
8819      return cp_lexer_consume_token (parser->lexer)->value;
8820
8821    default:
8822      break;
8823    }
8824
8825  /* If we do not already have a type-specifier, assume we are looking
8826     at a simple-type-specifier.  */
8827  type_spec = cp_parser_simple_type_specifier (parser, flags,
8828					       /*identifier_p=*/true);
8829
8830  /* If we didn't find a type-specifier, and a type-specifier was not
8831     optional in this context, issue an error message.  */
8832  if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8833    {
8834      cp_parser_error (parser, "expected type specifier");
8835      return error_mark_node;
8836    }
8837
8838  return type_spec;
8839}
8840
8841/* Parse a simple-type-specifier.
8842
8843   simple-type-specifier:
8844     :: [opt] nested-name-specifier [opt] type-name
8845     :: [opt] nested-name-specifier template template-id
8846     char
8847     wchar_t
8848     bool
8849     short
8850     int
8851     long
8852     signed
8853     unsigned
8854     float
8855     double
8856     void
8857
8858   GNU Extension:
8859
8860   simple-type-specifier:
8861     __typeof__ unary-expression
8862     __typeof__ ( type-id )
8863
8864   For the various keywords, the value returned is simply the
8865   TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8866   For the first two productions, and if IDENTIFIER_P is false, the
8867   value returned is the indicated TYPE_DECL.  */
8868
8869static tree
8870cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8871				 bool identifier_p)
8872{
8873  tree type = NULL_TREE;
8874  cp_token *token;
8875
8876  /* Peek at the next token.  */
8877  token = cp_lexer_peek_token (parser->lexer);
8878
8879  /* If we're looking at a keyword, things are easy.  */
8880  switch (token->keyword)
8881    {
8882    case RID_CHAR:
8883      type = char_type_node;
8884      break;
8885    case RID_WCHAR:
8886      type = wchar_type_node;
8887      break;
8888    case RID_BOOL:
8889      type = boolean_type_node;
8890      break;
8891    case RID_SHORT:
8892      type = short_integer_type_node;
8893      break;
8894    case RID_INT:
8895      type = integer_type_node;
8896      break;
8897    case RID_LONG:
8898      type = long_integer_type_node;
8899      break;
8900    case RID_SIGNED:
8901      type = integer_type_node;
8902      break;
8903    case RID_UNSIGNED:
8904      type = unsigned_type_node;
8905      break;
8906    case RID_FLOAT:
8907      type = float_type_node;
8908      break;
8909    case RID_DOUBLE:
8910      type = double_type_node;
8911      break;
8912    case RID_VOID:
8913      type = void_type_node;
8914      break;
8915
8916    case RID_TYPEOF:
8917      {
8918	tree operand;
8919
8920	/* Consume the `typeof' token.  */
8921	cp_lexer_consume_token (parser->lexer);
8922	/* Parse the operand to `typeof'.  */
8923	operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8924	/* If it is not already a TYPE, take its type.  */
8925	if (!TYPE_P (operand))
8926	  operand = finish_typeof (operand);
8927
8928	return operand;
8929      }
8930
8931    default:
8932      break;
8933    }
8934
8935  /* If the type-specifier was for a built-in type, we're done.  */
8936  if (type)
8937    {
8938      tree id;
8939
8940      /* Consume the token.  */
8941      id = cp_lexer_consume_token (parser->lexer)->value;
8942
8943      /* There is no valid C++ program where a non-template type is
8944	 followed by a "<".  That usually indicates that the user thought
8945	 that the type was a template.  */
8946      cp_parser_check_for_invalid_template_id (parser, type);
8947
8948      return identifier_p ? id : TYPE_NAME (type);
8949    }
8950
8951  /* The type-specifier must be a user-defined type.  */
8952  if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8953    {
8954      bool qualified_p;
8955      bool global_p;
8956
8957      /* Don't gobble tokens or issue error messages if this is an
8958	 optional type-specifier.  */
8959      if (flags & CP_PARSER_FLAGS_OPTIONAL)
8960	cp_parser_parse_tentatively (parser);
8961
8962      /* Look for the optional `::' operator.  */
8963      global_p
8964	= (cp_parser_global_scope_opt (parser,
8965				       /*current_scope_valid_p=*/false)
8966	   != NULL_TREE);
8967      /* Look for the nested-name specifier.  */
8968      qualified_p
8969	= (cp_parser_nested_name_specifier_opt (parser,
8970						/*typename_keyword_p=*/false,
8971						/*check_dependency_p=*/true,
8972						/*type_p=*/false,
8973						/*is_declaration=*/false)
8974	   != NULL_TREE);
8975      /* If we have seen a nested-name-specifier, and the next token
8976	 is `template', then we are using the template-id production.  */
8977      if (parser->scope
8978	  && cp_parser_optional_template_keyword (parser))
8979	{
8980	  /* Look for the template-id.  */
8981	  type = cp_parser_template_id (parser,
8982					/*template_keyword_p=*/true,
8983					/*check_dependency_p=*/true,
8984					/*is_declaration=*/false);
8985	  /* If the template-id did not name a type, we are out of
8986	     luck.  */
8987	  if (TREE_CODE (type) != TYPE_DECL)
8988	    {
8989	      cp_parser_error (parser, "expected template-id for type");
8990	      type = NULL_TREE;
8991	    }
8992	}
8993      /* Otherwise, look for a type-name.  */
8994      else
8995	type = cp_parser_type_name (parser);
8996      /* Keep track of all name-lookups performed in class scopes.  */
8997      if (type
8998	  && !global_p
8999	  && !qualified_p
9000	  && TREE_CODE (type) == TYPE_DECL
9001	  && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9002	maybe_note_name_used_in_class (DECL_NAME (type), type);
9003      /* If it didn't work out, we don't have a TYPE.  */
9004      if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9005	  && !cp_parser_parse_definitely (parser))
9006	type = NULL_TREE;
9007    }
9008
9009  /* If we didn't get a type-name, issue an error message.  */
9010  if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9011    {
9012      cp_parser_error (parser, "expected type-name");
9013      return error_mark_node;
9014    }
9015
9016  /* There is no valid C++ program where a non-template type is
9017     followed by a "<".  That usually indicates that the user thought
9018     that the type was a template.  */
9019  if (type && type != error_mark_node)
9020    cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9021
9022  return type;
9023}
9024
9025/* Parse a type-name.
9026
9027   type-name:
9028     class-name
9029     enum-name
9030     typedef-name
9031
9032   enum-name:
9033     identifier
9034
9035   typedef-name:
9036     identifier
9037
9038   Returns a TYPE_DECL for the the type.  */
9039
9040static tree
9041cp_parser_type_name (cp_parser* parser)
9042{
9043  tree type_decl;
9044  tree identifier;
9045
9046  /* We can't know yet whether it is a class-name or not.  */
9047  cp_parser_parse_tentatively (parser);
9048  /* Try a class-name.  */
9049  type_decl = cp_parser_class_name (parser,
9050				    /*typename_keyword_p=*/false,
9051				    /*template_keyword_p=*/false,
9052				    /*type_p=*/false,
9053				    /*check_dependency_p=*/true,
9054				    /*class_head_p=*/false,
9055				    /*is_declaration=*/false);
9056  /* If it's not a class-name, keep looking.  */
9057  if (!cp_parser_parse_definitely (parser))
9058    {
9059      /* It must be a typedef-name or an enum-name.  */
9060      identifier = cp_parser_identifier (parser);
9061      if (identifier == error_mark_node)
9062	return error_mark_node;
9063
9064      /* Look up the type-name.  */
9065      type_decl = cp_parser_lookup_name_simple (parser, identifier);
9066      /* Issue an error if we did not find a type-name.  */
9067      if (TREE_CODE (type_decl) != TYPE_DECL)
9068	{
9069	  if (!cp_parser_simulate_error (parser))
9070	    cp_parser_name_lookup_error (parser, identifier, type_decl,
9071					 "is not a type");
9072	  type_decl = error_mark_node;
9073	}
9074      /* Remember that the name was used in the definition of the
9075	 current class so that we can check later to see if the
9076	 meaning would have been different after the class was
9077	 entirely defined.  */
9078      else if (type_decl != error_mark_node
9079	       && !parser->scope)
9080	maybe_note_name_used_in_class (identifier, type_decl);
9081    }
9082
9083  return type_decl;
9084}
9085
9086
9087/* Parse an elaborated-type-specifier.  Note that the grammar given
9088   here incorporates the resolution to DR68.
9089
9090   elaborated-type-specifier:
9091     class-key :: [opt] nested-name-specifier [opt] identifier
9092     class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9093     enum :: [opt] nested-name-specifier [opt] identifier
9094     typename :: [opt] nested-name-specifier identifier
9095     typename :: [opt] nested-name-specifier template [opt]
9096       template-id
9097
9098   GNU extension:
9099
9100   elaborated-type-specifier:
9101     class-key attributes :: [opt] nested-name-specifier [opt] identifier
9102     class-key attributes :: [opt] nested-name-specifier [opt]
9103               template [opt] template-id
9104     enum attributes :: [opt] nested-name-specifier [opt] identifier
9105
9106   If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9107   declared `friend'.  If IS_DECLARATION is TRUE, then this
9108   elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9109   something is being declared.
9110
9111   Returns the TYPE specified.  */
9112
9113static tree
9114cp_parser_elaborated_type_specifier (cp_parser* parser,
9115                                     bool is_friend,
9116                                     bool is_declaration)
9117{
9118  enum tag_types tag_type;
9119  tree identifier;
9120  tree type = NULL_TREE;
9121  tree attributes = NULL_TREE;
9122
9123  /* See if we're looking at the `enum' keyword.  */
9124  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9125    {
9126      /* Consume the `enum' token.  */
9127      cp_lexer_consume_token (parser->lexer);
9128      /* Remember that it's an enumeration type.  */
9129      tag_type = enum_type;
9130      /* Parse the attributes.  */
9131      attributes = cp_parser_attributes_opt (parser);
9132    }
9133  /* Or, it might be `typename'.  */
9134  else if (cp_lexer_next_token_is_keyword (parser->lexer,
9135					   RID_TYPENAME))
9136    {
9137      /* Consume the `typename' token.  */
9138      cp_lexer_consume_token (parser->lexer);
9139      /* Remember that it's a `typename' type.  */
9140      tag_type = typename_type;
9141      /* The `typename' keyword is only allowed in templates.  */
9142      if (!processing_template_decl)
9143	pedwarn ("using `typename' outside of template");
9144    }
9145  /* Otherwise it must be a class-key.  */
9146  else
9147    {
9148      tag_type = cp_parser_class_key (parser);
9149      if (tag_type == none_type)
9150	return error_mark_node;
9151      /* Parse the attributes.  */
9152      attributes = cp_parser_attributes_opt (parser);
9153    }
9154
9155  /* Look for the `::' operator.  */
9156  cp_parser_global_scope_opt (parser,
9157			      /*current_scope_valid_p=*/false);
9158  /* Look for the nested-name-specifier.  */
9159  if (tag_type == typename_type)
9160    {
9161      if (cp_parser_nested_name_specifier (parser,
9162					   /*typename_keyword_p=*/true,
9163					   /*check_dependency_p=*/true,
9164					   /*type_p=*/true,
9165					   is_declaration)
9166	  == error_mark_node)
9167	return error_mark_node;
9168    }
9169  else
9170    /* Even though `typename' is not present, the proposed resolution
9171       to Core Issue 180 says that in `class A<T>::B', `B' should be
9172       considered a type-name, even if `A<T>' is dependent.  */
9173    cp_parser_nested_name_specifier_opt (parser,
9174					 /*typename_keyword_p=*/true,
9175					 /*check_dependency_p=*/true,
9176					 /*type_p=*/true,
9177					 is_declaration);
9178  /* For everything but enumeration types, consider a template-id.  */
9179  if (tag_type != enum_type)
9180    {
9181      bool template_p = false;
9182      tree decl;
9183
9184      /* Allow the `template' keyword.  */
9185      template_p = cp_parser_optional_template_keyword (parser);
9186      /* If we didn't see `template', we don't know if there's a
9187         template-id or not.  */
9188      if (!template_p)
9189	cp_parser_parse_tentatively (parser);
9190      /* Parse the template-id.  */
9191      decl = cp_parser_template_id (parser, template_p,
9192				    /*check_dependency_p=*/true,
9193				    is_declaration);
9194      /* If we didn't find a template-id, look for an ordinary
9195         identifier.  */
9196      if (!template_p && !cp_parser_parse_definitely (parser))
9197	;
9198      /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9199	 in effect, then we must assume that, upon instantiation, the
9200	 template will correspond to a class.  */
9201      else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9202	       && tag_type == typename_type)
9203	type = make_typename_type (parser->scope, decl,
9204				   /*complain=*/1);
9205      else
9206	type = TREE_TYPE (decl);
9207    }
9208
9209  /* For an enumeration type, consider only a plain identifier.  */
9210  if (!type)
9211    {
9212      identifier = cp_parser_identifier (parser);
9213
9214      if (identifier == error_mark_node)
9215	{
9216	  parser->scope = NULL_TREE;
9217	  return error_mark_node;
9218	}
9219
9220      /* For a `typename', we needn't call xref_tag.  */
9221      if (tag_type == typename_type)
9222	return make_typename_type (parser->scope, identifier,
9223				   /*complain=*/1);
9224      /* Look up a qualified name in the usual way.  */
9225      if (parser->scope)
9226	{
9227	  tree decl;
9228
9229	  /* In an elaborated-type-specifier, names are assumed to name
9230	     types, so we set IS_TYPE to TRUE when calling
9231	     cp_parser_lookup_name.  */
9232	  decl = cp_parser_lookup_name (parser, identifier,
9233					/*is_type=*/true,
9234					/*is_template=*/false,
9235					/*is_namespace=*/false,
9236					/*check_dependency=*/true);
9237
9238	  /* If we are parsing friend declaration, DECL may be a
9239	     TEMPLATE_DECL tree node here.  However, we need to check
9240	     whether this TEMPLATE_DECL results in valid code.  Consider
9241	     the following example:
9242
9243	       namespace N {
9244		 template <class T> class C {};
9245	       }
9246	       class X {
9247		 template <class T> friend class N::C; // #1, valid code
9248	       };
9249	       template <class T> class Y {
9250		 friend class N::C;		       // #2, invalid code
9251	       };
9252
9253	     For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9254	     name lookup of `N::C'.  We see that friend declaration must
9255	     be template for the code to be valid.  Note that
9256	     processing_template_decl does not work here since it is
9257	     always 1 for the above two cases.  */
9258
9259	  decl = (cp_parser_maybe_treat_template_as_class
9260		  (decl, /*tag_name_p=*/is_friend
9261			 && parser->num_template_parameter_lists));
9262
9263	  if (TREE_CODE (decl) != TYPE_DECL)
9264	    {
9265	      error ("expected type-name");
9266	      return error_mark_node;
9267	    }
9268
9269	  if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9270	    check_elaborated_type_specifier
9271	      (tag_type, decl,
9272	       (parser->num_template_parameter_lists
9273		|| DECL_SELF_REFERENCE_P (decl)));
9274
9275	  type = TREE_TYPE (decl);
9276	}
9277      else
9278	{
9279	  /* An elaborated-type-specifier sometimes introduces a new type and
9280	     sometimes names an existing type.  Normally, the rule is that it
9281	     introduces a new type only if there is not an existing type of
9282	     the same name already in scope.  For example, given:
9283
9284	       struct S {};
9285	       void f() { struct S s; }
9286
9287	     the `struct S' in the body of `f' is the same `struct S' as in
9288	     the global scope; the existing definition is used.  However, if
9289	     there were no global declaration, this would introduce a new
9290	     local class named `S'.
9291
9292	     An exception to this rule applies to the following code:
9293
9294	       namespace N { struct S; }
9295
9296	     Here, the elaborated-type-specifier names a new type
9297	     unconditionally; even if there is already an `S' in the
9298	     containing scope this declaration names a new type.
9299	     This exception only applies if the elaborated-type-specifier
9300	     forms the complete declaration:
9301
9302	       [class.name]
9303
9304	       A declaration consisting solely of `class-key identifier ;' is
9305	       either a redeclaration of the name in the current scope or a
9306	       forward declaration of the identifier as a class name.  It
9307	       introduces the name into the current scope.
9308
9309	     We are in this situation precisely when the next token is a `;'.
9310
9311	     An exception to the exception is that a `friend' declaration does
9312	     *not* name a new type; i.e., given:
9313
9314	       struct S { friend struct T; };
9315
9316	     `T' is not a new type in the scope of `S'.
9317
9318	     Also, `new struct S' or `sizeof (struct S)' never results in the
9319	     definition of a new type; a new type can only be declared in a
9320	     declaration context.  */
9321
9322 	  /* Warn about attributes. They are ignored.  */
9323 	  if (attributes)
9324	    warning ("type attributes are honored only at type definition");
9325
9326	  type = xref_tag (tag_type, identifier,
9327			   (is_friend
9328			    || !is_declaration
9329			    || cp_lexer_next_token_is_not (parser->lexer,
9330							   CPP_SEMICOLON)),
9331			   parser->num_template_parameter_lists);
9332	}
9333    }
9334  if (tag_type != enum_type)
9335    cp_parser_check_class_key (tag_type, type);
9336
9337  /* A "<" cannot follow an elaborated type specifier.  If that
9338     happens, the user was probably trying to form a template-id.  */
9339  cp_parser_check_for_invalid_template_id (parser, type);
9340
9341  return type;
9342}
9343
9344/* Parse an enum-specifier.
9345
9346   enum-specifier:
9347     enum identifier [opt] { enumerator-list [opt] }
9348
9349   Returns an ENUM_TYPE representing the enumeration.  */
9350
9351static tree
9352cp_parser_enum_specifier (cp_parser* parser)
9353{
9354  cp_token *token;
9355  tree identifier = NULL_TREE;
9356  tree type;
9357
9358  /* Look for the `enum' keyword.  */
9359  if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9360    return error_mark_node;
9361  /* Peek at the next token.  */
9362  token = cp_lexer_peek_token (parser->lexer);
9363
9364  /* See if it is an identifier.  */
9365  if (token->type == CPP_NAME)
9366    identifier = cp_parser_identifier (parser);
9367
9368  /* Look for the `{'.  */
9369  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9370    return error_mark_node;
9371
9372  /* At this point, we're going ahead with the enum-specifier, even
9373     if some other problem occurs.  */
9374  cp_parser_commit_to_tentative_parse (parser);
9375
9376  /* Issue an error message if type-definitions are forbidden here.  */
9377  cp_parser_check_type_definition (parser);
9378
9379  /* Create the new type.  */
9380  type = start_enum (identifier ? identifier : make_anon_name ());
9381
9382  /* Peek at the next token.  */
9383  token = cp_lexer_peek_token (parser->lexer);
9384  /* If it's not a `}', then there are some enumerators.  */
9385  if (token->type != CPP_CLOSE_BRACE)
9386    cp_parser_enumerator_list (parser, type);
9387  /* Look for the `}'.  */
9388  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9389
9390  /* Finish up the enumeration.  */
9391  finish_enum (type);
9392
9393  return type;
9394}
9395
9396/* Parse an enumerator-list.  The enumerators all have the indicated
9397   TYPE.
9398
9399   enumerator-list:
9400     enumerator-definition
9401     enumerator-list , enumerator-definition  */
9402
9403static void
9404cp_parser_enumerator_list (cp_parser* parser, tree type)
9405{
9406  while (true)
9407    {
9408      cp_token *token;
9409
9410      /* Parse an enumerator-definition.  */
9411      cp_parser_enumerator_definition (parser, type);
9412      /* Peek at the next token.  */
9413      token = cp_lexer_peek_token (parser->lexer);
9414      /* If it's not a `,', then we've reached the end of the
9415	 list.  */
9416      if (token->type != CPP_COMMA)
9417	break;
9418      /* Otherwise, consume the `,' and keep going.  */
9419      cp_lexer_consume_token (parser->lexer);
9420      /* If the next token is a `}', there is a trailing comma.  */
9421      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9422	{
9423	  if (pedantic && !in_system_header)
9424	    pedwarn ("comma at end of enumerator list");
9425	  break;
9426	}
9427    }
9428}
9429
9430/* Parse an enumerator-definition.  The enumerator has the indicated
9431   TYPE.
9432
9433   enumerator-definition:
9434     enumerator
9435     enumerator = constant-expression
9436
9437   enumerator:
9438     identifier  */
9439
9440static void
9441cp_parser_enumerator_definition (cp_parser* parser, tree type)
9442{
9443  cp_token *token;
9444  tree identifier;
9445  tree value;
9446
9447  /* Look for the identifier.  */
9448  identifier = cp_parser_identifier (parser);
9449  if (identifier == error_mark_node)
9450    return;
9451
9452  /* Peek at the next token.  */
9453  token = cp_lexer_peek_token (parser->lexer);
9454  /* If it's an `=', then there's an explicit value.  */
9455  if (token->type == CPP_EQ)
9456    {
9457      /* Consume the `=' token.  */
9458      cp_lexer_consume_token (parser->lexer);
9459      /* Parse the value.  */
9460      value = cp_parser_constant_expression (parser,
9461					     /*allow_non_constant_p=*/false,
9462					     NULL);
9463    }
9464  else
9465    value = NULL_TREE;
9466
9467  /* Create the enumerator.  */
9468  build_enumerator (identifier, value, type);
9469}
9470
9471/* Parse a namespace-name.
9472
9473   namespace-name:
9474     original-namespace-name
9475     namespace-alias
9476
9477   Returns the NAMESPACE_DECL for the namespace.  */
9478
9479static tree
9480cp_parser_namespace_name (cp_parser* parser)
9481{
9482  tree identifier;
9483  tree namespace_decl;
9484
9485  /* Get the name of the namespace.  */
9486  identifier = cp_parser_identifier (parser);
9487  if (identifier == error_mark_node)
9488    return error_mark_node;
9489
9490  /* Look up the identifier in the currently active scope.  Look only
9491     for namespaces, due to:
9492
9493       [basic.lookup.udir]
9494
9495       When looking up a namespace-name in a using-directive or alias
9496       definition, only namespace names are considered.
9497
9498     And:
9499
9500       [basic.lookup.qual]
9501
9502       During the lookup of a name preceding the :: scope resolution
9503       operator, object, function, and enumerator names are ignored.
9504
9505     (Note that cp_parser_class_or_namespace_name only calls this
9506     function if the token after the name is the scope resolution
9507     operator.)  */
9508  namespace_decl = cp_parser_lookup_name (parser, identifier,
9509					  /*is_type=*/false,
9510					  /*is_template=*/false,
9511					  /*is_namespace=*/true,
9512					  /*check_dependency=*/true);
9513  /* If it's not a namespace, issue an error.  */
9514  if (namespace_decl == error_mark_node
9515      || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9516    {
9517      cp_parser_error (parser, "expected namespace-name");
9518      namespace_decl = error_mark_node;
9519    }
9520
9521  return namespace_decl;
9522}
9523
9524/* Parse a namespace-definition.
9525
9526   namespace-definition:
9527     named-namespace-definition
9528     unnamed-namespace-definition
9529
9530   named-namespace-definition:
9531     original-namespace-definition
9532     extension-namespace-definition
9533
9534   original-namespace-definition:
9535     namespace identifier { namespace-body }
9536
9537   extension-namespace-definition:
9538     namespace original-namespace-name { namespace-body }
9539
9540   unnamed-namespace-definition:
9541     namespace { namespace-body } */
9542
9543static void
9544cp_parser_namespace_definition (cp_parser* parser)
9545{
9546  tree identifier;
9547
9548  /* Look for the `namespace' keyword.  */
9549  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9550
9551  /* Get the name of the namespace.  We do not attempt to distinguish
9552     between an original-namespace-definition and an
9553     extension-namespace-definition at this point.  The semantic
9554     analysis routines are responsible for that.  */
9555  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9556    identifier = cp_parser_identifier (parser);
9557  else
9558    identifier = NULL_TREE;
9559
9560  /* Look for the `{' to start the namespace.  */
9561  cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9562  /* Start the namespace.  */
9563  push_namespace (identifier);
9564  /* Parse the body of the namespace.  */
9565  cp_parser_namespace_body (parser);
9566  /* Finish the namespace.  */
9567  pop_namespace ();
9568  /* Look for the final `}'.  */
9569  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9570}
9571
9572/* Parse a namespace-body.
9573
9574   namespace-body:
9575     declaration-seq [opt]  */
9576
9577static void
9578cp_parser_namespace_body (cp_parser* parser)
9579{
9580  cp_parser_declaration_seq_opt (parser);
9581}
9582
9583/* Parse a namespace-alias-definition.
9584
9585   namespace-alias-definition:
9586     namespace identifier = qualified-namespace-specifier ;  */
9587
9588static void
9589cp_parser_namespace_alias_definition (cp_parser* parser)
9590{
9591  tree identifier;
9592  tree namespace_specifier;
9593
9594  /* Look for the `namespace' keyword.  */
9595  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9596  /* Look for the identifier.  */
9597  identifier = cp_parser_identifier (parser);
9598  if (identifier == error_mark_node)
9599    return;
9600  /* Look for the `=' token.  */
9601  cp_parser_require (parser, CPP_EQ, "`='");
9602  /* Look for the qualified-namespace-specifier.  */
9603  namespace_specifier
9604    = cp_parser_qualified_namespace_specifier (parser);
9605  /* Look for the `;' token.  */
9606  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9607
9608  /* Register the alias in the symbol table.  */
9609  do_namespace_alias (identifier, namespace_specifier);
9610}
9611
9612/* Parse a qualified-namespace-specifier.
9613
9614   qualified-namespace-specifier:
9615     :: [opt] nested-name-specifier [opt] namespace-name
9616
9617   Returns a NAMESPACE_DECL corresponding to the specified
9618   namespace.  */
9619
9620static tree
9621cp_parser_qualified_namespace_specifier (cp_parser* parser)
9622{
9623  /* Look for the optional `::'.  */
9624  cp_parser_global_scope_opt (parser,
9625			      /*current_scope_valid_p=*/false);
9626
9627  /* Look for the optional nested-name-specifier.  */
9628  cp_parser_nested_name_specifier_opt (parser,
9629				       /*typename_keyword_p=*/false,
9630				       /*check_dependency_p=*/true,
9631				       /*type_p=*/false,
9632				       /*is_declaration=*/true);
9633
9634  return cp_parser_namespace_name (parser);
9635}
9636
9637/* Parse a using-declaration.
9638
9639   using-declaration:
9640     using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9641     using :: unqualified-id ;  */
9642
9643static void
9644cp_parser_using_declaration (cp_parser* parser)
9645{
9646  cp_token *token;
9647  bool typename_p = false;
9648  bool global_scope_p;
9649  tree decl;
9650  tree identifier;
9651  tree scope;
9652  tree qscope;
9653
9654  /* Look for the `using' keyword.  */
9655  cp_parser_require_keyword (parser, RID_USING, "`using'");
9656
9657  /* Peek at the next token.  */
9658  token = cp_lexer_peek_token (parser->lexer);
9659  /* See if it's `typename'.  */
9660  if (token->keyword == RID_TYPENAME)
9661    {
9662      /* Remember that we've seen it.  */
9663      typename_p = true;
9664      /* Consume the `typename' token.  */
9665      cp_lexer_consume_token (parser->lexer);
9666    }
9667
9668  /* Look for the optional global scope qualification.  */
9669  global_scope_p
9670    = (cp_parser_global_scope_opt (parser,
9671				   /*current_scope_valid_p=*/false)
9672       != NULL_TREE);
9673
9674  /* If we saw `typename', or didn't see `::', then there must be a
9675     nested-name-specifier present.  */
9676  if (typename_p || !global_scope_p)
9677    qscope = cp_parser_nested_name_specifier (parser, typename_p,
9678					      /*check_dependency_p=*/true,
9679					      /*type_p=*/false,
9680					      /*is_declaration=*/true);
9681  /* Otherwise, we could be in either of the two productions.  In that
9682     case, treat the nested-name-specifier as optional.  */
9683  else
9684    qscope = cp_parser_nested_name_specifier_opt (parser,
9685						  /*typename_keyword_p=*/false,
9686						  /*check_dependency_p=*/true,
9687						  /*type_p=*/false,
9688						  /*is_declaration=*/true);
9689  if (!qscope)
9690    qscope = global_namespace;
9691
9692  /* Parse the unqualified-id.  */
9693  identifier = cp_parser_unqualified_id (parser,
9694					 /*template_keyword_p=*/false,
9695					 /*check_dependency_p=*/true,
9696					 /*declarator_p=*/true);
9697
9698  /* The function we call to handle a using-declaration is different
9699     depending on what scope we are in.  */
9700  if (identifier == error_mark_node)
9701    ;
9702  else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9703	   && TREE_CODE (identifier) != BIT_NOT_EXPR)
9704    /* [namespace.udecl]
9705
9706       A using declaration shall not name a template-id.  */
9707    error ("a template-id may not appear in a using-declaration");
9708  else
9709    {
9710      scope = current_scope ();
9711      if (scope && TYPE_P (scope))
9712	{
9713	  /* Create the USING_DECL.  */
9714	  decl = do_class_using_decl (build_nt (SCOPE_REF,
9715						parser->scope,
9716						identifier));
9717	  /* Add it to the list of members in this class.  */
9718	  finish_member_declaration (decl);
9719	}
9720      else
9721	{
9722	  decl = cp_parser_lookup_name_simple (parser, identifier);
9723	  if (decl == error_mark_node)
9724	    cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9725	  else if (scope)
9726	    do_local_using_decl (decl, qscope, identifier);
9727	  else
9728	    do_toplevel_using_decl (decl, qscope, identifier);
9729	}
9730    }
9731
9732  /* Look for the final `;'.  */
9733  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9734}
9735
9736/* Parse a using-directive.
9737
9738   using-directive:
9739     using namespace :: [opt] nested-name-specifier [opt]
9740       namespace-name ;  */
9741
9742static void
9743cp_parser_using_directive (cp_parser* parser)
9744{
9745  tree namespace_decl;
9746  tree attribs;
9747
9748  /* Look for the `using' keyword.  */
9749  cp_parser_require_keyword (parser, RID_USING, "`using'");
9750  /* And the `namespace' keyword.  */
9751  cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9752  /* Look for the optional `::' operator.  */
9753  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9754  /* And the optional nested-name-specifier.  */
9755  cp_parser_nested_name_specifier_opt (parser,
9756				       /*typename_keyword_p=*/false,
9757				       /*check_dependency_p=*/true,
9758				       /*type_p=*/false,
9759				       /*is_declaration=*/true);
9760  /* Get the namespace being used.  */
9761  namespace_decl = cp_parser_namespace_name (parser);
9762  /* And any specified attributes.  */
9763  attribs = cp_parser_attributes_opt (parser);
9764  /* Update the symbol table.  */
9765  parse_using_directive (namespace_decl, attribs);
9766  /* Look for the final `;'.  */
9767  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9768}
9769
9770/* Parse an asm-definition.
9771
9772   asm-definition:
9773     asm ( string-literal ) ;
9774
9775   GNU Extension:
9776
9777   asm-definition:
9778     asm volatile [opt] ( string-literal ) ;
9779     asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9780     asm volatile [opt] ( string-literal : asm-operand-list [opt]
9781                          : asm-operand-list [opt] ) ;
9782     asm volatile [opt] ( string-literal : asm-operand-list [opt]
9783                          : asm-operand-list [opt]
9784                          : asm-operand-list [opt] ) ;  */
9785
9786static void
9787cp_parser_asm_definition (cp_parser* parser)
9788{
9789  cp_token *token;
9790  tree string;
9791  tree outputs = NULL_TREE;
9792  tree inputs = NULL_TREE;
9793  tree clobbers = NULL_TREE;
9794  tree asm_stmt;
9795  bool volatile_p = false;
9796  bool extended_p = false;
9797
9798  /* Look for the `asm' keyword.  */
9799  cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9800  /* See if the next token is `volatile'.  */
9801  if (cp_parser_allow_gnu_extensions_p (parser)
9802      && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9803    {
9804      /* Remember that we saw the `volatile' keyword.  */
9805      volatile_p = true;
9806      /* Consume the token.  */
9807      cp_lexer_consume_token (parser->lexer);
9808    }
9809  /* Look for the opening `('.  */
9810  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9811  /* Look for the string.  */
9812  token = cp_parser_require (parser, CPP_STRING, "asm body");
9813  if (!token)
9814    return;
9815  string = token->value;
9816  /* If we're allowing GNU extensions, check for the extended assembly
9817     syntax.  Unfortunately, the `:' tokens need not be separated by
9818     a space in C, and so, for compatibility, we tolerate that here
9819     too.  Doing that means that we have to treat the `::' operator as
9820     two `:' tokens.  */
9821  if (cp_parser_allow_gnu_extensions_p (parser)
9822      && at_function_scope_p ()
9823      && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9824	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9825    {
9826      bool inputs_p = false;
9827      bool clobbers_p = false;
9828
9829      /* The extended syntax was used.  */
9830      extended_p = true;
9831
9832      /* Look for outputs.  */
9833      if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9834	{
9835	  /* Consume the `:'.  */
9836	  cp_lexer_consume_token (parser->lexer);
9837	  /* Parse the output-operands.  */
9838	  if (cp_lexer_next_token_is_not (parser->lexer,
9839					  CPP_COLON)
9840	      && cp_lexer_next_token_is_not (parser->lexer,
9841					     CPP_SCOPE)
9842	      && cp_lexer_next_token_is_not (parser->lexer,
9843					     CPP_CLOSE_PAREN))
9844	    outputs = cp_parser_asm_operand_list (parser);
9845	}
9846      /* If the next token is `::', there are no outputs, and the
9847	 next token is the beginning of the inputs.  */
9848      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9849	/* The inputs are coming next.  */
9850	inputs_p = true;
9851
9852      /* Look for inputs.  */
9853      if (inputs_p
9854	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9855	{
9856	  /* Consume the `:' or `::'.  */
9857	  cp_lexer_consume_token (parser->lexer);
9858	  /* Parse the output-operands.  */
9859	  if (cp_lexer_next_token_is_not (parser->lexer,
9860					  CPP_COLON)
9861	      && cp_lexer_next_token_is_not (parser->lexer,
9862					     CPP_CLOSE_PAREN))
9863	    inputs = cp_parser_asm_operand_list (parser);
9864	}
9865      else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9866	/* The clobbers are coming next.  */
9867	clobbers_p = true;
9868
9869      /* Look for clobbers.  */
9870      if (clobbers_p
9871	  || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9872	{
9873	  /* Consume the `:' or `::'.  */
9874	  cp_lexer_consume_token (parser->lexer);
9875	  /* Parse the clobbers.  */
9876	  if (cp_lexer_next_token_is_not (parser->lexer,
9877					  CPP_CLOSE_PAREN))
9878	    clobbers = cp_parser_asm_clobber_list (parser);
9879	}
9880    }
9881  /* Look for the closing `)'.  */
9882  if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9883    cp_parser_skip_to_closing_parenthesis (parser, true, false,
9884					   /*consume_paren=*/true);
9885  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9886
9887  /* Create the ASM_STMT.  */
9888  if (at_function_scope_p ())
9889    {
9890      asm_stmt =
9891	finish_asm_stmt (volatile_p
9892			 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9893			 string, outputs, inputs, clobbers);
9894      /* If the extended syntax was not used, mark the ASM_STMT.  */
9895      if (!extended_p)
9896	ASM_INPUT_P (asm_stmt) = 1;
9897    }
9898  else
9899    assemble_asm (string);
9900}
9901
9902/* Declarators [gram.dcl.decl] */
9903
9904/* Parse an init-declarator.
9905
9906   init-declarator:
9907     declarator initializer [opt]
9908
9909   GNU Extension:
9910
9911   init-declarator:
9912     declarator asm-specification [opt] attributes [opt] initializer [opt]
9913
9914   function-definition:
9915     decl-specifier-seq [opt] declarator ctor-initializer [opt]
9916       function-body
9917     decl-specifier-seq [opt] declarator function-try-block
9918
9919   GNU Extension:
9920
9921   function-definition:
9922     __extension__ function-definition
9923
9924   The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9925   Returns a representation of the entity declared.  If MEMBER_P is TRUE,
9926   then this declarator appears in a class scope.  The new DECL created
9927   by this declarator is returned.
9928
9929   If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9930   for a function-definition here as well.  If the declarator is a
9931   declarator for a function-definition, *FUNCTION_DEFINITION_P will
9932   be TRUE upon return.  By that point, the function-definition will
9933   have been completely parsed.
9934
9935   FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9936   is FALSE.  */
9937
9938static tree
9939cp_parser_init_declarator (cp_parser* parser,
9940			   tree decl_specifiers,
9941			   tree prefix_attributes,
9942			   bool function_definition_allowed_p,
9943			   bool member_p,
9944			   int declares_class_or_enum,
9945			   bool* function_definition_p)
9946{
9947  cp_token *token;
9948  tree declarator;
9949  tree attributes;
9950  tree asm_specification;
9951  tree initializer;
9952  tree decl = NULL_TREE;
9953  tree scope;
9954  bool is_initialized;
9955  bool is_parenthesized_init;
9956  bool is_non_constant_init;
9957  int ctor_dtor_or_conv_p;
9958  bool friend_p;
9959  bool pop_p = false;
9960
9961  /* Assume that this is not the declarator for a function
9962     definition.  */
9963  if (function_definition_p)
9964    *function_definition_p = false;
9965
9966  /* Defer access checks while parsing the declarator; we cannot know
9967     what names are accessible until we know what is being
9968     declared.  */
9969  resume_deferring_access_checks ();
9970
9971  /* Parse the declarator.  */
9972  declarator
9973    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9974			    &ctor_dtor_or_conv_p,
9975			    /*parenthesized_p=*/NULL,
9976			    /*member_p=*/false);
9977  /* Gather up the deferred checks.  */
9978  stop_deferring_access_checks ();
9979
9980  /* If the DECLARATOR was erroneous, there's no need to go
9981     further.  */
9982  if (declarator == error_mark_node)
9983    return error_mark_node;
9984
9985  cp_parser_check_for_definition_in_return_type (declarator,
9986						 declares_class_or_enum);
9987
9988  /* Figure out what scope the entity declared by the DECLARATOR is
9989     located in.  `grokdeclarator' sometimes changes the scope, so
9990     we compute it now.  */
9991  scope = get_scope_of_declarator (declarator);
9992
9993  /* If we're allowing GNU extensions, look for an asm-specification
9994     and attributes.  */
9995  if (cp_parser_allow_gnu_extensions_p (parser))
9996    {
9997      /* Look for an asm-specification.  */
9998      asm_specification = cp_parser_asm_specification_opt (parser);
9999      /* And attributes.  */
10000      attributes = cp_parser_attributes_opt (parser);
10001    }
10002  else
10003    {
10004      asm_specification = NULL_TREE;
10005      attributes = NULL_TREE;
10006    }
10007
10008  /* Peek at the next token.  */
10009  token = cp_lexer_peek_token (parser->lexer);
10010  /* Check to see if the token indicates the start of a
10011     function-definition.  */
10012  if (cp_parser_token_starts_function_definition_p (token))
10013    {
10014      if (!function_definition_allowed_p)
10015	{
10016	  /* If a function-definition should not appear here, issue an
10017	     error message.  */
10018	  cp_parser_error (parser,
10019			   "a function-definition is not allowed here");
10020	  return error_mark_node;
10021	}
10022      else
10023	{
10024	  /* Neither attributes nor an asm-specification are allowed
10025	     on a function-definition.  */
10026	  if (asm_specification)
10027	    error ("an asm-specification is not allowed on a function-definition");
10028	  if (attributes)
10029	    error ("attributes are not allowed on a function-definition");
10030	  /* This is a function-definition.  */
10031	  *function_definition_p = true;
10032
10033	  /* Parse the function definition.  */
10034	  if (member_p)
10035	    decl = cp_parser_save_member_function_body (parser,
10036							decl_specifiers,
10037							declarator,
10038							prefix_attributes);
10039	  else
10040	    decl
10041	      = (cp_parser_function_definition_from_specifiers_and_declarator
10042		 (parser, decl_specifiers, prefix_attributes, declarator));
10043
10044	  return decl;
10045	}
10046    }
10047
10048  /* [dcl.dcl]
10049
10050     Only in function declarations for constructors, destructors, and
10051     type conversions can the decl-specifier-seq be omitted.
10052
10053     We explicitly postpone this check past the point where we handle
10054     function-definitions because we tolerate function-definitions
10055     that are missing their return types in some modes.  */
10056  if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10057    {
10058      cp_parser_error (parser,
10059		       "expected constructor, destructor, or type conversion");
10060      return error_mark_node;
10061    }
10062
10063  /* An `=' or an `(' indicates an initializer.  */
10064  is_initialized = (token->type == CPP_EQ
10065		     || token->type == CPP_OPEN_PAREN);
10066  /* If the init-declarator isn't initialized and isn't followed by a
10067     `,' or `;', it's not a valid init-declarator.  */
10068  if (!is_initialized
10069      && token->type != CPP_COMMA
10070      && token->type != CPP_SEMICOLON)
10071    {
10072      cp_parser_error (parser, "expected init-declarator");
10073      return error_mark_node;
10074    }
10075
10076  /* Because start_decl has side-effects, we should only call it if we
10077     know we're going ahead.  By this point, we know that we cannot
10078     possibly be looking at any other construct.  */
10079  cp_parser_commit_to_tentative_parse (parser);
10080
10081  /* If the decl specifiers were bad, issue an error now that we're
10082     sure this was intended to be a declarator.  Then continue
10083     declaring the variable(s), as int, to try to cut down on further
10084     errors.  */
10085  if (decl_specifiers != NULL
10086      && TREE_VALUE (decl_specifiers) == error_mark_node)
10087    {
10088      cp_parser_error (parser, "invalid type in declaration");
10089      TREE_VALUE (decl_specifiers) = integer_type_node;
10090    }
10091
10092  /* Check to see whether or not this declaration is a friend.  */
10093  friend_p = cp_parser_friend_p (decl_specifiers);
10094
10095  /* Check that the number of template-parameter-lists is OK.  */
10096  if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10097    return error_mark_node;
10098
10099  /* Enter the newly declared entry in the symbol table.  If we're
10100     processing a declaration in a class-specifier, we wait until
10101     after processing the initializer.  */
10102  if (!member_p)
10103    {
10104      if (parser->in_unbraced_linkage_specification_p)
10105	{
10106	  decl_specifiers = tree_cons (error_mark_node,
10107				       get_identifier ("extern"),
10108				       decl_specifiers);
10109	  have_extern_spec = false;
10110	}
10111      decl = start_decl (declarator, decl_specifiers,
10112			 is_initialized, attributes, prefix_attributes);
10113    }
10114
10115  /* Enter the SCOPE.  That way unqualified names appearing in the
10116     initializer will be looked up in SCOPE.  */
10117  if (scope)
10118    pop_p = push_scope (scope);
10119
10120  /* Perform deferred access control checks, now that we know in which
10121     SCOPE the declared entity resides.  */
10122  if (!member_p && decl)
10123    {
10124      tree saved_current_function_decl = NULL_TREE;
10125
10126      /* If the entity being declared is a function, pretend that we
10127	 are in its scope.  If it is a `friend', it may have access to
10128	 things that would not otherwise be accessible.  */
10129      if (TREE_CODE (decl) == FUNCTION_DECL)
10130	{
10131	  saved_current_function_decl = current_function_decl;
10132	  current_function_decl = decl;
10133	}
10134
10135      /* Perform the access control checks for the declarator and the
10136	 the decl-specifiers.  */
10137      perform_deferred_access_checks ();
10138
10139      /* Restore the saved value.  */
10140      if (TREE_CODE (decl) == FUNCTION_DECL)
10141	current_function_decl = saved_current_function_decl;
10142    }
10143
10144  /* Parse the initializer.  */
10145  if (is_initialized)
10146    initializer = cp_parser_initializer (parser,
10147					 &is_parenthesized_init,
10148					 &is_non_constant_init);
10149  else
10150    {
10151      initializer = NULL_TREE;
10152      is_parenthesized_init = false;
10153      is_non_constant_init = true;
10154    }
10155
10156  /* The old parser allows attributes to appear after a parenthesized
10157     initializer.  Mark Mitchell proposed removing this functionality
10158     on the GCC mailing lists on 2002-08-13.  This parser accepts the
10159     attributes -- but ignores them.  */
10160  if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10161    if (cp_parser_attributes_opt (parser))
10162      warning ("attributes after parenthesized initializer ignored");
10163
10164  /* Leave the SCOPE, now that we have processed the initializer.  It
10165     is important to do this before calling cp_finish_decl because it
10166     makes decisions about whether to create DECL_STMTs or not based
10167     on the current scope.  */
10168  if (pop_p)
10169    pop_scope (scope);
10170
10171  /* For an in-class declaration, use `grokfield' to create the
10172     declaration.  */
10173  if (member_p)
10174    {
10175      decl = grokfield (declarator, decl_specifiers,
10176			initializer, /*asmspec=*/NULL_TREE,
10177			/*attributes=*/NULL_TREE);
10178      if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10179	cp_parser_save_default_args (parser, decl);
10180    }
10181
10182  /* Finish processing the declaration.  But, skip friend
10183     declarations.  */
10184  if (!friend_p && decl)
10185    cp_finish_decl (decl,
10186		    initializer,
10187		    asm_specification,
10188		    /* If the initializer is in parentheses, then this is
10189		       a direct-initialization, which means that an
10190		       `explicit' constructor is OK.  Otherwise, an
10191		       `explicit' constructor cannot be used.  */
10192		    ((is_parenthesized_init || !is_initialized)
10193		     ? 0 : LOOKUP_ONLYCONVERTING));
10194
10195  /* Remember whether or not variables were initialized by
10196     constant-expressions.  */
10197  if (decl && TREE_CODE (decl) == VAR_DECL
10198      && is_initialized && !is_non_constant_init)
10199    DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10200
10201  return decl;
10202}
10203
10204/* Parse a declarator.
10205
10206   declarator:
10207     direct-declarator
10208     ptr-operator declarator
10209
10210   abstract-declarator:
10211     ptr-operator abstract-declarator [opt]
10212     direct-abstract-declarator
10213
10214   GNU Extensions:
10215
10216   declarator:
10217     attributes [opt] direct-declarator
10218     attributes [opt] ptr-operator declarator
10219
10220   abstract-declarator:
10221     attributes [opt] ptr-operator abstract-declarator [opt]
10222     attributes [opt] direct-abstract-declarator
10223
10224   Returns a representation of the declarator.  If the declarator has
10225   the form `* declarator', then an INDIRECT_REF is returned, whose
10226   only operand is the sub-declarator.  Analogously, `& declarator' is
10227   represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
10228   used.  The first operand is the TYPE for `X'.  The second operand
10229   is an INDIRECT_REF whose operand is the sub-declarator.
10230
10231   Otherwise, the representation is as for a direct-declarator.
10232
10233   (It would be better to define a structure type to represent
10234   declarators, rather than abusing `tree' nodes to represent
10235   declarators.  That would be much clearer and save some memory.
10236   There is no reason for declarators to be garbage-collected, for
10237   example; they are created during parser and no longer needed after
10238   `grokdeclarator' has been called.)
10239
10240   For a ptr-operator that has the optional cv-qualifier-seq,
10241   cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10242   node.
10243
10244   If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10245   detect constructor, destructor or conversion operators. It is set
10246   to -1 if the declarator is a name, and +1 if it is a
10247   function. Otherwise it is set to zero. Usually you just want to
10248   test for >0, but internally the negative value is used.
10249
10250   (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10251   a decl-specifier-seq unless it declares a constructor, destructor,
10252   or conversion.  It might seem that we could check this condition in
10253   semantic analysis, rather than parsing, but that makes it difficult
10254   to handle something like `f()'.  We want to notice that there are
10255   no decl-specifiers, and therefore realize that this is an
10256   expression, not a declaration.)
10257
10258   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10259   the declarator is a direct-declarator of the form "(...)".
10260
10261   MEMBER_P is true iff this declarator is a member-declarator.  */
10262
10263static tree
10264cp_parser_declarator (cp_parser* parser,
10265                      cp_parser_declarator_kind dcl_kind,
10266                      int* ctor_dtor_or_conv_p,
10267		      bool* parenthesized_p,
10268		      bool member_p)
10269{
10270  cp_token *token;
10271  tree declarator;
10272  enum tree_code code;
10273  tree cv_qualifier_seq;
10274  tree class_type;
10275  tree attributes = NULL_TREE;
10276
10277  /* Assume this is not a constructor, destructor, or type-conversion
10278     operator.  */
10279  if (ctor_dtor_or_conv_p)
10280    *ctor_dtor_or_conv_p = 0;
10281
10282  if (cp_parser_allow_gnu_extensions_p (parser))
10283    attributes = cp_parser_attributes_opt (parser);
10284
10285  /* Peek at the next token.  */
10286  token = cp_lexer_peek_token (parser->lexer);
10287
10288  /* Check for the ptr-operator production.  */
10289  cp_parser_parse_tentatively (parser);
10290  /* Parse the ptr-operator.  */
10291  code = cp_parser_ptr_operator (parser,
10292				 &class_type,
10293				 &cv_qualifier_seq);
10294  /* If that worked, then we have a ptr-operator.  */
10295  if (cp_parser_parse_definitely (parser))
10296    {
10297      /* If a ptr-operator was found, then this declarator was not
10298	 parenthesized.  */
10299      if (parenthesized_p)
10300	*parenthesized_p = true;
10301      /* The dependent declarator is optional if we are parsing an
10302	 abstract-declarator.  */
10303      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10304	cp_parser_parse_tentatively (parser);
10305
10306      /* Parse the dependent declarator.  */
10307      declarator = cp_parser_declarator (parser, dcl_kind,
10308					 /*ctor_dtor_or_conv_p=*/NULL,
10309					 /*parenthesized_p=*/NULL,
10310					 /*member_p=*/false);
10311
10312      /* If we are parsing an abstract-declarator, we must handle the
10313	 case where the dependent declarator is absent.  */
10314      if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10315	  && !cp_parser_parse_definitely (parser))
10316	declarator = NULL_TREE;
10317
10318      /* Build the representation of the ptr-operator.  */
10319      if (code == INDIRECT_REF)
10320	declarator = make_pointer_declarator (cv_qualifier_seq,
10321					      declarator);
10322      else
10323	declarator = make_reference_declarator (cv_qualifier_seq,
10324						declarator);
10325      /* Handle the pointer-to-member case.  */
10326      if (class_type)
10327	declarator = build_nt (SCOPE_REF, class_type, declarator);
10328    }
10329  /* Everything else is a direct-declarator.  */
10330  else
10331    {
10332      if (parenthesized_p)
10333	*parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10334						   CPP_OPEN_PAREN);
10335      declarator = cp_parser_direct_declarator (parser, dcl_kind,
10336						ctor_dtor_or_conv_p,
10337						member_p);
10338    }
10339
10340  if (attributes && declarator != error_mark_node)
10341    declarator = tree_cons (attributes, declarator, NULL_TREE);
10342
10343  return declarator;
10344}
10345
10346/* Parse a direct-declarator or direct-abstract-declarator.
10347
10348   direct-declarator:
10349     declarator-id
10350     direct-declarator ( parameter-declaration-clause )
10351       cv-qualifier-seq [opt]
10352       exception-specification [opt]
10353     direct-declarator [ constant-expression [opt] ]
10354     ( declarator )
10355
10356   direct-abstract-declarator:
10357     direct-abstract-declarator [opt]
10358       ( parameter-declaration-clause )
10359       cv-qualifier-seq [opt]
10360       exception-specification [opt]
10361     direct-abstract-declarator [opt] [ constant-expression [opt] ]
10362     ( abstract-declarator )
10363
10364   Returns a representation of the declarator.  DCL_KIND is
10365   CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10366   direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10367   we are parsing a direct-declarator.  It is
10368   CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10369   of ambiguity we prefer an abstract declarator, as per
10370   [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10371   cp_parser_declarator.
10372
10373   For the declarator-id production, the representation is as for an
10374   id-expression, except that a qualified name is represented as a
10375   SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
10376   see the documentation of the FUNCTION_DECLARATOR_* macros for
10377   information about how to find the various declarator components.
10378   An array-declarator is represented as an ARRAY_REF.  The
10379   direct-declarator is the first operand; the constant-expression
10380   indicating the size of the array is the second operand.  */
10381
10382static tree
10383cp_parser_direct_declarator (cp_parser* parser,
10384                             cp_parser_declarator_kind dcl_kind,
10385                             int* ctor_dtor_or_conv_p,
10386			     bool member_p)
10387{
10388  cp_token *token;
10389  tree declarator = NULL_TREE;
10390  tree scope = NULL_TREE;
10391  bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10392  bool saved_in_declarator_p = parser->in_declarator_p;
10393  bool first = true;
10394  bool pop_p = false;
10395
10396  while (true)
10397    {
10398      /* Peek at the next token.  */
10399      token = cp_lexer_peek_token (parser->lexer);
10400      if (token->type == CPP_OPEN_PAREN)
10401	{
10402	  /* This is either a parameter-declaration-clause, or a
10403  	     parenthesized declarator. When we know we are parsing a
10404  	     named declarator, it must be a parenthesized declarator
10405  	     if FIRST is true. For instance, `(int)' is a
10406  	     parameter-declaration-clause, with an omitted
10407  	     direct-abstract-declarator. But `((*))', is a
10408  	     parenthesized abstract declarator. Finally, when T is a
10409  	     template parameter `(T)' is a
10410  	     parameter-declaration-clause, and not a parenthesized
10411  	     named declarator.
10412
10413	     We first try and parse a parameter-declaration-clause,
10414	     and then try a nested declarator (if FIRST is true).
10415
10416	     It is not an error for it not to be a
10417	     parameter-declaration-clause, even when FIRST is
10418	     false. Consider,
10419
10420	       int i (int);
10421	       int i (3);
10422
10423	     The first is the declaration of a function while the
10424	     second is a the definition of a variable, including its
10425	     initializer.
10426
10427	     Having seen only the parenthesis, we cannot know which of
10428	     these two alternatives should be selected.  Even more
10429	     complex are examples like:
10430
10431               int i (int (a));
10432	       int i (int (3));
10433
10434	     The former is a function-declaration; the latter is a
10435	     variable initialization.
10436
10437	     Thus again, we try a parameter-declaration-clause, and if
10438	     that fails, we back out and return.  */
10439
10440	  if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10441	    {
10442	      tree params;
10443	      unsigned saved_num_template_parameter_lists;
10444
10445	      /* In a member-declarator, the only valid interpretation
10446		 of a parenthesis is the start of a
10447		 parameter-declaration-clause.  (It is invalid to
10448		 initialize a static data member with a parenthesized
10449		 initializer; only the "=" form of initialization is
10450		 permitted.)  */
10451	      if (!member_p)
10452		cp_parser_parse_tentatively (parser);
10453
10454	      /* Consume the `('.  */
10455	      cp_lexer_consume_token (parser->lexer);
10456	      if (first)
10457		{
10458		  /* If this is going to be an abstract declarator, we're
10459		     in a declarator and we can't have default args.  */
10460		  parser->default_arg_ok_p = false;
10461		  parser->in_declarator_p = true;
10462		}
10463
10464	      /* Inside the function parameter list, surrounding
10465		 template-parameter-lists do not apply.  */
10466	      saved_num_template_parameter_lists
10467		= parser->num_template_parameter_lists;
10468	      parser->num_template_parameter_lists = 0;
10469
10470	      /* Parse the parameter-declaration-clause.  */
10471	      params = cp_parser_parameter_declaration_clause (parser);
10472
10473	      parser->num_template_parameter_lists
10474		= saved_num_template_parameter_lists;
10475
10476	      /* If all went well, parse the cv-qualifier-seq and the
10477	     	 exception-specification.  */
10478	      if (member_p || cp_parser_parse_definitely (parser))
10479		{
10480		  tree cv_qualifiers;
10481		  tree exception_specification;
10482
10483		  if (ctor_dtor_or_conv_p)
10484		    *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10485		  first = false;
10486		  /* Consume the `)'.  */
10487		  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10488
10489		  /* Parse the cv-qualifier-seq.  */
10490		  cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10491		  /* And the exception-specification.  */
10492		  exception_specification
10493		    = cp_parser_exception_specification_opt (parser);
10494
10495		  /* Create the function-declarator.  */
10496		  declarator = make_call_declarator (declarator,
10497						     params,
10498						     cv_qualifiers,
10499						     exception_specification);
10500		  /* Any subsequent parameter lists are to do with
10501	 	     return type, so are not those of the declared
10502	 	     function.  */
10503		  parser->default_arg_ok_p = false;
10504
10505		  /* Repeat the main loop.  */
10506		  continue;
10507		}
10508	    }
10509
10510	  /* If this is the first, we can try a parenthesized
10511	     declarator.  */
10512	  if (first)
10513	    {
10514	      bool saved_in_type_id_in_expr_p;
10515
10516	      parser->default_arg_ok_p = saved_default_arg_ok_p;
10517	      parser->in_declarator_p = saved_in_declarator_p;
10518
10519	      /* Consume the `('.  */
10520	      cp_lexer_consume_token (parser->lexer);
10521	      /* Parse the nested declarator.  */
10522	      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10523	      parser->in_type_id_in_expr_p = true;
10524	      declarator
10525		= cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10526					/*parenthesized_p=*/NULL,
10527					member_p);
10528	      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10529	      first = false;
10530	      /* Expect a `)'.  */
10531	      if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10532		declarator = error_mark_node;
10533	      if (declarator == error_mark_node)
10534		break;
10535
10536	      goto handle_declarator;
10537	    }
10538	  /* Otherwise, we must be done.  */
10539	  else
10540	    break;
10541	}
10542      else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10543	       && token->type == CPP_OPEN_SQUARE)
10544	{
10545	  /* Parse an array-declarator.  */
10546	  tree bounds;
10547
10548	  if (ctor_dtor_or_conv_p)
10549	    *ctor_dtor_or_conv_p = 0;
10550
10551	  first = false;
10552	  parser->default_arg_ok_p = false;
10553	  parser->in_declarator_p = true;
10554	  /* Consume the `['.  */
10555	  cp_lexer_consume_token (parser->lexer);
10556	  /* Peek at the next token.  */
10557	  token = cp_lexer_peek_token (parser->lexer);
10558	  /* If the next token is `]', then there is no
10559	     constant-expression.  */
10560	  if (token->type != CPP_CLOSE_SQUARE)
10561	    {
10562	      bool non_constant_p;
10563
10564	      bounds
10565		= cp_parser_constant_expression (parser,
10566						 /*allow_non_constant=*/true,
10567						 &non_constant_p);
10568	      if (!non_constant_p)
10569		bounds = fold_non_dependent_expr (bounds);
10570	    }
10571	  else
10572	    bounds = NULL_TREE;
10573	  /* Look for the closing `]'.  */
10574	  if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10575	    {
10576	      declarator = error_mark_node;
10577	      break;
10578	    }
10579
10580	  declarator = build_nt (ARRAY_REF, declarator, bounds);
10581	}
10582      else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10583	{
10584	  /* Parse a declarator-id */
10585	  if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10586	    cp_parser_parse_tentatively (parser);
10587	  declarator = cp_parser_declarator_id (parser);
10588	  if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10589	    {
10590	      if (!cp_parser_parse_definitely (parser))
10591		declarator = error_mark_node;
10592	      else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10593		{
10594		  cp_parser_error (parser, "expected unqualified-id");
10595		  declarator = error_mark_node;
10596		}
10597	    }
10598
10599	  if (declarator == error_mark_node)
10600	    break;
10601
10602	  if (TREE_CODE (declarator) == SCOPE_REF
10603	      && !current_scope ())
10604	    {
10605	      tree scope = TREE_OPERAND (declarator, 0);
10606
10607	      /* In the declaration of a member of a template class
10608	     	 outside of the class itself, the SCOPE will sometimes
10609	     	 be a TYPENAME_TYPE.  For example, given:
10610
10611               	 template <typename T>
10612	       	 int S<T>::R::i = 3;
10613
10614             	 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
10615             	 this context, we must resolve S<T>::R to an ordinary
10616             	 type, rather than a typename type.
10617
10618	     	 The reason we normally avoid resolving TYPENAME_TYPEs
10619	     	 is that a specialization of `S' might render
10620	     	 `S<T>::R' not a type.  However, if `S' is
10621	     	 specialized, then this `i' will not be used, so there
10622	     	 is no harm in resolving the types here.  */
10623	      if (TREE_CODE (scope) == TYPENAME_TYPE)
10624		{
10625		  tree type;
10626
10627		  /* Resolve the TYPENAME_TYPE.  */
10628		  type = resolve_typename_type (scope,
10629						/*only_current_p=*/false);
10630		  /* If that failed, the declarator is invalid.  */
10631		  if (type == error_mark_node)
10632		    error ("`%T::%D' is not a type",
10633			   TYPE_CONTEXT (scope),
10634			   TYPE_IDENTIFIER (scope));
10635		  /* Build a new DECLARATOR.  */
10636		  declarator = build_nt (SCOPE_REF,
10637					 type,
10638					 TREE_OPERAND (declarator, 1));
10639		}
10640	    }
10641
10642	  /* Check to see whether the declarator-id names a constructor,
10643	     destructor, or conversion.  */
10644	  if (declarator && ctor_dtor_or_conv_p
10645	      && ((TREE_CODE (declarator) == SCOPE_REF
10646		   && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10647		  || (TREE_CODE (declarator) != SCOPE_REF
10648		      && at_class_scope_p ())))
10649	    {
10650	      tree unqualified_name;
10651	      tree class_type;
10652
10653	      /* Get the unqualified part of the name.  */
10654	      if (TREE_CODE (declarator) == SCOPE_REF)
10655		{
10656		  class_type = TREE_OPERAND (declarator, 0);
10657		  unqualified_name = TREE_OPERAND (declarator, 1);
10658		}
10659	      else
10660		{
10661		  class_type = current_class_type;
10662		  unqualified_name = declarator;
10663		}
10664
10665	      /* See if it names ctor, dtor or conv.  */
10666	      if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10667		  || IDENTIFIER_TYPENAME_P (unqualified_name)
10668		  || constructor_name_p (unqualified_name, class_type)
10669		  || (TREE_CODE (unqualified_name) == TYPE_DECL
10670		      && same_type_p (TREE_TYPE (unqualified_name),
10671				      class_type)))
10672		*ctor_dtor_or_conv_p = -1;
10673	    }
10674
10675	handle_declarator:;
10676	  scope = get_scope_of_declarator (declarator);
10677	  if (scope)
10678	    /* Any names that appear after the declarator-id for a
10679	       member are looked up in the containing scope.  */
10680	    pop_p = push_scope (scope);
10681	  parser->in_declarator_p = true;
10682	  if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10683	      || (declarator
10684		  && (TREE_CODE (declarator) == SCOPE_REF
10685		      || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10686	    /* Default args are only allowed on function
10687	       declarations.  */
10688	    parser->default_arg_ok_p = saved_default_arg_ok_p;
10689	  else
10690	    parser->default_arg_ok_p = false;
10691
10692	  first = false;
10693	}
10694      /* We're done.  */
10695      else
10696	break;
10697    }
10698
10699  /* For an abstract declarator, we might wind up with nothing at this
10700     point.  That's an error; the declarator is not optional.  */
10701  if (!declarator)
10702    cp_parser_error (parser, "expected declarator");
10703
10704  /* If we entered a scope, we must exit it now.  */
10705  if (pop_p)
10706    pop_scope (scope);
10707
10708  parser->default_arg_ok_p = saved_default_arg_ok_p;
10709  parser->in_declarator_p = saved_in_declarator_p;
10710
10711  return declarator;
10712}
10713
10714/* Parse a ptr-operator.
10715
10716   ptr-operator:
10717     * cv-qualifier-seq [opt]
10718     &
10719     :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10720
10721   GNU Extension:
10722
10723   ptr-operator:
10724     & cv-qualifier-seq [opt]
10725
10726   Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10727   used.  Returns ADDR_EXPR if a reference was used.  In the
10728   case of a pointer-to-member, *TYPE is filled in with the
10729   TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10730   with the cv-qualifier-seq, or NULL_TREE, if there are no
10731   cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10732
10733static enum tree_code
10734cp_parser_ptr_operator (cp_parser* parser,
10735                        tree* type,
10736                        tree* cv_qualifier_seq)
10737{
10738  enum tree_code code = ERROR_MARK;
10739  cp_token *token;
10740
10741  /* Assume that it's not a pointer-to-member.  */
10742  *type = NULL_TREE;
10743  /* And that there are no cv-qualifiers.  */
10744  *cv_qualifier_seq = NULL_TREE;
10745
10746  /* Peek at the next token.  */
10747  token = cp_lexer_peek_token (parser->lexer);
10748  /* If it's a `*' or `&' we have a pointer or reference.  */
10749  if (token->type == CPP_MULT || token->type == CPP_AND)
10750    {
10751      /* Remember which ptr-operator we were processing.  */
10752      code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10753
10754      /* Consume the `*' or `&'.  */
10755      cp_lexer_consume_token (parser->lexer);
10756
10757      /* A `*' can be followed by a cv-qualifier-seq, and so can a
10758	 `&', if we are allowing GNU extensions.  (The only qualifier
10759	 that can legally appear after `&' is `restrict', but that is
10760	 enforced during semantic analysis.  */
10761      if (code == INDIRECT_REF
10762	  || cp_parser_allow_gnu_extensions_p (parser))
10763	*cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10764    }
10765  else
10766    {
10767      /* Try the pointer-to-member case.  */
10768      cp_parser_parse_tentatively (parser);
10769      /* Look for the optional `::' operator.  */
10770      cp_parser_global_scope_opt (parser,
10771				  /*current_scope_valid_p=*/false);
10772      /* Look for the nested-name specifier.  */
10773      cp_parser_nested_name_specifier (parser,
10774				       /*typename_keyword_p=*/false,
10775				       /*check_dependency_p=*/true,
10776				       /*type_p=*/false,
10777				       /*is_declaration=*/false);
10778      /* If we found it, and the next token is a `*', then we are
10779	 indeed looking at a pointer-to-member operator.  */
10780      if (!cp_parser_error_occurred (parser)
10781	  && cp_parser_require (parser, CPP_MULT, "`*'"))
10782	{
10783	  /* The type of which the member is a member is given by the
10784	     current SCOPE.  */
10785	  *type = parser->scope;
10786	  /* The next name will not be qualified.  */
10787	  parser->scope = NULL_TREE;
10788	  parser->qualifying_scope = NULL_TREE;
10789	  parser->object_scope = NULL_TREE;
10790	  /* Indicate that the `*' operator was used.  */
10791	  code = INDIRECT_REF;
10792	  /* Look for the optional cv-qualifier-seq.  */
10793	  *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10794	}
10795      /* If that didn't work we don't have a ptr-operator.  */
10796      if (!cp_parser_parse_definitely (parser))
10797	cp_parser_error (parser, "expected ptr-operator");
10798    }
10799
10800  return code;
10801}
10802
10803/* Parse an (optional) cv-qualifier-seq.
10804
10805   cv-qualifier-seq:
10806     cv-qualifier cv-qualifier-seq [opt]
10807
10808   Returns a TREE_LIST.  The TREE_VALUE of each node is the
10809   representation of a cv-qualifier.  */
10810
10811static tree
10812cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10813{
10814  tree cv_qualifiers = NULL_TREE;
10815
10816  while (true)
10817    {
10818      tree cv_qualifier;
10819
10820      /* Look for the next cv-qualifier.  */
10821      cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10822      /* If we didn't find one, we're done.  */
10823      if (!cv_qualifier)
10824	break;
10825
10826      /* Add this cv-qualifier to the list.  */
10827      cv_qualifiers
10828	= tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10829    }
10830
10831  /* We built up the list in reverse order.  */
10832  return nreverse (cv_qualifiers);
10833}
10834
10835/* Parse an (optional) cv-qualifier.
10836
10837   cv-qualifier:
10838     const
10839     volatile
10840
10841   GNU Extension:
10842
10843   cv-qualifier:
10844     __restrict__ */
10845
10846static tree
10847cp_parser_cv_qualifier_opt (cp_parser* parser)
10848{
10849  cp_token *token;
10850  tree cv_qualifier = NULL_TREE;
10851
10852  /* Peek at the next token.  */
10853  token = cp_lexer_peek_token (parser->lexer);
10854  /* See if it's a cv-qualifier.  */
10855  switch (token->keyword)
10856    {
10857    case RID_CONST:
10858    case RID_VOLATILE:
10859    case RID_RESTRICT:
10860      /* Save the value of the token.  */
10861      cv_qualifier = token->value;
10862      /* Consume the token.  */
10863      cp_lexer_consume_token (parser->lexer);
10864      break;
10865
10866    default:
10867      break;
10868    }
10869
10870  return cv_qualifier;
10871}
10872
10873/* Parse a declarator-id.
10874
10875   declarator-id:
10876     id-expression
10877     :: [opt] nested-name-specifier [opt] type-name
10878
10879   In the `id-expression' case, the value returned is as for
10880   cp_parser_id_expression if the id-expression was an unqualified-id.
10881   If the id-expression was a qualified-id, then a SCOPE_REF is
10882   returned.  The first operand is the scope (either a NAMESPACE_DECL
10883   or TREE_TYPE), but the second is still just a representation of an
10884   unqualified-id.  */
10885
10886static tree
10887cp_parser_declarator_id (cp_parser* parser)
10888{
10889  tree id_expression;
10890
10891  /* The expression must be an id-expression.  Assume that qualified
10892     names are the names of types so that:
10893
10894       template <class T>
10895       int S<T>::R::i = 3;
10896
10897     will work; we must treat `S<T>::R' as the name of a type.
10898     Similarly, assume that qualified names are templates, where
10899     required, so that:
10900
10901       template <class T>
10902       int S<T>::R<T>::i = 3;
10903
10904     will work, too.  */
10905  id_expression = cp_parser_id_expression (parser,
10906					   /*template_keyword_p=*/false,
10907					   /*check_dependency_p=*/false,
10908					   /*template_p=*/NULL,
10909					   /*declarator_p=*/true);
10910  /* If the name was qualified, create a SCOPE_REF to represent
10911     that.  */
10912  if (parser->scope)
10913    {
10914      id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10915      parser->scope = NULL_TREE;
10916    }
10917
10918  return id_expression;
10919}
10920
10921/* Parse a type-id.
10922
10923   type-id:
10924     type-specifier-seq abstract-declarator [opt]
10925
10926   Returns the TYPE specified.  */
10927
10928static tree
10929cp_parser_type_id (cp_parser* parser)
10930{
10931  tree type_specifier_seq;
10932  tree abstract_declarator;
10933
10934  /* Parse the type-specifier-seq.  */
10935  type_specifier_seq
10936    = cp_parser_type_specifier_seq (parser);
10937  if (type_specifier_seq == error_mark_node)
10938    return error_mark_node;
10939
10940  /* There might or might not be an abstract declarator.  */
10941  cp_parser_parse_tentatively (parser);
10942  /* Look for the declarator.  */
10943  abstract_declarator
10944    = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10945			    /*parenthesized_p=*/NULL,
10946			    /*member_p=*/false);
10947  /* Check to see if there really was a declarator.  */
10948  if (!cp_parser_parse_definitely (parser))
10949    abstract_declarator = NULL_TREE;
10950
10951  return groktypename (build_tree_list (type_specifier_seq,
10952					abstract_declarator));
10953}
10954
10955/* Parse a type-specifier-seq.
10956
10957   type-specifier-seq:
10958     type-specifier type-specifier-seq [opt]
10959
10960   GNU extension:
10961
10962   type-specifier-seq:
10963     attributes type-specifier-seq [opt]
10964
10965   Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10966   type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10967
10968static tree
10969cp_parser_type_specifier_seq (cp_parser* parser)
10970{
10971  bool seen_type_specifier = false;
10972  tree type_specifier_seq = NULL_TREE;
10973
10974  /* Parse the type-specifiers and attributes.  */
10975  while (true)
10976    {
10977      tree type_specifier;
10978
10979      /* Check for attributes first.  */
10980      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10981	{
10982	  type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10983					  NULL_TREE,
10984					  type_specifier_seq);
10985	  continue;
10986	}
10987
10988      /* After the first type-specifier, others are optional.  */
10989      if (seen_type_specifier)
10990	cp_parser_parse_tentatively (parser);
10991      /* Look for the type-specifier.  */
10992      type_specifier = cp_parser_type_specifier (parser,
10993						 CP_PARSER_FLAGS_NONE,
10994						 /*is_friend=*/false,
10995						 /*is_declaration=*/false,
10996						 NULL,
10997						 NULL);
10998      /* If the first type-specifier could not be found, this is not a
10999	 type-specifier-seq at all.  */
11000      if (!seen_type_specifier && type_specifier == error_mark_node)
11001	return error_mark_node;
11002      /* If subsequent type-specifiers could not be found, the
11003	 type-specifier-seq is complete.  */
11004      else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
11005	break;
11006
11007      /* Add the new type-specifier to the list.  */
11008      type_specifier_seq
11009	= tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
11010      seen_type_specifier = true;
11011    }
11012
11013  /* We built up the list in reverse order.  */
11014  return nreverse (type_specifier_seq);
11015}
11016
11017/* Parse a parameter-declaration-clause.
11018
11019   parameter-declaration-clause:
11020     parameter-declaration-list [opt] ... [opt]
11021     parameter-declaration-list , ...
11022
11023   Returns a representation for the parameter declarations.  Each node
11024   is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
11025   representation.)  If the parameter-declaration-clause ends with an
11026   ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
11027   list.  A return value of NULL_TREE indicates a
11028   parameter-declaration-clause consisting only of an ellipsis.  */
11029
11030static tree
11031cp_parser_parameter_declaration_clause (cp_parser* parser)
11032{
11033  tree parameters;
11034  cp_token *token;
11035  bool ellipsis_p;
11036
11037  /* Peek at the next token.  */
11038  token = cp_lexer_peek_token (parser->lexer);
11039  /* Check for trivial parameter-declaration-clauses.  */
11040  if (token->type == CPP_ELLIPSIS)
11041    {
11042      /* Consume the `...' token.  */
11043      cp_lexer_consume_token (parser->lexer);
11044      return NULL_TREE;
11045    }
11046  else if (token->type == CPP_CLOSE_PAREN)
11047    /* There are no parameters.  */
11048    {
11049#ifndef NO_IMPLICIT_EXTERN_C
11050      if (in_system_header && current_class_type == NULL
11051	  && current_lang_name == lang_name_c)
11052	return NULL_TREE;
11053      else
11054#endif
11055	return void_list_node;
11056    }
11057  /* Check for `(void)', too, which is a special case.  */
11058  else if (token->keyword == RID_VOID
11059	   && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11060	       == CPP_CLOSE_PAREN))
11061    {
11062      /* Consume the `void' token.  */
11063      cp_lexer_consume_token (parser->lexer);
11064      /* There are no parameters.  */
11065      return void_list_node;
11066    }
11067
11068  /* Parse the parameter-declaration-list.  */
11069  parameters = cp_parser_parameter_declaration_list (parser);
11070  /* If a parse error occurred while parsing the
11071     parameter-declaration-list, then the entire
11072     parameter-declaration-clause is erroneous.  */
11073  if (parameters == error_mark_node)
11074    return error_mark_node;
11075
11076  /* Peek at the next token.  */
11077  token = cp_lexer_peek_token (parser->lexer);
11078  /* If it's a `,', the clause should terminate with an ellipsis.  */
11079  if (token->type == CPP_COMMA)
11080    {
11081      /* Consume the `,'.  */
11082      cp_lexer_consume_token (parser->lexer);
11083      /* Expect an ellipsis.  */
11084      ellipsis_p
11085	= (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11086    }
11087  /* It might also be `...' if the optional trailing `,' was
11088     omitted.  */
11089  else if (token->type == CPP_ELLIPSIS)
11090    {
11091      /* Consume the `...' token.  */
11092      cp_lexer_consume_token (parser->lexer);
11093      /* And remember that we saw it.  */
11094      ellipsis_p = true;
11095    }
11096  else
11097    ellipsis_p = false;
11098
11099  /* Finish the parameter list.  */
11100  return finish_parmlist (parameters, ellipsis_p);
11101}
11102
11103/* Parse a parameter-declaration-list.
11104
11105   parameter-declaration-list:
11106     parameter-declaration
11107     parameter-declaration-list , parameter-declaration
11108
11109   Returns a representation of the parameter-declaration-list, as for
11110   cp_parser_parameter_declaration_clause.  However, the
11111   `void_list_node' is never appended to the list.  */
11112
11113static tree
11114cp_parser_parameter_declaration_list (cp_parser* parser)
11115{
11116  tree parameters = NULL_TREE;
11117
11118  /* Look for more parameters.  */
11119  while (true)
11120    {
11121      tree parameter;
11122      bool parenthesized_p;
11123      /* Parse the parameter.  */
11124      parameter
11125	= cp_parser_parameter_declaration (parser,
11126					   /*template_parm_p=*/false,
11127					   &parenthesized_p);
11128
11129      /* If a parse error occurred parsing the parameter declaration,
11130	 then the entire parameter-declaration-list is erroneous.  */
11131      if (parameter == error_mark_node)
11132	{
11133	  parameters = error_mark_node;
11134	  break;
11135	}
11136      /* Add the new parameter to the list.  */
11137      TREE_CHAIN (parameter) = parameters;
11138      parameters = parameter;
11139
11140      /* Peek at the next token.  */
11141      if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11142	  || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11143	/* The parameter-declaration-list is complete.  */
11144	break;
11145      else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11146	{
11147	  cp_token *token;
11148
11149	  /* Peek at the next token.  */
11150	  token = cp_lexer_peek_nth_token (parser->lexer, 2);
11151	  /* If it's an ellipsis, then the list is complete.  */
11152	  if (token->type == CPP_ELLIPSIS)
11153	    break;
11154	  /* Otherwise, there must be more parameters.  Consume the
11155	     `,'.  */
11156	  cp_lexer_consume_token (parser->lexer);
11157	  /* When parsing something like:
11158
11159	        int i(float f, double d)
11160
11161             we can tell after seeing the declaration for "f" that we
11162	     are not looking at an initialization of a variable "i",
11163	     but rather at the declaration of a function "i".
11164
11165	     Due to the fact that the parsing of template arguments
11166	     (as specified to a template-id) requires backtracking we
11167	     cannot use this technique when inside a template argument
11168	     list.  */
11169	  if (!parser->in_template_argument_list_p
11170	      && !parser->in_type_id_in_expr_p
11171	      && cp_parser_parsing_tentatively (parser)
11172	      && !cp_parser_committed_to_tentative_parse (parser)
11173	      /* However, a parameter-declaration of the form
11174		 "foat(f)" (which is a valid declaration of a
11175		 parameter "f") can also be interpreted as an
11176		 expression (the conversion of "f" to "float").  */
11177	      && !parenthesized_p)
11178	    cp_parser_commit_to_tentative_parse (parser);
11179	}
11180      else
11181	{
11182	  cp_parser_error (parser, "expected `,' or `...'");
11183	  if (!cp_parser_parsing_tentatively (parser)
11184	      || cp_parser_committed_to_tentative_parse (parser))
11185	    cp_parser_skip_to_closing_parenthesis (parser,
11186						   /*recovering=*/true,
11187						   /*or_comma=*/false,
11188						   /*consume_paren=*/false);
11189	  break;
11190	}
11191    }
11192
11193  /* We built up the list in reverse order; straighten it out now.  */
11194  return nreverse (parameters);
11195}
11196
11197/* Parse a parameter declaration.
11198
11199   parameter-declaration:
11200     decl-specifier-seq declarator
11201     decl-specifier-seq declarator = assignment-expression
11202     decl-specifier-seq abstract-declarator [opt]
11203     decl-specifier-seq abstract-declarator [opt] = assignment-expression
11204
11205   If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11206   declares a template parameter.  (In that case, a non-nested `>'
11207   token encountered during the parsing of the assignment-expression
11208   is not interpreted as a greater-than operator.)
11209
11210   Returns a TREE_LIST representing the parameter-declaration.  The
11211   TREE_PURPOSE is the default argument expression, or NULL_TREE if
11212   there is no default argument.  The TREE_VALUE is a representation
11213   of the decl-specifier-seq and declarator.  In particular, the
11214   TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11215   decl-specifier-seq and whose TREE_VALUE represents the declarator.
11216   If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11217   the declarator is of the form "(p)".  */
11218
11219static tree
11220cp_parser_parameter_declaration (cp_parser *parser,
11221				 bool template_parm_p,
11222				 bool *parenthesized_p)
11223{
11224  int declares_class_or_enum;
11225  bool greater_than_is_operator_p;
11226  tree decl_specifiers;
11227  tree attributes;
11228  tree declarator;
11229  tree default_argument;
11230  tree parameter;
11231  cp_token *token;
11232  const char *saved_message;
11233
11234  /* In a template parameter, `>' is not an operator.
11235
11236     [temp.param]
11237
11238     When parsing a default template-argument for a non-type
11239     template-parameter, the first non-nested `>' is taken as the end
11240     of the template parameter-list rather than a greater-than
11241     operator.  */
11242  greater_than_is_operator_p = !template_parm_p;
11243
11244  /* Type definitions may not appear in parameter types.  */
11245  saved_message = parser->type_definition_forbidden_message;
11246  parser->type_definition_forbidden_message
11247    = "types may not be defined in parameter types";
11248
11249  /* Parse the declaration-specifiers.  */
11250  decl_specifiers
11251    = cp_parser_decl_specifier_seq (parser,
11252				    CP_PARSER_FLAGS_NONE,
11253				    &attributes,
11254				    &declares_class_or_enum);
11255  /* If an error occurred, there's no reason to attempt to parse the
11256     rest of the declaration.  */
11257  if (cp_parser_error_occurred (parser))
11258    {
11259      parser->type_definition_forbidden_message = saved_message;
11260      return error_mark_node;
11261    }
11262
11263  /* Peek at the next token.  */
11264  token = cp_lexer_peek_token (parser->lexer);
11265  /* If the next token is a `)', `,', `=', `>', or `...', then there
11266     is no declarator.  */
11267  if (token->type == CPP_CLOSE_PAREN
11268      || token->type == CPP_COMMA
11269      || token->type == CPP_EQ
11270      || token->type == CPP_ELLIPSIS
11271      || token->type == CPP_GREATER)
11272    {
11273      declarator = NULL_TREE;
11274      if (parenthesized_p)
11275	*parenthesized_p = false;
11276    }
11277  /* Otherwise, there should be a declarator.  */
11278  else
11279    {
11280      bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11281      parser->default_arg_ok_p = false;
11282
11283      /* After seeing a decl-specifier-seq, if the next token is not a
11284	 "(", there is no possibility that the code is a valid
11285	 expression.  Therefore, if parsing tentatively, we commit at
11286	 this point.  */
11287      if (!parser->in_template_argument_list_p
11288	  /* In an expression context, having seen:
11289
11290	       (int((char ...
11291
11292	     we cannot be sure whether we are looking at a
11293	     function-type (taking a "char" as a parameter) or a cast
11294	     of some object of type "char" to "int".  */
11295	  && !parser->in_type_id_in_expr_p
11296	  && cp_parser_parsing_tentatively (parser)
11297	  && !cp_parser_committed_to_tentative_parse (parser)
11298	  && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11299	cp_parser_commit_to_tentative_parse (parser);
11300      /* Parse the declarator.  */
11301      declarator = cp_parser_declarator (parser,
11302					 CP_PARSER_DECLARATOR_EITHER,
11303					 /*ctor_dtor_or_conv_p=*/NULL,
11304					 parenthesized_p,
11305					 /*member_p=*/false);
11306      parser->default_arg_ok_p = saved_default_arg_ok_p;
11307      /* After the declarator, allow more attributes.  */
11308      attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11309    }
11310
11311  /* The restriction on defining new types applies only to the type
11312     of the parameter, not to the default argument.  */
11313  parser->type_definition_forbidden_message = saved_message;
11314
11315  /* If the next token is `=', then process a default argument.  */
11316  if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11317    {
11318      bool saved_greater_than_is_operator_p;
11319      /* Consume the `='.  */
11320      cp_lexer_consume_token (parser->lexer);
11321
11322      /* If we are defining a class, then the tokens that make up the
11323	 default argument must be saved and processed later.  */
11324      if (!template_parm_p && at_class_scope_p ()
11325	  && TYPE_BEING_DEFINED (current_class_type))
11326	{
11327	  unsigned depth = 0;
11328
11329	  /* Create a DEFAULT_ARG to represented the unparsed default
11330             argument.  */
11331	  default_argument = make_node (DEFAULT_ARG);
11332	  DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11333
11334	  /* Add tokens until we have processed the entire default
11335	     argument.  */
11336	  while (true)
11337	    {
11338	      bool done = false;
11339	      cp_token *token;
11340
11341	      /* Peek at the next token.  */
11342	      token = cp_lexer_peek_token (parser->lexer);
11343	      /* What we do depends on what token we have.  */
11344	      switch (token->type)
11345		{
11346		  /* In valid code, a default argument must be
11347		     immediately followed by a `,' `)', or `...'.  */
11348		case CPP_COMMA:
11349		case CPP_CLOSE_PAREN:
11350		case CPP_ELLIPSIS:
11351		  /* If we run into a non-nested `;', `}', or `]',
11352		     then the code is invalid -- but the default
11353		     argument is certainly over.  */
11354		case CPP_SEMICOLON:
11355		case CPP_CLOSE_BRACE:
11356		case CPP_CLOSE_SQUARE:
11357		  if (depth == 0)
11358		    done = true;
11359		  /* Update DEPTH, if necessary.  */
11360		  else if (token->type == CPP_CLOSE_PAREN
11361			   || token->type == CPP_CLOSE_BRACE
11362			   || token->type == CPP_CLOSE_SQUARE)
11363		    --depth;
11364		  break;
11365
11366		case CPP_OPEN_PAREN:
11367		case CPP_OPEN_SQUARE:
11368		case CPP_OPEN_BRACE:
11369		  ++depth;
11370		  break;
11371
11372		case CPP_GREATER:
11373		  /* If we see a non-nested `>', and `>' is not an
11374		     operator, then it marks the end of the default
11375		     argument.  */
11376		  if (!depth && !greater_than_is_operator_p)
11377		    done = true;
11378		  break;
11379
11380		  /* If we run out of tokens, issue an error message.  */
11381		case CPP_EOF:
11382		  error ("file ends in default argument");
11383		  done = true;
11384		  break;
11385
11386		case CPP_NAME:
11387		case CPP_SCOPE:
11388		  /* In these cases, we should look for template-ids.
11389		     For example, if the default argument is
11390		     `X<int, double>()', we need to do name lookup to
11391		     figure out whether or not `X' is a template; if
11392		     so, the `,' does not end the default argument.
11393
11394		     That is not yet done.  */
11395		  break;
11396
11397		default:
11398		  break;
11399		}
11400
11401	      /* If we've reached the end, stop.  */
11402	      if (done)
11403		break;
11404
11405	      /* Add the token to the token block.  */
11406	      token = cp_lexer_consume_token (parser->lexer);
11407	      cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11408					 token);
11409	    }
11410	}
11411      /* Outside of a class definition, we can just parse the
11412         assignment-expression.  */
11413      else
11414	{
11415	  bool saved_local_variables_forbidden_p;
11416
11417	  /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11418	     set correctly.  */
11419	  saved_greater_than_is_operator_p
11420	    = parser->greater_than_is_operator_p;
11421	  parser->greater_than_is_operator_p = greater_than_is_operator_p;
11422	  /* Local variable names (and the `this' keyword) may not
11423	     appear in a default argument.  */
11424	  saved_local_variables_forbidden_p
11425	    = parser->local_variables_forbidden_p;
11426	  parser->local_variables_forbidden_p = true;
11427	  /* Parse the assignment-expression.  */
11428	  default_argument = cp_parser_assignment_expression (parser);
11429	  /* Restore saved state.  */
11430	  parser->greater_than_is_operator_p
11431	    = saved_greater_than_is_operator_p;
11432	  parser->local_variables_forbidden_p
11433	    = saved_local_variables_forbidden_p;
11434	}
11435      if (!parser->default_arg_ok_p)
11436	{
11437	  if (!flag_pedantic_errors)
11438	    warning ("deprecated use of default argument for parameter of non-function");
11439	  else
11440	    {
11441	      error ("default arguments are only permitted for function parameters");
11442	      default_argument = NULL_TREE;
11443	    }
11444	}
11445    }
11446  else
11447    default_argument = NULL_TREE;
11448
11449  /* Create the representation of the parameter.  */
11450  if (attributes)
11451    decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11452  parameter = build_tree_list (default_argument,
11453			       build_tree_list (decl_specifiers,
11454						declarator));
11455
11456  return parameter;
11457}
11458
11459/* Parse a function-body.
11460
11461   function-body:
11462     compound_statement  */
11463
11464static void
11465cp_parser_function_body (cp_parser *parser)
11466{
11467  cp_parser_compound_statement (parser, false);
11468}
11469
11470/* Parse a ctor-initializer-opt followed by a function-body.  Return
11471   true if a ctor-initializer was present.  */
11472
11473static bool
11474cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11475{
11476  tree body;
11477  bool ctor_initializer_p;
11478
11479  /* Begin the function body.  */
11480  body = begin_function_body ();
11481  /* Parse the optional ctor-initializer.  */
11482  ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11483  /* Parse the function-body.  */
11484  cp_parser_function_body (parser);
11485  /* Finish the function body.  */
11486  finish_function_body (body);
11487
11488  return ctor_initializer_p;
11489}
11490
11491/* Parse an initializer.
11492
11493   initializer:
11494     = initializer-clause
11495     ( expression-list )
11496
11497   Returns a expression representing the initializer.  If no
11498   initializer is present, NULL_TREE is returned.
11499
11500   *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11501   production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11502   set to FALSE if there is no initializer present.  If there is an
11503   initializer, and it is not a constant-expression, *NON_CONSTANT_P
11504   is set to true; otherwise it is set to false.  */
11505
11506static tree
11507cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11508		       bool* non_constant_p)
11509{
11510  cp_token *token;
11511  tree init;
11512
11513  /* Peek at the next token.  */
11514  token = cp_lexer_peek_token (parser->lexer);
11515
11516  /* Let our caller know whether or not this initializer was
11517     parenthesized.  */
11518  *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11519  /* Assume that the initializer is constant.  */
11520  *non_constant_p = false;
11521
11522  if (token->type == CPP_EQ)
11523    {
11524      /* Consume the `='.  */
11525      cp_lexer_consume_token (parser->lexer);
11526      /* Parse the initializer-clause.  */
11527      init = cp_parser_initializer_clause (parser, non_constant_p);
11528    }
11529  else if (token->type == CPP_OPEN_PAREN)
11530    init = cp_parser_parenthesized_expression_list (parser, false,
11531						    non_constant_p);
11532  else
11533    {
11534      /* Anything else is an error.  */
11535      cp_parser_error (parser, "expected initializer");
11536      init = error_mark_node;
11537    }
11538
11539  return init;
11540}
11541
11542/* Parse an initializer-clause.
11543
11544   initializer-clause:
11545     assignment-expression
11546     { initializer-list , [opt] }
11547     { }
11548
11549   Returns an expression representing the initializer.
11550
11551   If the `assignment-expression' production is used the value
11552   returned is simply a representation for the expression.
11553
11554   Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11555   the elements of the initializer-list (or NULL_TREE, if the last
11556   production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11557   NULL_TREE.  There is no way to detect whether or not the optional
11558   trailing `,' was provided.  NON_CONSTANT_P is as for
11559   cp_parser_initializer.  */
11560
11561static tree
11562cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11563{
11564  tree initializer = NULL_TREE;
11565
11566  /* Assume the expression is constant.  */
11567  *non_constant_p = false;
11568
11569  /* If it is not a `{', then we are looking at an
11570     assignment-expression.  */
11571  if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11572    {
11573      /* Speed up common initializers (simply a literal).  */
11574      cp_token* token = cp_lexer_peek_token (parser->lexer);
11575      cp_token* token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11576
11577      if (token2->type == CPP_COMMA)
11578	switch (token->type)
11579	  {
11580	  case CPP_CHAR:
11581	  case CPP_WCHAR:
11582	  case CPP_NUMBER:
11583	    token = cp_lexer_consume_token (parser->lexer);
11584	    initializer = token->value;
11585	    break;
11586
11587	  case CPP_STRING:
11588	  case CPP_WSTRING:
11589	    token = cp_lexer_consume_token (parser->lexer);
11590	    if (TREE_CHAIN (token->value))
11591	      initializer = TREE_CHAIN (token->value);
11592	    else
11593	      initializer = token->value;
11594	    break;
11595
11596	  default:
11597	    break;
11598	  }
11599
11600      /* Otherwise, fall back to the generic assignment expression.  */
11601      if (!initializer)
11602	{
11603	  initializer
11604	    = cp_parser_constant_expression (parser,
11605					    /*allow_non_constant_p=*/true,
11606					    non_constant_p);
11607	  if (!*non_constant_p)
11608	    initializer = fold_non_dependent_expr (initializer);
11609	}
11610    }
11611  else
11612    {
11613      /* Consume the `{' token.  */
11614      cp_lexer_consume_token (parser->lexer);
11615      /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11616      initializer = make_node (CONSTRUCTOR);
11617      /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11618	 necessary, but check_initializer depends upon it, for
11619	 now.  */
11620      TREE_HAS_CONSTRUCTOR (initializer) = 1;
11621      /* If it's not a `}', then there is a non-trivial initializer.  */
11622      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11623	{
11624	  /* Parse the initializer list.  */
11625	  CONSTRUCTOR_ELTS (initializer)
11626	    = cp_parser_initializer_list (parser, non_constant_p);
11627	  /* A trailing `,' token is allowed.  */
11628	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11629	    cp_lexer_consume_token (parser->lexer);
11630	}
11631      /* Now, there should be a trailing `}'.  */
11632      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11633    }
11634
11635  return initializer;
11636}
11637
11638/* Parse an initializer-list.
11639
11640   initializer-list:
11641     initializer-clause
11642     initializer-list , initializer-clause
11643
11644   GNU Extension:
11645
11646   initializer-list:
11647     identifier : initializer-clause
11648     initializer-list, identifier : initializer-clause
11649
11650   Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11651   for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11652   IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
11653   as for cp_parser_initializer.  */
11654
11655static tree
11656cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11657{
11658  tree initializers = NULL_TREE;
11659
11660  /* Assume all of the expressions are constant.  */
11661  *non_constant_p = false;
11662
11663  /* Parse the rest of the list.  */
11664  while (true)
11665    {
11666      cp_token *token;
11667      tree identifier;
11668      tree initializer;
11669      bool clause_non_constant_p;
11670
11671      /* If the next token is an identifier and the following one is a
11672	 colon, we are looking at the GNU designated-initializer
11673	 syntax.  */
11674      if (cp_parser_allow_gnu_extensions_p (parser)
11675	  && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11676	  && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11677	{
11678	  /* Consume the identifier.  */
11679	  identifier = cp_lexer_consume_token (parser->lexer)->value;
11680	  /* Consume the `:'.  */
11681	  cp_lexer_consume_token (parser->lexer);
11682	}
11683      else
11684	identifier = NULL_TREE;
11685
11686      /* Parse the initializer.  */
11687      initializer = cp_parser_initializer_clause (parser,
11688						  &clause_non_constant_p);
11689      /* If any clause is non-constant, so is the entire initializer.  */
11690      if (clause_non_constant_p)
11691	*non_constant_p = true;
11692      /* Add it to the list.  */
11693      initializers = tree_cons (identifier, initializer, initializers);
11694
11695      /* If the next token is not a comma, we have reached the end of
11696	 the list.  */
11697      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11698	break;
11699
11700      /* Peek at the next token.  */
11701      token = cp_lexer_peek_nth_token (parser->lexer, 2);
11702      /* If the next token is a `}', then we're still done.  An
11703	 initializer-clause can have a trailing `,' after the
11704	 initializer-list and before the closing `}'.  */
11705      if (token->type == CPP_CLOSE_BRACE)
11706	break;
11707
11708      /* Consume the `,' token.  */
11709      cp_lexer_consume_token (parser->lexer);
11710    }
11711
11712  /* The initializers were built up in reverse order, so we need to
11713     reverse them now.  */
11714  return nreverse (initializers);
11715}
11716
11717/* Classes [gram.class] */
11718
11719/* Parse a class-name.
11720
11721   class-name:
11722     identifier
11723     template-id
11724
11725   TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11726   to indicate that names looked up in dependent types should be
11727   assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11728   keyword has been used to indicate that the name that appears next
11729   is a template.  TYPE_P is true iff the next name should be treated
11730   as class-name, even if it is declared to be some other kind of name
11731   as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11732   dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
11733   being defined in a class-head.
11734
11735   Returns the TYPE_DECL representing the class.  */
11736
11737static tree
11738cp_parser_class_name (cp_parser *parser,
11739		      bool typename_keyword_p,
11740		      bool template_keyword_p,
11741		      bool type_p,
11742		      bool check_dependency_p,
11743		      bool class_head_p,
11744		      bool is_declaration)
11745{
11746  tree decl;
11747  tree scope;
11748  bool typename_p;
11749  cp_token *token;
11750
11751  /* All class-names start with an identifier.  */
11752  token = cp_lexer_peek_token (parser->lexer);
11753  if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11754    {
11755      cp_parser_error (parser, "expected class-name");
11756      return error_mark_node;
11757    }
11758
11759  /* PARSER->SCOPE can be cleared when parsing the template-arguments
11760     to a template-id, so we save it here.  */
11761  scope = parser->scope;
11762  if (scope == error_mark_node)
11763    return error_mark_node;
11764
11765  /* Any name names a type if we're following the `typename' keyword
11766     in a qualified name where the enclosing scope is type-dependent.  */
11767  typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11768		&& dependent_type_p (scope));
11769  /* Handle the common case (an identifier, but not a template-id)
11770     efficiently.  */
11771  if (token->type == CPP_NAME
11772      && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11773    {
11774      tree identifier;
11775
11776      /* Look for the identifier.  */
11777      identifier = cp_parser_identifier (parser);
11778      /* If the next token isn't an identifier, we are certainly not
11779	 looking at a class-name.  */
11780      if (identifier == error_mark_node)
11781	decl = error_mark_node;
11782      /* If we know this is a type-name, there's no need to look it
11783	 up.  */
11784      else if (typename_p)
11785	decl = identifier;
11786      else
11787	{
11788	  /* If the next token is a `::', then the name must be a type
11789	     name.
11790
11791	     [basic.lookup.qual]
11792
11793	     During the lookup for a name preceding the :: scope
11794	     resolution operator, object, function, and enumerator
11795	     names are ignored.  */
11796	  if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11797	    type_p = true;
11798	  /* Look up the name.  */
11799	  decl = cp_parser_lookup_name (parser, identifier,
11800					type_p,
11801					/*is_template=*/false,
11802					/*is_namespace=*/false,
11803					check_dependency_p);
11804	}
11805    }
11806  else
11807    {
11808      /* Try a template-id.  */
11809      decl = cp_parser_template_id (parser, template_keyword_p,
11810				    check_dependency_p,
11811				    is_declaration);
11812      if (decl == error_mark_node)
11813	return error_mark_node;
11814    }
11815
11816  decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11817
11818  /* If this is a typename, create a TYPENAME_TYPE.  */
11819  if (typename_p && decl != error_mark_node)
11820    {
11821      decl = make_typename_type (scope, decl, /*complain=*/1);
11822      if (decl != error_mark_node)
11823	decl = TYPE_NAME (decl);
11824    }
11825
11826  /* Check to see that it is really the name of a class.  */
11827  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11828      && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11829      && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11830    /* Situations like this:
11831
11832	 template <typename T> struct A {
11833	   typename T::template X<int>::I i;
11834	 };
11835
11836       are problematic.  Is `T::template X<int>' a class-name?  The
11837       standard does not seem to be definitive, but there is no other
11838       valid interpretation of the following `::'.  Therefore, those
11839       names are considered class-names.  */
11840    decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11841  else if (decl == error_mark_node
11842	   || TREE_CODE (decl) != TYPE_DECL
11843	   || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11844    {
11845      cp_parser_error (parser, "expected class-name");
11846      return error_mark_node;
11847    }
11848
11849  return decl;
11850}
11851
11852/* Parse a class-specifier.
11853
11854   class-specifier:
11855     class-head { member-specification [opt] }
11856
11857   Returns the TREE_TYPE representing the class.  */
11858
11859static tree
11860cp_parser_class_specifier (cp_parser* parser)
11861{
11862  cp_token *token;
11863  tree type;
11864  tree attributes;
11865  int has_trailing_semicolon;
11866  bool nested_name_specifier_p;
11867  unsigned saved_num_template_parameter_lists;
11868  bool pop_p = false;
11869  tree scope = NULL_TREE;
11870
11871  push_deferring_access_checks (dk_no_deferred);
11872
11873  /* Parse the class-head.  */
11874  type = cp_parser_class_head (parser,
11875			       &nested_name_specifier_p,
11876			       &attributes);
11877  /* If the class-head was a semantic disaster, skip the entire body
11878     of the class.  */
11879  if (!type)
11880    {
11881      cp_parser_skip_to_end_of_block_or_statement (parser);
11882      pop_deferring_access_checks ();
11883      return error_mark_node;
11884    }
11885
11886  /* Look for the `{'.  */
11887  if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11888    {
11889      pop_deferring_access_checks ();
11890      return error_mark_node;
11891    }
11892
11893  /* Issue an error message if type-definitions are forbidden here.  */
11894  cp_parser_check_type_definition (parser);
11895  /* Remember that we are defining one more class.  */
11896  ++parser->num_classes_being_defined;
11897  /* Inside the class, surrounding template-parameter-lists do not
11898     apply.  */
11899  saved_num_template_parameter_lists
11900    = parser->num_template_parameter_lists;
11901  parser->num_template_parameter_lists = 0;
11902
11903  /* Start the class.  */
11904  if (nested_name_specifier_p)
11905    {
11906      scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
11907      pop_p = push_scope (scope);
11908    }
11909  type = begin_class_definition (type);
11910  if (type == error_mark_node)
11911    /* If the type is erroneous, skip the entire body of the class.  */
11912    cp_parser_skip_to_closing_brace (parser);
11913  else
11914    /* Parse the member-specification.  */
11915    cp_parser_member_specification_opt (parser);
11916  /* Look for the trailing `}'.  */
11917  cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11918  /* We get better error messages by noticing a common problem: a
11919     missing trailing `;'.  */
11920  token = cp_lexer_peek_token (parser->lexer);
11921  has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11922  /* Look for trailing attributes to apply to this class.  */
11923  if (cp_parser_allow_gnu_extensions_p (parser))
11924    {
11925      tree sub_attr = cp_parser_attributes_opt (parser);
11926      attributes = chainon (attributes, sub_attr);
11927    }
11928  if (type != error_mark_node)
11929    type = finish_struct (type, attributes);
11930  if (pop_p)
11931    pop_scope (scope);
11932  /* If this class is not itself within the scope of another class,
11933     then we need to parse the bodies of all of the queued function
11934     definitions.  Note that the queued functions defined in a class
11935     are not always processed immediately following the
11936     class-specifier for that class.  Consider:
11937
11938       struct A {
11939         struct B { void f() { sizeof (A); } };
11940       };
11941
11942     If `f' were processed before the processing of `A' were
11943     completed, there would be no way to compute the size of `A'.
11944     Note that the nesting we are interested in here is lexical --
11945     not the semantic nesting given by TYPE_CONTEXT.  In particular,
11946     for:
11947
11948       struct A { struct B; };
11949       struct A::B { void f() { } };
11950
11951     there is no need to delay the parsing of `A::B::f'.  */
11952  if (--parser->num_classes_being_defined == 0)
11953    {
11954      tree queue_entry;
11955      tree fn;
11956
11957      /* In a first pass, parse default arguments to the functions.
11958	 Then, in a second pass, parse the bodies of the functions.
11959	 This two-phased approach handles cases like:
11960
11961	    struct S {
11962              void f() { g(); }
11963              void g(int i = 3);
11964            };
11965
11966         */
11967      for (TREE_PURPOSE (parser->unparsed_functions_queues)
11968	     = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11969	   (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11970	   TREE_PURPOSE (parser->unparsed_functions_queues)
11971	     = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11972	{
11973	  fn = TREE_VALUE (queue_entry);
11974	  /* Make sure that any template parameters are in scope.  */
11975	  maybe_begin_member_template_processing (fn);
11976	  /* If there are default arguments that have not yet been processed,
11977	     take care of them now.  */
11978	  cp_parser_late_parsing_default_args (parser, fn);
11979	  /* Remove any template parameters from the symbol table.  */
11980	  maybe_end_member_template_processing ();
11981	}
11982      /* Now parse the body of the functions.  */
11983      for (TREE_VALUE (parser->unparsed_functions_queues)
11984	     = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11985	   (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11986	   TREE_VALUE (parser->unparsed_functions_queues)
11987	     = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11988	{
11989	  /* Figure out which function we need to process.  */
11990	  fn = TREE_VALUE (queue_entry);
11991
11992	  /* A hack to prevent garbage collection.  */
11993	  function_depth++;
11994
11995	  /* Parse the function.  */
11996	  cp_parser_late_parsing_for_member (parser, fn);
11997	  function_depth--;
11998	}
11999
12000    }
12001
12002  /* Put back any saved access checks.  */
12003  pop_deferring_access_checks ();
12004
12005  /* Restore the count of active template-parameter-lists.  */
12006  parser->num_template_parameter_lists
12007    = saved_num_template_parameter_lists;
12008
12009  return type;
12010}
12011
12012/* Parse a class-head.
12013
12014   class-head:
12015     class-key identifier [opt] base-clause [opt]
12016     class-key nested-name-specifier identifier base-clause [opt]
12017     class-key nested-name-specifier [opt] template-id
12018       base-clause [opt]
12019
12020   GNU Extensions:
12021     class-key attributes identifier [opt] base-clause [opt]
12022     class-key attributes nested-name-specifier identifier base-clause [opt]
12023     class-key attributes nested-name-specifier [opt] template-id
12024       base-clause [opt]
12025
12026   Returns the TYPE of the indicated class.  Sets
12027   *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12028   involving a nested-name-specifier was used, and FALSE otherwise.
12029
12030   Returns NULL_TREE if the class-head is syntactically valid, but
12031   semantically invalid in a way that means we should skip the entire
12032   body of the class.  */
12033
12034static tree
12035cp_parser_class_head (cp_parser* parser,
12036		      bool* nested_name_specifier_p,
12037		      tree *attributes_p)
12038{
12039  cp_token *token;
12040  tree nested_name_specifier;
12041  enum tag_types class_key;
12042  tree id = NULL_TREE;
12043  tree type = NULL_TREE;
12044  tree attributes;
12045  bool template_id_p = false;
12046  bool qualified_p = false;
12047  bool invalid_nested_name_p = false;
12048  bool invalid_explicit_specialization_p = false;
12049  bool pop_p = false;
12050  unsigned num_templates;
12051
12052  /* Assume no nested-name-specifier will be present.  */
12053  *nested_name_specifier_p = false;
12054  /* Assume no template parameter lists will be used in defining the
12055     type.  */
12056  num_templates = 0;
12057
12058  /* Look for the class-key.  */
12059  class_key = cp_parser_class_key (parser);
12060  if (class_key == none_type)
12061    return error_mark_node;
12062
12063  /* Parse the attributes.  */
12064  attributes = cp_parser_attributes_opt (parser);
12065
12066  /* If the next token is `::', that is invalid -- but sometimes
12067     people do try to write:
12068
12069       struct ::S {};
12070
12071     Handle this gracefully by accepting the extra qualifier, and then
12072     issuing an error about it later if this really is a
12073     class-head.  If it turns out just to be an elaborated type
12074     specifier, remain silent.  */
12075  if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12076    qualified_p = true;
12077
12078  push_deferring_access_checks (dk_no_check);
12079
12080  /* Determine the name of the class.  Begin by looking for an
12081     optional nested-name-specifier.  */
12082  nested_name_specifier
12083    = cp_parser_nested_name_specifier_opt (parser,
12084					   /*typename_keyword_p=*/false,
12085					   /*check_dependency_p=*/false,
12086					   /*type_p=*/false,
12087					   /*is_declaration=*/false);
12088  /* If there was a nested-name-specifier, then there *must* be an
12089     identifier.  */
12090  if (nested_name_specifier)
12091    {
12092      /* Although the grammar says `identifier', it really means
12093	 `class-name' or `template-name'.  You are only allowed to
12094	 define a class that has already been declared with this
12095	 syntax.
12096
12097	 The proposed resolution for Core Issue 180 says that whever
12098	 you see `class T::X' you should treat `X' as a type-name.
12099
12100	 It is OK to define an inaccessible class; for example:
12101
12102           class A { class B; };
12103           class A::B {};
12104
12105         We do not know if we will see a class-name, or a
12106	 template-name.  We look for a class-name first, in case the
12107	 class-name is a template-id; if we looked for the
12108	 template-name first we would stop after the template-name.  */
12109      cp_parser_parse_tentatively (parser);
12110      type = cp_parser_class_name (parser,
12111				   /*typename_keyword_p=*/false,
12112				   /*template_keyword_p=*/false,
12113				   /*type_p=*/true,
12114				   /*check_dependency_p=*/false,
12115				   /*class_head_p=*/true,
12116				   /*is_declaration=*/false);
12117      /* If that didn't work, ignore the nested-name-specifier.  */
12118      if (!cp_parser_parse_definitely (parser))
12119	{
12120	  invalid_nested_name_p = true;
12121	  id = cp_parser_identifier (parser);
12122	  if (id == error_mark_node)
12123	    id = NULL_TREE;
12124	}
12125      /* If we could not find a corresponding TYPE, treat this
12126	 declaration like an unqualified declaration.  */
12127      if (type == error_mark_node)
12128	nested_name_specifier = NULL_TREE;
12129      /* Otherwise, count the number of templates used in TYPE and its
12130	 containing scopes.  */
12131      else
12132	{
12133	  tree scope;
12134
12135	  for (scope = TREE_TYPE (type);
12136	       scope && TREE_CODE (scope) != NAMESPACE_DECL;
12137	       scope = (TYPE_P (scope)
12138			? TYPE_CONTEXT (scope)
12139			: DECL_CONTEXT (scope)))
12140	    if (TYPE_P (scope)
12141		&& CLASS_TYPE_P (scope)
12142		&& CLASSTYPE_TEMPLATE_INFO (scope)
12143		&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12144		&& !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12145	      ++num_templates;
12146	}
12147    }
12148  /* Otherwise, the identifier is optional.  */
12149  else
12150    {
12151      /* We don't know whether what comes next is a template-id,
12152	 an identifier, or nothing at all.  */
12153      cp_parser_parse_tentatively (parser);
12154      /* Check for a template-id.  */
12155      id = cp_parser_template_id (parser,
12156				  /*template_keyword_p=*/false,
12157				  /*check_dependency_p=*/true,
12158				  /*is_declaration=*/true);
12159      /* If that didn't work, it could still be an identifier.  */
12160      if (!cp_parser_parse_definitely (parser))
12161	{
12162	  if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12163	    id = cp_parser_identifier (parser);
12164	  else
12165	    id = NULL_TREE;
12166	}
12167      else
12168	{
12169	  template_id_p = true;
12170	  ++num_templates;
12171	}
12172    }
12173
12174  pop_deferring_access_checks ();
12175
12176  if (id)
12177    cp_parser_check_for_invalid_template_id (parser, id);
12178
12179  /* If it's not a `:' or a `{' then we can't really be looking at a
12180     class-head, since a class-head only appears as part of a
12181     class-specifier.  We have to detect this situation before calling
12182     xref_tag, since that has irreversible side-effects.  */
12183  if (!cp_parser_next_token_starts_class_definition_p (parser))
12184    {
12185      cp_parser_error (parser, "expected `{' or `:'");
12186      return error_mark_node;
12187    }
12188
12189  /* At this point, we're going ahead with the class-specifier, even
12190     if some other problem occurs.  */
12191  cp_parser_commit_to_tentative_parse (parser);
12192  /* Issue the error about the overly-qualified name now.  */
12193  if (qualified_p)
12194    cp_parser_error (parser,
12195		     "global qualification of class name is invalid");
12196  else if (invalid_nested_name_p)
12197    cp_parser_error (parser,
12198		     "qualified name does not name a class");
12199  else if (nested_name_specifier)
12200    {
12201      tree scope;
12202
12203      /* Reject typedef-names in class heads.  */
12204      if (!DECL_IMPLICIT_TYPEDEF_P (type))
12205	{
12206	  error ("invalid class name in declaration of `%D'", type);
12207	  type = NULL_TREE;
12208	  goto done;
12209	}
12210
12211      /* Figure out in what scope the declaration is being placed.  */
12212      scope = current_scope ();
12213      if (!scope)
12214	scope = current_namespace;
12215      /* If that scope does not contain the scope in which the
12216	 class was originally declared, the program is invalid.  */
12217      if (scope && !is_ancestor (scope, nested_name_specifier))
12218	{
12219	  error ("declaration of `%D' in `%D' which does not "
12220		 "enclose `%D'", type, scope, nested_name_specifier);
12221	  type = NULL_TREE;
12222	  goto done;
12223	}
12224      /* [dcl.meaning]
12225
12226         A declarator-id shall not be qualified exception of the
12227	 definition of a ... nested class outside of its class
12228	 ... [or] a the definition or explicit instantiation of a
12229	 class member of a namespace outside of its namespace.  */
12230      if (scope == nested_name_specifier)
12231	{
12232	  pedwarn ("extra qualification ignored");
12233	  nested_name_specifier = NULL_TREE;
12234	  num_templates = 0;
12235	}
12236    }
12237  /* An explicit-specialization must be preceded by "template <>".  If
12238     it is not, try to recover gracefully.  */
12239  if (at_namespace_scope_p ()
12240      && parser->num_template_parameter_lists == 0
12241      && template_id_p)
12242    {
12243      error ("an explicit specialization must be preceded by 'template <>'");
12244      invalid_explicit_specialization_p = true;
12245      /* Take the same action that would have been taken by
12246	 cp_parser_explicit_specialization.  */
12247      ++parser->num_template_parameter_lists;
12248      begin_specialization ();
12249    }
12250  /* There must be no "return" statements between this point and the
12251     end of this function; set "type "to the correct return value and
12252     use "goto done;" to return.  */
12253  /* Make sure that the right number of template parameters were
12254     present.  */
12255  if (!cp_parser_check_template_parameters (parser, num_templates))
12256    {
12257      /* If something went wrong, there is no point in even trying to
12258	 process the class-definition.  */
12259      type = NULL_TREE;
12260      goto done;
12261    }
12262
12263  /* Look up the type.  */
12264  if (template_id_p)
12265    {
12266      type = TREE_TYPE (id);
12267      maybe_process_partial_specialization (type);
12268    }
12269  else if (!nested_name_specifier)
12270    {
12271      /* If the class was unnamed, create a dummy name.  */
12272      if (!id)
12273	id = make_anon_name ();
12274      type = xref_tag (class_key, id, /*globalize=*/false,
12275		       parser->num_template_parameter_lists);
12276    }
12277  else
12278    {
12279      tree class_type;
12280      bool pop_p = false;
12281
12282      /* Given:
12283
12284	    template <typename T> struct S { struct T };
12285	    template <typename T> struct S<T>::T { };
12286
12287	 we will get a TYPENAME_TYPE when processing the definition of
12288	 `S::T'.  We need to resolve it to the actual type before we
12289	 try to define it.  */
12290      if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12291	{
12292	  class_type = resolve_typename_type (TREE_TYPE (type),
12293					      /*only_current_p=*/false);
12294	  if (class_type != error_mark_node)
12295	    type = TYPE_NAME (class_type);
12296	  else
12297	    {
12298	      cp_parser_error (parser, "could not resolve typename type");
12299	      type = error_mark_node;
12300	    }
12301	}
12302
12303      maybe_process_partial_specialization (TREE_TYPE (type));
12304      class_type = current_class_type;
12305      /* Enter the scope indicated by the nested-name-specifier.  */
12306      if (nested_name_specifier)
12307	pop_p = push_scope (nested_name_specifier);
12308      /* Get the canonical version of this type.  */
12309      type = TYPE_MAIN_DECL (TREE_TYPE (type));
12310      if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12311	  && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12312	type = push_template_decl (type);
12313      type = TREE_TYPE (type);
12314      if (nested_name_specifier)
12315	{
12316	  *nested_name_specifier_p = true;
12317	  if (pop_p)
12318	    pop_scope (nested_name_specifier);
12319	}
12320    }
12321  /* Indicate whether this class was declared as a `class' or as a
12322     `struct'.  */
12323  if (TREE_CODE (type) == RECORD_TYPE)
12324    CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12325  cp_parser_check_class_key (class_key, type);
12326
12327  /* Enter the scope containing the class; the names of base classes
12328     should be looked up in that context.  For example, given:
12329
12330       struct A { struct B {}; struct C; };
12331       struct A::C : B {};
12332
12333     is valid.  */
12334  if (nested_name_specifier)
12335    pop_p = push_scope (nested_name_specifier);
12336  /* Now, look for the base-clause.  */
12337  token = cp_lexer_peek_token (parser->lexer);
12338  if (token->type == CPP_COLON)
12339    {
12340      tree bases;
12341
12342      /* Get the list of base-classes.  */
12343      bases = cp_parser_base_clause (parser);
12344      /* Process them.  */
12345      xref_basetypes (type, bases);
12346    }
12347  /* Leave the scope given by the nested-name-specifier.  We will
12348     enter the class scope itself while processing the members.  */
12349  if (pop_p)
12350    pop_scope (nested_name_specifier);
12351
12352 done:
12353  if (invalid_explicit_specialization_p)
12354    {
12355      end_specialization ();
12356      --parser->num_template_parameter_lists;
12357    }
12358  *attributes_p = attributes;
12359  return type;
12360}
12361
12362/* Parse a class-key.
12363
12364   class-key:
12365     class
12366     struct
12367     union
12368
12369   Returns the kind of class-key specified, or none_type to indicate
12370   error.  */
12371
12372static enum tag_types
12373cp_parser_class_key (cp_parser* parser)
12374{
12375  cp_token *token;
12376  enum tag_types tag_type;
12377
12378  /* Look for the class-key.  */
12379  token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12380  if (!token)
12381    return none_type;
12382
12383  /* Check to see if the TOKEN is a class-key.  */
12384  tag_type = cp_parser_token_is_class_key (token);
12385  if (!tag_type)
12386    cp_parser_error (parser, "expected class-key");
12387  return tag_type;
12388}
12389
12390/* Parse an (optional) member-specification.
12391
12392   member-specification:
12393     member-declaration member-specification [opt]
12394     access-specifier : member-specification [opt]  */
12395
12396static void
12397cp_parser_member_specification_opt (cp_parser* parser)
12398{
12399  while (true)
12400    {
12401      cp_token *token;
12402      enum rid keyword;
12403
12404      /* Peek at the next token.  */
12405      token = cp_lexer_peek_token (parser->lexer);
12406      /* If it's a `}', or EOF then we've seen all the members.  */
12407      if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12408	break;
12409
12410      /* See if this token is a keyword.  */
12411      keyword = token->keyword;
12412      switch (keyword)
12413	{
12414	case RID_PUBLIC:
12415	case RID_PROTECTED:
12416	case RID_PRIVATE:
12417	  /* Consume the access-specifier.  */
12418	  cp_lexer_consume_token (parser->lexer);
12419	  /* Remember which access-specifier is active.  */
12420	  current_access_specifier = token->value;
12421	  /* Look for the `:'.  */
12422	  cp_parser_require (parser, CPP_COLON, "`:'");
12423	  break;
12424
12425	default:
12426	  /* Otherwise, the next construction must be a
12427	     member-declaration.  */
12428	  cp_parser_member_declaration (parser);
12429	}
12430    }
12431}
12432
12433/* Parse a member-declaration.
12434
12435   member-declaration:
12436     decl-specifier-seq [opt] member-declarator-list [opt] ;
12437     function-definition ; [opt]
12438     :: [opt] nested-name-specifier template [opt] unqualified-id ;
12439     using-declaration
12440     template-declaration
12441
12442   member-declarator-list:
12443     member-declarator
12444     member-declarator-list , member-declarator
12445
12446   member-declarator:
12447     declarator pure-specifier [opt]
12448     declarator constant-initializer [opt]
12449     identifier [opt] : constant-expression
12450
12451   GNU Extensions:
12452
12453   member-declaration:
12454     __extension__ member-declaration
12455
12456   member-declarator:
12457     declarator attributes [opt] pure-specifier [opt]
12458     declarator attributes [opt] constant-initializer [opt]
12459     identifier [opt] attributes [opt] : constant-expression  */
12460
12461static void
12462cp_parser_member_declaration (cp_parser* parser)
12463{
12464  tree decl_specifiers;
12465  tree prefix_attributes;
12466  tree decl;
12467  int declares_class_or_enum;
12468  bool friend_p;
12469  cp_token *token;
12470  int saved_pedantic;
12471
12472  /* Check for the `__extension__' keyword.  */
12473  if (cp_parser_extension_opt (parser, &saved_pedantic))
12474    {
12475      /* Recurse.  */
12476      cp_parser_member_declaration (parser);
12477      /* Restore the old value of the PEDANTIC flag.  */
12478      pedantic = saved_pedantic;
12479
12480      return;
12481    }
12482
12483  /* Check for a template-declaration.  */
12484  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12485    {
12486      /* Parse the template-declaration.  */
12487      cp_parser_template_declaration (parser, /*member_p=*/true);
12488
12489      return;
12490    }
12491
12492  /* Check for a using-declaration.  */
12493  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12494    {
12495      /* Parse the using-declaration.  */
12496      cp_parser_using_declaration (parser);
12497
12498      return;
12499    }
12500
12501  /* Parse the decl-specifier-seq.  */
12502  decl_specifiers
12503    = cp_parser_decl_specifier_seq (parser,
12504				    CP_PARSER_FLAGS_OPTIONAL,
12505				    &prefix_attributes,
12506				    &declares_class_or_enum);
12507  /* Check for an invalid type-name.  */
12508  if (cp_parser_diagnose_invalid_type_name (parser))
12509    return;
12510  /* If there is no declarator, then the decl-specifier-seq should
12511     specify a type.  */
12512  if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12513    {
12514      /* If there was no decl-specifier-seq, and the next token is a
12515	 `;', then we have something like:
12516
12517	   struct S { ; };
12518
12519	 [class.mem]
12520
12521	 Each member-declaration shall declare at least one member
12522	 name of the class.  */
12523      if (!decl_specifiers)
12524	{
12525	  if (pedantic)
12526	    pedwarn ("extra semicolon");
12527	}
12528      else
12529	{
12530	  tree type;
12531
12532	  /* See if this declaration is a friend.  */
12533	  friend_p = cp_parser_friend_p (decl_specifiers);
12534	  /* If there were decl-specifiers, check to see if there was
12535	     a class-declaration.  */
12536	  type = check_tag_decl (decl_specifiers);
12537	  /* Nested classes have already been added to the class, but
12538	     a `friend' needs to be explicitly registered.  */
12539	  if (friend_p)
12540	    {
12541	      /* If the `friend' keyword was present, the friend must
12542		 be introduced with a class-key.  */
12543	       if (!declares_class_or_enum)
12544		 error ("a class-key must be used when declaring a friend");
12545	       /* In this case:
12546
12547		    template <typename T> struct A {
12548                      friend struct A<T>::B;
12549                    };
12550
12551		  A<T>::B will be represented by a TYPENAME_TYPE, and
12552		  therefore not recognized by check_tag_decl.  */
12553	       if (!type)
12554		 {
12555		   tree specifier;
12556
12557		   for (specifier = decl_specifiers;
12558			specifier;
12559			specifier = TREE_CHAIN (specifier))
12560		     {
12561		       tree s = TREE_VALUE (specifier);
12562
12563		       if (TREE_CODE (s) == IDENTIFIER_NODE)
12564                         get_global_value_if_present (s, &type);
12565		       if (TREE_CODE (s) == TYPE_DECL)
12566			 s = TREE_TYPE (s);
12567		       if (TYPE_P (s))
12568			 {
12569			   type = s;
12570			   break;
12571			 }
12572		     }
12573		 }
12574	       if (!type || !TYPE_P (type))
12575		 error ("friend declaration does not name a class or "
12576			"function");
12577	       else
12578		 make_friend_class (current_class_type, type,
12579				    /*complain=*/true);
12580	    }
12581	  /* If there is no TYPE, an error message will already have
12582	     been issued.  */
12583	  else if (!type)
12584	    ;
12585	  /* An anonymous aggregate has to be handled specially; such
12586	     a declaration really declares a data member (with a
12587	     particular type), as opposed to a nested class.  */
12588	  else if (ANON_AGGR_TYPE_P (type))
12589	    {
12590	      /* Remove constructors and such from TYPE, now that we
12591		 know it is an anonymous aggregate.  */
12592	      fixup_anonymous_aggr (type);
12593	      /* And make the corresponding data member.  */
12594	      decl = build_decl (FIELD_DECL, NULL_TREE, type);
12595	      /* Add it to the class.  */
12596	      finish_member_declaration (decl);
12597	    }
12598	  else
12599	    cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12600	}
12601    }
12602  else
12603    {
12604      /* See if these declarations will be friends.  */
12605      friend_p = cp_parser_friend_p (decl_specifiers);
12606
12607      /* Keep going until we hit the `;' at the end of the
12608	 declaration.  */
12609      while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12610	{
12611	  tree attributes = NULL_TREE;
12612	  tree first_attribute;
12613
12614	  /* Peek at the next token.  */
12615	  token = cp_lexer_peek_token (parser->lexer);
12616
12617	  /* Check for a bitfield declaration.  */
12618	  if (token->type == CPP_COLON
12619	      || (token->type == CPP_NAME
12620		  && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12621		  == CPP_COLON))
12622	    {
12623	      tree identifier;
12624	      tree width;
12625
12626	      /* Get the name of the bitfield.  Note that we cannot just
12627		 check TOKEN here because it may have been invalidated by
12628		 the call to cp_lexer_peek_nth_token above.  */
12629	      if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12630		identifier = cp_parser_identifier (parser);
12631	      else
12632		identifier = NULL_TREE;
12633
12634	      /* Consume the `:' token.  */
12635	      cp_lexer_consume_token (parser->lexer);
12636	      /* Get the width of the bitfield.  */
12637	      width
12638		= cp_parser_constant_expression (parser,
12639						 /*allow_non_constant=*/false,
12640						 NULL);
12641
12642	      /* Look for attributes that apply to the bitfield.  */
12643	      attributes = cp_parser_attributes_opt (parser);
12644	      /* Remember which attributes are prefix attributes and
12645		 which are not.  */
12646	      first_attribute = attributes;
12647	      /* Combine the attributes.  */
12648	      attributes = chainon (prefix_attributes, attributes);
12649
12650	      /* Create the bitfield declaration.  */
12651	      decl = grokbitfield (identifier,
12652				   decl_specifiers,
12653				   width);
12654	      /* Apply the attributes.  */
12655	      cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12656	    }
12657	  else
12658	    {
12659	      tree declarator;
12660	      tree initializer;
12661	      tree asm_specification;
12662	      int ctor_dtor_or_conv_p;
12663
12664	      /* Parse the declarator.  */
12665	      declarator
12666		= cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12667					&ctor_dtor_or_conv_p,
12668					/*parenthesized_p=*/NULL,
12669					/*member_p=*/true);
12670
12671	      /* If something went wrong parsing the declarator, make sure
12672		 that we at least consume some tokens.  */
12673	      if (declarator == error_mark_node)
12674		{
12675		  /* Skip to the end of the statement.  */
12676		  cp_parser_skip_to_end_of_statement (parser);
12677		  /* If the next token is not a semicolon, that is
12678		     probably because we just skipped over the body of
12679		     a function.  So, we consume a semicolon if
12680		     present, but do not issue an error message if it
12681		     is not present.  */
12682		  if (cp_lexer_next_token_is (parser->lexer,
12683					      CPP_SEMICOLON))
12684		    cp_lexer_consume_token (parser->lexer);
12685		  return;
12686		}
12687
12688	      cp_parser_check_for_definition_in_return_type
12689		(declarator, declares_class_or_enum);
12690
12691	      /* Look for an asm-specification.  */
12692	      asm_specification = cp_parser_asm_specification_opt (parser);
12693	      /* Look for attributes that apply to the declaration.  */
12694	      attributes = cp_parser_attributes_opt (parser);
12695	      /* Remember which attributes are prefix attributes and
12696		 which are not.  */
12697	      first_attribute = attributes;
12698	      /* Combine the attributes.  */
12699	      attributes = chainon (prefix_attributes, attributes);
12700
12701	      /* If it's an `=', then we have a constant-initializer or a
12702		 pure-specifier.  It is not correct to parse the
12703		 initializer before registering the member declaration
12704		 since the member declaration should be in scope while
12705		 its initializer is processed.  However, the rest of the
12706		 front end does not yet provide an interface that allows
12707		 us to handle this correctly.  */
12708	      if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12709		{
12710		  /* In [class.mem]:
12711
12712		     A pure-specifier shall be used only in the declaration of
12713		     a virtual function.
12714
12715		     A member-declarator can contain a constant-initializer
12716		     only if it declares a static member of integral or
12717		     enumeration type.
12718
12719		     Therefore, if the DECLARATOR is for a function, we look
12720		     for a pure-specifier; otherwise, we look for a
12721		     constant-initializer.  When we call `grokfield', it will
12722		     perform more stringent semantics checks.  */
12723		  if (TREE_CODE (declarator) == CALL_EXPR)
12724		    initializer = cp_parser_pure_specifier (parser);
12725		  else
12726		    /* Parse the initializer.  */
12727		    initializer = cp_parser_constant_initializer (parser);
12728		}
12729	      /* Otherwise, there is no initializer.  */
12730	      else
12731		initializer = NULL_TREE;
12732
12733	      /* See if we are probably looking at a function
12734		 definition.  We are certainly not looking at at a
12735		 member-declarator.  Calling `grokfield' has
12736		 side-effects, so we must not do it unless we are sure
12737		 that we are looking at a member-declarator.  */
12738	      if (cp_parser_token_starts_function_definition_p
12739		  (cp_lexer_peek_token (parser->lexer)))
12740		{
12741		  /* The grammar does not allow a pure-specifier to be
12742		     used when a member function is defined.  (It is
12743		     possible that this fact is an oversight in the
12744		     standard, since a pure function may be defined
12745		     outside of the class-specifier.  */
12746		  if (initializer)
12747		    error ("pure-specifier on function-definition");
12748		  decl = cp_parser_save_member_function_body (parser,
12749							      decl_specifiers,
12750							      declarator,
12751							      attributes);
12752		  /* If the member was not a friend, declare it here.  */
12753		  if (!friend_p)
12754		    finish_member_declaration (decl);
12755		  /* Peek at the next token.  */
12756		  token = cp_lexer_peek_token (parser->lexer);
12757		  /* If the next token is a semicolon, consume it.  */
12758		  if (token->type == CPP_SEMICOLON)
12759		    cp_lexer_consume_token (parser->lexer);
12760		  return;
12761		}
12762	      else
12763		{
12764		  /* Create the declaration.  */
12765		  decl = grokfield (declarator, decl_specifiers,
12766				    initializer, asm_specification,
12767				    attributes);
12768		  /* Any initialization must have been from a
12769		     constant-expression.  */
12770		  if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12771		    DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12772		}
12773	    }
12774
12775	  /* Reset PREFIX_ATTRIBUTES.  */
12776	  while (attributes && TREE_CHAIN (attributes) != first_attribute)
12777	    attributes = TREE_CHAIN (attributes);
12778	  if (attributes)
12779	    TREE_CHAIN (attributes) = NULL_TREE;
12780
12781	  /* If there is any qualification still in effect, clear it
12782	     now; we will be starting fresh with the next declarator.  */
12783	  parser->scope = NULL_TREE;
12784	  parser->qualifying_scope = NULL_TREE;
12785	  parser->object_scope = NULL_TREE;
12786	  /* If it's a `,', then there are more declarators.  */
12787	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12788	    cp_lexer_consume_token (parser->lexer);
12789	  /* If the next token isn't a `;', then we have a parse error.  */
12790	  else if (cp_lexer_next_token_is_not (parser->lexer,
12791					       CPP_SEMICOLON))
12792	    {
12793	      cp_parser_error (parser, "expected `;'");
12794	      /* Skip tokens until we find a `;'.  */
12795	      cp_parser_skip_to_end_of_statement (parser);
12796
12797	      break;
12798	    }
12799
12800	  if (decl)
12801	    {
12802	      /* Add DECL to the list of members.  */
12803	      if (!friend_p)
12804		finish_member_declaration (decl);
12805
12806	      if (TREE_CODE (decl) == FUNCTION_DECL)
12807		cp_parser_save_default_args (parser, decl);
12808	    }
12809	}
12810    }
12811
12812  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12813}
12814
12815/* Parse a pure-specifier.
12816
12817   pure-specifier:
12818     = 0
12819
12820   Returns INTEGER_ZERO_NODE if a pure specifier is found.
12821   Otherwise, ERROR_MARK_NODE is returned.  */
12822
12823static tree
12824cp_parser_pure_specifier (cp_parser* parser)
12825{
12826  cp_token *token;
12827
12828  /* Look for the `=' token.  */
12829  if (!cp_parser_require (parser, CPP_EQ, "`='"))
12830    return error_mark_node;
12831  /* Look for the `0' token.  */
12832  token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12833  /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12834     to get information from the lexer about how the number was
12835     spelled in order to fix this problem.  */
12836  if (!token || !integer_zerop (token->value))
12837    return error_mark_node;
12838
12839  return integer_zero_node;
12840}
12841
12842/* Parse a constant-initializer.
12843
12844   constant-initializer:
12845     = constant-expression
12846
12847   Returns a representation of the constant-expression.  */
12848
12849static tree
12850cp_parser_constant_initializer (cp_parser* parser)
12851{
12852  /* Look for the `=' token.  */
12853  if (!cp_parser_require (parser, CPP_EQ, "`='"))
12854    return error_mark_node;
12855
12856  /* It is invalid to write:
12857
12858       struct S { static const int i = { 7 }; };
12859
12860     */
12861  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12862    {
12863      cp_parser_error (parser,
12864		       "a brace-enclosed initializer is not allowed here");
12865      /* Consume the opening brace.  */
12866      cp_lexer_consume_token (parser->lexer);
12867      /* Skip the initializer.  */
12868      cp_parser_skip_to_closing_brace (parser);
12869      /* Look for the trailing `}'.  */
12870      cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12871
12872      return error_mark_node;
12873    }
12874
12875  return cp_parser_constant_expression (parser,
12876					/*allow_non_constant=*/false,
12877					NULL);
12878}
12879
12880/* Derived classes [gram.class.derived] */
12881
12882/* Parse a base-clause.
12883
12884   base-clause:
12885     : base-specifier-list
12886
12887   base-specifier-list:
12888     base-specifier
12889     base-specifier-list , base-specifier
12890
12891   Returns a TREE_LIST representing the base-classes, in the order in
12892   which they were declared.  The representation of each node is as
12893   described by cp_parser_base_specifier.
12894
12895   In the case that no bases are specified, this function will return
12896   NULL_TREE, not ERROR_MARK_NODE.  */
12897
12898static tree
12899cp_parser_base_clause (cp_parser* parser)
12900{
12901  tree bases = NULL_TREE;
12902
12903  /* Look for the `:' that begins the list.  */
12904  cp_parser_require (parser, CPP_COLON, "`:'");
12905
12906  /* Scan the base-specifier-list.  */
12907  while (true)
12908    {
12909      cp_token *token;
12910      tree base;
12911
12912      /* Look for the base-specifier.  */
12913      base = cp_parser_base_specifier (parser);
12914      /* Add BASE to the front of the list.  */
12915      if (base != error_mark_node)
12916	{
12917	  TREE_CHAIN (base) = bases;
12918	  bases = base;
12919	}
12920      /* Peek at the next token.  */
12921      token = cp_lexer_peek_token (parser->lexer);
12922      /* If it's not a comma, then the list is complete.  */
12923      if (token->type != CPP_COMMA)
12924	break;
12925      /* Consume the `,'.  */
12926      cp_lexer_consume_token (parser->lexer);
12927    }
12928
12929  /* PARSER->SCOPE may still be non-NULL at this point, if the last
12930     base class had a qualified name.  However, the next name that
12931     appears is certainly not qualified.  */
12932  parser->scope = NULL_TREE;
12933  parser->qualifying_scope = NULL_TREE;
12934  parser->object_scope = NULL_TREE;
12935
12936  return nreverse (bases);
12937}
12938
12939/* Parse a base-specifier.
12940
12941   base-specifier:
12942     :: [opt] nested-name-specifier [opt] class-name
12943     virtual access-specifier [opt] :: [opt] nested-name-specifier
12944       [opt] class-name
12945     access-specifier virtual [opt] :: [opt] nested-name-specifier
12946       [opt] class-name
12947
12948   Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12949   ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12950   indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12951   (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12952
12953static tree
12954cp_parser_base_specifier (cp_parser* parser)
12955{
12956  cp_token *token;
12957  bool done = false;
12958  bool virtual_p = false;
12959  bool duplicate_virtual_error_issued_p = false;
12960  bool duplicate_access_error_issued_p = false;
12961  bool class_scope_p, template_p;
12962  tree access = access_default_node;
12963  tree type;
12964
12965  /* Process the optional `virtual' and `access-specifier'.  */
12966  while (!done)
12967    {
12968      /* Peek at the next token.  */
12969      token = cp_lexer_peek_token (parser->lexer);
12970      /* Process `virtual'.  */
12971      switch (token->keyword)
12972	{
12973	case RID_VIRTUAL:
12974	  /* If `virtual' appears more than once, issue an error.  */
12975	  if (virtual_p && !duplicate_virtual_error_issued_p)
12976	    {
12977	      cp_parser_error (parser,
12978			       "`virtual' specified more than once in base-specified");
12979	      duplicate_virtual_error_issued_p = true;
12980	    }
12981
12982	  virtual_p = true;
12983
12984	  /* Consume the `virtual' token.  */
12985	  cp_lexer_consume_token (parser->lexer);
12986
12987	  break;
12988
12989	case RID_PUBLIC:
12990	case RID_PROTECTED:
12991	case RID_PRIVATE:
12992	  /* If more than one access specifier appears, issue an
12993	     error.  */
12994	  if (access != access_default_node
12995	      && !duplicate_access_error_issued_p)
12996	    {
12997	      cp_parser_error (parser,
12998			       "more than one access specifier in base-specified");
12999	      duplicate_access_error_issued_p = true;
13000	    }
13001
13002	  access = ridpointers[(int) token->keyword];
13003
13004	  /* Consume the access-specifier.  */
13005	  cp_lexer_consume_token (parser->lexer);
13006
13007	  break;
13008
13009	default:
13010	  done = true;
13011	  break;
13012	}
13013    }
13014  /* It is not uncommon to see programs mechanically, errouneously, use
13015     the 'typename' keyword to denote (dependent) qualified types
13016     as base classes.  */
13017  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13018    {
13019      if (!processing_template_decl)
13020	error ("keyword `typename' not allowed outside of templates");
13021      else
13022	error ("keyword `typename' not allowed in this context "
13023	       "(the base class is implicitly a type)");
13024      cp_lexer_consume_token (parser->lexer);
13025    }
13026
13027  /* Look for the optional `::' operator.  */
13028  cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13029  /* Look for the nested-name-specifier.  The simplest way to
13030     implement:
13031
13032       [temp.res]
13033
13034       The keyword `typename' is not permitted in a base-specifier or
13035       mem-initializer; in these contexts a qualified name that
13036       depends on a template-parameter is implicitly assumed to be a
13037       type name.
13038
13039     is to pretend that we have seen the `typename' keyword at this
13040     point.  */
13041  cp_parser_nested_name_specifier_opt (parser,
13042				       /*typename_keyword_p=*/true,
13043				       /*check_dependency_p=*/true,
13044				       /*type_p=*/true,
13045				       /*is_declaration=*/true);
13046  /* If the base class is given by a qualified name, assume that names
13047     we see are type names or templates, as appropriate.  */
13048  class_scope_p = (parser->scope && TYPE_P (parser->scope));
13049  template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13050
13051  /* Finally, look for the class-name.  */
13052  type = cp_parser_class_name (parser,
13053			       class_scope_p,
13054			       template_p,
13055			       /*type_p=*/true,
13056			       /*check_dependency_p=*/true,
13057			       /*class_head_p=*/false,
13058			       /*is_declaration=*/true);
13059
13060  if (type == error_mark_node)
13061    return error_mark_node;
13062
13063  return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13064}
13065
13066/* Exception handling [gram.exception] */
13067
13068/* Parse an (optional) exception-specification.
13069
13070   exception-specification:
13071     throw ( type-id-list [opt] )
13072
13073   Returns a TREE_LIST representing the exception-specification.  The
13074   TREE_VALUE of each node is a type.  */
13075
13076static tree
13077cp_parser_exception_specification_opt (cp_parser* parser)
13078{
13079  cp_token *token;
13080  tree type_id_list;
13081
13082  /* Peek at the next token.  */
13083  token = cp_lexer_peek_token (parser->lexer);
13084  /* If it's not `throw', then there's no exception-specification.  */
13085  if (!cp_parser_is_keyword (token, RID_THROW))
13086    return NULL_TREE;
13087
13088  /* Consume the `throw'.  */
13089  cp_lexer_consume_token (parser->lexer);
13090
13091  /* Look for the `('.  */
13092  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13093
13094  /* Peek at the next token.  */
13095  token = cp_lexer_peek_token (parser->lexer);
13096  /* If it's not a `)', then there is a type-id-list.  */
13097  if (token->type != CPP_CLOSE_PAREN)
13098    {
13099      const char *saved_message;
13100
13101      /* Types may not be defined in an exception-specification.  */
13102      saved_message = parser->type_definition_forbidden_message;
13103      parser->type_definition_forbidden_message
13104	= "types may not be defined in an exception-specification";
13105      /* Parse the type-id-list.  */
13106      type_id_list = cp_parser_type_id_list (parser);
13107      /* Restore the saved message.  */
13108      parser->type_definition_forbidden_message = saved_message;
13109    }
13110  else
13111    type_id_list = empty_except_spec;
13112
13113  /* Look for the `)'.  */
13114  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13115
13116  return type_id_list;
13117}
13118
13119/* Parse an (optional) type-id-list.
13120
13121   type-id-list:
13122     type-id
13123     type-id-list , type-id
13124
13125   Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13126   in the order that the types were presented.  */
13127
13128static tree
13129cp_parser_type_id_list (cp_parser* parser)
13130{
13131  tree types = NULL_TREE;
13132
13133  while (true)
13134    {
13135      cp_token *token;
13136      tree type;
13137
13138      /* Get the next type-id.  */
13139      type = cp_parser_type_id (parser);
13140      /* Add it to the list.  */
13141      types = add_exception_specifier (types, type, /*complain=*/1);
13142      /* Peek at the next token.  */
13143      token = cp_lexer_peek_token (parser->lexer);
13144      /* If it is not a `,', we are done.  */
13145      if (token->type != CPP_COMMA)
13146	break;
13147      /* Consume the `,'.  */
13148      cp_lexer_consume_token (parser->lexer);
13149    }
13150
13151  return nreverse (types);
13152}
13153
13154/* Parse a try-block.
13155
13156   try-block:
13157     try compound-statement handler-seq  */
13158
13159static tree
13160cp_parser_try_block (cp_parser* parser)
13161{
13162  tree try_block;
13163
13164  cp_parser_require_keyword (parser, RID_TRY, "`try'");
13165  try_block = begin_try_block ();
13166  cp_parser_compound_statement (parser, false);
13167  finish_try_block (try_block);
13168  cp_parser_handler_seq (parser);
13169  finish_handler_sequence (try_block);
13170
13171  return try_block;
13172}
13173
13174/* Parse a function-try-block.
13175
13176   function-try-block:
13177     try ctor-initializer [opt] function-body handler-seq  */
13178
13179static bool
13180cp_parser_function_try_block (cp_parser* parser)
13181{
13182  tree try_block;
13183  bool ctor_initializer_p;
13184
13185  /* Look for the `try' keyword.  */
13186  if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13187    return false;
13188  /* Let the rest of the front-end know where we are.  */
13189  try_block = begin_function_try_block ();
13190  /* Parse the function-body.  */
13191  ctor_initializer_p
13192    = cp_parser_ctor_initializer_opt_and_function_body (parser);
13193  /* We're done with the `try' part.  */
13194  finish_function_try_block (try_block);
13195  /* Parse the handlers.  */
13196  cp_parser_handler_seq (parser);
13197  /* We're done with the handlers.  */
13198  finish_function_handler_sequence (try_block);
13199
13200  return ctor_initializer_p;
13201}
13202
13203/* Parse a handler-seq.
13204
13205   handler-seq:
13206     handler handler-seq [opt]  */
13207
13208static void
13209cp_parser_handler_seq (cp_parser* parser)
13210{
13211  while (true)
13212    {
13213      cp_token *token;
13214
13215      /* Parse the handler.  */
13216      cp_parser_handler (parser);
13217      /* Peek at the next token.  */
13218      token = cp_lexer_peek_token (parser->lexer);
13219      /* If it's not `catch' then there are no more handlers.  */
13220      if (!cp_parser_is_keyword (token, RID_CATCH))
13221	break;
13222    }
13223}
13224
13225/* Parse a handler.
13226
13227   handler:
13228     catch ( exception-declaration ) compound-statement  */
13229
13230static void
13231cp_parser_handler (cp_parser* parser)
13232{
13233  tree handler;
13234  tree declaration;
13235
13236  cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13237  handler = begin_handler ();
13238  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13239  declaration = cp_parser_exception_declaration (parser);
13240  finish_handler_parms (declaration, handler);
13241  cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13242  cp_parser_compound_statement (parser, false);
13243  finish_handler (handler);
13244}
13245
13246/* Parse an exception-declaration.
13247
13248   exception-declaration:
13249     type-specifier-seq declarator
13250     type-specifier-seq abstract-declarator
13251     type-specifier-seq
13252     ...
13253
13254   Returns a VAR_DECL for the declaration, or NULL_TREE if the
13255   ellipsis variant is used.  */
13256
13257static tree
13258cp_parser_exception_declaration (cp_parser* parser)
13259{
13260  tree type_specifiers;
13261  tree declarator;
13262  const char *saved_message;
13263
13264  /* If it's an ellipsis, it's easy to handle.  */
13265  if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13266    {
13267      /* Consume the `...' token.  */
13268      cp_lexer_consume_token (parser->lexer);
13269      return NULL_TREE;
13270    }
13271
13272  /* Types may not be defined in exception-declarations.  */
13273  saved_message = parser->type_definition_forbidden_message;
13274  parser->type_definition_forbidden_message
13275    = "types may not be defined in exception-declarations";
13276
13277  /* Parse the type-specifier-seq.  */
13278  type_specifiers = cp_parser_type_specifier_seq (parser);
13279  /* If it's a `)', then there is no declarator.  */
13280  if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13281    declarator = NULL_TREE;
13282  else
13283    declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13284				       /*ctor_dtor_or_conv_p=*/NULL,
13285				       /*parenthesized_p=*/NULL,
13286				       /*member_p=*/false);
13287
13288  /* Restore the saved message.  */
13289  parser->type_definition_forbidden_message = saved_message;
13290
13291  return start_handler_parms (type_specifiers, declarator);
13292}
13293
13294/* Parse a throw-expression.
13295
13296   throw-expression:
13297     throw assignment-expression [opt]
13298
13299   Returns a THROW_EXPR representing the throw-expression.  */
13300
13301static tree
13302cp_parser_throw_expression (cp_parser* parser)
13303{
13304  tree expression;
13305  cp_token* token;
13306
13307  cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13308  token = cp_lexer_peek_token (parser->lexer);
13309  /* Figure out whether or not there is an assignment-expression
13310     following the "throw" keyword.  */
13311  if (token->type == CPP_COMMA
13312      || token->type == CPP_SEMICOLON
13313      || token->type == CPP_CLOSE_PAREN
13314      || token->type == CPP_CLOSE_SQUARE
13315      || token->type == CPP_CLOSE_BRACE
13316      || token->type == CPP_COLON)
13317    expression = NULL_TREE;
13318  else
13319    expression = cp_parser_assignment_expression (parser);
13320
13321  return build_throw (expression);
13322}
13323
13324/* GNU Extensions */
13325
13326/* Parse an (optional) asm-specification.
13327
13328   asm-specification:
13329     asm ( string-literal )
13330
13331   If the asm-specification is present, returns a STRING_CST
13332   corresponding to the string-literal.  Otherwise, returns
13333   NULL_TREE.  */
13334
13335static tree
13336cp_parser_asm_specification_opt (cp_parser* parser)
13337{
13338  cp_token *token;
13339  tree asm_specification;
13340
13341  /* Peek at the next token.  */
13342  token = cp_lexer_peek_token (parser->lexer);
13343  /* If the next token isn't the `asm' keyword, then there's no
13344     asm-specification.  */
13345  if (!cp_parser_is_keyword (token, RID_ASM))
13346    return NULL_TREE;
13347
13348  /* Consume the `asm' token.  */
13349  cp_lexer_consume_token (parser->lexer);
13350  /* Look for the `('.  */
13351  cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13352
13353  /* Look for the string-literal.  */
13354  token = cp_parser_require (parser, CPP_STRING, "string-literal");
13355  if (token)
13356    asm_specification = token->value;
13357  else
13358    asm_specification = NULL_TREE;
13359
13360  /* Look for the `)'.  */
13361  cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13362
13363  return asm_specification;
13364}
13365
13366/* Parse an asm-operand-list.
13367
13368   asm-operand-list:
13369     asm-operand
13370     asm-operand-list , asm-operand
13371
13372   asm-operand:
13373     string-literal ( expression )
13374     [ string-literal ] string-literal ( expression )
13375
13376   Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13377   each node is the expression.  The TREE_PURPOSE is itself a
13378   TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13379   string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13380   is a STRING_CST for the string literal before the parenthesis.  */
13381
13382static tree
13383cp_parser_asm_operand_list (cp_parser* parser)
13384{
13385  tree asm_operands = NULL_TREE;
13386
13387  while (true)
13388    {
13389      tree string_literal;
13390      tree expression;
13391      tree name;
13392      cp_token *token;
13393
13394      if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13395	{
13396	  /* Consume the `[' token.  */
13397	  cp_lexer_consume_token (parser->lexer);
13398	  /* Read the operand name.  */
13399	  name = cp_parser_identifier (parser);
13400	  if (name != error_mark_node)
13401	    name = build_string (IDENTIFIER_LENGTH (name),
13402				 IDENTIFIER_POINTER (name));
13403	  /* Look for the closing `]'.  */
13404	  cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13405	}
13406      else
13407	name = NULL_TREE;
13408      /* Look for the string-literal.  */
13409      token = cp_parser_require (parser, CPP_STRING, "string-literal");
13410      string_literal = token ? token->value : error_mark_node;
13411      /* Look for the `('.  */
13412      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13413      /* Parse the expression.  */
13414      expression = cp_parser_expression (parser);
13415      /* Look for the `)'.  */
13416      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13417      /* Add this operand to the list.  */
13418      asm_operands = tree_cons (build_tree_list (name, string_literal),
13419				expression,
13420				asm_operands);
13421      /* If the next token is not a `,', there are no more
13422	 operands.  */
13423      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13424	break;
13425      /* Consume the `,'.  */
13426      cp_lexer_consume_token (parser->lexer);
13427    }
13428
13429  return nreverse (asm_operands);
13430}
13431
13432/* Parse an asm-clobber-list.
13433
13434   asm-clobber-list:
13435     string-literal
13436     asm-clobber-list , string-literal
13437
13438   Returns a TREE_LIST, indicating the clobbers in the order that they
13439   appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13440
13441static tree
13442cp_parser_asm_clobber_list (cp_parser* parser)
13443{
13444  tree clobbers = NULL_TREE;
13445
13446  while (true)
13447    {
13448      cp_token *token;
13449      tree string_literal;
13450
13451      /* Look for the string literal.  */
13452      token = cp_parser_require (parser, CPP_STRING, "string-literal");
13453      string_literal = token ? token->value : error_mark_node;
13454      /* Add it to the list.  */
13455      clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13456      /* If the next token is not a `,', then the list is
13457	 complete.  */
13458      if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13459	break;
13460      /* Consume the `,' token.  */
13461      cp_lexer_consume_token (parser->lexer);
13462    }
13463
13464  return clobbers;
13465}
13466
13467/* Parse an (optional) series of attributes.
13468
13469   attributes:
13470     attributes attribute
13471
13472   attribute:
13473     __attribute__ (( attribute-list [opt] ))
13474
13475   The return value is as for cp_parser_attribute_list.  */
13476
13477static tree
13478cp_parser_attributes_opt (cp_parser* parser)
13479{
13480  tree attributes = NULL_TREE;
13481
13482  while (true)
13483    {
13484      cp_token *token;
13485      tree attribute_list;
13486
13487      /* Peek at the next token.  */
13488      token = cp_lexer_peek_token (parser->lexer);
13489      /* If it's not `__attribute__', then we're done.  */
13490      if (token->keyword != RID_ATTRIBUTE)
13491	break;
13492
13493      /* Consume the `__attribute__' keyword.  */
13494      cp_lexer_consume_token (parser->lexer);
13495      /* Look for the two `(' tokens.  */
13496      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13497      cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13498
13499      /* Peek at the next token.  */
13500      token = cp_lexer_peek_token (parser->lexer);
13501      if (token->type != CPP_CLOSE_PAREN)
13502	/* Parse the attribute-list.  */
13503	attribute_list = cp_parser_attribute_list (parser);
13504      else
13505	/* If the next token is a `)', then there is no attribute
13506	   list.  */
13507	attribute_list = NULL;
13508
13509      /* Look for the two `)' tokens.  */
13510      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13511      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13512
13513      /* Add these new attributes to the list.  */
13514      attributes = chainon (attributes, attribute_list);
13515    }
13516
13517  return attributes;
13518}
13519
13520/* Parse an attribute-list.
13521
13522   attribute-list:
13523     attribute
13524     attribute-list , attribute
13525
13526   attribute:
13527     identifier
13528     identifier ( identifier )
13529     identifier ( identifier , expression-list )
13530     identifier ( expression-list )
13531
13532   Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13533   TREE_PURPOSE of each node is the identifier indicating which
13534   attribute is in use.  The TREE_VALUE represents the arguments, if
13535   any.  */
13536
13537static tree
13538cp_parser_attribute_list (cp_parser* parser)
13539{
13540  tree attribute_list = NULL_TREE;
13541
13542  while (true)
13543    {
13544      cp_token *token;
13545      tree identifier;
13546      tree attribute;
13547
13548      /* Look for the identifier.  We also allow keywords here; for
13549	 example `__attribute__ ((const))' is legal.  */
13550      token = cp_lexer_peek_token (parser->lexer);
13551      if (token->type == CPP_NAME
13552	  || token->type == CPP_KEYWORD)
13553	{
13554	  /* Consume the token.  */
13555	  token = cp_lexer_consume_token (parser->lexer);
13556
13557	  /* Save away the identifier that indicates which attribute
13558	     this is.  */
13559	  identifier = token->value;
13560	  attribute = build_tree_list (identifier, NULL_TREE);
13561
13562	  /* Peek at the next token.  */
13563	  token = cp_lexer_peek_token (parser->lexer);
13564	  /* If it's an `(', then parse the attribute arguments.  */
13565	  if (token->type == CPP_OPEN_PAREN)
13566	    {
13567	      tree arguments;
13568
13569	      arguments = (cp_parser_parenthesized_expression_list
13570			   (parser, true, /*non_constant_p=*/NULL));
13571	      /* Save the identifier and arguments away.  */
13572	      TREE_VALUE (attribute) = arguments;
13573	    }
13574
13575	  /* Add this attribute to the list.  */
13576	  TREE_CHAIN (attribute) = attribute_list;
13577	  attribute_list = attribute;
13578
13579	  /* Now, look for more attributes.  */
13580	  token = cp_lexer_peek_token (parser->lexer);
13581	}
13582      /* Now, look for more attributes.  If the next token isn't a
13583	 `,', we're done.  */
13584      if (token->type != CPP_COMMA)
13585	break;
13586
13587      /* Consume the comma and keep going.  */
13588      cp_lexer_consume_token (parser->lexer);
13589    }
13590
13591  /* We built up the list in reverse order.  */
13592  return nreverse (attribute_list);
13593}
13594
13595/* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13596   present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13597   current value of the PEDANTIC flag, regardless of whether or not
13598   the `__extension__' keyword is present.  The caller is responsible
13599   for restoring the value of the PEDANTIC flag.  */
13600
13601static bool
13602cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13603{
13604  /* Save the old value of the PEDANTIC flag.  */
13605  *saved_pedantic = pedantic;
13606
13607  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13608    {
13609      /* Consume the `__extension__' token.  */
13610      cp_lexer_consume_token (parser->lexer);
13611      /* We're not being pedantic while the `__extension__' keyword is
13612	 in effect.  */
13613      pedantic = 0;
13614
13615      return true;
13616    }
13617
13618  return false;
13619}
13620
13621/* Parse a label declaration.
13622
13623   label-declaration:
13624     __label__ label-declarator-seq ;
13625
13626   label-declarator-seq:
13627     identifier , label-declarator-seq
13628     identifier  */
13629
13630static void
13631cp_parser_label_declaration (cp_parser* parser)
13632{
13633  /* Look for the `__label__' keyword.  */
13634  cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13635
13636  while (true)
13637    {
13638      tree identifier;
13639
13640      /* Look for an identifier.  */
13641      identifier = cp_parser_identifier (parser);
13642      /* Declare it as a lobel.  */
13643      finish_label_decl (identifier);
13644      /* If the next token is a `;', stop.  */
13645      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13646	break;
13647      /* Look for the `,' separating the label declarations.  */
13648      cp_parser_require (parser, CPP_COMMA, "`,'");
13649    }
13650
13651  /* Look for the final `;'.  */
13652  cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13653}
13654
13655/* Support Functions */
13656
13657/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13658   NAME should have one of the representations used for an
13659   id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13660   is returned.  If PARSER->SCOPE is a dependent type, then a
13661   SCOPE_REF is returned.
13662
13663   If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13664   returned; the name was already resolved when the TEMPLATE_ID_EXPR
13665   was formed.  Abstractly, such entities should not be passed to this
13666   function, because they do not need to be looked up, but it is
13667   simpler to check for this special case here, rather than at the
13668   call-sites.
13669
13670   In cases not explicitly covered above, this function returns a
13671   DECL, OVERLOAD, or baselink representing the result of the lookup.
13672   If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13673   is returned.
13674
13675   If IS_TYPE is TRUE, bindings that do not refer to types are
13676   ignored.
13677
13678   If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13679   ignored.
13680
13681   If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13682   are ignored.
13683
13684   If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13685   types.  */
13686
13687static tree
13688cp_parser_lookup_name (cp_parser *parser, tree name,
13689		       bool is_type, bool is_template, bool is_namespace,
13690		       bool check_dependency)
13691{
13692  tree decl;
13693  tree object_type = parser->context->object_type;
13694
13695  /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13696     no longer valid.  Note that if we are parsing tentatively, and
13697     the parse fails, OBJECT_TYPE will be automatically restored.  */
13698  parser->context->object_type = NULL_TREE;
13699
13700  if (name == error_mark_node)
13701    return error_mark_node;
13702
13703  /* A template-id has already been resolved; there is no lookup to
13704     do.  */
13705  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13706    return name;
13707  if (BASELINK_P (name))
13708    {
13709      my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13710			   == TEMPLATE_ID_EXPR),
13711			  20020909);
13712      return name;
13713    }
13714
13715  /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13716     it should already have been checked to make sure that the name
13717     used matches the type being destroyed.  */
13718  if (TREE_CODE (name) == BIT_NOT_EXPR)
13719    {
13720      tree type;
13721
13722      /* Figure out to which type this destructor applies.  */
13723      if (parser->scope)
13724	type = parser->scope;
13725      else if (object_type)
13726	type = object_type;
13727      else
13728	type = current_class_type;
13729      /* If that's not a class type, there is no destructor.  */
13730      if (!type || !CLASS_TYPE_P (type))
13731	return error_mark_node;
13732      if (!CLASSTYPE_DESTRUCTORS (type))
13733	  return error_mark_node;
13734      /* If it was a class type, return the destructor.  */
13735      return CLASSTYPE_DESTRUCTORS (type);
13736    }
13737
13738  /* By this point, the NAME should be an ordinary identifier.  If
13739     the id-expression was a qualified name, the qualifying scope is
13740     stored in PARSER->SCOPE at this point.  */
13741  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13742		      20000619);
13743
13744  /* Perform the lookup.  */
13745  if (parser->scope)
13746    {
13747      bool dependent_p;
13748
13749      if (parser->scope == error_mark_node)
13750	return error_mark_node;
13751
13752      /* If the SCOPE is dependent, the lookup must be deferred until
13753	 the template is instantiated -- unless we are explicitly
13754	 looking up names in uninstantiated templates.  Even then, we
13755	 cannot look up the name if the scope is not a class type; it
13756	 might, for example, be a template type parameter.  */
13757      dependent_p = (TYPE_P (parser->scope)
13758		     && !(parser->in_declarator_p
13759			  && currently_open_class (parser->scope))
13760		     && dependent_type_p (parser->scope));
13761      if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13762	   && dependent_p)
13763	{
13764	  if (is_type)
13765	    /* The resolution to Core Issue 180 says that `struct A::B'
13766	       should be considered a type-name, even if `A' is
13767	       dependent.  */
13768	    decl = TYPE_NAME (make_typename_type (parser->scope,
13769						  name,
13770						  /*complain=*/1));
13771	  else if (is_template)
13772	    decl = make_unbound_class_template (parser->scope,
13773						name,
13774						/*complain=*/1);
13775	  else
13776	    decl = build_nt (SCOPE_REF, parser->scope, name);
13777	}
13778      else
13779	{
13780	  bool pop_p = false;
13781
13782	  /* If PARSER->SCOPE is a dependent type, then it must be a
13783	     class type, and we must not be checking dependencies;
13784	     otherwise, we would have processed this lookup above.  So
13785	     that PARSER->SCOPE is not considered a dependent base by
13786	     lookup_member, we must enter the scope here.  */
13787	  if (dependent_p)
13788	    pop_p = push_scope (parser->scope);
13789	  /* If the PARSER->SCOPE is a a template specialization, it
13790	     may be instantiated during name lookup.  In that case,
13791	     errors may be issued.  Even if we rollback the current
13792	     tentative parse, those errors are valid.  */
13793	  decl = lookup_qualified_name (parser->scope, name, is_type,
13794					/*complain=*/true);
13795	  if (pop_p)
13796	    pop_scope (parser->scope);
13797	}
13798      parser->qualifying_scope = parser->scope;
13799      parser->object_scope = NULL_TREE;
13800    }
13801  else if (object_type)
13802    {
13803      tree object_decl = NULL_TREE;
13804      /* Look up the name in the scope of the OBJECT_TYPE, unless the
13805	 OBJECT_TYPE is not a class.  */
13806      if (CLASS_TYPE_P (object_type))
13807	/* If the OBJECT_TYPE is a template specialization, it may
13808	   be instantiated during name lookup.  In that case, errors
13809	   may be issued.  Even if we rollback the current tentative
13810	   parse, those errors are valid.  */
13811	object_decl = lookup_member (object_type,
13812				     name,
13813				     /*protect=*/0, is_type);
13814      /* Look it up in the enclosing context, too.  */
13815      decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13816			       is_namespace,
13817			       /*flags=*/0);
13818      parser->object_scope = object_type;
13819      parser->qualifying_scope = NULL_TREE;
13820      if (object_decl)
13821	decl = object_decl;
13822    }
13823  else
13824    {
13825      decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13826			       is_namespace,
13827			       /*flags=*/0);
13828      parser->qualifying_scope = NULL_TREE;
13829      parser->object_scope = NULL_TREE;
13830    }
13831
13832  /* If the lookup failed, let our caller know.  */
13833  if (!decl
13834      || decl == error_mark_node
13835      || (TREE_CODE (decl) == FUNCTION_DECL
13836	  && DECL_ANTICIPATED (decl)))
13837    return error_mark_node;
13838
13839  /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13840  if (TREE_CODE (decl) == TREE_LIST)
13841    {
13842      /* The error message we have to print is too complicated for
13843	 cp_parser_error, so we incorporate its actions directly.  */
13844      if (!cp_parser_simulate_error (parser))
13845	{
13846	  error ("reference to `%D' is ambiguous", name);
13847	  print_candidates (decl);
13848	}
13849      return error_mark_node;
13850    }
13851
13852  my_friendly_assert (DECL_P (decl)
13853		      || TREE_CODE (decl) == OVERLOAD
13854		      || TREE_CODE (decl) == SCOPE_REF
13855		      || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13856		      || BASELINK_P (decl),
13857		      20000619);
13858
13859  /* If we have resolved the name of a member declaration, check to
13860     see if the declaration is accessible.  When the name resolves to
13861     set of overloaded functions, accessibility is checked when
13862     overload resolution is done.
13863
13864     During an explicit instantiation, access is not checked at all,
13865     as per [temp.explicit].  */
13866  if (DECL_P (decl))
13867    check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13868
13869  return decl;
13870}
13871
13872/* Like cp_parser_lookup_name, but for use in the typical case where
13873   CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13874   IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
13875
13876static tree
13877cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13878{
13879  return cp_parser_lookup_name (parser, name,
13880				/*is_type=*/false,
13881				/*is_template=*/false,
13882				/*is_namespace=*/false,
13883				/*check_dependency=*/true);
13884}
13885
13886/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13887   the current context, return the TYPE_DECL.  If TAG_NAME_P is
13888   true, the DECL indicates the class being defined in a class-head,
13889   or declared in an elaborated-type-specifier.
13890
13891   Otherwise, return DECL.  */
13892
13893static tree
13894cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13895{
13896  /* If the TEMPLATE_DECL is being declared as part of a class-head,
13897     the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13898
13899       struct A {
13900         template <typename T> struct B;
13901       };
13902
13903       template <typename T> struct A::B {};
13904
13905     Similarly, in a elaborated-type-specifier:
13906
13907       namespace N { struct X{}; }
13908
13909       struct A {
13910         template <typename T> friend struct N::X;
13911       };
13912
13913     However, if the DECL refers to a class type, and we are in
13914     the scope of the class, then the name lookup automatically
13915     finds the TYPE_DECL created by build_self_reference rather
13916     than a TEMPLATE_DECL.  For example, in:
13917
13918       template <class T> struct S {
13919         S s;
13920       };
13921
13922     there is no need to handle such case.  */
13923
13924  if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13925    return DECL_TEMPLATE_RESULT (decl);
13926
13927  return decl;
13928}
13929
13930/* If too many, or too few, template-parameter lists apply to the
13931   declarator, issue an error message.  Returns TRUE if all went well,
13932   and FALSE otherwise.  */
13933
13934static bool
13935cp_parser_check_declarator_template_parameters (cp_parser* parser,
13936                                                tree declarator)
13937{
13938  unsigned num_templates;
13939
13940  /* We haven't seen any classes that involve template parameters yet.  */
13941  num_templates = 0;
13942
13943  switch (TREE_CODE (declarator))
13944    {
13945    case CALL_EXPR:
13946    case ARRAY_REF:
13947    case INDIRECT_REF:
13948    case ADDR_EXPR:
13949      {
13950	tree main_declarator = TREE_OPERAND (declarator, 0);
13951	return
13952	  cp_parser_check_declarator_template_parameters (parser,
13953							  main_declarator);
13954      }
13955
13956    case SCOPE_REF:
13957      {
13958	tree scope;
13959	tree member;
13960
13961	scope = TREE_OPERAND (declarator, 0);
13962	member = TREE_OPERAND (declarator, 1);
13963
13964	/* If this is a pointer-to-member, then we are not interested
13965	   in the SCOPE, because it does not qualify the thing that is
13966	   being declared.  */
13967	if (TREE_CODE (member) == INDIRECT_REF)
13968	  return (cp_parser_check_declarator_template_parameters
13969		  (parser, member));
13970
13971	while (scope && CLASS_TYPE_P (scope))
13972	  {
13973	    /* You're supposed to have one `template <...>'
13974	       for every template class, but you don't need one
13975	       for a full specialization.  For example:
13976
13977	       template <class T> struct S{};
13978	       template <> struct S<int> { void f(); };
13979	       void S<int>::f () {}
13980
13981	       is correct; there shouldn't be a `template <>' for
13982	       the definition of `S<int>::f'.  */
13983	    if (CLASSTYPE_TEMPLATE_INFO (scope)
13984		&& (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13985		    || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13986		&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13987	      ++num_templates;
13988
13989	    scope = TYPE_CONTEXT (scope);
13990	  }
13991      }
13992
13993      /* Fall through.  */
13994
13995    default:
13996      /* If the DECLARATOR has the form `X<y>' then it uses one
13997	 additional level of template parameters.  */
13998      if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13999	++num_templates;
14000
14001      return cp_parser_check_template_parameters (parser,
14002						  num_templates);
14003    }
14004}
14005
14006/* NUM_TEMPLATES were used in the current declaration.  If that is
14007   invalid, return FALSE and issue an error messages.  Otherwise,
14008   return TRUE.  */
14009
14010static bool
14011cp_parser_check_template_parameters (cp_parser* parser,
14012                                     unsigned num_templates)
14013{
14014  /* If there are more template classes than parameter lists, we have
14015     something like:
14016
14017       template <class T> void S<T>::R<T>::f ();  */
14018  if (parser->num_template_parameter_lists < num_templates)
14019    {
14020      error ("too few template-parameter-lists");
14021      return false;
14022    }
14023  /* If there are the same number of template classes and parameter
14024     lists, that's OK.  */
14025  if (parser->num_template_parameter_lists == num_templates)
14026    return true;
14027  /* If there are more, but only one more, then we are referring to a
14028     member template.  That's OK too.  */
14029  if (parser->num_template_parameter_lists == num_templates + 1)
14030      return true;
14031  /* Otherwise, there are too many template parameter lists.  We have
14032     something like:
14033
14034     template <class T> template <class U> void S::f();  */
14035  error ("too many template-parameter-lists");
14036  return false;
14037}
14038
14039/* Parse a binary-expression of the general form:
14040
14041   binary-expression:
14042     <expr>
14043     binary-expression <token> <expr>
14044
14045   The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
14046   to parser the <expr>s.  If the first production is used, then the
14047   value returned by FN is returned directly.  Otherwise, a node with
14048   the indicated EXPR_TYPE is returned, with operands corresponding to
14049   the two sub-expressions.  */
14050
14051static tree
14052cp_parser_binary_expression (cp_parser* parser,
14053                             const cp_parser_token_tree_map token_tree_map,
14054                             cp_parser_expression_fn fn)
14055{
14056  tree lhs;
14057
14058  /* Parse the first expression.  */
14059  lhs = (*fn) (parser);
14060  /* Now, look for more expressions.  */
14061  while (true)
14062    {
14063      cp_token *token;
14064      const cp_parser_token_tree_map_node *map_node;
14065      tree rhs;
14066
14067      /* Peek at the next token.  */
14068      token = cp_lexer_peek_token (parser->lexer);
14069      /* If the token is `>', and that's not an operator at the
14070	 moment, then we're done.  */
14071      if (token->type == CPP_GREATER
14072	  && !parser->greater_than_is_operator_p)
14073	break;
14074      /* If we find one of the tokens we want, build the corresponding
14075	 tree representation.  */
14076      for (map_node = token_tree_map;
14077	   map_node->token_type != CPP_EOF;
14078	   ++map_node)
14079	if (map_node->token_type == token->type)
14080	  {
14081	    /* Assume that an overloaded operator will not be used.  */
14082	    bool overloaded_p = false;
14083
14084	    /* Consume the operator token.  */
14085	    cp_lexer_consume_token (parser->lexer);
14086	    /* Parse the right-hand side of the expression.  */
14087	    rhs = (*fn) (parser);
14088	    /* Build the binary tree node.  */
14089	    lhs = build_x_binary_op (map_node->tree_type, lhs, rhs,
14090				     &overloaded_p);
14091	    /* If the binary operator required the use of an
14092	       overloaded operator, then this expression cannot be an
14093	       integral constant-expression.  An overloaded operator
14094	       can be used even if both operands are otherwise
14095	       permissible in an integral constant-expression if at
14096	       least one of the operands is of enumeration type.  */
14097	    if (overloaded_p
14098		&& (cp_parser_non_integral_constant_expression
14099		    (parser, "calls to overloaded operators")))
14100	      lhs = error_mark_node;
14101	    break;
14102	  }
14103
14104      /* If the token wasn't one of the ones we want, we're done.  */
14105      if (map_node->token_type == CPP_EOF)
14106	break;
14107    }
14108
14109  return lhs;
14110}
14111
14112/* Parse an optional `::' token indicating that the following name is
14113   from the global namespace.  If so, PARSER->SCOPE is set to the
14114   GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14115   unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14116   Returns the new value of PARSER->SCOPE, if the `::' token is
14117   present, and NULL_TREE otherwise.  */
14118
14119static tree
14120cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14121{
14122  cp_token *token;
14123
14124  /* Peek at the next token.  */
14125  token = cp_lexer_peek_token (parser->lexer);
14126  /* If we're looking at a `::' token then we're starting from the
14127     global namespace, not our current location.  */
14128  if (token->type == CPP_SCOPE)
14129    {
14130      /* Consume the `::' token.  */
14131      cp_lexer_consume_token (parser->lexer);
14132      /* Set the SCOPE so that we know where to start the lookup.  */
14133      parser->scope = global_namespace;
14134      parser->qualifying_scope = global_namespace;
14135      parser->object_scope = NULL_TREE;
14136
14137      return parser->scope;
14138    }
14139  else if (!current_scope_valid_p)
14140    {
14141      parser->scope = NULL_TREE;
14142      parser->qualifying_scope = NULL_TREE;
14143      parser->object_scope = NULL_TREE;
14144    }
14145
14146  return NULL_TREE;
14147}
14148
14149/* Returns TRUE if the upcoming token sequence is the start of a
14150   constructor declarator.  If FRIEND_P is true, the declarator is
14151   preceded by the `friend' specifier.  */
14152
14153static bool
14154cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14155{
14156  bool constructor_p;
14157  tree type_decl = NULL_TREE;
14158  bool nested_name_p;
14159  cp_token *next_token;
14160
14161  /* The common case is that this is not a constructor declarator, so
14162     try to avoid doing lots of work if at all possible.  It's not
14163     valid declare a constructor at function scope.  */
14164  if (at_function_scope_p ())
14165    return false;
14166  /* And only certain tokens can begin a constructor declarator.  */
14167  next_token = cp_lexer_peek_token (parser->lexer);
14168  if (next_token->type != CPP_NAME
14169      && next_token->type != CPP_SCOPE
14170      && next_token->type != CPP_NESTED_NAME_SPECIFIER
14171      && next_token->type != CPP_TEMPLATE_ID)
14172    return false;
14173
14174  /* Parse tentatively; we are going to roll back all of the tokens
14175     consumed here.  */
14176  cp_parser_parse_tentatively (parser);
14177  /* Assume that we are looking at a constructor declarator.  */
14178  constructor_p = true;
14179
14180  /* Look for the optional `::' operator.  */
14181  cp_parser_global_scope_opt (parser,
14182			      /*current_scope_valid_p=*/false);
14183  /* Look for the nested-name-specifier.  */
14184  nested_name_p
14185    = (cp_parser_nested_name_specifier_opt (parser,
14186					    /*typename_keyword_p=*/false,
14187					    /*check_dependency_p=*/false,
14188					    /*type_p=*/false,
14189					    /*is_declaration=*/false)
14190       != NULL_TREE);
14191  /* Outside of a class-specifier, there must be a
14192     nested-name-specifier.  */
14193  if (!nested_name_p &&
14194      (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14195       || friend_p))
14196    constructor_p = false;
14197  /* If we still think that this might be a constructor-declarator,
14198     look for a class-name.  */
14199  if (constructor_p)
14200    {
14201      /* If we have:
14202
14203	   template <typename T> struct S { S(); };
14204	   template <typename T> S<T>::S ();
14205
14206	 we must recognize that the nested `S' names a class.
14207	 Similarly, for:
14208
14209	   template <typename T> S<T>::S<T> ();
14210
14211	 we must recognize that the nested `S' names a template.  */
14212      type_decl = cp_parser_class_name (parser,
14213					/*typename_keyword_p=*/false,
14214					/*template_keyword_p=*/false,
14215					/*type_p=*/false,
14216					/*check_dependency_p=*/false,
14217					/*class_head_p=*/false,
14218					/*is_declaration=*/false);
14219      /* If there was no class-name, then this is not a constructor.  */
14220      constructor_p = !cp_parser_error_occurred (parser);
14221    }
14222
14223  /* If we're still considering a constructor, we have to see a `(',
14224     to begin the parameter-declaration-clause, followed by either a
14225     `)', an `...', or a decl-specifier.  We need to check for a
14226     type-specifier to avoid being fooled into thinking that:
14227
14228       S::S (f) (int);
14229
14230     is a constructor.  (It is actually a function named `f' that
14231     takes one parameter (of type `int') and returns a value of type
14232     `S::S'.  */
14233  if (constructor_p
14234      && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14235    {
14236      if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14237	  && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14238	  /* A parameter declaration begins with a decl-specifier,
14239	     which is either the "attribute" keyword, a storage class
14240	     specifier, or (usually) a type-specifier.  */
14241	  && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14242	  && !cp_parser_storage_class_specifier_opt (parser))
14243	{
14244	  tree type;
14245	  bool pop_p = false;
14246	  unsigned saved_num_template_parameter_lists;
14247
14248	  /* Names appearing in the type-specifier should be looked up
14249	     in the scope of the class.  */
14250	  if (current_class_type)
14251	    type = NULL_TREE;
14252	  else
14253	    {
14254	      type = TREE_TYPE (type_decl);
14255	      if (TREE_CODE (type) == TYPENAME_TYPE)
14256		{
14257		  type = resolve_typename_type (type,
14258						/*only_current_p=*/false);
14259		  if (type == error_mark_node)
14260		    {
14261		      cp_parser_abort_tentative_parse (parser);
14262		      return false;
14263		    }
14264		}
14265	      pop_p = push_scope (type);
14266	    }
14267
14268	  /* Inside the constructor parameter list, surrounding
14269	     template-parameter-lists do not apply.  */
14270	  saved_num_template_parameter_lists
14271	    = parser->num_template_parameter_lists;
14272	  parser->num_template_parameter_lists = 0;
14273
14274	  /* Look for the type-specifier.  */
14275	  cp_parser_type_specifier (parser,
14276				    CP_PARSER_FLAGS_NONE,
14277				    /*is_friend=*/false,
14278				    /*is_declarator=*/true,
14279				    /*declares_class_or_enum=*/NULL,
14280				    /*is_cv_qualifier=*/NULL);
14281
14282	  parser->num_template_parameter_lists
14283	    = saved_num_template_parameter_lists;
14284
14285	  /* Leave the scope of the class.  */
14286	  if (pop_p)
14287	    pop_scope (type);
14288
14289	  constructor_p = !cp_parser_error_occurred (parser);
14290	}
14291    }
14292  else
14293    constructor_p = false;
14294  /* We did not really want to consume any tokens.  */
14295  cp_parser_abort_tentative_parse (parser);
14296
14297  return constructor_p;
14298}
14299
14300/* Parse the definition of the function given by the DECL_SPECIFIERS,
14301   ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14302   they must be performed once we are in the scope of the function.
14303
14304   Returns the function defined.  */
14305
14306static tree
14307cp_parser_function_definition_from_specifiers_and_declarator
14308  (cp_parser* parser,
14309   tree decl_specifiers,
14310   tree attributes,
14311   tree declarator)
14312{
14313  tree fn;
14314  bool success_p;
14315
14316  /* Begin the function-definition.  */
14317  success_p = begin_function_definition (decl_specifiers,
14318					 attributes,
14319					 declarator);
14320
14321  /* If there were names looked up in the decl-specifier-seq that we
14322     did not check, check them now.  We must wait until we are in the
14323     scope of the function to perform the checks, since the function
14324     might be a friend.  */
14325  perform_deferred_access_checks ();
14326
14327  if (!success_p)
14328    {
14329      /* If begin_function_definition didn't like the definition, skip
14330	 the entire function.  */
14331      error ("invalid function declaration");
14332      cp_parser_skip_to_end_of_block_or_statement (parser);
14333      fn = error_mark_node;
14334    }
14335  else
14336    fn = cp_parser_function_definition_after_declarator (parser,
14337							 /*inline_p=*/false);
14338
14339  return fn;
14340}
14341
14342/* Parse the part of a function-definition that follows the
14343   declarator.  INLINE_P is TRUE iff this function is an inline
14344   function defined with a class-specifier.
14345
14346   Returns the function defined.  */
14347
14348static tree
14349cp_parser_function_definition_after_declarator (cp_parser* parser,
14350						bool inline_p)
14351{
14352  tree fn;
14353  bool ctor_initializer_p = false;
14354  bool saved_in_unbraced_linkage_specification_p;
14355  unsigned saved_num_template_parameter_lists;
14356
14357  /* If the next token is `return', then the code may be trying to
14358     make use of the "named return value" extension that G++ used to
14359     support.  */
14360  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14361    {
14362      /* Consume the `return' keyword.  */
14363      cp_lexer_consume_token (parser->lexer);
14364      /* Look for the identifier that indicates what value is to be
14365	 returned.  */
14366      cp_parser_identifier (parser);
14367      /* Issue an error message.  */
14368      error ("named return values are no longer supported");
14369      /* Skip tokens until we reach the start of the function body.  */
14370      while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14371	     && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14372	cp_lexer_consume_token (parser->lexer);
14373    }
14374  /* The `extern' in `extern "C" void f () { ... }' does not apply to
14375     anything declared inside `f'.  */
14376  saved_in_unbraced_linkage_specification_p
14377    = parser->in_unbraced_linkage_specification_p;
14378  parser->in_unbraced_linkage_specification_p = false;
14379  /* Inside the function, surrounding template-parameter-lists do not
14380     apply.  */
14381  saved_num_template_parameter_lists
14382    = parser->num_template_parameter_lists;
14383  parser->num_template_parameter_lists = 0;
14384  /* If the next token is `try', then we are looking at a
14385     function-try-block.  */
14386  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14387    ctor_initializer_p = cp_parser_function_try_block (parser);
14388  /* A function-try-block includes the function-body, so we only do
14389     this next part if we're not processing a function-try-block.  */
14390  else
14391    ctor_initializer_p
14392      = cp_parser_ctor_initializer_opt_and_function_body (parser);
14393
14394  /* Finish the function.  */
14395  fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14396			(inline_p ? 2 : 0));
14397  /* Generate code for it, if necessary.  */
14398  expand_or_defer_fn (fn);
14399  /* Restore the saved values.  */
14400  parser->in_unbraced_linkage_specification_p
14401    = saved_in_unbraced_linkage_specification_p;
14402  parser->num_template_parameter_lists
14403    = saved_num_template_parameter_lists;
14404
14405  return fn;
14406}
14407
14408/* Parse a template-declaration, assuming that the `export' (and
14409   `extern') keywords, if present, has already been scanned.  MEMBER_P
14410   is as for cp_parser_template_declaration.  */
14411
14412static void
14413cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14414{
14415  tree decl = NULL_TREE;
14416  tree parameter_list;
14417  bool friend_p = false;
14418
14419  /* Look for the `template' keyword.  */
14420  if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14421    return;
14422
14423  /* And the `<'.  */
14424  if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14425    return;
14426
14427  /* If the next token is `>', then we have an invalid
14428     specialization.  Rather than complain about an invalid template
14429     parameter, issue an error message here.  */
14430  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14431    {
14432      cp_parser_error (parser, "invalid explicit specialization");
14433      begin_specialization ();
14434      parameter_list = NULL_TREE;
14435    }
14436  else
14437    {
14438      /* Parse the template parameters.  */
14439      begin_template_parm_list ();
14440      parameter_list = cp_parser_template_parameter_list (parser);
14441      parameter_list = end_template_parm_list (parameter_list);
14442    }
14443
14444  /* Look for the `>'.  */
14445  cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14446  /* We just processed one more parameter list.  */
14447  ++parser->num_template_parameter_lists;
14448  /* If the next token is `template', there are more template
14449     parameters.  */
14450  if (cp_lexer_next_token_is_keyword (parser->lexer,
14451				      RID_TEMPLATE))
14452    cp_parser_template_declaration_after_export (parser, member_p);
14453  else
14454    {
14455      decl = cp_parser_single_declaration (parser,
14456					   member_p,
14457					   &friend_p);
14458
14459      /* If this is a member template declaration, let the front
14460	 end know.  */
14461      if (member_p && !friend_p && decl)
14462	{
14463	  if (TREE_CODE (decl) == TYPE_DECL)
14464	    cp_parser_check_access_in_redeclaration (decl);
14465
14466	  decl = finish_member_template_decl (decl);
14467	}
14468      else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14469	make_friend_class (current_class_type, TREE_TYPE (decl),
14470			   /*complain=*/true);
14471    }
14472  /* We are done with the current parameter list.  */
14473  --parser->num_template_parameter_lists;
14474
14475  /* Finish up.  */
14476  finish_template_decl (parameter_list);
14477
14478  /* Register member declarations.  */
14479  if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14480    finish_member_declaration (decl);
14481
14482  /* If DECL is a function template, we must return to parse it later.
14483     (Even though there is no definition, there might be default
14484     arguments that need handling.)  */
14485  if (member_p && decl
14486      && (TREE_CODE (decl) == FUNCTION_DECL
14487	  || DECL_FUNCTION_TEMPLATE_P (decl)))
14488    TREE_VALUE (parser->unparsed_functions_queues)
14489      = tree_cons (NULL_TREE, decl,
14490		   TREE_VALUE (parser->unparsed_functions_queues));
14491}
14492
14493/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14494   `function-definition' sequence.  MEMBER_P is true, this declaration
14495   appears in a class scope.
14496
14497   Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14498   *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14499
14500static tree
14501cp_parser_single_declaration (cp_parser* parser,
14502			      bool member_p,
14503			      bool* friend_p)
14504{
14505  int declares_class_or_enum;
14506  tree decl = NULL_TREE;
14507  tree decl_specifiers;
14508  tree attributes;
14509  bool function_definition_p = false;
14510
14511  /* Defer access checks until we know what is being declared.  */
14512  push_deferring_access_checks (dk_deferred);
14513
14514  /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14515     alternative.  */
14516  decl_specifiers
14517    = cp_parser_decl_specifier_seq (parser,
14518				    CP_PARSER_FLAGS_OPTIONAL,
14519				    &attributes,
14520				    &declares_class_or_enum);
14521  if (friend_p)
14522    *friend_p = cp_parser_friend_p (decl_specifiers);
14523  /* Gather up the access checks that occurred the
14524     decl-specifier-seq.  */
14525  stop_deferring_access_checks ();
14526
14527  /* Check for the declaration of a template class.  */
14528  if (declares_class_or_enum)
14529    {
14530      if (cp_parser_declares_only_class_p (parser))
14531	{
14532	  decl = shadow_tag (decl_specifiers);
14533	  if (decl)
14534	    decl = TYPE_NAME (decl);
14535	  else
14536	    decl = error_mark_node;
14537	}
14538    }
14539  else
14540    decl = NULL_TREE;
14541  /* If it's not a template class, try for a template function.  If
14542     the next token is a `;', then this declaration does not declare
14543     anything.  But, if there were errors in the decl-specifiers, then
14544     the error might well have come from an attempted class-specifier.
14545     In that case, there's no need to warn about a missing declarator.  */
14546  if (!decl
14547      && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14548	  || !value_member (error_mark_node, decl_specifiers)))
14549    decl = cp_parser_init_declarator (parser,
14550				      decl_specifiers,
14551				      attributes,
14552				      /*function_definition_allowed_p=*/true,
14553				      member_p,
14554				      declares_class_or_enum,
14555				      &function_definition_p);
14556
14557  pop_deferring_access_checks ();
14558
14559  /* Clear any current qualification; whatever comes next is the start
14560     of something new.  */
14561  parser->scope = NULL_TREE;
14562  parser->qualifying_scope = NULL_TREE;
14563  parser->object_scope = NULL_TREE;
14564  /* Look for a trailing `;' after the declaration.  */
14565  if (!function_definition_p
14566      && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14567    cp_parser_skip_to_end_of_block_or_statement (parser);
14568
14569  return decl;
14570}
14571
14572/* Parse a cast-expression that is not the operand of a unary "&".  */
14573
14574static tree
14575cp_parser_simple_cast_expression (cp_parser *parser)
14576{
14577  return cp_parser_cast_expression (parser, /*address_p=*/false);
14578}
14579
14580/* Parse a functional cast to TYPE.  Returns an expression
14581   representing the cast.  */
14582
14583static tree
14584cp_parser_functional_cast (cp_parser* parser, tree type)
14585{
14586  tree expression_list;
14587  tree cast;
14588
14589  expression_list
14590    = cp_parser_parenthesized_expression_list (parser, false,
14591					       /*non_constant_p=*/NULL);
14592
14593  cast = build_functional_cast (type, expression_list);
14594  /* [expr.const]/1: In an integral constant expression "only type
14595     conversions to integral or enumeration type can be used".  */
14596  if (cast != error_mark_node && !type_dependent_expression_p (type)
14597      && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14598    {
14599      if (cp_parser_non_integral_constant_expression
14600	  (parser, "a call to a constructor"))
14601	return error_mark_node;
14602    }
14603  return cast;
14604}
14605
14606/* Save the tokens that make up the body of a member function defined
14607   in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14608   already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14609   specifiers applied to the declaration.  Returns the FUNCTION_DECL
14610   for the member function.  */
14611
14612static tree
14613cp_parser_save_member_function_body (cp_parser* parser,
14614				     tree decl_specifiers,
14615				     tree declarator,
14616				     tree attributes)
14617{
14618  cp_token_cache *cache;
14619  tree fn;
14620
14621  /* Create the function-declaration.  */
14622  fn = start_method (decl_specifiers, declarator, attributes);
14623  /* If something went badly wrong, bail out now.  */
14624  if (fn == error_mark_node)
14625    {
14626      /* If there's a function-body, skip it.  */
14627      if (cp_parser_token_starts_function_definition_p
14628	  (cp_lexer_peek_token (parser->lexer)))
14629	cp_parser_skip_to_end_of_block_or_statement (parser);
14630      return error_mark_node;
14631    }
14632
14633  /* Remember it, if there default args to post process.  */
14634  cp_parser_save_default_args (parser, fn);
14635
14636  /* Create a token cache.  */
14637  cache = cp_token_cache_new ();
14638  /* Save away the tokens that make up the body of the
14639     function.  */
14640  cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14641  /* Handle function try blocks.  */
14642  while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14643    cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14644
14645  /* Save away the inline definition; we will process it when the
14646     class is complete.  */
14647  DECL_PENDING_INLINE_INFO (fn) = cache;
14648  DECL_PENDING_INLINE_P (fn) = 1;
14649
14650  /* We need to know that this was defined in the class, so that
14651     friend templates are handled correctly.  */
14652  DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14653
14654  /* We're done with the inline definition.  */
14655  finish_method (fn);
14656
14657  /* Add FN to the queue of functions to be parsed later.  */
14658  TREE_VALUE (parser->unparsed_functions_queues)
14659    = tree_cons (NULL_TREE, fn,
14660		 TREE_VALUE (parser->unparsed_functions_queues));
14661
14662  return fn;
14663}
14664
14665/* Parse a template-argument-list, as well as the trailing ">" (but
14666   not the opening ">").  See cp_parser_template_argument_list for the
14667   return value.  */
14668
14669static tree
14670cp_parser_enclosed_template_argument_list (cp_parser* parser)
14671{
14672  tree arguments;
14673  tree saved_scope;
14674  tree saved_qualifying_scope;
14675  tree saved_object_scope;
14676  bool saved_greater_than_is_operator_p;
14677
14678  /* [temp.names]
14679
14680     When parsing a template-id, the first non-nested `>' is taken as
14681     the end of the template-argument-list rather than a greater-than
14682     operator.  */
14683  saved_greater_than_is_operator_p
14684    = parser->greater_than_is_operator_p;
14685  parser->greater_than_is_operator_p = false;
14686  /* Parsing the argument list may modify SCOPE, so we save it
14687     here.  */
14688  saved_scope = parser->scope;
14689  saved_qualifying_scope = parser->qualifying_scope;
14690  saved_object_scope = parser->object_scope;
14691  /* Parse the template-argument-list itself.  */
14692  if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14693    arguments = NULL_TREE;
14694  else
14695    arguments = cp_parser_template_argument_list (parser);
14696  /* Look for the `>' that ends the template-argument-list. If we find
14697     a '>>' instead, it's probably just a typo.  */
14698  if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14699    {
14700      if (!saved_greater_than_is_operator_p)
14701	{
14702	  /* If we're in a nested template argument list, the '>>' has to be
14703	    a typo for '> >'. We emit the error message, but we continue
14704	    parsing and we push a '>' as next token, so that the argument
14705	    list will be parsed correctly..  */
14706	  cp_token* token;
14707	  error ("`>>' should be `> >' within a nested template argument list");
14708	  token = cp_lexer_peek_token (parser->lexer);
14709	  token->type = CPP_GREATER;
14710	}
14711      else
14712	{
14713	  /* If this is not a nested template argument list, the '>>' is
14714	    a typo for '>'. Emit an error message and continue.  */
14715	  error ("spurious `>>', use `>' to terminate a template argument list");
14716	  cp_lexer_consume_token (parser->lexer);
14717	}
14718    }
14719  else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14720    error ("missing `>' to terminate the template argument list");
14721  /* The `>' token might be a greater-than operator again now.  */
14722  parser->greater_than_is_operator_p
14723    = saved_greater_than_is_operator_p;
14724  /* Restore the SAVED_SCOPE.  */
14725  parser->scope = saved_scope;
14726  parser->qualifying_scope = saved_qualifying_scope;
14727  parser->object_scope = saved_object_scope;
14728
14729  return arguments;
14730}
14731
14732/* MEMBER_FUNCTION is a member function, or a friend.  If default
14733   arguments, or the body of the function have not yet been parsed,
14734   parse them now.  */
14735
14736static void
14737cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14738{
14739  cp_lexer *saved_lexer;
14740
14741  /* If this member is a template, get the underlying
14742     FUNCTION_DECL.  */
14743  if (DECL_FUNCTION_TEMPLATE_P (member_function))
14744    member_function = DECL_TEMPLATE_RESULT (member_function);
14745
14746  /* There should not be any class definitions in progress at this
14747     point; the bodies of members are only parsed outside of all class
14748     definitions.  */
14749  my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14750  /* While we're parsing the member functions we might encounter more
14751     classes.  We want to handle them right away, but we don't want
14752     them getting mixed up with functions that are currently in the
14753     queue.  */
14754  parser->unparsed_functions_queues
14755    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14756
14757  /* Make sure that any template parameters are in scope.  */
14758  maybe_begin_member_template_processing (member_function);
14759
14760  /* If the body of the function has not yet been parsed, parse it
14761     now.  */
14762  if (DECL_PENDING_INLINE_P (member_function))
14763    {
14764      tree function_scope;
14765      cp_token_cache *tokens;
14766
14767      /* The function is no longer pending; we are processing it.  */
14768      tokens = DECL_PENDING_INLINE_INFO (member_function);
14769      DECL_PENDING_INLINE_INFO (member_function) = NULL;
14770      DECL_PENDING_INLINE_P (member_function) = 0;
14771      /* If this was an inline function in a local class, enter the scope
14772	 of the containing function.  */
14773      function_scope = decl_function_context (member_function);
14774      if (function_scope)
14775	push_function_context_to (function_scope);
14776
14777      /* Save away the current lexer.  */
14778      saved_lexer = parser->lexer;
14779      /* Make a new lexer to feed us the tokens saved for this function.  */
14780      parser->lexer = cp_lexer_new_from_tokens (tokens);
14781      parser->lexer->next = saved_lexer;
14782
14783      /* Set the current source position to be the location of the first
14784	 token in the saved inline body.  */
14785      cp_lexer_peek_token (parser->lexer);
14786
14787      /* Let the front end know that we going to be defining this
14788	 function.  */
14789      start_function (NULL_TREE, member_function, NULL_TREE,
14790		      SF_PRE_PARSED | SF_INCLASS_INLINE);
14791
14792      /* Now, parse the body of the function.  */
14793      cp_parser_function_definition_after_declarator (parser,
14794						      /*inline_p=*/true);
14795
14796      /* Leave the scope of the containing function.  */
14797      if (function_scope)
14798	pop_function_context_from (function_scope);
14799      /* Restore the lexer.  */
14800      parser->lexer = saved_lexer;
14801    }
14802
14803  /* Remove any template parameters from the symbol table.  */
14804  maybe_end_member_template_processing ();
14805
14806  /* Restore the queue.  */
14807  parser->unparsed_functions_queues
14808    = TREE_CHAIN (parser->unparsed_functions_queues);
14809}
14810
14811/* If DECL contains any default args, remember it on the unparsed
14812   functions queue.  */
14813
14814static void
14815cp_parser_save_default_args (cp_parser* parser, tree decl)
14816{
14817  tree probe;
14818
14819  for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14820       probe;
14821       probe = TREE_CHAIN (probe))
14822    if (TREE_PURPOSE (probe))
14823      {
14824	TREE_PURPOSE (parser->unparsed_functions_queues)
14825	  = tree_cons (NULL_TREE, decl,
14826		       TREE_PURPOSE (parser->unparsed_functions_queues));
14827	break;
14828      }
14829  return;
14830}
14831
14832/* FN is a FUNCTION_DECL which may contains a parameter with an
14833   unparsed DEFAULT_ARG.  Parse the default args now.  */
14834
14835static void
14836cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14837{
14838  cp_lexer *saved_lexer;
14839  cp_token_cache *tokens;
14840  bool saved_local_variables_forbidden_p;
14841  tree parameters;
14842
14843  /* While we're parsing the default args, we might (due to the
14844     statement expression extension) encounter more classes.  We want
14845     to handle them right away, but we don't want them getting mixed
14846     up with default args that are currently in the queue.  */
14847  parser->unparsed_functions_queues
14848    = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14849
14850  for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14851       parameters;
14852       parameters = TREE_CHAIN (parameters))
14853    {
14854      if (!TREE_PURPOSE (parameters)
14855	  || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14856	continue;
14857
14858       /* Save away the current lexer.  */
14859      saved_lexer = parser->lexer;
14860       /* Create a new one, using the tokens we have saved.  */
14861      tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
14862      parser->lexer = cp_lexer_new_from_tokens (tokens);
14863
14864       /* Set the current source position to be the location of the
14865     	  first token in the default argument.  */
14866      cp_lexer_peek_token (parser->lexer);
14867
14868       /* Local variable names (and the `this' keyword) may not appear
14869     	  in a default argument.  */
14870      saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14871      parser->local_variables_forbidden_p = true;
14872       /* Parse the assignment-expression.  */
14873      if (DECL_FRIEND_CONTEXT (fn))
14874	push_nested_class (DECL_FRIEND_CONTEXT (fn));
14875      else if (DECL_CLASS_SCOPE_P (fn))
14876	push_nested_class (DECL_CONTEXT (fn));
14877      TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14878      if (DECL_FRIEND_CONTEXT (fn) || DECL_CLASS_SCOPE_P (fn))
14879	pop_nested_class ();
14880
14881      /* If the token stream has not been completely used up, then
14882	 there was extra junk after the end of the default
14883	 argument.  */
14884      if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14885	cp_parser_error (parser, "expected `,'");
14886
14887       /* Restore saved state.  */
14888      parser->lexer = saved_lexer;
14889      parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14890    }
14891
14892  /* Restore the queue.  */
14893  parser->unparsed_functions_queues
14894    = TREE_CHAIN (parser->unparsed_functions_queues);
14895}
14896
14897/* Parse the operand of `sizeof' (or a similar operator).  Returns
14898   either a TYPE or an expression, depending on the form of the
14899   input.  The KEYWORD indicates which kind of expression we have
14900   encountered.  */
14901
14902static tree
14903cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14904{
14905  static const char *format;
14906  tree expr = NULL_TREE;
14907  const char *saved_message;
14908  bool saved_integral_constant_expression_p;
14909
14910  /* Initialize FORMAT the first time we get here.  */
14911  if (!format)
14912    format = "types may not be defined in `%s' expressions";
14913
14914  /* Types cannot be defined in a `sizeof' expression.  Save away the
14915     old message.  */
14916  saved_message = parser->type_definition_forbidden_message;
14917  /* And create the new one.  */
14918  parser->type_definition_forbidden_message
14919    = xmalloc (strlen (format)
14920	       + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14921	       + 1 /* `\0' */);
14922  sprintf ((char *) parser->type_definition_forbidden_message,
14923	   format, IDENTIFIER_POINTER (ridpointers[keyword]));
14924
14925  /* The restrictions on constant-expressions do not apply inside
14926     sizeof expressions.  */
14927  saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14928  parser->integral_constant_expression_p = false;
14929
14930  /* Do not actually evaluate the expression.  */
14931  ++skip_evaluation;
14932  /* If it's a `(', then we might be looking at the type-id
14933     construction.  */
14934  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14935    {
14936      tree type;
14937      bool saved_in_type_id_in_expr_p;
14938
14939      /* We can't be sure yet whether we're looking at a type-id or an
14940	 expression.  */
14941      cp_parser_parse_tentatively (parser);
14942      /* Consume the `('.  */
14943      cp_lexer_consume_token (parser->lexer);
14944      /* Parse the type-id.  */
14945      saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14946      parser->in_type_id_in_expr_p = true;
14947      type = cp_parser_type_id (parser);
14948      parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14949      /* Now, look for the trailing `)'.  */
14950      cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14951      /* If all went well, then we're done.  */
14952      if (cp_parser_parse_definitely (parser))
14953	{
14954	  /* Build a list of decl-specifiers; right now, we have only
14955	     a single type-specifier.  */
14956	  type = build_tree_list (NULL_TREE,
14957				  type);
14958
14959	  /* Call grokdeclarator to figure out what type this is.  */
14960	  expr = grokdeclarator (NULL_TREE,
14961				 type,
14962				 TYPENAME,
14963				 /*initialized=*/0,
14964				 /*attrlist=*/NULL);
14965	}
14966    }
14967
14968  /* If the type-id production did not work out, then we must be
14969     looking at the unary-expression production.  */
14970  if (!expr)
14971    expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14972  /* Go back to evaluating expressions.  */
14973  --skip_evaluation;
14974
14975  /* Free the message we created.  */
14976  free ((char *) parser->type_definition_forbidden_message);
14977  /* And restore the old one.  */
14978  parser->type_definition_forbidden_message = saved_message;
14979  parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14980
14981  return expr;
14982}
14983
14984/* If the current declaration has no declarator, return true.  */
14985
14986static bool
14987cp_parser_declares_only_class_p (cp_parser *parser)
14988{
14989  /* If the next token is a `;' or a `,' then there is no
14990     declarator.  */
14991  return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14992	  || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14993}
14994
14995/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14996   Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14997
14998static bool
14999cp_parser_friend_p (tree decl_specifiers)
15000{
15001  while (decl_specifiers)
15002    {
15003      /* See if this decl-specifier is `friend'.  */
15004      if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
15005	  && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
15006	return true;
15007
15008      /* Go on to the next decl-specifier.  */
15009      decl_specifiers = TREE_CHAIN (decl_specifiers);
15010    }
15011
15012  return false;
15013}
15014
15015/* If the next token is of the indicated TYPE, consume it.  Otherwise,
15016   issue an error message indicating that TOKEN_DESC was expected.
15017
15018   Returns the token consumed, if the token had the appropriate type.
15019   Otherwise, returns NULL.  */
15020
15021static cp_token *
15022cp_parser_require (cp_parser* parser,
15023                   enum cpp_ttype type,
15024                   const char* token_desc)
15025{
15026  if (cp_lexer_next_token_is (parser->lexer, type))
15027    return cp_lexer_consume_token (parser->lexer);
15028  else
15029    {
15030      /* Output the MESSAGE -- unless we're parsing tentatively.  */
15031      if (!cp_parser_simulate_error (parser))
15032	{
15033	  char *message = concat ("expected ", token_desc, NULL);
15034	  cp_parser_error (parser, message);
15035	  free (message);
15036	}
15037      return NULL;
15038    }
15039}
15040
15041/* Like cp_parser_require, except that tokens will be skipped until
15042   the desired token is found.  An error message is still produced if
15043   the next token is not as expected.  */
15044
15045static void
15046cp_parser_skip_until_found (cp_parser* parser,
15047                            enum cpp_ttype type,
15048                            const char* token_desc)
15049{
15050  cp_token *token;
15051  unsigned nesting_depth = 0;
15052
15053  if (cp_parser_require (parser, type, token_desc))
15054    return;
15055
15056  /* Skip tokens until the desired token is found.  */
15057  while (true)
15058    {
15059      /* Peek at the next token.  */
15060      token = cp_lexer_peek_token (parser->lexer);
15061      /* If we've reached the token we want, consume it and
15062	 stop.  */
15063      if (token->type == type && !nesting_depth)
15064	{
15065	  cp_lexer_consume_token (parser->lexer);
15066	  return;
15067	}
15068      /* If we've run out of tokens, stop.  */
15069      if (token->type == CPP_EOF)
15070	return;
15071      if (token->type == CPP_OPEN_BRACE
15072	  || token->type == CPP_OPEN_PAREN
15073	  || token->type == CPP_OPEN_SQUARE)
15074	++nesting_depth;
15075      else if (token->type == CPP_CLOSE_BRACE
15076	       || token->type == CPP_CLOSE_PAREN
15077	       || token->type == CPP_CLOSE_SQUARE)
15078	{
15079	  if (nesting_depth-- == 0)
15080	    return;
15081	}
15082      /* Consume this token.  */
15083      cp_lexer_consume_token (parser->lexer);
15084    }
15085}
15086
15087/* If the next token is the indicated keyword, consume it.  Otherwise,
15088   issue an error message indicating that TOKEN_DESC was expected.
15089
15090   Returns the token consumed, if the token had the appropriate type.
15091   Otherwise, returns NULL.  */
15092
15093static cp_token *
15094cp_parser_require_keyword (cp_parser* parser,
15095                           enum rid keyword,
15096                           const char* token_desc)
15097{
15098  cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15099
15100  if (token && token->keyword != keyword)
15101    {
15102      dyn_string_t error_msg;
15103
15104      /* Format the error message.  */
15105      error_msg = dyn_string_new (0);
15106      dyn_string_append_cstr (error_msg, "expected ");
15107      dyn_string_append_cstr (error_msg, token_desc);
15108      cp_parser_error (parser, error_msg->s);
15109      dyn_string_delete (error_msg);
15110      return NULL;
15111    }
15112
15113  return token;
15114}
15115
15116/* Returns TRUE iff TOKEN is a token that can begin the body of a
15117   function-definition.  */
15118
15119static bool
15120cp_parser_token_starts_function_definition_p (cp_token* token)
15121{
15122  return (/* An ordinary function-body begins with an `{'.  */
15123	  token->type == CPP_OPEN_BRACE
15124	  /* A ctor-initializer begins with a `:'.  */
15125	  || token->type == CPP_COLON
15126	  /* A function-try-block begins with `try'.  */
15127	  || token->keyword == RID_TRY
15128	  /* The named return value extension begins with `return'.  */
15129	  || token->keyword == RID_RETURN);
15130}
15131
15132/* Returns TRUE iff the next token is the ":" or "{" beginning a class
15133   definition.  */
15134
15135static bool
15136cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15137{
15138  cp_token *token;
15139
15140  token = cp_lexer_peek_token (parser->lexer);
15141  return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15142}
15143
15144/* Returns TRUE iff the next token is the "," or ">" ending a
15145   template-argument.   */
15146
15147static bool
15148cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15149{
15150  cp_token *token;
15151
15152  token = cp_lexer_peek_token (parser->lexer);
15153  return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15154}
15155
15156/* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15157   (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15158
15159static bool
15160cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15161						     size_t n)
15162{
15163  cp_token *token;
15164
15165  token = cp_lexer_peek_nth_token (parser->lexer, n);
15166  if (token->type == CPP_LESS)
15167    return true;
15168  /* Check for the sequence `<::' in the original code. It would be lexed as
15169     `[:', where `[' is a digraph, and there is no whitespace before
15170     `:'.  */
15171  if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15172    {
15173      cp_token *token2;
15174      token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15175      if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15176	return true;
15177    }
15178  return false;
15179}
15180
15181/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15182   or none_type otherwise.  */
15183
15184static enum tag_types
15185cp_parser_token_is_class_key (cp_token* token)
15186{
15187  switch (token->keyword)
15188    {
15189    case RID_CLASS:
15190      return class_type;
15191    case RID_STRUCT:
15192      return record_type;
15193    case RID_UNION:
15194      return union_type;
15195
15196    default:
15197      return none_type;
15198    }
15199}
15200
15201/* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15202
15203static void
15204cp_parser_check_class_key (enum tag_types class_key, tree type)
15205{
15206  if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15207    pedwarn ("`%s' tag used in naming `%#T'",
15208	    class_key == union_type ? "union"
15209	     : class_key == record_type ? "struct" : "class",
15210	     type);
15211}
15212
15213/* Issue an error message if DECL is redeclared with different
15214   access than its original declaration [class.access.spec/3].
15215   This applies to nested classes and nested class templates.
15216   [class.mem/1].  */
15217
15218static void cp_parser_check_access_in_redeclaration (tree decl)
15219{
15220  if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15221    return;
15222
15223  if ((TREE_PRIVATE (decl)
15224       != (current_access_specifier == access_private_node))
15225      || (TREE_PROTECTED (decl)
15226	  != (current_access_specifier == access_protected_node)))
15227    error ("%D redeclared with different access", decl);
15228}
15229
15230/* Look for the `template' keyword, as a syntactic disambiguator.
15231   Return TRUE iff it is present, in which case it will be
15232   consumed.  */
15233
15234static bool
15235cp_parser_optional_template_keyword (cp_parser *parser)
15236{
15237  if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15238    {
15239      /* The `template' keyword can only be used within templates;
15240	 outside templates the parser can always figure out what is a
15241	 template and what is not.  */
15242      if (!processing_template_decl)
15243	{
15244	  error ("`template' (as a disambiguator) is only allowed "
15245		 "within templates");
15246	  /* If this part of the token stream is rescanned, the same
15247	     error message would be generated.  So, we purge the token
15248	     from the stream.  */
15249	  cp_lexer_purge_token (parser->lexer);
15250	  return false;
15251	}
15252      else
15253	{
15254	  /* Consume the `template' keyword.  */
15255	  cp_lexer_consume_token (parser->lexer);
15256	  return true;
15257	}
15258    }
15259
15260  return false;
15261}
15262
15263/* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15264   set PARSER->SCOPE, and perform other related actions.  */
15265
15266static void
15267cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15268{
15269  tree value;
15270  tree check;
15271
15272  /* Get the stored value.  */
15273  value = cp_lexer_consume_token (parser->lexer)->value;
15274  /* Perform any access checks that were deferred.  */
15275  for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15276    perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15277  /* Set the scope from the stored value.  */
15278  parser->scope = TREE_VALUE (value);
15279  parser->qualifying_scope = TREE_TYPE (value);
15280  parser->object_scope = NULL_TREE;
15281}
15282
15283/* Add tokens to CACHE until an non-nested END token appears.  */
15284
15285static void
15286cp_parser_cache_group (cp_parser *parser,
15287		       cp_token_cache *cache,
15288		       enum cpp_ttype end,
15289		       unsigned depth)
15290{
15291  while (true)
15292    {
15293      cp_token *token;
15294
15295      /* Abort a parenthesized expression if we encounter a brace.  */
15296      if ((end == CPP_CLOSE_PAREN || depth == 0)
15297	  && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15298	return;
15299      /* If we've reached the end of the file, stop.  */
15300      if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15301	return;
15302      /* Consume the next token.  */
15303      token = cp_lexer_consume_token (parser->lexer);
15304      /* Add this token to the tokens we are saving.  */
15305      cp_token_cache_push_token (cache, token);
15306      /* See if it starts a new group.  */
15307      if (token->type == CPP_OPEN_BRACE)
15308	{
15309	  cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15310	  if (depth == 0)
15311	    return;
15312	}
15313      else if (token->type == CPP_OPEN_PAREN)
15314	cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15315      else if (token->type == end)
15316	return;
15317    }
15318}
15319
15320/* Begin parsing tentatively.  We always save tokens while parsing
15321   tentatively so that if the tentative parsing fails we can restore the
15322   tokens.  */
15323
15324static void
15325cp_parser_parse_tentatively (cp_parser* parser)
15326{
15327  /* Enter a new parsing context.  */
15328  parser->context = cp_parser_context_new (parser->context);
15329  /* Begin saving tokens.  */
15330  cp_lexer_save_tokens (parser->lexer);
15331  /* In order to avoid repetitive access control error messages,
15332     access checks are queued up until we are no longer parsing
15333     tentatively.  */
15334  push_deferring_access_checks (dk_deferred);
15335}
15336
15337/* Commit to the currently active tentative parse.  */
15338
15339static void
15340cp_parser_commit_to_tentative_parse (cp_parser* parser)
15341{
15342  cp_parser_context *context;
15343  cp_lexer *lexer;
15344
15345  /* Mark all of the levels as committed.  */
15346  lexer = parser->lexer;
15347  for (context = parser->context; context->next; context = context->next)
15348    {
15349      if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15350	break;
15351      context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15352      while (!cp_lexer_saving_tokens (lexer))
15353	lexer = lexer->next;
15354      cp_lexer_commit_tokens (lexer);
15355    }
15356}
15357
15358/* Abort the currently active tentative parse.  All consumed tokens
15359   will be rolled back, and no diagnostics will be issued.  */
15360
15361static void
15362cp_parser_abort_tentative_parse (cp_parser* parser)
15363{
15364  cp_parser_simulate_error (parser);
15365  /* Now, pretend that we want to see if the construct was
15366     successfully parsed.  */
15367  cp_parser_parse_definitely (parser);
15368}
15369
15370/* Stop parsing tentatively.  If a parse error has occurred, restore the
15371   token stream.  Otherwise, commit to the tokens we have consumed.
15372   Returns true if no error occurred; false otherwise.  */
15373
15374static bool
15375cp_parser_parse_definitely (cp_parser* parser)
15376{
15377  bool error_occurred;
15378  cp_parser_context *context;
15379
15380  /* Remember whether or not an error occurred, since we are about to
15381     destroy that information.  */
15382  error_occurred = cp_parser_error_occurred (parser);
15383  /* Remove the topmost context from the stack.  */
15384  context = parser->context;
15385  parser->context = context->next;
15386  /* If no parse errors occurred, commit to the tentative parse.  */
15387  if (!error_occurred)
15388    {
15389      /* Commit to the tokens read tentatively, unless that was
15390	 already done.  */
15391      if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15392	cp_lexer_commit_tokens (parser->lexer);
15393
15394      pop_to_parent_deferring_access_checks ();
15395    }
15396  /* Otherwise, if errors occurred, roll back our state so that things
15397     are just as they were before we began the tentative parse.  */
15398  else
15399    {
15400      cp_lexer_rollback_tokens (parser->lexer);
15401      pop_deferring_access_checks ();
15402    }
15403  /* Add the context to the front of the free list.  */
15404  context->next = cp_parser_context_free_list;
15405  cp_parser_context_free_list = context;
15406
15407  return !error_occurred;
15408}
15409
15410/* Returns true if we are parsing tentatively -- but have decided that
15411   we will stick with this tentative parse, even if errors occur.  */
15412
15413static bool
15414cp_parser_committed_to_tentative_parse (cp_parser* parser)
15415{
15416  return (cp_parser_parsing_tentatively (parser)
15417	  && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15418}
15419
15420/* Returns nonzero iff an error has occurred during the most recent
15421   tentative parse.  */
15422
15423static bool
15424cp_parser_error_occurred (cp_parser* parser)
15425{
15426  return (cp_parser_parsing_tentatively (parser)
15427	  && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15428}
15429
15430/* Returns nonzero if GNU extensions are allowed.  */
15431
15432static bool
15433cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15434{
15435  return parser->allow_gnu_extensions_p;
15436}
15437
15438
15439
15440/* The parser.  */
15441
15442static GTY (()) cp_parser *the_parser;
15443
15444/* External interface.  */
15445
15446/* Parse one entire translation unit.  */
15447
15448void
15449c_parse_file (void)
15450{
15451  bool error_occurred;
15452
15453  the_parser = cp_parser_new ();
15454  push_deferring_access_checks (flag_access_control
15455				? dk_no_deferred : dk_no_check);
15456  error_occurred = cp_parser_translation_unit (the_parser);
15457  the_parser = NULL;
15458}
15459
15460/* This variable must be provided by every front end.  */
15461
15462int yydebug;
15463
15464#include "gt-cp-parser.h"
15465